Merge pull request #16 from yunyingk/feat/issue-10-task-center
feat: add CLI task center queries
This commit is contained in:
11 files changed
+338
-1
No files matched your search
@@ -36,6 +36,7 @@ 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 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) |
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
# hose-use 专卷:待办中心与审批任务
|
||||
|
||||
## CLI
|
||||
|
||||
```bash
|
||||
hose task todo # 待我审批
|
||||
hose task approved # 我已审批
|
||||
hose task records # 审批记录
|
||||
hose task todo --staff <员工工号或ID> # 在当前页结果中按人员过滤
|
||||
hose task todo --json --count 50
|
||||
hose task records --from 2026-01-01 --to 2026-09-05
|
||||
```
|
||||
|
||||
任务结果包含任务 ID、流程 ID、单号、标题、单据模板、提交人、金额、状态、当前节点和可用动作。分页使用 `--start` 与 `--count`。
|
||||
|
||||
当前实现对接合思 Web 待办工作台实际使用的业务接口:待审批/审批记录走 `/api/flow/v2/backlogs/search`,已审批走 `/api/flow/v1/flows/approved`。接口响应的完整原始对象保留在 JSON 结果的 `raw` 字段中,便于后续接入“我发起的”和“抄送我的”视图。
|
||||
|
||||
当前只通过 CLI 暴露任务查询;MCP 不新增待办专用工具。
|
||||
@@ -0,0 +1,59 @@
|
||||
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
|
||||
import { numberOption, stringOption } from "../parser.js";
|
||||
import { printJson, printTable } from "../printer.js";
|
||||
import { createEkbClient } from "../../core/auth/ekb.js";
|
||||
import { listTasks } from "../../domains/task/tools.js";
|
||||
import type { TaskView } from "../../domains/task/types.js";
|
||||
|
||||
function dateOption(value: string | undefined): number | undefined {
|
||||
if (!value) return undefined;
|
||||
const numeric = Number(value);
|
||||
if (Number.isFinite(numeric)) return numeric;
|
||||
const parsed = Date.parse(value);
|
||||
return Number.isNaN(parsed) ? undefined : parsed;
|
||||
}
|
||||
|
||||
export async function handleTaskCommand(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] || "todo";
|
||||
const view: TaskView | undefined = action === "todo" || action === "pending"
|
||||
? "todo"
|
||||
: action === "approved" || action === "done"
|
||||
? "approved"
|
||||
: action === "processed" || action === "records" || action === "history"
|
||||
? "processed"
|
||||
: undefined;
|
||||
if (!view) {
|
||||
throw new Error("未知 task 子命令。支持: todo, approved, records (processed)");
|
||||
}
|
||||
const count = numberOption(parsed.options, ["count", "limit", "n"], 20);
|
||||
const start = numberOption(parsed.options, ["start", "offset"], 0);
|
||||
const staffId = stringOption(parsed.options, ["staff", "staff-id", "staffId"]);
|
||||
const from = dateOption(stringOption(parsed.options, ["from", "start-date"]));
|
||||
const to = dateOption(stringOption(parsed.options, ["to", "end-date"]));
|
||||
const result = await listTasks(await createEkbClient(corp), corp.corpId || corp.id, view, { start, count, staffId, from, to });
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return { stdout: printJson({ corpId: corp.corpId || corp.id, corpName: corp.name, ...result }, runtime.outputOptions), stderr: "", exitCode: 0 };
|
||||
}
|
||||
const headers = ["#", "任务 ID", "单号", "标题", "模板", "提交人", "金额", "状态", "当前节点", "动作"];
|
||||
const rows = result.items.map((item, index) => [
|
||||
index + 1,
|
||||
item.taskId,
|
||||
item.billCode || "-",
|
||||
item.title || "-",
|
||||
item.templateName || "-",
|
||||
item.submitter ? `${item.submitter.name || "-"}${item.submitter.code ? `(${item.submitter.code})` : ""}` : "-",
|
||||
item.amount ? `${item.amount.symbol || ""}${item.amount.value} ${item.amount.currency || ""}`.trim() : "-",
|
||||
item.state || "-",
|
||||
item.nodeName || "-",
|
||||
item.action || "-",
|
||||
]);
|
||||
const labels: Record<TaskView, string> = { todo: "待审批", approved: "已审批", processed: "审批记录" };
|
||||
return {
|
||||
stdout: `====== ${labels[view]}任务 (企业: ${corp.name} | 总数: ${result.total} | 当前返回: ${result.items.length}) ======\n${printTable(headers, rows)}\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
@@ -121,6 +121,7 @@ export function printHelp(): string {
|
||||
workflow 工作流管理 (list, get, find, toggle)
|
||||
bill 单据与费用明细检索 (list, get <单号|ID>, schema, link <单号|ID>)
|
||||
contact 企业通讯录与人员检索 (list, search <关键词>, get <staffId>)
|
||||
task 待办中心与审批任务 (todo, approved, records)
|
||||
platform 自建应用管理 (list)
|
||||
skill Agent Skill 管理 (install, status, dump)
|
||||
probe 连通性诊断 (probe [corpId])
|
||||
@@ -149,6 +150,8 @@ export function printHelp(): string {
|
||||
hose-use corps switch ID01MCcOgJl1td # 切换工作企业
|
||||
hose-use contact search 张三 # 模糊搜索员工工号、邮箱与电话
|
||||
hose-use contact list --limit=20 # 采样查看企业通讯录(带防爆截断保护)
|
||||
hose-use task todo --json # 查看当前待我审批任务
|
||||
hose-use task approved # 查看我已审批任务
|
||||
hose-use workflow list # 汇总查看全部工作流
|
||||
hose-use workflow find --task=code-js2 # 搜索包含自定义脚本的工作流
|
||||
hose-use bill list # 查看当前企业单据与报销列表
|
||||
|
||||
@@ -16,6 +16,7 @@ import { handleBillCommand } from "./commands/bill.js";
|
||||
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 type { CliResult, CliRunOptions } from "./types.js";
|
||||
|
||||
const STANDING_COMMANDS = new Set(["http", "server", "web", "mcp"]);
|
||||
@@ -135,6 +136,10 @@ async function dispatchCli(
|
||||
case "staff":
|
||||
case "staffs":
|
||||
return await handleContactCommand(parsed, runtime);
|
||||
case "task":
|
||||
case "tasks":
|
||||
case "todo":
|
||||
return await handleTaskCommand(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(7);
|
||||
expect(res.files.length).toBe(8);
|
||||
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);
|
||||
|
||||
@@ -9,6 +9,7 @@ import refBill from "../../skills/hose-use/references/bill.md" with { type: "tex
|
||||
import refWorkflow from "../../skills/hose-use/references/workflow.md" with { type: "text" };
|
||||
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" };
|
||||
|
||||
export const EMBEDDED_SKILL_FILES: Record<string, string> = {
|
||||
"SKILL.md": skillMain,
|
||||
@@ -16,6 +17,7 @@ export const EMBEDDED_SKILL_FILES: Record<string, string> = {
|
||||
"references/auth.md": refAuth,
|
||||
"references/bill.md": refBill,
|
||||
"references/contact.md": refContact,
|
||||
"references/task.md": refTask,
|
||||
"references/workflow.md": refWorkflow,
|
||||
"references/service.md": refService,
|
||||
};
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
import type { KyInstance } from "ky";
|
||||
import type { TaskQueryOptions, TaskView } from "./types.js";
|
||||
|
||||
const JOIN = [
|
||||
"flowId.form.payMoney.standardNumCode,standardNumCode,/v1/basedata/enumItems",
|
||||
"flowId.form.expenseMoney.standardNumCode,standardNumCode,/v1/basedata/enumItems",
|
||||
"flowId.form.loanMoney.standardNumCode,standardNumCode,/v1/basedata/enumItems",
|
||||
"flowId.form.requisitionMoney.standardNumCode,standardNumCode,/v1/basedata/enumItems",
|
||||
"flowId.id,nodeState,/flow/v2/backlogs/state",
|
||||
];
|
||||
|
||||
function dateFilter(options: TaskQueryOptions): string {
|
||||
if (options.from === undefined && options.to === undefined) return "";
|
||||
const from = options.from ?? 0;
|
||||
const to = options.to ?? 32503680000000;
|
||||
return `&&((updateTime > ${from}) && (updateTime < ${to}))`;
|
||||
}
|
||||
|
||||
function approvedDateFilter(options: TaskQueryOptions): string {
|
||||
if (options.from === undefined && options.to === undefined) return "";
|
||||
const from = options.from ?? 0;
|
||||
const to = options.to ?? 32503680000000;
|
||||
return `&&((approvedFlow.updateTime > ${from}) && (approvedFlow.updateTime < ${to}))`;
|
||||
}
|
||||
|
||||
function selectForBacklog(): string {
|
||||
return "flowId(ownerId(id,name,enName),form(title,code,specificationId(active,id,name,enName,nameSpell,code),submitDate,submitterId(id,name,enName,nickName,code,email,cellphone,note),reviewStatus,expenseMoney,loanMoney,requisitionMoney,payMoney),`...`),`...`";
|
||||
}
|
||||
|
||||
export async function fetchTaskPage(
|
||||
client: KyInstance,
|
||||
corpId: string,
|
||||
view: TaskView,
|
||||
options: TaskQueryOptions = {}
|
||||
): Promise<{ count: number; items: Record<string, unknown>[] }> {
|
||||
const start = options.start ?? 0;
|
||||
const count = options.count ?? 20;
|
||||
|
||||
if (view === "approved") {
|
||||
const response = await client.post("api/flow/v1/flows/approved", {
|
||||
searchParams: {
|
||||
join: "id,nodeState,/flow/v2/backlogs/state",
|
||||
corpId,
|
||||
},
|
||||
json: {
|
||||
query: {
|
||||
filterBy: `(approvedFlow.action==\"APPROVED\")&&((formType != \"permit\"))${approvedDateFilter(options)}`,
|
||||
limit: { start, count },
|
||||
select: "ownerId(id,name,enName),form(title,code,specificationId(active,id,name,enName,nameSpell,code),submitDate,submitterId(id,name,enName,nickName,code,email,cellphone,note),reviewStatus,expenseMoney,loanMoney,requisitionMoney,payMoney,`...`),`...`",
|
||||
},
|
||||
},
|
||||
});
|
||||
const json = (await response.json()) as { count?: number; items?: Record<string, unknown>[] };
|
||||
return { count: json.count ?? json.items?.length ?? 0, items: json.items ?? [] };
|
||||
}
|
||||
|
||||
const state = view === "todo" ? "APPROVING" : "PROCESSED";
|
||||
const orderBy = view === "todo"
|
||||
? [
|
||||
{ value: "flowId.state", order: "ASC" },
|
||||
{ value: "isUrgent", order: "DESC" },
|
||||
{ value: "updateTime", order: "ASC" },
|
||||
]
|
||||
: [{ value: "updateTime", order: "DESC" }];
|
||||
const searchParams = new URLSearchParams({ corpId });
|
||||
for (const join of JOIN) searchParams.append("join", join);
|
||||
const response = await client.post("api/flow/v2/backlogs/search", {
|
||||
searchParams,
|
||||
json: {
|
||||
filterBy: `(state.in(\"${state}\"))${dateFilter(options)}`,
|
||||
limit: { start, count },
|
||||
select: selectForBacklog(),
|
||||
orderBy,
|
||||
...(view === "todo" ? { creditSearch: true } : {}),
|
||||
},
|
||||
});
|
||||
const json = (await response.json()) as { count?: number; items?: Record<string, unknown>[] };
|
||||
return { count: json.count ?? json.items?.length ?? 0, items: json.items ?? [] };
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { normalizeTask } from "./tools.js";
|
||||
|
||||
describe("Task Domain - 待办任务模型", () => {
|
||||
test("normalizes backlog and preserves approval actions", () => {
|
||||
const item = normalizeTask({
|
||||
id: "backlog-1",
|
||||
state: "APPROVING",
|
||||
nodeState: { nodeName: "主管审批" },
|
||||
actions: { "staff-1": ["freeflow.agree", "freeflow.reject"] },
|
||||
flowId: {
|
||||
id: "flow-1",
|
||||
formType: "expense",
|
||||
ownerId: { id: "staff-1", name: "张三" },
|
||||
form: {
|
||||
code: "BX26000001",
|
||||
title: "差旅报销",
|
||||
specificationId: { name: "报销单" },
|
||||
submitterId: { id: "staff-2", name: "李四", code: "1002" },
|
||||
expenseMoney: { standard: "120.00", standardStrCode: "CNY", standardSymbol: "¥" },
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(item.taskId).toBe("backlog-1");
|
||||
expect(item.flowId).toBe("flow-1");
|
||||
expect(item.billCode).toBe("BX26000001");
|
||||
expect(item.submitter?.name).toBe("李四");
|
||||
expect(item.amount?.value).toBe("120.00");
|
||||
expect(item.actions).toEqual(["freeflow.agree", "freeflow.reject"]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,89 @@
|
||||
import type { KyInstance } from "ky";
|
||||
import { fetchTaskPage } from "./api.js";
|
||||
import type { TaskItem, TaskPageResult, TaskPerson, TaskQueryOptions, TaskView } from "./types.js";
|
||||
|
||||
function record(value: unknown): Record<string, unknown> {
|
||||
return value && typeof value === "object" ? value as Record<string, unknown> : {};
|
||||
}
|
||||
|
||||
function person(value: unknown): TaskPerson | undefined {
|
||||
const item = record(value);
|
||||
if (!Object.keys(item).length) return undefined;
|
||||
return {
|
||||
id: typeof item.id === "string" ? item.id : undefined,
|
||||
name: typeof item.name === "string" ? item.name : undefined,
|
||||
code: typeof item.code === "string" ? item.code : undefined,
|
||||
email: typeof item.email === "string" ? item.email : undefined,
|
||||
cellphone: typeof item.cellphone === "string" ? item.cellphone : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
function money(value: unknown): TaskItem["amount"] {
|
||||
const item = record(value);
|
||||
if (item.standard === undefined) return undefined;
|
||||
return {
|
||||
value: String(item.standard),
|
||||
currency: typeof item.standardStrCode === "string" ? item.standardStrCode : undefined,
|
||||
symbol: typeof item.standardSymbol === "string" ? item.standardSymbol : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
export function normalizeTask(raw: Record<string, unknown>): TaskItem {
|
||||
const flow = record(raw.flowId);
|
||||
const form = record(flow.form ?? raw.form);
|
||||
const nodeState = record(raw.nodeState ?? flow.nodeState);
|
||||
const amount = money(form.expenseMoney ?? form.payMoney ?? form.requisitionMoney ?? form.loanMoney);
|
||||
const specification = record(form.specificationId);
|
||||
const submitter = person(form.submitterId);
|
||||
const owner = person(flow.ownerId ?? raw.ownerId);
|
||||
const actionsByStaff = record(flow.actions ?? raw.actions);
|
||||
const actionValues = Object.values(actionsByStaff).flatMap((value) => Array.isArray(value) ? value : []);
|
||||
const logs = Array.isArray(flow.logs) ? flow.logs : [];
|
||||
const logIndex = typeof raw.logId === "number" ? raw.logId : -1;
|
||||
const log = record(logs[logIndex] ?? logs[logs.length - 1]);
|
||||
const attributes = record(log.attributes);
|
||||
return {
|
||||
taskId: String(raw.id ?? flow.id ?? ""),
|
||||
flowId: typeof flow.id === "string" ? flow.id : typeof raw.id === "string" ? raw.id : undefined,
|
||||
billId: typeof flow.id === "string" ? flow.id : undefined,
|
||||
billCode: typeof form.code === "string" ? form.code : undefined,
|
||||
title: typeof form.title === "string" ? form.title : undefined,
|
||||
templateName: typeof specification.name === "string" ? specification.name : undefined,
|
||||
formType: typeof flow.formType === "string" ? flow.formType : typeof raw.type === "string" ? raw.type : undefined,
|
||||
state: typeof raw.state === "string" ? raw.state : typeof flow.state === "string" ? flow.state : undefined,
|
||||
action: typeof log.action === "string" ? log.action : typeof raw.action === "string" ? raw.action : undefined,
|
||||
actionTime: typeof log.time === "number" ? log.time : undefined,
|
||||
comment: typeof attributes.comment === "string" ? attributes.comment : undefined,
|
||||
nodeName: typeof nodeState.nodeName === "string" ? nodeState.nodeName : typeof raw.nodeName === "string" ? raw.nodeName : undefined,
|
||||
submitDate: typeof form.submitDate === "number" ? form.submitDate : undefined,
|
||||
submitter,
|
||||
owner,
|
||||
amount,
|
||||
actions: actionValues.filter((value): value is string => typeof value === "string"),
|
||||
updatedAt: typeof flow.updateTime === "number" ? flow.updateTime : typeof raw.updateTime === "number" ? raw.updateTime : undefined,
|
||||
raw,
|
||||
};
|
||||
}
|
||||
|
||||
export async function listTasks(
|
||||
client: KyInstance,
|
||||
corpId: string,
|
||||
view: TaskView,
|
||||
options: TaskQueryOptions = {}
|
||||
): Promise<TaskPageResult> {
|
||||
const page = await fetchTaskPage(client, corpId, view, options);
|
||||
let items = page.items.map(normalizeTask);
|
||||
if (options.staffId) {
|
||||
const target = options.staffId.toLowerCase();
|
||||
items = items.filter((item) => [item.submitter?.id, item.submitter?.code, item.owner?.id, item.owner?.code]
|
||||
.filter(Boolean)
|
||||
.some((value) => String(value).toLowerCase().includes(target)));
|
||||
}
|
||||
return {
|
||||
view,
|
||||
total: page.count,
|
||||
start: options.start ?? 0,
|
||||
count: options.count ?? 20,
|
||||
items,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const TaskViewSchema = z.enum(["todo", "approved", "processed"]);
|
||||
export type TaskView = z.infer<typeof TaskViewSchema>;
|
||||
|
||||
export interface TaskQueryOptions {
|
||||
start?: number;
|
||||
count?: number;
|
||||
from?: number;
|
||||
to?: number;
|
||||
staffId?: string;
|
||||
}
|
||||
|
||||
export interface TaskPerson {
|
||||
id?: string;
|
||||
name?: string;
|
||||
code?: string;
|
||||
email?: string;
|
||||
cellphone?: string;
|
||||
}
|
||||
|
||||
export interface TaskItem {
|
||||
taskId: string;
|
||||
flowId?: string;
|
||||
billId?: string;
|
||||
billCode?: string;
|
||||
title?: string;
|
||||
templateName?: string;
|
||||
formType?: string;
|
||||
state?: string;
|
||||
action?: string;
|
||||
actionTime?: number;
|
||||
comment?: string;
|
||||
nodeName?: string;
|
||||
submitDate?: number;
|
||||
submitter?: TaskPerson;
|
||||
owner?: TaskPerson;
|
||||
amount?: { value: string; currency?: string; symbol?: string };
|
||||
actions?: string[];
|
||||
updatedAt?: number;
|
||||
raw: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface TaskPageResult {
|
||||
view: TaskView;
|
||||
total: number;
|
||||
start: number;
|
||||
count: number;
|
||||
items: TaskItem[];
|
||||
}
|
||||
Reference in new issue
Block a user