import { n as Stream, t as CapabilityFactory } from "./core-CSZf27yz.js";
import { StandardSchemaV1 } from "@standard-schema/spec";

//#region ../app/machine.d.ts
type Message<Type extends string = string, Params = unknown> = {
  type: Type;
  params: Params;
  actor?: string;
};
type Transition<State, Payload, Effect> = (state: State, payload: Payload) => State | [State, Effect];
interface Edge$1<State, Effect, S extends StandardSchemaV1 | undefined = StandardSchemaV1 | undefined> {
  readonly schema?: S;
  transition: Transition<State, any, Effect>;
}
//#endregion
//#region ../app/app.d.ts
type MaybePromise$1<T> = T | Promise<T>;
type Infer$1<S extends StandardSchemaV1> = StandardSchemaV1.InferOutput<S>;
interface ActionExample {
  params: Record<string, unknown>;
  description?: string;
}
interface ActionMeta {
  kind: 'handler' | 'procedure';
  audience?: ProcedureAudience;
  description?: string;
  examples?: ActionExample[];
  schemaHint?: unknown;
}
interface DispatchResult<State> {
  state?: State;
  command?: Command;
  subscriptions?: Subscription[];
  result: unknown;
}
type CapabilityRequest<Id extends string = string, Methods = unknown, Events = unknown> = ReturnType<CapabilityFactory<Id, Methods, Events>>;
type CapabilityRequests = Record<string, CapabilityRequest>;
type NoCapabilities = Record<string, never>;
type NoProcedures = Record<string, never>;
type MethodsOf<C> = C extends CapabilityRequest<string, infer M, infer _S> ? M : never;
type StreamsOf<C> = C extends CapabilityRequest<string, infer _M, infer S> ? S : never;
type Command = {
  kind: 'none';
} | {
  kind: 'batch';
  commands: Command[];
} | {
  kind: 'capability';
  capability: string;
  method: string;
  args: readonly unknown[];
  to: string;
  coalesce?: true;
};
interface CommandBuilder {
  to(edge: string): Command;
  coalesce(): CommandBuilder;
}
interface Subscription {
  capability: string;
  method: string;
  args: readonly unknown[];
  to: string;
  coalesce?: true;
}
interface SubscriptionBuilder {
  to(edge: string): Subscription;
  coalesce(): SubscriptionBuilder;
}
type Result<T> = {
  ok: true;
  value: T;
} | {
  ok: false;
  error: {
    message: string;
  };
};
type MethodArgs<M, K extends keyof M> = M[K] extends ((...args: infer A) => unknown) ? A : never;
type MethodOutput<M, K extends keyof M> = M[K] extends ((...args: any[]) => infer R) ? Awaited<R> : never;
type StreamArgs<S, K extends keyof S> = S[K] extends ((...args: infer A) => unknown) ? A : never;
type StreamEvent<S, K extends keyof S> = S[K] extends ((...args: any[]) => Stream<infer E>) ? E : never;
interface Edge<State, S extends StandardSchemaV1 | undefined = StandardSchemaV1 | undefined> extends Edge$1<State, Command, S> {
  readonly __edge: 'on' | 'result' | 'commit' | 'event';
  description?: string;
  examples?: ActionExample[];
}
interface Edges<State, Caps extends CapabilityRequests> {
  on<S extends StandardSchemaV1>(schema: S, transition: Transition<State, Infer$1<S>, Command>, meta?: {
    description?: string;
    examples?: ActionExample[];
  }): Edge<State, S>;
  commit<S extends StandardSchemaV1>(schema: S, transition: Transition<State, Infer$1<S>, Command>): Edge<State>;
  fromResult<C extends keyof Caps & string, M extends keyof MethodsOf<Caps[C]> & string>(capability: C, method: M, transition: Transition<State, Result<MethodOutput<MethodsOf<Caps[C]>, M>>, Command>): Edge<State, undefined>;
  fromEvent<C extends keyof Caps & string, M extends keyof StreamsOf<Caps[C]> & string>(capability: C, method: M, transition: Transition<State, StreamEvent<StreamsOf<Caps[C]>, M>, Command>): Edge<State, undefined>;
  cmd<C extends keyof Caps & string, M extends keyof MethodsOf<Caps[C]> & string>(capability: C, method: M, ...args: MethodArgs<MethodsOf<Caps[C]>, M>): CommandBuilder;
}
interface Subs<Caps extends CapabilityRequests> {
  capability<C extends keyof Caps & string, M extends keyof StreamsOf<Caps[C]> & string>(capability: C, method: M, ...args: StreamArgs<StreamsOf<Caps[C]>, M>): SubscriptionBuilder;
}
interface AppHandle<State, Caps extends CapabilityRequests = NoCapabilities> {
  readonly state: State;
  dispatch(message: Message): Promise<{
    seq: number;
  }>;
  settled(seq: number, opts?: {
    timeout?: number;
  }): Promise<State>;
  invoke<C extends keyof Caps & string, M extends keyof MethodsOf<Caps[C]> & string>(capability: C, method: M, ...args: MethodArgs<MethodsOf<Caps[C]>, M>): Promise<MethodOutput<MethodsOf<Caps[C]>, M>>;
}
interface RawAppHandle {
  readonly state: unknown;
  dispatch(message: Message): Promise<{
    seq: number;
  }>;
  settled(seq: number, opts?: {
    timeout?: number;
  }): Promise<unknown>;
  invoke(capability: string, method: string, ...args: unknown[]): Promise<unknown>;
}
type ProcedureAudience = 'gui' | 'agent' | 'both';
interface ProcedureDef<S extends StandardSchemaV1, State, Caps extends CapabilityRequests, R> {
  readonly __kind: 'procedure';
  audience: ProcedureAudience;
  description?: string;
  schema: S;
  run: (params: Infer$1<S>, app: AppHandle<State, Caps>) => MaybePromise$1<R>;
}
type ProcedureDefs<State, Caps extends CapabilityRequests = CapabilityRequests> = Record<string, ProcedureDef<any, State, Caps, any>>;
interface App<State, Caps extends CapabilityRequests = NoCapabilities, Procs extends ProcedureDefs<State, Caps> = NoProcedures, Edges_ extends Record<string, Edge<State>> = Record<string, never>> {
  readonly initialState: State;
  actions: Record<string, ActionMeta>;
  capabilities: Record<string, {
    id: string;
    scopes: readonly string[];
  }>;
  readonly __capabilities?: Caps;
  readonly __procs?: Procs;
  readonly __edges?: Edges_;
  dispatch: (message: Message, ctx: RuntimeContext<State>) => Promise<DispatchResult<State>>;
  runProcedure: (name: string, params: unknown, app: AppHandle<State, Caps>) => Promise<unknown>;
  subscriptions: (state: State) => Subscription[];
}
interface RuntimeContext<State = unknown> {
  readonly state: State;
}
type CapabilityDecl = Record<string, {
  id: string;
  scopes: string[];
}>;
/**
 * The installed-app record — and, when the app is hosted, the whole of it.
 *
 * `baseUrl` is what splits the two shapes. With one, the manifest is a SPEC: an
 * immutable, version-pinned origin the bytes are mounted from, plus the hashes that
 * identify them. `/applications/<id>/` then holds this file and the reducer; mounted
 * bytes use the host's HTTP cache and never replicate because the origin can re-serve
 * them. Without one, the bundle IS the bytes: they are
 * copied into `/applications/<id>/` and replicate, which is the only correct answer
 * for an app authored locally (a devkit draft has no upstream to refetch from).
 *
 * That makes residency a CONSEQUENCE of the spec rather than a separate flag, and it
 * makes the record self-describing: a device can validate and repair an app it has
 * only ever received over sync, with no catalog of its own to consult.
 */
