//#region src/experimental/channels/surface.d.ts
/**
 * A JSON value safe to persist across Worker and Durable Object boundaries.
 *
 * The recursive arms are interfaces rather than inline type literals so that
 * TypeScript defers resolving them. Workers RPC checks a return type with
 * `R extends Rpc.Serializable<R>`, which eagerly expands a self-referential
 * type alias and fails with "Type instantiation is excessively deep". Deferred
 * arms let a surface be returned from a Durable Object method.
 */
type ChannelSurfaceValue =
  | null
  | boolean
  | number
  | string
  | ChannelSurfaceArray
  | ChannelSurfaceObject;
interface ChannelSurfaceArray extends ReadonlyArray<ChannelSurfaceValue> {}
interface ChannelSurfaceObject {
  readonly [key: string]: ChannelSurfaceValue;
}
/** A durable configured destination for one outbound message attempt. */
type ChannelMessageSurface<
  TChannelKey extends string = string,
  TAddress extends ChannelSurfaceValue = ChannelSurfaceValue
> = Readonly<{
  channelKey: TChannelKey;
  version: 1;
  address: TAddress /** Human-readable destination text captured when the surface is created. */;
  label: string;
}>;
/** A Channel-produced destination before its Host stamps the configured key. */
type ChannelMessageSurfaceInput<
  TAddress extends ChannelSurfaceValue = ChannelSurfaceValue
> = Readonly<{
  version: 1;
  address: TAddress;
  label: string;
}>;
/** Validate the common envelope before provider-specific address parsing. */
declare function isChannelMessageSurface(
  value: unknown
): value is ChannelMessageSurface;
//#endregion
//#region src/experimental/channels/protocol.d.ts
type Json = null | boolean | number | string | JsonArray | JsonObject;
interface JsonArray extends ReadonlyArray<Json> {}
interface JsonObject {
  readonly [key: string]: Json;
}
type Participant = Readonly<{
  /** Stable within the conversation. What the agent compares for ownership. */ id: string;
  name?: string;
}>;
type TranscriptMessage = {
  id: string;
  role: "system" | "user" | "assistant";
  parts: MessagePart[];
  metadata?: JsonObject;
};
type MessagePart =
  | {
      type: "text";
      text: string;
      providerMetadata?: JsonObject;
    }
  | {
      type: "reasoning";
      text: string;
      providerMetadata?: JsonObject;
    }
  | {
      type: "file";
      mediaType: string;
      url: string;
      filename?: string;
      providerMetadata?: JsonObject;
    }
  | {
      type: "source-url";
      sourceId: string;
      url: string;
      title?: string;
      providerMetadata?: JsonObject;
    }
  | {
      type: "source-document";
      sourceId: string;
      mediaType: string;
      title: string;
      filename?: string;
      providerMetadata?: JsonObject;
    }
  | {
      type: "data";
      name: string;
      id?: string;
      data: Json;
    }
  | ToolPart;
type ToolState =
  | "input-streaming"
  | "input-available"
  | "approval-requested"
  | "approval-responded"
  | "output-available"
  | "output-error"
  | "output-denied";
type ToolPart = {
  type: "tool";
  toolCallId: string;
  toolName: string;
  state: ToolState;
  input?: Json;
  output?: Json;
  errorText?: string /** Output so far; a later output replaces it. */;
  preliminary?: boolean;
  approval?: {
    id: string;
    approved?: boolean;
    reason?: string;
  } /** Id of the participant who may run this client tool call. Absent for server tools. */;
  owner?: string;
  providerExecuted?: boolean;
  dynamic?: boolean;
  title?: string;
  providerMetadata?: JsonObject;
};
type ResponseChunk =
  | {
      type: "text-start";
      id: string;
      providerMetadata?: JsonObject;
    }
  | {
      type: "text-delta";
      id: string;
      delta: string;
    }
  | {
      type: "text-end";
      id: string;
      providerMetadata?: JsonObject;
    }
  | {
      type: "reasoning-start";
      id: string;
      providerMetadata?: JsonObject;
    }
  | {
      type: "reasoning-delta";
      id: string;
      delta: string;
    }
  | {
      type: "reasoning-end";
      id: string;
      providerMetadata?: JsonObject;
    }
  | {
      type: "tool-input-start";
      toolCallId: string;
      toolName: string;
      owner?: string;
      providerExecuted?: boolean;
      dynamic?: boolean;
      title?: string;
    }
  | {
      type: "tool-input-delta";
      toolCallId: string;
      delta: string;
    }
  | {
      type: "tool-input-available";
      toolCallId: string;
      toolName: string;
      input: Json;
      owner?: string;
      providerExecuted?: boolean;
      dynamic?: boolean;
      title?: string;
      providerMetadata?: JsonObject;
    }
  | {
      type: "tool-input-error";
      toolCallId: string;
      toolName: string;
      input: Json;
      errorText: string;
      providerExecuted?: boolean;
      dynamic?: boolean;
      title?: string;
    }
  | {
      type: "tool-approval-request";
      toolCallId: string;
      approvalId: string;
    }
  | {
      type: "tool-output-available";
      toolCallId: string;
      output: Json;
      preliminary?: boolean;
      providerExecuted?: boolean;
    }
  | {
      type: "tool-output-error";
      toolCallId: string;
      errorText: string;
      providerExecuted?: boolean;
    }
  | {
      type: "tool-output-denied";
      toolCallId: string;
    }
  | Extract<
      MessagePart,
      {
        type: "file" | "source-url" | "source-document";
      }
    >
  | {
      type: "data";
      name: string;
      id?: string;
      data: Json;
      transient?: boolean;
    } /** Merged into the metadata of the message this response builds. */
  | {
      type: "metadata";
      metadata: JsonObject;
    };
