wispy-cli 0.7.0 → 0.9.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.
@@ -0,0 +1,343 @@
1
+ /**
2
+ * core/subagents.mjs — Sub-agent orchestration for Wispy v0.9.0
3
+ *
4
+ * Class SubAgentManager:
5
+ * - async spawn(opts) → SubAgent
6
+ * - list() → SubAgent[]
7
+ * - get(id) → SubAgent
8
+ * - kill(id) → void
9
+ * - steer(id, message) → void
10
+ * - async waitFor(id, timeoutMs?) → Result
11
+ * - async waitForAll(ids) → Result[]
12
+ */
13
+
14
+ import os from "node:os";
15
+ import path from "node:path";
16
+ import { readFile, writeFile, mkdir } from "node:fs/promises";
17
+ import { WISPY_DIR } from "./config.mjs";
18
+
19
+ const SUBAGENTS_DIR = path.join(WISPY_DIR, "subagents");
20
+
21
+ function makeId() {
22
+ return `sa-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 6)}`;
23
+ }
24
+
25
+ export class SubAgent {
26
+ constructor({ id, task, label, model, timeout, workstream, status = "pending" }) {
27
+ this.id = id;
28
+ this.task = task;
29
+ this.label = label ?? `sub-agent-${id}`;
30
+ this.model = model ?? null;
31
+ this.timeout = timeout ?? 300_000; // ms
32
+ this.workstream = workstream ?? "default";
33
+ this.status = status; // pending → running → completed | failed | killed | timeout
34
+ this.result = null;
35
+ this.error = null;
36
+ this.createdAt = new Date().toISOString();
37
+ this.startedAt = null;
38
+ this.completedAt = null;
39
+ this._abortController = new AbortController();
40
+ this._steerMessages = []; // guidance queue
41
+ this._promise = null; // internal execution promise
42
+ }
43
+
44
+ toJSON() {
45
+ return {
46
+ id: this.id,
47
+ task: this.task,
48
+ label: this.label,
49
+ model: this.model,
50
+ timeout: this.timeout,
51
+ workstream: this.workstream,
52
+ status: this.status,
53
+ result: this.result,
54
+ error: this.error,
55
+ createdAt: this.createdAt,
56
+ startedAt: this.startedAt,
57
+ completedAt: this.completedAt,
58
+ };
59
+ }
60
+ }
61
+
62
+ export class SubAgentManager {
63
+ /**
64
+ * @param {import('./engine.mjs').WispyEngine} engine
65
+ * @param {import('./session.mjs').SessionManager} sessionManager
66
+ */
67
+ constructor(engine, sessionManager) {
68
+ this._engine = engine;
69
+ this._sessions = sessionManager;
70
+ this._agents = new Map(); // id → SubAgent
71
+ }
72
+
73
+ /**
74
+ * Spawn a new sub-agent.
75
+ * @param {object} opts
76
+ * @param {string} opts.task
77
+ * @param {string} [opts.label]
78
+ * @param {string} [opts.model]
79
+ * @param {number} [opts.timeout] - milliseconds (default 300_000)
80
+ * @param {string} [opts.workstream]
81
+ * @param {Function} [opts.onComplete] - callback(result)
82
+ * @param {Function} [opts.onNotify] - channel notification callback(type, text)
83
+ * @returns {Promise<SubAgent>}
84
+ */
85
+ async spawn(opts) {
86
+ const agent = new SubAgent({
87
+ id: makeId(),
88
+ task: opts.task,
89
+ label: opts.label,
90
+ model: opts.model,
91
+ timeout: opts.timeout ? opts.timeout * 1000 : 300_000,
92
+ workstream: opts.workstream ?? this._engine._activeWorkstream,
93
+ });
94
+
95
+ this._agents.set(agent.id, agent);
96
+
97
+ // Run async without awaiting
98
+ agent._promise = this._run(agent, opts).catch((err) => {
99
+ if (agent.status === "running" || agent.status === "pending") {
100
+ agent.status = "failed";
101
+ agent.error = err.message;
102
+ agent.completedAt = new Date().toISOString();
103
+ this._persist(agent);
104
+ opts.onNotify?.("error", `❌ Sub-agent '${agent.label}' failed: ${err.message}`);
105
+ }
106
+ });
107
+
108
+ return agent;
109
+ }
110
+
111
+ /**
112
+ * Internal: run the sub-agent's agentic loop.
113
+ */
114
+ async _run(agent, opts) {
115
+ agent.status = "running";
116
+ agent.startedAt = new Date().toISOString();
117
+
118
+ // Create an isolated session for this sub-agent
119
+ const session = this._sessions.create({ workstream: agent.workstream });
120
+
121
+ // Build initial messages
122
+ const systemPrompt = `You are Wispy 🌿 — a sub-agent handling a delegated task.
123
+ Be focused, thorough, and efficient. Complete the task fully.
124
+ Reply in the same language as the task. Sign off with 🌿.`;
125
+
126
+ const messages = [
127
+ { role: "system", content: systemPrompt },
128
+ { role: "user", content: agent.task },
129
+ ];
130
+
131
+ // Timeout logic
132
+ let timedOut = false;
133
+ const timeoutHandle = setTimeout(() => {
134
+ timedOut = true;
135
+ agent._abortController.abort();
136
+ }, agent.timeout);
137
+
138
+ try {
139
+ const MAX_ROUNDS = 15;
140
+ let round = 0;
141
+
142
+ while (round < MAX_ROUNDS) {
143
+ // Check if killed
144
+ if (agent.status === "killed") break;
145
+ if (timedOut) {
146
+ agent.status = "timeout";
147
+ agent.error = "Timed out";
148
+ agent.completedAt = new Date().toISOString();
149
+ await this._persist(agent);
150
+ opts?.onNotify?.("error", `⏰ Sub-agent '${agent.label}' timed out.`);
151
+ return;
152
+ }
153
+
154
+ // Inject any steering messages
155
+ while (agent._steerMessages.length > 0) {
156
+ const steerMsg = agent._steerMessages.shift();
157
+ messages.push({ role: "user", content: `[Guidance from orchestrator]: ${steerMsg}` });
158
+ }
159
+
160
+ // Call provider
161
+ const result = await this._engine.providers.chat(
162
+ messages,
163
+ this._engine.tools.getDefinitions(),
164
+ { model: agent.model }
165
+ );
166
+
167
+ if (result.type === "text") {
168
+ // Final answer
169
+ agent.result = result.text;
170
+ agent.status = "completed";
171
+ agent.completedAt = new Date().toISOString();
172
+ await this._persist(agent);
173
+
174
+ const summary = result.text.slice(0, 200).replace(/\n/g, " ");
175
+ opts?.onNotify?.("success", `✅ Sub-agent '${agent.label}' completed: ${summary}`);
176
+ opts?.onComplete?.(agent);
177
+ clearTimeout(timeoutHandle);
178
+ return;
179
+ }
180
+
181
+ // Handle tool calls
182
+ messages.push({ role: "assistant", toolCalls: result.calls, content: "" });
183
+
184
+ for (const call of result.calls) {
185
+ const toolResult = await this._engine._executeTool(
186
+ call.name, call.args, messages, session, {}
187
+ );
188
+ messages.push({
189
+ role: "tool_result",
190
+ toolName: call.name,
191
+ toolUseId: call.id ?? call.name,
192
+ result: toolResult,
193
+ });
194
+ }
195
+
196
+ round++;
197
+ }
198
+
199
+ // Max rounds reached
200
+ agent.result = "(max rounds reached — partial work above)";
201
+ agent.status = "completed";
202
+ agent.completedAt = new Date().toISOString();
203
+ await this._persist(agent);
204
+ opts?.onNotify?.("success", `✅ Sub-agent '${agent.label}' completed (max rounds).`);
205
+ } catch (err) {
206
+ clearTimeout(timeoutHandle);
207
+ if (agent.status !== "killed") {
208
+ agent.status = "failed";
209
+ agent.error = err.message;
210
+ agent.completedAt = new Date().toISOString();
211
+ await this._persist(agent);
212
+ opts?.onNotify?.("error", `❌ Sub-agent '${agent.label}' failed: ${err.message}`);
213
+ }
214
+ } finally {
215
+ clearTimeout(timeoutHandle);
216
+ }
217
+ }
218
+
219
+ /**
220
+ * List all sub-agents (active + recent in-memory).
221
+ */
222
+ list() {
223
+ return Array.from(this._agents.values());
224
+ }
225
+
226
+ /**
227
+ * Get a sub-agent by ID.
228
+ */
229
+ get(id) {
230
+ return this._agents.get(id) ?? null;
231
+ }
232
+
233
+ /**
234
+ * Kill (cancel) a running sub-agent.
235
+ */
236
+ kill(id) {
237
+ const agent = this._agents.get(id);
238
+ if (!agent) return;
239
+ if (agent.status === "running" || agent.status === "pending") {
240
+ agent.status = "killed";
241
+ agent.completedAt = new Date().toISOString();
242
+ agent._abortController.abort();
243
+ this._persist(agent).catch(() => {});
244
+ }
245
+ }
246
+
247
+ /**
248
+ * Send guidance/steering to a running sub-agent.
249
+ */
250
+ steer(id, message) {
251
+ const agent = this._agents.get(id);
252
+ if (!agent) throw new Error(`Sub-agent not found: ${id}`);
253
+ if (agent.status !== "running" && agent.status !== "pending") {
254
+ throw new Error(`Sub-agent ${id} is not running (status: ${agent.status})`);
255
+ }
256
+ agent._steerMessages.push(message);
257
+ }
258
+
259
+ /**
260
+ * Wait for a specific sub-agent to complete.
261
+ * @param {string} id
262
+ * @param {number} [timeoutMs]
263
+ * @returns {Promise<{id, status, result, error}>}
264
+ */
265
+ async waitFor(id, timeoutMs) {
266
+ const agent = this._agents.get(id);
267
+ if (!agent) throw new Error(`Sub-agent not found: ${id}`);
268
+
269
+ if (agent.status === "completed" || agent.status === "failed" ||
270
+ agent.status === "killed" || agent.status === "timeout") {
271
+ return agent.toJSON();
272
+ }
273
+
274
+ if (!agent._promise) throw new Error(`Sub-agent ${id} has no active promise`);
275
+
276
+ if (timeoutMs) {
277
+ const timeoutPromise = new Promise((_, reject) =>
278
+ setTimeout(() => reject(new Error(`waitFor timed out after ${timeoutMs}ms`)), timeoutMs)
279
+ );
280
+ await Promise.race([agent._promise, timeoutPromise]);
281
+ } else {
282
+ await agent._promise;
283
+ }
284
+
285
+ return agent.toJSON();
286
+ }
287
+
288
+ /**
289
+ * Wait for multiple sub-agents to complete.
290
+ * @param {string[]} ids
291
+ * @returns {Promise<Array>}
292
+ */
293
+ async waitForAll(ids) {
294
+ return Promise.all(ids.map(id => this.waitFor(id)));
295
+ }
296
+
297
+ /**
298
+ * Persist a sub-agent's result to disk.
299
+ */
300
+ async _persist(agent) {
301
+ try {
302
+ await mkdir(SUBAGENTS_DIR, { recursive: true });
303
+ const filePath = path.join(SUBAGENTS_DIR, `${agent.id}.json`);
304
+ await writeFile(filePath, JSON.stringify(agent.toJSON(), null, 2) + "\n", "utf8");
305
+ } catch {
306
+ // Non-fatal
307
+ }
308
+ }
309
+
310
+ /**
311
+ * Load persisted sub-agent from disk (for history).
312
+ */
313
+ async loadFromDisk(id) {
314
+ try {
315
+ const filePath = path.join(SUBAGENTS_DIR, `${id}.json`);
316
+ const data = JSON.parse(await readFile(filePath, "utf8"));
317
+ return data;
318
+ } catch {
319
+ return null;
320
+ }
321
+ }
322
+
323
+ /**
324
+ * List persisted sub-agent history from disk.
325
+ */
326
+ async listHistory(limit = 20) {
327
+ try {
328
+ const { readdir } = await import("node:fs/promises");
329
+ const files = await readdir(SUBAGENTS_DIR);
330
+ const jsonFiles = files.filter(f => f.endsWith(".json")).sort().reverse().slice(0, limit);
331
+ const results = [];
332
+ for (const f of jsonFiles) {
333
+ try {
334
+ const data = JSON.parse(await readFile(path.join(SUBAGENTS_DIR, f), "utf8"));
335
+ results.push(data);
336
+ } catch {}
337
+ }
338
+ return results;
339
+ } catch {
340
+ return [];
341
+ }
342
+ }
343
+ }
package/core/tools.mjs CHANGED
@@ -221,6 +221,60 @@ export class ToolRegistry {
221
221
  required: ["task"],
222
222
  },
