@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.
@@ -2365,16 +2365,17 @@ var mapUsage = (usage) => {
2365
2365
  providerMetadata: { openai: usage }
2366
2366
  });
2367
2367
  };
2368
- var normaliseTextDelta = (prev, incoming) => {
2369
- if (!incoming) return { emit: "", textAcc: prev };
2370
- if (incoming === prev) return { emit: "", textAcc: prev };
2371
- if (prev.length > 0 && incoming.startsWith(prev)) {
2372
- return { emit: incoming.slice(prev.length), textAcc: incoming };
2368
+ var normaliseTextDelta = (prev, incoming, mode = "unknown") => {
2369
+ if (!incoming) return { emit: "", textAcc: prev, mode };
2370
+ if (mode === "unknown") {
2371
+ if (prev.length === 0) return { emit: incoming, textAcc: incoming, mode };
2372
+ mode = incoming.length > prev.length && incoming.startsWith(prev) ? "cumulative" : "incremental";
2373
2373
  }
2374
- if (prev.length > 0 && prev.startsWith(incoming)) {
2375
- return { emit: "", textAcc: prev };
2374
+ if (mode === "cumulative") {
2375
+ if (incoming.startsWith(prev)) return { emit: incoming.slice(prev.length), textAcc: incoming, mode };
2376
+ if (prev.startsWith(incoming)) return { emit: "", textAcc: prev, mode };
2376
2377
  }
2377
- return { emit: incoming, textAcc: prev + incoming };
2378
+ return { emit: incoming, textAcc: prev + incoming, mode };
2378
2379
  };
2379
2380
  var step = (state, event) => effect$1.Effect.gen(function* () {
2380
2381
  const events = [];
@@ -2385,13 +2386,15 @@ var step = (state, event) => effect$1.Effect.gen(function* () {
2385
2386
  const toolDeltas = delta?.tool_calls ?? [];
2386
2387
  let tools = state.tools;
2387
2388
  let textAcc = state.textAcc;
2389
+ let textMode = state.textMode;
2388
2390
  const reasoningChunk = delta?.reasoning_content ?? delta?.reasoning;
2389
2391
  if (reasoningChunk) {
2390
2392
  events.push(LLMEvent.reasoningDelta({ id: "reasoning-0", text: reasoningChunk }));
2391
2393
  }
2392
2394
  if (delta?.content) {
2393
- const normalised = normaliseTextDelta(textAcc, delta.content);
2395
+ const normalised = normaliseTextDelta(textAcc, delta.content, textMode);
2394
2396
  textAcc = normalised.textAcc;
2397
+ textMode = normalised.mode;
2395
2398
  if (normalised.emit) {
2396
2399
  events.push(LLMEvent.textDelta({ id: "text-0", text: normalised.emit }));
2397
2400
  }
@@ -2415,7 +2418,8 @@ var step = (state, event) => effect$1.Effect.gen(function* () {
2415
2418
  toolCallEvents: finished?.events ?? state.toolCallEvents,
2416
2419
  usage,
2417
2420
  finishReason,
2418
- textAcc
2421
+ textAcc,
2422
+ textMode
2419
2423
  },
2420
2424
  events
2421
2425
  ];
@@ -2436,7 +2440,8 @@ var protocol = protocol_exports.make({
2436
2440
  initial: () => ({
2437
2441
  tools: tool_stream_exports.empty(),
2438
2442
  toolCallEvents: [],
2439
- textAcc: ""
2443
+ textAcc: "",
2444
+ textMode: "unknown"
2440
2445
  }),
2441
2446
  step,
2442
2447
  onHalt: finishEvents