cortico-world-self-awareness 0.2.0 → 0.4.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 +22 -0
- package/README.md +12 -0
- package/package.json +2 -1
- package/src/config.ts +21 -0
- package/src/pool.ts +221 -218
- package/src/site.ts +123 -0
- package/src/think.ts +26 -6
- package/src/time.ts +65 -37
- package/src/world.ts +196 -41
- package/webui/index.html +180 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.4.0
|
|
4
|
+
|
|
5
|
+
- **新 `synthesize` 模式**:手上两件不相干的事,看它们有没有相通的地方。四种变五种。
|
|
6
|
+
「回看 / 走神 / 打算 / 琢磨」都是围绕**一件事**,只有它围绕**两件事**——联系和创造是从那儿来的。
|
|
7
|
+
提示词里给了出口:真的没有就说没有。
|
|
8
|
+
- **出错能看见了**:状态灯直接显示错误(比如「出错:HTTP 401」)。以前 key 失效了,
|
|
9
|
+
界面上还显示正常在跑。
|
|
10
|
+
|
|
11
|
+
## 0.3.0
|
|
12
|
+
## 0.3.0
|
|
13
|
+
|
|
14
|
+
五件事(界面 / 素材 / 延续 / 情绪 / 时间感),外加两个真 bug。
|
|
15
|
+
|
|
16
|
+
- **本地时间线页面**(`siteEnabled`,默认开,端口 7802):按时间翻她想过的每一条——模式用颜色分开、重要度用点、接着上次的会标出来;底部是她自己收成的「她对自己的看法」。只绑 127.0.0.1,World 页有「看她的时间线」链接。
|
|
17
|
+
- **素材扩到原文**:以前只吃卡片(结论),而库里大半是「谁让我做什么」,三五条之后就绕圈。现在一条原话 + 一张卡混着挑。原文筛 12–70 字、有标点、不是界面回执。
|
|
18
|
+
- **能接着上次想**:三成概率接着上一条往下说,提示词明确要求「说新的一层,别把上次那句重说一遍」。实测有效——「那戒指还在怀里揣着…」之后接的是「没完成也能带走,那是不是本来就不用交出去」。
|
|
19
|
+
- **心情进上下文**:从记忆核心的 `mood` 映射成「很好 / 还行 / 一般 / 有点低落 / 很难过」。
|
|
20
|
+
- **时间带时段**:「11-15 23:30(深夜)」。不带的话她会想出「下午出去走走」。
|
|
21
|
+
- **修:推理模型下 `max_tokens` 被 reasoning 吃干**。deepseek-flash 实测 reasoning 就占 298 token——反思给 120 上限时正文直接是空(报「模型返回了空内容」)。现在思考保底 600、反思 600,而且**空返回会自动加额重试一次**(`max_tokens` 只是上限,给大不额外花钱)。
|
|
22
|
+
- **修:反思失败也会清空 `pendingImportance`**,攒了几十条的重要度一次网络抖动就白扔。改成只有成功(或模型明确说「没有」)才清。
|
|
23
|
+
|
|
24
|
+
## 0.2.0
|
|
3
25
|
## 0.2.0
|
|
4
26
|
|
|
5
27
|
两件补上:**消息抢占**,以及和记忆核心真正接起来。
|
package/README.md
CHANGED
|
@@ -95,6 +95,18 @@
|
|
|
95
95
|
|
|
96
96
|
---
|
|
97
97
|
|
|
98
|
+
## 看她的时间线
|
|
99
|
+
|
|
100
|
+
插件会开一个只在本机的小网页(默认 `http://127.0.0.1:7802`,被占用会自动往后找)。
|
|
101
|
+
在 World 页点「看她的时间线」就能打开。
|
|
102
|
+
|
|
103
|
+
- 按时间倒序,每条标:时间、模式(回看 / 走神 / 打算 / 琢磨)、重要度点
|
|
104
|
+
- 接着上次想下去的会单独标出来
|
|
105
|
+
- 底部是「她对自己的看法」——几条碎念攒够重要度后自动收成的一句
|
|
106
|
+
- 顶部:今天几次、花了多少 token、下次什么时候、现在是跑着还是关着
|
|
107
|
+
|
|
108
|
+
只绑 `127.0.0.1`,不对外,也没有写操作。
|
|
109
|
+
|
|
98
110
|
## 想过的东西在哪
|
|
99
111
|
|
|
100
112
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cortico-world-self-awareness",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "桌宠自己的内在时间线:没人说话的时候她也在想事情——想起旧事、走神、反复琢磨一件没想明白的。\n\n它只负责「想」:想到的写在本机一个文件里,不发消息,也不占对话的 token。配合「记忆核心」使用——你开口时,记忆核心会挑一条带进上下文,标明时间,并说清那不一定是你现在的想法。单独装也能跑,但那是一本她自己不会翻的日记。\n\n模型自选,OpenAI 兼容接口就行;默认一天最多 12 次,一次几分钱。\n\nQQ 交流群:745879632",
|
|
6
6
|
"keywords": [
|
|
@@ -49,6 +49,7 @@
|
|
|
49
49
|
},
|
|
50
50
|
"files": [
|
|
51
51
|
"src",
|
|
52
|
+
"webui",
|
|
52
53
|
"describe.json",
|
|
53
54
|
"icon.svg",
|
|
54
55
|
"CHANGELOG.md",
|
package/src/config.ts
CHANGED
|
@@ -24,6 +24,9 @@ export interface SelfAwarenessConfig {
|
|
|
24
24
|
temperature: number;
|
|
25
25
|
/** 一句话交代她是谁,写进思考的上下文。留空就只按记忆来。 */
|
|
26
26
|
personaNote: string;
|
|
27
|
+
/** 本地时间线页面 */
|
|
28
|
+
siteEnabled: boolean;
|
|
29
|
+
sitePort: number;
|
|
27
30
|
/** 记忆核心的数据目录。留空自动找同级的 memory-core。 */
|
|
28
31
|
memoryCoreDir: string;
|
|
29
32
|
}
|
|
@@ -51,6 +54,8 @@ export const SELF_AWARENESS_DEFAULTS: SelfAwarenessConfig = {
|
|
|
51
54
|
maxOutputTokens: 220,
|
|
52
55
|
temperature: 0.95,
|
|
53
56
|
personaNote: '',
|
|
57
|
+
siteEnabled: true,
|
|
58
|
+
sitePort: 7802,
|
|
54
59
|
memoryCoreDir: '',
|
|
55
60
|
};
|
|
56
61
|
|
|
@@ -164,6 +169,20 @@ const RAW_GROUP: ConfigGroup = {
|
|
|
164
169
|
default: '',
|
|
165
170
|
'x-hot': true,
|
|
166
171
|
} as never,
|
|
172
|
+
'worlds.self-awareness.siteEnabled': {
|
|
173
|
+
type: 'boolean',
|
|
174
|
+
title: '本地时间线页面',
|
|
175
|
+
description: '开一个只在本机的小网页,能按时间翻她想过的每一条、她自己收成的自我认知,以及今天花了多少。只绑 127.0.0.1,不对外。',
|
|
176
|
+
default: true,
|
|
177
|
+
},
|
|
178
|
+
'worlds.self-awareness.sitePort': {
|
|
179
|
+
type: 'integer',
|
|
180
|
+
title: '页面端口',
|
|
181
|
+
description: '默认 7802。被占用会自动往后找。改完要重启 World。',
|
|
182
|
+
minimum: 1024,
|
|
183
|
+
maximum: 65_535,
|
|
184
|
+
default: 7802,
|
|
185
|
+
},
|
|
167
186
|
'worlds.self-awareness.memoryCoreDir': {
|
|
168
187
|
type: 'string',
|
|
169
188
|
title: '记忆核心目录',
|
|
@@ -190,6 +209,8 @@ const SETTINGS_ORDER = [
|
|
|
190
209
|
'worlds.self-awareness.maxOutputTokens',
|
|
191
210
|
'worlds.self-awareness.temperature',
|
|
192
211
|
'worlds.self-awareness.personaNote',
|
|
212
|
+
'worlds.self-awareness.siteEnabled',
|
|
213
|
+
'worlds.self-awareness.sitePort',
|
|
193
214
|
'worlds.self-awareness.memoryCoreDir',
|
|
194
215
|
];
|
|
195
216
|
|
package/src/pool.ts
CHANGED
|
@@ -1,218 +1,221 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 想法池:想法本身 + 「下次隔多久」这件事。
|
|
3
|
-
*
|
|
4
|
-
* 节奏是这里最要紧的一段。固定间隔就是闹钟;我们要的是:
|
|
5
|
-
* 这次想得多、想得深 → 歇久点;这次敷衍了一句 → 过一会儿再想。
|
|
6
|
-
* 间隔由内容算出来,不是钟表算出来。
|
|
7
|
-
*/
|
|
8
|
-
import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
9
|
-
import { join } from 'node:path';
|
|
10
|
-
import { randomUUID } from 'node:crypto';
|
|
11
|
-
|
|
12
|
-
export type ThoughtMode = 'reflect' | 'wonder' | 'plan' | 'doubt';
|
|
13
|
-
|
|
14
|
-
export const THOUGHT_MODES: ThoughtMode[] = ['reflect', 'wonder', 'plan', 'doubt'];
|
|
15
|
-
|
|
16
|
-
export interface Thought {
|
|
17
|
-
id: string;
|
|
18
|
-
ts: string;
|
|
19
|
-
mode: ThoughtMode;
|
|
20
|
-
text: string;
|
|
21
|
-
/** 1-10。高到一定程度会触发「反思」——往上抽象一层。 */
|
|
22
|
-
importance: number;
|
|
23
|
-
/** 这次是拿哪些卡当引子想出来的(卡片 id 前 8 位),用来防止反复嚼同一件事。 */
|
|
24
|
-
seeds: string[];
|
|
25
|
-
promptTokens?: number;
|
|
26
|
-
outputTokens?: number;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
const
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
state.
|
|
217
|
-
|
|
218
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 想法池:想法本身 + 「下次隔多久」这件事。
|
|
3
|
+
*
|
|
4
|
+
* 节奏是这里最要紧的一段。固定间隔就是闹钟;我们要的是:
|
|
5
|
+
* 这次想得多、想得深 → 歇久点;这次敷衍了一句 → 过一会儿再想。
|
|
6
|
+
* 间隔由内容算出来,不是钟表算出来。
|
|
7
|
+
*/
|
|
8
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
9
|
+
import { join } from 'node:path';
|
|
10
|
+
import { randomUUID } from 'node:crypto';
|
|
11
|
+
|
|
12
|
+
export type ThoughtMode = 'reflect' | 'wonder' | 'plan' | 'doubt' | 'synthesize';
|
|
13
|
+
|
|
14
|
+
export const THOUGHT_MODES: ThoughtMode[] = ['reflect', 'wonder', 'plan', 'doubt', 'synthesize'];
|
|
15
|
+
|
|
16
|
+
export interface Thought {
|
|
17
|
+
id: string;
|
|
18
|
+
ts: string;
|
|
19
|
+
mode: ThoughtMode;
|
|
20
|
+
text: string;
|
|
21
|
+
/** 1-10。高到一定程度会触发「反思」——往上抽象一层。 */
|
|
22
|
+
importance: number;
|
|
23
|
+
/** 这次是拿哪些卡当引子想出来的(卡片 id 前 8 位),用来防止反复嚼同一件事。 */
|
|
24
|
+
seeds: string[];
|
|
25
|
+
promptTokens?: number;
|
|
26
|
+
outputTokens?: number;
|
|
27
|
+
/** 这条是接着上一条想下去的,不是新起一件 */
|
|
28
|
+
carry?: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface Reflection {
|
|
32
|
+
id: string;
|
|
33
|
+
ts: string;
|
|
34
|
+
text: string;
|
|
35
|
+
fromCount: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function ensureDir(dir: string): void {
|
|
39
|
+
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function readJsonl<T>(file: string): T[] {
|
|
43
|
+
if (!existsSync(file)) return [];
|
|
44
|
+
let raw = '';
|
|
45
|
+
try {
|
|
46
|
+
raw = readFileSync(file, 'utf8');
|
|
47
|
+
} catch {
|
|
48
|
+
return [];
|
|
49
|
+
}
|
|
50
|
+
const out: T[] = [];
|
|
51
|
+
for (const line of raw.split('\n')) {
|
|
52
|
+
const s = line.trim();
|
|
53
|
+
if (!s) continue;
|
|
54
|
+
try {
|
|
55
|
+
out.push(JSON.parse(s) as T);
|
|
56
|
+
} catch {
|
|
57
|
+
// 坏行跳过:手改过文件也别让整个池子读不出来
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return out;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export class ThoughtPool {
|
|
64
|
+
private readonly dir: string;
|
|
65
|
+
private cache: Thought[] | null = null;
|
|
66
|
+
|
|
67
|
+
constructor(dir: string) {
|
|
68
|
+
this.dir = dir;
|
|
69
|
+
ensureDir(dir);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
get file(): string {
|
|
73
|
+
return join(this.dir, 'thoughts.jsonl');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
get reflectionFile(): string {
|
|
77
|
+
return join(this.dir, 'reflections.jsonl');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
all(): Thought[] {
|
|
81
|
+
if (!this.cache) this.cache = readJsonl<Thought>(this.file);
|
|
82
|
+
return this.cache;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
recent(n: number): Thought[] {
|
|
86
|
+
return this.all().slice(-Math.max(0, n));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
add(t: Omit<Thought, 'id' | 'ts'> & { ts?: string }): Thought {
|
|
90
|
+
const thought: Thought = {
|
|
91
|
+
id: randomUUID().slice(0, 8),
|
|
92
|
+
ts: t.ts ?? new Date().toISOString(),
|
|
93
|
+
mode: t.mode,
|
|
94
|
+
text: t.text,
|
|
95
|
+
importance: clampImportance(t.importance),
|
|
96
|
+
seeds: t.seeds ?? [],
|
|
97
|
+
...(t.promptTokens ? { promptTokens: t.promptTokens } : {}),
|
|
98
|
+
...(t.outputTokens ? { outputTokens: t.outputTokens } : {}),
|
|
99
|
+
...(t.carry ? { carry: true } : {}),
|
|
100
|
+
};
|
|
101
|
+
ensureDir(this.dir);
|
|
102
|
+
// 先让 cache 落地再 append:否则 all() 会从文件里读回刚写的那条,push 之后就有了两份
|
|
103
|
+
const list = this.all();
|
|
104
|
+
appendFileSync(this.file, `${JSON.stringify(thought)}\n`, 'utf8');
|
|
105
|
+
list.push(thought);
|
|
106
|
+
return thought;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
reflections(): Reflection[] {
|
|
110
|
+
return readJsonl<Reflection>(this.reflectionFile);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
addReflection(text: string, fromCount: number): Reflection {
|
|
114
|
+
const r: Reflection = {
|
|
115
|
+
id: randomUUID().slice(0, 8),
|
|
116
|
+
ts: new Date().toISOString(),
|
|
117
|
+
text,
|
|
118
|
+
fromCount,
|
|
119
|
+
};
|
|
120
|
+
ensureDir(this.dir);
|
|
121
|
+
appendFileSync(this.reflectionFile, `${JSON.stringify(r)}\n`, 'utf8');
|
|
122
|
+
return r;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** 最近这些 seed 用过没有(防止两周内反复拿同一张卡当引子)。 */
|
|
126
|
+
seedsUsedSince(cutoffMs: number): Set<string> {
|
|
127
|
+
const used = new Set<string>();
|
|
128
|
+
for (const t of this.all()) {
|
|
129
|
+
if ((Date.parse(t.ts) || 0) < cutoffMs) continue;
|
|
130
|
+
for (const s of t.seeds) used.add(s);
|
|
131
|
+
}
|
|
132
|
+
return used;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function clampImportance(n: unknown): number {
|
|
137
|
+
const v = Number(n);
|
|
138
|
+
if (!Number.isFinite(v)) return 4;
|
|
139
|
+
return Math.max(1, Math.min(10, Math.round(v)));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* 这次想法有多「密」:0 是敷衍一句,1 是想了很深。
|
|
144
|
+
* 三个因素:长度够不够、重要度、有没有真的抓住某个具体的东西。
|
|
145
|
+
*/
|
|
146
|
+
export function density(t: Pick<Thought, 'text' | 'importance' | 'seeds'>): number {
|
|
147
|
+
const len = String(t.text ?? '').trim().length;
|
|
148
|
+
// 40 字以下算稀薄,180 字以上算满
|
|
149
|
+
const lenScore = Math.min(1, Math.max(0, (len - 20) / 160));
|
|
150
|
+
const impScore = (clampImportance(t.importance) - 1) / 9;
|
|
151
|
+
const seedScore = t.seeds.length ? Math.min(1, t.seeds.length / 2) : 0;
|
|
152
|
+
return clamp01(lenScore * 0.45 + impScore * 0.4 + seedScore * 0.15);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* 下次隔多久。密度高往 max 靠,密度低往 min 靠,再加 ±12% 的抖动。
|
|
157
|
+
* 抖动不是为了「随机」,是为了不让间隔看起来像一个公式。
|
|
158
|
+
*/
|
|
159
|
+
export function nextGapMinutes(
|
|
160
|
+
densityValue: number,
|
|
161
|
+
minMin: number,
|
|
162
|
+
maxMin: number,
|
|
163
|
+
rand: () => number = Math.random,
|
|
164
|
+
): number {
|
|
165
|
+
const lo = Math.max(1, Math.min(minMin, maxMin));
|
|
166
|
+
const hi = Math.max(lo + 1, Math.max(minMin, maxMin));
|
|
167
|
+
const d = clamp01(densityValue);
|
|
168
|
+
const base = lo + (hi - lo) * d;
|
|
169
|
+
const jitter = base * (rand() * 0.24 - 0.12);
|
|
170
|
+
return Math.max(lo * 0.8, Math.min(hi * 1.2, base + jitter));
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function clamp01(n: number): number {
|
|
174
|
+
return Math.max(0, Math.min(1, Number.isFinite(n) ? n : 0));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** 累计重要度到这条线就该往上抽象一层了(写一条 reflection)。 */
|
|
178
|
+
export const REFLECT_THRESHOLD = 24;
|
|
179
|
+
|
|
180
|
+
export interface BudgetState {
|
|
181
|
+
day: string;
|
|
182
|
+
count: number;
|
|
183
|
+
tokens: number;
|
|
184
|
+
pendingImportance: number;
|
|
185
|
+
lastThoughtAt?: string;
|
|
186
|
+
nextDueAt?: string;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const BUDGET_FILE = 'state.json';
|
|
190
|
+
|
|
191
|
+
export function loadBudget(dir: string): BudgetState {
|
|
192
|
+
const file = join(dir, BUDGET_FILE);
|
|
193
|
+
if (!existsSync(file)) return { day: '', count: 0, tokens: 0, pendingImportance: 0 };
|
|
194
|
+
try {
|
|
195
|
+
const raw = JSON.parse(readFileSync(file, 'utf8')) as Partial<BudgetState>;
|
|
196
|
+
return {
|
|
197
|
+
day: String(raw.day ?? ''),
|
|
198
|
+
count: Number(raw.count) || 0,
|
|
199
|
+
tokens: Number(raw.tokens) || 0,
|
|
200
|
+
pendingImportance: Number(raw.pendingImportance) || 0,
|
|
201
|
+
...(raw.lastThoughtAt ? { lastThoughtAt: String(raw.lastThoughtAt) } : {}),
|
|
202
|
+
...(raw.nextDueAt ? { nextDueAt: String(raw.nextDueAt) } : {}),
|
|
203
|
+
};
|
|
204
|
+
} catch {
|
|
205
|
+
return { day: '', count: 0, tokens: 0, pendingImportance: 0 };
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export function saveBudget(dir: string, state: BudgetState): void {
|
|
210
|
+
ensureDir(dir);
|
|
211
|
+
writeFileSync(join(dir, BUDGET_FILE), `${JSON.stringify(state, null, 2)}\n`, 'utf8');
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** 跨天清零。返回是不是新的一天。 */
|
|
215
|
+
export function rollDay(state: BudgetState, today: string): boolean {
|
|
216
|
+
if (state.day === today) return false;
|
|
217
|
+
state.day = today;
|
|
218
|
+
state.count = 0;
|
|
219
|
+
state.tokens = 0;
|
|
220
|
+
return true;
|
|
221
|
+
}
|