@stalfh233/omc-cli 0.7.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/coverage/m1-coverage-manifest-v1.json +88 -2
- package/dist/bootstrap.js +49 -0
- package/dist/call.js +23 -8
- package/dist/capability-overview.js +16 -4
- package/dist/cli.js +36 -16
- package/dist/contract-resources.js +2 -0
- package/dist/coverage-registry.js +4 -0
- package/dist/coverage.js +16 -2
- package/dist/endpoints.js +65 -0
- package/dist/environment-policy.js +15 -1
- package/dist/field-change.js +9 -2
- package/dist/field-families.js +27 -6
- package/dist/governed-execution.js +18 -12
- package/dist/human-summary.js +8 -0
- package/dist/intent/capabilities/field.js +6 -0
- package/dist/lanes/environment.js +3 -0
- package/dist/lanes/field-contract.js +117 -6
- package/dist/lanes/file-contract.js +69 -0
- package/dist/lanes/file-coverage.js +16 -0
- package/dist/lanes/file.js +257 -0
- package/dist/lanes/lane-ids.js +1 -0
- package/dist/lanes/list-view-contract.js +3 -3
- package/dist/lanes/list-view-shapes.js +56 -0
- package/dist/lanes/list-view.js +40 -5
- package/dist/lanes/meta.js +5 -4
- package/dist/lanes/model.js +8 -4
- package/dist/lanes/org-query-contract.js +62 -0
- package/dist/lanes/org-query-coverage.js +27 -0
- package/dist/lanes/org-query.js +610 -0
- package/dist/lanes/organization.js +44 -11
- package/dist/lanes/runtime-data.js +228 -59
- package/dist/lanes/workflow-contract.js +110 -1
- package/dist/lanes/workflow-coverage.js +13 -0
- package/dist/lanes/workflow.js +710 -13
- package/dist/omc-core-lazy.js +37 -1
- package/dist/package.js +3 -1
- package/dist/read-shapes.js +3 -3
- package/dist/recipes/runtime-data.js +8 -6
- package/dist/refusal-guidance.js +2 -1
- package/dist/rule-support.js +15 -17
- package/dist/session-manager.js +9 -3
- package/dist/session.js +70 -11
- package/dist/token-cache.js +5 -0
- package/dist/tool-types.js +15 -1
- package/dist/tools.js +18 -0
- package/dist/version-family-gaps.js +8 -1
- package/dist/version-partition.js +633 -0
- package/dist/wire.js +8 -2
- package/dist/write.js +11 -2
- package/package.json +3 -3
- package/skills/omc-capabilities.md +9 -2
- package/skills/omc-refusals.md +18 -0
- package/skills/omc-write-hazards.md +8 -0
|
@@ -0,0 +1,610 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { ToolInputError } from "../tool-types.js";
|
|
3
|
+
import { defineTool } from "../tool-types.js";
|
|
4
|
+
import { governedRead } from "../read.js";
|
|
5
|
+
import { wireCall } from "../wire.js";
|
|
6
|
+
import { ENDPOINTS } from "../endpoints.js";
|
|
7
|
+
import { environmentArg, capabilityArg } from "../args.js";
|
|
8
|
+
import { LANES } from "./lane-ids.js";
|
|
9
|
+
import { collectOrgDepartments } from "./org-tree.js";
|
|
10
|
+
import { ORG_QUERY_ENDPOINTS, ORG_QUERY_SCOPES } from "./org-query-contract.js";
|
|
11
|
+
/**
|
|
12
|
+
* Organization light-query read faces.
|
|
13
|
+
*
|
|
14
|
+
* Five read-only tools that answer the everyday "who/where in the org" questions
|
|
15
|
+
* an agent asks before it can fill a workflow participant, an owner, or a
|
|
16
|
+
* department scope. They mirror the platform MCP organization tool set, but use
|
|
17
|
+
* the session controller endpoints because the openapi v3 equivalents require an
|
|
18
|
+
* OAuth2 client id the CLI does not have (see `org-query-contract.ts`).
|
|
19
|
+
*
|
|
20
|
+
* Every tool is `kind:"read"`: no writable gate, no production confirmation.
|
|
21
|
+
* Pagination is explicit and never inlines an unbounded result: server-paged
|
|
22
|
+
* faces pass `page/size` through, locally paged faces slice here, and the shared
|
|
23
|
+
* `governedRead` pipeline still externalizes oversized payloads to an artifact
|
|
24
|
+
* (`detailFile` + `detailSha256`).
|
|
25
|
+
*/
|
|
26
|
+
const LANE = LANES.workflow;
|
|
27
|
+
/** Server rows accepted for the locally-paged user face (platform ignores its Pageable). */
|
|
28
|
+
const USER_SCAN_LIMIT = 2000;
|
|
29
|
+
/** Department-walk fallback bound (a mobile-only lookup has no server filter). */
|
|
30
|
+
const MOBILE_SCAN_DEPARTMENT_LIMIT = 200;
|
|
31
|
+
/** `detailed` adds one department read per row; keep the N+1 bounded. */
|
|
32
|
+
const DETAILED_MAX_PAGE_SIZE = 50;
|
|
33
|
+
const DEPT_VISIBLE_LABELS = Object.freeze({
|
|
34
|
+
ALL_VISIBLE: "所有人/部门可见",
|
|
35
|
+
PART_VISIBLE: "指定人/部门可见",
|
|
36
|
+
NOT_VISIBLE: "均不可见",
|
|
37
|
+
});
|
|
38
|
+
const STAFF_VISIBLE_LABELS = Object.freeze({
|
|
39
|
+
ALL_VISIBLE: "全部",
|
|
40
|
+
// The platform enum really is spelled `PRAT_VISIBLE` (StaffVisibleType.java).
|
|
41
|
+
PRAT_VISIBLE: "指定人/部门可见",
|
|
42
|
+
ONLY_DEPT: "所在部门及下级部门",
|
|
43
|
+
ONESELF: "仅自己",
|
|
44
|
+
});
|
|
45
|
+
function asRecord(value) {
|
|
46
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : null;
|
|
47
|
+
}
|
|
48
|
+
/** Tolerant row extraction: raw array, `{content:[…]}` PageVO, or nothing. */
|
|
49
|
+
function asRows(value) {
|
|
50
|
+
if (Array.isArray(value))
|
|
51
|
+
return value.filter((row) => asRecord(row) !== null);
|
|
52
|
+
const record = asRecord(value);
|
|
53
|
+
if (record && Array.isArray(record.content)) {
|
|
54
|
+
return record.content.filter((row) => asRecord(row) !== null);
|
|
55
|
+
}
|
|
56
|
+
return [];
|
|
57
|
+
}
|
|
58
|
+
function textOf(row, keys) {
|
|
59
|
+
for (const key of keys) {
|
|
60
|
+
const value = row[key];
|
|
61
|
+
if (value !== undefined && value !== null && value !== "")
|
|
62
|
+
return String(value);
|
|
63
|
+
}
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
/** `UserStatus` is an IStringEnum; tolerate a string or a `{name|value|code}` shell. */
|
|
67
|
+
function statusName(value) {
|
|
68
|
+
if (value === null || value === undefined || value === "")
|
|
69
|
+
return null;
|
|
70
|
+
if (typeof value === "string")
|
|
71
|
+
return value;
|
|
72
|
+
const record = asRecord(value);
|
|
73
|
+
if (record !== null)
|
|
74
|
+
return textOf(record, ["name", "value", "code"]);
|
|
75
|
+
return String(value);
|
|
76
|
+
}
|
|
77
|
+
function projectUser(row) {
|
|
78
|
+
return {
|
|
79
|
+
id: textOf(row, ["id"]),
|
|
80
|
+
name: textOf(row, ["name"]),
|
|
81
|
+
username: textOf(row, ["username"]),
|
|
82
|
+
userId: textOf(row, ["userId"]),
|
|
83
|
+
mobile: textOf(row, ["mobile"]),
|
|
84
|
+
status: statusName(row.status),
|
|
85
|
+
departmentId: textOf(row, ["departmentId", "currentDeptId"]),
|
|
86
|
+
corpId: textOf(row, ["corpId"]),
|
|
87
|
+
sortKey: row.sortKey ?? null,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function projectDepartment(row) {
|
|
91
|
+
return {
|
|
92
|
+
id: textOf(row, ["id", "deptId"]),
|
|
93
|
+
name: textOf(row, ["name", "deptName"]),
|
|
94
|
+
code: textOf(row, ["code"]),
|
|
95
|
+
corpId: textOf(row, ["corpId"]),
|
|
96
|
+
parentId: textOf(row, ["parentId"]),
|
|
97
|
+
...(row.sortKey !== undefined ? { sortKey: row.sortKey } : {}),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
function slicePage(rows, page, size) {
|
|
101
|
+
const offset = page * size;
|
|
102
|
+
const items = rows.slice(offset, offset + size);
|
|
103
|
+
return { items: [...items], offset, hasMore: offset + size < rows.length };
|
|
104
|
+
}
|
|
105
|
+
function pageTotalOf(data, rows) {
|
|
106
|
+
const record = asRecord(data);
|
|
107
|
+
const total = record?.totalElements;
|
|
108
|
+
return typeof total === "number" && Number.isFinite(total) ? total : rows.length;
|
|
109
|
+
}
|
|
110
|
+
/** `deptVisibleScope` / `staffVisibleScope` are JSON strings on 8.6.26. */
|
|
111
|
+
function visibleScopeOf(raw) {
|
|
112
|
+
if (raw === null || raw === undefined || raw === "")
|
|
113
|
+
return null;
|
|
114
|
+
const item = (value) => {
|
|
115
|
+
const row = asRecord(value) ?? {};
|
|
116
|
+
return { id: row.id ?? null, name: row.name ?? null, unitType: row.unitType ?? null };
|
|
117
|
+
};
|
|
118
|
+
if (Array.isArray(raw))
|
|
119
|
+
return { items: raw.map(item) };
|
|
120
|
+
if (typeof raw === "string") {
|
|
121
|
+
try {
|
|
122
|
+
const parsed = JSON.parse(raw);
|
|
123
|
+
if (Array.isArray(parsed))
|
|
124
|
+
return { items: parsed.map(item) };
|
|
125
|
+
return { items: null, raw: raw.length > 2000 ? `${raw.slice(0, 2000)}…` : raw, note: "可见域不是 JSON 数组,原样回传 raw" };
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
return { items: null, raw: raw.length > 2000 ? `${raw.slice(0, 2000)}…` : raw, note: "可见域不是合法 JSON,原样回传 raw" };
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return { items: null, raw: JSON.stringify(raw) };
|
|
132
|
+
}
|
|
133
|
+
function digitsOf(value) {
|
|
134
|
+
return String(value ?? "").replace(/\D/g, "");
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Mobile compare: exact digits, or a digit-suffix match for queries of 7+ digits
|
|
138
|
+
* so `+86 138…`, `138-…`, `138 …` all hit the stored value.
|
|
139
|
+
*/
|
|
140
|
+
function mobileMatches(candidate, query) {
|
|
141
|
+
const want = digitsOf(query);
|
|
142
|
+
if (want === "")
|
|
143
|
+
return false;
|
|
144
|
+
const have = digitsOf(candidate);
|
|
145
|
+
if (have === want)
|
|
146
|
+
return true;
|
|
147
|
+
const shorter = want.length <= have.length ? want : have;
|
|
148
|
+
const longer = want.length <= have.length ? have : want;
|
|
149
|
+
return shorter.length >= 7 && longer.endsWith(shorter);
|
|
150
|
+
}
|
|
151
|
+
function capRowsInline(result, specs) {
|
|
152
|
+
let output = result;
|
|
153
|
+
let omitted = false;
|
|
154
|
+
for (const [key, keep] of specs) {
|
|
155
|
+
const rows = Array.isArray(output[key]) ? output[key] : null;
|
|
156
|
+
if (rows === null || rows.length <= keep)
|
|
157
|
+
continue;
|
|
158
|
+
output = {
|
|
159
|
+
...output,
|
|
160
|
+
[key]: rows.slice(0, keep),
|
|
161
|
+
[`${key}InlineOmitted`]: rows.length - keep,
|
|
162
|
+
[`${key}Count`]: rows.length,
|
|
163
|
+
};
|
|
164
|
+
omitted = true;
|
|
165
|
+
}
|
|
166
|
+
return omitted
|
|
167
|
+
? { ...output, omitted: true, artifactHint: "完整行在 detailFile/detailArtifactId;内联只保留前若干行" }
|
|
168
|
+
: output;
|
|
169
|
+
}
|
|
170
|
+
async function scanUsersByDepartmentTree(session) {
|
|
171
|
+
const walked = await collectOrgDepartments(session, { limit: MOBILE_SCAN_DEPARTMENT_LIMIT });
|
|
172
|
+
const seen = new Map();
|
|
173
|
+
let departmentsScanned = 0;
|
|
174
|
+
for (const department of walked.rows) {
|
|
175
|
+
if (department.id === "")
|
|
176
|
+
continue;
|
|
177
|
+
departmentsScanned += 1;
|
|
178
|
+
const data = (await wireCall(session, {
|
|
179
|
+
path: ENDPOINTS.organizationDepartmentUsers,
|
|
180
|
+
query: { corpId: session.enterpriseId(), deptId: department.id },
|
|
181
|
+
})).data;
|
|
182
|
+
for (const row of asRows(data)) {
|
|
183
|
+
const id = textOf(row, ["id", "userId"]);
|
|
184
|
+
if (id === null || seen.has(id))
|
|
185
|
+
continue;
|
|
186
|
+
seen.set(id, { ...row, departmentId: textOf(row, ["departmentId"]) ?? department.id });
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return { users: [...seen.values()], departmentsScanned, truncated: walked.truncated };
|
|
190
|
+
}
|
|
191
|
+
export function registerOrgQueryLane(tools) {
|
|
192
|
+
tools.push(defineTool({
|
|
193
|
+
name: "org.searchUnits",
|
|
194
|
+
search: { intents: ["按名字搜人和部门", "搜部门", "找人找部门", "组织单元搜索"] },
|
|
195
|
+
layer: "low",
|
|
196
|
+
kind: "read",
|
|
197
|
+
lane: LANE,
|
|
198
|
+
scope: ORG_QUERY_SCOPES.searchUnits,
|
|
199
|
+
description: "按名称同时搜用户和部门(`POST organization/user/search` + `POST organization/user/searchDepartments`,对应平台 MCP 的 organization_search_units)。返回两条独立命中清单:users[{id,name,username,userId,mobile,status,departmentId,corpId}] 与 departments[{id,name,code,corpId,parentId}],各带 total。**陷阱**:①关键字必填——平台对空 `wd` 直接回 10005「关键字不能为空」,空关键字不是「返回全部」;②用户搜索 `page` 从 0 开始,服务端默认每页 100,本工具默认 50、上限 100;③部门搜索**不分页**(一次回全部命中),本工具按同一个 page/size 在本地切片,`departmentsTotal` 才是真实命中数、`departmentsTruncated` 表示本页之外还有;④用户侧是姓名/账号/拼音的包含匹配,部门侧是部门名包含匹配;⑤用户结果会剔除已删除用户与内置 admin 账号。只想搜部门用 org.department.search(带 queryCode);要「某部门下的全部成员」用 org.department.users;要按手机号/部门组合筛在职用户用 org.users。",
|
|
200
|
+
inputSchema: {
|
|
201
|
+
environment: environmentArg,
|
|
202
|
+
name: z.string().trim().min(1).describe("名称关键字(平台 `wd`;空串/空白会被平台拒 10005)。用户按姓名/账号/拼音包含匹配,部门按名称包含匹配"),
|
|
203
|
+
keyword: z.string().trim().min(1).optional().describe("`name` 的别名"),
|
|
204
|
+
page: z.number().int().min(0).optional().describe("页号,从 0 开始,默认 0。用户侧服务端分页,部门侧本地切片"),
|
|
205
|
+
size: z.number().int().min(1).max(100).optional().describe("每页条数,默认 50,上限 100"),
|
|
206
|
+
capability: capabilityArg,
|
|
207
|
+
},
|
|
208
|
+
handler: async (ctx, args, self) => {
|
|
209
|
+
const name = args.name ?? args.keyword;
|
|
210
|
+
if (name === undefined) {
|
|
211
|
+
throw new ToolInputError({
|
|
212
|
+
status: "error",
|
|
213
|
+
blocker: "tool-input-invalid",
|
|
214
|
+
tool: "org.searchUnits",
|
|
215
|
+
message: "需要 name(平台 wd 不接受空串;空关键字不是「返回全部」)",
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
const page = args.page ?? 0;
|
|
219
|
+
const size = args.size ?? 50;
|
|
220
|
+
return governedRead(ctx, self, { ...args, name }, async (session) => {
|
|
221
|
+
const userData = (await wireCall(session, {
|
|
222
|
+
path: ORG_QUERY_ENDPOINTS.userSearch,
|
|
223
|
+
method: "POST",
|
|
224
|
+
body: { wd: name, page, size },
|
|
225
|
+
})).data;
|
|
226
|
+
const users = asRows(userData).map(projectUser);
|
|
227
|
+
const usersTotal = pageTotalOf(userData, users);
|
|
228
|
+
const departmentData = (await wireCall(session, {
|
|
229
|
+
path: ORG_QUERY_ENDPOINTS.userSearchDepartments,
|
|
230
|
+
method: "POST",
|
|
231
|
+
body: { wd: name },
|
|
232
|
+
})).data;
|
|
233
|
+
const allDepartments = asRows(departmentData);
|
|
234
|
+
const departmentSlice = slicePage(allDepartments, page, size);
|
|
235
|
+
const departments = departmentSlice.items.map(projectDepartment);
|
|
236
|
+
return {
|
|
237
|
+
name,
|
|
238
|
+
page,
|
|
239
|
+
size,
|
|
240
|
+
users,
|
|
241
|
+
usersTotal,
|
|
242
|
+
usersHasMore: page * size + users.length < usersTotal,
|
|
243
|
+
departments,
|
|
244
|
+
departmentsTotal: allDepartments.length,
|
|
245
|
+
departmentsTruncated: departmentSlice.hasMore,
|
|
246
|
+
nextPage: page * size + users.length < usersTotal || departmentSlice.hasMore ? page + 1 : null,
|
|
247
|
+
note: "部门侧平台不分页,按同一 page/size 本地切片;departmentsTotal 是真实命中数。",
|
|
248
|
+
};
|
|
249
|
+
}, { compact: (result) => capRowsInline(result, [["users", 50], ["departments", 50]]) });
|
|
250
|
+
},
|
|
251
|
+
}));
|
|
252
|
+
tools.push(defineTool({
|
|
253
|
+
name: "org.department.search",
|
|
254
|
+
search: { intents: ["按名字搜部门", "找部门 id", "部门叫什么", "搜组织"] },
|
|
255
|
+
layer: "low",
|
|
256
|
+
kind: "read",
|
|
257
|
+
lane: LANE,
|
|
258
|
+
scope: ORG_QUERY_SCOPES.departmentSearch,
|
|
259
|
+
description: "按名称搜部门(`POST organization/user/searchDepartments`,body `{wd}`)。返回 departments[{id,name,parentId,queryCode}]、total、hasMore。**用途**:`org.department.list` 是整棵树的 BFS(受上限截断),大租户里按名字定位部门时它会漏;本工具直接命中服务端的部门名包含查询。**陷阱**:①`wd` 必填且空白会被平台拒(10005「关键字不能为空」);②平台这个端点**不分页**,一次返回全部命中,本工具的 `page`(从 0 开始)/`size`(默认 50,上限 100)是**本地切片**,`total` 才是真实命中数;③`queryCode` 是平台的层级编码(`A#B#C` 形状,用于可见范围/权限匹配),不是 `code`;④结果只含名称命中的部门,没有上下级补全——要层级关系用 org.department.list 或 org.departmentTree。",
|
|
260
|
+
inputSchema: {
|
|
261
|
+
environment: environmentArg,
|
|
262
|
+
wd: z.string().trim().min(1).describe("部门名称关键字(平台 `wd`;空串/空白报 10005)"),
|
|
263
|
+
page: z.number().int().min(0).optional().describe("页号,从 0 开始,默认 0(本地切片)"),
|
|
264
|
+
size: z.number().int().min(1).max(100).optional().describe("每页条数,默认 50,上限 100"),
|
|
265
|
+
capability: capabilityArg,
|
|
266
|
+
},
|
|
267
|
+
handler: async (ctx, args, self) => governedRead(ctx, self, args, async (session) => {
|
|
268
|
+
const page = args.page ?? 0;
|
|
269
|
+
const size = args.size ?? 50;
|
|
270
|
+
const data = (await wireCall(session, {
|
|
271
|
+
path: ORG_QUERY_ENDPOINTS.userSearchDepartments,
|
|
272
|
+
method: "POST",
|
|
273
|
+
body: { wd: args.wd },
|
|
274
|
+
})).data;
|
|
275
|
+
const allDepartments = asRows(data);
|
|
276
|
+
const sliced = slicePage(allDepartments, page, size);
|
|
277
|
+
const departments = sliced.items.map((row) => ({
|
|
278
|
+
id: textOf(row, ["id", "deptId"]),
|
|
279
|
+
name: textOf(row, ["name", "deptName"]),
|
|
280
|
+
parentId: textOf(row, ["parentId"]),
|
|
281
|
+
queryCode: textOf(row, ["queryCode"]),
|
|
282
|
+
}));
|
|
283
|
+
return {
|
|
284
|
+
wd: args.wd,
|
|
285
|
+
page,
|
|
286
|
+
size,
|
|
287
|
+
offset: sliced.offset,
|
|
288
|
+
total: allDepartments.length,
|
|
289
|
+
returned: departments.length,
|
|
290
|
+
hasMore: sliced.hasMore,
|
|
291
|
+
nextPage: sliced.hasMore ? page + 1 : null,
|
|
292
|
+
truncated: sliced.hasMore,
|
|
293
|
+
departments,
|
|
294
|
+
};
|
|
295
|
+
}, {
|
|
296
|
+
compact: (result) => {
|
|
297
|
+
const departments = Array.isArray(result.departments) ? result.departments : [];
|
|
298
|
+
if (departments.length <= 50)
|
|
299
|
+
return result;
|
|
300
|
+
return {
|
|
301
|
+
...result,
|
|
302
|
+
departments: departments.slice(0, 50),
|
|
303
|
+
departmentsInlineOmitted: departments.length - 50,
|
|
304
|
+
departmentsCount: departments.length,
|
|
305
|
+
omitted: true,
|
|
306
|
+
};
|
|
307
|
+
},
|
|
308
|
+
}),
|
|
309
|
+
}));
|
|
310
|
+
tools.push(defineTool({
|
|
311
|
+
name: "org.visibleDepartments",
|
|
312
|
+
search: { intents: ["选人可见范围", "部门可见范围", "这个部门能看到谁", "选人控件权限"] },
|
|
313
|
+
layer: "low",
|
|
314
|
+
kind: "read",
|
|
315
|
+
lane: LANE,
|
|
316
|
+
scope: ORG_QUERY_SCOPES.visibleDepartments,
|
|
317
|
+
description: "查部门「选人可见范围」(`GET organization/department/search_dept_detail?deptId=` 的 `data.scopeModel`,与平台 MCP organization_query_visible_departments 同源 `DepartmentFacade.getSelectionScope`)。逐个 departmentId 读,返回 scopes[](配置过的部门)与 unconfigured[](没有范围行的部门)。**陷阱**:①平台只在**配置过**选人范围时返回 scopeModel,没有配置行 = `unconfigured`,不要读成「全不可见」;②`deptVisibleType` 是 ALL_VISIBLE / PART_VISIBLE / NOT_VISIBLE(所有人部门可见 / 指定人部门可见 / 均不可见),`staffVisibleType` 是 ALL_VISIBLE / PRAT_VISIBLE(平台拼写就是 PRAT)/ ONLY_DEPT / ONESELF;③可见域 `deptVisibleScope`、`staffVisibleScope` 在平台上存成 JSON **字符串**,工具解析成 items[{id,name,unitType}],解析失败时原样回 `raw` 并注 note;④`specialStaffVisibleRules`(指定成员可见规则)只有 2.1.5 返回,8.6 没有——字段缺失就是平台没给,不要读成「空规则」;⑤只读:设置/修改范围不在这里,用权限范围写面。",
|
|
318
|
+
inputSchema: {
|
|
319
|
+
environment: environmentArg,
|
|
320
|
+
departmentIds: z.array(z.string().min(1)).min(1).max(50).describe("要读可见范围的部门 id 列表(1..50 个;每个一次 GET)"),
|
|
321
|
+
capability: capabilityArg,
|
|
322
|
+
},
|
|
323
|
+
handler: async (ctx, args, self) => governedRead(ctx, self, args, async (session) => {
|
|
324
|
+
const scopes = [];
|
|
325
|
+
const unconfigured = [];
|
|
326
|
+
for (const departmentId of args.departmentIds) {
|
|
327
|
+
const data = (await wireCall(session, {
|
|
328
|
+
path: ORG_QUERY_ENDPOINTS.departmentScopeDetail,
|
|
329
|
+
query: { deptId: departmentId },
|
|
330
|
+
})).data;
|
|
331
|
+
const detail = asRecord(data);
|
|
332
|
+
const scope = asRecord(detail?.scopeModel);
|
|
333
|
+
if (scope === null) {
|
|
334
|
+
unconfigured.push(departmentId);
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
const deptVisibleType = textOf(scope, ["deptVisibleType"]);
|
|
338
|
+
const staffVisibleType = textOf(scope, ["staffVisibleType"]);
|
|
339
|
+
const detailRecord = detail ?? {};
|
|
340
|
+
scopes.push({
|
|
341
|
+
departmentId: textOf(scope, ["departmentId"]) ?? textOf(detailRecord, ["deptId"]) ?? departmentId,
|
|
342
|
+
deptName: textOf(detailRecord, ["deptName"]),
|
|
343
|
+
parentId: textOf(detailRecord, ["parentId"]),
|
|
344
|
+
deptVisibleType,
|
|
345
|
+
deptVisibleTypeLabel: deptVisibleType === null ? null : DEPT_VISIBLE_LABELS[deptVisibleType] ?? "未知枚举",
|
|
346
|
+
deptVisibleScope: visibleScopeOf(scope.deptVisibleScope),
|
|
347
|
+
staffVisibleType,
|
|
348
|
+
staffVisibleTypeLabel: staffVisibleType === null ? null : STAFF_VISIBLE_LABELS[staffVisibleType] ?? "未知枚举",
|
|
349
|
+
staffVisibleScope: visibleScopeOf(scope.staffVisibleScope),
|
|
350
|
+
...(scope.specialStaffVisibleRules === undefined
|
|
351
|
+
? {}
|
|
352
|
+
: { specialStaffVisibleRules: scope.specialStaffVisibleRules }),
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
return {
|
|
356
|
+
requested: args.departmentIds,
|
|
357
|
+
count: scopes.length,
|
|
358
|
+
scopes,
|
|
359
|
+
unconfigured,
|
|
360
|
+
...(unconfigured.length > 0
|
|
361
|
+
? { note: "unconfigured 的部门没有选人范围行(平台未配置),不是「全不可见」;未配置时的实际可见性由上级/平台默认规则决定,工具不臆测。" }
|
|
362
|
+
: {}),
|
|
363
|
+
...(scopes.some((scope) => scope.specialStaffVisibleRules === undefined)
|
|
364
|
+
? { specialStaffVisibleRulesNote: "平台版本不返回 specialStaffVisibleRules(8.6 没有;2.1.5 才有),字段缺失≠空规则。" }
|
|
365
|
+
: {}),
|
|
366
|
+
};
|
|
367
|
+
}, {
|
|
368
|
+
compact: (result) => {
|
|
369
|
+
const scopes = Array.isArray(result.scopes) ? result.scopes : [];
|
|
370
|
+
return {
|
|
371
|
+
...result,
|
|
372
|
+
scopes: scopes.slice(0, 20),
|
|
373
|
+
...(scopes.length > 20 ? { scopesInlineOmitted: scopes.length - 20, scopesCount: scopes.length } : {}),
|
|
374
|
+
};
|
|
375
|
+
},
|
|
376
|
+
}),
|
|
377
|
+
}));
|
|
378
|
+
tools.push(defineTool({
|
|
379
|
+
name: "org.departmentTree",
|
|
380
|
+
search: { intents: ["查子部门", "部门下一级", "父部门下的部门", "组织树下级"] },
|
|
381
|
+
layer: "low",
|
|
382
|
+
kind: "read",
|
|
383
|
+
lane: LANE,
|
|
384
|
+
scope: ORG_QUERY_SCOPES.departmentTree,
|
|
385
|
+
description: "查指定父部门的**直属子部门**(`GET organization/department/tree?deptIds=<parentId>`,对应平台 MCP organization_query_department_tree)。省略 parentId 时按平台语义取「企业根部门的子部门」:先 `GET organization/department/get_corp_root_dept_info?corpId=` 解析根部门,再取它的直属子部门;根部门解析失败则直接列根部门并置 `rootFallback:true`。**陷阱**:①`deptIds` 传父部门 id 只回**一层**——平台默认走 `getLevelChildDeptList(list, 2)`,不会展开整棵子树;②空 `deptIds` 回的是**根部门**,不是全部部门;③平台已过滤已删除/隐藏部门并按自身规则排序,工具不再重排;④没有分页:一个父部门的子部门一次返回,超过 200 条会 `truncated:true` 并把完整结果外置到 artifact。要整棵部门树(多层 BFS)用 org.department.list;要部门详情/可见范围用 org.visibleDepartments。",
|
|
386
|
+
inputSchema: {
|
|
387
|
+
environment: environmentArg,
|
|
388
|
+
parentId: z.string().min(1).optional().describe("父部门 id;省略=企业根部门的直属子部门。只返回一层,不展开子树"),
|
|
389
|
+
capability: capabilityArg,
|
|
390
|
+
},
|
|
391
|
+
handler: async (ctx, args, self) => governedRead(ctx, self, args, async (session) => {
|
|
392
|
+
let parentId = args.parentId;
|
|
393
|
+
let resolvedFromCorpRoot = false;
|
|
394
|
+
let rootFallback = false;
|
|
395
|
+
if (parentId === undefined) {
|
|
396
|
+
try {
|
|
397
|
+
const root = asRecord((await wireCall(session, {
|
|
398
|
+
path: ORG_QUERY_ENDPOINTS.corpRootDepartment,
|
|
399
|
+
query: { corpId: session.enterpriseId() },
|
|
400
|
+
})).data);
|
|
401
|
+
const rootId = root === null ? null : textOf(root, ["id", "deptId"]);
|
|
402
|
+
if (rootId !== null) {
|
|
403
|
+
parentId = rootId;
|
|
404
|
+
resolvedFromCorpRoot = true;
|
|
405
|
+
}
|
|
406
|
+
else {
|
|
407
|
+
rootFallback = true;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
catch {
|
|
411
|
+
rootFallback = true;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
const data = (await wireCall(session, {
|
|
415
|
+
path: ORG_QUERY_ENDPOINTS.departmentTree,
|
|
416
|
+
query: parentId === undefined ? {} : { deptIds: parentId },
|
|
417
|
+
})).data;
|
|
418
|
+
const tree = asRecord(data);
|
|
419
|
+
const allDepartments = asRows(tree?.departmentList);
|
|
420
|
+
const departments = allDepartments.slice(0, 200).map(projectDepartment);
|
|
421
|
+
const myDepartment = asRows(tree?.myDepartment).map(projectDepartment);
|
|
422
|
+
return {
|
|
423
|
+
parentId: parentId ?? null,
|
|
424
|
+
resolvedFromCorpRoot,
|
|
425
|
+
...(rootFallback ? { rootFallback: true } : {}),
|
|
426
|
+
scopeOfResult: parentId === undefined ? "roots" : "direct-children",
|
|
427
|
+
count: departments.length,
|
|
428
|
+
departments,
|
|
429
|
+
...(myDepartment.length > 0 ? { myDepartment } : {}),
|
|
430
|
+
...(allDepartments.length > 200
|
|
431
|
+
? { truncated: true, truncatedKeys: ["departments"], departmentsCount: allDepartments.length, note: "子部门超过 200 条,内联只保留前 200;完整结果在 detailFile。" }
|
|
432
|
+
: {}),
|
|
433
|
+
...(departments.length === 0
|
|
434
|
+
? { note: "没有子部门:父部门 id 不存在 / 是叶子部门 / 该部门对当前账号不可见。" }
|
|
435
|
+
: {}),
|
|
436
|
+
};
|
|
437
|
+
}, { compact: (result) => capRowsInline(result, [["departments", 50]]) }),
|
|
438
|
+
}));
|
|
439
|
+
tools.push(defineTool({
|
|
440
|
+
name: "org.users",
|
|
441
|
+
search: { intents: ["查在职用户", "按手机号找人", "部门下的人分页", "用户清单"] },
|
|
442
|
+
layer: "low",
|
|
443
|
+
kind: "read",
|
|
444
|
+
lane: LANE,
|
|
445
|
+
scope: ORG_QUERY_SCOPES.users,
|
|
446
|
+
description: "分页查在职用户(`POST organization/user/searchCondiction`,对应平台 MCP 的 organization_query_simple_users / organization_query_detailed_users)。服务端条件:name(姓名 like)、departmentIds、userIds(**业务 userId 列**,不是记录 id)、roleIds;手机号 mobile 在本地按「数字后缀」匹配(容忍 +86/空格/短横线)。projection=simple 回 id/name/username/userId/mobile/status/departmentId/corpId/sortKey;projection=detailed 再带 email/employeeNo/createdTime/modifiedTime 与 departments[{deptId,deptName,isMain}]。**detailed 的成本**:每行多一次 `GET organization/user/departments?id=`(N+1),成本随 `size` 线性增长,所以 **detailed 时 `size` 实际有效上限是 50**(`simple` 可到 100);detailed+size>50 在本地直接拒绝,零请求。**陷阱**:①平台这个端点**忽略 page/size**(服务层没把 Pageable 传给仓储),所以 `page` 从 0 开始是**工具在本地切片**,`total` 也是本地过滤后的条数——不要拿它跟平台分页语义混用;②不给任何服务端条件时平台直接回空(同函数早退),而 mobile **不是**服务端条件:只给 mobile 时工具改走部门树扫描(`source:\"department-tree-scan\"`,最多 200 个部门、超过会 `truncated`),所以尽量同时给 name 或 departmentIds;③在职 = status 为 ENABLE,平台条件面不筛在职,工具在本地再过滤一次(缺 status 的行计入 statusUnknown);内置 admin 账号同样剔除;④`ids` 是 `userIds` 的别名,匹配的仍是业务 userId 列,记录 id(recId)请用 org.user.info;⑤`source` 说明本次候选集从哪来,排查「为什么没查到」先看它。",
|
|
447
|
+
inputSchema: {
|
|
448
|
+
environment: environmentArg,
|
|
449
|
+
name: z.string().trim().min(1).optional().describe("姓名关键字(服务端 name like)"),
|
|
450
|
+
mobile: z.string().trim().min(1).optional().describe("手机号(本地数字后缀匹配;只给 mobile 时走部门树扫描)"),
|
|
451
|
+
departmentIds: z.array(z.string().min(1)).min(1).optional().describe("部门 id 列表(服务端 deptIds,含这些部门的成员)"),
|
|
452
|
+
userIds: z.array(z.string().min(1)).min(1).optional().describe("业务 userId 列表(服务端 userIds 匹配 userId 列;不是记录 id)"),
|
|
453
|
+
ids: z.array(z.string().min(1)).min(1).optional().describe("`userIds` 的别名(匹配业务 userId 列,不是记录 id)"),
|
|
454
|
+
roleIds: z.array(z.string().min(1)).min(1).optional().describe("角色 id 列表(服务端 roleIds)"),
|
|
455
|
+
projection: z.enum(["simple", "detailed"]).optional().describe("simple(默认,size 可到 100)或 detailed(每行多一次部门读,size 上限 50)"),
|
|
456
|
+
page: z.number().int().min(0).optional().describe("页号,从 0 开始,默认 0(本地切片)"),
|
|
457
|
+
size: z.number().int().min(1).max(100).optional().describe("每页条数,默认 50,上限 100;projection=detailed 时上限 50(每行多一次执行成本)"),
|
|
458
|
+
capability: capabilityArg,
|
|
459
|
+
},
|
|
460
|
+
inputRefine: (value) => {
|
|
461
|
+
const projection = value.projection;
|
|
462
|
+
const size = value.size;
|
|
463
|
+
if (projection === "detailed" && typeof size === "number" && size > DETAILED_MAX_PAGE_SIZE) {
|
|
464
|
+
return {
|
|
465
|
+
path: "size",
|
|
466
|
+
message: `projection=\"detailed\" 时 size 上限为 ${DETAILED_MAX_PAGE_SIZE}(每行多一次 organization/user/departments 读,N+1,成本随 size 线性增长);当前 size=${size}。改用 projection=\"simple\",或把 size 降到 ${DETAILED_MAX_PAGE_SIZE} 以内。`,
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
return undefined;
|
|
470
|
+
},
|
|
471
|
+
handler: async (ctx, args, self) => {
|
|
472
|
+
const name = args.name;
|
|
473
|
+
const mobile = args.mobile;
|
|
474
|
+
const departmentIds = args.departmentIds;
|
|
475
|
+
const userIds = args.userIds ?? args.ids;
|
|
476
|
+
const roleIds = args.roleIds;
|
|
477
|
+
const projection = args.projection ?? "simple";
|
|
478
|
+
const page = args.page ?? 0;
|
|
479
|
+
const size = args.size ?? 50;
|
|
480
|
+
if (name === undefined && mobile === undefined && departmentIds === undefined && userIds === undefined && roleIds === undefined) {
|
|
481
|
+
throw new ToolInputError({
|
|
482
|
+
status: "error",
|
|
483
|
+
blocker: "tool-input-invalid",
|
|
484
|
+
tool: "org.users",
|
|
485
|
+
message: "至少给一个条件:name / mobile / departmentIds / userIds(或 ids)/ roleIds。平台在没有任何服务端条件时直接回空,空查不是「列全部在职用户」",
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
return governedRead(ctx, self, args, async (session) => {
|
|
489
|
+
const hasServerFilter = name !== undefined || departmentIds !== undefined || userIds !== undefined || roleIds !== undefined;
|
|
490
|
+
let candidates = [];
|
|
491
|
+
let source;
|
|
492
|
+
let departmentsScanned;
|
|
493
|
+
let scanTruncated = false;
|
|
494
|
+
if (hasServerFilter) {
|
|
495
|
+
const data = (await wireCall(session, {
|
|
496
|
+
path: ORG_QUERY_ENDPOINTS.userSearchCondition,
|
|
497
|
+
method: "POST",
|
|
498
|
+
body: {
|
|
499
|
+
page: 0,
|
|
500
|
+
size: USER_SCAN_LIMIT,
|
|
501
|
+
...(name === undefined ? {} : { userName: name }),
|
|
502
|
+
...(departmentIds === undefined ? {} : { deptIds: departmentIds }),
|
|
503
|
+
...(userIds === undefined ? {} : { userIds }),
|
|
504
|
+
...(roleIds === undefined ? {} : { roleIds }),
|
|
505
|
+
},
|
|
506
|
+
})).data;
|
|
507
|
+
candidates = asRows(data);
|
|
508
|
+
source = "user/searchCondiction";
|
|
509
|
+
scanTruncated = candidates.length >= USER_SCAN_LIMIT;
|
|
510
|
+
}
|
|
511
|
+
else {
|
|
512
|
+
const walked = await scanUsersByDepartmentTree(session);
|
|
513
|
+
candidates = walked.users;
|
|
514
|
+
departmentsScanned = walked.departmentsScanned;
|
|
515
|
+
scanTruncated = walked.truncated;
|
|
516
|
+
source = "department-tree-scan";
|
|
517
|
+
}
|
|
518
|
+
let statusUnknown = 0;
|
|
519
|
+
const inService = candidates.filter((row) => {
|
|
520
|
+
if (textOf(row, ["userId"]) === "admin")
|
|
521
|
+
return false;
|
|
522
|
+
const status = statusName(row.status);
|
|
523
|
+
if (status === null) {
|
|
524
|
+
statusUnknown += 1;
|
|
525
|
+
return true;
|
|
526
|
+
}
|
|
527
|
+
return status === "ENABLE";
|
|
528
|
+
});
|
|
529
|
+
const filtered = mobile === undefined ? inService : inService.filter((row) => mobileMatches(row.mobile, mobile));
|
|
530
|
+
const sliced = slicePage(filtered, page, size);
|
|
531
|
+
const rows = sliced.items.map(projectUser);
|
|
532
|
+
if (projection === "detailed") {
|
|
533
|
+
for (const row of rows) {
|
|
534
|
+
const id = row.id === null || row.id === undefined ? null : String(row.id);
|
|
535
|
+
const candidate = id === null ? undefined : candidates.find((entry) => textOf(entry, ["id"]) === id);
|
|
536
|
+
row.email = candidate === undefined ? null : textOf(candidate, ["email"]);
|
|
537
|
+
row.employeeNo = candidate === undefined ? null : textOf(candidate, ["employeeNo"]);
|
|
538
|
+
row.createdTime = candidate?.createdTime ?? null;
|
|
539
|
+
row.modifiedTime = candidate?.modifiedTime ?? null;
|
|
540
|
+
if (id === null) {
|
|
541
|
+
row.departments = null;
|
|
542
|
+
continue;
|
|
543
|
+
}
|
|
544
|
+
try {
|
|
545
|
+
const data = (await wireCall(session, {
|
|
546
|
+
path: ORG_QUERY_ENDPOINTS.userDepartments,
|
|
547
|
+
query: { id },
|
|
548
|
+
})).data;
|
|
549
|
+
row.departments = asRows(data).map((department) => ({
|
|
550
|
+
deptId: textOf(department, ["deptId", "id"]),
|
|
551
|
+
deptName: textOf(department, ["deptName", "name"]),
|
|
552
|
+
isMain: department.isMain ?? null,
|
|
553
|
+
}));
|
|
554
|
+
}
|
|
555
|
+
catch {
|
|
556
|
+
row.departments = null;
|
|
557
|
+
row.departmentsError = "部门读取失败(用户行保留,departments=null)";
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
return {
|
|
562
|
+
projection,
|
|
563
|
+
filters: {
|
|
564
|
+
name: name ?? null,
|
|
565
|
+
mobile: mobile ?? null,
|
|
566
|
+
departmentIds: departmentIds ?? null,
|
|
567
|
+
userIds: userIds ?? null,
|
|
568
|
+
roleIds: roleIds ?? null,
|
|
569
|
+
},
|
|
570
|
+
source,
|
|
571
|
+
page,
|
|
572
|
+
size,
|
|
573
|
+
offset: sliced.offset,
|
|
574
|
+
total: filtered.length,
|
|
575
|
+
returned: rows.length,
|
|
576
|
+
hasMore: sliced.hasMore,
|
|
577
|
+
nextPage: sliced.hasMore ? page + 1 : null,
|
|
578
|
+
inServiceOnly: true,
|
|
579
|
+
mobileMatch: mobile === undefined ? null : "digits-suffix",
|
|
580
|
+
scanned: candidates.length,
|
|
581
|
+
...(departmentsScanned === undefined ? {} : { departmentsScanned }),
|
|
582
|
+
...(statusUnknown > 0 ? { statusUnknown } : {}),
|
|
583
|
+
...(scanTruncated
|
|
584
|
+
? {
|
|
585
|
+
truncated: true,
|
|
586
|
+
note: `候选集达到扫描上限(${hasServerFilter ? `${USER_SCAN_LIMIT} 行` : `${MOBILE_SCAN_DEPARTMENT_LIMIT} 个部门`}),命中可能不完整;请加 name/departmentIds 收窄。`,
|
|
587
|
+
}
|
|
588
|
+
: {}),
|
|
589
|
+
users: rows,
|
|
590
|
+
};
|
|
591
|
+
}, {
|
|
592
|
+
compact: (result) => {
|
|
593
|
+
const users = Array.isArray(result.users) ? result.users : [];
|
|
594
|
+
const kept = users.slice(0, 20).map((row) => {
|
|
595
|
+
const departments = Array.isArray(row.departments) ? row.departments : null;
|
|
596
|
+
if (departments === null || departments.length <= 10)
|
|
597
|
+
return row;
|
|
598
|
+
return { ...row, departments: departments.slice(0, 10), departmentsInlineOmitted: departments.length - 10 };
|
|
599
|
+
});
|
|
600
|
+
return {
|
|
601
|
+
...result,
|
|
602
|
+
users: kept,
|
|
603
|
+
...(users.length > 20 ? { usersInlineOmitted: users.length - 20, usersCount: users.length } : {}),
|
|
604
|
+
};
|
|
605
|
+
},
|
|
606
|
+
});
|
|
607
|
+
},
|
|
608
|
+
}));
|
|
609
|
+
}
|
|
610
|
+
//# sourceMappingURL=org-query.js.map
|