models.ts 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. /**
  2. * 领域模型 —— 前端唯一权威类型定义
  3. *
  4. * 依据《数据访问层设计.md》§三。云函数返回的原始形状由 `ParseApi` 的映射函数
  5. * 转成本文件定义的模型,页面只见模型,不见原始字段。
  6. *
  7. * 🔴 禁止在页面/组件内另建同义类型;禁止把云函数的原始 JSON 形状泄漏到视图层。
  8. */
  9. // ---------- 通用 ----------
  10. /** 加载状态三态 + 空闲 */
  11. export type LoadState = 'idle' | 'loading' | 'ready' | 'empty' | 'error';
  12. // ---------- 数字生命 ----------
  13. /** 数字生命运行状态(与云函数 xlosi3m46q 的 status 取值一致) */
  14. export type AgentStatus = 'running' | 'idle' | 'error' | 'offline' | 'stopped';
  15. export interface HostMetrics {
  16. cpuPercent?: number;
  17. memPercent?: number;
  18. diskPercent?: number;
  19. }
  20. export interface Balance {
  21. /** 服务端换算后的余额(元)。映射层负责归一:兼容旧键 remainRmb 后统一写入此处 */
  22. remainCny?: number;
  23. /** 已用金额(元)。云函数一直有给,映射层此前丢弃(2026-11-11 补) */
  24. usedCny?: number;
  25. /** 业务再算页面外链(FR-07 AC-07.4) */
  26. pageUrl?: string;
  27. /**
  28. * 是否为**主人账号**的共用余额(2026-11-11)。
  29. *
  30. * 🔴 容器账单挂在主人账号上,同一主人的所有容器报的是**同一个**数,
  31. * 不是「本容器剩余额度」。UI 必须据此标注,不能让用户误读。
  32. */
  33. sharedAccount?: boolean;
  34. }
  35. export interface ChannelConfig {
  36. matrix?: boolean;
  37. homeserver?: string;
  38. matrixConfig?: {
  39. userId?: string;
  40. homeserver?: string;
  41. serverName?: string;
  42. /**
  43. * 容器侧上报的通道状态(实测数据里有,此前类型未声明)。
  44. * `'connected'` = 容器 Matrix 通道在跑;缺失 = 未上报(可能离线)。
  45. */
  46. channelState?: string;
  47. /** listener 健康检查通过(旧字段,与 channelState 二选一为真即在线) */
  48. listenerOk?: boolean;
  49. /** 更细的状态明细(gateway / matrix / wecom 各自状态) */
  50. channelDetail?: {
  51. matrixState?: string;
  52. gatewayState?: string;
  53. wecomState?: string;
  54. codeVersion?: string;
  55. source?: string;
  56. };
  57. };
  58. /** 企业微信通道是否点亮(数组形态归一后得出) */
  59. wecom?: boolean;
  60. /** 企微机器人 BotID(数组形态下仅知通道名、无此值,故可选) */
  61. wecomBotId?: string;
  62. }
  63. /**
  64. * Token 用量。
  65. * 🔴 映射口径:云函数返回的 `usage` / `usageSessions` 是**字符串形态的 JSON**,
  66. * 且字段名未与服务端逐字段核对(《API-契约》§六-4)。映射函数负责
  67. * ① 兼容「已是对象 / 是 JSON 字符串」;② 归一字段名;③ 未识别字段保留在 raw。
  68. */
  69. export interface Usage {
  70. promptTokens?: number;
  71. completionTokens?: number;
  72. totalTokens?: number;
  73. updatedAt?: string;
  74. /** 原始未归一字段(排查用,UI 不直接消费) */
  75. raw?: Record<string, unknown>;
  76. }
  77. /** 单次会话的 token 用量(云函数 `usageSessions` 元素,契约见 reporter 字段规范 §3.4) */
  78. export interface UsageSession {
  79. sessionId?: string;
  80. title?: string;
  81. model?: string;
  82. messageCount?: number;
  83. toolCallCount?: number;
  84. inputTokens?: number;
  85. outputTokens?: number;
  86. cacheReadTokens?: number;
  87. reasoningTokens?: number;
  88. estimatedCostUsd?: number;
  89. costStatus?: string;
  90. /** epoch 数字(服务端不转换) */
  91. startedAt?: number;
  92. }
  93. /**
  94. * 容器上报的**单个会话**运行状态(2026-11-09,方案 A)。
  95. *
  96. * 由容器在 `pre_llm_call` / `post_llm_call` 钩子里维护,写进 `extra.currentSessions`。
  97. * `sessionKey` 是容器的 `build_session_key()` 产物,形如:
  98. * `agent:main:matrix:dm:<!roomId>[:<threadId>]`
  99. *
  100. * 🔴 App 的会话 id 是 Matrix 的 **thread 根 event_id**,与这里的 `sessionKey`
  101. * 是否对得上**必须实测验证** —— 对不上就是「报了但匹配不到会话」。
  102. */
  103. export interface AgentSessionState {
  104. /** 容器的 session_key 原文 */
  105. sessionKey: string;
  106. /**
  107. * 发起该会话的用户 mxid(2026-11-11 新增,容器按需补)。
  108. *
  109. * 🔴 为什么需要:容器**可能被多人共享**(同公司同事都能用)。
  110. * `currentSessions` 是容器级的一个列表,A 在跑任务时 B 也会看到这条记录。
  111. * 没有 user 信息,App 只能按 roomId 猜「这条是不是我的」——
  112. * 不同用户的 DM 房间不同,roomId 天然隔离;但**群聊里多人共用一个 thread** 时
  113. * roomId 相同,就无法区分「是在为谁跑」。
  114. *
  115. * 容器查 `state.db.sessions.user_id` 得到;查不到时**省略该字段**
  116. * (App 按「无 user 信息」处理 → 退回 roomId 匹配,不误判)。
  117. */
  118. userId?: string;
  119. /**
  120. * 本会话本轮的任务摘要(2026-11-11 新增)。
  121. *
  122. * 容器取 `pre_llm_call` 里的 `user_message` 首行,截断到 60 字。
  123. * **只在 `status === 'running'` 时有意义**;idle 时容器可能省略。
  124. *
  125. * 🔴 为什么需要:容器级的 `currentTask` 没有会话归属 —— 容器跑会话 A 时,
  126. * 用户在会话 B 也会看到 A 的任务名(用户实测反馈)。
  127. * 这个字段让顶栏能精确显示「**本会话**在做什么」。
  128. */
  129. task?: string;
  130. /** 该会话当前是否在跑 */
  131. status: 'running' | 'idle';
  132. /** 本轮开始的 epoch 毫秒(idle 时可缺省) */
  133. turnStartAt?: number;
  134. /** 容器真实上报的任务阶段、当前动作和进度;App 不推算这些字段。 */
  135. stage?: string;
  136. action?: string;
  137. /** 只有任务源能给出真实进度时才提供,取值 0–100。 */
  138. progress?: number;
  139. /**
  140. * 该条记录最后更新的 epoch 毫秒(2026-11-11 新增)。
  141. *
  142. * 🔴 用途:**判断 `running` 是否已经过期**。
  143. *
  144. * 用户实测:容器上报本会话 `running`,但 turn 其实早已结束,
  145. * 界面一直挂着「正在处理」+ 左侧一直转圈。根因是 `currentSessions`
  146. * 只在 turn 首尾写,中间不刷新;若容器恰好漏了结束那一次,状态就永久卡住。
  147. *
  148. * App 侧据此兜底:`running` 且 `updatedAt` 已经很久没变 → 视为过期,
  149. * 不再相信它。没有这个字段时不做过期判定(老容器不受影响)。
  150. */
  151. updatedAt?: number;
  152. }
  153. export interface Person {
  154. id: string;
  155. name: string;
  156. avatar?: string;
  157. mobile?: string;
  158. /** 同事行展开时用 */
  159. agents?: Agent[];
  160. /**
  161. * 所属公司 id(`Company.objectId`)。
  162. * 通讯录按公司分组时使用;缺失时归入「未归属」。
  163. */
  164. companyId?: string;
  165. /**
  166. * 所属部门。
  167. *
  168. * 🔴 **预留字段**(2026-11-04):服务端目前没有部门维度,
  169. * 该值恒为 undefined,通讯录会归入「未分组」。
  170. * 待组织架构落地后由 `_User` / `Company` 表补充,前端不用改结构。
  171. */
  172. department?: string;
  173. }
  174. export interface Agent {
  175. id: string;
  176. name: string;
  177. avatar?: string;
  178. /** 专长描述(原型 specialty) */
  179. specialty?: string;
  180. gender?: 'male' | 'female' | 'unknown';
  181. status: AgentStatus;
  182. /** 状态中文文案(由 core/format 统一给出,不在组件里拼) */
  183. statusText?: string;
  184. currentTask?: string;
  185. currentAction?: string;
  186. progress?: number;
  187. heartbeatAt?: string;
  188. /**
  189. * 心跳距今**秒数**(2026-11-07 新增)。
  190. *
  191. * 云函数 `xlosi3m46q` 直接返回这个字段(实测它有值,而 `heartbeatAt` 为 undefined)。
  192. * 容器掉线后**不会**把 `status` 改成 offline,只会停止心跳 ——
  193. * 实测两个容器 `heartbeatAgeSec = 3914610`(45 天)却仍报 `running`。
  194. * 状态展示(`core/agent-state.ts`)用它判定「其实已离线」。
  195. */
  196. heartbeatAgeSec?: number;
  197. updatedAt?: string;
  198. host?: HostMetrics;
  199. balance?: Balance;
  200. channels?: ChannelConfig;
  201. usage?: Usage;
  202. /**
  203. * 会话级用量明细(云函数 `usageSessions`,JSON 字符串形态)。
  204. * 用于「Token 用量」下钻,缺失时不影响总量展示。
  205. */
  206. usageSessions?: UsageSession[];
  207. owner?: Person;
  208. companyId?: string;
  209. /**
  210. * 会话级运行状态(2026-11-09,方案 A)。
  211. *
  212. * 🔴 为什么需要:容器上报的 `status` 是**整个容器一个值** ——
  213. * 同一容器的多个会话共用它,于是「会话 A 在跑」时,会话 B 的界面也显示
  214. * 「正在执行」(用户实测)。
  215. *
  216. * 容器把每个会话的状态塞进 `extra.currentSessions`(`extra` 是平台
  217. * 云函数字段白名单里的合法字段,无需改云函数),这里解析出来供按会话展示。
  218. *
  219. * 容器尚未上报时为 `undefined` —— 调用方必须回落到全局 `status`,
  220. * 不能因为缺这个字段就显示「未知」。
  221. */
  222. currentSessions?: AgentSessionState[];
  223. /** 所属部门 —— **预留字段**(2026-11-04)。
  224. * 服务端暂无此维度,恒为 undefined;通讯录归入「未分组」。
  225. */
  226. department?: string;
  227. /** 容器标识(云函数 `containerRef`,Docker 短 ID / 主机名,排障时用来反查来源) */
  228. containerRef?: string;
  229. /**
  230. * Agent 自身 token 健康灯(云函数 `sessionStatus`)。
  231. * `ok` / `expired` / `unknown` —— expired 时余额等依赖 token 的字段会降级。
  232. */
  233. sessionStatus?: 'ok' | 'expired' | 'unknown';
  234. /** 是否已接入 FMODE(决定「进会话 vs 进详情」,见 AgentRouteService) */
  235. matrixReady: boolean;
  236. /** 服务端确认已完成交付;与当前是否在线无关。缺省兼容历史接入数据。 */
  237. provisioned?: boolean;
  238. /**
  239. * 套餐来源(2026-12-03)。
  240. *
  241. * 🔴 为什么要这个字段:FmodeAgent 原来**没有任何**能区分「平台创建的容器」
  242. * 和「用户在自己机器上接入的容器」的字段,列表里两者长得一模一样。
  243. * 现在由开通云函数在创建时写入:
  244. * · `trial` / `starter` → 用户本地容器(自带 Hermes 接入 App)
  245. * · `advanced` / `enterprise` → 平台容器(R730 上创建)
  246. * 历史记录没有此字段时回落到「有 job 即平台」,见 `edition.containerKind()`。
  247. */
  248. planCode?: 'trial' | 'starter' | 'advanced' | 'enterprise';
  249. /** 试用截止时间(ISO)。仅 trial 有;时钟从首次创建起算,复用不重置。 */
  250. trialEndsAt?: string;
  251. /** 试用是否已过期(云函数按当前时间派生)。过期后 App 端不再放行进入。 */
  252. trialExpired?: boolean;
  253. /** 会话未读数 */
  254. unread: number;
  255. }
  256. // ---------- 会话与消息 ----------
  257. /**
  258. * 会话。
  259. *
  260. * ## 两种形态(2026-10-21 新增群聊)
  261. *
  262. * | kind | 说明 | 用到哪些字段 |
  263. * |---|---|---|
  264. * | `dm` | 用户 ↔ 单个数字生命 | `agentId` / `peer` |
  265. * | `group` | 一个房间里有多个数字生命 | `members` / `name` |
  266. *
  267. * 🔴 群聊里**同一个房间里会有多个 agent 发言**,所以消息必须能标出「是谁说的」。
  268. * 此前消息只分 `me` / `agent`,无法区分多个 agent(见 `Message.senderId`)。
  269. */
  270. export interface Conversation {
  271. /** Matrix roomId(演示态为合成 id) */
  272. id: string;
  273. /** 会话形态;缺省视为 dm(兼容旧数据) */
  274. kind?: 'dm' | 'group';
  275. /** dm:对方的 agentId。群聊时为空 */
  276. agentId?: string;
  277. /** dm:对方 */
  278. peer?: Agent;
  279. /** group:群名称 */
  280. name?: string;
  281. /** group:房间内的 agent 成员(不含自己) */
  282. members?: Agent[];
  283. lastMessagePreview?: string;
  284. lastMessageAt?: string;
  285. unread: number;
  286. }
  287. export type MessageKind = 'text' | 'notice' | 'emote';
  288. export type MessageState = 'sending' | 'sent' | 'failed';
  289. export interface FileCard {
  290. kind: 'file';
  291. name: string;
  292. size: number;
  293. mimeType: string;
  294. url: string;
  295. thumb?: string;
  296. /**
  297. * 云盘(OBS/S3)公网地址(2026-11-09)。
  298. *
  299. * 用户要求「之后查看都走云盘」:有这个地址就优先用它,
  300. * 没有则回落到 Matrix 的 mxc 转换地址。缺失说明当时 S3 上传失败。
  301. */
  302. cloudUrl?: string;
  303. }
  304. export interface Message {
  305. id: string;
  306. conversationId: string;
  307. /**
  308. * 发送方类别。
  309. * 🔴 `agent` 不区分具体是哪个 agent —— 群聊里要结合 `senderId` / `senderName` 才能区分。
  310. */
  311. sender: 'me' | 'agent';
  312. /** 群聊:具体发送者的 Matrix ID(dm 下可省略) */
  313. senderId?: string;
  314. /** 群聊:展示用名字(agent 的 botName,或人类昵称) */
  315. senderName?: string;
  316. /** 这条消息 @ 了哪些人(Matrix ID)。群聊 UI 用来高亮「在叫我 / 叫我同事」 */
  317. mentions?: string[];
  318. kind: MessageKind;
  319. text?: string;
  320. file?: FileCard;
  321. /** 同一条容器回复里的多份成果;file 保留首份以兼容单附件调用方。 */
  322. files?: FileCard[];
  323. artifactId?: string;
  324. taskId?: string;
  325. ts: string;
  326. state: MessageState;
  327. /** 失败原因(AC-04.8:失败消息不丢失,可重试) */
  328. error?: string;
  329. /**
  330. * 发送时写入 Matrix 事件的本地 id(`com.fmode.local`)。
  331. *
  332. * 服务端 echo 会原样带回它 —— 回读时据此**认领**本地那条乐观消息,
  333. * 避免同一句话同时以「本地一条 + 回读一条」出现两遍(2026-11-09)。
  334. * 容器 / 历史事件没有这个字段。
  335. */
  336. localId?: string;
  337. /**
  338. * 这条消息**回复的是哪一条**(Matrix `m.in_reply_to.event_id`)。
  339. *
  340. * 🔴 2026-11-10:容器对每条用户消息分别回话(图文被拆成两条时各跑一轮),
  341. * 回合聚合靠它把回复归到正确的那条提问上。
  342. */
  343. replyTo?: string;
  344. /**
  345. * 容器声明的消息类别(2026-11-11,Matrix `content["fmode.kind"]`)。
  346. *
  347. * ## 为什么需要
  348. *
  349. * App 一直靠**文本启发式**区分「工具执行过程」与「正文」
  350. * (emoji 前缀、`· 工具名` 行、裸命令行),正文里出现代码块或命令行就会误判
  351. * —— 这是「日志吞正文」反复出问题的根源。
  352. *
  353. * 容器侧(lj-test)核查后确认:网关的 `_interim_send` 标记在 Matrix adapter
  354. * **没有被写进消息内容**,所以 App 拿不到任何结构化信号。
  355. * 已请容器在 `send()` 里把它落成 `content["fmode.kind"]`。
  356. *
  357. * ## 语义
  358. *
  359. * - `'process'`:工具执行/进度行,应进折叠区
  360. * - `'final'`:正文/结论,应直接显示
  361. * - **缺省**:容器未改造 → App **回退到启发式**(保证老容器照常可用)
  362. */
  363. fmodeKind?: 'process' | 'final';
  364. }
  365. // ---------- 成果与项目 ----------
  366. export interface Artifact {
  367. name: string;
  368. url: string;
  369. type: string;
  370. /**
  371. * 容器内的原始路径(非 URL 时才有)。
  372. * 该 artifact **尚未上传到云盘**,由 `ArtifactSyncService` 触发同步;
  373. * `url` 为空时 UI 不得提供下载(避免给一个打不开的链接)。
  374. */
  375. source?: string;
  376. }
  377. export type DeliverableState = 'delivered' | 'failed';
  378. /**
  379. * 文件台账条目(云函数 fmode-file-hub,2026-11-10)
  380. *
  381. * 用户裁决:消息不入库,只有**文件与成果**入库,让右侧栏能按会话直接加载。
  382. * 只存访问链接与元信息,不存文件内容。
  383. */
  384. export interface FileEntry {
  385. /** 会话归属(App 的会话 id = Matrix thread 根),核心字段 */
  386. threadRoot: string;
  387. /** 产出的容器;用户上传的文件也要带,便于按容器聚合 */
  388. agentId?: string;
  389. roomId?: string;
  390. name: string;
  391. /** 云盘访问链接(https,必须是 s3.fmode.cn / file-cloud.fmode.cn) */
  392. url: string;
  393. /** 云盘对象键,url 只是它的投影 */
  394. key?: string;
  395. mimeType?: string;
  396. size?: number;
  397. /** 谁放进来的:用户自己传的,还是容器产出的 */
  398. uploadedBy: 'user' | 'agent';
  399. /** 容器内的原始路径(仅容器产出时有) */
  400. source?: string;
  401. /**
  402. * 所属群会话 id(2026-11-11,v0.2.0)。
  403. *
  404. * 伪群聊下容器只知道 DM 房间,所以只有 **用户上传** 能直接带这个值;
  405. * 容器产出靠 App 锚点反查或惰性补归并补上。缺省 = 不属于任何群(单聊)。
  406. */
  407. groupId?: string;
  408. /** 群归属的来源:'app'(直接告知)/ 'anchor'(派发锚点匹配)/ 'claim'(补归并) */
  409. groupIdSource?: string;
  410. }
  411. /** 从文件台账拉回的一条记录(服务端已归一化) */
  412. export interface SessionFile {
  413. id: string;
  414. threadRoot: string;
  415. agentId: string;
  416. roomId: string;
  417. name: string;
  418. /** 未知大小用 -1(与云函数口径一致) */
  419. size: number;
  420. mimeType: string;
  421. url: string;
  422. storageKey: string;
  423. uploadedBy: 'user' | 'agent';
  424. status: string;
  425. /** epoch 毫秒 */
  426. deliveredAt: number;
  427. /** 所属群会话 id(2026-11-11,v0.2.0);'' = 不属于任何群 */
  428. groupId?: string;
  429. }
  430. export interface Deliverable {
  431. id: string;
  432. agentId: string;
  433. title: string;
  434. subtitle?: string;
  435. summary?: string;
  436. projectId?: string;
  437. projectName?: string;
  438. artifacts: Artifact[];
  439. tags: string[];
  440. deliveredAt: string;
  441. state: DeliverableState;
  442. /** 成果正文(演示态用;真实态由 artifacts 外链承载) */
  443. content?: string;
  444. /** 文件类型标签(DOC / CSV …) */
  445. fileType?: string;
  446. filename?: string;
  447. size?: string;
  448. /**
  449. * 文件字节数(2026-11-11)。
  450. *
  451. * 🔴 与上面 `size?: string` 分工不同:`size` 是**展示字符串**("4.7 KB"),
  452. * 不可逆、不能参与去重;这里是**原始字节数**,用于:
  453. * · 右栏「文件」显示大小
  454. * · 两路数据(台账 / 消息流)按「名字+大小」去重
  455. * 未知时缺省(不是 0)——0 会被当成「真的 0 字节」。
  456. */
  457. sizeBytes?: number;
  458. version?: string;
  459. }
  460. export interface DeliverableGroup {
  461. project?: string;
  462. projectName: string;
  463. count: number;
  464. items: Deliverable[];
  465. }
  466. export interface Project {
  467. id: string;
  468. name: string;
  469. code?: string;
  470. description?: string;
  471. brief?: string;
  472. status?: string;
  473. owner?: string;
  474. due?: string;
  475. updated?: string;
  476. agentIds: string[];
  477. deliverableIds: string[];
  478. hasBlocked: boolean;
  479. }
  480. // ---------- 事件(派生,非独立表 —— SPEC FR-06 口径) ----------
  481. export type EventCategory = 'delivery' | 'execution' | 'alert';
  482. export interface EventItem {
  483. id: string;
  484. category: EventCategory;
  485. title: string;
  486. description?: string;
  487. ts: string;
  488. agentId: string;
  489. artifactId?: string;
  490. taskId?: string;
  491. }
  492. // ---------- 任务看板(FR-07b) ----------
  493. export type TaskStatus =
  494. | 'triage'
  495. | 'todo'
  496. | 'scheduled'
  497. | 'ready'
  498. | 'running'
  499. | 'blocked'
  500. | 'done';
  501. export interface TaskRun {
  502. id: string;
  503. status: string;
  504. outcome?: string;
  505. stepKey?: string;
  506. title?: string;
  507. detail?: string;
  508. summary?: string;
  509. error?: string;
  510. startedAt?: string;
  511. endedAt?: string;
  512. }
  513. export interface TaskComment {
  514. id: string;
  515. author?: string;
  516. body: string;
  517. createdAt: string;
  518. }
  519. export interface TaskAttachment {
  520. id: string;
  521. filename: string;
  522. size?: number;
  523. contentType?: string;
  524. /** 可选下载地址;真实接口未提供时保持空值,避免伪造下载能力。 */
  525. url?: string;
  526. }
  527. export interface Task {
  528. id: string;
  529. agentId: string;
  530. projectId?: string;
  531. title: string;
  532. status: TaskStatus;
  533. assignee?: string;
  534. owner?: string;
  535. branchName?: string;
  536. body?: string;
  537. blockKind?: string;
  538. consecutiveFailures?: number;
  539. /** 已阻塞时长(小时,用于「已阻塞 436h」表达) */
  540. blockedHours?: number;
  541. createdAt?: string;
  542. updatedAt?: string;
  543. startedAt?: string;
  544. completedAt?: string;
  545. runs: TaskRun[];
  546. comments: TaskComment[];
  547. attachments: TaskAttachment[];
  548. }
  549. // ---------- 工作组(多容器协作) ----------
  550. /**
  551. * 工作组 —— 一次任务的临时组合(员工团队)。
  552. *
  553. * 与 `Conversation` 的区别:会话是「和谁聊」,工作组是「谁在一起干活」。
  554. * 数据源是云函数 `fmode-workgroup`(服务端建房),不是 Matrix 直读。
  555. */
  556. export interface WorkGroup {
  557. id: string;
  558. name: string;
  559. goal?: string;
  560. roomId: string;
  561. /** 成员 agentId 列表 */
  562. members: string[];
  563. status?: string;
  564. }
  565. // ---------- 登录 ----------
  566. export interface Me {
  567. id: string;
  568. name: string;
  569. mobile?: string;
  570. avatar?: string;
  571. companyId?: string;
  572. /**
  573. * 所属组织名(Company.name)。
  574. *
  575. * 🔴 此前「我的」页显示硬编码的「企业空间」——是**假文案**。
  576. * 实测 `Company` 表可拿到 `name`(如 `E4KpGvTEto → 飞码AI`),故改为真实数据。
  577. * 取不到时为 `undefined`,UI 显示「—」而非编造。
  578. */
  579. companyName?: string;
  580. /** 仅用于决定请求哪个 scope 与界面提示;**不得**作为授权依据(FR-20 AC-20.2) */
  581. roles?: string[];
  582. }
  583. export interface AuthSession {
  584. token: string;
  585. me: Me;
  586. }
  587. // ---------- 组织成员管理(2026-11-10) ----------
  588. /** 组织成员(管理员用户管理页的一行) */
  589. export interface OrgUser {
  590. id: string;
  591. username: string;
  592. nickname: string;
  593. mobile: string;
  594. /** 是否已软删(列表默认不返回已删的) */
  595. isDeleted: boolean;
  596. /** 创建时间(epoch ms) */
  597. createdAt: number;
  598. }
  599. /** 成员分页结果 */
  600. export interface OrgUserPage {
  601. items: OrgUser[];
  602. total: number;
  603. page: number;
  604. pageSize: number;
  605. }
  606. /**
  607. * 当前账号的组织归属状态(2026-11-10)。
  608. *
  609. * 🔴 `hasOrg === false` 表示「未加入任何正式组织」—— 包括两种情形:
  610. * · 完全没有组织(companyId 为空)
  611. * · **只有注册时自动创建的个人试用组织**(名字=用户名,带 30 天试用)
  612. * 判定在服务端完成,前端只消费结果,不自行推断。
  613. */
  614. export interface OrgMembership {
  615. userId: string;
  616. hasOrg: boolean;
  617. companyId: string;
  618. companyName: string;
  619. }
  620. // ---------- Matrix 开通与上传(字段与《API-契约》§2.3/§2.5 对齐) ----------
  621. /** 对应云函数 RP5Ht55NeX 的返回 */
  622. export interface MatrixProvision {
  623. cached?: boolean;
  624. bindingId?: string;
  625. status?: string;
  626. userMxId: string | null;
  627. agentMxId: string | null;
  628. homeserver: string;
  629. accessToken: string;
  630. deviceId: string;
  631. roomId: string | null;
  632. userCreated?: boolean;
  633. }
  634. /**
  635. * 成果文件搬运任务(表 `FmodeArtifactJob`)。
  636. *
  637. * 由 **App(前端)** 创建、由 **容器** 领取执行:
  638. *
  639. * ```
  640. * App: 发现成果附件只有容器路径 → 签好 PUT 地址 → 建任务(pending)
  641. * 容器: 轮询 pending → 读 source → 按 headers 原样 PUT 到 uploadUrl → 回执 done
  642. * App: 下一次刷新时,成果的 artifacts[i].url 已变成云盘地址
  643. * ```
  644. *
  645. * 🔴 `uploadUrl` 是**限时(5 分钟)单对象写入授权**,不是长期凭据;
  646. * 过期后由 App 重新签发,容器不需要也不应该保存它。
  647. */
  648. export interface ArtifactJob {
  649. id: string;
  650. deliverableId: string;
  651. agentId: string;
  652. /** 容器内文件路径 */
  653. source: string;
  654. filename: string;
  655. mimeType: string;
  656. /** 🔴 以下三项由**云函数**签名后写入;客户端只读,不得自行构造 */
  657. uploadUrl: string;
  658. publicUrl: string;
  659. headers: Record<string, string>;
  660. /** epoch 秒(预签名有效期,默认 5 分钟) */
  661. expiresAt: number;
  662. status?: 'pending' | 'done' | 'failed';
  663. error?: string;
  664. }
  665. /** 对应云函数 AlP56LCKFm 的返回(取签名阶段) */
  666. export interface UploadTicket { uploadUrl: string;
  667. key: string;
  668. publicUrl: string;
  669. headers?: Record<string, string>;
  670. }
  671. // ---------- 整体配色(ADR-006 v2.0) ----------
  672. export type PaletteId = 'platinum' | 'ice' | 'clay' | 'slate';
  673. export interface PaletteOption {
  674. id: PaletteId;
  675. name: string;
  676. description: string;
  677. }
  678. // ---------- 数字生命 / 任务的业务文案与语义色映射(唯一实现) ----------
  679. export const AGENT_STATUS_TEXT: Record<AgentStatus, string> = {
  680. running: '正在执行',
  681. idle: '空闲',
  682. error: '异常',
  683. offline: '离线',
  684. stopped: '已停止',
  685. };
  686. /**
  687. * 任务状态中文文案(对应源库 tasks.status 七态)。
  688. * 依据《设计令牌.md》v3.0 §5.1 与 statuses 表。
  689. */
  690. export const TASK_STATUS_TEXT: Record<TaskStatus, string> = {
  691. triage: '待分诊',
  692. todo: '待办',
  693. scheduled: '已排期',
  694. ready: '就绪',
  695. running: '执行中',
  696. blocked: '已阻塞',
  697. done: '已完成',
  698. };
  699. /** 任务状态筛选顺序(UI 用) */
  700. export const TASK_STATUS_ORDER: TaskStatus[] = [
  701. 'triage',
  702. 'todo',
  703. 'scheduled',
  704. 'ready',
  705. 'running',
  706. 'blocked',
  707. 'done',
  708. ];
  709. /**
  710. * 语义色断言(《设计令牌.md》v3.0 §5.1)。
  711. * 🔴 阻塞 = danger(用户裁决 2026-10-14);运行中 = info(不用品牌色,避免与 accent 撞色)。
  712. */
  713. export type SemanticTone = 'success' | 'danger' | 'warning' | 'info' | 'neutral';
  714. export function agentStatusTone(status: AgentStatus): SemanticTone {
  715. switch (status) {
  716. case 'running':
  717. return 'success';
  718. case 'idle':
  719. return 'warning';
  720. case 'error':
  721. case 'offline':
  722. case 'stopped':
  723. return 'danger';
  724. default:
  725. return 'neutral';
  726. }
  727. }
  728. export function taskStatusTone(status: TaskStatus): SemanticTone {
  729. switch (status) {
  730. case 'running':
  731. return 'info';
  732. case 'blocked':
  733. return 'danger';
  734. case 'done':
  735. return 'success';
  736. default:
  737. return 'neutral';
  738. }
  739. }
  740. // ---------- 数字生命「在线可对话」判定(唯一实现) ----------
  741. /**
  742. * 数字生命的**可对话在线状态**。
  743. *
  744. * ## 🔴 为什么需要这一层(实测教训)
  745. *
  746. * 项目里存在**两套语义不同的状态**,此前被混用,导致同一容器在
  747. * 「通讯录」与「会话列表」显示不一致(用户实测反馈):
  748. *
  749. * | 维度 | 字段 | 含义 | 实测问题 |
  750. * |---|---|---|---|
  751. * | 运行态 | `Agent.status` | reporter 上报的容器运行状态 | **是历史值**:芯葶 `heartbeatAt` 停在 2026-09-23(近一月前)却仍为 `online` |
  752. * | 接入态 | `channels.matrixConfig.channelState` / `listenerOk` | 容器 Matrix 通道是否在跑 | 决定**能否真的对话** |
  753. *
  754. * 用户能感知的是「**能不能对话**」→ 属**接入态**。
  755. * 故本判定**以接入态为准**,运行态只作辅助(在工作台/详情展示)。
  756. *
  757. * ## 口径
  758. *
  759. * ```
  760. * online 接入态 connected / listenerOk 为真 → 可真对话
  761. * unreported 有绑定但通道状态未上报 → 能连 Matrix,对方可能不在
  762. * offline 无绑定(matrixReady 为假) → 不可对话
  763. * ```
  764. */
  765. export type AgentPresence = 'online' | 'unreported' | 'offline';
  766. /** 判定可对话状态(**全项目唯一实现**,禁止各页另算) */
  767. export function agentPresence(a: Agent | undefined | null): AgentPresence {
  768. if (!a || !a.matrixReady) return 'offline';
  769. const cfg = a.channels?.matrixConfig;
  770. const state = cfg?.channelState ?? cfg?.channelDetail?.matrixState;
  771. if (state === 'connected' || cfg?.listenerOk) return 'online';
  772. // 有绑定但容器未上报通道状态
  773. return 'unreported';
  774. }
  775. /** 可对话状态 → 中文文案(UI 展示,配颜色使用) */
  776. export const AGENT_PRESENCE_TEXT: Record<AgentPresence, string> = {
  777. online: '在线',
  778. unreported: '未上报',
  779. offline: '未接入',
  780. };
  781. /** 可对话状态 → 语义色(复用单轨映射) */
  782. export function agentPresenceTone(p: AgentPresence): SemanticTone {
  783. switch (p) {
  784. case 'online':
  785. return 'success';
  786. case 'unreported':
  787. return 'warning';
  788. default:
  789. return 'neutral';
  790. }
  791. }
  792. /**
  793. * 云端工作区记录(表 `FmodeWorkspace`,云函数 `fmodeagent-chatthreads` 的 wsList)。
  794. *
  795. * 2026-11-01 上云:工作区此前只存 localStorage,换设备后文件夹全丢,
  796. * 而会话的 workspaceId 还在 —— 归属无处安放,只能落进「孤儿」区。
  797. */
  798. export interface WorkspaceRecord {
  799. /** 客户端生成的工作区 id(`ws_<ts>_<rand>`),跨设备稳定 */
  800. id: string;
  801. name: string;
  802. /** 毫秒时间戳;用于稳定排序 */
  803. createdAt: number;
  804. }
  805. /** 工作区 upsert 载荷 */
  806. export interface WorkspaceUpsert {
  807. id: string;
  808. name: string;
  809. createdAt: number;
  810. }
  811. /**
  812. * 云端会话目录记录(表 `FmodeChatThread`,云函数 `fmodeagent-chatthreads`)。
  813. *
  814. * 2026-10-31 引入:会话索引此前只存在本地 localStorage,换浏览器/清缓存即丢失。
  815. * 云端记录是**唯一真相源**,本地只作缓存。
  816. *
  817. * 🔴 归属由服务端按 sessionToken 强制确定,客户端不传也不信任任何 user 字段。
  818. */
  819. export interface ChatThreadRecord {
  820. /** 云端记录 id */
  821. objectId: string;
  822. agentId: string;
  823. roomId: string;
  824. /** 线程根 event_id(App 内部称 sessionId) */
  825. threadRoot: string;
  826. title: string;
  827. /** 用户手动改过标题 → upsert 不覆盖 */
  828. titleLocked: boolean;
  829. messageCount: number;
  830. /** 毫秒时间戳 */
  831. lastMessageAt: number;
  832. archived: boolean;
  833. lastPreview: string;
  834. /** 归属工作区 id(`''` / 缺省 = 日常对话);换设备后仍能还原层级 */
  835. workspaceId?: string; /** 会话形态(2026-11-05):缺省 = dm */
  836. kind?: 'dm' | 'group';
  837. /** 群会话成员容器 id 列表 */
  838. memberIds?: string[];
  839. /**
  840. * 本会话关联的成果链接(云盘可访问地址)。
  841. *
  842. * 🔴 2026-11-09 用户裁决:成果按会话存放 —— 每条会话在库里有一条记录,
  843. * 记录里放该会话成果的在线链接数组,查询时读出来即可。
  844. * 这样不依赖容器上报 threadRoot,「这条对话的成果」由数据本身决定。
  845. */
  846. deliverableUrls?: string[];
  847. }
  848. /** upsert 提交项(不含服务端生成字段) */
  849. export interface ChatThreadUpsert {
  850. agentId: string;
  851. roomId: string;
  852. threadRoot: string;
  853. title?: string;
  854. messageCount?: number;
  855. /** 毫秒时间戳 */
  856. lastMessageAt?: number;
  857. lastPreview?: string;
  858. /**
  859. * 归属的工作区 id(`null` / 缺省 = 日常对话)。
  860. *
  861. * 🔴 必须上云(2026-11-01):此前只存本地 localStorage,导致两个问题:
  862. * ① 每次云端重建列表时归属靠本地缓存「捡」,捡不到就掉回日常对话;
  863. * ② 换浏览器/清缓存后归属永久丢失,用户以为「工作区里的对话没了」。
  864. */
  865. workspaceId?: string | null;
  866. /** 会话形态(2026-11-05):缺省 = dm,兼容存量 */
  867. kind?: 'dm' | 'group';
  868. /** 群会话成员容器 id 列表;单聊为空 */
  869. memberIds?: string[];
  870. /**
  871. * 本会话关联的成果链接(云盘可访问地址)。
  872. *
  873. * 🔴 2026-11-09 用户裁决:成果按会话存放 —— 每条会话在库里有一条记录,
  874. * 记录里放该会话成果的在线链接数组,查询时读出来即可。
  875. * 这样不依赖容器上报 threadRoot,「这条对话的成果」由数据本身决定。
  876. */
  877. deliverableUrls?: string[];
  878. /** 重新出现时撤销软删 */
  879. revive?: boolean;
  880. }
  881. // ============================================================
  882. // 套餐与开通(2026-11-25 第一期:仅前端展示与申请意向)
  883. // ============================================================
  884. /**
  885. * 套餐档位。
  886. *
  887. * 🔴 **这是纯前端展示模型,不是服务端 DTO**。
  888. * 平台侧的 `fmode-subscription` / `fmode-provisioning` 云函数尚未实现
  889. * (见 `docs/03-interface/api/套餐与平台异步开通-API契约.md` §一.4),
  890. * 因此前端**无法**得知用户真实已开通哪个套餐。
  891. */
  892. export type PlanCode = 'trial' | 'starter' | 'advanced' | 'enterprise';
  893. /**
  894. * 付费套餐档位(目录里的三项)。
  895. *
  896. * `trial` 是试用,走独立横幅,不在 `catalog` 里 —— 用这个更窄的类型
  897. * 可以让 TS 在编译期拦住「对 trial 调 `planOf()`」这类错误。
  898. */
  899. export type PaidPlanCode = Exclude<PlanCode, 'trial'>;
  900. /** 套餐目录项(权益矩阵的一行) */
  901. export interface PlanCatalogItem {
  902. code: PaidPlanCode;
  903. /** 完整名称(桌面端用) */
  904. name: string;
  905. /** 简称(移动端窄列用) */
  906. shortName: string;
  907. /** 一句话定位 */
  908. tagline: string;
  909. /** 基础容器数 */
  910. quota: number;
  911. /** 配额单位文案,如「个容器」「个共享」 */
  912. quotaLabel: string;
  913. /** 计费周期,空串表示不展示 */
  914. billingUnit: string;
  915. /** 计费说明,如「含一年资源费用」 */
  916. billingNote: string;
  917. /** 权益明细(桌面端列表) */
  918. features: string[];
  919. /** 移动端配额行文案 */
  920. mobileQuota: string;
  921. /** 移动端权益行文案 */
  922. mobileBenefit: string;
  923. /** 是否推荐 */
  924. recommended?: boolean;
  925. /** 是否允许付费增配 */
  926. addonAllowed: boolean;
  927. /**
  928. * 是否「联系客服定制」(2026-12-03 用户裁决):企业版不显示价格与时长选择,
  929. * 改为展示客服联系方式、定制化开发说明。
  930. */
  931. contactOnly?: boolean;
  932. /**
  933. * 额度展示口径(2026-12-03 用户裁决「只显示上限」):
  934. * `limit` = 只显示上限(3 个容器),不显示已用数。
  935. * 已用数仍用于**禁止超额开通**,只是不上屏。
  936. */
  937. quotaDisplay?: 'limit' | 'usage';
  938. }
  939. /**
  940. * 申请状态。
  941. *
  942. * 🔴 本期**只有两态**:`none` 未提交 / `pending` 已提交待人工确认。
  943. * **没有 `active`** —— 服务端未实现开通,前端不得伪造「已开通」。
  944. */
  945. export type PlanApplicationStatus = 'none' | 'pending';
  946. /** 一条套餐申请意向(本地记录,待人工确认) */
  947. export interface PlanApplication {
  948. planCode: PlanCode;
  949. /** 提交时刻(ISO) */
  950. submittedAt: string;
  951. /** 是否为增配申请 */
  952. addon?: number;
  953. }
  954. // ---------- 容器开通(2026-11-25) ----------
  955. /** 开通任务五状态(PRD FR-30) */
  956. export type ProvisionStatusValue = 'queued' | 'provisioning' | 'verifying' | 'ready' | 'failed';
  957. /** 开通受理结果 */
  958. export interface ProvisionStartResult {
  959. /** `onboarding` = 该档位用用户自己的容器,只返回接入提示词 */
  960. mode: 'onboarding' | 'provision';
  961. /** `mode: 'provision'` 时的任务 id */
  962. jobId?: string;
  963. agentId?: string;
  964. status?: ProvisionStatusValue;
  965. /** `mode: 'onboarding'` 时的接入提示词 */
  966. message?: string;
  967. instruction?: string;
  968. }
  969. /** 服务端核验明细(只用于诊断,不直接展示给用户) */
  970. export interface ProvisionVerifyDetail {
  971. record?: boolean;
  972. matrix?: boolean;
  973. heartbeat?: boolean;
  974. roundtrip?: boolean;
  975. detail?: {
  976. status?: string;
  977. heartbeatAgeSec?: number;
  978. roomId?: string;
  979. recordError?: string;
  980. matrixError?: string;
  981. };
  982. }
  983. /** 开通任务状态 */
  984. export interface ProvisionStatus {
  985. jobId: string;
  986. /** Only included by the current user's task list to reconcile an ambiguous start response. */
  987. idempotencyKey?: string;
  988. agentId?: string;
  989. planCode?: string;
  990. status: ProvisionStatusValue;
  991. stage?: string;
  992. matrixInfo?: {
  993. agentMxId?: string;
  994. userMxId?: string;
  995. roomId?: string;
  996. homeserver?: string;
  997. };
  998. /** 面向用户的失败说明(不含内部术语) */
  999. error?: string | null;
  1000. /** 稳定失败原因码,前端可据此给不同引导 */
  1001. reasonCode?:
  1002. | 'R730_UNREACHABLE'
  1003. | 'CONTAINER_STAGE_FAILED'
  1004. | 'PROVISION_TIMEOUT'
  1005. | 'CONTAINER_NOT_READY'
  1006. | 'QUOTA_EXCEEDED'
  1007. | 'MATRIX_SETUP_FAILED'
  1008. | null;
  1009. /** 是否值得重试(通道抖动可重试;配额满不可) */
  1010. retryable?: boolean | null;
  1011. /** 失败发生在哪个阶段 */
  1012. failedStage?: string | null;
  1013. /** 本任务专用的 Matrix 房间(一任务一房间,不是 matrixInfo.roomId) */
  1014. jobRoomId?: string | null;
  1015. /**
  1016. * Agent 写进 `FmodeProvisionStep` 的完整阶段流水。
  1017. *
  1018. * 🔴 **只用于展示真实进度,不参与成败判定**:
  1019. * 容器是否可用只认 `verify` 的三项服务端核验。
  1020. */
  1021. steps?: ProvisionStep[];
  1022. /** 当前正在做的阶段(最后一条 running,否则最后一条) */
  1023. currentStep?: ProvisionStep | null;
  1024. verify?: ProvisionVerifyDetail | null;
  1025. createdAt?: string;
  1026. updatedAt?: string;
  1027. }
  1028. /**
  1029. * 容器开通的一个阶段记录(对应 Parse 的 `FmodeProvisionStep`)。
  1030. *
  1031. * 由主容器在每进入/完成一个阶段时写入,云函数读出后原样下发。
  1032. */
  1033. export interface ProvisionStep {
  1034. stage?: ProvisionStage | null;
  1035. status?: ProvisionStepStatus | null;
  1036. /** 人话描述,如「写 .env / 清残留 / 改 SOUL」 */
  1037. step?: string | null;
  1038. /** 关键证据,如 `docker ps` 那一行 */
  1039. evidence?: string | null;
  1040. reasonCode?: string | null;
  1041. elapsedSec?: number | null;
  1042. agentId?: string | null;
  1043. /** 写入时间(ISO) */
  1044. at?: string | null;
  1045. }
  1046. export type ProvisionStage = 'prepare' | 'copy' | 'configure' | 'create' | 'start' | 'verify';
  1047. export type ProvisionStepStatus = 'running' | 'ok' | 'failed' | 'warn';
  1048. /** 主容器按 `container-provision-job` 技能输出的结果块 */
  1049. export interface ProvisionAgentReport {
  1050. status?: 'READY' | 'FAILED' | 'PARTIAL';
  1051. stage?: string;
  1052. agentId?: string;
  1053. errors?: string[];
  1054. warnings?: string[];
  1055. elapsedSec?: number;
  1056. }