@cogitator-ai/bridge 0.2.0 → 0.3.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 (74) hide show
  1. package/README.md +156 -8
  2. package/dist/adapter.d.ts +123 -0
  3. package/dist/adapter.d.ts.map +1 -0
  4. package/dist/adapter.js +2 -0
  5. package/dist/adapter.js.map +1 -0
  6. package/dist/adapters/claude.d.ts +90 -0
  7. package/dist/adapters/claude.d.ts.map +1 -0
  8. package/dist/adapters/claude.js +359 -0
  9. package/dist/adapters/claude.js.map +1 -0
  10. package/dist/adapters/codex.d.ts +58 -0
  11. package/dist/adapters/codex.d.ts.map +1 -0
  12. package/dist/adapters/codex.js +330 -0
  13. package/dist/adapters/codex.js.map +1 -0
  14. package/dist/adapters/exec.d.ts +57 -0
  15. package/dist/adapters/exec.d.ts.map +1 -0
  16. package/dist/adapters/exec.js +149 -0
  17. package/dist/adapters/exec.js.map +1 -0
  18. package/dist/adapters/hosted.d.ts +168 -0
  19. package/dist/adapters/hosted.d.ts.map +1 -0
  20. package/dist/adapters/hosted.js +361 -0
  21. package/dist/adapters/hosted.js.map +1 -0
  22. package/dist/client.d.ts +9 -2
  23. package/dist/client.d.ts.map +1 -1
  24. package/dist/client.js +17 -3
  25. package/dist/client.js.map +1 -1
  26. package/dist/dispatcher.d.ts +67 -17
  27. package/dist/dispatcher.d.ts.map +1 -1
  28. package/dist/dispatcher.js +240 -17
  29. package/dist/dispatcher.js.map +1 -1
  30. package/dist/host.d.ts +1 -1
  31. package/dist/host.d.ts.map +1 -1
  32. package/dist/index.d.ts +13 -2
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.js +6 -0
  35. package/dist/index.js.map +1 -1
  36. package/dist/inputs.d.ts +40 -1
  37. package/dist/inputs.d.ts.map +1 -1
  38. package/dist/inputs.js +25 -2
  39. package/dist/inputs.js.map +1 -1
  40. package/dist/paths.d.ts +6 -0
  41. package/dist/paths.d.ts.map +1 -1
  42. package/dist/paths.js +19 -0
  43. package/dist/paths.js.map +1 -1
  44. package/dist/protocol.d.ts +5 -0
  45. package/dist/protocol.d.ts.map +1 -1
  46. package/dist/protocol.js +5 -0
  47. package/dist/protocol.js.map +1 -1
  48. package/dist/server.d.ts.map +1 -1
  49. package/dist/server.js +6 -1
  50. package/dist/server.js.map +1 -1
  51. package/dist/testing/conformance.d.ts +65 -0
  52. package/dist/testing/conformance.d.ts.map +1 -0
  53. package/dist/testing/conformance.js +217 -0
  54. package/dist/testing/conformance.js.map +1 -0
  55. package/dist/testing/fake-harnesses.d.ts +101 -0
  56. package/dist/testing/fake-harnesses.d.ts.map +1 -0
  57. package/dist/testing/fake-harnesses.js +291 -0
  58. package/dist/testing/fake-harnesses.js.map +1 -0
  59. package/dist/testing/index.d.ts +5 -0
  60. package/dist/testing/index.d.ts.map +1 -0
  61. package/dist/testing/index.js +3 -0
  62. package/dist/testing/index.js.map +1 -0
  63. package/dist/types.d.ts +3 -39
  64. package/dist/types.d.ts.map +1 -1
  65. package/dist/types.js +2 -0
  66. package/dist/types.js.map +1 -1
  67. package/dist/waiter.d.ts +36 -0
  68. package/dist/waiter.d.ts.map +1 -0
  69. package/dist/waiter.js +92 -0
  70. package/dist/waiter.js.map +1 -0
  71. package/fakes/claude.mjs +450 -0
  72. package/fakes/codex.mjs +391 -0
  73. package/fakes/lib.mjs +507 -0
  74. package/package.json +7 -1
