workspaceAssets.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import { randomUUID, createHash } from 'node:crypto'
  2. import { mkdirSync, writeFileSync } from 'node:fs'
  3. import { join, basename } from 'node:path'
  4. import {HttpError} from './errors.js'
  5. import { authorizeFileRead } from './fileAccess.js'
  6. import { lookup } from 'mime-types'
  7. import { messageFileReferences, type MessageFileOrigin } from '../shared/messageFiles.js'
  8. import { normalizeUploadName } from '../shared/uploadNames.js'
  9. import { WorkspaceStore } from './workspaceStore.js'
  10. import { WorkspaceNodes, type WorkspaceNode } from './workspaceGateway.js'
  11. import type { WorkspaceAgent, WorkspaceFile, WorkspaceMessage } from '../shared/workspace.js'
  12. export interface StoredWorkspaceFile extends WorkspaceFile, MessageFileOrigin {
  13. path: string
  14. digest?: string
  15. sourceNodeId?:string
  16. }
  17. const archiveKey = (nodeId: string, profile: string, messageId: string, path: string) =>
  18. createHash('sha256').update(JSON.stringify([nodeId, profile, messageId, path])).digest('hex')
  19. export function publicFile(f: StoredWorkspaceFile): WorkspaceFile {
  20. const { path: _path, digest: _digest, messageFileSource: _source, ...result } = f
  21. return result.sender === 'user' ? { ...result, name: normalizeUploadName(result.name) } : result
  22. }
  23. export function libraryFile(
  24. f: WorkspaceFile,
  25. id: number,
  26. conversation?: { id: string; name: string },
  27. ) {
  28. const path = `/api/app/files/${f.id}/download`
  29. const origin = {
  30. id,
  31. sourceKind: f.sender === 'user' ? 'upload' : 'agent_generated',
  32. eventKind: f.sender === 'user' ? 'uploaded' : 'generated',
  33. username: '',
  34. profile: f.profile ?? '',
  35. agent: f.profile ?? '',
  36. contextType: 'session',
  37. sessionId: f.conversationId ?? '',
  38. sessionTitle: conversation?.name ?? '',
  39. workspaceConversationId: conversation?.id,
  40. messageId: f.messageId ?? '',
  41. runId: '',
  42. workspace: '',
  43. originalPath: path,
  44. referencePath: path,
  45. observedAt: f.createdAt,
  46. messageTimestamp: f.createdAt,
  47. authorKind: f.sender,
  48. }
  49. return {
  50. ...f,
  51. id,
  52. itemId: id,
  53. path,
  54. extension: f.name.split('.').at(-1) ?? '',
  55. modifiedAt: f.createdAt,
  56. exists: true,
  57. firstSeenAt: f.createdAt,
  58. lastSeenAt: f.createdAt,
  59. messageTimestamp: f.createdAt,
  60. origins: [origin],
  61. archiveStatus: 'ready',
  62. availability: 'archived',
  63. archivedAt: f.createdAt,
  64. messageId: f.messageId ?? null,
  65. sessionId: f.conversationId ?? null,
  66. displayName: f.name,
  67. contentType: f.mimeType,
  68. byteCount: f.size,
  69. sizeBytes: f.size,
  70. kind: f.mimeType.startsWith('image/')
  71. ? 'image'
  72. : f.mimeType.startsWith('video/')
  73. ? 'video'
  74. : f.mimeType.startsWith('text/')
  75. ? 'text'
  76. : 'file',
  77. createdAt: new Date(f.createdAt).toISOString(),
  78. downloadUrl: path,
  79. previewUrl: `/api/app/files/${f.id}/preview`,
  80. }
  81. }
  82. export class WorkspaceAssets {
  83. private stopped = false
  84. private pending = new Set<string>()
  85. constructor(
  86. readonly store: WorkspaceStore,
  87. readonly nodes: WorkspaceNodes,
  88. readonly home: string,
  89. ) {}
  90. private requireCapacity(owner: string, messageId: string, bytes: number): void {
  91. const count = Number(this.store.db.prepare("SELECT count(*) AS n FROM workspace_entities WHERE owner=? AND kind='file' AND json_extract(data,'$.messageId')=?").get(owner, messageId)?.n ?? 0)
  92. if (bytes > 25 * 1024 * 1024 || count >= 8)
  93. throw new HttpError(413, '每轮最多 8 个产物,每个不超过 25 MiB', 'artifact_limit')
  94. const used = Number(this.store.db.prepare("SELECT coalesce(sum(json_extract(data,'$.size')),0) AS n FROM workspace_entities WHERE kind='file'").get()?.n ?? 0)
  95. if (used + bytes > 2 * 1024 * 1024 * 1024) throw new HttpError(413, '产物存储空间已达上限', 'artifact_quota')
  96. }
  97. /** Legacy archives lost nodeId, but their immutable archive key includes it.
  98. * Restore only a unique authorized match; never assume a file came from local. */
  99. recoverFileSource(owner: string, file: StoredWorkspaceFile): StoredWorkspaceFile {
  100. if (file.sourceNodeId || !file.sourcePath || !file.profile || !file.messageId) return file
  101. const records = Number(this.store.db.prepare("SELECT count(*) AS n FROM workspace_entities WHERE owner=? AND kind='file' AND json_extract(data,'$.sourcePath')=? AND json_extract(data,'$.profile')=? AND json_extract(data,'$.messageId')=?").get(owner, file.sourcePath, file.profile, file.messageId)?.n ?? 0)
  102. if (records !== 1) return file
  103. const candidates = ['local', ...this.store.list<WorkspaceNode>(owner, 'node').map(node => node.id)]
  104. let revokedMatch = false
  105. const matches = [...new Set(candidates)].filter(nodeId => {
  106. if (!this.store.get(owner, 'archived-path', archiveKey(nodeId, file.profile!, file.messageId!, file.sourcePath!))) return false
  107. try { this.nodes.requireSource(owner, { nodeId, profile: file.profile! }) }
  108. catch { revokedMatch = true; return false }
  109. return true
  110. })
  111. if (revokedMatch || matches.length !== 1) return file
  112. const recovered = { ...file, sourceNodeId: matches[0]! }
  113. this.store.put(owner, 'file', file.id, recovered)
  114. return recovered
  115. }
  116. publish(owner:string,message:WorkspaceMessage,agent:WorkspaceAgent,name:string,bytes:Buffer):WorkspaceFile {
  117. const digest=createHash('sha256').update(bytes).digest('hex')
  118. name=basename(name).replace(/[\\/\u0000-\u001f\u007f]/g,'_').slice(0,240)||'artifact'
  119. const files=this.store.list<StoredWorkspaceFile>(owner,'file')
  120. const existing=files.find(file=>file.messageId===message.id&&file.name===name&&file.digest===digest)
  121. if(existing)return publicFile(existing)
  122. this.requireCapacity(owner, message.id, bytes.length)
  123. const dir=join(this.home,'workspace-files',owner);mkdirSync(dir,{recursive:true,mode:0o700})
  124. const path=join(dir,digest)
  125. try{writeFileSync(path,bytes,{mode:0o600,flag:'wx'})}catch(error){if((error as NodeJS.ErrnoException).code!=='EEXIST')throw error}
  126. const file:StoredWorkspaceFile={id:randomUUID(),name,mimeType:lookup(name)||'application/octet-stream',path,digest,size:bytes.length,sender:'agent',messageFileSource:'attachment',profile:agent.profile,sourceNodeId:agent.nodeId,conversationId:message.conversationId,messageId:message.id,createdAt:Date.now()}
  127. this.store.atomic(()=>{this.store.put(owner,'file',file.id,file);this.store.event(owner,'files.changed',publicFile(file),message.conversationId)})
  128. return publicFile(file)
  129. }
  130. async archive(owner: string, message: WorkspaceMessage): Promise<void> {
  131. if (!message.agentId || message.role !== 'assistant' || message.visible === false) return
  132. const agent = this.store.require<WorkspaceAgent>(owner, 'agent', message.agentId)
  133. await this.archiveText(
  134. owner,
  135. message.content,
  136. agent.nodeId,
  137. agent.profile,
  138. message.conversationId,
  139. message.id,
  140. 'agent',
  141. agent.remoteAgentId,
  142. )
  143. if (this.stopped) return
  144. const attachments = this.store
  145. .visibleFiles(owner)
  146. .filter((f) => f.messageId === message.id)
  147. .map(publicFile)
  148. if (attachments.length || message.attachments.length) {
  149. message.attachments = attachments
  150. this.store.saveMessage(owner, message)
  151. }
  152. }
  153. async archiveText(
  154. owner: string,
  155. text: string,
  156. nodeId: string,
  157. profile: string,
  158. conversationId: string,
  159. messageId: string,
  160. sender: 'user' | 'agent' = 'agent',
  161. remoteAgentId?: string,
  162. ): Promise<void> {
  163. if (this.stopped) return
  164. const message=this.store.get<WorkspaceMessage>(owner,'message',messageId)
  165. if(message?.execution==='computer'||(!message?.execution&&message?.agentId&&this.store.get<WorkspaceAgent>(owner,'agent',message.agentId)?.execution==='computer'))return
  166. const paths = new Set([...messageFileReferences(text)].filter(path => /^(?:\/|~\/)/.test(path)))
  167. // User attachment staging is explicit upload metadata, not a tool result.
  168. if (sender === 'user') {
  169. for (const match of text.matchAll(/"(?:path|file_path|output_path)"\s*:\s*"((?:\/|~\/)[^"\n<>]+)"/g)) {
  170. paths.add(match[1]!)
  171. }
  172. }
  173. for (const path of [...paths].slice(0, 8)) {
  174. const key = archiveKey(nodeId, profile, messageId, path)
  175. if (this.store.get(owner, 'archived-path', key) || this.pending.has(`${owner}:${key}`))
  176. continue
  177. this.pending.add(`${owner}:${key}`)
  178. try {
  179. this.nodes.requireSource(owner, { nodeId, profile })
  180. this.requireCapacity(owner, messageId, 0)
  181. const target = remoteAgentId ? this.nodes.targetForAgent(owner,{nodeId,remoteAgentId}) : this.nodes.target(owner,nodeId)
  182. const authorizedPath = await authorizeFileRead({ home: this.home }, target.session, path, profile)
  183. this.nodes.requireSource(owner, { nodeId, profile })
  184. const response = await target.session.request('/api/files/download', {
  185. search: new URLSearchParams({ profile, path: authorizedPath }),
  186. maxResponseBytes: 25 * 1024 * 1024,
  187. })
  188. if (response.status !== 200 || this.stopped) continue
  189. // A download may outlive an assignment or directory-policy change.
  190. this.nodes.requireSource(owner, { nodeId, profile })
  191. if (await authorizeFileRead({ home: this.home }, target.session, path, profile) !== authorizedPath || this.stopped) continue
  192. this.nodes.requireSource(owner, { nodeId, profile })
  193. this.requireCapacity(owner, messageId, response.body.length)
  194. const dir = join(this.home, 'workspace-files', owner)
  195. mkdirSync(dir, { recursive: true, mode: 0o700 })
  196. const digest = createHash('sha256').update(response.body).digest('hex')
  197. const id = randomUUID(),
  198. saved = join(dir, digest),
  199. name = basename(path)
  200. try { writeFileSync(saved, response.body, { mode: 0o600, flag: 'wx' }) }
  201. catch (error) { if ((error as NodeJS.ErrnoException).code !== 'EEXIST') throw error }
  202. const file: StoredWorkspaceFile = {
  203. id,
  204. sourcePath: path,
  205. messageFileSource: 'reference',
  206. path: saved,
  207. digest,
  208. name,
  209. mimeType: String(
  210. response.headers.get('content-type') || lookup(name) || 'application/octet-stream',
  211. ).split(';')[0]!,
  212. size: response.body.length,
  213. sender,
  214. profile,
  215. sourceNodeId: nodeId,
  216. conversationId,
  217. messageId,
  218. createdAt: Date.now(),
  219. }
  220. this.store.atomic(() => {
  221. this.store.put(owner, 'file', id, file)
  222. this.store.put(owner, 'archived-path', key, true)
  223. this.store.event(owner, 'files.changed', publicFile(file), conversationId)
  224. })
  225. } catch {
  226. /* A missing upstream file does not fail an otherwise completed reply. */
  227. } finally {
  228. this.pending.delete(`${owner}:${key}`)
  229. }
  230. }
  231. }
  232. close(): void {
  233. this.stopped = true
  234. }
  235. }