opencode-growth 0.1.1 → 0.1.59
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 +60 -30
- package/package.json +3 -3
- package/src/adapter-opencode/curator-runner.mjs +254 -0
- package/src/adapter-opencode/index.mjs +305 -19
- package/src/adapter-opencode/install.mjs +63 -12
- package/src/adapter-opencode/journey-tool.mjs +57 -0
- package/src/adapter-opencode/memory-tool.mjs +57 -5
- package/src/adapter-opencode/provider-tools.mjs +237 -0
- package/src/adapter-opencode/review-runner.mjs +75 -21
- package/src/adapter-opencode/session-search-tool.mjs +66 -0
- package/src/adapter-opencode/skill-tool.mjs +221 -0
- package/src/cli/commands.mjs +74 -1
- package/src/cli/curator-cmd.mjs +147 -0
- package/src/cli/journey-cmd.mjs +61 -0
- package/src/cli/journey-view.mjs +174 -0
- package/src/cli/main.mjs +172 -25
- package/src/cli/memory-cmd.mjs +165 -0
- package/src/cli/session-index-cmd.mjs +74 -0
- package/src/cli/skill-cmd.mjs +192 -0
- package/src/core/approve.mjs +155 -0
- package/src/core/audit.mjs +51 -0
- package/src/core/background-review.mjs +88 -1
- package/src/core/home.mjs +56 -21
- package/src/core/journey.mjs +298 -0
- package/src/core/lock.mjs +6 -0
- package/src/core/memories.mjs +186 -9
- package/src/core/memory-actions.mjs +73 -18
- package/src/core/session-freshness.mjs +201 -0
- package/src/core/session-index.mjs +467 -0
- package/src/core/session-query.mjs +161 -0
- package/src/core/session-source.mjs +202 -0
- package/src/core/session-tool.mjs +181 -0
- package/src/core/skill-actions.mjs +372 -0
- package/src/core/skill-backup.mjs +95 -0
- package/src/core/skill-lifecycle.mjs +265 -0
- package/src/core/skill-usage.mjs +119 -0
- package/src/core/skills.mjs +28 -2
- package/src/core/sqlite-runtime.mjs +35 -0
- package/src/core/write-approval.mjs +13 -9
- package/src/host/rest-client.mjs +70 -0
- package/src/provider/config.mjs +187 -0
- package/src/provider/openviking-rest.mjs +742 -0
- package/src/provider/runtime.mjs +417 -0
- package/src/provider/wiring.mjs +164 -0
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
// S1 形成面 · `skill` 工具的**定义**(照 `memory-tool.mjs` 形制)。
|
|
2
|
+
//
|
|
3
|
+
// 这一层只做一件事:把宿主给的上下文(谁的会话、有没有确认通道、宿主闸状态)
|
|
4
|
+
// 变成动作层的输入,并**把认账回执渲染出来**。写盘、校验、闸门分流全在
|
|
5
|
+
// `src/core/skill-actions.mjs`,这里不做 IO。
|
|
6
|
+
//
|
|
7
|
+
// 三条与 memory-tool 同款的硬形制:
|
|
8
|
+
// 1. 宿主的 `tool()` 是恒等函数 ⇒ 不 import `@opencode-ai/plugin`;
|
|
9
|
+
// 2. args 里**没有** origin / agent / created_by——模型不能自报身份、不能自贴 agent-created;
|
|
10
|
+
// 3. 注册 fail-soft:拿不到 zod 就返回 `__error`,不炸整个插件(冻结/注入还在)。
|
|
11
|
+
//
|
|
12
|
+
// 认账回执(§6 规则①⑤):成功/失败/待批都渲染 status+name+location/detail——
|
|
13
|
+
// 「跳过认账回执」的变异会让 test/skill-tool.test.mjs ③⑤⑦ 一起红。
|
|
14
|
+
import fs from "node:fs"
|
|
15
|
+
import path from "node:path"
|
|
16
|
+
import { pathToFileURL } from "node:url"
|
|
17
|
+
|
|
18
|
+
import { SKILL_ACTIONS, runSkillAction, skillGateFromHostConfig } from "../core/skill-actions.mjs"
|
|
19
|
+
import { identityFor } from "../core/memory-actions.mjs"
|
|
20
|
+
import { readExternalDirs } from "../core/skills.mjs"
|
|
21
|
+
|
|
22
|
+
/** 宿主的 `tool()` 实测就是恒等函数(`packages/plugin/src/tool.ts`:`return input`)。 */
|
|
23
|
+
const identityTool = (def) => def
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 无人值守的 agent 名单。**部署者配置的东西,不是模型能碰的**——
|
|
27
|
+
* 与 memory-tool 同一份名单,同一套身份推导。
|
|
28
|
+
*/
|
|
29
|
+
export const DEFAULT_BACKGROUND_AGENTS = ["ocg-learn"]
|
|
30
|
+
|
|
31
|
+
export const SKILL_TOOL_DESCRIPTION = [
|
|
32
|
+
"创建与增长跨项目技能(形制照 Hermes skill_manage):skills/<name>/SKILL.md 一目录一技能。",
|
|
33
|
+
"create:新建技能(name 小写连字符;description 必填 ≤60 字符、`Use when …` 句式;正文为技能手册);",
|
|
34
|
+
"patch:在**同一文件**里定点改写(old_string 必须唯一命中);view:读技能正文(记一笔使用账);list:查看现有技能与触发句;",
|
|
35
|
+
"archive:归档技能(只搬不删,进 skills/.archive/)——**必须**带 absorbed_into=<伞技能名> 点名合并去向(伞须已存在)。",
|
|
36
|
+
"写入前会做静态校验(名称/触发句/布局)并回执认账要素(name+location);",
|
|
37
|
+
"同名已存在、校验不通过、宿主禁用或跨来源同名时会明说「未生效/冲突」,不会谎报已交付。",
|
|
38
|
+
].join("")
|
|
39
|
+
|
|
40
|
+
/** 把动作结果渲染成一段模型和人都读得懂的话(认账回执的呈现层)。 */
|
|
41
|
+
export function renderSkillResult(r) {
|
|
42
|
+
if (!r || typeof r !== "object") return "技能操作没有返回结果。"
|
|
43
|
+
const rec = r.receipt ?? {}
|
|
44
|
+
const head = `${rec.status ?? (r.ok === false ? "未生效" : "已交付")}`
|
|
45
|
+
if (r.ok === false) {
|
|
46
|
+
return [`技能操作${head}:${r.error ?? r.reason ?? "未知原因"}`,
|
|
47
|
+
rec.name ? `对象:${rec.name}` : null,
|
|
48
|
+
rec.location ? `位置:${rec.location}` : null,
|
|
49
|
+
rec.detail ? `依据:${rec.detail}` : null,
|
|
50
|
+
].filter(Boolean).join("\n")
|
|
51
|
+
}
|
|
52
|
+
if (r.staged) {
|
|
53
|
+
return `技能提案已排入待批(${r.action}):${rec.name ?? ""}${rec.file ? ` → ${rec.file}` : ""}。它在有人确认前不会生效。`
|
|
54
|
+
}
|
|
55
|
+
switch (r.action) {
|
|
56
|
+
case "list": {
|
|
57
|
+
const items = r.skills ?? []
|
|
58
|
+
if (!items.length) return "没有技能。"
|
|
59
|
+
return [`技能 ${items.length} 个:`, ...items.map((s, i) => `${i + 1}. ${s.name} —— ${s.description ?? "(无 description)"} [${s.location}]`)].join("\n")
|
|
60
|
+
}
|
|
61
|
+
case "view": {
|
|
62
|
+
return [`技能${head}(view)`,
|
|
63
|
+
`对象:${rec.name ?? "?"}`,
|
|
64
|
+
`位置:${rec.location ?? "?"}`,
|
|
65
|
+
"正文:",
|
|
66
|
+
String(r.content ?? ""),
|
|
67
|
+
].join("\n")
|
|
68
|
+
}
|
|
69
|
+
default:
|
|
70
|
+
return [`技能${head}(${r.action})`,
|
|
71
|
+
`对象:${rec.name ?? "?"}`,
|
|
72
|
+
`位置:${rec.location ?? "?"}`,
|
|
73
|
+
rec.createdBy ? `归属:${rec.createdBy}` : null,
|
|
74
|
+
].filter(Boolean).join("\n")
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function buildSkillTool({
|
|
79
|
+
tool,
|
|
80
|
+
z,
|
|
81
|
+
root,
|
|
82
|
+
backgroundAgents = DEFAULT_BACKGROUND_AGENTS,
|
|
83
|
+
foregroundAgents = (process.env.OCG_FOREGROUND_AGENTS ?? "").split(",").map((x) => x.trim()).filter(Boolean),
|
|
84
|
+
/** 部署方声明:这个会话面真的有人能回答确认框吗(与 memory-tool 同款语义)。 */
|
|
85
|
+
interactive = process.env.OCG_INTERACTIVE === "1",
|
|
86
|
+
/**
|
|
87
|
+
* 宿主配置(opencode.json 的解析结果)。给 null 就按「宿主闸全开」走——
|
|
88
|
+
* 回执认账要素里 hostPermission/agentSkillEnabled 由 `skillGateFromHostConfig` 从它推。
|
|
89
|
+
* 三夹具(permission.skill.<name>=deny / 目标 agent tools.skill:false / 跨来源同名)
|
|
90
|
+
* 就在这条链上被判成「未生效/冲突」。
|
|
91
|
+
*/
|
|
92
|
+
hostConfig = null,
|
|
93
|
+
} = {}) {
|
|
94
|
+
if (!z?.enum || !z?.string) throw new Error("buildSkillTool 需要宿主给的 z")
|
|
95
|
+
const wrap = typeof tool === "function" ? tool : identityTool
|
|
96
|
+
if (!root) throw new Error("buildSkillTool 需要 root(D 目录)")
|
|
97
|
+
|
|
98
|
+
return wrap({
|
|
99
|
+
description: SKILL_TOOL_DESCRIPTION,
|
|
100
|
+
args: {
|
|
101
|
+
action: z.enum(SKILL_ACTIONS).describe("要做什么"),
|
|
102
|
+
name: z.string().optional().describe("技能名(小写连字符;create/patch/list 都要)"),
|
|
103
|
+
description: z.string().optional().describe("create 用:`Use when <触发>. <一行行为>.`,≤60 字符"),
|
|
104
|
+
content: z.string().optional().describe("create 用:技能正文(Markdown 手册)"),
|
|
105
|
+
old_string: z.string().optional().describe("patch 用:要替换的原文(必须唯一命中)"),
|
|
106
|
+
new_string: z.string().optional().describe("patch 用:替换后的新文"),
|
|
107
|
+
absorbed_into: z.string().optional().describe("archive 用:合并去向的伞技能名(必须已存在)"),
|
|
108
|
+
// 注意这里**没有** origin / agent / created_by:身份与归属由宿主 ctx 和
|
|
109
|
+
// 部署者名单决定(extra#4),created_by 由动作层按身份写进 frontmatter。
|
|
110
|
+
},
|
|
111
|
+
async execute(args, ctx) {
|
|
112
|
+
const ask = typeof ctx?.ask === "function" ? (input) => ctx.ask(input) : null
|
|
113
|
+
const identity = identityFor({
|
|
114
|
+
agent: ctx?.agent ?? null,
|
|
115
|
+
hasAsk: Boolean(ask),
|
|
116
|
+
interactive,
|
|
117
|
+
backgroundAgents,
|
|
118
|
+
foregroundAgents,
|
|
119
|
+
})
|
|
120
|
+
const name = args?.name ?? ""
|
|
121
|
+
const gate = skillGateFromHostConfig({ hostConfig: hostConfig ?? {}, agent: ctx?.agent ?? null, name })
|
|
122
|
+
const r = await runSkillAction({
|
|
123
|
+
root,
|
|
124
|
+
identity,
|
|
125
|
+
action: args?.action,
|
|
126
|
+
name,
|
|
127
|
+
description: args?.description,
|
|
128
|
+
content: args?.content,
|
|
129
|
+
old_string: args?.old_string,
|
|
130
|
+
new_string: args?.new_string,
|
|
131
|
+
absorbedInto: args?.absorbed_into,
|
|
132
|
+
ask,
|
|
133
|
+
hostPermission: gate.hostPermission,
|
|
134
|
+
agentSkillEnabled: gate.agentSkillEnabled,
|
|
135
|
+
externalDirs: readExternalDirs(root),
|
|
136
|
+
})
|
|
137
|
+
return {
|
|
138
|
+
title: r.ok === false ? "技能操作未完成" : r.staged ? "已排入待批" : "技能已更新",
|
|
139
|
+
output: renderSkillResult(r),
|
|
140
|
+
metadata: {
|
|
141
|
+
ok: r.ok !== false,
|
|
142
|
+
staged: Boolean(r.staged),
|
|
143
|
+
action: r.action ?? null,
|
|
144
|
+
origin: r.origin ?? null,
|
|
145
|
+
receiptStatus: r.receipt?.status ?? null,
|
|
146
|
+
},
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
})
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* 在插件里注册:**动态** import 宿主 zod,失败不炸整个插件。
|
|
154
|
+
* zod 由宿主装在 `<XDG_CONFIG_HOME>/opencode/node_modules/`(插件目录的上一级),
|
|
155
|
+
* 从仓库 `src/` 直接 import 会失败——解析基线不同(真机探针实测)。
|
|
156
|
+
*/
|
|
157
|
+
async function loadZod() {
|
|
158
|
+
try {
|
|
159
|
+
const m = await import("zod")
|
|
160
|
+
const z = m?.z ?? m?.default?.z ?? m?.default
|
|
161
|
+
if (typeof z?.object === "function") return z
|
|
162
|
+
} catch {
|
|
163
|
+
/* 解析基线不对,往下走 */
|
|
164
|
+
}
|
|
165
|
+
const roots = [
|
|
166
|
+
process.env.XDG_CONFIG_HOME,
|
|
167
|
+
process.env.APPDATA,
|
|
168
|
+
process.env.HOME ? path.join(process.env.HOME, ".config") : null,
|
|
169
|
+
].filter(Boolean)
|
|
170
|
+
for (const r of roots) {
|
|
171
|
+
for (const rel of ["opencode/node_modules/zod/index.cjs", "opencode/node_modules/zod/index.js"]) {
|
|
172
|
+
const p = path.join(r, rel)
|
|
173
|
+
try {
|
|
174
|
+
if (!fs.existsSync(p)) continue
|
|
175
|
+
const m = await import(pathToFileURL(p).href)
|
|
176
|
+
const z = m?.z ?? m?.default?.z ?? m?.default
|
|
177
|
+
if (typeof z?.object === "function") return z
|
|
178
|
+
} catch {
|
|
179
|
+
/* 换下一个 */
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return null
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* 宿主配置(`<配置根>/opencode/opencode.json`)的**只读**取用:给认账闸(三夹具)供原料。
|
|
188
|
+
* 读不到 / 坏 JSON ⇒ `{}`(=宿主闸全开),不猜。配置根与 zod 同款(XDG_CONFIG_HOME / APPDATA / ~/.config)。
|
|
189
|
+
*/
|
|
190
|
+
export function readHostConfig() {
|
|
191
|
+
const roots = [
|
|
192
|
+
process.env.XDG_CONFIG_HOME,
|
|
193
|
+
process.env.APPDATA,
|
|
194
|
+
process.env.HOME ? path.join(process.env.HOME, ".config") : null,
|
|
195
|
+
].filter(Boolean)
|
|
196
|
+
for (const r of roots) {
|
|
197
|
+
const p = path.join(r, "opencode", "opencode.json")
|
|
198
|
+
try {
|
|
199
|
+
if (!fs.existsSync(p)) continue
|
|
200
|
+
const cfg = JSON.parse(fs.readFileSync(p, "utf8"))
|
|
201
|
+
if (cfg && typeof cfg === "object") return cfg
|
|
202
|
+
} catch {
|
|
203
|
+
/* 换下一个 / 坏 JSON 当没有 */
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return {}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export async function registerSkillTool({ root, z = null, tool = null, backgroundAgents = DEFAULT_BACKGROUND_AGENTS, foregroundAgents = undefined, interactive = undefined, hostConfig = null } = {}) {
|
|
210
|
+
try {
|
|
211
|
+
const resolvedZ = z ?? (await loadZod())
|
|
212
|
+
if (!resolvedZ) return { __error: "技能工具未能注册:拿不到宿主的 zod(会在插件目录或宿主配置根里找)" }
|
|
213
|
+
return buildSkillTool({
|
|
214
|
+
tool: tool ?? identityTool, z: resolvedZ, root, backgroundAgents, hostConfig: hostConfig ?? null,
|
|
215
|
+
...(foregroundAgents !== undefined ? { foregroundAgents } : {}),
|
|
216
|
+
...(interactive !== undefined ? { interactive } : {}),
|
|
217
|
+
})
|
|
218
|
+
} catch (err) {
|
|
219
|
+
return { __error: `技能工具未能注册:${err?.message ?? err}` }
|
|
220
|
+
}
|
|
221
|
+
}
|
package/src/cli/commands.mjs
CHANGED
|
@@ -22,6 +22,7 @@ import { consumeQueue, queueOffset } from "../learn/consume.mjs"
|
|
|
22
22
|
import { ok, fail, codeForReason } from "./envelope.mjs"
|
|
23
23
|
import { installAll, resolveInstallPaths, uninstallAll } from "../adapter-opencode/install.mjs"
|
|
24
24
|
import { storeHashOf } from "../core/memories.mjs"
|
|
25
|
+
import { listPending, approvePending } from "../core/approve.mjs"
|
|
25
26
|
import { doctorSkillsSection, readExternalDirs, skillsDir } from "../core/skills.mjs"
|
|
26
27
|
|
|
27
28
|
const isDir = (p) => {
|
|
@@ -213,6 +214,7 @@ export function installCmd(opts, ctx) {
|
|
|
213
214
|
opts["dry-run"] ? "dry-run:只是计划,一个字节都没写" : `装好了:${out.shim}`,
|
|
214
215
|
{
|
|
215
216
|
store: out.store,
|
|
217
|
+
...storeProvenance(ctx),
|
|
216
218
|
configDir: out.configDir,
|
|
217
219
|
shim: out.shim,
|
|
218
220
|
pluginDir: out.pluginDir,
|
|
@@ -333,6 +335,62 @@ export function memoryForget(opts, ctx) {
|
|
|
333
335
|
})
|
|
334
336
|
}
|
|
335
337
|
|
|
338
|
+
// 待批的处置面(C8 的第三件:`pending|approve|reject` 三件齐全)。
|
|
339
|
+
// reject 不需要命令:**删掉待批文件即拒绝**(语义已由文件面钉住);这里补齐另外两件——
|
|
340
|
+
// 列出来(人得先看见才能处置)与批准(**必须署名**:没署名不算批准)。
|
|
341
|
+
export function memoryPendingCmd(opts, ctx) {
|
|
342
|
+
const { L } = ctx
|
|
343
|
+
const pending = listPending(L)
|
|
344
|
+
return ok("memory.pending", `${pending.length} staged proposal(s)`, {
|
|
345
|
+
store: L.root,
|
|
346
|
+
pending,
|
|
347
|
+
hint: pending.length ? `approve one: oc-grow --store ${L.root} memory approve <file> --as <who-you-are>` : null,
|
|
348
|
+
})
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
export function memoryApproveCmd(opts, ctx) {
|
|
352
|
+
const { L } = ctx
|
|
353
|
+
const ref = opts._[0]
|
|
354
|
+
if (!ref) return fail("memory.approve", "USAGE", "memory approve <pendingFile|ref> --as <who-you-are>")
|
|
355
|
+
const who = opts.as ?? null
|
|
356
|
+
const r = approvePending(L, String(ref), { approvedBy: who })
|
|
357
|
+
if (r.ok) {
|
|
358
|
+
return ok("memory.approve", `${r.op} applied — approved by ${r.approvedBy}`, {
|
|
359
|
+
file: r.file,
|
|
360
|
+
op: r.op,
|
|
361
|
+
kind: r.kind,
|
|
362
|
+
origin: r.origin,
|
|
363
|
+
approvedBy: r.approvedBy,
|
|
364
|
+
pending: r.pending,
|
|
365
|
+
})
|
|
366
|
+
}
|
|
367
|
+
const codes = {
|
|
368
|
+
usage: "USAGE",
|
|
369
|
+
"not-found": "NOT_FOUND",
|
|
370
|
+
ambiguous: "AMBIGUOUS_PROPOSAL",
|
|
371
|
+
invalid: "INVALID_PROPOSAL",
|
|
372
|
+
"human-approval-required": "HUMAN_APPROVAL_REQUIRED",
|
|
373
|
+
"apply-failed": "APPLY_FAILED",
|
|
374
|
+
busy: "BUSY",
|
|
375
|
+
lost: "LOCK_LOST",
|
|
376
|
+
}
|
|
377
|
+
const said = {
|
|
378
|
+
"not-found": `没有这条待批提案:${ref}`,
|
|
379
|
+
ambiguous: `「${ref}」命中多条待批提案,请写得更具体:${(r.hits ?? []).join(" / ")}`,
|
|
380
|
+
invalid: `这条提案不完整、不能批准:${r.detail ?? ""}`,
|
|
381
|
+
"human-approval-required": "批准需要署名:--as <who-you-are>(没有署名的批准不算批准)",
|
|
382
|
+
"apply-failed": `应用失败、待批文件已保留:${r.detail ?? ""}`,
|
|
383
|
+
busy: "提交锁被占用,稍后重试",
|
|
384
|
+
lost: "提交锁在写入期间易主,本次未生效",
|
|
385
|
+
}
|
|
386
|
+
return fail("memory.approve", codes[r.reason] ?? "REFUSED", said[r.reason] ?? `无法批准:${r.reason}`, {
|
|
387
|
+
file: r.file ?? null,
|
|
388
|
+
files: r.files ?? null,
|
|
389
|
+
hits: r.hits ?? null,
|
|
390
|
+
reason: r.reason,
|
|
391
|
+
})
|
|
392
|
+
}
|
|
393
|
+
|
|
336
394
|
// ── learn ────────────────────────────────────────────────────────────
|
|
337
395
|
|
|
338
396
|
/**
|
|
@@ -402,11 +460,26 @@ export function doctor(opts, ctx) {
|
|
|
402
460
|
const resolved = resolveInstallPaths({ store: ctx?.L?.root ?? null, configDir: opts?.config ? String(opts.config) : null })
|
|
403
461
|
// handler 返回的是 {envelope, exit} 包装(不是信封本身),且 data 是冻结的 ⇒ 造新对象返回
|
|
404
462
|
const env = envOut?.envelope
|
|
405
|
-
|
|
463
|
+
const provenance = storeProvenance(ctx)
|
|
464
|
+
if (env && env.data) return { ...envOut, envelope: { ...env, data: { ...env.data, resolved, ...provenance } } }
|
|
406
465
|
} catch { /* 解析不出就不报,不因此让 doctor 挂掉 */ }
|
|
407
466
|
return envOut
|
|
408
467
|
}
|
|
409
468
|
|
|
469
|
+
/**
|
|
470
|
+
* 「这个 store 是怎么被挑出来的」——三个入口共用一条规则,于是**来源必须跟着回执走**:
|
|
471
|
+
* 否则被压过的那个变量(指向另一个库)谁也看不见,下次就会有人对着错的库排障。
|
|
472
|
+
*/
|
|
473
|
+
function storeProvenance(ctx) {
|
|
474
|
+
const r = ctx?.storeResolution
|
|
475
|
+
if (!r) return {}
|
|
476
|
+
return {
|
|
477
|
+
store_source: r.source ?? null,
|
|
478
|
+
store_via: r.via ?? null,
|
|
479
|
+
store_conflicts: r.conflicts ?? [],
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
410
483
|
/** uninstall:只撤我们写的东西(回执 + shim + 我们追加的那条 skills path),幂等。 */
|
|
411
484
|
export function uninstallCmd(opts, ctx) {
|
|
412
485
|
const { L } = ctx
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
// S4 · G2 独立 curator CLI 的三个动词(`curator prune|run|ledger`)。
|
|
2
|
+
//
|
|
3
|
+
// 判据来源=GOAL §S4「G2:curator 可独立跑(run/prune/ledger),产物与内建 curator 同形」
|
|
4
|
+
// + Extra criterion 5「独立=不依赖交互会话事件触发(机械 prune/ledger 直跑;需模型的整理
|
|
5
|
+
// 允许连 OpenCode 服务——不另造 agent 运行时)」。
|
|
6
|
+
//
|
|
7
|
+
// 动词面(与 Hermes `hermes curator` 对齐的收束记 D117;D114 是初版三动词的对照):
|
|
8
|
+
// · `run`(无 `--consolidate`)=**机械迁移**(老化/归档),核心=
|
|
9
|
+
// `core/skill-lifecycle.applyLifecycleTransitions` —— 与内建 pass 同一个函数,
|
|
10
|
+
// 零宿主零模型即可跑。这与 Hermes 的 `hermes curator run` 同名同义(机械 pass)。
|
|
11
|
+
// · `run --consolidate --host <url>` =带模型的整理 pass,核心=
|
|
12
|
+
// `adapter-opencode/curator-runner.runCuratorPass` —— 与内建触发(会话事件)走同一执行体;
|
|
13
|
+
// 独立跑时**连宿主服务**,不自行起 agent 运行时。对应 Hermes 的 `run --consolidate`。
|
|
14
|
+
// · `prune` =`run`(机械)的**兼容别名**,保留一版并提示迁移(H 无此动词)。
|
|
15
|
+
// · `ledger` =账本读/归档(`core/audit`),产物与内建 pass 写进的是**同一份账本**。
|
|
16
|
+
import { ok, fail } from "./envelope.mjs"
|
|
17
|
+
import { readAudit, compactAudit } from "../core/audit.mjs"
|
|
18
|
+
import { applyLifecycleTransitions } from "../core/skill-lifecycle.mjs"
|
|
19
|
+
import { listCuratorCandidates, runCuratorPass, curatorConsolidateEnabled } from "../adapter-opencode/curator-runner.mjs"
|
|
20
|
+
import { normalizeMessages } from "../adapter-opencode/review-runner.mjs"
|
|
21
|
+
import { createRestClient } from "../host/rest-client.mjs"
|
|
22
|
+
|
|
23
|
+
/** 账本里属于「技能变更」的事件(照 Hermes skill ledger:技能的 create/patch/archive/…)。 */
|
|
24
|
+
const SKILL_EVENT_RE = /^skill[-_]/i
|
|
25
|
+
const DEFAULT_LEDGER_LIMIT = 20
|
|
26
|
+
|
|
27
|
+
const usage = (cmd) =>
|
|
28
|
+
fail(
|
|
29
|
+
cmd,
|
|
30
|
+
"USAGE",
|
|
31
|
+
[
|
|
32
|
+
"usage: oc-grow curator run [--dry-run]",
|
|
33
|
+
" oc-grow curator run --consolidate --host <http://127.0.0.1:PORT> [--session <sid>] [--dry-run]",
|
|
34
|
+
" oc-grow curator prune [--dry-run] (兼容别名,等价于 curator run;建议改用 run)",
|
|
35
|
+
" oc-grow curator ledger [--limit <n>] [--skill <name>] | [--compact --older-than-days <n>]",
|
|
36
|
+
].join(" · "),
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
/** 机械面:老化迁移。零宿主、零模型,产物=库变化 + 时钟 + 审计(三者都走内建同一函数)。 */
|
|
40
|
+
function mechanicalPass(cmd, label, flags, ctx) {
|
|
41
|
+
const dryRun = Boolean(flags["dry-run"])
|
|
42
|
+
const r = applyLifecycleTransitions({ root: ctx.L.root, dryRun, origin: "foreground" })
|
|
43
|
+
if (!r.ok) return fail(cmd, "DEGRADED", `机械维护失败:${r.error ?? "未知原因"}`)
|
|
44
|
+
const msg =
|
|
45
|
+
`${label}${dryRun ? "(dry-run:库/快照/时钟/审计全不动)" : ""}:检查 ${r.checked} · 锚时钟 ${r.seeded} · ` +
|
|
46
|
+
`stale ${r.marked_stale} · 归档 ${r.archived} · 提案 ${r.staged} · 复活 ${r.reactivated}`
|
|
47
|
+
return ok(cmd, msg, r)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* `curator prune` —— `curator run`(机械)的兼容别名:H 没有 `prune` 动词,多一个同名异义的命令面
|
|
52
|
+
* 会让「同名触发不同副作用」,所以机械路径的规范入口是 `run`;这里只保留一版并提示迁移(D117)。
|
|
53
|
+
*/
|
|
54
|
+
export async function curatorPruneCmd(flags, ctx) {
|
|
55
|
+
const r = mechanicalPass("curator.prune", "curator prune", flags, ctx)
|
|
56
|
+
// 兼容别名:产物与 `run` 逐字相同,只多一句迁移提示(信封的 message 在 envelope 下)
|
|
57
|
+
r.envelope.message = `${r.envelope.message} · 提示:curator prune 是兼容别名,规范入口是 oc-grow curator run`
|
|
58
|
+
return r
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* `curator run` —— 与 Hermes 同名同义:**不带 `--consolidate` 就是机械 pass**(零宿主零模型);
|
|
63
|
+
* 带 `--consolidate` 才进模型整理,这时必须给 `--host`(连正在跑的 OpenCode 服务,不自行起 agent 运行时)。
|
|
64
|
+
* `--host` 单独给也算 `--consolidate`(只有模型 pass 需要宿主),避免「给了宿主却什么都没发生」。
|
|
65
|
+
*/
|
|
66
|
+
export async function curatorRunCmd(flags, ctx) {
|
|
67
|
+
const cmd = "curator.run"
|
|
68
|
+
const host = flags.host ?? flags.url ?? null
|
|
69
|
+
const consolidate = Boolean(flags.consolidate) || Boolean(host) || curatorConsolidateEnabled(ctx.L.root)
|
|
70
|
+
if (!consolidate) return mechanicalPass(cmd, "curator run(机械迁移)", flags, ctx)
|
|
71
|
+
if (!host) {
|
|
72
|
+
return fail(
|
|
73
|
+
cmd,
|
|
74
|
+
"USAGE",
|
|
75
|
+
"curator run --consolidate 需要 --host <http://127.0.0.1:PORT> —— 连正在跑的 OpenCode 服务;本命令不自行起 agent 运行时",
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
const dryRun = Boolean(flags["dry-run"])
|
|
79
|
+
const client = createRestClient({ baseUrl: String(host) })
|
|
80
|
+
try {
|
|
81
|
+
let sessionId = flags.session ? String(flags.session) : null
|
|
82
|
+
if (!sessionId) {
|
|
83
|
+
// 独立跑=不需要人在某个交互会话里:自己建一个根会话当父(子会话挂它下面,产物仍落在宿主会话体系里)。
|
|
84
|
+
const created = await client.session.create({ body: {} })
|
|
85
|
+
sessionId = created?.data?.id ?? created?.id ?? null
|
|
86
|
+
if (!sessionId) return fail(cmd, "DEGRADED", "宿主没给出会话 id(POST /session 的返回里没有 id)")
|
|
87
|
+
}
|
|
88
|
+
const skills = listCuratorCandidates(ctx.L.root)
|
|
89
|
+
const r = await runCuratorPass({ client, sessionId, root: ctx.L.root, skills, dryRun })
|
|
90
|
+
if (!r?.ok) return fail(cmd, "DEGRADED", `整理 pass 没跑成:${r?.error ?? "未知原因"}`)
|
|
91
|
+
const messages = normalizeMessages(r.messages)
|
|
92
|
+
const report = [].concat(messages ?? [])
|
|
93
|
+
.filter((m) => (m?.role ?? m?.info?.role) === "assistant")
|
|
94
|
+
.map((m) => {
|
|
95
|
+
if (typeof m?.text === "string" && m.text.trim()) return m.text
|
|
96
|
+
const parts = m?.parts ?? m?.info?.parts ?? []
|
|
97
|
+
return (Array.isArray(parts) ? parts : []).filter((p) => p?.type === "text").map((p) => String(p.text ?? "")).join("\n")
|
|
98
|
+
})
|
|
99
|
+
.filter((t) => t && t.trim())
|
|
100
|
+
.join("\n")
|
|
101
|
+
.trim()
|
|
102
|
+
return ok(
|
|
103
|
+
cmd,
|
|
104
|
+
`curator run --consolidate${dryRun ? "(dry-run)" : ""}:父会话 ${sessionId} · 子会话 ${r.childId} · ` +
|
|
105
|
+
`候选 ${skills.length} 个 · ${report ? "已取回报告" : "未取回文本(子会话没产出 assistant 文本)"}`,
|
|
106
|
+
{ sessionId, childId: r.childId, candidates: skills.length, report: report || null, restriction: r.restriction ?? null },
|
|
107
|
+
)
|
|
108
|
+
} catch (e) {
|
|
109
|
+
return fail(cmd, "DEGRADED", `连宿主失败:${String(e?.message ?? e)}`)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** 账本面:读(newest-first、可按技能过滤、可限量)或按龄归档。 */
|
|
114
|
+
export async function curatorLedgerCmd(flags, ctx) {
|
|
115
|
+
const cmd = "curator.ledger"
|
|
116
|
+
if (flags.compact) {
|
|
117
|
+
const days = flags["older-than-days"] === undefined ? 30 : Number(flags["older-than-days"])
|
|
118
|
+
if (!Number.isFinite(days) || days <= 0) return fail(cmd, "USAGE", "--older-than-days 必须是正数")
|
|
119
|
+
const r = compactAudit(ctx.L, { olderThanDays: days })
|
|
120
|
+
if (!r.ok) return fail(cmd, "USAGE", r.error)
|
|
121
|
+
return ok(
|
|
122
|
+
cmd,
|
|
123
|
+
`curator ledger 按龄归档:搬走 ${r.moved} 条(${r.files} 个文件)· 留 ${r.kept} 条${r.archived ? ` · 归档目录 ${r.archived}` : ""}`,
|
|
124
|
+
r,
|
|
125
|
+
)
|
|
126
|
+
}
|
|
127
|
+
const limitRaw = flags.limit === undefined ? DEFAULT_LEDGER_LIMIT : Number(flags.limit)
|
|
128
|
+
if (!Number.isFinite(limitRaw) || limitRaw < 0) return fail(cmd, "USAGE", "--limit 必须是非负数")
|
|
129
|
+
const { events, skipped } = readAudit(ctx.L, 5000)
|
|
130
|
+
let rows = events.filter((e) => SKILL_EVENT_RE.test(String(e?.event ?? "")))
|
|
131
|
+
if (flags.skill) rows = rows.filter((e) => String(e?.name ?? "") === String(flags.skill))
|
|
132
|
+
rows = rows
|
|
133
|
+
.map((e, i) => ({ e, i }))
|
|
134
|
+
.sort((a, b) => {
|
|
135
|
+
const ta = Date.parse(a.e?.ts ?? "")
|
|
136
|
+
const tb = Date.parse(b.e?.ts ?? "")
|
|
137
|
+
if (Number.isFinite(ta) && Number.isFinite(tb) && ta !== tb) return tb - ta
|
|
138
|
+
return b.i - a.i // 同时刻(或坏 ts)按写入倒序
|
|
139
|
+
})
|
|
140
|
+
.slice(0, limitRaw)
|
|
141
|
+
.map(({ e }) => e)
|
|
142
|
+
if (!rows.length) {
|
|
143
|
+
const scope = flags.skill ? `技能「${flags.skill}」` : "任何技能"
|
|
144
|
+
return ok(cmd, `curator ledger 是空的:账本里没有${scope}的变更条目(跑过 curator prune 或 skill create|patch|archive 才会有)`, { entries: [], skipped })
|
|
145
|
+
}
|
|
146
|
+
return ok(cmd, `curator ledger:${rows.length} 条(newest-first · 读 ${events.length} 条 · 坏行 ${skipped})`, { entries: rows, skipped })
|
|
147
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// 片 3 命令层:journey / journey list —— 全程只读(§5.5-③/⑤)。
|
|
2
|
+
// 人读帧在 message 里(render 走 default 分支吐 message);--json 时 message 是一行摘要,
|
|
3
|
+
// data 是 §5.3 的机读 payload(信封命令面一致优先)。
|
|
4
|
+
import process from "node:process"
|
|
5
|
+
|
|
6
|
+
import { ok, fail } from "./envelope.mjs"
|
|
7
|
+
import { buildJourney } from "../core/journey.mjs"
|
|
8
|
+
import { parseSince, parseReveal, applyFilters, collectRows, renderFrame, renderRowLines, KINDS, STATES } from "./journey-view.mjs"
|
|
9
|
+
|
|
10
|
+
const ALL_STATES = new Set(Object.values(STATES).flat())
|
|
11
|
+
|
|
12
|
+
function build(flags, { L }, argv) {
|
|
13
|
+
const now = Math.floor(Date.now() / 1000)
|
|
14
|
+
let sinceInfo = null
|
|
15
|
+
if (flags.since !== undefined) {
|
|
16
|
+
if (flags.since === true) return { bad: fail(argv[0], "USAGE", "--since 需要值(7d / 24h / ISO 时间)") }
|
|
17
|
+
sinceInfo = parseSince(flags.since, now)
|
|
18
|
+
if (!sinceInfo) return { bad: fail(argv[0], "USAGE", `--since 只接 7d / 24h / ISO 时间,拿到:${flags.since}`) }
|
|
19
|
+
}
|
|
20
|
+
if (flags.kind !== undefined && (flags.kind === true || !KINDS.includes(flags.kind))) {
|
|
21
|
+
return { bad: fail(argv[0], "USAGE", `--kind 只接 ${KINDS.join(" / ")},拿到:${flags.kind}`) }
|
|
22
|
+
}
|
|
23
|
+
if (flags.state !== undefined && (flags.state === true || !ALL_STATES.has(flags.state))) {
|
|
24
|
+
return { bad: fail(argv[0], "USAGE", `--state 只接 ${[...ALL_STATES].join(" / ")},拿到:${flags.state}`) }
|
|
25
|
+
}
|
|
26
|
+
let reveal = 1
|
|
27
|
+
if (flags.reveal !== undefined) {
|
|
28
|
+
reveal = parseReveal(String(flags.reveal))
|
|
29
|
+
if (reveal === null) return { bad: fail(argv[0], "USAGE", `--reveal 只接 0..1,拿到:${flags.reveal}`) }
|
|
30
|
+
}
|
|
31
|
+
const run = { clock: now, args: argv.slice(), identity: "ocg-cli" }
|
|
32
|
+
const payload0 = buildJourney(L, { since: sinceInfo?.since ?? null, sinceLabel: sinceInfo?.label ?? null, now, run })
|
|
33
|
+
const payload = applyFilters(payload0, { kind: flags.kind ?? null, state: flags.state ?? null })
|
|
34
|
+
return { payload, sinceInfo, reveal, now }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function journeyCmd(flags, ctx) {
|
|
38
|
+
const argv = ["journey", ...(flags._ ?? [])]
|
|
39
|
+
const b = build(flags, ctx, argv)
|
|
40
|
+
if (b.bad) return b.bad
|
|
41
|
+
const frame = renderFrame(b.payload, { since: b.sinceInfo, reveal: b.reveal })
|
|
42
|
+
if (flags.play && !process.stdout.isTTY) process.stderr.write("play: degraded - no live terminal\n")
|
|
43
|
+
// --json 输出数据不输出帧:message 只给一行摘要(动画帧不许污染 JSON)
|
|
44
|
+
const total = (b.payload.nodes ?? []).length + (b.payload.ocg?.candidates ?? []).length + (b.payload.ocg?.events ?? []).length
|
|
45
|
+
const message = flags.json
|
|
46
|
+
? `journey: ${total} objects · window ${b.payload.ocg.window}`
|
|
47
|
+
: frame
|
|
48
|
+
return ok("journey", message, b.payload)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function journeyListCmd(flags, ctx) {
|
|
52
|
+
const argv = ["journey", "list", ...(flags._ ?? [])]
|
|
53
|
+
const b = build(flags, ctx, argv)
|
|
54
|
+
if (b.bad) return b.bad
|
|
55
|
+
const rows = collectRows(b.payload)
|
|
56
|
+
return ok("journey.list", renderRowLines(rows), {
|
|
57
|
+
rows,
|
|
58
|
+
filter: b.payload.ocg.filter,
|
|
59
|
+
window: b.payload.ocg.window,
|
|
60
|
+
})
|
|
61
|
+
}
|