feat: add CLI message center commands
This commit is contained in:
1 parent
0be1365289
commit
621c8cb7dc
11 files changed
+242
-4
No files matched your search
@@ -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,
|
||||
};
|
||||
}
|
||||
@@ -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 # 查看当前企业单据与报销列表
|
||||
|
||||
@@ -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":
|
||||
|
||||
@@ -34,7 +34,7 @@ describe("embedded-skill - 多文件内嵌 Skill 体系", () => {
|
||||
try {
|
||||
const res = installSkillFiles(tmp);
|
||||
expect(res.targetDir).toBe(tmp);
|
||||
expect(res.files.length).toBe(8);
|
||||
expect(res.files.length).toBe(9);
|
||||
expect(existsSync(join(tmp, "SKILL.md"))).toBe(true);
|
||||
expect(existsSync(join(tmp, "agents", "openai.yaml"))).toBe(true);
|
||||
expect(existsSync(join(tmp, "references", "auth.md"))).toBe(true);
|
||||
|
||||
@@ -10,6 +10,7 @@ import refWorkflow from "../../skills/hose-use/references/workflow.md" with { ty
|
||||
import refService from "../../skills/hose-use/references/service.md" with { type: "text" };
|
||||
import refContact from "../../skills/hose-use/references/contact.md" with { type: "text" };
|
||||
import refTask from "../../skills/hose-use/references/task.md" with { type: "text" };
|
||||
import refMessage from "../../skills/hose-use/references/message.md" with { type: "text" };
|
||||
|
||||
export const EMBEDDED_SKILL_FILES: Record<string, string> = {
|
||||
"SKILL.md": skillMain,
|
||||
@@ -18,6 +19,7 @@ export const EMBEDDED_SKILL_FILES: Record<string, string> = {
|
||||
"references/bill.md": refBill,
|
||||
"references/contact.md": refContact,
|
||||
"references/task.md": refTask,
|
||||
"references/message.md": refMessage,
|
||||
"references/workflow.md": refWorkflow,
|
||||
"references/service.md": refService,
|
||||
};
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import type { KyInstance } from "ky";
|
||||
import type { MessageQueryOptions } from "./types.js";
|
||||
|
||||
export async function fetchMessagePage(
|
||||
client: KyInstance,
|
||||
corpId: string,
|
||||
options: MessageQueryOptions = {}
|
||||
): Promise<{ count: number; items: Record<string, unknown>[] }> {
|
||||
const start = options.start ?? 0;
|
||||
const count = options.count ?? 20;
|
||||
const read = options.read ?? false;
|
||||
const response = await client.get("api/message/v1/messages", {
|
||||
searchParams: {
|
||||
read: String(read),
|
||||
start: String(start),
|
||||
count: String(count),
|
||||
corpId,
|
||||
},
|
||||
});
|
||||
const json = (await response.json()) as { count?: number; items?: Record<string, unknown>[] };
|
||||
return { count: json.count ?? json.items?.length ?? 0, items: json.items ?? [] };
|
||||
}
|
||||
|
||||
export async function markMessageRead(client: KyInstance, corpId: string, messageId: string): Promise<void> {
|
||||
// The downstream route accepts the compound ID with its colon separators intact.
|
||||
await client.put(`api/message/v1/messages/$${messageId}`, {
|
||||
searchParams: { corpId },
|
||||
json: { id: messageId },
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { filterMessages, normalizeMessage } from "./tools.js";
|
||||
|
||||
describe("Message Domain - 消息中心模型", () => {
|
||||
test("normalizes message fields and filters by type", () => {
|
||||
const item = normalizeMessage({
|
||||
id: "msg-1",
|
||||
title: "你的单据被驳回",
|
||||
content: "单据:有效性。",
|
||||
messageType: "INFO",
|
||||
msgTypeName: "审批通知",
|
||||
read: false,
|
||||
sortTime: 1776052034000,
|
||||
});
|
||||
expect(item.id).toBe("msg-1");
|
||||
expect(item.read).toBe(false);
|
||||
expect(filterMessages([item], "审批")).toHaveLength(1);
|
||||
expect(filterMessages([item], "Loan_Repayment")).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
import type { KyInstance } from "ky";
|
||||
import { fetchMessagePage, markMessageRead } from "./api.js";
|
||||
import type { MessageItem, MessagePageResult, MessageQueryOptions } from "./types.js";
|
||||
|
||||
export function normalizeMessage(raw: Record<string, unknown>): MessageItem {
|
||||
return {
|
||||
id: String(raw.id ?? ""),
|
||||
title: typeof raw.title === "string" ? raw.title : undefined,
|
||||
content: typeof raw.content === "string" ? raw.content : undefined,
|
||||
messageType: typeof raw.messageType === "string" ? raw.messageType : undefined,
|
||||
msgType: typeof raw.msgType === "string" ? raw.msgType : undefined,
|
||||
msgTypeName: typeof raw.msgTypeName === "string" ? raw.msgTypeName : undefined,
|
||||
msgName: typeof raw.msgName === "string" ? raw.msgName : undefined,
|
||||
read: raw.read === true,
|
||||
createTime: typeof raw.createTime === "number" ? raw.createTime : undefined,
|
||||
updateTime: typeof raw.updateTime === "number" ? raw.updateTime : undefined,
|
||||
sortTime: typeof raw.sortTime === "number" ? raw.sortTime : undefined,
|
||||
sourceId: typeof raw.sourceId === "string" || raw.sourceId === null ? raw.sourceId : undefined,
|
||||
sourceType: typeof raw.sourceType === "string" ? raw.sourceType : undefined,
|
||||
sourceStatus: typeof raw.sourceStatus === "string" || raw.sourceStatus === null ? raw.sourceStatus : undefined,
|
||||
url: typeof raw.url === "string" ? raw.url : undefined,
|
||||
raw,
|
||||
};
|
||||
}
|
||||
|
||||
export async function listMessages(
|
||||
client: KyInstance,
|
||||
corpId: string,
|
||||
options: MessageQueryOptions = {}
|
||||
): Promise<MessagePageResult> {
|
||||
const page = await fetchMessagePage(client, corpId, options);
|
||||
return {
|
||||
total: page.count,
|
||||
start: options.start ?? 0,
|
||||
count: options.count ?? 20,
|
||||
read: options.read ?? false,
|
||||
items: page.items.map(normalizeMessage),
|
||||
};
|
||||
}
|
||||
|
||||
export async function readMessage(client: KyInstance, corpId: string, messageId: string): Promise<void> {
|
||||
await markMessageRead(client, corpId, messageId);
|
||||
}
|
||||
|
||||
export function filterMessages(items: MessageItem[], messageType?: string): MessageItem[] {
|
||||
if (!messageType) return items;
|
||||
const target = messageType.toLowerCase();
|
||||
return items.filter((item) => [item.messageType, item.msgType, item.msgTypeName, item.msgName]
|
||||
.filter(Boolean)
|
||||
.some((value) => String(value).toLowerCase() === target || String(value).toLowerCase().includes(target)));
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
export interface MessageQueryOptions {
|
||||
start?: number;
|
||||
count?: number;
|
||||
read?: boolean;
|
||||
}
|
||||
|
||||
export interface MessageItem {
|
||||
id: string;
|
||||
title?: string;
|
||||
content?: string;
|
||||
messageType?: string;
|
||||
msgType?: string;
|
||||
msgTypeName?: string;
|
||||
msgName?: string;
|
||||
read: boolean;
|
||||
createTime?: number;
|
||||
updateTime?: number;
|
||||
sortTime?: number;
|
||||
sourceId?: string | null;
|
||||
sourceType?: string;
|
||||
sourceStatus?: string | null;
|
||||
url?: string;
|
||||
raw: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface MessagePageResult {
|
||||
total: number;
|
||||
start: number;
|
||||
count: number;
|
||||
read: boolean;
|
||||
items: MessageItem[];
|
||||
}
|
||||
Reference in new issue
Block a user