@sanqianx/claude-ai-workbench 0.13.2 → 0.13.4
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 -201
- package/README.md +156 -156
- package/apps/agent-terminal/app.css +652 -613
- package/apps/agent-terminal/app.js +143 -67
- package/apps/agent-terminal/conversation-composer.js +44 -0
- package/apps/agent-terminal/index.html +172 -170
- package/apps/agent-terminal/panel-host-proof.html +152 -152
- package/apps/agent-terminal/panel-proof-content.html +41 -41
- package/apps/agent-terminal/right-workspace-dock.css +189 -159
- package/apps/agent-terminal/right-workspace-dock.js +652 -521
- package/apps/agent-terminal/user-question-card.js +68 -0
- package/apps/ai-profile-settings-host/index.html +25 -25
- package/apps/ai-profile-settings-host/page.css +1 -1
- package/apps/ai-profile-settings-host/page.js +113 -113
- package/apps/minimal-test-host/README.md +15 -15
- package/apps/minimal-test-host/host.css +2 -2
- package/apps/minimal-test-host/host.js +35 -35
- package/apps/minimal-test-host/index.html +58 -58
- package/apps/minimal-test-host/optional-mock/fault-injection.js +15 -15
- package/docs/sdk/FILE_LAYOUT.md +31 -31
- package/docs/sdk/INTEGRATION.md +126 -126
- package/docs/sdk/README.md +21 -21
- package/docs/sdk/SESSION_EVENTS.md +15 -13
- package/docs/sdk/openapi.yaml +16 -1
- package/package.json +1 -1
- package/packages/client/README.md +29 -29
- package/packages/client/index.d.ts +122 -122
- package/packages/client/index.js +260 -260
- package/packages/client/package.json +2 -2
- package/packages/contracts/README.md +9 -9
- package/packages/contracts/index.d.ts +519 -517
- package/packages/contracts/index.js +312 -312
- package/packages/contracts/package.json +1 -1
- package/packages/core/README.md +8 -8
- package/packages/core/index.d.ts +76 -76
- package/packages/core/index.js +222 -222
- package/packages/core/package.json +2 -2
- package/packages/server/README.md +17 -17
- package/packages/server/bin/agent-terminal-server.js +42 -42
- package/packages/server/bin/claude-workbench.js +225 -225
- package/packages/server/index.d.ts +171 -171
- package/packages/server/index.js +230 -230
- package/packages/server/lib/agent-discovery.js +133 -133
- package/packages/server/lib/agent-registry.js +143 -143
- package/packages/server/lib/agent-spawn-env.js +17 -17
- package/packages/server/lib/attachment-store.js +78 -78
- package/packages/server/lib/bridges/app-server-bridge.js +373 -373
- package/packages/server/lib/bridges/opencode-server-bridge.js +506 -506
- package/packages/server/lib/capture-settings-store.js +35 -35
- package/packages/server/lib/claude-history-recovery.js +87 -87
- package/packages/server/lib/cli-session-recovery.js +28 -28
- package/packages/server/lib/context-resolver.js +55 -55
- package/packages/server/lib/conversation-capture.js +51 -51
- package/packages/server/lib/delegation-phase-store.js +357 -357
- package/packages/server/lib/delegation-store.js +474 -468
- package/packages/server/lib/diagnostics.js +32 -32
- package/packages/server/lib/drivers/cli-json-runner.js +974 -974
- package/packages/server/lib/drivers/codex-driver.js +1461 -1442
- package/packages/server/lib/drivers/opencode-driver.js +1554 -1554
- package/packages/server/lib/drivers/zcode-driver.js +1250 -1250
- package/packages/server/lib/drivers/zcode-profile-runtime.js +71 -71
- package/packages/server/lib/event-stream.js +138 -138
- package/packages/server/lib/folder-picker.js +623 -623
- package/packages/server/lib/http-handler.js +339 -339
- package/packages/server/lib/json-file.js +26 -26
- package/packages/server/lib/knowledge-context.js +33 -33
- package/packages/server/lib/model-detection.js +295 -295
- package/packages/server/lib/parent-context.js +44 -0
- package/packages/server/lib/profile-store.js +283 -283
- package/packages/server/lib/project-store.js +102 -102
- package/packages/server/lib/runtime-loader.js +120 -120
- package/packages/server/lib/session-archive-store.js +53 -53
- package/packages/server/lib/session-history.js +612 -612
- package/packages/server/lib/system-doctor.js +125 -125
- package/packages/server/lib/ui-preferences-store.js +48 -48
- package/packages/server/lib/user-questions.js +67 -0
- package/packages/server/lib/workbench-service.js +1696 -1675
- package/packages/server/package.json +2 -2
- package/packages/ui/README.md +30 -30
- package/packages/ui/ai-profile-settings.d.ts +45 -45
- package/packages/ui/ai-profile-settings.js +283 -283
- package/packages/ui/claude-terminal.d.ts +31 -31
- package/packages/ui/claude-terminal.js +343 -343
- package/packages/ui/index.d.ts +18 -18
- package/packages/ui/index.js +51 -51
- package/packages/ui/package.json +2 -2
- package/packages/ui/theme.css +13 -13
- package/src/assets/claude-prompts.json +21 -21
- package/src/backend/http/ai-profile-routes.js +61 -61
- package/src/backend/http/claude-routes.js +170 -170
- package/src/backend/http/node-helpers.js +45 -45
- package/src/backend/index.js +21 -21
- package/src/backend/lib/ai-adapter.js +144 -144
- package/src/backend/lib/ai-profile-store.js +259 -259
- package/src/backend/lib/ai-vendor-presets.js +83 -83
- package/src/backend/lib/ai-workspace.js +140 -140
- package/src/backend/lib/automation-config.js +253 -253
- package/src/backend/lib/claude-cli-runner.js +2408 -2392
- package/src/backend/lib/data-dir.js +169 -169
- package/src/backend/lib/llm-client.js +176 -176
- package/src/backend/lib/model-context-windows.js +82 -82
- package/src/backend/lib/native-task-control.js +217 -217
- package/src/backend/lib/native-task-history-index.js +475 -475
- package/src/backend/lib/native-task-history-reader.js +526 -526
- package/src/backend/lib/native-task-messages.js +457 -457
- package/src/backend/lib/native-task-registry.js +325 -325
- package/src/backend/lib/native-task-runtime.js +242 -242
- package/src/backend/lib/native-task-trace.js +440 -440
- package/src/backend/lib/native-task-transcript.js +393 -393
- package/src/backend/lib/prompt-registry.js +166 -166
- package/src/backend/lib/runtime-config.js +121 -121
- package/src/backend/lib/sdk-session-stream.js +143 -143
- package/src/backend/lib/sdk-turn-stream.js +16 -16
- package/src/backend/lib/store-scope.js +131 -131
- package/src/backend/lib/task-identity.js +104 -104
- package/src/backend/lib/task-state-projection.js +58 -58
- package/src/index.js +7 -7
- package/src/sdk-wave-a.ts +1 -1
- package/src/sdk-wave-b.ts +1 -1
- package/src/sdk-wave-c.ts +1 -1
- package/src/sdk-wave-d.ts +1 -1
- package/src/sdk-wave-e.ts +1 -1
|
@@ -1,83 +1,83 @@
|
|
|
1
|
-
// Built-in vendor presets for common Anthropic-compatible providers.
|
|
2
|
-
// baseUrl and model names verified against each vendor's public docs (2026-06).
|
|
3
|
-
// When a user picks one of these vendor names in the profile form, all fields
|
|
4
|
-
// below are auto-filled — they only need to supply an API key.
|
|
5
|
-
//
|
|
6
|
-
// baseUrl MUST be the /anthropic-compatible endpoint, not the OpenAI-compatible
|
|
7
|
-
// one, because the runner speaks the Anthropic Messages API. Wrong path → 404.
|
|
8
|
-
const AI_VENDOR_PRESETS = [
|
|
9
|
-
{
|
|
10
|
-
id: 'anthropic',
|
|
11
|
-
name: 'Anthropic Claude',
|
|
12
|
-
baseUrl: 'https://api.anthropic.com',
|
|
13
|
-
mainModel: 'claude-sonnet-4-5',
|
|
14
|
-
thinkingModel: 'claude-opus-4-7',
|
|
15
|
-
haikuModel: 'claude-haiku-4-5',
|
|
16
|
-
sonnetModel: 'claude-sonnet-4-5',
|
|
17
|
-
opusModel: 'claude-opus-4-7',
|
|
18
|
-
contextWindow: 200000,
|
|
19
|
-
website: 'https://www.anthropic.com',
|
|
20
|
-
},
|
|
21
|
-
{
|
|
22
|
-
id: 'minimax',
|
|
23
|
-
name: 'MiniMax',
|
|
24
|
-
// Domestic (China) endpoint. minimax.io is the overseas domain with a
|
|
25
|
-
// separate account system — API keys are NOT interchangeable. Domestic
|
|
26
|
-
// users register at minimaxi.com, so we default to that.
|
|
27
|
-
baseUrl: 'https://api.minimaxi.com/anthropic',
|
|
28
|
-
mainModel: 'MiniMax-M3',
|
|
29
|
-
thinkingModel: 'MiniMax-M3',
|
|
30
|
-
haikuModel: 'MiniMax-M3',
|
|
31
|
-
sonnetModel: 'MiniMax-M3',
|
|
32
|
-
opusModel: 'MiniMax-M3',
|
|
33
|
-
contextWindow: 1048576,
|
|
34
|
-
website: 'https://platform.minimaxi.com',
|
|
35
|
-
},
|
|
36
|
-
{
|
|
37
|
-
id: 'zhipu',
|
|
38
|
-
name: '智谱 GLM',
|
|
39
|
-
// Anthropic-compatible endpoint, not the OpenAI-style /api/paas/v4.
|
|
40
|
-
baseUrl: 'https://open.bigmodel.cn/api/anthropic',
|
|
41
|
-
mainModel: 'glm-4.6',
|
|
42
|
-
thinkingModel: 'glm-4.6',
|
|
43
|
-
haikuModel: 'glm-4.5-air',
|
|
44
|
-
sonnetModel: 'glm-4.6',
|
|
45
|
-
opusModel: 'glm-4.6',
|
|
46
|
-
contextWindow: 1048576,
|
|
47
|
-
website: 'https://open.bigmodel.cn',
|
|
48
|
-
},
|
|
49
|
-
{
|
|
50
|
-
id: 'deepseek',
|
|
51
|
-
name: 'DeepSeek',
|
|
52
|
-
// Anthropic-compatible endpoint at /anthropic, not the bare domain.
|
|
53
|
-
baseUrl: 'https://api.deepseek.com/anthropic',
|
|
54
|
-
mainModel: 'deepseek-chat',
|
|
55
|
-
thinkingModel: 'deepseek-reasoner',
|
|
56
|
-
haikuModel: 'deepseek-chat',
|
|
57
|
-
sonnetModel: 'deepseek-chat',
|
|
58
|
-
opusModel: 'deepseek-reasoner',
|
|
59
|
-
contextWindow: 128000,
|
|
60
|
-
website: 'https://platform.deepseek.com',
|
|
61
|
-
},
|
|
62
|
-
];
|
|
63
|
-
|
|
64
|
-
function listVendorPresetNames() {
|
|
65
|
-
return AI_VENDOR_PRESETS.map(v => v.name);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// Match by name (case-insensitive) or id (case-insensitive). Returns the
|
|
69
|
-
// preset object or null. Custom vendor names that don't match return null —
|
|
70
|
-
// the caller then leaves the user-typed value alone.
|
|
71
|
-
function findVendorPresetByName(name) {
|
|
72
|
-
const norm = String(name || '').trim().toLowerCase();
|
|
73
|
-
if (!norm) return null;
|
|
74
|
-
return AI_VENDOR_PRESETS.find(v =>
|
|
75
|
-
v.name.toLowerCase() === norm || v.id.toLowerCase() === norm
|
|
76
|
-
) || null;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
module.exports = {
|
|
80
|
-
AI_VENDOR_PRESETS,
|
|
81
|
-
listVendorPresetNames,
|
|
82
|
-
findVendorPresetByName,
|
|
83
|
-
};
|
|
1
|
+
// Built-in vendor presets for common Anthropic-compatible providers.
|
|
2
|
+
// baseUrl and model names verified against each vendor's public docs (2026-06).
|
|
3
|
+
// When a user picks one of these vendor names in the profile form, all fields
|
|
4
|
+
// below are auto-filled — they only need to supply an API key.
|
|
5
|
+
//
|
|
6
|
+
// baseUrl MUST be the /anthropic-compatible endpoint, not the OpenAI-compatible
|
|
7
|
+
// one, because the runner speaks the Anthropic Messages API. Wrong path → 404.
|
|
8
|
+
const AI_VENDOR_PRESETS = [
|
|
9
|
+
{
|
|
10
|
+
id: 'anthropic',
|
|
11
|
+
name: 'Anthropic Claude',
|
|
12
|
+
baseUrl: 'https://api.anthropic.com',
|
|
13
|
+
mainModel: 'claude-sonnet-4-5',
|
|
14
|
+
thinkingModel: 'claude-opus-4-7',
|
|
15
|
+
haikuModel: 'claude-haiku-4-5',
|
|
16
|
+
sonnetModel: 'claude-sonnet-4-5',
|
|
17
|
+
opusModel: 'claude-opus-4-7',
|
|
18
|
+
contextWindow: 200000,
|
|
19
|
+
website: 'https://www.anthropic.com',
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
id: 'minimax',
|
|
23
|
+
name: 'MiniMax',
|
|
24
|
+
// Domestic (China) endpoint. minimax.io is the overseas domain with a
|
|
25
|
+
// separate account system — API keys are NOT interchangeable. Domestic
|
|
26
|
+
// users register at minimaxi.com, so we default to that.
|
|
27
|
+
baseUrl: 'https://api.minimaxi.com/anthropic',
|
|
28
|
+
mainModel: 'MiniMax-M3',
|
|
29
|
+
thinkingModel: 'MiniMax-M3',
|
|
30
|
+
haikuModel: 'MiniMax-M3',
|
|
31
|
+
sonnetModel: 'MiniMax-M3',
|
|
32
|
+
opusModel: 'MiniMax-M3',
|
|
33
|
+
contextWindow: 1048576,
|
|
34
|
+
website: 'https://platform.minimaxi.com',
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
id: 'zhipu',
|
|
38
|
+
name: '智谱 GLM',
|
|
39
|
+
// Anthropic-compatible endpoint, not the OpenAI-style /api/paas/v4.
|
|
40
|
+
baseUrl: 'https://open.bigmodel.cn/api/anthropic',
|
|
41
|
+
mainModel: 'glm-4.6',
|
|
42
|
+
thinkingModel: 'glm-4.6',
|
|
43
|
+
haikuModel: 'glm-4.5-air',
|
|
44
|
+
sonnetModel: 'glm-4.6',
|
|
45
|
+
opusModel: 'glm-4.6',
|
|
46
|
+
contextWindow: 1048576,
|
|
47
|
+
website: 'https://open.bigmodel.cn',
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
id: 'deepseek',
|
|
51
|
+
name: 'DeepSeek',
|
|
52
|
+
// Anthropic-compatible endpoint at /anthropic, not the bare domain.
|
|
53
|
+
baseUrl: 'https://api.deepseek.com/anthropic',
|
|
54
|
+
mainModel: 'deepseek-chat',
|
|
55
|
+
thinkingModel: 'deepseek-reasoner',
|
|
56
|
+
haikuModel: 'deepseek-chat',
|
|
57
|
+
sonnetModel: 'deepseek-chat',
|
|
58
|
+
opusModel: 'deepseek-reasoner',
|
|
59
|
+
contextWindow: 128000,
|
|
60
|
+
website: 'https://platform.deepseek.com',
|
|
61
|
+
},
|
|
62
|
+
];
|
|
63
|
+
|
|
64
|
+
function listVendorPresetNames() {
|
|
65
|
+
return AI_VENDOR_PRESETS.map(v => v.name);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Match by name (case-insensitive) or id (case-insensitive). Returns the
|
|
69
|
+
// preset object or null. Custom vendor names that don't match return null —
|
|
70
|
+
// the caller then leaves the user-typed value alone.
|
|
71
|
+
function findVendorPresetByName(name) {
|
|
72
|
+
const norm = String(name || '').trim().toLowerCase();
|
|
73
|
+
if (!norm) return null;
|
|
74
|
+
return AI_VENDOR_PRESETS.find(v =>
|
|
75
|
+
v.name.toLowerCase() === norm || v.id.toLowerCase() === norm
|
|
76
|
+
) || null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
module.exports = {
|
|
80
|
+
AI_VENDOR_PRESETS,
|
|
81
|
+
listVendorPresetNames,
|
|
82
|
+
findVendorPresetByName,
|
|
83
|
+
};
|
|
@@ -1,140 +1,140 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const { getDataDir } = require('./data-dir');
|
|
4
|
-
const { getWorkbenchConfig } = require('./runtime-config');
|
|
5
|
-
|
|
6
|
-
const SITE_ROOT = path.resolve(__dirname, '..');
|
|
7
|
-
|
|
8
|
-
function appRoot() {
|
|
9
|
-
return getDataDir();
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function aiRoot() {
|
|
13
|
-
const cfg = getWorkbenchConfig();
|
|
14
|
-
return path.join(appRoot(), cfg.aiRootDirName || '_ai');
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function standardSubdirs() {
|
|
18
|
-
const cfg = getWorkbenchConfig();
|
|
19
|
-
const dirs = Array.isArray(cfg.standardSubdirs) && cfg.standardSubdirs.length
|
|
20
|
-
? cfg.standardSubdirs.slice()
|
|
21
|
-
: ['drafts', 'runs', 'context-packs', 'backups'];
|
|
22
|
-
if (cfg.sessionDirName && !dirs.includes(cfg.sessionDirName)) dirs.push(cfg.sessionDirName);
|
|
23
|
-
return dirs;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function safeSlug(slug) {
|
|
27
|
-
return typeof slug === 'string' && /^[a-z0-9][a-z0-9-]{0,40}$/.test(slug);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function projectAIPath(slug) {
|
|
31
|
-
if (!safeSlug(slug)) throw new Error(`invalid slug for AI workspace: ${slug}`);
|
|
32
|
-
return path.join(aiRoot(), slug);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function ensureProjectAIPath(slug) {
|
|
36
|
-
const root = projectAIPath(slug);
|
|
37
|
-
for (const sub of standardSubdirs()) {
|
|
38
|
-
fs.mkdirSync(path.join(root, sub), { recursive: true });
|
|
39
|
-
}
|
|
40
|
-
return root;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function legacyAIPath(kbPath) {
|
|
44
|
-
return path.join(kbPath, '_ai');
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function copyDir(src, dest) {
|
|
48
|
-
fs.mkdirSync(dest, { recursive: true });
|
|
49
|
-
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
|
|
50
|
-
const from = path.join(src, entry.name);
|
|
51
|
-
const to = path.join(dest, entry.name);
|
|
52
|
-
if (entry.isDirectory()) copyDir(from, to);
|
|
53
|
-
else if (entry.isFile()) fs.copyFileSync(from, to);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function migrateAIWorkspace({ slug, kbPath, preserveOriginal = true }) {
|
|
58
|
-
const legacy = legacyAIPath(kbPath);
|
|
59
|
-
const target = ensureProjectAIPath(slug);
|
|
60
|
-
const result = {
|
|
61
|
-
ok: true,
|
|
62
|
-
slug,
|
|
63
|
-
legacyPath: legacy,
|
|
64
|
-
targetPath: target,
|
|
65
|
-
copied: false,
|
|
66
|
-
preservedOriginal: preserveOriginal,
|
|
67
|
-
};
|
|
68
|
-
if (!fs.existsSync(legacy)) return result;
|
|
69
|
-
copyDir(legacy, target);
|
|
70
|
-
result.copied = true;
|
|
71
|
-
if (!preserveOriginal) {
|
|
72
|
-
fs.rmSync(legacy, { recursive: true, force: true });
|
|
73
|
-
result.preservedOriginal = false;
|
|
74
|
-
}
|
|
75
|
-
return result;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function runPath(slug, runId) {
|
|
79
|
-
return path.join(projectAIPath(slug), 'runs', `${runId}.json`);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function draftDir(slug, runId) {
|
|
83
|
-
return path.join(projectAIPath(slug), 'drafts', runId);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
function contextPackDir(slug, runId) {
|
|
87
|
-
return path.join(projectAIPath(slug), 'context-packs', runId);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function findExistingRunPath({ slug, kbPath, runId }) {
|
|
91
|
-
const primary = path.join(projectAIPath(slug), 'runs', `${runId}.json`);
|
|
92
|
-
return primary;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function findExistingDraftDir({ slug, kbPath, runId }) {
|
|
96
|
-
const primary = path.join(projectAIPath(slug), 'drafts', runId);
|
|
97
|
-
return primary;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function listProjectDirs(projectSlug = null, projects = null) {
|
|
101
|
-
if (projectSlug) return [projectAIPath(projectSlug)];
|
|
102
|
-
const dirs = [];
|
|
103
|
-
const root = aiRoot();
|
|
104
|
-
if (fs.existsSync(root)) {
|
|
105
|
-
for (const entry of fs.readdirSync(root, { withFileTypes: true })) {
|
|
106
|
-
if (entry.isDirectory() && safeSlug(entry.name)) dirs.push(path.join(root, entry.name));
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
if (projects) {
|
|
110
|
-
for (const [slug] of Object.entries(projects)) {
|
|
111
|
-
if (safeSlug(slug)) dirs.push(projectAIPath(slug));
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
return [...new Set(dirs.map(d => path.resolve(d)))];
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const exported = {
|
|
118
|
-
SITE_ROOT,
|
|
119
|
-
appRoot,
|
|
120
|
-
aiRoot,
|
|
121
|
-
standardSubdirs,
|
|
122
|
-
projectAIPath,
|
|
123
|
-
ensureProjectAIPath,
|
|
124
|
-
legacyAIPath,
|
|
125
|
-
migrateAIWorkspace,
|
|
126
|
-
runPath,
|
|
127
|
-
draftDir,
|
|
128
|
-
contextPackDir,
|
|
129
|
-
findExistingRunPath,
|
|
130
|
-
findExistingDraftDir,
|
|
131
|
-
listProjectDirs,
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
Object.defineProperties(exported, {
|
|
135
|
-
APP_ROOT: { enumerable: true, get: appRoot },
|
|
136
|
-
AI_ROOT: { enumerable: true, get: aiRoot },
|
|
137
|
-
STANDARD_SUBDIRS: { enumerable: true, get: standardSubdirs },
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
module.exports = exported;
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const { getDataDir } = require('./data-dir');
|
|
4
|
+
const { getWorkbenchConfig } = require('./runtime-config');
|
|
5
|
+
|
|
6
|
+
const SITE_ROOT = path.resolve(__dirname, '..');
|
|
7
|
+
|
|
8
|
+
function appRoot() {
|
|
9
|
+
return getDataDir();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function aiRoot() {
|
|
13
|
+
const cfg = getWorkbenchConfig();
|
|
14
|
+
return path.join(appRoot(), cfg.aiRootDirName || '_ai');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function standardSubdirs() {
|
|
18
|
+
const cfg = getWorkbenchConfig();
|
|
19
|
+
const dirs = Array.isArray(cfg.standardSubdirs) && cfg.standardSubdirs.length
|
|
20
|
+
? cfg.standardSubdirs.slice()
|
|
21
|
+
: ['drafts', 'runs', 'context-packs', 'backups'];
|
|
22
|
+
if (cfg.sessionDirName && !dirs.includes(cfg.sessionDirName)) dirs.push(cfg.sessionDirName);
|
|
23
|
+
return dirs;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function safeSlug(slug) {
|
|
27
|
+
return typeof slug === 'string' && /^[a-z0-9][a-z0-9-]{0,40}$/.test(slug);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function projectAIPath(slug) {
|
|
31
|
+
if (!safeSlug(slug)) throw new Error(`invalid slug for AI workspace: ${slug}`);
|
|
32
|
+
return path.join(aiRoot(), slug);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function ensureProjectAIPath(slug) {
|
|
36
|
+
const root = projectAIPath(slug);
|
|
37
|
+
for (const sub of standardSubdirs()) {
|
|
38
|
+
fs.mkdirSync(path.join(root, sub), { recursive: true });
|
|
39
|
+
}
|
|
40
|
+
return root;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function legacyAIPath(kbPath) {
|
|
44
|
+
return path.join(kbPath, '_ai');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function copyDir(src, dest) {
|
|
48
|
+
fs.mkdirSync(dest, { recursive: true });
|
|
49
|
+
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
|
|
50
|
+
const from = path.join(src, entry.name);
|
|
51
|
+
const to = path.join(dest, entry.name);
|
|
52
|
+
if (entry.isDirectory()) copyDir(from, to);
|
|
53
|
+
else if (entry.isFile()) fs.copyFileSync(from, to);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function migrateAIWorkspace({ slug, kbPath, preserveOriginal = true }) {
|
|
58
|
+
const legacy = legacyAIPath(kbPath);
|
|
59
|
+
const target = ensureProjectAIPath(slug);
|
|
60
|
+
const result = {
|
|
61
|
+
ok: true,
|
|
62
|
+
slug,
|
|
63
|
+
legacyPath: legacy,
|
|
64
|
+
targetPath: target,
|
|
65
|
+
copied: false,
|
|
66
|
+
preservedOriginal: preserveOriginal,
|
|
67
|
+
};
|
|
68
|
+
if (!fs.existsSync(legacy)) return result;
|
|
69
|
+
copyDir(legacy, target);
|
|
70
|
+
result.copied = true;
|
|
71
|
+
if (!preserveOriginal) {
|
|
72
|
+
fs.rmSync(legacy, { recursive: true, force: true });
|
|
73
|
+
result.preservedOriginal = false;
|
|
74
|
+
}
|
|
75
|
+
return result;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function runPath(slug, runId) {
|
|
79
|
+
return path.join(projectAIPath(slug), 'runs', `${runId}.json`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function draftDir(slug, runId) {
|
|
83
|
+
return path.join(projectAIPath(slug), 'drafts', runId);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function contextPackDir(slug, runId) {
|
|
87
|
+
return path.join(projectAIPath(slug), 'context-packs', runId);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function findExistingRunPath({ slug, kbPath, runId }) {
|
|
91
|
+
const primary = path.join(projectAIPath(slug), 'runs', `${runId}.json`);
|
|
92
|
+
return primary;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function findExistingDraftDir({ slug, kbPath, runId }) {
|
|
96
|
+
const primary = path.join(projectAIPath(slug), 'drafts', runId);
|
|
97
|
+
return primary;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function listProjectDirs(projectSlug = null, projects = null) {
|
|
101
|
+
if (projectSlug) return [projectAIPath(projectSlug)];
|
|
102
|
+
const dirs = [];
|
|
103
|
+
const root = aiRoot();
|
|
104
|
+
if (fs.existsSync(root)) {
|
|
105
|
+
for (const entry of fs.readdirSync(root, { withFileTypes: true })) {
|
|
106
|
+
if (entry.isDirectory() && safeSlug(entry.name)) dirs.push(path.join(root, entry.name));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (projects) {
|
|
110
|
+
for (const [slug] of Object.entries(projects)) {
|
|
111
|
+
if (safeSlug(slug)) dirs.push(projectAIPath(slug));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return [...new Set(dirs.map(d => path.resolve(d)))];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const exported = {
|
|
118
|
+
SITE_ROOT,
|
|
119
|
+
appRoot,
|
|
120
|
+
aiRoot,
|
|
121
|
+
standardSubdirs,
|
|
122
|
+
projectAIPath,
|
|
123
|
+
ensureProjectAIPath,
|
|
124
|
+
legacyAIPath,
|
|
125
|
+
migrateAIWorkspace,
|
|
126
|
+
runPath,
|
|
127
|
+
draftDir,
|
|
128
|
+
contextPackDir,
|
|
129
|
+
findExistingRunPath,
|
|
130
|
+
findExistingDraftDir,
|
|
131
|
+
listProjectDirs,
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
Object.defineProperties(exported, {
|
|
135
|
+
APP_ROOT: { enumerable: true, get: appRoot },
|
|
136
|
+
AI_ROOT: { enumerable: true, get: aiRoot },
|
|
137
|
+
STANDARD_SUBDIRS: { enumerable: true, get: standardSubdirs },
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
module.exports = exported;
|