@hyperspell/openclaw-hyperspell 0.12.0 → 0.13.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 +37 -1
- package/dist/client.js +341 -0
- package/dist/commands/setup.js +568 -0
- package/dist/commands/slash.js +159 -0
- package/dist/config.js +349 -0
- package/{graph/cron.ts → dist/graph/cron.js} +11 -15
- package/dist/graph/index.js +4 -0
- package/dist/graph/ops.js +221 -0
- package/dist/graph/state.js +68 -0
- package/dist/graph/tools.js +87 -0
- package/dist/hooks/auto-context.js +199 -0
- package/dist/hooks/auto-trace.js +143 -0
- package/dist/hooks/emotional-state.js +155 -0
- package/dist/hooks/memory-sync.js +54 -0
- package/dist/hooks/startup-orientation.js +236 -0
- package/dist/index.js +132 -0
- package/dist/lib/browser.js +29 -0
- package/dist/lib/sender.js +173 -0
- package/dist/lib/voice-id.js +22 -0
- package/dist/logger.js +32 -0
- package/dist/sync/markdown.js +151 -0
- package/dist/tools/remember.js +97 -0
- package/dist/tools/search.js +87 -0
- package/openclaw.plugin.json +25 -1
- package/package.json +7 -12
- package/client.ts +0 -566
- package/commands/setup.ts +0 -673
- package/commands/slash.ts +0 -198
- package/config.test.ts +0 -202
- package/config.ts +0 -497
- package/graph/index.ts +0 -5
- package/graph/ops.ts +0 -259
- package/graph/state.ts +0 -79
- package/graph/tools.ts +0 -117
- package/hooks/auto-context.ts +0 -272
- package/hooks/auto-trace.test.ts +0 -81
- package/hooks/auto-trace.ts +0 -197
- package/hooks/emotional-state.test.ts +0 -160
- package/hooks/emotional-state.ts +0 -179
- package/hooks/memory-sync.ts +0 -65
- package/index.ts +0 -152
- package/lib/browser.ts +0 -31
- package/lib/sender.test.ts +0 -234
- package/lib/sender.ts +0 -234
- package/lib/voice-id.ts +0 -39
- package/logger.ts +0 -41
- package/sync/markdown.ts +0 -186
- package/tools/remember.ts +0 -132
- package/tools/search.ts +0 -131
- package/types/openclaw.d.ts +0 -76
package/lib/sender.ts
DELETED
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
CanReadScope,
|
|
3
|
-
HyperspellConfig,
|
|
4
|
-
Role,
|
|
5
|
-
ScopeName,
|
|
6
|
-
} from "../config.ts"
|
|
7
|
-
import { normalizeScope } from "../config.ts"
|
|
8
|
-
import { log } from "../logger.ts"
|
|
9
|
-
import { getVoiceIdentifier } from "./voice-id.ts"
|
|
10
|
-
|
|
11
|
-
export interface ResolvedUser {
|
|
12
|
-
userId: string
|
|
13
|
-
name: string
|
|
14
|
-
context?: string
|
|
15
|
-
/** Profile-level role override from senderMap. Falls back to scoping.users[userId].role. */
|
|
16
|
-
role?: string
|
|
17
|
-
/** True if the sender was matched in senderMap; false if falling back to sharedUserId */
|
|
18
|
-
resolved: boolean
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function matchFromSenderMap(
|
|
22
|
-
ctx: Record<string, unknown> | undefined,
|
|
23
|
-
cfg: HyperspellConfig,
|
|
24
|
-
): ResolvedUser | undefined {
|
|
25
|
-
const multiUser = cfg.multiUser
|
|
26
|
-
if (!multiUser) {
|
|
27
|
-
return cfg.userId
|
|
28
|
-
? { userId: cfg.userId, name: cfg.userId, resolved: true }
|
|
29
|
-
: undefined
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// Try direct senderId lookup (slash command contexts)
|
|
33
|
-
const senderId =
|
|
34
|
-
(ctx?.senderId as string) ??
|
|
35
|
-
(ctx?.requesterSenderId as string) ??
|
|
36
|
-
undefined
|
|
37
|
-
if (senderId && multiUser.senderMap[senderId]) {
|
|
38
|
-
const profile = multiUser.senderMap[senderId]
|
|
39
|
-
log.debug(`sender resolved via senderId: ${senderId} -> ${profile.userId}`)
|
|
40
|
-
return { ...profile, resolved: true }
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// Try sessionKey substring matching (longest-first to avoid partial matches)
|
|
44
|
-
const sessionKey = ctx?.sessionKey as string | undefined
|
|
45
|
-
if (sessionKey) {
|
|
46
|
-
const sortedEntries = Object.entries(multiUser.senderMap).sort(
|
|
47
|
-
([a], [b]) => b.length - a.length,
|
|
48
|
-
)
|
|
49
|
-
for (const [handle, profile] of sortedEntries) {
|
|
50
|
-
if (sessionKey.includes(handle)) {
|
|
51
|
-
log.debug(
|
|
52
|
-
`sender resolved via sessionKey: ${handle} -> ${profile.userId}`,
|
|
53
|
-
)
|
|
54
|
-
return { ...profile, resolved: true }
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// Fallback: use sharedUserId for unknown senders
|
|
60
|
-
log.debug("sender unresolved, falling back to sharedUserId")
|
|
61
|
-
return {
|
|
62
|
-
userId: multiUser.sharedUserId,
|
|
63
|
-
name: multiUser.sharedUserId,
|
|
64
|
-
resolved: false,
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Synchronous sender resolution from sessionKey + senderMap. Use this for
|
|
70
|
-
* hooks and tools that don't receive audio — the voice-ID path is skipped.
|
|
71
|
-
*/
|
|
72
|
-
export function resolveUser(
|
|
73
|
-
ctx: Record<string, unknown> | undefined,
|
|
74
|
-
cfg: HyperspellConfig,
|
|
75
|
-
): ResolvedUser | undefined {
|
|
76
|
-
return matchFromSenderMap(ctx, cfg)
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Asynchronous sender resolution. Checks voice-ID first (if enabled and
|
|
81
|
-
* audio is in ctx), then falls back to the synchronous path. Callers that
|
|
82
|
-
* never receive audio should use `resolveUser` to avoid unnecessary async
|
|
83
|
-
* overhead.
|
|
84
|
-
*/
|
|
85
|
-
export async function resolveUserAsync(
|
|
86
|
-
ctx: Record<string, unknown> | undefined,
|
|
87
|
-
cfg: HyperspellConfig,
|
|
88
|
-
): Promise<ResolvedUser | undefined> {
|
|
89
|
-
const voiceCfg = cfg.multiUser?.scoping?.voiceId
|
|
90
|
-
const audio = ctx?.audio as Buffer | string | undefined
|
|
91
|
-
if (voiceCfg?.enabled && audio && cfg.multiUser) {
|
|
92
|
-
try {
|
|
93
|
-
const identifier = getVoiceIdentifier(cfg)
|
|
94
|
-
const result = await identifier.identify(audio)
|
|
95
|
-
const threshold = voiceCfg.confidenceThreshold ?? 0.7
|
|
96
|
-
if (result && result.confidence >= threshold) {
|
|
97
|
-
// Find profile for the voice-identified userId
|
|
98
|
-
for (const profile of Object.values(cfg.multiUser.senderMap)) {
|
|
99
|
-
if (profile.userId === result.userId) {
|
|
100
|
-
log.debug(
|
|
101
|
-
`sender resolved via voice: ${result.userId} (conf=${result.confidence.toFixed(2)})`,
|
|
102
|
-
)
|
|
103
|
-
return { ...profile, resolved: true }
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
} catch (err) {
|
|
108
|
-
log.error("voice-id identification failed", err)
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
return matchFromSenderMap(ctx, cfg)
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Get all unique userIds from the multiUser config (for knowledge graph scanning).
|
|
116
|
-
*/
|
|
117
|
-
export function getAllUserIds(cfg: HyperspellConfig): string[] {
|
|
118
|
-
if (!cfg.multiUser) {
|
|
119
|
-
return cfg.userId ? [cfg.userId] : []
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const userIds = new Set<string>()
|
|
123
|
-
for (const profile of Object.values(cfg.multiUser.senderMap)) {
|
|
124
|
-
userIds.add(profile.userId)
|
|
125
|
-
}
|
|
126
|
-
userIds.add(cfg.multiUser.sharedUserId)
|
|
127
|
-
return [...userIds]
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Resolve the Role for a user. Looks first at the profile-level `role`
|
|
132
|
-
* override, then at `scoping.users[userId].role`. Returns undefined if
|
|
133
|
-
* scoping is disabled or the user has no role assignment.
|
|
134
|
-
*/
|
|
135
|
-
export function resolveRole(
|
|
136
|
-
user: ResolvedUser | undefined,
|
|
137
|
-
cfg: HyperspellConfig,
|
|
138
|
-
): Role | undefined {
|
|
139
|
-
const scoping = cfg.multiUser?.scoping
|
|
140
|
-
if (!scoping || !user) return undefined
|
|
141
|
-
const roleName = user.role ?? scoping.users[user.userId]?.role
|
|
142
|
-
if (!roleName) return undefined
|
|
143
|
-
return scoping.roles[roleName]
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Readable scopes for this user. If scoping is disabled, returns ["*"] so
|
|
148
|
-
* `buildScopeFilter` produces no filter and PR #6 behavior is preserved.
|
|
149
|
-
*/
|
|
150
|
-
export function getCanReadScopes(
|
|
151
|
-
user: ResolvedUser | undefined,
|
|
152
|
-
cfg: HyperspellConfig,
|
|
153
|
-
): CanReadScope[] {
|
|
154
|
-
if (!cfg.multiUser?.scoping) return ["*"]
|
|
155
|
-
const role = resolveRole(user, cfg)
|
|
156
|
-
return role?.canRead ?? []
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Default write scope for this user — explicit param > role default > global default.
|
|
161
|
-
*/
|
|
162
|
-
export function getDefaultWriteScope(
|
|
163
|
-
user: ResolvedUser | undefined,
|
|
164
|
-
cfg: HyperspellConfig,
|
|
165
|
-
): ScopeName {
|
|
166
|
-
const role = resolveRole(user, cfg)
|
|
167
|
-
return (
|
|
168
|
-
role?.defaultWriteScope ??
|
|
169
|
-
cfg.multiUser?.scoping?.defaultScope ??
|
|
170
|
-
"private"
|
|
171
|
-
)
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Build a MongoDB-style metadata filter for Hyperspell's `options.filter`.
|
|
176
|
-
*
|
|
177
|
-
* Contract:
|
|
178
|
-
* - `["*"]` (wildcard) → returns `undefined` (no filter).
|
|
179
|
-
* - `[]` (empty) → returns a **match-nothing** filter, NOT `undefined`. An
|
|
180
|
-
* empty `canRead` is a deliberate "no access" signal and must not silently
|
|
181
|
-
* return all results.
|
|
182
|
-
* - Otherwise: `$or` of named-scope clause and (if "self" present) an
|
|
183
|
-
* own-user clause keyed by `openclaw_user`.
|
|
184
|
-
*/
|
|
185
|
-
export function buildScopeFilter(
|
|
186
|
-
canRead: CanReadScope[],
|
|
187
|
-
userId: string,
|
|
188
|
-
): Record<string, unknown> | undefined {
|
|
189
|
-
if (canRead.includes("*")) return undefined
|
|
190
|
-
if (canRead.length === 0) {
|
|
191
|
-
// Deliberate "no access" — match nothing.
|
|
192
|
-
return { openclaw_scope: "__never__" }
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
const namedScopes = canRead.filter((s) => s !== "self" && s !== "*")
|
|
196
|
-
const includeSelf = canRead.includes("self")
|
|
197
|
-
|
|
198
|
-
const clauses: Array<Record<string, unknown>> = []
|
|
199
|
-
if (namedScopes.length > 0) {
|
|
200
|
-
clauses.push({
|
|
201
|
-
openclaw_scope: { $in: namedScopes.map((s) => normalizeScope(s)) },
|
|
202
|
-
})
|
|
203
|
-
}
|
|
204
|
-
if (includeSelf && userId) {
|
|
205
|
-
clauses.push({ openclaw_user: userId })
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
if (clauses.length === 0) {
|
|
209
|
-
return { openclaw_scope: "__never__" }
|
|
210
|
-
}
|
|
211
|
-
if (clauses.length === 1) {
|
|
212
|
-
return clauses[0]
|
|
213
|
-
}
|
|
214
|
-
return { $or: clauses }
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* Route a write: private scope stays in the user's own Hyperspell space
|
|
219
|
-
* (defense-in-depth); shared scopes go to `sharedUserId` with optional
|
|
220
|
-
* per-scope collection.
|
|
221
|
-
*/
|
|
222
|
-
export function routeWrite(
|
|
223
|
-
user: ResolvedUser | undefined,
|
|
224
|
-
scope: ScopeName,
|
|
225
|
-
cfg: HyperspellConfig,
|
|
226
|
-
): { userId: string | undefined; collection: string | undefined } {
|
|
227
|
-
const scoping = cfg.multiUser?.scoping
|
|
228
|
-
if (scope === "private") {
|
|
229
|
-
return { userId: user?.userId, collection: undefined }
|
|
230
|
-
}
|
|
231
|
-
const sharedUserId = cfg.multiUser?.sharedUserId ?? user?.userId
|
|
232
|
-
const collection = scoping?.collections?.[scope]
|
|
233
|
-
return { userId: sharedUserId, collection }
|
|
234
|
-
}
|
package/lib/voice-id.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import type { HyperspellConfig } from "../config.ts"
|
|
2
|
-
|
|
3
|
-
export interface VoiceIdResult {
|
|
4
|
-
userId: string
|
|
5
|
-
confidence: number
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export interface VoiceIdentifier {
|
|
9
|
-
identify(audio: Buffer | string): Promise<VoiceIdResult | null>
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const NO_OP: VoiceIdentifier = {
|
|
13
|
-
async identify() {
|
|
14
|
-
return null
|
|
15
|
-
},
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const registry = new Map<string, VoiceIdentifier>()
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Register a voice-ID adapter implementation under a name that can be
|
|
22
|
-
* referenced from `cfg.multiUser.scoping.voiceId.adapter`. Call this from
|
|
23
|
-
* downstream code that wants to plug in a real speaker diarization model;
|
|
24
|
-
* Phase 1 ships with no adapters registered — the no-op is used.
|
|
25
|
-
*/
|
|
26
|
-
export function registerVoiceIdentifier(
|
|
27
|
-
name: string,
|
|
28
|
-
impl: VoiceIdentifier,
|
|
29
|
-
): void {
|
|
30
|
-
registry.set(name, impl)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function getVoiceIdentifier(cfg: HyperspellConfig): VoiceIdentifier {
|
|
34
|
-
const name = cfg.multiUser?.scoping?.voiceId?.adapter
|
|
35
|
-
if (name && registry.has(name)) {
|
|
36
|
-
return registry.get(name) as VoiceIdentifier
|
|
37
|
-
}
|
|
38
|
-
return NO_OP
|
|
39
|
-
}
|
package/logger.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
type Logger = {
|
|
2
|
-
info: (message: string, ...args: unknown[]) => void
|
|
3
|
-
warn: (message: string, ...args: unknown[]) => void
|
|
4
|
-
error: (message: string, ...args: unknown[]) => void
|
|
5
|
-
debug: (message: string, ...args: unknown[]) => void
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
let _logger: Logger = console
|
|
9
|
-
let _debug = false
|
|
10
|
-
|
|
11
|
-
export function initLogger(logger: Logger, debug: boolean): void {
|
|
12
|
-
_logger = logger
|
|
13
|
-
_debug = debug
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export const log = {
|
|
17
|
-
info: (message: string, ...args: unknown[]) => {
|
|
18
|
-
_logger.info(`hyperspell: ${message}`, ...args)
|
|
19
|
-
},
|
|
20
|
-
warn: (message: string, ...args: unknown[]) => {
|
|
21
|
-
_logger.warn(`hyperspell: ${message}`, ...args)
|
|
22
|
-
},
|
|
23
|
-
error: (message: string, ...args: unknown[]) => {
|
|
24
|
-
_logger.error(`hyperspell: ${message}`, ...args)
|
|
25
|
-
},
|
|
26
|
-
debug: (message: string, ...args: unknown[]) => {
|
|
27
|
-
if (_debug) {
|
|
28
|
-
_logger.debug(`hyperspell: ${message}`, ...args)
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
|
-
debugRequest: (method: string, params: unknown) => {
|
|
32
|
-
if (_debug) {
|
|
33
|
-
_logger.debug(`hyperspell: [${method}] request`, params)
|
|
34
|
-
}
|
|
35
|
-
},
|
|
36
|
-
debugResponse: (method: string, result: unknown) => {
|
|
37
|
-
if (_debug) {
|
|
38
|
-
_logger.debug(`hyperspell: [${method}] response`, result)
|
|
39
|
-
}
|
|
40
|
-
},
|
|
41
|
-
}
|
package/sync/markdown.ts
DELETED
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
import * as fs from "node:fs"
|
|
2
|
-
import * as path from "node:path"
|
|
3
|
-
import type { HyperspellClient } from "../client.ts"
|
|
4
|
-
import { log } from "../logger.ts"
|
|
5
|
-
|
|
6
|
-
const FRONTMATTER_REGEX = /^---\n([\s\S]*?)\n---\n?/
|
|
7
|
-
|
|
8
|
-
interface MarkdownFile {
|
|
9
|
-
filePath: string
|
|
10
|
-
title: string
|
|
11
|
-
content: string
|
|
12
|
-
hyperspellId: string | null
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Parse frontmatter from markdown content
|
|
17
|
-
*/
|
|
18
|
-
function parseFrontmatter(content: string): { frontmatter: Record<string, string>; body: string } {
|
|
19
|
-
const match = content.match(FRONTMATTER_REGEX)
|
|
20
|
-
if (!match) {
|
|
21
|
-
return { frontmatter: {}, body: content }
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const frontmatterText = match[1]
|
|
25
|
-
const body = content.slice(match[0].length)
|
|
26
|
-
const frontmatter: Record<string, string> = {}
|
|
27
|
-
|
|
28
|
-
for (const line of frontmatterText.split("\n")) {
|
|
29
|
-
const colonIndex = line.indexOf(":")
|
|
30
|
-
if (colonIndex > 0) {
|
|
31
|
-
const key = line.slice(0, colonIndex).trim()
|
|
32
|
-
const value = line.slice(colonIndex + 1).trim()
|
|
33
|
-
frontmatter[key] = value
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return { frontmatter, body }
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Serialize frontmatter back to string
|
|
42
|
-
*/
|
|
43
|
-
function serializeFrontmatter(frontmatter: Record<string, string>): string {
|
|
44
|
-
const lines = Object.entries(frontmatter).map(([key, value]) => `${key}: ${value}`)
|
|
45
|
-
return `---\n${lines.join("\n")}\n---\n`
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Read a markdown file and parse its content
|
|
50
|
-
*/
|
|
51
|
-
function readMarkdownFile(filePath: string): MarkdownFile | null {
|
|
52
|
-
try {
|
|
53
|
-
const content = fs.readFileSync(filePath, "utf-8")
|
|
54
|
-
const { frontmatter, body } = parseFrontmatter(content)
|
|
55
|
-
const title = frontmatter.title || path.basename(filePath, ".md")
|
|
56
|
-
|
|
57
|
-
return {
|
|
58
|
-
filePath,
|
|
59
|
-
title,
|
|
60
|
-
content: body.trim(),
|
|
61
|
-
hyperspellId: frontmatter.hyperspell_id || null,
|
|
62
|
-
}
|
|
63
|
-
} catch (err) {
|
|
64
|
-
log.error(`Failed to read markdown file: ${filePath}`, err)
|
|
65
|
-
return null
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Update the hyperspell_id in the frontmatter of a markdown file
|
|
71
|
-
*/
|
|
72
|
-
function updateFrontmatterId(filePath: string, hyperspellId: string): void {
|
|
73
|
-
try {
|
|
74
|
-
const content = fs.readFileSync(filePath, "utf-8")
|
|
75
|
-
const { frontmatter, body } = parseFrontmatter(content)
|
|
76
|
-
|
|
77
|
-
frontmatter.hyperspell_id = hyperspellId
|
|
78
|
-
|
|
79
|
-
const newContent = serializeFrontmatter(frontmatter) + body
|
|
80
|
-
fs.writeFileSync(filePath, newContent)
|
|
81
|
-
|
|
82
|
-
log.debug(`Updated frontmatter in ${filePath} with hyperspell_id: ${hyperspellId}`)
|
|
83
|
-
} catch (err) {
|
|
84
|
-
log.error(`Failed to update frontmatter in ${filePath}`, err)
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Get all markdown files from the memory directory, including subdirectories
|
|
90
|
-
*/
|
|
91
|
-
export function getMemoryFiles(workspaceDir: string): string[] {
|
|
92
|
-
const memoryDir = path.join(workspaceDir, "memory")
|
|
93
|
-
|
|
94
|
-
if (!fs.existsSync(memoryDir)) {
|
|
95
|
-
return []
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
const results: string[] = []
|
|
99
|
-
|
|
100
|
-
function walk(dir: string): void {
|
|
101
|
-
try {
|
|
102
|
-
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
103
|
-
const fullPath = path.join(dir, entry.name)
|
|
104
|
-
if (entry.isDirectory()) {
|
|
105
|
-
walk(fullPath)
|
|
106
|
-
} else if (entry.name.endsWith(".md")) {
|
|
107
|
-
results.push(fullPath)
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
} catch (err) {
|
|
111
|
-
log.error(`Failed to read directory: ${dir}`, err)
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
walk(memoryDir)
|
|
116
|
-
return results
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Sync a single markdown file to Hyperspell
|
|
121
|
-
*/
|
|
122
|
-
export async function syncMarkdownFile(
|
|
123
|
-
client: HyperspellClient,
|
|
124
|
-
filePath: string,
|
|
125
|
-
options?: { userId?: string },
|
|
126
|
-
): Promise<{ success: boolean; resourceId?: string; error?: string }> {
|
|
127
|
-
const file = readMarkdownFile(filePath)
|
|
128
|
-
if (!file) {
|
|
129
|
-
return { success: false, error: "Failed to read file" }
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
if (!file.content) {
|
|
133
|
-
return { success: false, error: "File has no content" }
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
try {
|
|
137
|
-
const result = await client.addMemory(file.content, {
|
|
138
|
-
title: file.title,
|
|
139
|
-
resourceId: file.hyperspellId || undefined,
|
|
140
|
-
collection: "openclaw",
|
|
141
|
-
metadata: {
|
|
142
|
-
openclaw_source: "memory_sync",
|
|
143
|
-
file_path: filePath,
|
|
144
|
-
},
|
|
145
|
-
userId: options?.userId,
|
|
146
|
-
})
|
|
147
|
-
|
|
148
|
-
// Update frontmatter with new resource ID if it changed or was newly created
|
|
149
|
-
if (result.resourceId !== file.hyperspellId) {
|
|
150
|
-
updateFrontmatterId(filePath, result.resourceId)
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
return { success: true, resourceId: result.resourceId }
|
|
154
|
-
} catch (err) {
|
|
155
|
-
const errorMsg = err instanceof Error ? err.message : String(err)
|
|
156
|
-
log.error(`Failed to sync ${filePath}`, err)
|
|
157
|
-
return { success: false, error: errorMsg }
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* Sync all markdown files in the memory directory
|
|
163
|
-
*/
|
|
164
|
-
export async function syncAllMemoryFiles(
|
|
165
|
-
client: HyperspellClient,
|
|
166
|
-
workspaceDir: string,
|
|
167
|
-
options?: { userId?: string },
|
|
168
|
-
): Promise<{ synced: number; failed: number; errors: string[] }> {
|
|
169
|
-
const files = getMemoryFiles(workspaceDir)
|
|
170
|
-
let synced = 0
|
|
171
|
-
let failed = 0
|
|
172
|
-
const errors: string[] = []
|
|
173
|
-
|
|
174
|
-
for (const filePath of files) {
|
|
175
|
-
const result = await syncMarkdownFile(client, filePath, { userId: options?.userId })
|
|
176
|
-
if (result.success) {
|
|
177
|
-
synced++
|
|
178
|
-
log.info(`Synced: ${path.basename(filePath)} -> ${result.resourceId}`)
|
|
179
|
-
} else {
|
|
180
|
-
failed++
|
|
181
|
-
errors.push(`${path.basename(filePath)}: ${result.error}`)
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
return { synced, failed, errors }
|
|
186
|
-
}
|
package/tools/remember.ts
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import { Type } from "@sinclair/typebox"
|
|
2
|
-
import type { HyperspellClient } from "../client.ts"
|
|
3
|
-
import type { HyperspellConfig } from "../config.ts"
|
|
4
|
-
import {
|
|
5
|
-
getDefaultWriteScope,
|
|
6
|
-
resolveRole,
|
|
7
|
-
resolveUser,
|
|
8
|
-
routeWrite,
|
|
9
|
-
} from "../lib/sender.ts"
|
|
10
|
-
import { log } from "../logger.ts"
|
|
11
|
-
|
|
12
|
-
export function createRememberToolFactory(
|
|
13
|
-
client: HyperspellClient,
|
|
14
|
-
cfg: HyperspellConfig,
|
|
15
|
-
) {
|
|
16
|
-
const scopingEnabled = !!cfg.multiUser?.scoping
|
|
17
|
-
const availableScopes = cfg.multiUser?.scoping?.scopes ?? []
|
|
18
|
-
const scopeDescription = scopingEnabled
|
|
19
|
-
? `Privacy scope for this memory. Available: ${availableScopes.join(", ")}. Defaults to the user's role default.`
|
|
20
|
-
: "Privacy scope (only used when scoping is enabled in config)."
|
|
21
|
-
|
|
22
|
-
return (ctx: Record<string, unknown>) => ({
|
|
23
|
-
name: "hyperspell_remember",
|
|
24
|
-
label: "Memory Store",
|
|
25
|
-
description: "Save important information to the user's memory.",
|
|
26
|
-
parameters: Type.Object({
|
|
27
|
-
text: Type.String({ description: "Information to remember" }),
|
|
28
|
-
title: Type.Optional(
|
|
29
|
-
Type.String({ description: "Optional title for the memory" }),
|
|
30
|
-
),
|
|
31
|
-
date: Type.Optional(
|
|
32
|
-
Type.String({ description: "Date of the memory (ISO 8601 or YYYY-MM-DD). Helps ranking and enables date-range filtering. Defaults to now if omitted." }),
|
|
33
|
-
),
|
|
34
|
-
userId: Type.Optional(
|
|
35
|
-
Type.String({
|
|
36
|
-
description:
|
|
37
|
-
"Store for a specific user or 'shared' for everyone. Omit to store for current sender.",
|
|
38
|
-
}),
|
|
39
|
-
),
|
|
40
|
-
scope: Type.Optional(
|
|
41
|
-
Type.String({ description: scopeDescription }),
|
|
42
|
-
),
|
|
43
|
-
}),
|
|
44
|
-
async execute(
|
|
45
|
-
_toolCallId: string,
|
|
46
|
-
params: { text: string; title?: string; date?: string; userId?: string; scope?: string },
|
|
47
|
-
) {
|
|
48
|
-
const resolved = resolveUser(ctx, cfg)
|
|
49
|
-
|
|
50
|
-
// Scope resolution: explicit param > role default > global default > "private"
|
|
51
|
-
const scope =
|
|
52
|
-
params.scope ??
|
|
53
|
-
(scopingEnabled ? getDefaultWriteScope(resolved, cfg) : "private")
|
|
54
|
-
|
|
55
|
-
// Validate scope is in declared vocabulary (if scoping is enabled)
|
|
56
|
-
if (
|
|
57
|
-
scopingEnabled &&
|
|
58
|
-
availableScopes.length > 0 &&
|
|
59
|
-
!availableScopes.includes(scope)
|
|
60
|
-
) {
|
|
61
|
-
return {
|
|
62
|
-
content: [
|
|
63
|
-
{
|
|
64
|
-
type: "text" as const,
|
|
65
|
-
text: `Unknown scope "${scope}". Available: ${availableScopes.join(", ")}.`,
|
|
66
|
-
},
|
|
67
|
-
],
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// canWriteScopes enforcement (role-level deny list)
|
|
72
|
-
if (scopingEnabled) {
|
|
73
|
-
const role = resolveRole(resolved, cfg)
|
|
74
|
-
if (role?.canWriteScopes && !role.canWriteScopes.includes(scope)) {
|
|
75
|
-
return {
|
|
76
|
-
content: [
|
|
77
|
-
{
|
|
78
|
-
type: "text" as const,
|
|
79
|
-
text: `You cannot write to scope "${scope}".`,
|
|
80
|
-
},
|
|
81
|
-
],
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// Route: explicit userId override takes precedence; otherwise derive from scope
|
|
87
|
-
let userId: string | undefined
|
|
88
|
-
let collection: string | undefined
|
|
89
|
-
if (params.userId) {
|
|
90
|
-
userId = params.userId
|
|
91
|
-
} else if (scopingEnabled) {
|
|
92
|
-
const routed = routeWrite(resolved, scope, cfg)
|
|
93
|
-
userId = routed.userId
|
|
94
|
-
collection = routed.collection
|
|
95
|
-
} else {
|
|
96
|
-
userId = resolved?.userId
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
log.debug(
|
|
100
|
-
`remember tool: "${params.text.slice(0, 50)}..." date=${params.date ?? "now"} userId=${userId} scope=${scope}`,
|
|
101
|
-
)
|
|
102
|
-
|
|
103
|
-
try {
|
|
104
|
-
await client.addMemory(params.text, {
|
|
105
|
-
title: params.title,
|
|
106
|
-
date: params.date,
|
|
107
|
-
collection,
|
|
108
|
-
metadata: { source: "openclaw_tool" },
|
|
109
|
-
userId,
|
|
110
|
-
scope: scopingEnabled ? scope : undefined,
|
|
111
|
-
})
|
|
112
|
-
|
|
113
|
-
const preview =
|
|
114
|
-
params.text.length > 80 ? `${params.text.slice(0, 80)}…` : params.text
|
|
115
|
-
|
|
116
|
-
return {
|
|
117
|
-
content: [{ type: "text" as const, text: `Stored: "${preview}"` }],
|
|
118
|
-
}
|
|
119
|
-
} catch (err) {
|
|
120
|
-
log.error("remember tool failed", err)
|
|
121
|
-
return {
|
|
122
|
-
content: [
|
|
123
|
-
{
|
|
124
|
-
type: "text" as const,
|
|
125
|
-
text: `Failed to store memory: ${err instanceof Error ? err.message : String(err)}`,
|
|
126
|
-
},
|
|
127
|
-
],
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
},
|
|
131
|
-
})
|
|
132
|
-
}
|