@runfusion/fusion 0.1.2 → 0.2.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.
Files changed (69) hide show
  1. package/README.md +2 -0
  2. package/dist/bin.js +4055 -1755
  3. package/dist/client/assets/AgentDetailView-CDZED6Dy.css +1 -0
  4. package/dist/client/assets/AgentDetailView-zycSdnO8.js +28 -0
  5. package/dist/client/assets/AgentsView-DoQkkDLf.css +1 -0
  6. package/dist/client/assets/AgentsView-pO7WiBS5.js +522 -0
  7. package/dist/client/assets/ChatView-BOd-sxbT.js +1 -0
  8. package/dist/client/assets/DevServerView-09GQf34f.js +11 -0
  9. package/dist/client/assets/DevServerView-ZeBGQkLI.css +1 -0
  10. package/dist/client/assets/DirectoryPicker-CcdN1Zs7.js +1 -0
  11. package/dist/client/assets/DocumentsView-CS8aiwtz.js +1 -0
  12. package/dist/client/assets/DocumentsView-Co9to4Zp.css +1 -0
  13. package/dist/client/assets/InsightsView-Bu9Cv8Ol.js +11 -0
  14. package/dist/client/assets/InsightsView-Egu71gmh.css +1 -0
  15. package/dist/client/assets/MemoryView-CtqgDtV9.js +2 -0
  16. package/dist/client/assets/MemoryView-DhinauGs.css +1 -0
  17. package/dist/client/assets/NodesView-BInPcedy.js +14 -0
  18. package/dist/client/assets/NodesView-DlQZHGXA.css +1 -0
  19. package/dist/client/assets/PiExtensionsManager-COxkYM2m.js +11 -0
  20. package/dist/client/assets/PiExtensionsManager-CPgmJgDk.css +1 -0
  21. package/dist/client/assets/PluginManager-CXUWZBOc.js +1 -0
  22. package/dist/client/assets/PluginManager-D64RIzmL.css +1 -0
  23. package/dist/client/assets/RoadmapsView-BOYnyMCh.css +1 -0
  24. package/dist/client/assets/RoadmapsView-BbCexaoi.js +6 -0
  25. package/dist/client/assets/SetupWizardModal-Cakxqkad.js +1 -0
  26. package/dist/client/assets/SkillsView-Cytf009Z.css +1 -0
  27. package/dist/client/assets/SkillsView-D3iqYCVf.js +1 -0
  28. package/dist/client/assets/folder-open-kO5Hsk66.js +6 -0
  29. package/dist/client/assets/index-BiSuUXCa.css +1 -0
  30. package/dist/client/assets/index-y194HxzU.js +644 -0
  31. package/dist/client/assets/upload-DHBQat92.js +6 -0
  32. package/dist/client/index.html +2 -2
  33. package/dist/client/sw.js +45 -1
  34. package/dist/client/theme-data.css +109 -0
  35. package/dist/extension.js +969 -408
  36. package/dist/pi-claude-cli/index.ts +131 -0
  37. package/dist/pi-claude-cli/package.json +39 -0
  38. package/dist/pi-claude-cli/src/__tests__/control-handler.test.ts +191 -0
  39. package/dist/pi-claude-cli/src/__tests__/event-bridge.test.ts +1244 -0
  40. package/dist/pi-claude-cli/src/__tests__/mcp-config.test.ts +272 -0
  41. package/dist/pi-claude-cli/src/__tests__/process-manager.test.ts +619 -0
  42. package/dist/pi-claude-cli/src/__tests__/prompt-builder.test.ts +1067 -0
  43. package/dist/pi-claude-cli/src/__tests__/provider.test.ts +1902 -0
  44. package/dist/pi-claude-cli/src/__tests__/stream-parser.test.ts +188 -0
  45. package/dist/pi-claude-cli/src/__tests__/thinking-config.test.ts +141 -0
  46. package/dist/pi-claude-cli/src/__tests__/tool-mapping.test.ts +252 -0
  47. package/dist/pi-claude-cli/src/control-handler.ts +68 -0
  48. package/dist/pi-claude-cli/src/event-bridge.ts +386 -0
  49. package/dist/pi-claude-cli/src/mcp-config.ts +111 -0
  50. package/dist/pi-claude-cli/src/mcp-schema-server.cjs +49 -0
  51. package/dist/pi-claude-cli/src/process-manager.ts +218 -0
  52. package/dist/pi-claude-cli/src/prompt-builder.ts +536 -0
  53. package/dist/pi-claude-cli/src/provider.ts +354 -0
  54. package/dist/pi-claude-cli/src/stream-parser.ts +37 -0
  55. package/dist/pi-claude-cli/src/thinking-config.ts +83 -0
  56. package/dist/pi-claude-cli/src/tool-mapping.ts +147 -0
  57. package/dist/pi-claude-cli/src/types.ts +87 -0
  58. package/package.json +11 -4
  59. package/skill/fusion/SKILL.md +5 -3
  60. package/skill/fusion/references/cli-commands.md +22 -22
  61. package/skill/fusion/references/extension-tools.md +3 -1
  62. package/skill/fusion/references/fusion-capabilities.md +28 -35
  63. package/skill/fusion/references/task-structure.md +4 -4
  64. package/skill/fusion/workflows/dashboard-cli.md +6 -6
  65. package/skill/fusion/workflows/specifications.md +5 -3
  66. package/skill/fusion/workflows/task-lifecycle.md +1 -1
  67. package/skill/fusion/workflows/task-management.md +3 -1
  68. package/dist/client/assets/index-Djv5vKo0.css +0 -1
  69. package/dist/client/assets/index-zfXYuUXG.js +0 -1241
