@linxin666/dsh-liangshen 0.4.10 → 0.4.12

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.
@@ -33,6 +33,7 @@ import {
33
33
  replayActivations,
34
34
  validatePagedToolPatterns,
35
35
  } from './paging.mjs'
36
+ import { pagedNamespacesFor } from './tool-catalog.mjs'
36
37
 
37
38
  /** Cordis plugin name used by loader diagnostics. */
38
39
  export const name = 'liangshen-tool-activate'
@@ -79,6 +80,45 @@ export function apply(ctx, config) {
79
80
  return []
80
81
  }
81
82
 
83
+ /**
84
+ * The paged namespaces this scope carries, keyed by namespace to the sorted
85
+ * names of its tools.
86
+ *
87
+ * The catalog publishes the pre-restriction partition of every scope it
88
+ * pages, and reading it back is the only faithful source: the catalog's live
89
+ * `tools.restrict` filter removes the paged names from every scoped reader,
90
+ * so recomputing from `tools.schemas(agent)` finds an empty page and refuses
91
+ * activations the same request's catalog advertises. The schema read stays as
92
+ * the fallback for a scope nothing pages — a native presentation, a
93
+ * deployment without the catalog row, or a test stub.
94
+ * @param agent - agent scope the call acts for.
95
+ * @returns namespace to tool-name map, empty when nothing is paged.
96
+ */
97
+ const pagedToolsOf = (agent) => {
98
+ const paged = new Map()
99
+ const published = pagedNamespacesFor(agent)
100
+ if (published !== undefined && published.size > 0) {
101
+ for (const [namespace, group] of published) {
102
+ paged.set(namespace, group.map(tool => tool?.name).filter(toolName => typeof toolName === 'string').sort())
103
+ }
104
+ return paged
105
+ }
106
+ for (const schema of schemasFor(agent)) {
107
+ const toolName = schema?.name
108
+ if (typeof toolName !== 'string' || toolName === 'tool_activate') continue
109
+ const owner = namespaceOf(toolName, pagedToolPatterns)
110
+ if (owner === undefined) continue
111
+ let group = paged.get(owner)
112
+ if (group === undefined) {
113
+ group = []
114
+ paged.set(owner, group)
115
+ }
116
+ group.push(toolName)
117
+ }
118
+ for (const group of paged.values()) group.sort()
119
+ return paged
120
+ }
121
+
82
122
  ctx.tools.register({
83
123
  name: 'tool_activate',
84
124
  description: [
@@ -120,24 +160,10 @@ export function apply(ctx, config) {
120
160
  async execute(args, exec) {
121
161
  const namespace = args.namespace
122
162
  const agent = exec?.agent
123
- const schemas = schemasFor(agent)
124
163
 
125
164
  // The paged namespaces the session actually carries, with their tools in
126
165
  // stable name order for deterministic reporting.
127
- const paged = new Map()
128
- for (const schema of schemas) {
129
- const toolName = schema?.name
130
- if (typeof toolName !== 'string' || toolName === 'tool_activate') continue
131
- const owner = namespaceOf(toolName, pagedToolPatterns)
132
- if (owner === undefined) continue
133
- let group = paged.get(owner)
134
- if (group === undefined) {
135
- group = []
136
- paged.set(owner, group)
137
- }
138
- group.push(toolName)
139
- }
140
- for (const group of paged.values()) group.sort()
166
+ const paged = pagedToolsOf(agent)
141
167
 
142
168
  if (!paged.has(namespace)) {
143
169
  const available = [...paged.keys()].sort()
@@ -96,6 +96,54 @@ export const name = 'liangshen-tool-catalog'
96
96
  /** Prompt assembly must exist before the wire catalog can be observed. */
97
97
  export const inject = ['systemPrompt']
98
98
 
99
+ /**
100
+ * The paged namespaces each agent scope carries, published by the paging sync
101
+ * below for the sibling `tool-activate` row.
102
+ *
103
+ * The activation tool cannot recompute this itself: the live restriction the
104
+ * catalog installs is what removes the paged names from every scoped reader, so
105
+ * `tools.schemas(agent)` inside the activation tool already lacks exactly the
106
+ * names it must offer. Publishing the pre-restriction partition keeps one fact
107
+ * behind both the catalog message and the tool that loads a namespace back.
108
+ * Keyed by agent, and by the agent's session for a caller that reaches the same
109
+ * session through another scope object; both are weak, so a departed scope
110
+ * leaves nothing behind.
111
+ */
112
+ const pagedSurfaceByAgent = new WeakMap()
113
+ const pagedSurfaceBySession = new WeakMap()
114
+
115
+ /**
116
+ * Publish (or, with `undefined`, retract) one scope's paged namespaces.
117
+ * @param agent - agent scope the partition belongs to.
118
+ * @param surface - namespace-keyed namespaces, as `partitionWireTools` returns.
119
+ */
120
+ function publishPagedSurface(agent, surface) {
121
+ if (agent === undefined || agent === null) return
122
+ const session = agent.session
123
+ if (surface === undefined) {
124
+ pagedSurfaceByAgent.delete(agent)
125
+ if (session !== undefined && session !== null) pagedSurfaceBySession.delete(session)
126
+ return
127
+ }
128
+ pagedSurfaceByAgent.set(agent, surface)
129
+ if (session !== undefined && session !== null) pagedSurfaceBySession.set(session, surface)
130
+ }
131
+
132
+ /**
133
+ * The paged namespaces one agent scope carries, keyed by namespace to that
134
+ * namespace's tool schemas.
135
+ * @param agent - agent scope the caller acts for.
136
+ * @returns the published partition, or undefined when nothing paged this scope.
137
+ */
138
+ export function pagedNamespacesFor(agent) {
139
+ if (agent === undefined || agent === null) return undefined
140
+ const byAgent = pagedSurfaceByAgent.get(agent)
141
+ if (byAgent !== undefined) return byAgent
142
+ const session = agent.session
143
+ if (session === undefined || session === null) return undefined
144
+ return pagedSurfaceBySession.get(session)
145
+ }
146
+
99
147
  /** Default cap for one tool's one-line summary in the injected compact list. */
100
148
  const DEFAULT_DESCRIPTION_MAX_LENGTH = 200
101
149
 
@@ -705,6 +753,10 @@ export function apply(ctx, config) {
705
753
  */
706
754
  const syncScopePaging = (agent) => {
707
755
  clearScopePaging(agent)
756
+ // Retract the earlier publication first: every early return below means this
757
+ // scope is not paged, and a stale page would keep advertising namespaces the
758
+ // activation tool can no longer reach.
759
+ publishPagedSurface(agent, undefined)
708
760
 
709
761
  const tools = agent?.ctx?.tools
710
762
  if (tools === undefined || typeof tools.restrict !== 'function') {
@@ -719,6 +771,10 @@ export function apply(ctx, config) {
719
771
  // assembly-path emergency that warning describes.
720
772
  const fullSurface = quietSchemas(agent) ?? []
721
773
  if (fullSurface.length === 0) return undefined
774
+ // Publish before the withhold decision: with every namespace already active
775
+ // there is nothing to restrict, and the activation tool must still know the
776
+ // namespace exists to report it as active instead of unknown.
777
+ publishPagedSurface(agent, inactiveNamespaces(fullSurface, pagedToolPatterns, []))
722
778
  const active = replayActivations(sessionEvents(agent?.session), maxActiveNamespaces, pagedToolPatterns).active
723
779
  const withheld = withheldToolNames(fullSurface, pagedToolPatterns, active)
724
780
  if (withheld.length === 0) return undefined
@@ -149,6 +149,7 @@ export function LiangShenSettingsCard(props: LiangShenSettingsCardProps) {
149
149
  label={t('settings.presentation')}
150
150
  hint={t('settings.presentationHint')}
151
151
  choices={PRESENTATION_CHOICES.map(choice => ({ value: choice, label: t(`presentation.${choice}`) }))}
152
+ presentation="segment"
152
153
  {...fieldProps}
153
154
  {...state.presentation}
154
155
  onEdit={(text) => { props.edit('presentation', text) }}