workspaceRuntime.ts 63 KB

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