cortico-world-hindsight 0.1.4 → 0.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # 更新说明
2
2
 
3
+ ## 0.1.5
4
+
5
+ 改成按计划把完整对话原文写进记忆库;新增「上传设置」面板。
6
+
7
+ - 去掉每轮自动上传(`autoRemember` 与它的子项),换成两个计划:定时上传(默认开,每天 22:00)与间隔上传(默认开,每 2 小时)。到点把上次上传之后的完整对话原文写进 bank,嵌入与抽事实交给 Hindsight,插件不写摘要。
8
+ - 上传内容是原文连同图片:人说的话、Coo 的回复按时刻合并,图片按它出现的位置插入;正文超过 `uploadMaxChars` 时切条而不是截断,内容不丢。
9
+ - 服务端拒收图片时退回纯文本重试,并投递一条事件让模型告诉主人;成功的那条只留档(`deliver: false`),不会每两小时吵醒她一次。
10
+ - 新增「上传设置」面板:两个开关与它们的时刻/间隔、下次触发时刻、待整理的条数、上次整理的结果,以及「立即整理一次」。改设置立即生效,不用重启。
11
+ - 环境提示词跟着改:说明这段对话会按计划整理,留档的是原文、不需要为存档逐句照抄;值得单独留下的结论仍由模型用 `hindsight_remember` 写。同时去掉服务地址、bank 名与连通状态那几行——她读不出用处。
12
+ - `hindsight_remember` 的工具说明同步:不再自称「自动写入的补充」。
13
+ - 新增 8 个用例:原文整段写入、Coo 的回复按时刻并入、环境事件不进上传、空窗口不发请求、成功不投递/失败投递、图片被拒后退回纯文本、面板报计划与上次结果、以及用假时钟验证到点真的会自己传。
14
+
3
15
  ## 0.1.4
4
16
 
5
17
  写入时把时区写死,修整理阶段把 UTC 当本地时间的问题。
package/README.md CHANGED
@@ -13,17 +13,16 @@ World id 是 `hindsight`,配置段是 `worlds.hindsight`,WebUI 页是 `world
13
13
  | 跨很多条记忆回答 | `hindsight_reflect` 让服务端综合成一段,会引用来自哪些事实 |
14
14
  | 看整理出的概览 | `hindsight_pages` 列出或搜索知识页面 |
15
15
  | 报连接与规模 | `hindsight_status` 只读,别轮询 |
16
- | 每轮自动留档 | 一轮结束时把这一轮的事件正文与自己的回复合起来写入,只收对话事件,见 `autoRemember` / `autoRememberTypes` |
17
- | 图片一起记 | 这一轮里的图片按原位置随对话写入,服务端看图抽取(要求抽取模型能看图) |
18
- | 看清它在干什么 | 三个面板:连接、记忆、知识页面 |
16
+ | 按计划整理对话 | 定时(默认每天 22:00)与间隔(默认每 2 小时)把这一段的**完整原文**写进记忆库,含图片 |
17
+ | 看清它在干什么 | 四个面板:连接、上传设置、记忆、知识页面 |
19
18
 
20
- 自动写入只留真正说出口的内容:本 World 自己的事件、状态快照、临时事件与空正文都跳过;一轮不到 24 个字不写,「嗯」「好」这种轮次不值得花服务端的抽取成本。`tests/memory.test.ts` 锁的是这几条判据。
19
+ **上传的是原文,不是摘要。** 到点把上次上传之后的对话整段写进去——人说的话、Coo 的回复、以及这段时间里的图片——嵌入与抽事实都交给 Hindsight 自己做。插件不写概要,也不替它挑内容。
21
20
 
22
- **类型白名单**(`autoRememberTypes`,默认 `message, speech, answer`)是另一道闸,看事件类型的最后一段。一轮里投递给模型的不止对话:换歌、游戏开局、天气变化都是事件。桌宠更麻烦——它说的话和「被拎起来放到屏幕某处」来自同一个 World(`desktop-pet`),按来源分不开,只有类型能分:`desktop-pet.message` 是人说的话,`desktop-pet.touch` 是动作。不在名单里的类型不进记忆库,也不花服务端的抽取成本。
21
+ 两次上传之间只收对话事件(类型白名单 `message, speech, answer`):换歌、被拎起来放到屏幕某处、换形象、游戏开局、天气变化这些环境事件不进记忆,它们不是谁说的话,写进去只是噪声。
23
22
 
24
- `autoRememberSources` 是第三道闸,按来源 World 过滤,默认留空。接了多个对话通道、只想留其中一个时才用。
23
+ 图片随原文按位置写入。服务端的抽取模型看不了图时会整条拒绝,插件去掉图片退回纯文本重试一次,并投递一条事件让模型告诉主人——成功的那条只留档,不吵醒她。
25
24
 
26
- **图片**(`autoRememberImages`,默认开)随对话一起写:图片块留在它出现的位置,服务端按「图 + 前后的话」一起抽取。实测一张白底红圆的图会被抽成「图片内容为一个白底上的红色实心圆」。这要求服务端的抽取模型能看图;不能看时整条写入会被服务端拒绝,插件去掉图片退回纯文本重试一次,并在日志里记一行。每轮最多 `maxImagesPerTurn` 张,单张超过 `maxImageBytes` 的跳过。
25
+ **记忆仍然由模型判断着补**:环境提示词告诉它什么值得单独写(主人的喜好、习惯、他身边的人与宠物、正在做的事、两个人一起经历的事、他明确要求记住的话)。整段上传是留档,不代替判断。`tests/memory.test.ts` 与 `tests/world.test.ts` 锁的是这几条判据。
27
26
 
28
27
  ## 安装
29
28
 
@@ -51,15 +50,16 @@ cd <Cortico 检出>/extensions && corepack pnpm add cortico-world-hindsight
51
50
 
52
51
  | 键 | 默认 | 说明 |
53
52
  |---|---|---|
54
- | `baseUrl` | `http://192.168.1.1:8888` | Hindsight 服务根地址 |
53
+ | `baseUrl` | `http://localhost:8888` | Hindsight 服务根地址 |
55
54
  | `bankId` | `coopanion` | 写入与检索用的 bank;第一次写入时服务端会建它 |
