@langchain/core 0.2.18 → 0.2.20

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.
Files changed (155) hide show
  1. package/dist/callbacks/manager.cjs +94 -19
  2. package/dist/callbacks/manager.d.ts +3 -1
  3. package/dist/callbacks/manager.js +94 -19
  4. package/dist/language_models/base.d.ts +0 -10
  5. package/dist/language_models/chat_models.cjs +5 -6
  6. package/dist/language_models/chat_models.d.ts +9 -10
  7. package/dist/language_models/chat_models.js +5 -6
  8. package/dist/language_models/llms.cjs +5 -6
  9. package/dist/language_models/llms.d.ts +10 -12
  10. package/dist/language_models/llms.js +5 -6
  11. package/dist/messages/base.cjs +16 -1
  12. package/dist/messages/base.d.ts +9 -0
  13. package/dist/messages/base.js +14 -0
  14. package/dist/messages/tool.cjs +23 -0
  15. package/dist/messages/tool.d.ts +15 -0
  16. package/dist/messages/tool.js +24 -1
  17. package/dist/messages/utils.cjs +1 -1
  18. package/dist/messages/utils.js +1 -1
  19. package/dist/output_parsers/string.cjs +1 -0
  20. package/dist/output_parsers/string.js +1 -0
  21. package/dist/runnables/base.cjs +35 -12
  22. package/dist/runnables/base.js +35 -12
  23. package/dist/runnables/config.cjs +41 -0
  24. package/dist/runnables/config.js +41 -0
  25. package/dist/runnables/remote.cjs +17 -14
  26. package/dist/runnables/remote.js +17 -14
  27. package/dist/runnables/types.d.ts +10 -0
  28. package/dist/tools/index.cjs +15 -6
  29. package/dist/tools/index.d.ts +21 -12
  30. package/dist/tools/index.js +15 -6
  31. package/dist/utils/math.cjs +6 -4
  32. package/dist/utils/math.js +6 -4
  33. package/dist/utils/ml-distance/distances.cjs +18 -0
  34. package/dist/utils/ml-distance/distances.d.ts +8 -0
  35. package/dist/utils/ml-distance/distances.js +14 -0
  36. package/dist/utils/ml-distance/similarities.cjs +21 -0
  37. package/dist/utils/ml-distance/similarities.d.ts +7 -0
  38. package/dist/utils/ml-distance/similarities.js +17 -0
  39. package/dist/utils/ml-distance-euclidean/euclidean.cjs +15 -0
  40. package/dist/utils/ml-distance-euclidean/euclidean.d.ts +2 -0
  41. package/dist/utils/ml-distance-euclidean/euclidean.js +10 -0
  42. package/dist/utils/signal.cjs +28 -0
  43. package/dist/utils/signal.d.ts +1 -0
  44. package/dist/utils/signal.js +24 -0
  45. package/dist/utils/stream.cjs +19 -4
  46. package/dist/utils/stream.d.ts +3 -1
  47. package/dist/utils/stream.js +19 -4
  48. package/dist/utils/testing/index.cjs +168 -4
  49. package/dist/utils/testing/index.d.ts +100 -6
  50. package/dist/utils/testing/index.js +167 -4
  51. package/package.json +4 -4
  52. package/dist/caches/tests/in_memory_cache.test.d.ts +0 -1
  53. package/dist/caches/tests/in_memory_cache.test.js +0 -33
  54. package/dist/callbacks/tests/callbacks.test.d.ts +0 -1
  55. package/dist/callbacks/tests/callbacks.test.js +0 -495
  56. package/dist/callbacks/tests/manager.int.test.d.ts +0 -1
  57. package/dist/callbacks/tests/manager.int.test.js +0 -29
  58. package/dist/callbacks/tests/run_collector.test.d.ts +0 -1
  59. package/dist/callbacks/tests/run_collector.test.js +0 -58
  60. package/dist/language_models/tests/chat_models.test.d.ts +0 -1
  61. package/dist/language_models/tests/chat_models.test.js +0 -204
  62. package/dist/language_models/tests/count_tokens.test.d.ts +0 -1
  63. package/dist/language_models/tests/count_tokens.test.js +0 -19
  64. package/dist/language_models/tests/llms.test.d.ts +0 -1
  65. package/dist/language_models/tests/llms.test.js +0 -52
  66. package/dist/messages/tests/base_message.test.d.ts +0 -1
  67. package/dist/messages/tests/base_message.test.js +0 -245
  68. package/dist/messages/tests/message_utils.test.d.ts +0 -1
  69. package/dist/messages/tests/message_utils.test.js +0 -434
  70. package/dist/output_parsers/openai_tools/tests/json_output_tools_parser.test.d.ts +0 -1
  71. package/dist/output_parsers/openai_tools/tests/json_output_tools_parser.test.js +0 -81
  72. package/dist/output_parsers/tests/json.test.d.ts +0 -1
  73. package/dist/output_parsers/tests/json.test.js +0 -427
  74. package/dist/output_parsers/tests/output_parser.test.d.ts +0 -1
  75. package/dist/output_parsers/tests/output_parser.test.js +0 -78
  76. package/dist/output_parsers/tests/string.test.d.ts +0 -1
  77. package/dist/output_parsers/tests/string.test.js +0 -68
  78. package/dist/output_parsers/tests/structured.test.d.ts +0 -1
  79. package/dist/output_parsers/tests/structured.test.js +0 -166
  80. package/dist/output_parsers/tests/xml.test.d.ts +0 -1
  81. package/dist/output_parsers/tests/xml.test.js +0 -81
  82. package/dist/prompts/tests/chat.mustache.test.d.ts +0 -1
  83. package/dist/prompts/tests/chat.mustache.test.js +0 -129
  84. package/dist/prompts/tests/chat.test.d.ts +0 -1
  85. package/dist/prompts/tests/chat.test.js +0 -557
  86. package/dist/prompts/tests/few_shot.test.d.ts +0 -1
  87. package/dist/prompts/tests/few_shot.test.js +0 -224
  88. package/dist/prompts/tests/pipeline.test.d.ts +0 -1
  89. package/dist/prompts/tests/pipeline.test.js +0 -101
  90. package/dist/prompts/tests/prompt.mustache.test.d.ts +0 -1
  91. package/dist/prompts/tests/prompt.mustache.test.js +0 -105
  92. package/dist/prompts/tests/prompt.test.d.ts +0 -1
  93. package/dist/prompts/tests/prompt.test.js +0 -78
  94. package/dist/prompts/tests/structured.test.d.ts +0 -1
  95. package/dist/prompts/tests/structured.test.js +0 -37
  96. package/dist/prompts/tests/template.test.d.ts +0 -1
  97. package/dist/prompts/tests/template.test.js +0 -24
  98. package/dist/runnables/tests/runnable.test.d.ts +0 -1
  99. package/dist/runnables/tests/runnable.test.js +0 -491
  100. package/dist/runnables/tests/runnable_binding.test.d.ts +0 -1
  101. package/dist/runnables/tests/runnable_binding.test.js +0 -46
  102. package/dist/runnables/tests/runnable_branch.test.d.ts +0 -1
  103. package/dist/runnables/tests/runnable_branch.test.js +0 -116
  104. package/dist/runnables/tests/runnable_graph.test.d.ts +0 -1
  105. package/dist/runnables/tests/runnable_graph.test.js +0 -100
  106. package/dist/runnables/tests/runnable_history.test.d.ts +0 -1
  107. package/dist/runnables/tests/runnable_history.test.js +0 -177
  108. package/dist/runnables/tests/runnable_interface.test.d.ts +0 -1
  109. package/dist/runnables/tests/runnable_interface.test.js +0 -209
  110. package/dist/runnables/tests/runnable_map.test.d.ts +0 -1
  111. package/dist/runnables/tests/runnable_map.test.js +0 -238
  112. package/dist/runnables/tests/runnable_passthrough.test.d.ts +0 -1
  113. package/dist/runnables/tests/runnable_passthrough.test.js +0 -96
  114. package/dist/runnables/tests/runnable_remote.int.test.d.ts +0 -1
  115. package/dist/runnables/tests/runnable_remote.int.test.js +0 -138
  116. package/dist/runnables/tests/runnable_remote.test.d.ts +0 -1
  117. package/dist/runnables/tests/runnable_remote.test.js +0 -200
  118. package/dist/runnables/tests/runnable_retry.test.d.ts +0 -1
  119. package/dist/runnables/tests/runnable_retry.test.js +0 -125
  120. package/dist/runnables/tests/runnable_stream_events.test.d.ts +0 -1
  121. package/dist/runnables/tests/runnable_stream_events.test.js +0 -1013
  122. package/dist/runnables/tests/runnable_stream_events_v2.test.d.ts +0 -1
  123. package/dist/runnables/tests/runnable_stream_events_v2.test.js +0 -2022
  124. package/dist/runnables/tests/runnable_stream_log.test.d.ts +0 -1
  125. package/dist/runnables/tests/runnable_stream_log.test.js +0 -71
  126. package/dist/runnables/tests/runnable_tools.test.d.ts +0 -1
  127. package/dist/runnables/tests/runnable_tools.test.js +0 -149
  128. package/dist/runnables/tests/runnable_tracing.int.test.d.ts +0 -1
  129. package/dist/runnables/tests/runnable_tracing.int.test.js +0 -37
  130. package/dist/runnables/tests/runnable_with_fallbacks.test.d.ts +0 -1
  131. package/dist/runnables/tests/runnable_with_fallbacks.test.js +0 -36
  132. package/dist/singletons/tests/async_local_storage.test.d.ts +0 -1
  133. package/dist/singletons/tests/async_local_storage.test.js +0 -153
  134. package/dist/structured_query/tests/utils.test.d.ts +0 -1
  135. package/dist/structured_query/tests/utils.test.js +0 -47
  136. package/dist/tools/tests/tools.test.d.ts +0 -1
  137. package/dist/tools/tests/tools.test.js +0 -85
  138. package/dist/tracers/tests/langchain_tracer.int.test.d.ts +0 -1
  139. package/dist/tracers/tests/langchain_tracer.int.test.js +0 -74
  140. package/dist/tracers/tests/langsmith_interop.test.d.ts +0 -1
  141. package/dist/tracers/tests/langsmith_interop.test.js +0 -551
  142. package/dist/tracers/tests/tracer.test.d.ts +0 -1
  143. package/dist/tracers/tests/tracer.test.js +0 -378
  144. package/dist/utils/testing/tests/chatfake.test.d.ts +0 -1
  145. package/dist/utils/testing/tests/chatfake.test.js +0 -112
  146. package/dist/utils/tests/async_caller.test.d.ts +0 -1
  147. package/dist/utils/tests/async_caller.test.js +0 -27
  148. package/dist/utils/tests/enviroment.test.d.ts +0 -1
  149. package/dist/utils/tests/enviroment.test.js +0 -6
  150. package/dist/utils/tests/function_calling.test.d.ts +0 -1
  151. package/dist/utils/tests/function_calling.test.js +0 -107
  152. package/dist/utils/tests/math_utils.test.d.ts +0 -1
  153. package/dist/utils/tests/math_utils.test.js +0 -139
  154. package/dist/utils/tests/polyfill_stream.test.d.ts +0 -1
  155. package/dist/utils/tests/polyfill_stream.test.js +0 -15
