@daex-dev/sdk 0.0.0-stage → 0.8.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 +478 -2
- package/dist/client-DVZHOtg6.d.cts +2635 -0
- package/dist/client-DVZHOtg6.d.ts +2635 -0
- package/dist/connect/browser.cjs +209 -0
- package/dist/connect/browser.d.cts +81 -0
- package/dist/connect/browser.d.ts +81 -0
- package/dist/connect/browser.js +183 -0
- package/dist/connect/index.cjs +1954 -0
- package/dist/connect/index.d.cts +433 -0
- package/dist/connect/index.d.ts +433 -0
- package/dist/connect/index.js +1888 -0
- package/dist/electron/global.cjs +18 -0
- package/dist/electron/global.d.cts +35 -0
- package/dist/electron/global.d.ts +35 -0
- package/dist/electron/global.js +0 -0
- package/dist/electron/index.cjs +1133 -0
- package/dist/electron/index.d.cts +510 -0
- package/dist/electron/index.d.ts +510 -0
- package/dist/electron/index.js +1073 -0
- package/dist/electron/preload-auto.cjs +38 -0
- package/dist/electron/preload.cjs +66 -0
- package/dist/electron/preload.d.cts +46 -0
- package/dist/electron/preload.d.ts +46 -0
- package/dist/electron/preload.js +43 -0
- package/dist/electron/protocol-DXRtliy3.d.cts +904 -0
- package/dist/electron/protocol-DXRtliy3.d.ts +904 -0
- package/dist/electron/renderer.cjs +631 -0
- package/dist/electron/renderer.d.cts +508 -0
- package/dist/electron/renderer.d.ts +508 -0
- package/dist/electron/renderer.global.js +634 -0
- package/dist/electron/renderer.js +608 -0
- package/dist/index.cjs +6731 -0
- package/dist/index.d.cts +316 -0
- package/dist/index.d.ts +316 -0
- package/dist/index.js +6661 -0
- package/dist/server.cjs +1019 -0
- package/dist/server.d.cts +118 -0
- package/dist/server.d.ts +118 -0
- package/dist/server.js +989 -0
- package/dist/testing.cjs +815 -0
- package/dist/testing.d.cts +29 -0
- package/dist/testing.d.ts +29 -0
- package/dist/testing.js +787 -0
- package/dist/types-DUGv-9QF.d.cts +166 -0
- package/dist/types-DUGv-9QF.d.ts +166 -0
- package/package.json +109 -4
|
@@ -0,0 +1,1073 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var ADE_ERROR_CODES = [
|
|
3
|
+
"binary_not_found",
|
|
4
|
+
"download_failed",
|
|
5
|
+
"checksum_mismatch",
|
|
6
|
+
"spawn_failed",
|
|
7
|
+
"connect_failed",
|
|
8
|
+
"handshake_failed",
|
|
9
|
+
"rpc_error",
|
|
10
|
+
"rpc_timeout",
|
|
11
|
+
"transport_closed",
|
|
12
|
+
"protocol_error",
|
|
13
|
+
"thread_not_found",
|
|
14
|
+
"invalid_option",
|
|
15
|
+
"runtime_unavailable",
|
|
16
|
+
"approval_not_found",
|
|
17
|
+
"unauthorized",
|
|
18
|
+
"disposed",
|
|
19
|
+
"runtime_incompatible",
|
|
20
|
+
"turn_in_flight",
|
|
21
|
+
"unsupported",
|
|
22
|
+
"invalid_output",
|
|
23
|
+
"sign_in_failed"
|
|
24
|
+
];
|
|
25
|
+
var CODE_SET = new Set(ADE_ERROR_CODES);
|
|
26
|
+
var DaexError = class extends Error {
|
|
27
|
+
code;
|
|
28
|
+
cause;
|
|
29
|
+
constructor(code, message, options) {
|
|
30
|
+
super(message);
|
|
31
|
+
this.name = "AdeError";
|
|
32
|
+
this.code = code;
|
|
33
|
+
if (options && "cause" in options) this.cause = options.cause;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
function errorMessage(value) {
|
|
37
|
+
return value instanceof Error ? value.message : String(value);
|
|
38
|
+
}
|
|
39
|
+
var AdeError = DaexError;
|
|
40
|
+
|
|
41
|
+
// src/permissions.ts
|
|
42
|
+
import path from "path";
|
|
43
|
+
|
|
44
|
+
// src/types.ts
|
|
45
|
+
var PENDING_INPUT_KINDS = [
|
|
46
|
+
"approval",
|
|
47
|
+
"question",
|
|
48
|
+
"structured_question",
|
|
49
|
+
"permissions",
|
|
50
|
+
"plan_approval",
|
|
51
|
+
"model_selection"
|
|
52
|
+
];
|
|
53
|
+
|
|
54
|
+
// src/approvals.ts
|
|
55
|
+
var APPROVAL_DECISIONS = [
|
|
56
|
+
"accept",
|
|
57
|
+
"accept_always",
|
|
58
|
+
"reject"
|
|
59
|
+
];
|
|
60
|
+
var PENDING_INPUT_KIND_SET = new Set(PENDING_INPUT_KINDS);
|
|
61
|
+
|
|
62
|
+
// src/clientEvents.ts
|
|
63
|
+
var DAEX_CLIENT_EVENTS = ["exit", "transport", "restart", "threadLifecycle", "usageLimit"];
|
|
64
|
+
|
|
65
|
+
// src/signInRegistry.ts
|
|
66
|
+
var TERMINAL_STATES = /* @__PURE__ */ new Set(["succeeded", "failed", "cancelled", "expired"]);
|
|
67
|
+
function createSignInRegistry(serializeError2) {
|
|
68
|
+
const entries = /* @__PURE__ */ new Map();
|
|
69
|
+
const forget = (loginId, entry) => {
|
|
70
|
+
entries.delete(loginId);
|
|
71
|
+
entry.stop();
|
|
72
|
+
};
|
|
73
|
+
const owned = (loginId, owner) => {
|
|
74
|
+
const entry = entries.get(loginId);
|
|
75
|
+
return entry && entry.owner === owner ? entry : null;
|
|
76
|
+
};
|
|
77
|
+
return {
|
|
78
|
+
/** Track a sign-in the client just started; answers what `signIn` returns across the bridge. */
|
|
79
|
+
async start(owner, handle) {
|
|
80
|
+
let status = {};
|
|
81
|
+
const stop = handle.on("update", (next) => {
|
|
82
|
+
status = next;
|
|
83
|
+
});
|
|
84
|
+
if (!status.loginId) {
|
|
85
|
+
stop();
|
|
86
|
+
await handle.cancel().catch(() => {
|
|
87
|
+
});
|
|
88
|
+
throw new DaexError("protocol_error", "The sign-in did not provide its initial status.");
|
|
89
|
+
}
|
|
90
|
+
entries.set(status.loginId, {
|
|
91
|
+
owner,
|
|
92
|
+
handle,
|
|
93
|
+
get status() {
|
|
94
|
+
return status;
|
|
95
|
+
},
|
|
96
|
+
stop,
|
|
97
|
+
result: handle.done.then((account) => ({ account }), (error) => ({ error: serializeError2(error) }))
|
|
98
|
+
});
|
|
99
|
+
return { loginId: status.loginId, status };
|
|
100
|
+
},
|
|
101
|
+
/** The latest status; once it is terminal, also the outcome, and the entry is released. */
|
|
102
|
+
async status(owner, loginId) {
|
|
103
|
+
const entry = owned(loginId, owner);
|
|
104
|
+
if (!entry) throw new DaexError("invalid_option", `Sign-in "${loginId}" was not found.`);
|
|
105
|
+
if (!TERMINAL_STATES.has(entry.status.state)) return { status: entry.status };
|
|
106
|
+
const result = await entry.result;
|
|
107
|
+
forget(loginId, entry);
|
|
108
|
+
return { status: entry.status, ...result };
|
|
109
|
+
},
|
|
110
|
+
async submitCode(owner, loginId, code) {
|
|
111
|
+
const entry = owned(loginId, owner);
|
|
112
|
+
if (!entry) throw new DaexError("invalid_option", "This sign-in is no longer active.");
|
|
113
|
+
await entry.handle.submitCode(code);
|
|
114
|
+
},
|
|
115
|
+
/** Cancelling a sign-in that is already gone is not an error. */
|
|
116
|
+
async cancel(owner, loginId) {
|
|
117
|
+
const entry = owned(loginId, owner);
|
|
118
|
+
if (!entry) return;
|
|
119
|
+
forget(loginId, entry);
|
|
120
|
+
await entry.handle.cancel();
|
|
121
|
+
},
|
|
122
|
+
/** Cancel every sign-in an owner started: it went away and can no longer finish them. */
|
|
123
|
+
releaseOwner(owner) {
|
|
124
|
+
for (const [loginId, entry] of entries) {
|
|
125
|
+
if (entry.owner !== owner) continue;
|
|
126
|
+
forget(loginId, entry);
|
|
127
|
+
void entry.handle.cancel().catch(() => {
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// src/electron/protocol.ts
|
|
135
|
+
var DAEX_DEFAULT_CHANNEL_PREFIX = "ade";
|
|
136
|
+
var DAEX_DEFAULT_BRIDGE_KEY = "ade";
|
|
137
|
+
function invokeChannel(prefix) {
|
|
138
|
+
return `${prefix}:invoke`;
|
|
139
|
+
}
|
|
140
|
+
function eventChannel(prefix) {
|
|
141
|
+
return `${prefix}:event`;
|
|
142
|
+
}
|
|
143
|
+
function serializeAdeError(error) {
|
|
144
|
+
const candidate = error && typeof error === "object" ? error : null;
|
|
145
|
+
return {
|
|
146
|
+
__adeError: true,
|
|
147
|
+
name: "AdeError",
|
|
148
|
+
code: candidate?.name === "AdeError" && typeof candidate.code === "string" ? candidate.code : "rpc_error",
|
|
149
|
+
message: typeof candidate?.message === "string" ? candidate.message : String(error)
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
var DAEX_IPC_METHODS = [
|
|
153
|
+
"providers.status",
|
|
154
|
+
"providers.refresh",
|
|
155
|
+
"providers.capabilities",
|
|
156
|
+
"providers.subscribe",
|
|
157
|
+
"providers.unsubscribe",
|
|
158
|
+
"models.list",
|
|
159
|
+
"doctor",
|
|
160
|
+
"client.subscribe",
|
|
161
|
+
"client.unsubscribe",
|
|
162
|
+
"threads.open",
|
|
163
|
+
"threads.list",
|
|
164
|
+
"threads.get",
|
|
165
|
+
"threads.delete",
|
|
166
|
+
"threads.archive",
|
|
167
|
+
"threads.unarchive",
|
|
168
|
+
"thread.export",
|
|
169
|
+
"thread.update",
|
|
170
|
+
"thread.retry",
|
|
171
|
+
"thread.editLast",
|
|
172
|
+
"thread.historyPage",
|
|
173
|
+
"thread.send",
|
|
174
|
+
"thread.steer",
|
|
175
|
+
"thread.interrupt",
|
|
176
|
+
"thread.respond",
|
|
177
|
+
"thread.queue.list",
|
|
178
|
+
"thread.queue.cancel",
|
|
179
|
+
"thread.queue.edit",
|
|
180
|
+
"thread.queue.move",
|
|
181
|
+
"thread.queue.dispatch",
|
|
182
|
+
"thread.queue.cancelDispatched",
|
|
183
|
+
"thread.queue.restore",
|
|
184
|
+
"thread.stopTask",
|
|
185
|
+
"thread.slashCommands",
|
|
186
|
+
"thread.recover",
|
|
187
|
+
"thread.resolveUnprocessed",
|
|
188
|
+
"thread.usageLimit.resumeNow",
|
|
189
|
+
"thread.usageLimit.continueOnAlternate",
|
|
190
|
+
"thread.switchAccount",
|
|
191
|
+
"thread.history",
|
|
192
|
+
"thread.setModel",
|
|
193
|
+
"thread.approve",
|
|
194
|
+
"thread.pendingApprovals",
|
|
195
|
+
"accounts.list",
|
|
196
|
+
"accounts.add",
|
|
197
|
+
"accounts.remove",
|
|
198
|
+
"accounts.rename",
|
|
199
|
+
"accounts.setDefault",
|
|
200
|
+
"accounts.signOut",
|
|
201
|
+
"accounts.refresh",
|
|
202
|
+
"accounts.signIn",
|
|
203
|
+
"accounts.signInStatus",
|
|
204
|
+
"accounts.signInSubmitCode",
|
|
205
|
+
"accounts.signInCancel",
|
|
206
|
+
"credentials.list",
|
|
207
|
+
"credentials.add",
|
|
208
|
+
"credentials.remove",
|
|
209
|
+
"credentials.verify",
|
|
210
|
+
"presets.list",
|
|
211
|
+
"presets.add",
|
|
212
|
+
"presets.update",
|
|
213
|
+
"presets.remove",
|
|
214
|
+
"thread.subscribe",
|
|
215
|
+
"thread.unsubscribe"
|
|
216
|
+
];
|
|
217
|
+
var DAEX_IPC_THREAD_KEY_METHODS = /* @__PURE__ */ new Set([
|
|
218
|
+
"threads.open",
|
|
219
|
+
"threads.get",
|
|
220
|
+
"threads.delete",
|
|
221
|
+
"threads.archive",
|
|
222
|
+
"threads.unarchive",
|
|
223
|
+
"thread.export",
|
|
224
|
+
"thread.update",
|
|
225
|
+
"thread.retry",
|
|
226
|
+
"thread.editLast",
|
|
227
|
+
"thread.historyPage",
|
|
228
|
+
"thread.send",
|
|
229
|
+
"thread.steer",
|
|
230
|
+
"thread.interrupt",
|
|
231
|
+
"thread.respond",
|
|
232
|
+
"thread.queue.list",
|
|
233
|
+
"thread.queue.cancel",
|
|
234
|
+
"thread.queue.edit",
|
|
235
|
+
"thread.queue.move",
|
|
236
|
+
"thread.queue.dispatch",
|
|
237
|
+
"thread.queue.cancelDispatched",
|
|
238
|
+
"thread.queue.restore",
|
|
239
|
+
"thread.stopTask",
|
|
240
|
+
"thread.slashCommands",
|
|
241
|
+
"thread.recover",
|
|
242
|
+
"thread.resolveUnprocessed",
|
|
243
|
+
"thread.usageLimit.resumeNow",
|
|
244
|
+
"thread.usageLimit.continueOnAlternate",
|
|
245
|
+
"thread.switchAccount",
|
|
246
|
+
"thread.history",
|
|
247
|
+
"thread.setModel",
|
|
248
|
+
"thread.approve",
|
|
249
|
+
"thread.pendingApprovals",
|
|
250
|
+
"thread.subscribe"
|
|
251
|
+
]);
|
|
252
|
+
function envelopeDedupeKey(envelope, occurrence = 0) {
|
|
253
|
+
const sequence = typeof envelope.sequence === "number" ? String(envelope.sequence) : "?";
|
|
254
|
+
const type = typeof envelope.event?.type === "string" ? envelope.event.type : "?";
|
|
255
|
+
const base = `${envelope.sessionId}:${sequence}:${envelope.timestamp}:${type}`;
|
|
256
|
+
return occurrence > 0 ? `${base}#${occurrence}` : base;
|
|
257
|
+
}
|
|
258
|
+
function keyEnvelopes(envelopes) {
|
|
259
|
+
const counts = /* @__PURE__ */ new Map();
|
|
260
|
+
return envelopes.map((envelope) => {
|
|
261
|
+
const base = envelopeDedupeKey(envelope);
|
|
262
|
+
const occurrence = counts.get(base) ?? 0;
|
|
263
|
+
counts.set(base, occurrence + 1);
|
|
264
|
+
return envelopeDedupeKey(envelope, occurrence);
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
function compareEnvelopes(a, b) {
|
|
268
|
+
const aSeq = typeof a.sequence === "number" ? a.sequence : null;
|
|
269
|
+
const bSeq = typeof b.sequence === "number" ? b.sequence : null;
|
|
270
|
+
if (aSeq !== null && bSeq === null) return -1;
|
|
271
|
+
if (aSeq === null && bSeq !== null) return 1;
|
|
272
|
+
if (aSeq !== null && bSeq !== null) {
|
|
273
|
+
if (aSeq !== bSeq) return aSeq - bSeq;
|
|
274
|
+
return 0;
|
|
275
|
+
}
|
|
276
|
+
if (a.timestamp !== b.timestamp) return a.timestamp < b.timestamp ? -1 : 1;
|
|
277
|
+
return 0;
|
|
278
|
+
}
|
|
279
|
+
function mergeHistoryWithBuffer(history, buffered) {
|
|
280
|
+
const seen = /* @__PURE__ */ new Set();
|
|
281
|
+
const merged = [];
|
|
282
|
+
const keys = [...keyEnvelopes(history), ...keyEnvelopes(buffered)];
|
|
283
|
+
[...history, ...buffered].forEach((envelope, index) => {
|
|
284
|
+
const key = keys[index];
|
|
285
|
+
if (seen.has(key)) return;
|
|
286
|
+
seen.add(key);
|
|
287
|
+
merged.push(envelope);
|
|
288
|
+
});
|
|
289
|
+
return merged.sort(compareEnvelopes);
|
|
290
|
+
}
|
|
291
|
+
var ADE_DEFAULT_BRIDGE_KEY = DAEX_DEFAULT_BRIDGE_KEY;
|
|
292
|
+
var ADE_DEFAULT_CHANNEL_PREFIX = DAEX_DEFAULT_CHANNEL_PREFIX;
|
|
293
|
+
var ADE_IPC_METHODS = DAEX_IPC_METHODS;
|
|
294
|
+
var ADE_IPC_THREAD_KEY_METHODS = DAEX_IPC_THREAD_KEY_METHODS;
|
|
295
|
+
|
|
296
|
+
// src/electron/rendererOptions.ts
|
|
297
|
+
var DAEX_IPC_RENDERER_OPEN_FIELDS = ["provider", "model", "title", "reasoningEffort"];
|
|
298
|
+
function rendererOpenOptions(key, rendererOptions, log) {
|
|
299
|
+
if (!rendererOptions) return null;
|
|
300
|
+
const allowed = new Set(DAEX_IPC_RENDERER_OPEN_FIELDS);
|
|
301
|
+
const filtered = {};
|
|
302
|
+
for (const [field, value] of Object.entries(rendererOptions)) {
|
|
303
|
+
if (value === void 0) continue;
|
|
304
|
+
if (!allowed.has(field)) {
|
|
305
|
+
log(
|
|
306
|
+
`[ade-electron] threads.open "${key}": dropped renderer option "${field}"; only ${DAEX_IPC_RENDERER_OPEN_FIELDS.join(", ")} cross the bridge. Use the openOptions hook to configure threads.`
|
|
307
|
+
);
|
|
308
|
+
continue;
|
|
309
|
+
}
|
|
310
|
+
if (typeof value !== "string") {
|
|
311
|
+
log(`[ade-electron] threads.open "${key}": dropped renderer option "${field}" (not a string)`);
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
filtered[field] = value;
|
|
315
|
+
}
|
|
316
|
+
return Object.keys(filtered).length > 0 ? filtered : null;
|
|
317
|
+
}
|
|
318
|
+
function rendererSendOptions(method, key, raw, log) {
|
|
319
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return {};
|
|
320
|
+
const out = {};
|
|
321
|
+
for (const [field, value] of Object.entries(raw)) {
|
|
322
|
+
if (value === void 0) continue;
|
|
323
|
+
if (field === "displayText" && typeof value === "string") out.displayText = value;
|
|
324
|
+
else if (field === "reasoningEffort" && (typeof value === "string" || value === null)) out.reasoningEffort = value;
|
|
325
|
+
else if (field === "attachments" && Array.isArray(value)) {
|
|
326
|
+
const refs = value.flatMap(rendererFileRef);
|
|
327
|
+
if (value.length === 0 || refs.length > 0) out.attachments = refs;
|
|
328
|
+
else log(`[ade-electron] ${method} "${key}": dropped "attachments": no entry has a usable path`);
|
|
329
|
+
} else {
|
|
330
|
+
log(`[ade-electron] ${method} "${key}": dropped renderer option "${field}"`);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
return out;
|
|
334
|
+
}
|
|
335
|
+
function rendererFileRef(item) {
|
|
336
|
+
if (!item || typeof item !== "object") return [];
|
|
337
|
+
const entry = item;
|
|
338
|
+
if (typeof entry.path !== "string" || !entry.path.trim()) return [];
|
|
339
|
+
const ref = { path: entry.path };
|
|
340
|
+
if (typeof entry.name === "string") ref.name = entry.name;
|
|
341
|
+
if (typeof entry.mimeType === "string") ref.mimeType = entry.mimeType;
|
|
342
|
+
if (typeof entry.bytes === "number" && Number.isFinite(entry.bytes)) ref.bytes = entry.bytes;
|
|
343
|
+
if (entry.type === "file" || entry.type === "image") ref.type = entry.type;
|
|
344
|
+
if (typeof entry.hydrate === "boolean") ref.hydrate = entry.hydrate;
|
|
345
|
+
return [ref];
|
|
346
|
+
}
|
|
347
|
+
var ADE_IPC_RENDERER_OPEN_FIELDS = DAEX_IPC_RENDERER_OPEN_FIELDS;
|
|
348
|
+
|
|
349
|
+
// src/electron/main.ts
|
|
350
|
+
var subscriptionCounter = 0;
|
|
351
|
+
function nextSubscriptionId(prefix) {
|
|
352
|
+
subscriptionCounter += 1;
|
|
353
|
+
return `${prefix}-${subscriptionCounter}`;
|
|
354
|
+
}
|
|
355
|
+
function daexErrorCodeOf(error) {
|
|
356
|
+
if (!error || typeof error !== "object") return null;
|
|
357
|
+
const candidate = error;
|
|
358
|
+
if (candidate.name !== "AdeError") return null;
|
|
359
|
+
return typeof candidate.code === "string" ? candidate.code : null;
|
|
360
|
+
}
|
|
361
|
+
function serializeError(error) {
|
|
362
|
+
return serializeAdeError(error);
|
|
363
|
+
}
|
|
364
|
+
function unauthorized(method) {
|
|
365
|
+
return new DaexError("unauthorized", `The host refused ${method} for this renderer.`);
|
|
366
|
+
}
|
|
367
|
+
function requireString(value, label) {
|
|
368
|
+
if (typeof value !== "string" || value.trim().length === 0) {
|
|
369
|
+
throw new DaexError("invalid_option", `${label} must be a non-empty string.`);
|
|
370
|
+
}
|
|
371
|
+
return value;
|
|
372
|
+
}
|
|
373
|
+
function requireRecord(value, label) {
|
|
374
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
375
|
+
throw new DaexError("invalid_option", `${label} must be an object.`);
|
|
376
|
+
}
|
|
377
|
+
return value;
|
|
378
|
+
}
|
|
379
|
+
var DECISIONS = new Set(APPROVAL_DECISIONS);
|
|
380
|
+
function requireApprovalDecision(value) {
|
|
381
|
+
if (typeof value !== "string" || !DECISIONS.has(value)) {
|
|
382
|
+
throw new DaexError(
|
|
383
|
+
"invalid_option",
|
|
384
|
+
`decision must be one of ${APPROVAL_DECISIONS.join(", ")}; got ${JSON.stringify(value)}.`
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
return value;
|
|
388
|
+
}
|
|
389
|
+
function removeWebContentsListener(webContents, event, listener) {
|
|
390
|
+
if (typeof webContents.removeListener === "function") {
|
|
391
|
+
webContents.removeListener(event, listener);
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
if (typeof webContents.off === "function") webContents.off(event, listener);
|
|
395
|
+
}
|
|
396
|
+
function navigationEndsRendererWorld(args) {
|
|
397
|
+
const [first, second, third, fourth] = args;
|
|
398
|
+
if (first && typeof first === "object") {
|
|
399
|
+
const details = first;
|
|
400
|
+
if ("isSameDocument" in details || "isMainFrame" in details) {
|
|
401
|
+
if (details.isSameDocument === true) return false;
|
|
402
|
+
if (details.isMainFrame === false) return false;
|
|
403
|
+
return true;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
if (typeof second === "string") {
|
|
407
|
+
if (third === true) return false;
|
|
408
|
+
if (fourth === false) return false;
|
|
409
|
+
return true;
|
|
410
|
+
}
|
|
411
|
+
return true;
|
|
412
|
+
}
|
|
413
|
+
function registerDaexIpc(ipcMain, clientSource, opts = {}) {
|
|
414
|
+
let watchedClient = null;
|
|
415
|
+
let stopWatching = null;
|
|
416
|
+
const watchLifecycle = (client) => {
|
|
417
|
+
if (watchedClient === client) return;
|
|
418
|
+
stopWatching?.();
|
|
419
|
+
stopWatching = null;
|
|
420
|
+
watchedClient = client;
|
|
421
|
+
try {
|
|
422
|
+
stopWatching = client.on("threadLifecycle", (payload) => {
|
|
423
|
+
const key = payload?.key;
|
|
424
|
+
const change = payload?.change;
|
|
425
|
+
if (typeof key !== "string") return;
|
|
426
|
+
if (change === "deleted") forgetKeyEverywhere(key);
|
|
427
|
+
if (change === "deleted" || change === "archived") notifyThreadRemoved(key, change);
|
|
428
|
+
});
|
|
429
|
+
} catch (error) {
|
|
430
|
+
log(`[ade-electron] cannot follow thread deletes on this client: ${errorMessage(error)}`);
|
|
431
|
+
}
|
|
432
|
+
};
|
|
433
|
+
const currentClient = () => {
|
|
434
|
+
const client = typeof clientSource === "function" ? clientSource() : clientSource;
|
|
435
|
+
if (!client || typeof client !== "object" || !client.threads) {
|
|
436
|
+
throw new DaexError("disposed", "The host has no DAEX chat client to serve this call.");
|
|
437
|
+
}
|
|
438
|
+
return client;
|
|
439
|
+
};
|
|
440
|
+
const prefix = opts.channelPrefix?.trim() || DAEX_DEFAULT_CHANNEL_PREFIX;
|
|
441
|
+
const log = opts.logger ?? (() => {
|
|
442
|
+
});
|
|
443
|
+
const { allowThreadKey } = opts;
|
|
444
|
+
const renderers = /* @__PURE__ */ new Map();
|
|
445
|
+
const signIns = createSignInRegistry(serializeError);
|
|
446
|
+
let disposed = false;
|
|
447
|
+
function push(webContents, payload) {
|
|
448
|
+
if (webContents.isDestroyed()) return;
|
|
449
|
+
try {
|
|
450
|
+
webContents.send(eventChannel(prefix), payload);
|
|
451
|
+
} catch (error) {
|
|
452
|
+
log(`[ade-electron] push dropped: ${errorMessage(error)}`);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
function disposeRenderer(id) {
|
|
456
|
+
const entry = renderers.get(id);
|
|
457
|
+
if (!entry || entry.disposed) return;
|
|
458
|
+
entry.disposed = true;
|
|
459
|
+
renderers.delete(id);
|
|
460
|
+
for (const subscriptionId of [...entry.subscriptions.keys()]) releaseSubscription(entry, subscriptionId);
|
|
461
|
+
signIns.releaseOwner(id);
|
|
462
|
+
entry.threads.clear();
|
|
463
|
+
entry.opening.clear();
|
|
464
|
+
entry.rendererOptions.clear();
|
|
465
|
+
entry.detach();
|
|
466
|
+
log(`[ade-electron] released renderer ${id}`);
|
|
467
|
+
}
|
|
468
|
+
function rendererFor(event) {
|
|
469
|
+
const webContents = event.sender;
|
|
470
|
+
const existing = renderers.get(webContents.id);
|
|
471
|
+
if (existing) return existing;
|
|
472
|
+
const id = webContents.id;
|
|
473
|
+
const onDestroyed = () => disposeRenderer(id);
|
|
474
|
+
const onNavigation = (...args) => {
|
|
475
|
+
if (!navigationEndsRendererWorld(args)) return;
|
|
476
|
+
disposeRenderer(id);
|
|
477
|
+
};
|
|
478
|
+
webContents.once("destroyed", onDestroyed);
|
|
479
|
+
webContents.on("did-start-navigation", onNavigation);
|
|
480
|
+
webContents.on("did-navigate", onNavigation);
|
|
481
|
+
const entry = {
|
|
482
|
+
webContents,
|
|
483
|
+
client: null,
|
|
484
|
+
threads: /* @__PURE__ */ new Map(),
|
|
485
|
+
rendererOptions: /* @__PURE__ */ new Map(),
|
|
486
|
+
opening: /* @__PURE__ */ new Map(),
|
|
487
|
+
subscriptions: /* @__PURE__ */ new Map(),
|
|
488
|
+
syncing: null,
|
|
489
|
+
disposed: false,
|
|
490
|
+
detach: () => {
|
|
491
|
+
removeWebContentsListener(webContents, "destroyed", onDestroyed);
|
|
492
|
+
removeWebContentsListener(webContents, "did-start-navigation", onNavigation);
|
|
493
|
+
removeWebContentsListener(webContents, "did-navigate", onNavigation);
|
|
494
|
+
}
|
|
495
|
+
};
|
|
496
|
+
renderers.set(id, entry);
|
|
497
|
+
return entry;
|
|
498
|
+
}
|
|
499
|
+
function snapshot(thread, key) {
|
|
500
|
+
return {
|
|
501
|
+
id: thread.id,
|
|
502
|
+
key,
|
|
503
|
+
title: thread.title ?? null,
|
|
504
|
+
model: thread.model ?? null,
|
|
505
|
+
mcpCapability: thread.mcpCapability ?? null,
|
|
506
|
+
instructionsCapability: thread.instructionsCapability ?? null,
|
|
507
|
+
settingSourcesCapability: thread.settingSourcesCapability ?? null,
|
|
508
|
+
permissionCapability: thread.permissionCapability ?? null
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
async function hostOpenOptions(client, key, raw) {
|
|
512
|
+
const rendererOptions = raw && typeof raw === "object" && !Array.isArray(raw) ? raw : void 0;
|
|
513
|
+
if (opts.openOptions) {
|
|
514
|
+
let summary = null;
|
|
515
|
+
try {
|
|
516
|
+
summary = typeof client.threads.get === "function" ? await client.threads.get(key) : null;
|
|
517
|
+
} catch (error) {
|
|
518
|
+
log(`[ade-electron] threads.open "${key}": could not read whether the key exists (${errorMessage(error)}); treating it as new`);
|
|
519
|
+
}
|
|
520
|
+
const context = { exists: summary !== null, summary };
|
|
521
|
+
return await opts.openOptions(key, rendererOptions, context) ?? void 0;
|
|
522
|
+
}
|
|
523
|
+
const filtered = rendererOpenOptions(key, rendererOptions, log);
|
|
524
|
+
if (!filtered) return void 0;
|
|
525
|
+
if (typeof filtered.model === "string" && opts.allowModel && !await opts.allowModel(key, { modelId: filtered.model })) {
|
|
526
|
+
throw unauthorized(`threads.open with model "${filtered.model}"`);
|
|
527
|
+
}
|
|
528
|
+
return filtered;
|
|
529
|
+
}
|
|
530
|
+
async function openThread(entry, client, key, raw, options) {
|
|
531
|
+
const existing = entry.threads.get(key);
|
|
532
|
+
if (existing) {
|
|
533
|
+
if (options?.refresh) await client.threads.open(key, { refresh: options.refresh });
|
|
534
|
+
return existing;
|
|
535
|
+
}
|
|
536
|
+
const pending = entry.opening.get(key);
|
|
537
|
+
if (pending) return pending;
|
|
538
|
+
const started = (async () => {
|
|
539
|
+
let target = client;
|
|
540
|
+
let targetOptions = options;
|
|
541
|
+
for (; ; ) {
|
|
542
|
+
const thread = await (targetOptions ? target.threads.open(key, targetOptions) : target.threads.open(key));
|
|
543
|
+
if (entry.disposed) return thread;
|
|
544
|
+
const current = entry.client ?? target;
|
|
545
|
+
if (current === target) {
|
|
546
|
+
entry.threads.set(key, thread);
|
|
547
|
+
entry.rendererOptions.set(key, raw);
|
|
548
|
+
return thread;
|
|
549
|
+
}
|
|
550
|
+
if (allowThreadKey && !await allowThreadKey(key)) throw unauthorized("threads.open");
|
|
551
|
+
target = current;
|
|
552
|
+
targetOptions = await hostOpenOptions(current, key, raw);
|
|
553
|
+
}
|
|
554
|
+
})().finally(() => {
|
|
555
|
+
entry.opening.delete(key);
|
|
556
|
+
});
|
|
557
|
+
entry.opening.set(key, started);
|
|
558
|
+
return started;
|
|
559
|
+
}
|
|
560
|
+
function releaseSubscription(entry, subscriptionId) {
|
|
561
|
+
const subscription = entry.subscriptions.get(subscriptionId);
|
|
562
|
+
if (!subscription) return;
|
|
563
|
+
entry.subscriptions.delete(subscriptionId);
|
|
564
|
+
try {
|
|
565
|
+
subscription.stop();
|
|
566
|
+
} catch (error) {
|
|
567
|
+
log(`[ade-electron] unsubscribe ${subscriptionId} failed: ${errorMessage(error)}`);
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
function attachThreadSubscription(entry, thread, key, subscriptionId) {
|
|
571
|
+
const stop = thread.on("event", (envelope) => {
|
|
572
|
+
push(entry.webContents, { kind: "thread", subscriptionId, key, envelope });
|
|
573
|
+
});
|
|
574
|
+
entry.subscriptions.set(subscriptionId, { kind: "thread", key, stop });
|
|
575
|
+
}
|
|
576
|
+
function attachClientSubscription(entry, client, subscriptionId) {
|
|
577
|
+
const stops = DAEX_CLIENT_EVENTS.map(
|
|
578
|
+
(event) => client.on(event, (payload) => {
|
|
579
|
+
const message = { kind: "client", subscriptionId, event, payload };
|
|
580
|
+
const key = event === "threadLifecycle" ? payload.key : void 0;
|
|
581
|
+
if (!allowThreadKey || typeof key !== "string") {
|
|
582
|
+
push(entry.webContents, message);
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
void Promise.resolve(allowThreadKey(key)).then((allowed) => {
|
|
586
|
+
if (allowed && !entry.disposed) push(entry.webContents, message);
|
|
587
|
+
}).catch((error) => log(`[ade-electron] allowThreadKey failed for "${key}": ${errorMessage(error)}`));
|
|
588
|
+
})
|
|
589
|
+
);
|
|
590
|
+
entry.subscriptions.set(subscriptionId, {
|
|
591
|
+
kind: "client",
|
|
592
|
+
stop: () => {
|
|
593
|
+
for (const stop of stops) stop();
|
|
594
|
+
}
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
function attachProvidersSubscription(entry, client, subscriptionId) {
|
|
598
|
+
const stop = client.providers.onChange((statuses) => {
|
|
599
|
+
push(entry.webContents, { kind: "providers", subscriptionId, statuses });
|
|
600
|
+
});
|
|
601
|
+
entry.subscriptions.set(subscriptionId, { kind: "providers", stop });
|
|
602
|
+
}
|
|
603
|
+
async function syncClient(entry) {
|
|
604
|
+
while (entry.syncing) await entry.syncing;
|
|
605
|
+
const client = currentClient();
|
|
606
|
+
if (!disposed) watchLifecycle(client);
|
|
607
|
+
if (entry.client === client) return;
|
|
608
|
+
const move = moveToClient(entry, client).finally(() => {
|
|
609
|
+
entry.syncing = null;
|
|
610
|
+
});
|
|
611
|
+
entry.syncing = move;
|
|
612
|
+
await move;
|
|
613
|
+
}
|
|
614
|
+
async function moveToClient(entry, client) {
|
|
615
|
+
const previous = entry.client;
|
|
616
|
+
entry.client = client;
|
|
617
|
+
if (!previous) return;
|
|
618
|
+
log(`[ade-electron] the host swapped its ADE client; moving renderer ${entry.webContents.id} across`);
|
|
619
|
+
const subscriptions = [...entry.subscriptions];
|
|
620
|
+
for (const [subscriptionId] of subscriptions) releaseSubscription(entry, subscriptionId);
|
|
621
|
+
const opened = [...entry.rendererOptions];
|
|
622
|
+
entry.threads.clear();
|
|
623
|
+
entry.rendererOptions.clear();
|
|
624
|
+
for (const [key, raw] of opened) {
|
|
625
|
+
if (entry.disposed) return;
|
|
626
|
+
try {
|
|
627
|
+
if (allowThreadKey && !await allowThreadKey(key)) {
|
|
628
|
+
log(`[ade-electron] dropped "${key}" on the new client: allowThreadKey refused it; its subscriptions end`);
|
|
629
|
+
continue;
|
|
630
|
+
}
|
|
631
|
+
await openThread(entry, client, key, raw, await hostOpenOptions(client, key, raw));
|
|
632
|
+
} catch (error) {
|
|
633
|
+
log(`[ade-electron] dropped "${key}" on the new client: ${errorMessage(error)}; its subscriptions end`);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
if (entry.disposed) return;
|
|
637
|
+
for (const [subscriptionId, subscription] of subscriptions) {
|
|
638
|
+
try {
|
|
639
|
+
if (subscription.kind === "thread") {
|
|
640
|
+
const thread = entry.threads.get(subscription.key);
|
|
641
|
+
if (thread) attachThreadSubscription(entry, thread, subscription.key, subscriptionId);
|
|
642
|
+
} else if (subscription.kind === "client") {
|
|
643
|
+
attachClientSubscription(entry, client, subscriptionId);
|
|
644
|
+
} else {
|
|
645
|
+
attachProvidersSubscription(entry, client, subscriptionId);
|
|
646
|
+
}
|
|
647
|
+
} catch (error) {
|
|
648
|
+
log(`[ade-electron] could not move subscription ${subscriptionId} to the new client: ${errorMessage(error)}`);
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
function forgetKeyEverywhere(key) {
|
|
653
|
+
for (const entry of renderers.values()) {
|
|
654
|
+
entry.threads.delete(key);
|
|
655
|
+
entry.rendererOptions.delete(key);
|
|
656
|
+
for (const [subscriptionId, subscription] of [...entry.subscriptions]) {
|
|
657
|
+
if (subscription.kind === "thread" && subscription.key === key) releaseSubscription(entry, subscriptionId);
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
function notifyThreadRemoved(key, kind) {
|
|
662
|
+
if (!opts.onThreadRemoved) return;
|
|
663
|
+
try {
|
|
664
|
+
opts.onThreadRemoved(key, kind);
|
|
665
|
+
} catch (error) {
|
|
666
|
+
log(`[ade-electron] onThreadRemoved threw for "${key}": ${errorMessage(error)}`);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
function requireThread(entry, key) {
|
|
670
|
+
const thread = entry.threads.get(key);
|
|
671
|
+
if (!thread) {
|
|
672
|
+
throw new DaexError(
|
|
673
|
+
"thread_not_found",
|
|
674
|
+
`This renderer has no open thread "${key}". Call threads.open first; a reload drops the bridge's handles but not the conversation.`
|
|
675
|
+
);
|
|
676
|
+
}
|
|
677
|
+
return thread;
|
|
678
|
+
}
|
|
679
|
+
const handlers = {
|
|
680
|
+
"providers.status": () => currentClient().providers.status(),
|
|
681
|
+
"providers.refresh": () => currentClient().providers.refresh(),
|
|
682
|
+
"providers.capabilities": (_entry, args) => currentClient().providers.capabilities(requireString(args[0], "provider")),
|
|
683
|
+
"providers.subscribe": (entry) => {
|
|
684
|
+
const subscriptionId = nextSubscriptionId("providers");
|
|
685
|
+
attachProvidersSubscription(entry, currentClient(), subscriptionId);
|
|
686
|
+
const result = { subscriptionId };
|
|
687
|
+
return result;
|
|
688
|
+
},
|
|
689
|
+
"providers.unsubscribe": (entry, args) => {
|
|
690
|
+
releaseSubscription(entry, requireString(args[0], "subscriptionId"));
|
|
691
|
+
return null;
|
|
692
|
+
},
|
|
693
|
+
"models.list": () => currentClient().models.list(),
|
|
694
|
+
"doctor": () => currentClient().doctor(),
|
|
695
|
+
"client.subscribe": (entry) => {
|
|
696
|
+
const subscriptionId = nextSubscriptionId("client");
|
|
697
|
+
attachClientSubscription(entry, currentClient(), subscriptionId);
|
|
698
|
+
const result = { subscriptionId };
|
|
699
|
+
return result;
|
|
700
|
+
},
|
|
701
|
+
"client.unsubscribe": (entry, args) => {
|
|
702
|
+
releaseSubscription(entry, requireString(args[0], "subscriptionId"));
|
|
703
|
+
return null;
|
|
704
|
+
},
|
|
705
|
+
"threads.open": async (entry, args) => {
|
|
706
|
+
const key = requireString(args[0], "thread key");
|
|
707
|
+
const client = currentClient();
|
|
708
|
+
const options = await hostOpenOptions(client, key, args[1]);
|
|
709
|
+
const thread = await openThread(entry, client, key, args[1], options);
|
|
710
|
+
return snapshot(thread, key);
|
|
711
|
+
},
|
|
712
|
+
"threads.list": async () => {
|
|
713
|
+
const rows = await currentClient().threads.list();
|
|
714
|
+
if (!allowThreadKey) return rows;
|
|
715
|
+
const allowed = await Promise.all(
|
|
716
|
+
rows.map(async (row) => row.key !== null && await allowThreadKey(row.key))
|
|
717
|
+
);
|
|
718
|
+
return rows.filter((_row, index) => allowed[index]);
|
|
719
|
+
},
|
|
720
|
+
"threads.get": (_entry, args) => {
|
|
721
|
+
const key = requireString(args[0], "thread key");
|
|
722
|
+
return currentClient().threads.get(key);
|
|
723
|
+
},
|
|
724
|
+
"threads.delete": async (_entry, args) => {
|
|
725
|
+
const key = requireString(args[0], "thread key");
|
|
726
|
+
await currentClient().threads.delete(key);
|
|
727
|
+
forgetKeyEverywhere(key.trim());
|
|
728
|
+
return null;
|
|
729
|
+
},
|
|
730
|
+
"threads.archive": async (_entry, args) => {
|
|
731
|
+
const key = requireString(args[0], "thread key");
|
|
732
|
+
await currentClient().threads.archive(key);
|
|
733
|
+
return null;
|
|
734
|
+
},
|
|
735
|
+
"threads.unarchive": async (_entry, args) => {
|
|
736
|
+
const key = requireString(args[0], "thread key");
|
|
737
|
+
await currentClient().threads.unarchive(key);
|
|
738
|
+
return null;
|
|
739
|
+
},
|
|
740
|
+
"thread.export": (_entry, args) => {
|
|
741
|
+
const key = requireString(args[0], "thread key");
|
|
742
|
+
return currentClient().exportThread(key);
|
|
743
|
+
},
|
|
744
|
+
"thread.update": async (entry, args) => {
|
|
745
|
+
const key = requireString(args[0], "thread key");
|
|
746
|
+
const patch = args[1] ?? {};
|
|
747
|
+
const safe = {
|
|
748
|
+
...patch.title === null || typeof patch.title === "string" ? { title: patch.title } : {},
|
|
749
|
+
...patch.reasoningEffort === null || typeof patch.reasoningEffort === "string" ? { reasoningEffort: patch.reasoningEffort } : {},
|
|
750
|
+
...typeof patch.fastMode === "boolean" ? { fastMode: patch.fastMode } : {}
|
|
751
|
+
};
|
|
752
|
+
const options = args[2] ?? void 0;
|
|
753
|
+
return requireThread(entry, key).update(safe, options?.force === true ? { force: true } : {});
|
|
754
|
+
},
|
|
755
|
+
"thread.retry": (entry, args) => {
|
|
756
|
+
const key = requireString(args[0], "thread key");
|
|
757
|
+
return requireThread(entry, key).retry();
|
|
758
|
+
},
|
|
759
|
+
"thread.editLast": (entry, args) => {
|
|
760
|
+
const key = requireString(args[0], "thread key");
|
|
761
|
+
const text = typeof args[1] === "string" ? args[1] : "";
|
|
762
|
+
const { displayText, attachments } = rendererSendOptions("thread.editLast", key, args[2], log);
|
|
763
|
+
return requireThread(entry, key).editLast(text, {
|
|
764
|
+
...displayText !== void 0 ? { displayText } : {},
|
|
765
|
+
...attachments ? { attachments } : {}
|
|
766
|
+
});
|
|
767
|
+
},
|
|
768
|
+
"thread.historyPage": (entry, args) => {
|
|
769
|
+
const key = requireString(args[0], "thread key");
|
|
770
|
+
const options = args[1] ?? void 0;
|
|
771
|
+
return requireThread(entry, key).historyPage(options ?? {});
|
|
772
|
+
},
|
|
773
|
+
"thread.send": async (entry, args) => {
|
|
774
|
+
const key = requireString(args[0], "thread key");
|
|
775
|
+
const text = typeof args[1] === "string" ? args[1] : "";
|
|
776
|
+
await requireThread(entry, key).send(text, rendererSendOptions("thread.send", key, args[2], log));
|
|
777
|
+
return null;
|
|
778
|
+
},
|
|
779
|
+
"thread.steer": async (entry, args) => {
|
|
780
|
+
const key = requireString(args[0], "thread key");
|
|
781
|
+
const text = typeof args[1] === "string" ? args[1] : "";
|
|
782
|
+
const attachments = rendererSendOptions("thread.steer", key, args[2], log).attachments;
|
|
783
|
+
await requireThread(entry, key).steer(text, attachments ? { attachments } : {});
|
|
784
|
+
return null;
|
|
785
|
+
},
|
|
786
|
+
"thread.interrupt": async (entry, args) => {
|
|
787
|
+
const key = requireString(args[0], "thread key");
|
|
788
|
+
const raw = args[1] && typeof args[1] === "object" ? args[1] : null;
|
|
789
|
+
const options = raw ? {
|
|
790
|
+
...raw.queue === "keep" || raw.queue === "drop" ? { queue: raw.queue } : {},
|
|
791
|
+
...typeof raw.mode === "string" ? { mode: raw.mode } : {}
|
|
792
|
+
} : void 0;
|
|
793
|
+
await requireThread(entry, key).interrupt(options);
|
|
794
|
+
return null;
|
|
795
|
+
},
|
|
796
|
+
"thread.respond": async (entry, args) => {
|
|
797
|
+
const key = requireString(args[0], "thread key");
|
|
798
|
+
const requestId = requireString(args[1], "request id");
|
|
799
|
+
const response = requireRecord(args[2], "response");
|
|
800
|
+
await requireThread(entry, key).respond(requestId, response);
|
|
801
|
+
return null;
|
|
802
|
+
},
|
|
803
|
+
"thread.queue.list": (entry, args) => requireThread(entry, requireString(args[0], "thread key")).queue.list(),
|
|
804
|
+
"thread.queue.cancel": async (entry, args) => {
|
|
805
|
+
await requireThread(entry, requireString(args[0], "thread key")).queue.cancel(requireString(args[1], "queue id"));
|
|
806
|
+
return null;
|
|
807
|
+
},
|
|
808
|
+
"thread.queue.edit": async (entry, args) => {
|
|
809
|
+
await requireThread(entry, requireString(args[0], "thread key")).queue.edit(requireString(args[1], "queue id"), typeof args[2] === "string" ? args[2] : "");
|
|
810
|
+
return null;
|
|
811
|
+
},
|
|
812
|
+
"thread.queue.move": async (entry, args) => {
|
|
813
|
+
const index = Number.isInteger(args[2]) ? args[2] : -1;
|
|
814
|
+
await requireThread(entry, requireString(args[0], "thread key")).queue.move(requireString(args[1], "queue id"), index);
|
|
815
|
+
return null;
|
|
816
|
+
},
|
|
817
|
+
"thread.queue.dispatch": async (entry, args) => {
|
|
818
|
+
await requireThread(entry, requireString(args[0], "thread key")).queue.dispatch(requireString(args[1], "queue id"));
|
|
819
|
+
return null;
|
|
820
|
+
},
|
|
821
|
+
"thread.queue.cancelDispatched": async (entry, args) => {
|
|
822
|
+
await requireThread(entry, requireString(args[0], "thread key")).queue.cancelDispatched(requireString(args[1], "queue id"));
|
|
823
|
+
return null;
|
|
824
|
+
},
|
|
825
|
+
"thread.queue.restore": async (entry, args) => {
|
|
826
|
+
await requireThread(entry, requireString(args[0], "thread key")).queue.restore();
|
|
827
|
+
return null;
|
|
828
|
+
},
|
|
829
|
+
"thread.stopTask": async (entry, args) => {
|
|
830
|
+
await requireThread(entry, requireString(args[0], "thread key")).stopTask(requireString(args[1], "task id"));
|
|
831
|
+
return null;
|
|
832
|
+
},
|
|
833
|
+
"thread.slashCommands": (entry, args) => requireThread(entry, requireString(args[0], "thread key")).slashCommands(),
|
|
834
|
+
"thread.recover": (entry, args) => {
|
|
835
|
+
const options = args[1] && typeof args[1] === "object" ? args[1] : {};
|
|
836
|
+
return requireThread(entry, requireString(args[0], "thread key")).recover(options);
|
|
837
|
+
},
|
|
838
|
+
"thread.resolveUnprocessed": (entry, args) => {
|
|
839
|
+
const raw = args[1] && typeof args[1] === "object" ? args[1] : { steerId: "", action: "dismiss" };
|
|
840
|
+
return requireThread(entry, requireString(args[0], "thread key")).resolveUnprocessed(raw);
|
|
841
|
+
},
|
|
842
|
+
"thread.usageLimit.resumeNow": (entry, args) => requireThread(entry, requireString(args[0], "thread key")).usageLimit.resumeNow(),
|
|
843
|
+
"thread.usageLimit.continueOnAlternate": (entry, args) => requireThread(entry, requireString(args[0], "thread key")).usageLimit.continueOnAlternate(),
|
|
844
|
+
"thread.switchAccount": (entry, args) => requireThread(entry, requireString(args[0], "thread key")).switchAccount(requireString(args[1], "account id")),
|
|
845
|
+
"thread.history": (entry, args) => {
|
|
846
|
+
const key = requireString(args[0], "thread key");
|
|
847
|
+
const options = args[1] ?? void 0;
|
|
848
|
+
return requireThread(entry, key).history(options);
|
|
849
|
+
},
|
|
850
|
+
"thread.setModel": async (entry, args) => {
|
|
851
|
+
const key = requireString(args[0], "thread key");
|
|
852
|
+
const modelId = requireString(args[1], "modelId");
|
|
853
|
+
if (opts.allowModel && !await opts.allowModel(key, { modelId })) {
|
|
854
|
+
throw unauthorized(`thread.setModel to "${modelId}"`);
|
|
855
|
+
}
|
|
856
|
+
const options = args[2] ?? void 0;
|
|
857
|
+
return requireThread(entry, key).setModel(modelId, options);
|
|
858
|
+
},
|
|
859
|
+
"thread.approve": async (entry, args) => {
|
|
860
|
+
const key = requireString(args[0], "thread key");
|
|
861
|
+
const itemId = requireString(args[1], "itemId");
|
|
862
|
+
const decision = requireApprovalDecision(args[2]);
|
|
863
|
+
const responseText = typeof args[3] === "string" ? args[3] : void 0;
|
|
864
|
+
await requireThread(entry, key).approve(itemId, decision, responseText);
|
|
865
|
+
return null;
|
|
866
|
+
},
|
|
867
|
+
"thread.pendingApprovals": (entry, args) => {
|
|
868
|
+
const key = requireString(args[0], "thread key");
|
|
869
|
+
return requireThread(entry, key).pendingApprovals();
|
|
870
|
+
},
|
|
871
|
+
"thread.subscribe": (entry, args) => {
|
|
872
|
+
const key = requireString(args[0], "thread key");
|
|
873
|
+
const thread = requireThread(entry, key);
|
|
874
|
+
const subscriptionId = nextSubscriptionId(`thread:${key}`);
|
|
875
|
+
attachThreadSubscription(entry, thread, key, subscriptionId);
|
|
876
|
+
const result = { subscriptionId };
|
|
877
|
+
return result;
|
|
878
|
+
},
|
|
879
|
+
"thread.unsubscribe": (entry, args) => {
|
|
880
|
+
releaseSubscription(entry, requireString(args[0], "subscriptionId"));
|
|
881
|
+
return null;
|
|
882
|
+
},
|
|
883
|
+
"accounts.list": (_entry, args) => currentClient().accounts.list(typeof args[0] === "string" ? args[0] : void 0),
|
|
884
|
+
"accounts.add": (_entry, args) => currentClient().accounts.add(requireString(args[0], "provider"), args[1] && typeof args[1] === "object" ? args[1] : void 0),
|
|
885
|
+
"accounts.remove": (_entry, args) => currentClient().accounts.remove(requireString(args[0], "account id")),
|
|
886
|
+
"accounts.rename": (_entry, args) => currentClient().accounts.rename(requireString(args[0], "account id"), requireString(args[1], "label")),
|
|
887
|
+
"accounts.setDefault": (_entry, args) => currentClient().accounts.setDefault(requireString(args[0], "account id")),
|
|
888
|
+
"accounts.signOut": (_entry, args) => currentClient().accounts.signOut(requireString(args[0], "account id")),
|
|
889
|
+
"accounts.refresh": () => currentClient().accounts.refresh(),
|
|
890
|
+
"accounts.signIn": async (entry, args) => {
|
|
891
|
+
const handle = await currentClient().accounts.signIn(requireString(args[0], "account id"), args[1] && typeof args[1] === "object" ? args[1] : void 0);
|
|
892
|
+
return await signIns.start(entry.webContents.id, handle);
|
|
893
|
+
},
|
|
894
|
+
"accounts.signInStatus": (entry, args) => signIns.status(entry.webContents.id, requireString(args[0], "login id")),
|
|
895
|
+
"accounts.signInSubmitCode": async (entry, args) => {
|
|
896
|
+
await signIns.submitCode(entry.webContents.id, requireString(args[0], "login id"), requireString(args[1], "sign-in code"));
|
|
897
|
+
return null;
|
|
898
|
+
},
|
|
899
|
+
"accounts.signInCancel": async (entry, args) => {
|
|
900
|
+
await signIns.cancel(entry.webContents.id, requireString(args[0], "login id"));
|
|
901
|
+
return null;
|
|
902
|
+
},
|
|
903
|
+
"credentials.list": (_entry, args) => currentClient().credentials.list(typeof args[0] === "string" ? args[0] : void 0),
|
|
904
|
+
"credentials.add": (_entry, args) => currentClient().credentials.add(requireRecord(args[0], "credential")),
|
|
905
|
+
"credentials.remove": (_entry, args) => currentClient().credentials.remove(requireString(args[0], "credential id"), typeof args[1] === "string" ? args[1] : void 0),
|
|
906
|
+
"credentials.verify": (_entry, args) => currentClient().credentials.verify(requireString(args[0], "provider")),
|
|
907
|
+
"presets.list": () => currentClient().presets.list(),
|
|
908
|
+
"presets.add": (_entry, args) => currentClient().presets.add(requireRecord(args[0], "preset")),
|
|
909
|
+
"presets.update": (_entry, args) => currentClient().presets.update(requireString(args[0], "preset id"), requireRecord(args[1], "preset patch")),
|
|
910
|
+
"presets.remove": (_entry, args) => currentClient().presets.remove(requireString(args[0], "preset id"))
|
|
911
|
+
};
|
|
912
|
+
async function dispatch(event, request) {
|
|
913
|
+
const method = typeof request?.method === "string" ? request.method : "";
|
|
914
|
+
const args = Array.isArray(request?.args) ? request.args : [];
|
|
915
|
+
try {
|
|
916
|
+
if (disposed) throw new DaexError("disposed", "The DAEX IPC bridge has been disposed.");
|
|
917
|
+
if (!Object.hasOwn(handlers, method)) {
|
|
918
|
+
throw new DaexError("invalid_option", `Unknown DAEX bridge method "${method}".`);
|
|
919
|
+
}
|
|
920
|
+
const known = method;
|
|
921
|
+
if (opts.authorize && !await opts.authorize(event, method, args)) {
|
|
922
|
+
throw unauthorized(method);
|
|
923
|
+
}
|
|
924
|
+
if (known.startsWith("accounts.") || known.startsWith("credentials.") || known.startsWith("presets.")) {
|
|
925
|
+
const gate = opts.allowAccountManagement;
|
|
926
|
+
const allowed = typeof gate === "function" ? await gate(event, method, args) : gate === true;
|
|
927
|
+
if (!allowed) throw unauthorized(method);
|
|
928
|
+
}
|
|
929
|
+
if (allowThreadKey && DAEX_IPC_THREAD_KEY_METHODS.has(known)) {
|
|
930
|
+
const key = typeof args[0] === "string" ? args[0] : "";
|
|
931
|
+
if (!await allowThreadKey(key)) throw unauthorized(method);
|
|
932
|
+
}
|
|
933
|
+
if (disposed) throw new DaexError("disposed", "The DAEX IPC bridge has been disposed.");
|
|
934
|
+
if (event.sender.isDestroyed()) throw new DaexError("disposed", "The renderer that made this call is gone.");
|
|
935
|
+
const handler = handlers[known];
|
|
936
|
+
const entry = rendererFor(event);
|
|
937
|
+
await syncClient(entry);
|
|
938
|
+
const value = await handler(entry, args);
|
|
939
|
+
return { ok: true, value: value ?? null };
|
|
940
|
+
} catch (error) {
|
|
941
|
+
log(`[ade-electron] ${method || "(no method)"} failed: ${errorMessage(error)}`);
|
|
942
|
+
return { ok: false, error: serializeError(error) };
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
if (typeof clientSource !== "function") watchLifecycle(clientSource);
|
|
946
|
+
ipcMain.handle(
|
|
947
|
+
invokeChannel(prefix),
|
|
948
|
+
(event, payload) => dispatch(event, payload ?? {})
|
|
949
|
+
);
|
|
950
|
+
const dispose = () => {
|
|
951
|
+
if (disposed) return;
|
|
952
|
+
disposed = true;
|
|
953
|
+
stopWatching?.();
|
|
954
|
+
stopWatching = null;
|
|
955
|
+
watchedClient = null;
|
|
956
|
+
ipcMain.removeHandler(invokeChannel(prefix));
|
|
957
|
+
for (const id of [...renderers.keys()]) disposeRenderer(id);
|
|
958
|
+
};
|
|
959
|
+
return Object.assign(dispose, {
|
|
960
|
+
forget: (key) => {
|
|
961
|
+
if (typeof key === "string" && key.trim()) forgetKeyEverywhere(key.trim());
|
|
962
|
+
}
|
|
963
|
+
});
|
|
964
|
+
}
|
|
965
|
+
var adeErrorCodeOf = daexErrorCodeOf;
|
|
966
|
+
var registerAdeIpc = registerDaexIpc;
|
|
967
|
+
|
|
968
|
+
// src/electron/bridgeCheck.ts
|
|
969
|
+
function fail(detail) {
|
|
970
|
+
throw new DaexError("invalid_option", `The preload bridge does not match @daex-dev/sdk: ${detail}`);
|
|
971
|
+
}
|
|
972
|
+
async function checkDaexBridge(expose, opts = {}) {
|
|
973
|
+
const key = opts.key?.trim() || DAEX_DEFAULT_BRIDGE_KEY;
|
|
974
|
+
const prefix = opts.channelPrefix?.trim() || DAEX_DEFAULT_CHANNEL_PREFIX;
|
|
975
|
+
const exposed = /* @__PURE__ */ new Map();
|
|
976
|
+
const invokes = [];
|
|
977
|
+
const listeners = /* @__PURE__ */ new Map();
|
|
978
|
+
const reply = { ok: true, value: { probe: true } };
|
|
979
|
+
const contextBridge = {
|
|
980
|
+
exposeInMainWorld(name, api) {
|
|
981
|
+
exposed.set(name, api);
|
|
982
|
+
}
|
|
983
|
+
};
|
|
984
|
+
const ipcRenderer = {
|
|
985
|
+
invoke(channel, ...args2) {
|
|
986
|
+
invokes.push({ channel, args: args2 });
|
|
987
|
+
return Promise.resolve(reply);
|
|
988
|
+
},
|
|
989
|
+
on(channel, listener) {
|
|
990
|
+
let set = listeners.get(channel);
|
|
991
|
+
if (!set) {
|
|
992
|
+
set = /* @__PURE__ */ new Set();
|
|
993
|
+
listeners.set(channel, set);
|
|
994
|
+
}
|
|
995
|
+
set.add(listener);
|
|
996
|
+
return ipcRenderer;
|
|
997
|
+
},
|
|
998
|
+
removeListener(channel, listener) {
|
|
999
|
+
listeners.get(channel)?.delete(listener);
|
|
1000
|
+
return ipcRenderer;
|
|
1001
|
+
}
|
|
1002
|
+
};
|
|
1003
|
+
expose(contextBridge, ipcRenderer);
|
|
1004
|
+
const bridge = exposed.get(key);
|
|
1005
|
+
if (!bridge || typeof bridge !== "object") {
|
|
1006
|
+
fail(`nothing was exposed on window.${key} (exposed: ${[...exposed.keys()].join(", ") || "nothing"}).`);
|
|
1007
|
+
}
|
|
1008
|
+
if (typeof bridge.invoke !== "function" || typeof bridge.onEvent !== "function") {
|
|
1009
|
+
fail(`window.${key} must have the functions invoke and onEvent.`);
|
|
1010
|
+
}
|
|
1011
|
+
const args = ["probe", 1];
|
|
1012
|
+
const result = await bridge.invoke(
|
|
1013
|
+
"doctor",
|
|
1014
|
+
args
|
|
1015
|
+
);
|
|
1016
|
+
const call = invokes[0];
|
|
1017
|
+
if (!call) fail("invoke() did not call ipcRenderer.invoke.");
|
|
1018
|
+
if (call.channel !== invokeChannel(prefix)) {
|
|
1019
|
+
fail(`invoke() used channel "${call.channel}"; the main process listens on "${invokeChannel(prefix)}".`);
|
|
1020
|
+
}
|
|
1021
|
+
const payload = call.args[0];
|
|
1022
|
+
if (call.args.length !== 1 || !payload || payload.method !== "doctor" || !Array.isArray(payload.args) || payload.args.length !== args.length || payload.args.some((value, index) => value !== args[index])) {
|
|
1023
|
+
fail("invoke(method, args) must send exactly one argument, { method, args }.");
|
|
1024
|
+
}
|
|
1025
|
+
if (result !== reply) fail("invoke() must resolve to ipcRenderer.invoke's result unchanged.");
|
|
1026
|
+
const received = [];
|
|
1027
|
+
const stop = bridge.onEvent((value) => {
|
|
1028
|
+
received.push(value);
|
|
1029
|
+
});
|
|
1030
|
+
const eventName = eventChannel(prefix);
|
|
1031
|
+
const attached = listeners.get(eventName);
|
|
1032
|
+
if (!attached || attached.size !== 1) {
|
|
1033
|
+
const other = [...listeners.keys()].filter((name) => (listeners.get(name)?.size ?? 0) > 0);
|
|
1034
|
+
fail(`onEvent() must listen on "${eventName}" (listening on: ${other.join(", ") || "nothing"}).`);
|
|
1035
|
+
}
|
|
1036
|
+
const sample = { kind: "probe" };
|
|
1037
|
+
for (const listener of attached) listener({ sender: null }, sample);
|
|
1038
|
+
if (received.length !== 1 || received[0] !== sample) {
|
|
1039
|
+
fail("onEvent() must pass the payload (the second ipcRenderer argument) to the listener unchanged.");
|
|
1040
|
+
}
|
|
1041
|
+
if (typeof stop !== "function") fail("onEvent() must return a function that removes the listener.");
|
|
1042
|
+
stop();
|
|
1043
|
+
if ((listeners.get(eventName)?.size ?? 0) !== 0) {
|
|
1044
|
+
fail("the function onEvent() returned did not remove its ipcRenderer listener.");
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
var checkAdeBridge = checkDaexBridge;
|
|
1048
|
+
export {
|
|
1049
|
+
ADE_DEFAULT_BRIDGE_KEY,
|
|
1050
|
+
ADE_DEFAULT_CHANNEL_PREFIX,
|
|
1051
|
+
ADE_IPC_METHODS,
|
|
1052
|
+
ADE_IPC_RENDERER_OPEN_FIELDS,
|
|
1053
|
+
ADE_IPC_THREAD_KEY_METHODS,
|
|
1054
|
+
AdeError,
|
|
1055
|
+
DAEX_DEFAULT_BRIDGE_KEY,
|
|
1056
|
+
DAEX_DEFAULT_CHANNEL_PREFIX,
|
|
1057
|
+
DAEX_IPC_METHODS,
|
|
1058
|
+
DAEX_IPC_RENDERER_OPEN_FIELDS,
|
|
1059
|
+
DAEX_IPC_THREAD_KEY_METHODS,
|
|
1060
|
+
DaexError,
|
|
1061
|
+
adeErrorCodeOf,
|
|
1062
|
+
checkAdeBridge,
|
|
1063
|
+
checkDaexBridge,
|
|
1064
|
+
compareEnvelopes,
|
|
1065
|
+
daexErrorCodeOf,
|
|
1066
|
+
envelopeDedupeKey,
|
|
1067
|
+
eventChannel,
|
|
1068
|
+
invokeChannel,
|
|
1069
|
+
mergeHistoryWithBuffer,
|
|
1070
|
+
navigationEndsRendererWorld,
|
|
1071
|
+
registerAdeIpc,
|
|
1072
|
+
registerDaexIpc
|
|
1073
|
+
};
|