chatPresentation.ts 1.7 KB

123456789101112131415161718192021222324252627282930
  1. import type { WorkspaceEvent, WorkspaceMessage } from '../shared/workspace.js'
  2. /** Display projections never change the durable history or model context. */
  3. export function sessionForList(value: Record<string, unknown>): Record<string, unknown> {
  4. const { system_prompt: _prompt, systemPrompt: _camelPrompt, tool_names: _tools, toolNames: _camelTools, ...summary } = value
  5. return summary
  6. }
  7. export function workspaceMessageForClient(message: WorkspaceMessage): WorkspaceMessage {
  8. return { ...message, tools: message.tools.map((tool, index) => {
  9. if (Buffer.byteLength(JSON.stringify(tool)) <= 8192) return tool
  10. const summary: Record<string, unknown> = {}
  11. for (const key of ['id', 'name', 'tool_name', 'status', 'duration_seconds', 'durationSeconds', 'duration_s']) {
  12. if (tool[key] !== undefined) summary[key] = tool[key]
  13. }
  14. if (!summary.status && (tool.result !== undefined || tool.output !== undefined)) summary.status = 'completed'
  15. if (typeof tool.error === 'string') summary.error = tool.error.slice(0, 512)
  16. if (typeof tool.preview === 'string') summary.preview = tool.preview.slice(0, 512)
  17. const query = new URLSearchParams({ revision: String(message.revision ?? 0) })
  18. if (tool.id != null) query.set('toolId', String(tool.id))
  19. summary.detailsUrl = `/api/app/conversations/${encodeURIComponent(message.conversationId)}/messages/${encodeURIComponent(message.id)}/tools/${index}?${query}`
  20. return summary
  21. }) }
  22. }
  23. export function workspaceEventForClient(event: WorkspaceEvent): WorkspaceEvent {
  24. return event.type === 'message.changed'
  25. ? { ...event, data: workspaceMessageForClient(event.data as WorkspaceMessage) }
  26. : event
  27. }