Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

6.4 KB
Raw Blame
/**
 * The observability mechanisms: what a node may require, declared apart from what implements it.
 */
import { Interface } from "@adelie/core/kernel";
import type { Opaque } from "@adelie/core/kernel";
import type {
  ErrorCodeCount,
  ErrorFilter,
  ErrorIgnoreRule,
  ErrorIgnoreRuleInsert,
  ErrorIgnoreRuleMatch,
  ErrorItem,
  ErrorRecordInsert,
  ErrorSummary,
} from "../db/repos/errors.js";
import type { ErrorRecordArgs } from "../runtime/error-recorder.js";
import type {
  UsageAgentBucketCount,
  UsageFilter,
  UsageGroupModelSums,
  UsageModelSums,
  UsageRecordInsert,
  UsageSeriesGranularity,
  UsageSeriesModelSums,
  UsageSessionDaySums,
  UnsettledUsageRow,
} from "../db/repos/usage.js";
import type {
  UsageErrorIgnoreAdded,
  UsageErrorIgnoreRule,
  UsageErrorIgnoreWrite,
  UsageErrorsPage,
  UsageGroupBy,
  UsageResponse,
  UsageModelTotals,
  UsageSessionDay,
} from "../api/types.js";
import type { UsageContext } from "../runtime/usage-recorder.js";
import type {
  AbortPayload,
  ApprovalDecisionPayload,
  CompactionBeginPayload,
  CompactionEndPayload,
  ImageUrlPayload,
  InlineDataPayload,
  InlineThinkingPayload,
  McpConnectBeginPayload,
  McpConnectEndPayload,
  OmniMessage,
  OmniPayload,
  PartialTextPayload,
  PartialThinkingPayload,
  PartialToolCallOutputPayload,
  PartialToolCallPayload,
  RequestBeginPayload,
  RequestEndPayload,
  SessionMetaPayload,
  SubagentPayload,
  TextPayload,
  ThinkingPayload,
  TokenUsagePayload,
  ToolCallOutputPayload,
  ToolCallPayload,
  ToolListReadyPayload,
} from "@adelie/core";
import type {
  ErrorIgnoreAddInput,
  UsageQuery,
  UsageErrorsClearQuery,
  UsageErrorsQuery,
} from "../services/usage-service.js";

/** ErrorLog: the mechanism ErrorsRepo implements. */
@Interface()
export abstract class ErrorLog {
  abstract insert(r: ErrorRecordInsert): void;
  abstract summary(projectId: string, f?: ErrorFilter): ErrorSummary;
  abstract topCode(projectId: string, f?: ErrorFilter): ErrorCodeCount | null;
  abstract recent(
    projectId: string,
    f?: ErrorFilter,
    limit?: number,
    offset?: number,
    utcOffsetMinutes?: number,
  ): ErrorItem[];
  abstract rowCount(projectId: string, f?: ErrorFilter, utcOffsetMinutes?: number): number;
  abstract deleteFiltered(projectId: string, f?: Omit<ErrorFilter, "includeGlobal">): number;
  abstract deleteByAgent(projectId: string, agentId: string): void;
  abstract deleteByProject(projectId: string): void;
  abstract ignoreRules(projectId: string): ErrorIgnoreRule[];
  abstract addIgnoreRule(projectId: string, r: ErrorIgnoreRuleInsert): ErrorIgnoreRule | null;
  abstract removeIgnoreRule(projectId: string, id: number): boolean;
  abstract ignoreRuleMatches(
    projectId: string,
    rule: ErrorIgnoreRuleMatch,
    f?: Omit<ErrorFilter, "ignored">,
  ): number;
}

/** Errors: the mechanism ErrorRecorder implements. */
@Interface()
export abstract class Errors {
  abstract record(args: ErrorRecordArgs): void;
}

/** UsageStore: the mechanism UsageRepo implements. */
@Interface()
export abstract class UsageStore {
  abstract insert(r: UsageRecordInsert): void;
  abstract unsettledRefs(): Array<{ projectId: string; provider: string; modelId: string }>;
  abstract unsettledRows(projectId: string, provider: string, modelId: string): UnsettledUsageRow[];
  abstract settle(rows: ReadonlyArray<{ id: number; cost: number | null }>): void;
  abstract bucketByModel(projectId: string, f?: UsageFilter): UsageModelSums[];
  abstract groupsByModel(
    projectId: string,
    groupBy: UsageGroupBy,
    f?: UsageFilter,
  ): UsageGroupModelSums[];
  abstract seriesByModel(
    projectId: string,
    granularity: UsageSeriesGranularity,
    f?: UsageFilter,
  ): UsageSeriesModelSums[];
  abstract sessionDayByModel(projectId: string, f?: UsageFilter): UsageSessionDaySums[];
  abstract agentSeries(
    projectId: string,
    granularity: UsageSeriesGranularity,
    f?: UsageFilter,
  ): UsageAgentBucketCount[];
  abstract distinctAgentIds(projectId: string): string[];
  abstract distinctModels(projectId: string): { provider: string; modelId: string }[];
  abstract deleteByProject(projectId: string): void;
}

/** UsageRecording: the mechanism UsageRecorder implements. */
@Interface()
export abstract class UsageRecording {
  abstract record(ctx: UsageContext, msg: OmniMessage<OmniPayload>): Promise<void>;
}

/** UsageQueries: the mechanism UsageService implements. */
@Interface()
export abstract class UsageQueries {
  abstract query(projectId: string, q: UsageQuery): Promise<UsageResponse>;
  /**
   * Usage by calendar day, each day split by Session (newest day first, `days` back from
   * today inclusive). Days with no usage are absent; today is always present, at zero when
   * nothing ran. The rows carry no Session title — the route that serves them adds it from
   * the Session index, which this service does not hold.
   */
  abstract sessionDays(projectId: string, opts?: { days?: number }): UsageSessionDay[];
  abstract queryErrors(projectId: string, q: UsageErrorsQuery): UsageErrorsPage;
  abstract clearErrors(projectId: string, q: UsageErrorsClearQuery): number;
  abstract errorIgnores(projectId: string): UsageErrorIgnoreRule[];
  /** Adds one rule; null when the Project already has one for that class (the route answers 409). */
  abstract addErrorIgnore(
    projectId: string,
    input: ErrorIgnoreAddInput,
    range?: Omit<UsageErrorsClearQuery, "until" | "kind" | "includeGlobalErrors">,
  ): UsageErrorIgnoreAdded | null;
  abstract removeErrorIgnore(projectId: string, id: number): boolean;
  abstract modelTotals(projectId: string): UsageModelTotals;
  /** Fixes the cost of every usage row that has none yet; returns how many it settled. */
  abstract settleUnsettledCosts(): Promise<number>;
  /**
   * The Project's lifetime cost: every usage record it ever wrote, summed from the costs fixed
   * when the rows were written. `unpriced` says some of that usage ran on a model this Project
   * has no price for, so `cost` is a lower bound.
   */
  abstract lifetimeCost(
    projectId: string,
  ): Promise<Opaque<"LifetimeCost", { cost: number | null; unpriced: boolean }>>;
  abstract costBySession(
    projectId: string,
    sessionIds: readonly string[],
    fromTs: string,
    toTs: string,
  ): Promise<Opaque<"CostBySession", { bySession: Map<string, number>; unpriced: boolean }>>;
  abstract dailyCostForSessions(
    projectId: string,
    sessionIds: readonly string[],
    fromTs: string,
    toTs: string,
  ): Promise<Array<{ date: string; cost: number }>>;
}