cortico-world-feast 0.7.2 → 0.7.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/src/definition.js CHANGED
@@ -98,6 +98,12 @@ export const feastDefinition = {
98
98
  cfg: ctx.cfg,
99
99
  dataDir: ctx.dataDir,
100
100
  timezone: ctx.timezone,
101
+ // 补齐配置段用:宿主的 /api/config 读的是落盘的 worlds.feast(不做默认值合并),
102
+ // 段里缺键时界面上会显示成 false / 空 / 0,和实际生效值对不上。
103
+ // 详见 FeastWorld#ensureConfigSection。
104
+ persist: typeof ctx.persist === 'function' ? (patch) => ctx.persist(patch) : null,
105
+ // 天气联网可注入(dry-mount / 测试用);宿主正常挂载时不传,走全局 fetch。
106
+ weatherFetch: typeof ctx.weatherFetch === 'function' ? ctx.weatherFetch : null,
101
107
  }),
102
108
  /** 自描述:可能出现的全部工具与事件,不进提示词。 */
103
109
  describe: {
package/src/life/free.js CHANGED
@@ -255,7 +255,7 @@ export function notFreeReason(item) {
255
255
  */
256
256
  export function freeEventText({
257
257
  slot, retry = false, play, cfg = {}, where = '', menu = '', nowHm = '', tz = 'Asia/Shanghai',
258
- freeNow = true,
258
+ freeNow = true, weatherShort = '', weatherHint = '',
259
259
  } = {}) {
260
260
  const goal = clampInt(cfg?.lifeFreeGoal, 0, 12, 3);
261
261
  const p = playProgress(play, goal);
@@ -283,9 +283,12 @@ export function freeEventText({
283
283
  }
284
284
  }
285
285
  lines.push(`今天已经玩过:${freeDoneText(play, tz)}。`);
286
+ // 天气:出门那句提醒跟着真实天气走,别在下雨天说「日头足」。取不到就整段不出现。
287
+ if (weatherShort && !retry) lines.push(`外面这会儿:${weatherShort}。`);
286
288
  if (menu && !retry) {
287
289
  lines.push('想做的话,下面这些只是个例子,不照着也行;想到什么就做什么,什么都不做也可以:');
288
290
  lines.push(menu);
291
+ if (weatherHint) lines.push(weatherHint);
289
292
  }
290
293
  const tail = [];
291
294
  tail.push('做完随手用 feast_life_play 记一笔(activity 写做了什么,note 一句细节)。不记也没关系,记只是给你自己攒。');
package/src/weather.js ADDED
@@ -0,0 +1,219 @@
1
+ /**
2
+ * 食时 · 天气(0.7.3)。
3
+ *
4
+ * 解决的是「真机大太阳、她却说下雨」这种割裂:让鱼知道**此刻外面真实是什么天气**。
5
+ *
6
+ * 设计红线(别改回去):
7
+ * · **城市是每个安装者各自填的**,不是代码常量、不自动探测 IP、不内置城市表。
8
+ * 这是个发给所有人的 npm 包,写死任何一个城市对别人都是错的。
9
+ * · **留空 / 没填城市 / 联网失败 / 解析失败 → 返回 null,不提天气**。
10
+ * 宁可不说天气,也绝不按季节编一个 —— 编出来的天气只是换个方式继续割裂。
11
+ * · 不爬天气网页的 HTML(改版就废),只用 open-meteo 的结构化 JSON:
12
+ * geocoding-api.open-meteo.com (城市名 → 经纬度,中文可直接查)
13
+ * api.open-meteo.com (经纬度 → 当前天气,免 Key、零注册)
14
+ * · 网络请求全在 fetchWeather() 里,且**绝不抛错**;World 只在 tick 里 await 它,
15
+ * envPromptVars() 是同步的,只读内存快照,不做网络。
16
+ *
17
+ * 本文件只依赖全局 fetch(Node >= 22.6,package.json engines 已要求)。
18
+ */
19
+
20
+ const GEO_URL = 'https://geocoding-api.open-meteo.com/v1/search';
21
+ const FCST_URL = 'https://api.open-meteo.com/v1/forecast';
22
+ /** 超时:挂在 tick 里,不能让一次联网卡住整个 World 的看表。 */
23
+ const TIMEOUT_MS = 6000;
24
+
25
+ /** WMO weather_code → 中文天气描述。open-meteo 的 current.weather_code 用这套码。 */
26
+ const WMO = {
27
+ 0: '晴',
28
+ 1: '大部晴朗', 2: '多云', 3: '阴',
29
+ 45: '有雾', 48: '雾凇',
30
+ 51: '小毛毛雨', 53: '毛毛雨', 55: '大毛毛雨',
31
+ 56: '冻毛毛雨', 57: '强冻毛毛雨',
32
+ 61: '小雨', 63: '中雨', 65: '大雨',
33
+ 66: '冻雨', 67: '强冻雨',
34
+ 71: '小雪', 73: '中雪', 75: '大雪', 77: '雪粒',
35
+ 80: '小阵雨', 81: '阵雨', 82: '强阵雨',
36
+ 85: '小阵雪', 86: '大阵雪',
37
+ 95: '雷阵雨', 96: '雷阵雨伴冰雹', 99: '强雷阵雨伴冰雹',
38
+ };
39
+
40
+ /** 天气大类,用来选联动方向;unknown 不联动。 */
41
+ const KIND = {
42
+ clear: 'clear', hot: 'hot', rain: 'rain', cold: 'cold', mild: 'mild',
43
+ };
44
+
45
+ /**
46
+ * 把 WMO 码 + 温度 + 昼夜归成一类。纯函数,方便测试。
47
+ * @returns {'clear'|'hot'|'rain'|'cold'|'mild'|'unknown'}
48
+ */
49
+ export function classifyWeather({ code, tempC, isDay = 1 } = {}) {
50
+ if (code === undefined || code === null || !Number.isFinite(Number(code))) return 'unknown';
51
+ const c = Number(code);
52
+ const t = Number.isFinite(Number(tempC)) ? Number(tempC) : null;
53
+ const raining = (c >= 51 && c <= 67) || (c >= 80 && c <= 82) || (c >= 95 && c <= 99);
54
+ const snowing = (c >= 71 && c <= 77) || (c >= 85 && c <= 86);
55
+ if (raining || snowing) return 'rain';
56
+ if (t !== null && t <= 8) return 'cold';
57
+ if (t !== null && t >= 30) return 'hot';
58
+ if (c === 0 || c === 1) return 'clear';
59
+ return 'mild';
60
+ }
61
+
62
+ /**
63
+ * 城市名是从 geocoding 回来的外部文本,最终要进系统前缀。
64
+ * 宿主对前缀做 {{...}} 模板替换(见 life/inject.js 的 safe()),
65
+ * 所以这里把花括号拆开,免得一个手滑填了「{{x}}」的城市被当成模板变量。
66
+ */
67
+ function safeCity(s) {
68
+ return String(s ?? '').replace(/\{\{/g, '{').replace(/\}\}/g, '}').slice(0, 40);
69
+ }
70
+
71
+ /** WMO 码 → 中文词。未知码返回空串(调用方据此不提天气)。纯函数。 */
72
+ export function weatherWord(code) {
73
+ if (code === undefined || code === null) return '';
74
+ return WMO[Number(code)] ?? '';
75
+ }
76
+
77
+ /**
78
+ * 给模型看的一行天气。纯函数、同步、绝不抛错。
79
+ * 返回 '' 表示「不提天气」(没数据 / 没填城市 / 联网失败都走这条)。
80
+ *
81
+ * @param {object|null} snap fetchWeather() 的快照,或内存里的旧快照
82
+ */
83
+ export function weatherLine(snap, { who = '你' } = {}) {
84
+ if (!snap || !snap.ok) return '';
85
+ const word = weatherWord(snap.code);
86
+ if (!word) return '';
87
+ const bits = [];
88
+ if (snap.city) bits.push(`${safeCity(snap.city)}这边`);
89
+ bits.push(word);
90
+ if (Number.isFinite(snap.tempC)) bits.push(`${Math.round(snap.tempC)}°C`);
91
+ if (snap.isDay === 0) bits.push('这会儿是夜里');
92
+ // 「你」是模型自己:别写成任务/天气报告,写成她抬眼能看到的背景。
93
+ return `外面${bits.join(',')}。${who}聊到的时候,照眼睛看到的说就好,别报成天气预报。`;
94
+ }
95
+
96
+ /**
97
+ * 天气联动方向 → 给 pickFew 的 pool 筛选谓词。纯函数。
98
+ * 用现有的 category / keywords / taste 粗筛,**不改菜品契约**(DISH-SPEC 不许加字段)。
99
+ *
100
+ * · hot / clear(大太阳、热)→ 偏冰饮、甜品、凉菜、清爽的
101
+ * · rain / cold(下雨、冷)→ 偏汤羹、火锅、热菜、热汤面
102
+ * · mild / unknown → 不过滤(返回 null,调用方不传 pool)
103
+ *
104
+ * 返回的是一个**宽松**谓词:宁可少联动,也不要把 800 道筛到只剩两三道。
105
+ * @returns {((d:object)=>boolean)|null}
106
+ */
107
+ export function weatherDishFilter(kind) {
108
+ if (kind === 'hot' || kind === 'clear') {
109
+ return (d) => {
110
+ const cat = d.category;
111
+ const kw = (d.keywords ?? []).join('');
112
+ const name = d.name ?? '';
113
+ return cat === '饮品' || cat === '甜品' || cat === '凉菜'
114
+ || /冰|冻|凉|冷|爽|清|椰|果|茶|露|雪|刨|布丁|冻|酸梅|绿豆/.test(kw + name);
115
+ };
116
+ }
117
+ if (kind === 'rain' || kind === 'cold') {
118
+ return (d) => {
119
+ const cat = d.category;
120
+ const kw = (d.keywords ?? []).join('');
121
+ const name = d.name ?? '';
122
+ return cat === '汤羹' || cat === '火锅' || cat === '热菜'
123
+ || /汤|炖|煲|烩|热|暖|锅|羹|面|粥|辣|姜|羊肉|涮/.test(kw + name);
124
+ };
125
+ }
126
+ return null;
127
+ }
128
+
129
+ /** 天气联动时,饭点候选那行提示语。纯函数。 */
130
+ export function weatherPickHint(kind) {
131
+ if (kind === 'hot' || kind === 'clear') return '(今天外面热,随手挑的都是清爽口的;想换别的照样能用 feast_menu。)';
132
+ if (kind === 'rain' || kind === 'cold') return '(今天外面凉/下雨,随手挑的都是热乎的;想换别的照样能用 feast_menu。)';
133
+ return '';
134
+ }
135
+
136
+ /** 天气联动时,自由时间例子后面搭的一句。纯函数。 */
137
+ export function weatherFreeHint(kind) {
138
+ if (kind === 'hot' || kind === 'clear') return '外面日头足,想出门就挑凉快的时候、带瓶水。';
139
+ if (kind === 'rain' || kind === 'cold') return '外面凉/下雨,出门记得加件衣裳、带把伞。';
140
+ return '';
141
+ }
142
+
143
+ /** 和 weatherLine 同义但更短,给 feast_life_now 回执用。 */
144
+ export function weatherShort(snap) {
145
+ if (!snap || !snap.ok) return '';
146
+ const word = weatherWord(snap.code);
147
+ if (!word) return '';
148
+ const t = Number.isFinite(snap.tempC) ? ` ${Math.round(snap.tempC)}°C` : '';
149
+ const city = snap.city ? `${safeCity(snap.city)} ` : '';
150
+ return `${city}${word}${t}`;
151
+ }
152
+
153
+ /**
154
+ * 唯一的联网函数:城市名 → 经纬度 → 当前天气。
155
+ *
156
+ * **绝不抛错**:任何失败都返回 `{ ok: false }`。调用方据此「不提天气」。
157
+ * 支持注入 fetchImpl / timeoutMs 便于测试;生产用全局 fetch。
158
+ *
159
+ * @param {object} opts
160
+ * @param {string} opts.city 用户填的城市名(中文即可),空 = 不请求
161
+ * @param {string} [opts.timezone] 传给 open-meteo 的时区(只影响返回的时间串,不改数值)
162
+ * @param {function} [opts.fetchImpl]
163
+ * @param {number} [opts.timeoutMs]
164
+ * @returns {Promise<{ok:boolean, city?:string, code?:number, tempC?:number, isDay?:number, kind?:string}>}
165
+ */
166
+ export async function fetchWeather({
167
+ city = '', timezone = 'Asia/Shanghai', fetchImpl, timeoutMs = TIMEOUT_MS,
168
+ } = {}) {
169
+ const name = String(city ?? '').trim();
170
+ if (!name) return { ok: false }; // 默认关、留空即不提天气
171
+ const doFetch = typeof fetchImpl === 'function' ? fetchImpl : (typeof fetch === 'function' ? fetch : null);
172
+ if (!doFetch) return { ok: false };
173
+
174
+ const getJson = async (url) => {
175
+ const ctl = typeof AbortController === 'function' ? new AbortController() : null;
176
+ const timer = ctl ? setTimeout(() => ctl.abort(), timeoutMs) : null;
177
+ try {
178
+ const res = await doFetch(url, ctl ? { signal: ctl.signal } : undefined);
179
+ if (!res || !res.ok) return null;
180
+ return await res.json();
181
+ } catch {
182
+ return null;
183
+ } finally {
184
+ if (timer) clearTimeout(timer);
185
+ }
186
+ };
187
+
188
+ try {
189
+ const geoUrl = `${GEO_URL}?name=${encodeURIComponent(name)}&count=1&language=zh&format=json`;
190
+ const geo = await getJson(geoUrl);
191
+ const hit = Array.isArray(geo?.results) ? geo.results[0] : null;
192
+ const lat = Number(hit?.latitude);
193
+ const lon = Number(hit?.longitude);
194
+ if (!hit || !Number.isFinite(lat) || !Number.isFinite(lon)) return { ok: false };
195
+
196
+ const fcstUrl = `${FCST_URL}?latitude=${lat}&longitude=${lon}`
197
+ + `&current=temperature_2m,weather_code,is_day&timezone=${encodeURIComponent(timezone)}`;
198
+ const data = await getJson(fcstUrl);
199
+ const cur = data?.current;
200
+ if (!cur) return { ok: false };
201
+ const code = Number(cur.weather_code);
202
+ const tempC = Number(cur.temperature_2m);
203
+ const isDay = Number(cur.is_day);
204
+ if (!Number.isFinite(code)) return { ok: false };
205
+ return {
206
+ ok: true,
207
+ city: safeCity(hit.name || name),
208
+ code,
209
+ tempC: Number.isFinite(tempC) ? tempC : undefined,
210
+ isDay: Number.isFinite(isDay) ? isDay : 1,
211
+ kind: classifyWeather({ code, tempC, isDay }),
212
+ };
213
+ } catch {
214
+ return { ok: false };
215
+ }
216
+ }
217
+
218
+ /** 空快照常量:开关关着 / 还没刷到时用。和 wardrobe 的 emptyWardrobe 一个意思。 */
219
+ export const EMPTY_WEATHER = Object.freeze({ ok: false });
package/src/world.js CHANGED
@@ -3,9 +3,9 @@
3
3
  *
4
4
  * 做的是"让她真的吃到东西"这件事:
5
5
  *
6
- * · 库里 700 道地域菜 + 白米饭 1 道(28 个菜系组),每道带九维口味分值 + 香气/口感/余韵 + 实吃描写;
6
+ * · 库里 824 道菜(28 个菜系组,含特别餐食 25 道),每道带九维口味分值 + 香气/口感/余韵 + 实吃描写;
7
7
  * cuisine 只说地域/传统,形态一律看 category;
8
- * 唯一允许的非地域组是 `staples`(白饭),里面只有白米饭一碗,有守门测试钉死上限;
8
+ * 唯一允许的非地域组是 `staples`(特别餐食)= 白米饭 + 四家连锁各 6 道,上限 25,有守门测试钉死;
9
9
  * · feast_eat 不是把档案贴出来,而是叠一层"这一口"(温度、状态、配的那口),
10
10
  * 同一道菜连着吃三次说的不一样;
11
11
  * · 主人可以从 WebUI 面板点一道菜"端给她",那是一条约她来吃的 internal 事件;
@@ -40,6 +40,10 @@ import {
40
40
  normalizeCombo, resolveCombo, tableLine, tableTaste,
41
41
  } from './combos.js';
42
42
  import { renderBite, renderProfile } from './eat.js';
43
+ import {
44
+ EMPTY_WEATHER, fetchWeather, weatherDishFilter, weatherFreeHint, weatherLine,
45
+ weatherPickHint, weatherShort,
46
+ } from './weather.js';
43
47
  import {
44
48
  dayIn, eatenMarkOf, eatenSince, eatenTodayText, eatCount, hmIn, lastEaten, markNudged, newState,
45
49
  nextMealText, normalizeState, planTick, recentEaten, recordEat, recordReaction, rollover,
@@ -323,14 +327,27 @@ export class FeastWorld {
323
327
  #play = null;
324
328
  /** 生图素材库快照:envPromptVars() 是同步的,不能 await,读这里。tick 里刷新(见 #lifeEnsure)。 */
325
329
  #wardrobe = emptyWardrobe();
330
+ /** 真实天气快照:同样同步读、tick 里异步刷新。envPromptVars() 绝不能联网。
331
+ * 取不到就一直是 EMPTY_WEATHER —— 「宁可不说天气,也绝不编一个」。 */
332
+ #weather = EMPTY_WEATHER;
333
+ /** 上次真正去取天气的时刻(ms):按 weatherRefreshMin 节流,别把接口当心跳打。 */
334
+ #weatherAt = 0;
326
335
  /** 上次「重新想想」推给她的时间(按 date):连点不该连推,免得刷屏。 */
327
336
  #lifeRewriteAt = {};
337
+ /** 宿主的 persist:深合并进 worlds.feast 并落盘(dry-mount 时没有)。 */
338
+ #persist = null;
339
+ /** 天气联网实现注入(仅测试用;生产走 weather.js 的全局 fetch)。 */
340
+ #weatherFetch = null;
341
+ /** 构造时拿到的原始配置段(未合并 DEFAULTS):用来判断哪些键是「盘上没有的」。 */
342
+ #rawCfg = null;
328
343
 
329
344
  constructor(opts = {}) {
330
345
  this.id = ID;
331
346
  // 配置段可能是手写的、只补了半截:和默认值合并一层,缺的键按默认值走,
332
347
  // 不让一个漏写的 mealReminder 悄悄把饭点关掉。
333
- this.cfg = { ...DEFAULTS, ...(opts.cfg ?? {}) };
348
+ // 原始那份也留着(#rawCfg):启动补配置时要判断「盘上到底有没有这个键」。
349
+ this.#rawCfg = opts.cfg ?? {};
350
+ this.cfg = { ...DEFAULTS, ...this.#rawCfg };
334
351
  this.dataDir = opts.dataDir ?? process.cwd();
335
352
  this.language = 'zh';
336
353
  this.timezone = opts.timezone || 'Asia/Shanghai';
@@ -339,6 +356,54 @@ export class FeastWorld {
339
356
  this.#setInterval = opts.timers?.setInterval ?? ((fn, ms) => setInterval(fn, ms));
340
357
  this.#clearInterval = opts.timers?.clearInterval ?? ((t) => clearInterval(t));
341
358
  this.#setTimeout = opts.timers?.setTimeout ?? ((fn, ms) => setTimeout(fn, ms));
359
+ this.#persist = typeof opts.persist === 'function' ? opts.persist : null;
360
+ // 测试注入:让天气联网可替换(生产不传,用全局 fetch)。和 random/timers 同构。
361
+ this.#weatherFetch = typeof opts.weatherFetch === 'function' ? opts.weatherFetch : null;
362
+ }
363
+
364
+ /**
365
+ * 把 `worlds.feast` 段里缺的键补成默认值(启动时做一次)。
366
+ *
367
+ * 为什么必须补:宿主的 `/api/config` 拿落盘的段**直接**渲染
368
+ * (`readGroupValues` 不合并 defaults)——
369
+ * boolean 缺 → 界面显示 `false`
370
+ * string 缺 → 显示空
371
+ * integer 缺 → 显示 `0`
372
+ * 而 World 自己构造时是 `{ ...DEFAULTS, ...cfg }`,缺键按默认值走。
373
+ * 两边一凑,就成了「界面写着关闭、其实开着」。
374
+ *
375
+ * 真机实测:那台机器从 11-05 起 `worlds.feast` 只有 12→24 个键,
376
+ * 配置页 10 项显示错——`lifeEnabled` 显示 false 但她天天在过日子、
377
+ * `lifeWardrobeOn` 显示 false 但联动在跑。用户想关一个「看着已关」的
378
+ * 开关,第一步反而是去把它打开,行为完全反了。
379
+ *
380
+ * 只补**缺的**键,不覆盖用户已有的值;没有 persist(dry-mount、
381
+ * describe 生成)就跳过。绝不抛错:补不上也要能正常起。
382
+ */
383
+ #ensureConfigSection() {
384
+ if (!this.#persist) return;
385
+ try {
386
+ // 判据用构造时拿到的**原始段**(this.#rawCfg),不能用 this.cfg:
387
+ // this.cfg 已经和 DEFAULTS 合并过,每个键都在,永远判不出「缺」。
388
+ const raw = this.#rawCfg ?? {};
389
+ const patch = {};
390
+ for (const [key, value] of Object.entries(DEFAULTS)) {
391
+ // enabled 归宿主管(部署决定),不在这里写。
392
+ if (key === 'enabled') continue;
393
+ if (key in raw) continue;
394
+ patch[key] = value;
395
+ }
396
+ const keys = Object.keys(patch);
397
+ if (!keys.length) return;
398
+ this.#persist(patch);
399
+ // 活对象同步一次:persist 会深合并进同一份 cfg,这里只是保险。
400
+ Object.assign(this.cfg, patch);
401
+ Object.assign(raw, patch);
402
+ this.#host?.log?.info?.('食时:配置段补齐了缺失的键', { keys });
403
+ } catch (err) {
404
+ // 补不上就照默认值跑,功能不受影响,只是界面会继续显示错——记一条警告。
405
+ this.#host?.log?.warn?.('食时:配置段没补齐(界面可能显示和实际不一致)', { err: msg(err) });
406
+ }
342
407
  }
343
408
 
344
409
  // -------------------------------------------------------------------------
@@ -433,6 +498,59 @@ export class FeastWorld {
433
498
  }
434
499
  }
435
500
 
501
+ /** 天气总开关:默认关;没填城市也等于关(不联网、不提天气)。 */
502
+ #weatherOn() {
503
+ return this.cfg?.weatherEnabled === true
504
+ && typeof this.cfg?.weatherCity === 'string'
505
+ && this.cfg.weatherCity.trim().length > 0;
506
+ }
507
+
508
+ /**
509
+ * 刷新真实天气(tick 里调,按 weatherRefreshMin 节流)。
510
+ * fetchWeather 自己绝不抛错,这里也不额外抛:失败只是让快照停在「不提天气」。
511
+ * 成功才覆盖旧快照;一次都没成功过就一直不提天气,不拿季节糊弄。
512
+ */
513
+ async #refreshWeather(now = this.#now()) {
514
+ if (!this.#weatherOn()) { this.#weather = EMPTY_WEATHER; return; }
515
+ const everyMs = Math.max(5, Math.min(360, Number(this.cfg?.weatherRefreshMin) || 30)) * 60_000;
516
+ if (this.#weatherAt && now - this.#weatherAt < everyMs) return; // 没到点,沿用上次
517
+ this.#weatherAt = now;
518
+ try {
519
+ const snap = await fetchWeather({
520
+ city: this.cfg.weatherCity,
521
+ timezone: this.timezone,
522
+ ...(this.#weatherFetch ? { fetchImpl: this.#weatherFetch } : {}),
523
+ });
524
+ // 只在成功时覆盖;失败保留上一次的有效快照,免得网络一抖天气就没了。
525
+ if (snap && snap.ok) this.#weather = snap;
526
+ else if (!this.#weather?.ok) this.#weather = EMPTY_WEATHER;
527
+ } catch {
528
+ // fetchWeather 契约上不抛,这层只是保险:绝不让天气把 tick 带崩。
529
+ }
530
+ }
531
+
532
+ /** envPromptVars / 事件里都用它:当前天气那一行,取不到返回 ''。 */
533
+ #weatherLine() {
534
+ return weatherLine(this.#weather);
535
+ }
536
+
537
+ /**
538
+ * 天气联动的候选池:用现有 category/keywords 粗筛,只影响「随手给的几道」,
539
+ * 不影响点名要某道菜、也不影响筛选结果。取不到天气 / 筛出来太少 → 返回 null(退回全库)。
540
+ * @returns {Array|null}
541
+ */
542
+ #weatherPool() {
543
+ const kind = this.#weather?.ok ? this.#weather.kind : null;
544
+ const filter = weatherDishFilter(kind);
545
+ if (!filter) return null;
546
+ const narrowed = this.#lib.dishes.filter(filter);
547
+ return narrowed.length >= 3 ? narrowed : null;
548
+ }
549
+
550
+ #weatherKindForHint() {
551
+ return this.#weatherPool() ? this.#weather.kind : null;
552
+ }
553
+
436
554
  // -------------------------------------------------------------------------
437
555
  // 提示词
438
556
  // -------------------------------------------------------------------------
@@ -445,8 +563,10 @@ export class FeastWorld {
445
563
  * 这个方法绝不能抛错:契约里抛错会把整个 World 卸载掉。
446
564
  */
447
565
  envPromptVars() {
566
+ // 天气是独立的:哪怕「她的一天」整个关着,也该让鱼知道外面什么天气。
567
+ const weather = this.#weatherLine();
448
568
  try {
449
- if (!this.#lifeOn()) return { feast_life_block: '' };
569
+ if (!this.#lifeOn()) return { feast_life_block: '', feast_weather_block: weather };
450
570
  const now = this.#now();
451
571
  const z = zonedNow(this.timezone, now);
452
572
  const today = this.#lifeToday?.date === z.date ? this.#lifeToday : null;
@@ -454,10 +574,11 @@ export class FeastWorld {
454
574
  const wardrobe = this.#wardrobeOn() ? wardrobeHintLine(this.#wardrobe) : '';
455
575
  return {
456
576
  feast_life_block: envBlock({ today, tomorrow, cfg: this.cfg, tz: this.timezone, nowMs: now, wardrobe }),
577
+ feast_weather_block: weather,
457
578
  };
458
579
  } catch (err) {
459
580
  this.#host?.log?.warn?.('食时:她的一天环境提示词算不出来', { err: msg(err) });
460
- return { feast_life_block: '' };
581
+ return { feast_life_block: '', feast_weather_block: weather };
461
582
  }
462
583
  }
463
584
 
@@ -524,9 +645,13 @@ export class FeastWorld {
524
645
  lines.push(`按条件找到 ${pool.length} 道${pool.length > 12 ? ',先列 12 道' : ''}:`);
525
646
  for (const d of pool.slice(0, 12)) lines.push(`- ${briefLine(d)}`);
526
647
  } else {
527
- const picks = pickFew(lib, this.#menuSize(), { rng: this.#random });
648
+ // 不带筛选时也让天气带一带:外面热偏清爽、下雨偏热乎。池子太小就退回全库。
649
+ const wpool = this.#weatherPool();
650
+ const picks = pickFew(lib, this.#menuSize(), { rng: this.#random, ...(wpool ? { pool: wpool } : {}) });
528
651
  lines.push('今天这几道可以考虑(随机给的,不喜欢就换个筛选):');
529
652
  for (const d of picks) lines.push(`- ${briefLine(d)}`);
653
+ const whint = weatherPickHint(this.#weatherKindForHint());
654
+ if (whint) lines.push(whint);
530
655
  }
531
656
  lines.push('', index);
532
657
  lines.push('吃哪道就用 feast_eat,dish 填菜名或 id;想先闻闻味儿不动筷子就用 feast_taste。');
@@ -723,6 +848,13 @@ export class FeastWorld {
723
848
  const now = this.#now();
724
849
  // 主人端来的菜过期就撤掉,免得半天后突然回一句。
725
850
  servedStillFresh(this.#state, now, this.#cfgNumber('ownerFeedTTL', 1, 240));
851
+ // 天气在每拍最前面刷(内部按 weatherRefreshMin 节流,不会真的一拍一次请求)。
852
+ // 单独兜住,天气出问题不该连饭点也不叫了。
853
+ try {
854
+ await this.#refreshWeather(now);
855
+ } catch (err) {
856
+ this.#host?.log?.warn?.('食时:天气这一拍没刷成', { err: msg(err) });
857
+ }
726
858
  // 「她的一天」必须在饭点判定之前跑:非饭点那一拍会 return,不能把它跳过去。
727
859
  // 单独兜住异常,日程出问题不该连饭点也不叫了。
728
860
  try {
@@ -772,16 +904,25 @@ export class FeastWorld {
772
904
  : `【食时 · ${plan.slot}】到饭点了。不是主人找你,也不是待办,是你自己该吃了。`;
773
905
 
774
906
  const lines = [head, `今天已经吃过:${eatenTodayText(state, this.timezone, now)}。`];
907
+ // 天气就在这一口旁边:外面大太阳就别端出一碗「下雨天」的话来。
908
+ const wshort = weatherShort(this.#weather);
909
+ if (wshort) lines.push(`外面这会儿:${wshort}。`);
775
910
  // 融合点:饭点本来就在她的一天里,顺带说清她这会儿原本在做什么。
776
911
  const life = this.#lifeNowBrief(now);
777
912
  if (life) lines.push(life);
778
913
  lines.push('挑一道现在想吃的:');
779
914
 
915
+ // 天气联动:大太阳/热 → 偏清爽,下雨/冷 → 偏热乎。只用现成的 category/keywords
916
+ // 粗筛,不改菜品契约;筛不到东西或天气没取到就退回全库,不硬凑。
917
+ const pool = this.#weatherPool();
780
918
  const picks = pickFew(this.#lib, Math.min(3, this.#menuSize()), {
781
919
  rng: this.#random,
782
920
  avoidId: this.#avoidRepeat() ? lastEaten(state)?.id : undefined,
921
+ ...(pool ? { pool } : {}),
783
922
  });
784
923
  for (const d of picks) lines.push(`- ${briefLine(d)}`);
924
+ const whint = weatherPickHint(this.#weatherKindForHint());
925
+ if (whint) lines.push(whint);
785
926
  lines.push(`以上是随手给的几道;想换就用 feast_menu(能按 ${this.#lib.cuisines.slice(0, 6).join('/')} 这类菜系筛)。`);
786
927
 
787
928
  const tail = ['直接 feast_eat 吃一道(dish 填菜名或 id;不带参数也是随机)。'];
@@ -1886,6 +2027,9 @@ export class FeastWorld {
1886
2027
  nowHm: z.hm,
1887
2028
  tz: this.timezone,
1888
2029
  freeNow,
2030
+ // 天气:出门那句提醒跟着真实天气走,取不到就整段不出现。
2031
+ weatherShort: weatherShort(this.#weather),
2032
+ weatherHint: weatherFreeHint(this.#weather?.ok ? this.#weather.kind : null),
1889
2033
  }),
1890
2034
  meta: { slot: plan.slot, retry: plan.action === 'retry', done: play.done.length },
1891
2035
  }, { deliver: true, trigger: 'flush' });
@@ -1937,11 +2081,15 @@ export class FeastWorld {
1937
2081
  : String(DEFAULTS.lifeFreeMenu);
1938
2082
  const done = play.done.length;
1939
2083
  const mood = typeof args?.mood === 'string' && args.mood.trim() ? `(${args.mood.trim().slice(0, 60)})` : '';
2084
+ const wshort = weatherShort(this.#weather);
2085
+ const whint = weatherFreeHint(this.#weather?.ok ? this.#weather.kind : null);
1940
2086
  const lines = [
1941
2087
  `行,现在就是你自己的时间${mood}——不用等下一档,也不管今天叫过几次。`,
1942
2088
  `今天已经记了 ${done} 件。`,
2089
+ ...(wshort ? [`外面这会儿:${wshort}。`] : []),
1943
2090
  '下面只是个例子,不照着也行;想到什么就做什么,什么都不做也可以:',
1944
2091
  menu,
2092
+ ...(whint ? [whint] : []),
1945
2093
  '做完随手用 feast_life_play 记一笔。不记也没关系,记只是给你自己攒。',
1946
2094
  ];
1947
2095
  if (goal > 0 && done >= goal) {
@@ -2388,6 +2536,8 @@ export class FeastWorld {
2388
2536
  const menu = typeof this.cfg?.lifeFreeMenu === 'string' && this.cfg.lifeFreeMenu.trim()
2389
2537
  ? this.cfg.lifeFreeMenu.trim()
2390
2538
  : String(DEFAULTS.lifeFreeMenu);
2539
+ const wshort = weatherShort(this.#weather);
2540
+ const whint = weatherFreeHint(this.#weather?.ok ? this.#weather.kind : null);
2391
2541
  await this.#host?.pushEvent?.({
2392
2542
  type: EVENT_LIFE_FREE,
2393
2543
  source: this.id,
@@ -2397,8 +2547,10 @@ export class FeastWorld {
2397
2547
  text: [
2398
2548
  '【食时 · 你自己的时间】主人说,这会儿你想干点什么就去吧。',
2399
2549
  `今天已经记了 ${play.done.length} 件${goal > 0 ? `(到 ${goal} 件只是不再按点叫你,不是上限)` : ''}。`,
2550
+ ...(wshort ? [`外面这会儿:${wshort}。`] : []),
2400
2551
  '想做什么都行,下面只是个例子,不照着也行:',
2401
2552
  menu,
2553
+ ...(whint ? [whint] : []),
2402
2554
  '做完随手用 feast_life_play 记一笔。',
2403
2555
  ].join('\n'),
2404
2556
  meta: { reason: 'owner-start' },
@@ -2450,6 +2602,8 @@ export class FeastWorld {
2450
2602
  async start(host) {
2451
2603
  if (this.#started) return;
2452
2604
  this.#host = host;
2605
+ // 先补配置段:宿主的配置页读的是落盘的段,缺键会显示和实际不一致。
2606
+ this.#ensureConfigSection();
2453
2607
  this.#lib = await loadDishes(host?.log);
2454
2608
  this.#state = await this.#load();
2455
2609
  this.#started = true;
@@ -2462,6 +2616,13 @@ export class FeastWorld {
2462
2616
  } catch (err) {
2463
2617
  host?.log?.warn?.('食时:她的一天没读进来', { err: msg(err) });
2464
2618
  }
2619
+ // 启动就先取一次真实天气:envPromptVars() 是同步的,前缀重建时得有内存快照可读。
2620
+ // 取不到就保持「不提天气」,下一拍 tick 再试。
2621
+ try {
2622
+ await this.#refreshWeather(this.#now());
2623
+ } catch (err) {
2624
+ host?.log?.warn?.('食时:启动时天气没取到', { err: msg(err) });
2625
+ }
2465
2626
  host?.log?.info?.('食时已启动', {
2466
2627
  dishes: this.#lib.dishes.length,
2467
2628
  cuisines: this.#lib.cuisines.length,