Skip to content

Open-source, local-first AI agent for real work.

3.6 KB
Raw Blame
import type { ToolPhase } from "@vetta/agent-core";
import type { AssistantMessageEvent, CacheUsageReporting } from "@vetta/ai";
import type { RuntimeEventSource, SessionError } from "./contracts.js";
import type { RuntimeFailure } from "./failure-contract.js";
import type { SessionExtensionObservation } from "./session-extensions/contracts.js";

export type RuntimeSessionLifecyclePhase =
	| "created"
	| "agent_start"
	| "turn_start"
	| "turn_end"
	| "agent_end"
	| "aborted";

interface RuntimeSessionObservationBase {
	readonly source: RuntimeEventSource;
	readonly timestamp?: number;
}

/**
 * Session 执行期间的瞬时观察事件。
 *
 * 该合同不依赖旧 coding-agent AgentSessionEvent,也不包含宿主生成的 eventId、
 * sessionId 与 schemaVersion。各生产执行后端都先适配到这里,
 * 再由 runtime-host 生成稳定的 SessionEvent。
 */
export type RuntimeSessionObservationEvent = RuntimeSessionObservationBase &
	(
		| { readonly type: "lifecycle"; readonly phase: RuntimeSessionLifecyclePhase }
		| { readonly type: "model.request.started"; readonly modelCallIndex: number }
		| {
				readonly type: "assistant.event";
				readonly modelCallIndex: number;
				readonly event: AssistantMessageEvent;
		  }
		| {
				readonly type: "tool.start";
				readonly toolCallId: string;
				readonly toolName: string;
				readonly args: unknown;
				readonly startedAt: number;
		  }
		| {
				readonly type: "tool.update";
				readonly toolCallId: string;
				readonly toolName: string;
				readonly partialResult: unknown;
		  }
		| {
				readonly type: "tool.phase";
				readonly toolCallId: string;
				readonly toolName: string;
				readonly label: string;
				readonly atMs: number;
		  }
		| {
				readonly type: "tool.end";
				readonly toolCallId: string;
				readonly toolName: string;
				readonly isError: boolean;
				readonly result: unknown;
				readonly startedAt: number;
				readonly durationMs: number;
				readonly phases: readonly ToolPhase[];
		  }
		| {
				readonly type: "usage.update";
				readonly input: number;
				readonly output: number;
				readonly cacheRead: number;
				readonly cacheWrite: number;
				readonly cacheUsageReporting?: CacheUsageReporting;
				readonly model?: {
					readonly api: string;
					readonly provider: string;
					readonly id: string;
				};
				readonly costTotal: number;
				readonly contextPercent: number | null;
				readonly contextTokens?: number | null;
				readonly contextWindow: number;
		  }
		| { readonly type: "error"; readonly error: SessionError; readonly turnId?: string }
		| SessionExtensionObservation
		| {
				readonly type: "retry.start";
				readonly attempt: number;
				readonly maxAttempts: number;
				readonly delayMs: number;
				readonly errorMessage: string;
				readonly failure?: RuntimeFailure;
		  }
		| {
				readonly type: "retry.end";
				readonly success: boolean;
				readonly attempt: number;
				readonly finalError?: string;
				readonly failure?: RuntimeFailure;
		  }
		| { readonly type: "active_tools_update"; readonly activeToolNames: readonly string[] }
		| {
				readonly type: "compaction.start";
				readonly reason: "threshold" | "overflow" | "manual";
				readonly contextTokens?: number;
				readonly contextWindow?: number;
				readonly thresholdTokens?: number;
		  }
		| {
				readonly type: "compaction.end";
				readonly success: boolean;
				readonly reason?: "threshold" | "overflow" | "manual";
				readonly tokensBefore?: number;
				readonly contextPercent?: number | null;
				readonly contextTokens?: number | null;
				readonly contextWindow?: number;
				readonly errorMessage?: string;
				readonly failure?: RuntimeFailure;
		  }
	);