Repository navigation
Commit bf76922
feat(console-ai): an image or PDF attached in the AI chat is uploaded as the user and sent to the agent as a file part (#34459)
Part of #32749 — the console half of ruling A. The agent half (turning
these file parts into model input, and putting the file into a record's
file field) is cloud code and keeps the card open.
Clause-②: no
Metadata-protocol: none
## 维护者速读
- 改了什么:控制台「问 AI」聊天里附上的图片(PNG/JPEG/GIF/WebP)和
PDF,现在以当前用户身份上传到存储(不绑定任何记录),并作为文件部件随这一轮消息发给 agent,带上 `sys_file`
id;输入框显示附件按钮和附件卡片。表格附件照旧在页面里解析。
- 为什么改:裁决 A(#32749):员工把发票照片发给自带 AI,不该再收到「只支持表格类附件,内容没有读取」;同一份上传以后由 agent
直接挂到记录的文件字段上,不用再选第二次。
- 风险与回滚:只动控制台聊天的发送路径和 `useObjectChat().sendMessage` 的一个可选参数;agent
端(cloud)还没读这些文件部件之前,模型只看到文件名和「已作为文件发送」的说明。回滚即 revert 本 PR。
- 先看:`ui/packages/app-shell/src/console/ai/chatAttachments.ts`(类型、10 MiB
上限、上传失败的处理);`AiChatPage.tsx` 的
`doSend`;`ui/packages/plugin-chatbot/src/useObjectChat.ts` 的
`sendMessage`。
- 两分钟验证:`pnpm --dir ui exec vitest run
packages/app-shell/src/console/ai/__tests__/AiChatPage.attachmentsUpload-32749.test.tsx`,预期
5 个用例全部通过。
- 需要你做的:无,席位落地。
## What changes
- **Composer** (`AiChatPage`): the chat's `ChatbotEnhanced` now gets
`enableFileUpload`, so the attach button and the attachment chips show.
Before, a file pasted or dropped into the composer was attached with no
chip, and there was no button to pick one.
- **Send path** (`doSend` in `AiChatPage`, helpers in the new
`chatAttachments.ts`):
- spreadsheets (`.xlsx .xlsm .csv .tsv .txt`) keep the in-page parse and
brief, unchanged;
- an image (`image/png`, `image/jpeg`, `image/gif`, `image/webp`) or a
PDF (`application/pdf`) of at most 10 MiB is uploaded through the
console's ambient upload adapter (`useUpload()` from
`@object-ui/fields`, which the console mounts with
`createObjectStackUploadAdapter`), as the signed-in user, and completed
without `attachTo`, so no record holds it. Each uploaded file becomes
one file part on the user turn (shape below), and the turn's text names
the files it sent;
- any other file, a file over the cap, and a file whose upload failed is
not sent; the turn's text names it and says why ("over the 10 MB cap",
"type not supported", "upload failed", each "content not read"). The
turn still sends. The old blanket "only spreadsheets are read; I did not
read these files" line is gone;
- a spreadsheet beside an image: the sheet is briefed and the image
uploaded, in one turn.
- **`@object-ui/plugin-chatbot`**: `useObjectChat().sendMessage(content,
files?, fileParts?)` takes an optional third argument, `fileParts` (new
exported type `ChatFilePart`, declared structurally so the package does
not republish the pinned AI SDK type). The API mode passes them to the
AI SDK's `sendMessage({ text, files })`, which puts them in the user
message before the text part. Two-argument calls send exactly what they
sent before (`{ text }`). The second argument, `files: File[]`, was
never sent and still is not; its doc now says so.
## Seam for the agent half
What the cloud agent receives, in the chat route's request body
(`messages[]`, last `role: 'user'` message), per uploaded file:
```json
{
"type": "file",
"mediaType": "image/png",
"filename": "receipt.png",
"url": "https://HOST/api/v1/storage/files/FILE_ID",
"providerMetadata": { "objectstack": { "fileId": "FILE_ID" } }
}
```
- **Where:** `parts` of the user message, before its `text` part (the AI
SDK's order), one part per uploaded file, in attach order.
`AiMessageSchema.parts` is `z.array(z.unknown())`, so no spec change.
- **`fileId`:** `providerMetadata.objectstack.fileId`, the `sys_file`
id. The AI SDK keeps `providerMetadata` on the part as sent, and the
transport sends `messages` verbatim, so it reaches the route; the test
reads it off the captured request body.
- **`url`:** the storage service's stable download endpoint,
`/api/v1/storage/files/FILE_ID` as the upload adapter answers it, made
absolute against the page's location. Never a data URL. The endpoint is
the governed download door (302 to a short-lived signed URL for a caller
who may read the file), so a model provider cannot fetch it: the agent
reads the bytes as the user, by `fileId`, and converts the part for its
model.
- **`mediaType`:** one of `image/png`, `image/jpeg`, `image/gif`,
`image/webp`, `application/pdf`. The browser's type, or, when the
browser gives none, the one the extension names.
- **The stored file:** `sys_file` row with `owner_id` = the user,
`scope: 'user'` (the server default), `status: 'committed'` (the upload
was completed, so the download door serves it and a later record write
may name it), no owning record. It is "pending" only in the ruling's
sense: no record has claimed it yet. A record write by the same user
naming it in a file field claims it (the existing file-reference
lifecycle); what binds it to "this user, this conversation" for the
attach step is the agent half's, after #32686.
- **Caps:** 10 MiB per image and per PDF. PDFs take the same 10 MiB as
images: the ruling gives no PDF cap, the composer already refuses any
file over 10 MiB before the send sees it (`maxFileSize` default), and
one cap keeps the rule one sentence; a larger PDF still goes onto a
record through its file field. No cap on the count per turn.
- **Text:** the user text carries a line naming the files sent (`(用户附上了
N 个文件:NAMES — 已作为文件随本条消息发送。)`) and one line per not-sent group (over cap
/ unsupported type / upload failed, each naming the files and saying the
content was not read). A model that cannot read images sees the names
only — the ruling's attached condition (say so plainly, never invent a
value from a file name) is the agent half's.
## Tests
`ui/packages/app-shell/src/console/ai/__tests__/AiChatPage.attachmentsUpload-32749.test.tsx`
mounts the real `AiChatPage` send path with the real
`useChatConversation` and the real `useObjectChat` transport, a `fetch`
stub capturing the chat POST, and a test upload adapter through the real
`UploadProvider`:
1. an image and a PDF are uploaded, and the user message carries exactly
two file parts with the URL and `providerMetadata.objectstack.fileId`
above;
2. CONTROL: a spreadsheet is parsed (parser called once), nothing is
uploaded, no file part;
3. a 10 MiB + 1 byte JPEG and a `.docx` are not uploaded and are named
in the text as over the cap / unsupported;
4. one upload failing: it is named as failed, and the turn still sends
the other file's part;
5. a spreadsheet beside an image: parsed once, uploaded once, one file
part.
<details><summary>Runs (head 2b95bb8 for the file list; test runs on
the branch's code before the main merge, which touched only
objectql)</summary>
```
vitest run packages/app-shell/src/console/ai/__tests__/AiChatPage.attachmentsUpload-32749.test.tsx
Tests 5 passed (5) · VERDICT command-exit 0
vitest run packages/plugin-chatbot/ packages/app-shell/src/console/ai/
Test Files 156 passed (156) · Tests 1362 passed (1362) · VERDICT command-exit 0 (before the 5th pin was added)
pnpm --filter @object-ui/plugin-chatbot run type-check VERDICT command-exit 0
pnpm --filter @object-ui/app-shell run type-check VERDICT command-exit 0 (tsconfig.test.json includes src/**/*.test.tsx)
eslint on the five changed ui files 0 errors (warnings all pre-existing)
```
</details>
<details><summary>Ablation (each mutation landed on disk, tests read the
source through the vitest alias, restored to HEAD)</summary>
```
A1 AiChatPage: sendMessage(text + chatAttachmentNotes(outcome), undefined, outcome.parts)
-> sendMessage(text + chatAttachmentNotes(outcome))
ablation-replace: ok mutation landed: anchor 1 -> 0, blob 78062f0 -> 3ce0c5693378
× uploads an image and a PDF and sends one file part per file, carrying its sys_file id
AssertionError: expected [] to deeply equal [ { type: 'file', …(4) }, …(1) ]
× names a failed upload as not read, and still sends the turn with the file that did upload
AssertionError: expected [] to deeply equal [ 'file-ok.png' ]
Tests 2 failed | 2 passed (4)
ablation-replace: ok restored: blob == HEAD (78062f0) and `git diff HEAD` is empty
A2 useObjectChat: ? { text: trimmed, files: fileParts.map((part) => ({ ...part })) } -> ? { text: trimmed }
ablation-replace: ok mutation landed: anchor 1 -> 0, blob 928647e -> 301ea3a70cce
the same two tests fail with the same two assertions; Tests 2 failed | 2 passed (4)
ablation-replace: ok restored: blob == HEAD (928647e) and `git diff HEAD` is empty
```
The control (spreadsheet) and the disclosure-only test stay green under
both mutations, as they should: neither sends a file part.
</details>
## Acceptance notes
- The claim's file surface names `AiChatPage.tsx`, its tests and a
storage-helper call site. This PR also edits
`ui/packages/plugin-chatbot/src/useObjectChat.ts` (plus one barrel
export): `useObjectChat().sendMessage` dropped every file it was handed,
so no console-only change could put a part on the turn. The change is
one optional argument; existing calls are byte-identical on the wire.
- The changeset is in the root `.changeset/`, not `ui/.changeset/`:
`ui/AGENTS.md` says the root config does not read `ui/.changeset/` and
no new entries go there.
- The attachment notes are Chinese whatever the conversation's language,
like the spreadsheet brief and the line they replace; moving the whole
attachment brief onto the conversation-language resolver
(`outboundAgentText.ts`) is not done here. Follow-up: #34461.
- No busy state while uploads run: the composer clears, and the user
bubble appears when the uploads settle (seconds for a large photo). A
second message sent meanwhile can reach the agent first. Follow-up:
#34462 (introduced here).
- The composer refuses a file over its `maxFileSize` (10 MiB) with only
a `console.warn`; the send's own cap is the guard for any other entry.
On main before this PR; follow-up: #34463.
## Verification
- `node scripts/pm/dispatch-gates.mjs --ran`: 39 derived, 39 run, 0
NOT-MEASURED, every one exit 0 (a derived zero).
- CI runs the rest (Lint UI, Type Check UI, Test UI, Build & E2E UI and
the repo gates); its result is read at the final head.
## Human Minimum
- [x] 1. Schemas start as Zod; `packages/spec` holds no runtime logic.
(spec untouched)
- [x] 2. Every name I added uses the casing for its kind.
- [x] 3. No formatter ran; eslint on the changed files reports 0 errors
(repo-wide `pnpm lint` is CI's).
- [x] 4. One issue, one branch off an up-to-date `main`, one pull
request.
- [x] 5. A change that publishes carries a changeset, and both
declaration lines are filled in here and in the changeset.
- [x] 6. `packages/spec` touched: n/a.
- [x] 7. No hand edits to `content/docs/references/`,
`packages/*/CHANGELOG.md` or `content/docs/releases/`.
- [x] 8. Title, body and comments are in English (the maintainer
quick-read is Chinese by the repo's rule).
- [x] The *Before Submitting* list in CONTRIBUTING.md is done.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01FEkQ5obaLbqgzRUR9xXfft)_
<!-- signed: repo:objectstack#1 · os-project-manager ·
session_01FEkQ5obaLbqgzRUR9xXfft -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent ea28882 commit bf76922
6 files changed
Lines changed: 519 additions & 18 deletions
File tree
- .changeset
- ui/packages
- app-shell/src/console/ai
- __tests__
- plugin-chatbot/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
155 | 155 | | |
156 | 156 | | |
157 | 157 | | |
| 158 | + | |
| 159 | + | |
158 | 160 | | |
159 | 161 | | |
160 | 162 | | |
| |||
2428 | 2430 | | |
2429 | 2431 | | |
2430 | 2432 | | |
2431 | | - | |
2432 | | - | |
| 2433 | + | |
| 2434 | + | |
| 2435 | + | |
| 2436 | + | |
| 2437 | + | |
| 2438 | + | |
| 2439 | + | |
| 2440 | + | |
| 2441 | + | |
| 2442 | + | |
| 2443 | + | |
| 2444 | + | |
| 2445 | + | |
| 2446 | + | |
| 2447 | + | |
2433 | 2448 | | |
2434 | 2449 | | |
2435 | 2450 | | |
2436 | | - | |
| 2451 | + | |
2437 | 2452 | | |
2438 | 2453 | | |
2439 | | - | |
2440 | | - | |
2441 | | - | |
2442 | | - | |
2443 | | - | |
2444 | | - | |
2445 | | - | |
| 2454 | + | |
2446 | 2455 | | |
2447 | 2456 | | |
2448 | 2457 | | |
| |||
2474 | 2483 | | |
2475 | 2484 | | |
2476 | 2485 | | |
2477 | | - | |
2478 | | - | |
2479 | | - | |
| 2486 | + | |
| 2487 | + | |
| 2488 | + | |
2480 | 2489 | | |
2481 | 2490 | | |
2482 | 2491 | | |
2483 | | - | |
| 2492 | + | |
2484 | 2493 | | |
2485 | 2494 | | |
2486 | 2495 | | |
| |||
3169 | 3178 | | |
3170 | 3179 | | |
3171 | 3180 | | |
| 3181 | + | |
| 3182 | + | |
| 3183 | + | |
| 3184 | + | |
3172 | 3185 | | |
3173 | 3186 | | |
3174 | 3187 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
0 commit comments