@sanqianx/claude-ai-workbench 0.11.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.
- package/LICENSE +201 -0
- package/README.md +129 -0
- package/apps/agent-terminal/app.css +567 -0
- package/apps/agent-terminal/app.js +2392 -0
- package/apps/agent-terminal/index.html +158 -0
- package/apps/ai-profile-settings-host/index.html +25 -0
- package/apps/ai-profile-settings-host/page.css +1 -0
- package/apps/ai-profile-settings-host/page.js +113 -0
- package/apps/minimal-test-host/README.md +15 -0
- package/apps/minimal-test-host/host.css +2 -0
- package/apps/minimal-test-host/host.js +35 -0
- package/apps/minimal-test-host/index.html +58 -0
- package/apps/minimal-test-host/optional-mock/fault-injection.js +15 -0
- package/docs/sdk/FILE_LAYOUT.md +31 -0
- package/docs/sdk/INTEGRATION.md +126 -0
- package/docs/sdk/README.md +21 -0
- package/docs/sdk/openapi.yaml +760 -0
- package/package.json +51 -0
- package/packages/client/README.md +11 -0
- package/packages/client/index.d.ts +81 -0
- package/packages/client/index.js +198 -0
- package/packages/client/package.json +29 -0
- package/packages/contracts/README.md +9 -0
- package/packages/contracts/index.d.ts +252 -0
- package/packages/contracts/index.js +295 -0
- package/packages/contracts/package.json +24 -0
- package/packages/core/README.md +8 -0
- package/packages/core/index.d.ts +76 -0
- package/packages/core/index.js +214 -0
- package/packages/core/package.json +29 -0
- package/packages/server/README.md +17 -0
- package/packages/server/bin/agent-terminal-server.js +36 -0
- package/packages/server/bin/claude-workbench-server.js +46 -0
- package/packages/server/bin/claude-workbench.js +225 -0
- package/packages/server/index.d.ts +145 -0
- package/packages/server/index.js +183 -0
- package/packages/server/lib/agent-discovery.js +112 -0
- package/packages/server/lib/agent-registry.js +98 -0
- package/packages/server/lib/attachment-store.js +78 -0
- package/packages/server/lib/bridges/app-server-bridge.js +372 -0
- package/packages/server/lib/bridges/opencode-server-bridge.js +490 -0
- package/packages/server/lib/capture-settings-store.js +34 -0
- package/packages/server/lib/context-resolver.js +40 -0
- package/packages/server/lib/drivers/cli-json-runner.js +960 -0
- package/packages/server/lib/drivers/codex-driver.js +1413 -0
- package/packages/server/lib/drivers/opencode-driver.js +1527 -0
- package/packages/server/lib/drivers/zcode-driver.js +1167 -0
- package/packages/server/lib/event-stream.js +35 -0
- package/packages/server/lib/folder-picker.js +623 -0
- package/packages/server/lib/http-handler.js +254 -0
- package/packages/server/lib/json-file.js +19 -0
- package/packages/server/lib/model-detection.js +295 -0
- package/packages/server/lib/profile-store.js +149 -0
- package/packages/server/lib/project-store.js +102 -0
- package/packages/server/lib/session-archive-store.js +53 -0
- package/packages/server/lib/system-doctor.js +119 -0
- package/packages/server/lib/ui-preferences-store.js +48 -0
- package/packages/server/lib/workbench-service.js +620 -0
- package/packages/server/package.json +34 -0
- package/packages/ui/README.md +30 -0
- package/packages/ui/ai-profile-settings.d.ts +45 -0
- package/packages/ui/ai-profile-settings.js +283 -0
- package/packages/ui/claude-terminal.d.ts +31 -0
- package/packages/ui/claude-terminal.js +343 -0
- package/packages/ui/index.d.ts +18 -0
- package/packages/ui/index.js +51 -0
- package/packages/ui/package.json +42 -0
- package/packages/ui/theme.css +13 -0
- package/src/assets/claude-prompts.json +21 -0
- package/src/backend/http/ai-profile-routes.js +61 -0
- package/src/backend/http/claude-routes.js +170 -0
- package/src/backend/http/node-helpers.js +45 -0
- package/src/backend/index.js +21 -0
- package/src/backend/lib/ai-adapter.js +144 -0
- package/src/backend/lib/ai-profile-store.js +259 -0
- package/src/backend/lib/ai-vendor-presets.js +83 -0
- package/src/backend/lib/ai-workspace.js +140 -0
- package/src/backend/lib/automation-config.js +253 -0
- package/src/backend/lib/claude-cli-runner.js +1749 -0
- package/src/backend/lib/data-dir.js +169 -0
- package/src/backend/lib/llm-client.js +176 -0
- package/src/backend/lib/model-context-windows.js +82 -0
- package/src/backend/lib/prompt-registry.js +166 -0
- package/src/backend/lib/runtime-config.js +121 -0
- package/src/index.js +7 -0
- package/src/sdk-wave-a.ts +1 -0
- package/src/sdk-wave-b.ts +1 -0
- package/src/sdk-wave-c.ts +1 -0
- package/src/sdk-wave-d.ts +1 -0
- package/src/sdk-wave-e.ts +1 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
const { AI_VENDOR_PRESETS, listVendorPresetNames } = require('../lib/ai-vendor-presets');
|
|
2
|
+
const { listAdapters } = require('../lib/ai-adapter');
|
|
3
|
+
const { sendJson, readBody } = require('./node-helpers');
|
|
4
|
+
|
|
5
|
+
async function handleAiProfileRoutes(req, res, options = {}) {
|
|
6
|
+
const store = options.store || options.profileStore;
|
|
7
|
+
const basePath = options.basePath || '/api/ai-profiles';
|
|
8
|
+
const vendorPath = options.vendorPath || '/api/ai-vendor-presets';
|
|
9
|
+
const url = options.url || new URL(req.url, 'http://127.0.0.1');
|
|
10
|
+
const pathname = url.pathname;
|
|
11
|
+
if (pathname !== basePath && pathname !== vendorPath && !pathname.startsWith(`${basePath}/`)) return false;
|
|
12
|
+
if (!store) {
|
|
13
|
+
sendJson(res, 503, { ok: false, error: 'AI profile store is not available' });
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
if (req.method === 'GET' && pathname === basePath) {
|
|
19
|
+
const publicConfig = store.publicConfig();
|
|
20
|
+
sendJson(res, 200, {
|
|
21
|
+
ok: true,
|
|
22
|
+
...publicConfig,
|
|
23
|
+
adapters: listAdapters(),
|
|
24
|
+
});
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (req.method === 'GET' && pathname === vendorPath) {
|
|
29
|
+
sendJson(res, 200, { ok: true, presets: AI_VENDOR_PRESETS, names: listVendorPresetNames() });
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (req.method === 'PUT' && pathname === basePath) {
|
|
34
|
+
const body = await readBody(req);
|
|
35
|
+
const saved = store.writeConfig(body);
|
|
36
|
+
sendJson(res, 200, { ok: true, config: saved });
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (req.method === 'POST' && pathname === `${basePath}/test`) {
|
|
41
|
+
const body = await readBody(req);
|
|
42
|
+
const result = await store.testProfile({
|
|
43
|
+
profileId: body.profileId,
|
|
44
|
+
profile: body.profile,
|
|
45
|
+
prompt: body.prompt || 'what model are you?',
|
|
46
|
+
});
|
|
47
|
+
sendJson(res, 200, result);
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
} catch (error) {
|
|
51
|
+
sendJson(res, error.status || 400, { ok: false, error: error.message, errors: error.errors || undefined });
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
sendJson(res, 404, { ok: false, error: 'unknown AI profile route' });
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
module.exports = {
|
|
60
|
+
handleAiProfileRoutes,
|
|
61
|
+
};
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
const { sendJson, readBody, writeSseHeaders, writeSse } = require('./node-helpers');
|
|
2
|
+
|
|
3
|
+
function sessionIdFromPath(pathname, basePath) {
|
|
4
|
+
const rest = pathname.slice(basePath.length).replace(/^\/+/, '');
|
|
5
|
+
const parts = rest.split('/');
|
|
6
|
+
return parts[0] === 'sessions' ? parts[1] || '' : '';
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function ensureRunner(runner) {
|
|
10
|
+
if (!runner || typeof runner.listSessions !== 'function') {
|
|
11
|
+
throw new Error('Claude runner is not available');
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async function handleClaudeRoutes(req, res, options = {}) {
|
|
16
|
+
const runner = options.runner;
|
|
17
|
+
const basePath = options.basePath || '/api/claude';
|
|
18
|
+
const url = options.url || new URL(req.url, 'http://127.0.0.1');
|
|
19
|
+
const pathname = url.pathname;
|
|
20
|
+
if (pathname !== basePath && !pathname.startsWith(`${basePath}/`)) return false;
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
ensureRunner(runner);
|
|
24
|
+
} catch (error) {
|
|
25
|
+
sendJson(res, 503, { ok: false, error: error.message });
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (req.method === 'GET' && pathname === `${basePath}/sessions`) {
|
|
30
|
+
const projectSlug = url.searchParams.get('slug') || url.searchParams.get('projectSlug') || null;
|
|
31
|
+
sendJson(res, 200, { ok: true, sessions: runner.listSessions({ projectSlug }) });
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (req.method === 'GET' && pathname === `${basePath}/sessions-stream`) {
|
|
36
|
+
writeSseHeaders(res);
|
|
37
|
+
writeSse(res, 'claude/snapshot', { sessions: runner.listSessions({}) });
|
|
38
|
+
const unsubscribe = runner.subscribeList(event => {
|
|
39
|
+
try { writeSse(res, 'claude/sessions-changed', event); } catch {}
|
|
40
|
+
});
|
|
41
|
+
const heartbeat = setInterval(() => {
|
|
42
|
+
try { res.write(`: keepalive ${Date.now()}\n\n`); } catch {}
|
|
43
|
+
}, 15000);
|
|
44
|
+
const cleanup = () => {
|
|
45
|
+
clearInterval(heartbeat);
|
|
46
|
+
try { unsubscribe(); } catch {}
|
|
47
|
+
};
|
|
48
|
+
req.on('close', cleanup);
|
|
49
|
+
req.on('error', cleanup);
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (req.method === 'POST' && pathname.startsWith(`${basePath}/projects/`) && pathname.endsWith('/start')) {
|
|
54
|
+
const slug = pathname.slice(`${basePath}/projects/`.length).replace(/\/start$/, '');
|
|
55
|
+
const resolveProject = options.resolveProject || options.projectResolver;
|
|
56
|
+
if (typeof resolveProject !== 'function') {
|
|
57
|
+
sendJson(res, 500, { ok: false, error: 'resolveProject option is required' });
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
const project = await resolveProject(slug, req);
|
|
61
|
+
if (!project) {
|
|
62
|
+
sendJson(res, 404, { ok: false, error: `project not found: ${slug}` });
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
const started = runner.startChatSession({
|
|
66
|
+
slug: project.slug || slug,
|
|
67
|
+
projectPath: project.projectPath || project.gitPath || project.localPath || project.path,
|
|
68
|
+
kbPath: project.kbPath || project.workPath || project.projectPath || project.path,
|
|
69
|
+
aiProfile: project.aiProfile || null,
|
|
70
|
+
permissionMode: project.permissionMode || 'default',
|
|
71
|
+
});
|
|
72
|
+
sendJson(res, 200, { ok: true, slug, ...started, aiProfileId: project.aiProfile && project.aiProfile.id || null });
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const sessionId = sessionIdFromPath(pathname, basePath);
|
|
77
|
+
if (!sessionId) {
|
|
78
|
+
sendJson(res, 404, { ok: false, error: 'unknown Claude route' });
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (req.method === 'GET' && pathname === `${basePath}/sessions/${sessionId}`) {
|
|
83
|
+
const state = runner.getState(sessionId);
|
|
84
|
+
if (!state) sendJson(res, 404, { ok: false, error: 'session not found' });
|
|
85
|
+
else sendJson(res, 200, { ok: true, ...state });
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (req.method === 'GET' && pathname === `${basePath}/sessions/${sessionId}/token-usage`) {
|
|
90
|
+
if (!runner.getSession(sessionId)) sendJson(res, 404, { ok: false, error: 'session not found' });
|
|
91
|
+
else sendJson(res, 200, { ok: true, sessionId, ...runner.getSessionTokenUsage(sessionId) });
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (req.method === 'GET' && pathname === `${basePath}/sessions/${sessionId}/stream`) {
|
|
96
|
+
if (!runner.getSession(sessionId)) {
|
|
97
|
+
sendJson(res, 404, { ok: false, error: 'session not found' });
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
writeSseHeaders(res);
|
|
101
|
+
writeSse(res, 'claude/hello', { sessionId, time: new Date().toISOString() });
|
|
102
|
+
const session = runner.getSession(sessionId);
|
|
103
|
+
const events = Array.isArray(session && session.outputBuffer) ? session.outputBuffer : [];
|
|
104
|
+
for (const event of events.slice(-1000)) {
|
|
105
|
+
if (event && event.type) writeSse(res, event.type, event);
|
|
106
|
+
}
|
|
107
|
+
const unsubscribe = runner.subscribe(sessionId, event => {
|
|
108
|
+
try { writeSse(res, event.type, event); } catch {}
|
|
109
|
+
});
|
|
110
|
+
const heartbeat = setInterval(() => {
|
|
111
|
+
try { res.write(`: keepalive ${Date.now()}\n\n`); } catch {}
|
|
112
|
+
}, 15000);
|
|
113
|
+
const cleanup = () => {
|
|
114
|
+
clearInterval(heartbeat);
|
|
115
|
+
try { unsubscribe(); } catch {}
|
|
116
|
+
};
|
|
117
|
+
req.on('close', cleanup);
|
|
118
|
+
req.on('error', cleanup);
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (req.method === 'POST' && pathname === `${basePath}/sessions/${sessionId}/input`) {
|
|
123
|
+
if (!runner.getSession(sessionId)) {
|
|
124
|
+
sendJson(res, 404, { ok: false, error: 'session not found' });
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
const body = await readBody(req);
|
|
128
|
+
if (!body || typeof body.text !== 'string' || !body.text.trim()) {
|
|
129
|
+
sendJson(res, 400, { ok: false, error: 'body.text is required' });
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
const resolveProfile = options.resolveProfile || options.profileResolver;
|
|
133
|
+
const profile = typeof resolveProfile === 'function'
|
|
134
|
+
? await resolveProfile(runner.getState(sessionId), body, req)
|
|
135
|
+
: null;
|
|
136
|
+
const result = await runner.sendInput(sessionId, body.text, profile, {
|
|
137
|
+
permissionMode: typeof body.permissionMode === 'string' ? body.permissionMode : undefined,
|
|
138
|
+
allowedTools: Array.isArray(body.allowedTools) ? body.allowedTools : undefined,
|
|
139
|
+
});
|
|
140
|
+
sendJson(res, 200, { ok: true, sessionId, pendingPermission: result && result.pendingPermission || null, aiProfileId: profile && profile.id || null });
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (req.method === 'POST' && pathname === `${basePath}/sessions/${sessionId}/permission`) {
|
|
145
|
+
const body = await readBody(req);
|
|
146
|
+
if (!body || typeof body.requestId !== 'string') {
|
|
147
|
+
sendJson(res, 400, { ok: false, error: 'body.requestId is required' });
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
const result = runner.resolvePermission(sessionId, body.requestId, {
|
|
151
|
+
allow: body.allow === true,
|
|
152
|
+
message: typeof body.message === 'string' ? body.message : '',
|
|
153
|
+
});
|
|
154
|
+
sendJson(res, 200, { ok: true, sessionId, ...result });
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (req.method === 'POST' && pathname === `${basePath}/sessions/${sessionId}/abort`) {
|
|
159
|
+
runner.abort(sessionId);
|
|
160
|
+
sendJson(res, 200, { ok: true, sessionId });
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
sendJson(res, 404, { ok: false, error: 'unknown Claude route' });
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
module.exports = {
|
|
169
|
+
handleClaudeRoutes,
|
|
170
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
function sendJson(res, status, payload) {
|
|
2
|
+
res.writeHead(status, {
|
|
3
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
4
|
+
'Cache-Control': 'no-store',
|
|
5
|
+
});
|
|
6
|
+
res.end(JSON.stringify(payload));
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function readBody(req) {
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
const chunks = [];
|
|
12
|
+
req.on('data', chunk => chunks.push(chunk));
|
|
13
|
+
req.on('end', () => {
|
|
14
|
+
if (!chunks.length) return resolve({});
|
|
15
|
+
const text = Buffer.concat(chunks).toString('utf8');
|
|
16
|
+
try {
|
|
17
|
+
resolve(JSON.parse(text));
|
|
18
|
+
} catch (error) {
|
|
19
|
+
reject(new Error(`invalid JSON body: ${error.message}`));
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
req.on('error', reject);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function writeSseHeaders(res) {
|
|
27
|
+
res.writeHead(200, {
|
|
28
|
+
'Content-Type': 'text/event-stream',
|
|
29
|
+
'Cache-Control': 'no-store, no-transform',
|
|
30
|
+
Connection: 'keep-alive',
|
|
31
|
+
'X-Accel-Buffering': 'no',
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function writeSse(res, event, data) {
|
|
36
|
+
res.write(`event: ${event}\n`);
|
|
37
|
+
res.write(`data: ${JSON.stringify(data)}\n\n`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
module.exports = {
|
|
41
|
+
sendJson,
|
|
42
|
+
readBody,
|
|
43
|
+
writeSseHeaders,
|
|
44
|
+
writeSse,
|
|
45
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const runtimeConfig = require('./lib/runtime-config');
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
...runtimeConfig,
|
|
5
|
+
dataDir: require('./lib/data-dir'),
|
|
6
|
+
aiWorkspace: require('./lib/ai-workspace'),
|
|
7
|
+
automationConfig: require('./lib/automation-config'),
|
|
8
|
+
modelContextWindows: require('./lib/model-context-windows'),
|
|
9
|
+
llmClient: require('./lib/llm-client'),
|
|
10
|
+
promptRegistry: require('./lib/prompt-registry'),
|
|
11
|
+
aiVendorPresets: require('./lib/ai-vendor-presets'),
|
|
12
|
+
aiAdapter: require('./lib/ai-adapter'),
|
|
13
|
+
aiProfiles: require('./lib/ai-profile-store'),
|
|
14
|
+
createRunner() {
|
|
15
|
+
return require('./lib/claude-cli-runner');
|
|
16
|
+
},
|
|
17
|
+
http: {
|
|
18
|
+
...require('./http/claude-routes'),
|
|
19
|
+
...require('./http/ai-profile-routes'),
|
|
20
|
+
},
|
|
21
|
+
};
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// AI analyzer adapter interface (TASK-005 + TASK-008).
|
|
2
|
+
// Each adapter implements the same shape so the orchestrator can swap implementations.
|
|
3
|
+
//
|
|
4
|
+
// analyzeCommitBatch({ project, commits, contextPack }) -> { changes: [...] }
|
|
5
|
+
// validateOutput(output) -> { valid: boolean, errors: string[] }
|
|
6
|
+
//
|
|
7
|
+
// All adapters MUST return objects that pass validateOutput. The orchestrator never
|
|
8
|
+
// writes drafts to disk unless the output validates.
|
|
9
|
+
|
|
10
|
+
const fs = require('fs');
|
|
11
|
+
const path = require('path');
|
|
12
|
+
|
|
13
|
+
function isString(v) { return typeof v === 'string'; }
|
|
14
|
+
function isNonEmptyString(v) { return isString(v) && v.length > 0; }
|
|
15
|
+
function isStringArray(v) { return Array.isArray(v) && v.every(isString); }
|
|
16
|
+
function isObject(v) { return v && typeof v === 'object' && !Array.isArray(v); }
|
|
17
|
+
|
|
18
|
+
function normalizeKnowledgeLanguage(value) {
|
|
19
|
+
return value === 'en-US' ? 'en-US' : 'zh-CN';
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function languageName(value) {
|
|
23
|
+
return normalizeKnowledgeLanguage(value) === 'en-US' ? 'English' : 'Simplified Chinese';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const changeEntrySchema = {
|
|
27
|
+
commit: isNonEmptyString,
|
|
28
|
+
classification: (v) => ['new-feature', 'existing-feature-update', 'bug-fix', 'refactor', 'infrastructure', 'test-only', 'docs-only'].includes(v),
|
|
29
|
+
developmentIntent: isNonEmptyString,
|
|
30
|
+
goalImpact: isNonEmptyString,
|
|
31
|
+
evidence: isStringArray,
|
|
32
|
+
proposedOps: (v) => Array.isArray(v) && v.every(op => isObject(op) && isNonEmptyString(op.op) && isNonEmptyString(op.path)),
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
function validateCommitBatchOutput(output) {
|
|
36
|
+
const errors = [];
|
|
37
|
+
if (!isObject(output)) { errors.push('output is not an object'); return { valid: false, errors }; }
|
|
38
|
+
if (!Array.isArray(output.changes)) { errors.push('missing or invalid: changes'); return { valid: false, errors }; }
|
|
39
|
+
for (let i = 0; i < output.changes.length; i++) {
|
|
40
|
+
const c = output.changes[i];
|
|
41
|
+
if (!isObject(c)) { errors.push(`change[${i}] is not an object`); continue; }
|
|
42
|
+
for (const [key, check] of Object.entries(changeEntrySchema)) {
|
|
43
|
+
if (!(key in c)) { errors.push(`change[${i}] missing: ${key}`); continue; }
|
|
44
|
+
if (!check(c[key])) errors.push(`change[${i}].${key} is invalid`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return { valid: errors.length === 0, errors };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// ---- claude-code-agent: real LLM via Anthropic-compatible API ----
|
|
51
|
+
//
|
|
52
|
+
// This adapter calls an Anthropic Messages API (real Anthropic or any
|
|
53
|
+
// compatible proxy such as api.minimaxi.com/anthropic). The model returns
|
|
54
|
+
// JSON that the KB orchestrator validates against the existing schemas.
|
|
55
|
+
//
|
|
56
|
+
// The adapter is intentionally minimal: it does ONE call per commit batch
|
|
57
|
+
// and asks the model to return a strict JSON object matching the schema.
|
|
58
|
+
// Validation happens in `validateOutput`; if the model misbehaves, the run
|
|
59
|
+
// is marked failed and the user can re-trigger.
|
|
60
|
+
|
|
61
|
+
const { completeJson } = require('./llm-client');
|
|
62
|
+
|
|
63
|
+
const COMMIT_SYSTEM = `You are a senior software architect reviewing a batch of git commits. For each commit, decide what useful knowledge should be recorded in the project's knowledge base. Respond with a single JSON object matching the schema in the user message. Do not invent file paths. Cite the provided evidence (context pack entries) whenever possible. Summarize the user's development intent; do not preserve raw prompts. Do not propose operations that would write outside changes/, modules/, README.md, GOAL.md, or ARCHITECTURE.md.`;
|
|
64
|
+
|
|
65
|
+
function buildCommitUser({ project, commits, contextPack }) {
|
|
66
|
+
const outputLanguage = languageName(project && project.knowledgeLanguage);
|
|
67
|
+
const ctx = (contextPack && contextPack.entries || []).slice(0, 30).map(e => `- ${e.path}: ${(e.summary || '').slice(0, 200)}`).join('\n');
|
|
68
|
+
const list = (commits || []).map(c => `- full=${c.hash} | short=${c.short || c.hash.slice(0,7)} | ${c.date || ''} | ${c.author || ''} | ${c.subject || ''}`).join('\n');
|
|
69
|
+
return `Project slug: ${project.slug}
|
|
70
|
+
Knowledge output language: ${normalizeKnowledgeLanguage(project.knowledgeLanguage)} (${outputLanguage})
|
|
71
|
+
|
|
72
|
+
Pending commits (chronological order, oldest first). For each line, the "full=" value is the COMPLETE 40-character commit hash; you MUST copy that exact value into the JSON "commit" field:
|
|
73
|
+
${list || '(no commits)'}
|
|
74
|
+
|
|
75
|
+
Context pack excerpts:
|
|
76
|
+
${ctx || '(no context pack available)'}
|
|
77
|
+
|
|
78
|
+
Return a JSON object of the form:
|
|
79
|
+
{
|
|
80
|
+
"changes": [
|
|
81
|
+
{
|
|
82
|
+
"commit": string, // FULL 40-char commit hash copied from the input
|
|
83
|
+
"classification": "new-feature" | "existing-feature-update" | "bug-fix" | "refactor" | "infrastructure" | "test-only" | "docs-only",
|
|
84
|
+
"developmentIntent": string, // concise summary of the user's request or prompt intent; never raw prompt logs
|
|
85
|
+
"goalImpact": string, // one short sentence
|
|
86
|
+
"evidence": [string], // paths cited from the context pack
|
|
87
|
+
"proposedOps": [{ "op": "create-file" | "append-section" | "update-section", "path": string, "fromTemplate"?: string }]
|
|
88
|
+
}
|
|
89
|
+
]
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
Constraints:
|
|
93
|
+
- All human-readable natural language string values MUST be written in ${outputLanguage}. Keep JSON keys, enum values, file paths, and commit hashes exactly as specified.
|
|
94
|
+
- One entry per commit in the input list, in the same order.
|
|
95
|
+
- "commit" MUST be the full 40-character hash from the input (the value after "full="), NOT the short hash.
|
|
96
|
+
- proposedOps[].path MUST be a relative path under changes/ or modules/, or exactly README.md, GOAL.md, or ARCHITECTURE.md. Goal and architecture edits require human review.
|
|
97
|
+
- Do not include any text outside the JSON object.`;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const claudeCodeAgent = {
|
|
101
|
+
id: 'claude-code-agent',
|
|
102
|
+
name: 'Claude Code Agent (Anthropic-compatible)',
|
|
103
|
+
description: 'Real LLM through an Anthropic-compatible Messages API. AI profiles can reuse this implementation with different model providers.',
|
|
104
|
+
async analyzeCommitBatch({ project, commits, contextPack }) {
|
|
105
|
+
const user = buildCommitUser({ project, commits, contextPack });
|
|
106
|
+
const r = await completeJson({ system: COMMIT_SYSTEM, user, maxTokens: 4096, profileId: project && project.aiProfileId });
|
|
107
|
+
if (!r.parsed) {
|
|
108
|
+
console.error(`[claude-code-agent] LLM did not return valid JSON. text head: ${r.text.slice(0, 500)} ... text tail: ${r.text.slice(-500)}`);
|
|
109
|
+
const e = new Error(`LLM returned no parseable JSON (${r.parseError && r.parseError.message || 'no output'})`);
|
|
110
|
+
e.llmText = r.text;
|
|
111
|
+
throw e;
|
|
112
|
+
}
|
|
113
|
+
// Sanity log: how many changes did the LLM return, and what do the commits look like?
|
|
114
|
+
const changes = (r.parsed && r.parsed.changes) || [];
|
|
115
|
+
console.log(`[claude-code-agent] parsed ${changes.length} changes; first change: ${JSON.stringify(changes[0]).slice(0, 300)}`);
|
|
116
|
+
return r.parsed;
|
|
117
|
+
},
|
|
118
|
+
validateOutput(output) {
|
|
119
|
+
return validateCommitBatchOutput(output);
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const ADAPTERS = {
|
|
124
|
+
'claude-code-agent': claudeCodeAgent,
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
function getAdapter(id) {
|
|
128
|
+
return ADAPTERS[id] || null;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function listAdapters() {
|
|
132
|
+
return Object.values(ADAPTERS).map(a => ({
|
|
133
|
+
id: a.id,
|
|
134
|
+
name: a.name,
|
|
135
|
+
description: a.description || null,
|
|
136
|
+
}));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
module.exports = {
|
|
140
|
+
ADAPTERS,
|
|
141
|
+
getAdapter,
|
|
142
|
+
listAdapters,
|
|
143
|
+
validateCommitBatchOutput,
|
|
144
|
+
};
|