dsh-note-journal 0.0.0-stage → 1.0.1
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/README.md +16 -2
- package/client.js +99 -0
- package/cordis.patch.yml +3 -0
- package/docs/DETAIL.md +44 -0
- package/icon.svg +16 -0
- package/index.js +573 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +42 -3
package/README.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Note journal
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Per-session note mode for DeepSeek Harness: record messages without a model
|
|
4
|
+
request, then get a weekly, monthly and yearly report written in a fresh
|
|
5
|
+
session.
|
|
6
|
+
|
|
7
|
+
- Client chip beside the composer (`conversation.input.left`) and the `/note`
|
|
8
|
+
command toggle the mode for one session.
|
|
9
|
+
- The mode is folded from the `/note` command lifecycle events, so it survives
|
|
10
|
+
restarts and forks without a private log format.
|
|
11
|
+
- Records are captured into `%DSH_HOME%\note-journal\notes.jsonl`; the session
|
|
12
|
+
log keeps the authoritative copy.
|
|
13
|
+
- Three durable schedules (Monday 09:00, 1st 09:00, Jan 1 09:00, host local
|
|
14
|
+
time) open a new session whose agent calls `journal_notes` and writes the
|
|
15
|
+
report.
|
|
16
|
+
|
|
17
|
+
See `docs/DETAIL.md` (Chinese) for usage, data locations and limitations.
|
package/client.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client half of the note journal bundle: one chip beside the composer that
|
|
3
|
+
* toggles note mode and reflects the host-computed `noteJournal` projection.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
window.__ModuleLoader__.load({
|
|
7
|
+
id: 'dsh-note-journal',
|
|
8
|
+
factory(require) {
|
|
9
|
+
const React = require('react')
|
|
10
|
+
|
|
11
|
+
const NS = 'noteJournal'
|
|
12
|
+
|
|
13
|
+
const zh = {
|
|
14
|
+
'off.label': '笔记',
|
|
15
|
+
'off.title': '开启笔记模式:之后的消息只记录,AI 不回复、不消耗 token',
|
|
16
|
+
'on.label': '记录中',
|
|
17
|
+
'on.title': '笔记模式已开启:消息只记录不回复。点击关闭',
|
|
18
|
+
'failed': '切换失败',
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const en = {
|
|
22
|
+
'off.label': 'Notes',
|
|
23
|
+
'off.title': 'Turn on note mode: messages are recorded only, the model never answers',
|
|
24
|
+
'on.label': 'Recording',
|
|
25
|
+
'on.title': 'Note mode is on: messages are recorded without a reply. Click to turn it off',
|
|
26
|
+
'failed': 'Toggle failed',
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const CSS = [
|
|
30
|
+
'.nj-chip{display:inline-flex;align-items:center;gap:6px;height:26px;padding:0 9px;',
|
|
31
|
+
'border-radius:13px;border:1px solid var(--dsw-alias-border-l1);background:transparent;',
|
|
32
|
+
'color:var(--dsw-alias-label-secondary);font-size:12px;line-height:1;cursor:pointer;',
|
|
33
|
+
'font-family:inherit;transition:background .12s ease,border-color .12s ease,color .12s ease}',
|
|
34
|
+
'.nj-chip:hover:not(:disabled){background:var(--dsw-alias-bg-layer-2)}',
|
|
35
|
+
'.nj-chip-on{border-color:var(--dsw-alias-brand-primary);color:var(--dsw-alias-brand-primary)}',
|
|
36
|
+
'.nj-chip-failed{border-color:var(--dsw-alias-state-error-primary);color:var(--dsw-alias-state-error-primary)}',
|
|
37
|
+
'.nj-chip:disabled{opacity:.6;cursor:default}',
|
|
38
|
+
'.nj-dot{width:6px;height:6px;border-radius:50%;background:var(--dsw-alias-state-idle-primary);flex:none}',
|
|
39
|
+
'.nj-chip-on .nj-dot{background:var(--dsw-alias-brand-primary)}',
|
|
40
|
+
'.nj-chip-failed .nj-dot{background:var(--dsw-alias-state-error-primary)}',
|
|
41
|
+
].join('')
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
inject: ['slots', 'locale', 'remote', 'remote.commands'],
|
|
45
|
+
apply(ctx) {
|
|
46
|
+
ctx.effect(() => ctx.locale.register(NS, { zh, en }), 'note-journal: dictionaries')
|
|
47
|
+
const bound = ctx.locale.bind(NS)
|
|
48
|
+
|
|
49
|
+
function NoteModeChip(props) {
|
|
50
|
+
const t = typeof props.t === 'function' ? props.t : bound
|
|
51
|
+
const sessionId = props.sessionId
|
|
52
|
+
const value = props.useProjection('noteJournal')
|
|
53
|
+
const active = value !== undefined && value !== null && value.active === true
|
|
54
|
+
const [busy, setBusy] = React.useState(false)
|
|
55
|
+
const [failed, setFailed] = React.useState(false)
|
|
56
|
+
|
|
57
|
+
const toggle = () => {
|
|
58
|
+
if (busy) return
|
|
59
|
+
setBusy(true)
|
|
60
|
+
setFailed(false)
|
|
61
|
+
const line = active ? '/note off' : '/note on'
|
|
62
|
+
ctx.remote.commands.execute(sessionId, line, [])
|
|
63
|
+
.then((result) => {
|
|
64
|
+
if (!result || result.ok !== true || result.value === undefined) setFailed(true)
|
|
65
|
+
}, () => setFailed(true))
|
|
66
|
+
.then(() => setBusy(false))
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return React.createElement(
|
|
70
|
+
React.Fragment,
|
|
71
|
+
null,
|
|
72
|
+
React.createElement('style', null, CSS),
|
|
73
|
+
React.createElement(
|
|
74
|
+
'button',
|
|
75
|
+
{
|
|
76
|
+
type: 'button',
|
|
77
|
+
className: 'nj-chip' + (active ? ' nj-chip-on' : '') + (failed ? ' nj-chip-failed' : ''),
|
|
78
|
+
'aria-pressed': active,
|
|
79
|
+
'aria-label': t(active ? 'on.title' : 'off.title'),
|
|
80
|
+
title: t(active ? 'on.title' : 'off.title'),
|
|
81
|
+
disabled: busy,
|
|
82
|
+
onClick: toggle,
|
|
83
|
+
},
|
|
84
|
+
React.createElement('span', { className: 'nj-dot', 'aria-hidden': true }),
|
|
85
|
+
React.createElement('span', { className: 'nj-label' }, failed ? t('failed') : t(active ? 'on.label' : 'off.label')),
|
|
86
|
+
),
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
ctx.slots.inject('conversation.input.left', () => ctx.slots.register({
|
|
91
|
+
name: 'conversation.input.left',
|
|
92
|
+
id: 'note-journal',
|
|
93
|
+
order: 20,
|
|
94
|
+
locale: NS,
|
|
95
|
+
}, NoteModeChip))
|
|
96
|
+
},
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
})
|
package/cordis.patch.yml
ADDED
package/docs/DETAIL.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# 笔记模式
|
|
2
|
+
|
|
3
|
+
把单个会话切换成「只记录、不回复」的模式,并定期把记录整理成周报、月报和年报。
|
|
4
|
+
|
|
5
|
+
## 用法
|
|
6
|
+
|
|
7
|
+
1. 打开任意会话,输入框左侧会出现 **笔记** 按钮(也可以在输入框发送 `/note`)。
|
|
8
|
+
2. 点一下按钮,按钮变成 **记录中**,当前会话进入笔记模式。
|
|
9
|
+
3. 之后在这个会话里发的每条消息都只写进会话记录:不会发起模型请求,不消耗 token,AI 也不会回复。
|
|
10
|
+
4. 再点一下按钮(或发送 `/note`)退出笔记模式,恢复正常对话。
|
|
11
|
+
|
|
12
|
+
按钮状态从会话日志推导(`/note` 命令的 `command/run` 与 `command/done` 事件),所以重启、恢复或 fork 之后依然正确。
|
|
13
|
+
|
|
14
|
+
## 报告
|
|
15
|
+
|
|
16
|
+
插件启动时会在计划任务里登记三个持久任务:
|
|
17
|
+
|
|
18
|
+
| 任务 | 触发时间 | 约定 |
|
|
19
|
+
| --- | --- | --- |
|
|
20
|
+
| 笔记周报 | 每周一 09:00 | 汇总上一个完整自然周 |
|
|
21
|
+
| 笔记月报 | 每月 1 日 09:00 | 汇总上一个完整自然月 |
|
|
22
|
+
| 笔记年报 | 每年 1 月 1 日 09:00 | 汇总上一个完整自然年 |
|
|
23
|
+
|
|
24
|
+
到点时,DSH 会新建一个会话,会话里的智能体调用 `journal_notes` 工具读取该时段的笔记,然后直接写出报告。你可以像普通会话一样追问。
|
|
25
|
+
|
|
26
|
+
新会话的工作目录取「最近活跃会话所在的目录」,因此会落在你当前的工作区里、在侧边栏可见。如果想固定到某个目录,在 `cordis.patch.yml` 的插件行里加 `config: { reportCwd: "D:\\你的目录" }`。
|
|
27
|
+
|
|
28
|
+
如果某个时段没有任何笔记,报告只有一行「本周/本月/本年没有笔记记录。」
|
|
29
|
+
|
|
30
|
+
任务在「计划任务」里可见,可以暂停、改时间或删除;插件每次启动只会补建缺少的三个任务,不会覆盖你的改动。
|
|
31
|
+
|
|
32
|
+
## 数据
|
|
33
|
+
|
|
34
|
+
- 笔记原文的派生缓存:`%DSH_HOME%\note-journal\notes.jsonl`(默认 `C:\Users\<你>\.dsh\note-journal\notes.jsonl`),一行一条 `{t, s, title, text}`。
|
|
35
|
+
- 真正的源数据仍然是会话日志本身:每一条笔记都会以普通用户消息写进对应会话。
|
|
36
|
+
- 时区取自运行 DSH 的进程(当前为 `Asia/Shanghai`)。
|
|
37
|
+
|
|
38
|
+
## 已知限制
|
|
39
|
+
|
|
40
|
+
- 删除 `notes.jsonl` 后,报告只能看到之后的记录(会话日志里的历史仍然存在,但不会被重新导入)。
|
|
41
|
+
- 报告依赖智能体主动调用 `journal_notes`;如果它没有调用,报告内容会不完整。
|
|
42
|
+
- 笔记模式只影响提交给模型的对话;会话标题、会话列表等界面行为不变。
|
|
43
|
+
- 第一次进入笔记模式时会话没有标题,插件会把它命名为「笔记 MM-DD」,以跳过自动生成标题的模型调用。
|
|
44
|
+
- 如果 AI 正在处理上一轮任务时你在笔记模式下发消息,这一轮任务会被立即结束(笔记模式保证不发起任何模型请求);消息本身仍会被完整记录。
|
package/icon.svg
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="64" height="64" role="img" aria-label="Note journal">
|
|
2
|
+
<defs>
|
|
3
|
+
<linearGradient id="nj-bg" x1="0" y1="0" x2="1" y2="1">
|
|
4
|
+
<stop offset="0" stop-color="#2b3a4d"/>
|
|
5
|
+
<stop offset="1" stop-color="#16202b"/>
|
|
6
|
+
</linearGradient>
|
|
7
|
+
</defs>
|
|
8
|
+
<rect x="2" y="2" width="60" height="60" rx="16" fill="url(#nj-bg)"/>
|
|
9
|
+
<rect x="17" y="12" width="30" height="40" rx="5" fill="#f5f7fa"/>
|
|
10
|
+
<rect x="23" y="21" width="18" height="2.6" rx="1.3" fill="#8fa1b5"/>
|
|
11
|
+
<rect x="23" y="28" width="14" height="2.6" rx="1.3" fill="#b7c4d2"/>
|
|
12
|
+
<rect x="23" y="35" width="18" height="2.6" rx="1.3" fill="#b7c4d2"/>
|
|
13
|
+
<rect x="23" y="42" width="9" height="2.6" rx="1.3" fill="#b7c4d2"/>
|
|
14
|
+
<circle cx="47" cy="47" r="9" fill="#2fbf8f"/>
|
|
15
|
+
<circle cx="47" cy="47" r="3.6" fill="#f5f7fa"/>
|
|
16
|
+
</svg>
|
package/index.js
ADDED
|
@@ -0,0 +1,573 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Note journal — per-session "record only" mode plus periodic reports.
|
|
3
|
+
*
|
|
4
|
+
* A session is switched into note mode with the `/note` command (or the chip
|
|
5
|
+
* beside the composer). While note mode is active the plugin:
|
|
6
|
+
* - records every submitted message into the session log itself, and
|
|
7
|
+
* - ends the turn with an empty, model-free step, so no request is ever sent.
|
|
8
|
+
*
|
|
9
|
+
* The mode is derived from the `command/run` + `command/done` events of the
|
|
10
|
+
* `/note` command, so it survives restart and fork without a private format.
|
|
11
|
+
*
|
|
12
|
+
* A `journal_notes` tool exposes the captured notes, and three durable
|
|
13
|
+
* schedules (weekly / monthly / yearly) open a fresh session in which the
|
|
14
|
+
* agent writes the report.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync } from 'node:fs'
|
|
18
|
+
import { homedir } from 'node:os'
|
|
19
|
+
import { join } from 'node:path'
|
|
20
|
+
|
|
21
|
+
export const name = 'note-journal'
|
|
22
|
+
|
|
23
|
+
/** Services this plugin cannot work without. */
|
|
24
|
+
export const inject = ['tools', 'sessionProjections', 'agents']
|
|
25
|
+
|
|
26
|
+
const PROJECTION_KEY = 'noteJournal'
|
|
27
|
+
const COMMAND_NAME = 'note'
|
|
28
|
+
|
|
29
|
+
/** Stable, reference-equal wire views so unrelated state changes publish nothing. */
|
|
30
|
+
const ON_VIEW = Object.freeze({ active: true })
|
|
31
|
+
const OFF_VIEW = Object.freeze({ active: false })
|
|
32
|
+
|
|
33
|
+
const REPORT_TITLES = {
|
|
34
|
+
week: '笔记周报',
|
|
35
|
+
month: '笔记月报',
|
|
36
|
+
year: '笔记年报',
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const REPORT_LABELS = {
|
|
40
|
+
week: '周报',
|
|
41
|
+
month: '月报',
|
|
42
|
+
year: '年报',
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const REPORT_PROMPTS = {
|
|
46
|
+
week: [
|
|
47
|
+
'这是「笔记模式」插件的自动周报任务。',
|
|
48
|
+
'',
|
|
49
|
+
'步骤:',
|
|
50
|
+
'1. 调用 journal_notes 工具,参数 period="week"(该工具返回最近一个完整自然周的笔记)。',
|
|
51
|
+
'2. 根据返回的笔记写一份中文周报。',
|
|
52
|
+
'',
|
|
53
|
+
'要求:',
|
|
54
|
+
'- 用 Markdown,中文。',
|
|
55
|
+
'- 开头写「# 周报 · <起始日期> ~ <结束日期>」。',
|
|
56
|
+
'- 先给 3-5 条要点总结:这一周记录了什么、有什么进展、遇到什么问题。',
|
|
57
|
+
'- 再按会话分组列出值得回看的原始记录,保留时间与原文要点。',
|
|
58
|
+
'- 只依据笔记内容,不要编造。',
|
|
59
|
+
'- 如果工具提示该时段没有任何笔记,只输出一行「本周没有笔记记录。」,不要写别的。',
|
|
60
|
+
].join('\n'),
|
|
61
|
+
month: [
|
|
62
|
+
'这是「笔记模式」插件的自动月报任务。',
|
|
63
|
+
'',
|
|
64
|
+
'步骤:',
|
|
65
|
+
'1. 调用 journal_notes 工具,参数 period="month"(该工具返回最近一个完整自然月的笔记)。',
|
|
66
|
+
'2. 根据返回的笔记写一份中文月报。',
|
|
67
|
+
'',
|
|
68
|
+
'要求:',
|
|
69
|
+
'- 用 Markdown,中文。',
|
|
70
|
+
'- 开头写「# 月报 · <起始日期> ~ <结束日期>」。',
|
|
71
|
+
'- 先给本月主题概览与 3-6 条要点总结。',
|
|
72
|
+
'- 再按会话分组梳理主要线索、进展与遗留问题。',
|
|
73
|
+
'- 只依据笔记内容,不要编造。',
|
|
74
|
+
'- 如果工具提示该时段没有任何笔记,只输出一行「本月没有笔记记录。」,不要写别的。',
|
|
75
|
+
].join('\n'),
|
|
76
|
+
year: [
|
|
77
|
+
'这是「笔记模式」插件的自动年报任务。',
|
|
78
|
+
'',
|
|
79
|
+
'步骤:',
|
|
80
|
+
'1. 调用 journal_notes 工具,参数 period="year"(该工具返回最近一个完整自然年的笔记)。',
|
|
81
|
+
'2. 根据返回的笔记写一份中文年报。',
|
|
82
|
+
'',
|
|
83
|
+
'要求:',
|
|
84
|
+
'- 用 Markdown,中文。',
|
|
85
|
+
'- 开头写「# 年报 · <起始日期> ~ <结束日期>」。',
|
|
86
|
+
'- 先给全年回顾:主要主题、明显的变化或转折、反复出现的问题。',
|
|
87
|
+
'- 再按会话分组提炼值得保留的结论与后续要做的事。',
|
|
88
|
+
'- 只依据笔记内容,不要编造。',
|
|
89
|
+
'- 如果工具提示该时段没有任何笔记,只输出一行「本年没有笔记记录。」,不要写别的。',
|
|
90
|
+
].join('\n'),
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/* ------------------------------------------------------------------ */
|
|
94
|
+
/* Local time helpers */
|
|
95
|
+
/* ------------------------------------------------------------------ */
|
|
96
|
+
|
|
97
|
+
const pad2 = value => String(value).padStart(2, '0')
|
|
98
|
+
|
|
99
|
+
/** `YYYYMMDD` for a local date. */
|
|
100
|
+
function dateStamp(date) {
|
|
101
|
+
return `${date.getFullYear()}${pad2(date.getMonth() + 1)}${pad2(date.getDate())}`
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** `YYYYMMDDTHHMMSS` in local time, the RFC 5545 DTSTART form without a Z. */
|
|
105
|
+
function localStamp(date) {
|
|
106
|
+
return `${dateStamp(date)}T${pad2(date.getHours())}${pad2(date.getMinutes())}${pad2(date.getSeconds())}`
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** `YYYY-MM-DD HH:mm` for display. */
|
|
110
|
+
function displayTime(date) {
|
|
111
|
+
return `${date.getFullYear()}-${pad2(date.getMonth() + 1)}-${pad2(date.getDate())} ${pad2(date.getHours())}:${pad2(date.getMinutes())}`
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Human readable Chinese date, used for a pinned note-session title. */
|
|
115
|
+
function shortDate(date) {
|
|
116
|
+
return `${pad2(date.getMonth() + 1)}-${pad2(date.getDate())}`
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** The host's IANA time zone. */
|
|
120
|
+
function hostTimeZone() {
|
|
121
|
+
try {
|
|
122
|
+
const zone = Intl.DateTimeFormat().resolvedOptions().timeZone
|
|
123
|
+
if (typeof zone === 'string' && zone !== '') return zone
|
|
124
|
+
} catch { /* fall through */ }
|
|
125
|
+
return 'UTC'
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Next occurrence of `weekday` (0 = Sunday) at the given local clock time, strictly after `now`. */
|
|
129
|
+
function nextWeekday(now, weekday, hour, minute) {
|
|
130
|
+
const date = new Date(now.getTime())
|
|
131
|
+
date.setHours(hour, minute, 0, 0)
|
|
132
|
+
let delta = (weekday - date.getDay() + 7) % 7
|
|
133
|
+
if (delta === 0 && date.getTime() <= now.getTime()) delta = 7
|
|
134
|
+
date.setDate(date.getDate() + delta)
|
|
135
|
+
return date
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Next first-of-month at the given local clock time, strictly after `now`. */
|
|
139
|
+
function nextMonthStart(now, hour, minute) {
|
|
140
|
+
const date = new Date(now.getFullYear(), now.getMonth(), 1, hour, minute, 0, 0)
|
|
141
|
+
if (date.getTime() <= now.getTime()) date.setMonth(date.getMonth() + 1, 1)
|
|
142
|
+
return date
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Next first-of-year at the given local clock time, strictly after `now`. */
|
|
146
|
+
function nextYearStart(now, hour, minute) {
|
|
147
|
+
const date = new Date(now.getFullYear(), 0, 1, hour, minute, 0, 0)
|
|
148
|
+
if (date.getTime() <= now.getTime()) date.setFullYear(date.getFullYear() + 1, 0, 1)
|
|
149
|
+
return date
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* The last completed reporting window for `period`, in local time.
|
|
154
|
+
* @param {'week'|'month'|'year'} period
|
|
155
|
+
* @param {Date} now
|
|
156
|
+
* @returns {{ start: Date, end: Date }}
|
|
157
|
+
*/
|
|
158
|
+
function periodRange(period, now) {
|
|
159
|
+
if (period === 'week') {
|
|
160
|
+
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate())
|
|
161
|
+
const monday = new Date(today.getTime())
|
|
162
|
+
monday.setDate(monday.getDate() - ((monday.getDay() + 6) % 7))
|
|
163
|
+
const start = new Date(monday.getTime())
|
|
164
|
+
start.setDate(start.getDate() - 7)
|
|
165
|
+
return { start, end: monday }
|
|
166
|
+
}
|
|
167
|
+
if (period === 'month') {
|
|
168
|
+
return {
|
|
169
|
+
start: new Date(now.getFullYear(), now.getMonth() - 1, 1),
|
|
170
|
+
end: new Date(now.getFullYear(), now.getMonth(), 1),
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
start: new Date(now.getFullYear() - 1, 0, 1),
|
|
175
|
+
end: new Date(now.getFullYear(), 0, 1),
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/* ------------------------------------------------------------------ */
|
|
180
|
+
/* Note storage (a derived cache of the session logs) */
|
|
181
|
+
/* ------------------------------------------------------------------ */
|
|
182
|
+
|
|
183
|
+
function resolveDataDir() {
|
|
184
|
+
const home = process.env.DSH_HOME
|
|
185
|
+
if (typeof home === 'string' && home.trim() !== '') return join(home.trim(), 'note-journal')
|
|
186
|
+
return join(homedir(), '.dsh', 'note-journal')
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function notesFile(dataDir) {
|
|
190
|
+
return join(dataDir, 'notes.jsonl')
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function ensureDataDir(dataDir) {
|
|
194
|
+
if (!existsSync(dataDir)) mkdirSync(dataDir, { recursive: true })
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function appendNote(dataDir, record) {
|
|
198
|
+
ensureDataDir(dataDir)
|
|
199
|
+
appendFileSync(notesFile(dataDir), `${JSON.stringify(record)}\n`, 'utf8')
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function readNotes(dataDir) {
|
|
203
|
+
const file = notesFile(dataDir)
|
|
204
|
+
if (!existsSync(file)) return []
|
|
205
|
+
const out = []
|
|
206
|
+
let raw
|
|
207
|
+
try {
|
|
208
|
+
raw = readFileSync(file, 'utf8')
|
|
209
|
+
} catch {
|
|
210
|
+
return []
|
|
211
|
+
}
|
|
212
|
+
for (const line of raw.split('\n')) {
|
|
213
|
+
if (line.trim() === '') continue
|
|
214
|
+
try {
|
|
215
|
+
const parsed = JSON.parse(line)
|
|
216
|
+
if (parsed && typeof parsed.t === 'number' && typeof parsed.text === 'string') out.push(parsed)
|
|
217
|
+
} catch { /* skip a damaged line */ }
|
|
218
|
+
}
|
|
219
|
+
return out
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Text of one user message, or an empty string for attachment-only input. */
|
|
223
|
+
function messageText(message) {
|
|
224
|
+
const blocks = Array.isArray(message?.content) ? message.content : []
|
|
225
|
+
return blocks
|
|
226
|
+
.filter(block => block && block.type === 'text' && typeof block.text === 'string')
|
|
227
|
+
.map(block => block.text)
|
|
228
|
+
.join('\n')
|
|
229
|
+
.trim()
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/* ------------------------------------------------------------------ */
|
|
233
|
+
/* Projection */
|
|
234
|
+
/* ------------------------------------------------------------------ */
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* A tiny zod-compatible schema: the projection registry only calls `parse`.
|
|
238
|
+
* @param {(value: any) => any} check
|
|
239
|
+
*/
|
|
240
|
+
function schema(check) {
|
|
241
|
+
return {
|
|
242
|
+
parse(value) {
|
|
243
|
+
return check(value)
|
|
244
|
+
},
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const stateSchema = schema((value) => {
|
|
249
|
+
if (value === null || typeof value !== 'object') throw new Error('note-journal: state must be an object')
|
|
250
|
+
if (typeof value.active !== 'boolean') throw new Error('note-journal: state.active must be boolean')
|
|
251
|
+
const running = value.running
|
|
252
|
+
if (running !== null && running !== undefined) {
|
|
253
|
+
if (typeof running !== 'object' || typeof running.commandId !== 'string' || typeof running.wanted !== 'boolean') {
|
|
254
|
+
throw new Error('note-journal: state.running is malformed')
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return {
|
|
258
|
+
active: value.active,
|
|
259
|
+
running: running === null || running === undefined
|
|
260
|
+
? null
|
|
261
|
+
: { commandId: running.commandId, wanted: running.wanted },
|
|
262
|
+
}
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
const viewSchema = schema((value) => {
|
|
266
|
+
if (value === null || typeof value !== 'object' || typeof value.active !== 'boolean') {
|
|
267
|
+
throw new Error('note-journal: view is malformed')
|
|
268
|
+
}
|
|
269
|
+
return value.active ? ON_VIEW : OFF_VIEW
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
/** The `noteJournal` unit: folds `/note` command events into an on/off mode. */
|
|
273
|
+
const projectionDefinition = {
|
|
274
|
+
key: PROJECTION_KEY,
|
|
275
|
+
stateVersion: 1,
|
|
276
|
+
stateSchema,
|
|
277
|
+
init: () => ({ active: false, running: null }),
|
|
278
|
+
apply: (state, event) => {
|
|
279
|
+
if (event.type === 'command/run' && event.data?.name === COMMAND_NAME) {
|
|
280
|
+
const raw = typeof event.data.args === 'string' ? event.data.args.trim().toLowerCase() : ''
|
|
281
|
+
const wanted = raw === 'on' ? true : raw === 'off' ? false : !state.active
|
|
282
|
+
return { ...state, running: { commandId: String(event.data.commandId), wanted } }
|
|
283
|
+
}
|
|
284
|
+
if (event.type === 'command/done' && state.running !== null
|
|
285
|
+
&& String(event.data?.commandId) === state.running.commandId) {
|
|
286
|
+
const committed = event.data?.kind === 'success' ? state.running.wanted : state.active
|
|
287
|
+
return committed === state.active && state.running === null
|
|
288
|
+
? state
|
|
289
|
+
: { ...state, active: committed, running: null }
|
|
290
|
+
}
|
|
291
|
+
return state
|
|
292
|
+
},
|
|
293
|
+
wire: {
|
|
294
|
+
viewSchema,
|
|
295
|
+
view: state => (state.active ? ON_VIEW : OFF_VIEW),
|
|
296
|
+
},
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/* ------------------------------------------------------------------ */
|
|
300
|
+
/* Plugin */
|
|
301
|
+
/* ------------------------------------------------------------------ */
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* @param {import('@deepseek-ai/cordis').Context} ctx
|
|
305
|
+
*/
|
|
306
|
+
export function apply(ctx, config) {
|
|
307
|
+
const dataDir = resolveDataDir()
|
|
308
|
+
const timeZone = hostTimeZone()
|
|
309
|
+
const configuredCwd = typeof config?.reportCwd === 'string' && config.reportCwd.trim() !== ''
|
|
310
|
+
? config.reportCwd.trim()
|
|
311
|
+
: undefined
|
|
312
|
+
let handle
|
|
313
|
+
|
|
314
|
+
const ready = (async () => {
|
|
315
|
+
handle = await ctx.sessionProjections.register(projectionDefinition)
|
|
316
|
+
return handle
|
|
317
|
+
})()
|
|
318
|
+
|
|
319
|
+
const warn = (message, error) => {
|
|
320
|
+
try {
|
|
321
|
+
ctx.logger?.warn?.(`note-journal: ${message}: ${error instanceof Error ? error.message : String(error)}`)
|
|
322
|
+
} catch { /* logging must never break the turn */ }
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** Current committed note mode of a session (false when the unit is not ready). */
|
|
326
|
+
async function noteActive(session) {
|
|
327
|
+
try {
|
|
328
|
+
const unit = await ready
|
|
329
|
+
return unit.stateOf(session).active === true
|
|
330
|
+
} catch (error) {
|
|
331
|
+
warn('could not read the note projection', error)
|
|
332
|
+
return false
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// 1. Record submitted messages instead of asking the model.
|
|
337
|
+
ctx.on('agent/pre-step', async (payload, next) => {
|
|
338
|
+
const decision = await next()
|
|
339
|
+
if (decision.kind === 'reject') return decision
|
|
340
|
+
const session = payload.agent.session
|
|
341
|
+
if (!(await noteActive(session))) return decision
|
|
342
|
+
|
|
343
|
+
const titles = ctx.get('sessionTitle')
|
|
344
|
+
let title
|
|
345
|
+
try {
|
|
346
|
+
title = titles?.get?.(session)?.title
|
|
347
|
+
} catch { /* a title is optional */ }
|
|
348
|
+
|
|
349
|
+
// The loop appends `user/message` only for an entering step, so record the
|
|
350
|
+
// batch here before ending the turn without a request.
|
|
351
|
+
for (const message of decision.messages) {
|
|
352
|
+
if (message?.source?.kind === 'runtime-context') continue
|
|
353
|
+
try {
|
|
354
|
+
session.append('user/message', message, { surfaceOp: 'append' })
|
|
355
|
+
} catch (error) {
|
|
356
|
+
warn('could not record a note message in the session log', error)
|
|
357
|
+
continue
|
|
358
|
+
}
|
|
359
|
+
if (message?.source?.kind !== 'user') continue
|
|
360
|
+
const text = messageText(message)
|
|
361
|
+
if (text === '') continue
|
|
362
|
+
try {
|
|
363
|
+
appendNote(dataDir, { t: Date.now(), s: String(session.id), title: title ?? '', text })
|
|
364
|
+
} catch (error) {
|
|
365
|
+
warn('could not write the note cache', error)
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// `reject` is the only decision the loop checks BEFORE it looks at
|
|
370
|
+
// messages, so a later waterfall listener that spreads extra context into
|
|
371
|
+
// `messages` cannot turn it back into a model request. An empty entering
|
|
372
|
+
// step looks tidier (the turn ends `completed`) but another listener may
|
|
373
|
+
// re-add injected context to it, which would spend a model call.
|
|
374
|
+
return { kind: 'reject' }
|
|
375
|
+
})
|
|
376
|
+
|
|
377
|
+
// 2. `/note [on|off]` switches the mode; the projection derives it from the
|
|
378
|
+
// command lifecycle events the registry appends for us.
|
|
379
|
+
ctx.inject(['commands'], (commandCtx) => {
|
|
380
|
+
commandCtx.commands.register({
|
|
381
|
+
name: COMMAND_NAME,
|
|
382
|
+
description: '切换当前会话的笔记模式:只记录消息,AI 不回复',
|
|
383
|
+
input: { hint: '[on|off]' },
|
|
384
|
+
handler: async ({ agent, rawInput }) => {
|
|
385
|
+
const session = agent.session
|
|
386
|
+
const current = await noteActive(session)
|
|
387
|
+
const word = rawInput.trim().toLowerCase()
|
|
388
|
+
const nextActive = word === 'on' ? true : word === 'off' ? false : !current
|
|
389
|
+
if (nextActive === current) {
|
|
390
|
+
return {
|
|
391
|
+
kind: 'success',
|
|
392
|
+
text: current ? '笔记模式已开启:消息只记录,AI 不会回复。' : '笔记模式已关闭。',
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
if (nextActive) {
|
|
396
|
+
const titles = ctx.get('sessionTitle')
|
|
397
|
+
try {
|
|
398
|
+
if (titles !== undefined && titles.get(session) === undefined) {
|
|
399
|
+
titles.rename(session, `笔记 ${shortDate(new Date())}`)
|
|
400
|
+
}
|
|
401
|
+
} catch (error) {
|
|
402
|
+
warn('could not name the note session', error)
|
|
403
|
+
}
|
|
404
|
+
return {
|
|
405
|
+
kind: 'success',
|
|
406
|
+
text: '已进入笔记模式:之后的消息只记录、不回复,也不消耗 token。再次发送 /note 退出。',
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
return { kind: 'success', text: '已退出笔记模式:恢复正常对话。' }
|
|
410
|
+
},
|
|
411
|
+
})
|
|
412
|
+
})
|
|
413
|
+
|
|
414
|
+
// 3. The report session reads the notes through this tool.
|
|
415
|
+
ctx.tools.register({
|
|
416
|
+
name: 'journal_notes',
|
|
417
|
+
description: '读取指定时段内由「笔记模式」记录的全部笔记。'
|
|
418
|
+
+ 'period="week" 返回最近一个完整自然周,"month" 返回最近一个完整自然月,'
|
|
419
|
+
+ '"year" 返回最近一个完整自然年。按会话分组、按时间排序,包含原始文本。',
|
|
420
|
+
parameters: {
|
|
421
|
+
period: {
|
|
422
|
+
type: 'string',
|
|
423
|
+
enum: ['week', 'month', 'year'],
|
|
424
|
+
default: 'week',
|
|
425
|
+
description: '要读取的时段:week(上周)、month(上月)或 year(去年)。',
|
|
426
|
+
},
|
|
427
|
+
},
|
|
428
|
+
output: {
|
|
429
|
+
schema: {
|
|
430
|
+
type: 'object',
|
|
431
|
+
additionalProperties: false,
|
|
432
|
+
properties: {
|
|
433
|
+
period: { type: 'string' },
|
|
434
|
+
start: { type: 'string' },
|
|
435
|
+
end: { type: 'string' },
|
|
436
|
+
count: { type: 'integer' },
|
|
437
|
+
sessions: { type: 'integer' },
|
|
438
|
+
notes: { type: 'string' },
|
|
439
|
+
},
|
|
440
|
+
required: ['period', 'start', 'end', 'count', 'sessions', 'notes'],
|
|
441
|
+
},
|
|
442
|
+
render: (_args, value) => [{ type: 'text', text: value.notes }],
|
|
443
|
+
},
|
|
444
|
+
execute: (args) => {
|
|
445
|
+
const period = args.period === 'month' || args.period === 'year' ? args.period : 'week'
|
|
446
|
+
const { start, end } = periodRange(period, new Date())
|
|
447
|
+
const from = start.getTime()
|
|
448
|
+
const to = end.getTime()
|
|
449
|
+
const records = readNotes(dataDir)
|
|
450
|
+
.filter(record => record.t >= from && record.t < to)
|
|
451
|
+
.sort((left, right) => left.t - right.t)
|
|
452
|
+
|
|
453
|
+
const header = `时段:${displayTime(start)} ~ ${displayTime(end)}`
|
|
454
|
+
if (records.length === 0) {
|
|
455
|
+
return {
|
|
456
|
+
period,
|
|
457
|
+
start: start.toISOString(),
|
|
458
|
+
end: end.toISOString(),
|
|
459
|
+
count: 0,
|
|
460
|
+
sessions: 0,
|
|
461
|
+
notes: `${header}\n该时段(${REPORT_LABELS[period]})没有任何笔记。`,
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
const groups = new Map()
|
|
466
|
+
for (const record of records) {
|
|
467
|
+
const key = `${record.title || '未命名会话'}\u0000${record.s}`
|
|
468
|
+
if (!groups.has(key)) groups.set(key, [])
|
|
469
|
+
groups.get(key).push(record)
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
const lines = [header, `笔记条数:${records.length}`, `涉及会话:${groups.size}`, '']
|
|
473
|
+
let index = 0
|
|
474
|
+
for (const [key, items] of groups) {
|
|
475
|
+
index += 1
|
|
476
|
+
const [title, sessionId] = key.split('\u0000')
|
|
477
|
+
lines.push(`## ${index}. ${title}(${sessionId})`)
|
|
478
|
+
for (const item of items) {
|
|
479
|
+
const clock = displayTime(new Date(item.t)).slice(5)
|
|
480
|
+
lines.push(`- ${clock} ${item.text.replace(/\n/g, '\n ')}`)
|
|
481
|
+
}
|
|
482
|
+
lines.push('')
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
return {
|
|
486
|
+
period,
|
|
487
|
+
start: start.toISOString(),
|
|
488
|
+
end: end.toISOString(),
|
|
489
|
+
count: records.length,
|
|
490
|
+
sessions: groups.size,
|
|
491
|
+
notes: lines.join('\n').trimEnd(),
|
|
492
|
+
}
|
|
493
|
+
},
|
|
494
|
+
})
|
|
495
|
+
|
|
496
|
+
// 4. Keep the three report schedules armed (idempotent, matched by title).
|
|
497
|
+
ctx.inject(['schedule'], (scheduleCtx) => {
|
|
498
|
+
const schedule = scheduleCtx.schedule
|
|
499
|
+
let disposed = false
|
|
500
|
+
|
|
501
|
+
const ensure = async () => {
|
|
502
|
+
const existing = await schedule.list({})
|
|
503
|
+
const known = new Set(
|
|
504
|
+
existing.items
|
|
505
|
+
.filter(item => item.status !== 'ended')
|
|
506
|
+
.map(item => item.title),
|
|
507
|
+
)
|
|
508
|
+
// Give the report session a directory that belongs to the user's
|
|
509
|
+
// workspaces, so it joins that workspace and shows up in the sidebar.
|
|
510
|
+
let cwd = configuredCwd
|
|
511
|
+
const working = ctx.get('workingDirectory')
|
|
512
|
+
const fallbackCwd = typeof working?.defaultDirectory === 'string' ? working.defaultDirectory : undefined
|
|
513
|
+
if (cwd === undefined) {
|
|
514
|
+
try {
|
|
515
|
+
const controller = ctx.get('sessionController')
|
|
516
|
+
if (controller !== undefined) {
|
|
517
|
+
const listed = await controller.list({}, new AbortController().signal)
|
|
518
|
+
const candidates = (listed?.items ?? [])
|
|
519
|
+
.filter(item => item !== undefined
|
|
520
|
+
&& item.origin !== 'subagent'
|
|
521
|
+
&& item.parentSessionId === undefined
|
|
522
|
+
&& typeof item.cwd === 'string'
|
|
523
|
+
&& item.cwd !== ''
|
|
524
|
+
&& item.cwd !== fallbackCwd)
|
|
525
|
+
.sort((left, right) => (right.updatedAt ?? 0) - (left.updatedAt ?? 0))
|
|
526
|
+
cwd = candidates[0]?.cwd
|
|
527
|
+
}
|
|
528
|
+
} catch (error) {
|
|
529
|
+
warn('could not read the Session list for a report directory', error)
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
if (cwd === undefined) cwd = fallbackCwd
|
|
533
|
+
const now = new Date()
|
|
534
|
+
const specs = [
|
|
535
|
+
{
|
|
536
|
+
period: 'week',
|
|
537
|
+
rrule: `DTSTART;TZID=${timeZone}:${localStamp(nextWeekday(now, 1, 9, 0))}\nRRULE:FREQ=WEEKLY;BYDAY=MO`,
|
|
538
|
+
},
|
|
539
|
+
{
|
|
540
|
+
period: 'month',
|
|
541
|
+
rrule: `DTSTART;TZID=${timeZone}:${localStamp(nextMonthStart(now, 9, 0))}\nRRULE:FREQ=MONTHLY;BYMONTHDAY=1`,
|
|
542
|
+
},
|
|
543
|
+
{
|
|
544
|
+
period: 'year',
|
|
545
|
+
rrule: `DTSTART;TZID=${timeZone}:${localStamp(nextYearStart(now, 9, 0))}\nRRULE:FREQ=YEARLY;BYMONTH=1;BYMONTHDAY=1`,
|
|
546
|
+
},
|
|
547
|
+
]
|
|
548
|
+
for (const spec of specs) {
|
|
549
|
+
if (disposed) return
|
|
550
|
+
const title = REPORT_TITLES[spec.period]
|
|
551
|
+
if (known.has(title)) continue
|
|
552
|
+
try {
|
|
553
|
+
await schedule.create({
|
|
554
|
+
title,
|
|
555
|
+
prompt: REPORT_PROMPTS[spec.period],
|
|
556
|
+
timing: { kind: 'rrule', rrule: spec.rrule },
|
|
557
|
+
target: { kind: 'new', create: cwd === undefined ? {} : { cwd } },
|
|
558
|
+
})
|
|
559
|
+
} catch (error) {
|
|
560
|
+
warn(`could not create the ${spec.period} report schedule`, error)
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
ctx.effect(() => {
|
|
566
|
+
disposed = false
|
|
567
|
+
void ensure().catch(error => warn('could not set up report schedules', error))
|
|
568
|
+
return () => {
|
|
569
|
+
disposed = true
|
|
570
|
+
}
|
|
571
|
+
}, 'note-journal: report schedules')
|
|
572
|
+
})
|
|
573
|
+
}
|
package/locale/en.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "Note journal",
|
|
4
|
+
"description": "Switch a single chat into note mode: your messages are recorded without any model reply or token use, and a weekly, monthly and yearly report is generated from them in a fresh session.",
|
|
5
|
+
"detail": "docs/DETAIL.md"
|
|
6
|
+
}
|
|
7
|
+
}
|
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-note-journal",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Note mode for individual DeepSeek Harness sessions plus weekly, monthly and yearly reports.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"dsh",
|
|
7
|
+
"dsh-plugin",
|
|
8
|
+
"dsh-bundle",
|
|
9
|
+
"deepseek-harness",
|
|
10
|
+
"notes",
|
|
11
|
+
"journal",
|
|
12
|
+
"note-mode"
|
|
13
|
+
],
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"author": "Emma-covus",
|
|
16
|
+
"type": "module",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": "./index.js",
|
|
19
|
+
"./client": "./client.js",
|
|
20
|
+
"./locale/*.json": "./locale/*.json",
|
|
21
|
+
"./icon": "./icon.svg"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"index.js",
|
|
25
|
+
"client.js",
|
|
26
|
+
"cordis.patch.yml",
|
|
27
|
+
"locale/*.json",
|
|
28
|
+
"docs",
|
|
29
|
+
"icon.svg",
|
|
30
|
+
"README.md"
|
|
31
|
+
],
|
|
32
|
+
"dsh": {
|
|
33
|
+
"bundle": {
|
|
34
|
+
"patch": "./cordis.patch.yml"
|
|
35
|
+
},
|
|
36
|
+
"client": {
|
|
37
|
+
"platform": "web",
|
|
38
|
+
"immediately": true,
|
|
39
|
+
"inject": [
|
|
40
|
+
"@deepseek-ai/dsh-client-locale",
|
|
41
|
+
"@deepseek-ai/dsh-client-ui-conversation"
|
|
42
|
+
]
|
|
43
|
+
}
|
|
44
|
+
}
|
|
6
45
|
}
|