dsh-starlite 0.1.3 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -211,14 +211,20 @@ var init_interaction = __esm({
211
211
 
212
212
  // ../protocol/src/control.ts
213
213
  function parseControlDesktopReport(value) {
214
- const row = object(value, ["controlVersion", "enabled", "available", "model", "preferences"]);
214
+ const row = object(value, ["controlVersion", "enabled", "available", "externalBusy", "model", "preferences"]);
215
215
  version(row);
216
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
- } };
217
+ const result2 = {
218
+ controlVersion: 1,
219
+ enabled: boolean(row.enabled),
220
+ available: boolean(row.available),
221
+ ...row.externalBusy === void 0 ? {} : { externalBusy: boolean(row.externalBusy) },
222
+ preferences: {
223
+ bubblesVisible: boolean(preferences.bubblesVisible),
224
+ pointerEnabled: boolean(preferences.pointerEnabled),
225
+ scale: number(preferences.scale, 0.25, 3)
226
+ }
227
+ };
222
228
  if (row.model !== void 0) {
223
229
  const model = object(row.model, ["revision", "title", "animations"]);
224
230
  if (!Array.isArray(model.animations) || model.animations.length > 256) throw Error("invalid animation catalog");
@@ -292,7 +298,7 @@ function parseControlAck(value) {
292
298
  leaseId: ref(row.leaseId),
293
299
  commandId: ref(row.commandId),
294
300
  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"]) }
301
+ ...row.reason === void 0 ? {} : { reason: choice(row.reason, ["unavailable", "unknown-animation", "user-preference", "stale", "failed", "busy"]) }
296
302
  };
297
303
  }
298
304
  function parseControlRelease(value) {
@@ -349,6 +355,61 @@ var init_control = __esm({
349
355
  }
350
356
  });
351
357
 
358
+ // ../protocol/src/hub.ts
359
+ function parsePetSource(value) {
360
+ const row = object2(value, ["id", "name", "capabilities"]);
361
+ const id = slug(row.id);
362
+ const name2 = safeText(row.name, 60);
363
+ let capabilities;
364
+ if (row.capabilities !== void 0) {
365
+ const cap = object2(row.capabilities, ["billing", "questions", "navigation", "control"]);
366
+ capabilities = {
367
+ ...cap.billing === void 0 ? {} : { billing: bool(cap.billing) },
368
+ ...cap.questions === void 0 ? {} : { questions: bool(cap.questions) },
369
+ ...cap.navigation === void 0 ? {} : { navigation: bool(cap.navigation) },
370
+ ...cap.control === void 0 ? {} : { control: bool(cap.control) }
371
+ };
372
+ }
373
+ return {
374
+ id,
375
+ name: name2,
376
+ ...capabilities ? { capabilities } : {}
377
+ };
378
+ }
379
+ function object2(value, allowed) {
380
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
381
+ throw new Error("invalid hub object");
382
+ }
383
+ const row = value;
384
+ for (const key of Object.keys(row)) {
385
+ if (!allowed.includes(key)) throw new Error("unknown hub field");
386
+ }
387
+ return row;
388
+ }
389
+ function slug(value) {
390
+ if (typeof value !== "string" || !/^[a-z0-9][a-z0-9_-]{0,31}$/u.test(value)) {
391
+ throw new Error("invalid agent identifier");
392
+ }
393
+ return value;
394
+ }
395
+ function safeText(value, max) {
396
+ if (typeof value !== "string" || !value.trim() || value.length > max || /[\u0000-\u001f\u007f-\u009f\u202a-\u202e\u2066-\u2069]/u.test(value)) {
397
+ throw new Error("invalid hub text");
398
+ }
399
+ return value.trim();
400
+ }
401
+ function bool(value) {
402
+ if (typeof value !== "boolean") throw new Error("invalid hub boolean");
403
+ return value;
404
+ }
405
+ var init_hub = __esm({
406
+ "../protocol/src/hub.ts"() {
407
+ "use strict";
408
+ init_interaction();
409
+ init_control();
410
+ }
411
+ });
412
+
352
413
  // ../protocol/src/index.ts
