skyloom 1.9.0 → 1.11.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/src/cli/main.ts CHANGED
@@ -10,6 +10,7 @@ import { createSystemContext, orchestrateTask } from "../core/factory";
10
10
  import { loadConfig, USER_CONFIG_DIR } from "../core/config";
11
11
  import { classify } from "../core/router";
12
12
  import { InteractiveMode, ModeController } from "./mode";
13
+ import { readInput, type TUIContext } from "./tui";
13
14
 
14
15
  const MODE = new ModeController();
15
16
  const VERSION = (() => { try { return require("../../package.json").version; } catch { return "1.5.2"; } })();
@@ -77,6 +78,12 @@ program.command("web").option("-p,--port <p>", "port", "3000")
77
78
  program.command("mcp").action(() => { import("../core/mcp_server").then(m => m.startMCPServer()); });
78
79
  program.command("config").action(() => { const c = loadConfig(); process.stdout.write(chalk.cyan("\nConfig: ") + USER_CONFIG_DIR + "\n"); for (const [n, a] of Object.entries(c.agents || {})) process.stdout.write(` ${chalk.bold(n)}: ${(a as any).model || "default"}\n`); });
79
80
  program.command("init").action(() => { if (!fs.existsSync(USER_CONFIG_DIR)) fs.mkdirSync(USER_CONFIG_DIR, { recursive: true }); process.stdout.write(chalk.green("✓ ") + USER_CONFIG_DIR + "\n"); });
81
+ program.command("apikey").description("Manage API keys (persisted to ~/.skyloom/config.yaml)")
82
+ .argument("[action]", "set|list").argument("[provider]", "e.g. deepseek").argument("[key]", "API key")
83
+ .action((action?: string, provider?: string, key?: string) => {
84
+ if (action === "set" && provider && key) { saveApiKey(provider, key); process.stdout.write(chalk.green("✓ Saved " + provider + " API key\n")); }
85
+ else { process.stdout.write(chalk.dim("Usage: sky apikey set deepseek YOUR_KEY\n")); }
86
+ });
80
87
  program.command("version").action(() => { process.stdout.write(`Skyloom v${VERSION}\n`); });
81
88
 
82
89
  /* ═══════════════════════════════════════
@@ -143,20 +150,45 @@ function render(text: string): string[] {
143
150
  /* ═══════════════════════════════════════
144
151
  Chat loop
145
152
  ═══════════════════════════════════════ */
146
- /* Check for API key availability */
153
+ /* API key persistence — read from config file too */
147
154
  function checkApiKeys(): string | null {
148
- const keys = ["DEEPSEEK_API_KEY","OPENAI_API_KEY","ANTHROPIC_API_KEY","GROQ_API_KEY","OPENROUTER_API_KEY"];
149
- for (const k of keys) { if (process.env[k]) return k; }
155
+ // Check env vars
156
+ const envKeys = ["DEEPSEEK_API_KEY","OPENAI_API_KEY","ANTHROPIC_API_KEY","GROQ_API_KEY","OPENROUTER_API_KEY"];
157
+ for (const k of envKeys) { if (process.env[k]) return "env:" + k; }
158
+ // Check config file
159
+ try {
160
+ const path = require("path"); const fs = require("fs"); const yaml = require("yaml");
161
+ const cfgPath = path.join(require("os").homedir(), ".skyloom", "config.yaml");
162
+ if (fs.existsSync(cfgPath)) {
163
+ const cfg = yaml.parse(fs.readFileSync(cfgPath, "utf-8")) || {};
164
+ const keys = cfg.api_keys || {};
165
+ for (const [p, k] of Object.entries(keys)) { if (k) return "cfg:" + p; }
166
+ }
167
+ } catch { /* ignore */ }
150
168
  return null;
151
169
  }
152
170
 
