@agentica/core 0.32.9 → 0.33.0
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/events/AgenticaCallEvent.d.ts +0 -4
- package/lib/events/AgenticaExecuteEvent.d.ts +1 -1
- package/lib/events/AgenticaJsonParseErrorEvent.d.ts +4 -0
- package/lib/events/AgenticaResponseEvent.d.ts +1 -0
- package/lib/events/AgenticaValidateEvent.d.ts +1 -1
- package/lib/factory/events.d.ts +4 -2
- package/lib/factory/events.js +11 -3
- package/lib/factory/events.js.map +1 -1
- package/lib/index.mjs +100 -85
- package/lib/index.mjs.map +1 -1
- package/lib/json/IAgenticaEventJson.d.ts +9 -5
- package/lib/orchestrate/call.js +9 -5
- package/lib/orchestrate/call.js.map +1 -1
- package/lib/orchestrate/cancel.js +1 -1
- package/lib/orchestrate/cancel.js.map +1 -1
- package/lib/orchestrate/describe.js +1 -1
- package/lib/orchestrate/describe.js.map +1 -1
- package/lib/orchestrate/initialize.js +1 -1
- package/lib/orchestrate/initialize.js.map +1 -1
- package/lib/orchestrate/internal/cancelFunctionFromContext.js +1 -1
- package/lib/orchestrate/internal/cancelFunctionFromContext.js.map +1 -1
- package/lib/orchestrate/internal/selectFunctionFromContext.js +1 -1
- package/lib/orchestrate/internal/selectFunctionFromContext.js.map +1 -1
- package/lib/orchestrate/select.js +1 -1
- package/lib/orchestrate/select.js.map +1 -1
- package/lib/utils/ChatGptCompletionMessageUtil.d.ts +2 -2
- package/lib/utils/ChatGptCompletionMessageUtil.js +3 -3
- package/lib/utils/ChatGptCompletionMessageUtil.js.map +1 -1
- package/lib/utils/ChatGptCompletionMessageUtil.spec.js +4 -4
- package/lib/utils/ChatGptCompletionMessageUtil.spec.js.map +1 -1
- package/lib/utils/ChatGptCompletionStreamingUtil.js.map +1 -1
- package/lib/utils/ChatGptCompletionStreamingUtil.spec.js +2 -1
- package/lib/utils/ChatGptCompletionStreamingUtil.spec.js.map +1 -1
- package/lib/utils/JsonUtil.js +1 -1
- package/lib/utils/JsonUtil.js.map +1 -1
- package/lib/utils/JsonUtil.spec.js +38 -38
- package/lib/utils/JsonUtil.spec.js.map +1 -1
- package/lib/utils/StreamUtil.spec.js.map +1 -1
- package/lib/utils/request.d.ts +7 -7
- package/lib/utils/request.js +67 -65
- package/lib/utils/request.js.map +1 -1
- package/package.json +7 -7
- package/src/events/AgenticaCallEvent.ts +0 -5
- package/src/events/AgenticaExecuteEvent.ts +1 -1
- package/src/events/AgenticaJsonParseErrorEvent.ts +4 -0
- package/src/events/AgenticaResponseEvent.ts +2 -0
- package/src/events/AgenticaValidateEvent.ts +1 -1
- package/src/factory/events.ts +15 -5
- package/src/json/IAgenticaEventJson.ts +11 -6
- package/src/orchestrate/call.ts +14 -7
- package/src/orchestrate/cancel.ts +1 -1
- package/src/orchestrate/describe.ts +1 -1
- package/src/orchestrate/initialize.ts +1 -1
- package/src/orchestrate/internal/cancelFunctionFromContext.ts +1 -1
- package/src/orchestrate/internal/selectFunctionFromContext.ts +1 -1
- package/src/orchestrate/select.ts +1 -1
- package/src/utils/ChatGptCompletionMessageUtil.spec.ts +4 -4
- package/src/utils/ChatGptCompletionMessageUtil.ts +5 -4
- package/src/utils/ChatGptCompletionStreamingUtil.spec.ts +437 -435
- package/src/utils/ChatGptCompletionStreamingUtil.ts +5 -4
- package/src/utils/JsonUtil.spec.ts +58 -60
- package/src/utils/JsonUtil.ts +1 -3
- package/src/utils/StreamUtil.spec.ts +1 -1
- package/src/utils/StreamUtil.ts +2 -2
- package/src/utils/request.ts +92 -85
package/src/factory/events.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
159
|
+
call_id: string;
|
|
155
160
|
|
|
156
161
|
/**
|
|
157
162
|
* Target operation had called.
|
package/src/orchestrate/call.ts
CHANGED
|
@@ -174,7 +174,7 @@ export async function call<Model extends ILlmSchema.Model>(
|
|
|
174
174
|
async function predicate<Model extends ILlmSchema.Model>(
|
|
175
175
|
ctx: AgenticaContext<Model> | MicroAgenticaContext<Model>,
|
|
176
176
|
operation: AgenticaOperation<Model>,
|
|
177
|
-
toolCall: OpenAI.
|
|
177
|
+
toolCall: OpenAI.ChatCompletionMessageFunctionToolCall,
|
|
178
178
|
previousValidationErrors: AgenticaValidateEvent<Model>[],
|
|
179
179
|
life: number,
|
|
180
180
|
): Promise<AgenticaExecuteEvent<Model>> {
|
|
@@ -187,14 +187,14 @@ async function predicate<Model extends ILlmSchema.Model>(
|
|
|
187
187
|
);
|
|
188
188
|
await ctx.dispatch(call);
|
|
189
189
|
if (call.type === "jsonParseError") {
|
|
190
|
-
return correctJsonError(ctx, call, previousValidationErrors, life - 1);
|
|
190
|
+
return correctJsonError(ctx, toolCall, call, previousValidationErrors, life - 1);
|
|
191
191
|
}
|
|
192
192
|
|
|
193
193
|
// CHECK TYPE VALIDATION
|
|
194
194
|
const check: IValidation<unknown> = operation.function.validate(call.arguments);
|
|
195
195
|
if (check.success === false) {
|
|
196
196
|
const event: AgenticaValidateEvent<Model> = createValidateEvent({
|
|
197
|
-
|
|
197
|
+
call_id: toolCall.id,
|
|
198
198
|
operation,
|
|
199
199
|
result: check,
|
|
200
200
|
life,
|
|
@@ -225,6 +225,7 @@ async function correctTypeError<Model extends ILlmSchema.Model>(
|
|
|
225
225
|
): Promise<AgenticaExecuteEvent<Model>> {
|
|
226
226
|
return correctError<Model>(ctx, {
|
|
227
227
|
giveUp: () => createExecuteEvent({
|
|
228
|
+
call_id: callEvent.id,
|
|
228
229
|
operation: callEvent.operation,
|
|
229
230
|
arguments: callEvent.arguments,
|
|
230
231
|
value: {
|
|
@@ -260,12 +261,14 @@ async function correctTypeError<Model extends ILlmSchema.Model>(
|
|
|
260
261
|
|
|
261
262
|
async function correctJsonError<Model extends ILlmSchema.Model>(
|
|
262
263
|
ctx: AgenticaContext<Model> | MicroAgenticaContext<Model>,
|
|
264
|
+
toolCall: OpenAI.ChatCompletionMessageFunctionToolCall,
|
|
263
265
|
parseErrorEvent: AgenticaJsonParseErrorEvent<Model>,
|
|
264
266
|
previousValidationErrors: AgenticaValidateEvent<Model>[],
|
|
265
267
|
life: number,
|
|
266
268
|
): Promise<AgenticaExecuteEvent<Model>> {
|
|
267
269
|
return correctError<Model>(ctx, {
|
|
268
270
|
giveUp: () => createExecuteEvent({
|
|
271
|
+
call_id: toolCall.id,
|
|
269
272
|
operation: parseErrorEvent.operation,
|
|
270
273
|
arguments: {},
|
|
271
274
|
value: {
|
|
@@ -294,7 +297,7 @@ async function correctJsonError<Model extends ILlmSchema.Model>(
|
|
|
294
297
|
|
|
295
298
|
function parseArguments<Model extends ILlmSchema.Model>(
|
|
296
299
|
operation: AgenticaOperation<Model>,
|
|
297
|
-
toolCall: OpenAI.
|
|
300
|
+
toolCall: OpenAI.ChatCompletionMessageFunctionToolCall,
|
|
298
301
|
life: number,
|
|
299
302
|
): AgenticaCallEvent<Model> | AgenticaJsonParseErrorEvent<Model> {
|
|
300
303
|
try {
|
|
@@ -307,7 +310,7 @@ function parseArguments<Model extends ILlmSchema.Model>(
|
|
|
307
310
|
}
|
|
308
311
|
catch (error) {
|
|
309
312
|
return createJsonParseErrorEvent({
|
|
310
|
-
|
|
313
|
+
call_id: toolCall.id,
|
|
311
314
|
operation,
|
|
312
315
|
arguments: toolCall.function.arguments,
|
|
313
316
|
errorMessage: error instanceof Error ? error.message : String(error),
|
|
@@ -366,7 +369,7 @@ async function correctError<Model extends ILlmSchema.Model>(
|
|
|
366
369
|
name: props.operation.name,
|
|
367
370
|
arguments: props.toolCall.arguments,
|
|
368
371
|
},
|
|
369
|
-
} satisfies OpenAI.
|
|
372
|
+
} satisfies OpenAI.ChatCompletionMessageFunctionToolCall,
|
|
370
373
|
],
|
|
371
374
|
} satisfies OpenAI.ChatCompletionAssistantMessageParam,
|
|
372
375
|
{
|
|
@@ -412,7 +415,9 @@ async function correctError<Model extends ILlmSchema.Model>(
|
|
|
412
415
|
const chunks: OpenAI.ChatCompletionChunk[] = await StreamUtil.readAll(stream);
|
|
413
416
|
const completion: OpenAI.ChatCompletion = ChatGptCompletionMessageUtil.merge(chunks);
|
|
414
417
|
|
|
415
|
-
const toolCall: OpenAI.
|
|
418
|
+
const toolCall: OpenAI.ChatCompletionMessageFunctionToolCall | undefined = completion.choices[0]?.message.tool_calls?.filter(
|
|
419
|
+
tc => tc.type === "function",
|
|
420
|
+
).find(
|
|
416
421
|
s => s.function.name === props.operation.name,
|
|
417
422
|
);
|
|
418
423
|
return toolCall === undefined
|
|
@@ -448,6 +453,7 @@ async function executeFunction<Model extends ILlmSchema.Model>(
|
|
|
448
453
|
}
|
|
449
454
|
})();
|
|
450
455
|
return createExecuteEvent({
|
|
456
|
+
call_id: call.id,
|
|
451
457
|
operation: call.operation,
|
|
452
458
|
arguments: call.arguments,
|
|
453
459
|
value,
|
|
@@ -456,6 +462,7 @@ async function executeFunction<Model extends ILlmSchema.Model>(
|
|
|
456
462
|
}
|
|
457
463
|
catch (error) {
|
|
458
464
|
return createExecuteEvent({
|
|
465
|
+
call_id: call.id,
|
|
459
466
|
operation: call.operation,
|
|
460
467
|
arguments: call.arguments,
|
|
461
468
|
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
|
|
|
@@ -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) {
|
|
@@ -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,8 +177,8 @@ function mergeChoice(acc: ChatCompletion.Choice, cur: ChatCompletionChunk.Choice
|
|
|
176
177
|
return acc;
|
|
177
178
|
}
|
|
178
179
|
|
|
179
|
-
function mergeToolCalls(acc:
|
|
180
|
-
if (cur.function != null) {
|
|
180
|
+
function mergeToolCalls(acc: ChatCompletionMessageFunctionToolCall, cur: ChatCompletionChunk.Choice.Delta.ToolCall): ChatCompletionMessageToolCall {
|
|
181
|
+
if (cur.function != null && cur.type === "function") {
|
|
181
182
|
acc.function.arguments += cur.function.arguments ?? "";
|
|
182
183
|
acc.function.name += cur.function.name ?? "";
|
|
183
184
|
}
|