Files
oh-my-pi/packages/coding-agent/src/extensibility/extensions/compact-handler.ts
T
can1357 d24d11a274 fix: resolved AI/OAuth helper duplication via shared modules
- Standardized missing-file read errors and now return `File not found: <path>` for absent edit targets.
- Centralized AI provider, usage, and OAuth helpers into shared modules to remove duplicated logic.
- Migrated OAuth/API-key login flows to shared factory helpers and removed inline prompt/token-exchange code.
- Reused shared tools and formatter utilities for discovery, stream tails, LSP batching, and source formatting.
- Consolidated repeated test helpers and fixtures into shared modules, replacing inline helper duplicates.
2026-04-23 21:02:14 +02:00

41 lines
1.5 KiB
TypeScript

/**
* 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-mode, rpc-mode, and the executor.
*/
import type { Model } from "@oh-my-pi/pi-ai";
import type { CompactOptions } from "./types";
interface CompactableSession {
compact(instructions?: string, options?: CompactOptions): Promise<unknown>;
}
export async function runExtensionCompact(
session: CompactableSession,
instructionsOrOptions: string | CompactOptions | undefined,
): Promise<void> {
const instructions = typeof instructionsOrOptions === "string" ? instructionsOrOptions : undefined;
const options =
instructionsOrOptions && typeof instructionsOrOptions === "object" ? instructionsOrOptions : undefined;
await session.compact(instructions, options);
}
interface SetModelCapableSession {
modelRegistry: { getApiKey(model: Model): Promise<string | undefined> };
setModel(model: Model): Promise<unknown>;
}
/**
* 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<boolean> {
const key = await session.modelRegistry.getApiKey(model);
if (!key) return false;
await session.setModel(model);
return true;
}