opc-agent 4.0.0 → 4.0.2
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 +404 -80
- package/README.zh-CN.md +82 -0
- package/dist/cli/chat.d.ts +2 -0
- package/dist/cli/chat.js +134 -0
- package/dist/cli/setup.d.ts +4 -0
- package/dist/cli/setup.js +303 -0
- package/dist/cli.js +106 -6
- package/dist/hub/brain-seed.d.ts +14 -0
- package/dist/hub/brain-seed.js +77 -0
- package/dist/hub/client.d.ts +25 -0
- package/dist/hub/client.js +44 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.js +12 -3
- package/dist/providers/index.d.ts +1 -1
- package/dist/providers/index.js +74 -1
- package/dist/scheduler/cron-engine.d.ts +41 -0
- package/dist/scheduler/cron-engine.js +200 -0
- package/dist/scheduler/index.d.ts +3 -0
- package/dist/scheduler/index.js +7 -0
- package/dist/skills/builtin/index.d.ts +6 -0
- package/dist/skills/builtin/index.js +402 -0
- package/dist/skills/marketplace.d.ts +30 -0
- package/dist/skills/marketplace.js +142 -0
- package/dist/skills/types.d.ts +34 -0
- package/dist/skills/types.js +16 -0
- package/dist/studio/server.d.ts +25 -0
- package/dist/studio/server.js +780 -0
- package/dist/studio/templates-data.d.ts +21 -0
- package/dist/studio/templates-data.js +148 -0
- package/dist/studio-ui/index.html +2502 -1073
- package/dist/tools/builtin/index.d.ts +1 -0
- package/dist/tools/builtin/index.js +7 -2
- package/dist/tools/builtin/web-search.d.ts +9 -0
- package/dist/tools/builtin/web-search.js +150 -0
- package/dist/tools/document-processor.d.ts +39 -0
- package/dist/tools/document-processor.js +188 -0
- package/dist/tools/image-generator.d.ts +42 -0
- package/dist/tools/image-generator.js +136 -0
- package/dist/tools/web-scraper.d.ts +20 -0
- package/dist/tools/web-scraper.js +148 -0
- package/dist/tools/web-search.d.ts +51 -0
- package/dist/tools/web-search.js +152 -0
- package/install.ps1 +154 -0
- package/install.sh +164 -0
- package/package.json +63 -52
- package/src/cli/chat.ts +99 -0
- package/src/cli/setup.ts +314 -0
- package/src/cli.ts +108 -6
- package/src/hub/brain-seed.ts +54 -0
- package/src/hub/client.ts +60 -0
- package/src/index.ts +4 -2
- package/src/providers/index.ts +80 -1
- package/src/scheduler/cron-engine.ts +191 -0
- package/src/scheduler/index.ts +2 -0
- package/src/skills/builtin/index.ts +408 -0
- package/src/skills/marketplace.ts +113 -0
- package/src/skills/types.ts +42 -0
- package/src/studio/server.ts +1591 -791
- package/src/studio/templates-data.ts +178 -0
- package/src/studio-ui/index.html +2502 -1073
- package/src/tools/builtin/index.ts +37 -35
- package/src/tools/builtin/web-search.ts +126 -0
- package/src/tools/document-processor.ts +213 -0
- package/src/tools/image-generator.ts +150 -0
- package/src/tools/web-scraper.ts +179 -0
- package/src/tools/web-search.ts +180 -0
- package/tests/cron-engine.test.ts +101 -0
- package/tests/document-processor.test.ts +69 -0
- package/tests/e2e-nocode.test.ts +442 -0
- package/tests/image-generator.test.ts +84 -0
- package/tests/settings-api.test.ts +148 -0
- package/tests/setup.test.ts +73 -0
- package/tests/studio.test.ts +402 -229
- package/tests/voice-interaction.test.ts +38 -0
- package/tests/web-search.test.ts +155 -0
package/package.json
CHANGED
|
@@ -1,52 +1,63 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "opc-agent",
|
|
3
|
-
"version": "4.0.
|
|
4
|
-
"description": "Open Agent Framework — Build, test, and run AI Agents for business workstations",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"types": "dist/index.d.ts",
|
|
7
|
-
"bin": {
|
|
8
|
-
"opc": "dist/cli.js"
|
|
9
|
-
},
|
|
10
|
-
"scripts": {
|
|
11
|
-
"build": "tsc",
|
|
12
|
-
"postbuild": "xcopy /E /I /Y src\\studio-ui dist\\studio-ui",
|
|
13
|
-
"test": "vitest run",
|
|
14
|
-
"dev": "tsc --watch",
|
|
15
|
-
"lint": "tsc --noEmit",
|
|
16
|
-
"
|
|
17
|
-
"docs:
|
|
18
|
-
"docs:
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
"
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
"@types/
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
|
|
52
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "opc-agent",
|
|
3
|
+
"version": "4.0.2",
|
|
4
|
+
"description": "Open Agent Framework — Build, test, and run AI Agents for business workstations",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"bin": {
|
|
8
|
+
"opc": "dist/cli.js"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"postbuild": "xcopy /E /I /Y src\\studio-ui dist\\studio-ui",
|
|
13
|
+
"test": "vitest run",
|
|
14
|
+
"dev": "tsc --watch",
|
|
15
|
+
"lint": "tsc --noEmit",
|
|
16
|
+
"setup": "opc setup",
|
|
17
|
+
"docs:dev": "vitepress dev docs",
|
|
18
|
+
"docs:build": "vitepress build docs",
|
|
19
|
+
"docs:preview": "vitepress preview docs"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"agent",
|
|
23
|
+
"ai",
|
|
24
|
+
"llm",
|
|
25
|
+
"framework",
|
|
26
|
+
"typescript",
|
|
27
|
+
"agent-framework"
|
|
28
|
+
],
|
|
29
|
+
"author": "Deepleaper",
|
|
30
|
+
"license": "Apache-2.0",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "https://github.com/Deepleaper/opc-agent.git"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"agent-workstation": "^2.0.1",
|
|
37
|
+
"agentkits": "^2.0.0",
|
|
38
|
+
"commander": "^12.0.0",
|
|
39
|
+
"express": "^4.21.0",
|
|
40
|
+
"js-yaml": "^4.1.0",
|
|
41
|
+
"mammoth": "^1.12.0",
|
|
42
|
+
"pdf-parse": "^1.1.4",
|
|
43
|
+
"ws": "^8.20.0",
|
|
44
|
+
"zod": "^3.23.0"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@types/express": "^4.17.21",
|
|
48
|
+
"@types/js-yaml": "^4.0.9",
|
|
49
|
+
"@types/node": "^20.11.0",
|
|
50
|
+
"@types/ws": "^8.18.1",
|
|
51
|
+
"typescript": "^5.5.0",
|
|
52
|
+
"vitepress": "^1.5.0",
|
|
53
|
+
"vitest": "^2.0.0"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"deepbrain": "^2.0.0"
|
|
57
|
+
},
|
|
58
|
+
"peerDependenciesMeta": {
|
|
59
|
+
"deepbrain": {
|
|
60
|
+
"optional": true
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
package/src/cli/chat.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import * as readline from 'readline';
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
import * as path from 'path';
|
|
4
|
+
import * as os from 'os';
|
|
5
|
+
import { createProvider } from '../providers';
|
|
6
|
+
|
|
7
|
+
const OPC_HOME = path.join(os.homedir(), '.opc');
|
|
8
|
+
const CONFIG_PATH = path.join(OPC_HOME, 'config.json');
|
|
9
|
+
|
|
10
|
+
const c = {
|
|
11
|
+
green: (s: string) => `\x1b[32m${s}\x1b[0m`,
|
|
12
|
+
red: (s: string) => `\x1b[31m${s}\x1b[0m`,
|
|
13
|
+
cyan: (s: string) => `\x1b[36m${s}\x1b[0m`,
|
|
14
|
+
bold: (s: string) => `\x1b[1m${s}\x1b[0m`,
|
|
15
|
+
dim: (s: string) => `\x1b[2m${s}\x1b[0m`,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
function loadConfig(): Record<string, any> {
|
|
19
|
+
if (!fs.existsSync(CONFIG_PATH)) return {};
|
|
20
|
+
try { return JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf-8')); } catch { return {}; }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export async function runChat(): Promise<void> {
|
|
24
|
+
const config = loadConfig();
|
|
25
|
+
|
|
26
|
+
if (!config.provider || !config.model) {
|
|
27
|
+
console.log(`${c.red('✘')} 未找到配置。请先运行 ${c.cyan('opc setup')} 完成初始设置。`);
|
|
28
|
+
process.exit(1);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const agentId = config.defaultAgent;
|
|
32
|
+
let agentConfig: Record<string, any> = {};
|
|
33
|
+
if (agentId) {
|
|
34
|
+
const agentPath = path.join(OPC_HOME, 'agents', agentId, 'config.json');
|
|
35
|
+
if (fs.existsSync(agentPath)) {
|
|
36
|
+
try { agentConfig = JSON.parse(fs.readFileSync(agentPath, 'utf-8')); } catch { /* ignore */ }
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const agentName = agentConfig.name || 'AI 助手';
|
|
41
|
+
const systemPrompt = agentConfig.description
|
|
42
|
+
? `你是「${agentName}」,${agentConfig.description}。请用简洁友好的方式回答。`
|
|
43
|
+
: `你是「${agentName}」,一个智能 AI 助手。请用简洁友好的方式回答。`;
|
|
44
|
+
|
|
45
|
+
console.log('');
|
|
46
|
+
console.log(c.bold(`💬 与「${agentName}」对话`));
|
|
47
|
+
console.log(c.dim(' 输入 /quit 退出'));
|
|
48
|
+
console.log('');
|
|
49
|
+
|
|
50
|
+
let provider: any;
|
|
51
|
+
try {
|
|
52
|
+
provider = createProvider({
|
|
53
|
+
provider: config.provider,
|
|
54
|
+
model: config.model,
|
|
55
|
+
apiKey: config.apiKey,
|
|
56
|
+
baseUrl: config.baseUrl,
|
|
57
|
+
} as any);
|
|
58
|
+
} catch (err: any) {
|
|
59
|
+
console.log(`${c.red('✘')} 无法初始化模型: ${err.message}`);
|
|
60
|
+
console.log(c.dim(` Provider: ${config.provider}, Model: ${config.model}`));
|
|
61
|
+
process.exit(1);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const messages: Array<{ role: string; content: string }> = [
|
|
65
|
+
{ role: 'system', content: systemPrompt },
|
|
66
|
+
];
|
|
67
|
+
|
|
68
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
69
|
+
|
|
70
|
+
const askQuestion = (): void => {
|
|
71
|
+
rl.question(c.cyan('你: '), async (input: string) => {
|
|
72
|
+
const trimmed = input.trim();
|
|
73
|
+
if (!trimmed) { askQuestion(); return; }
|
|
74
|
+
if (trimmed === '/quit' || trimmed === '/exit' || trimmed === '/q') {
|
|
75
|
+
console.log(c.dim('\n👋 再见!'));
|
|
76
|
+
rl.close();
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
messages.push({ role: 'user', content: trimmed });
|
|
81
|
+
|
|
82
|
+
try {
|
|
83
|
+
process.stdout.write(c.green(`${agentName}: `));
|
|
84
|
+
const response = await provider.chat(messages);
|
|
85
|
+
const reply = typeof response === 'string' ? response : response?.content || response?.message?.content || JSON.stringify(response);
|
|
86
|
+
console.log(reply);
|
|
87
|
+
console.log('');
|
|
88
|
+
messages.push({ role: 'assistant', content: reply });
|
|
89
|
+
} catch (err: any) {
|
|
90
|
+
console.log(c.red(`\n 错误: ${err.message}`));
|
|
91
|
+
console.log('');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
askQuestion();
|
|
95
|
+
});
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
askQuestion();
|
|
99
|
+
}
|
package/src/cli/setup.ts
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import * as readline from 'readline';
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
import * as path from 'path';
|
|
4
|
+
import * as http from 'http';
|
|
5
|
+
import * as https from 'https';
|
|
6
|
+
import * as os from 'os';
|
|
7
|
+
|
|
8
|
+
// ── Colors ──────────────────────────────────────────────────────────────────
|
|
9
|
+
const c = {
|
|
10
|
+
green: (s: string) => `\x1b[32m${s}\x1b[0m`,
|
|
11
|
+
red: (s: string) => `\x1b[31m${s}\x1b[0m`,
|
|
12
|
+
yellow: (s: string) => `\x1b[33m${s}\x1b[0m`,
|
|
13
|
+
blue: (s: string) => `\x1b[34m${s}\x1b[0m`,
|
|
14
|
+
cyan: (s: string) => `\x1b[36m${s}\x1b[0m`,
|
|
15
|
+
bold: (s: string) => `\x1b[1m${s}\x1b[0m`,
|
|
16
|
+
dim: (s: string) => `\x1b[2m${s}\x1b[0m`,
|
|
17
|
+
magenta: (s: string) => `\x1b[35m${s}\x1b[0m`,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
// ── Types ───────────────────────────────────────────────────────────────────
|
|
21
|
+
interface SetupConfig {
|
|
22
|
+
provider: 'ollama' | 'openai' | 'deepseek' | 'qwen' | 'anthropic';
|
|
23
|
+
model?: string;
|
|
24
|
+
embeddingModel?: string;
|
|
25
|
+
apiKey?: string;
|
|
26
|
+
baseUrl?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface AgentTemplate {
|
|
30
|
+
id: string;
|
|
31
|
+
icon: string;
|
|
32
|
+
name: string;
|
|
33
|
+
description: string;
|
|
34
|
+
defaultAgentName: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const TEMPLATES: AgentTemplate[] = [
|
|
38
|
+
{ id: 'customer-service', icon: '🎧', name: '客服助手', description: '回答客户问题,自动学习产品知识', defaultAgentName: '我的客服助手' },
|
|
39
|
+
{ id: 'content-writer', icon: '✍️', name: '写作助手', description: '帮你写文章、邮件、报告', defaultAgentName: '我的写作助手' },
|
|
40
|
+
{ id: 'data-analyst', icon: '📊', name: '数据分析师', description: '分析数据,生成洞察', defaultAgentName: '我的数据分析师' },
|
|
41
|
+
{ id: 'translator', icon: '🌐', name: '翻译助手', description: '多语言翻译,越用越准', defaultAgentName: '我的翻译助手' },
|
|
42
|
+
{ id: 'general', icon: '🤖', name: '通用助手', description: '什么都能聊', defaultAgentName: '我的AI助手' },
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
const OPC_HOME = path.join(os.homedir(), '.opc');
|
|
46
|
+
const CONFIG_PATH = path.join(OPC_HOME, 'config.json');
|
|
47
|
+
|
|
48
|
+
// ── Readline helpers ────────────────────────────────────────────────────────
|
|
49
|
+
export function createRL(input?: NodeJS.ReadableStream, output?: NodeJS.WritableStream): readline.Interface {
|
|
50
|
+
return readline.createInterface({
|
|
51
|
+
input: input ?? process.stdin,
|
|
52
|
+
output: output ?? process.stdout,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function ask(rl: readline.Interface, question: string): Promise<string> {
|
|
57
|
+
return new Promise((resolve) => rl.question(question, (a) => resolve(a.trim())));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// ── HTTP helpers ────────────────────────────────────────────────────────────
|
|
61
|
+
function httpGet(url: string, timeout = 5000): Promise<{ status: number; body: string }> {
|
|
62
|
+
return new Promise((resolve, reject) => {
|
|
63
|
+
const mod = url.startsWith('https') ? https : http;
|
|
64
|
+
const req = mod.get(url, { timeout }, (res) => {
|
|
65
|
+
let body = '';
|
|
66
|
+
res.on('data', (chunk: Buffer) => { body += chunk; });
|
|
67
|
+
res.on('end', () => resolve({ status: res.statusCode ?? 0, body }));
|
|
68
|
+
});
|
|
69
|
+
req.on('error', reject);
|
|
70
|
+
req.on('timeout', () => { req.destroy(); reject(new Error('timeout')); });
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// ── Step 1: Welcome ─────────────────────────────────────────────────────────
|
|
75
|
+
function printWelcome(): void {
|
|
76
|
+
console.log('');
|
|
77
|
+
console.log(c.bold(' 🎉 欢迎使用 OPC Agent!'));
|
|
78
|
+
console.log(c.dim(' 让我们用 3 分钟配置你的第一个 AI Agent。'));
|
|
79
|
+
console.log('');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// ── Step 2: Model detection ─────────────────────────────────────────────────
|
|
83
|
+
async function detectOllama(): Promise<{ running: boolean; models: string[] }> {
|
|
84
|
+
try {
|
|
85
|
+
const { body } = await httpGet('http://localhost:11434/api/tags');
|
|
86
|
+
const data = JSON.parse(body);
|
|
87
|
+
const models = (data.models || []).map((m: any) => m.name || m.model);
|
|
88
|
+
return { running: true, models };
|
|
89
|
+
} catch {
|
|
90
|
+
return { running: false, models: [] };
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function stepModel(rl: readline.Interface): Promise<SetupConfig> {
|
|
95
|
+
console.log(c.bold('📡 Step 1/4: 配置 AI 模型'));
|
|
96
|
+
console.log(c.dim(' 正在检测本地 Ollama...'));
|
|
97
|
+
console.log('');
|
|
98
|
+
|
|
99
|
+
const ollama = await detectOllama();
|
|
100
|
+
|
|
101
|
+
if (ollama.running) {
|
|
102
|
+
console.log(c.green(' ✔ Ollama 运行中!已安装模型:'));
|
|
103
|
+
if (ollama.models.length === 0) {
|
|
104
|
+
console.log(c.dim(' (无模型)'));
|
|
105
|
+
} else {
|
|
106
|
+
for (const m of ollama.models) {
|
|
107
|
+
console.log(` • ${m}`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
console.log('');
|
|
111
|
+
|
|
112
|
+
const hasChat = ollama.models.some((m) => m.startsWith('qwen2.5'));
|
|
113
|
+
const hasEmbed = ollama.models.some((m) => m.startsWith('nomic-embed-text'));
|
|
114
|
+
|
|
115
|
+
if (!hasChat) {
|
|
116
|
+
const dl = await ask(rl, ` 推荐模型 ${c.cyan('qwen2.5:7b')} 未安装,是否下载? [Y/n] `);
|
|
117
|
+
if (dl.toLowerCase() !== 'n') {
|
|
118
|
+
console.log(c.dim(' → 请在终端运行: ollama pull qwen2.5:7b'));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (!hasEmbed) {
|
|
123
|
+
const dl = await ask(rl, ` 推荐 Embedding 模型 ${c.cyan('nomic-embed-text')} 未安装,是否下载? [Y/n] `);
|
|
124
|
+
if (dl.toLowerCase() !== 'n') {
|
|
125
|
+
console.log(c.dim(' → 请在终端运行: ollama pull nomic-embed-text'));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
console.log('');
|
|
130
|
+
return {
|
|
131
|
+
provider: 'ollama',
|
|
132
|
+
model: hasChat ? ollama.models.find((m) => m.startsWith('qwen2.5'))! : 'qwen2.5:7b',
|
|
133
|
+
embeddingModel: hasEmbed ? 'nomic-embed-text' : 'nomic-embed-text',
|
|
134
|
+
baseUrl: 'http://localhost:11434',
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Ollama not running
|
|
139
|
+
console.log(c.yellow(' ⚠ 未检测到 Ollama'));
|
|
140
|
+
console.log('');
|
|
141
|
+
console.log(' 请选择:');
|
|
142
|
+
console.log(` ${c.cyan('A')} ) 安装 Ollama(推荐,免费本地运行)`);
|
|
143
|
+
console.log(` ${c.cyan('B')} ) 使用云端 API`);
|
|
144
|
+
console.log('');
|
|
145
|
+
|
|
146
|
+
const choice = await ask(rl, ' 你的选择 [A/B]: ');
|
|
147
|
+
|
|
148
|
+
if (choice.toUpperCase() !== 'B') {
|
|
149
|
+
console.log('');
|
|
150
|
+
console.log(c.bold(' 📦 安装 Ollama:'));
|
|
151
|
+
console.log('');
|
|
152
|
+
if (process.platform === 'win32') {
|
|
153
|
+
console.log(' 1. 访问 https://ollama.com/download');
|
|
154
|
+
console.log(' 2. 下载 Windows 版并安装');
|
|
155
|
+
console.log(' 3. 安装后运行: ollama pull qwen2.5:7b');
|
|
156
|
+
} else if (process.platform === 'darwin') {
|
|
157
|
+
console.log(' brew install ollama');
|
|
158
|
+
console.log(' ollama serve &');
|
|
159
|
+
console.log(' ollama pull qwen2.5:7b');
|
|
160
|
+
} else {
|
|
161
|
+
console.log(' curl -fsSL https://ollama.com/install.sh | sh');
|
|
162
|
+
console.log(' ollama serve &');
|
|
163
|
+
console.log(' ollama pull qwen2.5:7b');
|
|
164
|
+
}
|
|
165
|
+
console.log('');
|
|
166
|
+
console.log(c.dim(' 安装完成后重新运行 opc setup'));
|
|
167
|
+
return { provider: 'ollama', model: 'qwen2.5:7b', embeddingModel: 'nomic-embed-text', baseUrl: 'http://localhost:11434' };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Cloud API
|
|
171
|
+
console.log('');
|
|
172
|
+
console.log(' 选择云端 Provider:');
|
|
173
|
+
console.log(` ${c.cyan('1')} ) OpenAI`);
|
|
174
|
+
console.log(` ${c.cyan('2')} ) DeepSeek`);
|
|
175
|
+
console.log(` ${c.cyan('3')} ) 通义千问 (Qwen)`);
|
|
176
|
+
console.log(` ${c.cyan('4')} ) Anthropic`);
|
|
177
|
+
console.log('');
|
|
178
|
+
|
|
179
|
+
const providerChoice = await ask(rl, ' 选择 [1-4]: ');
|
|
180
|
+
const providers: Record<string, { provider: SetupConfig['provider']; baseUrl: string; model: string; testUrl: string }> = {
|
|
181
|
+
'1': { provider: 'openai', baseUrl: 'https://api.openai.com/v1', model: 'gpt-4o-mini', testUrl: 'https://api.openai.com/v1/models' },
|
|
182
|
+
'2': { provider: 'deepseek', baseUrl: 'https://api.deepseek.com/v1', model: 'deepseek-chat', testUrl: 'https://api.deepseek.com/v1/models' },
|
|
183
|
+
'3': { provider: 'qwen', baseUrl: 'https://dashscope.aliyuncs.com/compatible-mode/v1', model: 'qwen-plus', testUrl: 'https://dashscope.aliyuncs.com/compatible-mode/v1/models' },
|
|
184
|
+
'4': { provider: 'anthropic', baseUrl: 'https://api.anthropic.com/v1', model: 'claude-3-5-sonnet-20241022', testUrl: 'https://api.anthropic.com/v1/models' },
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
const p = providers[providerChoice] ?? providers['1'];
|
|
188
|
+
console.log('');
|
|
189
|
+
const apiKey = await ask(rl, ` 请输入 ${c.bold(p.provider)} API Key: `);
|
|
190
|
+
|
|
191
|
+
if (apiKey) {
|
|
192
|
+
console.log(c.dim(' 正在测试连接...'));
|
|
193
|
+
try {
|
|
194
|
+
await httpGet(p.testUrl);
|
|
195
|
+
console.log(c.green(' ✔ 连接成功!'));
|
|
196
|
+
} catch {
|
|
197
|
+
console.log(c.yellow(' ⚠ 无法验证连接,但配置已保存,稍后可测试'));
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
console.log('');
|
|
202
|
+
return { provider: p.provider, model: p.model, apiKey, baseUrl: p.baseUrl };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// ── Step 3: Choose template ─────────────────────────────────────────────────
|
|
206
|
+
async function stepTemplate(rl: readline.Interface): Promise<AgentTemplate> {
|
|
207
|
+
console.log(c.bold('📋 Step 2/4: 选择 Agent 模板'));
|
|
208
|
+
console.log('');
|
|
209
|
+
|
|
210
|
+
for (let i = 0; i < TEMPLATES.length; i++) {
|
|
211
|
+
const t = TEMPLATES[i];
|
|
212
|
+
console.log(` ${c.cyan(String(i + 1))} ) ${t.icon} ${c.bold(t.name)} — ${c.dim(t.description)}`);
|
|
213
|
+
}
|
|
214
|
+
console.log(` ${c.cyan('6')} ) 📋 更多模板...(打开 Studio 浏览)`);
|
|
215
|
+
console.log('');
|
|
216
|
+
|
|
217
|
+
const choice = await ask(rl, ' 选择模板 [1-6]: ');
|
|
218
|
+
const idx = parseInt(choice, 10) - 1;
|
|
219
|
+
|
|
220
|
+
if (choice === '6') {
|
|
221
|
+
console.log(c.dim(' → 运行 opc studio 浏览更多模板'));
|
|
222
|
+
return TEMPLATES[4]; // default to general
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const template = TEMPLATES[idx] ?? TEMPLATES[4];
|
|
226
|
+
console.log(c.green(` ✔ 已选择: ${template.icon} ${template.name}`));
|
|
227
|
+
console.log('');
|
|
228
|
+
return template;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// ── Step 4: Create agent ────────────────────────────────────────────────────
|
|
232
|
+
async function stepCreateAgent(rl: readline.Interface, template: AgentTemplate, config: SetupConfig): Promise<{ name: string; id: string; dir: string }> {
|
|
233
|
+
console.log(c.bold('🤖 Step 3/4: 创建 Agent'));
|
|
234
|
+
console.log('');
|
|
235
|
+
|
|
236
|
+
const name = (await ask(rl, ` Agent 名称 [${template.defaultAgentName}]: `)) || template.defaultAgentName;
|
|
237
|
+
const description = await ask(rl, ` 简短描述(可选): `);
|
|
238
|
+
|
|
239
|
+
// Generate a slug id
|
|
240
|
+
const id = name
|
|
241
|
+
.replace(/[^\w\u4e00-\u9fff]/g, '-')
|
|
242
|
+
.replace(/-+/g, '-')
|
|
243
|
+
.replace(/^-|-$/g, '')
|
|
244
|
+
.toLowerCase() || `agent-${Date.now()}`;
|
|
245
|
+
|
|
246
|
+
const agentDir = path.join(OPC_HOME, 'agents', id);
|
|
247
|
+
fs.mkdirSync(agentDir, { recursive: true });
|
|
248
|
+
|
|
249
|
+
// Write agent config
|
|
250
|
+
const agentConfig = {
|
|
251
|
+
id,
|
|
252
|
+
name,
|
|
253
|
+
description: description || template.description,
|
|
254
|
+
template: template.id,
|
|
255
|
+
provider: config.provider,
|
|
256
|
+
model: config.model,
|
|
257
|
+
embeddingModel: config.embeddingModel,
|
|
258
|
+
apiKey: config.apiKey,
|
|
259
|
+
baseUrl: config.baseUrl,
|
|
260
|
+
createdAt: new Date().toISOString(),
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
fs.writeFileSync(path.join(agentDir, 'config.json'), JSON.stringify(agentConfig, null, 2));
|
|
264
|
+
|
|
265
|
+
// Initialize brain directory
|
|
266
|
+
const brainDir = path.join(agentDir, 'brain');
|
|
267
|
+
fs.mkdirSync(brainDir, { recursive: true });
|
|
268
|
+
fs.writeFileSync(path.join(brainDir, 'README.md'), `# ${name}\n\n将知识文档放在这里,Agent 会自动学习。\n`);
|
|
269
|
+
|
|
270
|
+
// Save global config
|
|
271
|
+
fs.mkdirSync(OPC_HOME, { recursive: true });
|
|
272
|
+
const globalConfig = fs.existsSync(CONFIG_PATH) ? JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf-8')) : {};
|
|
273
|
+
globalConfig.defaultAgent = id;
|
|
274
|
+
globalConfig.provider = config.provider;
|
|
275
|
+
globalConfig.model = config.model;
|
|
276
|
+
globalConfig.baseUrl = config.baseUrl;
|
|
277
|
+
if (config.apiKey) globalConfig.apiKey = config.apiKey;
|
|
278
|
+
if (config.embeddingModel) globalConfig.embeddingModel = config.embeddingModel;
|
|
279
|
+
fs.writeFileSync(CONFIG_PATH, JSON.stringify(globalConfig, null, 2));
|
|
280
|
+
|
|
281
|
+
console.log('');
|
|
282
|
+
console.log(` ${c.green('✔')} Agent 已创建: ${c.bold(name)}`);
|
|
283
|
+
console.log(` ${c.dim(' 目录: ' + agentDir)}`);
|
|
284
|
+
console.log('');
|
|
285
|
+
|
|
286
|
+
return { name, id, dir: agentDir };
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// ── Step 5: Completion ──────────────────────────────────────────────────────
|
|
290
|
+
function printCompletion(agentName: string): void {
|
|
291
|
+
console.log(c.bold(` ✅ 你的 AI Agent「${agentName}」已创建!`));
|
|
292
|
+
console.log('');
|
|
293
|
+
console.log(' 启动方式:');
|
|
294
|
+
console.log(` ${c.cyan('opc studio')} — 打开网页管理界面`);
|
|
295
|
+
console.log(` ${c.cyan('opc chat')} — 终端直接对话`);
|
|
296
|
+
console.log(` ${c.cyan('opc start')} — 后台运行`);
|
|
297
|
+
console.log('');
|
|
298
|
+
console.log(c.dim(' 你的 Agent 会自动学习和进化,越用越聪明!🧬'));
|
|
299
|
+
console.log('');
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// ── Main entry ──────────────────────────────────────────────────────────────
|
|
303
|
+
export async function runSetup(input?: NodeJS.ReadableStream, output?: NodeJS.WritableStream): Promise<void> {
|
|
304
|
+
const rl = createRL(input, output);
|
|
305
|
+
try {
|
|
306
|
+
printWelcome();
|
|
307
|
+
const config = await stepModel(rl);
|
|
308
|
+
const template = await stepTemplate(rl);
|
|
309
|
+
const agent = await stepCreateAgent(rl, template, config);
|
|
310
|
+
printCompletion(agent.name);
|
|
311
|
+
} finally {
|
|
312
|
+
rl.close();
|
|
313
|
+
}
|
|
314
|
+
}
|
package/src/cli.ts
CHANGED
|
@@ -36,6 +36,9 @@ import type { CronJob } from './core/scheduler';
|
|
|
36
36
|
import type { Span } from './traces';
|
|
37
37
|
import { spawn } from 'child_process';
|
|
38
38
|
import { searchRoles, getPopularRoles, getRole, getCategories } from 'agent-workstation';
|
|
39
|
+
import { fetchTemplates as hubFetchTemplates, fetchTemplate as hubFetchTemplate, fetchBrainSeeds, isHubAvailable } from './hub/client';
|
|
40
|
+
import type { HubTemplate } from './hub/client';
|
|
41
|
+
import { downloadAndLearnBrainSeeds } from './hub/brain-seed';
|
|
39
42
|
|
|
40
43
|
const program = new Command();
|
|
41
44
|
|
|
@@ -358,9 +361,34 @@ export class EchoSkill extends BaseSkill {
|
|
|
358
361
|
}
|
|
359
362
|
|
|
360
363
|
const name = opts.yes ? (nameArg ?? 'my-agent') : (nameArg ?? await promptUser('Project name', 'my-agent'));
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
+
|
|
365
|
+
// Try fetching templates from Hub API, fall back to bundled
|
|
366
|
+
let hubTemplates: HubTemplate[] = [];
|
|
367
|
+
let useHub = false;
|
|
368
|
+
try {
|
|
369
|
+
const hubAvailable = await isHubAvailable();
|
|
370
|
+
if (hubAvailable) {
|
|
371
|
+
hubTemplates = await hubFetchTemplates();
|
|
372
|
+
if (hubTemplates.length > 0) useHub = true;
|
|
373
|
+
}
|
|
374
|
+
} catch {
|
|
375
|
+
// Hub unreachable — fall back to bundled templates
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
let template: string;
|
|
379
|
+
let selectedHubTemplate: HubTemplate | undefined;
|
|
380
|
+
if (opts.yes) {
|
|
381
|
+
template = opts.template ?? 'customer-service';
|
|
382
|
+
} else if (opts.template) {
|
|
383
|
+
template = opts.template;
|
|
384
|
+
if (useHub) selectedHubTemplate = hubTemplates.find(t => t.id === template);
|
|
385
|
+
} else if (useHub) {
|
|
386
|
+
console.log(` ${icon.info} ${color.dim('Using templates from Workstation Hub')}`);
|
|
387
|
+
template = await select('Select a template:', hubTemplates.map(t => ({ value: t.id, label: `${t.name} - ${t.description}` })));
|
|
388
|
+
selectedHubTemplate = hubTemplates.find(t => t.id === template);
|
|
389
|
+
} else {
|
|
390
|
+
template = await select('Select a template:', Object.entries(TEMPLATES).map(([value, { label }]) => ({ value, label })));
|
|
391
|
+
}
|
|
364
392
|
|
|
365
393
|
const dir = path.resolve(name);
|
|
366
394
|
if (fs.existsSync(dir)) {
|
|
@@ -703,6 +731,22 @@ on startup to understand the project context.
|
|
|
703
731
|
console.log(` ${icon.file} Dockerfile`);
|
|
704
732
|
console.log(` ${icon.file} README.md`);
|
|
705
733
|
console.log(`\n Template: ${color.cyan(template)}`);
|
|
734
|
+
|
|
735
|
+
// Download brain-seed files from Hub if available
|
|
736
|
+
if (selectedHubTemplate) {
|
|
737
|
+
try {
|
|
738
|
+
const seeds = await fetchBrainSeeds(selectedHubTemplate.id);
|
|
739
|
+
if (seeds.length > 0) {
|
|
740
|
+
const result = await downloadAndLearnBrainSeeds(dir, seeds);
|
|
741
|
+
console.log(`\n 📚 Imported ${color.bold(String(result.savedFiles.length))} knowledge files into brain-seed/`);
|
|
742
|
+
if (result.learnedCount > 0) {
|
|
743
|
+
console.log(` 🧠 Auto-learned ${color.bold(String(result.learnedCount))} files into local DeepBrain`);
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
} catch {
|
|
747
|
+
// Brain-seed download failed — non-fatal, project still usable
|
|
748
|
+
}
|
|
749
|
+
}
|
|
706
750
|
console.log(`\n${color.bold('Next steps:')}`);
|
|
707
751
|
console.log(` 1. cd ${name}`);
|
|
708
752
|
console.log(` 2. npm install`);
|
|
@@ -2110,13 +2154,71 @@ program
|
|
|
2110
2154
|
.command('studio')
|
|
2111
2155
|
.description('Start OPC Studio web UI')
|
|
2112
2156
|
.option('--port <port>', 'Port to listen on', '4000')
|
|
2157
|
+
.option('--no-open', 'Do not open browser automatically')
|
|
2113
2158
|
.action(async (opts: any) => {
|
|
2114
2159
|
const { StudioServer } = require('./studio/server');
|
|
2115
|
-
const
|
|
2116
|
-
|
|
2117
|
-
|
|
2160
|
+
const net = require('net');
|
|
2161
|
+
const port = parseInt(opts.port, 10);
|
|
2162
|
+
|
|
2163
|
+
const checkPort = (p: number): Promise<boolean> => new Promise((resolve) => {
|
|
2164
|
+
const sock = new net.Socket();
|
|
2165
|
+
sock.setTimeout(400);
|
|
2166
|
+
sock.once('connect', () => { sock.destroy(); resolve(true); });
|
|
2167
|
+
sock.once('error', () => { sock.destroy(); resolve(false); });
|
|
2168
|
+
sock.once('timeout', () => { sock.destroy(); resolve(false); });
|
|
2169
|
+
sock.connect(p, 'localhost');
|
|
2118
2170
|
});
|
|
2171
|
+
|
|
2172
|
+
const server = new StudioServer({ port, agentDir: process.cwd() });
|
|
2119
2173
|
await server.start();
|
|
2174
|
+
|
|
2175
|
+
// Try to start sub-module UI servers with graceful fallback
|
|
2176
|
+
const subModules = [
|
|
2177
|
+
{ name: 'DeepBrain', icon: '🧠', pkg: 'deepbrain', port: 4001, serveMethod: 'serveUI' },
|
|
2178
|
+
{ name: 'AgentKits', icon: '📊', pkg: 'agent-kits', port: 4002, serveMethod: 'serveUI' },
|
|
2179
|
+
{ name: 'Workstation', icon: '👤', pkg: 'agent-workstation', port: 4003, serveMethod: 'serveUI' },
|
|
2180
|
+
];
|
|
2181
|
+
|
|
2182
|
+
const moduleStatuses: string[] = [];
|
|
2183
|
+
for (const mod of subModules) {
|
|
2184
|
+
try {
|
|
2185
|
+
const already = await checkPort(mod.port);
|
|
2186
|
+
if (already) {
|
|
2187
|
+
moduleStatuses.push(` ${icon.success} ${mod.icon} ${mod.name} already running on :${mod.port}`);
|
|
2188
|
+
continue;
|
|
2189
|
+
}
|
|
2190
|
+
const modExports = require(mod.pkg);
|
|
2191
|
+
if (typeof modExports[mod.serveMethod] === 'function') {
|
|
2192
|
+
modExports[mod.serveMethod]({ port: mod.port });
|
|
2193
|
+
await new Promise(r => setTimeout(r, 600));
|
|
2194
|
+
const started = await checkPort(mod.port);
|
|
2195
|
+
moduleStatuses.push(started
|
|
2196
|
+
? ` ${icon.success} ${mod.icon} ${mod.name} started on :${mod.port}`
|
|
2197
|
+
: ` ${icon.warn} ${mod.icon} ${mod.name} failed to start`);
|
|
2198
|
+
} else {
|
|
2199
|
+
moduleStatuses.push(` ${color.dim('○')} ${mod.icon} ${mod.name} no serve method`);
|
|
2200
|
+
}
|
|
2201
|
+
} catch {
|
|
2202
|
+
moduleStatuses.push(` ${color.dim('○')} ${mod.icon} ${mod.name} not installed`);
|
|
2203
|
+
}
|
|
2204
|
+
}
|
|
2205
|
+
|
|
2206
|
+
if (moduleStatuses.length > 0) {
|
|
2207
|
+
console.log('\nModules:');
|
|
2208
|
+
moduleStatuses.forEach(s => console.log(s));
|
|
2209
|
+
}
|
|
2210
|
+
|
|
2211
|
+
const url = `http://localhost:${port}`;
|
|
2212
|
+
console.log(`\n${icon.success} OPC Studio ready → ${color.cyan(url)}`);
|
|
2213
|
+
|
|
2214
|
+
if (opts.open !== false) {
|
|
2215
|
+
try {
|
|
2216
|
+
const { exec } = require('child_process');
|
|
2217
|
+
const openCmd = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'start ""' : 'xdg-open';
|
|
2218
|
+
exec(`${openCmd} ${url}`);
|
|
2219
|
+
} catch {}
|
|
2220
|
+
}
|
|
2221
|
+
|
|
2120
2222
|
console.log(color.dim('Press Ctrl+C to stop'));
|
|
2121
2223
|
});
|
|
2122
2224
|
|