@binblink/wasmnest-plugin-sdk-desktop 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +287 -2
- package/README_en.md +284 -0
- package/bin/cli.js +602 -0
- package/bin/manifest-gen.js +454 -0
- package/bin/manifest-validate.js +432 -0
- package/bin/permission-vocabulary.json +5 -0
- package/bin/plugin-watch.js +331 -0
- package/bin/wasm-hash.js +124 -0
- package/dev-shell/README.md +121 -0
- package/dev-shell/index.html +12 -0
- package/dev-shell/package.json +28 -0
- package/dev-shell/pnpm-workspace.yaml +9 -0
- package/dev-shell/postcss.config.js +6 -0
- package/dev-shell/src/App.vue +90 -0
- package/dev-shell/src/components/LogPanel.vue +90 -0
- package/dev-shell/src/loader.ts +109 -0
- package/dev-shell/src/locale.ts +29 -0
- package/dev-shell/src/locales.ts +50 -0
- package/dev-shell/src/main.ts +46 -0
- package/dev-shell/src/mock/agent-hub-commands.ts +1065 -0
- package/dev-shell/src/mock/agent-hub.ts +42 -0
- package/dev-shell/src/mock/app-env.ts +102 -0
- package/dev-shell/src/mock/commands.ts +54 -0
- package/dev-shell/src/mock/domain-context.ts +46 -0
- package/dev-shell/src/mock/events.ts +16 -0
- package/dev-shell/src/mock/file-transfer.ts +512 -0
- package/dev-shell/src/mock/tauri-ipc.ts +109 -0
- package/dev-shell/src/registry.ts +90 -0
- package/dev-shell/src/shell/components/AppMountSlot.vue +241 -0
- package/dev-shell/src/shell/components/ShellIcon.vue +141 -0
- package/dev-shell/src/shell/components/ShellStatusBar.vue +87 -0
- package/dev-shell/src/shell/components/ShellToolbar.vue +287 -0
- package/dev-shell/src/shell/mount/appSession.ts +45 -0
- package/dev-shell/src/shell/mount/devAppMount.ts +80 -0
- package/dev-shell/src/shell/types.ts +132 -0
- package/dev-shell/src/styles/dev.css +27 -0
- package/dev-shell/src/styles/shell.css +546 -0
- package/dev-shell/src/styles/style.css +1132 -0
- package/dev-shell/src/theme.ts +140 -0
- package/dev-shell/src/vite-env.d.ts +24 -0
- package/dev-shell/tailwind.config.js +98 -0
- package/dev-shell/vite.config.ts +174 -0
- package/dist/index.d.ts +109 -0
- package/dist/index.js +170 -0
- package/dist/types.d.ts +585 -0
- package/dist/types.js +0 -0
- package/dist/vite-plugin.d.ts +25 -0
- package/dist/vite-plugin.js +80 -0
- package/package.json +89 -3
- package/src/config.ts +49 -0
- package/src/global-dialog.ts +169 -0
- package/src/index.ts +80 -0
- package/src/runtime.ts +54 -0
- package/src/types.ts +630 -0
- package/src/ui/MarkdownEditor.vue +520 -0
- package/src/ui/PluginGlobalDialog.vue +401 -0
- package/src/ui/Select.d.ts +36 -0
- package/src/ui/Select.vue +220 -0
- package/src/ui/TerminalInputRail.vue +184 -0
- package/src/ui/apply-select-position.ts +65 -0
- package/src/ui/markdown-editor-syntax.ts +234 -0
- package/src/ui/markdown-editor.d.ts +36 -0
- package/src/ui/plugin-global-dialog.d.ts +12 -0
- package/src/ui/select-position.ts +79 -0
- package/src/ui/terminal-input-rail.d.ts +30 -0
- package/src/ui/zoom-compensation.d.ts +9 -0
- package/src/ui/zoom-compensation.ts +52 -0
- package/src/vite-plugin.ts +124 -0
- package/template/README.md +39 -0
- package/template/package.json +23 -0
- package/template/plugin.json +14 -0
- package/template/rust/Cargo.toml +21 -0
- package/template/rust/src/lib.rs +39 -0
- package/template/src/env.d.ts +24 -0
- package/template/src/index.ts +37 -0
- package/template/tsconfig.json +17 -0
- package/template/vite.config.ts +21 -0
- package/tools/componentize/Cargo.lock +270 -0
- package/tools/componentize/Cargo.toml +15 -0
- package/tools/componentize/src/main.rs +94 -0
|
@@ -0,0 +1,1065 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent-hub 领域命令 mock 实现(2026-10-25 自 agent-hub.ts 拆出)
|
|
3
|
+
*
|
|
4
|
+
* 种子派生状态 / 运行中安装模拟 / 事件推送 / 五域命令注册全部住本文件;
|
|
5
|
+
* agent-hub.ts 只做种子查找门面(getDevMock → 本模块)。
|
|
6
|
+
*/
|
|
7
|
+
import type { DomainContext } from './domain-context'
|
|
8
|
+
|
|
9
|
+
// ==================== 种子派生状态(最小本地形状,插件自有扩展字段见插件 devMockTypes) ====================
|
|
10
|
+
|
|
11
|
+
interface DetectCliInfo {
|
|
12
|
+
installed: boolean | null
|
|
13
|
+
version: string | null
|
|
14
|
+
method: string
|
|
15
|
+
paths: string[]
|
|
16
|
+
dual: boolean
|
|
17
|
+
status: string
|
|
18
|
+
error: string | null
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface InstallRunScript {
|
|
22
|
+
command: string
|
|
23
|
+
output: string[]
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface SkillEntry {
|
|
27
|
+
dir: string
|
|
28
|
+
path: string
|
|
29
|
+
name: string
|
|
30
|
+
description: string | null
|
|
31
|
+
allowedTools: string | null
|
|
32
|
+
files: Array<{ path: string; hash: string | null }>
|
|
33
|
+
hash: string
|
|
34
|
+
error: string | null
|
|
35
|
+
distribution: Record<'claude' | 'pi', { status: string; missingFiles: string[]; staleFiles: string[] }>
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface ProviderPreset {
|
|
39
|
+
id: number
|
|
40
|
+
name: string
|
|
41
|
+
baseUrl: string
|
|
42
|
+
apiStyle: string
|
|
43
|
+
models: string[]
|
|
44
|
+
notes: string | null
|
|
45
|
+
createdAt: number
|
|
46
|
+
updatedAt: number
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
interface UsageSessionRow {
|
|
50
|
+
id: number
|
|
51
|
+
adapter: string
|
|
52
|
+
[key: string]: unknown
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// ==================== 领域种子状态(最小本地形状) ====================
|
|
56
|
+
// 已知字段具名化(mock 内类型安全),未知种子字段经 `[key: string]: unknown`
|
|
57
|
+
// 索引透传——形状真源在插件 `types.ts` / `devMockTypes.ts`,dev-shell 不收录插件包
|
|
58
|
+
|
|
59
|
+
/** 宽松透传基底(替代 Record<string, any>:既有属性有契约,未知字段不逃逸) */
|
|
60
|
+
interface Seed {
|
|
61
|
+
[key: string]: unknown
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** 探测域(AgentHubState 子集) */
|
|
65
|
+
interface DetectionSeed extends Seed {
|
|
66
|
+
envStatus: string
|
|
67
|
+
env: unknown
|
|
68
|
+
authGranted?: boolean
|
|
69
|
+
clis: Record<string, DetectCliInfo>
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** npm 镜像域(InstallDomainState.mirror 子集) */
|
|
73
|
+
interface MirrorSeed extends Seed {
|
|
74
|
+
speed: { status: string }
|
|
75
|
+
npmrc: {
|
|
76
|
+
backupExists?: boolean
|
|
77
|
+
fileRegistry?: string | null
|
|
78
|
+
appliedAt?: number
|
|
79
|
+
restoredAt?: number
|
|
80
|
+
}
|
|
81
|
+
/** 用户自定义源(add-custom-source / remove-custom-source 维护;条目 = { id, url, addedAt }) */
|
|
82
|
+
customSources?: Array<{ id: string; url: string; addedAt?: number | null }>
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** 运行中安装/卸载(ActiveRun 子集) */
|
|
86
|
+
interface ActiveRunSeed extends Seed {
|
|
87
|
+
runId: string
|
|
88
|
+
cli: string
|
|
89
|
+
action: 'install' | 'update' | 'uninstall'
|
|
90
|
+
command: string
|
|
91
|
+
startedAt: number
|
|
92
|
+
cancelRequested: boolean
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** 安装域(InstallDomainState 子集 + 剧本扩展) */
|
|
96
|
+
interface InstallSeed extends Seed {
|
|
97
|
+
runScripts?: Partial<Record<string, InstallRunScript>>
|
|
98
|
+
mirror: MirrorSeed
|
|
99
|
+
active: ActiveRunSeed | null
|
|
100
|
+
last: Seed | null
|
|
101
|
+
updates: Record<string, { outdated: boolean | null; checkedAt?: number } | null>
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Skills 域(SkillsDomainState 子集 + 编辑器扩展) */
|
|
105
|
+
interface SkillsSeed extends Seed {
|
|
106
|
+
skillContents?: Record<string, string>
|
|
107
|
+
skills: SkillEntry[]
|
|
108
|
+
status?: string
|
|
109
|
+
scannedAt?: number | null
|
|
110
|
+
libraryRoot?: string
|
|
111
|
+
localImport?: { path: string; name: string }
|
|
112
|
+
import?: { last: Record<string, unknown> | null }
|
|
113
|
+
github?: { last: Record<string, unknown> | null }
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Claude 桥接视图(ProvidersDomainState 子集) */
|
|
117
|
+
interface ClaudeSeed extends Seed {
|
|
118
|
+
bridge: { providerConfigSh?: boolean }
|
|
119
|
+
env: { authTokenMask: string | null; baseUrl: string; model: string | null }
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** 供应商域(ProvidersDomainState 子集 + 导入/应用扩展) */
|
|
123
|
+
interface ProvidersSeed extends Seed {
|
|
124
|
+
presets: ProviderPreset[]
|
|
125
|
+
claude: ClaudeSeed
|
|
126
|
+
importDiscoveries?: Array<{
|
|
127
|
+
name: string
|
|
128
|
+
baseUrl: string
|
|
129
|
+
apiStyle: string
|
|
130
|
+
models: string[]
|
|
131
|
+
notes: string | null
|
|
132
|
+
}>
|
|
133
|
+
importKeys?: Record<string, string>
|
|
134
|
+
applyFiles?: Record<string, string[]>
|
|
135
|
+
applyBridges?: string[]
|
|
136
|
+
import?: { last: Record<string, unknown> | null }
|
|
137
|
+
apply?: { last: Record<string, unknown> | null }
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** 来源的一个目录(wire 装饰形状:内置默认目录 `removable=false`) */
|
|
141
|
+
interface UsageSourcePathSeed {
|
|
142
|
+
path: string
|
|
143
|
+
removable: boolean
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** 使用统计域状态(UsageDomainState 子集:sources/adapters/home 为自由域) */
|
|
147
|
+
interface UsageStateSeed extends Seed {
|
|
148
|
+
status?: string
|
|
149
|
+
syncedAt?: number
|
|
150
|
+
home?: string
|
|
151
|
+
/** 选目录 mock 的递增计数(每次 pick 换一个演示目录,避免与已登记目录撞上) */
|
|
152
|
+
pickSeq?: number
|
|
153
|
+
/**
|
|
154
|
+
* 来源 = 名称 + 多个目录(与 guest `usage::sources` 现行 wire 同构)
|
|
155
|
+
*
|
|
156
|
+
* `paths` 是**已装饰**的 wire 形态(`[{ path, removable }]`):内置默认目录
|
|
157
|
+
* `removable=false` 不可移除,用户追加的目录 `removable=true`。种子即此形状,
|
|
158
|
+
* 故增删目录时只追加/剔除 `removable:true` 的项,不重新推导默认值。
|
|
159
|
+
*/
|
|
160
|
+
sources?: Array<{ name: string; paths: UsageSourcePathSeed[]; builtin?: boolean; [key: string]: unknown }>
|
|
161
|
+
adapters?: Record<string, unknown>
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** 使用统计容器(usage devMock 形状) */
|
|
165
|
+
interface UsageSeed {
|
|
166
|
+
state: UsageStateSeed
|
|
167
|
+
stats: Record<string, unknown>
|
|
168
|
+
sessions: UsageSessionRow[]
|
|
169
|
+
sessionDetails?: Record<string, Record<string, unknown>>
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
let detection: DetectionSeed | null = null
|
|
173
|
+
let install: InstallSeed | null = null
|
|
174
|
+
let skills: SkillsSeed | null = null
|
|
175
|
+
let providers: ProvidersSeed | null = null
|
|
176
|
+
let usage: UsageSeed | null = null
|
|
177
|
+
|
|
178
|
+
/** 深拷贝(种子 → 工作状态 / 返回值出站,避免外部改动写回种子) */
|
|
179
|
+
function clone<T>(v: T): T {
|
|
180
|
+
// 种子为受控 JSON 数据基;structuredClone 失败(函数/类实例等不可克隆值)时
|
|
181
|
+
// 回退引用拷贝(外部只读使用,不写回)
|
|
182
|
+
try {
|
|
183
|
+
return structuredClone(v)
|
|
184
|
+
} catch {
|
|
185
|
+
return v
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** `~/x` 展开为绝对路径(与 guest `usage::sources::normalize_path` 同口径) */
|
|
190
|
+
function expandHome(raw: string, home: string): string {
|
|
191
|
+
if (!raw) return ''
|
|
192
|
+
return raw.startsWith('~/') ? `${home}/${raw.slice(2)}` : raw
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* 目录是否已被任一来源登记(全局唯一)
|
|
197
|
+
*
|
|
198
|
+
* 与 guest `path_registered` 同口径:同一目录归属两个来源会让同一批会话文件
|
|
199
|
+
* 以两个适配器名各入一次库,统计重复,故 mock 也要拦(此前该查重用 `s.path`
|
|
200
|
+
* 字段,seed 早已改用 `paths` 数组 → 比较恒不成立,重复检查形同虚设)。
|
|
201
|
+
*/
|
|
202
|
+
function pathRegistered(
|
|
203
|
+
sources: Array<{ paths?: UsageSourcePathSeed[] }>,
|
|
204
|
+
path: string,
|
|
205
|
+
): boolean {
|
|
206
|
+
return sources.some((s) => (s.paths ?? []).some((p) => p.path === path))
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** FNV-1a 32bit hex(guest skills.rs 同款逐文件 hash 算法) */
|
|
210
|
+
function fnv1aHex(text: string): string {
|
|
211
|
+
let h = 0x811c9dc5
|
|
212
|
+
for (let i = 0; i < text.length; i++) {
|
|
213
|
+
h ^= text.charCodeAt(i)
|
|
214
|
+
h = Math.imul(h, 0x01000193)
|
|
215
|
+
}
|
|
216
|
+
return (h >>> 0).toString(16).padStart(8, '0')
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** key 掩码(与 guest 掩码形态一致:前 5 位 + *** + 尾 4 位) */
|
|
220
|
+
function maskKey(key: string): string {
|
|
221
|
+
if (key.length <= 9) return 'sk-****'
|
|
222
|
+
return `${key.slice(0, 5)}***${key.slice(-4)}`
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const NPMMIRROR = 'https://registry.npmmirror.com'
|
|
226
|
+
const NPMJS = 'https://registry.npmjs.org'
|
|
227
|
+
|
|
228
|
+
// ==================== 运行中安装/卸载模拟(单 run) ====================
|
|
229
|
+
|
|
230
|
+
let runTimer: ReturnType<typeof setInterval> | null = null
|
|
231
|
+
let runState: {
|
|
232
|
+
runId: string
|
|
233
|
+
cli: string
|
|
234
|
+
action: 'install' | 'update' | 'uninstall'
|
|
235
|
+
command: string
|
|
236
|
+
useMirror: boolean
|
|
237
|
+
startedAt: number
|
|
238
|
+
cancelRequested: boolean
|
|
239
|
+
lines: string[]
|
|
240
|
+
cursor: number
|
|
241
|
+
} | null = null
|
|
242
|
+
let runSeq = 0
|
|
243
|
+
|
|
244
|
+
function stopRunTimer(): void {
|
|
245
|
+
if (runTimer !== null) {
|
|
246
|
+
clearInterval(runTimer)
|
|
247
|
+
runTimer = null
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** 卸载剧本(与安装白名单同源包名;claude native/opencode standalone 官方命令) */
|
|
252
|
+
const UNINSTALL_SCRIPTS: Record<string, { command: string; output: string[] }> = {
|
|
253
|
+
claude: {
|
|
254
|
+
command: 'npm uninstall -g @anthropic-ai/claude-code',
|
|
255
|
+
output: ['$ npm uninstall -g @anthropic-ai/claude-code', '', 'removed 1 package in 3s'],
|
|
256
|
+
},
|
|
257
|
+
codex: {
|
|
258
|
+
command: 'npm uninstall -g @openai/codex',
|
|
259
|
+
output: ['$ npm uninstall -g @openai/codex', '', 'removed 1 package in 4s'],
|
|
260
|
+
},
|
|
261
|
+
opencode: {
|
|
262
|
+
command: 'opencode uninstall --force',
|
|
263
|
+
output: ['$ opencode uninstall --force', '', 'opencode removed'],
|
|
264
|
+
},
|
|
265
|
+
pi: {
|
|
266
|
+
command: 'npm uninstall -g @earendil-works/pi-coding-agent',
|
|
267
|
+
output: ['$ npm uninstall -g @earendil-works/pi-coding-agent', '', 'removed 1 package in 3s'],
|
|
268
|
+
},
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
interface RunSpec {
|
|
272
|
+
cli: string
|
|
273
|
+
action: 'install' | 'update' | 'uninstall'
|
|
274
|
+
command: string
|
|
275
|
+
useMirror: boolean
|
|
276
|
+
lines: string[]
|
|
277
|
+
/** run 播完后的额外副作用(卸载:翻转探测为未安装) */
|
|
278
|
+
onDone?: () => void
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** run 模拟:每 tick 回放一行剧本输出,播完置终态并推送
|
|
282
|
+
* @returns 新 run 的 runId(启动失败时返回 null) */
|
|
283
|
+
function startRunWith(context: DomainContext, spec: RunSpec): string | null {
|
|
284
|
+
runState = {
|
|
285
|
+
runId: `mock-run-${++runSeq}`,
|
|
286
|
+
cli: spec.cli,
|
|
287
|
+
action: spec.action,
|
|
288
|
+
command: spec.command,
|
|
289
|
+
useMirror: spec.useMirror,
|
|
290
|
+
startedAt: Date.now(),
|
|
291
|
+
cancelRequested: false,
|
|
292
|
+
lines: [...spec.lines],
|
|
293
|
+
cursor: 0,
|
|
294
|
+
}
|
|
295
|
+
install!.active = {
|
|
296
|
+
runId: runState.runId,
|
|
297
|
+
cli: runState.cli,
|
|
298
|
+
action: runState.action,
|
|
299
|
+
command: runState.command,
|
|
300
|
+
useMirror: runState.useMirror,
|
|
301
|
+
startedAt: runState.startedAt,
|
|
302
|
+
cancelRequested: false,
|
|
303
|
+
} as any
|
|
304
|
+
emitInstall(context)
|
|
305
|
+
|
|
306
|
+
runTimer = setInterval(() => {
|
|
307
|
+
if (!runState || !install) return
|
|
308
|
+
runState.cursor++
|
|
309
|
+
const done = runState.cursor >= runState.lines.length
|
|
310
|
+
const output = runState.lines.slice(0, runState.cursor).join('\n')
|
|
311
|
+
if (done) {
|
|
312
|
+
install.last = {
|
|
313
|
+
cli: runState.cli,
|
|
314
|
+
action: runState.action,
|
|
315
|
+
command: runState.command,
|
|
316
|
+
ok: true,
|
|
317
|
+
cancelled: false,
|
|
318
|
+
exitCode: 0,
|
|
319
|
+
timedOut: false,
|
|
320
|
+
error: null,
|
|
321
|
+
output,
|
|
322
|
+
finishedAt: Date.now(),
|
|
323
|
+
} as any
|
|
324
|
+
install.active = null
|
|
325
|
+
const onDone = spec.onDone
|
|
326
|
+
runState = null
|
|
327
|
+
stopRunTimer()
|
|
328
|
+
onDone?.()
|
|
329
|
+
} else if (install.active) {
|
|
330
|
+
install.active.cancelRequested = runState.cancelRequested
|
|
331
|
+
}
|
|
332
|
+
emitInstall(context)
|
|
333
|
+
}, 700)
|
|
334
|
+
return runState.runId
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/** 安装/更新 run(剧本来自种子 runScripts;action 按 outdated 判定) */
|
|
338
|
+
function startRun(context: DomainContext, cli: string, useMirror: boolean): string | null {
|
|
339
|
+
const script = install?.runScripts?.[cli]
|
|
340
|
+
if (!script) return null
|
|
341
|
+
const updates = (install?.updates ?? {}) as Record<string, { outdated: boolean | null }>
|
|
342
|
+
return startRunWith(context, {
|
|
343
|
+
cli,
|
|
344
|
+
action: updates[cli]?.outdated ? 'update' : 'install',
|
|
345
|
+
command: useMirror ? `${script.command} --registry=${NPMMIRROR}` : script.command,
|
|
346
|
+
useMirror,
|
|
347
|
+
lines: [...script.output],
|
|
348
|
+
})
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/** 卸载 run:剧本固定(无用户输入),播完翻转探测为未安装(与 guest 完成
|
|
352
|
+
* 后自动重探测同效) */
|
|
353
|
+
function startUninstallRun(context: DomainContext, cli: string): string | null {
|
|
354
|
+
const script = UNINSTALL_SCRIPTS[cli]
|
|
355
|
+
if (!script) return null
|
|
356
|
+
return startRunWith(context, {
|
|
357
|
+
cli,
|
|
358
|
+
action: 'uninstall',
|
|
359
|
+
command: script.command,
|
|
360
|
+
useMirror: false,
|
|
361
|
+
lines: [...script.output],
|
|
362
|
+
onDone: () => {
|
|
363
|
+
if (detection?.clis?.[cli]) {
|
|
364
|
+
detection.clis[cli] = {
|
|
365
|
+
installed: false,
|
|
366
|
+
version: null,
|
|
367
|
+
method: 'unknown',
|
|
368
|
+
paths: [],
|
|
369
|
+
dual: false,
|
|
370
|
+
status: 'not-installed',
|
|
371
|
+
error: null,
|
|
372
|
+
}
|
|
373
|
+
emitDetection(context)
|
|
374
|
+
}
|
|
375
|
+
},
|
|
376
|
+
})
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
function cancelRun(context: DomainContext): void {
|
|
380
|
+
if (!runState || !install) return
|
|
381
|
+
stopRunTimer()
|
|
382
|
+
install.last = {
|
|
383
|
+
cli: runState.cli,
|
|
384
|
+
action: runState.action,
|
|
385
|
+
command: runState.command,
|
|
386
|
+
ok: false,
|
|
387
|
+
cancelled: true,
|
|
388
|
+
exitCode: null,
|
|
389
|
+
timedOut: false,
|
|
390
|
+
error: null,
|
|
391
|
+
output: runState.lines.slice(0, runState.cursor).join('\n') + '\n^C cancelled',
|
|
392
|
+
finishedAt: Date.now(),
|
|
393
|
+
} as any
|
|
394
|
+
install.active = null
|
|
395
|
+
runState = null
|
|
396
|
+
emitInstall(context)
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// ==================== 事件推送(全量状态,与 guest emit 同形) ====================
|
|
400
|
+
|
|
401
|
+
function emitDetection(context: DomainContext): void {
|
|
402
|
+
if (detection) context.events.emit('plugin:agent-hub:detection', clone(detection))
|
|
403
|
+
}
|
|
404
|
+
function emitInstall(context: DomainContext): void {
|
|
405
|
+
if (install) {
|
|
406
|
+
const { runScripts: _scripts, ...state } = install
|
|
407
|
+
context.events.emit('plugin:agent-hub:install', clone(state))
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
function emitSkills(context: DomainContext): void {
|
|
411
|
+
if (skills) {
|
|
412
|
+
const { skillContents: _c, localImport: _l, ...state } = skills
|
|
413
|
+
context.events.emit('plugin:agent-hub:skills', clone(state))
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
function emitProviders(context: DomainContext): void {
|
|
417
|
+
if (providers) {
|
|
418
|
+
const {
|
|
419
|
+
importDiscoveries: _d,
|
|
420
|
+
importKeys: _k,
|
|
421
|
+
applyFiles: _f,
|
|
422
|
+
applyBridges: _b,
|
|
423
|
+
...state
|
|
424
|
+
} = providers
|
|
425
|
+
context.events.emit('plugin:agent-hub:providers', clone(state))
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
function emitUsage(context: DomainContext): void {
|
|
429
|
+
if (usage) context.events.emit('plugin:agent-hub:usage', clone(usage.state))
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// ==================== 命令 handler ====================
|
|
433
|
+
|
|
434
|
+
function registerCommands(context: DomainContext): void {
|
|
435
|
+
// ==================== 探测域 ====================
|
|
436
|
+
if (detection) {
|
|
437
|
+
context.commands.register('agent-hub.get-state', () => ({ state: clone(detection) }))
|
|
438
|
+
// 探测动画:env + 全 CLI 置 detecting → 每 500ms 逐 CLI 落终态 → env 终态
|
|
439
|
+
context.commands.register('agent-hub.detect', () => {
|
|
440
|
+
// 早退守卫:模块级 detection 在闭包内 TS 收窄失效(setTimeout 里还有
|
|
441
|
+
// `if (!detection) return` 双保险);种子缺失时返回错误而非崩溃
|
|
442
|
+
if (!detection) return { ok: false, error: 'detection seed unavailable' }
|
|
443
|
+
const final = clone(detection)
|
|
444
|
+
const detecting = clone(final)
|
|
445
|
+
detecting.envStatus = 'detecting'
|
|
446
|
+
for (const k of Object.keys(detecting.clis)) detecting.clis[k] = { ...detecting.clis[k], status: 'detecting' }
|
|
447
|
+
detection = detecting
|
|
448
|
+
emitDetection(context)
|
|
449
|
+
const cliIds = Object.keys(final.clis)
|
|
450
|
+
cliIds.forEach((id, i) => {
|
|
451
|
+
timers.push(
|
|
452
|
+
setTimeout(() => {
|
|
453
|
+
if (!detection) return
|
|
454
|
+
detection.clis[id] = final.clis[id]
|
|
455
|
+
emitDetection(context)
|
|
456
|
+
}, 500 + i * 500),
|
|
457
|
+
)
|
|
458
|
+
})
|
|
459
|
+
timers.push(
|
|
460
|
+
setTimeout(() => {
|
|
461
|
+
if (!detection) return
|
|
462
|
+
detection.envStatus = final.envStatus
|
|
463
|
+
detection.env = final.env
|
|
464
|
+
emitDetection(context)
|
|
465
|
+
}, 600 + cliIds.length * 500),
|
|
466
|
+
)
|
|
467
|
+
return { ok: true }
|
|
468
|
+
})
|
|
469
|
+
context.commands.register('agent-hub.request-auth', () => {
|
|
470
|
+
detection!.authGranted = true
|
|
471
|
+
emitDetection(context)
|
|
472
|
+
return { ok: true }
|
|
473
|
+
})
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
// ==================== 安装域 ====================
|
|
477
|
+
if (install) {
|
|
478
|
+
context.commands.register('agent-hub.get-install-state', () => {
|
|
479
|
+
const { runScripts: _scripts, ...state } = install!
|
|
480
|
+
return { state: clone(state) }
|
|
481
|
+
})
|
|
482
|
+
context.commands.register('agent-hub.get-run-output', () => {
|
|
483
|
+
if (!runState) return { status: 'idle', output: null }
|
|
484
|
+
return {
|
|
485
|
+
status: 'running',
|
|
486
|
+
cli: runState.cli,
|
|
487
|
+
action: runState.action,
|
|
488
|
+
command: runState.command,
|
|
489
|
+
output: runState.lines.slice(0, runState.cursor).join('\n'),
|
|
490
|
+
}
|
|
491
|
+
})
|
|
492
|
+
context.commands.register('agent-hub.describe-install', (args: any) => {
|
|
493
|
+
const script = install?.runScripts?.[args?.cli]
|
|
494
|
+
if (!script) return Promise.reject(new Error(`no install recipe for cli: ${args?.cli}`))
|
|
495
|
+
return {
|
|
496
|
+
command: args?.mirror ? `${script.command} --registry=${NPMMIRROR}` : script.command,
|
|
497
|
+
}
|
|
498
|
+
})
|
|
499
|
+
context.commands.register('agent-hub.speed-test', () => {
|
|
500
|
+
const seedSpeed = clone(install!.mirror.speed)
|
|
501
|
+
seedSpeed.status = 'testing'
|
|
502
|
+
install!.mirror.speed = seedSpeed
|
|
503
|
+
emitInstall(context)
|
|
504
|
+
return new Promise((resolve) => {
|
|
505
|
+
timers.push(
|
|
506
|
+
setTimeout(() => {
|
|
507
|
+
if (!install) return
|
|
508
|
+
install.mirror.speed = { ...clone(install.mirror.speed), status: 'ok' }
|
|
509
|
+
emitInstall(context)
|
|
510
|
+
resolve({ ok: true })
|
|
511
|
+
}, 1400),
|
|
512
|
+
)
|
|
513
|
+
})
|
|
514
|
+
})
|
|
515
|
+
context.commands.register('agent-hub.apply-mirror', (args: any) => {
|
|
516
|
+
install!.mirror.npmrc = {
|
|
517
|
+
...install!.mirror.npmrc,
|
|
518
|
+
backupExists: true,
|
|
519
|
+
fileRegistry: args?.target === 'npmjs' ? NPMJS : NPMMIRROR,
|
|
520
|
+
appliedAt: Date.now(),
|
|
521
|
+
}
|
|
522
|
+
emitInstall(context)
|
|
523
|
+
return { ok: true }
|
|
524
|
+
})
|
|
525
|
+
context.commands.register('agent-hub.restore-npmrc', () => {
|
|
526
|
+
install!.mirror.npmrc = {
|
|
527
|
+
...install!.mirror.npmrc,
|
|
528
|
+
backupExists: false,
|
|
529
|
+
fileRegistry: null,
|
|
530
|
+
restoredAt: Date.now(),
|
|
531
|
+
}
|
|
532
|
+
emitInstall(context)
|
|
533
|
+
return { ok: true }
|
|
534
|
+
})
|
|
535
|
+
// 自定义源增删(mirror.customSources;条目 = { id, url, addedAt })
|
|
536
|
+
context.commands.register('agent-hub.add-custom-source', (args: any) => {
|
|
537
|
+
const url: string = String(args?.url ?? '').trim()
|
|
538
|
+
if (!url) return Promise.reject(new Error('add-custom-source: url required'))
|
|
539
|
+
const list = install!.mirror.customSources ?? (install!.mirror.customSources = [])
|
|
540
|
+
if (!list.some((s) => s.url === url)) {
|
|
541
|
+
list.push({ id: `custom-${list.length + 1}`, url, addedAt: Date.now() })
|
|
542
|
+
}
|
|
543
|
+
emitInstall(context)
|
|
544
|
+
return { ok: true }
|
|
545
|
+
})
|
|
546
|
+
context.commands.register('agent-hub.remove-custom-source', (args: any) => {
|
|
547
|
+
const url: string = String(args?.url ?? '')
|
|
548
|
+
const list = install!.mirror.customSources ?? []
|
|
549
|
+
const before = list.length
|
|
550
|
+
install!.mirror.customSources = list.filter((s) => s.url !== url)
|
|
551
|
+
emitInstall(context)
|
|
552
|
+
return { removed: before - install!.mirror.customSources.length }
|
|
553
|
+
})
|
|
554
|
+
// 模型列表查询:真机由 guest 代发 GET(浏览器无外网)→ 回演示模型列表,
|
|
555
|
+
// 形状与 guest 的 FetchModelsResult 同构(models[] 非空,否则 app 判失败)
|
|
556
|
+
context.commands.register('agent-hub.fetch-models', (args: any) => {
|
|
557
|
+
const url: string = String(args?.url ?? '')
|
|
558
|
+
const models = ['gpt-4o', 'gpt-4o-mini', 'claude-sonnet-4']
|
|
559
|
+
return { models, url, count: models.length }
|
|
560
|
+
})
|
|
561
|
+
context.commands.register('agent-hub.check-updates', () => {
|
|
562
|
+
return new Promise((resolve) => {
|
|
563
|
+
timers.push(
|
|
564
|
+
setTimeout(() => {
|
|
565
|
+
if (!install) return
|
|
566
|
+
// checkedAt 刷新为本次检查时间(outdated 判定沿用种子)
|
|
567
|
+
for (const k of Object.keys(install.updates)) {
|
|
568
|
+
if (install.updates[k]) install.updates[k].checkedAt = Date.now()
|
|
569
|
+
}
|
|
570
|
+
emitInstall(context)
|
|
571
|
+
resolve({ ok: true })
|
|
572
|
+
}, 1200),
|
|
573
|
+
)
|
|
574
|
+
})
|
|
575
|
+
})
|
|
576
|
+
context.commands.register('agent-hub.install', (args: any) => {
|
|
577
|
+
const cli: string = args?.cli ?? ''
|
|
578
|
+
if (!install?.runScripts?.[cli]) {
|
|
579
|
+
return Promise.reject(new Error(`no install recipe for cli: ${cli}`))
|
|
580
|
+
}
|
|
581
|
+
if (runState) return Promise.reject(new Error('another run is active'))
|
|
582
|
+
// startRun 同步落 runState 并返回新 runId(无剧本时 null)——避免
|
|
583
|
+
// 早退收窄后 `runState?.runId` 落在 never 上(TS 不跟踪跨函数赋值)
|
|
584
|
+
const runId = startRun(context, cli, !!args?.mirror)
|
|
585
|
+
return { runId }
|
|
586
|
+
})
|
|
587
|
+
context.commands.register('agent-hub.uninstall', (args: any) => {
|
|
588
|
+
const cli: string = args?.cli ?? ''
|
|
589
|
+
if (!UNINSTALL_SCRIPTS[cli]) {
|
|
590
|
+
return Promise.reject(new Error(`no uninstall recipe for cli: ${cli}`))
|
|
591
|
+
}
|
|
592
|
+
if (runState) return Promise.reject(new Error('another run is active'))
|
|
593
|
+
const runId = startUninstallRun(context, cli)
|
|
594
|
+
return { runId }
|
|
595
|
+
})
|
|
596
|
+
context.commands.register('agent-hub.cancel-run', () => {
|
|
597
|
+
cancelRun(context)
|
|
598
|
+
return { ok: true }
|
|
599
|
+
})
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
// ==================== Skills 域 ====================
|
|
603
|
+
if (skills) {
|
|
604
|
+
context.commands.register('agent-hub.get-skills-state', () => {
|
|
605
|
+
const { skillContents: _c, localImport: _l, ...state } = skills!
|
|
606
|
+
return { state: clone(state) }
|
|
607
|
+
})
|
|
608
|
+
context.commands.register('agent-hub.scan-skills', () => {
|
|
609
|
+
skills!.status = 'scanning'
|
|
610
|
+
emitSkills(context)
|
|
611
|
+
return new Promise((resolve) => {
|
|
612
|
+
timers.push(
|
|
613
|
+
setTimeout(() => {
|
|
614
|
+
if (!skills) return
|
|
615
|
+
skills.status = 'ready'
|
|
616
|
+
skills.scannedAt = Date.now()
|
|
617
|
+
emitSkills(context)
|
|
618
|
+
resolve({ ok: true })
|
|
619
|
+
}, 900),
|
|
620
|
+
)
|
|
621
|
+
})
|
|
622
|
+
})
|
|
623
|
+
context.commands.register('agent-hub.read-skill', (args: any) => {
|
|
624
|
+
const entry = (skills!.skills as SkillEntry[]).find((s) => s.dir === args?.dir)
|
|
625
|
+
if (!entry) return Promise.reject(new Error(`skill not found: ${args?.dir}`))
|
|
626
|
+
const content = skills!.skillContents?.[args.dir] ?? ''
|
|
627
|
+
return {
|
|
628
|
+
dir: entry.dir,
|
|
629
|
+
name: entry.name,
|
|
630
|
+
description: entry.description,
|
|
631
|
+
allowedTools: entry.allowedTools,
|
|
632
|
+
content,
|
|
633
|
+
}
|
|
634
|
+
})
|
|
635
|
+
// 保存:baseContent 与磁盘现状不一致且未 force → 冲突回磁盘现状;
|
|
636
|
+
// 成功保存重算 SKILL.md hash 并把已分发目标转 stale(模拟副本落后检测)
|
|
637
|
+
context.commands.register('agent-hub.save-skill', (args: any) => {
|
|
638
|
+
const dir: string = args?.dir ?? ''
|
|
639
|
+
const contents = skills!.skillContents ?? (skills!.skillContents = {})
|
|
640
|
+
const current = contents[dir]
|
|
641
|
+
if (typeof current === 'string' && args?.baseContent !== current && !args?.force) {
|
|
642
|
+
return { saved: false, conflict: true, current }
|
|
643
|
+
}
|
|
644
|
+
contents[dir] = args?.content ?? ''
|
|
645
|
+
const entry = (skills!.skills as SkillEntry[]).find((s) => s.dir === dir)
|
|
646
|
+
if (entry) {
|
|
647
|
+
const file = entry.files.find((f) => f.path === 'SKILL.md')
|
|
648
|
+
if (file) file.hash = fnv1aHex(args.content ?? '')
|
|
649
|
+
entry.hash = fnv1aHex(args.content ?? '')
|
|
650
|
+
for (const target of Object.values(entry.distribution)) {
|
|
651
|
+
if (target.status === 'distributed') {
|
|
652
|
+
target.status = 'stale'
|
|
653
|
+
if (!target.staleFiles.includes('SKILL.md')) target.staleFiles.push('SKILL.md')
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
return new Promise((resolve) => {
|
|
658
|
+
timers.push(setTimeout(() => { emitSkills(context); resolve({ saved: true }) }, 300))
|
|
659
|
+
})
|
|
660
|
+
})
|
|
661
|
+
context.commands.register('agent-hub.distribute-skill', (args: any) => {
|
|
662
|
+
const dir: string = args?.dir ?? ''
|
|
663
|
+
const targets: string[] = Array.isArray(args?.targets) && args.targets.length > 0
|
|
664
|
+
? args.targets
|
|
665
|
+
: ['claude', 'pi']
|
|
666
|
+
return new Promise((resolve) => {
|
|
667
|
+
timers.push(
|
|
668
|
+
setTimeout(() => {
|
|
669
|
+
const entry = skills
|
|
670
|
+
? (skills.skills as SkillEntry[]).find((s) => s.dir === dir)
|
|
671
|
+
: undefined
|
|
672
|
+
if (entry) {
|
|
673
|
+
for (const t of targets) {
|
|
674
|
+
entry.distribution[t as 'claude' | 'pi'] = { status: 'distributed', missingFiles: [], staleFiles: [] }
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
emitSkills(context)
|
|
678
|
+
resolve({ ok: true })
|
|
679
|
+
}, 500),
|
|
680
|
+
)
|
|
681
|
+
})
|
|
682
|
+
})
|
|
683
|
+
// GitHub 安装:url 末段解析 skill 名;同名未覆盖 → exists 分支;
|
|
684
|
+
// 成功追加最小条目(SKILL.md 单文件)并更新 github.last
|
|
685
|
+
context.commands.register('agent-hub.install-github-skill', (args: any) => {
|
|
686
|
+
const url: string = args?.url ?? ''
|
|
687
|
+
let name = ''
|
|
688
|
+
try {
|
|
689
|
+
const seg = new URL(url).pathname.split('/').filter(Boolean).pop() ?? ''
|
|
690
|
+
name = seg.replace(/\.git$/, '').replace(/\s+/g, '-').toLowerCase()
|
|
691
|
+
} catch {
|
|
692
|
+
return Promise.reject(new Error(`invalid github url: ${url}`))
|
|
693
|
+
}
|
|
694
|
+
if (!name) return Promise.reject(new Error(`invalid github url: ${url}`))
|
|
695
|
+
const list = skills!.skills as SkillEntry[]
|
|
696
|
+
if (!args?.overwrite && list.some((s) => s.dir === name)) {
|
|
697
|
+
return { installed: false, exists: [name] }
|
|
698
|
+
}
|
|
699
|
+
const content = skills!.skillContents?.[name] ?? `---\nname: ${name}\ndescription: (GitHub 安装演示)\n---\n\n# ${name}\n`
|
|
700
|
+
const entry: SkillEntry = {
|
|
701
|
+
dir: name,
|
|
702
|
+
path: `${skills!.libraryRoot}/${name}`,
|
|
703
|
+
name,
|
|
704
|
+
description: `(GitHub 安装演示) ${name}`,
|
|
705
|
+
allowedTools: null,
|
|
706
|
+
files: [{ path: 'SKILL.md', hash: fnv1aHex(content) }],
|
|
707
|
+
hash: fnv1aHex(content),
|
|
708
|
+
error: null,
|
|
709
|
+
distribution: {
|
|
710
|
+
claude: { status: 'none', missingFiles: [], staleFiles: [] },
|
|
711
|
+
pi: { status: 'none', missingFiles: [], staleFiles: [] },
|
|
712
|
+
},
|
|
713
|
+
}
|
|
714
|
+
const idx = list.findIndex((s) => s.dir === name)
|
|
715
|
+
if (idx >= 0) list[idx] = entry
|
|
716
|
+
else list.push(entry)
|
|
717
|
+
skills!.github = {
|
|
718
|
+
last: { ok: true, installed: [name], skippedFiles: 0, skipped: [], error: null, at: Date.now() },
|
|
719
|
+
}
|
|
720
|
+
emitSkills(context)
|
|
721
|
+
return { installed: true, names: [name] }
|
|
722
|
+
})
|
|
723
|
+
// 本地导入:无 path → 命中种子演示目录;同名未 force → exists 分支;
|
|
724
|
+
// 成功导入追加/替换条目并更新 import.last
|
|
725
|
+
context.commands.register('agent-hub.import-skill', (args: any) => {
|
|
726
|
+
const seed = skills!.localImport
|
|
727
|
+
if (!args?.path) {
|
|
728
|
+
if (!seed) return { picked: false }
|
|
729
|
+
return { picked: true, path: seed.path, name: seed.name }
|
|
730
|
+
}
|
|
731
|
+
const name: string = args.name ?? args.path.split('/').filter(Boolean).pop() ?? ''
|
|
732
|
+
const list = skills!.skills as SkillEntry[]
|
|
733
|
+
if (!args?.force && list.some((s) => s.dir === name)) {
|
|
734
|
+
return { picked: true, path: args.path, name, exists: true }
|
|
735
|
+
}
|
|
736
|
+
const content = skills!.skillContents?.[name] ?? `# ${name}\n`
|
|
737
|
+
const entry: SkillEntry = {
|
|
738
|
+
dir: name,
|
|
739
|
+
path: `${skills!.libraryRoot}/${name}`,
|
|
740
|
+
name,
|
|
741
|
+
description: `(本地导入) ${name}`,
|
|
742
|
+
allowedTools: null,
|
|
743
|
+
files: [{ path: 'SKILL.md', hash: fnv1aHex(content) }],
|
|
744
|
+
hash: fnv1aHex(content),
|
|
745
|
+
error: null,
|
|
746
|
+
distribution: {
|
|
747
|
+
claude: { status: 'none', missingFiles: [], staleFiles: [] },
|
|
748
|
+
pi: { status: 'none', missingFiles: [], staleFiles: [] },
|
|
749
|
+
},
|
|
750
|
+
}
|
|
751
|
+
const idx = list.findIndex((s) => s.dir === name)
|
|
752
|
+
if (idx >= 0) list[idx] = entry
|
|
753
|
+
else list.push(entry)
|
|
754
|
+
skills!.import = {
|
|
755
|
+
last: { ok: true, name, fileCount: entry.files.length, error: null, at: Date.now() },
|
|
756
|
+
}
|
|
757
|
+
emitSkills(context)
|
|
758
|
+
return { picked: true, path: args.path, name }
|
|
759
|
+
})
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
// ==================== 供应商域 ====================
|
|
763
|
+
if (providers) {
|
|
764
|
+
context.commands.register('agent-hub.get-providers-state', () => {
|
|
765
|
+
const {
|
|
766
|
+
importDiscoveries: _d,
|
|
767
|
+
importKeys: _k,
|
|
768
|
+
applyFiles: _f,
|
|
769
|
+
applyBridges: _b,
|
|
770
|
+
...state
|
|
771
|
+
} = providers!
|
|
772
|
+
return { state: clone(state) }
|
|
773
|
+
})
|
|
774
|
+
context.commands.register('agent-hub.save-preset', (args: any) => {
|
|
775
|
+
const presets = providers!.presets as ProviderPreset[]
|
|
776
|
+
const name: string = args?.name ?? ''
|
|
777
|
+
if (presets.some((p) => p.name === name && p.id !== args?.id)) {
|
|
778
|
+
return { saved: false, nameExists: true }
|
|
779
|
+
}
|
|
780
|
+
if (typeof args?.id === 'number') {
|
|
781
|
+
const p = presets.find((x) => x.id === args.id)
|
|
782
|
+
if (!p) return { saved: false }
|
|
783
|
+
Object.assign(p, {
|
|
784
|
+
name: args.name ?? p.name,
|
|
785
|
+
baseUrl: args.baseUrl ?? p.baseUrl,
|
|
786
|
+
apiStyle: args.apiStyle ?? p.apiStyle,
|
|
787
|
+
models: Array.isArray(args.models) ? args.models : p.models,
|
|
788
|
+
updatedAt: Date.now(),
|
|
789
|
+
})
|
|
790
|
+
} else {
|
|
791
|
+
const id = presets.reduce((m, p) => Math.max(m, p.id), 0) + 1
|
|
792
|
+
presets.push({
|
|
793
|
+
id,
|
|
794
|
+
name,
|
|
795
|
+
baseUrl: args?.baseUrl ?? '',
|
|
796
|
+
apiStyle: args?.apiStyle ?? 'openai',
|
|
797
|
+
models: Array.isArray(args?.models) ? args.models : [],
|
|
798
|
+
notes: null,
|
|
799
|
+
createdAt: Date.now(),
|
|
800
|
+
updatedAt: Date.now(),
|
|
801
|
+
})
|
|
802
|
+
}
|
|
803
|
+
emitProviders(context)
|
|
804
|
+
return { saved: true }
|
|
805
|
+
})
|
|
806
|
+
context.commands.register('agent-hub.delete-preset', (args: any) => {
|
|
807
|
+
const presets = providers!.presets as ProviderPreset[]
|
|
808
|
+
const idx = presets.findIndex((p) => p.id === args?.id)
|
|
809
|
+
if (idx >= 0) presets.splice(idx, 1)
|
|
810
|
+
emitProviders(context)
|
|
811
|
+
return { ok: idx >= 0 }
|
|
812
|
+
})
|
|
813
|
+
// 反向导入:种子发现条目按名去重 → created/skipped + key 掩码
|
|
814
|
+
context.commands.register('agent-hub.import-providers', () => {
|
|
815
|
+
const presets = providers!.presets as ProviderPreset[]
|
|
816
|
+
const created: string[] = []
|
|
817
|
+
const skipped: string[] = []
|
|
818
|
+
for (const d of providers!.importDiscoveries ?? []) {
|
|
819
|
+
if (presets.some((p) => p.name === d.name)) {
|
|
820
|
+
skipped.push(d.name)
|
|
821
|
+
continue
|
|
822
|
+
}
|
|
823
|
+
const id = presets.reduce((m, p) => Math.max(m, p.id), 0) + 1
|
|
824
|
+
presets.push({ ...d, id, createdAt: Date.now(), updatedAt: Date.now() })
|
|
825
|
+
created.push(d.name)
|
|
826
|
+
}
|
|
827
|
+
const keys = clone(providers!.importKeys ?? {})
|
|
828
|
+
const result = { created, skipped, keys }
|
|
829
|
+
providers!.import = { last: { ok: true, ...clone(result), error: null, at: Date.now() } }
|
|
830
|
+
emitProviders(context)
|
|
831
|
+
return result
|
|
832
|
+
})
|
|
833
|
+
// 应用预设:claude 桥接冲突(未 force)→ conflict 分支;成功更新 claude 视图
|
|
834
|
+
context.commands.register('agent-hub.apply-provider', (args: any) => {
|
|
835
|
+
const presets = providers!.presets as ProviderPreset[]
|
|
836
|
+
const preset = presets.find((p) => p.id === args?.id)
|
|
837
|
+
if (!preset) return { applied: false, error: 'preset not found' }
|
|
838
|
+
const target: string = args?.target ?? ''
|
|
839
|
+
const bridges: string[] = providers!.applyBridges ?? []
|
|
840
|
+
if (target === 'claude' && providers!.claude.bridge.providerConfigSh && !args?.force) {
|
|
841
|
+
return { applied: false, bridgeConflict: true, bridges: [...bridges] }
|
|
842
|
+
}
|
|
843
|
+
const key = args?.key ?? {}
|
|
844
|
+
if (key.kind === 'inline' && typeof key.value === 'string') {
|
|
845
|
+
providers!.claude.env.authTokenMask = maskKey(key.value)
|
|
846
|
+
} else if (key.kind === 'none') {
|
|
847
|
+
providers!.claude.env.authTokenMask = null
|
|
848
|
+
}
|
|
849
|
+
providers!.claude.env.baseUrl = preset.baseUrl
|
|
850
|
+
providers!.claude.env.model = preset.models[0] ?? null
|
|
851
|
+
const files: string[] = [...(providers!.applyFiles?.[target] ?? [])]
|
|
852
|
+
providers!.apply = {
|
|
853
|
+
last: {
|
|
854
|
+
ok: true,
|
|
855
|
+
preset: preset.name,
|
|
856
|
+
target: target as any,
|
|
857
|
+
files,
|
|
858
|
+
keyMode: key.kind ?? 'none',
|
|
859
|
+
keyLen: typeof key.value === 'string' ? key.value.length : null,
|
|
860
|
+
error: null,
|
|
861
|
+
at: Date.now(),
|
|
862
|
+
},
|
|
863
|
+
}
|
|
864
|
+
emitProviders(context)
|
|
865
|
+
return { applied: true, files }
|
|
866
|
+
})
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
// ==================== 使用统计域 ====================
|
|
870
|
+
if (usage) {
|
|
871
|
+
context.commands.register('agent-hub.get-usage-state', () => ({ state: clone(usage!.state) }))
|
|
872
|
+
// 扫描:syncing 事件 → 1.6s 后 ok 事件(前端 syncing → ok 自动重拉看板/列表)
|
|
873
|
+
context.commands.register('agent-hub.scan-usage', () => {
|
|
874
|
+
usage!.state = { ...usage!.state, status: 'syncing' }
|
|
875
|
+
emitUsage(context)
|
|
876
|
+
return new Promise((resolve) => {
|
|
877
|
+
timers.push(
|
|
878
|
+
setTimeout(() => {
|
|
879
|
+
if (!usage) return
|
|
880
|
+
usage.state = { ...usage.state, status: 'ok', syncedAt: Date.now() }
|
|
881
|
+
emitUsage(context)
|
|
882
|
+
resolve({ state: clone(usage.state) })
|
|
883
|
+
}, 1600),
|
|
884
|
+
)
|
|
885
|
+
})
|
|
886
|
+
})
|
|
887
|
+
context.commands.register('agent-hub.get-usage-stats', () => clone(usage!.stats))
|
|
888
|
+
// 清空使用数据:会话明细与统计归零,来源清单保留(来源是用户登记的资产,
|
|
889
|
+
// 不是「使用数据」);app 侧读回执里的 state 重绘看板
|
|
890
|
+
context.commands.register('agent-hub.clear-usage-data', () => {
|
|
891
|
+
usage!.sessions = []
|
|
892
|
+
usage!.sessionDetails = {}
|
|
893
|
+
usage!.stats = {}
|
|
894
|
+
usage!.state = { ...usage!.state, status: 'ok', syncedAt: Date.now() }
|
|
895
|
+
emitUsage(context)
|
|
896
|
+
return { state: clone(usage!.state) }
|
|
897
|
+
})
|
|
898
|
+
// 多条件查询:adapter / 关键词 / 时间范围(与 guest list_sessions 语义对齐)
|
|
899
|
+
context.commands.register('agent-hub.list-usage-sessions', (args: any) => {
|
|
900
|
+
const offset = Math.max(0, Number(args?.offset) || 0)
|
|
901
|
+
const limit = Math.min(200, Math.max(1, Number(args?.limit) || 15))
|
|
902
|
+
const adapter: string = args?.adapter ?? ''
|
|
903
|
+
const q: string = String(args?.q ?? '').trim().toLowerCase()
|
|
904
|
+
const from = Number(args?.from) || 0
|
|
905
|
+
const to = Number(args?.to) || Infinity
|
|
906
|
+
const all = usage!.sessions.filter((s) => {
|
|
907
|
+
if (adapter !== '' && s.adapter !== adapter) return false
|
|
908
|
+
if (
|
|
909
|
+
q &&
|
|
910
|
+
![s.title, s.project, s.cli_session_id].some((v) => String(v ?? '').toLowerCase().includes(q))
|
|
911
|
+
)
|
|
912
|
+
return false
|
|
913
|
+
const started = Number(s.started_at) || 0
|
|
914
|
+
if (from && started < from) return false
|
|
915
|
+
if (Number.isFinite(to) && started > to) return false
|
|
916
|
+
return true
|
|
917
|
+
})
|
|
918
|
+
return {
|
|
919
|
+
sessions: clone(all.slice(offset, offset + limit)),
|
|
920
|
+
total: all.length,
|
|
921
|
+
offset,
|
|
922
|
+
limit,
|
|
923
|
+
}
|
|
924
|
+
})
|
|
925
|
+
// 来源清单:内置只读 + 自定义增删 + 每来源多目录(state.sources 持久化,扫描计数合并)
|
|
926
|
+
context.commands.register('agent-hub.list-usage-sources', () => {
|
|
927
|
+
const sources = (usage!.state.sources ?? []).map((s) => {
|
|
928
|
+
const scan = (usage!.state.adapters ?? {})[s.name]
|
|
929
|
+
return scan ? { ...s, scan: clone(scan) } : s
|
|
930
|
+
})
|
|
931
|
+
return { sources: clone(sources) }
|
|
932
|
+
})
|
|
933
|
+
context.commands.register('agent-hub.pick-source-dir', () => {
|
|
934
|
+
// 浏览器里无法弹系统对话框:返回演示目录(项目 .pi/sessions 形态),
|
|
935
|
+
// 供前端完整走「选择 → 派生名 / 下拉选已有来源 → 添加」链路。
|
|
936
|
+
// 首个来源(claude)换个目录,让下拉追加路径在 demo 里看得见区别。
|
|
937
|
+
const home = usage!.state.home ?? '/home/dev'
|
|
938
|
+
const n = (usage!.state.pickSeq = (usage!.state.pickSeq ?? 0) + 1)
|
|
939
|
+
const tail = n === 1 ? 'projects/tauriProject/WasmNest/.pi/sessions' : `work/pick-${n}`
|
|
940
|
+
return { picked: true, path: `${home}/${tail}` }
|
|
941
|
+
})
|
|
942
|
+
context.commands.register('agent-hub.add-usage-source', (args: any) => {
|
|
943
|
+
const name = String(args?.name ?? '').trim()
|
|
944
|
+
const path = expandHome(String(args?.path ?? '').trim(), usage!.state.home ?? '')
|
|
945
|
+
if (!name || !path) return Promise.reject(new Error('add-source: name and path required'))
|
|
946
|
+
if (!/^[a-z][a-z0-9-]{0,31}$/.test(name))
|
|
947
|
+
return Promise.reject(new Error('add-source: invalid name'))
|
|
948
|
+
const srcs = usage!.state.sources ?? []
|
|
949
|
+
if (srcs.some((s) => s.name === name))
|
|
950
|
+
return Promise.reject(new Error('add-source: source name already registered'))
|
|
951
|
+
if (pathRegistered(srcs, path))
|
|
952
|
+
return Promise.reject(new Error('add-source: path already registered'))
|
|
953
|
+
srcs.push({ name, paths: [{ path, removable: true }], builtin: false, kind: 'jsonl' })
|
|
954
|
+
;(usage!.state.adapters ?? {})[name] = {
|
|
955
|
+
files: 0,
|
|
956
|
+
parsed: 0,
|
|
957
|
+
skipped: 0,
|
|
958
|
+
sessions: 0,
|
|
959
|
+
error: null,
|
|
960
|
+
}
|
|
961
|
+
usage!.state = { ...usage!.state }
|
|
962
|
+
emitUsage(context)
|
|
963
|
+
return { state: clone(usage!.state) }
|
|
964
|
+
})
|
|
965
|
+
context.commands.register('agent-hub.add-usage-source-path', (args: any) => {
|
|
966
|
+
const name = String(args?.name ?? '').trim()
|
|
967
|
+
const path = expandHome(String(args?.path ?? '').trim(), usage!.state.home ?? '')
|
|
968
|
+
if (!name || !path) return Promise.reject(new Error('add-source-path: name and path required'))
|
|
969
|
+
const srcs = usage!.state.sources ?? []
|
|
970
|
+
const target = srcs.find((s) => s.name === name)
|
|
971
|
+
if (!target) return Promise.reject(new Error('add-source-path: source not found'))
|
|
972
|
+
if (pathRegistered(srcs, path))
|
|
973
|
+
return Promise.reject(new Error('add-source-path: path already registered'))
|
|
974
|
+
target.paths = [...(target.paths ?? []), { path, removable: true }]
|
|
975
|
+
usage!.state = { ...usage!.state }
|
|
976
|
+
emitUsage(context)
|
|
977
|
+
return { state: clone(usage!.state) }
|
|
978
|
+
})
|
|
979
|
+
context.commands.register('agent-hub.remove-usage-source-path', (args: any) => {
|
|
980
|
+
const name = String(args?.name ?? '').trim()
|
|
981
|
+
const path = expandHome(String(args?.path ?? '').trim(), usage!.state.home ?? '')
|
|
982
|
+
const srcs = usage!.state.sources ?? []
|
|
983
|
+
const target = srcs.find((s) => s.name === name)
|
|
984
|
+
if (!target) return Promise.reject(new Error('remove-source-path: source not found'))
|
|
985
|
+
const hit = (target.paths ?? []).find((p) => p.path === path)
|
|
986
|
+
if (!hit) return Promise.reject(new Error('remove-source-path: path not in source'))
|
|
987
|
+
if (hit.removable === false)
|
|
988
|
+
return Promise.reject(new Error('remove-source-path: builtin path protected'))
|
|
989
|
+
if ((target.paths ?? []).length <= 1)
|
|
990
|
+
return Promise.reject(new Error('remove-source-path: last path protected'))
|
|
991
|
+
target.paths = target.paths.filter((p) => p.path !== path)
|
|
992
|
+
usage!.state = { ...usage!.state }
|
|
993
|
+
emitUsage(context)
|
|
994
|
+
return { state: clone(usage!.state) }
|
|
995
|
+
})
|
|
996
|
+
context.commands.register('agent-hub.remove-usage-source', (args: any) => {
|
|
997
|
+
const name = String(args?.name ?? '')
|
|
998
|
+
const srcs = usage!.state.sources ?? []
|
|
999
|
+
const target = srcs.find((s) => s.name === name)
|
|
1000
|
+
if (!target) return Promise.reject(new Error(`remove-source: ${name} not found`))
|
|
1001
|
+
if (target.builtin) return Promise.reject(new Error('remove-source: builtin sources cannot be removed'))
|
|
1002
|
+
usage!.state.sources = srcs.filter((s) => s.name !== name)
|
|
1003
|
+
delete (usage!.state.adapters ?? {})[name]
|
|
1004
|
+
usage!.state = { ...usage!.state }
|
|
1005
|
+
emitUsage(context)
|
|
1006
|
+
return { state: clone(usage!.state) }
|
|
1007
|
+
})
|
|
1008
|
+
context.commands.register('agent-hub.read-usage-session', (args: any) => {
|
|
1009
|
+
const id = Number(args?.id)
|
|
1010
|
+
const preset = usage!.sessionDetails?.[String(id)] ?? usage!.sessionDetails?.[id]
|
|
1011
|
+
if (preset) return clone(preset)
|
|
1012
|
+
const row = usage!.sessions.find((s) => s.id === id)
|
|
1013
|
+
if (!row) return Promise.reject(new Error(`session not found: ${id}`))
|
|
1014
|
+
return {
|
|
1015
|
+
session: clone(row),
|
|
1016
|
+
events: [],
|
|
1017
|
+
eventsTruncated: false,
|
|
1018
|
+
raw: [],
|
|
1019
|
+
rawTruncated: false,
|
|
1020
|
+
skippedLines: 0,
|
|
1021
|
+
}
|
|
1022
|
+
})
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
/** devMock 种子容器(插件 AgentHubDevMock 的本地镜像,见 wasm-apps/agent-hub/src/devMockTypes.ts) */
|
|
1027
|
+
export interface AgentHubSeed {
|
|
1028
|
+
detection?: DetectionSeed
|
|
1029
|
+
install?: InstallSeed
|
|
1030
|
+
skills?: SkillsSeed
|
|
1031
|
+
providers?: ProvidersSeed
|
|
1032
|
+
usage?: UsageSeed
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
/** 待清理定时器(探测动画/测速/扫描等模拟延时) */
|
|
1036
|
+
let timers: number[] = []
|
|
1037
|
+
|
|
1038
|
+
/**
|
|
1039
|
+
* 注入种子并注册命令(agent-hub.ts 门面的实现体;种子查找已在门面完成)
|
|
1040
|
+
*/
|
|
1041
|
+
export function registerAgentHubMockImpl(
|
|
1042
|
+
context: DomainContext,
|
|
1043
|
+
seed: AgentHubSeed,
|
|
1044
|
+
): {
|
|
1045
|
+
dispose(): void
|
|
1046
|
+
} {
|
|
1047
|
+
detection = seed.detection ? clone(seed.detection) : null
|
|
1048
|
+
install = seed.install ? clone(seed.install) : null
|
|
1049
|
+
skills = seed.skills ? clone(seed.skills) : null
|
|
1050
|
+
providers = seed.providers ? clone(seed.providers) : null
|
|
1051
|
+
usage = seed.usage ? clone(seed.usage) : null
|
|
1052
|
+
|
|
1053
|
+
timers = []
|
|
1054
|
+
registerCommands(context)
|
|
1055
|
+
|
|
1056
|
+
return {
|
|
1057
|
+
dispose() {
|
|
1058
|
+
stopRunTimer()
|
|
1059
|
+
runState = null
|
|
1060
|
+
while (timers.length) clearTimeout(timers.pop()!)
|
|
1061
|
+
detection = install = skills = providers = null
|
|
1062
|
+
usage = null
|
|
1063
|
+
},
|
|
1064
|
+
}
|
|
1065
|
+
}
|