56
- | `autoRemember` | `true` | 每轮对话结束自动写入 |
57
- | `autoRememberTypes` | `message, speech, answer` | 自动写入只收类型最后一段是这些词的事件,逗号分隔;留空表示不按类型过滤 |
58
- | `autoRememberSources` | 空 | 自动写入只收这些来源的事件,逗号分隔;留空表示不按来源过滤 |
59
- | `autoRememberMaxChars` | `4000` | 自动写入的正文上限,超出从头截断;图片不计入 |
60
- | `autoRememberImages` | `true` | 这一轮的图片随对话一起写入 |
61
- | `maxImagesPerTurn` | `4` | 一轮最多带几张图 |
62
- | `maxImageBytes` | `4000000` | 单张图片的字节上限,超过的跳过 |
55
+ | `uploadScheduleEnabled` | `true` | 每天在 `uploadScheduleAt` 整理一次 |
56
+ | `uploadScheduleAt` | `22:00` | 整理时刻,`HH:mm`,按部署时区 |
57
+ | `uploadIntervalEnabled` | `true` | 每隔一段时间整理一次 |
58
+ | `uploadIntervalMinutes` | `120` | 间隔分钟数,最少 15 |
59
+ | `uploadMaxChars` | `6000` | 一条正文的字数上限;超出切条,不截断 |
60
+ | `maxImagesPerTurn` | `4` | 一次上传最多带几张图 |
61
+ | `maxImageBytes` | `4000000` | 单张图片的字节上限,超过的跳过并在通知里说明 |
62
+ | `dedupeOnWrite` | `true` | `hindsight_remember` 写之前先查一遍,同一句话不重复写 |
63
63
  | `tags` | `source:chat, origin:coopanion` | 每条记忆带上的标签,逗号分隔 |
64
64
  | `requestTimeoutMs` | `20000` | 普通请求超时 |
65
65
  | `reflectTimeoutMs` | `120000` | 复盘超时;它要跑多轮检索再生成回答,实测十几秒 |
@@ -93,6 +93,7 @@ cd <Cortico 检出>/extensions && corepack pnpm add cortico-world-hindsight
93
93
  ## 面板
94
94
 
95
95
  - **连接**:地址、bank、API 版本、事实与文档条数、待处理与失败操作、最近写入时间、这台服务上有哪些 bank;一个「测试连接」按钮;密钥保存。
96
+ - **上传设置**:定时上传(开关 + 时刻)、间隔上传(开关 + 间隔)、下次触发时刻、待整理的对话条数、上次整理的结果,以及「立即整理一次」。
96
97
  - **记忆**:按意思搜(召回命中的原句与来处)、让服务端复盘一个问题、最近写入的 20 条。
97
98
  - **知识页面**:页面的树;按词搜页面。
98
99
 
@@ -104,16 +105,29 @@ cd <Cortico 检出>/extensions && corepack pnpm add cortico-world-hindsight
104
105
 
105
106
  复盘很慢(服务端多轮检索加一次生成),也只在这种跨记忆的问题上值得用;单条事实用召回。
106
107
 
108
+ ## 仓库内容
109
+
110
+ 发布出去的只有运行所需的文件:
111
+
112
+ | 路径 | 作用 |
113
+ |---|---|
114
+ | `src/` | World 实现:客户端、配置、工具、上传计划、面板源码 |
115
+ | `dist/webui.js` | 面板产物,manifest 的 `webUiClient` 指向它 |
116
+ | `package.json` / `describe.json` | 扩展清单与自描述 |
117
+ | `README.md` / `CHANGELOG.md` / `LICENSE` | 文档与许可 |
118
+
119
+ 测试、构建配置与草稿脚本不在仓库里;它们只留在开发机上,不影响安装与运行。
120
+
107
121
  ## 开发
108
122
 
123
+ 改源码之后重建面板产物:
124
+
109
125
  ```bash
110
126
  corepack pnpm install
111
127
  corepack pnpm run build # 面板产物 dist/webui.js
112
- corepack pnpm exec vitest run # 60 个用例
113
- corepack pnpm run typecheck # Node 与面板两侧
114
128
  ```
115
129
 
116
- 契约校验要回到 Cortico 检出:
130
+ 改了工具声明、事件、环境提示词或 `webUi()` 的声明之后,要在 Cortico 检出里重新生成自描述并校验:
117
131
 
118
132
  ```bash
119
133
  cd <cortico 检出>
@@ -121,4 +135,4 @@ corepack pnpm exec tsx scripts/extension-describe.ts <这个目录> # 重新
121
135
  corepack pnpm exec tsx scripts/extension-check.ts <这个目录>
122
136
  ```
123
137
 
