@myclaw163/game-agent-runtime 0.0.0-stage → 0.1.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/README.md +67 -2
- package/dist/framework/action-diagnostics.d.ts +11 -0
- package/dist/framework/action-diagnostics.js +67 -0
- package/dist/framework/capability-registry.d.ts +58 -0
- package/dist/framework/capability-registry.js +325 -0
- package/dist/framework/contracts.d.ts +121 -0
- package/dist/framework/contracts.js +2 -0
- package/dist/framework/control/control-scheduler.d.ts +69 -0
- package/dist/framework/control/control-scheduler.js +1016 -0
- package/dist/framework/control/domain-set.d.ts +18 -0
- package/dist/framework/control/domain-set.js +70 -0
- package/dist/framework/control/index.d.ts +3 -0
- package/dist/framework/control/index.js +3 -0
- package/dist/framework/control/types.d.ts +142 -0
- package/dist/framework/control/types.js +13 -0
- package/dist/framework/control-coordinator.d.ts +7 -0
- package/dist/framework/control-coordinator.js +7 -0
- package/dist/framework/events/event-catalog.d.ts +23 -0
- package/dist/framework/events/event-catalog.js +52 -0
- package/dist/framework/events/event-delivery-service.d.ts +33 -0
- package/dist/framework/events/event-delivery-service.js +165 -0
- package/dist/framework/events/event-ingress.d.ts +28 -0
- package/dist/framework/events/event-ingress.js +76 -0
- package/dist/framework/events/event-repository.d.ts +38 -0
- package/dist/framework/events/event-repository.js +20 -0
- package/dist/framework/events/index.d.ts +7 -0
- package/dist/framework/events/index.js +7 -0
- package/dist/framework/events/memory-event-repository.d.ts +77 -0
- package/dist/framework/events/memory-event-repository.js +909 -0
- package/dist/framework/events/runtime-resume.d.ts +11 -0
- package/dist/framework/events/runtime-resume.js +7 -0
- package/dist/framework/events/sqlite-event-repository.d.ts +94 -0
- package/dist/framework/events/sqlite-event-repository.js +2044 -0
- package/dist/framework/events/store-compatibility.d.ts +17 -0
- package/dist/framework/events/store-compatibility.js +17 -0
- package/dist/framework/events/types.d.ts +244 -0
- package/dist/framework/events/types.js +39 -0
- package/dist/framework/index.d.ts +7 -0
- package/dist/framework/index.js +7 -0
- package/dist/framework/load-node-sqlite.d.ts +5 -0
- package/dist/framework/load-node-sqlite.js +19 -0
- package/dist/framework/mailbox/coalescer.d.ts +30 -0
- package/dist/framework/mailbox/coalescer.js +45 -0
- package/dist/framework/mailbox/host-notification-sink.d.ts +31 -0
- package/dist/framework/mailbox/host-notification-sink.js +13 -0
- package/dist/framework/mailbox/identity.d.ts +15 -0
- package/dist/framework/mailbox/identity.js +28 -0
- package/dist/framework/mailbox/index.d.ts +7 -0
- package/dist/framework/mailbox/index.js +7 -0
- package/dist/framework/mailbox/mailbox-dispatcher.d.ts +65 -0
- package/dist/framework/mailbox/mailbox-dispatcher.js +242 -0
- package/dist/framework/mailbox/mailbox-repository.d.ts +17 -0
- package/dist/framework/mailbox/mailbox-repository.js +9 -0
- package/dist/framework/mailbox/notification-projector.d.ts +25 -0
- package/dist/framework/mailbox/notification-projector.js +50 -0
- package/dist/framework/mailbox/types.d.ts +128 -0
- package/dist/framework/mailbox/types.js +1 -0
- package/dist/framework/modules/runtime-module-loader-hooks.mjs +156 -0
- package/dist/framework/modules/runtime-module-loader.d.ts +14 -0
- package/dist/framework/modules/runtime-module-loader.js +64 -0
- package/dist/framework/modules/runtime-module-output.d.ts +7 -0
- package/dist/framework/modules/runtime-module-output.js +15 -0
- package/dist/framework/modules/runtime-module-worker-bootstrap.mjs +23 -0
- package/dist/framework/participation-manager.d.ts +94 -0
- package/dist/framework/participation-manager.js +185 -0
- package/dist/framework/persistence/async-sqlite-persistence-worker.d.ts +1 -0
- package/dist/framework/persistence/async-sqlite-persistence-worker.js +62 -0
- package/dist/framework/persistence/memory-first-persistence.d.ts +36 -0
- package/dist/framework/persistence/memory-first-persistence.js +459 -0
- package/dist/framework/plugins/contracts.d.ts +345 -0
- package/dist/framework/plugins/contracts.js +1 -0
- package/dist/framework/plugins/index.d.ts +4 -0
- package/dist/framework/plugins/index.js +4 -0
- package/dist/framework/plugins/runtime-event-identity.d.ts +3 -0
- package/dist/framework/plugins/runtime-event-identity.js +11 -0
- package/dist/framework/plugins/runtime-input-journal.d.ts +85 -0
- package/dist/framework/plugins/runtime-input-journal.js +716 -0
- package/dist/framework/plugins/runtime-plugin-scheduler.d.ts +188 -0
- package/dist/framework/plugins/runtime-plugin-scheduler.js +1660 -0
- package/dist/framework/runtime.d.ts +264 -0
- package/dist/framework/runtime.js +1811 -0
- package/dist/framework/serial-task-queue.d.ts +6 -0
- package/dist/framework/serial-task-queue.js +12 -0
- package/dist/framework/state/contracts.d.ts +30 -0
- package/dist/framework/state/contracts.js +1 -0
- package/dist/framework/state/current-state-store.d.ts +17 -0
- package/dist/framework/state/current-state-store.js +153 -0
- package/dist/framework/state/durable-agent-state-snapshot.d.ts +61 -0
- package/dist/framework/state/durable-agent-state-snapshot.js +162 -0
- package/dist/framework/state/freshness.d.ts +7 -0
- package/dist/framework/state/freshness.js +52 -0
- package/dist/framework/state/history/contracts.d.ts +133 -0
- package/dist/framework/state/history/contracts.js +1 -0
- package/dist/framework/state/history/history-writer.d.ts +55 -0
- package/dist/framework/state/history/history-writer.js +157 -0
- package/dist/framework/state/history/memory-state-history-repository.d.ts +25 -0
- package/dist/framework/state/history/memory-state-history-repository.js +199 -0
- package/dist/framework/state/history/sqlite-state-history-repository.d.ts +28 -0
- package/dist/framework/state/history/sqlite-state-history-repository.js +565 -0
- package/dist/framework/state/history/trajectory-projector.d.ts +53 -0
- package/dist/framework/state/history/trajectory-projector.js +130 -0
- package/dist/framework/state/index.d.ts +12 -0
- package/dist/framework/state/index.js +12 -0
- package/dist/framework/state/state-ingress.d.ts +18 -0
- package/dist/framework/state/state-ingress.js +68 -0
- package/dist/framework/state/state-provider.d.ts +11 -0
- package/dist/framework/state/state-provider.js +56 -0
- package/dist/framework/state/types.d.ts +107 -0
- package/dist/framework/state/types.js +19 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/mailbox/adapters/claude-channel-adapter.d.ts +34 -0
- package/dist/mailbox/adapters/claude-channel-adapter.js +71 -0
- package/dist/mailbox/adapters/claude-channel-socket.d.ts +56 -0
- package/dist/mailbox/adapters/claude-channel-socket.js +279 -0
- package/dist/mailbox/adapters/desktop-event-socket.d.ts +81 -0
- package/dist/mailbox/adapters/desktop-event-socket.js +339 -0
- package/dist/mailbox/agent-mailbox-coordinator.d.ts +104 -0
- package/dist/mailbox/agent-mailbox-coordinator.js +412 -0
- package/dist/mailbox/host-monitor-rate-gate.d.ts +60 -0
- package/dist/mailbox/host-monitor-rate-gate.js +93 -0
- package/dist/mailbox/mailbox-event-batch-delivery.d.ts +73 -0
- package/dist/mailbox/mailbox-event-batch-delivery.js +251 -0
- package/dist/mailbox/mailbox-host-relay.d.ts +52 -0
- package/dist/mailbox/mailbox-host-relay.js +299 -0
- package/dist/mailbox/mailbox-plugin.d.ts +58 -0
- package/dist/mailbox/mailbox-plugin.js +128 -0
- package/dist/mailbox/mailbox-service.d.ts +128 -0
- package/dist/mailbox/mailbox-service.js +582 -0
- package/dist/memory/contracts.d.ts +12 -0
- package/dist/memory/contracts.js +1 -0
- package/dist/memory/strategy-memory-rpc.d.ts +21 -0
- package/dist/memory/strategy-memory-rpc.js +86 -0
- package/dist/memory/strategy-memory.d.ts +15 -0
- package/dist/memory/strategy-memory.js +95 -0
- package/dist/observability/detail-capture.d.ts +25 -0
- package/dist/observability/detail-capture.js +136 -0
- package/dist/observability/index.d.ts +5 -0
- package/dist/observability/index.js +5 -0
- package/dist/observability/process-timing.d.ts +14 -0
- package/dist/observability/process-timing.js +56 -0
- package/dist/observability/recorder.d.ts +56 -0
- package/dist/observability/recorder.js +202 -0
- package/dist/observability/redaction.d.ts +30 -0
- package/dist/observability/redaction.js +166 -0
- package/dist/observability/reporter.d.ts +32 -0
- package/dist/observability/reporter.js +140 -0
- package/dist/observability/sinks.d.ts +36 -0
- package/dist/observability/sinks.js +121 -0
- package/dist/observability/types.d.ts +81 -0
- package/dist/observability/types.js +1 -0
- package/dist/owner/control-channel.d.ts +42 -0
- package/dist/owner/control-channel.js +188 -0
- package/dist/owner/parent-process-watch.d.ts +15 -0
- package/dist/owner/parent-process-watch.js +40 -0
- package/dist/owner/process-identity.d.ts +10 -0
- package/dist/owner/process-identity.js +65 -0
- package/dist/owner/runtime-lock.d.ts +5 -0
- package/dist/owner/runtime-lock.js +47 -0
- package/dist/perception/binding-resolution.d.ts +9 -0
- package/dist/perception/binding-resolution.js +14 -0
- package/dist/perception/contracts.d.ts +85 -0
- package/dist/perception/contracts.js +1 -0
- package/dist/perception/map-concurrent.d.ts +2 -0
- package/dist/perception/map-concurrent.js +12 -0
- package/dist/perception/perception-host.d.ts +114 -0
- package/dist/perception/perception-host.js +338 -0
- package/dist/perception/perception-worker-host.d.ts +11 -0
- package/dist/perception/perception-worker-host.js +44 -0
- package/dist/perception/perception-worker.d.ts +21 -0
- package/dist/perception/perception-worker.js +77 -0
- package/dist/perception/strategy-names.d.ts +7 -0
- package/dist/perception/strategy-names.js +22 -0
- package/dist/sdk/perception.d.ts +5 -0
- package/dist/sdk/perception.js +6 -0
- package/dist/sdk/strategy.d.ts +6 -0
- package/dist/sdk/strategy.js +3 -0
- package/dist/strategy/activation-manager.d.ts +105 -0
- package/dist/strategy/activation-manager.js +367 -0
- package/dist/strategy/behavior-decision-history.d.ts +20 -0
- package/dist/strategy/behavior-decision-history.js +59 -0
- package/dist/strategy/behavior-instance-snapshot.d.ts +31 -0
- package/dist/strategy/behavior-instance-snapshot.js +145 -0
- package/dist/strategy/contracts.d.ts +189 -0
- package/dist/strategy/contracts.js +1 -0
- package/dist/strategy/control-driver.d.ts +25 -0
- package/dist/strategy/control-driver.js +131 -0
- package/dist/strategy/in-process-runner.d.ts +25 -0
- package/dist/strategy/in-process-runner.js +40 -0
- package/dist/strategy/strategy-host.d.ts +165 -0
- package/dist/strategy/strategy-host.js +1456 -0
- package/dist/strategy/strategy-script-runner.d.ts +81 -0
- package/dist/strategy/strategy-script-runner.js +235 -0
- package/dist/strategy/strategy-worker-host.d.ts +27 -0
- package/dist/strategy/strategy-worker-host.js +96 -0
- package/dist/strategy/thread-strategy-executor.d.ts +120 -0
- package/dist/strategy/thread-strategy-executor.js +778 -0
- package/examples/grid-game.mjs +129 -0
- package/examples/grid-perception-worker.mjs +6 -0
- package/examples/grid-perception.mjs +12 -0
- package/examples/grid-strategy-worker.mjs +11 -0
- package/examples/grid-strategy.mjs +13 -0
- package/examples/run-grid-game.mjs +24 -0
- package/package.json +53 -4
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// A complete second adapter. It imports only the installed public framework package.
|
|
2
|
+
import { randomUUID, createHash } from 'node:crypto';
|
|
3
|
+
import { readFileSync } from 'node:fs';
|
|
4
|
+
import { Runtime, MemoryEventRepository, MemoryStateHistoryRepository, MemoryRuntimeInputJournal, EventCatalog } from '@myclaw163/game-agent-runtime';
|
|
5
|
+
import { StrategyHost, STRATEGY_LIFECYCLE_EVENT_TYPES } from '@myclaw163/game-agent-runtime/strategy/strategy-host.js';
|
|
6
|
+
import { createStrategyControlDriver } from '@myclaw163/game-agent-runtime/strategy/control-driver.js';
|
|
7
|
+
import { StrategyMemoryStore } from '@myclaw163/game-agent-runtime/memory/strategy-memory.js';
|
|
8
|
+
import { PerceptionHost } from '@myclaw163/game-agent-runtime/perception/perception-host.js';
|
|
9
|
+
import { MailboxPlugin } from '@myclaw163/game-agent-runtime/mailbox/mailbox-plugin.js';
|
|
10
|
+
import { AgentMailboxCoordinator } from '@myclaw163/game-agent-runtime/mailbox/agent-mailbox-coordinator.js';
|
|
11
|
+
import { MailboxEventBatchPump } from '@myclaw163/game-agent-runtime/mailbox/mailbox-event-batch-delivery.js';
|
|
12
|
+
import { StrategyScriptRunner } from '@myclaw163/game-agent-runtime/strategy/strategy-script-runner.js';
|
|
13
|
+
import { PerceptionWorker } from '@myclaw163/game-agent-runtime/perception/perception-worker.js';
|
|
14
|
+
import { runtimeModuleThreadLoaderConfig } from '@myclaw163/game-agent-runtime/framework/modules/runtime-module-loader.js';
|
|
15
|
+
|
|
16
|
+
const strategyUrl = new URL('./grid-strategy.mjs', import.meta.url).href;
|
|
17
|
+
const sourceDigest = () => createHash('sha256').update(readFileSync(new URL(strategyUrl))).digest('hex');
|
|
18
|
+
const workerBridge = {
|
|
19
|
+
hostUrl: new URL('./grid-strategy-worker.mjs', import.meta.url).href,
|
|
20
|
+
pin: source => ({...source,sha256:sourceDigest()}),
|
|
21
|
+
resolve: source => { if(source.sha256!==sourceDigest()) throw Error('grid_source_changed'); return source; },
|
|
22
|
+
sourceUrl: () => strategyUrl,
|
|
23
|
+
loader: () => runtimeModuleThreadLoaderConfig(),
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export async function createGridGame({ intervalMs = 20 } = {}) {
|
|
27
|
+
let board = { column: 0, obstacle: false }, sequence = 0, sourceContext;
|
|
28
|
+
const receipts = [], decisions = [], delivered = [], strategyWorkerIds = [];
|
|
29
|
+
const memory = new StrategyMemoryStore(), events = new MemoryEventRepository(), history = new MemoryStateHistoryRepository(), journal = new MemoryRuntimeInputJournal();
|
|
30
|
+
const publish = async (patch = {}) => {
|
|
31
|
+
board = { ...board, ...patch };
|
|
32
|
+
const scope = sourceContext.currentSegment().scope;
|
|
33
|
+
await sourceContext.acceptInputBatch({ scope, sourceId: 'grid.source', connectionId: 'grid.connection',
|
|
34
|
+
sourceKey: `grid:${++sequence}`, sourceSeq: sequence, domainTick: sequence, state: { ...board }, events: [],
|
|
35
|
+
completeness: 'complete', receivedAt: new Date().toISOString() });
|
|
36
|
+
};
|
|
37
|
+
const catalog = { has: id => id === 'move', prepare: async (id, input) => {
|
|
38
|
+
if (id !== 'move') throw Error('unknown_grid_strategy');
|
|
39
|
+
const parameters = { target: 3, step: 1, ...input };
|
|
40
|
+
if (!Number.isInteger(parameters.target) || !Number.isInteger(parameters.step) || parameters.step < 1) throw Error('invalid_grid_parameters');
|
|
41
|
+
return { manifest: { id, description: 'Move to a column', execution: 'serial' }, input: parameters, rawInput: input, value: null, source: {id,execution:'serial'} };
|
|
42
|
+
} };
|
|
43
|
+
const strategy = new StrategyHost({ id: 'grid.strategy', stateTopic: 'grid.strategy.status', actionCapability: 'grid.action',
|
|
44
|
+
after: ['grid.actions', 'grid.perception'], catalog, memory, adapter: {
|
|
45
|
+
input: (input, parameters) => ({ epochId: input.epoch.epochSeq, scope: input.epoch.scope, nowMs: input.epoch.state.cycle?.nowMs ?? Date.now(),
|
|
46
|
+
tick: input.frame.backendTick ?? 0, cycle: input.epoch.state.cycle, events: input.pendingEvents,
|
|
47
|
+
value: { board: input.epoch.state, parameters } }),
|
|
48
|
+
boundary: input => ({ notificationOnly: !!input.epoch.state.cycle?.notificationOnly, cycle: !!input.epoch.state.cycle,
|
|
49
|
+
hasObservation: true, observationTick: input.frame.backendTick }),
|
|
50
|
+
policy: () => ({ resume: true }),
|
|
51
|
+
observations: { capture: frame => ({ revision: frame.epochId }), snapshot: id => ({ revision: id }), clear() {} },
|
|
52
|
+
action: action => ({ ...action }), display: () => undefined, reportEligible: () => false,
|
|
53
|
+
report: async () => undefined, reportSucceeded: () => false,
|
|
54
|
+
createRunner: options => {
|
|
55
|
+
const runner = new StrategyScriptRunner({...options,bridge:workerBridge,
|
|
56
|
+
createApi: (_input,context)=>({memory:context.memory}),normalize:value=>value??{}});
|
|
57
|
+
const decide = method => async (frame,signal,observation) => {
|
|
58
|
+
decisions.push({epoch:frame.epochId,tick:frame.tick,value:frame.value});
|
|
59
|
+
const result = await runner[method]({...frame.value,epochId:frame.epochId,scope:frame.scope},signal,observation);
|
|
60
|
+
if(result.status?.data?.workerId) strategyWorkerIds.push(result.status.data.workerId);
|
|
61
|
+
return result;
|
|
62
|
+
};
|
|
63
|
+
return {execution:runner.execution,everyTicks:runner.everyTicks,prepare:()=>runner.prepare(),stop:reason=>runner.stop(reason),
|
|
64
|
+
diagnosticSnapshot:()=>runner.diagnosticSnapshot(),decideSerial:decide('decideSerial'),decideParallel:decide('decideParallel')};
|
|
65
|
+
},
|
|
66
|
+
} });
|
|
67
|
+
const sensor = { id: 'obstacle', name: 'Obstacle sensor', value: null,
|
|
68
|
+
module: { consumes: [], produces: ['grid.obstacle'], strategies: { move: { name: 'move', cooldownMs: 0 } } } };
|
|
69
|
+
const perception = new PerceptionHost({ id: 'grid.perception', after: [], sources: [sensor], behaviors: () => [{ id: 'move', loadable: true }], adapter: {
|
|
70
|
+
reset() {}, recover: saved => saved.adapterState,
|
|
71
|
+
prepare: input => ({ state: null, resetScope: false, resetModules: false, notificationOnly: !!input.epoch.state.cycle?.notificationOnly,
|
|
72
|
+
nowMs: Date.now(), reflexAllowed: true, identity: input.epoch.scope.segmentId, value: input }),
|
|
73
|
+
configure: (_source, value) => value, describe: source => ({ id: source.id, name: source.name }),
|
|
74
|
+
input: (_source, boundary, _baseline, previous, _config, bindings) => ({ inputState: null, evaluate: true,
|
|
75
|
+
invocation: { scope: boundary.value.epoch.scope, value: { board: boundary.value.epoch.state, previous, enabled: bindings.enabledBindings.includes('move') } } }),
|
|
76
|
+
notification: event => ({ event: { type: event.type, ...event.payload }, monitorText: JSON.stringify(event.payload) }),
|
|
77
|
+
createRunner: (_source, bindMemory) => {
|
|
78
|
+
const worker = new PerceptionWorker({workerData:{sourceUrl:new URL('./grid-perception.mjs',import.meta.url).href,
|
|
79
|
+
runtimeModuleBootstrap:{hostUrl:new URL('./grid-perception-worker.mjs',import.meta.url).href,...runtimeModuleThreadLoaderConfig()}}},
|
|
80
|
+
(input,active)=>bindMemory({scope:input.scope,value:input},active));
|
|
81
|
+
return {start:()=>worker.start(),stop:()=>worker.stop(),evaluate:invocation=>worker.evaluate({...invocation.value,scope:invocation.scope})};
|
|
82
|
+
},
|
|
83
|
+
} });
|
|
84
|
+
perception.attachMemory(memory); perception.attachDriver(strategy);
|
|
85
|
+
const actions = { kind: 'epoch', id: 'grid.actions', version: '1', capabilities: { provides: ['grid.action'] },
|
|
86
|
+
start(context) { context.capabilities.provideAction({ capability: 'grid.action', describeControl: () => ({ domains: ['grid.motion'] }),
|
|
87
|
+
async perform(action, scope) {
|
|
88
|
+
if (action.action !== 'advance' || !Number.isInteger(action.amount)) throw Error('invalid_grid_action');
|
|
89
|
+
receipts.push({ before: board.column, amount: action.amount });
|
|
90
|
+
await publish({ column: board.column + action.amount });
|
|
91
|
+
return { scope, actionId: randomUUID(), status: 'success', source: 'backend', actionOutcome: 'confirmed', value: { column: board.column } };
|
|
92
|
+
} }); }, update: () => ({ outcome: 'noop' }) };
|
|
93
|
+
const relay = { kind: 'epoch', id: 'grid.results', version: '1', agentNotificationProducer: true,
|
|
94
|
+
consumes: { events: [...STRATEGY_LIFECYCLE_EVENT_TYPES] }, produces: { events: [...STRATEGY_LIFECYCLE_EVENT_TYPES] },
|
|
95
|
+
notificationRelay: { producerPluginIds: ['grid.strategy'], previousEpochOnly: true },
|
|
96
|
+
update(input) { let cursor; const output = [];
|
|
97
|
+
for (const event of input.pendingEvents) {
|
|
98
|
+
if (event.producerPluginId === 'grid.strategy' && event.epochSeq >= input.epoch.epochSeq) break;
|
|
99
|
+
cursor = event.commitSeq;
|
|
100
|
+
if (event.producerPluginId !== 'grid.strategy' || event.notificationOf) continue;
|
|
101
|
+
output.push({ type: event.type, forwardEventId: event.eventId, dedupeKey: event.eventId, payload: {},
|
|
102
|
+
notificationContent: { event: { type: event.type, ...event.payload }, monitorText: event.type } });
|
|
103
|
+
}
|
|
104
|
+
return { outcome: 'processed', events: output, consumeEventsThroughCommitSeq: cursor };
|
|
105
|
+
} };
|
|
106
|
+
const mailbox = new MailboxPlugin({ id: 'grid.mailbox', after: ['grid.perception', 'grid.strategy', 'grid.results'] });
|
|
107
|
+
const runtime = new Runtime({ adapterId: 'grid', accountId: 'demo', initialSegment: { kind: 'board', identity: 'board-1' },
|
|
108
|
+
eventRepository: events, stateHistoryRepository: history, inputJournal: journal, initialEventConsumerIds: [],
|
|
109
|
+
eventCatalog: new EventCatalog(['grid.obstacle', 'perception.module_failed', 'perception.behavior_unavailable', ...STRATEGY_LIFECYCLE_EVENT_TYPES]
|
|
110
|
+
.map(type => ({ type, producer: 'local_synth', deliveryClass: 'monitor_and_events' }))),
|
|
111
|
+
notificationPolicy: input => ['grid.perception', 'grid.results'].includes(input.producerPluginId) ? { source: 'perception' } : undefined,
|
|
112
|
+
runtimeEpochPlugins: [actions, perception, strategy, relay, mailbox],
|
|
113
|
+
processingCadence: { intervalMs, isStateUpdate: input => input.sourceId === 'grid.source', createState: ({ latestInput, cycle }) => ({ ...latestInput.state, cycle }) },
|
|
114
|
+
});
|
|
115
|
+
strategy.attachDriver(createStrategyControlDriver(runtime, { domains: ['grid.motion'], owner: 'grid.strategy' }));
|
|
116
|
+
const coordinator = new AgentMailboxCoordinator({ repository: events, readyView: () => runtime.readReadyView(), consumerId: 'grid.agent', coalesceMs: 0,
|
|
117
|
+
compactState: () => null, admitEvent: event => event.notificationSource === 'perception', presentEvent: event => event.notificationContent.event });
|
|
118
|
+
const pump = new MailboxEventBatchPump({ source: coordinator, sink: { async deliver(envelope) {
|
|
119
|
+
delivered.push(...envelope.batch.events); return { acceptedAt: new Date().toISOString() };
|
|
120
|
+
}, async close() {} } });
|
|
121
|
+
mailbox.attachDriver(pump);
|
|
122
|
+
await runtime.startSourcePlugin({ kind: 'source', id: 'grid.source', version: '1', async start(context) {
|
|
123
|
+
sourceContext = context; context.bindConnection('grid.source', 'grid.connection');
|
|
124
|
+
context.connectionChanged('grid.source', 'grid.connection', 'connected');
|
|
125
|
+
await publish(); return { stop() {} };
|
|
126
|
+
} });
|
|
127
|
+
return { runtime, strategy, perception, events, memory, journal, publish, receipts, decisions, delivered, strategyWorkerIds,
|
|
128
|
+
board: () => ({ ...board }), close: () => runtime.close() };
|
|
129
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { threadId } from 'node:worker_threads';
|
|
2
|
+
import { Perception } from '@myclaw163/game-agent-runtime/sdk/perception.js';
|
|
3
|
+
|
|
4
|
+
export default class ObstacleSensor extends Perception {
|
|
5
|
+
async evaluate({ board, previous, enabled }, api) {
|
|
6
|
+
const seen = (await api.memory.get('observations') ?? 0) + 1;
|
|
7
|
+
await api.memory.set('observations', seen);
|
|
8
|
+
return { state: board.obstacle,
|
|
9
|
+
events: board.obstacle !== previous ? [{ type: 'grid.obstacle', key: String(seen), payload: { blocked: board.obstacle, workerId: threadId } }] : [],
|
|
10
|
+
activations: board.obstacle && enabled ? [{ strategy: 'move', input: { target: 2 }, reason: 'avoid_obstacle' }] : [] };
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { runStrategyWorker } from '@myclaw163/game-agent-runtime/strategy/strategy-worker-host.js';
|
|
2
|
+
|
|
3
|
+
await runStrategyWorker({
|
|
4
|
+
async load(data) {
|
|
5
|
+
const Module = (await import(data.sourceUrl)).default;
|
|
6
|
+
return { execution: data.source.execution, stateMode: data.source.stateMode,
|
|
7
|
+
create: () => new Module(), prepare: input => ({ manifest: Module.manifest, input }) };
|
|
8
|
+
},
|
|
9
|
+
createApi: (_input, context) => ({ memory: context.memory }),
|
|
10
|
+
normalize: value => value ?? {},
|
|
11
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { threadId } from 'node:worker_threads';
|
|
2
|
+
import { Strategy } from '@myclaw163/game-agent-runtime/sdk/strategy.js';
|
|
3
|
+
|
|
4
|
+
export default class MoveTo extends Strategy {
|
|
5
|
+
static manifest = { id: 'move', description: 'Move to a column', execution: 'serial' };
|
|
6
|
+
async decide({ board, parameters }, api) {
|
|
7
|
+
const prior = await api.memory.get('decisions') ?? 0;
|
|
8
|
+
await api.memory.set('decisions', prior + 1);
|
|
9
|
+
const status = { summary: 'moving', data: { workerId: threadId } };
|
|
10
|
+
if (board.column >= parameters.target) return { status, finish: { outcome: 'completed', reason: 'arrived' } };
|
|
11
|
+
return { status, actions: [{ action: 'advance', amount: parameters.step }] };
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
import { pathToFileURL } from 'node:url';
|
|
4
|
+
import { createGridGame } from './grid-game.mjs';
|
|
5
|
+
|
|
6
|
+
export async function runGridDemo() {
|
|
7
|
+
const game = await createGridGame();
|
|
8
|
+
try {
|
|
9
|
+
await game.strategy.startShort('move', { target: 3 }, { source: 'user', maxRuntimeMs: 10_000 });
|
|
10
|
+
const deadline = Date.now() + 10_000;
|
|
11
|
+
while (game.board().column < 3 || game.strategy.status().stack.short || game.delivered.length === 0) {
|
|
12
|
+
if (Date.now() >= deadline) throw Error('Grid demo did not finish within 10 seconds.');
|
|
13
|
+
await delay(20);
|
|
14
|
+
}
|
|
15
|
+
const workers = [...new Set(game.strategyWorkerIds)];
|
|
16
|
+
if (!workers.length || workers.some(id => id <= 0)) throw Error('Strategy did not execute in a Worker.');
|
|
17
|
+
return { column: game.board().column, actionCount: game.receipts.length,
|
|
18
|
+
notificationCount: game.delivered.length, strategyWorkerIds: workers };
|
|
19
|
+
} finally { await game.close(); }
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) {
|
|
23
|
+
console.log(JSON.stringify(await runGridDemo(), null, 2));
|
|
24
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@myclaw163/game-agent-runtime",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Game-neutral local agent runtime",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=24.12.0"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"dist/",
|
|
11
|
+
"README.md",
|
|
12
|
+
"examples/*.mjs"
|
|
13
|
+
],
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"default": "./dist/index.js"
|
|
18
|
+
},
|
|
19
|
+
"./framework/*.js": {
|
|
20
|
+
"types": "./dist/framework/*.d.ts",
|
|
21
|
+
"default": "./dist/framework/*.js"
|
|
22
|
+
},
|
|
23
|
+
"./framework/modules/*.mjs": "./dist/framework/modules/*.mjs",
|
|
24
|
+
"./observability/*.js": {
|
|
25
|
+
"types": "./dist/observability/*.d.ts",
|
|
26
|
+
"default": "./dist/observability/*.js"
|
|
27
|
+
},
|
|
28
|
+
"./*.js": {
|
|
29
|
+
"types": "./dist/*.d.ts",
|
|
30
|
+
"default": "./dist/*.js"
|
|
31
|
+
},
|
|
32
|
+
"./examples/*.mjs": "./examples/*.mjs"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsc && node scripts/copy-assets.mjs",
|
|
36
|
+
"typecheck": "tsc --noEmit",
|
|
37
|
+
"test": "vitest run --maxWorkers=8",
|
|
38
|
+
"prepack": "npm run build",
|
|
39
|
+
"check:boundaries": "node scripts/check-boundaries.mjs"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"amaro": "^1.1.11",
|
|
43
|
+
"tsx": "^4.23.12",
|
|
44
|
+
"fs-native-extensions": "1.5.1"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@types/node": "^24.19.2",
|
|
48
|
+
"typescript": "^5.4.0",
|
|
49
|
+
"vitest": "^3.1.0"
|
|
50
|
+
},
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public",
|
|
53
|
+
"registry": "https://registry.npmjs.org/"
|
|
54
|
+
}
|
|
55
|
+
}
|