@aigne/core 1.9.0 → 1.10.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/CHANGELOG.md +12 -0
- package/lib/cjs/agents/agent.d.ts +29 -3
- package/lib/cjs/agents/agent.js +47 -22
- package/lib/cjs/agents/ai-agent.d.ts +3 -2
- package/lib/cjs/agents/ai-agent.js +22 -9
- package/lib/cjs/agents/user-agent.d.ts +3 -3
- package/lib/cjs/agents/user-agent.js +14 -8
- package/lib/cjs/execution-engine/context.d.ts +18 -7
- package/lib/cjs/execution-engine/context.js +76 -28
- package/lib/cjs/models/chat-model.d.ts +4 -0
- package/lib/cjs/models/chat-model.js +6 -0
- package/lib/cjs/models/open-router-chat-model.d.ts +1 -0
- package/lib/cjs/models/open-router-chat-model.js +1 -0
- package/lib/cjs/models/openai-chat-model.d.ts +12 -3
- package/lib/cjs/models/openai-chat-model.js +112 -46
- package/lib/cjs/utils/stream-utils.d.ts +15 -0
- package/lib/cjs/utils/stream-utils.js +159 -0
- package/lib/cjs/utils/type-utils.d.ts +1 -0
- package/lib/cjs/utils/type-utils.js +7 -0
- package/lib/dts/agents/agent.d.ts +29 -3
- package/lib/dts/agents/ai-agent.d.ts +3 -2
- package/lib/dts/agents/user-agent.d.ts +3 -3
- package/lib/dts/execution-engine/context.d.ts +18 -7
- package/lib/dts/models/chat-model.d.ts +4 -0
- package/lib/dts/models/open-router-chat-model.d.ts +1 -0
- package/lib/dts/models/openai-chat-model.d.ts +12 -3
- package/lib/dts/utils/stream-utils.d.ts +15 -0
- package/lib/dts/utils/type-utils.d.ts +1 -0
- package/lib/esm/agents/agent.d.ts +29 -3
- package/lib/esm/agents/agent.js +47 -22
- package/lib/esm/agents/ai-agent.d.ts +3 -2
- package/lib/esm/agents/ai-agent.js +23 -10
- package/lib/esm/agents/user-agent.d.ts +3 -3
- package/lib/esm/agents/user-agent.js +15 -9
- package/lib/esm/execution-engine/context.d.ts +18 -7
- package/lib/esm/execution-engine/context.js +78 -30
- package/lib/esm/models/chat-model.d.ts +4 -0
- package/lib/esm/models/chat-model.js +6 -0
- package/lib/esm/models/open-router-chat-model.d.ts +1 -0
- package/lib/esm/models/open-router-chat-model.js +1 -0
- package/lib/esm/models/openai-chat-model.d.ts +12 -3
- package/lib/esm/models/openai-chat-model.js +112 -45
- package/lib/esm/utils/stream-utils.d.ts +15 -0
- package/lib/esm/utils/stream-utils.js +144 -0
- package/lib/esm/utils/type-utils.d.ts +1 -0
- package/lib/esm/utils/type-utils.js +6 -0
- package/package.json +2 -1
|
@@ -7,16 +7,20 @@ exports.ROLE_MAP = exports.OpenAIChatModel = exports.openAIChatModelOptionsSchem
|
|
|
7
7
|
exports.contentsFromInputMessages = contentsFromInputMessages;
|
|
8
8
|
exports.toolsFromInputTools = toolsFromInputTools;
|
|
9
9
|
exports.jsonSchemaToOpenAIJsonSchema = jsonSchemaToOpenAIJsonSchema;
|
|
10
|
-
exports.extractResultFromStream = extractResultFromStream;
|
|
11
10
|
const nanoid_1 = require("nanoid");
|
|
12
11
|
const openai_1 = __importDefault(require("openai"));
|
|
13
12
|
const zod_1 = require("zod");
|
|
14
13
|
const json_schema_js_1 = require("../utils/json-schema.js");
|
|
15
14
|
const model_utils_js_1 = require("../utils/model-utils.js");
|
|
16
15
|
const prompts_js_1 = require("../utils/prompts.js");
|
|
16
|
+
const stream_utils_js_1 = require("../utils/stream-utils.js");
|
|
17
17
|
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
18
18
|
const chat_model_js_1 = require("./chat-model.js");
|
|
19
19
|
const CHAT_MODEL_OPENAI_DEFAULT_MODEL = "gpt-4o-mini";
|
|
20
|
+
const OPENAI_CHAT_MODEL_CAPABILITIES = {
|
|
21
|
+
"o4-mini": { supportsParallelToolCalls: false, supportsTemperature: false },
|
|
22
|
+
"o3-mini": { supportsParallelToolCalls: false, supportsTemperature: false },
|
|
23
|
+
};
|
|
20
24
|
exports.openAIChatModelOptionsSchema = zod_1.z.object({
|
|
21
25
|
apiKey: zod_1.z.string().optional(),
|
|
22
26
|
baseURL: zod_1.z.string().optional(),
|
|
@@ -39,6 +43,8 @@ class OpenAIChatModel extends chat_model_js_1.ChatModel {
|
|
|
39
43
|
this.options = options;
|
|
40
44
|
if (options)
|
|
41
45
|
(0, type_utils_js_1.checkArguments)(this.name, exports.openAIChatModelOptionsSchema, options);
|
|
46
|
+
const preset = options?.model ? OPENAI_CHAT_MODEL_CAPABILITIES[options.model] : undefined;
|
|
47
|
+
Object.assign(this, preset);
|
|
42
48
|
}
|
|
43
49
|
_client;
|
|
44
50
|
apiKeyEnvName = "OPENAI_API_KEY";
|
|
@@ -48,6 +54,7 @@ class OpenAIChatModel extends chat_model_js_1.ChatModel {
|
|
|
48
54
|
supportsToolsUseWithJsonSchema = true;
|
|
49
55
|
supportsParallelToolCalls = true;
|
|
50
56
|
supportsToolsEmptyParameters = true;
|
|
57
|
+
supportsTemperature = true;
|
|
51
58
|
get client() {
|
|
52
59
|
const apiKey = this.options?.apiKey || process.env[this.apiKeyEnvName] || this.apiKeyDefault;
|
|
53
60
|
if (!apiKey)
|
|
@@ -61,14 +68,17 @@ class OpenAIChatModel extends chat_model_js_1.ChatModel {
|
|
|
61
68
|
get modelOptions() {
|
|
62
69
|
return this.options?.modelOptions;
|
|
63
70
|
}
|
|
64
|
-
async process(input) {
|
|
71
|
+
async process(input, _context, options) {
|
|
72
|
+
const messages = await this.getRunMessages(input);
|
|
65
73
|
const body = {
|
|
66
74
|
model: this.options?.model || CHAT_MODEL_OPENAI_DEFAULT_MODEL,
|
|
67
|
-
temperature:
|
|
75
|
+
temperature: this.supportsTemperature
|
|
76
|
+
? (input.modelOptions?.temperature ?? this.modelOptions?.temperature)
|
|
77
|
+
: undefined,
|
|
68
78
|
top_p: input.modelOptions?.topP ?? this.modelOptions?.topP,
|
|
69
79
|
frequency_penalty: input.modelOptions?.frequencyPenalty ?? this.modelOptions?.frequencyPenalty,
|
|
70
80
|
presence_penalty: input.modelOptions?.presencePenalty ?? this.modelOptions?.presencePenalty,
|
|
71
|
-
messages
|
|
81
|
+
messages,
|
|
72
82
|
stream_options: {
|
|
73
83
|
include_usage: true,
|
|
74
84
|
},
|
|
@@ -84,6 +94,9 @@ class OpenAIChatModel extends chat_model_js_1.ChatModel {
|
|
|
84
94
|
parallel_tool_calls: this.getParallelToolCalls(input),
|
|
85
95
|
response_format: responseFormat,
|
|
86
96
|
});
|
|
97
|
+
if (options?.streaming && input.responseFormat?.type !== "json_schema") {
|
|
98
|
+
return await extractResultFromStream(stream, false, true);
|
|
99
|
+
}
|
|
87
100
|
const result = await extractResultFromStream(stream, jsonMode);
|
|
88
101
|
if (!this.supportsToolsUseWithJsonSchema &&
|
|
89
102
|
!result.toolCalls?.length &&
|
|
@@ -232,51 +245,104 @@ function jsonSchemaToOpenAIJsonSchema(schema) {
|
|
|
232
245
|
}
|
|
233
246
|
return schema;
|
|
234
247
|
}
|
|
235
|
-
async function extractResultFromStream(stream, jsonMode
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
+
async function extractResultFromStream(stream, jsonMode, streaming) {
|
|
249
|
+
const result = new ReadableStream({
|
|
250
|
+
async start(controller) {
|
|
251
|
+
try {
|
|
252
|
+
let text = "";
|
|
253
|
+
let refusal = "";
|
|
254
|
+
const toolCalls = [];
|
|
255
|
+
let model;
|
|
256
|
+
for await (const chunk of stream) {
|
|
257
|
+
const choice = chunk.choices?.[0];
|
|
258
|
+
if (!model) {
|
|
259
|
+
model = chunk.model;
|
|
260
|
+
controller.enqueue({
|
|
261
|
+
delta: {
|
|
262
|
+
json: {
|
|
263
|
+
model,
|
|
264
|
+
},
|
|
265
|
+
},
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
if (choice?.delta.tool_calls?.length) {
|
|
269
|
+
for (const call of choice.delta.tool_calls) {
|
|
270
|
+
// Gemini not support tool call delta
|
|
271
|
+
if (call.index !== undefined) {
|
|
272
|
+
handleToolCallDelta(toolCalls, call);
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
handleCompleteToolCall(toolCalls, call);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
if (choice?.delta.content) {
|
|
280
|
+
text += choice.delta.content;
|
|
281
|
+
if (!jsonMode) {
|
|
282
|
+
controller.enqueue({
|
|
283
|
+
delta: {
|
|
284
|
+
text: {
|
|
285
|
+
text: choice.delta.content,
|
|
286
|
+
},
|
|
287
|
+
},
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
if (choice?.delta.refusal) {
|
|
292
|
+
refusal += choice.delta.refusal;
|
|
293
|
+
if (!jsonMode) {
|
|
294
|
+
controller.enqueue({
|
|
295
|
+
delta: {
|
|
296
|
+
text: { text: choice.delta.refusal },
|
|
297
|
+
},
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
if (chunk.usage) {
|
|
302
|
+
controller.enqueue({
|
|
303
|
+
delta: {
|
|
304
|
+
json: {
|
|
305
|
+
usage: {
|
|
306
|
+
inputTokens: chunk.usage.prompt_tokens,
|
|
307
|
+
outputTokens: chunk.usage.completion_tokens,
|
|
308
|
+
},
|
|
309
|
+
},
|
|
310
|
+
},
|
|
311
|
+
});
|
|
312
|
+
}
|
|
248
313
|
}
|
|
249
|
-
|
|
250
|
-
|
|
314
|
+
text = text || refusal;
|
|
315
|
+
if (jsonMode && text) {
|
|
316
|
+
controller.enqueue({
|
|
317
|
+
delta: {
|
|
318
|
+
json: {
|
|
319
|
+
json: (0, json_schema_js_1.parseJSON)(text),
|
|
320
|
+
},
|
|
321
|
+
},
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
if (toolCalls.length) {
|
|
325
|
+
controller.enqueue({
|
|
326
|
+
delta: {
|
|
327
|
+
json: {
|
|
328
|
+
toolCalls: toolCalls.map(({ args, ...c }) => ({
|
|
329
|
+
...c,
|
|
330
|
+
function: { ...c.function, arguments: (0, json_schema_js_1.parseJSON)(args) },
|
|
331
|
+
})),
|
|
332
|
+
},
|
|
333
|
+
},
|
|
334
|
+
});
|
|
251
335
|
}
|
|
252
336
|
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
}
|
|
263
|
-
const result = {
|
|
264
|
-
usage,
|
|
265
|
-
model,
|
|
266
|
-
};
|
|
267
|
-
if (jsonMode && text) {
|
|
268
|
-
result.json = (0, json_schema_js_1.parseJSON)(text);
|
|
269
|
-
}
|
|
270
|
-
else {
|
|
271
|
-
result.text = text;
|
|
272
|
-
}
|
|
273
|
-
if (toolCalls.length) {
|
|
274
|
-
result.toolCalls = toolCalls.map(({ args, ...c }) => ({
|
|
275
|
-
...c,
|
|
276
|
-
function: { ...c.function, arguments: (0, json_schema_js_1.parseJSON)(args) },
|
|
277
|
-
}));
|
|
278
|
-
}
|
|
279
|
-
return result;
|
|
337
|
+
catch (error) {
|
|
338
|
+
controller.error(error);
|
|
339
|
+
}
|
|
340
|
+
finally {
|
|
341
|
+
controller.close();
|
|
342
|
+
}
|
|
343
|
+
},
|
|
344
|
+
});
|
|
345
|
+
return streaming ? result : await (0, stream_utils_js_1.agentResponseStreamToObject)(result);
|
|
280
346
|
}
|
|
281
347
|
function handleToolCallDelta(toolCalls, call) {
|
|
282
348
|
toolCalls[call.index] ??= {
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { AgentProcessAsyncGenerator, AgentResponseChunk, AgentResponseStream, Message } from "../agents/agent.js";
|
|
2
|
+
import { type PromiseOrValue } from "./type-utils.js";
|
|
3
|
+
export declare function objectToAgentResponseStream<T extends Message>(json: T): AgentResponseStream<T>;
|
|
4
|
+
export declare function mergeAgentResponseChunk<T extends Message>(output: T, chunk: AgentResponseChunk<T>): T;
|
|
5
|
+
export declare function agentResponseStreamToObject<T extends Message>(stream: AgentResponseStream<T> | AgentProcessAsyncGenerator<T>): Promise<T>;
|
|
6
|
+
export declare function asyncGeneratorToReadableStream<T extends Message>(generator: AgentProcessAsyncGenerator<T>): AgentResponseStream<T>;
|
|
7
|
+
export declare function onAgentResponseStreamEnd<T extends Message>(stream: AgentResponseStream<T>, callback: (result: T) => PromiseOrValue<Partial<T> | void>, options?: {
|
|
8
|
+
errorCallback?: (error: Error) => Error;
|
|
9
|
+
processChunk?: (chunk: AgentResponseChunk<T>) => AgentResponseChunk<T>;
|
|
10
|
+
}): ReadableStream<any>;
|
|
11
|
+
export declare function isAsyncGenerator<T extends AsyncGenerator>(value: AsyncGenerator | unknown): value is T;
|
|
12
|
+
export declare function arrayToAgentProcessAsyncGenerator<T extends Message>(chunks: (AgentResponseChunk<T> | Error)[], result?: Partial<T>): AgentProcessAsyncGenerator<T>;
|
|
13
|
+
export declare function arrayToAgentResponseStream<T>(chunks: (AgentResponseChunk<T> | Error)[]): AgentResponseStream<T>;
|
|
14
|
+
export declare function readableStreamToArray<T>(stream: ReadableStream<T>): Promise<T[]>;
|
|
15
|
+
export declare function readableStreamToAsyncIterator<T>(stream: ReadableStream<T>): AsyncIterable<T>;
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.objectToAgentResponseStream = objectToAgentResponseStream;
|
|
7
|
+
exports.mergeAgentResponseChunk = mergeAgentResponseChunk;
|
|
8
|
+
exports.agentResponseStreamToObject = agentResponseStreamToObject;
|
|
9
|
+
exports.asyncGeneratorToReadableStream = asyncGeneratorToReadableStream;
|
|
10
|
+
exports.onAgentResponseStreamEnd = onAgentResponseStreamEnd;
|
|
11
|
+
exports.isAsyncGenerator = isAsyncGenerator;
|
|
12
|
+
exports.arrayToAgentProcessAsyncGenerator = arrayToAgentProcessAsyncGenerator;
|
|
13
|
+
exports.arrayToAgentResponseStream = arrayToAgentResponseStream;
|
|
14
|
+
exports.readableStreamToArray = readableStreamToArray;
|
|
15
|
+
exports.readableStreamToAsyncIterator = readableStreamToAsyncIterator;
|
|
16
|
+
const fast_deep_equal_1 = __importDefault(require("fast-deep-equal"));
|
|
17
|
+
const type_utils_js_1 = require("./type-utils.js");
|
|
18
|
+
function objectToAgentResponseStream(json) {
|
|
19
|
+
return new ReadableStream({
|
|
20
|
+
start(controller) {
|
|
21
|
+
controller.enqueue({ delta: { json } });
|
|
22
|
+
controller.close();
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
function mergeAgentResponseChunk(output, chunk) {
|
|
27
|
+
if (chunk.delta.text) {
|
|
28
|
+
for (const [key, text] of Object.entries(chunk.delta.text)) {
|
|
29
|
+
const original = output[key];
|
|
30
|
+
const t = (original || "") + (text || "");
|
|
31
|
+
if (t)
|
|
32
|
+
Object.assign(output, { [key]: t });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (chunk.delta.json) {
|
|
36
|
+
Object.assign(output, (0, type_utils_js_1.omitBy)(chunk.delta.json, (v) => v === undefined));
|
|
37
|
+
}
|
|
38
|
+
return output;
|
|
39
|
+
}
|
|
40
|
+
async function agentResponseStreamToObject(stream) {
|
|
41
|
+
const json = {};
|
|
42
|
+
if (stream instanceof ReadableStream) {
|
|
43
|
+
for await (const value of readableStreamToAsyncIterator(stream)) {
|
|
44
|
+
mergeAgentResponseChunk(json, value);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
for (;;) {
|
|
49
|
+
const chunk = await stream.next();
|
|
50
|
+
if (chunk.value) {
|
|
51
|
+
if (chunk.done) {
|
|
52
|
+
Object.assign(json, chunk.value);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
mergeAgentResponseChunk(json, chunk.value);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (chunk.done)
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return json;
|
|
63
|
+
}
|
|
64
|
+
function asyncGeneratorToReadableStream(generator) {
|
|
65
|
+
return new ReadableStream({
|
|
66
|
+
async start(controller) {
|
|
67
|
+
try {
|
|
68
|
+
for (;;) {
|
|
69
|
+
const chunk = await generator.next();
|
|
70
|
+
if (chunk.value) {
|
|
71
|
+
if (chunk.done) {
|
|
72
|
+
controller.enqueue({ delta: { json: chunk.value } });
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
controller.enqueue(chunk.value);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (chunk.done)
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
controller.error(error);
|
|
84
|
+
}
|
|
85
|
+
finally {
|
|
86
|
+
controller.close();
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
function onAgentResponseStreamEnd(stream, callback, options) {
|
|
92
|
+
return new ReadableStream({
|
|
93
|
+
async start(controller) {
|
|
94
|
+
try {
|
|
95
|
+
const json = {};
|
|
96
|
+
for await (const value of readableStreamToAsyncIterator(stream)) {
|
|
97
|
+
const chunk = options?.processChunk ? options.processChunk(value) : value;
|
|
98
|
+
controller.enqueue(chunk);
|
|
99
|
+
mergeAgentResponseChunk(json, value);
|
|
100
|
+
}
|
|
101
|
+
const result = await callback(json);
|
|
102
|
+
if (result && !(0, fast_deep_equal_1.default)(result, json)) {
|
|
103
|
+
let chunk = { delta: { json: result } };
|
|
104
|
+
if (options?.processChunk)
|
|
105
|
+
chunk = options.processChunk(chunk);
|
|
106
|
+
controller.enqueue(chunk);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
controller.error(options?.errorCallback?.(error) ?? error);
|
|
111
|
+
}
|
|
112
|
+
finally {
|
|
113
|
+
controller.close();
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
function isAsyncGenerator(value) {
|
|
119
|
+
return typeof value === "object" && value !== null && Symbol.asyncIterator in value;
|
|
120
|
+
}
|
|
121
|
+
async function* arrayToAgentProcessAsyncGenerator(chunks, result) {
|
|
122
|
+
for (const chunk of chunks) {
|
|
123
|
+
if (chunk instanceof Error)
|
|
124
|
+
throw chunk;
|
|
125
|
+
yield chunk;
|
|
126
|
+
}
|
|
127
|
+
if (result !== undefined)
|
|
128
|
+
return result;
|
|
129
|
+
}
|
|
130
|
+
function arrayToAgentResponseStream(chunks) {
|
|
131
|
+
return new ReadableStream({
|
|
132
|
+
start(controller) {
|
|
133
|
+
for (const chunk of chunks) {
|
|
134
|
+
if (chunk instanceof Error) {
|
|
135
|
+
controller.error(chunk);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
controller.enqueue(chunk);
|
|
139
|
+
}
|
|
140
|
+
controller.close();
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
async function readableStreamToArray(stream) {
|
|
145
|
+
const result = [];
|
|
146
|
+
for await (const value of readableStreamToAsyncIterator(stream)) {
|
|
147
|
+
result.push(value);
|
|
148
|
+
}
|
|
149
|
+
return result;
|
|
150
|
+
}
|
|
151
|
+
async function* readableStreamToAsyncIterator(stream) {
|
|
152
|
+
const reader = stream.getReader();
|
|
153
|
+
while (true) {
|
|
154
|
+
const { value, done } = await reader.read();
|
|
155
|
+
if (done)
|
|
156
|
+
break;
|
|
157
|
+
yield value;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
@@ -7,6 +7,7 @@ export declare function isEmpty(obj: unknown): boolean;
|
|
|
7
7
|
export declare function isNonNullable<T>(value: T): value is NonNullable<T>;
|
|
8
8
|
export declare function isNotEmpty<T>(arr: T[]): arr is [T, ...T[]];
|
|
9
9
|
export declare function duplicates<T>(arr: T[], key?: (item: T) => unknown): T[];
|
|
10
|
+
export declare function omitBy<T extends Record<string, unknown>, K extends keyof T>(obj: T, predicate: (value: T[K], key: K) => boolean): Partial<T>;
|
|
10
11
|
export declare function orArrayToArray<T>(value?: T | T[]): T[];
|
|
11
12
|
export declare function createAccessorArray<T>(array: T[], accessor: (array: T[], name: string) => T | undefined): T[] & {
|
|
12
13
|
[key: string]: T;
|
|
@@ -5,6 +5,7 @@ exports.isEmpty = isEmpty;
|
|
|
5
5
|
exports.isNonNullable = isNonNullable;
|
|
6
6
|
exports.isNotEmpty = isNotEmpty;
|
|
7
7
|
exports.duplicates = duplicates;
|
|
8
|
+
exports.omitBy = omitBy;
|
|
8
9
|
exports.orArrayToArray = orArrayToArray;
|
|
9
10
|
exports.createAccessorArray = createAccessorArray;
|
|
10
11
|
exports.checkArguments = checkArguments;
|
|
@@ -42,6 +43,12 @@ function duplicates(arr, key = (item) => item) {
|
|
|
42
43
|
}
|
|
43
44
|
return Array.from(duplicates);
|
|
44
45
|
}
|
|
46
|
+
function omitBy(obj, predicate) {
|
|
47
|
+
return Object.fromEntries(Object.entries(obj).filter(([key, value]) => {
|
|
48
|
+
const k = key;
|
|
49
|
+
return !predicate(value, k);
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
45
52
|
function orArrayToArray(value) {
|
|
46
53
|
if (isNil(value))
|
|
47
54
|
return [];
|
|
@@ -19,6 +19,9 @@ export interface AgentOptions<I extends Message = Message, O extends Message = M
|
|
|
19
19
|
disableEvents?: boolean;
|
|
20
20
|
memory?: AgentMemory | AgentMemoryOptions | true;
|
|
21
21
|
}
|
|
22
|
+
export interface AgentCallOptions {
|
|
23
|
+
streaming?: boolean;
|
|
24
|
+
}
|
|
22
25
|
export declare abstract class Agent<I extends Message = Message, O extends Message = Message> {
|
|
23
26
|
constructor({ inputSchema, outputSchema, ...options }: AgentOptions<I, O>);
|
|
24
27
|
readonly memory?: AgentMemory;
|
|
@@ -50,14 +53,37 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
50
53
|
get isCallable(): boolean;
|
|
51
54
|
private checkContextStatus;
|
|
52
55
|
private newDefaultContext;
|
|
53
|
-
call(input: I | string, context
|
|
56
|
+
call(input: I | string, context: Context | undefined, options: AgentCallOptions & {
|
|
57
|
+
streaming: true;
|
|
58
|
+
}): Promise<AgentResponseStream<O>>;
|
|
59
|
+
call(input: I | string, context?: Context, options?: AgentCallOptions & {
|
|
60
|
+
streaming?: false;
|
|
61
|
+
}): Promise<O>;
|
|
62
|
+
call(input: I | string, context?: Context, options?: AgentCallOptions): Promise<AgentResponse<O>>;
|
|
63
|
+
private processAgentOutput;
|
|
64
|
+
private processAgentError;
|
|
54
65
|
protected checkUsageAgentCalls(context: Context): void;
|
|
55
66
|
protected preprocess(_: I, context: Context): void;
|
|
56
67
|
protected postprocess(input: I, output: O, context: Context): void;
|
|
57
|
-
abstract process(input: I, context: Context):
|
|
68
|
+
abstract process(input: I, context: Context, options?: AgentCallOptions): AgentProcessResult<O | TransferAgentOutput>;
|
|
58
69
|
shutdown(): Promise<void>;
|
|
59
70
|
[inspect.custom](): string;
|
|
60
71
|
}
|
|
72
|
+
export type AgentResponse<T> = T | AgentResponseStream<T>;
|
|
73
|
+
export type AgentResponseStream<T> = ReadableStream<AgentResponseChunk<T>>;
|
|
74
|
+
export type AgentResponseChunk<T> = AgentResponseDelta<T>;
|
|
75
|
+
export interface AgentResponseDelta<T> {
|
|
76
|
+
delta: {
|
|
77
|
+
text?: Partial<{
|
|
78
|
+
[key in keyof T as Extract<T[key], string> extends string ? key : never]: string;
|
|
79
|
+
}> | {
|
|
80
|
+
[key: string]: string;
|
|
81
|
+
};
|
|
82
|
+
json?: Partial<T>;
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
export type AgentProcessAsyncGenerator<O extends Message> = AsyncGenerator<AgentResponseChunk<O>, Partial<O> | undefined | void>;
|
|
86
|
+
export type AgentProcessResult<O extends Message> = Promise<AgentResponse<O>> | AgentProcessAsyncGenerator<O>;
|
|
61
87
|
export type AgentInputOutputSchema<I extends Message = Message> = ZodType<I> | ((agent: Agent) => ZodType<I>);
|
|
62
88
|
export interface FunctionAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
|
|
63
89
|
fn?: FunctionAgentFn<I, O>;
|
|
@@ -66,6 +92,6 @@ export declare class FunctionAgent<I extends Message = Message, O extends Messag
|
|
|
66
92
|
static from<I extends Message, O extends Message>(options: FunctionAgentOptions<I, O> | FunctionAgentFn<I, O>): FunctionAgent<I, O>;
|
|
67
93
|
constructor(options: FunctionAgentOptions<I, O>);
|
|
68
94
|
fn: FunctionAgentFn<I, O>;
|
|
69
|
-
process(input: I, context: Context): Promise<
|
|
95
|
+
process(input: I, context: Context, options?: AgentCallOptions): Promise<AgentResponse<O | TransferAgentOutput>>;
|
|
70
96
|
}
|
|
71
97
|
export type FunctionAgentFn<I extends Message = Message, O extends Message = Message> = (input: I, context: Context) => O | Promise<O> | Agent | Promise<Agent>;
|
|
@@ -2,7 +2,8 @@ import { z } from "zod";
|
|
|
2
2
|
import type { Context } from "../execution-engine/context.js";
|
|
3
3
|
import { ChatModel } from "../models/chat-model.js";
|
|
4
4
|
import { PromptBuilder } from "../prompt/prompt-builder.js";
|
|
5
|
-
import { Agent, type AgentOptions, type Message } from "./agent.js";
|
|
5
|
+
import { Agent, type AgentOptions, type AgentProcessAsyncGenerator, type Message } from "./agent.js";
|
|
6
|
+
import { type TransferAgentOutput } from "./types.js";
|
|
6
7
|
export interface AIAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
|
|
7
8
|
model?: ChatModel;
|
|
8
9
|
instructions?: string | PromptBuilder;
|
|
@@ -64,5 +65,5 @@ export declare class AIAgent<I extends Message = Message, O extends Message = Me
|
|
|
64
65
|
instructions: PromptBuilder;
|
|
65
66
|
outputKey?: string;
|
|
66
67
|
toolChoice?: AIAgentToolChoice;
|
|
67
|
-
process(input: I, context: Context):
|
|
68
|
+
process(input: I, context: Context): AgentProcessAsyncGenerator<O | TransferAgentOutput>;
|
|
68
69
|
}
|
|
@@ -2,7 +2,7 @@ import { ReadableStream } from "node:stream/web";
|
|
|
2
2
|
import { type Context, type Runnable } from "../execution-engine/context.js";
|
|
3
3
|
import type { MessagePayload } from "../execution-engine/message-queue.js";
|
|
4
4
|
import { type PromiseOrValue } from "../utils/type-utils.js";
|
|
5
|
-
import { Agent, type AgentOptions, type Message } from "./agent.js";
|
|
5
|
+
import { Agent, type AgentOptions, type AgentProcessAsyncGenerator, type Message } from "./agent.js";
|
|
6
6
|
export interface UserAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
|
|
7
7
|
context: Context;
|
|
8
8
|
process?: (input: I, context: Context) => PromiseOrValue<O>;
|
|
@@ -14,8 +14,8 @@ export declare class UserAgent<I extends Message = Message, O extends Message =
|
|
|
14
14
|
context: Context;
|
|
15
15
|
private _process?;
|
|
16
16
|
private activeAgent?;
|
|
17
|
-
call
|
|
18
|
-
process(input: I, context: Context):
|
|
17
|
+
call: Agent<I, O>["call"];
|
|
18
|
+
process(input: I, context: Context): AgentProcessAsyncGenerator<O>;
|
|
19
19
|
publish: Context["publish"];
|
|
20
20
|
subscribe: Context["subscribe"];
|
|
21
21
|
unsubscribe: Context["unsubscribe"];
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import EventEmitter from "node:events";
|
|
2
|
-
import { Agent, type FunctionAgentFn, type Message } from "../agents/agent.js";
|
|
2
|
+
import { Agent, type AgentCallOptions, type AgentProcessAsyncGenerator, type AgentResponse, type AgentResponseStream, type FunctionAgentFn, type Message } from "../agents/agent.js";
|
|
3
3
|
import { UserAgent } from "../agents/user-agent.js";
|
|
4
4
|
import type { ChatModel } from "../models/chat-model.js";
|
|
5
5
|
import { type OmitPropertiesFromArrayFirstElement } from "../utils/type-utils.js";
|
|
@@ -27,7 +27,7 @@ export interface ContextEventMap {
|
|
|
27
27
|
export type ContextEmitEventMap = {
|
|
28
28
|
[K in keyof ContextEventMap]: OmitPropertiesFromArrayFirstElement<ContextEventMap[K], "contextId" | "parentContextId" | "timestamp">;
|
|
29
29
|
};
|
|
30
|
-
export interface CallOptions {
|
|
30
|
+
export interface CallOptions extends AgentCallOptions {
|
|
31
31
|
returnActiveAgent?: boolean;
|
|
32
32
|
disableTransfer?: boolean;
|
|
33
33
|
}
|
|
@@ -48,19 +48,30 @@ export interface Context extends TypedEventEmitter<ContextEventMap, ContextEmitE
|
|
|
48
48
|
* @param agent Agent to call
|
|
49
49
|
* @param message Message to pass to the agent
|
|
50
50
|
* @param options.returnActiveAgent return the active agent
|
|
51
|
+
* @param options.streaming return a stream of the output
|
|
51
52
|
* @returns the output of the agent and the final active agent
|
|
52
53
|
*/
|
|
53
54
|
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options: CallOptions & {
|
|
54
55
|
returnActiveAgent: true;
|
|
56
|
+
streaming?: false;
|
|
55
57
|
}): Promise<[O, Runnable]>;
|
|
58
|
+
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options: CallOptions & {
|
|
59
|
+
returnActiveAgent: true;
|
|
60
|
+
streaming: true;
|
|
61
|
+
}): Promise<[AgentResponseStream<O>, Promise<Runnable>]>;
|
|
56
62
|
/**
|
|
57
63
|
* Call an agent with a message
|
|
58
64
|
* @param agent Agent to call
|
|
59
65
|
* @param message Message to pass to the agent
|
|
60
66
|
* @returns the output of the agent
|
|
61
67
|
*/
|
|
62
|
-
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options?: CallOptions
|
|
63
|
-
|
|
68
|
+
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options?: CallOptions & {
|
|
69
|
+
streaming?: false;
|
|
70
|
+
}): Promise<O>;
|
|
71
|
+
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options: CallOptions & {
|
|
72
|
+
streaming: true;
|
|
73
|
+
}): Promise<AgentResponseStream<O>>;
|
|
74
|
+
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message?: I | string, options?: CallOptions): UserAgent<I, O> | Promise<AgentResponse<O> | [AgentResponse<O>, Runnable]>;
|
|
64
75
|
/**
|
|
65
76
|
* Publish a message to a topic, the engine will call the listeners of the topic
|
|
66
77
|
* @param topic topic name, or an array of topic names
|
|
@@ -99,6 +110,7 @@ export declare class ExecutionContext implements Context {
|
|
|
99
110
|
reset?: boolean;
|
|
100
111
|
}): ExecutionContext;
|
|
101
112
|
call: Context["call"];
|
|
113
|
+
private onCallSuccess;
|
|
102
114
|
publish: Context["publish"];
|
|
103
115
|
subscribe: Context["subscribe"];
|
|
104
116
|
unsubscribe: Context["unsubscribe"];
|
|
@@ -122,9 +134,8 @@ declare class ExecutionContextInternal {
|
|
|
122
134
|
private timer?;
|
|
123
135
|
private initTimeout;
|
|
124
136
|
get status(): "normal" | "timeout";
|
|
125
|
-
call<I extends Message, O extends Message>(agent: Runnable<I, O>, input: I, context: Context, options?: CallOptions):
|
|
126
|
-
|
|
127
|
-
output: O;
|
|
137
|
+
call<I extends Message, O extends Message>(agent: Runnable<I, O>, input: I, context: Context, options?: CallOptions): AgentProcessAsyncGenerator<O & {
|
|
138
|
+
__activeAgent__: Runnable;
|
|
128
139
|
}>;
|
|
129
140
|
private callAgent;
|
|
130
141
|
}
|
|
@@ -2,6 +2,10 @@ import { Agent, type Message } from "../agents/agent.js";
|
|
|
2
2
|
import type { Context } from "../execution-engine/context.js";
|
|
3
3
|
export declare abstract class ChatModel extends Agent<ChatModelInput, ChatModelOutput> {
|
|
4
4
|
constructor();
|
|
5
|
+
protected supportsParallelToolCalls: boolean;
|
|
6
|
+
getModelCapabilities(): {
|
|
7
|
+
supportsParallelToolCalls: boolean;
|
|
8
|
+
};
|
|
5
9
|
protected preprocess(input: ChatModelInput, context: Context): void;
|
|
6
10
|
protected postprocess(input: ChatModelInput, output: ChatModelOutput, context: Context): void;
|
|
7
11
|
}
|
|
@@ -2,4 +2,5 @@ import { OpenAIChatModel, type OpenAIChatModelOptions } from "./openai-chat-mode
|
|
|
2
2
|
export declare class OpenRouterChatModel extends OpenAIChatModel {
|
|
3
3
|
constructor(options?: OpenAIChatModelOptions);
|
|
4
4
|
protected apiKeyEnvName: string;
|
|
5
|
+
protected supportsParallelToolCalls: boolean;
|
|
5
6
|
}
|
|
@@ -1,8 +1,17 @@
|
|
|
1
1
|
import OpenAI from "openai";
|
|
2
2
|
import type { ChatCompletionMessageParam, ChatCompletionTool } from "openai/resources";
|
|
3
|
-
import type { Stream } from "openai/streaming.js";
|
|
4
3
|
import { z } from "zod";
|
|
4
|
+
import type { AgentCallOptions, AgentResponse } from "../agents/agent.js";
|
|
5
|
+
import type { Context } from "../execution-engine/context.js";
|
|
5
6
|
import { ChatModel, type ChatModelInput, type ChatModelInputMessage, type ChatModelInputTool, type ChatModelOptions, type ChatModelOutput, type Role } from "./chat-model.js";
|
|
7
|
+
export interface OpenAIChatModelCapabilities {
|
|
8
|
+
supportsNativeStructuredOutputs: boolean;
|
|
9
|
+
supportsEndWithSystemMessage: boolean;
|
|
10
|
+
supportsToolsUseWithJsonSchema: boolean;
|
|
11
|
+
supportsParallelToolCalls: boolean;
|
|
12
|
+
supportsToolsEmptyParameters: boolean;
|
|
13
|
+
supportsTemperature: boolean;
|
|
14
|
+
}
|
|
6
15
|
export interface OpenAIChatModelOptions {
|
|
7
16
|
apiKey?: string;
|
|
8
17
|
baseURL?: string;
|
|
@@ -71,9 +80,10 @@ export declare class OpenAIChatModel extends ChatModel {
|
|
|
71
80
|
protected supportsToolsUseWithJsonSchema: boolean;
|
|
72
81
|
protected supportsParallelToolCalls: boolean;
|
|
73
82
|
protected supportsToolsEmptyParameters: boolean;
|
|
83
|
+
protected supportsTemperature: boolean;
|
|
74
84
|
get client(): OpenAI;
|
|
75
85
|
get modelOptions(): ChatModelOptions | undefined;
|
|
76
|
-
process(input: ChatModelInput): Promise<ChatModelOutput
|
|
86
|
+
process(input: ChatModelInput, _context: Context, options?: AgentCallOptions): Promise<AgentResponse<ChatModelOutput>>;
|
|
77
87
|
private getParallelToolCalls;
|
|
78
88
|
private getRunMessages;
|
|
79
89
|
private getRunResponseFormat;
|
|
@@ -87,4 +97,3 @@ export declare function toolsFromInputTools(tools?: ChatModelInputTool[], option
|
|
|
87
97
|
addTypeToEmptyParameters?: boolean;
|
|
88
98
|
}): ChatCompletionTool[] | undefined;
|
|
89
99
|
export declare function jsonSchemaToOpenAIJsonSchema(schema: Record<string, unknown>): Record<string, unknown>;
|
|
90
|
-
export declare function extractResultFromStream(stream: Stream<OpenAI.Chat.Completions.ChatCompletionChunk>, jsonMode?: boolean): Promise<ChatModelOutput>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { AgentProcessAsyncGenerator, AgentResponseChunk, AgentResponseStream, Message } from "../agents/agent.js";
|
|
2
|
+
import { type PromiseOrValue } from "./type-utils.js";
|
|
3
|
+
export declare function objectToAgentResponseStream<T extends Message>(json: T): AgentResponseStream<T>;
|
|
4
|
+
export declare function mergeAgentResponseChunk<T extends Message>(output: T, chunk: AgentResponseChunk<T>): T;
|
|
5
|
+
export declare function agentResponseStreamToObject<T extends Message>(stream: AgentResponseStream<T> | AgentProcessAsyncGenerator<T>): Promise<T>;
|
|
6
|
+
export declare function asyncGeneratorToReadableStream<T extends Message>(generator: AgentProcessAsyncGenerator<T>): AgentResponseStream<T>;
|
|
7
|
+
export declare function onAgentResponseStreamEnd<T extends Message>(stream: AgentResponseStream<T>, callback: (result: T) => PromiseOrValue<Partial<T> | void>, options?: {
|
|
8
|
+
errorCallback?: (error: Error) => Error;
|
|
9
|
+
processChunk?: (chunk: AgentResponseChunk<T>) => AgentResponseChunk<T>;
|
|
10
|
+
}): ReadableStream<any>;
|
|
11
|
+
export declare function isAsyncGenerator<T extends AsyncGenerator>(value: AsyncGenerator | unknown): value is T;
|
|
12
|
+
export declare function arrayToAgentProcessAsyncGenerator<T extends Message>(chunks: (AgentResponseChunk<T> | Error)[], result?: Partial<T>): AgentProcessAsyncGenerator<T>;
|
|
13
|
+
export declare function arrayToAgentResponseStream<T>(chunks: (AgentResponseChunk<T> | Error)[]): AgentResponseStream<T>;
|
|
14
|
+
export declare function readableStreamToArray<T>(stream: ReadableStream<T>): Promise<T[]>;
|
|
15
|
+
export declare function readableStreamToAsyncIterator<T>(stream: ReadableStream<T>): AsyncIterable<T>;
|