dsh-multigameassistant 2.2.3 → 2.3.1

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/game-intel.js CHANGED
@@ -3,14 +3,16 @@ import {
3
3
  GAME_GUIDE_FILE,
4
4
  GAME_MANIFEST_FILE,
5
5
  GameManifest,
6
- parseGameManifest
7
- } from "./chunk-RGEGJH6R.js";
6
+ GameSource,
7
+ parseGameManifest,
8
+ routineLogs
9
+ } from "./chunk-C7X2KJ5C.js";
8
10
  import {
9
11
  finalText,
10
12
  sessionConversations
11
- } from "./chunk-A6YQRWHZ.js";
12
- import "./chunk-6NJSSOB6.js";
13
- import "./chunk-KMVQK5CI.js";
13
+ } from "./chunk-4QXRDWGS.js";
14
+ import "./chunk-QSETYM2Y.js";
15
+ import "./chunk-M7GNYL5B.js";
14
16
  import {
15
17
  ExeIcons,
16
18
  writeFileAtomic
@@ -26,7 +28,7 @@ import {
26
28
  SCREEN_SOURCE,
27
29
  SHOT_IMAGE,
28
30
  taskMessage
29
- } from "./chunk-LXHF6AKU.js";
31
+ } from "./chunk-34VNH3YS.js";
30
32
  import {
31
33
  external_exports
32
34
  } from "./chunk-6DSSLS7I.js";
