diff --git a/packages/aiCore/src/core/plugins/built-in/index.ts b/packages/aiCore/src/core/plugins/built-in/index.ts index 2510b756fa..a6abc21252 100644 --- a/packages/aiCore/src/core/plugins/built-in/index.ts +++ b/packages/aiCore/src/core/plugins/built-in/index.ts @@ -5,5 +5,5 @@ export const BUILT_IN_PLUGIN_PREFIX = 'built-in:' export { createLoggingPlugin } from './logging' -export type { MCPPromptConfig, ToolUseResult } from './mcpPrompt' -export { createMCPPromptPlugin } from './mcpPrompt' +export type { MCPPromptConfig, ToolUseResult } from './mcpPromptPlugin' +export { createMCPPromptPlugin } from './mcpPromptPlugin' diff --git a/packages/aiCore/src/core/plugins/built-in/mcpPrompt.ts b/packages/aiCore/src/core/plugins/built-in/mcpPromptPlugin.ts similarity index 99% rename from packages/aiCore/src/core/plugins/built-in/mcpPrompt.ts rename to packages/aiCore/src/core/plugins/built-in/mcpPromptPlugin.ts index c75486fe93..7f3fb2a7f6 100644 --- a/packages/aiCore/src/core/plugins/built-in/mcpPrompt.ts +++ b/packages/aiCore/src/core/plugins/built-in/mcpPromptPlugin.ts @@ -286,10 +286,10 @@ function defaultParseToolUse(content: string, tools: ToolSet): ToolUseResult[] { /** * 创建 MCP Prompt 插件 */ -export const createMCPPromptPlugin = definePlugin((config: MCPPromptConfig = {}) => { +export const createMCPPromptPlugin = (config: MCPPromptConfig = {}) => { const { enabled = true, buildSystemPrompt = defaultBuildSystemPrompt, parseToolUse = defaultParseToolUse } = config - return { + return definePlugin({ name: 'built-in:mcp-prompt', transformParams: async (params: any, context: MCPRequestContext) => { @@ -562,5 +562,5 @@ export const createMCPPromptPlugin = definePlugin((config: MCPPromptConfig = {}) } }) } - } -}) + }) +} diff --git a/packages/aiCore/src/core/plugins/examples/example-plugins.ts b/packages/aiCore/src/core/plugins/examples/example-plugins.ts deleted file mode 100644 index 5345f72c69..0000000000 --- a/packages/aiCore/src/core/plugins/examples/example-plugins.ts +++ /dev/null @@ -1,192 +0,0 @@ -import type { AiPlugin } from '../types' - -/** - * 【First 钩子示例】模型别名解析插件 - */ -export const ModelAliasPlugin: AiPlugin = { - name: 'model-alias', - enforce: 'pre', - - async resolveModel(modelId) { - const aliases: Record = { - gpt4: 'gpt-4-turbo-preview', - claude: 'claude-3-sonnet-20240229', - gemini: 'gemini-pro' - } - - return aliases[modelId] || null - } -} - -/** - * 【Sequential 钩子示例】参数验证和转换插件 - */ -export const ParamsValidationPlugin: AiPlugin = { - name: 'params-validation', - - async transformParams(params) { - // 参数验证 - if (!params.messages || !Array.isArray(params.messages)) { - throw new Error('Invalid messages parameter') - } - - // 参数转换:添加默认配置 - return { - ...params, - temperature: params.temperature ?? 0.7, - max_tokens: params.max_tokens ?? 4096, - stream: params.stream ?? true - } - }, - - async transformResult(result, context) { - // 结果后处理:添加元数据 - return { - ...result, - metadata: { - ...result.metadata, - processedAt: new Date().toISOString(), - provider: context.providerId, - model: context.modelId - } - } - } -} - -/** - * 【Parallel 钩子示例】日志记录插件 - */ -export const LoggingPlugin: AiPlugin = { - name: 'logging', - - async onRequestStart(context) { - console.log(`🚀 AI请求开始: ${context.providerId}/${context.modelId}`, { - requestId: context.requestId, - timestamp: new Date().toISOString() - }) - }, - - async onRequestEnd(context, result) { - const duration = Date.now() - context.startTime - console.log(`✅ AI请求完成: ${context.requestId} (${duration}ms)`, { - provider: context.providerId, - model: context.modelId, - hasResult: !!result - }) - }, - - async onError(error, context) { - const duration = Date.now() - context.startTime - console.error(`❌ AI请求失败: ${context.requestId} (${duration}ms)`, { - provider: context.providerId, - model: context.modelId, - error: error.message, - stack: error.stack - }) - } -} - -/** - * 【Parallel 钩子示例】性能监控插件 - */ -export const PerformancePlugin: AiPlugin = { - name: 'performance', - enforce: 'post', - - async onRequestEnd(context) { - const duration = Date.now() - context.startTime - - // 记录性能指标 - const metrics = { - requestId: context.requestId, - provider: context.providerId, - model: context.modelId, - duration, - timestamp: context.startTime, - success: true - } - - // 发送到监控系统(这里只是示例) - // await sendMetrics(metrics) - console.log('📊 性能指标:', metrics) - }, - - async onError(error, context) { - const duration = Date.now() - context.startTime - - const metrics = { - requestId: context.requestId, - provider: context.providerId, - model: context.modelId, - duration, - timestamp: context.startTime, - success: false, - errorType: error.constructor.name - } - - console.log('📊 错误指标:', metrics) - } -} - -/** - * 【Stream 钩子示例】内容过滤插件 - */ -export const ContentFilterPlugin: AiPlugin = { - name: 'content-filter', - - transformStream() { - return () => - new TransformStream({ - transform(chunk, controller) { - // 过滤敏感内容 - if (chunk.type === 'text-delta') { - const filtered = chunk.textDelta.replace(/\b(敏感词|违禁词)\b/g, '***') - controller.enqueue({ - ...chunk, - textDelta: filtered - }) - } else { - controller.enqueue(chunk) - } - } - }) - } -} - -/** - * 【First 钩子示例】模板加载插件 - */ -export const TemplatePlugin: AiPlugin = { - name: 'template-loader', - - async loadTemplate(templateName) { - const templates: Record = { - chat: { - systemPrompt: '你是一个有用的AI助手', - temperature: 0.7 - }, - coding: { - systemPrompt: '你是一个专业的编程助手,请提供清晰、高质量的代码', - temperature: 0.3 - }, - creative: { - systemPrompt: '你是一个创意写作助手,请发挥想象力', - temperature: 0.9 - } - } - - return templates[templateName] || null - } -} - -/** - * 示例插件组合 - */ -export const defaultPlugins: AiPlugin[] = [ - ModelAliasPlugin, - TemplatePlugin, - ParamsValidationPlugin, - LoggingPlugin, - PerformancePlugin, - ContentFilterPlugin -]