@@ -1,204 +0,0 @@
1
- /* eslint-disable no-promise-executor-return */
2
- import { test } from "@jest/globals";
3
- import { z } from "zod";
4
- import { zodToJsonSchema } from "zod-to-json-schema";
5
- import { FakeChatModel, FakeListChatModel } from "../../utils/testing/index.js";
6
- import { HumanMessage } from "../../messages/human.js";
7
- import { getBufferString } from "../../messages/utils.js";
8
- test("Test ChatModel accepts array shorthand for messages", async () => {
9
- const model = new FakeChatModel({});
10
- const response = await model.invoke([["human", "Hello there!"]]);
11
- expect(response.content).toEqual("Hello there!");
12
- });
13
- test("Test ChatModel accepts object shorthand for messages", async () => {
14
- const model = new FakeChatModel({});
15
- const response = await model.invoke([
16
- {
17
- type: "human",
18
- content: "Hello there!",
19
- additional_kwargs: {},
20
- example: true,
21
- },
22
- ]);
23
- expect(response.content).toEqual("Hello there!");
24
- });
25
- test("Test ChatModel uses callbacks", async () => {
26
- const model = new FakeChatModel({});
27
- let acc = "";
28
- const response = await model.invoke("Hello there!", {
29
- callbacks: [
30
- {
31
- handleLLMNewToken: (token) => {
32
- console.log(token);
33
- acc += token;
34
- },
35
- },
36
- ],
37
- });
38
- expect(response.content).toEqual(acc);
39
- });
40
- test("Test ChatModel uses callbacks with a cache", async () => {
41
- const model = new FakeChatModel({
42
- cache: true,
43
- });
44
- let acc = "";
45
- const response = await model.invoke("Hello there!");
46
- const response2 = await model.invoke("Hello there!", {
47
- callbacks: [
48
- {
49
- handleLLMNewToken: (token) => {
50
- console.log(token);
51
- acc += token;
52
- },
53
- },
54
- ],
55
- });
56
- // If callbacks are backgrounded
57
- await new Promise((resolve) => setTimeout(resolve, 1000));
58
- expect(response.content).toEqual(response2.content);
59
- expect(response2.content).toEqual(acc);
60
- });
61
- test("Test ChatModel legacy params withStructuredOutput", async () => {
62
- const model = new FakeListChatModel({
63
- responses: [`{ "test": true, "nested": { "somethingelse": "somevalue" } }`],
64
- }).withStructuredOutput({
65
- includeRaw: false,
66
- schema: z.object({
67
- test: z.boolean(),
68
- nested: z.object({
69
- somethingelse: z.string(),
70
- }),
71
- }),
72
- });
73
- const response = await model.invoke("Hello there!");
74
- // @ts-expect-error not in run output type
75
- console.log(response.notthere);
76
- console.log(response.nested.somethingelse);
77
- expect(response).toEqual({
78
- test: true,
79
- nested: { somethingelse: "somevalue" },
80
- });
81
- });
82
- // test("Test ChatModel legacy params includeRaw withStructuredOutput", async () => {
83
- // const model = new FakeListChatModel({
84
- // responses: [`{ "test": true, "nested": { "somethingelse": "somevalue" } }`],
85
- // }).withStructuredOutput({
86
- // includeRaw: true,
87
- // schema: z.object({
88
- // test: z.boolean(),
89
- // nested: z.object({
90
- // somethingelse: z.string(),
91
- // }),
92
- // }),
93
- // });
94
- // const response = await model.invoke("Hello there!");
95
- // // @ts-expect-error legacy
96
- // console.log(response.nested);
97
- // console.log(response.parsed.nested);
98
- // });
99
- test("Test ChatModel withStructuredOutput with supplied type arg", async () => {
100
- const model = new FakeListChatModel({
101
- responses: [`{ "test": true, "nested": { "somethingelse": "somevalue" } }`],
102
- }).withStructuredOutput({
103
- includeRaw: false,
104
- schema: z.object({
105
- test: z.boolean(),
106
- nested: z.object({
107
- somethingelse: z.string(),
108
- }),
109
- }),
110
- });
111
- const response = await model.invoke("Hello there!");
112
- // @ts-expect-error run output type forced to something else
113
- console.log(response.nested.somethingelse);
114
- // No error here
115
- console.log(response.forcedArg);
116
- expect(response).toEqual({
117
- test: true,
118
- nested: { somethingelse: "somevalue" },
119
- });
120
- });
121
- test("Test ChatModel withStructuredOutput new syntax", async () => {
122
- const model = new FakeListChatModel({
123
- responses: [`{ "test": true, "nested": { "somethingelse": "somevalue" } }`],
124
- }).withStructuredOutput(z.object({
125
- test: z.boolean(),
126
- nested: z.object({
127
- somethingelse: z.string(),
128
- }),
129
- }));
130
- const response = await model.invoke("Hello there!");
131
- // @ts-expect-error run output type forced to something else
132
- console.log(response.nested.somethingelse);
133
- // No error here
134
- console.log(response.forcedArg);
135
- expect(response).toEqual({
136
- test: true,
137
- nested: { somethingelse: "somevalue" },
138
- });
139
- });
140
- test("Test ChatModel withStructuredOutput new syntax and JSON schema", async () => {
141
- const model = new FakeListChatModel({
142
- responses: [`{ "test": true, "nested": { "somethingelse": "somevalue" } }`],
143
- }).withStructuredOutput(zodToJsonSchema(z.object({
144
- test: z.boolean(),
145
- nested: z.object({
146
- somethingelse: z.string(),
147
- }),
148
- })));
149
- const response = await model.invoke("Hello there!");
150
- // No error here
151
- console.log(response.nested.somethingelse);
152
- // Also no error here
153
- console.log(response.forcedArg);
154
- expect(response).toEqual({
155
- test: true,
156
- nested: { somethingelse: "somevalue" },
157
- });
158
- });
159
- test("Test ChatModel withStructuredOutput new syntax and includeRaw", async () => {
160
- const model = new FakeListChatModel({
161
- responses: [`{ "test": true, "nested": { "somethingelse": "somevalue" } }`],
162
- }).withStructuredOutput(z.object({
163
- test: z.boolean(),
164
- nested: z.object({
165
- somethingelse: z.string(),
166
- }),
167
- }), { includeRaw: true });
168
- const response = await model.invoke("Hello there!");
169
- // @ts-expect-error run output includes raw
170
- console.log(response.nested.somethingelse);
171
- // No error
172
- console.log(response.parsed);
173
- });
174
- test("Test ChatModel can cache complex messages", async () => {
175
- const model = new FakeChatModel({
176
- cache: true,
177
- });
178
- if (!model.cache) {
179
- throw new Error("Cache not enabled");
180
- }
181
- const contentToCache = [
182
- {
183
- type: "text",
184
- text: "Hello there!",
185
- },
186
- ];
187
- const humanMessage = new HumanMessage({
188
- content: contentToCache,
189
- });
190
- const prompt = getBufferString([humanMessage]);
191
- const llmKey = model._getSerializedCacheKeyParametersForCall({});
192
- // Invoke model to trigger cache update
193
- await model.invoke([humanMessage]);
194
- const value = await model.cache.lookup(prompt, llmKey);
195
- expect(value).toBeDefined();
196
- if (!value)
197
- return;
198
- expect(value[0].text).toEqual(JSON.stringify(contentToCache, null, 2));
199
- expect("message" in value[0]).toBeTruthy();
200
- if (!("message" in value[0]))
201
- return;
202
- const cachedMsg = value[0].message;
203
- expect(cachedMsg.content).toEqual(JSON.stringify(contentToCache, null, 2));
204
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,19 +0,0 @@
1
- import { test, expect } from "@jest/globals";
2
- import { calculateMaxTokens, getModelContextSize } from "../base.js";
3
- test("properly calculates correct max tokens", async () => {
4
- expect(await calculateMaxTokens({ prompt: "", modelName: "gpt-3.5-turbo-16k" })).toBe(16384);
5
- expect(await calculateMaxTokens({
6
- prompt: "",
7
- modelName: "gpt-3.5-turbo-16k-0613",
8
- })).toBe(16384);
9
- expect(await calculateMaxTokens({ prompt: "", modelName: "gpt-3.5-turbo" })).toBe(4096);
10
- expect(await calculateMaxTokens({ prompt: "", modelName: "gpt-4" })).toBe(8192);
11
- expect(await calculateMaxTokens({ prompt: "", modelName: "gpt-4-32k" })).toBe(32768);
12
- });
13
- test("properly gets model context size", async () => {
14
- expect(await getModelContextSize("gpt-3.5-turbo-16k")).toBe(16384);
15
- expect(await getModelContextSize("gpt-3.5-turbo-16k-0613")).toBe(16384);
16
- expect(await getModelContextSize("gpt-3.5-turbo")).toBe(4096);
17
- expect(await getModelContextSize("gpt-4")).toBe(8192);
18
- expect(await getModelContextSize("gpt-4-32k")).toBe(32768);
19
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,52 +0,0 @@
1
- /* eslint-disable no-promise-executor-return */
2
- import { test } from "@jest/globals";
3
- import { FakeLLM, FakeStreamingLLM } from "../../utils/testing/index.js";
4
- import { HumanMessagePromptTemplate } from "../../prompts/chat.js";
5
- test("Test FakeLLM uses callbacks", async () => {
6
- const model = new FakeLLM({});
7
- let acc = "";
8
- const response = await model.invoke("Hello there!", {
9
- callbacks: [
10
- {
11
- handleLLMNewToken: (token) => {
12
- console.log(token);
13
- acc += token;
14
- },
15
- },
16
- ],
17
- });
18
- expect(response).toEqual(acc);
19
- });
20
- test("Test FakeLLM uses callbacks with a cache", async () => {
21
- const model = new FakeLLM({
22
- cache: true,
23
- });
24
- let acc = "";
25
- const response = await model.invoke("Hello there!");
26
- const response2 = await model.invoke("Hello there!", {
27
- callbacks: [
28
- {
29
- handleLLMNewToken: (token) => {
30
- console.log(token);
31
- acc += token;
32
- },
33
- },
34
- ],
35
- });
36
- // If callbacks are backgrounded
37
- await new Promise((resolve) => setTimeout(resolve, 1000));
38
- expect(response).toEqual(response2);
39
- expect(response2).toEqual(acc);
40
- });
41
- test("Test FakeStreamingLLM works when streaming through a prompt", async () => {
42
- const prompt = HumanMessagePromptTemplate.fromTemplate("hello there {name}");
43
- const model = new FakeStreamingLLM({});
44
- const chain = prompt.pipe(model);
45
- const stream = await chain.stream({ name: "test" });
46
- const chunks = [];
47
- for await (const chunk of stream) {
48
- chunks.push(chunk);
49
- }
50
- expect(chunks.length).toBeGreaterThan(1);
51
- expect(chunks.join("")).toEqual("Human: hello there test");
52
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,245 +0,0 @@
1
- import { test, describe, it, expect } from "@jest/globals";
2
- import { ChatPromptTemplate } from "../../prompts/chat.js";
3
- import { HumanMessage, AIMessage, ToolMessage, ToolMessageChunk, AIMessageChunk, } from "../index.js";
4
- import { load } from "../../load/index.js";
5
- test("Test ChatPromptTemplate can format OpenAI content image messages", async () => {
6
- const message = new HumanMessage({
7
- content: [
8
- {
9
- type: "image_url",
10
- image_url: {
11
- url: `data:image/jpeg;base64,{image_string}`,
12
- },
13
- },
14
- ],
15
- });
16
- const prompt = ChatPromptTemplate.fromMessages([
17
- message,
18
- ["ai", "Will this format with multiple messages?: {yes_or_no}"],
19
- ]);
20
- const formatted = await prompt.invoke({
21
- image_string: "base_64_encoded_string",
22
- yes_or_no: "YES!",
23
- });
24
- expect(formatted.messages[0].content[0]).toEqual({
25
- type: "image_url",
26
- image_url: {
27
- url: "data:image/jpeg;base64,base_64_encoded_string",
28
- },
29
- });
30
- expect(formatted.messages[1].content).toEqual("Will this format with multiple messages?: YES!");
31
- });
32
- test("Test ChatPromptTemplate can format OpenAI content image messages", async () => {
33
- const message = new HumanMessage({
34
- content: [
35
- {
36
- type: "image_url",
37
- image_url: {
38
- url: `data:image/jpeg;base64,{image_string}`,
39
- },
40
- },
41
- ],
42
- });
43
- const prompt = ChatPromptTemplate.fromMessages([
44
- message,
45
- ["ai", "Will this format with multiple messages?: {yes_or_no}"],
46
- ]);
47
- const formatted = await prompt.invoke({
48
- image_string: "base_64_encoded_string",
49
- yes_or_no: "YES!",
50
- });
51
- expect(formatted.messages[0].content[0]).toEqual({
52
- type: "image_url",
53
- image_url: {
54
- url: "data:image/jpeg;base64,base_64_encoded_string",
55
- },
56
- });
57
- expect(formatted.messages[1].content).toEqual("Will this format with multiple messages?: YES!");
58
- });
59
- test("Deserialisation and serialisation of additional_kwargs and tool_call_id", async () => {
60
- const config = {
61
- importMap: { messages: { AIMessage } },
62
- optionalImportEntrypoints: [],
63
- optionalImportsMap: {},
64
- secretsMap: {},
65
- };
66
- const message = new AIMessage({
67
- content: "",
68
- additional_kwargs: {
69
- tool_calls: [
70
- {
71
- id: "call_tXJNP1S6LHT5tLfaNHCbYCtH",
72
- type: "function",
73
- function: {
74
- name: "Weather",
75
- arguments: '{\n "location": "Prague"\n}',
76
- },
77
- },
78
- ],
79
- },
80
- });
81
- const deserialized = await load(JSON.stringify(message), config);
82
- expect(deserialized).toEqual(message);
83
- });
84
- test("Deserialisation and serialisation of tool_call_id", async () => {
85
- const config = {
86
- importMap: { messages: { ToolMessage } },
87
- optionalImportEntrypoints: [],
88
- optionalImportsMap: {},
89
- secretsMap: {},
90
- };
91
- const message = new ToolMessage({
92
- content: '{"value": 32}',
93
- tool_call_id: "call_tXJNP1S6LHT5tLfaNHCbYCtH",
94
- });
95
- const deserialized = await load(JSON.stringify(message), config);
96
- expect(deserialized).toEqual(message);
97
- });
98
- test("Deserialisation and serialisation of messages with ID", async () => {
99
- const config = {
100
- importMap: { messages: { AIMessage } },
101
- optionalImportEntrypoints: [],
102
- optionalImportsMap: {},
103
- secretsMap: {},
104
- };
105
- const messageId = "uuid-1234";
106
- const message = new AIMessage({
107
- content: "The sky is blue because...",
108
- id: messageId,
109
- });
110
- const deserialized = await load(JSON.stringify(message), config);
111
- expect(deserialized).toEqual(message);
112
- expect(deserialized.id).toBe(messageId);
113
- });
114
- test("Can concat artifact (string) of ToolMessageChunk", () => {
115
- const rawOutputOne = "Hello";
116
- const rawOutputTwo = " world";
117
- const chunk1 = new ToolMessageChunk({
118
- content: "Hello",
119
- tool_call_id: "1",
120
- artifact: rawOutputOne,
121
- });
122
- const chunk2 = new ToolMessageChunk({
123
- content: " world",
124
- tool_call_id: "1",
125
- artifact: rawOutputTwo,
126
- });
127
- const concated = chunk1.concat(chunk2);
128
- expect(concated.artifact).toBe(`${rawOutputOne}${rawOutputTwo}`);
129
- });
130
- test("Can concat artifact (array) of ToolMessageChunk", () => {
131
- const rawOutputOne = ["Hello", " world"];
132
- const rawOutputTwo = ["!!"];
133
- const chunk1 = new ToolMessageChunk({
134
- content: "Hello",
135
- tool_call_id: "1",
136
- artifact: rawOutputOne,
137
- });
138
- const chunk2 = new ToolMessageChunk({
139
- content: " world",
140
- tool_call_id: "1",
141
- artifact: rawOutputTwo,
142
- });
143
- const concated = chunk1.concat(chunk2);
144
- expect(concated.artifact).toEqual(["Hello", " world", "!!"]);
145
- });
146
- test("Can concat artifact (object) of ToolMessageChunk", () => {
147
- const rawOutputOne = {
148
- foo: "bar",
149
- };
150
- const rawOutputTwo = {
151
- bar: "baz",
152
- };
153
- const chunk1 = new ToolMessageChunk({
154
- content: "Hello",
155
- tool_call_id: "1",
156
- artifact: rawOutputOne,
157
- });
158
- const chunk2 = new ToolMessageChunk({
159
- content: " world",
160
- tool_call_id: "1",
161
- artifact: rawOutputTwo,
162
- });
163
- const concated = chunk1.concat(chunk2);
164
- expect(concated.artifact).toEqual({
165
- foo: "bar",
166
- bar: "baz",
167
- });
168
- });
169
- describe("Complex AIMessageChunk concat", () => {
170
- it("concatenates content arrays of strings", () => {
171
- expect(new AIMessageChunk({
172
- content: [{ type: "text", text: "I am" }],
173
- id: "ai4",
174
- }).concat(new AIMessageChunk({ content: [{ type: "text", text: " indeed." }] }))).toEqual(new AIMessageChunk({
175
- id: "ai4",
176
- content: [
177
- { type: "text", text: "I am" },
178
- { type: "text", text: " indeed." },
179
- ],
180
- }));
181
- });
182
- it("concatenates mixed content arrays", () => {
183
- expect(new AIMessageChunk({
184
- content: [{ index: 0, type: "text", text: "I am" }],
185
- }).concat(new AIMessageChunk({ content: [{ type: "text", text: " indeed." }] }))).toEqual(new AIMessageChunk({
186
- content: [
187
- { index: 0, type: "text", text: "I am" },
188
- { type: "text", text: " indeed." },
189
- ],
190
- }));
191
- });
192
- it("merges content arrays with same index", () => {
193
- expect(new AIMessageChunk({ content: [{ index: 0, text: "I am" }] }).concat(new AIMessageChunk({ content: [{ index: 0, text: " indeed." }] }))).toEqual(new AIMessageChunk({ content: [{ index: 0, text: "I am indeed." }] }));
194
- });
195
- it("does not merge when one chunk is missing an index", () => {
196
- expect(new AIMessageChunk({ content: [{ index: 0, text: "I am" }] }).concat(new AIMessageChunk({ content: [{ text: " indeed." }] }))).toEqual(new AIMessageChunk({
197
- content: [{ index: 0, text: "I am" }, { text: " indeed." }],
198
- }));
199
- });
200
- it("does not create a holey array when there's a gap between indexes", () => {
201
- expect(new AIMessageChunk({ content: [{ index: 0, text: "I am" }] }).concat(new AIMessageChunk({ content: [{ index: 2, text: " indeed." }] }))).toEqual(new AIMessageChunk({
202
- content: [
203
- { index: 0, text: "I am" },
204
- { index: 2, text: " indeed." },
205
- ],
206
- }));
207
- });
208
- it("does not merge content arrays with separate indexes", () => {
209
- expect(new AIMessageChunk({ content: [{ index: 0, text: "I am" }] }).concat(new AIMessageChunk({ content: [{ index: 1, text: " indeed." }] }))).toEqual(new AIMessageChunk({
210
- content: [
211
- { index: 0, text: "I am" },
212
- { index: 1, text: " indeed." },
213
- ],
214
- }));
215
- });
216
- it("merges content arrays with same index and type", () => {
217
- expect(new AIMessageChunk({
218
- content: [{ index: 0, text: "I am", type: "text_block" }],
219
- }).concat(new AIMessageChunk({
220
- content: [{ index: 0, text: " indeed.", type: "text_block" }],
221
- }))).toEqual(new AIMessageChunk({
222
- content: [{ index: 0, text: "I am indeed.", type: "text_block" }],
223
- }));
224
- });
225
- it("merges content arrays with same index and different types without updating type", () => {
226
- expect(new AIMessageChunk({
227
- content: [{ index: 0, text: "I am", type: "text_block" }],
228
- }).concat(new AIMessageChunk({
229
- content: [{ index: 0, text: " indeed.", type: "text_block_delta" }],
230
- }))).toEqual(new AIMessageChunk({
231
- content: [{ index: 0, text: "I am indeed.", type: "text_block" }],
232
- }));
233
- });
234
- it("concatenates empty string content and merges other fields", () => {
235
- expect(new AIMessageChunk({
236
- content: [{ index: 0, type: "text", text: "I am" }],
237
- }).concat(new AIMessageChunk({
238
- content: [{ type: "text", text: "" }],
239
- response_metadata: { extra: "value" },
240
- }))).toEqual(new AIMessageChunk({
241
- content: [{ index: 0, type: "text", text: "I am" }],
242
- response_metadata: { extra: "value" },
243
- }));
244
- });
245
- });
@@ -1 +0,0 @@
1
- export {};