thingd 0.0.0-development
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 +201 -0
- package/README.md +123 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/stores/in-memory-thing-store.d.ts +24 -0
- package/dist/stores/in-memory-thing-store.d.ts.map +1 -0
- package/dist/stores/in-memory-thing-store.js +219 -0
- package/dist/stores/native-thing-store.d.ts +19 -0
- package/dist/stores/native-thing-store.d.ts.map +1 -0
- package/dist/stores/native-thing-store.js +162 -0
- package/dist/stores/remote-thing-store.d.ts +27 -0
- package/dist/stores/remote-thing-store.d.ts.map +1 -0
- package/dist/stores/remote-thing-store.js +131 -0
- package/dist/thingd.d.ts +28 -0
- package/dist/thingd.d.ts.map +1 -0
- package/dist/thingd.js +84 -0
- package/dist/types.d.ts +100 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# thingd
|
|
2
|
+
|
|
3
|
+
Object-shaped local memory for AI-native apps. SQLite-simple, MCP-native, with search, events, and durable queues.
|
|
4
|
+
|
|
5
|
+
This is the Node.js SDK for `thingd`. It exposes the intended object/event/queue abstractions with an in-memory store by default. Inside the `thingd` repository it can also use the private Rust-backed native driver for local SQLite persistence, and a remote driver for sidecar/server mode.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
You can install the published SDK from npm:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install thingd
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Example Usage
|
|
16
|
+
|
|
17
|
+
### 1. Connecting and Basic Operations
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { ThingD } from "thingd";
|
|
21
|
+
|
|
22
|
+
// Open the default in-memory store (for testing)
|
|
23
|
+
const db = await ThingD.open();
|
|
24
|
+
|
|
25
|
+
// Store an object
|
|
26
|
+
await db.put("decisions", {
|
|
27
|
+
id: "rust-core",
|
|
28
|
+
text: "Use Rust for the core engine and TypeScript for the developer API.",
|
|
29
|
+
project: "thingd",
|
|
30
|
+
confidence: 0.9,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Retrieve an object
|
|
34
|
+
const decision = await db.get("decisions", "rust-core");
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### 2. Events and Timelines
|
|
38
|
+
|
|
39
|
+
Every meaningful mutation can produce an event. Events make memory easier for humans and agents to understand.
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
await db.events.append("project:thingd", {
|
|
43
|
+
type: "decision.made",
|
|
44
|
+
text: "thingd will be object-shaped and MCP-native.",
|
|
45
|
+
actor: "sayan",
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const events = await db.events.list("project:thingd");
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### 3. Durable Queues
|
|
52
|
+
|
|
53
|
+
`thingd` includes queue primitives because AI apps constantly need background work (chunking, embeddings, summarization, etc).
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
// Enqueue a job
|
|
57
|
+
await db.queue("embed").push({
|
|
58
|
+
object: "decisions/rust-core",
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// Claim a job with a 30-second visibility lease
|
|
62
|
+
const job = await db.queue("embed").claim({
|
|
63
|
+
leaseMs: 30_000,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
if (job) {
|
|
67
|
+
try {
|
|
68
|
+
// Process the job
|
|
69
|
+
await embedDocument(job.payload.object);
|
|
70
|
+
// Mark as complete
|
|
71
|
+
await db.queue("embed").ack(job.id);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
// Return to the queue for a retry
|
|
74
|
+
await db.queue("embed").nack(job.id, {
|
|
75
|
+
delayMs: 5_000,
|
|
76
|
+
error: String(error),
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### 4. Search (Planned / Experimental)
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
const hits = await db.search("why did we choose rust?", {
|
|
86
|
+
collections: ["decisions"],
|
|
87
|
+
limit: 5,
|
|
88
|
+
});
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Running against a sidecar / Docker Runtime
|
|
92
|
+
|
|
93
|
+
To use a running `thingd` sidecar or Docker runtime instead of the local memory, you can connect over HTTP MCP:
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
THINGD_URL=http://127.0.0.1:8757
|
|
97
|
+
THINGD_AUTH_TOKEN=change-me
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
import { ThingD } from "thingd";
|
|
102
|
+
|
|
103
|
+
// Automatically uses remote driver if THINGD_URL is set
|
|
104
|
+
const db = await ThingD.open();
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Or configure the remote driver explicitly:
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
const db = await ThingD.open({
|
|
111
|
+
url: "http://127.0.0.1:8757/mcp",
|
|
112
|
+
driver: "remote",
|
|
113
|
+
authToken: "change-me",
|
|
114
|
+
});
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Remote mode uses the exact same SDK methods, but talks to the sidecar over Streamable HTTP MCP.
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
For full architecture details, local SQLite native drivers, and MCP server documentation, visit the [thingd GitHub repository](https://github.com/sayanmohsin/thingd).
|
|
122
|
+
|
|
123
|
+
`thingd` is open source under the Apache-2.0 license.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type { ThingDDriver, ThingDOpenConfig, ThingDOpenOptions } from "./thingd.js";
|
|
2
|
+
export { ThingD } from "./thingd.js";
|
|
3
|
+
export { InMemoryThingStore } from "./stores/in-memory-thing-store.js";
|
|
4
|
+
export { NativeThingStore } from "./stores/native-thing-store.js";
|
|
5
|
+
export type { RemoteThingStoreOptions } from "./stores/remote-thing-store.js";
|
|
6
|
+
export { RemoteThingStore } from "./stores/remote-thing-store.js";
|
|
7
|
+
export type { ThingDeleteResult, MemoryEvent, MemoryObject, MemoryQueue, MemorySearchOptions, MemorySearchResult, ThingStore, QueueClaimOptions, QueueJob, QueueJobOptions, QueueJobPayload, QueueJobResult, QueueJobStatus, QueueNackOptions, StoredMemoryEvent, StoredMemoryObject, } from "./types.js";
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,YAAY,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AACrF,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,kBAAkB,EAAE,MAAM,mCAAmC,CAAC;AACvE,OAAO,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;AAClE,YAAY,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAC9E,OAAO,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;AAClE,YAAY,EACV,iBAAiB,EACjB,WAAW,EACX,YAAY,EACZ,WAAW,EACX,mBAAmB,EACnB,kBAAkB,EAClB,UAAU,EACV,iBAAiB,EACjB,QAAQ,EACR,eAAe,EACf,eAAe,EACf,cAAc,EACd,cAAc,EACd,gBAAgB,EAChB,iBAAiB,EACjB,kBAAkB,GACnB,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ThingDeleteResult, MemoryEvent, MemoryObject, MemorySearchOptions, MemorySearchResult, ThingStore, QueueClaimOptions, QueueJob, QueueJobOptions, QueueJobPayload, QueueJobResult, QueueNackOptions, StoredMemoryEvent, StoredMemoryObject } from "../types.js";
|
|
2
|
+
export declare class InMemoryThingStore implements ThingStore {
|
|
3
|
+
private readonly collections;
|
|
4
|
+
private readonly events;
|
|
5
|
+
private readonly queues;
|
|
6
|
+
put(collection: string, object: MemoryObject): Promise<StoredMemoryObject>;
|
|
7
|
+
get(collection: string, id: string): Promise<StoredMemoryObject | null>;
|
|
8
|
+
delete(collection: string, id: string): Promise<ThingDeleteResult>;
|
|
9
|
+
appendEvent(stream: string, event: MemoryEvent): Promise<StoredMemoryEvent>;
|
|
10
|
+
listEvents(stream?: string): Promise<StoredMemoryEvent[]>;
|
|
11
|
+
pushJob(queue: string, payload: QueueJobPayload, options?: QueueJobOptions): Promise<QueueJob>;
|
|
12
|
+
claimJob(queue: string, options?: QueueClaimOptions): Promise<QueueJob | null>;
|
|
13
|
+
ackJob(queue: string, jobId: string): Promise<QueueJobResult>;
|
|
14
|
+
nackJob(queue: string, jobId: string, options?: QueueNackOptions): Promise<QueueJobResult>;
|
|
15
|
+
listJobs(queue: string): Promise<QueueJob[]>;
|
|
16
|
+
listDeadJobs(queue: string): Promise<QueueJob[]>;
|
|
17
|
+
search(query: string, options?: MemorySearchOptions): Promise<MemorySearchResult[]>;
|
|
18
|
+
private getCollection;
|
|
19
|
+
private getQueue;
|
|
20
|
+
private findJob;
|
|
21
|
+
private releaseExpiredLeases;
|
|
22
|
+
private cloneJob;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=in-memory-thing-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"in-memory-thing-store.d.ts","sourceRoot":"","sources":["../../src/stores/in-memory-thing-store.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,iBAAiB,EACjB,WAAW,EACX,YAAY,EACZ,mBAAmB,EACnB,kBAAkB,EAClB,UAAU,EACV,iBAAiB,EACjB,QAAQ,EACR,eAAe,EACf,eAAe,EACf,cAAc,EACd,gBAAgB,EAChB,iBAAiB,EACjB,kBAAkB,EACnB,MAAM,aAAa,CAAC;AAIrB,qBAAa,kBAAmB,YAAW,UAAU;IACnD,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAsD;IAClF,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA2B;IAClD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAiC;IAElD,GAAG,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAiB1E,GAAG,CAAC,UAAU,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,GAAG,IAAI,CAAC;IAIvE,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAMlE,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAY3E,UAAU,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,EAAE,CAAC;IAQzD,OAAO,CACX,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,eAAe,EACxB,OAAO,GAAE,eAAoB,GAC5B,OAAO,CAAC,QAAQ,CAAC;IAuBd,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,GAAE,iBAAsB,GAAG,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC;IAwBlF,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC;IAiC7D,OAAO,CACX,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,gBAAqB,GAC7B,OAAO,CAAC,cAAc,CAAC;IA0CpB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC;IAI5C,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC;IAMhD,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,GAAE,mBAAwB,GAAG,OAAO,CAAC,kBAAkB,EAAE,CAAC;IAyC7F,OAAO,CAAC,aAAa;IAMrB,OAAO,CAAC,QAAQ;IAMhB,OAAO,CAAC,OAAO;IAIf,OAAO,CAAC,oBAAoB;IAY5B,OAAO,CAAC,QAAQ;CAQjB"}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
const DEFAULT_LEASE_MS = 30_000;
|
|
3
|
+
export class InMemoryThingStore {
|
|
4
|
+
collections = new Map();
|
|
5
|
+
events = [];
|
|
6
|
+
queues = new Map();
|
|
7
|
+
async put(collection, object) {
|
|
8
|
+
const records = this.getCollection(collection);
|
|
9
|
+
const now = new Date().toISOString();
|
|
10
|
+
const existing = records.get(object.id);
|
|
11
|
+
const record = {
|
|
12
|
+
...object,
|
|
13
|
+
id: object.id,
|
|
14
|
+
collection,
|
|
15
|
+
createdAt: existing?.createdAt ?? now,
|
|
16
|
+
updatedAt: now,
|
|
17
|
+
version: (existing?.version ?? 0) + 1,
|
|
18
|
+
};
|
|
19
|
+
records.set(object.id, record);
|
|
20
|
+
return record;
|
|
21
|
+
}
|
|
22
|
+
async get(collection, id) {
|
|
23
|
+
return this.collections.get(collection)?.get(id) ?? null;
|
|
24
|
+
}
|
|
25
|
+
async delete(collection, id) {
|
|
26
|
+
return {
|
|
27
|
+
deleted: this.collections.get(collection)?.delete(id) ?? false,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
async appendEvent(stream, event) {
|
|
31
|
+
const record = {
|
|
32
|
+
...event,
|
|
33
|
+
id: randomUUID(),
|
|
34
|
+
stream,
|
|
35
|
+
createdAt: new Date().toISOString(),
|
|
36
|
+
};
|
|
37
|
+
this.events.push(record);
|
|
38
|
+
return record;
|
|
39
|
+
}
|
|
40
|
+
async listEvents(stream) {
|
|
41
|
+
if (!stream) {
|
|
42
|
+
return [...this.events];
|
|
43
|
+
}
|
|
44
|
+
return this.events.filter((event) => event.stream === stream);
|
|
45
|
+
}
|
|
46
|
+
async pushJob(queue, payload, options = {}) {
|
|
47
|
+
const jobs = this.getQueue(queue);
|
|
48
|
+
const now = new Date().toISOString();
|
|
49
|
+
const job = {
|
|
50
|
+
id: options.idempotencyKey ?? randomUUID(),
|
|
51
|
+
queue,
|
|
52
|
+
payload,
|
|
53
|
+
status: "ready",
|
|
54
|
+
attempts: 0,
|
|
55
|
+
maxAttempts: options.maxAttempts ?? 3,
|
|
56
|
+
createdAt: now,
|
|
57
|
+
availableAt: new Date(Date.now() + (options.delayMs ?? 0)).toISOString(),
|
|
58
|
+
};
|
|
59
|
+
const existing = jobs.find((candidate) => candidate.id === job.id);
|
|
60
|
+
if (existing) {
|
|
61
|
+
return this.cloneJob(existing);
|
|
62
|
+
}
|
|
63
|
+
jobs.push(job);
|
|
64
|
+
return this.cloneJob(job);
|
|
65
|
+
}
|
|
66
|
+
async claimJob(queue, options = {}) {
|
|
67
|
+
this.releaseExpiredLeases(queue);
|
|
68
|
+
const now = new Date();
|
|
69
|
+
const job = this.queues
|
|
70
|
+
.get(queue)
|
|
71
|
+
?.find((candidate) => candidate.status === "ready" && candidate.availableAt <= now.toISOString());
|
|
72
|
+
if (!job) {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
job.status = "leased";
|
|
76
|
+
job.attempts += 1;
|
|
77
|
+
job.leasedAt = now.toISOString();
|
|
78
|
+
job.leaseExpiresAt = new Date(now.getTime() + (options.leaseMs ?? DEFAULT_LEASE_MS)).toISOString();
|
|
79
|
+
return this.cloneJob(job);
|
|
80
|
+
}
|
|
81
|
+
async ackJob(queue, jobId) {
|
|
82
|
+
const job = this.findJob(queue, jobId);
|
|
83
|
+
if (!job) {
|
|
84
|
+
return {
|
|
85
|
+
ok: false,
|
|
86
|
+
reason: "not_found",
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
if (job.status === "completed" || job.status === "dead") {
|
|
90
|
+
return {
|
|
91
|
+
ok: false,
|
|
92
|
+
reason: "terminal",
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
if (job.status !== "leased") {
|
|
96
|
+
return {
|
|
97
|
+
ok: false,
|
|
98
|
+
reason: "not_leased",
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
job.status = "completed";
|
|
102
|
+
job.completedAt = new Date().toISOString();
|
|
103
|
+
return {
|
|
104
|
+
ok: true,
|
|
105
|
+
job: this.cloneJob(job),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
async nackJob(queue, jobId, options = {}) {
|
|
109
|
+
const job = this.findJob(queue, jobId);
|
|
110
|
+
if (!job) {
|
|
111
|
+
return {
|
|
112
|
+
ok: false,
|
|
113
|
+
reason: "not_found",
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
if (job.status === "completed" || job.status === "dead") {
|
|
117
|
+
return {
|
|
118
|
+
ok: false,
|
|
119
|
+
reason: "terminal",
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
if (job.status !== "leased") {
|
|
123
|
+
return {
|
|
124
|
+
ok: false,
|
|
125
|
+
reason: "not_leased",
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
job.lastError = options.error;
|
|
129
|
+
job.leasedAt = undefined;
|
|
130
|
+
job.leaseExpiresAt = undefined;
|
|
131
|
+
if (job.attempts >= job.maxAttempts) {
|
|
132
|
+
job.status = "dead";
|
|
133
|
+
job.deadAt = new Date().toISOString();
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
job.status = "ready";
|
|
137
|
+
job.availableAt = new Date(Date.now() + (options.delayMs ?? 0)).toISOString();
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
ok: true,
|
|
141
|
+
job: this.cloneJob(job),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
async listJobs(queue) {
|
|
145
|
+
return (this.queues.get(queue) ?? []).map((job) => this.cloneJob(job));
|
|
146
|
+
}
|
|
147
|
+
async listDeadJobs(queue) {
|
|
148
|
+
return (this.queues.get(queue) ?? [])
|
|
149
|
+
.filter((job) => job.status === "dead")
|
|
150
|
+
.map((job) => this.cloneJob(job));
|
|
151
|
+
}
|
|
152
|
+
async search(query, options = {}) {
|
|
153
|
+
const normalizedQuery = query.toLowerCase();
|
|
154
|
+
const collections = options.collections ? new Set(options.collections) : null;
|
|
155
|
+
const limit = options.limit ?? 10;
|
|
156
|
+
const results = [];
|
|
157
|
+
for (const [collection, records] of this.collections) {
|
|
158
|
+
if (collections && !collections.has(collection)) {
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
for (const record of records.values()) {
|
|
162
|
+
const haystack = JSON.stringify(record).toLowerCase();
|
|
163
|
+
if (haystack.includes(normalizedQuery)) {
|
|
164
|
+
results.push({
|
|
165
|
+
kind: "object",
|
|
166
|
+
id: record.id,
|
|
167
|
+
collection,
|
|
168
|
+
score: 1,
|
|
169
|
+
value: record,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
for (const event of this.events) {
|
|
175
|
+
const haystack = JSON.stringify(event).toLowerCase();
|
|
176
|
+
if (haystack.includes(normalizedQuery)) {
|
|
177
|
+
results.push({
|
|
178
|
+
kind: "event",
|
|
179
|
+
id: event.id,
|
|
180
|
+
stream: event.stream,
|
|
181
|
+
score: 1,
|
|
182
|
+
value: event,
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return results.slice(0, limit);
|
|
187
|
+
}
|
|
188
|
+
getCollection(collection) {
|
|
189
|
+
const records = this.collections.get(collection) ?? new Map();
|
|
190
|
+
this.collections.set(collection, records);
|
|
191
|
+
return records;
|
|
192
|
+
}
|
|
193
|
+
getQueue(queue) {
|
|
194
|
+
const jobs = this.queues.get(queue) ?? [];
|
|
195
|
+
this.queues.set(queue, jobs);
|
|
196
|
+
return jobs;
|
|
197
|
+
}
|
|
198
|
+
findJob(queue, jobId) {
|
|
199
|
+
return this.queues.get(queue)?.find((job) => job.id === jobId) ?? null;
|
|
200
|
+
}
|
|
201
|
+
releaseExpiredLeases(queue) {
|
|
202
|
+
const now = new Date().toISOString();
|
|
203
|
+
for (const job of this.queues.get(queue) ?? []) {
|
|
204
|
+
if (job.status === "leased" && job.leaseExpiresAt && job.leaseExpiresAt <= now) {
|
|
205
|
+
job.status = "ready";
|
|
206
|
+
job.leasedAt = undefined;
|
|
207
|
+
job.leaseExpiresAt = undefined;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
cloneJob(job) {
|
|
212
|
+
return {
|
|
213
|
+
...job,
|
|
214
|
+
payload: {
|
|
215
|
+
...job.payload,
|
|
216
|
+
},
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ThingDeleteResult, MemoryEvent, MemoryObject, MemorySearchOptions, MemorySearchResult, ThingStore, QueueClaimOptions, QueueJob, QueueJobOptions, QueueJobPayload, QueueJobResult, QueueNackOptions, StoredMemoryEvent, StoredMemoryObject } from "../types.js";
|
|
2
|
+
export declare class NativeThingStore implements ThingStore {
|
|
3
|
+
private readonly binding;
|
|
4
|
+
static open(path: string): Promise<NativeThingStore>;
|
|
5
|
+
private constructor();
|
|
6
|
+
put(collection: string, object: MemoryObject): Promise<StoredMemoryObject>;
|
|
7
|
+
get(collection: string, id: string): Promise<StoredMemoryObject | null>;
|
|
8
|
+
delete(collection: string, id: string): Promise<ThingDeleteResult>;
|
|
9
|
+
appendEvent(stream: string, event: MemoryEvent): Promise<StoredMemoryEvent>;
|
|
10
|
+
listEvents(stream?: string): Promise<StoredMemoryEvent[]>;
|
|
11
|
+
pushJob(queue: string, payload: QueueJobPayload, options?: QueueJobOptions): Promise<QueueJob>;
|
|
12
|
+
claimJob(queue: string, options?: QueueClaimOptions): Promise<QueueJob | null>;
|
|
13
|
+
ackJob(queue: string, jobId: string): Promise<QueueJobResult>;
|
|
14
|
+
nackJob(queue: string, jobId: string, options?: QueueNackOptions): Promise<QueueJobResult>;
|
|
15
|
+
listJobs(queue: string): Promise<QueueJob[]>;
|
|
16
|
+
listDeadJobs(queue: string): Promise<QueueJob[]>;
|
|
17
|
+
search(query: string, options?: MemorySearchOptions): Promise<MemorySearchResult[]>;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=native-thing-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"native-thing-store.d.ts","sourceRoot":"","sources":["../../src/stores/native-thing-store.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,iBAAiB,EACjB,WAAW,EACX,YAAY,EACZ,mBAAmB,EACnB,kBAAkB,EAClB,UAAU,EACV,iBAAiB,EACjB,QAAQ,EACR,eAAe,EACf,eAAe,EACf,cAAc,EACd,gBAAgB,EAChB,iBAAiB,EACjB,kBAAkB,EACnB,MAAM,aAAa,CAAC;AAwErB,qBAAa,gBAAiB,YAAW,UAAU;IAM7B,OAAO,CAAC,QAAQ,CAAC,OAAO;WAL/B,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAK1D,OAAO;IAED,GAAG,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,kBAA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+
export class NativeThingStore {
|
|
5
|
+
binding;
|
|
6
|
+
static async open(path) {
|
|
7
|
+
const native = await loadNativeModule();
|
|
8
|
+
return new NativeThingStore(native.NativeThingStore.open(path));
|
|
9
|
+
}
|
|
10
|
+
constructor(binding) {
|
|
11
|
+
this.binding = binding;
|
|
12
|
+
}
|
|
13
|
+
async put(collection, object) {
|
|
14
|
+
const record = parseJson(this.binding.putObjectJson(collection, object.id, JSON.stringify(object)));
|
|
15
|
+
return objectFromNative(record);
|
|
16
|
+
}
|
|
17
|
+
async get(collection, id) {
|
|
18
|
+
const record = this.binding.getObjectJson(collection, id);
|
|
19
|
+
return record ? objectFromNative(parseJson(record)) : null;
|
|
20
|
+
}
|
|
21
|
+
async delete(collection, id) {
|
|
22
|
+
return {
|
|
23
|
+
deleted: this.binding.deleteObject(collection, id),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
async appendEvent(stream, event) {
|
|
27
|
+
const record = parseJson(this.binding.appendEventJson(stream, JSON.stringify(event)));
|
|
28
|
+
return eventFromNative(record);
|
|
29
|
+
}
|
|
30
|
+
async listEvents(stream) {
|
|
31
|
+
return parseJson(this.binding.listEventsJson(stream)).map(eventFromNative);
|
|
32
|
+
}
|
|
33
|
+
async pushJob(queue, payload, options = {}) {
|
|
34
|
+
const record = parseJson(this.binding.pushJobJson(queue, options.idempotencyKey ?? randomUUID(), JSON.stringify(payload), options.maxAttempts ?? 3, options.delayMs ?? 0));
|
|
35
|
+
return jobFromNative(record);
|
|
36
|
+
}
|
|
37
|
+
async claimJob(queue, options = {}) {
|
|
38
|
+
const record = this.binding.claimJobJson(queue, options.leaseMs ?? DEFAULT_LEASE_MS);
|
|
39
|
+
return record ? jobFromNative(parseJson(record)) : null;
|
|
40
|
+
}
|
|
41
|
+
async ackJob(queue, jobId) {
|
|
42
|
+
return resultFromNative(parseJson(this.binding.ackJobJson(queue, jobId)));
|
|
43
|
+
}
|
|
44
|
+
async nackJob(queue, jobId, options = {}) {
|
|
45
|
+
const result = resultFromNative(parseJson(this.binding.nackJobJson(queue, jobId, options.delayMs ?? 0)));
|
|
46
|
+
if (result.ok && options.error) {
|
|
47
|
+
result.job.lastError = options.error;
|
|
48
|
+
}
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
async listJobs(queue) {
|
|
52
|
+
return parseJson(this.binding.listJobsJson(queue)).map(jobFromNative);
|
|
53
|
+
}
|
|
54
|
+
async listDeadJobs(queue) {
|
|
55
|
+
return parseJson(this.binding.listDeadJobsJson(queue)).map(jobFromNative);
|
|
56
|
+
}
|
|
57
|
+
async search(query, options = {}) {
|
|
58
|
+
const normalizedQuery = query.toLowerCase();
|
|
59
|
+
const collectionsJson = options.collections ? JSON.stringify(options.collections) : undefined;
|
|
60
|
+
const objects = parseJson(this.binding.listObjectsJson(collectionsJson)).map(objectFromNative);
|
|
61
|
+
const events = parseJson(this.binding.listEventsJson()).map(eventFromNative);
|
|
62
|
+
const results = [];
|
|
63
|
+
for (const object of objects) {
|
|
64
|
+
const haystack = JSON.stringify(object).toLowerCase();
|
|
65
|
+
if (haystack.includes(normalizedQuery)) {
|
|
66
|
+
results.push({
|
|
67
|
+
kind: "object",
|
|
68
|
+
id: object.id,
|
|
69
|
+
collection: object.collection,
|
|
70
|
+
score: 1,
|
|
71
|
+
value: object,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
for (const event of events) {
|
|
76
|
+
const haystack = JSON.stringify(event).toLowerCase();
|
|
77
|
+
if (haystack.includes(normalizedQuery)) {
|
|
78
|
+
results.push({
|
|
79
|
+
kind: "event",
|
|
80
|
+
id: event.id,
|
|
81
|
+
stream: event.stream,
|
|
82
|
+
score: 1,
|
|
83
|
+
value: event,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return results.slice(0, options.limit ?? 10);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
async function loadNativeModule() {
|
|
91
|
+
try {
|
|
92
|
+
return (await import(NATIVE_PACKAGE_NAME));
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
throw new Error(`The native thingd driver is not available. Run "pnpm --filter thingd-native build" before using driver: "native". ${formatUnknownError(error)}`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function objectFromNative(record) {
|
|
99
|
+
const value = parseJson(record.body);
|
|
100
|
+
const now = new Date().toISOString();
|
|
101
|
+
return {
|
|
102
|
+
...value,
|
|
103
|
+
id: record.id,
|
|
104
|
+
collection: record.collection,
|
|
105
|
+
createdAt: now,
|
|
106
|
+
updatedAt: now,
|
|
107
|
+
version: record.version,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
function eventFromNative(record) {
|
|
111
|
+
const value = parseJson(record.body);
|
|
112
|
+
return {
|
|
113
|
+
...value,
|
|
114
|
+
type: value.type ?? record.eventType,
|
|
115
|
+
id: String(record.sequence),
|
|
116
|
+
stream: record.stream,
|
|
117
|
+
createdAt: new Date().toISOString(),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
function jobFromNative(record) {
|
|
121
|
+
return {
|
|
122
|
+
id: record.id,
|
|
123
|
+
queue: record.queue,
|
|
124
|
+
payload: parseJson(record.body),
|
|
125
|
+
status: record.status,
|
|
126
|
+
attempts: record.attempts,
|
|
127
|
+
maxAttempts: record.maxAttempts,
|
|
128
|
+
createdAt: new Date().toISOString(),
|
|
129
|
+
availableAt: timestampToIso(record.availableAtMs),
|
|
130
|
+
leasedAt: optionalTimestampToIso(record.leasedAtMs),
|
|
131
|
+
leaseExpiresAt: optionalTimestampToIso(record.leaseExpiresAtMs),
|
|
132
|
+
completedAt: optionalTimestampToIso(record.completedAtMs),
|
|
133
|
+
deadAt: optionalTimestampToIso(record.deadAtMs),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
function resultFromNative(result) {
|
|
137
|
+
if (!result.ok) {
|
|
138
|
+
return result;
|
|
139
|
+
}
|
|
140
|
+
return {
|
|
141
|
+
ok: true,
|
|
142
|
+
job: jobFromNative(result.job),
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
function parseJson(json) {
|
|
146
|
+
return JSON.parse(json);
|
|
147
|
+
}
|
|
148
|
+
function timestampToIso(value) {
|
|
149
|
+
if (value <= 0) {
|
|
150
|
+
return new Date().toISOString();
|
|
151
|
+
}
|
|
152
|
+
return new Date(value).toISOString();
|
|
153
|
+
}
|
|
154
|
+
function optionalTimestampToIso(value) {
|
|
155
|
+
return value === undefined ? undefined : timestampToIso(value);
|
|
156
|
+
}
|
|
157
|
+
function formatUnknownError(error) {
|
|
158
|
+
if (error instanceof Error) {
|
|
159
|
+
return `Original error: ${error.message}`;
|
|
160
|
+
}
|
|
161
|
+
return "";
|
|
162
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ThingDeleteResult, MemoryEvent, MemoryObject, MemorySearchOptions, MemorySearchResult, ThingStore, QueueClaimOptions, QueueJob, QueueJobOptions, QueueJobPayload, QueueJobResult, QueueNackOptions, StoredMemoryEvent, StoredMemoryObject } from "../types.js";
|
|
2
|
+
export type RemoteThingStoreOptions = {
|
|
3
|
+
url: string;
|
|
4
|
+
authToken?: string;
|
|
5
|
+
clientName?: string;
|
|
6
|
+
clientVersion?: string;
|
|
7
|
+
};
|
|
8
|
+
export declare class RemoteThingStore implements ThingStore {
|
|
9
|
+
private readonly client;
|
|
10
|
+
static open(urlOrOptions: string | RemoteThingStoreOptions): Promise<RemoteThingStore>;
|
|
11
|
+
private constructor();
|
|
12
|
+
put(collection: string, object: MemoryObject): Promise<StoredMemoryObject>;
|
|
13
|
+
get(collection: string, id: string): Promise<StoredMemoryObject | null>;
|
|
14
|
+
delete(collection: string, id: string): Promise<ThingDeleteResult>;
|
|
15
|
+
appendEvent(stream: string, event: MemoryEvent): Promise<StoredMemoryEvent>;
|
|
16
|
+
listEvents(stream?: string): Promise<StoredMemoryEvent[]>;
|
|
17
|
+
pushJob(queue: string, payload: QueueJobPayload, options?: QueueJobOptions): Promise<QueueJob>;
|
|
18
|
+
claimJob(queue: string, options?: QueueClaimOptions): Promise<QueueJob | null>;
|
|
19
|
+
ackJob(queue: string, jobId: string): Promise<QueueJobResult>;
|
|
20
|
+
nackJob(queue: string, jobId: string, options?: QueueNackOptions): Promise<QueueJobResult>;
|
|
21
|
+
listJobs(queue: string): Promise<QueueJob[]>;
|
|
22
|
+
listDeadJobs(queue: string): Promise<QueueJob[]>;
|
|
23
|
+
search(query: string, options?: MemorySearchOptions): Promise<MemorySearchResult[]>;
|
|
24
|
+
close(): Promise<void>;
|
|
25
|
+
private callTool;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=remote-thing-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remote-thing-store.d.ts","sourceRoot":"","sources":["../../src/stores/remote-thing-store.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,iBAAiB,EACjB,WAAW,EACX,YAAY,EACZ,mBAAmB,EACnB,kBAAkB,EAClB,UAAU,EACV,iBAAiB,EACjB,QAAQ,EACR,eAAe,EACf,eAAe,EACf,cAAc,EACd,gBAAgB,EAChB,iBAAiB,EACjB,kBAAkB,EACnB,MAAM,aAAa,CAAC;AAErB,MAAM,MAAM,uBAAuB,GAAG;IACpC,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF,qBAAa,gBAAiB,YAAW,UAAU;IA2B7B,OAAO,CAAC,QAAQ,CAAC,MAAM;WA1B9B,IAAI,CAAC,YAAY,EAAE,MAAM,GAAG,uBAAuB,GAAG,OAAO,CAAC,gBAAgB,CAAC;IA0B5F,OAAO;IAEP,GAAG,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,kBAAkB,CAAC;I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"@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
3
|
+
export class RemoteThingStore {
|
|
4
|
+
client;
|
|
5
|
+
static async open(urlOrOptions) {
|
|
6
|
+
const options = typeof urlOrOptions === "string"
|
|
7
|
+
? {
|
|
8
|
+
url: urlOrOptions,
|
|
9
|
+
}
|
|
10
|
+
: urlOrOptions;
|
|
11
|
+
const client = new Client({
|
|
12
|
+
name: options.clientName ?? "thingd-node-sdk",
|
|
13
|
+
version: options.clientVersion ?? "0.1.0",
|
|
14
|
+
});
|
|
15
|
+
const transport = new StreamableHTTPClientTransport(new URL(resolveMcpUrl(options.url)), {
|
|
16
|
+
requestInit: options.authToken
|
|
17
|
+
? {
|
|
18
|
+
headers: {
|
|
19
|
+
Authorization: `Bearer ${options.authToken}`,
|
|
20
|
+
},
|
|
21
|
+
}
|
|
22
|
+
: undefined,
|
|
23
|
+
});
|
|
24
|
+
await client.connect(transport);
|
|
25
|
+
return new RemoteThingStore(client);
|
|
26
|
+
}
|
|
27
|
+
constructor(client) {
|
|
28
|
+
this.client = client;
|
|
29
|
+
}
|
|
30
|
+
put(collection, object) {
|
|
31
|
+
return this.callTool("thing.put", {
|
|
32
|
+
collection,
|
|
33
|
+
object,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
get(collection, id) {
|
|
37
|
+
return this.callTool("thing.get", {
|
|
38
|
+
collection,
|
|
39
|
+
id,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
delete(collection, id) {
|
|
43
|
+
return this.callTool("thing.delete", {
|
|
44
|
+
collection,
|
|
45
|
+
id,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
appendEvent(stream, event) {
|
|
49
|
+
return this.callTool("thing.events.append", {
|
|
50
|
+
stream,
|
|
51
|
+
event,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
listEvents(stream) {
|
|
55
|
+
return this.callTool("thing.events.list", {
|
|
56
|
+
stream,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
pushJob(queue, payload, options = {}) {
|
|
60
|
+
return this.callTool("thing.queue.push", {
|
|
61
|
+
queue,
|
|
62
|
+
payload,
|
|
63
|
+
idempotencyKey: options.idempotencyKey,
|
|
64
|
+
maxAttempts: options.maxAttempts,
|
|
65
|
+
delayMs: options.delayMs,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
claimJob(queue, options = {}) {
|
|
69
|
+
return this.callTool("thing.queue.claim", {
|
|
70
|
+
queue,
|
|
71
|
+
leaseMs: options.leaseMs,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
ackJob(queue, jobId) {
|
|
75
|
+
return this.callTool("thing.queue.ack", {
|
|
76
|
+
queue,
|
|
77
|
+
id: jobId,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
nackJob(queue, jobId, options = {}) {
|
|
81
|
+
return this.callTool("thing.queue.nack", {
|
|
82
|
+
queue,
|
|
83
|
+
id: jobId,
|
|
84
|
+
delayMs: options.delayMs,
|
|
85
|
+
error: options.error,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
listJobs(queue) {
|
|
89
|
+
return this.callTool("thing.queue.list", {
|
|
90
|
+
queue,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
listDeadJobs(queue) {
|
|
94
|
+
return this.callTool("thing.queue.dead", {
|
|
95
|
+
queue,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
search(query, options = {}) {
|
|
99
|
+
return this.callTool("thing.search", {
|
|
100
|
+
query,
|
|
101
|
+
collections: options.collections,
|
|
102
|
+
limit: options.limit,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
async close() {
|
|
106
|
+
await this.client.close();
|
|
107
|
+
}
|
|
108
|
+
async callTool(name, args) {
|
|
109
|
+
return parseJsonToolResult((await this.client.callTool({
|
|
110
|
+
name,
|
|
111
|
+
arguments: args,
|
|
112
|
+
})));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function resolveMcpUrl(value) {
|
|
116
|
+
const normalized = value.startsWith("thingd://")
|
|
117
|
+
? `http://${value.slice("thingd://".length)}`
|
|
118
|
+
: value;
|
|
119
|
+
const url = new URL(normalized);
|
|
120
|
+
if (url.pathname === "" || url.pathname === "/") {
|
|
121
|
+
url.pathname = "/mcp";
|
|
122
|
+
}
|
|
123
|
+
return url.toString();
|
|
124
|
+
}
|
|
125
|
+
function parseJsonToolResult(result) {
|
|
126
|
+
const text = result.content.find((part) => part.type === "text" && typeof part.text === "string")?.text;
|
|
127
|
+
if (!text) {
|
|
128
|
+
throw new Error("thingd remote tool did not return JSON text content");
|
|
129
|
+
}
|
|
130
|
+
return JSON.parse(text);
|
|
131
|
+
}
|
package/dist/thingd.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ThingDeleteResult, MemoryEvent, MemoryObject, MemoryQueue, MemorySearchOptions, MemorySearchResult, ThingStore, StoredMemoryEvent, StoredMemoryObject } from "./types.js";
|
|
2
|
+
export type ThingDDriver = "memory" | "native" | "remote";
|
|
3
|
+
export type ThingDOpenOptions = {
|
|
4
|
+
driver?: ThingDDriver;
|
|
5
|
+
store?: ThingStore;
|
|
6
|
+
authToken?: string;
|
|
7
|
+
};
|
|
8
|
+
export type ThingDOpenConfig = ThingDOpenOptions & {
|
|
9
|
+
path?: string;
|
|
10
|
+
url?: string;
|
|
11
|
+
};
|
|
12
|
+
export declare class ThingD {
|
|
13
|
+
readonly path: string;
|
|
14
|
+
private readonly store;
|
|
15
|
+
static open(pathOrConfig?: string | ThingDOpenConfig, options?: ThingDOpenOptions): Promise<ThingD>;
|
|
16
|
+
private constructor();
|
|
17
|
+
put(collection: string, object: MemoryObject): Promise<StoredMemoryObject>;
|
|
18
|
+
get(collection: string, id: string): Promise<StoredMemoryObject | null>;
|
|
19
|
+
delete(collection: string, id: string): Promise<ThingDeleteResult>;
|
|
20
|
+
search(query: string, options?: MemorySearchOptions): Promise<MemorySearchResult[]>;
|
|
21
|
+
readonly events: {
|
|
22
|
+
append: (stream: string, event: MemoryEvent) => Promise<StoredMemoryEvent>;
|
|
23
|
+
list: (stream?: string) => Promise<StoredMemoryEvent[]>;
|
|
24
|
+
};
|
|
25
|
+
queue(name: string): MemoryQueue;
|
|
26
|
+
close(): Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=thingd.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"thingd.d.ts","sourceRoot":"","sources":["../src/thingd.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,iBAAiB,EACjB,WAAW,EACX,YAAY,EACZ,WAAW,EACX,mBAAmB,EACnB,kBAAkB,EAClB,UAAU,EAKV,iBAAiB,EACjB,kBAAkB,EACnB,MAAM,YAAY,CAAC;AAEpB,MAAM,MAAM,YAAY,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,CAAC;AAE1D,MAAM,MAAM,iBAAiB,GAAG;IAC9B,MAAM,CAAC,EAAE,YAAY,CAAC;IACtB,KAAK,CAAC,EAAE,UAAU,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG,iBAAiB,GAAG;IACjD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,GAAG,CAAC,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,qBAAa,MAAM;IAcf,QAAQ,CAAC,IAAI,EAAE,MAAM;IACrB,OAAO,CAAC,QAAQ,CAAC,KAAK;WAdX,IAAI,CACf,YAAY,CAAC,EAAE,MAAM,GAAG,gBAAgB,EACxC,OAAO,GAAE,iBAAsB,GAC9B,OAAO,CAAC,MAAM,CAAC;IASlB,OAAO;IAKP,GAAG,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAI1E,GAAG,CAAC,UAAU,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,GAAG,IAAI,CAAC;IAIvE,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAIlE,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,GAAE,mBAAwB,GAAG,OAAO,CAAC,kBAAkB,EAAE,CAAC;IAIvF,QAAQ,CAAC,MAAM;yBACI,MAAM,SAAS,WAAW,KAAG,OAAO,CAAC,iBAAiB,CAAC;wBAExD,MAAM,KAAG,OAAO,CAAC,iBAAiB,EAAE,CAAC;MACrD;IAEF,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW;IAY1B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAG7B"}
|
package/dist/thingd.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { InMemoryThingStore } from "./stores/in-memory-thing-store.js";
|
|
2
|
+
import { NativeThingStore } from "./stores/native-thing-store.js";
|
|
3
|
+
import { RemoteThingStore } from "./stores/remote-thing-store.js";
|
|
4
|
+
export class ThingD {
|
|
5
|
+
path;
|
|
6
|
+
store;
|
|
7
|
+
static async open(pathOrConfig, options = {}) {
|
|
8
|
+
const resolvedOptions = resolveOpenOptions(pathOrConfig, options);
|
|
9
|
+
return new ThingD(resolvedOptions.path, await openStore(resolvedOptions.path, resolvedOptions));
|
|
10
|
+
}
|
|
11
|
+
constructor(path, store) {
|
|
12
|
+
this.path = path;
|
|
13
|
+
this.store = store;
|
|
14
|
+
}
|
|
15
|
+
put(collection, object) {
|
|
16
|
+
return this.store.put(collection, object);
|
|
17
|
+
}
|
|
18
|
+
get(collection, id) {
|
|
19
|
+
return this.store.get(collection, id);
|
|
20
|
+
}
|
|
21
|
+
delete(collection, id) {
|
|
22
|
+
return this.store.delete(collection, id);
|
|
23
|
+
}
|
|
24
|
+
search(query, options = {}) {
|
|
25
|
+
return this.store.search(query, options);
|
|
26
|
+
}
|
|
27
|
+
events = {
|
|
28
|
+
append: (stream, event) => this.store.appendEvent(stream, event),
|
|
29
|
+
list: (stream) => this.store.listEvents(stream),
|
|
30
|
+
};
|
|
31
|
+
queue(name) {
|
|
32
|
+
return {
|
|
33
|
+
push: (payload, options) => this.store.pushJob(name, payload, options),
|
|
34
|
+
claim: (options) => this.store.claimJob(name, options),
|
|
35
|
+
ack: (jobId) => this.store.ackJob(name, jobId),
|
|
36
|
+
nack: (jobId, options) => this.store.nackJob(name, jobId, options),
|
|
37
|
+
list: () => this.store.listJobs(name),
|
|
38
|
+
dead: () => this.store.listDeadJobs(name),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
async close() {
|
|
42
|
+
await this.store.close?.();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function resolveOpenOptions(pathOrConfig, options) {
|
|
46
|
+
const config = typeof pathOrConfig === "string"
|
|
47
|
+
? {
|
|
48
|
+
path: pathOrConfig,
|
|
49
|
+
}
|
|
50
|
+
: (pathOrConfig ?? {});
|
|
51
|
+
const path = config.url ?? config.path ?? process.env.THINGD_URL ?? ":memory:";
|
|
52
|
+
const driver = options.driver ?? config.driver ?? inferDriver(path);
|
|
53
|
+
return {
|
|
54
|
+
...config,
|
|
55
|
+
...options,
|
|
56
|
+
path,
|
|
57
|
+
driver,
|
|
58
|
+
authToken: options.authToken ?? config.authToken ?? process.env.THINGD_AUTH_TOKEN,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function inferDriver(path) {
|
|
62
|
+
if (isRemotePath(path)) {
|
|
63
|
+
return "remote";
|
|
64
|
+
}
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
function isRemotePath(path) {
|
|
68
|
+
return path.startsWith("http://") || path.startsWith("https://") || path.startsWith("thingd://");
|
|
69
|
+
}
|
|
70
|
+
async function openStore(path, options) {
|
|
71
|
+
if (options.store) {
|
|
72
|
+
return options.store;
|
|
73
|
+
}
|
|
74
|
+
if (options.driver === "remote") {
|
|
75
|
+
return RemoteThingStore.open({
|
|
76
|
+
url: path,
|
|
77
|
+
authToken: options.authToken,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
if (options.driver === "native") {
|
|
81
|
+
return NativeThingStore.open(path);
|
|
82
|
+
}
|
|
83
|
+
return new InMemoryThingStore();
|
|
84
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
export type MemoryObject = {
|
|
2
|
+
id: string;
|
|
3
|
+
[key: string]: unknown;
|
|
4
|
+
};
|
|
5
|
+
export type StoredMemoryObject = MemoryObject & {
|
|
6
|
+
collection: string;
|
|
7
|
+
createdAt: string;
|
|
8
|
+
updatedAt: string;
|
|
9
|
+
version: number;
|
|
10
|
+
};
|
|
11
|
+
export type MemoryEvent = {
|
|
12
|
+
type: string;
|
|
13
|
+
text?: string;
|
|
14
|
+
[key: string]: unknown;
|
|
15
|
+
};
|
|
16
|
+
export type StoredMemoryEvent = MemoryEvent & {
|
|
17
|
+
id: string;
|
|
18
|
+
stream: string;
|
|
19
|
+
createdAt: string;
|
|
20
|
+
};
|
|
21
|
+
export type QueueJobPayload = Record<string, unknown>;
|
|
22
|
+
export type QueueJobStatus = "ready" | "leased" | "completed" | "dead";
|
|
23
|
+
export type QueueJob = {
|
|
24
|
+
id: string;
|
|
25
|
+
queue: string;
|
|
26
|
+
payload: QueueJobPayload;
|
|
27
|
+
status: QueueJobStatus;
|
|
28
|
+
attempts: number;
|
|
29
|
+
maxAttempts: number;
|
|
30
|
+
createdAt: string;
|
|
31
|
+
availableAt: string;
|
|
32
|
+
leasedAt?: string;
|
|
33
|
+
leaseExpiresAt?: string;
|
|
34
|
+
completedAt?: string;
|
|
35
|
+
deadAt?: string;
|
|
36
|
+
lastError?: string;
|
|
37
|
+
};
|
|
38
|
+
export type QueueJobOptions = {
|
|
39
|
+
idempotencyKey?: string;
|
|
40
|
+
maxAttempts?: number;
|
|
41
|
+
delayMs?: number;
|
|
42
|
+
};
|
|
43
|
+
export type QueueClaimOptions = {
|
|
44
|
+
leaseMs?: number;
|
|
45
|
+
};
|
|
46
|
+
export type QueueNackOptions = {
|
|
47
|
+
delayMs?: number;
|
|
48
|
+
error?: string;
|
|
49
|
+
};
|
|
50
|
+
export type QueueJobResult = {
|
|
51
|
+
ok: true;
|
|
52
|
+
job: QueueJob;
|
|
53
|
+
} | {
|
|
54
|
+
ok: false;
|
|
55
|
+
reason: "not_found" | "not_leased" | "terminal";
|
|
56
|
+
};
|
|
57
|
+
export type ThingDeleteResult = {
|
|
58
|
+
deleted: boolean;
|
|
59
|
+
};
|
|
60
|
+
export type MemorySearchOptions = {
|
|
61
|
+
collections?: string[];
|
|
62
|
+
limit?: number;
|
|
63
|
+
};
|
|
64
|
+
export type MemorySearchResult = {
|
|
65
|
+
kind: "object";
|
|
66
|
+
id: string;
|
|
67
|
+
collection: string;
|
|
68
|
+
score: number;
|
|
69
|
+
value: StoredMemoryObject;
|
|
70
|
+
} | {
|
|
71
|
+
kind: "event";
|
|
72
|
+
id: string;
|
|
73
|
+
stream: string;
|
|
74
|
+
score: number;
|
|
75
|
+
value: StoredMemoryEvent;
|
|
76
|
+
};
|
|
77
|
+
export type MemoryQueue = {
|
|
78
|
+
push(payload: QueueJobPayload, options?: QueueJobOptions): Promise<QueueJob>;
|
|
79
|
+
claim(options?: QueueClaimOptions): Promise<QueueJob | null>;
|
|
80
|
+
ack(jobId: string): Promise<QueueJobResult>;
|
|
81
|
+
nack(jobId: string, options?: QueueNackOptions): Promise<QueueJobResult>;
|
|
82
|
+
list(): Promise<QueueJob[]>;
|
|
83
|
+
dead(): Promise<QueueJob[]>;
|
|
84
|
+
};
|
|
85
|
+
export interface ThingStore {
|
|
86
|
+
put(collection: string, object: MemoryObject): Promise<StoredMemoryObject>;
|
|
87
|
+
get(collection: string, id: string): Promise<StoredMemoryObject | null>;
|
|
88
|
+
delete(collection: string, id: string): Promise<ThingDeleteResult>;
|
|
89
|
+
appendEvent(stream: string, event: MemoryEvent): Promise<StoredMemoryEvent>;
|
|
90
|
+
listEvents(stream?: string): Promise<StoredMemoryEvent[]>;
|
|
91
|
+
pushJob(queue: string, payload: QueueJobPayload, options?: QueueJobOptions): Promise<QueueJob>;
|
|
92
|
+
claimJob(queue: string, options?: QueueClaimOptions): Promise<QueueJob | null>;
|
|
93
|
+
ackJob(queue: string, jobId: string): Promise<QueueJobResult>;
|
|
94
|
+
nackJob(queue: string, jobId: string, options?: QueueNackOptions): Promise<QueueJobResult>;
|
|
95
|
+
listJobs(queue: string): Promise<QueueJob[]>;
|
|
96
|
+
listDeadJobs(queue: string): Promise<QueueJob[]>;
|
|
97
|
+
search(query: string, options?: MemorySearchOptions): Promise<MemorySearchResult[]>;
|
|
98
|
+
close?(): Promise<void>;
|
|
99
|
+
}
|
|
100
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,GAAG;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG,YAAY,GAAG;IAC9C,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,WAAW,GAAG;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG,WAAW,GAAG;IAC5C,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,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|
+
"name": "thingd",
|
|
3
|
+
"version": "0.0.0-development",
|
|
4
|
+
"description": "Object-shaped local memory for AI-native apps.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"author": "Sayan Mohsin",
|
|
7
|
+
"license": "Apache-2.0",
|
|
8
|
+
"homepage": "https://github.com/sayanmohsin/thingd#readme",
|
|
9
|
+
"bugs": {
|
|
10
|
+
"url": "https://github.com/sayanmohsin/thingd/issues"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"ai",
|
|
14
|
+
"agents",
|
|
15
|
+
"mcp",
|
|
16
|
+
"memory",
|
|
17
|
+
"queue",
|
|
18
|
+
"sqlite",
|
|
19
|
+
"rust",
|
|
20
|
+
"local-first",
|
|
21
|
+
"embedded-database"
|
|
22
|
+
],
|
|
23
|
+
"sideEffects": false,
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/sayanmohsin/thingd.git"
|
|
27
|
+
},
|
|
28
|
+
"main": "./dist/index.js",
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"import": "./dist/index.js"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist",
|
|
38
|
+
"README.md"
|
|
39
|
+
],
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@modelcontextprotocol/sdk": "^1.29.0"
|
|
42
|
+
},
|
|
43
|
+
"engines": {
|
|
44
|
+
"node": ">=20"
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public",
|
|
48
|
+
"provenance": true
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "tsc -p tsconfig.json",
|
|
52
|
+
"test": "pnpm build && node --test test/*.test.mjs"
|
|
53
|
+
}
|
|
54
|
+
}
|