dsh-mcviseron-plugin 0.6.2 → 0.6.4

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/src/index.ts CHANGED
@@ -4,6 +4,8 @@ import * as appearance from './appearance/index.ts'
4
4
  import * as autoReviewFix from './auto-review-fix/index.ts'
5
5
  import { DEFAULT_AUTO_REVIEW_CONFIG } from './auto-review-fix/index.ts'
6
6
  import * as restart from './restart/index.ts'
7
+ import * as sessionEnd from './session-end/index.ts'
8
+ import { DEFAULT_SESSION_END_CONFIG } from './session-end/index.ts'
7
9
  import { DEFAULT_RESTART_CONFIG } from './restart/index.ts'
8
10
  import * as taskGuard from './task-guard/index.ts'
9
11
  import { DEFAULT_TASK_GUARD_CONFIG } from './task-guard/index.ts'
@@ -24,6 +26,7 @@ export const Config = z.object({
24
26
  autoReviewFix: autoReviewFix.Config,
25
27
  taskGuard: taskGuard.Config,
26
28
  restart: restart.Config,
29
+ sessionEnd: sessionEnd.Config,
27
30
  }).default({
28
31
  enabled: DEFAULT_SCHEDULE_CONFIG.enabled,
29
32
  lightStart: DEFAULT_SCHEDULE_CONFIG.lightStart,
@@ -31,6 +34,7 @@ export const Config = z.object({
31
34
  autoReviewFix: DEFAULT_AUTO_REVIEW_CONFIG,
32
35
  taskGuard: DEFAULT_TASK_GUARD_CONFIG,
33
36
  restart: DEFAULT_RESTART_CONFIG,
37
+ sessionEnd: DEFAULT_SESSION_END_CONFIG,
34
38
  })
35
39
 
36
40
  export interface MasterConfig {
@@ -40,6 +44,7 @@ export interface MasterConfig {
40
44
  autoReviewFix?: unknown
41
45
  taskGuard?: unknown
42
46
  restart?: unknown
47
+ sessionEnd?: unknown
43
48
  }
44
49
 
45
50
  /** Mount one component without letting a missing service fail the master entry. */
@@ -67,5 +72,11 @@ export function apply(ctx: Context, rawConfig?: MasterConfig): void {
67
72
  })
68
73
  mount(ctx, autoReviewFix, toPlain(config.autoReviewFix) ?? DEFAULT_AUTO_REVIEW_CONFIG)
69
74
  mount(ctx, taskGuard, toPlain(config.taskGuard) ?? DEFAULT_TASK_GUARD_CONFIG)
70
- mount(ctx, restart, toPlain(config.restart) ?? DEFAULT_RESTART_CONFIG)
75
+ const restartConfig = (toPlain(config.restart) ?? DEFAULT_RESTART_CONFIG) as { appPath?: unknown }
76
+ mount(ctx, restart, restartConfig)
77
+ const sessionEndConfig = (toPlain(config.sessionEnd) ?? DEFAULT_SESSION_END_CONFIG) as Record<string, unknown>
78
+ mount(ctx, sessionEnd, {
79
+ ...sessionEndConfig,
80
+ appPath: typeof restartConfig.appPath === 'string' ? restartConfig.appPath : '',
81
+ })
71
82
  }
@@ -135,6 +135,35 @@ export function makeTaskScript(target: RestartTarget, taskName: string, delaySec
135
135
  ].join('\n')
136
136
  }
137
137
 
138
+ /**
139
+ * Schedule the desktop app to start again, then close its Electron main process.
140
+ * Exported for the session-end action; the settings route keeps its own throttle.
141
+ * @param target - resolved desktop executable and PIDs.
142
+ */
143
+ export function launchRestart(target: RestartTarget): void {
144
+ launchHelper(target)
145
+ }
146
+
147
+ /**
148
+ * Close the desktop app without relaunching it.
149
+ * @param target - resolved desktop target, when available.
150
+ */
151
+ export function closeDesktop(target?: RestartTarget): void {
152
+ const pid = target?.mainPid ?? process.ppid
153
+ setTimeout(() => {
154
+ try {
155
+ if (target !== undefined || process.platform === 'win32') process.kill(pid)
156
+ else process.exit(0)
157
+ } catch {
158
+ try {
159
+ process.exit(0)
160
+ } catch {
161
+ /* the host is already gone */
162
+ }
163
+ }
164
+ }, 300)
165
+ }
166
+
138
167
  /** Register the direct-launch task, then close the Electron main process. */
