Skip to content

armgit / hypit

Public Imported from GitHub
0

Clone any viral video with AI agents. Not just a script, the whole workflow: swap the face, the words, the B-roll, ship 100 variants in one command, and get your 100M views.

14.2 KB
Raw Blame
import { endpointActions } from "@hypit/hypit/endpoint";
import type { EndpointInstance } from "@hypit/hypit/endpoint";
import type { Facet } from "@hypit/hypit/facet";
import type { CanonicalValue } from "@hypit/hypit/protocol";
import { credentialRef } from "@hypit/hypit/runtime";
import type { CredentialRef, CredentialStore, CredentialValue } from "@hypit/hypit/runtime";

export { credentialRef, verifyCredentialRef } from "@hypit/hypit/runtime";
export type {
  CredentialRef,
  CredentialStore,
  CredentialValue,
  WritableCredentialStore,
} from "@hypit/hypit/runtime";

export const runtimeEndpointAdapterFacetAbi = "hypit.runtime-local-endpoint-adapter@1";
export const runtimeCredentialStoreAdapterFacetAbi = "hypit.runtime-local-credential-store-adapter@1";

export type RuntimeAdapterKind = "endpoint" | "credential-store";

type RuntimeAdapterAddress = {
  readonly use: string;
  readonly kind: RuntimeAdapterKind;
};

export type RuntimeAdapterFactoryContext = {
  /** Persistent machine/user state supplied by the Host, outside every author project. */
  readonly hostStateRoot: string;
  /** Runtime/Profile state belonging to this project. */
  readonly dataRoot: string;
  readonly instance: string;
  readonly pool?: string;
  readonly config: CanonicalValue;
};

export type RuntimeDoctorDiagnostic = {
  readonly severity: "error" | "warning" | "info";
  readonly code: string;
  readonly message: string;
  readonly subject?: string;
};

export type ManagedProgramCommand = {
  /** Optional human-readable purpose, supplied by the program owner. Not a lifecycle state. */
  readonly label?: string;
  readonly command: string;
  readonly args: readonly string[];
  readonly cwd?: string;
  readonly env?: Readonly<Record<string, string>>;
};

export type ManagedProgramInstallation = {
  /** Read-only inspection of the selected environment and resources; no downloads or receipts. */
  probe(): Promise<ManagedProgramState>;
  /** Reconcile an installed environment before a cold start, using its package manager's cache. */
  readonly prepareBeforeStart?: boolean;
  /** Explicit preparation commands, run for missing resources or cold reconciliation above. */
  readonly commands: readonly ManagedProgramCommand[];
};

export type ManagedProgramState =
  | { readonly state: "ready" }
  | { readonly state: "down"; readonly detail: string }
  | { readonly state: "mismatch"; readonly detail: string };

export type ManagedProgram = {
  readonly id: string;
  /** Shared lifecycle files live here; absent means this Runtime owns them. */
  readonly stateRoot?: string;
  /** Installation and runtime readiness are deliberately separate states. */
  readonly installation?: ManagedProgramInstallation;
  probe(): Promise<ManagedProgramState>;
  readonly start?: ManagedProgramCommand;
};

export type RuntimeEndpointActivation = {
  readonly endpoint: EndpointInstance;
  readonly program?: ManagedProgram;
  readonly diagnose?: (context: {
    readonly credentials: Readonly<Record<string, CredentialValue>>;
    readonly capabilities?: readonly import("@hypit/hypit/protocol").CapabilityRef[];
  }) => readonly RuntimeDoctorDiagnostic[] | Promise<readonly RuntimeDoctorDiagnostic[]>;
};

export type RuntimeOpened<T> = {
  readonly value: T;
  readonly close?: () => void | Promise<void>;
};

export type RuntimeEndpointAdapterImplementation = {
  activate(context: RuntimeAdapterFactoryContext): RuntimeEndpointActivation | Promise<RuntimeEndpointActivation>;
};

