@wular/coder 0.3.2 → 0.3.3

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.
@@ -1,4 +1,4 @@
1
- import * as z from 'zod/mini';
1
+ import * as z from "zod/mini";
2
2
  //#region src/lib/config.d.ts
3
3
  /**
4
4
  * Strict schema for a config object (a file's contents or the merged result).
@@ -7,178 +7,111 @@ import * as z from 'zod/mini';
7
7
  * z.infer'd types).
8
8
  */
9
9
  declare const engineSchema: z.ZodMiniEnum<{
10
- codex: 'codex';
11
- claude: 'claude';
10
+ codex: "codex";
11
+ claude: "claude";
12
12
  }>;
13
13
  declare const agentSchema: z.ZodMiniEnum<{
14
- codex: 'codex';
15
- claude: 'claude';
16
- custom: 'custom';
14
+ codex: "codex";
15
+ claude: "claude";
16
+ custom: "custom";
17
17
  }>;
18
18
  declare const effortSchema: z.ZodMiniEnum<{
19
- low: 'low';
20
- medium: 'medium';
21
- high: 'high';
19
+ low: "low";
20
+ medium: "medium";
21
+ high: "high";
22
22
  }>;
23
23
  declare const permissionSchema: z.ZodMiniEnum<{
24
- 'read-only': 'read-only';
25
- 'workspace-write': 'workspace-write';
26
- auto: 'auto';
24
+ "read-only": "read-only";
25
+ "workspace-write": "workspace-write";
26
+ auto: "auto";
27
27
  }>;