139
168
  function launchHelper(target: RestartTarget): void {
140
169
  const logFile = restartLogPath()
package/src/routes.ts CHANGED
@@ -63,6 +63,11 @@ const FIELD_RULES: Record<string, FieldRule> = {
63
63
  'taskGuard.exclude': { type: 'strings' },
64
64
  'restart.enabled': { type: 'boolean' },
65
65
  'restart.appPath': { type: 'string' },
66
+ 'sessionEnd.enabled': { type: 'boolean' },
67
+ 'sessionEnd.graceMs': { type: 'number', min: 0, max: 15000 },
68
+ 'sessionEnd.notifyEmailOnAi': { type: 'boolean' },
69
+ 'sessionEnd.emailSubject': { type: 'string' },
70
+ 'sessionEnd.emailBody': { type: 'string' },
66
71
  }
67
72
 
68
73
  function ruleFor(path: unknown): FieldRule | undefined {
@@ -146,6 +151,7 @@ function toPluginView(descriptor: SettingsBridgeView): Record<string, unknown> {
146
151
  autoReviewFix: isRecord(record.autoReviewFix) ? record.autoReviewFix : {},
147
152
  taskGuard: isRecord(record.taskGuard) ? record.taskGuard : {},
148
153
  restart: isRecord(record.restart) ? record.restart : {},
154
+ sessionEnd: isRecord(record.sessionEnd) ? record.sessionEnd : {},
149
155
  },
150
156
  revision: descriptor.revision,
151
157
  }
@@ -0,0 +1,163 @@
1
+ import nodemailer from 'nodemailer'
2
+ import { toPlain } from '../volatile.ts'
3
+ import {
4
+ DEFAULT_SESSION_END_CONFIG,
5
+ type SessionEndOptions,
6
+ } from './index.ts'
7
+
8
+ /**
9
+ * Optional integration with dsh-email-notify.
10
+ *
11
+ * The email plugin does not expose a notification service, so this module
12
+ * reads its public settings namespace (the same SMTP account the user already
13
+ * configured) and sends one custom message through nodemailer. It never
14
+ * modifies the email plugin and sends nothing when the plugin is absent,
15
+ * disabled, or missing any sendable SMTP field.
16
+ */
17
+
18
+ export interface EmailNotifyConfig {
19
+ smtpHost: string
20
+ smtpPort: number
21
+ smtpSecure: boolean
22
+ smtpUser: string
23
+ smtpPassword: string
24
+ mailFrom: string
25
+ mailTo: string
26
+ }
27
+
28
+ export interface SessionEndEmailInput {
29
+ settings: unknown
30
+ template: SessionEndOptions
31
+ sessionId: string
32
+ sessionName: string
33
+ action: 'restart' | 'close'
34
+ reason: string
35
+ now?: () => Date
36
+ sendMail?: (email: EmailNotifyConfig, message: { subject: string; text: string }) => Promise<void>
37
+ }
38
+
39
+ export interface SessionEndEmailResult {
40
+ sent: boolean
41
+ reason?: string
42
+ }
43
+
44
+ interface SettingsFace {
45
+ describe?: (options?: { redactSecrets?: boolean }) => unknown
46
+ }
47
+
48
+ function text(value: unknown): string {
49
+ return typeof value === 'string' ? value.trim() : ''
50
+ }
51
+
52
+ function readNumber(value: unknown, fallback: number): number {
53
+ return typeof value === 'number' && Number.isFinite(value) && value > 0 && value <= 65535
54
+ ? Math.trunc(value)
55
+ : fallback
56
+ }
57
+
58
+ /** Extract the live email-notify config from one settings describe view. */
59
+ export function emailNotifyConfigFromView(view: unknown): EmailNotifyConfig | undefined {
60
+ if (view === null || typeof view !== 'object' || Array.isArray(view)) return undefined
61
+ const value = toPlain((view as { value?: unknown }).value)
62
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) return undefined
63
+ const source = value as Record<string, unknown>
64
+ if (source.enabled === false) return undefined
65
+ const smtpHost = text(source.smtpHost)
66
+ const smtpUser = text(source.smtpUser)
67
+ const smtpPassword = text(source.smtpPassword)
68
+ const mailTo = text(source.mailTo)
69
+ const mailFrom = text(source.mailFrom) || smtpUser
70
+ if (smtpHost === '' || smtpUser === '' || smtpPassword === '' || mailTo === '' || mailFrom === '') return undefined
71
+ return {
72
+ smtpHost,
73
+ smtpPort: readNumber(source.smtpPort, 465),
74
+ smtpSecure: source.smtpSecure !== false,
75
+ smtpUser,
76
+ smtpPassword,
77
+ mailFrom,
78
+ mailTo,
79
+ }
80
+ }
81
+
82
+ /** Read the dsh-email-notify namespace from the live settings service. */
83
+ export function resolveEmailNotifyConfig(settings: unknown): EmailNotifyConfig | undefined {
84
+ const describe = (settings as SettingsFace | undefined)?.describe
85
+ if (typeof describe !== 'function') return undefined
86
+ try {
87
+ const views = describe.call(settings, { redactSecrets: false })
88
+ if (!Array.isArray(views)) return undefined
89
+ const view = views.find(item => String((item as { ns?: unknown })?.ns ?? '') === 'email-notify')
90
+ return emailNotifyConfigFromView(view)
91
+ } catch {
92
+ return undefined
93
+ }
94
+ }
95
+
96
+ function actionText(action: 'restart' | 'close'): string {
97
+ return action === 'restart' ? '重启 DeepSeek Harness' : '关闭 DeepSeek Harness'
98
+ }
99
+
100
+ /** Render the custom subject/body with the same simple placeholder model. */
101
+ export function renderSessionEndEmail(
102
+ template: SessionEndOptions,
103
+ input: {
104
+ sessionId: string
105
+ sessionName: string
106
+ action: 'restart' | 'close'
107
+ reason: string
108
+ now: Date
109
+ },
110
+ ): { subject: string; text: string } {
111
+ const vars: Record<string, string> = {
112
+ action: input.action,
113
+ actionText: actionText(input.action),
114
+ sessionId: input.sessionId,
115
+ sessionName: input.sessionName === '' ? input.sessionId : input.sessionName,
116
+ time: input.now.toLocaleString('zh-CN', { hour12: false }),
117
+ reason: input.reason === '' ? '(未提供)' : input.reason,
118
+ }
119
+ const render = (source: string): string => source.replace(/\{\{([a-zA-Z]+)\}\}/g, (_match, key: string) => vars[key] ?? '')
120
+ return {
121
+ subject: render(template.emailSubject ?? DEFAULT_SESSION_END_CONFIG.emailSubject),
122
+ text: render(template.emailBody ?? DEFAULT_SESSION_END_CONFIG.emailBody),
123
+ }
124
+ }
125
+
126
+ async function defaultSendMail(email: EmailNotifyConfig, message: { subject: string; text: string }): Promise<void> {
127
+ const transport = nodemailer.createTransport({
128
+ host: email.smtpHost,
129
+ port: email.smtpPort,
130
+ secure: email.smtpSecure,
131
+ auth: { user: email.smtpUser, pass: email.smtpPassword },
132
+ })
133
+ await transport.sendMail({
134
+ from: email.mailFrom,
135
+ to: email.mailTo,
136
+ subject: message.subject,
137
+ text: message.text,
138
+ })
139
+ }
140
+
141
+ /**
142
+ * Send the AI-armed session-end reminder when email-notify is configured.
143
+ * Never throws: email is an optional side channel and must not block the tool.
144
+ */
145
+ export async function sendSessionEndEmail(input: SessionEndEmailInput): Promise<SessionEndEmailResult> {
146
+ if (input.template.notifyEmailOnAi === false) return { sent: false, reason: 'disabled' }
147
+ const email = resolveEmailNotifyConfig(input.settings)
148
+ if (email === undefined) return { sent: false, reason: 'email-notify-unavailable' }
149
+ const now = input.now?.() ?? new Date()
150
+ const rendered = renderSessionEndEmail(input.template, {
151
+ sessionId: input.sessionId,
152
+ sessionName: input.sessionName,
153
+ action: input.action,
154
+ reason: input.reason,
155
+ now,
156
+ })
157
+ try {
158
+ await (input.sendMail ?? defaultSendMail)(email, rendered)
159
+ return { sent: true }
160
+ } catch (error) {
161
+ return { sent: false, reason: error instanceof Error ? error.message : String(error) }
162
+ }
163
+ }