223
223
  },
224
+ // ── Sub-agent tools (v0.9) ───────────────────────────────────────────────
225
+ {
226
+ name: "spawn_subagent",
227
+ description: "Spawn an isolated sub-agent to handle a task independently. Use for parallel work, long-running tasks, or delegating to specialized models.",
228
+ parameters: {
229
+ type: "object",
230
+ properties: {
231
+ task: { type: "string", description: "The task description/prompt for the sub-agent" },
232
+ label: { type: "string", description: "Human-readable label for the sub-agent" },
233
+ model: { type: "string", description: "Model override (e.g., 'flash' for cheap tasks, 'claude-opus-4-5' for complex)" },
234
+ timeout_seconds: { type: "number", description: "Max runtime in seconds (default 300)" },
235
+ },
236
+ required: ["task"],
237
+ },
238
+ },
239
+ {
240
+ name: "list_subagents",
241
+ description: "List all sub-agents (active and recent)",
242
+ parameters: { type: "object", properties: {}, required: [] },
243
+ },
244
+ {
245
+ name: "get_subagent_result",
246
+ description: "Get the result of a completed sub-agent",
247
+ parameters: {
248
+ type: "object",
249
+ properties: {
250
+ id: { type: "string", description: "Sub-agent ID" },
251
+ },
252
+ required: ["id"],
253
+ },
254
+ },
255
+ {
256
+ name: "kill_subagent",
257
+ description: "Cancel a running sub-agent",
258
+ parameters: {
259
+ type: "object",
260
+ properties: {
261
+ id: { type: "string", description: "Sub-agent ID" },
262
+ },
263
+ required: ["id"],
264
+ },
265
+ },
266
+ {
267
+ name: "steer_subagent",
268
+ description: "Send additional guidance to a running sub-agent",
269
+ parameters: {
270
+ type: "object",
271
+ properties: {
272
+ id: { type: "string", description: "Sub-agent ID" },
273
+ message: { type: "string", description: "Guidance message" },
274
+ },
275
+ required: ["id", "message"],
276
+ },
277
+ },
224
278
  ];
