@langchain/core 1.1.31 → 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 +14 -0
- package/dist/language_models/base.cjs +1 -1
- package/dist/language_models/base.js +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/testing/fake_model_builder.cjs +69 -17
- package/dist/testing/fake_model_builder.cjs.map +1 -1
- package/dist/testing/fake_model_builder.d.cts +69 -18
- package/dist/testing/fake_model_builder.d.cts.map +1 -1
- package/dist/testing/fake_model_builder.d.ts +69 -18
- package/dist/testing/fake_model_builder.d.ts.map +1 -1
- package/dist/testing/fake_model_builder.js +69 -18
- package/dist/testing/fake_model_builder.js.map +1 -1
- package/dist/testing/index.cjs +2 -0
- package/dist/testing/index.cjs.map +1 -1
- package/dist/testing/index.d.cts +2 -2
- package/dist/testing/index.d.ts +2 -2
- package/dist/testing/index.js +3 -2
- package/dist/testing/index.js.map +1 -1
- 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/package.json +2 -2
|
@@ -12,7 +12,22 @@ function convertToV1FromChatGoogleMessage(message) {
|
|
|
12
12
|
type: "text",
|
|
13
13
|
text: message.content
|
|
14
14
|
}];
|
|
15
|
-
else
|
|
15
|
+
else {
|
|
16
|
+
const originalBlock = message.additional_kwargs?.originalTextContentBlock;
|
|
17
|
+
if (originalBlock?.thoughtSignature) {
|
|
18
|
+
if (!message.content.some((b) => "thoughtSignature" in b)) {
|
|
19
|
+
const result = [...message.content];
|
|
20
|
+
for (let i = result.length - 1; i >= 0; i--) {
|
|
21
|
+
const block = result[i];
|
|
22
|
+
if (block.type === "text" && !block.thought) {
|
|
23
|
+
block.thoughtSignature = originalBlock.thoughtSignature;
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return message.content;
|
|
30
|
+
}
|
|
16
31
|
});
|
|
17
32
|
for (const block of content) {
|
|
18
33
|
const contentBlockBase = require_utils.iife(() => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"google.cjs","names":["iife","_isContentBlock","_isString","_isObject"],"sources":["../../../src/messages/block_translators/google.ts"],"sourcesContent":["import { AIMessage } from \"../ai.js\";\nimport { ContentBlock } from \"../content/index.js\";\nimport type { StandardContentBlockTranslator } from \"./index.js\";\nimport { iife, _isContentBlock, _isObject, _isString } from \"./utils.js\";\n\nfunction convertToV1FromChatGoogleMessage(\n message: AIMessage\n): Array<ContentBlock.Standard> {\n function* iterateContent(): Iterable<ContentBlock.Standard> {\n const content = iife(() => {\n if (typeof message.content === \"string\") {\n if (message.additional_kwargs.originalTextContentBlock) {\n return [\n {\n ...message.additional_kwargs.originalTextContentBlock,\n type: \"text\",\n },\n ];\n } else {\n return [{ type: \"text\", text: message.content }];\n }\n } else {\n return message.content;\n }\n });\n for (const block of content) {\n const contentBlockBase: ContentBlock.Standard = iife(() => {\n if (_isContentBlock(block, \"text\") && _isString(block.text)) {\n return {\n type: \"text\",\n text: block.text,\n };\n } else if (\n _isContentBlock(block, \"inlineData\") &&\n _isObject(block.inlineData) &&\n _isString(block.inlineData.mimeType) &&\n _isString(block.inlineData.data)\n ) {\n return {\n type: \"file\",\n mimeType: block.inlineData.mimeType,\n data: block.inlineData.data,\n };\n } else if (\n _isContentBlock(block, \"functionCall\") &&\n _isObject(block.functionCall) &&\n _isString(block.functionCall.name) &&\n _isObject(block.functionCall.args)\n ) {\n return {\n type: \"tool_call\",\n id: message.id,\n name: block.functionCall.name,\n args: block.functionCall.args,\n };\n } else if (_isContentBlock(block, \"functionResponse\")) {\n return { type: \"non_standard\", value: block };\n } else if (\n _isContentBlock(block, \"fileData\") &&\n _isObject(block.fileData) &&\n _isString(block.fileData.mimeType) &&\n _isString(block.fileData.fileUri)\n ) {\n return {\n type: \"file\",\n mimeType: block.fileData.mimeType,\n fileId: block.fileData.fileUri,\n };\n } else if (_isContentBlock(block, \"executableCode\")) {\n return { type: \"non_standard\", value: block };\n } else if (_isContentBlock(block, \"codeExecutionResult\")) {\n return { type: \"non_standard\", value: block };\n }\n return { type: \"non_standard\", value: block };\n });\n const contentBlock: ContentBlock.Standard = iife(() => {\n if (\"thought\" in block && block.thought) {\n const reasoning: string =\n contentBlockBase.type === \"text\" ? contentBlockBase.text : \"\";\n return {\n type: \"reasoning\",\n reasoning,\n reasoningContentBlock: contentBlockBase,\n };\n } else {\n return contentBlockBase;\n }\n });\n\n const ret: ContentBlock.Standard = {\n thought: block.thought,\n thoughtSignature: block.thoughtSignature,\n partMetadata: block.partMetadata,\n ...contentBlock,\n };\n for (const attribute in ret) {\n if (ret[attribute] === undefined) {\n delete ret[attribute];\n }\n }\n\n yield ret;\n continue;\n }\n }\n return Array.from(iterateContent());\n}\n\nexport const ChatGoogleTranslator: StandardContentBlockTranslator = {\n translateContent: convertToV1FromChatGoogleMessage,\n translateContentChunk: convertToV1FromChatGoogleMessage,\n};\n"],"mappings":";;;AAKA,SAAS,iCACP,SAC8B;CAC9B,UAAU,iBAAkD;EAC1D,MAAM,UAAUA,yBAAW;AACzB,OAAI,OAAO,QAAQ,YAAY,SAC7B,KAAI,QAAQ,kBAAkB,yBAC5B,QAAO,CACL;IACE,GAAG,QAAQ,kBAAkB;IAC7B,MAAM;IACP,CACF;OAED,QAAO,CAAC;IAAE,MAAM;IAAQ,MAAM,QAAQ;IAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"google.cjs","names":["iife","_isContentBlock","_isString","_isObject"],"sources":["../../../src/messages/block_translators/google.ts"],"sourcesContent":["import { AIMessage } from \"../ai.js\";\nimport { ContentBlock } from \"../content/index.js\";\nimport type { StandardContentBlockTranslator } from \"./index.js\";\nimport { iife, _isContentBlock, _isObject, _isString } from \"./utils.js\";\n\nfunction convertToV1FromChatGoogleMessage(\n message: AIMessage\n): Array<ContentBlock.Standard> {\n function* iterateContent(): Iterable<ContentBlock.Standard> {\n const content = iife(() => {\n if (typeof message.content === \"string\") {\n if (message.additional_kwargs.originalTextContentBlock) {\n return [\n {\n ...message.additional_kwargs.originalTextContentBlock,\n type: \"text\",\n },\n ];\n } else {\n return [{ type: \"text\", text: message.content }];\n }\n } else {\n const originalBlock = message.additional_kwargs\n ?.originalTextContentBlock as Record<string, unknown> | undefined;\n if (originalBlock?.thoughtSignature) {\n // During streaming with thinking models, thoughtSignature arrives in\n // a metadata-only chunk and is stored in originalTextContentBlock.\n // When content is an array (due to thinking parts), this signature\n // isn't carried into the content array by mergeContent().\n // Merge it into the last non-thinking text block.\n const hasSignatureInContent = message.content.some(\n (b: Record<string, unknown>) => \"thoughtSignature\" in b\n );\n if (!hasSignatureInContent) {\n const result = [...message.content];\n for (let i = result.length - 1; i >= 0; i--) {\n const block = result[i] as Record<string, unknown>;\n if (block.type === \"text\" && !block.thought) {\n block.thoughtSignature = originalBlock.thoughtSignature;\n return result;\n }\n }\n }\n }\n return message.content;\n }\n });\n for (const block of content) {\n const contentBlockBase: ContentBlock.Standard = iife(() => {\n if (_isContentBlock(block, \"text\") && _isString(block.text)) {\n return {\n type: \"text\",\n text: block.text,\n };\n } else if (\n _isContentBlock(block, \"inlineData\") &&\n _isObject(block.inlineData) &&\n _isString(block.inlineData.mimeType) &&\n _isString(block.inlineData.data)\n ) {\n return {\n type: \"file\",\n mimeType: block.inlineData.mimeType,\n data: block.inlineData.data,\n };\n } else if (\n _isContentBlock(block, \"functionCall\") &&\n _isObject(block.functionCall) &&\n _isString(block.functionCall.name) &&\n _isObject(block.functionCall.args)\n ) {\n return {\n type: \"tool_call\",\n id: message.id,\n name: block.functionCall.name,\n args: block.functionCall.args,\n };\n } else if (_isContentBlock(block, \"functionResponse\")) {\n return { type: \"non_standard\", value: block };\n } else if (\n _isContentBlock(block, \"fileData\") &&\n _isObject(block.fileData) &&\n _isString(block.fileData.mimeType) &&\n _isString(block.fileData.fileUri)\n ) {\n return {\n type: \"file\",\n mimeType: block.fileData.mimeType,\n fileId: block.fileData.fileUri,\n };\n } else if (_isContentBlock(block, \"executableCode\")) {\n return { type: \"non_standard\", value: block };\n } else if (_isContentBlock(block, \"codeExecutionResult\")) {\n return { type: \"non_standard\", value: block };\n }\n return { type: \"non_standard\", value: block };\n });\n const contentBlock: ContentBlock.Standard = iife(() => {\n if (\"thought\" in block && block.thought) {\n const reasoning: string =\n contentBlockBase.type === \"text\" ? contentBlockBase.text : \"\";\n return {\n type: \"reasoning\",\n reasoning,\n reasoningContentBlock: contentBlockBase,\n };\n } else {\n return contentBlockBase;\n }\n });\n\n const ret: ContentBlock.Standard = {\n thought: block.thought,\n thoughtSignature: block.thoughtSignature,\n partMetadata: block.partMetadata,\n ...contentBlock,\n };\n for (const attribute in ret) {\n if (ret[attribute] === undefined) {\n delete ret[attribute];\n }\n }\n\n yield ret;\n continue;\n }\n }\n return Array.from(iterateContent());\n}\n\nexport const ChatGoogleTranslator: StandardContentBlockTranslator = {\n translateContent: convertToV1FromChatGoogleMessage,\n translateContentChunk: convertToV1FromChatGoogleMess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M,aAAa;KAC1B;aACQF,8BAAgB,OAAO,mBAAmB,CACnD,QAAO;KAAE,MAAM;KAAgB,OAAO;KAAO;aAE7CA,8BAAgB,OAAO,WAAW,IAClCE,wBAAU,MAAM,SAAS,IACzBD,wBAAU,MAAM,SAAS,SAAS,IAClCA,wBAAU,MAAM,SAAS,QAAQ,CAEjC,QAAO;KACL,MAAM;KACN,UAAU,MAAM,SAAS;KACzB,QAAQ,MAAM,SAAS;KACxB;aACQD,8BAAgB,OAAO,iBAAiB,CACjD,QAAO;KAAE,MAAM;KAAgB,OAAO;KAAO;aACpCA,8BAAgB,OAAO,sBAAsB,CACtD,QAAO;KAAE,MAAM;KAAgB,OAAO;KAAO;AAE/C,WAAO;KAAE,MAAM;KAAgB,OAAO;KAAO;KAC7C;GACF,MAAM,eAAsCD,yBAAW;AACrD,QAAI,aAAa,SAAS,MAAM,QAG9B,QAAO;KACL,MAAM;KACN,WAHA,iBAAiB,SAAS,SAAS,iBAAiB,OAAO;KAI3D,uBAAuB;KACxB;QAED,QAAO;KAET;GAEF,MAAM,MAA6B;IACjC,SAAS,MAAM;IACf,kBAAkB,MAAM;IACxB,cAAc,MAAM;IACpB,GAAG;IACJ;AACD,QAAK,MAAM,aAAa,IACtB,KAAI,IAAI,eAAe,OACrB,QAAO,IAAI;AAIf,SAAM;;;AAIV,QAAO,MAAM,KAAK,gBAAgB,CAAC;;AAGrC,MAAa,uBAAuD;CAClE,kBAAkB;CAClB,uBAAuB;CACxB"}
|
|
@@ -12,7 +12,22 @@ function convertToV1FromChatGoogleMessage(message) {
|
|
|
12
12
|
type: "text",
|
|
13
13
|
text: message.content
|
|
14
14
|
}];
|
|
15
|
-
else
|
|
15
|
+
else {
|
|
16
|
+
const originalBlock = message.additional_kwargs?.originalTextContentBlock;
|
|
17
|
+
if (originalBlock?.thoughtSignature) {
|
|
18
|
+
if (!message.content.some((b) => "thoughtSignature" in b)) {
|
|
19
|
+
const result = [...message.content];
|
|
20
|
+
for (let i = result.length - 1; i >= 0; i--) {
|
|
21
|
+
const block = result[i];
|
|
22
|
+
if (block.type === "text" && !block.thought) {
|
|
23
|
+
block.thoughtSignature = originalBlock.thoughtSignature;
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return message.content;
|
|
30
|
+
}
|
|
16
31
|
});
|
|
17
32
|
for (const block of content) {
|
|
18
33
|
const contentBlockBase = iife(() => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"google.js","names":[],"sources":["../../../src/messages/block_translators/google.ts"],"sourcesContent":["import { AIMessage } from \"../ai.js\";\nimport { ContentBlock } from \"../content/index.js\";\nimport type { StandardContentBlockTranslator } from \"./index.js\";\nimport { iife, _isContentBlock, _isObject, _isString } from \"./utils.js\";\n\nfunction convertToV1FromChatGoogleMessage(\n message: AIMessage\n): Array<ContentBlock.Standard> {\n function* iterateContent(): Iterable<ContentBlock.Standard> {\n const content = iife(() => {\n if (typeof message.content === \"string\") {\n if (message.additional_kwargs.originalTextContentBlock) {\n return [\n {\n ...message.additional_kwargs.originalTextContentBlock,\n type: \"text\",\n },\n ];\n } else {\n return [{ type: \"text\", text: message.content }];\n }\n } else {\n return message.content;\n }\n });\n for (const block of content) {\n const contentBlockBase: ContentBlock.Standard = iife(() => {\n if (_isContentBlock(block, \"text\") && _isString(block.text)) {\n return {\n type: \"text\",\n text: block.text,\n };\n } else if (\n _isContentBlock(block, \"inlineData\") &&\n _isObject(block.inlineData) &&\n _isString(block.inlineData.mimeType) &&\n _isString(block.inlineData.data)\n ) {\n return {\n type: \"file\",\n mimeType: block.inlineData.mimeType,\n data: block.inlineData.data,\n };\n } else if (\n _isContentBlock(block, \"functionCall\") &&\n _isObject(block.functionCall) &&\n _isString(block.functionCall.name) &&\n _isObject(block.functionCall.args)\n ) {\n return {\n type: \"tool_call\",\n id: message.id,\n name: block.functionCall.name,\n args: block.functionCall.args,\n };\n } else if (_isContentBlock(block, \"functionResponse\")) {\n return { type: \"non_standard\", value: block };\n } else if (\n _isContentBlock(block, \"fileData\") &&\n _isObject(block.fileData) &&\n _isString(block.fileData.mimeType) &&\n _isString(block.fileData.fileUri)\n ) {\n return {\n type: \"file\",\n mimeType: block.fileData.mimeType,\n fileId: block.fileData.fileUri,\n };\n } else if (_isContentBlock(block, \"executableCode\")) {\n return { type: \"non_standard\", value: block };\n } else if (_isContentBlock(block, \"codeExecutionResult\")) {\n return { type: \"non_standard\", value: block };\n }\n return { type: \"non_standard\", value: block };\n });\n const contentBlock: ContentBlock.Standard = iife(() => {\n if (\"thought\" in block && block.thought) {\n const reasoning: string =\n contentBlockBase.type === \"text\" ? contentBlockBase.text : \"\";\n return {\n type: \"reasoning\",\n reasoning,\n reasoningContentBlock: contentBlockBase,\n };\n } else {\n return contentBlockBase;\n }\n });\n\n const ret: ContentBlock.Standard = {\n thought: block.thought,\n thoughtSignature: block.thoughtSignature,\n partMetadata: block.partMetadata,\n ...contentBlock,\n };\n for (const attribute in ret) {\n if (ret[attribute] === undefined) {\n delete ret[attribute];\n }\n }\n\n yield ret;\n continue;\n }\n }\n return Array.from(iterateContent());\n}\n\nexport const ChatGoogleTranslator: StandardContentBlockTranslator = {\n translateContent: convertToV1FromChatGoogleMessage,\n translateContentChunk: convertToV1FromChatGoogleMessage,\n};\n"],"mappings":";;;AAKA,SAAS,iCACP,SAC8B;CAC9B,UAAU,iBAAkD;EAC1D,MAAM,UAAU,WAAW;AACzB,OAAI,OAAO,QAAQ,YAAY,SAC7B,KAAI,QAAQ,kBAAkB,yBAC5B,QAAO,CACL;IACE,GAAG,QAAQ,kBAAkB;IAC7B,MAAM;IACP,CACF;OAED,QAAO,CAAC;IAAE,MAAM;IAAQ,MAAM,QAAQ;IAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"google.js","names":[],"sources":["../../../src/messages/block_translators/google.ts"],"sourcesContent":["import { AIMessage } from \"../ai.js\";\nimport { ContentBlock } from \"../content/index.js\";\nimport type { StandardContentBlockTranslator } from \"./index.js\";\nimport { iife, _isContentBlock, _isObject, _isString } from \"./utils.js\";\n\nfunction convertToV1FromChatGoogleMessage(\n message: AIMessage\n): Array<ContentBlock.Standard> {\n function* iterateContent(): Iterable<ContentBlock.Standard> {\n const content = iife(() => {\n if (typeof message.content === \"string\") {\n if (message.additional_kwargs.originalTextContentBlock) {\n return [\n {\n ...message.additional_kwargs.originalTextContentBlock,\n type: \"text\",\n },\n ];\n } else {\n return [{ type: \"text\", text: message.content }];\n }\n } else {\n const originalBlock = message.additional_kwargs\n ?.originalTextContentBlock as Record<string, unknown> | undefined;\n if (originalBlock?.thoughtSignature) {\n // During streaming with thinking models, thoughtSignature arrives in\n // a metadata-only chunk and is stored in originalTextContentBlock.\n // When content is an array (due to thinking parts), this signature\n // isn't carried into the content array by mergeContent().\n // Merge it into the last non-thinking text block.\n const hasSignatureInContent = message.content.some(\n (b: Record<string, unknown>) => \"thoughtSignature\" in b\n );\n if (!hasSignatureInContent) {\n const result = [...message.content];\n for (let i = result.length - 1; i >= 0; i--) {\n const block = result[i] as Record<string, unknown>;\n if (block.type === \"text\" && !block.thought) {\n block.thoughtSignature = originalBlock.thoughtSignature;\n return result;\n }\n }\n }\n }\n return message.content;\n }\n });\n for (const block of content) {\n const contentBlockBase: ContentBlock.Standard = iife(() => {\n if (_isContentBlock(block, \"text\") && _isString(block.text)) {\n return {\n type: \"text\",\n text: block.text,\n };\n } else if (\n _isContentBlock(block, \"inlineData\") &&\n _isObject(block.inlineData) &&\n _isString(block.inlineData.mimeType) &&\n _isString(block.inlineData.data)\n ) {\n return {\n type: \"file\",\n mimeType: block.inlineData.mimeType,\n data: block.inlineData.data,\n };\n } else if (\n _isContentBlock(block, \"functionCall\") &&\n _isObject(block.functionCall) &&\n _isString(block.functionCall.name) &&\n _isObject(block.functionCall.args)\n ) {\n return {\n type: \"tool_call\",\n id: message.id,\n name: block.functionCall.name,\n args: block.functionCall.args,\n };\n } else if (_isContentBlock(block, \"functionResponse\")) {\n return { type: \"non_standard\", value: block };\n } else if (\n _isContentBlock(block, \"fileData\") &&\n _isObject(block.fileData) &&\n _isString(block.fileData.mimeType) &&\n _isString(block.fileData.fileUri)\n ) {\n return {\n type: \"file\",\n mimeType: block.fileData.mimeType,\n fileId: block.fileData.fileUri,\n };\n } else if (_isContentBlock(block, \"executableCode\")) {\n return { type: \"non_standard\", value: block };\n } else if (_isContentBlock(block, \"codeExecutionResult\")) {\n return { type: \"non_standard\", value: block };\n }\n return { type: \"non_standard\", value: block };\n });\n const contentBlock: ContentBlock.Standard = iife(() => {\n if (\"thought\" in block && block.thought) {\n const reasoning: string =\n contentBlockBase.type === \"text\" ? contentBlockBase.text : \"\";\n return {\n type: \"reasoning\",\n reasoning,\n reasoningContentBlock: contentBlockBase,\n };\n } else {\n return contentBlockBase;\n }\n });\n\n const ret: ContentBlock.Standard = {\n thought: block.thought,\n thoughtSignature: block.thoughtSignature,\n partMetadata: block.partMetadata,\n ...contentBlock,\n };\n for (const attribute in ret) {\n if (ret[attribute] === undefined) {\n delete ret[attribute];\n }\n }\n\n yield ret;\n continue;\n }\n }\n return Array.from(iterateContent());\n}\n\nexport const ChatGoogleTranslator: StandardContentBlockTranslator = {\n translateContent: convertToV1FromChatGoogleMessage,\n translateContentChunk: convertToV1FromChatGoogleMessage,\n};\n"],"mappings":";;;AAKA,SAAS,iCACP,SAC8B;CAC9B,UAAU,iBAAkD;EAC1D,MAAM,UAAU,WAAW;AACzB,OAAI,OAAO,QAAQ,YAAY,SAC7B,KAAI,QAAQ,kBAAkB,yBAC5B,QAAO,CACL;IACE,GAAG,QAAQ,kBAAkB;IAC7B,MAAM;IACP,CACF;OAED,QAAO,CAAC;IAAE,MAAM;IAAQ,MAAM,QAAQ;IAAS,CAAC;QAE7C;IACL,MAAM,gBAAgB,QAAQ,mBAC1B;AACJ,QAAI,eAAe,kBASjB;SAAI,CAH0B,QAAQ,QAAQ,MAC3C,MAA+B,sBAAsB,EACvD,EAC2B;MAC1B,M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AAgB,OAAO;KAAO;KAC7C;GACF,MAAM,eAAsC,WAAW;AACrD,QAAI,aAAa,SAAS,MAAM,QAG9B,QAAO;KACL,MAAM;KACN,WAHA,iBAAiB,SAAS,SAAS,iBAAiB,OAAO;KAI3D,uBAAuB;KACxB;QAED,QAAO;KAET;GAEF,MAAM,MAA6B;IACjC,SAAS,MAAM;IACf,kBAAkB,MAAM;IACxB,cAAc,MAAM;IACpB,GAAG;IACJ;AACD,QAAK,MAAM,aAAa,IACtB,KAAI,IAAI,eAAe,OACrB,QAAO,IAAI;AAIf,SAAM;;;AAIV,QAAO,MAAM,KAAK,gBAAgB,CAAC;;AAGrC,MAAa,uBAAuD;CAClE,kBAAkB;CAClB,uBAAuB;CACxB"}
|
|
@@ -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
|
|
@@ -1 +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 *\n * Each builder method queues a model response consumed in order per `invoke()`:\n *\n * `.respond(entry)` — enqueue a `BaseMessage`, `Error`, or factory function.\n *\n * `.respondWithTools(toolCalls[])` — enqueue an `AIMessage` with the given\n * tool calls. Content is derived from the input messages automatically.\n *\n * Both can be mixed freely in one chain. When all queued responses are\n * consumed, further invocations throw.\n *\n * Additional configuration:\n * - `.alwaysThrow(error)` — every call throws (overrides the queue)\n * - `.structuredResponse(value)` — value returned by `withStructuredOutput()`\n *\n * The model records all invocations in `.calls` / `.callCount`.\n */\nclass 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 get calls(): FakeModelCall[] {\n return this._calls;\n }\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 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 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 alwaysThrow(error: Error): this {\n this._alwaysThrowError = error;\n return this;\n }\n\n structuredResponse(value: Record<string, any>): this {\n this._structuredResponseValue = value;\n return this;\n }\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 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 fake chat model for testing.\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,YAAY;AAChB,SAAS,iBAAyB;AAChC,cAAa;AACb,QAAO,WAAW;;;;;;;;;;;;;;;;;;;;;;;;AAyBpB,IAAM,iBAAN,MAAM,uBAAuBA,kDAAc;CACzC,AAAQ,QAAsB,EAAE;CAEhC,AAAQ;CAER,AAAQ;CAER,AAAQ,SAAwC,EAAE;CAElD,AAAQ,aAAa;CAErB,AAAQ,SAA0B,EAAE;CAEpC,IAAI,QAAyB;AAC3B,SAAO,KAAK;;CAGd,IAAI,YAAoB;AACtB,SAAO,KAAK,OAAO;;CAGrB,cAAc;AACZ,QAAM,EAAE,CAAC;;CAGX,WAAmB;AACjB,SAAO;;CAGT,oBAAoB;AAClB,SAAO,EAAE;;CAGX,QAAQ,OAAoD;AAC1D,MAAI,OAAO,UAAU,WACnB,MAAK,MAAM,KAAK;GAAE,MAAM;GAAW,SAAS;GAAO,CAAC;WAC3CC,yBAAY,WAAW,MAAM,CACtC,MAAK,MAAM,KAAK;GAAE,MAAM;GAAW,SAAS;GAAO,CAAC;MAEpD,MAAK,MAAM,KAAK;GAAE,MAAM;GAAS,OAAO;GAAO,CAAC;AAElD,SAAO;;CAGT,iBACE,WACM;AACN,OAAK,MAAM,KAAK;GACd,MAAM;GACN,WAAW,UAAU,KAAK,QAAQ;IAChC,MAAM,GAAG;IACT,MAAM,GAAG;IACT,IAAI,GAAG,MAAM,gBAAgB;IAC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AAO;GACL,aAAa,CAAC;IAAE,MAAM;IAAS,SAbjB,IAAIE,qBAAU;KAC5B;KACA,IAAI,iBAAiB,UAAU;KAC/B,YACE,MAAM,UAAU,SAAS,IACrB,MAAM,UAAU,KAAK,QAAQ;MAC3B,GAAG;MACH,MAAM;MACP,EAAE,GACH;KACP,CAAC;IAGwC,CAAC;GACzC,WAAW,EAAE;GACd;;;;;;;;;;;;;;;;;;;AAoBL,SAAgB,YAA4B;AAC1C,QAAO,IAAI,gBAAgB"}
|
|
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,YAAY;AAChB,SAAS,iBAAyB;AAChC,cAAa;AACb,QAAO,WAAW;;;;;;;;;;AAWpB,IAAa,iBAAb,MAAa,uBAAuBA,kDAAc;CAChD,AAAQ,QAAsB,EAAE;CAEhC,AAAQ;CAER,AAAQ;CAER,AAAQ,SAAwC,EAAE;CAElD,AAAQ,aAAa;CAErB,AAAQ,SAA0B,EAAE;;;;;;CAOpC,IAAI,QAAyB;AAC3B,SAAO,KAAK;;;;;CAMd,IAAI,YAAoB;AACtB,SAAO,KAAK,OAAO;;CAGrB,cAAc;AACZ,QAAM,EAAE,CAAC;;CAGX,WAAmB;AACjB,SAAO;;CAGT,oBAAoB;AAClB,SAAO,EAAE;;;;;;;;CASX,QAAQ,OAAoD;AAC1D,MAAI,OAAO,UAAU,WACnB,MAAK,MAAM,KAAK;GAAE,MAAM;GAAW,SAAS;GAAO,CAAC;WAC3CC,yBAAY,WAAW,MAAM,CACtC,MAAK,MAAM,KAAK;GAAE,MAAM;GAAW,SAAS;GAAO,CAAC;MAEpD,MAAK,MAAM,KAAK;GAAE,MAAM;GAAS,OAAO;GAAO,CAAC;AAElD,SAAO;;;;;;;;;CAUT,iBACE,WACM;AACN,OAAK,MAAM,KAAK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;;AAGH,MAAI,MAAM,SAAS,UACjB,QAAO,EACL,aAAa,CAAC;GAAE,MAAM;GAAI,SAAS,MAAM;GAAS,CAAC,EACpD;EAGH,MAAM,UAAU,cAAc,SAAS;AAavC,SAAO;GACL,aAAa,CAAC;IAAE,MAAM;IAAS,SAbjB,IAAIE,qBAAU;KAC5B;KACA,IAAI,iBAAiB,UAAU;KAC/B,YACE,MAAM,UAAU,SAAS,IACrB,MAAM,UAAU,KAAK,QAAQ;MAC3B,GAAG;MACH,MAAM;MACP,EAAE,GACH;KACP,CAAC;IAGwC,CAAC;GACzC,WAAW,EAAE;GACd;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwCL,SAAgB,YAA4B;AAC1C,QAAO,IAAI,gBAAgB"}
|
|
@@ -21,22 +21,8 @@ interface FakeModelCall {
|
|
|
21
21
|
*
|
|
22
22
|
* Queue responses with `.respond()` and `.respondWithTools()`, then
|
|
23
23
|
* pass the instance directly wherever a chat model is expected.
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* `.respond(entry)` — enqueue a `BaseMessage`, `Error`, or factory function.
|
|
28
|
-
*
|
|
29
|
-
* `.respondWithTools(toolCalls[])` — enqueue an `AIMessage` with the given
|
|
30
|
-
* tool calls. Content is derived from the input messages automatically.
|
|
31
|
-
*
|
|
32
|
-
* Both can be mixed freely in one chain. When all queued responses are
|
|
33
|
-
* consumed, further invocations throw.
|
|
34
|
-
*
|
|
35
|
-
* Additional configuration:
|
|
36
|
-
* - `.alwaysThrow(error)` — every call throws (overrides the queue)
|
|
37
|
-
* - `.structuredResponse(value)` — value returned by `withStructuredOutput()`
|
|
38
|
-
*
|
|
39
|
-
* The model records all invocations in `.calls` / `.callCount`.
|
|
24
|
+
* Responses are consumed in first-in-first-out order — one per `invoke()` call.
|
|
25
|
+
* When all queued responses are consumed, further invocations throw.
|
|
40
26
|
*/
|
|
41
27
|
declare class FakeBuiltModel extends BaseChatModel {
|
|
42
28
|
private queue;
|
|
@@ -45,20 +31,65 @@ declare class FakeBuiltModel extends BaseChatModel {
|
|
|
45
31
|
private _tools;
|
|
46
32
|
private _callIndex;
|
|
47
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
|
+
*/
|
|
48
39
|
get calls(): FakeModelCall[];
|
|
40
|
+
/**
|
|
41
|
+
* The number of times this model has been invoked.
|
|
42
|
+
*/
|
|
49
43
|
get callCount(): number;
|
|
50
44
|
constructor();
|
|
51
45
|
_llmType(): string;
|
|
52
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
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
|
+
*/
|
|
54
61
|
respondWithTools(toolCalls: Array<{
|
|
55
62
|
name: string;
|
|
56
63
|
args: Record<string, any>;
|
|
57
64
|
id?: string;
|
|
58
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
|
+
*/
|
|
59
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
|
+
*/
|
|
60
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
|
+
*/
|
|
61
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
|
+
*/
|
|
62
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, {
|
|
63
94
|
raw: BaseMessage;
|
|
64
95
|
parsed: RunOutput;
|
|
@@ -66,7 +97,27 @@ declare class FakeBuiltModel extends BaseChatModel {
|
|
|
66
97
|
_generate(messages: BaseMessage[], options?: this["ParsedCallOptions"], _runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
|
|
67
98
|
}
|
|
68
99
|
/**
|
|
69
|
-
* Creates a
|
|
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. |
|
|
70
121
|
*
|
|
71
122
|
* @example
|
|
72
123
|
* ```typescript
|
|
@@ -83,5 +134,5 @@ declare class FakeBuiltModel extends BaseChatModel {
|
|
|
83
134
|
*/
|
|
84
135
|
declare function fakeModel(): FakeBuiltModel;
|
|
85
136
|
//#endregion
|
|
86
|
-
export { fakeModel };
|
|
137
|
+
export { FakeBuiltModel, fakeModel };
|
|
87
138
|
//# sourceMappingURL=fake_model_builder.d.cts.map
|
|
@@ -1 +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;EAC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,SAAA;EAAA;EAQ5B,SAAA,CACJ,QAAA,EAAU,WAAA,IACV,OAAA,8BACA,WAAA,GAAc,wBAAA,GACb,OAAA,CAAQ,UAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA6FG,SAAA,CAAA,GAAa,cAAA"}
|
|
@@ -22,22 +22,8 @@ interface FakeModelCall {
|
|
|
22
22
|
*
|
|
23
23
|
* Queue responses with `.respond()` and `.respondWithTools()`, then
|
|
24
24
|
* pass the instance directly wherever a chat model is expected.
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
* `.respond(entry)` — enqueue a `BaseMessage`, `Error`, or factory function.
|
|
29
|
-
*
|
|
30
|
-
* `.respondWithTools(toolCalls[])` — enqueue an `AIMessage` with the given
|
|
31
|
-
* tool calls. Content is derived from the input messages automatically.
|
|
32
|
-
*
|
|
33
|
-
* Both can be mixed freely in one chain. When all queued responses are
|
|
34
|
-
* consumed, further invocations throw.
|
|
35
|
-
*
|
|
36
|
-
* Additional configuration:
|
|
37
|
-
* - `.alwaysThrow(error)` — every call throws (overrides the queue)
|
|
38
|
-
* - `.structuredResponse(value)` — value returned by `withStructuredOutput()`
|
|
39
|
-
*
|
|
40
|
-
* The model records all invocations in `.calls` / `.callCount`.
|
|
25
|
+
* Responses are consumed in first-in-first-out order — one per `invoke()` call.
|
|
26
|
+
* When all queued responses are consumed, further invocations throw.
|
|
41
27
|
*/
|
|
42
28
|
declare class FakeBuiltModel extends BaseChatModel {
|
|
43
29
|
private queue;
|
|
@@ -46,20 +32,65 @@ declare class FakeBuiltModel extends BaseChatModel {
|
|
|
46
32
|
private _tools;
|
|
47
33
|
private _callIndex;
|
|
48
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
|
+
*/
|
|
49
40
|
get calls(): FakeModelCall[];
|
|
41
|
+
/**
|
|
42
|
+
* The number of times this model has been invoked.
|
|
43
|
+
*/
|
|
50
44
|
get callCount(): number;
|
|
51
45
|
constructor();
|
|
52
46
|
_llmType(): string;
|
|
53
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
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
|
+
*/
|
|
55
62
|
respondWithTools(toolCalls: Array<{
|
|
56
63
|
name: string;
|
|
57
64
|
args: Record<string, any>;
|
|
58
65
|
id?: string;
|
|
59
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
|
+
*/
|
|
60
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
|
+
*/
|
|
61
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
|
+
*/
|
|
62
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
|
+
*/
|
|
63
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, {
|
|
64
95
|
raw: BaseMessage;
|
|
65
96
|
parsed: RunOutput;
|
|
@@ -67,7 +98,27 @@ declare class FakeBuiltModel extends BaseChatModel {
|
|
|
67
98
|
_generate(messages: BaseMessage[], options?: this["ParsedCallOptions"], _runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
|
|
68
99
|
}
|
|
69
100
|
/**
|
|
70
|
-
* Creates a
|
|
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. |
|
|
71
122
|
*
|
|
72
123
|
* @example
|
|
73
124
|
* ```typescript
|
|
@@ -84,5 +135,5 @@ declare class FakeBuiltModel extends BaseChatModel {
|
|
|
84
135
|
*/
|
|
85
136
|
declare function fakeModel(): FakeBuiltModel;
|
|
86
137
|
//#endregion
|
|
87
|
-
export { fakeModel };
|
|
138
|
+
export { FakeBuiltModel, fakeModel };
|
|
88
139
|
//# sourceMappingURL=fake_model_builder.d.ts.map
|
|
@@ -1 +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
|
|
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;;;;;;;;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