@donglegeyu/company-ui 1.2.43 → 1.2.44

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.
Files changed (67) hide show
  1. package/dist/cjs/ai-assistant/AiActionGuard/AiActionConfirmModal.d.ts +10 -0
  2. package/dist/cjs/ai-assistant/AiActionGuard/AiActionConfirmModal.js +87 -0
  3. package/dist/cjs/ai-assistant/AiActionGuard/AiActionGuard.d.ts +11 -0
  4. package/dist/cjs/ai-assistant/AiActionGuard/AiActionGuard.js +467 -0
  5. package/dist/cjs/ai-assistant/AiActionGuard/index.d.ts +5 -0
  6. package/dist/cjs/ai-assistant/AiActionGuard/index.js +51 -0
  7. package/dist/cjs/ai-assistant/AiActionGuard/policy.d.ts +23 -0
  8. package/dist/cjs/ai-assistant/AiActionGuard/policy.js +146 -0
  9. package/dist/cjs/ai-assistant/AiActionGuard/types.d.ts +130 -0
  10. package/dist/cjs/ai-assistant/AiActionGuard/types.js +17 -0
  11. package/dist/cjs/ai-assistant/aiDraft.d.ts +60 -0
  12. package/dist/cjs/ai-assistant/aiDraft.js +17 -0
  13. package/dist/cjs/ai-assistant/index.d.ts +2 -0
  14. package/dist/cjs/ai-assistant/index.js +6 -1
  15. package/dist/cjs/shared-components/BaseInfoForm/index.d.ts +7 -2
  16. package/dist/cjs/shared-components/BaseInfoForm/index.js +146 -47
  17. package/dist/cjs/sidebar-config.js +15 -1
  18. package/dist/cjs/template-components/AiAssistantDock/index.d.ts +7 -1
  19. package/dist/cjs/template-components/AiAssistantDock/index.js +9 -6
  20. package/dist/cjs/template-components/DrawerDetailTemplate/index.d.ts +42 -0
  21. package/dist/cjs/template-components/DrawerDetailTemplate/index.js +120 -0
  22. package/dist/cjs/template-components/DrawerDetailTemplate/index.scss +56 -0
  23. package/dist/cjs/template-components/DrawerFormTemplate/index.d.ts +35 -0
  24. package/dist/cjs/template-components/DrawerFormTemplate/index.js +132 -0
  25. package/dist/cjs/template-components/DrawerFormTemplate/index.scss +3 -0
  26. package/dist/cjs/template-components/FilterForm/index.d.ts +6 -1
  27. package/dist/cjs/template-components/FilterForm/index.js +175 -54
  28. package/dist/cjs/template-components/FormPageTemplate/index.d.ts +10 -1
  29. package/dist/cjs/template-components/FormPageTemplate/index.js +109 -1
  30. package/dist/cjs/template-components/SmartListTemplate/index.d.ts +6 -1
  31. package/dist/cjs/template-components/SmartListTemplate/index.js +6 -2
  32. package/dist/cjs/template-components/index.d.ts +4 -0
  33. package/dist/cjs/template-components/index.js +6 -0
  34. package/dist/es/ai-assistant/AiActionGuard/AiActionConfirmModal.d.ts +10 -0
  35. package/dist/es/ai-assistant/AiActionGuard/AiActionConfirmModal.js +58 -0
  36. package/dist/es/ai-assistant/AiActionGuard/AiActionGuard.d.ts +11 -0
  37. package/dist/es/ai-assistant/AiActionGuard/AiActionGuard.js +676 -0
  38. package/dist/es/ai-assistant/AiActionGuard/index.d.ts +5 -0
  39. package/dist/es/ai-assistant/AiActionGuard/index.js +4 -0
  40. package/dist/es/ai-assistant/AiActionGuard/policy.d.ts +23 -0
  41. package/dist/es/ai-assistant/AiActionGuard/policy.js +97 -0
  42. package/dist/es/ai-assistant/AiActionGuard/types.d.ts +130 -0
  43. package/dist/es/ai-assistant/AiActionGuard/types.js +1 -0
  44. package/dist/es/ai-assistant/aiDraft.d.ts +60 -0
  45. package/dist/es/ai-assistant/aiDraft.js +1 -0
  46. package/dist/es/ai-assistant/index.d.ts +2 -0
  47. package/dist/es/ai-assistant/index.js +3 -1
  48. package/dist/es/shared-components/BaseInfoForm/index.d.ts +7 -2
  49. package/dist/es/shared-components/BaseInfoForm/index.js +128 -24
  50. package/dist/es/sidebar-config.js +9 -0
  51. package/dist/es/template-components/AiAssistantDock/index.d.ts +7 -1
  52. package/dist/es/template-components/AiAssistantDock/index.js +13 -1
  53. package/dist/es/template-components/DrawerDetailTemplate/index.d.ts +42 -0
  54. package/dist/es/template-components/DrawerDetailTemplate/index.js +112 -0
  55. package/dist/es/template-components/DrawerDetailTemplate/index.scss +56 -0
  56. package/dist/es/template-components/DrawerFormTemplate/index.d.ts +35 -0
  57. package/dist/es/template-components/DrawerFormTemplate/index.js +149 -0
  58. package/dist/es/template-components/DrawerFormTemplate/index.scss +3 -0
  59. package/dist/es/template-components/FilterForm/index.d.ts +6 -1
  60. package/dist/es/template-components/FilterForm/index.js +165 -7
  61. package/dist/es/template-components/FormPageTemplate/index.d.ts +10 -1
  62. package/dist/es/template-components/FormPageTemplate/index.js +103 -2
  63. package/dist/es/template-components/SmartListTemplate/index.d.ts +6 -1
  64. package/dist/es/template-components/SmartListTemplate/index.js +6 -2
  65. package/dist/es/template-components/index.d.ts +4 -0
  66. package/dist/es/template-components/index.js +2 -0
  67. package/package.json +1 -1
