@linxin666/dsh-liangshen 0.3.22 → 0.3.24

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 (37) hide show
  1. package/README.i18n.yaml +2 -2
  2. package/README.md +45 -38
  3. package/README.zh.md +44 -37
  4. package/lib/client.js +1058 -11
  5. package/lib/client.js.map +1 -1
  6. package/lib/index.js +105 -22
  7. package/lib/types/client/LiangShenSettingsCard.d.ts +61 -0
  8. package/lib/types/client/PluginSettingsCard.d.ts +133 -0
  9. package/lib/types/client/index.d.ts +34 -0
  10. package/lib/types/client/locales.d.ts +27 -1
  11. package/lib/types/client/plugin-card-seat.d.ts +96 -0
  12. package/lib/types/client/settings-form.d.ts +197 -0
  13. package/lib/types/core/lever.d.ts +6 -3
  14. package/lib/types/index.d.ts +23 -11
  15. package/lib/types/sync.d.ts +29 -2
  16. package/package.json +6 -2
  17. package/presets/liangshen/NOTICE +4 -4
  18. package/presets/liangshen/agent.cordis.yml +130 -119
  19. package/presets/liangshen/minimal-prompt.mjs +71 -36
  20. package/presets/liangshen/paging.mjs +232 -0
  21. package/presets/liangshen/tool-activate.mjs +176 -0
  22. package/presets/liangshen/tool-catalog.mjs +610 -218
  23. package/presets/liangshen/working-context.mjs +214 -0
  24. package/src/client/LiangShenLever.tsx +4 -1
  25. package/src/client/LiangShenSettingsCard.tsx +149 -0
  26. package/src/client/PluginSettingsCard.tsx +550 -0
  27. package/src/client/index.ts +60 -1
  28. package/src/client/locales.ts +53 -1
  29. package/src/client/plugin-card-seat.ts +185 -0
  30. package/src/client/settings-card.module.css +418 -0
  31. package/src/client/settings-form.ts +439 -0
  32. package/src/core/lever.ts +7 -4
  33. package/src/index.ts +35 -14
  34. package/src/sync.test.ts +86 -7
  35. package/src/sync.ts +108 -6
  36. package/tools/benchmark-live-run.mjs +24 -33
  37. package/presets/liangshen/custom-bash.mjs +0 -213
