Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117import fixture, { PersonalAgent as ExecutionPersonalAgent, Conversation as ExecutionConversation,} from "./execution-worker";import { getAgentByName, type Agent, type SubAgentClass } from "agents";import { action } from "@cloudflare/think";import type { Env } from "../../worker/personal-agent";export { Sandbox } from "./execution-worker";
export class Conversation extends ExecutionConversation { private acquisitionDelay = false; private acquiring = false; private delayNextParent = false; configureScheduleAcquisition() { this.acquisitionDelay = true; } inspectScheduleAcquisition() { return this.acquiring; } getActions() { const actions = super.getActions(); return { ...actions, createSchedule: action({ ...actions.createSchedule.config, execute: async (input, ctx) => { this.delayNextParent = this.acquisitionDelay; this.acquisitionDelay = false; return actions.createSchedule.config.execute(input, ctx); }, }), }; } async parentAgent<T extends Agent>( cls: SubAgentClass<T>, ): Promise<DurableObjectStub<T>> { const parent = await super.parentAgent(cls); if (this.delayNextParent) { this.delayNextParent = false; this.acquiring = true; await new Promise((resolve) => setTimeout(resolve, 1500)); this.acquiring = false; } return parent; }}
export class PersonalAgent extends ExecutionPersonalAgent { private fault = ""; private waiting = false; async createTaskForConversation( ...args: Parameters<ExecutionPersonalAgent["createTaskForConversation"]> ) { if (this.fault === "late-parent") { this.fault = ""; this.waiting = true; await new Promise((resolve) => setTimeout(resolve, 1500)); this.waiting = false; } const task = await super.createTaskForConversation(...args); if (this.fault === "lost-reply") { this.fault = ""; throw new Error("Fixture lost task reply PRIVATE-SCHEDULE-ERROR"); } return task; } async schedule<T = string>( when: Date | string | number, callback: keyof this, payload?: T, options?: Parameters<ExecutionPersonalAgent["schedule"]>[3], ) { if (this.fault === "unavailable" && callback === "dispatchScheduledTask") throw new Error( "Fixture native schedule unavailable PRIVATE-SCHEDULE-ERROR", ); return super.schedule(when, callback, payload, options); } async scheduleFixture(action: string, input: Record<string, unknown>) { if (action === "fault") { this.fault = input.mode as string; return; } if (action === "waiting") return this.waiting; // Test only, and never create a missing facet when observing deletion. if (!this.hasSubAgent(Conversation, input.conversationId as string)) return null; const child = await this.subAgent( Conversation, input.conversationId as string, ); if (action === "delay-acquisition") return child.configureScheduleAcquisition(); if (action === "acquiring") return child.inspectScheduleAcquisition(); throw new Error("Unknown schedule fixture action"); }}
export default { async fetch(request, env, ctx) { const path = new URL(request.url).pathname; if (path.startsWith("/__schedule/")) { const personal = await getAgentByName( env.PersonalAgent as unknown as DurableObjectNamespace<PersonalAgent>, "personal", ); return Response.json( (await personal.scheduleFixture( path.split("/").at(-1)!, await request.json(), )) ?? null, ); } return fixture.fetch(request, env, ctx); },} satisfies ExportedHandler<Env>;