@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.
@@ -40,6 +40,7 @@ __export(openai_chat_exports, {
40
40
  bodyFields: () => bodyFields,
41
41
  httpTransport: () => httpTransport,
42
42
  model: () => model2,
43
+ normaliseTextDelta: () => normaliseTextDelta,
43
44
  protocol: () => protocol,
44
45
  route: () => route
45
46
  });
@@ -2348,16 +2349,17 @@ var mapUsage = (usage) => {
2348
2349
  providerMetadata: { openai: usage }
2349
2350
  });
2350
2351
  };
2351
- var normaliseTextDelta = (prev, incoming) => {
2352
- if (!incoming) return { emit: "", textAcc: prev };
2353
- if (incoming === prev) return { emit: "", textAcc: prev };
2354
- if (prev.length > 0 && incoming.startsWith(prev)) {
2355
- return { emit: incoming.slice(prev.length), textAcc: incoming };
2352
+ var normaliseTextDelta = (prev, incoming, mode = "unknown") => {
2353
+ if (!incoming) return { emit: "", textAcc: prev, mode };
2354
+ if (mode === "unknown") {
2355
+ if (prev.length === 0) return { emit: incoming, textAcc: incoming, mode };
2356
+ mode = incoming.length > prev.length && incoming.startsWith(prev) ? "cumulative" : "incremental";
2356
2357
  }
2357
- if (prev.length > 0 && prev.startsWith(incoming)) {
2358
- return { emit: "", textAcc: prev };
2358
+ if (mode === "cumulative") {
2359
+ if (incoming.startsWith(prev)) return { emit: incoming.slice(prev.length), textAcc: incoming, mode };
2360
+ if (prev.startsWith(incoming)) return { emit: "", textAcc: prev, mode };
2359
2361
  }
2360
- return { emit: incoming, textAcc: prev + incoming };
2362
+ return { emit: incoming, textAcc: prev + incoming, mode };
2361
2363
  };
2362
2364
  var step = (state, event) => effect$1.Effect.gen(function* () {
2363
2365
  const events = [];
@@ -2368,13 +2370,15 @@ var step = (state, event) => effect$1.Effect.gen(function* () {
2368
2370
  const toolDeltas = delta?.tool_calls ?? [];
2369
2371
  let tools = state.tools;
2370
2372
  let textAcc = state.textAcc;
2373
+ let textMode = state.textMode;
2371
2374
  const reasoningChunk = delta?.reasoning_content ?? delta?.reasoning;
2372
2375
  if (reasoningChunk) {
2373
2376
  events.push(LLMEvent.reasoningDelta({ id: "reasoning-0", text: reasoningChunk }));
2374
2377
  }
2375
2378
  if (delta?.content) {
2376
- const normalised = normaliseTextDelta(textAcc, delta.content);
2379
+ const normalised = normaliseTextDelta(textAcc, delta.content, textMode);
2377
2380
  textAcc = normalised.textAcc;
2381
+ textMode = normalised.mode;
2378
2382
  if (normalised.emit) {
2379
2383
  events.push(LLMEvent.textDelta({ id: "text-0", text: normalised.emit }));
2380
2384
  }
@@ -2398,7 +2402,8 @@ var step = (state, event) => effect$1.Effect.gen(function* () {
2398
2402
  toolCallEvents: finished?.events ?? state.toolCallEvents,
2399
2403
  usage,
2400
2404
  finishReason,
2401
- textAcc
2405
+ textAcc,
2406
+ textMode
2402
2407
  },
2403
2408
  events
2404
2409
  ];
@@ -2419,7 +2424,8 @@ var protocol = protocol_exports.make({
2419
2424
  initial: () => ({
2420
2425
  tools: tool_stream_exports.empty(),
2421
2426
  toolCallEvents: [],
2422
- textAcc: ""
2427
+ textAcc: "",
2428
+ textMode: "unknown"
2423
2429
  }),
2424
2430
  step,
2425
2431
  onHalt: finishEvents
@@ -2449,6 +2455,7 @@ exports.PATH = PATH;
2449
2455
  exports.bodyFields = bodyFields;
2450
2456
  exports.httpTransport = httpTransport;
2451
2457
  exports.model = model2;
2458
+ exports.normaliseTextDelta = normaliseTextDelta;
2452
2459
  exports.protocol = protocol;
2453
2460
  exports.route = route;
2454
2461
  //# sourceMappingURL=openai-chat.cjs.map