@dsh-workbench/shared 1.5.0 → 1.5.2-alpha.0

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.
@@ -30,13 +30,13 @@ export interface ApprovalDetailProps {
30
30
  readonly renderShell?: ((input: ApprovalInput, t: Translate) => ReactNode) | undefined;
31
31
  }
32
32
  /**
33
- * 审批屏的详情格:按工具名找到认领它的模块交给它渲染,没有模块认领就退回外壳那张表或一段原始命令。
33
+ * 审批屏的详情格:先画模块交的四行事实或它自己的渲染器,再退回外壳那张表或一段原始命令。
34
34
  * @param props.callId - 这次工具调用的 id;不给就直接走兜底(连调用都找不到)。
35
35
  * @param props.useChat - 读会话快照的钩子(dsh 注入);不给就当作读不到调用。
36
36
  * @param props.units - 已装模块的面,按各自的 `approval.tools` 认领这次调用。
37
37
  * @param props.t - 翻译函数;不给就用返回 key 的直通实现。
38
38
  * @param props.renderShell - 没有模块认领时外壳自己的渲染(那张表);不给且这次调用没带 `command` 时整格返回 `null`。
39
- * @returns 认领方的渲染、外壳那张表,或一段可复制的命令。
39
+ * @returns 认领方的四行表 / 自定义渲染、外壳那张表,或一段可复制的命令。
40
40
  */
41
- export declare function ApprovalDetail(props: ApprovalDetailProps): JSX.Element | null;
41
+ export declare function ApprovalDetail(props: ApprovalDetailProps): string | number | bigint | boolean | Iterable<ReactNode> | Promise<string | number | bigint | boolean | import("react").ReactPortal | import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>> | Iterable<ReactNode> | null | undefined> | JSX.Element | null | undefined;
42
42
  export {};
@@ -1,5 +1,6 @@
1
1
  import { Fragment, h } from '../../lib/react.js';
2
2
  import { CodeBlock } from '@deepseek-ai/dsh-client-ui-primitives';
3
+ import { ApprovalPlan } from './ApprovalPlan.js';
3
4
  import { CardBoundary } from './CardBoundary.js';
4
5
  import './ApprovalDetail.css';
5
6
  function callOf(snapshot, callId) {
@@ -22,14 +23,44 @@ function parseArgs(raw) {
22
23
  return {};
23
24
  }
24
25
  }
26
+ function reasonOf(error) {
27
+ return error instanceof Error ? error.message : String(error);
28
+ }
29
+ function planRowsOf(plan, t) {
30
+ const rows = [
31
+ { label: plan.objectLabel ?? t('approval.fact.object'), value: plan.object },
32
+ { label: t('approval.fact.change'), value: plan.change },
33
+ { label: t('approval.fact.target'), value: plan.target },
34
+ { label: t('approval.fact.impact'), value: plan.impact },
35
+ ];
36
+ return plan.extra === undefined ? rows : [...rows, ...plan.extra];
37
+ }
38
+ function planNodeOf(plan, t) {
39
+ return h(ApprovalPlan, {
40
+ rows: planRowsOf(plan, t),
41
+ ...(plan.warn === undefined ? {} : { warn: plan.warn }),
42
+ });
43
+ }
44
+ function missingNodeOf(t) {
45
+ return h(ApprovalPlan, { rows: [], warn: t('approval.missingDetail') });
46
+ }
47
+ function planOf(plan, input, t) {
48
+ try {
49
+ return plan(input, t);
50
+ }
51
+ catch (error) {
52
+ console.error(`[dsh-workbench] 模块审批详情读取失败:${reasonOf(error)}`);
53
+ return null;
54
+ }
55
+ }
25
56
  /**
26
- * 审批屏的详情格:按工具名找到认领它的模块交给它渲染,没有模块认领就退回外壳那张表或一段原始命令。
57
+ * 审批屏的详情格:先画模块交的四行事实或它自己的渲染器,再退回外壳那张表或一段原始命令。
27
58
  * @param props.callId - 这次工具调用的 id;不给就直接走兜底(连调用都找不到)。
28
59
  * @param props.useChat - 读会话快照的钩子(dsh 注入);不给就当作读不到调用。
29
60
  * @param props.units - 已装模块的面,按各自的 `approval.tools` 认领这次调用。
30
61
  * @param props.t - 翻译函数;不给就用返回 key 的直通实现。
31
62
  * @param props.renderShell - 没有模块认领时外壳自己的渲染(那张表);不给且这次调用没带 `command` 时整格返回 `null`。
32
- * @returns 认领方的渲染、外壳那张表,或一段可复制的命令。
63
+ * @returns 认领方的四行表 / 自定义渲染、外壳那张表,或一段可复制的命令。
33
64
  */
