dsh-extra-plan 0.0.0-stage → 1.7.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.
- package/LICENSE +21 -0
- package/README.md +179 -2
- package/assets/presets/extra-plan/agent.cordis.yml +580 -0
- package/assets/presets/extra-plan/preset-definition-extra-plan-executor.generated.yml +115 -0
- package/assets/presets/extra-plan/preset-definition-extra-plan-planner.generated.yml +113 -0
- package/assets/presets/extra-plan/preset-definition-extra-plan-prober.generated.yml +107 -0
- package/assets/presets/extra-plan/preset-definition-extra-plan-reviewer.generated.yml +115 -0
- package/assets/presets/extra-plan/preset-definition.generated.yml +335 -0
- package/assets/presets/extra-plan/preset.yml +2 -0
- package/cordis.patch.yml +7 -0
- package/index.js +1671 -0
- package/lib/agent-runtime.js +172 -0
- package/lib/agent-session.js +65 -0
- package/lib/assembly-presentation.js +267 -0
- package/lib/client-carrier.js +7 -0
- package/lib/client.js +314 -0
- package/lib/extra-plan-gate-tool.js +387 -0
- package/lib/gate-decisions.js +744 -0
- package/lib/gate-words.js +108 -0
- package/lib/live-config.js +293 -0
- package/lib/log-retention.js +132 -0
- package/lib/model-routing.js +648 -0
- package/lib/path-policy.js +464 -0
- package/lib/plan-draft-tool.js +294 -0
- package/lib/planner-budget.js +136 -0
- package/lib/preset-defaults.generated.js +17 -0
- package/lib/preset-settings.js +3 -0
- package/lib/preset-sync.js +809 -0
- package/lib/preset-yaml.js +438 -0
- package/lib/role-contract.js +11 -0
- package/lib/run-code-scanner.js +360 -0
- package/lib/run-code-static.js +621 -0
- package/lib/runtime-diagnostics.js +89 -0
- package/lib/runtime-lifecycle.js +162 -0
- package/lib/runtime-static.js +23 -0
- package/lib/save-contract.js +263 -0
- package/lib/save-persistence.js +223 -0
- package/lib/save-plan-markdown-parser.js +369 -0
- package/lib/save-plan-validation-diagnostics.js +49 -0
- package/lib/save-plan-validation.js +812 -0
- package/lib/save-probe-validation.js +173 -0
- package/lib/save-source-verification.js +195 -0
- package/lib/save-tool-factories.js +270 -0
- package/lib/sdk-text-cache.js +161 -0
- package/lib/settings-contract.js +191 -0
- package/lib/settings.js +369 -0
- package/lib/shell-mutation.js +484 -0
- package/lib/usage-ledger-background.js +214 -0
- package/lib/usage-ledger-worker.js +116 -0
- package/lib/usage-ledger.js +1393 -0
- package/locale/en.json +6 -0
- package/locale/settings/en.json +6 -0
- package/locale/settings/zh.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +56 -4
- package/scripts/generate-runtime-defaults.mjs +796 -0
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// 每次 apply 独立创建 agent runtime factory;role 与 cache 状态不会跨 createAgentRuntime 调用共享。
|
|
2
|
+
|
|
3
|
+
import { isSubagentChild } from './agent-session.js'
|
|
4
|
+
import { ROLE_IDS, PLANNER_PRESET_ID, PROBER_PRESET_ID, REVIEWER_PRESET_ID, USAGE_ROLE_BY_PRESET } from './role-contract.js'
|
|
5
|
+
|
|
6
|
+
const CHILD_HOST_MODE_BY_PRESET = Object.freeze(Object.assign(Object.create(null), {
|
|
7
|
+
[ROLE_IDS[0]]: 'workspace-write',
|
|
8
|
+
[PLANNER_PRESET_ID]: 'read-only',
|
|
9
|
+
[REVIEWER_PRESET_ID]: 'workspace-write',
|
|
10
|
+
[PROBER_PRESET_ID]: 'read-only',
|
|
11
|
+
}))
|
|
12
|
+
|
|
13
|
+
// 规划子代理的会话角色预设 id(与 assets/presets/extra-plan/agent.cordis.yml 第二文档一致)。
|
|
14
|
+
export { PLANNER_PRESET_ID } from './role-contract.js'
|
|
15
|
+
|
|
16
|
+
// 只有 parent session 存在时委派才算 live;缺少 parent/agents 或读取出错都 fail-open。
|
|
17
|
+
export function isLiveDelegation(agent, agents) {
|
|
18
|
+
const header = agent.session.header
|
|
19
|
+
if (header === undefined || header === null) return true
|
|
20
|
+
const parentSession = header.parentSession
|
|
21
|
+
if (parentSession === undefined) return true
|
|
22
|
+
if (agents === undefined) return true
|
|
23
|
+
try {
|
|
24
|
+
return agents.get(parentSession) !== undefined
|
|
25
|
+
} catch (error) {
|
|
26
|
+
return true
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function createAgentRuntime({ getAgents, sandboxPolicy, warn }) {
|
|
31
|
+
const getAgentsOf = typeof getAgents === 'function' ? getAgents : () => undefined
|
|
32
|
+
const warnOf = typeof warn === 'function' ? warn : (...args) => console.warn(...args)
|
|
33
|
+
const subagentAsPlannerWarned = new WeakSet()
|
|
34
|
+
const usageContexts = new WeakMap()
|
|
35
|
+
const MAX_USAGE_PARENT_DEPTH = 8
|
|
36
|
+
|
|
37
|
+
function isChild(agent, events) {
|
|
38
|
+
if (!isSubagentChild(agent, events)) return false
|
|
39
|
+
const child = isLiveDelegation(agent, getAgentsOf())
|
|
40
|
+
if (!child && !subagentAsPlannerWarned.has(agent)) {
|
|
41
|
+
subagentAsPlannerWarned.add(agent)
|
|
42
|
+
warnOf('extra-plan: subagent session "' + agent.session.header.id + '" is live without its parent agent — root gates apply (resumed-as-root or misclassification)')
|
|
43
|
+
}
|
|
44
|
+
return child
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function isPresetChild(agent, events, presetId) {
|
|
48
|
+
if (agent === undefined || agent === null) return false
|
|
49
|
+
const session = agent.session
|
|
50
|
+
if (session === undefined || session === null) return false
|
|
51
|
+
const header = session.header
|
|
52
|
+
if (header === undefined || header === null) return false
|
|
53
|
+
const preset = typeof header.agentPreset === 'string' ? header.agentPreset : ''
|
|
54
|
+
return preset === presetId && isSubagentChild(agent, events)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// 规划子代理:子会话且 session.header.agentPreset === 'extra-plan-planner'。
|
|
58
|
+
// 宿主 subagent descriptor 的 mode 恒为 continuable(manager 固定值),
|
|
59
|
+
// 不能再作判据;判据改为会话 header 的角色预设 id,其余值与缺失一律非 planner。
|
|
60
|
+
function isPlannerChild(agent, events) {
|
|
61
|
+
return isPresetChild(agent, events, PLANNER_PRESET_ID)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// 验收者子代理:子会话且 session.header.agentPreset === 'extra-plan-reviewer'。
|
|
65
|
+
function isReviewerChild(agent, events) {
|
|
66
|
+
return isPresetChild(agent, events, REVIEWER_PRESET_ID)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function usageRootOf(sessionId, parentSessionId, delegationDepth) {
|
|
70
|
+
if (parentSessionId === '') return sessionId
|
|
71
|
+
if (delegationDepth === 1) return parentSessionId
|
|
72
|
+
let agents
|
|
73
|
+
try {
|
|
74
|
+
agents = getAgentsOf()
|
|
75
|
+
} catch (error) {
|
|
76
|
+
return ''
|
|
77
|
+
}
|
|
78
|
+
if (agents === undefined || agents === null || typeof agents.get !== 'function') return ''
|
|
79
|
+
const seen = new Set()
|
|
80
|
+
if (sessionId !== '') seen.add(sessionId)
|
|
81
|
+
let currentId = parentSessionId
|
|
82
|
+
for (let level = 0; level < MAX_USAGE_PARENT_DEPTH; level += 1) {
|
|
83
|
+
if (typeof currentId !== 'string' || currentId === '' || seen.has(currentId)) return ''
|
|
84
|
+
seen.add(currentId)
|
|
85
|
+
let parent
|
|
86
|
+
try {
|
|
87
|
+
parent = agents.get(currentId)
|
|
88
|
+
} catch (error) {
|
|
89
|
+
return ''
|
|
90
|
+
}
|
|
91
|
+
if (parent === undefined || parent === null || parent.session === undefined || parent.session === null) return ''
|
|
92
|
+
const header = parent.session.header
|
|
93
|
+
if (header === undefined || header === null) return ''
|
|
94
|
+
const nextId = typeof header.parentSession === 'string' ? header.parentSession : ''
|
|
95
|
+
if (nextId === '') return currentId
|
|
96
|
+
if (seen.has(nextId)) return ''
|
|
97
|
+
currentId = nextId
|
|
98
|
+
}
|
|
99
|
+
return ''
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// 用量身份上下文:改造8 后仅用于角色判定与诊断上下文(不再喂给任何实时账本写入,
|
|
103
|
+
// 账本改由启动扫描按会话日志重建),字段口径仍是九身份键所需的那几项。
|
|
104
|
+
// 生产面无消费者,仅保留给回归取证(step-00 的 AR-* 用例直呼本函数)。
|
|
105
|
+
function usageContextOf(agent, events) {
|
|
106
|
+
const cached = usageContexts.get(agent)
|
|
107
|
+
if (cached !== undefined) return cached
|
|
108
|
+
const session = agent !== undefined && agent !== null ? agent.session : undefined
|
|
109
|
+
const header = session !== undefined && session !== null ? session.header : undefined
|
|
110
|
+
const sessionId = header !== undefined && header !== null && typeof header.id === 'string' ? header.id : ''
|
|
111
|
+
const parentSessionId = header !== undefined && header !== null && typeof header.parentSession === 'string' ? header.parentSession : ''
|
|
112
|
+
const agentPreset = header !== undefined && header !== null && typeof header.agentPreset === 'string' ? header.agentPreset : ''
|
|
113
|
+
const delegationDepth = header !== undefined && header !== null && Number.isInteger(header.delegationDepth) && header.delegationDepth >= 0 ? header.delegationDepth : 0
|
|
114
|
+
const child = isSubagentChild(agent, events)
|
|
115
|
+
const context = Object.freeze({
|
|
116
|
+
sessionId,
|
|
117
|
+
parentSessionId,
|
|
118
|
+
rootSessionId: child ? (parentSessionId === '' ? '' : usageRootOf(sessionId, parentSessionId, delegationDepth)) : sessionId,
|
|
119
|
+
agentPreset,
|
|
120
|
+
delegationDepth,
|
|
121
|
+
// 自有属性守卫:agentPreset 若命中 Object.prototype 属性名(toString/__proto__/constructor 等)不得取到函数值;回落 'child',与 applyChildPolicy 的函数级判定同口径(按函数名引用,不记行号)。
|
|
122
|
+
role: child ? (Object.prototype.hasOwnProperty.call(USAGE_ROLE_BY_PRESET, agentPreset) ? USAGE_ROLE_BY_PRESET[agentPreset] : 'child') : 'main',
|
|
123
|
+
})
|
|
124
|
+
usageContexts.set(agent, context)
|
|
125
|
+
return context
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// 真实工具集防御取数:不缓存,服务缺失/异常/非数组均返回 undefined。
|
|
129
|
+
function toolSchemasOf(agent) {
|
|
130
|
+
if (agent === undefined || agent === null) return undefined
|
|
131
|
+
const agentCtx = agent.ctx
|
|
132
|
+
if (agentCtx === undefined || agentCtx === null) return undefined
|
|
133
|
+
let tools
|
|
134
|
+
try {
|
|
135
|
+
tools = typeof agentCtx.get === 'function' ? agentCtx.get('tools') : undefined
|
|
136
|
+
} catch (error) {
|
|
137
|
+
tools = undefined
|
|
138
|
+
}
|
|
139
|
+
if (tools === undefined || tools === null || typeof tools !== 'object') return undefined
|
|
140
|
+
if (typeof tools.schemas !== 'function') return undefined
|
|
141
|
+
try {
|
|
142
|
+
const schemas = tools.schemas(agent)
|
|
143
|
+
return Array.isArray(schemas) ? schemas : undefined
|
|
144
|
+
} catch (error) {
|
|
145
|
+
return undefined
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function applyChildPolicy(agent) {
|
|
150
|
+
const session = agent.session
|
|
151
|
+
const header = session !== undefined && session !== null ? session.header : undefined
|
|
152
|
+
const preset = header !== undefined && header !== null ? header.agentPreset : undefined
|
|
153
|
+
if (typeof preset !== 'string' || !Object.prototype.hasOwnProperty.call(CHILD_HOST_MODE_BY_PRESET, preset)) return
|
|
154
|
+
const mode = CHILD_HOST_MODE_BY_PRESET[preset]
|
|
155
|
+
const override = sandboxPolicy !== undefined && sandboxPolicy !== null && typeof sandboxPolicy.overrideOf === 'function'
|
|
156
|
+
? sandboxPolicy.overrideOf(session)
|
|
157
|
+
: undefined
|
|
158
|
+
if (override === mode) return
|
|
159
|
+
session.append('sandbox/mode', { mode, source: 'delegation' })
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
// 子会话基线:只做角色识别与宿主模式落定。改造8 起用量账本由插件启动扫描会话日志维护,
|
|
164
|
+
// 这里不再有账本折叠调用(子会话用量不再在 disposed/创建时折叠落账)。
|
|
165
|
+
function childBaseline(agent, events) {
|
|
166
|
+
const child = isChild(agent, events)
|
|
167
|
+
if (child) applyChildPolicy(agent)
|
|
168
|
+
return child
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return { isChild, isPlannerChild, isReviewerChild, toolSchemasOf, usageContextOf, childBaseline }
|
|
172
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// sessionEvents/isSubagentChild 的唯一来源:零依赖纯函数只读入参、不保存模块或会话状态;
|
|
2
|
+
// index.js 与 model-routing.js 共用同一实现,避免两侧漂移。
|
|
3
|
+
// 当前宿主 API 以 snapshotEvents() 为主:无参返回冻结事件数组,snapshotEvents(from,to)/eventAt(seq)
|
|
4
|
+
// 提供范围或单点读取;来源缺失或无效时返回 [],调用方的 Array.isArray/length 防御保持可用。
|
|
5
|
+
// 子代理识别先看 session.header,再扫描 subagent/descriptor 事件;主会话不缓存 false。
|
|
6
|
+
// events(可选第二参,T8):调用方已用 observeSession 预取到不可变快照时直接传入复用(同一次钩子内
|
|
7
|
+
// 多处消费共用同一份数组,免掉重复物化);不传/传 undefined 仍走 snapshotEvents 兜底。
|
|
8
|
+
// 改造8(账本与弃用迁移)后,@deprecated 同步兜底仍被两类调用方使用:tools/pre-execute(同步钩子
|
|
9
|
+
// 无法 await observeSession),以及未显式传 events 的调用方(isSubagentChild 多处判定、isBootstrapPhase
|
|
10
|
+
// 的既有夹具/同步调用方)——不再按「只剩两处」计数。
|
|
11
|
+
// usage 账本已改为插件启动时扫描本地会话日志维护,不再经本函数读事件。
|
|
12
|
+
// events 非法(非数组)时按「未传」处理、落回兜底路径,不让脏入参穿透成非数组返回值。
|
|
13
|
+
export function sessionEvents(session, events) {
|
|
14
|
+
if (events !== undefined && Array.isArray(events)) return events
|
|
15
|
+
if (session === undefined || session === null) return []
|
|
16
|
+
// 0.2.2 已 @deprecated(宿主 @deepseek-ai/dsh-session/lib/types/index.d.ts 的 Session 类型同签名保留),同步入口兜底:
|
|
17
|
+
// 同步钩子无法 await observeSession,只有 async 钩子才走 events 通道。改造8 后本兜底只剩 tools/pre-execute
|
|
18
|
+
// 这一处同步钩子使用(isBootstrapPhase 已改走 sessionEvents(session, events),不再经本兜底);
|
|
19
|
+
// usage 账本不再经此函数(改为启动扫描会话日志)。
|
|
20
|
+
if (typeof session.snapshotEvents === 'function') return session.snapshotEvents()
|
|
21
|
+
return []
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Session 读取迁移(T8,0.2.2):经 ctx 能力探测取 sessionQuery 服务,为 async 钩子预取一份不可变
|
|
25
|
+
// SessionObservation(extends Disposable)。拿不到服务 / 方法缺失 / 调用抛错一律返回 undefined,
|
|
26
|
+
// 调用方据此落回上面 sessionEvents 的 @deprecated 同步兜底。宿主 SessionObservation 继承 Disposable、
|
|
27
|
+
// 只提供 [Symbol.dispose]() 而无独立释放方法,故返回的 lease 由调用方在 finally 里先判 lease 与
|
|
28
|
+
// [Symbol.dispose] 可用、再调用 lease[Symbol.dispose]() 释放。
|
|
29
|
+
export async function observeSessionLeaseOf(agent) {
|
|
30
|
+
if (agent === undefined || agent === null) return undefined
|
|
31
|
+
let service
|
|
32
|
+
try {
|
|
33
|
+
const ctx = agent.ctx
|
|
34
|
+
service = ctx !== undefined && ctx !== null && typeof ctx.get === 'function' ? ctx.get('sessionQuery') : undefined
|
|
35
|
+
} catch (error) {
|
|
36
|
+
return undefined
|
|
37
|
+
}
|
|
38
|
+
if (service === undefined || service === null || typeof service.observeSession !== 'function') return undefined
|
|
39
|
+
try {
|
|
40
|
+
return await service.observeSession(agent.session.header.id)
|
|
41
|
+
} catch (error) {
|
|
42
|
+
return undefined
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// 可靠子代理识别(持久标记):优先 session.header,日志 descriptor 扫描兜底。
|
|
47
|
+
// events(可选入参,P1-4):调用方已持有同一份任务内快照时直接传入复用(同一次 tools/pre-execute
|
|
48
|
+
// 内多处判定共用一份快照);不传/传 undefined 时内部自取 snapshotEvents()(其它调用点行为不变)。
|
|
49
|
+
// 语义不变:header 命中即 true;header 未标记时仍按事件流里的 subagent/descriptor 兜底扫描,
|
|
50
|
+
// 主会话(无 origin/delegationDepth 且无 descriptor)每趟仍全量扫、不缓存 false。
|
|
51
|
+
export function isSubagentChild(agent, events) {
|
|
52
|
+
if (agent === undefined || agent === null) return false
|
|
53
|
+
const session = agent.session
|
|
54
|
+
if (session === undefined || session === null) return false
|
|
55
|
+
const header = session.header
|
|
56
|
+
if (header !== undefined && header !== null) {
|
|
57
|
+
if (header.origin === 'subagent') return true
|
|
58
|
+
if (typeof header.delegationDepth === 'number' && header.delegationDepth > 0) return true
|
|
59
|
+
}
|
|
60
|
+
const scanEvents = sessionEvents(session, events)
|
|
61
|
+
for (const event of scanEvents) {
|
|
62
|
+
if (event !== null && typeof event === 'object' && event.type === 'subagent/descriptor') return true
|
|
63
|
+
}
|
|
64
|
+
return false
|
|
65
|
+
}
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
// A/C/M 与 skill catalog 的静态投影 helper:只读输入,不修改 registry/result。
|
|
2
|
+
// SDK renderer 的模块级 import promise 刻意共享;宿主现场读取留在本模块。
|
|
3
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
4
|
+
import { isAbsolute } from 'node:path'
|
|
5
|
+
import { pathToFileURL } from 'node:url'
|
|
6
|
+
import { createRequire } from 'node:module'
|
|
7
|
+
|
|
8
|
+
// creativeMode=false 只改变模型可见投影;registry 绑定和 runtime 安全边界保持不变。
|
|
9
|
+
// 下方显式宿主列表是静态宿主值回归的比对来源。
|
|
10
|
+
export const HOST_CORDIS_TOOLS = Object.freeze([
|
|
11
|
+
'cordis_inspect_list',
|
|
12
|
+
'cordis_inspect_query',
|
|
13
|
+
])
|
|
14
|
+
export const CORDIS_PRESENTATION_TOOLS = HOST_CORDIS_TOOLS
|
|
15
|
+
export const CORDIS_PRESENTATION_TOOL_SET = new Set(CORDIS_PRESENTATION_TOOLS)
|
|
16
|
+
// 两个只读 Cordis 名称继续从呈现层隐藏;runtime binding 不变。
|
|
17
|
+
export const PTC_SECTION_NAME = 'tools:ptc-only'
|
|
18
|
+
export const SDK_SECTION_NAME = 'tools:sdk'
|
|
19
|
+
export const READ_SECTION_NAME = 'tool:read'
|
|
20
|
+
// C=1 创造模式官方 skill 面(skill-filesystem 行 bundledSkillDir 静态注册,换通道原因:
|
|
21
|
+
// 旧 customSkillDirs 经 ctx.fs 扫 asar 抛非 absent 错 → 改 trustedHost/node:fs 通道 + watch: false):
|
|
22
|
+
// 跨版本模型可见投影集合:rc.1旧三名与 rc.2/rc.3 共用的 dsh-doc,不代表任一宿主的物理技能数;C=0 时由 projectSkillCatalogDecision 从模型可见 catalog 隐藏
|
|
23
|
+
// (旧实现是「不在插件里注册」;静态注册后改为投影隐藏——仅名字不可见,按名调用仍可达)。
|
|
24
|
+
export const CREATIVE_SKILL_NAMES = new Set([
|
|
25
|
+
// DSH_COMPAT_RC1:旧 rc.1 兼容三名;删除边界仅限这三名且须另行授权,不含共享安全核心。
|
|
26
|
+
'cordis-plugin-development',
|
|
27
|
+
'editing-cordis-compositions',
|
|
28
|
+
'cordis-composition-reference',
|
|
29
|
+
// DSH_CURRENT_RC2:最新路径的 dsh-doc(rc.2 与 rc.3 共用),不在 rc.1 删除边界内;agent-experience 不加入此集合。
|
|
30
|
+
'dsh-doc',
|
|
31
|
+
])
|
|
32
|
+
|
|
33
|
+
export function sectionOf(sections, name) {
|
|
34
|
+
if (!Array.isArray(sections)) return undefined
|
|
35
|
+
return sections.find((section) => section !== null && typeof section === 'object' && section.name === name)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// HP 首轮的 tool:read 文本由 index.js 的 cfg.bootstrapReadHint 提供,本模块不渲染该最小提示。
|
|
39
|
+
|
|
40
|
+
export function skillCatalogEntriesOf(source) {
|
|
41
|
+
if (source === null || typeof source !== 'object' || source.kind !== 'skill-catalog' || !Array.isArray(source.entries)) return undefined
|
|
42
|
+
const entries = []
|
|
43
|
+
for (const entry of source.entries) {
|
|
44
|
+
if (entry === null || typeof entry !== 'object' || typeof entry.name !== 'string' || typeof entry.description !== 'string') return undefined
|
|
45
|
+
entries.push({ name: entry.name, description: entry.description })
|
|
46
|
+
}
|
|
47
|
+
return entries
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function renderSkillCatalogText(source, entries) {
|
|
51
|
+
const lines = ['<system-reminder>']
|
|
52
|
+
if (source.update === true) {
|
|
53
|
+
lines.push('The available skill catalog changed. This complete catalog replaces every earlier available-skills list in this session:', '', '<available_skills>')
|
|
54
|
+
for (const entry of entries) lines.push('- \`' + entry.name + '\`: ' + entry.description)
|
|
55
|
+
lines.push('</available_skills>', '')
|
|
56
|
+
if (entries.length === 0) {
|
|
57
|
+
lines.push('No skills are currently available through the \`skill\` tool. Do not use names from earlier skill catalogs.')
|
|
58
|
+
lines.push('A user may still invoke a skill directly; its <skill_content> block then appears in this conversation. Follow it, and do not call the \`skill\` tool for it.')
|
|
59
|
+
} else {
|
|
60
|
+
lines.push('Use only names in this replacement catalog. If the user names a listed skill, or the task clearly matches its description, call the \`skill\` tool with the exact name before acting.')
|
|
61
|
+
lines.push('A user may also invoke a skill directly; its <skill_content> block then appears in this conversation. Follow it, and do not call the \`skill\` tool again for that skill.')
|
|
62
|
+
}
|
|
63
|
+
} else {
|
|
64
|
+
lines.push('A skill is a reusable set of task-specific instructions. The following skills are available in this session:', '', '<available_skills>')
|
|
65
|
+
for (const entry of entries) lines.push('- \`' + entry.name + '\`: ' + entry.description)
|
|
66
|
+
lines.push('</available_skills>', '')
|
|
67
|
+
lines.push('If the user names a skill, or the task clearly matches its description, call the \`skill\` tool with the exact skill name before taking task actions. This catalog contains summaries only; do not infer or follow the skill instructions until it has been loaded.')
|
|
68
|
+
lines.push('A user may also invoke a skill directly; its <skill_content> block then appears in this conversation. Follow it, and do not call the \`skill\` tool again for that skill.')
|
|
69
|
+
}
|
|
70
|
+
lines.push('</system-reminder>')
|
|
71
|
+
return lines.join('\n')
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function projectSkillCatalogDecision(decision) {
|
|
75
|
+
if (decision === null || typeof decision !== 'object' || decision.kind === 'reject' || !Array.isArray(decision.messages)) return decision
|
|
76
|
+
let changed = false
|
|
77
|
+
const messages = decision.messages.map((message) => {
|
|
78
|
+
if (message === null || typeof message !== 'object') return message
|
|
79
|
+
const entries = skillCatalogEntriesOf(message.source)
|
|
80
|
+
if (entries === undefined) return message
|
|
81
|
+
const visible = entries.filter((entry) => !CREATIVE_SKILL_NAMES.has(entry.name))
|
|
82
|
+
if (visible.length === entries.length) return message
|
|
83
|
+
changed = true
|
|
84
|
+
const source = { ...message.source, entries: visible }
|
|
85
|
+
const text = renderSkillCatalogText(source, visible)
|
|
86
|
+
let replaced = false
|
|
87
|
+
const content = Array.isArray(message.content)
|
|
88
|
+
? message.content.map((part) => {
|
|
89
|
+
if (!replaced && part !== null && typeof part === 'object' && part.type === 'text') {
|
|
90
|
+
replaced = true
|
|
91
|
+
return { ...part, text }
|
|
92
|
+
}
|
|
93
|
+
return part
|
|
94
|
+
})
|
|
95
|
+
: []
|
|
96
|
+
if (!replaced) content.push({ type: 'text', text })
|
|
97
|
+
return { ...message, source, content }
|
|
98
|
+
})
|
|
99
|
+
return changed ? { ...decision, messages } : decision
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function isCordisPresentationTool(name) {
|
|
103
|
+
return typeof name === 'string' && CORDIS_PRESENTATION_TOOL_SET.has(name)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function filteredCordisSchemas(schemas) {
|
|
107
|
+
if (!Array.isArray(schemas)) return []
|
|
108
|
+
return schemas.filter((schema) => schema !== null && typeof schema === 'object' && !isCordisPresentationTool(schema.name))
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function hasSection(sections, name) {
|
|
112
|
+
return Array.isArray(sections) && sections.some((section) => section !== null && typeof section === 'object' && section.name === name)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function hasNonEmptySection(sections, name) {
|
|
116
|
+
return Array.isArray(sections) && sections.some((section) => section !== null && typeof section === 'object' && section.name === name && typeof section.text === 'string' && section.text !== '')
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// 只投影 PromptAssembly 的模型可见副本;不修改 registry、result 或其 schema。
|
|
120
|
+
export function projectAssemblyForPresentation(assembly, schemas, options = {}) {
|
|
121
|
+
if (assembly === null || typeof assembly !== 'object') return assembly
|
|
122
|
+
const hideCordis = options.hideCordis !== false
|
|
123
|
+
const ptcOnly = options.ptcOnly === true
|
|
124
|
+
const keepSectionNames = options.keepSectionNames instanceof Set ? options.keepSectionNames : null
|
|
125
|
+
const schemaNames = Array.isArray(schemas)
|
|
126
|
+
? new Set(schemas.filter((schema) => schema !== null && typeof schema === 'object' && typeof schema.name === 'string').map((schema) => schema.name))
|
|
127
|
+
: null
|
|
128
|
+
const tools = Array.isArray(assembly.tools)
|
|
129
|
+
? assembly.tools.filter((tool) => {
|
|
130
|
+
if (tool === null || typeof tool !== 'object' || typeof tool.name !== 'string') return false
|
|
131
|
+
if (hideCordis && isCordisPresentationTool(tool.name)) return false
|
|
132
|
+
if (ptcOnly && tool.name !== 'run_code') return false
|
|
133
|
+
return schemaNames === null || schemaNames.has(tool.name)
|
|
134
|
+
})
|
|
135
|
+
: assembly.tools
|
|
136
|
+
const sections = Array.isArray(assembly.sections)
|
|
137
|
+
? assembly.sections
|
|
138
|
+
.filter((section) => section !== null && typeof section === 'object' && section.name !== 'tool:cordis' && (keepSectionNames === null || keepSectionNames.has(section.name)))
|
|
139
|
+
.map((section) => section.name === SDK_SECTION_NAME && typeof options.sdkText === 'string'
|
|
140
|
+
? { ...section, text: options.sdkText }
|
|
141
|
+
: section)
|
|
142
|
+
: assembly.sections
|
|
143
|
+
return { ...assembly, sections, tools }
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function sdkSchemasForRendering(schemas) {
|
|
147
|
+
return filteredCordisSchemas(schemas)
|
|
148
|
+
.filter((schema) => schema.name !== 'run_code')
|
|
149
|
+
.map((schema) => ({
|
|
150
|
+
...schema,
|
|
151
|
+
output: schema.output !== undefined ? schema.output : { type: 'object', additionalProperties: true },
|
|
152
|
+
}))
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function errorWithCause(message, cause) {
|
|
156
|
+
const error = new Error(message)
|
|
157
|
+
error.cause = cause
|
|
158
|
+
return error
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function assertInstallAnchor(installAnchor) {
|
|
162
|
+
if (typeof installAnchor !== 'string' || installAnchor.trim() === '' || !isAbsolute(installAnchor) || installAnchor.split(/[\\/]/).pop() !== 'package.json') {
|
|
163
|
+
throw new Error('extra-plan: installAnchor 必须是绝对 dsh 应用 package.json 文件')
|
|
164
|
+
}
|
|
165
|
+
if (!existsSync(installAnchor)) throw new Error('extra-plan: installAnchor 不存在:' + installAnchor)
|
|
166
|
+
let manifest
|
|
167
|
+
try {
|
|
168
|
+
manifest = JSON.parse(readFileSync(installAnchor, 'utf8'))
|
|
169
|
+
} catch (cause) {
|
|
170
|
+
throw errorWithCause('extra-plan: installAnchor manifest 无法读取:' + installAnchor, cause)
|
|
171
|
+
}
|
|
172
|
+
if (manifest === null || typeof manifest !== 'object' || manifest.name !== '@deepseek-ai/dsh') {
|
|
173
|
+
throw new Error('extra-plan: installAnchor 不是 @deepseek-ai/dsh 应用 package.json:' + installAnchor)
|
|
174
|
+
}
|
|
175
|
+
return installAnchor
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// 单候选语义:目标包(dsh-tool-executor-ptc)只有一个入口,解析成功即返回该入口路径(不再是数组)。
|
|
179
|
+
export function dshToolsEntryPath(installAnchor) {
|
|
180
|
+
const anchor = assertInstallAnchor(installAnchor)
|
|
181
|
+
try {
|
|
182
|
+
return createRequire(anchor).resolve('@deepseek-ai/dsh-tool-executor-ptc')
|
|
183
|
+
} catch (cause) {
|
|
184
|
+
throw errorWithCause('extra-plan: 无法从 installAnchor 解析 @deepseek-ai/dsh-tool-executor-ptc:' + anchor, cause)
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const sdkRendererModulePromises = new Map()
|
|
189
|
+
export function loadSdkRendererModule(installAnchor) {
|
|
190
|
+
const anchor = assertInstallAnchor(installAnchor)
|
|
191
|
+
const cached = sdkRendererModulePromises.get(anchor)
|
|
192
|
+
if (cached !== undefined) return cached
|
|
193
|
+
// 入口解析在 async 包装外同步抛:调用方须经 async 函数(如 resolveToolsSdkRenderer)承接,否则会变成同步异常。
|
|
194
|
+
const entry = dshToolsEntryPath(anchor)
|
|
195
|
+
const pending = (async () => {
|
|
196
|
+
try {
|
|
197
|
+
return await import(pathToFileURL(entry).href)
|
|
198
|
+
} catch (cause) {
|
|
199
|
+
throw errorWithCause('extra-plan: dsh-tool-executor-ptc SDK renderer 不可用(installAnchor=' + anchor + ')', cause)
|
|
200
|
+
}
|
|
201
|
+
})()
|
|
202
|
+
sdkRendererModulePromises.set(anchor, pending)
|
|
203
|
+
pending.catch(() => {
|
|
204
|
+
if (sdkRendererModulePromises.get(anchor) === pending) sdkRendererModulePromises.delete(anchor)
|
|
205
|
+
})
|
|
206
|
+
return pending
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// 返回当前语言对应的官方 renderer 函数引用;模块 promise 按 installAnchor 隔离缓存。
|
|
210
|
+
export async function resolveToolsSdkRenderer(language = 'typescript', installAnchor) {
|
|
211
|
+
const rendererModule = await loadSdkRendererModule(installAnchor)
|
|
212
|
+
const render = language === 'python' ? rendererModule.renderToolsSdkPy : rendererModule.renderToolsSdk
|
|
213
|
+
if (typeof render !== 'function') throw new Error(`extra-plan: unsupported SDK renderer language ${language}`)
|
|
214
|
+
return render
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// 只接收已过滤 schema,整体调用官方 renderer 重建 tools:sdk,不从原始文本删块。
|
|
218
|
+
export async function renderFilteredToolsSdk(schemas, language = 'typescript', installAnchor) {
|
|
219
|
+
const render = await resolveToolsSdkRenderer(language, installAnchor)
|
|
220
|
+
return render(sdkSchemasForRendering(schemas))
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export function toolRegistryOf(agent) {
|
|
224
|
+
if (agent === undefined || agent === null || agent.ctx === undefined || agent.ctx === null || typeof agent.ctx.get !== 'function') return undefined
|
|
225
|
+
try {
|
|
226
|
+
const tools = agent.ctx.get('tools')
|
|
227
|
+
return tools !== null && typeof tools === 'object' ? tools : undefined
|
|
228
|
+
} catch (error) {
|
|
229
|
+
return undefined
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export function toolSdkSchemasOf(agent) {
|
|
234
|
+
const tools = toolRegistryOf(agent)
|
|
235
|
+
if (tools === undefined) return undefined
|
|
236
|
+
try {
|
|
237
|
+
// 0.2.2 取数口:definitions(agent)(agent 是合法 ScopeKey)。parameters 原样透传不 snapshot
|
|
238
|
+
// (等于宿主 projectToolSchema(def, false) 口径);deferLoading 仅 true 时携带;
|
|
239
|
+
// output 取 definition.output.schema,缺省回退宽松对象 schema。
|
|
240
|
+
if (typeof tools.definitions === 'function') {
|
|
241
|
+
const definitions = tools.definitions(agent)
|
|
242
|
+
if (Array.isArray(definitions)) {
|
|
243
|
+
return definitions
|
|
244
|
+
.filter((definition) => definition !== null
|
|
245
|
+
&& typeof definition === 'object'
|
|
246
|
+
&& definition.name !== 'run_code'
|
|
247
|
+
&& !isCordisPresentationTool(definition.name))
|
|
248
|
+
.map((definition) => ({
|
|
249
|
+
name: definition.name,
|
|
250
|
+
description: definition.description,
|
|
251
|
+
parameters: definition.parameters,
|
|
252
|
+
...(definition.deferLoading === true ? { deferLoading: true } : {}),
|
|
253
|
+
output: definition.output?.schema ?? { type: 'object', additionalProperties: true },
|
|
254
|
+
}))
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (typeof tools.schemas !== 'function') return undefined
|
|
258
|
+
const schemas = tools.schemas(agent)
|
|
259
|
+
if (!Array.isArray(schemas)) return undefined
|
|
260
|
+
return schemas.filter((schema) => schema !== null && typeof schema === 'object' && schema.name !== 'run_code' && !isCordisPresentationTool(schema.name)).map((schema) => ({
|
|
261
|
+
...schema,
|
|
262
|
+
output: schema.output !== undefined ? schema.output : { type: 'object', additionalProperties: true },
|
|
263
|
+
}))
|
|
264
|
+
} catch (error) {
|
|
265
|
+
return undefined
|
|
266
|
+
}
|
|
267
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// 配置组件的内部 Client carrier。
|
|
2
|
+
// 仅由 settings 通过宿主官方 Loader Group 动态挂载;不提供 bundle row、公开 export 或业务副作用。
|
|
3
|
+
export const name = 'dsh-extra-plan-client-carrier'
|
|
4
|
+
export const inject = []
|
|
5
|
+
export function apply() {
|
|
6
|
+
// dsh.client package manifest 驱动 lib/client.js 加载;此 entry 本身保持空实现。
|
|
7
|
+
}
|