353
414
  function sanitizeBubbleText(input, maximumGraphemes = DEFAULT_BUBBLE_GRAPHEMES) {
354
415
  if (typeof input !== "string" || !Number.isSafeInteger(maximumGraphemes)) {
@@ -384,6 +445,7 @@ function parsePetSnapshot(value) {
384
445
  }
385
446
  const bubble2 = value.bubble === void 0 ? void 0 : parseBubble(value.bubble);
386
447
  const session = value.session === void 0 ? void 0 : parsePetSession(value.session);
448
+ const source = value.source === void 0 ? void 0 : parsePetSource(value.source);
387
449
  if (value.activity !== void 0 && !ACTIVITIES.includes(value.activity)) throw Error("invalid activity");
388
450
  if (value.inputRequest !== void 0 && (!session || !isOpaqueRef(value.inputRequest))) throw Error("invalid input reference");
389
451
  return {
@@ -394,7 +456,8 @@ function parsePetSnapshot(value) {
394
456
  ...bubble2 ? { bubble: bubble2 } : {},
395
457
  ...session ? { session } : {},
396
458
  ...value.activity === void 0 ? {} : { activity: value.activity },
397
- ...value.inputRequest === void 0 ? {} : { inputRequest: value.inputRequest }
459
+ ...value.inputRequest === void 0 ? {} : { inputRequest: value.inputRequest },
460
+ ...source ? { source } : {}
398
461
  };
399
462
  }
400
463
  function isPetState(value) {
@@ -435,8 +498,10 @@ var init_src = __esm({
435
498
  "use strict";
436
499
  init_billing();
437
500
  init_control();
501
+ init_hub();
438
502
  init_interaction();
439
503
  init_interaction();
504
+ init_hub();
440
505
  PET_PROTOCOL_VERSION = 1;
441
506
  DEFAULT_BUBBLE_GRAPHEMES = 120;
442
507
  MAX_WIRE_BYTES = 4096;
@@ -1227,13 +1292,10 @@ function companionEnvironment(source) {
1227
1292
  }
1228
1293
  return result2;
1229
1294
  }
1230
- async function launchWindowsPet(connection, options = {}) {
1231
- const url = buildConnectionUrl(connection);
1232
- const host = options.host ?? detectCompanionHost();
1233
- if (!host) throw new Error("StarLite requires a Windows x64 or WSL2 x64 DSH Host");
1295
+ async function prepareWindowsExecutable(host, options = {}) {
1234
1296
  const paths = host === "windows" ? win32 : posix;
1235
1297
  options.signal?.throwIfAborted();
1236
- let executable;
1298
+ let executable = "";
1237
1299
  const override = process.env.DSH_INOCHI2D_PET_EXE;
1238
1300
  if (override !== void 0) {
1239
1301
  const local = validateExecutableOverride(override);
@@ -1251,25 +1313,35 @@ async function launchWindowsPet(connection, options = {}) {
1251
1313
  const localCache = host === "windows" ? windowsCache : (await execFileText("wslpath", ["-u", windowsCache], options.signal)).trim();
1252
1314
  const roaming = host === "windows" ? windowsDirectories[1] : (await execFileText("wslpath", ["-u", windowsDirectories[1]], options.signal)).trim();
1253
1315
  if (host === "wsl" && (!validPosixDirectory(localCache) || !validPosixDirectory(roaming))) throw new Error("invalid Windows application directories");
1254
- const defaultModelId = await defaultModelToAcquire(paths.join(roaming, "com.starcried.dsh-inochi2d-pet", "settings.json"));
1316
+ const defaultModelIds = await defaultModelsToAcquire(paths.join(roaming, "com.starcried.dsh-inochi2d-pet", "settings.json"));
1255
1317
  const metadata = JSON.parse(await readFile(new URL("../package.json", import.meta.url), "utf8"));
1256
1318
  if (typeof metadata !== "object" || metadata === null || !("version" in metadata) || typeof metadata.version !== "string") {
1257
1319
  throw new Error("invalid StarLite plugin package version");
1258
1320
  }
1259
- const prepared = await prepareEmbeddedCompanion({
1260
- assetDirectory: fileURLToPath(new URL("../assets/windows-x64/", import.meta.url)),
1261
- cacheDirectory: paths.join(localCache, "StarCried", "StarLite", "companions"),
1262
- expectedVersion: metadata.version,
1263
- defaultModelId,
1264
- ...options.signal ? { signal: options.signal } : {}
1265
- });
1266
- executable = prepared.executable;
1267
- if (defaultModelId && !prepared.defaultModelReady) options.onDefaultModelFailure?.();
1321
+ for (const defaultModelId of defaultModelIds.length ? defaultModelIds : [null]) {
1322
+ const prepared = await prepareEmbeddedCompanion({
1323
+ assetDirectory: fileURLToPath(new URL("../assets/windows-x64/", import.meta.url)),
1324
+ cacheDirectory: paths.join(localCache, "StarCried", "StarLite", "companions"),
1325
+ expectedVersion: metadata.version,
1326
+ defaultModelId,
1327
+ ...options.signal ? { signal: options.signal } : {}
1328
+ });
1329
+ executable = prepared.executable;
1330
+ if (defaultModelId && !prepared.defaultModelReady) options.onDefaultModelFailure?.();
1331
+ }
1268
1332
  }
1269
1333
  if (host === "windows" && validateExecutableOverride(executable) !== executable || host === "wsl" && (!validPosixDirectory(executable) || posix.basename(executable).toLowerCase() !== "dsh-inochi2d-pet.exe")) {
1270
1334
  throw new Error("StarLite for DSH executable is invalid");
1271
1335
  }
1272
1336
  options.signal?.throwIfAborted();
1337
+ return executable;
1338
+ }
1339
+ async function launchWindowsPet(connection, options = {}) {
1340
+ const url = buildConnectionUrl(connection);
1341
+ const host = options.host ?? detectCompanionHost();
1342
+ if (!host) throw new Error("StarLite requires a Windows x64 or WSL2 x64 DSH Host");
1343
+ const executable = await prepareWindowsExecutable(host, options);
1344
+ options.signal?.throwIfAborted();
1273
1345
  return new Promise((resolve2, reject2) => {
1274
1346
  const child = spawn(executable, [url], {
1275
1347
  // Windows' non-detached child job ends with the Host; the pet must retain
@@ -1298,35 +1370,39 @@ function validWindowsDirectory(path) {
1298
1370
  function validPosixDirectory(path) {
1299
1371
  return posix.isAbsolute(path) && !/[\p{Cc}]/u.test(path) && !path.split("/").some((part) => part === "." || part === "..");
1300
1372
  }
1301
- async function defaultModelToAcquire(path) {
1373
+ async function defaultModelsToAcquire(path) {
1302
1374
  const maximum = 4 * 1024 * 1024;
1303
1375
  try {
1304
1376
  const info = await lstat2(path);
1305
- if (!info.isFile() || info.isSymbolicLink() || info.size < 1 || info.size > maximum) return null;
1377
+ if (!info.isFile() || info.isSymbolicLink() || info.size < 1 || info.size > maximum) return [];
1306
1378
  const file = await open2(path, constants2.O_RDONLY | (constants2.O_NOFOLLOW ?? 0));
1307
1379
  try {
1308
1380
  const opened = await file.stat();
1309
- if (!opened.isFile() || opened.size !== info.size) return null;
1381
+ if (!opened.isFile() || opened.size !== info.size) return [];
1310
1382
  const bytes = Buffer.alloc(info.size + 1);
1311
1383
  const result2 = await file.read(bytes, 0, bytes.length, 0);
1312
- if (result2.bytesRead !== info.size) return null;
1384
+ if (result2.bytesRead !== info.size) return [];
1313
1385
  const value = JSON.parse(bytes.subarray(0, result2.bytesRead).toString("utf8"));
1314
- if (typeof value !== "object" || value === null || Array.isArray(value)) return null;
1315
- if (!("schemaVersion" in value)) return null;
1386
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return [];
1387
+ if (!("schemaVersion" in value)) return [];
1316
1388
  if (value.schemaVersion === 4) {
1317
- if (!("activeModelId" in value) || !isDefaultModelId(value.activeModelId)) return null;
1318
- if ("models" in value && Array.isArray(value.models) && value.models.some((model) => typeof model === "object" && model !== null && !Array.isArray(model) && "id" in model && model.id === value.activeModelId)) return null;
1319
- return value.activeModelId;
1320
- }
1321
- if (![1, 2, 3].includes(value.schemaVersion)) return null;
1389
+ const models = "models" in value && Array.isArray(value.models) ? value.models : [];
1390
+ const installed = (id) => models.some((model) => typeof model === "object" && model !== null && !Array.isArray(model) && "id" in model && model.id === id);
1391
+ const updates = "pendingModelUpdates" in value ? value.pendingModelUpdates : [];
1392
+ if (!Array.isArray(updates) || updates.length > 2 || !updates.every((id) => isDefaultModelId(id) && installed(id))) return [];
1393
+ const requested = new Set(updates);
1394
+ if ("activeModelId" in value && isDefaultModelId(value.activeModelId) && !installed(value.activeModelId)) requested.add(value.activeModelId);
1395
+ return [...requested];
1396
+ }
1397
+ if (![1, 2, 3].includes(value.schemaVersion)) return [];
1322
1398
  const preferences = "preferences" in value ? value.preferences : void 0;
1323
- if (typeof preferences === "object" && preferences !== null && "modelEnabled" in preferences) return preferences.modelEnabled === true ? "weiguang-default" : null;
1324
- return "weiguang-default";
1399
+ if (typeof preferences === "object" && preferences !== null && "modelEnabled" in preferences) return preferences.modelEnabled === true ? ["weiguang-default"] : [];
1400
+ return ["weiguang-default"];
1325
1401
  } finally {
1326
1402
  await file.close();
1327
1403
  }
1328
1404
  } catch (error) {
1329
- return typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT" ? "weiguang-default" : null;
1405
+ return typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT" ? ["weiguang-default"] : [];
1330
1406
  }
1331
1407
  }
1332
1408
  function validateExecutableOverride(override) {
@@ -2550,7 +2626,7 @@ var init_questions = __esm({
2550
2626
  }
2551
2627
  #directory() {
2552
2628
  const service2 = this.#service("agents");
2553
- return typeof service2?.get === "function" && typeof service2.roots === "function" ? service2 : void 0;
2629
+ return typeof service2?.get === "function" && (typeof service2.roots === "function" || typeof service2.list === "function") ? service2 : void 0;
2554
2630
  }
2555
2631
  #service(name2) {
2556
2632
  try {
@@ -2562,7 +2638,10 @@ var init_questions = __esm({
2562
2638
  #liveRoot(agent, key) {
2563
2639
  try {
2564
2640
  const row = record5(agent), directory = this.#directory();
2565
- return Boolean(row && directory && row.session === key && typeof row.id === "string" && directory.get(row.id) === agent && directory.roots().includes(agent));
2641
+ if (!row || !directory || row.session !== key || typeof row.id !== "string" || directory.get(row.id) !== agent) return false;
2642
+ if (typeof directory.roots === "function") return directory.roots().includes(agent);
2643
+ const header = record5(record5(key)?.header);
2644
+ return Boolean(header && header.origin !== "subagent");
2566
2645
  } catch {
2567
2646
  return false;
2568
2647
  }
@@ -2572,7 +2651,8 @@ var init_questions = __esm({
2572
2651
  if (!service2 || !projections || !directory) return [];
2573
2652
  const result2 = [];
2574
2653
  try {
2575
- for (const agent of directory.roots().slice(0, 128)) {
2654
+ const agents = typeof directory.roots === "function" ? directory.roots() : directory.list();
2655
+ for (const agent of agents.slice(0, 128)) {
2576
2656
  const key = record5(agent)?.session;
2577
2657
  if (!key || !this.#liveRoot(agent, key)) continue;
2578
2658
  const active = record5(record5(projections.stateOf(key, "userQuestions"))?.questions)?.active;
@@ -2634,7 +2714,7 @@ function opaqueRef(value) {
2634
2714
  function sessionIdentity(value) {
2635
2715
  return typeof value === "string" && value.length > 0 && value.length <= 256 && !Array.from(value).some((character) => character.charCodeAt(0) < 32);
2636
2716
  }
2637
- function object2(value) {
2717
+ function object3(value) {
2638
2718
  return value && typeof value === "object" && !Array.isArray(value) ? value : void 0;
2639
2719
  }
2640
2720
  var NAVIGATION_CHANNEL;
@@ -2690,7 +2770,7 @@ var init_desktop_navigation = __esm({
2690
2770
  return { result: result2, cancel: () => pending.finish(false) };
2691
2771
  }
2692
2772
  async handle(endpoint, payload, signal, peer) {
2693
- const row = object2(payload);
2773
+ const row = object3(payload);
2694
2774
  if (this.#closed || signal.aborted || !row || !opaqueRef(row.clientRef) || !peer) return unavailable();
2695
2775
  if (endpoint === "listen" && Object.keys(row).length === 1) return this.#listen(row.clientRef, peer, signal);
2696
2776
  const owner = this.#owner;
@@ -2766,7 +2846,7 @@ var init_desktop_navigation = __esm({
2766
2846
  // src/navigation.ts
2767
2847
  import { spawn as spawn2 } from "node:child_process";
2768
2848
  function createDshNavigator(context, openUrl = openLocalBrowser) {
2769
- if (object2(context.get?.("profileContext"))?.name !== "desktop") {
2849
+ if (object3(context.get?.("profileContext"))?.name !== "desktop") {
2770
2850
  return { open: (key) => openDshSession(context, key, openUrl), close: async () => void 0 };
2771
2851
  }
2772
2852
  let closed = false;
@@ -2774,7 +2854,7 @@ function createDshNavigator(context, openUrl = openLocalBrowser) {
2774
2854
  let binding;
2775
2855
  const attach = (scope) => {
2776
2856
  if (closed) return;
2777
- const rpc = object2(object2(scope.get?.("connection"))?.rpc);
2857
+ const rpc = object3(object3(scope.get?.("connection"))?.rpc);
2778
2858
  if (typeof rpc?.handle !== "function") throw Error("DSH Desktop navigation contract unavailable");
2779
2859
  const broker = new DesktopNavigationBroker();
2780
2860
  const remove = rpc.handle(NAVIGATION_CHANNEL, (endpoint, payload, signal, peer) => broker.handle(endpoint, payload, signal, peer));
@@ -2970,11 +3050,12 @@ var init_session_control = __esm({
2970
3050
  this.changed();
2971
3051
  }
2972
3052
  inspect(session) {
3053
+ const externalBusy = this.desktop?.externalBusy === true;
2973
3054
  return {
2974
3055
  enabled: this.enabled,
2975
3056
  available: this.ready,
2976
3057
  controlledBySelf: !!this.owner && this.owner.session === session,
2977
- busy: this.active && this.owner?.session !== session,
3058
+ busy: this.active && this.owner?.session !== session || externalBusy,
2978
3059
  leaseMs: CONTROL_LEASE_MS,
2979
3060
  ...this.desktop?.model ? { model: { title: this.desktop.model.title, animations: this.desktop.model.animations.map((item) => ({ ...item })) } } : {},
2980
3061
  ...this.desktop ? { preferences: { ...this.desktop.preferences } } : {}
@@ -2983,6 +3064,7 @@ var init_session_control = __esm({
2983
3064
  async acquire(session, title, signal) {
2984
3065
  if (signal.aborted) return result("interrupted", "cancelled");
2985
3066
  if (!this.ready) return result("rejected", this.enabled ? "unavailable" : "disabled");
3067
+ if (this.desktop?.externalBusy === true && (!this.owner || this.owner.session !== session)) return result("rejected", "busy");
2986
3068
  if (this.owner && this.owner.session !== session) return result("rejected", "busy");
2987
3069
  if ([...this.pending.values()].some((item) => item.frame.kind === "acquire")) return result("rejected", "busy");
2988
3070
  if (!this.owner) {