import { executeGuarded } from "@aimform/guard"; export interface AimSemanticAdapterOptions { baseUrl: string; headers?: Record; fetch?: typeof globalThis.fetch; timeoutMs?: number; guard?: import("@aimform/guard").GuardBinding; } export interface AimSemanticAdapter { readonly provider: "aim"; capabilities(): Promise; execute(path: string, body?: unknown): Promise; } function normalizeBaseUrl(value: string): string { const url = new URL(value); url.pathname = url.pathname.replace(/\/$/, ""); return url.toString().replace(/\/$/, ""); } function isAbortError(error: unknown): boolean { return error instanceof Error && (error.name === "AbortError" || error.name === "TimeoutError"); } /** Host-provided Aim semantic workspace transport owned by the AI System. */ export class AimSemanticHttpAdapter implements AimSemanticAdapter { readonly provider = "aim" as const; private readonly baseUrl: string; private readonly fetchImpl: typeof globalThis.fetch; private readonly headers: Record; private readonly timeoutMs: number; private readonly guard?: import("@aimform/guard").GuardBinding; constructor(options: AimSemanticAdapterOptions) { this.baseUrl = normalizeBaseUrl(options.baseUrl); this.fetchImpl = (options.fetch ?? globalThis.fetch).bind(globalThis); this.headers = { accept: "application/json", ...options.headers }; this.timeoutMs = options.timeoutMs ?? 30_000; this.guard = options.guard; } async capabilities(): Promise { return this.request("/v1/capabilities"); } async execute(path: string, body?: unknown): Promise { return this.request(path, body); } private async request(path: string, body?: unknown): Promise { if (!path.startsWith("/v1/")) throw new Error("Aim API paths must be under /v1."); const controller = new AbortController(); const timeout = setTimeout(() => controller.abort(), this.timeoutMs); try { return await executeGuarded({ binding: this.guard, provider: this.provider, operation: { operation: path.slice(1).replaceAll("/", "."), reservationMode: "lease", estimate: { usage: { requests: 1, bytesWritten: body === undefined ? 0 : JSON.stringify(body).length }, certainty: { requests: "upper-bound", bytesWritten: "estimate" }, }, }, execute: async () => { const response = await this.fetchImpl(`${this.baseUrl}${path}`, { method: body === undefined ? "GET" : "POST", headers: body === undefined ? this.headers : { ...this.headers, "content-type": "application/json" }, ...(body === undefined ? {} : { body: JSON.stringify(body) }), signal: controller.signal, }); const payload: unknown = await response.json().catch(() => undefined); if (!response.ok) { const message = payload && typeof payload === "object" && "error" in payload ? String((payload as { error?: unknown }).error) : `Aim API request failed with status ${response.status}`; throw new Error(message); } return payload as T; }, measure: (payload) => ({ requests: 1, bytesRead: JSON.stringify(payload ?? null).length }), }); } catch (error) { if (isAbortError(error)) throw new Error(`Aim API request timed out after ${this.timeoutMs}ms`); throw error; } finally { clearTimeout(timeout); } } }