runner.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. import { streamChat } from "../llm-client"
  2. import type { StreamCallbacks } from "../llm-client"
  3. import { accumulateToolCalls } from "./tool-call-parser"
  4. import { toOpenAITools } from "./tools-schema"
  5. import type { ToolRegistry } from "./registry"
  6. import type { AgentConfig, AgentMessage, AgentRunCallbacks, AgentRunRecord, ToolCall, ToolCallDelta } from "./types"
  7. import { DEFAULT_MAX_ROUNDS, TOOL_EXECUTE_TIMEOUT_MS } from "./types"
  8. import type { TaskBreakpoint } from "./task-breakpoint"
  9. import {
  10. clearTaskBreakpoint,
  11. createTaskBreakpoint,
  12. saveTaskBreakpoint,
  13. updateBreakpointStage,
  14. } from "./task-breakpoint"
  15. import type { ChatMessage } from "../llm-providers"
  16. import { isReasoningDisabled, isReasoningOnlyResponseError, withReasoningDisabled } from "../reasoning-retry"
  17. import { addLlmUsage } from "../llm-usage"
  18. import { trimChatMessagesToBudget } from "../chat-request-budget"
  19. import { ToolEvidenceLedger } from "./tool-evidence-ledger"
  20. export class ModelDoesNotSupportToolsError extends Error {
  21. constructor() {
  22. super("当前模型不支持工具调用")
  23. this.name = "ModelDoesNotSupportToolsError"
  24. }
  25. }
  26. function messageContentText(content: AgentMessage["content"]): string {
  27. if (typeof content === "string") return content
  28. return content
  29. .map((block) => (block.type === "text" ? block.text : ""))
  30. .join("")
  31. }
  32. function withToolTimeout<T>(operation: Promise<T>, timeoutMs: number | undefined): Promise<T> {
  33. const resolvedTimeoutMs = timeoutMs ?? TOOL_EXECUTE_TIMEOUT_MS
  34. if (resolvedTimeoutMs <= 0) return operation
  35. return Promise.race([
  36. operation,
  37. new Promise<never>((_, reject) =>
  38. setTimeout(() => reject(new Error("工具执行超时")), resolvedTimeoutMs),
  39. ),
  40. ])
  41. }
  42. export class AgentRunner {
  43. async run(
  44. config: AgentConfig,
  45. registry: ToolRegistry,
  46. messages: AgentMessage[],
  47. callbacks: AgentRunCallbacks,
  48. signal?: AbortSignal,
  49. ): Promise<AgentRunRecord> {
  50. const record: AgentRunRecord = { toolCalls: [], roundsUsed: 0, finalText: "" }
  51. const workingMessages = [...messages]
  52. let finalText = ""
  53. const maxRounds = config.maxRounds || DEFAULT_MAX_ROUNDS
  54. const projectPath = config.projectPath
  55. const taskGoal =
  56. config.taskGoal ||
  57. messageContentText([...messages].reverse().find((m) => m.role === "user")?.content ?? "") ||
  58. "未命名任务"
  59. const taskContract = `## 任务契约\n初始任务目标:${taskGoal.slice(0, 1800)}\n执行过程中不得因历史裁剪丢失该目标;当前用户新要求优先。`
  60. const contractInsertIndex = workingMessages.findIndex((message) => message.role !== "system")
  61. workingMessages.splice(contractInsertIndex < 0 ? workingMessages.length : contractInsertIndex, 0, {
  62. role: "system",
  63. content: taskContract,
  64. })
  65. const evidenceLedger = new ToolEvidenceLedger(config.toolResultContextLimit ?? 6000)
  66. let taskBreakpoint: TaskBreakpoint | null = projectPath
  67. ? createTaskBreakpoint({
  68. taskGoal,
  69. currentStage: "agent_round_1",
  70. })
  71. : null
  72. const persistTaskBreakpoint = async () => {
  73. if (!projectPath || !taskBreakpoint) return
  74. try {
  75. await saveTaskBreakpoint(projectPath, taskBreakpoint)
  76. } catch {
  77. // 断点保存失败不应中断当前 AI 会话
  78. }
  79. }
  80. const clearPersistedBreakpoint = async () => {
  81. if (!projectPath) return
  82. try {
  83. await clearTaskBreakpoint(projectPath)
  84. } catch {
  85. // clearTaskBreakpoint 内部已吞掉错误,这里保持双保险
  86. }
  87. }
  88. if (taskBreakpoint) {
  89. await persistTaskBreakpoint()
  90. }
  91. for (let round = 0; round < maxRounds; round++) {
  92. record.roundsUsed = round + 1
  93. if (signal?.aborted) {
  94. for (const tc of record.toolCalls) {
  95. if (tc.status === "running") {
  96. tc.status = "cancelled"
  97. tc.finishedAt = Date.now()
  98. callbacks.onToolEvent?.({
  99. type: "cancelled",
  100. callId: tc.id,
  101. name: tc.name,
  102. params: tc.params,
  103. timestamp: tc.finishedAt,
  104. })
  105. }
  106. }
  107. callbacks.onError(new Error("操作已取消"))
  108. return record
  109. }
  110. const toolCallDeltas: ToolCallDelta[] = []
  111. let roundText = ""
  112. let streamError: Error | undefined
  113. const streamCallbacks: StreamCallbacks = {
  114. onToken: (t: string) => {
  115. roundText += t
  116. },
  117. onToolCallDelta: (delta: ToolCallDelta) => {
  118. toolCallDeltas.push(delta)
  119. },
  120. onUsage: (usage) => {
  121. record.usage = addLlmUsage(record.usage, usage)
  122. },
  123. onDone: () => {
  124. // stream finished
  125. },
  126. onError: (err: Error) => {
  127. streamError = err
  128. },
  129. }
  130. const openaiTools = config.tools.length > 0 ? toOpenAITools(config.tools) : undefined
  131. const buildRequestOverrides = (baseOverrides = config.requestOverrides) =>
  132. openaiTools
  133. ? { ...baseOverrides, tools: openaiTools as any, toolChoice: "auto" as const }
  134. : baseOverrides
  135. let requestOverrides = buildRequestOverrides()
  136. const streamRound = async () => {
  137. const internalBudget = Math.max(1, Math.floor((config.llmConfig.maxContextSize || 204_800) * 0.75))
  138. const compacted = trimChatMessagesToBudget(workingMessages as ChatMessage[], internalBudget) as AgentMessage[]
  139. workingMessages.splice(0, workingMessages.length, ...compacted)
  140. await streamChat(
  141. config.llmConfig,
  142. workingMessages as ChatMessage[],
  143. streamCallbacks,
  144. signal,
  145. requestOverrides,
  146. )
  147. }
  148. try {
  149. await streamRound()
  150. } catch (err) {
  151. const msg = err instanceof Error ? err.message : String(err)
  152. if (openaiTools && /tool|function.?call|unsupported|不支持工具/i.test(msg)) {
  153. const modelErr = new ModelDoesNotSupportToolsError()
  154. callbacks.onError(modelErr)
  155. return record
  156. }
  157. callbacks.onError(err instanceof Error ? err : new Error(String(err)))
  158. return record
  159. }
  160. if (
  161. streamError &&
  162. isReasoningOnlyResponseError(streamError) &&
  163. !isReasoningDisabled(config.llmConfig, requestOverrides)
  164. ) {
  165. roundText = ""
  166. toolCallDeltas.length = 0
  167. streamError = undefined
  168. requestOverrides = buildRequestOverrides(withReasoningDisabled(config.requestOverrides))
  169. try {
  170. await streamRound()
  171. } catch (err) {
  172. callbacks.onError(err instanceof Error ? err : new Error(String(err)))
  173. return record
  174. }
  175. }
  176. if (streamError) {
  177. callbacks.onError(streamError)
  178. return record
  179. }
  180. // Check for tool calls
  181. const toolCalls = accumulateToolCalls(toolCallDeltas)
  182. if (toolCalls.length === 0) {
  183. finalText = roundText
  184. record.finalText = finalText
  185. if (roundText) callbacks.onText(roundText)
  186. await clearPersistedBreakpoint()
  187. callbacks.onDone()
  188. return record
  189. }
  190. // Add assistant message with tool calls
  191. const assistantMsg: AgentMessage = {
  192. role: "assistant",
  193. content: roundText || "",
  194. tool_calls: toolCalls,
  195. }
  196. workingMessages.push(assistantMsg)
  197. // Execute each tool call
  198. for (const tc of toolCalls) {
  199. const toolName = tc.function.name
  200. const tool = registry.get(toolName)
  201. const saveToolProgress = async () => {
  202. if (!taskBreakpoint) return
  203. const usedTools = taskBreakpoint.usedTools.includes(toolName)
  204. ? taskBreakpoint.usedTools
  205. : [...taskBreakpoint.usedTools, toolName]
  206. taskBreakpoint = updateBreakpointStage(
  207. { ...taskBreakpoint, usedTools },
  208. `agent_round_${round + 1}`,
  209. `tool:${toolName}`,
  210. )
  211. await persistTaskBreakpoint()
  212. }
  213. const params = (() => {
  214. try { return JSON.parse(tc.function.arguments || "{}") }
  215. catch { return {} }
  216. })()
  217. const toolCallRecord: AgentRunRecord["toolCalls"][number] = {
  218. id: tc.id,
  219. name: toolName,
  220. params,
  221. result: "",
  222. status: "running",
  223. startedAt: Date.now(),
  224. finishedAt: Date.now(),
  225. }
  226. const callbackToolCall: ToolCall = { id: tc.id, name: toolName, arguments: params }
  227. const executionContext = {
  228. callId: tc.id,
  229. toolName,
  230. onToolEvent: callbacks.onToolEvent,
  231. onActivityEvent: callbacks.onActivityEvent,
  232. }
  233. callbacks.onToolCall(callbackToolCall)
  234. callbacks.onToolEvent?.({
  235. type: "call_started",
  236. callId: tc.id,
  237. name: toolName,
  238. params,
  239. timestamp: toolCallRecord.startedAt,
  240. })
  241. if (!tool) {
  242. const errorMsg = `错误: 未知工具 ${toolName}`
  243. callbacks.onToolError(tc.id, errorMsg)
  244. toolCallRecord.status = "error"
  245. toolCallRecord.result = errorMsg
  246. toolCallRecord.finishedAt = Date.now()
  247. record.toolCalls.push(toolCallRecord)
  248. callbacks.onToolEvent?.({
  249. type: "error",
  250. callId: tc.id,
  251. name: toolName,
  252. params,
  253. result: errorMsg,
  254. timestamp: toolCallRecord.finishedAt,
  255. })
  256. workingMessages.push({
  257. role: "tool",
  258. content: evidenceLedger.format(toolName, params, toolCallRecord.result),
  259. tool_call_id: tc.id,
  260. name: toolName,
  261. })
  262. await saveToolProgress()
  263. continue
  264. }
  265. const permission = tool.permission ?? (tool.category === "write" ? "confirm" : "auto")
  266. if (permission === "confirm") {
  267. let preview = ""
  268. try {
  269. const previewFn = tool.generatePreview ?? tool.execute
  270. preview = await withToolTimeout(previewFn(params, signal, executionContext), tool.executeTimeoutMs)
  271. } catch (e) {
  272. preview = `预览生成失败:${e instanceof Error ? e.message : String(e)}`
  273. }
  274. toolCallRecord.status = "approval_required"
  275. ;(toolCallRecord as any).preview = preview
  276. toolCallRecord.result = preview
  277. toolCallRecord.finishedAt = Date.now()
  278. record.toolCalls.push(toolCallRecord)
  279. callbacks.onToolEvent?.({
  280. type: "approval_required",
  281. callId: tc.id,
  282. name: toolName,
  283. params,
  284. result: preview,
  285. preview,
  286. timestamp: toolCallRecord.finishedAt,
  287. })
  288. workingMessages.push({
  289. role: "tool",
  290. content: evidenceLedger.format(toolName, params, preview),
  291. tool_call_id: tc.id,
  292. name: toolName,
  293. })
  294. await saveToolProgress()
  295. continue
  296. }
  297. try {
  298. const result = await withToolTimeout(tool.execute(params, signal, executionContext), tool.executeTimeoutMs)
  299. toolCallRecord.result = result
  300. toolCallRecord.status = "done"
  301. toolCallRecord.finishedAt = Date.now()
  302. callbacks.onToolResult(tc.id, result)
  303. callbacks.onToolEvent?.({
  304. type: "result",
  305. callId: tc.id,
  306. name: toolName,
  307. params,
  308. result,
  309. timestamp: toolCallRecord.finishedAt,
  310. })
  311. } catch (err) {
  312. toolCallRecord.status = "error"
  313. toolCallRecord.result = `错误: ${err instanceof Error ? err.message : String(err)}`
  314. toolCallRecord.finishedAt = Date.now()
  315. callbacks.onToolError(tc.id, toolCallRecord.result)
  316. callbacks.onToolEvent?.({
  317. type: "error",
  318. callId: tc.id,
  319. name: toolName,
  320. params,
  321. result: toolCallRecord.result,
  322. timestamp: toolCallRecord.finishedAt,
  323. })
  324. }
  325. record.toolCalls.push(toolCallRecord)
  326. await saveToolProgress()
  327. workingMessages.push({
  328. role: "tool",
  329. content: evidenceLedger.format(toolName, params, toolCallRecord.result),
  330. tool_call_id: tc.id,
  331. name: toolName,
  332. })
  333. }
  334. // Continue loop
  335. if (signal?.aborted) {
  336. for (const tc of record.toolCalls) {
  337. if (tc.status === "running") {
  338. tc.status = "cancelled"
  339. tc.finishedAt = Date.now()
  340. callbacks.onToolEvent?.({
  341. type: "cancelled",
  342. callId: tc.id,
  343. name: tc.name,
  344. params: tc.params,
  345. timestamp: tc.finishedAt,
  346. })
  347. }
  348. }
  349. callbacks.onError(new Error("操作已取消"))
  350. return record
  351. }
  352. }
  353. // Exceeded max rounds
  354. callbacks.onError(new Error(`Agent 已达到最大调用轮次(${maxRounds}),请尝试减少引用内容或拆分任务`))
  355. return record
  356. }
  357. }