@langchain/core 1.1.30 → 1.1.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +27 -0
- package/dist/language_models/base.cjs +1 -1
- package/dist/language_models/base.js +1 -1
- package/dist/load/import_map.cjs +2 -0
- package/dist/load/import_map.cjs.map +1 -1
- package/dist/load/import_map.js +4 -2
- package/dist/load/import_map.js.map +1 -1
- package/dist/messages/ai.cjs.map +1 -1
- package/dist/messages/ai.js.map +1 -1
- package/dist/messages/base.cjs +4 -3
- package/dist/messages/base.cjs.map +1 -1
- package/dist/messages/base.d.cts.map +1 -1
- package/dist/messages/base.d.ts.map +1 -1
- package/dist/messages/base.js +4 -3
- package/dist/messages/base.js.map +1 -1
- package/dist/messages/block_translators/google.cjs +16 -1
- package/dist/messages/block_translators/google.cjs.map +1 -1
- package/dist/messages/block_translators/google.js +16 -1
- package/dist/messages/block_translators/google.js.map +1 -1
- package/dist/runnables/base.cjs +1 -1
- package/dist/runnables/base.cjs.map +1 -1
- package/dist/runnables/base.js +2 -2
- package/dist/runnables/base.js.map +1 -1
- package/dist/{utils/testing → testing}/fake_model_builder.cjs +75 -23
- package/dist/testing/fake_model_builder.cjs.map +1 -0
- package/dist/testing/fake_model_builder.d.cts +138 -0
- package/dist/testing/fake_model_builder.d.cts.map +1 -0
- package/dist/testing/fake_model_builder.d.ts +139 -0
- package/dist/testing/fake_model_builder.d.ts.map +1 -0
- package/dist/{utils/testing → testing}/fake_model_builder.js +75 -24
- package/dist/testing/fake_model_builder.js.map +1 -0
- package/dist/testing/index.cjs +43 -0
- package/dist/testing/index.cjs.map +1 -0
- package/dist/testing/index.d.cts +3 -0
- package/dist/testing/index.d.ts +3 -0
- package/dist/testing/index.js +24 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/matchers.cjs +213 -0
- package/dist/testing/matchers.cjs.map +1 -0
- package/dist/testing/matchers.d.cts +94 -0
- package/dist/testing/matchers.d.cts.map +1 -0
- package/dist/testing/matchers.d.ts +94 -0
- package/dist/testing/matchers.d.ts.map +1 -0
- package/dist/testing/matchers.js +203 -0
- package/dist/testing/matchers.js.map +1 -0
- package/dist/tools/index.cjs +4 -0
- package/dist/tools/index.cjs.map +1 -1
- package/dist/tools/index.d.cts +22 -21
- package/dist/tools/index.d.cts.map +1 -1
- package/dist/tools/index.d.ts +22 -21
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +4 -0
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/types.cjs.map +1 -1
- package/dist/tools/types.d.cts +13 -5
- package/dist/tools/types.d.cts.map +1 -1
- package/dist/tools/types.d.ts +13 -5
- package/dist/tools/types.d.ts.map +1 -1
- package/dist/tools/types.js.map +1 -1
- package/dist/utils/testing/index.cjs +1 -4
- package/dist/utils/testing/index.cjs.map +1 -1
- package/dist/utils/testing/index.d.cts +1 -2
- package/dist/utils/testing/index.d.ts +1 -2
- package/dist/utils/testing/index.js +2 -4
- package/dist/utils/testing/index.js.map +1 -1
- package/package.json +16 -4
- package/dist/utils/testing/fake_model_builder.cjs.map +0 -1
- package/dist/utils/testing/fake_model_builder.d.cts +0 -87
- package/dist/utils/testing/fake_model_builder.d.cts.map +0 -1
- package/dist/utils/testing/fake_model_builder.d.ts +0 -88
- package/dist/utils/testing/fake_model_builder.d.ts.map +0 -1
- package/dist/utils/testing/fake_model_builder.js.map +0 -1
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
const require_base = require('
|
|
2
|
-
const require_ai = require('
|
|
3
|
-
const require_base$1 = require('
|
|
4
|
-
require('
|
|
5
|
-
const require_language_models_chat_models = require('
|
|
1
|
+
const require_base = require('../messages/base.cjs');
|
|
2
|
+
const require_ai = require('../messages/ai.cjs');
|
|
3
|
+
const require_base$1 = require('../runnables/base.cjs');
|
|
4
|
+
require('../messages/index.cjs');
|
|
5
|
+
const require_language_models_chat_models = require('../language_models/chat_models.cjs');
|
|
6
6
|
|
|
7
|
-
//#region src/
|
|
7
|
+
//#region src/testing/fake_model_builder.ts
|
|
8
8
|
function deriveContent(messages) {
|
|
9
9
|
return messages.map((m) => m.text).filter(Boolean).join("-");
|
|
10
10
|
}
|
|
@@ -18,22 +18,8 @@ function nextToolCallId() {
|
|
|
18
18
|
*
|
|
19
19
|
* Queue responses with `.respond()` and `.respondWithTools()`, then
|
|
20
20
|
* pass the instance directly wherever a chat model is expected.
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* `.respond(entry)` — enqueue a `BaseMessage`, `Error`, or factory function.
|
|
25
|
-
*
|
|
26
|
-
* `.respondWithTools(toolCalls[])` — enqueue an `AIMessage` with the given
|
|
27
|
-
* tool calls. Content is derived from the input messages automatically.
|
|
28
|
-
*
|
|
29
|
-
* Both can be mixed freely in one chain. When all queued responses are
|
|
30
|
-
* consumed, further invocations throw.
|
|
31
|
-
*
|
|
32
|
-
* Additional configuration:
|
|
33
|
-
* - `.alwaysThrow(error)` — every call throws (overrides the queue)
|
|
34
|
-
* - `.structuredResponse(value)` — value returned by `withStructuredOutput()`
|
|
35
|
-
*
|
|
36
|
-
* The model records all invocations in `.calls` / `.callCount`.
|
|
21
|
+
* Responses are consumed in first-in-first-out order — one per `invoke()` call.
|
|
22
|
+
* When all queued responses are consumed, further invocations throw.
|
|
37
23
|
*/
|
|
38
24
|
var FakeBuiltModel = class FakeBuiltModel extends require_language_models_chat_models.BaseChatModel {
|
|
39
25
|
queue = [];
|
|
@@ -42,9 +28,17 @@ var FakeBuiltModel = class FakeBuiltModel extends require_language_models_chat_m
|
|
|
42
28
|
_tools = [];
|
|
43
29
|
_callIndex = 0;
|
|
44
30
|
_calls = [];
|
|
31
|
+
/**
|
|
32
|
+
* All invocations recorded by this model, in order.
|
|
33
|
+
* Each entry contains the `messages` array and `options` that were
|
|
34
|
+
* passed to `invoke()`.
|
|
35
|
+
*/
|
|
45
36
|
get calls() {
|
|
46
37
|
return this._calls;
|
|
47
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* The number of times this model has been invoked.
|
|
41
|
+
*/
|
|
48
42
|
get callCount() {
|
|
49
43
|
return this._calls.length;
|
|
50
44
|
}
|
|
@@ -57,6 +51,12 @@ var FakeBuiltModel = class FakeBuiltModel extends require_language_models_chat_m
|
|
|
57
51
|
_combineLLMOutput() {
|
|
58
52
|
return [];
|
|
59
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Enqueue a response that the model will return on its next invocation.
|
|
56
|
+
* @param entry A {@link BaseMessage} to return, an `Error` to throw, or
|
|
57
|
+
* a factory `(messages) => BaseMessage | Error` for dynamic responses.
|
|
58
|
+
* @returns `this`, for chaining.
|
|
59
|
+
*/
|
|
60
60
|
respond(entry) {
|
|
61
61
|
if (typeof entry === "function") this.queue.push({
|
|
62
62
|
kind: "factory",
|
|
@@ -72,6 +72,13 @@ var FakeBuiltModel = class FakeBuiltModel extends require_language_models_chat_m
|
|
|
72
72
|
});
|
|
73
73
|
return this;
|
|
74
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* Enqueue an {@link AIMessage} that carries the given tool calls.
|
|
77
|
+
* Content is derived from the input messages at invocation time.
|
|
78
|
+
* @param toolCalls Array of tool calls. Each entry needs `name` and
|
|
79
|
+
* `args`; `id` is optional and auto-generated when omitted.
|
|
80
|
+
* @returns `this`, for chaining.
|
|
81
|
+
*/
|
|
75
82
|
respondWithTools(toolCalls) {
|
|
76
83
|
this.queue.push({
|
|
77
84
|
kind: "toolCalls",
|
|
@@ -84,14 +91,31 @@ var FakeBuiltModel = class FakeBuiltModel extends require_language_models_chat_m
|
|
|
84
91
|
});
|
|
85
92
|
return this;
|
|
86
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* Make every invocation throw the given error, regardless of the queue.
|
|
96
|
+
* @param error The error to throw.
|
|
97
|
+
* @returns `this`, for chaining.
|
|
98
|
+
*/
|
|
87
99
|
alwaysThrow(error) {
|
|
88
100
|
this._alwaysThrowError = error;
|
|
89
101
|
return this;
|
|
90
102
|
}
|
|
103
|
+
/**
|
|
104
|
+
* Set the value that {@link withStructuredOutput} will resolve to.
|
|
105
|
+
* @param value The structured object to return.
|
|
106
|
+
* @returns `this`, for chaining.
|
|
107
|
+
*/
|
|
91
108
|
structuredResponse(value) {
|
|
92
109
|
this._structuredResponseValue = value;
|
|
93
110
|
return this;
|
|
94
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* Bind tools to the model. Returns a new model that shares the same
|
|
114
|
+
* response queue and call history.
|
|
115
|
+
* @param tools The tools to bind, as {@link StructuredTool} instances or
|
|
116
|
+
* plain {@link ToolSpec} objects.
|
|
117
|
+
* @returns A new RunnableBinding with the tools bound.
|
|
118
|
+
*/
|
|
95
119
|
bindTools(tools) {
|
|
96
120
|
const merged = [...this._tools, ...tools];
|
|
97
121
|
const next = new FakeBuiltModel();
|
|
@@ -103,6 +127,13 @@ var FakeBuiltModel = class FakeBuiltModel extends require_language_models_chat_m
|
|
|
103
127
|
next._callIndex = this._callIndex;
|
|
104
128
|
return next.withConfig({});
|
|
105
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* Returns a {@link Runnable} that produces the {@link structuredResponse}
|
|
132
|
+
* value. The schema argument is accepted for compatibility but ignored.
|
|
133
|
+
* @param _params Schema or params (ignored).
|
|
134
|
+
* @param _config Options (ignored).
|
|
135
|
+
* @returns A Runnable that resolves to the structured response value.
|
|
136
|
+
*/
|
|
106
137
|
withStructuredOutput(_params, _config) {
|
|
107
138
|
const { _structuredResponseValue } = this;
|
|
108
139
|
return require_base$1.RunnableLambda.from(async () => {
|
|
@@ -150,7 +181,27 @@ var FakeBuiltModel = class FakeBuiltModel extends require_language_models_chat_m
|
|
|
150
181
|
}
|
|
151
182
|
};
|
|
152
183
|
/**
|
|
153
|
-
* Creates a
|
|
184
|
+
* Creates a new {@link FakeBuiltModel} for testing.
|
|
185
|
+
*
|
|
186
|
+
* Returns a chainable builder — queue responses, then pass the model
|
|
187
|
+
* anywhere a chat model is expected. Responses are consumed in FIFO
|
|
188
|
+
* order, one per `invoke()` call.
|
|
189
|
+
*
|
|
190
|
+
* ## API summary
|
|
191
|
+
*
|
|
192
|
+
* | Method | Description |
|
|
193
|
+
* | --- | --- |
|
|
194
|
+
* | `fakeModel()` | Creates a new fake chat model. Returns a chainable builder. |
|
|
195
|
+
* | `.respond(message)` | Queue an `AIMessage` (or any `BaseMessage`) to return on the next invocation. |
|
|
196
|
+
* | `.respond(error)` | Queue an `Error` to throw on the next invocation. |
|
|
197
|
+
* | `.respond(factory)` | Queue a function `(messages) => BaseMessage \| Error` for dynamic responses. |
|
|
198
|
+
* | `.respondWithTools(toolCalls)` | Shorthand for `.respond()` with tool calls. Each entry needs `name` and `args`; `id` is optional. |
|
|
199
|
+
* | `.alwaysThrow(error)` | Make every invocation throw this error, regardless of the queue. |
|
|
200
|
+
* | `.structuredResponse(value)` | Set the value returned by `.withStructuredOutput()`. |
|
|
201
|
+
* | `.bindTools(tools)` | Bind tools to the model. Returns a `RunnableBinding` that shares the response queue and call recording. |
|
|
202
|
+
* | `.withStructuredOutput(schema)` | Returns a runnable that produces the `.structuredResponse()` value. |
|
|
203
|
+
* | `.calls` | Array of `{ messages, options }` for every invocation (read-only). |
|
|
204
|
+
* | `.callCount` | Number of times the model has been invoked. |
|
|
154
205
|
*
|
|
155
206
|
* @example
|
|
156
207
|
* ```typescript
|
|
@@ -170,5 +221,6 @@ function fakeModel() {
|
|
|
170
221
|
}
|
|
171
222
|
|
|
172
223
|
//#endregion
|
|
224
|
+
exports.FakeBuiltModel = FakeBuiltModel;
|
|
173
225
|
exports.fakeModel = fakeModel;
|
|
174
226
|
//# sourceMappingURL=fake_model_builder.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fake_model_builder.cjs","names":["BaseChatModel","BaseMessage","RunnableLambda","AIMessage"],"sources":["../../src/testing/fake_model_builder.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { CallbackManagerForLLMRun } from \"../callbacks/manager.js\";\nimport {\n BaseChatModel,\n BaseChatModelCallOptions,\n} from \"../language_models/chat_models.js\";\nimport {\n BaseLanguageModelInput,\n StructuredOutputMethodOptions,\n StructuredOutputMethodParams,\n} from \"../language_models/base.js\";\nimport { BaseMessage, AIMessage } from \"../messages/index.js\";\nimport type { ToolCall } from \"../messages/tool.js\";\nimport type { ChatResult } from \"../outputs.js\";\nimport { Runnable, RunnableLambda } from \"../runnables/base.js\";\nimport { StructuredTool } from \"../tools/index.js\";\nimport type { InteropZodType } from \"../utils/types/zod.js\";\nimport type { ToolSpec } from \"../utils/testing/chat_models.js\";\n\ntype ResponseFactory = (messages: BaseMessage[]) => BaseMessage | Error;\n\ntype QueueEntry =\n | { kind: \"message\"; message: BaseMessage }\n | { kind: \"toolCalls\"; toolCalls: ToolCall[] }\n | { kind: \"error\"; error: Error }\n | { kind: \"factory\"; factory: ResponseFactory };\n\ninterface FakeModelCall {\n messages: BaseMessage[];\n options: any;\n}\n\nfunction deriveContent(messages: BaseMessage[]): string {\n return messages\n .map((m) => m.text)\n .filter(Boolean)\n .join(\"-\");\n}\n\nlet idCounter = 0;\nfunction nextToolCallId(): string {\n idCounter += 1;\n return `fake_tc_${idCounter}`;\n}\n\n/**\n * A fake chat model for testing, created via {@link fakeModel}.\n *\n * Queue responses with `.respond()` and `.respondWithTools()`, then\n * pass the instance directly wherever a chat model is expected.\n * Responses are consumed in first-in-first-out order — one per `invoke()` call.\n * When all queued responses are consumed, further invocations throw.\n */\nexport class FakeBuiltModel extends BaseChatModel {\n private queue: QueueEntry[] = [];\n\n private _alwaysThrowError: Error | undefined;\n\n private _structuredResponseValue: any;\n\n private _tools: (StructuredTool | ToolSpec)[] = [];\n\n private _callIndex = 0;\n\n private _calls: FakeModelCall[] = [];\n\n /**\n * All invocations recorded by this model, in order.\n * Each entry contains the `messages` array and `options` that were\n * passed to `invoke()`.\n */\n get calls(): FakeModelCall[] {\n return this._calls;\n }\n\n /**\n * The number of times this model has been invoked.\n */\n get callCount(): number {\n return this._calls.length;\n }\n\n constructor() {\n super({});\n }\n\n _llmType(): string {\n return \"fake-model-builder\";\n }\n\n _combineLLMOutput() {\n return [];\n }\n\n /**\n * Enqueue a response that the model will return on its next invocation.\n * @param entry A {@link BaseMessage} to return, an `Error` to throw, or\n * a factory `(messages) => BaseMessage | Error` for dynamic responses.\n * @returns `this`, for chaining.\n */\n respond(entry: BaseMessage | Error | ResponseFactory): this {\n if (typeof entry === \"function\") {\n this.queue.push({ kind: \"factory\", factory: entry });\n } else if (BaseMessage.isInstance(entry)) {\n this.queue.push({ kind: \"message\", message: entry });\n } else {\n this.queue.push({ kind: \"error\", error: entry });\n }\n return this;\n }\n\n /**\n * Enqueue an {@link AIMessage} that carries the given tool calls.\n * Content is derived from the input messages at invocation time.\n * @param toolCalls Array of tool calls. Each entry needs `name` and\n * `args`; `id` is optional and auto-generated when omitted.\n * @returns `this`, for chaining.\n */\n respondWithTools(\n toolCalls: Array<{ name: string; args: Record<string, any>; id?: string }>\n ): this {\n this.queue.push({\n kind: \"toolCalls\",\n toolCalls: toolCalls.map((tc) => ({\n name: tc.name,\n args: tc.args,\n id: tc.id ?? nextToolCallId(),\n type: \"tool_call\" as const,\n })),\n });\n return this;\n }\n\n /**\n * Make every invocation throw the given error, regardless of the queue.\n * @param error The error to throw.\n * @returns `this`, for chaining.\n */\n alwaysThrow(error: Error): this {\n this._alwaysThrowError = error;\n return this;\n }\n\n /**\n * Set the value that {@link withStructuredOutput} will resolve to.\n * @param value The structured object to return.\n * @returns `this`, for chaining.\n */\n structuredResponse(value: Record<string, any>): this {\n this._structuredResponseValue = value;\n return this;\n }\n\n /**\n * Bind tools to the model. Returns a new model that shares the same\n * response queue and call history.\n * @param tools The tools to bind, as {@link StructuredTool} instances or\n * plain {@link ToolSpec} objects.\n * @returns A new RunnableBinding with the tools bound.\n */\n bindTools(tools: (StructuredTool | ToolSpec)[]) {\n const merged = [...this._tools, ...tools];\n const next = new FakeBuiltModel();\n next.queue = this.queue;\n next._alwaysThrowError = this._alwaysThrowError;\n next._structuredResponseValue = this._structuredResponseValue;\n next._tools = merged;\n next._calls = this._calls;\n next._callIndex = this._callIndex;\n\n return next.withConfig({} as BaseChatModelCallOptions);\n }\n\n /**\n * Returns a {@link Runnable} that produces the {@link structuredResponse}\n * value. The schema argument is accepted for compatibility but ignored.\n * @param _params Schema or params (ignored).\n * @param _config Options (ignored).\n * @returns A Runnable that resolves to the structured response value.\n */\n withStructuredOutput<\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n _params:\n | StructuredOutputMethodParams<RunOutput, boolean>\n | InteropZodType<RunOutput>\n | Record<string, any>,\n _config?: StructuredOutputMethodOptions<boolean>\n ):\n | Runnable<BaseLanguageModelInput, RunOutput>\n | Runnable<\n BaseLanguageModelInput,\n { raw: BaseMessage; parsed: RunOutput }\n > {\n const { _structuredResponseValue } = this;\n return RunnableLambda.from(async () => {\n return _structuredResponseValue as RunOutput;\n }) as Runnable;\n }\n\n async _generate(\n messages: BaseMessage[],\n options?: this[\"ParsedCallOptions\"],\n _runManager?: CallbackManagerForLLMRun\n ): Promise<ChatResult> {\n this._calls.push({ messages: [...messages], options });\n\n const currentCallIndex = this._callIndex;\n this._callIndex += 1;\n\n if (this._alwaysThrowError) {\n throw this._alwaysThrowError;\n }\n\n const entry = this.queue[currentCallIndex];\n if (!entry) {\n throw new Error(\n `FakeModel: no response queued for invocation ${currentCallIndex} (${this.queue.length} total queued).`\n );\n }\n\n if (entry.kind === \"error\") {\n throw entry.error;\n }\n\n if (entry.kind === \"factory\") {\n const result = entry.factory(messages);\n if (!BaseMessage.isInstance(result)) {\n throw result;\n }\n return {\n generations: [{ text: \"\", message: result }],\n };\n }\n\n if (entry.kind === \"message\") {\n return {\n generations: [{ text: \"\", message: entry.message }],\n };\n }\n\n const content = deriveContent(messages);\n const message = new AIMessage({\n content,\n id: currentCallIndex.toString(),\n tool_calls:\n entry.toolCalls.length > 0\n ? entry.toolCalls.map((tc) => ({\n ...tc,\n type: \"tool_call\" as const,\n }))\n : undefined,\n });\n\n return {\n generations: [{ text: content, message }],\n llmOutput: {},\n };\n }\n}\n\n/**\n * Creates a new {@link FakeBuiltModel} for testing.\n *\n * Returns a chainable builder — queue responses, then pass the model\n * anywhere a chat model is expected. Responses are consumed in FIFO\n * order, one per `invoke()` call.\n *\n * ## API summary\n *\n * | Method | Description |\n * | --- | --- |\n * | `fakeModel()` | Creates a new fake chat model. Returns a chainable builder. |\n * | `.respond(message)` | Queue an `AIMessage` (or any `BaseMessage`) to return on the next invocation. |\n * | `.respond(error)` | Queue an `Error` to throw on the next invocation. |\n * | `.respond(factory)` | Queue a function `(messages) => BaseMessage \\| Error` for dynamic responses. |\n * | `.respondWithTools(toolCalls)` | Shorthand for `.respond()` with tool calls. Each entry needs `name` and `args`; `id` is optional. |\n * | `.alwaysThrow(error)` | Make every invocation throw this error, regardless of the queue. |\n * | `.structuredResponse(value)` | Set the value returned by `.withStructuredOutput()`. |\n * | `.bindTools(tools)` | Bind tools to the model. Returns a `RunnableBinding` that shares the response queue and call recording. |\n * | `.withStructuredOutput(schema)` | Returns a runnable that produces the `.structuredResponse()` value. |\n * | `.calls` | Array of `{ messages, options }` for every invocation (read-only). |\n * | `.callCount` | Number of times the model has been invoked. |\n *\n * @example\n * ```typescript\n * const model = fakeModel()\n * .respondWithTools([{ name: \"search\", args: { query: \"weather\" } }])\n * .respond(new AIMessage(\"Sunny and warm.\"));\n *\n * const r1 = await model.invoke([new HumanMessage(\"What's the weather?\")]);\n * // r1.tool_calls[0].name === \"search\"\n *\n * const r2 = await model.invoke([new HumanMessage(\"Thanks\")]);\n * // r2.content === \"Sunny and warm.\"\n * ```\n */\nexport function fakeModel(): FakeBuiltModel {\n return new FakeBuiltModel();\n}\n"],"mappings":";;;;;;;AAgCA,SAAS,cAAc,UAAiC;AACtD,QAAO,SACJ,KAAK,MAAM,EAAE,KAAK,CAClB,OAAO,QAAQ,CACf,KAAK,IAAI;;AAGd,IAAI,YA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@ -0,0 +1,138 @@
|
|
|
1
|
+
import { BaseMessage } from "../messages/base.cjs";
|
|
2
|
+
import { AIMessageChunk } from "../messages/ai.cjs";
|
|
3
|
+
import { MessageStructure, MessageToolSet } from "../messages/message.cjs";
|
|
4
|
+
import { ChatResult } from "../outputs.cjs";
|
|
5
|
+
import { InteropZodType } from "../utils/types/zod.cjs";
|
|
6
|
+
import { CallbackManagerForLLMRun } from "../callbacks/manager.cjs";
|
|
7
|
+
import { Runnable } from "../runnables/base.cjs";
|
|
8
|
+
import { BaseLanguageModelInput, StructuredOutputMethodOptions, StructuredOutputMethodParams } from "../language_models/base.cjs";
|
|
9
|
+
import { StructuredTool } from "../tools/index.cjs";
|
|
10
|
+
import { BaseChatModel, BaseChatModelCallOptions } from "../language_models/chat_models.cjs";
|
|
11
|
+
import { ToolSpec } from "../utils/testing/chat_models.cjs";
|
|
12
|
+
|
|
13
|
+
//#region src/testing/fake_model_builder.d.ts
|
|
14
|
+
type ResponseFactory = (messages: BaseMessage[]) => BaseMessage | Error;
|
|
15
|
+
interface FakeModelCall {
|
|
16
|
+
messages: BaseMessage[];
|
|
17
|
+
options: any;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* A fake chat model for testing, created via {@link fakeModel}.
|
|
21
|
+
*
|
|
22
|
+
* Queue responses with `.respond()` and `.respondWithTools()`, then
|
|
23
|
+
* pass the instance directly wherever a chat model is expected.
|
|
24
|
+
* Responses are consumed in first-in-first-out order — one per `invoke()` call.
|
|
25
|
+
* When all queued responses are consumed, further invocations throw.
|
|
26
|
+
*/
|
|
27
|
+
declare class FakeBuiltModel extends BaseChatModel {
|
|
28
|
+
private queue;
|
|
29
|
+
private _alwaysThrowError;
|
|
30
|
+
private _structuredResponseValue;
|
|
31
|
+
private _tools;
|
|
32
|
+
private _callIndex;
|
|
33
|
+
private _calls;
|
|
34
|
+
/**
|
|
35
|
+
* All invocations recorded by this model, in order.
|
|
36
|
+
* Each entry contains the `messages` array and `options` that were
|
|
37
|
+
* passed to `invoke()`.
|
|
38
|
+
*/
|
|
39
|
+
get calls(): FakeModelCall[];
|
|
40
|
+
/**
|
|
41
|
+
* The number of times this model has been invoked.
|
|
42
|
+
*/
|
|
43
|
+
get callCount(): number;
|
|
44
|
+
constructor();
|
|
45
|
+
_llmType(): string;
|
|
46
|
+
_combineLLMOutput(): never[];
|
|
47
|
+
/**
|
|
48
|
+
* Enqueue a response that the model will return on its next invocation.
|
|
49
|
+
* @param entry A {@link BaseMessage} to return, an `Error` to throw, or
|
|
50
|
+
* a factory `(messages) => BaseMessage | Error` for dynamic responses.
|
|
51
|
+
* @returns `this`, for chaining.
|
|
52
|
+
*/
|
|
53
|
+
respond(entry: BaseMessage | Error | ResponseFactory): this;
|
|
54
|
+
/**
|
|
55
|
+
* Enqueue an {@link AIMessage} that carries the given tool calls.
|
|
56
|
+
* Content is derived from the input messages at invocation time.
|
|
57
|
+
* @param toolCalls Array of tool calls. Each entry needs `name` and
|
|
58
|
+
* `args`; `id` is optional and auto-generated when omitted.
|
|
59
|
+
* @returns `this`, for chaining.
|
|
60
|
+
*/
|
|
61
|
+
respondWithTools(toolCalls: Array<{
|
|
62
|
+
name: string;
|
|
63
|
+
args: Record<string, any>;
|
|
64
|
+
id?: string;
|
|
65
|
+
}>): this;
|
|
66
|
+
/**
|
|
67
|
+
* Make every invocation throw the given error, regardless of the queue.
|
|
68
|
+
* @param error The error to throw.
|
|
69
|
+
* @returns `this`, for chaining.
|
|
70
|
+
*/
|
|
71
|
+
alwaysThrow(error: Error): this;
|
|
72
|
+
/**
|
|
73
|
+
* Set the value that {@link withStructuredOutput} will resolve to.
|
|
74
|
+
* @param value The structured object to return.
|
|
75
|
+
* @returns `this`, for chaining.
|
|
76
|
+
*/
|
|
77
|
+
structuredResponse(value: Record<string, any>): this;
|
|
78
|
+
/**
|
|
79
|
+
* Bind tools to the model. Returns a new model that shares the same
|
|
80
|
+
* response queue and call history.
|
|
81
|
+
* @param tools The tools to bind, as {@link StructuredTool} instances or
|
|
82
|
+
* plain {@link ToolSpec} objects.
|
|
83
|
+
* @returns A new RunnableBinding with the tools bound.
|
|
84
|
+
*/
|
|
85
|
+
bindTools(tools: (StructuredTool | ToolSpec)[]): Runnable<BaseLanguageModelInput, AIMessageChunk<MessageStructure<MessageToolSet>>, BaseChatModelCallOptions>;
|
|
86
|
+
/**
|
|
87
|
+
* Returns a {@link Runnable} that produces the {@link structuredResponse}
|
|
88
|
+
* value. The schema argument is accepted for compatibility but ignored.
|
|
89
|
+
* @param _params Schema or params (ignored).
|
|
90
|
+
* @param _config Options (ignored).
|
|
91
|
+
* @returns A Runnable that resolves to the structured response value.
|
|
92
|
+
*/
|
|
93
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(_params: StructuredOutputMethodParams<RunOutput, boolean> | InteropZodType<RunOutput> | Record<string, any>, _config?: StructuredOutputMethodOptions<boolean>): Runnable<BaseLanguageModelInput, RunOutput> | Runnable<BaseLanguageModelInput, {
|
|
94
|
+
raw: BaseMessage;
|
|
95
|
+
parsed: RunOutput;
|
|
96
|
+
}>;
|
|
97
|
+
_generate(messages: BaseMessage[], options?: this["ParsedCallOptions"], _runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Creates a new {@link FakeBuiltModel} for testing.
|
|
101
|
+
*
|
|
102
|
+
* Returns a chainable builder — queue responses, then pass the model
|
|
103
|
+
* anywhere a chat model is expected. Responses are consumed in FIFO
|
|
104
|
+
* order, one per `invoke()` call.
|
|
105
|
+
*
|
|
106
|
+
* ## API summary
|
|
107
|
+
*
|
|
108
|
+
* | Method | Description |
|
|
109
|
+
* | --- | --- |
|
|
110
|
+
* | `fakeModel()` | Creates a new fake chat model. Returns a chainable builder. |
|
|
111
|
+
* | `.respond(message)` | Queue an `AIMessage` (or any `BaseMessage`) to return on the next invocation. |
|
|
112
|
+
* | `.respond(error)` | Queue an `Error` to throw on the next invocation. |
|
|
113
|
+
* | `.respond(factory)` | Queue a function `(messages) => BaseMessage \| Error` for dynamic responses. |
|
|
114
|
+
* | `.respondWithTools(toolCalls)` | Shorthand for `.respond()` with tool calls. Each entry needs `name` and `args`; `id` is optional. |
|
|
115
|
+
* | `.alwaysThrow(error)` | Make every invocation throw this error, regardless of the queue. |
|
|
116
|
+
* | `.structuredResponse(value)` | Set the value returned by `.withStructuredOutput()`. |
|
|
117
|
+
* | `.bindTools(tools)` | Bind tools to the model. Returns a `RunnableBinding` that shares the response queue and call recording. |
|
|
118
|
+
* | `.withStructuredOutput(schema)` | Returns a runnable that produces the `.structuredResponse()` value. |
|
|
119
|
+
* | `.calls` | Array of `{ messages, options }` for every invocation (read-only). |
|
|
120
|
+
* | `.callCount` | Number of times the model has been invoked. |
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```typescript
|
|
124
|
+
* const model = fakeModel()
|
|
125
|
+
* .respondWithTools([{ name: "search", args: { query: "weather" } }])
|
|
126
|
+
* .respond(new AIMessage("Sunny and warm."));
|
|
127
|
+
*
|
|
128
|
+
* const r1 = await model.invoke([new HumanMessage("What's the weather?")]);
|
|
129
|
+
* // r1.tool_calls[0].name === "search"
|
|
130
|
+
*
|
|
131
|
+
* const r2 = await model.invoke([new HumanMessage("Thanks")]);
|
|
132
|
+
* // r2.content === "Sunny and warm."
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
declare function fakeModel(): FakeBuiltModel;
|
|
136
|
+
//#endregion
|
|
137
|
+
export { FakeBuiltModel, fakeModel };
|
|
138
|
+
//# sourceMappingURL=fake_model_builder.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fake_model_builder.d.cts","names":[],"sources":["../../src/testing/fake_model_builder.ts"],"mappings":";;;;;;;;;;;;;KAmBK,eAAA,IAAmB,QAAA,EAAU,WAAA,OAAkB,WAAA,GAAc,KAAA;AAAA,UAQxD,aAAA;EACR,QAAA,EAAU,WAAA;EACV,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essage } from "../messages/base.js";
|
|
2
|
+
import { AIMessageChunk } from "../messages/ai.js";
|
|
3
|
+
import { MessageStructure, MessageToolSet } from "../messages/message.js";
|
|
4
|
+
import { ChatResult } from "../outputs.js";
|
|
5
|
+
import { InteropZodType } from "../utils/types/zod.js";
|
|
6
|
+
import { CallbackManagerForLLMRun } from "../callbacks/manager.js";
|
|
7
|
+
import { Runnable } from "../runnables/base.js";
|
|
8
|
+
import { BaseLanguageModelInput, StructuredOutputMethodOptions, StructuredOutputMethodParams } from "../language_models/base.js";
|
|
9
|
+
import "../messages/index.js";
|
|
10
|
+
import { StructuredTool } from "../tools/index.js";
|
|
11
|
+
import { BaseChatModel, BaseChatModelCallOptions } from "../language_models/chat_models.js";
|
|
12
|
+
import { ToolSpec } from "../utils/testing/chat_models.js";
|
|
13
|
+
|
|
14
|
+
//#region src/testing/fake_model_builder.d.ts
|
|
15
|
+
type ResponseFactory = (messages: BaseMessage[]) => BaseMessage | Error;
|
|
16
|
+
interface FakeModelCall {
|
|
17
|
+
messages: BaseMessage[];
|
|
18
|
+
options: any;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A fake chat model for testing, created via {@link fakeModel}.
|
|
22
|
+
*
|
|
23
|
+
* Queue responses with `.respond()` and `.respondWithTools()`, then
|
|
24
|
+
* pass the instance directly wherever a chat model is expected.
|
|
25
|
+
* Responses are consumed in first-in-first-out order — one per `invoke()` call.
|
|
26
|
+
* When all queued responses are consumed, further invocations throw.
|
|
27
|
+
*/
|
|
28
|
+
declare class FakeBuiltModel extends BaseChatModel {
|
|
29
|
+
private queue;
|
|
30
|
+
private _alwaysThrowError;
|
|
31
|
+
private _structuredResponseValue;
|
|
32
|
+
private _tools;
|
|
33
|
+
private _callIndex;
|
|
34
|
+
private _calls;
|
|
35
|
+
/**
|
|
36
|
+
* All invocations recorded by this model, in order.
|
|
37
|
+
* Each entry contains the `messages` array and `options` that were
|
|
38
|
+
* passed to `invoke()`.
|
|
39
|
+
*/
|
|
40
|
+
get calls(): FakeModelCall[];
|
|
41
|
+
/**
|
|
42
|
+
* The number of times this model has been invoked.
|
|
43
|
+
*/
|
|
44
|
+
get callCount(): number;
|
|
45
|
+
constructor();
|
|
46
|
+
_llmType(): string;
|
|
47
|
+
_combineLLMOutput(): never[];
|
|
48
|
+
/**
|
|
49
|
+
* Enqueue a response that the model will return on its next invocation.
|
|
50
|
+
* @param entry A {@link BaseMessage} to return, an `Error` to throw, or
|
|
51
|
+
* a factory `(messages) => BaseMessage | Error` for dynamic responses.
|
|
52
|
+
* @returns `this`, for chaining.
|
|
53
|
+
*/
|
|
54
|
+
respond(entry: BaseMessage | Error | ResponseFactory): this;
|
|
55
|
+
/**
|
|
56
|
+
* Enqueue an {@link AIMessage} that carries the given tool calls.
|
|
57
|
+
* Content is derived from the input messages at invocation time.
|
|
58
|
+
* @param toolCalls Array of tool calls. Each entry needs `name` and
|
|
59
|
+
* `args`; `id` is optional and auto-generated when omitted.
|
|
60
|
+
* @returns `this`, for chaining.
|
|
61
|
+
*/
|
|
62
|
+
respondWithTools(toolCalls: Array<{
|
|
63
|
+
name: string;
|
|
64
|
+
args: Record<string, any>;
|
|
65
|
+
id?: string;
|
|
66
|
+
}>): this;
|
|
67
|
+
/**
|
|
68
|
+
* Make every invocation throw the given error, regardless of the queue.
|
|
69
|
+
* @param error The error to throw.
|
|
70
|
+
* @returns `this`, for chaining.
|
|
71
|
+
*/
|
|
72
|
+
alwaysThrow(error: Error): this;
|
|
73
|
+
/**
|
|
74
|
+
* Set the value that {@link withStructuredOutput} will resolve to.
|
|
75
|
+
* @param value The structured object to return.
|
|
76
|
+
* @returns `this`, for chaining.
|
|
77
|
+
*/
|
|
78
|
+
structuredResponse(value: Record<string, any>): this;
|
|
79
|
+
/**
|
|
80
|
+
* Bind tools to the model. Returns a new model that shares the same
|
|
81
|
+
* response queue and call history.
|
|
82
|
+
* @param tools The tools to bind, as {@link StructuredTool} instances or
|
|
83
|
+
* plain {@link ToolSpec} objects.
|
|
84
|
+
* @returns A new RunnableBinding with the tools bound.
|
|
85
|
+
*/
|
|
86
|
+
bindTools(tools: (StructuredTool | ToolSpec)[]): Runnable<BaseLanguageModelInput, AIMessageChunk<MessageStructure<MessageToolSet>>, BaseChatModelCallOptions>;
|
|
87
|
+
/**
|
|
88
|
+
* Returns a {@link Runnable} that produces the {@link structuredResponse}
|
|
89
|
+
* value. The schema argument is accepted for compatibility but ignored.
|
|
90
|
+
* @param _params Schema or params (ignored).
|
|
91
|
+
* @param _config Options (ignored).
|
|
92
|
+
* @returns A Runnable that resolves to the structured response value.
|
|
93
|
+
*/
|
|
94
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(_params: StructuredOutputMethodParams<RunOutput, boolean> | InteropZodType<RunOutput> | Record<string, any>, _config?: StructuredOutputMethodOptions<boolean>): Runnable<BaseLanguageModelInput, RunOutput> | Runnable<BaseLanguageModelInput, {
|
|
95
|
+
raw: BaseMessage;
|
|
96
|
+
parsed: RunOutput;
|
|
97
|
+
}>;
|
|
98
|
+
_generate(messages: BaseMessage[], options?: this["ParsedCallOptions"], _runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Creates a new {@link FakeBuiltModel} for testing.
|
|
102
|
+
*
|
|
103
|
+
* Returns a chainable builder — queue responses, then pass the model
|
|
104
|
+
* anywhere a chat model is expected. Responses are consumed in FIFO
|
|
105
|
+
* order, one per `invoke()` call.
|
|
106
|
+
*
|
|
107
|
+
* ## API summary
|
|
108
|
+
*
|
|
109
|
+
* | Method | Description |
|
|
110
|
+
* | --- | --- |
|
|
111
|
+
* | `fakeModel()` | Creates a new fake chat model. Returns a chainable builder. |
|
|
112
|
+
* | `.respond(message)` | Queue an `AIMessage` (or any `BaseMessage`) to return on the next invocation. |
|
|
113
|
+
* | `.respond(error)` | Queue an `Error` to throw on the next invocation. |
|
|
114
|
+
* | `.respond(factory)` | Queue a function `(messages) => BaseMessage \| Error` for dynamic responses. |
|
|
115
|
+
* | `.respondWithTools(toolCalls)` | Shorthand for `.respond()` with tool calls. Each entry needs `name` and `args`; `id` is optional. |
|
|
116
|
+
* | `.alwaysThrow(error)` | Make every invocation throw this error, regardless of the queue. |
|
|
117
|
+
* | `.structuredResponse(value)` | Set the value returned by `.withStructuredOutput()`. |
|
|
118
|
+
* | `.bindTools(tools)` | Bind tools to the model. Returns a `RunnableBinding` that shares the response queue and call recording. |
|
|
119
|
+
* | `.withStructuredOutput(schema)` | Returns a runnable that produces the `.structuredResponse()` value. |
|
|
120
|
+
* | `.calls` | Array of `{ messages, options }` for every invocation (read-only). |
|
|
121
|
+
* | `.callCount` | Number of times the model has been invoked. |
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```typescript
|
|
125
|
+
* const model = fakeModel()
|
|
126
|
+
* .respondWithTools([{ name: "search", args: { query: "weather" } }])
|
|
127
|
+
* .respond(new AIMessage("Sunny and warm."));
|
|
128
|
+
*
|
|
129
|
+
* const r1 = await model.invoke([new HumanMessage("What's the weather?")]);
|
|
130
|
+
* // r1.tool_calls[0].name === "search"
|
|
131
|
+
*
|
|
132
|
+
* const r2 = await model.invoke([new HumanMessage("Thanks")]);
|
|
133
|
+
* // r2.content === "Sunny and warm."
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
declare function fakeModel(): FakeBuiltModel;
|
|
137
|
+
//#endregion
|
|
138
|
+
export { FakeBuiltModel, fakeModel };
|
|
139
|
+
//# sourceMappingURL=fake_model_builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fake_model_builder.d.ts","names":[],"sources":["../../src/testing/fake_model_builder.ts"],"mappings":";;;;;;;;;;;;;;KAmBK,eAAA,IAAmB,QAAA,EAAU,WAAA,OAAkB,WAAA,GAAc,KAAA;AAAA,UAQxD,aAAA;EACR,QAAA,EAAU,WAAA;EACV,OAAA;AAAA;AAZ8D;;;;;;;;AAAA,cAoC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
|
-
import { AIMessage } from "
|
|
3
|
-
import { RunnableLambda } from "
|
|
4
|
-
import "
|
|
5
|
-
import { BaseChatModel } from "
|
|
1
|
+
import { BaseMessage } from "../messages/base.js";
|
|
2
|
+
import { AIMessage } from "../messages/ai.js";
|
|
3
|
+
import { RunnableLambda } from "../runnables/base.js";
|
|
4
|
+
import "../messages/index.js";
|
|
5
|
+
import { BaseChatModel } from "../language_models/chat_models.js";
|
|
6
6
|
|
|
7
|
-
//#region src/
|
|
7
|
+
//#region src/testing/fake_model_builder.ts
|
|
8
8
|
function deriveContent(messages) {
|
|
9
9
|
return messages.map((m) => m.text).filter(Boolean).join("-");
|
|
10
10
|
}
|
|
@@ -18,22 +18,8 @@ function nextToolCallId() {
|
|
|
18
18
|
*
|
|
19
19
|
* Queue responses with `.respond()` and `.respondWithTools()`, then
|
|
20
20
|
* pass the instance directly wherever a chat model is expected.
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* `.respond(entry)` — enqueue a `BaseMessage`, `Error`, or factory function.
|
|
25
|
-
*
|
|
26
|
-
* `.respondWithTools(toolCalls[])` — enqueue an `AIMessage` with the given
|
|
27
|
-
* tool calls. Content is derived from the input messages automatically.
|
|
28
|
-
*
|
|
29
|
-
* Both can be mixed freely in one chain. When all queued responses are
|
|
30
|
-
* consumed, further invocations throw.
|
|
31
|
-
*
|
|
32
|
-
* Additional configuration:
|
|
33
|
-
* - `.alwaysThrow(error)` — every call throws (overrides the queue)
|
|
34
|
-
* - `.structuredResponse(value)` — value returned by `withStructuredOutput()`
|
|
35
|
-
*
|
|
36
|
-
* The model records all invocations in `.calls` / `.callCount`.
|
|
21
|
+
* Responses are consumed in first-in-first-out order — one per `invoke()` call.
|
|
22
|
+
* When all queued responses are consumed, further invocations throw.
|
|
37
23
|
*/
|
|
38
24
|
var FakeBuiltModel = class FakeBuiltModel extends BaseChatModel {
|
|
39
25
|
queue = [];
|
|
@@ -42,9 +28,17 @@ var FakeBuiltModel = class FakeBuiltModel extends BaseChatModel {
|
|
|
42
28
|
_tools = [];
|
|
43
29
|
_callIndex = 0;
|
|
44
30
|
_calls = [];
|
|
31
|
+
/**
|
|
32
|
+
* All invocations recorded by this model, in order.
|
|
33
|
+
* Each entry contains the `messages` array and `options` that were
|
|
34
|
+
* passed to `invoke()`.
|
|
35
|
+
*/
|
|
45
36
|
get calls() {
|
|
46
37
|
return this._calls;
|
|
47
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* The number of times this model has been invoked.
|
|
41
|
+
*/
|
|
48
42
|
get callCount() {
|
|
49
43
|
return this._calls.length;
|
|
50
44
|
}
|
|
@@ -57,6 +51,12 @@ var FakeBuiltModel = class FakeBuiltModel extends BaseChatModel {
|
|
|
57
51
|
_combineLLMOutput() {
|
|
58
52
|
return [];
|
|
59
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Enqueue a response that the model will return on its next invocation.
|
|
56
|
+
* @param entry A {@link BaseMessage} to return, an `Error` to throw, or
|
|
57
|
+
* a factory `(messages) => BaseMessage | Error` for dynamic responses.
|
|
58
|
+
* @returns `this`, for chaining.
|
|
59
|
+
*/
|
|
60
60
|
respond(entry) {
|
|
61
61
|
if (typeof entry === "function") this.queue.push({
|
|
62
62
|
kind: "factory",
|
|
@@ -72,6 +72,13 @@ var FakeBuiltModel = class FakeBuiltModel extends BaseChatModel {
|
|
|
72
72
|
});
|
|
73
73
|
return this;
|
|
74
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* Enqueue an {@link AIMessage} that carries the given tool calls.
|
|
77
|
+
* Content is derived from the input messages at invocation time.
|
|
78
|
+
* @param toolCalls Array of tool calls. Each entry needs `name` and
|
|
79
|
+
* `args`; `id` is optional and auto-generated when omitted.
|
|
80
|
+
* @returns `this`, for chaining.
|
|
81
|
+
*/
|
|
75
82
|
respondWithTools(toolCalls) {
|
|
76
83
|
this.queue.push({
|
|
77
84
|
kind: "toolCalls",
|
|
@@ -84,14 +91,31 @@ var FakeBuiltModel = class FakeBuiltModel extends BaseChatModel {
|
|
|
84
91
|
});
|
|
85
92
|
return this;
|
|
86
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* Make every invocation throw the given error, regardless of the queue.
|
|
96
|
+
* @param error The error to throw.
|
|
97
|
+
* @returns `this`, for chaining.
|
|
98
|
+
*/
|
|
87
99
|
alwaysThrow(error) {
|
|
88
100
|
this._alwaysThrowError = error;
|
|
89
101
|
return this;
|
|
90
102
|
}
|
|
103
|
+
/**
|
|
104
|
+
* Set the value that {@link withStructuredOutput} will resolve to.
|
|
105
|
+
* @param value The structured object to return.
|
|
106
|
+
* @returns `this`, for chaining.
|
|
107
|
+
*/
|
|
91
108
|
structuredResponse(value) {
|
|
92
109
|
this._structuredResponseValue = value;
|
|
93
110
|
return this;
|
|
94
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* Bind tools to the model. Returns a new model that shares the same
|
|
114
|
+
* response queue and call history.
|
|
115
|
+
* @param tools The tools to bind, as {@link StructuredTool} instances or
|
|
116
|
+
* plain {@link ToolSpec} objects.
|
|
117
|
+
* @returns A new RunnableBinding with the tools bound.
|
|
118
|
+
*/
|
|
95
119
|
bindTools(tools) {
|
|
96
120
|
const merged = [...this._tools, ...tools];
|
|
97
121
|
const next = new FakeBuiltModel();
|
|
@@ -103,6 +127,13 @@ var FakeBuiltModel = class FakeBuiltModel extends BaseChatModel {
|
|
|
103
127
|
next._callIndex = this._callIndex;
|
|
104
128
|
return next.withConfig({});
|
|
105
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* Returns a {@link Runnable} that produces the {@link structuredResponse}
|
|
132
|
+
* value. The schema argument is accepted for compatibility but ignored.
|
|
133
|
+
* @param _params Schema or params (ignored).
|
|
134
|
+
* @param _config Options (ignored).
|
|
135
|
+
* @returns A Runnable that resolves to the structured response value.
|
|
136
|
+
*/
|
|
106
137
|
withStructuredOutput(_params, _config) {
|
|
107
138
|
const { _structuredResponseValue } = this;
|
|
108
139
|
return RunnableLambda.from(async () => {
|
|
@@ -150,7 +181,27 @@ var FakeBuiltModel = class FakeBuiltModel extends BaseChatModel {
|
|
|
150
181
|
}
|
|
151
182
|
};
|
|
152
183
|
/**
|
|
153
|
-
* Creates a
|
|
184
|
+
* Creates a new {@link FakeBuiltModel} for testing.
|
|
185
|
+
*
|
|
186
|
+
* Returns a chainable builder — queue responses, then pass the model
|
|
187
|
+
* anywhere a chat model is expected. Responses are consumed in FIFO
|
|
188
|
+
* order, one per `invoke()` call.
|
|
189
|
+
*
|
|
190
|
+
* ## API summary
|
|
191
|
+
*
|
|
192
|
+
* | Method | Description |
|
|
193
|
+
* | --- | --- |
|
|
194
|
+
* | `fakeModel()` | Creates a new fake chat model. Returns a chainable builder. |
|
|
195
|
+
* | `.respond(message)` | Queue an `AIMessage` (or any `BaseMessage`) to return on the next invocation. |
|
|
196
|
+
* | `.respond(error)` | Queue an `Error` to throw on the next invocation. |
|
|
197
|
+
* | `.respond(factory)` | Queue a function `(messages) => BaseMessage \| Error` for dynamic responses. |
|
|
198
|
+
* | `.respondWithTools(toolCalls)` | Shorthand for `.respond()` with tool calls. Each entry needs `name` and `args`; `id` is optional. |
|
|
199
|
+
* | `.alwaysThrow(error)` | Make every invocation throw this error, regardless of the queue. |
|
|
200
|
+
* | `.structuredResponse(value)` | Set the value returned by `.withStructuredOutput()`. |
|
|
201
|
+
* | `.bindTools(tools)` | Bind tools to the model. Returns a `RunnableBinding` that shares the response queue and call recording. |
|
|
202
|
+
* | `.withStructuredOutput(schema)` | Returns a runnable that produces the `.structuredResponse()` value. |
|
|
203
|
+
* | `.calls` | Array of `{ messages, options }` for every invocation (read-only). |
|
|
204
|
+
* | `.callCount` | Number of times the model has been invoked. |
|
|
154
205
|
*
|
|
155
206
|
* @example
|
|
156
207
|
* ```typescript
|
|
@@ -170,5 +221,5 @@ function fakeModel() {
|
|
|
170
221
|
}
|
|
171
222
|
|
|
172
223
|
//#endregion
|
|
173
|
-
export { fakeModel };
|
|
224
|
+
export { FakeBuiltModel, fakeModel };
|
|
174
225
|
//# sourceMappingURL=fake_model_builder.js.map
|