import {
  b as ChannelMessageSurface,
  m as ResponseChunk,
  p as Participant,
  x as ChannelMessageSurfaceInput
} from "./protocol-CukuI73M.js";

//#region src/experimental/channels/identity.d.ts
type ChannelIdentity = Readonly<{
  /** Configured Channel that observed this identity. */ channelKey: string /** Stable Channel namespace in which `subject` is unique. @default "default" */;
  scope?: string /** Stable Channel subject within `scope`. */;
  subject: string;
}>;
/** A Channel-produced identity before its Host stamps the configured key. */
type ChannelIdentityInput = Readonly<{
  scope?: string;
  subject: string;
}>;
/** Build a stable key for comparing or indexing a Channel identity. */
declare function identityKey(identity: ChannelIdentity): string;
//#endregion
//#region src/experimental/channels/ingress.d.ts
/** Serializable provider attachment facts available after ingress returns. */
type ChannelAttachment = {
  id?: string;
  mediaType?: string;
  name?: string;
  size?: number;
  text?: string;
  url?: string;
};
type ChannelActor = {
  readonly id: string /** Stable Channel identity suitable for explicit application linking. */;
  readonly identity?: ChannelIdentity;
  readonly username?: string;
  readonly fullName?: string;
  readonly isBot?: boolean | "unknown";
  readonly isSelf?: boolean;
};
/** An actor before the Host stamps its configured Channel key. */
type ChannelActorInput = Omit<ChannelActor, "identity"> & {
  readonly identity?: ChannelIdentityInput;
};
type ChannelEventContext = {
  /** Immutable Channel-scoped identity, independent of application routing. */ readonly eventId: string;
  readonly thread: {
    readonly id: string;
    readonly isDirectMessage: boolean | "unknown";
  } /** Exact provider destination for responding to this inbound event. */;
  readonly replySurface?: ChannelMessageSurface;
  readonly actor?: ChannelActor;
};
/** A normalized inbound message from a Channel. */
type ChannelInboundMessage = ChannelEventContext & {
  readonly type: "message";
  readonly message: {
    readonly id: string;
    readonly text: string;
    readonly title?: string;
    readonly markdown?: string;
    readonly attachments?: readonly ChannelAttachment[];
    readonly isMention?: boolean;
    readonly reply?: {
      readonly id: string;
      readonly text?: string;
    };
    readonly metadata?: {
      /** ISO 8601 timestamp supplied by the provider. */ readonly sentAt?: string;
      readonly edited?: boolean /** ISO 8601 timestamp supplied by the provider. */;
      readonly editedAt?: string;
      readonly autoReply?: boolean;
    };
  };
};
/** A provider-normalized response to an external approval request. */
type ChannelApprovalResponse = ChannelEventContext & {
  readonly type: "approval-response";
  readonly approvalId: string;
  readonly decision:
    | "approve"
    | "reject" /** Provider reference for this inbound response. */;
  readonly reference: string;
};
type ChannelIngressEvent = ChannelInboundMessage | ChannelApprovalResponse;
/** A normalized message before the Host stamps its configured Channel key. */
type ChannelEventContextInput = Omit<
  ChannelEventContext,
  "replySurface" | "actor"
> & {
  readonly replySurface?: ChannelMessageSurfaceInput;
  readonly actor?: ChannelActorInput;
};
type ChannelInboundMessageInput = Omit<
  ChannelInboundMessage,
  "replySurface" | "actor"
> & {
  readonly replySurface?: ChannelMessageSurfaceInput;
  readonly actor?: ChannelActorInput;
};
/** A normalized approval before the Host stamps its configured Channel key. */
type ChannelApprovalResponseInput = Omit<
  ChannelApprovalResponse,
  "replySurface" | "actor"
> & {
  readonly replySurface?: ChannelMessageSurfaceInput;
  readonly actor?: ChannelActorInput;
};
/** Authenticated event produced by a Channel before Host dispatch. */
type ChannelIngressEventInput =
  | ChannelInboundMessageInput
  | ChannelApprovalResponseInput;
