dsh-memsearch-community 0.0.0-stage → 0.2.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/LICENSE +24 -0
- package/README.md +121 -2
- package/cordis.patch.yml +8 -0
- package/docs/DETAIL.md +61 -0
- package/icon.svg +22 -0
- package/lib/index.js +328 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +61 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 dsh-memsearch-community contributors
|
|
4
|
+
|
|
5
|
+
Portions derived from dsh-memsearch 0.1.7 (MIT), itself inspired by the
|
|
6
|
+
memsearch CLI. Upstream copyright belongs to its respective authors.
|
|
7
|
+
|
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
10
|
+
in the Software without restriction, including without limitation the rights
|
|
11
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
13
|
+
furnished to do so, subject to the following conditions:
|
|
14
|
+
|
|
15
|
+
The above copyright notice and this permission notice shall be included in all
|
|
16
|
+
copies or substantial portions of the Software.
|
|
17
|
+
|
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,122 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-memsearch-community
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Automatic, local, cross-session memory for **DeepSeek Harness**.
|
|
4
|
+
|
|
5
|
+
Every finished turn is appended to a daily markdown journal, the journal is
|
|
6
|
+
indexed into a local vector store, and the agent can then recall earlier
|
|
7
|
+
sessions with semantic search. Nothing leaves the machine: embeddings are
|
|
8
|
+
computed locally with ONNX.
|
|
9
|
+
|
|
10
|
+
> This is an **unofficial community fork** of `dsh-memsearch` (upstream 0.1.7,
|
|
11
|
+
> MIT). It is not affiliated with the upstream author or with DeepSeek.
|
|
12
|
+
|
|
13
|
+
## Changes from upstream
|
|
14
|
+
|
|
15
|
+
**0.2.1 — capture the real prompt**
|
|
16
|
+
|
|
17
|
+
DeepSeek Harness delivers injected context as ordinary `user/message` events:
|
|
18
|
+
`runtime-context`, `time-context`, `skill-catalog`, `model-selection` and the
|
|
19
|
+
plugin's own status line. Upstream skipped only `source.kind === "plugin"`, so
|
|
20
|
+
every journal entry began with harness boilerplate such as
|
|
21
|
+
`Time sampled while preparing turn 3, step 1: …` instead of what the user
|
|
22
|
+
actually asked.
|
|
23
|
+
|
|
24
|
+
The extractor now accepts only `source.kind === "user"` (plus messages that
|
|
25
|
+
carry no source at all), rejects text that still looks like an injected block,
|
|
26
|
+
and pairs the prompt with the final assistant reply that follows it.
|
|
27
|
+
|
|
28
|
+
**0.2.0 — installable bundle**
|
|
29
|
+
|
|
30
|
+
Packaged as a DSH bundle: `dsh.bundle.patch`, locale metadata, an icon, and a
|
|
31
|
+
detail document, so the plugin installs by package name instead of a hand-edited
|
|
32
|
+
profile patch. Also: Windows `PATH` handling, and graceful degradation when the
|
|
33
|
+
`memsearch` CLI is missing.
|
|
34
|
+
|
|
35
|
+
## What it does
|
|
36
|
+
|
|
37
|
+
| Behaviour | Detail |
|
|
38
|
+
| --- | --- |
|
|
39
|
+
| Journal capture | After each turn, appends the user prompt and the final assistant reply to `~/.memsearch/memory/YYYY-MM-DD.md` |
|
|
40
|
+
| Auto indexing | Runs `memsearch index` in the background so the next turn can search the previous one |
|
|
41
|
+
| Session status | Injects one short status line at the start of a session (version, embedding provider, journal path, number of past journal files) |
|
|
42
|
+
| Graceful degradation | Without the `memsearch` CLI it still writes the journal, and skips indexing instead of failing |
|
|
43
|
+
|
|
44
|
+
It does **not** push memories into the model's context automatically. It gives
|
|
45
|
+
the agent a searchable store; the agent (or you) decides when to query it.
|
|
46
|
+
|
|
47
|
+
## Requirements
|
|
48
|
+
|
|
49
|
+
- DeepSeek Harness `0.2.x`
|
|
50
|
+
- Node.js >= 18 (provided by DSH)
|
|
51
|
+
- Optional but recommended: the [`memsearch`](https://pypi.org/project/memsearch/) CLI on `PATH` for indexing and search.
|
|
52
|
+
Without it, journaling works and recall does not.
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
pip install memsearch # or pipx install memsearch
|
|
56
|
+
memsearch --version
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Install
|
|
60
|
+
|
|
61
|
+
### From a published package
|
|
62
|
+
|
|
63
|
+
Ask the agent, or use **Plugins → Add plugin** and give the package name:
|
|
64
|
+
|
|
65
|
+
```
|
|
66
|
+
dsh-memsearch-community
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
The agent-side equivalent is `plugin_manager` with `action: install_bundle`
|
|
70
|
+
and the package name as `target`.
|
|
71
|
+
|
|
72
|
+
### From a local checkout
|
|
73
|
+
|
|
74
|
+
A local install links the directory instead of copying it, and a linked
|
|
75
|
+
directory does **not** get its dependencies installed. Install them once
|
|
76
|
+
inside the checkout first, otherwise activation fails with
|
|
77
|
+
`Cannot find package '@deepseek-ai/schemastery'`:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
cd dsh-memsearch-community
|
|
81
|
+
npm install --omit=dev # installs the plugin's deps and peer deps locally
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Then install the directory:
|
|
85
|
+
|
|
86
|
+
```
|
|
87
|
+
plugin_manager → install_bundle → target: /absolute/path/to/dsh-memsearch-community
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Then start a new session. The first message of the session shows the memory
|
|
91
|
+
status line when the plugin is live.
|
|
92
|
+
|
|
93
|
+
## Configuration
|
|
94
|
+
|
|
95
|
+
The bundle inserts one row, `memsearch-community`. Its configuration:
|
|
96
|
+
|
|
97
|
+
| Key | Default | Meaning |
|
|
98
|
+
| --- | --- | --- |
|
|
99
|
+
| `enabled` | `true` | Master switch |
|
|
100
|
+
| `memoryDir` | `""` | Journal directory. Empty = `$MEMSEARCH_DIR/memory`, else `~/.memsearch/memory` |
|
|
101
|
+
| `collection` | `""` | Milvus collection name. Empty = the CLI's configured default |
|
|
102
|
+
| `injectSessionStatus` | `true` | Inject the status line at session start |
|
|
103
|
+
| `capture` | `true` | Append a journal entry after each turn |
|
|
104
|
+
| `captureMinPromptLength` | `10` | Skip turns whose user prompt is shorter than this |
|
|
105
|
+
| `maxCaptureChars` | `8000` | Per-field cap on the captured text |
|
|
106
|
+
| `summarizePlugin` | `""` | Optional CLI summarize plugin id |
|
|
107
|
+
| `indexAfterCapture` | `true` | Run `memsearch index` after each capture |
|
|
108
|
+
|
|
109
|
+
## Limitations
|
|
110
|
+
|
|
111
|
+
- Only the **final** assistant reply is stored; tool calls and intermediate
|
|
112
|
+
steps are not. Use `memsearch transcript` for those.
|
|
113
|
+
- Prompts shorter than `captureMinPromptLength` are not journaled.
|
|
114
|
+
- Indexing is asynchronous, so a turn may be searchable a moment after it ends.
|
|
115
|
+
- Search returns similarity-ranked chunks, not chronological order. "What was
|
|
116
|
+
the *first* document I sent?" needs the journal file or the session logs.
|
|
117
|
+
- If `dsh-memsearch` (upstream) is also enabled, both plugins capture and the
|
|
118
|
+
journal receives duplicate entries. Enable only one.
|
|
119
|
+
|
|
120
|
+
## Licence
|
|
121
|
+
|
|
122
|
+
MIT. See [LICENSE](LICENSE). Portions derived from `dsh-memsearch` 0.1.7.
|
package/cordis.patch.yml
ADDED
package/docs/DETAIL.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# Memory search
|
|
2
|
+
|
|
3
|
+
Gives DeepSeek Harness a long-term memory that lives entirely on your machine.
|
|
4
|
+
|
|
5
|
+
## What it does
|
|
6
|
+
|
|
7
|
+
After every turn the plugin appends the user prompt and the final assistant
|
|
8
|
+
reply to a markdown journal under `~/.memsearch/memory/`, one file per day.
|
|
9
|
+
It then asks the `memsearch` CLI to index the journal into a local vector
|
|
10
|
+
store. On the next session start it prints a one-line status showing the CLI
|
|
11
|
+
version, the embedding provider, the journal path and how many past journal
|
|
12
|
+
files exist.
|
|
13
|
+
|
|
14
|
+
The journal is ordinary markdown: you can read it, diff it, back it up, or
|
|
15
|
+
delete it. The vector index is a local Milvus file at `~/.memsearch/milvus.db`.
|
|
16
|
+
|
|
17
|
+
## Requirements
|
|
18
|
+
|
|
19
|
+
The plugin works in two modes.
|
|
20
|
+
|
|
21
|
+
- **With the `memsearch` CLI installed** — journaling, indexing and semantic
|
|
22
|
+
search all work. Install it with `pip install memsearch`.
|
|
23
|
+
- **Without the CLI** — journaling still works; indexing and recall are
|
|
24
|
+
skipped and the status line says so. Nothing crashes.
|
|
25
|
+
|
|
26
|
+
## Using it
|
|
27
|
+
|
|
28
|
+
You do not have to do anything for memory to accumulate. To recall something,
|
|
29
|
+
ask the agent about an earlier session in plain language, for example
|
|
30
|
+
"what port did we use last time", and it can search the journal.
|
|
31
|
+
|
|
32
|
+
On the command line, the same store is available directly:
|
|
33
|
+
|
|
34
|
+
```
|
|
35
|
+
memsearch search "what did we decide about the proxy pool" -k 5
|
|
36
|
+
memsearch stats
|
|
37
|
+
memsearch index ~/.memsearch/memory
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## What it does not do
|
|
41
|
+
|
|
42
|
+
It never injects memories into the conversation on its own. The first
|
|
43
|
+
assistant step sees a status line, not the memories themselves; retrieving
|
|
44
|
+
them is a deliberate search.
|
|
45
|
+
|
|
46
|
+
## Known limitations
|
|
47
|
+
|
|
48
|
+
- Only the final assistant reply is journaled. Intermediate steps and tool
|
|
49
|
+
calls are dropped; `memsearch transcript` can recover them from session logs.
|
|
50
|
+
- Prompts shorter than 10 characters are not journaled.
|
|
51
|
+
- Indexing runs in the background, so a turn becomes searchable shortly after
|
|
52
|
+
it ends rather than instantly.
|
|
53
|
+
- Search ranks by similarity, not by time, so ordinal questions such as "the
|
|
54
|
+
first paper I sent you" are better answered from the journal file itself.
|
|
55
|
+
- Do not enable this alongside the upstream `dsh-memsearch` plugin: both would
|
|
56
|
+
capture, and the journal would receive duplicate entries.
|
|
57
|
+
|
|
58
|
+
## Privacy
|
|
59
|
+
|
|
60
|
+
Everything stays local. Embeddings are computed by a local ONNX model; no
|
|
61
|
+
network calls are made by this plugin.
|
package/icon.svg
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 128 128" width="128" height="128" role="img" aria-label="Memory search">
|
|
2
|
+
<defs>
|
|
3
|
+
<linearGradient id="bg" x1="0" y1="0" x2="1" y2="1">
|
|
4
|
+
<stop offset="0" stop-color="#2b3a67"/>
|
|
5
|
+
<stop offset="1" stop-color="#1b2440"/>
|
|
6
|
+
</linearGradient>
|
|
7
|
+
<linearGradient id="page" x1="0" y1="0" x2="0" y2="1">
|
|
8
|
+
<stop offset="0" stop-color="#ffffff"/>
|
|
9
|
+
<stop offset="1" stop-color="#dfe6f5"/>
|
|
10
|
+
</linearGradient>
|
|
11
|
+
</defs>
|
|
12
|
+
<rect x="4" y="4" width="120" height="120" rx="26" fill="url(#bg)"/>
|
|
13
|
+
<rect x="30" y="26" width="52" height="70" rx="7" fill="url(#page)"/>
|
|
14
|
+
<rect x="30" y="26" width="9" height="70" rx="4.5" fill="#8fa6d8"/>
|
|
15
|
+
<g stroke="#8fa6d8" stroke-width="4" stroke-linecap="round">
|
|
16
|
+
<line x1="48" y1="44" x2="72" y2="44"/>
|
|
17
|
+
<line x1="48" y1="57" x2="72" y2="57"/>
|
|
18
|
+
<line x1="48" y1="70" x2="64" y2="70"/>
|
|
19
|
+
</g>
|
|
20
|
+
<circle cx="83" cy="80" r="19" fill="none" stroke="#ffd166" stroke-width="8"/>
|
|
21
|
+
<line x1="96" y1="93" x2="110" y2="107" stroke="#ffd166" stroke-width="9" stroke-linecap="round"/>
|
|
22
|
+
</svg>
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-memsearch — adapted build for DSH 0.2.2-rc.5
|
|
3
|
+
*
|
|
4
|
+
* 基于上游 dsh-memsearch 0.1.7 源码改造,改动点:
|
|
5
|
+
* 1. peer 版本范围放宽到覆盖当前运行时(cordis / dsh-agent);
|
|
6
|
+
* 2. CLI 探测做缓存:memsearch 命令不存在时静默跳过索引 / 状态注入,
|
|
7
|
+
* 不再让未接住的异常把 DSH 进程拖崩;
|
|
8
|
+
* 3. pre-step 钩子整体包 try/catch,任何异常降级为放行原决策;
|
|
9
|
+
* 4. 兼容 Windows:PATH 用 ; 分隔,CLI 解析同时覆盖 .cmd / .exe。
|
|
10
|
+
*
|
|
11
|
+
* 行为与上游一致:
|
|
12
|
+
* - 会话第一步注入一条状态消息(版本 / embedding / 日记目录 / 历史条数);
|
|
13
|
+
* - 每轮结束把用户提问 + 最终回复追加到 <memoryDir>/<YYYY-MM-DD>.md;
|
|
14
|
+
* - 有 CLI 时后台增量建索引;无 CLI 时只写本地日记,检索暂不可用。
|
|
15
|
+
*/
|
|
16
|
+
import z from "@deepseek-ai/schemastery";
|
|
17
|
+
import "@deepseek-ai/cordis";
|
|
18
|
+
import { randomUUID } from "node:crypto";
|
|
19
|
+
import { execFile } from "node:child_process";
|
|
20
|
+
import { appendFileSync, existsSync, mkdirSync, readdirSync, readFileSync } from "node:fs";
|
|
21
|
+
import os from "node:os";
|
|
22
|
+
import path from "node:path";
|
|
23
|
+
|
|
24
|
+
const name = "dsh-memsearch";
|
|
25
|
+
const inject = ["agents"];
|
|
26
|
+
|
|
27
|
+
const Config = z.object({
|
|
28
|
+
enabled: z.boolean().default(true),
|
|
29
|
+
/** Journal dir; empty = $MEMSEARCH_DIR/memory or ~/.memsearch/memory. */
|
|
30
|
+
memoryDir: z.string().default(""),
|
|
31
|
+
/** Milvus collection passed to the CLI; empty = configured default. */
|
|
32
|
+
collection: z.string().default(""),
|
|
33
|
+
/** Inject the session-start memsearch status + recall hint. */
|
|
34
|
+
injectSessionStatus: z.boolean().default(true),
|
|
35
|
+
/** Auto-append a journal entry after each turn. */
|
|
36
|
+
capture: z.boolean().default(true),
|
|
37
|
+
/** Ignore turns whose user prompt is shorter than this. */
|
|
38
|
+
captureMinPromptLength: z.number().default(10),
|
|
39
|
+
/** Cap on the raw exchange text written per turn. */
|
|
40
|
+
maxCaptureChars: z.number().default(8000),
|
|
41
|
+
/** Optional summarize plugin id; "" = raw fallback. */
|
|
42
|
+
summarizePlugin: z.string().default(""),
|
|
43
|
+
/** Re-run `memsearch index` after appending a journal entry. */
|
|
44
|
+
indexAfterCapture: z.boolean().default(true),
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const MS_TIMEOUT = 300000; // long: first onnx model download
|
|
48
|
+
const MS_STATUS_TIMEOUT = 15000;
|
|
49
|
+
const MAX_BUFFER = 8 * 1024 * 1024;
|
|
50
|
+
|
|
51
|
+
function home() {
|
|
52
|
+
return os.homedir();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function expandPath(p) {
|
|
56
|
+
return p.replace(/^~(?=$|\/)/, home());
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function resolveMemoryDir(configured) {
|
|
60
|
+
if (configured) return expandPath(configured);
|
|
61
|
+
const base = process.env.MEMSEARCH_DIR;
|
|
62
|
+
if (base) return path.join(expandPath(base), "memory");
|
|
63
|
+
return path.join(home(), ".memsearch", "memory");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** PATH separator: Windows ';', POSIX ':'. */
|
|
67
|
+
function pathSep() {
|
|
68
|
+
return process.platform === "win32" ? ";" : ":";
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function memsearchEnv() {
|
|
72
|
+
const env = { ...process.env };
|
|
73
|
+
// Windows 无开发者模式时 HF 缓存无法建软链,抑制该无害警告。
|
|
74
|
+
env.HF_HUB_DISABLE_SYMLINKS_WARNING = "1";
|
|
75
|
+
const existing = (env.PATH || "").split(pathSep()).filter(Boolean);
|
|
76
|
+
const dirs = ["$HOME/.local/bin", "$HOME/.cargo/bin", "$HOME/bin", "/usr/local/bin", "/usr/bin", "/bin"]
|
|
77
|
+
.map((d) => expandPath(d))
|
|
78
|
+
.filter((d) => existsSync(d) && !existing.includes(d));
|
|
79
|
+
// DSH bundled Python Scripts (Windows per-user install): memsearch.exe lives here.
|
|
80
|
+
const runtimeScripts = [
|
|
81
|
+
`${home()}/AppData/Local/Programs/DeepSeek Harness/resources/runtime/primary-runtime/dependencies/python/Scripts`,
|
|
82
|
+
]
|
|
83
|
+
.map((d) => expandPath(d))
|
|
84
|
+
.filter((d) => existsSync(d) && !existing.includes(d));
|
|
85
|
+
env.PATH = [...runtimeScripts, ...dirs, ...existing].join(pathSep());
|
|
86
|
+
return env;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Resolve the memsearch executable once per process.
|
|
91
|
+
* Returns null when the CLI is not installed; callers must treat null as "skip".
|
|
92
|
+
*/
|
|
93
|
+
let cliCache = undefined;
|
|
94
|
+
function resolveCli() {
|
|
95
|
+
if (cliCache !== undefined) return cliCache;
|
|
96
|
+
const env = memsearchEnv();
|
|
97
|
+
const candidates = ["memsearch", "memsearch.exe", "memsearch.cmd", "memsearch.bat"];
|
|
98
|
+
const pathDirs = (env.PATH || "").split(pathSep()).filter(Boolean);
|
|
99
|
+
for (const dir of pathDirs) {
|
|
100
|
+
for (const cand of candidates) {
|
|
101
|
+
const full = path.join(dir, cand);
|
|
102
|
+
if (existsSync(full)) {
|
|
103
|
+
cliCache = full;
|
|
104
|
+
return cliCache;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
cliCache = null;
|
|
109
|
+
return cliCache;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Run the memsearch CLI; never rejects. Returns { err, stdout, stderr, skipped }. */
|
|
113
|
+
function runMemsearch(args, { timeout = MS_TIMEOUT, input } = {}) {
|
|
114
|
+
return new Promise((resolve) => {
|
|
115
|
+
const cli = resolveCli();
|
|
116
|
+
if (!cli) {
|
|
117
|
+
resolve({ err: new Error("memsearch CLI not installed"), stdout: "", stderr: "", skipped: true });
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
execFile(cli, args, { env: memsearchEnv(), timeout, maxBuffer: MAX_BUFFER, ...(input !== undefined ? { input } : {}) }, (err, stdout, stderr) => {
|
|
121
|
+
resolve({ err, stdout: stdout || "", stderr: stderr || "", skipped: false });
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function localDateParts(now = new Date()) {
|
|
127
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
128
|
+
return {
|
|
129
|
+
date: `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}`,
|
|
130
|
+
time: `${pad(now.getHours())}:${pad(now.getMinutes())}`,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function journalPath(memoryDir, now = new Date()) {
|
|
135
|
+
return path.join(memoryDir, `${localDateParts(now).date}.md`);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function memoryFiles(memoryDir) {
|
|
139
|
+
try {
|
|
140
|
+
return readdirSync(memoryDir).filter((f) => f.endsWith(".md")).sort();
|
|
141
|
+
} catch {
|
|
142
|
+
return [];
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function collectionArgs(config) {
|
|
147
|
+
return config.collection ? ["--collection", config.collection] : [];
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function truncate(text, max) {
|
|
151
|
+
if (text.length <= max) return text;
|
|
152
|
+
return `${text.slice(0, max)}...(truncated)`;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function textOf(message) {
|
|
156
|
+
if (!message || !Array.isArray(message.content)) return "";
|
|
157
|
+
return message.content
|
|
158
|
+
.filter((b) => b && b.type === "text" && typeof b.text === "string")
|
|
159
|
+
.map((b) => b.text)
|
|
160
|
+
.join("\n")
|
|
161
|
+
.trim();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Unwrap an event's message payload. */
|
|
165
|
+
function messageOf(data) {
|
|
166
|
+
if (data && typeof data === "object" && data.message && typeof data.message === "object") {
|
|
167
|
+
return data.message;
|
|
168
|
+
}
|
|
169
|
+
return data;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Blocks the harness injects into the conversation; never a real user prompt. */
|
|
173
|
+
const INJECTED_TEXT = /^\s*(<system-reminder>|Time sampled while preparing)/;
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* The last exchange of the session: the newest genuine user prompt plus the
|
|
177
|
+
* final assistant reply that follows it.
|
|
178
|
+
*
|
|
179
|
+
* Injected context reaches the session as ordinary `user/message` events, so
|
|
180
|
+
* `source.kind` decides what counts: only `kind: "user"` (and a message with no
|
|
181
|
+
* source at all) is a real prompt. Everything else — `plugin`, `runtime-context`,
|
|
182
|
+
* `time-context`, `skill-catalog`, `model-selection` — is skipped.
|
|
183
|
+
*/
|
|
184
|
+
function extractExchange(session) {
|
|
185
|
+
let userText = "";
|
|
186
|
+
let userSeq = -1;
|
|
187
|
+
const events = Array.isArray(session.events) ? session.events : [];
|
|
188
|
+
for (let i = 0; i < events.length; i++) {
|
|
189
|
+
const event = events[i];
|
|
190
|
+
if (!event || typeof event !== "object" || event.type !== "user/message") continue;
|
|
191
|
+
const source = event.data && event.data.source;
|
|
192
|
+
if (source && source.kind !== "user") continue; // injected context or our own status line
|
|
193
|
+
const text = textOf(messageOf(event.data));
|
|
194
|
+
if (!text || INJECTED_TEXT.test(text)) continue;
|
|
195
|
+
userText = text;
|
|
196
|
+
userSeq = typeof event.seq === "number" ? event.seq : i;
|
|
197
|
+
}
|
|
198
|
+
if (!userText) return { userText: "", assistantText: "", assistantSeq: undefined };
|
|
199
|
+
|
|
200
|
+
let assistantText = "";
|
|
201
|
+
let assistantSeq;
|
|
202
|
+
for (let i = events.length - 1; i >= 0; i--) {
|
|
203
|
+
const event = events[i];
|
|
204
|
+
if (!event || typeof event !== "object" || event.type !== "assistant/message") continue;
|
|
205
|
+
if (typeof event.seq === "number" && typeof userSeq === "number" && event.seq < userSeq) continue;
|
|
206
|
+
const text = textOf(messageOf(event.data));
|
|
207
|
+
if (!text) continue;
|
|
208
|
+
assistantText = text;
|
|
209
|
+
assistantSeq = event.seq;
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
return { userText, assistantText, assistantSeq };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
async function buildStatusText(config, memoryDir) {
|
|
216
|
+
const cliAvailable = Boolean(resolveCli());
|
|
217
|
+
let versionText = "n/a";
|
|
218
|
+
let providerText = "(no CLI)";
|
|
219
|
+
if (cliAvailable) {
|
|
220
|
+
const version = await runMemsearch(["--version"], { timeout: MS_STATUS_TIMEOUT });
|
|
221
|
+
const provider = await runMemsearch(["config", "get", "embedding.provider"], { timeout: MS_STATUS_TIMEOUT });
|
|
222
|
+
versionText = (version.stdout || version.stderr || "?").replace(/^memsearch,\s*/i, "").replace(/,\s*$/, "").trim();
|
|
223
|
+
providerText = provider.stdout.trim() || "(default)";
|
|
224
|
+
}
|
|
225
|
+
const files = memoryFiles(memoryDir);
|
|
226
|
+
let text = `[memsearch ${versionText}] embedding: ${providerText} | journal: ${memoryDir} | ${files.length} past memory file(s)`;
|
|
227
|
+
if (files.length > 0) {
|
|
228
|
+
text += ` (${files[0]} to ${files[files.length - 1]}). Use the memory-recall skill to search when the user's question could benefit from historical context.`;
|
|
229
|
+
} else {
|
|
230
|
+
text += ". No past memories yet; this session's exchanges will be saved automatically.";
|
|
231
|
+
}
|
|
232
|
+
if (!cliAvailable) {
|
|
233
|
+
text += " (memsearch CLI 未安装:本轮只写日记,索引 / 语义检索暂不可用。)";
|
|
234
|
+
}
|
|
235
|
+
return text;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function createStatusMessage(text) {
|
|
239
|
+
return {
|
|
240
|
+
id: randomUUID(),
|
|
241
|
+
role: "user",
|
|
242
|
+
content: [{ type: "text", text }],
|
|
243
|
+
source: { kind: "memory-recall", plugin: name, form: "snapshot", sections: [{ name, text }] },
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async function captureTurn(config, memoryDir, session, state) {
|
|
248
|
+
try {
|
|
249
|
+
const { userText, assistantText, assistantSeq } = extractExchange(session);
|
|
250
|
+
if (!userText || userText.length < config.captureMinPromptLength) return;
|
|
251
|
+
if (assistantSeq === undefined) return;
|
|
252
|
+
if (state.lastCaptured.get(session.id) === assistantSeq) return; // already captured
|
|
253
|
+
state.lastCaptured.set(session.id, assistantSeq);
|
|
254
|
+
|
|
255
|
+
let summary = `=== Final exchange, authoritative for outcome ===\n[User]: ${truncate(userText, config.maxCaptureChars)}\n[DSH]: ${truncate(assistantText || "(no text)", config.maxCaptureChars)}`;
|
|
256
|
+
|
|
257
|
+
if (config.summarizePlugin && resolveCli()) {
|
|
258
|
+
const res = await runMemsearch(["summarize", "--plugin", config.summarizePlugin, "--agent-name", "DSH"], { input: summary });
|
|
259
|
+
if (!res.err && res.stdout.trim()) summary = res.stdout.trim();
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
mkdirSync(memoryDir, { recursive: true });
|
|
263
|
+
const file = journalPath(memoryDir);
|
|
264
|
+
appendFileSync(file, `\n### ${localDateParts().time}\n${summary}\n`, "utf8");
|
|
265
|
+
|
|
266
|
+
if (config.indexAfterCapture && resolveCli()) {
|
|
267
|
+
runMemsearch(["index", memoryDir, ...collectionArgs(config)]).then((res) => {
|
|
268
|
+
if (res.err && !res.skipped) console.error(`[dsh-memsearch] index failed: ${(res.stderr || res.err.message).slice(0, 300)}`);
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
} catch (err) {
|
|
272
|
+
console.error(`[dsh-memsearch] captureTurn failed: ${err && err.message ? err.message : String(err)}`);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function apply(ctx, config) {
|
|
277
|
+
if (!config || !config.enabled) return;
|
|
278
|
+
const memoryDir = resolveMemoryDir(config.memoryDir);
|
|
279
|
+
const state = {
|
|
280
|
+
statusInjected: new WeakSet(),
|
|
281
|
+
sessionIndexed: new WeakSet(),
|
|
282
|
+
lastCaptured: new Map(),
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
ctx.on("agent/pre-step", async ({ agent, messages, step, signal }, next) => {
|
|
286
|
+
try {
|
|
287
|
+
const decision = await next();
|
|
288
|
+
if (!decision || decision.kind === "reject" || (signal && signal.aborted) || step !== 1) return decision;
|
|
289
|
+
const session = agent && agent.session;
|
|
290
|
+
if (!session) return decision;
|
|
291
|
+
|
|
292
|
+
if (!state.sessionIndexed.has(session) && memoryFiles(memoryDir).length > 0 && resolveCli()) {
|
|
293
|
+
state.sessionIndexed.add(session);
|
|
294
|
+
runMemsearch(["index", memoryDir, ...collectionArgs(config)]).then((res) => {
|
|
295
|
+
if (res.err && !res.skipped) console.error(`[dsh-memsearch] session index failed: ${(res.stderr || res.err.message).slice(0, 300)}`);
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
if (config.injectSessionStatus && !state.statusInjected.has(session)) {
|
|
300
|
+
state.statusInjected.add(session);
|
|
301
|
+
const statusText = await buildStatusText(config, memoryDir);
|
|
302
|
+
return { kind: "enter", messages: [createStatusMessage(statusText), ...(decision.messages || [])] };
|
|
303
|
+
}
|
|
304
|
+
return decision;
|
|
305
|
+
} catch (err) {
|
|
306
|
+
// 兜底:插件异常绝不拖垮主进程,放行原决策。
|
|
307
|
+
console.error(`[dsh-memsearch] pre-step failed: ${err && err.message ? err.message : String(err)}`);
|
|
308
|
+
try {
|
|
309
|
+
return await next();
|
|
310
|
+
} catch {
|
|
311
|
+
return { kind: "enter", messages: [] };
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}, { prepend: true });
|
|
315
|
+
|
|
316
|
+
if (config.capture) {
|
|
317
|
+
ctx.on("session/event", (session, event) => {
|
|
318
|
+
try {
|
|
319
|
+
if (!event || event.type !== "turn/end") return;
|
|
320
|
+
if (session && session.events) captureTurn(config, memoryDir, session, state);
|
|
321
|
+
} catch (err) {
|
|
322
|
+
console.error(`[dsh-memsearch] session/event failed: ${err && err.message ? err.message : String(err)}`);
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
export { Config, apply, extractExchange, inject, name };
|
package/locale/en.json
ADDED
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,63 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-memsearch-community",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "Automatic local semantic memory for DeepSeek Harness: journals every turn, indexes it with memsearch, and lets the agent recall earlier sessions. Community fork that captures only genuine user prompts.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "lib/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./lib/index.js",
|
|
9
|
+
"./locale/*.json": "./locale/*.json",
|
|
10
|
+
"./icon": "./icon.svg"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"lib",
|
|
14
|
+
"cordis.patch.yml",
|
|
15
|
+
"locale",
|
|
16
|
+
"docs",
|
|
17
|
+
"icon.svg",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"dsh": {
|
|
22
|
+
"bundle": {
|
|
23
|
+
"patch": "./cordis.patch.yml"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@deepseek-ai/schemastery": "^3.18.1"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"@deepseek-ai/cordis": "^4.0.1",
|
|
31
|
+
"@deepseek-ai/dsh-agent": ">=0.1.0-rc.6 <0.3.0"
|
|
32
|
+
},
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=18"
|
|
35
|
+
},
|
|
36
|
+
"keywords": [
|
|
37
|
+
"dsh",
|
|
38
|
+
"dsh-plugin",
|
|
39
|
+
"deepseek-harness",
|
|
40
|
+
"cordis",
|
|
41
|
+
"memsearch",
|
|
42
|
+
"memory",
|
|
43
|
+
"semantic-search",
|
|
44
|
+
"rag"
|
|
45
|
+
],
|
|
46
|
+
"license": "MIT",
|
|
47
|
+
"author": {
|
|
48
|
+
"name": "winters410",
|
|
49
|
+
"url": "https://github.com/winters410"
|
|
50
|
+
},
|
|
51
|
+
"repository": {
|
|
52
|
+
"type": "git",
|
|
53
|
+
"url": "https://github.com/winters410/dsh-memsearch-community.git"
|
|
54
|
+
},
|
|
55
|
+
"bugs": {
|
|
56
|
+
"url": "https://github.com/winters410/dsh-memsearch-community/issues"
|
|
57
|
+
},
|
|
58
|
+
"homepage": "https://github.com/winters410/dsh-memsearch-community#readme",
|
|
59
|
+
"publishConfig": {
|
|
60
|
+
"access": "public",
|
|
61
|
+
"registry": "https://registry.npmjs.org/"
|
|
62
|
+
}
|
|
63
|
+
}
|