@linxin666/dsh-liangshen 0.3.23 → 0.4.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.
Files changed (59) hide show
  1. package/README.i18n.yaml +2 -2
  2. package/README.md +55 -41
  3. package/README.zh.md +54 -40
  4. package/cordis.patch.yml +3 -2
  5. package/icon.svg +3 -0
  6. package/lib/client.js +1380 -29
  7. package/lib/client.js.map +1 -1
  8. package/lib/index.js +616 -367
  9. package/lib/types/announce.test.d.ts +10 -0
  10. package/lib/types/client/LiangShenSettingsCard.d.ts +78 -0
  11. package/lib/types/client/PluginSettingsCard.d.ts +137 -0
  12. package/lib/types/client/index.d.ts +57 -4
  13. package/lib/types/client/lever-controller.d.ts +16 -2
  14. package/lib/types/client/locales.d.ts +40 -1
  15. package/lib/types/client/main-session.d.ts +32 -0
  16. package/lib/types/client/plugin-card-seat.d.ts +103 -0
  17. package/lib/types/client/settings-form.d.ts +197 -0
  18. package/lib/types/composition.d.ts +116 -0
  19. package/lib/types/composition.test.d.ts +7 -0
  20. package/lib/types/core/lever.d.ts +6 -3
  21. package/lib/types/index.d.ts +160 -33
  22. package/lib/types/index.test.d.ts +5 -0
  23. package/package.json +27 -18
  24. package/presets/liangshen/NOTICE +4 -4
  25. package/presets/liangshen/agent.cordis.yml +168 -123
  26. package/presets/liangshen/fact-ledger.mjs +141 -0
  27. package/presets/liangshen/guard.mjs +426 -0
  28. package/presets/liangshen/minimal-prompt.mjs +73 -38
  29. package/presets/liangshen/paging.mjs +232 -0
  30. package/presets/liangshen/tool-activate.mjs +176 -0
  31. package/presets/liangshen/tool-catalog.mjs +615 -219
  32. package/presets/liangshen/working-context.mjs +222 -0
  33. package/src/announce.test.ts +248 -55
  34. package/src/client/LiangShenLever.module.css +2 -0
  35. package/src/client/LiangShenLever.tsx +41 -10
  36. package/src/client/LiangShenSettingsCard.tsx +233 -0
  37. package/src/client/PluginSettingsCard.tsx +555 -0
  38. package/src/client/index.ts +102 -5
  39. package/src/client/lever-controller.ts +76 -9
  40. package/src/client/locales.ts +79 -1
  41. package/src/client/main-session.ts +43 -0
  42. package/src/client/plugin-card-seat.ts +192 -0
  43. package/src/client/settings-card.module.css +418 -0
  44. package/src/client/settings-form.ts +442 -0
  45. package/src/composition.test.ts +294 -0
  46. package/src/composition.ts +554 -0
  47. package/src/core/lever.ts +7 -4
  48. package/src/index.test.ts +68 -1
  49. package/src/index.ts +293 -101
  50. package/tools/benchmark-live-run.mjs +193 -63
  51. package/lib/types/schema.d.ts +0 -18
  52. package/lib/types/schema.test.d.ts +0 -1
  53. package/lib/types/sync.d.ts +0 -47
  54. package/lib/types/sync.test.d.ts +0 -1
  55. package/presets/liangshen/custom-bash.mjs +0 -213
  56. package/src/schema.test.ts +0 -64
  57. package/src/schema.ts +0 -110
  58. package/src/sync.test.ts +0 -224
  59. package/src/sync.ts +0 -222
