@skein-js/runtime 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 +46 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.js +207 -0
- package/package.json +65 -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,46 @@
|
|
|
1
|
+
# @skein-js/runtime
|
|
2
|
+
|
|
3
|
+
Assembles a skein-js [`ProtocolDeps`](../agent-protocol/src/deps.ts) from a `langgraph.json`
|
|
4
|
+
plus a chosen set of drivers, and hands it to any framework adapter through the injectable
|
|
5
|
+
`{ deps }` seam. This is the piece that lets `skein dev` and `skein up` run the **same** engine
|
|
6
|
+
against either the zero-setup in-memory drivers or production-shaped Postgres + Redis.
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
import { buildRuntime } from "@skein-js/runtime";
|
|
10
|
+
|
|
11
|
+
const runtime = await buildRuntime({
|
|
12
|
+
configPath: "/abs/path/to/langgraph.json",
|
|
13
|
+
store: "postgres", // "memory" | "postgres" (postgres reads DATABASE_URL)
|
|
14
|
+
queue: "redis", // "memory" | "redis" (redis reads REDIS_URL)
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const server = await createExpressServer({ deps: runtime.deps, cors: runtime.cors });
|
|
18
|
+
// ... on shutdown:
|
|
19
|
+
await runtime.dispose();
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
- **`store: "postgres"`** connects `PostgresSkeinStore` (from `DATABASE_URL`), runs its
|
|
23
|
+
migrations, and uses `PostgresSaver` as the LangGraph checkpointer.
|
|
24
|
+
- **`queue: "redis"`** uses the BullMQ run queue + Redis Streams/pub-sub event bus (`REDIS_URL`).
|
|
25
|
+
- **`store: "memory"` + `queue: "memory"`** delegates to `@skein-js/express`'s reloadable
|
|
26
|
+
in-memory runtime, so `skein dev`'s hot-reload and cross-restart state persistence still work.
|
|
27
|
+
|
|
28
|
+
Graph hot-reload (`reloadGraphs()`) works in every mode; `snapshotState`/`hydrateState` are
|
|
29
|
+
present only in all-memory mode (durable stores keep their own state).
|
|
30
|
+
|
|
31
|
+
## Semantic search (`store.index.embed`)
|
|
32
|
+
|
|
33
|
+
When `store: "postgres"` and `langgraph.json` declares a `store.index`, `buildRuntime` resolves the
|
|
34
|
+
`embed` value into an embedder and enables pgvector semantic search — honoring both forms the
|
|
35
|
+
LangGraph CLI documents:
|
|
36
|
+
|
|
37
|
+
- **`"provider:model"`** — e.g. `"openai:text-embedding-3-small"`. Mirrors Python `init_embeddings`:
|
|
38
|
+
the provider prefix selects a `@langchain/<provider>` package (dynamically imported — install it in
|
|
39
|
+
your project, e.g. `@langchain/openai`, and set its API key). Supported prefixes: `openai`,
|
|
40
|
+
`azure_openai`, `cohere`, `google_genai`, `mistralai`, `bedrock`, `ollama`.
|
|
41
|
+
- **Custom-function path** — e.g. `"./embeddings.ts:embed"`. The export is either a raw
|
|
42
|
+
`(texts: string[]) => number[][]` (the shape LangGraph documents) or a LangChain `Embeddings`
|
|
43
|
+
instance. Resolved through the same `path:export` loader used for graphs — no extra dependency.
|
|
44
|
+
|
|
45
|
+
`store.index.dims` is required whenever `embed` is set. Without a `store.index`, Postgres search
|
|
46
|
+
falls back to naive text matching (identical to the memory driver).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { ProtocolDeps } from '@skein-js/agent-protocol';
|
|
2
|
+
import { ModuleImporter } from '@skein-js/config';
|
|
3
|
+
import { DevStateSnapshot } from '@skein-js/express';
|
|
4
|
+
import { CorsOptions } from 'cors';
|
|
5
|
+
import { EmbedFunction } from '@skein-js/storage-postgres';
|
|
6
|
+
|
|
7
|
+
/** Where protocol resources (assistants/threads/runs/store) and checkpoints are persisted. */
|
|
8
|
+
type StoreDriver = "memory" | "postgres";
|
|
9
|
+
/** Where background runs are queued and stream frames are fanned out. */
|
|
10
|
+
type QueueDriver = "memory" | "redis";
|
|
11
|
+
interface BuildRuntimeOptions {
|
|
12
|
+
/** Absolute path to `langgraph.json`. */
|
|
13
|
+
configPath: string;
|
|
14
|
+
/** TS-capable importer (e.g. the CLI's vite loader). Omitted for plain JS/Node resolution. */
|
|
15
|
+
importModule?: ModuleImporter;
|
|
16
|
+
/** `"memory"` (default dev) or `"postgres"` (reads `DATABASE_URL`). */
|
|
17
|
+
store: StoreDriver;
|
|
18
|
+
/** `"memory"` (default dev) or `"redis"` (reads `REDIS_URL`). */
|
|
19
|
+
queue: QueueDriver;
|
|
20
|
+
}
|
|
21
|
+
interface SkeinRuntime {
|
|
22
|
+
/** Assembled dependency bundle to pass as `createExpressServer({ deps })`. */
|
|
23
|
+
deps: ProtocolDeps;
|
|
24
|
+
/** CORS mapped from the config's `http.cors`, or `undefined` when none is declared. */
|
|
25
|
+
cors?: CorsOptions;
|
|
26
|
+
/** Re-read the config and swap in freshly imported graphs, keeping every driver + all state. */
|
|
27
|
+
reloadGraphs(): Promise<void>;
|
|
28
|
+
/** Tear down whichever concrete Postgres/Redis resources were created (no-op for all-memory). */
|
|
29
|
+
dispose(): Promise<void>;
|
|
30
|
+
/** Present only in all-memory mode — durable stores keep their own state. */
|
|
31
|
+
snapshotState?(): DevStateSnapshot;
|
|
32
|
+
/** Present only in all-memory mode. */
|
|
33
|
+
hydrateState?(snapshot: DevStateSnapshot): void;
|
|
34
|
+
}
|
|
35
|
+
/** Assemble a {@link SkeinRuntime} for the requested driver combination. */
|
|
36
|
+
declare function buildRuntime(options: BuildRuntimeOptions): Promise<SkeinRuntime>;
|
|
37
|
+
|
|
38
|
+
/** Thrown when a selected driver's connection env or `store.index.embed` config can't be resolved. */
|
|
39
|
+
declare class RuntimeConfigError extends Error {
|
|
40
|
+
constructor(message: string);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface ResolveEmbedOptions {
|
|
44
|
+
/** Directory holding `langgraph.json`, used to resolve a custom-function path. */
|
|
45
|
+
configDir: string;
|
|
46
|
+
/** TS-capable importer (the CLI's vite loader) for a custom-function `.ts` path. */
|
|
47
|
+
importModule?: ModuleImporter;
|
|
48
|
+
}
|
|
49
|
+
/** Resolve a `store.index.embed` value to an `EmbedFunction`, honoring both LangGraph forms. */
|
|
50
|
+
declare function resolveEmbed(embed: string, options: ResolveEmbedOptions): Promise<EmbedFunction>;
|
|
51
|
+
|
|
52
|
+
export { type BuildRuntimeOptions, type QueueDriver, type ResolveEmbedOptions, RuntimeConfigError, type SkeinRuntime, type StoreDriver, buildRuntime, resolveEmbed };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
// src/build-runtime.ts
|
|
2
|
+
import { MemorySaver } from "@langchain/langgraph";
|
|
3
|
+
import { PostgresSaver } from "@langchain/langgraph-checkpoint-postgres";
|
|
4
|
+
import {
|
|
5
|
+
loadAuthEngine,
|
|
6
|
+
loadConfig
|
|
7
|
+
} from "@skein-js/config";
|
|
8
|
+
import {
|
|
9
|
+
corsFromHttpConfig,
|
|
10
|
+
loadReloadableInMemoryRuntime
|
|
11
|
+
} from "@skein-js/express";
|
|
12
|
+
import { RedisRunEventBus, RedisRunQueue } from "@skein-js/redis";
|
|
13
|
+
import { MemoryRunEventBus, MemoryRunQueue, MemorySkeinStore } from "@skein-js/storage-memory";
|
|
14
|
+
import { PostgresSkeinStore } from "@skein-js/storage-postgres";
|
|
15
|
+
|
|
16
|
+
// src/errors.ts
|
|
17
|
+
var RuntimeConfigError = class extends Error {
|
|
18
|
+
constructor(message) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.name = "RuntimeConfigError";
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// src/resolve-embed.ts
|
|
25
|
+
import { pathToFileURL } from "url";
|
|
26
|
+
import { parseGraphSpec } from "@skein-js/config";
|
|
27
|
+
var PROVIDERS = {
|
|
28
|
+
openai: { package: "@langchain/openai", exportName: "OpenAIEmbeddings" },
|
|
29
|
+
azure_openai: { package: "@langchain/openai", exportName: "AzureOpenAIEmbeddings" },
|
|
30
|
+
cohere: { package: "@langchain/cohere", exportName: "CohereEmbeddings" },
|
|
31
|
+
google_genai: { package: "@langchain/google-genai", exportName: "GoogleGenerativeAIEmbeddings" },
|
|
32
|
+
mistralai: { package: "@langchain/mistralai", exportName: "MistralAIEmbeddings" },
|
|
33
|
+
bedrock: { package: "@langchain/aws", exportName: "BedrockEmbeddings" },
|
|
34
|
+
ollama: { package: "@langchain/ollama", exportName: "OllamaEmbeddings" }
|
|
35
|
+
};
|
|
36
|
+
function toEmbedFunction(value, source) {
|
|
37
|
+
if (typeof value === "function") {
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
if (typeof value === "object" && value !== null && "embedDocuments" in value && typeof value.embedDocuments === "function") {
|
|
41
|
+
const embeddings = value;
|
|
42
|
+
return (texts) => embeddings.embedDocuments(texts);
|
|
43
|
+
}
|
|
44
|
+
throw new RuntimeConfigError(
|
|
45
|
+
`store.index.embed "${source}" must resolve to a function (texts: string[]) => number[][] or a LangChain Embeddings instance.`
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
function isCustomFunctionPath(embed) {
|
|
49
|
+
const head = embed.split(":", 1)[0] ?? "";
|
|
50
|
+
return head.startsWith(".") || head.startsWith("/") || /\.([mc]?[jt]s|py)$/.test(head);
|
|
51
|
+
}
|
|
52
|
+
async function resolvePathEmbed(embed, options) {
|
|
53
|
+
const { sourceFile, exportSymbol } = parseGraphSpec(embed, options.configDir);
|
|
54
|
+
const importer = options.importModule ?? ((file) => import(pathToFileURL(file).href));
|
|
55
|
+
const module = await importer(sourceFile);
|
|
56
|
+
const exported = module[exportSymbol];
|
|
57
|
+
if (exported === void 0) {
|
|
58
|
+
throw new RuntimeConfigError(
|
|
59
|
+
`store.index.embed "${embed}" \u2014 module "${sourceFile}" has no export "${exportSymbol}".`
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
return toEmbedFunction(exported, embed);
|
|
63
|
+
}
|
|
64
|
+
async function resolveProviderEmbed(provider, model) {
|
|
65
|
+
const entry = PROVIDERS[provider];
|
|
66
|
+
if (!entry) {
|
|
67
|
+
throw new RuntimeConfigError(
|
|
68
|
+
`Unknown embeddings provider "${provider}". Supported: ${Object.keys(PROVIDERS).join(", ")}. Or point store.index.embed at a "./path:export" custom embedder.`
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
let module;
|
|
72
|
+
try {
|
|
73
|
+
module = await import(entry.package);
|
|
74
|
+
} catch {
|
|
75
|
+
throw new RuntimeConfigError(
|
|
76
|
+
`store.index.embed uses "${provider}:", so install ${entry.package} in your project (and set the provider's API key).`
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
const EmbeddingsClass = module[entry.exportName];
|
|
80
|
+
if (typeof EmbeddingsClass !== "function") {
|
|
81
|
+
throw new RuntimeConfigError(`${entry.package} does not export "${entry.exportName}".`);
|
|
82
|
+
}
|
|
83
|
+
const embeddings = new EmbeddingsClass({ model });
|
|
84
|
+
return (texts) => embeddings.embedDocuments(texts);
|
|
85
|
+
}
|
|
86
|
+
async function resolveEmbed(embed, options) {
|
|
87
|
+
if (isCustomFunctionPath(embed)) {
|
|
88
|
+
return resolvePathEmbed(embed, options);
|
|
89
|
+
}
|
|
90
|
+
const separator = embed.indexOf(":");
|
|
91
|
+
if (separator === -1) {
|
|
92
|
+
throw new RuntimeConfigError(
|
|
93
|
+
`store.index.embed "${embed}" must be "provider:model" (e.g. "openai:text-embedding-3-small") or a "./path:export" custom embedder.`
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
return resolveProviderEmbed(embed.slice(0, separator), embed.slice(separator + 1));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// src/build-runtime.ts
|
|
100
|
+
function requireEnv(name, driver) {
|
|
101
|
+
const value = process.env[name];
|
|
102
|
+
if (!value) {
|
|
103
|
+
throw new RuntimeConfigError(`The "${driver}" driver requires ${name} to be set.`);
|
|
104
|
+
}
|
|
105
|
+
return value;
|
|
106
|
+
}
|
|
107
|
+
function reroutableGraphResolver(initial) {
|
|
108
|
+
let current = initial;
|
|
109
|
+
return {
|
|
110
|
+
resolver: {
|
|
111
|
+
ids: initial.ids,
|
|
112
|
+
load: (graphId) => current.load(graphId),
|
|
113
|
+
schemas: async (graphId) => await current.schemas(graphId)
|
|
114
|
+
},
|
|
115
|
+
reroute: (next) => {
|
|
116
|
+
current = next;
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
async function resolveStoreIndex(index, options) {
|
|
121
|
+
if (!index?.embed) return void 0;
|
|
122
|
+
if (typeof index.dims !== "number") {
|
|
123
|
+
throw new RuntimeConfigError(
|
|
124
|
+
`store.index.embed is set but store.index.dims is missing \u2014 set the embedding dimensionality (e.g. 1536 for openai:text-embedding-3-small).`
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
const embed = await resolveEmbed(index.embed, options);
|
|
128
|
+
return { dims: index.dims, fields: index.fields, embed };
|
|
129
|
+
}
|
|
130
|
+
async function buildRuntime(options) {
|
|
131
|
+
const { configPath, importModule, store, queue } = options;
|
|
132
|
+
if (store === "memory" && queue === "memory") {
|
|
133
|
+
const runtime = await loadReloadableInMemoryRuntime(configPath, importModule);
|
|
134
|
+
return {
|
|
135
|
+
deps: runtime.deps,
|
|
136
|
+
cors: runtime.cors,
|
|
137
|
+
reloadGraphs: () => runtime.reloadGraphs(),
|
|
138
|
+
dispose: async () => {
|
|
139
|
+
},
|
|
140
|
+
snapshotState: () => runtime.snapshotState(),
|
|
141
|
+
hydrateState: (snapshot) => runtime.hydrateState(snapshot)
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
const first = await loadConfig({ configPath, importModule });
|
|
145
|
+
const { resolver, reroute } = reroutableGraphResolver(first.graphs);
|
|
146
|
+
const disposers = [];
|
|
147
|
+
const disposeAll = async () => {
|
|
148
|
+
await Promise.allSettled(disposers.map((dispose) => dispose()));
|
|
149
|
+
};
|
|
150
|
+
try {
|
|
151
|
+
const { skeinStore, checkpointer } = await (async () => {
|
|
152
|
+
if (store === "postgres") {
|
|
153
|
+
const databaseUrl = requireEnv("DATABASE_URL", "postgres");
|
|
154
|
+
const index = await resolveStoreIndex(first.config.store?.index, {
|
|
155
|
+
configDir: first.configDir,
|
|
156
|
+
importModule
|
|
157
|
+
});
|
|
158
|
+
const pgStore = await PostgresSkeinStore.connect(databaseUrl, index ? { index } : {});
|
|
159
|
+
disposers.push(() => pgStore.close());
|
|
160
|
+
await pgStore.migrate();
|
|
161
|
+
const saver = PostgresSaver.fromConnString(databaseUrl);
|
|
162
|
+
disposers.push(() => saver.end());
|
|
163
|
+
await saver.setup();
|
|
164
|
+
return { skeinStore: pgStore, checkpointer: saver };
|
|
165
|
+
}
|
|
166
|
+
return { skeinStore: new MemorySkeinStore(), checkpointer: new MemorySaver() };
|
|
167
|
+
})();
|
|
168
|
+
const { runQueue, bus } = (() => {
|
|
169
|
+
if (queue === "redis") {
|
|
170
|
+
const redisUrl = requireEnv("REDIS_URL", "redis");
|
|
171
|
+
const redisQueue = new RedisRunQueue(redisUrl);
|
|
172
|
+
disposers.push(() => redisQueue.dispose());
|
|
173
|
+
const redisBus = new RedisRunEventBus(redisUrl);
|
|
174
|
+
disposers.push(() => redisBus.dispose());
|
|
175
|
+
return { runQueue: redisQueue, bus: redisBus };
|
|
176
|
+
}
|
|
177
|
+
return { runQueue: new MemoryRunQueue(), bus: new MemoryRunEventBus() };
|
|
178
|
+
})();
|
|
179
|
+
const deps = {
|
|
180
|
+
store: skeinStore,
|
|
181
|
+
graphs: resolver,
|
|
182
|
+
queue: runQueue,
|
|
183
|
+
bus,
|
|
184
|
+
checkpointer,
|
|
185
|
+
auth: await loadAuthEngine(first.config.auth, {
|
|
186
|
+
configDir: first.configDir,
|
|
187
|
+
importModule
|
|
188
|
+
})
|
|
189
|
+
};
|
|
190
|
+
return {
|
|
191
|
+
deps,
|
|
192
|
+
cors: corsFromHttpConfig(first.config.http),
|
|
193
|
+
reloadGraphs: async () => {
|
|
194
|
+
reroute((await loadConfig({ configPath, importModule })).graphs);
|
|
195
|
+
},
|
|
196
|
+
dispose: disposeAll
|
|
197
|
+
};
|
|
198
|
+
} catch (error) {
|
|
199
|
+
await disposeAll();
|
|
200
|
+
throw error;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
export {
|
|
204
|
+
RuntimeConfigError,
|
|
205
|
+
buildRuntime,
|
|
206
|
+
resolveEmbed
|
|
207
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@skein-js/runtime",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Assembles skein-js ProtocolDeps from langgraph.json + selected drivers (memory/Postgres/Redis).",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": "Maina Wycliffe <wmmaina7@gmail.com>",
|
|
7
|
+
"homepage": "https://github.com/mainawycliffe/skein/tree/main/packages/runtime#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/mainawycliffe/skein.git",
|
|
11
|
+
"directory": "packages/runtime"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/mainawycliffe/skein/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"langgraph",
|
|
18
|
+
"agent-protocol",
|
|
19
|
+
"runtime",
|
|
20
|
+
"postgres",
|
|
21
|
+
"redis"
|
|
22
|
+
],
|
|
23
|
+
"type": "module",
|
|
24
|
+
"main": "./dist/index.js",
|
|
25
|
+
"module": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"import": "./dist/index.js"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist",
|
|
35
|
+
"LICENSE",
|
|
36
|
+
"README.md"
|
|
37
|
+
],
|
|
38
|
+
"sideEffects": false,
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=20"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"cors": "^2.8.5",
|
|
44
|
+
"@skein-js/agent-protocol": "0.1.0",
|
|
45
|
+
"@skein-js/express": "0.1.0",
|
|
46
|
+
"@skein-js/redis": "0.1.0",
|
|
47
|
+
"@skein-js/storage-memory": "0.1.0",
|
|
48
|
+
"@skein-js/storage-postgres": "0.1.0",
|
|
49
|
+
"@skein-js/config": "0.1.0"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"@langchain/langgraph": "^1.4.0",
|
|
53
|
+
"@langchain/langgraph-checkpoint-postgres": "^1.0.4"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@langchain/core": "^1.2.0",
|
|
57
|
+
"@langchain/langgraph": "^1.4.0",
|
|
58
|
+
"@langchain/langgraph-checkpoint-postgres": "^1.0.4",
|
|
59
|
+
"@types/cors": "^2.8.17",
|
|
60
|
+
"@skein-js/test-support": "0.0.0"
|
|
61
|
+
},
|
|
62
|
+
"publishConfig": {
|
|
63
|
+
"access": "public"
|
|
64
|
+
}
|
|
65
|
+
}
|