workspaceRoutes.ts 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. import { WORKSPACE_PATCH_CAPABILITY } from '../shared/workspaceMessagePatch.js'
  2. import { workspaceDetail, streamWorkspace } from './workspaceSync.js'
  3. import { readServerIdentity } from './serverIdentity.js'
  4. import { randomUUID } from 'node:crypto'
  5. import Router from '@koa/router'
  6. import type Koa from 'koa'
  7. import { createReadStream, statSync } from 'node:fs'
  8. import { z } from 'zod'
  9. import { WorkspaceStore, agentInput, agentPatch, parse } from './workspaceStore.js'
  10. import { WorkspaceRuntime, sendInput } from './workspaceRuntime.js'
  11. import { WorkspaceNodes, type WorkspaceNode } from './workspaceGateway.js'
  12. import {
  13. WorkspaceAssets,
  14. libraryFile,
  15. publicFile,
  16. type StoredWorkspaceFile,
  17. } from './workspaceAssets.js'
  18. import { receiveGroupUploads, type UploadStore } from './uploads.js'
  19. import { HttpError } from './errors.js'
  20. import type { PushCoordinator } from './pushCoordinator.js'
  21. import type { LocalAuthStore } from './localAuth.js'
  22. import type { CsrfProtection } from './security.js'
  23. import { compareWorkspaceConversations } from '../shared/workspace.js'
  24. import type {
  25. WorkspaceMessage,
  26. WorkspaceAgent,
  27. WorkspaceConversation,
  28. WorkspaceRun,
  29. WorkspaceInteraction,
  30. } from '../shared/workspace.js'
  31. const body = (ctx: Koa.Context): any => (ctx.request as Koa.Request & { body?: unknown }).body ?? {}
  32. const number = (v: unknown, fallback: number) =>
  33. typeof v === 'string' && /^\d+$/.test(v) ? Math.min(Number(v), Number.MAX_SAFE_INTEGER) : fallback
  34. export function workspaceRouter(
  35. store: WorkspaceStore,
  36. runtime: WorkspaceRuntime,
  37. nodes: WorkspaceNodes,
  38. assets: WorkspaceAssets,
  39. uploads: UploadStore,
  40. auth: LocalAuthStore,
  41. push: PushCoordinator,
  42. csrf?: CsrfProtection,
  43. ): Router {
  44. const router = new Router(),
  45. owner = (ctx: Koa.Context) => auth.require(ctx).id
  46. const libraryId = (user: string, id: string) =>
  47. Number(
  48. store.db
  49. .prepare("SELECT rowid AS n FROM workspace_entities WHERE owner=? AND kind='file' AND id=?")
  50. .get(user, id)?.n ?? 0,
  51. )
  52. const fileRecord = (user: string, id: string): StoredWorkspaceFile => {
  53. if (/^\d+$/.test(id)) {
  54. const row = store.db
  55. .prepare("SELECT data FROM workspace_entities WHERE owner=? AND kind='file' AND rowid=?")
  56. .get(user, Number(id))
  57. if (row) {const file:StoredWorkspaceFile=JSON.parse(String(row.data));if(file.sourceNodeId&&file.profile)nodes.requireSource(user,{nodeId:file.sourceNodeId,profile:file.profile});return file}
  58. }
  59. const file=store.require<StoredWorkspaceFile>(user,'file',id)
  60. if(file.sourceNodeId&&file.profile)nodes.requireSource(user,{nodeId:file.sourceNodeId,profile:file.profile})
  61. return file
  62. }
  63. router.get('/api/app/capabilities', (ctx) => {
  64. owner(ctx)
  65. ctx.set('Cache-Control', 'no-store')
  66. ctx.body = {
  67. csrfToken: csrf?.issue(ctx),
  68. protocolVersion: 1,
  69. serverKind: 'yaoyao-web',
  70. features: [
  71. 'agents',
  72. 'conversations',
  73. 'conversationTasks',
  74. 'editableGroups',
  75. 'agentTeamManagement',
  76. 'computerControl','sharedComputers','computerManagement',
  77. 'files',
  78. 'voice',
  79. 'context',
  80. ...(auth.require(ctx).role === 'admin' ? ['nodes', 'pairedNodes', 'remoteAgentReferences', 'editableNodeAddress'] : []),
  81. 'events', 'workspace-stream-v1',
  82. ...(store.messagePatchesEnabled ? [WORKSPACE_PATCH_CAPABILITY] : []),
  83. ],
  84. }
  85. })
  86. router.get('/api/app/agents/sources', async (ctx) => {
  87. ctx.body = await nodes.sources(owner(ctx))
  88. })
  89. router.get('/api/app/agents', async (ctx) => {
  90. const user=owner(ctx), agents=store.list<WorkspaceAgent>(user,'agent')
  91. for (const agent of agents) if (agent.remoteAgentId) void nodes.refreshRemoteAgent(user,agent).catch(()=>{})
  92. ctx.body = { agents: agents.map(agent=>store.agentSummary(agent)) }
  93. })
  94. router.get('/api/app/nodes/:id/agents', async ctx => {
  95. auth.requireAdmin(ctx)
  96. ctx.body = { agents: await nodes.remoteAgents(owner(ctx),ctx.params.id) }
  97. })
  98. router.post('/api/app/agents/remote', async ctx => {
  99. auth.requireAdmin(ctx)
  100. const input = parse(z.object({nodeId:z.string().uuid(),agentId:z.string().uuid()}).strict(),body(ctx))
  101. ctx.body = { agent: await nodes.importRemoteAgent(owner(ctx),input.nodeId,input.agentId) };ctx.status=201
  102. })
  103. router.post('/api/app/agents', async (ctx) => {
  104. const user = owner(ctx),
  105. input = parse(agentInput, body(ctx))
  106. const authorization = auth.pushAuthorizationVersion(user)
  107. nodes.requireSource(user, input)
  108. const sources = await nodes.sources(user)
  109. if (!sources.sources.some((s) => s.nodeId === input.nodeId && s.profile === input.profile))
  110. throw new HttpError(409, '基础机器人当前不可用', 'source_unavailable')
  111. nodes.requireSource(user, input)
  112. if (input.canManageTeam || input.execution==='computer') await runtime.teamTools.requireAvailable(user, input)
  113. if (auth.pushAuthorizationVersion(user) !== authorization) throw new HttpError(401,'账号授权已变化,请重新登录','session_revoked')
  114. ctx.body = { agent: store.agentSummary(store.createAgent(user, input)) }
  115. ctx.status = 201
  116. })
  117. router.patch('/api/app/agents/:id', async (ctx) => {
  118. const user = owner(ctx), agent = store.require<WorkspaceAgent>(user, 'agent', ctx.params.id), input = parse(agentPatch,body(ctx))
  119. const authorization = auth.pushAuthorizationVersion(user)
  120. const updated={...agent,...input}
  121. const sourceChanged=updated.nodeId!==agent.nodeId||updated.profile!==agent.profile
  122. nodes.requireSource(user,updated)
  123. if(sourceChanged){
  124. const sources=await nodes.sources(user)
  125. if(!sources.sources.some(s=>s.nodeId===updated.nodeId&&s.profile===updated.profile))throw new HttpError(409,'基础机器人当前不可用','source_unavailable')
  126. }
  127. if ((input.canManageTeam === true && agent.canManageTeam !== true) || input.execution==='computer' || (sourceChanged&&(updated.canManageTeam||updated.execution==='computer'))) await runtime.teamTools.requireAvailable(user, updated)
  128. if(sourceChanged&&runtime.cloud?.selected(user,updated))await runtime.cloud.requireAvailable(user,updated,nodes.target(user,updated.nodeId))
  129. if (auth.pushAuthorizationVersion(user) !== authorization) throw new HttpError(401,'账号授权已变化,请重新登录','session_revoked')
  130. nodes.requireSource(user, updated)
  131. ctx.body = { agent: store.agentSummary(store.atomic(() => {
  132. if (input.archived === true && !agent.temporaryGoalId) {
  133. const direct = store.list<WorkspaceConversation>(user, 'conversation').find(c => c.kind === 'direct' && c.memberIds[0] === agent.id)
  134. if (direct) store.changeConversationLifecycle(user, direct.id, 'archive')
  135. }
  136. nodes.requireSource(user,{...store.require<WorkspaceAgent>(user,'agent',ctx.params.id),...input})
  137. return store.updateAgent(user, ctx.params.id, input)
  138. })) }
  139. })
  140. router.delete('/api/app/agents/:id', (ctx) => {
  141. store.deleteAgent(owner(ctx), ctx.params.id)
  142. ctx.body = { ok: true }
  143. })
  144. router.delete('/api/app/conversations/:id', (ctx) => {
  145. store.deleteConversation(owner(ctx), ctx.params.id)
  146. ctx.body = { ok: true }
  147. })
  148. router.get('/api/app/conversations/:id/lifecycle', ctx => {
  149. ctx.body = store.conversationLifecycle(owner(ctx), ctx.params.id)
  150. })
  151. router.post('/api/app/conversations/:id/lifecycle', ctx => {
  152. const input = parse(z.object({ action: z.enum(['archive', 'restore', 'delete']), confirmationToken: z.string().regex(/^[a-f0-9]{64}$/).optional() }).strict(), body(ctx))
  153. if (input.action !== 'restore' && !input.confirmationToken) throw new HttpError(409, '请先确认聊天操作', 'lifecycle_confirmation_required')
  154. store.changeConversationLifecycle(owner(ctx), ctx.params.id, input.action, input.confirmationToken)
  155. runtime.wake()
  156. ctx.body = { ok: true }
  157. })
  158. router.get('/api/app/conversations', (ctx) => {
  159. const user = owner(ctx)
  160. ctx.body = {
  161. conversations: store
  162. .list<WorkspaceConversation>(user, 'conversation')
  163. .map(c => store.conversationSummary(user, c))
  164. .sort(compareWorkspaceConversations),
  165. cursor: store.cursor(user),
  166. }
  167. })
  168. router.post('/api/app/conversations', (ctx) => {
  169. const user = owner(ctx),
  170. input = body(ctx)
  171. for (const id of Array.isArray(input.memberIds) ? input.memberIds : [])
  172. nodes.requireSource(user, store.require<WorkspaceAgent>(user, 'agent', id))
  173. const conversation = store.createGroup(user, input)
  174. try {
  175. push.setGroupSubscription(user, conversation.id, true, conversation.lastSeq)
  176. } catch {
  177. /* Optional delivery cannot undo a created chat. */
  178. }
  179. ctx.body = { conversation }
  180. ctx.status = 201
  181. })
  182. router.get('/api/app/conversations/:id/tasks', (ctx) => {
  183. const user = owner(ctx)
  184. store.resolveTask(user, ctx.params.id)
  185. ctx.body = { tasks: store.tasks(user, ctx.params.id) }
  186. })
  187. router.get('/api/app/conversations/:id/tasks/:taskId/plan', (ctx) => {
  188. const user = owner(ctx)
  189. store.requireTask(user, ctx.params.id, ctx.params.taskId)
  190. ctx.body = { goal: store.get(user, 'goal', ctx.params.taskId) ?? null,
  191. assignments: runtime.tasks.assignments(user, ctx.params.taskId) }
  192. })
  193. router.patch('/api/app/conversations/:id/tasks/:taskId/plan', (ctx) => {
  194. const user = owner(ctx)
  195. store.requireTask(user, ctx.params.id, ctx.params.taskId)
  196. ctx.body = { goal: runtime.tasks.updateCriteria(user, ctx.params.taskId, body(ctx)) }
  197. })
  198. router.post('/api/app/conversations/:id/tasks/:taskId/stop', async (ctx) => {
  199. const user=owner(ctx)
  200. store.requireTask(user,ctx.params.id,ctx.params.taskId)
  201. await runtime.stopTask(user,ctx.params.id,ctx.params.taskId)
  202. ctx.body={ok:true}
  203. })
  204. router.post('/api/app/conversations/:id/tasks/:taskId/resume', async (ctx) => {
  205. const user=owner(ctx), input=parse(z.object({requestId:z.string().uuid()}).strict(),body(ctx))
  206. const version=auth.pushAuthorizationVersion(user)
  207. store.requireTask(user,ctx.params.id,ctx.params.taskId)
  208. const goal=store.require<import('../shared/agentTasks.js').AgentGoal>(user,'goal',ctx.params.taskId)
  209. const agent=store.require<WorkspaceAgent>(user,'agent',goal.coordinatorId)
  210. await runtime.teamTools.requireAvailable(user,agent)
  211. if(auth.pushAuthorizationVersion(user)!==version)throw new HttpError(401,'账号授权已变化,请重新登录','session_revoked')
  212. ctx.body=store.command(user,input.requestId,{operation:'goal.resume-user',goalId:goal.id},()=>{
  213. const run=runtime.send(user,ctx.params.id,{requestId:randomUUID(),taskId:goal.id,content:'继续处理当前目标,请先核对已有工作,避免重复执行。'})
  214. const updated=runtime.tasks.resume(user,agent.id,{requestId:randomUUID(),goalId:goal.id},
  215. {conversationId:ctx.params.id,conversationTaskId:goal.id,runId:run.id,agentId:agent.id})
  216. return {goal:updated,run}
  217. })
  218. })
  219. router.post('/api/app/conversations/:id/tasks', (ctx) => {
  220. const task = store.createTask(owner(ctx), ctx.params.id, body(ctx))
  221. ctx.status = 201
  222. ctx.body = { task }
  223. })
  224. router.patch('/api/app/conversations/:id/tasks/:taskId', (ctx) => {
  225. ctx.body = { task: store.updateTask(owner(ctx), ctx.params.id, ctx.params.taskId, body(ctx)) }
  226. })
  227. router.put('/api/app/conversations/:id/tasks/:taskId/read', (ctx) => {
  228. const { seq, unreadVersion } = parse(z.object({ seq: z.number().int().nonnegative(), unreadVersion: z.number().int().nonnegative().optional() }).strict(), body(ctx))
  229. const user = owner(ctx)
  230. ctx.body = { task: store.markTaskRead(user, ctx.params.id, ctx.params.taskId, seq, unreadVersion),
  231. conversation: store.conversationSummary(user, store.require(user, 'conversation', ctx.params.id)) }
  232. })
  233. router.delete('/api/app/conversations/:id/tasks/:taskId', async (ctx) => {
  234. const user = owner(ctx)
  235. await runtime.stopTask(user, ctx.params.id, ctx.params.taskId)
  236. store.deleteTask(user, ctx.params.id, ctx.params.taskId)
  237. ctx.body = { ok: true }
  238. })
  239. router.get('/api/app/conversations/:id', (ctx) => {
  240. ctx.body = workspaceDetail(store, runtime, owner(ctx), ctx.params.id,
  241. typeof ctx.query.taskId === 'string' ? ctx.query.taskId : undefined,
  242. Math.max(1, Math.min(100, number(ctx.query.limit, 100))))
  243. })
  244. router.patch('/api/app/conversations/:id', async (ctx) => {
  245. const user = owner(ctx), input = body(ctx)
  246. for (const id of Array.isArray(input.memberIds) ? input.memberIds : [])
  247. nodes.requireSource(user, store.require<WorkspaceAgent>(user, 'agent', id))
  248. const conversation = store.updateConversation(user, ctx.params.id, input)
  249. if (conversation.archived) await runtime.stopConversation(user, conversation.id)
  250. runtime.wake()
  251. ctx.body = { conversation: store.require(user, 'conversation', conversation.id) }
  252. })
  253. router.post('/api/app/conversations/:id/agents/:agentId/stop', async (ctx) => {
  254. const user = owner(ctx),
  255. taskId = typeof ctx.query.taskId === 'string' ? ctx.query.taskId : undefined,
  256. task = store.resolveTask(user, ctx.params.id, taskId)
  257. await runtime.stopAgent(user, ctx.params.id, ctx.params.agentId, task?.id)
  258. ctx.body = { ok: true }
  259. })
  260. router.get('/api/app/conversations/:id/messages', (ctx) => {
  261. const user = owner(ctx),
  262. taskId = typeof ctx.query.taskId === 'string' ? ctx.query.taskId : undefined,
  263. task = store.resolveTask(user, ctx.params.id, taskId)
  264. ctx.body = {
  265. messages: store.messages(
  266. user,
  267. ctx.params.id,
  268. number(ctx.query.before, Number.MAX_SAFE_INTEGER),
  269. Math.min(200, number(ctx.query.limit, 100)),
  270. false,
  271. task?.id,
  272. ),
  273. cursor: store.cursor(user),
  274. }
  275. })
  276. router.post('/api/app/conversations/:id/messages', async (ctx) => {
  277. const user = owner(ctx)
  278. const input = parse(sendInput, body(ctx))
  279. if (input.mode === 'goal') {
  280. const version = auth.pushAuthorizationVersion(user)
  281. const conversation = store.require<WorkspaceConversation>(user, 'conversation', ctx.params.id)
  282. await runtime.teamTools.requireAvailable(user, store.require<WorkspaceAgent>(user, 'agent', conversation.administratorId))
  283. if (auth.pushAuthorizationVersion(user) !== version) throw new HttpError(401, '账号授权已变化,请重新登录', 'session_revoked')
  284. }
  285. const accepted = runtime.send(user, ctx.params.id, input)
  286. const run = store.require<WorkspaceRun>(user, 'run', accepted.id)
  287. ctx.body = {
  288. requestId: input.requestId, run,
  289. message: store.messageForDisplay(user, store.require<WorkspaceMessage>(user, 'message', run.messageId)),
  290. conversation: store.conversationSummary(user, store.require<WorkspaceConversation>(user, 'conversation', run.conversationId)),
  291. task: run.conversationTaskId ? store.requireTask(user, run.conversationId, run.conversationTaskId) : null,
  292. cursor: store.cursor(user),
  293. }
  294. ctx.status = 202
  295. })
  296. router.put('/api/app/conversations/:id/read', (ctx) => {
  297. const user = owner(ctx),
  298. c = store.require<WorkspaceConversation>(user, 'conversation', ctx.params.id)
  299. const { seq, unreadVersion } = parse(z.object({ seq: z.number().int().nonnegative(), unreadVersion: z.number().int().nonnegative().optional() }).strict(), body(ctx))
  300. if (c.kind === 'group') {
  301. const taskId = typeof ctx.query.taskId === 'string' ? ctx.query.taskId : undefined,
  302. task = store.resolveTask(user, c.id, taskId)!
  303. store.markTaskRead(user, c.id, task.id, seq, unreadVersion)
  304. }
  305. ctx.body = { conversation: store.markConversationRead(user, c.id, seq, unreadVersion) }
  306. })
  307. router.post('/api/app/runs/:id/stop', async (ctx) => {
  308. await runtime.stop(owner(ctx), ctx.params.id)
  309. ctx.body = { ok: true }
  310. })
  311. router.post('/api/app/runs/:id/reconcile', (ctx) => {
  312. const user = owner(ctx)
  313. store.require(user, 'run', ctx.params.id)
  314. void runtime.reconcile(user, ctx.params.id).catch(() => {})
  315. ctx.status = 202
  316. ctx.body = { ok: true }
  317. })
  318. router.post('/api/app/interactions/:id/respond', async (ctx) => {
  319. const b = parse(z.object({ answer: z.string().min(1).max(16_000) }).strict(), body(ctx))
  320. await runtime.respond(owner(ctx), ctx.params.id, b.answer)
  321. ctx.body = { ok: true }
  322. })
  323. router.get('/api/app/workspace/snapshot', ctx => {
  324. const user = owner(ctx)
  325. const conversations = store.list<WorkspaceConversation>(user, 'conversation')
  326. .map(c => store.conversationSummary(user, c)).sort(compareWorkspaceConversations)
  327. const details = conversations.filter(c => !c.archived).map(c => workspaceDetail(store, runtime, user, c.id))
  328. ctx.set('Cache-Control', 'no-store')
  329. ctx.body = { agents: store.list<WorkspaceAgent>(user, 'agent').map(a => store.agentSummary(a)),
  330. conversations, details, cursor: store.cursor(user), serverIdentity: readServerIdentity(store) }
  331. })
  332. router.get('/api/app/events/stream', ctx => streamWorkspace(ctx, store, auth))
  333. router.get('/api/app/events', (ctx) => {
  334. const user = owner(ctx),
  335. after = number(ctx.query.after, 0),
  336. events = store.events(user, after)
  337. ctx.set('Cache-Control', 'no-store')
  338. ctx.body = { events, cursor: events.at(-1)?.seq ?? after, serverIdentity: readServerIdentity(store) }
  339. })
  340. router.get('/api/app/nodes', (ctx) => {
  341. auth.requireAdmin(ctx)
  342. ctx.body = {
  343. nodes: store
  344. .list<WorkspaceNode>(owner(ctx), 'node')
  345. .map(({ secret: _secret, ...node }) => node),
  346. }
  347. })
  348. router.post('/api/app/nodes', async (ctx) => {
  349. auth.requireAdmin(ctx)
  350. const input = parse(z.object({ qrPayload: z.string().min(1).max(8192), name: z.string().max(100).default(''), nodeId:z.string().uuid().optional() }).strict(), body(ctx))
  351. await nodes.pair(owner(ctx), input)
  352. ctx.status = 201; ctx.body = { ok: true }
  353. })
  354. router.patch('/api/app/nodes/:id', async (ctx) => {
  355. auth.requireAdmin(ctx)
  356. const input = parse(z.object({ name: z.string().min(1).max(100), url: z.string().url().max(2048) }).strict(), body(ctx))
  357. await nodes.update(owner(ctx), ctx.params.id, input)
  358. ctx.body = { ok: true }
  359. })
  360. router.delete('/api/app/nodes/:id', (ctx) => {
  361. auth.requireAdmin(ctx)
  362. nodes.remove(owner(ctx), ctx.params.id)
  363. ctx.body = { ok: true }
  364. })
  365. router.post('/api/app/uploads', async (ctx) => {
  366. const user = owner(ctx),
  367. refs = await receiveGroupUploads(ctx.req, uploads, user)
  368. const files = uploads
  369. .records(
  370. refs.map((r) => r.id),
  371. user,
  372. )
  373. .map((r) => ({
  374. id: r.id,
  375. name: r.name,
  376. mimeType: r.mimeType,
  377. size: r.size,
  378. path: r.path,
  379. sender: 'user' as const,
  380. createdAt: Date.now(),
  381. }))
  382. // A library entry owns its bytes even when the user cancels the draft.
  383. uploads.markReferenced(files.map(file => file.id), user)
  384. for (const f of files) store.put(user, 'file', f.id, f)
  385. ctx.status = 201
  386. ctx.body = { files: files.map(publicFile) }
  387. })
  388. router.get('/api/app/files', (ctx) => {
  389. const files = store
  390. .visibleFiles(owner(ctx))
  391. .filter(
  392. (f) =>
  393. (!ctx.query.search ||
  394. f.name.toLowerCase().includes(String(ctx.query.search).toLowerCase())) &&
  395. (!ctx.query.profile || f.profile === ctx.query.profile) &&
  396. (!ctx.query.session_id || f.conversationId === ctx.query.session_id) &&
  397. (!ctx.query.sender || f.sender === ctx.query.sender),
  398. )
  399. .sort((a, b) => b.createdAt - a.createdAt || a.id.localeCompare(b.id))
  400. .map((f) => libraryFile(publicFile(f), libraryId(owner(ctx), f.id), store.get<WorkspaceConversation>(owner(ctx),'conversation',f.conversationId ?? '')))
  401. .filter((f) => !ctx.query.kind || f.kind === ctx.query.kind)
  402. const start = number(ctx.query.cursor, 0),
  403. limit = Math.min(200, number(ctx.query.limit, 50))
  404. ctx.body = {
  405. items: files.slice(start, start + limit),
  406. total: files.length,
  407. nextCursor: start + limit < files.length ? String(start + limit) : null,
  408. }
  409. })
  410. router.get('/api/app/files/stats', (ctx) => {
  411. const files = store.visibleFiles(owner(ctx))
  412. ctx.body = { count: files.length, totalBytes: files.reduce((sum, f) => sum + f.size, 0) }
  413. })
  414. router.post('/api/app/message-files/query', (ctx) => {
  415. const ids = parse(
  416. z.object({ messageIds: z.array(z.union([z.string(), z.number()])).max(500) }),
  417. body(ctx),
  418. ).messageIds.map(String)
  419. const files = store.visibleFiles(owner(ctx))
  420. ctx.body = {
  421. messages: Object.fromEntries(
  422. ids.map((id) => [
  423. id,
  424. files
  425. .filter((f) => f.messageId === id || store.get<WorkspaceMessage>(owner(ctx),'message',id)?.attachments.some(attachment=>attachment.id===f.id))
  426. .map((f, ordinal) => ({
  427. itemId: libraryId(owner(ctx), f.id),
  428. messageId: id,
  429. ordinal,
  430. originalPath: `/api/app/files/${f.id}/download`,
  431. referencePath: `/api/app/files/${f.id}/download`,
  432. name: f.name,
  433. size: f.size,
  434. mimeType: f.mimeType,
  435. archiveStatus: 'ready',
  436. archivedAt: f.createdAt,
  437. availability: 'archived',
  438. })),
  439. ]),
  440. ),
  441. }
  442. })
  443. for (const action of ['download', 'preview'])
  444. router.get(`/api/app/files/:id/${action}`, (ctx) => {
  445. const file = fileRecord(owner(ctx), ctx.params.id),
  446. size = statSync(file.path).size
  447. const activeContent =
  448. /(?:html|svg|javascript|ecmascript|xml)/i.test(file.mimeType) ||
  449. /\.(?:html?|svg|js|mjs|xml|xhtml)$/i.test(file.name)
  450. ctx.type = activeContent ? 'application/octet-stream' : file.mimeType
  451. ctx.set(
  452. 'Content-Disposition',
  453. `${action === 'preview' && !activeContent ? 'inline' : 'attachment'}; filename*=UTF-8''${encodeURIComponent(file.name)}`,
  454. )
  455. ctx.set('Accept-Ranges', 'bytes')
  456. ctx.set('Cache-Control', 'private, no-store')
  457. if(size===0){ctx.body=Buffer.alloc(0);return}
  458. const range = ctx.get('range'),
  459. match = /^bytes=(\d*)-(\d*)$/.exec(range)
  460. let start = 0,
  461. end = size - 1
  462. if (range) {
  463. if (!match || (!match[1] && !match[2]))
  464. throw new HttpError(416, 'Range invalid', 'invalid_range')
  465. if (!match[1]) start = Math.max(0, size - Number(match[2]))
  466. else {
  467. start = Number(match[1])
  468. end = match[2] ? Math.min(size - 1, Number(match[2])) : size - 1
  469. }
  470. if (start > end || start >= size) {
  471. ctx.set('Content-Range', `bytes */${size}`)
  472. throw new HttpError(416, 'Range invalid', 'invalid_range')
  473. }
  474. ctx.status = 206
  475. ctx.set('Content-Range', `bytes ${start}-${end}/${size}`)
  476. }
  477. ctx.length = Math.max(0, end - start + 1)
  478. ctx.body = size ? createReadStream(file.path, { start, end }) : Buffer.alloc(0)
  479. })
  480. const contextKey = (ctx: Koa.Context) =>
  481. JSON.stringify([ctx.query.profile ?? ctx.get('x-hermes-profile') ?? 'default', ctx.params.id])
  482. router.get('/api/app/session-context/:id', (ctx) => {
  483. ctx.body = { snapshot: store.get(owner(ctx), 'native-context', contextKey(ctx)) ?? null }
  484. })
  485. router.put('/api/app/session-context/:id', (ctx) => {
  486. const user = owner(ctx),
  487. key = contextKey(ctx)
  488. const value = parse(
  489. z
  490. .object({
  491. usedTokens: z.number().int().nonnegative(),
  492. limitTokens: z.number().int().positive().nullable().optional(),
  493. percent: z.number().nonnegative().nullable().optional(),
  494. compressions: z.number().int().nonnegative().nullable().optional(),
  495. model: z.string().max(512).nullable().optional(),
  496. provider: z.string().max(256).nullable().optional(),
  497. observedAt: z.number().nonnegative(),
  498. })
  499. .strict(),
  500. body(ctx),
  501. )
  502. const previous = store.get<{ observedAt: number }>(user, 'native-context', key)
  503. const snapshot =
  504. previous && previous.observedAt > value.observedAt
  505. ? previous
  506. : { ...value, sessionId: ctx.params.id, updatedAt: Date.now() / 1000 }
  507. store.put(user, 'native-context', key, snapshot)
  508. ctx.body = { snapshot }
  509. })
  510. registerVoice(router, store, nodes, owner, auth)
  511. return router
  512. }
  513. function registerVoice(
  514. router: Router,
  515. store: WorkspaceStore,
  516. vault: WorkspaceNodes,
  517. owner: (ctx: Koa.Context) => string,
  518. auth: LocalAuthStore,
  519. ): void {
  520. const read = (key: string): any => {
  521. const saved = store.get<string>('_system', 'voice', key)
  522. return saved ? vault.open(saved) : {}
  523. }
  524. const save = (key: string, value: unknown) =>
  525. store.put('_system', 'voice', key, vault.seal(value))
  526. const settings = () => ({
  527. voices: [],
  528. currentVoiceId: '',
  529. apiKey: '',
  530. updatedAt: 0,
  531. ...read('duplex'),
  532. })
  533. const publicSettings = () => {
  534. const { apiKey, ...value } = settings()
  535. return { ...value, hasApiKey: Boolean(apiKey) }
  536. }
  537. router.get('/api/app/admin/duplex-voice', (ctx) => {
  538. auth.requireAdmin(ctx)
  539. ctx.body = publicSettings()
  540. })
  541. router.put('/api/app/admin/duplex-voice', (ctx) => {
  542. auth.requireAdmin(ctx)
  543. const b = parse(
  544. z
  545. .object({
  546. apiKey: z.string().max(4096).optional(),
  547. voices: z
  548. .array(z.object({ id: z.string().min(1).max(200), name: z.string().min(1).max(200) }))
  549. .max(100),
  550. currentVoiceId: z.string().max(200),
  551. })
  552. .strict(),
  553. body(ctx),
  554. )
  555. if (
  556. new Set(b.voices.map((v) => v.id)).size !== b.voices.length ||
  557. (b.currentVoiceId && !b.voices.some((v) => v.id === b.currentVoiceId))
  558. )
  559. throw new HttpError(400, '音色设置无效')
  560. save('duplex', { ...settings(), ...b, updatedAt: Date.now() })
  561. ctx.body = publicSettings()
  562. })
  563. router.get('/api/app/voice/runtime', (ctx) => {
  564. const user = owner(ctx),
  565. value = settings()
  566. ctx.body = {
  567. ...value,
  568. currentVoiceId: store.get(user, 'voice-selection', 'current') ?? value.currentVoiceId,
  569. }
  570. })
  571. router.put('/api/app/voice/current-voice', (ctx) => {
  572. const user = owner(ctx),
  573. value = settings(),
  574. id = parse(z.object({ currentVoiceId: z.string() }).strict(), body(ctx)).currentVoiceId
  575. if (!value.voices.some((v: any) => v.id === id)) throw new HttpError(400, '音色不存在')
  576. store.put(user, 'voice-selection', 'current', id)
  577. ctx.body = { voices: value.voices, currentVoiceId: id, updatedAt: Date.now() }
  578. })
  579. for (const kind of ['tts', 'stt']) {
  580. const list = () => {
  581. const data = read(kind)
  582. return {
  583. activeProvider: data.activeProvider ?? (kind === 'tts' ? 'edge' : 'browser'),
  584. settings: Object.entries(data.providers ?? {}).map(([provider, v]: [string, any]) => ({
  585. provider,
  586. settings: v.settings,
  587. secrets: Object.fromEntries(Object.keys(v.secrets ?? {}).map((k) => [k, '[stored]'])),
  588. })),
  589. }
  590. }
  591. router.get(`/api/app/${kind}/settings`, (ctx) => {
  592. owner(ctx)
  593. ctx.body = list()
  594. })
  595. router.put(`/api/app/${kind}/settings/active`, (ctx) => {
  596. auth.requireAdmin(ctx)
  597. const data = read(kind),
  598. provider = parse(
  599. z.object({ provider: z.string().min(1).max(100) }).strict(),
  600. body(ctx),
  601. ).provider
  602. if (!data.providers?.[provider] && !['edge', 'browser'].includes(provider))
  603. throw new HttpError(400, '请先配置服务商')
  604. data.activeProvider = provider
  605. save(kind, data)
  606. ctx.body = list()
  607. })
  608. router.put(`/api/app/${kind}/settings/:provider`, (ctx) => {
  609. auth.requireAdmin(ctx)
  610. const input = parse(
  611. z
  612. .object({
  613. settings: z.record(z.string(), z.unknown()).default({}),
  614. secrets: z.record(z.string(), z.string().max(4096)).default({}),
  615. activeProvider: z.string().optional(),
  616. })
  617. .strict(),
  618. body(ctx),
  619. )
  620. if (JSON.stringify(input.settings).length > 32_000) throw new HttpError(413, '设置过大')
  621. const data = read(kind)
  622. data.providers ??= {}
  623. const old = data.providers[ctx.params.provider] ?? { settings: {}, secrets: {} }
  624. data.providers[ctx.params.provider] = {
  625. settings: { ...old.settings, ...input.settings },
  626. secrets: {
  627. ...old.secrets,
  628. ...Object.fromEntries(Object.entries(input.secrets).filter(([, v]) => v !== '[stored]')),
  629. },
  630. }
  631. if (input.activeProvider) data.activeProvider = input.activeProvider
  632. save(kind, data)
  633. ctx.body = list()
  634. })
  635. router.delete(`/api/app/${kind}/settings/:provider`, (ctx) => {
  636. auth.requireAdmin(ctx)
  637. const data = read(kind)
  638. if (data.providers) delete data.providers[ctx.params.provider]
  639. if (data.activeProvider === ctx.params.provider)
  640. data.activeProvider = kind === 'tts' ? 'edge' : 'browser'
  641. save(kind, data)
  642. ctx.body = list()
  643. })
  644. router.delete(`/api/app/${kind}/settings/:provider/secret/:secret`, (ctx) => {
  645. auth.requireAdmin(ctx)
  646. const data = read(kind)
  647. if (data.providers?.[ctx.params.provider]?.secrets)
  648. delete data.providers[ctx.params.provider].secrets[ctx.params.secret]
  649. save(kind, data)
  650. ctx.body = list()
  651. })
  652. router.delete(`/api/app/${kind}/settings/:provider/base-url-preset`, (ctx) => {
  653. auth.requireAdmin(ctx)
  654. const data = read(kind),
  655. s = data.providers?.[ctx.params.provider]?.settings
  656. if (s) s.baseUrlPresets = (s.baseUrlPresets ?? []).filter((v: string) => v !== ctx.query.url)
  657. save(kind, data)
  658. ctx.body = list()
  659. })
  660. }
  661. router.get('/api/app/voice/providers-info', (ctx) => {
  662. owner(ctx)
  663. ctx.body = {
  664. tts: ['edge', 'openai', 'custom', 'mimo', 'doubao'],
  665. stt: ['browser', 'openai', 'custom', 'doubao'],
  666. }
  667. })
  668. router.post('/api/app/voice/probe', async (ctx) => {
  669. auth.requireAdmin(ctx)
  670. const b = parse(
  671. z
  672. .object({
  673. kind: z.enum(['tts', 'stt']),
  674. provider: z.string().optional(),
  675. compatibility: z.enum(['manual', 'openai-compatible']).default('openai-compatible'),
  676. baseUrl: z.string().url(),
  677. apiKey: z.string().max(4096).optional(),
  678. })
  679. .strict(),
  680. body(ctx),
  681. ),
  682. url = new URL(b.baseUrl)
  683. if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password)
  684. throw new HttpError(400, '服务地址无效')
  685. if (b.compatibility === 'manual') {
  686. ctx.body = { ok: true, models: [], normalizedBaseUrl: b.baseUrl, manualModelAllowed: true }
  687. return
  688. }
  689. url.pathname = `${url.pathname.replace(/\/$/, '')}/models`
  690. const response = await fetch(url, {
  691. headers: b.apiKey ? { Authorization: `Bearer ${b.apiKey}` } : {},
  692. signal: AbortSignal.timeout(10_000),
  693. redirect: 'error',
  694. })
  695. const value = (await response.json()) as any
  696. ctx.body = {
  697. ok: response.ok,
  698. models: Array.isArray(value.data)
  699. ? value.data.slice(0, 500).map((m: any) => String(m.id))
  700. : [],
  701. normalizedBaseUrl: b.baseUrl,
  702. manualModelAllowed: true,
  703. errorSummary: response.ok ? '' : `HTTP ${response.status}`,
  704. }
  705. })
  706. }