import { Agent } from "agents";
import { getAgent } from "ayjnt/rpc";
import type { GeneratedEnv } from "@ayjnt/env";
import type NavigatorAgent from "../navigator/agent.ts";
import type ScoutAgent from "../scout/agent.ts";
import type EngineerAgent from "../engineer/agent.ts";
type Phase = "idle" | "survey" | "approach" | "extract" | "return" | "done";
type State = { phase: Phase; cycle: number; running: boolean; scheduleId: string | null; log: { at: number; text: string; level: string }[]; crew: { navigator: any; scout: any; engineer: any } };
export default class CommanderAgent extends Agent<GeneratedEnv, State> {
override initialState: State = { phase: "idle", cycle: 0, running: false, scheduleId: null, log: [], crew: { navigator: null, scout: null, engineer: null } };
async start(): Promise<State> {
const s = await this.scheduleEvery(2, "tick");
this.setState({ ...this.state, running: true, scheduleId: s.id });
await this.nav().then((a) => a.setCourse({ x: 40, y: 10, z: 0 }));
return this.state;
}
async tick() {
if (!this.state.running) return;
const nav = await this.nav();
const scout = await this.scout();
const eng = await this.engineer();
const [navStatus, engStatus] = await Promise.all([nav.tick(), eng.degrade()]);
const scoutStatus = this.state.cycle % 3 === 0 ? await scout.scan() : await scout.report();
// Fuel emergency? divert to return phase
// Arrived? advance to next phase
this.setState({ ...this.state, cycle: this.state.cycle + 1,
crew: { navigator: navStatus, scout: scoutStatus, engineer: engStatus } });
}
private async nav() { return getAgent<NavigatorAgent>(this.env.NAVIGATOR_AGENT, this.name); }
private async scout() { return getAgent<ScoutAgent>(this.env.SCOUT_AGENT, this.name); }
private async engineer() { return getAgent<EngineerAgent>(this.env.ENGINEER_AGENT, this.name); }
override async onRequest(request: Request) { /* POST start/stop/reset */ return Response.json(this.state); }
}