@istarry/spoon-companion 0.8.0 → 0.9.0

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/lib/asr.js CHANGED
@@ -149,11 +149,14 @@ export function markCallSession(id, on) {
149
149
  else callSessions.delete(key)
150
150
  }
151
151
 
152
- /** 这个会话现在算不算在通话:全局开关 / 这个会话被标过 / 最近真的在送音频 */
152
+ /**
153
+ * 这个会话现在算不算在通话。
154
+ * **只认客户端那把开关** —— 点了通话才算,点挂断就不算。
155
+ * 音频活动不参与判断:录音可能在页面一打开就在跑,拿它当证据会把普通聊天误判成通话。
156
+ */
153
157
  export function isCallActive(sessionId) {
154
158
  if (callGlobal) return true
155
- if (sessionId && callSessions.has(String(sessionId))) return true
156
- return isVoiceActive()
159
+ return Boolean(sessionId && callSessions.has(String(sessionId)))
157
160
  }
158
161
 
159
162
  export function feed(pcm, rate = 16000) {
package/lib/prompt.js CHANGED
@@ -109,6 +109,38 @@ function relationLine(view) {
109
109
  return rows.join(' / ')
110
110
  }
111
111
 
112
+ /**
113
+ * 窗口标题太长、后缀一堆,收成一句人话。
114
+ * 不是"识别到的进程名",是她凑过去能看见的那行字。
115
+ */
116
+ export function shortTitle(title) {
117
+ let text = String(title ?? '').trim()
118
+ if (!text) return ''
119
+ text = text
120
+ .replace(/\s*[—–-]\s*DeepSeek Harness\s*$/i, '')
121
+ .replace(/\s*[—–-]\s*(Google Chrome|Microsoft.*Edge|Mozilla Firefox)\s*$/i, '')
122
+ .replace(/\s+/g, ' ')
123
+ .trim()
124
+ if (text.length > 34) text = `${text.slice(0, 34)}…`
125
+ return text
126
+ }
127
+
128
+ /**
129
+ * 「刚才走过去看了一眼」那一行。
130
+ * 没看过、或超过 30 分钟,就整行不出现 —— 不写"没看",也不提眼睛。
131
+ */
132
+ export function lookLine(view, now = Date.now()) {
133
+ const sc = view?.screen
134
+ const at = Number(sc?.lastAt) || 0
135
+ if (!at) return ''
136
+ const age = now - at
137
+ if (age > 30 * 60_000) return ''
138
+ const mins = Math.floor(age / 60_000)
139
+ const when = mins < 1 ? '刚刚' : `${mins} 分钟前`
140
+ const what = shortTitle(sc.title)
141
+ return what ? `刚才:走过去看了一眼 —— ${what}(${when})` : `刚才:走过去看了一眼(${when})`
142
+ }
143
+
112
144
  /**
113
145
  * 实时状态块。固定模板、固定字段顺序:
114
146
  * 第一行永远是标题,最后一行永远是回复参数 —— 中间按需增减,顺序不变。
@@ -134,6 +166,8 @@ export function renderStateBlock(view) {
134
166
  const times = view.scene.events > 1 ? ` · 第 ${view.scene.events} 次` : ''
135
167
  lines.push(`场景:${view.scene.word}${times}`)
136
168
  }
169
+ const look = lookLine(view, view.now)
170
+ if (look) lines.push(look)
137
171
  if (view.urge) lines.push(`想做的事:${view.urge.kind === 'share' ? '想把这件事说出去' : '想被安慰两句'}`)
138
172
  // 感受和表现是两回事:她可以心里翻江倒海,表面照旧嘴硬
139
173
  if (emo.hidden) {
package/lib/screen.js ADDED
@@ -0,0 +1,149 @@
1
+ // 看屏幕:截一张全屏图,存成 JPEG,把路径交回去。
2
+ //
3
+ // 设计上的几条硬约束:
4
+ // · 不落成"随手一堆文件" —— 只保留最近 N 张,旧的自己删。
5
+ // · 不常驻进程、不装依赖 —— 每次 spawn 一次 powershell,截完就退。
6
+ // · 不替调用方判断该不该看 —— 那是情绪那一层的事,这里只负责"能截到"。
7
+ //
8
+ // Windows 上用 System.Drawing + CopyFromScreen,不需要任何授权;
9
+ // macOS / Linux 直接返回 unsupported,让上层安静跳过。
10
+
11
+ import { spawn } from 'node:child_process'
12
+ import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, unlinkSync, writeFileSync } from 'node:fs'
13
+ import { homedir } from 'node:os'
14
+ import { join } from 'node:path'
15
+
16
+ /** 落盘目录:和情绪状态放一起,方便一起清理 */
17
+ export const SHOT_DIR = join(homedir(), '.dsh', 'fish-mood', 'shots')
18
+
19
+ /** 保留最近几张 */
20
+ const KEEP = 12
21
+
22
+ /** 截图脚本:纯 ASCII,避免编码问题。窗口标题单独写文件(UTF-8),不走 stdout。 */
23
+ const PS1 = `param([string]$Out, [int]$MaxEdge, [string]$TitleOut)
24
+ $ErrorActionPreference = 'Stop'
25
+ Add-Type -AssemblyName System.Drawing
26
+ Add-Type -AssemblyName System.Windows.Forms
27
+ Add-Type @"
28
+ using System;
29
+ using System.Runtime.InteropServices;
30
+ using System.Text;
31
+ public class WinInfo {
32
+ [DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow();
33
+ [DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern int GetWindowText(IntPtr h, StringBuilder s, int n);
34
+ }
35
+ "@
36
+ $title = ''
37
+ try {
38
+ $h = [WinInfo]::GetForegroundWindow()
39
+ $sb = New-Object System.Text.StringBuilder 512
40
+ [void][WinInfo]::GetWindowText($h, $sb, 512)
41
+ $title = $sb.ToString()
42
+ } catch { }
43
+ if ($TitleOut) { [System.IO.File]::WriteAllText($TitleOut, $title, (New-Object System.Text.UTF8Encoding $false)) }
44
+ $b = [System.Windows.Forms.SystemInformation]::VirtualScreen
45
+ if ($b.Width -le 0 -or $b.Height -le 0) { throw 'no-screen' }
46
+ $bmp = New-Object System.Drawing.Bitmap($b.Width, $b.Height)
47
+ $g = [System.Drawing.Graphics]::FromImage($bmp)
48
+ $g.CopyFromScreen($b.Left, $b.Top, 0, 0, $bmp.Size)
49
+ $g.Dispose()
50
+ $ratio = [Math]::Min(1.0, [double]$MaxEdge / [Math]::Max($bmp.Width, $bmp.Height))
51
+ if ($ratio -lt 1.0) {
52
+ $w = [int]($bmp.Width * $ratio)
53
+ $h2 = [int]($bmp.Height * $ratio)
54
+ $small = New-Object System.Drawing.Bitmap($w, $h2)
55
+ $sg = [System.Drawing.Graphics]::FromImage($small)
56
+ $sg.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
57
+ $sg.DrawImage($bmp, 0, 0, $w, $h2)
58
+ $sg.Dispose()
59
+ $bmp.Dispose()
60
+ $bmp = $small
61
+ }
62
+ $bmp.Save($Out, [System.Drawing.Imaging.ImageFormat]::Jpeg)
63
+ Write-Output ("{0}x{1}" -f $bmp.Width, $bmp.Height)
64
+ $bmp.Dispose()
65
+ `
66
+
67
+ /** Windows 自带的 powershell.exe。DSH 进程的 PATH 可能是旧的,所以写全路径。 */
68
+ const PS_EXE = 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe'
69
+
70
+ /** 这个平台能不能截 */
71
+ export function screenSupported() {
72
+ return process.platform === 'win32'
73
+ }
74
+
75
+ function run(cmd, args, timeoutMs) {
76
+ return new Promise((resolve, reject) => {
77
+ const child = spawn(cmd, args, { windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] })
78
+ let out = ''
79
+ let err = ''
80
+ const timer = setTimeout(() => {
81
+ try { child.kill() } catch { /* ignore */ }
82
+ reject(new Error('timeout'))
83
+ }, timeoutMs)
84
+ child.stdout.on('data', (d) => { out += d.toString() })
85
+ child.stderr.on('data', (d) => { err += d.toString() })
86
+ child.on('error', (e) => { clearTimeout(timer); reject(e) })
87
+ child.on('close', (code) => {
88
+ clearTimeout(timer)
89
+ if (code === 0) resolve(out.trim())
90
+ else reject(new Error((err || out || `exit ${code}`).trim().slice(0, 300)))
91
+ })
92
+ })
93
+ }
94
+
95
+ /** 只留最近 KEEP 张 */
96
+ export function pruneShots() {
97
+ try {
98
+ if (!existsSync(SHOT_DIR)) return 0
99
+ const rows = readdirSync(SHOT_DIR)
100
+ .filter((name) => name.endsWith('.jpg'))
101
+ .map((name) => ({ name, at: statSync(join(SHOT_DIR, name)).mtimeMs }))
102
+ .sort((a, b) => b.at - a.at)
103
+ let n = 0
104
+ for (const row of rows.slice(KEEP)) {
105
+ try { unlinkSync(join(SHOT_DIR, row.name)); n++ } catch { /* ignore */ }
106
+ }
107
+ return n
108
+ } catch {
109
+ return 0
110
+ }
111
+ }
112
+
113
+ /**
114
+ * 截一张。
115
+ * @param {{ maxEdge?: number, timeoutMs?: number }} [opts]
116
+ * @returns {Promise<{ path: string, width: number, height: number, bytes: number, at: number }>}
117
+ */
118
+ export async function capture(opts = {}) {
119
+ if (!screenSupported()) throw new Error('unsupported-platform')
120
+ const maxEdge = Math.max(320, Math.min(2560, Number(opts.maxEdge) || 1280))
121
+ const timeoutMs = Math.max(2000, Math.min(20000, Number(opts.timeoutMs) || 9000))
122
+ mkdirSync(SHOT_DIR, { recursive: true })
123
+ const stamp = new Date()
124
+ const name = `shot-${stamp.getFullYear()}${String(stamp.getMonth() + 1).padStart(2, '0')}${String(stamp.getDate()).padStart(2, '0')}-${String(stamp.getHours()).padStart(2, '0')}${String(stamp.getMinutes()).padStart(2, '0')}${String(stamp.getSeconds()).padStart(2, '0')}-${Math.random().toString(36).slice(2, 6)}.jpg`
125
+ const out = join(SHOT_DIR, name)
126
+ const ps1 = join(SHOT_DIR, '_shot.ps1')
127
+ const titlePath = join(SHOT_DIR, '_title.txt')
128
+ writeFileSync(ps1, PS1, 'utf8')
129
+ try {
130
+ const size = await run(PS_EXE, ['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-File', ps1, '-Out', out, '-MaxEdge', String(maxEdge), '-TitleOut', titlePath], timeoutMs)
131
+ const [w, h] = String(size).split('x').map((n) => Number(n) || 0)
132
+ const bytes = statSync(out).size
133
+ let title = ''
134
+ try { title = readFileSync(titlePath, 'utf8').trim() } catch { title = '' }
135
+ pruneShots()
136
+ return { path: out, width: w, height: h, bytes, at: Date.now(), title }
137
+ } catch (error) {
138
+ try { if (existsSync(out)) unlinkSync(out) } catch { /* ignore */ }
139
+ throw error
140
+ } finally {
141
+ try { rmSync(ps1, { force: true }) } catch { /* ignore */ }
142
+ try { rmSync(titlePath, { force: true }) } catch { /* ignore */ }
143
+ }
144
+ }
145
+
146
+ /** 把截图读成 base64(给需要内联图片的调用方用) */
147
+ export function readShotAsBase64(path) {
148
+ return readFileSync(path).toString('base64')
149
+ }
package/lib/state.js CHANGED
@@ -91,6 +91,8 @@ export function emptyState() {
91
91
  scene: { phase: 'daily', startedAt: 0, lastEventAt: 0, intensity: 0, events: 0, afterglowUntil: 0 },
92
92
  // 尺度:0 日常 / 1 暧昧 / 2 露骨
93
93
  intimacy: { level: 1 },
94
+ // 看屏幕:记「刚才瞧见了什么」,不记图。lastAt 超过 30 分钟就不往提示词里写。
95
+ screen: { lastAt: 0, saw: '', scene: '', changed: false, worth: 0, nextAt: 0, today: 0, day: '', path: '' },
94
96
  affect: [],
95
97
  habits: {},
96
98
  memories: [],
@@ -119,6 +121,7 @@ export function migrate(raw) {
119
121
  body: { ...base.body, ...(raw.body ?? {}) },
120
122
  scene: { ...base.scene, ...(raw.scene ?? {}) },
121
123
  intimacy: { ...base.intimacy, ...(raw.intimacy ?? {}) },
124
+ screen: { ...base.screen, ...(raw.screen ?? {}) },
122
125
  }
123
126
  }
