@intelligo-dev/chat 1.0.0-beta.5

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 (51) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +75 -0
  3. package/dist/body.d.ts +41 -0
  4. package/dist/body.d.ts.map +1 -0
  5. package/dist/body.js +113 -0
  6. package/dist/body.js.map +1 -0
  7. package/dist/client.d.ts +56 -0
  8. package/dist/client.d.ts.map +1 -0
  9. package/dist/client.js +62 -0
  10. package/dist/client.js.map +1 -0
  11. package/dist/config.d.ts +195 -0
  12. package/dist/config.d.ts.map +1 -0
  13. package/dist/config.js +20 -0
  14. package/dist/config.js.map +1 -0
  15. package/dist/errors.d.ts +23 -0
  16. package/dist/errors.d.ts.map +1 -0
  17. package/dist/errors.js +39 -0
  18. package/dist/errors.js.map +1 -0
  19. package/dist/handler.d.ts +30 -0
  20. package/dist/handler.d.ts.map +1 -0
  21. package/dist/handler.js +544 -0
  22. package/dist/handler.js.map +1 -0
  23. package/dist/index.d.ts +30 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +24 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/messages.d.ts +19 -0
  28. package/dist/messages.d.ts.map +1 -0
  29. package/dist/messages.js +34 -0
  30. package/dist/messages.js.map +1 -0
  31. package/dist/quota.d.ts +24 -0
  32. package/dist/quota.d.ts.map +1 -0
  33. package/dist/quota.js +41 -0
  34. package/dist/quota.js.map +1 -0
  35. package/dist/testing.d.ts +43 -0
  36. package/dist/testing.d.ts.map +1 -0
  37. package/dist/testing.js +89 -0
  38. package/dist/testing.js.map +1 -0
  39. package/dist/title.d.ts +7 -0
  40. package/dist/title.d.ts.map +1 -0
  41. package/dist/title.js +15 -0
  42. package/dist/title.js.map +1 -0
  43. package/dist/usage.d.ts +19 -0
  44. package/dist/usage.d.ts.map +1 -0
  45. package/dist/usage.js +26 -0
  46. package/dist/usage.js.map +1 -0
  47. package/dist/windowing.d.ts +39 -0
  48. package/dist/windowing.d.ts.map +1 -0
  49. package/dist/windowing.js +80 -0
  50. package/dist/windowing.js.map +1 -0
  51. package/package.json +63 -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/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # @intelligo-dev/chat
