dsh-better-goal 0.2.0 → 0.2.1
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/README.md +10 -11
- package/docs/DEVELOPMENT.md +4 -11
- package/package.json +1 -1
- package/src/client.js +3 -49
- package/src/config.js +5 -3
- package/src/index.js +1 -9
- package/src/ask-user.js +0 -223
package/README.md
CHANGED
|
@@ -27,13 +27,13 @@
|
|
|
27
27
|
| --- | --- | --- |
|
|
28
28
|
| **缺密钥、缺权限,必须由人介入** | 自主续跑时不能直接自行暂停;报告受阻有轮数门槛,目标仍在进行中就会继续排下一轮 | 立即 `pause`,在回复里说明缺什么,停止自动续跑;不会为了“完成”而伪造、跳过或缩小范围 |
|
|
29
29
|
| **阻塞已经确认,还要不要凑轮数** | 自主续跑时,默认至少经过 **3 个目标轮次**才接受受阻报告;刚开始就确认缺密钥,也不能立即用它停下 | 不设这个门槛;明确无法自行解决时即可停下,不必重复空转 |
|
|
30
|
-
| **向用户提问后没人回答** | 默认提问会阻塞当前执行;需另行启用定时提问模式才有超时 |
|
|
31
|
-
| **希望始终等人确认** | 默认阻塞等待;定时模式另外配置 |
|
|
30
|
+
| **向用户提问后没人回答** | 默认提问会阻塞当前执行;需另行启用定时提问模式才有超时 | 由独立的 **dsh-better-ask-user** 统一管理提问等待;超时后仍可回答,Agent 可先做不依赖答案的工作 |
|
|
31
|
+
| **希望始终等人确认** | 默认阻塞等待;定时模式另外配置 | 在 **dsh-better-ask-user** 设置中把等待设为 **`-1`**,即可一直等待;超时从不代表同意 |
|
|
32
32
|
| **阻塞解除后继续** | 模型恢复受直接人类请求限制;已暂停目标由 Web 或 `/goal resume` 恢复 | 用户或 Agent 都可明确继续,不要求先出现一条新的人类消息;保留原目标与已用轮数 |
|
|
33
33
|
| **轮数控制** | 默认每个目标上限 **256 轮**,达到配置上限会停止;但有限的重试额度不等于能够立即报告阻塞 | **Agent 决定 / 无上限 / 固定数字**三档策略;主动暂停与轮数上限分开处理 |
|
|
34
34
|
| **模型如何管理目标** | 分散在 3 个工具中,操作还受状态与授权条件限制 | 一个工具、五个动作:**获取 / 覆盖写入 / 暂停 / 成功 / 继续** |
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
两者都能保存目标、跨轮推进、暂停和恢复;**这些不是本插件独有的能力**。提问等待已拆到独立的 **dsh-better-ask-user**,本插件只负责目标、续跑与轮数策略。
|
|
37
37
|
|
|
38
38
|
### 例子:任务卡在必需的 API 密钥
|
|
39
39
|
|
|
@@ -170,9 +170,9 @@ flowchart TD
|
|
|
170
170
|
| --- | --- |
|
|
171
171
|
| **轮数上限策略**<br>`roundLimitMode`<br>默认 **`agent`**(由 Agent 决定) | 默认让 Agent 决定;创建时没有给上限,就不设轮数上限。 |
|
|
172
172
|
| **固定轮数上限**<br>`roundLimit`<br>默认 **`256`** | 只在“固定轮数”模式生效;达到后停止自动续跑。 |
|
|
173
|
-
|
|
|
173
|
+
| **旧提问超时字段**<br>`questionTimeoutSeconds` | **已弃用,仅保留读取**;新的等待、续期和超时行为在 **dsh-better-ask-user** 中配置。 |
|
|
174
174
|
|
|
175
|
-
> **默认组合:`agent` + `256
|
|
175
|
+
> **默认组合:`agent` + `256`。** 其中 `256` 只在切到“固定轮数”后生效。旧配置里的 `120` 秒只用于一次性迁移到 ask-user,不再影响本插件的运行。
|
|
176
176
|
|
|
177
177
|
设置只影响之后的新轮次和新提问:不重置已用轮数,也不会自动恢复已经停下的目标。
|
|
178
178
|
|
|
@@ -195,12 +195,11 @@ flowchart TD
|
|
|
195
195
|
|
|
196
196
|
达到上限时,目标会变为受阻并停止自动续跑。把上限调高后,可以 `/goal resume` 继续;把上限调到已用轮数以下不会让计数倒退,只会让运行中的目标停下。
|
|
197
197
|
|
|
198
|
-
###
|
|
198
|
+
### 提问等待
|
|
199
199
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
- 修改设置只影响新发起的等待;已经开始等待的问题保留原期限。
|
|
200
|
+
提问等待、输入续期、Web 卡片和超时行为已经迁移到独立插件 **dsh-better-ask-user**。本插件不再注册 `ask_user_question` 影子,也不再消费 `questionTimeoutSeconds`;该字段只作为旧配置保留,供 ask-user 做一次性迁移。
|
|
201
|
+
|
|
202
|
+
超时仍然不是同意,也不是跳过。原生 pending 行为会让问题保持可回答;默认选择模式会把结果明确标记为自动选择,模型侧会收到警告。
|
|
204
203
|
|
|
205
204
|
## 恢复、暂停与完成
|
|
206
205
|
|
|
@@ -243,7 +242,7 @@ flowchart TD
|
|
|
243
242
|
<details>
|
|
244
243
|
<summary><strong>提问超时是不是等于同意或跳过?</strong></summary>
|
|
245
244
|
|
|
246
|
-
不是。超时只表示这一批回答还没到;问题仍可回答,Agent 不应把超时当成授权或默认选项。真正被答案阻塞时,Agent
|
|
245
|
+
不是。超时只表示这一批回答还没到;问题仍可回答,Agent 不应把超时当成授权或默认选项。真正被答案阻塞时,Agent 应报告失败并等你处理,而不是伪造答案。具体的等待与默认选择规则见 **dsh-better-ask-user**。
|
|
247
246
|
|
|
248
247
|
</details>
|
|
249
248
|
|
package/docs/DEVELOPMENT.md
CHANGED
|
@@ -112,7 +112,7 @@ GoalBar 不新建 goal,只通过正式 `remote.goals` RPC 做编辑、暂停
|
|
|
112
112
|
| --- | --- | --- | --- |
|
|
113
113
|
| `roundLimitMode` | `agent` | `agent` / `unlimited` / `fixed` | 选择解释轮数上限的策略。 |
|
|
114
114
|
| `roundLimit` | `256` | 正安全整数 | 只在 `fixed` 模式使用。 |
|
|
115
|
-
| `questionTimeoutSeconds` | `120` | `-1` 或 `1..2147483` 的整数 |
|
|
115
|
+
| `questionTimeoutSeconds` | `120` | `-1` 或 `1..2147483` 的整数 | **已弃用、仅保留读取**;新的提问等待由 dsh-better-ask-user 管理,用于旧配置迁移。 |
|
|
116
116
|
|
|
117
117
|
`resolveRoundLimit` 的解析顺序:
|
|
118
118
|
|
|
@@ -122,16 +122,9 @@ GoalBar 不新建 goal,只通过正式 `remote.goals` RPC 做编辑、暂停
|
|
|
122
122
|
|
|
123
123
|
工具 schema 会在下一个 step 刷新:`agent` 模式暴露可选的 `max_goal_rounds`,`fixed` / `unlimited` 模式不暴露。驱动在每次准入时重新读取 live config,所以热设置只影响之后的新轮次和操作,不重置计数,也不自动恢复已停止的 goal。
|
|
124
124
|
|
|
125
|
-
##
|
|
125
|
+
## 提问等待
|
|
126
126
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
- 仅当 goal 为 `active` 且 activation 为 `armed` 时接管;其他情况完全委托给原工具。
|
|
130
|
-
- 复用宿主当前工具的 `questions` schema,不导入 `@deepseek-ai/dsh-tool-ask-user`,也不给“无提问”预设新增工具。
|
|
131
|
-
- 每次新等待读取一次 `questionTimeoutSeconds`;正在等待的提问保留原期限。
|
|
132
|
-
- 宿主设置覆盖模型传入的等待时间;`-1` 走不带超时的 `userQuestions.ask`。
|
|
133
|
-
- 超时返回原生 pending 结果,问题仍可回答;模型侧提示明确说明“pending, not a skipped answer”,不得把超时当授权或默认答案。
|
|
134
|
-
- 超时后用户仍可提交答案,迟到答案由原生 `userQuestions` 服务结算。
|
|
127
|
+
提问等待已迁移到独立的 **dsh-better-ask-user**。本仓库不再包含 `src/ask-user.js`,也不再注册 `ask_user_question` 影子或消费 `questionTimeoutSeconds`。保留该 Config 字段仅用于读取旧配置,供 ask-user 的一次性迁移使用;设置页不再展示该字段。
|
|
135
128
|
|
|
136
129
|
## 完成与暂停
|
|
137
130
|
|
|
@@ -188,7 +181,7 @@ Apollo 本地开发时,先在其根目录执行 `npm install`。`dsh plugin ad
|
|
|
188
181
|
npm test
|
|
189
182
|
```
|
|
190
183
|
|
|
191
|
-
测试覆盖 Config 默认值与边界、round policy、goal service 的持久 schema/replay/CAS、driver 的续跑与中断、单工具动作、命令、GoalBar
|
|
184
|
+
测试覆盖 Config 默认值与边界、round policy、goal service 的持久 schema/replay/CAS、driver 的续跑与中断、单工具动作、命令、GoalBar 和设置页。提问等待的影子测试已随实现迁移到 ask-user。
|
|
192
185
|
|
|
193
186
|
宿主冒烟需要指向已安装的 DSH 包:
|
|
194
187
|
|
package/package.json
CHANGED
package/src/client.js
CHANGED
|
@@ -53,8 +53,6 @@ window.__ModuleLoader__.load({
|
|
|
53
53
|
const ROW_CONFIG_KEYS = Object.freeze([ROW_KEY, APOLLO_ROW_KEY, SCOPED_APOLLO_ROW_KEY])
|
|
54
54
|
const ROUND_LIMIT_MODES = Object.freeze(['agent', 'unlimited', 'fixed'])
|
|
55
55
|
const DEFAULT_ROUND_LIMIT = 256
|
|
56
|
-
const DEFAULT_QUESTION_TIMEOUT_SECONDS = 120
|
|
57
|
-
const MAX_QUESTION_TIMEOUT_SECONDS = 2147483
|
|
58
56
|
const UNLIMITED_ROUNDS = Number.MAX_SAFE_INTEGER
|
|
59
57
|
const STYLE_GOAL = 'dsh-better-goal/goal.css'
|
|
60
58
|
const STYLE_SETTINGS = 'dsh-better-goal/settings.css'
|
|
@@ -63,7 +61,7 @@ window.__ModuleLoader__.load({
|
|
|
63
61
|
|
|
64
62
|
/** Simplified Chinese dictionary (key-set source of truth). */
|
|
65
63
|
const zh = {
|
|
66
|
-
summary: '
|
|
64
|
+
summary: '独立目标条与轮数上限策略',
|
|
67
65
|
waitingForm: '正在等待宿主提供这一行的配置快照…',
|
|
68
66
|
noFormHint: '长时间停在这里说明宿主没有为这一行提供可编辑配置:本部署没组合 dsh-settings/config-editor,或本行的 Config 未被识别。',
|
|
69
67
|
loading: '正在读取配置…',
|
|
@@ -113,7 +111,7 @@ window.__ModuleLoader__.load({
|
|
|
113
111
|
|
|
114
112
|
/** English dictionary, checked complete against the zh key set. */
|
|
115
113
|
const en = {
|
|
116
|
-
summary: 'Standalone GoalBar
|
|
114
|
+
summary: 'Standalone GoalBar and round-cap policy',
|
|
117
115
|
waitingForm: 'Waiting for the host to provide this row’s configuration snapshot…',
|
|
118
116
|
noFormHint: 'Staying here means the host exposes no editable config for this row: dsh-settings/config-editor is not composed, or this row’s Config was not recognized.',
|
|
119
117
|
loading: 'Loading configuration…',
|
|
@@ -303,11 +301,6 @@ window.__ModuleLoader__.load({
|
|
|
303
301
|
return typeof value === 'number' && Number.isSafeInteger(value) && value >= 1
|
|
304
302
|
}
|
|
305
303
|
|
|
306
|
-
function isValidQuestionTimeout(value) {
|
|
307
|
-
if (value === -1) return true
|
|
308
|
-
return Number.isSafeInteger(value) && value >= 1 && value <= MAX_QUESTION_TIMEOUT_SECONDS
|
|
309
|
-
}
|
|
310
|
-
|
|
311
304
|
function isUnlimitedCap(value) {
|
|
312
305
|
return value === UNLIMITED_ROUNDS
|
|
313
306
|
}
|
|
@@ -756,9 +749,6 @@ window.__ModuleLoader__.load({
|
|
|
756
749
|
return {
|
|
757
750
|
roundLimitMode: ROUND_LIMIT_MODES.includes(source.roundLimitMode) ? source.roundLimitMode : 'agent',
|
|
758
751
|
roundLimit: isPositiveSafeInteger(source.roundLimit) ? source.roundLimit : DEFAULT_ROUND_LIMIT,
|
|
759
|
-
questionTimeoutSeconds: isValidQuestionTimeout(source.questionTimeoutSeconds)
|
|
760
|
-
? source.questionTimeoutSeconds
|
|
761
|
-
: DEFAULT_QUESTION_TIMEOUT_SECONDS,
|
|
762
752
|
}
|
|
763
753
|
}
|
|
764
754
|
|
|
@@ -767,14 +757,12 @@ window.__ModuleLoader__.load({
|
|
|
767
757
|
return {
|
|
768
758
|
roundLimitMode: config.roundLimitMode,
|
|
769
759
|
roundLimit: String(config.roundLimit),
|
|
770
|
-
questionTimeoutSeconds: String(config.questionTimeoutSeconds),
|
|
771
760
|
}
|
|
772
761
|
}
|
|
773
762
|
|
|
774
763
|
function sameConfig(left, right) {
|
|
775
764
|
return left.roundLimitMode === right.roundLimitMode
|
|
776
765
|
&& left.roundLimit === right.roundLimit
|
|
777
|
-
&& left.questionTimeoutSeconds === right.questionTimeoutSeconds
|
|
778
766
|
}
|
|
779
767
|
|
|
780
768
|
function parsePositiveSafeIntegerText(text) {
|
|
@@ -785,14 +773,6 @@ window.__ModuleLoader__.load({
|
|
|
785
773
|
return Number.isSafeInteger(value) && value >= 1 && value <= UNLIMITED_ROUNDS ? value : undefined
|
|
786
774
|
}
|
|
787
775
|
|
|
788
|
-
function parseQuestionTimeoutText(text) {
|
|
789
|
-
if (typeof text !== 'string') return undefined
|
|
790
|
-
const trimmed = text.trim()
|
|
791
|
-
if (trimmed === '-1') return -1
|
|
792
|
-
const value = parsePositiveSafeIntegerText(trimmed)
|
|
793
|
-
return value !== undefined && value <= MAX_QUESTION_TIMEOUT_SECONDS ? value : undefined
|
|
794
|
-
}
|
|
795
|
-
|
|
796
776
|
/**
|
|
797
777
|
* Validate one text draft and produce the JSON value to write.
|
|
798
778
|
* The hidden round-limit text is ignored outside `fixed` mode: the host
|
|
@@ -806,13 +786,10 @@ window.__ModuleLoader__.load({
|
|
|
806
786
|
if (parsed === undefined) return { error: 'invalidRoundLimit' }
|
|
807
787
|
roundLimit = parsed
|
|
808
788
|
}
|
|
809
|
-
const questionTimeoutSeconds = parseQuestionTimeoutText(draft.questionTimeoutSeconds)
|
|
810
|
-
if (questionTimeoutSeconds === undefined) return { error: 'invalidQuestionTimeout' }
|
|
811
789
|
return {
|
|
812
790
|
value: {
|
|
813
791
|
roundLimitMode: draft.roundLimitMode,
|
|
814
792
|
roundLimit,
|
|
815
|
-
questionTimeoutSeconds,
|
|
816
793
|
},
|
|
817
794
|
}
|
|
818
795
|
}
|
|
@@ -826,9 +803,6 @@ window.__ModuleLoader__.load({
|
|
|
826
803
|
if (host.roundLimit !== next.roundLimit) {
|
|
827
804
|
ops.push({ op: 'set', path: ['roundLimit'], value: next.roundLimit })
|
|
828
805
|
}
|
|
829
|
-
if (host.questionTimeoutSeconds !== next.questionTimeoutSeconds) {
|
|
830
|
-
ops.push({ op: 'set', path: ['questionTimeoutSeconds'], value: next.questionTimeoutSeconds })
|
|
831
|
-
}
|
|
832
806
|
return ops
|
|
833
807
|
}
|
|
834
808
|
|
|
@@ -903,7 +877,6 @@ window.__ModuleLoader__.load({
|
|
|
903
877
|
}
|
|
904
878
|
|
|
905
879
|
const roundLimitValue = parsePositiveSafeIntegerText(live.roundLimit)
|
|
906
|
-
const timeoutValue = parseQuestionTimeoutText(live.questionTimeoutSeconds)
|
|
907
880
|
const status = notice !== null
|
|
908
881
|
? h('span', { className: 'dbg-status', 'data-tone': notice.tone, role: notice.tone === 'error' ? 'alert' : 'status' }, notice.text)
|
|
909
882
|
: invalid !== null
|
|
@@ -957,26 +930,7 @@ window.__ModuleLoader__.load({
|
|
|
957
930
|
onChange: (event) => edit((current) => ({ ...current, roundLimit: event.target.value })),
|
|
958
931
|
}))
|
|
959
932
|
: null,
|
|
960
|
-
|
|
961
|
-
id: 'dsh-better-goal-question-timeout',
|
|
962
|
-
label: tr(t, 'questionTimeoutLabel'),
|
|
963
|
-
hint: timeoutValue === -1
|
|
964
|
-
? `${tr(t, 'questionTimeoutHint')} ${tr(t, 'questionTimeoutNever')}`
|
|
965
|
-
: tr(t, 'questionTimeoutHint'),
|
|
966
|
-
},
|
|
967
|
-
h('input', {
|
|
968
|
-
className: 'dbg-input',
|
|
969
|
-
id: 'dsh-better-goal-question-timeout',
|
|
970
|
-
type: 'number',
|
|
971
|
-
min: -1,
|
|
972
|
-
max: MAX_QUESTION_TIMEOUT_SECONDS,
|
|
973
|
-
step: 1,
|
|
974
|
-
inputMode: 'numeric',
|
|
975
|
-
value: live.questionTimeoutSeconds,
|
|
976
|
-
disabled: !writable || busy,
|
|
977
|
-
'aria-invalid': invalid === 'invalidQuestionTimeout' ? true : undefined,
|
|
978
|
-
onChange: (event) => edit((current) => ({ ...current, questionTimeoutSeconds: event.target.value })),
|
|
979
|
-
}))),
|
|
933
|
+
),
|
|
980
934
|
h('div', { className: 'dbg-footer' },
|
|
981
935
|
h('button', {
|
|
982
936
|
type: 'button',
|
package/src/config.js
CHANGED
|
@@ -9,8 +9,10 @@
|
|
|
9
9
|
* `agent` (the model may pass `max_goal_rounds`), `unlimited` (the
|
|
10
10
|
* compatibility sentinel), or `fixed` (the configured `roundLimit`).
|
|
11
11
|
* - `roundLimit` is the cap used only by `fixed` mode.
|
|
12
|
-
* - `questionTimeoutSeconds`
|
|
13
|
-
*
|
|
12
|
+
* - `questionTimeoutSeconds` is deprecated and load-only. It is retained so
|
|
13
|
+
* existing profiles and the ask-user migration can read the old value; the
|
|
14
|
+
* goal plugin no longer consumes it and the settings page no longer edits
|
|
15
|
+
* it. The standalone ask-user plugin owns question waits.
|
|
14
16
|
*
|
|
15
17
|
* Every field is `.volatile()`: changing it hot-swaps the stable reference on
|
|
16
18
|
* the live fiber instead of remounting the row. Consumers (`readConfig`,
|
|
@@ -75,7 +77,7 @@ export const Config = z.object({
|
|
|
75
77
|
roundLimitMode: z.union(ROUND_LIMIT_MODES).default(DEFAULTS.roundLimitMode).volatile(),
|
|
76
78
|
/** Fixed cap; used only when `roundLimitMode` is `fixed`. */
|
|
77
79
|
roundLimit: RoundLimit.volatile(),
|
|
78
|
-
/**
|
|
80
|
+
/** Deprecated/load-only goal-question wait; kept for profile migration. */
|
|
79
81
|
questionTimeoutSeconds: QuestionTimeout.volatile(),
|
|
80
82
|
})
|
|
81
83
|
|
package/src/index.js
CHANGED
|
@@ -2,7 +2,6 @@ import { Config, readConfig as snapshotConfig, UNLIMITED_ROUNDS } from './config
|
|
|
2
2
|
import { GoalService } from './service.js'
|
|
3
3
|
import { createGoalTool, LEGACY_GOAL_TOOLS } from './tool.js'
|
|
4
4
|
import { registerGoalCommand } from './commands.js'
|
|
5
|
-
import { createGoalAskTool } from './ask-user.js'
|
|
6
5
|
import { apply as mountDriver } from './driver.js'
|
|
7
6
|
import { mountSettings } from './settings.js'
|
|
8
7
|
|
|
@@ -33,12 +32,6 @@ export function apply(ctx, config = {}) {
|
|
|
33
32
|
state.goalDispose = tools.register(createGoalTool(state.actor, readConfig))
|
|
34
33
|
state.mode = settings.roundLimitMode
|
|
35
34
|
}
|
|
36
|
-
state.askDispose?.()
|
|
37
|
-
state.askDispose = undefined
|
|
38
|
-
const baseAsk = tools.get('ask_user_question', state.agent)
|
|
39
|
-
if (baseAsk && state.actor.get('userQuestions')) {
|
|
40
|
-
state.askDispose = tools.register(createGoalAskTool(state.actor, baseAsk, readConfig))
|
|
41
|
-
}
|
|
42
35
|
}
|
|
43
36
|
function setup(agent) {
|
|
44
37
|
if (states.has(agent)) return states.get(agent)
|
|
@@ -56,7 +49,6 @@ export function apply(ctx, config = {}) {
|
|
|
56
49
|
return state
|
|
57
50
|
}
|
|
58
51
|
function cleanup(state) {
|
|
59
|
-
state.askDispose?.()
|
|
60
52
|
state.goalDispose?.()
|
|
61
53
|
state.commandDispose?.()
|
|
62
54
|
state.restrictDispose?.()
|
|
@@ -77,7 +69,7 @@ export function apply(ctx, config = {}) {
|
|
|
77
69
|
const schemas = new Map(state.actor.get('tools').schemas(state.agent).map((tool) => [tool.name, tool]))
|
|
78
70
|
assembly.tools = assembly.tools.flatMap((tool) => {
|
|
79
71
|
if (LEGACY_GOAL_TOOLS.includes(tool.name)) return []
|
|
80
|
-
if (tool.name === 'goal'
|
|
72
|
+
if (tool.name === 'goal') {
|
|
81
73
|
const schema = schemas.get(tool.name)
|
|
82
74
|
return schema ? [schema] : []
|
|
83
75
|
}
|
package/src/ask-user.js
DELETED
|
@@ -1,223 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Goal-scoped timed `ask_user_question` shadow.
|
|
3
|
-
*
|
|
4
|
-
* The root plugin keeps this definition stable whenever the composition
|
|
5
|
-
* already registered an ask tool. Execution delegates to that tool outside
|
|
6
|
-
* an active, armed goal. The definition reuses that live tool's `questions`
|
|
7
|
-
* schema, declares the native timed-wait marker, and uses the host's volatile
|
|
8
|
-
* `questionTimeoutSeconds` setting for every NEW wait. Writes and goal state
|
|
9
|
-
* remain outside this module.
|
|
10
|
-
*
|
|
11
|
-
* The tool deliberately does not import `@deepseek-ai/dsh-tool-ask-user`:
|
|
12
|
-
* only the public user-questions service and the native marker are used, so
|
|
13
|
-
* this plugin never depends on the host tool's runtime row.
|
|
14
|
-
*
|
|
15
|
-
* @module dsh-better-goal/ask-user
|
|
16
|
-
*/
|
|
17
|
-
import {
|
|
18
|
-
ToolArgsError,
|
|
19
|
-
validateJsonSchemaValue,
|
|
20
|
-
} from '@deepseek-ai/dsh-tools'
|
|
21
|
-
import { TIMED_WAIT_PARAMETER } from '@deepseek-ai/dsh-user-questions'
|
|
22
|
-
|
|
23
|
-
/** Default foreground goal-question wait in seconds. */
|
|
24
|
-
export const DEFAULT_QUESTION_TIMEOUT_SECONDS = 120
|
|
25
|
-
|
|
26
|
-
/** Largest question wait a native platform timer can represent, in seconds. */
|
|
27
|
-
export const MAX_QUESTION_TIMEOUT_SECONDS = 2147483
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Model-visible notice carried by a timed-out (still answerable) result. This
|
|
31
|
-
* is intentionally the native tool's wording: the recorded text is re-read by
|
|
32
|
-
* the `userQuestions` projection and the client question row, and a pending
|
|
33
|
-
* result must never read as human authorization.
|
|
34
|
-
*/
|
|
35
|
-
export const PENDING_QUESTION_NOTICE = 'No answer batch arrived before the timeout. This is pending, not a skipped answer. Continue useful independent work. The user can still answer; their reply will be a user message identified as answer_to_pending_question with this callId and the original questions. Do not treat this as permission.'
|
|
36
|
-
|
|
37
|
-
/** Native timed ask-tool description, with the host-controlled wait made explicit. */
|
|
38
|
-
const TIMED_ASK_DESCRIPTION = 'Ask brief, direct, self-contained questions about missing information, preferences, or decisions. Use user-facing terms; assume no knowledge of background work or internal names. Use distinct stable question IDs. A submitted skipped question is an answer item with empty selected and no custom; pending instead means no answer batch arrived before the timeout and the user can still answer. While a goal is active and armed, the host controls the wait duration and ignores timeout arguments. Otherwise the existing question tool behavior is preserved.'
|
|
39
|
-
|
|
40
|
-
/** The native timed-wait marker; its presence in the logged schema makes a call answerable after a timeout. */
|
|
41
|
-
const TIMED_WAIT_PARAMETER_SCHEMA = {
|
|
42
|
-
type: 'integer',
|
|
43
|
-
description: 'Compatibility wait field. The host goal-question timeout controls every new wait; any value supplied here is ignored.',
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** Native answers branch of the timed tool's output schema. */
|
|
47
|
-
const ANSWER_OUTPUT_SCHEMA = {
|
|
48
|
-
type: 'object',
|
|
49
|
-
additionalProperties: false,
|
|
50
|
-
required: ['answers'],
|
|
51
|
-
properties: {
|
|
52
|
-
answers: {
|
|
53
|
-
type: 'array',
|
|
54
|
-
description: 'Submitted answer batch with one item per question. A skipped question has empty selected and no custom; unlike pending, the user has completed the batch.',
|
|
55
|
-
items: {
|
|
56
|
-
type: 'object',
|
|
57
|
-
additionalProperties: false,
|
|
58
|
-
required: ['id', 'selected'],
|
|
59
|
-
properties: {
|
|
60
|
-
id: {
|
|
61
|
-
type: 'string',
|
|
62
|
-
description: 'Stable question id echoed from the request.',
|
|
63
|
-
},
|
|
64
|
-
selected: {
|
|
65
|
-
type: 'array',
|
|
66
|
-
items: { type: 'string' },
|
|
67
|
-
description: 'Selected option labels. Empty with no custom means the user explicitly skipped this question.',
|
|
68
|
-
},
|
|
69
|
-
custom: {
|
|
70
|
-
type: 'string',
|
|
71
|
-
description: 'Optional free-form answer; omitted for a skipped question.',
|
|
72
|
-
},
|
|
73
|
-
},
|
|
74
|
-
},
|
|
75
|
-
},
|
|
76
|
-
},
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** Native pending branch of the timed tool's output schema. */
|
|
80
|
-
const PENDING_OUTPUT_SCHEMA = {
|
|
81
|
-
type: 'object',
|
|
82
|
-
additionalProperties: false,
|
|
83
|
-
required: ['pending', 'callId', 'message'],
|
|
84
|
-
properties: {
|
|
85
|
-
pending: {
|
|
86
|
-
type: 'boolean',
|
|
87
|
-
enum: [true],
|
|
88
|
-
description: 'True when the foreground wait expired before the user submitted an answer batch. The questions remain answerable; this is not a skipped answer.',
|
|
89
|
-
},
|
|
90
|
-
callId: {
|
|
91
|
-
type: 'string',
|
|
92
|
-
description: 'Tool call whose unanswered questions remain pending.',
|
|
93
|
-
},
|
|
94
|
-
message: {
|
|
95
|
-
type: 'string',
|
|
96
|
-
description: 'How to continue while the questions stay answerable.',
|
|
97
|
-
},
|
|
98
|
-
},
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/** Native timed output shape: a real answer batch or a pending, still-answerable result. */
|
|
102
|
-
const OUTPUT_SCHEMA = {
|
|
103
|
-
oneOf: [PENDING_OUTPUT_SCHEMA, ANSWER_OUTPUT_SCHEMA],
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/** Check one configured timeout value without coercing it. */
|
|
107
|
-
function validateTimeoutSeconds(seconds) {
|
|
108
|
-
if (seconds !== -1 && (!Number.isInteger(seconds) || seconds < 1 || seconds > MAX_QUESTION_TIMEOUT_SECONDS)) {
|
|
109
|
-
throw new TypeError(`questionTimeoutSeconds must be -1 or a positive integer up to ${MAX_QUESTION_TIMEOUT_SECONDS} seconds`)
|
|
110
|
-
}
|
|
111
|
-
return seconds
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/** Read the live setting once per new wait; volatile references are unwrapped without caching. */
|
|
115
|
-
function readQuestionTimeoutSeconds(readConfig) {
|
|
116
|
-
const config = typeof readConfig === 'function' ? readConfig() : undefined
|
|
117
|
-
const raw = config?.questionTimeoutSeconds
|
|
118
|
-
const value = raw !== null && typeof raw === 'object' && typeof raw.get === 'function'
|
|
119
|
-
? raw.get()
|
|
120
|
-
: raw
|
|
121
|
-
return validateTimeoutSeconds(value ?? DEFAULT_QUESTION_TIMEOUT_SECONDS)
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/** Reject duplicate question ids within one call. */
|
|
125
|
-
function validateQuestionIds(questions) {
|
|
126
|
-
const ids = new Set()
|
|
127
|
-
for (const question of questions) {
|
|
128
|
-
if (ids.has(question.id)) throw new Error(`question id ${JSON.stringify(question.id)} must be unique within this call`)
|
|
129
|
-
ids.add(question.id)
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/** Translate the model schema into the service request without changing ownership. */
|
|
134
|
-
function questionRequest(questions, exec) {
|
|
135
|
-
return {
|
|
136
|
-
questions: questions.map((question) => ({
|
|
137
|
-
id: question.id,
|
|
138
|
-
question: question.question,
|
|
139
|
-
...(question.header !== undefined ? { header: question.header } : {}),
|
|
140
|
-
...(question.options !== undefined ? { options: question.options.map((option) => ({ ...option })) } : {}),
|
|
141
|
-
...(question.multi_select !== undefined ? { multiSelect: question.multi_select } : {}),
|
|
142
|
-
})),
|
|
143
|
-
...(exec.agent !== undefined ? { agent: exec.agent } : {}),
|
|
144
|
-
signal: exec.signal,
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/** Copy service-owned answer arrays into the model-facing result. */
|
|
149
|
-
function answerResult(result) {
|
|
150
|
-
return {
|
|
151
|
-
answers: result.answers.map((answer) => ({
|
|
152
|
-
id: answer.id,
|
|
153
|
-
selected: [...answer.selected],
|
|
154
|
-
...(answer.custom !== undefined ? { custom: answer.custom } : {}),
|
|
155
|
-
})),
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/** Build the scoped shadow definition from the currently registered ask tool. */
|
|
160
|
-
export function createGoalAskTool(ctx, baseDefinition, readConfig) {
|
|
161
|
-
if (baseDefinition?.name !== 'ask_user_question') {
|
|
162
|
-
throw new TypeError('createGoalAskTool requires the existing ask_user_question definition')
|
|
163
|
-
}
|
|
164
|
-
const baseQuestions = baseDefinition.parameters?.properties?.questions
|
|
165
|
-
if (baseQuestions === undefined) {
|
|
166
|
-
throw new TypeError('createGoalAskTool requires the base definition to declare a questions parameter')
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const parameters = {
|
|
170
|
-
...baseDefinition.parameters,
|
|
171
|
-
properties: {
|
|
172
|
-
...baseDefinition.parameters.properties,
|
|
173
|
-
[TIMED_WAIT_PARAMETER]: TIMED_WAIT_PARAMETER_SCHEMA,
|
|
174
|
-
},
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
const execute = async (args, exec) => {
|
|
178
|
-
const violations = validateJsonSchemaValue(parameters, args, '')
|
|
179
|
-
if (violations.length > 0) throw new ToolArgsError(violations)
|
|
180
|
-
validateQuestionIds(args.questions)
|
|
181
|
-
|
|
182
|
-
const goal = exec.agent && ctx.get('goals')?.get(exec.agent)
|
|
183
|
-
if (goal?.phase !== 'active' || goal.activation !== 'armed') {
|
|
184
|
-
const baseArgs = { ...args }
|
|
185
|
-
if (!(TIMED_WAIT_PARAMETER in baseDefinition.parameters.properties)) delete baseArgs[TIMED_WAIT_PARAMETER]
|
|
186
|
-
return baseDefinition.execute(baseArgs, exec)
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
const seconds = readQuestionTimeoutSeconds(readConfig)
|
|
190
|
-
const userQuestions = ctx.get('userQuestions')
|
|
191
|
-
if (userQuestions === undefined) throw new Error('ask_user_question requires the userQuestions service')
|
|
192
|
-
const request = questionRequest(args.questions, exec)
|
|
193
|
-
|
|
194
|
-
if (seconds === -1) {
|
|
195
|
-
return answerResult(await userQuestions.ask({
|
|
196
|
-
...request,
|
|
197
|
-
wait: { callId: exec.callId },
|
|
198
|
-
}))
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
if (exec.agent === undefined) throw new Error('timed questions require a live agent')
|
|
202
|
-
const result = await userQuestions.askTimed({
|
|
203
|
-
...request,
|
|
204
|
-
agent: exec.agent,
|
|
205
|
-
}, exec.callId, seconds * 1000)
|
|
206
|
-
return 'pending' in result
|
|
207
|
-
? { ...result, message: PENDING_QUESTION_NOTICE }
|
|
208
|
-
: answerResult(result)
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
return {
|
|
212
|
-
...baseDefinition,
|
|
213
|
-
description: TIMED_ASK_DESCRIPTION,
|
|
214
|
-
parameters,
|
|
215
|
-
output: {
|
|
216
|
-
schema: OUTPUT_SCHEMA,
|
|
217
|
-
render: (_args, value) => [{ type: 'text', text: JSON.stringify(value) }],
|
|
218
|
-
},
|
|
219
|
-
execute,
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
export default createGoalAskTool
|