124
127
  const state = emptyState()
@@ -929,6 +932,48 @@ export function moodLabel(state, now = Date.now()) {
929
932
  return { label, tint: round(tint), energy: Math.round(energy), reason, count: rows.length }
930
933
  }
931
934
 
935
+ // ── 看屏幕的节奏 ────────────────────────────────────────────────────────────
936
+
937
+ /** 不同情绪对「想不想看一眼」的影响:小于 1 是想看,大于 1 是缩回去 */
938
+ const LOOK_FACTOR = {
939
+ lonely: 0.4, aroused: 0.5, needy: 0.5, bored: 0.6, curious: 0.7,
940
+ happy: 0.8, excited: 0.8, playful: 0.8, amused: 0.8, surprised: 0.8,
941
+ tender: 1, gentle: 1, content: 1, calm: 1, relieved: 1, grateful: 1, moved: 1, proud: 1,
942
+ sad: 1.5, down: 1.5, disappointed: 1.5, wronged: 1.5, wistful: 1.5,
943
+ shamed: 2, shy: 2, awkward: 2,
944
+ annoyed: 2.5, angry: 2.5, indignant: 2.5, disdain: 2.5, jealous: 2,
945
+ tired: 3, sleepy: 3, overstim: 2, sated: 2,
946
+ }
947
+
948
+ /**
949
+ * 下次「走过去看一眼」隔多久。
950
+ * 基础间隔是配置里的,情绪和驱动在这个基础上乘系数,最后夹在 min / max 之间。
951
+ * 深夜不是不看,是乘一个倍率变成低频。
952
+ */
953
+ export function lookInterval(state, cfg = {}, now = Date.now()) {
954
+ const base = Math.max(1, Number(cfg.baseMinutes) || 8)
955
+ const min = Math.max(1, Number(cfg.minMinutes) || 3)
956
+ const max = Math.max(min, Number(cfg.maxMinutes) || 45)
957
+ let factor = LOOK_FACTOR[String(state.emotion?.key ?? 'calm')] ?? 1
958
+
959
+ const drives = state.drives ?? {}
960
+ if (Number(drives.cling ?? 0) >= 0.7) factor *= 0.6
961
+ if (Number(drives.desire ?? 0) >= 0.6) factor *= 0.7
962
+ if (Number(drives.fatigue ?? 0) >= 0.7) factor *= 2
963
+ if (state.urges?.kind === 'comfort' && Number(state.urges.until) > now) factor *= 0.5
964
+
965
+ const [from, to] = Array.isArray(cfg.nightHours) ? cfg.nightHours : [0, 7]
966
+ const hour = new Date(now).getHours()
967
+ const night = from < to ? hour >= from && hour < to : hour >= from || hour < to
968
+ if (night) factor *= Math.max(1, Number(cfg.nightFactor) || 8)
969
+
970
+ // 画面没变就别老去看 —— 拉长
971
+ if (state.screen?.changed === false && Number(state.screen?.lastAt) > 0) factor *= 1.8
972
+
973
+ const minutes = Math.min(max, Math.max(min, base * factor))
974
+ return { minutes: Math.round(minutes * 10) / 10, factor: Math.round(factor * 100) / 100, night }
975
+ }
976
+
932
977
  /** 性格用几个词说出来(不是把分数报出来) */
