chatluna-plugin-livingmemory 0.0.8 → 0.0.10

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/lib/index.js CHANGED
@@ -2,13 +2,13 @@ import Schema from "schemastery";
2
2
  import { Schema as Schema$1, modelAddressSchema, schemasteryToJsonSchemaNode } from "@chatluna/schema";
3
3
  import { routeIdOf } from "@chatluna/satori";
4
4
  import { Service } from "cordis";
5
+ import { existsSync, watch } from "node:fs";
5
6
  import { createHash, randomUUID } from "node:crypto";
6
7
  import { defineTool } from "@chatluna/tool";
7
8
  import { isRecord } from "@chatluna/utils/object";
8
9
  import { $ } from "@cordisjs/plugin-database";
9
10
  import { estimateMessageTokens, resolveChatlunaDataDir } from "@chatluna/utils";
10
11
  import { dirname, resolve } from "node:path";
11
- import { existsSync } from "node:fs";
12
12
  import { rename, rm } from "node:fs/promises";
13
13
  import { setImmediate, setTimeout } from "node:timers/promises";
14
14
  import { performance as performance$1 } from "node:perf_hooks";
@@ -2383,6 +2383,16 @@ var LivingMemoryRepository = class {
2383
2383
  async getEntriesWithSources(presetId, ids) {
2384
2384
  return this.withSourceLabels(await this.getEntriesByPresetAndIds(presetId, ids));
2385
2385
  }
2386
+ async listProfileMemories(presetId, profileId) {
2387
+ const [profile] = await this.database.get("living_memory_user_profile", {
2388
+ presetId,
2389
+ id: profileId
2390
+ }, ["sourceMemoryIds"]);
2391
+ const ids = profile?.sourceMemoryIds ?? [];
2392
+ const entries = await this.getEntriesWithSources(presetId, ids);
2393
+ const byId = new Map(entries.map((entry) => [entry.id, entry]));
2394
+ return ids.map((id) => byId.get(id)).filter((entry) => entry !== void 0);
2395
+ }
2386
2396
  async getSearchEntriesByPresetAndIds(presetId, ids) {
2387
2397
  return this.withSourceLabels(await this.entries.getSearchEntriesByPresetAndIds(presetId, ids));
2388
2398
  }
@@ -7621,10 +7631,8 @@ const filterJobList = (items, query) => {
7621
7631
  return paginate(sorted, query.page, query.pageSize);
7622
7632
  };
7623
7633
  const filterUserProfileList = (items, query) => {
7624
- const sorted = items.sort((left, right) => {
7625
- if (+right.updatedAt !== +left.updatedAt) return +right.updatedAt - +left.updatedAt;
7626
- return left.speakerLabel.localeCompare(right.speakerLabel);
7627
- });
7634
+ const keyword = query.keyword?.trim().toLowerCase();
7635
+ const sorted = (keyword ? items.filter((profile) => profile.speakerLabel.toLowerCase().includes(keyword) || profile.speakerId?.toLowerCase().includes(keyword)) : items).toSorted((left, right) => +right.updatedAt - +left.updatedAt || left.speakerLabel.localeCompare(right.speakerLabel));
7628
7636
  return paginate(sorted, query.page, query.pageSize);
7629
7637
  };
7630
7638
  // src/service/app/admin_service.ts
@@ -7690,7 +7698,16 @@ var LivingMemoryAdminService = class {
7690
7698
  return this.deps.dreamCoordinator.start(presetId);
7691
7699
  }
7692
7700
  async listUserProfiles(query) {
7693
- return filterUserProfileList(await this.deps.repository.listUserProfilesByPreset(query.presetId), query);
7701
+ const [profiles, speakers] = await Promise.all([this.deps.repository.listUserProfilesByPreset(query.presetId), this.listPresetSpeakers(query.presetId)]);
7702
+ const byKey = new Map(speakers.map((speaker) => [speaker.speakerKey, speaker]));
7703
+ return filterUserProfileList(profiles.map((profile) => {
7704
+ const speaker = byKey.get(profile.speakerKey);
7705
+ return {
7706
+ ...profile,
7707
+ speakerId: speaker?.speakerId ?? null,
7708
+ platform: speaker?.platform ?? null
7709
+ };
7710
+ }), query);
7694
7711
  }
7695
7712
  listPresetSpeakers(presetId) {
7696
7713
  return this.deps.repository.listPresetSpeakers(presetId);
@@ -7868,6 +7885,7 @@ const livingMemoryWebUI = (ctx, ui) => {
7868
7885
  ctx.chatluna_webui.state("livingmemory.profiles", {
7869
7886
  params: Schema$1.object({
7870
7887
  presetId: Schema$1.string().required(),
7888
+ keyword: Schema$1.string(),
7871
7889
  page: Schema$1.natural(),
7872
7890
  pageSize: Schema$1.natural()
7873
7891
  }),
@@ -7883,6 +7901,16 @@ const livingMemoryWebUI = (ctx, ui) => {
7883
7901
  },
7884
7902
  refreshOn
7885
7903
  });
7904
+ ctx.chatluna_webui.state("livingmemory.profileMemories", {
7905
+ params: Schema$1.object({
7906
+ presetId: Schema$1.string().required(),
7907
+ profileId: Schema$1.string().required()
7908
+ }),
7909
+ get: async ({ presetId, profileId }) => {
7910
+ return (await ui.repository.listProfileMemories(presetId, profileId)).map(toEntryView);
7911
+ },
7912
+ refreshOn
7913
+ });
7886
7914
  ctx.chatluna_webui.state("livingmemory.snapshots", {
7887
7915
  params: Schema$1.object({
7888
7916
  presetId: Schema$1.string().required(),
@@ -8106,8 +8134,10 @@ const toProfileView = (profile) => ({
8106
8134
  id: profile.id,
8107
8135
  speakerKey: profile.speakerKey,
8108
8136
  speakerLabel: profile.speakerLabel,
8137
+ speakerId: profile.speakerId,
8138
+ platform: profile.platform,
8109
8139
  content: profile.content,
8110
- sourceMemoryIds: [...profile.sourceMemoryIds],
8140
+ sourceMemoryCount: profile.sourceMemoryIds.length,
8111
8141
  updatedAt: profile.updatedAt.getTime()
8112
8142
  });
8113
8143
  function toSnapshotView(snapshot, sources) {
@@ -8635,10 +8665,21 @@ var LivingmemoryService = class extends Service {
8635
8665
  name: entry.options.name
8636
8666
  } : null;
8637
8667
  } });
8638
- ctx.chatluna_webui.addEntry({
8668
+ const files = {
8639
8669
  baseUrl: import.meta.url,
8640
8670
  source: "../client/index.ts",
8641
8671
  bundle: "../dist/client.js"
8672
+ };
8673
+ const webui = ctx.chatluna_webui;
8674
+ let entry = webui.addEntry(files);
8675
+ if (!webui.config.devMode && existsSync(new URL("../scripts/build-client.mjs", import.meta.url))) ctx.effect(() => {
8676
+ const watcher = watch(new URL("../", import.meta.url), (_event, file) => {
8677
+ if (file !== ".webui-reload") return;
8678
+ const previous = entry;
8679
+ entry = webui.addEntry(files);
8680
+ previous.dispose();
8681
+ });
8682
+ return () => watcher.close();
8642
8683
  });
8643
8684
  livingMemoryWebUI(ctx, ui);
8644
8685
  }