@@ -37,8 +39,8 @@ import {
37
39
  // packages/game-intel/lib/index.js
38
40
  import { createHash as createHash2 } from "node:crypto";
39
41
  import { existsSync } from "node:fs";
40
- import { mkdir, readdir, readFile as readFile2, rm, stat, writeFile } from "node:fs/promises";
41
- import { isAbsolute, join as join2, resolve } from "node:path";
42
+ import { mkdir, readdir, readFile as readFile3, rm, stat, writeFile } from "node:fs/promises";
43
+ import { isAbsolute, join as join3, resolve } from "node:path";
42
44
  import { Service } from "@deepseek-ai/cordis";
43
45
 
44
46
  // packages/game-intel/lib/calendar.js
@@ -359,8 +361,18 @@ var Calendar = class {
359
361
  return this.#saved.readings[game];
360
362
  }
361
363
  /** Banners still open. */
364
+ /** Banners still open: the data sites', then those the agent read on the web that no site lists. */
362
365
  pools(game, now) {
363
- return (this.#saved.pools[game] ?? []).filter((p) => p.endsAt > now);
366
+ const site = (this.#saved.pools[game] ?? []).filter((p) => p.endsAt > now);
367
+ const read = (this.#saved.pools[`${game}#agent`] ?? []).filter((p) => p.endsAt > now && !site.some((s) => sameEvent([s.name], [p.name])));
368
+ return [...site, ...read].sort((a, b) => a.endsAt - b.endsAt);
369
+ }
370
+ /** Banners the agent read on the web (game_pool): one of the same name is replaced; ended ones go. Kept apart from the sites', which a refresh replaces. */
371
+ async recordPools(game, pools, now) {
372
+ const key = `${game}#agent`;
373
+ const kept = (this.#saved.pools[key] ?? []).filter((p) => p.endsAt > now && !pools.some((n) => sameEvent([n.name], [p.name])));
374
+ this.#saved.pools[key] = [...kept, ...pools.map((p) => ({ ...p, up: [...p.up] }))];
375
+ await this.save();
364
376
  }
365
377
  /** Every picture address a data site gave: the only ones the host will fetch. */
366
378
  pictures() {
@@ -450,6 +462,7 @@ var Calendar = class {
450
462
  delete this.#saved.refreshed[gameId];
451
463
  delete this.#saved.versions[gameId];
452
464
  delete this.#saved.pools[gameId];
465
+ delete this.#saved.pools[`${gameId}#agent`];
453
466
  delete this.#saved.links[gameId];
454
467
  delete this.#saved.notices[gameId];
455
468
  this.#saved.settings.hidden = this.#saved.settings.hidden.filter((id) => id !== gameId);
@@ -4579,8 +4592,15 @@ async function readSmw(smw, game, now, fetchImpl, signal) {
4579
4592
  }
4580
4593
  async function withCovers(api, pattern, events, fetchImpl, signal) {
4581
4594
  const fileOf = (e) => `File:${pattern.replace("{page}", e.aliases[0] ?? e.name)}`;
4595
+ const urls = await fileUrls(api, events.map(fileOf), fetchImpl, signal);
4596
+ return events.map((e) => {
4597
+ const cover = urls.get(fileOf(e));
4598
+ return cover === void 0 ? e : { ...e, cover };
4599
+ });
4600
+ }
4601
+ async function fileUrls(api, files, fetchImpl, signal) {
4582
4602
  const urls = /* @__PURE__ */ new Map();
4583
- const titles = [...new Set(events.map(fileOf))];
4603
+ const titles = [...new Set(files)];
4584
4604
  for (let i = 0; i < titles.length; i += 40) {
4585
4605
  const chunk = titles.slice(i, i + 40);
4586
4606
  try {
@@ -4598,11 +4618,107 @@ async function withCovers(api, pattern, events, fetchImpl, signal) {
4598
4618
  } catch {
4599
4619
  }
4600
4620
  }
4601
- return events.map((e) => {
4602
- const cover = urls.get(fileOf(e));
4603
- return cover === void 0 ? e : { ...e, cover };
4621
+ return urls;
4622
+ }
4623
+ function poolSince(src, now, timeZone) {
4624
+ return sinceText({ dates: src.dates === "text" ? "text" : "date" }, now, timeZone);
4625
+ }
4626
+ function poolValue(src, value, timeZone, now) {
4627
+ const at = readValue(value, timeZone, now);
4628
+ if (src.dates !== "local" || typeof at !== "number" || typeof value !== "object")
4629
+ return at;
4630
+ return at - zoneOffset(at, timeZone);
4631
+ }
4632
+ var FILE_PAGE = /^(?:文件|File|Image|图像):/i;
4633
+ function parsePools(results, src, timeZone, now) {
4634
+ if (typeof results !== "object" || results === null || Array.isArray(results))
4635
+ return [];
4636
+ const pools = [];
4637
+ const clean = (t) => t === void 0 || t.includes("{{{") ? void 0 : t;
4638
+ for (const [page, raw] of Object.entries(results)) {
4639
+ const out = raw.printouts ?? {};
4640
+ const first = (prop) => prop === void 0 ? void 0 : out[prop]?.[0];
4641
+ const endsAt = poolValue(src, first(src.end), timeZone, now);
4642
+ if (typeof endsAt !== "number" || endsAt < now)
4643
+ continue;
4644
+ const startsAt = poolValue(src, first(src.start), timeZone, now);
4645
+ const pageName = page.split("#").at(-1).replace(FILE_PAGE, "").replace(/.(?:png|jpe?g|gif|webp)$/i, "").trim();
4646
+ const name = clean(textValue(first(src.title))) ?? pageName;
4647
+ const type = clean(textValue(first(src.type)));
4648
+ const up = [...new Set(src.up.flatMap((prop) => (out[prop] ?? []).map(textValue)).map(clean).filter((t) => t !== void 0))];
4649
+ pools.push({ page: page.split("#")[0], name, endsAt, up, ...typeof startsAt === "number" ? { startsAt } : {}, ...type === void 0 ? {} : { type } });
4650
+ }
4651
+ return pools.sort((a, b) => a.endsAt - b.endsAt);
4652
+ }
4653
+ async function readPools(src, game, now, fetchImpl, signal) {
4654
+ const show = [src.title, src.type, src.start, src.end, ...src.up].filter((p) => p !== void 0).map((p) => `|?${p}`).join("");
4655
+ const query = `${src.conditions}[[${src.end}::>${poolSince(src, now, game.timezone)}]]${show}|limit=${src.limit}`;
4656
+ const timeout = AbortSignal.timeout(TIMEOUT_MS);
4657
+ let body;
4658
+ try {
4659
+ const response = await fetchImpl(`${src.api}?action=ask&format=json&query=${encodeURIComponent(query)}`, { signal: signal === void 0 ? timeout : AbortSignal.any([signal, timeout]), headers: { "user-agent": USER_AGENT, accept: "application/json" } });
4660
+ if (!response.ok)
4661
+ throw new Error(`HTTP ${response.status}`);
4662
+ body = await response.json();
4663
+ } catch (error) {
4664
+ const why = error instanceof Error ? error.name === "TimeoutError" ? `${TIMEOUT_MS / 1e3} \u79D2\u6CA1\u6709\u54CD\u5E94` : error.name === "SyntaxError" ? "\u8FD4\u56DE\u7684\u4E0D\u662F JSON\uFF08\u53EF\u80FD\u88AB\u7AD9\u70B9\u7684\u9632\u62A4\u62E6\u4E0B\u4E86\uFF09" : error.message : String(error);
4665
+ throw new Error(`${new URL(src.api).host} \u7684\u5361\u6C60\u8BFB\u4E0D\u5230\uFF1A${why}`);
4666
+ }
4667
+ if (body.error !== void 0)
4668
+ throw new Error(`${new URL(src.api).host} \u4E0D\u63A5\u53D7\u5361\u6C60\u67E5\u8BE2\uFF1A${body.error.info ?? "\u672A\u77E5\u9519\u8BEF"}`);
4669
+ const pools = parsePools(body.query?.results, src, game.timezone, now);
4670
+ const fileOf = (p) => FILE_PAGE.test(p.page) ? p.page : src.image === void 0 ? void 0 : `File:${src.image.replace("{page}", p.page)}`;
4671
+ const files = pools.map(fileOf).filter((f) => f !== void 0);
4672
+ const urls = files.length === 0 ? /* @__PURE__ */ new Map() : await fileUrls(src.api, files, fetchImpl, signal);
4673
+ return pools.map(({ page, ...p }) => {
4674
+ const f = fileOf({ page, name: p.name });
4675
+ const cover = f === void 0 ? void 0 : urls.get(f);
4676
+ return cover === void 0 ? p : { ...p, cover };
4604
4677
  });
4605
4678
  }
4679
+ function parseHoyo(body, now) {
4680
+ const banners = body?.banners;
4681
+ if (!Array.isArray(banners))
4682
+ throw new Error("\u683C\u5F0F\u4E0D\u5BF9\uFF08\u6CA1\u6709 banners\uFF09");
4683
+ const pools = [];
4684
+ for (const raw of banners) {
4685
+ const b = raw ?? {};
4686
+ const seconds = (v) => typeof v === "number" && v > 0 ? v * 1e3 : void 0;
4687
+ const endsAt = seconds(b["end_time"]);
4688
+ if (endsAt === void 0 || endsAt < now)
4689
+ continue;
4690
+ const startsAt = seconds(b["start_time"]);
4691
+ const items = ["characters", "weapons", "light_cones", "w_engines", "bangboos"].flatMap((k) => Array.isArray(b[k]) ? b[k] : []).filter((i) => typeof i["name"] === "string" && i["name"].trim() !== "").sort((x, y) => Number(y["rarity"] ?? 0) - Number(x["rarity"] ?? 0));
4692
+ if (items.length === 0)
4693
+ continue;
4694
+ const top = items[0];
4695
+ const icon = typeof top["icon"] === "string" && /^https:\/\/\S+$/.test(top["icon"]) ? top["icon"] : void 0;
4696
+ const weapon = !Array.isArray(b["characters"]) || b["characters"].length === 0;
4697
+ const named = typeof b["name"] === "string" && b["name"].trim() !== "" ? b["name"].trim() : void 0;
4698
+ pools.push({
4699
+ name: named ?? `${String(top["name"]).trim()}${weapon ? " \u6B66\u5668" : ""}`,
4700
+ endsAt,
4701
+ up: [...new Set(items.map((i) => String(i["name"]).trim()))],
4702
+ ...startsAt === void 0 ? {} : { startsAt },
4703
+ ...named === void 0 ? { type: weapon ? "\u6B66\u5668 / \u5149\u9525" : "\u89D2\u8272" } : {},
4704
+ ...icon === void 0 ? {} : { cover: icon }
4705
+ });
4706
+ }
4707
+ return pools.sort((a, b) => a.endsAt - b.endsAt);
4708
+ }
4709
+ async function readHoyo(src, now, fetchImpl, signal) {
4710
+ const url = `${src.api.replace(/\/$/, "")}/${src.game}/calendar?lang=${src.lang}`;
4711
+ const timeout = AbortSignal.timeout(TIMEOUT_MS);
4712
+ try {
4713
+ const response = await fetchImpl(url, { signal: signal === void 0 ? timeout : AbortSignal.any([signal, timeout]), headers: { "user-agent": USER_AGENT, accept: "application/json" } });
4714
+ if (!response.ok)
4715
+ throw new Error(`HTTP ${response.status}`);
4716
+ return parseHoyo(await response.json(), now);
4717
+ } catch (error) {
4718
+ const why = error instanceof Error ? error.name === "TimeoutError" ? `${TIMEOUT_MS / 1e3} \u79D2\u6CA1\u6709\u54CD\u5E94` : error.message : String(error);
4719
+ throw new Error(`${new URL(src.api).host} \u7684\u5361\u6C60\u8BFB\u4E0D\u5230\uFF1A${why}`);
4720
+ }
4721
+ }
4606
4722
  function parseSra(body, timeZone, now) {
4607
4723
  const b = typeof body === "object" && body !== null ? body : {};
4608
4724
  const str = (v) => typeof v === "string" && v.trim() !== "" ? v.trim() : void 0;
@@ -4812,6 +4928,26 @@ var DailyReport = class {
4812
4928
  };
4813
4929
 
4814
4930
  // packages/game-intel/lib/report.js
4931
+ var WEEK_WARN_MS = 2 * MS.DAY;
4932
+ function routineLines(game, now) {
4933
+ const lines = [];
4934
+ for (const [period, label] of [["daily", "\u65E5\u5E38"], ["weekly", "\u5468\u5E38"]]) {
4935
+ const list = (game.routines ?? []).filter((r) => r.period === period);
4936
+ if (list.length === 0)
4937
+ continue;
4938
+ const missing = list.filter((r) => r.done === void 0);
4939
+ const done = list.length - missing.length;
4940
+ const resets = list.find((r) => r.resetsAt !== void 0)?.resetsAt;
4941
+ if (missing.length === 0) {
4942
+ lines.push(`\xB7 ${label} ${done}/${list.length} \u90FD\u505A\u5B8C\u4E86`);
4943
+ continue;
4944
+ }
4945
+ const warn = period === "weekly" && resets !== void 0 && resets - now < WEEK_WARN_MS;
4946
+ const when = resets === void 0 ? "" : `\uFF08${shortDate(resets, game.timezone)} \u91CD\u7F6E${warn ? `\uFF0C${leftText(resets, now)}` : ""}\uFF09`;
4947
+ lines.push(`\xB7 ${warn ? "\u26A0 " : ""}${label} ${done}/${list.length}\uFF0C\u8FD8\u5DEE\uFF1A${missing.map((r) => r.name).join("\u3001")}${when}`);
4948
+ }
4949
+ return lines;
4950
+ }
4815
4951
  var UPCOMING_MS = 3 * MS.DAY;
4816
4952
  function buildReport(games, activities, settings, now) {
4817
4953
  const local = wallClock(now, Intl.DateTimeFormat().resolvedOptions().timeZone);
@@ -4825,7 +4961,8 @@ function buildReport(games, activities, settings, now) {
4825
4961
  const mine = activities.filter((a) => a.game === game.id);
4826
4962
  const running = mine.filter((a) => (a.startsAt ?? 0) <= now && Math.max(a.endsAt ?? Infinity, a.shopEndsAt ?? 0) > now);
4827
4963
  const upcoming = mine.filter((a) => a.startsAt !== void 0 && a.startsAt > now && a.startsAt - now <= UPCOMING_MS);
4828
- if (running.length === 0 && upcoming.length === 0)
4964
+ const routines = routineLines(game, now);
4965
+ if (running.length === 0 && upcoming.length === 0 && routines.length === 0)
4829
4966
  continue;
4830
4967
  const long = running.filter((a) => isLong(a) && a.todo.length === 0 && daysLeft(a.endsAt, now) >= settings.soonDays);
4831
4968
  const lines = running.filter((a) => !long.includes(a)).map((a) => {
@@ -4844,7 +4981,7 @@ function buildReport(games, activities, settings, now) {
4844
4981
  lines.push(`\xB7 \u957F\u671F\uFF1A${long.map((a) => `${a.name}\uFF08${leftText(a.endsAt, now)}\uFF09`).join("\u3001")}`);
4845
4982
  }
4846
4983
  sections.push(`\u3010${game.name}\u3011
4847
- ${lines.join("\n")}`);
4984
+ ${[...routines, ...lines].join("\n")}`);
4848
4985
  }
4849
4986
  if (sections.length === 0)
4850
4987
  return { title, text: "\u4ECA\u5929\u6CA1\u6709\u8FDB\u884C\u4E2D\u7684\u6D3B\u52A8\uFF08\u6216\u8005\u8FD8\u6CA1\u8BFB\u5230\uFF09\u3002\u53EF\u4EE5\u5728\u300C\u6E38\u620F\u300D\u9875\u5237\u65B0\u6570\u636E\u7AD9\uFF0C\u6216\u8BA9 agent \u8FDB\u6E38\u620F\u770B\u770B\u3002", count: 0 };
@@ -4883,9 +5020,10 @@ function recentDigest(games, activities, settings, now) {
4883
5020
  text += `\uFF1B\u8FDB\u5EA6 ${a.progress}`;
4884
5021
  lines.push(text);
4885
5022
  }
4886
- if (lines.length > 0)
5023
+ const routines = routineLines(game, now);
5024
+ if (lines.length + routines.length > 0)
4887
5025
  sections.push(`\u3010${game.name}\u3011
4888
- ${lines.join("\n")}`);
5026
+ ${[...routines, ...lines].join("\n")}`);
4889
5027
  }
4890
5028
  return sections.length === 0 ? void 0 : sections.join("\n\n");
4891
5029
  }
@@ -4960,6 +5098,7 @@ function matchScripts(scripts, games) {
4960
5098
  var SRA_GAMES = [
4961
5099
  { sra: "sr", name: "\u5D29\u574F\uFF1A\u661F\u7A79\u94C1\u9053", timezone: "Asia/Shanghai", words: ["\u661F\u7A79\u94C1\u9053", "\u661F\u94C1", "\u5D29\u94C1", "Star Rail", "StarRail", "HSR", "March7th", "\u4E09\u6708\u4E03"] },
4962
5100
  { sra: "ys", name: "\u539F\u795E", timezone: "Asia/Shanghai", words: ["Genshin", "BetterGI", "better-genshin"] },
5101
+ { sra: "bh3", name: "\u5D29\u574F3", timezone: "Asia/Shanghai", words: ["\u5D29\u574F3", "\u5D29\u574F\u4E09", "Honkai Impact 3", "HonkaiImpact3rd"] },
4963
5102
  { sra: "zzz", name: "\u7EDD\u533A\u96F6", timezone: "Asia/Shanghai", words: ["Zenless", "ZZZ", "OneDragon", "\u4E00\u6761\u9F99"] },
4964
5103
  { sra: "ww", name: "\u9E23\u6F6E", timezone: "Asia/Shanghai", words: ["Wuthering", "ok-ww", "okww", "WutheringWaves"] },
4965
5104
  { sra: "nte", name: "\u5F02\u73AF", timezone: "Asia/Shanghai", words: ["Neverness"] },
@@ -4986,6 +5125,253 @@ function followedManifest(game) {
4986
5125
  sources: [{ id: "sra", name: "SRA \u516C\u5171 API", homepage: "https://starrailassistant.top/reference/public-api/", sra: { game: game.sra } }]
4987
5126
  });
4988
5127
  }
5128
+ var POOL_SOURCES = {
5129
+ // PRTS: one file page per banner (its key art), with the CN server's open and close times.
5130
+ ak: GameSource.parse({
5131
+ id: "prts-pools",
5132
+ name: "PRTS \u5BFB\u8BBF",
5133
+ homepage: "https://prts.wiki/w/\u5361\u6C60\u4E00\u89C8/\u9650\u65F6\u5BFB\u8BBF",
5134
+ pools: { api: "https://prts.wiki/api.php", conditions: "[[\u5BFB\u8BBF\u540Dcn::+]]", title: "\u5BFB\u8BBF\u540Dcn", start: "\u5BFB\u8BBF\u5F00\u542F\u65F6\u95F4cn", end: "\u5BFB\u8BBF\u5173\u95ED\u65F6\u95F4cn", up: ["\u51FA\u7387\u63D0\u5347\u5E72\u5458"] }
5135
+ }),
5136
+ ys: GameSource.parse({
5137
+ id: "hoyo-pools",
5138
+ name: "\u7948\u613F\uFF08ennead API\uFF09",
5139
+ homepage: "https://api.ennead.cc/mihoyo/genshin/calendar?lang=zh-cn",
5140
+ hoyo: { game: "genshin" }
5141
+ })
5142
+ };
5143
+ var givesPools = (s) => s.pools !== void 0 || s.hoyo !== void 0;
5144
+ function poolSources(manifest) {
5145
+ const own = manifest.sources.filter(givesPools);
5146
+ if (own.length > 0)
5147
+ return own;
5148
+ return [...new Set(manifest.sources.map((s) => s.sra?.game).filter((g) => g !== void 0))].map((g) => POOL_SOURCES[g]).filter((s) => s !== void 0);
5149
+ }
5150
+
5151
+ // packages/game-intel/lib/routines.js
5152
+ import { readFile as readFile2 } from "node:fs/promises";
5153
+ import { join as join2 } from "node:path";
5154
+ function hm(at) {
5155
+ const [h, m] = at.split(":").map(Number);
5156
+ return [h, m];
5157
+ }
5158
+ function dailyPeriod(now, timeZone, reset) {
5159
+ const [h, m] = hm(reset);
5160
+ const w = wallClock(now, timeZone);
5161
+ let start = zonedTime(w.year, w.month, w.day, h, m, timeZone);
5162
+ if (start > now)
5163
+ start = zonedTime(w.year, w.month, w.day - 1, h, m, timeZone);
5164
+ const s = wallClock(start, timeZone);
5165
+ return { start, end: zonedTime(s.year, s.month, s.day + 1, h, m, timeZone) };
5166
+ }
5167
+ function weeklyPeriod(now, timeZone, day, at) {
5168
+ const [h, m] = hm(at);
5169
+ const w = wallClock(now, timeZone);
5170
+ const weekday = (new Date(Date.UTC(w.year, w.month - 1, w.day)).getUTCDay() + 6) % 7 + 1;
5171
+ const back = (weekday - day + 7) % 7;
5172
+ let start = zonedTime(w.year, w.month, w.day - back, h, m, timeZone);
5173
+ if (start > now)
5174
+ start = zonedTime(w.year, w.month, w.day - back - 7, h, m, timeZone);
5175
+ const s = wallClock(start, timeZone);
5176
+ return { start, end: zonedTime(s.year, s.month, s.day + 7, h, m, timeZone) };
5177
+ }
5178
+ function resetsOf(manifest, now) {
5179
+ return {
5180
+ daily: dailyPeriod(now, manifest.timezone, manifest.dailyReset),
5181
+ weekly: weeklyPeriod(now, manifest.timezone, manifest.weeklyReset.day, manifest.weeklyReset.at ?? manifest.dailyReset)
5182
+ };
5183
+ }
5184
+ function patternText(pattern) {
5185
+ return pattern.replace(/\\([^a-zA-Z0-9])/g, "$1").replace(/\\[dswbDSWB]/g, "").replace(/\(\?[:=!]/g, "(").replace(/\.\*\??|\.\+\??/g, "\u2026").replace(/[?*+^$]/g, "").replace(/\|/g, " / ").replace(/[()[\]{}]/g, "").replace(/\s+/g, " ").trim();
5186
+ }
5187
+ function compile(patterns) {
5188
+ const out = [];
5189
+ for (const p of patterns) {
5190
+ try {
5191
+ out.push(new RegExp(p));
5192
+ } catch {
5193
+ }
5194
+ }
5195
+ return out;
5196
+ }
5197
+ function gameRoutines(manifest, scripts, own, hidden) {
5198
+ const out = /* @__PURE__ */ new Map();
5199
+ for (const r of manifest.routines) {
5200
+ if (out.has(r.id))
5201
+ continue;
5202
+ const logs = routineLogs(r);
5203
+ out.set(r.id, {
5204
+ id: r.id,
5205
+ name: r.name,
5206
+ period: r.period,
5207
+ ...r.description === void 0 ? {} : { description: r.description },
5208
+ from: "game",
5209
+ events: compile(r.events ?? []),
5210
+ links: logs.length === 0 ? [] : [{ logs: compile(logs), tasks: [], text: logs.map((p) => `\u811A\u672C\u65E5\u5FD7\u300C${patternText(p)}\u300D`) }]
5211
+ });
5212
+ }
5213
+ for (const script of scripts) {
5214
+ for (const r of script.manifest?.routines ?? []) {
5215
+ const logs = routineLogs(r);
5216
+ const tasks = r.tasks ?? [];
5217
+ const titles = tasks.map((t) => script.manifest?.tasks.find((x) => x.id === t)?.title ?? t);
5218
+ const link2 = {
5219
+ script: script.id,
5220
+ scriptName: script.name,
5221
+ logs: compile(logs),
5222
+ tasks,
5223
+ text: [...logs.map((p) => `${script.name} \u65E5\u5FD7\u300C${patternText(p)}\u300D`), ...titles.length === 0 ? [] : [`${script.name}\u300C${titles.join("\u300D\u300C")}\u300D\u8FD0\u884C\u6210\u529F`]]
5224
+ };
5225
+ const known = out.get(r.id);
5226
+ if (known !== void 0) {
5227
+ if (link2.text.length > 0)
5228
+ known.links.push(link2);
5229
+ continue;
5230
+ }
5231
+ out.set(r.id, {
5232
+ id: r.id,
5233
+ name: r.name,
5234
+ period: r.period,
5235
+ ...r.description === void 0 ? {} : { description: r.description },
5236
+ from: `script:${script.id}`,
5237
+ events: compile(r.events ?? []),
5238
+ links: link2.text.length === 0 ? [] : [link2]
5239
+ });
5240
+ }
5241
+ }
5242
+ for (const r of own)
5243
+ if (!out.has(r.id))
5244
+ out.set(r.id, { id: r.id, name: r.name, period: r.period, from: "own", events: [], links: [] });
5245
+ return [...out.values()].filter((r) => !hidden.includes(r.id));
5246
+ }
5247
+ function routineEvents(def, activities, now) {
5248
+ if (def.period !== "event" || def.events.length === 0)
5249
+ return [];
5250
+ return activities.filter((a) => def.events.some((re) => re.test(a.name)) && (a.endsAt ?? Infinity) > now).sort((a, b) => Number((b.startsAt ?? 0) <= now) - Number((a.startsAt ?? 0) <= now) || (a.startsAt ?? 0) - (b.startsAt ?? 0));
5251
+ }
5252
+ function periodKey(def, resets, events, now) {
5253
+ if (def.period === "daily")
5254
+ return { key: `d:${resets.daily.start}`, start: resets.daily.start, end: resets.daily.end };
5255
+ if (def.period === "weekly")
5256
+ return { key: `w:${resets.weekly.start}`, start: resets.weekly.start, end: resets.weekly.end };
5257
+ const running = events.find((a) => (a.startsAt ?? 0) <= now);
5258
+ return running === void 0 ? void 0 : { key: `e:${running.id}`, start: running.startsAt ?? 0, ...running.endsAt === void 0 ? {} : { end: running.endsAt } };
5259
+ }
5260
+ function withOfficialRoutines(mine, official) {
5261
+ if (official === void 0 || official.routines.length === 0)
5262
+ return mine;
5263
+ const taskIds = new Set(mine.tasks.map((t) => t.id));
5264
+ const own = new Map(mine.routines.map((r) => [r.id, r]));
5265
+ const routines = [...mine.routines.map((r) => {
5266
+ const theirs = official.routines.find((o) => o.id === r.id);
5267
+ if (theirs === void 0)
5268
+ return r;
5269
+ const logs = [.../* @__PURE__ */ new Set([...routineLogs(r), ...routineLogs(theirs)])];
5270
+ const tasks = [.../* @__PURE__ */ new Set([...r.tasks ?? [], ...(theirs.tasks ?? []).filter((t) => taskIds.has(t))])];
5271
+ return { ...r, ...logs.length === 0 ? {} : { log: logs }, ...tasks.length === 0 ? {} : { tasks }, ...r.events === void 0 && theirs.events !== void 0 ? { events: theirs.events } : {} };
5272
+ })];
5273
+ for (const r of official.routines) {
5274
+ if (own.has(r.id))
5275
+ continue;
5276
+ const tasks = (r.tasks ?? []).filter((t) => taskIds.has(t));
5277
+ const { tasks: _dropped, ...rest } = r;
5278
+ routines.push(tasks.length === 0 ? rest : { ...rest, tasks });
5279
+ }
5280
+ return { tasks: mine.tasks, routines };
5281
+ }
5282
+ var Done = external_exports.object({
5283
+ key: external_exports.string(),
5284
+ at: external_exports.number(),
5285
+ by: external_exports.enum(["log", "task", "agent", "user"]),
5286
+ detail: external_exports.string().optional(),
5287
+ script: external_exports.string().optional(),
5288
+ runId: external_exports.string().optional()
5289
+ });
5290
+ var BookSchema = external_exports.object({
5291
+ /** By game, then routine id: the last time it was done (only counts in its period). */
5292
+ done: external_exports.record(external_exports.string(), external_exports.record(external_exports.string(), Done)).default({}),
5293
+ /** The user's own, by game. */
5294
+ own: external_exports.record(external_exports.string(), external_exports.array(external_exports.object({ id: external_exports.string(), name: external_exports.string(), period: external_exports.enum(["daily", "weekly"]), at: external_exports.number() }))).default({}),
5295
+ /** Pack routines the user took off the page, by game. */
5296
+ hidden: external_exports.record(external_exports.string(), external_exports.array(external_exports.string())).default({})
5297
+ });
5298
+ var RoutineBook = class {
5299
+ #file;
5300
+ #book = { done: {}, own: {}, hidden: {} };
5301
+ constructor(dir) {
5302
+ this.#file = join2(dir, "routines.json");
5303
+ }
5304
+ async load() {
5305
+ try {
5306
+ this.#book = BookSchema.parse(JSON.parse(await readFile2(this.#file, "utf8")));
5307
+ } catch {
5308
+ }
5309
+ }
5310
+ async #save() {
5311
+ await writeFileAtomic(this.#file, `${JSON.stringify(this.#book, null, 1)}
5312
+ `);
5313
+ }
5314
+ done(game, routine, key) {
5315
+ const d = this.#book.done[game]?.[routine];
5316
+ return d?.key === key ? d : void 0;
5317
+ }
5318
+ /** Mark it done in the period `key`. @returns false when it already was (the first word stands). */
5319
+ async mark(game, routine, done) {
5320
+ if (this.#book.done[game]?.[routine]?.key === done.key)
5321
+ return false;
5322
+ (this.#book.done[game] ??= {})[routine] = done;
5323
+ await this.#save();
5324
+ return true;
5325
+ }
5326
+ async unmark(game, routine) {
5327
+ if (this.#book.done[game]?.[routine] === void 0)
5328
+ return;
5329
+ delete this.#book.done[game][routine];
5330
+ await this.#save();
5331
+ }
5332
+ own(game) {
5333
+ return [...this.#book.own[game] ?? []];
5334
+ }
5335
+ async addOwn(game, name, period, at) {
5336
+ const list = this.#book.own[game] ??= [];
5337
+ let n = list.length + 1;
5338
+ while (list.some((r) => r.id === `own-${n}`))
5339
+ n++;
5340
+ const routine = { id: `own-${n}`, name: name.trim(), period, at };
5341
+ list.push(routine);
5342
+ await this.#save();
5343
+ return routine;
5344
+ }
5345
+ /** Delete the user's own (true), or hide a pack's (false). */
5346
+ async remove(game, routine) {
5347
+ const own = this.#book.own[game] ?? [];
5348
+ if (own.some((r) => r.id === routine)) {
5349
+ this.#book.own[game] = own.filter((r) => r.id !== routine);
5350
+ delete this.#book.done[game]?.[routine];
5351
+ await this.#save();
5352
+ return true;
5353
+ }
5354
+ const hidden = this.#book.hidden[game] ??= [];
5355
+ if (!hidden.includes(routine))
5356
+ hidden.push(routine);
5357
+ await this.#save();
5358
+ return false;
5359
+ }
5360
+ hidden(game) {
5361
+ return [...this.#book.hidden[game] ?? []];
5362
+ }
5363
+ async restore(game) {
5364
+ delete this.#book.hidden[game];
5365
+ await this.#save();
5366
+ }
5367
+ /** Forget a game (one made here that is deleted). */
5368
+ async forget(game) {
5369
+ delete this.#book.done[game];
5370
+ delete this.#book.own[game];
5371
+ delete this.#book.hidden[game];
5372
+ await this.#save();
5373
+ }
5374
+ };
4989
5375
 
4990
5376
  // packages/game-intel/lib/reading-tasks.js
4991
5377
  var ReadingTasks = class {
@@ -5348,7 +5734,9 @@ function gameTools(deps) {
5348
5734
  screen: g.screen === void 0 ? "\u672A\u8BBE\u7F6E" : g.screen.target === "emulator" ? "target=emulator" : `target=window${g.screen.window === void 0 ? "\uFF08\u5148 window_list \u627E\u7A97\u53E3\uFF09" : `\uFF0Cwindow=${g.screen.window}`}`,
5349
5735
  sources: g.sources.map((s) => ({ name: s.name, auto: s.auto, ...s.homepage === void 0 ? {} : { homepage: s.homepage } })),
5350
5736
  ...g.refreshed === void 0 ? {} : { refreshed: { at: new Date(g.refreshed.at).toISOString(), ok: g.refreshed.ok, count: g.refreshed.count, ...g.refreshed.detail === void 0 ? {} : { detail: g.refreshed.detail } } },
5351
- ...g.reading === void 0 ? {} : { last_read_in_game: new Date(g.reading.at).toISOString() }
5737
+ ...g.reading === void 0 ? {} : { last_read_in_game: new Date(g.reading.at).toISOString() },
5738
+ ...g.pools?.length ? { pools: g.pools.map((p) => ({ name: p.name, ends: shortDate(p.endsAt, g.timezone), ...p.up.length ? { up: p.up.slice(0, 6) } : {} })) } : {},
5739
+ ...g.routines?.length ? { routines: g.routines.map((r) => ({ name: r.name, period: r.period === "daily" ? "\u65E5\u5E38" : r.period === "weekly" ? "\u5468\u5E38" : "\u5468\u671F\u73A9\u6CD5", done: r.done !== void 0 })) } : {}
5352
5740
  }))
5353
5741
  })
5354
5742
  }),
@@ -5560,6 +5948,143 @@ function gameTools(deps) {
5560
5948
  };
5561
5949
  }
5562
5950
  }),
5951
+ tool({
5952
+ name: "game_routine",
5953
+ description: [
5954
+ "\u6E38\u620F\u9875\u300C\u65E5\u5E38 \xB7 \u5468\u5E38\u300D\u7684\u6E05\u5355\uFF1Aaction=list \u770B\u6BCF\u4E00\u9879\u8FD9\u4E00\u671F\uFF08\u4ECA\u5929\u3001\u8FD9\u5468\u3001\u8FD9\u4E00\u671F\u5468\u671F\u73A9\u6CD5\uFF09\u505A\u6CA1\u505A\u5B8C\u3001\u8C01\u52FE\u7684\u3001\u9760\u4EC0\u4E48\u81EA\u52A8\u52FE\u3001\u4EC0\u4E48\u65F6\u5019\u91CD\u7F6E\uFF1Baction=set \u52FE\u4E0A\u6216\u53D6\u6D88\u4E00\u9879\u3002",
5955
+ "\u811A\u672C\u65E5\u5FD7\u6216\u4EFB\u52A1\u6210\u529F\u80FD\u8BC1\u660E\u7684\u9879\u76EE\u63D2\u4EF6\u4F1A\u81EA\u5DF1\u52FE\uFF0C\u4E5F\u4F1A\u56DE\u5934\u8BFB\u8FD9\u4E00\u671F\u65E9\u4E9B\u65F6\u5019\u7684\u8FD0\u884C\u8BB0\u5F55\u8865\u4E0A\uFF08list \u4E4B\u524D\u5DF2\u7ECF\u8BFB\u8FC7\u4E00\u904D\uFF09\u2014\u2014\u6240\u4EE5\u5148 list\uFF0C\u6CA1\u52FE\u4E0A\u7684\u518D\u67E5\u539F\u56E0\uFF1A\u811A\u672C\u5305\u6CA1\u5199 routines\u3001log \u5199\u7684\u4E0D\u662F\u65E5\u5FD7\u91CC\u771F\u5B9E\u7684\u539F\u6587\u3001\u6216\u8005\u786E\u5B9E\u6CA1\u505A\u3002",
5956
+ "set \u53EA\u5728\u4F60\u4EB2\u773C\u770B\u5230\u5B8C\u6210\u8BC1\u636E\u65F6\u7528\uFF08\u8FD0\u884C\u65E5\u5FD7\u91CC\u7684\u539F\u6587\u3001\u6E38\u620F\u753B\u9762\u4E0A\u7684\u5B8C\u6210\u6587\u5B57\uFF09\uFF0Cevidence \u5199\u539F\u6587\u548C\u51FA\u5904\uFF1B\u9875\u9762\u4E0A\u4F1A\u663E\u793A\u6210\u300Cagent \u8BB0\u7684\u300D\uFF0C\u548C\u7528\u6237\u81EA\u5DF1\u52FE\u7684\u5206\u5F00\u3002\u4E0D\u8981\u7528\u6D4F\u89C8\u5668\u70B9\u6E38\u620F\u9875\u3001\u6216\u8C03\u7528\u63A5\u53E3\u53BB\u52FE\uFF0C\u90A3\u6837\u4F1A\u88AB\u5F53\u6210\u7528\u6237\u52FE\u7684\u3002",
5957
+ "\u7528\u6237\u4E0D\u80FD\u5728\u9875\u9762\u4E0A\u81EA\u5DF1\u52A0\u65E5\u5E38\u5468\u5E38\uFF0C\u8981\u52A0\u90FD\u7531 agent \u52A0\uFF1A\u811A\u672C\u505A\u7684\u5199\u8FDB\u90A3\u4E2A\u811A\u672C\u7684\u811A\u672C\u5305 routines\uFF08pack_propose\uFF0Clog \u5199\u65E5\u5FD7\u91CC\u505A\u5B8C\u65F6\u7684\u539F\u6587\uFF09\uFF0C\u8DD1\u5B8C\u4F1A\u81EA\u52A8\u52FE\uFF1Baction=add \u53EA\u52A0\u6CA1\u6709\u811A\u672C\u505A\u3001\u53EA\u80FD\u624B\u52A8\u52FE\u7684\uFF08name\u3001period\uFF09\uFF0C\u540C\u540D\u7684\u4E0D\u4F1A\u91CD\u590D\u52A0\u3002"
5958
+ ].join(""),
5959
+ properties: {
5960
+ game: GAME,
5961
+ action: { type: "string", enum: ["list", "set", "add"], description: "list\uFF1A\u770B\u6E05\u5355\uFF1Bset\uFF1A\u52FE\u4E0A\u6216\u53D6\u6D88\u4E00\u9879\uFF1Badd\uFF1A\u52A0\u4E00\u9879\u53EA\u80FD\u624B\u52A8\u52FE\u7684\u3002" },
5962
+ routine: { type: "string", description: "action=set\uFF1A\u65E5\u5E38\u7684 id \u6216\u540D\u79F0\uFF08\u7167 list \u91CC\u7684\u5199\uFF09\u3002" },
5963
+ done: { type: "boolean", description: "action=set\uFF1Atrue \u52FE\u4E0A\uFF08\u9ED8\u8BA4\uFF09\uFF0Cfalse \u53D6\u6D88\u3002" },
5964
+ evidence: { type: "string", description: "action=set \u4E14 done=true\uFF1A\u770B\u5230\u7684\u5B8C\u6210\u8BC1\u636E\u539F\u6587\u548C\u51FA\u5904\uFF0C\u4F8B\u5982\u300CMAA \u8FD0\u884C run-\u2026 \u65E5\u5FD7\uFF1A\u5B8C\u6210\u4EFB\u52A1: \u57FA\u5EFA\u6362\u73ED\u300D\u6216\u300C\u6E38\u620F\u753B\u9762\uFF1A\u6BCF\u65E5\u5B9E\u8BAD 500/500\u300D\u3002" },
5965
+ name: { type: "string", description: "action=add\uFF1A\u6E38\u620F\u91CC\u7684\u53EB\u6CD5\uFF0C\u4F8B\u5982\u300C\u6BCF\u65E5\u59D4\u6258\u300D\u300C\u5468\u672C\u300D\uFF0C40 \u5B57\u4EE5\u5185\u3002" },
5966
+ period: { type: "string", enum: ["daily", "weekly"], description: "action=add\uFF1Adaily \u65E5\u5E38\uFF08\u6BCF\u5929\u91CD\u7F6E\uFF09\uFF0Cweekly \u5468\u5E38\uFF08\u6BCF\u5468\u91CD\u7F6E\uFF09\u3002" }
5967
+ },
5968
+ required: ["game", "action"],
5969
+ kind: "other",
5970
+ title: (a) => `${a["action"] === "set" ? "\u52FE\u9009\u65E5\u5E38" : a["action"] === "add" ? "\u6DFB\u52A0\u65E5\u5E38" : "\u65E5\u5E38\u6E05\u5355"}\uFF1A${String(a["game"] ?? "")}`,
5971
+ execute: async (args) => {
5972
+ const a = parse(external_exports.object({
5973
+ game: external_exports.string(),
5974
+ action: external_exports.enum(["list", "set", "add"]),
5975
+ routine: external_exports.string().trim().min(1).max(80).nullish(),
5976
+ done: external_exports.boolean().nullish(),
5977
+ evidence: external_exports.string().trim().max(300).nullish(),
5978
+ name: external_exports.string().trim().min(1).max(40).nullish(),
5979
+ period: external_exports.enum(["daily", "weekly"]).nullish()
5980
+ }), args);
5981
+ const game = known(a.game);
5982
+ const PERIOD = { daily: "\u65E5\u5E38", weekly: "\u5468\u5E38", event: "\u5468\u671F\u73A9\u6CD5" };
5983
+ const BY = { log: "\u811A\u672C\u65E5\u5FD7", task: "\u811A\u672C\u4EFB\u52A1\u6210\u529F", agent: "agent \u8BB0\u7684", user: "\u7528\u6237\u81EA\u5DF1\u52FE\u7684" };
5984
+ const view = (r) => ({
5985
+ id: r.id,
5986
+ name: r.name,
5987
+ kind: PERIOD[r.period],
5988
+ done: r.done === void 0 ? false : { at: new Date(r.done.at).toLocaleString("zh-CN", { timeZone: game.timezone, hour12: false }), by: BY[r.done.by], ...r.done.detail === void 0 ? {} : { detail: r.done.detail }, ...r.done.script === void 0 ? {} : { script: r.done.script } },
5989
+ auto: r.links.length > 0 ? r.links : "\u6CA1\u6709\uFF1A\u53EA\u80FD\u624B\u52A8\u52FE\uFF08\u811A\u672C\u5305\u91CC\u52A0 routines \u7684 log \u6216 tasks \u624D\u4F1A\u81EA\u52A8\u52FE\uFF09",
5990
+ ...r.resetsAt === void 0 ? {} : { resets: shortDate(r.resetsAt, game.timezone) }
5991
+ });
5992
+ if (a.action === "list") {
5993
+ const status = await deps.routines(game.id);
5994
+ return {
5995
+ routines: status.routines.map(view),
5996
+ next: status.routines.length === 0 ? "\u8FD9\u4E2A\u6E38\u620F\u8FD8\u6CA1\u6709\u65E5\u5E38\u5468\u5E38\uFF1A\u6E38\u620F\u5305\u6216\u73A9\u5B83\u7684\u811A\u672C\u5305\u91CC\u5199 routines \u624D\u4F1A\u5217\u51FA\u6765\u3002" : "\u6CA1\u52FE\u4E0A\u7684\u9879\u76EE\uFF1A\u5148\u770B auto \u91CC\u7684\u65E5\u5FD7\u539F\u6587\u5728\u8FD9\u4E00\u671F\u7684\u8FD0\u884C\u65E5\u5FD7\u91CC\u6709\u6CA1\u6709\uFF08script_run_log \u641C\u7D22\uFF09\uFF0C\u6709\u5C31\u662F\u63D2\u4EF6\u6CA1\u8BFB\u5230\uFF0C\u544A\u8BC9\u7528\u6237\uFF1B\u6CA1\u6709\u5C31\u662F\u8FD8\u6CA1\u505A\u6216\u811A\u672C\u5305\u7684 log \u5199\u5F97\u4E0D\u5BF9\u3002"
5997
+ };
5998
+ }
5999
+ if (a.action === "add") {
6000
+ if (a.name == null || a.period == null)
6001
+ throw new Error("action=add \u8981\u5199 name\uFF08\u6E38\u620F\u91CC\u7684\u53EB\u6CD5\uFF09\u548C period\uFF08daily \u6216 weekly\uFF09");
6002
+ const routine2 = await deps.addRoutine(game.id, a.name, a.period);
6003
+ return { routine: view(routine2), next: `\u52A0\u4E0A\u4E86\uFF0C\u6E38\u620F\u9875\u7684\u300C${PERIOD[a.period]}\u300D\u91CC\u80FD\u770B\u5230\uFF1B\u5B83\u53EA\u80FD\u624B\u52A8\u52FE\uFF08\u4F60\u770B\u5230\u5B8C\u6210\u8BC1\u636E\u65F6\u7528 set \u52FE\uFF09\u3002` };
6004
+ }
6005
+ if (a.routine == null)
6006
+ throw new Error("action=set \u8981\u5199 routine\uFF08\u7167 list \u91CC\u7684 id \u6216\u540D\u79F0\uFF09");
6007
+ const done = a.done ?? true;
6008
+ if (done && (a.evidence == null || a.evidence.length < 4))
6009
+ throw new Error("\u52FE\u4E0A\u8981\u5199 evidence\uFF1A\u4F60\u770B\u5230\u7684\u5B8C\u6210\u8BC1\u636E\u539F\u6587\u548C\u51FA\u5904\uFF08\u65E5\u5FD7\u884C\u3001\u6E38\u620F\u753B\u9762\u4E0A\u7684\u6587\u5B57\uFF09\u3002\u6CA1\u6709\u8BC1\u636E\u5C31\u4E0D\u8981\u52FE\u3002");
6010
+ const routine = await deps.setRoutine(game.id, a.routine, done, a.evidence ?? void 0);
6011
+ return { routine: view(routine), next: done ? "\u52FE\u4E0A\u4E86\uFF0C\u6E38\u620F\u9875\u663E\u793A\u4E3A\u300Cagent \u8BB0\u7684\u300D\u3002" : "\u53D6\u6D88\u4E86\u3002" };
6012
+ }
6013
+ }),
6014
+ tool({
6015
+ name: "game_pool",
6016
+ description: [
6017
+ "\u628A\u8BFB\u5230\u7684\u5361\u6C60\uFF08\u7948\u613F\u3001\u8DC3\u8FC1\u3001\u8C03\u9891\u3001\u5524\u53D6\u3001\u5BFB\u8BBF\u7B49\u62BD\u5361\u6C60\uFF09\u8BB0\u5230\u6E38\u620F\u9875\u7684\u300C\u5361\u6C60\u300D\u91CC\uFF1A\u6BCF\u4E2A\u5361\u6C60\u5199\u540D\u79F0\u3001UP \u7684\u89D2\u8272\u6216\u6B66\u5668\uFF08\u4E94\u661F\u5728\u524D\uFF09\u3001\u5F00\u59CB\u548C\u7ED3\u675F\u65F6\u95F4\uFF08\u6E38\u620F\u670D\u52A1\u5668\u65F6\u95F4\uFF09\u3002",
6018
+ "\u8BFB\u5B98\u65B9\u516C\u544A\u3001\u6E38\u620F\u5185\u516C\u544A\u6216 Wiki \u65F6\u770B\u5230\u5361\u6C60\u5C31\u8BB0\uFF0C\u6570\u636E\u7AD9\u5DF2\u7ECF\u5217\u51FA\u7684\u5361\u6C60\u4E0D\u7528\u518D\u8BB0\uFF1B\u540C\u540D\u7684\u518D\u8BB0\u5C31\u662F\u66F4\u65B0\u539F\u6765\u90A3\u6761\uFF0C\u7ED3\u675F\u4E86\u7684\u4F1A\u81EA\u5DF1\u6E05\u6389\u3002\u53EA\u8BB0\u6709\u51FA\u5904\u3001\u6709\u7ED3\u675F\u65F6\u95F4\u7684\u3002"
6019
+ ].join(""),
6020
+ properties: {
6021
+ game: GAME,
6022
+ pools: {
6023
+ type: "array",
6024
+ description: "\u6BCF\u4E2A\u5361\u6C60\u4E00\u6761\u3002",
6025
+ items: {
6026
+ type: "object",
6027
+ properties: {
6028
+ name: { type: "string", description: "\u5361\u6C60\u540D\uFF0C\u4F8B\u5982\u300C\u8776\u7ACB\u950B\u9537\u300D\u6216\u300C\u89D2\u8272\u6D3B\u52A8\u8DC3\u8FC1\uFF1A\u9EC4\u6CC9\u300D\u3002" },
6029
+ type: { type: "string", description: "\u5361\u6C60\u7C7B\u578B\uFF0C\u4F8B\u5982 \u89D2\u8272\u6D3B\u52A8\u8DC3\u8FC1\u3001\u5149\u9525\u6D3B\u52A8\u8DC3\u8FC1\u3001\u6B66\u5668\u6D3B\u52A8\u7948\u613F\uFF08\u53EF\u9009\uFF09\u3002" },
6030
+ up: { type: "array", items: { type: "string" }, description: "\u6982\u7387 UP \u7684\u89D2\u8272\u6216\u6B66\u5668\uFF0C\u4E94\u661F\u5728\u524D\u3002" },
6031
+ starts_at: { type: "string", description: "\u5F00\u59CB\u65F6\u95F4\uFF0C\u5199\u6210 2026/12/02 12:00\uFF08\u6E38\u620F\u670D\u52A1\u5668\u65F6\u95F4\uFF09\uFF1B\u300C\u7248\u672C\u66F4\u65B0\u540E\u300D\u8FD9\u7C7B\u5199\u7248\u672C\u5F00\u59CB\u7684\u65F6\u95F4\u3002" },
6032
+ ends_at: { type: "string", description: "\u7ED3\u675F\u65F6\u95F4\uFF0C\u5199\u6210 2026/12/22 14:59\uFF08\u6E38\u620F\u670D\u52A1\u5668\u65F6\u95F4\uFF09\u3002" },
6033
+ cover: { type: "string", description: "\u5361\u6C60\u6216\u7B2C\u4E00\u4E2A UP \u89D2\u8272\u7684\u56FE\u7247\uFF08https\uFF0C\u53EF\u9009\uFF09\u3002" }
6034
+ },
6035
+ required: ["name", "ends_at"],
6036
+ additionalProperties: false
6037
+ }
6038
+ }
6039
+ },
6040
+ required: ["game", "pools"],
6041
+ kind: "other",
6042
+ title: (a) => `\u8BB0\u5F55\u5361\u6C60\uFF1A${String(a["game"] ?? "")}`,
6043
+ execute: async (args) => {
6044
+ const a = parse(external_exports.object({
6045
+ game: external_exports.string(),
6046
+ pools: external_exports.array(external_exports.object({
6047
+ name: external_exports.string().trim().min(1).max(80),
6048
+ type: external_exports.string().trim().max(40).nullish(),
6049
+ up: external_exports.array(external_exports.string().trim().min(1).max(40)).max(20).nullish(),
6050
+ starts_at: external_exports.string().nullish(),
6051
+ ends_at: external_exports.string(),
6052
+ cover: external_exports.string().nullish()
6053
+ })).min(1).max(20)
6054
+ }), args);
6055
+ const game = known(a.game);
6056
+ const now = deps.now();
6057
+ const skipped = [];
6058
+ const pools = [];
6059
+ for (const p of a.pools) {
6060
+ const endsAt = parseDateText(p.ends_at, game.timezone, now);
6061
+ if (typeof endsAt !== "number") {
6062
+ skipped.push({ name: p.name, reason: `\u7ED3\u675F\u65F6\u95F4\u300C${p.ends_at}\u300D\u8BFB\u4E0D\u61C2\uFF0C\u5199\u6210 2026/12/22 14:59` });
6063
+ continue;
6064
+ }
6065
+ if (endsAt <= now) {
6066
+ skipped.push({ name: p.name, reason: "\u5DF2\u7ECF\u7ED3\u675F\u4E86" });
6067
+ continue;
6068
+ }
6069
+ const startsAt = p.starts_at == null ? void 0 : parseDateText(p.starts_at, game.timezone, now);
6070
+ pools.push({
6071
+ name: p.name,
6072
+ endsAt,
6073
+ up: p.up ?? [],
6074
+ ...typeof startsAt === "number" ? { startsAt } : {},
6075
+ ...p.type == null || p.type === "" ? {} : { type: p.type },
6076
+ ...p.cover != null && /^https:\/\/\S+$/.test(p.cover) ? { cover: p.cover } : {}
6077
+ });
6078
+ }
6079
+ const open = pools.length === 0 ? void 0 : await deps.recordPools(game.id, pools);
6080
+ return {
6081
+ recorded: pools.map((p) => ({ name: p.name, ends: shortDate(p.endsAt, game.timezone), ...p.up.length ? { up: p.up } : {} })),
6082
+ ...skipped.length ? { skipped } : {},
6083
+ ...open === void 0 ? {} : { open_now: open },
6084
+ next: pools.length ? "\u8BB0\u4E0B\u4E86\uFF0C\u6E38\u620F\u9875\u7684\u300C\u5361\u6C60\u300D\u91CC\u80FD\u770B\u5230\u3002" : "\u4E00\u4E2A\u90FD\u6CA1\u8BB0\u4E0B\uFF1A\u770B skipped \u91CC\u7684\u539F\u56E0\u3002"
6085
+ };
6086
+ }
6087
+ }),
5563
6088
  tool({
5564
6089
  name: "game_activity_save",
5565
6090
  description: [
@@ -5700,7 +6225,7 @@ function gameTools(deps) {
5700
6225
  }
5701
6226
 
5702
6227
  // packages/game-intel/lib/index.js
5703
- var readsItself = (s) => s.smw !== void 0 || s.sra !== void 0 || s.steam !== void 0;
6228
+ var readsItself = (s) => s.smw !== void 0 || s.sra !== void 0 || s.steam !== void 0 || givesPools(s);
5704
6229
  var ConfigSchema = external_exports.object({
5705
6230
  /** The calendar and screenshots: `<dataDir>/games/`; the user's Game Packs: `<dataDir>/games/<id>/game.yaml`. */
5706
6231
  dataDir: external_exports.string().min(1),
@@ -5715,12 +6240,14 @@ var GAME_ROLE = {
5715
6240
  id: "games",
5716
6241
  name: "\u6E38\u620F",
5717
6242
  description: "\u8BFB\u6E38\u620F\u6D3B\u52A8\u548C\u8D44\u8BAF\u3001\u5199\u65E5\u62A5\u3001\u6DFB\u52A0\u6E38\u620F",
6243
+ handles: "\u6E38\u620F\u672C\u8EAB\u7684\u4E8B\u4EA4\u7ED9\u5B83\uFF1A\u67E5\u6E38\u620F\u7684\u6D3B\u52A8\u3001\u8D44\u8BAF\u3001\u5361\u6C60\u548C\u8FDB\u5EA6\uFF08\u5B98\u7F51\u3001\u516C\u544A\uFF0C\u5FC5\u8981\u65F6\u8FDB\u6E38\u620F\u770B\u753B\u9762\uFF09\u5E76\u8BB0\u8FDB\u65E5\u5386\uFF0C\u5199\u65E5\u62A5\uFF0C\u6DFB\u52A0\u6E38\u620F\u3001\u627E\u6E38\u620F\u7684\u5B89\u88C5\u8DEF\u5F84\u3002\u4E0D\u8FD0\u884C\u3001\u4E0D\u5B89\u88C5\u811A\u672C\u3002",
5718
6244
  order: 20,
5719
6245
  titles: ["\u6E38\u620F\u4FE1\u606F", "\u6E38\u620F\u8D44\u8BAF", "\u6DFB\u52A0\u6E38\u620F", "\u65E5\u62A5"],
5720
6246
  // Games and their screens, and what scripts ran (for the daily report); no installing, runs or pipelines.
5721
6247
  // skill_hub_list: its reading prompt and game_list send it there when a game's skill is missing.
5722
6248
  // browser_*: official sites and wikis that need clicking through (the built-in browser).
5723
- tools: ["game_*", "screen_*", "window_*", "browser_*", "emulator_status", "emulator_start", "script_list", "script_tasks", "script_runs", "skill_hub_list", "system_info", "skill_focus", "skill_save"],
6249
+ // script_run_log: why a 日常 did not tick is answered from the run's own lines (game_routine sends it there).
6250
+ tools: ["game_*", "screen_*", "window_*", "browser_*", "emulator_status", "emulator_start", "script_list", "script_tasks", "script_runs", "script_run_log", "skill_hub_list", "system_info", "skill_focus", "skill_save"],
5724
6251
  duty: [
5725
6252
  "# \u4F60\u7684\u89D2\u8272\uFF1A\u6E38\u620F",
5726
6253
  "\u4F60\u662F MultiGameAssistant \u7684\u300C\u6E38\u620F\u300Dagent\uFF1A\u8BFB\u6E38\u620F\u7684\u6D3B\u52A8\u3001\u8D44\u8BAF\u548C\u8FDB\u5EA6\u5E76\u8BB0\u4E0B\u6765\uFF08game_record\uFF09\uFF0C\u5199\u65E5\u62A5\uFF0C\u6DFB\u52A0\u6E38\u620F\u3002",
@@ -5731,11 +6258,13 @@ var SKILL = new URL("../skills/game-intel.md", import.meta.url);
5731
6258
  var WEB_SKILL = new URL("../skills/web-reading.md", import.meta.url);
5732
6259
  var IMAGE_TYPES = [["jpg", "image/jpeg"], ["png", "image/png"], ["webp", "image/webp"], ["gif", "image/gif"], ["avif", "image/avif"]];
5733
6260
  var MAX_IMAGE_BYTES = 8 * 1024 * 1024;
5734
- var WEB_DELIVERY = "\u4F60\u662F\u6B64\u6B21\u7F51\u9875\u63A2\u67E5\u7684\u6267\u884C\u8005\u3002\u6309\u6765\u6E90\u8BFB\u53D6\u8FD1\u671F\u5217\u8868\u53CA\u540E\u7EED\u9875\uFF0C\u76F4\u5230\u65E5\u671F\u8FB9\u754C\u6216\u672B\u9875\u3002\u7F51\u9875\u53EA\u6709\u5C11\u91CF\u7ED3\u679C\u6216\u8BFB\u4E0D\u5230\u6B63\u6587\u65F6\uFF0C\u6838\u5BF9\u5176\u4ED6\u72EC\u7ACB\u7684\u5B98\u65B9\u6765\u6E90\u3002\u5C06\u6BCF\u7BC7\u5DF2\u6838\u5B9E\u7684\u516C\u544A\u7528 game_notice \u6279\u91CF\u5165\u5E93\uFF0C\u6709\u65F6\u95F4\u8303\u56F4\u7684\u6D3B\u52A8\u7528 game_record \u5165\u5E93\u3002\u56FE\u7247\u4F18\u5148\u53D6\u6B63\u6587\u9996\u56FE\u3001\u5206\u4EAB\u56FE\u6216\u63A5\u53E3\u539F\u56FE\uFF1B\u5DF2\u6709\u6761\u76EE\u53EF\u4EE5\u8865\u56FE\u3002\u7528 game_activities \u6838\u5BF9\u5199\u5165\u7ED3\u679C\u3002\u6700\u540E\u4EA4\u56DE\u6240\u6709\u5019\u9009\uFF0C\u8868\u5934\u4E3A\u300C\u6807\u9898 | \u65E5\u671F | \u76F4\u94FE | \u914D\u56FE\u300D\uFF0C\u6BCF\u884C\u4E00\u7BC7\u3002\u5355\u5217\u300C\u8DF3\u8FC7 \u5B8C\u6574\u7F51\u5740 | \u5177\u4F53\u7406\u7531\u300D\u3002\u4EA4\u4EE3\u5B9E\u9645\u8986\u76D6\u7684\u6765\u6E90\u3001\u65E5\u671F\u8FB9\u754C\u548C\u8BFB\u53D6\u5931\u8D25\u5904\u3002";
6261
+ var WEB_DELIVERY = "\u4F60\u662F\u6B64\u6B21\u7F51\u9875\u63A2\u67E5\u7684\u6267\u884C\u8005\u3002\u6309\u6765\u6E90\u8BFB\u53D6\u8FD1\u671F\u5217\u8868\u53CA\u540E\u7EED\u9875\uFF0C\u76F4\u5230\u65E5\u671F\u8FB9\u754C\u6216\u672B\u9875\u3002\u7F51\u9875\u53EA\u6709\u5C11\u91CF\u7ED3\u679C\u6216\u8BFB\u4E0D\u5230\u6B63\u6587\u65F6\uFF0C\u6838\u5BF9\u5176\u4ED6\u72EC\u7ACB\u7684\u5B98\u65B9\u6765\u6E90\u3002\u5C06\u6BCF\u7BC7\u5DF2\u6838\u5B9E\u7684\u516C\u544A\u7528 game_notice \u6279\u91CF\u5165\u5E93\uFF0C\u6709\u65F6\u95F4\u8303\u56F4\u7684\u6D3B\u52A8\u7528 game_record \u5165\u5E93\uFF0C\u5361\u6C60\uFF08\u7948\u613F\u3001\u8DC3\u8FC1\u3001\u8C03\u9891\u3001\u5524\u53D6\u3001\u5BFB\u8BBF\uFF09\u7528 game_pool \u5165\u5E93\u3002\u56FE\u7247\u4F18\u5148\u53D6\u6B63\u6587\u9996\u56FE\u3001\u5206\u4EAB\u56FE\u6216\u63A5\u53E3\u539F\u56FE\uFF1B\u5DF2\u6709\u6761\u76EE\u53EF\u4EE5\u8865\u56FE\u3002\u7528 game_activities \u6838\u5BF9\u5199\u5165\u7ED3\u679C\u3002\u6700\u540E\u4EA4\u56DE\u6240\u6709\u5019\u9009\uFF0C\u8868\u5934\u4E3A\u300C\u6807\u9898 | \u65E5\u671F | \u76F4\u94FE | \u914D\u56FE\u300D\uFF0C\u6BCF\u884C\u4E00\u7BC7\u3002\u5355\u5217\u300C\u8DF3\u8FC7 \u5B8C\u6574\u7F51\u5740 | \u5177\u4F53\u7406\u7531\u300D\u3002\u4EA4\u4EE3\u5B9E\u9645\u8986\u76D6\u7684\u6765\u6E90\u3001\u65E5\u671F\u8FB9\u754C\u548C\u8BFB\u53D6\u5931\u8D25\u5904\u3002";
5735
6262
  var SUMMARY_TIMEOUT_MS = 3 * 6e4;
5736
6263
  var AdgGameIntel = class extends Service {
5737
6264
  static inject = ["adgNotify"];
5738
6265
  calendar;
6266
+ /** What is done of each game's 日常 / 周常 (routines.ts). */
6267
+ routines;
5739
6268
  ready;
5740
6269
  /** Injectable for tests. */
5741
6270
  fetch = (url, init) => fetch(url, init);
@@ -5757,10 +6286,12 @@ var AdgGameIntel = class extends Service {
5757
6286
  const spec = ConfigSchema.parse(config);
5758
6287
  const log = ctx.logger("adg-game-intel");
5759
6288
  this.log = log;
5760
- this.dir = join2(spec.dataDir, "games");
5761
- this.builtinDir = spec.gamesDir ?? join2(spec.dataDir, "skill-hub", "skills");
6289
+ this.dir = join3(spec.dataDir, "games");
6290
+ this.builtinDir = spec.gamesDir ?? join3(spec.dataDir, "skill-hub", "skills");
5762
6291
  this.calendar = new Calendar(this.dir);
5763
- this.icons = new ExeIcons(join2(this.dir, "icons"));
6292
+ const routines = new RoutineBook(this.dir);
6293
+ this.routines = routines;
6294
+ this.icons = new ExeIcons(join3(this.dir, "icons"));
5764
6295
  const calendar = this.calendar;
5765
6296
  const changed = (game) => ctx.get("adgGateway")?.broadcast("games/changed", game === void 0 ? {} : { game });
5766
6297
  this.readings = new ReadingTasks(async (key, reading) => {
@@ -5774,6 +6305,7 @@ var AdgGameIntel = class extends Service {
5774
6305
  lastDay: () => calendar.report.lastDay,
5775
6306
  send: async (day) => {
5776
6307
  await this.refresh().catch(() => []);
6308
+ await this.backfill(true);
5777
6309
  await this.sendReport(day);
5778
6310
  },
5779
6311
  log: { warn: (m) => log.warn(m) }
@@ -5781,20 +6313,27 @@ var AdgGameIntel = class extends Service {
5781
6313
  this.daily = daily;
5782
6314
  this.ready = (async () => {
5783
6315
  await calendar.load();
6316
+ await routines.load();
5784
6317
  await this.loadGames(log);
5785
6318
  await this.adoptEvidence().catch((error) => log.warn(`old screenshots not converted: ${String(error)}`));
5786
6319
  await calendar.rederive((game) => this.timezone(game)).catch((error) => log.warn(`calendar not worked out again: ${String(error)}`));
5787
6320
  daily.start();
5788
6321
  })();
5789
6322
  ctx.effect(() => () => this.daily.stop(), "adg-game-intel daily report");
6323
+ ctx.inject(["adgGateway"], (scoped) => {
6324
+ scoped.effect(() => {
6325
+ scoped.adgGateway.skillScope.resolveGames(this.gameIdOf);
6326
+ return () => scoped.adgGateway.skillScope.resolveGames(() => void 0);
6327
+ }, "adg-game-intel game names for skills");
6328
+ });
5790
6329
  ctx.inject(["skills", "adgGateway"], (scoped) => {
5791
6330
  const disposers = [];
5792
6331
  void (async () => {
5793
6332
  await this.ready;
5794
- const guide = parseFrontmatter(await readFile2(SKILL, "utf8"));
6333
+ const guide = parseFrontmatter(await readFile3(SKILL, "utf8"));
5795
6334
  if (guide !== void 0)
5796
6335
  disposers.push(scoped.adgGateway.generalSkill({ ...guide, source: "bundled" }, () => scoped.skills.register({ ...guide, source: "bundled" })));
5797
- const web = parseFrontmatter(await readFile2(WEB_SKILL, "utf8"));
6336
+ const web = parseFrontmatter(await readFile3(WEB_SKILL, "utf8"));
5798
6337
  if (web !== void 0)
5799
6338
  disposers.push(scoped.adgGateway.generalSkill({ ...web, source: "bundled" }, () => scoped.skills.register({ ...web, source: "bundled" })));
5800
6339
  for (const game of this.games) {
@@ -5807,7 +6346,7 @@ var AdgGameIntel = class extends Service {
5807
6346
  }
5808
6347
  try {
5809
6348
  const owner = { games: [game.manifest.id, game.manifest.name] };
5810
- disposers.push(scoped.adgGateway.skillScope.register({ ...skill, source: game.builtin ? "bundled" : "custom", path: join2(game.dir, GAME_GUIDE_FILE), resourceBase: { kind: "directory", path: game.dir }, owner, kind: "game-pack" }));
6349
+ disposers.push(scoped.adgGateway.skillScope.register({ ...skill, source: game.builtin ? "bundled" : "custom", path: join3(game.dir, GAME_GUIDE_FILE), resourceBase: { kind: "directory", path: game.dir }, owner, kind: "game-pack" }));
5811
6350
  } catch (error) {
5812
6351
  log.warn(`game ${game.manifest.id}: skill not registered: ${error instanceof Error ? error.message : String(error)}`);
5813
6352
  }
@@ -5828,6 +6367,22 @@ var AdgGameIntel = class extends Service {
5828
6367
  await this.ready;
5829
6368
  return this.recordNotices(game, notices);
5830
6369
  },
6370
+ routines: (game) => this.routineStatus(game),
6371
+ setRoutine: async (game, routine, done, evidence) => {
6372
+ await this.ready;
6373
+ return this.setRoutine(game, routine, done, "agent", evidence);
6374
+ },
6375
+ addRoutine: async (game, name, period) => {
6376
+ await this.ready;
6377
+ return this.addRoutine(game, name, period);
6378
+ },
6379
+ recordPools: async (game, pools) => {
6380
+ await this.ready;
6381
+ this.game(game);
6382
+ await calendar.recordPools(game, pools, this.clock.now());
6383
+ this.changed(game);
6384
+ return calendar.pools(game, this.clock.now()).length;
6385
+ },
5831
6386
  refresh: async (game) => {
5832
6387
  await this.ready;
5833
6388
  return this.refresh(game);
@@ -5881,6 +6436,19 @@ var AdgGameIntel = class extends Service {
5881
6436
  dispose();
5882
6437
  };
5883
6438
  }));
6439
+ ctx.inject(["adgRunner"], (scoped) => {
6440
+ scoped.effect(() => scoped.adgRunner.runner.watch({
6441
+ line: (run, line2) => this.routineLine(run, line2),
6442
+ state: (run) => this.routineRun(run)
6443
+ }), "adg-game-intel routines");
6444
+ void this.ready.then(() => this.backfill(true));
6445
+ });
6446
+ ctx.inject(["adgScripts"], (scoped) => {
6447
+ scoped.effect(() => scoped.adgScripts.library.onPacksChanged(() => {
6448
+ this.runHits.clear();
6449
+ void this.ready.then(() => this.backfill(true));
6450
+ }), "adg-game-intel routines on pack changes");
6451
+ });
5884
6452
  ctx.inject(["adgGateway"], (scoped) => {
5885
6453
  scoped.effect(() => scoped.adgGateway.role(GAME_ROLE), "adg-game-intel role");
5886
6454
  scoped.adgGateway.register({
@@ -5890,6 +6458,7 @@ var AdgGameIntel = class extends Service {
5890
6458
  "games/list": async () => {
5891
6459
  await this.ready;
5892
6460
  await this.loadGames(log);
6461
+ void this.backfill();
5893
6462
  const shown = new Set(this.games.map((g) => g.manifest.id));
5894
6463
  return { games: this.infos(), activities: calendar.list().filter((a) => shown.has(a.game)), notices: calendar.notices().filter((n) => shown.has(n.game)), settings: calendar.settings, report: { ...calendar.report, ...this.daily.next() === void 0 ? {} : { next: this.daily.next() } } };
5895
6464
  },
@@ -5942,13 +6511,23 @@ var AdgGameIntel = class extends Service {
5942
6511
  throw new Error("\u8FD9\u4E2A\u6D3B\u52A8\u6CA1\u6709\u4FDD\u5B58\u622A\u56FE");
5943
6512
  if (screen.image !== void 0)
5944
6513
  return this.picture(screen.image);
5945
- return { mediaType: "image/png", data: (await readFile2(this.evidenceFile(id))).toString("base64") };
6514
+ return { mediaType: "image/png", data: (await readFile3(this.evidenceFile(id))).toString("base64") };
5946
6515
  },
5947
6516
  "games/icon": async (params, call) => {
5948
6517
  await this.ready;
5949
6518
  const icon = await this.icon(params.game, call.signal);
5950
6519
  return icon === null ? {} : { icon };
5951
6520
  },
6521
+ "games/android/locate": async (params, call) => {
6522
+ await this.ready;
6523
+ const found = await this.locateAndroid(params.scriptId, call.signal);
6524
+ changed();
6525
+ return found;
6526
+ },
6527
+ "games/android/app": async (params) => {
6528
+ const info = await ctx.get("adgEmulators")?.appInfo?.(params.package, false);
6529
+ return { ...info?.label === void 0 ? {} : { label: info.label }, ...info?.icon === void 0 ? {} : { icon: info.icon } };
6530
+ },
5952
6531
  "games/image": async (params) => {
5953
6532
  await this.ready;
5954
6533
  return this.picture(params.url);
@@ -6007,6 +6586,31 @@ var AdgGameIntel = class extends Service {
6007
6586
  await this.showSra(sra, follow);
6008
6587
  return {};
6009
6588
  },
6589
+ "games/routine/set": async (params) => {
6590
+ await this.ready;
6591
+ const p = params;
6592
+ await this.setRoutine(p.game, p.routine, p.done, "user");
6593
+ return {};
6594
+ },
6595
+ "games/routine/add": async (params) => {
6596
+ await this.ready;
6597
+ const p = params;
6598
+ const { manifest } = this.game(p.game);
6599
+ const own = await this.routines.addOwn(p.game, p.name, p.period, this.clock.now());
6600
+ changed(p.game);
6601
+ return { routine: this.routineInfo(manifest, this.scriptsOf(p.game)).routines.find((r) => r.id === own.id) };
6602
+ },
6603
+ "games/routine/remove": async (params) => {
6604
+ await this.ready;
6605
+ const p = params;
6606
+ this.game(p.game);
6607
+ if (p.restore === true)
6608
+ await this.routines.restore(p.game);
6609
+ else if (p.routine !== void 0)
6610
+ await this.routines.remove(p.game, p.routine);
6611
+ changed(p.game);
6612
+ return {};
6613
+ },
6010
6614
  "games/activity/progress": async (params) => {
6011
6615
  await this.ready;
6012
6616
  const { activityId, ...patch } = params;
@@ -6052,7 +6656,7 @@ var AdgGameIntel = class extends Service {
6052
6656
  if (OWN_IMAGE.test(url) || SHOT_IMAGE.test(url)) {
6053
6657
  const [kind, file] = url.split(":");
6054
6658
  const mediaType2 = IMAGE_TYPES.find(([ext2]) => file.endsWith(`.${ext2}`))[1];
6055
- const bytes2 = await readFile2(join2(this.imageDir(kind), file)).catch(() => {
6659
+ const bytes2 = await readFile3(join3(this.imageDir(kind), file)).catch(() => {
6056
6660
  throw new Error("\u8FD9\u5F20\u56FE\u7247\u5DF2\u7ECF\u4E0D\u5728\u4E86");
6057
6661
  });
6058
6662
  return { mediaType: mediaType2, data: bytes2.toString("base64") };
@@ -6060,9 +6664,9 @@ var AdgGameIntel = class extends Service {
6060
6664
  if (!this.calendar.pictures().has(url) && !this.games.some((g) => g.manifest.art === url))
6061
6665
  throw new Error("\u8FD9\u5F20\u56FE\u7247\u4E0D\u662F\u6570\u636E\u7AD9\u6216\u6E38\u620F\u5305\u7ED9\u7684\u5730\u5740");
6062
6666
  const name = createHash2("sha256").update(url).digest("hex").slice(0, 32);
6063
- const dir = join2(this.dir, "images");
6667
+ const dir = join3(this.dir, "images");
6064
6668
  for (const [ext2, mediaType2] of IMAGE_TYPES) {
6065
- const cached = await readFile2(join2(dir, `${name}.${ext2}`)).catch(() => void 0);
6669
+ const cached = await readFile3(join3(dir, `${name}.${ext2}`)).catch(() => void 0);
6066
6670
  if (cached !== void 0)
6067
6671
  return { mediaType: mediaType2, data: cached.toString("base64") };
6068
6672
  }
@@ -6077,7 +6681,7 @@ var AdgGameIntel = class extends Service {
6077
6681
  if (bytes.length > MAX_IMAGE_BYTES)
6078
6682
  throw new Error("\u56FE\u7247\u592A\u5927");
6079
6683
  await mkdir(dir, { recursive: true });
6080
- await writeFile(join2(dir, `${name}.${ext}`), bytes);
6684
+ await writeFile(join3(dir, `${name}.${ext}`), bytes);
6081
6685
  return { mediaType, data: bytes.toString("base64") };
6082
6686
  }
6083
6687
  // ── games ──
@@ -6091,20 +6695,20 @@ var AdgGameIntel = class extends Service {
6091
6695
  continue;
6092
6696
  }
6093
6697
  for (const id of entries) {
6094
- const dir = join2(base, id);
6698
+ const dir = join3(base, id);
6095
6699
  let text;
6096
6700
  try {
6097
- text = await readFile2(join2(dir, GAME_MANIFEST_FILE), "utf8");
6701
+ text = await readFile3(join3(dir, GAME_MANIFEST_FILE), "utf8");
6098
6702
  } catch {
6099
6703
  continue;
6100
6704
  }
6101
6705
  try {
6102
6706
  const manifest = parseGameManifest(text, `${id}/${GAME_MANIFEST_FILE}`);
6103
6707
  for (const source of manifest.sources) {
6104
- if (!source.steam && !source.sra && !source.smw)
6708
+ if (!source.steam && !source.sra && !source.smw && !givesPools(source))
6105
6709
  source.steam = steamNewsSource(source.homepage);
6106
6710
  }
6107
- const guide = await readFile2(join2(dir, GAME_GUIDE_FILE), "utf8").catch(() => void 0);
6711
+ const guide = await readFile3(join3(dir, GAME_GUIDE_FILE), "utf8").catch(() => void 0);
6108
6712
  found.set(manifest.id, { manifest, dir, builtin, ...builtin ? {} : { own: true }, ...guide === void 0 ? {} : { guide } });
6109
6713
  } catch (error) {
6110
6714
  log.warn(`game pack ${id}: ${error instanceof Error ? error.message : String(error)}`);
@@ -6125,7 +6729,16 @@ var AdgGameIntel = class extends Service {
6125
6729
  const links = this.calendar.links(g.manifest.id);
6126
6730
  return links.length === 0 ? g : { ...g, manifest: { ...g.manifest, sources: [...g.manifest.sources, ...links.map((l) => ({ id: l.id, name: l.name, homepage: l.url }))] } };
6127
6731
  });
6732
+ this.ctx.get("adgGateway")?.skillScope.resolveGames(this.gameIdOf);
6128
6733
  }
6734
+ /** The game a key names, by its id, name or an alias (any case); undefined when no game is known by it. */
6735
+ gameIdOf = (key) => {
6736
+ const k = key.trim().toLowerCase();
6737
+ if (k === "")
6738
+ return void 0;
6739
+ const named = (g) => [g.manifest.id, g.manifest.name, ...g.manifest.aliases].some((n) => n.toLowerCase() === k);
6740
+ return (this.games.find(named) ?? this.allGames.find(named))?.manifest.id;
6741
+ };
6129
6742
  /** Keep a page about a game as a source and have the agent read it now. @returns its id and the reading session. */
6130
6743
  async addLink(gameId, url, name) {
6131
6744
  this.game(gameId);
@@ -6181,7 +6794,7 @@ var AdgGameIntel = class extends Service {
6181
6794
  throw new Error(`\u627E\u4E0D\u5230\u56FE\u7247\u6587\u4EF6\uFF1A${full}`);
6182
6795
  if (info.size > MAX_IMAGE_BYTES)
6183
6796
  throw new Error("\u56FE\u7247\u592A\u5927\uFF08\u6700\u591A 8 MB\uFF09");
6184
- return readFile2(full);
6797
+ return readFile3(full);
6185
6798
  }
6186
6799
  timezone(game) {
6187
6800
  return this.games.find((g) => g.manifest.id === game)?.manifest.timezone ?? "Asia/Shanghai";
@@ -6244,7 +6857,7 @@ var AdgGameIntel = class extends Service {
6244
6857
  const path = from.replace(/^file:\/\/\/?/i, "");
6245
6858
  const workspace = this.ctx.get("adgGateway")?.workspaceDir();
6246
6859
  const full = isAbsolute(path) || workspace === void 0 ? path : resolve(workspace, path);
6247
- return readFile2(full).catch(() => {
6860
+ return readFile3(full).catch(() => {
6248
6861
  throw new Error(`\u8BFB\u4E0D\u5230\u56FE\u7247\u6587\u4EF6\uFF1A${full}`);
6249
6862
  });
6250
6863
  }
@@ -6261,18 +6874,18 @@ var AdgGameIntel = class extends Service {
6261
6874
  const file = `${createHash2("sha256").update(bytes).digest("hex").slice(0, 32)}.${ext}`;
6262
6875
  const dir = this.imageDir(kind);
6263
6876
  await mkdir(dir, { recursive: true });
6264
- await writeFile(join2(dir, file), bytes);
6877
+ await writeFile(join3(dir, file), bytes);
6265
6878
  return `${kind}:${file}`;
6266
6879
  }
6267
6880
  imageDir(kind) {
6268
- return join2(this.dir, "images", kind === "own" ? "own" : "shots");
6881
+ return join3(this.dir, "images", kind === "own" ? "own" : "shots");
6269
6882
  }
6270
6883
  /** Pictures the user added, and screenshots, that no event has any more. */
6271
6884
  async sweepImages() {
6272
6885
  for (const [kind, used] of [["own", this.calendar.ownImages()], ["shot", this.calendar.shotImages()]]) {
6273
6886
  const dir = this.imageDir(kind);
6274
6887
  const files = await readdir(dir).catch(() => []);
6275
- await Promise.all(files.filter((f) => !used.has(`${kind}:${f}`)).map((f) => rm(join2(dir, f), { force: true }).catch(() => {
6888
+ await Promise.all(files.filter((f) => !used.has(`${kind}:${f}`)).map((f) => rm(join3(dir, f), { force: true }).catch(() => {
6276
6889
  })));
6277
6890
  }
6278
6891
  }
@@ -6330,8 +6943,17 @@ var AdgGameIntel = class extends Service {
6330
6943
  const mine = new Set([...byName, ...byDescription].filter(([, ids]) => ids.includes(gameId)).map(([rid]) => rid));
6331
6944
  return { items: items.filter((i) => mine.has(i.rid)), from };
6332
6945
  }
6333
- /** A game's program icon, for its avatar; null when it has no PC program this app knows of. */
6946
+ /**
6947
+ * A game's icon, for its avatar: its PC program's, else its app's in the
6948
+ * emulator (a game played there first: `screen.target: emulator`); null when neither is known.
6949
+ */
6334
6950
  async icon(gameId, signal) {
6951
+ const phoneFirst = this.games.find((g) => g.manifest.id === gameId)?.manifest.screen?.target === "emulator";
6952
+ if (phoneFirst)
6953
+ return await this.androidIcon(gameId) ?? this.pcIcon(gameId, signal);
6954
+ return await this.pcIcon(gameId, signal) ?? this.androidIcon(gameId);
6955
+ }
6956
+ async pcIcon(gameId, signal) {
6335
6957
  const program = await this.program(gameId);
6336
6958
  if (program === void 0)
6337
6959
  return null;
@@ -6342,6 +6964,92 @@ var AdgGameIntel = class extends Service {
6342
6964
  return null;
6343
6965
  }
6344
6966
  }
6967
+ /**
6968
+ * The icon of the game's app in the emulator: one read before (on the
6969
+ * script page's 在模拟器里查找, or earlier here), else read now when the
6970
+ * emulator is already up; it is never started for an icon.
6971
+ */
6972
+ async androidIcon(gameId) {
6973
+ const emulators = this.ctx.get("adgEmulators");
6974
+ if (emulators?.appInfo === void 0)
6975
+ return null;
6976
+ const packages = await this.androidPackages(gameId);
6977
+ for (const pkg of packages) {
6978
+ const icon = (await emulators.appInfo(pkg, false).catch(() => void 0))?.icon;
6979
+ if (icon !== void 0)
6980
+ return icon;
6981
+ }
6982
+ for (const pkg of packages.slice(0, 6)) {
6983
+ const icon = (await emulators.appInfo(pkg, true).catch(() => void 0))?.icon;
6984
+ if (icon !== void 0)
6985
+ return icon;
6986
+ }
6987
+ return null;
6988
+ }
6989
+ /** A game's Android packages: what its scripts start (saved on the script page, or their packs'), then the game pack's builds. */
6990
+ async androidPackages(gameId) {
6991
+ const runner = this.ctx.get("adgRunner")?.runner;
6992
+ const library = this.ctx.get("adgScripts")?.library;
6993
+ const out = [];
6994
+ const add = (pkg) => {
6995
+ if (pkg !== void 0 && !out.includes(pkg))
6996
+ out.push(pkg);
6997
+ };
6998
+ const scripts = this.scriptsOf(gameId);
6999
+ for (const id of scripts)
7000
+ add((await runner?.launch.get(id).catch(() => void 0))?.androidPackage);
7001
+ for (const id of scripts) {
7002
+ let pack;
7003
+ try {
7004
+ pack = library?.packOf(id) ?? library?.pack(id);
7005
+ } catch {
7006
+ pack = void 0;
7007
+ }
7008
+ add(pack?.manifest.game?.android?.package);
7009
+ }
7010
+ for (const p of this.games.find((g) => g.manifest.id === gameId)?.manifest.android?.packages ?? [])
7011
+ add(p.package);
7012
+ return out;
7013
+ }
7014
+ /**
7015
+ * Which installed app in the emulator is the game a script plays: hints from
7016
+ * the script's pack (game.name, game.android.package), its saved package,
7017
+ * and the game packs that list the script (names, aliases, android.packages:
7018
+ * one per client, like MAA's 客户端类型).
7019
+ */
7020
+ async locateAndroid(scriptId, signal) {
7021
+ const emulators = this.ctx.get("adgEmulators");
7022
+ if (emulators?.findApps === void 0)
7023
+ throw new Error("\u6A21\u62DF\u5668\u63D2\u4EF6\u6CA1\u6709\u52A0\u8F7D");
7024
+ const library = this.ctx.get("adgScripts")?.library;
7025
+ let pack;
7026
+ try {
7027
+ pack = library?.packOf(scriptId)?.manifest;
7028
+ } catch {
7029
+ pack = void 0;
7030
+ }
7031
+ const settings = await this.ctx.get("adgRunner")?.runner.launch.get(scriptId).catch(() => void 0);
7032
+ const games = this.games.filter((g) => this.scriptsOf(g.manifest.id).includes(scriptId)).map((g) => g.manifest);
7033
+ const packages = [];
7034
+ const add = (p) => {
7035
+ const same = packages.find((q) => q.package.toLowerCase() === p.package.toLowerCase());
7036
+ if (same === void 0)
7037
+ packages.push({ package: p.package, source: p.source, ...p.client === void 0 ? {} : { client: p.client } });
7038
+ else if (same.client === void 0 && p.client !== void 0)
7039
+ same.client = p.client;
7040
+ };
7041
+ for (const g of games)
7042
+ for (const p of g.android?.packages ?? [])
7043
+ add({ package: p.package, client: p.client, source: `\u300C${g.name}\u300D\u8D44\u6599\u91CC\u7684${p.client ?? ""}\u5305\u540D` });
7044
+ if (pack?.game?.android?.package !== void 0)
7045
+ add({ package: pack.game.android.package, source: "\u811A\u672C\u5305\u5199\u7684\u5305\u540D" });
7046
+ if (settings?.androidPackage !== void 0)
7047
+ add({ package: settings.androidPackage, source: "\u4E4B\u524D\u4FDD\u5B58\u7684\u5305\u540D" });
7048
+ const names = [...new Set([pack?.game?.name, ...games.flatMap((g) => [g.name, ...g.aliases])].filter((n) => n !== void 0 && n.trim() !== ""))];
7049
+ if (packages.length === 0 && names.length === 0)
7050
+ throw new Error("\u4E0D\u77E5\u9053\u8FD9\u4E2A\u811A\u672C\u73A9\u7684\u662F\u54EA\u4E2A\u6E38\u620F\uFF1A\u811A\u672C\u5305\u6CA1\u6709\u5199 game\uFF0C\u6E38\u620F\u9875\u4E5F\u6CA1\u6709\u5173\u8054\u5B83");
7051
+ return emulators.findApps({ packages, names }, signal);
7052
+ }
6345
7053
  /**
6346
7054
  * Where a game's PC program is: the one a script that plays it starts (its
6347
7055
  * saved 游戏位置), else where 自动查找 finds the program the packs that play
@@ -6405,6 +7113,250 @@ var AdgGameIntel = class extends Service {
6405
7113
  throw new Error(`\u6CA1\u6709\u6E38\u620F\u300C${id}\u300D`);
6406
7114
  return game;
6407
7115
  }
7116
+ // ── 日常 / 周常 ──
7117
+ /**
7118
+ * The packs of the scripts that play a game (pack ids or library script ids), once each, with their names.
7119
+ * A user pack keeps the routines of the official one it replaced (withOfficialRoutines).
7120
+ */
7121
+ scriptPacks(ids) {
7122
+ const library = this.ctx.get("adgScripts")?.library;
7123
+ if (library === void 0)
7124
+ return [];
7125
+ const names = new Map(library.list().scripts.map((s) => [s.id, s.name]));
7126
+ const out = /* @__PURE__ */ new Map();
7127
+ for (const id of ids) {
7128
+ let pack;
7129
+ try {
7130
+ pack = library.packOf(id);
7131
+ } catch {
7132
+ pack = void 0;
7133
+ }
7134
+ pack ??= library.pack(id);
7135
+ if (pack === void 0 || out.has(pack.manifest.id))
7136
+ continue;
7137
+ out.set(pack.manifest.id, { id: pack.manifest.id, name: names.get(id) ?? pack.manifest.name, manifest: withOfficialRoutines(pack.manifest, pack.over?.manifest) });
7138
+ }
7139
+ return [...out.values()];
7140
+ }
7141
+ routineDefs(manifest, scripts) {
7142
+ return gameRoutines(manifest, this.scriptPacks(scripts), this.routines.own(manifest.id), this.routines.hidden(manifest.id));
7143
+ }
7144
+ /** A game's routines as the page shows them, with what is done in the current period. */
7145
+ routineInfo(m, scripts) {
7146
+ const now = this.clock.now();
7147
+ const resets = resetsOf(m, now);
7148
+ const activities = this.calendar.list(m.id);
7149
+ const hidden = this.routines.hidden(m.id).length;
7150
+ const routines = this.routineDefs(m, scripts).map((def) => {
7151
+ const events = routineEvents(def, activities, now);
7152
+ const period = periodKey(def, resets, events, now);
7153
+ let done = period === void 0 ? void 0 : this.routines.done(m.id, def.id, period.key);
7154
+ if (def.period === "event") {
7155
+ const running = events.find((a) => (a.startsAt ?? 0) <= now);
7156
+ done = running?.finished === void 0 ? void 0 : done ?? { at: running.finished.at, by: "user" };
7157
+ }
7158
+ if (done !== void 0)
7159
+ done = { at: done.at, by: done.by, ...done.detail === void 0 ? {} : { detail: done.detail }, ...done.script === void 0 ? {} : { script: done.script }, ...done.runId === void 0 ? {} : { runId: done.runId } };
7160
+ return {
7161
+ id: def.id,
7162
+ name: def.name,
7163
+ period: def.period,
7164
+ from: def.from,
7165
+ links: def.links.flatMap((l) => l.text),
7166
+ ...def.description === void 0 ? {} : { description: def.description },
7167
+ ...done === void 0 ? {} : { done },
7168
+ ...period?.end === void 0 ? {} : { resetsAt: period.end },
7169
+ ...def.period === "event" ? { activities: events.map((a) => a.id) } : {}
7170
+ };
7171
+ });
7172
+ return { routines, resets: { daily: resets.daily.end, weekly: resets.weekly.end }, ...hidden === 0 ? {} : { hiddenRoutines: hidden } };
7173
+ }
7174
+ /** A game's routines as the agent reads them (game_routine), after looking through the period's runs. */
7175
+ async routineStatus(gameId) {
7176
+ await this.ready;
7177
+ const { manifest } = this.game(gameId);
7178
+ await this.backfill(true);
7179
+ const info = this.routineInfo(manifest, this.scriptsOf(gameId));
7180
+ return { game: gameId, routines: info.routines ?? [], resets: info.resets };
7181
+ }
7182
+ /**
7183
+ * Tick a routine off for the current period, or take it back: the person on
7184
+ * the game page (`user`), or an agent that saw it done (`agent`, with what it
7185
+ * saw). Found by id or by name.
7186
+ */
7187
+ /** A hand-ticked 日常 / 周常 of the game's own, added by an agent (game_routine add; the page has no way to add one by hand). */
7188
+ async addRoutine(gameId, name, period) {
7189
+ const { manifest } = this.game(gameId);
7190
+ const same = this.routineDefs(manifest, this.scriptsOf(gameId)).find((r) => r.name === name.trim());
7191
+ if (same !== void 0)
7192
+ throw new Error(`\u5DF2\u7ECF\u6709\u300C${same.name}\u300D\uFF08${same.id}\uFF09\u4E86\uFF0C\u4E0D\u7528\u518D\u52A0`);
7193
+ const own = await this.routines.addOwn(gameId, name.trim(), period, this.clock.now());
7194
+ this.changed(gameId);
7195
+ return this.routineInfo(manifest, this.scriptsOf(gameId)).routines.find((r) => r.id === own.id);
7196
+ }
7197
+ async setRoutine(gameId, routine, done, by = "user", detail) {
7198
+ const { manifest } = this.game(gameId);
7199
+ const defs = this.routineDefs(manifest, this.scriptsOf(gameId));
7200
+ const def = defs.find((r) => r.id === routine) ?? defs.find((r) => r.name === routine.trim());
7201
+ if (def === void 0)
7202
+ throw new Error(`\u6CA1\u6709\u65E5\u5E38\u300C${routine}\u300D\uFF1B\u6709\uFF1A${defs.map((r) => `${r.name}\uFF08${r.id}\uFF09`).join("\u3001") || "\u6CA1\u6709"}`);
7203
+ const now = this.clock.now();
7204
+ const events = routineEvents(def, this.calendar.list(gameId), now);
7205
+ const period = periodKey(def, resetsOf(manifest, now), events, now);
7206
+ if (period === void 0)
7207
+ throw new Error(`\u300C${def.name}\u300D\u73B0\u5728\u6CA1\u6709\u6B63\u5728\u8FDB\u884C\u7684\u4E00\u671F`);
7208
+ if (done) {
7209
+ await this.routines.unmark(gameId, def.id);
7210
+ await this.routines.mark(gameId, def.id, { key: period.key, at: now, by, ...detail === void 0 || detail.trim() === "" ? {} : { detail: detail.trim().slice(0, 200) } });
7211
+ } else
7212
+ await this.routines.unmark(gameId, def.id);
7213
+ const running = events.find((a) => (a.startsAt ?? 0) <= now);
7214
+ if (def.period === "event" && running !== void 0)
7215
+ await this.calendar.progress(running.id, { finished: done }, now);
7216
+ this.changed(gameId);
7217
+ return this.routineInfo(manifest, this.scriptsOf(gameId)).routines.find((r) => r.id === def.id);
7218
+ }
7219
+ /** The routines a run can tick off, worked out once per run: of the games its script plays, its pack's links and the games' own. */
7220
+ runHits = /* @__PURE__ */ new Map();
7221
+ runsDone = /* @__PURE__ */ new Set();
7222
+ ticking = Promise.resolve();
7223
+ routineHits(run) {
7224
+ const known = this.runHits.get(run.id);
7225
+ if (known !== void 0)
7226
+ return known;
7227
+ const hits = this.findHits(run);
7228
+ if (this.games.length > 0)
7229
+ this.runHits.set(run.id, hits);
7230
+ return hits;
7231
+ }
7232
+ findHits(run) {
7233
+ const hits = [];
7234
+ let packId;
7235
+ try {
7236
+ packId = this.ctx.get("adgScripts")?.library.packOf(run.scriptId)?.manifest.id;
7237
+ } catch {
7238
+ packId = void 0;
7239
+ }
7240
+ const byGame = this.scriptsByGame();
7241
+ for (const { manifest } of this.games) {
7242
+ const scripts = byGame.get(manifest.id) ?? manifest.scripts;
7243
+ if (!scripts.includes(run.scriptId) && (packId === void 0 || !scripts.includes(packId)))
7244
+ continue;
7245
+ for (const def of this.routineDefs(manifest, scripts)) {
7246
+ for (const link2 of def.links)
7247
+ if (link2.script === void 0 || link2.script === packId)
7248
+ hits.push({ game: manifest.id, def, link: link2 });
7249
+ }
7250
+ }
7251
+ return hits;
7252
+ }
7253
+ routineLine(run, line2) {
7254
+ if (run.kind !== "task" || line2.source === "adg")
7255
+ return;
7256
+ for (const hit of this.routineHits(run)) {
7257
+ if (hit.link.logs.some((re) => re.test(line2.text)))
7258
+ void this.tick(hit.game, hit.def, { at: line2.at, by: "log", detail: line2.text.trim().slice(0, 200), script: run.scriptName, runId: run.id });
7259
+ }
7260
+ }
7261
+ routineRun(run) {
7262
+ if (run.endedAt === void 0 || this.runsDone.has(run.id))
7263
+ return;
7264
+ this.runsDone.add(run.id);
7265
+ if (run.kind === "task" && run.state === "succeeded") {
7266
+ for (const hit of this.routineHits(run)) {
7267
+ if (hit.link.tasks.includes(run.taskId))
7268
+ void this.tick(hit.game, hit.def, { at: run.endedAt, by: "task", detail: run.taskTitle, script: run.scriptName, runId: run.id });
7269
+ }
7270
+ }
7271
+ setTimeout(() => {
7272
+ this.runHits.delete(run.id);
7273
+ this.runsDone.delete(run.id);
7274
+ }, 6e4).unref?.();
7275
+ }
7276
+ /**
7277
+ * Tick a routine off for its current period (one at a time: the book is one
7278
+ * file). What happened before the period began (yesterday's log line, last
7279
+ * week's run) does not count. @returns whether it was ticked off now.
7280
+ */
7281
+ tick(gameId, def, done) {
7282
+ const result = this.ticking.then(async () => {
7283
+ await this.ready;
7284
+ const { manifest } = this.game(gameId);
7285
+ const now = this.clock.now();
7286
+ const events = routineEvents(def, this.calendar.list(gameId), now);
7287
+ const period = periodKey(def, resetsOf(manifest, now), events, now);
7288
+ if (period === void 0 || done.at < period.start || !await this.routines.mark(gameId, def.id, { key: period.key, ...done }))
7289
+ return false;
7290
+ const running = events.find((a) => (a.startsAt ?? 0) <= now);
7291
+ if (def.period === "event" && running !== void 0 && running.finished === void 0)
7292
+ await this.calendar.progress(running.id, { finished: true }, now);
7293
+ this.changed(gameId);
7294
+ return true;
7295
+ }).catch((error) => {
7296
+ this.log.warn(`routine ${def.id} of ${gameId} not ticked off: ${error instanceof Error ? error.message : String(error)}`);
7297
+ return false;
7298
+ });
7299
+ this.ticking = result;
7300
+ return result;
7301
+ }
7302
+ /** Runs already looked through, with the links they were read for (read again when those change). */
7303
+ scanned = /* @__PURE__ */ new Map();
7304
+ backfilling;
7305
+ backfilledAt = 0;
7306
+ /**
7307
+ * Tick off what the period's saved runs show: runs that ended before a
7308
+ * routine or its log pattern existed (an agent rewrote the pack), or before
7309
+ * this version. Each run is read once per set of links; `force` skips the
7310
+ * once-a-minute limit. @returns how many were ticked off.
7311
+ */
7312
+ backfill(force = false) {
7313
+ if (this.backfilling !== void 0)
7314
+ return this.backfilling;
7315
+ if (!force && this.clock.now() - this.backfilledAt < 6e4)
7316
+ return Promise.resolve(0);
7317
+ this.backfilling = (async () => {
7318
+ await this.ready;
7319
+ const runner = this.ctx.get("adgRunner")?.runner;
7320
+ if (runner === void 0 || this.games.length === 0)
7321
+ return 0;
7322
+ const since = this.clock.now() - 42 * 24 * 36e5;
7323
+ let ticked = 0;
7324
+ for (const run of runner.list().filter((r) => r.kind === "task" && r.endedAt !== void 0 && r.startedAt >= since).reverse()) {
7325
+ const hits = this.findHits(run);
7326
+ if (hits.length === 0)
7327
+ continue;
7328
+ const signature = hits.map((h) => `${h.game}/${h.def.id}:${h.link.logs.map((r) => r.source).join("|")}:${h.link.tasks.join(",")}`).join(";");
7329
+ if (this.scanned.get(run.id) === signature)
7330
+ continue;
7331
+ if (run.state === "succeeded") {
7332
+ for (const h of hits)
7333
+ if (h.link.tasks.includes(run.taskId))
7334
+ ticked += Number(await this.tick(h.game, h.def, { at: run.endedAt, by: "task", detail: run.taskTitle, script: run.scriptName, runId: run.id }));
7335
+ }
7336
+ const patterns = [...new Set(hits.flatMap((h) => h.link.logs.map((re) => re.source)))];
7337
+ if (patterns.length > 0) {
7338
+ const read = await runner.readLog(run.id, { search: `/${patterns.join("|")}/`, limit: 2e3 }).catch(() => void 0);
7339
+ for (const line2 of read?.lines ?? []) {
7340
+ if (line2.source === "adg")
7341
+ continue;
7342
+ for (const h of hits) {
7343
+ if (h.link.logs.some((re) => re.test(line2.text)))
7344
+ ticked += Number(await this.tick(h.game, h.def, { at: line2.at, by: "log", detail: line2.text.trim().slice(0, 200), script: run.scriptName, runId: run.id }));
7345
+ }
7346
+ }
7347
+ }
7348
+ this.scanned.set(run.id, signature);
7349
+ }
7350
+ return ticked;
7351
+ })().catch((error) => {
7352
+ this.log.warn(`routines not filled in from past runs: ${error instanceof Error ? error.message : String(error)}`);
7353
+ return 0;
7354
+ }).finally(() => {
7355
+ this.backfilling = void 0;
7356
+ this.backfilledAt = this.clock.now();
7357
+ });
7358
+ return this.backfilling;
7359
+ }
6408
7360
  infos() {
6409
7361
  const scripts = this.scriptsByGame();
6410
7362
  return this.games.map(({ manifest: m, builtin, followed, own }) => {
@@ -6422,7 +7374,7 @@ var AdgGameIntel = class extends Service {
6422
7374
  scripts: scripts.get(m.id) ?? m.scripts,
6423
7375
  timezone: m.timezone,
6424
7376
  ...m.screen === void 0 ? {} : { screen: { target: m.screen.target, ...m.screen.window === void 0 ? {} : { window: m.screen.window } } },
6425
- sources: m.sources.map((s) => ({ id: s.id, name: s.name, ...s.homepage === void 0 ? {} : { homepage: s.homepage }, auto: readsItself(s), .../^link-\d+$/.test(s.id) && this.calendar.links(m.id).some((l) => l.id === s.id) ? { added: true } : {} })),
7377
+ sources: [...m.sources, ...poolSources(m).filter((s) => !m.sources.includes(s))].map((s) => ({ id: s.id, name: s.name, ...s.homepage === void 0 ? {} : { homepage: s.homepage }, auto: readsItself(s), .../^link-\d+$/.test(s.id) && this.calendar.links(m.id).some((l) => l.id === s.id) ? { added: true } : {} })),
6426
7378
  builtin,
6427
7379
  ...m.art === void 0 ? {} : { art: m.art },
6428
7380
  ...banner === void 0 ? {} : { banner },
@@ -6434,7 +7386,8 @@ var AdgGameIntel = class extends Service {
6434
7386
  ...reading === void 0 ? {} : { reading },
6435
7387
  ...webReading === void 0 ? {} : { webReading },
6436
7388
  ...version === void 0 ? {} : { version },
6437
- ...pools.length === 0 ? {} : { pools }
7389
+ ...pools.length === 0 ? {} : { pools },
7390
+ ...m.single === true ? {} : this.routineInfo(m, scripts.get(m.id) ?? m.scripts)
6438
7391
  };
6439
7392
  });
6440
7393
  }
@@ -6455,6 +7408,7 @@ var AdgGameIntel = class extends Service {
6455
7408
  let notices = 0;
6456
7409
  let version;
6457
7410
  let pools;
7411
+ let wikiPools;
6458
7412
  for (const source of sources) {
6459
7413
  signal?.throwIfAborted();
6460
7414
  const now = this.clock.now();
@@ -6463,6 +7417,10 @@ var AdgGameIntel = class extends Service {
6463
7417
  notices += (await this.calendar.applyNotices(manifest.id, { id: source.id, name: source.name }, await readSteam(source.steam, this.fetch, signal), now)).length;
6464
7418
  continue;
6465
7419
  }
7420
+ if (givesPools(source)) {
7421
+ (wikiPools ??= []).push(...await this.readBanners(source, manifest, now, signal));
7422
+ continue;
7423
+ }
6466
7424
  let events;
6467
7425
  if (source.sra !== void 0) {
6468
7426
  const read = await readSra(source.sra, manifest, now, this.fetch, signal);
@@ -6479,6 +7437,19 @@ var AdgGameIntel = class extends Service {
6479
7437
  failures.push(`${source.name}\uFF1A${error instanceof Error ? error.message : String(error)}`);
6480
7438
  }
6481
7439
  }
7440
+ if ((pools?.length ?? 0) === 0 && selected === void 0) {
7441
+ for (const source of poolSources(manifest).filter((s) => !sources.includes(s))) {
7442
+ try {
7443
+ (wikiPools ??= []).push(...await this.readBanners(source, manifest, this.clock.now(), signal));
7444
+ } catch (error) {
7445
+ failures.push(`${source.name}\uFF1A${error instanceof Error ? error.message : String(error)}`);
7446
+ }
7447
+ }
7448
+ }
7449
+ if ((pools?.length ?? 0) === 0 && wikiPools !== void 0)
7450
+ pools = wikiPools;
7451
+ else if ((pools?.length ?? 0) === 0 && poolSources(manifest).length > 0)
7452
+ pools = void 0;
6482
7453
  if (failures.length > 0 && count + notices === 0)
6483
7454
  await this.calendar.refreshFailed(manifest.id, failures.join("\uFF1B"), this.clock.now());
6484
7455
  else
@@ -6489,6 +7460,10 @@ var AdgGameIntel = class extends Service {
6489
7460
  await this.sweepImages();
6490
7461
  return results;
6491
7462
  }
7463
+ /** A banner source (a wiki's SMW data, ennead's miHoYo calendar). */
7464
+ readBanners(source, manifest, now, signal) {
7465
+ return source.pools !== void 0 ? readPools(source.pools, manifest, now, this.fetch, signal) : readHoyo(source.hoyo, now, this.fetch, signal);
7466
+ }
6492
7467
  // ── what the agent read ──
6493
7468
  /** Announcements the agent read (game_notice), each credited to the site it came from. */
6494
7469
  async recordNotices(gameId, notices) {
@@ -6533,7 +7508,7 @@ var AdgGameIntel = class extends Service {
6533
7508
  if (a.screen?.evidence !== true || a.screen.image !== void 0)
6534
7509
  continue;
6535
7510
  const file = this.evidenceFile(a.id);
6536
- const bytes = await readFile2(file).catch(() => void 0);
7511
+ const bytes = await readFile3(file).catch(() => void 0);
6537
7512
  if (bytes === void 0)
6538
7513
  continue;
6539
7514
  const kept = await this.keepImage(bytes, "shot").catch(() => void 0);
@@ -6547,7 +7522,7 @@ var AdgGameIntel = class extends Service {
6547
7522
  }
6548
7523
  /** Where a screenshot was kept before `shot:` pictures. */
6549
7524
  evidenceFile(activityId) {
6550
- return join2(this.dir, "evidence", `${activityId.replace(/[^\p{L}\p{N}-]+/gu, "_")}.png`);
7525
+ return join3(this.dir, "evidence", `${activityId.replace(/[^\p{L}\p{N}-]+/gu, "_")}.png`);
6551
7526
  }
6552
7527
  /** Ask the agent to read the game screen, in a session of its own. @returns the session id. */
6553
7528
  /**
@@ -6575,13 +7550,13 @@ var AdgGameIntel = class extends Service {
6575
7550
  const existing = this.games.find((g) => g.manifest.id === manifest.id);
6576
7551
  if (existing !== void 0 && existing.builtin && existing.followed !== true)
6577
7552
  throw new Error(`\u5DF2\u7ECF\u6709\u6280\u80FD\u5E93\u7684\u6E38\u620F\u5305\u300C${existing.manifest.name}\u300D\uFF08${manifest.id}\uFF09\uFF0C\u6362\u4E00\u4E2A id\uFF0C\u6216\u8005\u76F4\u63A5\u7528\u5B83`);
6578
- const dir = join2(this.dir, manifest.id);
7553
+ const dir = join3(this.dir, manifest.id);
6579
7554
  await mkdir(dir, { recursive: true });
6580
- await writeFile(join2(dir, GAME_MANIFEST_FILE), `# \u7531 agent \u7528 game_add \u5199\u7684\u6E38\u620F\u5305\uFF1B\u53EF\u4EE5\u76F4\u63A5\u6539\uFF08JSON \u4E5F\u662F YAML\uFF09\u3002
7555
+ await writeFile(join3(dir, GAME_MANIFEST_FILE), `# \u7531 agent \u7528 game_add \u5199\u7684\u6E38\u620F\u5305\uFF1B\u53EF\u4EE5\u76F4\u63A5\u6539\uFF08JSON \u4E5F\u662F YAML\uFF09\u3002
6581
7556
  ${JSON.stringify(input, null, 2)}
6582
7557
  `);
6583
7558
  if (guide !== void 0 && guide.trim() !== "") {
6584
- await writeFile(join2(dir, GAME_GUIDE_FILE), `---
7559
+ await writeFile(join3(dir, GAME_GUIDE_FILE), `---
6585
7560
  name: game-${manifest.id}
6586
7561
  description: ${JSON.stringify(`${manifest.name}\u7684\u6D3B\u52A8\u548C\u8D44\u8BAF\uFF1A\u4ECE\u54EA\u91CC\u770B\u3001\u600E\u4E48\u770B\u3002\u7528\u6237\u95EE${manifest.name}\u7684\u6D3B\u52A8\u65F6\u4F7F\u7528\u3002`)}
6587
7562
  ---
@@ -6643,9 +7618,9 @@ ${guide.trim()}
6643
7618
  if (sra !== void 0)
6644
7619
  return this.showSra(sra.sra, true);
6645
7620
  const id = await this.freeId(wanted);
6646
- const dir = join2(this.dir, id);
7621
+ const dir = join3(this.dir, id);
6647
7622
  await mkdir(dir, { recursive: true });
6648
- await writeFile(join2(dir, GAME_MANIFEST_FILE), `# \u5728\u6E38\u620F\u9875\u6309\u540D\u5B57\u6DFB\u52A0\u7684\u6E38\u620F\uFF1A\u6765\u6E90\u7531 agent \u627E\uFF08\u6E38\u620F\u9875\u4E0A\u7684\u300C\u8BA9 agent \u53BB\u627E\u300D\uFF09\uFF0C\u4E5F\u53EF\u4EE5\u76F4\u63A5\u6539\uFF08JSON \u4E5F\u662F YAML\uFF09\u3002
7623
+ await writeFile(join3(dir, GAME_MANIFEST_FILE), `# \u5728\u6E38\u620F\u9875\u6309\u540D\u5B57\u6DFB\u52A0\u7684\u6E38\u620F\uFF1A\u6765\u6E90\u7531 agent \u627E\uFF08\u6E38\u620F\u9875\u4E0A\u7684\u300C\u8BA9 agent \u53BB\u627E\u300D\uFF09\uFF0C\u4E5F\u53EF\u4EE5\u76F4\u63A5\u6539\uFF08JSON \u4E5F\u662F YAML\uFF09\u3002
6649
7624
  ${JSON.stringify({ id, name: wanted, sources: [] }, null, 2)}
6650
7625
  `);
6651
7626
  await this.loadGames(this.log);
@@ -6691,6 +7666,7 @@ ${JSON.stringify({ id, name: wanted, sources: [] }, null, 2)}
6691
7666
  } else if (game.own === true) {
6692
7667
  await rm(game.dir, { recursive: true, force: true });
6693
7668
  await this.calendar.forget(gameId);
7669
+ await this.routines.forget(gameId);
6694
7670
  } else {
6695
7671
  await this.calendar.hide(gameId, true);
6696
7672
  }
@@ -6900,7 +7876,13 @@ ${JSON.stringify({ id, name: wanted, sources: [] }, null, 2)}
6900
7876
  return { title, text };
6901
7877
  }
6902
7878
  reportGames() {
6903
- return this.games.map((g) => ({ id: g.manifest.id, name: g.manifest.name, timezone: g.manifest.timezone }));
7879
+ const scripts = this.scriptsByGame();
7880
+ return this.games.map((g) => ({
7881
+ id: g.manifest.id,
7882
+ name: g.manifest.name,
7883
+ timezone: g.manifest.timezone,
7884
+ ...g.manifest.single === true ? {} : { routines: this.routineInfo(g.manifest, scripts.get(g.manifest.id) ?? g.manifest.scripts).routines }
7885
+ }));
6904
7886
  }
6905
7887
  /**
6906
7888
  * The agent's summary of the recent events (just started, ending soon,
@@ -6922,7 +7904,7 @@ ${JSON.stringify({ id, name: wanted, sources: [] }, null, 2)}
6922
7904
  const work = await conversations.prepareTask(GAME_ROLE.id, `\u65E5\u62A5\uFF1A${local.month}\u6708${local.day}\u65E5`);
6923
7905
  const prompt = [
6924
7906
  "\u628A\u4E0B\u9762\u8FD9\u4E9B\u8FD1\u671F\u7684\u6E38\u620F\u6D3B\u52A8\u5199\u6210\u4ECA\u5929\u53D1\u5230\u98DE\u4E66\u7684\u65E5\u62A5\u6B63\u6587\u3002\u53EA\u6839\u636E\u7ED9\u51FA\u7684\u5185\u5BB9\u5199\uFF0C\u4E0D\u8C03\u7528\u5DE5\u5177\uFF0C\u4E0D\u63D0\u95EE\u3002",
6925
- "\u8981\u6C42\uFF1A\u4E2D\u6587\uFF0C\u6309\u6E38\u620F\u5206\u6BB5\uFF08\u6BB5\u9996\u7528\u3010\u6E38\u620F\u540D\u3011\uFF09\uFF0C\u6BCF\u4E2A\u6E38\u620F\u4E00\u4E24\u53E5\uFF1B\u5148\u8BF4\u5FEB\u7ED3\u675F\u4E14\u6CA1\u505A\u5B8C\u7684\uFF08\u5199\u6E05\u5269\u51E0\u5929\u3001\u8FD8\u5DEE\u4EC0\u4E48\uFF09\uFF0C\u518D\u8BF4\u521A\u5F00\u59CB\u548C\u5373\u5C06\u5F00\u59CB\u7684\uFF1B\u7528\u6237\u5DF2\u505A\u5B8C\u7684\u4E0D\u7528\u63D0\u9192\uFF1B\u4E0D\u7528 Markdown \u6807\u9898\u3001\u52A0\u7C97\u6216\u8868\u683C\uFF1B\u6574\u7BC7\u4E0D\u8D85\u8FC7 12 \u884C\u3002\u8BED\u6C14\u3001\u79F0\u547C\u548C\u81EA\u79F0\u6309\u4F60\u7684\u300C\u56DE\u590D\u98CE\u683C\u300D\u6765\uFF08\u6CA1\u6709\u8BBE\u5B9A\u5C31\u5E73\u5B9E\u5730\u5199\uFF09\uFF1A\u53EF\u4EE5\u7528\u4E00\u53E5\u7B26\u5408\u98CE\u683C\u7684\u8BDD\u5F00\u5934\uFF0C\u5185\u5BB9\u7167\u4E0A\u9762\u7684\u8981\u6C42\uFF0C\u4E0D\u8981\u5BA2\u5957\u7684\u7ED3\u675F\u8BED\u3002",
7907
+ "\u8981\u6C42\uFF1A\u4E2D\u6587\uFF0C\u6309\u6E38\u620F\u5206\u6BB5\uFF08\u6BB5\u9996\u7528\u3010\u6E38\u620F\u540D\u3011\uFF09\uFF0C\u6BCF\u4E2A\u6E38\u620F\u4E00\u4E24\u53E5\uFF1B\u5148\u8BF4\u65E5\u5E38\u3001\u5468\u5E38\u8FD8\u5DEE\u54EA\u51E0\u9879\u3001\u4EC0\u4E48\u65F6\u5019\u91CD\u7F6E\uFF08\u90FD\u505A\u5B8C\u4E86\u4E00\u53E5\u5E26\u8FC7\uFF1B\u5468\u5E38\u5FEB\u91CD\u7F6E\u8FD8\u6CA1\u505A\u5B8C\u8981\u63D0\u9192\uFF09\uFF0C\u518D\u8BF4\u5FEB\u7ED3\u675F\u4E14\u6CA1\u505A\u5B8C\u7684\u6D3B\u52A8\uFF08\u5199\u6E05\u5269\u51E0\u5929\u3001\u8FD8\u5DEE\u4EC0\u4E48\uFF09\uFF0C\u6700\u540E\u8BF4\u521A\u5F00\u59CB\u548C\u5373\u5C06\u5F00\u59CB\u7684\uFF1B\u7528\u6237\u5DF2\u505A\u5B8C\u7684\u4E0D\u7528\u63D0\u9192\uFF1B\u4E0D\u7528 Markdown \u6807\u9898\u3001\u52A0\u7C97\u6216\u8868\u683C\uFF1B\u6574\u7BC7\u4E0D\u8D85\u8FC7 12 \u884C\u3002\u8BED\u6C14\u3001\u79F0\u547C\u548C\u81EA\u79F0\u6309\u4F60\u7684\u300C\u56DE\u590D\u98CE\u683C\u300D\u6765\uFF08\u6CA1\u6709\u8BBE\u5B9A\u5C31\u5E73\u5B9E\u5730\u5199\uFF09\uFF1A\u53EF\u4EE5\u7528\u4E00\u53E5\u7B26\u5408\u98CE\u683C\u7684\u8BDD\u5F00\u5934\uFF0C\u5185\u5BB9\u7167\u4E0A\u9762\u7684\u8981\u6C42\uFF0C\u4E0D\u8981\u5BA2\u5957\u7684\u7ED3\u675F\u8BED\u3002",
6926
7908
  "",
6927
7909
  digest
6928
7910
  ].join("\n");
@@ -6989,10 +7971,15 @@ export {
6989
7971
  DailyReport,
6990
7972
  GAME_ROLE,
6991
7973
  OWN_FIELDS,
7974
+ POOL_SOURCES,
7975
+ RoutineBook,
6992
7976
  buildReport,
7977
+ dailyPeriod,
6993
7978
  daysLeft,
6994
7979
  lib_default as default,
7980
+ gameRoutines,
6995
7981
  gameTools,
7982
+ givesPools,
6996
7983
  idKey,
6997
7984
  imageType,
6998
7985
  leftText,
@@ -7000,9 +7987,15 @@ export {
7000
7987
  nameKey,
7001
7988
  parseAsk,
7002
7989
  parseDateText,
7990
+ parseHoyo,
7991
+ parsePools,
7003
7992
  parseRemaining,
7004
7993
  parseSra,
7005
7994
  parseSteam,
7995
+ patternText,
7996
+ poolSources,
7997
+ readHoyo,
7998
+ readPools,
7006
7999
  readSmw,
7007
8000
  readSra,
7008
8001
  readSteam,
@@ -7013,5 +8006,6 @@ export {
7013
8006
  shortDate,
7014
8007
  smwQuery,
7015
8008
  wallClock,
8009
+ weeklyPeriod,
7016
8010
  zonedTime
7017
8011
  };