dashboard-model.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import type {
  2. ExecutionStatus,
  3. InteractionEvent,
  4. InteractionMessage,
  5. InteractionSession,
  6. } from "@actalk/inkos-core";
  7. import { formatModeLabel, normalizeStageLabel, type TuiCopy } from "./i18n.js";
  8. export interface DashboardMessageRow {
  9. readonly key: string;
  10. readonly label: string;
  11. readonly role: InteractionMessage["role"];
  12. readonly content: string;
  13. }
  14. export interface DashboardEventRow {
  15. readonly key: string;
  16. readonly status: ExecutionStatus;
  17. readonly summary: string;
  18. }
  19. export interface DashboardViewModel {
  20. readonly projectName: string;
  21. readonly activeBookTitle?: string;
  22. readonly modelLabel: string;
  23. readonly modeLabel: string;
  24. readonly executionStatus: ExecutionStatus;
  25. readonly executionLabel: string;
  26. readonly headerLine: string;
  27. readonly statusPrimaryLine: string;
  28. readonly statusSecondaryLine: string;
  29. readonly messageRows: ReadonlyArray<DashboardMessageRow>;
  30. readonly eventRows: ReadonlyArray<DashboardEventRow>;
  31. readonly pendingDecisionSummary?: string;
  32. readonly composerPlaceholder: string;
  33. readonly composerHelper: string;
  34. readonly composerStatus: string;
  35. readonly errorText?: string;
  36. }
  37. export interface BuildDashboardViewModelParams {
  38. readonly projectName: string;
  39. readonly activeBookTitle?: string;
  40. readonly modelLabel: string;
  41. readonly depthLabel?: string;
  42. readonly copy: TuiCopy;
  43. readonly session: InteractionSession;
  44. readonly isSubmitting: boolean;
  45. readonly lastError?: string;
  46. readonly sinceTimestamp?: number;
  47. readonly terminalRows?: number;
  48. readonly scrollOffset?: number;
  49. }
  50. export function buildDashboardViewModel(params: BuildDashboardViewModelParams): DashboardViewModel {
  51. const status = params.session.currentExecution?.status ?? "idle";
  52. const executionLabel = normalizeStageLabel(params.session.currentExecution?.stageLabel ?? status, params.copy);
  53. const modeLabel = formatModeLabel(params.session.automationMode, params.copy);
  54. const bookLabel = params.activeBookTitle ?? params.session.activeBookId ?? params.copy.labels.none;
  55. const draftTitle = params.session.creationDraft?.title;
  56. const draftQuestion = params.session.creationDraft?.nextQuestion;
  57. const sinceTimestamp = params.sinceTimestamp ?? 0;
  58. const terminalRows = params.terminalRows ?? process.stdout.rows ?? 24;
  59. const conversationLimit = Math.max(4, terminalRows - 10);
  60. const filteredMessages = params.session.messages
  61. .filter((message) => message.timestamp >= sinceTimestamp);
  62. const scrollOffset = params.scrollOffset ?? 0;
  63. const endIndex = filteredMessages.length - scrollOffset;
  64. const startIndex = Math.max(0, endIndex - conversationLimit);
  65. const messageRows = filteredMessages
  66. .slice(startIndex, endIndex > 0 ? endIndex : undefined)
  67. .map((message, index) => ({
  68. key: `${message.timestamp}-${index}`,
  69. label: roleLabel(message.role, params.copy),
  70. role: message.role,
  71. content: message.content,
  72. }));
  73. const eventRows = params.session.events
  74. .filter((event) => event.timestamp >= sinceTimestamp)
  75. .slice(-3)
  76. .map((event, index) => ({
  77. key: `${event.timestamp}-${index}`,
  78. status: event.status,
  79. summary: summarizeEvent(event, params.copy),
  80. }));
  81. const latestEventSummary = eventRows[eventRows.length - 1]?.summary;
  82. return {
  83. projectName: params.projectName,
  84. activeBookTitle: params.activeBookTitle ?? params.session.activeBookId,
  85. modelLabel: params.modelLabel,
  86. modeLabel,
  87. executionStatus: status,
  88. executionLabel,
  89. headerLine: [
  90. `${params.copy.labels.project} ${params.projectName}`,
  91. `${params.copy.labels.book} ${bookLabel}`,
  92. draftTitle ? `${params.copy.labels.draft} ${draftTitle}` : undefined,
  93. `${params.copy.labels.depth} ${params.depthLabel ?? params.copy.depthLabels.normal}`,
  94. `${params.copy.labels.session} ${params.session.sessionId.slice(-4)}`,
  95. params.copy.labels.messageCount(params.session.messages.length),
  96. ].filter(Boolean).join(" · "),
  97. statusPrimaryLine: `${params.copy.labels.stage} ${executionLabel} · ${params.copy.labels.model} ${params.modelLabel}`,
  98. statusSecondaryLine: params.lastError
  99. ? `${params.copy.labels.error} · ${compactInline(params.lastError)}`
  100. : params.isSubmitting && latestEventSummary
  101. ? `${params.copy.labels.recent} · ${latestEventSummary}`
  102. : params.session.pendingDecision?.summary
  103. ? `${params.copy.labels.pending} · ${compactInline(params.session.pendingDecision.summary)}`
  104. : draftQuestion
  105. ? `${params.copy.labels.draft} · ${compactInline(draftQuestion)}`
  106. : draftTitle
  107. ? `${params.copy.labels.draft} · ${draftTitle}`
  108. : latestEventSummary
  109. ? `${params.copy.labels.recent} · ${latestEventSummary}`
  110. : `${params.copy.labels.ready} · ${bookLabel}`,
  111. messageRows,
  112. eventRows,
  113. pendingDecisionSummary: params.session.pendingDecision?.summary,
  114. composerPlaceholder: params.copy.composer.placeholder,
  115. composerHelper: params.copy.composer.helper,
  116. composerStatus: params.isSubmitting
  117. ? params.copy.composer.submitting
  118. : params.lastError
  119. ? params.copy.composer.failed
  120. : params.copy.composer.ready,
  121. errorText: params.lastError,
  122. };
  123. }
  124. function roleLabel(role: InteractionMessage["role"], copy: TuiCopy): string {
  125. switch (role) {
  126. case "user":
  127. return copy.roles.user;
  128. case "assistant":
  129. return copy.roles.assistant;
  130. case "system":
  131. return copy.roles.system;
  132. default:
  133. return role;
  134. }
  135. }
  136. function summarizeEvent(event: InteractionEvent, copy: TuiCopy): string {
  137. const base = compactInline(event.detail?.trim() || event.kind);
  138. if (event.bookId && event.chapterNumber !== undefined) {
  139. const chapterLabel = copy.locale === "zh-CN"
  140. ? `第 ${event.chapterNumber} 章`
  141. : `ch.${event.chapterNumber}`;
  142. return `${base} (${event.bookId} ${chapterLabel})`;
  143. }
  144. if (event.bookId) {
  145. return `${base} (${event.bookId})`;
  146. }
  147. return base;
  148. }
  149. function compactInline(value: string): string {
  150. const singleLine = value.replace(/\s+/g, " ").trim();
  151. return singleLine.length > 72 ? singleLine.slice(0, 69) + "..." : singleLine;
  152. }