@@ -0,0 +1,168 @@
1
+ import type { StreamEvent } from '@cogitator-ai/server-shared';
2
+ import type { BridgeAdapter } from '../adapter.js';
3
+ import type { BridgeJob } from '../types.js';
4
+ /** How letters reach a session the host runs itself: handed to its agent in the same process. */
5
+ export declare const HOSTED_DELIVERY = "hosted";
6
+ /** One turn of an agent the host runs. */
7
+ export interface HostedTurn {
8
+ /** The agent's answer, once the turn is over. Rejects when the turn failed */
9
+ readonly result: Promise<string>;
10
+ /** Reaches the agent in the middle of the turn. False when the turn takes no more */
11
+ steer?(text: string): boolean;
12
+ /** Leaves a message the agent takes when it would stop. False when the turn takes no more */
13
+ followUp?(text: string): boolean;
14
+ /** Stops the turn */
15
+ abort?(reason?: unknown): void;
16
+ }
17
+ /** What a turn is started for. */
18
+ export interface HostedTurnContext {
19
+ /** The job the turn carries out, when the job's first letter starts it */
20
+ job?: BridgeJob;
21
+ }
22
+ /** A session of an agent the host runs. One turn goes on at a time. */
23
+ export interface HostedSession {
24
+ turn(text: string, context?: HostedTurnContext): HostedTurn;
25
+ close?(): Promise<void>;
26
+ }
27
+ /** What a hosted session is opened for. */
28
+ export interface HostedSessionContext {
29
+ /** The id the adapter gave the session */
30
+ sessionId: string;
31
+ /** The name the bridge knows the session by */
32
+ agentName: string;
33
+ cwd: string;
34
+ project: string;
35
+ /** The job it is started for */
36
+ job: BridgeJob;
37
+ }
38
+ export type OpenHostedSession = (context: HostedSessionContext) => HostedSession | Promise<HostedSession>;
39
+ export interface HostedAdapterOptions {
40
+ /** The word agents address these sessions by, such as `qwen` or `gemini` */
41
+ harness: string;
42
+ /** Opens a session for a job */
43
+ open: OpenHostedSession;
44
+ /** Whether a turn of these sessions takes messages while it goes on (`HostedTurn.steer`) */
45
+ steer?: boolean;
46
+ /** Hears what went wrong outside a call of the bridge: a report that could not be sent */
47
+ onError?: (error: Error) => void;
48
+ /** How long a turn that was told to stop is waited for when the host closes, 5 s by default */
49
+ stopGraceMs?: number;
50
+ }
51
+ /**
52
+ * Runs the sessions of a harness inside the host: agents of the Cogitator runtime on any model
53
+ * (`cogitatorSessions`), agents behind the Agent Client Protocol (`providerSessions`), or
54
+ * anything else that takes a text and answers. A job's first letter starts a turn, and the
55
+ * agent's answer to it is the job's report: the agent needs no tool to report, and one that
56
+ * failed reports that. A letter that arrives while a turn goes on reaches the agent in the
57
+ * middle of it when the turn takes that, and otherwise starts the next turn. A letter about a
58
+ * job that still waits for its turn waits with it, and does not reach the turn of another job.
59
+ * A job that is canceled or fails stops the turn that carries it out, and is not started when it
60
+ * still waited for its turn. The session is in the address book before its first turn starts, so
61
+ * an agent that answers at once is heard.
62
+ *
63
+ * The sessions live in the host's process and end with it.
64
+ */
65
+ export declare function hostedAdapter(options: HostedAdapterOptions): BridgeAdapter;
66
+ /** A run of the Cogitator runtime, as much of it as the bridge uses. `RunHandle` fits. */
67
+ export interface HostedRun {
68
+ readonly result: Promise<{
69
+ output: string;
70
+ status?: 'completed' | 'paused';
71
+ }>;
72
+ steer(message: string): boolean;
73
+ followUp(message: string): boolean;
74
+ abort(reason?: unknown): unknown;
75
+ }
76
+ /** The part of the Cogitator runtime the bridge uses. `Cogitator` fits. */
77
+ export interface HostedRuntime<TAgent> {
78
+ start(agent: TAgent, options: {
79
+ input: string;
80
+ threadId: string;
81
+ }): HostedRun;
82
+ }
83
+ export interface CogitatorSessionsOptions<TAgent> {
84
+ runtime: HostedRuntime<TAgent>;
85
+ /** The agent of every session, or how to make the one of a session */
86
+ agent: TAgent | ((context: HostedSessionContext) => TAgent);
87
+ }
88
+ /**
89
+ * Sessions of an agent of the Cogitator runtime, on whatever model the agent names: every turn
90
+ * is a run on the session's own thread, so with memory configured the agent remembers the
91
+ * turns before. A letter that arrives during a run steers it or follows it up. A run that
92
+ * pauses for an approval or for a question of a tool fails its job: its answer is not a report.
93
+ *
94
+ * @example
95
+ * ```ts
96
+ * const qwen = hostedAdapter({
97
+ * harness: 'qwen',
98
+ * steer: true,
99
+ * open: cogitatorSessions({ runtime: cogitator, agent: reviewer }),
100
+ * });
101
+ * ```
102
+ */
103
+ export declare function cogitatorSessions<TAgent>(options: CogitatorSessionsOptions<TAgent>): OpenHostedSession;
104
+ /** A turn of a session provider, as the bridge starts it. `SessionProviderTurn` of the app-server fits. */
105
+ export interface ProviderTurn {
106
+ readonly threadId: string;
107
+ readonly turnId: string;
108
+ readonly input: readonly {
109
+ type: 'text';
110
+ text: string;
111
+ }[];
112
+ readonly metadata: Readonly<Record<string, unknown>>;
113
+ saveMetadata(patch: Record<string, unknown>): Promise<void>;
114
+ permissionMode(): ProviderPermissionMode;
115
+ setPermissionMode(mode: ProviderPermissionMode): Promise<void>;
116
+ emit(event: StreamEvent): void;
117
+ readonly signal: AbortSignal;
118
+ }
119
+ export type ProviderPermissionMode = 'default' | 'acceptEdits' | 'plan' | 'auto' | 'dontAsk' | 'bypassPermissions';
120
+ /** An agent that runs outside the runtime, turn by turn. `AcpAgentProvider` fits. */
121
+ export interface TurnProvider {
122
+ runTurn(turn: ProviderTurn): Promise<{
123
+ output: string;
124
+ refused?: boolean;
125
+ }>;
126
+ close?(): Promise<void>;
127
+ }
128
+ export interface ProviderSessionsOptions {
129
+ /**
130
+ * The provider of every session, or how to make the one of a session. A provider works in the
131
+ * folder of its own workspace, whatever folder a job names: make one per session to follow the
132
+ * job (the adapter closes those), or give one and say where it works with `root`
133
+ */
134
+ provider: TurnProvider | ((context: HostedSessionContext) => TurnProvider | Promise<TurnProvider>);
135
+ /**
136
+ * The folder the one provider works in. A job for another folder is refused, rather than done
137
+ * in the wrong place. Required with one provider for every session
138
+ */
139
+ root?: string;
140
+ /**
141
+ * The permission mode of every turn, `default` when absent: what the provider's rules do not
142
+ * settle is refused, since no person sits at a session the bridge started
143
+ */
144
+ permissionMode?: ProviderPermissionMode;
145
+ }
146
+ /**
147
+ * Sessions of an agent behind a session provider, such as an agent of the Agent Client Protocol
148
+ * (Gemini CLI and the others of its registry) launched by `AcpAgentProvider`: every hosted
149
+ * session is a session of that agent, every turn a prompt. A turn of such an agent takes nothing
150
+ * while it goes on, so letters wait for the next one. A provider works in the folder of its
151
+ * workspace: one provider serves the jobs of that folder (`root`), a function makes a provider
152
+ * for the folder of each session. A later job of the session for a folder outside the one its
153
+ * provider works in fails with that reason, rather than being done in the wrong place. Whoever
154
+ * made a provider closes it, the adapter those it made.
155
+ *
156
+ * @example
157
+ * ```ts
158
+ * const gemini = hostedAdapter({
159
+ * harness: 'gemini',
160
+ * open: providerSessions({
161
+ * provider: new AcpAgentProvider({ agent, workspace, name: 'gemini' }),
162
+ * root: workspace.root,
163
+ * }),
164
+ * });
165
+ * ```
166
+ */
167
+ export declare function providerSessions(options: ProviderSessionsOptions): OpenHostedSession;
168
+ //# sourceMappingURL=hosted.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hosted.d.ts","sourceRoot":"","sources":["../../src/adapters/hosted.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC/D,OAAO,KAAK,EACV,aAAa,EAMd,MAAM,eAAe,CAAC;AAIvB,OAAO,KAAK,EAAE,SAAS,EAAwC,MAAM,aAAa,CAAC;AAEnF,iGAAiG;AACjG,eAAO,MAAM,eAAe,WAAW,CAAC;AAExC,0CAA0C;AAC1C,MAAM,WAAW,UAAU;IACzB,8EAA8E;IAC9E,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;IACjC,qFAAqF;IACrF,KAAK,CAAC,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;IAC9B,6FAA6F;IAC7F,QAAQ,CAAC,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;IACjC,qBAAqB;IACrB,KAAK,CAAC,CAAC,MAAM,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;CAChC;AAED,kCAAkC;AAClC,MAAM,WAAW,iBAAiB;IAChC,0EAA0E;IAC1E,GAAG,CAAC,EAAE,SAAS,CAAC;CACjB;AAED,uEAAuE;AACvE,MAAM,WAAW,aAAa;IAC5B,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,iBAAiB,GAAG,UAAU,CAAC;IAC5D,KAAK,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACzB;AAED,2CAA2C;AAC3C,MAAM,WAAW,oBAAoB;IACnC,0CAA0C;IAC1C,SAAS,EAAE,MAAM,CAAC;IAClB,+CAA+C;IAC/C,SAAS,EAAE,MAAM,CAAC;IAClB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MA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AAM,WAAW,uBAAuB;IACtC;;;;OAIG;IACH,QAAQ,EACN,YAAY,GAAG,CAAC,CAAC,OAAO,EAAE,oBAAoB,KAAK,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC;IAC3F;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;;OAGG;IACH,cAAc,CAAC,EAAE,sBAAsB,CAAC;CACzC;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,uBAAuB,GAAG,iBAAiB,CAwDpF"}
@@ -0,0 +1,361 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import { isBridgeError } from '../errors.js';
3
+ import { pause } from './exec.js';
4
+ import { isInside } from '../paths.js';
5
+ /** How letters reach a session the host runs itself: handed to its agent in the same process. */
6
+ export const HOSTED_DELIVERY = 'hosted';
7
+ const DEFAULT_STOP_GRACE_MS = 5_000;
8
+ const FINISHED = ['completed', 'failed', 'canceled'];
9
+ /** Letters an agent wants in the middle of its work, when its turn can take them. */
10
+ const STEERED = ['answer', 'permission', 'note'];
11
+ function asError(error) {
12
+ return error instanceof Error ? error : new Error(String(error));
13
+ }
14
+ /**
15
+ * Runs the sessions of a harness inside the host: agents of the Cogitator runtime on any model
16
+ * (`cogitatorSessions`), agents behind the Agent Client Protocol (`providerSessions`), or
17
+ * anything else that takes a text and answers. A job's first letter starts a turn, and the
18
+ * agent's answer to it is the job's report: the agent needs no tool to report, and one that
19
+ * failed reports that. A letter that arrives while a turn goes on reaches the agent in the
20
+ * middle of it when the turn takes that, and otherwise starts the next turn. A letter about a
21
+ * job that still waits for its turn waits with it, and does not reach the turn of another job.
22
+ * A job that is canceled or fails stops the turn that carries it out, and is not started when it
23
+ * still waited for its turn. The session is in the address book before its first turn starts, so
24
+ * an agent that answers at once is heard.
25
+ *
26
+ * The sessions live in the host's process and end with it.
27
+ */
28
+ export function hostedAdapter(options) {
29
+ const sessions = new Map();
30
+ const onError = options.onError ?? (() => undefined);
31
+ let bridge;
32
+ let closed = false;
33
+ function report(hosted, job, body, failed) {
34
+ if (!bridge)
35
+ return;
36
+ bridge
37
+ .send(hosted.agentName, {
38
+ job,
39
+ kind: 'report',
40
+ body,
41
+ ...(failed && { outcome: 'failed' }),
42
+ })
43
+ .catch((error) => {
44
+ if (isBridgeError(error) && error.code === 'job_finished')
45
+ return;
46
+ onError(asError(error));
47
+ });
48
+ }
49
+ function start(hosted, mail) {
50
+ let turn;
51
+ const { letter } = mail;
52
+ const job = letter.kind === 'task' ? letter.job : undefined;
53
+ try {
54
+ turn = hosted.session.turn(letter.text, job === undefined ? {} : { job: mail.job });
55
+ }
56
+ catch (error) {
57
+ turn = { result: Promise.reject(asError(error)) };
58
+ }
59
+ const running = { turn, ...(job !== undefined && { job }) };
60
+ hosted.running = running;
61
+ if (job !== undefined) {
62
+ hosted.waiting = hosted.waiting.filter((held) => held.letter.job !== job || !give(turn, held.letter));
63
+ }
64
+ turn.result
65
+ .then((answer) => {
66
+ if (job !== undefined)
67
+ report(hosted, job, answer.trim() || 'Done', false);
68
+ }, (error) => {
69
+ if (job !== undefined && !closed) {
70
+ report(hosted, job, `The agent failed: ${asError(error).message}`, true);
71
+ }
72
+ })
73
+ .finally(() => {
74
+ if (hosted.running !== running)
75
+ return;
76
+ delete hosted.running;
77
+ void startNext(hosted);
78
+ });
79
+ }
80
+ /** Whether the job a letter is about is over: reported on, failed or canceled. */
81
+ async function over(job) {
82
+ if (!bridge)
83
+ return false;
84
+ try {
85
+ return FINISHED.includes((await bridge.job(job)).state);
86
+ }
87
+ catch {
88
+ return false;
89
+ }
90
+ }
91
+ /**
92
+ * Starts the turn of the oldest letter that waits. A job that was canceled or failed while its
93
+ * first letter waited is not started, and what is written about it is dropped with it: the
94
+ * agent never saw the job.
95
+ */
96
+ async function startNext(hosted) {
97
+ if (hosted.choosing)
98
+ return;
99
+ hosted.choosing = true;
100
+ try {
101
+ while (!closed && !hosted.running) {
102
+ const next = hosted.waiting.shift();
103
+ if (!next)
104
+ return;
105
+ if (next.letter.kind === 'task' && (await over(next.letter.job))) {
106
+ forget(hosted, next.letter.job);
107
+ continue;
108
+ }
109
+ if (closed)
110
+ return;
111
+ start(hosted, next);
112
+ }
113
+ }
114
+ finally {
115
+ hosted.choosing = false;
116
+ }
117
+ }
118
+ /** Drops what waits about a job the agent never started, and what comes about it later. */
119
+ function forget(hosted, job) {
120
+ hosted.unseen.add(job);
121
+ hosted.waiting = hosted.waiting.filter((held) => held.letter.job !== job);
122
+ }
123
+ /** Stops the turn that carries out a job once the job is over: nobody waits for its answer. */
124
+ async function stopIfOver(hosted, job) {
125
+ const running = hosted.running;
126
+ if (running?.job !== job || !(await over(job)))
127
+ return;
128
+ if (hosted.running === running)
129
+ running.turn.abort?.(new Error(`Job #${job} is over`));
130
+ }
131
+ /** Hands a letter to a turn that goes on. False when the turn does not take it. */
132
+ function give(turn, letter) {
133
+ if (letter.kind === 'task')
134
+ return false;
135
+ if (STEERED.includes(letter.kind) && turn.steer?.(letter.text))
136
+ return true;
137
+ return turn.followUp?.(letter.text) ?? false;
138
+ }
139
+ /** Whether a job's first letter still waits for its turn in the session. */
140
+ function awaitsTurn(hosted, job) {
141
+ return hosted.waiting.some((held) => held.letter.kind === 'task' && held.letter.job === job);
142
+ }
143
+ function hand(hosted, mail) {
144
+ if (hosted.unseen.has(mail.letter.job))
145
+ return;
146
+ const turn = hosted.running?.turn;
147
+ if (!turn) {
148
+ if (hosted.choosing)
149
+ hosted.waiting.push(mail);
150
+ else
151
+ start(hosted, mail);
152
+ return;
153
+ }
154
+ if (!awaitsTurn(hosted, mail.letter.job) && give(turn, mail.letter))
155
+ return;
156
+ hosted.waiting.push(mail);
157
+ }
158
+ return {
159
+ harness: options.harness,
160
+ delivery: HOSTED_DELIVERY,
161
+ ownsSessions: true,
162
+ capabilities: {
163
+ createSession: true,
164
+ push: true,
165
+ steer: options.steer ?? false,
166
+ nativeWindow: false,
167
+ },
168
+ attach(calls) {
169
+ bridge = calls;
170
+ },
171
+ async createSession(request) {
172
+ if (closed)
173
+ throw new Error('The host is closing');
174
+ const sessionId = randomUUID();
175
+ const session = await options.open({
176
+ sessionId,
177
+ agentName: request.agentName,
178
+ cwd: request.cwd,
179
+ project: request.project,
180
+ job: request.job,
181
+ });
182
+ if (closed) {
183
+ await session.close?.();
184
+ throw new Error('The host is closing');
185
+ }
186
+ const hosted = {
187
+ agentName: request.agentName,
188
+ cwd: request.cwd,
189
+ session,
190
+ waiting: [],
191
+ unseen: new Set(),
192
+ };
193
+ sessions.set(sessionId, hosted);
194
+ await bridge?.register({
195
+ harness: options.harness,
196
+ sessionId,
197
+ cwd: request.cwd,
198
+ project: request.project,
199
+ agentName: request.agentName,
200
+ });
201
+ if (await over(request.job.number))
202
+ forget(hosted, request.job.number);
203
+ else if (!closed)
204
+ hand(hosted, { letter: request.firstLetter, job: request.job });
205
+ return { sessionId, deliveredFirstLetter: true };
206
+ },
207
+ async deliver({ agent, letter, job }) {
208
+ const hosted = sessions.get(agent.sessionId);
209
+ if (!hosted)
210
+ throw new Error(`The session of ${agent.name} does not run in this host`);
211
+ hosted.agentName = agent.name;
212
+ hand(hosted, { letter, job });
213
+ void stopIfOver(hosted, letter.job);
214
+ },
215
+ async liveSessions() {
216
+ return [...sessions].map(([sessionId, hosted]) => ({
217
+ sessionId,
218
+ cwd: hosted.cwd,
219
+ busy: hosted.running !== undefined,
220
+ }));
221
+ },
222
+ async close() {
223
+ closed = true;
224
+ const open = [...sessions.values()];
225
+ sessions.clear();
226
+ await Promise.all(open.map(async (hosted) => {
227
+ hosted.waiting.length = 0;
228
+ const turn = hosted.running?.turn;
229
+ if (turn) {
230
+ turn.abort?.(new Error('The host is closing'));
231
+ await Promise.race([
232
+ turn.result.catch(() => undefined),
233
+ pause(options.stopGraceMs ?? DEFAULT_STOP_GRACE_MS),
234
+ ]);
235
+ }
236
+ await hosted.session.close?.();
237
+ }));
238
+ },
239
+ };
240
+ }
241
+ /**
242
+ * Why a run that paused fails its job: nothing is finished, and nobody is there to approve a
243
+ * tool call or answer a tool's question in a session the bridge hosts.
244
+ */
245
+ const PAUSED = 'the run stopped to wait for an approval or an answer, which nobody gives in a hosted session. ' +
246
+ 'Give the agent the permission rules it needs before it starts';
247
+ /**
248
+ * Sessions of an agent of the Cogitator runtime, on whatever model the agent names: every turn
249
+ * is a run on the session's own thread, so with memory configured the agent remembers the
250
+ * turns before. A letter that arrives during a run steers it or follows it up. A run that
251
+ * pauses for an approval or for a question of a tool fails its job: its answer is not a report.
252
+ *
253
+ * @example
254
+ * ```ts
255
+ * const qwen = hostedAdapter({
256
+ * harness: 'qwen',
257
+ * steer: true,
258
+ * open: cogitatorSessions({ runtime: cogitator, agent: reviewer }),
259
+ * });
260
+ * ```
261
+ */
262
+ export function cogitatorSessions(options) {
263
+ const agentOf = (context) => typeof options.agent === 'function'
264
+ ? options.agent(context)
265
+ : options.agent;
266
+ return (context) => {
267
+ const agent = agentOf(context);
268
+ const threadId = `bridge-${context.sessionId}`;
269
+ return {
270
+ turn(text) {
271
+ const run = options.runtime.start(agent, { input: text, threadId });
272
+ return {
273
+ result: run.result.then((result) => {
274
+ if (result.status === 'paused')
275
+ throw new Error(PAUSED);
276
+ return result.output;
277
+ }),
278
+ steer: (message) => run.steer(message),
279
+ followUp: (message) => run.followUp(message),
280
+ abort: (reason) => {
281
+ run.abort(reason);
282
+ },
283
+ };
284
+ },
285
+ };
286
+ };
287
+ }
288
+ /**
289
+ * Sessions of an agent behind a session provider, such as an agent of the Agent Client Protocol
290
+ * (Gemini CLI and the others of its registry) launched by `AcpAgentProvider`: every hosted
291
+ * session is a session of that agent, every turn a prompt. A turn of such an agent takes nothing
292
+ * while it goes on, so letters wait for the next one. A provider works in the folder of its
293
+ * workspace: one provider serves the jobs of that folder (`root`), a function makes a provider
294
+ * for the folder of each session. A later job of the session for a folder outside the one its
295
+ * provider works in fails with that reason, rather than being done in the wrong place. Whoever
296
+ * made a provider closes it, the adapter those it made.
297
+ *
298
+ * @example
299
+ * ```ts
300
+ * const gemini = hostedAdapter({
301
+ * harness: 'gemini',
302
+ * open: providerSessions({
303
+ * provider: new AcpAgentProvider({ agent, workspace, name: 'gemini' }),
304
+ * root: workspace.root,
305
+ * }),
306
+ * });
307
+ * ```
308
+ */
309
+ export function providerSessions(options) {
310
+ const shared = typeof options.provider === 'function' ? undefined : options.provider;
311
+ if (shared && options.root === undefined) {
312
+ throw new Error('providerSessions needs `root`, the folder the provider works in, or a function that makes a provider for each session');
313
+ }
314
+ return async (context) => {
315
+ const folder = shared && options.root !== undefined ? options.root : context.cwd;
316
+ const elsewhere = (cwd) => isInside(folder, cwd)
317
+ ? undefined
318
+ : new Error(`this agent works in ${folder}, and the job is for ${cwd}. Hand it a job in its own folder`);
319
+ const refusal = elsewhere(context.cwd);
320
+ if (refusal)
321
+ throw refusal;
322
+ const make = options.provider;
323
+ const provider = shared ?? (await make(context));
324
+ let metadata = {};
325
+ let mode = options.permissionMode ?? 'default';
326
+ return {
327
+ ...(!shared && provider.close && { close: () => provider.close?.() ?? Promise.resolve() }),
328
+ turn(text, about) {
329
+ const misplaced = about?.job && elsewhere(about.job.cwd);
330
+ if (misplaced)
331
+ return { result: Promise.reject(misplaced) };
332
+ const stop = new AbortController();
333
+ const result = provider
334
+ .runTurn({
335
+ threadId: `bridge-${context.sessionId}`,
336
+ turnId: randomUUID(),
337
+ input: [{ type: 'text', text }],
338
+ get metadata() {
339
+ return metadata;
340
+ },
341
+ saveMetadata: async (patch) => {
342
+ metadata = { ...metadata, ...patch };
343
+ },
344
+ permissionMode: () => mode,
345
+ setPermissionMode: async (next) => {
346
+ mode = next;
347
+ },
348
+ emit: () => undefined,
349
+ signal: stop.signal,
350
+ })
351
+ .then((answer) => {
352
+ if (answer.refused)
353
+ throw new Error(answer.output.trim() || 'the agent refused the job');
354
+ return answer.output;
355
+ });
356
+ return { result, abort: (reason) => stop.abort(reason) };
357
+ },
358
+ };
359
+ };
360
+ }
361
+ //# sourceMapp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,CAAC;oBAClB,CAAC;oBACD,YAAY,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE;wBAC5B,QAAQ,GAAG,EAAE,GAAG,QAAQ,EAAE,GAAG,KAAK,EAAE,CAAC;oBACvC,CAAC;oBACD,cAAc,EAAE,GAAG,EAAE,CAAC,IAAI;oBAC1B,iBAAiB,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE;wBAChC,IAAI,GAAG,IAAI,CAAC;oBACd,CAAC;oBACD,IAAI,EAAE,GAAG,EAAE,CAAC,SAAS;oBACrB,MAAM,EAAE,IAAI,CAAC,MAAM;iBACpB,CAAC;qBACD,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE;oBACf,IAAI,MAAM,CAAC,OAAO;wBAChB,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,2BAA2B,CAAC,CAAC;oBACvE,OAAO,MAAM,CAAC,MAAM,CAAC;gBACvB,CAAC,CAAC,CAAC;gBACL,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC;YAC3D,CAAC;SACF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
package/dist/client.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- import type { CreateJobResult, SendLetterResult } from './dispatcher.js';
2
- import type { CreateJobInput, InboxInput, SendLetterInput } from './inputs.js';
1
+ import { type CreateJobInput, type CreateJobResult, type InboxInput, type SendLetterInput, type SendLetterResult, type WaitInput, type WaitResult } from './inputs.js';
3
2
  import { type BridgeStatus, type DispatcherInfo } from './protocol.js';