28
- declare const customModelSchema: z.ZodMiniObject<
29
- {
28
+ declare const customModelSchema: z.ZodMiniObject<{
29
+ baseUrl: z.ZodMiniURL;
30
+ model: z.ZodMiniString<string>;
31
+ envKey: z.ZodMiniOptional<z.ZodMiniString<string>>;
32
+ wireApi: z.ZodMiniOptional<z.ZodMiniEnum<{
33
+ chat: "chat";
34
+ responses: "responses";
35
+ }>>;
36
+ disabled: z.ZodMiniOptional<z.ZodMiniBoolean<boolean>>;
37
+ }, z.core.$strict>;
38
+ /** The merged, effective shape (everything present after DEFAULT_CONFIG). */
39
+ declare const effectiveConfigSchema: z.ZodMiniObject<{
40
+ chain: z.ZodMiniArray<z.ZodMiniEnum<{
41
+ codex: "codex";
42
+ claude: "claude";
43
+ custom: "custom";
44
+ }>>;
45
+ agents: z.ZodMiniObject<{
46
+ codex: z.ZodMiniOptional<z.ZodMiniObject<{
47
+ model: z.ZodMiniOptional<z.ZodMiniString<string>>;
48
+ effort: z.ZodMiniOptional<z.ZodMiniEnum<{
49
+ low: "low";
50
+ medium: "medium";
51
+ high: "high";
52
+ }>>;
53
+ permissions: z.ZodMiniOptional<z.ZodMiniEnum<{
54
+ "read-only": "read-only";
55
+ "workspace-write": "workspace-write";
56
+ auto: "auto";
57
+ }>>;
58
+ }, z.core.$strict>>;
59
+ claude: z.ZodMiniOptional<z.ZodMiniObject<{
60
+ model: z.ZodMiniOptional<z.ZodMiniString<string>>;
61
+ effort: z.ZodMiniOptional<z.ZodMiniEnum<{
62
+ low: "low";
63
+ medium: "medium";
64
+ high: "high";
65
+ }>>;
66
+ permissions: z.ZodMiniOptional<z.ZodMiniEnum<{
67
+ "read-only": "read-only";
68
+ "workspace-write": "workspace-write";
69
+ auto: "auto";
70
+ }>>;
71
+ }, z.core.$strict>>;
72
+ custom: z.ZodMiniOptional<z.ZodMiniObject<{
73
+ model: z.ZodMiniOptional<z.ZodMiniString<string>>;
74
+ effort: z.ZodMiniOptional<z.ZodMiniEnum<{
75
+ low: "low";
76
+ medium: "medium";
77
+ high: "high";
78
+ }>>;
79
+ permissions: z.ZodMiniOptional<z.ZodMiniEnum<{
80
+ "read-only": "read-only";
81
+ "workspace-write": "workspace-write";
82
+ auto: "auto";
83
+ }>>;
84
+ }, z.core.$strict>>;
85
+ }, z.core.$strict>;
86
+ models: z.ZodMiniOptional<z.ZodMiniRecord<z.ZodMiniString<string>, z.ZodMiniUnion<readonly [z.ZodMiniObject<{
30
87
  baseUrl: z.ZodMiniURL;
31
88
  model: z.ZodMiniString<string>;
32
89
  envKey: z.ZodMiniOptional<z.ZodMiniString<string>>;
33
- wireApi: z.ZodMiniOptional<
34
- z.ZodMiniEnum<{
35
- chat: 'chat';
36
- responses: 'responses';
37
- }>
38
- >;
90
+ wireApi: z.ZodMiniOptional<z.ZodMiniEnum<{
91
+ chat: "chat";
92
+ responses: "responses";
93
+ }>>;
39
94
  disabled: z.ZodMiniOptional<z.ZodMiniBoolean<boolean>>;
40
- },
41
- z.core.$strict
42
- >;
43
- /** The merged, effective shape (everything present after DEFAULT_CONFIG). */
44
- declare const effectiveConfigSchema: z.ZodMiniObject<
45
- {
46
- chain: z.ZodMiniArray<
47
- z.ZodMiniEnum<{
48
- codex: 'codex';
49
- claude: 'claude';
50
- custom: 'custom';
51
- }>
52
- >;
53
- agents: z.ZodMiniObject<
54
- {
55
- codex: z.ZodMiniOptional<
56
- z.ZodMiniObject<
57
- {
58
- model: z.ZodMiniOptional<z.ZodMiniString<string>>;
59
- effort: z.ZodMiniOptional<
60
- z.ZodMiniEnum<{
61
- low: 'low';
62
- medium: 'medium';
63
- high: 'high';
64
- }>
65
- >;
66
- permissions: z.ZodMiniOptional<
67
- z.ZodMiniEnum<{
68
- 'read-only': 'read-only';
69
- 'workspace-write': 'workspace-write';
70
- auto: 'auto';
71
- }>
72
- >;
73
- },
74
- z.core.$strict
75
- >
76
- >;
77
- claude: z.ZodMiniOptional<
78
- z.ZodMiniObject<
79
- {
80
- model: z.ZodMiniOptional<z.ZodMiniString<string>>;
81
- effort: z.ZodMiniOptional<
82
- z.ZodMiniEnum<{
83
- low: 'low';
84
- medium: 'medium';
85
- high: 'high';
86
- }>
87
- >;
88
- permissions: z.ZodMiniOptional<
89
- z.ZodMiniEnum<{
90
- 'read-only': 'read-only';
91
- 'workspace-write': 'workspace-write';
92
- auto: 'auto';
93
- }>
94
- >;
95
- },
96
- z.core.$strict
97
- >
98
- >;
99
- custom: z.ZodMiniOptional<
100
- z.ZodMiniObject<
101
- {
102
- model: z.ZodMiniOptional<z.ZodMiniString<string>>;
103
- effort: z.ZodMiniOptional<
104
- z.ZodMiniEnum<{
105
- low: 'low';
106
- medium: 'medium';
107
- high: 'high';
108
- }>
109
- >;
110
- permissions: z.ZodMiniOptional<
111
- z.ZodMiniEnum<{
112
- 'read-only': 'read-only';
113
- 'workspace-write': 'workspace-write';
114
- auto: 'auto';
115
- }>
116
- >;
117
- },
118
- z.core.$strict
119
- >
120
- >;
121
- },
122
- z.core.$strict
123
- >;
124
- models: z.ZodMiniOptional<
125
- z.ZodMiniRecord<
126
- z.ZodMiniString<string>,
127
- z.ZodMiniUnion<
128
- readonly [
129
- z.ZodMiniObject<
130
- {
131
- baseUrl: z.ZodMiniURL;
132
- model: z.ZodMiniString<string>;
133
- envKey: z.ZodMiniOptional<z.ZodMiniString<string>>;
134
- wireApi: z.ZodMiniOptional<
135
- z.ZodMiniEnum<{
136
- chat: 'chat';
137
- responses: 'responses';
138
- }>
139
- >;
140
- disabled: z.ZodMiniOptional<z.ZodMiniBoolean<boolean>>;
141
- },
142
- z.core.$strict
143
- >,
144
- z.ZodMiniObject<
145
- {
146
- provider: z.ZodMiniEnum<{
147
- codex: 'codex';
148
- claude: 'claude';
149
- }>;
150
- model: z.ZodMiniString<string>;
151
- effort: z.ZodMiniOptional<
152
- z.ZodMiniEnum<{
153
- low: 'low';
154
- medium: 'medium';
155
- high: 'high';
156
- }>
157
- >;
158
- disabled: z.ZodMiniOptional<z.ZodMiniBoolean<boolean>>;
159
- },
160
- z.core.$strict
161
- >,
162
- z.ZodMiniObject<
163
- {
164
- disabled: z.ZodMiniBoolean<boolean>;
165
- },
166
- z.core.$strict
167
- >,
168
- ]
169
- >
170
- >
171
- >;
172
- approvals: z.ZodMiniObject<
173
- {
174
- escalationTimeoutMs: z.ZodMiniNumber<number>;
175
- allowedNetworkHosts: z.ZodMiniArray<z.ZodMiniString<string>>;
176
- },
177
- z.core.$strict
178
- >;
179
- },
180
- z.core.$strict
181
- >;
95
+ }, z.core.$strict>, z.ZodMiniObject<{
96
+ provider: z.ZodMiniEnum<{
97
+ codex: "codex";
98
+ claude: "claude";
99
+ }>;
100
+ model: z.ZodMiniString<string>;
101
+ effort: z.ZodMiniOptional<z.ZodMiniEnum<{
102
+ low: "low";
103
+ medium: "medium";
104
+ high: "high";
105
+ }>>;
106
+ disabled: z.ZodMiniOptional<z.ZodMiniBoolean<boolean>>;
107
+ }, z.core.$strict>, z.ZodMiniObject<{
108
+ disabled: z.ZodMiniBoolean<boolean>;
109
+ }, z.core.$strict>]>>>;
110
+ approvals: z.ZodMiniObject<{
111
+ escalationTimeoutMs: z.ZodMiniNumber<number>;
112
+ allowedNetworkHosts: z.ZodMiniArray<z.ZodMiniString<string>>;
113
+ }, z.core.$strict>;
114
+ }, z.core.$strict>;
182
115
  type Engine = z.infer<typeof engineSchema>;
