diff --git a/js/package.json b/js/package.json index ff122d886..eec53685e 100644 --- a/js/package.json +++ b/js/package.json @@ -1,6 +1,6 @@ { "name": "langsmith", - "version": "0.1.56", + "version": "0.1.57", "description": "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform.", "packageManager": "yarn@1.22.19", "files": [ diff --git a/js/src/index.ts b/js/src/index.ts index da838f6bc..42fa4669f 100644 --- a/js/src/index.ts +++ b/js/src/index.ts @@ -14,4 +14,4 @@ export { RunTree, type RunTreeConfig } from "./run_trees.js"; export { overrideFetchImplementation } from "./singletons/fetch.js"; // Update using yarn bump-version -export const __version__ = "0.1.56"; +export const __version__ = "0.1.57"; diff --git a/js/src/run_trees.ts b/js/src/run_trees.ts index a2dad8bcb..64cc7fb2b 100644 --- a/js/src/run_trees.ts +++ b/js/src/run_trees.ts @@ -146,6 +146,8 @@ class Baggage { } export class RunTree implements BaseRun { + private static sharedClient: Client | null = null; + id: string; name: RunTreeConfig["name"]; run_type: string; @@ -173,7 +175,7 @@ export class RunTree implements BaseRun { constructor(originalConfig: RunTreeConfig) { const defaultConfig = RunTree.getDefaultConfig(); const { metadata, ...config } = originalConfig; - const client = config.client ?? new Client(); + const client = config.client ?? RunTree.getSharedClient(); const dedupedMetadata = { ...metadata, ...config?.extra?.metadata, @@ -226,6 +228,13 @@ export class RunTree implements BaseRun { }; } + private static getSharedClient(): Client { + if (!RunTree.sharedClient) { + RunTree.sharedClient = new Client(); + } + return RunTree.sharedClient; + } + public createChild(config: RunTreeConfig): RunTree { const child_execution_order = this.child_execution_order + 1; diff --git a/js/src/tests/run_trees.int.test.ts b/js/src/tests/run_trees.int.test.ts index 79efba3fa..ecd40976f 100644 --- a/js/src/tests/run_trees.int.test.ts +++ b/js/src/tests/run_trees.int.test.ts @@ -1,4 +1,5 @@ import { Client } from "../client.js"; +import * as uuid from "uuid"; import { RunTree, RunTreeConfig, @@ -14,14 +15,8 @@ import { test.concurrent( "Test post and patch run", async () => { - const projectName = `__test_run_tree`; + const projectName = `__test_run_tree_js ${uuid.v4()}`; const langchainClient = new Client({ timeout_ms: 30000 }); - try { - await langchainClient.readProject({ projectName }); - await langchainClient.deleteProject({ projectName }); - } catch (e) { - // Pass - } const parentRunConfig: RunTreeConfig = { name: "parent_run", run_type: "chain", @@ -113,6 +108,20 @@ test.concurrent( runMap.get("parent_run")?.id ); expect(runMap.get("parent_run")?.parent_run_id).toBeNull(); + await waitUntil( + async () => { + try { + const runs_ = await toArray( + langchainClient.listRuns({ traceId: runs[0].trace_id }) + ); + return runs_.length === 5; + } catch (e) { + return false; + } + }, + 30_000, // Wait up to 30 seconds + 3000 // every 3 second + ); const traceRunsIter = langchainClient.listRuns({ traceId: runs[0].trace_id, diff --git a/js/src/tests/run_trees.test.ts b/js/src/tests/run_trees.test.ts index 70e2c79d9..253c32f45 100644 --- a/js/src/tests/run_trees.test.ts +++ b/js/src/tests/run_trees.test.ts @@ -112,3 +112,12 @@ test("distributed", () => { "20210503T000000000001Z00000000-0000-0000-0000-00000000000.20210503T000001000002Z00000000-0000-0000-0000-00000000001", }); }); + +test("shared client between run trees", () => { + const runTree1 = new RunTree({ name: "tree_1" }); + const runTree2 = new RunTree({ name: "tree_2" }); + + expect(runTree1.client).toBeDefined(); + expect(runTree2.client).toBeDefined(); + expect(runTree1.client).toBe(runTree2.client); +});