- 彻底移除旧内部狭隘命名与垫片包袱,一步到位全仓整体重命名: * session-client.ts ➔ hose-web-session-client.ts (合思 Web 主站会话客户端) * script-engine-client.ts ➔ piper-workflow-client.ts (Piper 工作流引擎客户端) * unity-client.ts ➔ unity-easo-client.ts (Unity / EASO 运营中台与动态集群客户端) * sso-client.ts ➔ sso-auth-client.ts (SSO 与扫码鉴权客户端) - 新增 hose-docs-client.ts 纳为第 7 大通信底座客户端,收口 docs.ekuaibao.com 文档站点通信 - 端正 Piper 工作流引擎定位,完整覆盖工作流创建、保存、发布、108 类节点编排与 Job 流转 - 彻底整体替换全仓 import 引用与测试套件,新增 clients-semantics.test.ts 自动化校验 - 全量自动化测试 150 files 1155 tests 全部通过,类型检查零错误
106 lines
5.6 KiB
TypeScript
106 lines
5.6 KiB
TypeScript
import { booleanOption, numberOption, stringOption } from "../parser.js";
|
|
import { printJson, printTable } from "../printer.js";
|
|
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
|
|
import { createSessionClient } from "../../core/http/hose-web-session-client.js";
|
|
import { filterMessages, listMessages, mergeMessages, readMessage } from "../../domains/message/tools.js";
|
|
import type { MessageItem } from "../../domains/message/types.js";
|
|
import { isDryRun, mutationPreview, requireMutationConfirmation } from "../mutation.js";
|
|
|
|
function formatTime(value: number | undefined): string {
|
|
return value === undefined ? "-" : new Date(value).toISOString().replace("T", " ").slice(0, 16);
|
|
}
|
|
|
|
function renderItems(items: MessageItem[]): string {
|
|
const headers = ["消息 ID", "时间", "类型", "标题", "内容", "状态"];
|
|
const rows = items.map((item) => [
|
|
item.id,
|
|
formatTime(item.sortTime ?? item.createTime),
|
|
item.msgTypeName || item.messageType || "-",
|
|
item.title || "-",
|
|
item.content || "-",
|
|
item.read ? "已读" : "未读",
|
|
]);
|
|
return printTable(headers, rows);
|
|
}
|
|
|
|
export async function handleMessageCommand(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
|
const corp = runtime.selectedCorp;
|
|
if (!corp) throw new Error("未指定或未找到有效企业。请运行 hose-use corps list 或指定 --corp=<id>");
|
|
const action = parsed.action || parsed.args[0] || "list";
|
|
const corpId = corp.corpId || corp.id;
|
|
|
|
if (action === "count" || action === "unread") {
|
|
const client = await createSessionClient(corp);
|
|
const result = await listMessages(client, corpId, { start: 0, count: 1, read: false });
|
|
if (runtime.jsonMode) return { stdout: printJson({ corpId, unread: result.total }, runtime.outputOptions), stderr: "", exitCode: 0 };
|
|
return { stdout: `未读消息: ${result.total}\n`, stderr: "", exitCode: 0 };
|
|
}
|
|
|
|
if (action === "read" || action === "mark-read") {
|
|
const messageId = parsed.args[0] || stringOption(parsed.options, ["id", "message-id"]);
|
|
if (!messageId) return { stdout: "", stderr: "用法: hose-use message read <messageId>\n", exitCode: 1 };
|
|
if (isDryRun(parsed, runtime)) {
|
|
return mutationPreview(parsed, runtime, "标记消息为已读", messageId, { read: true });
|
|
}
|
|
requireMutationConfirmation(parsed, runtime, "标记消息为已读", `hose-use message read ${messageId} --yes`);
|
|
const client = await createSessionClient(corp);
|
|
await readMessage(client, corpId, messageId);
|
|
if (runtime.jsonMode) return { stdout: printJson({ corpId, id: messageId, read: true }, runtime.outputOptions), stderr: "", exitCode: 0 };
|
|
return { stdout: `已标记消息为已读: ${messageId}\n`, stderr: "", exitCode: 0 };
|
|
}
|
|
|
|
if (action === "read-all" || action === "mark-all-read") {
|
|
if (isDryRun(parsed, runtime)) {
|
|
return mutationPreview(parsed, runtime, "批量标记未读消息为已读", `corp:${corpId}`, { read: true, allUnread: true });
|
|
}
|
|
requireMutationConfirmation(parsed, runtime, "批量标记未读消息为已读", "hose-use message read-all --yes");
|
|
const client = await createSessionClient(corp);
|
|
const ids: string[] = [];
|
|
let start = 0;
|
|
let total = 0;
|
|
do {
|
|
const page = await listMessages(client, corpId, { start, count: 100, read: false });
|
|
total = page.total;
|
|
ids.push(...page.items.map((item) => item.id));
|
|
start += page.items.length;
|
|
if (page.items.length === 0) break;
|
|
} while (start < total);
|
|
for (const id of ids) await readMessage(client, corpId, id);
|
|
if (runtime.jsonMode) return { stdout: printJson({ corpId, requested: ids.length, read: ids.length, totalUnreadBefore: total }, runtime.outputOptions), stderr: "", exitCode: 0 };
|
|
return { stdout: `已标记 ${ids.length} 条消息为已读。\n`, stderr: "", exitCode: 0 };
|
|
}
|
|
|
|
if (action !== "list") {
|
|
return { stdout: "", stderr: `未知消息子命令: ${action}。支持: list, count, read <messageId>, read-all\n`, exitCode: 1 };
|
|
}
|
|
|
|
const client = await createSessionClient(corp);
|
|
|
|
const read = booleanOption(parsed.options, ["read"]);
|
|
const unread = booleanOption(parsed.options, ["unread"]);
|
|
const all = booleanOption(parsed.options, ["all"]);
|
|
const readFilter = read && !unread;
|
|
const count = numberOption(parsed.options, ["count", "limit", "n"], 20) ?? 20;
|
|
const start = numberOption(parsed.options, ["start", "offset"], 0) ?? 0;
|
|
const type = stringOption(parsed.options, ["type", "message-type"]);
|
|
const result = all
|
|
? await (async () => {
|
|
const [unreadPage, readPage] = await Promise.all([
|
|
listMessages(client, corpId, { start: 0, count: Math.max(100, start + count), read: false }),
|
|
listMessages(client, corpId, { start: 0, count: Math.max(100, start + count), read: true }),
|
|
]);
|
|
const merged = mergeMessages([...unreadPage.items, ...readPage.items]);
|
|
return { total: unreadPage.total + readPage.total, start, count, read: false, items: merged.slice(start, start + count) };
|
|
})()
|
|
: await listMessages(client, corpId, { start, count, read: readFilter });
|
|
const items = filterMessages(result.items, type);
|
|
if (runtime.jsonMode) {
|
|
return { stdout: printJson({ corpId, total: type ? items.length : result.total, start: result.start, count: result.count, read: all ? "all" : readFilter, items }, runtime.outputOptions), stderr: "", exitCode: 0 };
|
|
}
|
|
return {
|
|
stdout: `====== ${all ? "全部" : readFilter ? "已读" : "未读"}消息 (企业: ${corp.name} | 总数: ${type ? items.length : result.total} | 当前返回: ${items.length}) ======\n${items.length ? renderItems(items) : " (暂无数据)"}\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|