diff --git a/skills/hose-use/SKILL.md b/skills/hose-use/SKILL.md index 0d2e09f..d6b8bd6 100644 --- a/skills/hose-use/SKILL.md +++ b/skills/hose-use/SKILL.md @@ -36,7 +36,8 @@ metadata: | 单据 Web 详情直达链接生成 | `hose bill link <单号|ID>` | [references/bill.md](references/bill.md) | | 单据实体模型与字段元数据查询 | `hose bill schema` | [references/bill.md](references/bill.md) | | 企业通讯录检索 / 员工档案 / 防爆保护 | `hose contact search`, `hose contact list`, `hose contact get` | [references/contact.md](references/contact.md) | -| 待办中心 / 已审批 / 审批记录 | `hose task todo`, `hose task approved`, `hose task records` | [references/task.md](references/task.md) | +| 待办中心 / 已审批 / 我发起的 / 审批记录 / 抄送 | `hose task todo`, `hose task submitted`, `hose task records`, `hose task cc` | [references/task.md](references/task.md) | +| 消息中心 / 未读统计 / 标记已读 / 类型过滤 | `hose message list`, `hose message count`, `hose message read` | [references/message.md](references/message.md) | | 跨自建应用工作流聚合检索 | `hose workflow list`, `hose platform list` | [references/workflow.md](references/workflow.md) | | 特定任务节点扫描 (如脚本/Webhook) | `hose workflow find --task=code-js2` | [references/workflow.md](references/workflow.md) | | 工作流详情与开通切换 | `hose workflow get`, `hose workflow toggle` | [references/workflow.md](references/workflow.md) | @@ -71,13 +72,19 @@ hose bill list --json -o /tmp/bills.json # 导出全量单据报文 hose bill get n0GMIM0dkQmKbS # 精准查询单据详情 (中文字段映射) hose bill schema # 查询单据实体元数据字典 -# 4. 自建应用与工作流 +# 4. 待办与消息 +hose task submitted # 查看我发起的单据 +hose message list --unread # 查看未读消息 +hose message count # 查看未读数量 +hose message read # 标记单条消息已读 + +# 5. 自建应用与工作流 hose platform list # 查看自建应用列表 hose workflow list # 聚合扫描全部应用下的工作流 hose workflow find --task=code-js2 # 查找部署了代码节点的工作流 hose workflow get wf_01k83y... # 查看指定工作流节点拓扑 -# 5. 服务与守护进程 +# 6. 服务与守护进程 hose http --port=8888 # 启动前台 HTTP 服务与交互式文档 hose service start # 后台启动常驻守护进程服务 hose service status # 查看常驻守护进程运行状态与端点 diff --git a/skills/hose-use/references/message.md b/skills/hose-use/references/message.md new file mode 100644 index 0000000..8c3d06e --- /dev/null +++ b/skills/hose-use/references/message.md @@ -0,0 +1,14 @@ +# 消息中心 + +消息中心是 CLI-only 能力,不新增 MCP tool。命令默认查询未读消息。 + +```bash +hose message list --unread +hose message list --read +hose message list --type=Approve +hose message count +hose message read +hose message read-all +``` + +`message list --json` 返回消息 ID、标题、内容、消息类型、已读状态和原始字段;`--type` 在 CLI 层按 `messageType`、`msgType`、`msgTypeName` 或 `msgName` 做兼容过滤。 diff --git a/src/cli/commands/message.ts b/src/cli/commands/message.ts new file mode 100644 index 0000000..fd891bb --- /dev/null +++ b/src/cli/commands/message.ts @@ -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 { + const corp = runtime.selectedCorp; + if (!corp) throw new Error("未指定或未找到有效企业。请运行 hose corps list 或指定 --corp="); + 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 \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 , 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, + }; +} diff --git a/src/cli/printer.ts b/src/cli/printer.ts index 3283aa4..8f3f8af 100644 --- a/src/cli/printer.ts +++ b/src/cli/printer.ts @@ -122,6 +122,7 @@ export function printHelp(): string { bill 单据与费用明细检索 (list, get <单号|ID>, schema, link <单号|ID>) contact 企业通讯录与人员检索 (list, search <关键词>, get ) 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 # 查看当前企业单据与报销列表 diff --git a/src/cli/runner.ts b/src/cli/runner.ts index a3df1a1..39f729d 100644 --- a/src/cli/runner.ts +++ b/src/cli/runner.ts @@ -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": diff --git a/src/core/embedded-skill.test.ts b/src/core/embedded-skill.test.ts index 474027c..2789c42 100644 --- a/src/core/embedded-skill.test.ts +++ b/src/core/embedded-skill.test.ts @@ -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); diff --git a/src/core/embedded-skill.ts b/src/core/embedded-skill.ts index 9d1a195..ebedd23 100644 --- a/src/core/embedded-skill.ts +++ b/src/core/embedded-skill.ts @@ -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 = { "SKILL.md": skillMain, @@ -18,6 +19,7 @@ export const EMBEDDED_SKILL_FILES: Record = { "references/bill.md": refBill, "references/contact.md": refContact, "references/task.md": refTask, + "references/message.md": refMessage, "references/workflow.md": refWorkflow, "references/service.md": refService, }; diff --git a/src/domains/message/api.ts b/src/domains/message/api.ts new file mode 100644 index 0000000..78d401a --- /dev/null +++ b/src/domains/message/api.ts @@ -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[] }> { + 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[] }; + return { count: json.count ?? json.items?.length ?? 0, items: json.items ?? [] }; +} + +export async function markMessageRead(client: KyInstance, corpId: string, messageId: string): Promise { + // 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 }, + }); +} diff --git a/src/domains/message/message.test.ts b/src/domains/message/message.test.ts new file mode 100644 index 0000000..405a564 --- /dev/null +++ b/src/domains/message/message.test.ts @@ -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); + }); +}); diff --git a/src/domains/message/tools.ts b/src/domains/message/tools.ts new file mode 100644 index 0000000..d881abd --- /dev/null +++ b/src/domains/message/tools.ts @@ -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): 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 { + 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 { + 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))); +} diff --git a/src/domains/message/types.ts b/src/domains/message/types.ts new file mode 100644 index 0000000..07c1ef5 --- /dev/null +++ b/src/domains/message/types.ts @@ -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; +} + +export interface MessagePageResult { + total: number; + start: number; + count: number; + read: boolean; + items: MessageItem[]; +}