修改前端架构到Electron ,增加llm_api页面,增加chat SSE渲染,修改若安按钮,卡片名称修改等小功能,增加agent-core,增加SQlite,增加llm UDS Pipe websocke
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
import { initDatabase, closeDb } from './db/database'
|
||||
import { startWebSocketServer } from './transport/websocket'
|
||||
import { WorkflowEngine } from './workflow/engine'
|
||||
import { ToolRegistry, readFileTool } from './tools/registry'
|
||||
import type { AgentContext, LLMConfig, StreamCallbacks } from './types'
|
||||
|
||||
export * from './types'
|
||||
export { WorkflowEngine } from './workflow/engine'
|
||||
export { ToolRegistry, readFileTool } from './tools/registry'
|
||||
export { LLMProvider } from './llm/provider'
|
||||
export { llmConfigManager } from './llm/config'
|
||||
|
||||
// ============ 启动配置 ============
|
||||
|
||||
const WS_PORT = Number(process.env.AGENT_CORE_PORT) || 8787
|
||||
const DB_DIR = process.env.AGENT_CORE_DB_DIR || './data'
|
||||
|
||||
/**
|
||||
* agent-core 入口
|
||||
* 启动 SQLite + WebSocket 服务器
|
||||
*/
|
||||
function main() {
|
||||
console.log('========================================')
|
||||
console.log(' dndClaw agent-core 启动中...')
|
||||
console.log('========================================')
|
||||
|
||||
// 1. 初始化 SQLite
|
||||
initDatabase(DB_DIR)
|
||||
console.log(`[core] 数据库目录: ${DB_DIR}`)
|
||||
|
||||
// 2. 启动 WebSocket
|
||||
const wss = startWebSocketServer(WS_PORT)
|
||||
console.log(`[core] WebSocket 端口: ${WS_PORT}`)
|
||||
|
||||
// 优雅退出
|
||||
process.on('SIGINT', () => {
|
||||
console.log('\n[core] 正在关闭...')
|
||||
wss.close()
|
||||
closeDb()
|
||||
process.exit(0)
|
||||
})
|
||||
|
||||
console.log('========================================')
|
||||
console.log(' dndClaw agent-core 已就绪')
|
||||
console.log('========================================')
|
||||
}
|
||||
|
||||
// ============ 智能体工厂(供编程调用) ============
|
||||
|
||||
export function createAgent(config: LLMConfig) {
|
||||
const toolRegistry = new ToolRegistry()
|
||||
toolRegistry.register(readFileTool)
|
||||
|
||||
const context: AgentContext = {
|
||||
messages: [],
|
||||
config,
|
||||
tools: toolRegistry.list()
|
||||
}
|
||||
|
||||
const engine = new WorkflowEngine(context)
|
||||
|
||||
return {
|
||||
async chat(userInput: string, callbacks?: StreamCallbacks) {
|
||||
return engine.run(userInput, callbacks)
|
||||
},
|
||||
getMessages() {
|
||||
return context.messages
|
||||
},
|
||||
clear() {
|
||||
context.messages = []
|
||||
},
|
||||
registerTool(
|
||||
name: string,
|
||||
description: string,
|
||||
execute: (params: Record<string, unknown>) => Promise<string>
|
||||
) {
|
||||
toolRegistry.register({ name, description, parameters: {}, execute })
|
||||
context.tools = toolRegistry.list()
|
||||
},
|
||||
listTools() {
|
||||
return toolRegistry.list()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export type Agent = ReturnType<typeof createAgent>
|
||||
|
||||
// 直接运行时启动服务(ESM)
|
||||
import { fileURLToPath } from 'url'
|
||||
import { resolve } from 'path'
|
||||
const __filename = fileURLToPath(import.meta.url)
|
||||
if (process.argv[1] && resolve(process.argv[1]) === resolve(__filename)) {
|
||||
main()
|
||||
}
|
||||
Reference in New Issue
Block a user