返回 CodeWhale
escalation.js
根目录 / crates / engine / extension-host / src / dsh / upstream / creator / sandbox / escalation.js
1 /**
2 * `@deepseek-ai/dsh-sandbox` escalation excerpt for Codewhale built-in creator.
3 *
4 * Lines between the VERBATIM markers are copied unchanged from
5 * `@deepseek-ai/dsh-sandbox@0.2.2-rc.1` `lib/index.js` (lines 9-42, 68-71, and
6 * 137-158), MIT License, Copyright (c) 2026 DeepSeek. See ../LICENSE.
7 */
8 import { assertNever } from "@deepseek-ai/dsh-util-values";
9
10 // ---- VERBATIM @deepseek-ai/dsh-sandbox@0.2.2-rc.1 lib/index.js:9-42 ----
11 /** English escalation messages. */
12 const en = { escalation: (mode, justification) => `Allow this operation with ${mode} permissions: ${justification}` };
13 /** Chinese escalation messages. */
14 const zh = { escalation: (mode, justification) => `允许本次操作使用 ${mode} 权限:${justification}` };
15 /** Japanese escalation messages. */
16 const ja = { escalation: (mode, justification) => `${mode} 権限でこの操作を許可します:${justification}` };
17 /** Spanish dictionary, checked against the English keys. */
18 const es = { escalation: (mode, justification) => `Permitir esta operación con permisos ${mode}: ${justification}` };
19 /** French dictionary, checked against the English keys. */
20 const fr = { escalation: (mode, justification) => `Autoriser cette opération avec les permissions ${mode} : ${justification}` };
21 /** German dictionary, checked against the English keys. */
22 const de = { escalation: (mode, justification) => `Diesen Vorgang mit ${mode}-Berechtigungen zulassen: ${justification}` };
23 /** Russian dictionary, checked against the English keys. */
24 const ru = { escalation: (mode, justification) => `Разрешить эту операцию с правами ${mode}: ${justification}` };
25 /** Portuguese dictionary, checked against the English keys. */
26 const pt = { escalation: (mode, justification) => `Permitir esta operação com permissões ${mode}: ${justification}` };
27 /**
28 * Build translations for the existing Client localized-text resolver.
29 * @param mode - requested sandbox mode.
30 * @param justification - raw model justification, preserved in every translation.
31 * @returns localized approval text.
32 */
33 function escalationDisplayReason(mode, justification) {
34 return {
35 en: en.escalation(mode, justification),
36 zh: zh.escalation(mode, justification),
37 ja: ja.escalation(mode, justification),
38 es: es.escalation(mode, justification),
39 fr: fr.escalation(mode, justification),
40 de: de.escalation(mode, justification),
41 ru: ru.escalation(mode, justification),
42 pt: pt.escalation(mode, justification)
43 };
44 }
45 // ---- END VERBATIM ----
46
47 // ---- VERBATIM @deepseek-ai/dsh-sandbox@0.2.2-rc.1 lib/index.js:68-71 ----
48 const WIDER_MODES = {
49 "read-only": ["workspace-write", "danger-full-access"],
50 "workspace-write": ["danger-full-access"]
51 };
52 // ---- END VERBATIM ----
53
54 // ---- VERBATIM @deepseek-ai/dsh-sandbox@0.2.2-rc.1 lib/index.js:137-158 ----
55 async function approveEscalation(request, approval) {
56 const { requestedMode: mode, effectiveMode, justification, subject } = request;
57 if (mode === effectiveMode) return effectiveMode;
58 if (!(WIDER_MODES[effectiveMode] ?? []).includes(mode)) throw new Error(`sandbox escalation to "${mode}" is not strictly wider than this call's current "${effectiveMode}" mode`);
59 if (approval.approver === void 0) throw new Error(`sandbox escalation to "${mode}" requires approval, but no approval service is composed`);
60 if (approval.agent === void 0) throw new Error(`sandbox escalation to "${mode}" requires approval, but the call has no agent to route it through`);
61 const outcome = await approval.approver.request({
62 agent: approval.agent,
63 toolName: approval.toolName,
64 callId: approval.callId,
65 reason: `escalate sandbox to ${mode}: ${justification}`,
66 displayReason: escalationDisplayReason(mode, justification),
67 ...approval.signal ? { signal: approval.signal } : {}
68 });
69 switch (outcome) {
70 case "allowed-once": return mode;
71 case "rejected": throw new Error(`the user rejected escalating this ${subject} to "${mode}"; it stays denied, so stop and explain instead of working around it`);
72 case "cancelled": throw new Error(`approval for escalating to "${mode}" was cancelled`);
73 case "unavailable": throw new Error(`sandbox escalation to "${mode}" requires approval, but no approval channel is available`);
74 default: return assertNever(outcome, "EscalationOutcome");
75 }
76 }
77 // ---- END VERBATIM ----
78
79 export { WIDER_MODES, escalationDisplayReason, approveEscalation };
80
80 lines JAVASCRIPT