diffact 0.1.0 → 0.2.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.
Files changed (52) hide show
  1. package/dist/cli.mjs +2 -0
  2. package/dist/index-node-bKTmbwGt.mjs +1 -0
  3. package/dist/src-CPKE75x0.mjs +11 -0
  4. package/dist/src-Ceryd8j5.mjs +1 -0
  5. package/package.json +4 -5
  6. package/web/dist/assets/{code-block-37QAKDTI-C97XC0lL.js → code-block-37QAKDTI-yDNOZoY4.js} +1 -1
  7. package/web/dist/assets/{index-DWCfDth4.js → index-BlaXWu6U.js} +30 -30
  8. package/web/dist/assets/index-DMEToi1s.css +1 -0
  9. package/web/dist/index.html +2 -2
  10. package/dist/agent-manager.d.ts +0 -32
  11. package/dist/agent-manager.js +0 -502
  12. package/dist/app-server-client.d.ts +0 -38
  13. package/dist/app-server-client.js +0 -249
  14. package/dist/capabilities.d.ts +0 -2
  15. package/dist/capabilities.js +0 -27
  16. package/dist/cli.d.ts +0 -6
  17. package/dist/cli.js +0 -13
  18. package/dist/command-runner.d.ts +0 -83
  19. package/dist/command-runner.js +0 -427
  20. package/dist/editors.d.ts +0 -26
  21. package/dist/editors.js +0 -144
  22. package/dist/gh.d.ts +0 -61
  23. package/dist/gh.js +0 -185
  24. package/dist/git.d.ts +0 -57
  25. package/dist/git.js +0 -482
  26. package/dist/http.d.ts +0 -7
  27. package/dist/http.js +0 -98
  28. package/dist/index-node.d.ts +0 -5
  29. package/dist/index-node.js +0 -51
  30. package/dist/index.d.ts +0 -6
  31. package/dist/index.js +0 -1011
  32. package/dist/list-directories.d.ts +0 -8
  33. package/dist/list-directories.js +0 -32
  34. package/dist/log.d.ts +0 -2
  35. package/dist/log.js +0 -2
  36. package/dist/open-browser.d.ts +0 -5
  37. package/dist/open-browser.js +0 -23
  38. package/dist/project-commands.d.ts +0 -17
  39. package/dist/project-commands.js +0 -152
  40. package/dist/project-path.d.ts +0 -5
  41. package/dist/project-path.js +0 -33
  42. package/dist/runtime.d.ts +0 -65
  43. package/dist/runtime.js +0 -235
  44. package/dist/static.d.ts +0 -10
  45. package/dist/static.js +0 -127
  46. package/dist/types.d.ts +0 -17
  47. package/dist/types.js +0 -1
  48. package/dist/utils.d.ts +0 -3
  49. package/dist/utils.js +0 -26
  50. package/dist/ws-hub.d.ts +0 -20
  51. package/dist/ws-hub.js +0 -123
  52. package/web/dist/assets/index-CRDz04kv.css +0 -1
