workspaceRuntime.ts 62 KB

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