@sesamespace/hivemind 0.5.5 → 0.5.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-4U4GKI3X.js +2413 -0
- package/dist/chunk-4U4GKI3X.js.map +1 -0
- package/dist/chunk-62S2NGMN.js +79 -0
- package/dist/chunk-62S2NGMN.js.map +1 -0
- package/dist/{chunk-RXCV57H3.js → chunk-7RTJSPMX.js} +2 -2
- package/dist/chunk-IJRAVHQC.js +327 -0
- package/dist/chunk-IJRAVHQC.js.map +1 -0
- package/dist/{chunk-2OIRJFI5.js → chunk-JXLLCKMG.js} +129 -16
- package/dist/chunk-JXLLCKMG.js.map +1 -0
- package/dist/{chunk-SJI2KAIN.js → chunk-LJHJGDKY.js} +8 -2
- package/dist/chunk-LJHJGDKY.js.map +1 -0
- package/dist/{chunk-OQ272HKA.js → chunk-LSOEUOTO.js} +28 -3
- package/dist/chunk-LSOEUOTO.js.map +1 -0
- package/dist/{chunk-YEOAEJ62.js → chunk-PFN3BITA.js} +322 -2
- package/dist/chunk-PFN3BITA.js.map +1 -0
- package/dist/commands/fleet.js +3 -4
- package/dist/commands/init.js +1 -2
- package/dist/commands/service.js +1 -2
- package/dist/commands/start.js +3 -4
- package/dist/commands/upgrade.js +1 -2
- package/dist/commands/watchdog.js +10 -0
- package/dist/index.js +6 -3
- package/dist/main.js +22 -13
- package/dist/main.js.map +1 -1
- package/dist/start.js +1 -2
- package/dist/start.js.map +1 -1
- package/package.json +4 -3
- package/data/lancedb/contexts.lance/_transactions/0-c4755ab9-b604-4d90-851f-0491f3cbcbce.txn +0 -2
- package/data/lancedb/contexts.lance/_versions/1.manifest +0 -0
- package/data/lancedb/episode_access.lance/_transactions/0-407a6366-0dca-490a-868b-ea63bee3b40c.txn +0 -2
- package/data/lancedb/episode_access.lance/_versions/1.manifest +0 -0
- package/data/lancedb/episode_cooccurrence.lance/_transactions/0-0e103c7f-29d7-4f09-8100-505c076f01ae.txn +0 -1
- package/data/lancedb/episode_cooccurrence.lance/_versions/1.manifest +0 -0
- package/data/lancedb/episodes.lance/_transactions/0-e678cbac-792b-4a9d-a457-17b0d4d23607.txn +0 -1
- package/data/lancedb/episodes.lance/_versions/1.manifest +0 -0
- package/data/lancedb/l3_knowledge.lance/_transactions/0-cdb3561f-3a59-4e15-bded-e93c5f9a50e3.txn +0 -1
- package/data/lancedb/l3_knowledge.lance/_versions/1.manifest +0 -0
- package/data/lancedb/tasks.lance/_transactions/0-d1cf10ec-1eb8-48b4-bbbe-34b3a1083664.txn +0 -4
- package/data/lancedb/tasks.lance/_versions/1.manifest +0 -0
- package/dist/chunk-2OIRJFI5.js.map +0 -1
- package/dist/chunk-7D4SUZUM.js +0 -38
- package/dist/chunk-LRK64BAK.js +0 -3601
- package/dist/chunk-LRK64BAK.js.map +0 -1
- package/dist/chunk-MBS5A6BZ.js +0 -132
- package/dist/chunk-MBS5A6BZ.js.map +0 -1
- package/dist/chunk-OQ272HKA.js.map +0 -1
- package/dist/chunk-SJI2KAIN.js.map +0 -1
- package/dist/chunk-YEOAEJ62.js.map +0 -1
- /package/dist/{chunk-RXCV57H3.js.map → chunk-7RTJSPMX.js.map} +0 -0
- /package/dist/{chunk-7D4SUZUM.js.map → commands/watchdog.js.map} +0 -0
|
@@ -0,0 +1,2413 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SesameClient
|
|
3
|
+
} from "./chunk-GPI4RU7N.js";
|
|
4
|
+
|
|
5
|
+
// packages/runtime/src/llm-client.ts
|
|
6
|
+
var LLMClient = class {
|
|
7
|
+
baseUrl;
|
|
8
|
+
model;
|
|
9
|
+
maxTokens;
|
|
10
|
+
temperature;
|
|
11
|
+
apiKey;
|
|
12
|
+
constructor(config) {
|
|
13
|
+
this.baseUrl = config.base_url;
|
|
14
|
+
this.model = config.model;
|
|
15
|
+
this.maxTokens = config.max_tokens;
|
|
16
|
+
this.temperature = config.temperature;
|
|
17
|
+
this.apiKey = config.api_key ?? "";
|
|
18
|
+
}
|
|
19
|
+
async chat(messages) {
|
|
20
|
+
const resp = await fetch(`${this.baseUrl}/chat/completions`, {
|
|
21
|
+
method: "POST",
|
|
22
|
+
headers: {
|
|
23
|
+
"Content-Type": "application/json",
|
|
24
|
+
...this.apiKey ? { Authorization: `Bearer ${this.apiKey}` } : {}
|
|
25
|
+
},
|
|
26
|
+
body: JSON.stringify({
|
|
27
|
+
model: this.model,
|
|
28
|
+
messages,
|
|
29
|
+
max_tokens: this.maxTokens,
|
|
30
|
+
temperature: this.temperature
|
|
31
|
+
})
|
|
32
|
+
});
|
|
33
|
+
if (!resp.ok) {
|
|
34
|
+
const body = await resp.text();
|
|
35
|
+
throw new Error(`LLM request failed: ${resp.status} ${body}`);
|
|
36
|
+
}
|
|
37
|
+
const data = await resp.json();
|
|
38
|
+
return {
|
|
39
|
+
content: data.choices[0].message.content,
|
|
40
|
+
model: data.model,
|
|
41
|
+
usage: data.usage
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// packages/runtime/src/memory-client.ts
|
|
47
|
+
var MemoryClient = class {
|
|
48
|
+
baseUrl;
|
|
49
|
+
topK;
|
|
50
|
+
constructor(config) {
|
|
51
|
+
this.baseUrl = config.daemon_url;
|
|
52
|
+
this.topK = config.top_k;
|
|
53
|
+
}
|
|
54
|
+
async storeEpisode(input) {
|
|
55
|
+
const resp = await fetch(`${this.baseUrl}/episodes`, {
|
|
56
|
+
method: "POST",
|
|
57
|
+
headers: { "Content-Type": "application/json" },
|
|
58
|
+
body: JSON.stringify(input)
|
|
59
|
+
});
|
|
60
|
+
if (!resp.ok) {
|
|
61
|
+
throw new Error(`Memory store failed: ${resp.status} ${await resp.text()}`);
|
|
62
|
+
}
|
|
63
|
+
return resp.json();
|
|
64
|
+
}
|
|
65
|
+
async search(query, context, limit) {
|
|
66
|
+
const params = new URLSearchParams({ q: query });
|
|
67
|
+
if (context) params.set("context", context);
|
|
68
|
+
params.set("limit", String(limit ?? this.topK));
|
|
69
|
+
const resp = await fetch(`${this.baseUrl}/search?${params}`);
|
|
70
|
+
if (!resp.ok) {
|
|
71
|
+
throw new Error(`Memory search failed: ${resp.status} ${await resp.text()}`);
|
|
72
|
+
}
|
|
73
|
+
const data = await resp.json();
|
|
74
|
+
return data.episodes;
|
|
75
|
+
}
|
|
76
|
+
async getContext(name) {
|
|
77
|
+
const resp = await fetch(`${this.baseUrl}/contexts/${encodeURIComponent(name)}`);
|
|
78
|
+
if (!resp.ok) {
|
|
79
|
+
throw new Error(`Get context failed: ${resp.status} ${await resp.text()}`);
|
|
80
|
+
}
|
|
81
|
+
return resp.json();
|
|
82
|
+
}
|
|
83
|
+
async createContext(name, description = "") {
|
|
84
|
+
const resp = await fetch(`${this.baseUrl}/contexts`, {
|
|
85
|
+
method: "POST",
|
|
86
|
+
headers: { "Content-Type": "application/json" },
|
|
87
|
+
body: JSON.stringify({ name, description })
|
|
88
|
+
});
|
|
89
|
+
if (!resp.ok) {
|
|
90
|
+
throw new Error(`Create context failed: ${resp.status} ${await resp.text()}`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
async deleteContext(name) {
|
|
94
|
+
const resp = await fetch(`${this.baseUrl}/contexts/${encodeURIComponent(name)}`, {
|
|
95
|
+
method: "DELETE"
|
|
96
|
+
});
|
|
97
|
+
if (!resp.ok) {
|
|
98
|
+
throw new Error(`Delete context failed: ${resp.status} ${await resp.text()}`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
async listContexts() {
|
|
102
|
+
const resp = await fetch(`${this.baseUrl}/contexts`);
|
|
103
|
+
if (!resp.ok) {
|
|
104
|
+
throw new Error(`List contexts failed: ${resp.status} ${await resp.text()}`);
|
|
105
|
+
}
|
|
106
|
+
const data = await resp.json();
|
|
107
|
+
return data.contexts;
|
|
108
|
+
}
|
|
109
|
+
async searchCrossContext(query, limit) {
|
|
110
|
+
const params = new URLSearchParams({ q: query });
|
|
111
|
+
params.set("limit", String(limit ?? this.topK));
|
|
112
|
+
const resp = await fetch(`${this.baseUrl}/search/cross-context?${params}`);
|
|
113
|
+
if (!resp.ok) {
|
|
114
|
+
throw new Error(`Cross-context search failed: ${resp.status} ${await resp.text()}`);
|
|
115
|
+
}
|
|
116
|
+
const data = await resp.json();
|
|
117
|
+
return data.results;
|
|
118
|
+
}
|
|
119
|
+
async shareEpisode(episodeId, targetContext) {
|
|
120
|
+
const resp = await fetch(`${this.baseUrl}/episodes/${encodeURIComponent(episodeId)}/share`, {
|
|
121
|
+
method: "POST",
|
|
122
|
+
headers: { "Content-Type": "application/json" },
|
|
123
|
+
body: JSON.stringify({ target_context: targetContext })
|
|
124
|
+
});
|
|
125
|
+
if (!resp.ok) {
|
|
126
|
+
throw new Error(`Share episode failed: ${resp.status} ${await resp.text()}`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
async runPromotion(contextName) {
|
|
130
|
+
const params = new URLSearchParams();
|
|
131
|
+
if (contextName) params.set("context", contextName);
|
|
132
|
+
const resp = await fetch(`${this.baseUrl}/promotion/run?${params}`, {
|
|
133
|
+
method: "POST"
|
|
134
|
+
});
|
|
135
|
+
if (!resp.ok) {
|
|
136
|
+
throw new Error(`Promotion failed: ${resp.status} ${await resp.text()}`);
|
|
137
|
+
}
|
|
138
|
+
return resp.json();
|
|
139
|
+
}
|
|
140
|
+
async getL3Knowledge(contextName) {
|
|
141
|
+
const resp = await fetch(
|
|
142
|
+
`${this.baseUrl}/promotion/l3?context=${encodeURIComponent(contextName)}`
|
|
143
|
+
);
|
|
144
|
+
if (!resp.ok) {
|
|
145
|
+
throw new Error(`Get L3 failed: ${resp.status} ${await resp.text()}`);
|
|
146
|
+
}
|
|
147
|
+
const data = await resp.json();
|
|
148
|
+
return data.entries;
|
|
149
|
+
}
|
|
150
|
+
async setContextScoring(contextName, halfLifeHours) {
|
|
151
|
+
const resp = await fetch(
|
|
152
|
+
`${this.baseUrl}/contexts/${encodeURIComponent(contextName)}/scoring`,
|
|
153
|
+
{
|
|
154
|
+
method: "POST",
|
|
155
|
+
headers: { "Content-Type": "application/json" },
|
|
156
|
+
body: JSON.stringify({ half_life_hours: halfLifeHours })
|
|
157
|
+
}
|
|
158
|
+
);
|
|
159
|
+
if (!resp.ok) {
|
|
160
|
+
throw new Error(`Set scoring failed: ${resp.status} ${await resp.text()}`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
async recordAccess(episodeId) {
|
|
164
|
+
const resp = await fetch(`${this.baseUrl}/episodes/${encodeURIComponent(episodeId)}/access`, {
|
|
165
|
+
method: "POST"
|
|
166
|
+
});
|
|
167
|
+
if (!resp.ok) {
|
|
168
|
+
throw new Error(`Record access failed: ${resp.status} ${await resp.text()}`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
async recordCoAccess(episodeIds) {
|
|
172
|
+
const resp = await fetch(`${this.baseUrl}/episodes/co-access`, {
|
|
173
|
+
method: "POST",
|
|
174
|
+
headers: { "Content-Type": "application/json" },
|
|
175
|
+
body: JSON.stringify({ episode_ids: episodeIds })
|
|
176
|
+
});
|
|
177
|
+
if (!resp.ok) {
|
|
178
|
+
throw new Error(`Co-access failed: ${resp.status} ${await resp.text()}`);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
// Task engine methods
|
|
182
|
+
async createTask(input) {
|
|
183
|
+
const resp = await fetch(`${this.baseUrl}/tasks`, {
|
|
184
|
+
method: "POST",
|
|
185
|
+
headers: { "Content-Type": "application/json" },
|
|
186
|
+
body: JSON.stringify(input)
|
|
187
|
+
});
|
|
188
|
+
if (!resp.ok) {
|
|
189
|
+
throw new Error(`Create task failed: ${resp.status} ${await resp.text()}`);
|
|
190
|
+
}
|
|
191
|
+
return resp.json();
|
|
192
|
+
}
|
|
193
|
+
async listTasks(contextName, status) {
|
|
194
|
+
const params = new URLSearchParams({ context: contextName });
|
|
195
|
+
if (status) params.set("status", status);
|
|
196
|
+
const resp = await fetch(`${this.baseUrl}/tasks?${params}`);
|
|
197
|
+
if (!resp.ok) {
|
|
198
|
+
throw new Error(`List tasks failed: ${resp.status} ${await resp.text()}`);
|
|
199
|
+
}
|
|
200
|
+
const data = await resp.json();
|
|
201
|
+
return data.tasks;
|
|
202
|
+
}
|
|
203
|
+
async updateTask(taskId, updates) {
|
|
204
|
+
const resp = await fetch(`${this.baseUrl}/tasks/${encodeURIComponent(taskId)}`, {
|
|
205
|
+
method: "PATCH",
|
|
206
|
+
headers: { "Content-Type": "application/json" },
|
|
207
|
+
body: JSON.stringify(updates)
|
|
208
|
+
});
|
|
209
|
+
if (!resp.ok) {
|
|
210
|
+
throw new Error(`Update task failed: ${resp.status} ${await resp.text()}`);
|
|
211
|
+
}
|
|
212
|
+
return resp.json();
|
|
213
|
+
}
|
|
214
|
+
async getNextTask(contextName) {
|
|
215
|
+
const resp = await fetch(`${this.baseUrl}/tasks/next?context=${encodeURIComponent(contextName)}`);
|
|
216
|
+
if (!resp.ok) {
|
|
217
|
+
if (resp.status === 404) return null;
|
|
218
|
+
throw new Error(`Get next task failed: ${resp.status} ${await resp.text()}`);
|
|
219
|
+
}
|
|
220
|
+
return resp.json();
|
|
221
|
+
}
|
|
222
|
+
async healthCheck() {
|
|
223
|
+
try {
|
|
224
|
+
const resp = await fetch(`${this.baseUrl}/health`);
|
|
225
|
+
return resp.ok;
|
|
226
|
+
} catch {
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
// packages/runtime/src/context.ts
|
|
233
|
+
var SWITCH_PATTERNS = [
|
|
234
|
+
/^switch\s+to\s+(\S+)/i,
|
|
235
|
+
/^context:\s*(\S+)/i,
|
|
236
|
+
/^@(\S+)\s/,
|
|
237
|
+
/^working\s+on\s+(\S+)/i
|
|
238
|
+
];
|
|
239
|
+
var ContextManager = class {
|
|
240
|
+
contexts = /* @__PURE__ */ new Map();
|
|
241
|
+
activeContext = "global";
|
|
242
|
+
memory;
|
|
243
|
+
constructor(memory) {
|
|
244
|
+
this.memory = memory;
|
|
245
|
+
this.contexts.set("global", {
|
|
246
|
+
name: "global",
|
|
247
|
+
description: "Global context \u2014 identity, preferences, cross-cutting knowledge",
|
|
248
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
249
|
+
last_active: (/* @__PURE__ */ new Date()).toISOString()
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
async createContext(name, description = "") {
|
|
253
|
+
if (this.contexts.has(name)) {
|
|
254
|
+
return this.contexts.get(name);
|
|
255
|
+
}
|
|
256
|
+
const metadata = {
|
|
257
|
+
name,
|
|
258
|
+
description,
|
|
259
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
260
|
+
last_active: (/* @__PURE__ */ new Date()).toISOString()
|
|
261
|
+
};
|
|
262
|
+
this.contexts.set(name, metadata);
|
|
263
|
+
try {
|
|
264
|
+
await this.memory.createContext(name, description);
|
|
265
|
+
} catch (err) {
|
|
266
|
+
console.error(`Failed to register context '${name}' with daemon:`, err.message);
|
|
267
|
+
}
|
|
268
|
+
return metadata;
|
|
269
|
+
}
|
|
270
|
+
async deleteContext(name) {
|
|
271
|
+
if (name === "global") {
|
|
272
|
+
console.error("Cannot delete global context");
|
|
273
|
+
return false;
|
|
274
|
+
}
|
|
275
|
+
if (!this.contexts.has(name)) {
|
|
276
|
+
return false;
|
|
277
|
+
}
|
|
278
|
+
this.contexts.delete(name);
|
|
279
|
+
if (this.activeContext === name) {
|
|
280
|
+
this.activeContext = "global";
|
|
281
|
+
}
|
|
282
|
+
try {
|
|
283
|
+
await this.memory.deleteContext(name);
|
|
284
|
+
} catch (err) {
|
|
285
|
+
console.error(`Failed to delete context '${name}' from daemon:`, err.message);
|
|
286
|
+
}
|
|
287
|
+
return true;
|
|
288
|
+
}
|
|
289
|
+
listContexts() {
|
|
290
|
+
return Array.from(this.contexts.values());
|
|
291
|
+
}
|
|
292
|
+
getContext(name) {
|
|
293
|
+
return this.contexts.get(name);
|
|
294
|
+
}
|
|
295
|
+
getActiveContext() {
|
|
296
|
+
return this.activeContext;
|
|
297
|
+
}
|
|
298
|
+
switchContext(name) {
|
|
299
|
+
const previousContext = this.activeContext;
|
|
300
|
+
const isNew = !this.contexts.has(name);
|
|
301
|
+
if (isNew) {
|
|
302
|
+
this.contexts.set(name, {
|
|
303
|
+
name,
|
|
304
|
+
description: "",
|
|
305
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
306
|
+
last_active: (/* @__PURE__ */ new Date()).toISOString()
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
this.activeContext = name;
|
|
310
|
+
this.touchContext(name);
|
|
311
|
+
return { previousContext, activeContext: name, isNew };
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Parse a message and determine if it's a context switch command.
|
|
315
|
+
* Returns the context name if a switch is detected, null otherwise.
|
|
316
|
+
*/
|
|
317
|
+
parseContextSwitch(message) {
|
|
318
|
+
for (const pattern of SWITCH_PATTERNS) {
|
|
319
|
+
const match = message.match(pattern);
|
|
320
|
+
if (match) {
|
|
321
|
+
return match[1].toLowerCase();
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
return null;
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Infer context from message content by checking known context names.
|
|
328
|
+
* Returns the best matching context or current active context.
|
|
329
|
+
*/
|
|
330
|
+
inferContext(message) {
|
|
331
|
+
const lower = message.toLowerCase();
|
|
332
|
+
for (const [name] of this.contexts) {
|
|
333
|
+
if (name === "global") continue;
|
|
334
|
+
if (lower.includes(name.toLowerCase())) {
|
|
335
|
+
return name;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
return this.activeContext;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Route a message: check for explicit switch, then infer context.
|
|
342
|
+
* Returns the resolved context name and whether a switch happened.
|
|
343
|
+
*/
|
|
344
|
+
routeMessage(message) {
|
|
345
|
+
const switchTarget = this.parseContextSwitch(message);
|
|
346
|
+
if (switchTarget) {
|
|
347
|
+
const result = this.switchContext(switchTarget);
|
|
348
|
+
return { context: switchTarget, switched: true, switchedTo: result.activeContext };
|
|
349
|
+
}
|
|
350
|
+
const inferred = this.inferContext(message);
|
|
351
|
+
if (inferred !== this.activeContext) {
|
|
352
|
+
this.touchContext(inferred);
|
|
353
|
+
return { context: inferred, switched: false };
|
|
354
|
+
}
|
|
355
|
+
this.touchContext(this.activeContext);
|
|
356
|
+
return { context: this.activeContext, switched: false };
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Returns contexts that should be searched: active context + global.
|
|
360
|
+
*/
|
|
361
|
+
getSearchContexts() {
|
|
362
|
+
const contexts = [this.activeContext];
|
|
363
|
+
if (this.activeContext !== "global") {
|
|
364
|
+
contexts.push("global");
|
|
365
|
+
}
|
|
366
|
+
return contexts;
|
|
367
|
+
}
|
|
368
|
+
hasContext(name) {
|
|
369
|
+
return this.contexts.has(name);
|
|
370
|
+
}
|
|
371
|
+
touchContext(name) {
|
|
372
|
+
const ctx = this.contexts.get(name);
|
|
373
|
+
if (ctx) {
|
|
374
|
+
ctx.last_active = (/* @__PURE__ */ new Date()).toISOString();
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
|
|
379
|
+
// packages/runtime/src/task-engine.ts
|
|
380
|
+
var TaskEngine = class {
|
|
381
|
+
contextName;
|
|
382
|
+
memory;
|
|
383
|
+
constructor(options) {
|
|
384
|
+
this.contextName = options.contextName;
|
|
385
|
+
this.memory = options.memory;
|
|
386
|
+
}
|
|
387
|
+
async addTask(title, description, blockedBy = []) {
|
|
388
|
+
return this.memory.createTask({
|
|
389
|
+
context_name: this.contextName,
|
|
390
|
+
title,
|
|
391
|
+
description,
|
|
392
|
+
status: "planned",
|
|
393
|
+
blocked_by: blockedBy
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
async listTasks(status) {
|
|
397
|
+
return this.memory.listTasks(this.contextName, status);
|
|
398
|
+
}
|
|
399
|
+
async startTask(taskId) {
|
|
400
|
+
return this.memory.updateTask(taskId, { status: "active" });
|
|
401
|
+
}
|
|
402
|
+
async completeTask(taskId) {
|
|
403
|
+
return this.memory.updateTask(taskId, { status: "complete" });
|
|
404
|
+
}
|
|
405
|
+
async archiveTask(taskId) {
|
|
406
|
+
return this.memory.updateTask(taskId, { status: "archived" });
|
|
407
|
+
}
|
|
408
|
+
async getNextTask() {
|
|
409
|
+
return this.memory.getNextTask(this.contextName);
|
|
410
|
+
}
|
|
411
|
+
async pickAndStartNextTask() {
|
|
412
|
+
const next = await this.getNextTask();
|
|
413
|
+
if (next) {
|
|
414
|
+
return this.startTask(next.id);
|
|
415
|
+
}
|
|
416
|
+
return null;
|
|
417
|
+
}
|
|
418
|
+
setContext(contextName) {
|
|
419
|
+
this.contextName = contextName;
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Parse a task command from a chat message.
|
|
423
|
+
* Returns the action and parsed parameters, or null if not a task command.
|
|
424
|
+
*/
|
|
425
|
+
static parseTaskCommand(message) {
|
|
426
|
+
const lower = message.trim().toLowerCase();
|
|
427
|
+
const addMatch = message.match(/^(?:add|create)\s+task[:\s]+(.+)/i);
|
|
428
|
+
if (addMatch) {
|
|
429
|
+
return { action: "add", title: addMatch[1].trim() };
|
|
430
|
+
}
|
|
431
|
+
if (/^list\s+tasks/i.test(lower)) {
|
|
432
|
+
const statusMatch = lower.match(/list\s+tasks\s+(\w+)/);
|
|
433
|
+
return { action: "list", statusFilter: statusMatch?.[1] };
|
|
434
|
+
}
|
|
435
|
+
const completeMatch = message.match(/^complete\s+task\s+(\S+)/i);
|
|
436
|
+
if (completeMatch) {
|
|
437
|
+
return { action: "complete", taskId: completeMatch[1] };
|
|
438
|
+
}
|
|
439
|
+
const startMatch = message.match(/^start\s+task\s+(\S+)/i);
|
|
440
|
+
if (startMatch) {
|
|
441
|
+
return { action: "start", taskId: startMatch[1] };
|
|
442
|
+
}
|
|
443
|
+
if (/^next\s+task/i.test(lower)) {
|
|
444
|
+
return { action: "next" };
|
|
445
|
+
}
|
|
446
|
+
const archiveMatch = message.match(/^archive\s+task\s+(\S+)/i);
|
|
447
|
+
if (archiveMatch) {
|
|
448
|
+
return { action: "archive", taskId: archiveMatch[1] };
|
|
449
|
+
}
|
|
450
|
+
return null;
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
|
|
454
|
+
// packages/runtime/src/prompt.ts
|
|
455
|
+
import { readFileSync, existsSync, readdirSync } from "fs";
|
|
456
|
+
import { resolve } from "path";
|
|
457
|
+
var charterCache = null;
|
|
458
|
+
function loadCharter(path) {
|
|
459
|
+
if (charterCache && charterCache.path === path) return charterCache.content;
|
|
460
|
+
if (!existsSync(path)) return "";
|
|
461
|
+
const content = readFileSync(path, "utf-8");
|
|
462
|
+
charterCache = { path, content };
|
|
463
|
+
return content;
|
|
464
|
+
}
|
|
465
|
+
var WORKSPACE_FILE_PRIORITY = [
|
|
466
|
+
"SOUL.md",
|
|
467
|
+
// Who the agent is (personality, values)
|
|
468
|
+
"IDENTITY.md",
|
|
469
|
+
// Name, avatar, creature type
|
|
470
|
+
"AGENTS.md",
|
|
471
|
+
// Operating instructions
|
|
472
|
+
"USER.md",
|
|
473
|
+
// About the human(s)
|
|
474
|
+
"TOOLS.md"
|
|
475
|
+
// Tool-specific notes
|
|
476
|
+
];
|
|
477
|
+
var workspaceCache = null;
|
|
478
|
+
var WORKSPACE_CACHE_TTL_MS = 6e4;
|
|
479
|
+
function loadWorkspaceFiles(dir) {
|
|
480
|
+
const now = Date.now();
|
|
481
|
+
if (workspaceCache && workspaceCache.dir === dir && now - workspaceCache.loadedAt < WORKSPACE_CACHE_TTL_MS) {
|
|
482
|
+
return workspaceCache.files;
|
|
483
|
+
}
|
|
484
|
+
const files = /* @__PURE__ */ new Map();
|
|
485
|
+
if (!existsSync(dir)) return files;
|
|
486
|
+
for (const filename of WORKSPACE_FILE_PRIORITY) {
|
|
487
|
+
const filePath = resolve(dir, filename);
|
|
488
|
+
if (existsSync(filePath)) {
|
|
489
|
+
try {
|
|
490
|
+
files.set(filename, readFileSync(filePath, "utf-8"));
|
|
491
|
+
} catch {
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
try {
|
|
496
|
+
const entries = readdirSync(dir);
|
|
497
|
+
for (const entry of entries) {
|
|
498
|
+
if (entry.endsWith(".md") && !files.has(entry) && !entry.startsWith(".")) {
|
|
499
|
+
const filePath = resolve(dir, entry);
|
|
500
|
+
try {
|
|
501
|
+
files.set(entry, readFileSync(filePath, "utf-8"));
|
|
502
|
+
} catch {
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
} catch {
|
|
507
|
+
}
|
|
508
|
+
workspaceCache = { dir, files, loadedAt: now };
|
|
509
|
+
const names = [...files.keys()].join(", ");
|
|
510
|
+
if (files.size > 0) {
|
|
511
|
+
console.log(`[workspace] Loaded ${files.size} files: ${names}`);
|
|
512
|
+
}
|
|
513
|
+
return files;
|
|
514
|
+
}
|
|
515
|
+
function buildSystemPrompt(config, episodes, contextName = "global", l3Knowledge = []) {
|
|
516
|
+
let prompt = `You are ${config.name}. ${config.personality}
|
|
517
|
+
`;
|
|
518
|
+
if (config.workspace) {
|
|
519
|
+
const wsFiles = loadWorkspaceFiles(config.workspace);
|
|
520
|
+
if (wsFiles.size > 0) {
|
|
521
|
+
prompt += "\n# Workspace Context\n";
|
|
522
|
+
for (const [filename, content] of wsFiles) {
|
|
523
|
+
prompt += `
|
|
524
|
+
## ${filename}
|
|
525
|
+
${content}
|
|
526
|
+
`;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
if (config.team_charter && !config.workspace) {
|
|
531
|
+
const charter = loadCharter(config.team_charter);
|
|
532
|
+
if (charter) {
|
|
533
|
+
prompt += `
|
|
534
|
+
${charter}
|
|
535
|
+
`;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
prompt += `
|
|
539
|
+
## Communication
|
|
540
|
+
Messages are prefixed with [sender_handle]: or [sender_handle in group chat]: to tell you who's talking.
|
|
541
|
+
In group chats, multiple people (humans and agents) may be present. Address them by name when relevant.
|
|
542
|
+
Don't repeat or quote these prefixes in your responses \u2014 just respond naturally.
|
|
543
|
+
If you decide not to respond to a group message, reply with exactly: __SKIP__
|
|
544
|
+
`;
|
|
545
|
+
if (contextName !== "global") {
|
|
546
|
+
prompt += `
|
|
547
|
+
## Active Context: ${contextName}
|
|
548
|
+
You are currently working in the "${contextName}" project context.
|
|
549
|
+
`;
|
|
550
|
+
}
|
|
551
|
+
if (l3Knowledge.length > 0) {
|
|
552
|
+
prompt += "\n## Established Knowledge (learned patterns)\n\n";
|
|
553
|
+
for (const entry of l3Knowledge) {
|
|
554
|
+
prompt += `- ${entry.content}
|
|
555
|
+
`;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
if (episodes.length > 0) {
|
|
559
|
+
prompt += "\n## Relevant memories from previous conversations\n\n";
|
|
560
|
+
for (const ep of episodes) {
|
|
561
|
+
const timeAgo = formatTimeAgo(ep.timestamp);
|
|
562
|
+
const ctxLabel = ep.context_name !== contextName ? ` [from: ${ep.context_name}]` : "";
|
|
563
|
+
prompt += `[${timeAgo}]${ctxLabel} ${ep.role}: ${ep.content}
|
|
564
|
+
`;
|
|
565
|
+
}
|
|
566
|
+
prompt += "\nUse these memories naturally \u2014 reference past conversations when relevant, but don't force it.\n";
|
|
567
|
+
}
|
|
568
|
+
return prompt;
|
|
569
|
+
}
|
|
570
|
+
function buildMessages(systemPrompt, conversationHistory, currentMessage) {
|
|
571
|
+
return [
|
|
572
|
+
{ role: "system", content: systemPrompt },
|
|
573
|
+
...conversationHistory,
|
|
574
|
+
{ role: "user", content: currentMessage }
|
|
575
|
+
];
|
|
576
|
+
}
|
|
577
|
+
function formatTimeAgo(timestamp) {
|
|
578
|
+
const date = new Date(timestamp);
|
|
579
|
+
const now = /* @__PURE__ */ new Date();
|
|
580
|
+
const diffMs = now.getTime() - date.getTime();
|
|
581
|
+
const diffMins = Math.floor(diffMs / 6e4);
|
|
582
|
+
const diffHours = Math.floor(diffMs / 36e5);
|
|
583
|
+
const diffDays = Math.floor(diffMs / 864e5);
|
|
584
|
+
if (diffMins < 1) return "just now";
|
|
585
|
+
if (diffMins < 60) return `${diffMins}m ago`;
|
|
586
|
+
if (diffHours < 24) return `${diffHours}h ago`;
|
|
587
|
+
if (diffDays < 7) return `${diffDays}d ago`;
|
|
588
|
+
return date.toLocaleDateString();
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
// packages/runtime/src/agent.ts
|
|
592
|
+
var Agent = class {
|
|
593
|
+
config;
|
|
594
|
+
llm;
|
|
595
|
+
memory;
|
|
596
|
+
contextManager;
|
|
597
|
+
// Per-context conversation histories
|
|
598
|
+
conversationHistories = /* @__PURE__ */ new Map();
|
|
599
|
+
messageCount = 0;
|
|
600
|
+
PROMOTION_INTERVAL = 10;
|
|
601
|
+
// Run promotion every N messages
|
|
602
|
+
constructor(config, contextName = "global") {
|
|
603
|
+
this.config = config;
|
|
604
|
+
this.llm = new LLMClient(config.llm);
|
|
605
|
+
this.memory = new MemoryClient(config.memory);
|
|
606
|
+
this.contextManager = new ContextManager(this.memory);
|
|
607
|
+
if (contextName !== "global") {
|
|
608
|
+
this.contextManager.switchContext(contextName);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
async processMessage(userMessage) {
|
|
612
|
+
const specialResult = await this.handleSpecialCommand(userMessage);
|
|
613
|
+
if (specialResult) return specialResult;
|
|
614
|
+
const routing = this.contextManager.routeMessage(userMessage);
|
|
615
|
+
const contextName = routing.context;
|
|
616
|
+
if (routing.switched) {
|
|
617
|
+
try {
|
|
618
|
+
await this.memory.createContext(contextName);
|
|
619
|
+
} catch {
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
if (!this.conversationHistories.has(contextName)) {
|
|
623
|
+
this.conversationHistories.set(contextName, []);
|
|
624
|
+
}
|
|
625
|
+
const conversationHistory = this.conversationHistories.get(contextName);
|
|
626
|
+
const relevantEpisodes = await this.memory.search(userMessage, contextName, this.config.memory.top_k).catch((err) => {
|
|
627
|
+
console.error("Memory search failed, continuing without context:", err.message);
|
|
628
|
+
return [];
|
|
629
|
+
});
|
|
630
|
+
if (relevantEpisodes.length > 0) {
|
|
631
|
+
const episodeIds = relevantEpisodes.map((e) => e.id);
|
|
632
|
+
this.memory.recordCoAccess(episodeIds).catch(() => {
|
|
633
|
+
});
|
|
634
|
+
for (const ep of relevantEpisodes) {
|
|
635
|
+
this.memory.recordAccess(ep.id).catch(() => {
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
const l3Knowledge = await this.memory.getL3Knowledge(contextName).catch(() => []);
|
|
640
|
+
const systemPrompt = buildSystemPrompt(this.config.agent, relevantEpisodes, contextName, l3Knowledge);
|
|
641
|
+
const messages = buildMessages(systemPrompt, conversationHistory, userMessage);
|
|
642
|
+
const response = await this.llm.chat(messages);
|
|
643
|
+
conversationHistory.push(
|
|
644
|
+
{ role: "user", content: userMessage },
|
|
645
|
+
{ role: "assistant", content: response.content }
|
|
646
|
+
);
|
|
647
|
+
if (conversationHistory.length > 40) {
|
|
648
|
+
const trimmed = conversationHistory.slice(-40);
|
|
649
|
+
this.conversationHistories.set(contextName, trimmed);
|
|
650
|
+
}
|
|
651
|
+
await this.storeEpisodes(contextName, userMessage, response.content);
|
|
652
|
+
this.messageCount++;
|
|
653
|
+
if (this.messageCount % this.PROMOTION_INTERVAL === 0) {
|
|
654
|
+
this.memory.runPromotion(contextName).catch((err) => {
|
|
655
|
+
console.error("Promotion run failed:", err.message);
|
|
656
|
+
});
|
|
657
|
+
}
|
|
658
|
+
return {
|
|
659
|
+
content: response.content,
|
|
660
|
+
model: response.model,
|
|
661
|
+
context: contextName,
|
|
662
|
+
contextSwitched: routing.switched
|
|
663
|
+
};
|
|
664
|
+
}
|
|
665
|
+
async storeEpisodes(contextName, userMessage, assistantResponse) {
|
|
666
|
+
try {
|
|
667
|
+
await Promise.all([
|
|
668
|
+
this.memory.storeEpisode({
|
|
669
|
+
context_name: contextName,
|
|
670
|
+
role: "user",
|
|
671
|
+
content: userMessage
|
|
672
|
+
}),
|
|
673
|
+
this.memory.storeEpisode({
|
|
674
|
+
context_name: contextName,
|
|
675
|
+
role: "assistant",
|
|
676
|
+
content: assistantResponse
|
|
677
|
+
})
|
|
678
|
+
]);
|
|
679
|
+
} catch (err) {
|
|
680
|
+
console.error("Failed to store episodes:", err.message);
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
async handleSpecialCommand(message) {
|
|
684
|
+
const activeCtx = this.contextManager.getActiveContext();
|
|
685
|
+
const searchAllMatch = message.match(/^(?:search\s+all|cross-context\s+search)[:\s]+(.+)/i);
|
|
686
|
+
if (searchAllMatch) {
|
|
687
|
+
const query = searchAllMatch[1].trim();
|
|
688
|
+
try {
|
|
689
|
+
const results = await this.memory.searchCrossContext(query);
|
|
690
|
+
let response = "## Cross-Context Search Results\n\n";
|
|
691
|
+
if (results.length === 0) {
|
|
692
|
+
response += "No results found across any context.";
|
|
693
|
+
} else {
|
|
694
|
+
for (const group of results) {
|
|
695
|
+
response += `### Context: ${group.context}
|
|
696
|
+
`;
|
|
697
|
+
for (const ep of group.episodes) {
|
|
698
|
+
response += `- [${ep.role}] ${ep.content.slice(0, 200)}${ep.content.length > 200 ? "..." : ""} (score: ${ep.score.toFixed(3)})
|
|
699
|
+
`;
|
|
700
|
+
}
|
|
701
|
+
response += "\n";
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
return { content: response, model: "system", context: activeCtx };
|
|
705
|
+
} catch (err) {
|
|
706
|
+
return { content: `Cross-context search failed: ${err.message}`, model: "system", context: activeCtx };
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
const shareMatch = message.match(/^share\s+(\S+)\s+with\s+(\S+)/i);
|
|
710
|
+
if (shareMatch) {
|
|
711
|
+
const episodeId = shareMatch[1];
|
|
712
|
+
const targetContext = shareMatch[2];
|
|
713
|
+
try {
|
|
714
|
+
await this.memory.shareEpisode(episodeId, targetContext);
|
|
715
|
+
return { content: `Shared episode ${episodeId} with context "${targetContext}".`, model: "system", context: activeCtx };
|
|
716
|
+
} catch (err) {
|
|
717
|
+
return { content: `Failed to share: ${err.message}`, model: "system", context: activeCtx };
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
const taskCmd = TaskEngine.parseTaskCommand(message);
|
|
721
|
+
if (taskCmd) {
|
|
722
|
+
const engine = new TaskEngine({ contextName: activeCtx, memory: this.memory });
|
|
723
|
+
try {
|
|
724
|
+
switch (taskCmd.action) {
|
|
725
|
+
case "add": {
|
|
726
|
+
const task = await engine.addTask(taskCmd.title || "Untitled", taskCmd.description || "");
|
|
727
|
+
return { content: `Task created: [${task.id.slice(0, 8)}] ${task.title} (status: ${task.status})`, model: "system", context: activeCtx };
|
|
728
|
+
}
|
|
729
|
+
case "list": {
|
|
730
|
+
const tasks = await engine.listTasks(taskCmd.statusFilter);
|
|
731
|
+
if (tasks.length === 0) {
|
|
732
|
+
return { content: `No tasks${taskCmd.statusFilter ? ` with status "${taskCmd.statusFilter}"` : ""} in context "${activeCtx}".`, model: "system", context: activeCtx };
|
|
733
|
+
}
|
|
734
|
+
let response = `## Tasks in ${activeCtx}
|
|
735
|
+
|
|
736
|
+
`;
|
|
737
|
+
for (const t of tasks) {
|
|
738
|
+
const blockedBy = Array.isArray(t.blocked_by) ? t.blocked_by : JSON.parse(String(t.blocked_by) || "[]");
|
|
739
|
+
const blockedStr = blockedBy.length > 0 ? ` (blocked by: ${blockedBy.map((b) => b.slice(0, 8)).join(", ")})` : "";
|
|
740
|
+
response += `- [${t.status}] ${t.id.slice(0, 8)}: ${t.title}${blockedStr}
|
|
741
|
+
`;
|
|
742
|
+
}
|
|
743
|
+
return { content: response, model: "system", context: activeCtx };
|
|
744
|
+
}
|
|
745
|
+
case "start": {
|
|
746
|
+
const task = await engine.startTask(taskCmd.taskId);
|
|
747
|
+
return { content: task ? `Task ${taskCmd.taskId.slice(0, 8)} started.` : "Task not found.", model: "system", context: activeCtx };
|
|
748
|
+
}
|
|
749
|
+
case "complete": {
|
|
750
|
+
const task = await engine.completeTask(taskCmd.taskId);
|
|
751
|
+
return { content: task ? `Task ${taskCmd.taskId.slice(0, 8)} completed.` : "Task not found.", model: "system", context: activeCtx };
|
|
752
|
+
}
|
|
753
|
+
case "archive": {
|
|
754
|
+
const task = await engine.archiveTask(taskCmd.taskId);
|
|
755
|
+
return { content: task ? `Task ${taskCmd.taskId.slice(0, 8)} archived.` : "Task not found.", model: "system", context: activeCtx };
|
|
756
|
+
}
|
|
757
|
+
case "next": {
|
|
758
|
+
const task = await engine.pickAndStartNextTask();
|
|
759
|
+
if (task) {
|
|
760
|
+
return { content: `Picked up next task: [${task.id.slice(0, 8)}] ${task.title}`, model: "system", context: activeCtx };
|
|
761
|
+
}
|
|
762
|
+
return { content: "No available tasks to pick up.", model: "system", context: activeCtx };
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
} catch (err) {
|
|
766
|
+
return { content: `Task command failed: ${err.message}`, model: "system", context: activeCtx };
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
return null;
|
|
770
|
+
}
|
|
771
|
+
getMemoryClient() {
|
|
772
|
+
return this.memory;
|
|
773
|
+
}
|
|
774
|
+
getContextManager() {
|
|
775
|
+
return this.contextManager;
|
|
776
|
+
}
|
|
777
|
+
setContext(name) {
|
|
778
|
+
this.contextManager.switchContext(name);
|
|
779
|
+
}
|
|
780
|
+
getActiveContext() {
|
|
781
|
+
return this.contextManager.getActiveContext();
|
|
782
|
+
}
|
|
783
|
+
};
|
|
784
|
+
|
|
785
|
+
// packages/runtime/src/config.ts
|
|
786
|
+
import { readFileSync as readFileSync2, existsSync as existsSync2 } from "fs";
|
|
787
|
+
import { resolve as resolve2, dirname } from "path";
|
|
788
|
+
|
|
789
|
+
// node_modules/.pnpm/smol-toml@1.6.0/node_modules/smol-toml/dist/error.js
|
|
790
|
+
function getLineColFromPtr(string, ptr) {
|
|
791
|
+
let lines = string.slice(0, ptr).split(/\r\n|\n|\r/g);
|
|
792
|
+
return [lines.length, lines.pop().length + 1];
|
|
793
|
+
}
|
|
794
|
+
function makeCodeBlock(string, line, column) {
|
|
795
|
+
let lines = string.split(/\r\n|\n|\r/g);
|
|
796
|
+
let codeblock = "";
|
|
797
|
+
let numberLen = (Math.log10(line + 1) | 0) + 1;
|
|
798
|
+
for (let i = line - 1; i <= line + 1; i++) {
|
|
799
|
+
let l = lines[i - 1];
|
|
800
|
+
if (!l)
|
|
801
|
+
continue;
|
|
802
|
+
codeblock += i.toString().padEnd(numberLen, " ");
|
|
803
|
+
codeblock += ": ";
|
|
804
|
+
codeblock += l;
|
|
805
|
+
codeblock += "\n";
|
|
806
|
+
if (i === line) {
|
|
807
|
+
codeblock += " ".repeat(numberLen + column + 2);
|
|
808
|
+
codeblock += "^\n";
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
return codeblock;
|
|
812
|
+
}
|
|
813
|
+
var TomlError = class extends Error {
|
|
814
|
+
line;
|
|
815
|
+
column;
|
|
816
|
+
codeblock;
|
|
817
|
+
constructor(message, options) {
|
|
818
|
+
const [line, column] = getLineColFromPtr(options.toml, options.ptr);
|
|
819
|
+
const codeblock = makeCodeBlock(options.toml, line, column);
|
|
820
|
+
super(`Invalid TOML document: ${message}
|
|
821
|
+
|
|
822
|
+
${codeblock}`, options);
|
|
823
|
+
this.line = line;
|
|
824
|
+
this.column = column;
|
|
825
|
+
this.codeblock = codeblock;
|
|
826
|
+
}
|
|
827
|
+
};
|
|
828
|
+
|
|
829
|
+
// node_modules/.pnpm/smol-toml@1.6.0/node_modules/smol-toml/dist/util.js
|
|
830
|
+
function isEscaped(str, ptr) {
|
|
831
|
+
let i = 0;
|
|
832
|
+
while (str[ptr - ++i] === "\\")
|
|
833
|
+
;
|
|
834
|
+
return --i && i % 2;
|
|
835
|
+
}
|
|
836
|
+
function indexOfNewline(str, start = 0, end = str.length) {
|
|
837
|
+
let idx = str.indexOf("\n", start);
|
|
838
|
+
if (str[idx - 1] === "\r")
|
|
839
|
+
idx--;
|
|
840
|
+
return idx <= end ? idx : -1;
|
|
841
|
+
}
|
|
842
|
+
function skipComment(str, ptr) {
|
|
843
|
+
for (let i = ptr; i < str.length; i++) {
|
|
844
|
+
let c = str[i];
|
|
845
|
+
if (c === "\n")
|
|
846
|
+
return i;
|
|
847
|
+
if (c === "\r" && str[i + 1] === "\n")
|
|
848
|
+
return i + 1;
|
|
849
|
+
if (c < " " && c !== " " || c === "\x7F") {
|
|
850
|
+
throw new TomlError("control characters are not allowed in comments", {
|
|
851
|
+
toml: str,
|
|
852
|
+
ptr
|
|
853
|
+
});
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
return str.length;
|
|
857
|
+
}
|
|
858
|
+
function skipVoid(str, ptr, banNewLines, banComments) {
|
|
859
|
+
let c;
|
|
860
|
+
while ((c = str[ptr]) === " " || c === " " || !banNewLines && (c === "\n" || c === "\r" && str[ptr + 1] === "\n"))
|
|
861
|
+
ptr++;
|
|
862
|
+
return banComments || c !== "#" ? ptr : skipVoid(str, skipComment(str, ptr), banNewLines);
|
|
863
|
+
}
|
|
864
|
+
function skipUntil(str, ptr, sep, end, banNewLines = false) {
|
|
865
|
+
if (!end) {
|
|
866
|
+
ptr = indexOfNewline(str, ptr);
|
|
867
|
+
return ptr < 0 ? str.length : ptr;
|
|
868
|
+
}
|
|
869
|
+
for (let i = ptr; i < str.length; i++) {
|
|
870
|
+
let c = str[i];
|
|
871
|
+
if (c === "#") {
|
|
872
|
+
i = indexOfNewline(str, i);
|
|
873
|
+
} else if (c === sep) {
|
|
874
|
+
return i + 1;
|
|
875
|
+
} else if (c === end || banNewLines && (c === "\n" || c === "\r" && str[i + 1] === "\n")) {
|
|
876
|
+
return i;
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
throw new TomlError("cannot find end of structure", {
|
|
880
|
+
toml: str,
|
|
881
|
+
ptr
|
|
882
|
+
});
|
|
883
|
+
}
|
|
884
|
+
function getStringEnd(str, seek) {
|
|
885
|
+
let first = str[seek];
|
|
886
|
+
let target = first === str[seek + 1] && str[seek + 1] === str[seek + 2] ? str.slice(seek, seek + 3) : first;
|
|
887
|
+
seek += target.length - 1;
|
|
888
|
+
do
|
|
889
|
+
seek = str.indexOf(target, ++seek);
|
|
890
|
+
while (seek > -1 && first !== "'" && isEscaped(str, seek));
|
|
891
|
+
if (seek > -1) {
|
|
892
|
+
seek += target.length;
|
|
893
|
+
if (target.length > 1) {
|
|
894
|
+
if (str[seek] === first)
|
|
895
|
+
seek++;
|
|
896
|
+
if (str[seek] === first)
|
|
897
|
+
seek++;
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
return seek;
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
// node_modules/.pnpm/smol-toml@1.6.0/node_modules/smol-toml/dist/date.js
|
|
904
|
+
var DATE_TIME_RE = /^(\d{4}-\d{2}-\d{2})?[T ]?(?:(\d{2}):\d{2}(?::\d{2}(?:\.\d+)?)?)?(Z|[-+]\d{2}:\d{2})?$/i;
|
|
905
|
+
var TomlDate = class _TomlDate extends Date {
|
|
906
|
+
#hasDate = false;
|
|
907
|
+
#hasTime = false;
|
|
908
|
+
#offset = null;
|
|
909
|
+
constructor(date) {
|
|
910
|
+
let hasDate = true;
|
|
911
|
+
let hasTime = true;
|
|
912
|
+
let offset = "Z";
|
|
913
|
+
if (typeof date === "string") {
|
|
914
|
+
let match = date.match(DATE_TIME_RE);
|
|
915
|
+
if (match) {
|
|
916
|
+
if (!match[1]) {
|
|
917
|
+
hasDate = false;
|
|
918
|
+
date = `0000-01-01T${date}`;
|
|
919
|
+
}
|
|
920
|
+
hasTime = !!match[2];
|
|
921
|
+
hasTime && date[10] === " " && (date = date.replace(" ", "T"));
|
|
922
|
+
if (match[2] && +match[2] > 23) {
|
|
923
|
+
date = "";
|
|
924
|
+
} else {
|
|
925
|
+
offset = match[3] || null;
|
|
926
|
+
date = date.toUpperCase();
|
|
927
|
+
if (!offset && hasTime)
|
|
928
|
+
date += "Z";
|
|
929
|
+
}
|
|
930
|
+
} else {
|
|
931
|
+
date = "";
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
super(date);
|
|
935
|
+
if (!isNaN(this.getTime())) {
|
|
936
|
+
this.#hasDate = hasDate;
|
|
937
|
+
this.#hasTime = hasTime;
|
|
938
|
+
this.#offset = offset;
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
isDateTime() {
|
|
942
|
+
return this.#hasDate && this.#hasTime;
|
|
943
|
+
}
|
|
944
|
+
isLocal() {
|
|
945
|
+
return !this.#hasDate || !this.#hasTime || !this.#offset;
|
|
946
|
+
}
|
|
947
|
+
isDate() {
|
|
948
|
+
return this.#hasDate && !this.#hasTime;
|
|
949
|
+
}
|
|
950
|
+
isTime() {
|
|
951
|
+
return this.#hasTime && !this.#hasDate;
|
|
952
|
+
}
|
|
953
|
+
isValid() {
|
|
954
|
+
return this.#hasDate || this.#hasTime;
|
|
955
|
+
}
|
|
956
|
+
toISOString() {
|
|
957
|
+
let iso = super.toISOString();
|
|
958
|
+
if (this.isDate())
|
|
959
|
+
return iso.slice(0, 10);
|
|
960
|
+
if (this.isTime())
|
|
961
|
+
return iso.slice(11, 23);
|
|
962
|
+
if (this.#offset === null)
|
|
963
|
+
return iso.slice(0, -1);
|
|
964
|
+
if (this.#offset === "Z")
|
|
965
|
+
return iso;
|
|
966
|
+
let offset = +this.#offset.slice(1, 3) * 60 + +this.#offset.slice(4, 6);
|
|
967
|
+
offset = this.#offset[0] === "-" ? offset : -offset;
|
|
968
|
+
let offsetDate = new Date(this.getTime() - offset * 6e4);
|
|
969
|
+
return offsetDate.toISOString().slice(0, -1) + this.#offset;
|
|
970
|
+
}
|
|
971
|
+
static wrapAsOffsetDateTime(jsDate, offset = "Z") {
|
|
972
|
+
let date = new _TomlDate(jsDate);
|
|
973
|
+
date.#offset = offset;
|
|
974
|
+
return date;
|
|
975
|
+
}
|
|
976
|
+
static wrapAsLocalDateTime(jsDate) {
|
|
977
|
+
let date = new _TomlDate(jsDate);
|
|
978
|
+
date.#offset = null;
|
|
979
|
+
return date;
|
|
980
|
+
}
|
|
981
|
+
static wrapAsLocalDate(jsDate) {
|
|
982
|
+
let date = new _TomlDate(jsDate);
|
|
983
|
+
date.#hasTime = false;
|
|
984
|
+
date.#offset = null;
|
|
985
|
+
return date;
|
|
986
|
+
}
|
|
987
|
+
static wrapAsLocalTime(jsDate) {
|
|
988
|
+
let date = new _TomlDate(jsDate);
|
|
989
|
+
date.#hasDate = false;
|
|
990
|
+
date.#offset = null;
|
|
991
|
+
return date;
|
|
992
|
+
}
|
|
993
|
+
};
|
|
994
|
+
|
|
995
|
+
// node_modules/.pnpm/smol-toml@1.6.0/node_modules/smol-toml/dist/primitive.js
|
|
996
|
+
var INT_REGEX = /^((0x[0-9a-fA-F](_?[0-9a-fA-F])*)|(([+-]|0[ob])?\d(_?\d)*))$/;
|
|
997
|
+
var FLOAT_REGEX = /^[+-]?\d(_?\d)*(\.\d(_?\d)*)?([eE][+-]?\d(_?\d)*)?$/;
|
|
998
|
+
var LEADING_ZERO = /^[+-]?0[0-9_]/;
|
|
999
|
+
var ESCAPE_REGEX = /^[0-9a-f]{2,8}$/i;
|
|
1000
|
+
var ESC_MAP = {
|
|
1001
|
+
b: "\b",
|
|
1002
|
+
t: " ",
|
|
1003
|
+
n: "\n",
|
|
1004
|
+
f: "\f",
|
|
1005
|
+
r: "\r",
|
|
1006
|
+
e: "\x1B",
|
|
1007
|
+
'"': '"',
|
|
1008
|
+
"\\": "\\"
|
|
1009
|
+
};
|
|
1010
|
+
function parseString(str, ptr = 0, endPtr = str.length) {
|
|
1011
|
+
let isLiteral = str[ptr] === "'";
|
|
1012
|
+
let isMultiline = str[ptr++] === str[ptr] && str[ptr] === str[ptr + 1];
|
|
1013
|
+
if (isMultiline) {
|
|
1014
|
+
endPtr -= 2;
|
|
1015
|
+
if (str[ptr += 2] === "\r")
|
|
1016
|
+
ptr++;
|
|
1017
|
+
if (str[ptr] === "\n")
|
|
1018
|
+
ptr++;
|
|
1019
|
+
}
|
|
1020
|
+
let tmp = 0;
|
|
1021
|
+
let isEscape;
|
|
1022
|
+
let parsed = "";
|
|
1023
|
+
let sliceStart = ptr;
|
|
1024
|
+
while (ptr < endPtr - 1) {
|
|
1025
|
+
let c = str[ptr++];
|
|
1026
|
+
if (c === "\n" || c === "\r" && str[ptr] === "\n") {
|
|
1027
|
+
if (!isMultiline) {
|
|
1028
|
+
throw new TomlError("newlines are not allowed in strings", {
|
|
1029
|
+
toml: str,
|
|
1030
|
+
ptr: ptr - 1
|
|
1031
|
+
});
|
|
1032
|
+
}
|
|
1033
|
+
} else if (c < " " && c !== " " || c === "\x7F") {
|
|
1034
|
+
throw new TomlError("control characters are not allowed in strings", {
|
|
1035
|
+
toml: str,
|
|
1036
|
+
ptr: ptr - 1
|
|
1037
|
+
});
|
|
1038
|
+
}
|
|
1039
|
+
if (isEscape) {
|
|
1040
|
+
isEscape = false;
|
|
1041
|
+
if (c === "x" || c === "u" || c === "U") {
|
|
1042
|
+
let code = str.slice(ptr, ptr += c === "x" ? 2 : c === "u" ? 4 : 8);
|
|
1043
|
+
if (!ESCAPE_REGEX.test(code)) {
|
|
1044
|
+
throw new TomlError("invalid unicode escape", {
|
|
1045
|
+
toml: str,
|
|
1046
|
+
ptr: tmp
|
|
1047
|
+
});
|
|
1048
|
+
}
|
|
1049
|
+
try {
|
|
1050
|
+
parsed += String.fromCodePoint(parseInt(code, 16));
|
|
1051
|
+
} catch {
|
|
1052
|
+
throw new TomlError("invalid unicode escape", {
|
|
1053
|
+
toml: str,
|
|
1054
|
+
ptr: tmp
|
|
1055
|
+
});
|
|
1056
|
+
}
|
|
1057
|
+
} else if (isMultiline && (c === "\n" || c === " " || c === " " || c === "\r")) {
|
|
1058
|
+
ptr = skipVoid(str, ptr - 1, true);
|
|
1059
|
+
if (str[ptr] !== "\n" && str[ptr] !== "\r") {
|
|
1060
|
+
throw new TomlError("invalid escape: only line-ending whitespace may be escaped", {
|
|
1061
|
+
toml: str,
|
|
1062
|
+
ptr: tmp
|
|
1063
|
+
});
|
|
1064
|
+
}
|
|
1065
|
+
ptr = skipVoid(str, ptr);
|
|
1066
|
+
} else if (c in ESC_MAP) {
|
|
1067
|
+
parsed += ESC_MAP[c];
|
|
1068
|
+
} else {
|
|
1069
|
+
throw new TomlError("unrecognized escape sequence", {
|
|
1070
|
+
toml: str,
|
|
1071
|
+
ptr: tmp
|
|
1072
|
+
});
|
|
1073
|
+
}
|
|
1074
|
+
sliceStart = ptr;
|
|
1075
|
+
} else if (!isLiteral && c === "\\") {
|
|
1076
|
+
tmp = ptr - 1;
|
|
1077
|
+
isEscape = true;
|
|
1078
|
+
parsed += str.slice(sliceStart, tmp);
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
return parsed + str.slice(sliceStart, endPtr - 1);
|
|
1082
|
+
}
|
|
1083
|
+
function parseValue(value, toml, ptr, integersAsBigInt) {
|
|
1084
|
+
if (value === "true")
|
|
1085
|
+
return true;
|
|
1086
|
+
if (value === "false")
|
|
1087
|
+
return false;
|
|
1088
|
+
if (value === "-inf")
|
|
1089
|
+
return -Infinity;
|
|
1090
|
+
if (value === "inf" || value === "+inf")
|
|
1091
|
+
return Infinity;
|
|
1092
|
+
if (value === "nan" || value === "+nan" || value === "-nan")
|
|
1093
|
+
return NaN;
|
|
1094
|
+
if (value === "-0")
|
|
1095
|
+
return integersAsBigInt ? 0n : 0;
|
|
1096
|
+
let isInt = INT_REGEX.test(value);
|
|
1097
|
+
if (isInt || FLOAT_REGEX.test(value)) {
|
|
1098
|
+
if (LEADING_ZERO.test(value)) {
|
|
1099
|
+
throw new TomlError("leading zeroes are not allowed", {
|
|
1100
|
+
toml,
|
|
1101
|
+
ptr
|
|
1102
|
+
});
|
|
1103
|
+
}
|
|
1104
|
+
value = value.replace(/_/g, "");
|
|
1105
|
+
let numeric = +value;
|
|
1106
|
+
if (isNaN(numeric)) {
|
|
1107
|
+
throw new TomlError("invalid number", {
|
|
1108
|
+
toml,
|
|
1109
|
+
ptr
|
|
1110
|
+
});
|
|
1111
|
+
}
|
|
1112
|
+
if (isInt) {
|
|
1113
|
+
if ((isInt = !Number.isSafeInteger(numeric)) && !integersAsBigInt) {
|
|
1114
|
+
throw new TomlError("integer value cannot be represented losslessly", {
|
|
1115
|
+
toml,
|
|
1116
|
+
ptr
|
|
1117
|
+
});
|
|
1118
|
+
}
|
|
1119
|
+
if (isInt || integersAsBigInt === true)
|
|
1120
|
+
numeric = BigInt(value);
|
|
1121
|
+
}
|
|
1122
|
+
return numeric;
|
|
1123
|
+
}
|
|
1124
|
+
const date = new TomlDate(value);
|
|
1125
|
+
if (!date.isValid()) {
|
|
1126
|
+
throw new TomlError("invalid value", {
|
|
1127
|
+
toml,
|
|
1128
|
+
ptr
|
|
1129
|
+
});
|
|
1130
|
+
}
|
|
1131
|
+
return date;
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
// node_modules/.pnpm/smol-toml@1.6.0/node_modules/smol-toml/dist/extract.js
|
|
1135
|
+
function sliceAndTrimEndOf(str, startPtr, endPtr) {
|
|
1136
|
+
let value = str.slice(startPtr, endPtr);
|
|
1137
|
+
let commentIdx = value.indexOf("#");
|
|
1138
|
+
if (commentIdx > -1) {
|
|
1139
|
+
skipComment(str, commentIdx);
|
|
1140
|
+
value = value.slice(0, commentIdx);
|
|
1141
|
+
}
|
|
1142
|
+
return [value.trimEnd(), commentIdx];
|
|
1143
|
+
}
|
|
1144
|
+
function extractValue(str, ptr, end, depth, integersAsBigInt) {
|
|
1145
|
+
if (depth === 0) {
|
|
1146
|
+
throw new TomlError("document contains excessively nested structures. aborting.", {
|
|
1147
|
+
toml: str,
|
|
1148
|
+
ptr
|
|
1149
|
+
});
|
|
1150
|
+
}
|
|
1151
|
+
let c = str[ptr];
|
|
1152
|
+
if (c === "[" || c === "{") {
|
|
1153
|
+
let [value, endPtr2] = c === "[" ? parseArray(str, ptr, depth, integersAsBigInt) : parseInlineTable(str, ptr, depth, integersAsBigInt);
|
|
1154
|
+
if (end) {
|
|
1155
|
+
endPtr2 = skipVoid(str, endPtr2);
|
|
1156
|
+
if (str[endPtr2] === ",")
|
|
1157
|
+
endPtr2++;
|
|
1158
|
+
else if (str[endPtr2] !== end) {
|
|
1159
|
+
throw new TomlError("expected comma or end of structure", {
|
|
1160
|
+
toml: str,
|
|
1161
|
+
ptr: endPtr2
|
|
1162
|
+
});
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
return [value, endPtr2];
|
|
1166
|
+
}
|
|
1167
|
+
let endPtr;
|
|
1168
|
+
if (c === '"' || c === "'") {
|
|
1169
|
+
endPtr = getStringEnd(str, ptr);
|
|
1170
|
+
let parsed = parseString(str, ptr, endPtr);
|
|
1171
|
+
if (end) {
|
|
1172
|
+
endPtr = skipVoid(str, endPtr);
|
|
1173
|
+
if (str[endPtr] && str[endPtr] !== "," && str[endPtr] !== end && str[endPtr] !== "\n" && str[endPtr] !== "\r") {
|
|
1174
|
+
throw new TomlError("unexpected character encountered", {
|
|
1175
|
+
toml: str,
|
|
1176
|
+
ptr: endPtr
|
|
1177
|
+
});
|
|
1178
|
+
}
|
|
1179
|
+
endPtr += +(str[endPtr] === ",");
|
|
1180
|
+
}
|
|
1181
|
+
return [parsed, endPtr];
|
|
1182
|
+
}
|
|
1183
|
+
endPtr = skipUntil(str, ptr, ",", end);
|
|
1184
|
+
let slice = sliceAndTrimEndOf(str, ptr, endPtr - +(str[endPtr - 1] === ","));
|
|
1185
|
+
if (!slice[0]) {
|
|
1186
|
+
throw new TomlError("incomplete key-value declaration: no value specified", {
|
|
1187
|
+
toml: str,
|
|
1188
|
+
ptr
|
|
1189
|
+
});
|
|
1190
|
+
}
|
|
1191
|
+
if (end && slice[1] > -1) {
|
|
1192
|
+
endPtr = skipVoid(str, ptr + slice[1]);
|
|
1193
|
+
endPtr += +(str[endPtr] === ",");
|
|
1194
|
+
}
|
|
1195
|
+
return [
|
|
1196
|
+
parseValue(slice[0], str, ptr, integersAsBigInt),
|
|
1197
|
+
endPtr
|
|
1198
|
+
];
|
|
1199
|
+
}
|
|
1200
|
+
|
|
1201
|
+
// node_modules/.pnpm/smol-toml@1.6.0/node_modules/smol-toml/dist/struct.js
|
|
1202
|
+
var KEY_PART_RE = /^[a-zA-Z0-9-_]+[ \t]*$/;
|
|
1203
|
+
function parseKey(str, ptr, end = "=") {
|
|
1204
|
+
let dot = ptr - 1;
|
|
1205
|
+
let parsed = [];
|
|
1206
|
+
let endPtr = str.indexOf(end, ptr);
|
|
1207
|
+
if (endPtr < 0) {
|
|
1208
|
+
throw new TomlError("incomplete key-value: cannot find end of key", {
|
|
1209
|
+
toml: str,
|
|
1210
|
+
ptr
|
|
1211
|
+
});
|
|
1212
|
+
}
|
|
1213
|
+
do {
|
|
1214
|
+
let c = str[ptr = ++dot];
|
|
1215
|
+
if (c !== " " && c !== " ") {
|
|
1216
|
+
if (c === '"' || c === "'") {
|
|
1217
|
+
if (c === str[ptr + 1] && c === str[ptr + 2]) {
|
|
1218
|
+
throw new TomlError("multiline strings are not allowed in keys", {
|
|
1219
|
+
toml: str,
|
|
1220
|
+
ptr
|
|
1221
|
+
});
|
|
1222
|
+
}
|
|
1223
|
+
let eos = getStringEnd(str, ptr);
|
|
1224
|
+
if (eos < 0) {
|
|
1225
|
+
throw new TomlError("unfinished string encountered", {
|
|
1226
|
+
toml: str,
|
|
1227
|
+
ptr
|
|
1228
|
+
});
|
|
1229
|
+
}
|
|
1230
|
+
dot = str.indexOf(".", eos);
|
|
1231
|
+
let strEnd = str.slice(eos, dot < 0 || dot > endPtr ? endPtr : dot);
|
|
1232
|
+
let newLine = indexOfNewline(strEnd);
|
|
1233
|
+
if (newLine > -1) {
|
|
1234
|
+
throw new TomlError("newlines are not allowed in keys", {
|
|
1235
|
+
toml: str,
|
|
1236
|
+
ptr: ptr + dot + newLine
|
|
1237
|
+
});
|
|
1238
|
+
}
|
|
1239
|
+
if (strEnd.trimStart()) {
|
|
1240
|
+
throw new TomlError("found extra tokens after the string part", {
|
|
1241
|
+
toml: str,
|
|
1242
|
+
ptr: eos
|
|
1243
|
+
});
|
|
1244
|
+
}
|
|
1245
|
+
if (endPtr < eos) {
|
|
1246
|
+
endPtr = str.indexOf(end, eos);
|
|
1247
|
+
if (endPtr < 0) {
|
|
1248
|
+
throw new TomlError("incomplete key-value: cannot find end of key", {
|
|
1249
|
+
toml: str,
|
|
1250
|
+
ptr
|
|
1251
|
+
});
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
parsed.push(parseString(str, ptr, eos));
|
|
1255
|
+
} else {
|
|
1256
|
+
dot = str.indexOf(".", ptr);
|
|
1257
|
+
let part = str.slice(ptr, dot < 0 || dot > endPtr ? endPtr : dot);
|
|
1258
|
+
if (!KEY_PART_RE.test(part)) {
|
|
1259
|
+
throw new TomlError("only letter, numbers, dashes and underscores are allowed in keys", {
|
|
1260
|
+
toml: str,
|
|
1261
|
+
ptr
|
|
1262
|
+
});
|
|
1263
|
+
}
|
|
1264
|
+
parsed.push(part.trimEnd());
|
|
1265
|
+
}
|
|
1266
|
+
}
|
|
1267
|
+
} while (dot + 1 && dot < endPtr);
|
|
1268
|
+
return [parsed, skipVoid(str, endPtr + 1, true, true)];
|
|
1269
|
+
}
|
|
1270
|
+
function parseInlineTable(str, ptr, depth, integersAsBigInt) {
|
|
1271
|
+
let res = {};
|
|
1272
|
+
let seen = /* @__PURE__ */ new Set();
|
|
1273
|
+
let c;
|
|
1274
|
+
ptr++;
|
|
1275
|
+
while ((c = str[ptr++]) !== "}" && c) {
|
|
1276
|
+
if (c === ",") {
|
|
1277
|
+
throw new TomlError("expected value, found comma", {
|
|
1278
|
+
toml: str,
|
|
1279
|
+
ptr: ptr - 1
|
|
1280
|
+
});
|
|
1281
|
+
} else if (c === "#")
|
|
1282
|
+
ptr = skipComment(str, ptr);
|
|
1283
|
+
else if (c !== " " && c !== " " && c !== "\n" && c !== "\r") {
|
|
1284
|
+
let k;
|
|
1285
|
+
let t = res;
|
|
1286
|
+
let hasOwn = false;
|
|
1287
|
+
let [key, keyEndPtr] = parseKey(str, ptr - 1);
|
|
1288
|
+
for (let i = 0; i < key.length; i++) {
|
|
1289
|
+
if (i)
|
|
1290
|
+
t = hasOwn ? t[k] : t[k] = {};
|
|
1291
|
+
k = key[i];
|
|
1292
|
+
if ((hasOwn = Object.hasOwn(t, k)) && (typeof t[k] !== "object" || seen.has(t[k]))) {
|
|
1293
|
+
throw new TomlError("trying to redefine an already defined value", {
|
|
1294
|
+
toml: str,
|
|
1295
|
+
ptr
|
|
1296
|
+
});
|
|
1297
|
+
}
|
|
1298
|
+
if (!hasOwn && k === "__proto__") {
|
|
1299
|
+
Object.defineProperty(t, k, { enumerable: true, configurable: true, writable: true });
|
|
1300
|
+
}
|
|
1301
|
+
}
|
|
1302
|
+
if (hasOwn) {
|
|
1303
|
+
throw new TomlError("trying to redefine an already defined value", {
|
|
1304
|
+
toml: str,
|
|
1305
|
+
ptr
|
|
1306
|
+
});
|
|
1307
|
+
}
|
|
1308
|
+
let [value, valueEndPtr] = extractValue(str, keyEndPtr, "}", depth - 1, integersAsBigInt);
|
|
1309
|
+
seen.add(value);
|
|
1310
|
+
t[k] = value;
|
|
1311
|
+
ptr = valueEndPtr;
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
if (!c) {
|
|
1315
|
+
throw new TomlError("unfinished table encountered", {
|
|
1316
|
+
toml: str,
|
|
1317
|
+
ptr
|
|
1318
|
+
});
|
|
1319
|
+
}
|
|
1320
|
+
return [res, ptr];
|
|
1321
|
+
}
|
|
1322
|
+
function parseArray(str, ptr, depth, integersAsBigInt) {
|
|
1323
|
+
let res = [];
|
|
1324
|
+
let c;
|
|
1325
|
+
ptr++;
|
|
1326
|
+
while ((c = str[ptr++]) !== "]" && c) {
|
|
1327
|
+
if (c === ",") {
|
|
1328
|
+
throw new TomlError("expected value, found comma", {
|
|
1329
|
+
toml: str,
|
|
1330
|
+
ptr: ptr - 1
|
|
1331
|
+
});
|
|
1332
|
+
} else if (c === "#")
|
|
1333
|
+
ptr = skipComment(str, ptr);
|
|
1334
|
+
else if (c !== " " && c !== " " && c !== "\n" && c !== "\r") {
|
|
1335
|
+
let e = extractValue(str, ptr - 1, "]", depth - 1, integersAsBigInt);
|
|
1336
|
+
res.push(e[0]);
|
|
1337
|
+
ptr = e[1];
|
|
1338
|
+
}
|
|
1339
|
+
}
|
|
1340
|
+
if (!c) {
|
|
1341
|
+
throw new TomlError("unfinished array encountered", {
|
|
1342
|
+
toml: str,
|
|
1343
|
+
ptr
|
|
1344
|
+
});
|
|
1345
|
+
}
|
|
1346
|
+
return [res, ptr];
|
|
1347
|
+
}
|
|
1348
|
+
|
|
1349
|
+
// node_modules/.pnpm/smol-toml@1.6.0/node_modules/smol-toml/dist/parse.js
|
|
1350
|
+
function peekTable(key, table, meta, type) {
|
|
1351
|
+
let t = table;
|
|
1352
|
+
let m = meta;
|
|
1353
|
+
let k;
|
|
1354
|
+
let hasOwn = false;
|
|
1355
|
+
let state;
|
|
1356
|
+
for (let i = 0; i < key.length; i++) {
|
|
1357
|
+
if (i) {
|
|
1358
|
+
t = hasOwn ? t[k] : t[k] = {};
|
|
1359
|
+
m = (state = m[k]).c;
|
|
1360
|
+
if (type === 0 && (state.t === 1 || state.t === 2)) {
|
|
1361
|
+
return null;
|
|
1362
|
+
}
|
|
1363
|
+
if (state.t === 2) {
|
|
1364
|
+
let l = t.length - 1;
|
|
1365
|
+
t = t[l];
|
|
1366
|
+
m = m[l].c;
|
|
1367
|
+
}
|
|
1368
|
+
}
|
|
1369
|
+
k = key[i];
|
|
1370
|
+
if ((hasOwn = Object.hasOwn(t, k)) && m[k]?.t === 0 && m[k]?.d) {
|
|
1371
|
+
return null;
|
|
1372
|
+
}
|
|
1373
|
+
if (!hasOwn) {
|
|
1374
|
+
if (k === "__proto__") {
|
|
1375
|
+
Object.defineProperty(t, k, { enumerable: true, configurable: true, writable: true });
|
|
1376
|
+
Object.defineProperty(m, k, { enumerable: true, configurable: true, writable: true });
|
|
1377
|
+
}
|
|
1378
|
+
m[k] = {
|
|
1379
|
+
t: i < key.length - 1 && type === 2 ? 3 : type,
|
|
1380
|
+
d: false,
|
|
1381
|
+
i: 0,
|
|
1382
|
+
c: {}
|
|
1383
|
+
};
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
state = m[k];
|
|
1387
|
+
if (state.t !== type && !(type === 1 && state.t === 3)) {
|
|
1388
|
+
return null;
|
|
1389
|
+
}
|
|
1390
|
+
if (type === 2) {
|
|
1391
|
+
if (!state.d) {
|
|
1392
|
+
state.d = true;
|
|
1393
|
+
t[k] = [];
|
|
1394
|
+
}
|
|
1395
|
+
t[k].push(t = {});
|
|
1396
|
+
state.c[state.i++] = state = { t: 1, d: false, i: 0, c: {} };
|
|
1397
|
+
}
|
|
1398
|
+
if (state.d) {
|
|
1399
|
+
return null;
|
|
1400
|
+
}
|
|
1401
|
+
state.d = true;
|
|
1402
|
+
if (type === 1) {
|
|
1403
|
+
t = hasOwn ? t[k] : t[k] = {};
|
|
1404
|
+
} else if (type === 0 && hasOwn) {
|
|
1405
|
+
return null;
|
|
1406
|
+
}
|
|
1407
|
+
return [k, t, state.c];
|
|
1408
|
+
}
|
|
1409
|
+
function parse(toml, { maxDepth = 1e3, integersAsBigInt } = {}) {
|
|
1410
|
+
let res = {};
|
|
1411
|
+
let meta = {};
|
|
1412
|
+
let tbl = res;
|
|
1413
|
+
let m = meta;
|
|
1414
|
+
for (let ptr = skipVoid(toml, 0); ptr < toml.length; ) {
|
|
1415
|
+
if (toml[ptr] === "[") {
|
|
1416
|
+
let isTableArray = toml[++ptr] === "[";
|
|
1417
|
+
let k = parseKey(toml, ptr += +isTableArray, "]");
|
|
1418
|
+
if (isTableArray) {
|
|
1419
|
+
if (toml[k[1] - 1] !== "]") {
|
|
1420
|
+
throw new TomlError("expected end of table declaration", {
|
|
1421
|
+
toml,
|
|
1422
|
+
ptr: k[1] - 1
|
|
1423
|
+
});
|
|
1424
|
+
}
|
|
1425
|
+
k[1]++;
|
|
1426
|
+
}
|
|
1427
|
+
let p = peekTable(
|
|
1428
|
+
k[0],
|
|
1429
|
+
res,
|
|
1430
|
+
meta,
|
|
1431
|
+
isTableArray ? 2 : 1
|
|
1432
|
+
/* Type.EXPLICIT */
|
|
1433
|
+
);
|
|
1434
|
+
if (!p) {
|
|
1435
|
+
throw new TomlError("trying to redefine an already defined table or value", {
|
|
1436
|
+
toml,
|
|
1437
|
+
ptr
|
|
1438
|
+
});
|
|
1439
|
+
}
|
|
1440
|
+
m = p[2];
|
|
1441
|
+
tbl = p[1];
|
|
1442
|
+
ptr = k[1];
|
|
1443
|
+
} else {
|
|
1444
|
+
let k = parseKey(toml, ptr);
|
|
1445
|
+
let p = peekTable(
|
|
1446
|
+
k[0],
|
|
1447
|
+
tbl,
|
|
1448
|
+
m,
|
|
1449
|
+
0
|
|
1450
|
+
/* Type.DOTTED */
|
|
1451
|
+
);
|
|
1452
|
+
if (!p) {
|
|
1453
|
+
throw new TomlError("trying to redefine an already defined table or value", {
|
|
1454
|
+
toml,
|
|
1455
|
+
ptr
|
|
1456
|
+
});
|
|
1457
|
+
}
|
|
1458
|
+
let v = extractValue(toml, k[1], void 0, maxDepth, integersAsBigInt);
|
|
1459
|
+
p[1][p[0]] = v[0];
|
|
1460
|
+
ptr = v[1];
|
|
1461
|
+
}
|
|
1462
|
+
ptr = skipVoid(toml, ptr, true);
|
|
1463
|
+
if (toml[ptr] && toml[ptr] !== "\n" && toml[ptr] !== "\r") {
|
|
1464
|
+
throw new TomlError("each key-value declaration must be followed by an end-of-line", {
|
|
1465
|
+
toml,
|
|
1466
|
+
ptr
|
|
1467
|
+
});
|
|
1468
|
+
}
|
|
1469
|
+
ptr = skipVoid(toml, ptr);
|
|
1470
|
+
}
|
|
1471
|
+
return res;
|
|
1472
|
+
}
|
|
1473
|
+
|
|
1474
|
+
// packages/runtime/src/config.ts
|
|
1475
|
+
function defaultWorkerConfig() {
|
|
1476
|
+
return {
|
|
1477
|
+
enabled: false,
|
|
1478
|
+
primary_url: "http://localhost:3000",
|
|
1479
|
+
worker_port: 3100,
|
|
1480
|
+
worker_id: `worker-${process.pid}`,
|
|
1481
|
+
max_contexts: 4,
|
|
1482
|
+
task_poll_interval_ms: 5e3,
|
|
1483
|
+
status_report_interval_ms: 15e3
|
|
1484
|
+
};
|
|
1485
|
+
}
|
|
1486
|
+
function defaultSentinelConfig() {
|
|
1487
|
+
return {
|
|
1488
|
+
poll_interval_ms: 5e3,
|
|
1489
|
+
health_timeout_ms: 3e3,
|
|
1490
|
+
max_restart_attempts: 5,
|
|
1491
|
+
backoff_base_ms: 5e3,
|
|
1492
|
+
backoff_max_ms: 3e5,
|
|
1493
|
+
agent_startup_grace_ms: 15e3,
|
|
1494
|
+
health_port: 9484,
|
|
1495
|
+
pid_file: "/tmp/hivemind-agent.pid",
|
|
1496
|
+
stop_flag_file: "/tmp/hivemind-agent.stopped"
|
|
1497
|
+
};
|
|
1498
|
+
}
|
|
1499
|
+
function deepMerge(target, source) {
|
|
1500
|
+
const result = { ...target };
|
|
1501
|
+
for (const key of Object.keys(source)) {
|
|
1502
|
+
if (source[key] && typeof source[key] === "object" && !Array.isArray(source[key]) && target[key] && typeof target[key] === "object") {
|
|
1503
|
+
result[key] = deepMerge(target[key], source[key]);
|
|
1504
|
+
} else if (source[key] !== void 0 && source[key] !== "") {
|
|
1505
|
+
result[key] = source[key];
|
|
1506
|
+
}
|
|
1507
|
+
}
|
|
1508
|
+
return result;
|
|
1509
|
+
}
|
|
1510
|
+
function loadConfig(path) {
|
|
1511
|
+
const raw = readFileSync2(path, "utf-8");
|
|
1512
|
+
let parsed = parse(raw);
|
|
1513
|
+
const configDir = dirname(path);
|
|
1514
|
+
const localPath = resolve2(configDir, "local.toml");
|
|
1515
|
+
if (existsSync2(localPath)) {
|
|
1516
|
+
const localRaw = readFileSync2(localPath, "utf-8");
|
|
1517
|
+
const localParsed = parse(localRaw);
|
|
1518
|
+
parsed = deepMerge(parsed, localParsed);
|
|
1519
|
+
console.log(`[config] Merged overrides from ${localPath}`);
|
|
1520
|
+
}
|
|
1521
|
+
if (process.env.AGENT_NAME) {
|
|
1522
|
+
parsed.agent.name = process.env.AGENT_NAME;
|
|
1523
|
+
}
|
|
1524
|
+
if (process.env.LLM_API_KEY) {
|
|
1525
|
+
parsed.llm.api_key = process.env.LLM_API_KEY;
|
|
1526
|
+
}
|
|
1527
|
+
if (process.env.LLM_BASE_URL) {
|
|
1528
|
+
parsed.llm.base_url = process.env.LLM_BASE_URL;
|
|
1529
|
+
}
|
|
1530
|
+
if (process.env.SESAME_API_KEY) {
|
|
1531
|
+
parsed.sesame.api_key = process.env.SESAME_API_KEY;
|
|
1532
|
+
}
|
|
1533
|
+
if (process.env.MEMORY_DAEMON_URL) {
|
|
1534
|
+
parsed.memory.daemon_url = process.env.MEMORY_DAEMON_URL;
|
|
1535
|
+
}
|
|
1536
|
+
if (process.env.WORKER_PRIMARY_URL) {
|
|
1537
|
+
if (!parsed.worker) parsed.worker = defaultWorkerConfig();
|
|
1538
|
+
parsed.worker.primary_url = process.env.WORKER_PRIMARY_URL;
|
|
1539
|
+
}
|
|
1540
|
+
if (process.env.WORKER_PORT) {
|
|
1541
|
+
if (!parsed.worker) parsed.worker = defaultWorkerConfig();
|
|
1542
|
+
parsed.worker.worker_port = parseInt(process.env.WORKER_PORT, 10);
|
|
1543
|
+
}
|
|
1544
|
+
if (process.env.WORKER_ID) {
|
|
1545
|
+
if (!parsed.worker) parsed.worker = defaultWorkerConfig();
|
|
1546
|
+
parsed.worker.worker_id = process.env.WORKER_ID;
|
|
1547
|
+
}
|
|
1548
|
+
if (process.env.SENTINEL_POLL_INTERVAL_MS) {
|
|
1549
|
+
if (!parsed.sentinel) parsed.sentinel = defaultSentinelConfig();
|
|
1550
|
+
parsed.sentinel.poll_interval_ms = parseInt(process.env.SENTINEL_POLL_INTERVAL_MS, 10);
|
|
1551
|
+
}
|
|
1552
|
+
if (process.env.SENTINEL_HEALTH_PORT) {
|
|
1553
|
+
if (!parsed.sentinel) parsed.sentinel = defaultSentinelConfig();
|
|
1554
|
+
parsed.sentinel.health_port = parseInt(process.env.SENTINEL_HEALTH_PORT, 10);
|
|
1555
|
+
}
|
|
1556
|
+
if (process.env.SENTINEL_PID_FILE) {
|
|
1557
|
+
if (!parsed.sentinel) parsed.sentinel = defaultSentinelConfig();
|
|
1558
|
+
parsed.sentinel.pid_file = process.env.SENTINEL_PID_FILE;
|
|
1559
|
+
}
|
|
1560
|
+
if (process.env.SENTINEL_STOP_FLAG_FILE) {
|
|
1561
|
+
if (!parsed.sentinel) parsed.sentinel = defaultSentinelConfig();
|
|
1562
|
+
parsed.sentinel.stop_flag_file = process.env.SENTINEL_STOP_FLAG_FILE;
|
|
1563
|
+
}
|
|
1564
|
+
if (parsed.agent.workspace && !parsed.agent.workspace.startsWith("/")) {
|
|
1565
|
+
const configDir2 = dirname(path);
|
|
1566
|
+
parsed.agent.workspace = resolve2(configDir2, "..", parsed.agent.workspace);
|
|
1567
|
+
}
|
|
1568
|
+
return parsed;
|
|
1569
|
+
}
|
|
1570
|
+
|
|
1571
|
+
// packages/runtime/src/sesame.ts
|
|
1572
|
+
import { readFileSync as readFileSync3 } from "fs";
|
|
1573
|
+
import { resolve as resolve3, dirname as dirname2 } from "path";
|
|
1574
|
+
import { fileURLToPath } from "url";
|
|
1575
|
+
var RUNTIME_VERSION = "unknown";
|
|
1576
|
+
try {
|
|
1577
|
+
const __dirname = dirname2(fileURLToPath(import.meta.url));
|
|
1578
|
+
const pkg = JSON.parse(readFileSync3(resolve3(__dirname, "../package.json"), "utf-8"));
|
|
1579
|
+
RUNTIME_VERSION = pkg.version ?? "unknown";
|
|
1580
|
+
} catch {
|
|
1581
|
+
}
|
|
1582
|
+
var SesameClient2 = class {
|
|
1583
|
+
config;
|
|
1584
|
+
sdk;
|
|
1585
|
+
messageHandler = null;
|
|
1586
|
+
upgradeHandler = null;
|
|
1587
|
+
restartHandler = null;
|
|
1588
|
+
agentId = null;
|
|
1589
|
+
channels = /* @__PURE__ */ new Map();
|
|
1590
|
+
typingIntervals = /* @__PURE__ */ new Map();
|
|
1591
|
+
constructor(config) {
|
|
1592
|
+
this.config = config;
|
|
1593
|
+
this.sdk = new SesameClient({
|
|
1594
|
+
apiUrl: config.api_url.replace(/\/api\/v1$/, ""),
|
|
1595
|
+
wsUrl: config.ws_url,
|
|
1596
|
+
apiKey: config.api_key
|
|
1597
|
+
});
|
|
1598
|
+
}
|
|
1599
|
+
onMessage(handler) {
|
|
1600
|
+
this.messageHandler = handler;
|
|
1601
|
+
}
|
|
1602
|
+
onUpgrade(handler) {
|
|
1603
|
+
this.upgradeHandler = handler;
|
|
1604
|
+
}
|
|
1605
|
+
onRestart(handler) {
|
|
1606
|
+
this.restartHandler = handler;
|
|
1607
|
+
}
|
|
1608
|
+
async connect() {
|
|
1609
|
+
const manifest = await this.sdk.getManifest();
|
|
1610
|
+
this.agentId = manifest.agent.id;
|
|
1611
|
+
console.log(`[sesame] Authenticated as ${manifest.agent.handle} (${this.agentId})`);
|
|
1612
|
+
for (const ch of manifest.channels) {
|
|
1613
|
+
this.channels.set(ch.id, { kind: ch.kind, name: ch.name ?? void 0 });
|
|
1614
|
+
const label = ch.name || ch.id.slice(0, 8);
|
|
1615
|
+
console.log(`[sesame] Channel: ${label} (${ch.kind})`);
|
|
1616
|
+
}
|
|
1617
|
+
this.updatePresence("online", { emoji: "\u{1F7E2}" });
|
|
1618
|
+
this.sdk.on("upgrade.request", (event) => {
|
|
1619
|
+
console.log(`[sesame] Upgrade request received: ${event.packageName}@${event.targetVersion}`);
|
|
1620
|
+
if (this.upgradeHandler) {
|
|
1621
|
+
this.upgradeHandler({
|
|
1622
|
+
targetVersion: event.targetVersion,
|
|
1623
|
+
packageName: event.packageName,
|
|
1624
|
+
requestedBy: event.requestedBy,
|
|
1625
|
+
requestId: event.requestId
|
|
1626
|
+
});
|
|
1627
|
+
}
|
|
1628
|
+
});
|
|
1629
|
+
this.sdk.on("restart.request", (event) => {
|
|
1630
|
+
console.log(`[sesame] Restart requested by ${event.requestedBy}`);
|
|
1631
|
+
if (this.restartHandler) {
|
|
1632
|
+
this.restartHandler({
|
|
1633
|
+
requestedBy: event.requestedBy,
|
|
1634
|
+
requestId: event.requestId
|
|
1635
|
+
});
|
|
1636
|
+
}
|
|
1637
|
+
});
|
|
1638
|
+
this.sdk.on("control", (event) => {
|
|
1639
|
+
console.log(`[sesame] Control event: ${event.action}`, event.payload);
|
|
1640
|
+
});
|
|
1641
|
+
this.sdk.on("message", (event) => {
|
|
1642
|
+
const msg = event.data || event.message || event;
|
|
1643
|
+
const senderId = msg.senderId || msg.sender?.id;
|
|
1644
|
+
if (senderId === this.agentId) return;
|
|
1645
|
+
if (!this.messageHandler || !msg.content) return;
|
|
1646
|
+
const channelInfo = this.channels.get(msg.channelId);
|
|
1647
|
+
this.messageHandler({
|
|
1648
|
+
id: msg.id || "unknown",
|
|
1649
|
+
channelId: msg.channelId || "unknown",
|
|
1650
|
+
channelKind: channelInfo?.kind || "dm",
|
|
1651
|
+
content: msg.content,
|
|
1652
|
+
author: {
|
|
1653
|
+
id: senderId || "unknown",
|
|
1654
|
+
handle: msg.senderHandle || msg.metadata?.senderHandle || "unknown"
|
|
1655
|
+
},
|
|
1656
|
+
timestamp: msg.createdAt || (/* @__PURE__ */ new Date()).toISOString()
|
|
1657
|
+
});
|
|
1658
|
+
});
|
|
1659
|
+
await this.sdk.connect();
|
|
1660
|
+
console.log("[sesame] WebSocket connected");
|
|
1661
|
+
const ws = this.sdk.ws;
|
|
1662
|
+
if (ws?.readyState === 1) {
|
|
1663
|
+
ws.send(JSON.stringify({ type: "meta", runtime: "hivemind", version: RUNTIME_VERSION }));
|
|
1664
|
+
console.log(`[sesame] Reported version: hivemind@${RUNTIME_VERSION}`);
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1667
|
+
// ── Typing Indicators ──
|
|
1668
|
+
/**
|
|
1669
|
+
* Start sending typing indicators for a channel.
|
|
1670
|
+
* Sends immediately, then every 2.5s until stopTyping() is called.
|
|
1671
|
+
*/
|
|
1672
|
+
startTyping(channelId) {
|
|
1673
|
+
if (this.typingIntervals.has(channelId)) return;
|
|
1674
|
+
this.sdk.sendTyping(channelId);
|
|
1675
|
+
const interval = setInterval(() => {
|
|
1676
|
+
this.sdk.sendTyping(channelId);
|
|
1677
|
+
}, 2500);
|
|
1678
|
+
this.typingIntervals.set(channelId, interval);
|
|
1679
|
+
}
|
|
1680
|
+
/**
|
|
1681
|
+
* Stop sending typing indicators for a channel.
|
|
1682
|
+
*/
|
|
1683
|
+
stopTyping(channelId) {
|
|
1684
|
+
const interval = this.typingIntervals.get(channelId);
|
|
1685
|
+
if (interval) {
|
|
1686
|
+
clearInterval(interval);
|
|
1687
|
+
this.typingIntervals.delete(channelId);
|
|
1688
|
+
}
|
|
1689
|
+
}
|
|
1690
|
+
/**
|
|
1691
|
+
* Stop all active typing indicators.
|
|
1692
|
+
*/
|
|
1693
|
+
stopAllTyping() {
|
|
1694
|
+
for (const [channelId, interval] of this.typingIntervals) {
|
|
1695
|
+
clearInterval(interval);
|
|
1696
|
+
}
|
|
1697
|
+
this.typingIntervals.clear();
|
|
1698
|
+
}
|
|
1699
|
+
// ── Presence ──
|
|
1700
|
+
/**
|
|
1701
|
+
* Update agent presence/status.
|
|
1702
|
+
* @param status - "online" | "thinking" | "working" | "idle" | "offline"
|
|
1703
|
+
* @param options - Optional detail text, progress (0-100), emoji
|
|
1704
|
+
*/
|
|
1705
|
+
updatePresence(status, options) {
|
|
1706
|
+
if (typeof this.sdk.updatePresence === "function") {
|
|
1707
|
+
this.sdk.updatePresence(status, options);
|
|
1708
|
+
} else {
|
|
1709
|
+
const ws = this.sdk.ws;
|
|
1710
|
+
if (ws?.readyState === 1) {
|
|
1711
|
+
ws.send(JSON.stringify({ type: "status", status, ...options }));
|
|
1712
|
+
}
|
|
1713
|
+
}
|
|
1714
|
+
}
|
|
1715
|
+
// ── Read Receipts ──
|
|
1716
|
+
/**
|
|
1717
|
+
* Mark a channel as read up to a given sequence number.
|
|
1718
|
+
*/
|
|
1719
|
+
async markRead(channelId, seq) {
|
|
1720
|
+
await this.sdk.markRead(channelId, seq);
|
|
1721
|
+
}
|
|
1722
|
+
// ── Messages ──
|
|
1723
|
+
async sendMessage(channelId, content) {
|
|
1724
|
+
await this.sdk.sendMessage(channelId, { content });
|
|
1725
|
+
}
|
|
1726
|
+
getAgentId() {
|
|
1727
|
+
return this.agentId;
|
|
1728
|
+
}
|
|
1729
|
+
getChannelInfo(channelId) {
|
|
1730
|
+
return this.channels.get(channelId);
|
|
1731
|
+
}
|
|
1732
|
+
disconnect() {
|
|
1733
|
+
this.stopAllTyping();
|
|
1734
|
+
this.updatePresence("offline");
|
|
1735
|
+
this.sdk.disconnect();
|
|
1736
|
+
}
|
|
1737
|
+
};
|
|
1738
|
+
|
|
1739
|
+
// packages/runtime/src/pipeline.ts
|
|
1740
|
+
import { createServer } from "http";
|
|
1741
|
+
|
|
1742
|
+
// packages/runtime/src/health.ts
|
|
1743
|
+
var HEALTH_PORT = 9484;
|
|
1744
|
+
var HEALTH_PATH = "/health";
|
|
1745
|
+
|
|
1746
|
+
// packages/runtime/src/pipeline.ts
|
|
1747
|
+
import { readFileSync as readFileSync4, writeFileSync, unlinkSync } from "fs";
|
|
1748
|
+
import { resolve as resolve4, dirname as dirname3 } from "path";
|
|
1749
|
+
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
1750
|
+
var PACKAGE_VERSION = "unknown";
|
|
1751
|
+
try {
|
|
1752
|
+
const __dirname = dirname3(fileURLToPath2(import.meta.url));
|
|
1753
|
+
const pkg = JSON.parse(readFileSync4(resolve4(__dirname, "../package.json"), "utf-8"));
|
|
1754
|
+
PACKAGE_VERSION = pkg.version ?? "unknown";
|
|
1755
|
+
} catch {
|
|
1756
|
+
}
|
|
1757
|
+
var sesameConnected = false;
|
|
1758
|
+
var memoryConnected = false;
|
|
1759
|
+
var startTime = Date.now();
|
|
1760
|
+
function startHealthServer(port) {
|
|
1761
|
+
const server = createServer((req, res) => {
|
|
1762
|
+
if (req.method === "GET" && req.url === HEALTH_PATH) {
|
|
1763
|
+
const status = {
|
|
1764
|
+
status: sesameConnected ? "ok" : "degraded",
|
|
1765
|
+
pid: process.pid,
|
|
1766
|
+
uptime_s: Math.floor((Date.now() - startTime) / 1e3),
|
|
1767
|
+
sesame_connected: sesameConnected,
|
|
1768
|
+
memory_connected: memoryConnected,
|
|
1769
|
+
version: PACKAGE_VERSION
|
|
1770
|
+
};
|
|
1771
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
1772
|
+
res.end(JSON.stringify(status));
|
|
1773
|
+
} else {
|
|
1774
|
+
res.writeHead(404);
|
|
1775
|
+
res.end();
|
|
1776
|
+
}
|
|
1777
|
+
});
|
|
1778
|
+
server.listen(port, "127.0.0.1", () => {
|
|
1779
|
+
console.log(`[hivemind] Health endpoint listening on http://127.0.0.1:${port}${HEALTH_PATH}`);
|
|
1780
|
+
});
|
|
1781
|
+
return server;
|
|
1782
|
+
}
|
|
1783
|
+
function writePidFile(path) {
|
|
1784
|
+
writeFileSync(path, String(process.pid));
|
|
1785
|
+
console.log(`[hivemind] PID file written: ${path}`);
|
|
1786
|
+
}
|
|
1787
|
+
function cleanupPidFile(path) {
|
|
1788
|
+
try {
|
|
1789
|
+
unlinkSync(path);
|
|
1790
|
+
} catch {
|
|
1791
|
+
}
|
|
1792
|
+
}
|
|
1793
|
+
async function startPipeline(configPath) {
|
|
1794
|
+
const config = loadConfig(configPath);
|
|
1795
|
+
const sentinel = config.sentinel ?? defaultSentinelConfig();
|
|
1796
|
+
const healthPort = sentinel.health_port || HEALTH_PORT;
|
|
1797
|
+
const pidFile = sentinel.pid_file;
|
|
1798
|
+
console.log(`[hivemind] Starting ${config.agent.name} (pid ${process.pid})`);
|
|
1799
|
+
writePidFile(pidFile);
|
|
1800
|
+
const healthServer = startHealthServer(healthPort);
|
|
1801
|
+
const cleanupOnExit = () => {
|
|
1802
|
+
cleanupPidFile(pidFile);
|
|
1803
|
+
healthServer.close();
|
|
1804
|
+
};
|
|
1805
|
+
process.on("exit", cleanupOnExit);
|
|
1806
|
+
const memory = new MemoryClient(config.memory);
|
|
1807
|
+
const memoryOk = await memory.healthCheck();
|
|
1808
|
+
if (!memoryOk) {
|
|
1809
|
+
console.warn("[hivemind] Memory daemon unreachable at", config.memory.daemon_url);
|
|
1810
|
+
console.warn("[hivemind] Continuing without persistent memory \u2014 episodes will not be stored");
|
|
1811
|
+
} else {
|
|
1812
|
+
memoryConnected = true;
|
|
1813
|
+
console.log("[hivemind] Memory daemon connected");
|
|
1814
|
+
}
|
|
1815
|
+
const agent = new Agent(config);
|
|
1816
|
+
console.log(`[hivemind] Context manager initialized (active: ${agent.getActiveContext()})`);
|
|
1817
|
+
if (config.sesame.api_key) {
|
|
1818
|
+
await startSesameLoop(config, agent);
|
|
1819
|
+
} else {
|
|
1820
|
+
console.log("[hivemind] No Sesame API key configured \u2014 running in stdin mode");
|
|
1821
|
+
await startStdinLoop(agent);
|
|
1822
|
+
}
|
|
1823
|
+
}
|
|
1824
|
+
async function startSesameLoop(config, agent) {
|
|
1825
|
+
const sesame = new SesameClient2(config.sesame);
|
|
1826
|
+
let shuttingDown = false;
|
|
1827
|
+
const shutdown = (signal) => {
|
|
1828
|
+
if (shuttingDown) return;
|
|
1829
|
+
shuttingDown = true;
|
|
1830
|
+
console.log(`
|
|
1831
|
+
[hivemind] Received ${signal}, shutting down...`);
|
|
1832
|
+
try {
|
|
1833
|
+
sesame.updatePresence("offline", { emoji: "\u2B58" });
|
|
1834
|
+
sesame.disconnect();
|
|
1835
|
+
console.log("[hivemind] Sesame disconnected cleanly");
|
|
1836
|
+
} catch (err) {
|
|
1837
|
+
console.error("[hivemind] Error during disconnect:", err.message);
|
|
1838
|
+
}
|
|
1839
|
+
process.exit(0);
|
|
1840
|
+
};
|
|
1841
|
+
process.on("SIGTERM", () => shutdown("SIGTERM"));
|
|
1842
|
+
process.on("SIGINT", () => shutdown("SIGINT"));
|
|
1843
|
+
sesame.onMessage(async (msg) => {
|
|
1844
|
+
if (shuttingDown) return;
|
|
1845
|
+
console.log(`[sesame] ${msg.author.handle} (${msg.channelKind}): ${msg.content}`);
|
|
1846
|
+
sesame.startTyping(msg.channelId);
|
|
1847
|
+
sesame.updatePresence("thinking", { detail: `Replying to ${msg.author.handle}`, emoji: "\u{1F4AD}" });
|
|
1848
|
+
try {
|
|
1849
|
+
const prefix = msg.channelKind === "group" ? `[${msg.author.handle} in group chat]: ` : `[${msg.author.handle}]: `;
|
|
1850
|
+
const response = await agent.processMessage(prefix + msg.content);
|
|
1851
|
+
sesame.stopTyping(msg.channelId);
|
|
1852
|
+
if (response.content.trim() === "__SKIP__") {
|
|
1853
|
+
console.log(`[sesame] ${config.agent.name}: skipped (${msg.author.handle} in ${msg.channelKind})`);
|
|
1854
|
+
sesame.updatePresence("online", { emoji: "\u{1F7E2}" });
|
|
1855
|
+
return;
|
|
1856
|
+
}
|
|
1857
|
+
const ctxPrefix = response.contextSwitched ? `[switched to ${response.context}] ` : "";
|
|
1858
|
+
await sesame.sendMessage(msg.channelId, ctxPrefix + response.content);
|
|
1859
|
+
console.log(`[sesame] ${config.agent.name} (${response.context}): ${response.content.slice(0, 100)}...`);
|
|
1860
|
+
sesame.updatePresence("online", { emoji: "\u{1F7E2}" });
|
|
1861
|
+
} catch (err) {
|
|
1862
|
+
sesame.stopTyping(msg.channelId);
|
|
1863
|
+
sesame.updatePresence("online", { emoji: "\u{1F7E2}" });
|
|
1864
|
+
console.error("[sesame] Error processing message:", err.message);
|
|
1865
|
+
}
|
|
1866
|
+
});
|
|
1867
|
+
await sesame.connect();
|
|
1868
|
+
sesameConnected = true;
|
|
1869
|
+
console.log("[hivemind] Listening for Sesame messages");
|
|
1870
|
+
await new Promise(() => {
|
|
1871
|
+
});
|
|
1872
|
+
}
|
|
1873
|
+
async function startStdinLoop(agent) {
|
|
1874
|
+
const readline = await import("readline");
|
|
1875
|
+
const rl = readline.createInterface({
|
|
1876
|
+
input: process.stdin,
|
|
1877
|
+
output: process.stdout
|
|
1878
|
+
});
|
|
1879
|
+
console.log("[hivemind] Ready. Type a message (Ctrl+C to exit)");
|
|
1880
|
+
console.log("[hivemind] Commands: 'switch to <name>', 'list contexts', 'create context <name>'\n");
|
|
1881
|
+
rl.on("line", async (line) => {
|
|
1882
|
+
const input = line.trim();
|
|
1883
|
+
if (!input) return;
|
|
1884
|
+
if (input.toLowerCase() === "list contexts") {
|
|
1885
|
+
const contexts = agent.getContextManager().listContexts();
|
|
1886
|
+
console.log("\nContexts:");
|
|
1887
|
+
for (const ctx of contexts) {
|
|
1888
|
+
const active = ctx.name === agent.getActiveContext() ? " (active)" : "";
|
|
1889
|
+
console.log(` - ${ctx.name}${active}: ${ctx.description || "(no description)"}`);
|
|
1890
|
+
}
|
|
1891
|
+
console.log();
|
|
1892
|
+
return;
|
|
1893
|
+
}
|
|
1894
|
+
const createMatch = input.match(/^create context (\S+)\s*(.*)?$/i);
|
|
1895
|
+
if (createMatch) {
|
|
1896
|
+
const name = createMatch[1];
|
|
1897
|
+
const desc = createMatch[2] || "";
|
|
1898
|
+
await agent.getContextManager().createContext(name, desc);
|
|
1899
|
+
console.log(`
|
|
1900
|
+
Created context: ${name}
|
|
1901
|
+
`);
|
|
1902
|
+
return;
|
|
1903
|
+
}
|
|
1904
|
+
try {
|
|
1905
|
+
const response = await agent.processMessage(input);
|
|
1906
|
+
if (response.contextSwitched) {
|
|
1907
|
+
console.log(`
|
|
1908
|
+
[switched to context: ${response.context}]`);
|
|
1909
|
+
}
|
|
1910
|
+
console.log(`
|
|
1911
|
+
${response.content}
|
|
1912
|
+
`);
|
|
1913
|
+
} catch (err) {
|
|
1914
|
+
console.error("Error:", err.message);
|
|
1915
|
+
}
|
|
1916
|
+
});
|
|
1917
|
+
return new Promise((resolve5) => {
|
|
1918
|
+
rl.on("close", resolve5);
|
|
1919
|
+
});
|
|
1920
|
+
}
|
|
1921
|
+
|
|
1922
|
+
// packages/runtime/src/fleet/worker-server.ts
|
|
1923
|
+
import { createServer as createServer2 } from "http";
|
|
1924
|
+
var WorkerServer = class {
|
|
1925
|
+
server = null;
|
|
1926
|
+
workerId;
|
|
1927
|
+
port;
|
|
1928
|
+
startTime;
|
|
1929
|
+
assignedContexts = /* @__PURE__ */ new Map();
|
|
1930
|
+
activeContext = null;
|
|
1931
|
+
currentTask = null;
|
|
1932
|
+
maxContexts;
|
|
1933
|
+
memoryDaemonUrl;
|
|
1934
|
+
ollamaUrl;
|
|
1935
|
+
onAssignCallback = null;
|
|
1936
|
+
onSyncPushCallback = null;
|
|
1937
|
+
constructor(opts) {
|
|
1938
|
+
this.workerId = opts.workerId;
|
|
1939
|
+
this.port = opts.port;
|
|
1940
|
+
this.startTime = Date.now();
|
|
1941
|
+
this.maxContexts = opts.maxContexts ?? 4;
|
|
1942
|
+
this.memoryDaemonUrl = opts.memoryDaemonUrl ?? null;
|
|
1943
|
+
this.ollamaUrl = opts.ollamaUrl ?? null;
|
|
1944
|
+
}
|
|
1945
|
+
/** Start listening. */
|
|
1946
|
+
async start() {
|
|
1947
|
+
return new Promise((resolve5, reject) => {
|
|
1948
|
+
this.server = createServer2((req, res) => this.handleRequest(req, res));
|
|
1949
|
+
this.server.on("error", reject);
|
|
1950
|
+
this.server.listen(this.port, () => resolve5());
|
|
1951
|
+
});
|
|
1952
|
+
}
|
|
1953
|
+
/** Stop the server. */
|
|
1954
|
+
async stop() {
|
|
1955
|
+
return new Promise((resolve5) => {
|
|
1956
|
+
if (!this.server) {
|
|
1957
|
+
resolve5();
|
|
1958
|
+
return;
|
|
1959
|
+
}
|
|
1960
|
+
this.server.close(() => resolve5());
|
|
1961
|
+
});
|
|
1962
|
+
}
|
|
1963
|
+
getPort() {
|
|
1964
|
+
return this.port;
|
|
1965
|
+
}
|
|
1966
|
+
getAssignedContexts() {
|
|
1967
|
+
return Array.from(this.assignedContexts.keys());
|
|
1968
|
+
}
|
|
1969
|
+
setActiveContext(name) {
|
|
1970
|
+
this.activeContext = name;
|
|
1971
|
+
}
|
|
1972
|
+
setCurrentTask(taskId) {
|
|
1973
|
+
this.currentTask = taskId;
|
|
1974
|
+
}
|
|
1975
|
+
/** Register a callback for when a new context is assigned. */
|
|
1976
|
+
onContextAssigned(cb) {
|
|
1977
|
+
this.onAssignCallback = cb;
|
|
1978
|
+
}
|
|
1979
|
+
/** Register a handler for incoming sync push requests from Primary. */
|
|
1980
|
+
onSyncPush(cb) {
|
|
1981
|
+
this.onSyncPushCallback = cb;
|
|
1982
|
+
}
|
|
1983
|
+
// --- Request Router ---
|
|
1984
|
+
async handleRequest(req, res) {
|
|
1985
|
+
const url = new URL(req.url ?? "/", `http://localhost:${this.port}`);
|
|
1986
|
+
const path = url.pathname;
|
|
1987
|
+
const method = req.method ?? "GET";
|
|
1988
|
+
try {
|
|
1989
|
+
if (method === "GET" && path === "/health") {
|
|
1990
|
+
return this.handleHealth(res);
|
|
1991
|
+
}
|
|
1992
|
+
if (method === "POST" && path === "/assign") {
|
|
1993
|
+
const body = await readBody(req);
|
|
1994
|
+
return this.handleAssign(body, res);
|
|
1995
|
+
}
|
|
1996
|
+
if (method === "DELETE" && path.startsWith("/assign/")) {
|
|
1997
|
+
const contextName = decodeURIComponent(path.slice("/assign/".length));
|
|
1998
|
+
return this.handleUnassign(contextName, res);
|
|
1999
|
+
}
|
|
2000
|
+
if (method === "GET" && path === "/status") {
|
|
2001
|
+
return this.handleStatus(res);
|
|
2002
|
+
}
|
|
2003
|
+
if (method === "POST" && path === "/sync/push") {
|
|
2004
|
+
const body = await readBody(req);
|
|
2005
|
+
return this.handleSyncPush(body, res);
|
|
2006
|
+
}
|
|
2007
|
+
sendJson(res, 404, { error: "Not found" });
|
|
2008
|
+
} catch (err) {
|
|
2009
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
2010
|
+
sendJson(res, 500, { error: msg });
|
|
2011
|
+
}
|
|
2012
|
+
}
|
|
2013
|
+
// --- Handlers ---
|
|
2014
|
+
handleHealth(res) {
|
|
2015
|
+
const uptimeMs = Date.now() - this.startTime;
|
|
2016
|
+
const body = {
|
|
2017
|
+
worker_id: this.workerId,
|
|
2018
|
+
status: "healthy",
|
|
2019
|
+
uptime_seconds: Math.floor(uptimeMs / 1e3),
|
|
2020
|
+
assigned_contexts: Array.from(this.assignedContexts.keys()),
|
|
2021
|
+
active_context: this.activeContext,
|
|
2022
|
+
memory_daemon_ok: this.memoryDaemonUrl !== null,
|
|
2023
|
+
ollama_ok: this.ollamaUrl !== null
|
|
2024
|
+
};
|
|
2025
|
+
sendJson(res, 200, body);
|
|
2026
|
+
}
|
|
2027
|
+
handleAssign(raw, res) {
|
|
2028
|
+
const req = JSON.parse(raw);
|
|
2029
|
+
if (this.assignedContexts.size >= this.maxContexts) {
|
|
2030
|
+
const body2 = {
|
|
2031
|
+
context_name: req.context_name,
|
|
2032
|
+
accepted: false,
|
|
2033
|
+
reason: "Worker at max context capacity"
|
|
2034
|
+
};
|
|
2035
|
+
sendJson(res, 200, body2);
|
|
2036
|
+
return;
|
|
2037
|
+
}
|
|
2038
|
+
const assignment = {
|
|
2039
|
+
context_name: req.context_name,
|
|
2040
|
+
context_description: req.context_description,
|
|
2041
|
+
assigned_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
2042
|
+
};
|
|
2043
|
+
this.assignedContexts.set(req.context_name, assignment);
|
|
2044
|
+
if (this.onAssignCallback) {
|
|
2045
|
+
this.onAssignCallback(req.context_name, req.context_description);
|
|
2046
|
+
}
|
|
2047
|
+
const body = {
|
|
2048
|
+
context_name: req.context_name,
|
|
2049
|
+
accepted: true
|
|
2050
|
+
};
|
|
2051
|
+
sendJson(res, 200, body);
|
|
2052
|
+
}
|
|
2053
|
+
handleUnassign(contextName, res) {
|
|
2054
|
+
const existed = this.assignedContexts.delete(contextName);
|
|
2055
|
+
if (this.activeContext === contextName) {
|
|
2056
|
+
this.activeContext = null;
|
|
2057
|
+
this.currentTask = null;
|
|
2058
|
+
}
|
|
2059
|
+
sendJson(res, existed ? 200 : 404, {
|
|
2060
|
+
context_name: contextName,
|
|
2061
|
+
removed: existed
|
|
2062
|
+
});
|
|
2063
|
+
}
|
|
2064
|
+
handleStatus(res) {
|
|
2065
|
+
const report = {
|
|
2066
|
+
activity: this.currentTask ? "working" : "idle",
|
|
2067
|
+
current_context: this.activeContext,
|
|
2068
|
+
current_task: this.currentTask
|
|
2069
|
+
};
|
|
2070
|
+
sendJson(res, 200, report);
|
|
2071
|
+
}
|
|
2072
|
+
async handleSyncPush(raw, res) {
|
|
2073
|
+
if (!this.onSyncPushCallback) {
|
|
2074
|
+
sendJson(res, 501, { error: "Sync push handler not registered" });
|
|
2075
|
+
return;
|
|
2076
|
+
}
|
|
2077
|
+
const req = JSON.parse(raw);
|
|
2078
|
+
const result = await this.onSyncPushCallback(req);
|
|
2079
|
+
sendJson(res, 200, result);
|
|
2080
|
+
}
|
|
2081
|
+
};
|
|
2082
|
+
function readBody(req) {
|
|
2083
|
+
return new Promise((resolve5, reject) => {
|
|
2084
|
+
const chunks = [];
|
|
2085
|
+
req.on("data", (chunk) => chunks.push(chunk));
|
|
2086
|
+
req.on("end", () => resolve5(Buffer.concat(chunks).toString("utf-8")));
|
|
2087
|
+
req.on("error", reject);
|
|
2088
|
+
});
|
|
2089
|
+
}
|
|
2090
|
+
function sendJson(res, status, body) {
|
|
2091
|
+
const payload = JSON.stringify(body);
|
|
2092
|
+
res.writeHead(status, {
|
|
2093
|
+
"Content-Type": "application/json",
|
|
2094
|
+
"Content-Length": Buffer.byteLength(payload)
|
|
2095
|
+
});
|
|
2096
|
+
res.end(payload);
|
|
2097
|
+
}
|
|
2098
|
+
|
|
2099
|
+
// packages/runtime/src/fleet/worker-protocol.ts
|
|
2100
|
+
var WORKER_API_PREFIX = "/workers";
|
|
2101
|
+
var PRIMARY_ROUTES = {
|
|
2102
|
+
register: `${WORKER_API_PREFIX}/register`,
|
|
2103
|
+
status: (workerId) => `${WORKER_API_PREFIX}/${encodeURIComponent(workerId)}/status`,
|
|
2104
|
+
syncPull: `${WORKER_API_PREFIX}/sync/pull`
|
|
2105
|
+
};
|
|
2106
|
+
var WORKER_ROUTES = {
|
|
2107
|
+
health: "/health",
|
|
2108
|
+
assign: "/assign",
|
|
2109
|
+
unassign: (contextName) => `/assign/${encodeURIComponent(contextName)}`,
|
|
2110
|
+
status: "/status",
|
|
2111
|
+
syncPush: "/sync/push"
|
|
2112
|
+
};
|
|
2113
|
+
var DEFAULT_HEALTH_INTERVAL_MS = 3e4;
|
|
2114
|
+
var HEALTH_TIMEOUT_MS = 5e3;
|
|
2115
|
+
var DEFAULT_SYNC_INTERVAL_MS = 6e4;
|
|
2116
|
+
|
|
2117
|
+
// packages/runtime/src/worker.ts
|
|
2118
|
+
var WorkerRuntime = class {
|
|
2119
|
+
config;
|
|
2120
|
+
workerConfig;
|
|
2121
|
+
server;
|
|
2122
|
+
memory;
|
|
2123
|
+
agent;
|
|
2124
|
+
taskPollTimer = null;
|
|
2125
|
+
statusReportTimer = null;
|
|
2126
|
+
registeredWorkerId = null;
|
|
2127
|
+
running = false;
|
|
2128
|
+
executing = false;
|
|
2129
|
+
constructor(opts) {
|
|
2130
|
+
this.config = opts.config;
|
|
2131
|
+
this.workerConfig = opts.workerConfig;
|
|
2132
|
+
this.server = new WorkerServer({
|
|
2133
|
+
workerId: this.workerConfig.worker_id,
|
|
2134
|
+
port: this.workerConfig.worker_port,
|
|
2135
|
+
maxContexts: this.workerConfig.max_contexts,
|
|
2136
|
+
memoryDaemonUrl: this.config.memory.daemon_url,
|
|
2137
|
+
ollamaUrl: this.config.ollama.base_url
|
|
2138
|
+
});
|
|
2139
|
+
this.memory = new MemoryClient(this.config.memory);
|
|
2140
|
+
this.agent = new Agent(this.config);
|
|
2141
|
+
}
|
|
2142
|
+
/**
|
|
2143
|
+
* Start the worker: HTTP server, register with Primary, begin task loop.
|
|
2144
|
+
*/
|
|
2145
|
+
async start() {
|
|
2146
|
+
this.running = true;
|
|
2147
|
+
this.server.onContextAssigned((contextName, description) => {
|
|
2148
|
+
console.log(`[worker] Context assigned: "${contextName}" \u2014 ${description || "(no description)"}`);
|
|
2149
|
+
this.memory.createContext(contextName, description).catch((err) => {
|
|
2150
|
+
console.warn(`[worker] Failed to create context "${contextName}" in daemon:`, err.message);
|
|
2151
|
+
});
|
|
2152
|
+
});
|
|
2153
|
+
await this.server.start();
|
|
2154
|
+
console.log(`[worker] HTTP server listening on port ${this.workerConfig.worker_port}`);
|
|
2155
|
+
const memoryOk = await this.memory.healthCheck();
|
|
2156
|
+
if (memoryOk) {
|
|
2157
|
+
console.log("[worker] Local memory daemon connected");
|
|
2158
|
+
} else {
|
|
2159
|
+
console.warn("[worker] Memory daemon unreachable at", this.config.memory.daemon_url);
|
|
2160
|
+
console.warn("[worker] Continuing \u2014 episodes will not be stored until daemon is available");
|
|
2161
|
+
}
|
|
2162
|
+
await this.registerWithPrimary();
|
|
2163
|
+
this.startTaskLoop();
|
|
2164
|
+
this.startStatusReporting();
|
|
2165
|
+
console.log("[worker] Ready \u2014 waiting for context assignments");
|
|
2166
|
+
}
|
|
2167
|
+
/**
|
|
2168
|
+
* Stop the worker gracefully.
|
|
2169
|
+
*/
|
|
2170
|
+
async stop() {
|
|
2171
|
+
this.running = false;
|
|
2172
|
+
if (this.taskPollTimer) {
|
|
2173
|
+
clearInterval(this.taskPollTimer);
|
|
2174
|
+
this.taskPollTimer = null;
|
|
2175
|
+
}
|
|
2176
|
+
if (this.statusReportTimer) {
|
|
2177
|
+
clearInterval(this.statusReportTimer);
|
|
2178
|
+
this.statusReportTimer = null;
|
|
2179
|
+
}
|
|
2180
|
+
await this.server.stop();
|
|
2181
|
+
console.log("[worker] Stopped");
|
|
2182
|
+
}
|
|
2183
|
+
/**
|
|
2184
|
+
* Register this worker with the Primary node.
|
|
2185
|
+
*/
|
|
2186
|
+
async registerWithPrimary() {
|
|
2187
|
+
const workerUrl = `http://localhost:${this.workerConfig.worker_port}`;
|
|
2188
|
+
const registration = {
|
|
2189
|
+
url: workerUrl,
|
|
2190
|
+
capabilities: {
|
|
2191
|
+
max_contexts: this.workerConfig.max_contexts,
|
|
2192
|
+
has_ollama: true,
|
|
2193
|
+
has_memory_daemon: true,
|
|
2194
|
+
available_models: [this.config.memory.embedding_model]
|
|
2195
|
+
}
|
|
2196
|
+
};
|
|
2197
|
+
try {
|
|
2198
|
+
const resp = await fetch(
|
|
2199
|
+
`${this.workerConfig.primary_url}${PRIMARY_ROUTES.register}`,
|
|
2200
|
+
{
|
|
2201
|
+
method: "POST",
|
|
2202
|
+
headers: { "Content-Type": "application/json" },
|
|
2203
|
+
body: JSON.stringify(registration)
|
|
2204
|
+
}
|
|
2205
|
+
);
|
|
2206
|
+
if (!resp.ok) {
|
|
2207
|
+
console.error(`[worker] Registration failed: ${resp.status} ${await resp.text()}`);
|
|
2208
|
+
return null;
|
|
2209
|
+
}
|
|
2210
|
+
const result = await resp.json();
|
|
2211
|
+
this.registeredWorkerId = result.worker_id;
|
|
2212
|
+
console.log(`[worker] Registered with Primary as ${result.worker_id}`);
|
|
2213
|
+
return result;
|
|
2214
|
+
} catch (err) {
|
|
2215
|
+
console.error("[worker] Could not reach Primary at", this.workerConfig.primary_url);
|
|
2216
|
+
console.error("[worker] Will continue in standalone mode \u2014 retry registration manually");
|
|
2217
|
+
return null;
|
|
2218
|
+
}
|
|
2219
|
+
}
|
|
2220
|
+
/**
|
|
2221
|
+
* Start the task execution polling loop.
|
|
2222
|
+
* Iterates assigned contexts and picks up tasks from each queue.
|
|
2223
|
+
*/
|
|
2224
|
+
startTaskLoop() {
|
|
2225
|
+
this.taskPollTimer = setInterval(() => {
|
|
2226
|
+
if (!this.running || this.executing) return;
|
|
2227
|
+
this.executeNextTask().catch((err) => {
|
|
2228
|
+
console.error("[worker] Task execution error:", err.message);
|
|
2229
|
+
});
|
|
2230
|
+
}, this.workerConfig.task_poll_interval_ms);
|
|
2231
|
+
}
|
|
2232
|
+
/**
|
|
2233
|
+
* Find and execute the next available task across all assigned contexts.
|
|
2234
|
+
*/
|
|
2235
|
+
async executeNextTask() {
|
|
2236
|
+
const contexts = this.server.getAssignedContexts();
|
|
2237
|
+
if (contexts.length === 0) return;
|
|
2238
|
+
for (const contextName of contexts) {
|
|
2239
|
+
const engine = new TaskEngine({ contextName, memory: this.memory });
|
|
2240
|
+
const task = await engine.getNextTask();
|
|
2241
|
+
if (task) {
|
|
2242
|
+
this.executing = true;
|
|
2243
|
+
try {
|
|
2244
|
+
this.server.setActiveContext(contextName);
|
|
2245
|
+
this.server.setCurrentTask(task.id);
|
|
2246
|
+
console.log(`[worker] Executing task [${task.id.slice(0, 8)}] "${task.title}" in context "${contextName}"`);
|
|
2247
|
+
await engine.startTask(task.id);
|
|
2248
|
+
this.agent.setContext(contextName);
|
|
2249
|
+
try {
|
|
2250
|
+
await this.memory.createContext(contextName);
|
|
2251
|
+
} catch {
|
|
2252
|
+
}
|
|
2253
|
+
const taskPrompt = buildTaskPrompt(task.title, task.description);
|
|
2254
|
+
const response = await this.agent.processMessage(taskPrompt);
|
|
2255
|
+
console.log(`[worker] Task [${task.id.slice(0, 8)}] completed. Response: ${response.content.slice(0, 100)}...`);
|
|
2256
|
+
await engine.completeTask(task.id);
|
|
2257
|
+
await this.reportStatus("working", contextName, task.id);
|
|
2258
|
+
} catch (err) {
|
|
2259
|
+
console.error(`[worker] Task [${task.id.slice(0, 8)}] failed:`, err.message);
|
|
2260
|
+
await this.reportStatus("error", contextName, task.id, err.message);
|
|
2261
|
+
} finally {
|
|
2262
|
+
this.executing = false;
|
|
2263
|
+
this.server.setCurrentTask(null);
|
|
2264
|
+
}
|
|
2265
|
+
return;
|
|
2266
|
+
}
|
|
2267
|
+
}
|
|
2268
|
+
if (this.server.getAssignedContexts().length > 0) {
|
|
2269
|
+
this.server.setActiveContext(null);
|
|
2270
|
+
this.server.setCurrentTask(null);
|
|
2271
|
+
}
|
|
2272
|
+
}
|
|
2273
|
+
/**
|
|
2274
|
+
* Periodically report status back to Primary.
|
|
2275
|
+
*/
|
|
2276
|
+
startStatusReporting() {
|
|
2277
|
+
this.statusReportTimer = setInterval(() => {
|
|
2278
|
+
if (!this.running || !this.registeredWorkerId) return;
|
|
2279
|
+
const contexts = this.server.getAssignedContexts();
|
|
2280
|
+
if (contexts.length === 0) return;
|
|
2281
|
+
this.reportStatus(
|
|
2282
|
+
this.executing ? "working" : "idle",
|
|
2283
|
+
this.executing ? contexts[0] : null,
|
|
2284
|
+
null
|
|
2285
|
+
).catch(() => {
|
|
2286
|
+
});
|
|
2287
|
+
}, this.workerConfig.status_report_interval_ms);
|
|
2288
|
+
}
|
|
2289
|
+
/**
|
|
2290
|
+
* Send a status report to the Primary.
|
|
2291
|
+
*/
|
|
2292
|
+
async reportStatus(activity, currentContext, currentTask, error) {
|
|
2293
|
+
if (!this.registeredWorkerId) return;
|
|
2294
|
+
const report = {
|
|
2295
|
+
activity,
|
|
2296
|
+
current_context: currentContext,
|
|
2297
|
+
current_task: currentTask,
|
|
2298
|
+
error
|
|
2299
|
+
};
|
|
2300
|
+
try {
|
|
2301
|
+
const statusUrl = `${this.workerConfig.primary_url}${PRIMARY_ROUTES.status(this.registeredWorkerId)}`;
|
|
2302
|
+
await fetch(statusUrl, {
|
|
2303
|
+
method: "POST",
|
|
2304
|
+
headers: { "Content-Type": "application/json" },
|
|
2305
|
+
body: JSON.stringify(report)
|
|
2306
|
+
});
|
|
2307
|
+
} catch {
|
|
2308
|
+
}
|
|
2309
|
+
}
|
|
2310
|
+
// --- Accessors for testing ---
|
|
2311
|
+
getServer() {
|
|
2312
|
+
return this.server;
|
|
2313
|
+
}
|
|
2314
|
+
getAgent() {
|
|
2315
|
+
return this.agent;
|
|
2316
|
+
}
|
|
2317
|
+
getMemoryClient() {
|
|
2318
|
+
return this.memory;
|
|
2319
|
+
}
|
|
2320
|
+
isRunning() {
|
|
2321
|
+
return this.running;
|
|
2322
|
+
}
|
|
2323
|
+
isExecuting() {
|
|
2324
|
+
return this.executing;
|
|
2325
|
+
}
|
|
2326
|
+
getRegisteredWorkerId() {
|
|
2327
|
+
return this.registeredWorkerId;
|
|
2328
|
+
}
|
|
2329
|
+
};
|
|
2330
|
+
function buildTaskPrompt(title, description) {
|
|
2331
|
+
let prompt = `[TASK] ${title}`;
|
|
2332
|
+
if (description) {
|
|
2333
|
+
prompt += `
|
|
2334
|
+
|
|
2335
|
+
${description}`;
|
|
2336
|
+
}
|
|
2337
|
+
prompt += "\n\nPlease work on this task. Use your memory and knowledge to provide a thorough response.";
|
|
2338
|
+
return prompt;
|
|
2339
|
+
}
|
|
2340
|
+
async function startWorker(config) {
|
|
2341
|
+
if (!config.worker || !config.worker.enabled) {
|
|
2342
|
+
throw new Error("Worker mode is not enabled in config. Set [worker] enabled = true.");
|
|
2343
|
+
}
|
|
2344
|
+
const runtime = new WorkerRuntime({
|
|
2345
|
+
config,
|
|
2346
|
+
workerConfig: config.worker
|
|
2347
|
+
});
|
|
2348
|
+
await runtime.start();
|
|
2349
|
+
return runtime;
|
|
2350
|
+
}
|
|
2351
|
+
|
|
2352
|
+
export {
|
|
2353
|
+
LLMClient,
|
|
2354
|
+
MemoryClient,
|
|
2355
|
+
ContextManager,
|
|
2356
|
+
TaskEngine,
|
|
2357
|
+
buildSystemPrompt,
|
|
2358
|
+
buildMessages,
|
|
2359
|
+
Agent,
|
|
2360
|
+
defaultSentinelConfig,
|
|
2361
|
+
loadConfig,
|
|
2362
|
+
SesameClient2 as SesameClient,
|
|
2363
|
+
HEALTH_PATH,
|
|
2364
|
+
startPipeline,
|
|
2365
|
+
PRIMARY_ROUTES,
|
|
2366
|
+
WORKER_ROUTES,
|
|
2367
|
+
DEFAULT_HEALTH_INTERVAL_MS,
|
|
2368
|
+
HEALTH_TIMEOUT_MS,
|
|
2369
|
+
DEFAULT_SYNC_INTERVAL_MS,
|
|
2370
|
+
WorkerServer,
|
|
2371
|
+
WorkerRuntime,
|
|
2372
|
+
startWorker
|
|
2373
|
+
};
|
|
2374
|
+
/*! Bundled license information:
|
|
2375
|
+
|
|
2376
|
+
smol-toml/dist/error.js:
|
|
2377
|
+
smol-toml/dist/util.js:
|
|
2378
|
+
smol-toml/dist/date.js:
|
|
2379
|
+
smol-toml/dist/primitive.js:
|
|
2380
|
+
smol-toml/dist/extract.js:
|
|
2381
|
+
smol-toml/dist/struct.js:
|
|
2382
|
+
smol-toml/dist/parse.js:
|
|
2383
|
+
smol-toml/dist/stringify.js:
|
|
2384
|
+
smol-toml/dist/index.js:
|
|
2385
|
+
(*!
|
|
2386
|
+
* Copyright (c) Squirrel Chat et al., All rights reserved.
|
|
2387
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
2388
|
+
*
|
|
2389
|
+
* Redistribution and use in source and binary forms, with or without
|
|
2390
|
+
* modification, are permitted provided that the following conditions are met:
|
|
2391
|
+
*
|
|
2392
|
+
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
2393
|
+
* list of conditions and the following disclaimer.
|
|
2394
|
+
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
2395
|
+
* this list of conditions and the following disclaimer in the
|
|
2396
|
+
* documentation and/or other materials provided with the distribution.
|
|
2397
|
+
* 3. Neither the name of the copyright holder nor the names of its contributors
|
|
2398
|
+
* may be used to endorse or promote products derived from this software without
|
|
2399
|
+
* specific prior written permission.
|
|
2400
|
+
*
|
|
2401
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
2402
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
2403
|
+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
2404
|
+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
2405
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
2406
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
2407
|
+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
2408
|
+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
2409
|
+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
2410
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
2411
|
+
*)
|
|
2412
|
+
*/
|
|
2413
|
+
//# sourceMappingURL=chunk-4U4GKI3X.js.map
|