@@ -1,502 +0,0 @@
1
- import { AppServerClient } from "./app-server-client.js";
2
- import { resolveProjectRoot } from "./project-path.js";
3
- import { createId } from "./utils.js";
4
- const getApprovalPolicy = () => process.env.CODEX_APPROVAL_POLICY?.trim() || "on-request";
5
- const getSandboxMode = () => process.env.CODEX_SANDBOX_MODE?.trim() || "workspace-write";
6
- const parseTurnId = (params) => {
7
- if (!params || typeof params !== "object") {
8
- return null;
9
- }
10
- const record = params;
11
- const turn = record.turn;
12
- if (turn && typeof turn === "object" && "id" in turn) {
13
- const id = turn.id;
14
- return typeof id === "string" ? id : null;
15
- }
16
- if ("turnId" in record) {
17
- const id = record.turnId;
18
- return typeof id === "string" ? id : null;
19
- }
20
- return null;
21
- };
22
- const parseThreadId = (params) => {
23
- if (!params || typeof params !== "object") {
24
- return null;
25
- }
26
- const record = params;
27
- if ("threadId" in record) {
28
- const id = record.threadId;
29
- return typeof id === "string" ? id : null;
30
- }
31
- const thread = record.thread;
32
- if (thread && typeof thread === "object" && "id" in thread) {
33
- const id = thread.id;
34
- return typeof id === "string" ? id : null;
35
- }
36
- return null;
37
- };
38
- const parseTurnError = (params) => {
39
- if (!params || typeof params !== "object") {
40
- return null;
41
- }
42
- const record = params;
43
- const error = record.error;
44
- if (error && typeof error === "object" && "message" in error) {
45
- const message = error.message;
46
- return typeof message === "string" ? message : null;
47
- }
48
- return null;
49
- };
50
- const FILTERED_THREAD_CURSOR_PREFIX = "project:";
51
- const parseThreadListResponse = (value) => {
52
- if (!value || typeof value !== "object") {
53
- return { data: [], nextCursor: null };
54
- }
55
- const record = value;
56
- const data = Array.isArray(record.data)
57
- ? record.data.filter((item) => Boolean(item) && typeof item === "object")
58
- : [];
59
- const nextCursor = typeof record.nextCursor === "string" ? record.nextCursor : null;
60
- return { data, nextCursor };
61
- };
62
- const parseThreadListCursor = (cursor) => {
63
- if (!cursor || !cursor.startsWith(FILTERED_THREAD_CURSOR_PREFIX)) {
64
- return null;
65
- }
66
- const payload = cursor.slice(FILTERED_THREAD_CURSOR_PREFIX.length);
67
- try {
68
- const decoded = Buffer.from(payload, "base64").toString("utf8");
69
- const parsed = JSON.parse(decoded);
70
- if (!parsed || typeof parsed !== "object") {
71
- return null;
72
- }
73
- const record = parsed;
74
- const upstreamCursor = typeof record.upstreamCursor === "string" ? record.upstreamCursor : null;
75
- const buffered = Array.isArray(record.buffered)
76
- ? record.buffered.filter((item) => Boolean(item) && typeof item === "object")
77
- : [];
78
- return { upstreamCursor, buffered };
79
- }
80
- catch {
81
- return null;
82
- }
83
- };
84
- const serializeThreadListCursor = (state) => {
85
- const payload = Buffer.from(JSON.stringify(state)).toString("base64");
86
- return `${FILTERED_THREAD_CURSOR_PREFIX}${payload}`;
87
- };
88
- export class AgentManager {
89
- #hub;
90
- #agents = new Map();
91
- #agentsByThread = new Map();
92
- #client;
93
- constructor(params) {
94
- this.#hub = params.hub;
95
- this.#client = new AppServerClient({
96
- onNotification: (notification) => {
97
- this.#routeNotification(notification);
98
- },
99
- onRequest: async (request) => this.#handleRequest(request),
100
- onClose: (error) => {
101
- this.#handleAppServerClose(error);
102
- },
103
- });
104
- }
105
- listAgents() {
106
- return Array.from(this.#agents.values()).map((agent) => this.#toAgentInfo(agent));
107
- }
108
- async listThreads(params) {
109
- await this.#client.initialize({
110
- name: "diffact",
111
- title: "diffact",
112
- version: "0.1.0",
113
- });
114
- if (!params.projectPath) {
115
- return this.#client.request("thread/list", {
116
- cursor: params.cursor ?? null,
117
- limit: params.limit ?? null,
118
- modelProviders: null,
119
- });
120
- }
121
- const limit = params.limit ?? null;
122
- const parsedCursor = parseThreadListCursor(params.cursor ?? null);
123
- let upstreamCursor = parsedCursor?.upstreamCursor ?? params.cursor ?? null;
124
- const buffered = (parsedCursor?.buffered ?? []).filter((item) => item.cwd === params.projectPath);
125
- if (limit !== null && limit <= 0) {
126
- return { data: [], nextCursor: null };
127
- }
128
- if (limit === null) {
129
- const response = await this.#client.request("thread/list", {
130
- cursor: upstreamCursor,
131
- limit: null,
132
- modelProviders: null,
133
- });
134
- const page = parseThreadListResponse(response);
135
- return {
136
- data: page.data.filter((item) => item.cwd === params.projectPath),
137
- nextCursor: page.nextCursor,
138
- };
139
- }
140
- if (buffered.length >= limit) {
141
- const remainder = buffered.slice(limit);
142
- return {
143
- data: buffered.slice(0, limit),
144
- nextCursor: remainder.length
145
- ? serializeThreadListCursor({
146
- upstreamCursor,
147
- buffered: remainder,
148
- })
149
- : upstreamCursor,
150
- };
151
- }
152
- const collected = buffered.slice();
153
- const seen = new Set();
154
- while (true) {
155
- const response = await this.#client.request("thread/list", {
156
- cursor: upstreamCursor,
157
- limit,
158
- modelProviders: null,
159
- });
160
- const page = parseThreadListResponse(response);
161
- collected.push(...page.data.filter((item) => item.cwd === params.projectPath));
162
- if (collected.length >= limit) {
163
- const remainder = collected.slice(limit);
164
- return {
165
- data: collected.slice(0, limit),
166
- nextCursor: remainder.length
167
- ? serializeThreadListCursor({
168
- upstreamCursor: page.nextCursor ?? null,
169
- buffered: remainder,
170
- })
171
- : (page.nextCursor ?? null),
172
- };
173
- }
174
- if (!page.nextCursor) {
175
- return { data: collected, nextCursor: null };
176
- }
177
- if (seen.has(page.nextCursor)) {
178
- return { data: collected, nextCursor: page.nextCursor };
179
- }
180
- seen.add(page.nextCursor);
181
- upstreamCursor = page.nextCursor;
182
- }
183
- }
184
- getAgent(id) {
185
- const agent = this.#agents.get(id);
186
- return agent ? this.#toAgentInfo(agent) : null;
187
- }
188
- async resumeThread(params) {
189
- const existing = this.#agentsByThread.get(params.threadId);
190
- if (existing) {
191
- return this.#toAgentInfo(existing);
192
- }
193
- const id = createId();
194
- const now = new Date().toISOString();
195
- const runtime = {
196
- id,
197
- label: params.label || `thread-${params.threadId.slice(0, 6)}`,
198
- status: "completed",
199
- createdAt: now,
200
- projectPath: params.projectPath,
201
- threadId: params.threadId,
202
- lastError: undefined,
203
- exitCode: null,
204
- signal: null,
205
- stoppedByUser: false,
206
- activeTurnId: null,
207
- };
208
- this.#agents.set(id, runtime);
209
- this.#agentsByThread.set(params.threadId, runtime);
210
- this.#hub.send("agent_status", this.#toAgentInfo(runtime));
211
- try {
212
- await this.#client.initialize({
213
- name: "diffact",
214
- title: "diffact",
215
- version: "0.1.0",
216
- });
217
- const threadResponse = await this.#client.request("thread/resume", {
218
- threadId: params.threadId,
219
- });
220
- const threadId = parseThreadId(threadResponse);
221
- if (threadId && threadId !== params.threadId) {
222
- runtime.threadId = threadId;
223
- this.#agentsByThread.set(threadId, runtime);
224
- }
225
- }
226
- catch (error) {
227
- runtime.status = "error";
228
- runtime.lastError =
229
- error instanceof Error ? error.message : "app_server_error";
230
- this.#hub.send("agent_status", this.#toAgentInfo(runtime));
231
- }
232
- return this.#toAgentInfo(runtime);
233
- }
234
- async startAgent(params) {
235
- const project = await resolveProjectRoot(params.projectPath);
236
- const id = createId();
237
- const now = new Date().toISOString();
238
- const runtime = {
239
- id,
240
- label: params.label || `agent-${id.slice(0, 6)}`,
241
- status: "starting",
242
- createdAt: now,
243
- projectPath: project.rootPath,
244
- threadId: null,
245
- lastError: undefined,
246
- exitCode: null,
247
- signal: null,
248
- stoppedByUser: false,
249
- activeTurnId: null,
250
- };
251
- this.#agents.set(id, runtime);
252
- this.#hub.send("agent_status", this.#toAgentInfo(runtime));
253
- try {
254
- await this.#client.initialize({
255
- name: "diffact",
256
- title: "diffact",
257
- version: "0.1.0",
258
- });
259
- const threadResponse = await this.#client.request("thread/start", {
260
- model: null,
261
- modelProvider: null,
262
- cwd: project.rootPath,
263
- approvalPolicy: getApprovalPolicy(),
264
- sandbox: getSandboxMode(),
265
- config: null,
266
- baseInstructions: null,
267
- developerInstructions: null,
268
- experimentalRawEvents: false,
269
- });
270
- const threadId = parseThreadId(threadResponse);
271
- if (!threadId) {
272
- throw new Error("app_server_thread_start_failed");
273
- }
274
- runtime.threadId = threadId;
275
- this.#agentsByThread.set(threadId, runtime);
276
- this.#hub.send("agent_status", this.#toAgentInfo(runtime));
277
- await this.#client.request("turn/start", {
278
- threadId: runtime.threadId,
279
- input: [{ type: "text", text: params.prompt }],
280
- cwd: null,
281
- approvalPolicy: null,
282
- sandboxPolicy: null,
283
- model: null,
284
- effort: null,
285
- summary: null,
286
- outputSchema: null,
287
- });
288
- }
289
- catch (error) {
290
- runtime.status = "error";
291
- runtime.lastError =
292
- error instanceof Error ? error.message : "app_server_error";
293
- this.#hub.send("agent_status", this.#toAgentInfo(runtime));
294
- }
295
- return this.#toAgentInfo(runtime);
296
- }
297
- async startThread(params) {
298
- const project = await resolveProjectRoot(params.projectPath);
299
- const id = createId();
300
- const now = new Date().toISOString();
301
- const runtime = {
302
- id,
303
- label: params.label || `thread-${id.slice(0, 6)}`,
304
- status: "starting",
305
- createdAt: now,
306
- projectPath: project.rootPath,
307
- threadId: null,
308
- lastError: undefined,
309
- exitCode: null,
310
- signal: null,
311
- stoppedByUser: false,
312
- activeTurnId: null,
313
- };
314
- this.#agents.set(id, runtime);
315
- this.#hub.send("agent_status", this.#toAgentInfo(runtime));
316
- try {
317
- await this.#client.initialize({
318
- name: "diffact",
319
- title: "diffact",
320
- version: "0.1.0",
321
- });
322
- const threadResponse = await this.#client.request("thread/start", {
323
- model: null,
324
- modelProvider: null,
325
- cwd: project.rootPath,
326
- approvalPolicy: getApprovalPolicy(),
327
- sandbox: getSandboxMode(),
328
- config: null,
329
- baseInstructions: null,
330
- developerInstructions: null,
331
- experimentalRawEvents: false,
332
- });
333
- const threadId = parseThreadId(threadResponse);
334
- if (!threadId) {
335
- throw new Error("app_server_thread_start_failed");
336
- }
337
- runtime.threadId = threadId;
338
- runtime.status = "completed";
339
- this.#agentsByThread.set(threadId, runtime);
340
- this.#hub.send("agent_status", this.#toAgentInfo(runtime));
341
- }
342
- catch (error) {
343
- runtime.status = "error";
344
- runtime.lastError =
345
- error instanceof Error ? error.message : "app_server_error";
346
- this.#hub.send("agent_status", this.#toAgentInfo(runtime));
347
- }
348
- return this.#toAgentInfo(runtime);
349
- }
350
- stopAgent(id) {
351
- const agent = this.#agents.get(id);
352
- if (!agent) {
353
- return false;
354
- }
355
- agent.stoppedByUser = true;
356
- agent.status = "stopped";
357
- if (agent.threadId && agent.activeTurnId) {
358
- this.#client
359
- .request("turn/interrupt", {
360
- threadId: agent.threadId,
361
- turnId: agent.activeTurnId,
362
- })
363
- .catch(() => { });
364
- }
365
- if (agent.threadId) {
366
- this.#agentsByThread.delete(agent.threadId);
367
- }
368
- this.#hub.send("agent_status", this.#toAgentInfo(agent));
369
- return true;
370
- }
371
- stopThread(threadId) {
372
- const runtime = this.#agentsByThread.get(threadId);
373
- if (!runtime) {
374
- return false;
375
- }
376
- return this.stopAgent(runtime.id);
377
- }
378
- async sendMessage(id, message) {
379
- const agent = this.#agents.get(id);
380
- if (!agent || !agent.threadId) {
381
- return false;
382
- }
383
- agent.status = "running";
384
- agent.lastError = undefined;
385
- this.#hub.send("agent_status", this.#toAgentInfo(agent));
386
- try {
387
- await this.#client.initialize({
388
- name: "diffact",
389
- title: "diffact",
390
- version: "0.1.0",
391
- });
392
- await this.#client.request("turn/start", {
393
- threadId: agent.threadId,
394
- input: [{ type: "text", text: message }],
395
- cwd: null,
396
- approvalPolicy: null,
397
- sandboxPolicy: null,
398
- model: null,
399
- effort: null,
400
- summary: null,
401
- outputSchema: null,
402
- });
403
- }
404
- catch (error) {
405
- agent.status = "error";
406
- agent.lastError =
407
- error instanceof Error ? error.message : "app_server_error";
408
- this.#hub.send("agent_status", this.#toAgentInfo(agent));
409
- }
410
- return true;
411
- }
412
- #routeNotification(notification) {
413
- const threadId = parseThreadId(notification.params);
414
- if (threadId) {
415
- const runtime = this.#agentsByThread.get(threadId);
416
- if (runtime && !runtime.stoppedByUser) {
417
- this.#handleNotification(runtime, notification);
418
- }
419
- return;
420
- }
421
- for (const runtime of this.#agents.values()) {
422
- if (runtime.stoppedByUser) {
423
- continue;
424
- }
425
- this.#handleNotification(runtime, notification);
426
- }
427
- }
428
- #handleNotification(runtime, notification) {
429
- let statusChanged = false;
430
- if (notification.method === "thread/started") {
431
- const threadId = parseThreadId(notification.params);
432
- if (threadId) {
433
- runtime.threadId = threadId;
434
- this.#agentsByThread.set(threadId, runtime);
435
- statusChanged = true;
436
- }
437
- }
438
- if (notification.method === "turn/started") {
439
- runtime.status = "running";
440
- runtime.activeTurnId = parseTurnId(notification.params);
441
- statusChanged = true;
442
- }
443
- if (notification.method === "turn/completed") {
444
- runtime.status = "completed";
445
- runtime.activeTurnId = null;
446
- statusChanged = true;
447
- }
448
- if (notification.method === "error") {
449
- runtime.status = "error";
450
- runtime.lastError = parseTurnError(notification.params) || "turn_error";
451
- statusChanged = true;
452
- }
453
- if (statusChanged) {
454
- this.#hub.send("agent_status", this.#toAgentInfo(runtime));
455
- }
456
- this.#hub.send("agent_event", {
457
- projectPath: runtime.projectPath,
458
- threadId: runtime.threadId,
459
- event: notification,
460
- });
461
- }
462
- #handleAppServerClose(error) {
463
- this.#agentsByThread.clear();
464
- for (const runtime of this.#agents.values()) {
465
- if (runtime.stoppedByUser) {
466
- continue;
467
- }
468
- runtime.status = "error";
469
- runtime.lastError = error?.message || "app_server_closed";
470
- runtime.activeTurnId = null;
471
- this.#hub.send("agent_status", this.#toAgentInfo(runtime));
472
- }
473
- }
474
- async #handleRequest(request) {
475
- if (request.method === "item/commandExecution/requestApproval") {
476
- return { decision: "accept" };
477
- }
478
- if (request.method === "item/fileChange/requestApproval") {
479
- return { decision: "accept" };
480
- }
481
- if (request.method === "applyPatchApproval") {
482
- return { decision: "approved" };
483
- }
484
- if (request.method === "execCommandApproval") {
485
- return { decision: "approved" };
486
- }
487
- return null;
488
- }
489
- #toAgentInfo(agent) {
490
- return {
491
- id: agent.id,
492
- label: agent.label,
493
- status: agent.status,
494
- createdAt: agent.createdAt,
495
- projectPath: agent.projectPath,
496
- threadId: agent.threadId ?? null,
497
- lastError: agent.lastError,
498
- exitCode: agent.exitCode ?? null,
499
- signal: agent.signal ?? null,
500
- };
501
- }
502
- }
@@ -1,38 +0,0 @@
1
- type RequestId = string | number;
2
- export type AppServerMessage = {
3
- id?: RequestId;
4
- method?: string;
5
- params?: unknown;
6
- result?: unknown;
7
- error?: unknown;
8
- };
9
- export type AppServerNotification = {
10
- method: string;
11
- params?: unknown;
12
- };
13
- export type AppServerRequest = {
14
- id: RequestId;
15
- method: string;
16
- params?: unknown;
17
- };
18
- type AppServerClientOptions = {
19
- command?: string;
20
- args?: string[];
21
- onNotification?: (notification: AppServerNotification) => void;
22
- onRequest?: (request: AppServerRequest) => Promise<unknown> | unknown;
23
- onClose?: (error?: Error) => void;
24
- };
25
- export declare class AppServerClient {
26
- #private;
27
- constructor(options: AppServerClientOptions);
28
- connect(): Promise<void>;
29
- initialize(params: {
30
- name: string;
31
- title?: string;
32
- version: string;
33
- }): Promise<void>;
34
- request(method: string, params?: unknown): Promise<unknown>;
35
- notify(method: string, params?: unknown): void;
36
- close(): void;
37
- }
38
- export {};