workspaceRuntime.ts 67 KB

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