type RuntimeStoreAdapterImplementation<T> = {
  validate(context: RuntimeAdapterFactoryContext): void;
  open(context: RuntimeAdapterFactoryContext): RuntimeOpened<T> | Promise<RuntimeOpened<T>>;
  doctor?(context: RuntimeAdapterFactoryContext): readonly RuntimeDoctorDiagnostic[] | Promise<readonly RuntimeDoctorDiagnostic[]>;
};

type RuntimeAdapterImplementation =
  | RuntimeEndpointAdapterImplementation
  | RuntimeStoreAdapterImplementation<CredentialStore>;

export type RuntimeAdapterFacet = Facet & {
  readonly abi:
    | typeof runtimeEndpointAdapterFacetAbi
    | typeof runtimeCredentialStoreAdapterFacetAbi;
  readonly implementation: RuntimeAdapterImplementation;
};

function assert(condition: unknown, message: string): asserts condition {
  if (!condition) throw new Error(message);
}

function abiFor(kind: RuntimeAdapterKind): RuntimeAdapterFacet["abi"] {
  if (kind === "endpoint") return runtimeEndpointAdapterFacetAbi;
  return runtimeCredentialStoreAdapterFacetAbi;
}

function address(facet: RuntimeAdapterFacet): RuntimeAdapterAddress {
  const kind = facet.abi === runtimeEndpointAdapterFacetAbi
    ? "endpoint"
    : facet.abi === runtimeCredentialStoreAdapterFacetAbi ? "credential-store" : undefined;
  assert(kind !== undefined, `Runtime Adapter ${facet.abi} ABI is unsupported`);
  assert(facet.offers?.length === 1 && facet.offers[0]!.trim().length > 0,
    "Runtime Adapter must offer exactly one non-empty use name");
  return { use: facet.offers[0]!, kind };
}

function verifyImplementation(value: unknown, subject: string, kind: RuntimeAdapterKind): RuntimeAdapterImplementation {
  assert(value !== null && typeof value === "object", `${subject} implementation is not an object`);
  if (kind === "endpoint") {
    assert(typeof (value as { activate?: unknown }).activate === "function", `${subject} does not implement activate()`);
    return value as RuntimeEndpointAdapterImplementation;
  }
  assert(typeof (value as { validate?: unknown }).validate === "function", `${subject} does not implement validate()`);
  assert(typeof (value as { open?: unknown }).open === "function", `${subject} does not implement open()`);
  const doctor = (value as { doctor?: unknown }).doctor;
  assert(doctor === undefined || typeof doctor === "function", `${subject}.doctor must be a function`);
  return value as RuntimeAdapterImplementation;
}

function storeFacet<T>(kind: "credential-store", options: {
  readonly use: string;
  readonly validate: RuntimeStoreAdapterImplementation<T>["validate"];
  readonly open: RuntimeStoreAdapterImplementation<T>["open"];
  readonly doctor?: RuntimeStoreAdapterImplementation<T>["doctor"];
}): RuntimeAdapterFacet {
  assert(options.use.trim().length > 0, "Runtime Adapter use name is empty");
  const implementation = {
    validate: options.validate,
    open: options.open,
    ...(options.doctor === undefined ? {} : { doctor: options.doctor }),
  };
  return {
    abi: abiFor(kind),
    offers: [options.use],
    implementation: verifyImplementation(implementation, `Runtime Adapter ${options.use}`, kind),
  };
}

export function createRuntimeEndpointAdapterFacet(options: {
  readonly use: string;
  readonly activate: RuntimeEndpointAdapterImplementation["activate"];
}): RuntimeAdapterFacet {
  assert(options.use.trim().length > 0, "Runtime Adapter use name is empty");
  return {
    abi: runtimeEndpointAdapterFacetAbi,
    offers: [options.use],
    implementation: verifyImplementation({ activate: options.activate }, `Runtime Adapter ${options.use}`, "endpoint"),
  };
}

