cortico-world-self-awareness 0.4.1 → 0.5.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/CHANGELOG.md +21 -0
- package/README.md +15 -0
- package/package.json +1 -1
- package/src/pool.ts +20 -0
- package/src/think.ts +33 -7
- package/src/world.ts +29 -7
- package/webui/index.html +5 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.5.0
|
|
4
|
+
|
|
5
|
+
**她自己定下次什么时候想、想什么。**
|
|
6
|
+
|
|
7
|
+
- 输出协议加 `next`:`{"in":300,"note":"天亮了还惦记着,就看看戒指到底烫不烫"}`。
|
|
8
|
+
她给自己留一句接续的话和大致间隔,下次思考时原样递回去。
|
|
9
|
+
- **间隔优先用她说的**(15–360 分钟),她没说才按密度算。
|
|
10
|
+
**上限从 90 分钟放到 6 小时**——原来 90 分钟锁死,一天必然 12 次,那才是「定时感」的来源。
|
|
11
|
+
- 同一条留话只递一次;超过 12 小时不递(她说「明早看」,第二天晚上再提就是翻旧账)。
|
|
12
|
+
- 时间线每条下面会显示「留给自己:…(300 分钟后)」。
|
|
13
|
+
- **提示词位置很重要**:把要求写进规则列表之后才生效。放在末尾的格式注释里时,
|
|
14
|
+
长上下文下模型会忽略——实测连续 6 次一条都没留;移进规则后三种模式 3/3 都留,
|
|
15
|
+
而且留的是「天亮前要还是绕在这句上,就换个法子,别老在心里追问」这种真能接的话。
|
|
16
|
+
- 测试 39–44(6 项):她定 300 就 300、留话原样递回、只递最新那条、没留时按密度兜底。
|
|
17
|
+
|
|
18
|
+
### 验证时踩的坑
|
|
19
|
+
|
|
20
|
+
端到端跑了 4 次 tick 都没写进文件,我以为功能坏了——实际是**记忆核心判定她 `sleeping`,被 `sleepSilent` 拦住了**。
|
|
21
|
+
这个闸门是对的,但测试时得显式关掉。
|
|
22
|
+
|
|
23
|
+
## 0.4.1
|
|
3
24
|
## 0.4.1
|
|
4
25
|
|
|
5
26
|
- 「用哪家」改成「**快捷预设**」,说明也重写了。原来那句「选一个就行」会让人以为它决定用哪家服务,
|
package/README.md
CHANGED
|
@@ -36,6 +36,21 @@
|
|
|
36
36
|
|
|
37
37
|
所以两次思考的间隔是不规则的,但每次都有来源。
|
|
38
38
|
|
|
39
|
+
**更准确地说:间隔现在由她自己定。**
|
|
40
|
+
|
|
41
|
+
她每想完一件事,可以给自己留一句「下次从哪儿接着想」和一个大致间隔(15 分钟到 6 小时)。
|
|
42
|
+
下次思考时,那句话会原样递回到她眼前。她没留,才按上面的密度算。
|
|
43
|
+
|
|
44
|
+
于是会出现这种连续:
|
|
45
|
+
|
|
46
|
+
```
|
|
47
|
+
23:33 那戒指还在怀里揣着,烫不烫啊…
|
|
48
|
+
留给自己:天亮了还惦记着,就看看戒指到底烫不烫
|
|
49
|
+
|
|
50
|
+
01:23 那晚第四遍猛鬼街…到现在还惦记着
|
|
51
|
+
下次 300 分钟后再想
|
|
52
|
+
```
|
|
53
|
+
|
|
39
54
|
**例外是有人在说话。** 对方说完话后的 20 分钟里她不会去想事——人在聊天时,她该在听,不是走神。这个时长可以调,填 0 关掉。
|
|
40
55
|
|
|
41
56
|
---
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cortico-world-self-awareness",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "桌宠自己的内在时间线:没人说话的时候她也在想事情——想起旧事、走神、反复琢磨一件没想明白的。\n\n它只负责「想」:想到的写在本机一个文件里,不发消息,也不占对话的 token。配合「记忆核心」使用——你开口时,记忆核心会挑一条带进上下文,标明时间,并说清那不一定是你现在的想法。单独装也能跑,但那是一本她自己不会翻的日记。\n\n模型自选,OpenAI 兼容接口就行;默认一天最多 12 次,一次几分钱。\n\nQQ 交流群:745879632",
|
|
6
6
|
"keywords": [
|
package/src/pool.ts
CHANGED
|
@@ -26,6 +26,10 @@ export interface Thought {
|
|
|
26
26
|
outputTokens?: number;
|
|
27
27
|
/** 这条是接着上一条想下去的,不是新起一件 */
|
|
28
28
|
carry?: boolean;
|
|
29
|
+
/** 她自己定的下次间隔(分钟)。没写就按密度算。 */
|
|
30
|
+
nextIn?: number;
|
|
31
|
+
/** 她给自己留的话:「明早再看看那个帖子」。下次会原样递回去。 */
|
|
32
|
+
nextNote?: string;
|
|
29
33
|
}
|
|
30
34
|
|
|
31
35
|
export interface Reflection {
|
|
@@ -97,6 +101,8 @@ export class ThoughtPool {
|
|
|
97
101
|
...(t.promptTokens ? { promptTokens: t.promptTokens } : {}),
|
|
98
102
|
...(t.outputTokens ? { outputTokens: t.outputTokens } : {}),
|
|
99
103
|
...(t.carry ? { carry: true } : {}),
|
|
104
|
+
...(t.nextIn ? { nextIn: t.nextIn } : {}),
|
|
105
|
+
...(t.nextNote ? { nextNote: t.nextNote } : {}),
|
|
100
106
|
};
|
|
101
107
|
ensureDir(this.dir);
|
|
102
108
|
// 先让 cache 落地再 append:否则 all() 会从文件里读回刚写的那条,push 之后就有了两份
|
|
@@ -174,6 +180,17 @@ export function clamp01(n: number): number {
|
|
|
174
180
|
return Math.max(0, Math.min(1, Number.isFinite(n) ? n : 0));
|
|
175
181
|
}
|
|
176
182
|
|
|
183
|
+
/** 她自己说的「多久之后再想」的允许范围。上下都留边:说 1 分钟太密,说三天就断了。 */
|
|
184
|
+
export const NEXT_MIN_MINUTES = 15;
|
|
185
|
+
export const NEXT_MAX_MINUTES = 360;
|
|
186
|
+
|
|
187
|
+
/** 把模型给的值收进合法范围;不是数字就当没写。 */
|
|
188
|
+
export function clampNextIn(value: unknown): number | undefined {
|
|
189
|
+
const v = Number(value);
|
|
190
|
+
if (!Number.isFinite(v) || v <= 0) return undefined;
|
|
191
|
+
return Math.round(Math.max(NEXT_MIN_MINUTES, Math.min(NEXT_MAX_MINUTES, v)));
|
|
192
|
+
}
|
|
193
|
+
|
|
177
194
|
/** 累计重要度到这条线就该往上抽象一层了(写一条 reflection)。 */
|
|
178
195
|
export const REFLECT_THRESHOLD = 24;
|
|
179
196
|
|
|
@@ -184,6 +201,8 @@ export interface BudgetState {
|
|
|
184
201
|
pendingImportance: number;
|
|
185
202
|
lastThoughtAt?: string;
|
|
186
203
|
nextDueAt?: string;
|
|
204
|
+
/** 她留的那句话已经递过一次了,别重复递。 */
|
|
205
|
+
lastNoteId?: string;
|
|
187
206
|
}
|
|
188
207
|
|
|
189
208
|
const BUDGET_FILE = 'state.json';
|
|
@@ -200,6 +219,7 @@ export function loadBudget(dir: string): BudgetState {
|
|
|
200
219
|
pendingImportance: Number(raw.pendingImportance) || 0,
|
|
201
220
|
...(raw.lastThoughtAt ? { lastThoughtAt: String(raw.lastThoughtAt) } : {}),
|
|
202
221
|
...(raw.nextDueAt ? { nextDueAt: String(raw.nextDueAt) } : {}),
|
|
222
|
+
...(raw.lastNoteId ? { lastNoteId: String(raw.lastNoteId) } : {}),
|
|
203
223
|
};
|
|
204
224
|
} catch {
|
|
205
225
|
return { day: '', count: 0, tokens: 0, pendingImportance: 0 };
|
package/src/think.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* 全部用「你就是她」的第二人称写提示词——用第三人称会写成分析报告。
|
|
6
6
|
*/
|
|
7
7
|
import type { Thought, ThoughtMode } from './pool.ts';
|
|
8
|
-
import { clampImportance, density } from './pool.ts';
|
|
8
|
+
import { clampImportance, clampNextIn, density } from './pool.ts';
|
|
9
9
|
import { clip } from './time.ts';
|
|
10
10
|
|
|
11
11
|
export interface ThinkContext {
|
|
@@ -23,6 +23,8 @@ export interface ThinkContext {
|
|
|
23
23
|
selfView?: string;
|
|
24
24
|
/** 她是谁(配置里那句话) */
|
|
25
25
|
persona?: string;
|
|
26
|
+
/** 上次她给自己留的那句话,原样递回来 */
|
|
27
|
+
lastNote?: string;
|
|
26
28
|
/** 这次是接着上一条想下去,不是新起一件 */
|
|
27
29
|
carry?: boolean;
|
|
28
30
|
}
|
|
@@ -37,10 +39,13 @@ ${persona ? `你是谁:${persona}\n` : ''}
|
|
|
37
39
|
- 别汇报工作,别复述别人交给你做的事。独处时想的不是待办。
|
|
38
40
|
- 想不通就想不通,可以停在半路。人独处时大部分念头都没有结论。
|
|
39
41
|
- 不要提「用户」「对话」「模型」「AI」「记忆」「记录」「卡片」这些词。
|
|
42
|
+
- 想完之后,留一句「下次从哪儿接着想」和大致的间隔写进 next;想完了就空着。
|
|
40
43
|
|
|
41
44
|
只输出一行 JSON,不要别的:
|
|
42
|
-
{"thought":"你想到的那件事","importance":1}
|
|
43
|
-
importance 是 1-10:这件事对你有多重要。10 =
|
|
45
|
+
{"thought":"你想到的那件事","importance":1,"next":{"in":60,"note":"留给自己的一句话"}}
|
|
46
|
+
importance 是 1-10:这件事对你有多重要。10 = 你会一直想它。
|
|
47
|
+
next 是你给自己留的:还会想,就定个时间(15–360 分钟)和一句「下次从哪儿接着想」。
|
|
48
|
+
想完了、没什么可接的,就空着。`;
|
|
44
49
|
}
|
|
45
50
|
|
|
46
51
|
const MODE_ASK: Record<ThoughtMode, (ctx: ThinkContext) => string> = {
|
|
@@ -68,6 +73,7 @@ const MODE_ASK: Record<ThoughtMode, (ctx: ThinkContext) => string> = {
|
|
|
68
73
|
export function buildMessages(mode: ThoughtMode, ctx: ThinkContext): { role: string; content: string }[] {
|
|
69
74
|
const lines: string[] = [];
|
|
70
75
|
lines.push(`(现在 ${ctx.now}。${ctx.silence ? `上次跟人说话是 ${ctx.silence}前。` : ''})`);
|
|
76
|
+
if (ctx.lastNote) lines.push(`(上次你自己留了一句:${ctx.lastNote})`);
|
|
71
77
|
if (ctx.selfView) lines.push(`(你最近对自己的看法:${ctx.selfView})`);
|
|
72
78
|
if (ctx.memories.length) {
|
|
73
79
|
lines.push('(你记得的事:');
|
|
@@ -164,15 +170,32 @@ export async function callModel(opts: ModelOptions, messages: { role: string; co
|
|
|
164
170
|
* 把模型输出收成一条想法。
|
|
165
171
|
* 小模型经常不守 JSON 格式(多一层 ```、前面加一句「好的」),所以逐级降级。
|
|
166
172
|
*/
|
|
167
|
-
export
|
|
173
|
+
export interface ParsedThought {
|
|
174
|
+
text: string;
|
|
175
|
+
importance: number;
|
|
176
|
+
next?: { in?: number; note?: string };
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** 她给自己留的那句:范围收进 15–360 分钟,note 超过 40 字截断。都不合法就当没留。 */
|
|
180
|
+
export function parseNext(value: unknown): { in?: number; note?: string } | undefined {
|
|
181
|
+
if (!value || typeof value !== 'object') return undefined;
|
|
182
|
+
const o = value as { in?: unknown; note?: unknown };
|
|
183
|
+
const inMin = clampNextIn(o.in);
|
|
184
|
+
const note = typeof o.note === 'string' && o.note.trim() ? clip(o.note.trim(), 40) : undefined;
|
|
185
|
+
if (inMin === undefined && !note) return undefined;
|
|
186
|
+
return { ...(inMin !== undefined ? { in: inMin } : {}), ...(note ? { note } : {}) };
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function parseThought(raw: string): ParsedThought {
|
|
168
190
|
const src = String(raw ?? '').trim();
|
|
169
191
|
|
|
170
|
-
const tryJson = (s: string):
|
|
192
|
+
const tryJson = (s: string): ParsedThought | null => {
|
|
171
193
|
try {
|
|
172
|
-
const o = JSON.parse(s) as { thought?: unknown; text?: unknown; importance?: unknown };
|
|
194
|
+
const o = JSON.parse(s) as { thought?: unknown; text?: unknown; importance?: unknown; next?: unknown };
|
|
173
195
|
const text = String(o.thought ?? o.text ?? '').trim();
|
|
174
196
|
if (!text) return null;
|
|
175
|
-
|
|
197
|
+
const next = parseNext(o.next);
|
|
198
|
+
return { text, importance: clampImportance(o.importance), ...(next ? { next } : {}) };
|
|
176
199
|
} catch {
|
|
177
200
|
return null;
|
|
178
201
|
}
|
|
@@ -231,6 +254,9 @@ export async function thinkOnce(
|
|
|
231
254
|
text: clip(parsed.text, 400),
|
|
232
255
|
importance: parsed.importance,
|
|
233
256
|
seeds,
|
|
257
|
+
// 她自己留的那句要带出去,不然解析了也白解析
|
|
258
|
+
...(parsed.next?.in ? { nextIn: parsed.next.in } : {}),
|
|
259
|
+
...(parsed.next?.note ? { nextNote: parsed.next.note } : {}),
|
|
234
260
|
promptTokens: res.promptTokens,
|
|
235
261
|
outputTokens: res.outputTokens,
|
|
236
262
|
},
|
package/src/world.ts
CHANGED
|
@@ -274,7 +274,8 @@ export class SelfAwarenessWorld implements World {
|
|
|
274
274
|
|
|
275
275
|
const { mode, carry } = this.nextMode();
|
|
276
276
|
const seeds = this.pickSeeds(mode, now);
|
|
277
|
-
const
|
|
277
|
+
const note = this.latestNote(now);
|
|
278
|
+
const ctx = this.buildContext(core, now, seeds, carry, note?.text);
|
|
278
279
|
|
|
279
280
|
const started = Date.now();
|
|
280
281
|
const res = await thinkOnce(
|
|
@@ -310,12 +311,12 @@ export class SelfAwarenessWorld implements World {
|
|
|
310
311
|
this.budget.pendingImportance += thought.importance;
|
|
311
312
|
this.budget.lastThoughtAt = thought.ts;
|
|
312
313
|
|
|
313
|
-
//
|
|
314
|
-
const
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
Number(cfg.maxGapMin) || 90
|
|
318
|
-
);
|
|
314
|
+
// 下次隔多久:她自己说了就听她的,没说才按密度算
|
|
315
|
+
const herNext = Number(res.thought.nextIn);
|
|
316
|
+
const gap = Number.isFinite(herNext) && herNext > 0
|
|
317
|
+
? herNext
|
|
318
|
+
: nextGapMinutes(density(thought), Number(cfg.minGapMin) || 25, Number(cfg.maxGapMin) || 90);
|
|
319
|
+
if (note) this.budget.lastNoteId = note.id;
|
|
319
320
|
this.budget.nextDueAt = new Date(Date.now() + Math.round(gap * 60_000)).toISOString();
|
|
320
321
|
saveBudget(this.dataDir, this.budget);
|
|
321
322
|
|
|
@@ -329,6 +330,7 @@ export class SelfAwarenessWorld implements World {
|
|
|
329
330
|
mode: thought.mode,
|
|
330
331
|
importance: thought.importance,
|
|
331
332
|
gapMin: Math.round(gap),
|
|
333
|
+
byHer: Number.isFinite(herNext) && herNext > 0,
|
|
332
334
|
ms: Date.now() - started,
|
|
333
335
|
});
|
|
334
336
|
} catch {
|
|
@@ -404,6 +406,22 @@ export class SelfAwarenessWorld implements World {
|
|
|
404
406
|
return this.lastActivityAt;
|
|
405
407
|
}
|
|
406
408
|
|
|
409
|
+
/**
|
|
410
|
+
* 她上一条给自己留的话。超过 12 小时就不提了——她说「明早看看」,
|
|
411
|
+
* 现在都第二天晚上了,再提就是翻旧账。用过的记在 budget.lastNoteId,不重复递。
|
|
412
|
+
*/
|
|
413
|
+
private latestNote(now: number): { id: string; text: string } | null {
|
|
414
|
+
const recent = this.pool.recent(6).reverse();
|
|
415
|
+
for (const t of recent) {
|
|
416
|
+
if (!t.nextNote) continue;
|
|
417
|
+
if (t.id === this.budget.lastNoteId) continue;
|
|
418
|
+
const at = Date.parse(t.ts) || 0;
|
|
419
|
+
if (!at || now - at > 12 * 3_600_000) continue;
|
|
420
|
+
return { id: t.id, text: t.nextNote };
|
|
421
|
+
}
|
|
422
|
+
return null;
|
|
423
|
+
}
|
|
424
|
+
|
|
407
425
|
/** 记忆核心的 state.json。没装记忆核心就返回 null,不报错。 */
|
|
408
426
|
private readCoreState(): MemoryCoreState | null {
|
|
409
427
|
const dir = this.coreDir();
|
|
@@ -545,6 +563,7 @@ export class SelfAwarenessWorld implements World {
|
|
|
545
563
|
now: number,
|
|
546
564
|
seeds: Seed[],
|
|
547
565
|
carry?: string,
|
|
566
|
+
lastNote?: string,
|
|
548
567
|
): ThinkContext {
|
|
549
568
|
const tz = this.tz();
|
|
550
569
|
// 「11-15 23:30(深夜)」——只说时间她不知道那是深夜,会想出「下午出去走走」
|
|
@@ -573,6 +592,7 @@ export class SelfAwarenessWorld implements World {
|
|
|
573
592
|
silence,
|
|
574
593
|
memories,
|
|
575
594
|
recent: this.pool.recent(3).map((t) => clip(t.text, 60)),
|
|
595
|
+
lastNote,
|
|
576
596
|
focus: carry ? clip(carry, 50) : seeds[0] ? clip(seeds[0].text, 50) : undefined,
|
|
577
597
|
carry: Boolean(carry),
|
|
578
598
|
selfView: reflections.length ? clip(reflections.slice(-1)[0].text, 80) : undefined,
|
|
@@ -622,6 +642,8 @@ export class SelfAwarenessWorld implements World {
|
|
|
622
642
|
text: t.text,
|
|
623
643
|
importance: t.importance,
|
|
624
644
|
carry: Boolean(t.carry),
|
|
645
|
+
nextNote: t.nextNote ?? null,
|
|
646
|
+
nextIn: t.nextIn ?? null,
|
|
625
647
|
promptTokens: t.promptTokens ?? 0,
|
|
626
648
|
outputTokens: t.outputTokens ?? 0,
|
|
627
649
|
})),
|
package/webui/index.html
CHANGED
|
@@ -75,6 +75,10 @@
|
|
|
75
75
|
.text { white-space: pre-wrap; word-break: break-word; }
|
|
76
76
|
.carry { color: var(--faint); font-size: 11px; margin-left: 4px; }
|
|
77
77
|
.empty { color: var(--faint); padding: 26px 0; }
|
|
78
|
+
.note-line {
|
|
79
|
+
margin-top: 6px; padding-left: 10px; border-left: 1px solid rgba(201, 162, 255, 0.35);
|
|
80
|
+
color: rgba(201, 162, 255, 0.75); font-size: 12px;
|
|
81
|
+
}
|
|
78
82
|
.refl { padding: 12px 16px; border-left: 2px solid var(--accent); background: var(--panel); border-radius: 0 8px 8px 0; margin-bottom: 12px; }
|
|
79
83
|
.refl .meta { margin-bottom: 6px; }
|
|
80
84
|
footer { margin-top: 40px; color: var(--faint); font-size: 11.5px; letter-spacing: .03em; }
|
|
@@ -152,6 +156,7 @@ function render(d) {
|
|
|
152
156
|
${t.carry ? '<span class="carry">接着上次</span>' : ''}
|
|
153
157
|
</div>
|
|
154
158
|
<div class="text">${esc(t.text)}</div>
|
|
159
|
+
${t.nextNote ? `<div class="note-line">留给自己:${esc(t.nextNote)}${t.nextIn ? `(${t.nextIn} 分钟后)` : ''}</div>` : ''}
|
|
155
160
|
</div>`).join('')
|
|
156
161
|
: '<div class="empty">还没有想法。<br>她每 25–90 分钟想一次,第一条通常要等几分钟。</div>';
|
|
157
162
|
|