refactor: improve type imports and simplify system message handling in plugin functions

This commit is contained in:
suyao 2025-12-29 19:39:55 +08:00
parent 9ee15ceec9
commit 42b4a8f7d1
No known key found for this signature in database
4 changed files with 6 additions and 29 deletions

View File

@ -16,9 +16,9 @@ import { MockLanguageModelV3 } from 'ai/test'
import { vi } from 'vitest'
import * as z from 'zod'
import { StreamTextParams, StreamTextResult } from '../../core/plugins'
import type { StreamTextParams, StreamTextResult } from '../../core/plugins'
import type { ProviderId } from '../../core/providers/types'
import { AiRequestContext } from '../../types'
import type { AiRequestContext } from '../../types'
/**
* Type for partial overrides that allows omitting the model field

View File

@ -288,16 +288,11 @@ export const createPromptToolUsePlugin = (
// 构建系统提示符(只包含非 provider 工具)
const userSystemPrompt = typeof params.system === 'string' ? params.system : ''
const systemPrompt = buildSystemPrompt(userSystemPrompt, promptTools)
let systemMessage: string | null = systemPrompt
if (config.createSystemMessage) {
// 🎯 如果用户提供了自定义处理函数,使用它
systemMessage = config.createSystemMessage(systemPrompt, params, context)
}
// 保留 provide tools移除其他 tools
const transformedParams = {
...params,
...(systemMessage ? { system: systemMessage } : {}),
...(systemPrompt ? { system: systemPrompt } : {}),
tools: Object.keys(providerDefinedTools).length > 0 ? providerDefinedTools : undefined
}
context.originalParams = transformedParams

View File

@ -22,7 +22,6 @@ export interface PromptToolUseConfig extends BaseToolUsePluginConfig {
buildSystemPrompt?: (userSystemPrompt: string, tools: ToolSet) => string
// 自定义工具解析函数(可选,有默认实现)
parseToolUse?: (content: string, tools: ToolSet) => { results: ToolUseResult[]; content: string }
createSystemMessage?: (systemPrompt: string, originalParams: any, context: AiRequestContext) => string | null
}
/**

View File

@ -28,7 +28,7 @@ const logger = loggerService.withContext('PluginBuilder')
export function buildPlugins(
middlewareConfig: AiSdkMiddlewareConfig & { assistant: Assistant; topicId?: string }
): AiPlugin[] {
const plugins: AiPlugin[] = []
const plugins: AiPlugin<any, any>[] = []
if (middlewareConfig.topicId && getEnableDeveloperMode()) {
// 0. 添加 telemetry 插件
@ -58,7 +58,7 @@ export function buildPlugins(
}
// 0.3 OpenRouter reasoning redaction
if (middlewareConfig.provider?.id === SystemProviderIds.openrouter && middlewareConfig.enableReasoning) {
if (middlewareConfig.provider?.id === SystemProviderIds.openrouter) {
plugins.push(createOpenrouterReasoningPlugin())
}
@ -112,24 +112,7 @@ export function buildPlugins(
if (middlewareConfig.isPromptToolUse) {
plugins.push(
createPromptToolUsePlugin({
enabled: true,
createSystemMessage: (systemPrompt, params, context) => {
const modelId = typeof context.model === 'string' ? context.model : context.model.modelId
if (modelId.includes('o1-mini') || modelId.includes('o1-preview')) {
if (context.isRecursiveCall) {
return null
}
params.messages = [
{
role: 'assistant',
content: systemPrompt
},
...params.messages
]
return null
}
return systemPrompt
}
enabled: true
})
)
}