/** * Helper for wiring the `compact` action of an {@link ExtensionContext}. * * Extension-facing APIs accept `string | CompactOptions`, but `AgentSession.compact` * takes two positional arguments `(instructions, options)`. This helper splits the * union so the same adapter can be reused by print, SDK, ACP, and executor callers. */ import type { Model } from "@gajae-code/ai/core"; import type { CompactOptions } from "./types"; interface CompactableSession { compact(instructions?: string, options?: CompactOptions): Promise; } export async function runExtensionCompact( session: CompactableSession, instructionsOrOptions: string | CompactOptions | undefined, ): Promise { const instructions = typeof instructionsOrOptions === "string" ? instructionsOrOptions : undefined; const options = instructionsOrOptions && typeof instructionsOrOptions === "object" ? instructionsOrOptions : undefined; await session.compact(instructions, options); } interface SetModelCapableSession { credentialSessionId?: string; modelRegistry: { getApiKey(model: Model, sessionId?: string): Promise }; setModel(model: Model, role?: string, options?: { cause?: string }): Promise; /** Persist effective profile roles and clear its marker for a concrete default selection. */ materializeActiveDefaultModelProfileAssignment?(model: Model): boolean; /** Drop a session-only profile marker and its runtime role overrides. */ clearSessionOnlyModelProfileState?(): void; /** Fallback marker clear for legacy session adapters. */ setActiveModelProfile?(name: string | undefined): void; } /** * Helper for wiring the `setModel` action of an {@link ExtensionContext}. * * Returns false when no API key is available for the requested model. */ export async function runExtensionSetModel(session: SetModelCapableSession, model: Model): Promise { const key = await session.modelRegistry.getApiKey(model, session.credentialSessionId); if (!key) return false; await session.setModel(model, "default", { cause: "user-selection" }); // A durable profile is replaced by materializing its effective assignments // (otherwise a restart reapplies modelProfile.default and restores the // profile the caller just replaced); a session-only marker is dropped // together with its runtime role overrides. if (!session.materializeActiveDefaultModelProfileAssignment?.(model)) { if (session.clearSessionOnlyModelProfileState) session.clearSessionOnlyModelProfileState(); else session.setActiveModelProfile?.(undefined); } return true; }