@aigne/core 1.8.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.
Files changed (76) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/lib/cjs/agents/agent.d.ts +29 -3
  3. package/lib/cjs/agents/agent.js +48 -23
  4. package/lib/cjs/agents/ai-agent.d.ts +3 -2
  5. package/lib/cjs/agents/ai-agent.js +22 -9
  6. package/lib/cjs/agents/user-agent.d.ts +3 -3
  7. package/lib/cjs/agents/user-agent.js +14 -8
  8. package/lib/cjs/execution-engine/context.d.ts +18 -7
  9. package/lib/cjs/execution-engine/context.js +76 -28
  10. package/lib/cjs/loader/index.js +20 -4
  11. package/lib/cjs/models/chat-model.d.ts +4 -0
  12. package/lib/cjs/models/chat-model.js +6 -0
  13. package/lib/cjs/models/deepseek-chat-model.d.ts +7 -0
  14. package/lib/cjs/models/deepseek-chat-model.js +19 -0
  15. package/lib/cjs/models/gemini-chat-model.d.ts +8 -0
  16. package/lib/cjs/models/gemini-chat-model.js +20 -0
  17. package/lib/cjs/models/ollama-chat-model.d.ts +6 -0
  18. package/lib/cjs/models/ollama-chat-model.js +18 -0
  19. package/lib/cjs/models/open-router-chat-model.d.ts +6 -0
  20. package/lib/cjs/models/open-router-chat-model.js +18 -0
  21. package/lib/cjs/models/openai-chat-model.d.ts +33 -2
  22. package/lib/cjs/models/openai-chat-model.js +250 -80
  23. package/lib/cjs/models/xai-chat-model.d.ts +3 -11
  24. package/lib/cjs/models/xai-chat-model.js +1 -14
  25. package/lib/cjs/prompt/prompt-builder.js +3 -0
  26. package/lib/cjs/utils/prompts.d.ts +1 -0
  27. package/lib/cjs/utils/prompts.js +13 -0
  28. package/lib/cjs/utils/stream-utils.d.ts +15 -0
  29. package/lib/cjs/utils/stream-utils.js +159 -0
  30. package/lib/cjs/utils/type-utils.d.ts +2 -1
  31. package/lib/cjs/utils/type-utils.js +8 -1
  32. package/lib/dts/agents/agent.d.ts +29 -3
  33. package/lib/dts/agents/ai-agent.d.ts +3 -2
  34. package/lib/dts/agents/user-agent.d.ts +3 -3
  35. package/lib/dts/execution-engine/context.d.ts +18 -7
  36. package/lib/dts/models/chat-model.d.ts +4 -0
  37. package/lib/dts/models/deepseek-chat-model.d.ts +7 -0
  38. package/lib/dts/models/gemini-chat-model.d.ts +8 -0
  39. package/lib/dts/models/ollama-chat-model.d.ts +6 -0
  40. package/lib/dts/models/open-router-chat-model.d.ts +6 -0
  41. package/lib/dts/models/openai-chat-model.d.ts +33 -2
  42. package/lib/dts/models/xai-chat-model.d.ts +3 -11
  43. package/lib/dts/utils/prompts.d.ts +1 -0
  44. package/lib/dts/utils/stream-utils.d.ts +15 -0
  45. package/lib/dts/utils/type-utils.d.ts +2 -1
  46. package/lib/esm/agents/agent.d.ts +29 -3
  47. package/lib/esm/agents/agent.js +49 -24
  48. package/lib/esm/agents/ai-agent.d.ts +3 -2
  49. package/lib/esm/agents/ai-agent.js +23 -10
  50. package/lib/esm/agents/user-agent.d.ts +3 -3
  51. package/lib/esm/agents/user-agent.js +15 -9
  52. package/lib/esm/execution-engine/context.d.ts +18 -7
  53. package/lib/esm/execution-engine/context.js +78 -30
  54. package/lib/esm/loader/index.js +20 -4
  55. package/lib/esm/models/chat-model.d.ts +4 -0
  56. package/lib/esm/models/chat-model.js +6 -0
  57. package/lib/esm/models/deepseek-chat-model.d.ts +7 -0
  58. package/lib/esm/models/deepseek-chat-model.js +15 -0
  59. package/lib/esm/models/gemini-chat-model.d.ts +8 -0
  60. package/lib/esm/models/gemini-chat-model.js +16 -0
  61. package/lib/esm/models/ollama-chat-model.d.ts +6 -0
  62. package/lib/esm/models/ollama-chat-model.js +14 -0
  63. package/lib/esm/models/open-router-chat-model.d.ts +6 -0
  64. package/lib/esm/models/open-router-chat-model.js +14 -0
  65. package/lib/esm/models/openai-chat-model.d.ts +33 -2
  66. package/lib/esm/models/openai-chat-model.js +247 -80
  67. package/lib/esm/models/xai-chat-model.d.ts +3 -11
  68. package/lib/esm/models/xai-chat-model.js +1 -11
  69. package/lib/esm/prompt/prompt-builder.js +3 -0
  70. package/lib/esm/utils/prompts.d.ts +1 -0
  71. package/lib/esm/utils/prompts.js +10 -0
  72. package/lib/esm/utils/stream-utils.d.ts +15 -0
  73. package/lib/esm/utils/stream-utils.js +144 -0
  74. package/lib/esm/utils/type-utils.d.ts +2 -1
  75. package/lib/esm/utils/type-utils.js +7 -1
  76. package/package.json +2 -1
