2335 lines · 89.0 KB · TypeScript
RawDownload
/** * ProviderServiceLive - Cross-provider orchestration layer. * * Routes validated transport/API calls to provider adapters through * `ProviderAdapterRegistry` and `ProviderSessionDirectory`, and exposes a * unified provider event stream for subscribers. * * It does not implement provider protocol details (adapter concern). * * @module ProviderServiceLive */import {  EventId,  MessageId,  ModelSelection,  NonNegativeInt,  ProviderInterruptTurnInput,  ProviderRespondToRequestInput,  ProviderRespondToUserInputInput,  RuntimeRequestId,  ProviderSendTurnInput,  type ChatImageAttachment,  type SnapShotAccessibility,  type SnapShotAccessibilityNode,  PROVIDER_SEND_TURN_MAX_INPUT_CHARS,  ProviderSessionStartInput,  ProviderStopSessionInput,  ProviderUploadFeedbackInput,  ThreadId,  TurnId,  type ProviderInstanceId,  type ProviderDriverKind,  type ProviderRuntimeEvent,  type ProviderSession,} from "@t3tools/contracts";import { expandAssistantCitationsForProvider } from "@t3tools/shared/assistantCitations";import { HostProcessPlatform } from "@t3tools/shared/hostProcess";import { causeErrorTag } from "@t3tools/shared/observability";import { getModelSelectionStringOptionValue } from "@t3tools/shared/model";import { resolveProjectAgentBrowserAccess } from "@t3tools/shared/serverSettings";import * as DateTime from "effect/DateTime";import * as Deferred from "effect/Deferred";import * as Effect from "effect/Effect";import * as FileSystem from "effect/FileSystem";import * as Layer from "effect/Layer";import * as Option from "effect/Option";import * as Path from "effect/Path";import * as PubSub from "effect/PubSub";import * as Ref from "effect/Ref";import * as Schema from "effect/Schema";import * as SchemaIssue from "effect/SchemaIssue";import * as Stream from "effect/Stream"; import { appendUserInputAttachmentPaths } from "../userInputAttachments.ts";import { resolveAttachmentPath } from "../../attachmentStore.ts";import * as ServerConfig from "../../config.ts";import * as DeviceService from "../../device/DeviceService.ts";import { ensureAgentDeviceShim } from "../../device/AgentDeviceShim.ts";import type * as McpInvocationContext from "../../mcp/McpInvocationContext.ts";import {  increment,  providerMetricAttributes,  providerRuntimeEventsTotal,  providerSessionsTotal,  providerTurnDuration,  providerTurnsTotal,  providerTurnMetricAttributes,  withMetrics,} from "../../observability/Metrics.ts";import {  ProviderAdapterRequestError,  type ProviderAdapterError,  ProviderValidationError,  ProviderWorkspaceMissingError,} from "../Errors.ts";import type { ProviderAdapterShape } from "../Services/ProviderAdapter.ts";import * as ProviderAdapterRegistry from "../Services/ProviderAdapterRegistry.ts";import * as ProviderService from "../Services/ProviderService.ts";import * as ProviderSessionDirectory from "../Services/ProviderSessionDirectory.ts";import { type EventNdjsonLogger } from "./EventNdjsonLogger.ts";import * as ProviderEventLoggers from "./ProviderEventLoggers.ts";import * as AnalyticsService from "../../telemetry/AnalyticsService.ts";import * as McpProviderSession from "../../mcp/McpProviderSession.ts";import * as McpSessionRegistry from "../../mcp/McpSessionRegistry.ts";import * as ServerSettings from "../../serverSettings.ts";import * as ProjectionSnapshotQuery from "../../orchestration/Services/ProjectionSnapshotQuery.ts";const isModelSelection = Schema.is(ModelSelection);const encodePromptJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown)); interface SnapShotPromptAccessibilityNode {  readonly role: string;  readonly name?: string;  readonly value?: string;  readonly description?: string;  readonly bounds?: NonNullable<SnapShotAccessibilityNode["bounds"]>;  readonly state?: SnapShotAccessibilityNode["state"];  readonly actions?: ReadonlyArray<string>;  readonly children?: ReadonlyArray<SnapShotPromptAccessibilityNode>;} type SnapShotPromptAccessibility =  | {      readonly format: "flat-text";      readonly text: string;      readonly truncated?: true;    }  | {      readonly format: "element-tree";      readonly coordinateSpace?: "captured-image";      readonly imageSize?: { readonly width: number; readonly height: number };      readonly truncated?: true;      readonly root: SnapShotPromptAccessibilityNode;    }; function normalizedAccessibilityLabel(value: string): string {  return value.trim().replaceAll(/\s+/g, " ").toLowerCase();} function isRedundantWindowButtonDescription(node: SnapShotAccessibilityNode): boolean {  if (node.role !== "button" || !node.name || !node.description) return false;  return (    normalizedAccessibilityLabel(node.description) ===    `${normalizedAccessibilityLabel(node.name)} the window`  );} function isFullImageBounds(  bounds: NonNullable<SnapShotAccessibilityNode["bounds"]>,  imageSize: { readonly width: number; readonly height: number },): boolean {  return (    bounds.x === 0 &&    bounds.y === 0 &&    bounds.width === imageSize.width &&    bounds.height === imageSize.height  );} function compactAccessibilityNodeForPrompt(  node: SnapShotAccessibilityNode,  imageSize: { readonly width: number; readonly height: number },  options: { readonly isRoot: boolean; readonly parentName?: string },): ReadonlyArray<SnapShotPromptAccessibilityNode> {  const bounds =    node.bounds && !(options.isRoot && isFullImageBounds(node.bounds, imageSize))      ? node.bounds      : undefined;  const name = node.role !== "group" && node.name === options.parentName ? undefined : node.name;  const description = isRedundantWindowButtonDescription(node) ? undefined : node.description;  const actions = node.actions?.filter((action) => node.role !== "button" || action !== "press");  const children = node.children.flatMap((child) =>    compactAccessibilityNodeForPrompt(child, imageSize, {      isRoot: false,      ...(node.name        ? { parentName: node.name }        : options.parentName          ? { parentName: options.parentName }          : {}),    }),  );  const compacted: SnapShotPromptAccessibilityNode = {    role: node.role,    ...(name ? { name } : {}),    ...(node.value ? { value: node.value } : {}),    ...(description ? { description } : {}),    ...(bounds ? { bounds } : {}),    ...(node.state ? { state: node.state } : {}),    ...(actions && actions.length > 0 ? { actions } : {}),    ...(children.length > 0 ? { children } : {}),  };   const hasMetadata = Boolean(    compacted.name ||    compacted.value ||    compacted.description ||    compacted.bounds ||    compacted.state ||    compacted.actions,  );  if (!options.isRoot && node.role === "group" && !hasMetadata) return children;  if (    !options.isRoot &&    (node.role === "separator" || node.role === "tab_group") &&    !hasMetadata &&    children.length === 0  ) {    return [];  }  if (    !options.isRoot &&    node.role === "static_text" &&    node.name === options.parentName &&    !hasMetadata &&    children.length === 0  ) {    return [];  }  return [compacted];} function accessibilityNodeHasBounds(node: SnapShotPromptAccessibilityNode): boolean {  return Boolean(node.bounds || node.children?.some(accessibilityNodeHasBounds));} function compactAccessibilityForPrompt(  accessibility: SnapShotAccessibility,): SnapShotPromptAccessibility {  if (accessibility.format === "flat-text") {    return {      format: "flat-text",      text: accessibility.text,      ...(accessibility.truncated ? { truncated: true } : {}),    };  }   const root = compactAccessibilityNodeForPrompt(accessibility.root, accessibility.imageSize, {    isRoot: true,  })[0]!;  const hasBounds = accessibilityNodeHasBounds(root);  return {    format: "element-tree",    ...(hasBounds      ? { coordinateSpace: accessibility.coordinateSpace, imageSize: accessibility.imageSize }      : {}),    ...(accessibility.truncated ? { truncated: true } : {}),    root,  };} /** How long a manual context compaction may run before ProviderService gives up on it. */const COMPACTION_COMPLETION_TIMEOUT = "10 minutes"; interface PendingCompaction {  readonly completion: Deferred.Deferred<string>;  readonly native: boolean;  readonly providerInstanceId: ProviderInstanceId;  readonly requestId: MessageId | undefined;  readonly earlyEvents: ProviderRuntimeEvent[];  compactedEventObserved: boolean;  expectedTurnId: TurnId | undefined;} /** * Hook for tests that want to override the canonical event logger pulled * from `ProviderEventLoggers`. Production wiring leaves this undefined and * reads the logger off the tag. */export interface ProviderServiceLiveOptions {  readonly canonicalEventLogger?: EventNdjsonLogger;  /**   * Overrides MCP credential issuance. The real issuer reads a module-global   * registry that only a running MCP server installs, which makes the   * agent-browser-access gate unobservable from a unit test; this seam lets a   * test see whether a credential was requested at all.   */  readonly issueMcpCredential?: typeof McpSessionRegistry.issueActiveMcpCredential;} interface TurnAnalyticsMetadata {  readonly requestId: number;  readonly provider: ProviderDriverKind;  readonly startedAtMs: number;  readonly mixedModels: boolean;  readonly model?: string;  readonly effort?: string;  readonly interactionMode?: string;  readonly runtimeMode?: string;} interface ActiveTurnAnalytics {  readonly metadata: TurnAnalyticsMetadata;  readonly requestAssociated: boolean;} interface DeferredTurnAnalyticsCompletion {  readonly completionKey: string;  readonly completedAtMs: number;  readonly terminalProperties: Readonly<Record<string, unknown>>;} interface TurnAnalyticsSessionState {  readonly pendingByRequestId: Map<number, TurnAnalyticsMetadata>;  readonly activeByTurnId: Map<string, ActiveTurnAnalytics>;  readonly deferredCompletionsByTurnId: Map<string, DeferredTurnAnalyticsCompletion>;} interface TurnAnalyticsState {  readonly sessions: Map<string, TurnAnalyticsSessionState>;  readonly completedKeys: Set<string>;  readonly completedOrder: Array<string>;} const MAX_COMPLETED_TURN_ANALYTICS_KEYS = 512;const MAX_ACTIVE_TURN_ANALYTICS_PER_SESSION = 8; function setActiveTurnAnalytics(  session: TurnAnalyticsSessionState,  turnId: string,  active: ActiveTurnAnalytics,): void {  session.activeByTurnId.set(turnId, active);  while (session.activeByTurnId.size > MAX_ACTIVE_TURN_ANALYTICS_PER_SESSION) {    const oldestTurnId = session.activeByTurnId.keys().next().value;    if (oldestTurnId === undefined) return;    session.activeByTurnId.delete(oldestTurnId);  }} function turnAnalyticsSessionKey(instanceId: ProviderInstanceId, threadId: ThreadId): string {  return `${String(instanceId)}\u0000${String(threadId)}`;} function turnAnalyticsCompletionKey(  instanceId: ProviderInstanceId,  threadId: ThreadId,  turnId: string,): string {  return `${turnAnalyticsSessionKey(instanceId, threadId)}\u0000${turnId}`;} function turnEffort(modelSelection: ProviderSendTurnInput["modelSelection"]): string | undefined {  return (    getModelSelectionStringOptionValue(modelSelection, "reasoningEffort") ??    getModelSelectionStringOptionValue(modelSelection, "effort")  );} type ProviderServiceMethod<Name extends keyof ProviderService.ProviderService["Service"]> =  ProviderService.ProviderService["Service"][Name]; const ProviderRollbackConversationInput = Schema.Struct({  threadId: ThreadId,  numTurns: NonNegativeInt,}); function toValidationError(  operation: string,  issue: string,  cause?: unknown,): ProviderValidationError {  return new ProviderValidationError({    operation,    issue,    ...(cause !== undefined ? { cause } : {}),  });} const decodeInputOrValidationError = <S extends Schema.Top>(input: {  readonly operation: string;  readonly schema: S;  readonly payload: unknown;}) => {  const decodeProviderRequestInput = Schema.decodeUnknownEffect(input.schema);  return decodeProviderRequestInput(input.payload).pipe(    Effect.mapError(      (schemaError) =>        new ProviderValidationError({          operation: input.operation,          issue: SchemaIssue.makeFormatterDefault()(schemaError.issue),          cause: schemaError,        }),    ),  );}; function toRuntimeStatus(session: ProviderSession): "starting" | "running" | "stopped" | "error" {  switch (session.status) {    case "connecting":      return "starting";    case "error":      return "error";    case "closed":      return "stopped";    case "ready":    case "running":    default:      return "running";  }} function toRuntimePayloadFromSession(  session: ProviderSession,  extra?: {    readonly modelSelection?: unknown;    readonly continueAfterServerUpdate?: TurnId;    readonly lastRuntimeEvent?: string;    readonly lastRuntimeEventAt?: string;  },): Record<string, unknown> {  return {    cwd: session.cwd ?? null,    model: session.model ?? null,    activeTurnId: session.activeTurnId ?? null,    lastError: session.lastError ?? null,    ...(extra?.continueAfterServerUpdate !== undefined      ? { continueAfterServerUpdate: extra.continueAfterServerUpdate }      : {}),    ...(extra?.modelSelection !== undefined ? { modelSelection: extra.modelSelection } : {}),    ...(extra?.lastRuntimeEvent !== undefined ? { lastRuntimeEvent: extra.lastRuntimeEvent } : {}),    ...(extra?.lastRuntimeEventAt !== undefined      ? { lastRuntimeEventAt: extra.lastRuntimeEventAt }      : {}),  };} function readPersistedModelSelection(  runtimePayload: ProviderSessionDirectory.ProviderRuntimeBinding["runtimePayload"],): ModelSelection | undefined {  if (!runtimePayload || typeof runtimePayload !== "object" || Array.isArray(runtimePayload)) {    return undefined;  }  const raw = "modelSelection" in runtimePayload ? runtimePayload.modelSelection : undefined;  return isModelSelection(raw) ? raw : undefined;} function readPersistedCwd(  runtimePayload: ProviderSessionDirectory.ProviderRuntimeBinding["runtimePayload"],): string | undefined {  if (!runtimePayload || typeof runtimePayload !== "object" || Array.isArray(runtimePayload)) {    return undefined;  }  const rawCwd = "cwd" in runtimePayload ? runtimePayload.cwd : undefined;  if (typeof rawCwd !== "string") return undefined;  const trimmed = rawCwd.trim();  return trimmed.length > 0 ? trimmed : undefined;} const dieOnMissingBindingInstanceId = (  operation: string,  payload: {    readonly providerInstanceId?: ProviderInstanceId | undefined;    readonly provider?: ProviderDriverKind | undefined;  },): ProviderInstanceId => {  if (payload.providerInstanceId !== undefined) {    return payload.providerInstanceId;  }  throw new Error(    payload.provider      ? `${operation}: provider instance id is required for provider '${payload.provider}'.`      : `${operation}: provider instance id is required.`,  );}; const correlateRuntimeEventWithInstance = (  source: {    readonly instanceId: ProviderInstanceId;    readonly provider: ProviderDriverKind;  },  event: ProviderRuntimeEvent,): ProviderRuntimeEvent => {  if (event.provider !== source.provider) {    throw new Error(      `ProviderService.streamEvents: provider instance '${source.instanceId}' is backed by driver '${source.provider}' but emitted driver '${event.provider}'.`,    );  }  if (event.providerInstanceId !== undefined && event.providerInstanceId !== source.instanceId) {    throw new Error(      `ProviderService.streamEvents: provider instance '${source.instanceId}' emitted event for instance '${event.providerInstanceId}'.`,    );  }  return { ...event, providerInstanceId: source.instanceId };}; const makeProviderService = Effect.fn("makeProviderService")(function* (  options?: ProviderServiceLiveOptions,) {  const analytics = yield* Effect.service(AnalyticsService.AnalyticsService);  const serverConfig = yield* ServerConfig.ServerConfig;  const eventLoggers = yield* ProviderEventLoggers.ProviderEventLoggers;  // Options-provided logger wins (test overrides); otherwise we take whatever  // the `ProviderEventLoggers` tag exposes — `undefined` means "no canonical  // log writer is attached", which downstream code already handles as a  // no-op.  const canonicalEventLogger = options?.canonicalEventLogger ?? eventLoggers.canonical;   const registry = yield* ProviderAdapterRegistry.ProviderAdapterRegistry;  const directory = yield* ProviderSessionDirectory.ProviderSessionDirectory;  const serverSettings = yield* ServerSettings.ServerSettingsService;  const projectionQuery = yield* Effect.serviceOption(    ProjectionSnapshotQuery.ProjectionSnapshotQuery,  );  const issueMcpCredential =    options?.issueMcpCredential ?? McpSessionRegistry.issueActiveMcpCredential;  const fileSystem = yield* FileSystem.FileSystem;  const pathService = yield* Path.Path;  const runtimeEventPubSub = yield* PubSub.unbounded<ProviderRuntimeEvent>();  const pendingCompactions = new Map<ThreadId, PendingCompaction>();  const timedOutNativeCompactions = new Set<ThreadId>();  const settleCompaction = (threadId: ThreadId, pending: PendingCompaction, terminal: string) =>    Effect.gen(function* () {      if (pendingCompactions.get(threadId) !== pending) return false;      pendingCompactions.delete(threadId);      yield* Deferred.succeed(pending.completion, terminal);      return true;    });  const turnAnalytics = yield* Ref.make<TurnAnalyticsState>({    sessions: new Map(),    completedKeys: new Set(),    completedOrder: [],

Lines 1–500 of 2335.