dsh-mcviseron-plugin 0.6.5 → 0.6.6
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 +16 -0
- package/README.en.md +16 -4
- package/README.md +18 -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/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 +546 -116
- package/package.json +3 -3
- 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 +72 -8
|
@@ -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 {
|
|
@@ -531,16 +582,29 @@ function installAgentTool(ctx: Context, controller: SessionEndController, option
|
|
|
531
582
|
if (action === 'close' && !before.available.close) {
|
|
532
583
|
throw new Error('close is unavailable in this host')
|
|
533
584
|
}
|
|
534
|
-
|
|
585
|
+
const session = exec.agent?.session as { header?: { title?: unknown; name?: unknown }; title?: unknown } | undefined
|
|
586
|
+
const title = session?.header?.title ?? session?.header?.name ?? session?.title
|
|
587
|
+
let label = typeof title === 'string' && title !== '' ? title : ''
|
|
588
|
+
if (label === '') {
|
|
589
|
+
try {
|
|
590
|
+
const titles = serviceOf(ctx, 'sessionTitle') as { get?: (value: unknown) => unknown } | undefined
|
|
591
|
+
const snapshot = titles?.get?.(session)
|
|
592
|
+
const folded = snapshot !== null && typeof snapshot === 'object'
|
|
593
|
+
? (snapshot as { title?: unknown }).title
|
|
594
|
+
: undefined
|
|
595
|
+
if (typeof folded === 'string' && folded !== '') label = folded
|
|
596
|
+
} catch {
|
|
597
|
+
/* title is best-effort; the global notice falls back to the id */
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
controller.set(sessionId, action, 'ai', label)
|
|
535
601
|
if (action !== 'off' && options.notifyEmailOnAi) {
|
|
536
602
|
const key = sessionId + ':' + action
|
|
537
603
|
const now = Date.now()
|
|
538
604
|
const previous = emailed.get(key) ?? 0
|
|
539
605
|
if (now - previous > 60_000) {
|
|
540
606
|
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
|
|
607
|
+
const sessionName = label === '' ? sessionId : label
|
|
544
608
|
void sendSessionEndEmail({
|
|
545
609
|
settings: serviceOf(ctx, 'settings'),
|
|
546
610
|
template: options,
|