cortico-world-screen-diary 0.1.0 → 0.2.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 +7 -0
- package/README.md +39 -20
- package/describe.json +49 -12
- package/dist/webui.js +1 -1
- package/dist/webui.js.map +3 -3
- package/package.json +2 -2
- package/src/ENV_PROMPT.en.md +6 -4
- package/src/ENV_PROMPT.md +7 -5
- package/src/book.ts +89 -24
- package/src/config.ts +88 -38
- package/src/definition.ts +5 -3
- package/src/model-text.ts +25 -15
- package/src/state.ts +118 -0
- package/src/webui/client.ts +118 -51
- package/src/world.ts +369 -250
package/src/book.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
/** Diary storage:
|
|
1
|
+
/** Diary storage: short-lived detail entries, long-lived smoothed state samples, and the read cursor. */
|
|
2
2
|
import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
4
|
import type { EventEnvelope } from 'cortico/core/types.ts';
|
|
5
|
+
import type { Category } from './state.ts';
|
|
5
6
|
|
|
6
7
|
export interface DiaryEntry {
|
|
7
8
|
cursor: number;
|
|
@@ -11,9 +12,23 @@ export interface DiaryEntry {
|
|
|
11
12
|
app?: string;
|
|
12
13
|
title?: string;
|
|
13
14
|
caption?: string;
|
|
15
|
+
category?: Category;
|
|
16
|
+
tech?: number;
|
|
17
|
+
work?: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** One smoothed point of the two-axis state; details fade, these stay longer. */
|
|
21
|
+
export interface StateSample {
|
|
22
|
+
ts: string;
|
|
23
|
+
tech: number;
|
|
24
|
+
work: number;
|
|
25
|
+
category: Category;
|
|
26
|
+
zh: string;
|
|
27
|
+
en: string;
|
|
14
28
|
}
|
|
15
29
|
|
|
16
30
|
const ENTRIES = 'entries.jsonl';
|
|
31
|
+
const SAMPLES = 'samples.jsonl';
|
|
17
32
|
const CURSOR = 'cursor.json';
|
|
18
33
|
|
|
19
34
|
/** Local day (`YYYY-MM-DD`) and hour (`00`–`23`) of an ISO timestamp in the deployment timezone. */
|
|
@@ -43,17 +58,22 @@ export function entryOf(event: EventEnvelope): DiaryEntry {
|
|
|
43
58
|
};
|
|
44
59
|
}
|
|
45
60
|
|
|
46
|
-
/** Entries
|
|
61
|
+
/** Entries are pruned after `detailHours`; smoothed samples survive `keepDays`. */
|
|
47
62
|
export class DiaryBook {
|
|
48
63
|
private entries: DiaryEntry[] = [];
|
|
64
|
+
private stateSamples: StateSample[] = [];
|
|
49
65
|
private lastCursor = 0;
|
|
50
66
|
|
|
51
|
-
constructor(
|
|
67
|
+
constructor(
|
|
68
|
+
private readonly dir: string,
|
|
69
|
+
private readonly timezone: string,
|
|
70
|
+
private readonly keepDays: number,
|
|
71
|
+
private readonly detailHours = 72,
|
|
72
|
+
) {
|
|
52
73
|
mkdirSync(dir, { recursive: true });
|
|
53
74
|
this.load();
|
|
54
75
|
}
|
|
55
76
|
|
|
56
|
-
/** The last event cursor already imported. */
|
|
57
77
|
get cursor(): number {
|
|
58
78
|
return this.lastCursor;
|
|
59
79
|
}
|
|
@@ -62,35 +82,60 @@ export class DiaryBook {
|
|
|
62
82
|
return this.entries.length;
|
|
63
83
|
}
|
|
64
84
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
85
|
+
get sampleCount(): number {
|
|
86
|
+
return this.stateSamples.length;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Import events and advance the cursor even when `transform` drops an event (privacy filter).
|
|
91
|
+
* Events older than `detailHours` only advance the cursor.
|
|
92
|
+
*/
|
|
93
|
+
ingest(events: readonly EventEnvelope[], transform?: (entry: DiaryEntry) => DiaryEntry | null): number {
|
|
94
|
+
const cutoff = Date.now() - Math.max(1, this.detailHours) * 3_600_000;
|
|
68
95
|
let added = 0;
|
|
69
96
|
for (const event of events) {
|
|
70
97
|
this.lastCursor = Math.max(this.lastCursor, event.cursor);
|
|
71
98
|
if (Date.parse(event.ts) < cutoff) continue;
|
|
72
|
-
const entry = entryOf(event);
|
|
99
|
+
const entry = transform ? transform(entryOf(event)) : entryOf(event);
|
|
100
|
+
if (!entry) continue;
|
|
73
101
|
this.entries.push(entry);
|
|
74
102
|
appendFileSync(join(this.dir, ENTRIES), `${JSON.stringify(entry)}\n`, 'utf8');
|
|
75
103
|
added += 1;
|
|
76
104
|
}
|
|
77
105
|
writeFileSync(join(this.dir, CURSOR), JSON.stringify({ cursor: this.lastCursor }), 'utf8');
|
|
78
|
-
if (added) this.
|
|
106
|
+
if (added) this.pruneEntries(cutoff);
|
|
79
107
|
return added;
|
|
80
108
|
}
|
|
81
109
|
|
|
82
|
-
/**
|
|
110
|
+
/** Persist one smoothed state point; the World decides how often to sample. */
|
|
111
|
+
addSample(sample: StateSample): void {
|
|
112
|
+
this.stateSamples.push(sample);
|
|
113
|
+
appendFileSync(join(this.dir, SAMPLES), `${JSON.stringify(sample)}\n`, 'utf8');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Newest samples first. */
|
|
117
|
+
samples(limit = 200): StateSample[] {
|
|
118
|
+
return this.stateSamples.slice(-Math.max(1, limit)).reverse();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Samples of one local day, oldest first; today when `day` is omitted. */
|
|
122
|
+
samplesOfDay(day?: string): StateSample[] {
|
|
123
|
+
const key = day ?? localParts(new Date().toISOString(), this.timezone).day;
|
|
124
|
+
return this.stateSamples.filter((sample) => localParts(sample.ts, this.timezone).day === key);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Entries of one local day, oldest first. */
|
|
83
128
|
day(day?: string): DiaryEntry[] {
|
|
84
129
|
const key = day ?? localParts(new Date().toISOString(), this.timezone).day;
|
|
85
130
|
return this.entries.filter((entry) => localParts(entry.ts, this.timezone).day === key);
|
|
86
131
|
}
|
|
87
132
|
|
|
88
|
-
/** Newest entries first. */
|
|
133
|
+
/** Newest detail entries first (explicit recall only). */
|
|
89
134
|
recent(limit = 100): DiaryEntry[] {
|
|
90
135
|
return this.entries.slice(-Math.max(1, limit)).reverse();
|
|
91
136
|
}
|
|
92
137
|
|
|
93
|
-
/**
|
|
138
|
+
/** Explicit text search over short-lived detail entries; newest matches first. */
|
|
94
139
|
search(keyword: string, limit = 20): DiaryEntry[] {
|
|
95
140
|
const needle = keyword.trim().toLowerCase();
|
|
96
141
|
if (!needle) return [];
|
|
@@ -103,25 +148,28 @@ export class DiaryBook {
|
|
|
103
148
|
return hits;
|
|
104
149
|
}
|
|
105
150
|
|
|
106
|
-
stats(now = Date.now()): { total: number; days: number; topApps: Array<{ app: string; count: number }> } {
|
|
151
|
+
stats(now = Date.now()): { total: number; days: number; samples: number; topApps: Array<{ app: string; count: number }> } {
|
|
107
152
|
const daySet = new Set<string>();
|
|
108
153
|
const appCounts = new Map<string, number>();
|
|
109
154
|
for (const entry of this.entries) {
|
|
110
155
|
daySet.add(localParts(entry.ts, this.timezone).day);
|
|
111
156
|
if (entry.app) appCounts.set(entry.app, (appCounts.get(entry.app) ?? 0) + 1);
|
|
112
157
|
}
|
|
158
|
+
for (const sample of this.stateSamples) daySet.add(localParts(sample.ts, this.timezone).day);
|
|
113
159
|
const topApps = [...appCounts.entries()]
|
|
114
160
|
.sort((a, b) => b[1] - a[1])
|
|
115
161
|
.slice(0, 8)
|
|
116
162
|
.map(([app, count]) => ({ app, count }));
|
|
117
|
-
return { total: this.entries.length, days: daySet.size, topApps };
|
|
163
|
+
return { total: this.entries.length, days: daySet.size, samples: this.stateSamples.length, topApps };
|
|
118
164
|
}
|
|
119
165
|
|
|
120
|
-
/** Drop
|
|
166
|
+
/** Drop detail entries and samples, keeping the read cursor. */
|
|
121
167
|
clear(): number {
|
|
122
|
-
const removed = this.entries.length;
|
|
168
|
+
const removed = this.entries.length + this.stateSamples.length;
|
|
123
169
|
this.entries = [];
|
|
170
|
+
this.stateSamples = [];
|
|
124
171
|
writeFileSync(join(this.dir, ENTRIES), '', 'utf8');
|
|
172
|
+
writeFileSync(join(this.dir, SAMPLES), '', 'utf8');
|
|
125
173
|
return removed;
|
|
126
174
|
}
|
|
127
175
|
|
|
@@ -133,23 +181,40 @@ export class DiaryBook {
|
|
|
133
181
|
if (typeof state.cursor === 'number') this.lastCursor = state.cursor;
|
|
134
182
|
} catch { /* a broken cursor file starts from zero */ }
|
|
135
183
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
184
|
+
this.loadLines(ENTRIES, (value) => this.entries.push(value as DiaryEntry), this.detailCutoff());
|
|
185
|
+
this.loadLines(SAMPLES, (value) => this.stateSamples.push(value as StateSample), Date.now() - Math.max(1, this.keepDays) * 86_400_000);
|
|
186
|
+
this.pruneEntries(this.detailCutoff());
|
|
187
|
+
this.pruneSamples();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
private loadLines(file: string, push: (value: unknown) => void, cutoff: number): void {
|
|
191
|
+
const path = join(this.dir, file);
|
|
192
|
+
if (!existsSync(path)) return;
|
|
193
|
+
for (const line of readFileSync(path, 'utf8').split('\n')) {
|
|
140
194
|
if (!line.trim()) continue;
|
|
141
195
|
try {
|
|
142
|
-
const
|
|
143
|
-
if (typeof
|
|
196
|
+
const value = JSON.parse(line) as { ts?: unknown };
|
|
197
|
+
if (typeof value.ts === 'string' && Date.parse(value.ts) >= cutoff) push(value);
|
|
144
198
|
} catch { /* skip a torn line */ }
|
|
145
199
|
}
|
|
146
|
-
this.prune(cutoff);
|
|
147
200
|
}
|
|
148
201
|
|
|
149
|
-
private
|
|
202
|
+
private detailCutoff(): number {
|
|
203
|
+
return Date.now() - Math.max(1, this.detailHours) * 3_600_000;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
private pruneEntries(cutoff: number): void {
|
|
150
207
|
const kept = this.entries.filter((entry) => Date.parse(entry.ts) >= cutoff);
|
|
151
208
|
if (kept.length === this.entries.length) return;
|
|
152
209
|
this.entries = kept;
|
|
153
210
|
writeFileSync(join(this.dir, ENTRIES), kept.map((entry) => `${JSON.stringify(entry)}\n`).join(''), 'utf8');
|
|
154
211
|
}
|
|
212
|
+
|
|
213
|
+
private pruneSamples(): void {
|
|
214
|
+
const cutoff = Date.now() - Math.max(1, this.keepDays) * 86_400_000;
|
|
215
|
+
const kept = this.stateSamples.filter((sample) => Date.parse(sample.ts) >= cutoff);
|
|
216
|
+
if (kept.length === this.stateSamples.length) return;
|
|
217
|
+
this.stateSamples = kept;
|
|
218
|
+
writeFileSync(join(this.dir, SAMPLES), kept.map((sample) => `${JSON.stringify(sample)}\n`).join(''), 'utf8');
|
|
219
|
+
}
|
|
155
220
|
}
|
package/src/config.ts
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
1
|
-
/** Config section `worlds.screen-diary`:
|
|
1
|
+
/** Config section `worlds.screen-diary`: event stream, privacy filter, smoothing and retention. */
|
|
2
2
|
import type { ConfigGroup } from 'cortico/core/config-schema.ts';
|
|
3
3
|
import type { Language } from 'cortico/core/language.ts';
|
|
4
4
|
|
|
5
5
|
export interface DiaryConfig {
|
|
6
|
-
/** Read the producer's events; the World itself is enabled from the World overview. */
|
|
7
6
|
enabled: boolean;
|
|
8
|
-
/** How often the event store is checked for new events. */
|
|
9
7
|
pollMs: number;
|
|
10
|
-
/** On the first run, import this many minutes of existing events. */
|
|
11
8
|
lookbackMinutes: number;
|
|
12
|
-
/** Entries older than this many days are dropped. */
|
|
13
9
|
keepDays: number;
|
|
14
|
-
/** The event source id to read; the producer is `screen-sense` by default. */
|
|
15
10
|
source: string;
|
|
16
|
-
/** Most events imported per poll; the next poll continues from the last cursor. */
|
|
17
11
|
maxEventsPerPoll: number;
|
|
12
|
+
/** Blur AI chat apps to neutral instead of projecting them as development work. */
|
|
13
|
+
blurAi: boolean;
|
|
14
|
+
/** Extra app-name substrings to filter out, comma separated. */
|
|
15
|
+
ignoreApps: string;
|
|
16
|
+
/** EMA smoothing factor: higher reacts faster, lower is smoother. */
|
|
17
|
+
smoothing: number;
|
|
18
|
+
/** Detail entries (window titles, captions, text) are dropped after this many hours. */
|
|
19
|
+
detailHours: number;
|
|
20
|
+
/** Minimum gap between two stored state samples, seconds. */
|
|
21
|
+
sampleSec: number;
|
|
18
22
|
}
|
|
19
23
|
|
|
20
24
|
export const DIARY_DEFAULTS: DiaryConfig = {
|
|
@@ -24,54 +28,78 @@ export const DIARY_DEFAULTS: DiaryConfig = {
|
|
|
24
28
|
keepDays: 30,
|
|
25
29
|
source: 'screen-sense',
|
|
26
30
|
maxEventsPerPoll: 500,
|
|
31
|
+
blurAi: true,
|
|
32
|
+
ignoreApps: '',
|
|
33
|
+
smoothing: 0.25,
|
|
34
|
+
detailHours: 72,
|
|
35
|
+
sampleSec: 30,
|
|
27
36
|
};
|
|
28
37
|
|
|
29
38
|
export type DiaryConfigSection = DiaryConfig;
|
|
30
39
|
|
|
31
|
-
/** Text for the console config group; `zh` and `en` are complete. */
|
|
32
40
|
function diaryText(language: Language) {
|
|
33
41
|
const zh = language === 'zh' || language === 'zh-Hant';
|
|
34
42
|
return zh ? {
|
|
35
|
-
group: '
|
|
36
|
-
groupDesc: '
|
|
43
|
+
group: '事件流与记忆',
|
|
44
|
+
groupDesc: '从哪里读事件、怎么过滤与平滑、细节保留多久。',
|
|
37
45
|
enabledTitle: '读取事件流',
|
|
38
46
|
enabledDesc: '打开后按固定间隔增量读取生产方的事件。',
|
|
39
47
|
pollTitle: '轮询间隔',
|
|
40
48
|
pollDesc: '每隔这么久检查一次新事件。',
|
|
41
49
|
lookbackTitle: '首次回填',
|
|
42
50
|
lookbackDesc: '第一次启动时导入最近多少分钟已有的历史事件。',
|
|
43
|
-
keepTitle: '
|
|
44
|
-
keepDesc: '
|
|
51
|
+
keepTitle: '状态保留天数',
|
|
52
|
+
keepDesc: '平滑状态与日摘要保留多久;超过后删除。',
|
|
45
53
|
sourceTitle: '事件来源',
|
|
46
54
|
sourceDesc: '生产方的 World id,默认 screen-sense(窥屏助手)。',
|
|
47
55
|
maxTitle: '单次处理上限',
|
|
48
56
|
maxDesc: '一次最多导入多少条;超出的留给下一拍继续。',
|
|
57
|
+
blurAiTitle: '模糊 AI 对话',
|
|
58
|
+
blurAiDesc: 'AI 聊天窗口默认按中性处理,不记录具体窗口标题。',
|
|
59
|
+
ignoreTitle: '额外过滤',
|
|
60
|
+
ignoreAppsDesc: '逗号分隔的应用名片段;通讯类应用默认已过滤。',
|
|
61
|
+
smoothingTitle: '平滑强度',
|
|
62
|
+
smoothingDesc: '0.05 很平滑,0.9 反应很快;默认 0.25。',
|
|
63
|
+
detailTitle: '细节保留小时',
|
|
64
|
+
detailDesc: '窗口标题、速写与正文只保留这么久,之后只留下平滑状态。',
|
|
65
|
+
sampleTitle: '采样间隔',
|
|
66
|
+
sampleDesc: '两次状态采样至少隔这么多秒。',
|
|
49
67
|
storage: {
|
|
50
68
|
label: '活动日记',
|
|
51
|
-
note: '
|
|
52
|
-
stat: (
|
|
69
|
+
note: '删除全部细节记录与平滑状态;不会影响生产方的相册与配置。',
|
|
70
|
+
stat: (entries: string, samples: string) => `${entries} 条细节 · ${samples} 个状态采样`,
|
|
53
71
|
cleared: (n: number) => `已删除 ${n} 条记录`,
|
|
54
72
|
},
|
|
55
73
|
} : {
|
|
56
|
-
group: 'Event stream',
|
|
57
|
-
groupDesc: 'Where events come from, how
|
|
74
|
+
group: 'Event stream and memory',
|
|
75
|
+
groupDesc: 'Where events come from, how they are filtered and smoothed, and how long details last.',
|
|
58
76
|
enabledTitle: 'Read the event stream',
|
|
59
77
|
enabledDesc: 'When on, new events are imported at a fixed interval.',
|
|
60
78
|
pollTitle: 'Poll interval',
|
|
61
79
|
pollDesc: 'How often to check for new events.',
|
|
62
80
|
lookbackTitle: 'First import',
|
|
63
81
|
lookbackDesc: 'On the first run, import up to this many minutes of existing events.',
|
|
64
|
-
keepTitle: '
|
|
65
|
-
keepDesc: '
|
|
82
|
+
keepTitle: 'State kept, days',
|
|
83
|
+
keepDesc: 'Smoothed state and daily summaries are kept this many days.',
|
|
66
84
|
sourceTitle: 'Event source',
|
|
67
85
|
sourceDesc: 'The producer World id; `screen-sense` by default.',
|
|
68
86
|
maxTitle: 'Events per poll',
|
|
69
87
|
maxDesc: 'Most events imported at once; the rest wait for the next poll.',
|
|
88
|
+
blurAiTitle: 'Blur AI chats',
|
|
89
|
+
blurAiDesc: 'AI chat windows count as neutral and their titles are not stored.',
|
|
90
|
+
ignoreTitle: 'Extra filters',
|
|
91
|
+
ignoreAppsDesc: 'Comma separated app-name fragments; communication apps are filtered by default.',
|
|
92
|
+
smoothingTitle: 'Smoothing',
|
|
93
|
+
smoothingDesc: '0.05 is very smooth, 0.9 reacts fast; 0.25 by default.',
|
|
94
|
+
detailTitle: 'Detail hours',
|
|
95
|
+
detailDesc: 'Window titles, captions and text are kept this long, then only smoothed state remains.',
|
|
96
|
+
sampleTitle: 'Sample interval',
|
|
97
|
+
sampleDesc: 'At least this many seconds between two stored state samples.',
|
|
70
98
|
storage: {
|
|
71
99
|
label: 'Activity diary',
|
|
72
|
-
note: 'Deletes
|
|
73
|
-
stat: (
|
|
74
|
-
cleared: (n: number) => `Deleted ${n}
|
|
100
|
+
note: 'Deletes detail entries and smoothed state; the producer album and settings are untouched.',
|
|
101
|
+
stat: (entries: string, samples: string) => `${entries} entries · ${samples} state samples`,
|
|
102
|
+
cleared: (n: number) => `Deleted ${n} records`,
|
|
75
103
|
},
|
|
76
104
|
};
|
|
77
105
|
}
|
|
@@ -80,7 +108,6 @@ export function diaryStorageText(language: Language) {
|
|
|
80
108
|
return diaryText(language).storage;
|
|
81
109
|
}
|
|
82
110
|
|
|
83
|
-
/** The timeline panel's text, in the browser's language. */
|
|
84
111
|
export interface DiaryPanelText {
|
|
85
112
|
title: string;
|
|
86
113
|
description: string;
|
|
@@ -92,50 +119,68 @@ export interface DiaryPanelText {
|
|
|
92
119
|
waitingProducer: (source: string) => string;
|
|
93
120
|
loading: string;
|
|
94
121
|
failed: string;
|
|
95
|
-
|
|
122
|
+
stateTitle: string;
|
|
123
|
+
current: (label: string) => string;
|
|
124
|
+
trend: string;
|
|
125
|
+
noTrend: string;
|
|
126
|
+
details: string;
|
|
127
|
+
detailsNote: string;
|
|
96
128
|
search: string;
|
|
97
129
|
searchPlaceholder: string;
|
|
98
130
|
noResults: string;
|
|
99
|
-
|
|
100
|
-
|
|
131
|
+
privacyNote: string;
|
|
132
|
+
axisTech: string;
|
|
133
|
+
axisWork: string;
|
|
101
134
|
}
|
|
102
135
|
|
|
103
136
|
export function diaryPanelText(language: Language): DiaryPanelText {
|
|
104
137
|
const zh = language === 'zh' || language === 'zh-Hant';
|
|
105
138
|
return zh ? {
|
|
106
139
|
title: '活动日记',
|
|
107
|
-
description: '
|
|
140
|
+
description: '二维状态空间:技术重度与办公/娱乐的平滑轨迹;细节会随时间淡忘。',
|
|
108
141
|
refresh: '刷新',
|
|
109
142
|
clear: '清空日记',
|
|
110
|
-
clearConfirm: '
|
|
143
|
+
clearConfirm: '删除全部细节记录与平滑状态?不会影响窥屏助手的相册。',
|
|
111
144
|
cleared: (n) => `已删除 ${n} 条记录`,
|
|
112
|
-
empty: '
|
|
145
|
+
empty: '还没有记录。先安装并启用窥屏助手,切几次应用后这里会出现状态轨迹。',
|
|
113
146
|
waitingProducer: (source) => `还没有收到来自 ${source} 的事件;确认窥屏助手已安装并启用。`,
|
|
114
147
|
loading: '读取中…',
|
|
115
148
|
failed: '读取失败',
|
|
116
|
-
|
|
149
|
+
stateTitle: '当前状态',
|
|
150
|
+
current: (label) => `当前:${label}`,
|
|
151
|
+
trend: '最近轨迹',
|
|
152
|
+
noTrend: '还没有足够的采样点',
|
|
153
|
+
details: '详细记录(显式回忆)',
|
|
154
|
+
detailsNote: '只保留最近一段时间;通讯类应用不在此列。',
|
|
117
155
|
search: '搜索',
|
|
118
156
|
searchPlaceholder: '搜应用名、窗口标题或速写',
|
|
119
157
|
noResults: '没有匹配的记录',
|
|
120
|
-
|
|
121
|
-
|
|
158
|
+
privacyNote: '通讯类应用已过滤;AI 对话默认模糊为中性;窗口标题只短期保留。',
|
|
159
|
+
axisTech: '技术重度',
|
|
160
|
+
axisWork: '工作',
|
|
122
161
|
} : {
|
|
123
162
|
title: 'Activity diary',
|
|
124
|
-
description: 'A
|
|
163
|
+
description: 'A two-axis state space: technical weight and work/entertainment, smoothed over time; details fade.',
|
|
125
164
|
refresh: 'Refresh',
|
|
126
165
|
clear: 'Clear diary',
|
|
127
|
-
clearConfirm: 'Delete
|
|
128
|
-
cleared: (n) => `Deleted ${n}
|
|
166
|
+
clearConfirm: 'Delete detail entries and smoothed state? The screen-sense album is untouched.',
|
|
167
|
+
cleared: (n) => `Deleted ${n} records`,
|
|
129
168
|
empty: 'No entries yet. Install and enable screen-sense, switch apps a few times, then come back.',
|
|
130
169
|
waitingProducer: (source) => `No events from ${source} yet; check that screen-sense is installed and enabled.`,
|
|
131
170
|
loading: 'Loading…',
|
|
132
171
|
failed: 'Could not load',
|
|
133
|
-
|
|
172
|
+
stateTitle: 'Current state',
|
|
173
|
+
current: (label) => `Now: ${label}`,
|
|
174
|
+
trend: 'Recent trajectory',
|
|
175
|
+
noTrend: 'Not enough samples yet',
|
|
176
|
+
details: 'Detail entries (explicit recall)',
|
|
177
|
+
detailsNote: 'Kept briefly only; communication apps are excluded.',
|
|
134
178
|
search: 'Search',
|
|
135
179
|
searchPlaceholder: 'app, window title or caption',
|
|
136
180
|
noResults: 'No matching entries',
|
|
137
|
-
|
|
138
|
-
|
|
181
|
+
privacyNote: 'Communication apps are filtered; AI chats are blurred to neutral; titles are kept briefly.',
|
|
182
|
+
axisTech: 'Technical',
|
|
183
|
+
axisWork: 'Work',
|
|
139
184
|
};
|
|
140
185
|
}
|
|
141
186
|
|
|
@@ -153,7 +198,12 @@ export function diaryConfigGroup(language: Language = 'zh'): ConfigGroup {
|
|
|
153
198
|
[`${K}.enabled`]: { type: 'boolean', title: t.enabledTitle, description: t.enabledDesc, 'x-hot': true },
|
|
154
199
|
[`${K}.pollMs`]: { type: 'integer', title: t.pollTitle, description: t.pollDesc, minimum: 1000, maximum: 300000, 'x-suffix': 'ms', 'x-hot': true },
|
|
155
200
|
[`${K}.lookbackMinutes`]: { type: 'integer', title: t.lookbackTitle, description: t.lookbackDesc, minimum: 0, maximum: 1440, 'x-suffix': 'min' },
|
|
156
|
-
[`${K}.keepDays`]: { type: 'integer', title: t.keepTitle, description: t.keepDesc, minimum: 1, maximum: 3650, 'x-suffix': 'd'
|
|
201
|
+
[`${K}.keepDays`]: { type: 'integer', title: t.keepTitle, description: t.keepDesc, minimum: 1, maximum: 3650, 'x-suffix': 'd' },
|
|
202
|
+
[`${K}.detailHours`]: { type: 'integer', title: t.detailTitle, description: t.detailDesc, minimum: 1, maximum: 720, 'x-suffix': 'h', 'x-hot': true },
|
|
203
|
+
[`${K}.blurAi`]: { type: 'boolean', title: t.blurAiTitle, description: t.blurAiDesc, 'x-hot': true },
|
|
204
|
+
[`${K}.ignoreApps`]: { type: 'string', title: t.ignoreTitle, description: t.ignoreAppsDesc, 'x-hot': true },
|
|
205
|
+
[`${K}.smoothing`]: { type: 'number', title: t.smoothingTitle, description: t.smoothingDesc, minimum: 0.05, maximum: 0.9, 'x-hot': true },
|
|
206
|
+
[`${K}.sampleSec`]: { type: 'integer', title: t.sampleTitle, description: t.sampleDesc, minimum: 5, maximum: 600, 'x-suffix': 's' },
|
|
157
207
|
[`${K}.source`]: { type: 'string', title: t.sourceTitle, description: t.sourceDesc },
|
|
158
208
|
[`${K}.maxEventsPerPoll`]: { type: 'integer', title: t.maxTitle, description: t.maxDesc, minimum: 1, maximum: 5000 },
|
|
159
209
|
},
|
package/src/definition.ts
CHANGED
|
@@ -14,9 +14,11 @@ const DESCRIBE: WorldDescribe = {
|
|
|
14
14
|
tools: describeTools(
|
|
15
15
|
SCREEN_DIARY_TOOL_DECLS.map(({ name, description, parameters, tags }) => ({ name, description, parameters, tags })),
|
|
16
16
|
{
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
diary_state: { zh: '读当前平滑后的活动状态与最近采样。', en: 'Reads the current smoothed activity state and recent samples.' },
|
|
18
|
+
diary_recent: { zh: '读最近 10 个平滑状态采样,新的在前。', en: 'Reads the most recent smoothed state samples, newest first.' },
|
|
19
|
+
diary_today: { zh: '按半小时分桶读某天的平滑状态,默认今天。', en: 'Reads one day of smoothed state in half-hour bins, today by default.' },
|
|
20
|
+
diary_search: { zh: '显式回忆:在短期细节记录里搜索应用名、标题或速写。', en: 'Explicit recall over short-lived detail entries.' },
|
|
21
|
+
diary_stats: { zh: '报覆盖天数、状态采样数与主要状态。', en: 'Reports days, state samples and dominant states.' },
|
|
20
22
|
},
|
|
21
23
|
),
|
|
22
24
|
events: [],
|
package/src/model-text.ts
CHANGED
|
@@ -1,30 +1,40 @@
|
|
|
1
|
-
/** 这个 World
|
|
1
|
+
/** 这个 World 让模型读到的文本:状态、轨迹与显式回忆的回执,中英各一份。 */
|
|
2
2
|
import type { DiaryEntry } from './book.ts';
|
|
3
3
|
|
|
4
4
|
const zh = {
|
|
5
|
-
empty: '
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
empty: '活动日记还没有状态采样。',
|
|
6
|
+
state: (label: string) => `[diary] 当前状态:${label}`,
|
|
7
|
+
recentHead: (n: number) => `[diary] 最近 ${n} 个状态采样:`,
|
|
8
|
+
recentLine: (clock: string, label: string) => `${clock} ${label}`,
|
|
9
|
+
todayHead: (day: string) => `[diary] ${day} 的平滑状态:`,
|
|
10
|
+
binLine: (range: string, label: string) => `${range} ${label}`,
|
|
11
|
+
noSamples: '这一天还没有状态采样。',
|
|
12
|
+
searchHead: (keyword: string, n: number) => `[diary] 显式回忆:匹配「${keyword}」的最近 ${n} 条细节:`,
|
|
13
|
+
noMatch: (keyword: string) => `日记里没有匹配「${keyword}」的细节记录。`,
|
|
14
|
+
detailLine: (entry: DiaryEntry, clock: string) => {
|
|
8
15
|
const app = entry.app ? `${entry.app}${entry.title ? `(${entry.title})` : ''}:` : '';
|
|
9
16
|
return `${clock} ${app}${entry.caption ?? entry.text}`;
|
|
10
17
|
},
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
stats: (days: number, total: number, top: Array<{ app: string; count: number }>) =>
|
|
14
|
-
`[diary] ${days} 天共 ${total} 条` + (top.length ? `;最常出现:${top.map((item) => `${item.app} ${item.count} 次`).join('、')}` : '。'),
|
|
18
|
+
stats: (days: number, samples: number, top: Array<{ label: string; count: number }>) =>
|
|
19
|
+
`[diary] ${days} 天、${samples} 个状态采样` + (top.length ? `;主要状态:${top.map((item) => `${item.label} ${item.count}`).join('、')}` : '。'),
|
|
15
20
|
};
|
|
16
21
|
|
|
17
22
|
const en: typeof zh = {
|
|
18
|
-
empty: 'The activity diary has no
|
|
19
|
-
|
|
20
|
-
|
|
23
|
+
empty: 'The activity diary has no state samples yet.',
|
|
24
|
+
state: (label) => `[diary] Current state: ${label}`,
|
|
25
|
+
recentHead: (n) => `[diary] The ${n} most recent state samples:`,
|
|
26
|
+
recentLine: (clock, label) => `${clock} ${label}`,
|
|
27
|
+
todayHead: (day) => `[diary] Smoothed states for ${day}:`,
|
|
28
|
+
binLine: (range, label) => `${range} ${label}`,
|
|
29
|
+
noSamples: 'No state samples for that day yet.',
|
|
30
|
+
searchHead: (keyword, n) => `[diary] Explicit recall: the ${n} most recent detail matches for "${keyword}":`,
|
|
31
|
+
noMatch: (keyword) => `No detail entries match "${keyword}".`,
|
|
32
|
+
detailLine: (entry, clock) => {
|
|
21
33
|
const app = entry.app ? `${entry.app}${entry.title ? ` (${entry.title})` : ''}: ` : '';
|
|
22
34
|
return `${clock} ${app}${entry.caption ?? entry.text}`;
|
|
23
35
|
},
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
stats: (days, total, top) =>
|
|
27
|
-
`[diary] ${total} entries across ${days} days` + (top.length ? `; most seen: ${top.map((item) => `${item.app} (${item.count})`).join(', ')}` : '.'),
|
|
36
|
+
stats: (days, samples, top) =>
|
|
37
|
+
`[diary] ${days} days, ${samples} state samples` + (top.length ? `; main states: ${top.map((item) => `${item.label} ${item.count}`).join(', ')}` : '.'),
|
|
28
38
|
};
|
|
29
39
|
|
|
30
40
|
export type ModelLanguage = 'zh' | 'en';
|
package/src/state.ts
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Two-axis activity projection and smoothing, with privacy filtering.
|
|
3
|
+
*
|
|
4
|
+
* - vertical: technical weight (heavy / light)
|
|
5
|
+
* - horizontal: work (office) to entertainment
|
|
6
|
+
*
|
|
7
|
+
* Communication apps are filtered out before projection; AI chat apps are blurred to neutral by
|
|
8
|
+
* default. Nothing here keeps window titles: only a category and two numbers leave this module.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export type Category = 'develop' | 'game' | 'office' | 'video' | 'browser' | 'ai' | 'neutral';
|
|
12
|
+
|
|
13
|
+
export interface Projection {
|
|
14
|
+
category: Category;
|
|
15
|
+
tech: number;
|
|
16
|
+
work: number;
|
|
17
|
+
/** AI chat blurred to neutral; the caller should not store its text. */
|
|
18
|
+
blurred: boolean;
|
|
19
|
+
/** Communication apps: skip entirely. */
|
|
20
|
+
filtered: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface ClassifyOptions {
|
|
24
|
+
/** Blur AI chat apps to (0,0) instead of treating them as development work. */
|
|
25
|
+
blurAi: boolean;
|
|
26
|
+
/** Extra app-name substrings to filter out, comma separated. */
|
|
27
|
+
ignoreApps: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const PROJECTION_DEFAULTS: ClassifyOptions = { blurAi: true, ignoreApps: '' };
|
|
31
|
+
|
|
32
|
+
const COMMUNICATION = /wechat|weixin|\bqq\b|tim\.exe|discord|telegram|slack|teams|zoom|webex|meeting|outlook|thunderbird|foxmail|feishu|lark|dingtalk|wecom|whatsapp|signal|skype|line\b|messages|mail/i;
|
|
33
|
+
const AI_CHAT = /chatgpt|openai|claude|gemini|copilot|deepseek|kimi|doubao|yuanbao|chatbox|\bpoe\b|character\.ai|lm studio|ollama/i;
|
|
34
|
+
const DEVELOP = /code|vscode|cursor|idea|pycharm|webstorm|rider|goland|terminal|powershell|\bcmd\b|bash|wsl|git|docker|kubernetes|github|npm|pnpm|node|python|java|rust|unity|unreal|blender|postman|insomnia/i;
|
|
35
|
+
const GAME = /steam|game|league|valorant|minecraft|elden|genshin|honkai|starrail|epicgames|battle\.net|wegame|ubisoft|rockstar|riot/i;
|
|
36
|
+
const OFFICE = /word|excel|powerpoint|onenote|notion|obsidian|acrobat|\bpdf\b|wps|libreoffice|office|docs|excel/i;
|
|
37
|
+
const VIDEO = /bilibili|youtube|netflix|iqiyi|youku|qqvideo|tencentvideo|\bvlc\b|mpv|potplayer|disney|primevideo|twitch|spotify|music/i;
|
|
38
|
+
const BROWSER = /chrome|edge|firefox|safari|brave|opera|chromium/i;
|
|
39
|
+
const DEV_TITLE = /github|gitlab|\bnpm\b|pnpm|stackoverflow|developer|localhost|api\b|docs|documentation|code/i;
|
|
40
|
+
const VIDEO_TITLE = /bilibili|youtube|netflix|视频|直播|番剧|电影/i;
|
|
41
|
+
const OFFICE_TITLE = /google docs|office|excel|word|pdf|notion|文档/i;
|
|
42
|
+
const GAME_TITLE = /steam|游戏|game/i;
|
|
43
|
+
|
|
44
|
+
const pos = (category: Category, tech: number, work: number, blurred = false): Projection => ({ category, tech, work, blurred, filtered: false });
|
|
45
|
+
const NEUTRAL: Projection = pos('neutral', 0, 0);
|
|
46
|
+
|
|
47
|
+
/** Project one foreground window to the two axes. Titles are only used to infer a browser's topic. */
|
|
48
|
+
export function classify(app: string | null, title: string | null, options: ClassifyOptions = PROJECTION_DEFAULTS): Projection {
|
|
49
|
+
const a = app?.trim() ?? '';
|
|
50
|
+
const t = title?.trim() ?? '';
|
|
51
|
+
const extra = options.ignoreApps.split(',').map((item) => item.trim()).filter(Boolean);
|
|
52
|
+
if (!a) return NEUTRAL;
|
|
53
|
+
if (COMMUNICATION.test(a)) return { ...NEUTRAL, filtered: true };
|
|
54
|
+
if (extra.some((item) => a.toLowerCase().includes(item.toLowerCase()))) return { ...NEUTRAL, filtered: true };
|
|
55
|
+
if (AI_CHAT.test(a)) {
|
|
56
|
+
return options.blurAi ? { ...NEUTRAL, category: 'ai', blurred: true } : pos('develop', 0.5, 0.4);
|
|
57
|
+
}
|
|
58
|
+
if (DEVELOP.test(a)) return pos('develop', 0.8, 0.6);
|
|
59
|
+
if (GAME.test(a)) return pos('game', 0.6, -0.8);
|
|
60
|
+
if (OFFICE.test(a)) return pos('office', -0.2, 0.8);
|
|
61
|
+
if (VIDEO.test(a)) return pos('video', -0.5, -0.8);
|
|
62
|
+
if (BROWSER.test(a)) {
|
|
63
|
+
if (DEV_TITLE.test(t)) return pos('develop', 0.8, 0.6);
|
|
64
|
+
if (VIDEO_TITLE.test(t)) return pos('video', -0.5, -0.8);
|
|
65
|
+
if (OFFICE_TITLE.test(t)) return pos('office', -0.2, 0.8);
|
|
66
|
+
if (GAME_TITLE.test(t)) return pos('game', 0.6, -0.8);
|
|
67
|
+
return NEUTRAL;
|
|
68
|
+
}
|
|
69
|
+
return NEUTRAL;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The quadrant and its Chinese/English label for a smoothed point. */
|
|
73
|
+
export function encodeState(tech: number, work: number): { category: Category; zh: string; en: string } {
|
|
74
|
+
if (Math.abs(tech) < 0.2 && Math.abs(work) < 0.2) return { category: 'neutral', zh: '中性', en: 'neutral' };
|
|
75
|
+
if (work >= 0.2) {
|
|
76
|
+
return tech >= 0.2
|
|
77
|
+
? { category: 'develop', zh: '开发/工作', en: 'developing / work' }
|
|
78
|
+
: { category: 'office', zh: '文书/工作', en: 'documents / work' };
|
|
79
|
+
}
|
|
80
|
+
return tech >= 0.2
|
|
81
|
+
? { category: 'game', zh: '游戏/娱乐', en: 'playing / entertainment' }
|
|
82
|
+
: { category: 'video', zh: '视频/娱乐', en: 'watching / entertainment' };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Exponential moving average over projected points; one instance per World. */
|
|
86
|
+
export class StateFilter {
|
|
87
|
+
private tech = 0;
|
|
88
|
+
private work = 0;
|
|
89
|
+
private seen = false;
|
|
90
|
+
|
|
91
|
+
constructor(private readonly alpha = 0.25) {}
|
|
92
|
+
|
|
93
|
+
observe(point: { tech: number; work: number }): { tech: number; work: number } {
|
|
94
|
+
const alpha = Math.min(0.9, Math.max(0.05, this.alpha));
|
|
95
|
+
if (!this.seen) {
|
|
96
|
+
this.tech = point.tech;
|
|
97
|
+
this.work = point.work;
|
|
98
|
+
this.seen = true;
|
|
99
|
+
} else {
|
|
100
|
+
this.tech += alpha * (point.tech - this.tech);
|
|
101
|
+
this.work += alpha * (point.work - this.work);
|
|
102
|
+
}
|
|
103
|
+
return { tech: this.tech, work: this.work };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
get current(): { tech: number; work: number } {
|
|
107
|
+
return { tech: this.tech, work: this.work };
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Compact duration text, e.g. `2 小时 13 分`. */
|
|
112
|
+
export function formatSpan(ms: number, language: 'zh' | 'en' = 'zh'): string {
|
|
113
|
+
const minutes = Math.max(0, Math.round(ms / 60_000));
|
|
114
|
+
const hours = Math.floor(minutes / 60);
|
|
115
|
+
const rest = minutes % 60;
|
|
116
|
+
if (language === 'en') return hours ? `${hours}h ${rest}m` : `${rest}m`;
|
|
117
|
+
return hours ? `${hours} 小时 ${rest} 分` : `${rest} 分`;
|
|
118
|
+
}
|