@@ -0,0 +1,442 @@
1
+ // Generated by scripts/sync-shared.mjs from shared/client/settings/settings-form.ts. Do not edit this copy; edit the shared source and run "node scripts/sync-shared.mjs".
2
+ /**
3
+ * Staged form model behind the plugin settings card. A card stages what the
4
+ * user types and writes it only when they save — the settings write is a
5
+ * durable, revision-fenced document mutation, so staging keeps what is on
6
+ * screen exactly what a save would store. Family-shared slice inlined into
7
+ * each plugin's client bundle; mirrors the official ui-plugin-config
8
+ * card-store pattern.
9
+ */
10
+
11
+ import type { ConfigForm, ConfigFormSnapshot } from '@deepseek-ai/dsh-client-ui-settings/client'
12
+ import type { SnapshotStore } from '@deepseek-ai/dsh-client-store'
13
+ import { createSnapshotStore } from '@deepseek-ai/dsh-client-store'
14
+
15
+ /** The write one field's staged text performs when the card is saved. */
16
+ export type FieldWrite =
17
+ | { kind: 'set'; value: unknown }
18
+ | { kind: 'clear' }
19
+
20
+ /** How one field converts between its stored value and its draft text. */
21
+ export interface FieldSpec {
22
+ /** Field name inside the namespace section. */
23
+ field: string
24
+ /**
25
+ * Whether the Host treats this field as a secret and redacts its value from
26
+ * the read-back (role('secret') in the section schema). Redacted secrets are
27
+ * never compared against the draft on save: the Host strips them from every
28
+ * wire view layer, so the settled snapshot carries nothing to read back. A
29
+ * staged secret set is judged by the mutation settling; the rest of its
30
+ * batch, when one exists, is still judged by read-back, and the atomic
31
+ * mutation lands every write or none.
32
+ */
33
+ secret?: boolean
34
+ /** Render a stored value as draft text; the empty string when the section carries none. */
35
+ format: (value: unknown) => string
36
+ /**
37
+ * The write this draft text stages, or undefined when the text is not a
38
+ * value this field accepts — which blocks the save rather than discarding it.
39
+ */
40
+ parse: (text: string) => FieldWrite | undefined
41
+ }
42
+
43
+ /** One field as the card renders it. */
44
+ export interface FieldState {
45
+ /** Draft text the control renders. */
46
+ text: string
47
+ /** Whether saving would leave a user-layer entry for this field. */
48
+ overridden: boolean
49
+ /** Whether the draft is not a value this field accepts, which blocks saving. */
50
+ invalid: boolean
51
+ }
52
+
53
+ /** Form state every plugin settings card shares. */
54
+ export interface CardShell {
55
+ /** False while the namespace is still loading; the card renders nothing. */
56
+ available: boolean
57
+ /**
58
+ * Whether the namespace is actually served to this client. False when the
59
+ * Host deployment does not expose it (e.g. the owning plugin's settings
60
+ * domain is not mounted): the card renders an explanation
61
+ * instead of its form, so a missing namespace never looks like a missing
62
+ * plugin.
63
+ */
64
+ exposed: boolean
65
+ /** Whether the Host document accepts writes. */
66
+ writable: boolean
67
+ /** Whether the form holds edits that a save would write. */
68
+ dirty: boolean
69
+ /** Whether any staged draft is invalid, which blocks the save. */
70
+ invalid: boolean
71
+ /** Whether a save is crossing the wire. */
72
+ saving: boolean
73
+ /** Whether the last save did not land as staged; cleared by the next edit or save. */
74
+ failed: boolean
75
+ /**
76
+ * The rejection code/message the Host returned for the last failed save,
77
+ * surfaced next to the generic failure text. Undefined while no save has
78
+ * failed (or the failure carried no server reason).
79
+ */
80
+ failedReason?: string
81
+ }
82
+
83
+ /** The write actions the card's slot entry injects. */
84
+ export interface CardActions {
85
+ /** Stage draft text for one field. */
86
+ edit: (field: string, text: string) => void
87
+ /** Stage a clear, so saving lets the field re-inherit the composition layer. */
88
+ resetField: (field: string) => void
89
+ /** Write every staged edit, then re-seed from what the Host accepted. */
90
+ save: () => void
91
+ /** Drop every staged edit. */
92
+ discard: () => void
93
+ }
94
+
95
+ /** One field's staged edit. */
96
+ interface StagedEdit {
97
+ /** Draft text the control renders. */
98
+ text: string
99
+ /** True when this edit clears the field whatever text it shows. */
100
+ clear: boolean
101
+ }
102
+
103
+ /** One staged edit resolved into the write a save performs. */
104
+ interface PlannedWrite {
105
+ /** Field this entry writes. */
106
+ field: string
107
+ /** The durable write this entry performs, inside the save's one atomic mutation. */
108
+ op: BatchedWrite
109
+ /**
110
+ * Read the settled snapshot back and report whether the Host holds this
111
+ * write's effect. Undefined when the draft is not a value the field
112
+ * accepts: there is nothing to write, and the entry blocks the save.
113
+ */
114
+ judge: (() => boolean) | undefined
115
+ }
116
+
117
+ /** One durable write inside the save's atomic form mutation. */
118
+ export interface BatchedWrite {
119
+ /** Field this entry writes. */
120
+ field: string
121
+ /** set stores a value; unset drops the leaf. */
122
+ op: 'set' | 'unset'
123
+ /** Value for op set (absent for unset). */
124
+ value?: unknown
125
+ }
126
+
127
+ /** Constraints a numeric field's accepted drafts must satisfy, mirroring the host schema. */
128
+ export interface NumberConstraints {
129
+ /** The accepted value must be a whole number. */
130
+ integer?: boolean
131
+ /** The accepted value must be at least this. */
132
+ min?: number
133
+ }
134
+
135
+ /** A whole- or decimal-number field. An empty draft clears the field; any other draft that is not a finite number within the constraints blocks the save. */
136
+ export function numberField(field: string, constraints: NumberConstraints = {}): FieldSpec {
137
+ const { integer = false, min } = constraints
138
+ return {
139
+ field,
140
+ format: value => typeof value === 'number' ? String(value) : '',
141
+ parse: (text) => {
142
+ const trimmed = text.trim()
143
+ if (trimmed === '') return { kind: 'clear' }
144
+ const parsed = Number(trimmed)
145
+ if (!Number.isFinite(parsed)) return undefined
146
+ if (integer && !Number.isInteger(parsed)) return undefined
147
+ if (min !== undefined && parsed < min) return undefined
148
+ return { kind: 'set', value: parsed }
149
+ },
150
+ }
151
+ }
152
+
153
+ /** A free-text field. An empty draft clears the field. */
154
+ export function textField(field: string): FieldSpec {
155
+ return {
156
+ field,
157
+ format: value => typeof value === 'string' ? value : '',
158
+ parse: (text) => {
159
+ const trimmed = text.trim()
160
+ return trimmed === '' ? { kind: 'clear' } : { kind: 'set', value: trimmed }
161
+ },
162
+ }
163
+ }
164
+
165
+ /**
166
+ * A free-text field the Host treats as a secret and redacts from the read-back
167
+ * (role('secret') in the section schema). The card still edits it like text,
168
+ * but a save never compares the redacted value back: the staged set is judged
169
+ * by the mutation settling (see {@link FieldSpec.secret}).
170
+ */
171
+ export function secretField(field: string): FieldSpec {
172
+ return { ...textField(field), secret: true }
173
+ }
174
+
175
+ /** A boolean field, edited through true/false draft text. */
176
+ export function booleanField(field: string): FieldSpec {
177
+ return {
178
+ field,
179
+ format: value => typeof value === 'boolean' ? String(value) : '',
180
+ parse: (text) => {
181
+ const trimmed = text.trim()
182
+ if (trimmed === '') return { kind: 'clear' }
183
+ if (trimmed === 'true') return { kind: 'set', value: true }
184
+ if (trimmed === 'false') return { kind: 'set', value: false }
185
+ return undefined
186
+ },
187
+ }
188
+ }
189
+
190
+ /** An enumerated string field; only the listed choices are accepted. An empty draft clears the field. */
191
+ export function choiceField(field: string, choices: readonly string[]): FieldSpec {
192
+ return {
193
+ field,
194
+ format: value => typeof value === 'string' && choices.includes(value) ? value : '',
195
+ parse: (text) => {
196
+ if (text === '') return { kind: 'clear' }
197
+ return choices.includes(text) ? { kind: 'set', value: text } : undefined
198
+ },
199
+ }
200
+ }
201
+
202
+ /**
203
+ * Stages one card's edits over one settings namespace and writes them on save.
204
+ *
205
+ * The Host is the only authority on whether a value was accepted — its
206
+ * validators own the constraints no schema can express — so the outcome is
207
+ * read back from the section rather than predicted here. A save that did not
208
+ * land keeps its drafts, so the user can correct them instead of retyping.
209
+ */
210
+ export class CardForm<T> {
211
+ private readonly specs: Map<string, FieldSpec>
212
+ private readonly staged = new Map<string, StagedEdit>()
213
+ private readonly listeners = new Set<() => void>()
214
+ /** The form subscription installed in the constructor; released by dispose(). */
215
+ private readonly disposeForm: () => void
216
+ private disposed = false
217
+ private saving = false
218
+ private failed = false
219
+ private failedReason: string | undefined
220
+
221
+ /** @param scope - the bound configuration form for this card's namespace. */
222
+ constructor(
223
+ private readonly scope: ConfigForm<T>,
224
+ specs: FieldSpec[],
225
+ ) {
226
+ this.specs = new Map(specs.map(spec => [spec.field, spec]))
227
+ this.disposeForm = scope.subscribe(() => { this.publish() })
228
+ }
229
+
230
+ /**
231
+ * Release the form subscription and every bound store listener. The card
232
+ * must call this on teardown; later calls are no-ops.
233
+ */
234
+ dispose(): void {
235
+ if (this.disposed) return
236
+ this.disposed = true
237
+ this.disposeForm()
238
+ this.listeners.clear()
239
+ }
240
+
241
+ /** Publish a projection of this form, rebuilt whenever the form or a draft changes. */
242
+ bind<S>(project: () => S): SnapshotStore<S> {
243
+ const store = createSnapshotStore(project())
244
+ this.listeners.add(() => { store.set(project()) })
245
+ return store
246
+ }
247
+
248
+ /** Read the card-level state: what the Host serves, and what a save would do. */
249
+ shell(): CardShell {
250
+ const snapshot = this.scope.getSnapshot()
251
+ const plan = this.plan()
252
+ return {
253
+ available: snapshot.status !== 'loading',
254
+ exposed: snapshot.status === 'ready',
255
+ writable: snapshot.writable,
256
+ dirty: plan.length > 0,
257
+ invalid: plan.some(item => item.judge === undefined),
258
+ saving: this.saving,
259
+ failed: this.failed,
260
+ ...this.failedReason === undefined ? {} : { failedReason: this.failedReason },
261
+ }
262
+ }
263
+
264
+ /** Read one field's state from the effective section and its staged draft. */
265
+ field(field: string): FieldState {
266
+ const spec = this.specOf(field)
267
+ const staged = this.staged.get(field)
268
+ if (staged === undefined) {
269
+ return { text: spec.format(this.sectionValue(field)), overridden: this.stored(field), invalid: false }
270
+ }
271
+ const write = staged.clear ? { kind: 'clear' as const } : spec.parse(staged.text)
272
+ return {
273
+ text: staged.text,
274
+ overridden: write?.kind === 'set',
275
+ invalid: write === undefined,
276
+ }
277
+ }
278
+
279
+ /** The actions the card's slot registration injects. */
280
+ actions(): CardActions {
281
+ return {
282
+ edit: (field, text) => { this.stage(field, { text, clear: false }) },
283
+ resetField: (field) => {
284
+ this.stage(field, { text: this.specOf(field).format(this.baseValue(field)), clear: true })
285
+ },
286
+ save: () => { void this.save() },
287
+ discard: () => {
288
+ if (this.staged.size === 0 && !this.failed) return
289
+ this.staged.clear()
290
+ this.failed = false
291
+ this.failedReason = undefined
292
+ this.publish()
293
+ },
294
+ }
295
+ }
296
+
297
+ /**
298
+ * Write every staged edit in one atomic form mutation, then re-seed from
299
+ * what the Host accepted.
300
+ *
301
+ * The whole batch rides one mutate, so cross-field validate hooks
302
+ * (baseURL+model) judge it as a unit: the Host either applies every write
303
+ * or refuses the batch. The form contract answers a refusal or a skipped
304
+ * write with `false` (it recovers with a fresh Host view instead of
305
+ * throwing), so the outcome is judged twice: the answer itself, and then the
306
+ * settled snapshot read back one planned write at a time. One missed write
307
+ * fails the whole save. A transport that rejects instead (the dsh-web bridge
308
+ * controller on a dead connection) reports through the same failure path
309
+ * with its rejection message. A save that did not land keeps its drafts, so
310
+ * the user can correct them instead of retyping.
311
+ * @returns settlement after the mutation and the read-back.
312
+ */
313
+ async save(): Promise<void> {
314
+ const plan = this.plan()
315
+ const valid = plan.filter((item): item is PlannedWrite & { judge: () => boolean } => item.judge !== undefined)
316
+ if (plan.length === 0 || this.saving || valid.length !== plan.length) return
317
+ // Snapshot the staged entries this save writes, so an edit staged while it
318
+ // is in flight (which replaces the same key) survives: only delete the key
319
+ // when the entry is still the one this save started from.
320
+ const pending = new Map<string, StagedEdit | undefined>()
321
+ for (const item of plan) pending.set(item.field, this.staged.get(item.field))
322
+ this.saving = true
323
+ this.failed = false
324
+ this.failedReason = undefined
325
+ this.publish()
326
+ // One atomic namespace mutation: the form contract takes ordered path
327
+ // operations, so the whole staged batch is validated, persisted, and
328
+ // recovered together — either every write lands or none does.
329
+ const ops: Array<{ op: 'set'; path: string[]; value: string | number | boolean } | { op: 'unset'; path: string[] }> = valid.map(item => item.op.op === 'set'
330
+ ? { op: 'set', path: [item.field], value: (item.op as { value: string | number | boolean }).value }
331
+ : { op: 'unset', path: [item.field] })
332
+ let failedReason: string | undefined
333
+ let accepted = false
334
+ try {
335
+ accepted = await this.scope.mutate(ops)
336
+ } catch (error) {
337
+ failedReason = error instanceof Error ? error.message : String(error)
338
+ }
339
+ // `false` is the contract's refusal/skip answer, and the form still
340
+ // resolves after a refusal (it recovers with a fresh view), so the answer
341
+ // alone is not enough: judge every planned write against the settled
342
+ // snapshot as well. The mutation is atomic, so one missed write fails the
343
+ // whole save and keeps the drafts.
344
+ const landed = accepted && failedReason === undefined && valid.every(item => item.judge())
345
+ for (const [field, before] of pending) {
346
+ if (landed && this.staged.get(field) === before) this.staged.delete(field)
347
+ }
348
+ this.saving = false
349
+ this.failed = !landed
350
+ // A refused or unlanded batch carries no server reason: the card surfaces
351
+ // its generic failure copy; a rejecting transport (the bridge) adds its
352
+ // message.
353
+ this.failedReason = failedReason
354
+ this.publish()
355
+ }
356
+
357
+ /**
358
+ * Every staged edit a save would write. An entry whose draft is not a value
359
+ * its field accepts carries no write: the form is still dirty, and the save
360
+ * refuses rather than dropping the edit. A staged edit that matches the
361
+ * effective section is not a write at all.
362
+ * @returns the planned writes, in the order the fields were staged.
363
+ */
364
+ private plan(): PlannedWrite[] {
365
+ const plan: PlannedWrite[] = []
366
+ for (const [field, staged] of this.staged) {
367
+ const spec = this.specOf(field)
368
+ if (staged.clear) {
369
+ if (this.stored(field)) plan.push({ field, op: { field, op: 'unset' }, judge: () => this.landedUnset(field) })
370
+ continue
371
+ }
372
+ if (staged.text === spec.format(this.sectionValue(field))) continue
373
+ const write = spec.parse(staged.text)
374
+ if (write === undefined) plan.push({ field, op: { field, op: 'unset' }, judge: undefined })
375
+ else if (write.kind === 'clear') plan.push({ field, op: { field, op: 'unset' }, judge: () => this.landedUnset(field) })
376
+ else plan.push({ field, op: { field, op: 'set', value: write.value }, judge: () => this.landedSet(field, write.value) })
377
+ }
378
+ return plan
379
+ }
380
+
381
+ /**
382
+ * Read-back judgment for a planned set: the user layer must hold the
383
+ * intended value once the mutation has settled.
384
+ */
385
+ private landedSet(field: string, value: unknown): boolean {
386
+ // A redacted secret never appears in any wire view layer: the Host strips
387
+ // role('secret') fields and reports them through a sidecar the form
388
+ // snapshot does not expose, so there is nothing to compare the draft
389
+ // against. Settling is the only signal the form has; the rest of the
390
+ // batch, when one exists, still carries the atomic verdict by read-back.
391
+ if (this.specOf(field).secret) return true
392
+ return this.userLayer()?.[field] === value
393
+ }
394
+
395
+ /**
396
+ * Read-back judgment for a planned unset: the field must be gone from the
397
+ * user layer once the mutation has settled.
398
+ */
399
+ private landedUnset(field: string): boolean {
400
+ return !this.stored(field)
401
+ }
402
+
403
+ private stage(field: string, edit: StagedEdit): void {
404
+ this.staged.set(field, edit)
405
+ this.failed = false
406
+ this.failedReason = undefined
407
+ this.publish()
408
+ }
409
+
410
+ private specOf(field: string): FieldSpec {
411
+ const spec = this.specs.get(field)
412
+ // Every call site names a field this card declared; a missing one is a
413
+ // wiring mistake that must not degrade into a silently inert control.
414
+ if (spec === undefined) throw new Error(`settings card has no field ${field}`)
415
+ return spec
416
+ }
417
+
418
+ private snapshotOf(): ConfigFormSnapshot<T> {
419
+ return this.scope.getSnapshot()
420
+ }
421
+
422
+ private sectionValue(field: string): unknown {
423
+ return (this.snapshotOf().value as Record<string, unknown> | undefined)?.[field]
424
+ }
425
+
426
+ private baseValue(field: string): unknown {
427
+ return (this.snapshotOf().base as Record<string, unknown> | undefined)?.[field]
428
+ }
429
+
430
+ private userLayer(): Record<string, unknown> | undefined {
431
+ return this.snapshotOf().user as Record<string, unknown> | undefined
432
+ }
433
+
434
+ private stored(field: string): boolean {
435
+ const user = this.userLayer()
436
+ return user !== undefined && Object.hasOwn(user, field)
437
+ }
438
+
439
+ private publish(): void {
440
+ for (const listener of this.listeners) listener()
441
+ }
442
+ }