openmagic 0.35.1 → 0.36.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -6,7 +6,7 @@ import chalk from "chalk";
6
6
  import open from "open";
7
7
  import { resolve as resolve3, join as join5 } from "path";
8
8
  import { existsSync as existsSync5, readFileSync as readFileSync5 } from "fs";
9
- import { spawn } from "child_process";
9
+ import { spawn as spawn4 } from "child_process";
10
10
  import { createInterface } from "readline";
11
11
 
12
12
  // src/proxy.ts
@@ -326,6 +326,57 @@ function getProjectTree(roots) {
326
326
 
327
327
  // src/llm/registry.ts
328
328
  var MODEL_REGISTRY = {
329
+ // ─── Claude Code (CLI) ────────────────────────────────────────
330
+ "claude-code": {
331
+ name: "Claude Code (CLI)",
332
+ models: [
333
+ {
334
+ id: "claude-code",
335
+ name: "Claude Code",
336
+ vision: false,
337
+ context: 2e5,
338
+ maxOutput: 64e3
339
+ }
340
+ ],
341
+ apiBase: "",
342
+ keyPrefix: "",
343
+ keyPlaceholder: "not required",
344
+ local: true
345
+ },
346
+ // ─── Codex CLI ────────────────────────────────────────────────
347
+ "codex-cli": {
348
+ name: "Codex CLI",
349
+ models: [
350
+ {
351
+ id: "codex-cli",
352
+ name: "Codex CLI",
353
+ vision: false,
354
+ context: 192e3,
355
+ maxOutput: 1e5
356
+ }
357
+ ],
358
+ apiBase: "",
359
+ keyPrefix: "",
360
+ keyPlaceholder: "not required",
361
+ local: true
362
+ },
363
+ // ─── Gemini CLI ───────────────────────────────────────────────
364
+ "gemini-cli": {
365
+ name: "Gemini CLI",
366
+ models: [
367
+ {
368
+ id: "gemini-cli",
369
+ name: "Gemini CLI",
370
+ vision: false,
371
+ context: 1048576,
372
+ maxOutput: 65536
373
+ }
374
+ ],
375
+ apiBase: "",
376
+ keyPrefix: "",
377
+ keyPlaceholder: "not required",
378
+ local: true
379
+ },
329
380
  // ─── OpenAI ───────────────────────────────────────────────────
330
381
  openai: {
331
382
  name: "OpenAI",
@@ -1467,6 +1518,199 @@ async function chatGoogle(model, apiKey, messages, context, onChunk, onDone, onE
1467
1518
  }
1468
1519
  }
1469
1520
 
1521
+ // src/llm/claude-code.ts
1522
+ import { spawn } from "child_process";
1523
+ async function chatClaudeCode(messages, context, onChunk, onDone, onError) {
1524
+ const lastUserMsg = [...messages].reverse().find((m) => m.role === "user");
1525
+ const userPrompt = typeof lastUserMsg?.content === "string" ? lastUserMsg.content : "Help me with this element.";
1526
+ const contextParts = buildContextParts(context);
1527
+ const fullPrompt = buildUserMessage(userPrompt, contextParts);
1528
+ const proc = spawn(
1529
+ "claude",
1530
+ [
1531
+ "-p",
1532
+ "--output-format",
1533
+ "stream-json",
1534
+ "--verbose",
1535
+ "--max-turns",
1536
+ "1"
1537
+ // Single turn — OpenMagic manages its own retry loop
1538
+ ],
1539
+ {
1540
+ stdio: ["pipe", "pipe", "pipe"],
1541
+ cwd: process.cwd()
1542
+ }
1543
+ );
1544
+ proc.stdin.write(`${SYSTEM_PROMPT}
1545
+
1546
+ ${fullPrompt}`);
1547
+ proc.stdin.end();
1548
+ let fullContent = "";
1549
+ let buffer = "";
1550
+ let errOutput = "";
1551
+ proc.stdout.on("data", (data) => {
1552
+ buffer += data.toString();
1553
+ const lines = buffer.split("\n");
1554
+ buffer = lines.pop() || "";
1555
+ for (const line of lines) {
1556
+ if (!line.trim()) continue;
1557
+ try {
1558
+ const event = JSON.parse(line);
1559
+ const text = extractText(event);
1560
+ if (text) {
1561
+ fullContent += text;
1562
+ onChunk(text);
1563
+ }
1564
+ } catch {
1565
+ }
1566
+ }
1567
+ });
1568
+ proc.stderr.on("data", (data) => {
1569
+ errOutput += data.toString();
1570
+ });
1571
+ proc.on("error", (err) => {
1572
+ if (err.message.includes("ENOENT")) {
1573
+ onError(
1574
+ "Claude CLI not found. Install it with: npm install -g @anthropic-ai/claude-code"
1575
+ );
1576
+ } else {
1577
+ onError(`Claude CLI error: ${err.message}`);
1578
+ }
1579
+ });
1580
+ proc.on("close", (code) => {
1581
+ if (buffer.trim()) {
1582
+ try {
1583
+ const event = JSON.parse(buffer);
1584
+ const text = extractText(event);
1585
+ if (text) fullContent += text;
1586
+ if (event.result && typeof event.result === "string") {
1587
+ fullContent = event.result;
1588
+ }
1589
+ } catch {
1590
+ }
1591
+ }
1592
+ if (code === 0 || fullContent) {
1593
+ onDone({ content: fullContent });
1594
+ } else {
1595
+ const err = errOutput.trim();
1596
+ if (err.includes("not authenticated") || err.includes("login")) {
1597
+ onError("Claude CLI is not authenticated. Run `claude login` in your terminal.");
1598
+ } else if (err.includes("ENOENT") || err.includes("not found")) {
1599
+ onError("Claude CLI not found. Install it with: npm install -g @anthropic-ai/claude-code");
1600
+ } else {
1601
+ onError(err.slice(0, 500) || `Claude CLI exited with code ${code}`);
1602
+ }
1603
+ }
1604
+ });
1605
+ }
1606
+ function extractText(event) {
1607
+ if (event.type === "assistant") {
1608
+ const msg = event.message;
1609
+ if (msg?.content) {
1610
+ if (Array.isArray(msg.content)) {
1611
+ return msg.content.filter((b) => b.type === "text" && b.text).map((b) => b.text).join("");
1612
+ }
1613
+ if (typeof msg.content === "string") return msg.content;
1614
+ }
1615
+ if (typeof event.text === "string") return event.text;
1616
+ }
1617
+ if (event.type === "content_block_delta") {
1618
+ const delta = event.delta;
1619
+ if (typeof delta?.text === "string") return delta.text;
1620
+ }
1621
+ return void 0;
1622
+ }
1623
+
1624
+ // src/llm/codex-cli.ts
1625
+ import { spawn as spawn2 } from "child_process";
1626
+ async function chatCodexCli(messages, context, onChunk, onDone, onError) {
1627
+ const lastUserMsg = [...messages].reverse().find((m) => m.role === "user");
1628
+ const userPrompt = typeof lastUserMsg?.content === "string" ? lastUserMsg.content : "Help me with this element.";
1629
+ const contextParts = buildContextParts(context);
1630
+ const fullPrompt = `${SYSTEM_PROMPT}
1631
+
1632
+ ${buildUserMessage(userPrompt, contextParts)}`;
1633
+ const proc = spawn2("codex", ["--full-auto", fullPrompt], {
1634
+ stdio: ["ignore", "pipe", "pipe"],
1635
+ cwd: process.cwd()
1636
+ });
1637
+ let fullContent = "";
1638
+ let errOutput = "";
1639
+ proc.stdout.on("data", (data) => {
1640
+ const text = data.toString();
1641
+ fullContent += text;
1642
+ onChunk(text);
1643
+ });
1644
+ proc.stderr.on("data", (data) => {
1645
+ errOutput += data.toString();
1646
+ });
1647
+ proc.on("error", (err) => {
1648
+ if (err.message.includes("ENOENT")) {
1649
+ onError("Codex CLI not found. Install it with: npm install -g @openai/codex");
1650
+ } else {
1651
+ onError(`Codex CLI error: ${err.message}`);
1652
+ }
1653
+ });
1654
+ proc.on("close", (code) => {
1655
+ if (code === 0 || fullContent) {
1656
+ onDone({ content: fullContent });
1657
+ } else {
1658
+ const err = errOutput.trim();
1659
+ if (err.includes("OPENAI_API_KEY") || err.includes("api key") || err.includes("unauthorized")) {
1660
+ onError("Codex CLI requires OPENAI_API_KEY in your environment. Set it with: export OPENAI_API_KEY=sk-...");
1661
+ } else {
1662
+ onError(err.slice(0, 500) || `Codex CLI exited with code ${code}`);
1663
+ }
1664
+ }
1665
+ });
1666
+ }
1667
+
1668
+ // src/llm/gemini-cli.ts
1669
+ import { spawn as spawn3 } from "child_process";
1670
+ async function chatGeminiCli(messages, context, onChunk, onDone, onError) {
1671
+ const lastUserMsg = [...messages].reverse().find((m) => m.role === "user");
1672
+ const userPrompt = typeof lastUserMsg?.content === "string" ? lastUserMsg.content : "Help me with this element.";
1673
+ const contextParts = buildContextParts(context);
1674
+ const fullPrompt = `${SYSTEM_PROMPT}
1675
+
1676
+ ${buildUserMessage(userPrompt, contextParts)}`;
1677
+ const proc = spawn3("gemini", [], {
1678
+ stdio: ["pipe", "pipe", "pipe"],
1679
+ cwd: process.cwd()
1680
+ });
1681
+ proc.stdin.write(fullPrompt);
1682
+ proc.stdin.end();
1683
+ let fullContent = "";
1684
+ let errOutput = "";
1685
+ proc.stdout.on("data", (data) => {
1686
+ const text = data.toString();
1687
+ fullContent += text;
1688
+ onChunk(text);
1689
+ });
1690
+ proc.stderr.on("data", (data) => {
1691
+ errOutput += data.toString();
1692
+ });
1693
+ proc.on("error", (err) => {
1694
+ if (err.message.includes("ENOENT")) {
1695
+ onError("Gemini CLI not found. Install it with: npm install -g @google/gemini-cli");
1696
+ } else {
1697
+ onError(`Gemini CLI error: ${err.message}`);
1698
+ }
1699
+ });
1700
+ proc.on("close", (code) => {
1701
+ if (code === 0 || fullContent) {
1702
+ onDone({ content: fullContent });
1703
+ } else {
1704
+ const err = errOutput.trim();
1705
+ if (err.includes("auth") || err.includes("login") || err.includes("credentials")) {
1706
+ onError("Gemini CLI requires Google authentication. Run `gemini auth login` in your terminal.");
1707
+ } else {
1708
+ onError(err.slice(0, 500) || `Gemini CLI exited with code ${code}`);
1709
+ }
1710
+ }
1711
+ });
1712
+ }
1713
+
1470
1714
  // src/llm/proxy.ts
1471
1715
  var OPENAI_COMPATIBLE_PROVIDERS = /* @__PURE__ */ new Set([
1472
1716
  "openai",
@@ -1543,7 +1787,13 @@ async function handleLlmChat(params, onChunk, onDone, onError) {
1543
1787
  onDone({ content: result.content, modifications });
1544
1788
  };
1545
1789
  try {
1546
- if (provider === "anthropic") {
1790
+ if (provider === "claude-code") {
1791
+ await chatClaudeCode(messages, context, onChunk, wrappedOnDone, onError);
1792
+ } else if (provider === "codex-cli") {
1793
+ await chatCodexCli(messages, context, onChunk, wrappedOnDone, onError);
1794
+ } else if (provider === "gemini-cli") {
1795
+ await chatGeminiCli(messages, context, onChunk, wrappedOnDone, onError);
1796
+ } else if (provider === "anthropic") {
1547
1797
  await chatAnthropic(model, apiKey, messages, context, onChunk, wrappedOnDone, onError);
1548
1798
  } else if (provider === "google") {
1549
1799
  await chatGoogle(model, apiKey, messages, context, onChunk, wrappedOnDone, onError);
@@ -2383,7 +2633,7 @@ function waitForPort(port, timeoutMs = 6e4, shouldAbort) {
2383
2633
  function runCommand(cmd, args, cwd = process.cwd()) {
2384
2634
  return new Promise((resolve4) => {
2385
2635
  try {
2386
- const child = spawn(cmd, args, {
2636
+ const child = spawn4(cmd, args, {
2387
2637
  cwd,
2388
2638
  stdio: ["ignore", "pipe", "pipe"],
2389
2639
  shell: true
@@ -2641,7 +2891,7 @@ async function offerToStartDevServer(expectedPort) {
2641
2891
  }
2642
2892
  const staticPort = expectedPort || 8080;
2643
2893
  console.log(chalk.dim(` Starting static server on port ${staticPort}...`));
2644
- const staticChild = spawn("node", ["-e", `
2894
+ const staticChild = spawn4("node", ["-e", `
2645
2895
  const http = require("http");
2646
2896
  const fs = require("fs");
2647
2897
  const path = require("path");
@@ -2815,7 +3065,7 @@ async function offerToStartDevServer(expectedPort) {
2815
3065
  }
2816
3066
  let child;
2817
3067
  try {
2818
- child = spawn(runCmd, runArgs, {
3068
+ child = spawn4(runCmd, runArgs, {
2819
3069
  cwd: process.cwd(),
2820
3070
  stdio: "inherit",
2821
3071
  env: {