@tinyweb_dev/agent-framework-llm 0.4.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.
@@ -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;
@@ -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,EAAE,MAAM,iBAAiB,CAAA;AAIvC,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,EAAE,QAAQ,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,CAAC,QAAQ,EAAE,aAAa,CAAA;CACjC;AA8KD;;;;;;;;;;;;;;GAcG;AACH,KAAK,aAAa,GAAG,SAAS,GAAG,aAAa,GAAG,YAAY,CAAA;AAE7D,eAAO,MAAM,kBAAkB,GAC7B,MAAM,MAAM,EACZ,UAAU,MAAM,EAChB,OAAM,aAAyB,KAC9B;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAA;CAajF,CAAA;AAoED,eAAO,MAAM,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;eAiBnB,CAAA;AAIF,eAAO,MAAM,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;UAKxB,CAAA;AAEF,eAAO,MAAM,KAAK;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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
  });
@@ -2326,16 +2327,17 @@ var mapUsage = (usage) => {
2326
2327
  providerMetadata: { openai: usage }
2327
2328
  });
2328
2329
  };
2329
- var normaliseTextDelta = (prev, incoming) => {
2330
- if (!incoming) return { emit: "", textAcc: prev };
2331
- if (incoming === prev) return { emit: "", textAcc: prev };
2332
- if (prev.length > 0 && incoming.startsWith(prev)) {
2333
- return { emit: incoming.slice(prev.length), textAcc: incoming };
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";
2334
2335
  }
2335
- if (prev.length > 0 && prev.startsWith(incoming)) {
2336
- return { emit: "", textAcc: prev };
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 };
2337
2339
  }
2338
- return { emit: incoming, textAcc: prev + incoming };
2340
+ return { emit: incoming, textAcc: prev + incoming, mode };
2339
2341
  };
2340
2342
  var step = (state, event) => Effect.gen(function* () {
2341
2343
  const events = [];
@@ -2346,13 +2348,15 @@ var step = (state, event) => Effect.gen(function* () {
2346
2348
  const toolDeltas = delta?.tool_calls ?? [];
2347
2349
  let tools = state.tools;
2348
2350
  let textAcc = state.textAcc;
2351
+ let textMode = state.textMode;
2349
2352
  const reasoningChunk = delta?.reasoning_content ?? delta?.reasoning;
2350
2353
  if (reasoningChunk) {
2351
2354
  events.push(LLMEvent.reasoningDelta({ id: "reasoning-0", text: reasoningChunk }));
2352
2355
  }
2353
2356
  if (delta?.content) {
2354
- const normalised = normaliseTextDelta(textAcc, delta.content);
2357
+ const normalised = normaliseTextDelta(textAcc, delta.content, textMode);
2355
2358
  textAcc = normalised.textAcc;
2359
+ textMode = normalised.mode;
2356
2360
  if (normalised.emit) {
2357
2361
  events.push(LLMEvent.textDelta({ id: "text-0", text: normalised.emit }));
2358
2362
  }
@@ -2376,7 +2380,8 @@ var step = (state, event) => Effect.gen(function* () {
2376
2380
  toolCallEvents: finished?.events ?? state.toolCallEvents,
2377
2381
  usage,
2378
2382
  finishReason,
2379
- textAcc
2383
+ textAcc,
2384
+ textMode
2380
2385
  },
2381
2386
  events
2382
2387
  ];
@@ -2397,7 +2402,8 @@ var protocol = protocol_exports.make({
2397
2402
  initial: () => ({
2398
2403
  tools: tool_stream_exports.empty(),
2399
2404
  toolCallEvents: [],
2400
- textAcc: ""
2405
+ textAcc: "",
2406
+ textMode: "unknown"
2401
2407
  }),
2402
2408
  step,
2403
2409
  onHalt: finishEvents
@@ -2421,6 +2427,6 @@ var route = Route.make({
2421
2427
  });
2422
2428
  var model2 = route.model;
2423
2429
 
2424
- 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 };
2425
2431
  //# sourceMappingURL=openai-chat.js.map
2426
2432
  //# sourceMappingURL=openai-chat.js.map