/** Authenticated Channel output retained only until Host routing completes. */
type ChannelIngressEnvelope<TRaw = unknown> = {
  event: ChannelIngressEventInput;
  raw: TRaw;
};
/** The normalized envelopes and provider acknowledgement produced by ingress. */
type ChannelIngressResult<TRaw = unknown> = {
  events: readonly ChannelIngressEnvelope<TRaw>[];
  response: Response;
};
/** Match an HTTP request to one exact URL pathname. */
declare function matchesPath(request: Request, path: string): boolean;
/** Webhook-shaped inbound support owned by a Channel. */
interface ChannelIngress<TRaw = unknown> {
  /** Return null when this Channel does not own the request. */
  receive(request: Request): Promise<ChannelIngressResult<TRaw> | null>;
}
/** Structural input supported by Workers Email and Agent email handlers. */
type ChannelEmailInput = {
  from: string;
  to: string;
  headers: Headers;
  raw?: ReadableStream<Uint8Array>;
  getRaw?: () => Promise<Uint8Array>;
};
type ChannelEmailIngressResult<TRaw = unknown> = {
  events: readonly ChannelIngressEnvelope<TRaw>[];
};
/** Non-HTTP ingress for a Workers Email event. */
interface ChannelEmailIngress<TRaw = unknown> {
  /** Return null when this Channel does not own the email. */
  receive(
    email: ChannelEmailInput
  ): Promise<ChannelEmailIngressResult<TRaw> | null>;
}
//#endregion
//#region src/experimental/channels/channel.d.ts
type Awaitable<T> = T | Promise<T>;
/** A transport-neutral outbound message whose canonical content is Markdown. */
type ChannelMessage = {
  /** Optional topic. Each transport decides how to represent it. */ title?: string /** Canonical Markdown content. */;
  markdown: string;
};
/** A transport failure safe to expose to an AI model. */
type DeliveryFailure = {
  code: string;
  message: string;
};
/**
 * The result of a direct delivery attempt, defined by what reached the reader.
 *
 * `delivered` means the whole message reached the reader, not that a person
 * read it. `failed` means none of it did; its `retryable` field says whether
 * the same route can be attempted again. `uncertain` means an unknown amount
 * of the message reached the reader, so another attempt or route could
 * duplicate content. A stream that ends before its answer is complete is
 * `uncertain`, and carries a `reference` when the Channel created something
 * the caller can point at.
 */
type DeliveryResult =
  | {
      status: "delivered";
      reference?: string;
    }
  | {
      status: "failed";
      retryable: boolean;
      error: DeliveryFailure;
    }
  | {
      status: "uncertain";
      reference?: string;
      error: DeliveryFailure;
    };
/** A response's chunks, as `ChannelGateway.stream` takes them. */
type ChannelChunkSource = ReadableStream<ResponseChunk>;
/** Caller options for one finished delivery. */
type ChannelDeliveryOptions = {
  /** Caller-owned correlation an Adapter may use where the provider supports it. */ delivery?: ChannelDeliveryContext;
};
/** Caller options for one streamed answer. */
type ChannelStreamOptions = {
  /**
   * Optional topic. It is an option rather than a chunk because it is known
   * before the first token, and a Channel usually needs it in its opening
   * provider call.
   */
  title?: string /** Caller-owned correlation an Adapter may use where the provider supports it. */;
  delivery?: ChannelDeliveryContext;
};
/**
 * Caller-owned correlation supplied to one provider delivery attempt.
 *
 * This is not an idempotency guarantee. An Adapter may map it to a provider
 * idempotency primitive when one exists, or otherwise use it for observability.
 */
type ChannelDeliveryContext = {
  deliveryId: string;
};
/**
 * Who a sender is, as the application decides it. A string is shorthand
 * for `{ id }`; `null` refuses the sender. Participants that share an id
 * are the same participant.
 */
type ParticipantResult = Participant | string | null;
/**
 * Resolve who sent an authenticated event. Channels has no opinion on
 * identity: this is the only place a webhook sender becomes a participant.
 */
type ChannelParticipant<TRaw = unknown> = (
  event: ChannelIngressEvent,
  raw: TRaw
) => Awaitable<ParticipantResult>;
/**
 * Pick the agent object an event reaches, or return null to ignore it.
 *
 * The agent object is the authorization boundary: anyone who reaches one
 * may use every conversation in it. Default: an agent object of the
 * participant's own (`routes.perParticipant`).
 */