2
+
3
+ The AI-SDK-native chat transport: a Route Handler as a function.
4
+
5
+ Part of [Intelligo](https://github.com/intelligo-mn/framework), an application
6
+ framework and operational platform for vertical AI SaaS products. This package
7
+ is published from that repository and is not meant to be used on its own.
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ pnpm add @intelligo-dev/chat ai
13
+ ```
14
+
15
+ `ai` (the Vercel AI SDK) is a peer: the transport takes its tools, its models
16
+ and its stop conditions natively, and never wraps them (ADR-0003).
17
+
18
+ ## Use
19
+
20
+ ```ts
21
+ // app/api/chat/route.ts
22
+ import { createChatHandler } from "@intelligo-dev/chat";
23
+ import { chatServerConfig } from "@/lib/chat-server-config";
24
+
25
+ export const { POST, DELETE } = createChatHandler(chatServerConfig);
26
+ ```
27
+
28
+ ```ts
29
+ // lib/chat-server-config.ts
30
+ import type { ChatServerConfig } from "@intelligo-dev/chat";
31
+ import { composeIntelligo, executions } from "@/lib/intelligo";
32
+
33
+ export const chatServerConfig: ChatServerConfig = {
34
+ executions,
35
+ onRequest: composeIntelligo,
36
+ model: { defaultId: "google/gemini-2.5-flash", resolve: getChatModel },
37
+ agent: { id: "assistant", systemPrompt: "You are a helpful assistant." },
38
+ };
39
+ ```
40
+
41
+ That is a working chat. Every turn goes through auth, the plan's rate limit,
42
+ the feature gate, conversation persistence (`@intelligo-dev/core/conversations`)
43
+ and the execution boundary (`executions.begin()` decides entitlement against
44
+ the model that is about to run, and settles exactly once).
45
+
46
+ ## The seams
47
+
48
+ Each optional field is something a real product needed and used to fork the
49
+ route to get:
50
+
51
+ | Field | What it decides |
52
+ | ----------------------------- | ------------------------------------------------------------------------ |
53
+ | `resolveAgent(turn)` | Which agent runs — from the body, the row, a table; prompt, tools, model |
54
+ | `prepareMessages(turn, msgs)` | What the model is shown — windowing, summaries, injected context |
55
+ | `attachments` | Which file parts are accepted |
56
+ | `reasoning` | Whether reasoning parts stream to the client |
57
+ | `deriveTitle` | The conversation's title, sync or model-written |
58
+ | `persist` | Where the turn's messages go |
59
+ | `onTurn` | Telemetry: start, complete, fail, refuse |
60
+ | `messages(request)` | Refusal copy in the caller's locale |
61
+ | `authenticate`, `rateLimit` | The defaults are the framework's; a worker or a test can replace them |
62
+
63
+ None carry product vocabulary; all close over the caller's tenancy, so the
64
+ model is never told which workspace it is in.
65
+
66
+ ## Subpaths
67
+
68
+ - `@intelligo-dev/chat/client` — error codes, the quota-state shape and
69
+ `parseChatError`, for a client bundle. Imports nothing.
70
+ - `@intelligo-dev/chat/testing` — `createStubLanguageModel`, a deterministic
71
+ model that streams with no API key.
72
+
73
+ ## Licence
74
+
75
+ Apache-2.0
package/dist/body.d.ts ADDED
@@ -0,0 +1,41 @@
1
+ /**
2
+ * The request body, validated by hand.
3
+ *
4
+ * Not zod: a schema library in a package's public surface would have
5
+ * to be a peer (its `instanceof` fails across copies), and the body
6
+ * has four fields. The shape is the AI SDK's `DefaultChatTransport`
7
+ * one — `id`, `messages`, `trigger`, `messageId` — plus whatever the
8
+ * application's transport added, which is handed back untouched as
9
+ * `extra` for `resolveAgent` to read (an `agentId`, a model choice).
10
+ */
11
+ import type { UIMessage } from "ai";
12
+ export type ChatAttachmentPolicy = {
13
+ /** Media types a file part may carry, e.g. `["image/jpeg", "image/png"]`. */
14
+ accept: readonly string[];
15
+ /** Largest data-URL payload accepted, in bytes. Unbounded when omitted. */
16
+ maxBytes?: number;
17
+ };
18
+ export type ChatBody = {
19
+ id: string;
20
+ messages: UIMessage[];
21
+ trigger: "submit-message" | "regenerate-message" | undefined;
22
+ messageId: string | undefined;
23
+ /** Fields the application's transport added beyond the SDK's own. */
24
+ extra: Record<string, unknown>;
25
+ };
26
+ export type ChatBodyRejection = {
27
+ key: "invalidBody" | "messageTooLong" | "attachmentRejected";
28
+ params?: Record<string, string | number>;
29
+ };
30
+ export type ParsedChatBody = {
31
+ ok: true;
32
+ body: ChatBody;
33
+ } | {
34
+ ok: false;
35
+ rejection: ChatBodyRejection;
36
+ };
37
+ export declare function parseChatBody(json: unknown, options: {
38
+ maxMessageLength: number;
39
+ attachments: ChatAttachmentPolicy | false;
40
+ }): ParsedChatBody;
41
+ //# sourceMappingURL=body.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"body.d.ts","sourceRoot":"","sources":["../src/body.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,IAAI,CAAC;AAIpC,MAAM,MAAM,oBAAoB,GAAG;IACjC,6EAA6E;IAC7E,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC;IAC1B,2EAA2E;IAC3E,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,QAAQ,GAAG;IACrB,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,EAAE,SAAS,EAAE,CAAC;IACtB,OAAO,EAAE,gBAAgB,GAAG,oBAAoB,GAAG,SAAS,CAAC;IAC7D,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC;IAC9B,qEAAqE;IACrE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAChC,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG;IAC9B,GAAG,EAAE,aAAa,GAAG,gBAAgB,GAAG,oBAAoB,CAAC;IAC7D,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC;CAC1C,CAAC;AAEF,MAAM,MAAM,cAAc,GACtB;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,IAAI,EAAE,QAAQ,CAAA;CAAE,GAC5B;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,SAAS,EAAE,iBAAiB,CAAA;CAAE,CAAC;AAoDhD,wBAAgB,aAAa,CAC3B,IAAI,EAAE,OAAO,EACb,OAAO,EAAE;IACP,gBAAgB,EAAE,MAAM,CAAC;IACzB,WAAW,EAAE,oBAAoB,GAAG,KAAK,CAAC;CAC3C,GACA,cAAc,CAsDhB"}
package/dist/body.js ADDED
@@ -0,0 +1,113 @@
1
+ /**
2
+ * The request body, validated by hand.
3
+ *
4
+ * Not zod: a schema library in a package's public surface would have
5
+ * to be a peer (its `instanceof` fails across copies), and the body
6
+ * has four fields. The shape is the AI SDK's `DefaultChatTransport`
7
+ * one — `id`, `messages`, `trigger`, `messageId` — plus whatever the
8
+ * application's transport added, which is handed back untouched as
9
+ * `extra` for `resolveAgent` to read (an `agentId`, a model choice).
10
+ */
11
+ import { extractText } from "./windowing.js";
12
+ const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
13
+ const ROLES = new Set(["system", "user", "assistant"]);
14
+ const SDK_FIELDS = new Set(["id", "messages", "trigger", "messageId"]);
15
+ function isRecord(value) {
16
+ return typeof value === "object" && value !== null && !Array.isArray(value);
17
+ }
18
+ function isUIMessage(value) {
19
+ if (!isRecord(value))
20
+ return false;
21
+ if (typeof value.id !== "string" || !value.id)
22
+ return false;
23
+ if (typeof value.role !== "string" || !ROLES.has(value.role))
24
+ return false;
25
+ if (!Array.isArray(value.parts))
26
+ return false;
27
+ return value.parts.every((part) => isRecord(part) && typeof part.type === "string");
28
+ }
29
+ function dataUrlBytes(url) {
30
+ if (!url.startsWith("data:"))
31
+ return null;
32
+ const comma = url.indexOf(",");
33
+ if (comma === -1)
34
+ return null;
35
+ const payload = url.slice(comma + 1);
36
+ return url.slice(0, comma).endsWith(";base64")
37
+ ? Math.floor((payload.length * 3) / 4)
38
+ : payload.length;
39
+ }
40
+ function rejectedAttachment(message, policy) {
41
+ for (const part of message.parts) {
42
+ if (part.type !== "file")
43
+ continue;
44
+ if (policy === false)
45
+ return true;
46
+ const file = part;
47
+ if (typeof file.mediaType !== "string" ||
48
+ !policy.accept.includes(file.mediaType)) {
49
+ return true;
50
+ }
51
+ if (policy.maxBytes !== undefined && typeof file.url === "string") {
52
+ const bytes = dataUrlBytes(file.url);
53
+ if (bytes !== null && bytes > policy.maxBytes)
54
+ return true;
55
+ }
56
+ }
57
+ return false;
58
+ }
59
+ export function parseChatBody(json, options) {
60
+ const invalid = {
61
+ ok: false,
62
+ rejection: { key: "invalidBody" },
63
+ };
64
+ if (!isRecord(json))
65
+ return invalid;
66
+ if (typeof json.id !== "string" || !UUID.test(json.id))
67
+ return invalid;
68
+ if (!Array.isArray(json.messages) || json.messages.length === 0)
69
+ return invalid;
70
+ if (!json.messages.every(isUIMessage))
71
+ return invalid;
72
+ if (json.trigger !== undefined &&
73
+ json.trigger !== "submit-message" &&
74
+ json.trigger !== "regenerate-message") {
75
+ return invalid;
76
+ }
77
+ if (json.messageId !== undefined && typeof json.messageId !== "string") {
78
+ return invalid;
79
+ }
80
+ const messages = json.messages;
81
+ const last = messages[messages.length - 1];
82
+ if (last.role === "user") {
83
+ const length = extractText(last.parts).length;
84
+ if (length > options.maxMessageLength) {
85
+ return {
86
+ ok: false,
87
+ rejection: {
88
+ key: "messageTooLong",
89
+ params: { max: options.maxMessageLength },
90
+ },
91
+ };
92
+ }
93
+ if (rejectedAttachment(last, options.attachments)) {
94
+ return { ok: false, rejection: { key: "attachmentRejected" } };
95
+ }
96
+ }
97
+ const extra = {};
98
+ for (const [key, value] of Object.entries(json)) {
99
+ if (!SDK_FIELDS.has(key))
100
+ extra[key] = value;
101
+ }
102
+ return {
103
+ ok: true,
104
+ body: {
105
+ id: json.id,
106
+ messages,
107
+ trigger: json.trigger,
108
+ messageId: json.messageId,
109
+ extra,
110
+ },
111
+ };
112
+ }
113
+ //# sourceMappingURL=body.js.map
@@ -0,0 +1 @@
1
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AAE;oBACT,GAAG,EAAE,gBAAgB;oBACrB,MAAM,EAAE,EAAE,GAAG,EAAE,OAAO,CAAC,gBAAgB,EAAE;iBAC1C;aACF,CAAC;QACJ,CAAC;QACD,IAAI,kBAAkB,CAAC,IAAI,EAAE,OAAO,CAAC,WAAW,CAAC,EAAE,CAAC;YAClD,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,SAAS,EAAE,EAAE,GAAG,EAAE,oBAAoB,EAAE,EAAE,CAAC;QACjE,CAAC;IACH,CAAC;IAED,MAAM,KAAK,GAA4B,EAAE,CAAC;IAC1C,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;QAChD,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC;YAAE,KAAK,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;IAC/C,CAAC;IAED,OAAO;QACL,EAAE,EAAE,IAAI;QACR,IAAI,EAAE;YACJ,EAAE,EAAE,IAAI,CAAC,EAAE;YACX,QAAQ;YACR,OAAO,EAAE,IAAI,CAAC,OAA8B;YAC5C,SAAS,EAAE,IAAI,CAAC,SAA+B;YAC/C,KAAK;SACN;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,56 @@
1
+ /**
2
+ * What a chat client needs to know about the transport, and nothing a
3
+ * client bundle must not carry.
4
+ *
5
+ * This file imports nothing. A UI item reads the error codes, the
6
+ * quota-state shape and `parseChatError` from `@intelligo-dev/chat/client`
7
+ * without pulling the server handler — or Drizzle, Stripe and the auth
8
+ * server behind it — into the browser.
9
+ */
10
+ /** Stable codes the transport answers with. The status is fixed per code. */
11
+ export declare const CHAT_ERROR_CODES: readonly ["BAD_REQUEST", "UNAUTHORIZED", "QUOTA_EXCEEDED", "FEATURE_GATED", "NOT_FOUND", "RATE_LIMITED", "INTERNAL", "BILLING_NOT_CONFIGURED"];
12
+ export type ChatErrorCode = (typeof CHAT_ERROR_CODES)[number];
13
+ /** The JSON body of every non-2xx answer from the chat transport. */
14
+ export type ChatErrorBody = {
15
+ /** Human-readable, in the caller's locale when the app bound a translator. */
16
+ error: string;
17
+ code: ChatErrorCode;
18
+ /**
19
+ * The entitlement port's own refusal code (`insufficient_credits`,
20
+ * `allowance_depleted`, …) when `code` is `QUOTA_EXCEEDED` or
21
+ * `BILLING_NOT_CONFIGURED`.
22
+ */
23
+ reasonCode?: string;
24
+ /** Set on `RATE_LIMITED`. */
25
+ retryAfterSeconds?: number;
26
+ };
27
+ /**
28
+ * What the chat page opened with — a server-rendered estimate of the
29
+ * caller's credit, so the page can say "you are out" before a message
30
+ * is spent on finding out. Amounts are credits, the unit the balance
31
+ * and the estimate are both in, not money.
32
+ */
33
+ export type ChatQuotaState = {
34
+ /** False when the next turn would be refused. */
35
+ allowed: boolean;
36
+ /** Why, when the engine refused. */
37
+ reason: string | null;
38
+ /** Typed refusal, for a UI that wants to distinguish them. */
39
+ code: string | null;
40
+ /** Credits left across every pool. */
41
+ remaining: number;
42
+ /** Worst-case credits one turn could cost. */
43
+ estimated: number;
44
+ /** Where "upgrade" and "top up" should go. */
45
+ upgradeHref: string;
46
+ };
47
+ /**
48
+ * The transport's error body, from the error `useChat` surfaces.
49
+ *
50
+ * The AI SDK's transport throws an `Error` whose message is the
51
+ * response text, so a JSON refusal arrives as a string. Anything that
52
+ * is not one of ours — a network failure, a proxy's HTML page — is
53
+ * null, and belongs in a generic error strip rather than a banner.
54
+ */
55
+ export declare function parseChatError(error: unknown): ChatErrorBody | null;
56
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,6EAA6E;AAC7E,eAAO,MAAM,gBAAgB,gJASnB,CAAC;AAEX,MAAM,MAAM,aAAa,GAAG,CAAC,OAAO,gBAAgB,CAAC,CAAC,MAAM,CAAC,CAAC;AAE9D,qEAAqE;AACrE,MAAM,MAAM,aAAa,GAAG;IAC1B,8EAA8E;IAC9E,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,aAAa,CAAC;IACpB;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,6BAA6B;IAC7B,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B,CAAC;AAEF;;;;;GAKG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,iDAAiD;IACjD,OAAO,EAAE,OAAO,CAAC;IACjB,oCAAoC;IACpC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,8DAA8D;IAC9D,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,sCAAsC;IACtC,SAAS,EAAE,MAAM,CAAC;IAClB,8CAA8C;IAC9C,SAAS,EAAE,MAAM,CAAC;IAClB,8CAA8C;IAC9C,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAIF;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,aAAa,GAAG,IAAI,CAgCnE"}
package/dist/client.js ADDED
@@ -0,0 +1,62 @@
1
+ /**
2
+ * What a chat client needs to know about the transport, and nothing a
3
+ * client bundle must not carry.
4
+ *
5
+ * This file imports nothing. A UI item reads the error codes, the
6
+ * quota-state shape and `parseChatError` from `@intelligo-dev/chat/client`
7
+ * without pulling the server handler — or Drizzle, Stripe and the auth
8
+ * server behind it — into the browser.
9
+ */
10
+ /** Stable codes the transport answers with. The status is fixed per code. */
11
+ export const CHAT_ERROR_CODES = [
12
+ "BAD_REQUEST",
13
+ "UNAUTHORIZED",
14
+ "QUOTA_EXCEEDED",
15
+ "FEATURE_GATED",
16
+ "NOT_FOUND",
17
+ "RATE_LIMITED",
18
+ "INTERNAL",
19
+ "BILLING_NOT_CONFIGURED",
20
+ ];
21
+ const CODES = new Set(CHAT_ERROR_CODES);
22
+ /**
23
+ * The transport's error body, from the error `useChat` surfaces.
24
+ *
25
+ * The AI SDK's transport throws an `Error` whose message is the
26
+ * response text, so a JSON refusal arrives as a string. Anything that
27
+ * is not one of ours — a network failure, a proxy's HTML page — is
28
+ * null, and belongs in a generic error strip rather than a banner.
29
+ */
30
+ export function parseChatError(error) {
31
+ const text = error instanceof Error
32
+ ? error.message
33
+ : typeof error === "string"
34
+ ? error
35
+ : null;
36
+ if (!text)
37
+ return null;
38
+ try {
39
+ const parsed = JSON.parse(text);
40
+ if (typeof parsed !== "object" ||
41
+ parsed === null ||
42
+ typeof parsed.error !== "string" ||
43
+ typeof parsed.code !== "string" ||
44
+ !CODES.has(parsed.code)) {
45
+ return null;
46
+ }
47
+ return {
48
+ error: parsed.error,
49
+ code: parsed.code,
50
+ ...(typeof parsed.reasonCode === "string"
51
+ ? { reasonCode: parsed.reasonCode }
52
+ : {}),
53
+ ...(typeof parsed.retryAfterSeconds === "number"
54
+ ? { retryAfterSeconds: parsed.retryAfterSeconds }
55
+ : {}),
56
+ };
57
+ }
58
+ catch {
59
+ return null;
60
+ }
61
+ }
62
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,6EAA6E;AAC7E,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC9B,aAAa;IACb,cAAc;IACd,gBAAgB;IAChB,eAAe;IACf,WAAW;IACX,cAAc;IACd,UAAU;IACV,wBAAwB;CAChB,CAAC;AAwCX,MAAM,KAAK,GAAwB,IAAI,GAAG,CAAC,gBAAgB,CAAC,CAAC;AAE7D;;;;;;;GAOG;AACH,MAAM,UAAU,cAAc,CAAC,KAAc;IAC3C,MAAM,IAAI,GACR,KAAK,YAAY,KAAK;QACpB,CAAC,CAAC,KAAK,CAAC,OAAO;QACf,CAAC,CAAC,OAAO,KAAK,KAAK,QAAQ;YACzB,CAAC,CAAC,KAAK;YACP,CAAC,CAAC,IAAI,CAAC;IACb,I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