124
- 改了工具声明、事件、环境提示词或 `webUi()` 的声明之后要重新生成 `describe.json`,`check:extension` 会核对它与代码一致。
138
+ `check:extension` 会核对 `describe.json` 与代码一致,并验证构造、工具与面板声明。
package/describe.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "tools": [
3
3
  {
4
4
  "name": "hindsight_remember",
5
- "description": "Store something in long-term memory that should outlive this conversation: who the owner is, what they like or dislike, decisions, promises, things that happened. The server extracts facts from the text, so write complete sentences and name the people involved instead of using pronouns. A turn is already retained automatically, so use this for what deserves to be found later, or to correct what was stored. Returns how much was stored.",
5
+ "description": "Store something worth keeping for months in long-term memory: who the owner is and what they like, dislike or care about, the people, pets and projects around them, things that happened between you, anything they ask you to remember. Write one call per thing. The server extracts facts from the text, so use complete sentences and name the people involved instead of pronouns. Skip what only matters for the next few minutes. Returns how much was stored.",
6
6
  "parameters": {
7
7
  "type": "object",
8
8
  "additionalProperties": false,
@@ -157,13 +157,28 @@
157
157
  }
158
158
  }
159
159
  ],
160
- "events": [],
160
+ "events": [
161
+ {
162
+ "type": "hindsight.uploaded",
163
+ "description": {
164
+ "zh": "按计划把这一段的完整对话写进了记忆库;只留档,不投递给模型。",
165
+ "en": "A stretch of conversation was written into the bank on schedule; archived, not delivered to the model."
166
+ }
167
+ },
168
+ {
169
+ "type": "hindsight.upload-failed",
170
+ "description": {
171
+ "zh": "按计划整理记忆失败,或图片被服务端拒了;这条会投递,让模型告诉主人。",
172
+ "en": "A scheduled upload failed, or its images were rejected; this one is delivered so the model can tell the owner."
173
+ }
174
+ }
175
+ ],
161
176
  "prompts": [
162
177
  {
163
178
  "key": "worlds.hindsight.envPrompt",
164
179
  "title": "Hindsight · 环境提示词",
165
180
  "role": "envPrompt",
166
- "content": "你的长期记忆放在一台自建的 Hindsight 服务上,它与这次对话的上下文是分开的。\n\n- 服务:{{hindsight.endpoint}}\n- Memory bank:{{hindsight.bank}}\n- 此刻:{{hindsight.state}}\n- 每轮自动写入:{{hindsight.autoRemember}}\n\n每轮对话结束时,这一轮说了什么会被自动写进 bank,服务端在后台把它拆成事实。自动写入只收对话事件:主人打的字、说的话、对提问的回答。换歌、被拎起来放到屏幕某处、换形象、游戏开局、天气变化这些环境事件不进记忆,它们不是谁说的话。所以你不需要为「留个档」再调一次 `hindsight_remember`;那个工具用在自动写入做不好的地方:主人明确要你记住的事、比一轮对话更值得留下的结论、或者需要改掉以前记错的东西。写的时候写完整的句子,把人和物点明,别用「他」「那个」——拆事实的模型看不到你眼前的对话。\n\n想知道以前的事就查,别猜。拿一件具体的事实用 `hindsight_recall`,一次把问题问清楚;问「关于 X 你知道什么」这类要跨很多条记忆的问题用 `hindsight_reflect`,它慢,会跑十几秒。`hindsight_pages` 看的是服务端整理出来的页面,适合先摸清一个话题有哪些内容,找准确说法仍然用 `hindsight_recall`。\n\n召回的结果是记忆里存的句子,不是已经核对过的答案。里面提到的时间、数字和别人的说法都带着当时的情境;与眼前的事实冲突时,以眼前的事实为准,并考虑用 `hindsight_remember` 把新的情况写进去覆盖旧的。记忆里没有的东西就说没有,不要拿常识补齐。\n\n这些工具都在这一台机器上直接请求 {{hindsight.endpoint}}:服务连不上时回执会写明原因,换一种问法也不会变通。\n"
181
+ "content": "你的长期记忆放在一台自建的 Hindsight 服务上,它与这次对话的上下文是分开的:那边只有写进去的东西。\n\n{{hindsight.uploadPlan}}\n\n除了自动整理的对话,还有值得单独写下来的事:主人这个人——名字与称呼、喜好与厌恶、习惯、作息、在意的事;他身边的人、宠物、正在做的项目;你们之间发生过的、以后还会提到的事;他明确要你记住的话;需要改掉以前记错的东西。判断标准是「下次聊起来还用得上吗」。\n\n写的时候:一条只写一件事,几件不相关的分几次调用。写完整的句子,把人和物点明,别用「他」「那个」——拆事实的模型看不到你眼前的对话。跨天的事带上日期,别只写「今天」。主人说的话和他自己的推测要分开措辞,别把猜测记成事实。\n\n想知道以前的事就查,别猜。拿一件具体的事实用 `hindsight_recall`,一次把问题问清楚;问「关于 X 你知道什么」这类要跨很多条记忆的问题用 `hindsight_reflect`,它慢,会跑十几秒。`hindsight_pages` 看的是服务端整理出来的页面,适合先摸清一个话题有哪些内容,找准确说法仍然用 `hindsight_recall`。\n\n召回的结果是记忆里存的句子,不是已经核对过的答案。里面提到的时间、数字和别人的说法都带着当时的情境;与眼前的事实冲突时,以眼前的事实为准,并考虑用 `hindsight_remember` 把新的情况写进去。记忆里没有的东西就说没有,不要拿常识补齐。\n\n记忆服务连不上时,工具回执会写明原因;换一种问法重试不会变通,把原因告诉主人就行。\n"
167
182
  }
168
183
  ]
