@agentica/core 0.32.9 → 0.33.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.
Files changed (65) hide show
  1. package/lib/events/AgenticaCallEvent.d.ts +0 -4
  2. package/lib/events/AgenticaExecuteEvent.d.ts +1 -1
  3. package/lib/events/AgenticaJsonParseErrorEvent.d.ts +4 -0
  4. package/lib/events/AgenticaResponseEvent.d.ts +1 -0
  5. package/lib/events/AgenticaValidateEvent.d.ts +1 -1
  6. package/lib/factory/events.d.ts +4 -2
  7. package/lib/factory/events.js +11 -3
  8. package/lib/factory/events.js.map +1 -1
  9. package/lib/index.mjs +99 -84
  10. package/lib/index.mjs.map +1 -1
  11. package/lib/json/IAgenticaEventJson.d.ts +9 -5
  12. package/lib/orchestrate/call.js +11 -7
  13. package/lib/orchestrate/call.js.map +1 -1
  14. package/lib/orchestrate/cancel.js +1 -1
  15. package/lib/orchestrate/cancel.js.map +1 -1
  16. package/lib/orchestrate/describe.js +1 -1
  17. package/lib/orchestrate/describe.js.map +1 -1
  18. package/lib/orchestrate/initialize.js +1 -1
  19. package/lib/orchestrate/initialize.js.map +1 -1
  20. package/lib/orchestrate/internal/cancelFunctionFromContext.js +1 -1
  21. package/lib/orchestrate/internal/cancelFunctionFromContext.js.map +1 -1
  22. package/lib/orchestrate/internal/selectFunctionFromContext.js +1 -1
  23. package/lib/orchestrate/internal/selectFunctionFromContext.js.map +1 -1
  24. package/lib/orchestrate/select.js +1 -1
  25. package/lib/orchestrate/select.js.map +1 -1
  26. package/lib/utils/ChatGptCompletionMessageUtil.d.ts +2 -2
  27. package/lib/utils/ChatGptCompletionMessageUtil.js +2 -2
  28. package/lib/utils/ChatGptCompletionMessageUtil.js.map +1 -1
  29. package/lib/utils/ChatGptCompletionMessageUtil.spec.js +4 -4
  30. package/lib/utils/ChatGptCompletionMessageUtil.spec.js.map +1 -1
  31. package/lib/utils/ChatGptCompletionStreamingUtil.js.map +1 -1
  32. package/lib/utils/ChatGptCompletionStreamingUtil.spec.js +2 -1
  33. package/lib/utils/ChatGptCompletionStreamingUtil.spec.js.map +1 -1
  34. package/lib/utils/JsonUtil.js +1 -1
  35. package/lib/utils/JsonUtil.js.map +1 -1
  36. package/lib/utils/JsonUtil.spec.js +38 -38
  37. package/lib/utils/JsonUtil.spec.js.map +1 -1
  38. package/lib/utils/StreamUtil.spec.js.map +1 -1
  39. package/lib/utils/request.d.ts +7 -7
  40. package/lib/utils/request.js +67 -65
  41. package/lib/utils/request.js.map +1 -1
  42. package/package.json +7 -7
  43. package/src/events/AgenticaCallEvent.ts +0 -5
  44. package/src/events/AgenticaExecuteEvent.ts +1 -1
  45. package/src/events/AgenticaJsonParseErrorEvent.ts +4 -0
  46. package/src/events/AgenticaResponseEvent.ts +2 -0
  47. package/src/events/AgenticaValidateEvent.ts +1 -1
  48. package/src/factory/events.ts +15 -5
  49. package/src/json/IAgenticaEventJson.ts +11 -6
  50. package/src/orchestrate/call.ts +16 -14
  51. package/src/orchestrate/cancel.ts +1 -1
  52. package/src/orchestrate/describe.ts +1 -1
  53. package/src/orchestrate/initialize.ts +1 -1
  54. package/src/orchestrate/internal/cancelFunctionFromContext.ts +1 -1
  55. package/src/orchestrate/internal/selectFunctionFromContext.ts +1 -1
  56. package/src/orchestrate/select.ts +1 -1
  57. package/src/utils/ChatGptCompletionMessageUtil.spec.ts +4 -4
  58. package/src/utils/ChatGptCompletionMessageUtil.ts +4 -5
  59. package/src/utils/ChatGptCompletionStreamingUtil.spec.ts +437 -435
  60. package/src/utils/ChatGptCompletionStreamingUtil.ts +5 -4
  61. package/src/utils/JsonUtil.spec.ts +58 -60
  62. package/src/utils/JsonUtil.ts +1 -3
  63. package/src/utils/StreamUtil.spec.ts +1 -1
  64. package/src/utils/StreamUtil.ts +2 -2
  65. package/src/utils/request.ts +92 -85
