@hippodid/openclaw-plugin 1.0.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/LICENSE +190 -0
- package/README.md +141 -0
- package/openclaw.plugin.json +57 -0
- package/package.json +38 -0
- package/src/file-sync.ts +250 -0
- package/src/hippodid-client.ts +283 -0
- package/src/hooks/auto-capture.ts +58 -0
- package/src/hooks/auto-recall.ts +67 -0
- package/src/hooks/memory-flush.ts +22 -0
- package/src/hooks/session-lifecycle.ts +42 -0
- package/src/index.ts +220 -0
- package/src/tier-manager.ts +90 -0
- package/src/types.ts +160 -0
- package/src/workspace-detector.ts +104 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding any notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2026 HippoDid
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# HippoDid — Cloud Character Memory for OpenClaw
|
|
2
|
+
|
|
3
|
+
> Your MEMORY.md survives context compaction. Zero-config cloud sync with structured character memory.
|
|
4
|
+
|
|
5
|
+
## The Problem
|
|
6
|
+
|
|
7
|
+
OpenClaw's own docs warn: **memory dies on context compaction.** Your agent accumulates context over a session — preferences, project knowledge, debugging history. Then compaction triggers, and that context is gone. Next session starts from scratch.
|
|
8
|
+
|
|
9
|
+
HippoDid syncs your memory files to cloud **before** compaction destroys them. Next session, they're back — automatically.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
openclaw plugins install @hippodid/openclaw-plugin
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Add to your `openclaw.json`:
|
|
18
|
+
|
|
19
|
+
```json
|
|
20
|
+
{
|
|
21
|
+
"plugins": {
|
|
22
|
+
"hippodid": {
|
|
23
|
+
"enabled": true,
|
|
24
|
+
"apiKey": "hd_sk_...",
|
|
25
|
+
"characterId": "my-dev-agent"
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Restart the gateway:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
openclaw gateway restart
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## How It Works
|
|
38
|
+
|
|
39
|
+
```
|
|
40
|
+
1. Install → Plugin auto-detects your workspace memory directory
|
|
41
|
+
2. File watcher → Monitors MEMORY.md + memory/*.md for changes
|
|
42
|
+
3. Pre-compaction flush → Syncs to cloud BEFORE context compaction (the critical hook)
|
|
43
|
+
4. Session start → Hydrates files from cloud so memory survives across sessions
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
The plugin runs as a **non-slot plugin** — it doesn't replace your existing memory backend. It adds cloud persistence on top of whatever memory system you already use.
|
|
47
|
+
|
|
48
|
+
## Import Existing Memory
|
|
49
|
+
|
|
50
|
+
Already have memory files from previous sessions?
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
openclaw run hippodid:import --workspace ~/.openclaw/workspace
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
This uploads all `MEMORY.md` + `memory/*.md` files to your HippoDid character in one pass.
|
|
57
|
+
|
|
58
|
+
## CLI Commands
|
|
59
|
+
|
|
60
|
+
| Command | Description |
|
|
61
|
+
|---------|-------------|
|
|
62
|
+
| `hippodid:status` | Show tier, sync status, watched paths |
|
|
63
|
+
| `hippodid:sync` | Trigger immediate sync of all watched files |
|
|
64
|
+
| `hippodid:import` | Import existing workspace memory |
|
|
65
|
+
|
|
66
|
+
## HippoDid vs Mem0 vs memory-lancedb
|
|
67
|
+
|
|
68
|
+
| Feature | HippoDid | Mem0 Plugin | memory-lancedb |
|
|
69
|
+
|---------|----------|-------------|----------------|
|
|
70
|
+
| MEMORY.md sync | Bidirectional cloud sync | Ignores MEMORY.md entirely | Separate vector store |
|
|
71
|
+
| Import existing memory | One command, full history | Start from zero | Start from zero |
|
|
72
|
+
| Survives compaction | Pre-compaction flush hook | Partial (AUDN capture only) | Local vector DB persists |
|
|
73
|
+
| Cross-machine sync | Cloud-based | Mem0 cloud | Local only |
|
|
74
|
+
| Data portability | Export to Markdown anytime | Locked in Mem0 format | LanceDB binary format |
|
|
75
|
+
| Structured character model | Categories, profiles, decay | Flat user_id memories | Flat key-value |
|
|
76
|
+
| Free tier cost | $0 (file sync only, no AI) | Requires OPENAI_API_KEY | Requires embedding API key |
|
|
77
|
+
| Multi-agent isolation | Character-scoped by design | Known bleed bug (#3998) | Agent scope option |
|
|
78
|
+
|
|
79
|
+
## Configuration Reference
|
|
80
|
+
|
|
81
|
+
```json
|
|
82
|
+
{
|
|
83
|
+
"plugins": {
|
|
84
|
+
"hippodid": {
|
|
85
|
+
"enabled": true,
|
|
86
|
+
"apiKey": "hd_sk_...",
|
|
87
|
+
"characterId": "my-dev-agent",
|
|
88
|
+
"baseUrl": "https://api.hippodid.com",
|
|
89
|
+
"syncIntervalSeconds": 300,
|
|
90
|
+
"autoRecall": false,
|
|
91
|
+
"autoCapture": false,
|
|
92
|
+
"additionalPaths": [
|
|
93
|
+
{ "path": "./notes/project.md", "label": "project-notes" }
|
|
94
|
+
]
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
| Option | Type | Default | Description |
|
|
101
|
+
|--------|------|---------|-------------|
|
|
102
|
+
| `apiKey` | string | *required* | HippoDid API key (`hd_sk_...`) |
|
|
103
|
+
| `characterId` | string | *required* | Target character ID |
|
|
104
|
+
| `baseUrl` | string | `https://api.hippodid.com` | API endpoint |
|
|
105
|
+
| `syncIntervalSeconds` | number | `300` | Debounce interval for file sync (min: 60s) |
|
|
106
|
+
| `autoRecall` | boolean | `false` | Inject relevant memories before each turn (paid tier) |
|
|
107
|
+
| `autoCapture` | boolean | `false` | Extract and store facts after each turn (paid tier) |
|
|
108
|
+
| `additionalPaths` | array | `[]` | Extra files/directories to watch |
|
|
109
|
+
|
|
110
|
+
## Free vs Paid Tiers
|
|
111
|
+
|
|
112
|
+
**Free tier** (no credit card):
|
|
113
|
+
- File watcher with SHA-256 diff sync
|
|
114
|
+
- Pre-compaction flush (the critical feature)
|
|
115
|
+
- Session-start hydration from cloud
|
|
116
|
+
- Manual sync and import commands
|
|
117
|
+
|
|
118
|
+
**Paid tiers** add:
|
|
119
|
+
- **Auto-Recall**: Relevant memories injected as context before each agent turn
|
|
120
|
+
- **Auto-Capture**: Facts automatically extracted from conversations via AUDN pipeline
|
|
121
|
+
- Lower sync interval minimums
|
|
122
|
+
|
|
123
|
+
## Workspace Auto-Detection
|
|
124
|
+
|
|
125
|
+
The plugin automatically finds your memory files by searching:
|
|
126
|
+
|
|
127
|
+
1. `$OPENCLAW_WORKSPACE/memory/` and `$OPENCLAW_WORKSPACE/MEMORY.md`
|
|
128
|
+
2. `~/.openclaw/workspace/memory/` and `~/.openclaw/workspace/MEMORY.md`
|
|
129
|
+
3. Current working directory `./memory/` and `./MEMORY.md`
|
|
130
|
+
|
|
131
|
+
Add `additionalPaths` in config to watch extra files or directories.
|
|
132
|
+
|
|
133
|
+
## Requirements
|
|
134
|
+
|
|
135
|
+
- OpenClaw 2026.1.x+
|
|
136
|
+
- Node.js 22+
|
|
137
|
+
- HippoDid account (free at hippodid.dev)
|
|
138
|
+
|
|
139
|
+
## License
|
|
140
|
+
|
|
141
|
+
Apache 2.0
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "hippodid",
|
|
3
|
+
"name": "HippoDid — Cloud Character Memory",
|
|
4
|
+
"description": "Cloud sync for MEMORY.md with structured character memory. Survives context compaction.",
|
|
5
|
+
"configSchema": {
|
|
6
|
+
"type": "object",
|
|
7
|
+
"additionalProperties": false,
|
|
8
|
+
"properties": {
|
|
9
|
+
"apiKey": {
|
|
10
|
+
"type": "string",
|
|
11
|
+
"description": "HippoDid API key (hd_sk_...)"
|
|
12
|
+
},
|
|
13
|
+
"characterId": {
|
|
14
|
+
"type": "string",
|
|
15
|
+
"description": "Target character ID or name"
|
|
16
|
+
},
|
|
17
|
+
"baseUrl": {
|
|
18
|
+
"type": "string",
|
|
19
|
+
"default": "https://api.hippodid.com"
|
|
20
|
+
},
|
|
21
|
+
"syncIntervalSeconds": {
|
|
22
|
+
"type": "number",
|
|
23
|
+
"default": 300,
|
|
24
|
+
"minimum": 60
|
|
25
|
+
},
|
|
26
|
+
"autoRecall": {
|
|
27
|
+
"type": "boolean",
|
|
28
|
+
"default": false
|
|
29
|
+
},
|
|
30
|
+
"autoCapture": {
|
|
31
|
+
"type": "boolean",
|
|
32
|
+
"default": false
|
|
33
|
+
},
|
|
34
|
+
"additionalPaths": {
|
|
35
|
+
"type": "array",
|
|
36
|
+
"items": {
|
|
37
|
+
"type": "object",
|
|
38
|
+
"properties": {
|
|
39
|
+
"path": { "type": "string" },
|
|
40
|
+
"label": { "type": "string" }
|
|
41
|
+
},
|
|
42
|
+
"required": ["path"]
|
|
43
|
+
},
|
|
44
|
+
"default": []
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"required": ["apiKey", "characterId"]
|
|
48
|
+
},
|
|
49
|
+
"uiHints": {
|
|
50
|
+
"apiKey": { "label": "HippoDid API Key", "sensitive": true, "placeholder": "hd_sk_..." },
|
|
51
|
+
"characterId": { "label": "Character ID", "placeholder": "my-dev-agent" },
|
|
52
|
+
"baseUrl": { "label": "API Base URL", "advanced": true },
|
|
53
|
+
"syncIntervalSeconds": { "label": "Sync Interval (seconds)", "advanced": true },
|
|
54
|
+
"autoRecall": { "label": "Auto-Recall (Paid tier)", "help": "Inject relevant memories before each turn" },
|
|
55
|
+
"autoCapture": { "label": "Auto-Capture (Paid tier)", "help": "Extract and store facts after each turn" }
|
|
56
|
+
}
|
|
57
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hippodid/openclaw-plugin",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Cloud sync for MEMORY.md with structured character memory. Survives context compaction.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/SameThoughts/hippodid-openclaw-plugin.git"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"openclaw",
|
|
12
|
+
"openclaw-plugin",
|
|
13
|
+
"memory",
|
|
14
|
+
"character-memory",
|
|
15
|
+
"cloud-sync",
|
|
16
|
+
"hippodid",
|
|
17
|
+
"ai-memory"
|
|
18
|
+
],
|
|
19
|
+
"main": "src/index.ts",
|
|
20
|
+
"files": [
|
|
21
|
+
"src/",
|
|
22
|
+
"openclaw.plugin.json",
|
|
23
|
+
"LICENSE",
|
|
24
|
+
"README.md"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"typecheck": "tsc --noEmit",
|
|
28
|
+
"test": "vitest run",
|
|
29
|
+
"test:watch": "vitest",
|
|
30
|
+
"test:coverage": "vitest run --coverage"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/node": "^22.0.0",
|
|
34
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
35
|
+
"typescript": "^5.7.0",
|
|
36
|
+
"vitest": "^3.0.0"
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/file-sync.ts
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { readFile, writeFile, readdir, stat } from 'node:fs/promises';
|
|
3
|
+
import { watch, type FSWatcher } from 'node:fs';
|
|
4
|
+
import { resolve, join, extname } from 'node:path';
|
|
5
|
+
import type { HippoDidClient } from './hippodid-client.js';
|
|
6
|
+
import type { PluginConfig, FileTrackingEntry, WatchPath } from './types.js';
|
|
7
|
+
|
|
8
|
+
export interface FileSync {
|
|
9
|
+
start(): void;
|
|
10
|
+
stop(): void;
|
|
11
|
+
flushNow(): Promise<{ synced: number; changed: number }>;
|
|
12
|
+
hydrateFromCloud(): Promise<number>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface Logger {
|
|
16
|
+
info(message: string): void;
|
|
17
|
+
warn(message: string): void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function createFileSync(
|
|
21
|
+
client: HippoDidClient,
|
|
22
|
+
config: PluginConfig,
|
|
23
|
+
watchPaths: WatchPath[],
|
|
24
|
+
logger: Logger,
|
|
25
|
+
effectiveSyncIntervalSeconds?: number,
|
|
26
|
+
): FileSync {
|
|
27
|
+
const syncIntervalMs = (effectiveSyncIntervalSeconds ?? config.syncIntervalSeconds) * 1000;
|
|
28
|
+
const tracking = new Map<string, FileTrackingEntry>();
|
|
29
|
+
const watchers: FSWatcher[] = [];
|
|
30
|
+
let debounceTimer: ReturnType<typeof setTimeout> | null = null;
|
|
31
|
+
let pendingChanges = new Set<string>();
|
|
32
|
+
|
|
33
|
+
async function computeHash(filePath: string): Promise<string> {
|
|
34
|
+
const content = await readFile(filePath);
|
|
35
|
+
return createHash('sha256').update(content).digest('hex');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function toBase64(buffer: Buffer): string {
|
|
39
|
+
return buffer.toString('base64');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function fromBase64(content: string): string {
|
|
43
|
+
return Buffer.from(content, 'base64').toString('utf-8');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function resolveFilesForPath(wp: WatchPath): Promise<string[]> {
|
|
47
|
+
try {
|
|
48
|
+
const s = await stat(wp.path);
|
|
49
|
+
if (s.isDirectory()) {
|
|
50
|
+
const entries = await readdir(wp.path);
|
|
51
|
+
return entries
|
|
52
|
+
.filter((e) => extname(e) === '.md')
|
|
53
|
+
.map((e) => resolve(join(wp.path, e)));
|
|
54
|
+
}
|
|
55
|
+
return [resolve(wp.path)];
|
|
56
|
+
} catch {
|
|
57
|
+
return [];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function resolveAllFiles(): Promise<
|
|
62
|
+
Array<{ filePath: string; label: string }>
|
|
63
|
+
> {
|
|
64
|
+
const files: Array<{ filePath: string; label: string }> = [];
|
|
65
|
+
const seen = new Set<string>();
|
|
66
|
+
|
|
67
|
+
for (const wp of watchPaths) {
|
|
68
|
+
const resolved = await resolveFilesForPath(wp);
|
|
69
|
+
for (const filePath of resolved) {
|
|
70
|
+
if (!seen.has(filePath)) {
|
|
71
|
+
seen.add(filePath);
|
|
72
|
+
files.push({ filePath, label: wp.label });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return files;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async function syncFile(
|
|
80
|
+
filePath: string,
|
|
81
|
+
label: string,
|
|
82
|
+
): Promise<boolean> {
|
|
83
|
+
try {
|
|
84
|
+
const content = await readFile(filePath);
|
|
85
|
+
const hash = createHash('sha256').update(content).digest('hex');
|
|
86
|
+
const existing = tracking.get(filePath);
|
|
87
|
+
|
|
88
|
+
if (existing && existing.hash === hash) {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const base64Content = toBase64(content);
|
|
93
|
+
const result = await client.syncFile(
|
|
94
|
+
config.characterId,
|
|
95
|
+
filePath,
|
|
96
|
+
label,
|
|
97
|
+
base64Content,
|
|
98
|
+
hash,
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
if (result.ok) {
|
|
102
|
+
tracking.set(filePath, { hash, lastSyncedAt: new Date() });
|
|
103
|
+
return result.value.changed;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
logger.warn(`hippodid: sync failed for ${filePath}: ${result.error.message}`);
|
|
107
|
+
return false;
|
|
108
|
+
} catch (e) {
|
|
109
|
+
logger.warn(
|
|
110
|
+
`hippodid: error syncing ${filePath}: ${e instanceof Error ? e.message : 'unknown'}`,
|
|
111
|
+
);
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async function runSync(): Promise<{ synced: number; changed: number }> {
|
|
117
|
+
const files = await resolveAllFiles();
|
|
118
|
+
let synced = 0;
|
|
119
|
+
let changed = 0;
|
|
120
|
+
|
|
121
|
+
for (const { filePath, label } of files) {
|
|
122
|
+
const wasChanged = await syncFile(filePath, label);
|
|
123
|
+
synced++;
|
|
124
|
+
if (wasChanged) changed++;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
logger.info(
|
|
128
|
+
`hippodid: synced ${synced} files (${changed} changed, ${synced - changed} unchanged)`,
|
|
129
|
+
);
|
|
130
|
+
return { synced, changed };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function scheduleDebouncedSync(): void {
|
|
134
|
+
if (debounceTimer) {
|
|
135
|
+
clearTimeout(debounceTimer);
|
|
136
|
+
}
|
|
137
|
+
debounceTimer = setTimeout(
|
|
138
|
+
() => {
|
|
139
|
+
debounceTimer = null;
|
|
140
|
+
runSync().catch((e) => {
|
|
141
|
+
logger.warn(
|
|
142
|
+
`hippodid: debounced sync error: ${e instanceof Error ? e.message : 'unknown'}`,
|
|
143
|
+
);
|
|
144
|
+
});
|
|
145
|
+
},
|
|
146
|
+
syncIntervalMs,
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function onFileChange(filePath: string): void {
|
|
151
|
+
pendingChanges.add(filePath);
|
|
152
|
+
scheduleDebouncedSync();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
start(): void {
|
|
157
|
+
for (const wp of watchPaths) {
|
|
158
|
+
try {
|
|
159
|
+
const watcher = watch(wp.path, { persistent: false }, (_event, filename) => {
|
|
160
|
+
if (filename && extname(filename) === '.md') {
|
|
161
|
+
const fullPath = resolve(
|
|
162
|
+
join(
|
|
163
|
+
wp.path.endsWith('.md') ? resolve(wp.path, '..') : wp.path,
|
|
164
|
+
filename,
|
|
165
|
+
),
|
|
166
|
+
);
|
|
167
|
+
onFileChange(fullPath);
|
|
168
|
+
} else if (wp.path.endsWith('.md')) {
|
|
169
|
+
onFileChange(resolve(wp.path));
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
watcher.on('error', (err) => {
|
|
173
|
+
logger.warn(
|
|
174
|
+
`hippodid: watcher error for ${wp.path}: ${err instanceof Error ? err.message : 'unknown'}`,
|
|
175
|
+
);
|
|
176
|
+
});
|
|
177
|
+
watchers.push(watcher);
|
|
178
|
+
} catch (e) {
|
|
179
|
+
logger.warn(
|
|
180
|
+
`hippodid: failed to watch ${wp.path}: ${e instanceof Error ? e.message : 'unknown'}`,
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
scheduleDebouncedSync();
|
|
186
|
+
},
|
|
187
|
+
|
|
188
|
+
stop(): void {
|
|
189
|
+
for (const w of watchers) {
|
|
190
|
+
w.close();
|
|
191
|
+
}
|
|
192
|
+
watchers.length = 0;
|
|
193
|
+
|
|
194
|
+
if (debounceTimer) {
|
|
195
|
+
clearTimeout(debounceTimer);
|
|
196
|
+
debounceTimer = null;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
pendingChanges.clear();
|
|
200
|
+
},
|
|
201
|
+
|
|
202
|
+
async flushNow(): Promise<{ synced: number; changed: number }> {
|
|
203
|
+
if (debounceTimer) {
|
|
204
|
+
clearTimeout(debounceTimer);
|
|
205
|
+
debounceTimer = null;
|
|
206
|
+
}
|
|
207
|
+
return runSync();
|
|
208
|
+
},
|
|
209
|
+
|
|
210
|
+
async hydrateFromCloud(): Promise<number> {
|
|
211
|
+
const files = await resolveAllFiles();
|
|
212
|
+
let hydrated = 0;
|
|
213
|
+
|
|
214
|
+
for (const { filePath } of files) {
|
|
215
|
+
try {
|
|
216
|
+
const result = await client.getLatestSync(
|
|
217
|
+
config.characterId,
|
|
218
|
+
filePath,
|
|
219
|
+
);
|
|
220
|
+
if (!result.ok) {
|
|
221
|
+
logger.warn(
|
|
222
|
+
`hippodid: hydration lookup failed for ${filePath}: ${result.error.message}`,
|
|
223
|
+
);
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (result.value === null) {
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const cloudContent = fromBase64(result.value.fileContent);
|
|
232
|
+
await writeFile(filePath, cloudContent, 'utf-8');
|
|
233
|
+
|
|
234
|
+
const hash = createHash('sha256')
|
|
235
|
+
.update(Buffer.from(cloudContent))
|
|
236
|
+
.digest('hex');
|
|
237
|
+
tracking.set(filePath, { hash, lastSyncedAt: new Date() });
|
|
238
|
+
hydrated++;
|
|
239
|
+
} catch (e) {
|
|
240
|
+
logger.warn(
|
|
241
|
+
`hippodid: hydration error for ${filePath}: ${e instanceof Error ? e.message : 'unknown'}`,
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
logger.info(`hippodid: hydrated ${hydrated} files from cloud`);
|
|
247
|
+
return hydrated;
|
|
248
|
+
},
|
|
249
|
+
};
|
|
250
|
+
}
|