@@ -0,0 +1,10 @@
1
+ /// <reference types="react" />
2
+ import type { AiActionConfirmModalProps } from "./types";
3
+ /**
4
+ * AI 动作确认弹层:基于 CompanyModal。
5
+ *
6
+ * - 标题 / 内容 / 确认取消文案均可配(缺省取 request 上的文案)
7
+ * - 键盘可操作(antd Modal 内置 Esc 关闭与 Tab 焦点循环),关闭后焦点回到触发点
8
+ * - 遮罩不关闭,必须显式确认或取消,避免误触放行
9
+ */
10
+ export declare function AiActionConfirmModal({ open, request, title, content, okText, cancelText, onOk, onCancel, className, style, }: AiActionConfirmModalProps): import("react").JSX.Element;
@@ -0,0 +1,87 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/ai-assistant/AiActionGuard/AiActionConfirmModal.tsx
20
+ var AiActionConfirmModal_exports = {};
21
+ __export(AiActionConfirmModal_exports, {
22
+ AiActionConfirmModal: () => AiActionConfirmModal
23
+ });
24
+ module.exports = __toCommonJS(AiActionConfirmModal_exports);
25
+ var import_antd = require("antd");
26
+ var import_CompanyModal = require("../../basic-components/CompanyModal");
27
+ var import_jsx_runtime = require("react/jsx-runtime");
28
+ function AiActionConfirmModal({
29
+ open,
30
+ request,
31
+ title,
32
+ content,
33
+ okText,
34
+ cancelText,
35
+ onOk,
36
+ onCancel,
37
+ className,
38
+ style
39
+ }) {
40
+ const { token } = import_antd.theme.useToken();
41
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
42
+ import_CompanyModal.CompanyModal,
43
+ {
44
+ open,
45
+ centered: true,
46
+ maskClosable: false,
47
+ destroyOnHidden: true,
48
+ focusTriggerAfterClose: true,
49
+ title: title ?? (request == null ? void 0 : request.title) ?? "确认执行 AI 动作?",
50
+ okText: okText ?? (request == null ? void 0 : request.okText) ?? "确认",
51
+ cancelText: cancelText ?? (request == null ? void 0 : request.cancelText) ?? "取消",
52
+ onOk,
53
+ onCancel,
54
+ className,
55
+ style,
56
+ children: [
57
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: { color: token.colorText, lineHeight: token.lineHeight }, children: content ?? (request == null ? void 0 : request.content) ?? "该动作需经你确认后才会继续。" }),
58
+ request ? /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
59
+ "div",
60
+ {
61
+ style: {
62
+ marginTop: token.marginSM,
63
+ padding: `${token.paddingXS}px ${token.paddingSM}px`,
64
+ borderRadius: token.borderRadius,
65
+ background: token.colorFillTertiary,
66
+ color: token.colorTextSecondary,
67
+ fontSize: token.fontSizeSM,
68
+ lineHeight: token.lineHeightSM
69
+ },
70
+ children: [
71
+ "动作:",
72
+ request.label,
73
+ "(",
74
+ request.action,
75
+ ") · 数据域:",
76
+ request.domain
77
+ ]
78
+ }
79
+ ) : null
80
+ ]
81
+ }
82
+ );
83
+ }
84
+ // Annotate the CommonJS export names for ESM import in node:
85
+ 0 && (module.exports = {
86
+ AiActionConfirmModal
87
+ });
@@ -0,0 +1,11 @@
1
+ /// <reference types="react" />
2
+ import type { AiActionGuardProps, UseAiActionValue } from "./types";
3
+ /**
4
+ * AI 动作执行闸门(上下文 Provider):
5
+ *
6
+ * 策略判定 → 用户确认 → 实时权限 → 写类幂等 → 调用方执行 → 审计。
7
+ * 组件不发起任何业务请求,执行逻辑由调用方通过 run 的第三个参数注入。
8
+ */
9
+ export declare function AiActionGuard({ policy, domain, checkPermission, onAudit, onConfirm, userId, sessionId, confirmText, children, }: AiActionGuardProps): import("react").JSX.Element;
10
+ /** 读取动作闸门能力(需在 AiActionGuard 内使用) */
11
+ export declare function useAiAction(): UseAiActionValue;
@@ -0,0 +1,467 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/ai-assistant/AiActionGuard/AiActionGuard.tsx
20
+ var AiActionGuard_exports = {};
21
+ __export(AiActionGuard_exports, {
22
+ AiActionGuard: () => AiActionGuard,
23
+ useAiAction: () => useAiAction
24
+ });
25
+ module.exports = __toCommonJS(AiActionGuard_exports);
26
+ var import_react = require("react");
27
+ var import_AiActionConfirmModal = require("./AiActionConfirmModal");
28
+ var import_policy = require("./policy");
29
+ var import_jsx_runtime = require("react/jsx-runtime");
30
+ var AiActionContext = (0, import_react.createContext)(void 0);
31
+ var sessionSeed = 0;
32
+ function createSessionId() {
33
+ sessionSeed += 1;
34
+ return `ai-action-${Date.now().toString(36)}-${sessionSeed}`;
35
+ }
36
+ function AiActionGuard({
37
+ policy,
38
+ domain,
39
+ checkPermission,
40
+ onAudit,
41
+ onConfirm,
42
+ userId,
43
+ sessionId,
44
+ confirmText,
45
+ children
46
+ }) {
47
+ const policyRef = (0, import_react.useRef)(policy);
48
+ policyRef.current = policy;
49
+ const domainRef = (0, import_react.useRef)(domain);
50
+ domainRef.current = domain;
51
+ const checkPermissionRef = (0, import_react.useRef)(checkPermission);
52
+ checkPermissionRef.current = checkPermission;
53
+ const onAuditRef = (0, import_react.useRef)(onAudit);
54
+ onAuditRef.current = onAudit;
55
+ const onConfirmRef = (0, import_react.useRef)(onConfirm);
56
+ onConfirmRef.current = onConfirm;
57
+ const userIdRef = (0, import_react.useRef)(userId);
58
+ userIdRef.current = userId;
59
+ const confirmTextRef = (0, import_react.useRef)(confirmText);
60
+ confirmTextRef.current = confirmText;
61
+ const [generatedSessionId] = (0, import_react.useState)(createSessionId);
62
+ const sessionIdRef = (0, import_react.useRef)(sessionId ?? generatedSessionId);
63
+ sessionIdRef.current = sessionId ?? generatedSessionId;
64
+ const navigateCountRef = (0, import_react.useRef)(0);
65
+ const usedKeysRef = (0, import_react.useRef)(/* @__PURE__ */ new Set());
66
+ const undoRegistryRef = (0, import_react.useRef)(/* @__PURE__ */ new Map());
67
+ const undoSeedRef = (0, import_react.useRef)(0);
68
+ const [activeConfirm, setActiveConfirm] = (0, import_react.useState)(null);
69
+ const activeConfirmRef = (0, import_react.useRef)(null);
70
+ const confirmQueueRef = (0, import_react.useRef)([]);
71
+ (0, import_react.useEffect)(() => {
72
+ return () => {
73
+ var _a;
74
+ (_a = activeConfirmRef.current) == null ? void 0 : _a.resolve(false);
75
+ activeConfirmRef.current = null;
76
+ confirmQueueRef.current.forEach((entry) => entry.resolve(false));
77
+ confirmQueueRef.current = [];
78
+ };
79
+ }, []);
80
+ const enqueueConfirm = (0, import_react.useCallback)((request) => {
81
+ return new Promise((resolve) => {
82
+ const entry = { request, resolve };
83
+ if (activeConfirmRef.current) {
84
+ confirmQueueRef.current.push(entry);
85
+ return;
86
+ }
87
+ activeConfirmRef.current = entry;
88
+ setActiveConfirm(entry);
89
+ });
90
+ }, []);
91
+ const settleConfirm = (0, import_react.useCallback)((approved) => {
92
+ const current = activeConfirmRef.current;
93
+ activeConfirmRef.current = null;
94
+ current == null ? void 0 : current.resolve(approved);
95
+ const next = confirmQueueRef.current.shift() ?? null;
96
+ activeConfirmRef.current = next;
97
+ setActiveConfirm(next);
98
+ }, []);
99
+ const requestConfirmation = (0, import_react.useCallback)(
100
+ async (request) => {
101
+ const handler = onConfirmRef.current;
102
+ if (!handler) {
103
+ return { approved: await enqueueConfirm(request) };
104
+ }
105
+ try {
106
+ return { approved: Boolean(await handler(request)) };
107
+ } catch (error) {
108
+ console.error("AiActionGuard onConfirm 回调异常:", error);
109
+ return { approved: false, exception: error };
110
+ }
111
+ },
112
+ [enqueueConfirm]
113
+ );
114
+ const runPermissionCheck = (0, import_react.useCallback)(
115
+ async (action, params) => {
116
+ try {
117
+ const allowed = await checkPermissionRef.current(action, params);
118
+ return { allowed: Boolean(allowed) };
119
+ } catch (error) {
120
+ console.error("AiActionGuard checkPermission 异常,已按拒绝处理:", error);
121
+ return { allowed: false, error };
122
+ }
123
+ },
124
+ []
125
+ );
126
+ const emitAudit = (0, import_react.useCallback)(
127
+ (action, params, result, undoHandle) => {
128
+ const handler = onAuditRef.current;
129
+ if (!handler)
130
+ return;
131
+ const event = {
132
+ action,
133
+ params,
134
+ domain: domainRef.current,
135
+ result,
136
+ userId: userIdRef.current,
137
+ sessionId: sessionIdRef.current,
138
+ timestamp: Date.now(),
139
+ undoHandle
140
+ };
141
+ try {
142
+ handler(event);
143
+ } catch (error) {
144
+ console.error("AiActionGuard onAudit 回调异常:", error);
145
+ }
146
+ },
147
+ []
148
+ );
149
+ const registerUndo = (0, import_react.useCallback)(
150
+ (action, undo2) => {
151
+ undoSeedRef.current += 1;
152
+ const id = `undo-${Date.now().toString(36)}-${undoSeedRef.current}`;
153
+ undoRegistryRef.current.set(id, undo2);
154
+ return { id, action, label: import_policy.AI_ACTION_LABELS[action] };
155
+ },
156
+ []
157
+ );
158
+ const canRun = (0, import_react.useCallback)(
159
+ async (action, params = {}) => {
160
+ if (!(0, import_policy.isKnownAiAction)(action)) {
161
+ return {
162
+ ok: false,
163
+ decision: "deny",
164
+ reason: "denied",
165
+ message: `未登记的动作:${action}`
166
+ };
167
+ }
168
+ const decision = (0, import_policy.resolveAiActionDecision)(
169
+ policyRef.current,
170
+ action,
171
+ params,
172
+ navigateCountRef.current
173
+ );
174
+ if (decision === "deny") {
175
+ return {
176
+ ok: false,
177
+ decision,
178
+ reason: "denied",
179
+ message: `策略拒绝执行「${import_policy.AI_ACTION_LABELS[action]}」`
180
+ };
181
+ }
182
+ const permission = await runPermissionCheck(action, params);
183
+ if (permission.error) {
184
+ return {
185
+ ok: false,
186
+ decision,
187
+ reason: "error",
188
+ message: "权限校验异常,已按拒绝处理"
189
+ };
190
+ }
191
+ if (!permission.allowed) {
192
+ return {
193
+ ok: false,
194
+ decision,
195
+ reason: "forbidden",
196
+ message: "当前用户无权限执行该动作"
197
+ };
198
+ }
199
+ if ((0, import_policy.requiresIdempotencyKey)(action, params)) {
200
+ if (!params.idempotencyKey) {
201
+ return {
202
+ ok: false,
203
+ decision,
204
+ reason: "denied",
205
+ message: "写类动作缺少幂等键 idempotencyKey"
206
+ };
207
+ }
208
+ if (usedKeysRef.current.has(params.idempotencyKey)) {
209
+ return {
210
+ ok: false,
211
+ decision,
212
+ reason: "denied",
213
+ message: "幂等键已执行,跳过重复执行"
214
+ };
215
+ }
216
+ }
217
+ return { ok: true, decision };
218
+ },
219
+ [runPermissionCheck]
220
+ );
221
+ const run = (0, import_react.useCallback)(
222
+ async (action, params = {}, execute) => {
223
+ if (!(0, import_policy.isKnownAiAction)(action)) {
224
+ emitAudit(action, params, "denied");
225
+ return {
226
+ ok: false,
227
+ action,
228
+ decision: "deny",
229
+ reason: "denied",
230
+ message: `未登记的动作:${action}`
231
+ };
232
+ }
233
+ const decision = (0, import_policy.resolveAiActionDecision)(
234
+ policyRef.current,
235
+ action,
236
+ params,
237
+ navigateCountRef.current
238
+ );
239
+ if (decision === "deny") {
240
+ emitAudit(action, params, "denied");
241
+ return {
242
+ ok: false,
243
+ action,
244
+ decision,
245
+ reason: "denied",
246
+ message: `策略拒绝执行「${import_policy.AI_ACTION_LABELS[action]}」`
247
+ };
248
+ }
249
+ if (decision === "confirm") {
250
+ const request = (0, import_policy.createAiActionConfirmRequest)(
251
+ action,
252
+ params,
253
+ domainRef.current,
254
+ confirmTextRef.current
255
+ );
256
+ const confirmation = await requestConfirmation(request);
257
+ if (confirmation.exception) {
258
+ emitAudit(action, params, "error");
259
+ return {
260
+ ok: false,
261
+ action,
262
+ decision,
263
+ reason: "error",
264
+ message: "确认流程异常,已按取消处理"
265
+ };
266
+ }
267
+ if (!confirmation.approved) {
268
+ emitAudit(action, params, "cancelled");
269
+ return {
270
+ ok: false,
271
+ action,
272
+ decision,
273
+ reason: "cancelled",
274
+ message: "用户取消了该动作"
275
+ };
276
+ }
277
+ }
278
+ const permission = await runPermissionCheck(action, params);
279
+ if (permission.error) {
280
+ emitAudit(action, params, "error");
281
+ return {
282
+ ok: false,
283
+ action,
284
+ decision,
285
+ reason: "error",
286
+ message: "权限校验异常,已按拒绝处理"
287
+ };
288
+ }
289
+ if (!permission.allowed) {
290
+ emitAudit(action, params, "denied");
291
+ return {
292
+ ok: false,
293
+ action,
294
+ decision,
295
+ reason: "forbidden",
296
+ message: "当前用户无权限执行该动作"
297
+ };
298
+ }
299
+ const needIdempotency = (0, import_policy.requiresIdempotencyKey)(action, params);
300
+ const idempotencyKey = needIdempotency ? params.idempotencyKey : void 0;
301
+ if (needIdempotency) {
302
+ if (!idempotencyKey) {
303
+ emitAudit(action, params, "denied");
304
+ return {
305
+ ok: false,
306
+ action,
307
+ decision,
308
+ reason: "denied",
309
+ message: "写类动作缺少幂等键 idempotencyKey"
310
+ };
311
+ }
312
+ if (usedKeysRef.current.has(idempotencyKey)) {
313
+ emitAudit(action, params, "denied");
314
+ return {
315
+ ok: false,
316
+ action,
317
+ decision,
318
+ reason: "denied",
319
+ message: "幂等键已执行,跳过重复执行"
320
+ };
321
+ }
322
+ usedKeysRef.current.add(idempotencyKey);
323
+ }
324
+ try {
325
+ let data;
326
+ let undoHandle;
327
+ if (execute) {
328
+ const output = await execute(params);
329
+ data = output == null ? void 0 : output.data;
330
+ if (output == null ? void 0 : output.undo) {
331
+ undoHandle = registerUndo(action, output.undo);
332
+ }
333
+ }
334
+ if (action === "navigate") {
335
+ navigateCountRef.current += 1;
336
+ }
337
+ emitAudit(action, params, "ok", undoHandle);
338
+ return { ok: true, action, decision, data, undoHandle };
339
+ } catch (error) {
340
+ const message = error instanceof Error ? error.message : "动作执行失败";
341
+ console.error("AiActionGuard 动作执行异常:", error);
342
+ emitAudit(action, params, "error");
343
+ return { ok: false, action, decision, reason: "error", message };
344
+ }
345
+ },
346
+ [emitAudit, registerUndo, requestConfirmation, runPermissionCheck]
347
+ );
348
+ const undo = (0, import_react.useCallback)(
349
+ async (handle) => {
350
+ const undoParams = { handleId: handle.id, targetAction: handle.action };
351
+ const decision = (0, import_policy.resolveAiActionDecision)(
352
+ policyRef.current,
353
+ "undo",
354
+ undoParams,
355
+ navigateCountRef.current
356
+ );
357
+ if (decision === "deny") {
358
+ emitAudit("undo", undoParams, "denied", handle);
359
+ return {
360
+ ok: false,
361
+ action: "undo",
362
+ decision,
363
+ reason: "denied",
364
+ message: "策略拒绝执行「撤销」"
365
+ };
366
+ }
367
+ if (decision === "confirm") {
368
+ const request = (0, import_policy.createAiActionConfirmRequest)(
369
+ "undo",
370
+ undoParams,
371
+ domainRef.current,
372
+ confirmTextRef.current
373
+ );
374
+ const confirmation = await requestConfirmation(request);
375
+ if (confirmation.exception) {
376
+ emitAudit("undo", undoParams, "error", handle);
377
+ return {
378
+ ok: false,
379
+ action: "undo",
380
+ decision,
381
+ reason: "error",
382
+ message: "确认流程异常,已按取消处理"
383
+ };
384
+ }
385
+ if (!confirmation.approved) {
386
+ emitAudit("undo", undoParams, "cancelled", handle);
387
+ return {
388
+ ok: false,
389
+ action: "undo",
390
+ decision,
391
+ reason: "cancelled",
392
+ message: "用户取消了撤销"
393
+ };
394
+ }
395
+ }
396
+ const permission = await runPermissionCheck("undo", undoParams);
397
+ if (permission.error) {
398
+ emitAudit("undo", undoParams, "error", handle);
399
+ return {
400
+ ok: false,
401
+ action: "undo",
402
+ decision,
403
+ reason: "error",
404
+ message: "权限校验异常,已按拒绝处理"
405
+ };
406
+ }
407
+ if (!permission.allowed) {
408
+ emitAudit("undo", undoParams, "denied", handle);
409
+ return {
410
+ ok: false,
411
+ action: "undo",
412
+ decision,
413
+ reason: "forbidden",
414
+ message: "当前用户无权限执行撤销"
415
+ };
416
+ }
417
+ const undoFn = undoRegistryRef.current.get(handle.id);
418
+ if (!undoFn) {
419
+ emitAudit("undo", undoParams, "denied", handle);
420
+ return {
421
+ ok: false,
422
+ action: "undo",
423
+ decision,
424
+ reason: "denied",
425
+ message: "撤销句柄已失效"
426
+ };
427
+ }
428
+ try {
429
+ await undoFn();
430
+ undoRegistryRef.current.delete(handle.id);
431
+ emitAudit("undo", undoParams, "ok", handle);
432
+ return { ok: true, action: "undo", decision };
433
+ } catch (error) {
434
+ const message = error instanceof Error ? error.message : "撤销执行失败";
435
+ console.error("AiActionGuard 撤销异常:", error);
436
+ emitAudit("undo", undoParams, "error", handle);
437
+ return { ok: false, action: "undo", decision, reason: "error", message };
438
+ }
439
+ },
440
+ [emitAudit, requestConfirmation, runPermissionCheck]
441
+ );
442
+ const api = (0, import_react.useMemo)(() => ({ canRun, run, undo }), [canRun, run, undo]);
443
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(AiActionContext.Provider, { value: api, children: [
444
+ children,
445
+ onConfirm ? null : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
446
+ import_AiActionConfirmModal.AiActionConfirmModal,
447
+ {
448
+ open: Boolean(activeConfirm),
449
+ request: activeConfirm == null ? void 0 : activeConfirm.request,
450
+ onOk: () => settleConfirm(true),
451
+ onCancel: () => settleConfirm(false)
452
+ }
453
+ )
454
+ ] });
455
+ }
456
+ function useAiAction() {
457
+ const context = (0, import_react.useContext)(AiActionContext);
458
+ if (!context) {
459
+ throw new Error("useAiAction 必须在 AiActionGuard 内使用");
460
+ }
461
+ return context;
462
+ }
463
+ // Annotate the CommonJS export names for ESM import in node:
464
+ 0 && (module.exports = {
465
+ AiActionGuard,
466
+ useAiAction
467
+ });
@@ -0,0 +1,5 @@
1
+ export { AiActionGuard, useAiAction } from "./AiActionGuard";
2
+ export { AiActionConfirmModal } from "./AiActionConfirmModal";
3
+ export type { AiActionConfirmModalProps } from "./types";
4
+ export { AI_ACTION_LABELS, AI_ACTION_TYPES, DEFAULT_AI_ACTION_POLICY, WRITE_ACTION_TYPES, createAiActionConfirmRequest, isKnownAiAction, requiresIdempotencyKey, resolveAiActionDecision, } from "./policy";
5
+ export type { AiActionAuditEvent, AiActionAuditResult, AiActionCanRunResult, AiActionConfirmRequest, AiActionConfirmText, AiActionDecision, AiActionExecutionResult, AiActionExecutor, AiActionFailure, AiActionGuardProps, AiActionParams, AiActionPolicy, AiActionReason, AiActionResult, AiActionSuccess, AiActionType, AiUndoHandle, UseAiActionValue, } from "./types";
@@ -0,0 +1,51 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/ai-assistant/AiActionGuard/index.ts
20
+ var AiActionGuard_exports = {};
21
+ __export(AiActionGuard_exports, {
22
+ AI_ACTION_LABELS: () => import_policy.AI_ACTION_LABELS,
23
+ AI_ACTION_TYPES: () => import_policy.AI_ACTION_TYPES,
24
+ AiActionConfirmModal: () => import_AiActionConfirmModal.AiActionConfirmModal,
25
+ AiActionGuard: () => import_AiActionGuard.AiActionGuard,
26
+ DEFAULT_AI_ACTION_POLICY: () => import_policy.DEFAULT_AI_ACTION_POLICY,
27
+ WRITE_ACTION_TYPES: () => import_policy.WRITE_ACTION_TYPES,
28
+ createAiActionConfirmRequest: () => import_policy.createAiActionConfirmRequest,
29
+ isKnownAiAction: () => import_policy.isKnownAiAction,
30
+ requiresIdempotencyKey: () => import_policy.requiresIdempotencyKey,
31
+ resolveAiActionDecision: () => import_policy.resolveAiActionDecision,
32
+ useAiAction: () => import_AiActionGuard.useAiAction
33
+ });
34
+ module.exports = __toCommonJS(AiActionGuard_exports);
35
+ var import_AiActionGuard = require("./AiActionGuard");
36
+ var import_AiActionConfirmModal = require("./AiActionConfirmModal");
37
+ var import_policy = require("./policy");
38
+ // Annotate the CommonJS export names for ESM import in node:
39
+ 0 && (module.exports = {
40
+ AI_ACTION_LABELS,
41
+ AI_ACTION_TYPES,
42
+ AiActionConfirmModal,
43
+ AiActionGuard,
44
+ DEFAULT_AI_ACTION_POLICY,
45
+ WRITE_ACTION_TYPES,
46
+ createAiActionConfirmRequest,
47
+ isKnownAiAction,
48
+ requiresIdempotencyKey,
49
+ resolveAiActionDecision,
50
+ useAiAction
51
+ });
@@ -0,0 +1,23 @@
1
+ import type { AiActionConfirmRequest, AiActionConfirmText, AiActionDecision, AiActionParams, AiActionPolicy, AiActionType } from "./types";
2
+ /** 动作白名单(未知动作一律 deny) */
3
+ export declare const AI_ACTION_TYPES: readonly AiActionType[];
4
+ /** 写类动作:需幂等键;其中 create-draft 仅在 persist=true(落库)时按写类处理 */
5
+ export declare const WRITE_ACTION_TYPES: readonly AiActionType[];
6
+ /** 默认策略(可被 AiActionGuard.policy 覆盖) */
7
+ export declare const DEFAULT_AI_ACTION_POLICY: Readonly<Record<AiActionType, AiActionDecision>>;
8
+ /** 动作中文名(确认弹层与审计展示用) */
9
+ export declare const AI_ACTION_LABELS: Readonly<Record<AiActionType, string>>;
10
+ /** 运行时白名单判定(防 JS 侧传入未登记动作) */
11
+ export declare function isKnownAiAction(action: string): action is AiActionType;
12
+ /** 该动作在本次参数下是否属于「写」并要求幂等键 */
13
+ export declare function requiresIdempotencyKey(action: AiActionType, params: AiActionParams): boolean;
14
+ /**
15
+ * 解析本次动作决策:
16
+ * - 显式 policy 覆盖优先(不再叠加默认动态规则)
17
+ * - 默认策略下,同一会话内第 2 次及以上 navigate 需确认
18
+ * - 默认策略下,rollback 且调用方声明存在未保存修改时需确认
19
+ * - 未登记动作 deny
20
+ */
21
+ export declare function resolveAiActionDecision(policy: AiActionPolicy | undefined, action: AiActionType, params: AiActionParams, navigateCount: number): AiActionDecision;
22
+ /** 构造确认请求(内置弹层与宿主自定义 UI 共用) */
23
+ export declare function createAiActionConfirmRequest(action: AiActionType, params: AiActionParams, domain: string, confirmText?: AiActionConfirmText): AiActionConfirmRequest;