type ChannelRoute<TRaw = unknown> = (
  event: ChannelIngressEvent,
  raw: TRaw,
  participant: Participant
) => Awaitable<string | null>;
/** A WebSocket upgrade's conversation, once it is known to be for Channels. */
type ChannelUpgradeMatch = {
  /** The conversation to follow. Default: the agent's default conversation. */ conversationId?: string;
};
/** How a Channel takes WebSocket upgrades in the gateway. */
interface ChannelUpgrade {
  /** Return undefined for an upgrade that is not for this Channel. */
  match(request: Request): ChannelUpgradeMatch | undefined;
  /** Who is connecting. Return null to refuse the upgrade. */
  participant(request: Request): Awaitable<ParticipantResult>;
  /** The agent object the upgrade reaches, or null to refuse it. */
  route(request: Request, participant: Participant): Awaitable<string | null>;
}
/** A configured delivery route with optional ingress support. */
interface Channel<TRaw = unknown> {
  /**
   * Who sent an ingress event. Required when the Channel has `ingress` or
   * `emailIngress`; the gateway refuses to start without it.
   */
  participant?(
    event: ChannelIngressEvent,
    raw: TRaw
  ): Awaitable<ParticipantResult>;
  /** Pick the agent object an event reaches. See `ChannelRoute`. */
  route?(
    event: ChannelIngressEvent,
    raw: TRaw,
    participant: Participant
  ): Awaitable<string | null>;
  /** Derive a direct destination from this configured Channel's identity. */
  contactSurface?(identity: ChannelIdentity): ChannelMessageSurfaceInput | null;
  /**
   * Deliver one finished message. Only for Channels that cannot stream:
   * the gateway delivers to a Channel that can by streaming the message.
   */
  deliver?(
    surface: ChannelMessageSurface,
    message: ChannelMessage,
    options?: ChannelDeliveryOptions
  ): Promise<DeliveryResult>;
  /**
   * Deliver one progressively generated answer. Absent for Channels that
   * cannot stream, which the Host serves by collecting and calling `deliver`.
   *
   * The Channel owns the consumption loop. It must finalize whether the
   * stream closed or errored, because a model can fail mid-generation, and it
   * must not abandon a terminal provider call on error.
   */
  stream?(
    surface: ChannelMessageSurface,
    chunks: ChannelChunkSource,
    options: ChannelStreamOptions
  ): Promise<DeliveryResult>;
  readonly ingress?: ChannelIngress<TRaw>;
  readonly emailIngress?: ChannelEmailIngress<TRaw>;
  /** WebSocket upgrades this Channel takes, such as the Web Channel's. */
  readonly upgrade?: ChannelUpgrade;
}
//#endregion
export {
  ChannelIngressEventInput as A,
  ChannelEventContext as C,
  ChannelIngress as D,
  ChannelInboundMessageInput as E,
  identityKey as F,
  matchesPath as M,
  ChannelIdentity as N,
  ChannelIngressEnvelope as O,
  ChannelIdentityInput as P,
  ChannelEmailInput as S,
  ChannelInboundMessage as T,
  ChannelApprovalResponse as _,
  ChannelDeliveryOptions as a,
  ChannelEmailIngress as b,
  ChannelRoute as c,
  ChannelUpgradeMatch as d,
  DeliveryFailure as f,
  ChannelActorInput as g,
  ChannelActor as h,
  ChannelDeliveryContext as i,
  ChannelIngressResult as j,
  ChannelIngressEvent as k,
  ChannelStreamOptions as l,
  ParticipantResult as m,
  Channel as n,
  ChannelMessage as o,
  DeliveryResult as p,
  ChannelChunkSource as r,
  ChannelParticipant as s,
  Awaitable as t,
  ChannelUpgrade as u,
  ChannelApprovalResponseInput as v,
  ChannelEventContextInput as w,
  ChannelEmailIngressResult as x,
  ChannelAttachment as y
};
//# sourceMappingURL=channel-Bak_6zDP.d.ts.map
