beeai-framework 0.1.6 → 0.1.7
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/adapters/vercel/backend/chat.cjs +0 -1
- package/dist/adapters/vercel/backend/chat.cjs.map +1 -1
- package/dist/adapters/vercel/backend/chat.js +0 -1
- package/dist/adapters/vercel/backend/chat.js.map +1 -1
- package/dist/adapters/watsonx/backend/chat.cjs +6 -2
- package/dist/adapters/watsonx/backend/chat.cjs.map +1 -1
- package/dist/adapters/watsonx/backend/chat.d.cts +1 -1
- package/dist/adapters/watsonx/backend/chat.d.ts +1 -1
- package/dist/adapters/watsonx/backend/chat.js +6 -2
- package/dist/adapters/watsonx/backend/chat.js.map +1 -1
- package/dist/agent-Bf2FaCKy.d.ts +84 -0
- package/dist/agent-Bpt7IFa_.d.cts +84 -0
- package/dist/agents/experimental/remote/agent.cjs +25 -18
- package/dist/agents/experimental/remote/agent.cjs.map +1 -1
- package/dist/agents/experimental/remote/agent.d.cts +4 -3
- package/dist/agents/experimental/remote/agent.d.ts +4 -3
- package/dist/agents/experimental/remote/agent.js +26 -19
- package/dist/agents/experimental/remote/agent.js.map +1 -1
- package/dist/agents/{bee → react}/agent.cjs +5 -5
- package/dist/agents/react/agent.cjs.map +1 -0
- package/dist/agents/{bee → react}/agent.d.cts +1 -1
- package/dist/agents/{bee → react}/agent.d.ts +1 -1
- package/dist/agents/{bee → react}/agent.js +5 -5
- package/dist/agents/react/agent.js.map +1 -0
- package/dist/agents/{bee → react}/prompts.cjs +18 -18
- package/dist/agents/react/prompts.cjs.map +1 -0
- package/dist/agents/{bee → react}/prompts.d.cts +10 -10
- package/dist/agents/{bee → react}/prompts.d.ts +10 -10
- package/dist/agents/{bee → react}/prompts.js +10 -10
- package/dist/agents/react/prompts.js.map +1 -0
- package/dist/agents/{bee → react}/runners/base.cjs +1 -1
- package/dist/agents/{bee → react}/runners/base.cjs.map +1 -1
- package/dist/agents/{bee → react}/runners/base.d.cts +1 -1
- package/dist/agents/{bee → react}/runners/base.d.ts +1 -1
- package/dist/agents/{bee → react}/runners/base.js +1 -1
- package/dist/agents/{bee → react}/runners/base.js.map +1 -1
- package/dist/agents/{bee → react}/runners/deep-think/prompts.cjs +14 -14
- package/dist/agents/react/runners/deep-think/prompts.cjs.map +1 -0
- package/dist/agents/{bee/runners/granite → react/runners/deep-think}/prompts.d.cts +8 -8
- package/dist/agents/{bee/runners/granite → react/runners/deep-think}/prompts.d.ts +8 -8
- package/dist/agents/{bee → react}/runners/deep-think/prompts.js +9 -9
- package/dist/agents/react/runners/deep-think/prompts.js.map +1 -0
- package/dist/agents/{bee → react}/runners/deep-think/runner.cjs +10 -10
- package/dist/agents/react/runners/deep-think/runner.cjs.map +1 -0
- package/dist/agents/{bee → react}/runners/deep-think/runner.d.cts +4 -4
- package/dist/agents/{bee → react}/runners/deep-think/runner.d.ts +4 -4
- package/dist/agents/{bee → react}/runners/deep-think/runner.js +12 -12
- package/dist/agents/react/runners/deep-think/runner.js.map +1 -0
- package/dist/agents/{bee → react}/runners/default/runner.cjs +10 -10
- package/dist/agents/react/runners/default/runner.cjs.map +1 -0
- package/dist/agents/{bee → react}/runners/default/runner.d.cts +7 -7
- package/dist/agents/{bee → react}/runners/default/runner.d.ts +7 -7
- package/dist/agents/{bee → react}/runners/default/runner.js +11 -11
- package/dist/agents/react/runners/default/runner.js.map +1 -0
- package/dist/agents/{bee → react}/runners/default/runner.spec.cjs +5 -5
- package/dist/agents/react/runners/default/runner.spec.cjs.map +1 -0
- package/dist/agents/{bee → react}/runners/default/runner.spec.js +6 -6
- package/dist/agents/react/runners/default/runner.spec.js.map +1 -0
- package/dist/agents/{bee → react}/runners/granite/prompts.cjs +14 -14
- package/dist/agents/react/runners/granite/prompts.cjs.map +1 -0
- package/dist/agents/{bee/runners/deep-think → react/runners/granite}/prompts.d.cts +8 -8
- package/dist/agents/{bee/runners/deep-think → react/runners/granite}/prompts.d.ts +8 -8
- package/dist/agents/{bee → react}/runners/granite/prompts.js +9 -9
- package/dist/agents/react/runners/granite/prompts.js.map +1 -0
- package/dist/agents/{bee → react}/runners/granite/runner.cjs +10 -10
- package/dist/agents/react/runners/granite/runner.cjs.map +1 -0
- package/dist/agents/{bee → react}/runners/granite/runner.d.cts +4 -4
- package/dist/agents/{bee → react}/runners/granite/runner.d.ts +4 -4
- package/dist/agents/{bee → react}/runners/granite/runner.js +12 -12
- package/dist/agents/react/runners/granite/runner.js.map +1 -0
- package/dist/agents/{bee → react}/types.d.cts +45 -45
- package/dist/agents/{bee → react}/types.d.ts +45 -45
- package/dist/agents/toolCalling/agent.cjs +208 -0
- package/dist/agents/toolCalling/agent.cjs.map +1 -0
- package/dist/agents/toolCalling/agent.d.cts +70 -0
- package/dist/agents/toolCalling/agent.d.ts +70 -0
- package/dist/agents/toolCalling/agent.js +206 -0
- package/dist/agents/toolCalling/agent.js.map +1 -0
- package/dist/agents/toolCalling/prompts.cjs +65 -0
- package/dist/agents/toolCalling/prompts.cjs.map +1 -0
- package/dist/agents/toolCalling/prompts.d.cts +49 -0
- package/dist/agents/toolCalling/prompts.d.ts +49 -0
- package/dist/agents/toolCalling/prompts.js +62 -0
- package/dist/agents/toolCalling/prompts.js.map +1 -0
- package/dist/agents/toolCalling/types.cjs +4 -0
- package/dist/agents/toolCalling/types.cjs.map +1 -0
- package/dist/agents/toolCalling/types.d.cts +66 -0
- package/dist/agents/toolCalling/types.d.ts +66 -0
- package/dist/agents/toolCalling/types.js +3 -0
- package/dist/agents/toolCalling/types.js.map +1 -0
- package/dist/backend/chat.cjs +3 -0
- package/dist/backend/chat.cjs.map +1 -1
- package/dist/backend/chat.d.cts +2 -1
- package/dist/backend/chat.d.ts +2 -1
- package/dist/backend/chat.js +3 -0
- package/dist/backend/chat.js.map +1 -1
- package/dist/backend/message.cjs +9 -0
- package/dist/backend/message.cjs.map +1 -1
- package/dist/backend/message.d.cts +2 -0
- package/dist/backend/message.d.ts +2 -0
- package/dist/backend/message.js +9 -0
- package/dist/backend/message.js.map +1 -1
- package/dist/instrumentation/create-telemetry-middleware.cjs.map +1 -1
- package/dist/instrumentation/create-telemetry-middleware.js.map +1 -1
- package/dist/version.cjs +1 -1
- package/dist/version.js +1 -1
- package/dist/workflows/agent.cjs +2 -2
- package/dist/workflows/agent.cjs.map +1 -1
- package/dist/workflows/agent.d.cts +4 -4
- package/dist/workflows/agent.d.ts +4 -4
- package/dist/workflows/agent.js +2 -2
- package/dist/workflows/agent.js.map +1 -1
- package/package.json +3 -3
- package/dist/agent-BZ-_ATuJ.d.cts +0 -84
- package/dist/agent-BqoZ6FbM.d.ts +0 -84
- package/dist/agents/bee/agent.cjs.map +0 -1
- package/dist/agents/bee/agent.js.map +0 -1
- package/dist/agents/bee/prompts.cjs.map +0 -1
- package/dist/agents/bee/prompts.js.map +0 -1
- package/dist/agents/bee/runners/deep-think/prompts.cjs.map +0 -1
- package/dist/agents/bee/runners/deep-think/prompts.js.map +0 -1
- package/dist/agents/bee/runners/deep-think/runner.cjs.map +0 -1
- package/dist/agents/bee/runners/deep-think/runner.js.map +0 -1
- package/dist/agents/bee/runners/default/runner.cjs.map +0 -1
- package/dist/agents/bee/runners/default/runner.js.map +0 -1
- package/dist/agents/bee/runners/default/runner.spec.cjs.map +0 -1
- package/dist/agents/bee/runners/default/runner.spec.js.map +0 -1
- package/dist/agents/bee/runners/granite/prompts.cjs.map +0 -1
- package/dist/agents/bee/runners/granite/prompts.js.map +0 -1
- package/dist/agents/bee/runners/granite/runner.cjs.map +0 -1
- package/dist/agents/bee/runners/granite/runner.js.map +0 -1
- /package/dist/agents/{bee → react}/runners/default/runner.spec.d.cts +0 -0
- /package/dist/agents/{bee → react}/runners/default/runner.spec.d.ts +0 -0
- /package/dist/agents/{bee → react}/types.cjs +0 -0
- /package/dist/agents/{bee → react}/types.cjs.map +0 -0
- /package/dist/agents/{bee → react}/types.js +0 -0
- /package/dist/agents/{bee → react}/types.js.map +0 -0
|
@@ -2,7 +2,7 @@ import { BaseMemory } from '../../memory/base.cjs';
|
|
|
2
2
|
import { Message } from '../../backend/message.cjs';
|
|
3
3
|
import { C as Callback } from '../../emitter-C9EN5B0j.cjs';
|
|
4
4
|
import { AnyTool, BaseToolRunOptions, ToolOutput, ToolError } from '../../tools/base.cjs';
|
|
5
|
-
import {
|
|
5
|
+
import { ReActAgentSystemPrompt, ReActAgentAssistantPrompt, ReActAgentUserPrompt, ReActAgentUserEmptyPrompt, ReActAgentToolErrorPrompt, ReActAgentToolInputErrorPrompt, ReActAgentToolNoResultsPrompt, ReActAgentToolNotFoundPrompt, ReActAgentSchemaErrorPrompt } from './prompts.cjs';
|
|
6
6
|
import { LinePrefixParser } from '../../parsers/linePrefix.cjs';
|
|
7
7
|
import { ZodParserField, JSONParserField } from '../../parsers/field.cjs';
|
|
8
8
|
import { NonUndefined } from '../../internals/types.cjs';
|
|
@@ -43,79 +43,79 @@ import '@ai-sdk/provider';
|
|
|
43
43
|
* limitations under the License.
|
|
44
44
|
*/
|
|
45
45
|
|
|
46
|
-
interface
|
|
46
|
+
interface ReActAgentRunInput {
|
|
47
47
|
prompt: string | null;
|
|
48
48
|
}
|
|
49
|
-
interface
|
|
49
|
+
interface ReActAgentRunOutput {
|
|
50
50
|
result: Message;
|
|
51
|
-
iterations:
|
|
51
|
+
iterations: ReActAgentRunIteration[];
|
|
52
52
|
memory: BaseMemory;
|
|
53
53
|
}
|
|
54
|
-
interface
|
|
54
|
+
interface ReActAgentRunIteration {
|
|
55
55
|
raw: ChatModelOutput;
|
|
56
|
-
state:
|
|
56
|
+
state: ReActIterationResult;
|
|
57
57
|
}
|
|
58
|
-
interface
|
|
58
|
+
interface ReActAgentExecutionConfig {
|
|
59
59
|
totalMaxRetries?: number;
|
|
60
60
|
maxRetriesPerStep?: number;
|
|
61
61
|
maxIterations?: number;
|
|
62
62
|
}
|
|
63
|
-
interface
|
|
63
|
+
interface ReActAgentRunOptions {
|
|
64
64
|
signal?: AbortSignal;
|
|
65
|
-
execution?:
|
|
65
|
+
execution?: ReActAgentExecutionConfig;
|
|
66
66
|
}
|
|
67
|
-
interface
|
|
67
|
+
interface ReActAgentMeta {
|
|
68
68
|
iteration: number;
|
|
69
69
|
}
|
|
70
|
-
interface
|
|
70
|
+
interface ReActAgentUpdateMeta extends ReActAgentMeta {
|
|
71
71
|
success: boolean;
|
|
72
72
|
}
|
|
73
|
-
interface
|
|
73
|
+
interface ReActAgentCallbacks {
|
|
74
74
|
start?: Callback<{
|
|
75
|
-
meta:
|
|
75
|
+
meta: ReActAgentMeta;
|
|
76
76
|
tools: AnyTool[];
|
|
77
77
|
memory: BaseMemory;
|
|
78
78
|
}>;
|
|
79
79
|
error?: Callback<{
|
|
80
80
|
error: Error;
|
|
81
|
-
meta:
|
|
81
|
+
meta: ReActAgentMeta;
|
|
82
82
|
}>;
|
|
83
83
|
retry?: Callback<{
|
|
84
|
-
meta:
|
|
84
|
+
meta: ReActAgentMeta;
|
|
85
85
|
}>;
|
|
86
86
|
success?: Callback<{
|
|
87
87
|
data: Message;
|
|
88
|
-
iterations:
|
|
88
|
+
iterations: ReActAgentRunIteration[];
|
|
89
89
|
memory: BaseMemory;
|
|
90
|
-
meta:
|
|
90
|
+
meta: ReActAgentMeta;
|
|
91
91
|
}>;
|
|
92
92
|
update?: Callback<{
|
|
93
|
-
data:
|
|
93
|
+
data: ReActIterationResult;
|
|
94
94
|
update: {
|
|
95
|
-
key: keyof
|
|
95
|
+
key: keyof ReActIterationResult;
|
|
96
96
|
value: string;
|
|
97
97
|
parsedValue: unknown;
|
|
98
98
|
};
|
|
99
|
-
meta:
|
|
99
|
+
meta: ReActAgentUpdateMeta;
|
|
100
100
|
memory: BaseMemory;
|
|
101
101
|
}>;
|
|
102
102
|
partialUpdate?: Callback<{
|
|
103
|
-
data:
|
|
103
|
+
data: ReActAgentIterationResultPartial;
|
|
104
104
|
update: {
|
|
105
|
-
key: keyof
|
|
105
|
+
key: keyof ReActIterationResult;
|
|
106
106
|
value: string;
|
|
107
107
|
parsedValue: unknown;
|
|
108
108
|
};
|
|
109
|
-
meta:
|
|
109
|
+
meta: ReActAgentUpdateMeta;
|
|
110
110
|
}>;
|
|
111
111
|
toolStart?: Callback<{
|
|
112
112
|
data: {
|
|
113
113
|
tool: AnyTool;
|
|
114
114
|
input: unknown;
|
|
115
115
|
options: BaseToolRunOptions;
|
|
116
|
-
iteration:
|
|
116
|
+
iteration: ReActAgentIterationToolResult;
|
|
117
117
|
};
|
|
118
|
-
meta:
|
|
118
|
+
meta: ReActAgentMeta;
|
|
119
119
|
}>;
|
|
120
120
|
toolSuccess?: Callback<{
|
|
121
121
|
data: {
|
|
@@ -123,9 +123,9 @@ interface BeeCallbacks {
|
|
|
123
123
|
input: unknown;
|
|
124
124
|
options: BaseToolRunOptions;
|
|
125
125
|
result: ToolOutput;
|
|
126
|
-
iteration:
|
|
126
|
+
iteration: ReActAgentIterationToolResult;
|
|
127
127
|
};
|
|
128
|
-
meta:
|
|
128
|
+
meta: ReActAgentMeta;
|
|
129
129
|
}>;
|
|
130
130
|
toolError?: Callback<{
|
|
131
131
|
data: {
|
|
@@ -133,32 +133,32 @@ interface BeeCallbacks {
|
|
|
133
133
|
input: unknown;
|
|
134
134
|
options: BaseToolRunOptions;
|
|
135
135
|
error: ToolError;
|
|
136
|
-
iteration:
|
|
136
|
+
iteration: ReActAgentIterationToolResult;
|
|
137
137
|
};
|
|
138
|
-
meta:
|
|
138
|
+
meta: ReActAgentMeta;
|
|
139
139
|
}>;
|
|
140
140
|
}
|
|
141
|
-
interface
|
|
142
|
-
system: typeof
|
|
143
|
-
assistant: typeof
|
|
144
|
-
user: typeof
|
|
145
|
-
userEmpty: typeof
|
|
146
|
-
toolError: typeof
|
|
147
|
-
toolInputError: typeof
|
|
148
|
-
toolNoResultError: typeof
|
|
149
|
-
toolNotFoundError: typeof
|
|
150
|
-
schemaError: typeof
|
|
141
|
+
interface ReActAgentTemplates {
|
|
142
|
+
system: typeof ReActAgentSystemPrompt;
|
|
143
|
+
assistant: typeof ReActAgentAssistantPrompt;
|
|
144
|
+
user: typeof ReActAgentUserPrompt;
|
|
145
|
+
userEmpty: typeof ReActAgentUserEmptyPrompt;
|
|
146
|
+
toolError: typeof ReActAgentToolErrorPrompt;
|
|
147
|
+
toolInputError: typeof ReActAgentToolInputErrorPrompt;
|
|
148
|
+
toolNoResultError: typeof ReActAgentToolNoResultsPrompt;
|
|
149
|
+
toolNotFoundError: typeof ReActAgentToolNotFoundPrompt;
|
|
150
|
+
schemaError: typeof ReActAgentSchemaErrorPrompt;
|
|
151
151
|
}
|
|
152
|
-
type
|
|
152
|
+
type ReActAgentParserInput = LinePrefixParser.define<{
|
|
153
153
|
thought: ZodParserField<string>;
|
|
154
154
|
tool_name: ZodParserField<string>;
|
|
155
155
|
tool_input: JSONParserField<Record<string, any>>;
|
|
156
156
|
tool_output: ZodParserField<string>;
|
|
157
157
|
final_answer: ZodParserField<string>;
|
|
158
158
|
}>;
|
|
159
|
-
type
|
|
160
|
-
type
|
|
161
|
-
type
|
|
162
|
-
type
|
|
159
|
+
type ReActAgentParser = LinePrefixParser<ReActAgentParserInput>;
|
|
160
|
+
type ReActIterationResult = LinePrefixParser.inferOutput<ReActAgentParser>;
|
|
161
|
+
type ReActAgentIterationResultPartial = LinePrefixParser.inferPartialOutput<ReActAgentParser>;
|
|
162
|
+
type ReActAgentIterationToolResult = NonUndefined<ReActIterationResult, "tool_input" | "tool_name">;
|
|
163
163
|
|
|
164
|
-
export type {
|
|
164
|
+
export type { ReActAgentCallbacks, ReActAgentExecutionConfig, ReActAgentIterationResultPartial, ReActAgentIterationToolResult, ReActAgentMeta, ReActAgentParserInput, ReActAgentRunInput, ReActAgentRunIteration, ReActAgentRunOptions, ReActAgentRunOutput, ReActAgentTemplates, ReActAgentUpdateMeta, ReActIterationResult };
|
|
@@ -2,7 +2,7 @@ import { BaseMemory } from '../../memory/base.js';
|
|
|
2
2
|
import { Message } from '../../backend/message.js';
|
|
3
3
|
import { C as Callback } from '../../emitter-BxVxGBbJ.js';
|
|
4
4
|
import { AnyTool, BaseToolRunOptions, ToolOutput, ToolError } from '../../tools/base.js';
|
|
5
|
-
import {
|
|
5
|
+
import { ReActAgentSystemPrompt, ReActAgentAssistantPrompt, ReActAgentUserPrompt, ReActAgentUserEmptyPrompt, ReActAgentToolErrorPrompt, ReActAgentToolInputErrorPrompt, ReActAgentToolNoResultsPrompt, ReActAgentToolNotFoundPrompt, ReActAgentSchemaErrorPrompt } from './prompts.js';
|
|
6
6
|
import { LinePrefixParser } from '../../parsers/linePrefix.js';
|
|
7
7
|
import { ZodParserField, JSONParserField } from '../../parsers/field.js';
|
|
8
8
|
import { NonUndefined } from '../../internals/types.js';
|
|
@@ -43,79 +43,79 @@ import '@ai-sdk/provider';
|
|
|
43
43
|
* limitations under the License.
|
|
44
44
|
*/
|
|
45
45
|
|
|
46
|
-
interface
|
|
46
|
+
interface ReActAgentRunInput {
|
|
47
47
|
prompt: string | null;
|
|
48
48
|
}
|
|
49
|
-
interface
|
|
49
|
+
interface ReActAgentRunOutput {
|
|
50
50
|
result: Message;
|
|
51
|
-
iterations:
|
|
51
|
+
iterations: ReActAgentRunIteration[];
|
|
52
52
|
memory: BaseMemory;
|
|
53
53
|
}
|
|
54
|
-
interface
|
|
54
|
+
interface ReActAgentRunIteration {
|
|
55
55
|
raw: ChatModelOutput;
|
|
56
|
-
state:
|
|
56
|
+
state: ReActIterationResult;
|
|
57
57
|
}
|
|
58
|
-
interface
|
|
58
|
+
interface ReActAgentExecutionConfig {
|
|
59
59
|
totalMaxRetries?: number;
|
|
60
60
|
maxRetriesPerStep?: number;
|
|
61
61
|
maxIterations?: number;
|
|
62
62
|
}
|
|
63
|
-
interface
|
|
63
|
+
interface ReActAgentRunOptions {
|
|
64
64
|
signal?: AbortSignal;
|
|
65
|
-
execution?:
|
|
65
|
+
execution?: ReActAgentExecutionConfig;
|
|
66
66
|
}
|
|
67
|
-
interface
|
|
67
|
+
interface ReActAgentMeta {
|
|
68
68
|
iteration: number;
|
|
69
69
|
}
|
|
70
|
-
interface
|
|
70
|
+
interface ReActAgentUpdateMeta extends ReActAgentMeta {
|
|
71
71
|
success: boolean;
|
|
72
72
|
}
|
|
73
|
-
interface
|
|
73
|
+
interface ReActAgentCallbacks {
|
|
74
74
|
start?: Callback<{
|
|
75
|
-
meta:
|
|
75
|
+
meta: ReActAgentMeta;
|
|
76
76
|
tools: AnyTool[];
|
|
77
77
|
memory: BaseMemory;
|
|
78
78
|
}>;
|
|
79
79
|
error?: Callback<{
|
|
80
80
|
error: Error;
|
|
81
|
-
meta:
|
|
81
|
+
meta: ReActAgentMeta;
|
|
82
82
|
}>;
|
|
83
83
|
retry?: Callback<{
|
|
84
|
-
meta:
|
|
84
|
+
meta: ReActAgentMeta;
|
|
85
85
|
}>;
|
|
86
86
|
success?: Callback<{
|
|
87
87
|
data: Message;
|
|
88
|
-
iterations:
|
|
88
|
+
iterations: ReActAgentRunIteration[];
|
|
89
89
|
memory: BaseMemory;
|
|
90
|
-
meta:
|
|
90
|
+
meta: ReActAgentMeta;
|
|
91
91
|
}>;
|
|
92
92
|
update?: Callback<{
|
|
93
|
-
data:
|
|
93
|
+
data: ReActIterationResult;
|
|
94
94
|
update: {
|
|
95
|
-
key: keyof
|
|
95
|
+
key: keyof ReActIterationResult;
|
|
96
96
|
value: string;
|
|
97
97
|
parsedValue: unknown;
|
|
98
98
|
};
|
|
99
|
-
meta:
|
|
99
|
+
meta: ReActAgentUpdateMeta;
|
|
100
100
|
memory: BaseMemory;
|
|
101
101
|
}>;
|
|
102
102
|
partialUpdate?: Callback<{
|
|
103
|
-
data:
|
|
103
|
+
data: ReActAgentIterationResultPartial;
|
|
104
104
|
update: {
|
|
105
|
-
key: keyof
|
|
105
|
+
key: keyof ReActIterationResult;
|
|
106
106
|
value: string;
|
|
107
107
|
parsedValue: unknown;
|
|
108
108
|
};
|
|
109
|
-
meta:
|
|
109
|
+
meta: ReActAgentUpdateMeta;
|
|
110
110
|
}>;
|
|
111
111
|
toolStart?: Callback<{
|
|
112
112
|
data: {
|
|
113
113
|
tool: AnyTool;
|
|
114
114
|
input: unknown;
|
|
115
115
|
options: BaseToolRunOptions;
|
|
116
|
-
iteration:
|
|
116
|
+
iteration: ReActAgentIterationToolResult;
|
|
117
117
|
};
|
|
118
|
-
meta:
|
|
118
|
+
meta: ReActAgentMeta;
|
|
119
119
|
}>;
|
|
120
120
|
toolSuccess?: Callback<{
|
|
121
121
|
data: {
|
|
@@ -123,9 +123,9 @@ interface BeeCallbacks {
|
|
|
123
123
|
input: unknown;
|
|
124
124
|
options: BaseToolRunOptions;
|
|
125
125
|
result: ToolOutput;
|
|
126
|
-
iteration:
|
|
126
|
+
iteration: ReActAgentIterationToolResult;
|
|
127
127
|
};
|
|
128
|
-
meta:
|
|
128
|
+
meta: ReActAgentMeta;
|
|
129
129
|
}>;
|
|
130
130
|
toolError?: Callback<{
|
|
131
131
|
data: {
|
|
@@ -133,32 +133,32 @@ interface BeeCallbacks {
|
|
|
133
133
|
input: unknown;
|
|
134
134
|
options: BaseToolRunOptions;
|
|
135
135
|
error: ToolError;
|
|
136
|
-
iteration:
|
|
136
|
+
iteration: ReActAgentIterationToolResult;
|
|
137
137
|
};
|
|
138
|
-
meta:
|
|
138
|
+
meta: ReActAgentMeta;
|
|
139
139
|
}>;
|
|
140
140
|
}
|
|
141
|
-
interface
|
|
142
|
-
system: typeof
|
|
143
|
-
assistant: typeof
|
|
144
|
-
user: typeof
|
|
145
|
-
userEmpty: typeof
|
|
146
|
-
toolError: typeof
|
|
147
|
-
toolInputError: typeof
|
|
148
|
-
toolNoResultError: typeof
|
|
149
|
-
toolNotFoundError: typeof
|
|
150
|
-
schemaError: typeof
|
|
141
|
+
interface ReActAgentTemplates {
|
|
142
|
+
system: typeof ReActAgentSystemPrompt;
|
|
143
|
+
assistant: typeof ReActAgentAssistantPrompt;
|
|
144
|
+
user: typeof ReActAgentUserPrompt;
|
|
145
|
+
userEmpty: typeof ReActAgentUserEmptyPrompt;
|
|
146
|
+
toolError: typeof ReActAgentToolErrorPrompt;
|
|
147
|
+
toolInputError: typeof ReActAgentToolInputErrorPrompt;
|
|
148
|
+
toolNoResultError: typeof ReActAgentToolNoResultsPrompt;
|
|
149
|
+
toolNotFoundError: typeof ReActAgentToolNotFoundPrompt;
|
|
150
|
+
schemaError: typeof ReActAgentSchemaErrorPrompt;
|
|
151
151
|
}
|
|
152
|
-
type
|
|
152
|
+
type ReActAgentParserInput = LinePrefixParser.define<{
|
|
153
153
|
thought: ZodParserField<string>;
|
|
154
154
|
tool_name: ZodParserField<string>;
|
|
155
155
|
tool_input: JSONParserField<Record<string, any>>;
|
|
156
156
|
tool_output: ZodParserField<string>;
|
|
157
157
|
final_answer: ZodParserField<string>;
|
|
158
158
|
}>;
|
|
159
|
-
type
|
|
160
|
-
type
|
|
161
|
-
type
|
|
162
|
-
type
|
|
159
|
+
type ReActAgentParser = LinePrefixParser<ReActAgentParserInput>;
|
|
160
|
+
type ReActIterationResult = LinePrefixParser.inferOutput<ReActAgentParser>;
|
|
161
|
+
type ReActAgentIterationResultPartial = LinePrefixParser.inferPartialOutput<ReActAgentParser>;
|
|
162
|
+
type ReActAgentIterationToolResult = NonUndefined<ReActIterationResult, "tool_input" | "tool_name">;
|
|
163
163
|
|
|
164
|
-
export type {
|
|
164
|
+
export type { ReActAgentCallbacks, ReActAgentExecutionConfig, ReActAgentIterationResultPartial, ReActAgentIterationToolResult, ReActAgentMeta, ReActAgentParserInput, ReActAgentRunInput, ReActAgentRunIteration, ReActAgentRunOptions, ReActAgentRunOutput, ReActAgentTemplates, ReActAgentUpdateMeta, ReActIterationResult };
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var base_cjs = require('../base.cjs');
|
|
4
|
+
var base_cjs$1 = require('../../tools/base.cjs');
|
|
5
|
+
var emitter_cjs = require('../../emitter/emitter.cjs');
|
|
6
|
+
var utils_cjs = require('../../serializer/utils.cjs');
|
|
7
|
+
var unconstrainedMemory_cjs = require('../../memory/unconstrainedMemory.cjs');
|
|
8
|
+
var message_cjs = require('../../backend/message.cjs');
|
|
9
|
+
var remeda = require('remeda');
|
|
10
|
+
var counter_cjs = require('../../internals/helpers/counter.cjs');
|
|
11
|
+
var object_cjs = require('../../internals/helpers/object.cjs');
|
|
12
|
+
var decoratorCache_cjs = require('../../cache/decoratorCache.cjs');
|
|
13
|
+
var template_cjs = require('../../template.cjs');
|
|
14
|
+
var prompts_cjs = require('./prompts.cjs');
|
|
15
|
+
var zod = require('zod');
|
|
16
|
+
|
|
17
|
+
var __defProp = Object.defineProperty;
|
|
18
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
19
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
20
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
21
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
22
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
23
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
24
|
+
}
|
|
25
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
26
|
+
function _ts_metadata(k, v) {
|
|
27
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
28
|
+
}
|
|
29
|
+
__name(_ts_metadata, "_ts_metadata");
|
|
30
|
+
class ToolCallingAgent extends base_cjs.BaseAgent {
|
|
31
|
+
static {
|
|
32
|
+
__name(this, "ToolCallingAgent");
|
|
33
|
+
}
|
|
34
|
+
input;
|
|
35
|
+
emitter;
|
|
36
|
+
constructor(input) {
|
|
37
|
+
super(), this.input = input, this.emitter = emitter_cjs.Emitter.root.child({
|
|
38
|
+
namespace: [
|
|
39
|
+
"agent",
|
|
40
|
+
"toolCalling"
|
|
41
|
+
],
|
|
42
|
+
creator: this
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
static {
|
|
46
|
+
this.register();
|
|
47
|
+
}
|
|
48
|
+
async _run(input, options = {}, run) {
|
|
49
|
+
const tempMessageKey = "tempMessage";
|
|
50
|
+
const execution = {
|
|
51
|
+
totalMaxRetries: 20,
|
|
52
|
+
...object_cjs.omitUndefined(this.input.execution ?? {}),
|
|
53
|
+
...object_cjs.omitUndefined(options.execution ?? {})
|
|
54
|
+
};
|
|
55
|
+
const state = {
|
|
56
|
+
memory: new unconstrainedMemory_cjs.UnconstrainedMemory(),
|
|
57
|
+
result: void 0,
|
|
58
|
+
iteration: 0
|
|
59
|
+
};
|
|
60
|
+
await state.memory.add(new message_cjs.SystemMessage(this.templates.system.render({
|
|
61
|
+
role: void 0,
|
|
62
|
+
instructions: void 0
|
|
63
|
+
})));
|
|
64
|
+
if (input.prompt) {
|
|
65
|
+
const userMessage = new message_cjs.UserMessage(this.templates.task.render({
|
|
66
|
+
prompt: input.prompt,
|
|
67
|
+
context: input.context,
|
|
68
|
+
expectedOutput: remeda.isString(input.expectedOutput) ? input.expectedOutput : void 0
|
|
69
|
+
}));
|
|
70
|
+
await state.memory.add(userMessage);
|
|
71
|
+
}
|
|
72
|
+
const globalRetriesCounter = new counter_cjs.RetryCounter(execution.totalMaxRetries || 1, base_cjs.AgentError);
|
|
73
|
+
while (!state.result) {
|
|
74
|
+
state.iteration++;
|
|
75
|
+
if (state.iteration > (execution.totalMaxRetries ?? Infinity)) {
|
|
76
|
+
throw new base_cjs.AgentError(`Agent was not able to resolve the task in ${state.iteration} iterations.`);
|
|
77
|
+
}
|
|
78
|
+
await run.emitter.emit("start", {
|
|
79
|
+
state
|
|
80
|
+
});
|
|
81
|
+
const response = await this.input.llm.create({
|
|
82
|
+
messages: state.memory.messages.slice(),
|
|
83
|
+
tools: this.input.tools,
|
|
84
|
+
stream: false
|
|
85
|
+
});
|
|
86
|
+
await state.memory.addMany(response.messages);
|
|
87
|
+
const toolCallMessages = response.getToolCalls();
|
|
88
|
+
for (const toolCall of toolCallMessages) {
|
|
89
|
+
try {
|
|
90
|
+
const tool = this.input.tools.find((tool2) => tool2.name === toolCall.toolName);
|
|
91
|
+
if (!tool) {
|
|
92
|
+
throw new base_cjs.AgentError(`Tool ${toolCall.toolName} does not exist!`);
|
|
93
|
+
}
|
|
94
|
+
const toolInput = toolCall.args;
|
|
95
|
+
const toolResponse = await tool.run(toolInput).context({
|
|
96
|
+
state,
|
|
97
|
+
toolCallMsg: toolCall
|
|
98
|
+
});
|
|
99
|
+
await state.memory.add(new message_cjs.ToolMessage({
|
|
100
|
+
type: "tool-result",
|
|
101
|
+
toolCallId: toolCall.toolCallId,
|
|
102
|
+
toolName: toolCall.toolName,
|
|
103
|
+
result: toolResponse.getTextContent(),
|
|
104
|
+
isError: false
|
|
105
|
+
}));
|
|
106
|
+
} catch (e) {
|
|
107
|
+
if (e instanceof base_cjs$1.ToolError) {
|
|
108
|
+
globalRetriesCounter.use(e);
|
|
109
|
+
await state.memory.add(new message_cjs.ToolMessage({
|
|
110
|
+
type: "tool-result",
|
|
111
|
+
toolCallId: toolCall.toolCallId,
|
|
112
|
+
toolName: toolCall.toolName,
|
|
113
|
+
result: e.explain(),
|
|
114
|
+
isError: true
|
|
115
|
+
}));
|
|
116
|
+
} else {
|
|
117
|
+
throw e;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const textMessages = response.getTextMessages();
|
|
122
|
+
if (remeda.isEmpty(toolCallMessages) && remeda.isEmpty(textMessages)) {
|
|
123
|
+
await state.memory.add(new message_cjs.AssistantMessage("\n", {
|
|
124
|
+
[tempMessageKey]: true
|
|
125
|
+
}));
|
|
126
|
+
} else {
|
|
127
|
+
await state.memory.deleteMany(state.memory.messages.filter((msg) => msg.meta[tempMessageKey]));
|
|
128
|
+
}
|
|
129
|
+
if (!remeda.isEmpty(textMessages) && remeda.isEmpty(toolCallMessages)) {
|
|
130
|
+
if (input.expectedOutput && input.expectedOutput instanceof zod.ZodSchema) {
|
|
131
|
+
const structured = await this.input.llm.createStructure({
|
|
132
|
+
schema: input.expectedOutput,
|
|
133
|
+
messages: state.memory.messages.slice()
|
|
134
|
+
});
|
|
135
|
+
state.result = new message_cjs.AssistantMessage(JSON.stringify(structured, null, 4));
|
|
136
|
+
} else {
|
|
137
|
+
state.result = message_cjs.AssistantMessage.fromChunks(textMessages);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
await run.emitter.emit("success", {
|
|
141
|
+
state
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
await this.memory.addMany(state.memory.messages.slice(1));
|
|
145
|
+
return {
|
|
146
|
+
memory: state.memory,
|
|
147
|
+
result: state.result
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
get meta() {
|
|
151
|
+
const tools = this.input.tools.slice();
|
|
152
|
+
if (this.input.meta) {
|
|
153
|
+
return {
|
|
154
|
+
...this.input.meta,
|
|
155
|
+
tools
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
name: "ToolCalling",
|
|
160
|
+
tools,
|
|
161
|
+
description: "ToolCallingAgent that uses tools to accomplish the task.",
|
|
162
|
+
...tools.length > 0 && {
|
|
163
|
+
extraDescription: [
|
|
164
|
+
`Tools that I can use to accomplish given task.`,
|
|
165
|
+
...tools.map((tool) => `Tool '${tool.name}': ${tool.description}.`)
|
|
166
|
+
].join("\n")
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
get templates() {
|
|
171
|
+
const overrides = this.input.templates ?? {};
|
|
172
|
+
const defaultTemplates = {
|
|
173
|
+
system: prompts_cjs.ToolCallingAgentSystemPrompt,
|
|
174
|
+
task: prompts_cjs.ToolCallingAgentTaskPrompt
|
|
175
|
+
};
|
|
176
|
+
return object_cjs.mapObj(defaultTemplates)((key, defaultTemplate) => {
|
|
177
|
+
const override = overrides[key] ?? defaultTemplate;
|
|
178
|
+
if (override instanceof template_cjs.PromptTemplate) {
|
|
179
|
+
return override;
|
|
180
|
+
}
|
|
181
|
+
return override(defaultTemplate) ?? defaultTemplate;
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
createSnapshot() {
|
|
185
|
+
return {
|
|
186
|
+
...super.createSnapshot(),
|
|
187
|
+
input: utils_cjs.shallowCopy(this.input),
|
|
188
|
+
emitter: this.emitter
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
set memory(memory) {
|
|
192
|
+
this.input.memory = memory;
|
|
193
|
+
}
|
|
194
|
+
get memory() {
|
|
195
|
+
return this.input.memory;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
_ts_decorate([
|
|
199
|
+
decoratorCache_cjs.Cache({
|
|
200
|
+
enumerable: false
|
|
201
|
+
}),
|
|
202
|
+
_ts_metadata("design:type", typeof ToolCallingAgentTemplates === "undefined" ? Object : ToolCallingAgentTemplates),
|
|
203
|
+
_ts_metadata("design:paramtypes", [])
|
|
204
|
+
], ToolCallingAgent.prototype, "templates", null);
|
|
205
|
+
|
|
206
|
+
exports.ToolCallingAgent = ToolCallingAgent;
|
|
207
|
+
//# sourceMappingURL=agent.cjs.map
|
|
208
|
+
//# sourceMappingURL=agent.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["agent.ts"],"names":["ToolCallingAgent","BaseAgent","emitter","constructor","input","Emitter","root","child","namespace","creator","register","_run","options","run","tempMessageKey","execution","totalMaxRetries","omitUndefined","state","memory","UnconstrainedMemory","result","undefined","iteration","add","SystemMessage","templates","system","render","role","instructions","prompt","userMessage","UserMessage","task","context","expectedOutput","isString","globalRetriesCounter","RetryCounter","AgentError","Infinity","emit","response","llm","create","messages","slice","tools","stream","addMany","toolCallMessages","getToolCalls","toolCall","tool","find","name","toolName","toolInput","args","toolResponse","toolCallMsg","ToolMessage","type","toolCallId","getTextContent","isError","e","ToolError","use","explain","textMessages","getTextMessages","isEmpty","AssistantMessage","deleteMany","filter","msg","meta","ZodSchema","structured","createStructure","schema","JSON","stringify","fromChunks","description","length","extraDescription","map","join","overrides","defaultTemplates","ToolCallingAgentSystemPrompt","ToolCallingAgentTaskPrompt","mapObj","key","defaultTemplate","override","PromptTemplate","createSnapshot","shallowCopy","enumerable"],"mappings":";;;;;;;;;;;;;;;;;;AAcC,SAAA,YAAA,CAAA,UAAA,EAAA,MAAA,EAAA,GAAA,EAAA,IAAA,EAAA;;;;;;AAAA,MAAA,CAAA,YAAA,EAAA,cAAA,CAAA;;;;;AAiDM,MAAMA,yBAAyBC,kBAAAA,CAAAA;EA/DtC;;;;AAoEkBC,EAAAA,OAAAA;AAKhBC,EAAAA,WAAAA,CAA4BC,KAA8B,EAAA;AACxD,IAAK,KAAA,EAAA,EAAA,KADqBA,KAAAA,GAAAA,KAAAA,EAAAA,KALZF,OAAUG,GAAAA,mBAAAA,CAAQC,KAAKC,KAAiC,CAAA;MACtEC,SAAW,EAAA;AAAC,QAAA,OAAA;AAAS,QAAA;;MACrBC,OAAS,EAAA;KACX,CAAA;AAIA;EAEA;AACE,IAAA,IAAA,CAAKC,QAAQ,EAAA;AACf;AAEA,EAAA,MAAgBC,IACdP,CAAAA,KAAAA,EACAQ,OAAsC,GAAA,IACtCC,GACoC,EAAA;AACpC,IAAA,MAAMC,cAAiB,GAAA,aAAA;AACvB,IAAA,MAAMC,SAAY,GAAA;MAEhBC,eAAiB,EAAA,EAAA;MAEjB,GAAGC,wBAAc,CAAA,IAAA,CAAKb,KAAMW,CAAAA,SAAAA,IAAa,EAAC,CAAA;AAC1C,MAAA,GAAGE,wBAAcL,CAAAA,OAAAA,CAAQG,SAAa,IAAA,EAAC;AACzC,KAAA;AAEA,IAAA,MAAMG,KAAkC,GAAA;AACtCC,MAAAA,MAAAA,EAAQ,IAAIC,2CAAAA,EAAAA;MACZC,MAAQC,EAAAA,MAAAA;MACRC,SAAW,EAAA;AACb,KAAA;AACA,I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2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { AgentError, BaseAgent } from \"@/agents/base.js\";\nimport { AnyTool, ToolError, ToolOutput } from \"@/tools/base.js\";\nimport { BaseMemory } from \"@/memory/base.js\";\nimport { AgentMeta } from \"@/agents/types.js\";\nimport { Emitter } from \"@/emitter/emitter.js\";\nimport type {\n ToolCallingAgentExecutionConfig,\n ToolCallingAgentTemplates,\n ToolCallingAgentCallbacks,\n ToolCallingAgentRunInput,\n ToolCallingAgentRunOptions,\n ToolCallingAgentRunOutput,\n ToolCallingAgentRunState,\n} from \"@/agents/toolCalling/types.js\";\nimport { GetRunContext } from \"@/context.js\";\nimport { ChatModel } from \"@/backend/chat.js\";\nimport { shallowCopy } from \"@/serializer/utils.js\";\nimport { UnconstrainedMemory } from \"@/memory/unconstrainedMemory.js\";\nimport { AssistantMessage, SystemMessage, ToolMessage, UserMessage } from \"@/backend/message.js\";\nimport { isEmpty, isString } from \"remeda\";\nimport { RetryCounter } from \"@/internals/helpers/counter.js\";\nimport { mapObj, omitUndefined } from \"@/internals/helpers/object.js\";\nimport { Cache } from \"@/cache/decoratorCache.js\";\nimport { PromptTemplate } from \"@/template.js\";\nimport {\n ToolCallingAgentSystemPrompt,\n ToolCallingAgentTaskPrompt,\n} from \"@/agents/toolCalling/prompts.js\";\nimport { ZodSchema } from \"zod\";\n\nexport type ToolCallingAgentTemplateFactory<K extends keyof ToolCallingAgentTemplates> = (\n template: ToolCallingAgentTemplates[K],\n) => ToolCallingAgentTemplates[K];\n\nexport interface ToolCallingAgentInput {\n llm: ChatModel;\n memory: BaseMemory;\n tools: AnyTool[];\n meta?: Omit<AgentMeta, \"tools\">;\n templates?: Partial<{\n [K in keyof ToolCallingAgentTemplates]:\n | ToolCallingAgentTemplates[K]\n | ToolCallingAgentTemplateFactory<K>;\n }>;\n execution?: ToolCallingAgentExecutionConfig;\n}\n\nexport class ToolCallingAgent extends BaseAgent<\n ToolCallingAgentRunInput,\n ToolCallingAgentRunOutput,\n ToolCallingAgentRunOptions\n> {\n public readonly emitter = Emitter.root.child<ToolCallingAgentCallbacks>({\n namespace: [\"agent\", \"toolCalling\"],\n creator: this,\n });\n\n constructor(public readonly input: ToolCallingAgentInput) {\n super();\n }\n\n static {\n this.register();\n }\n\n protected async _run(\n input: ToolCallingAgentRunInput,\n options: ToolCallingAgentRunOptions = {},\n run: GetRunContext<typeof this>,\n ): Promise<ToolCallingAgentRunOutput> {\n const tempMessageKey = \"tempMessage\" as const;\n const execution = {\n maxRetriesPerStep: 3,\n totalMaxRetries: 20,\n maxIterations: 10,\n ...omitUndefined(this.input.execution ?? {}),\n ...omitUndefined(options.execution ?? {}),\n };\n\n const state: ToolCallingAgentRunState = {\n memory: new UnconstrainedMemory(),\n result: undefined,\n iteration: 0,\n };\n await state.memory.add(\n new SystemMessage(\n this.templates.system.render({\n role: undefined,\n instructions: undefined,\n }),\n ),\n );\n\n if (input.prompt) {\n const userMessage = new UserMessage(\n this.templates.task.render({\n prompt: input.prompt,\n context: input.context,\n expectedOutput: isString(input.expectedOutput) ? input.expectedOutput : undefined,\n }),\n );\n await state.memory.add(userMessage);\n }\n\n const globalRetriesCounter = new RetryCounter(execution.totalMaxRetries || 1, AgentError);\n\n while (!state.result) {\n state.iteration++;\n if (state.iteration > (execution.totalMaxRetries ?? Infinity)) {\n throw new AgentError(\n `Agent was not able to resolve the task in ${state.iteration} iterations.`,\n );\n }\n\n await run.emitter.emit(\"start\", { state });\n const response = await this.input.llm.create({\n messages: state.memory.messages.slice(),\n tools: this.input.tools,\n stream: false,\n });\n await state.memory.addMany(response.messages);\n\n const toolCallMessages = response.getToolCalls();\n for (const toolCall of toolCallMessages) {\n try {\n const tool = this.input.tools.find((tool) => tool.name === toolCall.toolName);\n if (!tool) {\n throw new AgentError(`Tool ${toolCall.toolName} does not exist!`);\n }\n\n const toolInput: any = toolCall.args;\n const toolResponse: ToolOutput = await tool.run(toolInput).context({\n state,\n toolCallMsg: toolCall,\n });\n await state.memory.add(\n new ToolMessage({\n type: \"tool-result\",\n toolCallId: toolCall.toolCallId,\n toolName: toolCall.toolName,\n result: toolResponse.getTextContent(),\n isError: false,\n }),\n );\n } catch (e) {\n if (e instanceof ToolError) {\n globalRetriesCounter.use(e);\n await state.memory.add(\n new ToolMessage({\n type: \"tool-result\",\n toolCallId: toolCall.toolCallId,\n toolName: toolCall.toolName,\n result: e.explain(),\n isError: true,\n }),\n );\n } else {\n throw e;\n }\n }\n }\n\n // handle empty messages for some models\n const textMessages = response.getTextMessages();\n if (isEmpty(toolCallMessages) && isEmpty(textMessages)) {\n await state.memory.add(new AssistantMessage(\"\\n\", { [tempMessageKey]: true }));\n } else {\n await state.memory.deleteMany(\n state.memory.messages.filter((msg) => msg.meta[tempMessageKey]),\n );\n }\n\n if (!isEmpty(textMessages) && isEmpty(toolCallMessages)) {\n if (input.expectedOutput && input.expectedOutput instanceof ZodSchema) {\n const structured = await this.input.llm.createStructure({\n schema: input.expectedOutput,\n messages: state.memory.messages.slice(),\n });\n state.result = new AssistantMessage(JSON.stringify(structured, null, 4));\n } else {\n state.result = AssistantMessage.fromChunks(textMessages);\n }\n }\n\n await run.emitter.emit(\"success\", { state });\n }\n\n await this.memory.addMany(state.memory.messages.slice(1));\n return { memory: state.memory, result: state.result };\n }\n\n get meta(): AgentMeta {\n const tools = this.input.tools.slice();\n\n if (this.input.meta) {\n return { ...this.input.meta, tools };\n }\n\n return {\n name: \"ToolCalling\",\n tools,\n description: \"ToolCallingAgent that uses tools to accomplish the task.\",\n ...(tools.length > 0 && {\n extraDescription: [\n `Tools that I can use to accomplish given task.`,\n ...tools.map((tool) => `Tool '${tool.name}': ${tool.description}.`),\n ].join(\"\\n\"),\n }),\n };\n }\n\n @Cache({ enumerable: false })\n protected get templates(): ToolCallingAgentTemplates {\n const overrides = this.input.templates ?? {};\n const defaultTemplates: ToolCallingAgentTemplates = {\n system: ToolCallingAgentSystemPrompt,\n task: ToolCallingAgentTaskPrompt,\n } as const;\n\n return mapObj(defaultTemplates)(\n (key, defaultTemplate: ToolCallingAgentTemplates[typeof key]) => {\n const override = overrides[key] ?? defaultTemplate;\n if (override instanceof PromptTemplate) {\n return override;\n }\n return override(defaultTemplate) ?? defaultTemplate;\n },\n );\n }\n\n createSnapshot() {\n return {\n ...super.createSnapshot(),\n input: shallowCopy(this.input),\n emitter: this.emitter,\n };\n }\n\n set memory(memory: BaseMemory) {\n this.input.memory = memory;\n }\n\n get memory() {\n return this.input.memory;\n }\n}\n"]}
|