@@ -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,9 +7,10 @@ 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;
13
14
  };
14
- export declare function checkArguments<T>(prefix: string, schema: ZodType<T>, args: T): void;
15
+ export declare function checkArguments<T>(prefix: string, schema: ZodType<T>, args: T): T;
15
16
  export declare function tryOrThrow<P extends PromiseOrValue<unknown>>(fn: () => P, error: string | Error | ((error: Error) => Error)): P;
@@ -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 [];
@@ -54,7 +61,7 @@ function createAccessorArray(array, accessor) {
54
61
  }
55
62
  function checkArguments(prefix, schema, args) {
56
63
  try {
57
- schema.parse(args, {
64
+ return schema.parse(args, {
58
65
  errorMap: (issue, ctx) => {
59
66
  if (issue.code === "invalid_union") {
60
67
  // handle all issues that are not invalid_type
@@ -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?: Context): Promise<O>;
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): Promise<O | TransferAgentOutput>;
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<TransferAgentOutput | O>;
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): Promise<import("./types.js").TransferAgentOutput | O>;
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(input: string | I, context?: Context): Promise<O>;
18
- process(input: I, context: Context): Promise<O>;
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): Promise<O>;
63
- call<I extends Message, O extends Message>(agent: Runnable<I, O>, message?: I | string, options?: CallOptions): UserAgent<I, O> | Promise<O | [O, Runnable]>;
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): Promise<{
126
- agent: Runnable;
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
  }
@@ -0,0 +1,7 @@
1
+ import { OpenAIChatModel, type OpenAIChatModelOptions } from "./openai-chat-model.js";
2
+ export declare class DeepSeekChatModel extends OpenAIChatModel {
3
+ constructor(options?: OpenAIChatModelOptions);
4
+ protected apiKeyEnvName: string;
5
+ protected supportsNativeStructuredOutputs: boolean;
6
+ protected supportsToolsEmptyParameters: boolean;
7
+ }
@@ -0,0 +1,8 @@
1
+ import { OpenAIChatModel, type OpenAIChatModelOptions } from "./openai-chat-model.js";
2
+ export declare class GeminiChatModel extends OpenAIChatModel {
3
+ constructor(options?: OpenAIChatModelOptions);
4
+ protected apiKeyEnvName: string;
5
+ protected supportsEndWithSystemMessage: boolean;
6
+ protected supportsToolsUseWithJsonSchema: boolean;
7
+ protected supportsParallelToolCalls: boolean;
8
+ }
@@ -0,0 +1,6 @@
1
+ import { OpenAIChatModel, type OpenAIChatModelOptions } from "./openai-chat-model.js";
2
+ export declare class OllamaChatModel extends OpenAIChatModel {
3
+ constructor(options?: OpenAIChatModelOptions);
4
+ protected apiKeyEnvName: string;
5
+ protected apiKeyDefault: string;
6
+ }
@@ -0,0 +1,6 @@
1
+ import { OpenAIChatModel, type OpenAIChatModelOptions } from "./openai-chat-model.js";
2
+ export declare class OpenRouterChatModel extends OpenAIChatModel {
3
+ constructor(options?: OpenAIChatModelOptions);
4
+ protected apiKeyEnvName: string;
5
+ protected supportsParallelToolCalls: boolean;
6
+ }
@@ -1,6 +1,17 @@
1
1
  import OpenAI from "openai";
2
+ import type { ChatCompletionMessageParam, ChatCompletionTool } from "openai/resources";
2
3
  import { z } from "zod";
3
- import { ChatModel, type ChatModelInput, type ChatModelOptions, type ChatModelOutput } from "./chat-model.js";
4
+ import type { AgentCallOptions, AgentResponse } from "../agents/agent.js";
5
+ import type { Context } from "../execution-engine/context.js";
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
+ }
4
15
  export interface OpenAIChatModelOptions {
5
16
  apiKey?: string;
6
17
  baseURL?: string;
@@ -62,7 +73,27 @@ export declare class OpenAIChatModel extends ChatModel {
62
73
  options?: OpenAIChatModelOptions | undefined;
63
74
  constructor(options?: OpenAIChatModelOptions | undefined);
64
75
  protected _client?: OpenAI;
76
+ protected apiKeyEnvName: string;
77
+ protected apiKeyDefault: string | undefined;
78
+ protected supportsNativeStructuredOutputs: boolean;
79
+ protected supportsEndWithSystemMessage: boolean;
80
+ protected supportsToolsUseWithJsonSchema: boolean;
81
+ protected supportsParallelToolCalls: boolean;
82
+ protected supportsToolsEmptyParameters: boolean;
83
+ protected supportsTemperature: boolean;
65
84
  get client(): OpenAI;
66
85
  get modelOptions(): ChatModelOptions | undefined;
67
- process(input: ChatModelInput): Promise<ChatModelOutput>;
86
+ process(input: ChatModelInput, _context: Context, options?: AgentCallOptions): Promise<AgentResponse<ChatModelOutput>>;
87
+ private getParallelToolCalls;
88
+ private getRunMessages;
89
+ private getRunResponseFormat;
90
+ private requestStructuredOutput;
68
91
  }
92
+ export declare const ROLE_MAP: {
93
+ [key in Role]: ChatCompletionMessageParam["role"];
94
+ };
95
+ export declare function contentsFromInputMessages(messages: ChatModelInputMessage[]): Promise<ChatCompletionMessageParam[]>;
96
+ export declare function toolsFromInputTools(tools?: ChatModelInputTool[], options?: {
97
+ addTypeToEmptyParameters?: boolean;
98
+ }): ChatCompletionTool[] | undefined;
99
+ export declare function jsonSchemaToOpenAIJsonSchema(schema: Record<string, unknown>): Record<string, unknown>;
@@ -1,13 +1,5 @@
1
- import OpenAI from "openai";
2
- import type { ChatModelOptions } from "./chat-model.js";
3
- import { OpenAIChatModel } from "./openai-chat-model.js";
4
- export interface XAIChatModelOptions {
5
- apiKey?: string;
6
- model?: string;
7
- modelOptions?: ChatModelOptions;
8
- baseURL?: string;
9
- }
1
+ import { OpenAIChatModel, type OpenAIChatModelOptions } from "./openai-chat-model.js";
10
2
  export declare class XAIChatModel extends OpenAIChatModel {
11
- constructor(options?: XAIChatModelOptions);
12
- get client(): OpenAI;
3
+ constructor(options?: OpenAIChatModelOptions);
4
+ protected apiKeyEnvName: string;
13
5
  }
@@ -0,0 +1 @@
1
+ export declare function getJsonOutputPrompt(schema: Record<string, unknown> | string): string;
@@ -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>;
@@ -7,9 +7,10 @@ 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;
13
14
  };
14
- export declare function checkArguments<T>(prefix: string, schema: ZodType<T>, args: T): void;
15
+ export declare function checkArguments<T>(prefix: string, schema: ZodType<T>, args: T): T;
15
16
  export declare function tryOrThrow<P extends PromiseOrValue<unknown>>(fn: () => P, error: string | Error | ((error: Error) => Error)): P;
@@ -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?: Context): Promise<O>;
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): Promise<O | TransferAgentOutput>;
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<TransferAgentOutput | O>;
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>;