/** * Browser half: the Settings → Plugins card for this plugin's trim namespace. * * The Plugins tab pairs every settings namespace the Host serves with a card * registered under the SAME key (a KEYED slot: `key: `, dispatched * with `entryKey`), so a host namespace without a card renders nothing. The tab * lays out one flex column and nothing else — the host's contract is that "a * plugin that ships a browser half owns its own card: chrome, controls, and * copy" — so the collapsible header, chevron, footer, and spacing below mirror * the host's `PluginCard`, using the host's own rules and `--dsw-alias-*` tokens. * Two deliberate deviations: the unsaved badge is hand-drawn rather than the * host's `Tag` primitive (that would need a non-baseline module request), and the * failure line uses `--dsw-alias-state-error-primary` because the host's own * `--dsw-alias-label-error` is not defined in the installed theme. * * Hand-written rather than bundled: the loader envelope is the whole contract, * `react` is a baseline platform module, and the repository deliberately stays * build-step free — `node --check` still lints it. */ window.__ModuleLoader__.load({ id: 'dsh-plugin-subagent-roles', factory: (require) => { var module = { exports: {} } var exports = module.exports const React = require('react') /** Settings namespace + slot key of the default row (see lib/settings.js). */ const NAMESPACE = 'subagent-roles' /** The trim postures the Host accepts, labelled through the locale seat. */ const MODES = [ { value: 'full', key: 'modeFull' }, { value: 'tools', key: 'modeTools' }, { value: 'off', key: 'modeOff' }, ] /** The tool-call budget postures. */ const BUDGET_MODES = [ { value: 'wrap-up', key: 'budgetWrapup' }, { value: 'interrupt', key: 'budgetInterrupt' }, { value: 'off', key: 'budgetOff' }, ] /** * Every settings key this card edits, in render order. * * Declared as DATA because the card must cover the WHOLE namespace: a key the * Host serves but this list forgets would be invisible in the UI while * `settings.yaml` still honoured it. `test/client.test.js` pins this list * against the settings schema exactly so that cannot happen silently. * * `kind` decides the control and the value conversion — `select` keeps a * string, `list` maps text to a string array, `count` maps text to a * non-negative integer (the Host stores a number, not a digit string). * `fallback` answers for a missing or unusable value; the row config always * supplies one through the `base` layer, so it is a last resort. */ const FIELDS = [ { settingsKey: 'childPromptTrim', kind: 'select', options: MODES, fallback: 'full', label: 'mode', hint: 'modeHint' }, { settingsKey: 'childPromptTrimNames', kind: 'list', fallback: [], label: 'names', hint: 'namesHint' }, { settingsKey: 'defaultMaxToolCalls', kind: 'count', fallback: 0, label: 'maxToolCalls', hint: 'maxToolCallsHint' }, { settingsKey: 'maxToolCallsHardCap', kind: 'count', fallback: 0, label: 'hardCap', hint: 'hardCapHint' }, { settingsKey: 'onToolCallBudget', kind: 'select', options: BUDGET_MODES, fallback: 'wrap-up', label: 'budgetMode', hint: 'budgetModeHint' }, { settingsKey: 'graceToolCalls', kind: 'count', fallback: 1, label: 'grace', hint: 'graceHint' }, ] /** The field keys the card writes through, derived so the two cannot drift. */ const FIELD_KEYS = FIELDS.map((field) => field.settingsKey) /** * Card copy, registered as this plugin's locale dictionaries so the card * follows the Language setting like every other DSH surface. `en` doubles as * the fallback for a deployment with no locale service. */ const COPY = { en: { title: 'Subagent roles', description: 'Child prompt trim and the tool-call budget — subagent sessions only; the main agent prompt is never touched.', mode: 'Mode', modeFull: 'full — tool, surface and harness sections', modeTools: 'tools — tool guidance only', modeOff: 'off — disable the trim', modeHint: 'Applies to the next child turn. "tools" keeps the Web GUI, harness-checkout, deliverable, and @-path parts.', names: 'Dropped prompt parts', namesHint: 'Comma-separated names, matched against sections and contexts. Empty keeps every part.', maxToolCalls: 'Default tool-call budget', maxToolCallsHint: 'Used when a role file declares no maxToolCalls. 0 means unlimited; a role file always wins.', hardCap: 'Tool-call hard cap', hardCapHint: 'Ceiling a role file cannot exceed. 0 disables the ceiling, and never clamps an "unlimited" 0.', budgetMode: 'On budget exhaustion', budgetWrapup: 'wrap-up — inject a notice, then allow the grace calls', budgetInterrupt: 'interrupt — stop the child immediately', budgetOff: 'off — warn only, never stop', budgetModeHint: 'Counted in tool CALLS, not turns: parallel calls in one step each count.', grace: 'Grace calls', graceHint: 'Extra calls wrap-up tolerates after its notice. 0 stops on the crossing call itself.', overridden: 'overridden', unsaved: 'unsaved', save: 'Save', saving: 'Saving…', discard: 'Discard', reset: 'Reset all to deployment defaults', resetField: 'Reset', saved: 'saved', saveFailed: 'save failed', unavailable: 'settings are not served by this deployment', expand: 'expand', collapse: 'collapse', }, zh: { title: '子代理角色', description: '子代理提示词裁剪与工具调用预算——只作用于子代理会话,主代理提示词永不改动。', mode: '模式', modeFull: 'full — 工具、界面与 harness 段落一并裁剪', modeTools: 'tools — 只裁剪工具说明', modeOff: 'off — 关闭裁剪', modeHint: '下一个子代理轮次即生效。"tools" 会保留 Web GUI、harness checkout、交付链接与 @路径 这几段。', names: '丢弃的提示词片段', namesHint: '逗号分隔的名字,section 与 context 一并匹配;清空即全部保留。', maxToolCalls: '默认工具调用额度', maxToolCallsHint: '角色文件未声明 maxToolCalls 时使用;0 表示不限。角色文件永远优先。', hardCap: '工具调用硬上限', hardCapHint: '角色文件不得超过的上限;0 表示不设上限,且永不削顶「不限」的 0。', budgetMode: '额度耗尽时', budgetWrapup: 'wrap-up — 先注入收尾通知,再放行 grace 次调用', budgetInterrupt: 'interrupt — 立即中断子代理', budgetOff: 'off — 只告警,不拦截', budgetModeHint: '计量单位是「次」而非「轮」:同一步内并行调用分别计 1。', grace: '宽限调用次数', graceHint: 'wrap-up 注入通知后额外放行的调用次数;0 表示在触发的那一次调用上直接收口。', overridden: '已覆盖', unsaved: '未保存', save: '保存', saving: '保存中…', discard: '放弃', reset: '全部重置为部署默认', resetField: '重置', saved: '已保存', saveFailed: '保存失败', unavailable: '本部署未提供设置服务', expand: '展开', collapse: '收起', }, } /** English fallback for a deployment that serves no locale service. */ const FALLBACK_T = (key) => COPY.en[key] ?? key /** * Card chrome. Every declaration below is copied from the host's own * `PluginCard.module.css` — same rules, same `--dsw-alias-*` tokens — with a * plugin-scoped class prefix, because a client bundle may not import host * values (bundle purity) but must not invent its own palette either. An * earlier revision invented a `label-inverse` alias for the Save button; * no such token exists, so the colour fell back to white over a light * background and the label became invisible. */ const CSS = [ '.dsr-card{border:.5px solid var(--dsw-alias-border-l4);background:var(--dsw-alias-bg-layer-3);border-radius:16px;list-style:none;transition:border-color .16s,background .16s}', '.dsr-card:hover{border-color:var(--dsw-alias-label-dimmed)}', '.dsr-cardOpen{background:var(--dsw-alias-bg-layer-2);border-color:var(--dsw-alias-label-dimmed)}', '.dsr-header{appearance:none;width:100%;font:inherit;color:inherit;text-align:left;cursor:pointer;background:0 0;border:0;border-radius:12px;align-items:center;gap:12px;padding:14px 16px;display:flex}', '.dsr-header:focus-visible{outline:2px solid var(--dsw-alias-brand-primary);outline-offset:-2px}', '.dsr-headText{flex-direction:column;flex:1;gap:4px;min-width:0;display:flex}', '.dsr-name{color:var(--dsw-alias-label-primary);font-size:15px;font-weight:600;line-height:1.4}', '.dsr-description{color:var(--dsw-alias-label-tertiary);font-size:13px;line-height:1.5}', '.dsr-pending{flex:none;border:.5px solid var(--dsw-alias-border-l2);border-radius:6px;padding:1px 8px;font-size:11px;line-height:1.6;color:var(--dsw-alias-label-secondary)}', '.dsr-chevron{color:var(--dsw-alias-label-tertiary);flex:none;transition:transform .16s}', '.dsr-chevronOpen{transform:rotate(180deg)}', '.dsr-body{border-top:.5px solid var(--dsw-alias-border-l2);margin:0 16px;padding-bottom:8px}', '.dsr-field{display:flex;flex-direction:column;gap:5px;padding-top:12px}', '.dsr-labelRow{display:flex;align-items:center;justify-content:space-between;gap:8px}', '.dsr-label{color:var(--dsw-alias-label-secondary);font-size:12px;line-height:1.5}', '.dsr-link{appearance:none;font:inherit;font-size:12px;line-height:1.5;cursor:pointer;border:0;background:0 0;padding:0;color:var(--dsw-alias-link)}', '.dsr-link:disabled{opacity:.4;cursor:default}', '.dsr-overridden{color:var(--dsw-alias-label-tertiary)}', '.dsr-hint{color:var(--dsw-alias-label-tertiary);font-size:12px;line-height:1.5}', '.dsr-input,.dsr-select{appearance:none;width:100%;box-sizing:border-box;font:inherit;font-size:13px;line-height:1.5;color:var(--dsw-alias-label-primary);background:var(--dsw-alias-bg-layer-3);border:1px solid var(--dsw-alias-border-l2);border-radius:8px;padding:5px 10px}', '.dsr-input:focus-visible,.dsr-select:focus-visible{outline:2px solid var(--dsw-alias-brand-primary);outline-offset:1px}', '.dsr-input:disabled,.dsr-select:disabled{opacity:.4;cursor:default}', '.dsr-footer{border-top:.5px solid var(--dsw-alias-border-l2);justify-content:flex-end;align-items:center;gap:8px;padding:12px 0 4px;display:flex}', '.dsr-reset{margin-right:auto}', '.dsr-button,.dsr-save{appearance:none;font:inherit;cursor:pointer;border:1px solid #0000;border-radius:8px;padding:5px 14px;font-size:13px;line-height:1.5}', '.dsr-button{border-color:var(--dsw-alias-border-l2);color:var(--dsw-alias-label-secondary);background:0 0}', '.dsr-button:hover:not(:disabled){color:var(--dsw-alias-label-primary);border-color:var(--dsw-alias-label-dimmed)}', '.dsr-save{background:var(--dsw-alias-label-primary);color:var(--dsw-alias-bg-layer-3)}', '.dsr-button:disabled,.dsr-save:disabled{opacity:.4;cursor:default}', '.dsr-button:focus-visible,.dsr-save:focus-visible{outline:2px solid var(--dsw-alias-brand-primary);outline-offset:1px}', '.dsr-status{min-width:0;flex:1;margin:0;font-size:12px;line-height:1.5;color:var(--dsw-alias-label-tertiary)}', '.dsr-statusFailed{color:var(--dsw-alias-state-error-primary)}', ].join('') /** Install the stylesheet once per page, like the host's own client plugins do. */ function installStyles() { const tagId = 'dsh-plugin-subagent-roles/card.css' if (typeof document === 'undefined') return if (document.querySelector(`style[data-plugin-css=${JSON.stringify(tagId)}]`) !== null) return const tag = document.createElement('style') tag.dataset.plugin = 'dsh-plugin-subagent-roles' tag.dataset.pluginCss = tagId tag.textContent = CSS document.head.appendChild(tag) } /** Comma-joined text for the name-list field. */ function toText(value) { return Array.isArray(value) ? value.join(', ') : '' } /** Split a staged list field back into the array the Host stores. */ function toList(text) { return String(text ?? '') .split(',') .map((entry) => entry.trim()) .filter((entry) => entry.length > 0) } /** The control text for one resolved settings value. */ function fieldText(field, value) { if (field.kind === 'list') return toText(value ?? field.fallback) if (field.kind === 'count') return String(Number.isSafeInteger(value) ? value : field.fallback) return typeof value === 'string' ? value : field.fallback } /** * The settings value one staged text stands for. * * A count is parsed strictly: the Host stores `z.natural()`, so "8.5", "-1", * and "" must not become a number it would refuse. Unusable text folds to the * field's fallback, which keeps Save from ever writing an invalid count. */ function fieldValue(field, text) { if (field.kind === 'list') return toList(text) if (field.kind === 'count') { const trimmed = String(text ?? '').trim() if (!/^\d+$/.test(trimmed)) return field.fallback const parsed = Number.parseInt(trimmed, 10) return Number.isSafeInteger(parsed) ? parsed : field.fallback } return text } /** Whether two staged texts describe the same settings value. */ function sameFieldValue(field, left, right) { if (field.kind === 'list') return sameNames(left, right) if (field.kind === 'count') return fieldValue(field, left) === fieldValue(field, right) return left === right } /** * The writes one Save performs. * * A staged field is CLEARED, not written: clearing re-inherits the * composition layer, while writing today's default would pin it as an * override the deployment can no longer retune. A field whose staged text * still equals the resolved value is NOT written at all — presence in the * user layer is what marks a field overridden, so writing it would shadow a * later deployment change to a field the user never touched. Pure, so the * semantics are unit-testable without a DOM. */ function planSave(staged, shown, current) { const steps = [] for (const field of FIELDS) { if (staged.includes(field.settingsKey)) { steps.push({ kind: 'unset', field: field.settingsKey }) continue } if (sameFieldValue(field, shown[field.settingsKey], current[field.settingsKey])) continue steps.push({ kind: 'set', field: field.settingsKey, value: fieldValue(field, shown[field.settingsKey]) }) } return steps } /** * Apply one edit to the staged state. * * Editing a field also CANCELS a reset staged for it: what the user typed is * what Save must write, and keeping the field staged would silently throw * that edit away in favour of a clear. * @param state - the current `draft` (or undefined), `shown`, and `staged`. * @param field - the field being edited. * @param next - the partial value to merge into the draft. * @returns the next draft and staged list. */ function applyEdit(state, field, next) { return { draft: { ...(state.draft ?? state.shown), ...next }, staged: state.staged.filter((entry) => entry !== field), } } /** Subscribe a component to one settings namespace snapshot. */ function useSettings(scope) { const subscribe = React.useCallback((onChange) => scope.subscribe(onChange), [scope]) const getSnapshot = React.useCallback(() => scope.getSnapshot(), [scope]) return React.useSyncExternalStore(subscribe, getSnapshot) } /** * One staged field: label row (with an inline reset), control, hint. * * The control is rendered by the caller with the id this component mints, so * the `