@skein-js/agent-protocol 0.1.0
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/LICENSE +202 -0
- package/README.md +72 -0
- package/dist/index.d.ts +370 -0
- package/dist/index.js +1382 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# @skein-js/agent-protocol
|
|
2
|
+
|
|
3
|
+
A framework-agnostic implementation of LangChain's [**Agent
|
|
4
|
+
Protocol**](https://github.com/langchain-ai/agent-protocol) for [LangGraph.js](https://langchain-ai.github.io/langgraphjs/).
|
|
5
|
+
|
|
6
|
+
This package is the **run engine + protocol handler table + SSE mapping**, and nothing else. It has
|
|
7
|
+
no opinion about your HTTP framework, your database, your queue, or your CLI — every collaborator is
|
|
8
|
+
**injected**. Give it a store, a queue, an event bus, a checkpointer, and a way to resolve graphs,
|
|
9
|
+
and it will serve assistants, threads, the three run modes (wait / stream / background), the store,
|
|
10
|
+
and human-in-the-loop interrupt/resume — wire-compatible with the official `@langchain/langgraph-sdk`
|
|
11
|
+
client.
|
|
12
|
+
|
|
13
|
+
It powers [skein-js](https://github.com/mainawycliffe/skein), but it depends only on the `@skein-js/core`
|
|
14
|
+
contracts and is designed to be consumed on its own.
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
npm install @skein-js/agent-protocol @skein-js/core @langchain/langgraph @langchain/langgraph-sdk
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { createProtocolRuntime } from "@skein-js/agent-protocol";
|
|
26
|
+
|
|
27
|
+
const runtime = createProtocolRuntime({
|
|
28
|
+
store, // a SkeinStore (e.g. @skein-js/storage-memory, @skein-js/storage-postgres)
|
|
29
|
+
graphs, // a GraphResolver — load(graphId) + schemas(graphId) (e.g. @skein-js/config's registry)
|
|
30
|
+
queue, // a RunQueue for background runs
|
|
31
|
+
bus, // a RunEventBus for streaming fan-out
|
|
32
|
+
checkpointer, // a LangGraph BaseCheckpointSaver (MemorySaver in dev)
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// One-time startup: register an assistant per graph, then start processing background runs.
|
|
36
|
+
await runtime.service.assistants.registerGraphAssistants();
|
|
37
|
+
runtime.worker.start();
|
|
38
|
+
|
|
39
|
+
// `runtime.handlers` is a transport-neutral table an adapter (e.g. @skein-js/express) mounts.
|
|
40
|
+
// `runtime.service` is the typed engine you can also drive directly.
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Two layers
|
|
44
|
+
|
|
45
|
+
- **`service`** (`createProtocolService(deps)` / `runtime.service`) — framework-agnostic logic over
|
|
46
|
+
already-validated, typed inputs. Returns plain values or an `AsyncIterable<RunFrame>`; throws
|
|
47
|
+
`SkeinHttpError`. Use this to embed the engine directly.
|
|
48
|
+
- **`handlers`** (`runtime.handlers`) — a thin table of `(ProtocolRequest) => ProtocolResponse`
|
|
49
|
+
handlers that validate raw input with Zod and delegate to the service. Framework adapters map
|
|
50
|
+
their request/response objects onto `ProtocolRequest` / `ProtocolResponse`.
|
|
51
|
+
|
|
52
|
+
`createProtocolRuntime` builds the service, handlers, and background worker over **one shared
|
|
53
|
+
context**, so cancelling a run through the service actually aborts it in the worker. Use the
|
|
54
|
+
individual `createProtocolService` / `createProtocolHandlers` factories only when you don't run a
|
|
55
|
+
worker in the same process.
|
|
56
|
+
|
|
57
|
+
### The injected contract (`ProtocolDeps`)
|
|
58
|
+
|
|
59
|
+
| Dependency | Contract (from `@skein-js/core`) | Responsibility |
|
|
60
|
+
| -------------- | ---------------------------------------------- | ------------------------------------------------------ |
|
|
61
|
+
| `store` | `SkeinStore` | Protocol resource rows (assistants/threads/runs/store) |
|
|
62
|
+
| `graphs` | `GraphResolver` | Resolve a `graph_id` to a compiled graph + schemas |
|
|
63
|
+
| `queue` | `RunQueue` | Hand background runs to a worker |
|
|
64
|
+
| `bus` | `RunEventBus` | Fan run frames out to streaming clients |
|
|
65
|
+
| `checkpointer` | `BaseCheckpointSaver` (`@langchain/langgraph`) | Graph state, history, and interrupt/resume |
|
|
66
|
+
|
|
67
|
+
Graph **state, history, and interrupt/resume are 100% LangGraph-native** via the checkpointer. The
|
|
68
|
+
`SkeinStore` owns only the protocol resource rows — it is deliberately not the checkpointer.
|
|
69
|
+
|
|
70
|
+
## License
|
|
71
|
+
|
|
72
|
+
Apache-2.0
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
import { GraphSchema, SkeinStore, RunQueue, RunEventBus, AuthEngine, Assistant, Config, StreamMode, Metadata, MultitaskStrategy, RunFrame, DefaultValues, Run, RunStatus, Item, StoreSearchQuery, SearchItem, Thread, ThreadState, StoreRepo } from '@skein-js/core';
|
|
2
|
+
import { CompiledGraph, BaseCheckpointSaver, BaseStore, Item as Item$1, Operation, OperationResults } from '@langchain/langgraph';
|
|
3
|
+
|
|
4
|
+
/** A factory export: called (optionally with per-run config) to produce a compiled graph. */
|
|
5
|
+
type CompiledGraphFactory = (config: {
|
|
6
|
+
configurable?: Record<string, unknown>;
|
|
7
|
+
}) => CompiledGraph<string> | Promise<CompiledGraph<string>>;
|
|
8
|
+
/** A resolved graph: either a compiled graph or a factory that produces one per config. */
|
|
9
|
+
type ResolvedGraph = CompiledGraph<string> | CompiledGraphFactory;
|
|
10
|
+
/** JSON schemas extracted from a graph (and any subgraphs), keyed by subgraph namespace. */
|
|
11
|
+
type GraphSchemas = Record<string, GraphSchema>;
|
|
12
|
+
/**
|
|
13
|
+
* How the engine turns a `graph_id` into something runnable and introspectable. Deliberately
|
|
14
|
+
* minimal — `@skein-js/config`'s `GraphRegistry` already satisfies it.
|
|
15
|
+
*/
|
|
16
|
+
interface GraphResolver {
|
|
17
|
+
/** Declared graph ids, used to auto-register one assistant per graph at startup. */
|
|
18
|
+
readonly ids: string[];
|
|
19
|
+
/** Load (and cache) the resolved graph for an id — a compiled graph or a per-config factory. */
|
|
20
|
+
load(graphId: string): Promise<ResolvedGraph>;
|
|
21
|
+
/** The graph's JSON schemas (input/output/state/config), for assistant introspection. */
|
|
22
|
+
schemas(graphId: string): Promise<GraphSchemas>;
|
|
23
|
+
}
|
|
24
|
+
/** A source of the current time; injected so tests are deterministic. */
|
|
25
|
+
type Clock = () => Date;
|
|
26
|
+
/** A minimal structured logger. Defaults to a no-op so nothing is required. */
|
|
27
|
+
interface Logger {
|
|
28
|
+
debug(message: string, meta?: unknown): void;
|
|
29
|
+
info(message: string, meta?: unknown): void;
|
|
30
|
+
warn(message: string, meta?: unknown): void;
|
|
31
|
+
error(message: string, meta?: unknown): void;
|
|
32
|
+
}
|
|
33
|
+
/** Everything the engine, services, and handlers need — all injected, no globals. */
|
|
34
|
+
interface ProtocolDeps {
|
|
35
|
+
/**
|
|
36
|
+
* Protocol resource rows (assistants, threads, runs, store items). Its `store` repo is also
|
|
37
|
+
* bridged into each graph run as a LangGraph `BaseStore` (see `SkeinBaseStore`), so graph nodes
|
|
38
|
+
* reach long-term cross-thread memory via `getStore()`.
|
|
39
|
+
*/
|
|
40
|
+
store: SkeinStore;
|
|
41
|
+
/** Resolves graph ids to runnable graphs and their schemas. */
|
|
42
|
+
graphs: GraphResolver;
|
|
43
|
+
/** Hands background runs to a worker. */
|
|
44
|
+
queue: RunQueue;
|
|
45
|
+
/** Fans run frames out to streaming clients (replay + live-tail). */
|
|
46
|
+
bus: RunEventBus;
|
|
47
|
+
/** LangGraph checkpointer — owns graph state, history, and interrupt/resume. */
|
|
48
|
+
checkpointer: BaseCheckpointSaver;
|
|
49
|
+
/** Time source; defaults to `() => new Date()`. */
|
|
50
|
+
clock?: Clock;
|
|
51
|
+
/** Structured logger; defaults to a no-op. */
|
|
52
|
+
logger?: Logger;
|
|
53
|
+
/**
|
|
54
|
+
* When true, the run engine logs per-run activity — start/finish (with status + duration), each
|
|
55
|
+
* tool call and tool result, and interrupts — through {@link logger}. Off by default; `skein dev
|
|
56
|
+
* --verbose` turns it on. Zero cost when off (the engine skips the stream inspection entirely).
|
|
57
|
+
*/
|
|
58
|
+
logRunActivity?: boolean;
|
|
59
|
+
/** Optional per-run wall-clock timeout in ms. When set, a run exceeding it becomes `"timeout"`. */
|
|
60
|
+
runTimeoutMs?: number;
|
|
61
|
+
/**
|
|
62
|
+
* Optional auth engine. When set, every request is authenticated (401 on failure) and authorized
|
|
63
|
+
* per resource + action (403 on deny), with ownership filters scoping reads and stamping writes.
|
|
64
|
+
* Absent means no authentication/authorization — every request is allowed (the current behavior).
|
|
65
|
+
*/
|
|
66
|
+
auth?: AuthEngine;
|
|
67
|
+
}
|
|
68
|
+
/** Fill in the optional deps (`clock`, `logger`) so the rest of the code can rely on them. */
|
|
69
|
+
interface ResolvedDeps extends ProtocolDeps {
|
|
70
|
+
clock: Clock;
|
|
71
|
+
logger: Logger;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
interface AssistantSearch {
|
|
75
|
+
graph_id?: string;
|
|
76
|
+
limit?: number;
|
|
77
|
+
offset?: number;
|
|
78
|
+
}
|
|
79
|
+
interface AssistantService {
|
|
80
|
+
/** Register one assistant per declared graph (idempotent — safe to call on every boot). */
|
|
81
|
+
registerGraphAssistants(): Promise<Assistant[]>;
|
|
82
|
+
get(assistantId: string): Promise<Assistant>;
|
|
83
|
+
list(): Promise<Assistant[]>;
|
|
84
|
+
search(query: AssistantSearch): Promise<Assistant[]>;
|
|
85
|
+
schemas(assistantId: string): Promise<GraphSchemas>;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Why a run was aborted. Absent (`null`) means the run failed on its own. */
|
|
89
|
+
type AbortReason = "cancel" | "timeout";
|
|
90
|
+
/** Handle handed to the engine for a run: the signal to thread into the graph, and the reason box. */
|
|
91
|
+
interface RunControl {
|
|
92
|
+
signal: AbortSignal;
|
|
93
|
+
/** Mutable so the aborter can record the reason the engine reads after the abort fires. */
|
|
94
|
+
reason: {
|
|
95
|
+
current: AbortReason | null;
|
|
96
|
+
};
|
|
97
|
+
/** Abort *this* run (e.g. the engine's own timeout); records the reason like an external cancel. */
|
|
98
|
+
abort(reason: AbortReason): void;
|
|
99
|
+
}
|
|
100
|
+
declare class RunControlRegistry {
|
|
101
|
+
#private;
|
|
102
|
+
/** Start tracking a run and return its abort signal + reason box. */
|
|
103
|
+
register(runId: string): RunControl;
|
|
104
|
+
/** Abort a tracked run with a reason. Returns false if the run isn't currently executing. */
|
|
105
|
+
abort(runId: string, reason: AbortReason): boolean;
|
|
106
|
+
/** Stop tracking a run once it has settled. */
|
|
107
|
+
clear(runId: string): void;
|
|
108
|
+
/** Whether a run is currently being tracked (i.e. executing). */
|
|
109
|
+
isTracking(runId: string): boolean;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
declare class ThreadLocks {
|
|
113
|
+
#private;
|
|
114
|
+
/** Run `task` after any in-flight task for `key` has settled, serializing access per key. */
|
|
115
|
+
run<T>(key: string, task: () => Promise<T>): Promise<T>;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
interface ProtocolContext {
|
|
119
|
+
deps: ResolvedDeps;
|
|
120
|
+
control: RunControlRegistry;
|
|
121
|
+
locks: ThreadLocks;
|
|
122
|
+
}
|
|
123
|
+
declare function createContext(deps: ProtocolDeps): ProtocolContext;
|
|
124
|
+
|
|
125
|
+
/** A run request as it arrives from any of the run endpoints (already validated). */
|
|
126
|
+
interface CreateRunInput {
|
|
127
|
+
thread_id?: string;
|
|
128
|
+
assistant_id: string;
|
|
129
|
+
input?: unknown;
|
|
130
|
+
command?: {
|
|
131
|
+
resume?: unknown;
|
|
132
|
+
update?: unknown;
|
|
133
|
+
goto?: unknown;
|
|
134
|
+
};
|
|
135
|
+
config?: Config;
|
|
136
|
+
context?: unknown;
|
|
137
|
+
stream_mode?: StreamMode | StreamMode[];
|
|
138
|
+
metadata?: Metadata;
|
|
139
|
+
multitask_strategy?: MultitaskStrategy;
|
|
140
|
+
interrupt_before?: string[] | "*";
|
|
141
|
+
interrupt_after?: string[] | "*";
|
|
142
|
+
}
|
|
143
|
+
/** A started streaming run: its id, plus the live frame iterable to serialize as SSE. */
|
|
144
|
+
interface StartedStream {
|
|
145
|
+
runId: string;
|
|
146
|
+
frames: AsyncIterable<RunFrame>;
|
|
147
|
+
}
|
|
148
|
+
interface RunService {
|
|
149
|
+
createWait(input: CreateRunInput): Promise<DefaultValues>;
|
|
150
|
+
createStream(input: CreateRunInput): Promise<StartedStream>;
|
|
151
|
+
createBackground(threadId: string, input: CreateRunInput): Promise<Run>;
|
|
152
|
+
get(runId: string): Promise<Run>;
|
|
153
|
+
listByThread(threadId: string): Promise<Run[]>;
|
|
154
|
+
cancel(runId: string): Promise<Run>;
|
|
155
|
+
delete(runId: string): Promise<void>;
|
|
156
|
+
join(runId: string, afterSeq?: number): Promise<AsyncIterable<RunFrame>>;
|
|
157
|
+
/** The terminal status of a run for the SSE terminal event, or null if the run is gone. */
|
|
158
|
+
finalStatus(runId: string): Promise<RunStatus | null>;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
interface StoreService {
|
|
162
|
+
put(namespace: string[], key: string, value: Record<string, unknown>): Promise<Item>;
|
|
163
|
+
get(namespace: string[], key: string): Promise<Item>;
|
|
164
|
+
delete(namespace: string[], key: string): Promise<void>;
|
|
165
|
+
search(query: StoreSearchQuery): Promise<SearchItem[]>;
|
|
166
|
+
listNamespaces(prefix?: string[]): Promise<string[][]>;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
interface CreateThreadInput {
|
|
170
|
+
thread_id?: string;
|
|
171
|
+
metadata?: Metadata;
|
|
172
|
+
}
|
|
173
|
+
interface PatchThreadInput {
|
|
174
|
+
metadata?: Metadata;
|
|
175
|
+
}
|
|
176
|
+
interface HistoryOptions {
|
|
177
|
+
limit?: number;
|
|
178
|
+
}
|
|
179
|
+
interface ThreadService {
|
|
180
|
+
create(input?: CreateThreadInput): Promise<Thread>;
|
|
181
|
+
get(threadId: string): Promise<Thread>;
|
|
182
|
+
list(): Promise<Thread[]>;
|
|
183
|
+
patch(threadId: string, patch: PatchThreadInput): Promise<Thread>;
|
|
184
|
+
delete(threadId: string): Promise<void>;
|
|
185
|
+
history(threadId: string, options?: HistoryOptions): Promise<ThreadState[]>;
|
|
186
|
+
/** The thread's current state snapshot — `GET /threads/{id}/state`, what `useStream` hydrates from. */
|
|
187
|
+
getState(threadId: string): Promise<ThreadState>;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Body for `POST /threads/{id}/stream` — a run request without the thread id (it's in the path). */
|
|
191
|
+
interface ThreadStreamInput {
|
|
192
|
+
assistant_id: string;
|
|
193
|
+
input?: unknown;
|
|
194
|
+
config?: Config;
|
|
195
|
+
context?: unknown;
|
|
196
|
+
stream_mode?: StreamMode | StreamMode[];
|
|
197
|
+
metadata?: Metadata;
|
|
198
|
+
}
|
|
199
|
+
/** Body for `POST /threads/{id}/commands` — a resume/goto/update command for an interrupted thread. */
|
|
200
|
+
interface CommandInput {
|
|
201
|
+
assistant_id?: string;
|
|
202
|
+
command?: {
|
|
203
|
+
resume?: unknown;
|
|
204
|
+
update?: unknown;
|
|
205
|
+
goto?: unknown;
|
|
206
|
+
};
|
|
207
|
+
/** Shorthand for `command.resume`. */
|
|
208
|
+
resume?: unknown;
|
|
209
|
+
stream_mode?: StreamMode | StreamMode[];
|
|
210
|
+
config?: Config;
|
|
211
|
+
context?: unknown;
|
|
212
|
+
metadata?: Metadata;
|
|
213
|
+
}
|
|
214
|
+
interface ThreadStreamService {
|
|
215
|
+
stream(threadId: string, input: ThreadStreamInput): Promise<StartedStream>;
|
|
216
|
+
joinStream(threadId: string, afterSeq?: number): Promise<StartedStream>;
|
|
217
|
+
command(threadId: string, input: CommandInput): Promise<StartedStream>;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
interface ProtocolService {
|
|
221
|
+
assistants: AssistantService;
|
|
222
|
+
threads: ThreadService;
|
|
223
|
+
threadStream: ThreadStreamService;
|
|
224
|
+
runs: RunService;
|
|
225
|
+
store: StoreService;
|
|
226
|
+
}
|
|
227
|
+
/** Assemble the service over an existing context (used by the runtime to share the context). */
|
|
228
|
+
declare function buildProtocolService(ctx: ProtocolContext): ProtocolService;
|
|
229
|
+
/** Build the service with its own context. Use {@link createProtocolRuntime} when you also run a
|
|
230
|
+
* background worker in the same process, so cancellation is shared. */
|
|
231
|
+
declare function createProtocolService(deps: ProtocolDeps): ProtocolService;
|
|
232
|
+
|
|
233
|
+
/** A normalized request an adapter maps its framework request onto. */
|
|
234
|
+
interface ProtocolRequest {
|
|
235
|
+
/** HTTP method (e.g. `"POST"`), used to synthesize the WHATWG `Request` an auth handler receives. */
|
|
236
|
+
method: string;
|
|
237
|
+
/** Absolute request URL, so a synthesized `Request` carries the path + query an auth handler reads. */
|
|
238
|
+
url: string;
|
|
239
|
+
params: Record<string, string>;
|
|
240
|
+
query: Record<string, string | string[] | undefined>;
|
|
241
|
+
body: unknown;
|
|
242
|
+
headers: Record<string, string | undefined>;
|
|
243
|
+
}
|
|
244
|
+
/** A normalized response an adapter serializes back onto its framework response. */
|
|
245
|
+
type ProtocolResponse = {
|
|
246
|
+
kind: "json";
|
|
247
|
+
status: number;
|
|
248
|
+
body: unknown;
|
|
249
|
+
} | {
|
|
250
|
+
kind: "empty";
|
|
251
|
+
status: number;
|
|
252
|
+
} | {
|
|
253
|
+
kind: "sse";
|
|
254
|
+
status: number;
|
|
255
|
+
events: AsyncIterable<string>;
|
|
256
|
+
};
|
|
257
|
+
type ProtocolHandler = (req: ProtocolRequest) => Promise<ProtocolResponse>;
|
|
258
|
+
interface ProtocolHandlers {
|
|
259
|
+
getAssistant: ProtocolHandler;
|
|
260
|
+
searchAssistants: ProtocolHandler;
|
|
261
|
+
getAssistantSchemas: ProtocolHandler;
|
|
262
|
+
createThread: ProtocolHandler;
|
|
263
|
+
getThread: ProtocolHandler;
|
|
264
|
+
listThreads: ProtocolHandler;
|
|
265
|
+
patchThread: ProtocolHandler;
|
|
266
|
+
deleteThread: ProtocolHandler;
|
|
267
|
+
getThreadHistory: ProtocolHandler;
|
|
268
|
+
getThreadState: ProtocolHandler;
|
|
269
|
+
createWaitRun: ProtocolHandler;
|
|
270
|
+
createStreamRun: ProtocolHandler;
|
|
271
|
+
createBackgroundRun: ProtocolHandler;
|
|
272
|
+
getRun: ProtocolHandler;
|
|
273
|
+
listThreadRuns: ProtocolHandler;
|
|
274
|
+
joinRunStream: ProtocolHandler;
|
|
275
|
+
cancelRun: ProtocolHandler;
|
|
276
|
+
deleteRun: ProtocolHandler;
|
|
277
|
+
postThreadStream: ProtocolHandler;
|
|
278
|
+
getThreadStream: ProtocolHandler;
|
|
279
|
+
postThreadCommands: ProtocolHandler;
|
|
280
|
+
putStoreItem: ProtocolHandler;
|
|
281
|
+
getStoreItem: ProtocolHandler;
|
|
282
|
+
deleteStoreItem: ProtocolHandler;
|
|
283
|
+
searchStoreItems: ProtocolHandler;
|
|
284
|
+
listStoreNamespaces: ProtocolHandler;
|
|
285
|
+
}
|
|
286
|
+
declare function createProtocolHandlers(service: ProtocolService): ProtocolHandlers;
|
|
287
|
+
|
|
288
|
+
interface RunWorkerOptions {
|
|
289
|
+
/** Max runs executing at once. Default 1 — strict per-thread serialization in dev. */
|
|
290
|
+
maxConcurrency?: number;
|
|
291
|
+
/** How long `stop()` waits for in-flight runs before aborting them (ms). Default 5000. */
|
|
292
|
+
shutdownGraceMs?: number;
|
|
293
|
+
}
|
|
294
|
+
interface RunWorker {
|
|
295
|
+
/** Begin consuming and executing queued runs. Idempotent. */
|
|
296
|
+
start(): void;
|
|
297
|
+
/** Stop consuming; drain in-flight runs, then abort any still running past the grace deadline. */
|
|
298
|
+
stop(): Promise<void>;
|
|
299
|
+
}
|
|
300
|
+
declare function createRunWorker(ctx: ProtocolContext, options?: RunWorkerOptions): RunWorker;
|
|
301
|
+
|
|
302
|
+
interface ProtocolRuntimeOptions {
|
|
303
|
+
worker?: RunWorkerOptions;
|
|
304
|
+
}
|
|
305
|
+
interface ProtocolRuntime {
|
|
306
|
+
service: ProtocolService;
|
|
307
|
+
handlers: ProtocolHandlers;
|
|
308
|
+
worker: RunWorker;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Wire the whole engine together over a single shared context. Call
|
|
312
|
+
* `runtime.service.assistants.registerGraphAssistants()` once at startup to seed assistants, then
|
|
313
|
+
* `runtime.worker.start()` to process background runs.
|
|
314
|
+
*/
|
|
315
|
+
declare function createProtocolRuntime(deps: ProtocolDeps, options?: ProtocolRuntimeOptions): ProtocolRuntime;
|
|
316
|
+
|
|
317
|
+
type LangGraphSearchItem = Item$1 & {
|
|
318
|
+
score?: number;
|
|
319
|
+
};
|
|
320
|
+
/**
|
|
321
|
+
* A LangGraph `BaseStore` backed by a skein `StoreRepo`. All behavior — naive scan in the memory
|
|
322
|
+
* driver, pgvector semantic search in Postgres — comes from the underlying repo; this class only
|
|
323
|
+
* adapts the method shapes.
|
|
324
|
+
*
|
|
325
|
+
* Not adapted (the repo has no equivalent, matching the HTTP surface): `search`'s `filter`, the
|
|
326
|
+
* per-`put` `index` override (indexing is configured once via `langgraph.json`'s `store.index`), and
|
|
327
|
+
* `listNamespaces`' `suffix`/`maxDepth`. These are accepted and ignored rather than throwing.
|
|
328
|
+
*/
|
|
329
|
+
declare class SkeinBaseStore extends BaseStore {
|
|
330
|
+
private readonly repo;
|
|
331
|
+
constructor(repo: StoreRepo);
|
|
332
|
+
get(namespace: string[], key: string): Promise<Item$1 | null>;
|
|
333
|
+
put(namespace: string[], key: string, value: Record<string, unknown>): Promise<void>;
|
|
334
|
+
delete(namespace: string[], key: string): Promise<void>;
|
|
335
|
+
search(namespacePrefix: string[], options?: {
|
|
336
|
+
filter?: Record<string, unknown>;
|
|
337
|
+
limit?: number;
|
|
338
|
+
offset?: number;
|
|
339
|
+
query?: string;
|
|
340
|
+
}): Promise<LangGraphSearchItem[]>;
|
|
341
|
+
listNamespaces(options?: {
|
|
342
|
+
prefix?: string[];
|
|
343
|
+
suffix?: string[];
|
|
344
|
+
maxDepth?: number;
|
|
345
|
+
limit?: number;
|
|
346
|
+
offset?: number;
|
|
347
|
+
}): Promise<string[][]>;
|
|
348
|
+
batch<Op extends Operation[]>(operations: Op): Promise<OperationResults<Op>>;
|
|
349
|
+
private runOperation;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/** SSE response headers an adapter should set before writing the event stream. */
|
|
353
|
+
declare const SSE_HEADERS: Readonly<Record<string, string>>;
|
|
354
|
+
/** Serialize one frame as an SSE block: `id:` for reconnect, `event:` name, JSON `data:`. */
|
|
355
|
+
declare function encodeFrame(frame: RunFrame): string;
|
|
356
|
+
/** Serialize the synthesized terminal event from a run's final status. */
|
|
357
|
+
declare function encodeTerminal(status: RunStatus): string;
|
|
358
|
+
/**
|
|
359
|
+
* Turn a frame iterable into an SSE string iterable, appending a terminal `end`/`error` event read
|
|
360
|
+
* from `finalStatus()` once the frames are exhausted (the bus closed). `finalStatus` is called
|
|
361
|
+
* lazily at the end so it reflects the run's terminal row, not its status when streaming began.
|
|
362
|
+
*/
|
|
363
|
+
declare function toSseEvents(frames: AsyncIterable<RunFrame>, finalStatus: () => Promise<RunStatus | null>): AsyncIterable<string>;
|
|
364
|
+
/**
|
|
365
|
+
* Parse a `Last-Event-ID` header into an `afterSeq` for {@link RunEventBus.subscribe}. A missing or
|
|
366
|
+
* malformed header means "from the beginning" (`0`), so a fresh connection replays everything.
|
|
367
|
+
*/
|
|
368
|
+
declare function parseAfterSeq(lastEventId: string | undefined): number;
|
|
369
|
+
|
|
370
|
+
export { type AssistantSearch, type AssistantService, type Clock, type CommandInput, type CompiledGraphFactory, type CreateRunInput, type CreateThreadInput, type GraphResolver, type GraphSchemas, type HistoryOptions, type Logger, type PatchThreadInput, type ProtocolContext, type ProtocolDeps, type ProtocolHandler, type ProtocolHandlers, type ProtocolRequest, type ProtocolResponse, type ProtocolRuntime, type ProtocolRuntimeOptions, type ProtocolService, type ResolvedGraph, type RunService, type RunWorker, type RunWorkerOptions, SSE_HEADERS, SkeinBaseStore, type StartedStream, type StoreService, type ThreadService, type ThreadStreamInput, type ThreadStreamService, buildProtocolService, createContext, createProtocolHandlers, createProtocolRuntime, createProtocolService, createRunWorker, encodeFrame, encodeTerminal, parseAfterSeq, toSseEvents };
|