dsh-embedded-workbench 0.9.0 → 0.9.2

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/src/client.js ADDED
@@ -0,0 +1,269 @@
1
+ /**
2
+ * embedded-workbench — browser half: the gate-injection switch on the dsh Web
3
+ * client's Plugins page.
4
+ *
5
+ * The Plugins page (`@deepseek-ai/dsh-client-ui-plugin-manager`) owns the
6
+ * sidebar **Plugins** entry and declares the slots a bundle's own configuration
7
+ * registers into. This module contributes one `plugins.bundle.config` entry,
8
+ * keyed by this package's npm name, so the switch renders on
9
+ * embedded-workbench's own page between its description and its rows.
10
+ *
11
+ * Why the switch writes through `configForms` rather than reaching for the
12
+ * profile file: dsh's settings service exposes only the Config fields declared
13
+ * `.volatile()`, and it rejects a write to any other path. `enabled` is such a
14
+ * field (see `src/index.ts`), so flipping the switch is an ordinary
15
+ * revision-fenced settings write that the running Host picks up in place — the
16
+ * injection there re-reads the reference on every model step.
17
+ *
18
+ * Shape: this is a prebuilt module-system bundle, not a source module. It calls
19
+ * `window.__ModuleLoader__.load({ id, factory })` with this package's resolved
20
+ * npm name, and `factory` returns the cordis plugin face. Only the client
21
+ * baseline is requested (`react` and
22
+ * `@deepseek-ai/dsh-client-ui-primitives`); every other capability arrives
23
+ * through cordis `inject`. `scripts/build-client.mjs` publishes this file
24
+ * verbatim as `lib/client.js`.
25
+ *
26
+ * @module dsh-embedded-workbench/client
27
+ */
28
+
29
+ window.__ModuleLoader__.load({
30
+ id: 'dsh-embedded-workbench',
31
+ factory: (require) => {
32
+ const React = require('react')
33
+ // The host's Switch and Button, vendored. A plain-JS client bundle must not
34
+ // require a Harness Client package: it changes without notice, we get no type
35
+ // check, and a throw blanks this slot entry. Markup, classes, token usage and
36
+ // the aria behaviour follow the host primitive; classes carry our own prefix.
37
+ const CONTROLS_CSS = [
38
+ '.ew-switch { box-sizing: border-box; position: relative; flex: 0 0 auto; width: 36px; height: 20px; padding: 2px; border: 0; border-radius: 999px; corner-shape: round; background: var(--dsw-alias-border-l3); cursor: pointer; }',
39
+ ".ew-switch[aria-checked='true'] { background: var(--dsw-alias-brand-primary); }",
40
+ '.ew-switch:disabled { cursor: default; opacity: 0.5; }',
41
+ '.ew-switch:focus-visible { outline: var(--dsw-focus-ring-width) solid var(--dsw-focus-ring-color, var(--dsw-alias-state-business-primary)); outline-offset: 2px; }',
42
+ '.ew-switch-thumb { display: block; width: 16px; height: 16px; border-radius: 50%; corner-shape: round; background: var(--dsw-alias-label-primary-foreground); transition: transform 120ms ease; }',
43
+ ".ew-switch[aria-checked='false'] .ew-switch-thumb { background: var(--dsw-alias-switch-thumb); }",
44
+ ".ew-switch[aria-checked='true'] .ew-switch-thumb { transform: translateX(16px); }",
45
+ '.ew-btn { box-sizing: border-box; display: inline-flex; align-items: center; justify-content: center; gap: 4px; border: none; border-radius: var(--dsw-radius-md); cursor: pointer; font-size: 14px; line-height: 22px; color: var(--dsw-alias-label-primary); background: transparent; padding: 0 14px; }',
46
+ '.ew-btn:disabled { cursor: not-allowed; opacity: 0.4; }',
47
+ '.ew-btn-sm { height: 28px; font-size: 12px; line-height: 18px; padding: 0 10px; border-radius: var(--dsw-radius-sm); }',
48
+ '.ew-btn-outline { border: 0.5px solid var(--dsw-alias-border-l3); background: transparent; }',
49
+ '.ew-btn-outline:hover:not(:disabled) { background: var(--dsw-alias-interactive-bg-hover); }',
50
+ ].join('\n')
51
+
52
+ /** Rendered inside the component tree, so unmounting removes the styles. */
53
+ function Controls() {
54
+ return React.createElement('style', null, CONTROLS_CSS)
55
+ }
56
+
57
+ function Switch(props) {
58
+ return React.createElement(
59
+ 'button',
60
+ {
61
+ type: 'button',
62
+ role: 'switch',
63
+ 'aria-checked': props.checked === true,
64
+ 'aria-label': props.label,
65
+ className: 'ew-switch',
66
+ disabled: props.disabled === true,
67
+ onClick: () => {
68
+ if (props.disabled !== true) props.onChange(!(props.checked === true))
69
+ },
70
+ },
71
+ React.createElement('span', { className: 'ew-switch-thumb' }),
72
+ )
73
+ }
74
+
75
+ Switch.displayName = 'dsh-embedded-workbench:Switch'
76
+ Button.displayName = 'dsh-embedded-workbench:Button'
77
+
78
+ function Button(props) {
79
+ const classes = ['ew-btn']
80
+ if (props.size === 'sm') classes.push('ew-btn-sm')
81
+ if (props.variant === 'outline') classes.push('ew-btn-outline')
82
+ return React.createElement(
83
+ 'button',
84
+ { type: 'button', className: classes.join(' '), disabled: props.disabled === true, onClick: props.onClick },
85
+ props.children,
86
+ )
87
+ }
88
+
89
+ /** Settings namespace: the Loader entry id this bundle's patch declares. */
90
+ const NS = 'embedded-workbench'
91
+ /** `plugins.bundle.config` key: the bundle's npm package name. */
92
+ const PACKAGE = 'dsh-embedded-workbench'
93
+ /** This page's dictionary namespace. */
94
+ const LOCALE_NS = 'embedded-workbench.plugins'
95
+ /** The Config field the switch writes inside the namespace's section. */
96
+ const FIELD = 'enabled'
97
+
98
+ /** English copy. */
99
+ const en = {
100
+ title: 'Gate injection',
101
+ label: 'Inject the gate text',
102
+ hint: 'Folds the Plan Verification Gate and the context-budget rule into the first model step of every session. Turning it off leaves all eight skills registered — only the injected text is dropped.',
103
+ overridden: 'Overridden',
104
+ reset: 'Reset to default',
105
+ readOnly: 'This deployment stores settings read-only.',
106
+ unavailable: 'This plugin is not loaded, so it cannot be configured right now.',
107
+ saveFailed: 'The deployment did not accept that value; the switch shows what is stored.',
108
+ }
109
+ /** Simplified Chinese copy. */
110
+ const zh = {
111
+ title: 'Gate 注入',
112
+ label: '注入 gate 文本',
113
+ hint: '把 Plan Verification Gate 与上下文预算规则折进每个会话的第一个模型步。关掉后 8 个技能仍然注册,只是不再注入那段提示文本。',
114
+ overridden: '已覆盖',
115
+ reset: '恢复默认',
116
+ readOnly: '本部署的设置为只读。',
117
+ unavailable: '该插件当前未加载,暂时无法配置。',
118
+ saveFailed: '本部署没有接受这个值,开关显示的是已存下的状态。',
119
+ }
120
+
121
+ /** Required cordis services. */
122
+ const inject = ['slots', 'locale', 'configForms']
123
+
124
+ const GROUP = { display: 'flex', flexDirection: 'column', gap: '8px' }
125
+ const TITLE = { margin: 0, fontSize: '14px', fontWeight: '500', lineHeight: '22px' }
126
+ const ROW = { display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: '16px' }
127
+ const LABEL = { fontSize: '13px', lineHeight: '20px' }
128
+ const NOTE = { margin: 0, fontSize: '12px', lineHeight: '18px', color: 'var(--dsw-alias-label-tertiary)' }
129
+ const FAILED = { margin: 0, fontSize: '12px', lineHeight: '18px', color: 'var(--dsw-alias-state-error-primary)' }
130
+
131
+ /**
132
+ * Whether a settings-layer value carries this field, which is what marks it
133
+ * overridden: an override equal to the default is still an override.
134
+ * @param layer - the raw user layer the form snapshot carries.
135
+ * @returns whether the layer holds the field.
136
+ */
137
+ function carries(layer) {
138
+ return layer !== null && typeof layer === 'object' && Object.prototype.hasOwnProperty.call(layer, FIELD)
139
+ }
140
+
141
+ /**
142
+ * Render the gate-injection switch, or the note saying why it cannot render.
143
+ * @param props - the page's `t` seat, the bound form snapshot hook, and the write actions.
144
+ * @returns the body of this bundle's configuration section.
145
+ */
146
+ function InjectionCard(props) {
147
+ const t = props.t
148
+ const state = props.useInjectionForm((snapshot) => snapshot)
149
+ const [pending, setPending] = React.useState(false)
150
+ const [failed, setFailed] = React.useState(false)
151
+
152
+ /** Run one settings write and report a refusal or a transport failure. */
153
+ const write = (run) => {
154
+ setPending(true)
155
+ setFailed(false)
156
+ Promise.resolve(run()).then(
157
+ (accepted) => {
158
+ setPending(false)
159
+ setFailed(accepted === false)
160
+ },
161
+ () => {
162
+ setPending(false)
163
+ setFailed(true)
164
+ },
165
+ )
166
+ }
167
+
168
+ if (state.status !== 'ready') {
169
+ return React.createElement('p', { style: NOTE }, t('unavailable'))
170
+ }
171
+
172
+ const section = state.value !== null && typeof state.value === 'object' ? state.value : {}
173
+ // The schema default is `true`; only an explicit false means off.
174
+ const checked = section[FIELD] !== false
175
+ const overridden = carries(state.user)
176
+ const locked = state.writable !== true || pending
177
+
178
+ const children = [
179
+ React.createElement(Controls, { key: 'styles' }),
180
+ React.createElement('h4', { key: 'title', style: TITLE }, t('title')),
181
+ React.createElement('div', { key: 'row', style: ROW }, [
182
+ React.createElement('span', { key: 'label', style: LABEL }, t('label')),
183
+ React.createElement(Switch, {
184
+ key: 'switch',
185
+ checked,
186
+ disabled: locked,
187
+ label: t('label'),
188
+ onChange: (next) => write(() => props.setEnabled(next)),
189
+ }),
190
+ ]),
191
+ React.createElement('p', { key: 'hint', style: NOTE }, state.writable === true ? t('hint') : t('readOnly')),
192
+ ]
193
+
194
+ if (overridden) {
195
+ children.push(
196
+ React.createElement('div', { key: 'overridden', style: ROW }, [
197
+ React.createElement('span', { key: 'badge', style: NOTE }, t('overridden')),
198
+ React.createElement(
199
+ Button,
200
+ {
201
+ key: 'reset',
202
+ variant: 'outline',
203
+ size: 'sm',
204
+ disabled: locked,
205
+ onClick: () => write(() => props.resetEnabled()),
206
+ },
207
+ t('reset'),
208
+ ),
209
+ ]),
210
+ )
211
+ }
212
+
213
+ if (failed) {
214
+ children.push(React.createElement('p', { key: 'failed', style: FAILED, role: 'alert' }, t('saveFailed')))
215
+ }
216
+
217
+ return React.createElement('div', { style: GROUP }, children)
218
+ }
219
+
220
+ /**
221
+ * Mount the switch while the Host serves embedded-workbench's settings
222
+ * namespace.
223
+ * @param ctx - the browser plugin context.
224
+ */
225
+ function apply(ctx) {
226
+ ctx.effect(() => ctx.locale.register(LOCALE_NS, { zh, en }), 'dsh-embedded-workbench: dictionaries')
227
+ // `whileServed` is the registration barrier that keeps this page alive only
228
+ // while the Host serves the namespace. Hosts predating it (measured: dsh
229
+ // 0.1.5-rc.3 and 0.1.6-alpha.2) have no live settings field to offer at all,
230
+ // so there is nothing to register — and calling it there would throw during
231
+ // this plugin's own activation, which the Web boot audit then reports as a
232
+ // failed client entry. Degrade to no page instead.
233
+ if (typeof ctx.configForms.whileServed !== 'function') return
234
+ // The page renders the section only for a bundle whose package name is in
235
+ // its configuration ledger, and the ledger follows this registration. The
236
+ // registration in turn waits for the namespace to be served, so a profile
237
+ // whose embedded-workbench row is switched off shows no trace of the
238
+ // switch.
239
+ ctx.effect(
240
+ () =>
241
+ ctx.configForms.whileServed([NS], () => {
242
+ const form = ctx.configForms.get(NS)
243
+ const source = {
244
+ getSnapshot: () => form.getSnapshot(),
245
+ subscribe: (listener) => form.subscribe(listener),
246
+ }
247
+ return ctx.slots.inject('plugins.bundle.config', () =>
248
+ ctx.slots.register(
249
+ {
250
+ name: 'plugins.bundle.config',
251
+ key: PACKAGE,
252
+ locale: LOCALE_NS,
253
+ inject: () => ({
254
+ hooks: { injectionForm: source },
255
+ setEnabled: (next) => form.set(FIELD, next),
256
+ resetEnabled: () => form.unset(FIELD),
257
+ }),
258
+ },
259
+ InjectionCard,
260
+ ),
261
+ )
262
+ }),
263
+ 'dsh-embedded-workbench: gate-injection switch',
264
+ )
265
+ }
266
+
267
+ return { inject, apply }
268
+ },
269
+ })
package/src/index.ts CHANGED
@@ -29,7 +29,7 @@
29
29
  */
