@ellyco/agentic 0.1.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/LICENSE +21 -0
- package/README.md +560 -0
- package/dist/graphs/graph.d.ts +218 -0
- package/dist/graphs/graph.d.ts.map +1 -0
- package/dist/graphs/graph.js +334 -0
- package/dist/graphs/graph.js.map +1 -0
- package/dist/graphs/index.d.ts +7 -0
- package/dist/graphs/index.d.ts.map +1 -0
- package/dist/graphs/index.js +15 -0
- package/dist/graphs/index.js.map +1 -0
- package/dist/graphs/iterator.d.ts +138 -0
- package/dist/graphs/iterator.d.ts.map +1 -0
- package/dist/graphs/iterator.js +184 -0
- package/dist/graphs/iterator.js.map +1 -0
- package/dist/graphs/merge-state.d.ts +22 -0
- package/dist/graphs/merge-state.d.ts.map +1 -0
- package/dist/graphs/merge-state.js +56 -0
- package/dist/graphs/merge-state.js.map +1 -0
- package/dist/graphs/node-sequence.d.ts +63 -0
- package/dist/graphs/node-sequence.d.ts.map +1 -0
- package/dist/graphs/node-sequence.js +84 -0
- package/dist/graphs/node-sequence.js.map +1 -0
- package/dist/graphs/registry.d.ts +5 -0
- package/dist/graphs/registry.d.ts.map +1 -0
- package/dist/graphs/registry.js +6 -0
- package/dist/graphs/registry.js.map +1 -0
- package/dist/graphs/runtime-context.d.ts +189 -0
- package/dist/graphs/runtime-context.d.ts.map +1 -0
- package/dist/graphs/runtime-context.js +254 -0
- package/dist/graphs/runtime-context.js.map +1 -0
- package/dist/graphs/state-machine.d.ts +105 -0
- package/dist/graphs/state-machine.d.ts.map +1 -0
- package/dist/graphs/state-machine.js +130 -0
- package/dist/graphs/state-machine.js.map +1 -0
- package/dist/graphs/store/base-store.d.ts +90 -0
- package/dist/graphs/store/base-store.d.ts.map +1 -0
- package/dist/graphs/store/base-store.js +50 -0
- package/dist/graphs/store/base-store.js.map +1 -0
- package/dist/graphs/store/sqlite-store.d.ts +88 -0
- package/dist/graphs/store/sqlite-store.d.ts.map +1 -0
- package/dist/graphs/store/sqlite-store.js +109 -0
- package/dist/graphs/store/sqlite-store.js.map +1 -0
- package/dist/graphs/store/stored-run.d.ts +77 -0
- package/dist/graphs/store/stored-run.d.ts.map +1 -0
- package/dist/graphs/store/stored-run.js +88 -0
- package/dist/graphs/store/stored-run.js.map +1 -0
- package/dist/graphs/types.d.ts +15 -0
- package/dist/graphs/types.d.ts.map +1 -0
- package/dist/graphs/types.js +3 -0
- package/dist/graphs/types.js.map +1 -0
- package/dist/messages/index.d.ts +6 -0
- package/dist/messages/index.d.ts.map +1 -0
- package/dist/messages/index.js +19 -0
- package/dist/messages/index.js.map +1 -0
- package/dist/messages/message.d.ts +143 -0
- package/dist/messages/message.d.ts.map +1 -0
- package/dist/messages/message.js +172 -0
- package/dist/messages/message.js.map +1 -0
- package/dist/messages/tool.d.ts +160 -0
- package/dist/messages/tool.d.ts.map +1 -0
- package/dist/messages/tool.js +173 -0
- package/dist/messages/tool.js.map +1 -0
- package/dist/models/BaseModel.d.ts +232 -0
- package/dist/models/BaseModel.d.ts.map +1 -0
- package/dist/models/BaseModel.js +247 -0
- package/dist/models/BaseModel.js.map +1 -0
- package/dist/models/BedrockModel.d.ts +112 -0
- package/dist/models/BedrockModel.d.ts.map +1 -0
- package/dist/models/BedrockModel.js +315 -0
- package/dist/models/BedrockModel.js.map +1 -0
- package/dist/models/TestModel.d.ts +135 -0
- package/dist/models/TestModel.d.ts.map +1 -0
- package/dist/models/TestModel.js +191 -0
- package/dist/models/TestModel.js.map +1 -0
- package/dist/nodes/function-node.d.ts +59 -0
- package/dist/nodes/function-node.d.ts.map +1 -0
- package/dist/nodes/function-node.js +72 -0
- package/dist/nodes/function-node.js.map +1 -0
- package/dist/nodes/index.d.ts +4 -0
- package/dist/nodes/index.d.ts.map +1 -0
- package/dist/nodes/index.js +9 -0
- package/dist/nodes/index.js.map +1 -0
- package/dist/nodes/interrupt-node.d.ts +51 -0
- package/dist/nodes/interrupt-node.d.ts.map +1 -0
- package/dist/nodes/interrupt-node.js +65 -0
- package/dist/nodes/interrupt-node.js.map +1 -0
- package/dist/nodes/model-node.d.ts +72 -0
- package/dist/nodes/model-node.d.ts.map +1 -0
- package/dist/nodes/model-node.js +80 -0
- package/dist/nodes/model-node.js.map +1 -0
- package/dist/nodes/types.d.ts +5 -0
- package/dist/nodes/types.d.ts.map +1 -0
- package/dist/nodes/types.js +3 -0
- package/dist/nodes/types.js.map +1 -0
- package/dist/tools.d.ts +65 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +56 -0
- package/dist/tools.js.map +1 -0
- package/dist/types.d.ts +17 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +32 -0
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BedrockModel = void 0;
|
|
4
|
+
const BaseModel_1 = require("./BaseModel");
|
|
5
|
+
const client_bedrock_runtime_1 = require("@aws-sdk/client-bedrock-runtime");
|
|
6
|
+
const messages_1 = require("../messages");
|
|
7
|
+
const z = require("zod");
|
|
8
|
+
/**
|
|
9
|
+
* Model implementation using AWS Bedrock as the backend.
|
|
10
|
+
* Handles communication with Bedrock API for inference, tools, and structured output.
|
|
11
|
+
*
|
|
12
|
+
* @class BedrockModel
|
|
13
|
+
* @extends {BaseModel}
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const model = new BedrockModel({
|
|
18
|
+
* modelId: "anthropic.claude-3-sonnet-20240229-v1:0",
|
|
19
|
+
* temperature: 0.7,
|
|
20
|
+
* maxTokens: 2048
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* model.withSystemMessage("You are a helpful assistant")
|
|
24
|
+
* .withTools([searchTool]);
|
|
25
|
+
*
|
|
26
|
+
* const response = await model.invoke([userMessage]);
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
class BedrockModel extends BaseModel_1.BaseModel {
|
|
30
|
+
modelId;
|
|
31
|
+
client;
|
|
32
|
+
/**
|
|
33
|
+
* Creates a new Bedrock model instance.
|
|
34
|
+
*
|
|
35
|
+
* @param {BedrockModelConfig} config - Configuration including model ID and AWS settings
|
|
36
|
+
*/
|
|
37
|
+
constructor(config) {
|
|
38
|
+
super(config);
|
|
39
|
+
this.modelId = config.modelId;
|
|
40
|
+
this.client = new client_bedrock_runtime_1.BedrockRuntimeClient(config.aws ?? {});
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Converts internal message format to Bedrock API message format.
|
|
44
|
+
* Handles BaseMessages (user, agent, system) and ToolUse messages (requests, responses, errors).
|
|
45
|
+
*
|
|
46
|
+
* @private
|
|
47
|
+
* @param {ModelMessages[]} messages - Messages to convert
|
|
48
|
+
* @returns {Message[]} Messages in Bedrock format
|
|
49
|
+
*/
|
|
50
|
+
convertMessagesToBedrockMessages(messages) {
|
|
51
|
+
const bedrockMessages = [];
|
|
52
|
+
for (const message of messages) {
|
|
53
|
+
if (message instanceof messages_1.BaseMessage) {
|
|
54
|
+
const role = message.role;
|
|
55
|
+
if (role === messages_1.MessageRole.SYSTEM && !this.systemMessage) {
|
|
56
|
+
this.systemMessage = message;
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
bedrockMessages.push({
|
|
60
|
+
role: role === messages_1.MessageRole.USER
|
|
61
|
+
? client_bedrock_runtime_1.ConversationRole.USER
|
|
62
|
+
: client_bedrock_runtime_1.ConversationRole.ASSISTANT,
|
|
63
|
+
content: [
|
|
64
|
+
{
|
|
65
|
+
...(message.hasText()
|
|
66
|
+
? { text: message.text }
|
|
67
|
+
: {}),
|
|
68
|
+
},
|
|
69
|
+
],
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (message instanceof messages_1.ToolUse) {
|
|
74
|
+
if (message instanceof messages_1.ToolRequest) {
|
|
75
|
+
bedrockMessages.push({
|
|
76
|
+
role: client_bedrock_runtime_1.ConversationRole.ASSISTANT,
|
|
77
|
+
content: [
|
|
78
|
+
{
|
|
79
|
+
toolUse: {
|
|
80
|
+
toolUseId: message.toolUseId,
|
|
81
|
+
name: message.toolName,
|
|
82
|
+
input: message.input,
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
],
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
if (message instanceof messages_1.ToolResponse) {
|
|
89
|
+
bedrockMessages.push({
|
|
90
|
+
role: client_bedrock_runtime_1.ConversationRole.USER,
|
|
91
|
+
content: [
|
|
92
|
+
{
|
|
93
|
+
toolResult: {
|
|
94
|
+
toolUseId: message.toolUseId,
|
|
95
|
+
content: [
|
|
96
|
+
{
|
|
97
|
+
json: message.output,
|
|
98
|
+
},
|
|
99
|
+
],
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
],
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
if (message instanceof messages_1.ToolError) {
|
|
106
|
+
bedrockMessages.push({
|
|
107
|
+
role: client_bedrock_runtime_1.ConversationRole.USER,
|
|
108
|
+
content: [
|
|
109
|
+
{
|
|
110
|
+
toolResult: {
|
|
111
|
+
toolUseId: message.toolUseId,
|
|
112
|
+
content: [
|
|
113
|
+
{
|
|
114
|
+
text: message.error,
|
|
115
|
+
},
|
|
116
|
+
],
|
|
117
|
+
status: client_bedrock_runtime_1.ToolResultStatus.ERROR,
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return bedrockMessages;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Converts a tool definition to Bedrock tool specification format.
|
|
129
|
+
*
|
|
130
|
+
* @private
|
|
131
|
+
* @param {ToolDefinition} tool - The tool definition to convert
|
|
132
|
+
* @returns {Tool} Bedrock formatted tool specification
|
|
133
|
+
*/
|
|
134
|
+
convertToolDefinitionToBedrockTool(tool) {
|
|
135
|
+
return {
|
|
136
|
+
toolSpec: {
|
|
137
|
+
name: tool.name,
|
|
138
|
+
description: tool.description,
|
|
139
|
+
inputSchema: {
|
|
140
|
+
json: z.toJSONSchema(tool.schema),
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Builds the tool configuration for Bedrock.
|
|
147
|
+
* For structured output, creates a single "output" tool that captures the schema.
|
|
148
|
+
* For regular tools, converts all tool definitions.
|
|
149
|
+
*
|
|
150
|
+
* @private
|
|
151
|
+
* @returns {ToolConfiguration} Tool configuration for Bedrock
|
|
152
|
+
*/
|
|
153
|
+
getBedrockToolConfig() {
|
|
154
|
+
if (this.structuredOutput) {
|
|
155
|
+
return {
|
|
156
|
+
tools: [
|
|
157
|
+
{
|
|
158
|
+
toolSpec: {
|
|
159
|
+
name: "output",
|
|
160
|
+
description: "Expected output from the model",
|
|
161
|
+
inputSchema: {
|
|
162
|
+
json: z.toJSONSchema(this.structuredOutput),
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
],
|
|
167
|
+
toolChoice: {
|
|
168
|
+
tool: { name: "output" },
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
return {
|
|
173
|
+
tools: this.tools.map((tool) => this.convertToolDefinitionToBedrockTool(tool)),
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Maps Bedrock stop reason to internal stop reason enum.
|
|
178
|
+
*
|
|
179
|
+
* @private
|
|
180
|
+
* @param {ConverseCommandOutput} response - The Bedrock API response
|
|
181
|
+
* @returns {InvokeResponseStopReason} Mapped stop reason
|
|
182
|
+
*/
|
|
183
|
+
mapStopReason(response) {
|
|
184
|
+
switch (response.stopReason) {
|
|
185
|
+
case client_bedrock_runtime_1.StopReason.STOP_SEQUENCE:
|
|
186
|
+
return BaseModel_1.InvokeResponseStopReason.STOP_SEQUENCE;
|
|
187
|
+
case client_bedrock_runtime_1.StopReason.MAX_TOKENS:
|
|
188
|
+
return BaseModel_1.InvokeResponseStopReason.MAX_TOKENS;
|
|
189
|
+
case client_bedrock_runtime_1.StopReason.END_TURN:
|
|
190
|
+
return BaseModel_1.InvokeResponseStopReason.END_TURN;
|
|
191
|
+
case client_bedrock_runtime_1.StopReason.TOOL_USE:
|
|
192
|
+
return BaseModel_1.InvokeResponseStopReason.TOOL_USE;
|
|
193
|
+
default:
|
|
194
|
+
return BaseModel_1.InvokeResponseStopReason.UNKNOWN;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Builds inference configuration chunk with temperature, topP, and maxTokens if set.
|
|
199
|
+
*
|
|
200
|
+
* @private
|
|
201
|
+
* @returns {{inferenceConfig?: InferenceConfiguration}} Configuration chunk or empty object
|
|
202
|
+
*/
|
|
203
|
+
getInferenceConfigChunk() {
|
|
204
|
+
const inferenceConfig = {
|
|
205
|
+
...(this.temperature ? { temperature: this.temperature } : {}),
|
|
206
|
+
...(this.topP ? { topP: this.topP } : {}),
|
|
207
|
+
...(this.maxTokens ? { maxTokens: this.maxTokens } : {}),
|
|
208
|
+
};
|
|
209
|
+
if (Object.keys(inferenceConfig).length > 0) {
|
|
210
|
+
return { inferenceConfig };
|
|
211
|
+
}
|
|
212
|
+
return {};
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Builds system message chunk.
|
|
216
|
+
* For structured output, adds a directive to only call the output tool.
|
|
217
|
+
*
|
|
218
|
+
* @private
|
|
219
|
+
* @returns {{system?: SystemContentBlock[]}} System messages or empty object
|
|
220
|
+
*/
|
|
221
|
+
getSystemMessageChunk() {
|
|
222
|
+
if (this.systemMessage) {
|
|
223
|
+
return {
|
|
224
|
+
system: [
|
|
225
|
+
{
|
|
226
|
+
text: this.systemMessage.toString(),
|
|
227
|
+
},
|
|
228
|
+
...(this.structuredOutput
|
|
229
|
+
? [
|
|
230
|
+
{
|
|
231
|
+
text: "IMPORTANT: ONLY call the output tool. No other messages!",
|
|
232
|
+
},
|
|
233
|
+
]
|
|
234
|
+
: []),
|
|
235
|
+
],
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
else if (this.structuredOutput) {
|
|
239
|
+
return {
|
|
240
|
+
system: [
|
|
241
|
+
{
|
|
242
|
+
text: "IMPORTANT: ONLY call the output tool. No other messages!",
|
|
243
|
+
},
|
|
244
|
+
],
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
return {};
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Builds tool configuration chunk.
|
|
251
|
+
*
|
|
252
|
+
* @private
|
|
253
|
+
* @returns {{toolConfig?: ToolConfiguration}} Tool configuration or empty object
|
|
254
|
+
*/
|
|
255
|
+
getToolConfigChunk() {
|
|
256
|
+
const toolConfig = this.getBedrockToolConfig();
|
|
257
|
+
if (toolConfig.tools && toolConfig.tools.length > 0) {
|
|
258
|
+
return {
|
|
259
|
+
toolConfig,
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
return {};
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Executes the model inference with Bedrock API.
|
|
266
|
+
* Sends messages to Bedrock, processes the response, and converts it to internal format.
|
|
267
|
+
*
|
|
268
|
+
* @protected
|
|
269
|
+
* @param {ModelMessages[]} inputMessages - Messages to send to the model
|
|
270
|
+
* @returns {Promise<InvokeResponse>} The model's response with messages and usage stats
|
|
271
|
+
*/
|
|
272
|
+
async runModel(inputMessages) {
|
|
273
|
+
const bedrockMessages = this.convertMessagesToBedrockMessages(inputMessages);
|
|
274
|
+
const command = new client_bedrock_runtime_1.ConverseCommand({
|
|
275
|
+
modelId: this.modelId,
|
|
276
|
+
...this.getInferenceConfigChunk(),
|
|
277
|
+
messages: bedrockMessages,
|
|
278
|
+
...this.getSystemMessageChunk(),
|
|
279
|
+
...this.getToolConfigChunk(),
|
|
280
|
+
});
|
|
281
|
+
const response = await this.client.send(command);
|
|
282
|
+
const message = response.output?.message;
|
|
283
|
+
const usage = {
|
|
284
|
+
inputTokens: response.usage?.inputTokens ?? 0,
|
|
285
|
+
outputTokens: response.usage?.outputTokens ?? 0,
|
|
286
|
+
};
|
|
287
|
+
const stopReason = this.mapStopReason(response);
|
|
288
|
+
if (!message || !message.content) {
|
|
289
|
+
return {
|
|
290
|
+
messages: [],
|
|
291
|
+
usage,
|
|
292
|
+
stopReason,
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
const messages = [];
|
|
296
|
+
for (const block of message.content) {
|
|
297
|
+
if ("text" in block) {
|
|
298
|
+
messages.push(new messages_1.AgentMessage(block.text));
|
|
299
|
+
}
|
|
300
|
+
else if ("toolUse" in block) {
|
|
301
|
+
messages.push(new messages_1.ToolRequest(block.toolUse.toolUseId, block.toolUse.name, block.toolUse.input));
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
console.warn("Unknown block type", block);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
return {
|
|
308
|
+
messages,
|
|
309
|
+
usage,
|
|
310
|
+
stopReason,
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
exports.BedrockModel = BedrockModel;
|
|
315
|
+
//# sourceMappingURL=BedrockModel.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BedrockModel.js","sourceRoot":"","sources":["../../src/models/BedrockModel.ts"],"names":[],"mappings":";;;AAAA,2CAMqB;AACrB,4EAcyC;AACzC,0CASqB;AACrB,yBAAyB;AAgBzB;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAa,YAAa,SAAQ,qBAAS;IAC/B,OAAO,CAAS;IAChB,MAAM,CAAuB;IAErC;;;;OAIG;IACH,YAAY,MAA0B;QAClC,KAAK,CAAC,MAAM,CAAC,CAAC;QACd,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,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;YACL,UAAU;SACb,CAAC;IACN,CAAC;CACJ;AA/TD,oCA+TC"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { ToolDefinition } from "../tools";
|
|
2
|
+
import { BaseModel, BaseModelConfig, InvokeResponse } from "./BaseModel";
|
|
3
|
+
import * as z from "zod";
|
|
4
|
+
import { AgentMessage, ModelMessages, ToolRequest } from "../messages";
|
|
5
|
+
/**
|
|
6
|
+
* Configuration for test model responses.
|
|
7
|
+
* Allows specifying what the model should respond with for specific inputs.
|
|
8
|
+
*
|
|
9
|
+
* @class TestResponseConfig
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const config = new TestResponseConfig()
|
|
14
|
+
* .userSends([userMessage])
|
|
15
|
+
* .respondWith([agentMessage]);
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare class TestResponseConfig {
|
|
19
|
+
/**
|
|
20
|
+
* Tools that should be available when matching this config
|
|
21
|
+
*/
|
|
22
|
+
toolsIncluded: ToolDefinition[];
|
|
23
|
+
/**
|
|
24
|
+
* Messages the model should respond with
|
|
25
|
+
*/
|
|
26
|
+
responseMessages: (AgentMessage | ToolRequest)[];
|
|
27
|
+
/**
|
|
28
|
+
* Input messages that trigger this config
|
|
29
|
+
*/
|
|
30
|
+
inputMessages: ModelMessages[];
|
|
31
|
+
/**
|
|
32
|
+
* Structured output schema for this config (if using structured output)
|
|
33
|
+
*/
|
|
34
|
+
structuredOutput?: z.ZodSchema<any>;
|
|
35
|
+
/**
|
|
36
|
+
* Specifies what messages the model should respond with.
|
|
37
|
+
*
|
|
38
|
+
* @param {(AgentMessage | ToolRequest)[]} messages - Response messages
|
|
39
|
+
* @returns {this} The config instance for method chaining
|
|
40
|
+
*/
|
|
41
|
+
respondWith(messages: (AgentMessage | ToolRequest)[]): this;
|
|
42
|
+
/**
|
|
43
|
+
* Specifies which tools should be included when matching this config.
|
|
44
|
+
*
|
|
45
|
+
* @param {ToolDefinition[]} tools - Tools to include
|
|
46
|
+
* @returns {this} The config instance for method chaining
|
|
47
|
+
*/
|
|
48
|
+
includedTools(tools: ToolDefinition[]): this;
|
|
49
|
+
/**
|
|
50
|
+
* Specifies the structured output schema for this config.
|
|
51
|
+
*
|
|
52
|
+
* @param {z.ZodSchema<any>} schema - The output schema
|
|
53
|
+
* @returns {this} The config instance for method chaining
|
|
54
|
+
*/
|
|
55
|
+
providedStructuredOutput(schema: z.ZodSchema<any>): this;
|
|
56
|
+
/**
|
|
57
|
+
* Specifies the input messages that should trigger this config.
|
|
58
|
+
*
|
|
59
|
+
* @param {ModelMessages[]} messages - Input messages to match
|
|
60
|
+
* @returns {this} The config instance for method chaining
|
|
61
|
+
*/
|
|
62
|
+
userSends(messages: ModelMessages[]): this;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* A mock model implementation for testing purposes.
|
|
66
|
+
* Allows you to specify predefined responses for specific message inputs.
|
|
67
|
+
* Useful for testing agent behavior without hitting real model APIs.
|
|
68
|
+
*
|
|
69
|
+
* @class TestModel
|
|
70
|
+
* @extends {BaseModel}
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* const testModel = new TestModel({ temperature: 0.7 });
|
|
75
|
+
*
|
|
76
|
+
* // Configure a response
|
|
77
|
+
* const config = new TestResponseConfig()
|
|
78
|
+
* .userSends([new UserMessage("Hello")])
|
|
79
|
+
* .respondWith([new AgentMessage("Hi there!")]);
|
|
80
|
+
*
|
|
81
|
+
* testModel.addTestConfig(config);
|
|
82
|
+
*
|
|
83
|
+
* // When invoked with matching messages, returns the configured response
|
|
84
|
+
* const response = await testModel.invoke([new UserMessage("Hello")]);
|
|
85
|
+
* // response.messages[0].text === "Hi there!"
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
export declare class TestModel extends BaseModel {
|
|
89
|
+
/**
|
|
90
|
+
* Array of configured test responses
|
|
91
|
+
*/
|
|
92
|
+
testConfigs: TestResponseConfig[];
|
|
93
|
+
/**
|
|
94
|
+
* Creates a new test model instance.
|
|
95
|
+
*
|
|
96
|
+
* @param {BaseModelConfig} config - Model configuration
|
|
97
|
+
*/
|
|
98
|
+
constructor(config: BaseModelConfig);
|
|
99
|
+
/**
|
|
100
|
+
* Adds a test configuration for a specific input-output pair.
|
|
101
|
+
*
|
|
102
|
+
* @param {TestResponseConfig} config - The test configuration
|
|
103
|
+
* @returns {this} The model instance for method chaining
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```typescript
|
|
107
|
+
* model.addTestConfig(
|
|
108
|
+
* new TestResponseConfig()
|
|
109
|
+
* .userSends([userMsg])
|
|
110
|
+
* .respondWith([agentMsg])
|
|
111
|
+
* );
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
addTestConfig(config: TestResponseConfig): this;
|
|
115
|
+
/**
|
|
116
|
+
* Finds a matching test configuration for the given messages.
|
|
117
|
+
* Matching is based on message content, tools, and structured output schema.
|
|
118
|
+
*
|
|
119
|
+
* @param {ModelMessages[]} messages - Messages to match against
|
|
120
|
+
* @returns {TestResponseConfig | undefined} The matching config, or undefined if no match
|
|
121
|
+
*
|
|
122
|
+
* @private
|
|
123
|
+
*/
|
|
124
|
+
findMatchingConfig(messages: ModelMessages[]): TestResponseConfig | undefined;
|
|
125
|
+
/**
|
|
126
|
+
* Executes the test model by finding and returning a matching configuration.
|
|
127
|
+
*
|
|
128
|
+
* @protected
|
|
129
|
+
* @param {ModelMessages[]} messages - Messages to respond to
|
|
130
|
+
* @returns {Promise<InvokeResponse>} The configured response
|
|
131
|
+
* @throws {Error} If no matching test configuration is found
|
|
132
|
+
*/
|
|
133
|
+
protected runModel(messages: ModelMessages[]): Promise<InvokeResponse>;
|
|
134
|
+
}
|
|
135
|
+
//# sourceMappingURL=TestModel.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TestModel.d.ts","sourceRoot":"","sources":["../../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AS;IAkCjC;;;;;;;OAOG;IACH,SAAS,CAAC,QAAQ,CACd,QAAQ,EAAE,aAAa,EAAE,GAC1B,OAAO,CAAC,cAAc,CAAC;CAc7B"}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TestModel = exports.TestResponseConfig = void 0;
|
|
4
|
+
const BaseModel_1 = require("./BaseModel");
|
|
5
|
+
const z = require("zod");
|
|
6
|
+
/**
|
|
7
|
+
* Configuration for test model responses.
|
|
8
|
+
* Allows specifying what the model should respond with for specific inputs.
|
|
9
|
+
*
|
|
10
|
+
* @class TestResponseConfig
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const config = new TestResponseConfig()
|
|
15
|
+
* .userSends([userMessage])
|
|
16
|
+
* .respondWith([agentMessage]);
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
class TestResponseConfig {
|
|
20
|
+
/**
|
|
21
|
+
* Tools that should be available when matching this config
|
|
22
|
+
*/
|
|
23
|
+
toolsIncluded = [];
|
|
24
|
+
/**
|
|
25
|
+
* Messages the model should respond with
|
|
26
|
+
*/
|
|
27
|
+
responseMessages = [];
|
|
28
|
+
/**
|
|
29
|
+
* Input messages that trigger this config
|
|
30
|
+
*/
|
|
31
|
+
inputMessages = [];
|
|
32
|
+
/**
|
|
33
|
+
* Structured output schema for this config (if using structured output)
|
|
34
|
+
*/
|
|
35
|
+
structuredOutput;
|
|
36
|
+
/**
|
|
37
|
+
* Specifies what messages the model should respond with.
|
|
38
|
+
*
|
|
39
|
+
* @param {(AgentMessage | ToolRequest)[]} messages - Response messages
|
|
40
|
+
* @returns {this} The config instance for method chaining
|
|
41
|
+
*/
|
|
42
|
+
respondWith(messages) {
|
|
43
|
+
this.responseMessages = messages;
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Specifies which tools should be included when matching this config.
|
|
48
|
+
*
|
|
49
|
+
* @param {ToolDefinition[]} tools - Tools to include
|
|
50
|
+
* @returns {this} The config instance for method chaining
|
|
51
|
+
*/
|
|
52
|
+
includedTools(tools) {
|
|
53
|
+
this.toolsIncluded = tools;
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Specifies the structured output schema for this config.
|
|
58
|
+
*
|
|
59
|
+
* @param {z.ZodSchema<any>} schema - The output schema
|
|
60
|
+
* @returns {this} The config instance for method chaining
|
|
61
|
+
*/
|
|
62
|
+
providedStructuredOutput(schema) {
|
|
63
|
+
this.structuredOutput = schema;
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Specifies the input messages that should trigger this config.
|
|
68
|
+
*
|
|
69
|
+
* @param {ModelMessages[]} messages - Input messages to match
|
|
70
|
+
* @returns {this} The config instance for method chaining
|
|
71
|
+
*/
|
|
72
|
+
userSends(messages) {
|
|
73
|
+
this.inputMessages = messages;
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
exports.TestResponseConfig = TestResponseConfig;
|
|
78
|
+
/**
|
|
79
|
+
* A mock model implementation for testing purposes.
|
|
80
|
+
* Allows you to specify predefined responses for specific message inputs.
|
|
81
|
+
* Useful for testing agent behavior without hitting real model APIs.
|
|
82
|
+
*
|
|
83
|
+
* @class TestModel
|
|
84
|
+
* @extends {BaseModel}
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```typescript
|
|
88
|
+
* const testModel = new TestModel({ temperature: 0.7 });
|
|
89
|
+
*
|
|
90
|
+
* // Configure a response
|
|
91
|
+
* const config = new TestResponseConfig()
|
|
92
|
+
* .userSends([new UserMessage("Hello")])
|
|
93
|
+
* .respondWith([new AgentMessage("Hi there!")]);
|
|
94
|
+
*
|
|
95
|
+
* testModel.addTestConfig(config);
|
|
96
|
+
*
|
|
97
|
+
* // When invoked with matching messages, returns the configured response
|
|
98
|
+
* const response = await testModel.invoke([new UserMessage("Hello")]);
|
|
99
|
+
* // response.messages[0].text === "Hi there!"
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
class TestModel extends BaseModel_1.BaseModel {
|
|
103
|
+
/**
|
|
104
|
+
* Array of configured test responses
|
|
105
|
+
*/
|
|
106
|
+
testConfigs = [];
|
|
107
|
+
/**
|
|
108
|
+
* Creates a new test model instance.
|
|
109
|
+
*
|
|
110
|
+
* @param {BaseModelConfig} config - Model configuration
|
|
111
|
+
*/
|
|
112
|
+
constructor(config) {
|
|
113
|
+
super(config);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Adds a test configuration for a specific input-output pair.
|
|
117
|
+
*
|
|
118
|
+
* @param {TestResponseConfig} config - The test configuration
|
|
119
|
+
* @returns {this} The model instance for method chaining
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```typescript
|
|
123
|
+
* model.addTestConfig(
|
|
124
|
+
* new TestResponseConfig()
|
|
125
|
+
* .userSends([userMsg])
|
|
126
|
+
* .respondWith([agentMsg])
|
|
127
|
+
* );
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
addTestConfig(config) {
|
|
131
|
+
this.testConfigs.push(config);
|
|
132
|
+
return this;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Finds a matching test configuration for the given messages.
|
|
136
|
+
* Matching is based on message content, tools, and structured output schema.
|
|
137
|
+
*
|
|
138
|
+
* @param {ModelMessages[]} messages - Messages to match against
|
|
139
|
+
* @returns {TestResponseConfig | undefined} The matching config, or undefined if no match
|
|
140
|
+
*
|
|
141
|
+
* @private
|
|
142
|
+
*/
|
|
143
|
+
findMatchingConfig(messages) {
|
|
144
|
+
const messagesSig = messages.map((message) => message.toJSON()).join("\n");
|
|
145
|
+
for (const config of this.testConfigs) {
|
|
146
|
+
const configMsgSig = config.inputMessages.map((message) => message.toJSON()).join("\n");
|
|
147
|
+
if (messagesSig !== configMsgSig)
|
|
148
|
+
continue;
|
|
149
|
+
if (config.structuredOutput) {
|
|
150
|
+
if (!this.structuredOutput)
|
|
151
|
+
continue;
|
|
152
|
+
if (JSON.stringify(z.toJSONSchema(config.structuredOutput)) ===
|
|
153
|
+
JSON.stringify(z.toJSONSchema(this.structuredOutput))) {
|
|
154
|
+
return config;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
const configToolNames = config.toolsIncluded.map((tool) => tool.name);
|
|
159
|
+
const modelToolNames = this.tools.map((tool) => tool.name);
|
|
160
|
+
if (configToolNames.every((name) => modelToolNames.includes(name))) {
|
|
161
|
+
return config;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return undefined;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Executes the test model by finding and returning a matching configuration.
|
|
169
|
+
*
|
|
170
|
+
* @protected
|
|
171
|
+
* @param {ModelMessages[]} messages - Messages to respond to
|
|
172
|
+
* @returns {Promise<InvokeResponse>} The configured response
|
|
173
|
+
* @throws {Error} If no matching test configuration is found
|
|
174
|
+
*/
|
|
175
|
+
runModel(messages) {
|
|
176
|
+
const matchingConfig = this.findMatchingConfig(messages);
|
|
177
|
+
if (!matchingConfig) {
|
|
178
|
+
throw new Error("No matching test config found");
|
|
179
|
+
}
|
|
180
|
+
return Promise.resolve({
|
|
181
|
+
messages: matchingConfig.responseMessages,
|
|
182
|
+
usage: {
|
|
183
|
+
inputTokens: 0,
|
|
184
|
+
outputTokens: 0,
|
|
185
|
+
},
|
|
186
|
+
stopReason: BaseModel_1.InvokeResponseStopReason.UNKNOWN,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
exports.TestModel = TestModel;
|
|
191
|
+
//# sourceMappingURL=TestModel.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TestModel.js","sourceRoot":"","sources":["../../src/models/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