dsh-starlite 0.1.0 → 0.1.2
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/dist/index.js
CHANGED
|
@@ -155,9 +155,9 @@ function parsePetInteraction(value) {
|
|
|
155
155
|
else if (value2.state === "timed" && Number.isSafeInteger(value2.deadline) && Number(value2.deadline) >= 0 && Number(value2.deadline) <= 864e13 && Number.isSafeInteger(value2.issuedAt) && Number(value2.issuedAt) >= 0 && Number(value2.issuedAt) <= Number(value2.deadline) && Number(value2.deadline) - Number(value2.issuedAt) <= 2147483647) wait = { state: "timed", deadline: Number(value2.deadline), issuedAt: Number(value2.issuedAt) };
|
|
156
156
|
else throw Error("invalid question wait");
|
|
157
157
|
}
|
|
158
|
-
const
|
|
159
|
-
bound(
|
|
160
|
-
return
|
|
158
|
+
const result2 = { interactionVersion: 1, requestId: row.requestId, sessionRef: row.sessionRef, questions, ...wait ? { wait } : {} };
|
|
159
|
+
bound(result2);
|
|
160
|
+
return result2;
|
|
161
161
|
}
|
|
162
162
|
function parsePetAnswer(value, request) {
|
|
163
163
|
const row = record2(value);
|
|
@@ -173,17 +173,17 @@ function parsePetAnswer(value, request) {
|
|
|
173
173
|
if (!selected.length && !custom) throw Error("empty answer");
|
|
174
174
|
return { id, selected, ...custom === void 0 ? {} : { custom } };
|
|
175
175
|
});
|
|
176
|
-
const
|
|
177
|
-
bound(
|
|
176
|
+
const result2 = { requestId: row.requestId, sessionRef: row.sessionRef, answers };
|
|
177
|
+
bound(result2);
|
|
178
178
|
if (request) {
|
|
179
|
-
if (
|
|
179
|
+
if (result2.requestId !== request.requestId || result2.sessionRef !== request.sessionRef || answers.length !== request.questions.length) throw Error("stale or incomplete answer");
|
|
180
180
|
for (const item of answers) {
|
|
181
181
|
const question = request.questions.find((q) => q.id === item.id);
|
|
182
182
|
if (!question || !question.multiSelect && (item.selected.length > 1 || item.selected.length > 0 && item.custom !== void 0)) throw Error("invalid selection");
|
|
183
183
|
if (item.selected.some((label) => !question.options.some((option) => option.label === label))) throw Error("unknown option");
|
|
184
184
|
}
|
|
185
185
|
}
|
|
186
|
-
return
|
|
186
|
+
return result2;
|
|
187
187
|
}
|
|
188
188
|
function record2(value) {
|
|
189
189
|
if (!value || typeof value !== "object" || Array.isArray(value)) throw Error("invalid interaction object");
|
|
@@ -209,6 +209,146 @@ var init_interaction = __esm({
|
|
|
209
209
|
}
|
|
210
210
|
});
|
|
211
211
|
|
|
212
|
+
// ../protocol/src/control.ts
|
|
213
|
+
function parseControlDesktopReport(value) {
|
|
214
|
+
const row = object(value, ["controlVersion", "enabled", "available", "model", "preferences"]);
|
|
215
|
+
version(row);
|
|
216
|
+
const preferences = object(row.preferences, ["bubblesVisible", "pointerEnabled", "scale"]);
|
|
217
|
+
const result2 = { controlVersion: 1, enabled: boolean(row.enabled), available: boolean(row.available), preferences: {
|
|
218
|
+
bubblesVisible: boolean(preferences.bubblesVisible),
|
|
219
|
+
pointerEnabled: boolean(preferences.pointerEnabled),
|
|
220
|
+
scale: number(preferences.scale, 0.25, 3)
|
|
221
|
+
} };
|
|
222
|
+
if (row.model !== void 0) {
|
|
223
|
+
const model = object(row.model, ["revision", "title", "animations"]);
|
|
224
|
+
if (!Array.isArray(model.animations) || model.animations.length > 256) throw Error("invalid animation catalog");
|
|
225
|
+
const names = /* @__PURE__ */ new Set();
|
|
226
|
+
result2.model = { revision: ref(model.revision), title: text2(model.title, 120), animations: model.animations.map((value2) => {
|
|
227
|
+
const item = object(value2, ["name", "durationMs"]), name2 = text2(item.name, 160);
|
|
228
|
+
if (names.has(name2)) throw Error("duplicate animation");
|
|
229
|
+
names.add(name2);
|
|
230
|
+
return { name: name2, durationMs: number(item.durationMs, 0, 36e5) };
|
|
231
|
+
}) };
|
|
232
|
+
}
|
|
233
|
+
if (result2.available && !result2.model) throw Error("available model missing");
|
|
234
|
+
return bounded(result2);
|
|
235
|
+
}
|
|
236
|
+
function parseControlPerform(value) {
|
|
237
|
+
const row = object(value, ["animation", "text", "durationMs", "stop"]), result2 = {};
|
|
238
|
+
if (row.stop !== void 0) {
|
|
239
|
+
if (row.stop !== true || Object.keys(row).length !== 1) throw Error("invalid stop");
|
|
240
|
+
return { stop: true };
|
|
241
|
+
}
|
|
242
|
+
if (row.animation !== void 0) result2.animation = text2(row.animation, 160);
|
|
243
|
+
if (row.text !== void 0) result2.text = text2(row.text, 1e3);
|
|
244
|
+
if (row.durationMs !== void 0) result2.durationMs = number(row.durationMs, 1e3, 3e4);
|
|
245
|
+
if (!result2.animation && !result2.text || result2.durationMs !== void 0 && !result2.text) throw Error("empty performance");
|
|
246
|
+
return bounded(result2);
|
|
247
|
+
}
|
|
248
|
+
function parseControlStage(value) {
|
|
249
|
+
const row = object(value, ["position", "scale", "pointerEnabled"]), result2 = {};
|
|
250
|
+
if (row.position !== void 0) {
|
|
251
|
+
const position = object(row.position, ["x", "y"]);
|
|
252
|
+
result2.position = { x: number(position.x, 0, 1), y: number(position.y, 0, 1) };
|
|
253
|
+
}
|
|
254
|
+
if (row.scale !== void 0) result2.scale = number(row.scale, 0.5, 1.5);
|
|
255
|
+
if (row.pointerEnabled !== void 0) result2.pointerEnabled = boolean(row.pointerEnabled);
|
|
256
|
+
if (!Object.keys(result2).length) throw Error("empty stage");
|
|
257
|
+
return result2;
|
|
258
|
+
}
|
|
259
|
+
function parseControlFrame(value) {
|
|
260
|
+
const row = object(value);
|
|
261
|
+
version(row);
|
|
262
|
+
const common = ["controlVersion", "sequence", "leaseId", "commandId", "leaseMs", "kind"];
|
|
263
|
+
const envelope = { controlVersion: 1, sequence: number(row.sequence, 0, Number.MAX_SAFE_INTEGER), leaseId: ref(row.leaseId), commandId: ref(row.commandId), leaseMs: number(row.leaseMs, CONTROL_LEASE_MS, CONTROL_LEASE_MS) };
|
|
264
|
+
if (!Number.isSafeInteger(envelope.sequence)) throw Error("invalid sequence");
|
|
265
|
+
let result2;
|
|
266
|
+
if (row.kind === "acquire") {
|
|
267
|
+
keys(row, [...common, "sessionTitle"]);
|
|
268
|
+
result2 = { ...envelope, kind: "acquire", sessionTitle: text2(row.sessionTitle, 120) };
|
|
269
|
+
} else if (row.kind === "activity") {
|
|
270
|
+
keys(row, common);
|
|
271
|
+
result2 = { ...envelope, kind: "activity" };
|
|
272
|
+
} else if (row.kind === "perform") {
|
|
273
|
+
keys(row, [...common, "animation", "text", "durationMs", "stop"]);
|
|
274
|
+
result2 = { ...envelope, kind: "perform", ...parseControlPerform(without(row, common)) };
|
|
275
|
+
} else if (row.kind === "stage") {
|
|
276
|
+
keys(row, [...common, "position", "scale", "pointerEnabled"]);
|
|
277
|
+
result2 = { ...envelope, kind: "stage", ...parseControlStage(without(row, common)) };
|
|
278
|
+
} else if (row.kind === "cancel") {
|
|
279
|
+
keys(row, [...common, "targetId"]);
|
|
280
|
+
result2 = { ...envelope, kind: "cancel", targetId: ref(row.targetId) };
|
|
281
|
+
} else if (row.kind === "release") {
|
|
282
|
+
keys(row, [...common, "reason"]);
|
|
283
|
+
result2 = { ...envelope, kind: "release", reason: choice(row.reason, CONTROL_RELEASE_REASONS) };
|
|
284
|
+
} else throw Error("invalid control kind");
|
|
285
|
+
return bounded(result2);
|
|
286
|
+
}
|
|
287
|
+
function parseControlAck(value) {
|
|
288
|
+
const row = object(value, ["controlVersion", "leaseId", "commandId", "status", "reason"]);
|
|
289
|
+
version(row);
|
|
290
|
+
return {
|
|
291
|
+
controlVersion: 1,
|
|
292
|
+
leaseId: ref(row.leaseId),
|
|
293
|
+
commandId: ref(row.commandId),
|
|
294
|
+
status: choice(row.status, ["started", "completed", "interrupted", "stopped", "rejected"]),
|
|
295
|
+
...row.reason === void 0 ? {} : { reason: choice(row.reason, ["unavailable", "unknown-animation", "user-preference", "stale", "failed"]) }
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
function parseControlRelease(value) {
|
|
299
|
+
const row = object(value, ["leaseId", "reason"]);
|
|
300
|
+
return { leaseId: ref(row.leaseId), reason: choice(row.reason, ["user", "model-changed", "unavailable"]) };
|
|
301
|
+
}
|
|
302
|
+
function object(value, allowed) {
|
|
303
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) throw Error("invalid control object");
|
|
304
|
+
const row = value;
|
|
305
|
+
if (allowed) keys(row, allowed);
|
|
306
|
+
return row;
|
|
307
|
+
}
|
|
308
|
+
function keys(row, allowed) {
|
|
309
|
+
if (Object.keys(row).some((key) => !allowed.includes(key))) throw Error("unknown control field");
|
|
310
|
+
}
|
|
311
|
+
function without(row, omitted) {
|
|
312
|
+
return Object.fromEntries(Object.entries(row).filter(([key]) => !omitted.includes(key)));
|
|
313
|
+
}
|
|
314
|
+
function version(row) {
|
|
315
|
+
if (row.controlVersion !== 1) throw Error("invalid control version");
|
|
316
|
+
}
|
|
317
|
+
function ref(value) {
|
|
318
|
+
if (!isOpaqueRef(value)) throw Error("invalid control reference");
|
|
319
|
+
return value;
|
|
320
|
+
}
|
|
321
|
+
function boolean(value) {
|
|
322
|
+
if (typeof value !== "boolean") throw Error("invalid control flag");
|
|
323
|
+
return value;
|
|
324
|
+
}
|
|
325
|
+
function number(value, min, max) {
|
|
326
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value < min || value > max) throw Error("invalid control number");
|
|
327
|
+
return value;
|
|
328
|
+
}
|
|
329
|
+
function choice(value, values) {
|
|
330
|
+
if (typeof value !== "string" || !values.includes(value)) throw Error("invalid control choice");
|
|
331
|
+
return value;
|
|
332
|
+
}
|
|
333
|
+
function text2(value, max) {
|
|
334
|
+
if (typeof value !== "string" || !value.trim() || value.length > max || /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f-\u009f\u202a-\u202e\u2066-\u2069]/u.test(value)) throw Error("invalid control text");
|
|
335
|
+
return value;
|
|
336
|
+
}
|
|
337
|
+
function bounded(value) {
|
|
338
|
+
if (new TextEncoder().encode(JSON.stringify(value)).byteLength > MAX_CONTROL_BYTES) throw Error("control exceeds wire limit");
|
|
339
|
+
return value;
|
|
340
|
+
}
|
|
341
|
+
var MAX_CONTROL_BYTES, CONTROL_LEASE_MS, CONTROL_RELEASE_REASONS;
|
|
342
|
+
var init_control = __esm({
|
|
343
|
+
"../protocol/src/control.ts"() {
|
|
344
|
+
"use strict";
|
|
345
|
+
init_interaction();
|
|
346
|
+
MAX_CONTROL_BYTES = 32768;
|
|
347
|
+
CONTROL_LEASE_MS = 3e5;
|
|
348
|
+
CONTROL_RELEASE_REASONS = ["user", "model-changed", "unavailable", "expired", "disconnected", "session-ended", "disabled", "cancelled"];
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
|
|
212
352
|
// ../protocol/src/index.ts
|
|
213
353
|
function sanitizeBubbleText(input, maximumGraphemes = DEFAULT_BUBBLE_GRAPHEMES) {
|
|
214
354
|
if (typeof input !== "string" || !Number.isSafeInteger(maximumGraphemes)) {
|
|
@@ -217,8 +357,8 @@ function sanitizeBubbleText(input, maximumGraphemes = DEFAULT_BUBBLE_GRAPHEMES)
|
|
|
217
357
|
if (maximumGraphemes < 1 || maximumGraphemes > 500) {
|
|
218
358
|
return "";
|
|
219
359
|
}
|
|
220
|
-
const
|
|
221
|
-
const normalized =
|
|
360
|
+
const bounded2 = input.length > MAX_SANITIZE_CODE_UNITS ? input.slice(0, MAX_SANITIZE_CODE_UNITS) : input;
|
|
361
|
+
const normalized = bounded2.normalize("NFC").replace(/\u001B(?:\[[0-?]*[ -/]*[@-~]|\][^\u0007]*(?:\u0007|\u001B\\)?|[@-_])/gu, "").replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F-\u009F]/gu, "").replace(/\s+/gu, " ").trim();
|
|
222
362
|
if (normalized.length === 0) {
|
|
223
363
|
return "";
|
|
224
364
|
}
|
|
@@ -264,11 +404,11 @@ function parseBubble(value) {
|
|
|
264
404
|
if (!isRecord(value) || !isBubbleKind(value.kind)) {
|
|
265
405
|
throw new Error("pet snapshot bubble is invalid");
|
|
266
406
|
}
|
|
267
|
-
const
|
|
268
|
-
if (
|
|
407
|
+
const text3 = sanitizeBubbleText(value.text);
|
|
408
|
+
if (text3.length === 0) {
|
|
269
409
|
throw new Error("pet snapshot bubble text is empty or invalid");
|
|
270
410
|
}
|
|
271
|
-
return { kind: value.kind, text:
|
|
411
|
+
return { kind: value.kind, text: text3 };
|
|
272
412
|
}
|
|
273
413
|
function isBubbleKind(value) {
|
|
274
414
|
return typeof value === "string" && BUBBLE_KINDS.includes(value);
|
|
@@ -294,6 +434,7 @@ var init_src = __esm({
|
|
|
294
434
|
"../protocol/src/index.ts"() {
|
|
295
435
|
"use strict";
|
|
296
436
|
init_billing();
|
|
437
|
+
init_control();
|
|
297
438
|
init_interaction();
|
|
298
439
|
init_interaction();
|
|
299
440
|
PET_PROTOCOL_VERSION = 1;
|
|
@@ -335,12 +476,12 @@ function resetForTurn(session) {
|
|
|
335
476
|
function handleAssistantChunk(session, data, now) {
|
|
336
477
|
const chunk = asRecord(data?.chunk);
|
|
337
478
|
const type = stringField(chunk, "type");
|
|
338
|
-
const
|
|
339
|
-
if (!
|
|
479
|
+
const text3 = stringField(chunk, "text");
|
|
480
|
+
if (!text3 || type !== "reasoning-delta" && type !== "text-delta") return;
|
|
340
481
|
if (type === "reasoning-delta") {
|
|
341
|
-
session.reasoning = appendVisible(session.reasoning,
|
|
482
|
+
session.reasoning = appendVisible(session.reasoning, text3, now);
|
|
342
483
|
} else {
|
|
343
|
-
session.response = appendVisible(session.response,
|
|
484
|
+
session.response = appendVisible(session.response, text3, now);
|
|
344
485
|
}
|
|
345
486
|
}
|
|
346
487
|
function handleAssistantMessage(session, data, now) {
|
|
@@ -351,18 +492,18 @@ function handleAssistantMessage(session, data, now) {
|
|
|
351
492
|
for (const rawBlock of message.content) {
|
|
352
493
|
const block = asRecord(rawBlock);
|
|
353
494
|
const type = stringField(block, "type");
|
|
354
|
-
const
|
|
355
|
-
if (!
|
|
356
|
-
if (type === "text") response = appendVisible(response,
|
|
357
|
-
if (type === "reasoning") reasoning = appendVisible(reasoning,
|
|
495
|
+
const text3 = stringField(block, "text");
|
|
496
|
+
if (!text3) continue;
|
|
497
|
+
if (type === "text") response = appendVisible(response, text3, now);
|
|
498
|
+
if (type === "reasoning") reasoning = appendVisible(reasoning, text3, now);
|
|
358
499
|
}
|
|
359
500
|
if (response) session.response = response;
|
|
360
501
|
if (reasoning) session.reasoning = reasoning;
|
|
361
502
|
}
|
|
362
503
|
function appendVisible(previous, addition, now) {
|
|
363
504
|
const separator = previous?.endsWithWhitespace ? " " : "";
|
|
364
|
-
const
|
|
365
|
-
return
|
|
505
|
+
const text3 = sanitizeBubbleText(`${previous?.text ?? ""}${separator}${addition}`);
|
|
506
|
+
return text3 ? { text: text3, at: now, endsWithWhitespace: /\s$/u.test(addition) } : previous;
|
|
366
507
|
}
|
|
367
508
|
function handleToolStarted(session, data, now, idField) {
|
|
368
509
|
const id = stringField(data, idField);
|
|
@@ -449,12 +590,12 @@ function selectCandidate(sessions, now) {
|
|
|
449
590
|
const late = last && [...last.inputs].filter(([, item]) => !item.blocking).at(-1);
|
|
450
591
|
return { state: "idle", at: now, activity: "idle", ...last ? { session: { ref: last.ref, title: last.title } } : {}, ...late ? { inputRequest: late[0] } : {} };
|
|
451
592
|
}
|
|
452
|
-
function notice(state,
|
|
453
|
-
return { state, bubble: bubble("notice",
|
|
593
|
+
function notice(state, text3, at) {
|
|
594
|
+
return { state, bubble: bubble("notice", text3), at };
|
|
454
595
|
}
|
|
455
596
|
function bubble(kind, rawText) {
|
|
456
|
-
const
|
|
457
|
-
return { kind, text:
|
|
597
|
+
const text3 = sanitizeBubbleText(rawText);
|
|
598
|
+
return { kind, text: text3 };
|
|
458
599
|
}
|
|
459
600
|
function latestVisible(session) {
|
|
460
601
|
if (!session.reasoning) return session.response;
|
|
@@ -465,11 +606,11 @@ function newest(current, next) {
|
|
|
465
606
|
return !current || next.at >= current.at ? next : current;
|
|
466
607
|
}
|
|
467
608
|
function newestMapValue(values) {
|
|
468
|
-
let
|
|
609
|
+
let result2;
|
|
469
610
|
for (const item of values.values()) {
|
|
470
|
-
if (!
|
|
611
|
+
if (!result2 || item.at >= result2.at) result2 = item;
|
|
471
612
|
}
|
|
472
|
-
return
|
|
613
|
+
return result2;
|
|
473
614
|
}
|
|
474
615
|
function candidateKey(candidate) {
|
|
475
616
|
return JSON.stringify([candidate.state, candidate.bubble, candidate.session, candidate.activity, candidate.inputRequest]);
|
|
@@ -553,8 +694,8 @@ var init_aggregator = __esm({
|
|
|
553
694
|
}
|
|
554
695
|
return { ref: session.ref, title: session.title };
|
|
555
696
|
}
|
|
556
|
-
sessionKey(
|
|
557
|
-
return [...this.#sessions].find(([, session]) => session.ref ===
|
|
697
|
+
sessionKey(ref2) {
|
|
698
|
+
return [...this.#sessions].find(([, session]) => session.ref === ref2)?.[0];
|
|
558
699
|
}
|
|
559
700
|
handleInputRequested(key, requestId, blocking = true) {
|
|
560
701
|
const session = this.#session(key);
|
|
@@ -873,7 +1014,7 @@ async function prepareEmbeddedCompanion(options) {
|
|
|
873
1014
|
function parseCompanionManifest(bytes, expectedVersion) {
|
|
874
1015
|
if (bytes.byteLength > MANIFEST_LIMIT) throw new Error("embedded companion manifest exceeds size limit");
|
|
875
1016
|
const value = JSON.parse(Buffer.from(bytes).toString("utf8"));
|
|
876
|
-
if (!record3(value) || ![2, 3].includes(value.schemaVersion) || !
|
|
1017
|
+
if (!record3(value) || ![2, 3].includes(value.schemaVersion) || !keys2(value, ["schemaVersion", "version", "platform", "executable", value.schemaVersion === 2 ? "defaultModel" : "defaultModels"]) || value.platform !== "windows-x64" || typeof value.version !== "string" || value.version.length > 64 || !/^\d+\.\d+\.\d+(?:-[a-zA-Z0-9.-]+)?$/u.test(value.version) || value.version !== expectedVersion || !record3(value.executable) || !keys2(value.executable, ["file", "sha256", "bytes"]) || value.executable.file !== EXECUTABLE || !digest(value.executable)) {
|
|
877
1018
|
throw new Error("invalid embedded companion manifest");
|
|
878
1019
|
}
|
|
879
1020
|
const models = value.schemaVersion === 2 ? [value.defaultModel] : value.defaultModels;
|
|
@@ -891,10 +1032,10 @@ function parseCompanionManifest(bytes, expectedVersion) {
|
|
|
891
1032
|
return value;
|
|
892
1033
|
}
|
|
893
1034
|
function validateDefaultSource(value, catalog) {
|
|
894
|
-
if (!record3(value) || !
|
|
1035
|
+
if (!record3(value) || !keys2(value, ["title", "url", "sha256", "bytes", ...catalog ? ["id", "license"] : []], ["localTest"]) || !validTitle(value.title) || !digest(value) || value.localTest !== void 0 && value.localTest !== true || typeof value.url !== "string" || value.url.length > 2048 || /[\p{Cc}\\]/u.test(value.url)) {
|
|
895
1036
|
throw new Error("invalid starter character metadata");
|
|
896
1037
|
}
|
|
897
|
-
if (catalog && (!isDefaultModelId(value.id) || !record3(value.license) || !
|
|
1038
|
+
if (catalog && (!isDefaultModelId(value.id) || !record3(value.license) || !keys2(value.license, ["title", "text"]) || !validTitle(value.license.title) || typeof value.license.text !== "string" || !value.license.text.trim() || Buffer.byteLength(value.license.text) > 100 * 1024 || value.license.text.includes("\0"))) throw new Error("invalid starter character license");
|
|
898
1039
|
const url = new URL(value.url);
|
|
899
1040
|
if (!url.pathname.toLowerCase().endsWith(".vrm") || url.username || url.password || url.hash || url.protocol !== "https:" && !(value.localTest === true && url.protocol === "http:" && url.hostname === "127.0.0.1" && /^http:\/\/127\.0\.0\.1(?::\d+)?\//u.test(value.url))) {
|
|
900
1041
|
throw new Error("invalid starter character download source");
|
|
@@ -1042,7 +1183,7 @@ function assertEqual(actual, expected) {
|
|
|
1042
1183
|
function record3(value) {
|
|
1043
1184
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1044
1185
|
}
|
|
1045
|
-
function
|
|
1186
|
+
function keys2(value, required, optional = []) {
|
|
1046
1187
|
return required.every((key) => Object.hasOwn(value, key)) && Object.keys(value).every((key) => required.includes(key) || optional.includes(key));
|
|
1047
1188
|
}
|
|
1048
1189
|
function digest(value) {
|
|
@@ -1080,11 +1221,11 @@ function detectCompanionHost(platform = process.platform, architecture = process
|
|
|
1080
1221
|
return platform === "linux" && inWsl ? "wsl" : void 0;
|
|
1081
1222
|
}
|
|
1082
1223
|
function companionEnvironment(source) {
|
|
1083
|
-
const
|
|
1224
|
+
const result2 = {};
|
|
1084
1225
|
for (const name2 of ["PATH", "Path", "HOME", "LANG", "LC_ALL", "SYSTEMROOT", "SystemRoot", "WINDIR", "TEMP", "TMP", "LOCALAPPDATA", "APPDATA", "USERPROFILE", "WSL_INTEROP", "WSL_DISTRO_NAME"]) {
|
|
1085
|
-
if (source[name2] !== void 0)
|
|
1226
|
+
if (source[name2] !== void 0) result2[name2] = source[name2];
|
|
1086
1227
|
}
|
|
1087
|
-
return
|
|
1228
|
+
return result2;
|
|
1088
1229
|
}
|
|
1089
1230
|
async function launchWindowsPet(connection, options = {}) {
|
|
1090
1231
|
const url = buildConnectionUrl(connection);
|
|
@@ -1167,9 +1308,9 @@ async function defaultModelToAcquire(path) {
|
|
|
1167
1308
|
const opened = await file.stat();
|
|
1168
1309
|
if (!opened.isFile() || opened.size !== info.size) return null;
|
|
1169
1310
|
const bytes = Buffer.alloc(info.size + 1);
|
|
1170
|
-
const
|
|
1171
|
-
if (
|
|
1172
|
-
const value = JSON.parse(bytes.subarray(0,
|
|
1311
|
+
const result2 = await file.read(bytes, 0, bytes.length, 0);
|
|
1312
|
+
if (result2.bytesRead !== info.size) return null;
|
|
1313
|
+
const value = JSON.parse(bytes.subarray(0, result2.bytesRead).toString("utf8"));
|
|
1173
1314
|
if (typeof value !== "object" || value === null || Array.isArray(value)) return null;
|
|
1174
1315
|
if (!("schemaVersion" in value)) return null;
|
|
1175
1316
|
if (value.schemaVersion === 4) {
|
|
@@ -1242,6 +1383,20 @@ async function startStatusServer(source, options = {}) {
|
|
|
1242
1383
|
let latest;
|
|
1243
1384
|
let closed = false;
|
|
1244
1385
|
let closing;
|
|
1386
|
+
let inputAvailable = false;
|
|
1387
|
+
const syncQuestions = () => options.questions?.setEnabled(inputAvailable && !options.control?.active);
|
|
1388
|
+
const unsubscribeControlState = options.control?.onChange(syncQuestions);
|
|
1389
|
+
const unsubscribeControl = options.control?.subscribe((frame) => {
|
|
1390
|
+
if (closed) return;
|
|
1391
|
+
const data = `event: control
|
|
1392
|
+
data: ${JSON.stringify(frame)}
|
|
1393
|
+
|
|
1394
|
+
`;
|
|
1395
|
+
for (const client of clients) if (!client.destroyed && !client.writableEnded) {
|
|
1396
|
+
if (client.writableLength > MAX_CONTROL_BYTES) client.destroy();
|
|
1397
|
+
else client.write(data);
|
|
1398
|
+
}
|
|
1399
|
+
});
|
|
1245
1400
|
const server = createServer((request, response) => {
|
|
1246
1401
|
const origin = request.headers.origin;
|
|
1247
1402
|
if (request.method === "OPTIONS") {
|
|
@@ -1266,6 +1421,21 @@ async function startStatusServer(source, options = {}) {
|
|
|
1266
1421
|
return;
|
|
1267
1422
|
}
|
|
1268
1423
|
const path = new URL(request.url ?? "/", "http://127.0.0.1").pathname;
|
|
1424
|
+
if (options.control && request.method === "POST" && ["/v1/control/desktop", "/v1/control/ack", "/v1/control/release", "/v1/control/open"].includes(path)) {
|
|
1425
|
+
const control = options.control;
|
|
1426
|
+
void readBody(request).then(async (body) => {
|
|
1427
|
+
if (closed || response.destroyed) return;
|
|
1428
|
+
if (path === "/v1/control/desktop") control.report(body);
|
|
1429
|
+
else if (path === "/v1/control/ack") control.acknowledge(body);
|
|
1430
|
+
else if (path === "/v1/control/open") await control.open(body);
|
|
1431
|
+
else control.userRelease(body);
|
|
1432
|
+
response.writeHead(204, { "Cache-Control": "no-store", ...origin ? corsHeaders(origin) : {} });
|
|
1433
|
+
response.end();
|
|
1434
|
+
}).catch(() => {
|
|
1435
|
+
if (!response.destroyed && !response.headersSent) reject(response, 409);
|
|
1436
|
+
});
|
|
1437
|
+
return;
|
|
1438
|
+
}
|
|
1269
1439
|
if (path === "/v1/health" && request.method === "GET") {
|
|
1270
1440
|
response.writeHead(204, {
|
|
1271
1441
|
"Cache-Control": "no-store",
|
|
@@ -1315,7 +1485,8 @@ async function startStatusServer(source, options = {}) {
|
|
|
1315
1485
|
if (path === "/v1/interaction/answer") await bridge.answer(row);
|
|
1316
1486
|
else if (path === "/v1/interaction/availability") {
|
|
1317
1487
|
if (typeof row.enabled !== "boolean" || Object.keys(row).some((k) => k !== "enabled")) throw Error("invalid availability");
|
|
1318
|
-
|
|
1488
|
+
inputAvailable = row.enabled;
|
|
1489
|
+
syncQuestions();
|
|
1319
1490
|
} else if (path === "/v1/web/open") {
|
|
1320
1491
|
if (Object.keys(row).length !== 0) throw Error("invalid navigation");
|
|
1321
1492
|
await bridge.openWebUi();
|
|
@@ -1351,11 +1522,13 @@ async function startStatusServer(source, options = {}) {
|
|
|
1351
1522
|
response.flushHeaders();
|
|
1352
1523
|
clients.add(response);
|
|
1353
1524
|
options.questions?.setConnected(true);
|
|
1525
|
+
options.control?.setConnected(true);
|
|
1354
1526
|
response.write(": connected\n\n");
|
|
1355
1527
|
if (latest) writeSnapshot(response, latest);
|
|
1356
1528
|
request.on("close", () => {
|
|
1357
1529
|
clients.delete(response);
|
|
1358
1530
|
options.questions?.setConnected(clients.size > 0);
|
|
1531
|
+
options.control?.setConnected(clients.size > 0);
|
|
1359
1532
|
});
|
|
1360
1533
|
});
|
|
1361
1534
|
const heartbeat = setInterval(() => {
|
|
@@ -1377,6 +1550,9 @@ async function startStatusServer(source, options = {}) {
|
|
|
1377
1550
|
await listenLoopback(server);
|
|
1378
1551
|
} catch (error) {
|
|
1379
1552
|
clearInterval(heartbeat);
|
|
1553
|
+
unsubscribeControl?.();
|
|
1554
|
+
unsubscribeControlState?.();
|
|
1555
|
+
options.control?.close();
|
|
1380
1556
|
await source.stop().catch(() => void 0);
|
|
1381
1557
|
server.close();
|
|
1382
1558
|
throw error;
|
|
@@ -1395,6 +1571,9 @@ async function startStatusServer(source, options = {}) {
|
|
|
1395
1571
|
closing = (async () => {
|
|
1396
1572
|
options.billing?.close();
|
|
1397
1573
|
options.questions?.close();
|
|
1574
|
+
options.control?.close();
|
|
1575
|
+
unsubscribeControl?.();
|
|
1576
|
+
unsubscribeControlState?.();
|
|
1398
1577
|
await shutdown(server, clients, source, heartbeat, true);
|
|
1399
1578
|
})();
|
|
1400
1579
|
return closing;
|
|
@@ -1761,7 +1940,7 @@ async function resolveKey(context, route) {
|
|
|
1761
1940
|
return typeof value === "string" && value.length > 0 && value.length <= 4096 && /^[!-~]+$/u.test(value) ? value : void 0;
|
|
1762
1941
|
}
|
|
1763
1942
|
function foldDailyUsage(events, inherited, date2, providers = ["deepseek"]) {
|
|
1764
|
-
const
|
|
1943
|
+
const result2 = { priced: 0, unpriced: 0, maximums: [0, 0] };
|
|
1765
1944
|
let header;
|
|
1766
1945
|
let last;
|
|
1767
1946
|
const flush = () => {
|
|
@@ -1771,11 +1950,11 @@ function foldDailyUsage(events, inherited, date2, providers = ["deepseek"]) {
|
|
|
1771
1950
|
}
|
|
1772
1951
|
const rates = typeof last.route?.model === "string" && typeof last.route.provider === "string" && providers.includes(last.route.provider) && Object.hasOwn(RATES, last.route.model) ? RATES[last.route.model] : void 0;
|
|
1773
1952
|
const sample = normalizeUsage(last.sample);
|
|
1774
|
-
if (!rates || !sample)
|
|
1953
|
+
if (!rates || !sample) result2.unpriced++;
|
|
1775
1954
|
else {
|
|
1776
|
-
|
|
1955
|
+
result2.priced++;
|
|
1777
1956
|
rates.forEach((rate, i) => {
|
|
1778
|
-
|
|
1957
|
+
result2.maximums[i] = (result2.maximums[i] ?? 0) + (rate[0] * sample.hit + rate[1] * sample.input + rate[2] * sample.output) / 1e6;
|
|
1779
1958
|
});
|
|
1780
1959
|
}
|
|
1781
1960
|
last = void 0;
|
|
@@ -1803,7 +1982,7 @@ function foldDailyUsage(events, inherited, date2, providers = ["deepseek"]) {
|
|
|
1803
1982
|
last = { turn: data.turn, step: data.step, sample: sample ?? last?.sample, route: source ?? header ?? last?.route, time: event.time };
|
|
1804
1983
|
}
|
|
1805
1984
|
flush();
|
|
1806
|
-
return
|
|
1985
|
+
return result2;
|
|
1807
1986
|
}
|
|
1808
1987
|
function normalizeUsage(sample) {
|
|
1809
1988
|
if (!sample || !count2(sample.inputTokens) || !count2(sample.outputTokens) || !count2(sample.cacheReadTokens) || sample.cacheWriteTokens !== void 0 && sample.cacheWriteTokens !== 0) return void 0;
|
|
@@ -1978,10 +2157,10 @@ var init_billing2 = __esm({
|
|
|
1978
2157
|
if (this.#accountWatchHealthy && this.#balance?.value.source === "deepseek-account" && now - this.#balance.at < 6e4) return this.#balance.value;
|
|
1979
2158
|
let value = { status: "unavailable", source: "deepseek-account", balances: [] };
|
|
1980
2159
|
try {
|
|
1981
|
-
this.#version ??= readFile2(new URL("../package.json", import.meta.url), "utf8").then((
|
|
1982
|
-
const
|
|
1983
|
-
if (typeof
|
|
1984
|
-
return
|
|
2160
|
+
this.#version ??= readFile2(new URL("../package.json", import.meta.url), "utf8").then((text3) => {
|
|
2161
|
+
const version2 = record4(JSON.parse(text3))?.version;
|
|
2162
|
+
if (typeof version2 !== "string" || version2.length > 64 || !/^\d+\.\d+\.\d+(?:-[a-zA-Z0-9.-]+)?$/u.test(version2)) throw new Error("invalid plugin version");
|
|
2163
|
+
return version2;
|
|
1985
2164
|
});
|
|
1986
2165
|
const client = {
|
|
1987
2166
|
version: await abortable(this.#version, signal),
|
|
@@ -2206,21 +2385,21 @@ var init_questions = __esm({
|
|
|
2206
2385
|
pending.finish(answer);
|
|
2207
2386
|
return;
|
|
2208
2387
|
}
|
|
2209
|
-
const late = pending.late,
|
|
2210
|
-
if (!
|
|
2388
|
+
const late = pending.late, service2 = this.#questionService();
|
|
2389
|
+
if (!service2 || !this.#currentLate(late)) {
|
|
2211
2390
|
pending.finish(void 0);
|
|
2212
2391
|
throw Error("question no longer pending");
|
|
2213
2392
|
}
|
|
2214
2393
|
this.#suppress(late.key, late.callId);
|
|
2215
2394
|
pending.finish(void 0);
|
|
2216
|
-
if (await
|
|
2395
|
+
if (await service2.answer(late.agent, late.callId, { answers: answer.answers }) !== true) throw Error("question no longer pending");
|
|
2217
2396
|
}
|
|
2218
|
-
async open(
|
|
2219
|
-
const key = this.aggregator.sessionKey(
|
|
2397
|
+
async open(ref2) {
|
|
2398
|
+
const key = this.aggregator.sessionKey(ref2);
|
|
2220
2399
|
if (key === void 0 || this.#closed) throw Error("session unavailable");
|
|
2221
2400
|
const captured = [...this.#pending.values()].filter((item) => item.key === key);
|
|
2222
2401
|
await this.openSession(key);
|
|
2223
|
-
if (this.#closed || this.aggregator.sessionKey(
|
|
2402
|
+
if (this.#closed || this.aggregator.sessionKey(ref2) !== key) return;
|
|
2224
2403
|
for (const pending of captured) {
|
|
2225
2404
|
if (this.#pending.get(pending.request.requestId) !== pending) continue;
|
|
2226
2405
|
if (pending.deadline !== void 0 && Date.now() >= pending.deadline) continue;
|
|
@@ -2276,9 +2455,9 @@ var init_questions = __esm({
|
|
|
2276
2455
|
if (signal?.aborted) throw questionError("ASK_ABORTED");
|
|
2277
2456
|
const wait = record5(source?.wait), timed = wait?.timed === true;
|
|
2278
2457
|
const callId = validCallId(wait?.callId) ? wait.callId : void 0;
|
|
2279
|
-
const
|
|
2458
|
+
const service2 = this.#questionService();
|
|
2280
2459
|
if (source?.wait !== void 0 && (!wait || !callId || wait.timed !== void 0 && typeof wait.timed !== "boolean")) return next();
|
|
2281
|
-
if (timed && (!callId || !
|
|
2460
|
+
if (timed && (!callId || !service2 || !this.#liveRoot(source?.agent, key))) return next();
|
|
2282
2461
|
this.#suppressed.delete(key);
|
|
2283
2462
|
if (this.#pending.size >= MAX_PENDING) {
|
|
2284
2463
|
const late = [...this.#pending.values()].find((item) => item.late);
|
|
@@ -2324,7 +2503,7 @@ var init_questions = __esm({
|
|
|
2324
2503
|
}
|
|
2325
2504
|
void (async () => {
|
|
2326
2505
|
try {
|
|
2327
|
-
iterator =
|
|
2506
|
+
iterator = service2.attachWait(source.agent, callId, claim.signal)[Symbol.asyncIterator]();
|
|
2328
2507
|
const first = await iterator.next();
|
|
2329
2508
|
if (settled) return;
|
|
2330
2509
|
const remaining = record5(first.value)?.remainingMs;
|
|
@@ -2358,19 +2537,20 @@ var init_questions = __esm({
|
|
|
2358
2537
|
return answer ? { answers: answer.answers } : next();
|
|
2359
2538
|
}
|
|
2360
2539
|
#request(key, questions, wait) {
|
|
2540
|
+
if (Array.isArray(questions) && questions.some((item) => record5(record5(item)?.intent)?.kind === "approval")) throw Error("permission question belongs to DSH");
|
|
2361
2541
|
return parsePetInteraction({ interactionVersion: 1, requestId: randomUUID4(), sessionRef: this.aggregator.sessionInfo(key).ref, questions, ...wait ? { wait } : {} });
|
|
2362
2542
|
}
|
|
2363
2543
|
#questionService() {
|
|
2364
|
-
const
|
|
2365
|
-
return typeof
|
|
2544
|
+
const service2 = this.#service("userQuestions");
|
|
2545
|
+
return typeof service2?.attachWait === "function" && typeof service2.answer === "function" ? service2 : void 0;
|
|
2366
2546
|
}
|
|
2367
2547
|
#projectionService() {
|
|
2368
|
-
const
|
|
2369
|
-
return typeof
|
|
2548
|
+
const service2 = this.#service("sessionProjections");
|
|
2549
|
+
return typeof service2?.stateOf === "function" && typeof service2.onChanged === "function" ? service2 : void 0;
|
|
2370
2550
|
}
|
|
2371
2551
|
#directory() {
|
|
2372
|
-
const
|
|
2373
|
-
return typeof
|
|
2552
|
+
const service2 = this.#service("agents");
|
|
2553
|
+
return typeof service2?.get === "function" && typeof service2.roots === "function" ? service2 : void 0;
|
|
2374
2554
|
}
|
|
2375
2555
|
#service(name2) {
|
|
2376
2556
|
try {
|
|
@@ -2388,9 +2568,9 @@ var init_questions = __esm({
|
|
|
2388
2568
|
}
|
|
2389
2569
|
}
|
|
2390
2570
|
#lateQuestions() {
|
|
2391
|
-
const
|
|
2392
|
-
if (!
|
|
2393
|
-
const
|
|
2571
|
+
const service2 = this.#questionService(), projections = this.#projectionService(), directory = this.#directory();
|
|
2572
|
+
if (!service2 || !projections || !directory) return [];
|
|
2573
|
+
const result2 = [];
|
|
2394
2574
|
try {
|
|
2395
2575
|
for (const agent of directory.roots().slice(0, 128)) {
|
|
2396
2576
|
const key = record5(agent)?.session;
|
|
@@ -2401,17 +2581,17 @@ var init_questions = __esm({
|
|
|
2401
2581
|
const item = record5(raw);
|
|
2402
2582
|
if (item?.state !== "continued" || !validCallId(item.callId)) continue;
|
|
2403
2583
|
try {
|
|
2404
|
-
|
|
2584
|
+
result2.push({ agent, key, callId: item.callId, request: this.#request(key, item.questions, { state: "continued" }) });
|
|
2405
2585
|
} catch {
|
|
2406
2586
|
continue;
|
|
2407
2587
|
}
|
|
2408
|
-
if (
|
|
2588
|
+
if (result2.length >= MAX_PENDING) return result2;
|
|
2409
2589
|
}
|
|
2410
2590
|
}
|
|
2411
2591
|
} catch {
|
|
2412
2592
|
return [];
|
|
2413
2593
|
}
|
|
2414
|
-
return
|
|
2594
|
+
return result2;
|
|
2415
2595
|
}
|
|
2416
2596
|
#currentLate(late) {
|
|
2417
2597
|
return !this.#closed && this.#connected && this.#enabled && !this.#suppressed.get(late.key)?.has(late.callId) && this.#lateQuestions().some((item) => sameLate(item, late));
|
|
@@ -2454,7 +2634,7 @@ function opaqueRef(value) {
|
|
|
2454
2634
|
function sessionIdentity(value) {
|
|
2455
2635
|
return typeof value === "string" && value.length > 0 && value.length <= 256 && !Array.from(value).some((character) => character.charCodeAt(0) < 32);
|
|
2456
2636
|
}
|
|
2457
|
-
function
|
|
2637
|
+
function object2(value) {
|
|
2458
2638
|
return value && typeof value === "object" && !Array.isArray(value) ? value : void 0;
|
|
2459
2639
|
}
|
|
2460
2640
|
var NAVIGATION_CHANNEL;
|
|
@@ -2488,7 +2668,7 @@ var init_desktop_navigation = __esm({
|
|
|
2488
2668
|
request(sessionId) {
|
|
2489
2669
|
this.#pending?.finish(false);
|
|
2490
2670
|
let pending;
|
|
2491
|
-
const
|
|
2671
|
+
const result2 = new Promise((resolve2, reject2) => {
|
|
2492
2672
|
const timer = setTimeout(() => pending.finish(false), DESKTOP_NAVIGATION_TIMEOUT_MS);
|
|
2493
2673
|
timer.unref?.();
|
|
2494
2674
|
pending = {
|
|
@@ -2504,13 +2684,13 @@ var init_desktop_navigation = __esm({
|
|
|
2504
2684
|
};
|
|
2505
2685
|
this.#pending = pending;
|
|
2506
2686
|
});
|
|
2507
|
-
void
|
|
2687
|
+
void result2.catch(() => void 0);
|
|
2508
2688
|
if (this.#closed) pending.finish(false);
|
|
2509
2689
|
else this.#deliver();
|
|
2510
|
-
return { result, cancel: () => pending.finish(false) };
|
|
2690
|
+
return { result: result2, cancel: () => pending.finish(false) };
|
|
2511
2691
|
}
|
|
2512
2692
|
async handle(endpoint, payload, signal, peer) {
|
|
2513
|
-
const row =
|
|
2693
|
+
const row = object2(payload);
|
|
2514
2694
|
if (this.#closed || signal.aborted || !row || !opaqueRef(row.clientRef) || !peer) return unavailable();
|
|
2515
2695
|
if (endpoint === "listen" && Object.keys(row).length === 1) return this.#listen(row.clientRef, peer, signal);
|
|
2516
2696
|
const owner = this.#owner;
|
|
@@ -2586,7 +2766,7 @@ var init_desktop_navigation = __esm({
|
|
|
2586
2766
|
// src/navigation.ts
|
|
2587
2767
|
import { spawn as spawn2 } from "node:child_process";
|
|
2588
2768
|
function createDshNavigator(context, openUrl = openLocalBrowser) {
|
|
2589
|
-
if (
|
|
2769
|
+
if (object2(context.get?.("profileContext"))?.name !== "desktop") {
|
|
2590
2770
|
return { open: (key) => openDshSession(context, key, openUrl), close: async () => void 0 };
|
|
2591
2771
|
}
|
|
2592
2772
|
let closed = false;
|
|
@@ -2594,7 +2774,7 @@ function createDshNavigator(context, openUrl = openLocalBrowser) {
|
|
|
2594
2774
|
let binding;
|
|
2595
2775
|
const attach = (scope) => {
|
|
2596
2776
|
if (closed) return;
|
|
2597
|
-
const rpc =
|
|
2777
|
+
const rpc = object2(object2(scope.get?.("connection"))?.rpc);
|
|
2598
2778
|
if (typeof rpc?.handle !== "function") throw Error("DSH Desktop navigation contract unavailable");
|
|
2599
2779
|
const broker = new DesktopNavigationBroker();
|
|
2600
2780
|
const remove = rpc.handle(NAVIGATION_CHANNEL, (endpoint, payload, signal, peer) => broker.handle(endpoint, payload, signal, peer));
|
|
@@ -2673,11 +2853,11 @@ async function openDshSession(context, key, openUrl = openLocalBrowser) {
|
|
|
2673
2853
|
await openUrl(target.href);
|
|
2674
2854
|
}
|
|
2675
2855
|
function browserEnvironment(source) {
|
|
2676
|
-
const
|
|
2856
|
+
const result2 = {};
|
|
2677
2857
|
for (const name2 of ["PATH", "HOME", "LANG", "LC_ALL", "DISPLAY", "WAYLAND_DISPLAY", "XDG_RUNTIME_DIR", "XDG_CURRENT_DESKTOP", "DBUS_SESSION_BUS_ADDRESS", "WSL_INTEROP", "WSL_DISTRO_NAME", "SYSTEMROOT", "SystemRoot", "TEMP", "TMP"]) {
|
|
2678
|
-
if (source[name2] !== void 0)
|
|
2858
|
+
if (source[name2] !== void 0) result2[name2] = source[name2];
|
|
2679
2859
|
}
|
|
2680
|
-
return
|
|
2860
|
+
return result2;
|
|
2681
2861
|
}
|
|
2682
2862
|
function browserWorkerScript(openModuleUrl) {
|
|
2683
2863
|
return `delete process.env.ELECTRON_RUN_AS_NODE;
|
|
@@ -2729,12 +2909,423 @@ var init_navigation = __esm({
|
|
|
2729
2909
|
}
|
|
2730
2910
|
});
|
|
2731
2911
|
|
|
2912
|
+
// src/session-control.ts
|
|
2913
|
+
import { randomUUID as randomUUID6 } from "node:crypto";
|
|
2914
|
+
function result(status, reason) {
|
|
2915
|
+
return { status, ...reason ? { reason } : {} };
|
|
2916
|
+
}
|
|
2917
|
+
var ACK_TIMEOUT_MS, ACTIVITY_FRAME_INTERVAL_MS, SessionControl;
|
|
2918
|
+
var init_session_control = __esm({
|
|
2919
|
+
"src/session-control.ts"() {
|
|
2920
|
+
"use strict";
|
|
2921
|
+
init_src();
|
|
2922
|
+
ACK_TIMEOUT_MS = 4e3;
|
|
2923
|
+
ACTIVITY_FRAME_INTERVAL_MS = 1e3;
|
|
2924
|
+
SessionControl = class {
|
|
2925
|
+
constructor(openSession) {
|
|
2926
|
+
this.openSession = openSession;
|
|
2927
|
+
}
|
|
2928
|
+
openSession;
|
|
2929
|
+
desktop;
|
|
2930
|
+
connected = false;
|
|
2931
|
+
owner;
|
|
2932
|
+
sequence = 0;
|
|
2933
|
+
closed = false;
|
|
2934
|
+
pending = /* @__PURE__ */ new Map();
|
|
2935
|
+
listeners = /* @__PURE__ */ new Set();
|
|
2936
|
+
availability = /* @__PURE__ */ new Set();
|
|
2937
|
+
get active() {
|
|
2938
|
+
return this.owner !== void 0;
|
|
2939
|
+
}
|
|
2940
|
+
get enabled() {
|
|
2941
|
+
return !this.closed && this.desktop?.enabled === true;
|
|
2942
|
+
}
|
|
2943
|
+
get ready() {
|
|
2944
|
+
return this.enabled && this.connected && this.desktop?.available === true && !!this.desktop.model;
|
|
2945
|
+
}
|
|
2946
|
+
subscribe(listener) {
|
|
2947
|
+
this.listeners.add(listener);
|
|
2948
|
+
return () => this.listeners.delete(listener);
|
|
2949
|
+
}
|
|
2950
|
+
onChange(listener) {
|
|
2951
|
+
this.availability.add(listener);
|
|
2952
|
+
return () => this.availability.delete(listener);
|
|
2953
|
+
}
|
|
2954
|
+
report(value) {
|
|
2955
|
+
const report = parseControlDesktopReport(value), previous = this.desktop;
|
|
2956
|
+
this.desktop = report;
|
|
2957
|
+
if (this.owner) {
|
|
2958
|
+
if (!report.enabled) this.release("disabled");
|
|
2959
|
+
else if (!report.available) this.release("unavailable");
|
|
2960
|
+
else if (previous?.model?.revision !== report.model?.revision) this.release("model-changed");
|
|
2961
|
+
}
|
|
2962
|
+
this.changed();
|
|
2963
|
+
}
|
|
2964
|
+
setConnected(value) {
|
|
2965
|
+
this.connected = value;
|
|
2966
|
+
if (!value) {
|
|
2967
|
+
this.release("disconnected");
|
|
2968
|
+
this.desktop = void 0;
|
|
2969
|
+
}
|
|
2970
|
+
this.changed();
|
|
2971
|
+
}
|
|
2972
|
+
inspect(session) {
|
|
2973
|
+
return {
|
|
2974
|
+
enabled: this.enabled,
|
|
2975
|
+
available: this.ready,
|
|
2976
|
+
controlledBySelf: !!this.owner && this.owner.session === session,
|
|
2977
|
+
busy: this.active && this.owner?.session !== session,
|
|
2978
|
+
leaseMs: CONTROL_LEASE_MS,
|
|
2979
|
+
...this.desktop?.model ? { model: { title: this.desktop.model.title, animations: this.desktop.model.animations.map((item) => ({ ...item })) } } : {},
|
|
2980
|
+
...this.desktop ? { preferences: { ...this.desktop.preferences } } : {}
|
|
2981
|
+
};
|
|
2982
|
+
}
|
|
2983
|
+
async acquire(session, title, signal) {
|
|
2984
|
+
if (signal.aborted) return result("interrupted", "cancelled");
|
|
2985
|
+
if (!this.ready) return result("rejected", this.enabled ? "unavailable" : "disabled");
|
|
2986
|
+
if (this.owner && this.owner.session !== session) return result("rejected", "busy");
|
|
2987
|
+
if ([...this.pending.values()].some((item) => item.frame.kind === "acquire")) return result("rejected", "busy");
|
|
2988
|
+
if (!this.owner) {
|
|
2989
|
+
this.owner = { session, leaseId: randomUUID6(), title: sanitizeBubbleText(title, 60).slice(0, 120) || "DSH session", timer: setTimeout(() => this.release("expired"), CONTROL_LEASE_MS), activityTimer: void 0, lastActivityFrameAt: -Infinity, lastMessageSequence: -1 };
|
|
2990
|
+
this.owner.timer.unref();
|
|
2991
|
+
this.changed();
|
|
2992
|
+
}
|
|
2993
|
+
this.renew();
|
|
2994
|
+
const leaseId = this.owner.leaseId;
|
|
2995
|
+
const outcome = await this.send({ kind: "acquire", sessionTitle: this.owner.title }, signal, ACK_TIMEOUT_MS);
|
|
2996
|
+
if (outcome.status === "rejected" && this.owner?.leaseId === leaseId) this.release("unavailable");
|
|
2997
|
+
return outcome;
|
|
2998
|
+
}
|
|
2999
|
+
relinquish(session) {
|
|
3000
|
+
if (!this.owner) return result("completed");
|
|
3001
|
+
if (this.owner.session !== session) return result("rejected", "not-owner");
|
|
3002
|
+
this.release("user");
|
|
3003
|
+
return result("completed");
|
|
3004
|
+
}
|
|
3005
|
+
renewLease(session, title, signal) {
|
|
3006
|
+
const denied = this.authorize(session, signal);
|
|
3007
|
+
return denied ? Promise.resolve(denied) : this.acquire(session, title, signal);
|
|
3008
|
+
}
|
|
3009
|
+
async perform(session, value, signal) {
|
|
3010
|
+
const command = parseControlPerform(value), denied = this.authorize(session, signal);
|
|
3011
|
+
if (denied) return denied;
|
|
3012
|
+
const animation = command.animation === void 0 ? void 0 : this.desktop?.model?.animations.find((item) => item.name === command.animation);
|
|
3013
|
+
if (command.animation !== void 0 && !animation) return result("rejected", "unknown-animation");
|
|
3014
|
+
if (animation && animation.durationMs > 3e4) return result("rejected", "animation-too-long");
|
|
3015
|
+
if (command.text !== void 0 && !this.desktop?.preferences.bubblesVisible) return result("rejected", "user-preference");
|
|
3016
|
+
this.interruptKind("perform");
|
|
3017
|
+
this.renew();
|
|
3018
|
+
return this.send({ kind: "perform", ...command }, signal, Math.max(animation?.durationMs ?? 0, command.durationMs ?? (command.text ? 6e3 : 0)) + 5e3);
|
|
3019
|
+
}
|
|
3020
|
+
async stage(session, value, signal) {
|
|
3021
|
+
const command = parseControlStage(value), denied = this.authorize(session, signal);
|
|
3022
|
+
if (denied) return denied;
|
|
3023
|
+
if ([...this.pending.values()].some((item) => item.frame.kind === "stage")) return result("rejected", "busy");
|
|
3024
|
+
this.renew();
|
|
3025
|
+
return this.send({ kind: "stage", ...command }, signal, ACK_TIMEOUT_MS);
|
|
3026
|
+
}
|
|
3027
|
+
acknowledge(value) {
|
|
3028
|
+
const ack = parseControlAck(value), pending = this.pending.get(ack.commandId);
|
|
3029
|
+
if (!pending || ack.leaseId !== this.owner?.leaseId || ack.leaseId !== pending.frame.leaseId) return;
|
|
3030
|
+
if (ack.status === "started") {
|
|
3031
|
+
pending.started = true;
|
|
3032
|
+
return;
|
|
3033
|
+
}
|
|
3034
|
+
pending.finish({ status: ack.status, ...ack.reason ? { reason: ack.reason } : {} });
|
|
3035
|
+
}
|
|
3036
|
+
userRelease(value) {
|
|
3037
|
+
const request = parseControlRelease(value);
|
|
3038
|
+
if (request.leaseId === this.owner?.leaseId) this.release(request.reason);
|
|
3039
|
+
}
|
|
3040
|
+
async open(value) {
|
|
3041
|
+
const row = value && typeof value === "object" && !Array.isArray(value) ? value : void 0;
|
|
3042
|
+
if (!row || Object.keys(row).length !== 1 || !isOpaqueRef(row.leaseId) || row.leaseId !== this.owner?.leaseId || !this.openSession) throw Error("session control unavailable");
|
|
3043
|
+
await this.openSession(this.owner.session);
|
|
3044
|
+
}
|
|
3045
|
+
/** Called only for actual assistant output; no content or real session ID crosses the wire. */
|
|
3046
|
+
activity(session, messageSequence) {
|
|
3047
|
+
const owner = this.owner;
|
|
3048
|
+
if (!owner || owner.session !== session || !this.ready) return;
|
|
3049
|
+
if (messageSequence !== void 0) {
|
|
3050
|
+
if (!Number.isSafeInteger(messageSequence) || messageSequence < 0 || messageSequence <= owner.lastMessageSequence) return;
|
|
3051
|
+
owner.lastMessageSequence = messageSequence;
|
|
3052
|
+
}
|
|
3053
|
+
this.renew();
|
|
3054
|
+
const elapsed = performance.now() - owner.lastActivityFrameAt;
|
|
3055
|
+
const emit = () => {
|
|
3056
|
+
owner.activityTimer = void 0;
|
|
3057
|
+
if (this.owner !== owner || !this.ready) return;
|
|
3058
|
+
owner.lastActivityFrameAt = performance.now();
|
|
3059
|
+
this.emit({ kind: "activity" });
|
|
3060
|
+
};
|
|
3061
|
+
if (elapsed >= ACTIVITY_FRAME_INTERVAL_MS) {
|
|
3062
|
+
clearTimeout(owner.activityTimer);
|
|
3063
|
+
emit();
|
|
3064
|
+
} else if (!owner.activityTimer) {
|
|
3065
|
+
owner.activityTimer = setTimeout(emit, ACTIVITY_FRAME_INTERVAL_MS - elapsed);
|
|
3066
|
+
owner.activityTimer.unref();
|
|
3067
|
+
}
|
|
3068
|
+
}
|
|
3069
|
+
releaseSession(session, reason = "session-ended") {
|
|
3070
|
+
if (this.owner?.session === session) this.release(reason);
|
|
3071
|
+
}
|
|
3072
|
+
close() {
|
|
3073
|
+
if (this.closed) return;
|
|
3074
|
+
this.release("unavailable");
|
|
3075
|
+
this.closed = true;
|
|
3076
|
+
this.desktop = void 0;
|
|
3077
|
+
this.listeners.clear();
|
|
3078
|
+
this.changed();
|
|
3079
|
+
this.availability.clear();
|
|
3080
|
+
}
|
|
3081
|
+
authorize(session, signal) {
|
|
3082
|
+
if (signal.aborted) {
|
|
3083
|
+
if (this.owner?.session === session) this.release("cancelled");
|
|
3084
|
+
return result("interrupted", "cancelled");
|
|
3085
|
+
}
|
|
3086
|
+
if (!this.ready) return result("rejected", this.enabled ? "unavailable" : "disabled");
|
|
3087
|
+
if (!this.owner || this.owner.session !== session) return result("rejected", this.owner ? "busy" : "not-owner");
|
|
3088
|
+
return void 0;
|
|
3089
|
+
}
|
|
3090
|
+
renew() {
|
|
3091
|
+
if (this.owner) {
|
|
3092
|
+
clearTimeout(this.owner.timer);
|
|
3093
|
+
this.owner.timer = setTimeout(() => this.release("expired"), CONTROL_LEASE_MS);
|
|
3094
|
+
this.owner.timer.unref();
|
|
3095
|
+
}
|
|
3096
|
+
}
|
|
3097
|
+
changed() {
|
|
3098
|
+
for (const listener of this.availability) {
|
|
3099
|
+
try {
|
|
3100
|
+
listener();
|
|
3101
|
+
} catch {
|
|
3102
|
+
}
|
|
3103
|
+
}
|
|
3104
|
+
}
|
|
3105
|
+
emit(command) {
|
|
3106
|
+
if (!this.owner) throw Error("session control unavailable");
|
|
3107
|
+
const frame = parseControlFrame({ controlVersion: 1, sequence: ++this.sequence, leaseId: this.owner.leaseId, commandId: randomUUID6(), leaseMs: CONTROL_LEASE_MS, ...command });
|
|
3108
|
+
for (const listener of this.listeners) {
|
|
3109
|
+
try {
|
|
3110
|
+
listener(frame);
|
|
3111
|
+
} catch {
|
|
3112
|
+
}
|
|
3113
|
+
}
|
|
3114
|
+
return frame;
|
|
3115
|
+
}
|
|
3116
|
+
send(command, signal, duration) {
|
|
3117
|
+
if (!this.owner || signal.aborted) return Promise.resolve(result("interrupted", "cancelled"));
|
|
3118
|
+
return new Promise((resolve2) => {
|
|
3119
|
+
const frame = parseControlFrame({ controlVersion: 1, sequence: ++this.sequence, leaseId: this.owner.leaseId, commandId: randomUUID6(), leaseMs: CONTROL_LEASE_MS, ...command });
|
|
3120
|
+
let done = false;
|
|
3121
|
+
const abort = () => this.release("cancelled");
|
|
3122
|
+
const finish = (outcome) => {
|
|
3123
|
+
if (done) return;
|
|
3124
|
+
done = true;
|
|
3125
|
+
clearTimeout(pending.timer);
|
|
3126
|
+
clearTimeout(startTimer);
|
|
3127
|
+
signal.removeEventListener("abort", abort);
|
|
3128
|
+
this.pending.delete(frame.commandId);
|
|
3129
|
+
resolve2(outcome);
|
|
3130
|
+
};
|
|
3131
|
+
const timeout = () => {
|
|
3132
|
+
if (done) return;
|
|
3133
|
+
this.emit({ kind: "cancel", targetId: frame.commandId });
|
|
3134
|
+
finish(result("rejected", "timeout"));
|
|
3135
|
+
if (frame.kind === "stage" || frame.kind === "acquire") this.release("unavailable");
|
|
3136
|
+
};
|
|
3137
|
+
const pending = { frame, started: false, timer: setTimeout(timeout, Math.min(35e3, Math.max(ACK_TIMEOUT_MS, duration))), finish };
|
|
3138
|
+
const startTimer = setTimeout(() => {
|
|
3139
|
+
if (!pending.started) timeout();
|
|
3140
|
+
}, ACK_TIMEOUT_MS);
|
|
3141
|
+
pending.timer.unref();
|
|
3142
|
+
startTimer.unref();
|
|
3143
|
+
this.pending.set(frame.commandId, pending);
|
|
3144
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
3145
|
+
for (const listener of this.listeners) {
|
|
3146
|
+
try {
|
|
3147
|
+
listener(frame);
|
|
3148
|
+
} catch {
|
|
3149
|
+
}
|
|
3150
|
+
}
|
|
3151
|
+
});
|
|
3152
|
+
}
|
|
3153
|
+
interruptKind(kind) {
|
|
3154
|
+
for (const pending of this.pending.values()) if (pending.frame.kind === kind) {
|
|
3155
|
+
this.emit({ kind: "cancel", targetId: pending.frame.commandId });
|
|
3156
|
+
pending.finish(result("interrupted", "superseded"));
|
|
3157
|
+
}
|
|
3158
|
+
}
|
|
3159
|
+
release(reason) {
|
|
3160
|
+
if (!this.owner) return;
|
|
3161
|
+
clearTimeout(this.owner.timer);
|
|
3162
|
+
clearTimeout(this.owner.activityTimer);
|
|
3163
|
+
for (const pending of this.pending.values()) pending.finish(result("interrupted", reason));
|
|
3164
|
+
this.emit({ kind: "release", reason });
|
|
3165
|
+
this.owner = void 0;
|
|
3166
|
+
this.changed();
|
|
3167
|
+
}
|
|
3168
|
+
};
|
|
3169
|
+
}
|
|
3170
|
+
});
|
|
3171
|
+
|
|
3172
|
+
// src/session-tools.ts
|
|
3173
|
+
function registerSessionTools(context, control) {
|
|
3174
|
+
const disposers = [];
|
|
3175
|
+
const output = { schema: { type: "object", additionalProperties: true }, render: (_args, value) => [{ type: "text", text: JSON.stringify(value) }] };
|
|
3176
|
+
const wrap = (name2, description, properties, required, execute) => ({
|
|
3177
|
+
name: name2,
|
|
3178
|
+
description,
|
|
3179
|
+
parameters: { type: "object", properties, required, additionalProperties: false },
|
|
3180
|
+
output,
|
|
3181
|
+
timeoutMs: 4e4,
|
|
3182
|
+
async execute(args, execution) {
|
|
3183
|
+
const exec = actor(execution), row = record7(args);
|
|
3184
|
+
if (!row || Object.keys(row).some((key) => !(key in properties)) || required.some((key) => !(key in row))) throw Error("Invalid StarLite arguments");
|
|
3185
|
+
if (exec.signal.aborted) return { status: "interrupted", reason: "cancelled" };
|
|
3186
|
+
if (name2 !== "starlite_inspect" && !control.enabled) return { status: "rejected", reason: "disabled" };
|
|
3187
|
+
return execute(row, exec);
|
|
3188
|
+
}
|
|
3189
|
+
});
|
|
3190
|
+
const tools = [
|
|
3191
|
+
wrap("starlite_inspect", "Inspect StarLite availability, the current model's exact animation names, preferences and session ownership. Read before acquiring or choosing animations.", {}, [], (_args, exec) => control.inspect(exec.agent.session)),
|
|
3192
|
+
wrap("starlite_control", "Acquire, renew or release temporary exclusive control of StarLite for this DSH session. Only use for user-requested character interaction. Release when done.", { action: { type: "string", enum: ["acquire", "renew", "release"] } }, ["action"], (args, exec) => {
|
|
3193
|
+
if (args.action === "release") return control.relinquish(exec.agent.session);
|
|
3194
|
+
if (args.action !== "acquire" && args.action !== "renew") throw Error("Invalid StarLite action");
|
|
3195
|
+
if (args.action === "renew") return control.renewLease(exec.agent.session, sessionTitle(context, exec.agent.session), exec.signal);
|
|
3196
|
+
return control.acquire(exec.agent.session, sessionTitle(context, exec.agent.session), exec.signal);
|
|
3197
|
+
}),
|
|
3198
|
+
wrap("starlite_perform", "During ownership, play one named animation and/or show short plain-text speech atomically. Await actual completion. A new performance replaces the old one. stop:true stops it. Does not override user bubble/privacy settings.", {
|
|
3199
|
+
animation: { type: "string", description: "Exact catalog name (1\u2013160 characters)." },
|
|
3200
|
+
text: { type: "string", description: "Plain text (1\u20131000 characters)." },
|
|
3201
|
+
durationMs: { type: "number", description: "Bubble lifetime: 1000\u201330000 milliseconds; only with text." },
|
|
3202
|
+
stop: { type: "boolean", const: true }
|
|
3203
|
+
}, [], (args, exec) => control.perform(exec.agent.session, args, exec.signal)),
|
|
3204
|
+
wrap("starlite_stage", "During ownership, temporarily position or resize the character on its current monitor, or adjust cursor following. All changes revert on release; no OS control.", {
|
|
3205
|
+
position: { type: "object", description: "Normalized current-monitor position; both axes must be between 0 and 1.", properties: { x: { type: "number" }, y: { type: "number" } }, required: ["x", "y"], additionalProperties: false },
|
|
3206
|
+
scale: { type: "number", description: "Temporary model scale: 0.5\u20131.5." },
|
|
3207
|
+
pointerEnabled: { type: "boolean" }
|
|
3208
|
+
}, [], (args, exec) => control.stage(exec.agent.session, args, exec.signal))
|
|
3209
|
+
];
|
|
3210
|
+
let skillDispose;
|
|
3211
|
+
let failed = false;
|
|
3212
|
+
let registrationEpoch = 0;
|
|
3213
|
+
const clear = () => {
|
|
3214
|
+
registrationEpoch++;
|
|
3215
|
+
try {
|
|
3216
|
+
skillDispose?.();
|
|
3217
|
+
} catch {
|
|
3218
|
+
}
|
|
3219
|
+
skillDispose = void 0;
|
|
3220
|
+
for (const dispose of disposers.splice(0).reverse()) {
|
|
3221
|
+
try {
|
|
3222
|
+
dispose();
|
|
3223
|
+
} catch {
|
|
3224
|
+
}
|
|
3225
|
+
}
|
|
3226
|
+
};
|
|
3227
|
+
const sync = () => {
|
|
3228
|
+
if (!control.enabled) {
|
|
3229
|
+
clear();
|
|
3230
|
+
failed = false;
|
|
3231
|
+
return;
|
|
3232
|
+
}
|
|
3233
|
+
if (failed) return;
|
|
3234
|
+
const registry = service(context, "tools");
|
|
3235
|
+
if (typeof registry?.register !== "function") return;
|
|
3236
|
+
if (!disposers.length) try {
|
|
3237
|
+
const lookup = registry.get;
|
|
3238
|
+
if (typeof lookup === "function" && tools.some((tool) => lookup.call(registry, tool.name))) throw Error("tool name already registered");
|
|
3239
|
+
const epoch = ++registrationEpoch;
|
|
3240
|
+
for (const tool of tools) {
|
|
3241
|
+
const definition = { ...tool, execute: (args, exec) => epoch === registrationEpoch ? tool.execute(args, exec) : Promise.resolve({ status: "rejected", reason: control.enabled ? "stale" : "disabled" }) };
|
|
3242
|
+
const dispose = registry.register.call(registry, definition, { when: (execution) => {
|
|
3243
|
+
try {
|
|
3244
|
+
actor(execution);
|
|
3245
|
+
return epoch === registrationEpoch && control.enabled;
|
|
3246
|
+
} catch {
|
|
3247
|
+
return false;
|
|
3248
|
+
}
|
|
3249
|
+
} });
|
|
3250
|
+
if (typeof dispose !== "function") throw Error("unsupported tool lifecycle");
|
|
3251
|
+
disposers.push(dispose);
|
|
3252
|
+
}
|
|
3253
|
+
} catch {
|
|
3254
|
+
clear();
|
|
3255
|
+
failed = true;
|
|
3256
|
+
context.logger?.warn?.("StarLite session tools are unavailable in this DSH version; normal companion behavior is unchanged.");
|
|
3257
|
+
return;
|
|
3258
|
+
}
|
|
3259
|
+
if (skillDispose) return;
|
|
3260
|
+
const skills = service(context, "skills");
|
|
3261
|
+
if (typeof skills?.register !== "function") return;
|
|
3262
|
+
try {
|
|
3263
|
+
const dispose = skills.register.call(skills, skill);
|
|
3264
|
+
if (typeof dispose === "function") skillDispose = dispose;
|
|
3265
|
+
} catch {
|
|
3266
|
+
}
|
|
3267
|
+
};
|
|
3268
|
+
const unsubscribe = control.onChange(sync);
|
|
3269
|
+
sync();
|
|
3270
|
+
return () => {
|
|
3271
|
+
unsubscribe();
|
|
3272
|
+
clear();
|
|
3273
|
+
};
|
|
3274
|
+
}
|
|
3275
|
+
function actor(value) {
|
|
3276
|
+
const row = record7(value), agent = record7(row?.agent), signal = row?.signal;
|
|
3277
|
+
if (!agent || agent.session === void 0 || agent.session === null || !signal || typeof signal.aborted !== "boolean" || typeof signal.addEventListener !== "function" || typeof signal.removeEventListener !== "function") throw Error("StarLite requires a live DSH execution session");
|
|
3278
|
+
return { agent: { session: agent.session }, signal };
|
|
3279
|
+
}
|
|
3280
|
+
function sessionTitle(context, session) {
|
|
3281
|
+
const titles = service(context, "sessionTitle");
|
|
3282
|
+
try {
|
|
3283
|
+
return typeof titles?.get === "function" ? record7(titles.get.call(titles, session))?.title : void 0;
|
|
3284
|
+
} catch {
|
|
3285
|
+
return void 0;
|
|
3286
|
+
}
|
|
3287
|
+
}
|
|
3288
|
+
function service(context, name2) {
|
|
3289
|
+
try {
|
|
3290
|
+
return record7(context.get?.(name2));
|
|
3291
|
+
} catch {
|
|
3292
|
+
return void 0;
|
|
3293
|
+
}
|
|
3294
|
+
}
|
|
3295
|
+
function record7(value) {
|
|
3296
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : void 0;
|
|
3297
|
+
}
|
|
3298
|
+
var skill;
|
|
3299
|
+
var init_session_tools = __esm({
|
|
3300
|
+
"src/session-tools.ts"() {
|
|
3301
|
+
"use strict";
|
|
3302
|
+
init_session_control();
|
|
3303
|
+
skill = {
|
|
3304
|
+
name: "starlite-character",
|
|
3305
|
+
source: "runtime",
|
|
3306
|
+
description: "Use the user's StarLite desktop character for an explicitly requested embodied conversation: named animations, short plain-text speech and temporary stage placement.",
|
|
3307
|
+
content: `# StarLite character interaction
|
|
3308
|
+
|
|
3309
|
+
Use this skill only when the user asks to interact through their StarLite character. Respect the user's existing persona; do not invent a new identity or claim that a model's visual likeness belongs to you.
|
|
3310
|
+
|
|
3311
|
+
1. Call starlite_inspect first. If disabled, ask the user to enable Session control in StarLite settings; do not change settings files or use shell commands to bypass it. If unavailable or busy, explain briefly and continue ordinary conversation.
|
|
3312
|
+
2. Call starlite_control with action acquire. Only the current DSH session owns the lease; never pass session identifiers. Control expires after five minutes without new assistant output from this session or an accepted control command. Your streamed replies and committed messages renew it automatically, including across turns; other sessions and connection heartbeats do not. Do not send keepalive messages or commands merely to prevent expiry.
|
|
3313
|
+
3. Use starlite_perform for one atomic gesture and/or bubble. animation must exactly match inspect's catalog. Keep speech concise, plain text, and suitable for a desktop bubble; a gesture without speech is often enough. durationMs is the bubble's 1000\u201330000 ms lifetime, not an animation-speed control. Each animation plays once. Await completed before choosing the next gesture; interrupted/stopped/rejected means it did not finish. Do not queue a script or flood the desktop with repetitive gestures. stop:true interrupts the current performance.
|
|
3314
|
+
4. Use starlite_stage sparingly and only for requested presentation changes: position x/y are 0\u20131 within the current monitor, scale is 0.5\u20131.5, pointerEnabled temporarily controls cursor following. These changes do not persist. Never move other windows or control the OS.
|
|
3315
|
+
5. When the user ends role interaction, call starlite_control with action release. On cancellation, model switch, hide, disconnection or expiry, ownership is revoked automatically and normal behavior resumes. Do not reacquire after the user exits unless asked again. Prefer release in a finally block if running a multi-command program.
|
|
3316
|
+
|
|
3317
|
+
The user's bubble/privacy choices remain authoritative: user-preference rejects combined speech+animation atomically. If speech is disabled, use animation-only when appropriate, and keep text in the DSH conversation. Pending questions and permission approvals stay in DSH during control. Never impersonate a tool approval, expose private tool results, send messages to external services, or request credentials through a character bubble. No microphone, speech synthesis, screen reading, external messages or paid request is performed by these tools.
|
|
3318
|
+
`
|
|
3319
|
+
};
|
|
3320
|
+
}
|
|
3321
|
+
});
|
|
3322
|
+
|
|
2732
3323
|
// src/adapter.ts
|
|
2733
3324
|
var adapter_exports = {};
|
|
2734
3325
|
__export(adapter_exports, {
|
|
2735
3326
|
startDshAdapter: () => startDshAdapter
|
|
2736
3327
|
});
|
|
2737
|
-
import { randomUUID as
|
|
3328
|
+
import { randomUUID as randomUUID7 } from "node:crypto";
|
|
2738
3329
|
async function startDshAdapter(context, dependencies = {}) {
|
|
2739
3330
|
if (!context || typeof context.on !== "function") {
|
|
2740
3331
|
throw new DshStartupError("incompatible-api");
|
|
@@ -2747,10 +3338,12 @@ async function startDshAdapter(context, dependencies = {}) {
|
|
|
2747
3338
|
const aggregator = new DshStateAggregator();
|
|
2748
3339
|
const billing = new DeepSeekBilling(context);
|
|
2749
3340
|
const questions = new QuestionBridge(aggregator, (key) => navigator.open(key), context);
|
|
3341
|
+
const control = new SessionControl((key) => navigator.open(key));
|
|
2750
3342
|
let server;
|
|
2751
3343
|
try {
|
|
2752
|
-
server = await (dependencies.startServer ?? startStatusServer)(aggregator, { billing, questions });
|
|
3344
|
+
server = await (dependencies.startServer ?? startStatusServer)(aggregator, { billing, questions, control });
|
|
2753
3345
|
} catch (error) {
|
|
3346
|
+
control.close();
|
|
2754
3347
|
questions.close();
|
|
2755
3348
|
billing.close();
|
|
2756
3349
|
await navigator.close();
|
|
@@ -2758,14 +3351,14 @@ async function startDshAdapter(context, dependencies = {}) {
|
|
|
2758
3351
|
}
|
|
2759
3352
|
const refreshTitle = (key) => {
|
|
2760
3353
|
try {
|
|
2761
|
-
const
|
|
2762
|
-
const value = typeof
|
|
3354
|
+
const service2 = asRecord2(context.get?.("sessionTitle"));
|
|
3355
|
+
const value = typeof service2?.get === "function" ? asRecord2(service2.get.call(service2, key)) : void 0;
|
|
2763
3356
|
aggregator.sessionInfo(key, typeof value?.title === "string" ? value.title : void 0);
|
|
2764
3357
|
} catch {
|
|
2765
3358
|
aggregator.sessionInfo(key);
|
|
2766
3359
|
}
|
|
2767
3360
|
};
|
|
2768
|
-
const disposers = [() => navigator.close()];
|
|
3361
|
+
const disposers = [() => navigator.close(), registerSessionTools(context, control)];
|
|
2769
3362
|
let approvalSequence = 0;
|
|
2770
3363
|
try {
|
|
2771
3364
|
subscribeRequired(context, disposers, "agent/status", (...arguments_) => {
|
|
@@ -2782,6 +3375,7 @@ async function startDshAdapter(context, dependencies = {}) {
|
|
|
2782
3375
|
const payload = asRecord2(arguments_[0]);
|
|
2783
3376
|
if (payload?.agent) {
|
|
2784
3377
|
const key = subjectKey(payload.agent);
|
|
3378
|
+
control.releaseSession(key);
|
|
2785
3379
|
questions.cancelSession(key);
|
|
2786
3380
|
refreshTitle(key);
|
|
2787
3381
|
aggregator.handleAgentError(key);
|
|
@@ -2790,8 +3384,10 @@ async function startDshAdapter(context, dependencies = {}) {
|
|
|
2790
3384
|
subscribeRequired(context, disposers, "agent/assistant-stream", (...arguments_) => {
|
|
2791
3385
|
const payload = asRecord2(arguments_[0]);
|
|
2792
3386
|
if (payload?.agent) {
|
|
2793
|
-
|
|
2794
|
-
|
|
3387
|
+
const key = subjectKey(payload.agent);
|
|
3388
|
+
refreshTitle(key);
|
|
3389
|
+
if (hasAssistantOutput(payload.frame)) control.activity(key);
|
|
3390
|
+
aggregator.handleAssistantStream(key, payload.frame);
|
|
2795
3391
|
}
|
|
2796
3392
|
});
|
|
2797
3393
|
subscribeRequired(context, disposers, "session/event", (...arguments_) => {
|
|
@@ -2800,7 +3396,16 @@ async function startDshAdapter(context, dependencies = {}) {
|
|
|
2800
3396
|
if (session && typeof event?.type === "string") {
|
|
2801
3397
|
const key = subjectKey(session);
|
|
2802
3398
|
refreshTitle(key);
|
|
2803
|
-
if (event.
|
|
3399
|
+
if (typeof event.seq === "number" && Number.isSafeInteger(event.seq) && event.seq >= 0) {
|
|
3400
|
+
if (event.type === "assistant/message" && hasAssistantMessage(event.data)) control.activity(key, event.seq);
|
|
3401
|
+
else if (event.type === "assistant/chunk" && hasAssistantOutput({ type: "chunk", chunk: asRecord2(event.data)?.chunk })) control.activity(key, event.seq);
|
|
3402
|
+
}
|
|
3403
|
+
if (event.type === "turn/end") {
|
|
3404
|
+
questions.cancelForeground(key);
|
|
3405
|
+
const reason = asRecord2(asRecord2(event.data)?.reason)?.kind;
|
|
3406
|
+
if (reason === "aborted") control.releaseSession(key, "cancelled");
|
|
3407
|
+
else if (reason === "error") control.releaseSession(key);
|
|
3408
|
+
}
|
|
2804
3409
|
aggregator.handleSessionEvent(subjectKey(session), event);
|
|
2805
3410
|
questions.refresh();
|
|
2806
3411
|
if (event.type === "assistant/message" || event.type === "assistant/attempt") billing.invalidate();
|
|
@@ -2835,6 +3440,7 @@ async function startDshAdapter(context, dependencies = {}) {
|
|
|
2835
3440
|
const payload = asRecord2(arguments_[0]);
|
|
2836
3441
|
if (payload?.agent) {
|
|
2837
3442
|
const key = subjectKey(payload.agent);
|
|
3443
|
+
control.releaseSession(key);
|
|
2838
3444
|
questions.cancelSession(key);
|
|
2839
3445
|
aggregator.disposeSession(key);
|
|
2840
3446
|
}
|
|
@@ -2850,6 +3456,7 @@ async function startDshAdapter(context, dependencies = {}) {
|
|
|
2850
3456
|
subscribeOptional(context, disposers, event, () => billing.invalidate(true));
|
|
2851
3457
|
}
|
|
2852
3458
|
} catch (error) {
|
|
3459
|
+
control.close();
|
|
2853
3460
|
questions.close();
|
|
2854
3461
|
billing.close();
|
|
2855
3462
|
await closeAdapter(disposers, server);
|
|
@@ -2859,7 +3466,7 @@ async function startDshAdapter(context, dependencies = {}) {
|
|
|
2859
3466
|
void (dependencies.launch ?? launchWindowsPet)({
|
|
2860
3467
|
port: server.port,
|
|
2861
3468
|
token: server.token,
|
|
2862
|
-
instanceId:
|
|
3469
|
+
instanceId: randomUUID7()
|
|
2863
3470
|
}, {
|
|
2864
3471
|
host,
|
|
2865
3472
|
signal: launchAbort.signal,
|
|
@@ -2881,14 +3488,15 @@ async function startDshAdapter(context, dependencies = {}) {
|
|
|
2881
3488
|
closed = true;
|
|
2882
3489
|
launchAbort.abort();
|
|
2883
3490
|
billing.close();
|
|
3491
|
+
control.close();
|
|
2884
3492
|
questions.close();
|
|
2885
3493
|
await closeAdapter(disposers, server);
|
|
2886
3494
|
};
|
|
2887
3495
|
}
|
|
2888
3496
|
function subscribeRequired(context, disposers, event, listener, options) {
|
|
2889
3497
|
try {
|
|
2890
|
-
const
|
|
2891
|
-
if (typeof
|
|
3498
|
+
const result2 = context.on(event, listener, options);
|
|
3499
|
+
if (typeof result2 === "function") disposers.push(result2);
|
|
2892
3500
|
} catch {
|
|
2893
3501
|
throw new DshStartupError("incompatible-api");
|
|
2894
3502
|
}
|
|
@@ -2905,8 +3513,25 @@ async function closeAdapter(disposers, server) {
|
|
|
2905
3513
|
await Promise.allSettled(tasks);
|
|
2906
3514
|
}
|
|
2907
3515
|
function subjectKey(subject) {
|
|
2908
|
-
const
|
|
2909
|
-
return
|
|
3516
|
+
const record8 = asRecord2(subject);
|
|
3517
|
+
return record8?.session ?? subject;
|
|
3518
|
+
}
|
|
3519
|
+
function hasAssistantOutput(value) {
|
|
3520
|
+
const frame = asRecord2(value);
|
|
3521
|
+
if (frame?.type !== "chunk") return false;
|
|
3522
|
+
const chunk = asRecord2(frame.chunk);
|
|
3523
|
+
if (chunk?.type === "text-delta" || chunk?.type === "reasoning-delta") return nonempty(chunk.text);
|
|
3524
|
+
return chunk?.type === "tool-call-delta" && (nonempty(chunk.argumentsDelta) || nonempty(chunk.name));
|
|
3525
|
+
}
|
|
3526
|
+
function hasAssistantMessage(value) {
|
|
3527
|
+
const content = asRecord2(asRecord2(value)?.message)?.content;
|
|
3528
|
+
return Array.isArray(content) && content.some((item) => {
|
|
3529
|
+
const block = asRecord2(item);
|
|
3530
|
+
return (block?.type === "text" || block?.type === "reasoning") && nonempty(block.text) || block?.type === "tool-call" && nonempty(block.name);
|
|
3531
|
+
});
|
|
3532
|
+
}
|
|
3533
|
+
function nonempty(value) {
|
|
3534
|
+
return typeof value === "string" && value.length > 0;
|
|
2910
3535
|
}
|
|
2911
3536
|
function asRecord2(value) {
|
|
2912
3537
|
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : void 0;
|
|
@@ -2921,6 +3546,8 @@ var init_adapter = __esm({
|
|
|
2921
3546
|
init_questions();
|
|
2922
3547
|
init_navigation();
|
|
2923
3548
|
init_startup_error();
|
|
3549
|
+
init_session_control();
|
|
3550
|
+
init_session_tools();
|
|
2924
3551
|
}
|
|
2925
3552
|
});
|
|
2926
3553
|
|