@rayss-dev/openclaude 0.28.0-by-rayss → 0.28.1-by-rayss
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 -1
- package/config/settings-schema.json +27 -0
- package/dist/acp-web.mjs +622 -0
- package/dist/acp.mjs +490953 -0
- package/dist/cli.mjs +28 -28
- package/dist/sdk.mjs +189 -168
- package/docs/settings-reference.md +22 -1
- package/package.json +6 -1
package/README.md
CHANGED
|
@@ -319,10 +319,12 @@ This fork adds reliability, UX, and agent-loop improvements on top of upstream O
|
|
|
319
319
|
- Inline search/filter in `/model` plus a "Press / to search" hint
|
|
320
320
|
- Enables model discovery for remote custom OpenAI-compatible endpoints
|
|
321
321
|
- Model discovery loads in the background when opening `/model`
|
|
322
|
-
- `maxContextWindow`
|
|
322
|
+
- `maxContextWindow` is a true override — it wins over env vars, the catalog/discovery cache, and descriptor limits for every model (can raise as well as cap); custom-route (`routeId === 'custom'`) fallback restored
|
|
323
323
|
|
|
324
324
|
### API resilience
|
|
325
325
|
- Retries transport failures, truncated streams, and connections terminated by server or proxy
|
|
326
|
+
- Every retry drops the pooled connection and reconnects fresh — keep-alive is disabled and a new client is created so a retry never reuses the connection that just failed
|
|
327
|
+
- `apiRetry` retry count and fixed delay editable from `/config` (attempts: 3/5/10/unlimited; delay: immediate/1s/5s/10s)
|
|
326
328
|
- Discovery cache replaces stale partitions instead of piling them up
|
|
327
329
|
|
|
328
330
|
### Loop and agent behavior
|
|
@@ -2357,6 +2357,33 @@
|
|
|
2357
2357
|
"additionalProperties": false
|
|
2358
2358
|
}
|
|
2359
2359
|
},
|
|
2360
|
+
"apiRetry": {
|
|
2361
|
+
"description": "Network retry policy. Use this to tune how OpenClaude handles rate limits (429), capacity issues (529) and transient errors — including unlimited retrying with a fixed delay to ride out a persistent rate limit.",
|
|
2362
|
+
"type": "object",
|
|
2363
|
+
"properties": {
|
|
2364
|
+
"maxRetries": {
|
|
2365
|
+
"description": "How many times a retryable request is retried (rate limits like 429, capacity 529, 5xx, transient network errors). Set 0 to never retry, any positive integer, or \"unlimited\" to keep retrying (with the fixed delayMs below) until the request succeeds or you cancel. When unset, the OPENCLAUDE_MAX_RETRIES env var is used (default 10).",
|
|
2366
|
+
"anyOf": [
|
|
2367
|
+
{
|
|
2368
|
+
"type": "integer",
|
|
2369
|
+
"minimum": 0,
|
|
2370
|
+
"maximum": 9007199254740991
|
|
2371
|
+
},
|
|
2372
|
+
{
|
|
2373
|
+
"type": "string",
|
|
2374
|
+
"const": "unlimited"
|
|
2375
|
+
}
|
|
2376
|
+
]
|
|
2377
|
+
},
|
|
2378
|
+
"delayMs": {
|
|
2379
|
+
"description": "Fixed delay in milliseconds between retry attempts when this is set. Set 5000 for 5 seconds, or 0 to retry immediately. A server-provided Retry-After that is even longer is still honored. When unset, the default exponential backoff (and OPENCLAUDE_RETRY_DELAY_MS env var) apply.",
|
|
2380
|
+
"type": "integer",
|
|
2381
|
+
"minimum": 0,
|
|
2382
|
+
"maximum": 9007199254740991
|
|
2383
|
+
}
|
|
2384
|
+
},
|
|
2385
|
+
"additionalProperties": false
|
|
2386
|
+
},
|
|
2360
2387
|
"fastMode": {
|
|
2361
2388
|
"description": "When true, fast mode is enabled. When absent or false, fast mode is off.",
|
|
2362
2389
|
"type": "boolean"
|
package/dist/acp-web.mjs
ADDED
|
@@ -0,0 +1,622 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// scripts/acp-web-server.mjs
|
|
4
|
+
import { createServer } from "node:http";
|
|
5
|
+
import { spawn } from "node:child_process";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
import { dirname, join, resolve } from "node:path";
|
|
8
|
+
import { createInterface } from "node:readline";
|
|
9
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
10
|
+
import { homedir } from "node:os";
|
|
11
|
+
var __dirname2 = dirname(fileURLToPath(import.meta.url));
|
|
12
|
+
var ACP_PATH = [join(__dirname2, "acp.mjs"), join(__dirname2, "..", "dist", "acp.mjs")].find((p) => existsSync(p)) ?? join(__dirname2, "..", "dist", "acp.mjs");
|
|
13
|
+
var args = process.argv.slice(2);
|
|
14
|
+
var getArg = (name, fallback) => {
|
|
15
|
+
const i = args.indexOf(name);
|
|
16
|
+
return i >= 0 && args[i + 1] ? args[i + 1] : fallback;
|
|
17
|
+
};
|
|
18
|
+
var PORT = Number(getArg("--port", process.env.ACP_WEB_PORT || "5178"));
|
|
19
|
+
var CWD = resolve(getArg("--cwd", process.env.ACP_WEB_CWD || process.cwd()));
|
|
20
|
+
var DATA_DIR = resolve(getArg("--data-dir", process.env.ACP_WEB_DATA_DIR || join(homedir(), ".openclaude", "acp-web")));
|
|
21
|
+
var SESSIONS_FILE = join(DATA_DIR, "sessions.json");
|
|
22
|
+
var sessions = new Map;
|
|
23
|
+
var pending = new Map;
|
|
24
|
+
var sseClients = new Map;
|
|
25
|
+
var permissionRequests = new Map;
|
|
26
|
+
var nextRequestId = 1;
|
|
27
|
+
function persist() {
|
|
28
|
+
try {
|
|
29
|
+
mkdirSync(DATA_DIR, { recursive: true });
|
|
30
|
+
const data = { version: 1, sessions: {} };
|
|
31
|
+
for (const [id, s] of sessions) {
|
|
32
|
+
data.sessions[id] = {
|
|
33
|
+
id,
|
|
34
|
+
cwd: s.cwd,
|
|
35
|
+
createdAt: s.createdAt,
|
|
36
|
+
lastActive: s.lastActive,
|
|
37
|
+
messages: s.messages
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
writeFileSync(SESSIONS_FILE, JSON.stringify(data, null, 2));
|
|
41
|
+
} catch (e) {
|
|
42
|
+
console.error(" ⚠ session history not saved:", String(e));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function loadPersisted() {
|
|
46
|
+
try {
|
|
47
|
+
return JSON.parse(readFileSync(SESSIONS_FILE, "utf-8")).sessions ?? {};
|
|
48
|
+
} catch {
|
|
49
|
+
return {};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
var acp = spawn(process.execPath, [ACP_PATH], {
|
|
53
|
+
stdio: ["pipe", "pipe", "inherit"],
|
|
54
|
+
cwd: CWD
|
|
55
|
+
});
|
|
56
|
+
var acpLines = createInterface({ input: acp.stdout });
|
|
57
|
+
acpLines.on("line", (line) => {
|
|
58
|
+
let msg;
|
|
59
|
+
try {
|
|
60
|
+
msg = JSON.parse(line);
|
|
61
|
+
} catch {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (msg.id !== undefined && typeof msg.method === "string") {
|
|
65
|
+
handleServerRequest(msg);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (msg.id === undefined) {
|
|
69
|
+
handleNotification(msg);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const waiter = pending.get(msg.id);
|
|
73
|
+
if (waiter) {
|
|
74
|
+
pending.delete(msg.id);
|
|
75
|
+
if (msg.error)
|
|
76
|
+
waiter.reject(new Error(msg.error.message));
|
|
77
|
+
else
|
|
78
|
+
waiter.resolve(msg.result);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
function sendRequest(method, params) {
|
|
82
|
+
return new Promise((resolve2, reject) => {
|
|
83
|
+
const id = nextRequestId++;
|
|
84
|
+
pending.set(id, { resolve: resolve2, reject });
|
|
85
|
+
acp.stdin.write(JSON.stringify({ jsonrpc: "2.0", id, method, params }) + `
|
|
86
|
+
`);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
var initPromise = sendRequest("initialize", {}).catch((err) => {
|
|
90
|
+
console.error("ACP initialize failed:", err);
|
|
91
|
+
throw err;
|
|
92
|
+
});
|
|
93
|
+
function handleNotification(msg) {
|
|
94
|
+
const { method, params } = msg;
|
|
95
|
+
if (method !== "session/update")
|
|
96
|
+
return;
|
|
97
|
+
const sessionId = params?.sessionId;
|
|
98
|
+
const state = sessionId ? sessions.get(sessionId) : undefined;
|
|
99
|
+
const u = params.update;
|
|
100
|
+
if (!state || !u)
|
|
101
|
+
return;
|
|
102
|
+
state.lastActive = Date.now();
|
|
103
|
+
if (u.sessionUpdate === "agent_message_chunk") {
|
|
104
|
+
const text = u.content?.text;
|
|
105
|
+
if (typeof text === "string" && text)
|
|
106
|
+
state.buffer += text;
|
|
107
|
+
} else if (u.sessionUpdate === "agent_message_complete") {
|
|
108
|
+
if (state.buffer) {
|
|
109
|
+
state.messages.push({ role: "assistant", text: state.buffer, ts: Date.now() });
|
|
110
|
+
state.buffer = "";
|
|
111
|
+
persist();
|
|
112
|
+
}
|
|
113
|
+
} else if (u.sessionUpdate === "tool_call") {
|
|
114
|
+
const title = typeof u.title === "string" ? u.title : "tool";
|
|
115
|
+
const last = state.messages[state.messages.length - 1];
|
|
116
|
+
if (!last || last.role !== "tool" || last.text !== title) {
|
|
117
|
+
state.messages.push({ role: "tool", text: title, toolName: title, ts: Date.now() });
|
|
118
|
+
persist();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
broadcast(sessionId, msg);
|
|
122
|
+
}
|
|
123
|
+
function handleServerRequest(msg) {
|
|
124
|
+
const { id, method, params } = msg;
|
|
125
|
+
if (method === "session/request_permission") {
|
|
126
|
+
const sessionId = params?.sessionId;
|
|
127
|
+
if (!sessionId)
|
|
128
|
+
return;
|
|
129
|
+
permissionRequests.set(id, sessionId);
|
|
130
|
+
broadcast(sessionId, msg);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
var server = createServer((req, res) => {
|
|
134
|
+
const url = new URL(req.url, `http://${req.headers.host}`);
|
|
135
|
+
const path = url.pathname;
|
|
136
|
+
if (req.method === "GET" && path === "/")
|
|
137
|
+
return serveIndex(res);
|
|
138
|
+
if (req.method === "GET" && path === "/events")
|
|
139
|
+
return handleSse(req, res);
|
|
140
|
+
if (req.method === "GET" && path === "/api/sessions")
|
|
141
|
+
return handleSessions(res);
|
|
142
|
+
if (req.method === "GET" && path === "/api/messages")
|
|
143
|
+
return handleMessages(req, res);
|
|
144
|
+
if (req.method === "POST" && path === "/api/session")
|
|
145
|
+
return handleNewSession(res);
|
|
146
|
+
if (req.method === "POST" && path === "/api/load")
|
|
147
|
+
return handleLoad(req, res);
|
|
148
|
+
if (req.method === "POST" && path === "/api/prompt")
|
|
149
|
+
return handlePrompt(req, res);
|
|
150
|
+
if (req.method === "POST" && path === "/api/respond")
|
|
151
|
+
return handleRespond(req, res);
|
|
152
|
+
if (req.method === "POST" && path === "/api/cancel")
|
|
153
|
+
return handleCancel(req, res);
|
|
154
|
+
res.writeHead(404, { "Content-Type": "application/json" });
|
|
155
|
+
res.end(JSON.stringify({ error: "not found" }));
|
|
156
|
+
});
|
|
157
|
+
async function loadSessionInto(state, cwd) {
|
|
158
|
+
await initPromise;
|
|
159
|
+
await sendRequest("session/load", { sessionId: state.id, cwd });
|
|
160
|
+
state.loaded = true;
|
|
161
|
+
state.lastActive = Date.now();
|
|
162
|
+
}
|
|
163
|
+
function serveIndex(res) {
|
|
164
|
+
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
|
|
165
|
+
res.end(INDEX_HTML);
|
|
166
|
+
}
|
|
167
|
+
function handleSse(req, res) {
|
|
168
|
+
const url = new URL(req.url, `http://${req.headers.host}`);
|
|
169
|
+
const sessionId = url.searchParams.get("sessionId");
|
|
170
|
+
if (!sessionId || !sessions.has(sessionId)) {
|
|
171
|
+
res.writeHead(400, { "Content-Type": "application/json" });
|
|
172
|
+
res.end(JSON.stringify({ error: "unknown session" }));
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
res.writeHead(200, {
|
|
176
|
+
"Content-Type": "text/event-stream",
|
|
177
|
+
"Cache-Control": "no-cache",
|
|
178
|
+
Connection: "keep-alive"
|
|
179
|
+
});
|
|
180
|
+
res.write(`retry: 2000
|
|
181
|
+
|
|
182
|
+
`);
|
|
183
|
+
let set = sseClients.get(sessionId);
|
|
184
|
+
if (!set) {
|
|
185
|
+
set = new Set;
|
|
186
|
+
sseClients.set(sessionId, set);
|
|
187
|
+
}
|
|
188
|
+
set.add(res);
|
|
189
|
+
req.on("close", () => {
|
|
190
|
+
set.delete(res);
|
|
191
|
+
if (set.size === 0)
|
|
192
|
+
sseClients.delete(sessionId);
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
function readBody(req) {
|
|
196
|
+
return new Promise((resolve2, reject) => {
|
|
197
|
+
let data = "";
|
|
198
|
+
req.on("data", (chunk) => data += chunk);
|
|
199
|
+
req.on("end", () => {
|
|
200
|
+
try {
|
|
201
|
+
resolve2(data ? JSON.parse(data) : {});
|
|
202
|
+
} catch {
|
|
203
|
+
reject(new Error("invalid JSON body"));
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
req.on("error", reject);
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
async function handleSessions(res) {
|
|
210
|
+
const list = [...sessions.values()].map((s) => ({
|
|
211
|
+
id: s.id,
|
|
212
|
+
cwd: s.cwd,
|
|
213
|
+
createdAt: s.createdAt,
|
|
214
|
+
lastActive: s.lastActive,
|
|
215
|
+
messageCount: s.messages.length
|
|
216
|
+
})).sort((a, b) => (b.lastActive ?? 0) - (a.lastActive ?? 0));
|
|
217
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
218
|
+
res.end(JSON.stringify({ ok: true, sessions: list }));
|
|
219
|
+
}
|
|
220
|
+
async function handleMessages(req, res) {
|
|
221
|
+
const url = new URL(req.url, `http://${req.headers.host}`);
|
|
222
|
+
const sessionId = url.searchParams.get("sessionId");
|
|
223
|
+
const state = sessionId && sessions.get(sessionId);
|
|
224
|
+
if (!state) {
|
|
225
|
+
res.writeHead(400, { "Content-Type": "application/json" });
|
|
226
|
+
res.end(JSON.stringify({ error: "unknown session" }));
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
const messages = state.buffer ? [...state.messages, { role: "assistant", text: state.buffer, ts: Date.now(), streaming: true }] : state.messages;
|
|
230
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
231
|
+
res.end(JSON.stringify({ ok: true, messages }));
|
|
232
|
+
}
|
|
233
|
+
async function handleNewSession(res) {
|
|
234
|
+
try {
|
|
235
|
+
await initPromise;
|
|
236
|
+
const result = await sendRequest("session/new", { cwd: CWD });
|
|
237
|
+
const sessionId = result.sessionId;
|
|
238
|
+
sessions.set(sessionId, {
|
|
239
|
+
id: sessionId,
|
|
240
|
+
cwd: CWD,
|
|
241
|
+
createdAt: Date.now(),
|
|
242
|
+
lastActive: Date.now(),
|
|
243
|
+
messages: [],
|
|
244
|
+
buffer: "",
|
|
245
|
+
loaded: true
|
|
246
|
+
});
|
|
247
|
+
persist();
|
|
248
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
249
|
+
res.end(JSON.stringify({ ok: true, sessionId }));
|
|
250
|
+
} catch (e) {
|
|
251
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
252
|
+
res.end(JSON.stringify({ ok: false, error: String(e) }));
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
async function handleLoad(req, res) {
|
|
256
|
+
let body;
|
|
257
|
+
try {
|
|
258
|
+
body = await readBody(req);
|
|
259
|
+
} catch (e) {
|
|
260
|
+
res.writeHead(400, { "Content-Type": "application/json" });
|
|
261
|
+
res.end(JSON.stringify({ error: String(e) }));
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
const { sessionId } = body;
|
|
265
|
+
const state = sessionId && sessions.get(sessionId);
|
|
266
|
+
if (!state) {
|
|
267
|
+
res.writeHead(400, { "Content-Type": "application/json" });
|
|
268
|
+
res.end(JSON.stringify({ error: "unknown session" }));
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
try {
|
|
272
|
+
if (!state.loaded)
|
|
273
|
+
await loadSessionInto(state, state.cwd || CWD);
|
|
274
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
275
|
+
res.end(JSON.stringify({ ok: true, sessionId }));
|
|
276
|
+
} catch (e) {
|
|
277
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
278
|
+
res.end(JSON.stringify({ ok: false, error: `session/load failed: ${String(e)}` }));
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
async function handlePrompt(req, res) {
|
|
282
|
+
let body;
|
|
283
|
+
try {
|
|
284
|
+
body = await readBody(req);
|
|
285
|
+
} catch (e) {
|
|
286
|
+
res.writeHead(400, { "Content-Type": "application/json" });
|
|
287
|
+
res.end(JSON.stringify({ error: String(e) }));
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
const { sessionId, prompt } = body;
|
|
291
|
+
const state = sessionId && sessions.get(sessionId);
|
|
292
|
+
if (!state) {
|
|
293
|
+
res.writeHead(400, { "Content-Type": "application/json" });
|
|
294
|
+
res.end(JSON.stringify({ error: "unknown session" }));
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
if (typeof prompt !== "string" || !prompt.trim()) {
|
|
298
|
+
res.writeHead(400, { "Content-Type": "application/json" });
|
|
299
|
+
res.end(JSON.stringify({ error: "prompt is required" }));
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
try {
|
|
303
|
+
if (!state.loaded)
|
|
304
|
+
await loadSessionInto(state, state.cwd || CWD);
|
|
305
|
+
} catch (e) {
|
|
306
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
307
|
+
res.end(JSON.stringify({ ok: false, error: `session/load failed: ${String(e)}` }));
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
311
|
+
res.end(JSON.stringify({ ok: true }));
|
|
312
|
+
state.messages.push({ role: "user", text: prompt.trim(), ts: Date.now() });
|
|
313
|
+
state.buffer = "";
|
|
314
|
+
state.lastActive = Date.now();
|
|
315
|
+
persist();
|
|
316
|
+
sendRequest("session/prompt", { sessionId, prompt }).catch(() => {});
|
|
317
|
+
}
|
|
318
|
+
async function handleRespond(req, res) {
|
|
319
|
+
let body;
|
|
320
|
+
try {
|
|
321
|
+
body = await readBody(req);
|
|
322
|
+
} catch (e) {
|
|
323
|
+
res.writeHead(400, { "Content-Type": "application/json" });
|
|
324
|
+
res.end(JSON.stringify({ error: String(e) }));
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
const { sessionId, requestId, optionId } = body;
|
|
328
|
+
if (!sessionId || !sessions.has(sessionId)) {
|
|
329
|
+
res.writeHead(400, { "Content-Type": "application/json" });
|
|
330
|
+
res.end(JSON.stringify({ error: "unknown session" }));
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
if (!permissionRequests.has(requestId)) {
|
|
334
|
+
res.writeHead(400, { "Content-Type": "application/json" });
|
|
335
|
+
res.end(JSON.stringify({ error: "unknown permission request" }));
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
339
|
+
res.end(JSON.stringify({ ok: true }));
|
|
340
|
+
acp.stdin.write(JSON.stringify({ jsonrpc: "2.0", id: requestId, params: { optionId } }) + `
|
|
341
|
+
`);
|
|
342
|
+
permissionRequests.delete(requestId);
|
|
343
|
+
}
|
|
344
|
+
async function handleCancel(req, res) {
|
|
345
|
+
let body;
|
|
346
|
+
try {
|
|
347
|
+
body = await readBody(req);
|
|
348
|
+
} catch (e) {
|
|
349
|
+
res.writeHead(400, { "Content-Type": "application/json" });
|
|
350
|
+
res.end(JSON.stringify({ error: String(e) }));
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
const { sessionId } = body;
|
|
354
|
+
if (!sessionId || !sessions.has(sessionId)) {
|
|
355
|
+
res.writeHead(400, { "Content-Type": "application/json" });
|
|
356
|
+
res.end(JSON.stringify({ error: "unknown session" }));
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
sendRequest("session/cancel", { sessionId }).catch(() => {});
|
|
360
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
361
|
+
res.end(JSON.stringify({ ok: true }));
|
|
362
|
+
}
|
|
363
|
+
function broadcast(sessionId, msg) {
|
|
364
|
+
const set = sseClients.get(sessionId);
|
|
365
|
+
if (!set)
|
|
366
|
+
return;
|
|
367
|
+
const payload = `data: ${JSON.stringify(msg)}
|
|
368
|
+
|
|
369
|
+
`;
|
|
370
|
+
for (const res of set) {
|
|
371
|
+
res.write(payload);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
for (const [id, rec] of Object.entries(loadPersisted())) {
|
|
375
|
+
sessions.set(id, {
|
|
376
|
+
id,
|
|
377
|
+
cwd: rec.cwd || CWD,
|
|
378
|
+
createdAt: rec.createdAt || Date.now(),
|
|
379
|
+
lastActive: rec.lastActive || Date.now(),
|
|
380
|
+
messages: Array.isArray(rec.messages) ? rec.messages : [],
|
|
381
|
+
buffer: "",
|
|
382
|
+
loaded: false
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
server.listen(PORT, () => {
|
|
386
|
+
console.log(`ACP Web Client running at http://localhost:${PORT}`);
|
|
387
|
+
console.log(` ACP process : ${ACP_PATH}`);
|
|
388
|
+
console.log(` Working dir : ${CWD}`);
|
|
389
|
+
console.log(` History file: ${SESSIONS_FILE}`);
|
|
390
|
+
});
|
|
391
|
+
process.on("exit", () => acp.kill());
|
|
392
|
+
process.on("SIGINT", () => {
|
|
393
|
+
acp.kill();
|
|
394
|
+
process.exit(0);
|
|
395
|
+
});
|
|
396
|
+
var INDEX_HTML = `<!doctype html>
|
|
397
|
+
<html lang="en">
|
|
398
|
+
<head>
|
|
399
|
+
<meta charset="utf-8">
|
|
400
|
+
<title>OpenClaude — ACP Web Chat</title>
|
|
401
|
+
<style>
|
|
402
|
+
:root { --bg:#0f1115; --panel:#171a21; --border:#262b36; --text:#e6e8ee; --muted:#8b93a7; --accent:#4f8cff; }
|
|
403
|
+
* { box-sizing:border-box; }
|
|
404
|
+
body { margin:0; font-family:system-ui,-apple-system,Segoe UI,Roboto,sans-serif; background:var(--bg); color:var(--text); height:100vh; display:flex; }
|
|
405
|
+
#sidebar { width:260px; border-right:1px solid var(--border); padding:16px; display:flex; flex-direction:column; gap:12px; background:var(--panel); }
|
|
406
|
+
#sidebar h1 { font-size:16px; margin:0; }
|
|
407
|
+
#sidebar p { font-size:12px; color:var(--muted); margin:0; line-height:1.5; }
|
|
408
|
+
#new-session { background:var(--accent); color:#fff; border:0; border-radius:6px; padding:10px; cursor:pointer; font-weight:600; }
|
|
409
|
+
#new-session:disabled { opacity:.5; cursor:default; }
|
|
410
|
+
#session-meta { font-size:12px; color:var(--muted); word-break:break-all; }
|
|
411
|
+
#session-list { flex:1; overflow-y:auto; display:flex; flex-direction:column; gap:6px; }
|
|
412
|
+
#session-list .head { font-size:11px; text-transform:uppercase; letter-spacing:.08em; color:var(--muted); margin:4px 0 0; }
|
|
413
|
+
.sess-item { background:#12151c; border:1px solid var(--border); border-radius:8px; padding:8px 10px; cursor:pointer; font-size:12px; line-height:1.4; }
|
|
414
|
+
.sess-item:hover { border-color:var(--accent); }
|
|
415
|
+
.sess-item.active { border-color:var(--accent); background:#14233f; }
|
|
416
|
+
.sess-item .time { font-weight:600; color:var(--text); }
|
|
417
|
+
.sess-item .count { color:var(--muted); }
|
|
418
|
+
.sess-item .cwd { color:var(--muted); font-size:11px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
|
|
419
|
+
#main { flex:1; display:flex; flex-direction:column; min-width:0; }
|
|
420
|
+
#messages { flex:1; overflow-y:auto; padding:20px; display:flex; flex-direction:column; gap:10px; }
|
|
421
|
+
.msg { max-width:70%; padding:10px 14px; border-radius:10px; line-height:1.5; white-space:pre-wrap; word-break:break-word; font-size:14px; }
|
|
422
|
+
.user { align-self:flex-end; background:var(--accent); color:#fff; }
|
|
423
|
+
.assistant { align-self:flex-start; background:var(--panel); border:1px solid var(--border); }
|
|
424
|
+
.tool { align-self:flex-start; background:#2a2f3a; border-left:3px solid #e2a03f; font-family:ui-monospace,monospace; font-size:12px; color:#d8dbe2; }
|
|
425
|
+
.perm { align-self:flex-start; background:#2a2130; border:1px solid #6b4f8a; padding:10px 14px; border-radius:10px; max-width:80%; font-size:13px; }
|
|
426
|
+
.perm button { margin:6px 6px 0 0; padding:6px 14px; border-radius:6px; border:0; cursor:pointer; font-weight:600; }
|
|
427
|
+
.perm .allow { background:#2e8b57; color:#fff; }
|
|
428
|
+
.perm .reject { background:#b33a3a; color:#fff; }
|
|
429
|
+
.status { align-self:center; font-size:12px; color:var(--muted); }
|
|
430
|
+
#inputbar { border-top:1px solid var(--border); padding:12px; display:flex; gap:8px; background:var(--panel); }
|
|
431
|
+
#prompt { flex:1; background:#0f1115; color:var(--text); border:1px solid var(--border); border-radius:8px; padding:10px 12px; font-size:14px; resize:none; }
|
|
432
|
+
#send { background:var(--accent); color:#fff; border:0; border-radius:8px; padding:10px 18px; cursor:pointer; font-weight:600; }
|
|
433
|
+
#send:disabled { opacity:.5; cursor:default; }
|
|
434
|
+
</style>
|
|
435
|
+
</head>
|
|
436
|
+
<body>
|
|
437
|
+
<div id="sidebar">
|
|
438
|
+
<h1>OpenClaude</h1>
|
|
439
|
+
<p>Chat via <strong>ACP</strong> over stdio. Riwayat sesi tersimpan lokal di <code>~/.openclaude/acp-web</code>.</p>
|
|
440
|
+
<button id="new-session">New session</button>
|
|
441
|
+
<div id="session-meta">No session yet.</div>
|
|
442
|
+
<div id="session-list"><div class="head">Riwayat sesi</div></div>
|
|
443
|
+
</div>
|
|
444
|
+
<div id="main">
|
|
445
|
+
<div id="messages"></div>
|
|
446
|
+
<div id="inputbar">
|
|
447
|
+
<textarea id="prompt" rows="1" placeholder="Ask OpenClaude… (Enter to send, Shift+Enter for newline)" disabled></textarea>
|
|
448
|
+
<button id="send" disabled>Send</button>
|
|
449
|
+
</div>
|
|
450
|
+
</div>
|
|
451
|
+
<script>
|
|
452
|
+
const el = {
|
|
453
|
+
messages: document.getElementById('messages'),
|
|
454
|
+
prompt: document.getElementById('prompt'),
|
|
455
|
+
send: document.getElementById('send'),
|
|
456
|
+
newSession: document.getElementById('new-session'),
|
|
457
|
+
meta: document.getElementById('session-meta'),
|
|
458
|
+
sessionList: document.getElementById('session-list'),
|
|
459
|
+
}
|
|
460
|
+
let sessionId = null
|
|
461
|
+
let es = null
|
|
462
|
+
let streaming = false
|
|
463
|
+
|
|
464
|
+
function addMsg(html, cls) {
|
|
465
|
+
const div = document.createElement('div')
|
|
466
|
+
div.className = 'msg ' + cls
|
|
467
|
+
div.innerHTML = html
|
|
468
|
+
el.messages.appendChild(div)
|
|
469
|
+
el.messages.scrollTop = el.messages.scrollHeight
|
|
470
|
+
return div
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
function addText(role, text) {
|
|
474
|
+
return addMsg(escapeHtml(String(text)).replace(/\\n/g, '<br>'), role)
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
function escapeHtml(s) {
|
|
478
|
+
return String(s).replace(/[&<>"']/g, c => ({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]))
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
function fmtTime(ts) {
|
|
482
|
+
const d = new Date(ts)
|
|
483
|
+
const p = n => String(n).padStart(2, '0')
|
|
484
|
+
return p(d.getHours()) + ':' + p(d.getMinutes()) + ' ' + p(d.getDate()) + '/' + p(d.getMonth() + 1)
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
function shortId(id) {
|
|
488
|
+
return id ? id.slice(0, 8) + '…' : ''
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
async function post(path, body) {
|
|
492
|
+
const res = await fetch(path, { method: 'POST', headers: {'Content-Type':'application/json'}, body: JSON.stringify(body) })
|
|
493
|
+
return res.json()
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
function setUiEnabled(on) {
|
|
497
|
+
el.prompt.disabled = !on
|
|
498
|
+
el.send.disabled = !on
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
function renderSessionList() {
|
|
502
|
+
fetch('/api/sessions').then(r => r.json()).then(r => {
|
|
503
|
+
const keep = el.sessionList.querySelector('.head')
|
|
504
|
+
el.sessionList.innerHTML = ''
|
|
505
|
+
el.sessionList.appendChild(keep)
|
|
506
|
+
for (const s of r.sessions || []) {
|
|
507
|
+
const item = document.createElement('div')
|
|
508
|
+
item.className = 'sess-item' + (s.id === sessionId ? ' active' : '')
|
|
509
|
+
item.innerHTML =
|
|
510
|
+
'<div class="time">' + fmtTime(s.lastActive || s.createdAt) + '</div>' +
|
|
511
|
+
'<div class="count">' + s.messageCount + ' pesan</div>' +
|
|
512
|
+
'<div class="cwd">' + escapeHtml(s.cwd || '') + '</div>'
|
|
513
|
+
item.addEventListener('click', () => loadSession(s.id))
|
|
514
|
+
el.sessionList.appendChild(item)
|
|
515
|
+
}
|
|
516
|
+
}).catch(() => {})
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
async function renderHistory() {
|
|
520
|
+
const r = await fetch('/api/messages?sessionId=' + encodeURIComponent(sessionId)).then(r => r.json())
|
|
521
|
+
el.messages.innerHTML = ''
|
|
522
|
+
streaming = false
|
|
523
|
+
for (const m of r.messages || []) {
|
|
524
|
+
if (m.role === 'tool') addMsg('<b>\uD83D\uDD27 ' + escapeHtml(m.text) + '</b>', 'tool')
|
|
525
|
+
else addText(m.role === 'user' ? 'user' : 'assistant', m.text)
|
|
526
|
+
}
|
|
527
|
+
if ((r.messages || []).some(m => m.streaming)) streaming = true
|
|
528
|
+
el.messages.scrollTop = el.messages.scrollHeight
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
async function loadSession(id) {
|
|
532
|
+
const r = await post('/api/load', { sessionId: id })
|
|
533
|
+
if (!r.ok) { alert('Gagal load sesi: ' + r.error); return }
|
|
534
|
+
sessionId = id
|
|
535
|
+
localStorage.setItem('openclaude-acp-session', id)
|
|
536
|
+
el.meta.textContent = 'Session: ' + shortId(id)
|
|
537
|
+
setUiEnabled(true)
|
|
538
|
+
await renderHistory()
|
|
539
|
+
connectEvents()
|
|
540
|
+
renderSessionList()
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
el.newSession.addEventListener('click', async () => {
|
|
544
|
+
el.newSession.disabled = true
|
|
545
|
+
const r = await post('/api/session', {})
|
|
546
|
+
if (!r.ok) { alert('Failed: ' + r.error); el.newSession.disabled = false; return }
|
|
547
|
+
sessionId = r.sessionId
|
|
548
|
+
localStorage.setItem('openclaude-acp-session', sessionId)
|
|
549
|
+
el.meta.textContent = 'Session: ' + shortId(sessionId)
|
|
550
|
+
setUiEnabled(true)
|
|
551
|
+
el.messages.innerHTML = ''
|
|
552
|
+
el.newSession.disabled = false
|
|
553
|
+
connectEvents()
|
|
554
|
+
renderSessionList()
|
|
555
|
+
})
|
|
556
|
+
|
|
557
|
+
function connectEvents() {
|
|
558
|
+
if (es) es.close()
|
|
559
|
+
es = new EventSource('/events?sessionId=' + encodeURIComponent(sessionId))
|
|
560
|
+
es.onmessage = (e) => {
|
|
561
|
+
let msg
|
|
562
|
+
try { msg = JSON.parse(e.data) } catch { return }
|
|
563
|
+
if (msg.method === 'session/update') {
|
|
564
|
+
const u = msg.params.update
|
|
565
|
+
if (u && u.sessionUpdate === 'agent_message_chunk') {
|
|
566
|
+
const text = u.content && u.content.text
|
|
567
|
+
if (text) {
|
|
568
|
+
if (!streaming) { addText('assistant', ''); streaming = true }
|
|
569
|
+
const last = el.messages.lastElementChild
|
|
570
|
+
last.innerHTML = escapeHtml(last.textContent + text).replace(/\\n/g, '<br>')
|
|
571
|
+
el.messages.scrollTop = el.messages.scrollHeight
|
|
572
|
+
}
|
|
573
|
+
} else if (u && u.sessionUpdate === 'tool_call') {
|
|
574
|
+
const id = u.content && u.content.toolCallId
|
|
575
|
+
const name = (u.content && u.content.title) || 'tool'
|
|
576
|
+
addMsg('<b>\uD83D\uDD27 ' + escapeHtml(name) + '</b> <span style="color:var(--muted)">(' + escapeHtml(id || '') + ')</span>', 'tool')
|
|
577
|
+
renderSessionList()
|
|
578
|
+
} else if (u && u.sessionUpdate === 'agent_message_complete') {
|
|
579
|
+
streaming = false
|
|
580
|
+
renderSessionList()
|
|
581
|
+
}
|
|
582
|
+
} else if (msg.method === 'session/request_permission') {
|
|
583
|
+
const p = msg.params
|
|
584
|
+
const opt = (p.options || []).map(o =>
|
|
585
|
+
'<button class="' + (o.optionId === 'allow' ? 'allow' : 'reject') + '" data-rid="' + escapeHtml(msg.id) + '" data-opt="' + escapeHtml(o.optionId) + '">' + escapeHtml(o.name) + '</button>'
|
|
586
|
+
).join('')
|
|
587
|
+
const title = p.toolCall && p.toolCall.title ? p.toolCall.title : 'tool'
|
|
588
|
+
addMsg('<b>\uD83D\uDD10 Permission: ' + escapeHtml(title) + '</b><br>' + opt, 'perm')
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
el.messages.addEventListener('click', async (e) => {
|
|
594
|
+
const btn = e.target.closest('button[data-rid]')
|
|
595
|
+
if (!btn) return
|
|
596
|
+
await post('/api/respond', { sessionId, requestId: btn.dataset.rid, optionId: btn.dataset.opt })
|
|
597
|
+
btn.closest('.perm').innerHTML = 'Decision: ' + btn.dataset.opt
|
|
598
|
+
})
|
|
599
|
+
|
|
600
|
+
function send() {
|
|
601
|
+
const text = el.prompt.value.trim()
|
|
602
|
+
if (!text || !sessionId || el.send.disabled) return
|
|
603
|
+
el.prompt.value = ''
|
|
604
|
+
addText('user', text)
|
|
605
|
+
streaming = false
|
|
606
|
+
post('/api/prompt', { sessionId, prompt: text })
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
el.send.addEventListener('click', send)
|
|
610
|
+
el.prompt.addEventListener('keydown', e => {
|
|
611
|
+
if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); send() }
|
|
612
|
+
})
|
|
613
|
+
|
|
614
|
+
// Boot: list history; auto-open the last session used in this browser.
|
|
615
|
+
renderSessionList()
|
|
616
|
+
const last = localStorage.getItem('openclaude-acp-session')
|
|
617
|
+
if (last) loadSession(last)
|
|
618
|
+
</script>
|
|
619
|
+
</body>
|
|
620
|
+
</html>`;
|
|
621
|
+
|
|
622
|
+
//# debugId=820F0ADD0DD98DE064756E2164756E21
|