@agentica/core 0.41.0 → 0.41.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/lib/index.mjs CHANGED
@@ -1143,8 +1143,8 @@ function transformCompletionChunk(source) {
1143
1143
  }
1144
1144
 
1145
1145
  function accumulate(origin, chunk) {
1146
- const choices = origin.choices;
1147
- chunk.choices.forEach((choice => {
1146
+ const choices = origin.choices ?? [];
1147
+ (chunk.choices ?? []).forEach((choice => {
1148
1148
  const accChoice = choices[choice.index];
1149
1149
  if (accChoice != null) {
1150
1150
  choices[choice.index] = mergeChoice(accChoice, choice);
@@ -1206,7 +1206,7 @@ function merge(chunks) {
1206
1206
  service_tier: firstChunk.service_tier,
1207
1207
  system_fingerprint: firstChunk.system_fingerprint
1208
1208
  });
1209
- result.choices.forEach((choice => {
1209
+ result.choices?.forEach((choice => {
1210
1210
  choice.message.tool_calls?.filter((tc => tc.type === "function")).forEach((toolCall => {
1211
1211
  if (toolCall.function.arguments === "") {
1212
1212
  toolCall.function.arguments = "{}";
@@ -1531,10 +1531,10 @@ async function reduceStreamingWithDispatch(stream, eventProcessor, abortSignal)
1531
1531
  }
1532
1532
  };
1533
1533
  if (acc.object === "chat.completion.chunk") {
1534
- registerContext([ acc, chunk ].flatMap((v => v.choices)));
1534
+ registerContext([ acc, chunk ].flatMap((v => v.choices ?? [])));
1535
1535
  return ChatGptCompletionMessageUtil.merge([ acc, chunk ]);
1536
1536
  }
1537
- registerContext(chunk.choices);
1537
+ registerContext(chunk.choices ?? []);
1538
1538
  return ChatGptCompletionMessageUtil.accumulate(acc, chunk);
1539
1539
  }), {
1540
1540
  abortSignal
@@ -1632,9 +1632,9 @@ async function call(ctx, operations) {
1632
1632
  const event = createAssistantMessageEvent(props);
1633
1633
  void ctx.dispatch(event).catch((() => {}));
1634
1634
  }));
1635
- const allAssistantMessagesEmpty = completion.choices.every((v => v.message.tool_calls == null && v.message.content === ""));
1635
+ const allAssistantMessagesEmpty = (completion.choices ?? []).every((v => v.message.tool_calls == null && v.message.content === ""));
1636
1636
  if (allAssistantMessagesEmpty) {
1637
- const firstChoice = completion.choices.at(0);
1637
+ const firstChoice = completion.choices?.[0];
1638
1638
  if (firstChoice?.message?.reasoning != null) {
1639
1639
  throw new AssistantMessageEmptyWithReasoningError(firstChoice?.message?.reasoning ?? "");
1640
1640
  }
@@ -1654,7 +1654,7 @@ async function call(ctx, operations) {
1654
1654
  }
1655
1655
  const executes = [];
1656
1656
  const retry = ctx.config?.retry ?? AgenticaConstant.RETRY;
1657
- for (const choice of completion.choices) {
1657
+ for (const choice of completion.choices ?? []) {
1658
1658
  for (const tc of choice.message.tool_calls ?? []) {
1659
1659
  if (tc.type === "function") {
1660
1660
  const operation = operations.find((s => s.name === tc.function.name));
@@ -1817,7 +1817,7 @@ async function correctError(ctx, props) {
1817
1817
  }
1818
1818
  return ChatGptCompletionMessageUtil.merge(await StreamUtil.readAll(result.value));
1819
1819
  })();
1820
- const toolCall = completion.choices[0]?.message.tool_calls?.filter((tc => tc.type === "function")).find((s => s.function.name === props.operation.name));
1820
+ const toolCall = completion.choices?.[0]?.message.tool_calls?.filter((tc => tc.type === "function")).find((s => s.function.name === props.operation.name));
1821
1821
  return toolCall === undefined ? props.giveUp() : predicate(ctx, props.operation, toolCall, props.previousValidationErrors, props.life);
1822
1822
  }
1823
1823
 
@@ -2070,7 +2070,7 @@ async function step$1(ctx, operations, retry, failures) {
2070
2070
  })();
2071
2071
  if (retry++ < (ctx.config?.retry ?? AgenticaConstant.RETRY)) {
2072
2072
  const failures = [];
2073
- for (const choice of completion.choices) {
2073
+ for (const choice of completion.choices ?? []) {
2074
2074
  for (const tc of choice.message.tool_calls ?? []) {
2075
2075
  if (tc.type !== "function" || tc.function.name !== "cancelFunctions") {
2076
2076
  continue;
@@ -2150,7 +2150,7 @@ async function step$1(ctx, operations, retry, failures) {
2150
2150
  return step$1(ctx, operations, retry, failures);
2151
2151
  }
2152
2152
  }
2153
- for (const choice of completion.choices) {
2153
+ for (const choice of completion.choices ?? []) {
2154
2154
  if (choice.message.tool_calls != null) {
2155
2155
  for (const tc of choice.message.tool_calls) {
2156
2156
  if (tc.type !== "function") {
@@ -2213,7 +2213,7 @@ async function describe(ctx, histories) {
2213
2213
  } ]
2214
2214
  });
2215
2215
  if (result.type === "none-stream") {
2216
- const message = result.value.choices[0]?.message.content ?? "";
2216
+ const message = result.value.choices?.[0]?.message.content ?? "";
2217
2217
  const event = createDescribeEvent({
2218
2218
  executes: histories,
2219
2219
  stream: toAsyncGenerator(message),
@@ -3098,7 +3098,7 @@ async function initialize(ctx) {
3098
3098
  tool_choice: "auto"
3099
3099
  });
3100
3100
  if (result.type === "none-stream") {
3101
- const message = result.value.choices[0]?.message.content ?? "";
3101
+ const message = result.value.choices?.[0]?.message.content ?? "";
3102
3102
  const event = createAssistantMessageEvent({
3103
3103
  stream: toAsyncGenerator(message),
3104
3104
  done: () => true,
@@ -3115,7 +3115,7 @@ async function initialize(ctx) {
3115
3115
  if (completion === null) {
3116
3116
  throw new Error("No completion received");
3117
3117
  }
3118
- if (completion.choices.some((c => c.message.tool_calls != null && c.message.tool_calls.some((tc => tc.type === "function" && tc.function.name === FUNCTION.name))))) {
3118
+ if (completion.choices?.some((c => c.message.tool_calls != null && c.message.tool_calls.some((tc => tc.type === "function" && tc.function.name === FUNCTION.name))))) {
3119
3119
  await ctx.initialize();
3120
3120
  }
3121
3121
  }
@@ -3315,9 +3315,9 @@ async function step(ctx, operations, retry, failures) {
3315
3315
  });
3316
3316
  if (result.type === "none-stream") {
3317
3317
  const completion = result.value;
3318
- const allAssistantMessagesEmpty = completion.choices.every((v => v.message.tool_calls == null && v.message.content === ""));
3318
+ const allAssistantMessagesEmpty = !!completion.choices?.every((v => v.message.tool_calls == null && v.message.content === ""));
3319
3319
  if (allAssistantMessagesEmpty) {
3320
- const firstChoice = completion.choices.at(0);
3320
+ const firstChoice = completion.choices?.at(0);
3321
3321
  if (firstChoice?.message?.reasoning != null) {
3322
3322
  throw new AssistantMessageEmptyWithReasoningError(firstChoice?.message?.reasoning ?? "");
3323
3323
  }
@@ -3329,9 +3329,9 @@ async function step(ctx, operations, retry, failures) {
3329
3329
  const event = createAssistantMessageEvent(props);
3330
3330
  void ctx.dispatch(event).catch((() => {}));
3331
3331
  }), ctx.abortSignal);
3332
- const allAssistantMessagesEmpty = completion.choices.every((v => v.message.tool_calls == null && v.message.content === ""));
3332
+ const allAssistantMessagesEmpty = !!completion.choices?.every((v => v.message.tool_calls == null && v.message.content === ""));
3333
3333
  if (allAssistantMessagesEmpty) {
3334
- const firstChoice = completion.choices.at(0);
3334
+ const firstChoice = completion.choices?.at(0);
3335
3335
  if (firstChoice?.message?.reasoning != null) {
3336
3336
  throw new AssistantMessageEmptyWithReasoningError(firstChoice?.message?.reasoning ?? "");
3337
3337
  }
@@ -3351,7 +3351,7 @@ async function step(ctx, operations, retry, failures) {
3351
3351
  }
3352
3352
  if (retry++ < (ctx.config?.retry ?? AgenticaConstant.RETRY)) {
3353
3353
  const failures = [];
3354
- for (const choice of completion.choices) {
3354
+ for (const choice of completion.choices ?? []) {
3355
3355
  for (const tc of choice.message.tool_calls ?? []) {
3356
3356
  if (tc.type !== "function" || tc.function.name !== "selectFunctions") {
3357
3357
  continue;
@@ -3431,7 +3431,7 @@ async function step(ctx, operations, retry, failures) {
3431
3431
  return step(ctx, operations, retry, failures);
3432
3432
  }
3433
3433
  }
3434
- for (const choice of completion.choices) {
3434
+ for (const choice of completion.choices ?? []) {
3435
3435
  if (choice.message.tool_calls != null) {
3436
3436
  for (const tc of choice.message.tool_calls) {
3437
3437
  if (tc.type !== "function") {