branch:
forever.ts
2321 bytesRaw
/**
 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 * !! WARNING: EXPERIMENTAL — DO NOT USE IN PRODUCTION                  !!
 * !!                                                                   !!
 * !! This API is under active development and WILL break between       !!
 * !! releases. Method names, types, behavior, and the mixin signature  !!
 * !! are all subject to change without notice.                         !!
 * !!                                                                   !!
 * !! If you use this, pin your @cloudflare/ai-chat version and expect  !!
 * !! to rewrite your code when upgrading.                              !!
 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 *
 * Experimental mixin for durable chat streaming.
 *
 * Usage:
 *   import { AIChatAgent } from "@cloudflare/ai-chat";
 *   import { withDurableChat } from "@cloudflare/ai-chat/experimental/forever";
 *
 *   class MyAgent extends withDurableChat(AIChatAgent)<Env, State> {
 *     async onChatMessage(onFinish, options) { ... }
 *   }
 *
 * This mixin adds:
 * - keepAlive during streaming — DO stays alive while LLM generates
 * - (planned) getPartialStreamText() — extract partial response from chunks
 * - (planned) onStreamInterrupted() — hook for recovery after eviction
 *
 * @experimental This API is not yet stable and may change.
 */
import type { AIChatAgent } from "../index";

console.warn(
  "[@cloudflare/ai-chat/experimental/forever] WARNING: You are using an experimental API that WILL break between releases. Do not use in production."
);

// ── Mixin ─────────────────────────────────────────────────────────────

// oxlint-disable-next-line @typescript-eslint/no-explicit-any -- mixin constructor pattern
type AIChatAgentConstructor = new (...args: any[]) => AIChatAgent;

export function withDurableChat<TBase extends typeof AIChatAgent>(Base: TBase) {
  class DurableChatAgent extends (Base as AIChatAgentConstructor) {
    // keepAlive() is inherited from Agent via AIChatAgent.
    // No override needed — the base implementation handles everything.
  }

  return DurableChatAgent as unknown as TBase;
}