@zhin.js/plugin-lottery 1.0.3 → 1.0.5

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.
@@ -19,6 +19,8 @@ interface AuthoringToolLike {
19
19
  readonly inputSchema?: unknown;
20
20
  readonly keywords?: readonly string[];
21
21
  readonly tags?: readonly string[];
22
+ readonly platforms?: readonly string[];
23
+ readonly scopes?: readonly ('private' | 'group' | 'channel')[];
22
24
  readonly permissions?: readonly string[];
23
25
  readonly hidden?: boolean;
24
26
  execute(input: never, ctx?: unknown): unknown | Promise<unknown>;
@@ -43,6 +45,8 @@ export function registerLotteryAgentTools(host: AgentToolsHost): () => void {
43
45
  source: 'lottery',
44
46
  ...(tool.keywords ? { keywords: tool.keywords } : {}),
45
47
  ...(tool.tags ? { tags: tool.tags } : {}),
48
+ ...(tool.platforms ? { platforms: tool.platforms } : {}),
49
+ ...(tool.scopes ? { scopes: tool.scopes } : {}),
46
50
  ...(tool.permissions ? { permissions: tool.permissions } : {}),
47
51
  ...(tool.hidden !== undefined ? { hidden: tool.hidden } : {}),
48
52
  execute: (input) => tool.execute(input as never, { pluginName: 'lottery', runtimeName: name }),
@@ -0,0 +1,18 @@
1
+ // Generated by build-plugin-runtime-entries.mjs. Do not edit.
2
+ import { defineCommand } from '@zhin.js/command';
3
+ import { parseGameId } from "../../lib/games/registry.js";
4
+ import { buildPipelineDeps, formatPipelineReply, getLotteryDb, runLotteryPipeline, resolveLotteryConfig, } from "../../lib/command-helpers.js";
5
+ import { resolveLotteryRuntime } from "../../lib/runtime-state.js";
6
+ export default defineCommand({
7
+ description: 'Run full pipeline: sync → review → recommend (manual, no push)',
8
+ async execute({ params, config, owner, use }) {
9
+ const cfg = resolveLotteryConfig(config);
10
+ const gid = parseGameId(String(params.game ?? ''));
11
+ const db = resolveLotteryRuntime({ owner, use })?.db ?? getLotteryDb();
12
+ const out = await runLotteryPipeline(buildPipelineDeps(cfg, db), {
13
+ gameId: gid ?? undefined,
14
+ push: false,
15
+ });
16
+ return formatPipelineReply(out);
17
+ },
18
+ });
@@ -0,0 +1,28 @@
1
+ // Generated by build-plugin-runtime-entries.mjs. Do not edit.
2
+ import { defineCommand } from '@zhin.js/command';
3
+ import { parseGameId } from "../../lib/games/registry.js";
4
+ import { formatBacktestSection, runBacktestForGames, } from "../../lib/evaluate/backtest.js";
5
+ import { getLotteryDb, resolveLotteryConfig, } from "../../lib/command-helpers.js";
6
+ import { lotteryEnabledGames } from "../../lib/config.js";
7
+ import { resolveLotteryRuntime } from "../../lib/runtime-state.js";
8
+ export default defineCommand({
9
+ description: 'Walk-forward backtest vs random baseline',
10
+ async execute({ params, config, owner, use }) {
11
+ const db = resolveLotteryRuntime({ owner, use })?.db ?? getLotteryDb();
12
+ if (!db)
13
+ return '数据库未就绪';
14
+ const cfg = resolveLotteryConfig(config);
15
+ const gid = parseGameId(String(params.game ?? ''));
16
+ const gameIds = gid ? [gid] : lotteryEnabledGames(cfg);
17
+ const summaries = await runBacktestForGames(db, gameIds, {
18
+ pickCount: cfg.pickCount,
19
+ window: cfg.backtestWindow,
20
+ randomTrials: cfg.backtestRandomTrials,
21
+ minHistory: cfg.backtestMinHistory,
22
+ historyLimit: cfg.historyLimit,
23
+ adaptive: cfg.backtestAdaptive,
24
+ });
25
+ const text = formatBacktestSection(summaries);
26
+ return text || '历史数据不足,请先执行 lottery 同步开奖';
27
+ },
28
+ });
@@ -0,0 +1,23 @@
1
+ // Generated by build-plugin-runtime-entries.mjs. Do not edit.
2
+ import { defineCommand } from '@zhin.js/command';
3
+ import { parseGameId } from "../../lib/games/registry.js";
4
+ import { loadDraws } from "../../lib/db.js";
5
+ import { getLotteryDb } from "../../lib/command-helpers.js";
6
+ import { resolveLotteryRuntime } from "../../lib/runtime-state.js";
7
+ export default defineCommand({
8
+ description: 'List historical draw results',
9
+ async execute({ params, args, owner, use }) {
10
+ const gid = parseGameId(String(params.game ?? ''));
11
+ if (!gid)
12
+ return '请指定玩法';
13
+ const db = resolveLotteryRuntime({ owner, use })?.db ?? getLotteryDb();
14
+ if (!db)
15
+ return '数据库未就绪';
16
+ // count 原来是第二动态段,约定式命令只支持单动态文件参数,改从 args 取
17
+ const count = Math.min(50, Math.max(1, Number(args[0]) || 10));
18
+ const draws = await loadDraws(db, gid, count);
19
+ if (!draws.length)
20
+ return '暂无数据';
21
+ return draws.map((d) => `${d.issue} ${d.drawTime} ${JSON.stringify(d.numbers)}`).join('\n');
22
+ },
23
+ });
@@ -0,0 +1,33 @@
1
+ // Generated by build-plugin-runtime-entries.mjs. Do not edit.
2
+ import { defineCommand } from '@zhin.js/command';
3
+ import { parseGameId } from "../../lib/games/registry.js";
4
+ import { loadDraws } from "../../lib/db.js";
5
+ import { recommendGame, formatPickLine, formatPickStats } from "../../lib/recommend/game-pick.js";
6
+ import { loadAccuracySnapshot, loadGameWeights } from "../../lib/evaluate/tracker.js";
7
+ import { getLotteryDb, resolveLotteryConfig, } from "../../lib/command-helpers.js";
8
+ import { lotteryKl8 } from "../../lib/config.js";
9
+ import { resolveLotteryRuntime } from "../../lib/runtime-state.js";
10
+ export default defineCommand({
11
+ description: 'Single-game stats snapshot',
12
+ async execute({ params, config, owner, use }) {
13
+ const gid = parseGameId(String(params.game ?? ''));
14
+ if (!gid)
15
+ return '请指定玩法';
16
+ const db = resolveLotteryRuntime({ owner, use })?.db ?? getLotteryDb();
17
+ if (!db)
18
+ return '数据库未就绪';
19
+ const cfg = resolveLotteryConfig(config);
20
+ const draws = await loadDraws(db, gid, cfg.historyLimit);
21
+ if (!draws.length)
22
+ return '暂无数据,请先 lottery';
23
+ const kl8 = lotteryKl8(cfg);
24
+ const pick = recommendGame(gid, draws, {
25
+ pickCount: gid === 'kl8' ? kl8.pickCount : cfg.pickCount,
26
+ tieSeed: 'stats',
27
+ weights: await loadGameWeights(db, gid),
28
+ accuracy: await loadAccuracySnapshot(db, gid),
29
+ kl8: gid === 'kl8' ? kl8 : undefined,
30
+ });
31
+ return [formatPickLine(pick), formatPickStats(pick), `样本 ${draws.length} 期`].join('\n');
32
+ },
33
+ });
@@ -0,0 +1,17 @@
1
+ // Generated by build-plugin-runtime-entries.mjs. Do not edit.
2
+ import { defineCommand } from '@zhin.js/command';
3
+ import { formatDailyReportText, loadTodayReport } from "../lib/recommend/report.js";
4
+ import { getLotteryDb } from "../lib/command-helpers.js";
5
+ import { resolveLotteryRuntime } from "../lib/runtime-state.js";
6
+ export default defineCommand({
7
+ description: 'Show today published recommendation report',
8
+ async execute({ owner, use }) {
9
+ const db = resolveLotteryRuntime({ owner, use })?.db ?? getLotteryDb();
10
+ if (!db)
11
+ return '数据库未就绪';
12
+ const report = await loadTodayReport(db);
13
+ if (!report)
14
+ return '今日尚无推荐,可执行 lottery 或等待定时任务';
15
+ return formatDailyReportText(report, '');
16
+ },
17
+ });
@@ -0,0 +1,28 @@
1
+ // Generated by build-plugin-runtime-entries.mjs. Do not edit.
2
+ import { defineCommand } from '@zhin.js/command';
3
+ import { parseGameId } from "../../lib/games/registry.js";
4
+ import { formatTrainReport, trainAllGameWeights, } from "../../lib/evaluate/backtest.js";
5
+ import { getLotteryDb, resolveLotteryConfig, } from "../../lib/command-helpers.js";
6
+ import { lotteryEnabledGames } from "../../lib/config.js";
7
+ import { resolveLotteryRuntime } from "../../lib/runtime-state.js";
8
+ export default defineCommand({
9
+ description: 'Full-history weight training',
10
+ async execute({ params, config, owner, use }) {
11
+ const db = resolveLotteryRuntime({ owner, use })?.db ?? getLotteryDb();
12
+ if (!db)
13
+ return '数据库未就绪';
14
+ const cfg = resolveLotteryConfig(config);
15
+ const gid = parseGameId(String(params.game ?? ''));
16
+ const gameIds = gid ? [gid] : lotteryEnabledGames(cfg);
17
+ const results = await trainAllGameWeights(db, gameIds, {
18
+ pickCount: cfg.pickCount,
19
+ minHistory: cfg.backtestMinHistory,
20
+ historyLimit: cfg.historyLimit,
21
+ randomTrials: cfg.backtestRandomTrials,
22
+ holdoutWindow: cfg.backtestWindow,
23
+ holdoutFallback: cfg.weightHoldoutFallback,
24
+ persist: true,
25
+ });
26
+ return formatTrainReport(results);
27
+ },
28
+ });
@@ -2,6 +2,7 @@
2
2
  * Shared runtime deps for lottery agent/ authoring tools.
3
3
  * Set once from plugin index during init — never call getPlugin() from tool execute.
4
4
  */
5
+ import { type Dispose } from '@zhin.js/plugin-runtime';
5
6
  import type { GameId } from './types.js';
6
7
  import type { LotteryDb } from './db.js';
7
8
  import type { Kl8Config } from './games/kl8-groups.js';
@@ -18,7 +19,11 @@ export interface LotteryAgentDeps {
18
19
  pipelinePush: boolean;
19
20
  }
20
21
  export declare function setLotteryAgentDeps(deps: LotteryAgentDeps): void;
21
- /** Generation-owned Agent dependency binding used by Plugin Runtime setup(). */
22
- export declare function registerLotteryAgentDeps(deps: LotteryAgentDeps): () => void;
22
+ /**
23
+ * Generation-owned Agent dependency binding used by Plugin Runtime setup().
24
+ * The fresh lifecycle is detached: callers own the returned dispose (setup()
25
+ * hooks it into context.lifecycle); provide() does not retain the context.
26
+ */
27
+ export declare function registerLotteryAgentDeps(deps: LotteryAgentDeps): Dispose;
23
28
  export declare function getLotteryAgentDeps(): LotteryAgentDeps;
24
29
  //# sourceMappingURL=lottery-agent-deps.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"lottery-agent-deps.d.ts","sourceRoot":"","sources":["../src/lottery-agent-deps.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAEvD,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,MAAM,SAAS,GAAG,IAAI,CAAC;IAC9B,SAAS,EAAE,MAAM;QAAE,SAAS,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,SAAS,CAAA;KAAE,CAAC;IAC7E,YAAY,EAAE,MAAM,MAAM,EAAE,CAAC;IAC7B,YAAY,EAAE,MAAM,MAAM,CAAC;IAC3B,eAAe,EAAE,MAAM,OAAO,CAAC;IAC/B,YAAY,EAAE,OAAO,CAAC;CACvB;AAKD,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,gBAAgB,GAAG,IAAI,CAEhE;AAED,gFAAgF;AAChF,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,gBAAgB,GAAG,MAAM,IAAI,CAO3E;AAED,wBAAgB,mBAAmB,IAAI,gBAAgB,CAItD"}
1
+ {"version":3,"file":"lottery-agent-deps.d.ts","sourceRoot":"","sources":["../src/lottery-agent-deps.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAuC,KAAK,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAC5F,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAEvD,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,MAAM,SAAS,GAAG,IAAI,CAAC;IAC9B,SAAS,EAAE,MAAM;QAAE,SAAS,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,SAAS,CAAA;KAAE,CAAC;IAC7E,YAAY,EAAE,MAAM,MAAM,EAAE,CAAC;IAC7B,YAAY,EAAE,MAAM,MAAM,CAAC;IAC3B,eAAe,EAAE,MAAM,OAAO,CAAC;IAC/B,YAAY,EAAE,OAAO,CAAC;CACvB;AAMD,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,gBAAgB,GAAG,IAAI,CAEhE;AAED;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,gBAAgB,GAAG,OAAO,CAExE;AAED,wBAAgB,mBAAmB,IAAI,gBAAgB,CAItD"}
@@ -1,20 +1,23 @@
1
+ /**
2
+ * Shared runtime deps for lottery agent/ authoring tools.
3
+ * Set once from plugin index during init — never call getPlugin() from tool execute.
4
+ */
5
+ import { createGenerationStore, DisposeStack } from '@zhin.js/plugin-runtime';
6
+ const agentDepsStore = createGenerationStore('lottery/agent-deps');
1
7
  let _deps = null;
2
- const registrations = [];
3
8
  export function setLotteryAgentDeps(deps) {
4
9
  _deps = deps;
5
10
  }
6
- /** Generation-owned Agent dependency binding used by Plugin Runtime setup(). */
11
+ /**
12
+ * Generation-owned Agent dependency binding used by Plugin Runtime setup().
13
+ * The fresh lifecycle is detached: callers own the returned dispose (setup()
14
+ * hooks it into context.lifecycle); provide() does not retain the context.
15
+ */
7
16
  export function registerLotteryAgentDeps(deps) {
8
- const registration = Object.freeze({ value: deps });
9
- registrations.push(registration);
10
- return () => {
11
- const index = registrations.lastIndexOf(registration);
12
- if (index >= 0)
13
- registrations.splice(index, 1);
14
- };
17
+ return agentDepsStore.provide({ lifecycle: new DisposeStack() }, deps);
15
18
  }
16
19
  export function getLotteryAgentDeps() {
17
- const deps = registrations[registrations.length - 1]?.value ?? _deps;
20
+ const deps = agentDepsStore.tryUse() ?? _deps;
18
21
  if (!deps)
19
22
  throw new Error('lottery agent deps not initialized');
20
23
  return deps;
@@ -1 +1 @@
1
- {"version":3,"file":"lottery-agent-deps.js","sourceRoot":"","sources":["../src/lottery-agent-deps.ts"],"names":[],"mappings":"AAiBA,IAAI,KAAK,GAA4B,IAAI,CAAC;AAC1C,MAAM,aAAa,GAAgD,EAAE,CAAC;AAEtE,MAAM,UAAU,mBAAmB,CAAC,IAAsB;IACxD,KAAK,GAAG,IAAI,CAAC;AACf,CAAC;AAED,gFAAgF;AAChF,MAAM,UAAU,wBAAwB,CAAC,IAAsB;IAC7D,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IACpD,aAAa,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IACjC,OAAO,GAAG,EAAE;QACV,MAAM,KAAK,GAAG,aAAa,CAAC,WAAW,CAAC,YAAY,CAAC,CAAC;QACtD,IAAI,KAAK,IAAI,CAAC;YAAE,aAAa,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;IACjD,CAAC,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,mBAAmB;IACjC,MAAM,IAAI,GAAG,aAAa,CAAC,aAAa,CAAC,MAAM,GAAG,CAAC,CAAC,EAAE,KAAK,IAAI,KAAK,CAAC;IACrE,IAAI,CAAC,IAAI;QAAE,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC;IACjE,OAAO,IAAI,CAAC;AACd,CAAC"}
1
+ {"version":3,"file":"lottery-agent-deps.js","sourceRoot":"","sources":["../src/lottery-agent-deps.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,qBAAqB,EAAE,YAAY,EAAgB,MAAM,yBAAyB,CAAC;AAc5F,MAAM,cAAc,GAAG,qBAAqB,CAAmB,oBAAoB,CAAC,CAAC;AAErF,IAAI,KAAK,GAA4B,IAAI,CAAC;AAE1C,MAAM,UAAU,mBAAmB,CAAC,IAAsB;IACxD,KAAK,GAAG,IAAI,CAAC;AACf,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,wBAAwB,CAAC,IAAsB;IAC7D,OAAO,cAAc,CAAC,OAAO,CAAC,EAAE,SAAS,EAAE,IAAI,YAAY,EAAE,EAAE,EAAE,IAAI,CAAC,CAAC;AACzE,CAAC;AAED,MAAM,UAAU,mBAAmB;IACjC,MAAM,IAAI,GAAG,cAAc,CAAC,MAAM,EAAE,IAAI,KAAK,CAAC;IAC9C,IAAI,CAAC,IAAI;QAAE,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC;IACjE,OAAO,IAAI,CAAC;AACd,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhin.js/plugin-lottery",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Multi-game lottery analysis for Zhin.js (Plugin Runtime)",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -14,7 +14,7 @@
14
14
  "./package.json": "./package.json"
15
15
  },
16
16
  "files": [
17
- "plugin.ts",
17
+ "plugin.js",
18
18
  "schema.json",
19
19
  "commands",
20
20
  "src",
@@ -41,18 +41,18 @@
41
41
  },
42
42
  "license": "MIT",
43
43
  "dependencies": {
44
- "@zhin.js/command": "1.0.2",
45
- "@zhin.js/plugin-runtime": "1.1.0"
44
+ "@zhin.js/command": "1.0.4",
45
+ "@zhin.js/plugin-runtime": "1.1.1"
46
46
  },
47
47
  "devDependencies": {
48
48
  "typescript": "^6.0.3",
49
49
  "vitest": "^4.1.10",
50
50
  "zod": "^4.4.3",
51
- "@zhin.js/agent": "1.0.5"
51
+ "@zhin.js/agent": "1.0.7"
52
52
  },
53
53
  "peerDependencies": {
54
54
  "zod": "^4.0.0",
55
- "@zhin.js/agent": "1.0.5"
55
+ "@zhin.js/agent": "1.0.7"
56
56
  },
57
57
  "peerDependenciesMeta": {
58
58
  "@zhin.js/agent": {
@@ -77,7 +77,7 @@
77
77
  "zhin": {
78
78
  "protocol": 1,
79
79
  "type": "plugin",
80
- "entry": "./plugin.ts",
80
+ "entry": "./plugin.js",
81
81
  "engine": "^1.0.0",
82
82
  "runtime": "trusted",
83
83
  "features": [
package/plugin.js ADDED
@@ -0,0 +1,101 @@
1
+ // Generated by build-plugin-runtime-entries.mjs. Do not edit.
2
+ import { definePlugin, agentToolsHostToken, databaseHostToken, outboundHostToken, scheduleHostToken, } from '@zhin.js/plugin-runtime';
3
+ import { lotteryEnabledGames, lotteryKl8, resolveLotteryConfig, } from "./lib/config.js";
4
+ import { defineLotteryTables } from "./lib/db.js";
5
+ import { registerLotteryDb } from "./lib/db-store.js";
6
+ import { registerLotteryAgentDeps } from "./lib/lottery-agent-deps.js";
7
+ import { createInMemoryLotteryDb } from "./lib/memory-db.js";
8
+ import { lotteryRuntimeToken } from "./lib/runtime-state.js";
9
+ import { runLotteryPipeline } from "./lib/pipeline.js";
10
+ import { registerLotteryOutboundPush } from "./lib/push.js";
11
+ /**
12
+ * Plugin Runtime:
13
+ * - DB: prefer databaseHostToken; else memory.
14
+ * - Push: OutboundHost + config.pushTargets (cron / publish).
15
+ * - Cron: scheduleHostToken daily pipeline.
16
+ */
17
+ export default definePlugin({
18
+ name: 'lottery',
19
+ metadata: {
20
+ displayName: 'Lottery',
21
+ },
22
+ async setup(context) {
23
+ const config = resolveLotteryConfig(context.config.get());
24
+ const db = context.resources.has(databaseHostToken)
25
+ ? context.resources.use(databaseHostToken)
26
+ : createInMemoryLotteryDb();
27
+ if (context.resources.has(databaseHostToken)) {
28
+ defineLotteryTables(db);
29
+ }
30
+ context.resources.provide(lotteryRuntimeToken, { db });
31
+ context.lifecycle.add(registerLotteryDb(db));
32
+ let outboundPush = null;
33
+ if (context.resources.has(outboundHostToken) && config.pushTargets.length > 0) {
34
+ const outbound = context.resources.use(outboundHostToken);
35
+ outboundPush = async (text) => {
36
+ for (const target of config.pushTargets) {
37
+ try {
38
+ await outbound.send({
39
+ adapter: target.adapter,
40
+ endpointId: target.endpointId || target.adapter,
41
+ channelType: target.channelType || 'private',
42
+ channelId: target.channelId,
43
+ content: text,
44
+ });
45
+ }
46
+ catch {
47
+ // OutboundHost logs; continue remaining targets.
48
+ }
49
+ }
50
+ };
51
+ }
52
+ context.lifecycle.add(registerLotteryOutboundPush(outboundPush));
53
+ context.lifecycle.add(registerLotteryAgentDeps({
54
+ getDb: () => db,
55
+ getConfig: () => ({
56
+ pickCount: config.pickCount,
57
+ historyLimit: config.historyLimit,
58
+ kl8: lotteryKl8(config),
59
+ }),
60
+ enabledGames: () => lotteryEnabledGames(config),
61
+ scheduleCron: () => config.scheduleCron,
62
+ scheduleEnabled: () => config.scheduleEnabled,
63
+ pipelinePush: config.pushTargets.length > 0,
64
+ }));
65
+ // Agent tools (agent/tools/*): register into the Runtime Agent Host when AI
66
+ // is installed. Lazy import keeps @zhin.js/agent out of IM-only installs.
67
+ if (context.resources.has(agentToolsHostToken)) {
68
+ const agentTools = context.resources.use(agentToolsHostToken);
69
+ const { registerLotteryAgentTools } = await import('./agent/runtime-tools.js');
70
+ context.lifecycle.add(registerLotteryAgentTools(agentTools));
71
+ }
72
+ if (!context.resources.has(scheduleHostToken) || !config.scheduleEnabled) {
73
+ return;
74
+ }
75
+ const schedule = context.resources.use(scheduleHostToken);
76
+ const dispose = schedule.register({
77
+ id: 'lottery/daily_pipeline',
78
+ cron: config.scheduleCron || '0 0 18 * * *',
79
+ description: 'Daily lottery pipeline',
80
+ async execute() {
81
+ await runLotteryPipeline({
82
+ getDb: () => db,
83
+ enabledGames: () => lotteryEnabledGames(config),
84
+ historyLimit: config.historyLimit,
85
+ pickCount: config.pickCount,
86
+ kl8: lotteryKl8(config),
87
+ backtest: {
88
+ enabled: config.backtestEnabled,
89
+ window: config.backtestWindow,
90
+ randomTrials: config.backtestRandomTrials,
91
+ minHistory: config.backtestMinHistory,
92
+ adaptive: config.backtestAdaptive,
93
+ },
94
+ weightPersist: config.weightPersistEnabled,
95
+ weightHoldoutFallback: config.weightHoldoutFallback,
96
+ }, { push: config.pushTargets.length > 0 });
97
+ },
98
+ });
99
+ context.lifecycle.add(dispose);
100
+ },
101
+ });
@@ -2,6 +2,7 @@
2
2
  * Shared runtime deps for lottery agent/ authoring tools.
3
3
  * Set once from plugin index during init — never call getPlugin() from tool execute.
4
4
  */
5
+ import { createGenerationStore, DisposeStack, type Dispose } from '@zhin.js/plugin-runtime';
5
6
  import type { GameId } from './types.js';
6
7
  import type { LotteryDb } from './db.js';
7
8
  import type { Kl8Config } from './games/kl8-groups.js';
@@ -15,25 +16,25 @@ export interface LotteryAgentDeps {
15
16
  pipelinePush: boolean;
16
17
  }
17
18
 
19
+ const agentDepsStore = createGenerationStore<LotteryAgentDeps>('lottery/agent-deps');
20
+
18
21
  let _deps: LotteryAgentDeps | null = null;
19
- const registrations: Array<{ readonly value: LotteryAgentDeps }> = [];
20
22
 
21
23
  export function setLotteryAgentDeps(deps: LotteryAgentDeps): void {
22
24
  _deps = deps;
23
25
  }
24
26
 
25
- /** Generation-owned Agent dependency binding used by Plugin Runtime setup(). */
26
- export function registerLotteryAgentDeps(deps: LotteryAgentDeps): () => void {
27
- const registration = Object.freeze({ value: deps });
28
- registrations.push(registration);
29
- return () => {
30
- const index = registrations.lastIndexOf(registration);
31
- if (index >= 0) registrations.splice(index, 1);
32
- };
27
+ /**
28
+ * Generation-owned Agent dependency binding used by Plugin Runtime setup().
29
+ * The fresh lifecycle is detached: callers own the returned dispose (setup()
30
+ * hooks it into context.lifecycle); provide() does not retain the context.
31
+ */
32
+ export function registerLotteryAgentDeps(deps: LotteryAgentDeps): Dispose {
33
+ return agentDepsStore.provide({ lifecycle: new DisposeStack() }, deps);
33
34
  }
34
35
 
35
36
  export function getLotteryAgentDeps(): LotteryAgentDeps {
36
- const deps = registrations[registrations.length - 1]?.value ?? _deps;
37
+ const deps = agentDepsStore.tryUse() ?? _deps;
37
38
  if (!deps) throw new Error('lottery agent deps not initialized');
38
39
  return deps;
39
40
  }
package/plugin.ts DELETED
@@ -1,115 +0,0 @@
1
- import {
2
- definePlugin,
3
- agentToolsHostToken,
4
- databaseHostToken,
5
- outboundHostToken,
6
- scheduleHostToken,
7
- } from '@zhin.js/plugin-runtime';
8
- import {
9
- lotteryEnabledGames,
10
- lotteryKl8,
11
- resolveLotteryConfig,
12
- type LotteryConfig,
13
- } from './src/config.js';
14
- import { defineLotteryTables } from './src/db.js';
15
- import { registerLotteryDb } from './src/db-store.js';
16
- import { registerLotteryAgentDeps } from './src/lottery-agent-deps.js';
17
- import { createInMemoryLotteryDb } from './src/memory-db.js';
18
- import { lotteryRuntimeToken } from './src/runtime-state.js';
19
- import { runLotteryPipeline } from './src/pipeline.js';
20
- import { registerLotteryOutboundPush } from './src/push.js';
21
-
22
- /**
23
- * Plugin Runtime:
24
- * - DB: prefer databaseHostToken; else memory.
25
- * - Push: OutboundHost + config.pushTargets (cron / publish).
26
- * - Cron: scheduleHostToken daily pipeline.
27
- */
28
- export default definePlugin<LotteryConfig>({
29
- name: 'lottery',
30
- metadata: {
31
- displayName: 'Lottery',
32
- },
33
- async setup(context) {
34
- const config = resolveLotteryConfig(context.config.get());
35
- const db = context.resources.has(databaseHostToken)
36
- ? context.resources.use(databaseHostToken)
37
- : createInMemoryLotteryDb();
38
- if (context.resources.has(databaseHostToken)) {
39
- defineLotteryTables(db);
40
- }
41
- context.resources.provide(lotteryRuntimeToken, { db });
42
- context.lifecycle.add(registerLotteryDb(db));
43
-
44
- let outboundPush: ((text: string) => Promise<void>) | null = null;
45
- if (context.resources.has(outboundHostToken) && config.pushTargets.length > 0) {
46
- const outbound = context.resources.use(outboundHostToken);
47
- outboundPush = async (text) => {
48
- for (const target of config.pushTargets) {
49
- try {
50
- await outbound.send({
51
- adapter: target.adapter,
52
- endpointId: target.endpointId || target.adapter,
53
- channelType: target.channelType || 'private',
54
- channelId: target.channelId,
55
- content: text,
56
- });
57
- } catch {
58
- // OutboundHost logs; continue remaining targets.
59
- }
60
- }
61
- };
62
- }
63
- context.lifecycle.add(registerLotteryOutboundPush(outboundPush));
64
-
65
- context.lifecycle.add(registerLotteryAgentDeps({
66
- getDb: () => db,
67
- getConfig: () => ({
68
- pickCount: config.pickCount,
69
- historyLimit: config.historyLimit,
70
- kl8: lotteryKl8(config),
71
- }),
72
- enabledGames: () => lotteryEnabledGames(config),
73
- scheduleCron: () => config.scheduleCron,
74
- scheduleEnabled: () => config.scheduleEnabled,
75
- pipelinePush: config.pushTargets.length > 0,
76
- }));
77
-
78
- // Agent tools (agent/tools/*): register into the Runtime Agent Host when AI
79
- // is installed. Lazy import keeps @zhin.js/agent out of IM-only installs.
80
- if (context.resources.has(agentToolsHostToken)) {
81
- const agentTools = context.resources.use(agentToolsHostToken);
82
- const { registerLotteryAgentTools } = await import('./agent/runtime-tools.js');
83
- context.lifecycle.add(registerLotteryAgentTools(agentTools));
84
- }
85
-
86
- if (!context.resources.has(scheduleHostToken) || !config.scheduleEnabled) {
87
- return;
88
- }
89
- const schedule = context.resources.use(scheduleHostToken);
90
- const dispose = schedule.register({
91
- id: 'lottery/daily_pipeline',
92
- cron: config.scheduleCron || '0 0 18 * * *',
93
- description: 'Daily lottery pipeline',
94
- async execute() {
95
- await runLotteryPipeline({
96
- getDb: () => db,
97
- enabledGames: () => lotteryEnabledGames(config),
98
- historyLimit: config.historyLimit,
99
- pickCount: config.pickCount,
100
- kl8: lotteryKl8(config),
101
- backtest: {
102
- enabled: config.backtestEnabled,
103
- window: config.backtestWindow,
104
- randomTrials: config.backtestRandomTrials,
105
- minHistory: config.backtestMinHistory,
106
- adaptive: config.backtestAdaptive,
107
- },
108
- weightPersist: config.weightPersistEnabled,
109
- weightHoldoutFallback: config.weightHoldoutFallback,
110
- }, { push: config.pushTargets.length > 0 });
111
- },
112
- });
113
- context.lifecycle.add(dispose);
114
- },
115
- });