30
30
 
31
31
  import { fileURLToPath } from 'node:url'
32
- import type { Context } from '@deepseek-ai/cordis'
32
+ import type { Context, Volatile } from '@deepseek-ai/cordis'
33
33
  import z from '@deepseek-ai/schemastery'
34
34
  import { createUserMessage } from '@deepseek-ai/dsh-llm'
35
35
  import type { ContextFormed } from '@deepseek-ai/dsh-llm'
@@ -75,19 +75,68 @@ Plugin embedded-workbench is active: embedded C/C++ firmware development skills
75
75
  To load the workflows and engineering policies behind these skills: load the embedded-workbench skill.
76
76
  </EXTREMELY_IMPORTANT>`
77
77
 
78
+ /** A schemastery field that may or may not carry `.volatile()`. */
79
+ interface LiveField {
80
+ volatile?: () => unknown
81
+ }
82
+
83
+ /**
84
+ * Declare a field as live where this host's schemastery can — `.volatile()`
85
+ * arrived in 3.18.3 — and leave it an ordinary field where it cannot.
86
+ *
87
+ * The fallback is load-bearing, not defensive padding. `Config` below is built
88
+ * while this module is still being evaluated, so an unconditional `.volatile()`
89
+ * on a host shipping schemastery 3.18.2 (measured: dsh 0.1.5-rc.2 and
90
+ * 0.1.5-rc.3) throws during import; the loader entry then fails and takes the
91
+ * WHOLE plugin tree — and the host's boot — down with it. Degrading costs only
92
+ * the Plugins-page switch, because the settings service projects nothing but
93
+ * fields under a `.volatile()` node; the skills and the gate injection are
94
+ * untouched. The returned schema keeps the plain field's static type; the
95
+ * `Config` interface below carries the union the host actually hands over.
96
+ */
97
+ function live<T>(field: T): T {
98
+ const probe = field as T & LiveField
99
+ return typeof probe.volatile === 'function' ? (probe.volatile() as T) : field
100
+ }
101
+
78
102
  export interface Config {
79
- enabled: boolean
103
+ /**
104
+ * The injection switch the Web client's Plugins page edits live: a `Volatile`
105
+ * reference on a host whose schemastery supports one, an ordinary boolean on a
106
+ * host that predates `.volatile()`. Read it through {@link injectionEnabled},
107
+ * which accepts both shapes.
108
+ */
109
+ enabled: Volatile<boolean> | boolean
80
110
  gateContent: string
81
111
  }
82
112
 
83
113
  export const Config = z.object({
114
+ // Live so the Web Plugins page can flip the gate injection inside a running
115
+ // session: dsh's settings service projects ONLY fields under a `.volatile()`
116
+ // node and rejects writes to every other path. The price is that the injection
117
+ // reads the reference per step instead of deciding once at mount, which is also
118
+ // what lets a toggle take effect without remounting the row.
119
+ //
84
120
  // On by default, but deliberately small: the payload carries the verification
85
121
  // gate and the context-budget rule only, so leaving it on costs a few hundred
86
122
  // tokens once per session rather than the ~900 of the previous payload.
87
- enabled: z.boolean().default(true),
123
+ enabled: live(z.boolean().default(true)),
124
+ // Not volatile, deliberately: the settings projection feeds a GUI form, and a
125
+ // multi-kilobyte text field does not belong in one. Override it in the
126
+ // profile's `cordis.patch.yml` row instead.
88
127
  gateContent: z.string().default(DEFAULT_GATE_CONTENT),
89
128
  })
90
129
 
130
+ /**
131
+ * Read the injection switch as a boolean, whichever shape this host produced.
132
+ * @param config - the resolved plugin configuration.
133
+ * @returns whether the gate may be injected.
134
+ */
135
+ function injectionEnabled(config: Config): boolean {
136
+ const value = config.enabled
137
+ return typeof value === 'boolean' ? value : value.get()
138
+ }
139
+
91
140
  function gateMessage(text: string): UserMessage {
92
141
  return createUserMessage({
93
142
  content: [{ type: 'text', text }],
@@ -158,7 +207,7 @@ function inspectProvider(config: Config): HostCordisInspectProviderRegistration
158
207
  query: async (method) => {
159
208
  if (method === 'status') {
160
209
  return {
161
- enabled: config.enabled,
210
+ enabled: injectionEnabled(config),
162
211
  gateContentLength: config.gateContent.length,
163
212
  }
164
213
  }
@@ -200,7 +249,12 @@ export function apply(ctx: Context, config: Config): void {
200
249
  customSkillDirs: [SKILLS_DIR],
201
250
  })
202
251
  })
203
- if (!config.enabled) return
252
+ // Injection listens unconditionally, including while the switch is off: the
253
+ // switch is volatile, so `apply` runs once and the value behind it can turn on
254
+ // later from the Web Plugins page. Returning early on a false value here would
255
+ // freeze that decision for the lifetime of the mount, and turning the switch
256
+ // back on could never take effect without a profile restart.
257
+ //
204
258
  // Inject the gate once per session on the FIRST model step that runs,
205
259
  // instead of at session-start: session-start injection lands in the agent's
206
260
  // inbox, which a blank-session preset switch (agentPreset.select ->
@@ -216,6 +270,7 @@ export function apply(ctx: Context, config: Config): void {
216
270
  const decision = await next()
217
271
  if (decision.kind === 'reject') return decision
218
272
  registerProvider()
273
+ if (!injectionEnabled(config)) return decision
219
274
  if (gateInHistory(agent.session)) return decision
220
275
  return {
221
276
  kind: 'enter',