@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
|
@@ -1,25 +1,34 @@
|
|
|
1
1
|
import { execFile } from "node:child_process";
|
|
2
|
-
import {
|
|
3
|
-
import { chmod, mkdir, readFile, writeFile } from "node:fs/promises";
|
|
2
|
+
import { lstat, mkdir, readFile } from "node:fs/promises";
|
|
4
3
|
import { join } from "node:path";
|
|
5
4
|
import { promisify } from "node:util";
|
|
5
|
+
import { writeTextAtomic } from "./durable_file_io.mjs";
|
|
6
6
|
|
|
7
7
|
const execFileAsync = promisify(execFile);
|
|
8
8
|
|
|
9
|
-
export const
|
|
10
|
-
package: "tightbeam",
|
|
11
|
-
version: "0.2.0",
|
|
9
|
+
export const TIGHTBEAM_COMPATIBILITY = Object.freeze({
|
|
12
10
|
minProtocol: "1.0",
|
|
13
11
|
maxProtocol: "1",
|
|
14
|
-
|
|
15
|
-
capability: "channels.v1",
|
|
12
|
+
requiredCapabilities: Object.freeze(["channels.v1"]),
|
|
16
13
|
});
|
|
17
14
|
export const AUTHORITY = "tldr-email";
|
|
18
15
|
export const APPLICATION = "tldr-email";
|
|
16
|
+
export const WELCOME_APPLICATION = "tldr-welcome";
|
|
19
17
|
export const PRINCIPAL_REF = "verified-owner";
|
|
20
18
|
export const ENDPOINT_SESSION = "tldr-email-owner";
|
|
19
|
+
export const WELCOME_ENDPOINT_SESSION = "tldr-welcome-sender";
|
|
21
20
|
|
|
22
|
-
export function unavailable() {
|
|
21
|
+
export function unavailable(failedDimensions = []) {
|
|
22
|
+
const dimensions = [
|
|
23
|
+
...new Set(
|
|
24
|
+
failedDimensions.filter(
|
|
25
|
+
(dimension) => typeof dimension === "string" && dimension.length > 0,
|
|
26
|
+
),
|
|
27
|
+
),
|
|
28
|
+
];
|
|
29
|
+
const diagnostic = dimensions.length
|
|
30
|
+
? ` Failed compatibility dimension${dimensions.length === 1 ? "" : "s"}: ${dimensions.join(", ")}.`
|
|
31
|
+
: "";
|
|
23
32
|
return Object.freeze({
|
|
24
33
|
ok: false,
|
|
25
34
|
data: null,
|
|
@@ -27,12 +36,51 @@ export function unavailable() {
|
|
|
27
36
|
code: "TIGHTBEAM_INCOMPATIBLE",
|
|
28
37
|
message: "tldr; requires a compatible Tightbeam installation.",
|
|
29
38
|
retryable: false,
|
|
39
|
+
remediation: `Install Tightbeam with supported protocol ${TIGHTBEAM_COMPATIBILITY.minProtocol} through ${TIGHTBEAM_COMPATIBILITY.maxProtocol} and required capability ${TIGHTBEAM_COMPATIBILITY.requiredCapabilities.join(", ")}, then rerun setup.${diagnostic}`,
|
|
40
|
+
}),
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function launcherUnavailable() {
|
|
45
|
+
return Object.freeze({
|
|
46
|
+
ok: false,
|
|
47
|
+
data: null,
|
|
48
|
+
error: Object.freeze({
|
|
49
|
+
code: "TIGHTBEAM_UNAVAILABLE",
|
|
50
|
+
message: "tldr; cannot launch the activated Tightbeam installation.",
|
|
51
|
+
retryable: false,
|
|
30
52
|
remediation:
|
|
31
|
-
"
|
|
53
|
+
"Activate Tightbeam, approve plugin trust, or start a new session, then rerun setup.",
|
|
32
54
|
}),
|
|
33
55
|
});
|
|
34
56
|
}
|
|
35
57
|
|
|
58
|
+
export function identityInvalid() {
|
|
59
|
+
return Object.freeze({
|
|
60
|
+
ok: false,
|
|
61
|
+
data: null,
|
|
62
|
+
error: Object.freeze({
|
|
63
|
+
code: "TLDR_EMAIL_IDENTITY_INVALID",
|
|
64
|
+
message: "tldr; cannot trust its local Tightbeam email identity.",
|
|
65
|
+
retryable: false,
|
|
66
|
+
remediation: "Repair tldr;",
|
|
67
|
+
}),
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function invalidIdentityState() {
|
|
72
|
+
const error = new Error("TLDR Tightbeam email identity is invalid");
|
|
73
|
+
error.code = "TLDR_EMAIL_IDENTITY_INVALID";
|
|
74
|
+
return error;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function debugIdentity(stage, data = {}) {
|
|
78
|
+
if (process.env.TLDR_AGENT_DEBUG_TIGHTBEAM_IDENTITY !== "1") return;
|
|
79
|
+
process.stderr.write(
|
|
80
|
+
`[🪳 TEMP tldr-enrollment-state-recovery] ${stage} ${JSON.stringify(data)}\n`,
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
36
84
|
export function commandFailure(result) {
|
|
37
85
|
return result?.ok !== true || result?.result?.compatible === false;
|
|
38
86
|
}
|
|
@@ -41,20 +89,41 @@ export function defaultIdentityStore(home) {
|
|
|
41
89
|
const path = join(home, "tightbeam-email-identity.json");
|
|
42
90
|
return Object.freeze({
|
|
43
91
|
async load() {
|
|
92
|
+
let metadata;
|
|
93
|
+
try {
|
|
94
|
+
metadata = await lstat(path);
|
|
95
|
+
} catch (error) {
|
|
96
|
+
if (error?.code === "ENOENT") return null;
|
|
97
|
+
throw invalidIdentityState();
|
|
98
|
+
}
|
|
99
|
+
if (!metadata.isFile()) throw invalidIdentityState();
|
|
100
|
+
let identity;
|
|
44
101
|
try {
|
|
45
|
-
|
|
46
|
-
return typeof identity?.app_id === "string" &&
|
|
47
|
-
typeof identity?.app_secret === "string"
|
|
48
|
-
? identity
|
|
49
|
-
: null;
|
|
102
|
+
identity = JSON.parse(await readFile(path, "utf8"));
|
|
50
103
|
} catch {
|
|
51
|
-
|
|
104
|
+
throw invalidIdentityState();
|
|
52
105
|
}
|
|
106
|
+
if (
|
|
107
|
+
typeof identity?.app_id !== "string" ||
|
|
108
|
+
identity.app_id.length === 0 ||
|
|
109
|
+
typeof identity?.app_secret !== "string" ||
|
|
110
|
+
identity.app_secret.length === 0 ||
|
|
111
|
+
(identity.welcome !== undefined &&
|
|
112
|
+
(typeof identity.welcome?.app_id !== "string" ||
|
|
113
|
+
identity.welcome.app_id.length === 0 ||
|
|
114
|
+
typeof identity.welcome?.app_secret !== "string" ||
|
|
115
|
+
identity.welcome.app_secret.length === 0))
|
|
116
|
+
) {
|
|
117
|
+
throw invalidIdentityState();
|
|
118
|
+
}
|
|
119
|
+
return identity;
|
|
53
120
|
},
|
|
54
121
|
async save(identity) {
|
|
55
122
|
await mkdir(home, { recursive: true, mode: 0o700 });
|
|
56
|
-
|
|
57
|
-
|
|
123
|
+
writeTextAtomic(path, `${JSON.stringify(identity)}\n`, {
|
|
124
|
+
durable: true,
|
|
125
|
+
mode: 0o600,
|
|
126
|
+
});
|
|
58
127
|
},
|
|
59
128
|
});
|
|
60
129
|
}
|
|
@@ -77,6 +146,23 @@ export async function productionRun({ command, admin, credentials, args }) {
|
|
|
77
146
|
return JSON.parse(stdout);
|
|
78
147
|
} catch (error) {
|
|
79
148
|
const output = `${error?.stderr ?? ""}\n${error?.stdout ?? ""}`;
|
|
149
|
+
const registeredApplication = args[2];
|
|
150
|
+
if (
|
|
151
|
+
admin &&
|
|
152
|
+
args[0] === "app" &&
|
|
153
|
+
args[1] === "register" &&
|
|
154
|
+
[APPLICATION, WELCOME_APPLICATION].includes(registeredApplication) &&
|
|
155
|
+
/identity_conflict/.test(output) &&
|
|
156
|
+
output.includes(
|
|
157
|
+
`application named "${registeredApplication}" is already registered`,
|
|
158
|
+
)
|
|
159
|
+
) {
|
|
160
|
+
return {
|
|
161
|
+
ok: false,
|
|
162
|
+
data: null,
|
|
163
|
+
error: { code: "TIGHTBEAM_APPLICATION_IDENTITY_CONFLICT" },
|
|
164
|
+
};
|
|
165
|
+
}
|
|
80
166
|
if (
|
|
81
167
|
admin &&
|
|
82
168
|
args[0] === "authority" &&
|
|
@@ -87,7 +173,28 @@ export async function productionRun({ command, admin, credentials, args }) {
|
|
|
87
173
|
) {
|
|
88
174
|
return { ok: true, result: { already_registered: true } };
|
|
89
175
|
}
|
|
90
|
-
|
|
176
|
+
if (typeof error?.stdout === "string" && error.stdout.trim().length > 0) {
|
|
177
|
+
try {
|
|
178
|
+
return JSON.parse(error.stdout);
|
|
179
|
+
} catch {
|
|
180
|
+
// A launched command that emits malformed data is not a missing launcher.
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
if (
|
|
184
|
+
["ENOENT", "EACCES", "EPERM"].includes(error?.code) ||
|
|
185
|
+
(error?.errno && !error?.stdout)
|
|
186
|
+
)
|
|
187
|
+
return null;
|
|
188
|
+
return {
|
|
189
|
+
ok: false,
|
|
190
|
+
data: null,
|
|
191
|
+
error: {
|
|
192
|
+
code: "TIGHTBEAM_OPERATION_FAILED",
|
|
193
|
+
message:
|
|
194
|
+
"tldr; launched Tightbeam but it did not return a valid response.",
|
|
195
|
+
retryable: false,
|
|
196
|
+
},
|
|
197
|
+
};
|
|
91
198
|
}
|
|
92
199
|
}
|
|
93
200
|
|
|
@@ -95,21 +202,12 @@ export function expectedPreflightArgs() {
|
|
|
95
202
|
return [
|
|
96
203
|
"preflight",
|
|
97
204
|
"--min-protocol",
|
|
98
|
-
|
|
205
|
+
TIGHTBEAM_COMPATIBILITY.minProtocol,
|
|
99
206
|
"--max-protocol",
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
"--expect-daemon-version",
|
|
106
|
-
TIGHTBEAM_CONTRACT.version,
|
|
207
|
+
TIGHTBEAM_COMPATIBILITY.maxProtocol,
|
|
208
|
+
...TIGHTBEAM_COMPATIBILITY.requiredCapabilities.flatMap((capability) => [
|
|
209
|
+
"--require-capability",
|
|
210
|
+
capability,
|
|
211
|
+
]),
|
|
107
212
|
];
|
|
108
213
|
}
|
|
109
|
-
|
|
110
|
-
export function inboundKey(providerMessageId) {
|
|
111
|
-
return `tldr-email-inbound-${createHash("sha256")
|
|
112
|
-
.update(providerMessageId)
|
|
113
|
-
.digest("hex")
|
|
114
|
-
.slice(0, 32)}`;
|
|
115
|
-
}
|
|
@@ -44,6 +44,7 @@ export function createTightbeamEmailAdapter({ channel, send } = {}) {
|
|
|
44
44
|
async function deliverOnce({
|
|
45
45
|
sendContext = {},
|
|
46
46
|
afterProviderAccepted,
|
|
47
|
+
afterReplyBindingRecorded,
|
|
47
48
|
afterProviderRecorded,
|
|
48
49
|
} = {}) {
|
|
49
50
|
const claimed = await channel.claimEmailDelivery();
|
|
@@ -52,6 +53,15 @@ export function createTightbeamEmailAdapter({ channel, send } = {}) {
|
|
|
52
53
|
if (!validClaim(claim)) {
|
|
53
54
|
throw new TypeError("Tightbeam returned an invalid claimed delivery");
|
|
54
55
|
}
|
|
56
|
+
if (typeof claim.reply_binding === "string") {
|
|
57
|
+
if (typeof channel.recordReplyBinding !== "function") {
|
|
58
|
+
throw new TypeError(
|
|
59
|
+
"Tightbeam email channel cannot persist reply bindings",
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
await channel.recordReplyBinding({ delivery: claim });
|
|
63
|
+
await afterReplyBindingRecorded?.({ claim });
|
|
64
|
+
}
|
|
55
65
|
const accepted = await channel.findProviderAcceptanceByDelivery?.(
|
|
56
66
|
claim.delivery_id,
|
|
57
67
|
);
|
|
@@ -1,124 +1,15 @@
|
|
|
1
|
-
import "./
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { expectedPreflightArgs } from "./tightbeam_channel_runtime.mjs";
|
|
2
|
+
import {
|
|
3
|
+
lookupProviderMap,
|
|
4
|
+
lookupProviderReplyBinding,
|
|
5
|
+
recordBoundProviderAcceptance,
|
|
6
|
+
recordInboundProviderAcceptance as persistInboundProviderAcceptance,
|
|
7
|
+
recordProviderMap,
|
|
8
|
+
recordProviderReplyBinding,
|
|
9
|
+
} from "./tightbeam_email_provider_map.mjs";
|
|
6
10
|
|
|
7
|
-
|
|
8
|
-
|
|
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 INDEX IF NOT EXISTS provider_acceptance_conversation
|
|
37
|
-
ON provider_acceptance(conversation_id);
|
|
38
|
-
CREATE INDEX IF NOT EXISTS provider_acceptance_thread
|
|
39
|
-
ON provider_acceptance(external_thread_id);`);
|
|
40
|
-
const columns = new Set(
|
|
41
|
-
db
|
|
42
|
-
.prepare("PRAGMA table_info(provider_acceptance)")
|
|
43
|
-
.all()
|
|
44
|
-
.map(({ name }) => name),
|
|
45
|
-
);
|
|
46
|
-
if (!columns.has("origin_endpoint_id")) {
|
|
47
|
-
db.exec(
|
|
48
|
-
"ALTER TABLE provider_acceptance ADD COLUMN origin_endpoint_id TEXT",
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
if (!columns.has("origin_session_id")) {
|
|
52
|
-
db.exec(
|
|
53
|
-
"ALTER TABLE provider_acceptance ADD COLUMN origin_session_id TEXT",
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
return db;
|
|
58
|
-
} catch (error) {
|
|
59
|
-
db.close();
|
|
60
|
-
throw error;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
async function recordProviderMap(home, record) {
|
|
65
|
-
await mkdir(home, { recursive: true, mode: 0o700 });
|
|
66
|
-
const db = openProviderMap(home);
|
|
67
|
-
try {
|
|
68
|
-
db.exec("BEGIN IMMEDIATE");
|
|
69
|
-
db.prepare(
|
|
70
|
-
`INSERT INTO provider_acceptance (
|
|
71
|
-
delivery_id, message_id, conversation_id, route_id, external_id, external_thread_id
|
|
72
|
-
, origin_endpoint_id, origin_session_id
|
|
73
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
74
|
-
ON CONFLICT(delivery_id) DO UPDATE SET
|
|
75
|
-
message_id = excluded.message_id,
|
|
76
|
-
conversation_id = excluded.conversation_id,
|
|
77
|
-
route_id = excluded.route_id,
|
|
78
|
-
origin_endpoint_id = excluded.origin_endpoint_id,
|
|
79
|
-
origin_session_id = excluded.origin_session_id,
|
|
80
|
-
external_id = excluded.external_id,
|
|
81
|
-
external_thread_id = excluded.external_thread_id`,
|
|
82
|
-
).run(
|
|
83
|
-
record.delivery_id,
|
|
84
|
-
record.message_id,
|
|
85
|
-
record.conversation_id,
|
|
86
|
-
record.route_id,
|
|
87
|
-
record.external_id,
|
|
88
|
-
record.external_thread_id,
|
|
89
|
-
record.origin_endpoint_id,
|
|
90
|
-
record.origin_session_id,
|
|
91
|
-
);
|
|
92
|
-
db.exec("COMMIT");
|
|
93
|
-
} catch (error) {
|
|
94
|
-
try {
|
|
95
|
-
db.exec("ROLLBACK");
|
|
96
|
-
} catch {}
|
|
97
|
-
throw error;
|
|
98
|
-
} finally {
|
|
99
|
-
db.close();
|
|
100
|
-
}
|
|
101
|
-
await chmod(providerMapPath(home), 0o600);
|
|
102
|
-
secureProviderMapFiles(providerMapPath(home));
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
function lookupProviderMap(home, column, value) {
|
|
106
|
-
const db = openProviderMap(home, { readOnly: true });
|
|
107
|
-
if (!db) return null;
|
|
108
|
-
try {
|
|
109
|
-
const latest =
|
|
110
|
-
column === "delivery_id" ? "" : " ORDER BY rowid DESC LIMIT 1";
|
|
111
|
-
return (
|
|
112
|
-
db
|
|
113
|
-
.prepare(
|
|
114
|
-
`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}`,
|
|
115
|
-
)
|
|
116
|
-
.get(value) || null
|
|
117
|
-
);
|
|
118
|
-
} finally {
|
|
119
|
-
db.close();
|
|
120
|
-
}
|
|
121
|
-
}
|
|
11
|
+
const REPLY_BINDING_CAPABILITY = "channels.reply-binding.v1";
|
|
12
|
+
const LISTENER_CAPABILITY = "listener.v1";
|
|
122
13
|
|
|
123
14
|
export function createTightbeamEmailDeliveryChannel({
|
|
124
15
|
execute,
|
|
@@ -129,6 +20,25 @@ export function createTightbeamEmailDeliveryChannel({
|
|
|
129
20
|
principalRef,
|
|
130
21
|
endpointSession,
|
|
131
22
|
} = {}) {
|
|
23
|
+
let capabilities;
|
|
24
|
+
|
|
25
|
+
async function deliveryCapabilities() {
|
|
26
|
+
if (capabilities) return capabilities;
|
|
27
|
+
const supported = async (capability) => {
|
|
28
|
+
const result = await execute({
|
|
29
|
+
admin: true,
|
|
30
|
+
credentials: null,
|
|
31
|
+
args: [...expectedPreflightArgs(), "--require-capability", capability],
|
|
32
|
+
});
|
|
33
|
+
return result?.ok === true && result.result?.compatible === true;
|
|
34
|
+
};
|
|
35
|
+
capabilities = Object.freeze({
|
|
36
|
+
replyBinding: await supported(REPLY_BINDING_CAPABILITY),
|
|
37
|
+
listener: await supported(LISTENER_CAPABILITY),
|
|
38
|
+
});
|
|
39
|
+
return capabilities;
|
|
40
|
+
}
|
|
41
|
+
|
|
132
42
|
async function registeredEmailIdentity() {
|
|
133
43
|
const identity = await readIdentity();
|
|
134
44
|
if (!identity) return null;
|
|
@@ -192,6 +102,7 @@ export function createTightbeamEmailDeliveryChannel({
|
|
|
192
102
|
if (commandFailure(routes) || !route) {
|
|
193
103
|
return Object.freeze({ ok: false, code: "route_unavailable" });
|
|
194
104
|
}
|
|
105
|
+
const capability = await deliveryCapabilities();
|
|
195
106
|
const claimed = await execute({
|
|
196
107
|
admin: false,
|
|
197
108
|
credentials: emailIdentity.identity,
|
|
@@ -202,6 +113,7 @@ export function createTightbeamEmailDeliveryChannel({
|
|
|
202
113
|
emailIdentity.endpoint.endpoint_id,
|
|
203
114
|
"--channel-route",
|
|
204
115
|
route.route_id,
|
|
116
|
+
...(capability.replyBinding ? ["--accept-reply-binding"] : []),
|
|
205
117
|
],
|
|
206
118
|
});
|
|
207
119
|
return commandFailure(claimed)
|
|
@@ -235,6 +147,10 @@ export function createTightbeamEmailDeliveryChannel({
|
|
|
235
147
|
}
|
|
236
148
|
|
|
237
149
|
async function recordProviderAcceptance({ delivery, provider }) {
|
|
150
|
+
if (typeof delivery?.reply_binding === "string") {
|
|
151
|
+
await recordBoundProviderAcceptance(home, delivery, provider);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
238
154
|
const identity = await readIdentity();
|
|
239
155
|
if (!identity) throw new Error("Tightbeam email identity is unavailable");
|
|
240
156
|
const routes = await execute({
|
|
@@ -265,26 +181,67 @@ export function createTightbeamEmailDeliveryChannel({
|
|
|
265
181
|
});
|
|
266
182
|
}
|
|
267
183
|
|
|
184
|
+
async function recordReplyBinding({ delivery }) {
|
|
185
|
+
if (
|
|
186
|
+
typeof delivery?.delivery_id !== "string" ||
|
|
187
|
+
typeof delivery.reply_binding !== "string" ||
|
|
188
|
+
delivery.reply_binding.length === 0
|
|
189
|
+
) {
|
|
190
|
+
throw new TypeError(
|
|
191
|
+
"Tightbeam claimed delivery has no opaque reply binding",
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
await recordProviderReplyBinding(home, delivery);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
async function recordInboundProviderAcceptance({
|
|
198
|
+
binding,
|
|
199
|
+
provider_message_id: providerMessageId,
|
|
200
|
+
provider_thread_id: providerThreadId,
|
|
201
|
+
tightbeam_message_id: tightbeamMessageId,
|
|
202
|
+
tightbeam_conversation_id: tightbeamConversationId,
|
|
203
|
+
}) {
|
|
204
|
+
if (
|
|
205
|
+
typeof binding?.reply_binding !== "string" ||
|
|
206
|
+
binding.reply_binding.length === 0 ||
|
|
207
|
+
typeof providerMessageId !== "string" ||
|
|
208
|
+
providerMessageId.length === 0 ||
|
|
209
|
+
(providerThreadId !== undefined &&
|
|
210
|
+
(typeof providerThreadId !== "string" ||
|
|
211
|
+
providerThreadId.length === 0)) ||
|
|
212
|
+
typeof tightbeamMessageId !== "string" ||
|
|
213
|
+
tightbeamMessageId.length === 0
|
|
214
|
+
) {
|
|
215
|
+
throw new TypeError("Accepted inbound provider identity is incomplete");
|
|
216
|
+
}
|
|
217
|
+
await persistInboundProviderAcceptance(home, {
|
|
218
|
+
reply_binding: binding.reply_binding,
|
|
219
|
+
provider_message_id: providerMessageId,
|
|
220
|
+
provider_thread_id: providerThreadId,
|
|
221
|
+
tightbeam_message_id: tightbeamMessageId,
|
|
222
|
+
tightbeam_conversation_id: tightbeamConversationId,
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
|
|
268
226
|
async function findThreadBinding(externalThreadId) {
|
|
269
|
-
const
|
|
227
|
+
const bound = lookupProviderReplyBinding(
|
|
270
228
|
home,
|
|
271
229
|
"external_thread_id",
|
|
272
230
|
externalThreadId,
|
|
273
231
|
);
|
|
274
|
-
return
|
|
275
|
-
? {
|
|
276
|
-
conversation_id: record.conversation_id,
|
|
277
|
-
route_id: record.route_id,
|
|
278
|
-
...(typeof record.origin_endpoint_id === "string"
|
|
279
|
-
? { origin_endpoint_id: record.origin_endpoint_id }
|
|
280
|
-
: {}),
|
|
281
|
-
...(typeof record.origin_session_id === "string"
|
|
282
|
-
? { origin_session_id: record.origin_session_id }
|
|
283
|
-
: {}),
|
|
284
|
-
}
|
|
285
|
-
: null;
|
|
232
|
+
return bound ? { reply_binding: bound.reply_binding } : null;
|
|
286
233
|
}
|
|
287
234
|
async function findProviderAcceptanceByDelivery(deliveryId) {
|
|
235
|
+
const bound = lookupProviderReplyBinding(home, "delivery_id", deliveryId);
|
|
236
|
+
if (
|
|
237
|
+
typeof bound?.external_id === "string" &&
|
|
238
|
+
typeof bound.external_thread_id === "string"
|
|
239
|
+
) {
|
|
240
|
+
return {
|
|
241
|
+
external_id: bound.external_id,
|
|
242
|
+
external_thread_id: bound.external_thread_id,
|
|
243
|
+
};
|
|
244
|
+
}
|
|
288
245
|
const record = lookupProviderMap(home, "delivery_id", deliveryId);
|
|
289
246
|
return record
|
|
290
247
|
? {
|
|
@@ -294,6 +251,21 @@ export function createTightbeamEmailDeliveryChannel({
|
|
|
294
251
|
: null;
|
|
295
252
|
}
|
|
296
253
|
async function findProviderThreadByConversation(conversationId) {
|
|
254
|
+
const bound = lookupProviderReplyBinding(
|
|
255
|
+
home,
|
|
256
|
+
"conversation_id",
|
|
257
|
+
conversationId,
|
|
258
|
+
);
|
|
259
|
+
if (
|
|
260
|
+
typeof bound?.external_id === "string" &&
|
|
261
|
+
typeof bound.external_thread_id === "string"
|
|
262
|
+
) {
|
|
263
|
+
return {
|
|
264
|
+
external_id:
|
|
265
|
+
bound.current_reply_parent_external_id || bound.external_id,
|
|
266
|
+
external_thread_id: bound.external_thread_id,
|
|
267
|
+
};
|
|
268
|
+
}
|
|
297
269
|
const record = lookupProviderMap(home, "conversation_id", conversationId);
|
|
298
270
|
return record
|
|
299
271
|
? {
|
|
@@ -305,7 +277,9 @@ export function createTightbeamEmailDeliveryChannel({
|
|
|
305
277
|
return Object.freeze({
|
|
306
278
|
claimEmailDelivery,
|
|
307
279
|
completeEmailDelivery,
|
|
280
|
+
recordReplyBinding,
|
|
308
281
|
recordProviderAcceptance,
|
|
282
|
+
recordInboundProviderAcceptance,
|
|
309
283
|
findProviderAcceptanceByDelivery,
|
|
310
284
|
findThreadBinding,
|
|
311
285
|
findProviderThreadByConversation,
|