@skein-js/storage-memory 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 +59 -0
- package/dist/index.js +340 -0
- package/package.json +50 -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/storage-memory
|
|
2
|
+
|
|
3
|
+
> In-memory SkeinStore + queue driver for development and tests.
|
|
4
|
+
|
|
5
|
+
Part of **[skein-js](https://github.com/mainawycliffe/skein)** — a TypeScript [Agent Protocol](https://github.com/langchain-ai/agent-protocol) server for [LangGraph.js](https://github.com/langchain-ai/langgraphjs), and a drop-in replacement for the LangGraph CLI.
|
|
6
|
+
|
|
7
|
+
**Status:** 🚧 Pre-alpha — implemented and passing the shared conformance suite.
|
|
8
|
+
|
|
9
|
+
## What it does
|
|
10
|
+
|
|
11
|
+
Zero-dependency, in-process drivers that power `skein dev`:
|
|
12
|
+
|
|
13
|
+
- **`MemorySkeinStore`** — `SkeinStore` over plain `Map`s (assistants / threads / runs / store items), with the run-concurrency guard, thread→runs cascade delete, and naive prefix/substring store search. Every read and write deep-clones at the boundary — like a real serializing driver — so callers can't mutate stored rows or corrupt the store through a retained input.
|
|
14
|
+
- **`MemoryRunQueue`** — a single-process FIFO of background runs.
|
|
15
|
+
- **`MemoryRunEventBus`** — buffered run-frame pub/sub with replay (`afterSeq`) and live-tail, so a client can join a run's stream late or reconnect.
|
|
16
|
+
|
|
17
|
+
Validated against the shared `SkeinStore` conformance suite, so it behaves identically to the Postgres driver.
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { MemorySkeinStore, MemoryRunQueue, MemoryRunEventBus } from "@skein-js/storage-memory";
|
|
23
|
+
|
|
24
|
+
const store = new MemorySkeinStore();
|
|
25
|
+
const thread = await store.threads.create({ metadata: { user: "a" } });
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Reuse
|
|
29
|
+
|
|
30
|
+
Pairs with `MemorySaver` from `@langchain/langgraph-checkpoint` for graph checkpoints — it stores only Agent Protocol _resources_, never graph state.
|
|
31
|
+
|
|
32
|
+
## Install
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
pnpm add @skein-js/storage-memory
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Learn more
|
|
39
|
+
|
|
40
|
+
- [skein-js overview](../../docs/index.md)
|
|
41
|
+
- [Reuse-first architecture](../../docs/reuse.md)
|
|
42
|
+
- [Roadmap](../../docs/roadmap.md)
|
|
43
|
+
|
|
44
|
+
## License
|
|
45
|
+
|
|
46
|
+
[Apache-2.0](../../LICENSE)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { SkeinStore, AssistantRepo, ThreadRepo, RunRepo, StoreRepo, Assistant, Thread, Run, RunKwargs, Item, RunEventBus, RunFrame, RunQueue, QueuedRun, RunProcessor, RunConsumerOptions, RunConsumer } from '@skein-js/core';
|
|
2
|
+
|
|
3
|
+
/** In-process SkeinStore for development and tests. */
|
|
4
|
+
declare class MemorySkeinStore implements SkeinStore {
|
|
5
|
+
#private;
|
|
6
|
+
readonly assistants: AssistantRepo;
|
|
7
|
+
readonly threads: ThreadRepo;
|
|
8
|
+
readonly runs: RunRepo;
|
|
9
|
+
readonly store: StoreRepo;
|
|
10
|
+
/**
|
|
11
|
+
* A JSON-serializable copy of every row, so `skein dev` can persist dev state to disk and
|
|
12
|
+
* restore it on the next boot. Rows are deep-cloned at the boundary, exactly like the repo reads.
|
|
13
|
+
*/
|
|
14
|
+
snapshot(): MemoryStoreSnapshot;
|
|
15
|
+
/** Replace all rows with a {@link snapshot}. Used by `skein dev` to restore persisted dev state. */
|
|
16
|
+
hydrate(snapshot: MemoryStoreSnapshot): void;
|
|
17
|
+
}
|
|
18
|
+
/** A JSON-serializable copy of a {@link MemorySkeinStore}'s rows. */
|
|
19
|
+
interface MemoryStoreSnapshot {
|
|
20
|
+
assistants: [string, Assistant][];
|
|
21
|
+
threads: [string, Thread][];
|
|
22
|
+
runs: [string, Run][];
|
|
23
|
+
runKwargs: [string, RunKwargs][];
|
|
24
|
+
items: [string, Item][];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* In-memory FIFO of background runs, drained by a single-process consumer — all `skein dev` needs.
|
|
29
|
+
* `@skein-js/redis` (BullMQ) provides the durable, cross-instance version; the run worker talks
|
|
30
|
+
* only to the `RunQueue` interface, so it is unaware which one it has.
|
|
31
|
+
*/
|
|
32
|
+
declare class MemoryRunQueue implements RunQueue {
|
|
33
|
+
#private;
|
|
34
|
+
enqueue(run: QueuedRun): Promise<void>;
|
|
35
|
+
consume(process: RunProcessor, options?: RunConsumerOptions): RunConsumer;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* In-memory fan-out of run frames. Each frame is buffered, so a subscriber that joins late (or
|
|
39
|
+
* reconnects with an `afterSeq`) replays what it missed and then live-tails until the run
|
|
40
|
+
* closes. Buffering also means publish never blocks on a slow consumer.
|
|
41
|
+
*
|
|
42
|
+
* A closed run's frame buffer is retained for late-join replay, but only for the most recent
|
|
43
|
+
* `maxRetainedRuns` closed runs (LRU). Beyond that the buffer is dropped and the channel is kept as
|
|
44
|
+
* a lightweight *closed tombstone* — so a long-lived process can't grow its frame memory without
|
|
45
|
+
* bound, yet a late join to an evicted run still completes at once (empty) instead of hanging.
|
|
46
|
+
* Active (unclosed) runs and runs with live subscribers keep their frames. `@skein-js/redis` bounds
|
|
47
|
+
* this with a real TTL in production.
|
|
48
|
+
*/
|
|
49
|
+
declare class MemoryRunEventBus implements RunEventBus {
|
|
50
|
+
#private;
|
|
51
|
+
constructor(options?: {
|
|
52
|
+
maxRetainedRuns?: number;
|
|
53
|
+
});
|
|
54
|
+
publish(runId: string, frame: RunFrame): Promise<void>;
|
|
55
|
+
close(runId: string): Promise<void>;
|
|
56
|
+
subscribe(runId: string, afterSeq?: number): AsyncIterable<RunFrame>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export { MemoryRunEventBus, MemoryRunQueue, MemorySkeinStore, type MemoryStoreSnapshot };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
// src/memory-skein-store.ts
|
|
2
|
+
import { randomUUID } from "crypto";
|
|
3
|
+
import {
|
|
4
|
+
isTerminalRunStatus,
|
|
5
|
+
SkeinHttpError
|
|
6
|
+
} from "@skein-js/core";
|
|
7
|
+
var nowIso = () => (/* @__PURE__ */ new Date()).toISOString();
|
|
8
|
+
var clone = (value) => structuredClone(value);
|
|
9
|
+
function readOne(map, id) {
|
|
10
|
+
const found = map.get(id);
|
|
11
|
+
return found ? clone(found) : null;
|
|
12
|
+
}
|
|
13
|
+
function readAll(map) {
|
|
14
|
+
return [...map.values()].map((row) => clone(row));
|
|
15
|
+
}
|
|
16
|
+
function write(map, id, row) {
|
|
17
|
+
const stored = clone(row);
|
|
18
|
+
map.set(id, stored);
|
|
19
|
+
return clone(stored);
|
|
20
|
+
}
|
|
21
|
+
function hasPrefix(namespace, prefix) {
|
|
22
|
+
if (!prefix || prefix.length === 0) return true;
|
|
23
|
+
if (prefix.length > namespace.length) return false;
|
|
24
|
+
return prefix.every((segment, i) => namespace[i] === segment);
|
|
25
|
+
}
|
|
26
|
+
function itemKey(namespace, key) {
|
|
27
|
+
return JSON.stringify([namespace, key]);
|
|
28
|
+
}
|
|
29
|
+
var MemorySkeinStore = class {
|
|
30
|
+
#assistants = /* @__PURE__ */ new Map();
|
|
31
|
+
#threads = /* @__PURE__ */ new Map();
|
|
32
|
+
#runs = /* @__PURE__ */ new Map();
|
|
33
|
+
// The opaque execution payload lives beside the run row (it is not part of the wire `Run`).
|
|
34
|
+
#runKwargs = /* @__PURE__ */ new Map();
|
|
35
|
+
#items = /* @__PURE__ */ new Map();
|
|
36
|
+
assistants = {
|
|
37
|
+
list: async () => readAll(this.#assistants),
|
|
38
|
+
get: async (assistantId) => readOne(this.#assistants, assistantId),
|
|
39
|
+
create: async (input) => {
|
|
40
|
+
const at = nowIso();
|
|
41
|
+
const assistant = {
|
|
42
|
+
assistant_id: input.assistant_id ?? randomUUID(),
|
|
43
|
+
graph_id: input.graph_id,
|
|
44
|
+
config: input.config ?? {},
|
|
45
|
+
context: input.context ?? {},
|
|
46
|
+
created_at: at,
|
|
47
|
+
updated_at: at,
|
|
48
|
+
metadata: input.metadata ?? {},
|
|
49
|
+
version: 1,
|
|
50
|
+
name: input.name ?? input.graph_id,
|
|
51
|
+
description: input.description
|
|
52
|
+
};
|
|
53
|
+
return write(this.#assistants, assistant.assistant_id, assistant);
|
|
54
|
+
},
|
|
55
|
+
delete: async (assistantId) => {
|
|
56
|
+
this.#assistants.delete(assistantId);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
threads = {
|
|
60
|
+
list: async () => readAll(this.#threads),
|
|
61
|
+
get: async (threadId) => readOne(this.#threads, threadId),
|
|
62
|
+
create: async (input) => {
|
|
63
|
+
const at = nowIso();
|
|
64
|
+
const thread = {
|
|
65
|
+
thread_id: input?.thread_id ?? randomUUID(),
|
|
66
|
+
created_at: at,
|
|
67
|
+
updated_at: at,
|
|
68
|
+
state_updated_at: at,
|
|
69
|
+
metadata: input?.metadata ?? {},
|
|
70
|
+
status: input?.status ?? "idle",
|
|
71
|
+
values: {},
|
|
72
|
+
interrupts: {}
|
|
73
|
+
};
|
|
74
|
+
return write(this.#threads, thread.thread_id, thread);
|
|
75
|
+
},
|
|
76
|
+
update: async (threadId, patch) => {
|
|
77
|
+
const existing = this.#threads.get(threadId);
|
|
78
|
+
if (!existing) throw SkeinHttpError.notFound(`Thread "${threadId}" not found.`);
|
|
79
|
+
const at = nowIso();
|
|
80
|
+
const updated = {
|
|
81
|
+
...existing,
|
|
82
|
+
metadata: patch.metadata ?? existing.metadata,
|
|
83
|
+
status: patch.status ?? existing.status,
|
|
84
|
+
values: patch.values ?? existing.values,
|
|
85
|
+
interrupts: patch.interrupts ?? existing.interrupts,
|
|
86
|
+
updated_at: at,
|
|
87
|
+
state_updated_at: patch.values !== void 0 ? at : existing.state_updated_at
|
|
88
|
+
};
|
|
89
|
+
return write(this.#threads, threadId, updated);
|
|
90
|
+
},
|
|
91
|
+
delete: async (threadId) => {
|
|
92
|
+
this.#threads.delete(threadId);
|
|
93
|
+
for (const [runId, run] of this.#runs) {
|
|
94
|
+
if (run.thread_id === threadId) {
|
|
95
|
+
this.#runs.delete(runId);
|
|
96
|
+
this.#runKwargs.delete(runId);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
runs = {
|
|
102
|
+
get: async (runId) => readOne(this.#runs, runId),
|
|
103
|
+
listByThread: async (threadId) => readAll(this.#runs).filter((run) => run.thread_id === threadId),
|
|
104
|
+
create: async (input) => {
|
|
105
|
+
const at = nowIso();
|
|
106
|
+
const run = {
|
|
107
|
+
run_id: input.run_id ?? randomUUID(),
|
|
108
|
+
thread_id: input.thread_id,
|
|
109
|
+
assistant_id: input.assistant_id,
|
|
110
|
+
created_at: at,
|
|
111
|
+
updated_at: at,
|
|
112
|
+
status: input.status ?? "pending",
|
|
113
|
+
metadata: input.metadata ?? {},
|
|
114
|
+
multitask_strategy: input.multitask_strategy ?? null
|
|
115
|
+
};
|
|
116
|
+
const stored = write(this.#runs, run.run_id, run);
|
|
117
|
+
if (input.kwargs) this.#runKwargs.set(run.run_id, clone(input.kwargs));
|
|
118
|
+
return stored;
|
|
119
|
+
},
|
|
120
|
+
setStatus: async (runId, status) => {
|
|
121
|
+
const existing = this.#runs.get(runId);
|
|
122
|
+
if (!existing) throw SkeinHttpError.notFound(`Run "${runId}" not found.`);
|
|
123
|
+
return write(this.#runs, runId, { ...existing, status, updated_at: nowIso() });
|
|
124
|
+
},
|
|
125
|
+
delete: async (runId) => {
|
|
126
|
+
this.#runs.delete(runId);
|
|
127
|
+
this.#runKwargs.delete(runId);
|
|
128
|
+
},
|
|
129
|
+
getKwargs: async (runId) => {
|
|
130
|
+
const found = this.#runKwargs.get(runId);
|
|
131
|
+
return found ? clone(found) : null;
|
|
132
|
+
},
|
|
133
|
+
hasActiveRun: async (threadId) => {
|
|
134
|
+
for (const run of this.#runs.values()) {
|
|
135
|
+
if (run.thread_id === threadId && !isTerminalRunStatus(run.status)) return true;
|
|
136
|
+
}
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
store = {
|
|
141
|
+
get: async (namespace, key) => {
|
|
142
|
+
const found = this.#items.get(itemKey(namespace, key));
|
|
143
|
+
return found ? clone(found) : null;
|
|
144
|
+
},
|
|
145
|
+
put: async (namespace, key, value) => {
|
|
146
|
+
const id = itemKey(namespace, key);
|
|
147
|
+
const at = nowIso();
|
|
148
|
+
const existing = this.#items.get(id);
|
|
149
|
+
const item = {
|
|
150
|
+
namespace: [...namespace],
|
|
151
|
+
key,
|
|
152
|
+
value,
|
|
153
|
+
createdAt: existing?.createdAt ?? at,
|
|
154
|
+
updatedAt: at
|
|
155
|
+
};
|
|
156
|
+
const stored = clone(item);
|
|
157
|
+
this.#items.set(id, stored);
|
|
158
|
+
return clone(stored);
|
|
159
|
+
},
|
|
160
|
+
delete: async (namespace, key) => {
|
|
161
|
+
this.#items.delete(itemKey(namespace, key));
|
|
162
|
+
},
|
|
163
|
+
search: async (query) => {
|
|
164
|
+
const needle = query.query?.toLowerCase();
|
|
165
|
+
const matches = [...this.#items.values()].filter((item) => hasPrefix(item.namespace, query.prefix)).filter(
|
|
166
|
+
(item) => needle ? JSON.stringify(item.value).toLowerCase().includes(needle) : true
|
|
167
|
+
).map((item) => {
|
|
168
|
+
const result = clone(item);
|
|
169
|
+
if (needle) result.score = 1;
|
|
170
|
+
return result;
|
|
171
|
+
});
|
|
172
|
+
const offset = query.offset ?? 0;
|
|
173
|
+
return matches.slice(offset, query.limit === void 0 ? void 0 : offset + query.limit);
|
|
174
|
+
},
|
|
175
|
+
listNamespaces: async (prefix) => {
|
|
176
|
+
const seen = /* @__PURE__ */ new Map();
|
|
177
|
+
for (const item of this.#items.values()) {
|
|
178
|
+
if (hasPrefix(item.namespace, prefix)) {
|
|
179
|
+
seen.set(JSON.stringify(item.namespace), item.namespace);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return [...seen.values()].map((namespace) => [...namespace]);
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
/**
|
|
186
|
+
* A JSON-serializable copy of every row, so `skein dev` can persist dev state to disk and
|
|
187
|
+
* restore it on the next boot. Rows are deep-cloned at the boundary, exactly like the repo reads.
|
|
188
|
+
*/
|
|
189
|
+
snapshot() {
|
|
190
|
+
const entries = (map) => [...map.entries()].map(([id, row]) => [id, clone(row)]);
|
|
191
|
+
return {
|
|
192
|
+
assistants: entries(this.#assistants),
|
|
193
|
+
threads: entries(this.#threads),
|
|
194
|
+
runs: entries(this.#runs),
|
|
195
|
+
runKwargs: entries(this.#runKwargs),
|
|
196
|
+
items: entries(this.#items)
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
/** Replace all rows with a {@link snapshot}. Used by `skein dev` to restore persisted dev state. */
|
|
200
|
+
hydrate(snapshot) {
|
|
201
|
+
const fill = (map, rows) => {
|
|
202
|
+
map.clear();
|
|
203
|
+
for (const [id, row] of rows) map.set(id, clone(row));
|
|
204
|
+
};
|
|
205
|
+
fill(this.#assistants, snapshot.assistants);
|
|
206
|
+
fill(this.#threads, snapshot.threads);
|
|
207
|
+
fill(this.#runs, snapshot.runs);
|
|
208
|
+
fill(this.#runKwargs, snapshot.runKwargs);
|
|
209
|
+
fill(this.#items, snapshot.items);
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
// src/memory-queue.ts
|
|
214
|
+
var MemoryRunQueue = class {
|
|
215
|
+
#items = [];
|
|
216
|
+
/** Resolvers of consumers parked waiting for the next run. */
|
|
217
|
+
#waiters = [];
|
|
218
|
+
async enqueue(run) {
|
|
219
|
+
this.#items.push({ ...run });
|
|
220
|
+
this.#waiters.shift()?.();
|
|
221
|
+
}
|
|
222
|
+
consume(process, options = {}) {
|
|
223
|
+
return new MemoryRunConsumer(this.#items, this.#waiters, process, options.concurrency ?? 1);
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
var MemoryRunConsumer = class {
|
|
227
|
+
constructor(items, waiters, process, concurrency) {
|
|
228
|
+
this.items = items;
|
|
229
|
+
this.waiters = waiters;
|
|
230
|
+
this.process = process;
|
|
231
|
+
this.concurrency = concurrency;
|
|
232
|
+
this.#loop = this.#run();
|
|
233
|
+
}
|
|
234
|
+
items;
|
|
235
|
+
waiters;
|
|
236
|
+
process;
|
|
237
|
+
concurrency;
|
|
238
|
+
#running = true;
|
|
239
|
+
#inFlight = /* @__PURE__ */ new Set();
|
|
240
|
+
#loop;
|
|
241
|
+
async #run() {
|
|
242
|
+
while (this.#running) {
|
|
243
|
+
if (this.#inFlight.size >= this.concurrency) {
|
|
244
|
+
await Promise.race(this.#inFlight);
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
const next = this.items.shift();
|
|
248
|
+
if (!next) {
|
|
249
|
+
if (!this.#running) break;
|
|
250
|
+
await new Promise((resolve) => this.waiters.push(resolve));
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
const task = this.process(next).catch(() => void 0).finally(() => this.#inFlight.delete(task));
|
|
254
|
+
this.#inFlight.add(task);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
async close(force = false) {
|
|
258
|
+
this.#running = false;
|
|
259
|
+
for (const wake of this.waiters.splice(0)) wake();
|
|
260
|
+
if (force) return;
|
|
261
|
+
await this.#loop;
|
|
262
|
+
await Promise.all(this.#inFlight);
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
var DEFAULT_MAX_RETAINED_RUNS = 1e3;
|
|
266
|
+
var MemoryRunEventBus = class {
|
|
267
|
+
#channels = /* @__PURE__ */ new Map();
|
|
268
|
+
/** Closed runs in close order (oldest first), for LRU eviction of retained buffers. */
|
|
269
|
+
#closedOrder = [];
|
|
270
|
+
#maxRetainedRuns;
|
|
271
|
+
constructor(options = {}) {
|
|
272
|
+
this.#maxRetainedRuns = options.maxRetainedRuns ?? DEFAULT_MAX_RETAINED_RUNS;
|
|
273
|
+
}
|
|
274
|
+
#channel(runId) {
|
|
275
|
+
let channel = this.#channels.get(runId);
|
|
276
|
+
if (!channel) {
|
|
277
|
+
channel = { frames: [], closed: false, waiters: [], subscribers: 0 };
|
|
278
|
+
this.#channels.set(runId, channel);
|
|
279
|
+
}
|
|
280
|
+
return channel;
|
|
281
|
+
}
|
|
282
|
+
#wake(channel) {
|
|
283
|
+
const waiters = channel.waiters.splice(0);
|
|
284
|
+
for (const resolve of waiters) resolve();
|
|
285
|
+
}
|
|
286
|
+
// Free the frame buffers of the oldest retained closed runs beyond the cap, leaving a closed
|
|
287
|
+
// tombstone so a late join replays nothing and completes at once (rather than hanging on a
|
|
288
|
+
// recreated open channel). A channel with live subscribers is mid-replay: keep its frames and
|
|
289
|
+
// re-queue it so it's reconsidered after the reader detaches.
|
|
290
|
+
#evictClosedBeyondCap() {
|
|
291
|
+
let scanned = 0;
|
|
292
|
+
while (this.#closedOrder.length > this.#maxRetainedRuns && scanned < this.#closedOrder.length) {
|
|
293
|
+
scanned += 1;
|
|
294
|
+
const runId = this.#closedOrder.shift();
|
|
295
|
+
if (runId === void 0) break;
|
|
296
|
+
const channel = this.#channels.get(runId);
|
|
297
|
+
if (!channel) continue;
|
|
298
|
+
if (channel.subscribers > 0) {
|
|
299
|
+
this.#closedOrder.push(runId);
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
channel.frames = [];
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
async publish(runId, frame) {
|
|
306
|
+
const channel = this.#channel(runId);
|
|
307
|
+
channel.frames.push(frame);
|
|
308
|
+
this.#wake(channel);
|
|
309
|
+
}
|
|
310
|
+
async close(runId) {
|
|
311
|
+
const channel = this.#channel(runId);
|
|
312
|
+
channel.closed = true;
|
|
313
|
+
this.#wake(channel);
|
|
314
|
+
this.#closedOrder.push(runId);
|
|
315
|
+
this.#evictClosedBeyondCap();
|
|
316
|
+
}
|
|
317
|
+
async *subscribe(runId, afterSeq = 0) {
|
|
318
|
+
const channel = this.#channel(runId);
|
|
319
|
+
channel.subscribers += 1;
|
|
320
|
+
try {
|
|
321
|
+
let index = 0;
|
|
322
|
+
for (; ; ) {
|
|
323
|
+
while (index < channel.frames.length) {
|
|
324
|
+
const frame = channel.frames[index];
|
|
325
|
+
index += 1;
|
|
326
|
+
if (frame && frame.seq > afterSeq) yield frame;
|
|
327
|
+
}
|
|
328
|
+
if (channel.closed) return;
|
|
329
|
+
await new Promise((resolve) => channel.waiters.push(resolve));
|
|
330
|
+
}
|
|
331
|
+
} finally {
|
|
332
|
+
channel.subscribers -= 1;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
export {
|
|
337
|
+
MemoryRunEventBus,
|
|
338
|
+
MemoryRunQueue,
|
|
339
|
+
MemorySkeinStore
|
|
340
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@skein-js/storage-memory",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "In-memory SkeinStore + queue driver for development and tests.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": "Maina Wycliffe <wmmaina7@gmail.com>",
|
|
7
|
+
"homepage": "https://github.com/mainawycliffe/skein/tree/main/packages/storage-memory#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/mainawycliffe/skein.git",
|
|
11
|
+
"directory": "packages/storage-memory"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/mainawycliffe/skein/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"langgraph",
|
|
18
|
+
"agent-protocol",
|
|
19
|
+
"storage",
|
|
20
|
+
"in-memory"
|
|
21
|
+
],
|
|
22
|
+
"type": "module",
|
|
23
|
+
"main": "./dist/index.js",
|
|
24
|
+
"module": "./dist/index.js",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"import": "./dist/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"LICENSE",
|
|
35
|
+
"README.md"
|
|
36
|
+
],
|
|
37
|
+
"sideEffects": false,
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=20"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@skein-js/core": "0.1.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@skein-js/test-support": "0.0.0"
|
|
46
|
+
},
|
|
47
|
+
"publishConfig": {
|
|
48
|
+
"access": "public"
|
|
49
|
+
}
|
|
50
|
+
}
|