-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for custom fetch implementation (#2993)
- Loading branch information
Showing
8 changed files
with
2,038 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** @type {import('jest').Config} */ | ||
export default { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
extensionsToTreatAsEsm: ['.ts'], | ||
moduleNameMapper: { | ||
'^(\\.{1,2}/.*)\\.js$': '$1', | ||
}, | ||
transform: { | ||
'^.+\\.tsx?$': [ | ||
'ts-jest', | ||
{ | ||
useESM: true, | ||
}, | ||
], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@langchain/langgraph-sdk", | ||
"version": "0.0.35", | ||
"version": "0.0.36", | ||
"description": "Client library for interacting with the LangGraph API", | ||
"type": "module", | ||
"packageManager": "[email protected]", | ||
|
@@ -9,7 +9,8 @@ | |
"build": "yarn clean && yarn lc_build --create-entrypoints --pre --tree-shaking", | ||
"prepublish": "yarn run build", | ||
"format": "prettier --write src", | ||
"lint": "prettier --check src && tsc --noEmit" | ||
"lint": "prettier --check src && tsc --noEmit", | ||
"test": "NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts" | ||
}, | ||
"main": "index.js", | ||
"license": "MIT", | ||
|
@@ -20,12 +21,16 @@ | |
"uuid": "^9.0.0" | ||
}, | ||
"devDependencies": { | ||
"@jest/globals": "^29.7.0", | ||
"@langchain/scripts": "^0.1.4", | ||
"@tsconfig/recommended": "^1.0.2", | ||
"@types/jest": "^29.5.12", | ||
"@types/node": "^20.12.12", | ||
"@types/uuid": "^9.0.1", | ||
"concat-md": "^0.5.1", | ||
"jest": "^29.7.0", | ||
"prettier": "^3.2.5", | ||
"ts-jest": "^29.1.2", | ||
"typedoc": "^0.26.1", | ||
"typedoc-plugin-markdown": "^4.1.0", | ||
"typescript": "^5.4.5" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Wrap the default fetch call due to issues with illegal invocations | ||
// in some environments: | ||
// https://stackoverflow.com/questions/69876859/why-does-bind-fix-failed-to-execute-fetch-on-window-illegal-invocation-err | ||
// @ts-expect-error Broad typing to support a range of fetch implementations | ||
const DEFAULT_FETCH_IMPLEMENTATION = (...args: any[]) => fetch(...args); | ||
|
||
const LANGSMITH_FETCH_IMPLEMENTATION_KEY = Symbol.for( | ||
"lg:fetch_implementation", | ||
); | ||
|
||
/** | ||
* Overrides the fetch implementation used for LangSmith calls. | ||
* You should use this if you need to use an implementation of fetch | ||
* other than the default global (e.g. for dealing with proxies). | ||
* @param fetch The new fetch function to use. | ||
*/ | ||
export const overrideFetchImplementation = (fetch: (...args: any[]) => any) => { | ||
(globalThis as any)[LANGSMITH_FETCH_IMPLEMENTATION_KEY] = fetch; | ||
}; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const _getFetchImplementation: () => (...args: any[]) => any = () => { | ||
return ( | ||
(globalThis as any)[LANGSMITH_FETCH_IMPLEMENTATION_KEY] ?? | ||
DEFAULT_FETCH_IMPLEMENTATION | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* eslint-disable no-process-env */ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { jest } from "@jest/globals"; | ||
import { Client } from "../client.js"; | ||
import { overrideFetchImplementation } from "../singletons/fetch.js"; | ||
|
||
describe.each([[""], ["mocked"]])("Client uses %s fetch", (description) => { | ||
let globalFetchMock: jest.Mock; | ||
let overriddenFetch: jest.Mock; | ||
let expectedFetchMock: jest.Mock; | ||
let unexpectedFetchMock: jest.Mock; | ||
|
||
beforeEach(() => { | ||
globalFetchMock = jest.fn(() => | ||
Promise.resolve({ | ||
ok: true, | ||
json: () => | ||
Promise.resolve({ | ||
batch_ingest_config: { | ||
use_multipart_endpoint: true, | ||
}, | ||
}), | ||
text: () => Promise.resolve(""), | ||
}), | ||
); | ||
overriddenFetch = jest.fn(() => | ||
Promise.resolve({ | ||
ok: true, | ||
json: () => | ||
Promise.resolve({ | ||
batch_ingest_config: { | ||
use_multipart_endpoint: true, | ||
}, | ||
}), | ||
text: () => Promise.resolve(""), | ||
}), | ||
); | ||
expectedFetchMock = | ||
description === "mocked" ? overriddenFetch : globalFetchMock; | ||
unexpectedFetchMock = | ||
description === "mocked" ? globalFetchMock : overriddenFetch; | ||
|
||
if (description === "mocked") { | ||
overrideFetchImplementation(overriddenFetch); | ||
} else { | ||
overrideFetchImplementation(globalFetchMock); | ||
} | ||
// Mock global fetch | ||
(globalThis as any).fetch = globalFetchMock; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
describe("createRuns", () => { | ||
it("should create an example with the given input and generation", async () => { | ||
const client = new Client({ apiKey: "test-api-key" }); | ||
|
||
const thread = await client.threads.create(); | ||
expect(expectedFetchMock).toHaveBeenCalledTimes(1); | ||
expect(unexpectedFetchMock).not.toHaveBeenCalled(); | ||
|
||
jest.clearAllMocks(); // Clear all mocks before the next operation | ||
|
||
// Then clear & run the function | ||
await client.runs.create(thread.thread_id, "somegraph", { | ||
input: { foo: "bar" }, | ||
}); | ||
expect(expectedFetchMock).toHaveBeenCalledTimes(1); | ||
expect(unexpectedFetchMock).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.