225
279
 
226
280
  for (const def of builtins) {
@@ -514,6 +568,12 @@ export class ToolRegistry {
514
568
  case "pipeline":
515
569
  case "spawn_async_agent":
516
570
  case "ralph_loop":
571
+ // Sub-agent tools (v0.9) — handled at engine level
572
+ case "spawn_subagent":
573
+ case "list_subagents":
574
+ case "get_subagent_result":
575
+ case "kill_subagent":
576
+ case "steer_subagent":
517
577
  return { success: false, error: `Tool "${name}" requires engine context. Call via WispyEngine.processMessage().` };
518
578
 
519
579
  default:
@@ -25,6 +25,7 @@ import {
25
25
  WISPY_DIR,
26
26
  CONVERSATIONS_DIR,
27
27
  MEMORY_DIR,
28
+ MemoryManager,
28
29
  } from "../core/index.mjs";
29
30
 
30
31
  // ---------------------------------------------------------------------------
@@ -325,6 +326,16 @@ ${bold("Wispy Commands:")}
325
326
  ${cyan("/skills")} List installed skills
326
327
  ${cyan("/sessions")} List sessions
327
328
  ${cyan("/mcp")} [list|connect|disconnect|config|reload] MCP management
329
+ ${cyan("/remember")} <text> Save text to main memory (MEMORY.md)
330
+ ${cyan("/forget")} <key> Delete a memory file
331
+ ${cyan("/memories")} List all memory files
332
+ ${cyan("/recall")} <query> Search memories
333
+
334
+ ${bold("Sub-agent Commands (v0.9):")}
335
+ ${cyan("/agents")} List active/recent sub-agents
336
+ ${cyan("/agent")} <id> Show sub-agent details and result
337
+ ${cyan("/kill")} <id> Cancel a running sub-agent
338
+
328
339
  ${cyan("/quit")} or ${cyan("/exit")} Exit
329
340
  `);
330
341
  return true;
@@ -555,6 +566,126 @@ ${bold("Wispy Commands:")}
555
566
  return true;
556
567
  }
557
568
 
569
+ // ── Memory commands ──────────────────────────────────────────────────────────
570
+
571
+ if (cmd === "/remember") {
572
+ const text = parts.slice(1).join(" ");
573
+ if (!text) { console.log(yellow("Usage: /remember <text>")); return true; }
574
+ await engine.memory.append("MEMORY", text);
575
+ console.log(green("✅ Saved to MEMORY.md"));
576
+ return true;
577
+ }
578
+
579
+ if (cmd === "/forget") {
580
+ const key = parts[1];
581
+ if (!key) { console.log(yellow("Usage: /forget <key>")); return true; }
582
+ const result = await engine.memory.delete(key);
583
+ if (result.success) {
584
+ console.log(green(`🗑️ Deleted memory: ${key}`));
585
+ } else {
586
+ console.log(red(`Memory "${key}" not found.`));
587
+ }
588
+ return true;
589
+ }
590
+
591
+ if (cmd === "/memories") {
592
+ const keys = await engine.memory.list();
593
+ if (keys.length === 0) {
594
+ console.log(dim("No memories stored yet. Use /remember <text> or ask wispy to remember things."));
595
+ } else {
596
+ console.log(bold(`\n🧠 Memories (${keys.length}):\n`));
597
+ for (const k of keys) {
598
+ console.log(` ${cyan(k.key.padEnd(30))} ${dim(k.preview ?? "")}`);
599
+ }
600
+ }
601
+ return true;
602
+ }
603
+
604
+ if (cmd === "/recall") {
605
+ const query = parts.slice(1).join(" ");
606
+ if (!query) { console.log(yellow("Usage: /recall <query>")); return true; }
607
+ const results = await engine.memory.search(query);
608
+ if (results.length === 0) {
609
+ console.log(dim(`No memories found for: "${query}"`));
610
+ } else {
611
+ console.log(bold(`\n🔍 Memory search: "${query}"\n`));
612
+ for (const r of results) {
613
+ console.log(` ${cyan(r.key)} ${dim(`(${r.matchCount} matches)`)}`);
614
+ for (const s of r.snippets.slice(0, 3)) {
615
+ console.log(` ${dim(`L${s.lineNumber}:`)} ${s.text.slice(0, 100)}`);
616
+ }
617
+ console.log("");
618
+ }
619
+ }
620
+ return true;
621
+ }
622
+
623
+ // ── Sub-agent commands (v0.9) ──────────────────────────────────────────────
624
+
625
+ if (cmd === "/agents") {
626
+ const inMemory = engine.subagents.list();
627
+ const history = await engine.subagents.listHistory(20);
628
+ const inMemoryIds = new Set(inMemory.map(a => a.id));
629
+ const all = [
630
+ ...inMemory.map(a => a.toJSON()),
631
+ ...history.filter(h => !inMemoryIds.has(h.id)),
632
+ ].slice(0, 30);
633
+
634
+ if (all.length === 0) {
635
+ console.log(dim("No sub-agents yet. Ask wispy to spawn one!"));
636
+ } else {
637
+ console.log(bold(`\n🤖 Sub-agents (${all.length}):\n`));
638
+ for (const a of all) {
639
+ const icon = { completed: green("✓"), failed: red("✗"), running: cyan("⟳"), killed: dim("✕"), timeout: yellow("⏰"), pending: dim("…") }[a.status] ?? "?";
640
+ const elapsed = a.completedAt ? "" : a.startedAt ? ` ${dim(`(running ${Math.round((Date.now() - new Date(a.startedAt)) / 1000)}s)`)}` : "";
641
+ console.log(` ${icon} ${bold(a.id)} ${dim("·")} ${a.label}${elapsed}`);
642
+ console.log(` ${dim(`task: ${(a.task ?? "").slice(0, 70)}${(a.task?.length ?? 0) > 70 ? "…" : ""}`)}`);
643
+ if (a.result) console.log(` ${dim(`result: ${a.result.slice(0, 80)}${a.result.length > 80 ? "…" : ""}`)}`);
644
+ if (a.error) console.log(` ${red(`error: ${a.error.slice(0, 80)}`)}`);
645
+ console.log("");
646
+ }
647
+ }
648
+ return true;
649
+ }
650
+
651
+ if (cmd === "/agent") {
652
+ const id = parts[1];
653
+ if (!id) { console.log(yellow("Usage: /agent <id>")); return true; }
654
+
655
+ let agent = engine.subagents.get(id)?.toJSON();
656
+ if (!agent) agent = await engine.subagents.loadFromDisk(id);
657
+
658
+ if (!agent) {
659
+ console.log(red(`Sub-agent not found: ${id}`));
660
+ } else {
661
+ console.log(bold(`\n🤖 Sub-agent: ${agent.id}\n`));
662
+ console.log(` ${dim("Label:")} ${agent.label}`);
663
+ console.log(` ${dim("Status:")} ${agent.status}`);
664
+ console.log(` ${dim("Model:")} ${agent.model ?? "(default)"}`);
665
+ console.log(` ${dim("Created:")} ${agent.createdAt}`);
666
+ if (agent.startedAt) console.log(` ${dim("Started:")} ${agent.startedAt}`);
667
+ if (agent.completedAt) console.log(` ${dim("Done:")} ${agent.completedAt}`);
668
+ console.log(`\n ${bold("Task:")}\n ${dim(agent.task)}\n`);
669
+ if (agent.result) {
670
+ console.log(` ${bold("Result:")}\n ${agent.result.slice(0, 2000)}`);
671
+ }
672
+ if (agent.error) {
673
+ console.log(` ${bold("Error:")} ${red(agent.error)}`);
674
+ }
675
+ }
676
+ return true;
677
+ }
678
+
679
+ if (cmd === "/kill") {
680
+ const id = parts[1];
681
+ if (!id) { console.log(yellow("Usage: /kill <id>")); return true; }
682
+ const agent = engine.subagents.get(id);
683
+ if (!agent) { console.log(red(`Sub-agent not found: ${id}`)); return true; }
684
+ engine.subagents.kill(id);
685
+ console.log(green(`🛑 Sub-agent '${agent.label}' killed.`));
686
+ return true;
687
+ }
688
+
558
689
  if (cmd === "/quit" || cmd === "/exit") {
559
690
  console.log(dim(`🌿 Bye! (${engine.providers.formatCost()})`));
560
691
  engine.destroy();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wispy-cli",
3
- "version": "0.7.0",
3
+ "version": "0.9.0",
4
4
  "description": "🌿 Wispy — AI workspace assistant with multi-agent orchestration and multi-channel bot support",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Minseo & Poropo",
@@ -44,15 +44,17 @@
44
44
  "test": "node --test test/basic.test.mjs"
45
45
  },
46
46
  "dependencies": {
47
+ "cron-parser": "^5.5.0",
47
48
  "ink": "^5.2.1",
48
49
  "ink-spinner": "^5.0.0",
49
50
  "ink-text-input": "^6.0.0",
50
- "react": "^18.3.1"
51
+ "react": "^18.3.1",
52
+ "uuid": "^13.0.0"
51
53
  },
52
54
  "peerDependencies": {
53
- "grammy": ">=1.0.0",
55
+ "@slack/bolt": ">=3.0.0",
54
56
  "discord.js": ">=14.0.0",
55
- "@slack/bolt": ">=3.0.0"
57
+ "grammy": ">=1.0.0"
56
58
  },
57
59
  "peerDependenciesMeta": {
58
60
  "grammy": {