@agentica/core 0.44.1 → 0.45.0-dev.20260426

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.
Files changed (68) hide show
  1. package/lib/events/AgenticaCallEvent.d.ts +2 -1
  2. package/lib/events/AgenticaCancelEvent.d.ts +2 -1
  3. package/lib/events/AgenticaExecuteEvent.d.ts +2 -1
  4. package/lib/events/AgenticaSelectEvent.d.ts +2 -1
  5. package/lib/factory/events.d.ts +5 -0
  6. package/lib/factory/events.js +9 -0
  7. package/lib/factory/events.js.map +1 -1
  8. package/lib/factory/histories.js +11 -4
  9. package/lib/factory/histories.js.map +1 -1
  10. package/lib/factory/histories.spec.d.ts +1 -0
  11. package/lib/factory/histories.spec.js +34 -0
  12. package/lib/factory/histories.spec.js.map +1 -0
  13. package/lib/histories/AgenticaCancelHistory.d.ts +2 -1
  14. package/lib/histories/AgenticaExecuteHistory.d.ts +2 -1
  15. package/lib/histories/AgenticaSelectHistory.d.ts +2 -1
  16. package/lib/histories/contents/AgenticaCallReasoningPayload.d.ts +26 -0
  17. package/lib/histories/contents/AgenticaCallReasoningPayload.js +3 -0
  18. package/lib/histories/contents/AgenticaCallReasoningPayload.js.map +1 -0
  19. package/lib/histories/contents/index.d.ts +1 -0
  20. package/lib/histories/contents/index.js +1 -0
  21. package/lib/histories/contents/index.js.map +1 -1
  22. package/lib/index.mjs +136 -49
  23. package/lib/index.mjs.map +1 -1
  24. package/lib/json/IAgenticaEventJson.d.ts +5 -4
  25. package/lib/json/IAgenticaHistoryJson.d.ts +4 -3
  26. package/lib/orchestrate/call.js +27 -11
  27. package/lib/orchestrate/call.js.map +1 -1
  28. package/lib/orchestrate/cancel.js +3 -1
  29. package/lib/orchestrate/cancel.js.map +1 -1
  30. package/lib/orchestrate/internal/cancelFunctionFromContext.js +2 -1
  31. package/lib/orchestrate/internal/cancelFunctionFromContext.js.map +1 -1
  32. package/lib/orchestrate/internal/selectFunctionFromContext.js +2 -1
  33. package/lib/orchestrate/internal/selectFunctionFromContext.js.map +1 -1
  34. package/lib/orchestrate/select.js +3 -1
  35. package/lib/orchestrate/select.js.map +1 -1
  36. package/lib/transformers/transformHistory.js +3 -0
  37. package/lib/transformers/transformHistory.js.map +1 -1
  38. package/lib/utils/ChatGptAssistantMessageUtil.d.ts +10 -0
  39. package/lib/utils/ChatGptAssistantMessageUtil.js +36 -0
  40. package/lib/utils/ChatGptAssistantMessageUtil.js.map +1 -0
  41. package/lib/utils/ChatGptCompletionMessageUtil.js +33 -15
  42. package/lib/utils/ChatGptCompletionMessageUtil.js.map +1 -1
  43. package/lib/utils/ChatGptCompletionMessageUtil.spec.js +27 -0
  44. package/lib/utils/ChatGptCompletionMessageUtil.spec.js.map +1 -1
  45. package/package.json +1 -1
  46. package/src/events/AgenticaCallEvent.ts +2 -1
  47. package/src/events/AgenticaCancelEvent.ts +2 -1
  48. package/src/events/AgenticaExecuteEvent.ts +2 -1
  49. package/src/events/AgenticaSelectEvent.ts +2 -1
  50. package/src/factory/events.ts +14 -0
  51. package/src/factory/histories.spec.ts +34 -0
  52. package/src/factory/histories.ts +16 -4
  53. package/src/histories/AgenticaCancelHistory.ts +2 -1
  54. package/src/histories/AgenticaExecuteHistory.ts +2 -1
  55. package/src/histories/AgenticaSelectHistory.ts +2 -1
  56. package/src/histories/contents/AgenticaCallReasoningPayload.ts +26 -0
  57. package/src/histories/contents/index.ts +1 -0
  58. package/src/json/IAgenticaEventJson.ts +5 -4
  59. package/src/json/IAgenticaHistoryJson.ts +4 -3
  60. package/src/orchestrate/call.ts +28 -4
  61. package/src/orchestrate/cancel.ts +7 -1
  62. package/src/orchestrate/internal/cancelFunctionFromContext.ts +3 -0
  63. package/src/orchestrate/internal/selectFunctionFromContext.ts +3 -0
  64. package/src/orchestrate/select.ts +7 -1
  65. package/src/transformers/transformHistory.ts +3 -0
  66. package/src/utils/ChatGptAssistantMessageUtil.ts +47 -0
  67. package/src/utils/ChatGptCompletionMessageUtil.spec.ts +30 -0
  68. package/src/utils/ChatGptCompletionMessageUtil.ts +31 -15
