@lelouchhe/webagent 0.1.0 → 0.1.2
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/README.md +3 -0
- package/bin/webagent.mjs +2 -2
- package/dist/index.html +2 -2
- package/dist/js/app.mmjrsqet.js +10 -0
- package/dist/js/{commands.mmjqzu9r.js → commands.mmjrsqet.js} +3 -3
- package/dist/js/{connection.mmjqzu9r.js → connection.mmjrsqet.js} +3 -3
- package/dist/js/{events.mmjqzu9r.js → events.mmjrsqet.js} +2 -2
- package/dist/js/{images.mmjqzu9r.js → images.mmjrsqet.js} +1 -1
- package/dist/js/{input.mmjqzu9r.js → input.mmjrsqet.js} +4 -4
- package/dist/js/{render.mmjqzu9r.js → render.mmjrsqet.js} +1 -1
- package/lib/bridge.js +284 -0
- package/lib/config.js +57 -0
- package/lib/routes.js +137 -0
- package/lib/server.js +144 -0
- package/lib/session-manager.js +198 -0
- package/lib/store.js +101 -0
- package/lib/title-service.js +71 -0
- package/lib/types.js +47 -0
- package/lib/ws-handler.js +254 -0
- package/package.json +5 -4
- package/dist/js/app.mmjqzu9r.js +0 -10
- package/src/bridge.ts +0 -317
- package/src/config.ts +0 -65
- package/src/routes.ts +0 -147
- package/src/server.ts +0 -159
- package/src/session-manager.ts +0 -223
- package/src/store.ts +0 -140
- package/src/title-service.ts +0 -81
- package/src/types.ts +0 -81
- package/src/ws-handler.ts +0 -264
- /package/dist/js/{state.mmjqzu9r.js → state.mmjrsqet.js} +0 -0
- /package/dist/{styles.mmjqzu9r.css → styles.mmjrsqet.css} +0 -0
package/src/routes.ts
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import { readFile, writeFile, mkdir } from "node:fs/promises";
|
|
2
|
-
import { join, extname } from "node:path";
|
|
3
|
-
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
4
|
-
import type { Store } from "./store.ts";
|
|
5
|
-
import type { Config } from "./config.ts";
|
|
6
|
-
|
|
7
|
-
const SAFE_ID = /^[a-zA-Z0-9_-]+$/;
|
|
8
|
-
|
|
9
|
-
const MIME: Record<string, string> = {
|
|
10
|
-
".html": "text/html",
|
|
11
|
-
".js": "application/javascript",
|
|
12
|
-
".css": "text/css",
|
|
13
|
-
".json": "application/json",
|
|
14
|
-
".svg": "image/svg+xml",
|
|
15
|
-
".png": "image/png",
|
|
16
|
-
".jpg": "image/jpeg",
|
|
17
|
-
".jpeg": "image/jpeg",
|
|
18
|
-
".gif": "image/gif",
|
|
19
|
-
".webp": "image/webp",
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export function createRequestHandler(store: Store, publicDir: string, dataDir: string, limits: Config["limits"]) {
|
|
23
|
-
return async (req: IncomingMessage, res: ServerResponse): Promise<void> => {
|
|
24
|
-
const url = req.url ?? "/";
|
|
25
|
-
|
|
26
|
-
// --- API routes ---
|
|
27
|
-
if (url.startsWith("/api/")) {
|
|
28
|
-
res.setHeader("Content-Type", "application/json");
|
|
29
|
-
|
|
30
|
-
// GET /api/sessions
|
|
31
|
-
if (url === "/api/sessions" && req.method === "GET") {
|
|
32
|
-
res.end(JSON.stringify(store.listSessions()));
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// GET /api/sessions/:id/events?thinking=0|1
|
|
37
|
-
const eventsMatch = url.match(/^\/api\/sessions\/([^/]+)\/events(\?.*)?$/);
|
|
38
|
-
if (eventsMatch && req.method === "GET") {
|
|
39
|
-
const sessionId = decodeURIComponent(eventsMatch[1]);
|
|
40
|
-
const params = new URLSearchParams(eventsMatch[2]?.slice(1) ?? "");
|
|
41
|
-
const excludeThinking = params.get("thinking") === "0";
|
|
42
|
-
const afterSeqRaw = params.get("after_seq");
|
|
43
|
-
const afterSeq = afterSeqRaw != null ? Number(afterSeqRaw) : undefined;
|
|
44
|
-
const session = store.getSession(sessionId);
|
|
45
|
-
if (!session) {
|
|
46
|
-
res.writeHead(404);
|
|
47
|
-
res.end(JSON.stringify({ error: "Session not found" }));
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
const events = store.getEvents(sessionId, { excludeThinking, afterSeq });
|
|
51
|
-
res.end(JSON.stringify(events));
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// POST /api/images/:sessionId
|
|
56
|
-
const imgMatch = url.match(/^\/api\/images\/([^/]+)$/);
|
|
57
|
-
if (imgMatch && req.method === "POST") {
|
|
58
|
-
const sessionId = decodeURIComponent(imgMatch[1]);
|
|
59
|
-
if (!SAFE_ID.test(sessionId)) {
|
|
60
|
-
res.writeHead(400);
|
|
61
|
-
res.end(JSON.stringify({ error: "Invalid session ID" }));
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
// Enforce upload size limit
|
|
65
|
-
const contentLength = parseInt(req.headers["content-length"] ?? "0", 10);
|
|
66
|
-
if (contentLength > limits.image_upload) {
|
|
67
|
-
res.writeHead(413);
|
|
68
|
-
res.end(JSON.stringify({ error: "Upload too large" }));
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
const chunks: Buffer[] = [];
|
|
72
|
-
let totalSize = 0;
|
|
73
|
-
for await (const chunk of req) {
|
|
74
|
-
totalSize += (chunk as Buffer).length;
|
|
75
|
-
if (totalSize > limits.image_upload) {
|
|
76
|
-
res.writeHead(413);
|
|
77
|
-
res.end(JSON.stringify({ error: "Upload too large" }));
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
chunks.push(chunk as Buffer);
|
|
81
|
-
}
|
|
82
|
-
let body: { data: string; mimeType: string };
|
|
83
|
-
try {
|
|
84
|
-
body = JSON.parse(Buffer.concat(chunks).toString());
|
|
85
|
-
} catch {
|
|
86
|
-
res.writeHead(400);
|
|
87
|
-
res.end(JSON.stringify({ error: "Invalid JSON" }));
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
const { data, mimeType } = body;
|
|
91
|
-
const ext = mimeType.split("/")[1]?.replace("jpeg", "jpg") ?? "png";
|
|
92
|
-
const seq = Date.now();
|
|
93
|
-
const relPath = `images/${sessionId}/${seq}.${ext}`;
|
|
94
|
-
const absPath = join(dataDir, relPath);
|
|
95
|
-
await mkdir(join(dataDir, "images", sessionId), { recursive: true });
|
|
96
|
-
await writeFile(absPath, Buffer.from(data, "base64"));
|
|
97
|
-
const imgUrl = `/data/${relPath}`;
|
|
98
|
-
res.end(JSON.stringify({ path: relPath, url: imgUrl }));
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
res.writeHead(404);
|
|
103
|
-
res.end(JSON.stringify({ error: "Not found" }));
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// --- Serve uploaded images: /data/images/... ---
|
|
108
|
-
if (url.startsWith("/data/images/")) {
|
|
109
|
-
const filePath = join(dataDir, url.slice(6)); // strip "/data/"
|
|
110
|
-
if (!filePath.startsWith(join(dataDir, "images"))) {
|
|
111
|
-
res.writeHead(403);
|
|
112
|
-
res.end("Forbidden");
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
try {
|
|
116
|
-
const data = await readFile(filePath);
|
|
117
|
-
const ext = extname(filePath);
|
|
118
|
-
res.writeHead(200, {
|
|
119
|
-
"Content-Type": MIME[ext] ?? "application/octet-stream",
|
|
120
|
-
"Cache-Control": "public, max-age=31536000, immutable",
|
|
121
|
-
});
|
|
122
|
-
res.end(data);
|
|
123
|
-
} catch {
|
|
124
|
-
res.writeHead(404);
|
|
125
|
-
res.end("Not found");
|
|
126
|
-
}
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// --- Static files ---
|
|
131
|
-
const filePath = join(publicDir, url === "/" ? "/index.html" : url);
|
|
132
|
-
if (!filePath.startsWith(publicDir)) {
|
|
133
|
-
res.writeHead(403);
|
|
134
|
-
res.end("Forbidden");
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
try {
|
|
138
|
-
const data = await readFile(filePath);
|
|
139
|
-
const ext = extname(filePath);
|
|
140
|
-
res.writeHead(200, { "Content-Type": MIME[ext] ?? "application/octet-stream" });
|
|
141
|
-
res.end(data);
|
|
142
|
-
} catch {
|
|
143
|
-
res.writeHead(404);
|
|
144
|
-
res.end("Not found");
|
|
145
|
-
}
|
|
146
|
-
};
|
|
147
|
-
}
|
package/src/server.ts
DELETED
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
import { createServer } from "node:http";
|
|
2
|
-
import { join } from "node:path";
|
|
3
|
-
import { fileURLToPath } from "node:url";
|
|
4
|
-
import { WebSocketServer } from "ws";
|
|
5
|
-
import { loadConfig } from "./config.ts";
|
|
6
|
-
import { AgentBridge } from "./bridge.ts";
|
|
7
|
-
import { Store } from "./store.ts";
|
|
8
|
-
import { SessionManager } from "./session-manager.ts";
|
|
9
|
-
import { TitleService } from "./title-service.ts";
|
|
10
|
-
import { createRequestHandler } from "./routes.ts";
|
|
11
|
-
import { setupWsHandler, broadcast } from "./ws-handler.ts";
|
|
12
|
-
import type { AgentEvent } from "./types.ts";
|
|
13
|
-
|
|
14
|
-
const config = loadConfig();
|
|
15
|
-
const __dirname = fileURLToPath(new URL(".", import.meta.url));
|
|
16
|
-
const PUBLIC_DIR = join(__dirname, "..", config.public_dir);
|
|
17
|
-
|
|
18
|
-
// --- Core dependencies ---
|
|
19
|
-
|
|
20
|
-
const store = new Store(config.data_dir);
|
|
21
|
-
console.log(`[store] using ${config.data_dir}/`);
|
|
22
|
-
|
|
23
|
-
const sessions = new SessionManager(store, config.default_cwd, config.data_dir);
|
|
24
|
-
const titleService = new TitleService(store, sessions, config.default_cwd);
|
|
25
|
-
|
|
26
|
-
let bridge: AgentBridge | null = null;
|
|
27
|
-
|
|
28
|
-
// --- HTTP + WebSocket servers ---
|
|
29
|
-
|
|
30
|
-
const server = createServer(createRequestHandler(store, PUBLIC_DIR, config.data_dir, config.limits));
|
|
31
|
-
const wss = new WebSocketServer({ server });
|
|
32
|
-
|
|
33
|
-
setupWsHandler({
|
|
34
|
-
wss,
|
|
35
|
-
store,
|
|
36
|
-
sessions,
|
|
37
|
-
titleService,
|
|
38
|
-
getBridge: () => bridge,
|
|
39
|
-
limits: config.limits,
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
// --- Bridge initialization ---
|
|
43
|
-
|
|
44
|
-
async function initBridge(): Promise<AgentBridge> {
|
|
45
|
-
const b = new AgentBridge(config.agent_cmd);
|
|
46
|
-
|
|
47
|
-
b.on("event", (event: AgentEvent) => {
|
|
48
|
-
if (sessions.restoringSessions.has(event.sessionId)) return;
|
|
49
|
-
|
|
50
|
-
switch (event.type) {
|
|
51
|
-
case "connected":
|
|
52
|
-
event.cancelTimeout = config.limits.cancel_timeout;
|
|
53
|
-
break;
|
|
54
|
-
case "session_created":
|
|
55
|
-
if (event.configOptions?.length) sessions.cachedConfigOptions = event.configOptions;
|
|
56
|
-
for (const opt of event.configOptions ?? []) {
|
|
57
|
-
store.updateSessionConfig(event.sessionId, opt.id, opt.currentValue);
|
|
58
|
-
}
|
|
59
|
-
break;
|
|
60
|
-
case "config_option_update":
|
|
61
|
-
if (event.configOptions?.length) sessions.cachedConfigOptions = event.configOptions;
|
|
62
|
-
for (const opt of event.configOptions ?? []) {
|
|
63
|
-
store.updateSessionConfig(event.sessionId, opt.id, opt.currentValue);
|
|
64
|
-
}
|
|
65
|
-
break;
|
|
66
|
-
case "message_chunk":
|
|
67
|
-
sessions.flushThinkingBuffer(event.sessionId);
|
|
68
|
-
sessions.appendAssistant(event.sessionId, event.text);
|
|
69
|
-
break;
|
|
70
|
-
case "thought_chunk":
|
|
71
|
-
sessions.flushAssistantBuffer(event.sessionId);
|
|
72
|
-
sessions.appendThinking(event.sessionId, event.text);
|
|
73
|
-
break;
|
|
74
|
-
case "tool_call":
|
|
75
|
-
sessions.flushBuffers(event.sessionId);
|
|
76
|
-
store.saveEvent(event.sessionId, event.type, { id: event.id, title: event.title, kind: event.kind, rawInput: event.rawInput });
|
|
77
|
-
break;
|
|
78
|
-
case "tool_call_update":
|
|
79
|
-
store.saveEvent(event.sessionId, event.type, { id: event.id, status: event.status, content: event.content });
|
|
80
|
-
break;
|
|
81
|
-
case "plan":
|
|
82
|
-
sessions.flushBuffers(event.sessionId);
|
|
83
|
-
store.saveEvent(event.sessionId, event.type, { entries: event.entries });
|
|
84
|
-
break;
|
|
85
|
-
case "permission_request": {
|
|
86
|
-
sessions.flushBuffers(event.sessionId);
|
|
87
|
-
store.saveEvent(event.sessionId, event.type, {
|
|
88
|
-
requestId: event.requestId, title: event.title, options: event.options,
|
|
89
|
-
});
|
|
90
|
-
// Auto-approve permissions in autopilot mode (allow_once only to avoid persisting across mode switches)
|
|
91
|
-
const mode = store.getSession(event.sessionId)?.mode ?? "";
|
|
92
|
-
if (mode.includes("#autopilot")) {
|
|
93
|
-
const opt = event.options.find((o: any) => o.kind === "allow_once");
|
|
94
|
-
if (opt) {
|
|
95
|
-
b.resolvePermission(event.requestId, opt.optionId);
|
|
96
|
-
const optionName = opt.label ?? opt.optionId;
|
|
97
|
-
store.saveEvent(event.sessionId, "permission_response", {
|
|
98
|
-
requestId: event.requestId, optionName, denied: false,
|
|
99
|
-
});
|
|
100
|
-
// Skip broadcasting the permission_request — send resolved directly
|
|
101
|
-
broadcast(wss, {
|
|
102
|
-
type: "permission_resolved",
|
|
103
|
-
sessionId: event.sessionId,
|
|
104
|
-
requestId: event.requestId,
|
|
105
|
-
optionName,
|
|
106
|
-
denied: false,
|
|
107
|
-
} as any);
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
break;
|
|
112
|
-
}
|
|
113
|
-
case "prompt_done":
|
|
114
|
-
sessions.activePrompts.delete(event.sessionId);
|
|
115
|
-
sessions.flushBuffers(event.sessionId);
|
|
116
|
-
store.saveEvent(event.sessionId, event.type, { stopReason: event.stopReason });
|
|
117
|
-
break;
|
|
118
|
-
case "error":
|
|
119
|
-
if (event.sessionId) {
|
|
120
|
-
sessions.activePrompts.delete(event.sessionId);
|
|
121
|
-
}
|
|
122
|
-
break;
|
|
123
|
-
}
|
|
124
|
-
broadcast(wss, event);
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
await b.start();
|
|
128
|
-
bridge = b;
|
|
129
|
-
return b;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// --- Graceful shutdown ---
|
|
133
|
-
|
|
134
|
-
async function shutdown() {
|
|
135
|
-
console.log("\n[server] shutting down...");
|
|
136
|
-
sessions.killAllBashProcs();
|
|
137
|
-
wss.close();
|
|
138
|
-
await bridge?.shutdown();
|
|
139
|
-
store.close();
|
|
140
|
-
server.close();
|
|
141
|
-
process.exit(0);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
process.on("SIGINT", shutdown);
|
|
145
|
-
process.on("SIGTERM", shutdown);
|
|
146
|
-
|
|
147
|
-
// --- Start ---
|
|
148
|
-
|
|
149
|
-
server.listen(config.port, "0.0.0.0", async () => {
|
|
150
|
-
console.log(`[server] listening on http://localhost:${config.port}`);
|
|
151
|
-
console.log(`[bridge] starting: ${config.agent_cmd}...`);
|
|
152
|
-
try {
|
|
153
|
-
await initBridge();
|
|
154
|
-
console.log(`[bridge] ready`);
|
|
155
|
-
sessions.hydrate();
|
|
156
|
-
} catch (err) {
|
|
157
|
-
console.error(`[bridge] failed to start:`, err);
|
|
158
|
-
}
|
|
159
|
-
});
|
package/src/session-manager.ts
DELETED
|
@@ -1,223 +0,0 @@
|
|
|
1
|
-
import type { ChildProcess } from "node:child_process";
|
|
2
|
-
import { rm } from "node:fs/promises";
|
|
3
|
-
import { stat } from "node:fs/promises";
|
|
4
|
-
import { join } from "node:path";
|
|
5
|
-
import type { Store } from "./store.ts";
|
|
6
|
-
import type { AgentBridge } from "./bridge.ts";
|
|
7
|
-
import type { AgentEvent, ConfigOption } from "./types.ts";
|
|
8
|
-
|
|
9
|
-
type SessionBridge = Pick<AgentBridge, "newSession" | "setConfigOption" | "loadSession">;
|
|
10
|
-
|
|
11
|
-
/** Known config option IDs that we persist per-session. */
|
|
12
|
-
const PERSISTED_CONFIG_IDS = ["model", "mode", "reasoning_effort"] as const;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Centralizes all session-related state that was previously scattered
|
|
16
|
-
* across module-level variables in server.ts.
|
|
17
|
-
*/
|
|
18
|
-
export class SessionManager {
|
|
19
|
-
readonly liveSessions = new Set<string>();
|
|
20
|
-
readonly restoringSessions = new Set<string>();
|
|
21
|
-
readonly sessionHasTitle = new Set<string>();
|
|
22
|
-
readonly assistantBuffers = new Map<string, string>();
|
|
23
|
-
readonly thinkingBuffers = new Map<string, string>();
|
|
24
|
-
readonly activePrompts = new Set<string>();
|
|
25
|
-
readonly runningBashProcs = new Map<string, ChildProcess>();
|
|
26
|
-
|
|
27
|
-
cachedConfigOptions: ConfigOption[] = [];
|
|
28
|
-
|
|
29
|
-
private store: Store;
|
|
30
|
-
private defaultCwd: string;
|
|
31
|
-
private dataDir: string;
|
|
32
|
-
|
|
33
|
-
constructor(store: Store, defaultCwd: string, dataDir: string) {
|
|
34
|
-
this.store = store;
|
|
35
|
-
this.defaultCwd = defaultCwd;
|
|
36
|
-
this.dataDir = dataDir;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** Populate sessionHasTitle from existing DB sessions on startup. */
|
|
40
|
-
hydrate(): void {
|
|
41
|
-
for (const s of this.store.listSessions()) {
|
|
42
|
-
if (s.title) this.sessionHasTitle.add(s.id);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** Create a new session in both bridge and store, inheriting the source session's config. */
|
|
47
|
-
async createSession(
|
|
48
|
-
bridge: SessionBridge,
|
|
49
|
-
cwd?: string,
|
|
50
|
-
inheritFromSessionId?: string,
|
|
51
|
-
): Promise<{ sessionId: string; configOptions: ConfigOption[] }> {
|
|
52
|
-
const sessionCwd = cwd ?? this.defaultCwd;
|
|
53
|
-
try {
|
|
54
|
-
const info = await stat(sessionCwd);
|
|
55
|
-
if (!info.isDirectory()) throw new Error("not a directory");
|
|
56
|
-
} catch {
|
|
57
|
-
throw new Error(`Directory does not exist: ${sessionCwd}`);
|
|
58
|
-
}
|
|
59
|
-
const sourceSession = inheritFromSessionId
|
|
60
|
-
? this.store.getSession(inheritFromSessionId)
|
|
61
|
-
: null;
|
|
62
|
-
const sessionId = await bridge.newSession(sessionCwd);
|
|
63
|
-
this.liveSessions.add(sessionId);
|
|
64
|
-
this.store.createSession(sessionId, sessionCwd);
|
|
65
|
-
|
|
66
|
-
// Inherit config options from source session
|
|
67
|
-
if (sourceSession) {
|
|
68
|
-
const inherited: Array<{ configId: string; value: string | null }> = [
|
|
69
|
-
{ configId: "model", value: sourceSession.model },
|
|
70
|
-
{ configId: "reasoning_effort", value: sourceSession.reasoning_effort },
|
|
71
|
-
];
|
|
72
|
-
for (const { configId, value } of inherited) {
|
|
73
|
-
if (!value) continue;
|
|
74
|
-
try {
|
|
75
|
-
await bridge.setConfigOption(sessionId, configId, value);
|
|
76
|
-
this.store.updateSessionConfig(sessionId, configId, value);
|
|
77
|
-
} catch {
|
|
78
|
-
// Option may no longer be available; ignore
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const session = this.store.getSession(sessionId);
|
|
84
|
-
return {
|
|
85
|
-
sessionId,
|
|
86
|
-
configOptions: session ? this.buildConfigOptions(session) : [],
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/** Resume a session — returns event to send to the requesting client. */
|
|
91
|
-
async resumeSession(
|
|
92
|
-
bridge: SessionBridge,
|
|
93
|
-
sessionId: string,
|
|
94
|
-
): Promise<AgentEvent> {
|
|
95
|
-
const session = this.store.getSession(sessionId);
|
|
96
|
-
if (!session) throw new Error("Session not found");
|
|
97
|
-
|
|
98
|
-
if (this.liveSessions.has(sessionId)) {
|
|
99
|
-
// Session already live — build configOptions with stored overrides
|
|
100
|
-
const configOptions = this.buildConfigOptions(session);
|
|
101
|
-
return {
|
|
102
|
-
type: "session_created",
|
|
103
|
-
sessionId,
|
|
104
|
-
cwd: session.cwd,
|
|
105
|
-
title: session.title,
|
|
106
|
-
configOptions,
|
|
107
|
-
busyKind: this.getBusyKind(sessionId) ?? undefined,
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// Restore via ACP
|
|
112
|
-
this.restoringSessions.add(sessionId);
|
|
113
|
-
try {
|
|
114
|
-
const restored = await bridge.loadSession(sessionId, session.cwd);
|
|
115
|
-
this.liveSessions.add(sessionId);
|
|
116
|
-
if (session.title) this.sessionHasTitle.add(sessionId);
|
|
117
|
-
const configOptions = this.applyStoredConfig(restored.configOptions, session);
|
|
118
|
-
console.log(`[session] restored: ${sessionId.slice(0, 8)}…`);
|
|
119
|
-
return {
|
|
120
|
-
type: "session_created",
|
|
121
|
-
sessionId,
|
|
122
|
-
cwd: session.cwd,
|
|
123
|
-
title: session.title,
|
|
124
|
-
configOptions,
|
|
125
|
-
busyKind: this.getBusyKind(sessionId) ?? undefined,
|
|
126
|
-
};
|
|
127
|
-
} catch (err) {
|
|
128
|
-
console.error(`[session] restore failed:`, err);
|
|
129
|
-
throw err;
|
|
130
|
-
} finally {
|
|
131
|
-
this.restoringSessions.delete(sessionId);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/** Build configOptions from cache, overriding currentValue with stored session values. */
|
|
136
|
-
private buildConfigOptions(session: { model: string | null; mode: string | null; reasoning_effort: string | null }): ConfigOption[] {
|
|
137
|
-
return this.applyStoredConfig(this.cachedConfigOptions, session);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/** Override currentValue in configOptions with stored session values. */
|
|
141
|
-
private applyStoredConfig(
|
|
142
|
-
configOptions: ConfigOption[],
|
|
143
|
-
session: { model: string | null; mode: string | null; reasoning_effort: string | null },
|
|
144
|
-
): ConfigOption[] {
|
|
145
|
-
if (!configOptions.length) return this.cachedConfigOptions;
|
|
146
|
-
const stored: Record<string, string | null> = {
|
|
147
|
-
model: session.model,
|
|
148
|
-
mode: session.mode,
|
|
149
|
-
reasoning_effort: session.reasoning_effort,
|
|
150
|
-
};
|
|
151
|
-
return configOptions.map((opt) => {
|
|
152
|
-
const override = stored[opt.id];
|
|
153
|
-
if (override) return { ...opt, currentValue: override };
|
|
154
|
-
return opt;
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/** Delete a session from store and clean up all state (including images). */
|
|
159
|
-
deleteSession(sessionId: string): void {
|
|
160
|
-
this.store.deleteSession(sessionId);
|
|
161
|
-
this.liveSessions.delete(sessionId);
|
|
162
|
-
this.sessionHasTitle.delete(sessionId);
|
|
163
|
-
this.assistantBuffers.delete(sessionId);
|
|
164
|
-
this.thinkingBuffers.delete(sessionId);
|
|
165
|
-
this.activePrompts.delete(sessionId);
|
|
166
|
-
this.runningBashProcs.delete(sessionId);
|
|
167
|
-
// Remove uploaded images for this session
|
|
168
|
-
rm(join(this.dataDir, "images", sessionId), { recursive: true, force: true }).catch(() => {});
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/** Flush assistant/thinking buffers to store. */
|
|
172
|
-
flushBuffers(sessionId: string): void {
|
|
173
|
-
this.flushAssistantBuffer(sessionId);
|
|
174
|
-
this.flushThinkingBuffer(sessionId);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/** Flush only the assistant message buffer to store. */
|
|
178
|
-
flushAssistantBuffer(sessionId: string): void {
|
|
179
|
-
const assistant = this.assistantBuffers.get(sessionId);
|
|
180
|
-
if (assistant) {
|
|
181
|
-
this.store.saveEvent(sessionId, "assistant_message", { text: assistant });
|
|
182
|
-
this.assistantBuffers.delete(sessionId);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/** Flush only the thinking buffer to store. */
|
|
187
|
-
flushThinkingBuffer(sessionId: string): void {
|
|
188
|
-
const thinking = this.thinkingBuffers.get(sessionId);
|
|
189
|
-
if (thinking) {
|
|
190
|
-
this.store.saveEvent(sessionId, "thinking", { text: thinking });
|
|
191
|
-
this.thinkingBuffers.delete(sessionId);
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/** Append to assistant message buffer. */
|
|
196
|
-
appendAssistant(sessionId: string, text: string): void {
|
|
197
|
-
const buf = (this.assistantBuffers.get(sessionId) ?? "") + text;
|
|
198
|
-
this.assistantBuffers.set(sessionId, buf);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/** Append to thinking buffer. */
|
|
202
|
-
appendThinking(sessionId: string, text: string): void {
|
|
203
|
-
const buf = (this.thinkingBuffers.get(sessionId) ?? "") + text;
|
|
204
|
-
this.thinkingBuffers.set(sessionId, buf);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/** Get CWD for a session (falls back to default). */
|
|
208
|
-
getSessionCwd(sessionId: string): string {
|
|
209
|
-
return this.store.getSession(sessionId)?.cwd ?? this.defaultCwd;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
getBusyKind(sessionId: string): "agent" | "bash" | null {
|
|
213
|
-
if (this.runningBashProcs.has(sessionId)) return "bash";
|
|
214
|
-
if (this.activePrompts.has(sessionId)) return "agent";
|
|
215
|
-
return null;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/** Kill all running bash processes (for shutdown). */
|
|
219
|
-
killAllBashProcs(): void {
|
|
220
|
-
for (const [, proc] of this.runningBashProcs) proc.kill("SIGKILL");
|
|
221
|
-
this.runningBashProcs.clear();
|
|
222
|
-
}
|
|
223
|
-
}
|
package/src/store.ts
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
import Database from "better-sqlite3";
|
|
2
|
-
import { mkdirSync } from "node:fs";
|
|
3
|
-
import { join } from "node:path";
|
|
4
|
-
|
|
5
|
-
export interface SessionRow {
|
|
6
|
-
id: string;
|
|
7
|
-
cwd: string;
|
|
8
|
-
title: string | null;
|
|
9
|
-
model: string | null;
|
|
10
|
-
mode: string | null;
|
|
11
|
-
reasoning_effort: string | null;
|
|
12
|
-
created_at: string;
|
|
13
|
-
last_active_at: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface EventRow {
|
|
17
|
-
id: number;
|
|
18
|
-
session_id: string;
|
|
19
|
-
seq: number;
|
|
20
|
-
type: string;
|
|
21
|
-
data: string; // JSON
|
|
22
|
-
created_at: string;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export class Store {
|
|
26
|
-
private db: Database.Database;
|
|
27
|
-
|
|
28
|
-
constructor(dataDir: string) {
|
|
29
|
-
mkdirSync(dataDir, { recursive: true });
|
|
30
|
-
this.db = new Database(join(dataDir, "webagent.db"));
|
|
31
|
-
this.db.pragma("journal_mode = WAL");
|
|
32
|
-
this.migrate();
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
private migrate(): void {
|
|
36
|
-
this.db.exec(`
|
|
37
|
-
CREATE TABLE IF NOT EXISTS sessions (
|
|
38
|
-
id TEXT PRIMARY KEY,
|
|
39
|
-
cwd TEXT NOT NULL,
|
|
40
|
-
title TEXT,
|
|
41
|
-
created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%d %H:%M:%f', 'now')),
|
|
42
|
-
last_active_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%d %H:%M:%f', 'now'))
|
|
43
|
-
);
|
|
44
|
-
CREATE TABLE IF NOT EXISTS events (
|
|
45
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
46
|
-
session_id TEXT NOT NULL REFERENCES sessions(id),
|
|
47
|
-
seq INTEGER NOT NULL,
|
|
48
|
-
type TEXT NOT NULL,
|
|
49
|
-
data TEXT NOT NULL DEFAULT '{}',
|
|
50
|
-
created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%d %H:%M:%f', 'now'))
|
|
51
|
-
);
|
|
52
|
-
CREATE INDEX IF NOT EXISTS idx_events_session ON events(session_id, seq);
|
|
53
|
-
`);
|
|
54
|
-
|
|
55
|
-
// Migrate existing tables: add columns if missing
|
|
56
|
-
const cols = this.db.prepare("PRAGMA table_info(sessions)").all() as Array<{ name: string }>;
|
|
57
|
-
const colNames = new Set(cols.map(c => c.name));
|
|
58
|
-
if (!colNames.has("title")) {
|
|
59
|
-
this.db.exec("ALTER TABLE sessions ADD COLUMN title TEXT");
|
|
60
|
-
}
|
|
61
|
-
if (!colNames.has("last_active_at")) {
|
|
62
|
-
this.db.exec("ALTER TABLE sessions ADD COLUMN last_active_at TEXT");
|
|
63
|
-
// Backfill from created_at
|
|
64
|
-
this.db.exec("UPDATE sessions SET last_active_at = created_at WHERE last_active_at IS NULL");
|
|
65
|
-
}
|
|
66
|
-
if (!colNames.has("model")) {
|
|
67
|
-
this.db.exec("ALTER TABLE sessions ADD COLUMN model TEXT");
|
|
68
|
-
}
|
|
69
|
-
if (!colNames.has("mode")) {
|
|
70
|
-
this.db.exec("ALTER TABLE sessions ADD COLUMN mode TEXT");
|
|
71
|
-
}
|
|
72
|
-
if (!colNames.has("reasoning_effort")) {
|
|
73
|
-
this.db.exec("ALTER TABLE sessions ADD COLUMN reasoning_effort TEXT");
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
createSession(id: string, cwd: string): SessionRow {
|
|
78
|
-
this.db.prepare("INSERT INTO sessions (id, cwd) VALUES (?, ?)").run(id, cwd);
|
|
79
|
-
return this.db.prepare("SELECT * FROM sessions WHERE id = ?").get(id) as SessionRow;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
listSessions(): SessionRow[] {
|
|
83
|
-
return this.db.prepare("SELECT * FROM sessions ORDER BY COALESCE(last_active_at, created_at) DESC").all() as SessionRow[];
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
getSession(id: string): SessionRow | undefined {
|
|
87
|
-
return this.db.prepare("SELECT * FROM sessions WHERE id = ?").get(id) as SessionRow | undefined;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
deleteSession(id: string): void {
|
|
91
|
-
this.db.prepare("DELETE FROM events WHERE session_id = ?").run(id);
|
|
92
|
-
this.db.prepare("DELETE FROM sessions WHERE id = ?").run(id);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
updateSessionTitle(id: string, title: string): void {
|
|
96
|
-
this.db.prepare("UPDATE sessions SET title = ? WHERE id = ?").run(title, id);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
updateSessionLastActive(id: string): void {
|
|
100
|
-
this.db.prepare("UPDATE sessions SET last_active_at = strftime('%Y-%m-%d %H:%M:%f', 'now') WHERE id = ?").run(id);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/** Update a config option value (model, mode, reasoning_effort) for a session. */
|
|
104
|
-
updateSessionConfig(id: string, configId: string, value: string): void {
|
|
105
|
-
const column = ({ model: "model", mode: "mode", reasoning_effort: "reasoning_effort" } as Record<string, string>)[configId];
|
|
106
|
-
if (!column) return;
|
|
107
|
-
this.db.prepare(`UPDATE sessions SET ${column} = ? WHERE id = ?`).run(value, id);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
saveEvent(sessionId: string, type: string, data: Record<string, unknown> = {}): EventRow {
|
|
111
|
-
const seq = (this.db.prepare(
|
|
112
|
-
"SELECT COALESCE(MAX(seq), 0) + 1 AS next FROM events WHERE session_id = ?"
|
|
113
|
-
).get(sessionId) as { next: number }).next;
|
|
114
|
-
|
|
115
|
-
this.db.prepare(
|
|
116
|
-
"INSERT INTO events (session_id, seq, type, data) VALUES (?, ?, ?, ?)"
|
|
117
|
-
).run(sessionId, seq, type, JSON.stringify(data));
|
|
118
|
-
|
|
119
|
-
return this.db.prepare("SELECT * FROM events WHERE session_id = ? AND seq = ?")
|
|
120
|
-
.get(sessionId, seq) as EventRow;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
getEvents(sessionId: string, opts?: { excludeThinking?: boolean; afterSeq?: number }): EventRow[] {
|
|
124
|
-
let query = "SELECT * FROM events WHERE session_id = ?";
|
|
125
|
-
const params: unknown[] = [sessionId];
|
|
126
|
-
if (opts?.afterSeq != null) {
|
|
127
|
-
query += " AND seq > ?";
|
|
128
|
-
params.push(opts.afterSeq);
|
|
129
|
-
}
|
|
130
|
-
if (opts?.excludeThinking) {
|
|
131
|
-
query += " AND type != 'thinking'";
|
|
132
|
-
}
|
|
133
|
-
query += " ORDER BY seq";
|
|
134
|
-
return this.db.prepare(query).all(...params) as EventRow[];
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
close(): void {
|
|
138
|
-
this.db.close();
|
|
139
|
-
}
|
|
140
|
-
}
|