Skip to content

Commit

Permalink
Adds signal test, fix bug (#297)
Browse files Browse the repository at this point in the history
* Adds signal test, fix bug

* Lint

* Fix
  • Loading branch information
jacoblee93 authored Aug 5, 2024
1 parent e0801a0 commit 3500df4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion langgraph/src/pregel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ export class Pregel<
proc.invoke(input, updatedConfig)
);

await executeTasks(tasks, this.stepTimeout);
await executeTasks(tasks, this.stepTimeout, config.signal);

// combine pending writes from all tasks
const pendingWrites: Array<[keyof Cc, unknown]> = [];
Expand Down
36 changes: 36 additions & 0 deletions langgraph/src/tests/prebuilt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,42 @@ describe("createReactAgent", () => {
]);
});

it("Should respect a passed signal", async () => {
const llm = new FakeToolCallingChatModel({
responses: [
new AIMessage({
content: "result1",
tool_calls: [
{ name: "search_api", id: "tool_abcd123", args: { query: "foo" } },
],
}),
new AIMessage("result2"),
],
sleep: 500,
});

const agent = createReactAgent({
llm,
tools: [new SearchAPIWithArtifact()],
messageModifier: "You are a helpful assistant",
});

const controller = new AbortController();

setTimeout(() => controller.abort(), 100);

await expect(async () => {
await agent.invoke(
{
messages: [new HumanMessage("Hello Input!")],
},
{
signal: controller.signal,
}
);
}).rejects.toThrowError();
});

it("Works with tools that return content_and_artifact response format", async () => {
const llm = new FakeToolCallingChatModel({
responses: [
Expand Down
4 changes: 4 additions & 0 deletions langgraph/src/tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-promise-executor-return */
import assert from "node:assert";
import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
import {
Expand Down Expand Up @@ -97,6 +98,9 @@ export class FakeToolCallingChatModel extends BaseChatModel {
if (this.thrownErrorString) {
throw new Error(this.thrownErrorString);
}
if (this.sleep !== undefined) {
await new Promise((resolve) => setTimeout(resolve, this.sleep));
}
const msg = this.responses?.[this.idx] ?? messages[this.idx];
const generation: ChatResult = {
generations: [
Expand Down

0 comments on commit 3500df4

Please sign in to comment.