dorkos 0.1.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/LICENSE +21 -0
- package/README.md +78 -0
- package/dist/bin/cli.js +82 -0
- package/dist/client/.gitkeep +0 -0
- package/dist/client/assets/code-block-37QAKDTI-lJVmOB7p.js +2 -0
- package/dist/client/assets/confetti.module-5PKC4vm3.js +2 -0
- package/dist/client/assets/index-CkhDDYrb.js +476 -0
- package/dist/client/assets/index-yeRKxnbi.css +1 -0
- package/dist/client/favicon.png +0 -0
- package/dist/client/index.html +23 -0
- package/dist/client/notification.wav +0 -0
- package/dist/server/index.js +4341 -0
- package/dist/server/index.js.map +7 -0
- package/package.json +58 -0
|
@@ -0,0 +1,4341 @@
|
|
|
1
|
+
import { createRequire } from 'module'; const require = createRequire(import.meta.url);
|
|
2
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
3
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
4
|
+
}) : x)(function(x) {
|
|
5
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
6
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
// ../../apps/server/src/index.ts
|
|
10
|
+
import dotenv from "dotenv";
|
|
11
|
+
import path9 from "path";
|
|
12
|
+
import { fileURLToPath as fileURLToPath5 } from "url";
|
|
13
|
+
|
|
14
|
+
// ../../apps/server/src/app.ts
|
|
15
|
+
import express from "express";
|
|
16
|
+
import cors from "cors";
|
|
17
|
+
import path8 from "path";
|
|
18
|
+
import { fileURLToPath as fileURLToPath4 } from "url";
|
|
19
|
+
import { apiReference } from "@scalar/express-api-reference";
|
|
20
|
+
|
|
21
|
+
// ../../apps/server/src/routes/sessions.ts
|
|
22
|
+
import path3 from "path";
|
|
23
|
+
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
24
|
+
import { Router } from "express";
|
|
25
|
+
|
|
26
|
+
// ../../apps/server/src/services/agent-manager.ts
|
|
27
|
+
import path from "path";
|
|
28
|
+
import { fileURLToPath } from "url";
|
|
29
|
+
import { execFileSync } from "child_process";
|
|
30
|
+
import { existsSync } from "fs";
|
|
31
|
+
import { query } from "@anthropic-ai/claude-agent-sdk";
|
|
32
|
+
var __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
33
|
+
var INTERACTION_TIMEOUT_MS = 10 * 60 * 1e3;
|
|
34
|
+
function resolveClaudeCliPath() {
|
|
35
|
+
try {
|
|
36
|
+
const sdkCli = __require.resolve("@anthropic-ai/claude-agent-sdk/cli.js");
|
|
37
|
+
if (existsSync(sdkCli)) return sdkCli;
|
|
38
|
+
} catch {
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
const bin = execFileSync("which", ["claude"], { encoding: "utf-8" }).trim();
|
|
42
|
+
if (bin && existsSync(bin)) return bin;
|
|
43
|
+
} catch {
|
|
44
|
+
}
|
|
45
|
+
return void 0;
|
|
46
|
+
}
|
|
47
|
+
function handleAskUserQuestion(session, toolUseId, input) {
|
|
48
|
+
session.eventQueue.push({
|
|
49
|
+
type: "question_prompt",
|
|
50
|
+
data: {
|
|
51
|
+
toolCallId: toolUseId,
|
|
52
|
+
questions: input.questions
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
session.eventQueueNotify?.();
|
|
56
|
+
return new Promise((resolve3) => {
|
|
57
|
+
const timeout = setTimeout(() => {
|
|
58
|
+
session.pendingInteractions.delete(toolUseId);
|
|
59
|
+
resolve3({ behavior: "deny", message: "User did not respond within 10 minutes" });
|
|
60
|
+
}, INTERACTION_TIMEOUT_MS);
|
|
61
|
+
session.pendingInteractions.set(toolUseId, {
|
|
62
|
+
type: "question",
|
|
63
|
+
toolCallId: toolUseId,
|
|
64
|
+
resolve: (answers) => {
|
|
65
|
+
clearTimeout(timeout);
|
|
66
|
+
session.pendingInteractions.delete(toolUseId);
|
|
67
|
+
resolve3({
|
|
68
|
+
behavior: "allow",
|
|
69
|
+
updatedInput: { ...input, answers }
|
|
70
|
+
});
|
|
71
|
+
},
|
|
72
|
+
reject: () => {
|
|
73
|
+
clearTimeout(timeout);
|
|
74
|
+
session.pendingInteractions.delete(toolUseId);
|
|
75
|
+
resolve3({ behavior: "deny", message: "Interaction cancelled" });
|
|
76
|
+
},
|
|
77
|
+
timeout
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
function handleToolApproval(session, toolUseId, toolName, input) {
|
|
82
|
+
session.eventQueue.push({
|
|
83
|
+
type: "approval_required",
|
|
84
|
+
data: {
|
|
85
|
+
toolCallId: toolUseId,
|
|
86
|
+
toolName,
|
|
87
|
+
input: JSON.stringify(input)
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
session.eventQueueNotify?.();
|
|
91
|
+
return new Promise((resolve3) => {
|
|
92
|
+
const timeout = setTimeout(() => {
|
|
93
|
+
session.pendingInteractions.delete(toolUseId);
|
|
94
|
+
resolve3({ behavior: "deny", message: "Tool approval timed out after 10 minutes" });
|
|
95
|
+
}, INTERACTION_TIMEOUT_MS);
|
|
96
|
+
session.pendingInteractions.set(toolUseId, {
|
|
97
|
+
type: "approval",
|
|
98
|
+
toolCallId: toolUseId,
|
|
99
|
+
resolve: (approved) => {
|
|
100
|
+
clearTimeout(timeout);
|
|
101
|
+
session.pendingInteractions.delete(toolUseId);
|
|
102
|
+
resolve3(
|
|
103
|
+
approved ? { behavior: "allow", updatedInput: input } : { behavior: "deny", message: "User denied tool execution" }
|
|
104
|
+
);
|
|
105
|
+
},
|
|
106
|
+
reject: () => {
|
|
107
|
+
clearTimeout(timeout);
|
|
108
|
+
session.pendingInteractions.delete(toolUseId);
|
|
109
|
+
resolve3({ behavior: "deny", message: "Interaction cancelled" });
|
|
110
|
+
},
|
|
111
|
+
timeout
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
var TASK_TOOL_NAMES = /* @__PURE__ */ new Set(["TaskCreate", "TaskUpdate", "TaskList", "TaskGet"]);
|
|
116
|
+
function buildTaskEvent(toolName, input) {
|
|
117
|
+
switch (toolName) {
|
|
118
|
+
case "TaskCreate":
|
|
119
|
+
return {
|
|
120
|
+
action: "create",
|
|
121
|
+
task: {
|
|
122
|
+
id: "",
|
|
123
|
+
subject: input.subject ?? "",
|
|
124
|
+
description: input.description,
|
|
125
|
+
activeForm: input.activeForm,
|
|
126
|
+
status: "pending"
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
case "TaskUpdate": {
|
|
130
|
+
const task = {
|
|
131
|
+
id: input.taskId ?? "",
|
|
132
|
+
subject: input.subject ?? "",
|
|
133
|
+
status: input.status ?? ""
|
|
134
|
+
};
|
|
135
|
+
if (input.activeForm) task.activeForm = input.activeForm;
|
|
136
|
+
if (input.description) task.description = input.description;
|
|
137
|
+
if (input.addBlockedBy) task.blockedBy = input.addBlockedBy;
|
|
138
|
+
if (input.addBlocks) task.blocks = input.addBlocks;
|
|
139
|
+
if (input.owner) task.owner = input.owner;
|
|
140
|
+
return { action: "update", task };
|
|
141
|
+
}
|
|
142
|
+
default:
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
var AgentManager = class {
|
|
147
|
+
sessions = /* @__PURE__ */ new Map();
|
|
148
|
+
sessionLocks = /* @__PURE__ */ new Map();
|
|
149
|
+
SESSION_TIMEOUT_MS = 30 * 60 * 1e3;
|
|
150
|
+
// 30 minutes
|
|
151
|
+
LOCK_TTL_MS = 5 * 60 * 1e3;
|
|
152
|
+
// 5 minutes
|
|
153
|
+
cwd;
|
|
154
|
+
claudeCliPath;
|
|
155
|
+
constructor(cwd) {
|
|
156
|
+
this.cwd = cwd ?? process.env.GATEWAY_CWD ?? path.resolve(__dirname, "../../../../");
|
|
157
|
+
this.claudeCliPath = resolveClaudeCliPath();
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Start or resume an agent session.
|
|
161
|
+
* For new sessions, sdkSessionId is assigned after the first query() init message.
|
|
162
|
+
* For resumed sessions, the sessionId IS the sdkSessionId.
|
|
163
|
+
*/
|
|
164
|
+
ensureSession(sessionId, opts) {
|
|
165
|
+
if (!this.sessions.has(sessionId)) {
|
|
166
|
+
this.sessions.set(sessionId, {
|
|
167
|
+
sdkSessionId: sessionId,
|
|
168
|
+
lastActivity: Date.now(),
|
|
169
|
+
permissionMode: opts.permissionMode,
|
|
170
|
+
cwd: opts.cwd,
|
|
171
|
+
hasStarted: opts.hasStarted ?? false,
|
|
172
|
+
pendingInteractions: /* @__PURE__ */ new Map(),
|
|
173
|
+
eventQueue: []
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
async *sendMessage(sessionId, content, opts) {
|
|
178
|
+
if (!this.sessions.has(sessionId)) {
|
|
179
|
+
this.ensureSession(sessionId, {
|
|
180
|
+
permissionMode: opts?.permissionMode ?? "default",
|
|
181
|
+
cwd: opts?.cwd,
|
|
182
|
+
hasStarted: true
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
const session = this.sessions.get(sessionId);
|
|
186
|
+
session.lastActivity = Date.now();
|
|
187
|
+
session.eventQueue = [];
|
|
188
|
+
const sdkOptions = {
|
|
189
|
+
cwd: session.cwd ?? this.cwd,
|
|
190
|
+
includePartialMessages: true,
|
|
191
|
+
settingSources: ["project", "user"],
|
|
192
|
+
...this.claudeCliPath ? { pathToClaudeCodeExecutable: this.claudeCliPath } : {}
|
|
193
|
+
};
|
|
194
|
+
if (session.hasStarted) {
|
|
195
|
+
sdkOptions.resume = session.sdkSessionId;
|
|
196
|
+
}
|
|
197
|
+
console.log(`[sendMessage] session=${sessionId} permissionMode=${session.permissionMode} hasStarted=${session.hasStarted} resume=${session.hasStarted ? session.sdkSessionId : "N/A"}`);
|
|
198
|
+
switch (session.permissionMode) {
|
|
199
|
+
case "bypassPermissions":
|
|
200
|
+
sdkOptions.permissionMode = "bypassPermissions";
|
|
201
|
+
sdkOptions.allowDangerouslySkipPermissions = true;
|
|
202
|
+
break;
|
|
203
|
+
case "plan":
|
|
204
|
+
sdkOptions.permissionMode = "plan";
|
|
205
|
+
break;
|
|
206
|
+
case "acceptEdits":
|
|
207
|
+
sdkOptions.permissionMode = "acceptEdits";
|
|
208
|
+
break;
|
|
209
|
+
default:
|
|
210
|
+
sdkOptions.permissionMode = "default";
|
|
211
|
+
}
|
|
212
|
+
if (session.model) {
|
|
213
|
+
sdkOptions.model = session.model;
|
|
214
|
+
}
|
|
215
|
+
sdkOptions.canUseTool = async (toolName, input, context) => {
|
|
216
|
+
if (toolName === "AskUserQuestion") {
|
|
217
|
+
console.log(`[canUseTool] ${toolName} \u2192 routing to question handler (toolUseID=${context.toolUseID})`);
|
|
218
|
+
return handleAskUserQuestion(session, context.toolUseID, input);
|
|
219
|
+
}
|
|
220
|
+
if (session.permissionMode === "default") {
|
|
221
|
+
console.log(`[canUseTool] ${toolName} \u2192 requesting approval (permissionMode=default, toolUseID=${context.toolUseID})`);
|
|
222
|
+
return handleToolApproval(session, context.toolUseID, toolName, input);
|
|
223
|
+
}
|
|
224
|
+
console.log(`[canUseTool] ${toolName} \u2192 auto-allow (permissionMode=${session.permissionMode}, toolUseID=${context.toolUseID})`);
|
|
225
|
+
return { behavior: "allow", updatedInput: input };
|
|
226
|
+
};
|
|
227
|
+
const agentQuery = query({ prompt: content, options: sdkOptions });
|
|
228
|
+
session.activeQuery = agentQuery;
|
|
229
|
+
let inTool = false;
|
|
230
|
+
let currentToolName = "";
|
|
231
|
+
let currentToolId = "";
|
|
232
|
+
let emittedDone = false;
|
|
233
|
+
let taskToolInput = "";
|
|
234
|
+
const toolState = {
|
|
235
|
+
get inTool() {
|
|
236
|
+
return inTool;
|
|
237
|
+
},
|
|
238
|
+
get currentToolName() {
|
|
239
|
+
return currentToolName;
|
|
240
|
+
},
|
|
241
|
+
get currentToolId() {
|
|
242
|
+
return currentToolId;
|
|
243
|
+
},
|
|
244
|
+
get taskToolInput() {
|
|
245
|
+
return taskToolInput;
|
|
246
|
+
},
|
|
247
|
+
appendTaskInput: (chunk) => {
|
|
248
|
+
taskToolInput += chunk;
|
|
249
|
+
},
|
|
250
|
+
resetTaskInput: () => {
|
|
251
|
+
taskToolInput = "";
|
|
252
|
+
},
|
|
253
|
+
setToolState: (tool, name, id) => {
|
|
254
|
+
inTool = tool;
|
|
255
|
+
currentToolName = name;
|
|
256
|
+
currentToolId = id;
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
try {
|
|
260
|
+
const sdkIterator = agentQuery[Symbol.asyncIterator]();
|
|
261
|
+
let pendingSdkPromise = null;
|
|
262
|
+
while (true) {
|
|
263
|
+
while (session.eventQueue.length > 0) {
|
|
264
|
+
const queuedEvent = session.eventQueue.shift();
|
|
265
|
+
if (queuedEvent.type === "done") emittedDone = true;
|
|
266
|
+
yield queuedEvent;
|
|
267
|
+
}
|
|
268
|
+
const queuePromise = new Promise((resolve3) => {
|
|
269
|
+
session.eventQueueNotify = () => resolve3("queue");
|
|
270
|
+
});
|
|
271
|
+
if (!pendingSdkPromise) {
|
|
272
|
+
pendingSdkPromise = sdkIterator.next().then((result2) => ({ sdk: true, result: result2 }));
|
|
273
|
+
}
|
|
274
|
+
const winner = await Promise.race([queuePromise, pendingSdkPromise]);
|
|
275
|
+
if (winner === "queue") {
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
pendingSdkPromise = null;
|
|
279
|
+
const { result } = winner;
|
|
280
|
+
if (result.done) break;
|
|
281
|
+
for await (const event of this.mapSdkMessage(result.value, session, sessionId, toolState)) {
|
|
282
|
+
if (event.type === "done") emittedDone = true;
|
|
283
|
+
yield event;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
} catch (err) {
|
|
287
|
+
yield {
|
|
288
|
+
type: "error",
|
|
289
|
+
data: {
|
|
290
|
+
message: err instanceof Error ? err.message : "SDK error"
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
} finally {
|
|
294
|
+
session.activeQuery = void 0;
|
|
295
|
+
}
|
|
296
|
+
if (!emittedDone) {
|
|
297
|
+
yield {
|
|
298
|
+
type: "done",
|
|
299
|
+
data: { sessionId }
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
async *mapSdkMessage(message, session, sessionId, toolState) {
|
|
304
|
+
if (message.type === "system" && "subtype" in message && message.subtype === "init") {
|
|
305
|
+
session.sdkSessionId = message.session_id;
|
|
306
|
+
session.hasStarted = true;
|
|
307
|
+
const initModel = message.model;
|
|
308
|
+
if (initModel) {
|
|
309
|
+
yield {
|
|
310
|
+
type: "session_status",
|
|
311
|
+
data: {
|
|
312
|
+
sessionId,
|
|
313
|
+
model: initModel
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
if (message.type === "stream_event") {
|
|
320
|
+
const event = message.event;
|
|
321
|
+
const eventType = event.type;
|
|
322
|
+
if (eventType === "content_block_start") {
|
|
323
|
+
const contentBlock = event.content_block;
|
|
324
|
+
if (contentBlock?.type === "tool_use") {
|
|
325
|
+
toolState.resetTaskInput();
|
|
326
|
+
toolState.setToolState(true, contentBlock.name, contentBlock.id);
|
|
327
|
+
yield {
|
|
328
|
+
type: "tool_call_start",
|
|
329
|
+
data: {
|
|
330
|
+
toolCallId: contentBlock.id,
|
|
331
|
+
toolName: contentBlock.name,
|
|
332
|
+
status: "running"
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
} else if (eventType === "content_block_delta") {
|
|
337
|
+
const delta = event.delta;
|
|
338
|
+
if (delta?.type === "text_delta" && !toolState.inTool) {
|
|
339
|
+
yield { type: "text_delta", data: { text: delta.text } };
|
|
340
|
+
} else if (delta?.type === "input_json_delta" && toolState.inTool) {
|
|
341
|
+
if (TASK_TOOL_NAMES.has(toolState.currentToolName)) {
|
|
342
|
+
toolState.appendTaskInput(delta.partial_json);
|
|
343
|
+
}
|
|
344
|
+
yield {
|
|
345
|
+
type: "tool_call_delta",
|
|
346
|
+
data: {
|
|
347
|
+
toolCallId: toolState.currentToolId,
|
|
348
|
+
toolName: toolState.currentToolName,
|
|
349
|
+
input: delta.partial_json,
|
|
350
|
+
status: "running"
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
} else if (eventType === "content_block_stop") {
|
|
355
|
+
if (toolState.inTool) {
|
|
356
|
+
const wasTaskTool = TASK_TOOL_NAMES.has(toolState.currentToolName);
|
|
357
|
+
const taskToolName = toolState.currentToolName;
|
|
358
|
+
yield {
|
|
359
|
+
type: "tool_call_end",
|
|
360
|
+
data: {
|
|
361
|
+
toolCallId: toolState.currentToolId,
|
|
362
|
+
toolName: toolState.currentToolName,
|
|
363
|
+
status: "complete"
|
|
364
|
+
}
|
|
365
|
+
};
|
|
366
|
+
toolState.setToolState(false, "", "");
|
|
367
|
+
if (wasTaskTool && toolState.taskToolInput) {
|
|
368
|
+
try {
|
|
369
|
+
const input = JSON.parse(toolState.taskToolInput);
|
|
370
|
+
const taskEvent = buildTaskEvent(taskToolName, input);
|
|
371
|
+
if (taskEvent) {
|
|
372
|
+
yield { type: "task_update", data: taskEvent };
|
|
373
|
+
}
|
|
374
|
+
} catch {
|
|
375
|
+
}
|
|
376
|
+
toolState.resetTaskInput();
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
if (message.type === "tool_use_summary") {
|
|
383
|
+
const summary = message;
|
|
384
|
+
for (const toolUseId of summary.preceding_tool_use_ids) {
|
|
385
|
+
yield {
|
|
386
|
+
type: "tool_result",
|
|
387
|
+
data: {
|
|
388
|
+
toolCallId: toolUseId,
|
|
389
|
+
toolName: "",
|
|
390
|
+
result: summary.summary,
|
|
391
|
+
status: "complete"
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
if (message.type === "result") {
|
|
398
|
+
const result = message;
|
|
399
|
+
const usage = result.usage;
|
|
400
|
+
const modelUsageMap = result.modelUsage;
|
|
401
|
+
const firstModelUsage = modelUsageMap ? Object.values(modelUsageMap)[0] : void 0;
|
|
402
|
+
yield {
|
|
403
|
+
type: "session_status",
|
|
404
|
+
data: {
|
|
405
|
+
sessionId,
|
|
406
|
+
model: result.model,
|
|
407
|
+
costUsd: result.total_cost_usd,
|
|
408
|
+
contextTokens: usage?.input_tokens,
|
|
409
|
+
contextMaxTokens: firstModelUsage?.contextWindow
|
|
410
|
+
}
|
|
411
|
+
};
|
|
412
|
+
yield {
|
|
413
|
+
type: "done",
|
|
414
|
+
data: { sessionId }
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
updateSession(sessionId, opts) {
|
|
419
|
+
let session = this.findSession(sessionId);
|
|
420
|
+
if (!session) {
|
|
421
|
+
this.ensureSession(sessionId, {
|
|
422
|
+
permissionMode: opts.permissionMode ?? "default",
|
|
423
|
+
hasStarted: true
|
|
424
|
+
});
|
|
425
|
+
session = this.sessions.get(sessionId);
|
|
426
|
+
}
|
|
427
|
+
if (opts.permissionMode) {
|
|
428
|
+
console.log(`[updateSession] ${sessionId} permissionMode: ${session.permissionMode} \u2192 ${opts.permissionMode}`);
|
|
429
|
+
session.permissionMode = opts.permissionMode;
|
|
430
|
+
if (session.activeQuery) {
|
|
431
|
+
console.log(`[updateSession] ${sessionId} calling setPermissionMode(${opts.permissionMode}) on active query`);
|
|
432
|
+
session.activeQuery.setPermissionMode(opts.permissionMode).catch((err) => {
|
|
433
|
+
console.error(`[updateSession] setPermissionMode failed:`, err);
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
if (opts.model) {
|
|
438
|
+
session.model = opts.model;
|
|
439
|
+
}
|
|
440
|
+
return true;
|
|
441
|
+
}
|
|
442
|
+
approveTool(sessionId, toolCallId, approved) {
|
|
443
|
+
const session = this.findSession(sessionId);
|
|
444
|
+
const pending = session?.pendingInteractions.get(toolCallId);
|
|
445
|
+
if (!pending || pending.type !== "approval") {
|
|
446
|
+
console.log(`[approveTool] ${sessionId} toolCallId=${toolCallId} approved=${approved} \u2192 NOT FOUND (session=${!!session}, pending=${!!pending}, type=${pending?.type})`);
|
|
447
|
+
return false;
|
|
448
|
+
}
|
|
449
|
+
console.log(`[approveTool] ${sessionId} toolCallId=${toolCallId} approved=${approved} \u2192 resolving`);
|
|
450
|
+
pending.resolve(approved);
|
|
451
|
+
return true;
|
|
452
|
+
}
|
|
453
|
+
submitAnswers(sessionId, toolCallId, answers) {
|
|
454
|
+
const session = this.findSession(sessionId);
|
|
455
|
+
const pending = session?.pendingInteractions.get(toolCallId);
|
|
456
|
+
if (!pending || pending.type !== "question") return false;
|
|
457
|
+
pending.resolve(answers);
|
|
458
|
+
return true;
|
|
459
|
+
}
|
|
460
|
+
checkSessionHealth() {
|
|
461
|
+
const now = Date.now();
|
|
462
|
+
for (const [id, session] of this.sessions) {
|
|
463
|
+
if (now - session.lastActivity > this.SESSION_TIMEOUT_MS) {
|
|
464
|
+
for (const interaction of session.pendingInteractions.values()) {
|
|
465
|
+
clearTimeout(interaction.timeout);
|
|
466
|
+
}
|
|
467
|
+
this.sessions.delete(id);
|
|
468
|
+
this.sessionLocks.delete(id);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
for (const [id, lock] of this.sessionLocks) {
|
|
472
|
+
if (now - lock.acquiredAt > lock.ttl) {
|
|
473
|
+
this.sessionLocks.delete(id);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* Find a session by its map key OR by its sdkSessionId.
|
|
479
|
+
* After the SDK assigns a real session ID (on first message), the client
|
|
480
|
+
* starts using the new ID while the map still stores it under the original key.
|
|
481
|
+
*/
|
|
482
|
+
findSession(sessionId) {
|
|
483
|
+
const direct = this.sessions.get(sessionId);
|
|
484
|
+
if (direct) return direct;
|
|
485
|
+
for (const session of this.sessions.values()) {
|
|
486
|
+
if (session.sdkSessionId === sessionId) return session;
|
|
487
|
+
}
|
|
488
|
+
return void 0;
|
|
489
|
+
}
|
|
490
|
+
hasSession(sessionId) {
|
|
491
|
+
return !!this.findSession(sessionId);
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Get the actual SDK session ID (may differ from input if SDK assigned a new one).
|
|
495
|
+
*/
|
|
496
|
+
getSdkSessionId(sessionId) {
|
|
497
|
+
return this.findSession(sessionId)?.sdkSessionId;
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* Attempt to acquire a lock on a session for a specific client.
|
|
501
|
+
* Returns true if the lock was acquired, false if the session is locked by another client.
|
|
502
|
+
*/
|
|
503
|
+
acquireLock(sessionId, clientId, res) {
|
|
504
|
+
const existing = this.sessionLocks.get(sessionId);
|
|
505
|
+
if (existing) {
|
|
506
|
+
const expired = Date.now() - existing.acquiredAt > existing.ttl;
|
|
507
|
+
if (expired) {
|
|
508
|
+
this.sessionLocks.delete(sessionId);
|
|
509
|
+
} else if (existing.clientId !== clientId) {
|
|
510
|
+
return false;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
const lock = {
|
|
514
|
+
clientId,
|
|
515
|
+
acquiredAt: Date.now(),
|
|
516
|
+
ttl: this.LOCK_TTL_MS,
|
|
517
|
+
response: res
|
|
518
|
+
};
|
|
519
|
+
this.sessionLocks.set(sessionId, lock);
|
|
520
|
+
res.on("close", () => {
|
|
521
|
+
const current = this.sessionLocks.get(sessionId);
|
|
522
|
+
if (current === lock) {
|
|
523
|
+
this.sessionLocks.delete(sessionId);
|
|
524
|
+
}
|
|
525
|
+
});
|
|
526
|
+
return true;
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Release a lock on a session if it's held by the specified client.
|
|
530
|
+
*/
|
|
531
|
+
releaseLock(sessionId, clientId) {
|
|
532
|
+
const lock = this.sessionLocks.get(sessionId);
|
|
533
|
+
if (lock && lock.clientId === clientId) {
|
|
534
|
+
this.sessionLocks.delete(sessionId);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Check if a session is locked.
|
|
539
|
+
* If clientId is provided, returns false if the lock is held by that client (owns the lock).
|
|
540
|
+
* Returns true if the session is locked by another client.
|
|
541
|
+
*/
|
|
542
|
+
isLocked(sessionId, clientId) {
|
|
543
|
+
const lock = this.sessionLocks.get(sessionId);
|
|
544
|
+
if (!lock) return false;
|
|
545
|
+
if (Date.now() - lock.acquiredAt > lock.ttl) {
|
|
546
|
+
this.sessionLocks.delete(sessionId);
|
|
547
|
+
return false;
|
|
548
|
+
}
|
|
549
|
+
if (clientId && lock.clientId === clientId) return false;
|
|
550
|
+
return true;
|
|
551
|
+
}
|
|
552
|
+
/**
|
|
553
|
+
* Get information about the current lock on a session.
|
|
554
|
+
* Returns null if the session is not locked or the lock has expired.
|
|
555
|
+
*/
|
|
556
|
+
getLockInfo(sessionId) {
|
|
557
|
+
const lock = this.sessionLocks.get(sessionId);
|
|
558
|
+
if (!lock) return null;
|
|
559
|
+
if (Date.now() - lock.acquiredAt > lock.ttl) {
|
|
560
|
+
this.sessionLocks.delete(sessionId);
|
|
561
|
+
return null;
|
|
562
|
+
}
|
|
563
|
+
return { clientId: lock.clientId, acquiredAt: lock.acquiredAt };
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
var agentManager = new AgentManager();
|
|
567
|
+
|
|
568
|
+
// ../../apps/server/src/services/transcript-reader.ts
|
|
569
|
+
import fs from "fs/promises";
|
|
570
|
+
import path2 from "path";
|
|
571
|
+
import os from "os";
|
|
572
|
+
var TranscriptReader = class {
|
|
573
|
+
metaCache = /* @__PURE__ */ new Map();
|
|
574
|
+
getProjectSlug(cwd) {
|
|
575
|
+
return cwd.replace(/[^a-zA-Z0-9-]/g, "-");
|
|
576
|
+
}
|
|
577
|
+
getTranscriptsDir(vaultRoot2) {
|
|
578
|
+
const slug = this.getProjectSlug(vaultRoot2);
|
|
579
|
+
return path2.join(os.homedir(), ".claude", "projects", slug);
|
|
580
|
+
}
|
|
581
|
+
/**
|
|
582
|
+
* List all sessions by scanning SDK JSONL transcript files.
|
|
583
|
+
* Extracts metadata (title, timestamps, preview) from file content and stats.
|
|
584
|
+
*/
|
|
585
|
+
async listSessions(vaultRoot2) {
|
|
586
|
+
const transcriptsDir = this.getTranscriptsDir(vaultRoot2);
|
|
587
|
+
let files;
|
|
588
|
+
try {
|
|
589
|
+
files = (await fs.readdir(transcriptsDir)).filter((f) => f.endsWith(".jsonl"));
|
|
590
|
+
} catch {
|
|
591
|
+
return [];
|
|
592
|
+
}
|
|
593
|
+
const sessions = [];
|
|
594
|
+
for (const file of files) {
|
|
595
|
+
const sessionId = file.replace(".jsonl", "");
|
|
596
|
+
const filePath = path2.join(transcriptsDir, file);
|
|
597
|
+
try {
|
|
598
|
+
const fileStat = await fs.stat(filePath);
|
|
599
|
+
const cached = this.metaCache.get(sessionId);
|
|
600
|
+
if (cached && cached.mtimeMs === fileStat.mtimeMs) {
|
|
601
|
+
sessions.push(cached.session);
|
|
602
|
+
continue;
|
|
603
|
+
}
|
|
604
|
+
const meta = await this.extractSessionMeta(filePath, sessionId, fileStat);
|
|
605
|
+
this.metaCache.set(sessionId, { session: meta, mtimeMs: fileStat.mtimeMs });
|
|
606
|
+
sessions.push(meta);
|
|
607
|
+
} catch {
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
sessions.sort((a, b) => b.updatedAt.localeCompare(a.updatedAt));
|
|
611
|
+
return sessions;
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* Get metadata for a single session.
|
|
615
|
+
* Reads both head (for title/timestamps) and tail (for latest model/context).
|
|
616
|
+
*/
|
|
617
|
+
async getSession(vaultRoot2, sessionId) {
|
|
618
|
+
const filePath = path2.join(this.getTranscriptsDir(vaultRoot2), `${sessionId}.jsonl`);
|
|
619
|
+
try {
|
|
620
|
+
const session = await this.extractSessionMeta(filePath, sessionId);
|
|
621
|
+
const tailStatus = await this.readTailStatus(filePath);
|
|
622
|
+
if (tailStatus.model) session.model = tailStatus.model;
|
|
623
|
+
if (tailStatus.permissionMode) session.permissionMode = tailStatus.permissionMode;
|
|
624
|
+
if (tailStatus.contextTokens) session.contextTokens = tailStatus.contextTokens;
|
|
625
|
+
return session;
|
|
626
|
+
} catch {
|
|
627
|
+
return null;
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* Read the tail of a JSONL file to get the most recent model, permissionMode, and context tokens.
|
|
632
|
+
* Reads the last ~16KB which typically contains the final assistant messages.
|
|
633
|
+
*/
|
|
634
|
+
async readTailStatus(filePath) {
|
|
635
|
+
const TAIL_SIZE = 16384;
|
|
636
|
+
try {
|
|
637
|
+
const stat4 = await fs.stat(filePath);
|
|
638
|
+
const fileHandle = await fs.open(filePath, "r");
|
|
639
|
+
try {
|
|
640
|
+
const readOffset = Math.max(0, stat4.size - TAIL_SIZE);
|
|
641
|
+
const buffer = Buffer.alloc(Math.min(TAIL_SIZE, stat4.size));
|
|
642
|
+
const { bytesRead } = await fileHandle.read(buffer, 0, buffer.length, readOffset);
|
|
643
|
+
const chunk = buffer.toString("utf-8", 0, bytesRead);
|
|
644
|
+
const lines = chunk.split("\n").filter((l) => l.trim());
|
|
645
|
+
let model;
|
|
646
|
+
let permissionMode;
|
|
647
|
+
let contextTokens;
|
|
648
|
+
for (const line of lines) {
|
|
649
|
+
let parsed;
|
|
650
|
+
try {
|
|
651
|
+
parsed = JSON.parse(line);
|
|
652
|
+
} catch {
|
|
653
|
+
continue;
|
|
654
|
+
}
|
|
655
|
+
if (parsed.type === "assistant" && parsed.message?.model) {
|
|
656
|
+
model = parsed.message.model;
|
|
657
|
+
if (parsed.message.usage) {
|
|
658
|
+
const u = parsed.message.usage;
|
|
659
|
+
contextTokens = (u.input_tokens ?? 0) + (u.cache_read_input_tokens ?? 0) + (u.cache_creation_input_tokens ?? 0);
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
if (parsed.type === "user" && parsed.permissionMode) {
|
|
663
|
+
const sdkMode = parsed.permissionMode;
|
|
664
|
+
if (sdkMode === "bypassPermissions" || sdkMode === "dangerously-skip") {
|
|
665
|
+
permissionMode = "bypassPermissions";
|
|
666
|
+
} else if (sdkMode === "plan") {
|
|
667
|
+
permissionMode = "plan";
|
|
668
|
+
} else if (sdkMode === "acceptEdits") {
|
|
669
|
+
permissionMode = "acceptEdits";
|
|
670
|
+
} else {
|
|
671
|
+
permissionMode = "default";
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
return { model, permissionMode, contextTokens };
|
|
676
|
+
} finally {
|
|
677
|
+
await fileHandle.close();
|
|
678
|
+
}
|
|
679
|
+
} catch {
|
|
680
|
+
return {};
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
/**
|
|
684
|
+
* Extract session metadata from a JSONL file.
|
|
685
|
+
* Reads only the first ~8KB for title/permissionMode, and uses file stat for timestamps.
|
|
686
|
+
*/
|
|
687
|
+
async extractSessionMeta(filePath, sessionId, fileStat) {
|
|
688
|
+
const stat4 = fileStat ?? await fs.stat(filePath);
|
|
689
|
+
const fileHandle = await fs.open(filePath, "r");
|
|
690
|
+
let chunk;
|
|
691
|
+
try {
|
|
692
|
+
const buffer = Buffer.alloc(8192);
|
|
693
|
+
const { bytesRead } = await fileHandle.read(buffer, 0, 8192, 0);
|
|
694
|
+
chunk = buffer.toString("utf-8", 0, bytesRead);
|
|
695
|
+
} finally {
|
|
696
|
+
await fileHandle.close();
|
|
697
|
+
}
|
|
698
|
+
const lines = chunk.split("\n").filter((l) => l.trim());
|
|
699
|
+
let firstUserMessage = "";
|
|
700
|
+
let permissionMode = "default";
|
|
701
|
+
let firstTimestamp = "";
|
|
702
|
+
let model;
|
|
703
|
+
let cwd;
|
|
704
|
+
for (const line of lines) {
|
|
705
|
+
let parsed;
|
|
706
|
+
try {
|
|
707
|
+
parsed = JSON.parse(line);
|
|
708
|
+
} catch {
|
|
709
|
+
continue;
|
|
710
|
+
}
|
|
711
|
+
if (parsed.type === "system" && parsed.subtype === "init" && parsed.permissionMode) {
|
|
712
|
+
const sdkMode = parsed.permissionMode;
|
|
713
|
+
if (sdkMode === "bypassPermissions" || sdkMode === "dangerously-skip") {
|
|
714
|
+
permissionMode = "bypassPermissions";
|
|
715
|
+
} else if (sdkMode === "plan") {
|
|
716
|
+
permissionMode = "plan";
|
|
717
|
+
} else if (sdkMode === "acceptEdits") {
|
|
718
|
+
permissionMode = "acceptEdits";
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
if (parsed.type === "user" && parsed.permissionMode) {
|
|
722
|
+
const sdkMode = parsed.permissionMode;
|
|
723
|
+
if (sdkMode === "bypassPermissions" || sdkMode === "dangerously-skip") {
|
|
724
|
+
permissionMode = "bypassPermissions";
|
|
725
|
+
} else if (sdkMode === "plan") {
|
|
726
|
+
permissionMode = "plan";
|
|
727
|
+
} else if (sdkMode === "acceptEdits") {
|
|
728
|
+
permissionMode = "acceptEdits";
|
|
729
|
+
} else {
|
|
730
|
+
permissionMode = "default";
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
if (!model && parsed.type === "assistant" && parsed.message?.model) {
|
|
734
|
+
model = parsed.message.model;
|
|
735
|
+
}
|
|
736
|
+
if (parsed.timestamp && !firstTimestamp) {
|
|
737
|
+
firstTimestamp = parsed.timestamp;
|
|
738
|
+
}
|
|
739
|
+
if (!cwd && parsed.cwd) {
|
|
740
|
+
cwd = parsed.cwd;
|
|
741
|
+
}
|
|
742
|
+
if (!firstUserMessage && parsed.type === "user" && parsed.message) {
|
|
743
|
+
const text = this.extractTextContent(parsed.message.content);
|
|
744
|
+
if (text.startsWith("<local-command") || text.startsWith("<command-name>") || text.startsWith("<command-message>") || text.startsWith("<task-notification>")) {
|
|
745
|
+
continue;
|
|
746
|
+
}
|
|
747
|
+
if (text.startsWith("This session is being continued")) {
|
|
748
|
+
continue;
|
|
749
|
+
}
|
|
750
|
+
const cleanText = this.stripSystemTags(text);
|
|
751
|
+
if (!cleanText.trim()) continue;
|
|
752
|
+
firstUserMessage = cleanText.trim();
|
|
753
|
+
}
|
|
754
|
+
if (firstUserMessage && firstTimestamp && model && cwd) break;
|
|
755
|
+
}
|
|
756
|
+
const title = firstUserMessage ? firstUserMessage.slice(0, 80) + (firstUserMessage.length > 80 ? "..." : "") : `Session ${sessionId.slice(0, 8)}`;
|
|
757
|
+
return {
|
|
758
|
+
id: sessionId,
|
|
759
|
+
title,
|
|
760
|
+
createdAt: firstTimestamp || stat4.birthtime.toISOString(),
|
|
761
|
+
updatedAt: stat4.mtime.toISOString(),
|
|
762
|
+
lastMessagePreview: void 0,
|
|
763
|
+
permissionMode,
|
|
764
|
+
model,
|
|
765
|
+
cwd
|
|
766
|
+
};
|
|
767
|
+
}
|
|
768
|
+
/**
|
|
769
|
+
* Read messages from an SDK session transcript.
|
|
770
|
+
*/
|
|
771
|
+
async readTranscript(vaultRoot2, sessionId) {
|
|
772
|
+
const transcriptsDir = this.getTranscriptsDir(vaultRoot2);
|
|
773
|
+
const filePath = path2.join(transcriptsDir, `${sessionId}.jsonl`);
|
|
774
|
+
let content;
|
|
775
|
+
try {
|
|
776
|
+
content = await fs.readFile(filePath, "utf-8");
|
|
777
|
+
} catch {
|
|
778
|
+
return [];
|
|
779
|
+
}
|
|
780
|
+
const messages = [];
|
|
781
|
+
const lines = content.split("\n").filter((l) => l.trim());
|
|
782
|
+
let pendingCommand = null;
|
|
783
|
+
let pendingSkillArgs = null;
|
|
784
|
+
const toolCallMap = /* @__PURE__ */ new Map();
|
|
785
|
+
const toolCallPartMap = /* @__PURE__ */ new Map();
|
|
786
|
+
for (const line of lines) {
|
|
787
|
+
let parsed;
|
|
788
|
+
try {
|
|
789
|
+
parsed = JSON.parse(line);
|
|
790
|
+
} catch {
|
|
791
|
+
continue;
|
|
792
|
+
}
|
|
793
|
+
if (parsed.type === "user" && parsed.message) {
|
|
794
|
+
const msgContent = parsed.message.content;
|
|
795
|
+
if (Array.isArray(msgContent)) {
|
|
796
|
+
let hasToolResult = false;
|
|
797
|
+
const textParts = [];
|
|
798
|
+
const sdkAnswers = parsed.toolUseResult?.answers;
|
|
799
|
+
for (const block of msgContent) {
|
|
800
|
+
if (block.type === "tool_result" && block.tool_use_id) {
|
|
801
|
+
hasToolResult = true;
|
|
802
|
+
const resultText = this.extractToolResultContent(block.content);
|
|
803
|
+
const tc = toolCallMap.get(block.tool_use_id);
|
|
804
|
+
if (tc) {
|
|
805
|
+
tc.result = resultText;
|
|
806
|
+
if (tc.toolName === "AskUserQuestion" && tc.questions && !tc.answers) {
|
|
807
|
+
tc.answers = sdkAnswers ? this.mapSdkAnswersToIndices(sdkAnswers, tc.questions) : this.parseQuestionAnswers(resultText, tc.questions);
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
const tcPart = toolCallPartMap.get(block.tool_use_id);
|
|
811
|
+
if (tcPart) {
|
|
812
|
+
tcPart.result = resultText;
|
|
813
|
+
if (tcPart.toolName === "AskUserQuestion" && tcPart.questions && !tcPart.answers) {
|
|
814
|
+
tcPart.answers = sdkAnswers ? this.mapSdkAnswersToIndices(sdkAnswers, tcPart.questions) : this.parseQuestionAnswers(resultText, tcPart.questions);
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
} else if (block.type === "text" && block.text) {
|
|
818
|
+
textParts.push(block.text);
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
if (hasToolResult && textParts.length === 0) {
|
|
822
|
+
if (parsed.toolUseResult?.commandName) {
|
|
823
|
+
const cmdName = "/" + parsed.toolUseResult.commandName.replace(/^\//, "");
|
|
824
|
+
pendingCommand = { commandName: cmdName, commandArgs: pendingSkillArgs || "" };
|
|
825
|
+
pendingSkillArgs = null;
|
|
826
|
+
}
|
|
827
|
+
continue;
|
|
828
|
+
}
|
|
829
|
+
if (pendingCommand) {
|
|
830
|
+
const { commandName, commandArgs } = pendingCommand;
|
|
831
|
+
pendingCommand = null;
|
|
832
|
+
const displayContent = commandArgs ? `${commandName} ${commandArgs}` : commandName;
|
|
833
|
+
messages.push({
|
|
834
|
+
id: parsed.uuid || crypto.randomUUID(),
|
|
835
|
+
role: "user",
|
|
836
|
+
content: displayContent,
|
|
837
|
+
messageType: "command",
|
|
838
|
+
commandName,
|
|
839
|
+
commandArgs: commandArgs || void 0
|
|
840
|
+
});
|
|
841
|
+
continue;
|
|
842
|
+
}
|
|
843
|
+
if (textParts.length > 0) {
|
|
844
|
+
const cleanText2 = this.stripSystemTags(textParts.join("\n"));
|
|
845
|
+
if (cleanText2.trim()) {
|
|
846
|
+
messages.push({
|
|
847
|
+
id: parsed.uuid || crypto.randomUUID(),
|
|
848
|
+
role: "user",
|
|
849
|
+
content: cleanText2
|
|
850
|
+
});
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
continue;
|
|
854
|
+
}
|
|
855
|
+
const text = typeof msgContent === "string" ? msgContent : "";
|
|
856
|
+
if (text.startsWith("<task-notification>")) {
|
|
857
|
+
continue;
|
|
858
|
+
}
|
|
859
|
+
if (text.startsWith("<command-message>") || text.startsWith("<command-name>")) {
|
|
860
|
+
const meta = this.extractCommandMeta(text);
|
|
861
|
+
if (meta) {
|
|
862
|
+
pendingCommand = meta;
|
|
863
|
+
}
|
|
864
|
+
continue;
|
|
865
|
+
}
|
|
866
|
+
if (text.startsWith("<local-command")) {
|
|
867
|
+
pendingCommand = null;
|
|
868
|
+
continue;
|
|
869
|
+
}
|
|
870
|
+
if (pendingCommand) {
|
|
871
|
+
const { commandName, commandArgs } = pendingCommand;
|
|
872
|
+
pendingCommand = null;
|
|
873
|
+
const displayContent = commandArgs ? `${commandName} ${commandArgs}` : commandName;
|
|
874
|
+
messages.push({
|
|
875
|
+
id: parsed.uuid || crypto.randomUUID(),
|
|
876
|
+
role: "user",
|
|
877
|
+
content: displayContent,
|
|
878
|
+
messageType: "command",
|
|
879
|
+
commandName,
|
|
880
|
+
commandArgs: commandArgs || void 0
|
|
881
|
+
});
|
|
882
|
+
continue;
|
|
883
|
+
}
|
|
884
|
+
if (text.startsWith("This session is being continued")) {
|
|
885
|
+
messages.push({
|
|
886
|
+
id: parsed.uuid || crypto.randomUUID(),
|
|
887
|
+
role: "user",
|
|
888
|
+
content: text,
|
|
889
|
+
messageType: "compaction"
|
|
890
|
+
});
|
|
891
|
+
continue;
|
|
892
|
+
}
|
|
893
|
+
const cleanText = this.stripSystemTags(text);
|
|
894
|
+
if (!cleanText.trim()) continue;
|
|
895
|
+
messages.push({
|
|
896
|
+
id: parsed.uuid || crypto.randomUUID(),
|
|
897
|
+
role: "user",
|
|
898
|
+
content: cleanText
|
|
899
|
+
});
|
|
900
|
+
} else if (parsed.type === "assistant" && parsed.message) {
|
|
901
|
+
const contentBlocks = parsed.message.content;
|
|
902
|
+
if (!Array.isArray(contentBlocks)) continue;
|
|
903
|
+
const parts = [];
|
|
904
|
+
const toolCalls = [];
|
|
905
|
+
for (const block of contentBlocks) {
|
|
906
|
+
if (block.type === "text" && block.text) {
|
|
907
|
+
const lastPart = parts[parts.length - 1];
|
|
908
|
+
if (lastPart && lastPart.type === "text") {
|
|
909
|
+
lastPart.text += "\n" + block.text;
|
|
910
|
+
} else {
|
|
911
|
+
parts.push({ type: "text", text: block.text });
|
|
912
|
+
}
|
|
913
|
+
} else if (block.type === "tool_use" && block.name && block.id) {
|
|
914
|
+
const tc = {
|
|
915
|
+
toolCallId: block.id,
|
|
916
|
+
toolName: block.name,
|
|
917
|
+
input: block.input ? JSON.stringify(block.input) : void 0,
|
|
918
|
+
status: "complete"
|
|
919
|
+
};
|
|
920
|
+
if (block.name === "AskUserQuestion" && block.input) {
|
|
921
|
+
if (Array.isArray(block.input.questions)) {
|
|
922
|
+
tc.questions = block.input.questions;
|
|
923
|
+
}
|
|
924
|
+
if (block.input.answers && typeof block.input.answers === "object") {
|
|
925
|
+
tc.answers = block.input.answers;
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
if (block.name === "Skill" && block.input) {
|
|
929
|
+
const input = block.input;
|
|
930
|
+
pendingSkillArgs = input.args || null;
|
|
931
|
+
}
|
|
932
|
+
toolCalls.push(tc);
|
|
933
|
+
toolCallMap.set(block.id, tc);
|
|
934
|
+
const toolCallPart = {
|
|
935
|
+
type: "tool_call",
|
|
936
|
+
toolCallId: block.id,
|
|
937
|
+
toolName: block.name,
|
|
938
|
+
input: block.input ? JSON.stringify(block.input) : void 0,
|
|
939
|
+
status: "complete",
|
|
940
|
+
...tc.questions ? {
|
|
941
|
+
interactiveType: "question",
|
|
942
|
+
questions: tc.questions,
|
|
943
|
+
answers: tc.answers
|
|
944
|
+
} : {}
|
|
945
|
+
};
|
|
946
|
+
parts.push(toolCallPart);
|
|
947
|
+
toolCallPartMap.set(block.id, toolCallPart);
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
if (parts.length === 0) continue;
|
|
951
|
+
const text = parts.filter((p) => p.type === "text").map((p) => p.text).join("\n").trim();
|
|
952
|
+
messages.push({
|
|
953
|
+
id: parsed.uuid || crypto.randomUUID(),
|
|
954
|
+
role: "assistant",
|
|
955
|
+
content: text,
|
|
956
|
+
toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
957
|
+
parts,
|
|
958
|
+
timestamp: parsed.timestamp
|
|
959
|
+
});
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
return messages;
|
|
963
|
+
}
|
|
964
|
+
/**
|
|
965
|
+
* List available SDK session transcript IDs.
|
|
966
|
+
*/
|
|
967
|
+
async listTranscripts(vaultRoot2) {
|
|
968
|
+
const transcriptsDir = this.getTranscriptsDir(vaultRoot2);
|
|
969
|
+
try {
|
|
970
|
+
const files = await fs.readdir(transcriptsDir);
|
|
971
|
+
return files.filter((f) => f.endsWith(".jsonl")).map((f) => f.replace(".jsonl", ""));
|
|
972
|
+
} catch {
|
|
973
|
+
return [];
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
/**
|
|
977
|
+
* Read task state from an SDK session transcript.
|
|
978
|
+
* Parses TaskCreate/TaskUpdate tool_use blocks and reconstructs final state.
|
|
979
|
+
*/
|
|
980
|
+
async getTranscriptETag(vaultRoot2, sessionId) {
|
|
981
|
+
const filePath = path2.join(this.getTranscriptsDir(vaultRoot2), `${sessionId}.jsonl`);
|
|
982
|
+
try {
|
|
983
|
+
const stat4 = await fs.stat(filePath);
|
|
984
|
+
return `"${stat4.mtimeMs}-${stat4.size}"`;
|
|
985
|
+
} catch {
|
|
986
|
+
return null;
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
async readTasks(vaultRoot2, sessionId) {
|
|
990
|
+
const transcriptsDir = this.getTranscriptsDir(vaultRoot2);
|
|
991
|
+
const filePath = path2.join(transcriptsDir, `${sessionId}.jsonl`);
|
|
992
|
+
let content;
|
|
993
|
+
try {
|
|
994
|
+
content = await fs.readFile(filePath, "utf-8");
|
|
995
|
+
} catch {
|
|
996
|
+
return [];
|
|
997
|
+
}
|
|
998
|
+
const lines = content.split("\n").filter((l) => l.trim());
|
|
999
|
+
const tasks = /* @__PURE__ */ new Map();
|
|
1000
|
+
let nextId = 1;
|
|
1001
|
+
for (const line of lines) {
|
|
1002
|
+
let parsed;
|
|
1003
|
+
try {
|
|
1004
|
+
parsed = JSON.parse(line);
|
|
1005
|
+
} catch {
|
|
1006
|
+
continue;
|
|
1007
|
+
}
|
|
1008
|
+
if (parsed.type !== "assistant") continue;
|
|
1009
|
+
const message = parsed.message;
|
|
1010
|
+
if (!message?.content || !Array.isArray(message.content)) continue;
|
|
1011
|
+
for (const block of message.content) {
|
|
1012
|
+
if (block.type !== "tool_use") continue;
|
|
1013
|
+
if (!block.name || !["TaskCreate", "TaskUpdate"].includes(block.name)) continue;
|
|
1014
|
+
const input = block.input;
|
|
1015
|
+
if (!input) continue;
|
|
1016
|
+
if (block.name === "TaskCreate") {
|
|
1017
|
+
const id = String(nextId++);
|
|
1018
|
+
tasks.set(id, {
|
|
1019
|
+
id,
|
|
1020
|
+
subject: input.subject ?? "",
|
|
1021
|
+
description: input.description,
|
|
1022
|
+
activeForm: input.activeForm,
|
|
1023
|
+
status: "pending"
|
|
1024
|
+
});
|
|
1025
|
+
} else if (block.name === "TaskUpdate" && input.taskId) {
|
|
1026
|
+
const existing = tasks.get(input.taskId);
|
|
1027
|
+
if (existing) {
|
|
1028
|
+
if (input.status) existing.status = input.status;
|
|
1029
|
+
if (input.subject) existing.subject = input.subject;
|
|
1030
|
+
if (input.activeForm) existing.activeForm = input.activeForm;
|
|
1031
|
+
if (input.description) existing.description = input.description;
|
|
1032
|
+
if (input.addBlockedBy) existing.blockedBy = [...existing.blockedBy ?? [], ...input.addBlockedBy];
|
|
1033
|
+
if (input.addBlocks) existing.blocks = [...existing.blocks ?? [], ...input.addBlocks];
|
|
1034
|
+
if (input.owner) existing.owner = input.owner;
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
return Array.from(tasks.values());
|
|
1040
|
+
}
|
|
1041
|
+
/**
|
|
1042
|
+
* Read new content from a transcript file starting from a byte offset.
|
|
1043
|
+
* Returns the new content and the updated file size (new offset).
|
|
1044
|
+
*/
|
|
1045
|
+
async readFromOffset(vaultRoot2, sessionId, fromOffset) {
|
|
1046
|
+
const filePath = path2.join(this.getTranscriptsDir(vaultRoot2), `${sessionId}.jsonl`);
|
|
1047
|
+
const stat4 = await fs.stat(filePath);
|
|
1048
|
+
if (stat4.size <= fromOffset) {
|
|
1049
|
+
return { content: "", newOffset: fromOffset };
|
|
1050
|
+
}
|
|
1051
|
+
const fileHandle = await fs.open(filePath, "r");
|
|
1052
|
+
try {
|
|
1053
|
+
const newBytes = stat4.size - fromOffset;
|
|
1054
|
+
const buffer = Buffer.alloc(newBytes);
|
|
1055
|
+
await fileHandle.read(buffer, 0, newBytes, fromOffset);
|
|
1056
|
+
return {
|
|
1057
|
+
content: buffer.toString("utf-8"),
|
|
1058
|
+
newOffset: stat4.size
|
|
1059
|
+
};
|
|
1060
|
+
} finally {
|
|
1061
|
+
await fileHandle.close();
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
extractToolResultContent(content) {
|
|
1065
|
+
if (!content) return "";
|
|
1066
|
+
if (typeof content === "string") return content;
|
|
1067
|
+
if (!Array.isArray(content)) return "";
|
|
1068
|
+
return content.filter((b) => b.type === "text" && b.text).map((b) => b.text).join("\n");
|
|
1069
|
+
}
|
|
1070
|
+
extractTextContent(content) {
|
|
1071
|
+
if (typeof content === "string") return content;
|
|
1072
|
+
if (!Array.isArray(content)) return "";
|
|
1073
|
+
return content.filter((b) => b.type === "text" && b.text).map((b) => b.text).join("\n");
|
|
1074
|
+
}
|
|
1075
|
+
extractCommandMeta(text) {
|
|
1076
|
+
const nameMatch = text.match(/<command-name>\/?([^<]+)<\/command-name>/);
|
|
1077
|
+
if (!nameMatch) return null;
|
|
1078
|
+
const commandName = "/" + nameMatch[1].replace(/^\//, "");
|
|
1079
|
+
const argsMatch = text.match(/<command-args>([\s\S]*?)<\/command-args>/);
|
|
1080
|
+
const commandArgs = argsMatch ? argsMatch[1].trim() : "";
|
|
1081
|
+
return { commandName, commandArgs };
|
|
1082
|
+
}
|
|
1083
|
+
stripSystemTags(text) {
|
|
1084
|
+
return text.replace(/<system-reminder>[\s\S]*?<\/system-reminder>/g, "").trim();
|
|
1085
|
+
}
|
|
1086
|
+
/**
|
|
1087
|
+
* Map SDK's toolUseResult.answers (keyed by question text) to index-keyed record.
|
|
1088
|
+
* SDK stores answers as { "Question text": "Answer value" }.
|
|
1089
|
+
* Client expects { "0": "Answer value", "1": "..." }.
|
|
1090
|
+
*/
|
|
1091
|
+
mapSdkAnswersToIndices(sdkAnswers, questions) {
|
|
1092
|
+
const answers = {};
|
|
1093
|
+
for (const [questionText, answerText] of Object.entries(sdkAnswers)) {
|
|
1094
|
+
const qIdx = questions.findIndex((q) => q.question === questionText);
|
|
1095
|
+
if (qIdx !== -1) {
|
|
1096
|
+
answers[String(qIdx)] = answerText;
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
if (Object.keys(answers).length === 0) {
|
|
1100
|
+
for (const [key, value] of Object.entries(sdkAnswers)) {
|
|
1101
|
+
if (/^\d+$/.test(key)) {
|
|
1102
|
+
answers[key] = value;
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
return answers;
|
|
1107
|
+
}
|
|
1108
|
+
/**
|
|
1109
|
+
* Parse answers from AskUserQuestion tool_result text (fallback).
|
|
1110
|
+
* Format: `..."Question text"="Answer text", "Q2"="A2". You can now...`
|
|
1111
|
+
* Falls back to empty record (truthy signal) if parsing fails.
|
|
1112
|
+
*/
|
|
1113
|
+
parseQuestionAnswers(resultText, questions) {
|
|
1114
|
+
const answers = {};
|
|
1115
|
+
const pairRegex = /"([^"]+?)"\s*=\s*"([^"]+?)"/g;
|
|
1116
|
+
let match;
|
|
1117
|
+
while ((match = pairRegex.exec(resultText)) !== null) {
|
|
1118
|
+
const [, questionText, answerText] = match;
|
|
1119
|
+
const qIdx = questions.findIndex((q) => q.question === questionText);
|
|
1120
|
+
if (qIdx !== -1) {
|
|
1121
|
+
answers[String(qIdx)] = answerText;
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
return answers;
|
|
1125
|
+
}
|
|
1126
|
+
};
|
|
1127
|
+
var transcriptReader = new TranscriptReader();
|
|
1128
|
+
|
|
1129
|
+
// ../../apps/server/src/services/stream-adapter.ts
|
|
1130
|
+
function initSSEStream(res) {
|
|
1131
|
+
res.writeHead(200, {
|
|
1132
|
+
"Content-Type": "text/event-stream",
|
|
1133
|
+
"Cache-Control": "no-cache",
|
|
1134
|
+
Connection: "keep-alive",
|
|
1135
|
+
"X-Accel-Buffering": "no"
|
|
1136
|
+
});
|
|
1137
|
+
}
|
|
1138
|
+
function sendSSEEvent(res, event) {
|
|
1139
|
+
res.write(`event: ${event.type}
|
|
1140
|
+
`);
|
|
1141
|
+
res.write(`data: ${JSON.stringify(event.data)}
|
|
1142
|
+
|
|
1143
|
+
`);
|
|
1144
|
+
}
|
|
1145
|
+
function endSSEStream(res) {
|
|
1146
|
+
res.end();
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
// ../shared/dist/schemas.js
|
|
1150
|
+
import { z } from "zod";
|
|
1151
|
+
import { extendZodWithOpenApi } from "@asteasolutions/zod-to-openapi";
|
|
1152
|
+
extendZodWithOpenApi(z);
|
|
1153
|
+
var PermissionModeSchema = z.enum(["default", "plan", "acceptEdits", "bypassPermissions"]).openapi("PermissionMode");
|
|
1154
|
+
var TaskStatusSchema = z.enum(["pending", "in_progress", "completed"]).openapi("TaskStatus");
|
|
1155
|
+
var StreamEventTypeSchema = z.enum([
|
|
1156
|
+
"text_delta",
|
|
1157
|
+
"tool_call_start",
|
|
1158
|
+
"tool_call_delta",
|
|
1159
|
+
"tool_call_end",
|
|
1160
|
+
"tool_result",
|
|
1161
|
+
"approval_required",
|
|
1162
|
+
"question_prompt",
|
|
1163
|
+
"error",
|
|
1164
|
+
"done",
|
|
1165
|
+
"session_status",
|
|
1166
|
+
"task_update",
|
|
1167
|
+
"sync_update",
|
|
1168
|
+
"sync_connected"
|
|
1169
|
+
]).openapi("StreamEventType");
|
|
1170
|
+
var QuestionOptionSchema = z.object({
|
|
1171
|
+
label: z.string(),
|
|
1172
|
+
description: z.string().optional()
|
|
1173
|
+
}).openapi("QuestionOption");
|
|
1174
|
+
var QuestionItemSchema = z.object({
|
|
1175
|
+
header: z.string(),
|
|
1176
|
+
question: z.string(),
|
|
1177
|
+
options: z.array(QuestionOptionSchema),
|
|
1178
|
+
multiSelect: z.boolean()
|
|
1179
|
+
}).openapi("QuestionItem");
|
|
1180
|
+
var SessionSchema = z.object({
|
|
1181
|
+
id: z.string().uuid(),
|
|
1182
|
+
title: z.string(),
|
|
1183
|
+
createdAt: z.string().datetime(),
|
|
1184
|
+
updatedAt: z.string().datetime(),
|
|
1185
|
+
lastMessagePreview: z.string().optional(),
|
|
1186
|
+
permissionMode: PermissionModeSchema,
|
|
1187
|
+
model: z.string().optional(),
|
|
1188
|
+
contextTokens: z.number().int().optional(),
|
|
1189
|
+
cwd: z.string().optional()
|
|
1190
|
+
}).openapi("Session");
|
|
1191
|
+
var CreateSessionRequestSchema = z.object({
|
|
1192
|
+
permissionMode: PermissionModeSchema.optional(),
|
|
1193
|
+
cwd: z.string().optional()
|
|
1194
|
+
}).openapi("CreateSessionRequest");
|
|
1195
|
+
var UpdateSessionRequestSchema = z.object({
|
|
1196
|
+
permissionMode: PermissionModeSchema.optional(),
|
|
1197
|
+
model: z.string().optional()
|
|
1198
|
+
}).openapi("UpdateSessionRequest");
|
|
1199
|
+
var SendMessageRequestSchema = z.object({
|
|
1200
|
+
content: z.string().min(1, "content is required"),
|
|
1201
|
+
cwd: z.string().optional()
|
|
1202
|
+
}).openapi("SendMessageRequest");
|
|
1203
|
+
var ApprovalRequestSchema = z.object({
|
|
1204
|
+
toolCallId: z.string()
|
|
1205
|
+
}).openapi("ApprovalRequest");
|
|
1206
|
+
var SubmitAnswersRequestSchema = z.object({
|
|
1207
|
+
toolCallId: z.string(),
|
|
1208
|
+
answers: z.record(z.string(), z.string())
|
|
1209
|
+
}).openapi("SubmitAnswersRequest");
|
|
1210
|
+
var ListSessionsQuerySchema = z.object({
|
|
1211
|
+
limit: z.coerce.number().int().min(1).max(500).optional().default(200),
|
|
1212
|
+
cwd: z.string().optional()
|
|
1213
|
+
}).openapi("ListSessionsQuery");
|
|
1214
|
+
var CommandsQuerySchema = z.object({
|
|
1215
|
+
refresh: z.enum(["true", "false"]).optional(),
|
|
1216
|
+
cwd: z.string().optional()
|
|
1217
|
+
}).openapi("CommandsQuery");
|
|
1218
|
+
var TextDeltaSchema = z.object({
|
|
1219
|
+
text: z.string()
|
|
1220
|
+
}).openapi("TextDelta");
|
|
1221
|
+
var ToolCallStatusSchema = z.enum(["pending", "running", "complete", "error"]);
|
|
1222
|
+
var ToolCallEventSchema = z.object({
|
|
1223
|
+
toolCallId: z.string(),
|
|
1224
|
+
toolName: z.string(),
|
|
1225
|
+
input: z.string().optional(),
|
|
1226
|
+
result: z.string().optional(),
|
|
1227
|
+
status: ToolCallStatusSchema
|
|
1228
|
+
}).openapi("ToolCallEvent");
|
|
1229
|
+
var ApprovalEventSchema = z.object({
|
|
1230
|
+
toolCallId: z.string(),
|
|
1231
|
+
toolName: z.string(),
|
|
1232
|
+
input: z.string()
|
|
1233
|
+
}).openapi("ApprovalEvent");
|
|
1234
|
+
var QuestionPromptEventSchema = z.object({
|
|
1235
|
+
toolCallId: z.string(),
|
|
1236
|
+
questions: z.array(QuestionItemSchema)
|
|
1237
|
+
}).openapi("QuestionPromptEvent");
|
|
1238
|
+
var ErrorEventSchema = z.object({
|
|
1239
|
+
message: z.string(),
|
|
1240
|
+
code: z.string().optional()
|
|
1241
|
+
}).openapi("ErrorEvent");
|
|
1242
|
+
var DoneEventSchema = z.object({
|
|
1243
|
+
sessionId: z.string()
|
|
1244
|
+
}).openapi("DoneEvent");
|
|
1245
|
+
var SessionStatusEventSchema = z.object({
|
|
1246
|
+
sessionId: z.string(),
|
|
1247
|
+
model: z.string().optional(),
|
|
1248
|
+
costUsd: z.number().optional(),
|
|
1249
|
+
contextTokens: z.number().int().optional(),
|
|
1250
|
+
contextMaxTokens: z.number().int().optional()
|
|
1251
|
+
}).openapi("SessionStatusEvent");
|
|
1252
|
+
var TaskItemSchema = z.object({
|
|
1253
|
+
id: z.string(),
|
|
1254
|
+
subject: z.string(),
|
|
1255
|
+
description: z.string().optional(),
|
|
1256
|
+
activeForm: z.string().optional(),
|
|
1257
|
+
status: TaskStatusSchema,
|
|
1258
|
+
blockedBy: z.array(z.string()).optional(),
|
|
1259
|
+
blocks: z.array(z.string()).optional(),
|
|
1260
|
+
owner: z.string().optional()
|
|
1261
|
+
}).openapi("TaskItem");
|
|
1262
|
+
var TaskUpdateEventSchema = z.object({
|
|
1263
|
+
action: z.enum(["create", "update", "snapshot"]),
|
|
1264
|
+
task: TaskItemSchema
|
|
1265
|
+
}).openapi("TaskUpdateEvent");
|
|
1266
|
+
var SyncUpdateEventSchema = z.object({
|
|
1267
|
+
sessionId: z.string(),
|
|
1268
|
+
timestamp: z.string()
|
|
1269
|
+
}).openapi("SyncUpdateEvent");
|
|
1270
|
+
var SyncConnectedEventSchema = z.object({
|
|
1271
|
+
sessionId: z.string()
|
|
1272
|
+
}).openapi("SyncConnectedEvent");
|
|
1273
|
+
var StreamEventSchema = z.object({
|
|
1274
|
+
type: StreamEventTypeSchema,
|
|
1275
|
+
data: z.union([
|
|
1276
|
+
TextDeltaSchema,
|
|
1277
|
+
ToolCallEventSchema,
|
|
1278
|
+
ApprovalEventSchema,
|
|
1279
|
+
QuestionPromptEventSchema,
|
|
1280
|
+
ErrorEventSchema,
|
|
1281
|
+
DoneEventSchema,
|
|
1282
|
+
SessionStatusEventSchema,
|
|
1283
|
+
TaskUpdateEventSchema,
|
|
1284
|
+
SyncUpdateEventSchema,
|
|
1285
|
+
SyncConnectedEventSchema
|
|
1286
|
+
])
|
|
1287
|
+
}).openapi("StreamEvent");
|
|
1288
|
+
var TextPartSchema = z.object({
|
|
1289
|
+
type: z.literal("text"),
|
|
1290
|
+
text: z.string()
|
|
1291
|
+
}).openapi("TextPart");
|
|
1292
|
+
var ToolCallPartSchema = z.object({
|
|
1293
|
+
type: z.literal("tool_call"),
|
|
1294
|
+
toolCallId: z.string(),
|
|
1295
|
+
toolName: z.string(),
|
|
1296
|
+
input: z.string().optional(),
|
|
1297
|
+
result: z.string().optional(),
|
|
1298
|
+
status: ToolCallStatusSchema,
|
|
1299
|
+
interactiveType: z.enum(["approval", "question"]).optional(),
|
|
1300
|
+
questions: z.array(QuestionItemSchema).optional(),
|
|
1301
|
+
answers: z.record(z.string(), z.string()).optional()
|
|
1302
|
+
}).openapi("ToolCallPart");
|
|
1303
|
+
var MessagePartSchema = z.discriminatedUnion("type", [
|
|
1304
|
+
TextPartSchema,
|
|
1305
|
+
ToolCallPartSchema
|
|
1306
|
+
]);
|
|
1307
|
+
var MessageTypeSchema = z.enum(["command", "compaction"]).openapi("MessageType");
|
|
1308
|
+
var HistoryToolCallSchema = z.object({
|
|
1309
|
+
toolCallId: z.string(),
|
|
1310
|
+
toolName: z.string(),
|
|
1311
|
+
input: z.string().optional(),
|
|
1312
|
+
result: z.string().optional(),
|
|
1313
|
+
status: z.literal("complete"),
|
|
1314
|
+
questions: z.array(QuestionItemSchema).optional(),
|
|
1315
|
+
answers: z.record(z.string(), z.string()).optional()
|
|
1316
|
+
}).openapi("HistoryToolCall");
|
|
1317
|
+
var HistoryMessageSchema = z.object({
|
|
1318
|
+
id: z.string(),
|
|
1319
|
+
role: z.enum(["user", "assistant"]),
|
|
1320
|
+
content: z.string(),
|
|
1321
|
+
toolCalls: z.array(HistoryToolCallSchema).optional(),
|
|
1322
|
+
parts: z.array(MessagePartSchema).optional(),
|
|
1323
|
+
timestamp: z.string().optional(),
|
|
1324
|
+
messageType: MessageTypeSchema.optional(),
|
|
1325
|
+
commandName: z.string().optional(),
|
|
1326
|
+
commandArgs: z.string().optional()
|
|
1327
|
+
}).openapi("HistoryMessage");
|
|
1328
|
+
var CommandEntrySchema = z.object({
|
|
1329
|
+
namespace: z.string(),
|
|
1330
|
+
command: z.string(),
|
|
1331
|
+
fullCommand: z.string(),
|
|
1332
|
+
description: z.string(),
|
|
1333
|
+
argumentHint: z.string().optional(),
|
|
1334
|
+
allowedTools: z.array(z.string()).optional(),
|
|
1335
|
+
filePath: z.string()
|
|
1336
|
+
}).openapi("CommandEntry");
|
|
1337
|
+
var CommandRegistrySchema = z.object({
|
|
1338
|
+
commands: z.array(CommandEntrySchema),
|
|
1339
|
+
lastScanned: z.string()
|
|
1340
|
+
}).openapi("CommandRegistry");
|
|
1341
|
+
var FileListQuerySchema = z.object({
|
|
1342
|
+
cwd: z.string().min(1)
|
|
1343
|
+
}).openapi("FileListQuery");
|
|
1344
|
+
var FileListResponseSchema = z.object({
|
|
1345
|
+
files: z.array(z.string()),
|
|
1346
|
+
truncated: z.boolean(),
|
|
1347
|
+
total: z.number().int()
|
|
1348
|
+
}).openapi("FileListResponse");
|
|
1349
|
+
var BrowseDirectoryQuerySchema = z.object({
|
|
1350
|
+
path: z.string().min(1).optional(),
|
|
1351
|
+
showHidden: z.coerce.boolean().optional().default(false)
|
|
1352
|
+
}).openapi("BrowseDirectoryQuery");
|
|
1353
|
+
var DirectoryEntrySchema = z.object({
|
|
1354
|
+
name: z.string(),
|
|
1355
|
+
path: z.string(),
|
|
1356
|
+
isDirectory: z.boolean()
|
|
1357
|
+
}).openapi("DirectoryEntry");
|
|
1358
|
+
var BrowseDirectoryResponseSchema = z.object({
|
|
1359
|
+
path: z.string(),
|
|
1360
|
+
entries: z.array(DirectoryEntrySchema),
|
|
1361
|
+
parent: z.string().nullable()
|
|
1362
|
+
}).openapi("BrowseDirectoryResponse");
|
|
1363
|
+
var TunnelStatusSchema = z.object({
|
|
1364
|
+
connected: z.boolean(),
|
|
1365
|
+
url: z.string().nullable(),
|
|
1366
|
+
port: z.number().int().nullable(),
|
|
1367
|
+
startedAt: z.string().nullable()
|
|
1368
|
+
}).openapi("TunnelStatus");
|
|
1369
|
+
var HealthResponseSchema = z.object({
|
|
1370
|
+
status: z.string(),
|
|
1371
|
+
version: z.string(),
|
|
1372
|
+
uptime: z.number(),
|
|
1373
|
+
tunnel: TunnelStatusSchema.optional()
|
|
1374
|
+
}).openapi("HealthResponse");
|
|
1375
|
+
var ServerConfigSchema = z.object({
|
|
1376
|
+
version: z.string(),
|
|
1377
|
+
port: z.number().int(),
|
|
1378
|
+
uptime: z.number(),
|
|
1379
|
+
workingDirectory: z.string(),
|
|
1380
|
+
nodeVersion: z.string(),
|
|
1381
|
+
claudeCliPath: z.string().nullable(),
|
|
1382
|
+
tunnel: z.object({
|
|
1383
|
+
enabled: z.boolean(),
|
|
1384
|
+
connected: z.boolean(),
|
|
1385
|
+
url: z.string().nullable(),
|
|
1386
|
+
authEnabled: z.boolean(),
|
|
1387
|
+
tokenConfigured: z.boolean()
|
|
1388
|
+
})
|
|
1389
|
+
}).openapi("ServerConfig");
|
|
1390
|
+
var GitStatusResponseSchema = z.object({
|
|
1391
|
+
branch: z.string().describe("Current branch name or HEAD SHA if detached"),
|
|
1392
|
+
ahead: z.number().int().describe("Commits ahead of remote tracking branch"),
|
|
1393
|
+
behind: z.number().int().describe("Commits behind remote tracking branch"),
|
|
1394
|
+
modified: z.number().int().describe("Count of modified files (staged + unstaged)"),
|
|
1395
|
+
staged: z.number().int().describe("Count of staged files"),
|
|
1396
|
+
untracked: z.number().int().describe("Count of untracked files"),
|
|
1397
|
+
conflicted: z.number().int().describe("Count of files with merge conflicts"),
|
|
1398
|
+
clean: z.boolean().describe("True if working directory is clean"),
|
|
1399
|
+
detached: z.boolean().describe("True if HEAD is detached"),
|
|
1400
|
+
tracking: z.string().nullable().describe("Remote tracking branch name")
|
|
1401
|
+
}).openapi("GitStatusResponse");
|
|
1402
|
+
var GitStatusErrorSchema = z.object({
|
|
1403
|
+
error: z.literal("not_git_repo")
|
|
1404
|
+
}).openapi("GitStatusError");
|
|
1405
|
+
var ErrorResponseSchema = z.object({
|
|
1406
|
+
error: z.string(),
|
|
1407
|
+
details: z.any().optional()
|
|
1408
|
+
}).openapi("ErrorResponse");
|
|
1409
|
+
var SessionLockedErrorSchema = z.object({
|
|
1410
|
+
error: z.literal("Session locked"),
|
|
1411
|
+
code: z.literal("SESSION_LOCKED"),
|
|
1412
|
+
lockedBy: z.string(),
|
|
1413
|
+
lockedAt: z.string()
|
|
1414
|
+
}).openapi("SessionLockedError");
|
|
1415
|
+
|
|
1416
|
+
// ../../apps/server/src/routes/sessions.ts
|
|
1417
|
+
var __dirname2 = path3.dirname(fileURLToPath2(import.meta.url));
|
|
1418
|
+
var vaultRoot = path3.resolve(__dirname2, "../../../../");
|
|
1419
|
+
var router = Router();
|
|
1420
|
+
router.post("/", async (req, res) => {
|
|
1421
|
+
const parsed = CreateSessionRequestSchema.safeParse(req.body);
|
|
1422
|
+
if (!parsed.success) {
|
|
1423
|
+
return res.status(400).json({ error: "Invalid request", details: parsed.error.format() });
|
|
1424
|
+
}
|
|
1425
|
+
const { permissionMode = "default", cwd } = parsed.data;
|
|
1426
|
+
const sessionId = crypto.randomUUID();
|
|
1427
|
+
agentManager.ensureSession(sessionId, { permissionMode, cwd });
|
|
1428
|
+
res.json({
|
|
1429
|
+
id: sessionId,
|
|
1430
|
+
title: `New Session`,
|
|
1431
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1432
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1433
|
+
permissionMode,
|
|
1434
|
+
cwd
|
|
1435
|
+
});
|
|
1436
|
+
});
|
|
1437
|
+
router.get("/", async (req, res) => {
|
|
1438
|
+
const parsed = ListSessionsQuerySchema.safeParse(req.query);
|
|
1439
|
+
if (!parsed.success) {
|
|
1440
|
+
return res.status(400).json({ error: "Invalid query", details: parsed.error.format() });
|
|
1441
|
+
}
|
|
1442
|
+
const { limit, cwd } = parsed.data;
|
|
1443
|
+
const projectDir = cwd || vaultRoot;
|
|
1444
|
+
const sessions = await transcriptReader.listSessions(projectDir);
|
|
1445
|
+
res.json(sessions.slice(0, limit));
|
|
1446
|
+
});
|
|
1447
|
+
router.get("/:id", async (req, res) => {
|
|
1448
|
+
const cwd = req.query.cwd || vaultRoot;
|
|
1449
|
+
const session = await transcriptReader.getSession(cwd, req.params.id);
|
|
1450
|
+
if (!session) return res.status(404).json({ error: "Session not found" });
|
|
1451
|
+
res.json(session);
|
|
1452
|
+
});
|
|
1453
|
+
router.get("/:id/tasks", async (req, res) => {
|
|
1454
|
+
const cwd = req.query.cwd || vaultRoot;
|
|
1455
|
+
const etag = await transcriptReader.getTranscriptETag(cwd, req.params.id);
|
|
1456
|
+
if (etag) {
|
|
1457
|
+
res.setHeader("ETag", etag);
|
|
1458
|
+
if (req.headers["if-none-match"] === etag) {
|
|
1459
|
+
return res.status(304).end();
|
|
1460
|
+
}
|
|
1461
|
+
}
|
|
1462
|
+
try {
|
|
1463
|
+
const tasks = await transcriptReader.readTasks(cwd, req.params.id);
|
|
1464
|
+
res.json({ tasks });
|
|
1465
|
+
} catch {
|
|
1466
|
+
res.status(404).json({ error: "Session not found" });
|
|
1467
|
+
}
|
|
1468
|
+
});
|
|
1469
|
+
router.get("/:id/messages", async (req, res) => {
|
|
1470
|
+
const cwd = req.query.cwd || vaultRoot;
|
|
1471
|
+
const etag = await transcriptReader.getTranscriptETag(cwd, req.params.id);
|
|
1472
|
+
if (etag) {
|
|
1473
|
+
res.setHeader("ETag", etag);
|
|
1474
|
+
if (req.headers["if-none-match"] === etag) {
|
|
1475
|
+
return res.status(304).end();
|
|
1476
|
+
}
|
|
1477
|
+
}
|
|
1478
|
+
const messages = await transcriptReader.readTranscript(cwd, req.params.id);
|
|
1479
|
+
res.json({ messages });
|
|
1480
|
+
});
|
|
1481
|
+
router.patch("/:id", async (req, res) => {
|
|
1482
|
+
const parsed = UpdateSessionRequestSchema.safeParse(req.body);
|
|
1483
|
+
if (!parsed.success) {
|
|
1484
|
+
return res.status(400).json({ error: "Invalid request", details: parsed.error.format() });
|
|
1485
|
+
}
|
|
1486
|
+
const { permissionMode, model } = parsed.data;
|
|
1487
|
+
const updated = agentManager.updateSession(req.params.id, { permissionMode, model });
|
|
1488
|
+
if (!updated) return res.status(404).json({ error: "Session not found" });
|
|
1489
|
+
const cwd = req.query.cwd || vaultRoot;
|
|
1490
|
+
const session = await transcriptReader.getSession(cwd, req.params.id);
|
|
1491
|
+
if (session) {
|
|
1492
|
+
session.permissionMode = permissionMode ?? session.permissionMode;
|
|
1493
|
+
session.model = model ?? session.model;
|
|
1494
|
+
}
|
|
1495
|
+
res.json(session ?? { id: req.params.id, permissionMode, model });
|
|
1496
|
+
});
|
|
1497
|
+
router.post("/:id/messages", async (req, res) => {
|
|
1498
|
+
const parsed = SendMessageRequestSchema.safeParse(req.body);
|
|
1499
|
+
if (!parsed.success) {
|
|
1500
|
+
return res.status(400).json({ error: "Invalid request", details: parsed.error.format() });
|
|
1501
|
+
}
|
|
1502
|
+
const { content, cwd } = parsed.data;
|
|
1503
|
+
const sessionId = req.params.id;
|
|
1504
|
+
const clientId = req.headers["x-client-id"] || crypto.randomUUID();
|
|
1505
|
+
const lockAcquired = agentManager.acquireLock(sessionId, clientId, res);
|
|
1506
|
+
if (!lockAcquired) {
|
|
1507
|
+
const lockInfo = agentManager.getLockInfo(sessionId);
|
|
1508
|
+
return res.status(409).json({
|
|
1509
|
+
error: "Session locked",
|
|
1510
|
+
code: "SESSION_LOCKED",
|
|
1511
|
+
lockedBy: lockInfo?.clientId ?? "unknown",
|
|
1512
|
+
lockedAt: lockInfo ? new Date(lockInfo.acquiredAt).toISOString() : (/* @__PURE__ */ new Date()).toISOString()
|
|
1513
|
+
});
|
|
1514
|
+
}
|
|
1515
|
+
res.on("close", () => {
|
|
1516
|
+
agentManager.releaseLock(sessionId, clientId);
|
|
1517
|
+
});
|
|
1518
|
+
initSSEStream(res);
|
|
1519
|
+
try {
|
|
1520
|
+
for await (const event of agentManager.sendMessage(sessionId, content, { cwd })) {
|
|
1521
|
+
sendSSEEvent(res, event);
|
|
1522
|
+
if (event.type === "done") {
|
|
1523
|
+
const actualSdkId = agentManager.getSdkSessionId(sessionId);
|
|
1524
|
+
if (actualSdkId && actualSdkId !== sessionId) {
|
|
1525
|
+
sendSSEEvent(res, {
|
|
1526
|
+
type: "done",
|
|
1527
|
+
data: { sessionId: actualSdkId }
|
|
1528
|
+
});
|
|
1529
|
+
}
|
|
1530
|
+
}
|
|
1531
|
+
}
|
|
1532
|
+
} catch (err) {
|
|
1533
|
+
sendSSEEvent(res, {
|
|
1534
|
+
type: "error",
|
|
1535
|
+
data: { message: err instanceof Error ? err.message : "Unknown error" }
|
|
1536
|
+
});
|
|
1537
|
+
} finally {
|
|
1538
|
+
agentManager.releaseLock(sessionId, clientId);
|
|
1539
|
+
endSSEStream(res);
|
|
1540
|
+
}
|
|
1541
|
+
});
|
|
1542
|
+
router.post("/:id/approve", async (req, res) => {
|
|
1543
|
+
const parsed = ApprovalRequestSchema.safeParse(req.body);
|
|
1544
|
+
if (!parsed.success) {
|
|
1545
|
+
return res.status(400).json({ error: "Invalid request", details: parsed.error.format() });
|
|
1546
|
+
}
|
|
1547
|
+
const { toolCallId } = parsed.data;
|
|
1548
|
+
const approved = agentManager.approveTool(req.params.id, toolCallId, true);
|
|
1549
|
+
if (!approved) return res.status(404).json({ error: "No pending approval" });
|
|
1550
|
+
res.json({ ok: true });
|
|
1551
|
+
});
|
|
1552
|
+
router.post("/:id/deny", async (req, res) => {
|
|
1553
|
+
const parsed = ApprovalRequestSchema.safeParse(req.body);
|
|
1554
|
+
if (!parsed.success) {
|
|
1555
|
+
return res.status(400).json({ error: "Invalid request", details: parsed.error.format() });
|
|
1556
|
+
}
|
|
1557
|
+
const { toolCallId } = parsed.data;
|
|
1558
|
+
const denied = agentManager.approveTool(req.params.id, toolCallId, false);
|
|
1559
|
+
if (!denied) return res.status(404).json({ error: "No pending approval" });
|
|
1560
|
+
res.json({ ok: true });
|
|
1561
|
+
});
|
|
1562
|
+
router.post("/:id/submit-answers", async (req, res) => {
|
|
1563
|
+
const parsed = SubmitAnswersRequestSchema.safeParse(req.body);
|
|
1564
|
+
if (!parsed.success) {
|
|
1565
|
+
return res.status(400).json({ error: "Invalid request", details: parsed.error.format() });
|
|
1566
|
+
}
|
|
1567
|
+
const { toolCallId, answers } = parsed.data;
|
|
1568
|
+
const ok = agentManager.submitAnswers(req.params.id, toolCallId, answers);
|
|
1569
|
+
if (!ok) return res.status(404).json({ error: "No pending question" });
|
|
1570
|
+
res.json({ ok: true });
|
|
1571
|
+
});
|
|
1572
|
+
router.get("/:id/stream", (req, res) => {
|
|
1573
|
+
const sessionId = req.params.id;
|
|
1574
|
+
const cwd = req.query.cwd || vaultRoot;
|
|
1575
|
+
const sessionBroadcaster2 = req.app.locals.sessionBroadcaster;
|
|
1576
|
+
res.setHeader("Content-Type", "text/event-stream");
|
|
1577
|
+
res.setHeader("Cache-Control", "no-cache");
|
|
1578
|
+
res.setHeader("Connection", "keep-alive");
|
|
1579
|
+
res.setHeader("X-Accel-Buffering", "no");
|
|
1580
|
+
res.flushHeaders();
|
|
1581
|
+
sessionBroadcaster2.registerClient(sessionId, cwd, res);
|
|
1582
|
+
});
|
|
1583
|
+
var sessions_default = router;
|
|
1584
|
+
|
|
1585
|
+
// ../../apps/server/src/routes/commands.ts
|
|
1586
|
+
import { Router as Router2 } from "express";
|
|
1587
|
+
|
|
1588
|
+
// ../../apps/server/src/services/command-registry.ts
|
|
1589
|
+
import fs2 from "fs/promises";
|
|
1590
|
+
import path4 from "path";
|
|
1591
|
+
import matter from "gray-matter";
|
|
1592
|
+
function parseFrontmatterFallback(content) {
|
|
1593
|
+
const match = content.match(/^---\n([\s\S]*?)\n---/);
|
|
1594
|
+
if (!match) return {};
|
|
1595
|
+
const result = {};
|
|
1596
|
+
for (const line of match[1].split("\n")) {
|
|
1597
|
+
const colonIdx = line.indexOf(":");
|
|
1598
|
+
if (colonIdx === -1) continue;
|
|
1599
|
+
const key = line.slice(0, colonIdx).trim();
|
|
1600
|
+
const val = line.slice(colonIdx + 1).trim();
|
|
1601
|
+
if (key && val) result[key] = val;
|
|
1602
|
+
}
|
|
1603
|
+
return result;
|
|
1604
|
+
}
|
|
1605
|
+
var CommandRegistryService = class {
|
|
1606
|
+
cache = null;
|
|
1607
|
+
commandsDir;
|
|
1608
|
+
constructor(vaultRoot2) {
|
|
1609
|
+
this.commandsDir = path4.join(vaultRoot2, ".claude", "commands");
|
|
1610
|
+
}
|
|
1611
|
+
async getCommands(forceRefresh = false) {
|
|
1612
|
+
if (this.cache && !forceRefresh) return this.cache;
|
|
1613
|
+
const commands = [];
|
|
1614
|
+
try {
|
|
1615
|
+
const entries = await fs2.readdir(this.commandsDir, {
|
|
1616
|
+
withFileTypes: true
|
|
1617
|
+
});
|
|
1618
|
+
for (const entry of entries) {
|
|
1619
|
+
if (!entry.isDirectory()) continue;
|
|
1620
|
+
const nsPath = path4.join(this.commandsDir, entry.name);
|
|
1621
|
+
const files = await fs2.readdir(nsPath);
|
|
1622
|
+
for (const file of files) {
|
|
1623
|
+
if (!file.endsWith(".md")) continue;
|
|
1624
|
+
const filePath = path4.join(nsPath, file);
|
|
1625
|
+
try {
|
|
1626
|
+
const content = await fs2.readFile(filePath, "utf-8");
|
|
1627
|
+
let frontmatter;
|
|
1628
|
+
try {
|
|
1629
|
+
frontmatter = matter(content).data;
|
|
1630
|
+
} catch {
|
|
1631
|
+
frontmatter = parseFrontmatterFallback(content);
|
|
1632
|
+
}
|
|
1633
|
+
const commandName = file.replace(".md", "");
|
|
1634
|
+
const allowedToolsRaw = frontmatter["allowed-tools"];
|
|
1635
|
+
commands.push({
|
|
1636
|
+
namespace: entry.name,
|
|
1637
|
+
command: commandName,
|
|
1638
|
+
fullCommand: `/${entry.name}:${commandName}`,
|
|
1639
|
+
description: frontmatter.description || "",
|
|
1640
|
+
argumentHint: frontmatter["argument-hint"],
|
|
1641
|
+
allowedTools: typeof allowedToolsRaw === "string" ? allowedToolsRaw.split(",").map((t) => t.trim()) : allowedToolsRaw,
|
|
1642
|
+
filePath: path4.relative(process.cwd(), filePath)
|
|
1643
|
+
});
|
|
1644
|
+
} catch (fileErr) {
|
|
1645
|
+
console.warn(`[CommandRegistry] Skipping ${entry.name}/${file}: ${fileErr.message}`);
|
|
1646
|
+
}
|
|
1647
|
+
}
|
|
1648
|
+
}
|
|
1649
|
+
} catch (err) {
|
|
1650
|
+
console.warn("[CommandRegistry] Could not read commands directory:", err.message);
|
|
1651
|
+
}
|
|
1652
|
+
commands.sort((a, b) => a.fullCommand.localeCompare(b.fullCommand));
|
|
1653
|
+
this.cache = { commands, lastScanned: (/* @__PURE__ */ new Date()).toISOString() };
|
|
1654
|
+
return this.cache;
|
|
1655
|
+
}
|
|
1656
|
+
invalidateCache() {
|
|
1657
|
+
this.cache = null;
|
|
1658
|
+
}
|
|
1659
|
+
};
|
|
1660
|
+
|
|
1661
|
+
// ../../apps/server/src/routes/commands.ts
|
|
1662
|
+
import path5 from "path";
|
|
1663
|
+
import { fileURLToPath as fileURLToPath3 } from "url";
|
|
1664
|
+
var __dirname3 = path5.dirname(fileURLToPath3(import.meta.url));
|
|
1665
|
+
var defaultRoot = process.env.GATEWAY_CWD ?? path5.resolve(__dirname3, "../../../../");
|
|
1666
|
+
var registryCache = /* @__PURE__ */ new Map();
|
|
1667
|
+
function getRegistry(cwd) {
|
|
1668
|
+
const root = cwd || defaultRoot;
|
|
1669
|
+
let registry2 = registryCache.get(root);
|
|
1670
|
+
if (!registry2) {
|
|
1671
|
+
registry2 = new CommandRegistryService(root);
|
|
1672
|
+
registryCache.set(root, registry2);
|
|
1673
|
+
}
|
|
1674
|
+
return registry2;
|
|
1675
|
+
}
|
|
1676
|
+
var router2 = Router2();
|
|
1677
|
+
router2.get("/", async (req, res) => {
|
|
1678
|
+
const parsed = CommandsQuerySchema.safeParse(req.query);
|
|
1679
|
+
if (!parsed.success) {
|
|
1680
|
+
return res.status(400).json({ error: "Invalid query", details: parsed.error.format() });
|
|
1681
|
+
}
|
|
1682
|
+
const refresh = parsed.data.refresh === "true";
|
|
1683
|
+
const registry2 = getRegistry(parsed.data.cwd);
|
|
1684
|
+
const commands = await registry2.getCommands(refresh);
|
|
1685
|
+
res.json(commands);
|
|
1686
|
+
});
|
|
1687
|
+
var commands_default = router2;
|
|
1688
|
+
|
|
1689
|
+
// ../../apps/server/src/routes/health.ts
|
|
1690
|
+
import { Router as Router3 } from "express";
|
|
1691
|
+
|
|
1692
|
+
// ../../apps/server/src/services/tunnel-manager.ts
|
|
1693
|
+
var TunnelManager = class {
|
|
1694
|
+
listener = null;
|
|
1695
|
+
_status = {
|
|
1696
|
+
enabled: false,
|
|
1697
|
+
connected: false,
|
|
1698
|
+
url: null,
|
|
1699
|
+
port: null,
|
|
1700
|
+
startedAt: null
|
|
1701
|
+
};
|
|
1702
|
+
get status() {
|
|
1703
|
+
return { ...this._status };
|
|
1704
|
+
}
|
|
1705
|
+
async start(config) {
|
|
1706
|
+
if (this.listener) throw new Error("Tunnel is already running");
|
|
1707
|
+
const ngrok = await import("@ngrok/ngrok");
|
|
1708
|
+
const forwardOpts = {
|
|
1709
|
+
addr: config.port,
|
|
1710
|
+
authtoken_from_env: true
|
|
1711
|
+
};
|
|
1712
|
+
if (config.authtoken) {
|
|
1713
|
+
forwardOpts.authtoken = config.authtoken;
|
|
1714
|
+
delete forwardOpts.authtoken_from_env;
|
|
1715
|
+
}
|
|
1716
|
+
if (config.basicAuth) forwardOpts.basic_auth = [config.basicAuth];
|
|
1717
|
+
if (config.domain) forwardOpts.domain = config.domain;
|
|
1718
|
+
this.listener = await ngrok.forward(forwardOpts);
|
|
1719
|
+
const url = this.listener.url() ?? "";
|
|
1720
|
+
this._status = {
|
|
1721
|
+
enabled: true,
|
|
1722
|
+
connected: true,
|
|
1723
|
+
url,
|
|
1724
|
+
port: config.port,
|
|
1725
|
+
startedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1726
|
+
};
|
|
1727
|
+
return url;
|
|
1728
|
+
}
|
|
1729
|
+
async stop() {
|
|
1730
|
+
if (this.listener) {
|
|
1731
|
+
await this.listener.close();
|
|
1732
|
+
this.listener = null;
|
|
1733
|
+
}
|
|
1734
|
+
this._status = {
|
|
1735
|
+
enabled: this._status.enabled,
|
|
1736
|
+
connected: false,
|
|
1737
|
+
url: null,
|
|
1738
|
+
port: this._status.port,
|
|
1739
|
+
startedAt: this._status.startedAt
|
|
1740
|
+
};
|
|
1741
|
+
}
|
|
1742
|
+
};
|
|
1743
|
+
var tunnelManager = new TunnelManager();
|
|
1744
|
+
|
|
1745
|
+
// ../../apps/server/src/routes/health.ts
|
|
1746
|
+
var router3 = Router3();
|
|
1747
|
+
router3.get("/", (_req, res) => {
|
|
1748
|
+
const response = {
|
|
1749
|
+
status: "ok",
|
|
1750
|
+
version: "1.0.0",
|
|
1751
|
+
uptime: process.uptime()
|
|
1752
|
+
};
|
|
1753
|
+
const tunnelStatus = tunnelManager.status;
|
|
1754
|
+
if (tunnelStatus.enabled) {
|
|
1755
|
+
response.tunnel = {
|
|
1756
|
+
connected: tunnelStatus.connected,
|
|
1757
|
+
url: tunnelStatus.url,
|
|
1758
|
+
port: tunnelStatus.port,
|
|
1759
|
+
startedAt: tunnelStatus.startedAt
|
|
1760
|
+
};
|
|
1761
|
+
}
|
|
1762
|
+
res.json(response);
|
|
1763
|
+
});
|
|
1764
|
+
var health_default = router3;
|
|
1765
|
+
|
|
1766
|
+
// ../../apps/server/src/routes/directory.ts
|
|
1767
|
+
import { Router as Router4 } from "express";
|
|
1768
|
+
import fs3 from "fs/promises";
|
|
1769
|
+
import path6 from "path";
|
|
1770
|
+
import os2 from "os";
|
|
1771
|
+
var router4 = Router4();
|
|
1772
|
+
var HOME = os2.homedir();
|
|
1773
|
+
router4.get("/", async (req, res) => {
|
|
1774
|
+
const parsed = BrowseDirectoryQuerySchema.safeParse(req.query);
|
|
1775
|
+
if (!parsed.success) {
|
|
1776
|
+
return res.status(400).json({ error: "Invalid query", details: parsed.error.format() });
|
|
1777
|
+
}
|
|
1778
|
+
const { path: userPath, showHidden } = parsed.data;
|
|
1779
|
+
const targetPath = userPath || HOME;
|
|
1780
|
+
if (targetPath.includes("\0")) {
|
|
1781
|
+
return res.status(400).json({ error: "Invalid path" });
|
|
1782
|
+
}
|
|
1783
|
+
let resolved;
|
|
1784
|
+
try {
|
|
1785
|
+
resolved = await fs3.realpath(targetPath);
|
|
1786
|
+
} catch (err) {
|
|
1787
|
+
const code = err.code;
|
|
1788
|
+
if (code === "ENOENT") return res.status(404).json({ error: "Directory not found" });
|
|
1789
|
+
if (code === "EACCES") return res.status(403).json({ error: "Permission denied" });
|
|
1790
|
+
throw err;
|
|
1791
|
+
}
|
|
1792
|
+
if (!resolved.startsWith(HOME)) {
|
|
1793
|
+
return res.status(403).json({ error: "Access denied: path outside home directory" });
|
|
1794
|
+
}
|
|
1795
|
+
let dirents;
|
|
1796
|
+
try {
|
|
1797
|
+
dirents = await fs3.readdir(resolved, { withFileTypes: true });
|
|
1798
|
+
} catch (err) {
|
|
1799
|
+
const code = err.code;
|
|
1800
|
+
if (code === "EACCES") return res.status(403).json({ error: "Permission denied" });
|
|
1801
|
+
if (code === "ENOTDIR") return res.status(400).json({ error: "Not a directory" });
|
|
1802
|
+
throw err;
|
|
1803
|
+
}
|
|
1804
|
+
const entries = dirents.filter((d) => d.isDirectory()).filter((d) => showHidden || !d.name.startsWith(".")).map((d) => ({
|
|
1805
|
+
name: d.name,
|
|
1806
|
+
path: path6.join(resolved, d.name),
|
|
1807
|
+
isDirectory: true
|
|
1808
|
+
})).sort((a, b) => a.name.localeCompare(b.name));
|
|
1809
|
+
const parent = path6.dirname(resolved);
|
|
1810
|
+
const hasParent = parent !== resolved && parent.startsWith(HOME);
|
|
1811
|
+
res.json({
|
|
1812
|
+
path: resolved,
|
|
1813
|
+
entries,
|
|
1814
|
+
parent: hasParent ? parent : null
|
|
1815
|
+
});
|
|
1816
|
+
});
|
|
1817
|
+
router4.get("/default", (_req, res) => {
|
|
1818
|
+
res.json({ path: process.cwd() });
|
|
1819
|
+
});
|
|
1820
|
+
var directory_default = router4;
|
|
1821
|
+
|
|
1822
|
+
// ../../apps/server/src/routes/config.ts
|
|
1823
|
+
import { Router as Router5 } from "express";
|
|
1824
|
+
var router5 = Router5();
|
|
1825
|
+
router5.get("/", (_req, res) => {
|
|
1826
|
+
let claudeCliPath = null;
|
|
1827
|
+
try {
|
|
1828
|
+
claudeCliPath = resolveClaudeCliPath() ?? null;
|
|
1829
|
+
} catch {
|
|
1830
|
+
}
|
|
1831
|
+
const tunnel = tunnelManager.status;
|
|
1832
|
+
res.json({
|
|
1833
|
+
version: "1.0.0",
|
|
1834
|
+
port: parseInt(process.env.GATEWAY_PORT || "6942", 10),
|
|
1835
|
+
uptime: process.uptime(),
|
|
1836
|
+
workingDirectory: process.cwd(),
|
|
1837
|
+
nodeVersion: process.version,
|
|
1838
|
+
claudeCliPath,
|
|
1839
|
+
tunnel: {
|
|
1840
|
+
enabled: tunnel.enabled,
|
|
1841
|
+
connected: tunnel.connected,
|
|
1842
|
+
url: tunnel.url,
|
|
1843
|
+
authEnabled: !!process.env.TUNNEL_AUTH,
|
|
1844
|
+
tokenConfigured: !!process.env.NGROK_AUTHTOKEN
|
|
1845
|
+
}
|
|
1846
|
+
});
|
|
1847
|
+
});
|
|
1848
|
+
var config_default = router5;
|
|
1849
|
+
|
|
1850
|
+
// ../../apps/server/src/routes/files.ts
|
|
1851
|
+
import { Router as Router6 } from "express";
|
|
1852
|
+
|
|
1853
|
+
// ../../apps/server/src/services/file-lister.ts
|
|
1854
|
+
import { execFile } from "child_process";
|
|
1855
|
+
import { promisify } from "util";
|
|
1856
|
+
import fs4 from "fs/promises";
|
|
1857
|
+
import path7 from "path";
|
|
1858
|
+
var execFileAsync = promisify(execFile);
|
|
1859
|
+
var EXCLUDED_DIRS = /* @__PURE__ */ new Set([
|
|
1860
|
+
"node_modules",
|
|
1861
|
+
".git",
|
|
1862
|
+
"dist",
|
|
1863
|
+
"build",
|
|
1864
|
+
".next",
|
|
1865
|
+
"coverage",
|
|
1866
|
+
"__pycache__",
|
|
1867
|
+
".cache"
|
|
1868
|
+
]);
|
|
1869
|
+
var MAX_FILES = 1e4;
|
|
1870
|
+
var CACHE_TTL = 5 * 60 * 1e3;
|
|
1871
|
+
var FileListService = class {
|
|
1872
|
+
cache = /* @__PURE__ */ new Map();
|
|
1873
|
+
async listFiles(cwd) {
|
|
1874
|
+
const cached = this.cache.get(cwd);
|
|
1875
|
+
if (cached && Date.now() - cached.timestamp < CACHE_TTL) {
|
|
1876
|
+
return { files: cached.files, truncated: cached.files.length >= MAX_FILES, total: cached.files.length };
|
|
1877
|
+
}
|
|
1878
|
+
let files;
|
|
1879
|
+
try {
|
|
1880
|
+
files = await this.listViaGit(cwd);
|
|
1881
|
+
} catch {
|
|
1882
|
+
files = await this.listViaReaddir(cwd);
|
|
1883
|
+
}
|
|
1884
|
+
const truncated = files.length > MAX_FILES;
|
|
1885
|
+
if (truncated) files = files.slice(0, MAX_FILES);
|
|
1886
|
+
this.cache.set(cwd, { files, timestamp: Date.now() });
|
|
1887
|
+
return { files, truncated, total: files.length };
|
|
1888
|
+
}
|
|
1889
|
+
async listViaGit(cwd) {
|
|
1890
|
+
const { stdout } = await execFileAsync("git", ["ls-files", "--cached", "--others", "--exclude-standard"], {
|
|
1891
|
+
cwd,
|
|
1892
|
+
maxBuffer: 10 * 1024 * 1024
|
|
1893
|
+
});
|
|
1894
|
+
return stdout.split("\n").filter(Boolean);
|
|
1895
|
+
}
|
|
1896
|
+
async listViaReaddir(cwd, prefix = "", depth = 0) {
|
|
1897
|
+
if (depth > 8) return [];
|
|
1898
|
+
const results = [];
|
|
1899
|
+
const entries = await fs4.readdir(path7.join(cwd, prefix), { withFileTypes: true });
|
|
1900
|
+
for (const entry of entries) {
|
|
1901
|
+
if (entry.name.startsWith(".") && entry.name !== ".") continue;
|
|
1902
|
+
const rel = prefix ? `${prefix}/${entry.name}` : entry.name;
|
|
1903
|
+
if (entry.isDirectory()) {
|
|
1904
|
+
if (EXCLUDED_DIRS.has(entry.name)) continue;
|
|
1905
|
+
results.push(...await this.listViaReaddir(cwd, rel, depth + 1));
|
|
1906
|
+
} else {
|
|
1907
|
+
results.push(rel);
|
|
1908
|
+
}
|
|
1909
|
+
if (results.length >= MAX_FILES) break;
|
|
1910
|
+
}
|
|
1911
|
+
return results;
|
|
1912
|
+
}
|
|
1913
|
+
invalidateCache(cwd) {
|
|
1914
|
+
if (cwd) this.cache.delete(cwd);
|
|
1915
|
+
else this.cache.clear();
|
|
1916
|
+
}
|
|
1917
|
+
};
|
|
1918
|
+
var fileLister = new FileListService();
|
|
1919
|
+
|
|
1920
|
+
// ../../apps/server/src/routes/files.ts
|
|
1921
|
+
var router6 = Router6();
|
|
1922
|
+
router6.get("/", async (req, res) => {
|
|
1923
|
+
const parsed = FileListQuerySchema.safeParse(req.query);
|
|
1924
|
+
if (!parsed.success) {
|
|
1925
|
+
return res.status(400).json({ error: "Invalid query", details: parsed.error.format() });
|
|
1926
|
+
}
|
|
1927
|
+
const result = await fileLister.listFiles(parsed.data.cwd);
|
|
1928
|
+
res.json(result);
|
|
1929
|
+
});
|
|
1930
|
+
var files_default = router6;
|
|
1931
|
+
|
|
1932
|
+
// ../../apps/server/src/routes/git.ts
|
|
1933
|
+
import { Router as Router7 } from "express";
|
|
1934
|
+
import { z as z2 } from "zod";
|
|
1935
|
+
|
|
1936
|
+
// ../../apps/server/src/services/git-status.ts
|
|
1937
|
+
import { execFile as execFile2 } from "node:child_process";
|
|
1938
|
+
import { promisify as promisify2 } from "node:util";
|
|
1939
|
+
var execFileAsync2 = promisify2(execFile2);
|
|
1940
|
+
async function getGitStatus(cwd) {
|
|
1941
|
+
try {
|
|
1942
|
+
const { stdout } = await execFileAsync2("git", ["status", "--porcelain=v1", "--branch"], {
|
|
1943
|
+
cwd,
|
|
1944
|
+
timeout: 5e3
|
|
1945
|
+
});
|
|
1946
|
+
return parsePorcelainOutput(stdout);
|
|
1947
|
+
} catch {
|
|
1948
|
+
return { error: "not_git_repo" };
|
|
1949
|
+
}
|
|
1950
|
+
}
|
|
1951
|
+
var CONFLICT_CODES = /* @__PURE__ */ new Set(["UU", "AA", "DD", "AU", "UA", "DU", "UD"]);
|
|
1952
|
+
var STAGED_CODES = /* @__PURE__ */ new Set(["M", "A", "D", "R", "C"]);
|
|
1953
|
+
function parsePorcelainOutput(stdout) {
|
|
1954
|
+
const lines = stdout.split("\n").filter(Boolean);
|
|
1955
|
+
let branch = "";
|
|
1956
|
+
let tracking = null;
|
|
1957
|
+
let ahead = 0;
|
|
1958
|
+
let behind = 0;
|
|
1959
|
+
let detached = false;
|
|
1960
|
+
let modified = 0;
|
|
1961
|
+
let staged = 0;
|
|
1962
|
+
let untracked = 0;
|
|
1963
|
+
let conflicted = 0;
|
|
1964
|
+
for (const line of lines) {
|
|
1965
|
+
if (line.startsWith("## ")) {
|
|
1966
|
+
const branchLine = line.slice(3);
|
|
1967
|
+
if (branchLine.startsWith("HEAD (no branch)")) {
|
|
1968
|
+
detached = true;
|
|
1969
|
+
branch = "HEAD";
|
|
1970
|
+
continue;
|
|
1971
|
+
}
|
|
1972
|
+
const bracketMatch = branchLine.match(/\[(.+)\]/);
|
|
1973
|
+
if (bracketMatch) {
|
|
1974
|
+
const info = bracketMatch[1];
|
|
1975
|
+
const aheadMatch = info.match(/ahead (\d+)/);
|
|
1976
|
+
const behindMatch = info.match(/behind (\d+)/);
|
|
1977
|
+
if (aheadMatch) ahead = parseInt(aheadMatch[1], 10);
|
|
1978
|
+
if (behindMatch) behind = parseInt(behindMatch[1], 10);
|
|
1979
|
+
}
|
|
1980
|
+
const branchPart = branchLine.replace(/\s*\[.*\]/, "");
|
|
1981
|
+
const dotIndex = branchPart.indexOf("...");
|
|
1982
|
+
if (dotIndex !== -1) {
|
|
1983
|
+
branch = branchPart.slice(0, dotIndex);
|
|
1984
|
+
tracking = branchPart.slice(dotIndex + 3);
|
|
1985
|
+
} else {
|
|
1986
|
+
branch = branchPart;
|
|
1987
|
+
}
|
|
1988
|
+
continue;
|
|
1989
|
+
}
|
|
1990
|
+
const x = line[0];
|
|
1991
|
+
const y = line[1];
|
|
1992
|
+
const code = `${x}${y}`;
|
|
1993
|
+
if (code === "??") {
|
|
1994
|
+
untracked++;
|
|
1995
|
+
} else if (CONFLICT_CODES.has(code)) {
|
|
1996
|
+
conflicted++;
|
|
1997
|
+
} else {
|
|
1998
|
+
if (STAGED_CODES.has(x)) staged++;
|
|
1999
|
+
if (y === "M" || y === "D") modified++;
|
|
2000
|
+
}
|
|
2001
|
+
}
|
|
2002
|
+
const clean = modified === 0 && staged === 0 && untracked === 0 && conflicted === 0;
|
|
2003
|
+
return { branch, ahead, behind, modified, staged, untracked, conflicted, clean, detached, tracking };
|
|
2004
|
+
}
|
|
2005
|
+
|
|
2006
|
+
// ../../apps/server/src/routes/git.ts
|
|
2007
|
+
var router7 = Router7();
|
|
2008
|
+
var GitStatusQuerySchema = z2.object({
|
|
2009
|
+
dir: z2.string().optional()
|
|
2010
|
+
});
|
|
2011
|
+
router7.get("/status", async (req, res) => {
|
|
2012
|
+
const parsed = GitStatusQuerySchema.safeParse(req.query);
|
|
2013
|
+
if (!parsed.success) {
|
|
2014
|
+
return res.status(400).json({ error: "Invalid query", details: parsed.error.format() });
|
|
2015
|
+
}
|
|
2016
|
+
const cwd = parsed.data.dir || process.cwd();
|
|
2017
|
+
const result = await getGitStatus(cwd);
|
|
2018
|
+
res.json(result);
|
|
2019
|
+
});
|
|
2020
|
+
var git_default = router7;
|
|
2021
|
+
|
|
2022
|
+
// ../../apps/server/src/services/openapi-registry.ts
|
|
2023
|
+
import {
|
|
2024
|
+
OpenAPIRegistry,
|
|
2025
|
+
OpenApiGeneratorV31
|
|
2026
|
+
} from "@asteasolutions/zod-to-openapi";
|
|
2027
|
+
import { z as z3 } from "zod";
|
|
2028
|
+
var registry = new OpenAPIRegistry();
|
|
2029
|
+
registry.registerPath({
|
|
2030
|
+
method: "get",
|
|
2031
|
+
path: "/api/health",
|
|
2032
|
+
tags: ["Health"],
|
|
2033
|
+
summary: "Health check",
|
|
2034
|
+
responses: {
|
|
2035
|
+
200: {
|
|
2036
|
+
description: "Server is healthy",
|
|
2037
|
+
content: { "application/json": { schema: HealthResponseSchema } }
|
|
2038
|
+
}
|
|
2039
|
+
}
|
|
2040
|
+
});
|
|
2041
|
+
registry.registerPath({
|
|
2042
|
+
method: "post",
|
|
2043
|
+
path: "/api/sessions",
|
|
2044
|
+
tags: ["Sessions"],
|
|
2045
|
+
summary: "Create a new session",
|
|
2046
|
+
request: {
|
|
2047
|
+
body: {
|
|
2048
|
+
content: { "application/json": { schema: CreateSessionRequestSchema } }
|
|
2049
|
+
}
|
|
2050
|
+
},
|
|
2051
|
+
responses: {
|
|
2052
|
+
200: {
|
|
2053
|
+
description: "Created session",
|
|
2054
|
+
content: { "application/json": { schema: SessionSchema } }
|
|
2055
|
+
},
|
|
2056
|
+
400: {
|
|
2057
|
+
description: "Validation error",
|
|
2058
|
+
content: { "application/json": { schema: ErrorResponseSchema } }
|
|
2059
|
+
}
|
|
2060
|
+
}
|
|
2061
|
+
});
|
|
2062
|
+
registry.registerPath({
|
|
2063
|
+
method: "get",
|
|
2064
|
+
path: "/api/sessions",
|
|
2065
|
+
tags: ["Sessions"],
|
|
2066
|
+
summary: "List all sessions",
|
|
2067
|
+
request: {
|
|
2068
|
+
query: ListSessionsQuerySchema
|
|
2069
|
+
},
|
|
2070
|
+
responses: {
|
|
2071
|
+
200: {
|
|
2072
|
+
description: "Array of sessions",
|
|
2073
|
+
content: {
|
|
2074
|
+
"application/json": { schema: z3.array(SessionSchema) }
|
|
2075
|
+
}
|
|
2076
|
+
},
|
|
2077
|
+
400: {
|
|
2078
|
+
description: "Validation error",
|
|
2079
|
+
content: { "application/json": { schema: ErrorResponseSchema } }
|
|
2080
|
+
}
|
|
2081
|
+
}
|
|
2082
|
+
});
|
|
2083
|
+
registry.registerPath({
|
|
2084
|
+
method: "get",
|
|
2085
|
+
path: "/api/sessions/{id}",
|
|
2086
|
+
tags: ["Sessions"],
|
|
2087
|
+
summary: "Get session details",
|
|
2088
|
+
request: {
|
|
2089
|
+
params: z3.object({ id: z3.string().uuid() })
|
|
2090
|
+
},
|
|
2091
|
+
responses: {
|
|
2092
|
+
200: {
|
|
2093
|
+
description: "Session details",
|
|
2094
|
+
content: { "application/json": { schema: SessionSchema } }
|
|
2095
|
+
},
|
|
2096
|
+
404: {
|
|
2097
|
+
description: "Session not found",
|
|
2098
|
+
content: { "application/json": { schema: ErrorResponseSchema } }
|
|
2099
|
+
}
|
|
2100
|
+
}
|
|
2101
|
+
});
|
|
2102
|
+
registry.registerPath({
|
|
2103
|
+
method: "get",
|
|
2104
|
+
path: "/api/sessions/{id}/messages",
|
|
2105
|
+
tags: ["Sessions"],
|
|
2106
|
+
summary: "Get message history",
|
|
2107
|
+
request: {
|
|
2108
|
+
params: z3.object({ id: z3.string().uuid() })
|
|
2109
|
+
},
|
|
2110
|
+
responses: {
|
|
2111
|
+
200: {
|
|
2112
|
+
description: "Message history",
|
|
2113
|
+
content: {
|
|
2114
|
+
"application/json": {
|
|
2115
|
+
schema: z3.object({ messages: z3.array(HistoryMessageSchema) })
|
|
2116
|
+
}
|
|
2117
|
+
}
|
|
2118
|
+
}
|
|
2119
|
+
}
|
|
2120
|
+
});
|
|
2121
|
+
registry.registerPath({
|
|
2122
|
+
method: "get",
|
|
2123
|
+
path: "/api/sessions/{id}/tasks",
|
|
2124
|
+
tags: ["Sessions"],
|
|
2125
|
+
summary: "Get task state from session transcript",
|
|
2126
|
+
request: {
|
|
2127
|
+
params: z3.object({ id: z3.string().uuid() })
|
|
2128
|
+
},
|
|
2129
|
+
responses: {
|
|
2130
|
+
200: {
|
|
2131
|
+
description: "Task list",
|
|
2132
|
+
content: {
|
|
2133
|
+
"application/json": {
|
|
2134
|
+
schema: z3.object({ tasks: z3.array(TaskItemSchema) })
|
|
2135
|
+
}
|
|
2136
|
+
}
|
|
2137
|
+
},
|
|
2138
|
+
404: {
|
|
2139
|
+
description: "Session not found",
|
|
2140
|
+
content: { "application/json": { schema: ErrorResponseSchema } }
|
|
2141
|
+
}
|
|
2142
|
+
}
|
|
2143
|
+
});
|
|
2144
|
+
registry.registerPath({
|
|
2145
|
+
method: "patch",
|
|
2146
|
+
path: "/api/sessions/{id}",
|
|
2147
|
+
tags: ["Sessions"],
|
|
2148
|
+
summary: "Update session settings",
|
|
2149
|
+
request: {
|
|
2150
|
+
params: z3.object({ id: z3.string().uuid() }),
|
|
2151
|
+
body: {
|
|
2152
|
+
content: { "application/json": { schema: UpdateSessionRequestSchema } }
|
|
2153
|
+
}
|
|
2154
|
+
},
|
|
2155
|
+
responses: {
|
|
2156
|
+
200: {
|
|
2157
|
+
description: "Updated session",
|
|
2158
|
+
content: { "application/json": { schema: SessionSchema } }
|
|
2159
|
+
},
|
|
2160
|
+
400: {
|
|
2161
|
+
description: "Validation error",
|
|
2162
|
+
content: { "application/json": { schema: ErrorResponseSchema } }
|
|
2163
|
+
},
|
|
2164
|
+
404: {
|
|
2165
|
+
description: "Session not found",
|
|
2166
|
+
content: { "application/json": { schema: ErrorResponseSchema } }
|
|
2167
|
+
}
|
|
2168
|
+
}
|
|
2169
|
+
});
|
|
2170
|
+
registry.registerPath({
|
|
2171
|
+
method: "post",
|
|
2172
|
+
path: "/api/sessions/{id}/messages",
|
|
2173
|
+
tags: ["Sessions"],
|
|
2174
|
+
summary: "Send message (SSE stream response)",
|
|
2175
|
+
description: 'Sends a message to the Claude agent and streams the response as Server-Sent Events. Event types: text_delta, tool_call_start, tool_call_delta, tool_call_end, tool_result, approval_required, question_prompt, error, done, session_status, task_update. Each SSE message has the format: `event: message\\ndata: {"type":"<type>","data":{...}}\\n\\n`',
|
|
2176
|
+
request: {
|
|
2177
|
+
params: z3.object({ id: z3.string().uuid() }),
|
|
2178
|
+
body: {
|
|
2179
|
+
content: { "application/json": { schema: SendMessageRequestSchema } }
|
|
2180
|
+
}
|
|
2181
|
+
},
|
|
2182
|
+
responses: {
|
|
2183
|
+
200: {
|
|
2184
|
+
description: "SSE stream of StreamEvent objects",
|
|
2185
|
+
content: {
|
|
2186
|
+
"text/event-stream": {
|
|
2187
|
+
schema: z3.string().openapi({ description: "Server-Sent Events stream" })
|
|
2188
|
+
}
|
|
2189
|
+
}
|
|
2190
|
+
},
|
|
2191
|
+
400: {
|
|
2192
|
+
description: "Validation error",
|
|
2193
|
+
content: { "application/json": { schema: ErrorResponseSchema } }
|
|
2194
|
+
}
|
|
2195
|
+
}
|
|
2196
|
+
});
|
|
2197
|
+
registry.registerPath({
|
|
2198
|
+
method: "post",
|
|
2199
|
+
path: "/api/sessions/{id}/approve",
|
|
2200
|
+
tags: ["Sessions"],
|
|
2201
|
+
summary: "Approve pending tool call",
|
|
2202
|
+
request: {
|
|
2203
|
+
params: z3.object({ id: z3.string().uuid() }),
|
|
2204
|
+
body: {
|
|
2205
|
+
content: { "application/json": { schema: ApprovalRequestSchema } }
|
|
2206
|
+
}
|
|
2207
|
+
},
|
|
2208
|
+
responses: {
|
|
2209
|
+
200: {
|
|
2210
|
+
description: "Approved",
|
|
2211
|
+
content: {
|
|
2212
|
+
"application/json": { schema: z3.object({ ok: z3.boolean() }) }
|
|
2213
|
+
}
|
|
2214
|
+
},
|
|
2215
|
+
400: {
|
|
2216
|
+
description: "Validation error",
|
|
2217
|
+
content: { "application/json": { schema: ErrorResponseSchema } }
|
|
2218
|
+
},
|
|
2219
|
+
404: {
|
|
2220
|
+
description: "No pending approval",
|
|
2221
|
+
content: { "application/json": { schema: ErrorResponseSchema } }
|
|
2222
|
+
}
|
|
2223
|
+
}
|
|
2224
|
+
});
|
|
2225
|
+
registry.registerPath({
|
|
2226
|
+
method: "post",
|
|
2227
|
+
path: "/api/sessions/{id}/deny",
|
|
2228
|
+
tags: ["Sessions"],
|
|
2229
|
+
summary: "Deny pending tool call",
|
|
2230
|
+
request: {
|
|
2231
|
+
params: z3.object({ id: z3.string().uuid() }),
|
|
2232
|
+
body: {
|
|
2233
|
+
content: { "application/json": { schema: ApprovalRequestSchema } }
|
|
2234
|
+
}
|
|
2235
|
+
},
|
|
2236
|
+
responses: {
|
|
2237
|
+
200: {
|
|
2238
|
+
description: "Denied",
|
|
2239
|
+
content: {
|
|
2240
|
+
"application/json": { schema: z3.object({ ok: z3.boolean() }) }
|
|
2241
|
+
}
|
|
2242
|
+
},
|
|
2243
|
+
400: {
|
|
2244
|
+
description: "Validation error",
|
|
2245
|
+
content: { "application/json": { schema: ErrorResponseSchema } }
|
|
2246
|
+
},
|
|
2247
|
+
404: {
|
|
2248
|
+
description: "No pending approval",
|
|
2249
|
+
content: { "application/json": { schema: ErrorResponseSchema } }
|
|
2250
|
+
}
|
|
2251
|
+
}
|
|
2252
|
+
});
|
|
2253
|
+
registry.registerPath({
|
|
2254
|
+
method: "post",
|
|
2255
|
+
path: "/api/sessions/{id}/submit-answers",
|
|
2256
|
+
tags: ["Sessions"],
|
|
2257
|
+
summary: "Submit answers for AskUserQuestion",
|
|
2258
|
+
request: {
|
|
2259
|
+
params: z3.object({ id: z3.string().uuid() }),
|
|
2260
|
+
body: {
|
|
2261
|
+
content: { "application/json": { schema: SubmitAnswersRequestSchema } }
|
|
2262
|
+
}
|
|
2263
|
+
},
|
|
2264
|
+
responses: {
|
|
2265
|
+
200: {
|
|
2266
|
+
description: "Answers submitted",
|
|
2267
|
+
content: {
|
|
2268
|
+
"application/json": { schema: z3.object({ ok: z3.boolean() }) }
|
|
2269
|
+
}
|
|
2270
|
+
},
|
|
2271
|
+
400: {
|
|
2272
|
+
description: "Validation error",
|
|
2273
|
+
content: { "application/json": { schema: ErrorResponseSchema } }
|
|
2274
|
+
},
|
|
2275
|
+
404: {
|
|
2276
|
+
description: "No pending question",
|
|
2277
|
+
content: { "application/json": { schema: ErrorResponseSchema } }
|
|
2278
|
+
}
|
|
2279
|
+
}
|
|
2280
|
+
});
|
|
2281
|
+
registry.registerPath({
|
|
2282
|
+
method: "get",
|
|
2283
|
+
path: "/api/directory",
|
|
2284
|
+
tags: ["Directory"],
|
|
2285
|
+
summary: "Browse directories",
|
|
2286
|
+
description: "Browse directories on the server filesystem. Restricted to the home directory for security.",
|
|
2287
|
+
request: {
|
|
2288
|
+
query: BrowseDirectoryQuerySchema
|
|
2289
|
+
},
|
|
2290
|
+
responses: {
|
|
2291
|
+
200: {
|
|
2292
|
+
description: "Directory listing",
|
|
2293
|
+
content: { "application/json": { schema: BrowseDirectoryResponseSchema } }
|
|
2294
|
+
},
|
|
2295
|
+
400: {
|
|
2296
|
+
description: "Invalid path",
|
|
2297
|
+
content: { "application/json": { schema: ErrorResponseSchema } }
|
|
2298
|
+
},
|
|
2299
|
+
403: {
|
|
2300
|
+
description: "Access denied (path outside home directory)",
|
|
2301
|
+
content: { "application/json": { schema: ErrorResponseSchema } }
|
|
2302
|
+
},
|
|
2303
|
+
404: {
|
|
2304
|
+
description: "Directory not found",
|
|
2305
|
+
content: { "application/json": { schema: ErrorResponseSchema } }
|
|
2306
|
+
}
|
|
2307
|
+
}
|
|
2308
|
+
});
|
|
2309
|
+
registry.registerPath({
|
|
2310
|
+
method: "get",
|
|
2311
|
+
path: "/api/directory/default",
|
|
2312
|
+
tags: ["Directory"],
|
|
2313
|
+
summary: "Get default working directory",
|
|
2314
|
+
description: "Returns the server's default working directory (process.cwd()).",
|
|
2315
|
+
responses: {
|
|
2316
|
+
200: {
|
|
2317
|
+
description: "Default directory path",
|
|
2318
|
+
content: {
|
|
2319
|
+
"application/json": {
|
|
2320
|
+
schema: z3.object({ path: z3.string() })
|
|
2321
|
+
}
|
|
2322
|
+
}
|
|
2323
|
+
}
|
|
2324
|
+
}
|
|
2325
|
+
});
|
|
2326
|
+
registry.registerPath({
|
|
2327
|
+
method: "get",
|
|
2328
|
+
path: "/api/commands",
|
|
2329
|
+
tags: ["Commands"],
|
|
2330
|
+
summary: "List all slash commands",
|
|
2331
|
+
request: {
|
|
2332
|
+
query: CommandsQuerySchema
|
|
2333
|
+
},
|
|
2334
|
+
responses: {
|
|
2335
|
+
200: {
|
|
2336
|
+
description: "Command registry",
|
|
2337
|
+
content: { "application/json": { schema: CommandRegistrySchema } }
|
|
2338
|
+
},
|
|
2339
|
+
400: {
|
|
2340
|
+
description: "Validation error",
|
|
2341
|
+
content: { "application/json": { schema: ErrorResponseSchema } }
|
|
2342
|
+
}
|
|
2343
|
+
}
|
|
2344
|
+
});
|
|
2345
|
+
function generateOpenAPISpec() {
|
|
2346
|
+
const generator = new OpenApiGeneratorV31(registry.definitions);
|
|
2347
|
+
return generator.generateDocument({
|
|
2348
|
+
openapi: "3.1.0",
|
|
2349
|
+
info: {
|
|
2350
|
+
title: "DorkOS API",
|
|
2351
|
+
version: "0.1.0",
|
|
2352
|
+
description: "REST/SSE API for Claude Code sessions, built with the Claude Agent SDK."
|
|
2353
|
+
},
|
|
2354
|
+
servers: [{ url: "http://localhost:6942" }]
|
|
2355
|
+
});
|
|
2356
|
+
}
|
|
2357
|
+
|
|
2358
|
+
// ../../apps/server/src/middleware/error-handler.ts
|
|
2359
|
+
function errorHandler(err, _req, res, _next) {
|
|
2360
|
+
console.error("[Gateway Error]", err.message, err.stack);
|
|
2361
|
+
res.status(500).json({
|
|
2362
|
+
error: err.message || "Internal Server Error",
|
|
2363
|
+
code: "INTERNAL_ERROR"
|
|
2364
|
+
});
|
|
2365
|
+
}
|
|
2366
|
+
|
|
2367
|
+
// ../../apps/server/src/app.ts
|
|
2368
|
+
var __dirname4 = path8.dirname(fileURLToPath4(import.meta.url));
|
|
2369
|
+
function createApp() {
|
|
2370
|
+
const app = express();
|
|
2371
|
+
app.use(cors());
|
|
2372
|
+
app.use(express.json());
|
|
2373
|
+
app.use("/api/sessions", sessions_default);
|
|
2374
|
+
app.use("/api/commands", commands_default);
|
|
2375
|
+
app.use("/api/health", health_default);
|
|
2376
|
+
app.use("/api/directory", directory_default);
|
|
2377
|
+
app.use("/api/config", config_default);
|
|
2378
|
+
app.use("/api/files", files_default);
|
|
2379
|
+
app.use("/api/git", git_default);
|
|
2380
|
+
const spec = generateOpenAPISpec();
|
|
2381
|
+
app.get("/api/openapi.json", (_req, res) => res.json(spec));
|
|
2382
|
+
app.use("/api/docs", apiReference({ content: spec }));
|
|
2383
|
+
app.use(errorHandler);
|
|
2384
|
+
if (process.env.NODE_ENV === "production") {
|
|
2385
|
+
const distPath = process.env.CLIENT_DIST_PATH ?? path8.join(__dirname4, "../../client/dist");
|
|
2386
|
+
app.use(express.static(distPath));
|
|
2387
|
+
app.get("*", (_req, res) => {
|
|
2388
|
+
res.sendFile(path8.join(distPath, "index.html"));
|
|
2389
|
+
});
|
|
2390
|
+
}
|
|
2391
|
+
return app;
|
|
2392
|
+
}
|
|
2393
|
+
|
|
2394
|
+
// ../../node_modules/chokidar/esm/index.js
|
|
2395
|
+
import { stat as statcb } from "fs";
|
|
2396
|
+
import { stat as stat3, readdir as readdir2 } from "fs/promises";
|
|
2397
|
+
import { EventEmitter } from "events";
|
|
2398
|
+
import * as sysPath2 from "path";
|
|
2399
|
+
|
|
2400
|
+
// ../../node_modules/readdirp/esm/index.js
|
|
2401
|
+
import { stat, lstat, readdir, realpath } from "node:fs/promises";
|
|
2402
|
+
import { Readable } from "node:stream";
|
|
2403
|
+
import { resolve as presolve, relative as prelative, join as pjoin, sep as psep } from "node:path";
|
|
2404
|
+
var EntryTypes = {
|
|
2405
|
+
FILE_TYPE: "files",
|
|
2406
|
+
DIR_TYPE: "directories",
|
|
2407
|
+
FILE_DIR_TYPE: "files_directories",
|
|
2408
|
+
EVERYTHING_TYPE: "all"
|
|
2409
|
+
};
|
|
2410
|
+
var defaultOptions = {
|
|
2411
|
+
root: ".",
|
|
2412
|
+
fileFilter: (_entryInfo) => true,
|
|
2413
|
+
directoryFilter: (_entryInfo) => true,
|
|
2414
|
+
type: EntryTypes.FILE_TYPE,
|
|
2415
|
+
lstat: false,
|
|
2416
|
+
depth: 2147483648,
|
|
2417
|
+
alwaysStat: false,
|
|
2418
|
+
highWaterMark: 4096
|
|
2419
|
+
};
|
|
2420
|
+
Object.freeze(defaultOptions);
|
|
2421
|
+
var RECURSIVE_ERROR_CODE = "READDIRP_RECURSIVE_ERROR";
|
|
2422
|
+
var NORMAL_FLOW_ERRORS = /* @__PURE__ */ new Set(["ENOENT", "EPERM", "EACCES", "ELOOP", RECURSIVE_ERROR_CODE]);
|
|
2423
|
+
var ALL_TYPES = [
|
|
2424
|
+
EntryTypes.DIR_TYPE,
|
|
2425
|
+
EntryTypes.EVERYTHING_TYPE,
|
|
2426
|
+
EntryTypes.FILE_DIR_TYPE,
|
|
2427
|
+
EntryTypes.FILE_TYPE
|
|
2428
|
+
];
|
|
2429
|
+
var DIR_TYPES = /* @__PURE__ */ new Set([
|
|
2430
|
+
EntryTypes.DIR_TYPE,
|
|
2431
|
+
EntryTypes.EVERYTHING_TYPE,
|
|
2432
|
+
EntryTypes.FILE_DIR_TYPE
|
|
2433
|
+
]);
|
|
2434
|
+
var FILE_TYPES = /* @__PURE__ */ new Set([
|
|
2435
|
+
EntryTypes.EVERYTHING_TYPE,
|
|
2436
|
+
EntryTypes.FILE_DIR_TYPE,
|
|
2437
|
+
EntryTypes.FILE_TYPE
|
|
2438
|
+
]);
|
|
2439
|
+
var isNormalFlowError = (error) => NORMAL_FLOW_ERRORS.has(error.code);
|
|
2440
|
+
var wantBigintFsStats = process.platform === "win32";
|
|
2441
|
+
var emptyFn = (_entryInfo) => true;
|
|
2442
|
+
var normalizeFilter = (filter) => {
|
|
2443
|
+
if (filter === void 0)
|
|
2444
|
+
return emptyFn;
|
|
2445
|
+
if (typeof filter === "function")
|
|
2446
|
+
return filter;
|
|
2447
|
+
if (typeof filter === "string") {
|
|
2448
|
+
const fl = filter.trim();
|
|
2449
|
+
return (entry) => entry.basename === fl;
|
|
2450
|
+
}
|
|
2451
|
+
if (Array.isArray(filter)) {
|
|
2452
|
+
const trItems = filter.map((item) => item.trim());
|
|
2453
|
+
return (entry) => trItems.some((f) => entry.basename === f);
|
|
2454
|
+
}
|
|
2455
|
+
return emptyFn;
|
|
2456
|
+
};
|
|
2457
|
+
var ReaddirpStream = class extends Readable {
|
|
2458
|
+
constructor(options = {}) {
|
|
2459
|
+
super({
|
|
2460
|
+
objectMode: true,
|
|
2461
|
+
autoDestroy: true,
|
|
2462
|
+
highWaterMark: options.highWaterMark
|
|
2463
|
+
});
|
|
2464
|
+
const opts = { ...defaultOptions, ...options };
|
|
2465
|
+
const { root, type } = opts;
|
|
2466
|
+
this._fileFilter = normalizeFilter(opts.fileFilter);
|
|
2467
|
+
this._directoryFilter = normalizeFilter(opts.directoryFilter);
|
|
2468
|
+
const statMethod = opts.lstat ? lstat : stat;
|
|
2469
|
+
if (wantBigintFsStats) {
|
|
2470
|
+
this._stat = (path10) => statMethod(path10, { bigint: true });
|
|
2471
|
+
} else {
|
|
2472
|
+
this._stat = statMethod;
|
|
2473
|
+
}
|
|
2474
|
+
this._maxDepth = opts.depth ?? defaultOptions.depth;
|
|
2475
|
+
this._wantsDir = type ? DIR_TYPES.has(type) : false;
|
|
2476
|
+
this._wantsFile = type ? FILE_TYPES.has(type) : false;
|
|
2477
|
+
this._wantsEverything = type === EntryTypes.EVERYTHING_TYPE;
|
|
2478
|
+
this._root = presolve(root);
|
|
2479
|
+
this._isDirent = !opts.alwaysStat;
|
|
2480
|
+
this._statsProp = this._isDirent ? "dirent" : "stats";
|
|
2481
|
+
this._rdOptions = { encoding: "utf8", withFileTypes: this._isDirent };
|
|
2482
|
+
this.parents = [this._exploreDir(root, 1)];
|
|
2483
|
+
this.reading = false;
|
|
2484
|
+
this.parent = void 0;
|
|
2485
|
+
}
|
|
2486
|
+
async _read(batch) {
|
|
2487
|
+
if (this.reading)
|
|
2488
|
+
return;
|
|
2489
|
+
this.reading = true;
|
|
2490
|
+
try {
|
|
2491
|
+
while (!this.destroyed && batch > 0) {
|
|
2492
|
+
const par = this.parent;
|
|
2493
|
+
const fil = par && par.files;
|
|
2494
|
+
if (fil && fil.length > 0) {
|
|
2495
|
+
const { path: path10, depth } = par;
|
|
2496
|
+
const slice = fil.splice(0, batch).map((dirent) => this._formatEntry(dirent, path10));
|
|
2497
|
+
const awaited = await Promise.all(slice);
|
|
2498
|
+
for (const entry of awaited) {
|
|
2499
|
+
if (!entry)
|
|
2500
|
+
continue;
|
|
2501
|
+
if (this.destroyed)
|
|
2502
|
+
return;
|
|
2503
|
+
const entryType = await this._getEntryType(entry);
|
|
2504
|
+
if (entryType === "directory" && this._directoryFilter(entry)) {
|
|
2505
|
+
if (depth <= this._maxDepth) {
|
|
2506
|
+
this.parents.push(this._exploreDir(entry.fullPath, depth + 1));
|
|
2507
|
+
}
|
|
2508
|
+
if (this._wantsDir) {
|
|
2509
|
+
this.push(entry);
|
|
2510
|
+
batch--;
|
|
2511
|
+
}
|
|
2512
|
+
} else if ((entryType === "file" || this._includeAsFile(entry)) && this._fileFilter(entry)) {
|
|
2513
|
+
if (this._wantsFile) {
|
|
2514
|
+
this.push(entry);
|
|
2515
|
+
batch--;
|
|
2516
|
+
}
|
|
2517
|
+
}
|
|
2518
|
+
}
|
|
2519
|
+
} else {
|
|
2520
|
+
const parent = this.parents.pop();
|
|
2521
|
+
if (!parent) {
|
|
2522
|
+
this.push(null);
|
|
2523
|
+
break;
|
|
2524
|
+
}
|
|
2525
|
+
this.parent = await parent;
|
|
2526
|
+
if (this.destroyed)
|
|
2527
|
+
return;
|
|
2528
|
+
}
|
|
2529
|
+
}
|
|
2530
|
+
} catch (error) {
|
|
2531
|
+
this.destroy(error);
|
|
2532
|
+
} finally {
|
|
2533
|
+
this.reading = false;
|
|
2534
|
+
}
|
|
2535
|
+
}
|
|
2536
|
+
async _exploreDir(path10, depth) {
|
|
2537
|
+
let files;
|
|
2538
|
+
try {
|
|
2539
|
+
files = await readdir(path10, this._rdOptions);
|
|
2540
|
+
} catch (error) {
|
|
2541
|
+
this._onError(error);
|
|
2542
|
+
}
|
|
2543
|
+
return { files, depth, path: path10 };
|
|
2544
|
+
}
|
|
2545
|
+
async _formatEntry(dirent, path10) {
|
|
2546
|
+
let entry;
|
|
2547
|
+
const basename3 = this._isDirent ? dirent.name : dirent;
|
|
2548
|
+
try {
|
|
2549
|
+
const fullPath = presolve(pjoin(path10, basename3));
|
|
2550
|
+
entry = { path: prelative(this._root, fullPath), fullPath, basename: basename3 };
|
|
2551
|
+
entry[this._statsProp] = this._isDirent ? dirent : await this._stat(fullPath);
|
|
2552
|
+
} catch (err) {
|
|
2553
|
+
this._onError(err);
|
|
2554
|
+
return;
|
|
2555
|
+
}
|
|
2556
|
+
return entry;
|
|
2557
|
+
}
|
|
2558
|
+
_onError(err) {
|
|
2559
|
+
if (isNormalFlowError(err) && !this.destroyed) {
|
|
2560
|
+
this.emit("warn", err);
|
|
2561
|
+
} else {
|
|
2562
|
+
this.destroy(err);
|
|
2563
|
+
}
|
|
2564
|
+
}
|
|
2565
|
+
async _getEntryType(entry) {
|
|
2566
|
+
if (!entry && this._statsProp in entry) {
|
|
2567
|
+
return "";
|
|
2568
|
+
}
|
|
2569
|
+
const stats = entry[this._statsProp];
|
|
2570
|
+
if (stats.isFile())
|
|
2571
|
+
return "file";
|
|
2572
|
+
if (stats.isDirectory())
|
|
2573
|
+
return "directory";
|
|
2574
|
+
if (stats && stats.isSymbolicLink()) {
|
|
2575
|
+
const full = entry.fullPath;
|
|
2576
|
+
try {
|
|
2577
|
+
const entryRealPath = await realpath(full);
|
|
2578
|
+
const entryRealPathStats = await lstat(entryRealPath);
|
|
2579
|
+
if (entryRealPathStats.isFile()) {
|
|
2580
|
+
return "file";
|
|
2581
|
+
}
|
|
2582
|
+
if (entryRealPathStats.isDirectory()) {
|
|
2583
|
+
const len = entryRealPath.length;
|
|
2584
|
+
if (full.startsWith(entryRealPath) && full.substr(len, 1) === psep) {
|
|
2585
|
+
const recursiveError = new Error(`Circular symlink detected: "${full}" points to "${entryRealPath}"`);
|
|
2586
|
+
recursiveError.code = RECURSIVE_ERROR_CODE;
|
|
2587
|
+
return this._onError(recursiveError);
|
|
2588
|
+
}
|
|
2589
|
+
return "directory";
|
|
2590
|
+
}
|
|
2591
|
+
} catch (error) {
|
|
2592
|
+
this._onError(error);
|
|
2593
|
+
return "";
|
|
2594
|
+
}
|
|
2595
|
+
}
|
|
2596
|
+
}
|
|
2597
|
+
_includeAsFile(entry) {
|
|
2598
|
+
const stats = entry && entry[this._statsProp];
|
|
2599
|
+
return stats && this._wantsEverything && !stats.isDirectory();
|
|
2600
|
+
}
|
|
2601
|
+
};
|
|
2602
|
+
function readdirp(root, options = {}) {
|
|
2603
|
+
let type = options.entryType || options.type;
|
|
2604
|
+
if (type === "both")
|
|
2605
|
+
type = EntryTypes.FILE_DIR_TYPE;
|
|
2606
|
+
if (type)
|
|
2607
|
+
options.type = type;
|
|
2608
|
+
if (!root) {
|
|
2609
|
+
throw new Error("readdirp: root argument is required. Usage: readdirp(root, options)");
|
|
2610
|
+
} else if (typeof root !== "string") {
|
|
2611
|
+
throw new TypeError("readdirp: root argument must be a string. Usage: readdirp(root, options)");
|
|
2612
|
+
} else if (type && !ALL_TYPES.includes(type)) {
|
|
2613
|
+
throw new Error(`readdirp: Invalid type passed. Use one of ${ALL_TYPES.join(", ")}`);
|
|
2614
|
+
}
|
|
2615
|
+
options.root = root;
|
|
2616
|
+
return new ReaddirpStream(options);
|
|
2617
|
+
}
|
|
2618
|
+
|
|
2619
|
+
// ../../node_modules/chokidar/esm/handler.js
|
|
2620
|
+
import { watchFile, unwatchFile, watch as fs_watch } from "fs";
|
|
2621
|
+
import { open, stat as stat2, lstat as lstat2, realpath as fsrealpath } from "fs/promises";
|
|
2622
|
+
import * as sysPath from "path";
|
|
2623
|
+
import { type as osType } from "os";
|
|
2624
|
+
var STR_DATA = "data";
|
|
2625
|
+
var STR_END = "end";
|
|
2626
|
+
var STR_CLOSE = "close";
|
|
2627
|
+
var EMPTY_FN = () => {
|
|
2628
|
+
};
|
|
2629
|
+
var pl = process.platform;
|
|
2630
|
+
var isWindows = pl === "win32";
|
|
2631
|
+
var isMacos = pl === "darwin";
|
|
2632
|
+
var isLinux = pl === "linux";
|
|
2633
|
+
var isFreeBSD = pl === "freebsd";
|
|
2634
|
+
var isIBMi = osType() === "OS400";
|
|
2635
|
+
var EVENTS = {
|
|
2636
|
+
ALL: "all",
|
|
2637
|
+
READY: "ready",
|
|
2638
|
+
ADD: "add",
|
|
2639
|
+
CHANGE: "change",
|
|
2640
|
+
ADD_DIR: "addDir",
|
|
2641
|
+
UNLINK: "unlink",
|
|
2642
|
+
UNLINK_DIR: "unlinkDir",
|
|
2643
|
+
RAW: "raw",
|
|
2644
|
+
ERROR: "error"
|
|
2645
|
+
};
|
|
2646
|
+
var EV = EVENTS;
|
|
2647
|
+
var THROTTLE_MODE_WATCH = "watch";
|
|
2648
|
+
var statMethods = { lstat: lstat2, stat: stat2 };
|
|
2649
|
+
var KEY_LISTENERS = "listeners";
|
|
2650
|
+
var KEY_ERR = "errHandlers";
|
|
2651
|
+
var KEY_RAW = "rawEmitters";
|
|
2652
|
+
var HANDLER_KEYS = [KEY_LISTENERS, KEY_ERR, KEY_RAW];
|
|
2653
|
+
var binaryExtensions = /* @__PURE__ */ new Set([
|
|
2654
|
+
"3dm",
|
|
2655
|
+
"3ds",
|
|
2656
|
+
"3g2",
|
|
2657
|
+
"3gp",
|
|
2658
|
+
"7z",
|
|
2659
|
+
"a",
|
|
2660
|
+
"aac",
|
|
2661
|
+
"adp",
|
|
2662
|
+
"afdesign",
|
|
2663
|
+
"afphoto",
|
|
2664
|
+
"afpub",
|
|
2665
|
+
"ai",
|
|
2666
|
+
"aif",
|
|
2667
|
+
"aiff",
|
|
2668
|
+
"alz",
|
|
2669
|
+
"ape",
|
|
2670
|
+
"apk",
|
|
2671
|
+
"appimage",
|
|
2672
|
+
"ar",
|
|
2673
|
+
"arj",
|
|
2674
|
+
"asf",
|
|
2675
|
+
"au",
|
|
2676
|
+
"avi",
|
|
2677
|
+
"bak",
|
|
2678
|
+
"baml",
|
|
2679
|
+
"bh",
|
|
2680
|
+
"bin",
|
|
2681
|
+
"bk",
|
|
2682
|
+
"bmp",
|
|
2683
|
+
"btif",
|
|
2684
|
+
"bz2",
|
|
2685
|
+
"bzip2",
|
|
2686
|
+
"cab",
|
|
2687
|
+
"caf",
|
|
2688
|
+
"cgm",
|
|
2689
|
+
"class",
|
|
2690
|
+
"cmx",
|
|
2691
|
+
"cpio",
|
|
2692
|
+
"cr2",
|
|
2693
|
+
"cur",
|
|
2694
|
+
"dat",
|
|
2695
|
+
"dcm",
|
|
2696
|
+
"deb",
|
|
2697
|
+
"dex",
|
|
2698
|
+
"djvu",
|
|
2699
|
+
"dll",
|
|
2700
|
+
"dmg",
|
|
2701
|
+
"dng",
|
|
2702
|
+
"doc",
|
|
2703
|
+
"docm",
|
|
2704
|
+
"docx",
|
|
2705
|
+
"dot",
|
|
2706
|
+
"dotm",
|
|
2707
|
+
"dra",
|
|
2708
|
+
"DS_Store",
|
|
2709
|
+
"dsk",
|
|
2710
|
+
"dts",
|
|
2711
|
+
"dtshd",
|
|
2712
|
+
"dvb",
|
|
2713
|
+
"dwg",
|
|
2714
|
+
"dxf",
|
|
2715
|
+
"ecelp4800",
|
|
2716
|
+
"ecelp7470",
|
|
2717
|
+
"ecelp9600",
|
|
2718
|
+
"egg",
|
|
2719
|
+
"eol",
|
|
2720
|
+
"eot",
|
|
2721
|
+
"epub",
|
|
2722
|
+
"exe",
|
|
2723
|
+
"f4v",
|
|
2724
|
+
"fbs",
|
|
2725
|
+
"fh",
|
|
2726
|
+
"fla",
|
|
2727
|
+
"flac",
|
|
2728
|
+
"flatpak",
|
|
2729
|
+
"fli",
|
|
2730
|
+
"flv",
|
|
2731
|
+
"fpx",
|
|
2732
|
+
"fst",
|
|
2733
|
+
"fvt",
|
|
2734
|
+
"g3",
|
|
2735
|
+
"gh",
|
|
2736
|
+
"gif",
|
|
2737
|
+
"graffle",
|
|
2738
|
+
"gz",
|
|
2739
|
+
"gzip",
|
|
2740
|
+
"h261",
|
|
2741
|
+
"h263",
|
|
2742
|
+
"h264",
|
|
2743
|
+
"icns",
|
|
2744
|
+
"ico",
|
|
2745
|
+
"ief",
|
|
2746
|
+
"img",
|
|
2747
|
+
"ipa",
|
|
2748
|
+
"iso",
|
|
2749
|
+
"jar",
|
|
2750
|
+
"jpeg",
|
|
2751
|
+
"jpg",
|
|
2752
|
+
"jpgv",
|
|
2753
|
+
"jpm",
|
|
2754
|
+
"jxr",
|
|
2755
|
+
"key",
|
|
2756
|
+
"ktx",
|
|
2757
|
+
"lha",
|
|
2758
|
+
"lib",
|
|
2759
|
+
"lvp",
|
|
2760
|
+
"lz",
|
|
2761
|
+
"lzh",
|
|
2762
|
+
"lzma",
|
|
2763
|
+
"lzo",
|
|
2764
|
+
"m3u",
|
|
2765
|
+
"m4a",
|
|
2766
|
+
"m4v",
|
|
2767
|
+
"mar",
|
|
2768
|
+
"mdi",
|
|
2769
|
+
"mht",
|
|
2770
|
+
"mid",
|
|
2771
|
+
"midi",
|
|
2772
|
+
"mj2",
|
|
2773
|
+
"mka",
|
|
2774
|
+
"mkv",
|
|
2775
|
+
"mmr",
|
|
2776
|
+
"mng",
|
|
2777
|
+
"mobi",
|
|
2778
|
+
"mov",
|
|
2779
|
+
"movie",
|
|
2780
|
+
"mp3",
|
|
2781
|
+
"mp4",
|
|
2782
|
+
"mp4a",
|
|
2783
|
+
"mpeg",
|
|
2784
|
+
"mpg",
|
|
2785
|
+
"mpga",
|
|
2786
|
+
"mxu",
|
|
2787
|
+
"nef",
|
|
2788
|
+
"npx",
|
|
2789
|
+
"numbers",
|
|
2790
|
+
"nupkg",
|
|
2791
|
+
"o",
|
|
2792
|
+
"odp",
|
|
2793
|
+
"ods",
|
|
2794
|
+
"odt",
|
|
2795
|
+
"oga",
|
|
2796
|
+
"ogg",
|
|
2797
|
+
"ogv",
|
|
2798
|
+
"otf",
|
|
2799
|
+
"ott",
|
|
2800
|
+
"pages",
|
|
2801
|
+
"pbm",
|
|
2802
|
+
"pcx",
|
|
2803
|
+
"pdb",
|
|
2804
|
+
"pdf",
|
|
2805
|
+
"pea",
|
|
2806
|
+
"pgm",
|
|
2807
|
+
"pic",
|
|
2808
|
+
"png",
|
|
2809
|
+
"pnm",
|
|
2810
|
+
"pot",
|
|
2811
|
+
"potm",
|
|
2812
|
+
"potx",
|
|
2813
|
+
"ppa",
|
|
2814
|
+
"ppam",
|
|
2815
|
+
"ppm",
|
|
2816
|
+
"pps",
|
|
2817
|
+
"ppsm",
|
|
2818
|
+
"ppsx",
|
|
2819
|
+
"ppt",
|
|
2820
|
+
"pptm",
|
|
2821
|
+
"pptx",
|
|
2822
|
+
"psd",
|
|
2823
|
+
"pya",
|
|
2824
|
+
"pyc",
|
|
2825
|
+
"pyo",
|
|
2826
|
+
"pyv",
|
|
2827
|
+
"qt",
|
|
2828
|
+
"rar",
|
|
2829
|
+
"ras",
|
|
2830
|
+
"raw",
|
|
2831
|
+
"resources",
|
|
2832
|
+
"rgb",
|
|
2833
|
+
"rip",
|
|
2834
|
+
"rlc",
|
|
2835
|
+
"rmf",
|
|
2836
|
+
"rmvb",
|
|
2837
|
+
"rpm",
|
|
2838
|
+
"rtf",
|
|
2839
|
+
"rz",
|
|
2840
|
+
"s3m",
|
|
2841
|
+
"s7z",
|
|
2842
|
+
"scpt",
|
|
2843
|
+
"sgi",
|
|
2844
|
+
"shar",
|
|
2845
|
+
"snap",
|
|
2846
|
+
"sil",
|
|
2847
|
+
"sketch",
|
|
2848
|
+
"slk",
|
|
2849
|
+
"smv",
|
|
2850
|
+
"snk",
|
|
2851
|
+
"so",
|
|
2852
|
+
"stl",
|
|
2853
|
+
"suo",
|
|
2854
|
+
"sub",
|
|
2855
|
+
"swf",
|
|
2856
|
+
"tar",
|
|
2857
|
+
"tbz",
|
|
2858
|
+
"tbz2",
|
|
2859
|
+
"tga",
|
|
2860
|
+
"tgz",
|
|
2861
|
+
"thmx",
|
|
2862
|
+
"tif",
|
|
2863
|
+
"tiff",
|
|
2864
|
+
"tlz",
|
|
2865
|
+
"ttc",
|
|
2866
|
+
"ttf",
|
|
2867
|
+
"txz",
|
|
2868
|
+
"udf",
|
|
2869
|
+
"uvh",
|
|
2870
|
+
"uvi",
|
|
2871
|
+
"uvm",
|
|
2872
|
+
"uvp",
|
|
2873
|
+
"uvs",
|
|
2874
|
+
"uvu",
|
|
2875
|
+
"viv",
|
|
2876
|
+
"vob",
|
|
2877
|
+
"war",
|
|
2878
|
+
"wav",
|
|
2879
|
+
"wax",
|
|
2880
|
+
"wbmp",
|
|
2881
|
+
"wdp",
|
|
2882
|
+
"weba",
|
|
2883
|
+
"webm",
|
|
2884
|
+
"webp",
|
|
2885
|
+
"whl",
|
|
2886
|
+
"wim",
|
|
2887
|
+
"wm",
|
|
2888
|
+
"wma",
|
|
2889
|
+
"wmv",
|
|
2890
|
+
"wmx",
|
|
2891
|
+
"woff",
|
|
2892
|
+
"woff2",
|
|
2893
|
+
"wrm",
|
|
2894
|
+
"wvx",
|
|
2895
|
+
"xbm",
|
|
2896
|
+
"xif",
|
|
2897
|
+
"xla",
|
|
2898
|
+
"xlam",
|
|
2899
|
+
"xls",
|
|
2900
|
+
"xlsb",
|
|
2901
|
+
"xlsm",
|
|
2902
|
+
"xlsx",
|
|
2903
|
+
"xlt",
|
|
2904
|
+
"xltm",
|
|
2905
|
+
"xltx",
|
|
2906
|
+
"xm",
|
|
2907
|
+
"xmind",
|
|
2908
|
+
"xpi",
|
|
2909
|
+
"xpm",
|
|
2910
|
+
"xwd",
|
|
2911
|
+
"xz",
|
|
2912
|
+
"z",
|
|
2913
|
+
"zip",
|
|
2914
|
+
"zipx"
|
|
2915
|
+
]);
|
|
2916
|
+
var isBinaryPath = (filePath) => binaryExtensions.has(sysPath.extname(filePath).slice(1).toLowerCase());
|
|
2917
|
+
var foreach = (val, fn) => {
|
|
2918
|
+
if (val instanceof Set) {
|
|
2919
|
+
val.forEach(fn);
|
|
2920
|
+
} else {
|
|
2921
|
+
fn(val);
|
|
2922
|
+
}
|
|
2923
|
+
};
|
|
2924
|
+
var addAndConvert = (main, prop, item) => {
|
|
2925
|
+
let container = main[prop];
|
|
2926
|
+
if (!(container instanceof Set)) {
|
|
2927
|
+
main[prop] = container = /* @__PURE__ */ new Set([container]);
|
|
2928
|
+
}
|
|
2929
|
+
container.add(item);
|
|
2930
|
+
};
|
|
2931
|
+
var clearItem = (cont) => (key) => {
|
|
2932
|
+
const set = cont[key];
|
|
2933
|
+
if (set instanceof Set) {
|
|
2934
|
+
set.clear();
|
|
2935
|
+
} else {
|
|
2936
|
+
delete cont[key];
|
|
2937
|
+
}
|
|
2938
|
+
};
|
|
2939
|
+
var delFromSet = (main, prop, item) => {
|
|
2940
|
+
const container = main[prop];
|
|
2941
|
+
if (container instanceof Set) {
|
|
2942
|
+
container.delete(item);
|
|
2943
|
+
} else if (container === item) {
|
|
2944
|
+
delete main[prop];
|
|
2945
|
+
}
|
|
2946
|
+
};
|
|
2947
|
+
var isEmptySet = (val) => val instanceof Set ? val.size === 0 : !val;
|
|
2948
|
+
var FsWatchInstances = /* @__PURE__ */ new Map();
|
|
2949
|
+
function createFsWatchInstance(path10, options, listener, errHandler, emitRaw) {
|
|
2950
|
+
const handleEvent = (rawEvent, evPath) => {
|
|
2951
|
+
listener(path10);
|
|
2952
|
+
emitRaw(rawEvent, evPath, { watchedPath: path10 });
|
|
2953
|
+
if (evPath && path10 !== evPath) {
|
|
2954
|
+
fsWatchBroadcast(sysPath.resolve(path10, evPath), KEY_LISTENERS, sysPath.join(path10, evPath));
|
|
2955
|
+
}
|
|
2956
|
+
};
|
|
2957
|
+
try {
|
|
2958
|
+
return fs_watch(path10, {
|
|
2959
|
+
persistent: options.persistent
|
|
2960
|
+
}, handleEvent);
|
|
2961
|
+
} catch (error) {
|
|
2962
|
+
errHandler(error);
|
|
2963
|
+
return void 0;
|
|
2964
|
+
}
|
|
2965
|
+
}
|
|
2966
|
+
var fsWatchBroadcast = (fullPath, listenerType, val1, val2, val3) => {
|
|
2967
|
+
const cont = FsWatchInstances.get(fullPath);
|
|
2968
|
+
if (!cont)
|
|
2969
|
+
return;
|
|
2970
|
+
foreach(cont[listenerType], (listener) => {
|
|
2971
|
+
listener(val1, val2, val3);
|
|
2972
|
+
});
|
|
2973
|
+
};
|
|
2974
|
+
var setFsWatchListener = (path10, fullPath, options, handlers) => {
|
|
2975
|
+
const { listener, errHandler, rawEmitter } = handlers;
|
|
2976
|
+
let cont = FsWatchInstances.get(fullPath);
|
|
2977
|
+
let watcher;
|
|
2978
|
+
if (!options.persistent) {
|
|
2979
|
+
watcher = createFsWatchInstance(path10, options, listener, errHandler, rawEmitter);
|
|
2980
|
+
if (!watcher)
|
|
2981
|
+
return;
|
|
2982
|
+
return watcher.close.bind(watcher);
|
|
2983
|
+
}
|
|
2984
|
+
if (cont) {
|
|
2985
|
+
addAndConvert(cont, KEY_LISTENERS, listener);
|
|
2986
|
+
addAndConvert(cont, KEY_ERR, errHandler);
|
|
2987
|
+
addAndConvert(cont, KEY_RAW, rawEmitter);
|
|
2988
|
+
} else {
|
|
2989
|
+
watcher = createFsWatchInstance(
|
|
2990
|
+
path10,
|
|
2991
|
+
options,
|
|
2992
|
+
fsWatchBroadcast.bind(null, fullPath, KEY_LISTENERS),
|
|
2993
|
+
errHandler,
|
|
2994
|
+
// no need to use broadcast here
|
|
2995
|
+
fsWatchBroadcast.bind(null, fullPath, KEY_RAW)
|
|
2996
|
+
);
|
|
2997
|
+
if (!watcher)
|
|
2998
|
+
return;
|
|
2999
|
+
watcher.on(EV.ERROR, async (error) => {
|
|
3000
|
+
const broadcastErr = fsWatchBroadcast.bind(null, fullPath, KEY_ERR);
|
|
3001
|
+
if (cont)
|
|
3002
|
+
cont.watcherUnusable = true;
|
|
3003
|
+
if (isWindows && error.code === "EPERM") {
|
|
3004
|
+
try {
|
|
3005
|
+
const fd = await open(path10, "r");
|
|
3006
|
+
await fd.close();
|
|
3007
|
+
broadcastErr(error);
|
|
3008
|
+
} catch (err) {
|
|
3009
|
+
}
|
|
3010
|
+
} else {
|
|
3011
|
+
broadcastErr(error);
|
|
3012
|
+
}
|
|
3013
|
+
});
|
|
3014
|
+
cont = {
|
|
3015
|
+
listeners: listener,
|
|
3016
|
+
errHandlers: errHandler,
|
|
3017
|
+
rawEmitters: rawEmitter,
|
|
3018
|
+
watcher
|
|
3019
|
+
};
|
|
3020
|
+
FsWatchInstances.set(fullPath, cont);
|
|
3021
|
+
}
|
|
3022
|
+
return () => {
|
|
3023
|
+
delFromSet(cont, KEY_LISTENERS, listener);
|
|
3024
|
+
delFromSet(cont, KEY_ERR, errHandler);
|
|
3025
|
+
delFromSet(cont, KEY_RAW, rawEmitter);
|
|
3026
|
+
if (isEmptySet(cont.listeners)) {
|
|
3027
|
+
cont.watcher.close();
|
|
3028
|
+
FsWatchInstances.delete(fullPath);
|
|
3029
|
+
HANDLER_KEYS.forEach(clearItem(cont));
|
|
3030
|
+
cont.watcher = void 0;
|
|
3031
|
+
Object.freeze(cont);
|
|
3032
|
+
}
|
|
3033
|
+
};
|
|
3034
|
+
};
|
|
3035
|
+
var FsWatchFileInstances = /* @__PURE__ */ new Map();
|
|
3036
|
+
var setFsWatchFileListener = (path10, fullPath, options, handlers) => {
|
|
3037
|
+
const { listener, rawEmitter } = handlers;
|
|
3038
|
+
let cont = FsWatchFileInstances.get(fullPath);
|
|
3039
|
+
const copts = cont && cont.options;
|
|
3040
|
+
if (copts && (copts.persistent < options.persistent || copts.interval > options.interval)) {
|
|
3041
|
+
unwatchFile(fullPath);
|
|
3042
|
+
cont = void 0;
|
|
3043
|
+
}
|
|
3044
|
+
if (cont) {
|
|
3045
|
+
addAndConvert(cont, KEY_LISTENERS, listener);
|
|
3046
|
+
addAndConvert(cont, KEY_RAW, rawEmitter);
|
|
3047
|
+
} else {
|
|
3048
|
+
cont = {
|
|
3049
|
+
listeners: listener,
|
|
3050
|
+
rawEmitters: rawEmitter,
|
|
3051
|
+
options,
|
|
3052
|
+
watcher: watchFile(fullPath, options, (curr, prev) => {
|
|
3053
|
+
foreach(cont.rawEmitters, (rawEmitter2) => {
|
|
3054
|
+
rawEmitter2(EV.CHANGE, fullPath, { curr, prev });
|
|
3055
|
+
});
|
|
3056
|
+
const currmtime = curr.mtimeMs;
|
|
3057
|
+
if (curr.size !== prev.size || currmtime > prev.mtimeMs || currmtime === 0) {
|
|
3058
|
+
foreach(cont.listeners, (listener2) => listener2(path10, curr));
|
|
3059
|
+
}
|
|
3060
|
+
})
|
|
3061
|
+
};
|
|
3062
|
+
FsWatchFileInstances.set(fullPath, cont);
|
|
3063
|
+
}
|
|
3064
|
+
return () => {
|
|
3065
|
+
delFromSet(cont, KEY_LISTENERS, listener);
|
|
3066
|
+
delFromSet(cont, KEY_RAW, rawEmitter);
|
|
3067
|
+
if (isEmptySet(cont.listeners)) {
|
|
3068
|
+
FsWatchFileInstances.delete(fullPath);
|
|
3069
|
+
unwatchFile(fullPath);
|
|
3070
|
+
cont.options = cont.watcher = void 0;
|
|
3071
|
+
Object.freeze(cont);
|
|
3072
|
+
}
|
|
3073
|
+
};
|
|
3074
|
+
};
|
|
3075
|
+
var NodeFsHandler = class {
|
|
3076
|
+
constructor(fsW) {
|
|
3077
|
+
this.fsw = fsW;
|
|
3078
|
+
this._boundHandleError = (error) => fsW._handleError(error);
|
|
3079
|
+
}
|
|
3080
|
+
/**
|
|
3081
|
+
* Watch file for changes with fs_watchFile or fs_watch.
|
|
3082
|
+
* @param path to file or dir
|
|
3083
|
+
* @param listener on fs change
|
|
3084
|
+
* @returns closer for the watcher instance
|
|
3085
|
+
*/
|
|
3086
|
+
_watchWithNodeFs(path10, listener) {
|
|
3087
|
+
const opts = this.fsw.options;
|
|
3088
|
+
const directory = sysPath.dirname(path10);
|
|
3089
|
+
const basename3 = sysPath.basename(path10);
|
|
3090
|
+
const parent = this.fsw._getWatchedDir(directory);
|
|
3091
|
+
parent.add(basename3);
|
|
3092
|
+
const absolutePath = sysPath.resolve(path10);
|
|
3093
|
+
const options = {
|
|
3094
|
+
persistent: opts.persistent
|
|
3095
|
+
};
|
|
3096
|
+
if (!listener)
|
|
3097
|
+
listener = EMPTY_FN;
|
|
3098
|
+
let closer;
|
|
3099
|
+
if (opts.usePolling) {
|
|
3100
|
+
const enableBin = opts.interval !== opts.binaryInterval;
|
|
3101
|
+
options.interval = enableBin && isBinaryPath(basename3) ? opts.binaryInterval : opts.interval;
|
|
3102
|
+
closer = setFsWatchFileListener(path10, absolutePath, options, {
|
|
3103
|
+
listener,
|
|
3104
|
+
rawEmitter: this.fsw._emitRaw
|
|
3105
|
+
});
|
|
3106
|
+
} else {
|
|
3107
|
+
closer = setFsWatchListener(path10, absolutePath, options, {
|
|
3108
|
+
listener,
|
|
3109
|
+
errHandler: this._boundHandleError,
|
|
3110
|
+
rawEmitter: this.fsw._emitRaw
|
|
3111
|
+
});
|
|
3112
|
+
}
|
|
3113
|
+
return closer;
|
|
3114
|
+
}
|
|
3115
|
+
/**
|
|
3116
|
+
* Watch a file and emit add event if warranted.
|
|
3117
|
+
* @returns closer for the watcher instance
|
|
3118
|
+
*/
|
|
3119
|
+
_handleFile(file, stats, initialAdd) {
|
|
3120
|
+
if (this.fsw.closed) {
|
|
3121
|
+
return;
|
|
3122
|
+
}
|
|
3123
|
+
const dirname3 = sysPath.dirname(file);
|
|
3124
|
+
const basename3 = sysPath.basename(file);
|
|
3125
|
+
const parent = this.fsw._getWatchedDir(dirname3);
|
|
3126
|
+
let prevStats = stats;
|
|
3127
|
+
if (parent.has(basename3))
|
|
3128
|
+
return;
|
|
3129
|
+
const listener = async (path10, newStats) => {
|
|
3130
|
+
if (!this.fsw._throttle(THROTTLE_MODE_WATCH, file, 5))
|
|
3131
|
+
return;
|
|
3132
|
+
if (!newStats || newStats.mtimeMs === 0) {
|
|
3133
|
+
try {
|
|
3134
|
+
const newStats2 = await stat2(file);
|
|
3135
|
+
if (this.fsw.closed)
|
|
3136
|
+
return;
|
|
3137
|
+
const at = newStats2.atimeMs;
|
|
3138
|
+
const mt = newStats2.mtimeMs;
|
|
3139
|
+
if (!at || at <= mt || mt !== prevStats.mtimeMs) {
|
|
3140
|
+
this.fsw._emit(EV.CHANGE, file, newStats2);
|
|
3141
|
+
}
|
|
3142
|
+
if ((isMacos || isLinux || isFreeBSD) && prevStats.ino !== newStats2.ino) {
|
|
3143
|
+
this.fsw._closeFile(path10);
|
|
3144
|
+
prevStats = newStats2;
|
|
3145
|
+
const closer2 = this._watchWithNodeFs(file, listener);
|
|
3146
|
+
if (closer2)
|
|
3147
|
+
this.fsw._addPathCloser(path10, closer2);
|
|
3148
|
+
} else {
|
|
3149
|
+
prevStats = newStats2;
|
|
3150
|
+
}
|
|
3151
|
+
} catch (error) {
|
|
3152
|
+
this.fsw._remove(dirname3, basename3);
|
|
3153
|
+
}
|
|
3154
|
+
} else if (parent.has(basename3)) {
|
|
3155
|
+
const at = newStats.atimeMs;
|
|
3156
|
+
const mt = newStats.mtimeMs;
|
|
3157
|
+
if (!at || at <= mt || mt !== prevStats.mtimeMs) {
|
|
3158
|
+
this.fsw._emit(EV.CHANGE, file, newStats);
|
|
3159
|
+
}
|
|
3160
|
+
prevStats = newStats;
|
|
3161
|
+
}
|
|
3162
|
+
};
|
|
3163
|
+
const closer = this._watchWithNodeFs(file, listener);
|
|
3164
|
+
if (!(initialAdd && this.fsw.options.ignoreInitial) && this.fsw._isntIgnored(file)) {
|
|
3165
|
+
if (!this.fsw._throttle(EV.ADD, file, 0))
|
|
3166
|
+
return;
|
|
3167
|
+
this.fsw._emit(EV.ADD, file, stats);
|
|
3168
|
+
}
|
|
3169
|
+
return closer;
|
|
3170
|
+
}
|
|
3171
|
+
/**
|
|
3172
|
+
* Handle symlinks encountered while reading a dir.
|
|
3173
|
+
* @param entry returned by readdirp
|
|
3174
|
+
* @param directory path of dir being read
|
|
3175
|
+
* @param path of this item
|
|
3176
|
+
* @param item basename of this item
|
|
3177
|
+
* @returns true if no more processing is needed for this entry.
|
|
3178
|
+
*/
|
|
3179
|
+
async _handleSymlink(entry, directory, path10, item) {
|
|
3180
|
+
if (this.fsw.closed) {
|
|
3181
|
+
return;
|
|
3182
|
+
}
|
|
3183
|
+
const full = entry.fullPath;
|
|
3184
|
+
const dir = this.fsw._getWatchedDir(directory);
|
|
3185
|
+
if (!this.fsw.options.followSymlinks) {
|
|
3186
|
+
this.fsw._incrReadyCount();
|
|
3187
|
+
let linkPath;
|
|
3188
|
+
try {
|
|
3189
|
+
linkPath = await fsrealpath(path10);
|
|
3190
|
+
} catch (e) {
|
|
3191
|
+
this.fsw._emitReady();
|
|
3192
|
+
return true;
|
|
3193
|
+
}
|
|
3194
|
+
if (this.fsw.closed)
|
|
3195
|
+
return;
|
|
3196
|
+
if (dir.has(item)) {
|
|
3197
|
+
if (this.fsw._symlinkPaths.get(full) !== linkPath) {
|
|
3198
|
+
this.fsw._symlinkPaths.set(full, linkPath);
|
|
3199
|
+
this.fsw._emit(EV.CHANGE, path10, entry.stats);
|
|
3200
|
+
}
|
|
3201
|
+
} else {
|
|
3202
|
+
dir.add(item);
|
|
3203
|
+
this.fsw._symlinkPaths.set(full, linkPath);
|
|
3204
|
+
this.fsw._emit(EV.ADD, path10, entry.stats);
|
|
3205
|
+
}
|
|
3206
|
+
this.fsw._emitReady();
|
|
3207
|
+
return true;
|
|
3208
|
+
}
|
|
3209
|
+
if (this.fsw._symlinkPaths.has(full)) {
|
|
3210
|
+
return true;
|
|
3211
|
+
}
|
|
3212
|
+
this.fsw._symlinkPaths.set(full, true);
|
|
3213
|
+
}
|
|
3214
|
+
_handleRead(directory, initialAdd, wh, target, dir, depth, throttler) {
|
|
3215
|
+
directory = sysPath.join(directory, "");
|
|
3216
|
+
throttler = this.fsw._throttle("readdir", directory, 1e3);
|
|
3217
|
+
if (!throttler)
|
|
3218
|
+
return;
|
|
3219
|
+
const previous = this.fsw._getWatchedDir(wh.path);
|
|
3220
|
+
const current = /* @__PURE__ */ new Set();
|
|
3221
|
+
let stream = this.fsw._readdirp(directory, {
|
|
3222
|
+
fileFilter: (entry) => wh.filterPath(entry),
|
|
3223
|
+
directoryFilter: (entry) => wh.filterDir(entry)
|
|
3224
|
+
});
|
|
3225
|
+
if (!stream)
|
|
3226
|
+
return;
|
|
3227
|
+
stream.on(STR_DATA, async (entry) => {
|
|
3228
|
+
if (this.fsw.closed) {
|
|
3229
|
+
stream = void 0;
|
|
3230
|
+
return;
|
|
3231
|
+
}
|
|
3232
|
+
const item = entry.path;
|
|
3233
|
+
let path10 = sysPath.join(directory, item);
|
|
3234
|
+
current.add(item);
|
|
3235
|
+
if (entry.stats.isSymbolicLink() && await this._handleSymlink(entry, directory, path10, item)) {
|
|
3236
|
+
return;
|
|
3237
|
+
}
|
|
3238
|
+
if (this.fsw.closed) {
|
|
3239
|
+
stream = void 0;
|
|
3240
|
+
return;
|
|
3241
|
+
}
|
|
3242
|
+
if (item === target || !target && !previous.has(item)) {
|
|
3243
|
+
this.fsw._incrReadyCount();
|
|
3244
|
+
path10 = sysPath.join(dir, sysPath.relative(dir, path10));
|
|
3245
|
+
this._addToNodeFs(path10, initialAdd, wh, depth + 1);
|
|
3246
|
+
}
|
|
3247
|
+
}).on(EV.ERROR, this._boundHandleError);
|
|
3248
|
+
return new Promise((resolve3, reject) => {
|
|
3249
|
+
if (!stream)
|
|
3250
|
+
return reject();
|
|
3251
|
+
stream.once(STR_END, () => {
|
|
3252
|
+
if (this.fsw.closed) {
|
|
3253
|
+
stream = void 0;
|
|
3254
|
+
return;
|
|
3255
|
+
}
|
|
3256
|
+
const wasThrottled = throttler ? throttler.clear() : false;
|
|
3257
|
+
resolve3(void 0);
|
|
3258
|
+
previous.getChildren().filter((item) => {
|
|
3259
|
+
return item !== directory && !current.has(item);
|
|
3260
|
+
}).forEach((item) => {
|
|
3261
|
+
this.fsw._remove(directory, item);
|
|
3262
|
+
});
|
|
3263
|
+
stream = void 0;
|
|
3264
|
+
if (wasThrottled)
|
|
3265
|
+
this._handleRead(directory, false, wh, target, dir, depth, throttler);
|
|
3266
|
+
});
|
|
3267
|
+
});
|
|
3268
|
+
}
|
|
3269
|
+
/**
|
|
3270
|
+
* Read directory to add / remove files from `@watched` list and re-read it on change.
|
|
3271
|
+
* @param dir fs path
|
|
3272
|
+
* @param stats
|
|
3273
|
+
* @param initialAdd
|
|
3274
|
+
* @param depth relative to user-supplied path
|
|
3275
|
+
* @param target child path targeted for watch
|
|
3276
|
+
* @param wh Common watch helpers for this path
|
|
3277
|
+
* @param realpath
|
|
3278
|
+
* @returns closer for the watcher instance.
|
|
3279
|
+
*/
|
|
3280
|
+
async _handleDir(dir, stats, initialAdd, depth, target, wh, realpath2) {
|
|
3281
|
+
const parentDir = this.fsw._getWatchedDir(sysPath.dirname(dir));
|
|
3282
|
+
const tracked = parentDir.has(sysPath.basename(dir));
|
|
3283
|
+
if (!(initialAdd && this.fsw.options.ignoreInitial) && !target && !tracked) {
|
|
3284
|
+
this.fsw._emit(EV.ADD_DIR, dir, stats);
|
|
3285
|
+
}
|
|
3286
|
+
parentDir.add(sysPath.basename(dir));
|
|
3287
|
+
this.fsw._getWatchedDir(dir);
|
|
3288
|
+
let throttler;
|
|
3289
|
+
let closer;
|
|
3290
|
+
const oDepth = this.fsw.options.depth;
|
|
3291
|
+
if ((oDepth == null || depth <= oDepth) && !this.fsw._symlinkPaths.has(realpath2)) {
|
|
3292
|
+
if (!target) {
|
|
3293
|
+
await this._handleRead(dir, initialAdd, wh, target, dir, depth, throttler);
|
|
3294
|
+
if (this.fsw.closed)
|
|
3295
|
+
return;
|
|
3296
|
+
}
|
|
3297
|
+
closer = this._watchWithNodeFs(dir, (dirPath, stats2) => {
|
|
3298
|
+
if (stats2 && stats2.mtimeMs === 0)
|
|
3299
|
+
return;
|
|
3300
|
+
this._handleRead(dirPath, false, wh, target, dir, depth, throttler);
|
|
3301
|
+
});
|
|
3302
|
+
}
|
|
3303
|
+
return closer;
|
|
3304
|
+
}
|
|
3305
|
+
/**
|
|
3306
|
+
* Handle added file, directory, or glob pattern.
|
|
3307
|
+
* Delegates call to _handleFile / _handleDir after checks.
|
|
3308
|
+
* @param path to file or ir
|
|
3309
|
+
* @param initialAdd was the file added at watch instantiation?
|
|
3310
|
+
* @param priorWh depth relative to user-supplied path
|
|
3311
|
+
* @param depth Child path actually targeted for watch
|
|
3312
|
+
* @param target Child path actually targeted for watch
|
|
3313
|
+
*/
|
|
3314
|
+
async _addToNodeFs(path10, initialAdd, priorWh, depth, target) {
|
|
3315
|
+
const ready = this.fsw._emitReady;
|
|
3316
|
+
if (this.fsw._isIgnored(path10) || this.fsw.closed) {
|
|
3317
|
+
ready();
|
|
3318
|
+
return false;
|
|
3319
|
+
}
|
|
3320
|
+
const wh = this.fsw._getWatchHelpers(path10);
|
|
3321
|
+
if (priorWh) {
|
|
3322
|
+
wh.filterPath = (entry) => priorWh.filterPath(entry);
|
|
3323
|
+
wh.filterDir = (entry) => priorWh.filterDir(entry);
|
|
3324
|
+
}
|
|
3325
|
+
try {
|
|
3326
|
+
const stats = await statMethods[wh.statMethod](wh.watchPath);
|
|
3327
|
+
if (this.fsw.closed)
|
|
3328
|
+
return;
|
|
3329
|
+
if (this.fsw._isIgnored(wh.watchPath, stats)) {
|
|
3330
|
+
ready();
|
|
3331
|
+
return false;
|
|
3332
|
+
}
|
|
3333
|
+
const follow = this.fsw.options.followSymlinks;
|
|
3334
|
+
let closer;
|
|
3335
|
+
if (stats.isDirectory()) {
|
|
3336
|
+
const absPath = sysPath.resolve(path10);
|
|
3337
|
+
const targetPath = follow ? await fsrealpath(path10) : path10;
|
|
3338
|
+
if (this.fsw.closed)
|
|
3339
|
+
return;
|
|
3340
|
+
closer = await this._handleDir(wh.watchPath, stats, initialAdd, depth, target, wh, targetPath);
|
|
3341
|
+
if (this.fsw.closed)
|
|
3342
|
+
return;
|
|
3343
|
+
if (absPath !== targetPath && targetPath !== void 0) {
|
|
3344
|
+
this.fsw._symlinkPaths.set(absPath, targetPath);
|
|
3345
|
+
}
|
|
3346
|
+
} else if (stats.isSymbolicLink()) {
|
|
3347
|
+
const targetPath = follow ? await fsrealpath(path10) : path10;
|
|
3348
|
+
if (this.fsw.closed)
|
|
3349
|
+
return;
|
|
3350
|
+
const parent = sysPath.dirname(wh.watchPath);
|
|
3351
|
+
this.fsw._getWatchedDir(parent).add(wh.watchPath);
|
|
3352
|
+
this.fsw._emit(EV.ADD, wh.watchPath, stats);
|
|
3353
|
+
closer = await this._handleDir(parent, stats, initialAdd, depth, path10, wh, targetPath);
|
|
3354
|
+
if (this.fsw.closed)
|
|
3355
|
+
return;
|
|
3356
|
+
if (targetPath !== void 0) {
|
|
3357
|
+
this.fsw._symlinkPaths.set(sysPath.resolve(path10), targetPath);
|
|
3358
|
+
}
|
|
3359
|
+
} else {
|
|
3360
|
+
closer = this._handleFile(wh.watchPath, stats, initialAdd);
|
|
3361
|
+
}
|
|
3362
|
+
ready();
|
|
3363
|
+
if (closer)
|
|
3364
|
+
this.fsw._addPathCloser(path10, closer);
|
|
3365
|
+
return false;
|
|
3366
|
+
} catch (error) {
|
|
3367
|
+
if (this.fsw._handleError(error)) {
|
|
3368
|
+
ready();
|
|
3369
|
+
return path10;
|
|
3370
|
+
}
|
|
3371
|
+
}
|
|
3372
|
+
}
|
|
3373
|
+
};
|
|
3374
|
+
|
|
3375
|
+
// ../../node_modules/chokidar/esm/index.js
|
|
3376
|
+
var SLASH = "/";
|
|
3377
|
+
var SLASH_SLASH = "//";
|
|
3378
|
+
var ONE_DOT = ".";
|
|
3379
|
+
var TWO_DOTS = "..";
|
|
3380
|
+
var STRING_TYPE = "string";
|
|
3381
|
+
var BACK_SLASH_RE = /\\/g;
|
|
3382
|
+
var DOUBLE_SLASH_RE = /\/\//;
|
|
3383
|
+
var DOT_RE = /\..*\.(sw[px])$|~$|\.subl.*\.tmp/;
|
|
3384
|
+
var REPLACER_RE = /^\.[/\\]/;
|
|
3385
|
+
function arrify(item) {
|
|
3386
|
+
return Array.isArray(item) ? item : [item];
|
|
3387
|
+
}
|
|
3388
|
+
var isMatcherObject = (matcher) => typeof matcher === "object" && matcher !== null && !(matcher instanceof RegExp);
|
|
3389
|
+
function createPattern(matcher) {
|
|
3390
|
+
if (typeof matcher === "function")
|
|
3391
|
+
return matcher;
|
|
3392
|
+
if (typeof matcher === "string")
|
|
3393
|
+
return (string) => matcher === string;
|
|
3394
|
+
if (matcher instanceof RegExp)
|
|
3395
|
+
return (string) => matcher.test(string);
|
|
3396
|
+
if (typeof matcher === "object" && matcher !== null) {
|
|
3397
|
+
return (string) => {
|
|
3398
|
+
if (matcher.path === string)
|
|
3399
|
+
return true;
|
|
3400
|
+
if (matcher.recursive) {
|
|
3401
|
+
const relative3 = sysPath2.relative(matcher.path, string);
|
|
3402
|
+
if (!relative3) {
|
|
3403
|
+
return false;
|
|
3404
|
+
}
|
|
3405
|
+
return !relative3.startsWith("..") && !sysPath2.isAbsolute(relative3);
|
|
3406
|
+
}
|
|
3407
|
+
return false;
|
|
3408
|
+
};
|
|
3409
|
+
}
|
|
3410
|
+
return () => false;
|
|
3411
|
+
}
|
|
3412
|
+
function normalizePath(path10) {
|
|
3413
|
+
if (typeof path10 !== "string")
|
|
3414
|
+
throw new Error("string expected");
|
|
3415
|
+
path10 = sysPath2.normalize(path10);
|
|
3416
|
+
path10 = path10.replace(/\\/g, "/");
|
|
3417
|
+
let prepend = false;
|
|
3418
|
+
if (path10.startsWith("//"))
|
|
3419
|
+
prepend = true;
|
|
3420
|
+
const DOUBLE_SLASH_RE2 = /\/\//;
|
|
3421
|
+
while (path10.match(DOUBLE_SLASH_RE2))
|
|
3422
|
+
path10 = path10.replace(DOUBLE_SLASH_RE2, "/");
|
|
3423
|
+
if (prepend)
|
|
3424
|
+
path10 = "/" + path10;
|
|
3425
|
+
return path10;
|
|
3426
|
+
}
|
|
3427
|
+
function matchPatterns(patterns, testString, stats) {
|
|
3428
|
+
const path10 = normalizePath(testString);
|
|
3429
|
+
for (let index = 0; index < patterns.length; index++) {
|
|
3430
|
+
const pattern = patterns[index];
|
|
3431
|
+
if (pattern(path10, stats)) {
|
|
3432
|
+
return true;
|
|
3433
|
+
}
|
|
3434
|
+
}
|
|
3435
|
+
return false;
|
|
3436
|
+
}
|
|
3437
|
+
function anymatch(matchers, testString) {
|
|
3438
|
+
if (matchers == null) {
|
|
3439
|
+
throw new TypeError("anymatch: specify first argument");
|
|
3440
|
+
}
|
|
3441
|
+
const matchersArray = arrify(matchers);
|
|
3442
|
+
const patterns = matchersArray.map((matcher) => createPattern(matcher));
|
|
3443
|
+
if (testString == null) {
|
|
3444
|
+
return (testString2, stats) => {
|
|
3445
|
+
return matchPatterns(patterns, testString2, stats);
|
|
3446
|
+
};
|
|
3447
|
+
}
|
|
3448
|
+
return matchPatterns(patterns, testString);
|
|
3449
|
+
}
|
|
3450
|
+
var unifyPaths = (paths_) => {
|
|
3451
|
+
const paths = arrify(paths_).flat();
|
|
3452
|
+
if (!paths.every((p) => typeof p === STRING_TYPE)) {
|
|
3453
|
+
throw new TypeError(`Non-string provided as watch path: ${paths}`);
|
|
3454
|
+
}
|
|
3455
|
+
return paths.map(normalizePathToUnix);
|
|
3456
|
+
};
|
|
3457
|
+
var toUnix = (string) => {
|
|
3458
|
+
let str = string.replace(BACK_SLASH_RE, SLASH);
|
|
3459
|
+
let prepend = false;
|
|
3460
|
+
if (str.startsWith(SLASH_SLASH)) {
|
|
3461
|
+
prepend = true;
|
|
3462
|
+
}
|
|
3463
|
+
while (str.match(DOUBLE_SLASH_RE)) {
|
|
3464
|
+
str = str.replace(DOUBLE_SLASH_RE, SLASH);
|
|
3465
|
+
}
|
|
3466
|
+
if (prepend) {
|
|
3467
|
+
str = SLASH + str;
|
|
3468
|
+
}
|
|
3469
|
+
return str;
|
|
3470
|
+
};
|
|
3471
|
+
var normalizePathToUnix = (path10) => toUnix(sysPath2.normalize(toUnix(path10)));
|
|
3472
|
+
var normalizeIgnored = (cwd = "") => (path10) => {
|
|
3473
|
+
if (typeof path10 === "string") {
|
|
3474
|
+
return normalizePathToUnix(sysPath2.isAbsolute(path10) ? path10 : sysPath2.join(cwd, path10));
|
|
3475
|
+
} else {
|
|
3476
|
+
return path10;
|
|
3477
|
+
}
|
|
3478
|
+
};
|
|
3479
|
+
var getAbsolutePath = (path10, cwd) => {
|
|
3480
|
+
if (sysPath2.isAbsolute(path10)) {
|
|
3481
|
+
return path10;
|
|
3482
|
+
}
|
|
3483
|
+
return sysPath2.join(cwd, path10);
|
|
3484
|
+
};
|
|
3485
|
+
var EMPTY_SET = Object.freeze(/* @__PURE__ */ new Set());
|
|
3486
|
+
var DirEntry = class {
|
|
3487
|
+
constructor(dir, removeWatcher) {
|
|
3488
|
+
this.path = dir;
|
|
3489
|
+
this._removeWatcher = removeWatcher;
|
|
3490
|
+
this.items = /* @__PURE__ */ new Set();
|
|
3491
|
+
}
|
|
3492
|
+
add(item) {
|
|
3493
|
+
const { items } = this;
|
|
3494
|
+
if (!items)
|
|
3495
|
+
return;
|
|
3496
|
+
if (item !== ONE_DOT && item !== TWO_DOTS)
|
|
3497
|
+
items.add(item);
|
|
3498
|
+
}
|
|
3499
|
+
async remove(item) {
|
|
3500
|
+
const { items } = this;
|
|
3501
|
+
if (!items)
|
|
3502
|
+
return;
|
|
3503
|
+
items.delete(item);
|
|
3504
|
+
if (items.size > 0)
|
|
3505
|
+
return;
|
|
3506
|
+
const dir = this.path;
|
|
3507
|
+
try {
|
|
3508
|
+
await readdir2(dir);
|
|
3509
|
+
} catch (err) {
|
|
3510
|
+
if (this._removeWatcher) {
|
|
3511
|
+
this._removeWatcher(sysPath2.dirname(dir), sysPath2.basename(dir));
|
|
3512
|
+
}
|
|
3513
|
+
}
|
|
3514
|
+
}
|
|
3515
|
+
has(item) {
|
|
3516
|
+
const { items } = this;
|
|
3517
|
+
if (!items)
|
|
3518
|
+
return;
|
|
3519
|
+
return items.has(item);
|
|
3520
|
+
}
|
|
3521
|
+
getChildren() {
|
|
3522
|
+
const { items } = this;
|
|
3523
|
+
if (!items)
|
|
3524
|
+
return [];
|
|
3525
|
+
return [...items.values()];
|
|
3526
|
+
}
|
|
3527
|
+
dispose() {
|
|
3528
|
+
this.items.clear();
|
|
3529
|
+
this.path = "";
|
|
3530
|
+
this._removeWatcher = EMPTY_FN;
|
|
3531
|
+
this.items = EMPTY_SET;
|
|
3532
|
+
Object.freeze(this);
|
|
3533
|
+
}
|
|
3534
|
+
};
|
|
3535
|
+
var STAT_METHOD_F = "stat";
|
|
3536
|
+
var STAT_METHOD_L = "lstat";
|
|
3537
|
+
var WatchHelper = class {
|
|
3538
|
+
constructor(path10, follow, fsw) {
|
|
3539
|
+
this.fsw = fsw;
|
|
3540
|
+
const watchPath = path10;
|
|
3541
|
+
this.path = path10 = path10.replace(REPLACER_RE, "");
|
|
3542
|
+
this.watchPath = watchPath;
|
|
3543
|
+
this.fullWatchPath = sysPath2.resolve(watchPath);
|
|
3544
|
+
this.dirParts = [];
|
|
3545
|
+
this.dirParts.forEach((parts) => {
|
|
3546
|
+
if (parts.length > 1)
|
|
3547
|
+
parts.pop();
|
|
3548
|
+
});
|
|
3549
|
+
this.followSymlinks = follow;
|
|
3550
|
+
this.statMethod = follow ? STAT_METHOD_F : STAT_METHOD_L;
|
|
3551
|
+
}
|
|
3552
|
+
entryPath(entry) {
|
|
3553
|
+
return sysPath2.join(this.watchPath, sysPath2.relative(this.watchPath, entry.fullPath));
|
|
3554
|
+
}
|
|
3555
|
+
filterPath(entry) {
|
|
3556
|
+
const { stats } = entry;
|
|
3557
|
+
if (stats && stats.isSymbolicLink())
|
|
3558
|
+
return this.filterDir(entry);
|
|
3559
|
+
const resolvedPath = this.entryPath(entry);
|
|
3560
|
+
return this.fsw._isntIgnored(resolvedPath, stats) && this.fsw._hasReadPermissions(stats);
|
|
3561
|
+
}
|
|
3562
|
+
filterDir(entry) {
|
|
3563
|
+
return this.fsw._isntIgnored(this.entryPath(entry), entry.stats);
|
|
3564
|
+
}
|
|
3565
|
+
};
|
|
3566
|
+
var FSWatcher = class extends EventEmitter {
|
|
3567
|
+
// Not indenting methods for history sake; for now.
|
|
3568
|
+
constructor(_opts = {}) {
|
|
3569
|
+
super();
|
|
3570
|
+
this.closed = false;
|
|
3571
|
+
this._closers = /* @__PURE__ */ new Map();
|
|
3572
|
+
this._ignoredPaths = /* @__PURE__ */ new Set();
|
|
3573
|
+
this._throttled = /* @__PURE__ */ new Map();
|
|
3574
|
+
this._streams = /* @__PURE__ */ new Set();
|
|
3575
|
+
this._symlinkPaths = /* @__PURE__ */ new Map();
|
|
3576
|
+
this._watched = /* @__PURE__ */ new Map();
|
|
3577
|
+
this._pendingWrites = /* @__PURE__ */ new Map();
|
|
3578
|
+
this._pendingUnlinks = /* @__PURE__ */ new Map();
|
|
3579
|
+
this._readyCount = 0;
|
|
3580
|
+
this._readyEmitted = false;
|
|
3581
|
+
const awf = _opts.awaitWriteFinish;
|
|
3582
|
+
const DEF_AWF = { stabilityThreshold: 2e3, pollInterval: 100 };
|
|
3583
|
+
const opts = {
|
|
3584
|
+
// Defaults
|
|
3585
|
+
persistent: true,
|
|
3586
|
+
ignoreInitial: false,
|
|
3587
|
+
ignorePermissionErrors: false,
|
|
3588
|
+
interval: 100,
|
|
3589
|
+
binaryInterval: 300,
|
|
3590
|
+
followSymlinks: true,
|
|
3591
|
+
usePolling: false,
|
|
3592
|
+
// useAsync: false,
|
|
3593
|
+
atomic: true,
|
|
3594
|
+
// NOTE: overwritten later (depends on usePolling)
|
|
3595
|
+
..._opts,
|
|
3596
|
+
// Change format
|
|
3597
|
+
ignored: _opts.ignored ? arrify(_opts.ignored) : arrify([]),
|
|
3598
|
+
awaitWriteFinish: awf === true ? DEF_AWF : typeof awf === "object" ? { ...DEF_AWF, ...awf } : false
|
|
3599
|
+
};
|
|
3600
|
+
if (isIBMi)
|
|
3601
|
+
opts.usePolling = true;
|
|
3602
|
+
if (opts.atomic === void 0)
|
|
3603
|
+
opts.atomic = !opts.usePolling;
|
|
3604
|
+
const envPoll = process.env.CHOKIDAR_USEPOLLING;
|
|
3605
|
+
if (envPoll !== void 0) {
|
|
3606
|
+
const envLower = envPoll.toLowerCase();
|
|
3607
|
+
if (envLower === "false" || envLower === "0")
|
|
3608
|
+
opts.usePolling = false;
|
|
3609
|
+
else if (envLower === "true" || envLower === "1")
|
|
3610
|
+
opts.usePolling = true;
|
|
3611
|
+
else
|
|
3612
|
+
opts.usePolling = !!envLower;
|
|
3613
|
+
}
|
|
3614
|
+
const envInterval = process.env.CHOKIDAR_INTERVAL;
|
|
3615
|
+
if (envInterval)
|
|
3616
|
+
opts.interval = Number.parseInt(envInterval, 10);
|
|
3617
|
+
let readyCalls = 0;
|
|
3618
|
+
this._emitReady = () => {
|
|
3619
|
+
readyCalls++;
|
|
3620
|
+
if (readyCalls >= this._readyCount) {
|
|
3621
|
+
this._emitReady = EMPTY_FN;
|
|
3622
|
+
this._readyEmitted = true;
|
|
3623
|
+
process.nextTick(() => this.emit(EVENTS.READY));
|
|
3624
|
+
}
|
|
3625
|
+
};
|
|
3626
|
+
this._emitRaw = (...args) => this.emit(EVENTS.RAW, ...args);
|
|
3627
|
+
this._boundRemove = this._remove.bind(this);
|
|
3628
|
+
this.options = opts;
|
|
3629
|
+
this._nodeFsHandler = new NodeFsHandler(this);
|
|
3630
|
+
Object.freeze(opts);
|
|
3631
|
+
}
|
|
3632
|
+
_addIgnoredPath(matcher) {
|
|
3633
|
+
if (isMatcherObject(matcher)) {
|
|
3634
|
+
for (const ignored of this._ignoredPaths) {
|
|
3635
|
+
if (isMatcherObject(ignored) && ignored.path === matcher.path && ignored.recursive === matcher.recursive) {
|
|
3636
|
+
return;
|
|
3637
|
+
}
|
|
3638
|
+
}
|
|
3639
|
+
}
|
|
3640
|
+
this._ignoredPaths.add(matcher);
|
|
3641
|
+
}
|
|
3642
|
+
_removeIgnoredPath(matcher) {
|
|
3643
|
+
this._ignoredPaths.delete(matcher);
|
|
3644
|
+
if (typeof matcher === "string") {
|
|
3645
|
+
for (const ignored of this._ignoredPaths) {
|
|
3646
|
+
if (isMatcherObject(ignored) && ignored.path === matcher) {
|
|
3647
|
+
this._ignoredPaths.delete(ignored);
|
|
3648
|
+
}
|
|
3649
|
+
}
|
|
3650
|
+
}
|
|
3651
|
+
}
|
|
3652
|
+
// Public methods
|
|
3653
|
+
/**
|
|
3654
|
+
* Adds paths to be watched on an existing FSWatcher instance.
|
|
3655
|
+
* @param paths_ file or file list. Other arguments are unused
|
|
3656
|
+
*/
|
|
3657
|
+
add(paths_, _origAdd, _internal) {
|
|
3658
|
+
const { cwd } = this.options;
|
|
3659
|
+
this.closed = false;
|
|
3660
|
+
this._closePromise = void 0;
|
|
3661
|
+
let paths = unifyPaths(paths_);
|
|
3662
|
+
if (cwd) {
|
|
3663
|
+
paths = paths.map((path10) => {
|
|
3664
|
+
const absPath = getAbsolutePath(path10, cwd);
|
|
3665
|
+
return absPath;
|
|
3666
|
+
});
|
|
3667
|
+
}
|
|
3668
|
+
paths.forEach((path10) => {
|
|
3669
|
+
this._removeIgnoredPath(path10);
|
|
3670
|
+
});
|
|
3671
|
+
this._userIgnored = void 0;
|
|
3672
|
+
if (!this._readyCount)
|
|
3673
|
+
this._readyCount = 0;
|
|
3674
|
+
this._readyCount += paths.length;
|
|
3675
|
+
Promise.all(paths.map(async (path10) => {
|
|
3676
|
+
const res = await this._nodeFsHandler._addToNodeFs(path10, !_internal, void 0, 0, _origAdd);
|
|
3677
|
+
if (res)
|
|
3678
|
+
this._emitReady();
|
|
3679
|
+
return res;
|
|
3680
|
+
})).then((results) => {
|
|
3681
|
+
if (this.closed)
|
|
3682
|
+
return;
|
|
3683
|
+
results.forEach((item) => {
|
|
3684
|
+
if (item)
|
|
3685
|
+
this.add(sysPath2.dirname(item), sysPath2.basename(_origAdd || item));
|
|
3686
|
+
});
|
|
3687
|
+
});
|
|
3688
|
+
return this;
|
|
3689
|
+
}
|
|
3690
|
+
/**
|
|
3691
|
+
* Close watchers or start ignoring events from specified paths.
|
|
3692
|
+
*/
|
|
3693
|
+
unwatch(paths_) {
|
|
3694
|
+
if (this.closed)
|
|
3695
|
+
return this;
|
|
3696
|
+
const paths = unifyPaths(paths_);
|
|
3697
|
+
const { cwd } = this.options;
|
|
3698
|
+
paths.forEach((path10) => {
|
|
3699
|
+
if (!sysPath2.isAbsolute(path10) && !this._closers.has(path10)) {
|
|
3700
|
+
if (cwd)
|
|
3701
|
+
path10 = sysPath2.join(cwd, path10);
|
|
3702
|
+
path10 = sysPath2.resolve(path10);
|
|
3703
|
+
}
|
|
3704
|
+
this._closePath(path10);
|
|
3705
|
+
this._addIgnoredPath(path10);
|
|
3706
|
+
if (this._watched.has(path10)) {
|
|
3707
|
+
this._addIgnoredPath({
|
|
3708
|
+
path: path10,
|
|
3709
|
+
recursive: true
|
|
3710
|
+
});
|
|
3711
|
+
}
|
|
3712
|
+
this._userIgnored = void 0;
|
|
3713
|
+
});
|
|
3714
|
+
return this;
|
|
3715
|
+
}
|
|
3716
|
+
/**
|
|
3717
|
+
* Close watchers and remove all listeners from watched paths.
|
|
3718
|
+
*/
|
|
3719
|
+
close() {
|
|
3720
|
+
if (this._closePromise) {
|
|
3721
|
+
return this._closePromise;
|
|
3722
|
+
}
|
|
3723
|
+
this.closed = true;
|
|
3724
|
+
this.removeAllListeners();
|
|
3725
|
+
const closers = [];
|
|
3726
|
+
this._closers.forEach((closerList) => closerList.forEach((closer) => {
|
|
3727
|
+
const promise = closer();
|
|
3728
|
+
if (promise instanceof Promise)
|
|
3729
|
+
closers.push(promise);
|
|
3730
|
+
}));
|
|
3731
|
+
this._streams.forEach((stream) => stream.destroy());
|
|
3732
|
+
this._userIgnored = void 0;
|
|
3733
|
+
this._readyCount = 0;
|
|
3734
|
+
this._readyEmitted = false;
|
|
3735
|
+
this._watched.forEach((dirent) => dirent.dispose());
|
|
3736
|
+
this._closers.clear();
|
|
3737
|
+
this._watched.clear();
|
|
3738
|
+
this._streams.clear();
|
|
3739
|
+
this._symlinkPaths.clear();
|
|
3740
|
+
this._throttled.clear();
|
|
3741
|
+
this._closePromise = closers.length ? Promise.all(closers).then(() => void 0) : Promise.resolve();
|
|
3742
|
+
return this._closePromise;
|
|
3743
|
+
}
|
|
3744
|
+
/**
|
|
3745
|
+
* Expose list of watched paths
|
|
3746
|
+
* @returns for chaining
|
|
3747
|
+
*/
|
|
3748
|
+
getWatched() {
|
|
3749
|
+
const watchList = {};
|
|
3750
|
+
this._watched.forEach((entry, dir) => {
|
|
3751
|
+
const key = this.options.cwd ? sysPath2.relative(this.options.cwd, dir) : dir;
|
|
3752
|
+
const index = key || ONE_DOT;
|
|
3753
|
+
watchList[index] = entry.getChildren().sort();
|
|
3754
|
+
});
|
|
3755
|
+
return watchList;
|
|
3756
|
+
}
|
|
3757
|
+
emitWithAll(event, args) {
|
|
3758
|
+
this.emit(event, ...args);
|
|
3759
|
+
if (event !== EVENTS.ERROR)
|
|
3760
|
+
this.emit(EVENTS.ALL, event, ...args);
|
|
3761
|
+
}
|
|
3762
|
+
// Common helpers
|
|
3763
|
+
// --------------
|
|
3764
|
+
/**
|
|
3765
|
+
* Normalize and emit events.
|
|
3766
|
+
* Calling _emit DOES NOT MEAN emit() would be called!
|
|
3767
|
+
* @param event Type of event
|
|
3768
|
+
* @param path File or directory path
|
|
3769
|
+
* @param stats arguments to be passed with event
|
|
3770
|
+
* @returns the error if defined, otherwise the value of the FSWatcher instance's `closed` flag
|
|
3771
|
+
*/
|
|
3772
|
+
async _emit(event, path10, stats) {
|
|
3773
|
+
if (this.closed)
|
|
3774
|
+
return;
|
|
3775
|
+
const opts = this.options;
|
|
3776
|
+
if (isWindows)
|
|
3777
|
+
path10 = sysPath2.normalize(path10);
|
|
3778
|
+
if (opts.cwd)
|
|
3779
|
+
path10 = sysPath2.relative(opts.cwd, path10);
|
|
3780
|
+
const args = [path10];
|
|
3781
|
+
if (stats != null)
|
|
3782
|
+
args.push(stats);
|
|
3783
|
+
const awf = opts.awaitWriteFinish;
|
|
3784
|
+
let pw;
|
|
3785
|
+
if (awf && (pw = this._pendingWrites.get(path10))) {
|
|
3786
|
+
pw.lastChange = /* @__PURE__ */ new Date();
|
|
3787
|
+
return this;
|
|
3788
|
+
}
|
|
3789
|
+
if (opts.atomic) {
|
|
3790
|
+
if (event === EVENTS.UNLINK) {
|
|
3791
|
+
this._pendingUnlinks.set(path10, [event, ...args]);
|
|
3792
|
+
setTimeout(() => {
|
|
3793
|
+
this._pendingUnlinks.forEach((entry, path11) => {
|
|
3794
|
+
this.emit(...entry);
|
|
3795
|
+
this.emit(EVENTS.ALL, ...entry);
|
|
3796
|
+
this._pendingUnlinks.delete(path11);
|
|
3797
|
+
});
|
|
3798
|
+
}, typeof opts.atomic === "number" ? opts.atomic : 100);
|
|
3799
|
+
return this;
|
|
3800
|
+
}
|
|
3801
|
+
if (event === EVENTS.ADD && this._pendingUnlinks.has(path10)) {
|
|
3802
|
+
event = EVENTS.CHANGE;
|
|
3803
|
+
this._pendingUnlinks.delete(path10);
|
|
3804
|
+
}
|
|
3805
|
+
}
|
|
3806
|
+
if (awf && (event === EVENTS.ADD || event === EVENTS.CHANGE) && this._readyEmitted) {
|
|
3807
|
+
const awfEmit = (err, stats2) => {
|
|
3808
|
+
if (err) {
|
|
3809
|
+
event = EVENTS.ERROR;
|
|
3810
|
+
args[0] = err;
|
|
3811
|
+
this.emitWithAll(event, args);
|
|
3812
|
+
} else if (stats2) {
|
|
3813
|
+
if (args.length > 1) {
|
|
3814
|
+
args[1] = stats2;
|
|
3815
|
+
} else {
|
|
3816
|
+
args.push(stats2);
|
|
3817
|
+
}
|
|
3818
|
+
this.emitWithAll(event, args);
|
|
3819
|
+
}
|
|
3820
|
+
};
|
|
3821
|
+
this._awaitWriteFinish(path10, awf.stabilityThreshold, event, awfEmit);
|
|
3822
|
+
return this;
|
|
3823
|
+
}
|
|
3824
|
+
if (event === EVENTS.CHANGE) {
|
|
3825
|
+
const isThrottled = !this._throttle(EVENTS.CHANGE, path10, 50);
|
|
3826
|
+
if (isThrottled)
|
|
3827
|
+
return this;
|
|
3828
|
+
}
|
|
3829
|
+
if (opts.alwaysStat && stats === void 0 && (event === EVENTS.ADD || event === EVENTS.ADD_DIR || event === EVENTS.CHANGE)) {
|
|
3830
|
+
const fullPath = opts.cwd ? sysPath2.join(opts.cwd, path10) : path10;
|
|
3831
|
+
let stats2;
|
|
3832
|
+
try {
|
|
3833
|
+
stats2 = await stat3(fullPath);
|
|
3834
|
+
} catch (err) {
|
|
3835
|
+
}
|
|
3836
|
+
if (!stats2 || this.closed)
|
|
3837
|
+
return;
|
|
3838
|
+
args.push(stats2);
|
|
3839
|
+
}
|
|
3840
|
+
this.emitWithAll(event, args);
|
|
3841
|
+
return this;
|
|
3842
|
+
}
|
|
3843
|
+
/**
|
|
3844
|
+
* Common handler for errors
|
|
3845
|
+
* @returns The error if defined, otherwise the value of the FSWatcher instance's `closed` flag
|
|
3846
|
+
*/
|
|
3847
|
+
_handleError(error) {
|
|
3848
|
+
const code = error && error.code;
|
|
3849
|
+
if (error && code !== "ENOENT" && code !== "ENOTDIR" && (!this.options.ignorePermissionErrors || code !== "EPERM" && code !== "EACCES")) {
|
|
3850
|
+
this.emit(EVENTS.ERROR, error);
|
|
3851
|
+
}
|
|
3852
|
+
return error || this.closed;
|
|
3853
|
+
}
|
|
3854
|
+
/**
|
|
3855
|
+
* Helper utility for throttling
|
|
3856
|
+
* @param actionType type being throttled
|
|
3857
|
+
* @param path being acted upon
|
|
3858
|
+
* @param timeout duration of time to suppress duplicate actions
|
|
3859
|
+
* @returns tracking object or false if action should be suppressed
|
|
3860
|
+
*/
|
|
3861
|
+
_throttle(actionType, path10, timeout) {
|
|
3862
|
+
if (!this._throttled.has(actionType)) {
|
|
3863
|
+
this._throttled.set(actionType, /* @__PURE__ */ new Map());
|
|
3864
|
+
}
|
|
3865
|
+
const action = this._throttled.get(actionType);
|
|
3866
|
+
if (!action)
|
|
3867
|
+
throw new Error("invalid throttle");
|
|
3868
|
+
const actionPath = action.get(path10);
|
|
3869
|
+
if (actionPath) {
|
|
3870
|
+
actionPath.count++;
|
|
3871
|
+
return false;
|
|
3872
|
+
}
|
|
3873
|
+
let timeoutObject;
|
|
3874
|
+
const clear = () => {
|
|
3875
|
+
const item = action.get(path10);
|
|
3876
|
+
const count = item ? item.count : 0;
|
|
3877
|
+
action.delete(path10);
|
|
3878
|
+
clearTimeout(timeoutObject);
|
|
3879
|
+
if (item)
|
|
3880
|
+
clearTimeout(item.timeoutObject);
|
|
3881
|
+
return count;
|
|
3882
|
+
};
|
|
3883
|
+
timeoutObject = setTimeout(clear, timeout);
|
|
3884
|
+
const thr = { timeoutObject, clear, count: 0 };
|
|
3885
|
+
action.set(path10, thr);
|
|
3886
|
+
return thr;
|
|
3887
|
+
}
|
|
3888
|
+
_incrReadyCount() {
|
|
3889
|
+
return this._readyCount++;
|
|
3890
|
+
}
|
|
3891
|
+
/**
|
|
3892
|
+
* Awaits write operation to finish.
|
|
3893
|
+
* Polls a newly created file for size variations. When files size does not change for 'threshold' milliseconds calls callback.
|
|
3894
|
+
* @param path being acted upon
|
|
3895
|
+
* @param threshold Time in milliseconds a file size must be fixed before acknowledging write OP is finished
|
|
3896
|
+
* @param event
|
|
3897
|
+
* @param awfEmit Callback to be called when ready for event to be emitted.
|
|
3898
|
+
*/
|
|
3899
|
+
_awaitWriteFinish(path10, threshold, event, awfEmit) {
|
|
3900
|
+
const awf = this.options.awaitWriteFinish;
|
|
3901
|
+
if (typeof awf !== "object")
|
|
3902
|
+
return;
|
|
3903
|
+
const pollInterval = awf.pollInterval;
|
|
3904
|
+
let timeoutHandler;
|
|
3905
|
+
let fullPath = path10;
|
|
3906
|
+
if (this.options.cwd && !sysPath2.isAbsolute(path10)) {
|
|
3907
|
+
fullPath = sysPath2.join(this.options.cwd, path10);
|
|
3908
|
+
}
|
|
3909
|
+
const now = /* @__PURE__ */ new Date();
|
|
3910
|
+
const writes = this._pendingWrites;
|
|
3911
|
+
function awaitWriteFinishFn(prevStat) {
|
|
3912
|
+
statcb(fullPath, (err, curStat) => {
|
|
3913
|
+
if (err || !writes.has(path10)) {
|
|
3914
|
+
if (err && err.code !== "ENOENT")
|
|
3915
|
+
awfEmit(err);
|
|
3916
|
+
return;
|
|
3917
|
+
}
|
|
3918
|
+
const now2 = Number(/* @__PURE__ */ new Date());
|
|
3919
|
+
if (prevStat && curStat.size !== prevStat.size) {
|
|
3920
|
+
writes.get(path10).lastChange = now2;
|
|
3921
|
+
}
|
|
3922
|
+
const pw = writes.get(path10);
|
|
3923
|
+
const df = now2 - pw.lastChange;
|
|
3924
|
+
if (df >= threshold) {
|
|
3925
|
+
writes.delete(path10);
|
|
3926
|
+
awfEmit(void 0, curStat);
|
|
3927
|
+
} else {
|
|
3928
|
+
timeoutHandler = setTimeout(awaitWriteFinishFn, pollInterval, curStat);
|
|
3929
|
+
}
|
|
3930
|
+
});
|
|
3931
|
+
}
|
|
3932
|
+
if (!writes.has(path10)) {
|
|
3933
|
+
writes.set(path10, {
|
|
3934
|
+
lastChange: now,
|
|
3935
|
+
cancelWait: () => {
|
|
3936
|
+
writes.delete(path10);
|
|
3937
|
+
clearTimeout(timeoutHandler);
|
|
3938
|
+
return event;
|
|
3939
|
+
}
|
|
3940
|
+
});
|
|
3941
|
+
timeoutHandler = setTimeout(awaitWriteFinishFn, pollInterval);
|
|
3942
|
+
}
|
|
3943
|
+
}
|
|
3944
|
+
/**
|
|
3945
|
+
* Determines whether user has asked to ignore this path.
|
|
3946
|
+
*/
|
|
3947
|
+
_isIgnored(path10, stats) {
|
|
3948
|
+
if (this.options.atomic && DOT_RE.test(path10))
|
|
3949
|
+
return true;
|
|
3950
|
+
if (!this._userIgnored) {
|
|
3951
|
+
const { cwd } = this.options;
|
|
3952
|
+
const ign = this.options.ignored;
|
|
3953
|
+
const ignored = (ign || []).map(normalizeIgnored(cwd));
|
|
3954
|
+
const ignoredPaths = [...this._ignoredPaths];
|
|
3955
|
+
const list = [...ignoredPaths.map(normalizeIgnored(cwd)), ...ignored];
|
|
3956
|
+
this._userIgnored = anymatch(list, void 0);
|
|
3957
|
+
}
|
|
3958
|
+
return this._userIgnored(path10, stats);
|
|
3959
|
+
}
|
|
3960
|
+
_isntIgnored(path10, stat4) {
|
|
3961
|
+
return !this._isIgnored(path10, stat4);
|
|
3962
|
+
}
|
|
3963
|
+
/**
|
|
3964
|
+
* Provides a set of common helpers and properties relating to symlink handling.
|
|
3965
|
+
* @param path file or directory pattern being watched
|
|
3966
|
+
*/
|
|
3967
|
+
_getWatchHelpers(path10) {
|
|
3968
|
+
return new WatchHelper(path10, this.options.followSymlinks, this);
|
|
3969
|
+
}
|
|
3970
|
+
// Directory helpers
|
|
3971
|
+
// -----------------
|
|
3972
|
+
/**
|
|
3973
|
+
* Provides directory tracking objects
|
|
3974
|
+
* @param directory path of the directory
|
|
3975
|
+
*/
|
|
3976
|
+
_getWatchedDir(directory) {
|
|
3977
|
+
const dir = sysPath2.resolve(directory);
|
|
3978
|
+
if (!this._watched.has(dir))
|
|
3979
|
+
this._watched.set(dir, new DirEntry(dir, this._boundRemove));
|
|
3980
|
+
return this._watched.get(dir);
|
|
3981
|
+
}
|
|
3982
|
+
// File helpers
|
|
3983
|
+
// ------------
|
|
3984
|
+
/**
|
|
3985
|
+
* Check for read permissions: https://stackoverflow.com/a/11781404/1358405
|
|
3986
|
+
*/
|
|
3987
|
+
_hasReadPermissions(stats) {
|
|
3988
|
+
if (this.options.ignorePermissionErrors)
|
|
3989
|
+
return true;
|
|
3990
|
+
return Boolean(Number(stats.mode) & 256);
|
|
3991
|
+
}
|
|
3992
|
+
/**
|
|
3993
|
+
* Handles emitting unlink events for
|
|
3994
|
+
* files and directories, and via recursion, for
|
|
3995
|
+
* files and directories within directories that are unlinked
|
|
3996
|
+
* @param directory within which the following item is located
|
|
3997
|
+
* @param item base path of item/directory
|
|
3998
|
+
*/
|
|
3999
|
+
_remove(directory, item, isDirectory) {
|
|
4000
|
+
const path10 = sysPath2.join(directory, item);
|
|
4001
|
+
const fullPath = sysPath2.resolve(path10);
|
|
4002
|
+
isDirectory = isDirectory != null ? isDirectory : this._watched.has(path10) || this._watched.has(fullPath);
|
|
4003
|
+
if (!this._throttle("remove", path10, 100))
|
|
4004
|
+
return;
|
|
4005
|
+
if (!isDirectory && this._watched.size === 1) {
|
|
4006
|
+
this.add(directory, item, true);
|
|
4007
|
+
}
|
|
4008
|
+
const wp = this._getWatchedDir(path10);
|
|
4009
|
+
const nestedDirectoryChildren = wp.getChildren();
|
|
4010
|
+
nestedDirectoryChildren.forEach((nested) => this._remove(path10, nested));
|
|
4011
|
+
const parent = this._getWatchedDir(directory);
|
|
4012
|
+
const wasTracked = parent.has(item);
|
|
4013
|
+
parent.remove(item);
|
|
4014
|
+
if (this._symlinkPaths.has(fullPath)) {
|
|
4015
|
+
this._symlinkPaths.delete(fullPath);
|
|
4016
|
+
}
|
|
4017
|
+
let relPath = path10;
|
|
4018
|
+
if (this.options.cwd)
|
|
4019
|
+
relPath = sysPath2.relative(this.options.cwd, path10);
|
|
4020
|
+
if (this.options.awaitWriteFinish && this._pendingWrites.has(relPath)) {
|
|
4021
|
+
const event = this._pendingWrites.get(relPath).cancelWait();
|
|
4022
|
+
if (event === EVENTS.ADD)
|
|
4023
|
+
return;
|
|
4024
|
+
}
|
|
4025
|
+
this._watched.delete(path10);
|
|
4026
|
+
this._watched.delete(fullPath);
|
|
4027
|
+
const eventName = isDirectory ? EVENTS.UNLINK_DIR : EVENTS.UNLINK;
|
|
4028
|
+
if (wasTracked && !this._isIgnored(path10))
|
|
4029
|
+
this._emit(eventName, path10);
|
|
4030
|
+
this._closePath(path10);
|
|
4031
|
+
}
|
|
4032
|
+
/**
|
|
4033
|
+
* Closes all watchers for a path
|
|
4034
|
+
*/
|
|
4035
|
+
_closePath(path10) {
|
|
4036
|
+
this._closeFile(path10);
|
|
4037
|
+
const dir = sysPath2.dirname(path10);
|
|
4038
|
+
this._getWatchedDir(dir).remove(sysPath2.basename(path10));
|
|
4039
|
+
}
|
|
4040
|
+
/**
|
|
4041
|
+
* Closes only file-specific watchers
|
|
4042
|
+
*/
|
|
4043
|
+
_closeFile(path10) {
|
|
4044
|
+
const closers = this._closers.get(path10);
|
|
4045
|
+
if (!closers)
|
|
4046
|
+
return;
|
|
4047
|
+
closers.forEach((closer) => closer());
|
|
4048
|
+
this._closers.delete(path10);
|
|
4049
|
+
}
|
|
4050
|
+
_addPathCloser(path10, closer) {
|
|
4051
|
+
if (!closer)
|
|
4052
|
+
return;
|
|
4053
|
+
let list = this._closers.get(path10);
|
|
4054
|
+
if (!list) {
|
|
4055
|
+
list = [];
|
|
4056
|
+
this._closers.set(path10, list);
|
|
4057
|
+
}
|
|
4058
|
+
list.push(closer);
|
|
4059
|
+
}
|
|
4060
|
+
_readdirp(root, opts) {
|
|
4061
|
+
if (this.closed)
|
|
4062
|
+
return;
|
|
4063
|
+
const options = { type: EVENTS.ALL, alwaysStat: true, lstat: true, ...opts, depth: 0 };
|
|
4064
|
+
let stream = readdirp(root, options);
|
|
4065
|
+
this._streams.add(stream);
|
|
4066
|
+
stream.once(STR_CLOSE, () => {
|
|
4067
|
+
stream = void 0;
|
|
4068
|
+
});
|
|
4069
|
+
stream.once(STR_END, () => {
|
|
4070
|
+
if (stream) {
|
|
4071
|
+
this._streams.delete(stream);
|
|
4072
|
+
stream = void 0;
|
|
4073
|
+
}
|
|
4074
|
+
});
|
|
4075
|
+
return stream;
|
|
4076
|
+
}
|
|
4077
|
+
};
|
|
4078
|
+
function watch(paths, options = {}) {
|
|
4079
|
+
const watcher = new FSWatcher(options);
|
|
4080
|
+
watcher.add(paths);
|
|
4081
|
+
return watcher;
|
|
4082
|
+
}
|
|
4083
|
+
var esm_default = { watch, FSWatcher };
|
|
4084
|
+
|
|
4085
|
+
// ../../apps/server/src/services/session-broadcaster.ts
|
|
4086
|
+
import { join as join3 } from "path";
|
|
4087
|
+
var SessionBroadcaster = class {
|
|
4088
|
+
constructor(transcriptReader2) {
|
|
4089
|
+
this.transcriptReader = transcriptReader2;
|
|
4090
|
+
}
|
|
4091
|
+
clients = /* @__PURE__ */ new Map();
|
|
4092
|
+
watchers = /* @__PURE__ */ new Map();
|
|
4093
|
+
offsets = /* @__PURE__ */ new Map();
|
|
4094
|
+
debounceTimers = /* @__PURE__ */ new Map();
|
|
4095
|
+
/**
|
|
4096
|
+
* Register an SSE client for a session.
|
|
4097
|
+
*
|
|
4098
|
+
* - Adds the response to the set of connected clients
|
|
4099
|
+
* - Starts a file watcher if none exists for this session
|
|
4100
|
+
* - Initializes offset to current file size (only broadcast new content)
|
|
4101
|
+
* - Sends sync_connected event to the client
|
|
4102
|
+
* - Auto-deregisters on response close
|
|
4103
|
+
*
|
|
4104
|
+
* @param sessionId - Session UUID
|
|
4105
|
+
* @param vaultRoot - Vault root path for resolving transcript directory
|
|
4106
|
+
* @param res - Express Response object configured for SSE
|
|
4107
|
+
*/
|
|
4108
|
+
registerClient(sessionId, vaultRoot2, res) {
|
|
4109
|
+
if (!this.clients.has(sessionId)) {
|
|
4110
|
+
this.clients.set(sessionId, /* @__PURE__ */ new Set());
|
|
4111
|
+
}
|
|
4112
|
+
this.clients.get(sessionId).add(res);
|
|
4113
|
+
if (!this.watchers.has(sessionId)) {
|
|
4114
|
+
this.startWatcher(sessionId, vaultRoot2);
|
|
4115
|
+
}
|
|
4116
|
+
res.write(`event: sync_connected
|
|
4117
|
+
data: ${JSON.stringify({ sessionId })}
|
|
4118
|
+
|
|
4119
|
+
`);
|
|
4120
|
+
res.on("close", () => {
|
|
4121
|
+
this.deregisterClient(sessionId, res);
|
|
4122
|
+
});
|
|
4123
|
+
}
|
|
4124
|
+
/**
|
|
4125
|
+
* Deregister an SSE client from a session.
|
|
4126
|
+
*
|
|
4127
|
+
* - Removes the response from the client set
|
|
4128
|
+
* - Stops the file watcher if no clients remain for this session
|
|
4129
|
+
* - Cleans up offsets and timers
|
|
4130
|
+
*
|
|
4131
|
+
* @param sessionId - Session UUID
|
|
4132
|
+
* @param res - Express Response object to remove
|
|
4133
|
+
*/
|
|
4134
|
+
deregisterClient(sessionId, res) {
|
|
4135
|
+
const clientSet = this.clients.get(sessionId);
|
|
4136
|
+
if (!clientSet) return;
|
|
4137
|
+
clientSet.delete(res);
|
|
4138
|
+
if (clientSet.size === 0) {
|
|
4139
|
+
this.clients.delete(sessionId);
|
|
4140
|
+
const watcher = this.watchers.get(sessionId);
|
|
4141
|
+
if (watcher) {
|
|
4142
|
+
watcher.close();
|
|
4143
|
+
this.watchers.delete(sessionId);
|
|
4144
|
+
}
|
|
4145
|
+
this.offsets.delete(sessionId);
|
|
4146
|
+
const timer = this.debounceTimers.get(sessionId);
|
|
4147
|
+
if (timer) {
|
|
4148
|
+
clearTimeout(timer);
|
|
4149
|
+
this.debounceTimers.delete(sessionId);
|
|
4150
|
+
}
|
|
4151
|
+
}
|
|
4152
|
+
}
|
|
4153
|
+
/**
|
|
4154
|
+
* Start a chokidar file watcher for a session's JSONL transcript.
|
|
4155
|
+
*
|
|
4156
|
+
* Watches the SDK transcript file and broadcasts updates on change events.
|
|
4157
|
+
* Changes are debounced (100ms) to batch rapid writes during streaming.
|
|
4158
|
+
*
|
|
4159
|
+
* @param sessionId - Session UUID
|
|
4160
|
+
* @param vaultRoot - Vault root path for resolving transcript directory
|
|
4161
|
+
*/
|
|
4162
|
+
startWatcher(sessionId, vaultRoot2) {
|
|
4163
|
+
const transcriptsDir = this.transcriptReader.getTranscriptsDir(vaultRoot2);
|
|
4164
|
+
const filePath = join3(transcriptsDir, `${sessionId}.jsonl`);
|
|
4165
|
+
this.initializeOffset(vaultRoot2, sessionId);
|
|
4166
|
+
const watcher = esm_default.watch(filePath, {
|
|
4167
|
+
persistent: true,
|
|
4168
|
+
ignoreInitial: true,
|
|
4169
|
+
// Don't fire for initial file scan
|
|
4170
|
+
awaitWriteFinish: {
|
|
4171
|
+
stabilityThreshold: 50,
|
|
4172
|
+
pollInterval: 25
|
|
4173
|
+
}
|
|
4174
|
+
});
|
|
4175
|
+
watcher.on("change", () => {
|
|
4176
|
+
const existingTimer = this.debounceTimers.get(sessionId);
|
|
4177
|
+
if (existingTimer) {
|
|
4178
|
+
clearTimeout(existingTimer);
|
|
4179
|
+
}
|
|
4180
|
+
const timer = setTimeout(() => {
|
|
4181
|
+
this.debounceTimers.delete(sessionId);
|
|
4182
|
+
this.broadcastUpdate(sessionId, vaultRoot2).catch((err) => {
|
|
4183
|
+
console.error(`[SessionBroadcaster] Failed to broadcast update for session ${sessionId}:`, err);
|
|
4184
|
+
});
|
|
4185
|
+
}, 100);
|
|
4186
|
+
this.debounceTimers.set(sessionId, timer);
|
|
4187
|
+
});
|
|
4188
|
+
this.watchers.set(sessionId, watcher);
|
|
4189
|
+
}
|
|
4190
|
+
/**
|
|
4191
|
+
* Initialize the byte offset for a session to the current file size.
|
|
4192
|
+
* This ensures we only broadcast new content, not existing history.
|
|
4193
|
+
*
|
|
4194
|
+
* @param vaultRoot - Vault root path
|
|
4195
|
+
* @param sessionId - Session UUID
|
|
4196
|
+
*/
|
|
4197
|
+
async initializeOffset(vaultRoot2, sessionId) {
|
|
4198
|
+
try {
|
|
4199
|
+
const { newOffset } = await this.transcriptReader.readFromOffset(vaultRoot2, sessionId, 0);
|
|
4200
|
+
this.offsets.set(sessionId, newOffset);
|
|
4201
|
+
} catch (err) {
|
|
4202
|
+
this.offsets.set(sessionId, 0);
|
|
4203
|
+
}
|
|
4204
|
+
}
|
|
4205
|
+
/**
|
|
4206
|
+
* Broadcast a sync_update event to all connected clients for a session.
|
|
4207
|
+
*
|
|
4208
|
+
* Reads new content from the transcript file since the last offset and
|
|
4209
|
+
* sends a sync_update SSE event if new content exists.
|
|
4210
|
+
*
|
|
4211
|
+
* @param sessionId - Session UUID
|
|
4212
|
+
* @param vaultRoot - Vault root path
|
|
4213
|
+
*/
|
|
4214
|
+
async broadcastUpdate(sessionId, vaultRoot2) {
|
|
4215
|
+
const currentOffset = this.offsets.get(sessionId) ?? 0;
|
|
4216
|
+
try {
|
|
4217
|
+
const { content, newOffset } = await this.transcriptReader.readFromOffset(
|
|
4218
|
+
vaultRoot2,
|
|
4219
|
+
sessionId,
|
|
4220
|
+
currentOffset
|
|
4221
|
+
);
|
|
4222
|
+
this.offsets.set(sessionId, newOffset);
|
|
4223
|
+
if (content.length === 0) {
|
|
4224
|
+
return;
|
|
4225
|
+
}
|
|
4226
|
+
const clientSet = this.clients.get(sessionId);
|
|
4227
|
+
if (!clientSet || clientSet.size === 0) {
|
|
4228
|
+
return;
|
|
4229
|
+
}
|
|
4230
|
+
const event = {
|
|
4231
|
+
sessionId,
|
|
4232
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
4233
|
+
};
|
|
4234
|
+
const eventData = `event: sync_update
|
|
4235
|
+
data: ${JSON.stringify(event)}
|
|
4236
|
+
|
|
4237
|
+
`;
|
|
4238
|
+
for (const client of Array.from(clientSet)) {
|
|
4239
|
+
try {
|
|
4240
|
+
client.write(eventData);
|
|
4241
|
+
} catch (err) {
|
|
4242
|
+
console.error(`[SessionBroadcaster] Failed to write to client for session ${sessionId}:`, err);
|
|
4243
|
+
}
|
|
4244
|
+
}
|
|
4245
|
+
} catch (err) {
|
|
4246
|
+
console.error(`[SessionBroadcaster] Failed to read offset for session ${sessionId}:`, err);
|
|
4247
|
+
}
|
|
4248
|
+
}
|
|
4249
|
+
/**
|
|
4250
|
+
* Shutdown the broadcaster, closing all watchers and client connections.
|
|
4251
|
+
* Should be called on server shutdown.
|
|
4252
|
+
*/
|
|
4253
|
+
shutdown() {
|
|
4254
|
+
Array.from(this.debounceTimers.values()).forEach((timer) => {
|
|
4255
|
+
clearTimeout(timer);
|
|
4256
|
+
});
|
|
4257
|
+
this.debounceTimers.clear();
|
|
4258
|
+
Array.from(this.watchers.values()).forEach((watcher) => {
|
|
4259
|
+
watcher.close();
|
|
4260
|
+
});
|
|
4261
|
+
this.watchers.clear();
|
|
4262
|
+
Array.from(this.clients.values()).forEach((clientSet) => {
|
|
4263
|
+
Array.from(clientSet).forEach((client) => {
|
|
4264
|
+
try {
|
|
4265
|
+
client.end();
|
|
4266
|
+
} catch {
|
|
4267
|
+
}
|
|
4268
|
+
});
|
|
4269
|
+
});
|
|
4270
|
+
this.clients.clear();
|
|
4271
|
+
this.offsets.clear();
|
|
4272
|
+
}
|
|
4273
|
+
};
|
|
4274
|
+
|
|
4275
|
+
// ../../apps/server/src/index.ts
|
|
4276
|
+
var __dirname5 = path9.dirname(fileURLToPath5(import.meta.url));
|
|
4277
|
+
if (!process.env.CLIENT_DIST_PATH) {
|
|
4278
|
+
dotenv.config({ path: path9.join(__dirname5, "../../../.env") });
|
|
4279
|
+
}
|
|
4280
|
+
var PORT = parseInt(process.env.GATEWAY_PORT || "6942", 10);
|
|
4281
|
+
var sessionBroadcaster = null;
|
|
4282
|
+
async function start() {
|
|
4283
|
+
const app = createApp();
|
|
4284
|
+
sessionBroadcaster = new SessionBroadcaster(transcriptReader);
|
|
4285
|
+
app.locals.sessionBroadcaster = sessionBroadcaster;
|
|
4286
|
+
const host = process.env.TUNNEL_ENABLED === "true" ? "0.0.0.0" : "localhost";
|
|
4287
|
+
app.listen(PORT, host, () => {
|
|
4288
|
+
console.log(`Gateway server running on http://localhost:${PORT}`);
|
|
4289
|
+
});
|
|
4290
|
+
setInterval(() => {
|
|
4291
|
+
agentManager.checkSessionHealth();
|
|
4292
|
+
}, 5 * 60 * 1e3);
|
|
4293
|
+
if (process.env.TUNNEL_ENABLED === "true") {
|
|
4294
|
+
const tunnelPort = parseInt(process.env.TUNNEL_PORT || String(PORT), 10);
|
|
4295
|
+
try {
|
|
4296
|
+
const url = await tunnelManager.start({
|
|
4297
|
+
port: tunnelPort,
|
|
4298
|
+
authtoken: process.env.NGROK_AUTHTOKEN,
|
|
4299
|
+
basicAuth: process.env.TUNNEL_AUTH,
|
|
4300
|
+
domain: process.env.TUNNEL_DOMAIN
|
|
4301
|
+
});
|
|
4302
|
+
const hasAuth = !!process.env.TUNNEL_AUTH;
|
|
4303
|
+
const isDevPort = tunnelPort !== PORT;
|
|
4304
|
+
console.log("");
|
|
4305
|
+
console.log("\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510");
|
|
4306
|
+
console.log("\u2502 ngrok tunnel active \u2502");
|
|
4307
|
+
console.log("\u2502 \u2502");
|
|
4308
|
+
console.log(`\u2502 URL: ${url.padEnd(40)} \u2502`);
|
|
4309
|
+
console.log(`\u2502 Port: ${String(tunnelPort).padEnd(40)} \u2502`);
|
|
4310
|
+
console.log(`\u2502 Auth: ${(hasAuth ? "basic auth enabled" : "none (open)").padEnd(40)} \u2502`);
|
|
4311
|
+
if (isDevPort) {
|
|
4312
|
+
console.log(`\u2502 Mode: ${("dev (Vite on :" + tunnelPort + ")").padEnd(40)} \u2502`);
|
|
4313
|
+
}
|
|
4314
|
+
console.log("\u2502 \u2502");
|
|
4315
|
+
console.log("\u2502 Free tier: 1GB/month bandwidth, session limits \u2502");
|
|
4316
|
+
console.log("\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518");
|
|
4317
|
+
console.log("");
|
|
4318
|
+
} catch (err) {
|
|
4319
|
+
console.warn("[Tunnel] Failed to start ngrok tunnel:", err instanceof Error ? err.message : err);
|
|
4320
|
+
console.warn("[Tunnel] Server continues without tunnel.");
|
|
4321
|
+
}
|
|
4322
|
+
}
|
|
4323
|
+
}
|
|
4324
|
+
function shutdown() {
|
|
4325
|
+
console.log("\nShutting down...");
|
|
4326
|
+
if (sessionBroadcaster) {
|
|
4327
|
+
sessionBroadcaster.shutdown();
|
|
4328
|
+
}
|
|
4329
|
+
tunnelManager.stop().finally(() => {
|
|
4330
|
+
process.exit(0);
|
|
4331
|
+
});
|
|
4332
|
+
}
|
|
4333
|
+
process.on("SIGINT", shutdown);
|
|
4334
|
+
process.on("SIGTERM", shutdown);
|
|
4335
|
+
start();
|
|
4336
|
+
/*! Bundled license information:
|
|
4337
|
+
|
|
4338
|
+
chokidar/esm/index.js:
|
|
4339
|
+
(*! chokidar - MIT License (c) 2012 Paul Miller (paulmillr.com) *)
|
|
4340
|
+
*/
|
|
4341
|
+
//# sourceMappingURL=index.js.map
|