183
116
  type Agent = z.infer<typeof agentSchema>;
184
117
  type Effort = z.infer<typeof effortSchema>;
@@ -200,6 +133,7 @@ interface Job {
200
133
  agent?: Agent;
201
134
  engine?: Engine;
202
135
  prompt?: string;
136
+ currentPrompt?: string | null;
203
137
  system?: string | null;
204
138
  model?: string | null;
205
139
  effort?: Effort | null;
@@ -211,6 +145,7 @@ interface Job {
211
145
  threadId?: string | null;
212
146
  turnId?: string | null;
213
147
  completedAt?: string;
148
+ resumedAt?: string;
214
149
  error?: string;
215
150
  archived?: boolean;
216
151
  archivedAt?: string;
@@ -291,49 +226,42 @@ type FlowRunStatus = 'queued' | 'running' | 'completed' | 'failed' | 'stopped';
291
226
  * attached orchestrator appends these; `flow stream` / `flow.stream()` replay
292
227
  * and follow them.
293
228
  */
294
- type FlowEvent =
295
- | {
296
- kind: 'task-start';
297
- taskId: string;
298
- name?: string;
299
- prompt: string;
300
- agent?: string;
301
- depth?: number;
302
- }
303
- | {
304
- kind: 'task-end';
305
- taskId: string;
306
- status: string;
307
- tokens: TokenUsage | null;
308
- }
309
- | {
310
- kind: 'gate-start';
311
- gateId: string;
312
- cmd: string;
313
- depth?: number;
314
- }
315
- | {
316
- kind: 'gate';
317
- gateId?: string;
318
- cmd: string;
319
- ok: boolean;
320
- code: number;
321
- depth?: number;
322
- }
323
- | {
324
- kind: 'log';
325
- message: string;
326
- depth?: number;
327
- }
328
- | {
329
- kind: 'flow-start';
330
- name: string;
331
- depth: number;
332
- }
333
- | {
334
- kind: 'replay';
335
- count: number;
336
- };
229
+ type FlowEvent = {
230
+ kind: 'task-start';
231
+ taskId: string;
232
+ name?: string;
233
+ prompt: string;
234
+ agent?: string;
235
+ depth?: number;
236
+ } | {
237
+ kind: 'task-end';
238
+ taskId: string;
239
+ status: string;
240
+ tokens: TokenUsage | null;
241
+ } | {
242
+ kind: 'gate-start';
243
+ gateId: string;
244
+ cmd: string;
245
+ depth?: number;
246
+ } | {
247
+ kind: 'gate';
248
+ gateId?: string;
249
+ cmd: string;
250
+ ok: boolean;
251
+ code: number;
252
+ depth?: number;
253
+ } | {
254
+ kind: 'log';
255
+ message: string;
256
+ depth?: number;
257
+ } | {
258
+ kind: 'flow-start';
259
+ name: string;
260
+ depth: number;
261
+ } | {
262
+ kind: 'replay';
263
+ count: number;
264
+ };
337
265
  /** flow.json: the persisted run record. */
338
266
  interface FlowRecord {
339
267
  runId: string;
@@ -375,28 +303,20 @@ interface DiscoveredFlow {
375
303
  * reply is validated and `data` is guaranteed (validation failure throws).
376
304
  * Inside a flow run, results are journaled and replayed on resume.
377
305
  */
378
- declare function task<T>(
379
- prompt: string,
380
- opts: FlowTaskOptions<T> & {
381
- returns: FlowSchema<T>;
382
- },
383
- ): Promise<
384
- FlowTaskResult<T> & {
385
- data: T;
386
- }
387
- >;
306
+ declare function task<T>(prompt: string, opts: FlowTaskOptions<T> & {
307
+ returns: FlowSchema<T>;
308
+ }): Promise<FlowTaskResult<T> & {
309
+ data: T;
310
+ }>;
388
311
  /** Dispatch one coder task and await its result. Mirrors `coder run`. */
389
312
  declare function task(prompt: string, opts?: FlowTaskOptions): Promise<FlowTaskResult>;
390
313
  /**
391
314
  * Run a shell command as a checkpoint. Never throws — inspect `ok`/`code`;
392
315
  * output is captured (capped) and journaled for resume.
393
316
  */
394
- declare function gate(
395
- cmd: string,
396
- opts?: {
397
- cwd?: string;
398
- },
399
- ): Promise<GateResult>;
317
+ declare function gate(cmd: string, opts?: {
318
+ cwd?: string;
319
+ }): Promise<GateResult>;
400
320
  type Stage = (prev: any, item: any, index: number) => unknown;
401
321
  type S<P, T, R> = (prev: P, item: T, index: number) => R | Promise<R>;
402
322
  /**
@@ -405,24 +325,9 @@ type S<P, T, R> = (prev: P, item: T, index: number) => R | Promise<R>;
405
325
  * that item to `null` and skips its remaining stages.
406
326
  */
407
327
  declare function pipeline<T, A>(items: T[], s1: S<T, T, A>): Promise<(A | null)[]>;
408
- declare function pipeline<T, A, B>(
409
- items: T[],
410
- s1: S<T, T, A>,
411
- s2: S<A, T, B>,
412
- ): Promise<(B | null)[]>;
413
- declare function pipeline<T, A, B, C>(
414
- items: T[],
415
- s1: S<T, T, A>,
416
- s2: S<A, T, B>,
417
- s3: S<B, T, C>,
418
- ): Promise<(C | null)[]>;
419
- declare function pipeline<T, A, B, C, D>(
420
- items: T[],
421
- s1: S<T, T, A>,
422
- s2: S<A, T, B>,
423
- s3: S<B, T, C>,
424
- s4: S<C, T, D>,
425
- ): Promise<(D | null)[]>;
328
+ declare function pipeline<T, A, B>(items: T[], s1: S<T, T, A>, s2: S<A, T, B>): Promise<(B | null)[]>;
329
+ declare function pipeline<T, A, B, C>(items: T[], s1: S<T, T, A>, s2: S<A, T, B>, s3: S<B, T, C>): Promise<(C | null)[]>;
330
+ declare function pipeline<T, A, B, C, D>(items: T[], s1: S<T, T, A>, s2: S<A, T, B>, s3: S<B, T, C>, s4: S<C, T, D>): Promise<(D | null)[]>;
426
331
  declare function pipeline<T>(items: T[], ...stages: Stage[]): Promise<any[]>;
427
332
  /** Emit a progress line: appended to the run's flow.log and streamed to watchers. */
428
333
  declare function log(msg: string): void;
@@ -460,6 +365,13 @@ interface JobLogEntry {
460
365
  kind?: string;
461
366
  [key: string]: unknown;
462
367
  }
368
+ interface TurnResultEntry {
369
+ at?: string;
370
+ prompt?: string | null;
371
+ finalMessage?: string;
372
+ status?: number;
373
+ [key: string]: unknown;
374
+ }
463
375
  //#endregion
464
376
  //#region src/lib/dispatch.d.ts
465
377
  /** The run-native-subagent fallback payload the CLI prints on exit 3. */
@@ -479,15 +391,7 @@ interface FallbackPayload {
479
391
  * CoderError; `code` says why. The CLI maps codes to exit codes (chain-exhausted
480
392
  * -> 3, approval-pending -> 4, the rest -> 1).
481
393
  */
482
- type CoderErrorCode =
483
- | 'nested-dispatch'
484
- | 'invalid-option'
485
- | 'read-only-unavailable'
486
- | 'startup-failed'
487
- | 'chain-exhausted'
488
- | 'approval-pending'
489
- | 'task-failed'
490
- | 'flow-failed';
394
+ type CoderErrorCode = 'nested-dispatch' | 'invalid-option' | 'read-only-unavailable' | 'startup-failed' | 'chain-exhausted' | 'approval-pending' | 'task-failed' | 'flow-failed';
491
395
  declare class CoderError extends Error {
492
396
  code: CoderErrorCode;
493
397
  hint?: string | string[];
@@ -503,21 +407,17 @@ declare class CoderError extends Error {
503
407
  result?: unknown;
504
408
  /** flow-failed: the run to resume. */
505
409
  runId?: string;
506
- constructor(
507
- code: CoderErrorCode,
508
- message: string,
509
- extra?: {
510
- hint?: string | string[];
511
- taskId?: string;
512
- payload?: FallbackPayload;
513
- approval?: {
514
- id: string;
515
- summary: string;
516
- };
517
- result?: unknown;
518
- runId?: string;
519
- },
520
- );
410
+ constructor(code: CoderErrorCode, message: string, extra?: {
411
+ hint?: string | string[];
412
+ taskId?: string;
413
+ payload?: FallbackPayload;
414
+ approval?: {
415
+ id: string;
416
+ summary: string;
417
+ };
418
+ result?: unknown;
419
+ runId?: string;
420
+ });
521
421
  }
522
422
  interface TaskResult {
523
423
  taskId: string;
@@ -525,6 +425,8 @@ interface TaskResult {
525
425
  result: TurnResult | null;
526
426
  /** The last `tail` progress-log entries; [] at the default tail of 0. */
527
427
  steps: JobLogEntry[];
428
+ /** One entry per finished turn (a steered task accretes turns). */
429
+ turns: TurnResultEntry[];
528
430
  job: Job;
529
431
  }
530
432
  //#endregion
@@ -539,34 +441,26 @@ declare const flowSdk: {
539
441
  /** Run a flow and await its result. */
540
442
  run(nameOrPath: string, opts?: RunOptions): Promise<RunSummary>;
541
443
  /** Recent flow runs (mirrors `coder flow list`). */
542
- list(opts?: { archived?: boolean; limit?: number }): FlowRecord[];
444
+ list(opts?: {
445
+ archived?: boolean;
446
+ limit?: number;
447
+ }): FlowRecord[];
543
448
  /** Flows discoverable from a directory (workspace + global). */
544
449
  discover(cwd?: string): DiscoveredFlow[];
545
450
  /** A run's record plus its step rows (omit the id for the most recent run). `tail` caps steps (default 'all'; 0 → []). */
546
- result(
547
- runId?: string,
548
- opts?: {
549
- tail?: number | 'all';
550
- },
551
- ):
552
- | (FlowRecord & {
553
- steps: FlowStep[];
554
- })
555
- | null;
451
+ result(runId?: string, opts?: {
452
+ tail?: number | "all";
453
+ }): (FlowRecord & {
454
+ steps: FlowStep[];
455
+ }) | null;
556
456
  /** Follow a run live: an async iterable of flow events, ending when the run is terminal. `tail` replays only the last n (default 'all'). */
557
- stream(
558
- runId?: string,
559
- opts?: {
560
- tail?: number | 'all';
561
- },
562
- ): AsyncGenerator<FlowEvent>;
457
+ stream(runId?: string, opts?: {
458
+ tail?: number | "all";
459
+ }): AsyncGenerator<FlowEvent>;
563
460
  /** Stop a running flow (and, by default, its still-running tasks). */
564
- stop(
565
- runId?: string,
566
- opts?: {
567
- keepTasks?: boolean;
568
- },
569
- ): Promise<StopSummary>;
461
+ stop(runId?: string, opts?: {
462
+ keepTasks?: boolean;
463
+ }): Promise<StopSummary>;
570
464
  /** Continue a stopped or edited run from its journal and await the result. */
571
465
  resume(runId?: string, opts?: RunOptions): Promise<RunSummary>;
572
466
  /** Archive a run (hide it from the default list). A running run must be stopped first. */
@@ -581,31 +475,4 @@ declare const flowSdk: {
581
475
  };
582
476
  };
583
477
  //#endregion
584
- export {
585
- Job as C,
586
- CustomModelConfig as E,
587
- Availability as S,
588
- CoderConfig as T,
589
- FlowStep as _,
590
- JobLogEntry as a,
591
- GateResult as b,
592
- StopSummary as c,
593
- log as d,
594
- pipeline as f,
595
- FlowRecord as g,
596
- FlowEvent as h,
597
- TaskResult as i,
598
- flow as l,
599
- DiscoveredFlow as m,
600
- flowSdk as n,
601
- RunOptions as o,
602
- task as p,
603
- CoderError as r,
604
- RunSummary as s,
605
- args as t,
606
- gate as u,
607
- FlowTaskOptions as v,
608
- TurnResult as w,
609
- Approval as x,
610
- FlowTaskResult as y,
611
- };
478
+ export { Job as C, CustomModelConfig as E, Availability as S, CoderConfig as T, FlowStep as _, JobLogEntry as a, GateResult as b, StopSummary as c, log as d, pipeline as f, FlowRecord as g, FlowEvent as h, TaskResult as i, flow as l, DiscoveredFlow as m, flowSdk as n, RunOptions as o, task as p, CoderError as r, RunSummary as s, args as t, gate as u, FlowTaskOptions as v, TurnResult as w, Approval as x, FlowTaskResult as y };
@@ -1 +1 @@
1
- export {};
1
+ export {}
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env node
2
- import{a as e,l as t,r as n,t as r,u as i}from"../app-server-Dwd7IcQH.js";import a from"node:process";import o from"node:fs";import s from"node:path";import*as c from"zod/mini";import l from"node:net";const u=new Set([`turn/start`,`review/start`,`thread/compact/start`]);function d(e,t,n){let r=new Set;return t?.threadId&&r.add(t.threadId),e===`review/start`&&n?.reviewThreadId&&r.add(n.reviewThreadId),r}function f(e,t,n){return n===void 0?{code:e,message:t}:{code:e,message:t,data:n}}function p(e,t){e.destroyed||e.write(`${JSON.stringify(t)}\n`)}const m=new Set([`turn/interrupt`,`turn/steer`]);function h(e){return typeof e?.method==`string`&&m.has(e.method)}function g(e){e&&(o.mkdirSync(s.dirname(e),{recursive:!0}),o.writeFileSync(e,`${a.pid}\n`,`utf8`))}async function _(){let[m,..._]=a.argv.slice(2);if(m!==`serve`)throw Error(`Usage: node src/lib/broker.mjs serve --endpoint <value> [--cwd <path>] [--pid-file <path>]`);let{options:v}=t(_,c.object({cwd:i,"pid-file":i,endpoint:c.string()})),y=v.cwd?s.resolve(a.cwd(),v.cwd):a.cwd(),b=e(String(v.endpoint)),x=v[`pid-file`]?s.resolve(v[`pid-file`]):null;g(x);let S=await n.connect(y,{disableBroker:!0}),C=null,w=null,T=null,E=new Set,D=new Map;function O(e){for(let[t,n]of D)n===e&&(D.delete(t),S.sendMessage({id:t,error:f(-32e3,`Broker client disconnected before answering the server request.`)}))}function k(e){O(e),C===e&&(C=null),w===e&&(w=null,T=null)}function A(e){let t=C??w;if(t&&(p(t,e),e.method===`turn/completed`&&w===t)){let n=e.params?.threadId??null;(!n||!T||T.has(n))&&(w=null,T=null,C===t&&(C=null))}}async function j(e){for(let e of E)e.end();await S.close().catch(()=>{}),await new Promise(t=>e.close(t)),b.kind===`unix`&&o.existsSync(b.path)&&o.unlinkSync(b.path),x&&o.existsSync(x)&&o.unlinkSync(x)}S.setNotificationHandler(A),S.handleServerRequest=e=>{let t=w??C;if(!t||t.destroyed){S.sendMessage({id:e.id,error:f(-32601,`No broker client available to answer ${e.method}.`)});return}D.set(e.id,t),p(t,e)};let M=l.createServer(e=>{E.add(e),e.setEncoding(`utf8`);let t=``;e.on(`data`,async n=>{t+=n;let i=t.indexOf(`
2
+ import{a as e,l as t,r as n,t as r,u as i}from"../app-server-bHl6RBZ2.js";import a from"node:process";import o from"node:fs";import s from"node:path";import*as c from"zod/mini";import l from"node:net";const u=new Set([`turn/start`,`review/start`,`thread/compact/start`]);function d(e,t,n){let r=new Set;return t?.threadId&&r.add(t.threadId),e===`review/start`&&n?.reviewThreadId&&r.add(n.reviewThreadId),r}function f(e,t,n){return n===void 0?{code:e,message:t}:{code:e,message:t,data:n}}function p(e,t){e.destroyed||e.write(`${JSON.stringify(t)}\n`)}const m=new Set([`turn/interrupt`,`turn/steer`]);function h(e){return typeof e?.method==`string`&&m.has(e.method)}function g(e){e&&(o.mkdirSync(s.dirname(e),{recursive:!0}),o.writeFileSync(e,`${a.pid}\n`,`utf8`))}async function _(){let[m,..._]=a.argv.slice(2);if(m!==`serve`)throw Error(`Usage: node src/lib/broker.mjs serve --endpoint <value> [--cwd <path>] [--pid-file <path>]`);let{options:v}=t(_,c.object({cwd:i,"pid-file":i,endpoint:c.string()})),y=v.cwd?s.resolve(a.cwd(),v.cwd):a.cwd(),b=String(v.endpoint),x=e(b),S=v[`pid-file`]?s.resolve(v[`pid-file`]):null;g(S);let C=await n.connect(y,{disableBroker:!0}),w=null,T=null,E=null,D=new Set,O=new Map;function k(e){for(let[t,n]of O)n===e&&(O.delete(t),C.sendMessage({id:t,error:f(-32e3,`Broker client disconnected before answering the server request.`)}))}function A(e){k(e),w===e&&(w=null),T===e&&(T=null,E=null)}function j(e){let t=w??T;if(t&&(p(t,e),e.method===`turn/completed`&&T===t)){let n=e.params?.threadId??null;(!n||!E||E.has(n))&&(T=null,E=null,w===t&&(w=null))}}async function M(e){for(let e of D)e.end();await C.close().catch(()=>{}),await new Promise(t=>e.close(t)),x.kind===`unix`&&o.existsSync(x.path)&&o.unlinkSync(x.path),S&&o.existsSync(S)&&o.unlinkSync(S)}C.setNotificationHandler(j),C.handleServerRequest=e=>{let t=T??w;if(!t||t.destroyed){C.sendMessage({id:e.id,error:f(-32601,`No broker client available to answer ${e.method}.`)});return}O.set(e.id,t),p(t,e)};let N=l.createServer(e=>{D.add(e),e.setEncoding(`utf8`);let t=``;e.on(`data`,async n=>{t+=n;let i=t.indexOf(`
3
3
  `);for(;i!==-1;){let n=t.slice(0,i);if(t=t.slice(i+1),i=t.indexOf(`
4
- `),!n.trim())continue;let o;try{o=JSON.parse(n)}catch(t){p(e,{id:null,error:f(-32700,`Invalid JSON: ${t instanceof Error?t.message:String(t)}`)});continue}if(o.id!==void 0&&!o.method&&D.has(o.id)){D.delete(o.id),S.sendMessage(o);continue}if(o.id!==void 0&&o.method===`initialize`){p(e,{id:o.id,result:{userAgent:`coder-broker`}});continue}if(o.method===`initialized`&&o.id===void 0||(o.id!==void 0&&o.method===`broker/shutdown`&&(p(e,{id:o.id,result:{}}),await j(M),a.exit(0)),o.id===void 0||!o.method))continue;let s=h(o)&&w&&w!==e&&!C;if((C&&C!==e||w&&w!==e)&&!s){p(e,{id:o.id,error:f(r,`Shared Codex broker is busy.`)});continue}if(s){try{let t=await S.request(o.method,o.params??{});p(e,{id:o.id,result:t})}catch(t){let n=t;p(e,{id:o.id,error:f(n.rpcCode??-32e3,n.message)})}continue}let c=u.has(o.method);C=e,c&&(w=e,T=d(o.method,o.params??{},null));try{let t=await S.request(o.method,o.params??{});p(e,{id:o.id,result:t}),c&&(T=d(o.method,o.params??{},t)),C===e&&(C=null)}catch(t){let n=t;p(e,{id:o.id,error:f(n.rpcCode??-32e3,n.message)}),C===e&&(C=null),w===e&&c&&(w=null,T=null)}}}),e.on(`close`,()=>{E.delete(e),k(e)}),e.on(`error`,()=>{E.delete(e),k(e)})});a.on(`SIGTERM`,async()=>{await j(M),a.exit(0)}),a.on(`SIGINT`,async()=>{await j(M),a.exit(0)}),M.listen(b.path)}_().catch(e=>{a.stderr.write(`${e instanceof Error?e.message:String(e)}\n`),a.exit(1)});export{};
4
+ `),!n.trim())continue;let o;try{o=JSON.parse(n)}catch(t){p(e,{id:null,error:f(-32700,`Invalid JSON: ${t instanceof Error?t.message:String(t)}`)});continue}if(o.id!==void 0&&!o.method&&O.has(o.id)){O.delete(o.id),C.sendMessage(o);continue}if(o.id!==void 0&&o.method===`initialize`){p(e,{id:o.id,result:{userAgent:`coder-broker`}});continue}if(o.method===`initialized`&&o.id===void 0||(o.id!==void 0&&o.method===`broker/shutdown`&&(p(e,{id:o.id,result:{}}),await M(N),a.exit(0)),o.id===void 0||!o.method))continue;let s=h(o)&&T&&T!==e&&!w;if((w&&w!==e||T&&T!==e)&&!s){p(e,{id:o.id,error:f(r,`Shared Codex broker is busy.`)});continue}if(s){try{let t=await C.request(o.method,o.params??{});p(e,{id:o.id,result:t})}catch(t){let n=t;p(e,{id:o.id,error:f(n.rpcCode??-32e3,n.message)})}continue}let c=u.has(o.method);w=e,c&&(T=e,E=d(o.method,o.params??{},null));try{let t=await C.request(o.method,o.params??{});p(e,{id:o.id,result:t}),c&&(E=d(o.method,o.params??{},t)),w===e&&(w=null)}catch(t){let n=t;p(e,{id:o.id,error:f(n.rpcCode??-32e3,n.message)}),w===e&&(w=null),T===e&&c&&(T=null,E=null)}}}),e.on(`close`,()=>{D.delete(e),A(e)}),e.on(`error`,()=>{D.delete(e),A(e)})});a.on(`SIGTERM`,async()=>{await M(N),a.exit(0)}),a.on(`SIGINT`,async()=>{await M(N),a.exit(0)}),N.listen(x.path)}_().catch(e=>{a.stderr.write(`${e instanceof Error?e.message:String(e)}\n`),a.exit(1)});export{};