@agentica/core 0.41.0 → 0.41.2

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.
@@ -193,9 +193,9 @@ async function step(
193
193
 
194
194
  if (result.type === "none-stream") {
195
195
  const completion = result.value;
196
- const allAssistantMessagesEmpty = completion.choices.every(v => v.message.tool_calls == null && v.message.content === "");
196
+ const allAssistantMessagesEmpty = !!completion.choices?.every(v => v.message.tool_calls == null && v.message.content === "");
197
197
  if (allAssistantMessagesEmpty) {
198
- const firstChoice = completion.choices.at(0);
198
+ const firstChoice = completion.choices?.at(0);
199
199
  if ((firstChoice?.message as { reasoning?: string })?.reasoning != null) {
200
200
  throw new AssistantMessageEmptyWithReasoningError((firstChoice?.message as { reasoning?: string })?.reasoning ?? "");
201
201
  }
@@ -208,9 +208,9 @@ async function step(
208
208
  const event: AgenticaAssistantMessageEvent = createAssistantMessageEvent(props);
209
209
  void ctx.dispatch(event).catch(() => {});
210
210
  }, ctx.abortSignal);
211
- const allAssistantMessagesEmpty = completion.choices.every(v => v.message.tool_calls == null && v.message.content === "");
211
+ const allAssistantMessagesEmpty = !!completion.choices?.every(v => v.message.tool_calls == null && v.message.content === "");
212
212
  if (allAssistantMessagesEmpty) {
213
- const firstChoice = completion.choices.at(0);
213
+ const firstChoice = completion.choices?.at(0);
214
214
  if ((firstChoice?.message as { reasoning?: string })?.reasoning != null) {
215
215
  throw new AssistantMessageEmptyWithReasoningError((firstChoice?.message as { reasoning?: string })?.reasoning ?? "");
216
216
  }
@@ -236,7 +236,7 @@ async function step(
236
236
  // ----
237
237
  if (retry++ < (ctx.config?.retry ?? AgenticaConstant.RETRY)) {
238
238
  const failures: IFailure[] = [];
239
- for (const choice of completion.choices) {
239
+ for (const choice of (completion.choices ?? [])) {
240
240
  for (const tc of choice.message.tool_calls ?? []) {
241
241
  if (tc.type !== "function" || tc.function.name !== "selectFunctions") {
242
242
  continue;
@@ -261,7 +261,7 @@ async function step(
261
261
  // ----
262
262
  // PROCESS COMPLETION
263
263
  // ----
264
- for (const choice of completion.choices) {
264
+ for (const choice of (completion.choices ?? [])) {
265
265
  // FUNCTION CALLING
266
266
  if (choice.message.tool_calls != null) {
267
267
  for (const tc of choice.message.tool_calls) {
@@ -23,8 +23,8 @@ function transformCompletionChunk(source: string | Uint8Array): ChatCompletionCh
23
23
  }
24
24
 
25
25
  function accumulate(origin: ChatCompletion, chunk: ChatCompletionChunk): ChatCompletion {
26
- const choices = origin.choices;
27
- chunk.choices.forEach((choice) => {
26
+ const choices = origin.choices ?? [];
27
+ (chunk.choices ?? []).forEach((choice) => {
28
28
  const accChoice = choices[choice.index];
29
29
  if (accChoice != null) {
30
30
  choices[choice.index] = mergeChoice(accChoice, choice);
@@ -101,7 +101,7 @@ function merge(chunks: ChatCompletionChunk[]): ChatCompletion {
101
101
  } as ChatCompletion);
102
102
 
103
103
  // post-process
104
- result.choices.forEach((choice) => {
104
+ result.choices?.forEach((choice) => {
105
105
  choice.message.tool_calls?.filter(tc => tc.type === "function").forEach((toolCall) => {
106
106
  if (toolCall.function.arguments === "") {
107
107
  toolCall.function.arguments = "{}";
@@ -55,10 +55,10 @@ async function reduceStreamingWithDispatch(stream: ReadableStream<ChatCompletion
55
55
  }
56
56
  };
57
57
  if (acc.object === "chat.completion.chunk") {
58
- registerContext([acc, chunk].flatMap(v => v.choices));
58
+ registerContext([acc, chunk].flatMap(v => v.choices ?? []));
59
59
  return ChatGptCompletionMessageUtil.merge([acc, chunk]);
60
60
  }
61
- registerContext(chunk.choices);
61
+ registerContext(chunk.choices ?? []);
62
62
  return ChatGptCompletionMessageUtil.accumulate(acc, chunk);
63
63
  }, { abortSignal });
64
64