@sparkelf/dsh-dataops-managed 0.3.29 → 0.3.30
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/lib/client.js +136 -136
- package/lib/index.js +622 -147
- package/lib/types/resource-management-api.d.ts +35 -0
- package/lib/types/tool-manager.d.ts +16 -0
- package/lib/types/tool-scope.d.ts +43 -0
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -4,8 +4,8 @@ import { credentialRef } from "@deepseek-ai/dsh-credentials";
|
|
|
4
4
|
import * as McpClient from "@sparkelf/dsh-plugin-mcp-credentials";
|
|
5
5
|
import { BUNDLED_SKILL_RANK } from "@deepseek-ai/dsh-skill";
|
|
6
6
|
import z from "@deepseek-ai/schemastery";
|
|
7
|
-
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
8
7
|
import { basename, dirname, extname, join } from "node:path";
|
|
8
|
+
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
9
9
|
import { randomUUID } from "node:crypto";
|
|
10
10
|
//#region src/auth-state.ts
|
|
11
11
|
let state = "unknown";
|
|
@@ -876,7 +876,32 @@ const AI_TOOL_ACCESS = {
|
|
|
876
876
|
dataops_collect_plan: { permission: "data:resource:manage" },
|
|
877
877
|
dataops_collect_execute: { permission: "data:resource:manage" },
|
|
878
878
|
dataops_collect_resources: { permission: "data:resource:read" },
|
|
879
|
-
dataops_open_resource_tab: {}
|
|
879
|
+
dataops_open_resource_tab: {},
|
|
880
|
+
dataops_resource_pending_list: {
|
|
881
|
+
capability: "resource.manage",
|
|
882
|
+
adminOnly: true,
|
|
883
|
+
defaultEnabled: true
|
|
884
|
+
},
|
|
885
|
+
dataops_resource_grant_list: {
|
|
886
|
+
capability: "resource.manage",
|
|
887
|
+
adminOnly: true,
|
|
888
|
+
defaultEnabled: true
|
|
889
|
+
},
|
|
890
|
+
dataops_resource_grant_batch: {
|
|
891
|
+
capability: "resource.manage",
|
|
892
|
+
adminOnly: true,
|
|
893
|
+
defaultEnabled: true
|
|
894
|
+
},
|
|
895
|
+
dataops_resource_approval_decide: {
|
|
896
|
+
capability: "resource.manage",
|
|
897
|
+
adminOnly: true,
|
|
898
|
+
defaultEnabled: true
|
|
899
|
+
},
|
|
900
|
+
dataops_resource_grant_revoke: {
|
|
901
|
+
capability: "resource.manage",
|
|
902
|
+
adminOnly: true,
|
|
903
|
+
defaultEnabled: true
|
|
904
|
+
}
|
|
880
905
|
};
|
|
881
906
|
/** Every managed tool name, in the order the manager considers them. */
|
|
882
907
|
const AI_TOOL_NAMES = Object.keys(AI_TOOL_ACCESS);
|
|
@@ -938,9 +963,16 @@ const hasPermission = (scope, key) => {
|
|
|
938
963
|
* Every check is disclosure metadata, not an execution gate: a visible tool still lets DataOps
|
|
939
964
|
* answer with 403 when the underlying grant changed between a scope sync and the call.
|
|
940
965
|
*/
|
|
966
|
+
function isToolCapabilityEnabled(name, state) {
|
|
967
|
+
const policy = AI_TOOL_ACCESS[name];
|
|
968
|
+
if (policy.capability === void 0) return true;
|
|
969
|
+
if (!state.enabled) return false;
|
|
970
|
+
return state.capabilities[policy.capability] ?? policy.defaultEnabled ?? false;
|
|
971
|
+
}
|
|
941
972
|
function isToolVisible(name, scope) {
|
|
942
973
|
const policy = AI_TOOL_ACCESS[name];
|
|
943
974
|
if (policy.superAdminOnly === true && !isSuperAdmin(scope)) return false;
|
|
975
|
+
if (policy.adminOnly === true && !isAdmin(scope)) return false;
|
|
944
976
|
if (policy.reviewer === true && !canReview(scope)) return false;
|
|
945
977
|
if (policy.permission !== void 0 && !hasPermission(scope, policy.permission)) return false;
|
|
946
978
|
return true;
|
|
@@ -984,6 +1016,141 @@ function normalizeScope(body) {
|
|
|
984
1016
|
};
|
|
985
1017
|
}
|
|
986
1018
|
//#endregion
|
|
1019
|
+
//#region src/connector-state.ts
|
|
1020
|
+
/**
|
|
1021
|
+
* The DataOps connector's workspace-visible state and skill-center catalogue.
|
|
1022
|
+
*
|
|
1023
|
+
* The skill center is skill-first, with connectors as its fourth tab. DataOps contributes one
|
|
1024
|
+
* connector whose capability rows are derived from the same scope that drives tool disclosure;
|
|
1025
|
+
* a capability the account cannot use is omitted rather than rendered as a disabled placeholder.
|
|
1026
|
+
*
|
|
1027
|
+
* This module owns only state and presentation. It never authorizes a tool call or a DataOps
|
|
1028
|
+
* request: the backend remains the final boundary.
|
|
1029
|
+
*
|
|
1030
|
+
* @module connector-state
|
|
1031
|
+
*/
|
|
1032
|
+
/** Same-origin route the skill center's connector tab reads and writes. */
|
|
1033
|
+
const CONNECTORS_PATH = "/api/dsh-skill-center/connectors";
|
|
1034
|
+
/** Connector id shown in the skill center. */
|
|
1035
|
+
const DATAOPS_CONNECTOR_ID = "dataops-managed";
|
|
1036
|
+
/** Resolve the persisted state file without depending on a global DSH path. */
|
|
1037
|
+
const CONNECTOR_STATE_PATH = join(process.env.DSH_HOME?.trim() || "/workspace/.dataops/dsh", "dataops-managed", "connector-state.json");
|
|
1038
|
+
/**
|
|
1039
|
+
* Capability definitions before permission filtering.
|
|
1040
|
+
*
|
|
1041
|
+
* The set follows `dataops/docs/dataops-connector-development-spec.md` §2.3's credential list.
|
|
1042
|
+
* `approval.push` was renamed to `resource.manage` when the capability grew from approval push
|
|
1043
|
+
* into resource approval and authorization. Skill plaza and review queue are governed by
|
|
1044
|
+
* `skill:*` permissions and stay out of the connector's capability switches.
|
|
1045
|
+
*/
|
|
1046
|
+
const CAPABILITIES = [
|
|
1047
|
+
{
|
|
1048
|
+
id: "data.query",
|
|
1049
|
+
name: "数据查询",
|
|
1050
|
+
description: "通过 DataOps 数据查询能力回答业务问题",
|
|
1051
|
+
defaultEnabled: true,
|
|
1052
|
+
visible: () => true,
|
|
1053
|
+
allowed: () => true
|
|
1054
|
+
},
|
|
1055
|
+
{
|
|
1056
|
+
id: "resource.manage",
|
|
1057
|
+
name: "资源管理",
|
|
1058
|
+
description: "在对话中批量审批资源申请,给用户或角色授予资源访问权限",
|
|
1059
|
+
defaultEnabled: true,
|
|
1060
|
+
visible: (scope) => isAdmin(scope),
|
|
1061
|
+
allowed: (scope) => isAdmin(scope)
|
|
1062
|
+
},
|
|
1063
|
+
{
|
|
1064
|
+
id: "admin.manage",
|
|
1065
|
+
name: "万相中台管理",
|
|
1066
|
+
description: "登录万相中台后台执行管理动作",
|
|
1067
|
+
defaultEnabled: true,
|
|
1068
|
+
visible: (scope) => isAdmin(scope),
|
|
1069
|
+
allowed: (scope) => isAdmin(scope)
|
|
1070
|
+
}
|
|
1071
|
+
];
|
|
1072
|
+
/**
|
|
1073
|
+
* Persisted ids that moved to a new capability id.
|
|
1074
|
+
*
|
|
1075
|
+
* `approval.push` shipped before the capability grew from approval push into resource
|
|
1076
|
+
* approval and authorization. Its stored toggle carries over to `resource.manage`; when both
|
|
1077
|
+
* keys are present the current id wins.
|
|
1078
|
+
*/
|
|
1079
|
+
const CAPABILITY_ALIASES = { "approval.push": "resource.manage" };
|
|
1080
|
+
/** The ids the catalogue accepts; any other persisted key is a legacy value. */
|
|
1081
|
+
const CAPABILITY_IDS = CAPABILITIES.map((capability) => capability.id);
|
|
1082
|
+
/**
|
|
1083
|
+
* Keep only boolean values for known capability ids.
|
|
1084
|
+
*
|
|
1085
|
+
* A state file written before the catalogue changed may still hold retired ids. Those keys are
|
|
1086
|
+
* ignored rather than treated as a corrupt file; a missing new key takes the capability's
|
|
1087
|
+
* `defaultEnabled`.
|
|
1088
|
+
* @param value - the `capabilities` member of a parsed state file.
|
|
1089
|
+
* @returns the readable toggles, keyed by current capability id.
|
|
1090
|
+
*/
|
|
1091
|
+
function migrateCapabilities(value) {
|
|
1092
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return {};
|
|
1093
|
+
const stored = value;
|
|
1094
|
+
const capabilities = {};
|
|
1095
|
+
for (const id of CAPABILITY_IDS) if (typeof stored[id] === "boolean") capabilities[id] = stored[id];
|
|
1096
|
+
for (const [legacyId, id] of Object.entries(CAPABILITY_ALIASES)) if (capabilities[id] === void 0 && typeof stored[legacyId] === "boolean") capabilities[id] = stored[legacyId];
|
|
1097
|
+
return capabilities;
|
|
1098
|
+
}
|
|
1099
|
+
/** Read state; a missing or corrupt file is a safe disabled state. */
|
|
1100
|
+
async function readConnectorState(path = CONNECTOR_STATE_PATH) {
|
|
1101
|
+
try {
|
|
1102
|
+
const payload = JSON.parse(await readFile(path, "utf8"));
|
|
1103
|
+
if (typeof payload !== "object" || payload === null || Array.isArray(payload)) return {
|
|
1104
|
+
enabled: false,
|
|
1105
|
+
capabilities: {}
|
|
1106
|
+
};
|
|
1107
|
+
const record = payload;
|
|
1108
|
+
return {
|
|
1109
|
+
enabled: record.enabled === true,
|
|
1110
|
+
capabilities: migrateCapabilities(record.capabilities)
|
|
1111
|
+
};
|
|
1112
|
+
} catch {
|
|
1113
|
+
return {
|
|
1114
|
+
enabled: false,
|
|
1115
|
+
capabilities: {}
|
|
1116
|
+
};
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
/** Persist state atomically. */
|
|
1120
|
+
async function writeConnectorState(state, path = CONNECTOR_STATE_PATH) {
|
|
1121
|
+
await mkdir(dirname(path), { recursive: true });
|
|
1122
|
+
const temporary = path + ".tmp-" + process.pid.toString();
|
|
1123
|
+
const payload = {
|
|
1124
|
+
enabled: state.enabled,
|
|
1125
|
+
capabilities: migrateCapabilities(state.capabilities)
|
|
1126
|
+
};
|
|
1127
|
+
await writeFile(temporary, JSON.stringify(payload, null, 2), "utf8");
|
|
1128
|
+
await rename(temporary, path);
|
|
1129
|
+
}
|
|
1130
|
+
/** Build the connector row for one current scope. */
|
|
1131
|
+
function buildConnectorView(scope, state) {
|
|
1132
|
+
const capabilities = scope === void 0 ? [] : CAPABILITIES.filter((capability) => capability.visible(scope) && capability.allowed(scope)).map((capability) => ({
|
|
1133
|
+
id: capability.id,
|
|
1134
|
+
name: capability.name,
|
|
1135
|
+
description: capability.description,
|
|
1136
|
+
enabled: state.capabilities[capability.id] ?? capability.defaultEnabled,
|
|
1137
|
+
permitted: true
|
|
1138
|
+
}));
|
|
1139
|
+
return {
|
|
1140
|
+
id: DATAOPS_CONNECTOR_ID,
|
|
1141
|
+
name: "万相AI中台连接器",
|
|
1142
|
+
vendor: "DataOps",
|
|
1143
|
+
description: "把万相AI中台的技能、审核和数据能力接入当前工作区。",
|
|
1144
|
+
enabled: state.enabled,
|
|
1145
|
+
connected: scope !== void 0,
|
|
1146
|
+
capabilities
|
|
1147
|
+
};
|
|
1148
|
+
}
|
|
1149
|
+
/** Whether one capability id is in the fixed catalogue. */
|
|
1150
|
+
function hasCapability(id) {
|
|
1151
|
+
return CAPABILITIES.some((capability) => capability.id === id);
|
|
1152
|
+
}
|
|
1153
|
+
//#endregion
|
|
987
1154
|
//#region src/collect-api.ts
|
|
988
1155
|
/**
|
|
989
1156
|
* DataOps spreadsheet-import transport for the collection skill.
|
|
@@ -1011,7 +1178,7 @@ const statusError = (result, operation) => {
|
|
|
1011
1178
|
if (result.status === 403) return /* @__PURE__ */ new Error("Forbidden (403): " + (reason || "the current role may not import resources"));
|
|
1012
1179
|
return /* @__PURE__ */ new Error(`${operation} failed: HTTP ${result.status}${reason ? ": " + reason : ""}`);
|
|
1013
1180
|
};
|
|
1014
|
-
const unwrap = (result, operation) => {
|
|
1181
|
+
const unwrap$1 = (result, operation) => {
|
|
1015
1182
|
if (result.status < 200 || result.status >= 300) throw statusError(result, operation);
|
|
1016
1183
|
return result.body;
|
|
1017
1184
|
};
|
|
@@ -1045,11 +1212,11 @@ var CollectApi = class {
|
|
|
1045
1212
|
method: "POST",
|
|
1046
1213
|
body: form
|
|
1047
1214
|
});
|
|
1048
|
-
return unwrap(result, "spreadsheet preview");
|
|
1215
|
+
return unwrap$1(result, "spreadsheet preview");
|
|
1049
1216
|
}
|
|
1050
1217
|
async dataSources() {
|
|
1051
1218
|
const result = await this.reviewApi.request("/api/resources/spreadsheet-import/data-sources", { method: "GET" });
|
|
1052
|
-
const body = unwrap(result, "import data sources");
|
|
1219
|
+
const body = unwrap$1(result, "import data sources");
|
|
1053
1220
|
return Array.isArray(body.dataSources) ? body.dataSources : [];
|
|
1054
1221
|
}
|
|
1055
1222
|
async plan(payload) {
|
|
@@ -1057,18 +1224,18 @@ var CollectApi = class {
|
|
|
1057
1224
|
method: "POST",
|
|
1058
1225
|
body: JSON.stringify(payload)
|
|
1059
1226
|
});
|
|
1060
|
-
return unwrap(result, "spreadsheet plan");
|
|
1227
|
+
return unwrap$1(result, "spreadsheet plan");
|
|
1061
1228
|
}
|
|
1062
1229
|
async execute(payload) {
|
|
1063
1230
|
const result = await this.reviewApi.request("/api/resources/spreadsheet-import/execute", {
|
|
1064
1231
|
method: "POST",
|
|
1065
1232
|
body: JSON.stringify(payload)
|
|
1066
1233
|
});
|
|
1067
|
-
return unwrap(result, "spreadsheet execute");
|
|
1234
|
+
return unwrap$1(result, "spreadsheet execute");
|
|
1068
1235
|
}
|
|
1069
1236
|
async listResources(query = "", limit = 20) {
|
|
1070
1237
|
const result = await this.reviewApi.request("/api/resources", { method: "GET" });
|
|
1071
|
-
const body = unwrap(result, "resource list");
|
|
1238
|
+
const body = unwrap$1(result, "resource list");
|
|
1072
1239
|
const rows = Array.isArray(body.resources) ? body.resources : [];
|
|
1073
1240
|
const keyword = query.trim().toLowerCase();
|
|
1074
1241
|
return rows.filter((row) => row.resourceType === void 0 || row.resourceType === "TABLE").filter((row) => {
|
|
@@ -1082,6 +1249,72 @@ var CollectApi = class {
|
|
|
1082
1249
|
}
|
|
1083
1250
|
};
|
|
1084
1251
|
//#endregion
|
|
1252
|
+
//#region src/resource-management-api.ts
|
|
1253
|
+
const reasonOf = (body) => {
|
|
1254
|
+
if (typeof body !== "object" || body === null || Array.isArray(body)) return "";
|
|
1255
|
+
const record = body;
|
|
1256
|
+
if (typeof record.error === "string") return record.error;
|
|
1257
|
+
if (typeof record.message === "string") return record.message;
|
|
1258
|
+
const details = record.details;
|
|
1259
|
+
return Array.isArray(details?.errors) ? details.errors.filter((v) => typeof v === "string").join("; ") : "";
|
|
1260
|
+
};
|
|
1261
|
+
const unwrap = (result, operation) => {
|
|
1262
|
+
if (result.status < 200 || result.status >= 300) {
|
|
1263
|
+
const reason = reasonOf(result.body);
|
|
1264
|
+
throw new Error(operation + " failed: HTTP " + result.status.toString() + (reason ? ": " + reason : ""));
|
|
1265
|
+
}
|
|
1266
|
+
return result.body;
|
|
1267
|
+
};
|
|
1268
|
+
/** Thin typed wrapper over the existing DataOps REST endpoints. */
|
|
1269
|
+
var ResourceManagementApi = class {
|
|
1270
|
+
reviewApi;
|
|
1271
|
+
constructor(reviewApi) {
|
|
1272
|
+
this.reviewApi = reviewApi;
|
|
1273
|
+
}
|
|
1274
|
+
async call(path, init = {}) {
|
|
1275
|
+
return unwrap(await this.reviewApi.request(path, init), path);
|
|
1276
|
+
}
|
|
1277
|
+
async listPendingTasks(query = "", limit = 20) {
|
|
1278
|
+
const params = new URLSearchParams({
|
|
1279
|
+
status: "pending",
|
|
1280
|
+
page: "1"
|
|
1281
|
+
});
|
|
1282
|
+
const keyword = query.trim();
|
|
1283
|
+
if (keyword) params.set("keyword", keyword);
|
|
1284
|
+
const rows = (await this.call("/api/tasks?" + params.toString(), { method: "GET" })).tasks;
|
|
1285
|
+
return Array.isArray(rows) ? rows.filter((row) => typeof row === "object" && row !== null).slice(0, Math.max(1, Math.min(100, limit))) : [];
|
|
1286
|
+
}
|
|
1287
|
+
async listGrants(resourceId) {
|
|
1288
|
+
const rows = (await this.call("/api/resources/" + resourceId.toString() + "/access-grants", { method: "GET" })).grants;
|
|
1289
|
+
return Array.isArray(rows) ? rows.filter((row) => typeof row === "object" && row !== null) : [];
|
|
1290
|
+
}
|
|
1291
|
+
async listApplications() {
|
|
1292
|
+
const rows = (await this.call("/api/tasks/asset-access-applications", { method: "GET" })).applications;
|
|
1293
|
+
return Array.isArray(rows) ? rows.filter((row) => typeof row === "object" && row !== null) : [];
|
|
1294
|
+
}
|
|
1295
|
+
async replaceGrants(resourceId, grants) {
|
|
1296
|
+
return this.call("/api/resources/" + resourceId.toString() + "/access-grants", {
|
|
1297
|
+
method: "PUT",
|
|
1298
|
+
body: JSON.stringify({ grants })
|
|
1299
|
+
});
|
|
1300
|
+
}
|
|
1301
|
+
async getTask(taskId) {
|
|
1302
|
+
const task = (await this.call("/api/tasks/" + taskId.toString(), { method: "GET" })).task;
|
|
1303
|
+
if (typeof task !== "object" || task === null) throw new Error("task detail is unavailable");
|
|
1304
|
+
return task;
|
|
1305
|
+
}
|
|
1306
|
+
async decideTask(decision) {
|
|
1307
|
+
const action = decision.decision === "approve" ? "complete" : "reject";
|
|
1308
|
+
return this.call("/api/tasks/" + decision.taskId.toString() + "/user-tasks/" + encodeURIComponent(decision.userTaskId) + "/" + action, {
|
|
1309
|
+
method: "POST",
|
|
1310
|
+
body: JSON.stringify({
|
|
1311
|
+
expectedVersion: decision.expectedVersion ?? 0,
|
|
1312
|
+
comment: decision.comment?.trim() || (decision.decision === "approve" ? "同意" : "驳回")
|
|
1313
|
+
})
|
|
1314
|
+
});
|
|
1315
|
+
}
|
|
1316
|
+
};
|
|
1317
|
+
//#endregion
|
|
1085
1318
|
//#region src/api-docs.ts
|
|
1086
1319
|
/** Raised when the upstream body is not an OpenAPI document this module can search. */
|
|
1087
1320
|
var ApiDocsUnavailableError = class extends Error {};
|
|
@@ -1341,6 +1574,7 @@ const forbidden = (message) => /* @__PURE__ */ new Error("Forbidden (403): " + m
|
|
|
1341
1574
|
*/
|
|
1342
1575
|
function buildAiManagementTools(ctx, reviewApi, readScope, collectHub) {
|
|
1343
1576
|
const collectApi = new CollectApi(reviewApi);
|
|
1577
|
+
const resourceManagementApi = new ResourceManagementApi(reviewApi);
|
|
1344
1578
|
const approve = async (tool, exec, reason) => {
|
|
1345
1579
|
if (exec.agent === void 0) throw new Error(tool + " requires an initiating agent");
|
|
1346
1580
|
const outcome = await ctx.approval.request({
|
|
@@ -2149,6 +2383,342 @@ function buildAiManagementTools(ctx, reviewApi, readScope, collectHub) {
|
|
|
2149
2383
|
};
|
|
2150
2384
|
}
|
|
2151
2385
|
})
|
|
2386
|
+
},
|
|
2387
|
+
{
|
|
2388
|
+
name: "dataops_resource_pending_list",
|
|
2389
|
+
definition: defineTool({
|
|
2390
|
+
name: "dataops_resource_pending_list",
|
|
2391
|
+
description: "List pending DataOps approval tasks and their active user tasks. Administrator only; the resource.manage connector capability must be on. Read-only.",
|
|
2392
|
+
parameters: {
|
|
2393
|
+
query: {
|
|
2394
|
+
type: "string",
|
|
2395
|
+
description: "Optional keyword over task title/code/requester."
|
|
2396
|
+
},
|
|
2397
|
+
limit: {
|
|
2398
|
+
type: "number",
|
|
2399
|
+
description: "Maximum tasks (default 20, max 100)."
|
|
2400
|
+
}
|
|
2401
|
+
},
|
|
2402
|
+
output: {
|
|
2403
|
+
schema: OPEN_OBJECT_SCHEMA,
|
|
2404
|
+
render: renderJson
|
|
2405
|
+
},
|
|
2406
|
+
execute: async (args) => {
|
|
2407
|
+
const limit = Math.max(1, Math.min(100, Math.round(args.limit ?? 20)));
|
|
2408
|
+
const tasks = await resourceManagementApi.listPendingTasks(args.query ?? "", limit);
|
|
2409
|
+
return {
|
|
2410
|
+
tasks: tasks.map((task) => ({
|
|
2411
|
+
id: task.id,
|
|
2412
|
+
code: task.code,
|
|
2413
|
+
title: task.title,
|
|
2414
|
+
status: task.status,
|
|
2415
|
+
requester: task.requester,
|
|
2416
|
+
requesterUserId: task.requesterUserId,
|
|
2417
|
+
workflow: task.workflow,
|
|
2418
|
+
userTasks: task.userTasks,
|
|
2419
|
+
updatedAt: task.updatedAt
|
|
2420
|
+
})),
|
|
2421
|
+
count: tasks.length
|
|
2422
|
+
};
|
|
2423
|
+
}
|
|
2424
|
+
})
|
|
2425
|
+
},
|
|
2426
|
+
{
|
|
2427
|
+
name: "dataops_resource_grant_list",
|
|
2428
|
+
definition: defineTool({
|
|
2429
|
+
name: "dataops_resource_grant_list",
|
|
2430
|
+
description: "List the current access grants of one DataOps resource. Administrator only; resource.manage capability must be on. Read-only.",
|
|
2431
|
+
parameters: { resource_id: {
|
|
2432
|
+
type: "number",
|
|
2433
|
+
required: true,
|
|
2434
|
+
description: "DataOps resource id."
|
|
2435
|
+
} },
|
|
2436
|
+
output: {
|
|
2437
|
+
schema: OPEN_OBJECT_SCHEMA,
|
|
2438
|
+
render: renderJson
|
|
2439
|
+
},
|
|
2440
|
+
execute: async (args) => {
|
|
2441
|
+
if (!Number.isSafeInteger(args.resource_id) || args.resource_id <= 0) throw new Error("resource_id must be a positive integer");
|
|
2442
|
+
const grants = await resourceManagementApi.listGrants(args.resource_id);
|
|
2443
|
+
return {
|
|
2444
|
+
resourceId: args.resource_id,
|
|
2445
|
+
grants
|
|
2446
|
+
};
|
|
2447
|
+
}
|
|
2448
|
+
})
|
|
2449
|
+
},
|
|
2450
|
+
{
|
|
2451
|
+
name: "dataops_resource_grant_batch",
|
|
2452
|
+
definition: defineTool({
|
|
2453
|
+
name: "dataops_resource_grant_batch",
|
|
2454
|
+
description: "Batch grant READ/MANAGE access on one DataOps resource to users, roles or departments. Administrator only; resource.manage capability must be on. The existing grants are preserved; the operation is audited by DataOps.",
|
|
2455
|
+
parameters: {
|
|
2456
|
+
resource_id: {
|
|
2457
|
+
type: "number",
|
|
2458
|
+
required: true,
|
|
2459
|
+
description: "DataOps resource id."
|
|
2460
|
+
},
|
|
2461
|
+
application_id: {
|
|
2462
|
+
type: "number",
|
|
2463
|
+
description: "Application id used for the grants. Defaults to an existing grant application or the first selectable application."
|
|
2464
|
+
},
|
|
2465
|
+
grants: {
|
|
2466
|
+
type: "array",
|
|
2467
|
+
required: true,
|
|
2468
|
+
description: "Targets to grant.",
|
|
2469
|
+
items: {
|
|
2470
|
+
type: "object",
|
|
2471
|
+
properties: {
|
|
2472
|
+
targetType: {
|
|
2473
|
+
type: "string",
|
|
2474
|
+
description: "USER, ROLE, DEPARTMENT or APPLICATION."
|
|
2475
|
+
},
|
|
2476
|
+
targetId: {
|
|
2477
|
+
type: "string",
|
|
2478
|
+
description: "User id, role code, department code or application id."
|
|
2479
|
+
},
|
|
2480
|
+
permission: {
|
|
2481
|
+
type: "string",
|
|
2482
|
+
description: "READ or MANAGE. Defaults to READ."
|
|
2483
|
+
},
|
|
2484
|
+
grantType: {
|
|
2485
|
+
type: "string",
|
|
2486
|
+
description: "REGULAR or TEMPORARY. Defaults to REGULAR."
|
|
2487
|
+
},
|
|
2488
|
+
applicationId: {
|
|
2489
|
+
type: "number",
|
|
2490
|
+
description: "Optional per-grant application id override."
|
|
2491
|
+
}
|
|
2492
|
+
},
|
|
2493
|
+
additionalProperties: false
|
|
2494
|
+
}
|
|
2495
|
+
}
|
|
2496
|
+
},
|
|
2497
|
+
output: {
|
|
2498
|
+
schema: OPEN_OBJECT_SCHEMA,
|
|
2499
|
+
render: renderJson
|
|
2500
|
+
},
|
|
2501
|
+
execute: async (args, exec) => {
|
|
2502
|
+
if (!Number.isSafeInteger(args.resource_id) || args.resource_id <= 0) throw new Error("resource_id must be a positive integer");
|
|
2503
|
+
if (!Array.isArray(args.grants) || args.grants.length === 0) throw new Error("grants must be a non-empty array");
|
|
2504
|
+
const existing = await resourceManagementApi.listGrants(args.resource_id);
|
|
2505
|
+
let appId = args.application_id;
|
|
2506
|
+
if (appId === void 0) appId = existing.map((g) => Number(g.applicationId)).find((id) => Number.isSafeInteger(id) && id > 0);
|
|
2507
|
+
if (appId === void 0) appId = (await resourceManagementApi.listApplications()).map((app) => Number(app.id)).find((id) => Number.isSafeInteger(id) && id > 0);
|
|
2508
|
+
if (appId === void 0) throw new Error("no application is available for access grants");
|
|
2509
|
+
const merged = /* @__PURE__ */ new Map();
|
|
2510
|
+
for (const grant of existing) {
|
|
2511
|
+
const targetType = String(grant.targetType ?? "");
|
|
2512
|
+
const targetId = String(grant.targetId ?? "");
|
|
2513
|
+
if (!targetType || !targetId) continue;
|
|
2514
|
+
const applicationId = Number(grant.applicationId) || appId;
|
|
2515
|
+
merged.set(targetType + ":" + targetId + ":" + applicationId, {
|
|
2516
|
+
targetType,
|
|
2517
|
+
targetId,
|
|
2518
|
+
permission: String(grant.permission ?? "READ"),
|
|
2519
|
+
grantType: String(grant.grantType ?? "REGULAR"),
|
|
2520
|
+
applicationId
|
|
2521
|
+
});
|
|
2522
|
+
}
|
|
2523
|
+
for (const grant of args.grants) {
|
|
2524
|
+
const targetType = String(grant.targetType ?? "").trim().toUpperCase();
|
|
2525
|
+
const targetId = String(grant.targetId ?? "").trim();
|
|
2526
|
+
if (![
|
|
2527
|
+
"USER",
|
|
2528
|
+
"ROLE",
|
|
2529
|
+
"DEPARTMENT",
|
|
2530
|
+
"APPLICATION"
|
|
2531
|
+
].includes(targetType) || !targetId) throw new Error("each grant needs targetType and targetId");
|
|
2532
|
+
const permission = String(grant.permission ?? "READ").trim().toUpperCase();
|
|
2533
|
+
if (![
|
|
2534
|
+
"READ",
|
|
2535
|
+
"WRITE",
|
|
2536
|
+
"MANAGE"
|
|
2537
|
+
].includes(permission)) throw new Error("permission must be READ, WRITE or MANAGE");
|
|
2538
|
+
const applicationId = grant.applicationId ?? appId;
|
|
2539
|
+
merged.set(targetType + ":" + targetId + ":" + applicationId, {
|
|
2540
|
+
targetType,
|
|
2541
|
+
targetId,
|
|
2542
|
+
permission,
|
|
2543
|
+
grantType: String(grant.grantType ?? "REGULAR").trim().toUpperCase(),
|
|
2544
|
+
applicationId
|
|
2545
|
+
});
|
|
2546
|
+
}
|
|
2547
|
+
const grants = [...merged.values()];
|
|
2548
|
+
await resourceManagementApi.replaceGrants(args.resource_id, grants);
|
|
2549
|
+
audit("dataops_resource_grant_batch", exec, {
|
|
2550
|
+
resourceId: args.resource_id,
|
|
2551
|
+
count: grants.length,
|
|
2552
|
+
applicationId: appId
|
|
2553
|
+
});
|
|
2554
|
+
return {
|
|
2555
|
+
resourceId: args.resource_id,
|
|
2556
|
+
applicationId: appId,
|
|
2557
|
+
grants,
|
|
2558
|
+
count: grants.length
|
|
2559
|
+
};
|
|
2560
|
+
}
|
|
2561
|
+
})
|
|
2562
|
+
},
|
|
2563
|
+
{
|
|
2564
|
+
name: "dataops_resource_approval_decide",
|
|
2565
|
+
definition: defineTool({
|
|
2566
|
+
name: "dataops_resource_approval_decide",
|
|
2567
|
+
description: "Approve or reject pending DataOps user tasks in batch. Administrator only; resource.manage capability must be on. Each decision needs task_id and user_task_id; expected_version is read from the task when omitted.",
|
|
2568
|
+
parameters: { decisions: {
|
|
2569
|
+
type: "array",
|
|
2570
|
+
required: true,
|
|
2571
|
+
description: "Approval decisions.",
|
|
2572
|
+
items: {
|
|
2573
|
+
type: "object",
|
|
2574
|
+
properties: {
|
|
2575
|
+
task_id: {
|
|
2576
|
+
type: "number",
|
|
2577
|
+
description: "DataOps task id."
|
|
2578
|
+
},
|
|
2579
|
+
user_task_id: {
|
|
2580
|
+
type: "string",
|
|
2581
|
+
description: "BPMN user task UUID."
|
|
2582
|
+
},
|
|
2583
|
+
decision: {
|
|
2584
|
+
type: "string",
|
|
2585
|
+
description: "approve or reject."
|
|
2586
|
+
},
|
|
2587
|
+
comment: {
|
|
2588
|
+
type: "string",
|
|
2589
|
+
description: "Decision comment."
|
|
2590
|
+
},
|
|
2591
|
+
expected_version: {
|
|
2592
|
+
type: "number",
|
|
2593
|
+
description: "Current taskVersion; read from task detail when omitted."
|
|
2594
|
+
}
|
|
2595
|
+
},
|
|
2596
|
+
additionalProperties: false
|
|
2597
|
+
}
|
|
2598
|
+
} },
|
|
2599
|
+
output: {
|
|
2600
|
+
schema: OPEN_OBJECT_SCHEMA,
|
|
2601
|
+
render: renderJson
|
|
2602
|
+
},
|
|
2603
|
+
execute: async (args, exec) => {
|
|
2604
|
+
if (!Array.isArray(args.decisions) || args.decisions.length === 0) throw new Error("decisions must be a non-empty array");
|
|
2605
|
+
const results = [];
|
|
2606
|
+
for (const decision of args.decisions) {
|
|
2607
|
+
const taskId = Number(decision.task_id);
|
|
2608
|
+
const userTaskId = String(decision.user_task_id ?? "");
|
|
2609
|
+
const action = String(decision.decision ?? "").toLowerCase();
|
|
2610
|
+
if (!Number.isSafeInteger(taskId) || taskId <= 0 || !userTaskId || action !== "approve" && action !== "reject") {
|
|
2611
|
+
results.push({
|
|
2612
|
+
taskId,
|
|
2613
|
+
userTaskId,
|
|
2614
|
+
ok: false,
|
|
2615
|
+
error: "invalid decision"
|
|
2616
|
+
});
|
|
2617
|
+
continue;
|
|
2618
|
+
}
|
|
2619
|
+
try {
|
|
2620
|
+
let expectedVersion = decision.expected_version;
|
|
2621
|
+
if (expectedVersion === void 0) {
|
|
2622
|
+
const task = await resourceManagementApi.getTask(taskId);
|
|
2623
|
+
const target = (Array.isArray(task.userTasks) ? task.userTasks : []).find((item) => item.id === userTaskId);
|
|
2624
|
+
expectedVersion = typeof target?.taskVersion === "number" ? target.taskVersion : 0;
|
|
2625
|
+
}
|
|
2626
|
+
await resourceManagementApi.decideTask({
|
|
2627
|
+
taskId,
|
|
2628
|
+
userTaskId,
|
|
2629
|
+
decision: action,
|
|
2630
|
+
comment: decision.comment,
|
|
2631
|
+
expectedVersion
|
|
2632
|
+
});
|
|
2633
|
+
results.push({
|
|
2634
|
+
taskId,
|
|
2635
|
+
userTaskId,
|
|
2636
|
+
decision: action,
|
|
2637
|
+
ok: true
|
|
2638
|
+
});
|
|
2639
|
+
} catch (error) {
|
|
2640
|
+
results.push({
|
|
2641
|
+
taskId,
|
|
2642
|
+
userTaskId,
|
|
2643
|
+
decision: action,
|
|
2644
|
+
ok: false,
|
|
2645
|
+
error: error instanceof Error ? error.message : String(error)
|
|
2646
|
+
});
|
|
2647
|
+
}
|
|
2648
|
+
}
|
|
2649
|
+
audit("dataops_resource_approval_decide", exec, {
|
|
2650
|
+
count: results.length,
|
|
2651
|
+
ok: results.filter((item) => item.ok === true).length
|
|
2652
|
+
});
|
|
2653
|
+
return { results };
|
|
2654
|
+
}
|
|
2655
|
+
})
|
|
2656
|
+
},
|
|
2657
|
+
{
|
|
2658
|
+
name: "dataops_resource_grant_revoke",
|
|
2659
|
+
definition: defineTool({
|
|
2660
|
+
name: "dataops_resource_grant_revoke",
|
|
2661
|
+
description: "Batch revoke access grants from one DataOps resource by target type and target id. Administrator only; resource.manage capability must be on. DataOps audits the resulting grant replacement.",
|
|
2662
|
+
parameters: {
|
|
2663
|
+
resource_id: {
|
|
2664
|
+
type: "number",
|
|
2665
|
+
required: true,
|
|
2666
|
+
description: "DataOps resource id."
|
|
2667
|
+
},
|
|
2668
|
+
targets: {
|
|
2669
|
+
type: "array",
|
|
2670
|
+
required: true,
|
|
2671
|
+
description: "Targets to revoke.",
|
|
2672
|
+
items: {
|
|
2673
|
+
type: "object",
|
|
2674
|
+
properties: {
|
|
2675
|
+
targetType: {
|
|
2676
|
+
type: "string",
|
|
2677
|
+
description: "USER, ROLE, DEPARTMENT or APPLICATION."
|
|
2678
|
+
},
|
|
2679
|
+
targetId: {
|
|
2680
|
+
type: "string",
|
|
2681
|
+
description: "Target id/code."
|
|
2682
|
+
}
|
|
2683
|
+
},
|
|
2684
|
+
additionalProperties: false
|
|
2685
|
+
}
|
|
2686
|
+
}
|
|
2687
|
+
},
|
|
2688
|
+
output: {
|
|
2689
|
+
schema: OPEN_OBJECT_SCHEMA,
|
|
2690
|
+
render: renderJson
|
|
2691
|
+
},
|
|
2692
|
+
execute: async (args, exec) => {
|
|
2693
|
+
if (!Number.isSafeInteger(args.resource_id) || args.resource_id <= 0) throw new Error("resource_id must be a positive integer");
|
|
2694
|
+
if (!Array.isArray(args.targets) || args.targets.length === 0) throw new Error("targets must be a non-empty array");
|
|
2695
|
+
const existing = await resourceManagementApi.listGrants(args.resource_id);
|
|
2696
|
+
const keys = new Set(args.targets.map((target) => String(target.targetType).toUpperCase() + ":" + String(target.targetId)));
|
|
2697
|
+
let appId = existing.map((g) => Number(g.applicationId)).find((id) => Number.isSafeInteger(id) && id > 0);
|
|
2698
|
+
if (appId === void 0) appId = (await resourceManagementApi.listApplications()).map((app) => Number(app.id)).find((id) => Number.isSafeInteger(id) && id > 0);
|
|
2699
|
+
if (appId === void 0) throw new Error("no application is available for access grants");
|
|
2700
|
+
const remaining = existing.filter((grant) => !keys.has(String(grant.targetType).toUpperCase() + ":" + String(grant.targetId))).map((grant) => ({
|
|
2701
|
+
targetType: String(grant.targetType),
|
|
2702
|
+
targetId: String(grant.targetId),
|
|
2703
|
+
permission: String(grant.permission ?? "READ"),
|
|
2704
|
+
grantType: String(grant.grantType ?? "REGULAR"),
|
|
2705
|
+
applicationId: Number(grant.applicationId) || appId
|
|
2706
|
+
}));
|
|
2707
|
+
const removed = existing.length - remaining.length;
|
|
2708
|
+
if (removed === 0) throw new Error("no matching grant to revoke");
|
|
2709
|
+
await resourceManagementApi.replaceGrants(args.resource_id, remaining);
|
|
2710
|
+
audit("dataops_resource_grant_revoke", exec, {
|
|
2711
|
+
resourceId: args.resource_id,
|
|
2712
|
+
removed
|
|
2713
|
+
});
|
|
2714
|
+
return {
|
|
2715
|
+
resourceId: args.resource_id,
|
|
2716
|
+
removed,
|
|
2717
|
+
remaining,
|
|
2718
|
+
count: remaining.length
|
|
2719
|
+
};
|
|
2720
|
+
}
|
|
2721
|
+
})
|
|
2152
2722
|
}
|
|
2153
2723
|
];
|
|
2154
2724
|
}
|
|
@@ -2170,6 +2740,7 @@ var AiToolManager = class {
|
|
|
2170
2740
|
unsubscribeExpiry;
|
|
2171
2741
|
disposeViewer;
|
|
2172
2742
|
scope;
|
|
2743
|
+
connectorState;
|
|
2173
2744
|
revision = 0;
|
|
2174
2745
|
inflight;
|
|
2175
2746
|
disposed = false;
|
|
@@ -2254,6 +2825,37 @@ var AiToolManager = class {
|
|
|
2254
2825
|
this.disposeViewer = void 0;
|
|
2255
2826
|
this.scope = void 0;
|
|
2256
2827
|
}
|
|
2828
|
+
/**
|
|
2829
|
+
* Read the connector state that gates capability-owned tools.
|
|
2830
|
+
*
|
|
2831
|
+
* A missing or unreadable state file is not a permission decision: it means the connector has
|
|
2832
|
+
* never been enabled in this workspace, so capability-gated tools stay unregistered.
|
|
2833
|
+
* @returns The persisted connector state, or a disabled placeholder when it cannot be read.
|
|
2834
|
+
*/
|
|
2835
|
+
async readConnectorState() {
|
|
2836
|
+
try {
|
|
2837
|
+
return await readConnectorState();
|
|
2838
|
+
} catch (error) {
|
|
2839
|
+
this.ctx.logger.warn("dataops-managed: connector state could not be read for tool disclosure");
|
|
2840
|
+
this.ctx.logger.warn(error);
|
|
2841
|
+
return {
|
|
2842
|
+
enabled: false,
|
|
2843
|
+
capabilities: {}
|
|
2844
|
+
};
|
|
2845
|
+
}
|
|
2846
|
+
}
|
|
2847
|
+
/**
|
|
2848
|
+
* Re-reconcile after the connector's capability switches changed.
|
|
2849
|
+
*
|
|
2850
|
+
* The skill center writes the connector state; calling this keeps the registered tool set in
|
|
2851
|
+
* step with it without waiting for the next credential or role sync.
|
|
2852
|
+
*/
|
|
2853
|
+
async refreshCapabilities() {
|
|
2854
|
+
if (this.disposed) return;
|
|
2855
|
+
const connectorState = await this.readConnectorState();
|
|
2856
|
+
if (this.disposed) return;
|
|
2857
|
+
this.applyScope(this.scope, connectorState);
|
|
2858
|
+
}
|
|
2257
2859
|
async loadScope(revision) {
|
|
2258
2860
|
let result;
|
|
2259
2861
|
try {
|
|
@@ -2264,22 +2866,29 @@ var AiToolManager = class {
|
|
|
2264
2866
|
return;
|
|
2265
2867
|
}
|
|
2266
2868
|
if (this.disposed || revision !== this.revision) return;
|
|
2869
|
+
const connectorState = await this.readConnectorState();
|
|
2870
|
+
if (this.disposed || revision !== this.revision) return;
|
|
2267
2871
|
if (result.status === 200) {
|
|
2268
|
-
this.applyScope(normalizeScope(result.body));
|
|
2872
|
+
this.applyScope(normalizeScope(result.body), connectorState);
|
|
2269
2873
|
return;
|
|
2270
2874
|
}
|
|
2271
2875
|
if (result.status === 401 || result.status === 403 || result.status === 404 || result.status === 503) {
|
|
2272
|
-
this.applyScope(void 0);
|
|
2876
|
+
this.applyScope(void 0, connectorState);
|
|
2273
2877
|
return;
|
|
2274
2878
|
}
|
|
2275
2879
|
this.ctx.logger.warn("dataops-managed: tool disclosure scope read failed with HTTP " + result.status.toString());
|
|
2276
2880
|
}
|
|
2277
2881
|
/** Store the scope and make the registry match it. */
|
|
2278
|
-
applyScope(scope) {
|
|
2882
|
+
applyScope(scope, connectorState) {
|
|
2279
2883
|
this.scope = scope;
|
|
2884
|
+
if (connectorState !== void 0) this.connectorState = connectorState;
|
|
2280
2885
|
if (this.disposed) return;
|
|
2281
2886
|
this.applyViewer(scope);
|
|
2282
|
-
const
|
|
2887
|
+
const capabilities = this.connectorState ?? {
|
|
2888
|
+
enabled: false,
|
|
2889
|
+
capabilities: {}
|
|
2890
|
+
};
|
|
2891
|
+
const desired = new Set(scope === void 0 ? [] : AI_TOOL_NAMES.filter((name) => isToolVisible(name, scope) && isToolCapabilityEnabled(name, capabilities)));
|
|
2283
2892
|
for (const [name, registration] of this.registrations) {
|
|
2284
2893
|
if (desired.has(name)) continue;
|
|
2285
2894
|
this.disposeRegistration(name, registration);
|
|
@@ -2446,141 +3055,6 @@ const resolveResourceTabConfig = (request, configuredWebBaseUrl) => {
|
|
|
2446
3055
|
};
|
|
2447
3056
|
};
|
|
2448
3057
|
//#endregion
|
|
2449
|
-
//#region src/connector-state.ts
|
|
2450
|
-
/**
|
|
2451
|
-
* The DataOps connector's workspace-visible state and skill-center catalogue.
|
|
2452
|
-
*
|
|
2453
|
-
* The skill center is skill-first, with connectors as its fourth tab. DataOps contributes one
|
|
2454
|
-
* connector whose capability rows are derived from the same scope that drives tool disclosure;
|
|
2455
|
-
* a capability the account cannot use is omitted rather than rendered as a disabled placeholder.
|
|
2456
|
-
*
|
|
2457
|
-
* This module owns only state and presentation. It never authorizes a tool call or a DataOps
|
|
2458
|
-
* request: the backend remains the final boundary.
|
|
2459
|
-
*
|
|
2460
|
-
* @module connector-state
|
|
2461
|
-
*/
|
|
2462
|
-
/** Same-origin route the skill center's connector tab reads and writes. */
|
|
2463
|
-
const CONNECTORS_PATH = "/api/dsh-skill-center/connectors";
|
|
2464
|
-
/** Connector id shown in the skill center. */
|
|
2465
|
-
const DATAOPS_CONNECTOR_ID = "dataops-managed";
|
|
2466
|
-
/** Resolve the persisted state file without depending on a global DSH path. */
|
|
2467
|
-
const CONNECTOR_STATE_PATH = join(process.env.DSH_HOME?.trim() || "/workspace/.dataops/dsh", "dataops-managed", "connector-state.json");
|
|
2468
|
-
/**
|
|
2469
|
-
* Capability definitions before permission filtering.
|
|
2470
|
-
*
|
|
2471
|
-
* The set follows `dataops/docs/dataops-connector-development-spec.md` §2.3's credential list.
|
|
2472
|
-
* `approval.push` was renamed to `resource.manage` when the capability grew from approval push
|
|
2473
|
-
* into resource approval and authorization. Skill plaza and review queue are governed by
|
|
2474
|
-
* `skill:*` permissions and stay out of the connector's capability switches.
|
|
2475
|
-
*/
|
|
2476
|
-
const CAPABILITIES = [
|
|
2477
|
-
{
|
|
2478
|
-
id: "data.query",
|
|
2479
|
-
name: "数据查询",
|
|
2480
|
-
description: "通过 DataOps 数据查询能力回答业务问题",
|
|
2481
|
-
defaultEnabled: true,
|
|
2482
|
-
visible: () => true,
|
|
2483
|
-
allowed: () => true
|
|
2484
|
-
},
|
|
2485
|
-
{
|
|
2486
|
-
id: "resource.manage",
|
|
2487
|
-
name: "资源管理",
|
|
2488
|
-
description: "在对话中批量审批资源申请,给用户或角色授予资源访问权限",
|
|
2489
|
-
defaultEnabled: true,
|
|
2490
|
-
visible: (scope) => isAdmin(scope),
|
|
2491
|
-
allowed: (scope) => isAdmin(scope)
|
|
2492
|
-
},
|
|
2493
|
-
{
|
|
2494
|
-
id: "admin.manage",
|
|
2495
|
-
name: "万相中台管理",
|
|
2496
|
-
description: "登录万相中台后台执行管理动作",
|
|
2497
|
-
defaultEnabled: true,
|
|
2498
|
-
visible: (scope) => isAdmin(scope),
|
|
2499
|
-
allowed: (scope) => isAdmin(scope)
|
|
2500
|
-
}
|
|
2501
|
-
];
|
|
2502
|
-
/**
|
|
2503
|
-
* Persisted ids that moved to a new capability id.
|
|
2504
|
-
*
|
|
2505
|
-
* `approval.push` shipped before the capability grew from approval push into resource
|
|
2506
|
-
* approval and authorization. Its stored toggle carries over to `resource.manage`; when both
|
|
2507
|
-
* keys are present the current id wins.
|
|
2508
|
-
*/
|
|
2509
|
-
const CAPABILITY_ALIASES = { "approval.push": "resource.manage" };
|
|
2510
|
-
/** The ids the catalogue accepts; any other persisted key is a legacy value. */
|
|
2511
|
-
const CAPABILITY_IDS = CAPABILITIES.map((capability) => capability.id);
|
|
2512
|
-
/**
|
|
2513
|
-
* Keep only boolean values for known capability ids.
|
|
2514
|
-
*
|
|
2515
|
-
* A state file written before the catalogue changed may still hold retired ids. Those keys are
|
|
2516
|
-
* ignored rather than treated as a corrupt file; a missing new key takes the capability's
|
|
2517
|
-
* `defaultEnabled`.
|
|
2518
|
-
* @param value - the `capabilities` member of a parsed state file.
|
|
2519
|
-
* @returns the readable toggles, keyed by current capability id.
|
|
2520
|
-
*/
|
|
2521
|
-
function migrateCapabilities(value) {
|
|
2522
|
-
if (typeof value !== "object" || value === null || Array.isArray(value)) return {};
|
|
2523
|
-
const stored = value;
|
|
2524
|
-
const capabilities = {};
|
|
2525
|
-
for (const id of CAPABILITY_IDS) if (typeof stored[id] === "boolean") capabilities[id] = stored[id];
|
|
2526
|
-
for (const [legacyId, id] of Object.entries(CAPABILITY_ALIASES)) if (capabilities[id] === void 0 && typeof stored[legacyId] === "boolean") capabilities[id] = stored[legacyId];
|
|
2527
|
-
return capabilities;
|
|
2528
|
-
}
|
|
2529
|
-
/** Read state; a missing or corrupt file is a safe disabled state. */
|
|
2530
|
-
async function readConnectorState(path = CONNECTOR_STATE_PATH) {
|
|
2531
|
-
try {
|
|
2532
|
-
const payload = JSON.parse(await readFile(path, "utf8"));
|
|
2533
|
-
if (typeof payload !== "object" || payload === null || Array.isArray(payload)) return {
|
|
2534
|
-
enabled: false,
|
|
2535
|
-
capabilities: {}
|
|
2536
|
-
};
|
|
2537
|
-
const record = payload;
|
|
2538
|
-
return {
|
|
2539
|
-
enabled: record.enabled === true,
|
|
2540
|
-
capabilities: migrateCapabilities(record.capabilities)
|
|
2541
|
-
};
|
|
2542
|
-
} catch {
|
|
2543
|
-
return {
|
|
2544
|
-
enabled: false,
|
|
2545
|
-
capabilities: {}
|
|
2546
|
-
};
|
|
2547
|
-
}
|
|
2548
|
-
}
|
|
2549
|
-
/** Persist state atomically. */
|
|
2550
|
-
async function writeConnectorState(state, path = CONNECTOR_STATE_PATH) {
|
|
2551
|
-
await mkdir(dirname(path), { recursive: true });
|
|
2552
|
-
const temporary = path + ".tmp-" + process.pid.toString();
|
|
2553
|
-
const payload = {
|
|
2554
|
-
enabled: state.enabled,
|
|
2555
|
-
capabilities: migrateCapabilities(state.capabilities)
|
|
2556
|
-
};
|
|
2557
|
-
await writeFile(temporary, JSON.stringify(payload, null, 2), "utf8");
|
|
2558
|
-
await rename(temporary, path);
|
|
2559
|
-
}
|
|
2560
|
-
/** Build the connector row for one current scope. */
|
|
2561
|
-
function buildConnectorView(scope, state) {
|
|
2562
|
-
const capabilities = scope === void 0 ? [] : CAPABILITIES.filter((capability) => capability.visible(scope) && capability.allowed(scope)).map((capability) => ({
|
|
2563
|
-
id: capability.id,
|
|
2564
|
-
name: capability.name,
|
|
2565
|
-
description: capability.description,
|
|
2566
|
-
enabled: state.capabilities[capability.id] ?? capability.defaultEnabled,
|
|
2567
|
-
permitted: true
|
|
2568
|
-
}));
|
|
2569
|
-
return {
|
|
2570
|
-
id: DATAOPS_CONNECTOR_ID,
|
|
2571
|
-
name: "万相AI中台连接器",
|
|
2572
|
-
vendor: "DataOps",
|
|
2573
|
-
description: "把万相AI中台的技能、审核和数据能力接入当前工作区。",
|
|
2574
|
-
enabled: state.enabled,
|
|
2575
|
-
connected: scope !== void 0,
|
|
2576
|
-
capabilities
|
|
2577
|
-
};
|
|
2578
|
-
}
|
|
2579
|
-
/** Whether one capability id is in the fixed catalogue. */
|
|
2580
|
-
function hasCapability(id) {
|
|
2581
|
-
return CAPABILITIES.some((capability) => capability.id === id);
|
|
2582
|
-
}
|
|
2583
|
-
//#endregion
|
|
2584
3058
|
//#region src/workspace-limits.ts
|
|
2585
3059
|
/** Narrow a value to a plain object. */
|
|
2586
3060
|
function asRecord(value) {
|
|
@@ -4172,6 +4646,7 @@ async function apply(ctx, config) {
|
|
|
4172
4646
|
state.enabled = body.enabled;
|
|
4173
4647
|
}
|
|
4174
4648
|
await writeConnectorState(state);
|
|
4649
|
+
await aiTools.refreshCapabilities();
|
|
4175
4650
|
writeJson(response, 200, { connectors: [buildConnectorView(aiTools.currentScope(), state)] });
|
|
4176
4651
|
return;
|
|
4177
4652
|
}
|