@leterhong/multichat 1.6.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/.agents/plugins/marketplace.json +32 -0
- package/.agents/plugins/plugins/developer-toolkit/.codex-plugin/plugin.json +24 -0
- package/.agents/plugins/plugins/developer-toolkit/skills/code-review/SKILL.md +9 -0
- package/.agents/plugins/plugins/developer-toolkit/skills/code-review/agents/openai.yaml +3 -0
- package/.agents/plugins/plugins/developer-toolkit/skills/release-notes/SKILL.md +9 -0
- package/.agents/plugins/plugins/developer-toolkit/skills/release-notes/agents/openai.yaml +3 -0
- package/.agents/plugins/plugins/developer-toolkit/skills/security-review/SKILL.md +9 -0
- package/.agents/plugins/plugins/developer-toolkit/skills/security-review/agents/openai.yaml +3 -0
- package/.agents/plugins/plugins/developer-toolkit/skills/test-plan/SKILL.md +9 -0
- package/.agents/plugins/plugins/developer-toolkit/skills/test-plan/agents/openai.yaml +3 -0
- package/.agents/plugins/plugins/writing-toolkit/.codex-plugin/plugin.json +24 -0
- package/.agents/plugins/plugins/writing-toolkit/skills/meeting-notes/SKILL.md +9 -0
- package/.agents/plugins/plugins/writing-toolkit/skills/meeting-notes/agents/openai.yaml +3 -0
- package/.agents/plugins/plugins/writing-toolkit/skills/resume-tune/SKILL.md +9 -0
- package/.agents/plugins/plugins/writing-toolkit/skills/resume-tune/agents/openai.yaml +3 -0
- package/.agents/plugins/plugins/writing-toolkit/skills/weekly-report/SKILL.md +9 -0
- package/.agents/plugins/plugins/writing-toolkit/skills/weekly-report/agents/openai.yaml +3 -0
- package/.agents/skills/.gitkeep +1 -0
- package/.agents/skills/wanghong-handwritten-ppt/SKILL.md +172 -0
- package/.agents/skills/wanghong-handwritten-ppt/assets/HTML-PPT-LICENSE +21 -0
- package/.agents/skills/wanghong-handwritten-ppt/assets/NEAT-ANNOTATIONS-LICENSE +21 -0
- package/.agents/skills/wanghong-handwritten-ppt/assets/animations.css +138 -0
- package/.agents/skills/wanghong-handwritten-ppt/assets/base.css +150 -0
- package/.agents/skills/wanghong-handwritten-ppt/assets/deepseek-example.css +893 -0
- package/.agents/skills/wanghong-handwritten-ppt/assets/neat-annotations.css +152 -0
- package/.agents/skills/wanghong-handwritten-ppt/assets/preview-cover.png +0 -0
- package/.agents/skills/wanghong-handwritten-ppt/assets/preview-diagram.png +0 -0
- package/.agents/skills/wanghong-handwritten-ppt/assets/preview-summary.png +0 -0
- package/.agents/skills/wanghong-handwritten-ppt/assets/preview-table.png +0 -0
- package/.agents/skills/wanghong-handwritten-ppt/assets/runtime.js +960 -0
- package/.agents/skills/wanghong-handwritten-ppt/assets/template.css +198 -0
- package/.agents/skills/wanghong-handwritten-ppt/evals/evals.json +20 -0
- package/.agents/skills/wanghong-handwritten-ppt/examples/deepseek-v4-flash/index.html +414 -0
- package/.agents/skills/wanghong-handwritten-ppt/examples/deepseek-v4-flash/style.css +897 -0
- package/.agents/skills/wanghong-handwritten-ppt/references/prompts.md +170 -0
- package/.agents/skills/wanghong-handwritten-ppt/references/style-guide.md +133 -0
- package/.agents/skills/wanghong-handwritten-ppt/scripts/check_deck.py +41 -0
- package/.agents/skills/wanghong-handwritten-ppt/scripts/prepare_render_html.py +137 -0
- package/.agents/skills/wanghong-handwritten-ppt/scripts/render.sh +110 -0
- package/.agents/skills/wanghong-handwritten-ppt/templates/deck.html +94 -0
- package/.agents/skills/wanghong-handwritten-ppt/tests/test_font_contract.py +73 -0
- package/LICENSE +21 -0
- package/README.md +186 -0
- package/backend/adapters/README.md +77 -0
- package/backend/adapters/anthropic.ts +115 -0
- package/backend/adapters/base.ts +47 -0
- package/backend/adapters/index.ts +41 -0
- package/backend/adapters/lmstudio.ts +7 -0
- package/backend/adapters/moonshot.ts +7 -0
- package/backend/adapters/ollama.ts +7 -0
- package/backend/adapters/openai-compatible.ts +51 -0
- package/backend/adapters/openai.ts +7 -0
- package/backend/adapters/qwen.ts +112 -0
- package/backend/adapters/wenxin.ts +136 -0
- package/backend/adapters/zhipu.ts +93 -0
- package/backend/dist/adapters/anthropic.js +130 -0
- package/backend/dist/adapters/anthropic.js.map +1 -0
- package/backend/dist/adapters/base.js +38 -0
- package/backend/dist/adapters/base.js.map +1 -0
- package/backend/dist/adapters/index.js +39 -0
- package/backend/dist/adapters/index.js.map +1 -0
- package/backend/dist/adapters/lmstudio.js +8 -0
- package/backend/dist/adapters/lmstudio.js.map +1 -0
- package/backend/dist/adapters/moonshot.js +8 -0
- package/backend/dist/adapters/moonshot.js.map +1 -0
- package/backend/dist/adapters/ollama.js +8 -0
- package/backend/dist/adapters/ollama.js.map +1 -0
- package/backend/dist/adapters/openai-compatible.js +47 -0
- package/backend/dist/adapters/openai-compatible.js.map +1 -0
- package/backend/dist/adapters/openai.js +8 -0
- package/backend/dist/adapters/openai.js.map +1 -0
- package/backend/dist/adapters/qwen.js +103 -0
- package/backend/dist/adapters/qwen.js.map +1 -0
- package/backend/dist/adapters/wenxin.js +114 -0
- package/backend/dist/adapters/wenxin.js.map +1 -0
- package/backend/dist/adapters/zhipu.js +86 -0
- package/backend/dist/adapters/zhipu.js.map +1 -0
- package/backend/dist/extensions/demo-mcp-server.js +75 -0
- package/backend/dist/extensions/demo-mcp-server.js.map +1 -0
- package/backend/dist/extensions/importer.js +828 -0
- package/backend/dist/extensions/importer.js.map +1 -0
- package/backend/dist/extensions/manager.js +1154 -0
- package/backend/dist/extensions/manager.js.map +1 -0
- package/backend/dist/lib/catalog.js +30 -0
- package/backend/dist/lib/catalog.js.map +1 -0
- package/backend/dist/lib/context.js +59 -0
- package/backend/dist/lib/context.js.map +1 -0
- package/backend/dist/lib/errors.js +75 -0
- package/backend/dist/lib/errors.js.map +1 -0
- package/backend/dist/lib/provider-store.js +116 -0
- package/backend/dist/lib/provider-store.js.map +1 -0
- package/backend/dist/lib/redact.js +11 -0
- package/backend/dist/lib/redact.js.map +1 -0
- package/backend/dist/lib/runtime-store.js +28 -0
- package/backend/dist/lib/runtime-store.js.map +1 -0
- package/backend/dist/lib/sqlite-store.js +218 -0
- package/backend/dist/lib/sqlite-store.js.map +1 -0
- package/backend/dist/lib/sse.js +46 -0
- package/backend/dist/lib/sse.js.map +1 -0
- package/backend/dist/lib/ssrf.js +131 -0
- package/backend/dist/lib/ssrf.js.map +1 -0
- package/backend/dist/lib/store.js +84 -0
- package/backend/dist/lib/store.js.map +1 -0
- package/backend/dist/lib/usage.js +219 -0
- package/backend/dist/lib/usage.js.map +1 -0
- package/backend/dist/lib/util.js +38 -0
- package/backend/dist/lib/util.js.map +1 -0
- package/backend/dist/lib/workspace-store.js +152 -0
- package/backend/dist/lib/workspace-store.js.map +1 -0
- package/backend/dist/mcp.js +429 -0
- package/backend/dist/mcp.js.map +1 -0
- package/backend/dist/routes/agents.js +51 -0
- package/backend/dist/routes/agents.js.map +1 -0
- package/backend/dist/routes/assistants.js +32 -0
- package/backend/dist/routes/assistants.js.map +1 -0
- package/backend/dist/routes/chat.js +697 -0
- package/backend/dist/routes/chat.js.map +1 -0
- package/backend/dist/routes/control-plane.js +200 -0
- package/backend/dist/routes/control-plane.js.map +1 -0
- package/backend/dist/routes/conversations.js +66 -0
- package/backend/dist/routes/conversations.js.map +1 -0
- package/backend/dist/routes/extensions-import.js +22 -0
- package/backend/dist/routes/extensions-import.js.map +1 -0
- package/backend/dist/routes/import.js +85 -0
- package/backend/dist/routes/import.js.map +1 -0
- package/backend/dist/routes/mcp-servers.js +56 -0
- package/backend/dist/routes/mcp-servers.js.map +1 -0
- package/backend/dist/routes/meta.js +37 -0
- package/backend/dist/routes/meta.js.map +1 -0
- package/backend/dist/routes/mock.js +28 -0
- package/backend/dist/routes/mock.js.map +1 -0
- package/backend/dist/routes/plugins.js +41 -0
- package/backend/dist/routes/plugins.js.map +1 -0
- package/backend/dist/routes/providers.js +197 -0
- package/backend/dist/routes/providers.js.map +1 -0
- package/backend/dist/routes/runs.js +57 -0
- package/backend/dist/routes/runs.js.map +1 -0
- package/backend/dist/routes/skills.js +73 -0
- package/backend/dist/routes/skills.js.map +1 -0
- package/backend/dist/routes/tools.js +40 -0
- package/backend/dist/routes/tools.js.map +1 -0
- package/backend/dist/routes/usage.js +14 -0
- package/backend/dist/routes/usage.js.map +1 -0
- package/backend/dist/routes/workspaces.js +158 -0
- package/backend/dist/routes/workspaces.js.map +1 -0
- package/backend/dist/runtime/agent.js +518 -0
- package/backend/dist/runtime/agent.js.map +1 -0
- package/backend/dist/server.js +103 -0
- package/backend/dist/server.js.map +1 -0
- package/backend/dist/types.js +3 -0
- package/backend/dist/types.js.map +1 -0
- package/backend/extensions/demo-mcp-server.js +70 -0
- package/backend/extensions/importer.ts +709 -0
- package/backend/extensions/manager.ts +1055 -0
- package/backend/lib/catalog.ts +28 -0
- package/backend/lib/context.ts +61 -0
- package/backend/lib/errors.ts +78 -0
- package/backend/lib/provider-store.ts +116 -0
- package/backend/lib/redact.ts +9 -0
- package/backend/lib/runtime-store.ts +38 -0
- package/backend/lib/sqlite-store.ts +220 -0
- package/backend/lib/sse.ts +42 -0
- package/backend/lib/ssrf.ts +120 -0
- package/backend/lib/store.ts +79 -0
- package/backend/lib/usage.ts +212 -0
- package/backend/lib/util.ts +31 -0
- package/backend/lib/workspace-store.ts +150 -0
- package/backend/mcp.ts +417 -0
- package/backend/package.json +34 -0
- package/backend/prompts.json +82 -0
- package/backend/routes/agents.ts +55 -0
- package/backend/routes/assistants.ts +30 -0
- package/backend/routes/chat.ts +660 -0
- package/backend/routes/control-plane.ts +165 -0
- package/backend/routes/conversations.ts +62 -0
- package/backend/routes/extensions-import.ts +15 -0
- package/backend/routes/import.ts +72 -0
- package/backend/routes/mcp-servers.ts +36 -0
- package/backend/routes/meta.ts +38 -0
- package/backend/routes/mock.ts +25 -0
- package/backend/routes/plugins.ts +27 -0
- package/backend/routes/providers.ts +166 -0
- package/backend/routes/runs.ts +55 -0
- package/backend/routes/skills.ts +52 -0
- package/backend/routes/tools.ts +40 -0
- package/backend/routes/usage.ts +13 -0
- package/backend/routes/workspaces.ts +124 -0
- package/backend/runtime/agent.ts +474 -0
- package/backend/server.ts +106 -0
- package/backend/types.ts +126 -0
- package/bin/multichat.mjs +535 -0
- package/frontend/dist/assets/agentPicker-C39VzmYa.js +13 -0
- package/frontend/dist/assets/compare-BpaGD32J.js +1 -0
- package/frontend/dist/assets/conversations-COuLmg99.js +7 -0
- package/frontend/dist/assets/data-ZH7Dux1K.js +1 -0
- package/frontend/dist/assets/extensionImport-CnTWKtWm.js +28 -0
- package/frontend/dist/assets/importExport-B3mB-9zs.js +70 -0
- package/frontend/dist/assets/index-CASVCOUO.js +34 -0
- package/frontend/dist/assets/index-DSvbjNLc.css +1 -0
- package/frontend/dist/assets/init-ZPK483vB.js +1 -0
- package/frontend/dist/assets/jsx-runtime-ZSsbr0oW.js +9 -0
- package/frontend/dist/assets/modal-CgHocv3n.js +2 -0
- package/frontend/dist/assets/modelPicker-EUmmVnbj.js +14 -0
- package/frontend/dist/assets/pluginsUI-B6jGUQkx.js +16 -0
- package/frontend/dist/assets/render-DgAg1K8A.js +1 -0
- package/frontend/dist/assets/send-BJhZ_5YA.js +14 -0
- package/frontend/dist/assets/settings-DdnYwZl-.js +2 -0
- package/frontend/dist/assets/shell-DFV2khQl.js +1 -0
- package/frontend/dist/assets/workbench-BY2J4vWC.js +41 -0
- package/frontend/dist/icon-maskable.svg +4 -0
- package/frontend/dist/icon.svg +4 -0
- package/frontend/dist/index.html +21 -0
- package/frontend/dist/manifest.webmanifest +26 -0
- package/package.json +58 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// ── Runs(Agent 运行历史)路由 ──────────────────────────────────────────
|
|
3
|
+
const ctx = require('../lib/context');
|
|
4
|
+
|
|
5
|
+
module.exports = function registerRuns(app) {
|
|
6
|
+
app.get('/api/runs', (req, res) => {
|
|
7
|
+
const limit = Math.min(100, Math.max(1, Number.parseInt(req.query.limit || '40', 10) || 40));
|
|
8
|
+
let runs = ctx.store.read(ctx.RUN_FILE, []);
|
|
9
|
+
if (req.query.agent) runs = runs.filter(x => x.agentId === req.query.agent);
|
|
10
|
+
res.json(runs.slice(0, limit));
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
app.get('/api/runs/:id', (req, res) => {
|
|
14
|
+
const run = ctx.store.read(ctx.RUN_FILE, []).find(x => x.id === req.params.id);
|
|
15
|
+
if (!run) return res.status(404).json({ error: 'run not found' });
|
|
16
|
+
res.json(run);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
// 取消正在运行的 Agent 任务:中断上游 fetch,并取消当前 MCP request。
|
|
20
|
+
app.post('/api/runs/:id/cancel', (req, res) => {
|
|
21
|
+
const ac = ctx.runAborts.get(req.params.id);
|
|
22
|
+
if (!ac) {
|
|
23
|
+
return res.status(404).json({ error: { message: '没有正在运行的任务或未注册取消句柄(可能已结束)' }, code: 'RUN_NOT_ACTIVE' });
|
|
24
|
+
}
|
|
25
|
+
ac.abort();
|
|
26
|
+
ctx.runAborts.delete(req.params.id);
|
|
27
|
+
res.json({ ok: true, id: req.params.id });
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
// 审批决策:写入持久化检查点,不占用原 Agent 响应连接。
|
|
31
|
+
app.post('/api/runs/:id/approval/:approvalId', (req, res, next) => {
|
|
32
|
+
void (async () => {
|
|
33
|
+
const run = ctx.store.read(ctx.RUN_FILE, []).find(x => x.id === req.params.id);
|
|
34
|
+
if (!run) return res.status(404).json({ error: { message: 'run not found' }, code: 'RUN_NOT_FOUND' });
|
|
35
|
+
const rec = (run.approvals || []).find(a => a.id === req.params.approvalId);
|
|
36
|
+
if (!rec || rec.status !== 'pending') {
|
|
37
|
+
return res.status(409).json({
|
|
38
|
+
error: { message: '审批请求不存在或已处理' + (rec ? '(当前状态:' + rec.status + ')' : '') },
|
|
39
|
+
code: 'APPROVAL_NOT_PENDING',
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
const action = req.body && req.body.action;
|
|
43
|
+
if (action !== 'approve' && action !== 'reject') {
|
|
44
|
+
return res.status(400).json({ error: { message: 'action 必须是 approve 或 reject' }, code: 'BAD_REQUEST' });
|
|
45
|
+
}
|
|
46
|
+
rec.status = action === 'approve' ? 'approved' : 'rejected';
|
|
47
|
+
rec.resolvedAt = new Date().toISOString();
|
|
48
|
+
run.status = 'paused';
|
|
49
|
+
run.pauseReason = action === 'approve' ? 'approval granted; ready to resume' : 'approval rejected; ready to resume';
|
|
50
|
+
if (run.checkpoint) run.checkpoint.approvalDecision = action;
|
|
51
|
+
await ctx.store.mutate(ctx.RUN_FILE, (runs) => runs.map(item => item.id === run.id ? run : item), []);
|
|
52
|
+
res.json({ ok: true, approvalId: req.params.approvalId, action, runId: req.params.id, resumable: true });
|
|
53
|
+
})().catch(next);
|
|
54
|
+
});
|
|
55
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Standard Agent Skills API. Skills are real directories with SKILL.md; this
|
|
4
|
+
// route never accepts executable MCP configuration and exposes no direct
|
|
5
|
+
// execution endpoint.
|
|
6
|
+
const extensions = require('../extensions/manager');
|
|
7
|
+
|
|
8
|
+
module.exports = function registerSkills(app) {
|
|
9
|
+
app.get('/api/skills', (req, res, next) => {
|
|
10
|
+
try { res.json(extensions.listSkills()); } catch (error) { next(error); }
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
app.get('/api/skills/:id', (req, res, next) => {
|
|
14
|
+
try {
|
|
15
|
+
res.json(extensions.findSkill(req.params.id));
|
|
16
|
+
} catch (error) { next(error); }
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
app.get('/api/skills/:id/diff', (req, res, next) => {
|
|
20
|
+
try {
|
|
21
|
+
const item = extensions.findSkill(req.params.id);
|
|
22
|
+
res.json(extensions.gitDiff(item.path));
|
|
23
|
+
} catch (error) { next(error); }
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
app.post('/api/skills', (req, res, next) => {
|
|
27
|
+
try { res.status(201).json(extensions.createSkill(req.body || {})); }
|
|
28
|
+
catch (error) { next(error); }
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
app.put('/api/skills/:id', (req, res, next) => {
|
|
32
|
+
try {
|
|
33
|
+
const body = req.body || {};
|
|
34
|
+
if (Object.keys(body).every(key => key === 'enabled')) {
|
|
35
|
+
return res.json(extensions.setSkillEnabled(req.params.id, body.enabled !== false));
|
|
36
|
+
}
|
|
37
|
+
const item = extensions.updateSkill(req.params.id, body);
|
|
38
|
+
if (body.enabled !== undefined) extensions.setSkillEnabled(req.params.id, body.enabled !== false);
|
|
39
|
+
res.json(item);
|
|
40
|
+
} catch (error) { next(error); }
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
app.post('/api/skills/:id/toggle', (req, res, next) => {
|
|
44
|
+
try { res.json(extensions.setSkillEnabled(req.params.id, req.body?.enabled !== false)); }
|
|
45
|
+
catch (error) { next(error); }
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
app.delete('/api/skills/:id', (req, res, next) => {
|
|
49
|
+
try { res.json(extensions.deleteSkill(req.params.id)); }
|
|
50
|
+
catch (error) { next(error); }
|
|
51
|
+
});
|
|
52
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// MultiChat's built-in functions are tools, not Agent Skills. Keeping this
|
|
4
|
+
// endpoint separate avoids conflating executable capabilities with SKILL.md.
|
|
5
|
+
const ctx = require('../lib/context');
|
|
6
|
+
const agent = require('../runtime/agent');
|
|
7
|
+
|
|
8
|
+
function listTools() {
|
|
9
|
+
return ctx.store.read(ctx.SKILL_FILE, [])
|
|
10
|
+
.filter(item => !['prompt', 'mcp'].includes(item.type))
|
|
11
|
+
.map(item => ({ ...agent.publicSkill(item), kind: 'builtin-tool' }));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
module.exports = function registerTools(app) {
|
|
15
|
+
app.get('/api/tools', (req, res) => res.json(listTools()));
|
|
16
|
+
app.put('/api/tools/:id', (req, res) => {
|
|
17
|
+
const rows = ctx.store.read(ctx.SKILL_FILE, []);
|
|
18
|
+
const index = rows.findIndex(item => item.id === req.params.id && !['prompt', 'mcp'].includes(item.type));
|
|
19
|
+
if (index < 0) return res.status(404).json({ error: 'tool not found', code: 'NOT_FOUND' });
|
|
20
|
+
rows[index].enabled = req.body?.enabled !== false;
|
|
21
|
+
ctx.store.write(ctx.SKILL_FILE, rows);
|
|
22
|
+
res.json(agent.publicSkill(rows[index]));
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// ── 本地试跑:只开放无网络、无副作用的纯计算类工具,避免端点被当作代理 ──
|
|
26
|
+
const TESTABLE_TYPES = new Set(['datetime', 'calculator']);
|
|
27
|
+
app.post('/api/tools/:id/test', async (req, res) => {
|
|
28
|
+
const tool = listTools().find(item => item.id === req.params.id);
|
|
29
|
+
if (!tool) return res.status(404).json({ error: 'tool not found', code: 'NOT_FOUND' });
|
|
30
|
+
if (!TESTABLE_TYPES.has(tool.type)) {
|
|
31
|
+
return res.status(400).json({ error: `「${tool.name}」暂不支持界面试跑(涉及网络访问,请通过对话调用)`, code: 'NOT_TESTABLE' });
|
|
32
|
+
}
|
|
33
|
+
const args = tool.type === 'calculator'
|
|
34
|
+
? { expression: String(req.body?.expression ?? '').trim() }
|
|
35
|
+
: {};
|
|
36
|
+
const started = Date.now();
|
|
37
|
+
const result = await agent.executeSkill(tool, args);
|
|
38
|
+
res.json({ ...result, elapsedMs: Date.now() - started });
|
|
39
|
+
});
|
|
40
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const ctx = require('../lib/context');
|
|
3
|
+
const usage = require('../lib/usage');
|
|
4
|
+
|
|
5
|
+
module.exports = function registerUsage(app) {
|
|
6
|
+
app.get('/api/usage', (req, res) => {
|
|
7
|
+
res.json(usage.usageSummary(ctx.store, {
|
|
8
|
+
range: req.query.range,
|
|
9
|
+
offsetMinutes: req.query.offset,
|
|
10
|
+
projectId: req.query.projectId,
|
|
11
|
+
}));
|
|
12
|
+
});
|
|
13
|
+
};
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// ── Workspaces / Projects / Assets / Prompts 路由 ───────────────────────
|
|
3
|
+
const ctx = require('../lib/context');
|
|
4
|
+
const { assetMeta, readResponseText } = require('../lib/util');
|
|
5
|
+
|
|
6
|
+
function sendWorkspaceError(res, error) {
|
|
7
|
+
const status = /not found/i.test(error.message) ? 404 : 400;
|
|
8
|
+
res.status(status).json({ error: error.message });
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
module.exports = function registerWorkspaces(app) {
|
|
12
|
+
app.get('/api/workspaces', (req, res) => {
|
|
13
|
+
const projects = ctx.workspaceStore.projects();
|
|
14
|
+
res.json(ctx.workspaceStore.workspaces().map(workspace => ({ ...workspace, projectCount: projects.filter(project => project.workspaceId === workspace.id).length })));
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
app.get('/api/workspaces/:id', (req, res) => {
|
|
18
|
+
const workspace = ctx.workspaceStore.getWorkspace(req.params.id);
|
|
19
|
+
if (!workspace) return res.status(404).json({ error: 'workspace not found' });
|
|
20
|
+
res.json({ ...workspace, projects: ctx.workspaceStore.projects().filter(project => project.workspaceId === workspace.id) });
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
app.post('/api/workspaces', (req, res) => {
|
|
24
|
+
try { res.status(201).json(ctx.workspaceStore.createWorkspace(req.body)); }
|
|
25
|
+
catch (error) { sendWorkspaceError(res, error); }
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
app.put('/api/workspaces/:id', (req, res) => {
|
|
29
|
+
try { res.json(ctx.workspaceStore.updateWorkspace(req.params.id, req.body)); }
|
|
30
|
+
catch (error) { sendWorkspaceError(res, error); }
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
app.delete('/api/workspaces/:id', (req, res) => {
|
|
34
|
+
try { res.json({ ok: true, workspace: ctx.workspaceStore.removeWorkspace(req.params.id) }); }
|
|
35
|
+
catch (error) { sendWorkspaceError(res, error); }
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
app.get('/api/projects', (req, res) => {
|
|
39
|
+
let projects = ctx.workspaceStore.projects();
|
|
40
|
+
if (req.query.workspaceId) projects = projects.filter(project => project.workspaceId === req.query.workspaceId);
|
|
41
|
+
res.json(projects);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
app.get('/api/projects/:id', (req, res) => {
|
|
45
|
+
const project = ctx.workspaceStore.getProject(req.params.id);
|
|
46
|
+
if (!project) return res.status(404).json({ error: 'project not found' });
|
|
47
|
+
res.json({ ...project, assets: ctx.workspaceStore.assets().filter(asset => asset.projectId === project.id).map(assetMeta) });
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
app.post('/api/projects', (req, res) => {
|
|
51
|
+
try {
|
|
52
|
+
const project = ctx.workspaceStore.createProject(req.body);
|
|
53
|
+
const workspace = ctx.workspaceStore.getWorkspace(project.workspaceId);
|
|
54
|
+
if (workspace && !workspace.defaultProjectId) ctx.workspaceStore.updateWorkspace(workspace.id, { defaultProjectId: project.id });
|
|
55
|
+
res.status(201).json(project);
|
|
56
|
+
} catch (error) { sendWorkspaceError(res, error); }
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
app.put('/api/projects/:id', (req, res) => {
|
|
60
|
+
try { res.json(ctx.workspaceStore.updateProject(req.params.id, req.body)); }
|
|
61
|
+
catch (error) { sendWorkspaceError(res, error); }
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
app.delete('/api/projects/:id', (req, res) => {
|
|
65
|
+
try { res.json({ ok: true, project: ctx.workspaceStore.removeProject(req.params.id) }); }
|
|
66
|
+
catch (error) { sendWorkspaceError(res, error); }
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
app.get('/api/assets', (req, res) => {
|
|
70
|
+
let assets = ctx.workspaceStore.assets();
|
|
71
|
+
if (req.query.projectId) assets = assets.filter(asset => asset.projectId === req.query.projectId);
|
|
72
|
+
if (req.query.workspaceId) assets = assets.filter(asset => asset.workspaceId === req.query.workspaceId);
|
|
73
|
+
res.json(assets.map(assetMeta));
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
app.get('/api/assets/:id', (req, res) => {
|
|
77
|
+
const asset = ctx.workspaceStore.getAsset(req.params.id);
|
|
78
|
+
if (!asset) return res.status(404).json({ error: 'asset not found' });
|
|
79
|
+
res.json(asset);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
app.post('/api/assets', async (req, res) => {
|
|
83
|
+
const body = req.body || {};
|
|
84
|
+
try {
|
|
85
|
+
let content = body.content;
|
|
86
|
+
let source = 'local';
|
|
87
|
+
let url = null;
|
|
88
|
+
let mimeType = body.mimeType || 'text/plain';
|
|
89
|
+
let name = body.name || '';
|
|
90
|
+
if (body.url) {
|
|
91
|
+
let value = String(body.url).trim();
|
|
92
|
+
let sameOrigin = false;
|
|
93
|
+
if (value.startsWith('/')) { value = 'http://127.0.0.1:' + ctx.PORT + value; sameOrigin = true; }
|
|
94
|
+
if (!/^https?:\/\//i.test(value)) return res.status(400).json({ error: 'only http(s) URL is allowed' });
|
|
95
|
+
const response = await ctx.safeFetch(value, { signal: AbortSignal.timeout(20000), headers: { accept: 'text/plain, text/markdown, application/json, text/html' } }, sameOrigin);
|
|
96
|
+
if (!response.ok) return res.status(502).json({ error: 'asset fetch failed with HTTP ' + response.status });
|
|
97
|
+
const contentType = response.headers.get('content-type') || '';
|
|
98
|
+
if (!/^(text\/|application\/(json|xml))/i.test(contentType)) {
|
|
99
|
+
return res.status(415).json({ error: '不支持的内容类型: ' + contentType });
|
|
100
|
+
}
|
|
101
|
+
content = await readResponseText(response, 2_000_000);
|
|
102
|
+
source = 'url';
|
|
103
|
+
url = value;
|
|
104
|
+
mimeType = response.headers.get('content-type') || mimeType;
|
|
105
|
+
if (!name) name = value.split('/').pop().split('?')[0] || '远程文件';
|
|
106
|
+
}
|
|
107
|
+
const asset = ctx.workspaceStore.createAsset({ ...body, content, source, url, mimeType, name });
|
|
108
|
+
res.status(201).json(assetMeta(asset));
|
|
109
|
+
} catch (error) { sendWorkspaceError(res, error); }
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
app.delete('/api/assets/:id', (req, res) => {
|
|
113
|
+
try { res.json({ ok: true, asset: assetMeta(ctx.workspaceStore.removeAsset(req.params.id)) }); }
|
|
114
|
+
catch (error) { sendWorkspaceError(res, error); }
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
// ── Prompt Templates ──
|
|
118
|
+
app.get('/api/prompts', (req, res) => {
|
|
119
|
+
const file = ctx.path.join(ctx.BACKEND_ROOT, 'prompts.json');
|
|
120
|
+
if (!ctx.fs.existsSync(file)) return res.json([]);
|
|
121
|
+
try { res.json(JSON.parse(ctx.fs.readFileSync(file, 'utf-8'))); }
|
|
122
|
+
catch { res.json([]); }
|
|
123
|
+
});
|
|
124
|
+
};
|