export function createRuntimeCredentialStoreAdapterFacet(options: {
  readonly use: string;
  readonly validate: RuntimeStoreAdapterImplementation<CredentialStore>["validate"];
  readonly open: RuntimeStoreAdapterImplementation<CredentialStore>["open"];
  readonly doctor?: RuntimeStoreAdapterImplementation<CredentialStore>["doctor"];
}): RuntimeAdapterFacet {
  return storeFacet("credential-store", options);
}

export function isRuntimeAdapterFacet(value: Facet): value is RuntimeAdapterFacet {
  try {
    const resolved = address(value as RuntimeAdapterFacet);
    verifyImplementation(value.implementation, "Runtime Adapter", resolved.kind);
    return true;
  } catch {
    return false;
  }
}

export class RuntimeAdapterRegistry {
  readonly #registrations = new Map<string, RuntimeAdapterImplementation>();

  #key(use: string, kind: RuntimeAdapterKind): string {
    return `${kind}\u0000${use}`;
  }

  registerFacet(facet: Facet): void {
    assert(isRuntimeAdapterFacet(facet), `Facet ${facet.abi} is not a valid Runtime Adapter`);
    const resolved = address(facet);
    const key = this.#key(resolved.use, resolved.kind);
    assert(!this.#registrations.has(key), `Runtime ${resolved.kind} Adapter ${resolved.use} is already registered`);
    this.#registrations.set(key, facet.implementation);
  }

  has(use: string, kind: RuntimeAdapterKind): boolean {
    return this.#registrations.has(this.#key(use, kind));
  }

  async activateEndpoint(use: string, context: RuntimeAdapterFactoryContext): Promise<RuntimeEndpointActivation> {
    const implementation = this.#registrations.get(this.#key(use, "endpoint")) as RuntimeEndpointAdapterImplementation | undefined;
    assert(implementation !== undefined, `Runtime Endpoint adapter ${use} is not registered`);
    const activation = await implementation.activate(context);
    assert(activation.endpoint.instance.id === context.instance,
      `Runtime Endpoint adapter ${use} created Endpoint ${activation.endpoint.instance.id} outside configured instance ${context.instance}`);
    return activation;
  }

  async createEndpoint(use: string, context: RuntimeAdapterFactoryContext): Promise<EndpointInstance> {
    return (await this.activateEndpoint(use, context)).endpoint;
  }

  async #openStore<T>(use: string, kind: "credential-store", context: RuntimeAdapterFactoryContext): Promise<RuntimeOpened<T>> {
    const implementation = this.#registrations.get(this.#key(use, kind)) as RuntimeStoreAdapterImplementation<T> | undefined;
    assert(implementation !== undefined, `Runtime ${kind} adapter ${use} is not registered`);
    implementation.validate(context);
    const opened = await implementation.open(context);
    assert(opened.value !== null && typeof opened.value === "object", `Runtime ${kind} adapter ${use} returned no value`);
    return opened;
  }

  async openCredentialStore(use: string, context: RuntimeAdapterFactoryContext): Promise<RuntimeOpened<CredentialStore>> {
    return await this.#openStore(use, "credential-store", context);
  }

  validate(use: string, kind: Exclude<RuntimeAdapterKind, "endpoint">, context: RuntimeAdapterFactoryContext): readonly RuntimeDoctorDiagnostic[] {
    const implementation = this.#registrations.get(this.#key(use, kind)) as RuntimeStoreAdapterImplementation<unknown> | undefined;
    if (implementation === undefined) return [{
      severity: "error", code: "RUNTIME_ADAPTER_MISSING", message: `Runtime Adapter ${use} is not registered`, subject: use,
    }];
    implementation.validate(context);
    return [];
  }

  async doctor(use: string, kind: Exclude<RuntimeAdapterKind, "endpoint">, context: RuntimeAdapterFactoryContext): Promise<readonly RuntimeDoctorDiagnostic[]> {
    const implementation = this.#registrations.get(this.#key(use, kind)) as RuntimeStoreAdapterImplementation<unknown> | undefined;
    if (implementation === undefined) return [{
      severity: "error", code: "RUNTIME_ADAPTER_MISSING", message: `Runtime Adapter ${use} is not registered`, subject: use,
    }];
    return implementation.doctor === undefined ? [] : await implementation.doctor(context);
  }
}

