octopi-agent 0.55.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +7 -0
  3. package/README.md +244 -0
  4. package/README_CN.md +241 -0
  5. package/dist/cli/args.d.ts +20 -0
  6. package/dist/cli/args.js +90 -0
  7. package/dist/cli/commands.d.ts +10 -0
  8. package/dist/cli/commands.js +253 -0
  9. package/dist/cli/daemon.d.ts +40 -0
  10. package/dist/cli/daemon.js +549 -0
  11. package/dist/cli/doctor/data.d.ts +64 -0
  12. package/dist/cli/doctor/data.js +209 -0
  13. package/dist/cli/doctor/index.d.ts +131 -0
  14. package/dist/cli/doctor/index.js +711 -0
  15. package/dist/cli/doctor/redact.d.ts +21 -0
  16. package/dist/cli/doctor/redact.js +48 -0
  17. package/dist/cli/doctor/restore.d.ts +36 -0
  18. package/dist/cli/doctor/restore.js +93 -0
  19. package/dist/cli/helpers.d.ts +38 -0
  20. package/dist/cli/helpers.js +158 -0
  21. package/dist/cli/index.d.ts +5 -0
  22. package/dist/cli/index.js +62 -0
  23. package/dist/cli/process-utils.d.ts +55 -0
  24. package/dist/cli/process-utils.js +306 -0
  25. package/dist/cli/serve-webui.d.ts +1 -0
  26. package/dist/cli/serve-webui.js +82 -0
  27. package/dist/cli/webui.d.ts +13 -0
  28. package/dist/cli/webui.js +249 -0
  29. package/dist/config-migrations.d.ts +198 -0
  30. package/dist/config-migrations.js +692 -0
  31. package/dist/config-schema/index.d.ts +1400 -0
  32. package/dist/config-schema/index.js +100 -0
  33. package/dist/config-schema/webui.d.ts +15 -0
  34. package/dist/config-schema/webui.js +15 -0
  35. package/dist/config-schema.d.ts +9 -0
  36. package/dist/config-schema.js +9 -0
  37. package/dist/config.d.ts +18 -0
  38. package/dist/config.js +136 -0
  39. package/dist/index.d.ts +24 -0
  40. package/dist/index.js +35 -0
  41. package/dist/init.d.ts +86 -0
  42. package/dist/init.js +386 -0
  43. package/dist/integration/index.d.ts +8 -0
  44. package/dist/integration/index.js +8 -0
  45. package/dist/integration/tui/app.d.ts +50 -0
  46. package/dist/integration/tui/app.js +584 -0
  47. package/dist/integration/tui/components.d.ts +82 -0
  48. package/dist/integration/tui/components.js +291 -0
  49. package/dist/integration/tui/index.d.ts +8 -0
  50. package/dist/integration/tui/index.js +8 -0
  51. package/dist/integration/tui/theme.d.ts +77 -0
  52. package/dist/integration/tui/theme.js +90 -0
  53. package/dist/testing/chaos-provider.d.ts +109 -0
  54. package/dist/testing/chaos-provider.js +211 -0
  55. package/dist/testing/index.d.ts +15 -0
  56. package/dist/testing/index.js +10 -0
  57. package/dist/testing/recording-provider.d.ts +75 -0
  58. package/dist/testing/recording-provider.js +172 -0
  59. package/dist/testing/replay-provider.d.ts +52 -0
  60. package/dist/testing/replay-provider.js +138 -0
  61. package/dist/testing/scenario-composer.d.ts +86 -0
  62. package/dist/testing/scenario-composer.js +200 -0
  63. package/dist/testing/scenario-runner.d.ts +134 -0
  64. package/dist/testing/scenario-runner.js +240 -0
  65. package/octopi.example.json +220 -0
  66. package/octopi.schema.json +2206 -0
  67. package/package.json +97 -0
