@kitn.ai/ui 0.22.2 → 0.24.0
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.
- package/dist/mcp.es.js +365 -117
- package/dist/state/stream.d.ts +22 -4
- package/package.json +10 -7
- package/src/agent-tooling/integrations/vercel-ai-sdk.ts +255 -14
- package/src/agent-tooling/mcp/tools/scaffold.ts +19 -187
- package/src/agent-tooling/route-emit.ts +281 -0
- package/src/state/stream.ts +92 -3
|
@@ -10,13 +10,50 @@ const vercelAiSdk: Integration = {
|
|
|
10
10
|
// No per-framework templates: the handler below is web-standard, so the
|
|
11
11
|
// scaffolder wraps it in the target framework's own route declaration.
|
|
12
12
|
routeTemplates: {},
|
|
13
|
-
webRoute: `import { streamText } from 'ai';
|
|
14
|
-
import type {
|
|
13
|
+
webRoute: `import { dynamicTool, jsonSchema, streamText } from 'ai';
|
|
14
|
+
import type {
|
|
15
|
+
AssistantContent,
|
|
16
|
+
FilePart,
|
|
17
|
+
JSONSchema7,
|
|
18
|
+
ModelMessage,
|
|
19
|
+
SystemModelMessage,
|
|
20
|
+
ToolResultPart,
|
|
21
|
+
ToolSet,
|
|
22
|
+
UserContent,
|
|
23
|
+
} from 'ai';
|
|
15
24
|
|
|
16
25
|
// Next.js only: add \`export const maxDuration = 30\` to the route file to allow
|
|
17
26
|
// long streaming responses. It is a Next route-segment config, not part of the
|
|
18
27
|
// handler, so it lives in the file rather than in here.
|
|
19
28
|
|
|
29
|
+
/**
|
|
30
|
+
* The model, PINNED — and deliberately NOT read off the request body.
|
|
31
|
+
*
|
|
32
|
+
* Change THIS LINE to change the model; it is the only place one is named. The
|
|
33
|
+
* AI Gateway takes a \`creator/model-name\` string, so any id it routes works
|
|
34
|
+
* here without touching another import.
|
|
35
|
+
*
|
|
36
|
+
* Why it is not forwarded from the client, unlike the openai / openrouter /
|
|
37
|
+
* anthropic routes:
|
|
38
|
+
*
|
|
39
|
+
* · Those three POST to ONE host with ONE id space, so the scaffold can seed a
|
|
40
|
+
* valid default. The Gateway is a router across every vendor's id space at
|
|
41
|
+
* once, so there is no default that is right for it — only one vendor's guess
|
|
42
|
+
* baked into a provider-agnostic template.
|
|
43
|
+
* · A forwarded model id on a \`needs-proxy\` route is a spend lever handed to
|
|
44
|
+
* anything that can POST here, and the Gateway bills per token per model.
|
|
45
|
+
*
|
|
46
|
+
* WHY THIS ID. It is the one this route has actually been driven against live —
|
|
47
|
+
* text, a single tool call and a multi-round tool loop, through the Gateway —
|
|
48
|
+
* and, unlike a frontier default, it ANSWERS ON A FREE GATEWAY ACCOUNT. A paid
|
|
49
|
+
* id fails a first \`npm run dev\` with
|
|
50
|
+
* \`Free tier users do not have access to this model\`, which reads as a broken
|
|
51
|
+
* scaffold rather than as a billing setting. It also supports tools and
|
|
52
|
+
* reasoning, so the two things this route re-frames are reachable by default,
|
|
53
|
+
* and it is cheaper than gpt-4o by roughly two orders of magnitude.
|
|
54
|
+
*/
|
|
55
|
+
const MODEL = 'openai/gpt-oss-120b';
|
|
56
|
+
|
|
20
57
|
/**
|
|
21
58
|
* One attachment to an AI SDK FilePart.
|
|
22
59
|
*
|
|
@@ -88,21 +125,211 @@ function toModelMessages(messages: ChatRequestBody['messages']): ModelMessage[]
|
|
|
88
125
|
});
|
|
89
126
|
}
|
|
90
127
|
|
|
128
|
+
/** The OpenAI function-calling envelope the front end sends, narrowed from
|
|
129
|
+
* \`unknown[]\`. Declared as this integration's \`clientToolFormat\`. */
|
|
130
|
+
type OpenAIFunctionTool = {
|
|
131
|
+
function?: { name?: string; description?: string; parameters?: unknown };
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* OpenAI function schemas -> the AI SDK's own ToolSet.
|
|
136
|
+
*
|
|
137
|
+
* \`dynamicTool\` is the helper for a schema known only at RUNTIME. The ordinary
|
|
138
|
+
* \`tool()\` infers its input type from a Zod schema written in the route, which
|
|
139
|
+
* a list arriving in the request body cannot have.
|
|
140
|
+
*
|
|
141
|
+
* NO \`execute\`, deliberately. A tool the SDK can run makes the ROUTE the loop
|
|
142
|
+
* owner: streamText would call it, feed the result back and answer in a single
|
|
143
|
+
* response, so the tool call would never reach the browser and \`<kai-tool>\`
|
|
144
|
+
* would have nothing to render. Without \`execute\` the SDK emits the call and
|
|
145
|
+
* stops, which is the contract the kit's front end already implements — run the
|
|
146
|
+
* tool, \`applyToolOutput\`, POST the thread again.
|
|
147
|
+
*/
|
|
148
|
+
function toToolSet(tools: ChatRequestBody['tools']): ToolSet | undefined {
|
|
149
|
+
if (!tools?.length) return undefined;
|
|
150
|
+
const out: ToolSet = {};
|
|
151
|
+
for (const raw of tools) {
|
|
152
|
+
const fn = (raw as OpenAIFunctionTool).function;
|
|
153
|
+
if (!fn?.name) continue;
|
|
154
|
+
out[fn.name] = dynamicTool({
|
|
155
|
+
description: fn.description ?? '',
|
|
156
|
+
inputSchema: jsonSchema((fn.parameters as JSONSchema7 | undefined) ?? { type: 'object' }),
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
return Object.keys(out).length > 0 ? out : undefined;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** AI SDK finish reasons -> OpenAI's spelling. They agree on 'stop', 'length'
|
|
163
|
+
* and 'error' and disagree on the other two, and readOpenAIStream's table reads
|
|
164
|
+
* OpenAI's — so an unmapped 'tool-calls' normalises to 'other' and the turn
|
|
165
|
+
* stops saying why it stopped. */
|
|
166
|
+
const FINISH_REASONS: Record<string, string> = {
|
|
167
|
+
'tool-calls': 'tool_calls',
|
|
168
|
+
'content-filter': 'content_filter',
|
|
169
|
+
};
|
|
170
|
+
|
|
91
171
|
async function chatHandler(request: Request): Promise<Response> {
|
|
92
|
-
const { messages } = await readChatRequest(request);
|
|
172
|
+
const { messages, tools } = await readChatRequest(request);
|
|
173
|
+
const toolSet = toToolSet(tools);
|
|
174
|
+
const prompt = toModelMessages(messages);
|
|
175
|
+
|
|
176
|
+
// THE SYSTEM TURN DOES NOT GO IN \`messages\`, and this is the one that costs a
|
|
177
|
+
// live run to find. \`SystemModelMessage\` is still part of the \`ModelMessage\`
|
|
178
|
+
// union, so a system entry in this array TYPECHECKS — and then \`ai\` v7's
|
|
179
|
+
// \`standardizePrompt\` throws \`InvalidPromptError: System messages are not
|
|
180
|
+
// allowed in the prompt or messages fields. Use the instructions option
|
|
181
|
+
// instead.\` The kit's own encoder puts the system prompt at \`messages[0]\`, so
|
|
182
|
+
// that is every single turn of a scaffolded app, not an edge case.
|
|
183
|
+
//
|
|
184
|
+
// Hoisted rather than joined into a string: \`instructions\` takes the message
|
|
185
|
+
// array, so several system turns keep their order and their count.
|
|
186
|
+
//
|
|
187
|
+
// On \`ai\` v5/v6 there is no \`instructions\` option and a system message in
|
|
188
|
+
// \`messages\` is correct — drop this split and pass \`prompt\` straight through
|
|
189
|
+
// if you pin an older SDK.
|
|
190
|
+
const instructions = prompt.filter((m): m is SystemModelMessage => m.role === 'system');
|
|
191
|
+
const conversation = prompt.filter((m) => m.role !== 'system');
|
|
93
192
|
|
|
193
|
+
// \`streamText\` is not awaited: it returns synchronously and does its work as
|
|
194
|
+
// the stream is iterated. Prompt validation is part of that work, so an
|
|
195
|
+
// invalid prompt surfaces from \`for await (… of result.fullStream)\` below
|
|
196
|
+
// rather than from this line — which is why the catch that reports it lives
|
|
197
|
+
// in the stream and not around this call. Confirmed by observation: a rejected
|
|
198
|
+
// prompt reached the browser as an in-band error frame, not as a 500.
|
|
94
199
|
const result = streamText({
|
|
95
|
-
model:
|
|
96
|
-
|
|
200
|
+
model: MODEL, // AI Gateway id; needs AI_GATEWAY_API_KEY
|
|
201
|
+
...(instructions.length > 0 ? { instructions } : {}),
|
|
202
|
+
messages: conversation,
|
|
203
|
+
...(toolSet ? { tools: toolSet } : {}),
|
|
97
204
|
});
|
|
98
205
|
|
|
99
206
|
const encoder = new TextEncoder();
|
|
207
|
+
|
|
208
|
+
// OpenAI correlates tool-call fragments by their POSITION in the tool_calls
|
|
209
|
+
// array; the SDK identifies each call by id and never sends a position. So one
|
|
210
|
+
// is derived from the other, in first-seen order, and every fragment of a call
|
|
211
|
+
// carries the same number. Getting this wrong does not throw — the fragments
|
|
212
|
+
// land on the wrong call and the arguments come out as spliced JSON.
|
|
213
|
+
const toolIndex = new Map<string, number>();
|
|
214
|
+
const indexOf = (id: string): number => {
|
|
215
|
+
const known = toolIndex.get(id);
|
|
216
|
+
if (known !== undefined) return known;
|
|
217
|
+
const next = toolIndex.size;
|
|
218
|
+
toolIndex.set(id, next);
|
|
219
|
+
return next;
|
|
220
|
+
};
|
|
221
|
+
// How many argument characters a call streamed. \`tool-call\` re-sends the whole
|
|
222
|
+
// input at the end, so emitting it unconditionally would DOUBLE the arguments
|
|
223
|
+
// of every call that streamed — and skipping it unconditionally would empty
|
|
224
|
+
// the arguments of any provider that does not stream them. Neither is safe to
|
|
225
|
+
// assume, so the decision is made per call from what actually arrived.
|
|
226
|
+
const streamedArgs = new Map<string, number>();
|
|
227
|
+
|
|
100
228
|
const sse = new ReadableStream({
|
|
101
229
|
async start(controller) {
|
|
230
|
+
const send = (chunk: unknown): void => {
|
|
231
|
+
controller.enqueue(encoder.encode(\`data: \${JSON.stringify(chunk)}\\n\\n\`));
|
|
232
|
+
};
|
|
102
233
|
try {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
234
|
+
// fullStream, NOT textStream. textStream is text deltas only: a tool call
|
|
235
|
+
// or a reasoning block goes past it silently, so a route built on it
|
|
236
|
+
// emits a plain answer and nothing else however the model replied.
|
|
237
|
+
for await (const part of result.fullStream) {
|
|
238
|
+
switch (part.type) {
|
|
239
|
+
case 'text-delta':
|
|
240
|
+
send({ choices: [{ delta: { content: part.text } }] });
|
|
241
|
+
break;
|
|
242
|
+
|
|
243
|
+
case 'reasoning-delta':
|
|
244
|
+
send({ choices: [{ delta: { reasoning: part.text } }] });
|
|
245
|
+
break;
|
|
246
|
+
|
|
247
|
+
// The call is ANNOUNCED here, before its arguments exist, which is
|
|
248
|
+
// what lets <kai-tool> open a panel with the tool's name in it while
|
|
249
|
+
// the arguments are still being written.
|
|
250
|
+
case 'tool-input-start':
|
|
251
|
+
streamedArgs.set(part.id, 0);
|
|
252
|
+
send({
|
|
253
|
+
choices: [{
|
|
254
|
+
delta: {
|
|
255
|
+
tool_calls: [{
|
|
256
|
+
index: indexOf(part.id),
|
|
257
|
+
id: part.id,
|
|
258
|
+
type: 'function',
|
|
259
|
+
function: { name: part.toolName, arguments: '' },
|
|
260
|
+
}],
|
|
261
|
+
},
|
|
262
|
+
}],
|
|
263
|
+
});
|
|
264
|
+
break;
|
|
265
|
+
|
|
266
|
+
case 'tool-input-delta':
|
|
267
|
+
streamedArgs.set(part.id, (streamedArgs.get(part.id) ?? 0) + part.delta.length);
|
|
268
|
+
send({
|
|
269
|
+
choices: [{
|
|
270
|
+
delta: { tool_calls: [{ index: indexOf(part.id), function: { arguments: part.delta } }] },
|
|
271
|
+
}],
|
|
272
|
+
});
|
|
273
|
+
break;
|
|
274
|
+
|
|
275
|
+
case 'tool-call':
|
|
276
|
+
// Only when nothing streamed: see \`streamedArgs\`.
|
|
277
|
+
if ((streamedArgs.get(part.toolCallId) ?? 0) === 0) {
|
|
278
|
+
send({
|
|
279
|
+
choices: [{
|
|
280
|
+
delta: {
|
|
281
|
+
tool_calls: [{
|
|
282
|
+
index: indexOf(part.toolCallId),
|
|
283
|
+
id: part.toolCallId,
|
|
284
|
+
type: 'function',
|
|
285
|
+
function: {
|
|
286
|
+
name: part.toolName,
|
|
287
|
+
arguments: JSON.stringify(part.input ?? {}),
|
|
288
|
+
},
|
|
289
|
+
}],
|
|
290
|
+
},
|
|
291
|
+
}],
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
break;
|
|
295
|
+
|
|
296
|
+
// One frame carries both, the way chat-completions sends them.
|
|
297
|
+
// \`reasoning_tokens\` is the number that proves thinking happened even
|
|
298
|
+
// when the provider streamed no reasoning text.
|
|
299
|
+
case 'finish':
|
|
300
|
+
send({
|
|
301
|
+
choices: [{
|
|
302
|
+
delta: {},
|
|
303
|
+
finish_reason: FINISH_REASONS[part.finishReason] ?? part.finishReason,
|
|
304
|
+
}],
|
|
305
|
+
usage: {
|
|
306
|
+
prompt_tokens: part.totalUsage.inputTokens,
|
|
307
|
+
completion_tokens: part.totalUsage.outputTokens,
|
|
308
|
+
total_tokens: part.totalUsage.totalTokens,
|
|
309
|
+
completion_tokens_details: {
|
|
310
|
+
reasoning_tokens: part.totalUsage.outputTokenDetails.reasoningTokens,
|
|
311
|
+
},
|
|
312
|
+
},
|
|
313
|
+
});
|
|
314
|
+
break;
|
|
315
|
+
|
|
316
|
+
// An error the SDK caught mid-stream. The status is long spent, so it
|
|
317
|
+
// goes IN BAND like the catch below.
|
|
318
|
+
case 'error':
|
|
319
|
+
send({
|
|
320
|
+
error: {
|
|
321
|
+
message: part.error instanceof Error ? part.error.message : String(part.error),
|
|
322
|
+
},
|
|
323
|
+
});
|
|
324
|
+
break;
|
|
325
|
+
|
|
326
|
+
// Everything else — text-start/end, tool-input-end, sources, files,
|
|
327
|
+
// step boundaries, raw provider frames — has no OpenAI-wire spelling
|
|
328
|
+
// and is dropped. \`source\` is the one worth knowing about: map it to
|
|
329
|
+
// \`delta.annotations[].url_citation\` if your model cites its sources.
|
|
330
|
+
default:
|
|
331
|
+
break;
|
|
332
|
+
}
|
|
106
333
|
}
|
|
107
334
|
} catch (err) {
|
|
108
335
|
// The status is spent by the time the SDK fails — the headers went out
|
|
@@ -110,7 +337,7 @@ async function chatHandler(request: Request): Promise<Response> {
|
|
|
110
337
|
// this on turn.error and keeps whatever streamed before it. Without it a
|
|
111
338
|
// failed key is an empty bubble and nothing in the console.
|
|
112
339
|
const message = err instanceof Error ? err.message : 'Model stream failed';
|
|
113
|
-
|
|
340
|
+
send({ error: { message } });
|
|
114
341
|
}
|
|
115
342
|
controller.enqueue(encoder.encode('data: [DONE]\\n\\n'));
|
|
116
343
|
controller.close();
|
|
@@ -127,12 +354,26 @@ async function chatHandler(request: Request): Promise<Response> {
|
|
|
127
354
|
},
|
|
128
355
|
});
|
|
129
356
|
}`,
|
|
130
|
-
streamMapping:
|
|
131
|
-
|
|
357
|
+
streamMapping:
|
|
358
|
+
"The Vercel AI SDK's toUIMessageStreamResponse() and toTextStreamResponse() don't emit OpenAI-format SSE, so the route re-frames the stream itself and readOpenAIStream from @kitn.ai/ui/wire parses it exactly as it does every other integration. Iterate result.fullStream, NOT result.textStream: textStream carries text deltas only, so a route built on it emits a plain answer however the model replied and drops every tool call and every reasoning block silently. fullStream yields typed parts: text-delta.text -> delta.content, reasoning-delta.text -> delta.reasoning, tool-input-start plus its tool-input-delta fragments -> delta.tool_calls, finish -> finish_reason plus a usage frame, error -> an in-band {error:{message}}. Two traps. (1) OpenAI correlates tool-call fragments by their POSITION in the tool_calls array and the SDK only ever gives an id, so the route keeps an id -> index map; passing anything else through as the index splices one call's arguments into another. (2) fullStream sends the complete input AGAIN on the tool-call part after streaming it in fragments, so emitting both doubles the arguments — the route tracks how much each call streamed and emits the tool-call part only for a provider that streamed none. Parts with no OpenAI spelling (text-start/end, tool-input-end, step boundaries, raw) are dropped; source parts have one — delta.annotations[].url_citation — and are left unmapped because the SDK's Source union carries document sources a url_citation cannot express. On the REQUEST side the trap that only a live run finds: ai v7 REFUSES a system message inside `messages` (InvalidPromptError, 'Use the instructions option instead') even though SystemModelMessage is still in the ModelMessage union and therefore typechecks — and the kit's encoder puts the system prompt at messages[0], so that is every turn. Hoist system turns into `instructions` and pass the rest as `messages`. That failure arrives from ITERATING fullStream, not from the streamText() call — streamText returns synchronously and validates as the stream is read — so the in-band catch around the loop is what reports it, and it reaches the browser as an error frame rather than as a 500.",
|
|
359
|
+
runNote:
|
|
360
|
+
"Set AI_GATEWAY_API_KEY for the AI Gateway (string model id form: creator/model-name). The route pins `const MODEL = 'openai/gpt-oss-120b'` — one line, at the top, and any id the Gateway routes works in it. That id is pinned because it answers on a FREE Gateway account: most ids (deepseek/*, meta/*, anthropic/*) return `Free tier users do not have access to this model` or a free-tier rate limit until the account has paid credits, which looks like a broken scaffold rather than a billing setting. For direct provider access, import its provider package (e.g. @ai-sdk/openai) and set the corresponding key (e.g. OPENAI_API_KEY). The tools the front end posts become `dynamicTool`s with NO `execute`, which is what keeps the tool loop in the app: the SDK emits the call and stops, the app runs it, renders it in <kai-tool> and posts the thread back. Give a tool an `execute` and the SDK runs the whole loop server-side, so nothing reaches the browser but the final sentence.",
|
|
132
361
|
docsSlug: 'integrations/vercel-ai-sdk',
|
|
133
|
-
//
|
|
134
|
-
//
|
|
135
|
-
|
|
362
|
+
// `tools` only. `model` is deliberately NOT forwarded — the route pins it in
|
|
363
|
+
// one named const, and see that const's comment for why the Gateway is the one
|
|
364
|
+
// host where a client-supplied id has no correct default. The catalog check
|
|
365
|
+
// agrees from the other direction: `every integration that forwards a model
|
|
366
|
+
// emits one valid for the host it POSTs to` reads the host off the route's own
|
|
367
|
+
// fetch(), and this route makes no fetch call at all — the SDK owns the
|
|
368
|
+
// transport — so a forwarded model here could not be validated against
|
|
369
|
+
// anything.
|
|
370
|
+
forwardsFromClient: ['tools'],
|
|
371
|
+
// 'openai': the ROUTE's request contract, not the SDK's own. `toToolSet` reads
|
|
372
|
+
// `raw.function.name` / `.function.parameters` off each entry — the OpenAI
|
|
373
|
+
// function-calling envelope — and rebuilds it as a `dynamicTool` with a
|
|
374
|
+
// `jsonSchema()` input. Sending the SDK's own tool shape from the client would
|
|
375
|
+
// leave `.function` undefined and every tool would arrive nameless.
|
|
376
|
+
clientToolFormat: 'openai',
|
|
136
377
|
// `ai` only. A direct provider (e.g. @ai-sdk/openai) is the alternative path
|
|
137
378
|
// described in runNote, not what this route imports, so it is not listed: the
|
|
138
379
|
// rule is what the emitted code actually imports.
|
|
@@ -8,6 +8,13 @@ import {
|
|
|
8
8
|
listArchetypes,
|
|
9
9
|
listIntegrations,
|
|
10
10
|
} from '../../registry';
|
|
11
|
+
// The route-emission facts, read rather than restated — and deliberately living
|
|
12
|
+
// OUTSIDE this file. This module builds its zod input schema at module scope, so
|
|
13
|
+
// a bundler asked for one of these three has to keep all 5,300 lines of it and
|
|
14
|
+
// all of zod with them; `create-kai`'s CLI bundle went 203 kB -> 904 kB when they
|
|
15
|
+
// were exported from here. See the header of `route-emit.ts`. Import FROM there,
|
|
16
|
+
// never re-export through here.
|
|
17
|
+
import { chatRoutePreamble, defaultModelFor } from '../../route-emit';
|
|
11
18
|
// The kit's media-type declaration, read rather than restated. This is the one
|
|
12
19
|
// import in agent-tooling that reaches outside itself, and the module it reaches
|
|
13
20
|
// for is the reason: `wire/media-types.ts` is pure (no I/O, no DOM, no solid-js),
|
|
@@ -68,11 +75,11 @@ interface PlacementStyle {
|
|
|
68
75
|
altNote?: string[];
|
|
69
76
|
}
|
|
70
77
|
|
|
71
|
-
// The chat element must fill its container.
|
|
72
|
-
//
|
|
73
|
-
//
|
|
78
|
+
// The chat element must fill its container. Every placement below is a
|
|
79
|
+
// `display: flex; flex-direction: column` shell, so the element is always a flex
|
|
80
|
+
// child and this is the only fill there is. A `height: 100%` variant for a plain
|
|
81
|
+
// block container used to sit here beside it; nothing ever selected it.
|
|
74
82
|
const FLEX_FILL = 'flex: 1; min-height: 0;';
|
|
75
|
-
const BLOCK_FILL = 'height: 100%; width: 100%;';
|
|
76
83
|
|
|
77
84
|
/**
|
|
78
85
|
* full-page, and it has to be true in a STOCK starter — not just in an empty page.
|
|
@@ -808,65 +815,6 @@ function realBodyPayload(opts: { defaultModel?: string; tools: boolean }): (thre
|
|
|
808
815
|
};
|
|
809
816
|
}
|
|
810
817
|
|
|
811
|
-
// ── SCAF-8: per-integration default model ids ─────────────────────────────────
|
|
812
|
-
|
|
813
|
-
/**
|
|
814
|
-
* Default model id per integration whose route forwards one.
|
|
815
|
-
*
|
|
816
|
-
* THE ID IS HOST-SPECIFIC, and there is no such thing as a safe generic one.
|
|
817
|
-
* This used to fall through to `'openai/gpt-4o-mini'` for anything unlisted, on
|
|
818
|
-
* the reasoning that "a route that forwards the client's model is by definition
|
|
819
|
-
* pointed at an OpenAI-compatible endpoint". That was false twice over the
|
|
820
|
-
* moment a first-party provider landed: `openai/gpt-4o-mini` is an OPENROUTER
|
|
821
|
-
* slug — api.openai.com 404s the prefixed form, and api.anthropic.com rejects it
|
|
822
|
-
* outright — so a scaffold generated for the provider it names could not run
|
|
823
|
-
* against it.
|
|
824
|
-
*
|
|
825
|
-
* tsc cannot see any of this; every one of those strings compiles. The guard is
|
|
826
|
-
* `scaffold.test.ts` → "the emitted model id is valid for the host its route
|
|
827
|
-
* POSTs to", which reads the id out of the EMITTED scaffold and the host out of
|
|
828
|
-
* the route source, so a new integration cannot reintroduce a wrong one.
|
|
829
|
-
*/
|
|
830
|
-
const CLIENT_MODEL_IDS: Record<string, string> = {
|
|
831
|
-
// Vendor-prefixed `vendor/model`: OpenRouter's own id space, and the ONLY one
|
|
832
|
-
// of the three where the prefix belongs.
|
|
833
|
-
openrouter: 'openai/gpt-4o-mini',
|
|
834
|
-
// No vendor prefix. This is what the route already pinned, so moving the knob
|
|
835
|
-
// to the client changes the wire not at all.
|
|
836
|
-
openai: 'gpt-4o-mini',
|
|
837
|
-
// Anthropic's id space. Matches what the route pinned; 'claude-sonnet-5' and
|
|
838
|
-
// 'claude-haiku-4-5' are the cheaper swaps (see this integration's runNote).
|
|
839
|
-
anthropic: 'claude-opus-5',
|
|
840
|
-
};
|
|
841
|
-
|
|
842
|
-
/**
|
|
843
|
-
* The default model id for an integration whose ROUTE reads the client's `model`
|
|
844
|
-
* field, and undefined for every other one.
|
|
845
|
-
*
|
|
846
|
-
* This used to be a substring test (`routeSrc.includes('model')`), which is true
|
|
847
|
-
* of any template that so much as writes `model: 'llama3.2'`. That emitted an
|
|
848
|
-
* editable `const model` into ollama, langgraph, vercel-ai-sdk and cloudflare
|
|
849
|
-
* scaffolds whose routes pin their own model and never read the field, so
|
|
850
|
-
* changing it did nothing, and cloudflare's default was not even a valid Workers
|
|
851
|
-
* AI id. `forwardsFromClient` states the fact instead of guessing at it.
|
|
852
|
-
*/
|
|
853
|
-
function defaultModelFor(integration: Integration): string | undefined {
|
|
854
|
-
if (!integration.forwardsFromClient.includes('model')) return undefined;
|
|
855
|
-
const id = CLIENT_MODEL_IDS[integration.id];
|
|
856
|
-
// No fallback, deliberately — the old `?? 'openai/gpt-4o-mini'` is what let a
|
|
857
|
-
// first-party provider inherit an OpenRouter slug and emit a scaffold that
|
|
858
|
-
// 404s on its own host. A model id is a per-host fact; an integration that
|
|
859
|
-
// forwards one has to say which.
|
|
860
|
-
if (id === undefined) {
|
|
861
|
-
throw new Error(
|
|
862
|
-
`Integration '${integration.id}' forwards the client's 'model' but has no CLIENT_MODEL_IDS entry, so the ` +
|
|
863
|
-
`scaffold would emit a model id that is not valid for the host its route POSTs to. Add one in ` +
|
|
864
|
-
`mcp/tools/scaffold.ts.`,
|
|
865
|
-
);
|
|
866
|
-
}
|
|
867
|
-
return id;
|
|
868
|
-
}
|
|
869
|
-
|
|
870
818
|
/**
|
|
871
819
|
* True when the scaffold should declare tool schemas and put them in the body.
|
|
872
820
|
*
|
|
@@ -4488,122 +4436,6 @@ const WEB_ROUTE_ADAPTERS: Record<string, WebRouteAdapter> = {
|
|
|
4488
4436
|
},
|
|
4489
4437
|
};
|
|
4490
4438
|
|
|
4491
|
-
/**
|
|
4492
|
-
* The request body, declared once per route file.
|
|
4493
|
-
*
|
|
4494
|
-
* `await request.json()` is `unknown` — it is whatever the client sent — so
|
|
4495
|
-
* destructuring it directly is TS2339 on EVERY field. That is not pedantry: it
|
|
4496
|
-
* is a hard `npm run build` failure the moment a Node-typed project compiles the
|
|
4497
|
-
* route, where `Request` comes from undici (`json(): Promise<unknown>`) rather
|
|
4498
|
-
* than from the DOM lib (`json(): Promise<any>`). A stock Vite app does exactly
|
|
4499
|
-
* that — `tsc -b` walks vite.config.ts → vite-chat-api.ts → src/server/chat.ts
|
|
4500
|
-
* with `lib` and no DOM — so the route ran fine and the build did not.
|
|
4501
|
-
*
|
|
4502
|
-
* `messages` is typed as the kit's OWN encoder output rather than restated
|
|
4503
|
-
* structurally, which keeps the two halves of the scaffold pinned to one type:
|
|
4504
|
-
* the front end sends `toOpenAIMessages(thread)`, and this is what that returns.
|
|
4505
|
-
* The import is type-only and erases at build time, so the route ships no
|
|
4506
|
-
* runtime dependency on the kit.
|
|
4507
|
-
*/
|
|
4508
|
-
const CHAT_REQUEST_BODY_IMPORT = `import type { OpenAIWireMessage } from '@kitn.ai/ui/wire';`;
|
|
4509
|
-
const CHAT_REQUEST_BODY_DECL = [
|
|
4510
|
-
`/**`,
|
|
4511
|
-
` * What the front end POSTs. \`request.json()\` is \`unknown\` (it is whatever the`,
|
|
4512
|
-
` * client sent), so the body is narrowed once here instead of at every use —`,
|
|
4513
|
-
` * without it this route does not compile under a server tsconfig. Widen it as`,
|
|
4514
|
-
` * you add fields of your own.`,
|
|
4515
|
-
` */`,
|
|
4516
|
-
`type ChatRequestBody = {`,
|
|
4517
|
-
` messages: OpenAIWireMessage[];`,
|
|
4518
|
-
` model?: string;`,
|
|
4519
|
-
` tools?: unknown[];`,
|
|
4520
|
-
`};`,
|
|
4521
|
-
``,
|
|
4522
|
-
`/** Narrow the JSON body once, at the edge. */`,
|
|
4523
|
-
`async function readChatRequest(request: Request): Promise<ChatRequestBody> {`,
|
|
4524
|
-
` return (await request.json()) as ChatRequestBody;`,
|
|
4525
|
-
`}`,
|
|
4526
|
-
];
|
|
4527
|
-
|
|
4528
|
-
/**
|
|
4529
|
-
* Attachments, on the way IN.
|
|
4530
|
-
*
|
|
4531
|
-
* A user turn's `content` is a plain string until it carries a file, at which
|
|
4532
|
-
* point `toOpenAIMessages` emits the ARRAY form. Every route that re-maps
|
|
4533
|
-
* messages into some other SDK's shape has to handle both, and the three that do
|
|
4534
|
-
* (anthropic, mastra, vercel-ai-sdk) were each written when only the string form
|
|
4535
|
-
* existed — so each one would have quietly dropped the attachment while still
|
|
4536
|
-
* compiling, which is the same defect the encoder was just fixed for.
|
|
4537
|
-
*
|
|
4538
|
-
* These two helpers are the shared half of that: flattening the wire shape is
|
|
4539
|
-
* identical everywhere, while the target shape is not, so each route maps
|
|
4540
|
-
* `WirePart[]` into its own SDK itself rather than inheriting a lowest common
|
|
4541
|
-
* denominator.
|
|
4542
|
-
*
|
|
4543
|
-
* Only injected into routes that actually call them — the eight pass-through
|
|
4544
|
-
* integrations forward `messages` untouched and need none of this, and an unused
|
|
4545
|
-
* declaration is a hard error under the gate's `--noUnusedLocals`.
|
|
4546
|
-
*/
|
|
4547
|
-
const CONTENT_PARTS_DECL = [
|
|
4548
|
-
`/** Where an attachment's bytes are: inline base64, or an address the PROVIDER`,
|
|
4549
|
-
` * fetches. Never both. */`,
|
|
4550
|
-
`type WireFileSource = { type: 'data'; data: string } | { type: 'url'; url: string };`,
|
|
4551
|
-
``,
|
|
4552
|
-
`/** One piece of a turn, with the string and array content forms flattened into`,
|
|
4553
|
-
` * a single shape. */`,
|
|
4554
|
-
`type WirePart =`,
|
|
4555
|
-
` | { kind: 'text'; text: string }`,
|
|
4556
|
-
` | { kind: 'file'; mediaType: string; filename?: string; source: WireFileSource };`,
|
|
4557
|
-
``,
|
|
4558
|
-
`const DATA_URI = /^data:([^;,]+);base64,([\\s\\S]*)$/;`,
|
|
4559
|
-
``,
|
|
4560
|
-
`/**`,
|
|
4561
|
-
` * Flatten a wire message's content into parts.`,
|
|
4562
|
-
` *`,
|
|
4563
|
-
` * An image sent by URL has no media type here — \`image_url\` carries only the`,
|
|
4564
|
-
` * address — so it reports the top-level segment \`'image'\`, which is all a URL`,
|
|
4565
|
-
` * source needs. Only images can reach that branch: the kit refuses to encode a`,
|
|
4566
|
-
` * remote PDF rather than guess at one.`,
|
|
4567
|
-
` */`,
|
|
4568
|
-
`function wireParts(content: OpenAIWireMessage['content']): WirePart[] {`,
|
|
4569
|
-
` if (content == null) return [];`,
|
|
4570
|
-
` if (typeof content === 'string') return content === '' ? [] : [{ kind: 'text', text: content }];`,
|
|
4571
|
-
` return content.map((part): WirePart => {`,
|
|
4572
|
-
` if (part.type === 'text') return { kind: 'text', text: part.text };`,
|
|
4573
|
-
` if (part.type === 'image_url') {`,
|
|
4574
|
-
` const asData = DATA_URI.exec(part.image_url.url);`,
|
|
4575
|
-
` return asData`,
|
|
4576
|
-
` ? { kind: 'file', mediaType: asData[1], source: { type: 'data', data: asData[2] } }`,
|
|
4577
|
-
` : { kind: 'file', mediaType: 'image', source: { type: 'url', url: part.image_url.url } };`,
|
|
4578
|
-
` }`,
|
|
4579
|
-
` const asData = DATA_URI.exec(part.file.file_data);`,
|
|
4580
|
-
` if (!asData) {`,
|
|
4581
|
-
` // LOUD on purpose. \`file_data\` is a data URI on this wire; anything else`,
|
|
4582
|
-
` // cannot be turned into bytes without fetching it, and forwarding a turn`,
|
|
4583
|
-
` // with the attachment quietly missing is the bug this whole path exists`,
|
|
4584
|
-
` // to prevent.`,
|
|
4585
|
-
` throw new Error(`,
|
|
4586
|
-
` 'Unsupported file content part: file_data must be a data: URI of the form data:<media type>;base64,<data>.',`,
|
|
4587
|
-
` );`,
|
|
4588
|
-
` }`,
|
|
4589
|
-
` return {`,
|
|
4590
|
-
` kind: 'file',`,
|
|
4591
|
-
` mediaType: asData[1],`,
|
|
4592
|
-
` filename: part.file.filename,`,
|
|
4593
|
-
` source: { type: 'data', data: asData[2] },`,
|
|
4594
|
-
` };`,
|
|
4595
|
-
` });`,
|
|
4596
|
-
`}`,
|
|
4597
|
-
``,
|
|
4598
|
-
`/** Just the text of a turn. System, assistant and tool messages are text-only`,
|
|
4599
|
-
` * on this wire, so this collapses the array form for them. */`,
|
|
4600
|
-
`function wireText(content: OpenAIWireMessage['content']): string {`,
|
|
4601
|
-
` return wireParts(content)`,
|
|
4602
|
-
` .map((p) => (p.kind === 'text' ? p.text : ''))`,
|
|
4603
|
-
` .join('');`,
|
|
4604
|
-
`}`,
|
|
4605
|
-
];
|
|
4606
|
-
|
|
4607
4439
|
/**
|
|
4608
4440
|
* Slot the body type in just above `chatHandler`.
|
|
4609
4441
|
*
|
|
@@ -4613,12 +4445,7 @@ const CONTENT_PARTS_DECL = [
|
|
|
4613
4445
|
* block that documents it — puts the declaration where a person would have
|
|
4614
4446
|
* written it.
|
|
4615
4447
|
*/
|
|
4616
|
-
function withChatRequestBody(fragment: string): string {
|
|
4617
|
-
// The content helpers ride along only where the route calls them; see
|
|
4618
|
-
// CONTENT_PARTS_DECL for why an unconditional injection would not compile.
|
|
4619
|
-
const decl = /\bwire(?:Parts|Text)\s*\(/.test(fragment)
|
|
4620
|
-
? [...CHAT_REQUEST_BODY_DECL, ``, ...CONTENT_PARTS_DECL]
|
|
4621
|
-
: CHAT_REQUEST_BODY_DECL;
|
|
4448
|
+
function withChatRequestBody(fragment: string, decl: readonly string[]): string {
|
|
4622
4449
|
const lines = fragment.split('\n');
|
|
4623
4450
|
let at = lines.findIndex((l) => /^(?:export\s+)?async function chatHandler\b/.test(l));
|
|
4624
4451
|
if (at < 0) return [...decl, ``, ...lines].join('\n');
|
|
@@ -4635,17 +4462,22 @@ function webRouteFor(integration: Integration, framework: string): RouteChoice |
|
|
|
4635
4462
|
// pull in an import to do it (SvelteKit's $env accessor). Both land at the top
|
|
4636
4463
|
// of the file, beside the adapter's own `before` lines.
|
|
4637
4464
|
const adapted = adapter.adaptFragment?.(fragment) ?? { fragment, imports: [] };
|
|
4465
|
+
// One preamble for the whole file: its `imports` go at the top beside the
|
|
4466
|
+
// adapter's own, its `decl` immediately above the handler. Both halves come
|
|
4467
|
+
// from `chatRoutePreamble` rather than from a constant here, so this emitter
|
|
4468
|
+
// and `create-kai`'s cannot answer "what does this route need" differently.
|
|
4469
|
+
const preamble = chatRoutePreamble(adapted.fragment);
|
|
4638
4470
|
return {
|
|
4639
4471
|
framework,
|
|
4640
4472
|
runtime: adapter.runtime,
|
|
4641
4473
|
exact: true,
|
|
4642
4474
|
template: [
|
|
4643
4475
|
`// ${adapter.file}`,
|
|
4644
|
-
|
|
4476
|
+
...preamble.imports,
|
|
4645
4477
|
...(adapter.before ?? []),
|
|
4646
4478
|
...adapted.imports,
|
|
4647
4479
|
``,
|
|
4648
|
-
withChatRequestBody(adapted.fragment),
|
|
4480
|
+
withChatRequestBody(adapted.fragment, preamble.decl),
|
|
4649
4481
|
...adapter.after,
|
|
4650
4482
|
].join('\n'),
|
|
4651
4483
|
};
|