export function runtimeConfigObject(value: CanonicalValue, subject: string): Record<string, CanonicalValue> {
  assert(value !== null && typeof value === "object" && !Array.isArray(value), `${subject} config must be an object`);
  return value as Record<string, CanonicalValue>;
}

export function runtimeConfigExact(value: Record<string, CanonicalValue>, allowed: readonly string[], subject: string): void {
  const unknown = Object.keys(value).filter((key) => !allowed.includes(key));
  assert(unknown.length === 0, `${subject} config does not accept ${unknown[0]}`);
}

export function runtimeConfigString(value: CanonicalValue | undefined, subject: string): string | undefined {
  if (value === undefined) return undefined;
  assert(typeof value === "string" && value.trim().length > 0, `${subject} must be a non-empty string`);
  return value;
}

export function runtimeConfigPositiveInteger(value: CanonicalValue | undefined, subject: string): number | undefined {
  if (value === undefined) return undefined;
  assert(typeof value === "number" && Number.isSafeInteger(value) && value > 0, `${subject} must be a positive integer`);
  return value;
}

export function runtimeConfigBoolean(value: CanonicalValue | undefined, subject: string): boolean | undefined {
  if (value === undefined) return undefined;
  assert(typeof value === "boolean", `${subject} must be a boolean`);
  return value;
}

export function runtimeConfigCredentialRef(
  value: CanonicalValue | undefined,
  subject: string,
): CredentialRef | undefined {
  if (value === undefined) return undefined;
  const object = runtimeConfigObject(value, subject);
  runtimeConfigExact(object, ["store", "key"], subject);
  const store = runtimeConfigString(object.store, `${subject}.store`);
  const key = runtimeConfigString(object.key, `${subject}.key`);
  assert(store !== undefined && key !== undefined, `${subject} requires store and key`);
  return credentialRef(store, key);
}

/** Common Profile representation for short action occupancy and rate budgets. */
export function runtimeConfigActionLimits(value: CanonicalValue | undefined): import("@hypit/hypit/endpoint").EndpointActionLimits | undefined {
  if (value === undefined) return undefined;
  const actions = runtimeConfigObject(value, "actionLimits");
  runtimeConfigExact(actions, endpointActions, "actionLimits");
  return Object.fromEntries(Object.entries(actions).map(([action, raw]) => {
    const limit = runtimeConfigObject(raw, `actionLimits.${action}`);
    runtimeConfigExact(limit, ["concurrency", "rate"], `actionLimits.${action}`);
    const concurrency = runtimeConfigPositiveInteger(limit.concurrency, `${action} concurrency`);
    let rate: { limit: number; periodMs: number } | undefined;
    if (limit.rate !== undefined) {
      const item = runtimeConfigObject(limit.rate, `${action} rate`);
      runtimeConfigExact(item, ["limit", "periodMs"], `${action} rate`);
      const units = runtimeConfigPositiveInteger(item.limit, `${action} rate.limit`);
      const periodMs = runtimeConfigPositiveInteger(item.periodMs, `${action} rate.periodMs`);
      if (units === undefined || periodMs === undefined) throw new Error(`${action} rate requires limit and periodMs`);
      rate = { limit: units, periodMs };
    }
    return [action, { ...(concurrency === undefined ? {} : { concurrency }), ...(rate === undefined ? {} : { rate }) }];
  }));
}