@@ -0,0 +1,218 @@
1
+ /**
2
+ * Process manager for spawning and managing Claude CLI subprocesses.
3
+ *
4
+ * Handles subprocess lifecycle: spawn with correct CLI flags, write NDJSON
5
+ * messages to stdin, force-kill after result (CLI hangs bug), and stderr capture.
6
+ * Also provides startup validation for CLI presence and authentication.
7
+ */
8
+
9
+ import spawn from "cross-spawn";
10
+ import { execSync } from "node:child_process";
11
+ import { writeFileSync, unlinkSync } from "node:fs";
12
+ import { join } from "node:path";
13
+ import { tmpdir } from "node:os";
14
+ import type { ChildProcess } from "node:child_process";
15
+
16
+ /**
17
+ * Spawn a Claude CLI subprocess with all required flags for stream-json communication.
18
+ *
19
+ * @param modelId - The model ID to pass via --model flag
20
+ * @param systemPrompt - Optional system prompt appended via --append-system-prompt
21
+ * @param options - Optional cwd, AbortSignal, and effort level
22
+ * @returns The spawned ChildProcess with piped stdin/stdout/stderr
23
+ */
24
+ export function spawnClaude(
25
+ modelId: string,
26
+ systemPrompt?: string,
27
+ options?: {
28
+ cwd?: string;
29
+ signal?: AbortSignal;
30
+ effort?: string;
31
+ mcpConfigPath?: string;
32
+ resumeSessionId?: string;
33
+ newSessionId?: string;
34
+ },
35
+ ): ChildProcess {
36
+ const args = [
37
+ "-p",
38
+ "--input-format",
39
+ "stream-json",
40
+ "--output-format",
41
+ "stream-json",
42
+ "--verbose",
43
+ "--include-partial-messages",
44
+ "--model",
45
+ modelId,
46
+ "--permission-prompt-tool",
47
+ "stdio",
48
+ ];
49
+
50
+ if (options?.resumeSessionId) {
51
+ // Resume an existing session — CLI loads prior conversation from disk
52
+ args.push("--resume", options.resumeSessionId);
53
+ } else if (options?.newSessionId) {
54
+ // First turn: create session with this ID so subsequent turns can --resume it
55
+ args.push("--session-id", options.newSessionId);
56
+ }
57
+
58
+ if (systemPrompt) {
59
+ // Write system prompt to a temp file to avoid ENAMETOOLONG on Windows.
60
+ // Claude CLI's --append-system-prompt accepts a file path or literal text.
61
+ const tmpFile = join(
62
+ tmpdir(),
63
+ `pi-claude-cli-sysprompt-${process.pid}.txt`,
64
+ );
65
+ writeFileSync(tmpFile, systemPrompt, "utf-8");
66
+ args.push("--append-system-prompt", tmpFile);
67
+ }
68
+
69
+ if (options?.effort) {
70
+ args.push("--effort", options.effort);
71
+ }
72
+
73
+ if (options?.mcpConfigPath) {
74
+ args.push("--mcp-config", options.mcpConfigPath);
75
+ }
76
+
77
+ const proc = spawn("claude", args, {
78
+ stdio: ["pipe", "pipe", "pipe"],
79
+ cwd: options?.cwd ?? process.cwd(),
80
+ });
81
+
82
+ return proc as ChildProcess;
83
+ }
84
+
85
+ /**
86
+ * Clean up the temp system prompt file created by spawnClaude.
87
+ * Safe to call multiple times or when no file exists.
88
+ */
89
+ export function cleanupSystemPromptFile(): void {
90
+ try {
91
+ unlinkSync(join(tmpdir(), `pi-claude-cli-sysprompt-${process.pid}.txt`));
92
+ } catch {
93
+ // File doesn't exist or already deleted — ignore
94
+ }
95
+ }
96
+
97
+ /**
98
+ * Write a user message to the subprocess stdin as NDJSON.
99
+ * Does NOT call stdin.end() -- stdin stays open for control_response in Phase 2.
100
+ *
101
+ * Accepts both string (text-only prompt) and array (ContentBlock[] with images)
102
+ * content. JSON.stringify handles both natively. The stream-json protocol
103
+ * supports either format in the content field.
104
+ *
105
+ * @param proc - The Claude subprocess
106
+ * @param prompt - The prompt text or ContentBlock[] to send
107
+ */
108
+ export function writeUserMessage(
109
+ proc: ChildProcess,
110
+ prompt: string | unknown[],
111
+ ): void {
112
+ const message = {
113
+ type: "user",
114
+ message: {
115
+ role: "user",
116
+ content: prompt,
117
+ },
118
+ };
119
+ proc.stdin!.write(JSON.stringify(message) + "\n");
120
+ }
121
+
122
+ /**
123
+ * Force-kill a subprocess immediately via SIGKILL.
124
+ * No-ops if the process is already dead (killed or exited).
125
+ * Cross-platform safe: Node.js treats SIGKILL as forceful termination on Windows.
126
+ *
127
+ * @param proc - The subprocess to force-kill
128
+ */
129
+ export function forceKillProcess(proc: ChildProcess): void {
130
+ if (proc.killed || proc.exitCode !== null) return;
131
+ proc.kill("SIGKILL");
132
+ }
133
+
134
+ /** Registry of active subprocesses for cleanup on teardown. */
135
+ const activeProcesses = new Set<ChildProcess>();
136
+
137
+ /**
138
+ * Register a subprocess in the global process registry.
139
+ * The process is automatically removed from the registry when it exits.
140
+ *
141
+ * @param proc - The subprocess to track
142
+ */
143
+ export function registerProcess(proc: ChildProcess): void {
144
+ activeProcesses.add(proc);
145
+ proc.on("exit", () => activeProcesses.delete(proc));
146
+ }
147
+
148
+ /**
149
+ * Force-kill all registered subprocesses and clear the registry.
150
+ * Safe to call multiple times -- no-ops on already-dead processes.
151
+ */
152
+ export function killAllProcesses(): void {
153
+ for (const proc of activeProcesses) {
154
+ forceKillProcess(proc);
155
+ }
156
+ activeProcesses.clear();
157
+ }
158
+
159
+ /**
160
+ * Force-kill the subprocess after a 500ms grace period.
161
+ * The Claude CLI hangs after emitting the result message (known bug).
162
+ * Brief grace period allows final stdout flushing before force-kill.
163
+ *
164
+ * @param proc - The Claude subprocess to clean up
165
+ */
166
+ export function cleanupProcess(proc: ChildProcess): void {
167
+ setTimeout(() => {
168
+ forceKillProcess(proc);
169
+ }, 500);
170
+ }
171
+
172
+ /**
173
+ * Attach a data listener to stderr and accumulate output into a buffer.
174
+ *
175
+ * @param proc - The Claude subprocess
176
+ * @returns A function that returns the accumulated stderr string
177
+ */
178
+ export function captureStderr(proc: ChildProcess): () => string {
179
+ let buffer = "";
180
+ proc.stderr!.on("data", (data: Buffer) => {
181
+ buffer += data.toString();
182
+ });
183
+ return () => buffer;
184
+ }
185
+
186
+ /**
187
+ * Validate that the Claude CLI is installed and on PATH.
188
+ * Throws with install instructions if not found.
189
+ */
190
+ export function validateCliPresence(): void {
191
+ try {
192
+ execSync("claude --version", { stdio: "pipe", timeout: 5000 });
193
+ } catch {
194
+ throw new Error(
195
+ "Claude Code CLI not found. Install it: npm install -g @anthropic-ai/claude-code\n" +
196
+ "Then authenticate: claude auth login",
197
+ );
198
+ }
199
+ }
200
+
201
+ /**
202
+ * Validate that the Claude CLI is authenticated.
203
+ * Returns false and warns if not authenticated.
204
+ *
205
+ * @returns true if authenticated, false otherwise
206
+ */
207
+ export function validateCliAuth(): boolean {
208
+ try {
209
+ execSync("claude auth status", { stdio: "pipe", timeout: 5000 });
210
+ return true;
211
+ } catch {
212
+ console.warn(
213
+ "[pi-claude-cli] Claude CLI is not authenticated. " +
214
+ "Run 'claude auth login' to authenticate.",
215
+ );
216
+ return false;
217
+ }
218
+ }