@tinyweb_dev/agent-framework-llm 0.3.0 → 0.4.1
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/index.cjs +29 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +29 -11
- package/dist/index.js.map +1 -1
- package/dist/protocols/openai-chat.cjs +30 -11
- package/dist/protocols/openai-chat.cjs.map +1 -1
- package/dist/protocols/openai-chat.d.ts +25 -0
- package/dist/protocols/openai-chat.d.ts.map +1 -1
- package/dist/protocols/openai-chat.js +30 -12
- package/dist/protocols/openai-chat.js.map +1 -1
- package/dist/protocols/openai-compatible-chat.cjs +28 -11
- package/dist/protocols/openai-compatible-chat.cjs.map +1 -1
- package/dist/protocols/openai-compatible-chat.js +28 -11
- package/dist/protocols/openai-compatible-chat.js.map +1 -1
- package/dist/providers/openai-compatible.cjs +28 -11
- package/dist/providers/openai-compatible.cjs.map +1 -1
- package/dist/providers/openai-compatible.js +28 -11
- package/dist/providers/openai-compatible.js.map +1 -1
- package/dist/providers/openai.cjs +28 -11
- package/dist/providers/openai.cjs.map +1 -1
- package/dist/providers/openai.js +28 -11
- package/dist/providers/openai.js.map +1 -1
- package/package.json +2 -2
|
@@ -136,7 +136,30 @@ interface ParserState {
|
|
|
136
136
|
readonly finishReason?: FinishReason;
|
|
137
137
|
/** Running assistant text used to normalise cumulative `delta.content` providers. */
|
|
138
138
|
readonly textAcc: string;
|
|
139
|
+
/** Whether `delta.content` is incremental or cumulative; decided on the second text chunk. */
|
|
140
|
+
readonly textMode: TextDeltaMode;
|
|
139
141
|
}
|
|
142
|
+
/**
|
|
143
|
+
* OpenAI Chat streams are *incremental* (`delta.content` is the next fragment).
|
|
144
|
+
* Some OpenAI-compatible proxies instead re-send the full text so far on every
|
|
145
|
+
* chunk (cumulative snapshots). Blindly forwarding those as text-delta makes
|
|
146
|
+
* UIs that concatenate deltas render duplicated prose.
|
|
147
|
+
*
|
|
148
|
+
* The stream's mode is decided once, on the second non-empty chunk: it is
|
|
149
|
+
* cumulative only when that chunk strictly extends everything received so far.
|
|
150
|
+
* Prefix checks are never applied to an incremental stream: a fragment that
|
|
151
|
+
* merely equals the start of the answer ("H" after "Hai sheet là “") is new
|
|
152
|
+
* text, and dropping it silently corrupts the reply ("“ọc sinh”").
|
|
153
|
+
*
|
|
154
|
+
* Returns the fragment to emit (empty = skip), the new accumulated text and
|
|
155
|
+
* the (possibly newly decided) mode.
|
|
156
|
+
*/
|
|
157
|
+
type TextDeltaMode = "unknown" | "incremental" | "cumulative";
|
|
158
|
+
export declare const normaliseTextDelta: (prev: string, incoming: string, mode?: TextDeltaMode) => {
|
|
159
|
+
readonly emit: string;
|
|
160
|
+
readonly textAcc: string;
|
|
161
|
+
readonly mode: TextDeltaMode;
|
|
162
|
+
};
|
|
140
163
|
export declare const protocol: Protocol<{
|
|
141
164
|
readonly messages: readonly ({
|
|
142
165
|
readonly content: string;
|
|
@@ -202,6 +225,7 @@ export declare const protocol: Protocol<{
|
|
|
202
225
|
}, string, {
|
|
203
226
|
readonly choices: readonly {
|
|
204
227
|
readonly delta?: {
|
|
228
|
+
readonly reasoning?: string | null | undefined;
|
|
205
229
|
readonly content?: string | null | undefined;
|
|
206
230
|
readonly tool_calls?: readonly {
|
|
207
231
|
readonly index: number;
|
|
@@ -211,6 +235,7 @@ export declare const protocol: Protocol<{
|
|
|
211
235
|
} | null | undefined;
|
|
212
236
|
readonly id?: string | null | undefined;
|
|
213
237
|
}[] | null | undefined;
|
|
238
|
+
readonly reasoning_content?: string | null | undefined;
|
|
214
239
|
} | null | undefined;
|
|
215
240
|
readonly finish_reason?: string | null | undefined;
|
|
216
241
|
}[];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-chat.d.ts","sourceRoot":"","sources":["../../src/protocols/openai-chat.ts"],"names":[],"mappings":"AACA,OAAO,EAAwB,MAAM,EAAE,MAAM,QAAQ,CAAA;AACrD,OAAO,EAAE,KAAK,EA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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;sCAQhB,CAAA;AAKF,eAAO,MAAM,KAAK,oHAAc,CAAA;AAEhC,OAAO,KAAK,UAAU,MAAM,eAAe,CAAA"}
|
|
@@ -18,6 +18,7 @@ __export(openai_chat_exports, {
|
|
|
18
18
|
bodyFields: () => bodyFields,
|
|
19
19
|
httpTransport: () => httpTransport,
|
|
20
20
|
model: () => model2,
|
|
21
|
+
normaliseTextDelta: () => normaliseTextDelta,
|
|
21
22
|
protocol: () => protocol,
|
|
22
23
|
route: () => route
|
|
23
24
|
});
|
|
@@ -2162,6 +2163,14 @@ var OpenAIChatToolCallDelta = Schema.Struct({
|
|
|
2162
2163
|
});
|
|
2163
2164
|
var OpenAIChatDelta = Schema.Struct({
|
|
2164
2165
|
content: optionalNull(Schema.String),
|
|
2166
|
+
/**
|
|
2167
|
+
* Thinking / chain-of-thought stream. DeepSeek + MiniMax native and
|
|
2168
|
+
* LiteLLM-normalized gateways use `reasoning_content`; OpenRouter (and some
|
|
2169
|
+
* Grok mirrors) use `reasoning`. Either must become `reasoning-delta` so
|
|
2170
|
+
* UIs are not stuck on a pre-text thinking spinner while the wire is live.
|
|
2171
|
+
*/
|
|
2172
|
+
reasoning_content: optionalNull(Schema.String),
|
|
2173
|
+
reasoning: optionalNull(Schema.String),
|
|
2165
2174
|
tool_calls: optionalNull(Schema.Array(OpenAIChatToolCallDelta))
|
|
2166
2175
|
});
|
|
2167
2176
|
var OpenAIChatChoice = Schema.Struct({
|
|
@@ -2318,16 +2327,17 @@ var mapUsage = (usage) => {
|
|
|
2318
2327
|
providerMetadata: { openai: usage }
|
|
2319
2328
|
});
|
|
2320
2329
|
};
|
|
2321
|
-
var normaliseTextDelta = (prev, incoming) => {
|
|
2322
|
-
if (!incoming) return { emit: "", textAcc: prev };
|
|
2323
|
-
if (
|
|
2324
|
-
|
|
2325
|
-
|
|
2330
|
+
var normaliseTextDelta = (prev, incoming, mode = "unknown") => {
|
|
2331
|
+
if (!incoming) return { emit: "", textAcc: prev, mode };
|
|
2332
|
+
if (mode === "unknown") {
|
|
2333
|
+
if (prev.length === 0) return { emit: incoming, textAcc: incoming, mode };
|
|
2334
|
+
mode = incoming.length > prev.length && incoming.startsWith(prev) ? "cumulative" : "incremental";
|
|
2326
2335
|
}
|
|
2327
|
-
if (
|
|
2328
|
-
return { emit:
|
|
2336
|
+
if (mode === "cumulative") {
|
|
2337
|
+
if (incoming.startsWith(prev)) return { emit: incoming.slice(prev.length), textAcc: incoming, mode };
|
|
2338
|
+
if (prev.startsWith(incoming)) return { emit: "", textAcc: prev, mode };
|
|
2329
2339
|
}
|
|
2330
|
-
return { emit: incoming, textAcc: prev + incoming };
|
|
2340
|
+
return { emit: incoming, textAcc: prev + incoming, mode };
|
|
2331
2341
|
};
|
|
2332
2342
|
var step = (state, event) => Effect.gen(function* () {
|
|
2333
2343
|
const events = [];
|
|
@@ -2338,9 +2348,15 @@ var step = (state, event) => Effect.gen(function* () {
|
|
|
2338
2348
|
const toolDeltas = delta?.tool_calls ?? [];
|
|
2339
2349
|
let tools = state.tools;
|
|
2340
2350
|
let textAcc = state.textAcc;
|
|
2351
|
+
let textMode = state.textMode;
|
|
2352
|
+
const reasoningChunk = delta?.reasoning_content ?? delta?.reasoning;
|
|
2353
|
+
if (reasoningChunk) {
|
|
2354
|
+
events.push(LLMEvent.reasoningDelta({ id: "reasoning-0", text: reasoningChunk }));
|
|
2355
|
+
}
|
|
2341
2356
|
if (delta?.content) {
|
|
2342
|
-
const normalised = normaliseTextDelta(textAcc, delta.content);
|
|
2357
|
+
const normalised = normaliseTextDelta(textAcc, delta.content, textMode);
|
|
2343
2358
|
textAcc = normalised.textAcc;
|
|
2359
|
+
textMode = normalised.mode;
|
|
2344
2360
|
if (normalised.emit) {
|
|
2345
2361
|
events.push(LLMEvent.textDelta({ id: "text-0", text: normalised.emit }));
|
|
2346
2362
|
}
|
|
@@ -2364,7 +2380,8 @@ var step = (state, event) => Effect.gen(function* () {
|
|
|
2364
2380
|
toolCallEvents: finished?.events ?? state.toolCallEvents,
|
|
2365
2381
|
usage,
|
|
2366
2382
|
finishReason,
|
|
2367
|
-
textAcc
|
|
2383
|
+
textAcc,
|
|
2384
|
+
textMode
|
|
2368
2385
|
},
|
|
2369
2386
|
events
|
|
2370
2387
|
];
|
|
@@ -2385,7 +2402,8 @@ var protocol = protocol_exports.make({
|
|
|
2385
2402
|
initial: () => ({
|
|
2386
2403
|
tools: tool_stream_exports.empty(),
|
|
2387
2404
|
toolCallEvents: [],
|
|
2388
|
-
textAcc: ""
|
|
2405
|
+
textAcc: "",
|
|
2406
|
+
textMode: "unknown"
|
|
2389
2407
|
}),
|
|
2390
2408
|
step,
|
|
2391
2409
|
onHalt: finishEvents
|
|
@@ -2409,6 +2427,6 @@ var route = Route.make({
|
|
|
2409
2427
|
});
|
|
2410
2428
|
var model2 = route.model;
|
|
2411
2429
|
|
|
2412
|
-
export { DEFAULT_BASE_URL, openai_chat_exports as OpenAIChat, PATH, bodyFields, httpTransport, model2 as model, protocol, route };
|
|
2430
|
+
export { DEFAULT_BASE_URL, openai_chat_exports as OpenAIChat, PATH, bodyFields, httpTransport, model2 as model, normaliseTextDelta, protocol, route };
|
|
2413
2431
|
//# sourceMappingURL=openai-chat.js.map
|
|
2414
2432
|
//# sourceMappingURL=openai-chat.js.map
|