@@ -0,0 +1,214 @@
1
+ /**
2
+ * working-context — the LiangShen preset's minimal recency projection: one
3
+ * durable user message carrying a single `[Working Context: ...]` line of the
4
+ * session's objective, machine-readable state, refreshed only when it changes.
5
+ *
6
+ * Every field is folded from the durable session event stream, so the line
7
+ * survives resume, reload, and compaction without any process-memory state,
8
+ * and every field is omitted when the log has nothing objective to say:
9
+ *
10
+ * - `plan mode: on|off` — from the session's `plan/mode` events (the last one
11
+ * wins, as the host's own plan projection folds them). Omitted when the log
12
+ * holds no `plan/mode` event at all: the ambient default is off, and the
13
+ * planMode service itself sits behind an isolate realm this preset row must
14
+ * not depend on.
15
+ * - `active namespaces: ...` — the paged tool namespaces currently on the
16
+ * wire, replayed from `tool_activate` calls exactly the way the wire
17
+ * partition in tool-catalog replays them.
18
+ * - `in progress: ...` — the in-progress todo titles from the latest
19
+ * `todo/write` event, cleared by the next `turn/start`, mirroring the host's
20
+ * todos projection.
21
+ *
22
+ * When no field has anything to say the step is left untouched. The injection
23
+ * and dedup discipline mirrors tool-catalog's pre-step: the message is
24
+ * persistent, carries the plugin source shape, and a replacement is published
25
+ * only when the rendered line changed or the published copy left the visible
26
+ * surface.
27
+ */
28
+
29
+ import {
30
+ DEFAULT_MAX_ACTIVE_NAMESPACES,
31
+ DEFAULT_PAGED_TOOL_PATTERNS,
32
+ integerAtLeast,
33
+ replayActivations,
34
+ validatePagedToolPatterns,
35
+ } from './paging.mjs'
36
+
37
+ /** Cordis plugin name used by loader diagnostics. */
38
+ export const name = 'liangshen-working-context'
39
+
40
+ /** Cap one rendered todo title so the line stays a single glance. */
41
+ const MAX_TITLE_CHARS = 80
42
+
43
+ /** At most this many in-progress titles render; the rest collapse to a count. */
44
+ const MAX_TITLES = 3
45
+
46
+ /** Session events, tolerating both snapshotEvents() and events array. */
47
+ function sessionEvents(session) {
48
+ if (Array.isArray(session?.events)) return session.events
49
+ if (typeof session?.snapshotEvents === 'function') return session.snapshotEvents()
50
+ return []
51
+ }
52
+
53
+ /** The latest plan/mode state in the log: seen flag plus active value. */
54
+ export function planModeState(events) {
55
+ let seen = false
56
+ let active = false
57
+ for (const event of Array.isArray(events) ? events : []) {
58
+ if (event?.type !== 'plan/mode' || typeof event.data?.active !== 'boolean') continue
59
+ seen = true
60
+ active = event.data.active
61
+ }
62
+ return { seen, active }
63
+ }
64
+
65
+ /**
66
+ * The in-progress todo titles as the host's todos projection folds them: the
67
+ * latest todo/write list, cleared by the next turn/start.
68
+ */
69
+ export function inProgressTodos(events) {
70
+ let todos
71
+ for (const event of Array.isArray(events) ? events : []) {
72
+ if (event?.type === 'turn/start') {
73
+ todos = undefined
74
+ continue
75
+ }
76
+ if (event?.type === 'todo/write' && Array.isArray(event.data?.todos)) {
77
+ todos = event.data.todos
78
+ }
79
+ }
80
+ return (todos ?? [])
81
+ .filter(item => item?.status === 'in_progress' && typeof item?.content === 'string' && item.content.trim() !== '')
82
+ .map(item => item.content.trim())
83
+ }
84
+
85
+ /** Clip one title to the per-title budget at a word-ish boundary. */
86
+ function clipTitle(title) {
87
+ if (title.length <= MAX_TITLE_CHARS) return title
88
+ return `${title.slice(0, MAX_TITLE_CHARS - 3)}...`
89
+ }
90
+
91
+ /**
92
+ * Render the working-context line for one session's events, or undefined when
93
+ * no field has anything objective to say.
94
+ */
95
+ export function renderWorkingContext(events, options) {
96
+ const patterns = options?.pagedToolPatterns ?? DEFAULT_PAGED_TOOL_PATTERNS
97
+ const maxActive = options?.maxActiveNamespaces ?? DEFAULT_MAX_ACTIVE_NAMESPACES
98
+ const fields = []
99
+
100
+ const plan = planModeState(events)
101
+ if (plan.seen) fields.push(`plan mode: ${plan.active ? 'on' : 'off'}`)
102
+
103
+ const { active } = replayActivations(events, maxActive, patterns)
104
+ if (active.length > 0) fields.push(`active namespaces: ${[...active].sort().join(', ')}`)
105
+
106
+ const titles = inProgressTodos(events)
107
+ if (titles.length > 0) {
108
+ const shown = titles.slice(0, MAX_TITLES).map(clipTitle)
109
+ const rest = titles.length - shown.length
110
+ fields.push(`in progress: ${shown.join('; ')}${rest > 0 ? `; +${rest} more` : ''}`)
111
+ }
112
+
113
+ return fields.length === 0 ? undefined : `[Working Context: ${fields.join(' | ')}]`
114
+ }
115
+
116
+ /** The text one message contributes, joined across its text blocks. */
117
+ function textOf(message) {
118
+ const blocks = Array.isArray(message?.content) ? message.content : []
119
+ return blocks
120
+ .filter(block => block?.type === 'text' && typeof block.text === 'string')
121
+ .map(block => block.text)
122
+ .join('\n')
123
+ }
124
+
125
+ /** Whether one message is this plugin's working-context line. */
126
+ function isContextMessage(message) {
127
+ const source = message?.source
128
+ return source?.kind === 'plugin' && source?.plugin === name
129
+ }
130
+
131
+ /** Visible surface positions, or undefined when the session exposes none. */
132
+ function visibleSeqSet(session) {
133
+ const nodes = session?.surface?.nodes
134
+ return Array.isArray(nodes) ? new Set(nodes) : undefined
135
+ }
136
+
137
+ /**
138
+ * Published working-context state read back from the durable log: the latest
139
+ * visible line's text, or just the published flag when every copy was shadowed.
140
+ */
141
+ function contextHistory(agent) {
142
+ const session = agent?.session
143
+ const events = sessionEvents(session)
144
+ const visible = visibleSeqSet(session)
145
+ let published = false
146
+ for (let index = events.length - 1; index >= 0; index -= 1) {
147
+ const event = events[index]
148
+ if (event?.type !== 'user/message' || !isContextMessage(event.data)) continue
149
+ published = true
150
+ if (visible === undefined || typeof event.seq !== 'number' || visible.has(event.seq)) {
151
+ return { published, text: textOf(event.data) }
152
+ }
153
+ }
154
+ return { published }
155
+ }
156
+
157
+ /** Build the durable working-context message for one rendered line. */
158
+ export function createContextMessage(line) {
159
+ return {
160
+ id: globalThis.crypto.randomUUID(),
161
+ role: 'user',
162
+ content: [{ type: 'text', text: line }],
163
+ source: { kind: 'plugin', plugin: name },
164
+ }
165
+ }
166
+
167
+ /** Register the per-step working-context injection. */
168
+ export function apply(ctx, config) {
169
+ const pagedToolPatterns = validatePagedToolPatterns(name, config?.pagedToolPatterns)
170
+ const maxActiveNamespaces = integerAtLeast(
171
+ name,
172
+ config?.maxActiveNamespaces,
173
+ 'maxActiveNamespaces',
174
+ 1,
175
+ DEFAULT_MAX_ACTIVE_NAMESPACES,
176
+ )
177
+
178
+ ctx.on('agent/pre-step', async (payload, next) => {
179
+ const decision = await next()
180
+ if (decision.kind !== 'enter') return decision
181
+ const agent = payload?.agent
182
+ if (agent?.session === undefined) return decision
183
+
184
+ const line = renderWorkingContext(sessionEvents(agent.session), {
185
+ pagedToolPatterns,
186
+ maxActiveNamespaces,
187
+ })
188
+
189
+ const history = contextHistory(agent)
190
+ const existing = decision.messages.find(message => isContextMessage(message))
191
+
192
+ // Nothing objective to say: inject nothing. A stale published line is left
193
+ // for compaction to age out rather than replaced with an empty marker.
194
+ if (line === undefined) {
195
+ if (existing === undefined) return decision
196
+ return { ...decision, messages: decision.messages.filter(message => message.id !== existing.id) }
197
+ }
198
+
199
+ if (history.text === line) {
200
+ if (existing === undefined) return decision
201
+ return { ...decision, messages: decision.messages.filter(message => message.id !== existing.id) }
202
+ }
203
+ if (existing !== undefined && textOf(existing) === line) return decision
204
+
205
+ const message = createContextMessage(line)
206
+ if (existing === undefined) {
207
+ return { ...decision, messages: [...decision.messages, message] }
208
+ }
209
+ return {
210
+ ...decision,
211
+ messages: decision.messages.map(item => (item.id === existing.id ? message : item)),
212
+ }
213
+ }, { prepend: true })
214
+ }
@@ -65,7 +65,10 @@ export function LiangShenLever(face: LeverFace): ReactElement | null {
65
65
  // blank-session window: a started session reports `locked`, a deployment
66
66
  // without the preset reports `missing`, and both mean the row renders
67
67
  // nothing instead of a dead control in the composer of a running session.
68
- if (state === 'locked' || state === 'missing') return null
68
+ // An `on` lever with an empty restore label has nothing to return to either
69
+ // (the roster supplies no other usable preset), so a push there would be the
70
+ // same dead control: render nothing rather than a switch that eats gestures.
71
+ if (state === 'locked' || state === 'missing' || (state === 'on' && restoreLabel === '')) return null
69
72
 
70
73
  const toggle = (): void => {
71
74
  if (!actionable) return
@@ -0,0 +1,149 @@
1
+ /**
2
+ * LiangShen settings card: availability and the wire presentation. Registers
3
+ * into the `web-ui.plugin.item` child slot the Web UI plugin group renders,
4
+ * bound to the `dsh-liangshen` namespace.
5
+ *
6
+ * The presentation field does not act on this client half: the Host writes it
7
+ * into the synced preset composition, so a session reads it from its preset.
8
+ * This card is the operator's only handle on it, which is why every field the
9
+ * Host schema carries appears here.
10
+ */
11
+
12
+ import type { InjectFace, PropsLocale, PropsRuntime } from '@deepseek-ai/dsh-client-ui-slots'
13
+ import type { SnapshotStore } from '@deepseek-ai/dsh-client-store'
14
+ import type { SettingsScope } from '@deepseek-ai/dsh-client-ui-settings/client'
15
+ import { BooleanField, ChoiceField, PluginSettingsCard } from './PluginSettingsCard.tsx'
16
+ import { CardForm, booleanField, choiceField, type CardActions, type CardShell, type FieldState as CardFieldState } from './settings-form.ts'
17
+
18
+ /** Wire presentations the tool catalog accepts (mirrors the Host schema). */
19
+ export const PRESENTATION_CHOICES = ['ptc', 'native', 'both'] as const
20
+
21
+ /** The LiangShen fields this card edits (the namespace's full schema). */
22
+ export interface LiangShenSettings {
23
+ /** Master switch for the plugin. */
24
+ enabled?: boolean
25
+ /** Whether the plugin announces itself in every agent's system prompt. */
26
+ announceToAgent?: boolean
27
+ /** Wire presentation written into the synced preset. */
28
+ presentation?: string
29
+ }
30
+
31
+ /** What the LiangShen card renders. */
32
+ export interface LiangShenSettingsCardState extends CardShell {
33
+ enabled: CardFieldState
34
+ announceToAgent: CardFieldState
35
+ presentation: CardFieldState
36
+ }
37
+
38
+ /** The registration-side face the card's slot entry injects. */
39
+ export interface LiangShenSettingsCardFace extends CardActions {
40
+ hooks: {
41
+ /** Card snapshot bound by the renderer as useLiangShenSettingsCard. */
42
+ liangShenSettingsCard: SnapshotStore<LiangShenSettingsCardState>
43
+ }
44
+ }
45
+
46
+ /** Bridges the `dsh-liangshen` scope onto the card's staged form. */
47
+ export class LiangShenSettingsCardController {
48
+ private readonly form: CardForm<LiangShenSettings>
49
+ private readonly store: SnapshotStore<LiangShenSettingsCardState>
50
+
51
+ /** @param scope - the bound settings scope for the `dsh-liangshen` namespace. */
52
+ constructor(scope: SettingsScope<LiangShenSettings>) {
53
+ this.form = new CardForm(scope, [
54
+ booleanField('enabled'),
55
+ booleanField('announceToAgent'),
56
+ choiceField('presentation', PRESENTATION_CHOICES),
57
+ ])
58
+ this.store = this.form.bind(() => this.projection())
59
+ }
60
+
61
+ private projection(): LiangShenSettingsCardState {
62
+ return {
63
+ ...this.form.shell(),
64
+ enabled: this.form.field('enabled'),
65
+ announceToAgent: this.form.field('announceToAgent'),
66
+ presentation: this.form.field('presentation'),
67
+ }
68
+ }
69
+
70
+ /**
71
+ * Build the face the card's slot registration injects.
72
+ * @returns the card's snapshot and its form actions.
73
+ */
74
+ inject(): LiangShenSettingsCardFace {
75
+ return { hooks: { liangShenSettingsCard: this.store }, ...this.form.actions() }
76
+ }
77
+
78
+ /** Release the card's scope subscription and bound stores. */
79
+ dispose(): void {
80
+ this.form.dispose()
81
+ }
82
+ }
83
+
84
+ /** Props the renderer binds for the LiangShen card. */
85
+ export type LiangShenSettingsCardProps =
86
+ PropsRuntime<'web-ui.plugin.item'>
87
+ & PropsLocale<'liangshen'>
88
+ & InjectFace<LiangShenSettingsCardFace>
89
+
90
+ /**
91
+ * Render the LiangShen card.
92
+ * @param props - locale copy, the card snapshot, and its form actions.
93
+ * @returns the card.
94
+ */
95
+ export function LiangShenSettingsCard(props: LiangShenSettingsCardProps) {
96
+ const { t } = props
97
+ const state = props.useLiangShenSettingsCard((snapshot: LiangShenSettingsCardState) => snapshot)
98
+ const fieldProps = {
99
+ overriddenLabel: t('settings.overridden'),
100
+ resetLabel: t('settings.reset'),
101
+ invalidLabel: t('settings.invalidValue'),
102
+ disabled: !state.writable,
103
+ inheritLabel: t('settings.inherit'),
104
+ }
105
+ return (
106
+ <PluginSettingsCard
107
+ t={t}
108
+ titleKey="settings.title"
109
+ descriptionKey="settings.description"
110
+ defaultOpen={false}
111
+ state={state}
112
+ onSave={props.save}
113
+ onDiscard={props.discard}
114
+ >
115
+ <BooleanField
116
+ id="settings-liangshen-enabled"
117
+ label={t('settings.enabled')}
118
+ hint={t('settings.enabledHint')}
119
+ onLabel={t('settings.on')}
120
+ offLabel={t('settings.off')}
121
+ {...fieldProps}
122
+ {...state.enabled}
123
+ onEdit={(text) => { props.edit('enabled', text) }}
124
+ onReset={() => { props.resetField('enabled') }}
125
+ />
126
+ <BooleanField
127
+ id="settings-liangshen-announce"
128
+ label={t('settings.announceToAgent')}
129
+ hint={t('settings.announceToAgentHint')}
130
+ onLabel={t('settings.on')}
131
+ offLabel={t('settings.off')}
132
+ {...fieldProps}
133
+ {...state.announceToAgent}
134
+ onEdit={(text) => { props.edit('announceToAgent', text) }}
135
+ onReset={() => { props.resetField('announceToAgent') }}
136
+ />
137
+ <ChoiceField
138
+ id="settings-liangshen-presentation"
139
+ label={t('settings.presentation')}
140
+ hint={t('settings.presentationHint')}
141
+ choices={PRESENTATION_CHOICES.map(choice => ({ value: choice, label: t(`presentation.${choice}`) }))}
142
+ {...fieldProps}
143
+ {...state.presentation}
144
+ onEdit={(text) => { props.edit('presentation', text) }}
145
+ onReset={() => { props.resetField('presentation') }}
146
+ />
147
+ </PluginSettingsCard>
148
+ )
149
+ }