34
65
  export function ApprovalDetail(props) {
35
66
  const useChat = props.useChat ?? (() => undefined);
@@ -40,8 +71,29 @@ export function ApprovalDetail(props) {
40
71
  const command = typeof args.command === 'string' ? args.command : '';
41
72
  const owner = (props.units ?? []).find((face) => face.approval?.tools.includes(input.tool) === true);
42
73
  if (owner?.approval !== undefined) {
43
- const Render = owner.approval.render;
44
- return (h(CardBoundary, { unit: owner.unit, resetKey: props.callId ?? '', t: t },
74
+ const approval = owner.approval;
75
+ if (approval.plan !== undefined) {
76
+ const plan = planOf(approval.plan, input, t);
77
+ if (plan === null)
78
+ return missingNodeOf(t);
79
+ const fallback = planNodeOf(plan, t);
80
+ const render = approval.render;
81
+ if (render === undefined)
82
+ return fallback;
83
+ let custom;
84
+ try {
85
+ custom = render({ input, plan, t });
86
+ }
87
+ catch (error) {
88
+ console.error(`[dsh-workbench] 模块审批自定义渲染失败:${reasonOf(error)}`);
89
+ return fallback;
90
+ }
91
+ if (custom === null || custom === undefined || custom === false)
92
+ return fallback;
93
+ return (h(CardBoundary, { unit: owner.unit, resetKey: props.callId ?? '', t: t, fallback: fallback }, custom));
94
+ }
95
+ const Render = approval.render;
96
+ return (h(CardBoundary, { unit: owner.unit, resetKey: props.callId ?? '', t: t, fallback: missingNodeOf(t) },
45
97
  h(Render, { input: input, t: t })));
46
98
  }
47
99
  const shell = props.renderShell?.(input, t);
@@ -9,6 +9,7 @@ export interface CardBoundaryProps {
9
9
  readonly resetKey: string;
10
10
  readonly t: Translate;
11
11
  readonly onFailure?: ((reason: string) => void) | undefined;
12
+ readonly fallback?: ReactNode | undefined;
12
13
  readonly children?: ReactNode | undefined;
13
14
  }
14
15
  /**
@@ -29,6 +30,7 @@ export declare function errorReasonOf(error: unknown): string;
29
30
  * @param props.resetKey - 重来键:它一变(如切了工作区)就自动清掉报错,把卡片重新渲染一遍。
30
31
  * @param props.t - 报错那句与重试钮文案的翻译函数。
31
32
  * @param props.onFailure - 失败时把原因报给宿主(面板状态要用);不给就只打一条 console。
33
+ * @param props.fallback - 出错时改画这一份;不给就画卡片失败提示与重试。
32
34
  * @param props.children - 正常时要渲染的卡片内容。
33
35
  * @returns 正常时是 `children`,炸过且还没重来之前是一条报错。
34
36
  */
@@ -18,6 +18,7 @@ export function errorReasonOf(error) {
18
18
  * @param props.resetKey - 重来键:它一变(如切了工作区)就自动清掉报错,把卡片重新渲染一遍。
19
19
  * @param props.t - 报错那句与重试钮文案的翻译函数。
20
20
  * @param props.onFailure - 失败时把原因报给宿主(面板状态要用);不给就只打一条 console。
21
+ * @param props.fallback - 出错时改画这一份;不给就画卡片失败提示与重试。
21
22
  * @param props.children - 正常时要渲染的卡片内容。
22
23
  * @returns 正常时是 `children`,炸过且还没重来之前是一条报错。
23
24
  */
@@ -43,6 +44,8 @@ export class CardBoundary extends Component {
43
44
  const { reason } = this.state;
44
45
  if (reason === null)
45
46
  return this.props.children;
47
+ if (this.props.fallback !== undefined)
48
+ return this.props.fallback;
46
49
  return (h(Note, { tone: "problem" },
47
50
  h("div", { className: styles.failure },
48
51
  h("span", null, this.props.t('panels.cardBroken', { unit: this.props.unit, reason })),
@@ -277,13 +277,7 @@ export interface UnitClientFace {
277
277
  readonly view?: CardView;
278
278
  readonly steps?: Readonly<Record<string, UnitStep>>;
279
279
  readonly messages?: Dictionaries;
280
- readonly approval?: {
281
- readonly tools: readonly string[];
282
- readonly render: (props: {
283
- readonly input: ApprovalInput;
284
- readonly t: Translate;
285
- }) => ReactNode;
286
- } | undefined;
280
+ readonly approval?: UnitApprovalFace | undefined;
287
281
  }
288
282
  /**
289
283
  * 带着自己 manifest 的模块面(外壳按 unit 认领时用)。
@@ -298,6 +292,52 @@ export interface ApprovalInput {
298
292
  readonly tool: string;
299
293
  readonly args: Readonly<Record<string, unknown>>;
300
294
  }
295
+ /**
296
+ * 审批详情里的一行附加事实:标签与值都由模块给。
297
+ */
298
+ export interface ApprovalPlanExtra {
299
+ readonly label: string;
300
+ readonly value: string;
301
+ }
302
+ /**
303
+ * 模块审批详情的四行数据:对象、修改后、目标位置、影响范围,外加可选的领域标签、附加行与警告。
304
+ */
305
+ export interface ApprovalPlanData {
306
+ readonly object: string;
307
+ readonly change: string;
308
+ readonly target: string;
309
+ readonly impact: string;
310
+ readonly objectLabel?: string | undefined;
311
+ readonly extra?: readonly ApprovalPlanExtra[] | undefined;
312
+ readonly warn?: string | undefined;
313
+ }
314
+ /**
315
+ * 带四行数据的模块审批面:`plan` 是下限,`render` 可选地接管详情区(同步纯函数,不直接调 hook)。
316
+ */
317
+ export interface ApprovalPlanFace {
318
+ readonly tools: readonly string[];
319
+ readonly plan: (input: ApprovalInput, t: Translate) => ApprovalPlanData | null;
320
+ readonly render?: ((props: {
321
+ readonly input: ApprovalInput;
322
+ readonly plan: ApprovalPlanData;
323
+ readonly t: Translate;
324
+ }) => ReactNode) | undefined;
325
+ }
326
+ /**
327
+ * 已安装旧模块的审批面:只有自定义渲染,没有四行数据。
328
+ */
329
+ export interface ApprovalLegacyFace {
330
+ readonly tools: readonly string[];
331
+ readonly plan?: undefined;
332
+ readonly render: (props: {
333
+ readonly input: ApprovalInput;
334
+ readonly t: Translate;
335
+ }) => ReactNode;
336
+ }
337
+ /**
338
+ * 一个模块交出来的审批面:新契约是四行数据 + 可选自定义,旧契约只有自定义。
339
+ */
340
+ export type UnitApprovalFace = ApprovalPlanFace | ApprovalLegacyFace;
301
341
  /**
302
342
  * 模块注册进外壳的那个口:注册一份面(拿回注销函数)、列出已装 unit。
303
343
  */
@@ -192,6 +192,7 @@ export interface MarketInstalledModule {
192
192
  }
193
193
  export interface MarketInstalledPayload {
194
194
  readonly modules: readonly MarketInstalledModule[];
195
+ readonly problem?: DiscoverProblem;
195
196
  }
196
197
  export interface InstalledPackageInfo {
197
198
  readonly pkg: string;
@@ -33,8 +33,11 @@ export interface ApprovalCall {
33
33
  }
34
34
  /**
35
35
  * 模块对一次写调用给出的判定:`read` 只是看看、`allow` 会写但本模块不问、`deny` 这一次根本跑不成
36
- * (连问都不该问,`reason` 原样交给 agent)、`ask` 要用户点头(`question` 接在明细后,`fallback` 是没明细时的整句;
37
- * `noteInDetail` 为真表示模块把 agent 的附注画在自己的详情座里,标题只留动作、不再追加)。
36
+ * (连问都不该问,`reason` 原样交给 agent)、`ask` 要用户点头(`headline` 是具体标题;没有它时
37
+ * `question` 接在明细后,`fallback` 是没明细时的整句;`noteInDetail` 为真表示模块把 agent 的附注
38
+ * 画在自己的详情座里,标题只留动作、不再追加)。
39
+ * `preflight` 是在显示审批之前先执行的机器检查:返回非空字符串表示这次执行不了,原样交给 agent、
40
+ * 用户不会被询问;返回空表示照常询问;抛出同样转成 `deny`(不会静默放行)。
38
41
  */
39
42
  export type ApprovalVerdict = {
40
43
  readonly kind: 'read';
@@ -46,9 +49,11 @@ export type ApprovalVerdict = {
46
49
  } | {
47
50
  readonly kind: 'ask';
48
51
  readonly reason: string;
52
+ readonly headline?: ApprovalDetail | undefined;
49
53
  readonly question: ApprovalDetail;
50
54
  readonly fallback: ApprovalDetail;
51
55
  readonly noteInDetail?: boolean;
56
+ readonly preflight?: (() => Promise<string | undefined>) | undefined;
52
57
  };
53
58
  export type ApprovalPolicy = (call: ApprovalCall) => ApprovalVerdict | undefined;
54
59
  /**
@@ -64,22 +69,29 @@ export interface ApprovalContext {
64
69
  }
65
70
  export declare function approvalFor(call: ApprovalCall, context?: ApprovalContext): Promise<AskDecision | undefined>;
66
71
  /**
67
- * 「写工具要点头」那一对的默认口径:点名的工具一律 `ask` + 同一份明细,其余一律 `read`。
72
+ * 审批屏上的一段中英文文本:给值,或按每次调用算。
73
+ */
74
+ export type ApprovalText = ApprovalDetail | ((call: ApprovalCall) => ApprovalDetail);
75
+ /**
76
+ * 「写工具要点头」那一对的默认口径:点名的工具一律 `ask`,其余一律 `read`。
68
77
  * 把它直接摊在 `mount()` 的返回值里:`...writeApprovalOf({ … })`。
69
78
  * @param options.tools 需要点头的工具名。
70
79
  * @param options.reason 为什么问(给 agent 看的理由)。
71
- * @param options.question 问句(zh / en,≤40 字)。
72
- * @param options.fallback 没有明细时的整句(zh / en,≤40 字)。
73
- * @param options.detail 审批屏头行那句明细(zh / en,≤20 字)。
80
+ * @param options.headline 审批屏的标题;有它时 `approvalDetail` 可以省,默认也用标题补 question / fallback。
81
+ * @param options.question 问句(zh / en,≤40 字);不给就取 `headline`。
82
+ * @param options.fallback 没有明细时的整句(zh / en,≤40 字);不给就取 `headline`。
83
+ * @param options.detail 审批屏头行那句明细(zh / en,≤20 字);有 `headline` 时可以省。
74
84
  * @returns `{ needsApproval, approvalDetail }`。
75
85
  */
76
- export declare function writeApprovalOf(options: {
86
+ export interface WriteApprovalOptions {
77
87
  readonly tools: readonly string[];
78
88
  readonly reason: string;
79
- readonly question: ApprovalDetail;
80
- readonly fallback: ApprovalDetail;
81
- readonly detail: ApprovalDetail;
82
- }): {
89
+ readonly headline?: ApprovalText | undefined;
90
+ readonly question?: ApprovalText | undefined;
91
+ readonly fallback?: ApprovalText | undefined;
92
+ readonly detail?: ApprovalText | undefined;
93
+ }
94
+ export declare function writeApprovalOf(options: WriteApprovalOptions): {
83
95
  needsApproval(call: ApprovalCall): ApprovalVerdict;
84
96
  approvalDetail(call: ApprovalCall): ApprovalFacts | undefined;
85
97
  };
@@ -75,6 +75,17 @@ export async function approvalFor(call, context = {}) {
75
75
  '下一步:在模块的 needsApproval 中补一条审批(包含「这次会发生什么」)。',
76
76
  };
77
77
  }
78
+ if (verdict.preflight !== undefined) {
79
+ let blocked;
80
+ try {
81
+ blocked = await verdict.preflight();
82
+ }
83
+ catch (error) {
84
+ return { kind: 'deny', reason: `审批前的检查没有完成,已拒绝这次调用:${messageOf(error)}` };
85
+ }
86
+ if (blocked !== undefined && blocked.trim() !== '')
87
+ return { kind: 'deny', reason: blocked.trim() };
88
+ }
78
89
  let facts;
79
90
  try {
80
91
  facts = await context.detailOf?.(call);
@@ -87,29 +98,45 @@ export async function approvalFor(call, context = {}) {
87
98
  if (context.skipAsk === true)
88
99
  return undefined;
89
100
  const note = context.declaredNote === true && verdict.noteInDetail !== true ? approvalNoteOf(call) : undefined;
101
+ const headline = verdict.headline;
102
+ const displayReason = headline !== undefined && headline.zh.trim() !== '' && headline.en.trim() !== ''
103
+ ? { en: oneLine(headline.en), zh: oneLine(headline.zh) }
104
+ : compose(facts, verdict, note);
90
105
  return {
91
106
  kind: 'ask',
92
107
  reason: verdict.reason,
93
- displayReason: compose(facts, verdict, note),
108
+ displayReason,
94
109
  };
95
110
  }
96
- /**
97
- * 「写工具要点头」那一对的默认口径:点名的工具一律 `ask` + 同一份明细,其余一律 `read`。
98
- * 把它直接摊在 `mount()` 的返回值里:`...writeApprovalOf({ … })`。
99
- * @param options.tools 需要点头的工具名。
100
- * @param options.reason 为什么问(给 agent 看的理由)。
101
- * @param options.question 问句(zh / en,≤40 字)。
102
- * @param options.fallback 没有明细时的整句(zh / en,≤40 字)。
103
- * @param options.detail 审批屏头行那句明细(zh / en,≤20 字)。
104
- * @returns `{ needsApproval, approvalDetail }`。
105
- */
111
+ function resolveText(value, call) {
112
+ if (value === undefined)
113
+ return undefined;
114
+ return typeof value === 'function' ? value(call) : value;
115
+ }
106
116
  export function writeApprovalOf(options) {
107
117
  const tools = new Set(options.tools);
108
118
  const named = (call) => call.name !== undefined && tools.has(call.name);
109
119
  return {
110
- needsApproval: (call) => named(call)
111
- ? { kind: 'ask', reason: options.reason, question: options.question, fallback: options.fallback }
112
- : { kind: 'read' },
113
- approvalDetail: (call) => (named(call) ? options.detail : undefined),
120
+ needsApproval: (call) => {
121
+ if (!named(call))
122
+ return { kind: 'read' };
123
+ const headline = resolveText(options.headline, call);
124
+ const question = resolveText(options.question, call) ?? headline;
125
+ const fallback = resolveText(options.fallback, call) ?? headline;
126
+ if (question === undefined || fallback === undefined) {
127
+ return {
128
+ kind: 'deny',
129
+ reason: 'writeApprovalOf 没有给 headline、question 或 fallback —— 这次无法问清,先不要发起审批',
130
+ };
131
+ }
132
+ return {
133
+ kind: 'ask',
134
+ reason: options.reason,
135
+ ...(headline === undefined ? {} : { headline }),
136
+ question,
137
+ fallback,
138
+ };
139
+ },
140
+ approvalDetail: (call) => (named(call) ? resolveText(options.detail, call) : undefined),
114
141
  };
115
142
  }
package/dist/i18n/en.js CHANGED
@@ -351,9 +351,11 @@ export const en = {
351
351
  'panels.market.checkUpdates': 'Check for updates',
352
352
  'panels.market.checkAll': 'Check all updates',
353
353
  'panels.market.checkingNow': 'Checking…',
354
- 'panels.market.checkFailed': 'The last check failed. Check your network and try again.',
354
+ 'panels.market.checkFailed': 'Check failed. Check your network and try again.',
355
+ 'panels.market.notCheckedYet': 'Not checked for updates yet',
355
356
  'panels.market.checkedAt': 'Last checked: {time}',
356
- 'panels.market.latestPartial': 'Could not check {n} modules',
357
+ 'panels.market.latestPartial': 'Could not check {n} modules for a new version',
358
+ 'panels.market.sourceUnreachable': 'Could not reach the module source. Try again.',
357
359
  'panels.market.latestNoRegistry': 'No source configured, so the latest version was not checked.',
358
360
  'panels.market.latestProbeFailed': 'Could not reach the source. Try again later.',
359
361
  'panels.market.justNow': 'just now',
@@ -837,6 +839,11 @@ export const en = {
837
839
  'approval.plan.do': 'What you can do',
838
840
  'approval.plan.agent': 'What the AI can do',
839
841
  'approval.plan.data': 'Data source',
842
+ 'approval.fact.object': 'Item',
843
+ 'approval.fact.change': 'After',
844
+ 'approval.fact.target': 'Destination',
845
+ 'approval.fact.impact': 'Impact',
846
+ 'approval.missingDetail': 'This module does not say what will change. Reject, and ask the agent to add approval details.',
840
847
  'approval.record.site': 'Site',
841
848
  'approval.record.what': 'Recording',
842
849
  'approval.record.lands': 'Saves to',
@@ -845,6 +852,42 @@ export const en = {
845
852
  'approval.record.oneAction': 'one action',
846
853
  'approval.longTask.what': 'Task to stop',
847
854
  'approval.longTask.signal': 'Stopping only sends an interrupt signal from the workbench; how the task wraps up is up to the module.',
855
+ 'approval.shell.module': 'Module',
856
+ 'approval.shell.panel': 'Panel',
857
+ 'approval.shell.bundle': 'Module package',
858
+ 'approval.shell.dependency': 'Dependency',
859
+ 'approval.shell.plugin': 'Plugin',
860
+ 'approval.shell.workbench': 'Workbench',
861
+ 'approval.shell.itself': 'itself',
862
+ 'approval.shell.projectPanel': "this project's panel",
863
+ 'approval.shell.machineDefaults': 'the machine-level defaults',
864
+ 'approval.shell.thisMachine': 'this machine',
865
+ 'approval.shell.config': "this machine's workbench config",
866
+ 'approval.shell.page': 'the current workbench page',
867
+ 'approval.shell.addPanel': 'add to this project panel',
868
+ 'approval.shell.resetProject': "clear this project's panel changes",
869
+ 'approval.shell.resetMachine': 'clear the machine-level defaults',
870
+ 'approval.shell.followDefaults': 'follow the machine-level defaults again',
871
+ 'approval.shell.moveFront': 'move to the front',
872
+ 'approval.shell.moveAt': 'move to position {index}',
873
+ 'approval.shell.uninstallModule': 'uninstall the module and its packages',
874
+ 'approval.shell.installBundle': 'install as a workbench module package',
875
+ 'approval.shell.updateLatest': 'update to the latest version',
876
+ 'approval.shell.uninstallPlugin': 'uninstall the plugin',
877
+ 'approval.shell.startDebug': 'start debugging',
878
+ 'approval.shell.resumeDebug': 'resume debugging',
879
+ 'approval.shell.impactPanel': 'display and order only; no module data',
880
+ 'approval.shell.impactMove': 'order only; no module data',
881
+ 'approval.shell.impactReset': 'restore machine defaults; modules stay',
882
+ 'approval.shell.impactFollow': 'only this row; other rows stay',
883
+ 'approval.shell.impactUninstallModule': 'card, tools and packages are removed',
884
+ 'approval.shell.impactInstallBundle': 'adds a package; no existing module data changes',
885
+ 'approval.shell.impactDependency': 'every module using it changes version',
886
+ 'approval.shell.impactPlugin': 'modules using it change version',
887
+ 'approval.shell.impactUninstallPlugin': 'modules depending on it may stop working',
888
+ 'approval.shell.impactWorkbench': 'takes effect after restarting panels and modules',
889
+ 'approval.shell.impactStartDebug': 'takes over this page; stay on it while debugging',
890
+ 'approval.shell.impactResumeDebug': 'takes over this page again; stay on it while debugging',
848
891
  'approval.plugin.name': 'Plugin',
849
892
  'approval.plugin.command': 'Command',
850
893
  'approval.plugin.note': 'Agent note',