interface AppManifest {
  id: string;
  name: string;
  description?: string;
  version?: string;
  icon?: string;
  app: string;
  gui?: string;
  cli?: string;
  include?: string[];
  capabilities?: CapabilityDecl;
  baseUrl?: string;
  hash?: string;
  files?: Record<string, string>;
}
interface AppBundle {
  manifest(): Promise<AppManifest>;
  list(): AsyncIterable<string>;
  read(path: string): Promise<Uint8Array>;
}
interface MemoryAppBundle {
  id: string;
  name: string;
  description?: string;
  version?: string;
  icon?: string;
  app: string;
  gui?: string;
  cli?: string;
  assets?: Record<string, string | Uint8Array>;
}
declare function appBundleFromMemory(input: MemoryAppBundle): AppBundle;
interface InstalledApp {
  manifest: AppManifest;
  read(path: string): Promise<Uint8Array>;
}
declare function safeBundlePath(path: string): string;
//#endregion
//#region ../app/sdk.d.ts
type MaybePromise<T> = T | Promise<T>;
type Infer<S extends StandardSchemaV1> = StandardSchemaV1.InferOutput<S>;
type Capabilities = Record<string, ReturnType<CapabilityFactory<string, unknown, unknown>>>;
interface Procedures<State, Caps extends Capabilities> {
  procedure<S extends StandardSchemaV1, R>(def: {
    audience?: ProcedureAudience;
    description?: string;
    schema: S;
    run: (params: Infer<S>, app: AppHandle<State, Caps>) => MaybePromise<R>;
  }): ProcedureDef<S, State, Caps, R>;
}
interface AppSpec<State, Caps extends Capabilities, Procs extends ProcedureDefs<State, Caps>, E extends Record<string, Edge<State>>> {
  init: State;
  capabilities?: Caps;
  update?: (t: Edges<State, Caps>) => E;
  subscriptions?: (state: State, sub: Subs<Caps>) => Subscription[];
  procedures?: (p: Procedures<State, Caps>) => Procs;
}
declare function defineApp<State, Caps extends Capabilities = Record<string, never>, Procs extends ProcedureDefs<State, Caps> = Record<string, never>, E extends Record<string, Edge<State>> = Record<string, never>>(spec: AppSpec<State, Caps, Procs, E>): App<State, Caps, Procs, E>;
interface Transport {
  invoke(message: Message): Promise<unknown>;
  subscribe(onState: (state: unknown) => void): () => void;
}
type ProcParamsOf<Def> = Def extends ProcedureDef<infer S, infer _ST, infer _C, infer _R> ? Infer<S> : never;
type ProcResultOf<Def> = Def extends ProcedureDef<infer _S, infer _ST, infer _C, infer R> ? Awaited<R> : never;
type EdgeClient<E, State> = { [K in keyof E as E[K] extends Edge<infer _St, infer S> ? S extends StandardSchemaV1 ? K : never : never]: E[K] extends Edge<infer _St, infer S> ? S extends StandardSchemaV1 ? (params: Infer<S>) => Promise<State> : never : never };
type Client<A> = A extends App<infer State, infer _Caps, infer Procs, infer Edges_> ? EdgeClient<Edges_, State> & { [K in keyof Procs & string]: (params: ProcParamsOf<Procs[K]>) => Promise<ProcResultOf<Procs[K]>> } : never;
declare function createClient<A>(transport: Transport): Client<A>;
//#endregion
export { Message as _, ActionMeta as a, AppHandle as c, InstalledApp as d, RawAppHandle as f, safeBundlePath as g, appBundleFromMemory as h, defineApp as i, AppManifest as l, Subscription as m, Transport as n, App as o, RuntimeContext as p, createClient as r, AppBundle as s, Client as t, DispatchResult as u };
//# sourceMappingURL=sdk-eksYUmch.d.ts.map