171
+ /** Save API key to config file */
172
+ function saveApiKey(provider: string, key: string): void {
173
+ const path = require("path"); const fs = require("fs"); const yaml = require("yaml");
174
+ const cfgPath = path.join(require("os").homedir(), ".skyloom", "config.yaml");
175
+ const dir = path.dirname(cfgPath);
176
+ if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
177
+ let cfg: any = {};
178
+ if (fs.existsSync(cfgPath)) { try { cfg = yaml.parse(fs.readFileSync(cfgPath, "utf-8")) || {}; } catch { } }
179
+ if (!cfg.api_keys) cfg.api_keys = {};
180
+ cfg.api_keys[provider] = key;
181
+ fs.writeFileSync(cfgPath, yaml.stringify(cfg), "utf-8");
182
+ }
183
+
153
184
  async function chat(agentName: string, modelOverride?: string): Promise<void> {
154
185
  const haveKey = checkApiKeys();
155
186
  if (!haveKey) {
156
187
  process.stdout.write("\n" + chalk.yellow(" ⚠ No API key configured.\n"));
157
- process.stdout.write(chalk.dim(" Set one: $env:DEEPSEEK_API_KEY = \"sk-your-key\" (PowerShell)\n"));
158
- process.stdout.write(chalk.dim(" export DEEPSEEK_API_KEY=sk-your-key (Bash)\n\n"));
159
- process.stdout.write(chalk.dim(" Then run: sky\n\n"));
188
+ process.stdout.write(chalk.dim(" Quick setup:\n"));
189
+ process.stdout.write(chalk.dim(" sky apikey set deepseek sk-your-key-here\n"));
190
+ process.stdout.write(chalk.dim(" Or env var:\n"));
191
+ process.stdout.write(chalk.dim(" $env:DEEPSEEK_API_KEY = \"sk-your-key\"\n\n"));
160
192
  process.exit(1);
161
193
  }
162
194
 
@@ -186,46 +218,56 @@ async function chat(agentName: string, modelOverride?: string): Promise<void> {
186
218
 
187
219
  process.stdout.write(chalk.dim(" Key: " + haveKey + "\n\n"));
188
220
 
189
- const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
221
+ // ── TUI loop ──
222
+ const ctx_: TUIContext = { agent: currentAgent, agents: ctx.agentMap, model: "default", cost: "$0", width: 80, height: 24 };
190
223
 
191
- function ask() { rl.question(chalk.cyan(" " + currentAgent.displayName + " ❯ "), handler); }
192
- async function handler(inp: string) {
193
- inp = inp.trim();
194
- if (!inp) { ask(); return; }
224
+ while (true) {
225
+ const inp = await readInput(process.stdin, process.stdout, ctx_);
226
+ if (!inp) continue;
195
227
 
196
228
  const cmdL = inp.toLowerCase();
197
229
 
198
230
  // Agent switch
231
+ let switched = false;
199
232
  for (const n of AGENT_NAMES) {
200
- if (cmdL === "/" + n) { const a = ctx.agentMap.get(n); if (a) { await a.init(); currentAgent = a; } process.stdout.write(chalk.dim(" ⟳ " + AGENT_DISPLAY[n] + "\n")); ask(); return; }
233
+ if (cmdL === "/" + n) { const a = ctx.agentMap.get(n); if (a) { await a.init(); currentAgent = a; ctx_.agent = a; } switched = true; break; }
201
234
  }
202
-
203
- if (cmdL === "/quit" || cmdL === "/exit") { process.stdout.write(chalk.dim("\n Session ended\n")); rl.close(); await ctx.closeAll(); process.exit(0); return; }
204
- if (cmdL === "/help") { process.stdout.write(helpText()); ask(); return; }
205
- if (cmdL === "/clear") { console.clear(); welcome(agent); process.stdout.write(chalk.dim(" Key: " + haveKey + "\n\n")); ask(); return; }
206
- if (cmdL === "/status") { process.stdout.write(chalk.bold("\n " + currentAgent.displayName + " (" + currentAgent.name + ")\n") + chalk.dim(" State: " + currentAgent.state + " · Memory: " + currentAgent.memory.shortTerm.length + " msgs\n\n")); ask(); return; }
207
- if (cmdL === "/cost") { process.stdout.write(chalk.bold("\n Total: " + formatCost(ctx.llm.getTotalCost()) + "\n\n")); ask(); return; }
208
- if (cmdL === "/compact") { const r = await currentAgent.compact(); process.stdout.write(chalk.green(" " + r + "\n\n")); ask(); return; }
209
- if (cmdL === "/version") { process.stdout.write(" Skyloom v" + VERSION + "\n"); ask(); return; }
210
- if (cmdL.startsWith("/task ")) { const g = inp.slice(6); process.stdout.write(chalk.cyan("\n " + g + "\n\n")); await runTask(g); ask(); return; }
211
- if (inp.startsWith("/")) { process.stdout.write(helpText()); ask(); return; }
235
+ if (switched) continue;
236
+ if (cmdL === "/quit" || cmdL === "/exit") break;
237
+ if (cmdL === "/clear") { console.clear(); continue; }
238
+ if (cmdL === "/help") { process.stdout.write(helpText()); continue; }
239
+ if (cmdL === "/version") { process.stdout.write(" Skyloom v" + VERSION + "\n"); continue; }
240
+ if (cmdL === "/status") { process.stdout.write(chalk.bold("\n " + currentAgent.displayName + " (" + currentAgent.name + ")\n") + chalk.dim(" State: " + currentAgent.state + " · Memory: " + currentAgent.memory.shortTerm.length + " msgs\n\n")); continue; }
241
+ if (cmdL === "/cost") { process.stdout.write(chalk.bold("\n Total: " + formatCost(ctx.llm.getTotalCost()) + "\n\n")); continue; }
242
+ if (cmdL === "/cost reset") { (ctx.llm as any).resetUsageStats?.(); process.stdout.write(chalk.dim(" Stats reset\n")); continue; }
243
+ if (cmdL === "/compact") { const r = await currentAgent.compact(); process.stdout.write(chalk.green(" " + r + "\n\n")); continue; }
244
+ if (cmdL === "/memory") { process.stdout.write(chalk.dim(" Short-term: " + currentAgent.memory.shortTerm.length + " msgs · Working: " + Object.keys(currentAgent.memory.working).length + " keys\n")); continue; }
245
+ if (cmdL === "/memory clear") { await currentAgent.memory.clearShortTerm(); process.stdout.write(chalk.dim(" Memory cleared\n")); continue; }
246
+ if (cmdL === "/workspace") { process.stdout.write(chalk.dim(" " + (ctx.workspacePath || "default") + "\n")); continue; }
247
+ if (cmdL === "/sessions") { const ss = await currentAgent.memory.listSessions(); process.stdout.write(chalk.bold("\n Sessions:\n")); for (const s of ss.slice(0, 10)) process.stdout.write(chalk.dim(" " + s.id?.slice(0, 10) + "... " + s.preview + " (" + s.messageCount + " msgs)\n")); continue; }
248
+ if (cmdL === "/mcp") { process.stdout.write(chalk.dim(" " + (ctx.mcpStatus?.join(", ") || "none") + "\n")); continue; }
249
+ if (cmdL.startsWith("/apikey set ")) { const p = inp.split(/\s+/); if (p.length >= 4) { saveApiKey(p[2], p[3]); process.stdout.write(chalk.green(" ✓ Saved " + p[2] + " API key\n")); } else { process.stdout.write(chalk.yellow(" Usage: /apikey set <provider> <key>\n")); } continue; }
250
+ if (cmdL === "/apikey") { process.stdout.write(chalk.bold("\n API Keys:\n")); for (const p of ["openai","deepseek","anthropic","groq","openrouter"]) { process.stdout.write(chalk.dim(" " + p.padEnd(14) + (!!process.env[p.toUpperCase() + "_API_KEY"] ? chalk.green("env") : chalk.dim("—")) + "\n")); } process.stdout.write("\n"); continue; }
251
+ if (cmdL.startsWith("/task ")) { const g = inp.slice(6); process.stdout.write(chalk.cyan("\n ✦ " + g + "\n\n")); await runTask(g); continue; }
252
+ if (cmdL.startsWith("/model")) { process.stdout.write(chalk.dim(" Configure in ~/.skyloom/config.yaml\n\n")); continue; }
253
+ if (inp.startsWith("/")) { process.stdout.write(helpText()); continue; }
212
254
 
213
255
  // ── Chat ──
214
256
  process.stdout.write(chalk.dim(" " + currentAgent.displayName + " thinking...\r"));
215
257
  try {
216
258
  const response = await currentAgent.chat(inp);
217
259
  process.stdout.write("\r" + " ".repeat(40) + "\r\n");
218
- const lines = render(response);
219
- for (const l of lines) process.stdout.write(l + "\n");
260
+ for (const l of render(response)) process.stdout.write(l + "\n");
220
261
  process.stdout.write("\n");
221
262
  } catch (e: any) {
222
263
  process.stdout.write("\r" + " ".repeat(40) + "\r");
223
264
  process.stdout.write(chalk.red(" ✗ " + (e.message || e) + "\n\n"));
224
265
  }
225
- ask();
226
266
  }
227
267
 
228
- ask();
268
+ process.stdout.write(chalk.dim("\n Session ended\n"));
269
+ await ctx.closeAll();
270
+ process.exit(0);
229
271
  }
230
272
 
231
273
  /* ═══════════════════════════════════════
package/src/cli/tui.ts ADDED
@@ -0,0 +1,268 @@
1
+ /**
2
+ * 天空织机 TUI — Full-screen terminal interface
3
+ *
4
+ * Layout:
5
+ * ┌─────────────────────────────────────────┐
6
+ * │ ≋ 雾 Fog · deepseek-chat · $0.02 · ⏻ │ ← header bar
7
+ * ├──────────┬──────────────────────────────┤
8
+ * │ ☼ 晴 Fair│ ✦ 你好!有什么可以帮你的? │
9
+ * │ ✱ 霜 │ │ ← messages
10
+ * │ ≋ 雾 ▸ │ 用户消息右对齐 │
11
+ * │ ❉ 雪 │ │
12
+ * │ ∘ 露 │ │
13
+ * │ ⸽ 雨 │ │
14
+ * ├──────────┴──────────────────────────────┤
15
+ * │ ┌─ /fog /rain /frost /snow ───────┐│ ← command palette (popup)
16
+ * │ ▶ /fog Switch to Fog ││
17
+ * │ /rain Switch to Rain ││
18
+ * │ └──────────────────────────────────────┘│
19
+ * │ > hello world [send] │ ← input bar
20
+ * └─────────────────────────────────────────┘
21
+ */
22
+
23
+ import * as readline from "readline";
24
+ import chalk from "chalk";
25
+
26
+ export interface TUIContext {
27
+ agent: any;
28
+ agents: Map<string, any>;
29
+ model: string;
30
+ cost: string;
31
+ width: number;
32
+ height: number;
33
+ }
34
+
35
+ /* ── Slash commands with icons ── */
36
+ const AGENT_CMDS: [string, string, string][] = [
37
+ ["≋", "/fog", "雾 Fog · 松烟墨"],
38
+ ["⸽", "/rain", "雨 Rain · 石青"],
39
+ ["✱", "/frost", "霜 Frost · 石绿"],
40
+ ["❉", "/snow", "雪 Snow · 铅白"],
41
+ ["∘", "/dew", "露 Dew · 赭石"],
42
+ ["☼", "/fair", "晴 Fair · 朱砂"],
43
+ ];
44
+
45
+ const ACTION_CMDS: [string, string][] = [
46
+ ["/help", "所有命令"],
47
+ ["/clear", "清屏"],
48
+ ["/status", "状态总览"],
49
+ ["/cost", "费用统计"],
50
+ ["/cost reset", "费用归零"],
51
+ ["/compact", "压缩上下文"],
52
+ ["/retry", "重发上条"],
53
+ ["/apikey set <p> <k>", "保存API Key"],
54
+ ["/apikey", "查看API Key"],
55
+ ["/model", "模型管理"],
56
+ ["/task <goal>", "多Agent编排"],
57
+ ["/memory", "记忆状态"],
58
+ ["/memory clear", "清除记忆"],
59
+ ["/sessions", "会话列表"],
60
+ ["/workspace", "工作空间"],
61
+ ["/mcp", "MCP服务器"],
62
+ ["/version", "版本信息"],
63
+ ["/quit", "退出"],
64
+ ];
65
+
66
+ /* ── Box drawing characters ── */
67
+ const B = { tl: "┌", tr: "┐", bl: "└", br: "┘", h: "─", v: "│", l: "├", r: "┤", cross: "┼", t: "┬", b: "┴", L: "░", o: "●" };
68
+
69
+ function bar(start: string, fill: string, end: string, width: number): string {
70
+ return start + fill.repeat(Math.max(0, width)) + end;
71
+ }
72
+
73
+ /* ── Render sidebar ── */
74
+ function renderSidebar(agent: any, agents: Map<string, any>, h: number): string[] {
75
+ const lines: string[] = [];
76
+ const W = 14; // sidebar width in chars
77
+
78
+ // Header
79
+ lines.push(chalk.cyan(bar(B.L + " 天空织机 ".padEnd(W - 2, B.L) + B.r, "", "", 0)));
80
+ lines.push(chalk.dim(B.v + " Skyloom " + B.v));
81
+
82
+ for (const n of ["fog", "rain", "frost", "snow", "dew", "fair"]) {
83
+ const isActive = agent.name === n;
84
+ const display: Record<string, string> = { fog: "≋ 雾 Fog", rain: "⸽ 雨 Rain", frost: "✱ 霜 Frost", snow: "❉ 雪 Snow", dew: "∘ 露 Dew", fair: "☼ 晴 Fair" };
85
+ const line = isActive
86
+ ? chalk.cyan(B.v + " " + B.o + " " + display[n].padEnd(W - 5) + B.v)
87
+ : chalk.dim(B.v + " " + display[n].padEnd(W - 5) + B.v);
88
+ lines.push(line);
89
+ }
90
+
91
+ // Fill remaining space
92
+ for (let i = lines.length; i < h; i++) {
93
+ lines.push(chalk.dim(B.v + " ".repeat(W - 2) + B.v));
94
+ }
95
+
96
+ // Footer
97
+ try {
98
+ const cu = agent.contextUsage();
99
+ const pct = cu.pct || 0;
100
+ lines.push(chalk.dim(B.v + " ctx " + String(pct).padStart(3) + "%" + " ".repeat(W - 10) + B.v));
101
+ } catch { lines.push(chalk.dim(B.v + " ".repeat(W - 2) + B.v)); }
102
+
103
+ lines.push(chalk.dim(bar(B.bl, B.h, B.br, W - 2)));
104
+ return lines;
105
+ }
106
+
107
+ /* ── Render command palette ── */
108
+ function renderPalette(filter: string, selIdx: number, width: number): string[] {
109
+ const lines: string[] = [];
110
+ const W = Math.min(width - 4, 56);
111
+
112
+ // Agent section first
113
+ const agentMatches = AGENT_CMDS.filter(([, cmd]) => cmd.includes(filter) || filter === "/");
114
+ const actionMatches = ACTION_CMDS.filter(([cmd]) => cmd.includes(filter));
115
+
116
+ const allItems: string[] = [];
117
+ for (const [icon, cmd, desc] of agentMatches) allItems.push(`${icon} ${cmd.padEnd(16)} ${desc}`);
118
+ for (const [cmd, desc] of actionMatches) allItems.push(` ${cmd.padEnd(18)} ${desc}`);
119
+
120
+ if (allItems.length === 0 && filter.length > 1) {
121
+ // No matches — show message
122
+ lines.push(chalk.dim(bar(B.tl, B.h, B.tr, W)));
123
+ lines.push(chalk.dim(B.v + " 未找到匹配命令 (esc 关闭)".padEnd(W) + B.v));
124
+ lines.push(chalk.dim(bar(B.bl, B.h, B.br, W)));
125
+ return lines;
126
+ }
127
+
128
+ if (allItems.length === 0) return lines;
129
+
130
+ const start = Math.max(0, Math.min(selIdx - 5, allItems.length - 10));
131
+ const end = Math.min(allItems.length, start + 10);
132
+
133
+ lines.push(chalk.dim(bar(B.tl, B.h, B.tr, W - 5)) + " ".padEnd(5));
134
+
135
+ for (let i = start; i < end; i++) {
136
+ const item = allItems[i];
137
+ const isSelected = i === selIdx;
138
+ const pad = W - item.replace(/\x1b\[[0-9;]*m/g, "").length + 2; // account for ANSI codes
139
+ lines.push(isSelected
140
+ ? chalk.cyan(B.v + " ▶ " + item).padEnd(W + 10) + chalk.cyan(B.v)
141
+ : chalk.dim(B.v + " " + item).padEnd(W + 10) + chalk.dim(B.v));
142
+ }
143
+
144
+ lines.push(chalk.dim(bar(B.bl, B.h, B.br, W - 5)) + " ".padEnd(5));
145
+ return lines;
146
+ }
147
+
148
+ /* ── Render message ── */
149
+ function renderMessage(role: string, text: string, width: number): string[] {
150
+ const lines: string[] = [];
151
+ const maxW = Math.min(width - 24, 60);
152
+ const prefix = role === "user" ? " " : " ";
153
+ const suffix = role === "user" ? "" : "";
154
+
155
+ for (const para of text.split("\n")) {
156
+ let remaining = para;
157
+ while (remaining.length > 0) {
158
+ const cut = remaining.length > maxW ? remaining.lastIndexOf(" ", maxW) : remaining.length;
159
+ const idx = cut > 0 ? cut : maxW;
160
+ const line = remaining.slice(0, idx).trimEnd();
161
+ if (role === "user") {
162
+ lines.push(chalk.dim(" ".repeat(Math.max(0, width - line.length - 4))) + chalk.cyan(line) + " ");
163
+ } else if (role === "assistant") {
164
+ lines.push(prefix + line + suffix);
165
+ } else {
166
+ lines.push(chalk.dim(" " + line));
167
+ }
168
+ remaining = remaining.slice(idx).trimStart();
169
+ }
170
+ }
171
+ return lines;
172
+ }
173
+
174
+ /* ── Read input with command palette ── */
175
+ export function readInput(stdin: NodeJS.ReadStream, stdout: NodeJS.WriteStream, ctx: TUIContext): Promise<string> {
176
+ return new Promise(resolve => {
177
+ let buf = "";
178
+ let cursor = 0;
179
+ let palette = false;
180
+ let selIdx = 0;
181
+
182
+ function render() {
183
+ // Clear screen and render full TUI
184
+ readline.cursorTo(stdout, 0, 0);
185
+ readline.clearScreenDown(stdout);
186
+
187
+ const w = stdout.columns || 80;
188
+ const h = stdout.rows || 24;
189
+ const sidebarW = 16;
190
+
191
+ // Header
192
+ stdout.write(chalk.bgBlack.cyan(" 天空织机 Skyloom v1.10 ".padEnd(w - 20, " ")) + chalk.bgBlack.dim(" deepseek".padEnd(10)) + chalk.bgBlack("\n"));
193
+ stdout.write(chalk.dim(bar("", B.h, "", w)) + "\n");
194
+
195
+ // Sidebar
196
+ const sidebar = renderSidebar(ctx.agent, ctx.agents, h - 5);
197
+ for (let i = 0; i < sidebar.length && i < h - 5; i++) {
198
+ stdout.write(sidebar[i] + "\n");
199
+ }
200
+
201
+ // Command palette (overlaid)
202
+ if (palette) {
203
+ const paletteLines = renderPalette(buf, selIdx, w);
204
+ // Move cursor up to position palette below header
205
+ const paletteY = 2;
206
+ for (let i = 0; i < paletteLines.length; i++) {
207
+ stdout.write(`\x1b[${paletteY + i};${sidebarW}H`); // position cursor
208
+ stdout.write(paletteLines[i]);
209
+ }
210
+ }
211
+
212
+ // Input bar at bottom
213
+ readline.cursorTo(stdout, sidebarW, h - 1);
214
+ stdout.write(chalk.dim(B.l + B.h.repeat(w - sidebarW - 2) + B.r));
215
+ readline.cursorTo(stdout, sidebarW, h);
216
+ stdout.write(chalk.cyan(" > ") + buf.slice(0, cursor) + chalk.inverse(buf[cursor] || " ") + buf.slice(cursor + 1));
217
+ }
218
+
219
+ if (!stdin.isTTY) {
220
+ const rl = readline.createInterface({ input: stdin });
221
+ rl.on("line", (line) => { rl.close(); resolve(line.trim()); });
222
+ return;
223
+ }
224
+
225
+ stdin.setRawMode(true);
226
+ stdin.resume();
227
+ render();
228
+
229
+ let escBuf = "";
230
+ stdin.on("data", (data: Buffer) => {
231
+ const str = data.toString();
232
+ escBuf += str;
233
+
234
+ if (escBuf.startsWith("\x1b[") && escBuf.length >= 3) {
235
+ const code = escBuf[2]; escBuf = "";
236
+ if (code === "A") { if (palette) selIdx = Math.max(0, selIdx - 1); render(); return; }
237
+ if (code === "B") { if (palette) { const all = [...AGENT_CMDS.map(c => c[1]), ...ACTION_CMDS.map(c => c[0])]; selIdx = Math.min(all.filter(a => a.includes(buf)).length - 1, selIdx + 1); } render(); return; }
238
+ if (code === "C") { if (cursor < buf.length) cursor++; render(); return; }
239
+ if (code === "D") { if (cursor > 0) cursor--; render(); return; }
240
+ }
241
+
242
+ for (const ch of escBuf) {
243
+ escBuf = "";
244
+ if (ch === "\x1b") { palette = false; render(); return; }
245
+ if (ch === "\r" || ch === "\n") {
246
+ if (palette) {
247
+ const all = [...AGENT_CMDS.map(c => c[1]), ...ACTION_CMDS.map(c => c[0])];
248
+ const filtered = all.filter(a => a.includes(buf));
249
+ if (filtered[selIdx]) buf = filtered[selIdx];
250
+ palette = false;
251
+ render();
252
+ stdin.setRawMode(false); stdin.pause(); resolve(buf.trim()); return;
253
+ }
254
+ stdin.setRawMode(false); stdin.pause(); resolve(buf.trim()); return;
255
+ }
256
+ if (ch === "\t") { /* ignore */ return; }
257
+ if (ch === "\x7f" || ch === "\b") { if (cursor > 0) { buf = buf.slice(0, cursor - 1) + buf.slice(cursor); cursor--; } if (!buf) palette = false; render(); return; }
258
+ if (ch === "\x03") { stdin.setRawMode(false); stdin.pause(); resolve("/quit"); return; }
259
+ if (ch >= " ") {
260
+ buf = buf.slice(0, cursor) + ch + buf.slice(cursor); cursor++;
261
+ if (ch === "/") { palette = true; selIdx = 0; }
262
+ else if (palette) selIdx = 0;
263
+ render(); return;
264
+ }
265
+ }
266
+ });
267
+ });
268
+ }
package/src/core/llm.ts CHANGED
@@ -756,9 +756,23 @@ export class LLMClient {
756
756
  else { const l = model.toLowerCase(); if (l.includes("claude")) provider = "anthropic"; else if (l.includes("deepseek")) provider = "deepseek"; else if (l.includes("groq")) provider = "groq"; else if (l.includes("openrouter")) provider = "openrouter"; else if (l.includes("gemini")) provider = "gemini"; }
757
757
  const envMap = getProviderEnvMap();
758
758
  const envVar = envMap.get(provider) || (provider.toUpperCase() + "_API_KEY");
759
- const key = process.env[envVar];
760
- if (!key) throw new Error("Missing " + envVar + ". Set environment variable or configure in ~/.skyloom/config.yaml");
761
- return key;
759
+
760
+ // 1. Check environment variable first
761
+ let key = process.env[envVar];
762
+ if (key) return key;
763
+
764
+ // 2. Check config file (~/.skyloom/config.yaml)
765
+ try {
766
+ const fs = require("fs"); const path = require("path"); const yaml = require("yaml");
767
+ const cfgPath = path.join(require("os").homedir(), ".skyloom", "config.yaml");
768
+ if (fs.existsSync(cfgPath)) {
769
+ const cfg = yaml.parse(fs.readFileSync(cfgPath, "utf-8")) || {};
770
+ const keys = cfg.api_keys || {};
771
+ if (keys[provider]) return keys[provider];
772
+ }
773
+ } catch { /* ignore */ }
774
+
775
+ throw new Error("Missing " + envVar + ". Run: sky apikey set " + provider + " YOUR_KEY");
762
776
  }
763
777
 
764
778
  private getBaseUrl(model: string): string {