workspaceRuntime.ts 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. import {DESKTOP_ENVIRONMENT_TOOLS,DESKTOP_ENVIRONMENT_RULES,DESKTOP_FILE_TOOL_IDS,type DesktopEnvironments} from './desktopEnvironments.js'
  2. import {GROK_COMPUTER_TOOLS,grokComputerRules,type GrokCloud} from './grokCloud.js'
  3. import {VM_COMPUTER_TOOLS,VM_COMPUTER_RULES,VmToolSession} from './vmComputer.js'
  4. import { randomUUID } from 'node:crypto'
  5. import { readFileSync } from 'node:fs'
  6. import { z } from 'zod'
  7. import { WorkspaceStore, parse } from './workspaceStore.js'
  8. import { WorkspaceNodes, WorkspaceGateway, type GatewayFrame } from './workspaceGateway.js'
  9. import { HttpError } from './errors.js'
  10. import { applyWorkingDirectory, configuredWorkingDirectory } from './sessionWorkingDirectory.js'
  11. import {type StoredWorkspaceFile} from './workspaceAssets.js'
  12. import { UploadStore } from './uploads.js'
  13. import type {
  14. WorkspaceAgent as Agent,
  15. WorkspaceConversation as Conversation,
  16. WorkspaceMessage as Message,
  17. WorkspaceRun,
  18. WorkspaceInteraction,
  19. WorkspaceAgentUsageSummary,
  20. } from '../shared/workspace.js'
  21. import { personaSection } from '../shared/workspace.js'
  22. import { readHostTools } from './hostToolSettings.js'
  23. function globalComputers(home: string) {
  24. try { return readHostTools(home) }
  25. catch { return { denyServerTools: true, scriptMachine: false, serverComputer: false, vm: false, cloud: false } }
  26. }
  27. import { WorkspaceScheduler, type Work, NO_REPLY, HOST_FALLBACK, WORKSPACE_CONCURRENCY_LIMIT, RESOURCE_WAIT_CODES } from './workspaceScheduler.js'
  28. import type { WorkspacePlugins, OpenBotPlugins } from './botPlugins/workspacePlugins.js'
  29. import { mentionedAgents } from './workspaceMentions.js'
  30. import { WorkspaceTeamTools, TEAM_TOOL_RULES } from './workspaceTeamTools.js'
  31. import { createWorkspaceToolLease, type WorkspaceToolLease } from './workspaceToolLease.js'
  32. import { WorkspaceTaskCoordinator } from './taskCoordinator.js'
  33. import { isDiscussion, discussionRounds } from './workspaceDiscussion.js'
  34. import { WorkspaceKnowledge } from './workspaceKnowledge.js'
  35. import type { OpenVikingService } from './openVikingService.js'
  36. import { WorkspaceCollaboration } from './workspaceCollaboration.js'
  37. import { WorkspaceKnowledgeTools, BOT_KNOWLEDGE_RULES } from './workspaceKnowledgeTools.js'
  38. export { mentionedAgents } from './workspaceMentions.js'
  39. type Run = WorkspaceRun
  40. export interface WorkspaceBinding {
  41. memoryVersion?: string
  42. runnerId?: string
  43. execution?:string
  44. vmExecution?:Agent["vmExecution"]
  45. hermesComputer?:boolean
  46. computerEnvironmentId?:string
  47. remoteAgentId?: string
  48. id: string
  49. nodeId: string
  50. profile: string
  51. storedId: string
  52. runtimeId: string
  53. aliases: string[]
  54. runId: string
  55. messageId: string
  56. conversationTaskId?: string
  57. taskId?: string
  58. contextSeq?: number
  59. }
  60. interface LiveTurn {
  61. gateway: WorkspaceGateway
  62. runtimeId: string
  63. runId: string
  64. agentId: string
  65. conversationTaskId?: string
  66. taskId: string
  67. done(error?: Error): void
  68. }
  69. export const sendInput = z
  70. .object({
  71. requestId: z.string().uuid(),
  72. content: z.string().max(65_536).default(''),
  73. taskId: z.string().uuid().optional(),
  74. mode: z.enum(['chat', 'goal']).optional(),
  75. rounds: z.number().int().min(1).max(12).optional(),
  76. coordinatorId: z.string().uuid().optional(),
  77. projectId: z.string().min(1).max(100).nullable().optional(),
  78. mentionIds: z.array(z.string().uuid()).max(8).default([]),
  79. fileIds: z.array(z.string().uuid()).max(8).default([]),
  80. })
  81. .strict()
  82. .refine((b) => b.content.trim() || b.fileIds.length, '请输入消息或添加附件')
  83. export class WorkspaceRuntime extends WorkspaceScheduler {
  84. readonly knowledge: WorkspaceKnowledge
  85. readonly openViking?: OpenVikingService
  86. readonly collaboration: WorkspaceCollaboration
  87. readonly knowledgeTools: WorkspaceKnowledgeTools
  88. onMemoryCandidate: (owner: string, run: Run) => void = () => {}
  89. plugins?: WorkspacePlugins
  90. get idleForUpdate(): boolean {
  91. return this.live.size === 0 && this.executing.size === 0 && !this.store.db.prepare("SELECT 1 FROM workspace_entities WHERE kind IN ('run','turn') AND COALESCE(json_extract(data,'$.status'),'unknown') NOT IN ('complete','failed','interrupted') LIMIT 1").get()
  92. }
  93. desktopEnvironments?:DesktopEnvironments
  94. cloud?:GrokCloud
  95. /** Wired to the runner hub: false degrades envs VM turns to profile sessions. */
  96. computerAvailable?:(owner:string,agent:Agent)=>boolean
  97. inspector?:import('./workspaceInspector.js').WorkspaceInspector
  98. private live = new Map<string, LiveTurn>()
  99. readonly teamTools: WorkspaceTeamTools
  100. readonly tasks: WorkspaceTaskCoordinator
  101. retireHelper:(owner:string,helper:Agent)=>Promise<void>=async()=>{throw new Error('电脑清理服务未连接')}
  102. publishArtifact:(owner:string,message:Message,agent:Agent,name:string,bytes:Buffer)=>import('../shared/workspace.js').WorkspaceFile=()=>{throw new Error('产物服务未初始化')}
  103. onTeamCreated: (owner: string, team: Conversation) => void = () => {}
  104. constructor(store: WorkspaceStore, readonly nodes: WorkspaceNodes, readonly uploads: UploadStore, userActive: (owner: string) => boolean = () => true,
  105. readonly authorizationVersion: (owner: string) => number = () => 0, openViking?: OpenVikingService) {
  106. super(store, userActive)
  107. this.openViking = openViking
  108. this.teamTools = new WorkspaceTeamTools(store, nodes, this)
  109. this.tasks = new WorkspaceTaskCoordinator(store, this)
  110. this.knowledge = new WorkspaceKnowledge(store.home, store, openViking)
  111. this.collaboration = new WorkspaceCollaboration(store, this)
  112. this.knowledgeTools = new WorkspaceKnowledgeTools(this)
  113. this.onRunSettled = (owner, run) => {
  114. this.collaboration.settled(owner, run)
  115. if (run.status === 'complete' && (!run.triggerKind || run.triggerKind === 'peer')) queueMicrotask(() => { if (!this.closing) this.onMemoryCandidate(owner, run) })
  116. }
  117. }
  118. async bindNewAgent(owner: string, id: string): Promise<void> {
  119. if (!this.openViking?.enabled) return
  120. await this.openViking.ensureUser(owner, { id, temporaryGoalId: undefined })
  121. }
  122. async cleanupAgent(owner: string, id: string): Promise<void> {
  123. if (!this.openViking) return
  124. await this.openViking.removeUser(owner, { id, temporaryGoalId: undefined })
  125. }
  126. send(owner: string, conversationId: string, input: unknown): Run {
  127. return this.submit(owner, conversationId, input)
  128. }
  129. async botCapabilities(owner: string, agent: Agent, target = agent.remoteAgentId || agent.execution === 'computer' ? this.nodes.targetForAgent(owner, agent) : this.nodes.target(owner, agent.nodeId)): Promise<{ tools: boolean; memory: boolean; extraction: boolean }> {
  130. try {
  131. const response = await target.session.request('/api/plugins/yaoyao-bot-bridge/capabilities', { search: new URLSearchParams({ profile: agent.profile }) })
  132. const value = JSON.parse(response.body.toString())
  133. const tools = response.status === 200 && value.ready === true && value.native_tools === true && value.in_process === true
  134. return { tools, memory: tools && value.memory_isolation === true, extraction: value.memory_extraction === true }
  135. } catch (error) {
  136. if (error instanceof HttpError && RESOURCE_WAIT_CODES.has(error.code ?? '')) throw error
  137. return { tools: false, memory: false, extraction: false }
  138. }
  139. }
  140. async stopPeerRun(owner: string, id: string): Promise<void> { await super.stop(owner, id) }
  141. async preemptPeerWork(owner: string, agentId: string, exceptRunId: string): Promise<void> {
  142. const ids = [...new Set(this.store.list<Work>(owner, 'turn').filter(w => w.agentId === agentId && w.runId !== exceptRunId && ['running', 'waiting'].includes(w.status)).map(w => w.runId))]
  143. for (const id of ids) if (this.store.get<Run>(owner, 'run', id)?.triggerKind) await super.stop(owner, id).catch(() => {})
  144. }
  145. override async stop(owner: string, id: string): Promise<void> {
  146. const root = this.store.require<Run>(owner, 'run', id)
  147. await this.collaboration.stopFor(owner, chain => chain.rootRunId === id || chain.id === root.collaborationChainId)
  148. await super.stop(owner, id)
  149. }
  150. /** Server-owned provenance. This is never exposed as a model-supplied identity. */
  151. dispatch(owner: string, conversationId: string, input: unknown, source: {
  152. agentId: string; assignmentId?: string; targetAgentId?: string; kind: NonNullable<Run['triggerKind']>; instruction?: string; taskReference?: {conversationId:string;taskId:string}
  153. peerMessageId?: string; collaborationChainId?: string; priority?: boolean; projectId?: string
  154. }): Run {
  155. this.nodes.requireSource(owner, this.store.require<Agent>(owner, 'agent', source.agentId))
  156. return this.submit(owner, conversationId, input, source)
  157. }
  158. private files(owner:string,ids:string[]){return ids.map(id=>{const file=this.store.get<StoredWorkspaceFile>(owner,'file',id);if(file){if(file.sourceNodeId&&file.profile)this.nodes.requireSource(owner,{nodeId:file.sourceNodeId,profile:file.profile});return file}return {...this.uploads.records([id],owner)[0]!,sender:'user' as const}})}
  159. private submit(owner: string, conversationId: string, input: unknown, source?: {
  160. agentId: string; assignmentId?: string; targetAgentId?: string; kind: NonNullable<Run['triggerKind']>; instruction?: string; taskReference?: {conversationId:string;taskId:string}
  161. peerMessageId?: string; collaborationChainId?: string; priority?: boolean; projectId?: string
  162. }): Run {
  163. if (!this.userActive(owner)) throw new HttpError(403,'账号授权已失效','account_inactive')
  164. const body = parse(sendInput, input)
  165. for (const id of this.store.require<Conversation>(owner, 'conversation', conversationId).memberIds)
  166. this.nodes.requireSource(owner, this.store.require<Agent>(owner, 'agent', id))
  167. const result = this.store.command(owner, body.requestId, { conversationId, ...body, ...(source ? { source } : {}) }, () => {
  168. const c = this.store.require<Conversation>(owner, 'conversation', conversationId)
  169. if (c.archived) throw new HttpError(409, '聊天已归档', 'conversation_archived')
  170. const projectId = source?.kind === 'peer' ? source.projectId : c.kind === 'group' ? c.projectId : body.projectId ?? undefined
  171. if (projectId) for (const id of source?.targetAgentId ? [source.targetAgentId] : c.memberIds) this.knowledge.requireProjectMember(owner, projectId, id)
  172. const conversationTask = this.store.resolveTask(owner, conversationId, body.taskId),
  173. conversationTaskId = conversationTask?.id
  174. if (conversationTaskId) {
  175. const alreadyActive = this.store.list<Run>(owner, 'run').some(run => run.conversationTaskId === conversationTaskId && !['complete', 'failed', 'interrupted'].includes(run.status))
  176. if (!alreadyActive && this.store.activeTaskCount(owner, conversationId) >= WORKSPACE_CONCURRENCY_LIMIT)
  177. throw new HttpError(409, `最多同时运行 ${WORKSPACE_CONCURRENCY_LIMIT} 个任务`, 'workspace_task_concurrency_limit')
  178. }
  179. if (body.mentionIds.some((a) => !this.store.taskMemberIds(owner,c,conversationTaskId).includes(a)))
  180. throw new HttpError(400, '只能 @ 群内成员', 'invalid_mentions')
  181. const records = this.files(owner,body.fileIds)
  182. const now = Date.now(),
  183. runId = randomUUID(),
  184. agents = this.store.taskMemberIds(owner,c,conversationTaskId).map((id) => this.store.require<Agent>(owner, 'agent', id))
  185. let goal = conversationTaskId ? this.store.get<import('../shared/agentTasks.js').AgentGoal>(owner, 'goal', conversationTaskId) : undefined
  186. if (body.mode === 'goal') {
  187. if (source || c.kind !== 'group' || !conversationTask || !body.content.trim())
  188. throw new HttpError(400, '请在群聊中说明需要交付的结果', 'goal_request_invalid')
  189. if (goal) throw new HttpError(409, '当前话题已有目标,请继续原目标或新建话题', 'goal_exists')
  190. if (body.coordinatorId) {
  191. if (!c.memberIds.includes(body.coordinatorId)) throw new HttpError(400, '负责人需要是群成员', 'invalid_members')
  192. c.administratorId = body.coordinatorId
  193. }
  194. const coordinator = this.store.require<Agent>(owner, 'agent', c.administratorId)
  195. if (coordinator.temporaryGoalId || coordinator.archived || coordinator.remoteAgentId)
  196. throw new HttpError(403, '负责人需要是本机上未归档的 Bot', 'goal_forbidden')
  197. if (body.coordinatorId) this.store.put(owner, 'conversation', c.id, c)
  198. if (this.store.list<Run>(owner, 'run').some(r => r.conversationTaskId === conversationTaskId && !['complete', 'failed', 'interrupted'].includes(r.status)))
  199. throw new HttpError(409, '请等待当前回复结束后再启动目标', 'goal_busy')
  200. goal = this.tasks.begin(owner, conversationTask, coordinator, body.content.trim(),
  201. { conversationId, conversationTaskId, runId, agentId: coordinator.id })
  202. }
  203. const mentions = [
  204. ...new Set(
  205. [...body.mentionIds, ...mentionedAgents(body.content, agents)],
  206. ),
  207. ]
  208. const message: Message = {
  209. id: randomUUID(),
  210. conversationId,
  211. conversationTaskId,
  212. seq: 0,
  213. role: source ? 'system' : 'user',
  214. peerMessageId: source?.peerMessageId,
  215. ...(source?.taskReference ? {taskReference:source.taskReference} : {}),
  216. ...(source ? { agentId: source.agentId, agentName: this.store.require<Agent>(owner, 'agent', source.agentId).name } : {}),
  217. content: body.content.trim(),
  218. reasoning: '',
  219. runId,
  220. status: 'complete',
  221. attachments: records.map((f) => ({
  222. id: f.id,
  223. name: f.name,
  224. mimeType: f.mimeType,
  225. size: f.size,
  226. sender: f.sender,
  227. createdAt: now,
  228. })),
  229. tools: [],
  230. createdAt: now,
  231. }
  232. this.store.saveMessage(owner, message)
  233. for (const file of records) {
  234. const existing = this.store.get<Record<string, unknown>>(owner, 'file', file.id)
  235. if (existing && !existing.messageId)
  236. this.store.put(owner, 'file', file.id, {
  237. ...existing,
  238. conversationId,
  239. messageId: message.id,
  240. })
  241. }
  242. const run: Run = {
  243. id: runId,
  244. conversationId,
  245. conversationTaskId,
  246. messageId: message.id,
  247. mentionIds: mentions,
  248. activeAgentId: c.administratorId,
  249. status: 'queued',
  250. round: 0,
  251. createdAt: now,
  252. updatedAt: now,
  253. authorizationVersion: this.authorizationVersion(owner),
  254. projectId,
  255. peerMessageId: source?.peerMessageId,
  256. collaborationChainId: source?.collaborationChainId,
  257. priority: source?.priority,
  258. ...(source ? { assignmentId: source.assignmentId, targetAgentId: source.targetAgentId, triggerKind: source.kind, internalInstruction: source.instruction } : {}),
  259. ...(source?.kind !== 'peer' && goal && ['running', 'review', 'waiting'].includes(goal.status) ? { goalId: goal.id, targetAgentId: source?.targetAgentId ?? goal.coordinatorId } : {}),
  260. }
  261. if (isDiscussion(c) && !run.goalId && !run.assignmentId && !source?.targetAgentId) run.discussion = { memberIds: mentions.length ? mentions : [...c.memberIds], rounds: discussionRounds(body.content, body.rounds) }
  262. this.admit(owner, run, c, message)
  263. this.uploads.markReferenced(body.fileIds, owner)
  264. return run
  265. })
  266. this.wake()
  267. return this.store.require<Run>(owner, 'run', result.id)
  268. }
  269. protected async performTurn(
  270. owner: string,
  271. c: Conversation,
  272. agent: Agent,
  273. run: Work,
  274. recovering = false,
  275. ): Promise<Message> {
  276. this.requireAuthorization(owner, run.runId)
  277. this.store.requireAgentTask(owner,agent,c.id,run.conversationTaskId)
  278. this.nodes.requireSource(owner, agent)
  279. if (agent.remoteAgentId) agent = await this.nodes.refreshRemoteAgent(owner,agent)
  280. const key = this.bindingKey(c.id, run.conversationTaskId, agent.id),
  281. dispatchedVm = globalComputers(this.store.home).vm && !!agent.envs && !agent.remoteAgentId && !agent.temporaryGoalId,
  282. computerExecution = !dispatchedVm && !!(agent.remoteAgentId || (agent.execution==='computer' && (!agent.envs || this.computerAvailable?.(owner,agent) !== false))),
  283. chatExecution = dispatchedVm ? 'profile' : computerExecution ? 'computer' : (agent.execution ?? 'profile'),
  284. target = computerExecution ? this.nodes.targetForAgent(owner,agent) : this.nodes.target(owner, agent.nodeId)
  285. let vmTools: VmToolSession | undefined
  286. const gateway = new WorkspaceGateway(target,{workId:run.id,publishArtifact:async(name,bytes)=>{
  287. this.requireAuthorization(owner,run.runId);this.nodes.requireSource(owner,agent)
  288. const file=this.publishArtifact(owner,resultMessage,agent,name,bytes)
  289. if(!resultMessage.attachments.some(item=>item.id===file.id))resultMessage.attachments.push(file)
  290. this.store.saveMessage(owner,resultMessage)
  291. return {...file,url:`/api/app/files/${file.id}/download`}
  292. },authorize:()=>{
  293. this.requireAuthorization(owner,run.runId);this.nodes.requireSource(owner,agent)
  294. const current=this.getWork(owner,run.id),latest=this.store.require<Agent>(owner,'agent',agent.id),conversation=this.store.require<Conversation>(owner,'conversation',c.id)
  295. if(this.closing||conversation.archived||!this.store.taskMemberIds(owner,conversation,run.conversationTaskId).includes(agent.id)||this.store.require<Run>(owner,'run',run.runId).stopRequested||current.cancelRequested||['interrupted','complete','failed'].includes(current.status)||latest.archived||latest.computerEnvironmentId!==agent.computerEnvironmentId||(latest.vmExecution??'worker')!==(agent.vmExecution??'worker')||(latest.allowHostEnvironment===true)!==(agent.allowHostEnvironment===true)||(latest.execution??'profile')!==(agent.execution??'profile')||latest.teamAuthorizationVersion!==agent.teamAuthorizationVersion)throw new HttpError(403,'本轮机器人或任务授权已结束','run_authorization_revoked')
  296. }})
  297. gateway.onTrace=entry=>this.inspector?.record(owner,c.id,{...entry,agentId:agent.id,runId:run.runId,taskId:run.conversationTaskId})
  298. const botCapabilities = await this.botCapabilities(owner, agent, target)
  299. const root = this.store.require<Run>(owner, 'run', run.runId)
  300. if (root.peerMessageId && !recovering) {
  301. const peer = this.store.require<import('../shared/workspaceKnowledge.js').WorkspacePeerMessage>(owner, 'peer-message', root.peerMessageId)
  302. const sender = this.store.require<Agent>(owner, 'agent', peer.fromAgentId)
  303. if (sender.archived || peer.status === 'stopped') throw new HttpError(403, 'Bot 协作权限已撤销', 'collaboration_forbidden')
  304. this.nodes.requireSource(owner, sender)
  305. }
  306. const memory = botCapabilities.memory && !recovering ? await this.knowledge.context(owner, agent.id, root.projectId) : { text: '', version: 'unsupported' }
  307. const memoryStatus = botCapabilities.memory ? 'ready' : 'upgrade_required'
  308. if (agent.memoryStatus !== memoryStatus) {
  309. agent = { ...agent, memoryStatus }
  310. this.store.put(owner, 'agent', agent.id, agent)
  311. this.store.event(owner, 'agent.changed', this.store.agentSummary(agent))
  312. }
  313. let binding = this.store.get<WorkspaceBinding>(owner, 'binding', key)
  314. const memoryChanged = !recovering && !!binding?.memoryVersion && binding.memoryVersion !== memory.version
  315. const movedRunner=memoryChanged||!!this.store.get(owner,'binding-reset',key)||!!binding&&((binding.vmExecution??'worker')!==(agent.vmExecution??'worker')||binding.computerEnvironmentId!==agent.computerEnvironmentId||binding.runnerId!==target.runner?.id||(binding.execution??'profile')!==chatExecution||!!binding.hermesComputer!==!!target.runner?.hermesComputer)
  316. if(movedRunner) {
  317. if(recovering)throw new HttpError(409,'执行节点已变化,不能在另一节点重放原执行','runner_target_changed')
  318. binding=undefined
  319. }
  320. let message =
  321. run.currentMessageId
  322. ? this.store.require<Message>(owner, 'message', run.currentMessageId)
  323. : undefined
  324. if (!message) {
  325. message = {
  326. id: randomUUID(),
  327. conversationId: c.id,
  328. conversationTaskId: run.conversationTaskId,
  329. seq: 0,
  330. role: 'assistant',
  331. execution:chatExecution,
  332. agentId: agent.id,
  333. agentName: agent.name,
  334. content: '',
  335. reasoning: '',
  336. status: 'streaming',
  337. runId: run.runId,
  338. taskId: run.id,
  339. visible: run.replyMode !== 'automatic' || run.requiredReply,
  340. tools: [],
  341. attachments: [],
  342. createdAt: Date.now(),
  343. }
  344. run.currentMessageId = message.id
  345. this.store.atomic(() => {
  346. this.store.saveMessage(owner, message!)
  347. this.saveWork(owner, run)
  348. })
  349. }
  350. if(!recovering&&message.status==='queued'){message.status='streaming';message.error=undefined;message.visible=run.replyMode!=='automatic'||run.requiredReply}
  351. const resultMessage = message
  352. let submitted = recovering,
  353. settled = false,
  354. completedEvidence = false,
  355. completing = false,
  356. runtimeId = '',
  357. lastFlush = 0
  358. let flushTimer: ReturnType<typeof setTimeout> | undefined
  359. let usageCompletion: Promise<void> | undefined
  360. const toolController = new AbortController()
  361. let toolLease: WorkspaceToolLease | undefined
  362. let pluginLease: OpenBotPlugins | undefined
  363. let resolveTurn!: (m: Message) => void, rejectTurn!: (e: Error) => void
  364. const completion = new Promise<Message>((resolve, reject) => {
  365. resolveTurn = resolve
  366. rejectTurn = reject
  367. })
  368. // Register rejection immediately; setup RPCs can still be awaiting a reply.
  369. void completion.catch(() => {})
  370. const finish = (error?: Error) => {
  371. if (settled) return
  372. settled = true
  373. toolController.abort()
  374. void toolLease?.dispose()
  375. void pluginLease?.dispose()
  376. clearTimeout(flushTimer)
  377. flushTimer = undefined
  378. this.live.delete(key)
  379. try {
  380. const current = this.getWork(owner, run.id)
  381. if (current.cancelRequested && current.status === 'interrupted') {
  382. resultMessage.status = 'interrupted'
  383. resultMessage.error = '已停止'
  384. this.store.atomic(() => {
  385. this.store.saveMessage(owner, resultMessage)
  386. this.saveWork(owner, current)
  387. this.resolveInteractions(owner, current.id)
  388. })
  389. if (error) rejectTurn(error)
  390. else resolveTurn(resultMessage)
  391. return
  392. }
  393. if(error&&!submitted&&current.status!=='interrupted'&&error instanceof HttpError&&RESOURCE_WAIT_CODES.has(error.code??'')){
  394. current.status='queued';current.resourceWait=true;current.error='等待执行节点或电脑资源';resultMessage.status='queued';resultMessage.visible=false;resultMessage.error=undefined
  395. }else if (error) {
  396. current.status = current.status === 'interrupted' ? 'interrupted' : submitted ? 'uncertain' : 'failed'
  397. current.error = error.message.slice(0, 1000)
  398. resultMessage.status = current.status
  399. resultMessage.error = current.error
  400. const optionalUnpublished = current.replyMode === 'automatic' && !current.requiredReply && resultMessage.visible === false
  401. resultMessage.visible = !optionalUnpublished && (submitted || current.status !== 'interrupted')
  402. if (!resultMessage.content.trim() && current.status === 'failed') resultMessage.content = `执行失败:${current.error}`
  403. } else {
  404. if (root.discussion && /^(?:\(?pass\)?\.?|\[\[YAOYAO_NO_REPLY_V1\]\])$/i.test(resultMessage.content.trim())) resultMessage.content = ''
  405. const silent = (resultMessage.content.trim() === NO_REPLY || !resultMessage.content.trim()) && current.replyMode === 'automatic' && (!current.requiredReply || current.hadInteraction) && !resultMessage.tools.length && !resultMessage.attachments.length
  406. current.silent = silent
  407. if (current.replyMode === 'automatic') {
  408. resultMessage.content = resultMessage.content.replace(/\[\[YAOYAO_[A-Z0-9_]*(?:\]\])?/g, '').trim()
  409. resultMessage.reasoning = resultMessage.reasoning.replace(/\[\[YAOYAO_[A-Z0-9_]*(?:\]\])?/g, '')
  410. }
  411. if (current.requiredReply && !silent && !resultMessage.content) resultMessage.content = HOST_FALLBACK
  412. if (!silent && !resultMessage.content && !resultMessage.attachments.length && !resultMessage.tools.length) {
  413. current.status = 'failed'; current.error = '未返回有效回复'; resultMessage.content = '执行失败:未返回有效回复'
  414. } else { current.status = 'complete'; current.error = undefined }
  415. resultMessage.visible = !silent
  416. if (silent) resultMessage.reasoning = ''
  417. resultMessage.status = current.status
  418. resultMessage.error = current.error
  419. }
  420. if (current.status !== 'uncertain') {
  421. for (const tool of resultMessage.tools) {
  422. if (!/complete|error|failed/.test(String(tool.status))) tool.status = current.status === 'complete' ? 'tool.complete' : 'tool.error'
  423. }
  424. }
  425. this.store.atomic(() => {
  426. this.store.saveMessage(owner, resultMessage)
  427. this.saveWork(owner, current)
  428. if (current.status !== 'uncertain') this.resolveInteractions(owner, current.id)
  429. })
  430. if (error) rejectTurn(error)
  431. else resolveTurn(resultMessage)
  432. } catch (failure) { rejectTurn(failure instanceof Error ? failure : new Error('无法保存执行状态')) }
  433. finally { vmTools?.close(); if (usageCompletion) void usageCompletion.finally(() => gateway.close()); else gateway.close() }
  434. }
  435. gateway.onDisconnect = () =>
  436. finish(completedEvidence ? undefined : new Error('Hermes 连接断开'))
  437. gateway.onEvent = (frame: GatewayFrame) => {
  438. if (settled || !runtimeId || frame.session_id !== runtimeId) return
  439. const currentTurn = this.getWork(owner, run.id)
  440. if (currentTurn.cancelRequested || currentTurn.status === 'interrupted') return
  441. this.inspector?.record(owner,c.id,{direction:'event',method:frame.type,data:frame.payload,agentId:agent.id,runId:run.runId,taskId:run.conversationTaskId})
  442. const p = frame.payload ?? {},
  443. type = frame.type
  444. const firstContent = type === 'message.delta' && !resultMessage.content.trim()
  445. if(type==='computer.paused'||type==='computer.resumed'){const current=this.getWork(owner,run.id);current.status=type==='computer.paused'?'waiting':'running';this.saveWork(owner,current);return}
  446. if (type === 'message.delta') {
  447. resultMessage.content += String(p.text ?? p.delta ?? '')
  448. if (resultMessage.content.trim() && !NO_REPLY.startsWith(resultMessage.content.trim()) && !resultMessage.content.trim().startsWith('[[YAOYAO_')) resultMessage.visible = true
  449. }
  450. else if (type === 'reasoning.delta')
  451. resultMessage.reasoning += String(p.text ?? p.delta ?? '')
  452. else if (type === 'message.interim') {
  453. resultMessage.content += '\n\n'
  454. } else if (type.startsWith('tool.')) {
  455. resultMessage.visible = true
  456. const id = String(p.tool_id ?? p.id ?? ''),
  457. index = resultMessage.tools.findIndex((t) => t.id === id)
  458. const value = { ...p, id, status: type }
  459. if (index >= 0) resultMessage.tools[index] = { ...resultMessage.tools[index], ...value }
  460. else resultMessage.tools.push(value)
  461. } else if (
  462. ['approval.request', 'approval.requested', 'clarify.request', 'clarify.requested'].includes(
  463. type,
  464. )
  465. ) {
  466. const upstreamId = String(p.request_id ?? p.requestId ?? p.id ?? '')
  467. const previous = this.store.list<WorkspaceInteraction>(owner, 'interaction').find(i => {
  468. const b = this.store.get<{ taskId: string; upstreamId: string }>(owner, 'interaction-binding', i.id)
  469. return b?.taskId === run.id && b.upstreamId === upstreamId
  470. })
  471. if (previous?.resolved) return
  472. const interaction: WorkspaceInteraction = previous ?? {
  473. id: randomUUID(),
  474. conversationId: c.id,
  475. conversationTaskId: run.conversationTaskId,
  476. runId: run.runId,
  477. agentId: agent.id,
  478. kind: type.startsWith('approval') ? 'approval' : 'clarification',
  479. message: String(p.question ?? p.message ?? p.prompt ?? '需要确认'),
  480. choices: Array.isArray(p.choices) ? p.choices.map(String) : [],
  481. resolved: false,
  482. }
  483. this.store.put(owner, 'interaction', interaction.id, interaction)
  484. this.store.put(owner, 'interaction-binding', interaction.id, { key, upstreamId, taskId: run.id, conversationTaskId: run.conversationTaskId })
  485. this.store.event(owner, 'interaction.changed', interaction, c.id)
  486. const current = this.getWork(owner, run.id)
  487. current.hadInteraction = true
  488. current.status = 'waiting'
  489. this.saveWork(owner, current)
  490. const policy = this.store.require<Agent>(owner, 'agent', agent.id).approvalPolicy ?? 'ask'
  491. if (interaction.kind === 'approval' && policy !== 'ask' && !previous) {
  492. // Use the same ownership, authorization and response deduplication as
  493. // a manual answer. Never grant session-wide or permanent approval.
  494. void this.respond(owner, interaction.id, policy === 'allow' ? 'once' : 'deny').catch(() => {
  495. const latest = this.store.get<WorkspaceInteraction>(owner, 'interaction', interaction.id)
  496. if (latest && !latest.resolved) this.notify(owner, c, this.store.require<Run>(owner, 'run', run.runId), undefined, latest)
  497. })
  498. } else if (!previous) this.notify(owner, c, this.store.require<Run>(owner, 'run', run.runId), undefined, interaction)
  499. } else if(type==='interaction.cancelled'){
  500. const upstreamId=String(p.request_id??'')
  501. for(const interaction of this.store.list<WorkspaceInteraction>(owner,'interaction')){
  502. const binding=this.store.get<{taskId:string;upstreamId:string}>(owner,'interaction-binding',interaction.id)
  503. if(!interaction.resolved&&binding?.taskId===run.id&&binding.upstreamId===upstreamId){
  504. interaction.resolved=true;this.store.put(owner,'interaction',interaction.id,interaction)
  505. this.store.event(owner,'interaction.changed',interaction,c.id)
  506. }
  507. }
  508. const current=this.getWork(owner,run.id)
  509. if(current.status==='waiting'&&!this.store.list<WorkspaceInteraction>(owner,'interaction').some(i=>i.runId===run.runId&&i.agentId===agent.id&&!i.resolved)){
  510. current.status='running';this.saveWork(owner,current)
  511. }
  512. } else if (['message.complete', 'run.completed'].includes(type)) {
  513. if (typeof p.text === 'string') resultMessage.content = p.text
  514. if (typeof p.reasoning === 'string') resultMessage.reasoning = p.reasoning
  515. if (p.status === 'interrupted') {
  516. const current = this.getWork(owner, run.id)
  517. current.status = 'interrupted'
  518. this.saveWork(owner, current)
  519. finish(new Error('已停止'))
  520. } else if (p.error || p.status === 'failed') {
  521. submitted = false
  522. finish(new HttpError(502, String(p.error ?? '运行失败'), 'run_failed'))
  523. } else if (!completing) {
  524. completedEvidence = true
  525. completing = true
  526. let timeout: ReturnType<typeof setTimeout>
  527. usageCompletion = Promise.race([
  528. gateway.rpc('session.usage', { session_id: runtimeId }),
  529. new Promise<undefined>((resolve) => {
  530. timeout = setTimeout(() => resolve(undefined), 1000)
  531. }),
  532. ])
  533. .then((usage) => {
  534. if (usage && !this.closing && this.store.get<{ taskId: string }>(owner, 'binding', key)?.taskId === run.id) {
  535. this.recordAgentUsage(owner, key, agent.id, usage)
  536. const used = usage.context_used ?? usage.used_tokens,
  537. limit = usage.context_max ?? usage.context_limit
  538. const contextKey = run.conversationTaskId ?? c.id,
  539. context = {
  540. ...usage,
  541. conversationTaskId: run.conversationTaskId,
  542. usedTokens: used,
  543. limitTokens: limit,
  544. percent:
  545. typeof used === 'number' && typeof limit === 'number' && limit > 0
  546. ? Math.round((used / limit) * 1000) / 10
  547. : undefined,
  548. }
  549. this.store.put(owner, 'context', contextKey, context)
  550. this.store.event(
  551. owner,
  552. 'context.changed',
  553. context,
  554. c.id,
  555. )
  556. }
  557. })
  558. .catch(() => {})
  559. .finally(() => {
  560. clearTimeout(timeout)
  561. })
  562. finish()
  563. }
  564. return
  565. } else if (['error', 'message.error', 'run.failed'].includes(type)) {
  566. submitted = false
  567. finish(new HttpError(502, String(p.message ?? p.error ?? '运行失败'), 'run_failed'))
  568. return
  569. } else if (['session.usage', 'usage.update', 'context.update'].includes(type)) {
  570. if (type !== 'context.update') this.recordAgentUsage(owner, key, agent.id, p)
  571. const context = { ...p, conversationTaskId: run.conversationTaskId }
  572. this.store.put(owner, 'context', run.conversationTaskId ?? c.id, context)
  573. this.store.event(owner, 'context.changed', context, c.id)
  574. } else if (type === 'session.info' && binding) {
  575. const storedId = p.stored_session_id ?? p.session_key
  576. if (typeof storedId === 'string' && storedId && storedId !== binding.storedId) {
  577. binding.aliases = [...new Set([...binding.aliases, binding.storedId, storedId])]
  578. binding.storedId = storedId
  579. this.store.put(owner, 'binding', key, binding)
  580. }
  581. }
  582. const flush = () => {
  583. clearTimeout(flushTimer)
  584. flushTimer = undefined
  585. if (settled || this.closing) return
  586. try {
  587. this.store.saveMessage(owner, resultMessage)
  588. lastFlush = Date.now()
  589. } catch (error) {
  590. finish(error instanceof Error ? error : new Error('无法保存流式消息'))
  591. }
  592. }
  593. const remaining = (this.store.messagePatchesEnabled ? 33 : 100) - (Date.now() - lastFlush)
  594. if (firstContent || !['message.delta', 'reasoning.delta', 'message.interim'].includes(type) || remaining <= 0) flush()
  595. // A stream may pause after any chunk. Publish the latest text even when
  596. // no subsequent event arrives to trigger the next leading-edge flush.
  597. else if (flushTimer === undefined) flushTimer = setTimeout(flush, remaining)
  598. }
  599. try {
  600. await gateway.connect()
  601. this.requireAuthorization(owner, run.runId)
  602. this.nodes.requireSource(owner, agent)
  603. // Paired Web nodes enforce their own profile config at the destination.
  604. const cwd = recovering || target.pairedToken ? undefined
  605. : await configuredWorkingDirectory((path, options) => target.session.request(path, options), agent.profile)
  606. this.nodes.requireSource(owner, agent)
  607. // Hermes rejects disabled_toolsets on session.create. Host-tool denial
  608. // stays a YaoYao policy; it is not a session-create parameter.
  609. const createSession = () => gateway.rpc('session.create', {
  610. profile: agent.profile,
  611. title: `Yaoyao ${c.id}${run.conversationTaskId ? ` / ${run.conversationTaskId}` : ''}`,
  612. source: 'yaoyao_workspace',
  613. ...(cwd ? { cwd } : {}),
  614. hidden: true,
  615. room_plumbing: true,
  616. close_on_disconnect: false,
  617. })
  618. let opened
  619. if (binding?.storedId) {
  620. try {
  621. opened = await gateway.rpc('session.resume', {
  622. profile: agent.profile,
  623. session_id: binding.storedId,
  624. omit_messages: true,
  625. ...(recovering&&target.runner?.computer?{recoverOnly:true}:{}),
  626. close_on_disconnect: false,
  627. })
  628. } catch (error) {
  629. const code = error instanceof HttpError ? error.code : ''
  630. const message = error instanceof Error ? error.message : ''
  631. if (recovering || (code !== 'computer_session_mode_changed' && !message.includes('执行方式已改变'))) throw error
  632. binding = undefined
  633. opened = await createSession()
  634. }
  635. } else opened = await createSession()
  636. runtimeId = String(opened.session_id ?? '')
  637. const storedId = String(
  638. opened.stored_session_id ?? opened.session_key ?? opened.resumed ?? binding?.storedId ?? '',
  639. )
  640. if (
  641. !runtimeId ||
  642. !storedId ||
  643. (opened.info?.profile_name && opened.info.profile_name !== agent.profile)
  644. )
  645. throw new Error('Hermes 会话身份不匹配')
  646. binding = {
  647. memoryVersion: recovering ? binding?.memoryVersion : memory.version,
  648. runnerId: target.runner?.id,
  649. execution:chatExecution,
  650. vmExecution:computerExecution?agent.vmExecution:undefined,
  651. hermesComputer:target.runner?.hermesComputer,
  652. computerEnvironmentId:agent.computerEnvironmentId,
  653. id: key,
  654. nodeId: agent.nodeId,
  655. remoteAgentId: agent.remoteAgentId,
  656. profile: agent.profile,
  657. storedId,
  658. runtimeId,
  659. aliases: [
  660. ...new Set([
  661. ...(binding?.aliases ?? []),
  662. ...(binding ? [binding.storedId] : []),
  663. storedId,
  664. ]),
  665. ],
  666. runId: run.runId,
  667. conversationTaskId: run.conversationTaskId,
  668. taskId: run.id,
  669. contextSeq: binding?.contextSeq ?? 0,
  670. messageId: resultMessage.id,
  671. }
  672. this.store.put(owner, 'binding', key, binding)
  673. this.live.set(key, { gateway, runtimeId, runId: run.runId, conversationTaskId: run.conversationTaskId, taskId: run.id, agentId: agent.id, done: finish })
  674. if (this.closing || this.getWork(owner, run.id).cancelRequested || this.getWork(owner, run.id).status === 'interrupted')
  675. throw new Error('运行已停止')
  676. if (recovering) {
  677. if (opened.running) {
  678. const current = this.getWork(owner, run.id)
  679. current.status = 'running'
  680. this.saveWork(owner, current)
  681. } else {
  682. const marker = `[yaoyao-run:${run.runId}:${resultMessage.id}]`
  683. let answer: { content?: string; text?: string } | undefined
  684. let found = false, seenLastEvent = false
  685. for (let page = 0; page < 20 && !found; page++) {
  686. const response = await target.session.request(`/api/sessions/${encodeURIComponent(storedId)}/messages`, {
  687. search: new URLSearchParams({ profile: agent.profile, limit: '500', offset: String(page * 500), order: 'latest', include_compacted: 'true' }),
  688. maxResponseBytes: 8 * 1024 * 1024,
  689. })
  690. if (response.status !== 200) throw new Error('无法核对历史')
  691. const history = JSON.parse(response.body.toString()).messages
  692. if (!Array.isArray(history)) throw new Error('历史响应无效')
  693. for (const item of [...history].reverse()) {
  694. if (item.role === 'user') {
  695. if (String(item.content ?? item.text).includes(marker)) { found = true; break }
  696. answer = undefined; seenLastEvent = false
  697. } else if (!seenLastEvent && ['assistant', 'tool'].includes(item.role)) {
  698. seenLastEvent = true
  699. const calls = item.tool_calls
  700. const hasCalls = Array.isArray(calls) ? calls.length > 0 : !!calls && calls !== '[]'
  701. if (item.role === 'assistant' && !hasCalls && !item.function_call && item.finish_reason !== 'tool_calls' && (item.content || item.text)) answer = item
  702. }
  703. }
  704. if (history.length < 500) break
  705. }
  706. if (!found || !answer) throw new Error('无法确认本轮的最终回复,正在核对原执行;不会重复提交')
  707. resultMessage.content = String(answer.content ?? answer.text)
  708. binding.contextSeq = Math.max(binding.contextSeq ?? 0, run.contextThroughSeq ?? 0)
  709. this.store.put(owner, 'binding', key, binding)
  710. finish()
  711. }
  712. } else {
  713. if (opened.running) throw new HttpError(409, '上游会话仍在运行', 'session_busy')
  714. this.nodes.requireSource(owner, agent)
  715. const team=botCapabilities.tools&&!agent.temporaryGoalId&&!agent.remoteAgentId&&!this.store.require<Run>(owner,'run',run.runId).assignmentId
  716. const cloud=!!this.cloud?.selected(owner,agent)
  717. const plugins=!!this.plugins?.selected(owner,agent)
  718. const envTools=this.desktopEnvironments?.envTools(owner,agent)??{view:false,browser:false,file:false}
  719. const desktop=envTools.view||envTools.browser
  720. const desktopEpochs=this.desktopEnvironments?.hostEpochs(owner,agent)??{}
  721. const knowledge = botCapabilities.tools && !agent.remoteAgentId
  722. const vmHolder:{session?:VmToolSession}={}
  723. const callVm=async(name:string,args:unknown)=>{
  724. if(!vmHolder.session){
  725. const vmTarget=this.nodes.targetForAgent(owner,{...agent,execution:'computer'})
  726. vmHolder.session=new VmToolSession(vmTarget,agent.profile,run.id,()=>{
  727. this.requireAuthorization(owner,run.runId);this.nodes.requireSource(owner,agent)
  728. const current=this.getWork(owner,run.id),latest=this.store.require<Agent>(owner,'agent',agent.id),conversation=this.store.require<Conversation>(owner,'conversation',c.id)
  729. if(this.closing||conversation.archived||!this.store.taskMemberIds(owner,conversation,run.conversationTaskId).includes(agent.id)||this.store.require<Run>(owner,'run',run.runId).stopRequested||current.cancelRequested||['interrupted','complete','failed'].includes(current.status)||latest.archived||latest.computerEnvironmentId!==agent.computerEnvironmentId)throw new HttpError(403,'本轮机器人或任务授权已结束','run_authorization_revoked')
  730. },async(name,bytes)=>{
  731. this.requireAuthorization(owner,run.runId)
  732. const file=this.publishArtifact(owner,resultMessage,agent,name,bytes)
  733. if(!resultMessage.attachments.some(item=>item.id===file.id))resultMessage.attachments.push(file)
  734. this.store.saveMessage(owner,resultMessage)
  735. return {...file,url:`/api/app/files/${file.id}/download`}
  736. })
  737. vmTools=vmHolder.session
  738. }
  739. return vmHolder.session.call(name,args)
  740. }
  741. if(team||cloud||desktop||plugins||knowledge||dispatchedVm){
  742. if(team){const granted=this.getWork(owner,run.id);granted.teamManagementRevision=agent.revision;this.saveWork(owner,granted)}
  743. if(desktop)await this.desktopEnvironments!.requireAvailable(owner,agent,target)
  744. if(cloud)await this.cloud!.requireAvailable(owner,agent,target)
  745. const assertActive=()=>{
  746. if(settled||this.closing||toolController.signal.aborted)throw new HttpError(410,'本轮运行已结束,请发起新一轮对话后重试。','run_finished')
  747. this.requireAuthorization(owner,run.runId);this.nodes.requireSource(owner,this.store.require<Agent>(owner,'agent',agent.id))
  748. if(team)this.teamTools.assertTurn(owner,run.id)
  749. if(plugins){const current=this.getWork(owner,run.id),latest=this.store.require<Agent>(owner,'agent',agent.id);if(current.cancelRequested||this.store.require<Run>(owner,'run',run.runId).stopRequested||latest.archived||!['running','waiting'].includes(current.status))throw new HttpError(403,'本轮插件授权已结束','plugin_grant_revoked')}
  750. }
  751. if(plugins)pluginLease=await this.plugins!.open(owner,agent,target,toolController.signal,assertActive)
  752. toolLease=await createWorkspaceToolLease({
  753. target,profile:agent.profile,workId:run.id,signal:toolController.signal,
  754. workspaceMemory:botCapabilities.memory,
  755. session:()=>({runtimeId,storedId:binding!.storedId}),assertActive,
  756. catalog:()=>[...(team?this.teamTools.catalog(owner,run.id):[]),...(knowledge?this.knowledgeTools.catalog(owner,run.id,botCapabilities.memory):[]),...(cloud?GROK_COMPUTER_TOOLS:[]),...(dispatchedVm?VM_COMPUTER_TOOLS:[]),...(desktop?DESKTOP_ENVIRONMENT_TOOLS.filter(t=>(envTools.view&&['desktop_environment_view','desktop_environment_action'].includes(t.id))||(envTools.file&&DESKTOP_FILE_TOOL_IDS.has(t.id))):[]),...(pluginLease?.catalog()??[])],
  757. call:async(toolId,args)=>{assertActive();return knowledge&&this.knowledgeTools.handles(toolId)?this.knowledgeTools.call(owner,run.id,toolId,args,botCapabilities.memory):toolId.startsWith('plugin_')&&pluginLease?pluginLease.call(toolId,args):toolId.startsWith('desktop_')?this.desktopEnvironments!.call(owner,agent.id,toolId,args,toolController.signal,assertActive,'local',desktopEpochs):toolId.startsWith('cloud_computer_')?this.cloud!.call(owner,agent.id,toolId,args,toolController.signal):toolId.startsWith('computer_')&&dispatchedVm?callVm(toolId,args):this.teamTools.call(owner,run.id,toolId,args)},
  758. onFailure:error=>{if(!settled)void gateway.rpc('session.interrupt',{session_id:runtimeId}).catch(()=>{}).finally(()=>finish(error))},
  759. })
  760. await toolLease.bind();if(settled)return await completion
  761. }
  762. await applyWorkingDirectory((method, params) => gateway.rpc(method, params), runtimeId, cwd, undefined, opened.info?.cwd)
  763. const trigger = this.store.require<Message>(owner, 'message', run.messageId)
  764. const attachmentRefs: string[] = []
  765. for (const file of trigger.attachments) {
  766. this.requireAuthorization(owner, run.runId)
  767. const record = this.files(owner,[file.id])[0]!
  768. const bytes = readFileSync(record.path).toString('base64')
  769. const attached = record.mimeType.startsWith('image/')
  770. ? await gateway.rpc('image.attach_bytes', {
  771. session_id: runtimeId,
  772. filename: record.name,
  773. content_base64: bytes,
  774. })
  775. : await gateway.rpc('file.attach', {
  776. session_id: runtimeId,
  777. name: record.name,
  778. data_url: `data:${record.mimeType};base64,${bytes}`,
  779. })
  780. if (attached.ref_text) attachmentRefs.push(String(attached.ref_text))
  781. }
  782. const members = run.turnConfiguration!.members
  783. const goal = run.conversationTaskId ? this.store.get<import('../shared/agentTasks.js').AgentGoal>(owner, 'goal', run.conversationTaskId) : undefined
  784. const assignmentId = this.store.require<Run>(owner, 'run', run.runId).assignmentId
  785. const globals = globalComputers(this.store.home)
  786. const eligible = !agent.remoteAgentId && !agent.temporaryGoalId && !agent.archived
  787. const open = [eligible && (globals.scriptMachine || globals.serverComputer) ? '脚本机' : '', eligible && globals.vm && agent.envs ? '虚拟环境' : '', eligible && globals.cloud ? '云虚拟机' : ''].filter(Boolean)
  788. const envDirective = `同一条对话里,只使用 Bot 设置里全局开放的环境:${open.join('、') || '无'}。脚本机里,本机是夭夭正在运行的这台电脑,省略 host 或写「本机」就看它,其他脚本机按名称点名。虚拟环境是服务端上的隔离虚拟机,云虚拟机是 Grok Bot 虚拟机。没开放的不要用。${deniedToolsets.length ? '服务端本机的终端和文件已全局禁用,不要用服务端本机顶上。' : '全局设置已允许使用服务端本机的终端和文件。'}`
  789. const rules = [
  790. envDirective,
  791. `你是 ${agent.name}。${agent.description ? `描述:${agent.description}\n\n` : ''}${personaSection(agent) ? `用户为它设定的工作规范:\n${personaSection(agent)}\n\n` : ''}以下是用户为这个独立机器人配置的角色与规则(版本 ${agent.revision}):\n${agent.remoteAgentId ? '配置由远端机器人管理。' : agent.instructions}`,
  792. c.kind === 'group' && Object.keys(c.memberRoles ?? {}).length
  793. ? `本群角色分工(仅在本群生效):\n${members.flatMap(member => {
  794. const role = c.memberRoles?.[member.id]
  795. return role ? [`@${member.name}:${role.name};${role.description}`] : []
  796. }).join('\n')}\n协作时使用上面的真实成员名称进行 @,不要使用职责名称代替成员名称。`
  797. : '',
  798. this.store.require<Run>(owner, 'run', run.runId).discussion
  799. ? `你正在群聊「${c.name}」平等讨论,第 ${run.depth + 1} 轮。你只代表自己,必须给出一条公开、有实质内容的回答,保留用户原始要求。不要代替其他成员发言,不要用私信或 @ 再次派发本轮工作。系统会安排其他成员。`
  800. : c.kind === 'group'
  801. ? `你正在群聊「${c.name}」发言。群成员:${members.map((a) => `@${a.name} (id=${a.id})`).join('、')}。\n群规则:${c.instructions}\n${c.mode === 'host' ? (agent.id === c.administratorId ? '你是管理员。必要时用精确 @成员名称 委派工作;收到结果后复核并给用户结论。任务完成时不要继续 @。' : '执行当前委派任务。公开给出结果,由管理员复核;不要安排其他成员。') : '按自己的职责回复,只在需要协作时 @成员。不要重复已完成的工作。'}`
  802. : '',
  803. c.kind === 'group' ? '只有安排具体的新工作时才用 @成员派工,并写明需要执行的动作。收到、感谢、审核通过、等待用户指令等确认不需要再次 @。任务收尾直接向用户报告结果;不要重复确认或把同一结果反复交回其他成员。' : '',
  804. run.requiredReply ? '你必须公开处理本次消息,直接回答、委派或澄清;禁止静默。管理员可按依赖一次 @一人,也可同时 @多人并行执行,整批结束后系统统一交回复核。' : run.replyMode === 'automatic' ? `你按自动参与配置收到消息。若与职责无关或仅是已完成工作的重复确认,禁止调用工具、禁止 @,完整答复只能是 ${NO_REPLY}。有新工作或新结果时正常回答。` : '',
  805. `本轮用户指定成员:${this.store.require<Run>(owner, 'run', run.runId).mentionIds.map(id => members.find(a => a.id === id)).filter(Boolean).map(a => '@' + a!.name).join('、') || '未指定'}`,
  806. '角色规则不赋予额外工具权限;仍遵守基础 Hermes 的工具和安全约束。',
  807. this.store.require<Run>(owner, 'run', run.runId).internalInstruction || '',
  808. goal && ['running', 'review', 'waiting'].includes(goal.status) ? `当前团队目标 ID:${goal.id}。目标:${goal.objective.slice(0,8000)}\n验收要求(版本 ${goal.acceptanceRevision ?? 1}):${goal.acceptanceCriteria.join(';')}\n${assignmentId ? `你在执行子任务 ${assignmentId},请完成分派并提交结果,不要扩大团队或再次委派。` : '先从用户要求提炼少量具体、可核对的交付条件;若仍是默认验收要求,使用 workspace_update_team_goal 保存。尊重用户调整后的要求。能直接完成就直接完成,不必创建子任务;仅确实需要分工时使用 workspace_assign_task,成员结果通过 workspace_review_assignment 复核,不要再用 @ 重复派发同一工作。最终使用 workspace_finish_team_task 记录完成、受阻或等待用户,完成时提供实际依据。'}` : '',
  809. agent.temporaryGoalId ? `你是当前任务的临时助手,任务 ID:${agent.temporaryGoalId}。仅处理分派工作,使用 computer_export 回传产物。任务结束后会退役;不要创建团队或改变自身权限。` : '',
  810. team ? TEAM_TOOL_RULES : '',
  811. knowledge ? BOT_KNOWLEDGE_RULES : '',
  812. memory.text,
  813. root.projectId ? `当前项目 ID:${root.projectId}。只能使用当前项目记忆。` : '',
  814. `本次来源消息 ID:${run.messageId};当前会话 ID:${c.id}。`,
  815. plugins ? '本轮已挂载用户为当前 Bot 授权的插件工具,工具名以 plugin_ 开头,说明中包含实际服务和操作。仅按用户当前任务使用;连接或重新授权应用请让用户打开 Bot 模式的工具 → 已连接应用。不要索取 API Key 或在回复中展示凭据。' : '',
  816. desktop ? DESKTOP_ENVIRONMENT_RULES+(this.desktopEnvironments?.onlineHostsLine(owner)??'') : '',
  817. cloud ? grokComputerRules() : '',
  818. dispatchedVm ? VM_COMPUTER_RULES : '',
  819. `[yaoyao-run:${run.runId}:${resultMessage.id}]`,
  820. ]
  821. .filter(Boolean)
  822. .join('\n\n')
  823. const text = c.kind === 'group' ? this.contextText(owner, c, agent, run, binding.contextSeq ?? 0, movedRunner)
  824. : movedRunner ? `执行节点已切换。以下是原会话的近期记录,旧路径和执行状态需要在当前节点重新核实;不要重新执行已完成的操作。\n\n${this.contextText(owner,c,agent,run,0,true)}` : trigger.content
  825. const admission = this.getWork(owner, run.id)
  826. if (!this.store.taskMemberIds(owner,this.store.require<Conversation>(owner,'conversation',c.id),run.conversationTaskId).includes(agent.id)) {
  827. admission.status = 'interrupted'; admission.error = '执行前成员已移除'; this.saveWork(owner, admission)
  828. throw new Error('执行前成员已移除')
  829. }
  830. this.nodes.requireSource(owner, agent)
  831. this.requireAuthorization(owner, run.runId)
  832. if (admission.cancelRequested || admission.status === 'interrupted') throw new Error('运行已停止')
  833. admission.contextThroughSeq = run.contextThroughSeq ?? run.triggerSeq
  834. admission.submitted = true
  835. this.saveWork(owner, admission)
  836. submitted = true
  837. try {
  838. await gateway.rpc('prompt.submit', {
  839. session_id: runtimeId,
  840. ...(target.runner?.computer?{workMarker:`[yaoyao-run:${run.runId}:${resultMessage.id}]`}:{}),
  841. text: `${rules}\n\n${text}\n${attachmentRefs.join('\n')}`,
  842. })
  843. this.store.remove(owner,'binding-reset',key)
  844. binding.contextSeq = Math.max(binding.contextSeq ?? 0, admission.contextThroughSeq)
  845. this.store.put(owner, 'binding', key, binding)
  846. } catch (error) {
  847. // A JSON-RPC error is a definitive rejection, not a lost receipt.
  848. if (error instanceof HttpError && ['gateway_rejected','runner_command_not_admitted'].includes(error.code??'')) { submitted = false; const current = this.getWork(owner, run.id); current.submitted = false; this.saveWork(owner, current) }
  849. throw error
  850. }
  851. }
  852. return await completion
  853. } catch (error) {
  854. finish(error instanceof Error ? error : new Error('执行失败'))
  855. return await completion
  856. }
  857. }
  858. private requireAuthorization(owner: string, runId: string): void {
  859. const run=this.store.require<Run>(owner,'run',runId)
  860. if(!this.userActive(owner)||(run.authorizationVersion!==undefined&&run.authorizationVersion!==this.authorizationVersion(owner)))
  861. throw new HttpError(403,'本轮账号授权已失效','run_authorization_revoked')
  862. }
  863. private contextText(owner: string, c: Conversation, agent: Agent, work: Work, after: number, includeOwn = false): string {
  864. const rootTrigger = this.store.require<Message>(owner, 'message', work.messageId)
  865. const roots = new Map(this.store.list<Run>(owner, 'run').filter(r => r.conversationId === c.id && r.conversationTaskId === work.conversationTaskId).map(r => [r.id, this.store.get<Message>(owner, 'message', r.messageId)?.seq ?? 0]))
  866. const history = this.store.messages(owner, c.id, Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, true, work.conversationTaskId)
  867. const unfinished = history.find(m => m.seq > after && m.seq <= work.triggerSeq && (['queued', 'streaming', 'uncertain'].includes(m.status) || (m.runId && (roots.get(m.runId) ?? 0) > rootTrigger.seq)))
  868. work.contextThroughSeq = unfinished ? unfinished.seq - 1 : work.triggerSeq
  869. const eligible = history
  870. .filter(m => m.seq > after && m.seq <= work.triggerSeq && m.visible !== false && ['complete', 'failed', 'interrupted'].includes(m.status)
  871. && (includeOwn || !(m.role === 'assistant' && m.agentId === agent.id)) && (!m.runId || (roots.get(m.runId) ?? 0) <= rootTrigger.seq))
  872. const selected: string[] = []
  873. let size = 0, omitted = 0
  874. for (const message of [...eligible].reverse()) {
  875. const files = message.attachments.map(f => `[附件 ${f.name}](${f.sourcePath || `/api/app/files/${f.id}/download`})`).join('\n')
  876. const content = message.content.length > 24_000 ? message.content.slice(0, 12_000) + '\n[内容过长,保留首尾片段]\n' + message.content.slice(-12_000) : message.content
  877. const reasoning = message.reasoning ? `\n思考摘要:${message.reasoning.slice(0, 2000)}` : ''
  878. const tools = [...new Set(message.tools.map(t => String(t.name ?? t.tool_name ?? '')).filter(name => /^[A-Za-z0-9][A-Za-z0-9_.-]{0,119}$/.test(name)))].slice(0, 16)
  879. const row = `${message.role === 'user' ? '用户' : message.agentName ?? '系统'}(${message.status}):${content}${reasoning}${tools.length ? `\n使用工具:${tools.join('、')}` : ''}${message.error ? `\n错误:${message.error}` : ''}${files ? '\n' + files : ''}`
  880. if (selected.length >= 50 || size + row.length > 30_000) { omitted++; continue }
  881. selected.unshift(row); size += row.length
  882. }
  883. // Preserve a large trigger as a bounded excerpt rather than silently losing it.
  884. if (!selected.length && eligible.length) selected.push(eligible.at(-1)!.content.slice(-30_000))
  885. const batch = work.reviewOf ? this.works(owner, work.runId).filter(t => t.batchId === work.reviewOf).map(t => `${this.store.get<Agent>(owner, 'agent', t.agentId)?.name ?? t.agentId}:${t.status}${t.error ? `;${t.error}` : ''}`).join('\n') : ''
  886. const request = this.store.require<Run>(owner, 'run', work.runId).discussion ? `本次用户原始要求:\n${rootTrigger.content.slice(0, 16000)}\n\n` : ''
  887. return `${request}${batch ? `本批次执行结果:\n${batch}\n\n` : ''}${omitted ? `较早上下文有 ${omitted} 条因长度限制省略,请勿假定已完整读取。\n\n` : ''}${selected.join('\n\n')}`
  888. }
  889. async respond(owner: string, id: string, answer: string): Promise<void> {
  890. type Reply = WorkspaceInteraction & { answer?: string; responseState?: 'sending' | 'uncertain' | 'sent' }
  891. const interaction = this.store.require<Reply>(owner, 'interaction', id)
  892. this.requireAuthorization(owner,interaction.runId)
  893. this.nodes.requireSource(owner, this.store.require<Agent>(owner, 'agent', interaction.agentId))
  894. if (interaction.resolved) {
  895. if (interaction.answer && interaction.answer !== answer) throw new HttpError(409, '该请求已使用其他答复完成', 'interaction_answer_conflict')
  896. return
  897. }
  898. if (interaction.responseState === 'sending' || interaction.responseState === 'uncertain') throw new HttpError(409, '答复状态待确认,请核对原请求', 'interaction_uncertain')
  899. const binding = this.store.require<{ key: string; upstreamId: string; taskId: string }>(owner, 'interaction-binding', id)
  900. const live = this.live.get(binding.key)
  901. if (!live || live.taskId !== binding.taskId || live.runId !== interaction.runId) throw new HttpError(409, '请先恢复此轮连接', 'interaction_offline')
  902. if (interaction.kind === 'approval' && !['once', 'session', 'always', 'deny', 'allow', 'approve', 'auto'].includes(answer)) throw new HttpError(400, '审批选项无效', 'invalid_approval')
  903. const automatic = interaction.kind === 'approval' && answer === 'auto'
  904. interaction.answer = answer; interaction.responseState = 'sending'
  905. this.store.atomic(() => {
  906. // A user choosing automatic approval grants a durable Bot preference.
  907. // Persist it before replying so immediately following tool requests use it.
  908. // Replayed/resolved requests above never reapply an old preference.
  909. if (automatic) this.store.updateAgent(owner, interaction.agentId, { approvalPolicy: 'allow' })
  910. this.store.put(owner, 'interaction', id, interaction)
  911. })
  912. try {
  913. await live.gateway.rpc(interaction.kind === 'approval' ? 'approval.respond' : 'clarify.respond', {
  914. session_id: live.runtimeId, request_id: binding.upstreamId,
  915. ...(interaction.kind === 'approval' ? { choice: automatic ? 'once' : answer } : { answer }),
  916. })
  917. } catch (error) {
  918. const latest = this.store.require<Reply>(owner, 'interaction', id)
  919. if (!latest.resolved) {
  920. latest.responseState = error instanceof HttpError && error.code === 'gateway_rejected' ? undefined : 'uncertain'
  921. this.store.put(owner, 'interaction', id, latest)
  922. }
  923. if (automatic) throw new HttpError(409, '自动允许已为此 Bot 开启,但本次答复未确认,请检查当前任务状态。', 'approval_auto_response_failed')
  924. throw error
  925. }
  926. interaction.resolved = true; interaction.responseState = 'sent'
  927. this.store.put(owner, 'interaction', id, interaction)
  928. this.store.event(owner, 'interaction.changed', interaction, interaction.conversationId)
  929. const current = this.getWork(owner, binding.taskId)
  930. if (current.status === 'waiting') { current.status = 'running'; this.saveWork(owner, current) }
  931. }
  932. protected async interruptTurn(owner: string, work: Work): Promise<void> {
  933. const key = this.bindingKey(work.conversationId, work.conversationTaskId, work.agentId)
  934. const live = this.live.get(key)
  935. if (live?.taskId === work.id) {
  936. await live.gateway.rpc('session.interrupt', { session_id: live.runtimeId })
  937. const current = this.getWork(owner, work.id)
  938. if (['complete', 'failed', 'interrupted'].includes(current.status)) {
  939. live.done(new Error('已停止'))
  940. return
  941. }
  942. current.status = 'interrupted'; current.error = '已停止'
  943. this.saveWork(owner, current)
  944. if(current.currentMessageId){const message=this.store.require<Message>(owner,'message',current.currentMessageId);message.status='interrupted';message.error='已停止';this.store.saveMessage(owner,message)}
  945. live.done(new Error('已停止'))
  946. } else {
  947. const binding = this.store.get<WorkspaceBinding>(owner, 'binding', key)
  948. if (work.submitted) {
  949. if (!binding || binding.taskId !== work.id) throw new Error('无法确认待停止成员的会话身份')
  950. const target=binding.remoteAgentId||binding.execution==='computer'?this.nodes.targetForAgent(owner,{...binding,id:work.agentId}):this.nodes.target(owner,binding.nodeId)
  951. if(binding.runnerId!==target.runner?.id)throw new Error('执行节点已变化,无法确认原任务已停止')
  952. const gateway = new WorkspaceGateway(target,{workId:work.id,cleanupOnly:true,sessionId:binding.storedId,authorize:()=>{const current=this.store.get<WorkspaceBinding>(owner,'binding',key);if(current?.taskId!==work.id||current.storedId!==binding.storedId)throw new Error('原会话绑定已改变')}})
  953. try {
  954. await gateway.connect()
  955. const opened = await gateway.rpc('session.resume', { profile: binding.profile, session_id: binding.storedId, omit_messages: true, close_on_disconnect: false,...(binding.execution==='computer'?{recoverOnly:true}:{}) })
  956. if (opened.running) await gateway.rpc('session.interrupt', { session_id: opened.session_id })
  957. } finally { gateway.close() }
  958. }
  959. const current = this.getWork(owner, work.id)
  960. current.status = 'interrupted'; current.error = '已停止'
  961. this.saveWork(owner, current)
  962. if (current.currentMessageId) {
  963. const message = this.store.require<Message>(owner, 'message', current.currentMessageId)
  964. message.status = 'interrupted'; this.store.saveMessage(owner, message)
  965. }
  966. }
  967. this.resolveInteractions(owner, work.id)
  968. }
  969. override async stopTask(owner: string, conversationId: string, taskId: string, visited = new Set<string>()): Promise<void> {
  970. const key = `${owner}:${taskId}`
  971. if (visited.has(key)) return
  972. visited.add(key)
  973. await this.collaboration.stopFor(owner, chain => chain.originConversationId === conversationId && chain.originTaskId === taskId)
  974. this.tasks.cancel(owner, taskId)
  975. await Promise.all([super.stopTask(owner, conversationId, taskId), this.tasks.cancelOrigin(owner, conversationId, taskId, visited)])
  976. }
  977. override async stopConversation(owner: string, conversationId: string): Promise<void> {
  978. await this.collaboration.stopFor(owner, chain => chain.originConversationId === conversationId)
  979. for (const task of this.store.list<import('../shared/workspace.js').WorkspaceTask>(owner, 'conversation-task'))
  980. if (task.conversationId === conversationId) this.tasks.cancel(owner, task.id)
  981. await Promise.all([super.stopConversation(owner, conversationId), this.tasks.cancelOrigin(owner, conversationId)])
  982. }
  983. /**
  984. * Hermes reports cumulative per-session token counters (input/output/total) alongside
  985. * context occupancy. Attribute the growth since the last snapshot to the agent's daily
  986. * usage bucket; a session reset (counters dropping) only re-baselines.
  987. */
  988. private recordAgentUsage(owner: string, key: string, agentId: string, usage: unknown): void {
  989. if (!usage || typeof usage !== 'object') return
  990. const value = usage as Record<string, unknown>
  991. const counter = (...names: string[]) => {
  992. for (const name of names) {
  993. const raw = value[name]
  994. if (typeof raw === 'number' && Number.isFinite(raw) && raw >= 0) return Math.round(raw)
  995. }
  996. return undefined
  997. }
  998. const input = counter('input', 'prompt', 'input_tokens'),
  999. output = counter('output', 'completion', 'output_tokens'),
  1000. total = counter('total', 'total_tokens') ?? (input === undefined && output === undefined ? undefined : (input ?? 0) + (output ?? 0))
  1001. if (total === undefined) return
  1002. this.store.atomic(() => {
  1003. const baseline = this.store.get<{ input?: number; output?: number; total?: number }>(owner, 'usage-baseline', key) ?? {}
  1004. const grew = (current: number | undefined, previous: number | undefined) => (current === undefined ? 0 : Math.max(0, current - (previous ?? 0)))
  1005. const inputDelta = grew(input, baseline.input), outputDelta = grew(output, baseline.output), totalDelta = grew(total, baseline.total)
  1006. this.store.put(owner, 'usage-baseline', key, { input, output, total })
  1007. if (!inputDelta && !outputDelta && !totalDelta) return
  1008. const now = new Date(),
  1009. date = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`,
  1010. id = `${agentId}:${date}`
  1011. const day = this.store.get<WorkspaceAgentUsageSummary & { agentId: string; date: string; updatedAt: number }>(owner, 'agent-token-day', id)
  1012. ?? { agentId, date, input: 0, output: 0, total: 0, updatedAt: 0 }
  1013. day.input += inputDelta; day.output += outputDelta; day.total += Math.max(totalDelta, inputDelta + outputDelta)
  1014. day.updatedAt = now.getTime()
  1015. this.store.put(owner, 'agent-token-day', id, day)
  1016. })
  1017. }
  1018. close(): void {
  1019. this.collaboration.close()
  1020. this.tasks.close()
  1021. this.closeScheduler()
  1022. for (const live of [...this.live.values()]) live.done(new Error('服务关闭'))
  1023. this.live.clear()
  1024. }
  1025. }