type TurnOutcome = "completed" | "failed" | "aborted" | "awaiting-input";
type TurnStatusBase = {
  turnId: string /** Event id of the inbound message that started the turn. */;
  startedBy: string;
};
type TurnStatus = TurnStatusBase &
  (
    | {
        status: "queued";
      }
    | {
        status: "running";
        responseId: string /** Saved assistant message the response continues. */;
        extends?: string;
      }
    | {
        status: "settled";
        outcome: Exclude<TurnOutcome, "failed">;
        messageIds: string[];
      }
    | {
        status: "settled";
        outcome: "failed";
        messageIds: string[] /** Shown to participants. Channels sends a generic message when absent. */;
        error?: string;
      }
  );
type ConversationSnapshot = {
  messages: TranscriptMessage[] /** Every turn that is queued, running or awaiting input. */;
  turns: TurnStatus[];
};
/** Tool a participant's client can run, advertised with an inbound event. */
type ClientTool = {
  name: string;
  description?: string;
  inputSchema?: JsonObject;
};
/** What an interface sends. */
type InboundEvent = {
  /** Stable across redelivery of the same event. */ eventId: string;
} & (
  | {
      type: "message" /** Save it under this id. */;
      message: TranscriptMessage & {
        role: "user";
      };
      clientTools?: ClientTool[];
    }
  | {
      type: "tool-result";
      turnId: string;
      toolCallId: string;
      result:
        | {
            ok: true;
            output: Json;
          }
        | {
            ok: false;
            errorText?: string;
          };
      clientTools?: ClientTool[];
    }
  | {
      type: "approval-response";
      turnId: string;
      approvalId: string;
      approved: boolean;
      reason?: string;
    }
  | {
      type: "cancel";
      turnId: string;
    } /** Start a new, empty conversation. The sending surface follows it. */
  | {
      type: "conversation-create";
    }
  /**
   * Start a new conversation with this one's transcript so far. The sending
   * surface follows it; other surfaces stay.
   */
  | {
      type: "conversation-fork";
    }
  /**
   * Start this conversation over, keeping its id. The agent may carry a
   * handoff note into the new context.
   */
  | {
      type: "conversation-reset";
      handoff?: string;
    }
);
/** Inbound events that act on a conversation rather than in it. */
type ConversationOperation = Extract<
  InboundEvent["type"],
  "conversation-create" | "conversation-fork" | "conversation-reset"
>;
/** One of an agent's conversations, as a client lists them. */
type ConversationInfo = {
  id: string /** The conversation this one was forked from. */;
  parent?: string /** True while a turn is running or queued. */;
  busy: boolean;
};
/** What an accepted inbound event produced, for the surface that sent it. */
type DispatchResult = {
  /** The conversation a create or fork started; the sender follows it. */ conversationId?: string;
};
/** Where an inbound event came from, stamped by Channels, never by the client. */
type EventOrigin = {
  conversationId: string;
  participant: Participant;
  surface: ChannelMessageSurface;
};
/**
 * Where an inbound event came from, as the gateway knows it: the route that
 * picked the agent, and the conversation when the route named one. Channels
 * resolves an unnamed conversation to the agent's default: the route itself,
 * or a harness's default session.
 */
type GatewayOrigin = Omit<EventOrigin, "conversationId"> & {
  route: string;
  conversationId?: string;
};
//#endregion
export {
  ChannelSurfaceObject as C,
  ChannelSurfaceArray as S,
  isChannelMessageSurface as T,
  TranscriptMessage as _,
  DispatchResult as a,
  ChannelMessageSurface as b,
  InboundEvent as c,
  JsonObject as d,
  MessagePart as f,
  ToolState as g,
  ToolPart as h,
  ConversationSnapshot as i,
  Json as l,
  ResponseChunk as m,
  ConversationInfo as n,
  EventOrigin as o,
  Participant as p,
  ConversationOperation as r,
  GatewayOrigin as s,
  ClientTool as t,
  JsonArray as u,
  TurnOutcome as v,
  ChannelSurfaceValue as w,
  ChannelMessageSurfaceInput as x,
  TurnStatus as y
};
//# sourceMappingURL=protocol-CukuI73M.d.ts.map
