@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.
- package/LICENSE +21 -0
- package/README.md +330 -2
- package/dist/adapter.d.ts +123 -0
- package/dist/adapter.d.ts.map +1 -0
- package/dist/adapter.js +2 -0
- package/dist/adapter.js.map +1 -0
- package/dist/adapters/claude.d.ts +90 -0
- package/dist/adapters/claude.d.ts.map +1 -0
- package/dist/adapters/claude.js +359 -0
- package/dist/adapters/claude.js.map +1 -0
- package/dist/adapters/codex.d.ts +58 -0
- package/dist/adapters/codex.d.ts.map +1 -0
- package/dist/adapters/codex.js +330 -0
- package/dist/adapters/codex.js.map +1 -0
- package/dist/adapters/exec.d.ts +57 -0
- package/dist/adapters/exec.d.ts.map +1 -0
- package/dist/adapters/exec.js +149 -0
- package/dist/adapters/exec.js.map +1 -0
- package/dist/adapters/hosted.d.ts +168 -0
- package/dist/adapters/hosted.d.ts.map +1 -0
- package/dist/adapters/hosted.js +361 -0
- package/dist/adapters/hosted.js.map +1 -0
- package/dist/client.d.ts +81 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +216 -0
- package/dist/client.js.map +1 -0
- package/dist/dispatcher.d.ts +296 -0
- package/dist/dispatcher.d.ts.map +1 -0
- package/dist/dispatcher.js +1524 -0
- package/dist/dispatcher.js.map +1 -0
- package/dist/errors.d.ts +10 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +16 -0
- package/dist/errors.js.map +1 -0
- package/dist/guards.d.ts +36 -0
- package/dist/guards.d.ts.map +1 -0
- package/dist/guards.js +85 -0
- package/dist/guards.js.map +1 -0
- package/dist/host.d.ts +49 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +150 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +32 -0
- package/dist/index.js.map +1 -0
- package/dist/inputs.d.ts +184 -0
- package/dist/inputs.d.ts.map +1 -0
- package/dist/inputs.js +116 -0
- package/dist/inputs.js.map +1 -0
- package/dist/job-record.d.ts +47 -0
- package/dist/job-record.d.ts.map +1 -0
- package/dist/job-record.js +65 -0
- package/dist/job-record.js.map +1 -0
- package/dist/letters.d.ts +49 -0
- package/dist/letters.d.ts.map +1 -0
- package/dist/letters.js +96 -0
- package/dist/letters.js.map +1 -0
- package/dist/paths.d.ts +38 -0
- package/dist/paths.d.ts.map +1 -0
- package/dist/paths.js +149 -0
- package/dist/paths.js.map +1 -0
- package/dist/protocol.d.ts +83 -0
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.js +71 -0
- package/dist/protocol.js.map +1 -0
- package/dist/registry.d.ts +62 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +185 -0
- package/dist/registry.js.map +1 -0
- package/dist/server.d.ts +31 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +254 -0
- package/dist/server.js.map +1 -0
- package/dist/store/sqlite.d.ts +56 -0
- package/dist/store/sqlite.d.ts.map +1 -0
- package/dist/store/sqlite.js +737 -0
- package/dist/store/sqlite.js.map +1 -0
- package/dist/store/store.d.ts +128 -0
- package/dist/store/store.d.ts.map +1 -0
- package/dist/store/store.js +2 -0
- package/dist/store/store.js.map +1 -0
- package/dist/testing/conformance.d.ts +65 -0
- package/dist/testing/conformance.d.ts.map +1 -0
- package/dist/testing/conformance.js +217 -0
- package/dist/testing/conformance.js.map +1 -0
- package/dist/testing/fake-harnesses.d.ts +101 -0
- package/dist/testing/fake-harnesses.d.ts.map +1 -0
- package/dist/testing/fake-harnesses.js +291 -0
- package/dist/testing/fake-harnesses.js.map +1 -0
- package/dist/testing/index.d.ts +5 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +3 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/types.d.ts +286 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +132 -0
- package/dist/types.js.map +1 -0
- package/dist/waiter.d.ts +36 -0
- package/dist/waiter.d.ts.map +1 -0
- package/dist/waiter.js +92 -0
- package/dist/waiter.js.map +1 -0
- package/fakes/claude.mjs +450 -0
- package/fakes/codex.mjs +391 -0
- package/fakes/lib.mjs +507 -0
- package/package.json +76 -3
package/dist/types.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { isAbsolute } from 'node:path';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
/** Metadata key of an A2A task holding the bridge's record of the job. */
|
|
4
|
+
export const BRIDGE_JOB_KEY = 'cogitator:bridge';
|
|
5
|
+
/** The sender of letters the bridge writes itself: reminders, and news of a job that ended. */
|
|
6
|
+
export const BRIDGE_SENDER = 'bridge';
|
|
7
|
+
/** Harnesses the bridge knows by name. Any other string names a harness an adapter brings. */
|
|
8
|
+
export const HARNESSES = {
|
|
9
|
+
claudeCode: 'claude-code',
|
|
10
|
+
codex: 'codex',
|
|
11
|
+
acp: 'acp',
|
|
12
|
+
cogitator: 'cogitator',
|
|
13
|
+
};
|
|
14
|
+
export const AdapterCapabilitiesSchema = z.object({
|
|
15
|
+
createSession: z.boolean(),
|
|
16
|
+
push: z.boolean(),
|
|
17
|
+
steer: z.boolean(),
|
|
18
|
+
nativeWindow: z.boolean(),
|
|
19
|
+
});
|
|
20
|
+
/** What a session without an adapter can do: it reads its mail when it asks for it. */
|
|
21
|
+
export const NO_CAPABILITIES = {
|
|
22
|
+
createSession: false,
|
|
23
|
+
push: false,
|
|
24
|
+
steer: false,
|
|
25
|
+
nativeWindow: false,
|
|
26
|
+
};
|
|
27
|
+
/** How letters reach a session that has no adapter: it takes them from its inbox itself. */
|
|
28
|
+
export const INBOX_DELIVERY = 'inbox';
|
|
29
|
+
/**
|
|
30
|
+
* The name of a harness: letters, digits, `.`, `_` and `-`, compared without regard to case and
|
|
31
|
+
* kept in lower case. It becomes part of agent names, so it takes nothing else.
|
|
32
|
+
*/
|
|
33
|
+
export const HarnessNameSchema = z
|
|
34
|
+
.string()
|
|
35
|
+
.trim()
|
|
36
|
+
.min(1)
|
|
37
|
+
.max(64)
|
|
38
|
+
.regex(/^[A-Za-z0-9][A-Za-z0-9._-]*$/, 'A harness name takes letters, digits, ".", "_" and "-"')
|
|
39
|
+
.transform((name) => name.toLowerCase());
|
|
40
|
+
const AbsolutePathSchema = z
|
|
41
|
+
.string()
|
|
42
|
+
.min(1)
|
|
43
|
+
.max(4096)
|
|
44
|
+
.refine((path) => isAbsolute(path), 'The path must be absolute');
|
|
45
|
+
/** Text a person typed that ends up on one line: no line breaks or other control characters. */
|
|
46
|
+
const OneLineSchema = z
|
|
47
|
+
.string()
|
|
48
|
+
.trim()
|
|
49
|
+
.min(1)
|
|
50
|
+
.max(256)
|
|
51
|
+
.regex(/^[^\p{Cc}]*$/u, 'No line breaks or control characters');
|
|
52
|
+
export const CallerIdentitySchema = z.strictObject({
|
|
53
|
+
harness: HarnessNameSchema,
|
|
54
|
+
sessionId: OneLineSchema,
|
|
55
|
+
cwd: AbsolutePathSchema,
|
|
56
|
+
project: AbsolutePathSchema.optional(),
|
|
57
|
+
sessionName: OneLineSchema.optional(),
|
|
58
|
+
agentName: OneLineSchema.optional(),
|
|
59
|
+
});
|
|
60
|
+
export const JOB_STATES = [
|
|
61
|
+
'submitted',
|
|
62
|
+
'working',
|
|
63
|
+
'input-required',
|
|
64
|
+
'completed',
|
|
65
|
+
'failed',
|
|
66
|
+
'canceled',
|
|
67
|
+
];
|
|
68
|
+
export const JobCheckSchema = z.object({
|
|
69
|
+
name: z.string().trim().min(1).max(200).optional(),
|
|
70
|
+
command: z.string().trim().min(1).max(4000),
|
|
71
|
+
cwd: z.string().min(1).max(4096).optional(),
|
|
72
|
+
timeoutMs: z.number().int().positive().max(86_400_000).optional(),
|
|
73
|
+
});
|
|
74
|
+
export const JobBudgetSchema = z.object({
|
|
75
|
+
letters: z.number().int().positive().max(10_000),
|
|
76
|
+
durationMs: z
|
|
77
|
+
.number()
|
|
78
|
+
.int()
|
|
79
|
+
.positive()
|
|
80
|
+
.max(30 * 86_400_000),
|
|
81
|
+
replyTimeoutMs: z
|
|
82
|
+
.number()
|
|
83
|
+
.int()
|
|
84
|
+
.positive()
|
|
85
|
+
.max(7 * 86_400_000),
|
|
86
|
+
});
|
|
87
|
+
export const LETTER_KINDS = [
|
|
88
|
+
'task',
|
|
89
|
+
'question',
|
|
90
|
+
'answer',
|
|
91
|
+
'report',
|
|
92
|
+
'note',
|
|
93
|
+
'permission',
|
|
94
|
+
];
|
|
95
|
+
export const DELIVERY_STATES = ['queued', 'delivered', 'read', 'handled'];
|
|
96
|
+
export const BridgeLimitsSchema = z.strictObject({
|
|
97
|
+
maxDepth: z.number().int().min(1).max(100),
|
|
98
|
+
budget: JobBudgetSchema,
|
|
99
|
+
maxBudget: JobBudgetSchema,
|
|
100
|
+
deliveryAttempts: z.number().int().min(1).max(1000),
|
|
101
|
+
deliveryBackoffMs: z.number().int().min(0).max(86_400_000),
|
|
102
|
+
deliveryBackoffMaxMs: z.number().int().min(0).max(86_400_000),
|
|
103
|
+
deliveryTimeoutMs: z.number().int().positive().max(3_600_000),
|
|
104
|
+
sessionStartTimeoutMs: z.number().int().positive().max(3_600_000),
|
|
105
|
+
maxBodyLength: z.number().int().positive().max(1_000_000),
|
|
106
|
+
agentIdleMs: z
|
|
107
|
+
.number()
|
|
108
|
+
.int()
|
|
109
|
+
.positive()
|
|
110
|
+
.max(365 * 86_400_000),
|
|
111
|
+
maxOpenJobs: z.number().int().positive().max(10_000),
|
|
112
|
+
jobsPerHour: z.number().int().positive().max(100_000),
|
|
113
|
+
maxStartingSessions: z.number().int().positive().max(100),
|
|
114
|
+
presenceCheckMs: z.number().int().positive().max(86_400_000),
|
|
115
|
+
});
|
|
116
|
+
export const DEFAULT_LIMITS = {
|
|
117
|
+
maxDepth: 3,
|
|
118
|
+
budget: { letters: 60, durationMs: 4 * 3_600_000, replyTimeoutMs: 30 * 60_000 },
|
|
119
|
+
maxBudget: { letters: 500, durationMs: 24 * 3_600_000, replyTimeoutMs: 6 * 3_600_000 },
|
|
120
|
+
deliveryAttempts: 5,
|
|
121
|
+
deliveryBackoffMs: 2_000,
|
|
122
|
+
deliveryBackoffMaxMs: 60_000,
|
|
123
|
+
deliveryTimeoutMs: 60_000,
|
|
124
|
+
sessionStartTimeoutMs: 180_000,
|
|
125
|
+
maxBodyLength: 200_000,
|
|
126
|
+
agentIdleMs: 12 * 3_600_000,
|
|
127
|
+
maxOpenJobs: 20,
|
|
128
|
+
jobsPerHour: 60,
|
|
129
|
+
maxStartingSessions: 2,
|
|
130
|
+
presenceCheckMs: 60_000,
|
|
131
|
+
};
|
|
132
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,0EAA0E;AAC1E,MAAM,CAAC,MAAM,cAAc,GAAG,kBAAkB,CAAC;AAEjD,+FAA+F;AAC/F,MAAM,CAAC,MAAM,aAAa,GAAG,QAAQ,CAAC;AAEtC,8FAA8F;AAC9F,MAAM,CAAC,MAAM,SAAS,GAAG;IACvB,UAAU,EAAE,aAAa;IACzB,KAAK,EAAE,OAAO;IACd,GAAG,EAAE,KAAK;IACV,SAAS,EAAE,WAAW;CACd,CAAC;AAcX,MAAM,CAAC,MAAM,yBAAyB,GAAG,CAAC,CAAC,MAAM,CAAC;IAChD,aAAa,EAAE,CAAC,CAAC,OAAO,EAAE;IAC1B,IAAI,EAAE,CAAC,CAAC,OAAO,EAAE;IACjB,KAAK,EAAE,CAAC,CAAC,OAAO,EAAE;IAClB,YAAY,EAAE,CAAC,CAAC,OAAO,EAAE;CAC1B,CAAC,CAAC;AAEH,uFAAuF;AACvF,MAAM,CAAC,MAAM,eAAe,GAAwB;IAClD,aAAa,EA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G,EAAE,CAAC,QAAQ,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC;IACrD,mBAAmB,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC;IACzD,eAAe,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC,GAAG,CAAC,UAAU,CAAC;CAC7D,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,cAAc,GAAiB;IAC1C,QAAQ,EAAE,CAAC;IACX,MAAM,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,UAAU,EAAE,CAAC,GAAG,SAAS,EAAE,cAAc,EAAE,EAAE,GAAG,MAAM,EAAE;IAC/E,SAAS,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,EAAE,GAAG,SAAS,EAAE,cAAc,EAAE,CAAC,GAAG,SAAS,EAAE;IACtF,gBAAgB,EAAE,CAAC;IACnB,iBAAiB,EAAE,KAAK;IACxB,oBAAoB,EAAE,MAAM;IAC5B,iBAAiB,EAAE,MAAM;IACzB,qBAAqB,EAAE,OAAO;IAC9B,aAAa,EAAE,OAAO;IACtB,WAAW,EAAE,EAAE,GAAG,SAAS;IAC3B,WAAW,EAAE,EAAE;IACf,WAAW,EAAE,EAAE;IACf,mBAAmB,EAAE,CAAC;IACtB,eAAe,EAAE,MAAM;CACxB,CAAC"}
|
package/dist/waiter.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { CallerIdentity, RenderedLetter } from './types.js';
|
|
2
|
+
export interface WaitForMailOptions {
|
|
3
|
+
socketPath: string;
|
|
4
|
+
/** The session whose mail is waited for */
|
|
5
|
+
caller: CallerIdentity;
|
|
6
|
+
/** How long to wait in all, a day at most */
|
|
7
|
+
timeoutMs: number;
|
|
8
|
+
/** How often to look for a dispatcher that does not run, 2 s by default */
|
|
9
|
+
retryMs?: number;
|
|
10
|
+
/** Ends the wait early */
|
|
11
|
+
signal?: AbortSignal;
|
|
12
|
+
}
|
|
13
|
+
export interface WaitForMailResult {
|
|
14
|
+
/**
|
|
15
|
+
* Why the wait ended: `letters` came, the time ran out (`timeout`), another waiter of the
|
|
16
|
+
* session took over (`replaced`), or `signal` aborted (`closed`)
|
|
17
|
+
*/
|
|
18
|
+
reason: 'letters' | 'timeout' | 'replaced' | 'closed';
|
|
19
|
+
letters: RenderedLetter[];
|
|
20
|
+
/**
|
|
21
|
+
* Tells the bridge the letters reached the session. Call it once they are where the session
|
|
22
|
+
* reads them: until then they stay queued, and the next waiter gets them again
|
|
23
|
+
*/
|
|
24
|
+
confirm(): Promise<void>;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Waits at the dispatcher for a session's mail, as the process a hook of the session runs. A
|
|
28
|
+
* dispatcher that does not run, or stops while the waiter waits, is waited for too: the session
|
|
29
|
+
* stays reachable across a restart of the dispatcher.
|
|
30
|
+
*
|
|
31
|
+
* @throws BridgeError what the dispatcher refuses the session for, other than not running
|
|
32
|
+
*/
|
|
33
|
+
export declare function waitForMail(options: WaitForMailOptions): Promise<WaitForMailResult>;
|
|
34
|
+
/** The text a waiter hands its session: the letters, oldest first, each under its signature. */
|
|
35
|
+
export declare function mailText(letters: readonly RenderedLetter[]): string;
|
|
36
|
+
//# sourceMappingURL=waiter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"waiter.d.ts","sourceRoot":"","sources":["../src/waiter.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjE,MAAM,WAAW,kBAAkB;IACjC,UAAU,EAAE,MAAM,CAAC;IACnB,2CAA2C;IAC3C,MAAM,EAAE,cAAc,CAAC;IACvB,6CAA6C;IAC7C,SAAS,EAAE,MAAM,CAAC;IAClB,2EAA2E;IAC3E,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,0BAA0B;IAC1B,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED,MAAM,WAAW,iBAAiB;IAChC;;;OAGG;IACH,MAAM,EAAE,SAAS,GAAG,SAAS,GAAG,UAAU,GAAG,QAAQ,CAAC;IACtD,OAAO,EAAE,cAAc,EAAE,CAAC;IAC1B;;;OAGG;IACH,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1B;AAiBD;;;;;;GAMG;AACH,wBAAsB,WAAW,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAwDzF;AAED,gGAAgG;AAChG,wBAAgB,QAAQ,CAAC,OAAO,EAAE,SAAS,cAAc,EAAE,GAAG,MAAM,CAEnE"}
|
package/dist/waiter.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { BridgeClient } from './client.js';
|
|
2
|
+
import { isBridgeError } from './errors.js';
|
|
3
|
+
import { MAX_WAIT_MS } from './inputs.js';
|
|
4
|
+
const DEFAULT_RETRY_MS = 2_000;
|
|
5
|
+
function pause(ms, signal) {
|
|
6
|
+
if (signal?.aborted)
|
|
7
|
+
return Promise.resolve();
|
|
8
|
+
return new Promise((resolve) => {
|
|
9
|
+
const done = () => {
|
|
10
|
+
clearTimeout(timer);
|
|
11
|
+
signal?.removeEventListener('abort', done);
|
|
12
|
+
resolve();
|
|
13
|
+
};
|
|
14
|
+
const timer = setTimeout(done, ms);
|
|
15
|
+
signal?.addEventListener('abort', done, { once: true });
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Waits at the dispatcher for a session's mail, as the process a hook of the session runs. A
|
|
20
|
+
* dispatcher that does not run, or stops while the waiter waits, is waited for too: the session
|
|
21
|
+
* stays reachable across a restart of the dispatcher.
|
|
22
|
+
*
|
|
23
|
+
* @throws BridgeError what the dispatcher refuses the session for, other than not running
|
|
24
|
+
*/
|
|
25
|
+
export async function waitForMail(options) {
|
|
26
|
+
const { signal } = options;
|
|
27
|
+
const deadline = Date.now() + Math.min(options.timeoutMs, MAX_WAIT_MS);
|
|
28
|
+
const retryMs = options.retryMs ?? DEFAULT_RETRY_MS;
|
|
29
|
+
const none = async () => undefined;
|
|
30
|
+
for (;;) {
|
|
31
|
+
const left = deadline - Date.now();
|
|
32
|
+
if (signal?.aborted)
|
|
33
|
+
return { reason: 'closed', letters: [], confirm: none };
|
|
34
|
+
if (left <= 0)
|
|
35
|
+
return { reason: 'timeout', letters: [], confirm: none };
|
|
36
|
+
let client;
|
|
37
|
+
try {
|
|
38
|
+
client = await BridgeClient.connect({
|
|
39
|
+
socketPath: options.socketPath,
|
|
40
|
+
caller: options.caller,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
if (!isBridgeError(error) || error.code !== 'not_running')
|
|
45
|
+
throw error;
|
|
46
|
+
await pause(Math.min(retryMs, left), signal);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (signal?.aborted) {
|
|
50
|
+
await client.close();
|
|
51
|
+
return { reason: 'closed', letters: [], confirm: none };
|
|
52
|
+
}
|
|
53
|
+
const onAbort = () => void client.close();
|
|
54
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
55
|
+
try {
|
|
56
|
+
const waited = await client.wait({ timeoutMs: left });
|
|
57
|
+
if (waited.reason === 'letters') {
|
|
58
|
+
const ids = waited.letters.map((letter) => letter.id);
|
|
59
|
+
const connection = client;
|
|
60
|
+
return {
|
|
61
|
+
reason: 'letters',
|
|
62
|
+
letters: waited.letters,
|
|
63
|
+
confirm: async () => {
|
|
64
|
+
try {
|
|
65
|
+
await connection.delivered(ids);
|
|
66
|
+
}
|
|
67
|
+
finally {
|
|
68
|
+
await connection.close();
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
await client.close();
|
|
74
|
+
if (waited.reason !== 'closed')
|
|
75
|
+
return { reason: waited.reason, letters: [], confirm: none };
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
await client.close().catch(() => undefined);
|
|
79
|
+
if (!isBridgeError(error) || error.code !== 'not_running')
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
finally {
|
|
83
|
+
signal?.removeEventListener('abort', onAbort);
|
|
84
|
+
}
|
|
85
|
+
await pause(Math.min(retryMs, Math.max(0, deadline - Date.now())), signal);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/** The text a waiter hands its session: the letters, oldest first, each under its signature. */
|
|
89
|
+
export function mailText(letters) {
|
|
90
|
+
return letters.map((letter) => letter.text.trimEnd()).join('\n\n');
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=waiter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"waiter.js","sourceRoot":"","sources":["../src/waiter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AA6B1C,MAAM,gBAAgB,GAAG,KAAK,CAAC;AAE/B,SAAS,KAAK,CAAC,EAAU,EAAE,MAA+B;IACxD,IAAI,MAAM,EAAE,OAAO;QAAE,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;IAC9C,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC7B,MAAM,IAAI,GAAG,GAAG,EAAE;YAChB,YAAY,CAAC,KAAK,CAAC,CAAC;YACpB,MAAM,EAAE,mBAAmB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;YAC3C,OAAO,EAAE,CAAC;QACZ,CAAC,CAAC;QACF,MAAM,KAAK,GAAG,UAAU,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QACnC,MAAM,EAAE,gBAAgB,CAAC,OAAO,EAAE,IAAI,EAAE,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