dsh-session-guard 0.1.2 → 0.1.3
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/CHANGELOG.ja.md +29 -29
- package/CHANGELOG.ko.md +29 -29
- package/CHANGELOG.md +44 -29
- package/INSTALL.ja.md +42 -42
- package/INSTALL.ko.md +42 -42
- package/INSTALL.md +44 -44
- package/INSTALL.zh.md +42 -42
- package/README.en.md +170 -170
- package/README.ja.md +106 -106
- package/README.ko.md +106 -106
- package/README.md +9 -0
- package/dsh.plugin.json +13 -0
- package/lib/client.js +270 -270
- package/package.json +95 -78
- package/src/bridge.js +76 -76
- package/src/client/index.ts +56 -56
- package/src/client/locales.ts +95 -95
- package/src/client/settings-card.tsx +191 -191
- package/src/client/status-badge.tsx +66 -66
- package/src/index.js +293 -293
- package/src/pause-gate.js +416 -416
- package/src/retry.js +202 -202
- package/src/settings.js +97 -97
- package/src/time.js +104 -104
package/src/retry.js
CHANGED
|
@@ -1,202 +1,202 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* dsh-session-guard — 后端自动重试(host side,D9)。
|
|
3
|
-
*
|
|
4
|
-
* 监听 session/event:turn/end 以 error / interrupted / max-tokens 结束时,
|
|
5
|
-
* 分类瞬时/永久失败,自适应退避后自动 `agent.followup(retryText)` 续跑。
|
|
6
|
-
*
|
|
7
|
-
* 关键纪律:
|
|
8
|
-
* - **冻结让路**:会话被本插件门控(高峰暂停 / 队列锁)时不触发重试——
|
|
9
|
-
* 与 input-traffic「freeze 是一等公民」一致,自动重试不得绕过会话门。
|
|
10
|
-
* - 用户手动介入(user/message)与成功回合重置连续计数。
|
|
11
|
-
* - 子代理会话不重试(由父代理处理)。
|
|
12
|
-
* - 永久失败(鉴权/余额/模型不存在/上下文超限等)重试无益 → 停止并告警。
|
|
13
|
-
*
|
|
14
|
-
* 纯决策逻辑(classifyTurnEnd / isTransientFailure / effectiveCooldown /
|
|
15
|
-
* shouldRetry)零依赖可单测;事件接线在 createRetry。
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
/** 默认重试配置。 */
|
|
19
|
-
export const DEFAULT_RETRY = Object.freeze({
|
|
20
|
-
retryEnabled: false, // ← 自动重试开关(默认关,保守)
|
|
21
|
-
retryText: '继续(自动重试)',
|
|
22
|
-
retryGraceMs: 3000, // 失败后等待多久再发
|
|
23
|
-
retryCooldownMs: 20000, // 同一会话两次重试最小间隔
|
|
24
|
-
retryBackoffFactor: 2,
|
|
25
|
-
retryBackoffMaxMs: 300000,
|
|
26
|
-
retryMaxConsecutive: 3, // 连续重试上限,超过停止
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
/** 瞬时 vs 永久失败分类:瞬时值得重试,永久重试无益。 */
|
|
30
|
-
export function isTransientFailure({ code, message, status } = {}) {
|
|
31
|
-
const haystack = `${code ?? ''} ${message ?? ''}`.toLowerCase()
|
|
32
|
-
if (status !== undefined && (status === 401 || status === 403)) return false
|
|
33
|
-
const permanent =
|
|
34
|
-
/auth|unauthor|forbidden|credential|api\s*[_-]?\s*key|permission/i.test(haystack) ||
|
|
35
|
-
/insufficient.*(balance|quota)|billing|payment/i.test(haystack) ||
|
|
36
|
-
/model[^a-z]*not[^a-z]*found|unknown[_-]?model|not.*support.*model/i.test(haystack) ||
|
|
37
|
-
/context.*(length|limit|overflow|exceed)|token.*limit|max.*context/i.test(haystack) ||
|
|
38
|
-
/invalid[_-]?request|bad[_-]?request/i.test(haystack)
|
|
39
|
-
return !permanent
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* turn/end reason → 是否可自动重试。
|
|
44
|
-
* - completed / aborted(用户停)/ blocked(策略拒)→ 否
|
|
45
|
-
* - error → 按 isTransientFailure 分类
|
|
46
|
-
* - interrupted(崩溃修复)→ 可重试
|
|
47
|
-
* - max-tokens → 可重试
|
|
48
|
-
*/
|
|
49
|
-
export function classifyTurnEnd(reason, failure) {
|
|
50
|
-
const kind = reason && reason.kind
|
|
51
|
-
if (kind === 'completed' || kind === 'aborted' || kind === 'blocked') return false
|
|
52
|
-
if (kind === 'error') return isTransientFailure(failure)
|
|
53
|
-
if (kind === 'interrupted' || kind === 'max-tokens') return true
|
|
54
|
-
return false
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** 自适应退避:consecutive 次连续后 cooldown * factor^n,封顶 max。 */
|
|
58
|
-
export function effectiveCooldown(consecutive, base, factor, max) {
|
|
59
|
-
const mult = Math.pow(factor, Math.max(0, consecutive))
|
|
60
|
-
return Math.min(Math.max(base, base * mult), Math.max(base, max))
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* 纯决策:此刻是否应触发重试。
|
|
65
|
-
* @param {object} s 会话状态 { consecutive, lastAttemptAt, pending }
|
|
66
|
-
* @param {object} cfg 重试配置
|
|
67
|
-
* @param {boolean} frozen 会话是否被门控(高峰暂停/队列锁)
|
|
68
|
-
* @param {number} now
|
|
69
|
-
*/
|
|
70
|
-
export function shouldRetry(s, cfg, frozen, now = Date.now()) {
|
|
71
|
-
if (!cfg.retryEnabled) return false
|
|
72
|
-
if (frozen) return false // 冻结让路(D9)
|
|
73
|
-
if (s.pending) return false // 已有排队重试
|
|
74
|
-
if (s.consecutive >= cfg.retryMaxConsecutive) return false
|
|
75
|
-
if (now - s.lastAttemptAt < effectiveCooldown(s.consecutive, cfg.retryCooldownMs, cfg.retryBackoffFactor, cfg.retryBackoffMaxMs)) return false
|
|
76
|
-
return true
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** 会话状态工厂。 */
|
|
80
|
-
export function freshRetryState() {
|
|
81
|
-
return { consecutive: 0, lastAttemptAt: 0, pending: false }
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* 事件接线(host):
|
|
86
|
-
* @param {object} deps
|
|
87
|
-
* @param {object} deps.ctx host context
|
|
88
|
-
* @param {()=>object} deps.getSettings 读实时配置
|
|
89
|
-
* @param {(sessionId:string)=>boolean} deps.isFrozen 会话是否被门控
|
|
90
|
-
* @param {(sessionId:string, text:string)=>void} [deps.send] 发送函数(默认 agent.followup)
|
|
91
|
-
*/
|
|
92
|
-
export function createRetry({ ctx, getSettings, isFrozen, send }) {
|
|
93
|
-
const states = new Map()
|
|
94
|
-
|
|
95
|
-
function state(sessionId) {
|
|
96
|
-
let s = states.get(sessionId)
|
|
97
|
-
if (!s) {
|
|
98
|
-
s = freshRetryState()
|
|
99
|
-
states.set(sessionId, s)
|
|
100
|
-
}
|
|
101
|
-
return s
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/** 从 turn/end reason 提取失败事实。 */
|
|
105
|
-
function failureFacts(reason) {
|
|
106
|
-
const error = reason && reason.error
|
|
107
|
-
return {
|
|
108
|
-
code: error && typeof error.code === 'string' ? error.code : 'UNKNOWN',
|
|
109
|
-
message: error && typeof error.message === 'string' ? error.message : '',
|
|
110
|
-
status: error && typeof error.status === 'number' ? error.status : undefined,
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
function schedule(sessionId, reason) {
|
|
115
|
-
const s = state(sessionId)
|
|
116
|
-
const cfg = getSettings()
|
|
117
|
-
if (s.pending) return
|
|
118
|
-
const frozen = isFrozen(sessionId)
|
|
119
|
-
if (!shouldRetry({ ...s, pending: true }, cfg, frozen)) return
|
|
120
|
-
s.pending = true
|
|
121
|
-
const grace = cfg.retryGraceMs ?? DEFAULT_RETRY.retryGraceMs
|
|
122
|
-
const timer = setTimeout(() => {
|
|
123
|
-
s.pending = false
|
|
124
|
-
// 到点再复核:门控/上限变化后放弃。
|
|
125
|
-
if (!shouldRetry(s, cfg, isFrozen(sessionId))) return
|
|
126
|
-
const agent = ctx.agents.get(sessionId)
|
|
127
|
-
if (!agent || agent.status !== 'idle') return
|
|
128
|
-
const text = cfg.retryText ?? DEFAULT_RETRY.retryText
|
|
129
|
-
try {
|
|
130
|
-
if (send) {
|
|
131
|
-
send(sessionId, text)
|
|
132
|
-
} else {
|
|
133
|
-
// 与 autoresume 同构:直接构造 plugin-source 消息,避免运行时依赖。
|
|
134
|
-
agent.followup({
|
|
135
|
-
id: `session-guard-retry-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
|
|
136
|
-
role: 'user',
|
|
137
|
-
content: [{ type: 'text', text }],
|
|
138
|
-
source: { kind: 'plugin', plugin: 'session-guard', form: 'notice' },
|
|
139
|
-
})
|
|
140
|
-
}
|
|
141
|
-
s.lastAttemptAt = Date.now()
|
|
142
|
-
s.consecutive += 1
|
|
143
|
-
ctx.logger?.info?.(`[session-guard] auto-retry ${sessionId} (${String(reason && reason.kind)}), #${s.consecutive}`)
|
|
144
|
-
} catch (e) {
|
|
145
|
-
ctx.logger?.warn?.(`[session-guard] auto-retry send failed: ${String(e && e.message || e)}`)
|
|
146
|
-
}
|
|
147
|
-
}, grace)
|
|
148
|
-
// 会话对象上的清理钩子(若宿主提供)。
|
|
149
|
-
const dispose = () => clearTimeout(timer)
|
|
150
|
-
return dispose
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
function onEvent(session, event) {
|
|
154
|
-
const sessionId = session && session.id
|
|
155
|
-
if (typeof sessionId !== 'string') return
|
|
156
|
-
const s = state(sessionId)
|
|
157
|
-
switch (event.type) {
|
|
158
|
-
case 'turn/end': {
|
|
159
|
-
const reason = event.data && event.data.reason
|
|
160
|
-
if (reason && reason.kind === 'completed') {
|
|
161
|
-
s.consecutive = 0 // 成功回合重置
|
|
162
|
-
s.pending = false
|
|
163
|
-
return
|
|
164
|
-
}
|
|
165
|
-
if (reason && reason.kind === 'aborted') {
|
|
166
|
-
// 用户主动停止:不重试,重置。
|
|
167
|
-
s.consecutive = 0
|
|
168
|
-
s.pending = false
|
|
169
|
-
return
|
|
170
|
-
}
|
|
171
|
-
if (reason && classifyTurnEnd(reason, failureFacts(reason))) {
|
|
172
|
-
schedule(sessionId, reason)
|
|
173
|
-
}
|
|
174
|
-
return
|
|
175
|
-
}
|
|
176
|
-
case 'user/message': {
|
|
177
|
-
// 用户手动介入:重置(无论是否我们的回显都保守重置)。
|
|
178
|
-
if (event.data && event.data.source && event.data.source.kind === 'user') {
|
|
179
|
-
s.consecutive = 0
|
|
180
|
-
s.pending = false
|
|
181
|
-
}
|
|
182
|
-
return
|
|
183
|
-
}
|
|
184
|
-
default:
|
|
185
|
-
return
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
ctx.on('session/event', (session, event) => {
|
|
190
|
-
try {
|
|
191
|
-
onEvent(session, event)
|
|
192
|
-
} catch (e) {
|
|
193
|
-
ctx.logger?.warn?.(`[session-guard] retry event failed: ${String(e && e.message || e)}`)
|
|
194
|
-
}
|
|
195
|
-
})
|
|
196
|
-
|
|
197
|
-
return {
|
|
198
|
-
onEvent,
|
|
199
|
-
state,
|
|
200
|
-
states,
|
|
201
|
-
}
|
|
202
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* dsh-session-guard — 后端自动重试(host side,D9)。
|
|
3
|
+
*
|
|
4
|
+
* 监听 session/event:turn/end 以 error / interrupted / max-tokens 结束时,
|
|
5
|
+
* 分类瞬时/永久失败,自适应退避后自动 `agent.followup(retryText)` 续跑。
|
|
6
|
+
*
|
|
7
|
+
* 关键纪律:
|
|
8
|
+
* - **冻结让路**:会话被本插件门控(高峰暂停 / 队列锁)时不触发重试——
|
|
9
|
+
* 与 input-traffic「freeze 是一等公民」一致,自动重试不得绕过会话门。
|
|
10
|
+
* - 用户手动介入(user/message)与成功回合重置连续计数。
|
|
11
|
+
* - 子代理会话不重试(由父代理处理)。
|
|
12
|
+
* - 永久失败(鉴权/余额/模型不存在/上下文超限等)重试无益 → 停止并告警。
|
|
13
|
+
*
|
|
14
|
+
* 纯决策逻辑(classifyTurnEnd / isTransientFailure / effectiveCooldown /
|
|
15
|
+
* shouldRetry)零依赖可单测;事件接线在 createRetry。
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** 默认重试配置。 */
|
|
19
|
+
export const DEFAULT_RETRY = Object.freeze({
|
|
20
|
+
retryEnabled: false, // ← 自动重试开关(默认关,保守)
|
|
21
|
+
retryText: '继续(自动重试)',
|
|
22
|
+
retryGraceMs: 3000, // 失败后等待多久再发
|
|
23
|
+
retryCooldownMs: 20000, // 同一会话两次重试最小间隔
|
|
24
|
+
retryBackoffFactor: 2,
|
|
25
|
+
retryBackoffMaxMs: 300000,
|
|
26
|
+
retryMaxConsecutive: 3, // 连续重试上限,超过停止
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
/** 瞬时 vs 永久失败分类:瞬时值得重试,永久重试无益。 */
|
|
30
|
+
export function isTransientFailure({ code, message, status } = {}) {
|
|
31
|
+
const haystack = `${code ?? ''} ${message ?? ''}`.toLowerCase()
|
|
32
|
+
if (status !== undefined && (status === 401 || status === 403)) return false
|
|
33
|
+
const permanent =
|
|
34
|
+
/auth|unauthor|forbidden|credential|api\s*[_-]?\s*key|permission/i.test(haystack) ||
|
|
35
|
+
/insufficient.*(balance|quota)|billing|payment/i.test(haystack) ||
|
|
36
|
+
/model[^a-z]*not[^a-z]*found|unknown[_-]?model|not.*support.*model/i.test(haystack) ||
|
|
37
|
+
/context.*(length|limit|overflow|exceed)|token.*limit|max.*context/i.test(haystack) ||
|
|
38
|
+
/invalid[_-]?request|bad[_-]?request/i.test(haystack)
|
|
39
|
+
return !permanent
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* turn/end reason → 是否可自动重试。
|
|
44
|
+
* - completed / aborted(用户停)/ blocked(策略拒)→ 否
|
|
45
|
+
* - error → 按 isTransientFailure 分类
|
|
46
|
+
* - interrupted(崩溃修复)→ 可重试
|
|
47
|
+
* - max-tokens → 可重试
|
|
48
|
+
*/
|
|
49
|
+
export function classifyTurnEnd(reason, failure) {
|
|
50
|
+
const kind = reason && reason.kind
|
|
51
|
+
if (kind === 'completed' || kind === 'aborted' || kind === 'blocked') return false
|
|
52
|
+
if (kind === 'error') return isTransientFailure(failure)
|
|
53
|
+
if (kind === 'interrupted' || kind === 'max-tokens') return true
|
|
54
|
+
return false
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** 自适应退避:consecutive 次连续后 cooldown * factor^n,封顶 max。 */
|
|
58
|
+
export function effectiveCooldown(consecutive, base, factor, max) {
|
|
59
|
+
const mult = Math.pow(factor, Math.max(0, consecutive))
|
|
60
|
+
return Math.min(Math.max(base, base * mult), Math.max(base, max))
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 纯决策:此刻是否应触发重试。
|
|
65
|
+
* @param {object} s 会话状态 { consecutive, lastAttemptAt, pending }
|
|
66
|
+
* @param {object} cfg 重试配置
|
|
67
|
+
* @param {boolean} frozen 会话是否被门控(高峰暂停/队列锁)
|
|
68
|
+
* @param {number} now
|
|
69
|
+
*/
|
|
70
|
+
export function shouldRetry(s, cfg, frozen, now = Date.now()) {
|
|
71
|
+
if (!cfg.retryEnabled) return false
|
|
72
|
+
if (frozen) return false // 冻结让路(D9)
|
|
73
|
+
if (s.pending) return false // 已有排队重试
|
|
74
|
+
if (s.consecutive >= cfg.retryMaxConsecutive) return false
|
|
75
|
+
if (now - s.lastAttemptAt < effectiveCooldown(s.consecutive, cfg.retryCooldownMs, cfg.retryBackoffFactor, cfg.retryBackoffMaxMs)) return false
|
|
76
|
+
return true
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** 会话状态工厂。 */
|
|
80
|
+
export function freshRetryState() {
|
|
81
|
+
return { consecutive: 0, lastAttemptAt: 0, pending: false }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 事件接线(host):
|
|
86
|
+
* @param {object} deps
|
|
87
|
+
* @param {object} deps.ctx host context
|
|
88
|
+
* @param {()=>object} deps.getSettings 读实时配置
|
|
89
|
+
* @param {(sessionId:string)=>boolean} deps.isFrozen 会话是否被门控
|
|
90
|
+
* @param {(sessionId:string, text:string)=>void} [deps.send] 发送函数(默认 agent.followup)
|
|
91
|
+
*/
|
|
92
|
+
export function createRetry({ ctx, getSettings, isFrozen, send }) {
|
|
93
|
+
const states = new Map()
|
|
94
|
+
|
|
95
|
+
function state(sessionId) {
|
|
96
|
+
let s = states.get(sessionId)
|
|
97
|
+
if (!s) {
|
|
98
|
+
s = freshRetryState()
|
|
99
|
+
states.set(sessionId, s)
|
|
100
|
+
}
|
|
101
|
+
return s
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** 从 turn/end reason 提取失败事实。 */
|
|
105
|
+
function failureFacts(reason) {
|
|
106
|
+
const error = reason && reason.error
|
|
107
|
+
return {
|
|
108
|
+
code: error && typeof error.code === 'string' ? error.code : 'UNKNOWN',
|
|
109
|
+
message: error && typeof error.message === 'string' ? error.message : '',
|
|
110
|
+
status: error && typeof error.status === 'number' ? error.status : undefined,
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function schedule(sessionId, reason) {
|
|
115
|
+
const s = state(sessionId)
|
|
116
|
+
const cfg = getSettings()
|
|
117
|
+
if (s.pending) return
|
|
118
|
+
const frozen = isFrozen(sessionId)
|
|
119
|
+
if (!shouldRetry({ ...s, pending: true }, cfg, frozen)) return
|
|
120
|
+
s.pending = true
|
|
121
|
+
const grace = cfg.retryGraceMs ?? DEFAULT_RETRY.retryGraceMs
|
|
122
|
+
const timer = setTimeout(() => {
|
|
123
|
+
s.pending = false
|
|
124
|
+
// 到点再复核:门控/上限变化后放弃。
|
|
125
|
+
if (!shouldRetry(s, cfg, isFrozen(sessionId))) return
|
|
126
|
+
const agent = ctx.agents.get(sessionId)
|
|
127
|
+
if (!agent || agent.status !== 'idle') return
|
|
128
|
+
const text = cfg.retryText ?? DEFAULT_RETRY.retryText
|
|
129
|
+
try {
|
|
130
|
+
if (send) {
|
|
131
|
+
send(sessionId, text)
|
|
132
|
+
} else {
|
|
133
|
+
// 与 autoresume 同构:直接构造 plugin-source 消息,避免运行时依赖。
|
|
134
|
+
agent.followup({
|
|
135
|
+
id: `session-guard-retry-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
|
|
136
|
+
role: 'user',
|
|
137
|
+
content: [{ type: 'text', text }],
|
|
138
|
+
source: { kind: 'plugin', plugin: 'session-guard', form: 'notice' },
|
|
139
|
+
})
|
|
140
|
+
}
|
|
141
|
+
s.lastAttemptAt = Date.now()
|
|
142
|
+
s.consecutive += 1
|
|
143
|
+
ctx.logger?.info?.(`[session-guard] auto-retry ${sessionId} (${String(reason && reason.kind)}), #${s.consecutive}`)
|
|
144
|
+
} catch (e) {
|
|
145
|
+
ctx.logger?.warn?.(`[session-guard] auto-retry send failed: ${String(e && e.message || e)}`)
|
|
146
|
+
}
|
|
147
|
+
}, grace)
|
|
148
|
+
// 会话对象上的清理钩子(若宿主提供)。
|
|
149
|
+
const dispose = () => clearTimeout(timer)
|
|
150
|
+
return dispose
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function onEvent(session, event) {
|
|
154
|
+
const sessionId = session && session.id
|
|
155
|
+
if (typeof sessionId !== 'string') return
|
|
156
|
+
const s = state(sessionId)
|
|
157
|
+
switch (event.type) {
|
|
158
|
+
case 'turn/end': {
|
|
159
|
+
const reason = event.data && event.data.reason
|
|
160
|
+
if (reason && reason.kind === 'completed') {
|
|
161
|
+
s.consecutive = 0 // 成功回合重置
|
|
162
|
+
s.pending = false
|
|
163
|
+
return
|
|
164
|
+
}
|
|
165
|
+
if (reason && reason.kind === 'aborted') {
|
|
166
|
+
// 用户主动停止:不重试,重置。
|
|
167
|
+
s.consecutive = 0
|
|
168
|
+
s.pending = false
|
|
169
|
+
return
|
|
170
|
+
}
|
|
171
|
+
if (reason && classifyTurnEnd(reason, failureFacts(reason))) {
|
|
172
|
+
schedule(sessionId, reason)
|
|
173
|
+
}
|
|
174
|
+
return
|
|
175
|
+
}
|
|
176
|
+
case 'user/message': {
|
|
177
|
+
// 用户手动介入:重置(无论是否我们的回显都保守重置)。
|
|
178
|
+
if (event.data && event.data.source && event.data.source.kind === 'user') {
|
|
179
|
+
s.consecutive = 0
|
|
180
|
+
s.pending = false
|
|
181
|
+
}
|
|
182
|
+
return
|
|
183
|
+
}
|
|
184
|
+
default:
|
|
185
|
+
return
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
ctx.on('session/event', (session, event) => {
|
|
190
|
+
try {
|
|
191
|
+
onEvent(session, event)
|
|
192
|
+
} catch (e) {
|
|
193
|
+
ctx.logger?.warn?.(`[session-guard] retry event failed: ${String(e && e.message || e)}`)
|
|
194
|
+
}
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
return {
|
|
198
|
+
onEvent,
|
|
199
|
+
state,
|
|
200
|
+
states,
|
|
201
|
+
}
|
|
202
|
+
}
|
package/src/settings.js
CHANGED
|
@@ -1,97 +1,97 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* dsh-session-guard — 设置子板块(设置 → 插件 → session-guard,简单开关)。
|
|
3
|
-
*
|
|
4
|
-
* 注册方式对齐 dsh-thinking-levels 的官方做法:
|
|
5
|
-
* - `@deepseek-ai/schemastery` 为**常规 dependency**(随插件安装,可解析)。
|
|
6
|
-
* - **不 value-import `@deepseek-ai/dsh-settings`**:该服务由 dsh runtime 经
|
|
7
|
-
* cordis `settings` 注入面提供,不属于 profile 的可解析树;这里只用本地最小
|
|
8
|
-
* 接口(SettingsScopeLike / SettingsServiceLike / SettingsAwareCtx)在
|
|
9
|
-
* `ctx.inject(['settings'], ...)` 里注册命名空间,`base` 层叠组合配置。
|
|
10
|
-
* - 仍 fail-open:任何解析 / 注入失败都静默降级用 DEFAULT_SETTINGS,绝不因
|
|
11
|
-
* 设置依赖而崩。
|
|
12
|
-
*/
|
|
13
|
-
import z from '@deepseek-ai/schemastery'
|
|
14
|
-
import { DEFAULT_RETRY } from './retry.js'
|
|
15
|
-
|
|
16
|
-
/** 设置命名空间(设置 → 插件 → session-guard)。 */
|
|
17
|
-
export const NS = 'session-guard'
|
|
18
|
-
|
|
19
|
-
/** 默认配置(核心逻辑依赖;设置服务不可用时即用此值)。 */
|
|
20
|
-
export const DEFAULT_SETTINGS = Object.freeze({
|
|
21
|
-
enabled: true, // 高峰自动处理开关(简单开关)
|
|
22
|
-
offPeakAutoResume: true, // 低谷自动恢复开关:低峰时段自动恢复被高峰暂停的会话
|
|
23
|
-
weekendMode: true, // 周末模式开关:识别周末,无视峰谷(简单开关)
|
|
24
|
-
timezone: 'Asia/Shanghai',
|
|
25
|
-
peakWindows: [
|
|
26
|
-
{ start: '09:00', end: '12:00' },
|
|
27
|
-
{ start: '14:00', end: '18:00' },
|
|
28
|
-
],
|
|
29
|
-
pauseMode: 'safe', // 透传 taskControl.pause mode
|
|
30
|
-
pauseReason: 'wait', // 透传 taskControl.pause reason
|
|
31
|
-
queueFallback: true, // 无会话门时回退锁等待队列(简单开关)
|
|
32
|
-
retryEnabled: false, // 自动重试开关(后端,D9;默认关,保守)
|
|
33
|
-
retryText: DEFAULT_RETRY.retryText,
|
|
34
|
-
retryGraceMs: DEFAULT_RETRY.retryGraceMs,
|
|
35
|
-
retryCooldownMs: DEFAULT_RETRY.retryCooldownMs,
|
|
36
|
-
retryBackoffFactor: DEFAULT_RETRY.retryBackoffFactor,
|
|
37
|
-
retryBackoffMaxMs: DEFAULT_RETRY.retryBackoffMaxMs,
|
|
38
|
-
retryMaxConsecutive: DEFAULT_RETRY.retryMaxConsecutive,
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* 设置 schema(schemastery 原生命令,zod 变体):
|
|
43
|
-
* - 枚举用 z.union([z.const(...)]);默认值用字段级 .default()。
|
|
44
|
-
*/
|
|
45
|
-
export const SettingsSchema = z.object({
|
|
46
|
-
enabled: z.boolean().default(DEFAULT_SETTINGS.enabled),
|
|
47
|
-
offPeakAutoResume: z.boolean().default(DEFAULT_SETTINGS.offPeakAutoResume),
|
|
48
|
-
weekendMode: z.boolean().default(DEFAULT_SETTINGS.weekendMode),
|
|
49
|
-
timezone: z.string().default(DEFAULT_SETTINGS.timezone),
|
|
50
|
-
peakWindows: z
|
|
51
|
-
.array(z.object({ start: z.string(), end: z.string() }))
|
|
52
|
-
.default(DEFAULT_SETTINGS.peakWindows),
|
|
53
|
-
pauseMode: z.union([z.const('safe'), z.const('force')]).default(DEFAULT_SETTINGS.pauseMode),
|
|
54
|
-
pauseReason: z.union([z.const('wait'), z.const('stop')]).default(DEFAULT_SETTINGS.pauseReason),
|
|
55
|
-
queueFallback: z.boolean().default(DEFAULT_SETTINGS.queueFallback),
|
|
56
|
-
retryEnabled: z.boolean().default(DEFAULT_SETTINGS.retryEnabled),
|
|
57
|
-
retryText: z.string().default(DEFAULT_SETTINGS.retryText),
|
|
58
|
-
retryGraceMs: z.number().min(0).default(DEFAULT_SETTINGS.retryGraceMs),
|
|
59
|
-
retryCooldownMs: z.number().min(0).default(DEFAULT_SETTINGS.retryCooldownMs),
|
|
60
|
-
retryBackoffFactor: z.number().min(1).default(DEFAULT_SETTINGS.retryBackoffFactor),
|
|
61
|
-
retryBackoffMaxMs: z.number().min(0).default(DEFAULT_SETTINGS.retryBackoffMaxMs),
|
|
62
|
-
retryMaxConsecutive: z.number().min(0).default(DEFAULT_SETTINGS.retryMaxConsecutive),
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* dsh settings 服务的最小本地接口(仿 dsh-thinking-levels 的 local face):
|
|
67
|
-
* 插件不 value-import `@deepseek-ai/dsh-settings`,只通过这些形状在注入面调用。
|
|
68
|
-
*/
|
|
69
|
-
|
|
70
|
-
/** @typedef {{ get(): unknown; watch(cb: () => void): () => void }} SettingsScopeLike */
|
|
71
|
-
/** @typedef {{ register(ns: string, schema: unknown, options?: { base?: unknown }): SettingsScopeLike }} SettingsServiceLike */
|
|
72
|
-
/** @typedef {{ inject(deps: readonly string[], fn: (s: { settings: SettingsServiceLike; effect(cb: () => (() => void) | void, label?: string): void }) => void): void }} SettingsAwareCtx */
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* 注册 设置 → 插件 → session-guard 子板块(简单开关)。
|
|
76
|
-
* - 走 `settings` 注入面,`base` 层叠组合配置;对 runtime 调用方经 `ctx.settings.get(NS)` 读取。
|
|
77
|
-
* - 任何失败(settings 服务缺失 / 注入异常)→ 返回 false,静默降级用默认配置。
|
|
78
|
-
* @param {object} ctx - host context(应含 cordis `settings` 注入面)。
|
|
79
|
-
* @returns {boolean} 注册成功 true;设置服务不可用时 false(fail-open)。
|
|
80
|
-
*/
|
|
81
|
-
export function registerSettings(ctx) {
|
|
82
|
-
try {
|
|
83
|
-
// 本插件顶层 `inject` 已声明 `settings`(见 src/index.js 的
|
|
84
|
-
// `export const inject`),所以 apply 时 `ctx.settings` 已是完整
|
|
85
|
-
// SettingsProvider(带 .register),直接注册即可 —— 无需再 `ctx.inject`
|
|
86
|
-
// 二次动态注入(对已在 fiber 上解析的服务做二次注入,回调作为异步插件
|
|
87
|
-
// apply 排队,`registerSettings` 同步返回 true 会掩盖实际操作未生效)。
|
|
88
|
-
// 与 dsh-thinking-levels / dsh-context 的区别仅在于它们顶层未声明
|
|
89
|
-
// settings,才必须动态注入;这里已声明,直接用最可靠。
|
|
90
|
-
const svc = /** @type {{ register(ns: string, schema: unknown, options?: { base?: unknown }): unknown }} */ (ctx.settings)
|
|
91
|
-
// base 用副本:DEFAULT_SETTINGS 被 Object.freeze,直接当 base 可能被写。
|
|
92
|
-
svc.register(NS, SettingsSchema, { base: { ...DEFAULT_SETTINGS } })
|
|
93
|
-
return true
|
|
94
|
-
} catch {
|
|
95
|
-
return false
|
|
96
|
-
}
|
|
97
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* dsh-session-guard — 设置子板块(设置 → 插件 → session-guard,简单开关)。
|
|
3
|
+
*
|
|
4
|
+
* 注册方式对齐 dsh-thinking-levels 的官方做法:
|
|
5
|
+
* - `@deepseek-ai/schemastery` 为**常规 dependency**(随插件安装,可解析)。
|
|
6
|
+
* - **不 value-import `@deepseek-ai/dsh-settings`**:该服务由 dsh runtime 经
|
|
7
|
+
* cordis `settings` 注入面提供,不属于 profile 的可解析树;这里只用本地最小
|
|
8
|
+
* 接口(SettingsScopeLike / SettingsServiceLike / SettingsAwareCtx)在
|
|
9
|
+
* `ctx.inject(['settings'], ...)` 里注册命名空间,`base` 层叠组合配置。
|
|
10
|
+
* - 仍 fail-open:任何解析 / 注入失败都静默降级用 DEFAULT_SETTINGS,绝不因
|
|
11
|
+
* 设置依赖而崩。
|
|
12
|
+
*/
|
|
13
|
+
import z from '@deepseek-ai/schemastery'
|
|
14
|
+
import { DEFAULT_RETRY } from './retry.js'
|
|
15
|
+
|
|
16
|
+
/** 设置命名空间(设置 → 插件 → session-guard)。 */
|
|
17
|
+
export const NS = 'session-guard'
|
|
18
|
+
|
|
19
|
+
/** 默认配置(核心逻辑依赖;设置服务不可用时即用此值)。 */
|
|
20
|
+
export const DEFAULT_SETTINGS = Object.freeze({
|
|
21
|
+
enabled: true, // 高峰自动处理开关(简单开关)
|
|
22
|
+
offPeakAutoResume: true, // 低谷自动恢复开关:低峰时段自动恢复被高峰暂停的会话
|
|
23
|
+
weekendMode: true, // 周末模式开关:识别周末,无视峰谷(简单开关)
|
|
24
|
+
timezone: 'Asia/Shanghai',
|
|
25
|
+
peakWindows: [
|
|
26
|
+
{ start: '09:00', end: '12:00' },
|
|
27
|
+
{ start: '14:00', end: '18:00' },
|
|
28
|
+
],
|
|
29
|
+
pauseMode: 'safe', // 透传 taskControl.pause mode
|
|
30
|
+
pauseReason: 'wait', // 透传 taskControl.pause reason
|
|
31
|
+
queueFallback: true, // 无会话门时回退锁等待队列(简单开关)
|
|
32
|
+
retryEnabled: false, // 自动重试开关(后端,D9;默认关,保守)
|
|
33
|
+
retryText: DEFAULT_RETRY.retryText,
|
|
34
|
+
retryGraceMs: DEFAULT_RETRY.retryGraceMs,
|
|
35
|
+
retryCooldownMs: DEFAULT_RETRY.retryCooldownMs,
|
|
36
|
+
retryBackoffFactor: DEFAULT_RETRY.retryBackoffFactor,
|
|
37
|
+
retryBackoffMaxMs: DEFAULT_RETRY.retryBackoffMaxMs,
|
|
38
|
+
retryMaxConsecutive: DEFAULT_RETRY.retryMaxConsecutive,
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 设置 schema(schemastery 原生命令,zod 变体):
|
|
43
|
+
* - 枚举用 z.union([z.const(...)]);默认值用字段级 .default()。
|
|
44
|
+
*/
|
|
45
|
+
export const SettingsSchema = z.object({
|
|
46
|
+
enabled: z.boolean().default(DEFAULT_SETTINGS.enabled),
|
|
47
|
+
offPeakAutoResume: z.boolean().default(DEFAULT_SETTINGS.offPeakAutoResume),
|
|
48
|
+
weekendMode: z.boolean().default(DEFAULT_SETTINGS.weekendMode),
|
|
49
|
+
timezone: z.string().default(DEFAULT_SETTINGS.timezone),
|
|
50
|
+
peakWindows: z
|
|
51
|
+
.array(z.object({ start: z.string(), end: z.string() }))
|
|
52
|
+
.default(DEFAULT_SETTINGS.peakWindows),
|
|
53
|
+
pauseMode: z.union([z.const('safe'), z.const('force')]).default(DEFAULT_SETTINGS.pauseMode),
|
|
54
|
+
pauseReason: z.union([z.const('wait'), z.const('stop')]).default(DEFAULT_SETTINGS.pauseReason),
|
|
55
|
+
queueFallback: z.boolean().default(DEFAULT_SETTINGS.queueFallback),
|
|
56
|
+
retryEnabled: z.boolean().default(DEFAULT_SETTINGS.retryEnabled),
|
|
57
|
+
retryText: z.string().default(DEFAULT_SETTINGS.retryText),
|
|
58
|
+
retryGraceMs: z.number().min(0).default(DEFAULT_SETTINGS.retryGraceMs),
|
|
59
|
+
retryCooldownMs: z.number().min(0).default(DEFAULT_SETTINGS.retryCooldownMs),
|
|
60
|
+
retryBackoffFactor: z.number().min(1).default(DEFAULT_SETTINGS.retryBackoffFactor),
|
|
61
|
+
retryBackoffMaxMs: z.number().min(0).default(DEFAULT_SETTINGS.retryBackoffMaxMs),
|
|
62
|
+
retryMaxConsecutive: z.number().min(0).default(DEFAULT_SETTINGS.retryMaxConsecutive),
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* dsh settings 服务的最小本地接口(仿 dsh-thinking-levels 的 local face):
|
|
67
|
+
* 插件不 value-import `@deepseek-ai/dsh-settings`,只通过这些形状在注入面调用。
|
|
68
|
+
*/
|
|
69
|
+
|
|
70
|
+
/** @typedef {{ get(): unknown; watch(cb: () => void): () => void }} SettingsScopeLike */
|
|
71
|
+
/** @typedef {{ register(ns: string, schema: unknown, options?: { base?: unknown }): SettingsScopeLike }} SettingsServiceLike */
|
|
72
|
+
/** @typedef {{ inject(deps: readonly string[], fn: (s: { settings: SettingsServiceLike; effect(cb: () => (() => void) | void, label?: string): void }) => void): void }} SettingsAwareCtx */
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 注册 设置 → 插件 → session-guard 子板块(简单开关)。
|
|
76
|
+
* - 走 `settings` 注入面,`base` 层叠组合配置;对 runtime 调用方经 `ctx.settings.get(NS)` 读取。
|
|
77
|
+
* - 任何失败(settings 服务缺失 / 注入异常)→ 返回 false,静默降级用默认配置。
|
|
78
|
+
* @param {object} ctx - host context(应含 cordis `settings` 注入面)。
|
|
79
|
+
* @returns {boolean} 注册成功 true;设置服务不可用时 false(fail-open)。
|
|
80
|
+
*/
|
|
81
|
+
export function registerSettings(ctx) {
|
|
82
|
+
try {
|
|
83
|
+
// 本插件顶层 `inject` 已声明 `settings`(见 src/index.js 的
|
|
84
|
+
// `export const inject`),所以 apply 时 `ctx.settings` 已是完整
|
|
85
|
+
// SettingsProvider(带 .register),直接注册即可 —— 无需再 `ctx.inject`
|
|
86
|
+
// 二次动态注入(对已在 fiber 上解析的服务做二次注入,回调作为异步插件
|
|
87
|
+
// apply 排队,`registerSettings` 同步返回 true 会掩盖实际操作未生效)。
|
|
88
|
+
// 与 dsh-thinking-levels / dsh-context 的区别仅在于它们顶层未声明
|
|
89
|
+
// settings,才必须动态注入;这里已声明,直接用最可靠。
|
|
90
|
+
const svc = /** @type {{ register(ns: string, schema: unknown, options?: { base?: unknown }): unknown }} */ (ctx.settings)
|
|
91
|
+
// base 用副本:DEFAULT_SETTINGS 被 Object.freeze,直接当 base 可能被写。
|
|
92
|
+
svc.register(NS, SettingsSchema, { base: { ...DEFAULT_SETTINGS } })
|
|
93
|
+
return true
|
|
94
|
+
} catch {
|
|
95
|
+
return false
|
|
96
|
+
}
|
|
97
|
+
}
|