@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.
package/dist/index.js CHANGED
@@ -2029,6 +2029,7 @@ __export(openai_chat_exports, {
2029
2029
  bodyFields: () => bodyFields,
2030
2030
  httpTransport: () => httpTransport,
2031
2031
  model: () => model2,
2032
+ normaliseTextDelta: () => normaliseTextDelta,
2032
2033
  protocol: () => protocol,
2033
2034
  route: () => route
2034
2035
  });
@@ -2426,16 +2427,17 @@ var mapUsage = (usage) => {
2426
2427
  providerMetadata: { openai: usage }
2427
2428
  });
2428
2429
  };
2429
- var normaliseTextDelta = (prev, incoming) => {
2430
- if (!incoming) return { emit: "", textAcc: prev };
2431
- if (incoming === prev) return { emit: "", textAcc: prev };
2432
- if (prev.length > 0 && incoming.startsWith(prev)) {
2433
- return { emit: incoming.slice(prev.length), textAcc: incoming };
2430
+ var normaliseTextDelta = (prev, incoming, mode = "unknown") => {
2431
+ if (!incoming) return { emit: "", textAcc: prev, mode };
2432
+ if (mode === "unknown") {
2433
+ if (prev.length === 0) return { emit: incoming, textAcc: incoming, mode };
2434
+ mode = incoming.length > prev.length && incoming.startsWith(prev) ? "cumulative" : "incremental";
2434
2435
  }
2435
- if (prev.length > 0 && prev.startsWith(incoming)) {
2436
- return { emit: "", textAcc: prev };
2436
+ if (mode === "cumulative") {
2437
+ if (incoming.startsWith(prev)) return { emit: incoming.slice(prev.length), textAcc: incoming, mode };
2438
+ if (prev.startsWith(incoming)) return { emit: "", textAcc: prev, mode };
2437
2439
  }
2438
- return { emit: incoming, textAcc: prev + incoming };
2440
+ return { emit: incoming, textAcc: prev + incoming, mode };
2439
2441
  };
2440
2442
  var step = (state, event) => Effect.gen(function* () {
2441
2443
  const events = [];
@@ -2446,13 +2448,15 @@ var step = (state, event) => Effect.gen(function* () {
2446
2448
  const toolDeltas = delta?.tool_calls ?? [];
2447
2449
  let tools = state.tools;
2448
2450
  let textAcc = state.textAcc;
2451
+ let textMode = state.textMode;
2449
2452
  const reasoningChunk = delta?.reasoning_content ?? delta?.reasoning;
2450
2453
  if (reasoningChunk) {
2451
2454
  events.push(LLMEvent.reasoningDelta({ id: "reasoning-0", text: reasoningChunk }));
2452
2455
  }
2453
2456
  if (delta?.content) {
2454
- const normalised = normaliseTextDelta(textAcc, delta.content);
2457
+ const normalised = normaliseTextDelta(textAcc, delta.content, textMode);
2455
2458
  textAcc = normalised.textAcc;
2459
+ textMode = normalised.mode;
2456
2460
  if (normalised.emit) {
2457
2461
  events.push(LLMEvent.textDelta({ id: "text-0", text: normalised.emit }));
2458
2462
  }
@@ -2476,7 +2480,8 @@ var step = (state, event) => Effect.gen(function* () {
2476
2480
  toolCallEvents: finished?.events ?? state.toolCallEvents,
2477
2481
  usage,
2478
2482
  finishReason,
2479
- textAcc
2483
+ textAcc,
2484
+ textMode
2480
2485
  },
2481
2486
  events
2482
2487
  ];
@@ -2497,7 +2502,8 @@ var protocol = protocol_exports.make({
2497
2502
  initial: () => ({
2498
2503
  tools: tool_stream_exports.empty(),
2499
2504
  toolCallEvents: [],
2500
- textAcc: ""
2505
+ textAcc: "",
2506
+ textMode: "unknown"
2501
2507
  }),
2502
2508
  step,
2503
2509
  onHalt: finishEvents