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;AACT","file":"index.cjs","sourcesContent":["/**\n * Error model for Krikos, the identity registry and lifecycle manager for fleets of Massive\n * Intelligence (IM) agents and non-human entities (NHE).\n *\n * Krikos throws exactly one error class so that host applications can catch and branch on a\n * single type, then dispatch on the stable machine-readable {@link KrikosError.code}. New codes\n * may be added in minor versions, so consumers must treat the code space as open and fall back\n * gracefully on codes they do not recognize.\n *\n * @packageDocumentation\n */\n\n/**\n * The only error type thrown by Krikos.\n *\n * Invariants:\n * - `name` is always the literal string `'KrikosError'`, safe for cross-realm checks where\n *   `instanceof` fails (multiple bundles, workers, iframes).\n * - `code` is a stable, machine-readable, SCREAMING_SNAKE identifier prefixed with `ERR_`.\n *   It never changes meaning across versions, although new codes may appear in minors.\n * - `message` is human-readable English prose intended for logs, never for parsing.\n *\n * @example\n * ```ts\n * try {\n *   krikos.activate(id);\n * } catch (error) {\n *   if (error instanceof KrikosError && error.code === 'ERR_INVALID_TRANSITION') {\n *     // The agent cannot move to active from its current lifecycle state.\n *   }\n * }\n * ```\n */\nexport class KrikosError extends Error {\n  /**\n   * Stable machine-readable error code, for example `'ERR_INVALID_TRANSITION'`.\n   *\n   * Branch on this field, never on `message`. The code space is minor-extensible.\n   */\n  readonly code: string;\n\n  /**\n   * @param message - Human-readable description of what went wrong and how to fix it.\n   * @param code - Stable machine-readable code, see {@link KrikosError.code}.\n   */\n  constructor(message: string, code: string) {\n    super(message);\n    this.name = 'KrikosError';\n    this.code = code;\n  }\n\n  /**\n   * Builds a {@link KrikosError} with code `'ERR_INVALID_INPUT'`.\n   *\n   * Used for every caller-side contract violation: an agent with no name, an empty capability\n   * name, a non-finite expiration timestamp, a capability action that is not a string, and any\n   * other input the type system cannot reject for untyped JavaScript callers.\n   *\n   * @param message - Human-readable description of the invalid input.\n   * @returns A new error instance, never thrown by this factory itself.\n   */\n  static invalid(message: string): KrikosError {\n    return new KrikosError(message, 'ERR_INVALID_INPUT');\n  }\n\n  /**\n   * Builds a {@link KrikosError} with code `'ERR_NOT_FOUND'`.\n   *\n   * Used when a caller references an agent by id that the registry does not hold, for example\n   * activating an id that was never issued.\n   *\n   * @param message - Human-readable description of what was not found.\n   * @returns A new error instance, never thrown by this factory itself.\n   */\n  static notFound(message: string): KrikosError {\n    return new KrikosError(message, 'ERR_NOT_FOUND');\n  }\n\n  /**\n   * Builds a {@link KrikosError} with code `'ERR_INVALID_TRANSITION'`.\n   *\n   * Used when a lifecycle operation is requested from a state that does not allow it, for\n   * example resuming an agent that was revoked, or activating one that is archived. The\n   * registry stays consistent; the offending transition is rejected whole.\n   *\n   * @param message - Human-readable description of the illegal transition.\n   * @returns A new error instance, never thrown by this factory itself.\n   */\n  static transition(message: string): KrikosError {\n    return new KrikosError(message, 'ERR_INVALID_TRANSITION');\n  }\n\n  /**\n   * Builds a {@link KrikosError} with code `'ERR_CONFLICT'`.\n   *\n   * Used when an operation collides with existing state, for example issuing an agent with an\n   * explicit id that is already registered.\n   *\n   * @param message - Human-readable description of the conflict.\n   * @returns A new error instance, never thrown by this factory itself.\n   */\n  static conflict(message: string): KrikosError {\n    return new KrikosError(message, 'ERR_CONFLICT');\n  }\n\n  /**\n   * Builds a {@link KrikosError} with code `'ERR_LIMIT_REACHED'`.\n   *\n   * Used when an operation would exceed a configured bound, for example issuing past\n   * `maxAgents`. The registry stays consistent; the offending write is rejected whole.\n   *\n   * @param message - Human-readable description of the limit that was hit.\n   * @returns A new error instance, never thrown by this factory itself.\n   */\n  static limit(message: string): KrikosError {\n    return new KrikosError(message, 'ERR_LIMIT_REACHED');\n  }\n}\n","/**\n * Vercel AI SDK integration for Krikos: authorize an agent's tool calls against its declared\n * capabilities before they run.\n *\n * The Vercel AI SDK models tools as a record of definitions, each with an `execute` function.\n * Krikos wraps those executors so a tool only runs when the acting agent is active, unexpired,\n * and declares a capability that admits the tool. A denied call is recorded in the audit trail\n * and rejected, so an over-reaching agent never silently performs an action it was not granted.\n *\n * Structural by design: Krikos imports nothing from the Vercel AI SDK. The tool shape is matched\n * by structure, so the same wrapper works across SDK minor versions.\n *\n * @packageDocumentation\n */\n\nimport { KrikosError } from '../errors.js';\nimport type { Krikos } from '../types.js';\n\n/** Minimal structural shape of a Vercel AI SDK tool: anything with an `execute` function. */\nexport interface ToolLike {\n  /** The tool's executor. Krikos wraps it; everything else on the tool is preserved. */\n  // biome-ignore lint/suspicious/noExplicitAny: the SDK's tool args and result are caller-defined.\n  execute?: (args: any, options?: any) => unknown;\n  /** Any other tool fields, preserved untouched. */\n  readonly [key: string]: unknown;\n}\n\n/** How one tool maps to a capability check. */\nexport interface ToolCapability {\n  /** The capability name the tool requires. */\n  readonly capability: string;\n  /** The action the tool performs. Defaults to the tool name. */\n  readonly action?: string;\n  /** Derives the resource from the tool's arguments, for resource-scoped capabilities. */\n  // biome-ignore lint/suspicious/noExplicitAny: the SDK's tool args are caller-defined.\n  readonly resource?: (args: any) => string;\n}\n\n/** A mapping from tool name to its capability requirement. */\nexport type ToolCapabilityMap = Readonly<Record<string, ToolCapability>>;\n\n/**\n * Wraps a record of Vercel AI SDK tools so each call is authorized for `agentId` before it runs.\n *\n * A tool present in `mapping` is gated on its capability requirement; a tool absent from `mapping`\n * is left untouched (no implicit gate, so an explicit allowlist is the only thing that enforces).\n * A denied call rejects with KrikosError `ERR_FORBIDDEN` and is recorded as `access.denied`.\n *\n * @param krikos - The registry holding the agent.\n * @param agentId - The acting agent.\n * @param tools - The Vercel AI SDK tools record.\n * @param mapping - Tool-to-capability requirements.\n * @returns A new tools record with guarded executors.\n */\nexport function guardVercelTools<T extends Record<string, ToolLike>>(\n  krikos: Krikos,\n  agentId: string,\n  tools: T,\n  mapping: ToolCapabilityMap,\n): T {\n  const guarded: Record<string, ToolLike> = {};\n  for (const [name, tool] of Object.entries(tools)) {\n    const requirement = mapping[name];\n    if (requirement === undefined || typeof tool.execute !== 'function') {\n      guarded[name] = tool;\n      continue;\n    }\n    const original = tool.execute.bind(tool);\n    const action = requirement.action ?? name;\n    guarded[name] = {\n      ...tool,\n      // biome-ignore lint/suspicious/noExplicitAny: matches the SDK's executor signature.\n      execute: (args: any, options?: any): unknown => {\n        const resource =\n          requirement.resource !== undefined ? requirement.resource(args) : undefined;\n        const allowed = krikos.authorize(\n          agentId,\n          requirement.capability,\n          action,\n          resource,\n          `vercel:${name}`,\n        );\n        if (!allowed) {\n          throw new KrikosError(\n            `agent '${agentId}' is not authorized for tool '${name}' (${requirement.capability}:${action}).`,\n            'ERR_FORBIDDEN',\n          );\n        }\n        return original(args, options);\n      },\n    };\n  }\n  return guarded as T;\n}\n"]}