@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
@@ -0,0 +1,322 @@
1
+ /**
2
+ * What an application tells the chat transport. Two things are required,
3
+ * the execution boundary and a way to run the model; everything else has a
4
+ * default that gives a clean install a working chat with no API keys.
5
+ * Optional seams close over the caller's tenancy, so the model is never told
6
+ * which workspace it is in. `streamTurn` replaces the model call and nothing
7
+ * else: auth, rate limit, gate, admission, persistence and settlement stay
8
+ * the transport's.
9
+ */
10
+ import type { LanguageModel, StopCondition, ToolSet, UIMessage, UIMessageChunk, UIMessageStreamWriter } from "ai";
11
+ import type { streamText } from "ai";
12
+ /** The provider-specific options `streamText` accepts (`ai` does not export the type). */
13
+ export type ProviderOptions = NonNullable<Parameters<typeof streamText>[0]["providerOptions"]>;
14
+ import type { Conversation } from "@intelligo-dev/core/conversations";
15
+ import type { Executions } from "@intelligo-dev/executions";
16
+ import type { ChatAttachmentPolicy } from "./body.js";
17
+ import type { ChatErrorCode, ChatModelOption } from "./client.js";
18
+ import type { ChatMessages } from "./errors.js";
19
+ import type { ChatGenerationOptions } from "./generation.js";
20
+ import type { ChatDataChunk, ChatUIMessage } from "./parts.js";
21
+ import type { TokenUsage } from "./usage.js";
22
+ import type { ConversationWindowOptions } from "./windowing.js";
23
+ export type { ChatAttachmentPolicy } from "./body.js";
24
+ export type { ChatMessages, ChatMessageKey, ChatMessageParams } from "./errors.js";
25
+ export type { ChatGenerationOptions } from "./generation.js";
26
+ /** The resolved caller. Every read and write is scoped to this pair. */
27
+ export interface ChatActor {
28
+ workspaceId: string;
29
+ userId: string;
30
+ }
31
+ /** What is known about a turn before the agent is resolved. */
32
+ export interface ChatTurnContext extends ChatActor {
33
+ request: Request;
34
+ conversationId: string;
35
+ /**
36
+ * Fields the client transport sent beyond the AI SDK's own — an
37
+ * `agentId`, a `modelId`. Opaque to the transport; `resolveAgent`
38
+ * reads them. `modelId` is also read by the default resolution when
39
+ * `models` is configured.
40
+ */
41
+ body: Record<string, unknown>;
42
+ /** The existing row, or null on a conversation's first turn. */
43
+ conversation: Conversation | null;
44
+ trigger: "submit-message" | "regenerate-message" | undefined;
45
+ /**
46
+ * Write a part to the client mid-turn — a status line, a task plan,
47
+ * a document streaming into the canvas (`createArtifactWriter`). A
48
+ * no-op before the stream opens and after it closes, so a tool bound
49
+ * through `agent.tools` may hold on to it.
50
+ */
51
+ write: (chunk: ChatDataChunk) => void;
52
+ /**
53
+ * Merge a patch into the conversation's `metadata` — a runtime's
54
+ * session id, a summary of pruned history. Shallow: top-level keys
55
+ * are replaced, other keys kept. Rejects before the row exists.
56
+ */
57
+ updateMetadata: (patch: Record<string, unknown>) => Promise<void>;
58
+ /**
59
+ * Add the tokens a tool spent on its own model call — a search that
60
+ * asks a model, a sub-agent — to what this turn settles. They are
61
+ * priced as the turn's model, so a call on another model is its own
62
+ * execution rather than usage added here. Usage added after the run
63
+ * settles is not charged.
64
+ */
65
+ addUsage: (usage: TokenUsage) => void;
66
+ }
67
+ /** The agent this turn runs as. */
68
+ export interface ResolvedAgent {
69
+ /** Stored on the conversation row when the transport creates it. */
70
+ id: string;
71
+ systemPrompt: string;
72
+ tools?: ToolSet;
73
+ /** Names the model may call this turn; every tool when omitted. */
74
+ activeTools?: string[];
75
+ /**
76
+ * Added after `stepCountIs(maxSteps)`, e.g. `hasToolCall("askUser")`.
77
+ *
78
+ * Consulted only on a turn that has tools: the SDK stops a run on
79
+ * this condition "when there are tool results in the last step", and
80
+ * a turn with no tools never has any, so it is a single step either
81
+ * way. The step cap itself is not removable — an uncapped step count
82
+ * is an uncapped bill.
83
+ */
84
+ stopWhen?: StopCondition<ToolSet> | StopCondition<ToolSet>[];
85
+ /** Overrides the request's and the config's model. Must be a registered model id. */
86
+ modelId?: string;
87
+ maxSteps?: number;
88
+ /** Overrides the config's; `null` disables the gate for this agent. */
89
+ featureKey?: string | null;
90
+ capability?: string;
91
+ /**
92
+ * Passed to `streamText` as-is — the provider's own knobs, e.g. a
93
+ * thinking budget (`{ google: { thinkingConfig: { includeThoughts: true } } }`,
94
+ * `{ anthropic: { thinking: { type: "enabled", budgetTokens: 2048 } } }`).
95
+ * With `reasoning: true` this is what makes a model's thoughts reach
96
+ * the transcript at all; the transport never names a provider.
97
+ */
98
+ providerOptions?: ProviderOptions;
99
+ /**
100
+ * How the model samples this turn — temperature, a token ceiling, a
101
+ * tool choice, a seed. An allowlist of the `streamText` options that
102
+ * do not touch settlement; see `ChatGenerationOptions` for what the
103
+ * transport keeps and why.
104
+ */
105
+ generation?: ChatGenerationOptions;
106
+ }
107
+ /**
108
+ * The one-agent shorthand, derived from `ResolvedAgent` so the two
109
+ * cannot drift: everything a `resolveAgent` function can return is
110
+ * settable here too, and the three fields below are the only ones that
111
+ * differ — an id and a prompt because the transport has defaults for
112
+ * them, and tools because the shorthand may close over the turn.
113
+ */
114
+ export interface ChatAgentConfig extends Omit<ResolvedAgent, "id" | "systemPrompt" | "tools"> {
115
+ /** Default `"assistant"`. */
116
+ id?: string;
117
+ systemPrompt?: string;
118
+ tools?: ToolSet | ((turn: ChatTurnContext) => ToolSet | Promise<ToolSet>);
119
+ }
120
+ /** A turn with its agent resolved — what the hooks below receive. */
121
+ export interface ChatTurn extends ChatTurnContext {
122
+ agent: ResolvedAgent;
123
+ /** The persisted history, read lazily: not every `prepareMessages` needs it. */
124
+ history: () => Promise<UIMessage[]>;
125
+ }
126
+ /** What the model is shown. */
127
+ export interface PreparedTurn {
128
+ messages: UIMessage[];
129
+ /** Replaces the agent's system prompt when set — a summary prefix, injected context. */
130
+ system?: string;
131
+ }
132
+ /**
133
+ * A turn produced by something other than `streamText`.
134
+ *
135
+ * `stream` carries the AI SDK's UI message chunks. The transport writes
136
+ * `start` and `finish` itself and drops any the stream emits, so a
137
+ * runtime whose adapter already frames the message needs no stripping.
138
+ * `usage` settles the execution: it resolves once the run is over,
139
+ * with the whole run's tokens. On a client abort the transport settles
140
+ * with whatever `usage` resolves to; reject it and the turn is failed.
141
+ */
142
+ export interface TurnStream {
143
+ stream: ReadableStream<UIMessageChunk>;
144
+ usage: Promise<TokenUsage & {
145
+ modelId?: string;
146
+ finishReason?: string;
147
+ }>;
148
+ }
149
+ export type StreamTurn = (turn: ChatTurn, prepared: PreparedTurn, context: {
150
+ modelId: string;
151
+ abortSignal: AbortSignal;
152
+ writer: UIMessageStreamWriter<ChatUIMessage>;
153
+ }) => TurnStream | Promise<TurnStream>;
154
+ export interface RateLimitDecision {
155
+ allowed: boolean;
156
+ retryAfterSeconds?: number;
157
+ limit?: number;
158
+ remaining?: number;
159
+ resetAt?: Date;
160
+ }
161
+ export interface ChatTurnEvents {
162
+ /** The execution is open and the model is about to run. */
163
+ start?: (event: {
164
+ turn: ChatTurn;
165
+ executionId: string;
166
+ requestId: string;
167
+ modelId: string;
168
+ }) => void | Promise<void>;
169
+ /** The run settled — after a normal finish, or after the client aborted. */
170
+ complete?: (event: {
171
+ turn: ChatTurn;
172
+ executionId: string;
173
+ modelId: string;
174
+ usage: TokenUsage;
175
+ aborted: boolean;
176
+ finishReason?: string;
177
+ rawFinishReason?: string;
178
+ providerMetadata?: unknown;
179
+ warnings?: unknown[];
180
+ }) => void | Promise<void>;
181
+ /** Something threw. `phase` says where; the turn may or may not have an agent yet. */
182
+ fail?: (event: {
183
+ turn: ChatTurnContext;
184
+ error: unknown;
185
+ phase: "stream" | "settlement" | "persistence" | "title" | "unhandled";
186
+ }) => void | Promise<void>;
187
+ /** The transport answered with a refusal rather than a stream. */
188
+ refuse?: (event: {
189
+ actor: ChatActor | null;
190
+ conversationId: string | null;
191
+ code: ChatErrorCode;
192
+ status: number;
193
+ reasonCode?: string;
194
+ }) => void | Promise<void>;
195
+ /**
196
+ * The user answered a tool's approval request. Fired once per
197
+ * response, from the continuation turn that carries it — the audit
198
+ * trail a product needs for a gated action.
199
+ */
200
+ approval?: (event: {
201
+ turn: ChatTurnContext;
202
+ toolName: string;
203
+ toolCallId: string;
204
+ approvalId: string;
205
+ approved: boolean;
206
+ reason?: string;
207
+ }) => void | Promise<void>;
208
+ /** The reader voted on a reply, or cleared a vote. */
209
+ feedback?: (event: {
210
+ actor: ChatActor;
211
+ conversationId: string;
212
+ messageId: string;
213
+ vote: "up" | "down" | null;
214
+ }) => void | Promise<void>;
215
+ }
216
+ export interface ChatModelsConfig {
217
+ /**
218
+ * Models the request may ask for by `modelId`. A list, or a function
219
+ * of the caller for a list that depends on the plan. A request naming
220
+ * a model outside it, or one whose `featureKey` the workspace lacks,
221
+ * is refused as `FEATURE_GATED` with reason `model_not_allowed`.
222
+ */
223
+ options: ChatModelOption[] | ((actor: ChatActor) => ChatModelOption[] | Promise<ChatModelOption[]>);
224
+ }
225
+ export interface ChatServerConfig {
226
+ /** The execution boundary, with its ports bound by the composition root. */
227
+ executions: Executions;
228
+ model: {
229
+ /** Must be registered in `@intelligo-dev/executions/pricing`. */
230
+ defaultId: string;
231
+ /**
232
+ * Turns a model id into a model for `streamText`. Required unless
233
+ * `streamTurn` is set, and unused when it is.
234
+ */
235
+ resolve?: (modelId: string, turn: ChatTurnContext) => LanguageModel | Promise<LanguageModel>;
236
+ };
237
+ /**
238
+ * Runs the turn instead of `streamText` — a Mastra agent, an eve
239
+ * session, a workflow. Everything around the model call stays the
240
+ * transport's. See `TurnStream`.
241
+ */
242
+ streamTurn?: StreamTurn;
243
+ /** Runs first on every request — the place to call `composeIntelligo()`. */
244
+ onRequest?: () => void | Promise<void>;
245
+ /** Plan feature key gating the surface. `null` disables the gate. Default `"chat"`. */
246
+ featureKey?: string | null;
247
+ /** Capability recorded on each execution. Default `"chat.message"`. */
248
+ capability?: string;
249
+ /** Longest user message accepted, in characters. Default 8000. */
250
+ maxMessageLength?: number;
251
+ /** Model steps one turn may take (a tool call and the reply using it are two). Default 5. */
252
+ maxSteps?: number;
253
+ /**
254
+ * The one-agent shorthand. Ignored when `resolveAgent` is set.
255
+ * `tools` may close over the turn's tenancy.
256
+ */
257
+ agent?: ChatAgentConfig;
258
+ /**
259
+ * Which agent runs this turn — from the body, the row, a table. The
260
+ * default resolves `agent`, keeping the id the conversation was
261
+ * created with.
262
+ */
263
+ resolveAgent?: (turn: ChatTurnContext) => ResolvedAgent | Promise<ResolvedAgent>;
264
+ /** The models a request may pick from. Unset: every request runs the default. */
265
+ models?: ChatModelsConfig;
266
+ /**
267
+ * What the model is shown. The default windows the incoming
268
+ * transcript by `windowing`. An application that prunes, summarises
269
+ * or injects context binds its own; `turn.history()` reads the
270
+ * persisted messages when the client's copy is not to be trusted.
271
+ */
272
+ prepareMessages?: (turn: ChatTurn, incoming: UIMessage[]) => PreparedTurn | Promise<PreparedTurn>;
273
+ /** Default `{ maxMessages: 40 }`; `false` shows the model everything. */
274
+ windowing?: ConversationWindowOptions | false;
275
+ /** File parts the transport accepts. Default `false`: any file part is a 400. */
276
+ attachments?: ChatAttachmentPolicy | false;
277
+ /** Stream the model's reasoning parts to the client. Default false. */
278
+ reasoning?: boolean;
279
+ /** Stream the model's source parts (citations) to the client. Default false. */
280
+ sources?: boolean;
281
+ /**
282
+ * Attach `{ modelId, usage, finishedAt }` to the assistant message as
283
+ * its metadata when the turn finishes. Default true.
284
+ */
285
+ messageMetadata?: boolean;
286
+ /**
287
+ * Answer cross-origin requests from these origins — an embedded
288
+ * widget on another site. Unset: no CORS headers, same-origin only.
289
+ */
290
+ cors?: {
291
+ origins: readonly string[];
292
+ };
293
+ /**
294
+ * Titles a conversation from its first user message when the row is
295
+ * created. Sync: stored on create. Async: the row is created untitled,
296
+ * renamed when the promise resolves, and the client is sent a
297
+ * transient `data-chat-title` part. Default: first line, truncated.
298
+ */
299
+ deriveTitle?: (firstUserText: string, turn: ChatTurnContext) => string | null | Promise<string | null>;
300
+ /**
301
+ * Where the turn's messages go. Default: the user message that
302
+ * opened the turn and the assistant reply, upserted through core.
303
+ * `false` persists nothing.
304
+ */
305
+ persist?: false | ((turn: ChatTurn, result: {
306
+ userMessage: UIMessage | null;
307
+ responseMessage: UIMessage;
308
+ isContinuation: boolean;
309
+ }) => Promise<void>);
310
+ /** Adjusts what settlement records — a floor for providers that report nothing. */
311
+ normalizeUsage?: (usage: TokenUsage) => TokenUsage;
312
+ /** Merged into the execution's metadata on begin and complete. */
313
+ metadata?: (turn: ChatTurn) => Record<string, unknown>;
314
+ onTurn?: ChatTurnEvents;
315
+ /** Localised refusal copy. Default: English. */
316
+ messages?: (request: Request) => ChatMessages | Promise<ChatMessages>;
317
+ /** Default: `requireWorkspace()`. Throw to answer 401. */
318
+ authenticate?: (request: Request) => Promise<ChatActor>;
319
+ /** Default: the workspace plan's per-minute limit. `false` disables. */
320
+ rateLimit?: false | ((actor: ChatActor) => Promise<RateLimitDecision>);
321
+ }
322
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EACV,aAAa,EACb,aAAa,EACb,OAAO,EACP,SAAS,EACT,cAAc,EACd,qBAAqB,EACtB,MAAM,IAAI,CAAC;AACZ,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAErC,0FAA0F;AAC1F,MAAM,MAAM,eAAe,GAAG,WAAW,CACvC,UAAU,CAAC,OAAO,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,iBAAiB,CAAC,CACpD,CAAC;AAEF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mCAAmC,CAAC;AACtE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAE5D,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,QAAQ,CAAC;AACnD,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAC/D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAC7C,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAC1D,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC5D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAC1C,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAC;AAE7D,YAAY,EAAE,oBA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,aAAa,EAAE,MAAM,EACrB,IAAI,EAAE,eAAe,KAClB,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IAC5C;;;;OAIG;IACH,OAAO,CAAC,EACJ,KAAK,GACL,CAAC,CACC,IAAI,EAAE,QAAQ,EACd,MAAM,EAAE;QACN,WAAW,EAAE,SAAS,GAAG,IAAI,CAAC;QAC9B,eAAe,EAAE,SAAS,CAAC;QAC3B,cAAc,EAAE,OAAO,CAAC;KACzB,KACE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;IACxB,mFAAmF;IACnF,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,UAAU,CAAC;IACnD,kEAAkE;IAClE,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,QAAQ,KAAK,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACvD,MAAM,CAAC,EAAE,cAAc,CAAC;IAExB,gDAAgD;IAChD,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;IACtE,0DAA0D;IAC1D,YAAY,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,SAAS,CAAC,CAAC;IACxD,wEAAwE;IACxE,SAAS,CAAC,EAAE,KAAK,GAAG,CAAC,CAAC,KAAK,EAAE,SAAS,KAAK,OAAO,CAAC,iBAAiB,CAAC,CAAC,CAAC;CACxE"}
package/dist/config.js ADDED
@@ -0,0 +1,11 @@
1
+ /**
2
+ * What an application tells the chat transport. Two things are required,
3
+ * the execution boundary and a way to run the model; everything else has a
4
+ * default that gives a clean install a working chat with no API keys.
5
+ * Optional seams close over the caller's tenancy, so the model is never told
6
+ * which workspace it is in. `streamTurn` replaces the model call and nothing
7
+ * else: auth, rate limit, gate, admission, persistence and settlement stay
8
+ * the transport's.
9
+ */
10
+ export {};
11
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG"}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Refusals: one status per code, one message key per situation.
3
+ *
4
+ * The transport never invents copy. It asks a translator for a key,
5
+ * and the application binds that translator to its own message files
6
+ * (`messages(request)` in the config). The English table below is the
7
+ * default a deployment gets when it binds nothing.
8
+ */
9
+ import type { ChatErrorBody, ChatErrorCode } from "./client.js";
10
+ export declare const CHAT_ERROR_STATUS: Readonly<Record<ChatErrorCode, number>>;
11
+ /**
12
+ * Every message the transport can answer with. One key per situation,
13
+ * not per code: `BAD_REQUEST` has three.
14
+ */
15
+ export type ChatMessageKey = "invalidBody" | "messageTooLong" | "attachmentRejected" | "unauthorized" | "rateLimited" | "featureGated" | "notFound" | "quotaExceeded" | "billingNotConfigured" | "modelUnavailable" | "internalError" | "streamError";
16
+ export type ChatMessageParams = Record<string, string | number>;
17
+ /** Resolves a message key in the caller's locale. */
18
+ export type ChatMessages = (key: ChatMessageKey, params?: ChatMessageParams) => string;
19
+ /** The default translator: English, with `{param}` interpolation. */
20
+ export declare const DEFAULT_CHAT_MESSAGES: ChatMessages;
21
+ /** A JSON refusal with the status its code fixes. */
22
+ export declare function refuse(code: ChatErrorCode, error: string, extra?: Partial<Omit<ChatErrorBody, "error" | "code">>, headers?: Record<string, string>): Response;
23
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAE7D,eAAO,MAAM,iBAAiB,EAAE,QAAQ,CAAC,MAAM,CAAC,aAAa,EAAE,MAAM,CAAC,CAUrE,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,cAAc,GACtB,aAAa,GACb,gBAAgB,GAChB,oBAAoB,GACpB,cAAc,GACd,aAAa,GACb,cAAc,GACd,UAAU,GACV,eAAe,GACf,sBAAsB,GACtB,kBAAkB,GAClB,eAAe,GACf,aAAa,CAAC;AAElB,MAAM,MAAM,iBAAiB,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC;AAEhE,qDAAqD;AACrD,MAAM,MAAM,YAAY,GAAG,CACzB,GAAG,EAAE,cAAc,EACnB,MAAM,CAAC,EAAE,iBAAiB,KACvB,MAAM,CAAC;AAiBZ,qEAAqE;AACrE,eAAO,MAAM,qBAAqB,EAAE,YAGjC,CAAC;AAEJ,qDAAqD;AACrD,wBAAgB,MAAM,CACpB,IAAI,EAAE,aAAa,EACnB,KAAK,EAAE,MAAM,EACb,KAAK,GAAE,OAAO,CAAC,IAAI,CAAC,aAAa,EAAE,OAAO,GAAG,MAAM,CAAC,CAAM,EAC1D,OAAO,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAM,GACnC,QAAQ,CAGV"}
package/dist/errors.js ADDED
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Refusals: one status per code, one message key per situation.
3
+ *
4
+ * The transport never invents copy. It asks a translator for a key,
5
+ * and the application binds that translator to its own message files
6
+ * (`messages(request)` in the config). The English table below is the
7
+ * default a deployment gets when it binds nothing.
8
+ */
9
+ export const CHAT_ERROR_STATUS = {
10
+ BAD_REQUEST: 400,
11
+ UNAUTHORIZED: 401,
12
+ QUOTA_EXCEEDED: 402,
13
+ FEATURE_GATED: 403,
14
+ NOT_FOUND: 404,
15
+ RATE_LIMITED: 429,
16
+ INTERNAL: 500,
17
+ BILLING_NOT_CONFIGURED: 503,
18
+ MODEL_UNAVAILABLE: 503,
19
+ };
20
+ const ENGLISH = {
21
+ invalidBody: "Invalid request body.",
22
+ messageTooLong: "Message is too long (max {max} characters).",
23
+ attachmentRejected: "That attachment type isn't accepted.",
24
+ unauthorized: "Unauthorized.",
25
+ rateLimited: "Rate limit exceeded. Try again in {seconds}s.",
26
+ featureGated: "Chat isn't included in your current plan.",
27
+ notFound: "That conversation no longer exists.",
28
+ quotaExceeded: "Usage quota exceeded. Upgrade your plan or purchase credits.",
29
+ billingNotConfigured: "Billing is not configured for this deployment.",
30
+ modelUnavailable: "Chat is temporarily unavailable. Please try again later.",
31
+ internalError: "Something went wrong.",
32
+ streamError: "Something went wrong while generating a response.",
33
+ };
34
+ /** The default translator: English, with `{param}` interpolation. */
35
+ export const DEFAULT_CHAT_MESSAGES = (key, params = {}) => ENGLISH[key].replace(/\{(\w+)\}/g, (match, name) => name in params ? String(params[name]) : match);
36
+ /** A JSON refusal with the status its code fixes. */
37
+ export function refuse(code, error, extra = {}, headers = {}) {
38
+ const body = { error, code, ...extra };
39
+ return Response.json(body, { status: CHAT_ERROR_STATUS[code], headers });
40
+ }
41
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,MAAM,CAAC,MAAM,iBAAiB,GAA4C;IACxE,WAAW,EAAE,GAAG;IAChB,YAAY,EAAE,GAAG;IACjB,cAAc,EAAE,GAAG;IACnB,aAAa,EAAE,GAAG;IAClB,SAAS,EAAE,GAAG;IACd,YAAY,EAAE,GAAG;IACjB,QAAQ,EAAE,GAAG;IACb,sBAAsB,EAAE,GAAG;IAC3B,iBAAiB,EAAE,GAAG;CACvB,CAAC;AA4BF,MAAM,OAAO,GAA6C;IACxD,WAAW,EAAE,uBAAuB;IACpC,cAAc,EAAE,6CAA6C;IAC7D,kBAAkB,EAAE,sCAAsC;IAC1D,YAAY,EAAE,eAAe;IAC7B,WAAW,EAAE,+CAA+C;IAC5D,YAAY,EAAE,2CAA2C;IACzD,QAAQ,EAAE,qCAAqC;IAC/C,aAAa,EAAE,8DAA8D;IAC7E,oBAAoB,EAAE,gDAAgD;IACtE,gBAAgB,EAAE,0DAA0D;IAC5E,aAAa,EAAE,uBAAuB;IACtC,WAAW,EAAE,mDAAmD;CACjE,CAAC;AAEF,qEAAqE;AACrE,MAAM,CAAC,MAAM,qBAAqB,GAAiB,CAAC,GAAG,EAAE,MAAM,GAAG,EAAE,EAAE,EAAE,CACtE,OAAO,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,YAAY,EAAE,CAAC,KAAK,EAAE,IAAY,EAAE,EAAE,CACzD,IAAI,IAAI,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAC9C,CAAC;AAEJ,qDAAqD;AACrD,MAAM,UAAU,MAAM,CACpB,IAAmB,EACnB,KAAa,EACb,QAAwD,EAAE,EAC1D,UAAkC,EAAE;IAEpC,MAAM,IAAI,GAAkB,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,KAAK,EAAE,CAAC;IACtD,OAAO,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,iBAAiB,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;AAC3E,CAAC"}
@@ -0,0 +1,22 @@
1
+ /**
2
+ * A reader's verdict on a reply, recorded once and reported once.
3
+ *
4
+ * The registry's `voteMessage` action is two lines over this: the
5
+ * write goes through core's votes table, and the `feedback` hook is
6
+ * the one place telemetry learns about it — the same hook whether the
7
+ * vote came from the page, a panel or a widget.
8
+ */
9
+ import type { ChatActor, ChatServerConfig } from "./config.js";
10
+ export type ChatFeedback = "up" | "down" | null;
11
+ export type RecordChatFeedbackResult = {
12
+ ok: true;
13
+ } | {
14
+ ok: false;
15
+ code: "not_found" | "database_error";
16
+ };
17
+ export declare function recordChatFeedback(config: Pick<ChatServerConfig, "onTurn">, actor: ChatActor, params: {
18
+ conversationId: string;
19
+ messageId: string;
20
+ vote: ChatFeedback;
21
+ }): Promise<RecordChatFeedbackResult>;
22
+ //# sourceMappingURL=feedback.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"feedback.d.ts","sourceRoot":"","sources":["../src/feedback.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAQH,OAAO,KAAK,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE5D,MAAM,MAAM,YAAY,GAAG,IAAI,GAAG,MAAM,GAAG,IAAI,CAAC;AAEhD,MAAM,MAAM,wBAAwB,GAClC;IAAE,EAAE,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,IAAI,EAAE,WAAW,GAAG,gBAAgB,CAAA;CAAE,CAAC;AAErE,wBAAsB,kBAAkB,CACtC,MAAM,EAAE,IAAI,CAAC,gBAAgB,EAAE,QAAQ,CAAC,EACxC,KAAK,EAAE,SAAS,EAChB,MAAM,EAAE;IAAE,cAAc,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,YAAY,CAAA;CAAE,GACxE,OAAO,CAAC,wBAAwB,CAAC,CAmCnC"}
@@ -0,0 +1,46 @@
1
+ /**
2
+ * A reader's verdict on a reply, recorded once and reported once.
3
+ *
4
+ * The registry's `voteMessage` action is two lines over this: the
5
+ * write goes through core's votes table, and the `feedback` hook is
6
+ * the one place telemetry learns about it — the same hook whether the
7
+ * vote came from the page, a panel or a widget.
8
+ */
9
+ import { clearVote, isConversationServiceError, voteMessage, } from "@intelligo-dev/core/conversations";
10
+ export async function recordChatFeedback(config, actor, params) {
11
+ try {
12
+ if (params.vote === null) {
13
+ await clearVote(actor, {
14
+ chatId: params.conversationId,
15
+ messageId: params.messageId,
16
+ });
17
+ }
18
+ else {
19
+ await voteMessage(actor, {
20
+ chatId: params.conversationId,
21
+ messageId: params.messageId,
22
+ type: params.vote,
23
+ });
24
+ }
25
+ }
26
+ catch (error) {
27
+ if (isConversationServiceError(error) &&
28
+ (error.code === "not_found" || error.code === "forbidden")) {
29
+ return { ok: false, code: "not_found" };
30
+ }
31
+ return { ok: false, code: "database_error" };
32
+ }
33
+ try {
34
+ await config.onTurn?.feedback?.({
35
+ actor,
36
+ conversationId: params.conversationId,
37
+ messageId: params.messageId,
38
+ vote: params.vote,
39
+ });
40
+ }
41
+ catch {
42
+ // Telemetry must never fail a vote.
43
+ }
44
+ return { ok: true };
45
+ }
46
+ //# sourceMappingURL=feedback.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"feedback.js","sourceRoot":"","sources":["../src/feedback.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EACL,SAAS,EACT,0BAA0B,EAC1B,WAAW,GACZ,MAAM,mCAAmC,CAAC;AAS3C,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACtC,MAAwC,EACxC,KAAgB,EAChB,MAAyE;IAEzE,IAAI,CAAC;QACH,IAAI,MAAM,CAAC,IAAI,KAAK,IAAI,EAAE,CAAC;YACzB,MAAM,SAAS,CAAC,KAAK,EAAE;gBACrB,MAAM,EAAE,MAAM,CAAC,cAAc;gBAC7B,SAAS,EAAE,MAAM,CAAC,SAAS;aAC5B,CAAC,CAAC;QACL,CAAC;aAAM,CAAC;YACN,MAAM,WAAW,CAAC,KAAK,EAAE;gBACvB,MAAM,EAAE,MAAM,CAAC,cAAc;gBAC7B,SAAS,EAAE,MAAM,CAAC,SAAS;gBAC3B,IAAI,EAAE,MAAM,CAAC,IAAI;aAClB,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IACE,0BAA0B,CAAC,KAAK,CAAC;YACjC,CAAC,KAAK,CAAC,IAAI,KAAK,WAAW,IAAI,KAAK,CAAC,IAAI,KAAK,WAAW,CAAC,EAC1D,CAAC;YACD,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC;QAC1C,CAAC;QACD,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,gBAAgB,EAAE,CAAC;IAC/C,CAAC;IAED,IAAI,CAAC;QACH,MAAM,MAAM,CAAC,MAAM,EAAE,QAAQ,EAAE,CAAC;YAC9B,KAAK;YACL,cAAc,EAAE,MAAM,CAAC,cAAc;YACrC,SAAS,EAAE,MAAM,CAAC,SAAS;YAC3B,IAAI,EAAE,MAAM,CAAC,IAAI;SAClB,CAAC,CAAC;IACL,CAAC;IAAC,MAAM,CAAC;QACP,oCAAoC;IACtC,CAAC;IACD,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC;AACtB,CAAC"}
@@ -0,0 +1,104 @@
1
+ /**
2
+ * The `streamText` options a product may set per turn: how the model
3
+ * samples (temperature, a token ceiling, a tool choice, a seed), none of
4
+ * which changes what settlement reads back. Written out as an allowlist
5
+ * rather than an `Omit<>` over the SDK's type, so an option a later SDK adds
6
+ * is not admitted silently.
7
+ *
8
+ * What the transport keeps, and why each one is not negotiable:
9
+ *
10
+ * `abortSignal` without `request.signal` an abandoned run bills in
11
+ * full and `onAbort` never fires.
12
+ * `timeout` a timeout lands on the error path, which releases
13
+ * the hold without charging tokens the provider has
14
+ * already produced. The route's deadline is
15
+ * `maxDuration`.
16
+ * `onFinish`, the transport captures `totalUsage` there; replacing
17
+ * `onEnd` it means every turn ends "stream ended without
18
+ * usage".
19
+ * `onAbort` settles from `sumStepUsage(steps)`.
20
+ * `onError` the failure path is `run.fail()` plus localised copy.
21
+ * `model` admission already priced the resolved `modelId`;
22
+ * another model bills one admission never saw.
23
+ * `system`, `prepareMessages` owns what the model is shown.
24
+ * `messages`,
25
+ * `prompt`,
26
+ * `instructions`
27
+ * `tools`, already seams on the agent, and the same object
28
+ * `activeTools` feeds `convertToModelMessages`; a second source
29
+ * desynchronises the two.
30
+ * `stopWhen` `stepCountIs(maxSteps)` must stay first and
31
+ * unremovable — an uncapped step count is an uncapped
32
+ * bill. `ResolvedAgent.stopWhen` appends to it.
33
+ * `_internal` the SDK's own test seam, not a product knob.
34
+ *
35
+ * Enforced three ways, because a cast defeats the type: the type itself,
36
+ * `pickGenerationOptions` copying only `GENERATION_KEYS` at runtime, and the
37
+ * handler spreading the result *first* so the transport's own keys win.
38
+ */
39
+ import type { LanguageModelCallOptions, PrepareStepFunction, RequestOptions, StreamTextTransform, TelemetryOptions, ToolChoice, ToolSet } from "ai";
40
+ import type { streamText } from "ai";
41
+ /**
42
+ * Settlement-neutral `streamText` options, sourced from the SDK's own
43
+ * types by indexed access so that a signature change upstream is a
44
+ * compile error here rather than a silently dropped field.
45
+ *
46
+ * Nested under `generation` rather than flattened onto `ResolvedAgent`
47
+ * for a concrete reason: `ai` has its own `reasoning` (an effort level
48
+ * for the model) and `ChatServerConfig.reasoning` already means "stream
49
+ * reasoning parts to the client". Flattening would put two different
50
+ * `reasoning` in one namespace, and every future SDK option would be
51
+ * one name collision away from a framework field.
52
+ */
53
+ export interface ChatGenerationOptions {
54
+ /**
55
+ * Ceiling on the tokens one step may produce. Unset, the transport
56
+ * uses the registered model's `maxOutputTokens` — the figure admission
57
+ * sized its hold with. A larger value here lets a step cost more than
58
+ * was held.
59
+ */
60
+ maxOutputTokens?: LanguageModelCallOptions["maxOutputTokens"];
61
+ temperature?: LanguageModelCallOptions["temperature"];
62
+ topP?: LanguageModelCallOptions["topP"];
63
+ topK?: LanguageModelCallOptions["topK"];
64
+ presencePenalty?: LanguageModelCallOptions["presencePenalty"];
65
+ frequencyPenalty?: LanguageModelCallOptions["frequencyPenalty"];
66
+ stopSequences?: LanguageModelCallOptions["stopSequences"];
67
+ seed?: LanguageModelCallOptions["seed"];
68
+ /**
69
+ * The model's reasoning effort — not `ChatServerConfig.reasoning`,
70
+ * which decides whether reasoning parts reach the client.
71
+ */
72
+ reasoning?: LanguageModelCallOptions["reasoning"];
73
+ /** Retries on a failed provider call. A retried call still reports one `totalUsage`. */
74
+ maxRetries?: RequestOptions["maxRetries"];
75
+ headers?: RequestOptions["headers"];
76
+ /** Picks among the tools the agent already declared; cannot introduce one. */
77
+ toolChoice?: ToolChoice<ToolSet>;
78
+ /** Per-step adjustment inside a loop the transport still caps with `maxSteps`. */
79
+ prepareStep?: PrepareStepFunction<ToolSet>;
80
+ /** Observation only. The current name; `experimental_telemetry` is the SDK's deprecated alias. */
81
+ telemetry?: TelemetryOptions;
82
+ /** Transforms the text stream. Usage is computed from steps, not from the transformed text. */
83
+ experimental_transform?: StreamTextTransform<ToolSet>;
84
+ /** Per-step observation `onTurn` has no equivalent for. */
85
+ onStepEnd?: Parameters<typeof streamText>[0]["onStepEnd"];
86
+ }
87
+ /**
88
+ * Every key of `ChatGenerationOptions`, as values.
89
+ *
90
+ * `satisfies` proves the array holds only real keys; `Exhaustive` below
91
+ * proves it holds all of them, so a field added to the interface
92
+ * without a line here is a compile error rather than an option that
93
+ * silently never reaches the model.
94
+ */
95
+ export declare const GENERATION_KEYS: readonly ["maxOutputTokens", "temperature", "topP", "topK", "presencePenalty", "frequencyPenalty", "stopSequences", "seed", "reasoning", "maxRetries", "headers", "toolChoice", "prepareStep", "telemetry", "experimental_transform", "onStepEnd"];
96
+ /**
97
+ * The allowlisted options, copied by name.
98
+ *
99
+ * Absent keys produce no own property rather than an explicit
100
+ * `undefined`: the handler spreads the result into a call where a
101
+ * present-but-undefined key would override the SDK's own default.
102
+ */
103
+ export declare function pickGenerationOptions(options: ChatGenerationOptions | undefined): Partial<ChatGenerationOptions>;
104
+ //# sourceMappingURL=generation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generation.d.ts","sourceRoot":"","sources":["../src/generation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAEH,OAAO,KAAK,EACV,wBAAwB,EACxB,mBAAmB,EACnB,cAAc,EACd,mBAAmB,EACnB,gBAAgB,EAChB,UAAU,EACV,OAAO,EACR,MAAM,IAAI,CAAC;AACZ,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAErC;;;;;;;;;;;GAWG;AACH,MAAM,WAAW,qBAAqB;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