dsh-miloco-bridge 0.1.3 → 0.1.5
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 +17 -11
- package/cordis.patch.yml +13 -2
- package/docs/ARCHITECTURE.md +74 -17
- package/docs/INSTALL.md +23 -13
- package/docs/TOOLS.md +1 -1
- package/docs/VERIFICATION.md +30 -16
- package/docs/images/miloco-overview.png +0 -0
- package/docs/images/miloco-settings.png +0 -0
- package/lib/client.js +979 -54
- package/lib/home.js +319 -0
- package/lib/index.js +57 -1
- package/lib/overlay.js +53 -7
- package/lib/paths.js +18 -0
- package/lib/push.js +328 -48
- package/lib/settings-api.js +300 -7
- package/lib/skills.js +9 -1
- package/lib/state.js +62 -2
- package/lib/tools-entry.js +3 -1
- package/lib/tools.js +24 -5
- package/package.json +7 -2
package/lib/home.js
ADDED
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Miloco household workspace.
|
|
3
|
+
*
|
|
4
|
+
* The household assistant owns exactly one directory: the workspace every
|
|
5
|
+
* conversation of the 「Miloco 家庭助手」 preset lives in. The plugin creates
|
|
6
|
+
* the directory, registers it as a host Workspace, retitles it, and keeps it
|
|
7
|
+
* first in the registry order so the sidebar lists it above normal workspaces.
|
|
8
|
+
*
|
|
9
|
+
* The directory is configurable at runtime: `overlay.home.dir` wins over the
|
|
10
|
+
* static `workspace.dir`. Registration is optional — a host without
|
|
11
|
+
* `workspaceRegistry` still gets the directory, only the id stays empty.
|
|
12
|
+
*
|
|
13
|
+
* @module dsh-miloco-bridge/home
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { mkdir } from 'node:fs/promises'
|
|
17
|
+
import { resolveMilocoHomeDir } from './paths.js'
|
|
18
|
+
|
|
19
|
+
/** Default title of the household workspace. */
|
|
20
|
+
export const DEFAULT_HOME_TITLE = '家庭助手'
|
|
21
|
+
|
|
22
|
+
/** Default Agent preset used by the household workspace. */
|
|
23
|
+
export const DEFAULT_HOME_AGENT_PRESET = 'miloco'
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Create the household workspace service.
|
|
27
|
+
*
|
|
28
|
+
* @param {{ctx: any, config: any, overlay?: any, logger?: any}} options - Service options.
|
|
29
|
+
* @returns {{
|
|
30
|
+
* dir: string,
|
|
31
|
+
* dirSource: 'overlay'|'config'|'default',
|
|
32
|
+
* workspaceId: string,
|
|
33
|
+
* title: string,
|
|
34
|
+
* agentPreset: string,
|
|
35
|
+
* ensure: () => Promise<any>,
|
|
36
|
+
* current: () => any,
|
|
37
|
+
* setDir: (value: string) => Promise<any>,
|
|
38
|
+
* setEnabled: (value: boolean) => Promise<any>,
|
|
39
|
+
* }} The service.
|
|
40
|
+
*/
|
|
41
|
+
export function createHomeService(options) {
|
|
42
|
+
const { ctx, config } = options
|
|
43
|
+
const overlay = options.overlay
|
|
44
|
+
const log = options.logger ?? ctx?.logger
|
|
45
|
+
const homeConfig = config?.workspace && typeof config.workspace === 'object'
|
|
46
|
+
? config.workspace
|
|
47
|
+
: (config?.home && typeof config.home === 'object' ? config.home : {})
|
|
48
|
+
|
|
49
|
+
const title = typeof homeConfig.title === 'string' && homeConfig.title.trim()
|
|
50
|
+
? homeConfig.title.trim()
|
|
51
|
+
: DEFAULT_HOME_TITLE
|
|
52
|
+
const agentPreset = typeof homeConfig.agentPreset === 'string' && homeConfig.agentPreset.trim()
|
|
53
|
+
? homeConfig.agentPreset.trim()
|
|
54
|
+
: DEFAULT_HOME_AGENT_PRESET
|
|
55
|
+
const staticDir = typeof homeConfig.dir === 'string' ? homeConfig.dir.trim() : ''
|
|
56
|
+
// Runtime switch: false stops registration (and lets a deliberate removal
|
|
57
|
+
// stick); the plugin still creates the directory for push sessions.
|
|
58
|
+
let enabled = homeConfig.enabled !== false
|
|
59
|
+
|
|
60
|
+
/** @type {string} */
|
|
61
|
+
let dir = resolveMilocoHomeDir(staticDir)
|
|
62
|
+
/** @type {'overlay'|'config'|'default'} */
|
|
63
|
+
let dirSource = staticDir ? 'config' : 'default'
|
|
64
|
+
/** @type {string} */
|
|
65
|
+
let workspaceId = ''
|
|
66
|
+
/** @type {Promise<void> | null} */
|
|
67
|
+
let pending = null
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Read the current snapshot.
|
|
71
|
+
* @returns {{path: string, workspaceId: string, title: string, agentPreset: string, dirSource: string}} The snapshot.
|
|
72
|
+
*/
|
|
73
|
+
function current() {
|
|
74
|
+
return { path: dir, workspaceId, title, agentPreset, dirSource, enabled }
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Resolve the effective directory from the overlay, then the static config.
|
|
79
|
+
* @returns {Promise<{dir: string, source: 'overlay'|'config'|'default'}>} The effective directory.
|
|
80
|
+
*/
|
|
81
|
+
async function effectiveDir() {
|
|
82
|
+
if (overlay && typeof overlay.current === 'function') {
|
|
83
|
+
try {
|
|
84
|
+
const value = String((await overlay.current())?.home?.dir ?? '').trim()
|
|
85
|
+
if (value) return { dir: resolveMilocoHomeDir(value), source: 'overlay' }
|
|
86
|
+
} catch { /* fall through to the static config */ }
|
|
87
|
+
}
|
|
88
|
+
if (staticDir) return { dir: resolveMilocoHomeDir(staticDir), source: 'config' }
|
|
89
|
+
return { dir: resolveMilocoHomeDir(''), source: 'default' }
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Resolve the effective switch from the overlay, then the static config.
|
|
94
|
+
* @returns {Promise<boolean>} Whether the household workspace is registered.
|
|
95
|
+
*/
|
|
96
|
+
async function effectiveEnabled() {
|
|
97
|
+
if (overlay && typeof overlay.current === 'function') {
|
|
98
|
+
try {
|
|
99
|
+
const value = (await overlay.current())?.workspace?.enabled
|
|
100
|
+
if (typeof value === 'boolean') return value
|
|
101
|
+
} catch { /* fall through to the static config */ }
|
|
102
|
+
}
|
|
103
|
+
return homeConfig.enabled !== false
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Move the household workspace before the first other workspace.
|
|
108
|
+
* @param {any} registry - Host workspace registry.
|
|
109
|
+
* @returns {Promise<void>} Resolves once the attempt finished.
|
|
110
|
+
*/
|
|
111
|
+
async function pinFirst(registry) {
|
|
112
|
+
if (!registry || !workspaceId || typeof registry.list !== 'function' || typeof registry.insertBefore !== 'function') return
|
|
113
|
+
let items
|
|
114
|
+
try {
|
|
115
|
+
items = registry.list()
|
|
116
|
+
} catch (error) {
|
|
117
|
+
log?.warn?.(`dsh-miloco-bridge: 读取工作区列表失败:${error instanceof Error ? error.message : error}`)
|
|
118
|
+
return
|
|
119
|
+
}
|
|
120
|
+
if (!Array.isArray(items) || items.length <= 1) return
|
|
121
|
+
if (String(items[0]?.id ?? '') === workspaceId) return
|
|
122
|
+
if (!items.some(item => String(item?.id ?? '') === workspaceId)) return
|
|
123
|
+
const anchor = items.find(item => String(item?.id ?? '') !== workspaceId)
|
|
124
|
+
if (!anchor) return
|
|
125
|
+
try {
|
|
126
|
+
await registry.insertBefore(workspaceId, anchor.id)
|
|
127
|
+
} catch (error) {
|
|
128
|
+
log?.warn?.(`dsh-miloco-bridge: 置顶家庭助手工作区失败:${error instanceof Error ? error.message : error}`)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Resolve the host workspace registry, tolerating a throwing `ctx.get`.
|
|
134
|
+
* @returns {any} The registry, or undefined when the host has none.
|
|
135
|
+
*/
|
|
136
|
+
function resolveRegistry() {
|
|
137
|
+
try {
|
|
138
|
+
return ctx?.get?.('workspaceRegistry')
|
|
139
|
+
} catch (error) {
|
|
140
|
+
log?.warn?.(`dsh-miloco-bridge: 读取工作区注册表失败:${error instanceof Error ? error.message : error}`)
|
|
141
|
+
return undefined
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Register the directory as a host workspace and keep its title.
|
|
147
|
+
* @returns {Promise<void>} Resolves once the attempt finished.
|
|
148
|
+
*/
|
|
149
|
+
async function register() {
|
|
150
|
+
const targetDir = dir
|
|
151
|
+
const registry = resolveRegistry()
|
|
152
|
+
if (!registry || typeof registry.create !== 'function') return
|
|
153
|
+
if (workspaceId) {
|
|
154
|
+
// The user may have deleted the household workspace from the sidebar.
|
|
155
|
+
// Re-register only when the registry list is readable and no longer
|
|
156
|
+
// contains the id; an unreadable list must not churn the registration.
|
|
157
|
+
let listed = null
|
|
158
|
+
if (typeof registry.list === 'function') {
|
|
159
|
+
try {
|
|
160
|
+
const items = registry.list()
|
|
161
|
+
if (Array.isArray(items)) listed = items
|
|
162
|
+
} catch { /* treat an unreadable list as "still registered" */ }
|
|
163
|
+
}
|
|
164
|
+
if (!listed || listed.some(item => String(item?.id ?? '') === workspaceId)) return
|
|
165
|
+
workspaceId = ''
|
|
166
|
+
}
|
|
167
|
+
try {
|
|
168
|
+
const workspace = await registry.create(targetDir, title)
|
|
169
|
+
// A concurrent setDir() may have moved `dir` while create() was pending;
|
|
170
|
+
// never attach the old directory's id to the new directory.
|
|
171
|
+
const createdId = String(workspace?.id ?? '')
|
|
172
|
+
if (dir === targetDir) workspaceId = createdId
|
|
173
|
+
if (workspaceId && workspace && typeof workspace.setTitle === 'function' && workspace.title !== title) {
|
|
174
|
+
try {
|
|
175
|
+
await workspace.setTitle(title)
|
|
176
|
+
} catch (error) {
|
|
177
|
+
log?.warn?.(`dsh-miloco-bridge: 重命名家庭助手工作区失败:${error instanceof Error ? error.message : error}`)
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
} catch (error) {
|
|
181
|
+
log?.warn?.(`dsh-miloco-bridge: 注册家庭助手工作区失败:${error instanceof Error ? error.message : error}`)
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Resolve the directory and make sure it exists.
|
|
187
|
+
* @returns {Promise<void>} Resolves once the attempt finished.
|
|
188
|
+
*/
|
|
189
|
+
async function prepare() {
|
|
190
|
+
const effective = await effectiveDir()
|
|
191
|
+
if (effective.dir !== dir) {
|
|
192
|
+
dir = effective.dir
|
|
193
|
+
workspaceId = ''
|
|
194
|
+
}
|
|
195
|
+
dirSource = effective.source
|
|
196
|
+
try {
|
|
197
|
+
await mkdir(dir, { recursive: true })
|
|
198
|
+
} catch (error) {
|
|
199
|
+
log?.warn?.(`dsh-miloco-bridge: 创建家庭助手目录失败:${error instanceof Error ? error.message : error}`)
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Drop the host registration of the household workspace.
|
|
205
|
+
*
|
|
206
|
+
* `delete()` also clears the session ledger, so the household conversations
|
|
207
|
+
* fall back to 「未分组」 instead of staying under a removed workspace.
|
|
208
|
+
*
|
|
209
|
+
* @returns {Promise<void>} Resolves once the attempt finished.
|
|
210
|
+
*/
|
|
211
|
+
async function unregister() {
|
|
212
|
+
if (!workspaceId) return
|
|
213
|
+
const registry = resolveRegistry()
|
|
214
|
+
if (!registry || typeof registry.delete !== 'function') return
|
|
215
|
+
const id = workspaceId
|
|
216
|
+
try {
|
|
217
|
+
await registry.delete(id)
|
|
218
|
+
workspaceId = ''
|
|
219
|
+
} catch (error) {
|
|
220
|
+
log?.warn?.(`dsh-miloco-bridge: 注销家庭助手工作区失败:${error instanceof Error ? error.message : error}`)
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Re-attach sessions whose stored cwd is the household directory.
|
|
226
|
+
*
|
|
227
|
+
* A workspace deleted from the sidebar takes its membership with it; after
|
|
228
|
+
* re-registration the directory match is still the durable fact.
|
|
229
|
+
*
|
|
230
|
+
* @returns {Promise<void>} Resolves once every attach attempt finished.
|
|
231
|
+
*/
|
|
232
|
+
async function attachMembers() {
|
|
233
|
+
const registry = resolveRegistry()
|
|
234
|
+
const workspace = registry?.get?.(workspaceId)
|
|
235
|
+
if (!workspace || typeof workspace.attachSession !== 'function') return
|
|
236
|
+
let sessions
|
|
237
|
+
try {
|
|
238
|
+
sessions = ctx?.get?.('sessions')
|
|
239
|
+
} catch { return }
|
|
240
|
+
const list = typeof sessions?.list === 'function' ? sessions.list() : []
|
|
241
|
+
for (const session of list) {
|
|
242
|
+
const id = session?.id
|
|
243
|
+
if (typeof id !== 'string' || !id) continue
|
|
244
|
+
if (session?.header?.cwd !== dir) continue
|
|
245
|
+
if (Array.isArray(workspace.sessionIds) && workspace.sessionIds.includes(id)) continue
|
|
246
|
+
try {
|
|
247
|
+
await workspace.attachSession(id)
|
|
248
|
+
} catch (error) {
|
|
249
|
+
log?.warn?.(`dsh-miloco-bridge: 恢复家庭工作区会话失败:${error instanceof Error ? error.message : error}`)
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Create the directory, register the host workspace and re-pin its order.
|
|
256
|
+
* Idempotent; the pin runs on every call so a user reordering the sidebar is
|
|
257
|
+
* corrected on the next ensure.
|
|
258
|
+
* @returns {Promise<any>} The snapshot.
|
|
259
|
+
*/
|
|
260
|
+
function ensure() {
|
|
261
|
+
if (!pending) {
|
|
262
|
+
pending = prepare().finally(() => { pending = null })
|
|
263
|
+
}
|
|
264
|
+
return pending.then(async () => {
|
|
265
|
+
enabled = await effectiveEnabled()
|
|
266
|
+
if (!enabled) {
|
|
267
|
+
await unregister()
|
|
268
|
+
return current()
|
|
269
|
+
}
|
|
270
|
+
await register()
|
|
271
|
+
if (workspaceId) {
|
|
272
|
+
await attachMembers()
|
|
273
|
+
await pinFirst(resolveRegistry())
|
|
274
|
+
}
|
|
275
|
+
return current()
|
|
276
|
+
})
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Turn the household workspace registration on or off at runtime.
|
|
281
|
+
* @param {boolean} value - `true` registers and pins the workspace.
|
|
282
|
+
* @returns {Promise<any>} The new snapshot.
|
|
283
|
+
*/
|
|
284
|
+
async function setEnabled(value) {
|
|
285
|
+
if (!overlay || typeof overlay.write !== 'function') {
|
|
286
|
+
throw new Error('运行时覆盖文件不可用,无法修改置顶开关')
|
|
287
|
+
}
|
|
288
|
+
await overlay.write({ workspace: { enabled: Boolean(value) } })
|
|
289
|
+
pending = null
|
|
290
|
+
return ensure()
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Change the household directory at runtime.
|
|
295
|
+
* @param {string} value - New directory; empty clears the overlay override.
|
|
296
|
+
* @returns {Promise<any>} The new snapshot.
|
|
297
|
+
*/
|
|
298
|
+
async function setDir(value) {
|
|
299
|
+
if (!overlay || typeof overlay.write !== 'function') {
|
|
300
|
+
throw new Error('运行时覆盖文件不可用,无法修改工作区目录')
|
|
301
|
+
}
|
|
302
|
+
const text = String(value ?? '').trim()
|
|
303
|
+
await overlay.write({ home: { dir: text } })
|
|
304
|
+
pending = null
|
|
305
|
+
return ensure()
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
return {
|
|
309
|
+
get dir() { return dir },
|
|
310
|
+
get dirSource() { return dirSource },
|
|
311
|
+
get workspaceId() { return workspaceId },
|
|
312
|
+
get title() { return title },
|
|
313
|
+
get agentPreset() { return agentPreset },
|
|
314
|
+
ensure,
|
|
315
|
+
current,
|
|
316
|
+
setDir,
|
|
317
|
+
setEnabled,
|
|
318
|
+
}
|
|
319
|
+
}
|
package/lib/index.js
CHANGED
|
@@ -18,7 +18,12 @@ import { createMilocoClient, resolveMilocoHome } from './miloco-client.js'
|
|
|
18
18
|
import { createMilocoTools } from './tools.js'
|
|
19
19
|
import { normalizePerceptionLevel } from './perception.js'
|
|
20
20
|
import { resolveDshHome } from './paths.js'
|
|
21
|
-
import {
|
|
21
|
+
import {
|
|
22
|
+
createHomeService,
|
|
23
|
+
DEFAULT_HOME_AGENT_PRESET,
|
|
24
|
+
DEFAULT_HOME_TITLE,
|
|
25
|
+
} from './home.js'
|
|
26
|
+
import { createOverlay, DEFAULT_OVERLAY_FILE, normalizePushTargetMode } from './overlay.js'
|
|
22
27
|
import {
|
|
23
28
|
clearSessions,
|
|
24
29
|
createStateStore,
|
|
@@ -49,11 +54,32 @@ export function normalizeConfig(raw) {
|
|
|
49
54
|
const modelRaw = pushRaw.model && typeof pushRaw.model === 'object' ? pushRaw.model : {}
|
|
50
55
|
const deployRaw = config.deploy && typeof config.deploy === 'object' ? config.deploy : {}
|
|
51
56
|
const timeoutMs = Number(config.timeoutMs)
|
|
57
|
+
// `home` keeps its original meaning: the Miloco **backend** install
|
|
58
|
+
// directory. The household workspace lives under its own `workspace` key so
|
|
59
|
+
// existing configurations (and the preset's own `home`) keep working.
|
|
60
|
+
// A `home` *object* is also accepted as the household workspace, which is
|
|
61
|
+
// the shape the design document describes.
|
|
62
|
+
const workspaceRaw = config.workspace && typeof config.workspace === 'object'
|
|
63
|
+
? config.workspace
|
|
64
|
+
: (config.home && typeof config.home === 'object' ? config.home : {})
|
|
65
|
+
const workspaceDir = typeof workspaceRaw.dir === 'string' ? workspaceRaw.dir.trim() : ''
|
|
52
66
|
|
|
53
67
|
return {
|
|
54
68
|
baseUrl: typeof config.baseUrl === 'string' ? config.baseUrl : '',
|
|
55
69
|
token: typeof config.token === 'string' ? config.token : '',
|
|
70
|
+
// Miloco 后端安装目录。
|
|
56
71
|
home: typeof config.home === 'string' ? config.home : resolveMilocoHome(process.env.MILOCO_HOME),
|
|
72
|
+
// 家庭助手专属工作区
|
|
73
|
+
workspace: {
|
|
74
|
+
dir: workspaceDir,
|
|
75
|
+
enabled: workspaceRaw.enabled !== false,
|
|
76
|
+
title: typeof workspaceRaw.title === 'string' && workspaceRaw.title.trim()
|
|
77
|
+
? workspaceRaw.title.trim()
|
|
78
|
+
: DEFAULT_HOME_TITLE,
|
|
79
|
+
agentPreset: typeof workspaceRaw.agentPreset === 'string' && workspaceRaw.agentPreset.trim()
|
|
80
|
+
? workspaceRaw.agentPreset.trim()
|
|
81
|
+
: DEFAULT_HOME_AGENT_PRESET,
|
|
82
|
+
},
|
|
57
83
|
timeoutMs: Number.isFinite(timeoutMs) && timeoutMs > 0 ? timeoutMs : 30_000,
|
|
58
84
|
readOnly: config.readOnly !== false,
|
|
59
85
|
cwd: typeof config.cwd === 'string' ? config.cwd : '',
|
|
@@ -72,6 +98,8 @@ export function normalizeConfig(raw) {
|
|
|
72
98
|
token: typeof pushRaw.token === 'string' ? pushRaw.token : '',
|
|
73
99
|
waitForResult: pushRaw.waitForResult !== false,
|
|
74
100
|
target: {
|
|
101
|
+
// archive = 专用归档会话;session = 指定会话,空则回退上次 Miloco 会话
|
|
102
|
+
mode: normalizePushTargetMode(targetRaw.mode) || 'archive',
|
|
75
103
|
sessionId: typeof targetRaw.sessionId === 'string' ? targetRaw.sessionId.trim() : '',
|
|
76
104
|
// 新建推送会话时挂哪个 Agent 预设(默认 miloco —— 推送进来的对话要能调米家工具)
|
|
77
105
|
agentPreset: typeof targetRaw.agentPreset === 'string'
|
|
@@ -148,6 +176,31 @@ export function apply(ctx, rawConfig) {
|
|
|
148
176
|
ctx.inject(['sessionController', 'webServer', 'sandboxPolicy'], (host) => {
|
|
149
177
|
/** @type {(() => void)[]} */
|
|
150
178
|
const pushDisposers = []
|
|
179
|
+
// 家庭助手专属工作区:插件自己创建目录,并注册成宿主工作区。
|
|
180
|
+
// effect 必须同步注册,异步初始化才有一个可以依附的作用域。
|
|
181
|
+
const home = createHomeService({ ctx: host, config, overlay, logger: host.logger })
|
|
182
|
+
host.effect(() => {
|
|
183
|
+
void home.ensure().then((state) => {
|
|
184
|
+
if (!fiberActive(host)) return
|
|
185
|
+
host.logger?.info?.(`dsh-miloco-bridge: 家庭助手工作区 ${state.path}`)
|
|
186
|
+
}).catch((error) => {
|
|
187
|
+
host.logger?.warn?.(`dsh-miloco-bridge: 初始化家庭助手工作区失败:${error instanceof Error ? error.message : error}`)
|
|
188
|
+
})
|
|
189
|
+
return () => {}
|
|
190
|
+
}, 'dsh-miloco-bridge: home workspace')
|
|
191
|
+
|
|
192
|
+
// Keep the workspace registered: deleting it from the sidebar is allowed
|
|
193
|
+
// by the host UI, so re-register (and re-attach its sessions) within 30s.
|
|
194
|
+
host.effect(() => {
|
|
195
|
+
const timer = setInterval(() => {
|
|
196
|
+
void home.ensure().catch((error) => {
|
|
197
|
+
host.logger?.warn?.(`dsh-miloco-bridge: 家庭助手工作区自愈失败:${error instanceof Error ? error.message : error}`)
|
|
198
|
+
})
|
|
199
|
+
}, 30_000)
|
|
200
|
+
if (typeof timer.unref === 'function') timer.unref()
|
|
201
|
+
return () => clearInterval(timer)
|
|
202
|
+
}, 'dsh-miloco-bridge: home workspace watchdog')
|
|
203
|
+
|
|
151
204
|
// Claim the cleanup effect before the dynamic import starts: the scope may
|
|
152
205
|
// be disposed while the import is pending, and an effect registered
|
|
153
206
|
// afterwards throws INACTIVE_EFFECT inside a detached promise.
|
|
@@ -169,6 +222,8 @@ export function apply(ctx, rawConfig) {
|
|
|
169
222
|
config,
|
|
170
223
|
logger: host.logger,
|
|
171
224
|
overlay,
|
|
225
|
+
state,
|
|
226
|
+
home,
|
|
172
227
|
agentPreset: config.push.target.agentPreset,
|
|
173
228
|
activity: {
|
|
174
229
|
last: async () => (await readSessions(state)).last,
|
|
@@ -197,6 +252,7 @@ export function apply(ctx, rawConfig) {
|
|
|
197
252
|
deploy,
|
|
198
253
|
uninstall,
|
|
199
254
|
writeConfig,
|
|
255
|
+
home,
|
|
200
256
|
logger: host.logger,
|
|
201
257
|
})
|
|
202
258
|
host.effect(() => disposeApi, 'dsh-miloco-bridge: settings api')
|
package/lib/overlay.js
CHANGED
|
@@ -5,7 +5,9 @@
|
|
|
5
5
|
* choices the owner changes at runtime from the Web settings tab:
|
|
6
6
|
*
|
|
7
7
|
* - how to reach Miloco (`connection.baseUrl` / `connection.token`);
|
|
8
|
-
* -
|
|
8
|
+
* - which household workspace directory the tools work in (`home.dir`);
|
|
9
|
+
* - whether the household workspace is registered and pinned (`workspace.enabled`);
|
|
10
|
+
* - where an inbound push lands (`push.target.mode` / `push.target.sessionId`);
|
|
9
11
|
* - whether a specific model handles the pushed turn (`push.model`);
|
|
10
12
|
* - the bearer token Miloco must present (`push.token`).
|
|
11
13
|
*
|
|
@@ -17,13 +19,19 @@
|
|
|
17
19
|
*
|
|
18
20
|
* {
|
|
19
21
|
* "connection": { "baseUrl": "http://127.0.0.1:1810", "token": "..." },
|
|
22
|
+
* "home": { "dir": "~/miloco-home" },
|
|
23
|
+
* "workspace": { "enabled": true },
|
|
20
24
|
* "push": {
|
|
21
25
|
* "token": "...",
|
|
22
|
-
* "target": { "sessionId": "" },
|
|
26
|
+
* "target": { "mode": "archive", "sessionId": "" },
|
|
23
27
|
* "model": { "provider": "", "model": "", "reasoningEffort": "" }
|
|
24
28
|
* }
|
|
25
29
|
* }
|
|
26
30
|
*
|
|
31
|
+
* `write()` merges section by section with what is already on disk, so a
|
|
32
|
+
* partial write (for example only `home.dir`) never drops the other
|
|
33
|
+
* sections. An explicit empty string still clears a field.
|
|
34
|
+
*
|
|
27
35
|
* @module dsh-miloco-bridge/overlay
|
|
28
36
|
*/
|
|
29
37
|
|
|
@@ -35,14 +43,29 @@ import { normalizePerceptionLevel } from './perception.js'
|
|
|
35
43
|
/** Default overlay file location. */
|
|
36
44
|
export const DEFAULT_OVERLAY_FILE = join(resolveDshHome(), 'miloco-plugin.json')
|
|
37
45
|
|
|
46
|
+
/** Supported push target modes: a dedicated archive session or a chosen session. */
|
|
47
|
+
export const PUSH_TARGET_MODES = ['archive', 'session']
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Normalise a push target mode.
|
|
51
|
+
* @param {unknown} value - Raw value.
|
|
52
|
+
* @returns {'archive'|'session'|''} A supported mode, or '' when unset/unknown.
|
|
53
|
+
*/
|
|
54
|
+
export function normalizePushTargetMode(value) {
|
|
55
|
+
const text = typeof value === 'string' ? value.trim() : ''
|
|
56
|
+
return PUSH_TARGET_MODES.includes(text) ? text : ''
|
|
57
|
+
}
|
|
58
|
+
|
|
38
59
|
/**
|
|
39
60
|
* Normalise an arbitrary overlay payload.
|
|
40
61
|
* @param {any} raw - Parsed overlay content.
|
|
41
|
-
* @returns {{connection: {baseUrl: string, token: string}, push: {token: string, target: {sessionId: string}, model: {provider: string, model: string, reasoningEffort: string}}}} The normalised overlay.
|
|
62
|
+
* @returns {{connection: {baseUrl: string, token: string}, home: {dir: string}, workspace: {enabled: boolean}, push: {token: string, target: {mode: 'archive'|'session'|'', sessionId: string}, model: {provider: string, model: string, reasoningEffort: string}, perception: {minLevel: string}}}} The normalised overlay.
|
|
42
63
|
*/
|
|
43
64
|
export function normalizeOverlay(raw) {
|
|
44
65
|
const root = raw && typeof raw === 'object' ? raw : {}
|
|
45
66
|
const connection = root.connection && typeof root.connection === 'object' ? root.connection : {}
|
|
67
|
+
const home = root.home && typeof root.home === 'object' ? root.home : {}
|
|
68
|
+
const workspace = root.workspace && typeof root.workspace === 'object' ? root.workspace : {}
|
|
46
69
|
const push = root.push && typeof root.push === 'object' ? root.push : {}
|
|
47
70
|
const target = push.target && typeof push.target === 'object' ? push.target : {}
|
|
48
71
|
const model = push.model && typeof push.model === 'object' ? push.model : {}
|
|
@@ -52,10 +75,19 @@ export function normalizeOverlay(raw) {
|
|
|
52
75
|
baseUrl: typeof connection.baseUrl === 'string' ? connection.baseUrl.trim() : '',
|
|
53
76
|
token: typeof connection.token === 'string' ? connection.token : '',
|
|
54
77
|
},
|
|
78
|
+
home: {
|
|
79
|
+
// 空 = 回退 config.workspace.dir,再回退插件默认目录
|
|
80
|
+
dir: typeof home.dir === 'string' ? home.dir.trim() : '',
|
|
81
|
+
},
|
|
82
|
+
workspace: {
|
|
83
|
+
// false = 不注册家庭工作区(会话归到「未分组」);默认开启
|
|
84
|
+
enabled: workspace.enabled !== false,
|
|
85
|
+
},
|
|
55
86
|
push: {
|
|
56
87
|
token: typeof push.token === 'string' ? push.token : '',
|
|
57
88
|
target: {
|
|
58
|
-
|
|
89
|
+
mode: normalizePushTargetMode(target.mode),
|
|
90
|
+
// 空 = 用「上一次触发 miloco 的会话」(仅 session 模式)
|
|
59
91
|
sessionId: typeof target.sessionId === 'string' ? target.sessionId.trim() : '',
|
|
60
92
|
},
|
|
61
93
|
model: {
|
|
@@ -115,14 +147,28 @@ export function createOverlay(options = {}) {
|
|
|
115
147
|
}
|
|
116
148
|
|
|
117
149
|
/**
|
|
118
|
-
* Write the overlay file atomically.
|
|
119
|
-
* @param {any} value - Payload to store.
|
|
150
|
+
* Write the overlay file atomically, merging with the disk state.
|
|
151
|
+
* @param {any} value - Payload to store; omitted sections keep their current value.
|
|
120
152
|
* @returns {Promise<void>} Resolves once the file is written.
|
|
121
153
|
*/
|
|
122
154
|
async function write(value) {
|
|
155
|
+
const existing = normalizeOverlay(await read())
|
|
156
|
+
const incoming = value && typeof value === 'object' ? value : {}
|
|
157
|
+
const merged = normalizeOverlay({
|
|
158
|
+
connection: { ...existing.connection, ...(incoming.connection ?? {}) },
|
|
159
|
+
home: { ...existing.home, ...(incoming.home ?? {}) },
|
|
160
|
+
workspace: { ...existing.workspace, ...(incoming.workspace ?? {}) },
|
|
161
|
+
push: {
|
|
162
|
+
...existing.push,
|
|
163
|
+
...(incoming.push ?? {}),
|
|
164
|
+
target: { ...existing.push.target, ...(incoming.push?.target ?? {}) },
|
|
165
|
+
model: { ...existing.push.model, ...(incoming.push?.model ?? {}) },
|
|
166
|
+
perception: { ...existing.push.perception, ...(incoming.push?.perception ?? {}) },
|
|
167
|
+
},
|
|
168
|
+
})
|
|
123
169
|
await mkdir(dirname(file), { recursive: true })
|
|
124
170
|
const temporary = `${file}.${process.pid}.tmp`
|
|
125
|
-
await writeFile(temporary, `${JSON.stringify(
|
|
171
|
+
await writeFile(temporary, `${JSON.stringify(merged, null, 2)}\n`, { encoding: 'utf8', mode: 0o600 })
|
|
126
172
|
await rename(temporary, file)
|
|
127
173
|
await chmod(file, 0o600).catch(() => {})
|
|
128
174
|
cache = { at: 0, value: undefined }
|
package/lib/paths.js
CHANGED
|
@@ -31,3 +31,21 @@ export function resolveDshHome() {
|
|
|
31
31
|
const raw = String(process.env.DSH_HOME ?? '').trim()
|
|
32
32
|
return raw ? expandHome(raw) : join(homedir(), '.dsh')
|
|
33
33
|
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Resolve the Miloco household workspace directory.
|
|
37
|
+
*
|
|
38
|
+
* The plugin owns this directory: it creates it and registers it as a host
|
|
39
|
+
* Workspace so every conversation opened inside it belongs to the household
|
|
40
|
+
* assistant. The default lives under the plugin's own data directory so the
|
|
41
|
+
* plugin can manage (and clean up) everything it owns.
|
|
42
|
+
*
|
|
43
|
+
* @param {string} [configured] - Explicit directory; empty falls back to the
|
|
44
|
+
* plugin data directory.
|
|
45
|
+
* @returns {string} Absolute household workspace directory.
|
|
46
|
+
*/
|
|
47
|
+
export function resolveMilocoHomeDir(configured) {
|
|
48
|
+
const text = String(configured ?? '').trim()
|
|
49
|
+
if (text) return expandHome(text)
|
|
50
|
+
return join(resolveDshHome(), 'plugin-data', 'dsh-miloco-bridge', 'home')
|
|
51
|
+
}
|