@@ -0,0 +1,209 @@
1
+ /**
2
+ * doctor 数据层:agent.db 迁移检查 + session.store 废弃检测
3
+ *
4
+ * 本地确定性;node:sqlite / agent.db 不可用时只报告,不拖垮 doctor。
5
+ *
6
+ * @module
7
+ */
8
+ import { existsSync, mkdirSync, readdirSync } from 'node:fs';
9
+ import { join, resolve } from 'node:path';
10
+ import { getOctopiHome } from '@octopi-agent/engine/paths.js';
11
+ /**
12
+ * 从配置 raw 解析 agent 数据目录目标
13
+ *
14
+ * @param home - OCTOPI_HOME
15
+ * @param raw - 配置对象
16
+ * @returns agent id + home 列表
17
+ */
18
+ export function resolveAgentDataTargets(home, raw) {
19
+ const agents = raw && Array.isArray(raw.agents) ? raw.agents : [{ id: 'default' }];
20
+ const out = [];
21
+ for (const agent of agents) {
22
+ const a = (typeof agent === 'object' && agent !== null ? agent : {});
23
+ const id = typeof a.id === 'string' && a.id ? a.id : 'default';
24
+ const agentHome = typeof a.home === 'string' && a.home
25
+ ? a.home
26
+ : typeof a.persona === 'string'
27
+ ? a.persona
28
+ : join(home, 'agents', id);
29
+ out.push({ id, home: agentHome });
30
+ }
31
+ return out;
32
+ }
33
+ /**
34
+ * 检测废弃 session.store.dataDir 与真实会话落点对照(不迁移数据)
35
+ *
36
+ * @param raw - 未展开配置
37
+ * @param targets - agent home
38
+ * @param configDir - 配置文件所在目录(dataDir 相对基准)
39
+ * @returns findings
40
+ */
41
+ export function checkDeprecatedSessionStore(raw, targets, configDir) {
42
+ const findings = [];
43
+ if (!raw || typeof raw !== 'object')
44
+ return findings;
45
+ const session = raw.session;
46
+ if (!session || typeof session !== 'object')
47
+ return findings;
48
+ const store = session.store;
49
+ if (!store || typeof store !== 'object')
50
+ return findings;
51
+ const dataDirRaw = store.dataDir;
52
+ const dataDir = typeof dataDirRaw === 'string' && dataDirRaw ? resolve(configDir, dataDirRaw) : undefined;
53
+ let legacyFiles = 0;
54
+ if (dataDir && existsSync(dataDir)) {
55
+ try {
56
+ legacyFiles = readdirSync(dataDir).filter((n) => n.endsWith('.jsonl') || n === 'sessions.json').length;
57
+ }
58
+ catch {
59
+ // 目录不可读时仍报告废弃字段,不猜测内容
60
+ legacyFiles = -1;
61
+ }
62
+ }
63
+ let liveFiles = 0;
64
+ const liveDir = getOctopiHome() + '/sessions';
65
+ if (existsSync(liveDir)) {
66
+ try {
67
+ liveFiles = readdirSync(liveDir).filter((n) => n.endsWith('.jsonl')).length;
68
+ }
69
+ catch {
70
+ /* ignore unreadable */
71
+ }
72
+ }
73
+ const dataDirExists = Boolean(dataDir && existsSync(dataDir));
74
+ const hintParts = [
75
+ 'runtime sessions live under OCTOPI_HOME/sessions/; session.store.dataDir is not read by Gateway',
76
+ ];
77
+ if (dataDir) {
78
+ hintParts.push(dataDirExists
79
+ ? `configured dataDir ${dataDir} exists (${legacyFiles < 0 ? 'unreadable' : `${legacyFiles} session-like file(s)`}) — not read by runtime`
80
+ : `configured dataDir ${dataDir} does not exist`);
81
+ }
82
+ hintParts.push(`live sessions/*.jsonl count ≈ ${liveFiles} in OCTOPI_HOME/sessions/`);
83
+ if (dataDirExists && legacyFiles > 0 && liveFiles === 0) {
84
+ hintParts.push('dataDir has session files but OCTOPI_HOME/sessions/ is empty — data may not be in the active path');
85
+ }
86
+ findings.push({
87
+ id: 'CFG010',
88
+ domain: 'config',
89
+ severity: dataDirExists && legacyFiles > 0 && liveFiles === 0 ? 'warn' : 'info',
90
+ message: 'session.store is deprecated; Gateway ignores store/dataDir',
91
+ hint: hintParts.join('; '),
92
+ fixable: true,
93
+ group: 'config',
94
+ });
95
+ return findings;
96
+ }
97
+ /**
98
+ * 检测数据层问题(不修改磁盘)
99
+ *
100
+ * @param targets - agent home 列表
101
+ * @returns findings
102
+ */
103
+ export function detectDataLayer(targets) {
104
+ const findings = [];
105
+ for (const t of targets) {
106
+ const dbPath = join(t.home, 'agent.db');
107
+ if (!existsSync(dbPath)) {
108
+ findings.push({
109
+ id: 'DB001',
110
+ domain: 'data',
111
+ severity: 'info',
112
+ message: `agent "${t.id}": agent.db not present yet`,
113
+ hint: 'created/migrated on --fix data group or first serve',
114
+ fixable: true,
115
+ group: 'data',
116
+ });
117
+ }
118
+ }
119
+ return findings;
120
+ }
121
+ /**
122
+ * 打开 agent.db 触发既有 schema migrate(幂等)
123
+ *
124
+ * @param target - agent home
125
+ * @returns 说明;失败时 throw 由调用方转 finding
126
+ */
127
+ export async function migrateAgentDatabase(target) {
128
+ const dbPath = join(target.home, 'agent.db');
129
+ const { AgentDatabase } = await import('@octopi-agent/engine/harness/memory/sqlite/agent-db.js');
130
+ const created = !existsSync(dbPath);
131
+ const db = await AgentDatabase.create({ dbPath });
132
+ try {
133
+ const stats = db.stats();
134
+ return created
135
+ ? `agent "${target.id}": created agent.db`
136
+ : `agent "${target.id}": agent.db opened + schema migrate ok (memories=${stats.memories ?? 0})`;
137
+ }
138
+ finally {
139
+ db.close();
140
+ }
141
+ }
142
+ /**
143
+ * 应用数据层修复
144
+ *
145
+ * @param targets - agent home 列表
146
+ * @param options - dryRun 时只描述将做什么
147
+ * @returns findings + notes
148
+ */
149
+ export async function applyDataLayerFixes(targets, options = {}) {
150
+ const notes = [];
151
+ const findings = [];
152
+ if (options.dryRun) {
153
+ for (const t of targets) {
154
+ const dbPath = join(t.home, 'agent.db');
155
+ notes.push(existsSync(dbPath)
156
+ ? `[dry-run] would open+migrate ${dbPath}`
157
+ : `[dry-run] would create ${dbPath}`);
158
+ }
159
+ findings.push({
160
+ id: 'FIX002',
161
+ domain: 'data',
162
+ severity: 'info',
163
+ message: `dry-run data fixes: ${notes.length} action(s); disk not modified`,
164
+ hint: notes.join('; ') || undefined,
165
+ fixable: true,
166
+ });
167
+ return { findings, notes };
168
+ }
169
+ for (const t of targets) {
170
+ mkdirSafe(t.home);
171
+ try {
172
+ notes.push(await migrateAgentDatabase(t));
173
+ findings.push({
174
+ id: 'DB001',
175
+ domain: 'data',
176
+ severity: 'ok',
177
+ message: notes[notes.length - 1],
178
+ fixable: false,
179
+ });
180
+ }
181
+ catch (err) {
182
+ const msg = err instanceof Error ? err.message : String(err);
183
+ notes.push(`agent "${t.id}" agent.db failed: ${msg}`);
184
+ findings.push({
185
+ id: 'DB003',
186
+ domain: 'data',
187
+ severity: 'error',
188
+ message: `agent "${t.id}": agent.db migrate failed: ${msg}`,
189
+ hint: 'schema migrate failed; inspect agent.db — requires Node.js >= 24 node:sqlite (create is not transactional — file may be partial)',
190
+ fixable: false,
191
+ });
192
+ }
193
+ }
194
+ const failed = findings.filter((f) => f.id === 'DB003').length;
195
+ findings.push({
196
+ id: 'FIX002',
197
+ domain: 'data',
198
+ severity: failed > 0 ? 'error' : 'ok',
199
+ message: failed > 0
200
+ ? `data-layer fixes completed with ${failed} failure(s) (${notes.length} note(s))`
201
+ : `data-layer fixes completed (${notes.length} note(s))`,
202
+ hint: notes.slice(0, 5).join('; ') || undefined,
203
+ fixable: false,
204
+ });
205
+ return { findings, notes };
206
+ }
207
+ function mkdirSafe(dir) {
208
+ mkdirSync(dir, { recursive: true });
209
+ }
@@ -0,0 +1,131 @@
1
+ /**
2
+ * doctor 检测与修复(本地确定性,零 LLM)
3
+ *
4
+ * @module
5
+ */
6
+ import type { CliArgs } from '../args.js';
7
+ import { applyDataLayerFixes, checkDeprecatedSessionStore, detectDataLayer, resolveAgentDataTargets } from './data.js';
8
+ import { latestConfigBackup, listConfigBackups, restoreConfigFromBackup } from './restore.js';
9
+ export type DoctorSeverity = 'error' | 'warn' | 'info' | 'ok';
10
+ export type FixGroup = 'config' | 'layout' | 'data';
11
+ export interface DoctorFinding {
12
+ id: string;
13
+ domain: 'config' | 'layout' | 'data' | 'runtime';
14
+ severity: DoctorSeverity;
15
+ message: string;
16
+ hint?: string;
17
+ fixable: boolean;
18
+ /** 可修复时所属分组(交互式勾选用) */
19
+ group?: FixGroup;
20
+ }
21
+ export interface DoctorReport {
22
+ home: string;
23
+ configPath: string | null;
24
+ findings: DoctorFinding[];
25
+ summary: {
26
+ error: number;
27
+ warn: number;
28
+ info: number;
29
+ ok: number;
30
+ };
31
+ fixableCount: number;
32
+ backups?: string[];
33
+ }
34
+ export interface FixCandidateGroup {
35
+ group: FixGroup;
36
+ items: Array<{
37
+ id: string;
38
+ message: string;
39
+ }>;
40
+ }
41
+ export type SelectFixGroups = (candidates: FixCandidateGroup[]) => Promise<FixGroup[]> | FixGroup[];
42
+ export interface DoctorOptions {
43
+ fix?: boolean;
44
+ dryRun?: boolean;
45
+ json?: boolean;
46
+ yes?: boolean;
47
+ only?: string[];
48
+ allowDeleteLegacyDirs?: boolean;
49
+ /** 强制/关闭交互式勾选;缺省时:TTY 且 --fix 且无 --yes → 交互 */
50
+ interactive?: boolean;
51
+ /** 注入选择器(测试);返回空数组 = 取消修复 */
52
+ selectFixGroups?: SelectFixGroups;
53
+ /** 仅应用这些分组;设置后跳过交互 */
54
+ selectedGroups?: FixGroup[];
55
+ }
56
+ /**
57
+ * 解析 doctor 检查的配置路径(显式 -c 最优先)
58
+ *
59
+ * @param args - CLI 参数
60
+ * @returns home 与 config 路径信息
61
+ */
62
+ export declare function resolveDoctorPaths(args: Pick<CliArgs, 'config'>): {
63
+ home: string;
64
+ configPath: string | null;
65
+ cwdConfig: string;
66
+ homeConfig: string;
67
+ };
68
+ /**
69
+ * 将 fixable findings 按分组聚合
70
+ *
71
+ * @param findings - 诊断结果
72
+ * @returns 可修复分组
73
+ */
74
+ export declare function groupFixableFindings(findings: DoctorFinding[]): FixCandidateGroup[];
75
+ /**
76
+ * 复检 apiKey 语义(展开后);只报告 ok/missing
77
+ *
78
+ * @param rawExpanded - env 展开后的配置
79
+ * @returns findings
80
+ */
81
+ export declare function checkApiKeySemantics(rawExpanded: unknown): DoctorFinding[];
82
+ /**
83
+ * 默认交互式选择器(TTY)
84
+ *
85
+ * @param candidates - 可修复分组
86
+ * @returns 用户选择的分组;取消为空数组
87
+ */
88
+ export declare function defaultSelectFixGroups(candidates: FixCandidateGroup[]): Promise<FixGroup[]>;
89
+ /**
90
+ * 执行 doctor 检测(及可选修复)
91
+ *
92
+ * @param args - CLI 参数
93
+ * @param options - doctor 选项
94
+ * @returns 报告
95
+ */
96
+ export declare function runDoctor(args: Pick<CliArgs, 'config'>, options?: DoctorOptions): Promise<DoctorReport>;
97
+ /**
98
+ * 应用布局侧修复
99
+ *
100
+ * @param home - OCTOPI_HOME
101
+ * @param raw - 配置对象
102
+ * @param options - doctor 选项
103
+ * @returns 已执行动作说明
104
+ */
105
+ export declare function applyLayoutFixes(home: string, raw: Record<string, unknown> | null, options: DoctorOptions): string[];
106
+ export declare function formatDoctorReport(report: DoctorReport): string;
107
+ /**
108
+ * 报告转 JSON(脱敏)
109
+ *
110
+ * @param report - doctor 报告
111
+ * @returns JSON 字符串
112
+ */
113
+ export declare function doctorReportToJson(report: DoctorReport): string;
114
+ /**
115
+ * 处理 --restore
116
+ *
117
+ * @param args - CLI 参数(restore 可为 true 或备份路径)
118
+ * @returns 是否已处理 restore 模式
119
+ */
120
+ export declare function doctorRestoreCommand(args: CliArgs): Promise<void>;
121
+ /**
122
+ * CLI 入口:octopi doctor
123
+ *
124
+ * @param args - CLI 参数
125
+ * @param options - 选项
126
+ */
127
+ export declare function doctorCommand(args: CliArgs, options?: DoctorOptions): Promise<void>;
128
+ /** 供测试:列出 agent 目录 */
129
+ export declare function listAgentDirs(home: string): string[];
130
+ export { listConfigBackups, latestConfigBackup, restoreConfigFromBackup };
131
+ export { detectDataLayer, applyDataLayerFixes, resolveAgentDataTargets, checkDeprecatedSessionStore, };