feat: add CLI message center commands

This commit is contained in:
gitea committed 2026-09-05 14:05:05 +08:00
1 parent 0be1365289
commit 621c8cb7dc
11 files changed
+242 -4

No files matched your search

+73
View File
@@ -0,0 +1,73 @@
import { booleanOption, numberOption, stringOption } from "../parser.js";
import { printJson, printTable } from "../printer.js";
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
import { createEkbClient } from "../../core/auth/ekb.js";
import { filterMessages, listMessages, readMessage } from "../../domains/message/tools.js";
import type { MessageItem } from "../../domains/message/types.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 corps list 或指定 --corp=<id>");
const action = parsed.action || parsed.args[0] || "list";
const client = await createEkbClient(corp);
const corpId = corp.corpId || corp.id;
if (action === "count" || action === "unread") {
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 message read <messageId>\n", exitCode: 1 };
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") {
const result = await listMessages(client, corpId, { start: 0, count: 100, read: false });
for (const item of result.items) await readMessage(client, corpId, item.id);
if (runtime.jsonMode) return { stdout: printJson({ corpId, requested: result.items.length, read: result.items.length }, runtime.outputOptions), stderr: "", exitCode: 0 };
return { stdout: `已标记 ${result.items.length} 条消息为已读。\n`, stderr: "", exitCode: 0 };
}
if (action !== "list") {
return { stdout: "", stderr: `未知消息子命令: ${action}。支持: list, count, read <messageId>, read-all\n`, exitCode: 1 };
}
const read = booleanOption(parsed.options, ["read"]);
const unread = booleanOption(parsed.options, ["unread"]);
const readFilter = read && !unread;
const count = numberOption(parsed.options, ["count", "limit", "n"], 20);
const start = numberOption(parsed.options, ["start", "offset"], 0);
const type = stringOption(parsed.options, ["type", "message-type"]);
const result = 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: readFilter, items }, runtime.outputOptions), stderr: "", exitCode: 0 };
}
return {
stdout: `====== ${readFilter ? "已读" : "未读"}消息 (企业: ${corp.name} | 总数: ${type ? items.length : result.total} | 当前返回: ${items.length}) ======\n${items.length ? renderItems(items) : " (暂无数据)"}\n`,
stderr: "",
exitCode: 0,
};
}
+3
View File
@@ -122,6 +122,7 @@ export function printHelp(): string {
bill 单据与费用明细检索 (list, get <单号|ID>, schema, link <单号|ID>)
contact 企业通讯录与人员检索 (list, search <关键词>, get <staffId>)
task 待办中心与审批任务 (todo, approved, records, submitted, cc)
message 消息中心与通知管理 (list, count, read, read-all)
platform 自建应用管理 (list)
skill Agent Skill 管理 (install, status, dump)
probe 连通性诊断 (probe [corpId])
@@ -154,6 +155,8 @@ export function printHelp(): string {
hose-use task approved # 查看我已审批任务
hose-use task submitted # 查看我发起的单据
hose-use task cc --unread # 查看未读抄送
hose-use message list --unread # 查看未读消息
hose-use message count # 查看未读消息数
hose-use workflow list # 汇总查看全部工作流
hose-use workflow find --task=code-js2 # 搜索包含自定义脚本的工作流
hose-use bill list # 查看当前企业单据与报销列表
+6
View File
@@ -17,6 +17,7 @@ import { handleContactCommand } from "./commands/contact.js";
import { handleServiceCommand } from "./commands/service.js";
import { handleHttpCommand } from "./commands/http.js";
import { handleTaskCommand } from "./commands/task.js";
import { handleMessageCommand } from "./commands/message.js";
import type { CliResult, CliRunOptions } from "./types.js";
const STANDING_COMMANDS = new Set(["http", "server", "web", "mcp"]);
@@ -140,6 +141,11 @@ async function dispatchCli(
case "tasks":
case "todo":
return await handleTaskCommand(parsed, runtime);
case "message":
case "messages":
case "notify":
case "notifications":
return await handleMessageCommand(parsed, runtime);
case "platform":
case "platforms":
case "app":