package/lib/index.mjs CHANGED
@@ -287,11 +287,49 @@ const AgenticaOperationComposer = {
287
287
  compose
288
288
  };
289
289
 
290
+ function isRecord(value) {
291
+ return value != null && typeof value === "object" && Array.isArray(value) === false;
292
+ }
293
+
294
+ function collect(source) {
295
+ if (isRecord(source) === false) {
296
+ return undefined;
297
+ }
298
+ const output = {};
299
+ for (const [key, value] of Object.entries(source)) {
300
+ if (key.startsWith("reasoning") && value !== undefined) {
301
+ output[key] = value;
302
+ }
303
+ }
304
+ return Object.keys(output).length === 0 ? undefined : output;
305
+ }
306
+
307
+ function assign(message, payload) {
308
+ const collected = collect(payload);
309
+ if (collected === undefined) {
310
+ return message;
311
+ }
312
+ return {
313
+ ...message,
314
+ ...collected
315
+ };
316
+ }
317
+
318
+ function assignFrom(message, source) {
319
+ return assign(message, collect(source));
320
+ }
321
+
322
+ const ChatGptAssistantMessageUtil = {
323
+ assign,
324
+ assignFrom,
325
+ collect
326
+ };
327
+
290
328
  function decodeHistory(history) {
291
329
  if (history.type === "describe") {
292
330
  return [];
293
331
  } else if (history.type === "select" || history.type === "cancel") {
294
- return [ {
332
+ return [ ChatGptAssistantMessageUtil.assign({
295
333
  role: "assistant",
296
334
  tool_calls: [ {
297
335
  type: "function",
@@ -306,13 +344,13 @@ function decodeHistory(history) {
306
344
  })
307
345
  }
308
346
  } ]
309
- }, {
347
+ }, history.assistant), {
310
348
  role: "tool",
311
349
  tool_call_id: history.id,
312
350
  content: ""
313
351
  } ];
314
352
  } else if (history.type === "execute") {
315
- return [ {
353
+ return [ ChatGptAssistantMessageUtil.assign({
316
354
  role: "assistant",
317
355
  tool_calls: [ {
318
356
  type: "function",
@@ -322,7 +360,7 @@ function decodeHistory(history) {
322
360
  arguments: JSON.stringify(history.arguments)
323
361
  }
324
362
  } ]
325
- }, {
363
+ }, history.assistant), {
326
364
  role: "tool",
327
365
  tool_call_id: history.id,
328
366
  content: JSON.stringify({
@@ -467,11 +505,13 @@ function createSelectHistory(props) {
467
505
  id: props.id,
468
506
  selection: props.selection,
469
507
  created_at: props.created_at,
508
+ assistant: props.assistant,
470
509
  toJSON: () => ({
471
510
  type: "select",
472
511
  id: props.id,
473
512
  created_at: props.created_at,
474
- selection: props.selection.toJSON()
513
+ selection: props.selection.toJSON(),
514
+ assistant: props.assistant
475
515
  })
476
516
  };
477
517
  }
@@ -482,11 +522,13 @@ function createCancelHistory(props) {
482
522
  id: props.id,
483
523
  created_at: props.created_at,
484
524
  selection: props.selection,
525
+ assistant: props.assistant,
485
526
  toJSON: () => ({
486
527
  type: "cancel",
487
528
  id: props.id,
488
529
  created_at: props.created_at,
489
- selection: props.selection.toJSON()
530
+ selection: props.selection.toJSON(),
531
+ assistant: props.assistant
490
532
  })
491
533
  };
492
534
  }
@@ -501,6 +543,7 @@ function createExecuteHistory(props) {
501
543
  arguments: props.arguments,
502
544
  value: props.value,
503
545
  success: props.success,
546
+ assistant: props.assistant,
504
547
  toJSON: () => ({
505
548
  type: "execute",
506
549
  id: props.id,
@@ -509,7 +552,8 @@ function createExecuteHistory(props) {
509
552
  operation: props.operation.toJSON(),
510
553
  arguments: props.arguments,
511
554
  value: props.value,
512
- success: props.success
555
+ success: props.success,
556
+ assistant: props.assistant
513
557
  })
514
558
  };
515
559
  }
@@ -536,16 +580,19 @@ function createSelectEvent(props) {
536
580
  id,
537
581
  created_at,
538
582
  selection: props.selection,
583
+ assistant: props.assistant,
539
584
  toJSON: () => ({
540
585
  type: "select",
541
586
  id,
542
587
  created_at,
543
- selection: props.selection.toJSON()
588
+ selection: props.selection.toJSON(),
589
+ assistant: props.assistant
544
590
  }),
545
591
  toHistory: () => createSelectHistory({
546
592
  id,
547
593
  created_at,
548
- selection: props.selection
594
+ selection: props.selection,
595
+ assistant: props.assistant
549
596
  })
550
597
  };
551
598
  }
@@ -558,11 +605,13 @@ function createCancelEvent(props) {
558
605
  id,
559
606
  created_at,
560
607
  selection: props.selection,
608
+ assistant: props.assistant,
561
609
  toJSON: () => ({
562
610
  type: "cancel",
563
611
  id,
564
612
  created_at,
565
- selection: props.selection.toJSON()
613
+ selection: props.selection.toJSON(),
614
+ assistant: props.assistant
566
615
  })
567
616
  };
568
617
  }
@@ -575,12 +624,14 @@ function createCallEvent(props) {
575
624
  created_at,
576
625
  operation: props.operation,
577
626
  arguments: props.arguments,
627
+ assistant: props.assistant,
578
628
  toJSON: () => ({
579
629
  type: "call",
580
630
  id: props.id,
581
631
  created_at,
582
632
  operation: props.operation.toJSON(),
583
- arguments: props.arguments
633
+ arguments: props.arguments,
634
+ assistant: props.assistant
584
635
  })
585
636
  };
586
637
  }
@@ -635,6 +686,7 @@ function createExecuteEvent(props) {
635
686
  arguments: props.arguments,
636
687
  value: props.value,
637
688
  success: props.success,
689
+ assistant: props.assistant,
638
690
  toJSON: () => ({
639
691
  type: "execute",
640
692
  id,
@@ -644,7 +696,8 @@ function createExecuteEvent(props) {
644
696
  operation: props.operation.toJSON(),
645
697
  arguments: props.arguments,
646
698
  value: props.value,
647
- success: props.success
699
+ success: props.success,
700
+ assistant: props.assistant
648
701
  }),
649
702
  toHistory: () => createExecuteHistory({
650
703
  id,
@@ -996,7 +1049,7 @@ function accumulate(origin, chunk) {
996
1049
  index: choice.index,
997
1050
  finish_reason: choice.finish_reason ?? null,
998
1051
  logprobs: choice.logprobs ?? null,
999
- message: {
1052
+ message: ChatGptAssistantMessageUtil.assignFrom({
1000
1053
  tool_calls: choice.delta.tool_calls !== undefined ? choice.delta.tool_calls.reduce((acc, cur) => {
1001
1054
  acc[cur.index] = {
1002
1055
  id: cur.id ?? "",
@@ -1010,11 +1063,8 @@ function accumulate(origin, chunk) {
1010
1063
  }, []) : undefined,
1011
1064
  content: choice.delta.content ?? null,
1012
1065
  refusal: choice.delta.refusal ?? null,
1013
- role: "assistant",
1014
- ...{
1015
- reasoning: choice.delta.reasoning ?? null
1016
- }
1017
- }
1066
+ role: "assistant"
1067
+ }, choice.delta)
1018
1068
  };
1019
1069
  });
1020
1070
  const usage = (() => {
@@ -1080,13 +1130,7 @@ function mergeChoice(acc, cur) {
1080
1130
  acc.message.refusal += cur.delta.refusal;
1081
1131
  }
1082
1132
  }
1083
- if (cur.delta.reasoning != null) {
1084
- if (acc.message.reasoning == null) {
1085
- acc.message.reasoning = cur.delta.reasoning;
1086
- } else {
1087
- acc.message.reasoning += cur.delta.reasoning;
1088
- }
1089
- }
1133
+ mergeAssistantMessagePayload(acc.message, cur.delta);
1090
1134
  if (cur.delta.tool_calls != null) {
1091
1135
  (_a = acc.message).tool_calls ?? (_a.tool_calls = []);
1092
1136
  const toolCalls = acc.message.tool_calls;
@@ -1118,6 +1162,23 @@ function mergeToolCalls(acc, cur) {
1118
1162
  return acc;
1119
1163
  }
1120
1164
 
1165
+ function mergeAssistantMessagePayload(message, delta) {
1166
+ const target = message;
1167
+ const source = delta;
1168
+ for (const [key, value] of Object.entries(source)) {
1169
+ if (key.startsWith("reasoning") === false || value === undefined) {
1170
+ continue;
1171
+ }
1172
+ if (typeof value === "string") {
1173
+ target[key] = typeof target[key] === "string" ? `${target[key]}${value}` : value;
1174
+ } else if (Array.isArray(value)) {
1175
+ target[key] = [ ...Array.isArray(target[key]) ? target[key] : [], ...value ];
1176
+ } else {
1177
+ target[key] = value;
1178
+ }
1179
+ }
1180
+ }
1181
+
1121
1182
  const ChatGptCompletionMessageUtil = {
1122
1183
  transformCompletionChunk,
1123
1184
  accumulate,
@@ -1412,7 +1473,7 @@ function createOperationSelection(props) {
1412
1473
  };
1413
1474
  }
1414
1475
 
1415
- function cancelFunctionFromContext(ctx, reference) {
1476
+ function cancelFunctionFromContext(ctx, reference, reasoning) {
1416
1477
  const index = ctx.stack.findIndex(item => item.operation.name === reference.name);
1417
1478
  if (index === -1) {
1418
1479
  return;
@@ -1423,7 +1484,8 @@ function cancelFunctionFromContext(ctx, reference) {
1423
1484
  selection: createOperationSelection({
1424
1485
  operation: item.operation,
1425
1486
  reason: reference.reason
1426
- })
1487
+ }),
1488
+ assistant: reasoning?.assistant
1427
1489
  });
1428
1490
  void ctx.dispatch(event).catch(() => {});
1429
1491
  }
@@ -1491,13 +1553,17 @@ async function call(ctx, operations) {
1491
1553
  const executes = [];
1492
1554
  const retry = ctx.config?.retry ?? AgenticaConstant.RETRY;
1493
1555
  for (const choice of completion.choices ?? []) {
1556
+ const assistant = ChatGptAssistantMessageUtil.collect(choice.message);
1557
+ const reasoning = assistant === undefined ? undefined : {
1558
+ assistant
1559
+ };
1494
1560
  for (const tc of choice.message.tool_calls ?? []) {
1495
1561
  if (tc.type === "function") {
1496
1562
  const operation = operations.find(s => s.name === tc.function.name);
1497
1563
  if (operation === undefined) {
1498
1564
  continue;
1499
1565
  }
1500
- const event = await predicate(ctx, operation, tc, [], retry);
1566
+ const event = await predicate(ctx, operation, tc, reasoning, [], retry);
1501
1567
  await ctx.dispatch(event);
1502
1568
  executes.push(event);
1503
1569
  if (isAgenticaContext(ctx)) {
@@ -1512,11 +1578,11 @@ async function call(ctx, operations) {
1512
1578
  return executes;
1513
1579
  }
1514
1580
 
1515
- async function predicate(ctx, operation, toolCall, previousValidationErrors, life) {
1516
- const call = parseArguments(operation, toolCall, life);
1581
+ async function predicate(ctx, operation, toolCall, reasoning, previousValidationErrors, life) {
1582
+ const call = parseArguments(operation, toolCall, reasoning, life);
1517
1583
  await ctx.dispatch(call);
1518
1584
  if (call.type === "jsonParseError") {
1519
- return correctJsonError(ctx, toolCall, call, previousValidationErrors, life - 1);
1585
+ return correctJsonError(ctx, toolCall, reasoning, call, previousValidationErrors, life - 1);
1520
1586
  }
1521
1587
  const check = operation.function.validate(call.arguments);
1522
1588
  if (check.success === false) {
@@ -1542,12 +1608,14 @@ async function correctTypeError(ctx, callEvent, validateEvent, previousValidatio
1542
1608
  arguments: callEvent.arguments,
1543
1609
  errors: validateEvent.result.errors
1544
1610
  }),
1545
- success: false
1611
+ success: false,
1612
+ assistant: callEvent.assistant
1546
1613
  }),
1547
1614
  operation: callEvent.operation,
1548
1615
  toolCall: {
1549
1616
  id: callEvent.id,
1550
1617
  arguments: JSON.stringify(callEvent.arguments),
1618
+ assistant: callEvent.assistant,
1551
1619
  result: [ "🚨 VALIDATION FAILURE: Your function arguments do not conform to the required schema.", "", "The validation errors below represent computed absolute truth from rigorous type validation.", "Each error is marked with ❌ comments showing the exact location, expected type, and actual value.", "", "You must fix ALL errors to achieve 100% schema compliance.", "", LlmJson.stringify(validateEvent.result) ].join("\n")
1552
1620
  },
1553
1621
  systemPrompt: ctx.config?.systemPrompt?.validate?.(previousValidationErrors.slice(0, -1)) ?? [ AgenticaSystemPrompt.VALIDATE, ...previousValidationErrors.length > 1 ? [ "", AgenticaSystemPrompt.VALIDATE_REPEATED.replace("${{HISTORICAL_ERRORS}}", previousValidationErrors.slice(0, -1).map((ve, i) => [ `### ${i + 1}. Previous Validation Error`, "", LlmJson.stringify(ve.result) ].join("\n")).join("\n\n")) ] : [] ].join("\n"),
@@ -1556,19 +1624,21 @@ async function correctTypeError(ctx, callEvent, validateEvent, previousValidatio
1556
1624
  });
1557
1625
  }
1558
1626
 
1559
- async function correctJsonError(ctx, toolCall, parseErrorEvent, previousValidationErrors, life) {
1627
+ async function correctJsonError(ctx, toolCall, reasoning, parseErrorEvent, previousValidationErrors, life) {
1560
1628
  return correctError(ctx, {
1561
1629
  giveUp: () => createExecuteEvent({
1562
1630
  call_id: toolCall.id,
1563
1631
  operation: parseErrorEvent.operation,
1564
1632
  arguments: {},
1565
1633
  value: new AgenticaJsonParseError(parseErrorEvent.failure),
1566
- success: false
1634
+ success: false,
1635
+ assistant: reasoning?.assistant
1567
1636
  }),
1568
1637
  operation: parseErrorEvent.operation,
1569
1638
  toolCall: {
1570
1639
  id: parseErrorEvent.id,
1571
1640
  arguments: parseErrorEvent.failure.input,
1641
+ assistant: reasoning?.assistant,
1572
1642
  result: dedent`
1573
1643
  Invalid JSON format.
1574
1644
 
@@ -1593,7 +1663,7 @@ async function correctJsonError(ctx, toolCall, parseErrorEvent, previousValidati
1593
1663
  });
1594
1664
  }
1595
1665
 
1596
- function parseArguments(operation, toolCall, life) {
1666
+ function parseArguments(operation, toolCall, reasoning, life) {
1597
1667
  const result = operation.function.parse(toolCall.function.arguments);
1598
1668
  if (result.success === false) {
1599
1669
  return createJsonParseErrorEvent({
@@ -1606,7 +1676,8 @@ function parseArguments(operation, toolCall, life) {
1606
1676
  return createCallEvent({
1607
1677
  id: toolCall.id,
1608
1678
  operation,
1609
- arguments: result.data
1679
+ arguments: result.data,
1680
+ assistant: reasoning?.assistant
1610
1681
  });
1611
1682
  }
1612
1683
 
@@ -1621,7 +1692,7 @@ async function correctError(ctx, props) {
1621
1692
  }, {
1622
1693
  role: "system",
1623
1694
  content: ctx.config?.systemPrompt?.execute?.(ctx.histories) ?? AgenticaSystemPrompt.EXECUTE
1624
- }, {
1695
+ }, ChatGptAssistantMessageUtil.assign({
1625
1696
  role: "assistant",
1626
1697
  tool_calls: [ {
1627
1698
  type: "function",
@@ -1631,7 +1702,7 @@ async function correctError(ctx, props) {
1631
1702
  arguments: props.toolCall.arguments
1632
1703
  }
1633
1704
  } ]
1634
- }, {
1705
+ }, props.toolCall.assistant), {
1635
1706
  role: "tool",
1636
1707
  content: props.toolCall.result,
1637
1708
  tool_call_id: props.toolCall.id
@@ -1658,14 +1729,18 @@ async function correctError(ctx, props) {
1658
1729
  }
1659
1730
  return ChatGptCompletionMessageUtil.merge(await StreamUtil.readAll(result.value));
1660
1731
  })();
1661
- const toolCall = completion.choices?.[0]?.message.tool_calls?.filter(tc => tc.type === "function").find(s => s.function.name === props.operation.name);
1732
+ const choice = completion.choices?.[0];
1733
+ const toolCall = choice?.message.tool_calls?.filter(tc => tc.type === "function").find(s => s.function.name === props.operation.name);
1662
1734
  if (toolCall === undefined) {
1663
1735
  return correctError(ctx, {
1664
1736
  ...props,
1665
1737
  life: props.life - 1
1666
1738
  });
1667
1739
  }
1668
- return predicate(ctx, props.operation, toolCall, props.previousValidationErrors, props.life);
1740
+ const assistant = choice === undefined ? undefined : ChatGptAssistantMessageUtil.collect(choice.message);
1741
+ return predicate(ctx, props.operation, toolCall, assistant === undefined ? undefined : {
1742
+ assistant
1743
+ }, props.previousValidationErrors, props.life);
1669
1744
  }
1670
1745
 
1671
1746
  async function executeFunction(call, operation) {
@@ -1691,7 +1766,8 @@ async function executeFunction(call, operation) {
1691
1766
  operation: call.operation,
1692
1767
  arguments: call.arguments,
1693
1768
  value,
1694
- success: true
1769
+ success: true,
1770
+ assistant: call.assistant
1695
1771
  });
1696
1772
  } catch (error) {
1697
1773
  return createExecuteEvent({
@@ -1704,7 +1780,8 @@ async function executeFunction(call, operation) {
1704
1780
  message: error.message,
1705
1781
  stack: error.stack
1706
1782
  } : error,
1707
- success: false
1783
+ success: false,
1784
+ assistant: call.assistant
1708
1785
  });
1709
1786
  }
1710
1787
  }
@@ -1937,6 +2014,7 @@ async function step$1(ctx, operations, retry, failures) {
1937
2014
  }
1938
2015
  }
1939
2016
  for (const choice of completion.choices ?? []) {
2017
+ const assistant = ChatGptAssistantMessageUtil.collect(choice.message);
1940
2018
  if (choice.message.tool_calls != null) {
1941
2019
  for (const tc of choice.message.tool_calls) {
1942
2020
  if (tc.type !== "function") {
@@ -1957,7 +2035,9 @@ async function step$1(ctx, operations, retry, failures) {
1957
2035
  continue;
1958
2036
  }
1959
2037
  for (const reference of input.functions) {
1960
- cancelFunctionFromContext(ctx, reference);
2038
+ cancelFunctionFromContext(ctx, reference, assistant === undefined ? undefined : {
2039
+ assistant
2040
+ });
1961
2041
  }
1962
2042
  }
1963
2043
  }
@@ -2118,7 +2198,7 @@ async function initialize(ctx) {
2118
2198
  }
2119
2199
  }
2120
2200
 
2121
- function selectFunctionFromContext(ctx, reference) {
2201
+ function selectFunctionFromContext(ctx, reference, reasoning) {
2122
2202
  const operation = ctx.operations.flat.get(reference.name);
2123
2203
  if (operation === undefined) {
2124
2204
  return;
@@ -2129,7 +2209,8 @@ function selectFunctionFromContext(ctx, reference) {
2129
2209
  });
2130
2210
  ctx.stack.push(selection);
2131
2211
  const event = createSelectEvent({
2132
- selection
2212
+ selection,
2213
+ assistant: reasoning?.assistant
2133
2214
  });
2134
2215
  void ctx.dispatch(event).catch(() => {});
2135
2216
  }
@@ -2370,6 +2451,7 @@ async function step(ctx, operations, retry, failures) {
2370
2451
  }
2371
2452
  }
2372
2453
  for (const choice of completion.choices ?? []) {
2454
+ const assistant = ChatGptAssistantMessageUtil.collect(choice.message);
2373
2455
  if (choice.message.tool_calls != null) {
2374
2456
  for (const tc of choice.message.tool_calls) {
2375
2457
  if (tc.type !== "function") {
@@ -2390,7 +2472,9 @@ async function step(ctx, operations, retry, failures) {
2390
2472
  continue;
2391
2473
  }
2392
2474
  for (const reference of input.functions) {
2393
- selectFunctionFromContext(ctx, reference);
2475
+ selectFunctionFromContext(ctx, reference, assistant === undefined ? undefined : {
2476
+ assistant
2477
+ });
2394
2478
  }
2395
2479
  }
2396
2480
  }
@@ -2506,7 +2590,8 @@ function transformSelect(props) {
2506
2590
  input: props.history.selection.operation
2507
2591
  }),
2508
2592
  reason: props.history.selection.reason
2509
- })
2593
+ }),
2594
+ assistant: props.history.assistant
2510
2595
  });
2511
2596
  }
2512
2597
 
@@ -2520,7 +2605,8 @@ function transformCancel(props) {
2520
2605
  input: props.history.selection.operation
2521
2606
  }),
2522
2607
  reason: props.history.selection.reason
2523
- })
2608
+ }),
2609
+ assistant: props.history.assistant
2524
2610
  });
2525
2611
  }
2526
2612
 
@@ -2534,7 +2620,8 @@ function transformExecute(props) {
2534
2620
  }),
2535
2621
  arguments: props.history.arguments,
2536
2622
  value: props.history.value,
2537
- success: props.history.success
2623
+ success: props.history.success,
2624
+ assistant: props.history.assistant
2538
2625
  });
2539
2626
  }
2540
2627