|
| 1 | +import { useEffect, useRef, useState, type FormEvent } from "react"; |
| 2 | +import { z } from "zod"; |
| 3 | +import { ChatApiError, requestJson } from "../../data/chat"; |
| 4 | +import { useWorkspaceI18n } from "./i18n"; |
| 5 | + |
| 6 | +const outcomes = { |
| 7 | + working_session_turn: ["已送入工作会话", "Sent to a working session"], |
| 8 | + todo_update: ["已更新任务", "Task updated"], |
| 9 | + authority_update: ["已更新权威记录", "Authority record updated"], |
| 10 | + design_update: ["已更新设计", "Design updated"], |
| 11 | + no_follow_up: ["无需跟进", "No follow-up"], |
| 12 | +} as const; |
| 13 | +const statuses = { |
| 14 | + pending: ["待处置", "Awaiting review"], |
| 15 | + not_captured: ["所选信源未收录此消息", "Message missing from the selected source"], |
| 16 | + ambiguous_route: ["多个信源收录了此消息,请指定信源路由", "Multiple sources captured this message. Enter a source route."], |
| 17 | + processed_without_receipt: ["消息已处理,缺少处置回执", "Message processed without a disposition receipt"], |
| 18 | + not_material_review: ["此消息不适用材料处置", "Material review does not apply to this message"], |
| 19 | + inbox_unavailable: ["收件箱不可用", "Inbox unavailable"], |
| 20 | + readback_unavailable: ["无法核实处置回执", "Disposition receipt could not be verified"], |
| 21 | +} as const; |
| 22 | +const lookupResult = z.discriminatedUnion("status", [ |
| 23 | + z.object({status: z.literal("committed"), receipt: z.object({ |
| 24 | + effect_kind: z.enum(Object.keys(outcomes) as [keyof typeof outcomes, ...(keyof typeof outcomes)[]]), |
| 25 | + effect_ref: z.string().regex(/^sha256:[0-9a-f]{24}$/), |
| 26 | + })}), |
| 27 | + z.object({status: z.enum(Object.keys(statuses) as [keyof typeof statuses, ...(keyof typeof statuses)[]])}), |
| 28 | +]); |
| 29 | + |
| 30 | +export function LarkMaterialLookup({goalId, agents}: Readonly<{goalId: string; agents: readonly string[]}>) { |
| 31 | + const {locale} = useWorkspaceI18n(); |
| 32 | + const zh = locale === "zh-CN"; |
| 33 | + const [agent, setAgent] = useState(""); |
| 34 | + const [message, setMessage] = useState(""); |
| 35 | + const [route, setRoute] = useState(""); |
| 36 | + const [result, setResult] = useState<z.infer<typeof lookupResult> | null>(null); |
| 37 | + const [error, setError] = useState<string | null>(null); |
| 38 | + const [busy, setBusy] = useState(false); |
| 39 | + const generation = useRef(0); |
| 40 | + useEffect(() => () => { generation.current++; }, []); |
| 41 | + function reset() { |
| 42 | + generation.current++; |
| 43 | + setBusy(false); |
| 44 | + setError(null); |
| 45 | + setResult(null); |
| 46 | + } |
| 47 | + async function lookup(event: FormEvent) { |
| 48 | + event.preventDefault(); |
| 49 | + reset(); |
| 50 | + const current = generation.current; |
| 51 | + setBusy(true); |
| 52 | + try { |
| 53 | + const value = lookupResult.parse(await requestJson<unknown>("/api/chat/lark/material-review/read", { |
| 54 | + method: "POST", body: JSON.stringify({ |
| 55 | + goal_id: goalId, message_id: message.trim(), agent_id: agent || undefined, route_key: route.trim() || undefined, |
| 56 | + }), |
| 57 | + })); |
| 58 | + if (current === generation.current) setResult(value); |
| 59 | + } catch (cause) { |
| 60 | + const code = cause instanceof ChatApiError ? cause.payload.error_code : null; |
| 61 | + if (current === generation.current) setError(typeof code === "string" ? code : "unknown"); |
| 62 | + } finally { |
| 63 | + if (current === generation.current) setBusy(false); |
| 64 | + } |
| 65 | + } |
| 66 | + return <details className="personal-capability-raw-values"> |
| 67 | + <summary>{zh ? "查询消息处置" : "Look up a message disposition"}</summary> |
| 68 | + <form className="personal-capability-field-summary" onSubmit={lookup} onChange={reset}> |
| 69 | + <div className="personal-capability-fields"> |
| 70 | + {agents.length > 0 && <label>{zh ? "收件箱" : "Inbox"} <select value={agent} onChange={e => setAgent(e.target.value)}> |
| 71 | + <option value="">{zh ? "目标默认收件箱" : "Goal default inbox"}</option> |
| 72 | + {agents.map(id => <option key={id} value={id}>{id}</option>)} |
| 73 | + </select></label>} |
| 74 | + <label>{zh ? "消息 ID" : "Message ID"} <input required value={message} onChange={e => setMessage(e.target.value)} /></label> |
| 75 | + <label>{zh ? "信源路由(可选)" : "Source route (optional)"} <input value={route} onChange={e => setRoute(e.target.value)} /></label> |
| 76 | + </div> |
| 77 | + <div className="personal-capability-actions"><button type="submit" disabled={busy || !message.trim()}>{zh ? "查询" : "Look up"}</button></div> |
| 78 | + <div role="status">{busy && (zh ? "查询中…" : "Looking up…")} |
| 79 | + {error === "extension_doctor_not_ready" ? <p> |
| 80 | + {zh ? "飞书扩展需要重新检查。在此目标的运行环境中执行 " : "Recheck the Lark extension in this goal’s runtime. Run "} |
| 81 | + <code>loopx extension doctor loopx-lark --execute</code> |
| 82 | + {zh ? ",然后重试。若使用自定义运行目录,请在 loopx 后加上相同的 --runtime-root 参数。" : ", then retry. For a custom runtime, include the same --runtime-root option after loopx."} |
| 83 | + </p> : error === "inbox_unavailable" ? (zh ? "所选收件箱不可用。请检查其绑定和扩展启用状态。" : "The selected inbox is unavailable. Check its binding and whether the extension is enabled.") |
| 84 | + : error === "invalid_inbox_lookup" ? (zh ? "请核对消息 ID 和信源路由后重试。" : "Check the message ID and source route, then retry.") |
| 85 | + : error && (zh ? "暂时无法查询,请重试。" : "Lookup unavailable. Please retry.")} |
| 86 | + {result && (result.status === "committed" ? <p>{outcomes[result.receipt.effect_kind][zh ? 0 : 1]} <code>{result.receipt.effect_ref}</code></p> |
| 87 | + : statuses[result.status][zh ? 0 : 1])} |
| 88 | + </div> |
| 89 | + </form> |
| 90 | + </details>; |
| 91 | +} |
0 commit comments