dsh-mcviseron-plugin 0.6.5 → 0.6.8
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.md +31 -0
- package/README.en.md +17 -4
- package/README.md +19 -6
- package/docs/auth-cookie.md +61 -0
- package/docs/auto-review-context.md +65 -0
- package/docs/automation-task.md +1 -1
- package/docs/dsh-cookie-launcher.md +55 -0
- package/docs/release.md +3 -3
- package/docs/session-end.md +4 -1
- package/lib/auto-review-fix.js +253 -23
- package/lib/client.js +199 -9
- package/lib/index.js +559 -125
- package/package.json +23 -18
- package/scripts/dsh-cookie-launcher/clean-dsh-auth-cookies.py +97 -0
- package/scripts/dsh-cookie-launcher/launch-dsh.vbs +16 -0
- package/scripts/install-dsh-cookie-launcher.ps1 +48 -0
- package/src/api.ts +6 -0
- package/src/auth-cookie/index.ts +175 -0
- package/src/auto-review-fix/index.ts +9 -0
- package/src/client/ConfigForm.tsx +3 -0
- package/src/client/SessionEndControl.tsx +3 -0
- package/src/client/SessionEndGlobalNotice.ts +183 -0
- package/src/client/SettingsSection.tsx +4 -0
- package/src/client/index.ts +15 -0
- package/src/client/locales.ts +14 -0
- package/src/components/auto-review-fix/index.ts +8 -0
- package/src/components/auto-review-fix/review-context.ts +285 -0
- package/src/index.ts +2 -0
- package/src/session-end/index.ts +85 -17
|
@@ -120,6 +120,10 @@ export function SettingsSection({ t }: SettingsSectionProps) {
|
|
|
120
120
|
<span style={pageStyles.componentName}>{t('page.components.restart')}</span>
|
|
121
121
|
<span style={pageStyles.componentHint}>{t('page.components.restartHint')}</span>
|
|
122
122
|
</div>
|
|
123
|
+
<div style={pageStyles.componentRow}>
|
|
124
|
+
<span style={pageStyles.componentName}>{t('page.components.authCookie')}</span>
|
|
125
|
+
<span style={pageStyles.componentHint}>{t('page.components.authCookieHint')}</span>
|
|
126
|
+
</div>
|
|
123
127
|
<div style={pageStyles.componentRow}>
|
|
124
128
|
<span style={pageStyles.componentName}>{t('page.components.sessionEnd')}</span>
|
|
125
129
|
<span style={pageStyles.componentHint}>{t('page.components.sessionEndHint')}</span>
|
package/src/client/index.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type {} from '@deepseek-ai/dsh-client-locale/client'
|
|
|
3
3
|
import type {} from '@deepseek-ai/dsh-client-ui-renderer/client'
|
|
4
4
|
import type {} from '@deepseek-ai/dsh-client-ui-settings/client'
|
|
5
5
|
import type {} from '@deepseek-ai/dsh-client-ui-slots'
|
|
6
|
+
import { AUTH_COOKIE_API } from '../api.ts'
|
|
6
7
|
import { AppearanceScheduleRow } from './AppearanceScheduleRow.tsx'
|
|
7
8
|
import { FallbackRestartRow } from './FallbackRestartRow.tsx'
|
|
8
9
|
import { SessionEndControl } from './SessionEndControl.tsx'
|
|
@@ -12,6 +13,7 @@ import { en, zh, type McviseronKey } from './locales.ts'
|
|
|
12
13
|
import { sectionPresence } from './section-presence.ts'
|
|
13
14
|
|
|
14
15
|
const NS = 'mcviseron'
|
|
16
|
+
let authCookiePruneStarted = false
|
|
15
17
|
|
|
16
18
|
declare module '@deepseek-ai/dsh-client-ui-slots' {
|
|
17
19
|
interface LocaleNamespaceMap {
|
|
@@ -77,6 +79,19 @@ function registerSafely(
|
|
|
77
79
|
* @param ctx - client cordis context.
|
|
78
80
|
*/
|
|
79
81
|
export function apply(ctx: ClientContext): void {
|
|
82
|
+
if (!authCookiePruneStarted) {
|
|
83
|
+
authCookiePruneStarted = true
|
|
84
|
+
try {
|
|
85
|
+
void fetch(AUTH_COOKIE_API.prune, {
|
|
86
|
+
method: 'POST',
|
|
87
|
+
credentials: 'same-origin',
|
|
88
|
+
cache: 'no-store',
|
|
89
|
+
}).catch(() => {})
|
|
90
|
+
} catch {
|
|
91
|
+
/* a shell without fetch still loads the rest of the plugin */
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
80
95
|
try {
|
|
81
96
|
ctx.effect(() => ctx.locale.register(NS, { zh, en }), 'mcviseron: dictionaries')
|
|
82
97
|
} catch (error) {
|
package/src/client/locales.ts
CHANGED
|
@@ -73,6 +73,11 @@ export const zh = {
|
|
|
73
73
|
'form.failed': '保存失败',
|
|
74
74
|
'form.unavailable': '无法读取插件设置:请确认 profile 中的插件条目 id 为 mcviseron',
|
|
75
75
|
'form.autoSave': '修改后自动保存',
|
|
76
|
+
'form.autoReview.reviewGuard': '裁剪 Auto review 上下文',
|
|
77
|
+
'form.autoReview.reviewGuardHint': '只压缩 reviewer 的历史;当前动作、工程约束和人类指令保留,避免长会话把审查模型撑爆',
|
|
78
|
+
'form.autoReview.reviewMaxHistory': '审查历史上限(字符)',
|
|
79
|
+
'form.autoReview.reviewMaxHistoryHint': '默认 120000;超出后旧工具调用会折叠成摘要',
|
|
80
|
+
'form.autoReview.reviewKeepRecent': '保留最近历史条数',
|
|
76
81
|
'form.autoReview.translate': '翻译审批理由',
|
|
77
82
|
'form.autoReview.translateHint': '英文审查理由会附上中文翻译,并写入审批事件;转手动后被拒时,自动审查理由会作为 notice 交回 AI',
|
|
78
83
|
'form.autoReview.keepPreset': '退出时保持权限预设',
|
|
@@ -108,6 +113,8 @@ export const zh = {
|
|
|
108
113
|
'page.tab.sessionEnd': '会话后动作',
|
|
109
114
|
'page.sessionEnd.title': '会话后动作',
|
|
110
115
|
'page.sessionEnd.description': '本轮最终回复完整显示后执行一次动作;重启会等待所有会话与后台任务结束,设置页的重启不受此约束。',
|
|
116
|
+
'page.components.authCookie': '认证 cookie 清理',
|
|
117
|
+
'page.components.authCookieHint': '自动运行,无设置',
|
|
111
118
|
'page.components.sessionEnd': '会话后动作',
|
|
112
119
|
'page.components.sessionEndHint': '输入框左侧的「会话后重启」下拉',
|
|
113
120
|
'form.sessionEnd.enabled': '启用会话后动作',
|
|
@@ -197,6 +204,11 @@ export const en = {
|
|
|
197
204
|
'form.failed': 'Save failed',
|
|
198
205
|
'form.unavailable': 'Cannot read the plugin settings: check that the profile entry id is mcviseron',
|
|
199
206
|
'form.autoSave': 'Changes save automatically',
|
|
207
|
+
'form.autoReview.reviewGuard': 'Trim the Auto-review context',
|
|
208
|
+
'form.autoReview.reviewGuardHint': 'Compacts only the reviewer history; the pending action, project constraints and human instructions stay',
|
|
209
|
+
'form.autoReview.reviewMaxHistory': 'Reviewer history limit (chars)',
|
|
210
|
+
'form.autoReview.reviewMaxHistoryHint': 'Defaults to 120000; older tool calls collapse into a summary',
|
|
211
|
+
'form.autoReview.reviewKeepRecent': 'Recent entries to keep',
|
|
200
212
|
'form.autoReview.translate': 'Translate the approval reason',
|
|
201
213
|
'form.autoReview.translateHint': 'Appends a Chinese translation to the reviewer note and writes it into the approval event; a manual rejection returns the auto-review reason to the AI',
|
|
202
214
|
'form.autoReview.keepPreset': 'Keep the permission preset on shutdown',
|
|
@@ -232,6 +244,8 @@ export const en = {
|
|
|
232
244
|
'page.tab.sessionEnd': 'After-session action',
|
|
233
245
|
'page.sessionEnd.title': 'After-session action',
|
|
234
246
|
'page.sessionEnd.description': 'One action after the final reply is fully displayed. A restart waits for every running conversation and background job; the Settings restart button is not delayed.',
|
|
247
|
+
'page.components.authCookie': 'Auth-cookie cleanup',
|
|
248
|
+
'page.components.authCookieHint': 'Runs automatically; no settings',
|
|
235
249
|
'page.components.sessionEnd': 'After-session action',
|
|
236
250
|
'page.components.sessionEndHint': 'The “Restart after session” dropdown left of the composer',
|
|
237
251
|
'form.sessionEnd.enabled': 'Enable after-session actions',
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto'
|
|
2
2
|
import type { Context } from '@deepseek-ai/cordis'
|
|
3
|
+
import { installReviewContextGuard, type ReviewContextSettings } from './review-context.ts'
|
|
3
4
|
import {
|
|
4
5
|
chineseKey,
|
|
5
6
|
cleanTranslation,
|
|
@@ -42,6 +43,7 @@ const FIBER_STATE_ACTIVE = 2
|
|
|
42
43
|
/** Settings actually used at runtime. */
|
|
43
44
|
export interface AutoReviewSettings extends TranslationSettings {
|
|
44
45
|
keepPresetGuard: boolean
|
|
46
|
+
reviewContext: ReviewContextSettings
|
|
45
47
|
}
|
|
46
48
|
|
|
47
49
|
/** Service objects already guarded in this process (survives plugin reloads). */
|
|
@@ -87,6 +89,11 @@ export function normalizeConfig(config?: unknown): AutoReviewSettings {
|
|
|
87
89
|
maxChars: numberSetting(source, 'translateMaxChars', 4000, 100, 20000),
|
|
88
90
|
provider: stringSetting(source, 'translateProvider', ''),
|
|
89
91
|
model: stringSetting(source, 'translateModel', ''),
|
|
92
|
+
reviewContext: {
|
|
93
|
+
enabled: boolSetting(source, 'reviewContextGuard', true),
|
|
94
|
+
maxHistoryChars: numberSetting(source, 'reviewMaxHistoryChars', 120000, 20000, 600000),
|
|
95
|
+
keepRecentEntries: numberSetting(source, 'reviewKeepRecentEntries', 16, 4, 64),
|
|
96
|
+
},
|
|
90
97
|
}
|
|
91
98
|
}
|
|
92
99
|
|
|
@@ -410,6 +417,7 @@ function installApprovalTranslation(ctx: Context, settings: AutoReviewSettings,
|
|
|
410
417
|
export function installAutoReview(ctx: Context, config?: unknown): void {
|
|
411
418
|
const settings = normalizeConfig(config)
|
|
412
419
|
if (settings.keepPresetGuard) installPresetGuard(ctx)
|
|
420
|
+
if (settings.reviewContext.enabled) installReviewContextGuard(ctx, settings.reviewContext)
|
|
413
421
|
if (settings.enabled) {
|
|
414
422
|
const relay = createRejectionRelay()
|
|
415
423
|
installRejectionRelay(ctx, relay)
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import { appendFileSync } from 'node:fs'
|
|
2
|
+
import { tmpdir } from 'node:os'
|
|
3
|
+
import { join } from 'node:path'
|
|
4
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Auto review serializes the whole session surface into FILTERED_HISTORY and
|
|
8
|
+
* asks the reviewer model to judge one pending call. A long session can exceed
|
|
9
|
+
* the reviewer's context window before it even starts. This module projects
|
|
10
|
+
* only the reviewer request: current action, project instructions, and every
|
|
11
|
+
* human/direct-parent instruction stay; old tool calls and oversized payloads
|
|
12
|
+
* are compacted.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export interface ReviewContextSettings {
|
|
16
|
+
enabled: boolean
|
|
17
|
+
maxHistoryChars: number
|
|
18
|
+
keepRecentEntries: number
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface ReviewOptions {
|
|
22
|
+
system?: unknown
|
|
23
|
+
messages?: unknown
|
|
24
|
+
[key: string]: unknown
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface ReviewMessage {
|
|
28
|
+
role?: unknown
|
|
29
|
+
content?: unknown
|
|
30
|
+
[key: string]: unknown
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const REVIEW_MARKER = 'REVIEW_POLICY'
|
|
34
|
+
const REVIEW_MARKER_2 = 'final authorization reviewer'
|
|
35
|
+
const HISTORY_HEADER = 'FILTERED_HISTORY\n\n'
|
|
36
|
+
const PROJECT_HEADER = 'PROJECT_INSTRUCTIONS\n\n'
|
|
37
|
+
const ACTION_HEADER = '\n\nPENDING_ACTION\n\n'
|
|
38
|
+
const USER_TEXT_CHARS = 4_000
|
|
39
|
+
const TOOL_ARGUMENT_CHARS = 2_000
|
|
40
|
+
const PROJECT_MAX_CHARS = 40_000
|
|
41
|
+
|
|
42
|
+
function record(value: unknown): value is Record<string, unknown> {
|
|
43
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function textOf(message: unknown): string | undefined {
|
|
47
|
+
if (!record(message) || !Array.isArray(message.content)) return undefined
|
|
48
|
+
const texts: string[] = []
|
|
49
|
+
for (const block of message.content) {
|
|
50
|
+
if (!record(block) || block.type !== 'text' || typeof block.text !== 'string') return undefined
|
|
51
|
+
texts.push(block.text)
|
|
52
|
+
}
|
|
53
|
+
return texts.join('')
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Detect the exact one-message request shape Auto review sends to llm.stream. */
|
|
57
|
+
export function isAutoReviewRequest(options: unknown): options is ReviewOptions {
|
|
58
|
+
if (!record(options)) return false
|
|
59
|
+
const system = options.system
|
|
60
|
+
if (typeof system !== 'string' || !system.includes(REVIEW_MARKER) || !system.includes(REVIEW_MARKER_2)) return false
|
|
61
|
+
if (!Array.isArray(options.messages) || options.messages.length === 0) return false
|
|
62
|
+
const text = textOf(options.messages[0])
|
|
63
|
+
return text !== undefined && text.includes(HISTORY_HEADER) && text.includes(ACTION_HEADER)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function truncateText(text: string, limit: number): string {
|
|
67
|
+
if (text.length <= limit) return text
|
|
68
|
+
return text.slice(0, limit) + '\n…[' + String(text.length - limit) + ' characters omitted]'
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function compactBlock(block: unknown): unknown {
|
|
72
|
+
if (!record(block)) return { type: 'text', text: '[non-text history block omitted from reviewer context]' }
|
|
73
|
+
if (block.type === 'text' && typeof block.text === 'string') {
|
|
74
|
+
return { ...block, text: truncateText(block.text, USER_TEXT_CHARS) }
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
type: 'text',
|
|
78
|
+
text: '[' + String(block.type ?? 'attachment') + ' omitted from reviewer context]',
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function compactEntry(entry: unknown): unknown {
|
|
83
|
+
if (!record(entry)) return { kind: 'summary', role: 'fact', content: [{ type: 'text', text: '[invalid history entry omitted]' }] }
|
|
84
|
+
if (entry.kind === 'user-message' && Array.isArray(entry.content)) {
|
|
85
|
+
return { ...entry, content: entry.content.map(compactBlock) }
|
|
86
|
+
}
|
|
87
|
+
if (entry.kind === 'tool-call') {
|
|
88
|
+
const value = entry.arguments
|
|
89
|
+
if (typeof value === 'string') {
|
|
90
|
+
return { ...entry, arguments: truncateText(value, TOOL_ARGUMENT_CHARS) }
|
|
91
|
+
}
|
|
92
|
+
if (value !== undefined) {
|
|
93
|
+
let rendered: string
|
|
94
|
+
try {
|
|
95
|
+
rendered = JSON.stringify(value) ?? ''
|
|
96
|
+
} catch {
|
|
97
|
+
rendered = ''
|
|
98
|
+
}
|
|
99
|
+
return { ...entry, arguments: truncateText(rendered, TOOL_ARGUMENT_CHARS) }
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return entry
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function protectedEntry(entry: unknown): boolean {
|
|
106
|
+
if (!record(entry)) return false
|
|
107
|
+
return entry.role === 'human-instruction' || entry.role === 'direct-parent-instruction'
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function summaryEntry(omitted: number, originalChars: number): Record<string, unknown> {
|
|
111
|
+
return {
|
|
112
|
+
kind: 'summary',
|
|
113
|
+
role: 'fact',
|
|
114
|
+
content: [{
|
|
115
|
+
type: 'text',
|
|
116
|
+
text: '[auto-review-fix] ' + String(omitted)
|
|
117
|
+
+ ' earlier history entries omitted from the reviewer context (original history '
|
|
118
|
+
+ String(originalChars) + ' chars). Current action and human instructions are retained.',
|
|
119
|
+
}],
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function jsonLength(value: unknown): number {
|
|
124
|
+
try {
|
|
125
|
+
return JSON.stringify(value)?.length ?? 0
|
|
126
|
+
} catch {
|
|
127
|
+
return Number.MAX_SAFE_INTEGER
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Compact and bound one history array while preserving authorization entries. */
|
|
132
|
+
export function boundEntries(entries: unknown[], maxChars: number, keepRecent: number): unknown[] {
|
|
133
|
+
const normalized = entries.map(compactEntry)
|
|
134
|
+
const originalChars = jsonLength(normalized)
|
|
135
|
+
const recentStart = Math.max(0, normalized.length - Math.max(1, keepRecent))
|
|
136
|
+
const selected = normalized.filter((entry, index) => protectedEntry(entry) || index >= recentStart)
|
|
137
|
+
let omitted = normalized.length - selected.length
|
|
138
|
+
while (selected.length > 1 && jsonLength(selected) > maxChars) {
|
|
139
|
+
const at = selected.findIndex(entry => !protectedEntry(entry))
|
|
140
|
+
if (at < 0) selected.shift()
|
|
141
|
+
else selected.splice(at, 1)
|
|
142
|
+
omitted += 1
|
|
143
|
+
}
|
|
144
|
+
if (omitted > 0) selected.unshift(summaryEntry(omitted, originalChars))
|
|
145
|
+
return selected
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function parseArray(text: string): unknown[] | undefined {
|
|
149
|
+
try {
|
|
150
|
+
const value = JSON.parse(text) as unknown
|
|
151
|
+
return Array.isArray(value) ? value : undefined
|
|
152
|
+
} catch {
|
|
153
|
+
return undefined
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function logTrim(originalChars: number, nextChars: number, originalEntries: number, nextEntries: number): void {
|
|
158
|
+
try {
|
|
159
|
+
appendFileSync(
|
|
160
|
+
join(tmpdir(), 'dsh-mcviseron-auto-review.log'),
|
|
161
|
+
new Date().toISOString() + ' review context trimmed: ' + String(originalChars) + ' -> '
|
|
162
|
+
+ String(nextChars) + ' chars, history ' + String(originalEntries) + ' -> ' + String(nextEntries) + '\n',
|
|
163
|
+
'utf8',
|
|
164
|
+
)
|
|
165
|
+
} catch {
|
|
166
|
+
/* diagnostics must never break the reviewer request */
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Return a copy of a reviewer request with bounded FILTERED_HISTORY and compact
|
|
172
|
+
* project instructions. Non-review requests and malformed requests are returned
|
|
173
|
+
* byte-for-byte unchanged.
|
|
174
|
+
*/
|
|
175
|
+
export function trimReviewRequest<T extends ReviewOptions>(options: T, settings: ReviewContextSettings): T {
|
|
176
|
+
if (!settings.enabled || !isAutoReviewRequest(options)) return options
|
|
177
|
+
const messages = options.messages as ReviewMessage[]
|
|
178
|
+
const message = messages[0]
|
|
179
|
+
const text = textOf(message)
|
|
180
|
+
if (text === undefined) return options
|
|
181
|
+
|
|
182
|
+
const projectAt = text.indexOf(PROJECT_HEADER)
|
|
183
|
+
const historyAt = text.indexOf(HISTORY_HEADER, projectAt + 1)
|
|
184
|
+
const actionAt = text.indexOf(ACTION_HEADER, historyAt + 1)
|
|
185
|
+
if (projectAt < 0 || historyAt < 0 || actionAt < 0) return options
|
|
186
|
+
|
|
187
|
+
const projectJson = text.slice(projectAt + PROJECT_HEADER.length, historyAt - 2)
|
|
188
|
+
const historyJson = text.slice(historyAt + HISTORY_HEADER.length, actionAt)
|
|
189
|
+
const project = parseArray(projectJson)
|
|
190
|
+
const history = parseArray(historyJson)
|
|
191
|
+
if (project === undefined || history === undefined) return options
|
|
192
|
+
|
|
193
|
+
const nextProject = boundEntries(project, PROJECT_MAX_CHARS, 4)
|
|
194
|
+
const nextHistory = boundEntries(history, settings.maxHistoryChars, settings.keepRecentEntries)
|
|
195
|
+
const nextText = text.slice(0, projectAt)
|
|
196
|
+
+ PROJECT_HEADER + JSON.stringify(nextProject)
|
|
197
|
+
+ HISTORY_HEADER + JSON.stringify(nextHistory)
|
|
198
|
+
+ text.slice(actionAt)
|
|
199
|
+
if (nextText === text) return options
|
|
200
|
+
|
|
201
|
+
logTrim(text.length, nextText.length, history.length, nextHistory.length)
|
|
202
|
+
const content = [{ type: 'text', text: nextText }]
|
|
203
|
+
return {
|
|
204
|
+
...options,
|
|
205
|
+
messages: [
|
|
206
|
+
{ ...message, content },
|
|
207
|
+
...messages.slice(1),
|
|
208
|
+
],
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const guardedLlms = new WeakMap<object, { settings: ReviewContextSettings }>()
|
|
213
|
+
|
|
214
|
+
function whenService(ctx: Context, name: string, run: (scope: Context) => void): void {
|
|
215
|
+
try {
|
|
216
|
+
const inject = (ctx as unknown as { inject?: (deps: string[], callback: (scope: Context) => void) => unknown }).inject
|
|
217
|
+
if (typeof inject === 'function') {
|
|
218
|
+
inject.call(ctx, [name], run)
|
|
219
|
+
return
|
|
220
|
+
}
|
|
221
|
+
const get = (ctx as unknown as { get?: (key: string) => unknown }).get
|
|
222
|
+
if (typeof get === 'function' && get.call(ctx, name) !== undefined) run(ctx)
|
|
223
|
+
} catch {
|
|
224
|
+
/* a host without the seam keeps the original llm.stream */
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function serviceOf(scope: Context, name: string): unknown {
|
|
229
|
+
try {
|
|
230
|
+
const direct = (scope as unknown as Record<string, unknown>)[name]
|
|
231
|
+
if (direct !== undefined) return direct
|
|
232
|
+
} catch {
|
|
233
|
+
/* fall through */
|
|
234
|
+
}
|
|
235
|
+
try {
|
|
236
|
+
const get = (scope as unknown as { get?: (key: string) => unknown }).get
|
|
237
|
+
return typeof get === 'function' ? get.call(scope, name) : undefined
|
|
238
|
+
} catch {
|
|
239
|
+
return undefined
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function effectOf(scope: Context, setup: () => () => void, label: string): void {
|
|
244
|
+
try {
|
|
245
|
+
const effect = (scope as unknown as { effect?: (body: () => () => void, label: string) => unknown }).effect
|
|
246
|
+
if (typeof effect === 'function') effect.call(scope, setup, label)
|
|
247
|
+
} catch {
|
|
248
|
+
/* a host without effect() still gets the wrapper */
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** Wrap llm.stream, but only rewrite requests that carry the Auto-review policy. */
|
|
253
|
+
export function installReviewContextGuard(ctx: Context, settings: ReviewContextSettings): void {
|
|
254
|
+
if (!settings.enabled) return
|
|
255
|
+
whenService(ctx, 'llm', (scope) => {
|
|
256
|
+
const llm = serviceOf(scope, 'llm') as { stream?: unknown } | undefined
|
|
257
|
+
if (llm === null || typeof llm !== 'object' || typeof llm.stream !== 'function') return
|
|
258
|
+
const existing = guardedLlms.get(llm)
|
|
259
|
+
if (existing !== undefined) {
|
|
260
|
+
existing.settings = settings
|
|
261
|
+
return
|
|
262
|
+
}
|
|
263
|
+
const original = llm.stream as (this: unknown, options: unknown) => unknown
|
|
264
|
+
const state = { settings }
|
|
265
|
+
const wrapper = function (this: unknown, options: unknown): unknown {
|
|
266
|
+
let next = options
|
|
267
|
+
try {
|
|
268
|
+
next = trimReviewRequest(options as ReviewOptions, state.settings)
|
|
269
|
+
} catch {
|
|
270
|
+
next = options
|
|
271
|
+
}
|
|
272
|
+
return original.call(this, next)
|
|
273
|
+
}
|
|
274
|
+
try {
|
|
275
|
+
llm.stream = wrapper
|
|
276
|
+
guardedLlms.set(llm, state)
|
|
277
|
+
} catch {
|
|
278
|
+
return
|
|
279
|
+
}
|
|
280
|
+
effectOf(scope, () => () => {
|
|
281
|
+
if (llm.stream === wrapper) llm.stream = original
|
|
282
|
+
guardedLlms.delete(llm)
|
|
283
|
+
}, 'mcviseron: restore llm.stream')
|
|
284
|
+
})
|
|
285
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Context } from '@deepseek-ai/cordis'
|
|
2
2
|
import z from '@deepseek-ai/schemastery'
|
|
3
3
|
import * as appearance from './appearance/index.ts'
|
|
4
|
+
import * as authCookie from './auth-cookie/index.ts'
|
|
4
5
|
import * as autoReviewFix from './auto-review-fix/index.ts'
|
|
5
6
|
import { DEFAULT_AUTO_REVIEW_CONFIG } from './auto-review-fix/index.ts'
|
|
6
7
|
import * as restart from './restart/index.ts'
|
|
@@ -65,6 +66,7 @@ function mount(ctx: Context, component: unknown, config: unknown): void {
|
|
|
65
66
|
*/
|
|
66
67
|
export function apply(ctx: Context, rawConfig?: MasterConfig): void {
|
|
67
68
|
const config = (toPlain(rawConfig) ?? {}) as MasterConfig
|
|
69
|
+
mount(ctx, authCookie, {})
|
|
68
70
|
mount(ctx, appearance, {
|
|
69
71
|
enabled: config.enabled ?? DEFAULT_SCHEDULE_CONFIG.enabled,
|
|
70
72
|
lightStart: config.lightStart ?? DEFAULT_SCHEDULE_CONFIG.lightStart,
|
package/src/session-end/index.ts
CHANGED
|
@@ -90,9 +90,16 @@ export interface SessionEndDeps {
|
|
|
90
90
|
log?: (message: string) => void
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
+
export interface SessionEndGlobalAction extends SessionEndView {
|
|
94
|
+
sessionId: string
|
|
95
|
+
label: string
|
|
96
|
+
armedAt: number
|
|
97
|
+
}
|
|
98
|
+
|
|
93
99
|
export interface SessionEndController {
|
|
94
100
|
get(sessionId: string): SessionEndView
|
|
95
|
-
|
|
101
|
+
list(): SessionEndGlobalAction[]
|
|
102
|
+
set(sessionId: string, mode: SessionEndMode, requestedBy: 'user' | 'ai', label?: string): SessionEndView
|
|
96
103
|
onTurnStart(sessionId: string): void
|
|
97
104
|
onTurnEnd(sessionId: string): void
|
|
98
105
|
dispose(): void
|
|
@@ -101,6 +108,8 @@ export interface SessionEndController {
|
|
|
101
108
|
interface Entry {
|
|
102
109
|
mode: Exclude<SessionEndMode, 'off'>
|
|
103
110
|
requestedBy: 'user' | 'ai'
|
|
111
|
+
label?: string
|
|
112
|
+
armedAt: number
|
|
104
113
|
waiting: boolean
|
|
105
114
|
runningAgents: number
|
|
106
115
|
runningJobs: number
|
|
@@ -203,6 +212,7 @@ export function createSessionEndController(deps: SessionEndDeps = {}): SessionEn
|
|
|
203
212
|
const log = deps.log ?? defaultLog
|
|
204
213
|
const entries = new Map<string, Entry>()
|
|
205
214
|
const timers = new Map<string, ReturnType<typeof setTimeout>>()
|
|
215
|
+
let dispatching = false
|
|
206
216
|
const resolveTarget = deps.resolveTarget ?? (() => resolveRestartTarget(
|
|
207
217
|
process.execPath,
|
|
208
218
|
process.ppid,
|
|
@@ -258,7 +268,18 @@ export function createSessionEndController(deps: SessionEndDeps = {}): SessionEn
|
|
|
258
268
|
|
|
259
269
|
const fire = (sessionId: string, entry: Entry): void => {
|
|
260
270
|
cancel(sessionId)
|
|
261
|
-
|
|
271
|
+
if (dispatching) {
|
|
272
|
+
entries.delete(sessionId)
|
|
273
|
+
return
|
|
274
|
+
}
|
|
275
|
+
// One process action wins. A second session must not schedule another
|
|
276
|
+
// restart/close task while the first is already leaving the process.
|
|
277
|
+
dispatching = true
|
|
278
|
+
for (const [id, timer] of timers) {
|
|
279
|
+
clearTimeout(timer)
|
|
280
|
+
timers.delete(id)
|
|
281
|
+
}
|
|
282
|
+
entries.clear()
|
|
262
283
|
const mode = entry.mode
|
|
263
284
|
try {
|
|
264
285
|
if (mode === 'restart') {
|
|
@@ -309,7 +330,15 @@ export function createSessionEndController(deps: SessionEndDeps = {}): SessionEn
|
|
|
309
330
|
get(sessionId) {
|
|
310
331
|
return view(sessionId)
|
|
311
332
|
},
|
|
312
|
-
|
|
333
|
+
list() {
|
|
334
|
+
return [...entries.entries()].map(([sessionId, entry]) => ({
|
|
335
|
+
sessionId,
|
|
336
|
+
label: entry.label ?? '',
|
|
337
|
+
armedAt: entry.armedAt,
|
|
338
|
+
...view(sessionId),
|
|
339
|
+
}))
|
|
340
|
+
},
|
|
341
|
+
set(sessionId, mode, requestedBy, label) {
|
|
313
342
|
if (sessionId === '') throw new TypeError('sessionId must be non-empty')
|
|
314
343
|
if (!enabled) throw new Error('session-end actions are disabled')
|
|
315
344
|
if (!isSessionEndMode(mode)) throw new TypeError('unknown session-end mode: ' + String(mode))
|
|
@@ -324,6 +353,8 @@ export function createSessionEndController(deps: SessionEndDeps = {}): SessionEn
|
|
|
324
353
|
entries.set(sessionId, {
|
|
325
354
|
mode,
|
|
326
355
|
requestedBy,
|
|
356
|
+
...(label === undefined || label === '' ? {} : { label }),
|
|
357
|
+
armedAt: Date.now(),
|
|
327
358
|
waiting: false,
|
|
328
359
|
runningAgents: 0,
|
|
329
360
|
runningJobs: 0,
|
|
@@ -351,6 +382,7 @@ export function createSessionEndController(deps: SessionEndDeps = {}): SessionEn
|
|
|
351
382
|
for (const timer of timers.values()) clearTimeout(timer)
|
|
352
383
|
timers.clear()
|
|
353
384
|
entries.clear()
|
|
385
|
+
dispatching = false
|
|
354
386
|
},
|
|
355
387
|
}
|
|
356
388
|
}
|
|
@@ -424,7 +456,26 @@ export function makeSessionEndRoutes(controller: SessionEndController): WebRoute
|
|
|
424
456
|
},
|
|
425
457
|
}
|
|
426
458
|
|
|
427
|
-
|
|
459
|
+
const global: WebRoute = {
|
|
460
|
+
kind: 'exact',
|
|
461
|
+
path: SESSION_END_API.global,
|
|
462
|
+
handler: (req, res): void => {
|
|
463
|
+
if (req.method !== 'POST' && req.method !== 'GET') {
|
|
464
|
+
json(res, 405, { ok: false, error: 'method-not-allowed' })
|
|
465
|
+
return
|
|
466
|
+
}
|
|
467
|
+
if (!isLoopbackRequest(req)) {
|
|
468
|
+
json(res, 403, { ok: false, error: 'forbidden' })
|
|
469
|
+
return
|
|
470
|
+
}
|
|
471
|
+
json(res, 200, {
|
|
472
|
+
ok: true,
|
|
473
|
+
actions: controller.list().filter(action => action.requestedBy === 'ai' && action.mode !== 'off'),
|
|
474
|
+
})
|
|
475
|
+
},
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
return [state, set, global]
|
|
428
479
|
}
|
|
429
480
|
|
|
430
481
|
function serviceOf(ctx: Context, name: string): unknown {
|
|
@@ -487,15 +538,19 @@ function installAgentTool(ctx: Context, controller: SessionEndController, option
|
|
|
487
538
|
'Use close only when the user explicitly asks to close DSH. Shutdown is user-only. If unsure whether a restart is required, ask the user instead of arming.',
|
|
488
539
|
].join(' '),
|
|
489
540
|
parameters: {
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
541
|
+
type: 'object',
|
|
542
|
+
additionalProperties: false,
|
|
543
|
+
required: ['action'],
|
|
544
|
+
properties: {
|
|
545
|
+
action: {
|
|
546
|
+
type: 'string',
|
|
547
|
+
enum: ['restart', 'close', 'off'],
|
|
548
|
+
description: 'restart DSH after this turn, close DSH, or clear the pending action',
|
|
549
|
+
},
|
|
550
|
+
reason: {
|
|
551
|
+
type: 'string',
|
|
552
|
+
description: 'Optional short reason shown in the tool result; the user sees the same action in the composer',
|
|
553
|
+
},
|
|
499
554
|
},
|
|
500
555
|
},
|
|
501
556
|
output: {
|
|
@@ -531,16 +586,29 @@ function installAgentTool(ctx: Context, controller: SessionEndController, option
|
|
|
531
586
|
if (action === 'close' && !before.available.close) {
|
|
532
587
|
throw new Error('close is unavailable in this host')
|
|
533
588
|
}
|
|
534
|
-
|
|
589
|
+
const session = exec.agent?.session as { header?: { title?: unknown; name?: unknown }; title?: unknown } | undefined
|
|
590
|
+
const title = session?.header?.title ?? session?.header?.name ?? session?.title
|
|
591
|
+
let label = typeof title === 'string' && title !== '' ? title : ''
|
|
592
|
+
if (label === '') {
|
|
593
|
+
try {
|
|
594
|
+
const titles = serviceOf(ctx, 'sessionTitle') as { get?: (value: unknown) => unknown } | undefined
|
|
595
|
+
const snapshot = titles?.get?.(session)
|
|
596
|
+
const folded = snapshot !== null && typeof snapshot === 'object'
|
|
597
|
+
? (snapshot as { title?: unknown }).title
|
|
598
|
+
: undefined
|
|
599
|
+
if (typeof folded === 'string' && folded !== '') label = folded
|
|
600
|
+
} catch {
|
|
601
|
+
/* title is best-effort; the global notice falls back to the id */
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
controller.set(sessionId, action, 'ai', label)
|
|
535
605
|
if (action !== 'off' && options.notifyEmailOnAi) {
|
|
536
606
|
const key = sessionId + ':' + action
|
|
537
607
|
const now = Date.now()
|
|
538
608
|
const previous = emailed.get(key) ?? 0
|
|
539
609
|
if (now - previous > 60_000) {
|
|
540
610
|
emailed.set(key, now)
|
|
541
|
-
const
|
|
542
|
-
const title = session?.header?.title ?? session?.header?.name ?? session?.title
|
|
543
|
-
const sessionName = typeof title === 'string' && title !== '' ? title : sessionId
|
|
611
|
+
const sessionName = label === '' ? sessionId : label
|
|
544
612
|
void sendSessionEndEmail({
|
|
545
613
|
settings: serviceOf(ctx, 'settings'),
|
|
546
614
|
template: options,
|