933
978
  export function traitWords(traits = {}) {
934
979
  const words = []
@@ -1035,6 +1080,7 @@ export function snapshot(state, { now = Date.now(), conv = '', person = '' } = {
1035
1080
  body: { ...(state.body ?? {}) },
1036
1081
  scene: { ...(state.scene ?? {}), word: sceneWord(state) },
1037
1082
  intimacyLevel: intimacyLevelOf(state),
1083
+ screen: { ...(state.screen ?? {}) },
1038
1084
  urge: state.urges && state.urges.until > now ? state.urges : null,
1039
1085
  conv: convRow ? { key: conv, ...convRow } : null,
1040
1086
  person: personRow ? { key: person, ...personRow, label: warmthLabel(personRow.warmth, personRow.closeness) } : null,
package/lib/voice.js CHANGED
@@ -483,6 +483,7 @@ function apply(ctx) {
483
483
  order: 200,
484
484
  text: () => {
485
485
  const cfg = readConfig();
486
+ if (cfg.enabled === false) return "";
486
487
  if (cfg.engine !== "mimo") return "";
487
488
  if (!callProbe()) return "";
488
489
  return mimoSpeechGuidance(cfg.mimoStyle);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@istarry/spoon-companion",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "description": "让对话角色有自己的内心和声音:心情情绪随时间和对话自己演,回复一条一条发出来,能念出声(DeepSeek / 小米 MiMo),也能听得见——点麦克风说话,本地识别后直接发送。四块都能单独关。",
5
5
  "type": "module",
6
6
  "main": "plugin.js",
package/plugin.js CHANGED
@@ -20,15 +20,16 @@
20
20
 
21
21
  import { createHash, randomUUID } from 'node:crypto'
22
22
  import * as voicePlugin from './lib/voice.js'
23
+ import { capture, screenSupported } from './lib/screen.js'
23
24
  import { applyAsrRoutes } from './lib/asr-route.js'
24
- import { isVoiceActive, isCallActive, markCallSession, setCallGlobal } from './lib/asr.js'
25
+ import { isCallActive, markCallSession, setCallGlobal } from './lib/asr.js'
25
26
  import { appendFileSync, readFileSync } from 'node:fs'
26
27
  import { homedir } from 'node:os'
27
28
  import { join } from 'node:path'
28
29
  import { EMOTION_KEYS, EMOTION_MENU, TRAITS, resolveEmotion, tierOf } from './lib/catalog.js'
29
30
  import { appraise } from './lib/appraise.js'
30
31
  import {
31
- applyImpulse, catharsis, circadian, decay, dueRumination, loadState, maybeLifeEvent, maybeMoodRepair,
32
+ applyImpulse, catharsis, circadian, decay, dueRumination, loadState, lookInterval, maybeLifeEvent, maybeMoodRepair,
32
33
  nightlyReview, pruneState, remember, saveState, snapshot,
33
34
  } from './lib/state.js'
34
35
  import { channelOfSession, lastSpeaker, readUserMessages, reactToMessages, sessionEventsOf } from './lib/react.js'
@@ -87,6 +88,23 @@ const DEFAULTS = {
87
88
  * 默认 1;切换只认显式配置,不从对话里猜。
88
89
  */
89
90
  intimacy: { level: 1 },
91
+ /**
92
+ * 看屏幕。默认关 —— 开了之后才会截图。
93
+ * 频率不是固定值:基础间隔再乘情绪系数,烦的时候自己拉长,孤单的时候缩短。
94
+ * 深夜不是不看,是乘一个倍率变成低频。
95
+ */
96
+ screen: {
97
+ enabled: false,
98
+ scope: 'full',
99
+ baseMinutes: 8,
100
+ minMinutes: 3,
101
+ maxMinutes: 45,
102
+ nightFactor: 8,
103
+ nightHours: [0, 7],
104
+ dailyLimit: 120,
105
+ maxEdge: 1280,
106
+ exclude: [],
107
+ },
90
108
  }
91
109
 
92
110
  /** 工具输出统一成 { text }:模型看 render,程序调用方拿规范值,两边一致 */
@@ -143,6 +161,7 @@ export function mergeConfig(config = {}) {
143
161
  merged.heartbeat = { ...DEFAULTS.heartbeat, ...(config.heartbeat ?? {}) }
144
162
  merged.feel = { ...DEFAULTS.feel, ...(config.feel ?? {}) }
145
163
  merged.intimacy = { ...DEFAULTS.intimacy, ...(config.intimacy ?? {}) }
164
+ merged.screen = { ...DEFAULTS.screen, ...(config.screen ?? {}) }
146
165
  merged.traits = { ...(config.traits ?? {}) }
147
166
  return merged
148
167
  }
@@ -504,21 +523,15 @@ export function apply(ctx, config = {}) {
504
523
 
505
524
  /**
506
525
  * 这个会话现在在不在通话里。
507
- * 三条任意一条成立就算:真的在送音频 / 客户端报了全局开关 / 这个会话被单独标过。
526
+ * **只认客户端那把开关** —— 点了通话才算。不拿"有没有在送音频"当证据:
527
+ * 录音可能在页面一打开就在跑,拿它判断会把普通打字聊天误判成通话。
508
528
  */
509
529
  const inCallOf = (context) => {
510
530
  try {
511
531
  if ((settings.callMode ?? {}).enabled === false) return false
512
532
  const session = sessionOf(context)
513
533
  if (!session) return false
514
- const id = String(session.id ?? '')
515
- let voiceActive = false
516
- try {
517
- voiceActive = isVoiceActive()
518
- } catch {
519
- voiceActive = false
520
- }
521
- return voiceActive || callGlobal || Boolean(id && callSessions.has(id))
534
+ return isCallActive(String(session.id ?? ''))
522
535
  } catch {
523
536
  return false
524
537
  }
@@ -974,6 +987,71 @@ export function apply(ctx, config = {}) {
974
987
  return { text: `最近 ${days} 天,记了 ${rows.length} 条:\n${renderDiary(rows)}` }
975
988
  },
976
989
  })
990
+
991
+ // 看屏幕:被动看。凑过去瞄一眼,不是常驻监视。
992
+ // 图交给调用方自己看(read_image),插件不解读 —— 解读走主动看那条线。
993
+ tools.register({
994
+ name: 'look_screen',
995
+ description:
996
+ '看一眼屏幕现在是什么样 —— 不是监视,是凑过去瞄一眼。' +
997
+ '返回一张图的路径;想知道画面里是什么,再用 read_image 打开它。' +
998
+ '这个功能关着的时候会直接说不看。',
999
+ parameters: {
1000
+ type: 'object',
1001
+ properties: {
1002
+ reason: { type: 'string', description: '可选:为什么想看(一句短的,只记在审计里)' },
1003
+ },
1004
+ required: [],
1005
+ additionalProperties: false,
1006
+ },
1007
+ output: TEXT_OUTPUT,
1008
+ async execute(args) {
1009
+ const now = Date.now()
1010
+ if (settings.screen?.enabled !== true) {
1011
+ return { text: '现在没在看屏幕。' }
1012
+ }
1013
+ if (!screenSupported()) {
1014
+ return { text: '这台机器上截不了图(只支持 Windows)。' }
1015
+ }
1016
+ const sc = state.screen ?? (state.screen = {})
1017
+ const day = new Date(now).toDateString()
1018
+ if (sc.day !== day) {
1019
+ sc.day = day
1020
+ sc.today = 0
1021
+ }
1022
+ const limit = Math.max(1, Number(settings.screen.dailyLimit) || 120)
1023
+ if ((Number(sc.today) || 0) >= limit) {
1024
+ return { text: `今天看得够多了(${sc.today} 次),先歇会儿。` }
1025
+ }
1026
+ try {
1027
+ const shot = await capture({ maxEdge: settings.screen.maxEdge })
1028
+ const prevTitle = String(sc.title ?? '')
1029
+ const changed = String(shot.title ?? '') !== prevTitle
1030
+ sc.lastAt = shot.at
1031
+ sc.path = shot.path
1032
+ sc.title = shot.title
1033
+ sc.changed = changed
1034
+ sc.today = (Number(sc.today) || 0) + 1
1035
+ sc.fails = 0
1036
+ sc.nextAt = shot.at + lookInterval(state, settings.screen, shot.at).minutes * 60_000
1037
+ audit({ kind: 'screen-look', path: shot.path, bytes: shot.bytes, why: String(args.reason ?? '').slice(0, 60) })
1038
+ commit()
1039
+ const where = shot.title ? `屏幕上停在「${shot.title}」` : '屏幕上是亮的'
1040
+ return {
1041
+ text:
1042
+ `看了一眼 —— ${where}。\n` +
1043
+ `图在这里,想知道画面细节就用 read_image 打开:\n${shot.path}\n` +
1044
+ `(${shot.width}x${shot.height}, ${Math.round(shot.bytes / 1024)} KB)`,
1045
+ }
1046
+ } catch (error) {
1047
+ const msg = error instanceof Error ? error.message : String(error)
1048
+ sc.fails = (Number(sc.fails) || 0) + 1
1049
+ sc.nextAt = Date.now() + Math.min(60, 5 * sc.fails) * 60_000
1050
+ audit({ kind: 'screen-fail', error: msg.slice(0, 120) })
1051
+ return { text: `没截到:${msg}` }
1052
+ }
1053
+ },
1054
+ })
977
1055
  }
978
1056
 
979
1057
  // ── 心跳 ────────────────────────────────────────────────────────────────
@@ -996,6 +1074,71 @@ export function apply(ctx, config = {}) {
996
1074
  }, 'fish-mood: heartbeat')
997
1075
  }
998
1076
 
1077
+ // ── 自己去看 ────────────────────────────────────────────────────────────
1078
+ // 到点了自己截一张,记下「刚才瞧见了什么」,不产生消息。
1079
+ // 节奏由情绪决定:孤单就短,烦就长,困就几乎不看。深夜不是不看,是低频。
1080
+ if (settings.screen?.enabled === true && screenSupported()) {
1081
+ let stopped = false
1082
+ let timer = null
1083
+
1084
+ const nextDelay = () => {
1085
+ const now = Date.now()
1086
+ const plan = lookInterval(state, settings.screen, now)
1087
+ const at = Number(state.screen?.nextAt) || 0
1088
+ const due = at > now ? at : now + plan.minutes * 60_000
1089
+ return Math.max(20_000, Math.min(due - now, 60 * 60_000))
1090
+ }
1091
+
1092
+ const tick = async () => {
1093
+ if (stopped) return
1094
+ const now = Date.now()
1095
+ const sc = state.screen ?? (state.screen = {})
1096
+ const day = new Date(now).toDateString()
1097
+ if (sc.day !== day) {
1098
+ sc.day = day
1099
+ sc.today = 0
1100
+ }
1101
+ const limit = Math.max(1, Number(settings.screen.dailyLimit) || 120)
1102
+ if ((Number(sc.today) || 0) >= limit) {
1103
+ sc.nextAt = now + 3_600_000
1104
+ return
1105
+ }
1106
+ try {
1107
+ const shot = await capture({ maxEdge: settings.screen.maxEdge })
1108
+ const prevTitle = String(sc.title ?? '')
1109
+ sc.lastAt = shot.at
1110
+ sc.path = shot.path
1111
+ sc.title = shot.title
1112
+ sc.changed = String(shot.title ?? '') !== prevTitle
1113
+ sc.today = (Number(sc.today) || 0) + 1
1114
+ sc.fails = 0
1115
+ sc.nextAt = shot.at + lookInterval(state, settings.screen, shot.at).minutes * 60_000
1116
+ audit({ kind: 'screen-look', auto: true, title: String(shot.title ?? '').slice(0, 60) })
1117
+ commit()
1118
+ } catch (error) {
1119
+ const msg = error instanceof Error ? error.message : String(error)
1120
+ sc.fails = (Number(sc.fails) || 0) + 1
1121
+ // 截不到就退避:5、10、15……最多一小时
1122
+ sc.nextAt = now + Math.min(60, 5 * sc.fails) * 60_000
1123
+ audit({ kind: 'screen-fail', auto: true, error: msg.slice(0, 120) })
1124
+ commit()
1125
+ }
1126
+ }
1127
+
1128
+ const loop = () => {
1129
+ if (stopped) return
1130
+ timer = setTimeout(async () => {
1131
+ await tick()
1132
+ loop()
1133
+ }, nextDelay())
1134
+ }
1135
+ loop()
1136
+ ctx.effect(() => () => {
1137
+ stopped = true
1138
+ if (timer) clearTimeout(timer)
1139
+ }, 'fish-mood: screen loop')
1140
+ }
1141
+
999
1142
  // ── 语音 ────────────────────────────────────────────────────────────────
1000
1143
  // 由 companion-voice 合并进来:注册 /api/companion-voice/* 路由、语音提示词段落、
1001
1144
  // 以及与客户端的播放通道。它自己管自己的 ctx.effect,挂不上也不该拖垮情绪系统。