@langchain/langgraph-sdk 0.0.17 → 0.0.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.
- package/dist/client.cjs +32 -3
- package/dist/client.d.ts +6 -1
- package/dist/client.js +32 -3
- package/dist/schema.d.ts +1 -1
- package/dist/types.d.ts +6 -1
- package/dist/utils/signals.cjs +22 -0
- package/dist/utils/signals.d.ts +1 -0
- package/dist/utils/signals.js +18 -0
- package/package.json +1 -1
package/dist/client.cjs
CHANGED
|
@@ -4,6 +4,7 @@ exports.Client = exports.StoreClient = exports.RunsClient = exports.ThreadsClien
|
|
|
4
4
|
const async_caller_js_1 = require("./utils/async_caller.cjs");
|
|
5
5
|
const index_js_1 = require("./utils/eventsource-parser/index.cjs");
|
|
6
6
|
const stream_js_1 = require("./utils/stream.cjs");
|
|
7
|
+
const signals_js_1 = require("./utils/signals.cjs");
|
|
7
8
|
class BaseClient {
|
|
8
9
|
constructor(config) {
|
|
9
10
|
Object.defineProperty(this, "asyncCaller", {
|
|
@@ -36,6 +37,8 @@ class BaseClient {
|
|
|
36
37
|
...config?.callerOptions,
|
|
37
38
|
});
|
|
38
39
|
this.timeoutMs = config?.timeoutMs || 12_000;
|
|
40
|
+
// default limit being capped by Chrome
|
|
41
|
+
// https://github.com/nodejs/undici/issues/1373
|
|
39
42
|
this.apiUrl = config?.apiUrl || "http://localhost:8123";
|
|
40
43
|
this.defaultHeaders = config?.defaultHeaders || {};
|
|
41
44
|
if (config?.apiKey != null) {
|
|
@@ -55,6 +58,16 @@ class BaseClient {
|
|
|
55
58
|
};
|
|
56
59
|
delete mutatedOptions.json;
|
|
57
60
|
}
|
|
61
|
+
let timeoutSignal = null;
|
|
62
|
+
if (typeof options?.timeoutMs !== "undefined") {
|
|
63
|
+
if (options.timeoutMs != null) {
|
|
64
|
+
timeoutSignal = AbortSignal.timeout(options.timeoutMs);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
timeoutSignal = AbortSignal.timeout(this.timeoutMs);
|
|
69
|
+
}
|
|
70
|
+
mutatedOptions.signal = (0, signals_js_1.mergeSignals)(timeoutSignal, mutatedOptions.signal);
|
|
58
71
|
const targetUrl = new URL(`${this.apiUrl}${path}`);
|
|
59
72
|
if (mutatedOptions.params) {
|
|
60
73
|
for (const [key, value] of Object.entries(mutatedOptions.params)) {
|
|
@@ -474,6 +487,7 @@ class RunsClient extends BaseClient {
|
|
|
474
487
|
const response = await this.asyncCaller.fetch(...this.prepareFetchOptions(endpoint, {
|
|
475
488
|
method: "POST",
|
|
476
489
|
json,
|
|
490
|
+
timeoutMs: null,
|
|
477
491
|
signal: payload?.signal,
|
|
478
492
|
}));
|
|
479
493
|
let parser;
|
|
@@ -579,11 +593,22 @@ class RunsClient extends BaseClient {
|
|
|
579
593
|
after_seconds: payload?.afterSeconds,
|
|
580
594
|
};
|
|
581
595
|
const endpoint = threadId == null ? `/runs/wait` : `/threads/${threadId}/runs/wait`;
|
|
582
|
-
|
|
596
|
+
const response = await this.fetch(endpoint, {
|
|
583
597
|
method: "POST",
|
|
584
598
|
json,
|
|
599
|
+
timeoutMs: null,
|
|
585
600
|
signal: payload?.signal,
|
|
586
601
|
});
|
|
602
|
+
const raiseError = payload?.raiseError !== undefined ? payload.raiseError : true;
|
|
603
|
+
if (raiseError &&
|
|
604
|
+
"__error__" in response &&
|
|
605
|
+
typeof response.__error__ === "object" &&
|
|
606
|
+
response.__error__ &&
|
|
607
|
+
"error" in response.__error__ &&
|
|
608
|
+
"message" in response.__error__) {
|
|
609
|
+
throw new Error(`${response.__error__?.error}: ${response.__error__?.message}`);
|
|
610
|
+
}
|
|
611
|
+
return response;
|
|
587
612
|
}
|
|
588
613
|
/**
|
|
589
614
|
* List all runs for a thread.
|
|
@@ -633,8 +658,11 @@ class RunsClient extends BaseClient {
|
|
|
633
658
|
* @param runId The ID of the run.
|
|
634
659
|
* @returns
|
|
635
660
|
*/
|
|
636
|
-
async join(threadId, runId) {
|
|
637
|
-
return this.fetch(`/threads/${threadId}/runs/${runId}/join
|
|
661
|
+
async join(threadId, runId, options) {
|
|
662
|
+
return this.fetch(`/threads/${threadId}/runs/${runId}/join`, {
|
|
663
|
+
timeoutMs: null,
|
|
664
|
+
signal: options?.signal,
|
|
665
|
+
});
|
|
638
666
|
}
|
|
639
667
|
/**
|
|
640
668
|
* Stream output from a run in real-time, until the run is done.
|
|
@@ -649,6 +677,7 @@ class RunsClient extends BaseClient {
|
|
|
649
677
|
async *joinStream(threadId, runId, signal) {
|
|
650
678
|
const response = await this.asyncCaller.fetch(...this.prepareFetchOptions(`/threads/${threadId}/runs/${runId}/stream`, {
|
|
651
679
|
method: "GET",
|
|
680
|
+
timeoutMs: null,
|
|
652
681
|
signal,
|
|
653
682
|
}));
|
|
654
683
|
let parser;
|
package/dist/client.d.ts
CHANGED
|
@@ -17,10 +17,13 @@ declare class BaseClient {
|
|
|
17
17
|
protected prepareFetchOptions(path: string, options?: RequestInit & {
|
|
18
18
|
json?: unknown;
|
|
19
19
|
params?: Record<string, unknown>;
|
|
20
|
+
timeoutMs?: number | null;
|
|
20
21
|
}): [url: URL, init: RequestInit];
|
|
21
22
|
protected fetch<T>(path: string, options?: RequestInit & {
|
|
22
23
|
json?: unknown;
|
|
23
24
|
params?: Record<string, unknown>;
|
|
25
|
+
timeoutMs?: number | null;
|
|
26
|
+
signal?: AbortSignal;
|
|
24
27
|
}): Promise<T>;
|
|
25
28
|
}
|
|
26
29
|
export declare class CronsClient extends BaseClient {
|
|
@@ -334,7 +337,9 @@ export declare class RunsClient extends BaseClient {
|
|
|
334
337
|
* @param runId The ID of the run.
|
|
335
338
|
* @returns
|
|
336
339
|
*/
|
|
337
|
-
join(threadId: string, runId: string
|
|
340
|
+
join(threadId: string, runId: string, options?: {
|
|
341
|
+
signal?: AbortSignal;
|
|
342
|
+
}): Promise<void>;
|
|
338
343
|
/**
|
|
339
344
|
* Stream output from a run in real-time, until the run is done.
|
|
340
345
|
* Output is not buffered, so any output produced before this call will
|
package/dist/client.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { AsyncCaller } from "./utils/async_caller.js";
|
|
2
2
|
import { createParser, } from "./utils/eventsource-parser/index.js";
|
|
3
3
|
import { IterableReadableStream } from "./utils/stream.js";
|
|
4
|
+
import { mergeSignals } from "./utils/signals.js";
|
|
4
5
|
class BaseClient {
|
|
5
6
|
constructor(config) {
|
|
6
7
|
Object.defineProperty(this, "asyncCaller", {
|
|
@@ -33,6 +34,8 @@ class BaseClient {
|
|
|
33
34
|
...config?.callerOptions,
|
|
34
35
|
});
|
|
35
36
|
this.timeoutMs = config?.timeoutMs || 12_000;
|
|
37
|
+
// default limit being capped by Chrome
|
|
38
|
+
// https://github.com/nodejs/undici/issues/1373
|
|
36
39
|
this.apiUrl = config?.apiUrl || "http://localhost:8123";
|
|
37
40
|
this.defaultHeaders = config?.defaultHeaders || {};
|
|
38
41
|
if (config?.apiKey != null) {
|
|
@@ -52,6 +55,16 @@ class BaseClient {
|
|
|
52
55
|
};
|
|
53
56
|
delete mutatedOptions.json;
|
|
54
57
|
}
|
|
58
|
+
let timeoutSignal = null;
|
|
59
|
+
if (typeof options?.timeoutMs !== "undefined") {
|
|
60
|
+
if (options.timeoutMs != null) {
|
|
61
|
+
timeoutSignal = AbortSignal.timeout(options.timeoutMs);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
timeoutSignal = AbortSignal.timeout(this.timeoutMs);
|
|
66
|
+
}
|
|
67
|
+
mutatedOptions.signal = mergeSignals(timeoutSignal, mutatedOptions.signal);
|
|
55
68
|
const targetUrl = new URL(`${this.apiUrl}${path}`);
|
|
56
69
|
if (mutatedOptions.params) {
|
|
57
70
|
for (const [key, value] of Object.entries(mutatedOptions.params)) {
|
|
@@ -468,6 +481,7 @@ export class RunsClient extends BaseClient {
|
|
|
468
481
|
const response = await this.asyncCaller.fetch(...this.prepareFetchOptions(endpoint, {
|
|
469
482
|
method: "POST",
|
|
470
483
|
json,
|
|
484
|
+
timeoutMs: null,
|
|
471
485
|
signal: payload?.signal,
|
|
472
486
|
}));
|
|
473
487
|
let parser;
|
|
@@ -573,11 +587,22 @@ export class RunsClient extends BaseClient {
|
|
|
573
587
|
after_seconds: payload?.afterSeconds,
|
|
574
588
|
};
|
|
575
589
|
const endpoint = threadId == null ? `/runs/wait` : `/threads/${threadId}/runs/wait`;
|
|
576
|
-
|
|
590
|
+
const response = await this.fetch(endpoint, {
|
|
577
591
|
method: "POST",
|
|
578
592
|
json,
|
|
593
|
+
timeoutMs: null,
|
|
579
594
|
signal: payload?.signal,
|
|
580
595
|
});
|
|
596
|
+
const raiseError = payload?.raiseError !== undefined ? payload.raiseError : true;
|
|
597
|
+
if (raiseError &&
|
|
598
|
+
"__error__" in response &&
|
|
599
|
+
typeof response.__error__ === "object" &&
|
|
600
|
+
response.__error__ &&
|
|
601
|
+
"error" in response.__error__ &&
|
|
602
|
+
"message" in response.__error__) {
|
|
603
|
+
throw new Error(`${response.__error__?.error}: ${response.__error__?.message}`);
|
|
604
|
+
}
|
|
605
|
+
return response;
|
|
581
606
|
}
|
|
582
607
|
/**
|
|
583
608
|
* List all runs for a thread.
|
|
@@ -627,8 +652,11 @@ export class RunsClient extends BaseClient {
|
|
|
627
652
|
* @param runId The ID of the run.
|
|
628
653
|
* @returns
|
|
629
654
|
*/
|
|
630
|
-
async join(threadId, runId) {
|
|
631
|
-
return this.fetch(`/threads/${threadId}/runs/${runId}/join
|
|
655
|
+
async join(threadId, runId, options) {
|
|
656
|
+
return this.fetch(`/threads/${threadId}/runs/${runId}/join`, {
|
|
657
|
+
timeoutMs: null,
|
|
658
|
+
signal: options?.signal,
|
|
659
|
+
});
|
|
632
660
|
}
|
|
633
661
|
/**
|
|
634
662
|
* Stream output from a run in real-time, until the run is done.
|
|
@@ -643,6 +671,7 @@ export class RunsClient extends BaseClient {
|
|
|
643
671
|
async *joinStream(threadId, runId, signal) {
|
|
644
672
|
const response = await this.asyncCaller.fetch(...this.prepareFetchOptions(`/threads/${threadId}/runs/${runId}/stream`, {
|
|
645
673
|
method: "GET",
|
|
674
|
+
timeoutMs: null,
|
|
646
675
|
signal,
|
|
647
676
|
}));
|
|
648
677
|
let parser;
|
package/dist/schema.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { JSONSchema7 } from "json-schema";
|
|
2
2
|
type Optional<T> = T | null | undefined;
|
|
3
3
|
type RunStatus = "pending" | "running" | "error" | "success" | "timeout" | "interrupted";
|
|
4
|
-
type ThreadStatus = "idle" | "busy" | "interrupted";
|
|
4
|
+
type ThreadStatus = "idle" | "busy" | "interrupted" | "error";
|
|
5
5
|
type MultitaskStrategy = "reject" | "interrupt" | "rollback" | "enqueue";
|
|
6
6
|
export interface Config {
|
|
7
7
|
/**
|
package/dist/types.d.ts
CHANGED
|
@@ -102,5 +102,10 @@ export interface CronsCreatePayload extends RunsCreatePayload {
|
|
|
102
102
|
*/
|
|
103
103
|
schedule: string;
|
|
104
104
|
}
|
|
105
|
-
export
|
|
105
|
+
export interface RunsWaitPayload extends RunsStreamPayload {
|
|
106
|
+
/**
|
|
107
|
+
* Raise errors returned by the run. Default is `true`.
|
|
108
|
+
*/
|
|
109
|
+
raiseError?: boolean;
|
|
110
|
+
}
|
|
106
111
|
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mergeSignals = void 0;
|
|
4
|
+
function mergeSignals(...signals) {
|
|
5
|
+
const nonZeroSignals = signals.filter((signal) => signal != null);
|
|
6
|
+
if (nonZeroSignals.length === 0)
|
|
7
|
+
return undefined;
|
|
8
|
+
if (nonZeroSignals.length === 1)
|
|
9
|
+
return nonZeroSignals[0];
|
|
10
|
+
const controller = new AbortController();
|
|
11
|
+
for (const signal of signals) {
|
|
12
|
+
if (signal?.aborted) {
|
|
13
|
+
controller.abort(signal.reason);
|
|
14
|
+
return controller.signal;
|
|
15
|
+
}
|
|
16
|
+
signal?.addEventListener("abort", () => controller.abort(signal.reason), {
|
|
17
|
+
once: true,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
return controller.signal;
|
|
21
|
+
}
|
|
22
|
+
exports.mergeSignals = mergeSignals;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function mergeSignals(...signals: (AbortSignal | null | undefined)[]): AbortSignal | undefined;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export function mergeSignals(...signals) {
|
|
2
|
+
const nonZeroSignals = signals.filter((signal) => signal != null);
|
|
3
|
+
if (nonZeroSignals.length === 0)
|
|
4
|
+
return undefined;
|
|
5
|
+
if (nonZeroSignals.length === 1)
|
|
6
|
+
return nonZeroSignals[0];
|
|
7
|
+
const controller = new AbortController();
|
|
8
|
+
for (const signal of signals) {
|
|
9
|
+
if (signal?.aborted) {
|
|
10
|
+
controller.abort(signal.reason);
|
|
11
|
+
return controller.signal;
|
|
12
|
+
}
|
|
13
|
+
signal?.addEventListener("abort", () => controller.abort(signal.reason), {
|
|
14
|
+
once: true,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return controller.signal;
|
|
18
|
+
}
|