dsh-skill-pool 0.0.0-stage → 0.3.7
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/CONTRACTS-v3.md +288 -0
- package/LICENSE +21 -0
- package/README.md +198 -2
- package/client.js +4434 -0
- package/cordis.patch.yml +6 -0
- package/icon.svg +21 -0
- package/index.js +174 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +62 -4
- package/src/activation.js +303 -0
- package/src/inventory.js +447 -0
- package/src/library.js +1168 -0
- package/src/mount.js +73 -0
- package/src/provider.js +309 -0
- package/src/remount.js +78 -0
- package/src/rules.js +243 -0
- package/src/skill-content.js +78 -0
- package/src/store.js +765 -0
- package/src/uploads.js +222 -0
- package/src/web-api.js +1037 -0
package/cordis.patch.yml
ADDED
package/icon.svg
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="64" height="64" role="img" aria-label="Resource Pool">
|
|
2
|
+
<defs>
|
|
3
|
+
<linearGradient id="rp-bg" x1="0" y1="0" x2="64" y2="64" gradientUnits="userSpaceOnUse">
|
|
4
|
+
<stop offset="0" stop-color="#2f6df6"/>
|
|
5
|
+
<stop offset="1" stop-color="#16b3a3"/>
|
|
6
|
+
</linearGradient>
|
|
7
|
+
</defs>
|
|
8
|
+
<rect x="2" y="2" width="60" height="60" rx="16" fill="url(#rp-bg)"/>
|
|
9
|
+
<g fill="none" stroke="#ffffff" stroke-width="3.2" stroke-linejoin="round" stroke-linecap="round">
|
|
10
|
+
<path d="M18 15h28"/>
|
|
11
|
+
<path d="M21 24h22"/>
|
|
12
|
+
<path d="M25 33h14"/>
|
|
13
|
+
</g>
|
|
14
|
+
<path d="M20 43h24l-6 8h-6l-6-8z" fill="#ffffff" opacity="0.92"/>
|
|
15
|
+
<g fill="#ffffff">
|
|
16
|
+
<circle cx="18" cy="15" r="3.1"/>
|
|
17
|
+
<circle cx="43" cy="24" r="3.1"/>
|
|
18
|
+
<circle cx="25" cy="33" r="3.1"/>
|
|
19
|
+
<circle cx="32" cy="55" r="2.6"/>
|
|
20
|
+
</g>
|
|
21
|
+
</svg>
|
package/index.js
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resource pool for DeepSeek Harness (single global pool).
|
|
3
|
+
*
|
|
4
|
+
* One Host plugin owns:
|
|
5
|
+
* - a durable document (`$DSH_HOME/resource-pool/state.json`) holding the pool
|
|
6
|
+
* members (skills / MCP servers with per-item mode+workspace scopes) and the
|
|
7
|
+
* skill-library sources;
|
|
8
|
+
* - the per-Agent gate that applies the pool to a live session by restricting
|
|
9
|
+
* tools, replacing the native `skill` loader with a filtered one, and stating
|
|
10
|
+
* the active pool in the system prompt;
|
|
11
|
+
* - the HTTP surface the settings page calls, plus a `resource_pool_status`
|
|
12
|
+
* tool so an Agent can explain why a resource is missing.
|
|
13
|
+
*
|
|
14
|
+
* The settings page is the browser half (`./client`); no sidebar entry.
|
|
15
|
+
*
|
|
16
|
+
* @module dsh-skill-pool
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { createRequire } from 'node:module'
|
|
20
|
+
|
|
21
|
+
import { defineTool } from '@deepseek-ai/dsh-tools'
|
|
22
|
+
import z from '@deepseek-ai/schemastery'
|
|
23
|
+
|
|
24
|
+
import { StateStore, statePath } from './src/store.js'
|
|
25
|
+
import { registerWebApi } from './src/web-api.js'
|
|
26
|
+
import { createActivation, modeOf, workspaceOf } from './src/activation.js'
|
|
27
|
+
import { createMountTracker } from './src/uploads.js'
|
|
28
|
+
import { remountPoolServers } from './src/remount.js'
|
|
29
|
+
import { resolvePool } from './src/rules.js'
|
|
30
|
+
|
|
31
|
+
/** Loader plugin name. */
|
|
32
|
+
export const name = 'skill-pool'
|
|
33
|
+
|
|
34
|
+
/** Required services; everything else is discovered through `ctx.get`. */
|
|
35
|
+
export const inject = ['tools']
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Package version reported to the panel.
|
|
39
|
+
*
|
|
40
|
+
* Read from package.json instead of a literal: a duplicated version string is
|
|
41
|
+
* exactly how the panel ended up claiming 0.3.0 while 0.3.1 was installed. Node
|
|
42
|
+
* resolves `./package.json` because it is in this package's `exports`.
|
|
43
|
+
*/
|
|
44
|
+
const VERSION = (() => {
|
|
45
|
+
try {
|
|
46
|
+
return createRequire(import.meta.url)('./package.json').version
|
|
47
|
+
} catch {
|
|
48
|
+
return 'unknown'
|
|
49
|
+
}
|
|
50
|
+
})()
|
|
51
|
+
|
|
52
|
+
/** Schemastery schema surfaced by `Config.listConfigs` and the profile patch. */
|
|
53
|
+
export const Config = z.object({
|
|
54
|
+
file: z.string().default('').description('Absolute state file path; empty uses $DSH_HOME/resource-pool/state.json.'),
|
|
55
|
+
promptLanguage: z.string().default('zh').description("Language of the injected pool summary: 'zh' | 'en'."),
|
|
56
|
+
enabled: z.boolean().default(true).description('Master switch; false keeps the panel but never gates a session.'),
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Mount the plugin.
|
|
61
|
+
* @param {import('@deepseek-ai/cordis').Context} ctx - plugin context.
|
|
62
|
+
* @param {{ file?: string, promptLanguage?: string, enabled?: boolean }} [config] - validated configuration.
|
|
63
|
+
*/
|
|
64
|
+
export function apply(ctx, config = {}) {
|
|
65
|
+
const logger = ctx.logger ?? undefined
|
|
66
|
+
const file = typeof config.file === 'string' && config.file.trim().length > 0 ? config.file.trim() : statePath()
|
|
67
|
+
const store = new StateStore({ file, logger, version: VERSION })
|
|
68
|
+
const promptLanguage = config.promptLanguage === 'en' ? 'en' : 'zh'
|
|
69
|
+
const masterEnabled = config.enabled !== false
|
|
70
|
+
|
|
71
|
+
/** Effective document: the profile's master switch wins over the stored one. */
|
|
72
|
+
const getState = async () => {
|
|
73
|
+
const state = await store.read({ fresh: true })
|
|
74
|
+
if (masterEnabled) return state
|
|
75
|
+
return { ...state, settings: { ...state.settings, enabled: false } }
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const activation = createActivation({ ctx, getState, logger, promptLanguage: () => promptLanguage })
|
|
79
|
+
|
|
80
|
+
const meta = { version: VERSION, file, pluginName: 'dsh-skill-pool', promptLanguage }
|
|
81
|
+
|
|
82
|
+
// Apply to existing Agents too: a plugin reload must not leave live sessions
|
|
83
|
+
// ungated until they are recreated.
|
|
84
|
+
ctx.inject(['agents'], () => {
|
|
85
|
+
void activation.reapplyAll()
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
ctx.on('agent/created', async ({ agent }) => {
|
|
89
|
+
// Listeners are awaited before creation resolves and before the Agent may
|
|
90
|
+
// assemble its first request; `applyToAgent` contains its own failures.
|
|
91
|
+
await activation.applyToAgent(agent)
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
ctx.on('agent/disposed', ({ agent }) => {
|
|
95
|
+
activation.disposeAgent(agent)
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
// A preset switch changes which modes are in effect without touching the pool
|
|
99
|
+
// document, so the session header alone would keep the old mode forever.
|
|
100
|
+
// `agent-preset/selected` is emitted with (sessionId, presetId) after the Agent
|
|
101
|
+
// has been rebound, which is exactly when the gate has to be recomputed.
|
|
102
|
+
ctx.on('agent-preset/selected', (sessionId) => {
|
|
103
|
+
const registry = ctx.get('agents')
|
|
104
|
+
const live = typeof registry?.list === 'function' ? registry.list() : []
|
|
105
|
+
const agent = live.find((candidate) => candidate?.session?.id === sessionId || candidate?.id === sessionId)
|
|
106
|
+
if (agent === undefined) return
|
|
107
|
+
void activation.applyToAgent(agent, { force: true })
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
// Registrations live on each Agent's context; release them when the plugin
|
|
111
|
+
// unloads instead of waiting for Agent disposal.
|
|
112
|
+
ctx.effect(() => () => activation.disposeAll(), 'resource-pool.activation')
|
|
113
|
+
|
|
114
|
+
const mounts = createMountTracker()
|
|
115
|
+
|
|
116
|
+
registerWebApi({
|
|
117
|
+
ctx,
|
|
118
|
+
store,
|
|
119
|
+
mounts,
|
|
120
|
+
meta,
|
|
121
|
+
logger,
|
|
122
|
+
onChanged: async () => {
|
|
123
|
+
await activation.reapplyAll()
|
|
124
|
+
},
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
// Storage keeps each MCP member's connection definition precisely so this
|
|
128
|
+
// activation can restore it: a Host restart drops every Loader row, and
|
|
129
|
+
// without this the pool would come back listing servers with zero tools.
|
|
130
|
+
// Waiting for `loader` means the rows are created as soon as the service that
|
|
131
|
+
// owns them exists, and the remount itself stays in the background.
|
|
132
|
+
let remounted = false
|
|
133
|
+
ctx.inject(['loader'], () => {
|
|
134
|
+
if (remounted) return
|
|
135
|
+
remounted = true
|
|
136
|
+
void remountPoolServers({ ctx, getState, mounts, logger }).catch((error) => {
|
|
137
|
+
logger?.warn?.(`[skill-pool] remount failed: ${error instanceof Error ? error.message : String(error)}`)
|
|
138
|
+
})
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
// Read-only self-report: answers "why is this skill/MCP tool missing?" for the
|
|
142
|
+
// calling Agent and doubles as the plugin's own health probe.
|
|
143
|
+
const statusTool = defineTool({
|
|
144
|
+
name: 'resource_pool_status',
|
|
145
|
+
description:
|
|
146
|
+
'Report the resource pool state for the current session: resolved Agent mode/workspace, which skills and MCP servers the pool activates here, and the pool size. Use it when a skill or MCP tool you expected is missing.',
|
|
147
|
+
parameters: {},
|
|
148
|
+
output: {
|
|
149
|
+
schema: { type: 'object', additionalProperties: true, properties: {} },
|
|
150
|
+
render: (_args, value) => [{ type: 'text', text: JSON.stringify(value, null, 2) }],
|
|
151
|
+
},
|
|
152
|
+
async execute(_args, exec) {
|
|
153
|
+
const state = await getState()
|
|
154
|
+
const agent = exec?.agent
|
|
155
|
+
const mode = agent === undefined ? '' : modeOf(ctx, agent)
|
|
156
|
+
const cwd = agent === undefined ? '' : workspaceOf(agent)
|
|
157
|
+
const pool = resolvePool(state, { mode, cwd })
|
|
158
|
+
return {
|
|
159
|
+
plugin: 'dsh-skill-pool',
|
|
160
|
+
version: VERSION,
|
|
161
|
+
stateFile: file,
|
|
162
|
+
promptLanguage,
|
|
163
|
+
masterEnabled,
|
|
164
|
+
mode,
|
|
165
|
+
cwd,
|
|
166
|
+
...pool,
|
|
167
|
+
counts: { skills: Object.keys(state.skills).length, mcp: Object.keys(state.mcp).length, sources: Object.keys(state.sources).length },
|
|
168
|
+
settings: state.settings,
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
ctx.effect(() => ctx.tools.register(statusTool), 'resource-pool.status-tool')
|
|
174
|
+
}
|
package/locale/en.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "Skill Pool",
|
|
4
|
+
"description": "One global pool where importing a skill adds it immediately; every skill and MCP server activates by its own Agent-mode and workspace conditions. Imports from files, ZIP or Git, mounts MCP from JSON, and Git sources can check for updates."
|
|
5
|
+
}
|
|
6
|
+
}
|
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,64 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-skill-pool",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.3.7",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "One pool for all your DSH skills: import from files, ZIP, GitHub or GitLab, then scope each skill to Agent modes and workspaces from the Settings panel — no code editing required.",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"dsh",
|
|
9
|
+
"deepseek-harness",
|
|
10
|
+
"plugin",
|
|
11
|
+
"skill",
|
|
12
|
+
"skills",
|
|
13
|
+
"skill-pool",
|
|
14
|
+
"skill-manager",
|
|
15
|
+
"mcp",
|
|
16
|
+
"agent-preset",
|
|
17
|
+
"workspace"
|
|
18
|
+
],
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/rj-yejianping/dsh-skill-pool.git"
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://github.com/rj-yejianping/dsh-skill-pool#readme",
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/rj-yejianping/dsh-skill-pool/issues"
|
|
26
|
+
},
|
|
27
|
+
"exports": {
|
|
28
|
+
".": "./index.js",
|
|
29
|
+
"./client": "./client.js",
|
|
30
|
+
"./locale/*.json": "./locale/*.json",
|
|
31
|
+
"./icon": "./icon.svg",
|
|
32
|
+
"./package.json": "./package.json"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"index.js",
|
|
36
|
+
"client.js",
|
|
37
|
+
"src/**/*.js",
|
|
38
|
+
"cordis.patch.yml",
|
|
39
|
+
"locale/*.json",
|
|
40
|
+
"icon.svg",
|
|
41
|
+
"README.md",
|
|
42
|
+
"LICENSE",
|
|
43
|
+
"CONTRACTS-v3.md"
|
|
44
|
+
],
|
|
45
|
+
"scripts": {
|
|
46
|
+
"test": "node --test test/*.test.mjs",
|
|
47
|
+
"lint": "node --check index.js && node --check client.js && node --check src/store.js && node --check src/rules.js && node --check src/inventory.js && node --check src/activation.js && node --check src/provider.js && node --check src/skill-content.js && node --check src/web-api.js && node --check src/uploads.js && node --check src/mount.js && node --check src/remount.js && node --check src/library.js"
|
|
48
|
+
},
|
|
49
|
+
"dsh": {
|
|
50
|
+
"bundle": {
|
|
51
|
+
"patch": "./cordis.patch.yml"
|
|
52
|
+
},
|
|
53
|
+
"client": {
|
|
54
|
+
"platform": "web",
|
|
55
|
+
"immediately": true,
|
|
56
|
+
"inject": [
|
|
57
|
+
"@deepseek-ai/dsh-client-ui-settings"
|
|
58
|
+
]
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
"publishConfig": {
|
|
62
|
+
"access": "public"
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-Agent application of the single global resource pool.
|
|
3
|
+
*
|
|
4
|
+
* The pool is the user's library, so "what the pool activates" is exactly
|
|
5
|
+
* "what this Agent may see". Two effects express that, one per namespace:
|
|
6
|
+
*
|
|
7
|
+
* 1. Skills — a library provider is registered on the **Agent's own context**.
|
|
8
|
+
* `SkillRegistry.registerProvider` files a provider into the layer of the
|
|
9
|
+
* calling context's scope, and an Agent's scope key is the Agent object, so
|
|
10
|
+
* that layer is visible to this Agent alone. A skill the pool did not admit
|
|
11
|
+
* is therefore absent from this Agent's catalog AND unloadable: strict hiding
|
|
12
|
+
* falls out of the registry instead of tool replacement.
|
|
13
|
+
* 2. MCP — `agent.ctx.tools.restrict({ deny })` removes exactly the public tool
|
|
14
|
+
* names of servers the pool did not activate. There is no layered equivalent
|
|
15
|
+
* for tools, so this remains a restriction.
|
|
16
|
+
*
|
|
17
|
+
* A system-prompt section then states the mode, workspace and what is active, so
|
|
18
|
+
* the model neither guesses nor tries to recall a resource it cannot reach.
|
|
19
|
+
*
|
|
20
|
+
* Everything is computed before the live activation is released, so a
|
|
21
|
+
* configuration change never leaves a window where the old restrictions are
|
|
22
|
+
* lifted but the new ones are not installed yet. Registrations are owned by
|
|
23
|
+
* `agent.ctx` and released on disposal.
|
|
24
|
+
*
|
|
25
|
+
* @module dsh-skill-pool/activation
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import { librarySkillsRoot } from './library.js'
|
|
29
|
+
import { createLibraryProvider } from './provider.js'
|
|
30
|
+
import { mcpServerOf, MCP_TOOL_PREFIX, resolvePool } from './rules.js'
|
|
31
|
+
|
|
32
|
+
const SECTION_NAME = 'resource-pool:active'
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Read the Agent mode of one live Agent.
|
|
36
|
+
* @param {object} ctx - plugin context.
|
|
37
|
+
* @param {any} agent - live Agent.
|
|
38
|
+
* @returns {string} preset id, never empty.
|
|
39
|
+
*/
|
|
40
|
+
export function modeOf(ctx, agent) {
|
|
41
|
+
// Order matters. `session.header.agentPreset` is a snapshot taken when the
|
|
42
|
+
// session was created and is never rewritten, so a session that switched preset
|
|
43
|
+
// before its first turn keeps reporting the original one forever. The live
|
|
44
|
+
// sources must win, or the pool resolves against a mode the Agent is not in.
|
|
45
|
+
const registry = ctx.get('agentPresets')
|
|
46
|
+
if (registry !== undefined && typeof registry.composedPreset === 'function' && agent?.ctx !== undefined) {
|
|
47
|
+
try {
|
|
48
|
+
const composed = registry.composedPreset(agent.ctx)
|
|
49
|
+
if (typeof composed === 'string' && composed.length > 0) return composed
|
|
50
|
+
} catch {
|
|
51
|
+
/* fall through to the logged selection */
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
// The preset projection is the latest `agent-preset/selected` in the session log.
|
|
55
|
+
const projections = ctx.get('sessionProjections')
|
|
56
|
+
if (projections !== undefined && typeof projections.stateOf === 'function' && agent?.session !== undefined) {
|
|
57
|
+
try {
|
|
58
|
+
const selected = projections.stateOf(agent.session, 'agentPreset')
|
|
59
|
+
if (typeof selected === 'string' && selected.length > 0) return selected
|
|
60
|
+
} catch {
|
|
61
|
+
/* fall through to the creation-time header */
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const header = agent?.session?.header?.agentPreset
|
|
65
|
+
if (typeof header === 'string' && header.trim().length > 0) return header.trim()
|
|
66
|
+
return 'default'
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Whether the preset a live Agent actually runs differs from its session header.
|
|
71
|
+
*
|
|
72
|
+
* A switch before the first turn only appends `agent-preset/selected`; the header
|
|
73
|
+
* stays as the creation snapshot. Callers use this to notice that re-applying the
|
|
74
|
+
* pool is warranted even though no document changed.
|
|
75
|
+
*
|
|
76
|
+
* @param {object} ctx - plugin context.
|
|
77
|
+
* @param {any} agent - live Agent.
|
|
78
|
+
* @returns {boolean} whether the header is stale for this Agent.
|
|
79
|
+
*/
|
|
80
|
+
export function presetHeaderStale(ctx, agent) {
|
|
81
|
+
const header = agent?.session?.header?.agentPreset
|
|
82
|
+
if (typeof header !== 'string' || header.trim().length === 0) return false
|
|
83
|
+
return modeOf(ctx, agent) !== header.trim()
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Read the working directory of one live Agent.
|
|
88
|
+
* @param {any} agent - live Agent.
|
|
89
|
+
* @returns {string} absolute directory or the process cwd.
|
|
90
|
+
*/
|
|
91
|
+
export function workspaceOf(agent) {
|
|
92
|
+
const cwd = agent?.session?.header?.cwd
|
|
93
|
+
if (typeof cwd === 'string' && cwd.length > 0) return cwd
|
|
94
|
+
return process.cwd()
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Chinese prompt summary. */
|
|
98
|
+
function summaryTextZh({ mode, cwd, skillNames, serverNames, skillGated, mcpGated }) {
|
|
99
|
+
const lines = ['## 技能池已激活', '', `Agent 模式:\`${mode}\`,工作区:\`${cwd}\`。`]
|
|
100
|
+
if (skillGated) {
|
|
101
|
+
lines.push(
|
|
102
|
+
skillNames.length > 0
|
|
103
|
+
? `技能池在本会话提供了这些技能:${skillNames.join('、')}。`
|
|
104
|
+
: '技能池在本会话没有提供任何技能。',
|
|
105
|
+
)
|
|
106
|
+
}
|
|
107
|
+
if (mcpGated) {
|
|
108
|
+
lines.push(
|
|
109
|
+
serverNames.length > 0
|
|
110
|
+
? `本会话可用的 MCP 连接只有:${serverNames.join('、')};其它 MCP 工具已被隐藏。`
|
|
111
|
+
: '本会话没有任何可用 MCP 连接;所有 MCP 工具已被隐藏。',
|
|
112
|
+
)
|
|
113
|
+
}
|
|
114
|
+
lines.push(
|
|
115
|
+
'未列出的技能池资源在本模式/工作区下确实不可用:不要尝试调用,也不要凭记忆复述它们的内容。',
|
|
116
|
+
'如果任务确实需要某个不可用的资源,请让用户在「设置 → 技能池」里为它加上当前的 Agent 模式或工作区条件。',
|
|
117
|
+
)
|
|
118
|
+
return lines.join('\n')
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** English prompt summary. */
|
|
122
|
+
function summaryTextEn({ mode, cwd, skillNames, serverNames, skillGated, mcpGated }) {
|
|
123
|
+
const lines = ['## Resource pool active', '', `Agent mode \`${mode}\`, workspace \`${cwd}\`.`]
|
|
124
|
+
if (skillGated) {
|
|
125
|
+
lines.push(
|
|
126
|
+
skillNames.length > 0
|
|
127
|
+
? `The resource pool supplies these skills in this session: ${skillNames.join(', ')}.`
|
|
128
|
+
: 'The resource pool supplies no skills in this session.',
|
|
129
|
+
)
|
|
130
|
+
}
|
|
131
|
+
if (mcpGated) {
|
|
132
|
+
lines.push(
|
|
133
|
+
serverNames.length > 0
|
|
134
|
+
? `The only MCP connections available in this session are: ${serverNames.join(', ')}; every other MCP tool is hidden.`
|
|
135
|
+
: 'No MCP connections are available in this session; all MCP tools are hidden.',
|
|
136
|
+
)
|
|
137
|
+
}
|
|
138
|
+
lines.push(
|
|
139
|
+
'Resource-pool resources that are not listed are genuinely unavailable in this mode/workspace: do not try to call them and do not recall their content from memory.',
|
|
140
|
+
'If a task really needs an unavailable resource, ask the user to add the current Agent mode or workspace to its condition in Settings → Resource pool.',
|
|
141
|
+
)
|
|
142
|
+
return lines.join('\n')
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Create the activation manager.
|
|
147
|
+
* @param {object} options - manager options.
|
|
148
|
+
* @param {object} options.ctx - plugin context.
|
|
149
|
+
* @param {(fresh?: boolean) => Promise<object>} options.getState - read the current document.
|
|
150
|
+
* @param {{ debug?: Function, warn?: Function }} [options.logger] - logger.
|
|
151
|
+
* @param {() => string} [options.promptLanguage] - 'zh' | 'en'.
|
|
152
|
+
* @param {() => string} [options.libraryRoot] - library skills root override.
|
|
153
|
+
* @returns {object} manager with applyToAgent/reapplyAll/disposeAgent/disposeAll.
|
|
154
|
+
*/
|
|
155
|
+
export function createActivation({ ctx, getState, logger, promptLanguage = () => 'zh', libraryRoot = () => librarySkillsRoot() }) {
|
|
156
|
+
/** @type {WeakMap<object, {disposers: Function[], signature: string, chain: Promise<void>}>} */
|
|
157
|
+
const agents = new WeakMap()
|
|
158
|
+
|
|
159
|
+
/** Release every registration owned for one Agent. */
|
|
160
|
+
const release = (agent) => {
|
|
161
|
+
const record = agents.get(agent)
|
|
162
|
+
if (record === undefined) return
|
|
163
|
+
for (const dispose of record.disposers.reverse()) {
|
|
164
|
+
try {
|
|
165
|
+
dispose()
|
|
166
|
+
} catch {
|
|
167
|
+
/* already disposed */
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
record.disposers = []
|
|
171
|
+
record.signature = ''
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Signature of a decision, used to skip no-op re-applications. */
|
|
175
|
+
const signatureOf = (pool, allowed, libraryNames) =>
|
|
176
|
+
JSON.stringify({ gated: pool.gated, skills: allowed, mcp: pool.mcpServers, gs: pool.gateSkills, gm: pool.gateMcp, lib: libraryNames })
|
|
177
|
+
|
|
178
|
+
/** Serialize applications per Agent. */
|
|
179
|
+
const applyToAgent = async (agent, { force = false } = {}) => {
|
|
180
|
+
if (agent?.ctx === undefined) return
|
|
181
|
+
let record = agents.get(agent)
|
|
182
|
+
if (record === undefined) {
|
|
183
|
+
record = { disposers: [], signature: '', chain: Promise.resolve() }
|
|
184
|
+
agents.set(agent, record)
|
|
185
|
+
}
|
|
186
|
+
const next = record.chain.then(() => applyNow(agent, record, { force })).catch((error) => {
|
|
187
|
+
logger?.warn?.(`[resource-pool] activation failed: ${error instanceof Error ? error.message : String(error)}`)
|
|
188
|
+
})
|
|
189
|
+
record.chain = next
|
|
190
|
+
return next
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Build one Agent's registrations. Runs serialized per Agent. */
|
|
194
|
+
const applyNow = async (agent, record, { force }) => {
|
|
195
|
+
const state = await getState()
|
|
196
|
+
const settings = state.settings ?? {}
|
|
197
|
+
const mode = modeOf(ctx, agent)
|
|
198
|
+
const cwd = workspaceOf(agent)
|
|
199
|
+
const pool = resolvePool(state, { mode, cwd })
|
|
200
|
+
|
|
201
|
+
// The pool owns every library skill, so an Agent sees a skill only when the
|
|
202
|
+
// pool admits it here. A disabled master switch means "do not gate at all",
|
|
203
|
+
// which for a per-Agent provider means "admit the whole library".
|
|
204
|
+
const libraryNames = Object.keys(state.skills ?? {}).sort()
|
|
205
|
+
const allowed = settings.enabled === false ? libraryNames : pool.skills
|
|
206
|
+
const signature = `${signatureOf(pool, allowed, libraryNames)}|${mode}|${cwd}`
|
|
207
|
+
if (!force && record.signature === signature) return
|
|
208
|
+
|
|
209
|
+
const tools = ctx.get('tools')
|
|
210
|
+
const skills = agent.ctx.get('skills')
|
|
211
|
+
|
|
212
|
+
// ---- Compute the whole next activation before touching the live one.
|
|
213
|
+
const allowedSkills = new Set(allowed)
|
|
214
|
+
const denyMcp = []
|
|
215
|
+
if (pool.gateMcp && tools !== undefined) {
|
|
216
|
+
const allowedServers = new Set(pool.mcpServers)
|
|
217
|
+
try {
|
|
218
|
+
for (const schema of tools.schemas()) {
|
|
219
|
+
const name = schema?.name
|
|
220
|
+
if (typeof name !== 'string' || !name.startsWith(MCP_TOOL_PREFIX)) continue
|
|
221
|
+
if (!allowedServers.has(mcpServerOf(name))) denyMcp.push(name)
|
|
222
|
+
}
|
|
223
|
+
} catch {
|
|
224
|
+
/* no observable global tool surface: nothing to hide */
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// ---- Apply synchronously from here on.
|
|
229
|
+
release(agent)
|
|
230
|
+
const disposers = record.disposers
|
|
231
|
+
const effect = (register, label) => {
|
|
232
|
+
try {
|
|
233
|
+
disposers.push(agent.ctx.effect(register, `resource-pool.${label}`))
|
|
234
|
+
} catch (error) {
|
|
235
|
+
logger?.debug?.(`[resource-pool] ${label} registration failed: ${error instanceof Error ? error.message : String(error)}`)
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (allowedSkills.size > 0) {
|
|
240
|
+
if (skills === undefined || typeof skills.registerProvider !== 'function') {
|
|
241
|
+
logger?.warn?.('[resource-pool] skills registry unavailable; library skills cannot be served')
|
|
242
|
+
} else {
|
|
243
|
+
const root = libraryRoot()
|
|
244
|
+
effect(
|
|
245
|
+
// Registered from `agent.ctx`: the layer belongs to this Agent alone.
|
|
246
|
+
() => skills.registerProvider((control) => createLibraryProvider({ root, allowed: allowedSkills, logger, control })),
|
|
247
|
+
'skill-provider',
|
|
248
|
+
)
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (denyMcp.length > 0) effect(() => agent.ctx.tools.restrict({ deny: denyMcp }), 'mcp-restrict')
|
|
253
|
+
|
|
254
|
+
const systemPrompt = ctx.get('systemPrompt')
|
|
255
|
+
if (settings.injectSummary !== false && pool.gated && systemPrompt !== undefined && typeof systemPrompt.getSectionOrder === 'function') {
|
|
256
|
+
const skillNames = allowedSkills.size > 0 ? [...allowedSkills].sort() : []
|
|
257
|
+
const summary = (promptLanguage() === 'en' ? summaryTextEn : summaryTextZh)({
|
|
258
|
+
mode,
|
|
259
|
+
cwd,
|
|
260
|
+
skillNames,
|
|
261
|
+
serverNames: pool.gateMcp ? pool.mcpServers : [],
|
|
262
|
+
skillGated: pool.gateSkills,
|
|
263
|
+
mcpGated: pool.gateMcp,
|
|
264
|
+
})
|
|
265
|
+
effect(
|
|
266
|
+
() =>
|
|
267
|
+
agent.ctx.systemPrompt.section({
|
|
268
|
+
name: SECTION_NAME,
|
|
269
|
+
order: systemPrompt.getSectionOrder('TOOL_READ') - 30,
|
|
270
|
+
text: summary,
|
|
271
|
+
}),
|
|
272
|
+
'summary',
|
|
273
|
+
)
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
record.signature = signature
|
|
277
|
+
logger?.debug?.(
|
|
278
|
+
`[resource-pool] activated mode=${mode} cwd=${cwd} skills=${allowedSkills.size}/${libraryNames.length} mcpHidden=${denyMcp.length}`,
|
|
279
|
+
)
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** Re-apply the current document to every live Agent. */
|
|
283
|
+
const reapplyAll = async () => {
|
|
284
|
+
const registry = ctx.get('agents')
|
|
285
|
+
const live = typeof registry?.list === 'function' ? registry.list() : []
|
|
286
|
+
await Promise.all(live.map((agent) => applyToAgent(agent, { force: true })))
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** Release one Agent's registrations. */
|
|
290
|
+
const disposeAgent = (agent) => {
|
|
291
|
+
release(agent)
|
|
292
|
+
agents.delete(agent)
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** Release every Agent's registrations (plugin unload). */
|
|
296
|
+
const disposeAll = () => {
|
|
297
|
+
const registry = ctx.get('agents')
|
|
298
|
+
const live = typeof registry?.list === 'function' ? registry.list() : []
|
|
299
|
+
for (const agent of live) disposeAgent(agent)
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
return { applyToAgent, reapplyAll, disposeAgent, disposeAll }
|
|
303
|
+
}
|