@cogitator-ai/bridge 0.0.0-stage → 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 (106) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +330 -2
  3. package/dist/adapter.d.ts +123 -0
  4. package/dist/adapter.d.ts.map +1 -0
  5. package/dist/adapter.js +2 -0
  6. package/dist/adapter.js.map +1 -0
  7. package/dist/adapters/claude.d.ts +90 -0
  8. package/dist/adapters/claude.d.ts.map +1 -0
  9. package/dist/adapters/claude.js +359 -0
  10. package/dist/adapters/claude.js.map +1 -0
  11. package/dist/adapters/codex.d.ts +58 -0
  12. package/dist/adapters/codex.d.ts.map +1 -0
  13. package/dist/adapters/codex.js +330 -0
  14. package/dist/adapters/codex.js.map +1 -0
  15. package/dist/adapters/exec.d.ts +57 -0
  16. package/dist/adapters/exec.d.ts.map +1 -0
  17. package/dist/adapters/exec.js +149 -0
  18. package/dist/adapters/exec.js.map +1 -0
  19. package/dist/adapters/hosted.d.ts +168 -0
  20. package/dist/adapters/hosted.d.ts.map +1 -0
  21. package/dist/adapters/hosted.js +361 -0
  22. package/dist/adapters/hosted.js.map +1 -0
  23. package/dist/client.d.ts +81 -0
  24. package/dist/client.d.ts.map +1 -0
  25. package/dist/client.js +216 -0
  26. package/dist/client.js.map +1 -0
  27. package/dist/dispatcher.d.ts +296 -0
  28. package/dist/dispatcher.d.ts.map +1 -0
  29. package/dist/dispatcher.js +1524 -0
  30. package/dist/dispatcher.js.map +1 -0
  31. package/dist/errors.d.ts +10 -0
  32. package/dist/errors.d.ts.map +1 -0
  33. package/dist/errors.js +16 -0
  34. package/dist/errors.js.map +1 -0
  35. package/dist/guards.d.ts +36 -0
  36. package/dist/guards.d.ts.map +1 -0
  37. package/dist/guards.js +85 -0
  38. package/dist/guards.js.map +1 -0
  39. package/dist/host.d.ts +49 -0
  40. package/dist/host.d.ts.map +1 -0
  41. package/dist/host.js +150 -0
  42. package/dist/host.js.map +1 -0
  43. package/dist/index.d.ts +50 -0
  44. package/dist/index.d.ts.map +1 -0
  45. package/dist/index.js +32 -0
  46. package/dist/index.js.map +1 -0
  47. package/dist/inputs.d.ts +184 -0
  48. package/dist/inputs.d.ts.map +1 -0
  49. package/dist/inputs.js +116 -0
  50. package/dist/inputs.js.map +1 -0
  51. package/dist/job-record.d.ts +47 -0
  52. package/dist/job-record.d.ts.map +1 -0
  53. package/dist/job-record.js +65 -0
  54. package/dist/job-record.js.map +1 -0
  55. package/dist/letters.d.ts +49 -0
  56. package/dist/letters.d.ts.map +1 -0
  57. package/dist/letters.js +96 -0
  58. package/dist/letters.js.map +1 -0
  59. package/dist/paths.d.ts +38 -0
  60. package/dist/paths.d.ts.map +1 -0
  61. package/dist/paths.js +149 -0
  62. package/dist/paths.js.map +1 -0
  63. package/dist/protocol.d.ts +83 -0
  64. package/dist/protocol.d.ts.map +1 -0
  65. package/dist/protocol.js +71 -0
  66. package/dist/protocol.js.map +1 -0
  67. package/dist/registry.d.ts +62 -0
  68. package/dist/registry.d.ts.map +1 -0
  69. package/dist/registry.js +185 -0
  70. package/dist/registry.js.map +1 -0
  71. package/dist/server.d.ts +31 -0
  72. package/dist/server.d.ts.map +1 -0
  73. package/dist/server.js +254 -0
  74. package/dist/server.js.map +1 -0
  75. package/dist/store/sqlite.d.ts +56 -0
  76. package/dist/store/sqlite.d.ts.map +1 -0
  77. package/dist/store/sqlite.js +737 -0
  78. package/dist/store/sqlite.js.map +1 -0
  79. package/dist/store/store.d.ts +128 -0
  80. package/dist/store/store.d.ts.map +1 -0
  81. package/dist/store/store.js +2 -0
  82. package/dist/store/store.js.map +1 -0
  83. package/dist/testing/conformance.d.ts +65 -0
  84. package/dist/testing/conformance.d.ts.map +1 -0
  85. package/dist/testing/conformance.js +217 -0
  86. package/dist/testing/conformance.js.map +1 -0
  87. package/dist/testing/fake-harnesses.d.ts +101 -0
  88. package/dist/testing/fake-harnesses.d.ts.map +1 -0
  89. package/dist/testing/fake-harnesses.js +291 -0
  90. package/dist/testing/fake-harnesses.js.map +1 -0
  91. package/dist/testing/index.d.ts +5 -0
  92. package/dist/testing/index.d.ts.map +1 -0
  93. package/dist/testing/index.js +3 -0
  94. package/dist/testing/index.js.map +1 -0
  95. package/dist/types.d.ts +286 -0
  96. package/dist/types.d.ts.map +1 -0
  97. package/dist/types.js +132 -0
  98. package/dist/types.js.map +1 -0
  99. package/dist/waiter.d.ts +36 -0
  100. package/dist/waiter.d.ts.map +1 -0
  101. package/dist/waiter.js +92 -0
  102. package/dist/waiter.js.map +1 -0
  103. package/fakes/claude.mjs +450 -0
  104. package/fakes/codex.mjs +391 -0
  105. package/fakes/lib.mjs +507 -0
  106. package/package.json +76 -3