169
184
  }
package/dist/webui.js CHANGED
@@ -16,7 +16,6 @@ var zh = {
16
16
  failed: "\u5931\u8D25",
17
17
  lastWrite: "\u6700\u8FD1\u5199\u5165",
18
18
  tags: "\u8BB0\u5FC6\u6807\u7B7E",
19
- autoRemember: "\u6BCF\u8F6E\u81EA\u52A8\u5199\u5165",
20
19
  on: "\u5F00",
21
20
  off: "\u5173",
22
21
  unset: "\u2014",
@@ -56,6 +55,30 @@ var zh = {
56
55
  noHits: "\u6CA1\u6709\u5339\u914D\u7684\u9875\u9762\u3002",
57
56
  stale: "\u5F85\u5237\u65B0",
58
57
  failed: "\u5931\u8D25"
58
+ },
59
+ upload: {
60
+ title: "\u4E0A\u4F20\u8BBE\u7F6E",
61
+ desc: "\u5230\u70B9\u628A\u8FD9\u4E00\u6BB5\u7684\u5B8C\u6574\u5BF9\u8BDD\u5199\u8FDB\u8BB0\u5FC6\u5E93\uFF0C\u6458\u8981\u4EA4\u7ED9 Hindsight \u81EA\u5DF1\u505A\u3002",
62
+ schedule: "\u5B9A\u65F6\u4E0A\u4F20",
63
+ scheduleAt: "\u65F6\u523B",
64
+ interval: "\u95F4\u9694\u4E0A\u4F20",
65
+ intervalMinutes: "\u95F4\u9694",
66
+ minutes: "\u5206\u949F",
67
+ save: "\u4FDD\u5B58",
68
+ saved: "\u5DF2\u4FDD\u5B58",
69
+ now: "\u7ACB\u5373\u6574\u7406\u4E00\u6B21",
70
+ uploading: "\u6574\u7406\u4E2D\u2026",
71
+ nextDaily: "\u4E0B\u6B21\u5B9A\u65F6",
72
+ nextInterval: "\u4E0B\u6B21\u95F4\u9694",
73
+ pending: "\u5F85\u6574\u7406",
74
+ pendingUnit: "\u6761\u5BF9\u8BDD",
75
+ lastUpload: "\u4E0A\u6B21\u6574\u7406",
76
+ never: "\u8FD8\u6CA1\u6709",
77
+ nothing: "\u8FD9\u6BB5\u65F6\u95F4\u6CA1\u6709\u5BF9\u8BDD\uFF0C\u4EC0\u4E48\u4E5F\u6CA1\u5199",
78
+ ok: "\u6210\u529F",
79
+ bad: "\u5931\u8D25",
80
+ hint: "\u5173\u6389\u4E24\u4E2A\u5F00\u5173\u5C31\u5B8C\u5168\u624B\u52A8\uFF1A\u53EA\u6709\u6A21\u578B\u81EA\u5DF1\u8C03\u8BB0\u5FC6\u5DE5\u5177\u65F6\u624D\u5199\u3002",
81
+ failed: "\u5931\u8D25"
59
82
  }
60
83
  };
