You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

234 lines
6.7 KiB

import './env';
import { hash } from "bcryptjs";
import { dbGlobal } from "./lib/db";
import { users } from "./lib/schema/auth";
import { agentTools } from "./lib/schema/agent-tool";
import { agents, agentToolAssociations, agentSessions, agentMessages, agentMessageFeedback } from "./lib/schema/agent";
const DEFAULT_SYSTEM_PROMPT = "You are a helpful AI assistant. Answer questions concisely and accurately. When tools are available, use them proactively to provide better answers.";
const CODER_SYSTEM_PROMPT = `You are a professional coding assistant. You help users with programming questions, code review, debugging, and software design.
Guidelines:
- Provide clear, concise, and accurate answers
- When showing code, use proper markdown code blocks with language tags
- Proactively use available tools (fetch, search) to gather information when needed
- Explain your reasoning when making architectural or design decisions
- If you're unsure, say so rather than guessing
- Focus on practical, working solutions`;
interface ToolSeed {
id: string;
name: string;
slug: string;
description: string;
type: string;
config: Record<string, unknown>;
enabled: number;
needsApproval: number;
adminOnly: number;
sortOrder: number;
}
const TOOLS_SEED: ToolSeed[] = [
{
id: "tool_fetch_html",
name: "fetch_html",
slug: "fetch_html",
description: "Fetch a URL and return the HTML content",
type: "fetch",
config: { parseMode: "raw", timeout: 30000, allowedDomains: ["*"], blockedDomains: [], maxResponseSize: 102400, defaultMethod: "GET", defaultHeaders: {} },
enabled: 1,
needsApproval: 0,
adminOnly: 0,
sortOrder: 10,
},
{
id: "tool_fetch_md",
name: "fetch_md",
slug: "fetch_md",
description: "Fetch a URL and return the content as markdown",
type: "fetch",
config: { parseMode: "markdown", timeout: 30000, allowedDomains: ["*"], blockedDomains: [], maxResponseSize: 102400, defaultMethod: "GET", defaultHeaders: {} },
enabled: 1,
needsApproval: 0,
adminOnly: 0,
sortOrder: 11,
},
{
id: "tool_fetch_json",
name: "fetch_json",
slug: "fetch_json",
description: "Fetch a URL and return the JSON content",
type: "fetch",
config: { parseMode: "json", timeout: 30000, allowedDomains: ["*"], blockedDomains: [], maxResponseSize: 102400, defaultMethod: "GET", defaultHeaders: {} },
enabled: 1,
needsApproval: 0,
adminOnly: 0,
sortOrder: 12,
},
{
id: "tool_user_info",
name: "user-info",
slug: "user-info",
description: "Get current user information",
type: "user-info",
config: {},
enabled: 1,
needsApproval: 0,
adminOnly: 0,
sortOrder: 80,
},
{
id: "tool_search",
name: "search",
slug: "search",
description: "Search the web for information",
type: "search",
config: { maxResults: 5 },
enabled: 1,
needsApproval: 0,
adminOnly: 0,
sortOrder: 90,
},
{
id: "tool_document",
name: "document",
slug: "document",
description: "创建、读取、更新、删除和列出文档,用于记忆管理和交付物产出",
type: "document",
config: {
approvalRequiredActions: ["delete"],
maxContentLength: 10000,
defaultTokenBudget: 2000,
maxListSize: 20,
},
enabled: 1,
needsApproval: 1,
adminOnly: 0,
sortOrder: 150,
},
{
id: "tool_agent_invoke",
name: "agent-invoke",
slug: "agent-invoke",
description: "调用其他 Agent 处理子任务,实现多 Agent 协作",
type: "agent-invoke",
config: {
maxRecursionDepth: 3,
resultTruncateLength: 8000,
timeoutMs: 60000,
maxOutputTokens: 8192,
},
enabled: 1,
needsApproval: 0,
adminOnly: 0,
sortOrder: 160,
},
];
async function seedTools() {
for (const t of TOOLS_SEED) {
await dbGlobal.insert(agentTools).values({
id: t.id,
name: t.name,
slug: t.slug,
description: t.description,
type: t.type,
config: JSON.stringify(t.config),
enabled: t.enabled,
needsApproval: t.needsApproval,
adminOnly: t.adminOnly,
sortOrder: t.sortOrder,
});
}
console.log(`Seeded ${TOOLS_SEED.length} tools`);
}
async function seedAgents() {
const [defaultAgent] = await dbGlobal
.insert(agents)
.values({
slug: "default",
name: "默认助手",
description: "通用 AI 助手,处理日常问答、信息查询、文本创作等通用任务。遇到专业编程问题时,可通过 agent-invoke 工具委托给编程助手处理。",
systemPrompt: DEFAULT_SYSTEM_PROMPT,
titleStrategy: "llm",
enableThinking: 0,
enableTools: 1,
maxStepCount: 6,
isDefault: 1,
isCallable: 0,
sortOrder: 0,
enabled: 1,
})
.returning();
const [coderAgent] = await dbGlobal
.insert(agents)
.values({
slug: "coder",
name: "编程助手",
description: "专业编程助手,擅长代码编写、调试、代码审查、架构设计和技术方案评估。可处理多种编程语言(Python、TypeScript、Go、Rust 等),能主动使用 fetch、search 等工具查阅技术文档。当用户需要编写代码、排查 bug、做技术选型或审查代码质量时,应委托给此 Agent。",
systemPrompt: CODER_SYSTEM_PROMPT,
titleStrategy: "first-line",
enableThinking: 1,
enableTools: 1,
maxStepCount: 12,
isDefault: 0,
isCallable: 1,
sortOrder: 10,
enabled: 1,
})
.returning();
console.log("Seeded 2 agents: default, coder");
const allToolIds = TOOLS_SEED.map((t) => t.id);
const coderToolIds = ["tool_fetch_md", "tool_search"];
for (let i = 0; i < allToolIds.length; i++) {
await dbGlobal.insert(agentToolAssociations).values({
agentId: defaultAgent.id,
toolId: allToolIds[i],
sortOrder: i,
});
}
for (let i = 0; i < coderToolIds.length; i++) {
await dbGlobal.insert(agentToolAssociations).values({
agentId: coderAgent.id,
toolId: coderToolIds[i],
sortOrder: i,
});
}
console.log(`Seeded ${allToolIds.length} tool associations for default agent`);
console.log(`Seeded ${coderToolIds.length} tool associations for coder agent`);
}
async function seedAdmin() {
const password = await hash("123456qaz", 10);
await dbGlobal.insert(users).values({
username: "admin",
password,
role: "admin",
status: "active",
nickname: "管理员",
});
console.log("Seeded admin user (admin / 123456qaz)");
}
async function main() {
await seedAdmin();
await seedTools();
await seedAgents();
console.log("Seed complete!");
process.exit(0);
}
main().catch((e) => {
console.error(e);
process.exit(1);
});