@joenandez/tldr 1.0.0 → 1.1.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/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/bin/tldr-agent +11 -0
- package/hooks/tldr-agent-capture-session-identity.sh +3 -2
- package/install/tldr-agent-bootstrap.sh +7 -0
- package/package.json +10 -6
- package/release/activation-manifest.json +6 -6
- package/release/payloads/arm64/TldrAgentAegis.pkg +0 -0
- package/release/payloads/arm64/tldr-agent.tar.gz +0 -0
- package/release/starport-release.json +14 -9
- package/skills/tldr/SKILL.md +43 -31
- package/src/lib/aegis_native_distribution.mjs +1 -1
- package/src/lib/aegis_protocol.mjs +1 -1
- package/src/lib/starport_aegis_package.mjs +53 -6
- package/src/lib/starport_onboarding.mjs +303 -6
- package/src/lib/starport_orchestrator.mjs +21 -20
- package/src/lib/starport_release_contract.mjs +48 -10
- package/src/lib/starport_runtime.mjs +131 -13
- package/src/lib/tightbeam_channel.mjs +83 -123
- package/src/lib/tightbeam_channel_runtime.mjs +131 -33
- package/src/lib/tightbeam_email_adapter.mjs +10 -0
- package/src/lib/tightbeam_email_delivery_channel.mjs +107 -133
- package/src/lib/tightbeam_email_provider_map.mjs +337 -0
- package/src/lib/tightbeam_inbound_publish.mjs +84 -0
- package/src/lib/tightbeam_welcome_dispatch.mjs +129 -33
- package/src/lib/tldr_agent_daemon_health.mjs +2 -1
- package/src/lib/tldr_agent_daemon_phase_timeouts.mjs +3 -5
- package/src/lib/tldr_agent_diagnostic_schema.mjs +1 -0
- package/src/lib/tldr_agent_launchd_definition.mjs +2 -0
- package/src/lib/tldr_agent_launchd_lifecycle.mjs +41 -10
- package/src/lib/tldr_agent_runtime_identity.mjs +14 -2
- package/src/lib/tldr_agent_source_lifecycle.mjs +18 -4
- package/src/lib/transports/email/render/email.mjs +66 -180
- package/src/lib/transports/email/render/html.mjs +37 -80
- package/src/lib/transports/email/render/text.mjs +2 -2
- package/src/lib/transports/email/render/tldr.mjs +10 -88
- package/src/lib/transports/email/render/tokens.mjs +56 -75
- package/src/lib/transports/email/templates.mjs +38 -27
- package/src/lib/transports/email.mjs +21 -1
- package/src/starport.mjs +12 -0
- package/src/lib/transports/email/render/glyphs.mjs +0 -66
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
import "./node_sqlite_warning.mjs";
|
|
2
|
+
import { chmodSync, existsSync } from "node:fs";
|
|
3
|
+
import { chmod, mkdir } from "node:fs/promises";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { DatabaseSync } from "node:sqlite";
|
|
6
|
+
|
|
7
|
+
function providerMapPath(home) {
|
|
8
|
+
return join(home, "tightbeam-email-provider-map.sqlite");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function secureProviderMapFiles(path) {
|
|
12
|
+
for (const candidate of [path, `${path}-wal`, `${path}-shm`]) {
|
|
13
|
+
if (existsSync(candidate)) chmodSync(candidate, 0o600);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function openProviderMap(home, { readOnly = false } = {}) {
|
|
18
|
+
const path = providerMapPath(home);
|
|
19
|
+
if (readOnly && !existsSync(path)) return null;
|
|
20
|
+
const db = new DatabaseSync(path, readOnly ? { readOnly: true } : {});
|
|
21
|
+
try {
|
|
22
|
+
if (!readOnly) {
|
|
23
|
+
secureProviderMapFiles(path);
|
|
24
|
+
db.exec("PRAGMA journal_mode = WAL; PRAGMA busy_timeout = 5000;");
|
|
25
|
+
secureProviderMapFiles(path);
|
|
26
|
+
db.exec(`CREATE TABLE IF NOT EXISTS provider_acceptance (
|
|
27
|
+
delivery_id TEXT PRIMARY KEY,
|
|
28
|
+
message_id TEXT NOT NULL,
|
|
29
|
+
conversation_id TEXT NOT NULL,
|
|
30
|
+
route_id TEXT NOT NULL,
|
|
31
|
+
origin_endpoint_id TEXT,
|
|
32
|
+
origin_session_id TEXT,
|
|
33
|
+
external_id TEXT NOT NULL,
|
|
34
|
+
external_thread_id TEXT NOT NULL
|
|
35
|
+
);
|
|
36
|
+
CREATE TABLE IF NOT EXISTS provider_reply_binding (
|
|
37
|
+
delivery_id TEXT PRIMARY KEY,
|
|
38
|
+
reply_binding TEXT NOT NULL,
|
|
39
|
+
conversation_id TEXT,
|
|
40
|
+
external_id TEXT,
|
|
41
|
+
external_thread_id TEXT,
|
|
42
|
+
current_reply_parent_external_id TEXT
|
|
43
|
+
);
|
|
44
|
+
CREATE TABLE IF NOT EXISTS provider_reply_parent_event (
|
|
45
|
+
provider_message_id TEXT PRIMARY KEY,
|
|
46
|
+
provider_thread_id TEXT NOT NULL,
|
|
47
|
+
delivery_id TEXT NOT NULL,
|
|
48
|
+
reply_binding TEXT NOT NULL,
|
|
49
|
+
tightbeam_message_id TEXT NOT NULL
|
|
50
|
+
);
|
|
51
|
+
CREATE INDEX IF NOT EXISTS provider_acceptance_conversation
|
|
52
|
+
ON provider_acceptance(conversation_id);
|
|
53
|
+
CREATE INDEX IF NOT EXISTS provider_acceptance_thread
|
|
54
|
+
ON provider_acceptance(external_thread_id);
|
|
55
|
+
CREATE INDEX IF NOT EXISTS provider_reply_binding_thread
|
|
56
|
+
ON provider_reply_binding(external_thread_id);
|
|
57
|
+
CREATE INDEX IF NOT EXISTS provider_reply_binding_conversation
|
|
58
|
+
ON provider_reply_binding(conversation_id);`);
|
|
59
|
+
const columns = new Set(
|
|
60
|
+
db
|
|
61
|
+
.prepare("PRAGMA table_info(provider_acceptance)")
|
|
62
|
+
.all()
|
|
63
|
+
.map(({ name }) => name),
|
|
64
|
+
);
|
|
65
|
+
if (!columns.has("origin_endpoint_id")) {
|
|
66
|
+
db.exec(
|
|
67
|
+
"ALTER TABLE provider_acceptance ADD COLUMN origin_endpoint_id TEXT",
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
if (!columns.has("origin_session_id")) {
|
|
71
|
+
db.exec(
|
|
72
|
+
"ALTER TABLE provider_acceptance ADD COLUMN origin_session_id TEXT",
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
const bindingColumns = new Set(
|
|
76
|
+
db
|
|
77
|
+
.prepare("PRAGMA table_info(provider_reply_binding)")
|
|
78
|
+
.all()
|
|
79
|
+
.map(({ name }) => name),
|
|
80
|
+
);
|
|
81
|
+
if (!bindingColumns.has("conversation_id")) {
|
|
82
|
+
db.exec(
|
|
83
|
+
"ALTER TABLE provider_reply_binding ADD COLUMN conversation_id TEXT",
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
if (!bindingColumns.has("current_reply_parent_external_id")) {
|
|
87
|
+
db.exec(
|
|
88
|
+
"ALTER TABLE provider_reply_binding ADD COLUMN current_reply_parent_external_id TEXT",
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return db;
|
|
93
|
+
} catch (error) {
|
|
94
|
+
db.close();
|
|
95
|
+
throw error;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function mutateProviderMap(home, mutate) {
|
|
100
|
+
await mkdir(home, { recursive: true, mode: 0o700 });
|
|
101
|
+
const db = openProviderMap(home);
|
|
102
|
+
try {
|
|
103
|
+
db.exec("BEGIN IMMEDIATE");
|
|
104
|
+
mutate(db);
|
|
105
|
+
db.exec("COMMIT");
|
|
106
|
+
} catch (error) {
|
|
107
|
+
try {
|
|
108
|
+
db.exec("ROLLBACK");
|
|
109
|
+
} catch {}
|
|
110
|
+
throw error;
|
|
111
|
+
} finally {
|
|
112
|
+
db.close();
|
|
113
|
+
}
|
|
114
|
+
await chmod(providerMapPath(home), 0o600);
|
|
115
|
+
secureProviderMapFiles(providerMapPath(home));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export async function recordProviderReplyBinding(home, delivery) {
|
|
119
|
+
await mutateProviderMap(home, (db) => {
|
|
120
|
+
db.prepare(
|
|
121
|
+
`INSERT INTO provider_reply_binding (delivery_id, reply_binding, conversation_id)
|
|
122
|
+
VALUES (?, ?, ?)
|
|
123
|
+
ON CONFLICT(delivery_id) DO UPDATE SET
|
|
124
|
+
reply_binding = excluded.reply_binding,
|
|
125
|
+
conversation_id = excluded.conversation_id`,
|
|
126
|
+
).run(
|
|
127
|
+
delivery.delivery_id,
|
|
128
|
+
delivery.reply_binding,
|
|
129
|
+
delivery.message.conversation_id,
|
|
130
|
+
);
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export async function recordBoundProviderAcceptance(home, delivery, provider) {
|
|
135
|
+
await mutateProviderMap(home, (db) => {
|
|
136
|
+
const binding = db
|
|
137
|
+
.prepare(
|
|
138
|
+
`SELECT conversation_id, current_reply_parent_external_id
|
|
139
|
+
FROM provider_reply_binding WHERE delivery_id = ?`,
|
|
140
|
+
)
|
|
141
|
+
.get(delivery.delivery_id);
|
|
142
|
+
if (!binding) {
|
|
143
|
+
throw new Error(
|
|
144
|
+
"Tightbeam reply binding is not durable before provider acceptance",
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
const previous = db
|
|
148
|
+
.prepare(
|
|
149
|
+
`SELECT current_reply_parent_external_id, external_id
|
|
150
|
+
FROM provider_reply_binding
|
|
151
|
+
WHERE conversation_id = ? AND delivery_id <> ?
|
|
152
|
+
AND external_id IS NOT NULL AND external_thread_id = ?
|
|
153
|
+
ORDER BY rowid DESC LIMIT 1`,
|
|
154
|
+
)
|
|
155
|
+
.get(
|
|
156
|
+
binding.conversation_id,
|
|
157
|
+
delivery.delivery_id,
|
|
158
|
+
provider.external_thread_id,
|
|
159
|
+
);
|
|
160
|
+
const replyParent =
|
|
161
|
+
binding.current_reply_parent_external_id ||
|
|
162
|
+
previous?.current_reply_parent_external_id ||
|
|
163
|
+
previous?.external_id ||
|
|
164
|
+
provider.external_id;
|
|
165
|
+
const updated = db
|
|
166
|
+
.prepare(
|
|
167
|
+
`UPDATE provider_reply_binding
|
|
168
|
+
SET external_id = ?, external_thread_id = ?,
|
|
169
|
+
current_reply_parent_external_id = ?
|
|
170
|
+
WHERE delivery_id = ?`,
|
|
171
|
+
)
|
|
172
|
+
.run(
|
|
173
|
+
provider.external_id,
|
|
174
|
+
provider.external_thread_id,
|
|
175
|
+
replyParent,
|
|
176
|
+
delivery.delivery_id,
|
|
177
|
+
);
|
|
178
|
+
if (updated.changes !== 1)
|
|
179
|
+
throw new Error("Tightbeam provider acceptance could not be recorded");
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export async function recordInboundProviderAcceptance(home, record) {
|
|
184
|
+
await mutateProviderMap(home, (db) => {
|
|
185
|
+
const hasProviderThread = typeof record.provider_thread_id === "string";
|
|
186
|
+
const bindings = db
|
|
187
|
+
.prepare(
|
|
188
|
+
`SELECT delivery_id, conversation_id, external_id, external_thread_id
|
|
189
|
+
FROM provider_reply_binding
|
|
190
|
+
WHERE reply_binding = ?${hasProviderThread ? " AND external_thread_id = ?" : ""}`,
|
|
191
|
+
)
|
|
192
|
+
.all(
|
|
193
|
+
record.reply_binding,
|
|
194
|
+
...(hasProviderThread ? [record.provider_thread_id] : []),
|
|
195
|
+
);
|
|
196
|
+
if (bindings.length !== 1 || typeof bindings[0].external_id !== "string") {
|
|
197
|
+
throw new Error("Tightbeam provider reply binding is not accepted");
|
|
198
|
+
}
|
|
199
|
+
const binding = bindings[0];
|
|
200
|
+
const providerThreadId = binding.external_thread_id;
|
|
201
|
+
if (
|
|
202
|
+
typeof record.tightbeam_conversation_id === "string" &&
|
|
203
|
+
record.tightbeam_conversation_id !== binding.conversation_id
|
|
204
|
+
) {
|
|
205
|
+
throw new Error(
|
|
206
|
+
"Tightbeam inbound conversation does not match its provider binding",
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const existing = db
|
|
211
|
+
.prepare(
|
|
212
|
+
`SELECT provider_thread_id, delivery_id, reply_binding, tightbeam_message_id
|
|
213
|
+
FROM provider_reply_parent_event
|
|
214
|
+
WHERE provider_message_id = ?`,
|
|
215
|
+
)
|
|
216
|
+
.get(record.provider_message_id);
|
|
217
|
+
if (existing) {
|
|
218
|
+
if (
|
|
219
|
+
existing.provider_thread_id !== providerThreadId ||
|
|
220
|
+
existing.delivery_id !== binding.delivery_id ||
|
|
221
|
+
existing.reply_binding !== record.reply_binding ||
|
|
222
|
+
existing.tightbeam_message_id !== record.tightbeam_message_id
|
|
223
|
+
) {
|
|
224
|
+
throw new Error(
|
|
225
|
+
"Provider inbound event is bound to different reply authority",
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
db.prepare(
|
|
232
|
+
`INSERT INTO provider_reply_parent_event
|
|
233
|
+
(provider_message_id, provider_thread_id, delivery_id, reply_binding, tightbeam_message_id)
|
|
234
|
+
VALUES (?, ?, ?, ?, ?)`,
|
|
235
|
+
).run(
|
|
236
|
+
record.provider_message_id,
|
|
237
|
+
providerThreadId,
|
|
238
|
+
binding.delivery_id,
|
|
239
|
+
record.reply_binding,
|
|
240
|
+
record.tightbeam_message_id,
|
|
241
|
+
);
|
|
242
|
+
const updated = db
|
|
243
|
+
.prepare(
|
|
244
|
+
`UPDATE provider_reply_binding
|
|
245
|
+
SET current_reply_parent_external_id = ?
|
|
246
|
+
WHERE delivery_id = ? AND reply_binding = ? AND external_thread_id = ?`,
|
|
247
|
+
)
|
|
248
|
+
.run(
|
|
249
|
+
record.provider_message_id,
|
|
250
|
+
binding.delivery_id,
|
|
251
|
+
record.reply_binding,
|
|
252
|
+
providerThreadId,
|
|
253
|
+
);
|
|
254
|
+
if (updated.changes !== 1) {
|
|
255
|
+
throw new Error("Tightbeam provider reply parent could not be advanced");
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export async function recordProviderMap(home, record) {
|
|
261
|
+
await mutateProviderMap(home, (db) => {
|
|
262
|
+
db.prepare(
|
|
263
|
+
`INSERT INTO provider_acceptance (
|
|
264
|
+
delivery_id, message_id, conversation_id, route_id, external_id, external_thread_id
|
|
265
|
+
, origin_endpoint_id, origin_session_id
|
|
266
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
267
|
+
ON CONFLICT(delivery_id) DO UPDATE SET
|
|
268
|
+
message_id = excluded.message_id,
|
|
269
|
+
conversation_id = excluded.conversation_id,
|
|
270
|
+
route_id = excluded.route_id,
|
|
271
|
+
origin_endpoint_id = excluded.origin_endpoint_id,
|
|
272
|
+
origin_session_id = excluded.origin_session_id,
|
|
273
|
+
external_id = excluded.external_id,
|
|
274
|
+
external_thread_id = excluded.external_thread_id`,
|
|
275
|
+
).run(
|
|
276
|
+
record.delivery_id,
|
|
277
|
+
record.message_id,
|
|
278
|
+
record.conversation_id,
|
|
279
|
+
record.route_id,
|
|
280
|
+
record.external_id,
|
|
281
|
+
record.external_thread_id,
|
|
282
|
+
record.origin_endpoint_id,
|
|
283
|
+
record.origin_session_id,
|
|
284
|
+
);
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export function lookupProviderMap(home, column, value) {
|
|
289
|
+
const db = openProviderMap(home, { readOnly: true });
|
|
290
|
+
if (!db) return null;
|
|
291
|
+
try {
|
|
292
|
+
const latest =
|
|
293
|
+
column === "delivery_id" ? "" : " ORDER BY rowid DESC LIMIT 1";
|
|
294
|
+
return (
|
|
295
|
+
db
|
|
296
|
+
.prepare(
|
|
297
|
+
`SELECT delivery_id, message_id, conversation_id, route_id, origin_endpoint_id, origin_session_id, external_id, external_thread_id FROM provider_acceptance WHERE ${column} = ?${latest}`,
|
|
298
|
+
)
|
|
299
|
+
.get(value) || null
|
|
300
|
+
);
|
|
301
|
+
} finally {
|
|
302
|
+
db.close();
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
export function lookupProviderReplyBinding(home, column, value) {
|
|
307
|
+
const db = openProviderMap(home, { readOnly: true });
|
|
308
|
+
if (!db) return null;
|
|
309
|
+
try {
|
|
310
|
+
const columns = new Set(
|
|
311
|
+
db
|
|
312
|
+
.prepare("PRAGMA table_info(provider_reply_binding)")
|
|
313
|
+
.all()
|
|
314
|
+
.map(({ name }) => name),
|
|
315
|
+
);
|
|
316
|
+
const currentReplyParent = columns.has("current_reply_parent_external_id")
|
|
317
|
+
? "current_reply_parent_external_id"
|
|
318
|
+
: "external_id AS current_reply_parent_external_id";
|
|
319
|
+
const accepted =
|
|
320
|
+
column === "delivery_id"
|
|
321
|
+
? ""
|
|
322
|
+
: " AND external_id IS NOT NULL AND external_thread_id IS NOT NULL";
|
|
323
|
+
const latest =
|
|
324
|
+
column === "delivery_id" ? "" : " ORDER BY rowid DESC LIMIT 1";
|
|
325
|
+
return (
|
|
326
|
+
db
|
|
327
|
+
.prepare(
|
|
328
|
+
`SELECT delivery_id, reply_binding, conversation_id, external_id, external_thread_id,
|
|
329
|
+
${currentReplyParent}
|
|
330
|
+
FROM provider_reply_binding WHERE ${column} = ?${accepted}${latest}`,
|
|
331
|
+
)
|
|
332
|
+
.get(value) || null
|
|
333
|
+
);
|
|
334
|
+
} finally {
|
|
335
|
+
db.close();
|
|
336
|
+
}
|
|
337
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
const RAW_REPLY_AUTHORITY_FIELDS = Object.freeze([
|
|
2
|
+
"conversation_id",
|
|
3
|
+
"route_id",
|
|
4
|
+
"origin_endpoint_id",
|
|
5
|
+
"origin_session_id",
|
|
6
|
+
]);
|
|
7
|
+
|
|
8
|
+
function hasRawReplyAuthority(binding) {
|
|
9
|
+
return RAW_REPLY_AUTHORITY_FIELDS.some((field) => field in binding);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function createTightbeamInboundPublisher({
|
|
13
|
+
execute,
|
|
14
|
+
readIdentity,
|
|
15
|
+
commandFailure,
|
|
16
|
+
delivery,
|
|
17
|
+
}) {
|
|
18
|
+
return async function publishInboundEmail({
|
|
19
|
+
binding,
|
|
20
|
+
provider_message_id: providerMessageId,
|
|
21
|
+
provider_thread_id: providerThreadId,
|
|
22
|
+
body,
|
|
23
|
+
}) {
|
|
24
|
+
const hasReplyBinding =
|
|
25
|
+
binding &&
|
|
26
|
+
typeof binding.reply_binding === "string" &&
|
|
27
|
+
binding.reply_binding.length > 0;
|
|
28
|
+
if (hasReplyBinding && hasRawReplyAuthority(binding)) {
|
|
29
|
+
return Object.freeze({ ok: false, code: "invalid_binding" });
|
|
30
|
+
}
|
|
31
|
+
if (hasReplyBinding) {
|
|
32
|
+
if (typeof providerMessageId !== "string" || typeof body !== "string") {
|
|
33
|
+
return Object.freeze({ ok: false, code: "thread_unmapped" });
|
|
34
|
+
}
|
|
35
|
+
const identity = await readIdentity();
|
|
36
|
+
if (!identity)
|
|
37
|
+
return Object.freeze({ ok: false, code: "not_configured" });
|
|
38
|
+
const published = await execute({
|
|
39
|
+
admin: false,
|
|
40
|
+
credentials: identity,
|
|
41
|
+
args: [
|
|
42
|
+
"channel",
|
|
43
|
+
"reply",
|
|
44
|
+
"publish",
|
|
45
|
+
"--reply-binding",
|
|
46
|
+
binding.reply_binding,
|
|
47
|
+
"--body",
|
|
48
|
+
body,
|
|
49
|
+
"--external-event-id",
|
|
50
|
+
providerMessageId,
|
|
51
|
+
"--provider-metadata-json",
|
|
52
|
+
JSON.stringify({ channel: "email" }),
|
|
53
|
+
],
|
|
54
|
+
});
|
|
55
|
+
if (
|
|
56
|
+
commandFailure(published) ||
|
|
57
|
+
typeof published.result?.message_id !== "string"
|
|
58
|
+
) {
|
|
59
|
+
return Object.freeze({ ok: false, code: "publish_failed" });
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
await delivery.recordInboundProviderAcceptance({
|
|
63
|
+
binding,
|
|
64
|
+
provider_message_id: providerMessageId,
|
|
65
|
+
provider_thread_id: providerThreadId,
|
|
66
|
+
tightbeam_message_id: published.result.message_id,
|
|
67
|
+
tightbeam_conversation_id: published.result.conversation_id,
|
|
68
|
+
});
|
|
69
|
+
} catch {
|
|
70
|
+
return Object.freeze({
|
|
71
|
+
ok: false,
|
|
72
|
+
code: "reply_parent_persist_failed",
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
// Tightbeam owns canonical routing and delivery choice. Preserve its
|
|
76
|
+
// result verbatim; the adapter only advances provider reply continuity.
|
|
77
|
+
return Object.freeze({ ok: true, ...published.result });
|
|
78
|
+
}
|
|
79
|
+
return Object.freeze({
|
|
80
|
+
ok: false,
|
|
81
|
+
code: binding ? "invalid_binding" : "thread_unmapped",
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
}
|
|
@@ -1,15 +1,101 @@
|
|
|
1
|
+
export function createTightbeamWelcomeIdentityManager({
|
|
2
|
+
readIdentity,
|
|
3
|
+
persistIdentity,
|
|
4
|
+
execute,
|
|
5
|
+
commandFailure,
|
|
6
|
+
unavailable,
|
|
7
|
+
identityInvalid,
|
|
8
|
+
debugIdentity,
|
|
9
|
+
acquireApplicationCredential,
|
|
10
|
+
application,
|
|
11
|
+
authority,
|
|
12
|
+
}) {
|
|
13
|
+
return async function ensureWelcomeIdentity() {
|
|
14
|
+
let identity;
|
|
15
|
+
try {
|
|
16
|
+
identity = await readIdentity();
|
|
17
|
+
} catch (error) {
|
|
18
|
+
if (error?.code !== "TLDR_EMAIL_IDENTITY_INVALID") throw error;
|
|
19
|
+
debugIdentity("identity_integrity_failed", { code: error.code });
|
|
20
|
+
return identityInvalid();
|
|
21
|
+
}
|
|
22
|
+
if (!identity) return unavailable();
|
|
23
|
+
|
|
24
|
+
if (!identity.welcome) {
|
|
25
|
+
const acquisition = await acquireApplicationCredential(
|
|
26
|
+
application,
|
|
27
|
+
"welcome_sender",
|
|
28
|
+
);
|
|
29
|
+
const created = acquisition.result;
|
|
30
|
+
if (
|
|
31
|
+
commandFailure(created) ||
|
|
32
|
+
typeof created.result?.app_id !== "string" ||
|
|
33
|
+
typeof created.result?.app_secret !== "string"
|
|
34
|
+
) {
|
|
35
|
+
if (acquisition.reregistered && created?.ok === false) return created;
|
|
36
|
+
return unavailable();
|
|
37
|
+
}
|
|
38
|
+
identity = {
|
|
39
|
+
...identity,
|
|
40
|
+
welcome: {
|
|
41
|
+
app_id: created.result.app_id,
|
|
42
|
+
app_secret: created.result.app_secret,
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
await persistIdentity(identity);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const grants = await execute({
|
|
49
|
+
admin: true,
|
|
50
|
+
credentials: null,
|
|
51
|
+
args: [
|
|
52
|
+
"app",
|
|
53
|
+
"set-permissions",
|
|
54
|
+
identity.welcome.app_id,
|
|
55
|
+
"--permissions-json",
|
|
56
|
+
JSON.stringify([
|
|
57
|
+
{
|
|
58
|
+
permission: "register_endpoints",
|
|
59
|
+
allowed_authorities: [authority],
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
permission: "send_as_principal",
|
|
63
|
+
allowed_authorities: [authority],
|
|
64
|
+
},
|
|
65
|
+
]),
|
|
66
|
+
],
|
|
67
|
+
});
|
|
68
|
+
if (commandFailure(grants)) return unavailable();
|
|
69
|
+
debugIdentity("welcome_sender_ready", { application });
|
|
70
|
+
return Object.freeze({ ok: true, identity: identity.welcome });
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
1
74
|
export function createTightbeamWelcomeDispatcher({
|
|
2
75
|
registerEmailChannel,
|
|
76
|
+
ensureWelcomeIdentity,
|
|
3
77
|
readIdentity,
|
|
4
78
|
execute,
|
|
5
79
|
commandFailure,
|
|
6
80
|
unavailable,
|
|
7
81
|
authority,
|
|
8
82
|
principalRef,
|
|
9
|
-
|
|
83
|
+
welcomeEndpointSession,
|
|
10
84
|
}) {
|
|
11
|
-
return async function dispatchWelcome({
|
|
12
|
-
|
|
85
|
+
return async function dispatchWelcome({
|
|
86
|
+
subject,
|
|
87
|
+
reason,
|
|
88
|
+
body,
|
|
89
|
+
idempotencyKey,
|
|
90
|
+
}) {
|
|
91
|
+
if (
|
|
92
|
+
typeof subject !== "string" ||
|
|
93
|
+
subject.trim().length === 0 ||
|
|
94
|
+
typeof reason !== "string" ||
|
|
95
|
+
reason.trim().length === 0 ||
|
|
96
|
+
typeof body !== "string" ||
|
|
97
|
+
typeof idempotencyKey !== "string"
|
|
98
|
+
) {
|
|
13
99
|
return Object.freeze({ ok: false, error: { code: "welcome_invalid" } });
|
|
14
100
|
}
|
|
15
101
|
const registered = await registerEmailChannel({
|
|
@@ -18,11 +104,13 @@ export function createTightbeamWelcomeDispatcher({
|
|
|
18
104
|
capabilities: ["send", "reply"],
|
|
19
105
|
});
|
|
20
106
|
if (registered.ok !== true) return registered;
|
|
21
|
-
const
|
|
22
|
-
if (
|
|
107
|
+
const welcomeIdentity = await ensureWelcomeIdentity();
|
|
108
|
+
if (welcomeIdentity.ok !== true) return welcomeIdentity;
|
|
109
|
+
const bridgeIdentity = await readIdentity();
|
|
110
|
+
if (!bridgeIdentity) return unavailable();
|
|
23
111
|
const owner = await execute({
|
|
24
112
|
admin: false,
|
|
25
|
-
credentials:
|
|
113
|
+
credentials: bridgeIdentity,
|
|
26
114
|
args: [
|
|
27
115
|
"principal",
|
|
28
116
|
"register",
|
|
@@ -34,27 +122,9 @@ export function createTightbeamWelcomeDispatcher({
|
|
|
34
122
|
"Owner",
|
|
35
123
|
],
|
|
36
124
|
});
|
|
37
|
-
const endpoint = await execute({
|
|
38
|
-
admin: false,
|
|
39
|
-
credentials: identity,
|
|
40
|
-
args: [
|
|
41
|
-
"endpoint",
|
|
42
|
-
"register",
|
|
43
|
-
"--authority",
|
|
44
|
-
authority,
|
|
45
|
-
"--principal",
|
|
46
|
-
owner.result?.principal_id,
|
|
47
|
-
"--runtime",
|
|
48
|
-
"reference",
|
|
49
|
-
"--session",
|
|
50
|
-
endpointSession,
|
|
51
|
-
"--reference",
|
|
52
|
-
authority,
|
|
53
|
-
],
|
|
54
|
-
});
|
|
55
125
|
const routes = await execute({
|
|
56
126
|
admin: false,
|
|
57
|
-
credentials:
|
|
127
|
+
credentials: bridgeIdentity,
|
|
58
128
|
args: ["channel", "route", "list"],
|
|
59
129
|
});
|
|
60
130
|
const route = routes?.result?.routes?.find(
|
|
@@ -62,16 +132,15 @@ export function createTightbeamWelcomeDispatcher({
|
|
|
62
132
|
);
|
|
63
133
|
if (
|
|
64
134
|
commandFailure(owner) ||
|
|
65
|
-
commandFailure(endpoint) ||
|
|
66
135
|
commandFailure(routes) ||
|
|
67
|
-
typeof
|
|
136
|
+
typeof owner.result?.principal_id !== "string" ||
|
|
68
137
|
!route
|
|
69
138
|
) {
|
|
70
139
|
return unavailable();
|
|
71
140
|
}
|
|
72
141
|
const welcome = await execute({
|
|
73
142
|
admin: false,
|
|
74
|
-
credentials: identity,
|
|
143
|
+
credentials: welcomeIdentity.identity,
|
|
75
144
|
args: [
|
|
76
145
|
"principal",
|
|
77
146
|
"register",
|
|
@@ -89,14 +158,43 @@ export function createTightbeamWelcomeDispatcher({
|
|
|
89
158
|
) {
|
|
90
159
|
return unavailable();
|
|
91
160
|
}
|
|
161
|
+
const endpoint = await execute({
|
|
162
|
+
admin: false,
|
|
163
|
+
credentials: welcomeIdentity.identity,
|
|
164
|
+
args: [
|
|
165
|
+
"endpoint",
|
|
166
|
+
"register",
|
|
167
|
+
"--authority",
|
|
168
|
+
authority,
|
|
169
|
+
"--principal",
|
|
170
|
+
welcome.result.principal_id,
|
|
171
|
+
"--runtime",
|
|
172
|
+
"reference",
|
|
173
|
+
"--session",
|
|
174
|
+
welcomeEndpointSession,
|
|
175
|
+
"--reference",
|
|
176
|
+
authority,
|
|
177
|
+
],
|
|
178
|
+
});
|
|
179
|
+
if (
|
|
180
|
+
commandFailure(endpoint) ||
|
|
181
|
+
typeof endpoint.result?.endpoint_id !== "string" ||
|
|
182
|
+
!Number.isInteger(endpoint.result?.process_generation)
|
|
183
|
+
) {
|
|
184
|
+
return unavailable();
|
|
185
|
+
}
|
|
92
186
|
const conversation = await execute({
|
|
93
187
|
admin: false,
|
|
94
|
-
credentials: identity,
|
|
188
|
+
credentials: welcomeIdentity.identity,
|
|
95
189
|
args: [
|
|
96
190
|
"conversation",
|
|
97
191
|
"create",
|
|
98
192
|
"--participants",
|
|
99
193
|
`${owner.result.principal_id},${welcome.result.principal_id}`,
|
|
194
|
+
"--subject",
|
|
195
|
+
subject,
|
|
196
|
+
"--reason",
|
|
197
|
+
reason,
|
|
100
198
|
],
|
|
101
199
|
});
|
|
102
200
|
if (
|
|
@@ -107,20 +205,18 @@ export function createTightbeamWelcomeDispatcher({
|
|
|
107
205
|
}
|
|
108
206
|
const committed = await execute({
|
|
109
207
|
admin: false,
|
|
110
|
-
credentials: identity,
|
|
208
|
+
credentials: welcomeIdentity.identity,
|
|
111
209
|
args: [
|
|
112
210
|
"message",
|
|
113
211
|
"commit",
|
|
114
212
|
"--conversation",
|
|
115
213
|
conversation.result.conversation_id,
|
|
116
214
|
"--from",
|
|
117
|
-
|
|
215
|
+
welcome.result.principal_id,
|
|
118
216
|
"--body",
|
|
119
217
|
body,
|
|
120
218
|
"--key",
|
|
121
219
|
idempotencyKey,
|
|
122
|
-
"--origin-channel-route",
|
|
123
|
-
route.route_id,
|
|
124
220
|
"--channel-selector",
|
|
125
221
|
"email",
|
|
126
222
|
"--effect",
|
|
@@ -317,7 +317,7 @@ export async function waitForTldrAgentHealth({
|
|
|
317
317
|
observeDaemon = null,
|
|
318
318
|
home,
|
|
319
319
|
port,
|
|
320
|
-
timeoutMs =
|
|
320
|
+
timeoutMs = 30_000,
|
|
321
321
|
requirePollFresh = true,
|
|
322
322
|
fetchImpl = globalThis.fetch,
|
|
323
323
|
now = Date.now,
|
|
@@ -356,6 +356,7 @@ export async function waitForTldrAgentHealth({
|
|
|
356
356
|
"package_version",
|
|
357
357
|
"source_revision",
|
|
358
358
|
"source_dirty",
|
|
359
|
+
"source_digest",
|
|
359
360
|
"schema_version",
|
|
360
361
|
].every(
|
|
361
362
|
(key) => body?.runtime?.[key] === expectedRuntimeIdentity?.[key],
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
export const DEFAULT_TLDR_AGENT_PHASE_TIMEOUT_MS = 5000;
|
|
2
2
|
export const DEFAULT_TLDR_AGENT_CHILD_GRACE_MS = 5000;
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
"comms_dispatcher",
|
|
7
|
-
]);
|
|
3
|
+
// Tightbeam owns reply obligations and dispatch. The tldr; daemon retains only
|
|
4
|
+
// the provider-to-channel bridge poll.
|
|
5
|
+
export const TLDR_AGENT_MAIL_PHASE_SEQUENCE = Object.freeze(["inbox_poll"]);
|
|
8
6
|
|
|
9
7
|
function numericTimeout(value, fallback) {
|
|
10
8
|
const timeout = Number(value);
|