61
84
  var en = {
@@ -75,7 +98,6 @@ var en = {
75
98
  failed: "Failed",
76
99
  lastWrite: "Last write",
77
100
  tags: "Memory tags",
78
- autoRemember: "Retain every turn",
79
101
  on: "on",
80
102
  off: "off",
81
103
  unset: "\u2014",
@@ -115,6 +137,30 @@ var en = {
115
137
  noHits: "No matching page.",
116
138
  stale: "stale",
117
139
  failed: "Failed"
140
+ },
141
+ upload: {
142
+ title: "Upload settings",
143
+ desc: "At each trigger the full text of the conversation goes into the bank; summarizing is Hindsight's job.",
144
+ schedule: "Upload daily",
145
+ scheduleAt: "At",
146
+ interval: "Upload on an interval",
147
+ intervalMinutes: "Every",
148
+ minutes: "min",
149
+ save: "Save",
150
+ saved: "Saved",
151
+ now: "Upload now",
152
+ uploading: "Uploading\u2026",
153
+ nextDaily: "Next daily",
154
+ nextInterval: "Next interval",
155
+ pending: "Waiting",
156
+ pendingUnit: "line(s)",
157
+ lastUpload: "Last upload",
158
+ never: "not yet",
159
+ nothing: "No conversation in this stretch; nothing was written",
160
+ ok: "ok",
161
+ bad: "failed",
162
+ hint: "With both switches off nothing uploads on its own: only the memory tools write.",
163
+ failed: "Failed"
118
164
  }
119
165
  };
120
166
  var stringsFor = (language) => language.toLowerCase().startsWith("zh") ? zh : en;
@@ -164,7 +210,6 @@ var connectionPanel = {
164
210
  [S.endpoint, state.baseUrl || S.unset],
165
211
  [S.bank, state.bank],
166
212
  [S.version, state.apiVersion || S.unset],
167
- [S.autoRemember, state.autoRemember ? S.on : S.off],
168
213
  [S.tags, state.tags.length ? state.tags.join(", ") : S.unset]
169
214
  ];
170
215
  if (state.stats) {
@@ -341,9 +386,83 @@ var pagesPanel = {
341
386
  await load();
342
387
  }
343
388
  };
389
+ var uploadPanel = {
390
+ mount: async (ctx) => {
391
+ const S = stringsFor(ctx.language).upload;
392
+ const { ui, root } = ctx;
393
+ const sheet = ui.sheet({ title: S.title, desc: S.desc });
394
+ const status = ui.msgline("");
395
+ const now = ui.button(S.now, { variant: "primary" });
396
+ const bar = ui.rowbar();
397
+ bar.append(now, status);
398
+ const schedule = ui.checkbox(S.schedule, { checked: true });
399
+ const scheduleAt = ui.input({ placeholder: "22:00", cls: "mono" });
400
+ const interval = ui.checkbox(S.interval, { checked: true });
401
+ const intervalMinutes = ui.input({ type: "number", placeholder: "120" });
402
+ const save = ui.button(S.save, { variant: "primary" });
403
+ const scheduleRow = ui.rowbar();
404
+ scheduleRow.append(schedule.el);
405
+ const scheduleAtRow = ui.rowbar();
406
+ scheduleAtRow.append(ui.field(S.scheduleAt, scheduleAt));
407
+ const intervalRow = ui.rowbar();
408
+ intervalRow.append(interval.el);
409
+ const intervalRow2 = ui.rowbar();
410
+ intervalRow2.append(ui.field(`${S.intervalMinutes}\uFF08${S.minutes}\uFF09`, intervalMinutes));
411
+ const saveRow = ui.rowbar();
412
+ saveRow.append(save);
413
+ const hint = ui.msgline(S.hint);
414
+ const facts = ui.table();
415
+ sheet.body.append(bar, scheduleRow, scheduleAtRow, intervalRow, intervalRow2, saveRow, status, facts.el, hint);
416
+ root.append(sheet.el);
417
+ const render = (state) => {
418
+ schedule.setChecked(state.scheduleEnabled);
419
+ interval.setChecked(state.intervalEnabled);
420
+ if (document.activeElement !== scheduleAt) scheduleAt.value = state.scheduleAt;
421
+ if (document.activeElement !== intervalMinutes) intervalMinutes.value = String(state.intervalMinutes);
422
+ facts.clear();
423
+ const rows = [
424
+ [S.nextDaily, state.nextDailyAt ? clock(new Date(state.nextDailyAt).toISOString()) : S.never],
425
+ [S.nextInterval, state.nextIntervalAt ? clock(new Date(state.nextIntervalAt).toISOString()) : S.never],
426
+ [S.pending, `${state.pendingEvents} ${S.pendingUnit}`],
427
+ [S.lastUpload, state.lastUploadAt ? `${clock(new Date(state.lastUploadAt).toISOString())} \xB7 ${state.lastUploadOk ? S.ok : S.bad}${state.lastUploadDetail ? ` \xB7 ${state.lastUploadDetail}` : ""}` : S.never]
428
+ ];
429
+ for (const [k, v] of rows) facts.addRow([k, v]);
430
+ return state.pendingEvents > 0 ? `${state.pendingEvents} ${S.pendingUnit}` : "";
431
+ };
432
+ const load = () => void attempt(status, () => ctx.invoke("state", []), render, S.failed);
433
+ now.addEventListener(
434
+ "click",
435
+ () => void attempt(status, async () => {
436
+ status.textContent = S.uploading;
437
+ return ctx.invoke("now", []);
438
+ }, (state) => {
439
+ const result = render(state);
440
+ return result || (state.lastUploadDetail || S.nothing);
441
+ }, S.failed),
442
+ { signal: ctx.signal }
443
+ );
444
+ save.addEventListener(
445
+ "click",
446
+ () => void attempt(status, async () => {
447
+ const minutes = Math.max(15, Math.trunc(Number(intervalMinutes.value)) || 120);
448
+ const message = await ctx.setConfig("world:hindsight", {
449
+ "worlds.hindsight.uploadScheduleEnabled": schedule.checked,
450
+ "worlds.hindsight.uploadScheduleAt": scheduleAt.value.trim() || "22:00",
451
+ "worlds.hindsight.uploadIntervalEnabled": interval.checked,
452
+ "worlds.hindsight.uploadIntervalMinutes": minutes
453
+ });
454
+ return message || S.saved;
455
+ }, (text) => text, S.failed),
456
+ { signal: ctx.signal }
457
+ );
458
+ await load();
459
+ ctx.interval(() => void load(), 6e4);
460
+ }
461
+ };
344
462
  var client_default = {
345
463
  panels: {
346
464
  connection: connectionPanel,
465
+ upload: uploadPanel,
347
466
  memories: memoriesPanel,
348
467
  pages: pagesPanel
349
468
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cortico-world-hindsight",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "type": "module",
5
5
  "description": "Cortico World: long-term memory over a self-hosted Hindsight server (remember, recall, reflect, knowledge pages, automatic transcript retention)",
6
6
  "license": "MIT",
package/src/ENV_PROMPT.md CHANGED
@@ -1,14 +1,13 @@
1
- 你的长期记忆放在一台自建的 Hindsight 服务上,它与这次对话的上下文是分开的。
1
+ 你的长期记忆放在一台自建的 Hindsight 服务上,它与这次对话的上下文是分开的:那边只有写进去的东西。
2
2
 
3
- - 服务:{{hindsight.endpoint}}
4
- - Memory bank:{{hindsight.bank}}
5
- - 此刻:{{hindsight.state}}
6
- - 每轮自动写入:{{hindsight.autoRemember}}
3
+ {{hindsight.uploadPlan}}
7
4
 
8
- 每轮对话结束时,这一轮说了什么会被自动写进 bank,服务端在后台把它拆成事实。自动写入只收对话事件:主人打的字、说的话、对提问的回答。换歌、被拎起来放到屏幕某处、换形象、游戏开局、天气变化这些环境事件不进记忆,它们不是谁说的话。所以你不需要为「留个档」再调一次 `hindsight_remember`;那个工具用在自动写入做不好的地方:主人明确要你记住的事、比一轮对话更值得留下的结论、或者需要改掉以前记错的东西。写的时候写完整的句子,把人和物点明,别用「他」「那个」——拆事实的模型看不到你眼前的对话。
5
+ 除了自动整理的对话,还有值得单独写下来的事:主人这个人——名字与称呼、喜好与厌恶、习惯、作息、在意的事;他身边的人、宠物、正在做的项目;你们之间发生过的、以后还会提到的事;他明确要你记住的话;需要改掉以前记错的东西。判断标准是「下次聊起来还用得上吗」。
6
+
7
+ 写的时候:一条只写一件事,几件不相关的分几次调用。写完整的句子,把人和物点明,别用「他」「那个」——拆事实的模型看不到你眼前的对话。跨天的事带上日期,别只写「今天」。主人说的话和他自己的推测要分开措辞,别把猜测记成事实。
9
8
 
10
9
  想知道以前的事就查,别猜。拿一件具体的事实用 `hindsight_recall`,一次把问题问清楚;问「关于 X 你知道什么」这类要跨很多条记忆的问题用 `hindsight_reflect`,它慢,会跑十几秒。`hindsight_pages` 看的是服务端整理出来的页面,适合先摸清一个话题有哪些内容,找准确说法仍然用 `hindsight_recall`。
11
10
 
12
- 召回的结果是记忆里存的句子,不是已经核对过的答案。里面提到的时间、数字和别人的说法都带着当时的情境;与眼前的事实冲突时,以眼前的事实为准,并考虑用 `hindsight_remember` 把新的情况写进去覆盖旧的。记忆里没有的东西就说没有,不要拿常识补齐。
11
+ 召回的结果是记忆里存的句子,不是已经核对过的答案。里面提到的时间、数字和别人的说法都带着当时的情境;与眼前的事实冲突时,以眼前的事实为准,并考虑用 `hindsight_remember` 把新的情况写进去。记忆里没有的东西就说没有,不要拿常识补齐。
13
12
 
14
- 这些工具都在这一台机器上直接请求 {{hindsight.endpoint}}:服务连不上时回执会写明原因,换一种问法也不会变通。
13
+ 记忆服务连不上时,工具回执会写明原因;换一种问法重试不会变通,把原因告诉主人就行。
package/src/client.ts CHANGED
@@ -20,7 +20,7 @@ export class HindsightError extends Error {
20
20
  }
21
21
 
22
22
  export interface HindsightClientOptions {
23
- /** 形如 `http://192.168.1.1:8888`;末尾的斜杠由客户端去掉。 */
23
+ /** 形如 `http://localhost:8888`;末尾的斜杠由客户端去掉。 */
24
24
  baseUrl: string;
25
25
  /** 每次请求现读,密钥在部署 .env 里轮换后不必重启。空串表示不带 Authorization 头。 */
26
26
  token: () => string;
package/src/config.ts CHANGED
@@ -9,20 +9,22 @@ export interface HindsightConfigSection {
9
9
  baseUrl: string;
10
10
  /** 写入与检索用的 bank。 */
11
11
  bankId: string;
12
- /** 每轮对话结束后把这一轮写进 bank。 */
13
- autoRemember: boolean;
14
- /** 自动写入只收这些来源的事件,逗号分隔;留空表示不按来源过滤。 */
15
- autoRememberSources: string;
16
- /** 自动写入只收类型最后一段是这些词的事件,逗号分隔;留空表示不按类型过滤。 */
17
- autoRememberTypes: string;
18
- /** 自动写入正文的字数上限。 */
19
- autoRememberMaxChars: number;
20
- /** 自动写入时把这一轮里的图片一起写进去。 */
21
- autoRememberImages: boolean;
22
- /** 一轮最多带几张图。 */
12
+ /** 每天在固定时刻提醒模型整理记忆。 */
13
+ uploadScheduleEnabled: boolean;
14
+ /** 提醒的时刻,`HH:mm`,按部署时区。 */
15
+ uploadScheduleAt: string;
16
+ /** 每隔一段时间整理一次。 */
17
+ uploadIntervalEnabled: boolean;
18
+ /** 间隔的分钟数。 */
19
+ uploadIntervalMinutes: number;
20
+ /** 一段上传正文的字数上限;超过就切成多条,不丢内容。 */
21
+ uploadMaxChars: number;
22
+ /** 一次提醒最多带几张图给 `hindsight_remember` 用。 */
23
23
  maxImagesPerTurn: number;
24
24
  /** 单张图片的字节上限,超过的跳过。 */
25
25
  maxImageBytes: number;
26
+ /** 写记忆前先查一遍,同一句话已经在库里就不重复写。 */
27
+ dedupeOnWrite: boolean;
26
28
  /** 每条记忆附带的标签,逗号分隔。 */
27
29
  tags: string;
28
30
  requestTimeoutMs: number;
@@ -33,22 +35,35 @@ export interface HindsightConfigSection {
33
35
 
34
36
  export const HINDSIGHT_DEFAULTS: HindsightConfigSection = {
35
37
  enabled: false,
36
- baseUrl: 'http://192.168.1.1:8888',
38
+ baseUrl: 'http://localhost:8888',
37
39
  bankId: 'coopanion',
38
- autoRemember: true,
39
- // 来源留空:桌宠的对话与动作来自同一个 World,按来源分不开,只有类型能分。
40
- autoRememberSources: '',
41
- autoRememberTypes: 'message, speech, answer',
42
- autoRememberMaxChars: 4000,
43
- autoRememberImages: true,
40
+ uploadScheduleEnabled: true,
41
+ uploadScheduleAt: '22:00',
42
+ uploadIntervalEnabled: true,
43
+ uploadIntervalMinutes: 120,
44
+ uploadMaxChars: 6000,
44
45
  maxImagesPerTurn: 4,
45
46
  maxImageBytes: 4_000_000,
47
+ dedupeOnWrite: true,
46
48
  tags: 'source:chat, origin:coopanion',
47
49
  requestTimeoutMs: 20000,
48
50
  reflectTimeoutMs: 120000,
49
51
  healthPollSec: 60,
50
52
  };
51
53
 
54
+ /** 提醒收哪些事件:对话,不含动作与环境状态。 */
55
+ export const REMINDER_TYPES = ['message', 'speech', 'answer'] as const;
56
+
57
+ /** `HH:mm` 解析成分钟数;不合格返回 null。 */
58
+ export function parseClock(raw: string): number | null {
59
+ const m = /^(\d{1,2}):(\d{2})$/.exec(raw.trim());
60
+ if (!m) return null;
61
+ const hour = Number(m[1]);
62
+ const minute = Number(m[2]);
63
+ if (hour > 23 || minute > 59) return null;
64
+ return hour * 60 + minute;
65
+ }
66
+
52
67
  /** 访问密钥的环境变量名。面板写它,客户端每次请求现读。 */
53
68
  export const HINDSIGHT_SECRET = 'HINDSIGHT_API_TOKEN';
54
69
 
@@ -86,38 +101,42 @@ export function hindsightConfigGroup(language: Language): ConfigGroup {
86
101
  'x-hot': true,
87
102
  description: t.bankIdDescription,
88
103
  },
89
- 'worlds.hindsight.autoRemember': {
104
+ 'worlds.hindsight.uploadScheduleEnabled': {
90
105
  type: 'boolean',
91
- title: t.autoRememberTitle,
106
+ title: t.uploadScheduleEnabledTitle,
92
107
  'x-hot': true,
93
- description: t.autoRememberDescription,
108
+ description: t.uploadScheduleEnabledDescription,
94
109
  },
95
- 'worlds.hindsight.autoRememberTypes': {
110
+ 'worlds.hindsight.uploadScheduleAt': {
96
111
  type: 'string',
97
- title: t.autoRememberTypesTitle,
112
+ title: t.uploadScheduleAtTitle,
98
113
  'x-hot': true,
99
- description: t.autoRememberTypesDescription,
114
+ description: t.uploadScheduleAtDescription,
100
115
  },
101
- 'worlds.hindsight.autoRememberSources': {
102
- type: 'string',
103
- title: t.autoRememberSourcesTitle,
116
+ 'worlds.hindsight.uploadIntervalEnabled': {
117
+ type: 'boolean',
118
+ title: t.uploadIntervalEnabledTitle,
104
119
  'x-hot': true,
105
- description: t.autoRememberSourcesDescription,
120
+ description: t.uploadIntervalEnabledDescription,
106
121
  },
107
- 'worlds.hindsight.autoRememberMaxChars': {
122
+ 'worlds.hindsight.uploadIntervalMinutes': {
108
123
  type: 'integer',
109
- title: t.autoRememberMaxCharsTitle,
110
- minimum: 200,
111
- maximum: 40000,
112
- multipleOf: 100,
124
+ title: t.uploadIntervalMinutesTitle,
125
+ minimum: 15,
126
+ maximum: 1440,
127
+ multipleOf: 5,
128
+ 'x-suffix': t.minutesSuffix,
113
129
  'x-hot': true,
114
- description: t.autoRememberMaxCharsDescription,
130
+ description: t.uploadIntervalMinutesDescription,
115
131
  },
116
- 'worlds.hindsight.autoRememberImages': {
117
- type: 'boolean',
118
- title: t.autoRememberImagesTitle,
132
+ 'worlds.hindsight.uploadMaxChars': {
133
+ type: 'integer',
134
+ title: t.uploadMaxCharsTitle,
135
+ minimum: 500,
136
+ maximum: 40000,
137
+ multipleOf: 500,
119
138
  'x-hot': true,
120
- description: t.autoRememberImagesDescription,
139
+ description: t.uploadMaxCharsDescription,
121
140
  },
122
141
  'worlds.hindsight.maxImagesPerTurn': {
123
142
  type: 'integer',
@@ -138,6 +157,12 @@ export function hindsightConfigGroup(language: Language): ConfigGroup {
138
157
  'x-hot': true,
139
158
  description: t.maxImageBytesDescription,
140
159
  },
160
+ 'worlds.hindsight.dedupeOnWrite': {
161
+ type: 'boolean',
162
+ title: t.dedupeOnWriteTitle,
163
+ 'x-hot': true,
164
+ description: t.dedupeOnWriteDescription,
165
+ },
141
166
  'worlds.hindsight.tags': {
142
167
  type: 'string',
143
168
  title: t.tagsTitle,
package/src/definition.ts CHANGED
@@ -6,7 +6,7 @@ export const HINDSIGHT: WorldDefinition<HindsightConfigSection> = {
6
6
  id: 'hindsight',
7
7
  label: 'Hindsight',
8
8
  defaults: () => ({ ...HINDSIGHT_DEFAULTS }),
9
- // `ctx.cfg` 是 `worlds.hindsight` 的活引用:地址、bank、超时与自动写入开关现读即生效。
9
+ // `ctx.cfg` 是 `worlds.hindsight` 的活引用:地址、bank、超时与上传计划现读即生效。
10
10
  create: (ctx) =>
11
11
  new HindsightWorld({
12
12
  cfg: ctx.cfg,
@@ -37,6 +37,21 @@ export const HINDSIGHT: WorldDefinition<HindsightConfigSection> = {
37
37
  en: 'Reports the server, whether it is reachable, and how much memory there is.',
38
38
  },
39
39
  }),
40
- events: [],
40
+ events: [
41
+ {
42
+ type: 'hindsight.uploaded',
43
+ description: {
44
+ zh: '按计划把这一段的完整对话写进了记忆库;只留档,不投递给模型。',
45
+ en: 'A stretch of conversation was written into the bank on schedule; archived, not delivered to the model.',
46
+ },
47
+ },
48
+ {
49
+ type: 'hindsight.upload-failed',
50
+ description: {
51
+ zh: '按计划整理记忆失败,或图片被服务端拒了;这条会投递,让模型告诉主人。',
52
+ en: 'A scheduled upload failed, or its images were rejected; this one is delivered so the model can tell the owner.',
53
+ },
54
+ },
55
+ ],
41
56
  },
42
57
  };
package/src/memory.ts CHANGED
@@ -21,6 +21,8 @@ export interface TranscriptEvent {
21
21
  text: string;
22
22
  tags?: readonly string[];
23
23
  ephemeral?: true;
24
+ /** 这条已经由调用方选好,不再过类型与来源两道闸。 */
25
+ verbatim?: true;
24
26
  /** 这条事件带的图片,按出现顺序。 */
25
27
  images?: readonly TranscriptImage[];
26
28
  }
@@ -115,8 +117,10 @@ export function buildTranscript(events: readonly TranscriptEvent[], opts: Transc
115
117
 
116
118
  for (const event of events) {
117
119
  if (event.source === opts.selfId) continue;
118
- if (opts.sources?.length && !opts.sources.includes(event.source)) continue;
119
- if (opts.types?.length && !opts.types.includes(eventKind(event.type))) continue;
120
+ if (!event.verbatim) {
121
+ if (opts.sources?.length && !opts.sources.includes(event.source)) continue;
122
+ if (opts.types?.length && !opts.types.includes(eventKind(event.type))) continue;
123
+ }
120
124
  if (event.ephemeral) continue;
121
125
  if (event.tags?.includes('snapshot')) continue;
122
126
 
package/src/model-text.ts CHANGED
@@ -31,6 +31,11 @@ export const MODEL_TEXT = {
31
31
  rememberAsync: (bank: string): string =>
32
32
  `Queued for bank "${bank}". The server extracts in the background; nothing is searchable until it finishes.`,
33
33
 
34
+ /** 库里已经有这句一样的话,没有重复写入。 */
35
+ rememberDuplicate: (existing: string): string =>
36
+ `Already in memory, word for word, so nothing was written: "${existing.trim()}". `
37
+ + 'Write it again only if something actually changed; that is what supersedes the old one.',
38
+
34
39
  recallEmpty: (query: string): string => `Nothing in memory matches "${query}".`,
35
40
 
36
41
  recallHead: (query: string, hits: number, bank: string): string =>
@@ -110,6 +115,24 @@ export const MODEL_TEXT = {
110
115
  /** 自动写入失败时给操作者看的一行,进 World 日志。 */
111
116
  rememberFailed: (error: string): string => `automatic retain failed: ${error}`,
112
117
 
118
+ /** 按计划上传成功:只落库,不吵醒模型。 */
119
+ uploaded: (detail: string): string =>
120
+ `The conversation since the last upload is now in long-term memory (${detail}). `
121
+ + 'It is the full text, so the server extracts the facts itself; nothing else needs writing for this stretch.',
122
+
123
+ /** 上传失败:投递给模型,让主人知道记忆没写进去。 */
124
+ uploadFailed: (detail: string): string =>
125
+ `[hindsight upload failed] The conversation since the last upload could not be written to memory: ${detail}. `
126
+ + 'Tell the owner the memory may be missing this stretch.',
127
+
128
+ /** 图片被服务端拒了,文字照常写入:投递给模型说明。 */
129
+ uploadImagesDropped: (detail: string): string =>
130
+ `[hindsight upload: images dropped] The text went in, but the images did not: ${detail}. `
131
+ + 'The server\'s extraction model may not accept images.',
132
+
133
+ imagesRejectedInUpload: (error: string, images: number): string =>
134
+ `${images} image(s) rejected, text only: ${error}`,
135
+
113
136
  /** 服务端拒绝了图片:这一轮退回纯文本,日志里说明原因。 */
114
137
  imagesRejected: (error: string): string =>
115
138
  `retain with images was rejected, retrying as text: ${error}`,