Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

3.3 KB
Raw Blame
// Shared, site-agnostic types. Everything downstream of `ChatTurn` is generic;
// only the per-site adapters know how ChatGPT vs Claude wire formats look.

export type SiteName = 'claude_web' | 'chatgpt_web';

export type Retention = 'metadata' | 'redacted' | 'full';

/** A single chat message (user or assistant), provider-neutral. */
export interface Msg {
  role: 'system' | 'user' | 'assistant' | 'tool';
  text: string;
}

export interface ToolCall {
  id: string;
  name: string;
  /** Raw arguments as sent by the model (JSON string or object). */
  arguments?: unknown;
  /** Tool result, if the turn captured one. */
  result?: unknown;
}

export interface Usage {
  /** Uncached prompt tokens. Disjoint from the two cache counts below. */
  inputTokens?: number;
  outputTokens?: number;
  /** Prompt tokens written to the cache on this turn. */
  cacheCreationInputTokens?: number;
  /** The subset of cacheCreationInputTokens written with a one-hour TTL. A
   *  breakdown, never added to a total; undefined when the stream did not
   *  report the split. */
  cacheCreation1hInputTokens?: number;
  /** Prompt tokens served from the cache on this turn. */
  cacheReadInputTokens?: number;
}

/**
 * The canonical representation of one completed (or best-effort partial) chat
 * turn. Produced by a SiteAdapter, consumed by the pure `normalizeTurn`.
 */
export interface ChatTurn {
  site: SiteName;
  /** Conversation id — becomes both beacon.session.id and gen_ai.conversation.id. */
  sessionId: string;
  /** Stable id for this request/response pair within the conversation. */
  turnId: string;
  requestModel?: string;
  responseModel?: string;
  promptText: string;
  inputMessages: Msg[];
  outputMessages: Msg[];
  responseText: string;
  toolCalls: ToolCall[];
  usage?: Usage;
  /** epoch ms */
  startedAt: number;
  /** epoch ms; undefined => stream never completed (aborted/partial). */
  completedAt?: number;
  captureMode: 'sse' | 'dom';
  /** Trimmed raw payload for debugging; subject to the size cap. */
  raw?: unknown;
}

export interface Settings {
  enabled: boolean;
  retention: Retention;
  /** OTLP logs endpoint; defaults to the local beacon collector. */
  endpoint: string;
  /** Per-site enable toggles. */
  sites: Record<SiteName, boolean>;
}

/** A setting an administrator fixed through Chrome policy (managed storage). The popup and options
 *  page show it disabled, and the service worker refuses to change it. */
export type LockedSetting = 'enabled' | 'retention' | 'endpoint' | `sites.${SiteName}`;

export const DEFAULT_SETTINGS: Settings = {
  enabled: true,
  retention: 'full',
  endpoint: 'http://127.0.0.1:4318/v1/logs',
  sites: { claude_web: true, chatgpt_web: true },
};

// ---- Messaging between MAIN interceptor → ISOLATED content → service worker ----

/**
 * Emitted by the MAIN-world interceptor over its private MessagePort (see
 * src/shared/channel.ts). `reqId` is a random, unguessable id per teed request.
 */
export type InterceptEvent =
  | { kind: 'request'; reqId: string; url: string; method: string; body: string | null }
  | { kind: 'chunk'; reqId: string; chunk: string }
  | { kind: 'done'; reqId: string }
  | { kind: 'error'; reqId: string; message: string };

/** Sent from the content script to the service worker. */
export interface RelayMessage {
  type: 'BEACON_RAW';
  host: string;
  event: InterceptEvent;
}