@kenkaiiii/ggcoder 4.10.1 → 4.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/dist/app-sidecar.d.ts +2 -0
- package/dist/app-sidecar.d.ts.map +1 -0
- package/dist/app-sidecar.js +1201 -0
- package/dist/app-sidecar.js.map +1 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +5 -22
- package/dist/cli.js.map +1 -1
- package/dist/core/agent-session.d.ts +94 -0
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +313 -1
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/auth-providers.d.ts +18 -0
- package/dist/core/auth-providers.d.ts.map +1 -0
- package/dist/core/auth-providers.js +71 -0
- package/dist/core/auth-providers.js.map +1 -0
- package/dist/core/event-bus.d.ts +5 -0
- package/dist/core/event-bus.d.ts.map +1 -1
- package/dist/core/event-bus.js +1 -0
- package/dist/core/event-bus.js.map +1 -1
- package/dist/core/project-discovery.d.ts +41 -0
- package/dist/core/project-discovery.d.ts.map +1 -0
- package/dist/core/project-discovery.js +441 -0
- package/dist/core/project-discovery.js.map +1 -0
- package/dist/core/radio.d.ts +44 -0
- package/dist/core/radio.d.ts.map +1 -0
- package/dist/core/radio.js +250 -0
- package/dist/core/radio.js.map +1 -0
- package/dist/core/settings-manager.d.ts +1 -1
- package/dist/core/shell-path.d.ts +12 -0
- package/dist/core/shell-path.d.ts.map +1 -0
- package/dist/core/shell-path.js +166 -0
- package/dist/core/shell-path.js.map +1 -0
- package/dist/core/telegram-config.d.ts +21 -0
- package/dist/core/telegram-config.d.ts.map +1 -0
- package/dist/core/telegram-config.js +52 -0
- package/dist/core/telegram-config.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/modes/serve-mode.d.ts +24 -0
- package/dist/modes/serve-mode.d.ts.map +1 -1
- package/dist/modes/serve-mode.js +54 -19
- package/dist/modes/serve-mode.js.map +1 -1
- package/dist/tools/tasks.d.ts +1 -1
- package/dist/ui/hooks/useTranscriptHistory.d.ts +2 -0
- package/dist/ui/hooks/useTranscriptHistory.d.ts.map +1 -1
- package/dist/ui/hooks/useTranscriptHistory.js +3 -2
- package/dist/ui/hooks/useTranscriptHistory.js.map +1 -1
- package/dist/ui/render.d.ts.map +1 -1
- package/dist/ui/render.js +60 -8
- package/dist/ui/render.js.map +1 -1
- package/dist/ui/terminal-history-repaint.test.d.ts +2 -0
- package/dist/ui/terminal-history-repaint.test.d.ts.map +1 -0
- package/dist/ui/terminal-history-repaint.test.js +73 -0
- package/dist/ui/terminal-history-repaint.test.js.map +1 -0
- package/dist/ui/terminal-history.d.ts +1 -0
- package/dist/ui/terminal-history.d.ts.map +1 -1
- package/dist/ui/terminal-history.js +31 -2
- package/dist/ui/terminal-history.js.map +1 -1
- package/dist/utils/git.d.ts +6 -0
- package/dist/utils/git.d.ts.map +1 -1
- package/dist/utils/git.js +12 -0
- package/dist/utils/git.js.map +1 -1
- package/package.json +4 -4
|
@@ -0,0 +1,1201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* gg-app sidecar — bridges the full ggcoder AgentSession to the Tauri webview
|
|
3
|
+
* over plain HTTP + Server-Sent Events (zero browser-side dependencies).
|
|
4
|
+
*
|
|
5
|
+
* Transport:
|
|
6
|
+
* GET /state → { provider, model, cwd, ready }
|
|
7
|
+
* GET /events → text/event-stream of forwarded agent + session events
|
|
8
|
+
* POST /prompt → { text } ; runs AgentSession.prompt(text)
|
|
9
|
+
* POST /cancel → aborts the in-flight run
|
|
10
|
+
*
|
|
11
|
+
* The agent spine (gg-ai → gg-agent → gg-core) and every tool are reused
|
|
12
|
+
* unchanged via AgentSession — this file is only a network seam.
|
|
13
|
+
*/
|
|
14
|
+
import http from "node:http";
|
|
15
|
+
import fs from "node:fs/promises";
|
|
16
|
+
import os from "node:os";
|
|
17
|
+
import path from "node:path";
|
|
18
|
+
import { AgentSession } from "./core/agent-session.js";
|
|
19
|
+
import { AuthStorage } from "./core/auth-storage.js";
|
|
20
|
+
import { MOONSHOT_OAUTH_KEY } from "@kenkaiiii/gg-core";
|
|
21
|
+
import { loginAnthropic } from "./core/oauth/anthropic.js";
|
|
22
|
+
import { loginOpenAI } from "./core/oauth/openai.js";
|
|
23
|
+
import { loginGemini } from "./core/oauth/gemini.js";
|
|
24
|
+
import { loginKimi } from "./core/oauth/kimi.js";
|
|
25
|
+
import { AUTH_PROVIDERS } from "./core/auth-providers.js";
|
|
26
|
+
import { ensureAppDirs, loadSavedSettings } from "./config.js";
|
|
27
|
+
import { SettingsManager } from "./core/settings-manager.js";
|
|
28
|
+
import { getDefaultModel, getModel, getMaxThinkingLevel, getContextWindow, MODELS, } from "./core/model-registry.js";
|
|
29
|
+
import { getGitBranch, isGitRepo } from "./utils/git.js";
|
|
30
|
+
import { getNextThinkingLevel, getSupportedThinkingLevels, isThinkingLevelSupported, } from "./core/thinking-level.js";
|
|
31
|
+
import { PROMPT_COMMANDS } from "./core/prompt-commands.js";
|
|
32
|
+
import { loadCustomCommands } from "./core/custom-commands.js";
|
|
33
|
+
import { discoverProjects, listRecentSessions } from "./core/project-discovery.js";
|
|
34
|
+
import { loadTasksSync, saveTasksSync, getNextPendingTask, markTaskInProgress, } from "./core/tasks-store.js";
|
|
35
|
+
import { initLogger, log } from "./core/logger.js";
|
|
36
|
+
import { RADIO_STATIONS, getCurrentStation, playRadio, stopRadio } from "./core/radio.js";
|
|
37
|
+
import { enrichProcessPath } from "./core/shell-path.js";
|
|
38
|
+
import { startServeMode } from "./modes/serve-mode.js";
|
|
39
|
+
import { loadTelegramConfig, saveTelegramConfig, verifyBotToken } from "./core/telegram-config.js";
|
|
40
|
+
const ALL_PROVIDERS = [
|
|
41
|
+
"anthropic",
|
|
42
|
+
"xiaomi",
|
|
43
|
+
"openai",
|
|
44
|
+
"gemini",
|
|
45
|
+
"glm",
|
|
46
|
+
"moonshot",
|
|
47
|
+
"minimax",
|
|
48
|
+
"deepseek",
|
|
49
|
+
"openrouter",
|
|
50
|
+
];
|
|
51
|
+
function appSettingsFile() {
|
|
52
|
+
return path.join(os.homedir(), ".gg", "gg-app.json");
|
|
53
|
+
}
|
|
54
|
+
function defaultProjectsRoot() {
|
|
55
|
+
return path.join(os.homedir(), "gg-projects");
|
|
56
|
+
}
|
|
57
|
+
async function loadAppSettings() {
|
|
58
|
+
try {
|
|
59
|
+
const raw = JSON.parse(await fs.readFile(appSettingsFile(), "utf-8"));
|
|
60
|
+
return {
|
|
61
|
+
projectsRoot: typeof raw.projectsRoot === "string" && raw.projectsRoot.trim()
|
|
62
|
+
? raw.projectsRoot
|
|
63
|
+
: defaultProjectsRoot(),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
return { projectsRoot: defaultProjectsRoot() };
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
async function saveAppSettings(settings) {
|
|
71
|
+
await fs.mkdir(path.dirname(appSettingsFile()), { recursive: true });
|
|
72
|
+
await fs.writeFile(appSettingsFile(), JSON.stringify(settings, null, 2), "utf-8");
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Persist the active model selection to ~/.gg/settings.json so it survives app
|
|
76
|
+
* restarts. Mirrors the CLI's handleModelSelect persistence (App.tsx).
|
|
77
|
+
*/
|
|
78
|
+
async function persistModelSelection(settingsFile, provider, model) {
|
|
79
|
+
try {
|
|
80
|
+
const sm = new SettingsManager(settingsFile);
|
|
81
|
+
await sm.load();
|
|
82
|
+
await sm.set("defaultProvider", provider);
|
|
83
|
+
await sm.set("defaultModel", model);
|
|
84
|
+
}
|
|
85
|
+
catch (err) {
|
|
86
|
+
log("WARN", "app-sidecar", "failed to persist model selection", { err: String(err) });
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Persist the thinking level to ~/.gg/settings.json so it survives app restarts.
|
|
91
|
+
* Mirrors the CLI's handleToggleThinking persistence (App.tsx).
|
|
92
|
+
*/
|
|
93
|
+
async function persistThinkingLevel(settingsFile, level) {
|
|
94
|
+
try {
|
|
95
|
+
const sm = new SettingsManager(settingsFile);
|
|
96
|
+
await sm.load();
|
|
97
|
+
await sm.set("thinkingEnabled", !!level);
|
|
98
|
+
if (level)
|
|
99
|
+
await sm.set("thinkingLevel", level);
|
|
100
|
+
}
|
|
101
|
+
catch (err) {
|
|
102
|
+
log("WARN", "app-sidecar", "failed to persist thinking level", { err: String(err) });
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/** Validate a project folder name: lowercase letters, digits, dashes only. */
|
|
106
|
+
function isValidProjectName(name) {
|
|
107
|
+
return /^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(name);
|
|
108
|
+
}
|
|
109
|
+
async function prepareAttachments(cwd, attachments) {
|
|
110
|
+
const dir = path.join(cwd, ".gg", "uploads");
|
|
111
|
+
await fs.mkdir(dir, { recursive: true }).catch(() => { });
|
|
112
|
+
const out = [];
|
|
113
|
+
for (const a of attachments) {
|
|
114
|
+
// Sanitize the filename and prefix with a short timestamp to avoid clobber.
|
|
115
|
+
const safe = a.name.replace(/[^\w.-]+/g, "_").slice(-80) || "file";
|
|
116
|
+
const fileName = `${Date.now().toString(36)}-${safe}`;
|
|
117
|
+
const filePath = path.join(dir, fileName);
|
|
118
|
+
try {
|
|
119
|
+
await fs.writeFile(filePath, Buffer.from(a.data, "base64"));
|
|
120
|
+
out.push({ ...a, path: filePath });
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
out.push({ ...a });
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return out;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Detect whether a restored user message is actually an injected self-correction
|
|
130
|
+
* hook prompt, by its distinctive opening phrase. Returns the hook kind so the
|
|
131
|
+
* webview can render the short notice line instead of the full prompt body.
|
|
132
|
+
*/
|
|
133
|
+
function detectHookKind(text) {
|
|
134
|
+
const t = text.trimStart();
|
|
135
|
+
if (t.startsWith("Ideal? Review the actual work"))
|
|
136
|
+
return "ideal";
|
|
137
|
+
if (t.startsWith("Stuck? You've repeated essentially"))
|
|
138
|
+
return "loop_break";
|
|
139
|
+
if (t.startsWith("Re-ground. The conversation was just compacted"))
|
|
140
|
+
return "regrounding";
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
// Separator AgentSession.prompt() inserts between a command's prompt body and
|
|
144
|
+
// the user's trailing args. Must stay in sync with the expansion there.
|
|
145
|
+
const COMMAND_ARGS_SEP = "\n\n## User Instructions\n\n";
|
|
146
|
+
/**
|
|
147
|
+
* Reverse a prompt-template command's expansion. When a `/name` command runs,
|
|
148
|
+
* the agent persists the FULL expanded prompt body as the user message — so on
|
|
149
|
+
* resume the raw body would render instead of the short `/name` chip the user
|
|
150
|
+
* saw live. Given the candidate commands (built-in + custom) and a restored
|
|
151
|
+
* message body, recover the original `/name [args]` invocation. Returns null
|
|
152
|
+
* when the text isn't a known command body (an ordinary user message).
|
|
153
|
+
*/
|
|
154
|
+
function detectPromptCommand(text, candidates) {
|
|
155
|
+
for (const c of candidates) {
|
|
156
|
+
if (!c.prompt)
|
|
157
|
+
continue;
|
|
158
|
+
if (text === c.prompt)
|
|
159
|
+
return `/${c.name}`;
|
|
160
|
+
if (text.startsWith(c.prompt + COMMAND_ARGS_SEP)) {
|
|
161
|
+
const args = text.slice(c.prompt.length + COMMAND_ARGS_SEP.length).trim();
|
|
162
|
+
return args ? `/${c.name} ${args}` : `/${c.name}`;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Pick a provider/model the user is actually logged into, preferring the saved
|
|
169
|
+
* defaults. Mirrors the CLI's resolveActiveProvider without exporting internals.
|
|
170
|
+
*/
|
|
171
|
+
async function resolveStart(auth, preferred, savedModel) {
|
|
172
|
+
const loggedIn = [];
|
|
173
|
+
for (const p of ALL_PROVIDERS) {
|
|
174
|
+
if (await auth.hasProviderAuth(p))
|
|
175
|
+
loggedIn.push(p);
|
|
176
|
+
}
|
|
177
|
+
if (loggedIn.length === 0) {
|
|
178
|
+
throw new Error('Not logged in to any provider. Run "ggcoder login" to authenticate.');
|
|
179
|
+
}
|
|
180
|
+
if (loggedIn.includes(preferred)) {
|
|
181
|
+
const saved = savedModel ? getModel(savedModel) : undefined;
|
|
182
|
+
return {
|
|
183
|
+
provider: preferred,
|
|
184
|
+
model: saved?.provider === preferred ? saved.id : getDefaultModel(preferred).id,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
const provider = loggedIn[0];
|
|
188
|
+
return { provider, model: getDefaultModel(provider).id };
|
|
189
|
+
}
|
|
190
|
+
async function main() {
|
|
191
|
+
const cwd = process.env.GG_APP_CWD ?? process.cwd();
|
|
192
|
+
// Default to an ephemeral port (0) so concurrent/orphaned instances never
|
|
193
|
+
// collide on a fixed port. The actual port is reported via the
|
|
194
|
+
// GG_APP_LISTENING handshake and consumed by the shell.
|
|
195
|
+
const port = Number(process.env.GG_APP_PORT ?? 0);
|
|
196
|
+
const host = "127.0.0.1";
|
|
197
|
+
const paths = await ensureAppDirs();
|
|
198
|
+
// Own log file so the app sidecar never clobbers the interactive CLI's
|
|
199
|
+
// ~/.gg/debug.log (initLogger truncates on each start).
|
|
200
|
+
const sidecarLog = path.join(paths.agentDir, "gg-app-sidecar.log");
|
|
201
|
+
initLogger(sidecarLog);
|
|
202
|
+
// The packaged desktop app launches from Finder/Dock with a minimal PATH that
|
|
203
|
+
// omits Homebrew/Cargo/version-manager dirs, so the agent can't find node,
|
|
204
|
+
// git, python, rg, etc. Enrich process.env.PATH from the login shell once,
|
|
205
|
+
// before anything spawns (bash tool, background tasks, LSP, git helpers all
|
|
206
|
+
// inherit it). Best-effort — never blocks startup beyond its internal cap.
|
|
207
|
+
await enrichProcessPath();
|
|
208
|
+
const auth = new AuthStorage(paths.authFile);
|
|
209
|
+
await auth.load();
|
|
210
|
+
const saved = loadSavedSettings(paths.settingsFile);
|
|
211
|
+
const preferred = saved.provider ?? "anthropic";
|
|
212
|
+
const { provider, model } = await resolveStart(auth, preferred, saved.model);
|
|
213
|
+
const thinkingLevel = saved.thinkingEnabled
|
|
214
|
+
? (saved.thinkingLevel ?? getMaxThinkingLevel(model))
|
|
215
|
+
: undefined;
|
|
216
|
+
// ── SSE fan-out (declared before the session so plan callbacks can use it) ─
|
|
217
|
+
const clients = new Set();
|
|
218
|
+
let clientSeq = 0;
|
|
219
|
+
function broadcast(type, data) {
|
|
220
|
+
const frame = `data: ${JSON.stringify({ type, data })}\n\n`;
|
|
221
|
+
for (const c of clients)
|
|
222
|
+
c.res.write(frame);
|
|
223
|
+
}
|
|
224
|
+
// When the shell respawns this sidecar for a chosen project, it passes the
|
|
225
|
+
// session file path to resume; empty/unset starts a fresh session.
|
|
226
|
+
const resumeSessionPath = process.env.GG_APP_SESSION_ID || undefined;
|
|
227
|
+
let abort = new AbortController();
|
|
228
|
+
const session = new AgentSession({
|
|
229
|
+
provider,
|
|
230
|
+
model,
|
|
231
|
+
cwd,
|
|
232
|
+
thinkingLevel,
|
|
233
|
+
sessionId: resumeSessionPath,
|
|
234
|
+
signal: abort.signal,
|
|
235
|
+
// Plan mode: the agent's enter_plan/exit_plan tools drive these. We flip
|
|
236
|
+
// session plan state (rebuilds the system prompt + enforces read-only
|
|
237
|
+
// tools) and surface the transition to the webview.
|
|
238
|
+
onEnterPlan: async (reason) => {
|
|
239
|
+
await session.setPlanMode(true);
|
|
240
|
+
broadcast("plan_enter", { reason: reason ?? "" });
|
|
241
|
+
},
|
|
242
|
+
onExitPlan: async (planPath) => {
|
|
243
|
+
await session.setPlanMode(false);
|
|
244
|
+
// Surface the plan's path + markdown so the webview can show the review
|
|
245
|
+
// modal (Accept / Feedback / Reject). Best-effort content read.
|
|
246
|
+
let content;
|
|
247
|
+
try {
|
|
248
|
+
content = await fs.readFile(planPath, "utf-8");
|
|
249
|
+
}
|
|
250
|
+
catch {
|
|
251
|
+
content = "";
|
|
252
|
+
}
|
|
253
|
+
broadcast("plan_exit", { planPath, content });
|
|
254
|
+
return "Plan submitted for user review. Wait for the user to approve, reject, or dismiss it before implementing.";
|
|
255
|
+
},
|
|
256
|
+
});
|
|
257
|
+
await session.initialize();
|
|
258
|
+
log("INFO", "app-sidecar", "session ready", { provider, model, cwd });
|
|
259
|
+
// Footer extras (context window, git branch, background tasks). The git
|
|
260
|
+
// branch is resolved once at startup and refreshed lazily; the context
|
|
261
|
+
// window follows the active model.
|
|
262
|
+
let gitBranch = await getGitBranch(cwd).catch(() => null);
|
|
263
|
+
let gitIsRepo = await isGitRepo(cwd).catch(() => false);
|
|
264
|
+
function currentContextWindow() {
|
|
265
|
+
const st = session.getState();
|
|
266
|
+
return getContextWindow(st.model, { provider: st.provider });
|
|
267
|
+
}
|
|
268
|
+
// Shared shape merged into /state + the SSE `ready` frame so the footer can
|
|
269
|
+
// render context %, branch, and tasks immediately on connect.
|
|
270
|
+
function footerExtras() {
|
|
271
|
+
return {
|
|
272
|
+
contextWindow: currentContextWindow(),
|
|
273
|
+
gitBranch,
|
|
274
|
+
isGitRepo: gitIsRepo,
|
|
275
|
+
tasks: session.listBackgroundProcesses(),
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
// Forward every relevant bus event to the webview.
|
|
279
|
+
session.eventBus.on("text_delta", (d) => broadcast("text_delta", d));
|
|
280
|
+
session.eventBus.on("thinking_delta", (d) => broadcast("thinking_delta", d));
|
|
281
|
+
session.eventBus.on("tool_call_start", (d) => broadcast("tool_call_start", d));
|
|
282
|
+
session.eventBus.on("tool_call_update", (d) => broadcast("tool_call_update", d));
|
|
283
|
+
session.eventBus.on("tool_call_end", (d) => broadcast("tool_call_end", d));
|
|
284
|
+
session.eventBus.on("turn_end", (d) => broadcast("turn_end", d));
|
|
285
|
+
session.eventBus.on("agent_done", (d) => broadcast("agent_done", d));
|
|
286
|
+
session.eventBus.on("error", (d) => broadcast("error", { message: d.error instanceof Error ? d.error.message : String(d.error) }));
|
|
287
|
+
session.eventBus.on("model_change", (d) => broadcast("model_change", d));
|
|
288
|
+
session.eventBus.on("hook", (d) => broadcast("hook", d));
|
|
289
|
+
session.eventBus.on("compaction_start", (d) => broadcast("compaction_start", d));
|
|
290
|
+
session.eventBus.on("compaction_end", (d) => broadcast("compaction_end", d));
|
|
291
|
+
let running = false;
|
|
292
|
+
let titleGenerated = false;
|
|
293
|
+
// ── Telegram serve (remote control via Telegram) ───────────
|
|
294
|
+
// A single embedded serve session lives in this sidecar process. Only the main
|
|
295
|
+
// window's home screen exposes the controls, so there's one bot per app.
|
|
296
|
+
let serveController = null;
|
|
297
|
+
// Resumed session: if it already has a conversation, generate its title now so
|
|
298
|
+
// the title bar shows it immediately on load (not just after the next prompt).
|
|
299
|
+
{
|
|
300
|
+
const hasHistory = session
|
|
301
|
+
.getMessages()
|
|
302
|
+
.some((m) => m.role === "user" || m.role === "assistant");
|
|
303
|
+
if (hasHistory) {
|
|
304
|
+
titleGenerated = true;
|
|
305
|
+
void session.generateTitle().then((title) => {
|
|
306
|
+
if (title)
|
|
307
|
+
broadcast("session_title", { title });
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
// Core run lifecycle shared by /prompt and the task runner: flips `running`,
|
|
312
|
+
// brackets the run with run_start/run_end, refreshes the footer extras, and
|
|
313
|
+
// generates the session title once. `label` is the text shown live with the
|
|
314
|
+
// run_start frame.
|
|
315
|
+
async function runAgent(label, run) {
|
|
316
|
+
running = true;
|
|
317
|
+
broadcast("run_start", { text: label });
|
|
318
|
+
try {
|
|
319
|
+
await run();
|
|
320
|
+
}
|
|
321
|
+
catch (err) {
|
|
322
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
323
|
+
broadcast("error", { message });
|
|
324
|
+
log("ERROR", "app-sidecar", "run failed", { message });
|
|
325
|
+
}
|
|
326
|
+
finally {
|
|
327
|
+
running = false;
|
|
328
|
+
// A run may have switched branches (git checkout) or spawned/finished
|
|
329
|
+
// background tasks — refresh the footer extras once it settles.
|
|
330
|
+
gitBranch = await getGitBranch(cwd).catch(() => gitBranch);
|
|
331
|
+
gitIsRepo = await isGitRepo(cwd).catch(() => gitIsRepo);
|
|
332
|
+
broadcast("run_end", {});
|
|
333
|
+
// Queue drains into the run as steering, so it's empty by run_end —
|
|
334
|
+
// sync the webview indicator.
|
|
335
|
+
broadcast("queued", { count: session.getQueuedCount() });
|
|
336
|
+
broadcast("extras", footerExtras());
|
|
337
|
+
// Generate a session title once, after the first run, for the title bar
|
|
338
|
+
// (best-effort, async — don't block the response).
|
|
339
|
+
if (!titleGenerated) {
|
|
340
|
+
titleGenerated = true;
|
|
341
|
+
void session.generateTitle().then((title) => {
|
|
342
|
+
if (title)
|
|
343
|
+
broadcast("session_title", { title });
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
// ── Task runner (project task list → sessions) ──────────────
|
|
349
|
+
// Mirrors the CLI's task flow: each task runs in its OWN fresh session, with a
|
|
350
|
+
// completion hint instructing the agent to mark the task done via the tasks
|
|
351
|
+
// tool. Run-all advances to the next pending task after each run finishes.
|
|
352
|
+
let taskRunAll = false;
|
|
353
|
+
async function runTaskById(taskId) {
|
|
354
|
+
const task = loadTasksSync(cwd).find((t) => t.id === taskId || t.id.startsWith(taskId));
|
|
355
|
+
if (!task)
|
|
356
|
+
return false;
|
|
357
|
+
// Fresh session per task so one task's context never bleeds into the next.
|
|
358
|
+
await session.newSession();
|
|
359
|
+
titleGenerated = false;
|
|
360
|
+
broadcast("session_reset", {});
|
|
361
|
+
markTaskInProgress(cwd, task.id);
|
|
362
|
+
broadcast("tasks_list", { tasks: loadTasksSync(cwd) });
|
|
363
|
+
broadcast("task_start", { id: task.id, title: task.title });
|
|
364
|
+
const shortId = task.id.slice(0, 8);
|
|
365
|
+
const completionHint = `\n\n---\nWhen you have fully completed this task, call the tasks tool to mark it done:\n` +
|
|
366
|
+
`tasks({ action: "done", id: "${shortId}" })`;
|
|
367
|
+
await runAgent(task.title, () => session.prompt(task.prompt + completionHint));
|
|
368
|
+
// The agent typically marks the task done via the tasks tool during the run;
|
|
369
|
+
// push the refreshed list so the webview's task modal reflects it.
|
|
370
|
+
broadcast("tasks_list", { tasks: loadTasksSync(cwd) });
|
|
371
|
+
return true;
|
|
372
|
+
}
|
|
373
|
+
async function runTasks(startId, all) {
|
|
374
|
+
taskRunAll = all;
|
|
375
|
+
let currentId = startId ?? getNextPendingTask(cwd)?.id ?? null;
|
|
376
|
+
while (currentId) {
|
|
377
|
+
const ran = await runTaskById(currentId);
|
|
378
|
+
if (!ran || !taskRunAll)
|
|
379
|
+
break;
|
|
380
|
+
const next = getNextPendingTask(cwd);
|
|
381
|
+
currentId = next ? next.id : null;
|
|
382
|
+
// Brief pause between tasks (mirrors the CLI cadence).
|
|
383
|
+
if (currentId)
|
|
384
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
385
|
+
}
|
|
386
|
+
taskRunAll = false;
|
|
387
|
+
broadcast("tasks_run_done", {});
|
|
388
|
+
}
|
|
389
|
+
// ── Provider auth (login) bridge ───────────────────────────
|
|
390
|
+
// OAuth login functions are interactive (open a URL, sometimes prompt for a
|
|
391
|
+
// pasted code). We run one at a time and surface every step over SSE so the
|
|
392
|
+
// webview can open the URL and collect a code via a modal. `pendingCode`
|
|
393
|
+
// resolves when the webview POSTs /auth/oauth/code.
|
|
394
|
+
let oauthInFlight = false;
|
|
395
|
+
let pendingCode = null;
|
|
396
|
+
function authCallbacks() {
|
|
397
|
+
return {
|
|
398
|
+
onOpenUrl: (url) => broadcast("auth_url", { url }),
|
|
399
|
+
onStatus: (message) => broadcast("auth_status", { message }),
|
|
400
|
+
onPromptCode: (message) => new Promise((resolve) => {
|
|
401
|
+
pendingCode = resolve;
|
|
402
|
+
broadcast("auth_need_code", { message });
|
|
403
|
+
}),
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
async function authStatusPayload() {
|
|
407
|
+
const providers = await Promise.all(AUTH_PROVIDERS.map(async (p) => ({
|
|
408
|
+
...p,
|
|
409
|
+
connected: await auth.hasProviderAuth(p.value),
|
|
410
|
+
})));
|
|
411
|
+
return { providers };
|
|
412
|
+
}
|
|
413
|
+
// Background tasks have no event source (the bash tool just spawns them), so
|
|
414
|
+
// poll the process manager and broadcast only when the snapshot changes. This
|
|
415
|
+
// keeps the webview footer live without a busy render loop. Adaptive cadence:
|
|
416
|
+
// tasks can only change while a run is active (the bash tool spawns them), so
|
|
417
|
+
// poll fast (1500ms) while running or while tasks exist, and back off to
|
|
418
|
+
// 5000ms when fully idle — fewer wakeups per idle window.
|
|
419
|
+
let lastTasksJson = "[]";
|
|
420
|
+
let tasksPoll;
|
|
421
|
+
let tasksPollStopped = false;
|
|
422
|
+
const scheduleTasksPoll = (delay) => {
|
|
423
|
+
if (tasksPollStopped)
|
|
424
|
+
return;
|
|
425
|
+
tasksPoll = setTimeout(() => {
|
|
426
|
+
const tasks = session.listBackgroundProcesses();
|
|
427
|
+
const next = JSON.stringify(tasks);
|
|
428
|
+
if (next !== lastTasksJson) {
|
|
429
|
+
lastTasksJson = next;
|
|
430
|
+
broadcast("tasks", { tasks });
|
|
431
|
+
}
|
|
432
|
+
const active = running || tasks.length > 0;
|
|
433
|
+
scheduleTasksPoll(active ? 1500 : 5000);
|
|
434
|
+
}, delay);
|
|
435
|
+
tasksPoll.unref?.();
|
|
436
|
+
};
|
|
437
|
+
scheduleTasksPoll(1500);
|
|
438
|
+
function readBody(req) {
|
|
439
|
+
return new Promise((resolve, reject) => {
|
|
440
|
+
const chunks = [];
|
|
441
|
+
req.on("data", (c) => chunks.push(c));
|
|
442
|
+
req.on("end", () => resolve(Buffer.concat(chunks).toString("utf-8")));
|
|
443
|
+
req.on("error", reject);
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
function json(res, status, body) {
|
|
447
|
+
const payload = JSON.stringify(body);
|
|
448
|
+
res.writeHead(status, {
|
|
449
|
+
"content-type": "application/json",
|
|
450
|
+
"access-control-allow-origin": "*",
|
|
451
|
+
});
|
|
452
|
+
res.end(payload);
|
|
453
|
+
}
|
|
454
|
+
const server = http.createServer((req, res) => {
|
|
455
|
+
const url = req.url ?? "/";
|
|
456
|
+
const method = req.method ?? "GET";
|
|
457
|
+
// CORS preflight — the webview origin differs from 127.0.0.1.
|
|
458
|
+
if (method === "OPTIONS") {
|
|
459
|
+
res.writeHead(204, {
|
|
460
|
+
"access-control-allow-origin": "*",
|
|
461
|
+
"access-control-allow-methods": "GET, POST, OPTIONS",
|
|
462
|
+
"access-control-allow-headers": "content-type",
|
|
463
|
+
});
|
|
464
|
+
res.end();
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
if (method === "GET" && url === "/state") {
|
|
468
|
+
const st = session.getState();
|
|
469
|
+
json(res, 200, {
|
|
470
|
+
...st,
|
|
471
|
+
running,
|
|
472
|
+
ready: true,
|
|
473
|
+
thinkingLevel: session.getThinkingLevel() ?? null,
|
|
474
|
+
supportedThinkingLevels: getSupportedThinkingLevels(st.provider, st.model),
|
|
475
|
+
...footerExtras(),
|
|
476
|
+
});
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
if (method === "GET" && url === "/events") {
|
|
480
|
+
res.writeHead(200, {
|
|
481
|
+
"content-type": "text/event-stream",
|
|
482
|
+
"cache-control": "no-cache",
|
|
483
|
+
connection: "keep-alive",
|
|
484
|
+
"access-control-allow-origin": "*",
|
|
485
|
+
});
|
|
486
|
+
res.write(`retry: 1000\n\n`);
|
|
487
|
+
const client = { id: ++clientSeq, res };
|
|
488
|
+
clients.add(client);
|
|
489
|
+
const st = session.getState();
|
|
490
|
+
res.write(`data: ${JSON.stringify({
|
|
491
|
+
type: "ready",
|
|
492
|
+
data: {
|
|
493
|
+
...st,
|
|
494
|
+
running,
|
|
495
|
+
thinkingLevel: session.getThinkingLevel() ?? null,
|
|
496
|
+
supportedThinkingLevels: getSupportedThinkingLevels(st.provider, st.model),
|
|
497
|
+
...footerExtras(),
|
|
498
|
+
},
|
|
499
|
+
})}\n\n`);
|
|
500
|
+
const keepAlive = setInterval(() => res.write(`: ping\n\n`), 15000);
|
|
501
|
+
req.on("close", () => {
|
|
502
|
+
clearInterval(keepAlive);
|
|
503
|
+
clients.delete(client);
|
|
504
|
+
});
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
if (method === "GET" && url === "/settings") {
|
|
508
|
+
// `configured` is true only when the user explicitly saved a projects root
|
|
509
|
+
// (the gg-app.json file exists with a value) — not when we fall back to the
|
|
510
|
+
// default. The home screen gates "Your Projects" on this.
|
|
511
|
+
void (async () => {
|
|
512
|
+
const s = await loadAppSettings();
|
|
513
|
+
let configured;
|
|
514
|
+
try {
|
|
515
|
+
const raw = JSON.parse(await fs.readFile(appSettingsFile(), "utf-8"));
|
|
516
|
+
configured = typeof raw.projectsRoot === "string" && raw.projectsRoot.trim().length > 0;
|
|
517
|
+
}
|
|
518
|
+
catch {
|
|
519
|
+
configured = false;
|
|
520
|
+
}
|
|
521
|
+
json(res, 200, { ...s, configured });
|
|
522
|
+
})();
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
if (method === "POST" && url === "/settings") {
|
|
526
|
+
void readBody(req).then(async (raw) => {
|
|
527
|
+
let projectsRoot;
|
|
528
|
+
try {
|
|
529
|
+
projectsRoot = JSON.parse(raw).projectsRoot ?? "";
|
|
530
|
+
}
|
|
531
|
+
catch {
|
|
532
|
+
json(res, 400, { error: "invalid JSON body" });
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
if (!projectsRoot.trim()) {
|
|
536
|
+
json(res, 400, { error: "projectsRoot is required" });
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
await saveAppSettings({ projectsRoot });
|
|
540
|
+
json(res, 200, { projectsRoot });
|
|
541
|
+
});
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
if (method === "POST" && url === "/create-project") {
|
|
545
|
+
void readBody(req).then(async (raw) => {
|
|
546
|
+
let name;
|
|
547
|
+
try {
|
|
548
|
+
name = JSON.parse(raw).name ?? "";
|
|
549
|
+
}
|
|
550
|
+
catch {
|
|
551
|
+
json(res, 400, { error: "invalid JSON body" });
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
name = name.trim();
|
|
555
|
+
if (!isValidProjectName(name)) {
|
|
556
|
+
json(res, 400, {
|
|
557
|
+
error: "Project name must be lowercase letters, digits, and dashes (e.g. my-project).",
|
|
558
|
+
});
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
const { projectsRoot } = await loadAppSettings();
|
|
562
|
+
const dir = path.join(projectsRoot, name);
|
|
563
|
+
try {
|
|
564
|
+
// Refuse to clobber an existing directory.
|
|
565
|
+
const exists = await fs
|
|
566
|
+
.stat(dir)
|
|
567
|
+
.then(() => true)
|
|
568
|
+
.catch(() => false);
|
|
569
|
+
if (exists) {
|
|
570
|
+
json(res, 409, { error: `A folder named "${name}" already exists.` });
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
await fs.mkdir(dir, { recursive: true });
|
|
574
|
+
json(res, 200, { path: dir });
|
|
575
|
+
}
|
|
576
|
+
catch (err) {
|
|
577
|
+
json(res, 500, { error: err instanceof Error ? err.message : String(err) });
|
|
578
|
+
}
|
|
579
|
+
});
|
|
580
|
+
return;
|
|
581
|
+
}
|
|
582
|
+
if (method === "GET" && url === "/projects") {
|
|
583
|
+
// Scan ggcoder + Claude Code + Codex session stores for known projects.
|
|
584
|
+
void discoverProjects()
|
|
585
|
+
.then((projects) => json(res, 200, { projects }))
|
|
586
|
+
.catch((err) => {
|
|
587
|
+
log("ERROR", "app-sidecar", "discoverProjects failed", {
|
|
588
|
+
message: err instanceof Error ? err.message : String(err),
|
|
589
|
+
});
|
|
590
|
+
json(res, 200, { projects: [] });
|
|
591
|
+
});
|
|
592
|
+
return;
|
|
593
|
+
}
|
|
594
|
+
if (method === "GET" && url.startsWith("/sessions")) {
|
|
595
|
+
const target = new URL(url, `http://${host}`).searchParams.get("cwd");
|
|
596
|
+
if (!target) {
|
|
597
|
+
json(res, 400, { error: "missing cwd query param" });
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
void listRecentSessions(target, 5)
|
|
601
|
+
.then((sessions) => json(res, 200, { sessions }))
|
|
602
|
+
.catch(() => json(res, 200, { sessions: [] }));
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
if (method === "GET" && url === "/history") {
|
|
606
|
+
// Flatten the resumed conversation into the webview's transcript shape:
|
|
607
|
+
// user + assistant TEXT only (tools live in the live panel, never the
|
|
608
|
+
// transcript; system + tool-result messages are omitted). Self-correction
|
|
609
|
+
// hook prompts (injected as user messages) are tagged with their `hook`
|
|
610
|
+
// kind so the webview renders the short "Hook engaged" line, not the raw
|
|
611
|
+
// prompt body — matching how they appear live.
|
|
612
|
+
//
|
|
613
|
+
// Prompt-template commands persist their FULL expanded body as the user
|
|
614
|
+
// message, so on resume we reverse the expansion (built-in + custom
|
|
615
|
+
// candidates) back to the short `/name [args]` chip the user saw live.
|
|
616
|
+
void (async () => {
|
|
617
|
+
const commandCandidates = [...PROMPT_COMMANDS, ...(await loadCustomCommands(cwd))];
|
|
618
|
+
const history = session
|
|
619
|
+
.getMessages()
|
|
620
|
+
.filter((m) => m.role === "user" || m.role === "assistant")
|
|
621
|
+
.map((m) => {
|
|
622
|
+
// `m.content` is a union of differently-typed arrays (user vs
|
|
623
|
+
// assistant parts), so a type-predicate filter won't narrow cleanly.
|
|
624
|
+
// A structural `"text" in c` check extracts text from any text-bearing
|
|
625
|
+
// part regardless of the surrounding union.
|
|
626
|
+
const text = typeof m.content === "string"
|
|
627
|
+
? m.content
|
|
628
|
+
: m.content
|
|
629
|
+
.map((c) => c.type === "text" && "text" in c && typeof c.text === "string" ? c.text : "")
|
|
630
|
+
.join("");
|
|
631
|
+
// Reconstruct image attachments as data URLs so they re-render on
|
|
632
|
+
// resume — the webview only ever saw the live SSE stream, and the
|
|
633
|
+
// persisted message holds the base64 bytes. Without this, attached
|
|
634
|
+
// images vanish when returning to a session.
|
|
635
|
+
const images = typeof m.content === "string"
|
|
636
|
+
? []
|
|
637
|
+
: m.content.flatMap((c) => c.type === "image" ? [`data:${c.mediaType};base64,${c.data}`] : []);
|
|
638
|
+
const hook = m.role === "user" ? detectHookKind(text) : null;
|
|
639
|
+
// Recover a `/name [args]` command invocation from its expanded body
|
|
640
|
+
// (skip messages already claimed as hooks).
|
|
641
|
+
const command = m.role === "user" && !hook ? detectPromptCommand(text, commandCandidates) : null;
|
|
642
|
+
return {
|
|
643
|
+
role: m.role,
|
|
644
|
+
text: command ?? text,
|
|
645
|
+
images,
|
|
646
|
+
hook,
|
|
647
|
+
command: command !== null,
|
|
648
|
+
};
|
|
649
|
+
})
|
|
650
|
+
// Keep messages with text OR images — an image-only user turn has empty
|
|
651
|
+
// text but must still appear.
|
|
652
|
+
.filter((m) => m.text.trim().length > 0 || m.images.length > 0);
|
|
653
|
+
json(res, 200, { history });
|
|
654
|
+
})();
|
|
655
|
+
return;
|
|
656
|
+
}
|
|
657
|
+
if (method === "GET" && url === "/commands") {
|
|
658
|
+
// Workflow commands with agent functionality: built-in prompt templates +
|
|
659
|
+
// the user's own `.gg/commands/*.md`. UI commands (model/quit/etc.) are
|
|
660
|
+
// handled webview-side and intentionally excluded.
|
|
661
|
+
void (async () => {
|
|
662
|
+
const builtins = PROMPT_COMMANDS.map((c) => ({
|
|
663
|
+
name: c.name,
|
|
664
|
+
aliases: c.aliases,
|
|
665
|
+
description: c.description,
|
|
666
|
+
source: "built-in",
|
|
667
|
+
}));
|
|
668
|
+
const custom = (await loadCustomCommands(cwd))
|
|
669
|
+
// A custom command can't shadow a built-in name.
|
|
670
|
+
.filter((c) => !PROMPT_COMMANDS.some((b) => b.name === c.name))
|
|
671
|
+
.map((c) => ({
|
|
672
|
+
name: c.name,
|
|
673
|
+
aliases: [],
|
|
674
|
+
description: c.description,
|
|
675
|
+
source: "custom",
|
|
676
|
+
}));
|
|
677
|
+
json(res, 200, { commands: [...builtins, ...custom] });
|
|
678
|
+
})();
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
if (method === "POST" && url === "/prompt") {
|
|
682
|
+
void readBody(req).then(async (raw) => {
|
|
683
|
+
let text;
|
|
684
|
+
let attachments;
|
|
685
|
+
try {
|
|
686
|
+
const body = JSON.parse(raw);
|
|
687
|
+
text = body.text ?? "";
|
|
688
|
+
attachments = Array.isArray(body.attachments) ? body.attachments : [];
|
|
689
|
+
}
|
|
690
|
+
catch {
|
|
691
|
+
json(res, 400, { error: "invalid JSON body" });
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
if (!text.trim() && attachments.length === 0) {
|
|
695
|
+
json(res, 400, { error: "empty prompt" });
|
|
696
|
+
return;
|
|
697
|
+
}
|
|
698
|
+
if (running) {
|
|
699
|
+
// Queue text prompts as mid-run steering (mirrors the CLI). Attachments
|
|
700
|
+
// aren't supported mid-run — reject those so the user resends after.
|
|
701
|
+
if (attachments.length > 0) {
|
|
702
|
+
json(res, 409, { error: "cannot attach files while the agent is running" });
|
|
703
|
+
return;
|
|
704
|
+
}
|
|
705
|
+
const count = session.queueMessage(text);
|
|
706
|
+
broadcast("queued", { count });
|
|
707
|
+
json(res, 202, { queued: true, count });
|
|
708
|
+
return;
|
|
709
|
+
}
|
|
710
|
+
json(res, 202, { accepted: true });
|
|
711
|
+
await runAgent(text, async () => {
|
|
712
|
+
if (attachments.length > 0) {
|
|
713
|
+
// Persist each attachment under .gg/uploads so files are inspectable
|
|
714
|
+
// by the agent's tools, then prompt with the media as native blocks.
|
|
715
|
+
const prepared = await prepareAttachments(cwd, attachments);
|
|
716
|
+
await session.promptWithAttachments(text, prepared);
|
|
717
|
+
}
|
|
718
|
+
else {
|
|
719
|
+
// Pass the raw text straight through. AgentSession.prompt() is the
|
|
720
|
+
// single source of truth for slash-command expansion (built-in +
|
|
721
|
+
// `.gg/commands/*.md` custom), so the agent gets the right body
|
|
722
|
+
// while the webview keeps showing the short `/name`.
|
|
723
|
+
await session.prompt(text);
|
|
724
|
+
}
|
|
725
|
+
});
|
|
726
|
+
});
|
|
727
|
+
return;
|
|
728
|
+
}
|
|
729
|
+
if (method === "GET" && url === "/tasks") {
|
|
730
|
+
json(res, 200, { tasks: loadTasksSync(cwd) });
|
|
731
|
+
return;
|
|
732
|
+
}
|
|
733
|
+
// ── Radio ─────────────────────────────────────────────────
|
|
734
|
+
// Playback runs in THIS sidecar process, which is unique per window, so a
|
|
735
|
+
// station only plays in the window that started it.
|
|
736
|
+
if (method === "GET" && url === "/radio") {
|
|
737
|
+
json(res, 200, { stations: RADIO_STATIONS, current: getCurrentStation() });
|
|
738
|
+
return;
|
|
739
|
+
}
|
|
740
|
+
if (method === "POST" && url === "/radio") {
|
|
741
|
+
void readBody(req).then((raw) => {
|
|
742
|
+
let station;
|
|
743
|
+
try {
|
|
744
|
+
station = JSON.parse(raw).station ?? "";
|
|
745
|
+
}
|
|
746
|
+
catch {
|
|
747
|
+
json(res, 400, { error: "invalid JSON body" });
|
|
748
|
+
return;
|
|
749
|
+
}
|
|
750
|
+
if (!station || station === "off") {
|
|
751
|
+
stopRadio();
|
|
752
|
+
json(res, 200, { current: null });
|
|
753
|
+
return;
|
|
754
|
+
}
|
|
755
|
+
const result = playRadio(station);
|
|
756
|
+
if (!result.ok) {
|
|
757
|
+
json(res, 400, { error: result.error ?? "Radio failed to start." });
|
|
758
|
+
return;
|
|
759
|
+
}
|
|
760
|
+
json(res, 200, { current: getCurrentStation() });
|
|
761
|
+
});
|
|
762
|
+
return;
|
|
763
|
+
}
|
|
764
|
+
if (method === "POST" && url === "/tasks/run") {
|
|
765
|
+
void readBody(req).then((raw) => {
|
|
766
|
+
let id;
|
|
767
|
+
let all;
|
|
768
|
+
try {
|
|
769
|
+
const body = JSON.parse(raw);
|
|
770
|
+
id = body.id ?? null;
|
|
771
|
+
all = Boolean(body.all);
|
|
772
|
+
}
|
|
773
|
+
catch {
|
|
774
|
+
json(res, 400, { error: "invalid JSON body" });
|
|
775
|
+
return;
|
|
776
|
+
}
|
|
777
|
+
if (running) {
|
|
778
|
+
json(res, 409, { error: "cannot run a task while the agent is running" });
|
|
779
|
+
return;
|
|
780
|
+
}
|
|
781
|
+
json(res, 202, { accepted: true });
|
|
782
|
+
void runTasks(id, all);
|
|
783
|
+
});
|
|
784
|
+
return;
|
|
785
|
+
}
|
|
786
|
+
if (method === "POST" && url === "/tasks/delete") {
|
|
787
|
+
void readBody(req).then((raw) => {
|
|
788
|
+
let id;
|
|
789
|
+
try {
|
|
790
|
+
id = JSON.parse(raw).id ?? "";
|
|
791
|
+
}
|
|
792
|
+
catch {
|
|
793
|
+
json(res, 400, { error: "invalid JSON body" });
|
|
794
|
+
return;
|
|
795
|
+
}
|
|
796
|
+
if (!id.trim()) {
|
|
797
|
+
json(res, 400, { error: "missing task id" });
|
|
798
|
+
return;
|
|
799
|
+
}
|
|
800
|
+
const remaining = loadTasksSync(cwd).filter((t) => t.id !== id && !t.id.startsWith(id));
|
|
801
|
+
saveTasksSync(cwd, remaining);
|
|
802
|
+
json(res, 200, { tasks: remaining });
|
|
803
|
+
});
|
|
804
|
+
return;
|
|
805
|
+
}
|
|
806
|
+
if (method === "GET" && url === "/models") {
|
|
807
|
+
void (async () => {
|
|
808
|
+
const loggedIn = [];
|
|
809
|
+
for (const p of ALL_PROVIDERS) {
|
|
810
|
+
if (await auth.hasProviderAuth(p))
|
|
811
|
+
loggedIn.push(p);
|
|
812
|
+
}
|
|
813
|
+
// Just the names, grouped by provider in registry order — the UI shows
|
|
814
|
+
// a clean multi-column list of model ids.
|
|
815
|
+
const models = MODELS.filter((m) => loggedIn.includes(m.provider)).map((m) => ({
|
|
816
|
+
id: m.id,
|
|
817
|
+
name: m.name,
|
|
818
|
+
provider: m.provider,
|
|
819
|
+
}));
|
|
820
|
+
json(res, 200, { models });
|
|
821
|
+
})();
|
|
822
|
+
return;
|
|
823
|
+
}
|
|
824
|
+
if (method === "POST" && url === "/model") {
|
|
825
|
+
void readBody(req).then(async (raw) => {
|
|
826
|
+
let modelId;
|
|
827
|
+
try {
|
|
828
|
+
modelId = JSON.parse(raw).model ?? "";
|
|
829
|
+
}
|
|
830
|
+
catch {
|
|
831
|
+
json(res, 400, { error: "invalid JSON body" });
|
|
832
|
+
return;
|
|
833
|
+
}
|
|
834
|
+
const target = getModel(modelId);
|
|
835
|
+
if (!target) {
|
|
836
|
+
json(res, 404, { error: `unknown model: ${modelId}` });
|
|
837
|
+
return;
|
|
838
|
+
}
|
|
839
|
+
if (running) {
|
|
840
|
+
json(res, 409, { error: "cannot switch model while running" });
|
|
841
|
+
return;
|
|
842
|
+
}
|
|
843
|
+
await session.switchModel(target.provider, target.id);
|
|
844
|
+
// Clamp the reasoning level to what the new model supports (mirrors the
|
|
845
|
+
// CLI): keep thinking on at the first supported tier if it was on but
|
|
846
|
+
// the prior level is unsupported here; leave it off if it was off.
|
|
847
|
+
const prevLevel = session.getThinkingLevel();
|
|
848
|
+
if (prevLevel && !isThinkingLevelSupported(target.provider, target.id, prevLevel)) {
|
|
849
|
+
session.setThinkingLevel(getNextThinkingLevel(target.provider, target.id, undefined));
|
|
850
|
+
}
|
|
851
|
+
// Persist so the selection (and clamped thinking level) survives restarts.
|
|
852
|
+
await persistModelSelection(paths.settingsFile, target.provider, target.id);
|
|
853
|
+
await persistThinkingLevel(paths.settingsFile, session.getThinkingLevel());
|
|
854
|
+
const payload = {
|
|
855
|
+
thinkingLevel: session.getThinkingLevel() ?? null,
|
|
856
|
+
supportedThinkingLevels: getSupportedThinkingLevels(target.provider, target.id),
|
|
857
|
+
};
|
|
858
|
+
// model_change is emitted by switchModel; follow with thinking_change so
|
|
859
|
+
// the footer toggle reflects the new model's supported levels.
|
|
860
|
+
broadcast("thinking_change", payload);
|
|
861
|
+
// The new model usually has a different context window — push extras so
|
|
862
|
+
// the footer's context meter rescales immediately.
|
|
863
|
+
broadcast("extras", footerExtras());
|
|
864
|
+
json(res, 200, { provider: target.provider, model: target.id, ...payload });
|
|
865
|
+
});
|
|
866
|
+
return;
|
|
867
|
+
}
|
|
868
|
+
if (method === "POST" && url === "/kill") {
|
|
869
|
+
void readBody(req).then(async (raw) => {
|
|
870
|
+
let id;
|
|
871
|
+
try {
|
|
872
|
+
id = JSON.parse(raw).id ?? "";
|
|
873
|
+
}
|
|
874
|
+
catch {
|
|
875
|
+
json(res, 400, { error: "invalid JSON body" });
|
|
876
|
+
return;
|
|
877
|
+
}
|
|
878
|
+
if (!id.trim()) {
|
|
879
|
+
json(res, 400, { error: "missing task id" });
|
|
880
|
+
return;
|
|
881
|
+
}
|
|
882
|
+
const message = await session.killBackgroundProcess(id);
|
|
883
|
+
// Push the updated task list right away rather than waiting for the poll.
|
|
884
|
+
broadcast("tasks", { tasks: session.listBackgroundProcesses() });
|
|
885
|
+
json(res, 200, { message });
|
|
886
|
+
});
|
|
887
|
+
return;
|
|
888
|
+
}
|
|
889
|
+
if (method === "POST" && url === "/thinking") {
|
|
890
|
+
const st = session.getState();
|
|
891
|
+
const next = getNextThinkingLevel(st.provider, st.model, session.getThinkingLevel());
|
|
892
|
+
session.setThinkingLevel(next);
|
|
893
|
+
// Persist so the thinking level survives app restarts (mirrors the CLI).
|
|
894
|
+
void persistThinkingLevel(paths.settingsFile, next);
|
|
895
|
+
const payload = {
|
|
896
|
+
thinkingLevel: next ?? null,
|
|
897
|
+
supportedThinkingLevels: getSupportedThinkingLevels(st.provider, st.model),
|
|
898
|
+
};
|
|
899
|
+
broadcast("thinking_change", payload);
|
|
900
|
+
json(res, 200, payload);
|
|
901
|
+
return;
|
|
902
|
+
}
|
|
903
|
+
if (method === "POST" && url === "/cancel") {
|
|
904
|
+
abort.abort();
|
|
905
|
+
abort = new AbortController();
|
|
906
|
+
session.setSignal(abort.signal);
|
|
907
|
+
running = false;
|
|
908
|
+
// Stop a run-all sweep so the next pending task isn't auto-started.
|
|
909
|
+
taskRunAll = false;
|
|
910
|
+
// Drop any queued steering and return it so the webview can restore it to
|
|
911
|
+
// the composer.
|
|
912
|
+
const drained = session.drainQueue();
|
|
913
|
+
broadcast("run_end", { cancelled: true });
|
|
914
|
+
broadcast("queued", { count: 0 });
|
|
915
|
+
json(res, 200, { cancelled: true, drained });
|
|
916
|
+
return;
|
|
917
|
+
}
|
|
918
|
+
if (method === "POST" && url === "/new-session") {
|
|
919
|
+
if (running) {
|
|
920
|
+
json(res, 409, { error: "cannot start a new session while running" });
|
|
921
|
+
return;
|
|
922
|
+
}
|
|
923
|
+
void session
|
|
924
|
+
.newSession()
|
|
925
|
+
.then(() => {
|
|
926
|
+
broadcast("session_reset", {});
|
|
927
|
+
json(res, 200, { ok: true });
|
|
928
|
+
})
|
|
929
|
+
.catch((err) => {
|
|
930
|
+
json(res, 500, { error: err instanceof Error ? err.message : String(err) });
|
|
931
|
+
});
|
|
932
|
+
return;
|
|
933
|
+
}
|
|
934
|
+
// ── Provider auth (login) ───────────────────────────────
|
|
935
|
+
if (method === "GET" && url === "/auth/status") {
|
|
936
|
+
void authStatusPayload().then((payload) => json(res, 200, payload));
|
|
937
|
+
return;
|
|
938
|
+
}
|
|
939
|
+
if (method === "POST" && url === "/auth/apikey") {
|
|
940
|
+
void readBody(req).then(async (raw) => {
|
|
941
|
+
let provider = "";
|
|
942
|
+
let key;
|
|
943
|
+
try {
|
|
944
|
+
const body = JSON.parse(raw);
|
|
945
|
+
provider = body.provider ?? "";
|
|
946
|
+
key = (body.key ?? "").trim();
|
|
947
|
+
}
|
|
948
|
+
catch {
|
|
949
|
+
json(res, 400, { error: "invalid JSON body" });
|
|
950
|
+
return;
|
|
951
|
+
}
|
|
952
|
+
const meta = AUTH_PROVIDERS.find((p) => p.value === provider);
|
|
953
|
+
if (!meta || !meta.methods.includes("apikey")) {
|
|
954
|
+
json(res, 400, { error: "provider does not support API key auth" });
|
|
955
|
+
return;
|
|
956
|
+
}
|
|
957
|
+
if (!key) {
|
|
958
|
+
json(res, 400, { error: "API key is required" });
|
|
959
|
+
return;
|
|
960
|
+
}
|
|
961
|
+
const creds = {
|
|
962
|
+
accessToken: key,
|
|
963
|
+
refreshToken: "",
|
|
964
|
+
expiresAt: Date.now() + 365 * 24 * 60 * 60 * 1000 * 100, // ~100y
|
|
965
|
+
...(meta.apiKeyBaseUrl ? { baseUrl: meta.apiKeyBaseUrl } : {}),
|
|
966
|
+
};
|
|
967
|
+
await auth.setCredentials(provider, creds);
|
|
968
|
+
broadcast("auth_done", { provider });
|
|
969
|
+
json(res, 200, { ok: true });
|
|
970
|
+
});
|
|
971
|
+
return;
|
|
972
|
+
}
|
|
973
|
+
if (method === "POST" && url === "/auth/oauth/start") {
|
|
974
|
+
void readBody(req).then((raw) => {
|
|
975
|
+
let provider = "";
|
|
976
|
+
try {
|
|
977
|
+
provider = JSON.parse(raw).provider ?? "";
|
|
978
|
+
}
|
|
979
|
+
catch {
|
|
980
|
+
json(res, 400, { error: "invalid JSON body" });
|
|
981
|
+
return;
|
|
982
|
+
}
|
|
983
|
+
const meta = AUTH_PROVIDERS.find((p) => p.value === provider);
|
|
984
|
+
if (!meta || !meta.methods.includes("oauth")) {
|
|
985
|
+
json(res, 400, { error: "provider does not support OAuth" });
|
|
986
|
+
return;
|
|
987
|
+
}
|
|
988
|
+
if (oauthInFlight) {
|
|
989
|
+
json(res, 409, { error: "a login is already in progress" });
|
|
990
|
+
return;
|
|
991
|
+
}
|
|
992
|
+
oauthInFlight = true;
|
|
993
|
+
json(res, 202, { accepted: true });
|
|
994
|
+
void (async () => {
|
|
995
|
+
const cb = authCallbacks();
|
|
996
|
+
try {
|
|
997
|
+
let creds;
|
|
998
|
+
let storageKey = provider;
|
|
999
|
+
if (provider === "anthropic")
|
|
1000
|
+
creds = await loginAnthropic(cb);
|
|
1001
|
+
else if (provider === "openai")
|
|
1002
|
+
creds = await loginOpenAI(cb);
|
|
1003
|
+
else if (provider === "gemini")
|
|
1004
|
+
creds = await loginGemini(cb);
|
|
1005
|
+
else if (provider === "moonshot") {
|
|
1006
|
+
creds = await loginKimi(cb);
|
|
1007
|
+
storageKey = MOONSHOT_OAUTH_KEY;
|
|
1008
|
+
}
|
|
1009
|
+
else {
|
|
1010
|
+
throw new Error(`OAuth not implemented for ${provider}`);
|
|
1011
|
+
}
|
|
1012
|
+
await auth.setCredentials(storageKey, creds);
|
|
1013
|
+
broadcast("auth_done", { provider });
|
|
1014
|
+
}
|
|
1015
|
+
catch (err) {
|
|
1016
|
+
broadcast("auth_error", {
|
|
1017
|
+
provider,
|
|
1018
|
+
message: err instanceof Error ? err.message : String(err),
|
|
1019
|
+
});
|
|
1020
|
+
}
|
|
1021
|
+
finally {
|
|
1022
|
+
oauthInFlight = false;
|
|
1023
|
+
pendingCode = null;
|
|
1024
|
+
}
|
|
1025
|
+
})();
|
|
1026
|
+
});
|
|
1027
|
+
return;
|
|
1028
|
+
}
|
|
1029
|
+
if (method === "POST" && url === "/auth/oauth/code") {
|
|
1030
|
+
void readBody(req).then((raw) => {
|
|
1031
|
+
let code;
|
|
1032
|
+
try {
|
|
1033
|
+
code = JSON.parse(raw).code ?? "";
|
|
1034
|
+
}
|
|
1035
|
+
catch {
|
|
1036
|
+
json(res, 400, { error: "invalid JSON body" });
|
|
1037
|
+
return;
|
|
1038
|
+
}
|
|
1039
|
+
if (!pendingCode) {
|
|
1040
|
+
json(res, 409, { error: "no login is awaiting a code" });
|
|
1041
|
+
return;
|
|
1042
|
+
}
|
|
1043
|
+
pendingCode(code.trim());
|
|
1044
|
+
pendingCode = null;
|
|
1045
|
+
json(res, 200, { ok: true });
|
|
1046
|
+
});
|
|
1047
|
+
return;
|
|
1048
|
+
}
|
|
1049
|
+
if (method === "POST" && url === "/auth/logout") {
|
|
1050
|
+
void readBody(req).then(async (raw) => {
|
|
1051
|
+
let provider;
|
|
1052
|
+
try {
|
|
1053
|
+
provider = JSON.parse(raw).provider ?? "";
|
|
1054
|
+
}
|
|
1055
|
+
catch {
|
|
1056
|
+
json(res, 400, { error: "invalid JSON body" });
|
|
1057
|
+
return;
|
|
1058
|
+
}
|
|
1059
|
+
await auth.clearCredentials(provider);
|
|
1060
|
+
// Moonshot's OAuth credential lives under a distinct key — clear both so
|
|
1061
|
+
// "disconnect" fully removes Kimi OAuth and the API key.
|
|
1062
|
+
if (provider === "moonshot")
|
|
1063
|
+
await auth.clearCredentials(MOONSHOT_OAUTH_KEY);
|
|
1064
|
+
broadcast("auth_done", { provider });
|
|
1065
|
+
json(res, 200, { ok: true });
|
|
1066
|
+
});
|
|
1067
|
+
return;
|
|
1068
|
+
}
|
|
1069
|
+
// ── Telegram config (mirrors `ggcoder telegram`) ─────────
|
|
1070
|
+
if (method === "GET" && url === "/telegram") {
|
|
1071
|
+
void loadTelegramConfig().then((cfg) => {
|
|
1072
|
+
if (!cfg) {
|
|
1073
|
+
json(res, 200, { configured: false });
|
|
1074
|
+
return;
|
|
1075
|
+
}
|
|
1076
|
+
// Never return the raw token to the webview — a short masked preview is
|
|
1077
|
+
// enough to show "already set".
|
|
1078
|
+
const t = cfg.botToken;
|
|
1079
|
+
const tokenPreview = t.length > 14 ? `${t.slice(0, 10)}\u2026${t.slice(-4)}` : "set";
|
|
1080
|
+
json(res, 200, { configured: true, userId: cfg.userId, tokenPreview });
|
|
1081
|
+
});
|
|
1082
|
+
return;
|
|
1083
|
+
}
|
|
1084
|
+
if (method === "POST" && url === "/telegram") {
|
|
1085
|
+
void readBody(req).then(async (raw) => {
|
|
1086
|
+
let botTokenInput;
|
|
1087
|
+
let userIdInput;
|
|
1088
|
+
try {
|
|
1089
|
+
const body = JSON.parse(raw);
|
|
1090
|
+
botTokenInput = (body.botToken ?? "").trim();
|
|
1091
|
+
userIdInput = String(body.userId ?? "").trim();
|
|
1092
|
+
}
|
|
1093
|
+
catch {
|
|
1094
|
+
json(res, 400, { error: "invalid JSON body" });
|
|
1095
|
+
return;
|
|
1096
|
+
}
|
|
1097
|
+
// Keep the existing token when the field is left blank (the webview shows
|
|
1098
|
+
// a masked preview, not the real token).
|
|
1099
|
+
const existing = await loadTelegramConfig();
|
|
1100
|
+
const botToken = botTokenInput || existing?.botToken || "";
|
|
1101
|
+
if (!botToken) {
|
|
1102
|
+
json(res, 400, { error: "Bot token is required." });
|
|
1103
|
+
return;
|
|
1104
|
+
}
|
|
1105
|
+
const userId = userIdInput ? parseInt(userIdInput, 10) : existing?.userId;
|
|
1106
|
+
if (!userId || Number.isNaN(userId)) {
|
|
1107
|
+
json(res, 400, { error: "A numeric Telegram user ID is required." });
|
|
1108
|
+
return;
|
|
1109
|
+
}
|
|
1110
|
+
const verified = await verifyBotToken(botToken);
|
|
1111
|
+
if (!verified.ok) {
|
|
1112
|
+
json(res, 400, { error: "Invalid bot token — Telegram rejected it." });
|
|
1113
|
+
return;
|
|
1114
|
+
}
|
|
1115
|
+
await saveTelegramConfig({ botToken, userId });
|
|
1116
|
+
json(res, 200, { ok: true, userId, username: verified.username ?? null });
|
|
1117
|
+
});
|
|
1118
|
+
return;
|
|
1119
|
+
}
|
|
1120
|
+
// ── Serve lifecycle (mirrors `ggcoder serve`) ───────────
|
|
1121
|
+
if (method === "GET" && url === "/serve") {
|
|
1122
|
+
void loadTelegramConfig().then((cfg) => json(res, 200, { running: serveController !== null, configured: cfg !== null }));
|
|
1123
|
+
return;
|
|
1124
|
+
}
|
|
1125
|
+
if (method === "POST" && url === "/serve/start") {
|
|
1126
|
+
void (async () => {
|
|
1127
|
+
if (serveController) {
|
|
1128
|
+
json(res, 200, { running: true });
|
|
1129
|
+
return;
|
|
1130
|
+
}
|
|
1131
|
+
const cfg = await loadTelegramConfig();
|
|
1132
|
+
if (!cfg) {
|
|
1133
|
+
json(res, 400, { error: "Telegram isn't set up yet. Open Serve settings first." });
|
|
1134
|
+
return;
|
|
1135
|
+
}
|
|
1136
|
+
const st = session.getState();
|
|
1137
|
+
try {
|
|
1138
|
+
serveController = await startServeMode({
|
|
1139
|
+
provider: st.provider,
|
|
1140
|
+
model: st.model,
|
|
1141
|
+
cwd,
|
|
1142
|
+
version: "app",
|
|
1143
|
+
thinkingLevel: session.getThinkingLevel() ?? undefined,
|
|
1144
|
+
telegram: { botToken: cfg.botToken, userId: cfg.userId },
|
|
1145
|
+
embedded: true,
|
|
1146
|
+
});
|
|
1147
|
+
broadcast("serve_change", { running: true });
|
|
1148
|
+
log("INFO", "app-sidecar", "serve started", { userId: cfg.userId });
|
|
1149
|
+
json(res, 200, { running: true });
|
|
1150
|
+
}
|
|
1151
|
+
catch (err) {
|
|
1152
|
+
serveController = null;
|
|
1153
|
+
json(res, 400, { error: err instanceof Error ? err.message : String(err) });
|
|
1154
|
+
}
|
|
1155
|
+
})();
|
|
1156
|
+
return;
|
|
1157
|
+
}
|
|
1158
|
+
if (method === "POST" && url === "/serve/stop") {
|
|
1159
|
+
void (async () => {
|
|
1160
|
+
if (serveController) {
|
|
1161
|
+
await serveController.stop().catch(() => { });
|
|
1162
|
+
serveController = null;
|
|
1163
|
+
broadcast("serve_change", { running: false });
|
|
1164
|
+
log("INFO", "app-sidecar", "serve stopped");
|
|
1165
|
+
}
|
|
1166
|
+
json(res, 200, { running: false });
|
|
1167
|
+
})();
|
|
1168
|
+
return;
|
|
1169
|
+
}
|
|
1170
|
+
json(res, 404, { error: "not found" });
|
|
1171
|
+
});
|
|
1172
|
+
server.listen(port, host, () => {
|
|
1173
|
+
const addr = server.address();
|
|
1174
|
+
// The Rust shell reads this line to learn the port.
|
|
1175
|
+
process.stdout.write(`GG_APP_LISTENING ${addr.port}\n`);
|
|
1176
|
+
log("INFO", "app-sidecar", "listening", { port: String(addr.port), host });
|
|
1177
|
+
});
|
|
1178
|
+
const shutdown = async () => {
|
|
1179
|
+
tasksPollStopped = true;
|
|
1180
|
+
if (tasksPoll)
|
|
1181
|
+
clearTimeout(tasksPoll);
|
|
1182
|
+
// Kill any playing radio so the stream dies with its window.
|
|
1183
|
+
stopRadio();
|
|
1184
|
+
// Stop the Telegram serve loop + dispose its per-chat sessions.
|
|
1185
|
+
if (serveController)
|
|
1186
|
+
await serveController.stop().catch(() => { });
|
|
1187
|
+
for (const c of clients)
|
|
1188
|
+
c.res.end();
|
|
1189
|
+
server.close();
|
|
1190
|
+
await session.dispose().catch(() => { });
|
|
1191
|
+
process.exit(0);
|
|
1192
|
+
};
|
|
1193
|
+
process.on("SIGINT", () => void shutdown());
|
|
1194
|
+
process.on("SIGTERM", () => void shutdown());
|
|
1195
|
+
}
|
|
1196
|
+
main().catch((err) => {
|
|
1197
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
1198
|
+
process.stderr.write(`GG_APP_FATAL ${message}\n`);
|
|
1199
|
+
process.exit(1);
|
|
1200
|
+
});
|
|
1201
|
+
//# sourceMappingURL=app-sidecar.js.map
|