@node-llm/core 1.11.0 → 1.12.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/dist/agent/Agent.d.ts +82 -95
- package/dist/agent/Agent.d.ts.map +1 -1
- package/dist/agent/Agent.js +118 -97
- package/dist/aliases.d.ts +130 -0
- package/dist/aliases.d.ts.map +1 -1
- package/dist/aliases.js +134 -4
- package/dist/chat/Chat.d.ts +1 -0
- package/dist/chat/Chat.d.ts.map +1 -1
- package/dist/models/models.json +2519 -596
- package/package.json +1 -1
package/dist/agent/Agent.d.ts
CHANGED
|
@@ -7,17 +7,21 @@ import { ThinkingConfig, ThinkingResult } from "../providers/Provider.js";
|
|
|
7
7
|
import { Schema } from "../schema/Schema.js";
|
|
8
8
|
import { NodeLLMCore } from "../llm.js";
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
10
|
+
* A value that can be a static T or a function that returns T based on inputs.
|
|
11
11
|
*/
|
|
12
|
-
export
|
|
13
|
-
|
|
12
|
+
export type LazyValue<T, I = any> = T | ((inputs: I) => T);
|
|
13
|
+
/**
|
|
14
|
+
* Configuration options for Agent.
|
|
15
|
+
*/
|
|
16
|
+
export interface AgentConfig<I = any> {
|
|
17
|
+
/** The model ID to use (e.g., "gpt-4o") */
|
|
14
18
|
model?: string;
|
|
15
|
-
/** The provider to use (e.g., "openai"
|
|
19
|
+
/** The provider to use (e.g., "openai") */
|
|
16
20
|
provider?: string;
|
|
17
|
-
/** System instructions for the agent */
|
|
18
|
-
instructions?: string
|
|
19
|
-
/** Tools available to the agent */
|
|
20
|
-
tools?: ToolResolvable[]
|
|
21
|
+
/** System instructions for the agent (can be lazy) */
|
|
22
|
+
instructions?: LazyValue<string, I>;
|
|
23
|
+
/** Tools available to the agent (can be lazy) */
|
|
24
|
+
tools?: LazyValue<ToolResolvable[], I>;
|
|
21
25
|
/** Temperature for response generation (0.0 - 1.0) */
|
|
22
26
|
temperature?: number;
|
|
23
27
|
/** Extended thinking configuration */
|
|
@@ -36,36 +40,17 @@ export interface AgentConfig {
|
|
|
36
40
|
assumeModelExists?: boolean;
|
|
37
41
|
/** Optional LLM instance to use instead of global NodeLLM */
|
|
38
42
|
llm?: NodeLLMCore;
|
|
43
|
+
/** Optional initial inputs to resolve lazy config immediately */
|
|
44
|
+
inputs?: I;
|
|
39
45
|
}
|
|
40
46
|
/**
|
|
41
47
|
* Base class for creating reusable, class-configured agents.
|
|
42
|
-
*
|
|
43
|
-
* Define your agent configuration using static properties, then instantiate
|
|
44
|
-
* and use it anywhere in your application.
|
|
45
|
-
*
|
|
46
|
-
* @example
|
|
47
|
-
* ```typescript
|
|
48
|
-
* class SupportAgent extends Agent {
|
|
49
|
-
* static model = "gpt-4o";
|
|
50
|
-
* static instructions = "You are a helpful support agent";
|
|
51
|
-
* static tools = [SearchDocs, LookupAccount];
|
|
52
|
-
* static temperature = 0.2;
|
|
53
|
-
* }
|
|
54
|
-
*
|
|
55
|
-
* const agent = new SupportAgent();
|
|
56
|
-
* const response = await agent.ask("How can I reset my password?");
|
|
57
|
-
* ```
|
|
58
|
-
*
|
|
59
|
-
* @example Override configuration per instance:
|
|
60
|
-
* ```typescript
|
|
61
|
-
* const agent = new SupportAgent({ model: "gpt-4o-mini" });
|
|
62
|
-
* ```
|
|
63
48
|
*/
|
|
64
|
-
export declare abstract class Agent<S extends Record<string, unknown> = Record<string, unknown>> {
|
|
49
|
+
export declare abstract class Agent<I extends Record<string, any> = Record<string, any>, S extends Record<string, unknown> = Record<string, unknown>> {
|
|
65
50
|
static model?: string;
|
|
66
51
|
static provider?: string;
|
|
67
|
-
static instructions?: string
|
|
68
|
-
static tools?: ToolResolvable[]
|
|
52
|
+
static instructions?: LazyValue<string, any>;
|
|
53
|
+
static tools?: LazyValue<ToolResolvable[], any>;
|
|
69
54
|
static temperature?: number;
|
|
70
55
|
static thinking?: ThinkingConfig;
|
|
71
56
|
static schema?: z.ZodType | Schema | Record<string, unknown>;
|
|
@@ -74,86 +59,101 @@ export declare abstract class Agent<S extends Record<string, unknown> = Record<s
|
|
|
74
59
|
static maxTokens?: number;
|
|
75
60
|
static maxToolCalls?: number;
|
|
76
61
|
static assumeModelExists?: boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Explicitly declare which inputs this agent expects.
|
|
64
|
+
* Useful for introspection and validation.
|
|
65
|
+
*/
|
|
66
|
+
static inputs?: string[];
|
|
77
67
|
/**
|
|
78
68
|
* Hook called when the agent starts a new session (ask/stream).
|
|
79
|
-
* @param context - Initial context including messages/options
|
|
80
69
|
*/
|
|
81
70
|
static onStart(_context: {
|
|
82
71
|
messages: unknown[];
|
|
83
72
|
}): void | Promise<void>;
|
|
84
|
-
/**
|
|
85
|
-
* Hook called when the agent generates a reasoning trace (thinking).
|
|
86
|
-
* @param thinking - The content of the thinking trace
|
|
87
|
-
* @param result - The full response object containing the thinking
|
|
88
|
-
*/
|
|
89
73
|
static onThinking(_thinking: ThinkingResult, _result: ChatResponseString): void | Promise<void>;
|
|
90
|
-
/**
|
|
91
|
-
* Hook called when a tool execution starts.
|
|
92
|
-
* @param toolCall - The tool call object (id, function name, arguments)
|
|
93
|
-
*/
|
|
94
74
|
static onToolStart(_toolCall: unknown): void | Promise<void>;
|
|
95
|
-
/**
|
|
96
|
-
* Hook called when a tool execution ends.
|
|
97
|
-
* @param toolCall - The tool call object
|
|
98
|
-
* @param result - The result of the tool execution
|
|
99
|
-
*/
|
|
100
75
|
static onToolEnd(_toolCall: unknown, _result: unknown): void | Promise<void>;
|
|
101
|
-
/**
|
|
102
|
-
* Hook called when a tool execution encounters an error.
|
|
103
|
-
* @param toolCall - The tool call object
|
|
104
|
-
* @param error - The error that occurred
|
|
105
|
-
*/
|
|
106
76
|
static onToolError(_toolCall: unknown, _error: Error): void | Promise<void>;
|
|
107
|
-
/**
|
|
108
|
-
* Hook called when the agent completes a response turn.
|
|
109
|
-
* @param result - The final response object
|
|
110
|
-
*/
|
|
111
77
|
static onComplete(_result: ChatResponseString): void | Promise<void>;
|
|
112
78
|
/**
|
|
113
79
|
* Run the agent immediately with a prompt.
|
|
114
|
-
* Creates a new instance of the agent, runs the prompt, and disposes it.
|
|
115
|
-
*
|
|
116
|
-
* @example
|
|
117
|
-
* ```typescript
|
|
118
|
-
* const result = await TravelAgent.ask("Find flights to Paris");
|
|
119
|
-
* ```
|
|
120
80
|
*/
|
|
121
|
-
static ask(message: string, options?: AskOptions
|
|
81
|
+
static ask<I extends Record<string, any>, S extends Record<string, any>>(this: new (overrides?: Partial<AgentConfig<I> & ChatOptions>) => Agent<I, S>, message: string, options?: AskOptions & {
|
|
82
|
+
inputs?: I;
|
|
83
|
+
}): Promise<ChatResponseString>;
|
|
122
84
|
/**
|
|
123
85
|
* Stream the agent response immediately.
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
* for await (const chunk of TravelAgent.stream("Write a poem")) {
|
|
129
|
-
* process.stdout.write(chunk.content);
|
|
130
|
-
* }
|
|
131
|
-
* ```
|
|
132
|
-
*/
|
|
133
|
-
static stream(message: string, options?: AskOptions): import("../index.js").Stream<import("../providers/Provider.js").ChatChunk>;
|
|
86
|
+
*/
|
|
87
|
+
static stream<I extends Record<string, any>, S extends Record<string, any>>(this: new (overrides?: Partial<AgentConfig<I> & ChatOptions>) => Agent<I, S>, message: string, options?: AskOptions & {
|
|
88
|
+
inputs?: I;
|
|
89
|
+
}): import("../index.js").Stream<import("../providers/Provider.js").ChatChunk>;
|
|
134
90
|
/** The underlying Chat instance */
|
|
135
91
|
protected readonly chat: Chat<S>;
|
|
92
|
+
/** Private reference to configuration overrides for lazy resolution */
|
|
93
|
+
private readonly config;
|
|
136
94
|
/**
|
|
137
95
|
* Create a new agent instance.
|
|
138
96
|
* @param overrides - Optional configuration to override static properties
|
|
139
97
|
*/
|
|
140
|
-
constructor(overrides?: Partial<AgentConfig & ChatOptions>);
|
|
98
|
+
constructor(overrides?: Partial<AgentConfig<I> & ChatOptions>);
|
|
99
|
+
/**
|
|
100
|
+
* Helper to resolve lazy instructions and tools based on inputs.
|
|
101
|
+
*/
|
|
102
|
+
private resolveLazyConfig;
|
|
103
|
+
/**
|
|
104
|
+
* Add instructions to the agent (replaces or appends).
|
|
105
|
+
*/
|
|
106
|
+
withInstructions(instructions: string, options?: {
|
|
107
|
+
replace?: boolean;
|
|
108
|
+
}): this;
|
|
109
|
+
/**
|
|
110
|
+
* Add tools to the agent.
|
|
111
|
+
*/
|
|
112
|
+
withTools(tools: ToolResolvable[], options?: {
|
|
113
|
+
replace?: boolean;
|
|
114
|
+
}): this;
|
|
115
|
+
/**
|
|
116
|
+
* Alias for withTools([tool]).
|
|
117
|
+
*/
|
|
118
|
+
use(tool: ToolResolvable): this;
|
|
141
119
|
/**
|
|
142
120
|
* Send a message to the agent and get a response.
|
|
143
|
-
* @param message - The user message
|
|
144
|
-
* @param options - Optional request options
|
|
145
121
|
*/
|
|
146
|
-
ask(message: string, options?: AskOptions
|
|
122
|
+
ask(message: string, options?: AskOptions & {
|
|
123
|
+
inputs?: I;
|
|
124
|
+
}): Promise<ChatResponseString>;
|
|
125
|
+
/**
|
|
126
|
+
* Hook called when a tool call starts.
|
|
127
|
+
*/
|
|
128
|
+
onToolCallStart(handler: (toolCall: any) => void | Promise<void>): this;
|
|
129
|
+
/**
|
|
130
|
+
* Hook called when a tool call ends.
|
|
131
|
+
*/
|
|
132
|
+
onToolCallEnd(handler: (toolCall: any, result: any) => void | Promise<void>): this;
|
|
133
|
+
/**
|
|
134
|
+
* Hook called when a tool call errors.
|
|
135
|
+
*/
|
|
136
|
+
onToolCallError(handler: (toolCall: any, error: Error) => any): this;
|
|
137
|
+
/**
|
|
138
|
+
* Hook called before a request.
|
|
139
|
+
*/
|
|
140
|
+
beforeRequest(handler: (messages: any[]) => any): this;
|
|
141
|
+
/**
|
|
142
|
+
* Hook called after a response.
|
|
143
|
+
*/
|
|
144
|
+
afterResponse(handler: (response: ChatResponseString) => any): this;
|
|
147
145
|
/**
|
|
148
146
|
* Alias for ask()
|
|
149
147
|
*/
|
|
150
|
-
say(message: string, options?: AskOptions
|
|
148
|
+
say(message: string, options?: AskOptions & {
|
|
149
|
+
inputs?: I;
|
|
150
|
+
}): Promise<ChatResponseString>;
|
|
151
151
|
/**
|
|
152
152
|
* Stream a response from the agent.
|
|
153
|
-
* @param message - The user message
|
|
154
|
-
* @param options - Optional request options
|
|
155
153
|
*/
|
|
156
|
-
stream(message: string, options?: AskOptions
|
|
154
|
+
stream(message: string, options?: AskOptions & {
|
|
155
|
+
inputs?: I;
|
|
156
|
+
}): import("../index.js").Stream<import("../providers/Provider.js").ChatChunk>;
|
|
157
157
|
/**
|
|
158
158
|
* Get the conversation history.
|
|
159
159
|
*/
|
|
@@ -173,19 +173,6 @@ export declare abstract class Agent<S extends Record<string, unknown> = Record<s
|
|
|
173
173
|
}
|
|
174
174
|
/**
|
|
175
175
|
* Helper function to define an agent inline without creating a class.
|
|
176
|
-
*
|
|
177
|
-
* @example
|
|
178
|
-
* ```typescript
|
|
179
|
-
* const SupportAgent = defineAgent({
|
|
180
|
-
* model: "gpt-4o",
|
|
181
|
-
* instructions: "You are a helpful support agent",
|
|
182
|
-
* tools: [SearchDocs, LookupAccount],
|
|
183
|
-
* temperature: 0.2
|
|
184
|
-
* });
|
|
185
|
-
*
|
|
186
|
-
* const agent = new SupportAgent();
|
|
187
|
-
* const response = await agent.ask("Help me!");
|
|
188
|
-
* ```
|
|
189
176
|
*/
|
|
190
|
-
export declare function defineAgent<S extends Record<string, unknown> = Record<string, unknown>>(config: AgentConfig): new (overrides?: Partial<AgentConfig & ChatOptions>) => Agent<S>;
|
|
177
|
+
export declare function defineAgent<I extends Record<string, any> = Record<string, any>, S extends Record<string, unknown> = Record<string, unknown>>(config: AgentConfig<I>): new (overrides?: Partial<AgentConfig<I> & ChatOptions>) => Agent<I, S>;
|
|
191
178
|
//# sourceMappingURL=Agent.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Agent.d.ts","sourceRoot":"","sources":["../../src/agent/Agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC1E,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC7C,OAAO,EAAW,WAAW,EAAE,MAAM,WAAW,CAAC;AAEjD;;GAEG;AACH,MAAM,WAAW,WAAW;
|
|
1
|
+
{"version":3,"file":"Agent.d.ts","sourceRoot":"","sources":["../../src/agent/Agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC1E,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC7C,OAAO,EAAW,WAAW,EAAE,MAAM,WAAW,CAAC;AAEjD;;GAEG;AACH,MAAM,MAAM,SAAS,CAAC,CAAC,EAAE,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;AAE3D;;GAEG;AACH,MAAM,WAAW,WAAW,CAAC,CAAC,GAA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EAAE,CAAC,CAAA;KAAE,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAI9F;;OAEG;IACH,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,UAAU,GAAG;QAAE,MAAM,CAAC,EAAE,CAAC,CAAA;KAAE;IAO7D;;OAEG;IACH,IAAI,OAAO,6CAEV;IAED;;OAEG;IACH,IAAI,OAAO,IAAI,MAAM,CAEpB;IAED;;OAEG;IACH,IAAI,UAAU,6CAEb;IAED;;OAEG;IACH,IAAI,cAAc,IAAI,IAAI,CAAC,CAAC,CAAC,CAE5B;CACF;AAED;;GAEG;AACH,wBAAgB,WAAW,CACzB,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACnD,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC3D,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,KAAK,SAAS,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,KAAK,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAehG"}
|
package/dist/agent/Agent.js
CHANGED
|
@@ -1,27 +1,6 @@
|
|
|
1
1
|
import { NodeLLM } from "../llm.js";
|
|
2
2
|
/**
|
|
3
3
|
* Base class for creating reusable, class-configured agents.
|
|
4
|
-
*
|
|
5
|
-
* Define your agent configuration using static properties, then instantiate
|
|
6
|
-
* and use it anywhere in your application.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```typescript
|
|
10
|
-
* class SupportAgent extends Agent {
|
|
11
|
-
* static model = "gpt-4o";
|
|
12
|
-
* static instructions = "You are a helpful support agent";
|
|
13
|
-
* static tools = [SearchDocs, LookupAccount];
|
|
14
|
-
* static temperature = 0.2;
|
|
15
|
-
* }
|
|
16
|
-
*
|
|
17
|
-
* const agent = new SupportAgent();
|
|
18
|
-
* const response = await agent.ask("How can I reset my password?");
|
|
19
|
-
* ```
|
|
20
|
-
*
|
|
21
|
-
* @example Override configuration per instance:
|
|
22
|
-
* ```typescript
|
|
23
|
-
* const agent = new SupportAgent({ model: "gpt-4o-mini" });
|
|
24
|
-
* ```
|
|
25
4
|
*/
|
|
26
5
|
export class Agent {
|
|
27
6
|
// Static configuration properties - override these in subclasses
|
|
@@ -37,89 +16,57 @@ export class Agent {
|
|
|
37
16
|
static maxTokens;
|
|
38
17
|
static maxToolCalls;
|
|
39
18
|
static assumeModelExists;
|
|
19
|
+
/**
|
|
20
|
+
* Explicitly declare which inputs this agent expects.
|
|
21
|
+
* Useful for introspection and validation.
|
|
22
|
+
*/
|
|
23
|
+
static inputs;
|
|
40
24
|
/**
|
|
41
25
|
* Hook called when the agent starts a new session (ask/stream).
|
|
42
|
-
* @param context - Initial context including messages/options
|
|
43
26
|
*/
|
|
44
27
|
static onStart(_context) {
|
|
45
28
|
// Override in subclass
|
|
46
29
|
}
|
|
47
|
-
/**
|
|
48
|
-
* Hook called when the agent generates a reasoning trace (thinking).
|
|
49
|
-
* @param thinking - The content of the thinking trace
|
|
50
|
-
* @param result - The full response object containing the thinking
|
|
51
|
-
*/
|
|
52
30
|
static onThinking(_thinking, _result) {
|
|
53
31
|
// Override in subclass
|
|
54
32
|
}
|
|
55
|
-
/**
|
|
56
|
-
* Hook called when a tool execution starts.
|
|
57
|
-
* @param toolCall - The tool call object (id, function name, arguments)
|
|
58
|
-
*/
|
|
59
33
|
static onToolStart(_toolCall) {
|
|
60
34
|
// Override in subclass
|
|
61
35
|
}
|
|
62
|
-
/**
|
|
63
|
-
* Hook called when a tool execution ends.
|
|
64
|
-
* @param toolCall - The tool call object
|
|
65
|
-
* @param result - The result of the tool execution
|
|
66
|
-
*/
|
|
67
36
|
static onToolEnd(_toolCall, _result) {
|
|
68
37
|
// Override in subclass
|
|
69
38
|
}
|
|
70
|
-
/**
|
|
71
|
-
* Hook called when a tool execution encounters an error.
|
|
72
|
-
* @param toolCall - The tool call object
|
|
73
|
-
* @param error - The error that occurred
|
|
74
|
-
*/
|
|
75
39
|
static onToolError(_toolCall, _error) {
|
|
76
40
|
// Override in subclass
|
|
77
41
|
}
|
|
78
|
-
/**
|
|
79
|
-
* Hook called when the agent completes a response turn.
|
|
80
|
-
* @param result - The final response object
|
|
81
|
-
*/
|
|
82
42
|
static onComplete(_result) {
|
|
83
43
|
// Override in subclass
|
|
84
44
|
}
|
|
85
45
|
// --- Static Execution API ---
|
|
86
46
|
/**
|
|
87
47
|
* Run the agent immediately with a prompt.
|
|
88
|
-
* Creates a new instance of the agent, runs the prompt, and disposes it.
|
|
89
|
-
*
|
|
90
|
-
* @example
|
|
91
|
-
* ```typescript
|
|
92
|
-
* const result = await TravelAgent.ask("Find flights to Paris");
|
|
93
|
-
* ```
|
|
94
48
|
*/
|
|
95
49
|
static async ask(message, options) {
|
|
96
|
-
const
|
|
97
|
-
const agent = new Ctor({});
|
|
50
|
+
const agent = new this({ ...options });
|
|
98
51
|
return agent.ask(message, options);
|
|
99
52
|
}
|
|
100
53
|
/**
|
|
101
54
|
* Stream the agent response immediately.
|
|
102
|
-
* Creates a new instance of the agent and streams the response.
|
|
103
|
-
*
|
|
104
|
-
* @example
|
|
105
|
-
* ```typescript
|
|
106
|
-
* for await (const chunk of TravelAgent.stream("Write a poem")) {
|
|
107
|
-
* process.stdout.write(chunk.content);
|
|
108
|
-
* }
|
|
109
|
-
* ```
|
|
110
55
|
*/
|
|
111
56
|
static stream(message, options) {
|
|
112
|
-
const
|
|
113
|
-
const agent = new Ctor({});
|
|
57
|
+
const agent = new this({ ...options });
|
|
114
58
|
return agent.stream(message, options);
|
|
115
59
|
}
|
|
116
60
|
/** The underlying Chat instance */
|
|
117
61
|
chat;
|
|
62
|
+
/** Private reference to configuration overrides for lazy resolution */
|
|
63
|
+
config;
|
|
118
64
|
/**
|
|
119
65
|
* Create a new agent instance.
|
|
120
66
|
* @param overrides - Optional configuration to override static properties
|
|
121
67
|
*/
|
|
122
68
|
constructor(overrides = {}) {
|
|
69
|
+
this.config = overrides;
|
|
123
70
|
const ctor = this.constructor;
|
|
124
71
|
// Build chat options from static properties + overrides
|
|
125
72
|
const chatOptions = {
|
|
@@ -131,53 +78,58 @@ export class Agent {
|
|
|
131
78
|
headers: { ...ctor.headers, ...overrides.headers },
|
|
132
79
|
params: { ...ctor.params, ...overrides.params },
|
|
133
80
|
thinking: overrides.thinking ?? ctor.thinking,
|
|
134
|
-
messages: overrides.messages
|
|
81
|
+
messages: overrides.messages
|
|
135
82
|
};
|
|
136
83
|
// Determine model
|
|
137
84
|
const model = overrides.model ?? ctor.model;
|
|
138
85
|
if (!model) {
|
|
139
86
|
throw new Error(`[Agent] No model specified. Set static model property or pass model in constructor.`);
|
|
140
87
|
}
|
|
141
|
-
// Use provided LLM instance or fall back to global NodeLLM
|
|
142
88
|
const llm = overrides.llm ?? NodeLLM;
|
|
143
89
|
this.chat = llm.chat(model, chatOptions);
|
|
144
|
-
//
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
this.chat.withInstructions(instructions);
|
|
90
|
+
// Initial resolution if inputs are provided in constructor
|
|
91
|
+
if (overrides.inputs) {
|
|
92
|
+
this.resolveLazyConfig(overrides.inputs);
|
|
148
93
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
94
|
+
else {
|
|
95
|
+
// Fallback: apply static/direct instructions immediately if they aren't functions
|
|
96
|
+
const instructions = overrides.instructions ?? ctor.instructions;
|
|
97
|
+
if (instructions && typeof instructions !== "function") {
|
|
98
|
+
this.chat.withInstructions(instructions);
|
|
99
|
+
}
|
|
100
|
+
const tools = overrides.tools ?? ctor.tools;
|
|
101
|
+
if (tools && typeof tools !== "function" && tools.length > 0) {
|
|
102
|
+
this.chat.withTools(tools);
|
|
103
|
+
}
|
|
153
104
|
}
|
|
154
105
|
// Apply schema
|
|
155
106
|
const schema = overrides.schema ?? ctor.schema;
|
|
156
107
|
if (schema) {
|
|
157
108
|
this.chat.withSchema(schema);
|
|
158
109
|
}
|
|
159
|
-
// Wire up
|
|
160
|
-
// Trigger onStart immediately
|
|
110
|
+
// Wire up telemetry hooks
|
|
161
111
|
if (ctor.onStart) {
|
|
162
112
|
this.chat.beforeRequest(async (messages) => {
|
|
163
|
-
if (ctor.onStart)
|
|
113
|
+
if (ctor.onStart)
|
|
164
114
|
await ctor.onStart({ messages });
|
|
165
|
-
}
|
|
166
115
|
});
|
|
167
116
|
}
|
|
168
117
|
if (ctor.onToolStart) {
|
|
169
118
|
this.chat.onToolCallStart(async (toolCall) => {
|
|
170
|
-
|
|
119
|
+
if (ctor.onToolStart)
|
|
120
|
+
await ctor.onToolStart(toolCall);
|
|
171
121
|
});
|
|
172
122
|
}
|
|
173
123
|
if (ctor.onToolEnd) {
|
|
174
124
|
this.chat.onToolCallEnd(async (toolCall, result) => {
|
|
175
|
-
|
|
125
|
+
if (ctor.onToolEnd)
|
|
126
|
+
await ctor.onToolEnd(toolCall, result);
|
|
176
127
|
});
|
|
177
128
|
}
|
|
178
129
|
if (ctor.onToolError) {
|
|
179
130
|
this.chat.onToolCallError(async (toolCall, error) => {
|
|
180
|
-
|
|
131
|
+
if (ctor.onToolError)
|
|
132
|
+
await ctor.onToolError(toolCall, error);
|
|
181
133
|
});
|
|
182
134
|
}
|
|
183
135
|
if (ctor.onComplete || ctor.onThinking) {
|
|
@@ -191,14 +143,95 @@ export class Agent {
|
|
|
191
143
|
});
|
|
192
144
|
}
|
|
193
145
|
}
|
|
146
|
+
/**
|
|
147
|
+
* Helper to resolve lazy instructions and tools based on inputs.
|
|
148
|
+
*/
|
|
149
|
+
resolveLazyConfig(inputs) {
|
|
150
|
+
if (!inputs)
|
|
151
|
+
return;
|
|
152
|
+
const ctor = this.constructor;
|
|
153
|
+
// 1. Resolve Instructions
|
|
154
|
+
let instructions = this.config.instructions ?? ctor.instructions;
|
|
155
|
+
if (typeof instructions === "function") {
|
|
156
|
+
instructions = instructions(inputs);
|
|
157
|
+
}
|
|
158
|
+
if (typeof instructions === "string") {
|
|
159
|
+
this.chat.withInstructions(instructions, { replace: true });
|
|
160
|
+
}
|
|
161
|
+
// 2. Resolve Tools
|
|
162
|
+
let tools = this.config.tools ?? ctor.tools;
|
|
163
|
+
if (typeof tools === "function") {
|
|
164
|
+
tools = tools(inputs);
|
|
165
|
+
}
|
|
166
|
+
if (Array.isArray(tools)) {
|
|
167
|
+
this.chat.withTools(tools, { replace: true });
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
// --- Fluent Configuration ---
|
|
171
|
+
/**
|
|
172
|
+
* Add instructions to the agent (replaces or appends).
|
|
173
|
+
*/
|
|
174
|
+
withInstructions(instructions, options) {
|
|
175
|
+
this.chat.withInstructions(instructions, options);
|
|
176
|
+
return this;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Add tools to the agent.
|
|
180
|
+
*/
|
|
181
|
+
withTools(tools, options) {
|
|
182
|
+
this.chat.withTools(tools, options);
|
|
183
|
+
return this;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Alias for withTools([tool]).
|
|
187
|
+
*/
|
|
188
|
+
use(tool) {
|
|
189
|
+
return this.withTools([tool]);
|
|
190
|
+
}
|
|
194
191
|
/**
|
|
195
192
|
* Send a message to the agent and get a response.
|
|
196
|
-
* @param message - The user message
|
|
197
|
-
* @param options - Optional request options
|
|
198
193
|
*/
|
|
199
194
|
async ask(message, options) {
|
|
195
|
+
if (options?.inputs) {
|
|
196
|
+
this.resolveLazyConfig(options.inputs);
|
|
197
|
+
}
|
|
200
198
|
return this.chat.ask(message, options);
|
|
201
199
|
}
|
|
200
|
+
/**
|
|
201
|
+
* Hook called when a tool call starts.
|
|
202
|
+
*/
|
|
203
|
+
onToolCallStart(handler) {
|
|
204
|
+
this.chat.onToolCallStart(handler);
|
|
205
|
+
return this;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Hook called when a tool call ends.
|
|
209
|
+
*/
|
|
210
|
+
onToolCallEnd(handler) {
|
|
211
|
+
this.chat.onToolCallEnd(handler);
|
|
212
|
+
return this;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Hook called when a tool call errors.
|
|
216
|
+
*/
|
|
217
|
+
onToolCallError(handler) {
|
|
218
|
+
this.chat.onToolCallError(handler);
|
|
219
|
+
return this;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Hook called before a request.
|
|
223
|
+
*/
|
|
224
|
+
beforeRequest(handler) {
|
|
225
|
+
this.chat.beforeRequest(handler);
|
|
226
|
+
return this;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Hook called after a response.
|
|
230
|
+
*/
|
|
231
|
+
afterResponse(handler) {
|
|
232
|
+
this.chat.afterResponse(handler);
|
|
233
|
+
return this;
|
|
234
|
+
}
|
|
202
235
|
/**
|
|
203
236
|
* Alias for ask()
|
|
204
237
|
*/
|
|
@@ -207,10 +240,11 @@ export class Agent {
|
|
|
207
240
|
}
|
|
208
241
|
/**
|
|
209
242
|
* Stream a response from the agent.
|
|
210
|
-
* @param message - The user message
|
|
211
|
-
* @param options - Optional request options
|
|
212
243
|
*/
|
|
213
244
|
stream(message, options) {
|
|
245
|
+
if (options?.inputs) {
|
|
246
|
+
this.resolveLazyConfig(options.inputs);
|
|
247
|
+
}
|
|
214
248
|
return this.chat.stream(message, options);
|
|
215
249
|
}
|
|
216
250
|
/**
|
|
@@ -240,19 +274,6 @@ export class Agent {
|
|
|
240
274
|
}
|
|
241
275
|
/**
|
|
242
276
|
* Helper function to define an agent inline without creating a class.
|
|
243
|
-
*
|
|
244
|
-
* @example
|
|
245
|
-
* ```typescript
|
|
246
|
-
* const SupportAgent = defineAgent({
|
|
247
|
-
* model: "gpt-4o",
|
|
248
|
-
* instructions: "You are a helpful support agent",
|
|
249
|
-
* tools: [SearchDocs, LookupAccount],
|
|
250
|
-
* temperature: 0.2
|
|
251
|
-
* });
|
|
252
|
-
*
|
|
253
|
-
* const agent = new SupportAgent();
|
|
254
|
-
* const response = await agent.ask("Help me!");
|
|
255
|
-
* ```
|
|
256
277
|
*/
|
|
257
278
|
export function defineAgent(config) {
|
|
258
279
|
return class extends Agent {
|