UNPKG

@voyager-0x/agent-mcp

Version:

Voyager MCP Agent - A powerful Model Context Protocol agent

89 lines (88 loc) 2.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.z = exports.mcpModulesManager = exports.createMcpServerModule = exports.createMcpDslModule = exports.WebMcpAgent = void 0; const agent_js_1 = require("./agent.js"); const config_js_1 = require("./config.js"); const module_js_1 = require("./module.js"); Object.defineProperty(exports, "mcpModulesManager", { enumerable: true, get: function () { return module_js_1.mcpModulesManager; } }); const streamBuffer_js_1 = require("./streamBuffer.js"); const zod_1 = require("zod"); Object.defineProperty(exports, "z", { enumerable: true, get: function () { return zod_1.z; } }); class WebMcpAgent { constructor(config) { this.streamBuffer = new streamBuffer_js_1.StreamBuffer(); this.onmessage = () => { }; this.onfinish = () => { }; config_js_1.aiConfigManager.setConfig(config); } updateConfig(config) { config_js_1.aiConfigManager.setConfig(config); } createChat() { this.agent = new agent_js_1.McpAgent(); this.streamBuffer.reset(); // 重置流式缓存器 this.agent?.session.on("reply", (deltaMessage) => { // 使用智能缓存器处理流式内容 const readyToSend = this.streamBuffer.processChunk(deltaMessage); if (readyToSend) { this.onmessage(readyToSend); } }); this.agent?.session.on("finish", () => { // 会话结束时,刷新缓存器中剩余的内容 const remainingContent = this.streamBuffer.flush(); if (remainingContent) { this.onmessage(remainingContent); } this.onfinish(); }); return this.agent; } /** * 获取聊天消息 * @returns 聊天消息列表 */ getChatMessages() { return this.agent?.session.list(); } /** * 获取当前聊天会话 * @returns 聊天会话 */ getAgent() { if (!this.agent) { this.createChat(); } return this.agent; } /** * 发送消息 * @param message 消息内容 */ chat(message) { if (!this.agent) { this.createChat(); } return this.agent.chat(message); } /** * 取消当前聊天 */ cancelChat() { if (this.agent) { this.agent.cancelChat(); } } /** * 注册回复事件 * @param onmessage 回复事件 * @param onfinish 完成事件 */ onReply(onmessage, onfinish) { this.onmessage = onmessage; this.onfinish = onfinish; } } exports.WebMcpAgent = WebMcpAgent; exports.createMcpDslModule = module_js_1.mcpModulesManager.createMcpDslModule.bind(module_js_1.mcpModulesManager); exports.createMcpServerModule = module_js_1.mcpModulesManager.createMcpServerModule.bind(module_js_1.mcpModulesManager);