koishi-plugin-chatluna-studio 1.1.4 → 1.1.5

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.
Files changed (2) hide show
  1. package/lib/index.js +50 -21
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -3346,7 +3346,7 @@ function registerConsole(console, options) {
3346
3346
  const { scope, ...query } = input;
3347
3347
  return modelRequests.getRecords(scope === "all" ? query : { ...query, attribution: scope });
3348
3348
  };
3349
- const readRecord4 = async (input) => {
3349
+ const readRecord5 = async (input) => {
3350
3350
  const detail = await modelRequests.requireRecord(input.recordId);
3351
3351
  const usage2 = await lookupChatLunaUsage(options.chatlunaUsage?.(), detail);
3352
3352
  return usage2 ? { ...detail, usage: usage2 } : detail;
@@ -3366,7 +3366,7 @@ function registerConsole(console, options) {
3366
3366
  const registerListener = console.addListener.bind(console);
3367
3367
  registerListener("chatluna-studio/workspace", getWorkspace, { authority: 4 });
3368
3368
  registerListener("chatluna-studio/model-request-records", listRecords, { authority: 4 });
3369
- registerListener("chatluna-studio/model-request-record", readRecord4, { authority: 4 });
3369
+ registerListener("chatluna-studio/model-request-record", readRecord5, { authority: 4 });
3370
3370
  registerListener("chatluna-studio/model-request-trajectory", readTrajectory, { authority: 4 });
3371
3371
  registerListener("chatluna-studio/model-request-facets", () => modelRequests.getFacets(), { authority: 4 });
3372
3372
  registerListener("chatluna-studio/clear-model-request-records", clearRecords, { authority: 4 });
@@ -4166,6 +4166,34 @@ function normalizeFileSystemError(error, fileName) {
4166
4166
  return new PresetRepositoryError("unsafe-file", `\u65E0\u6CD5\u5B89\u5168\u8BBF\u95EE\u9884\u8BBE\uFF1A${fileName}`, error);
4167
4167
  }
4168
4168
 
4169
+ // src/chatluna/runtime.ts
4170
+ function readRecord3(value) {
4171
+ return value !== null && typeof value === "object" ? value : void 0;
4172
+ }
4173
+ function readServiceFrom(host, serviceName, accept) {
4174
+ const get = readRecord3(host)?.get;
4175
+ if (typeof get !== "function") return void 0;
4176
+ const service = readRecord3(get.call(host, serviceName));
4177
+ return service ? accept(service) : void 0;
4178
+ }
4179
+ function listRegistryContexts(value) {
4180
+ const registry = readRecord3(readRecord3(value)?.registry);
4181
+ const values = registry?.values;
4182
+ if (typeof values !== "function") return [];
4183
+ return [...values.call(registry)].flatMap((runtime) => {
4184
+ const ctx = readRecord3(runtime)?.ctx;
4185
+ return ctx ? [ctx] : [];
4186
+ });
4187
+ }
4188
+ function findChatLunaRuntime(host, serviceName, accept) {
4189
+ const direct = readServiceFrom(host, serviceName, accept);
4190
+ if (direct) return direct;
4191
+ for (const ctx of listRegistryContexts(host)) {
4192
+ const resolved = readServiceFrom(ctx, serviceName, accept);
4193
+ if (resolved) return resolved;
4194
+ }
4195
+ }
4196
+
4169
4197
  // src/presets/runtime-correlation.ts
4170
4198
  var PresetRuntimeSnapshotTracker = class {
4171
4199
  constructor(ctx) {
@@ -4191,9 +4219,9 @@ var PresetRuntimeSnapshotTracker = class {
4191
4219
  }));
4192
4220
  this.disposers.push(on("chatluna/after-chat-error", (_error, conversationId) => this.finishCore(conversationId)));
4193
4221
  this.disposers.push(on("chatluna_character/before-chat", (payload) => {
4194
- const record = readRecord3(payload);
4222
+ const record = readRecord4(payload);
4195
4223
  const target = this.resolveSession(record?.session);
4196
- const preset = readRecord3(record?.preset);
4224
+ const preset = readRecord4(record?.preset);
4197
4225
  const presetName = readString2(record?.presetName) ?? readString2(preset?.name);
4198
4226
  if (!target || !preset || !presetName) return;
4199
4227
  const snapshot = snapshotCharacterPreset(presetName, preset);
@@ -4209,7 +4237,7 @@ var PresetRuntimeSnapshotTracker = class {
4209
4237
  }
4210
4238
  }));
4211
4239
  this.disposers.push(on("chatluna_character/after-chat", (payload) => {
4212
- const record = readRecord3(payload);
4240
+ const record = readRecord4(payload);
4213
4241
  const target = this.resolveSession(record?.session);
4214
4242
  if (!target) return;
4215
4243
  const session = record?.session;
@@ -4245,9 +4273,9 @@ var PresetRuntimeSnapshotTracker = class {
4245
4273
  this.coreTurnKeys.clear();
4246
4274
  }
4247
4275
  resolveSession(session) {
4248
- const record = readRecord3(session);
4249
- const bot = readRecord3(record?.bot);
4250
- const channel = readRecord3(record?.channel);
4276
+ const record = readRecord4(session);
4277
+ const bot = readRecord4(record?.bot);
4278
+ const channel = readRecord4(record?.channel);
4251
4279
  const botId = readString2(record?.selfId) ?? readString2(bot?.selfId);
4252
4280
  const conversationId = readString2(record?.channelId) ?? readString2(channel?.id);
4253
4281
  return botId && conversationId ? { botId, conversationId } : void 0;
@@ -4292,7 +4320,7 @@ function coreTemplates(preset) {
4292
4320
  }
4293
4321
  const messages = Array.isArray(preset.messages) ? preset.messages : [];
4294
4322
  const result = messages.flatMap((message, index) => {
4295
- const record = readRecord3(message);
4323
+ const record = readRecord4(message);
4296
4324
  const content = readMessageContent(record?.content);
4297
4325
  const role = readMessageRole(message);
4298
4326
  return content !== void 0 && role ? [{ path: ["prompts", index, "content"], role, template: content }] : [];
@@ -4308,7 +4336,7 @@ function coreRoleForPath(preset, path) {
4308
4336
  return index === void 0 ? void 0 : readMessageRole(Array.isArray(preset.messages) ? preset.messages[index] : void 0);
4309
4337
  }
4310
4338
  function readMessageRole(message) {
4311
- const record = readRecord3(message);
4339
+ const record = readRecord4(message);
4312
4340
  const direct = readString2(record?.role)?.toLowerCase();
4313
4341
  let type = direct;
4314
4342
  const getType = record?._getType;
@@ -4325,7 +4353,7 @@ function readMessageRole(message) {
4325
4353
  }
4326
4354
  function readMessageContent(value) {
4327
4355
  if (typeof value === "string") return value;
4328
- const record = readRecord3(value);
4356
+ const record = readRecord4(value);
4329
4357
  return readString2(record?.text);
4330
4358
  }
4331
4359
  function snapshotCharacterPreset(presetName, preset) {
@@ -4344,15 +4372,16 @@ function snapshotCharacterPreset(presetName, preset) {
4344
4372
  });
4345
4373
  }
4346
4374
  function resolveCorePreset(chatInterface, ctx, presetName) {
4347
- const interfaceContext = readRecord3(readRecord3(chatInterface)?.ctx);
4348
- const candidates = [readRecord3(interfaceContext?.chatluna), readRecord3(readRecord3(ctx)?.chatluna)];
4349
- for (const chatluna of candidates) {
4350
- const service = readRecord3(chatluna?.preset);
4351
- const getPreset = service?.getPreset;
4352
- if (typeof getPreset !== "function") continue;
4375
+ const hosts = [readRecord4(chatInterface)?.ctx, ctx];
4376
+ for (const host of hosts) {
4377
+ const service = findChatLunaRuntime(host, "chatluna", (chatluna) => {
4378
+ const preset = readRecord4(chatluna.preset);
4379
+ return typeof preset?.getPreset === "function" ? preset : void 0;
4380
+ });
4381
+ if (!service) continue;
4353
4382
  try {
4354
- const computed = getPreset.call(service, presetName, false);
4355
- const value = readRecord3(readRecord3(computed)?.value);
4383
+ const computed = service.getPreset.call(service, presetName, false);
4384
+ const value = readRecord4(readRecord4(computed)?.value);
4356
4385
  if (value) return value;
4357
4386
  } catch {
4358
4387
  }
@@ -4364,7 +4393,7 @@ function activeKey(target, turnId) {
4364
4393
  function sameTarget(left, right) {
4365
4394
  return left.botId === right.botId && left.conversationId === right.conversationId;
4366
4395
  }
4367
- function readRecord3(value) {
4396
+ function readRecord4(value) {
4368
4397
  return value !== null && typeof value === "object" ? value : void 0;
4369
4398
  }
4370
4399
  function readString2(value) {
@@ -4372,7 +4401,7 @@ function readString2(value) {
4372
4401
  }
4373
4402
  function readNestedString(value, path) {
4374
4403
  let current = value;
4375
- for (const key of path) current = readRecord3(current)?.[key];
4404
+ for (const key of path) current = readRecord4(current)?.[key];
4376
4405
  return readString2(current);
4377
4406
  }
4378
4407
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-chatluna-studio",
3
3
  "description": "在 Koishi 控制台中查看 ChatLuna 真实模型请求并编辑预设",
4
- "version": "1.1.4",
4
+ "version": "1.1.5",
5
5
  "type": "commonjs",
6
6
  "main": "lib/index.js",
7
7
  "typings": "lib/index.d.ts",