@sesamespace/hivemind 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/PLANNING.md +383 -0
- package/TASKS.md +60 -0
- package/install.sh +187 -0
- package/npm-package.json +28 -0
- package/package.json +13 -20
- package/packages/cli/package.json +23 -0
- package/{dist/chunk-DVR2KBL7.js → packages/cli/src/commands/fleet.ts} +50 -30
- package/packages/cli/src/commands/init.ts +230 -0
- package/{dist/chunk-MBS5A6BZ.js → packages/cli/src/commands/service.ts} +51 -42
- package/{dist/chunk-RNK5Q5GR.js → packages/cli/src/commands/start.ts} +12 -14
- package/{dist/main.js → packages/cli/src/main.ts} +12 -18
- package/packages/cli/tsconfig.json +8 -0
- package/packages/memory/Cargo.lock +6480 -0
- package/packages/memory/Cargo.toml +21 -0
- package/packages/memory/src/context.rs +179 -0
- package/packages/memory/src/embeddings.rs +51 -0
- package/packages/memory/src/main.rs +626 -0
- package/packages/memory/src/promotion.rs +637 -0
- package/packages/memory/src/scoring.rs +131 -0
- package/packages/memory/src/store.rs +460 -0
- package/packages/memory/src/tasks.rs +321 -0
- package/packages/runtime/package.json +24 -0
- package/packages/runtime/src/__tests__/fleet-integration.test.ts +235 -0
- package/packages/runtime/src/__tests__/fleet.test.ts +207 -0
- package/packages/runtime/src/__tests__/integration.test.ts +434 -0
- package/packages/runtime/src/agent.ts +255 -0
- package/packages/runtime/src/config.ts +130 -0
- package/packages/runtime/src/context.ts +192 -0
- package/packages/runtime/src/fleet/fleet-manager.ts +399 -0
- package/packages/runtime/src/fleet/memory-sync.ts +362 -0
- package/packages/runtime/src/fleet/primary-client.ts +285 -0
- package/packages/runtime/src/fleet/worker-protocol.ts +158 -0
- package/packages/runtime/src/fleet/worker-server.ts +246 -0
- package/packages/runtime/src/index.ts +57 -0
- package/packages/runtime/src/llm-client.ts +65 -0
- package/packages/runtime/src/memory-client.ts +309 -0
- package/packages/runtime/src/pipeline.ts +151 -0
- package/packages/runtime/src/prompt.ts +173 -0
- package/packages/runtime/src/sesame.ts +174 -0
- package/{dist/start.js → packages/runtime/src/start.ts} +7 -9
- package/packages/runtime/src/task-engine.ts +113 -0
- package/packages/runtime/src/worker.ts +339 -0
- package/packages/runtime/tsconfig.json +8 -0
- package/pnpm-workspace.yaml +2 -0
- package/run-aidan.sh +23 -0
- package/scripts/bootstrap.sh +196 -0
- package/scripts/build-npm.sh +94 -0
- package/scripts/com.hivemind.agent.plist +44 -0
- package/scripts/com.hivemind.memory.plist +31 -0
- package/tsconfig.json +22 -0
- package/tsup.config.ts +28 -0
- package/dist/chunk-2I2O6X5D.js +0 -1408
- package/dist/chunk-2I2O6X5D.js.map +0 -1
- package/dist/chunk-DVR2KBL7.js.map +0 -1
- package/dist/chunk-MBS5A6BZ.js.map +0 -1
- package/dist/chunk-NVJ424TB.js +0 -731
- package/dist/chunk-NVJ424TB.js.map +0 -1
- package/dist/chunk-RNK5Q5GR.js.map +0 -1
- package/dist/chunk-XNOWVLXD.js +0 -160
- package/dist/chunk-XNOWVLXD.js.map +0 -1
- package/dist/commands/fleet.js +0 -9
- package/dist/commands/fleet.js.map +0 -1
- package/dist/commands/init.js +0 -7
- package/dist/commands/init.js.map +0 -1
- package/dist/commands/service.js +0 -7
- package/dist/commands/service.js.map +0 -1
- package/dist/commands/start.js +0 -9
- package/dist/commands/start.js.map +0 -1
- package/dist/index.js +0 -41
- package/dist/index.js.map +0 -1
- package/dist/main.js.map +0 -1
- package/dist/start.js.map +0 -1
package/dist/chunk-2I2O6X5D.js
DELETED
|
@@ -1,1408 +0,0 @@
|
|
|
1
|
-
// packages/runtime/src/llm-client.ts
|
|
2
|
-
var LLMClient = class {
|
|
3
|
-
baseUrl;
|
|
4
|
-
model;
|
|
5
|
-
maxTokens;
|
|
6
|
-
temperature;
|
|
7
|
-
apiKey;
|
|
8
|
-
constructor(config) {
|
|
9
|
-
this.baseUrl = config.base_url;
|
|
10
|
-
this.model = config.model;
|
|
11
|
-
this.maxTokens = config.max_tokens;
|
|
12
|
-
this.temperature = config.temperature;
|
|
13
|
-
this.apiKey = config.api_key ?? "";
|
|
14
|
-
}
|
|
15
|
-
async chat(messages) {
|
|
16
|
-
const resp = await fetch(`${this.baseUrl}/chat/completions`, {
|
|
17
|
-
method: "POST",
|
|
18
|
-
headers: {
|
|
19
|
-
"Content-Type": "application/json",
|
|
20
|
-
...this.apiKey ? { Authorization: `Bearer ${this.apiKey}` } : {}
|
|
21
|
-
},
|
|
22
|
-
body: JSON.stringify({
|
|
23
|
-
model: this.model,
|
|
24
|
-
messages,
|
|
25
|
-
max_tokens: this.maxTokens,
|
|
26
|
-
temperature: this.temperature
|
|
27
|
-
})
|
|
28
|
-
});
|
|
29
|
-
if (!resp.ok) {
|
|
30
|
-
const body = await resp.text();
|
|
31
|
-
throw new Error(`LLM request failed: ${resp.status} ${body}`);
|
|
32
|
-
}
|
|
33
|
-
const data = await resp.json();
|
|
34
|
-
return {
|
|
35
|
-
content: data.choices[0].message.content,
|
|
36
|
-
model: data.model,
|
|
37
|
-
usage: data.usage
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
// packages/runtime/src/memory-client.ts
|
|
43
|
-
var MemoryClient = class {
|
|
44
|
-
baseUrl;
|
|
45
|
-
topK;
|
|
46
|
-
constructor(config) {
|
|
47
|
-
this.baseUrl = config.daemon_url;
|
|
48
|
-
this.topK = config.top_k;
|
|
49
|
-
}
|
|
50
|
-
async storeEpisode(input) {
|
|
51
|
-
const resp = await fetch(`${this.baseUrl}/episodes`, {
|
|
52
|
-
method: "POST",
|
|
53
|
-
headers: { "Content-Type": "application/json" },
|
|
54
|
-
body: JSON.stringify(input)
|
|
55
|
-
});
|
|
56
|
-
if (!resp.ok) {
|
|
57
|
-
throw new Error(`Memory store failed: ${resp.status} ${await resp.text()}`);
|
|
58
|
-
}
|
|
59
|
-
return resp.json();
|
|
60
|
-
}
|
|
61
|
-
async search(query, context, limit) {
|
|
62
|
-
const params = new URLSearchParams({ q: query });
|
|
63
|
-
if (context) params.set("context", context);
|
|
64
|
-
params.set("limit", String(limit ?? this.topK));
|
|
65
|
-
const resp = await fetch(`${this.baseUrl}/search?${params}`);
|
|
66
|
-
if (!resp.ok) {
|
|
67
|
-
throw new Error(`Memory search failed: ${resp.status} ${await resp.text()}`);
|
|
68
|
-
}
|
|
69
|
-
const data = await resp.json();
|
|
70
|
-
return data.episodes;
|
|
71
|
-
}
|
|
72
|
-
async getContext(name) {
|
|
73
|
-
const resp = await fetch(`${this.baseUrl}/contexts/${encodeURIComponent(name)}`);
|
|
74
|
-
if (!resp.ok) {
|
|
75
|
-
throw new Error(`Get context failed: ${resp.status} ${await resp.text()}`);
|
|
76
|
-
}
|
|
77
|
-
return resp.json();
|
|
78
|
-
}
|
|
79
|
-
async createContext(name, description = "") {
|
|
80
|
-
const resp = await fetch(`${this.baseUrl}/contexts`, {
|
|
81
|
-
method: "POST",
|
|
82
|
-
headers: { "Content-Type": "application/json" },
|
|
83
|
-
body: JSON.stringify({ name, description })
|
|
84
|
-
});
|
|
85
|
-
if (!resp.ok) {
|
|
86
|
-
throw new Error(`Create context failed: ${resp.status} ${await resp.text()}`);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
async deleteContext(name) {
|
|
90
|
-
const resp = await fetch(`${this.baseUrl}/contexts/${encodeURIComponent(name)}`, {
|
|
91
|
-
method: "DELETE"
|
|
92
|
-
});
|
|
93
|
-
if (!resp.ok) {
|
|
94
|
-
throw new Error(`Delete context failed: ${resp.status} ${await resp.text()}`);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
async listContexts() {
|
|
98
|
-
const resp = await fetch(`${this.baseUrl}/contexts`);
|
|
99
|
-
if (!resp.ok) {
|
|
100
|
-
throw new Error(`List contexts failed: ${resp.status} ${await resp.text()}`);
|
|
101
|
-
}
|
|
102
|
-
const data = await resp.json();
|
|
103
|
-
return data.contexts;
|
|
104
|
-
}
|
|
105
|
-
async searchCrossContext(query, limit) {
|
|
106
|
-
const params = new URLSearchParams({ q: query });
|
|
107
|
-
params.set("limit", String(limit ?? this.topK));
|
|
108
|
-
const resp = await fetch(`${this.baseUrl}/search/cross-context?${params}`);
|
|
109
|
-
if (!resp.ok) {
|
|
110
|
-
throw new Error(`Cross-context search failed: ${resp.status} ${await resp.text()}`);
|
|
111
|
-
}
|
|
112
|
-
const data = await resp.json();
|
|
113
|
-
return data.results;
|
|
114
|
-
}
|
|
115
|
-
async shareEpisode(episodeId, targetContext) {
|
|
116
|
-
const resp = await fetch(`${this.baseUrl}/episodes/${encodeURIComponent(episodeId)}/share`, {
|
|
117
|
-
method: "POST",
|
|
118
|
-
headers: { "Content-Type": "application/json" },
|
|
119
|
-
body: JSON.stringify({ target_context: targetContext })
|
|
120
|
-
});
|
|
121
|
-
if (!resp.ok) {
|
|
122
|
-
throw new Error(`Share episode failed: ${resp.status} ${await resp.text()}`);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
async runPromotion(contextName) {
|
|
126
|
-
const params = new URLSearchParams();
|
|
127
|
-
if (contextName) params.set("context", contextName);
|
|
128
|
-
const resp = await fetch(`${this.baseUrl}/promotion/run?${params}`, {
|
|
129
|
-
method: "POST"
|
|
130
|
-
});
|
|
131
|
-
if (!resp.ok) {
|
|
132
|
-
throw new Error(`Promotion failed: ${resp.status} ${await resp.text()}`);
|
|
133
|
-
}
|
|
134
|
-
return resp.json();
|
|
135
|
-
}
|
|
136
|
-
async getL3Knowledge(contextName) {
|
|
137
|
-
const resp = await fetch(
|
|
138
|
-
`${this.baseUrl}/promotion/l3?context=${encodeURIComponent(contextName)}`
|
|
139
|
-
);
|
|
140
|
-
if (!resp.ok) {
|
|
141
|
-
throw new Error(`Get L3 failed: ${resp.status} ${await resp.text()}`);
|
|
142
|
-
}
|
|
143
|
-
const data = await resp.json();
|
|
144
|
-
return data.entries;
|
|
145
|
-
}
|
|
146
|
-
async setContextScoring(contextName, halfLifeHours) {
|
|
147
|
-
const resp = await fetch(
|
|
148
|
-
`${this.baseUrl}/contexts/${encodeURIComponent(contextName)}/scoring`,
|
|
149
|
-
{
|
|
150
|
-
method: "POST",
|
|
151
|
-
headers: { "Content-Type": "application/json" },
|
|
152
|
-
body: JSON.stringify({ half_life_hours: halfLifeHours })
|
|
153
|
-
}
|
|
154
|
-
);
|
|
155
|
-
if (!resp.ok) {
|
|
156
|
-
throw new Error(`Set scoring failed: ${resp.status} ${await resp.text()}`);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
async recordAccess(episodeId) {
|
|
160
|
-
const resp = await fetch(`${this.baseUrl}/episodes/${encodeURIComponent(episodeId)}/access`, {
|
|
161
|
-
method: "POST"
|
|
162
|
-
});
|
|
163
|
-
if (!resp.ok) {
|
|
164
|
-
throw new Error(`Record access failed: ${resp.status} ${await resp.text()}`);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
async recordCoAccess(episodeIds) {
|
|
168
|
-
const resp = await fetch(`${this.baseUrl}/episodes/co-access`, {
|
|
169
|
-
method: "POST",
|
|
170
|
-
headers: { "Content-Type": "application/json" },
|
|
171
|
-
body: JSON.stringify({ episode_ids: episodeIds })
|
|
172
|
-
});
|
|
173
|
-
if (!resp.ok) {
|
|
174
|
-
throw new Error(`Co-access failed: ${resp.status} ${await resp.text()}`);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
// Task engine methods
|
|
178
|
-
async createTask(input) {
|
|
179
|
-
const resp = await fetch(`${this.baseUrl}/tasks`, {
|
|
180
|
-
method: "POST",
|
|
181
|
-
headers: { "Content-Type": "application/json" },
|
|
182
|
-
body: JSON.stringify(input)
|
|
183
|
-
});
|
|
184
|
-
if (!resp.ok) {
|
|
185
|
-
throw new Error(`Create task failed: ${resp.status} ${await resp.text()}`);
|
|
186
|
-
}
|
|
187
|
-
return resp.json();
|
|
188
|
-
}
|
|
189
|
-
async listTasks(contextName, status) {
|
|
190
|
-
const params = new URLSearchParams({ context: contextName });
|
|
191
|
-
if (status) params.set("status", status);
|
|
192
|
-
const resp = await fetch(`${this.baseUrl}/tasks?${params}`);
|
|
193
|
-
if (!resp.ok) {
|
|
194
|
-
throw new Error(`List tasks failed: ${resp.status} ${await resp.text()}`);
|
|
195
|
-
}
|
|
196
|
-
const data = await resp.json();
|
|
197
|
-
return data.tasks;
|
|
198
|
-
}
|
|
199
|
-
async updateTask(taskId, updates) {
|
|
200
|
-
const resp = await fetch(`${this.baseUrl}/tasks/${encodeURIComponent(taskId)}`, {
|
|
201
|
-
method: "PATCH",
|
|
202
|
-
headers: { "Content-Type": "application/json" },
|
|
203
|
-
body: JSON.stringify(updates)
|
|
204
|
-
});
|
|
205
|
-
if (!resp.ok) {
|
|
206
|
-
throw new Error(`Update task failed: ${resp.status} ${await resp.text()}`);
|
|
207
|
-
}
|
|
208
|
-
return resp.json();
|
|
209
|
-
}
|
|
210
|
-
async getNextTask(contextName) {
|
|
211
|
-
const resp = await fetch(`${this.baseUrl}/tasks/next?context=${encodeURIComponent(contextName)}`);
|
|
212
|
-
if (!resp.ok) {
|
|
213
|
-
if (resp.status === 404) return null;
|
|
214
|
-
throw new Error(`Get next task failed: ${resp.status} ${await resp.text()}`);
|
|
215
|
-
}
|
|
216
|
-
return resp.json();
|
|
217
|
-
}
|
|
218
|
-
async healthCheck() {
|
|
219
|
-
try {
|
|
220
|
-
const resp = await fetch(`${this.baseUrl}/health`);
|
|
221
|
-
return resp.ok;
|
|
222
|
-
} catch {
|
|
223
|
-
return false;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
};
|
|
227
|
-
|
|
228
|
-
// packages/runtime/src/context.ts
|
|
229
|
-
var SWITCH_PATTERNS = [
|
|
230
|
-
/^switch\s+to\s+(\S+)/i,
|
|
231
|
-
/^context:\s*(\S+)/i,
|
|
232
|
-
/^@(\S+)\s/,
|
|
233
|
-
/^working\s+on\s+(\S+)/i
|
|
234
|
-
];
|
|
235
|
-
var ContextManager = class {
|
|
236
|
-
contexts = /* @__PURE__ */ new Map();
|
|
237
|
-
activeContext = "global";
|
|
238
|
-
memory;
|
|
239
|
-
constructor(memory) {
|
|
240
|
-
this.memory = memory;
|
|
241
|
-
this.contexts.set("global", {
|
|
242
|
-
name: "global",
|
|
243
|
-
description: "Global context \u2014 identity, preferences, cross-cutting knowledge",
|
|
244
|
-
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
245
|
-
last_active: (/* @__PURE__ */ new Date()).toISOString()
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
async createContext(name, description = "") {
|
|
249
|
-
if (this.contexts.has(name)) {
|
|
250
|
-
return this.contexts.get(name);
|
|
251
|
-
}
|
|
252
|
-
const metadata = {
|
|
253
|
-
name,
|
|
254
|
-
description,
|
|
255
|
-
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
256
|
-
last_active: (/* @__PURE__ */ new Date()).toISOString()
|
|
257
|
-
};
|
|
258
|
-
this.contexts.set(name, metadata);
|
|
259
|
-
try {
|
|
260
|
-
await this.memory.createContext(name, description);
|
|
261
|
-
} catch (err) {
|
|
262
|
-
console.error(`Failed to register context '${name}' with daemon:`, err.message);
|
|
263
|
-
}
|
|
264
|
-
return metadata;
|
|
265
|
-
}
|
|
266
|
-
async deleteContext(name) {
|
|
267
|
-
if (name === "global") {
|
|
268
|
-
console.error("Cannot delete global context");
|
|
269
|
-
return false;
|
|
270
|
-
}
|
|
271
|
-
if (!this.contexts.has(name)) {
|
|
272
|
-
return false;
|
|
273
|
-
}
|
|
274
|
-
this.contexts.delete(name);
|
|
275
|
-
if (this.activeContext === name) {
|
|
276
|
-
this.activeContext = "global";
|
|
277
|
-
}
|
|
278
|
-
try {
|
|
279
|
-
await this.memory.deleteContext(name);
|
|
280
|
-
} catch (err) {
|
|
281
|
-
console.error(`Failed to delete context '${name}' from daemon:`, err.message);
|
|
282
|
-
}
|
|
283
|
-
return true;
|
|
284
|
-
}
|
|
285
|
-
listContexts() {
|
|
286
|
-
return Array.from(this.contexts.values());
|
|
287
|
-
}
|
|
288
|
-
getContext(name) {
|
|
289
|
-
return this.contexts.get(name);
|
|
290
|
-
}
|
|
291
|
-
getActiveContext() {
|
|
292
|
-
return this.activeContext;
|
|
293
|
-
}
|
|
294
|
-
switchContext(name) {
|
|
295
|
-
const previousContext = this.activeContext;
|
|
296
|
-
const isNew = !this.contexts.has(name);
|
|
297
|
-
if (isNew) {
|
|
298
|
-
this.contexts.set(name, {
|
|
299
|
-
name,
|
|
300
|
-
description: "",
|
|
301
|
-
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
302
|
-
last_active: (/* @__PURE__ */ new Date()).toISOString()
|
|
303
|
-
});
|
|
304
|
-
}
|
|
305
|
-
this.activeContext = name;
|
|
306
|
-
this.touchContext(name);
|
|
307
|
-
return { previousContext, activeContext: name, isNew };
|
|
308
|
-
}
|
|
309
|
-
/**
|
|
310
|
-
* Parse a message and determine if it's a context switch command.
|
|
311
|
-
* Returns the context name if a switch is detected, null otherwise.
|
|
312
|
-
*/
|
|
313
|
-
parseContextSwitch(message) {
|
|
314
|
-
for (const pattern of SWITCH_PATTERNS) {
|
|
315
|
-
const match = message.match(pattern);
|
|
316
|
-
if (match) {
|
|
317
|
-
return match[1].toLowerCase();
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
return null;
|
|
321
|
-
}
|
|
322
|
-
/**
|
|
323
|
-
* Infer context from message content by checking known context names.
|
|
324
|
-
* Returns the best matching context or current active context.
|
|
325
|
-
*/
|
|
326
|
-
inferContext(message) {
|
|
327
|
-
const lower = message.toLowerCase();
|
|
328
|
-
for (const [name] of this.contexts) {
|
|
329
|
-
if (name === "global") continue;
|
|
330
|
-
if (lower.includes(name.toLowerCase())) {
|
|
331
|
-
return name;
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
return this.activeContext;
|
|
335
|
-
}
|
|
336
|
-
/**
|
|
337
|
-
* Route a message: check for explicit switch, then infer context.
|
|
338
|
-
* Returns the resolved context name and whether a switch happened.
|
|
339
|
-
*/
|
|
340
|
-
routeMessage(message) {
|
|
341
|
-
const switchTarget = this.parseContextSwitch(message);
|
|
342
|
-
if (switchTarget) {
|
|
343
|
-
const result = this.switchContext(switchTarget);
|
|
344
|
-
return { context: switchTarget, switched: true, switchedTo: result.activeContext };
|
|
345
|
-
}
|
|
346
|
-
const inferred = this.inferContext(message);
|
|
347
|
-
if (inferred !== this.activeContext) {
|
|
348
|
-
this.touchContext(inferred);
|
|
349
|
-
return { context: inferred, switched: false };
|
|
350
|
-
}
|
|
351
|
-
this.touchContext(this.activeContext);
|
|
352
|
-
return { context: this.activeContext, switched: false };
|
|
353
|
-
}
|
|
354
|
-
/**
|
|
355
|
-
* Returns contexts that should be searched: active context + global.
|
|
356
|
-
*/
|
|
357
|
-
getSearchContexts() {
|
|
358
|
-
const contexts = [this.activeContext];
|
|
359
|
-
if (this.activeContext !== "global") {
|
|
360
|
-
contexts.push("global");
|
|
361
|
-
}
|
|
362
|
-
return contexts;
|
|
363
|
-
}
|
|
364
|
-
hasContext(name) {
|
|
365
|
-
return this.contexts.has(name);
|
|
366
|
-
}
|
|
367
|
-
touchContext(name) {
|
|
368
|
-
const ctx = this.contexts.get(name);
|
|
369
|
-
if (ctx) {
|
|
370
|
-
ctx.last_active = (/* @__PURE__ */ new Date()).toISOString();
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
};
|
|
374
|
-
|
|
375
|
-
// packages/runtime/src/task-engine.ts
|
|
376
|
-
var TaskEngine = class {
|
|
377
|
-
contextName;
|
|
378
|
-
memory;
|
|
379
|
-
constructor(options) {
|
|
380
|
-
this.contextName = options.contextName;
|
|
381
|
-
this.memory = options.memory;
|
|
382
|
-
}
|
|
383
|
-
async addTask(title, description, blockedBy = []) {
|
|
384
|
-
return this.memory.createTask({
|
|
385
|
-
context_name: this.contextName,
|
|
386
|
-
title,
|
|
387
|
-
description,
|
|
388
|
-
status: "planned",
|
|
389
|
-
blocked_by: blockedBy
|
|
390
|
-
});
|
|
391
|
-
}
|
|
392
|
-
async listTasks(status) {
|
|
393
|
-
return this.memory.listTasks(this.contextName, status);
|
|
394
|
-
}
|
|
395
|
-
async startTask(taskId) {
|
|
396
|
-
return this.memory.updateTask(taskId, { status: "active" });
|
|
397
|
-
}
|
|
398
|
-
async completeTask(taskId) {
|
|
399
|
-
return this.memory.updateTask(taskId, { status: "complete" });
|
|
400
|
-
}
|
|
401
|
-
async archiveTask(taskId) {
|
|
402
|
-
return this.memory.updateTask(taskId, { status: "archived" });
|
|
403
|
-
}
|
|
404
|
-
async getNextTask() {
|
|
405
|
-
return this.memory.getNextTask(this.contextName);
|
|
406
|
-
}
|
|
407
|
-
async pickAndStartNextTask() {
|
|
408
|
-
const next = await this.getNextTask();
|
|
409
|
-
if (next) {
|
|
410
|
-
return this.startTask(next.id);
|
|
411
|
-
}
|
|
412
|
-
return null;
|
|
413
|
-
}
|
|
414
|
-
setContext(contextName) {
|
|
415
|
-
this.contextName = contextName;
|
|
416
|
-
}
|
|
417
|
-
/**
|
|
418
|
-
* Parse a task command from a chat message.
|
|
419
|
-
* Returns the action and parsed parameters, or null if not a task command.
|
|
420
|
-
*/
|
|
421
|
-
static parseTaskCommand(message) {
|
|
422
|
-
const lower = message.trim().toLowerCase();
|
|
423
|
-
const addMatch = message.match(/^(?:add|create)\s+task[:\s]+(.+)/i);
|
|
424
|
-
if (addMatch) {
|
|
425
|
-
return { action: "add", title: addMatch[1].trim() };
|
|
426
|
-
}
|
|
427
|
-
if (/^list\s+tasks/i.test(lower)) {
|
|
428
|
-
const statusMatch = lower.match(/list\s+tasks\s+(\w+)/);
|
|
429
|
-
return { action: "list", statusFilter: statusMatch?.[1] };
|
|
430
|
-
}
|
|
431
|
-
const completeMatch = message.match(/^complete\s+task\s+(\S+)/i);
|
|
432
|
-
if (completeMatch) {
|
|
433
|
-
return { action: "complete", taskId: completeMatch[1] };
|
|
434
|
-
}
|
|
435
|
-
const startMatch = message.match(/^start\s+task\s+(\S+)/i);
|
|
436
|
-
if (startMatch) {
|
|
437
|
-
return { action: "start", taskId: startMatch[1] };
|
|
438
|
-
}
|
|
439
|
-
if (/^next\s+task/i.test(lower)) {
|
|
440
|
-
return { action: "next" };
|
|
441
|
-
}
|
|
442
|
-
const archiveMatch = message.match(/^archive\s+task\s+(\S+)/i);
|
|
443
|
-
if (archiveMatch) {
|
|
444
|
-
return { action: "archive", taskId: archiveMatch[1] };
|
|
445
|
-
}
|
|
446
|
-
return null;
|
|
447
|
-
}
|
|
448
|
-
};
|
|
449
|
-
|
|
450
|
-
// packages/runtime/src/prompt.ts
|
|
451
|
-
import { readFileSync, existsSync } from "fs";
|
|
452
|
-
var charterCache = null;
|
|
453
|
-
function loadCharter(path) {
|
|
454
|
-
if (charterCache && charterCache.path === path) return charterCache.content;
|
|
455
|
-
if (!existsSync(path)) return "";
|
|
456
|
-
const content = readFileSync(path, "utf-8");
|
|
457
|
-
charterCache = { path, content };
|
|
458
|
-
return content;
|
|
459
|
-
}
|
|
460
|
-
function buildSystemPrompt(config, episodes, contextName = "global", l3Knowledge = []) {
|
|
461
|
-
let prompt = `You are ${config.name}. ${config.personality}
|
|
462
|
-
`;
|
|
463
|
-
if (config.team_charter) {
|
|
464
|
-
const charter = loadCharter(config.team_charter);
|
|
465
|
-
if (charter) {
|
|
466
|
-
prompt += `
|
|
467
|
-
${charter}
|
|
468
|
-
`;
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
prompt += `
|
|
472
|
-
## Communication
|
|
473
|
-
Messages are prefixed with [sender_handle]: or [sender_handle in group chat]: to tell you who's talking.
|
|
474
|
-
In group chats, multiple people (humans and agents) may be present. Address them by name when relevant.
|
|
475
|
-
Don't repeat or quote these prefixes in your responses \u2014 just respond naturally.
|
|
476
|
-
If you decide not to respond to a group message, reply with exactly: __SKIP__
|
|
477
|
-
`;
|
|
478
|
-
if (contextName !== "global") {
|
|
479
|
-
prompt += `
|
|
480
|
-
## Active Context: ${contextName}
|
|
481
|
-
You are currently working in the "${contextName}" project context.
|
|
482
|
-
`;
|
|
483
|
-
}
|
|
484
|
-
if (l3Knowledge.length > 0) {
|
|
485
|
-
prompt += "\n## Established Knowledge (learned patterns)\n\n";
|
|
486
|
-
for (const entry of l3Knowledge) {
|
|
487
|
-
prompt += `- ${entry.content}
|
|
488
|
-
`;
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
if (episodes.length > 0) {
|
|
492
|
-
prompt += "\n## Relevant memories from previous conversations\n\n";
|
|
493
|
-
for (const ep of episodes) {
|
|
494
|
-
const timeAgo = formatTimeAgo(ep.timestamp);
|
|
495
|
-
const ctxLabel = ep.context_name !== contextName ? ` [from: ${ep.context_name}]` : "";
|
|
496
|
-
prompt += `[${timeAgo}]${ctxLabel} ${ep.role}: ${ep.content}
|
|
497
|
-
`;
|
|
498
|
-
}
|
|
499
|
-
prompt += "\nUse these memories naturally \u2014 reference past conversations when relevant, but don't force it.\n";
|
|
500
|
-
}
|
|
501
|
-
return prompt;
|
|
502
|
-
}
|
|
503
|
-
function buildMessages(systemPrompt, conversationHistory, currentMessage) {
|
|
504
|
-
return [
|
|
505
|
-
{ role: "system", content: systemPrompt },
|
|
506
|
-
...conversationHistory,
|
|
507
|
-
{ role: "user", content: currentMessage }
|
|
508
|
-
];
|
|
509
|
-
}
|
|
510
|
-
function formatTimeAgo(timestamp) {
|
|
511
|
-
const date = new Date(timestamp);
|
|
512
|
-
const now = /* @__PURE__ */ new Date();
|
|
513
|
-
const diffMs = now.getTime() - date.getTime();
|
|
514
|
-
const diffMins = Math.floor(diffMs / 6e4);
|
|
515
|
-
const diffHours = Math.floor(diffMs / 36e5);
|
|
516
|
-
const diffDays = Math.floor(diffMs / 864e5);
|
|
517
|
-
if (diffMins < 1) return "just now";
|
|
518
|
-
if (diffMins < 60) return `${diffMins}m ago`;
|
|
519
|
-
if (diffHours < 24) return `${diffHours}h ago`;
|
|
520
|
-
if (diffDays < 7) return `${diffDays}d ago`;
|
|
521
|
-
return date.toLocaleDateString();
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
// packages/runtime/src/agent.ts
|
|
525
|
-
var Agent = class {
|
|
526
|
-
config;
|
|
527
|
-
llm;
|
|
528
|
-
memory;
|
|
529
|
-
contextManager;
|
|
530
|
-
// Per-context conversation histories
|
|
531
|
-
conversationHistories = /* @__PURE__ */ new Map();
|
|
532
|
-
messageCount = 0;
|
|
533
|
-
PROMOTION_INTERVAL = 10;
|
|
534
|
-
// Run promotion every N messages
|
|
535
|
-
constructor(config, contextName = "global") {
|
|
536
|
-
this.config = config;
|
|
537
|
-
this.llm = new LLMClient(config.llm);
|
|
538
|
-
this.memory = new MemoryClient(config.memory);
|
|
539
|
-
this.contextManager = new ContextManager(this.memory);
|
|
540
|
-
if (contextName !== "global") {
|
|
541
|
-
this.contextManager.switchContext(contextName);
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
async processMessage(userMessage) {
|
|
545
|
-
const specialResult = await this.handleSpecialCommand(userMessage);
|
|
546
|
-
if (specialResult) return specialResult;
|
|
547
|
-
const routing = this.contextManager.routeMessage(userMessage);
|
|
548
|
-
const contextName = routing.context;
|
|
549
|
-
if (routing.switched) {
|
|
550
|
-
try {
|
|
551
|
-
await this.memory.createContext(contextName);
|
|
552
|
-
} catch {
|
|
553
|
-
}
|
|
554
|
-
}
|
|
555
|
-
if (!this.conversationHistories.has(contextName)) {
|
|
556
|
-
this.conversationHistories.set(contextName, []);
|
|
557
|
-
}
|
|
558
|
-
const conversationHistory = this.conversationHistories.get(contextName);
|
|
559
|
-
const relevantEpisodes = await this.memory.search(userMessage, contextName, this.config.memory.top_k).catch((err) => {
|
|
560
|
-
console.error("Memory search failed, continuing without context:", err.message);
|
|
561
|
-
return [];
|
|
562
|
-
});
|
|
563
|
-
if (relevantEpisodes.length > 0) {
|
|
564
|
-
const episodeIds = relevantEpisodes.map((e) => e.id);
|
|
565
|
-
this.memory.recordCoAccess(episodeIds).catch(() => {
|
|
566
|
-
});
|
|
567
|
-
for (const ep of relevantEpisodes) {
|
|
568
|
-
this.memory.recordAccess(ep.id).catch(() => {
|
|
569
|
-
});
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
|
-
const l3Knowledge = await this.memory.getL3Knowledge(contextName).catch(() => []);
|
|
573
|
-
const systemPrompt = buildSystemPrompt(this.config.agent, relevantEpisodes, contextName, l3Knowledge);
|
|
574
|
-
const messages = buildMessages(systemPrompt, conversationHistory, userMessage);
|
|
575
|
-
const response = await this.llm.chat(messages);
|
|
576
|
-
conversationHistory.push(
|
|
577
|
-
{ role: "user", content: userMessage },
|
|
578
|
-
{ role: "assistant", content: response.content }
|
|
579
|
-
);
|
|
580
|
-
if (conversationHistory.length > 40) {
|
|
581
|
-
const trimmed = conversationHistory.slice(-40);
|
|
582
|
-
this.conversationHistories.set(contextName, trimmed);
|
|
583
|
-
}
|
|
584
|
-
await this.storeEpisodes(contextName, userMessage, response.content);
|
|
585
|
-
this.messageCount++;
|
|
586
|
-
if (this.messageCount % this.PROMOTION_INTERVAL === 0) {
|
|
587
|
-
this.memory.runPromotion(contextName).catch((err) => {
|
|
588
|
-
console.error("Promotion run failed:", err.message);
|
|
589
|
-
});
|
|
590
|
-
}
|
|
591
|
-
return {
|
|
592
|
-
content: response.content,
|
|
593
|
-
model: response.model,
|
|
594
|
-
context: contextName,
|
|
595
|
-
contextSwitched: routing.switched
|
|
596
|
-
};
|
|
597
|
-
}
|
|
598
|
-
async storeEpisodes(contextName, userMessage, assistantResponse) {
|
|
599
|
-
try {
|
|
600
|
-
await Promise.all([
|
|
601
|
-
this.memory.storeEpisode({
|
|
602
|
-
context_name: contextName,
|
|
603
|
-
role: "user",
|
|
604
|
-
content: userMessage
|
|
605
|
-
}),
|
|
606
|
-
this.memory.storeEpisode({
|
|
607
|
-
context_name: contextName,
|
|
608
|
-
role: "assistant",
|
|
609
|
-
content: assistantResponse
|
|
610
|
-
})
|
|
611
|
-
]);
|
|
612
|
-
} catch (err) {
|
|
613
|
-
console.error("Failed to store episodes:", err.message);
|
|
614
|
-
}
|
|
615
|
-
}
|
|
616
|
-
async handleSpecialCommand(message) {
|
|
617
|
-
const activeCtx = this.contextManager.getActiveContext();
|
|
618
|
-
const searchAllMatch = message.match(/^(?:search\s+all|cross-context\s+search)[:\s]+(.+)/i);
|
|
619
|
-
if (searchAllMatch) {
|
|
620
|
-
const query = searchAllMatch[1].trim();
|
|
621
|
-
try {
|
|
622
|
-
const results = await this.memory.searchCrossContext(query);
|
|
623
|
-
let response = "## Cross-Context Search Results\n\n";
|
|
624
|
-
if (results.length === 0) {
|
|
625
|
-
response += "No results found across any context.";
|
|
626
|
-
} else {
|
|
627
|
-
for (const group of results) {
|
|
628
|
-
response += `### Context: ${group.context}
|
|
629
|
-
`;
|
|
630
|
-
for (const ep of group.episodes) {
|
|
631
|
-
response += `- [${ep.role}] ${ep.content.slice(0, 200)}${ep.content.length > 200 ? "..." : ""} (score: ${ep.score.toFixed(3)})
|
|
632
|
-
`;
|
|
633
|
-
}
|
|
634
|
-
response += "\n";
|
|
635
|
-
}
|
|
636
|
-
}
|
|
637
|
-
return { content: response, model: "system", context: activeCtx };
|
|
638
|
-
} catch (err) {
|
|
639
|
-
return { content: `Cross-context search failed: ${err.message}`, model: "system", context: activeCtx };
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
const shareMatch = message.match(/^share\s+(\S+)\s+with\s+(\S+)/i);
|
|
643
|
-
if (shareMatch) {
|
|
644
|
-
const episodeId = shareMatch[1];
|
|
645
|
-
const targetContext = shareMatch[2];
|
|
646
|
-
try {
|
|
647
|
-
await this.memory.shareEpisode(episodeId, targetContext);
|
|
648
|
-
return { content: `Shared episode ${episodeId} with context "${targetContext}".`, model: "system", context: activeCtx };
|
|
649
|
-
} catch (err) {
|
|
650
|
-
return { content: `Failed to share: ${err.message}`, model: "system", context: activeCtx };
|
|
651
|
-
}
|
|
652
|
-
}
|
|
653
|
-
const taskCmd = TaskEngine.parseTaskCommand(message);
|
|
654
|
-
if (taskCmd) {
|
|
655
|
-
const engine = new TaskEngine({ contextName: activeCtx, memory: this.memory });
|
|
656
|
-
try {
|
|
657
|
-
switch (taskCmd.action) {
|
|
658
|
-
case "add": {
|
|
659
|
-
const task = await engine.addTask(taskCmd.title || "Untitled", taskCmd.description || "");
|
|
660
|
-
return { content: `Task created: [${task.id.slice(0, 8)}] ${task.title} (status: ${task.status})`, model: "system", context: activeCtx };
|
|
661
|
-
}
|
|
662
|
-
case "list": {
|
|
663
|
-
const tasks = await engine.listTasks(taskCmd.statusFilter);
|
|
664
|
-
if (tasks.length === 0) {
|
|
665
|
-
return { content: `No tasks${taskCmd.statusFilter ? ` with status "${taskCmd.statusFilter}"` : ""} in context "${activeCtx}".`, model: "system", context: activeCtx };
|
|
666
|
-
}
|
|
667
|
-
let response = `## Tasks in ${activeCtx}
|
|
668
|
-
|
|
669
|
-
`;
|
|
670
|
-
for (const t of tasks) {
|
|
671
|
-
const blockedBy = Array.isArray(t.blocked_by) ? t.blocked_by : JSON.parse(String(t.blocked_by) || "[]");
|
|
672
|
-
const blockedStr = blockedBy.length > 0 ? ` (blocked by: ${blockedBy.map((b) => b.slice(0, 8)).join(", ")})` : "";
|
|
673
|
-
response += `- [${t.status}] ${t.id.slice(0, 8)}: ${t.title}${blockedStr}
|
|
674
|
-
`;
|
|
675
|
-
}
|
|
676
|
-
return { content: response, model: "system", context: activeCtx };
|
|
677
|
-
}
|
|
678
|
-
case "start": {
|
|
679
|
-
const task = await engine.startTask(taskCmd.taskId);
|
|
680
|
-
return { content: task ? `Task ${taskCmd.taskId.slice(0, 8)} started.` : "Task not found.", model: "system", context: activeCtx };
|
|
681
|
-
}
|
|
682
|
-
case "complete": {
|
|
683
|
-
const task = await engine.completeTask(taskCmd.taskId);
|
|
684
|
-
return { content: task ? `Task ${taskCmd.taskId.slice(0, 8)} completed.` : "Task not found.", model: "system", context: activeCtx };
|
|
685
|
-
}
|
|
686
|
-
case "archive": {
|
|
687
|
-
const task = await engine.archiveTask(taskCmd.taskId);
|
|
688
|
-
return { content: task ? `Task ${taskCmd.taskId.slice(0, 8)} archived.` : "Task not found.", model: "system", context: activeCtx };
|
|
689
|
-
}
|
|
690
|
-
case "next": {
|
|
691
|
-
const task = await engine.pickAndStartNextTask();
|
|
692
|
-
if (task) {
|
|
693
|
-
return { content: `Picked up next task: [${task.id.slice(0, 8)}] ${task.title}`, model: "system", context: activeCtx };
|
|
694
|
-
}
|
|
695
|
-
return { content: "No available tasks to pick up.", model: "system", context: activeCtx };
|
|
696
|
-
}
|
|
697
|
-
}
|
|
698
|
-
} catch (err) {
|
|
699
|
-
return { content: `Task command failed: ${err.message}`, model: "system", context: activeCtx };
|
|
700
|
-
}
|
|
701
|
-
}
|
|
702
|
-
return null;
|
|
703
|
-
}
|
|
704
|
-
getMemoryClient() {
|
|
705
|
-
return this.memory;
|
|
706
|
-
}
|
|
707
|
-
getContextManager() {
|
|
708
|
-
return this.contextManager;
|
|
709
|
-
}
|
|
710
|
-
setContext(name) {
|
|
711
|
-
this.contextManager.switchContext(name);
|
|
712
|
-
}
|
|
713
|
-
getActiveContext() {
|
|
714
|
-
return this.contextManager.getActiveContext();
|
|
715
|
-
}
|
|
716
|
-
};
|
|
717
|
-
|
|
718
|
-
// packages/runtime/src/config.ts
|
|
719
|
-
import { readFileSync as readFileSync2, existsSync as existsSync2 } from "fs";
|
|
720
|
-
import { resolve, dirname } from "path";
|
|
721
|
-
import { parse } from "@iarna/toml";
|
|
722
|
-
function defaultWorkerConfig() {
|
|
723
|
-
return {
|
|
724
|
-
enabled: false,
|
|
725
|
-
primary_url: "http://localhost:3000",
|
|
726
|
-
worker_port: 3100,
|
|
727
|
-
worker_id: `worker-${process.pid}`,
|
|
728
|
-
max_contexts: 4,
|
|
729
|
-
task_poll_interval_ms: 5e3,
|
|
730
|
-
status_report_interval_ms: 15e3
|
|
731
|
-
};
|
|
732
|
-
}
|
|
733
|
-
function deepMerge(target, source) {
|
|
734
|
-
const result = { ...target };
|
|
735
|
-
for (const key of Object.keys(source)) {
|
|
736
|
-
if (source[key] && typeof source[key] === "object" && !Array.isArray(source[key]) && target[key] && typeof target[key] === "object") {
|
|
737
|
-
result[key] = deepMerge(target[key], source[key]);
|
|
738
|
-
} else if (source[key] !== void 0 && source[key] !== "") {
|
|
739
|
-
result[key] = source[key];
|
|
740
|
-
}
|
|
741
|
-
}
|
|
742
|
-
return result;
|
|
743
|
-
}
|
|
744
|
-
function loadConfig(path) {
|
|
745
|
-
const raw = readFileSync2(path, "utf-8");
|
|
746
|
-
let parsed = parse(raw);
|
|
747
|
-
const configDir = dirname(path);
|
|
748
|
-
const localPath = resolve(configDir, "local.toml");
|
|
749
|
-
if (existsSync2(localPath)) {
|
|
750
|
-
const localRaw = readFileSync2(localPath, "utf-8");
|
|
751
|
-
const localParsed = parse(localRaw);
|
|
752
|
-
parsed = deepMerge(parsed, localParsed);
|
|
753
|
-
console.log(`[config] Merged overrides from ${localPath}`);
|
|
754
|
-
}
|
|
755
|
-
if (process.env.AGENT_NAME) {
|
|
756
|
-
parsed.agent.name = process.env.AGENT_NAME;
|
|
757
|
-
}
|
|
758
|
-
if (process.env.LLM_API_KEY) {
|
|
759
|
-
parsed.llm.api_key = process.env.LLM_API_KEY;
|
|
760
|
-
}
|
|
761
|
-
if (process.env.LLM_BASE_URL) {
|
|
762
|
-
parsed.llm.base_url = process.env.LLM_BASE_URL;
|
|
763
|
-
}
|
|
764
|
-
if (process.env.SESAME_API_KEY) {
|
|
765
|
-
parsed.sesame.api_key = process.env.SESAME_API_KEY;
|
|
766
|
-
}
|
|
767
|
-
if (process.env.MEMORY_DAEMON_URL) {
|
|
768
|
-
parsed.memory.daemon_url = process.env.MEMORY_DAEMON_URL;
|
|
769
|
-
}
|
|
770
|
-
if (process.env.WORKER_PRIMARY_URL) {
|
|
771
|
-
if (!parsed.worker) parsed.worker = defaultWorkerConfig();
|
|
772
|
-
parsed.worker.primary_url = process.env.WORKER_PRIMARY_URL;
|
|
773
|
-
}
|
|
774
|
-
if (process.env.WORKER_PORT) {
|
|
775
|
-
if (!parsed.worker) parsed.worker = defaultWorkerConfig();
|
|
776
|
-
parsed.worker.worker_port = parseInt(process.env.WORKER_PORT, 10);
|
|
777
|
-
}
|
|
778
|
-
if (process.env.WORKER_ID) {
|
|
779
|
-
if (!parsed.worker) parsed.worker = defaultWorkerConfig();
|
|
780
|
-
parsed.worker.worker_id = process.env.WORKER_ID;
|
|
781
|
-
}
|
|
782
|
-
return parsed;
|
|
783
|
-
}
|
|
784
|
-
|
|
785
|
-
// packages/runtime/src/sesame.ts
|
|
786
|
-
import { SesameClient as SesameSDK } from "@sesamespace/sdk";
|
|
787
|
-
var SesameClient = class {
|
|
788
|
-
config;
|
|
789
|
-
sdk;
|
|
790
|
-
messageHandler = null;
|
|
791
|
-
agentId = null;
|
|
792
|
-
channels = /* @__PURE__ */ new Map();
|
|
793
|
-
constructor(config) {
|
|
794
|
-
this.config = config;
|
|
795
|
-
this.sdk = new SesameSDK({
|
|
796
|
-
apiUrl: config.api_url.replace(/\/api\/v1$/, ""),
|
|
797
|
-
wsUrl: config.ws_url,
|
|
798
|
-
apiKey: config.api_key
|
|
799
|
-
});
|
|
800
|
-
}
|
|
801
|
-
onMessage(handler) {
|
|
802
|
-
this.messageHandler = handler;
|
|
803
|
-
}
|
|
804
|
-
async connect() {
|
|
805
|
-
const manifest = await this.sdk.getManifest();
|
|
806
|
-
this.agentId = manifest.agent.id;
|
|
807
|
-
console.log(`[sesame] Authenticated as ${manifest.agent.handle} (${this.agentId})`);
|
|
808
|
-
for (const ch of manifest.channels) {
|
|
809
|
-
this.channels.set(ch.id, { kind: ch.kind, name: ch.name ?? void 0 });
|
|
810
|
-
const label = ch.name || ch.id.slice(0, 8);
|
|
811
|
-
console.log(`[sesame] Channel: ${label} (${ch.kind})`);
|
|
812
|
-
}
|
|
813
|
-
this.sdk.on("message", (event) => {
|
|
814
|
-
const msg = event.data || event.message || event;
|
|
815
|
-
const senderId = msg.senderId || msg.sender?.id;
|
|
816
|
-
if (senderId === this.agentId) return;
|
|
817
|
-
if (!this.messageHandler || !msg.content) return;
|
|
818
|
-
const channelInfo = this.channels.get(msg.channelId);
|
|
819
|
-
this.messageHandler({
|
|
820
|
-
id: msg.id || "unknown",
|
|
821
|
-
channelId: msg.channelId || "unknown",
|
|
822
|
-
channelKind: channelInfo?.kind || "dm",
|
|
823
|
-
content: msg.content,
|
|
824
|
-
author: {
|
|
825
|
-
id: senderId || "unknown",
|
|
826
|
-
handle: msg.senderHandle || msg.metadata?.senderHandle || "unknown"
|
|
827
|
-
},
|
|
828
|
-
timestamp: msg.createdAt || (/* @__PURE__ */ new Date()).toISOString()
|
|
829
|
-
});
|
|
830
|
-
});
|
|
831
|
-
await this.sdk.connect();
|
|
832
|
-
console.log("[sesame] WebSocket connected");
|
|
833
|
-
}
|
|
834
|
-
async sendMessage(channelId, content) {
|
|
835
|
-
await this.sdk.sendMessage(channelId, { content });
|
|
836
|
-
}
|
|
837
|
-
getAgentId() {
|
|
838
|
-
return this.agentId;
|
|
839
|
-
}
|
|
840
|
-
getChannelInfo(channelId) {
|
|
841
|
-
return this.channels.get(channelId);
|
|
842
|
-
}
|
|
843
|
-
disconnect() {
|
|
844
|
-
this.sdk.disconnect();
|
|
845
|
-
}
|
|
846
|
-
};
|
|
847
|
-
|
|
848
|
-
// packages/runtime/src/pipeline.ts
|
|
849
|
-
async function startPipeline(configPath) {
|
|
850
|
-
const config = loadConfig(configPath);
|
|
851
|
-
console.log(`[hivemind] Starting ${config.agent.name} (pid ${process.pid})`);
|
|
852
|
-
const memory = new MemoryClient(config.memory);
|
|
853
|
-
const memoryOk = await memory.healthCheck();
|
|
854
|
-
if (!memoryOk) {
|
|
855
|
-
console.warn("[hivemind] Memory daemon unreachable at", config.memory.daemon_url);
|
|
856
|
-
console.warn("[hivemind] Continuing without persistent memory \u2014 episodes will not be stored");
|
|
857
|
-
} else {
|
|
858
|
-
console.log("[hivemind] Memory daemon connected");
|
|
859
|
-
}
|
|
860
|
-
const agent = new Agent(config);
|
|
861
|
-
console.log(`[hivemind] Context manager initialized (active: ${agent.getActiveContext()})`);
|
|
862
|
-
if (config.sesame.api_key) {
|
|
863
|
-
await startSesameLoop(config, agent);
|
|
864
|
-
} else {
|
|
865
|
-
console.log("[hivemind] No Sesame API key configured \u2014 running in stdin mode");
|
|
866
|
-
await startStdinLoop(agent);
|
|
867
|
-
}
|
|
868
|
-
}
|
|
869
|
-
async function startSesameLoop(config, agent) {
|
|
870
|
-
const sesame = new SesameClient(config.sesame);
|
|
871
|
-
let shuttingDown = false;
|
|
872
|
-
const shutdown = (signal) => {
|
|
873
|
-
if (shuttingDown) return;
|
|
874
|
-
shuttingDown = true;
|
|
875
|
-
console.log(`
|
|
876
|
-
[hivemind] Received ${signal}, shutting down...`);
|
|
877
|
-
try {
|
|
878
|
-
sesame.disconnect();
|
|
879
|
-
console.log("[hivemind] Sesame disconnected cleanly");
|
|
880
|
-
} catch (err) {
|
|
881
|
-
console.error("[hivemind] Error during disconnect:", err.message);
|
|
882
|
-
}
|
|
883
|
-
process.exit(0);
|
|
884
|
-
};
|
|
885
|
-
process.on("SIGTERM", () => shutdown("SIGTERM"));
|
|
886
|
-
process.on("SIGINT", () => shutdown("SIGINT"));
|
|
887
|
-
sesame.onMessage(async (msg) => {
|
|
888
|
-
if (shuttingDown) return;
|
|
889
|
-
console.log(`[sesame] ${msg.author.handle} (${msg.channelKind}): ${msg.content}`);
|
|
890
|
-
try {
|
|
891
|
-
const prefix = msg.channelKind === "group" ? `[${msg.author.handle} in group chat]: ` : `[${msg.author.handle}]: `;
|
|
892
|
-
const response = await agent.processMessage(prefix + msg.content);
|
|
893
|
-
if (response.content.trim() === "__SKIP__") {
|
|
894
|
-
console.log(`[sesame] ${config.agent.name}: skipped (${msg.author.handle} in ${msg.channelKind})`);
|
|
895
|
-
return;
|
|
896
|
-
}
|
|
897
|
-
const ctxPrefix = response.contextSwitched ? `[switched to ${response.context}] ` : "";
|
|
898
|
-
await sesame.sendMessage(msg.channelId, ctxPrefix + response.content);
|
|
899
|
-
console.log(`[sesame] ${config.agent.name} (${response.context}): ${response.content.slice(0, 100)}...`);
|
|
900
|
-
} catch (err) {
|
|
901
|
-
console.error("[sesame] Error processing message:", err.message);
|
|
902
|
-
}
|
|
903
|
-
});
|
|
904
|
-
await sesame.connect();
|
|
905
|
-
console.log("[hivemind] Listening for Sesame messages");
|
|
906
|
-
await new Promise(() => {
|
|
907
|
-
});
|
|
908
|
-
}
|
|
909
|
-
async function startStdinLoop(agent) {
|
|
910
|
-
const readline = await import("readline");
|
|
911
|
-
const rl = readline.createInterface({
|
|
912
|
-
input: process.stdin,
|
|
913
|
-
output: process.stdout
|
|
914
|
-
});
|
|
915
|
-
console.log("[hivemind] Ready. Type a message (Ctrl+C to exit)");
|
|
916
|
-
console.log("[hivemind] Commands: 'switch to <name>', 'list contexts', 'create context <name>'\n");
|
|
917
|
-
rl.on("line", async (line) => {
|
|
918
|
-
const input = line.trim();
|
|
919
|
-
if (!input) return;
|
|
920
|
-
if (input.toLowerCase() === "list contexts") {
|
|
921
|
-
const contexts = agent.getContextManager().listContexts();
|
|
922
|
-
console.log("\nContexts:");
|
|
923
|
-
for (const ctx of contexts) {
|
|
924
|
-
const active = ctx.name === agent.getActiveContext() ? " (active)" : "";
|
|
925
|
-
console.log(` - ${ctx.name}${active}: ${ctx.description || "(no description)"}`);
|
|
926
|
-
}
|
|
927
|
-
console.log();
|
|
928
|
-
return;
|
|
929
|
-
}
|
|
930
|
-
const createMatch = input.match(/^create context (\S+)\s*(.*)?$/i);
|
|
931
|
-
if (createMatch) {
|
|
932
|
-
const name = createMatch[1];
|
|
933
|
-
const desc = createMatch[2] || "";
|
|
934
|
-
await agent.getContextManager().createContext(name, desc);
|
|
935
|
-
console.log(`
|
|
936
|
-
Created context: ${name}
|
|
937
|
-
`);
|
|
938
|
-
return;
|
|
939
|
-
}
|
|
940
|
-
try {
|
|
941
|
-
const response = await agent.processMessage(input);
|
|
942
|
-
if (response.contextSwitched) {
|
|
943
|
-
console.log(`
|
|
944
|
-
[switched to context: ${response.context}]`);
|
|
945
|
-
}
|
|
946
|
-
console.log(`
|
|
947
|
-
${response.content}
|
|
948
|
-
`);
|
|
949
|
-
} catch (err) {
|
|
950
|
-
console.error("Error:", err.message);
|
|
951
|
-
}
|
|
952
|
-
});
|
|
953
|
-
return new Promise((resolve2) => {
|
|
954
|
-
rl.on("close", resolve2);
|
|
955
|
-
});
|
|
956
|
-
}
|
|
957
|
-
|
|
958
|
-
// packages/runtime/src/fleet/worker-server.ts
|
|
959
|
-
import { createServer } from "http";
|
|
960
|
-
var WorkerServer = class {
|
|
961
|
-
server = null;
|
|
962
|
-
workerId;
|
|
963
|
-
port;
|
|
964
|
-
startTime;
|
|
965
|
-
assignedContexts = /* @__PURE__ */ new Map();
|
|
966
|
-
activeContext = null;
|
|
967
|
-
currentTask = null;
|
|
968
|
-
maxContexts;
|
|
969
|
-
memoryDaemonUrl;
|
|
970
|
-
ollamaUrl;
|
|
971
|
-
onAssignCallback = null;
|
|
972
|
-
onSyncPushCallback = null;
|
|
973
|
-
constructor(opts) {
|
|
974
|
-
this.workerId = opts.workerId;
|
|
975
|
-
this.port = opts.port;
|
|
976
|
-
this.startTime = Date.now();
|
|
977
|
-
this.maxContexts = opts.maxContexts ?? 4;
|
|
978
|
-
this.memoryDaemonUrl = opts.memoryDaemonUrl ?? null;
|
|
979
|
-
this.ollamaUrl = opts.ollamaUrl ?? null;
|
|
980
|
-
}
|
|
981
|
-
/** Start listening. */
|
|
982
|
-
async start() {
|
|
983
|
-
return new Promise((resolve2, reject) => {
|
|
984
|
-
this.server = createServer((req, res) => this.handleRequest(req, res));
|
|
985
|
-
this.server.on("error", reject);
|
|
986
|
-
this.server.listen(this.port, () => resolve2());
|
|
987
|
-
});
|
|
988
|
-
}
|
|
989
|
-
/** Stop the server. */
|
|
990
|
-
async stop() {
|
|
991
|
-
return new Promise((resolve2) => {
|
|
992
|
-
if (!this.server) {
|
|
993
|
-
resolve2();
|
|
994
|
-
return;
|
|
995
|
-
}
|
|
996
|
-
this.server.close(() => resolve2());
|
|
997
|
-
});
|
|
998
|
-
}
|
|
999
|
-
getPort() {
|
|
1000
|
-
return this.port;
|
|
1001
|
-
}
|
|
1002
|
-
getAssignedContexts() {
|
|
1003
|
-
return Array.from(this.assignedContexts.keys());
|
|
1004
|
-
}
|
|
1005
|
-
setActiveContext(name) {
|
|
1006
|
-
this.activeContext = name;
|
|
1007
|
-
}
|
|
1008
|
-
setCurrentTask(taskId) {
|
|
1009
|
-
this.currentTask = taskId;
|
|
1010
|
-
}
|
|
1011
|
-
/** Register a callback for when a new context is assigned. */
|
|
1012
|
-
onContextAssigned(cb) {
|
|
1013
|
-
this.onAssignCallback = cb;
|
|
1014
|
-
}
|
|
1015
|
-
/** Register a handler for incoming sync push requests from Primary. */
|
|
1016
|
-
onSyncPush(cb) {
|
|
1017
|
-
this.onSyncPushCallback = cb;
|
|
1018
|
-
}
|
|
1019
|
-
// --- Request Router ---
|
|
1020
|
-
async handleRequest(req, res) {
|
|
1021
|
-
const url = new URL(req.url ?? "/", `http://localhost:${this.port}`);
|
|
1022
|
-
const path = url.pathname;
|
|
1023
|
-
const method = req.method ?? "GET";
|
|
1024
|
-
try {
|
|
1025
|
-
if (method === "GET" && path === "/health") {
|
|
1026
|
-
return this.handleHealth(res);
|
|
1027
|
-
}
|
|
1028
|
-
if (method === "POST" && path === "/assign") {
|
|
1029
|
-
const body = await readBody(req);
|
|
1030
|
-
return this.handleAssign(body, res);
|
|
1031
|
-
}
|
|
1032
|
-
if (method === "DELETE" && path.startsWith("/assign/")) {
|
|
1033
|
-
const contextName = decodeURIComponent(path.slice("/assign/".length));
|
|
1034
|
-
return this.handleUnassign(contextName, res);
|
|
1035
|
-
}
|
|
1036
|
-
if (method === "GET" && path === "/status") {
|
|
1037
|
-
return this.handleStatus(res);
|
|
1038
|
-
}
|
|
1039
|
-
if (method === "POST" && path === "/sync/push") {
|
|
1040
|
-
const body = await readBody(req);
|
|
1041
|
-
return this.handleSyncPush(body, res);
|
|
1042
|
-
}
|
|
1043
|
-
sendJson(res, 404, { error: "Not found" });
|
|
1044
|
-
} catch (err) {
|
|
1045
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
1046
|
-
sendJson(res, 500, { error: msg });
|
|
1047
|
-
}
|
|
1048
|
-
}
|
|
1049
|
-
// --- Handlers ---
|
|
1050
|
-
handleHealth(res) {
|
|
1051
|
-
const uptimeMs = Date.now() - this.startTime;
|
|
1052
|
-
const body = {
|
|
1053
|
-
worker_id: this.workerId,
|
|
1054
|
-
status: "healthy",
|
|
1055
|
-
uptime_seconds: Math.floor(uptimeMs / 1e3),
|
|
1056
|
-
assigned_contexts: Array.from(this.assignedContexts.keys()),
|
|
1057
|
-
active_context: this.activeContext,
|
|
1058
|
-
memory_daemon_ok: this.memoryDaemonUrl !== null,
|
|
1059
|
-
ollama_ok: this.ollamaUrl !== null
|
|
1060
|
-
};
|
|
1061
|
-
sendJson(res, 200, body);
|
|
1062
|
-
}
|
|
1063
|
-
handleAssign(raw, res) {
|
|
1064
|
-
const req = JSON.parse(raw);
|
|
1065
|
-
if (this.assignedContexts.size >= this.maxContexts) {
|
|
1066
|
-
const body2 = {
|
|
1067
|
-
context_name: req.context_name,
|
|
1068
|
-
accepted: false,
|
|
1069
|
-
reason: "Worker at max context capacity"
|
|
1070
|
-
};
|
|
1071
|
-
sendJson(res, 200, body2);
|
|
1072
|
-
return;
|
|
1073
|
-
}
|
|
1074
|
-
const assignment = {
|
|
1075
|
-
context_name: req.context_name,
|
|
1076
|
-
context_description: req.context_description,
|
|
1077
|
-
assigned_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
1078
|
-
};
|
|
1079
|
-
this.assignedContexts.set(req.context_name, assignment);
|
|
1080
|
-
if (this.onAssignCallback) {
|
|
1081
|
-
this.onAssignCallback(req.context_name, req.context_description);
|
|
1082
|
-
}
|
|
1083
|
-
const body = {
|
|
1084
|
-
context_name: req.context_name,
|
|
1085
|
-
accepted: true
|
|
1086
|
-
};
|
|
1087
|
-
sendJson(res, 200, body);
|
|
1088
|
-
}
|
|
1089
|
-
handleUnassign(contextName, res) {
|
|
1090
|
-
const existed = this.assignedContexts.delete(contextName);
|
|
1091
|
-
if (this.activeContext === contextName) {
|
|
1092
|
-
this.activeContext = null;
|
|
1093
|
-
this.currentTask = null;
|
|
1094
|
-
}
|
|
1095
|
-
sendJson(res, existed ? 200 : 404, {
|
|
1096
|
-
context_name: contextName,
|
|
1097
|
-
removed: existed
|
|
1098
|
-
});
|
|
1099
|
-
}
|
|
1100
|
-
handleStatus(res) {
|
|
1101
|
-
const report = {
|
|
1102
|
-
activity: this.currentTask ? "working" : "idle",
|
|
1103
|
-
current_context: this.activeContext,
|
|
1104
|
-
current_task: this.currentTask
|
|
1105
|
-
};
|
|
1106
|
-
sendJson(res, 200, report);
|
|
1107
|
-
}
|
|
1108
|
-
async handleSyncPush(raw, res) {
|
|
1109
|
-
if (!this.onSyncPushCallback) {
|
|
1110
|
-
sendJson(res, 501, { error: "Sync push handler not registered" });
|
|
1111
|
-
return;
|
|
1112
|
-
}
|
|
1113
|
-
const req = JSON.parse(raw);
|
|
1114
|
-
const result = await this.onSyncPushCallback(req);
|
|
1115
|
-
sendJson(res, 200, result);
|
|
1116
|
-
}
|
|
1117
|
-
};
|
|
1118
|
-
function readBody(req) {
|
|
1119
|
-
return new Promise((resolve2, reject) => {
|
|
1120
|
-
const chunks = [];
|
|
1121
|
-
req.on("data", (chunk) => chunks.push(chunk));
|
|
1122
|
-
req.on("end", () => resolve2(Buffer.concat(chunks).toString("utf-8")));
|
|
1123
|
-
req.on("error", reject);
|
|
1124
|
-
});
|
|
1125
|
-
}
|
|
1126
|
-
function sendJson(res, status, body) {
|
|
1127
|
-
const payload = JSON.stringify(body);
|
|
1128
|
-
res.writeHead(status, {
|
|
1129
|
-
"Content-Type": "application/json",
|
|
1130
|
-
"Content-Length": Buffer.byteLength(payload)
|
|
1131
|
-
});
|
|
1132
|
-
res.end(payload);
|
|
1133
|
-
}
|
|
1134
|
-
|
|
1135
|
-
// packages/runtime/src/fleet/worker-protocol.ts
|
|
1136
|
-
var WORKER_API_PREFIX = "/workers";
|
|
1137
|
-
var PRIMARY_ROUTES = {
|
|
1138
|
-
register: `${WORKER_API_PREFIX}/register`,
|
|
1139
|
-
status: (workerId) => `${WORKER_API_PREFIX}/${encodeURIComponent(workerId)}/status`,
|
|
1140
|
-
syncPull: `${WORKER_API_PREFIX}/sync/pull`
|
|
1141
|
-
};
|
|
1142
|
-
var WORKER_ROUTES = {
|
|
1143
|
-
health: "/health",
|
|
1144
|
-
assign: "/assign",
|
|
1145
|
-
unassign: (contextName) => `/assign/${encodeURIComponent(contextName)}`,
|
|
1146
|
-
status: "/status",
|
|
1147
|
-
syncPush: "/sync/push"
|
|
1148
|
-
};
|
|
1149
|
-
var DEFAULT_HEALTH_INTERVAL_MS = 3e4;
|
|
1150
|
-
var HEALTH_TIMEOUT_MS = 5e3;
|
|
1151
|
-
var DEFAULT_SYNC_INTERVAL_MS = 6e4;
|
|
1152
|
-
|
|
1153
|
-
// packages/runtime/src/worker.ts
|
|
1154
|
-
var WorkerRuntime = class {
|
|
1155
|
-
config;
|
|
1156
|
-
workerConfig;
|
|
1157
|
-
server;
|
|
1158
|
-
memory;
|
|
1159
|
-
agent;
|
|
1160
|
-
taskPollTimer = null;
|
|
1161
|
-
statusReportTimer = null;
|
|
1162
|
-
registeredWorkerId = null;
|
|
1163
|
-
running = false;
|
|
1164
|
-
executing = false;
|
|
1165
|
-
constructor(opts) {
|
|
1166
|
-
this.config = opts.config;
|
|
1167
|
-
this.workerConfig = opts.workerConfig;
|
|
1168
|
-
this.server = new WorkerServer({
|
|
1169
|
-
workerId: this.workerConfig.worker_id,
|
|
1170
|
-
port: this.workerConfig.worker_port,
|
|
1171
|
-
maxContexts: this.workerConfig.max_contexts,
|
|
1172
|
-
memoryDaemonUrl: this.config.memory.daemon_url,
|
|
1173
|
-
ollamaUrl: this.config.ollama.base_url
|
|
1174
|
-
});
|
|
1175
|
-
this.memory = new MemoryClient(this.config.memory);
|
|
1176
|
-
this.agent = new Agent(this.config);
|
|
1177
|
-
}
|
|
1178
|
-
/**
|
|
1179
|
-
* Start the worker: HTTP server, register with Primary, begin task loop.
|
|
1180
|
-
*/
|
|
1181
|
-
async start() {
|
|
1182
|
-
this.running = true;
|
|
1183
|
-
this.server.onContextAssigned((contextName, description) => {
|
|
1184
|
-
console.log(`[worker] Context assigned: "${contextName}" \u2014 ${description || "(no description)"}`);
|
|
1185
|
-
this.memory.createContext(contextName, description).catch((err) => {
|
|
1186
|
-
console.warn(`[worker] Failed to create context "${contextName}" in daemon:`, err.message);
|
|
1187
|
-
});
|
|
1188
|
-
});
|
|
1189
|
-
await this.server.start();
|
|
1190
|
-
console.log(`[worker] HTTP server listening on port ${this.workerConfig.worker_port}`);
|
|
1191
|
-
const memoryOk = await this.memory.healthCheck();
|
|
1192
|
-
if (memoryOk) {
|
|
1193
|
-
console.log("[worker] Local memory daemon connected");
|
|
1194
|
-
} else {
|
|
1195
|
-
console.warn("[worker] Memory daemon unreachable at", this.config.memory.daemon_url);
|
|
1196
|
-
console.warn("[worker] Continuing \u2014 episodes will not be stored until daemon is available");
|
|
1197
|
-
}
|
|
1198
|
-
await this.registerWithPrimary();
|
|
1199
|
-
this.startTaskLoop();
|
|
1200
|
-
this.startStatusReporting();
|
|
1201
|
-
console.log("[worker] Ready \u2014 waiting for context assignments");
|
|
1202
|
-
}
|
|
1203
|
-
/**
|
|
1204
|
-
* Stop the worker gracefully.
|
|
1205
|
-
*/
|
|
1206
|
-
async stop() {
|
|
1207
|
-
this.running = false;
|
|
1208
|
-
if (this.taskPollTimer) {
|
|
1209
|
-
clearInterval(this.taskPollTimer);
|
|
1210
|
-
this.taskPollTimer = null;
|
|
1211
|
-
}
|
|
1212
|
-
if (this.statusReportTimer) {
|
|
1213
|
-
clearInterval(this.statusReportTimer);
|
|
1214
|
-
this.statusReportTimer = null;
|
|
1215
|
-
}
|
|
1216
|
-
await this.server.stop();
|
|
1217
|
-
console.log("[worker] Stopped");
|
|
1218
|
-
}
|
|
1219
|
-
/**
|
|
1220
|
-
* Register this worker with the Primary node.
|
|
1221
|
-
*/
|
|
1222
|
-
async registerWithPrimary() {
|
|
1223
|
-
const workerUrl = `http://localhost:${this.workerConfig.worker_port}`;
|
|
1224
|
-
const registration = {
|
|
1225
|
-
url: workerUrl,
|
|
1226
|
-
capabilities: {
|
|
1227
|
-
max_contexts: this.workerConfig.max_contexts,
|
|
1228
|
-
has_ollama: true,
|
|
1229
|
-
has_memory_daemon: true,
|
|
1230
|
-
available_models: [this.config.memory.embedding_model]
|
|
1231
|
-
}
|
|
1232
|
-
};
|
|
1233
|
-
try {
|
|
1234
|
-
const resp = await fetch(
|
|
1235
|
-
`${this.workerConfig.primary_url}${PRIMARY_ROUTES.register}`,
|
|
1236
|
-
{
|
|
1237
|
-
method: "POST",
|
|
1238
|
-
headers: { "Content-Type": "application/json" },
|
|
1239
|
-
body: JSON.stringify(registration)
|
|
1240
|
-
}
|
|
1241
|
-
);
|
|
1242
|
-
if (!resp.ok) {
|
|
1243
|
-
console.error(`[worker] Registration failed: ${resp.status} ${await resp.text()}`);
|
|
1244
|
-
return null;
|
|
1245
|
-
}
|
|
1246
|
-
const result = await resp.json();
|
|
1247
|
-
this.registeredWorkerId = result.worker_id;
|
|
1248
|
-
console.log(`[worker] Registered with Primary as ${result.worker_id}`);
|
|
1249
|
-
return result;
|
|
1250
|
-
} catch (err) {
|
|
1251
|
-
console.error("[worker] Could not reach Primary at", this.workerConfig.primary_url);
|
|
1252
|
-
console.error("[worker] Will continue in standalone mode \u2014 retry registration manually");
|
|
1253
|
-
return null;
|
|
1254
|
-
}
|
|
1255
|
-
}
|
|
1256
|
-
/**
|
|
1257
|
-
* Start the task execution polling loop.
|
|
1258
|
-
* Iterates assigned contexts and picks up tasks from each queue.
|
|
1259
|
-
*/
|
|
1260
|
-
startTaskLoop() {
|
|
1261
|
-
this.taskPollTimer = setInterval(() => {
|
|
1262
|
-
if (!this.running || this.executing) return;
|
|
1263
|
-
this.executeNextTask().catch((err) => {
|
|
1264
|
-
console.error("[worker] Task execution error:", err.message);
|
|
1265
|
-
});
|
|
1266
|
-
}, this.workerConfig.task_poll_interval_ms);
|
|
1267
|
-
}
|
|
1268
|
-
/**
|
|
1269
|
-
* Find and execute the next available task across all assigned contexts.
|
|
1270
|
-
*/
|
|
1271
|
-
async executeNextTask() {
|
|
1272
|
-
const contexts = this.server.getAssignedContexts();
|
|
1273
|
-
if (contexts.length === 0) return;
|
|
1274
|
-
for (const contextName of contexts) {
|
|
1275
|
-
const engine = new TaskEngine({ contextName, memory: this.memory });
|
|
1276
|
-
const task = await engine.getNextTask();
|
|
1277
|
-
if (task) {
|
|
1278
|
-
this.executing = true;
|
|
1279
|
-
try {
|
|
1280
|
-
this.server.setActiveContext(contextName);
|
|
1281
|
-
this.server.setCurrentTask(task.id);
|
|
1282
|
-
console.log(`[worker] Executing task [${task.id.slice(0, 8)}] "${task.title}" in context "${contextName}"`);
|
|
1283
|
-
await engine.startTask(task.id);
|
|
1284
|
-
this.agent.setContext(contextName);
|
|
1285
|
-
try {
|
|
1286
|
-
await this.memory.createContext(contextName);
|
|
1287
|
-
} catch {
|
|
1288
|
-
}
|
|
1289
|
-
const taskPrompt = buildTaskPrompt(task.title, task.description);
|
|
1290
|
-
const response = await this.agent.processMessage(taskPrompt);
|
|
1291
|
-
console.log(`[worker] Task [${task.id.slice(0, 8)}] completed. Response: ${response.content.slice(0, 100)}...`);
|
|
1292
|
-
await engine.completeTask(task.id);
|
|
1293
|
-
await this.reportStatus("working", contextName, task.id);
|
|
1294
|
-
} catch (err) {
|
|
1295
|
-
console.error(`[worker] Task [${task.id.slice(0, 8)}] failed:`, err.message);
|
|
1296
|
-
await this.reportStatus("error", contextName, task.id, err.message);
|
|
1297
|
-
} finally {
|
|
1298
|
-
this.executing = false;
|
|
1299
|
-
this.server.setCurrentTask(null);
|
|
1300
|
-
}
|
|
1301
|
-
return;
|
|
1302
|
-
}
|
|
1303
|
-
}
|
|
1304
|
-
if (this.server.getAssignedContexts().length > 0) {
|
|
1305
|
-
this.server.setActiveContext(null);
|
|
1306
|
-
this.server.setCurrentTask(null);
|
|
1307
|
-
}
|
|
1308
|
-
}
|
|
1309
|
-
/**
|
|
1310
|
-
* Periodically report status back to Primary.
|
|
1311
|
-
*/
|
|
1312
|
-
startStatusReporting() {
|
|
1313
|
-
this.statusReportTimer = setInterval(() => {
|
|
1314
|
-
if (!this.running || !this.registeredWorkerId) return;
|
|
1315
|
-
const contexts = this.server.getAssignedContexts();
|
|
1316
|
-
if (contexts.length === 0) return;
|
|
1317
|
-
this.reportStatus(
|
|
1318
|
-
this.executing ? "working" : "idle",
|
|
1319
|
-
this.executing ? contexts[0] : null,
|
|
1320
|
-
null
|
|
1321
|
-
).catch(() => {
|
|
1322
|
-
});
|
|
1323
|
-
}, this.workerConfig.status_report_interval_ms);
|
|
1324
|
-
}
|
|
1325
|
-
/**
|
|
1326
|
-
* Send a status report to the Primary.
|
|
1327
|
-
*/
|
|
1328
|
-
async reportStatus(activity, currentContext, currentTask, error) {
|
|
1329
|
-
if (!this.registeredWorkerId) return;
|
|
1330
|
-
const report = {
|
|
1331
|
-
activity,
|
|
1332
|
-
current_context: currentContext,
|
|
1333
|
-
current_task: currentTask,
|
|
1334
|
-
error
|
|
1335
|
-
};
|
|
1336
|
-
try {
|
|
1337
|
-
const statusUrl = `${this.workerConfig.primary_url}${PRIMARY_ROUTES.status(this.registeredWorkerId)}`;
|
|
1338
|
-
await fetch(statusUrl, {
|
|
1339
|
-
method: "POST",
|
|
1340
|
-
headers: { "Content-Type": "application/json" },
|
|
1341
|
-
body: JSON.stringify(report)
|
|
1342
|
-
});
|
|
1343
|
-
} catch {
|
|
1344
|
-
}
|
|
1345
|
-
}
|
|
1346
|
-
// --- Accessors for testing ---
|
|
1347
|
-
getServer() {
|
|
1348
|
-
return this.server;
|
|
1349
|
-
}
|
|
1350
|
-
getAgent() {
|
|
1351
|
-
return this.agent;
|
|
1352
|
-
}
|
|
1353
|
-
getMemoryClient() {
|
|
1354
|
-
return this.memory;
|
|
1355
|
-
}
|
|
1356
|
-
isRunning() {
|
|
1357
|
-
return this.running;
|
|
1358
|
-
}
|
|
1359
|
-
isExecuting() {
|
|
1360
|
-
return this.executing;
|
|
1361
|
-
}
|
|
1362
|
-
getRegisteredWorkerId() {
|
|
1363
|
-
return this.registeredWorkerId;
|
|
1364
|
-
}
|
|
1365
|
-
};
|
|
1366
|
-
function buildTaskPrompt(title, description) {
|
|
1367
|
-
let prompt = `[TASK] ${title}`;
|
|
1368
|
-
if (description) {
|
|
1369
|
-
prompt += `
|
|
1370
|
-
|
|
1371
|
-
${description}`;
|
|
1372
|
-
}
|
|
1373
|
-
prompt += "\n\nPlease work on this task. Use your memory and knowledge to provide a thorough response.";
|
|
1374
|
-
return prompt;
|
|
1375
|
-
}
|
|
1376
|
-
async function startWorker(config) {
|
|
1377
|
-
if (!config.worker || !config.worker.enabled) {
|
|
1378
|
-
throw new Error("Worker mode is not enabled in config. Set [worker] enabled = true.");
|
|
1379
|
-
}
|
|
1380
|
-
const runtime = new WorkerRuntime({
|
|
1381
|
-
config,
|
|
1382
|
-
workerConfig: config.worker
|
|
1383
|
-
});
|
|
1384
|
-
await runtime.start();
|
|
1385
|
-
return runtime;
|
|
1386
|
-
}
|
|
1387
|
-
|
|
1388
|
-
export {
|
|
1389
|
-
LLMClient,
|
|
1390
|
-
MemoryClient,
|
|
1391
|
-
ContextManager,
|
|
1392
|
-
TaskEngine,
|
|
1393
|
-
buildSystemPrompt,
|
|
1394
|
-
buildMessages,
|
|
1395
|
-
Agent,
|
|
1396
|
-
loadConfig,
|
|
1397
|
-
SesameClient,
|
|
1398
|
-
startPipeline,
|
|
1399
|
-
PRIMARY_ROUTES,
|
|
1400
|
-
WORKER_ROUTES,
|
|
1401
|
-
DEFAULT_HEALTH_INTERVAL_MS,
|
|
1402
|
-
HEALTH_TIMEOUT_MS,
|
|
1403
|
-
DEFAULT_SYNC_INTERVAL_MS,
|
|
1404
|
-
WorkerServer,
|
|
1405
|
-
WorkerRuntime,
|
|
1406
|
-
startWorker
|
|
1407
|
-
};
|
|
1408
|
-
//# sourceMappingURL=chunk-2I2O6X5D.js.map
|