Skip to content

Commit

Permalink
reorg bc circular dep
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Dec 5, 2024
1 parent 0560c29 commit d532c22
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 31 deletions.
2 changes: 1 addition & 1 deletion langchain-core/src/runnables/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
atee,
pipeGeneratorWithSetup,
AsyncGeneratorWithSetup,
pickRunnableConfigKeys,
} from "../utils/stream.js";
import { raceWithSignal } from "../utils/signal.js";
import {
Expand All @@ -56,6 +55,7 @@ import {
} from "./iter.js";
import { _isToolCall, ToolInputParsingException } from "../tools/utils.js";
import { ToolCall } from "../messages/tool.js";
import { pickRunnableConfigKeys } from "./types.js";

export { type RunnableInterface, RunnableBatchOptions };

Expand Down
3 changes: 1 addition & 2 deletions langchain-core/src/runnables/iter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { AsyncLocalStorageProviderSingleton } from "../singletons/index.js";
import { pickRunnableConfigKeys } from "../utils/stream.js";
import { RunnableConfig } from "./config.js";
import { pickRunnableConfigKeys, type RunnableConfig } from "./types.js";

export function isIterableIterator(
thing: unknown
Expand Down
20 changes: 19 additions & 1 deletion langchain-core/src/runnables/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { z } from "zod";
import type { IterableReadableStreamInterface } from "../utils/stream.js";
import type { SerializableInterface } from "../load/serializable.js";
import type { BaseCallbackConfig } from "../callbacks/manager.js";
import type { IterableReadableStreamInterface } from "../types/stream.js";

export type RunnableBatchOptions = {
/** @deprecated Pass in via the standard runnable config object instead */
Expand Down Expand Up @@ -108,3 +108,21 @@ export interface RunnableConfig<
*/
signal?: AbortSignal;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function pickRunnableConfigKeys<CallOptions extends Record<string, any>>(
config?: CallOptions
): RunnableConfig | undefined {
return config
? {
configurable: config.configurable,
recursionLimit: config.recursionLimit,
callbacks: config.callbacks,
tags: config.tags,
metadata: config.metadata,
maxConcurrency: config.maxConcurrency,
timeout: config.timeout,
signal: config.signal,
}
: undefined;
}
2 changes: 1 addition & 1 deletion langchain-core/src/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { MessageContent } from "../messages/base.js";
import { AsyncLocalStorageProviderSingleton } from "../singletons/index.js";
import { _isToolCall, ToolInputParsingException } from "./utils.js";
import { isZodSchema } from "../utils/types/is_zod_schema.js";
import { pickRunnableConfigKeys } from "../utils/stream.js";
import { pickRunnableConfigKeys } from "../runnables/types.js";

export { ToolInputParsingException };

Expand Down
5 changes: 5 additions & 0 deletions langchain-core/src/types/stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Make this a type to override ReadableStream's async iterator type in case
// the popular web-streams-polyfill is imported - the supplied types
// in that case don't quite match.
export type IterableReadableStreamInterface<T> = ReadableStream<T> &
AsyncIterable<T>;
37 changes: 11 additions & 26 deletions langchain-core/src/utils/stream.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { RunnableConfig } from "../../runnables.js";
import { pickRunnableConfigKeys } from "../runnables/types.js";
import { AsyncLocalStorageProviderSingleton } from "../singletons/index.js";
import { IterableReadableStreamInterface } from "../types/stream.js";
import { raceWithSignal } from "./signal.js";

// Make this a type to override ReadableStream's async iterator type in case
// the popular web-streams-polyfill is imported - the supplied types
// in that case don't quite match.
export type IterableReadableStreamInterface<T> = ReadableStream<T> &
AsyncIterable<T>;
// Re-exported for backwards compatibility
// Do NOT import this type from this file inside the project. Instead, always import from `types/stream.js`
export { IterableReadableStreamInterface };

/*
* Support async iterator syntax for ReadableStreams in all environments.
Expand Down Expand Up @@ -181,24 +180,6 @@ export function concat<
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function pickRunnableConfigKeys<CallOptions extends Record<string, any>>(
config?: CallOptions
): RunnableConfig | undefined {
return config
? {
configurable: config.configurable,
recursionLimit: config.recursionLimit,
callbacks: config.callbacks,
tags: config.tags,
metadata: config.metadata,
maxConcurrency: config.maxConcurrency,
timeout: config.timeout,
signal: config.signal,
}
: undefined;
}

export class AsyncGeneratorWithSetup<
S = unknown,
T = unknown,
Expand Down Expand Up @@ -234,7 +215,9 @@ export class AsyncGeneratorWithSetup<
// to each generator is available.
this.setup = new Promise((resolve, reject) => {
void AsyncLocalStorageProviderSingleton.runWithConfig(
pickRunnableConfigKeys(params.config as RunnableConfig),
pickRunnableConfigKeys(
params.config as Record<string, unknown> | undefined
),
async () => {
this.firstResult = params.generator.next();
if (params.startSetup) {
Expand All @@ -257,7 +240,9 @@ export class AsyncGeneratorWithSetup<
}

return AsyncLocalStorageProviderSingleton.runWithConfig(
pickRunnableConfigKeys(this.config as RunnableConfig),
pickRunnableConfigKeys(
this.config as Record<string, unknown> | undefined
),
this.signal
? async () => {
return raceWithSignal(this.generator.next(...args), this.signal);
Expand Down

0 comments on commit d532c22

Please sign in to comment.