package/dist/server.js ADDED
@@ -0,0 +1,254 @@
1
+ import { chmodSync, unlinkSync } from 'node:fs';
2
+ import { createServer } from 'node:net';
3
+ import { JSON_RPC_ERROR_CODES, JsonRpcPeer, encodeNdjsonLine, readNdjson, } from '@cogitator-ai/server-shared';
4
+ import { BridgeError } from './errors.js';
5
+ import { AcknowledgeParamsSchema, DeliveredParamsSchema, BRIDGE_METHODS, BRIDGE_NOTIFICATIONS, BRIDGE_PROTOCOL_VERSION, CancelJobParamsSchema, HelloParamsSchema, JobNumberParamsSchema, SubscribeJournalParamsSchema, toJsonRpcError, } from './protocol.js';
6
+ /** The most connections the dispatcher holds at once. */
7
+ const MAX_CONNECTIONS = 256;
8
+ /** A connection that does not read is dropped once this much waits to be written to it. */
9
+ const MAX_PENDING_BYTES = 16 * 1024 * 1024;
10
+ function isPipe(path) {
11
+ return path.startsWith('\\\\.\\pipe\\');
12
+ }
13
+ function params(schema, value) {
14
+ const parsed = schema.safeParse(value ?? {});
15
+ if (parsed.success)
16
+ return parsed.data;
17
+ const issue = parsed.error.issues[0];
18
+ const path = issue.path.length > 0 ? `${issue.path.join('.')}: ` : '';
19
+ throw new BridgeError('invalid_input', `${path}${issue.message}`);
20
+ }
21
+ function describeError(error) {
22
+ if (error instanceof BridgeError)
23
+ return toJsonRpcError(error);
24
+ return {
25
+ code: JSON_RPC_ERROR_CODES.internalError,
26
+ message: `The bridge failed to handle the call: ${error instanceof Error ? error.message : String(error)}`,
27
+ };
28
+ }
29
+ class Connection {
30
+ socket;
31
+ options;
32
+ agent;
33
+ subscription;
34
+ peer;
35
+ /** Aborts when the socket closes, so a call that waits ends with its connection */
36
+ gone = new AbortController();
37
+ constructor(socket, options) {
38
+ this.socket = socket;
39
+ this.options = options;
40
+ this.peer = new JsonRpcPeer({
41
+ send: (message) => this.write(message),
42
+ describeError,
43
+ onError: (error) => options.onError?.(error),
44
+ });
45
+ socket.once('close', () => this.gone.abort());
46
+ this.handle();
47
+ }
48
+ async serve() {
49
+ try {
50
+ for await (const line of readNdjson(this.socket)) {
51
+ if ('value' in line) {
52
+ await this.peer.receive(line.value);
53
+ }
54
+ else {
55
+ await this.peer.respondError(null, {
56
+ code: JSON_RPC_ERROR_CODES.parseError,
57
+ message: `Line ${line.error.line}: ${line.error.message}`,
58
+ });
59
+ }
60
+ }
61
+ }
62
+ catch (error) {
63
+ if (!this.socket.destroyed)
64
+ this.options.onError?.(asError(error));
65
+ }
66
+ finally {
67
+ this.end();
68
+ }
69
+ }
70
+ end() {
71
+ this.subscription = undefined;
72
+ this.peer.close();
73
+ if (!this.socket.destroyed)
74
+ this.socket.destroy();
75
+ }
76
+ /** Passes a journal entry on when the connection asked for it */
77
+ hear(entry) {
78
+ if (!this.subscription || this.peer.closed)
79
+ return;
80
+ if (this.subscription.job !== undefined && this.subscription.job !== entry.job)
81
+ return;
82
+ this.peer.notify(BRIDGE_NOTIFICATIONS.journalEntry, entry).catch(() => undefined);
83
+ }
84
+ write(message) {
85
+ if (this.socket.destroyed)
86
+ return;
87
+ if (this.socket.writableLength > MAX_PENDING_BYTES) {
88
+ this.options.onError?.(new Error('A bridge connection stopped reading and was dropped'));
89
+ this.end();
90
+ return;
91
+ }
92
+ this.socket.write(encodeNdjsonLine(message));
93
+ }
94
+ caller() {
95
+ if (!this.agent) {
96
+ throw new BridgeError('not_registered', 'This connection did not say which session is calling. Say hello with the caller first');
97
+ }
98
+ return this.agent;
99
+ }
100
+ handle() {
101
+ const { dispatcher, info } = this.options;
102
+ const on = (method, handler) => this.peer.onRequest(method, (value) => handler(value));
103
+ on(BRIDGE_METHODS.hello, async (value) => {
104
+ const hello = params(HelloParamsSchema, value);
105
+ if (hello.protocol !== undefined && hello.protocol > BRIDGE_PROTOCOL_VERSION) {
106
+ throw new BridgeError('invalid_input', `The client speaks protocol ${hello.protocol}, this dispatcher speaks ${BRIDGE_PROTOCOL_VERSION}. Restart the dispatcher with the newer version`);
107
+ }
108
+ if (!hello.caller)
109
+ return { dispatcher: info };
110
+ const agent = await dispatcher.register(hello.caller);
111
+ this.agent = agent.name;
112
+ return { dispatcher: info, agent };
113
+ });
114
+ on(BRIDGE_METHODS.status, async () => ({
115
+ ...(await dispatcher.status()),
116
+ dispatcher: info,
117
+ }));
118
+ on(BRIDGE_METHODS.createJob, (value) => dispatcher.createJob(this.caller(), value));
119
+ on(BRIDGE_METHODS.getJob, (value) => dispatcher.job(params(JobNumberParamsSchema, value).job));
120
+ on(BRIDGE_METHODS.listJobs, (value) => dispatcher.jobs((value ?? {})));
121
+ on(BRIDGE_METHODS.cancelJob, (value) => {
122
+ const request = params(CancelJobParamsSchema, value);
123
+ return dispatcher.cancel(this.caller(), request.job, request.reason);
124
+ });
125
+ on(BRIDGE_METHODS.send, (value) => dispatcher.send(this.caller(), value));
126
+ on(BRIDGE_METHODS.inbox, (value) => dispatcher.inbox(this.caller(), (value ?? {})));
127
+ on(BRIDGE_METHODS.acknowledge, (value) => dispatcher.acknowledge(this.caller(), params(AcknowledgeParamsSchema, value).letter));
128
+ on(BRIDGE_METHODS.wait, (value) => dispatcher.wait(this.caller(), (value ?? {}), this.gone.signal));
129
+ on(BRIDGE_METHODS.delivered, (value) => dispatcher.delivered(this.caller(), params(DeliveredParamsSchema, value).letters));
130
+ on(BRIDGE_METHODS.listLetters, (value) => dispatcher.letters((value ?? {})));
131
+ on(BRIDGE_METHODS.listAgents, (value) => dispatcher.agents((value ?? {})));
132
+ on(BRIDGE_METHODS.readJournal, (value) => dispatcher.readJournal((value ?? {})));
133
+ on(BRIDGE_METHODS.subscribeJournal, (value) => {
134
+ const { job } = params(SubscribeJournalParamsSchema, value);
135
+ this.subscription = job === undefined ? {} : { job };
136
+ return { subscribed: true };
137
+ });
138
+ on(BRIDGE_METHODS.unsubscribeJournal, () => {
139
+ this.subscription = undefined;
140
+ return { subscribed: false };
141
+ });
142
+ }
143
+ }
144
+ function asError(error) {
145
+ return error instanceof Error ? error : new Error(String(error));
146
+ }
147
+ /**
148
+ * Narrows the umask to the owner and returns what puts it back. A worker thread cannot change
149
+ * the umask of the process: there the socket is narrowed right after it is bound instead.
150
+ */
151
+ function narrowUmask() {
152
+ try {
153
+ const previous = process.umask(0o177);
154
+ return () => {
155
+ process.umask(previous);
156
+ };
157
+ }
158
+ catch {
159
+ return () => undefined;
160
+ }
161
+ }
162
+ /**
163
+ * Removes what a dispatcher that died left at the socket path, a symlink included, without
164
+ * following it.
165
+ */
166
+ function removeStale(socketPath) {
167
+ try {
168
+ unlinkSync(socketPath);
169
+ }
170
+ catch (error) {
171
+ if (error.code !== 'ENOENT')
172
+ throw error;
173
+ }
174
+ }
175
+ /**
176
+ * Binds the socket. The bind happens inside this call, so the caller narrows the umask around it
177
+ * and the socket file is never there with wider access than its owner's.
178
+ */
179
+ function listen(server, socketPath) {
180
+ return new Promise((resolve, reject) => {
181
+ const onError = (error) => reject(error);
182
+ server.once('error', onError);
183
+ server.listen(socketPath, () => {
184
+ server.off('error', onError);
185
+ resolve();
186
+ });
187
+ });
188
+ }
189
+ /**
190
+ * Serves a dispatcher on a socket of this machine: JSON-RPC 2.0, one message per line. A
191
+ * connection says which session is calling with `bridge/hello`, and acts as that agent from then
192
+ * on. The calls of one connection are handled in the order they were written, one at a time, so
193
+ * a call written right behind the hello already acts as the agent. Whoever serves must hold the dispatcher's lease: a socket file left by a dispatcher that
194
+ * died is removed.
195
+ */
196
+ export async function serveBridge(options) {
197
+ const { dispatcher, socketPath } = options;
198
+ const connections = new Set();
199
+ const server = createServer((socket) => {
200
+ const connection = new Connection(socket, options);
201
+ connections.add(connection);
202
+ socket.on('error', () => undefined);
203
+ void connection.serve().finally(() => connections.delete(connection));
204
+ });
205
+ const pipe = isPipe(socketPath);
206
+ server.maxConnections = MAX_CONNECTIONS;
207
+ if (pipe) {
208
+ await listen(server, socketPath);
209
+ }
210
+ else {
211
+ removeStale(socketPath);
212
+ const restore = narrowUmask();
213
+ let listening;
214
+ try {
215
+ listening = listen(server, socketPath);
216
+ }
217
+ finally {
218
+ restore();
219
+ }
220
+ await listening;
221
+ chmodSync(socketPath, 0o600);
222
+ }
223
+ server.on('error', (error) => options.onError?.(error));
224
+ const onJournal = (entry) => {
225
+ for (const connection of connections)
226
+ connection.hear(entry);
227
+ };
228
+ dispatcher.on('journal', onJournal);
229
+ let closing;
230
+ return {
231
+ get connections() {
232
+ return connections.size;
233
+ },
234
+ close(closeOptions = {}) {
235
+ closing ??= (async () => {
236
+ dispatcher.off('journal', onJournal);
237
+ if (closeOptions.abandon) {
238
+ server.removeAllListeners('connection');
239
+ server.on('connection', (socket) => socket.destroy());
240
+ server.unref();
241
+ for (const connection of connections)
242
+ connection.end();
243
+ return;
244
+ }
245
+ const closed = new Promise((resolve) => server.close(() => resolve()));
246
+ for (const connection of connections)
247
+ connection.end();
248
+ await closed;
249
+ })();
250
+ return closing;
251
+ },
252
+ };
253
+ }
254
+ //# sourceMappingURL=server.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAChD,OAAO,EAAE,YAAY,EAA4B,MAAM,UAAU,CAAC;AAElE,OAAO,EACL,oBAAoB,EACpB,WAAW,EACX,gBAAgB,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G,EAAE,CAAC;gBACvD,MAAM,MAAM,CAAC;YACf,CAAC,CAAC,EAAE,CAAC;YACL,OAAO,OAAO,CAAC;QACjB,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,56 @@
1
+ import { type A2ATask, type TaskStore } from '@cogitator-ai/a2a';
2
+ import { type AgentRecord, type JournalEntry, type Letter } from '../types.js';
3
+ import type { AcquireLeaseOptions, AcquireLeaseResult, AgentFilter, BridgeStore, JobTaskFilter, JournalQuery, Lease, LeaseCandidate, LetterFilter, LetterPatch } from './store.js';
4
+ /** The version of the schema this code reads and writes, kept in `PRAGMA user_version`. */
5
+ export declare const BRIDGE_SCHEMA_VERSION = 1;
6
+ export interface OpenBridgeStoreOptions {
7
+ /** Open to read only: the file must exist and nothing is written, the schema included */
8
+ readonly?: boolean;
9
+ }
10
+ /**
11
+ * The bridge's store in one SQLite file. Reads and writes are synchronous underneath, and a
12
+ * transaction spans the awaits of the work it wraps, which holds as long as one piece of work
13
+ * runs at a time, as the dispatcher's queue makes sure.
14
+ */
15
+ export declare class SqliteBridgeStore implements BridgeStore {
16
+ private readonly db;
17
+ readonly readonly: boolean;
18
+ readonly tasks: TaskStore;
19
+ private readonly taskRows;
20
+ private open;
21
+ private inTransaction;
22
+ private constructor();
23
+ /**
24
+ * Opens the store at `path` (`:memory:` for one that lives with the process), creating the
25
+ * file and its folder when they are missing.
26
+ *
27
+ * @throws BridgeError `storage` when `better-sqlite3` is not installed, the file cannot be
28
+ * opened, or it was written by a newer version of the bridge
29
+ */
30
+ static open(path: string, options?: OpenBridgeStoreOptions): Promise<SqliteBridgeStore>;
31
+ private static migrate;
32
+ transaction<T>(work: () => Promise<T>): Promise<T>;
33
+ acquireLease(candidate: LeaseCandidate, options: AcquireLeaseOptions): AcquireLeaseResult;
34
+ renewLease(nonce: string, now: number): boolean;
35
+ releaseLease(nonce: string): void;
36
+ readLease(): Lease | undefined;
37
+ nextJobNumber(): number;
38
+ taskOfJob(number: number): A2ATask | undefined;
39
+ jobTasks(filter?: JobTaskFilter): A2ATask[];
40
+ putAgent(agent: AgentRecord): void;
41
+ getAgent(name: string): AgentRecord | undefined;
42
+ agentOfSession(harness: string, sessionId: string): AgentRecord | undefined;
43
+ listAgents(filter?: AgentFilter): AgentRecord[];
44
+ mergeAgent(from: string, to: string): void;
45
+ insertLetter(letter: Omit<Letter, 'seq'>): Letter;
46
+ getLetter(id: string): Letter | undefined;
47
+ updateLetter(id: string, patch: LetterPatch): Letter;
48
+ listLetters(filter?: LetterFilter): Letter[];
49
+ countLetters(filter?: LetterFilter): number;
50
+ queuedRecipients(): string[];
51
+ private letterQuery;
52
+ appendJournal(entry: Omit<JournalEntry, 'seq'>): JournalEntry;
53
+ readJournal(query?: JournalQuery): JournalEntry[];
54
+ close(): void;
55
+ }
56
+ //# sourceMappingURL=sqlite.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqlite.d.ts","sourceRoot":"","sources":["../../src/store/sqlite.ts"],"names":[],"mappings":"AAGA,OAAO,EAML,KAAK,OAAO,EAEZ,KAAK,SAAS,EACf,MAAM,mBAAmB,CAAC;AAG3B,OAAO,EAGL,KAAK,WAAW,EAGhB,KAAK,YAAY,EAEjB,KAAK,MAAM,EAGZ,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EACV,mBAAmB,EACnB,kBAAkB,EAClB,WAAW,EACX,WAAW,EACX,aAAa,EACb,YAAY,EACZ,KAAK,EACL,cAAc,EACd,YAAY,EACZ,WAAW,EACZ,MAAM,YAAY,CAAC;AAEpB,2FAA2F;AAC3F,eAAO,MAAM,qBAAqB,IAAI,CAAC;AAqWvC,MAAM,WAAW,sBAAsB;IACrC,yFAAyF;IACzF,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED;;;;GAIG;AACH,qBAAa,iBAAkB,YAAW,WAAW;IAOjD,OAAO,CAAC,QAAQ,CAAC,EAAE;IACnB,QAAQ,CAAC,QAAQ,EAAE,OAAO;IAP5B,QAAQ,CAA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