4
3
  import type { AgentFilter, JobTaskFilter, JournalQuery, LetterFilter } from './store/store.js';
5
4
  import type { AgentRecord, BridgeJob, CallerIdentity, JournalEntry, Letter, RenderedLetter } from './types.js';
@@ -47,6 +46,14 @@ export declare class BridgeClient {
47
46
  send(input: SendLetterInput): Promise<SendLetterResult>;
48
47
  inbox(input?: InboxInput): Promise<RenderedLetter[]>;
49
48
  acknowledge(letter: string): Promise<Letter>;
49
+ /**
50
+ * Waits for the session's mail, see `BridgeDispatcher.wait`. The connection answers nothing
51
+ * else until the wait ends: a process that waits uses a connection of its own. Closing the
52
+ * client ends the wait
53
+ */
54
+ wait(input?: WaitInput): Promise<WaitResult>;
55
+ /** Says the letters a wait returned reached the session */
56
+ delivered(letters: readonly string[]): Promise<Letter[]>;
50
57
  cancel(job: number, reason?: string): Promise<BridgeJob>;
51
58
  job(number: number): Promise<BridgeJob>;
52
59
  jobs(filter?: JobTaskFilter): Promise<BridgeJob[]>;
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAEzE,OAAO,KAAK,EAAE,cAAc,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC/E,OAAO,EAKL,KAAK,YAAY,EACjB,KAAK,cAAc,EAEpB,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAC/F,OAAO,KAAK,EACV,WAAW,EACX,SAAS,EACT,cAAc,EACd,YAAY,EACZ,MAAM,EACN,cAAc,EACf,MAAM,YAAY,CAAC;AAEpB,MAAM,WAAW,mBAAmB;IAClC,mCAAmC;IACnC,UAAU,EAAE,MAAM,CAAC;IACnB,sFAAsF;IACtF,MAAM,CAAC,EAAE,cAAc,CAAC;IACxB,4EAA4E;IAC5E,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AA2BD;;;;;;;;;;;GAWG;AACH,qBAAa,YAAY;IAOrB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,OAAO,CAAC,QAAQ,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AY,CAAC;IAI/B;;;OAGG;IACG,SAAS,CACb,QAAQ,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,EACvC,OAAO,GAAE;QAAE,GAAG,CAAC,EAAE,MAAM,CAAA;KAAO,GAC7B,OAAO,CAAC,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAc/B;;;;OAIG;IACH,OAAO,CAAC,gBAAgB;IAgBxB,oEAAoE;IAC9D,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;YASd,IAAI;CAOnB"}
package/dist/client.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { connect } from 'node:net';
2
2
  import { JsonRpcPeer, encodeNdjsonLine, readNdjson, } from '@cogitator-ai/server-shared';
3
3
  import { BridgeError } from './errors.js';
4
+ import { DEFAULT_WAIT_MS, } from './inputs.js';
4
5
  import { BRIDGE_METHODS, BRIDGE_NOTIFICATIONS, BRIDGE_PROTOCOL_VERSION, fromJsonRpcError, } from './protocol.js';
5
6
  const DEFAULT_TIMEOUT_MS = 30_000;
6
7
  const NO_DISPATCHER_CODES = new Set(['ENOENT', 'ECONNREFUSED', 'ENOTSOCK']);
@@ -119,6 +120,19 @@ export class BridgeClient {
119
120
  acknowledge(letter) {
120
121
  return this.call(BRIDGE_METHODS.acknowledge, { letter });
121
122
  }
123
+ /**
124
+ * Waits for the session's mail, see `BridgeDispatcher.wait`. The connection answers nothing
125
+ * else until the wait ends: a process that waits uses a connection of its own. Closing the
126
+ * client ends the wait
127
+ */
128
+ wait(input = {}) {
129
+ const waitMs = input.timeoutMs ?? DEFAULT_WAIT_MS;
130
+ return this.call(BRIDGE_METHODS.wait, input, waitMs + this.timeoutMs);
131
+ }
132
+ /** Says the letters a wait returned reached the session */
133
+ delivered(letters) {
134
+ return this.call(BRIDGE_METHODS.delivered, { letters });
135
+ }
122
136
  cancel(job, reason) {
123
137
  return this.call(BRIDGE_METHODS.cancelJob, { job, ...(reason !== undefined && { reason }) });
124
138
  }
@@ -182,7 +196,7 @@ export class BridgeClient {
182
196
  }
183
197
  /** Ends the connection. Calls still waiting for an answer reject */
184
198
  async close() {
185
- this.peer.close();
199
+ this.peer.close(new BridgeError('not_running', 'The connection to the bridge was closed'));
186
200
  if (this.socket.destroyed)
187
201
  return;
188
202
  await new Promise((resolve) => {
@@ -190,9 +204,9 @@ export class BridgeClient {
190
204
  this.socket.end();
191
205
  });
192
206
  }
193
- async call(method, params) {
207
+ async call(method, params, timeoutMs = this.timeoutMs) {
194
208
  try {
195
- return await this.peer.request(method, params, { timeoutMs: this.timeoutMs });
209
+ return await this.peer.request(method, params, { timeoutMs });
196
210
  }
197
211
  catch (error) {
198
212
  throw fromJsonRpcError(error);