@@ -114,16 +114,18 @@ export function createCallEvent<Model extends ILlmSchema.Model>(props: {
114
114
  }
115
115
 
116
116
  export function createJsonParseErrorEvent<Model extends ILlmSchema.Model>(props: {
117
- id: string;
117
+ call_id: string;
118
118
  operation: AgenticaOperation<Model>;
119
119
  arguments: string;
120
120
  errorMessage: string;
121
121
  life: number;
122
122
  }): AgenticaJsonParseErrorEvent<Model> {
123
+ const id: string = v4();
123
124
  const created_at: string = new Date().toISOString();
124
125
  return {
125
126
  type: "jsonParseError",
126
- id: props.id,
127
+ id,
128
+ call_id: props.call_id,
127
129
  created_at,
128
130
  operation: props.operation,
129
131
  arguments: props.arguments,
@@ -133,22 +135,25 @@ export function createJsonParseErrorEvent<Model extends ILlmSchema.Model>(props:
133
135
  }
134
136
 
135
137
  export function createValidateEvent<Model extends ILlmSchema.Model>(props: {
136
- id: string;
138
+ call_id: string;
137
139
  operation: AgenticaOperation<Model>;
138
140
  result: IValidation.IFailure;
139
141
  life: number;
140
142
  }): AgenticaValidateEvent<Model> {
143
+ const id: string = v4();
141
144
  const created_at: string = new Date().toISOString();
142
145
  return {
143
146
  type: "validate",
144
- id: props.id,
147
+ id,
148
+ call_id: props.call_id,
145
149
  created_at,
146
150
  operation: props.operation,
147
151
  result: props.result,
148
152
  life: props.life,
149
153
  toJSON: () => ({
150
154
  type: "validate",
151
- id: props.id,
155
+ id,
156
+ call_id: props.call_id,
152
157
  created_at,
153
158
  operation: props.operation.toJSON(),
154
159
  result: props.result,
@@ -158,6 +163,7 @@ export function createValidateEvent<Model extends ILlmSchema.Model>(props: {
158
163
  }
159
164
 
160
165
  export function createExecuteEvent<Model extends ILlmSchema.Model>(props: {
166
+ call_id: string;
161
167
  operation: AgenticaOperation<Model>;
162
168
  arguments: Record<string, unknown>;
163
169
  value: unknown;
@@ -169,6 +175,7 @@ export function createExecuteEvent<Model extends ILlmSchema.Model>(props: {
169
175
  type: "execute",
170
176
  id,
171
177
  created_at,
178
+ call_id: props.call_id,
172
179
  protocol: props.operation.protocol as "class",
173
180
  operation: props.operation as AgenticaOperation.Class<Model>,
174
181
  arguments: props.arguments,
@@ -178,6 +185,7 @@ export function createExecuteEvent<Model extends ILlmSchema.Model>(props: {
178
185
  type: "execute",
179
186
  id,
180
187
  created_at,
188
+ call_id: props.call_id,
181
189
  protocol: props.operation.protocol as "class",
182
190
  operation: props.operation.toJSON(),
183
191
  arguments: props.arguments,
@@ -318,6 +326,7 @@ export function createRequestEvent(props: {
318
326
  }
319
327
 
320
328
  export function createResponseEvent(props: {
329
+ request_id: string;
321
330
  source: AgenticaEventSource;
322
331
  body: OpenAI.ChatCompletionCreateParamsStreaming;
323
332
  options?: OpenAI.RequestOptions | undefined;
@@ -329,6 +338,7 @@ export function createResponseEvent(props: {
329
338
  return {
330
339
  type: "response",
331
340
  id,
341
+ request_id: props.request_id,
332
342
  created_at,
333
343
  source: props.source,
334
344
  body: props.body,
@@ -89,11 +89,6 @@ export namespace IAgenticaEventJson {
89
89
  * Event of calling a function.
90
90
  */
91
91
  export interface ICall extends IBase<"call"> {
92
- /**
93
- * ID of the tool calling.
94
- */
95
- id: string;
96
-
97
92
  /**
98
93
  * Target operation to call.
99
94
  */
@@ -110,6 +105,11 @@ export namespace IAgenticaEventJson {
110
105
  }
111
106
 
112
107
  export interface IValidate extends IBase<"validate"> {
108
+ /**
109
+ * ID of the tool calling.
110
+ */
111
+ call_id: string;
112
+
113
113
  /**
114
114
  * Target operation to call.
115
115
  */
@@ -124,6 +124,11 @@ export namespace IAgenticaEventJson {
124
124
  }
125
125
 
126
126
  export interface IJsonParseError extends IBase<"jsonParseError"> {
127
+ /**
128
+ * ID of the tool calling.
129
+ */
130
+ call_id: string;
131
+
127
132
  /**
128
133
  * Target operation to call.
129
134
  */
@@ -151,7 +156,7 @@ export namespace IAgenticaEventJson {
151
156
  /**
152
157
  * ID of the tool calling.
153
158
  */
154
- id: string;
159
+ call_id: string;
155
160
 
156
161
  /**
157
162
  * Target operation had called.
@@ -85,13 +85,10 @@ export async function call<Model extends ILlmSchema.Model>(
85
85
  type: "function",
86
86
  function: {
87
87
  name: s.name,
88
-
89
88
  description: s.function.description,
90
89
  parameters: (
91
- (
92
- "separated" in s.function
93
- && s.function.separated !== undefined
94
- )
90
+ "separated" in s.function
91
+ && s.function.separated !== undefined
95
92
  ? (s.function.separated.llm
96
93
  ?? ({
97
94
  type: "object",
@@ -100,7 +97,6 @@ export async function call<Model extends ILlmSchema.Model>(
100
97
  additionalProperties: false,
101
98
  $defs: {},
102
99
  } satisfies IChatGptSchema.IParameters))
103
-
104
100
  : s.function.parameters) as Record<string, any>,
105
101
  },
106
102
  }) as OpenAI.ChatCompletionTool,
@@ -174,7 +170,7 @@ export async function call<Model extends ILlmSchema.Model>(
174
170
  async function predicate<Model extends ILlmSchema.Model>(
175
171
  ctx: AgenticaContext<Model> | MicroAgenticaContext<Model>,
176
172
  operation: AgenticaOperation<Model>,
177
- toolCall: OpenAI.ChatCompletionMessageToolCall,
173
+ toolCall: OpenAI.ChatCompletionMessageFunctionToolCall,
178
174
  previousValidationErrors: AgenticaValidateEvent<Model>[],
179
175
  life: number,
180
176
  ): Promise<AgenticaExecuteEvent<Model>> {
@@ -187,14 +183,14 @@ async function predicate<Model extends ILlmSchema.Model>(
187
183
  );
188
184
  await ctx.dispatch(call);
189
185
  if (call.type === "jsonParseError") {
190
- return correctJsonError(ctx, call, previousValidationErrors, life - 1);
186
+ return correctJsonError(ctx, toolCall, call, previousValidationErrors, life - 1);
191
187
  }
192
188
 
193
189
  // CHECK TYPE VALIDATION
194
190
  const check: IValidation<unknown> = operation.function.validate(call.arguments);
195
191
  if (check.success === false) {
196
192
  const event: AgenticaValidateEvent<Model> = createValidateEvent({
197
- id: toolCall.id,
193
+ call_id: toolCall.id,
198
194
  operation,
199
195
  result: check,
200
196
  life,
@@ -225,6 +221,7 @@ async function correctTypeError<Model extends ILlmSchema.Model>(
225
221
  ): Promise<AgenticaExecuteEvent<Model>> {
226
222
  return correctError<Model>(ctx, {
227
223
  giveUp: () => createExecuteEvent({
224
+ call_id: callEvent.id,
228
225
  operation: callEvent.operation,
229
226
  arguments: callEvent.arguments,
230
227
  value: {
@@ -260,12 +257,14 @@ async function correctTypeError<Model extends ILlmSchema.Model>(
260
257
 
261
258
  async function correctJsonError<Model extends ILlmSchema.Model>(
262
259
  ctx: AgenticaContext<Model> | MicroAgenticaContext<Model>,
260
+ toolCall: OpenAI.ChatCompletionMessageFunctionToolCall,
263
261
  parseErrorEvent: AgenticaJsonParseErrorEvent<Model>,
264
262
  previousValidationErrors: AgenticaValidateEvent<Model>[],
265
263
  life: number,
266
264
  ): Promise<AgenticaExecuteEvent<Model>> {
267
265
  return correctError<Model>(ctx, {
268
266
  giveUp: () => createExecuteEvent({
267
+ call_id: toolCall.id,
269
268
  operation: parseErrorEvent.operation,
270
269
  arguments: {},
271
270
  value: {
@@ -294,7 +293,7 @@ async function correctJsonError<Model extends ILlmSchema.Model>(
294
293
 
295
294
  function parseArguments<Model extends ILlmSchema.Model>(
296
295
  operation: AgenticaOperation<Model>,
297
- toolCall: OpenAI.ChatCompletionMessageToolCall,
296
+ toolCall: OpenAI.ChatCompletionMessageFunctionToolCall,
298
297
  life: number,
299
298
  ): AgenticaCallEvent<Model> | AgenticaJsonParseErrorEvent<Model> {
300
299
  try {
@@ -307,7 +306,7 @@ function parseArguments<Model extends ILlmSchema.Model>(
307
306
  }
308
307
  catch (error) {
309
308
  return createJsonParseErrorEvent({
310
- id: toolCall.id,
309
+ call_id: toolCall.id,
311
310
  operation,
312
311
  arguments: toolCall.function.arguments,
313
312
  errorMessage: error instanceof Error ? error.message : String(error),
@@ -366,7 +365,7 @@ async function correctError<Model extends ILlmSchema.Model>(
366
365
  name: props.operation.name,
367
366
  arguments: props.toolCall.arguments,
368
367
  },
369
- } satisfies OpenAI.ChatCompletionMessageToolCall,
368
+ } satisfies OpenAI.ChatCompletionMessageFunctionToolCall,
370
369
  ],
371
370
  } satisfies OpenAI.ChatCompletionAssistantMessageParam,
372
371
  {
@@ -411,8 +410,9 @@ async function correctError<Model extends ILlmSchema.Model>(
411
410
  });
412
411
  const chunks: OpenAI.ChatCompletionChunk[] = await StreamUtil.readAll(stream);
413
412
  const completion: OpenAI.ChatCompletion = ChatGptCompletionMessageUtil.merge(chunks);
414
-
415
- const toolCall: OpenAI.ChatCompletionMessageToolCall | undefined = completion.choices[0]?.message.tool_calls?.find(
413
+ const toolCall: OpenAI.ChatCompletionMessageFunctionToolCall | undefined = completion.choices[0]?.message.tool_calls?.filter(
414
+ tc => tc.type === "function",
415
+ ).find(
416
416
  s => s.function.name === props.operation.name,
417
417
  );
418
418
  return toolCall === undefined
@@ -448,6 +448,7 @@ async function executeFunction<Model extends ILlmSchema.Model>(
448
448
  }
449
449
  })();
450
450
  return createExecuteEvent({
451
+ call_id: call.id,
451
452
  operation: call.operation,
452
453
  arguments: call.arguments,
453
454
  value,
@@ -456,6 +457,7 @@ async function executeFunction<Model extends ILlmSchema.Model>(
456
457
  }
457
458
  catch (error) {
458
459
  return createExecuteEvent({
460
+ call_id: call.id,
459
461
  operation: call.operation,
460
462
  arguments: call.arguments,
461
463
  value: error instanceof Error
@@ -182,7 +182,7 @@ async function step<Model extends ILlmSchema.Model>(
182
182
  const failures: IFailure[] = [];
183
183
  for (const choice of completion.choices) {
184
184
  for (const tc of choice.message.tool_calls ?? []) {
185
- if (tc.function.name !== "cancelFunctions") {
185
+ if (tc.type !== "function" || tc.function.name !== "cancelFunctions") {
186
186
  continue;
187
187
  }
188
188
 
@@ -44,7 +44,7 @@ export async function describe<Model extends ILlmSchema.Model>(
44
44
  executes: histories,
45
45
  ...props,
46
46
  });
47
- ctx.dispatch(event);
47
+ void ctx.dispatch(event).catch(() => {});
48
48
  }, ctx.abortSignal);
49
49
  }
50
50
 
@@ -65,7 +65,7 @@ export async function initialize<Model extends ILlmSchema.Model>(ctx: AgenticaCo
65
65
 
66
66
  const completion = await reduceStreamingWithDispatch(completionStream, (props) => {
67
67
  const event: AgenticaAssistantMessageEvent = createAssistantMessageEvent(props);
68
- ctx.dispatch(event);
68
+ void ctx.dispatch(event).catch(() => {});
69
69
  }, ctx.abortSignal);
70
70
 
71
71
  if (completion === null) {
@@ -31,5 +31,5 @@ export function cancelFunctionFromContext<Model extends ILlmSchema.Model>(
31
31
  reason: reference.reason,
32
32
  }),
33
33
  });
34
- ctx.dispatch(event);
34
+ void ctx.dispatch(event).catch(() => {});
35
35
  }
@@ -34,5 +34,5 @@ export function selectFunctionFromContext<
34
34
  const event: AgenticaSelectEvent<Model> = createSelectEvent({
35
35
  selection,
36
36
  });
37
- ctx.dispatch(event);
37
+ void ctx.dispatch(event).catch(() => {});
38
38
  }
@@ -226,7 +226,7 @@ async function step<Model extends ILlmSchema.Model>(
226
226
  const failures: IFailure[] = [];
227
227
  for (const choice of completion.choices) {
228
228
  for (const tc of choice.message.tool_calls ?? []) {
229
- if (tc.function.name !== "selectFunctions") {
229
+ if (tc.type !== "function" || tc.function.name !== "selectFunctions") {
230
230
  continue;
231
231
  }
232
232
  const input: object = JsonUtil.parse(tc.function.arguments) as object;
@@ -124,8 +124,8 @@ describe("chatGptCompletionMessageUtil", () => {
124
124
  };
125
125
 
126
126
  const result = ChatGptCompletionMessageUtil.accumulate(origin, chunk);
127
- expect(result.choices[0]?.message.tool_calls?.[0]?.function.name).toBe("test_function");
128
- expect(result.choices[0]?.message.tool_calls?.[0]?.function.arguments).toBe("{\"arg\": \"value\"}{\"arg2\": \"value2\"}");
127
+ expect(result.choices[0]?.message.tool_calls?.filter(tc => tc.type === "function")[0]?.function.name).toBe("test_function");
128
+ expect(result.choices[0]?.message.tool_calls?.filter(tc => tc.type === "function")[0]?.function.arguments).toBe("{\"arg\": \"value\"}{\"arg2\": \"value2\"}");
129
129
  });
130
130
 
131
131
  it("should handle usage aggregation", () => {
@@ -292,7 +292,7 @@ describe("chatGptCompletionMessageUtil", () => {
292
292
  };
293
293
 
294
294
  const result = ChatGptCompletionMessageUtil.mergeToolCalls(acc, cur);
295
- expect(result.function.arguments).toBe("{\"arg\": \"value\"}{\"arg2\": \"value2\"}");
295
+ expect(result.type === "function" ? result.function.arguments : "custom").toBe("{\"arg\": \"value\"}{\"arg2\": \"value2\"}");
296
296
  });
297
297
 
298
298
  it("should merge tool call function name", () => {
@@ -314,7 +314,7 @@ describe("chatGptCompletionMessageUtil", () => {
314
314
  };
315
315
 
316
316
  const result = ChatGptCompletionMessageUtil.mergeToolCalls(acc, cur);
317
- expect(result.function.name).toBe("test_function");
317
+ expect(result.type === "function" ? result.function.name : "custom").toBe("test_function");
318
318
  });
319
319
  });
320
320
  });
@@ -2,6 +2,7 @@ import type {
2
2
  ChatCompletion,
3
3
  ChatCompletionChunk,
4
4
  ChatCompletionMessage,
5
+ ChatCompletionMessageFunctionToolCall,
5
6
  ChatCompletionMessageToolCall,
6
7
  } from "openai/resources";
7
8
 
@@ -101,7 +102,7 @@ function merge(chunks: ChatCompletionChunk[]): ChatCompletion {
101
102
 
102
103
  // post-process
103
104
  result.choices.forEach((choice) => {
104
- choice.message.tool_calls?.forEach((toolCall) => {
105
+ choice.message.tool_calls?.filter(tc => tc.type === "function").forEach((toolCall) => {
105
106
  if (toolCall.function.arguments === "") {
106
107
  toolCall.function.arguments = "{}";
107
108
  }
@@ -154,7 +155,7 @@ function mergeChoice(acc: ChatCompletion.Choice, cur: ChatCompletionChunk.Choice
154
155
 
155
156
  cur.delta.tool_calls.forEach((toolCall) => {
156
157
  const existingToolCall = toolCalls[toolCall.index];
157
- if (existingToolCall != null) {
158
+ if (existingToolCall != null && existingToolCall.type === "function") {
158
159
  toolCalls[toolCall.index] = mergeToolCalls(
159
160
  existingToolCall,
160
161
  toolCall,
@@ -176,14 +177,12 @@ function mergeChoice(acc: ChatCompletion.Choice, cur: ChatCompletionChunk.Choice
176
177
  return acc;
177
178
  }
178
179
 
179
- function mergeToolCalls(acc: ChatCompletionMessageToolCall, cur: ChatCompletionChunk.Choice.Delta.ToolCall): ChatCompletionMessageToolCall {
180
+ function mergeToolCalls(acc: ChatCompletionMessageFunctionToolCall, cur: ChatCompletionChunk.Choice.Delta.ToolCall): ChatCompletionMessageToolCall {
180
181
  if (cur.function != null) {
181
182
  acc.function.arguments += cur.function.arguments ?? "";
182
183
  acc.function.name += cur.function.name ?? "";
183
184
  }
184
-
185
185
  acc.id += cur.id ?? "";
186
-
187
186
  return acc;
188
187
  }
189
188