@langchain/core 0.1.17 → 0.1.19

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.
@@ -18,8 +18,12 @@ class BaseListChatMessageHistory extends serializable_js_1.Serializable {
18
18
  addUserMessage(message) {
19
19
  return this.addMessage(new index_js_1.HumanMessage(message));
20
20
  }
21
+ /** @deprecated Use addAIMessage instead */
21
22
  addAIChatMessage(message) {
22
23
  return this.addMessage(new index_js_1.AIMessage(message));
23
24
  }
25
+ addAIMessage(message) {
26
+ return this.addMessage(new index_js_1.AIMessage(message));
27
+ }
24
28
  }
25
29
  exports.BaseListChatMessageHistory = BaseListChatMessageHistory;
@@ -18,6 +18,8 @@ export declare abstract class BaseChatMessageHistory extends Serializable {
18
18
  export declare abstract class BaseListChatMessageHistory extends Serializable {
19
19
  abstract addMessage(message: BaseMessage): Promise<void>;
20
20
  addUserMessage(message: string): Promise<void>;
21
+ /** @deprecated Use addAIMessage instead */
21
22
  addAIChatMessage(message: string): Promise<void>;
23
+ addAIMessage(message: string): Promise<void>;
22
24
  abstract getMessages(): Promise<BaseMessage[]>;
23
25
  }
@@ -14,7 +14,11 @@ export class BaseListChatMessageHistory extends Serializable {
14
14
  addUserMessage(message) {
15
15
  return this.addMessage(new HumanMessage(message));
16
16
  }
17
+ /** @deprecated Use addAIMessage instead */
17
18
  addAIChatMessage(message) {
18
19
  return this.addMessage(new AIMessage(message));
19
20
  }
21
+ addAIMessage(message) {
22
+ return this.addMessage(new AIMessage(message));
23
+ }
20
24
  }
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.RunnablePick = exports.RunnableAssign = exports._coerceToRunnable = exports.RunnableWithFallbacks = exports.RunnableParallel = exports.RunnableLambda = exports.RunnableMap = exports.RunnableSequence = exports.RunnableRetry = exports.RunnableEach = exports.RunnableBinding = exports.Runnable = void 0;
6
+ exports.RunnablePick = exports.RunnableAssign = exports._coerceToRunnable = exports.RunnableWithFallbacks = exports.RunnableParallel = exports.RunnableLambda = exports.RunnableMap = exports.RunnableSequence = exports.RunnableRetry = exports.RunnableEach = exports.RunnableBinding = exports.Runnable = exports._coerceToDict = void 0;
7
7
  const p_retry_1 = __importDefault(require("p-retry"));
8
8
  const manager_js_1 = require("../callbacks/manager.cjs");
9
9
  const log_stream_js_1 = require("../tracers/log_stream.cjs");
@@ -22,6 +22,7 @@ function _coerceToDict(value, defaultKey) {
22
22
  ? value
23
23
  : { [defaultKey]: value };
24
24
  }
25
+ exports._coerceToDict = _coerceToDict;
25
26
  /**
26
27
  * A Runnable is a generic unit of work that can be invoked, batched, streamed, and/or
27
28
  * transformed.
@@ -38,6 +38,7 @@ export type RunnableBatchOptions = {
38
38
  returnExceptions?: boolean;
39
39
  };
40
40
  export type RunnableRetryFailedAttemptHandler = (error: any) => any;
41
+ export declare function _coerceToDict(value: any, defaultKey: string): any;
41
42
  /**
42
43
  * A Runnable is a generic unit of work that can be invoked, batched, streamed, and/or
43
44
  * transformed.
@@ -7,7 +7,7 @@ import { DEFAULT_RECURSION_LIMIT, ensureConfig, getCallbackManagerForConfig, mer
7
7
  import { AsyncCaller } from "../utils/async_caller.js";
8
8
  import { RootListenersTracer } from "../tracers/root_listener.js";
9
9
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
- function _coerceToDict(value, defaultKey) {
10
+ export function _coerceToDict(value, defaultKey) {
11
11
  return value &&
12
12
  !Array.isArray(value) &&
13
13
  // eslint-disable-next-line no-instanceof/no-instanceof
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RunnableBranch = void 0;
4
4
  const base_js_1 = require("./base.cjs");
5
5
  const config_js_1 = require("./config.cjs");
6
+ const stream_js_1 = require("../utils/stream.cjs");
6
7
  /**
7
8
  * Class that represents a runnable branch. The RunnableBranch is
8
9
  * initialized with an array of branches and a default branch. When invoked,
@@ -129,7 +130,7 @@ class RunnableBranch extends base_js_1.Runnable {
129
130
  }
130
131
  if (!result) {
131
132
  result = await this.default.invoke(input, (0, config_js_1.patchConfig)(config, {
132
- callbacks: runManager?.getChild("default"),
133
+ callbacks: runManager?.getChild("branch:default"),
133
134
  }));
134
135
  }
135
136
  return result;
@@ -137,5 +138,70 @@ class RunnableBranch extends base_js_1.Runnable {
137
138
  async invoke(input, config = {}) {
138
139
  return this._callWithConfig(this._invoke, input, config);
139
140
  }
141
+ async *_streamIterator(input, config) {
142
+ const callbackManager_ = await (0, config_js_1.getCallbackManagerForConfig)(config);
143
+ const runManager = await callbackManager_?.handleChainStart(this.toJSON(), (0, base_js_1._coerceToDict)(input, "input"), undefined, undefined, undefined, undefined, config?.runName);
144
+ let finalOutput;
145
+ let finalOutputSupported = true;
146
+ let stream;
147
+ try {
148
+ for (let i = 0; i < this.branches.length; i += 1) {
149
+ const [condition, branchRunnable] = this.branches[i];
150
+ const conditionValue = await condition.invoke(input, (0, config_js_1.patchConfig)(config, {
151
+ callbacks: runManager?.getChild(`condition:${i + 1}`),
152
+ }));
153
+ if (conditionValue) {
154
+ stream = await branchRunnable.stream(input, (0, config_js_1.patchConfig)(config, {
155
+ callbacks: runManager?.getChild(`branch:${i + 1}`),
156
+ }));
157
+ for await (const chunk of stream) {
158
+ yield chunk;
159
+ if (finalOutputSupported) {
160
+ if (finalOutput === undefined) {
161
+ finalOutput = chunk;
162
+ }
163
+ else {
164
+ try {
165
+ finalOutput = (0, stream_js_1.concat)(finalOutput, chunk);
166
+ }
167
+ catch (e) {
168
+ finalOutput = undefined;
169
+ finalOutputSupported = false;
170
+ }
171
+ }
172
+ }
173
+ }
174
+ break;
175
+ }
176
+ }
177
+ if (stream === undefined) {
178
+ stream = await this.default.stream(input, (0, config_js_1.patchConfig)(config, {
179
+ callbacks: runManager?.getChild("branch:default"),
180
+ }));
181
+ for await (const chunk of stream) {
182
+ yield chunk;
183
+ if (finalOutputSupported) {
184
+ if (finalOutput === undefined) {
185
+ finalOutput = chunk;
186
+ }
187
+ else {
188
+ try {
189
+ finalOutput = (0, stream_js_1.concat)(finalOutput, chunk);
190
+ }
191
+ catch (e) {
192
+ finalOutput = undefined;
193
+ finalOutputSupported = false;
194
+ }
195
+ }
196
+ }
197
+ }
198
+ }
199
+ }
200
+ catch (e) {
201
+ await runManager?.handleChainError(e);
202
+ throw e;
203
+ }
204
+ await runManager?.handleChainEnd(finalOutput ?? {});
205
+ }
140
206
  }
141
207
  exports.RunnableBranch = RunnableBranch;
@@ -91,4 +91,5 @@ export declare class RunnableBranch<RunInput = any, RunOutput = any> extends Run
91
91
  ]): RunnableBranch<RunInput, RunOutput>;
92
92
  _invoke(input: RunInput, config?: Partial<RunnableConfig>, runManager?: CallbackManagerForChainRun): Promise<RunOutput>;
93
93
  invoke(input: RunInput, config?: RunnableConfig): Promise<RunOutput>;
94
+ _streamIterator(input: RunInput, config?: Partial<RunnableConfig>): AsyncGenerator<Awaited<RunOutput>, void, unknown>;
94
95
  }
@@ -1,5 +1,6 @@
1
- import { Runnable, _coerceToRunnable } from "./base.js";
2
- import { patchConfig } from "./config.js";
1
+ import { Runnable, _coerceToDict, _coerceToRunnable, } from "./base.js";
2
+ import { getCallbackManagerForConfig, patchConfig, } from "./config.js";
3
+ import { concat } from "../utils/stream.js";
3
4
  /**
4
5
  * Class that represents a runnable branch. The RunnableBranch is
5
6
  * initialized with an array of branches and a default branch. When invoked,
@@ -126,7 +127,7 @@ export class RunnableBranch extends Runnable {
126
127
  }
127
128
  if (!result) {
128
129
  result = await this.default.invoke(input, patchConfig(config, {
129
- callbacks: runManager?.getChild("default"),
130
+ callbacks: runManager?.getChild("branch:default"),
130
131
  }));
131
132
  }
132
133
  return result;
@@ -134,4 +135,69 @@ export class RunnableBranch extends Runnable {
134
135
  async invoke(input, config = {}) {
135
136
  return this._callWithConfig(this._invoke, input, config);
136
137
  }
138
+ async *_streamIterator(input, config) {
139
+ const callbackManager_ = await getCallbackManagerForConfig(config);
140
+ const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, undefined, undefined, undefined, config?.runName);
141
+ let finalOutput;
142
+ let finalOutputSupported = true;
143
+ let stream;
144
+ try {
145
+ for (let i = 0; i < this.branches.length; i += 1) {
146
+ const [condition, branchRunnable] = this.branches[i];
147
+ const conditionValue = await condition.invoke(input, patchConfig(config, {
148
+ callbacks: runManager?.getChild(`condition:${i + 1}`),
149
+ }));
150
+ if (conditionValue) {
151
+ stream = await branchRunnable.stream(input, patchConfig(config, {
152
+ callbacks: runManager?.getChild(`branch:${i + 1}`),
153
+ }));
154
+ for await (const chunk of stream) {
155
+ yield chunk;
156
+ if (finalOutputSupported) {
157
+ if (finalOutput === undefined) {
158
+ finalOutput = chunk;
159
+ }
160
+ else {
161
+ try {
162
+ finalOutput = concat(finalOutput, chunk);
163
+ }
164
+ catch (e) {
165
+ finalOutput = undefined;
166
+ finalOutputSupported = false;
167
+ }
168
+ }
169
+ }
170
+ }
171
+ break;
172
+ }
173
+ }
174
+ if (stream === undefined) {
175
+ stream = await this.default.stream(input, patchConfig(config, {
176
+ callbacks: runManager?.getChild("branch:default"),
177
+ }));
178
+ for await (const chunk of stream) {
179
+ yield chunk;
180
+ if (finalOutputSupported) {
181
+ if (finalOutput === undefined) {
182
+ finalOutput = chunk;
183
+ }
184
+ else {
185
+ try {
186
+ finalOutput = concat(finalOutput, chunk);
187
+ }
188
+ catch (e) {
189
+ finalOutput = undefined;
190
+ finalOutputSupported = false;
191
+ }
192
+ }
193
+ }
194
+ }
195
+ }
196
+ }
197
+ catch (e) {
198
+ await runManager?.handleChainError(e);
199
+ throw e;
200
+ }
201
+ await runManager?.handleChainEnd(finalOutput ?? {});
202
+ }
137
203
  }
@@ -225,7 +225,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
225
225
  throw new Error("No chain run to end.");
226
226
  }
227
227
  run.end_time = Date.now();
228
- run.error = error.message + (error.stack ? `\n\n${error.stack}` : "");
228
+ run.error = error.message + (error?.stack ? `\n\n${error.stack}` : "");
229
229
  run.events.push({
230
230
  name: "error",
231
231
  time: new Date(run.end_time).toISOString(),
@@ -222,7 +222,7 @@ export class BaseTracer extends BaseCallbackHandler {
222
222
  throw new Error("No chain run to end.");
223
223
  }
224
224
  run.end_time = Date.now();
225
- run.error = error.message + (error.stack ? `\n\n${error.stack}` : "");
225
+ run.error = error.message + (error?.stack ? `\n\n${error.stack}` : "");
226
226
  run.events.push({
227
227
  name: "error",
228
228
  time: new Date(run.end_time).toISOString(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.1.17",
3
+ "version": "0.1.19",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {
@@ -15,21 +15,24 @@
15
15
  "scripts": {
16
16
  "build": "yarn clean && yarn build:esm && yarn build:cjs && yarn run build:scripts",
17
17
  "build:esm": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist/ && rimraf dist/tests dist/**/tests",
18
- "build:cjs": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist-cjs/ -p tsconfig.cjs.json && node scripts/move-cjs-to-dist.js && rimraf dist-cjs",
19
- "build:watch": "node scripts/create-entrypoints.js && tsc --outDir dist/ --watch",
20
- "build:scripts": "node scripts/create-entrypoints.js && node scripts/check-tree-shaking.js",
18
+ "build:cjs": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist-cjs/ -p tsconfig.cjs.json && yarn move-cjs-to-dist && rimraf dist-cjs",
19
+ "build:watch": "yarn create-entrypoints && tsc --outDir dist/ --watch",
20
+ "build:scripts": "yarn create-entrypoints && yarn check-tree-shaking",
21
21
  "lint:eslint": "NODE_OPTIONS=--max-old-space-size=4096 eslint --cache --ext .ts,.js src/",
22
22
  "lint:dpdm": "dpdm --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts",
23
23
  "lint": "yarn lint:eslint && yarn lint:dpdm",
24
24
  "lint:fix": "yarn lint:eslint --fix && yarn lint:dpdm",
25
- "clean": "rimraf .turbo/ dist/ && NODE_OPTIONS=--max-old-space-size=4096 node scripts/create-entrypoints.js pre",
25
+ "clean": "rimraf .turbo/ dist/ && NODE_OPTIONS=--max-old-space-size=4096 yarn create-entrypoints -- --pre",
26
26
  "prepack": "yarn build",
27
27
  "release": "release-it --only-version --config .release-it.json",
28
28
  "test": "NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000 --maxWorkers=50%",
29
29
  "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathIgnorePatterns=\\.int\\.test.ts",
30
30
  "test:single": "NODE_OPTIONS=--experimental-vm-modules yarn run jest --config jest.config.cjs --testTimeout 100000",
31
- "format": "prettier --write \"src\" \"scripts\"",
32
- "format:check": "prettier --check \"src\" \"scripts\""
31
+ "format": "prettier --config .prettierrc --write \"src\"",
32
+ "format:check": "prettier --config .prettierrc --check \"src\"",
33
+ "move-cjs-to-dist": "yarn lc-build --config ./langchain.config.js --move-cjs-dist",
34
+ "create-entrypoints": "yarn lc-build --config ./langchain.config.js --create-entrypoints",
35
+ "check-tree-shaking": "yarn lc-build --config ./langchain.config.js --tree-shaking"
33
36
  },
34
37
  "author": "LangChain",
35
38
  "license": "MIT",
@@ -48,6 +51,7 @@
48
51
  },
49
52
  "devDependencies": {
50
53
  "@jest/globals": "^29.5.0",
54
+ "@langchain/scripts": "^0.0.2",
51
55
  "@swc/core": "^1.3.90",
52
56
  "@swc/jest": "^0.2.29",
53
57
  "dpdm": "^3.12.0",
@@ -440,9 +444,6 @@
440
444
  "utils/types.d.ts",
441
445
  "vectorstores.cjs",
442
446
  "vectorstores.js",
443
- "vectorstores.d.ts",
444
- "index.cjs",
445
- "index.js",
446
- "index.d.ts"
447
+ "vectorstores.d.ts"
447
448
  ]
448
449
  }
package/index.cjs DELETED
@@ -1 +0,0 @@
1
- module.exports = require('./dist/index.cjs');
package/index.d.ts DELETED
@@ -1 +0,0 @@
1
- export * from './dist/index.js'
package/index.js DELETED
@@ -1 +0,0 @@
1
- export * from './dist/index.js'