@intelligo-dev/chat 1.0.0-beta.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +6 -0
  3. package/README.md +116 -0
  4. package/dist/artifact-writer.d.ts +45 -0
  5. package/dist/artifact-writer.d.ts.map +1 -0
  6. package/dist/artifact-writer.js +77 -0
  7. package/dist/artifact-writer.js.map +1 -0
  8. package/dist/attachments.d.ts +56 -0
  9. package/dist/attachments.d.ts.map +1 -0
  10. package/dist/attachments.js +204 -0
  11. package/dist/attachments.js.map +1 -0
  12. package/dist/body.d.ts +72 -0
  13. package/dist/body.d.ts.map +1 -0
  14. package/dist/body.js +174 -0
  15. package/dist/body.js.map +1 -0
  16. package/dist/client.d.ts +65 -0
  17. package/dist/client.d.ts.map +1 -0
  18. package/dist/client.js +61 -0
  19. package/dist/client.js.map +1 -0
  20. package/dist/config.d.ts +322 -0
  21. package/dist/config.d.ts.map +1 -0
  22. package/dist/config.js +11 -0
  23. package/dist/config.js.map +1 -0
  24. package/dist/errors.d.ts +23 -0
  25. package/dist/errors.d.ts.map +1 -0
  26. package/dist/errors.js +41 -0
  27. package/dist/errors.js.map +1 -0
  28. package/dist/feedback.d.ts +22 -0
  29. package/dist/feedback.d.ts.map +1 -0
  30. package/dist/feedback.js +46 -0
  31. package/dist/feedback.js.map +1 -0
  32. package/dist/generation.d.ts +104 -0
  33. package/dist/generation.d.ts.map +1 -0
  34. package/dist/generation.js +85 -0
  35. package/dist/generation.js.map +1 -0
  36. package/dist/handler.d.ts +30 -0
  37. package/dist/handler.d.ts.map +1 -0
  38. package/dist/handler.js +913 -0
  39. package/dist/handler.js.map +1 -0
  40. package/dist/index.d.ts +31 -0
  41. package/dist/index.d.ts.map +1 -0
  42. package/dist/index.js +20 -0
  43. package/dist/index.js.map +1 -0
  44. package/dist/messages.d.ts +19 -0
  45. package/dist/messages.d.ts.map +1 -0
  46. package/dist/messages.js +34 -0
  47. package/dist/messages.js.map +1 -0
  48. package/dist/parts.d.ts +117 -0
  49. package/dist/parts.d.ts.map +1 -0
  50. package/dist/parts.js +13 -0
  51. package/dist/parts.js.map +1 -0
  52. package/dist/quota.d.ts +32 -0
  53. package/dist/quota.d.ts.map +1 -0
  54. package/dist/quota.js +81 -0
  55. package/dist/quota.js.map +1 -0
  56. package/dist/share.d.ts +24 -0
  57. package/dist/share.d.ts.map +1 -0
  58. package/dist/share.js +78 -0
  59. package/dist/share.js.map +1 -0
  60. package/dist/testing.d.ts +36 -0
  61. package/dist/testing.d.ts.map +1 -0
  62. package/dist/testing.js +82 -0
  63. package/dist/testing.js.map +1 -0
  64. package/dist/title.d.ts +7 -0
  65. package/dist/title.d.ts.map +1 -0
  66. package/dist/title.js +15 -0
  67. package/dist/title.js.map +1 -0
  68. package/dist/usage.d.ts +21 -0
  69. package/dist/usage.d.ts.map +1 -0
  70. package/dist/usage.js +35 -0
  71. package/dist/usage.js.map +1 -0
  72. package/dist/windowing.d.ts +38 -0
  73. package/dist/windowing.d.ts.map +1 -0
  74. package/dist/windowing.js +82 -0
  75. package/dist/windowing.js.map +1 -0
  76. package/package.json +78 -0
  77. package/src/artifact-writer.ts +114 -0
  78. package/src/attachments.ts +262 -0
  79. package/src/body.ts +236 -0
  80. package/src/client.ts +133 -0
  81. package/src/config.ts +376 -0
  82. package/src/errors.ts +80 -0
  83. package/src/feedback.ts +62 -0
  84. package/src/generation.ts +150 -0
  85. package/src/handler.ts +1164 -0
  86. package/src/index.ts +93 -0
  87. package/src/messages.ts +39 -0
  88. package/src/parts.ts +143 -0
  89. package/src/quota.ts +105 -0
  90. package/src/share.ts +103 -0
  91. package/src/testing.ts +150 -0
  92. package/src/title.ts +15 -0
  93. package/src/usage.ts +46 -0
  94. package/src/windowing.ts +110 -0
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 Intelligo
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/NOTICE ADDED
@@ -0,0 +1,6 @@
1
+ Intelligo
2
+ Copyright 2026 Turtuvshin Byambaa and the Intelligo contributors
3
+
4
+ This product is licensed under the Apache License, Version 2.0 (see
5
+ LICENSE). The Intelligo name and logo are trademarks; see TRADEMARK.md
6
+ in the source repository, https://github.com/intelligo-dev/intelligo.
package/README.md ADDED
@@ -0,0 +1,116 @@
1
+ # @intelligo-dev/chat
2
+
3
+ The AI-SDK-native chat transport: one Route Handler with auth, rate limits, feature gates, credits and persistence.
4
+
5
+ Part of [Intelligo](https://intelligo.dev), an application framework and
6
+ operational platform for vertical AI SaaS products. Every `@intelligo-dev/*`
7
+ package is released at one version and shares one database schema;
8
+ `pnpm dlx @intelligo-dev/cli@beta create my-app` installs the set an
9
+ application needs. Documentation:
10
+ [intelligo.dev/docs/packages/chat](https://intelligo.dev/docs/packages/chat).
11
+
12
+ ## Install
13
+
14
+ ```bash
15
+ pnpm add @intelligo-dev/chat@beta ai
16
+ ```
17
+
18
+ `ai` (the Vercel AI SDK) is a peer: the transport takes its tools, its models
19
+ and its stop conditions natively, and never wraps them.
20
+
21
+ ## Use
22
+
23
+ ```ts
24
+ // app/api/chat/route.ts
25
+ import { createChatHandler } from "@intelligo-dev/chat";
26
+ import { chatServerConfig } from "@/lib/chat-server-config";
27
+
28
+ export const { POST, DELETE } = createChatHandler(chatServerConfig);
29
+ ```
30
+
31
+ ```ts
32
+ // lib/chat-server-config.ts
33
+ import type { ChatServerConfig } from "@intelligo-dev/chat";
34
+ import { composeIntelligo, executions } from "@/lib/intelligo";
35
+
36
+ export const chatServerConfig: ChatServerConfig = {
37
+ executions,
38
+ onRequest: composeIntelligo,
39
+ model: { defaultId: "google/gemini-2.5-flash", resolve: getChatModel },
40
+ agent: { id: "assistant", systemPrompt: "You are a helpful assistant." },
41
+ };
42
+ ```
43
+
44
+ That is a working chat. Every turn goes through auth, the plan's rate limit,
45
+ the feature gate, conversation persistence (`@intelligo-dev/core/conversations`)
46
+ and the execution boundary (`executions.begin()` decides entitlement against
47
+ the model that is about to run, and settles exactly once).
48
+
49
+ ## The seams
50
+
51
+ Each optional field is something a real product needed and used to fork the
52
+ route to get:
53
+
54
+ | Field | What it decides |
55
+ | ----------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
56
+ | `resolveAgent(turn)` | Which agent runs — from the body, the row, a table; prompt, tools, model |
57
+ | `streamTurn(turn, prepared)` | Another runtime than `streamText` — a Mastra agent, an eve session — returning the AI SDK's chunks and the run's usage; everything else stays the transport's |
58
+ | `models` | The models a request may pick; anything else is `FEATURE_GATED` (`model_not_allowed`) |
59
+ | `prepareMessages(turn, msgs)` | What the model is shown — windowing, summaries, injected context |
60
+ | `agent.generation` | How the model samples — temperature, a token ceiling, a tool choice, a seed: an allowlist of the `streamText` options that do not touch settlement. `maxOutputTokens` defaults to the registered model's, the figure admission held |
61
+ | `attachments` | Which file parts are accepted; `mode: "stored"` uploads them through the storage port and signs URLs for the model only |
62
+ | `reasoning`, `sources` | Whether reasoning and source parts stream to the client |
63
+ | `messageMetadata` | `{ modelId, usage, finishedAt }` on the reply (default on) |
64
+ | `cors` | Origins an embedded widget may call from |
65
+ | `deriveTitle` | The conversation's title, sync or model-written |
66
+ | `persist` | Where the turn's messages go |
67
+ | `onTurn` | Telemetry: start, complete, fail, refuse, approval, feedback |
68
+ | `messages(request)` | Refusal copy in the caller's locale |
69
+ | `authenticate`, `rateLimit` | The defaults are the framework's; a worker or a test can replace them |
70
+
71
+ None carry product vocabulary; all close over the caller's tenancy, so the
72
+ model is never told which workspace it is in.
73
+
74
+ A tool reaches the client mid-turn through the turn: `turn.write()` sends a
75
+ `data-chat-*` part (a status line, a plan), and `createArtifactWriter(turn,
76
+ { kind, title })` streams a document into the chat's canvas — `append`
77
+ deltas, `finish({ documentId })`. A tool that calls a model itself — a
78
+ grounded search, a sub-agent on the same model — hands its tokens to
79
+ `turn.addUsage()`, and the turn settles them with its own. `sanitizeForShare` strips a transcript
80
+ for a public page; `recordChatFeedback` records a vote and tells the hook.
81
+ Stored attachments mount two more handlers, `createChatUploadHandler` and
82
+ `createChatAttachmentHandler`.
83
+
84
+ ## Refusals
85
+
86
+ Every non-2xx answer is `{ error, code, reasonCode? }` with one status per
87
+ code (`CHAT_ERROR_CODES`, `parseChatError`). Admission's refusals split by
88
+ who can fix them:
89
+
90
+ | Admission code | Answer | Whose problem |
91
+ | -------------------------------------------- | ---------------------------- | ------------------------------------------- |
92
+ | `insufficient_credits`, `allowance_depleted` | 402 `QUOTA_EXCEEDED` | The workspace's: upgrade or top up |
93
+ | `billing_not_configured` | 503 `BILLING_NOT_CONFIGURED` | The deployment's: no product or plans |
94
+ | `unknown_model` | 503 `MODEL_UNAVAILABLE` | The deployment's: the model id has no price |
95
+
96
+ The two 503s are logged at error level — `unknown_model` with the model id
97
+ — and `MODEL_UNAVAILABLE` answers with neutral copy (`modelUnavailable`)
98
+ rather than the engine's reason, which names the registry. `reasonCode`
99
+ carries the admission code either way.
100
+
101
+ `getChatQuotaState({ modelId })` is the same decision as a read, for a
102
+ page that wants to say so before a turn is spent; `getChatQuotaStates({
103
+ modelIds })` reads it once per model a picker offers.
104
+
105
+ ## Subpaths
106
+
107
+ - `@intelligo-dev/chat/client` — error codes, the quota-state shape,
108
+ `parseChatError`, the `data-chat-*` parts vocabulary (`ChatUIMessage`,
109
+ `isChatDataPart`) and `ChatModelOption`, for a client bundle. Imports
110
+ nothing at runtime.
111
+ - `@intelligo-dev/chat/testing` — `createStubLanguageModel`, a deterministic
112
+ model that streams with no API key.
113
+
114
+ ## Licence
115
+
116
+ Apache-2.0
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Stream a document into the chat canvas from inside a tool, as three
3
+ * calls over one `data-chat-artifact` part:
4
+ *
5
+ * const doc = createArtifactWriter(turn, { kind: "text", title });
6
+ * for (const chunk of chunks) doc.append(chunk);
7
+ * const saved = await saveDocument(actor, { id: doc.id, title, content, kind });
8
+ * doc.finish({ documentId: saved.id });
9
+ *
10
+ * Deltas are transient parts under the writer's `id` — the client
11
+ * accumulates them and nothing is persisted mid-stream. The `ready`
12
+ * part is persisted, so the message's card reopens the document from
13
+ * `documentId` after a reload; `error` is persisted too, so a failed
14
+ * document does not reload as a blank card.
15
+ */
16
+ import type { ChatDataChunk } from "./parts.js";
17
+ export type ArtifactWriterOptions = {
18
+ kind: string;
19
+ title: string;
20
+ /** The part id, and the default document id. A fresh UUID when omitted. */
21
+ id?: string;
22
+ };
23
+ export type ArtifactWriter = {
24
+ readonly id: string;
25
+ /** Stream one increment. The first call opens the canvas. */
26
+ append: (delta: string) => void;
27
+ /**
28
+ * Mark the document ready. `content` is persisted in the part only
29
+ * when passed — a document that lives in the documents table is
30
+ * reopened from `documentId` instead.
31
+ */
32
+ finish: (result?: {
33
+ documentId?: string;
34
+ version?: number;
35
+ content?: string;
36
+ title?: string;
37
+ }) => void;
38
+ fail: (error: unknown) => void;
39
+ /** Everything appended so far. */
40
+ readonly content: string;
41
+ };
42
+ export declare function createArtifactWriter(turn: {
43
+ write: (chunk: ChatDataChunk) => void;
44
+ }, options: ArtifactWriterOptions): ArtifactWriter;
45
+ //# sourceMappingURL=artifact-writer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"artifact-writer.d.ts","sourceRoot":"","sources":["../src/artifact-writer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAoB,aAAa,EAAE,MAAM,SAAS,CAAC;AAE/D,MAAM,MAAM,qBAAqB,GAAG;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,2EAA2E;IAC3E,EAAE,CAAC,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC3B,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,6DAA6D;IAC7D,MAAM,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAChC;;;;OAIG;IACH,MAAM,EAAE,CAAC,MAAM,CAAC,EAAE;QAChB,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,KAAK,CAAC,EAAE,MAAM,CAAC;KAChB,KAAK,IAAI,CAAC;IACX,IAAI,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;IAC/B,kCAAkC;IAClC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEF,wBAAgB,oBAAoB,CAClC,IAAI,EAAE;IAAE,KAAK,EAAE,CAAC,KAAK,EAAE,aAAa,KAAK,IAAI,CAAA;CAAE,EAC/C,OAAO,EAAE,qBAAqB,GAC7B,cAAc,CAiEhB"}
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Stream a document into the chat canvas from inside a tool, as three
3
+ * calls over one `data-chat-artifact` part:
4
+ *
5
+ * const doc = createArtifactWriter(turn, { kind: "text", title });
6
+ * for (const chunk of chunks) doc.append(chunk);
7
+ * const saved = await saveDocument(actor, { id: doc.id, title, content, kind });
8
+ * doc.finish({ documentId: saved.id });
9
+ *
10
+ * Deltas are transient parts under the writer's `id` — the client
11
+ * accumulates them and nothing is persisted mid-stream. The `ready`
12
+ * part is persisted, so the message's card reopens the document from
13
+ * `documentId` after a reload; `error` is persisted too, so a failed
14
+ * document does not reload as a blank card.
15
+ */
16
+ export function createArtifactWriter(turn, options) {
17
+ const id = options.id ?? crypto.randomUUID();
18
+ let title = options.title;
19
+ let content = "";
20
+ let opened = false;
21
+ let closed = false;
22
+ const emit = (data, transient) => {
23
+ turn.write({
24
+ type: "data-chat-artifact",
25
+ id,
26
+ data,
27
+ ...(transient ? { transient: true } : {}),
28
+ });
29
+ };
30
+ const base = () => ({
31
+ id,
32
+ kind: options.kind,
33
+ title,
34
+ status: "streaming",
35
+ });
36
+ return {
37
+ id,
38
+ get content() {
39
+ return content;
40
+ },
41
+ append(delta) {
42
+ if (closed)
43
+ return;
44
+ content += delta;
45
+ if (!opened) {
46
+ opened = true;
47
+ emit(base(), true);
48
+ }
49
+ emit({ ...base(), delta }, true);
50
+ },
51
+ finish(result = {}) {
52
+ if (closed)
53
+ return;
54
+ closed = true;
55
+ if (result.title)
56
+ title = result.title;
57
+ emit({
58
+ ...base(),
59
+ status: "ready",
60
+ ...(result.documentId ? { documentId: result.documentId } : {}),
61
+ ...(result.version !== undefined ? { version: result.version } : {}),
62
+ ...(result.content !== undefined ? { content: result.content } : {}),
63
+ }, false);
64
+ },
65
+ fail(error) {
66
+ if (closed)
67
+ return;
68
+ closed = true;
69
+ emit({
70
+ ...base(),
71
+ status: "error",
72
+ error: error instanceof Error ? error.message : String(error),
73
+ }, false);
74
+ },
75
+ };
76
+ }
77
+ //# sourceMappingURL=artifact-writer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"artifact-writer.js","sourceRoot":"","sources":["../src/artifact-writer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AA+BH,MAAM,UAAU,oBAAoB,CAClC,IAA+C,EAC/C,OAA8B;IAE9B,MAAM,EAAE,GAAG,OAAO,CAAC,EAAE,IAAI,MAAM,CAAC,UAAU,EAAE,CAAC;IAC7C,IAAI,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;IAC1B,IAAI,OAAO,GAAG,EAAE,CAAC;IACjB,IAAI,MAAM,GAAG,KAAK,CAAC;IACnB,IAAI,MAAM,GAAG,KAAK,CAAC;IAEnB,MAAM,IAAI,GAAG,CAAC,IAAsB,EAAE,SAAkB,EAAE,EAAE;QAC1D,IAAI,CAAC,KAAK,CAAC;YACT,IAAI,EAAE,oBAAoB;YAC1B,EAAE;YACF,IAAI;YACJ,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SAC1C,CAAC,CAAC;IACL,CAAC,CAAC;IAEF,MAAM,IAAI,GAAG,GAAqB,EAAE,CAAC,CAAC;QACpC,EAAE;QACF,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,KAAK;QACL,MAAM,EAAE,WAAW;KACpB,CAAC,CAAC;IAEH,OAAO;QACL,EAAE;QACF,IAAI,OAAO;YACT,OAAO,OAAO,CAAC;QACjB,CAAC;QACD,MAAM,CAAC,KAAK;YACV,IAAI,MAAM;gBAAE,OAAO;YACnB,OAAO,IAAI,KAAK,CAAC;YACjB,IAAI,CAAC,MAAM,EAAE,CAAC;gBACZ,MAAM,GAAG,IAAI,CAAC;gBACd,IAAI,CAAC,IAAI,EAAE,EAAE,IAAI,CAAC,CAAC;YACrB,CAAC;YACD,IAAI,CAAC,EAAE,GAAG,IAAI,EAAE,EAAE,KAAK,EAAE,EAAE,IAAI,CAAC,CAAC;QACnC,CAAC;QACD,MAAM,CAAC,MAAM,GAAG,EAAE;YAChB,IAAI,MAAM;gBAAE,OAAO;YACnB,MAAM,GAAG,IAAI,CAAC;YACd,IAAI,MAAM,CAAC,KAAK;gBAAE,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;YACvC,IAAI,CACF;gBACE,GAAG,IAAI,EAAE;gBACT,MAAM,EAAE,OAAO;gBACf,GAAG,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,MAAM,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;gBAC/D,GAAG,CAAC,MAAM,CAAC,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;gBACpE,GAAG,CAAC,MAAM,CAAC,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;aACrE,EACD,KAAK,CACN,CAAC;QACJ,CAAC;QACD,IAAI,CAAC,KAAK;YACR,IAAI,MAAM;gBAAE,OAAO;YACnB,MAAM,GAAG,IAAI,CAAC;YACd,IAAI,CACF;gBACE,GAAG,IAAI,EAAE;gBACT,MAAM,EAAE,OAAO;gBACf,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;aAC9D,EACD,KAAK,CACN,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Stored attachments: the upload route and the route that serves them.
3
+ *
4
+ * Inline attachments (the default policy) travel inside the message as
5
+ * data URLs and need nothing here. A deployment that wants files
6
+ * outside the transcript — larger than a message should carry, kept
7
+ * after the conversation is shared, extracted to text for the model —
8
+ * sets `attachments.mode: "stored"`, binds a storage adapter from its
9
+ * composition root, and mounts these two handlers:
10
+ *
11
+ * // app/api/chat/upload/route.ts
12
+ * export const { POST } = createChatUploadHandler(chatServerConfig);
13
+ * // app/api/chat/attachments/[id]/route.ts
14
+ * export const { GET } = createChatAttachmentHandler(chatServerConfig);
15
+ *
16
+ * The upload answers `{ id, url, filename, mediaType, size }`; the
17
+ * composer puts `url` in the file part; the transport signs it for the
18
+ * model and persists the app URL, which this route redirects to a
19
+ * fresh signed URL for anyone in the workspace who can open the
20
+ * conversation. No signed URL is ever persisted or shared.
21
+ */
22
+ import type { ChatServerConfig } from "./config.js";
23
+ export type ChatUploadResult = {
24
+ id: string;
25
+ url: string;
26
+ filename: string;
27
+ mediaType: string;
28
+ size: number;
29
+ };
30
+ export type ChatUploadHandler = {
31
+ POST: (request: Request) => Promise<Response>;
32
+ };
33
+ export type ChatAttachmentHandler = {
34
+ GET: (request: Request, context: {
35
+ params: {
36
+ id: string;
37
+ } | Promise<{
38
+ id: string;
39
+ }>;
40
+ }) => Promise<Response>;
41
+ };
42
+ /**
43
+ * `POST multipart/form-data` with one `file` field → an attachment row
44
+ * and the URL the composer puts in the file part. Refuses a type the
45
+ * policy does not accept, a file over `maxBytes`, and anything without
46
+ * a session.
47
+ */
48
+ export declare function createChatUploadHandler(config: ChatServerConfig): ChatUploadHandler;
49
+ /**
50
+ * `GET /api/chat/attachments/[id]` → a redirect to a short-lived signed
51
+ * URL, for anyone signed in to the attachment's workspace. The app URL
52
+ * is what the transcript persists, so it must keep working after the
53
+ * signed one expires.
54
+ */
55
+ export declare function createChatAttachmentHandler(config: ChatServerConfig): ChatAttachmentHandler;
56
+ //# sourceMappingURL=attachments.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attachments.d.ts","sourceRoot":"","sources":["../src/attachments.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAoBH,OAAO,KAAK,EAAa,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAO5D,MAAM,MAAM,gBAAgB,GAAG;IAC7B,EAAE,EAAE,MAAM,CAAC;IACX,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;CAC/C,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IAClC,GAAG,EAAE,CACH,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE;QAAE,MAAM,EAAE;YAAE,EAAE,EAAE,MAAM,CAAA;SAAE,GAAG,OAAO,CAAC;YAAE,EAAE,EAAE,MAAM,CAAA;SAAE,CAAC,CAAA;KAAE,KAC1D,OAAO,CAAC,QAAQ,CAAC,CAAC;CACxB,CAAC;AAgBF;;;;;GAKG;AACH,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,gBAAgB,GACvB,iBAAiB,CAqHnB;AAED;;;;;GAKG;AACH,wBAAgB,2BAA2B,CACzC,MAAM,EAAE,gBAAgB,GACvB,qBAAqB,CA8CvB"}