@vorim/sdk 3.8.1 → 3.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +208 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +133 -3
- package/dist/index.d.ts +133 -3
- package/dist/index.js +208 -1
- package/dist/index.js.map +1 -1
- package/dist/integrations/anthropic.d.cts +1 -1
- package/dist/integrations/anthropic.d.ts +1 -1
- package/dist/integrations/ap2.cjs +218 -0
- package/dist/integrations/ap2.cjs.map +1 -0
- package/dist/integrations/ap2.d.cts +95 -0
- package/dist/integrations/ap2.d.ts +95 -0
- package/dist/integrations/ap2.js +192 -0
- package/dist/integrations/ap2.js.map +1 -0
- package/dist/integrations/bank-rails.cjs +173 -0
- package/dist/integrations/bank-rails.cjs.map +1 -0
- package/dist/integrations/bank-rails.d.cts +87 -0
- package/dist/integrations/bank-rails.d.ts +87 -0
- package/dist/integrations/bank-rails.js +147 -0
- package/dist/integrations/bank-rails.js.map +1 -0
- package/dist/integrations/circle.cjs +159 -0
- package/dist/integrations/circle.cjs.map +1 -0
- package/dist/integrations/circle.d.cts +75 -0
- package/dist/integrations/circle.d.ts +75 -0
- package/dist/integrations/circle.js +133 -0
- package/dist/integrations/circle.js.map +1 -0
- package/dist/integrations/crewai.d.cts +1 -1
- package/dist/integrations/crewai.d.ts +1 -1
- package/dist/integrations/hermes.d.cts +1 -1
- package/dist/integrations/hermes.d.ts +1 -1
- package/dist/integrations/langchain.d.cts +1 -1
- package/dist/integrations/langchain.d.ts +1 -1
- package/dist/integrations/langgraph.d.cts +1 -1
- package/dist/integrations/langgraph.d.ts +1 -1
- package/dist/integrations/llamaindex.d.cts +1 -1
- package/dist/integrations/llamaindex.d.ts +1 -1
- package/dist/integrations/nekuda.cjs +165 -0
- package/dist/integrations/nekuda.cjs.map +1 -0
- package/dist/integrations/nekuda.d.cts +79 -0
- package/dist/integrations/nekuda.d.ts +79 -0
- package/dist/integrations/nekuda.js +139 -0
- package/dist/integrations/nekuda.js.map +1 -0
- package/dist/integrations/openai.d.cts +1 -1
- package/dist/integrations/openai.d.ts +1 -1
- package/dist/integrations/paypal-agent.cjs +215 -0
- package/dist/integrations/paypal-agent.cjs.map +1 -0
- package/dist/integrations/paypal-agent.d.cts +87 -0
- package/dist/integrations/paypal-agent.d.ts +87 -0
- package/dist/integrations/paypal-agent.js +189 -0
- package/dist/integrations/paypal-agent.js.map +1 -0
- package/dist/integrations/siem.d.cts +1 -1
- package/dist/integrations/siem.d.ts +1 -1
- package/dist/integrations/skyfire.cjs +166 -0
- package/dist/integrations/skyfire.cjs.map +1 -0
- package/dist/integrations/skyfire.d.cts +80 -0
- package/dist/integrations/skyfire.d.ts +80 -0
- package/dist/integrations/skyfire.js +140 -0
- package/dist/integrations/skyfire.js.map +1 -0
- package/dist/integrations/stripe-acp.d.cts +1 -1
- package/dist/integrations/stripe-acp.d.ts +1 -1
- package/dist/integrations/ucp.d.cts +1 -1
- package/dist/integrations/ucp.d.ts +1 -1
- package/dist/integrations/vercel-ai.d.cts +1 -1
- package/dist/integrations/vercel-ai.d.ts +1 -1
- package/dist/integrations/x402.cjs +211 -0
- package/dist/integrations/x402.cjs.map +1 -0
- package/dist/integrations/x402.d.cts +87 -0
- package/dist/integrations/x402.d.ts +87 -0
- package/dist/integrations/x402.js +185 -0
- package/dist/integrations/x402.js.map +1 -0
- package/dist/{types-Cb6V-boJ.d.cts → types-zzK9YOoa.d.cts} +86 -1
- package/dist/{types-Cb6V-boJ.d.ts → types-zzK9YOoa.d.ts} +86 -1
- package/package.json +71 -1
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/replay.ts"],"sourcesContent":["// ============================================================================\n// VORIM SDK — TypeScript\n// Thin client wrapping the Vorim AI REST API\n// ============================================================================\n\nimport type {\n Agent, AgentRegistrationInput, AgentRegistrationResult,\n TrustRecord, AuditEventInput, PermissionScope, PermissionCheckResult,\n AgentDelegationRecord, DelegationLinkClaims,\n BeforeActionInput, RuntimeDecision, DecisionVerdict,\n TokenBudget, TokenBudgetInput, BudgetSpend,\n} from './types.js';\nimport { jcsCanonicalise, hashPreviousEvent } from './replay.js';\n\n// __SDK_VERSION__ is replaced at build time (tsup define) with the package.json\n// version, so the User-Agent always matches the published version. Falls back to\n// '0.0.0' when run un-bundled (e.g. ts-node / tests) where the define isn't applied.\ndeclare const __SDK_VERSION__: string | undefined;\nconst SDK_VERSION = typeof __SDK_VERSION__ !== 'undefined' ? __SDK_VERSION__ : '0.0.0';\nconst USER_AGENT = `vorim-sdk/${SDK_VERSION}`;\n\n/** Promise-based delay. Used by the runtime escalation poller. */\nfunction sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n}\n\nexport interface VorimConfig {\n apiKey: string;\n baseUrl?: string;\n timeout?: number;\n /**\n * Auto-sign audit events with the agent's private key at emit time.\n * Default true in v3.1+. Set to false to opt out globally.\n * When enabled, the SDK signs the event using whichever private key was\n * stored via {@link VorimSDK.useAgentKey} or returned by\n * {@link VorimSDK.register}. Events for unknown agents pass through unsigned.\n */\n autoSign?: boolean;\n /**\n * Canonical form to use when signing. Default `\"v0\"` for backward-compat\n * with `@vorim/sdk` 3.1.x. Set to `\"v1\"` to sign the full event object\n * via RFC 8785 JCS, covering replayable-evidence fields (model_version,\n * tool_catalogue_hash, system_prompt_hash, prev_event_hash). v1 events\n * carry `canonical_form: \"v1\"` on the wire so the server can dispatch\n * verification correctly.\n */\n canonicalForm?: 'v0' | 'v1';\n /**\n * Hash-chain events per agent so deletion of a single audit row\n * becomes detectable. Default `false` (no chaining). When enabled,\n * the SDK sets `prev_event_hash` on each event to SHA-256 of the\n * previous event's canonical bytes for the same agent. The first\n * event after enable / process restart has `prev_event_hash = null`,\n * which the verifier reports as `chain_restart` (informational, not\n * a failure). Chain integrity is checked by `@vorim/verify`.\n */\n chainEvents?: boolean;\n /**\n * Client-side fail-open behaviour for {@link VorimSDK.beforeAction} when\n * the runtime decision API itself is unreachable (network error, DNS,\n * timeout, or 5xx). Default `true`: a transport failure returns a\n * synthetic `fallback` decision (`decision: 'fallback'`, `isFallback:\n * true`) so a momentary control-plane outage does not block the agent.\n * Set `false` to fail closed — `beforeAction` re-throws the transport\n * error and the caller must decide.\n *\n * Note: this governs only transport failures. A reachable server that\n * returns `deny` still denies regardless of this flag.\n */\n runtimeFailOpen?: boolean;\n}\n\n/**\n * VAIP v0 canonical bytes used by Vorim's per-event signing.\n *\n * Pipe-joined content fields with empty-string substitution for missing values.\n * This is intentionally duplicated from `@vorim/shared-types` so the published\n * SDK ships with zero runtime dependencies. A parity test in this package\n * imports both definitions and asserts byte-exact equality across a fixture\n * matrix, so they cannot drift silently.\n *\n * To upgrade the format, version the function (`canonicalPayloadV1`) — never\n * edit this one. Old signatures must remain verifiable.\n */\nexport function canonicalPayloadV0(event: AuditEventInput): string {\n return [\n event.event_type,\n event.action,\n event.resource ?? '',\n event.input_hash ?? '',\n event.output_hash ?? '',\n event.result,\n ].join('|');\n}\n\n/**\n * VAIP v1 canonical bytes for audit-event signing (RFC 8785 JCS).\n *\n * Signs the full event object excluding `signature` (the field being\n * computed) and `canonical_form` (metadata about the recipe). Unlike v0,\n * v1 covers the replayable-evidence fields and the metadata field.\n *\n * Re-exports `jcsCanonicalise` from `./replay.js` which is the byte-exact\n * twin of `@vorim/shared-types`' implementation. The cross-language parity\n * script enforces equivalence with the Python SDK.\n */\nexport function canonicalPayloadV1(event: AuditEventInput): string {\n // Strip the fields that are out of scope for v1 signing.\n const { signature: _sig, canonical_form: _cf, ...rest } = event as Record<string, unknown> & AuditEventInput;\n return jcsCanonicalise(rest);\n}\n\nexport class VorimSDK {\n private apiKey: string;\n private baseUrl: string;\n private timeout: number;\n private autoSign: boolean;\n private canonicalForm: 'v0' | 'v1';\n private chainEvents: boolean;\n private runtimeFailOpen: boolean;\n /**\n * In-memory keyring mapping agent_id -> PEM-encoded Ed25519 private key.\n * Populated automatically by register() and registerEphemeral(), or\n * explicitly via useAgentKey(). Lost on process restart by design; the\n * caller is responsible for durable key storage.\n */\n private agentKeys: Map<string, string> = new Map();\n /**\n * Per-agent hash of the last emitted event's canonical bytes. Used to\n * populate prev_event_hash on the next emit when chainEvents is on.\n * Empty string ↔ no previous event (chain restart). Reset by\n * forgetAgentKey() so reusing an agent_id after revocation doesn't\n * link to the old chain.\n */\n private lastEventHash: Map<string, string> = new Map();\n /**\n * Per-agent monotonic counter incremented by `forgetAgentKey`. An\n * emit that started before `forgetAgentKey` ran will read a stale\n * epoch and refuse to repopulate the chain tail, preventing the\n * revoked agent from inheriting a new lastEventHash from the\n * in-flight emit that the lock was holding.\n */\n private agentForgetEpoch: Map<string, number> = new Map();\n /**\n * Per-agent emit promise. Each new emit awaits the previous one so\n * the chain is constructed in order. Concurrent emits to the same\n * agent are serialised (correct); concurrent emits to different\n * agents remain parallel (fast).\n */\n private chainLocks: Map<string, Promise<void>> = new Map();\n\n constructor(config: VorimConfig) {\n this.apiKey = config.apiKey;\n this.baseUrl = (config.baseUrl || 'https://api.vorim.ai').replace(/\\/$/, '') + '/v1';\n this.timeout = config.timeout || 10000;\n this.autoSign = config.autoSign !== false;\n // Default to v1 as of 3.4.0. v0 only covers six fields and any\n // metadata, delegation context, or replayable-evidence field is\n // unsigned tail — see crypto-1 in the deep-audit findings. Callers\n // who explicitly want v0 (legacy verifier compatibility) must opt\n // in. A console.warn fires the first time an SDK instance is\n // created with v0 so the deprecation is visible in dev.\n this.canonicalForm = config.canonicalForm ?? 'v1';\n if (config.canonicalForm === 'v0') {\n try {\n // eslint-disable-next-line no-console\n console.warn(\n '[@vorim/sdk] canonicalForm=v0 is deprecated: it leaves metadata, ' +\n 'delegation context, and replayable-evidence fields unsigned. ' +\n 'Switch to canonicalForm=v1 (the new default in 3.4.0+) for full ' +\n 'tamper coverage. Pass canonicalForm: \"v0\" explicitly only if you ' +\n 'need byte-compat with verifiers older than @vorim/verify 0.2.0.',\n );\n } catch { /* ignore logger errors */ }\n }\n this.chainEvents = config.chainEvents ?? false;\n this.runtimeFailOpen = config.runtimeFailOpen ?? true;\n }\n\n /**\n * Register a previously-issued agent keypair so this SDK instance can\n * auto-sign events for it on emit. Use this when restoring an agent across\n * process restarts: read the agent's private_key from durable storage,\n * call useAgentKey(agentId, privateKey), and emit() will sign automatically.\n */\n useAgentKey(agentId: string, privateKeyPem: string): void {\n this.agentKeys.set(agentId, privateKeyPem);\n }\n\n /**\n * Forget an agent's signing key (e.g. after revocation). Subsequent emit()\n * calls for that agent will pass through unsigned unless a key is provided\n * inline. Also clears the per-agent chain state — re-using the same\n * agent_id after revocation does NOT link to the old chain.\n */\n forgetAgentKey(agentId: string): void {\n this.agentKeys.delete(agentId);\n this.lastEventHash.delete(agentId);\n this.chainLocks.delete(agentId);\n // Bump the per-agent epoch so any in-flight emit that completes\n // after this call refuses to write back a chain tail. Without\n // this, an emit that holds the chain lock can repopulate\n // lastEventHash after forgetAgentKey clears it, defeating the\n // revoked-agent isolation guarantee.\n this.agentForgetEpoch.set(agentId, (this.agentForgetEpoch.get(agentId) ?? 0) + 1);\n }\n\n /**\n * Cap on how many per-agent chain entries the SDK tracks. Set\n * generously — most callers will hold ≤ 1k agent_ids in memory.\n * Long-running processes that cycle through many agent_ids without\n * calling forgetAgentKey (e.g. ephemeral test agents) would\n * otherwise grow chainLocks / agentForgetEpoch / lastEventHash\n * unbounded.\n */\n private static readonly MAX_CHAIN_STATE_ENTRIES = 10_000;\n\n /**\n * Evict the oldest chain state entries when we cross the cap. Called\n * lazily from prepareEventChained — no scheduled task, no background\n * timer; only runs when the cap is breached during a real emit.\n */\n private evictChainStateIfNeeded(): void {\n if (this.chainLocks.size <= VorimSDK.MAX_CHAIN_STATE_ENTRIES) return;\n const toRemove = Math.ceil(this.chainLocks.size * 0.1); // drop oldest 10 %\n let removed = 0;\n for (const agentId of this.chainLocks.keys()) {\n if (removed >= toRemove) break;\n // Skip agents with an active key — they're in use.\n if (this.agentKeys.has(agentId)) continue;\n this.chainLocks.delete(agentId);\n this.lastEventHash.delete(agentId);\n this.agentForgetEpoch.delete(agentId);\n removed++;\n }\n }\n\n // ─── Health Check ────────────────────────────────────────────────\n\n /**\n * Ping the Vorim API to verify connectivity and API key validity.\n * Returns { status, timestamp } on success, throws VorimError on failure.\n */\n async ping(): Promise<{ status: string; timestamp: string }> {\n const response = await fetch(`${this.baseUrl.replace('/v1', '')}/health`, {\n headers: { 'User-Agent': USER_AGENT },\n signal: AbortSignal.timeout(this.timeout),\n });\n if (!response.ok) throw new VorimError(response.status, 'UNREACHABLE', 'Vorim API is not reachable');\n return response.json() as Promise<{ status: string; timestamp: string }>;\n }\n\n // ─── Agent Identity ────────────────────────────────────────────────\n\n /**\n * Register a new agent with Vorim AI.\n * Returns the agent identity and a private key (shown once).\n *\n * Side effect: the returned private_key is cached in this SDK instance's\n * in-memory keyring so subsequent emit() calls for this agent auto-sign.\n * The keyring is process-local; the caller is responsible for persisting\n * the private_key to durable storage if the agent should survive restarts.\n */\n async register(input: AgentRegistrationInput): Promise<AgentRegistrationResult> {\n const result = await this.post('/agents', input) as AgentRegistrationResult;\n if (result?.agent?.agent_id && result?.private_key) {\n this.agentKeys.set(result.agent.agent_id, result.private_key);\n }\n return result;\n }\n\n /**\n * Verify an agent's identity via the public Trust API.\n */\n async verify(agentId: string): Promise<TrustRecord> {\n return this.get(`/trust/verify/${agentId}`);\n }\n\n /**\n * Get agent details.\n */\n async getAgent(agentId: string): Promise<Agent> {\n return this.get(`/agents/${agentId}`);\n }\n\n /**\n * List all agents in the organisation.\n */\n async listAgents(params?: { page?: number; per_page?: number; status?: string }): Promise<{ agents: Agent[]; meta: any }> {\n const qs = new URLSearchParams(params as any).toString();\n // The API returns { data: Agent[], meta }. The generic get() unwraps to\n // `data` only, dropping `meta`, so read the full envelope here to honour\n // the documented { agents, meta } return shape (pagination relies on meta).\n const env = await this.requestEnvelope('GET', `/agents${qs ? '?' + qs : ''}`);\n return { agents: (env.data ?? []) as Agent[], meta: env.meta ?? null };\n }\n\n /**\n * Update an agent's metadata.\n */\n async updateAgent(agentId: string, updates: Partial<Pick<Agent, 'name' | 'description' | 'status' | 'capabilities'>>): Promise<Agent> {\n return this.patch(`/agents/${agentId}`, updates);\n }\n\n /**\n * Revoke an agent (permanent deactivation).\n */\n async revoke(agentId: string): Promise<void> {\n await this.delete(`/agents/${agentId}`);\n }\n\n // ─── Permissions ──────────────────────────────────────────────────\n\n /**\n * Check if an agent has a specific permission scope.\n * Target: < 5ms response via Redis cache.\n */\n async check(agentId: string, scope: PermissionScope): Promise<PermissionCheckResult> {\n return this.post(`/agents/${agentId}/permissions/verify`, { scope });\n }\n\n /**\n * Grant a permission scope to an agent.\n */\n async grant(agentId: string, scope: PermissionScope, options?: {\n valid_until?: string;\n rate_limit?: { max: number; window: string };\n }): Promise<any> {\n return this.post(`/agents/${agentId}/permissions`, { scope, ...options });\n }\n\n /**\n * List all active permissions for an agent.\n */\n async listPermissions(agentId: string): Promise<any[]> {\n return this.get(`/agents/${agentId}/permissions`);\n }\n\n /**\n * Revoke a specific permission scope from an agent.\n */\n async revokePermission(agentId: string, scope: PermissionScope): Promise<any> {\n return this.delete(`/agents/${agentId}/permissions/${scope}`);\n }\n\n // ─── Agent-to-Agent Identity Delegation (VAIP -02 § 5) ──────────────\n\n /**\n * Delegate the right to act on this agent's behalf to another agent\n * in the same org, with a strict subset of this agent's scopes.\n *\n * Multi-hop chains are supported up to depth 32. Scope subset is\n * enforced at every hop. Audit events emitted by the delegate carry\n * the chain context (`on_behalf_of`, `delegator_agent_id`,\n * `delegation_chain_id`, `delegation_depth`) so a verifier walking\n * the bundle can reconstruct the full chain.\n *\n * @example\n * ```ts\n * const chain = await vorim.delegateToAgent('agid_parent_abc', {\n * delegate_agent_id: 'agid_child_xyz',\n * scopes_delegated: ['agent:read', 'agent:execute'],\n * max_chain_depth: 1, // delegate may make ONE further hop\n * valid_until: '2026-12-31T00:00:00Z',\n * });\n * console.log(chain.public_chain_id); // chain_<hex>\n * ```\n */\n async delegateToAgent(\n delegatorAgentId: string,\n input: {\n delegate_agent_id: string;\n scopes_delegated: PermissionScope[];\n max_chain_depth?: number;\n valid_until?: string;\n /**\n * Optional Ed25519-signed delegation link (VAIP -02 § 5). When\n * provided, the server verifies the signature against the\n * delegator's stored public key, persists alongside the chain\n * row, and exports it in bundle delegation_tokens so the chain\n * is offline-verifiable by `@vorim/verify`. Compute via\n * {@link signDelegationLink} or set manually if signing\n * elsewhere.\n */\n signed_link?: {\n claims: DelegationLinkClaims;\n signature: string;\n };\n },\n ): Promise<AgentDelegationRecord> {\n return this.post(`/agents/${delegatorAgentId}/delegations`, input);\n }\n\n /**\n * Sign a delegation link with the delegator's private key. Returns\n * the signed link in the shape accepted by `delegateToAgent`'s\n * `signed_link` field.\n *\n * The signature is Ed25519 over the RFC 8785 JCS-canonical bytes of\n * the claims object. Same algorithm the server and `@vorim/verify`\n * use for verification.\n *\n * Use this when the delegator's private key is in this SDK's\n * keyring (i.e. set via `register()` or `useAgentKey()`).\n *\n * @example\n * ```ts\n * const signed = await vorim.signDelegationLink({\n * v: 0,\n * type: 'vaip-delegation-link',\n * chain_id: 'chain_abc', // server returns this on first delegation\n * depth: 1,\n * delegator: 'agid_parent',\n * delegate: 'agid_child',\n * scopes: ['agent:read', 'agent:execute'],\n * max_chain_depth: 1,\n * valid_from: new Date().toISOString(),\n * valid_until: null,\n * parent_link_hash: null,\n * });\n * ```\n */\n async signDelegationLink(\n claims: DelegationLinkClaims,\n ): Promise<{ claims: DelegationLinkClaims; signature: string }> {\n const key = this.agentKeys.get(claims.delegator);\n if (!key) {\n throw new VorimError(\n 400,\n 'NO_AGENT_KEY',\n `SDK has no private key for delegator ${claims.delegator}`,\n );\n }\n // Sign using the SDK's local JCS. Byte-equivalent to shared-types\n // and the server (enforced by scripts/check-replay-parity.sh).\n const bytes = jcsCanonicalise(claims as unknown as Record<string, unknown>);\n const signature = await this.sign(bytes, key);\n return { claims, signature };\n }\n\n /**\n * List active identity-chain entries this agent participates in\n * (either as delegator or delegate). Useful for showing the current\n * delegation graph in an admin UI.\n */\n async listAgentDelegations(agentId: string): Promise<AgentDelegationRecord[]> {\n return this.get(`/agents/${agentId}/delegations`);\n }\n\n /**\n * Walk a delegation chain by its public id. Returns the full chain\n * in depth order.\n */\n async getDelegationChain(\n agentId: string,\n chainId: string,\n ): Promise<AgentDelegationRecord[]> {\n return this.get(`/agents/${agentId}/delegation-chain/${chainId}`);\n }\n\n /**\n * Revoke this agent's delegation in a given chain. Cascades to all\n * descendants beneath this agent's depth in the chain.\n */\n async revokeAgentDelegation(\n agentId: string,\n chainId: string,\n ): Promise<{ revoked: number }> {\n return this.delete(`/agents/${agentId}/delegations/${chainId}`);\n }\n\n // ─── Runtime Control (Strategy A) ──────────────────────────────────\n\n /**\n * Gate an agent action BEFORE it is performed. Calls\n * `POST /v1/runtime/decisions` and returns a typed {@link RuntimeDecision}.\n *\n * By default (`throwOnDeny: true`) a `deny` verdict throws\n * {@link VorimDeniedError} — deny is carried in the response body, not\n * the HTTP status, so without this the caller would treat a denial as\n * success. Pass `{ throwOnDeny: false }` to handle deny yourself.\n *\n * On a `modify` verdict, use `decision.modifiedPayload` in place of your\n * original payload (e.g. a policy rule masked PII). This is\n * client-cooperative: Vorim returns the sanitised payload but does not sit\n * inline and does not enforce that you send it — carry `decisionId` into\n * the matching {@link emit} so the action stays auditable. On `escalate`,\n * poll {@link waitForDecisionResolution} for the human decision.\n *\n * Transport failures (network/DNS/timeout/5xx) respect the constructor's\n * `runtimeFailOpen` flag: when true (default) a synthetic `fallback`\n * decision is returned so a control-plane blip does not block the agent;\n * when false the underlying error is re-thrown.\n *\n * Always pass the public `agid_*` id as `agentId`.\n *\n * @example\n * const d = await vorim.beforeAction({\n * agentId: 'agid_acme_evilbot',\n * actionType: 'tool_call',\n * actionTarget: 'sendEmail',\n * requiredScope: 'agent:communicate',\n * payload: { to: 'customer@example.com' },\n * });\n * if (d.decision === 'allow') await sendEmail(d.modifiedPayload ?? payload);\n */\n async beforeAction(\n input: BeforeActionInput,\n options: { throwOnDeny?: boolean } = {},\n ): Promise<RuntimeDecision> {\n const throwOnDeny = options.throwOnDeny ?? true;\n const body = {\n agent_id: input.agentId,\n action_type: input.actionType,\n action_target: input.actionTarget,\n payload: input.payload,\n context: input.context,\n required_scope: input.requiredScope,\n idempotency_key: input.idempotencyKey,\n workflow_id: input.workflowId,\n estimated_tokens: input.estimatedTokens,\n };\n\n let raw: any;\n try {\n raw = await this.post('/runtime/decisions', body);\n } catch (err) {\n // Transport-level failure. A VorimError carrying a real HTTP status\n // < 500 (e.g. 400/403/404) is a genuine server verdict/validation\n // error and must propagate. Only network errors and 5xx are\n // candidates for client-side fail-open.\n const status = err instanceof VorimError ? err.status : 0;\n const code = err instanceof VorimError ? err.code : undefined;\n // A malformed (non-JSON) control-plane response is NOT eligible for\n // fail-open: we can't trust a policy plane we can't parse, so it must\n // fail closed (the error propagates and the gate denies). Everything\n // else with status 0 or >= 500 is a genuine transport failure.\n const isTransport = (status === 0 || status >= 500) && code !== 'MALFORMED_RESPONSE';\n if (isTransport && this.runtimeFailOpen) {\n return {\n decisionId: '',\n decision: 'fallback',\n reason: 'Runtime decision API unreachable; client fail-open applied',\n decisionRuleId: null,\n modifiedPayload: null,\n expiresAt: new Date(Date.now() + 60_000).toISOString(),\n latencyMs: 0,\n isFallback: true,\n policyVersion: 0,\n escalationResolution: null,\n };\n }\n throw err;\n }\n\n const decision = this.toRuntimeDecision(raw);\n if (decision.decision === 'deny' && throwOnDeny) {\n throw new VorimDeniedError(decision);\n }\n return decision;\n }\n\n // ─── Token Budgets (AI Cost Management) ──────────────────────────────\n //\n // A budget is a rolling token ceiling for one agent or one workflow.\n // Enforcement runs inside beforeAction(): pass `workflowId` + `estimatedTokens`\n // on the action, and a `token_budget_gt` policy rule denies the action before\n // the spend if it would breach the budget. These methods manage the budgets\n // and read live spend (dashboard-oriented; require a JWT-authed client or an\n // owner/admin API key).\n\n /**\n * Create or update a token budget for an agent or workflow. One budget per\n * (scope, scope_ref) — calling this again for the same target updates it.\n *\n * @example\n * await vorim.setBudget({\n * scope: 'workflow', scope_ref: 'nightly-summariser',\n * max_tokens: 2_000_000, window_seconds: 86400, enforcement: 'hard',\n * });\n */\n async setBudget(input: TokenBudgetInput): Promise<TokenBudget> {\n return this.put('/runtime/budgets', input);\n }\n\n /** List the org's configured token budgets. */\n async listBudgets(): Promise<TokenBudget[]> {\n return this.get('/runtime/budgets');\n }\n\n /** Live rolling spend, remaining allowance, and monthly total per budget. */\n async getBudgetSpend(): Promise<BudgetSpend[]> {\n return this.get('/runtime/budgets/spend');\n }\n\n /** Remove a token budget by id. */\n async deleteBudget(budgetId: string): Promise<void> {\n await this.delete(`/runtime/budgets/${encodeURIComponent(budgetId)}`);\n }\n\n /**\n * Poll a decision until it leaves the `escalate` state (a human\n * approved or denied it) or the timeout elapses. Returns the resolved\n * decision; throws {@link VorimError} `ESCALATION_TIMEOUT` (408) if the\n * decision is still pending when the timeout is reached.\n *\n * Requires an API key with the `runtime:decide` scope (the same scope\n * `beforeAction` uses).\n */\n async waitForDecisionResolution(\n decisionId: string,\n options: { timeoutMs?: number; pollIntervalMs?: number } = {},\n ): Promise<RuntimeDecision> {\n const timeoutMs = options.timeoutMs ?? 300_000;\n const pollIntervalMs = options.pollIntervalMs ?? 1000;\n const start = Date.now();\n // Always poll at least once even if timeoutMs is 0.\n do {\n const raw = await this.get(`/runtime/decisions/${decisionId}`);\n // The GET projection carries escalation_resolution; once set (or\n // once the verdict is no longer 'escalate') the escalation is done.\n if (raw?.decision !== 'escalate' || raw?.escalation_resolution) {\n return this.toRuntimeDecision(raw);\n }\n // Sleep only as long as the deadline allows, so the final throw\n // fires at ~timeoutMs rather than up to one pollIntervalMs late.\n const remaining = timeoutMs - (Date.now() - start);\n if (remaining <= 0) break;\n await sleep(Math.min(pollIntervalMs, remaining));\n } while (Date.now() - start < timeoutMs);\n throw new VorimError(\n 408,\n 'ESCALATION_TIMEOUT',\n `Decision ${decisionId} still pending after ${timeoutMs}ms`,\n { decisionId },\n );\n }\n\n /**\n * Map a snake_case decision row (from POST or GET) to the camelCase\n * {@link RuntimeDecision}. The SDK has no generic camelizer, so the\n * mapping is explicit — and tolerant of either the POST shape\n * (`decision_rule_id`, `latency_ms`) or absent fields on a GET row.\n *\n * Escalation-resolution translation (load-bearing): the server resolves\n * an escalation by setting `escalation_resolution` but does NOT flip the\n * row's `decision` column off `'escalate'`. If we returned that verbatim,\n * a caller's obvious `if (d.decision === 'deny')` check would never fire\n * on a human DENIAL — a silent fail-open. So when a resolution is present\n * we translate the verdict: approved → 'allow', denied → 'deny'. The raw\n * resolution is also surfaced as `escalationResolution` for callers that\n * want it explicitly.\n *\n * A missing verdict (malformed/truncated response) fails CLOSED to 'deny'\n * rather than 'fallback', so the deny check fires on garbled input.\n */\n private toRuntimeDecision(raw: any): RuntimeDecision {\n // Runtime-narrow to the enum: a surprise server value (e.g. 'pending')\n // collapses to null rather than leaking through the typed field, and\n // never drives a translation.\n const resolution: 'approved' | 'denied' | null =\n raw?.escalation_resolution === 'approved' ? 'approved'\n : raw?.escalation_resolution === 'denied' ? 'denied'\n : null;\n const hasVerdict = typeof raw?.decision === 'string';\n let decision: DecisionVerdict;\n // Only translate the resolution when the row is actually an escalation.\n // Gating on raw.decision==='escalate' means a resolution that somehow\n // appears on a payload-bearing 'modify' row can never override it and\n // drop modifiedPayload (defensive — the server only sets resolution on\n // escalate rows today).\n if (raw?.decision === 'escalate' && resolution === 'approved') decision = 'allow';\n else if (raw?.decision === 'escalate' && resolution === 'denied') decision = 'deny';\n else if (hasVerdict) decision = raw.decision as DecisionVerdict;\n else decision = 'deny'; // fail closed on a verdict-less response\n\n return {\n decisionId: raw?.decision_id ?? '',\n decision,\n reason: raw?.reason ?? (hasVerdict ? '' : 'Malformed decision response (no verdict); failing closed'),\n decisionRuleId: raw?.decision_rule_id ?? null,\n modifiedPayload: raw?.modified_payload ?? null,\n // Avoid '' (Date.parse('') === NaN). A verdict-less row gets an epoch\n // timestamp so any staleness check treats it as already expired.\n expiresAt: raw?.expires_at ?? (hasVerdict ? '' : new Date(0).toISOString()),\n latencyMs: raw?.latency_ms ?? 0,\n isFallback: raw?.is_fallback ?? !hasVerdict,\n policyVersion: raw?.policy_version ?? 0,\n escalationResolution: resolution,\n };\n }\n\n // ─── Audit ────────────────────────────────────────────────────────\n\n /**\n * Emit an audit event for an agent action.\n *\n * By default (autoSign=true on the SDK), the event is signed with the\n * agent's private key before it leaves the process. Set options.sign=false\n * to skip signing (e.g. for testing). If the SDK has no private key for the\n * event's agent_id, the event is sent unsigned and a warning is not\n * raised — callers that require signing should check event.signature on\n * the returned event after a round-trip, or use a server-side enforcement\n * flag (VORIM_VERIFY_AUDIT_SIGNATURES).\n */\n async emit(event: AuditEventInput, options?: { sign?: boolean }): Promise<{ ingested: number }> {\n const prepared = await this.prepareEvent(event, options?.sign);\n const result = await this.post('/audit/events', { events: [prepared] }) as { ingested: number };\n this.warnOnPartialIngest(1, result?.ingested ?? 0);\n return result;\n }\n\n /**\n * Emit a batch of audit events (up to 1,000). Each event is signed\n * independently using its agent_id to look up the signing key. See\n * {@link VorimSDK.emit} for signing behaviour and opt-out.\n *\n * When the server returns `ingested < events.length` (e.g. one event\n * referenced an unknown agent_id), a `console.warn` is emitted so\n * operators don't silently lose audit rows. The result is returned\n * unchanged so callers can inspect / act on it.\n */\n async emitBatch(events: AuditEventInput[], options?: { sign?: boolean }): Promise<{ ingested: number }> {\n const prepared = await Promise.all(events.map(e => this.prepareEvent(e, options?.sign)));\n const result = await this.post('/audit/events', { events: prepared }) as { ingested: number };\n this.warnOnPartialIngest(events.length, result?.ingested ?? 0);\n return result;\n }\n\n private warnOnPartialIngest(submitted: number, ingested: number): void {\n if (ingested < submitted) {\n try {\n // eslint-disable-next-line no-console\n console.warn(\n `[@vorim/sdk] partial ingest: server stored ${ingested}/${submitted} events. ` +\n `Common causes: unknown agent_id (cross-org or typo), invalid signature when strict ` +\n `verification is enabled.`,\n );\n } catch {\n /* ignore logger errors */\n }\n }\n }\n\n /**\n * Internal: prepare an event for transmission. Auto-signs if the SDK has a\n * key for this agent and signing isn't explicitly opted out. Pre-existing\n * signatures on the event are respected (caller-signed events are not\n * re-signed). Per-agent failure is non-fatal: if signing throws, the event\n * still sends unsigned so a single bad key doesn't break a batch.\n *\n * Canonical-form dispatch:\n * - If the event already carries `canonical_form`, that wins (caller\n * opted in/out for this specific event).\n * - Otherwise the SDK-level `canonicalForm` (config) applies.\n * - v0 signs the pipe-joined 6 fields. v1 signs the RFC 8785 JCS\n * bytes over the full event minus signature and canonical_form,\n * and the event goes on the wire with `canonical_form: \"v1\"`.\n *\n * Chain construction:\n * - When chainEvents is on, the event gets `prev_event_hash` set to\n * the SHA-256 of the previous event's canonical bytes for the\n * same agent, set BEFORE the signature is computed (so v1\n * signatures cover the chain link).\n * - Chain ops are serialised per-agent via `chainLocks` so\n * concurrent emits to the same agent build a deterministic chain.\n */\n private async prepareEvent(\n event: AuditEventInput,\n signOverride?: boolean\n ): Promise<AuditEventInput> {\n const shouldSign = signOverride ?? this.autoSign;\n if (!shouldSign && !this.chainEvents) return event;\n // Even unsigned events participate in the chain if chaining is on.\n if (this.chainEvents) {\n return this.prepareEventChained(event, shouldSign);\n }\n if (!shouldSign) return event;\n if (event.signature) return event;\n return this.signEventNow(event, shouldSign);\n }\n\n /** Serialise per-agent and apply chain hash + sign. */\n private async prepareEventChained(\n event: AuditEventInput,\n shouldSign: boolean,\n ): Promise<AuditEventInput> {\n const agentId = event.agent_id;\n this.evictChainStateIfNeeded();\n const prior = this.chainLocks.get(agentId) ?? Promise.resolve();\n let release!: () => void;\n const next = new Promise<void>(r => { release = r; });\n this.chainLocks.set(agentId, prior.then(() => next));\n await prior;\n // Snapshot the forget-epoch BEFORE doing any chain work. If the\n // epoch advances while we're in flight, forgetAgentKey() ran and\n // we must not write back the chain tail.\n const startEpoch = this.agentForgetEpoch.get(agentId) ?? 0;\n try {\n // Insert prev_event_hash if we have a previous bytes-hash for this\n // agent. First event after enable / process restart has none.\n const prev = this.lastEventHash.get(agentId);\n const eventWithPrev: AuditEventInput = prev\n ? { ...event, prev_event_hash: prev }\n : event;\n\n // Sign (or pass through unsigned if signing not requested / no\n // key). If forgetAgentKey runs between the lock acquisition and\n // the sign call, skip signing — the key has been revoked and\n // we must not use it. The event still ships unsigned, which the\n // caller can detect via the missing `signature` field.\n const epochBeforeSign = this.agentForgetEpoch.get(agentId) ?? 0;\n const keyStillPresent = this.agentKeys.has(agentId);\n const prepared = shouldSign && !eventWithPrev.signature && keyStillPresent && epochBeforeSign === startEpoch\n ? await this.signEventNow(eventWithPrev, shouldSign)\n : eventWithPrev;\n\n // Record this event's canonical bytes hash as the chain's new tail.\n // Use the v1 form for v1 events (matches what the signature covered);\n // v0 form for v0 events.\n const wireForm = (prepared.canonical_form as 'v0' | 'v1' | undefined) ?? 'v0';\n const bytes = wireForm === 'v1' ? canonicalPayloadV1(prepared) : canonicalPayloadV0(prepared);\n try {\n const tail = await hashPreviousEvent(bytes);\n // Only commit the tail if forgetAgentKey didn't run while we\n // were in flight. Without this guard the revoked agent\n // silently inherits a new chain tail from the in-flight emit.\n if ((this.agentForgetEpoch.get(agentId) ?? 0) === startEpoch) {\n this.lastEventHash.set(agentId, tail);\n }\n } catch {\n // hashing failure (extreme edge) is non-fatal; chain restarts next time\n this.lastEventHash.delete(agentId);\n }\n return prepared;\n } finally {\n release();\n }\n }\n\n /** Sign an event right now, no chain handling.\n *\n * On signing failure (malformed key, Web Crypto unavailable, etc.)\n * the event ships with its requested `canonical_form` preserved and\n * a one-line warning is logged via `console.warn`. The previous\n * behaviour silently dropped both the signature AND the\n * canonical_form marker, which caused the event to be classified\n * server-side as a v0 `signature_missing` instead of a v1 attempt\n * that failed — making the breakage invisible to operators. */\n private async signEventNow(\n event: AuditEventInput,\n _shouldSign: boolean,\n ): Promise<AuditEventInput> {\n const key = this.agentKeys.get(event.agent_id);\n if (!key) return event;\n const form: 'v0' | 'v1' = (event.canonical_form as 'v0' | 'v1' | undefined) ?? this.canonicalForm;\n const withForm: AuditEventInput = form === 'v0'\n ? event\n : { ...event, canonical_form: 'v1' };\n try {\n const payload = form === 'v1' ? canonicalPayloadV1(withForm) : canonicalPayloadV0(withForm);\n const signature = await this.sign(payload, key);\n return { ...withForm, signature };\n } catch (err) {\n // Surface the failure rather than silently dropping the signature.\n // Operators need a signal to detect broken signing at the SDK\n // boundary; verifiers will otherwise misattribute the cause.\n try {\n // eslint-disable-next-line no-console\n console.warn(\n // Don't echo agent_id verbatim — emit a short prefix for\n // correlation while keeping log-sink content lower-cardinality\n // and avoiding accidental leakage of org-identifiable ids.\n `[@vorim/sdk] signing failed for agent_id=${(event.agent_id ?? '').slice(0, 12)}... form=${form}: ${(err as Error)?.message ?? err}. ` +\n `Event will be emitted unsigned with canonical_form retained.`,\n );\n } catch {\n /* ignore logger errors */\n }\n // Preserve canonical_form so the event is still classified as a\n // v1 attempt server-side, just without a signature. This makes\n // the breakage detectable via the signature_missing + v1 form\n // pair instead of looking like a legacy v0 unsigned event.\n return withForm;\n }\n }\n\n /**\n * Export a signed audit bundle for a date range.\n * The server only supports 'json' (the sole allowed export format).\n */\n async exportAudit(from: string, to: string, format: 'json' = 'json'): Promise<any> {\n return this.post('/audit/export', { from, to, format });\n }\n\n // ─── API Keys ──────────────────────────────────────────────────────\n\n /**\n * List all API keys for the organisation.\n * Requires an API key with the apikeys:read scope (or a dashboard JWT).\n */\n async listApiKeys(): Promise<any[]> {\n return this.get('/api-keys');\n }\n\n /**\n * Create a new API key.\n * Requires an API key with the apikeys:write scope (or a dashboard JWT).\n */\n async createApiKey(name: string, options?: { scopes?: string[]; expires_at?: string }): Promise<any> {\n return this.post('/api-keys', { name, ...options });\n }\n\n /**\n * Revoke an API key. Returns the revoked key object (server returns the\n * formatted api_key record, not a { revoked } flag).\n * Requires an API key with the apikeys:write scope (or a dashboard JWT).\n */\n async deleteApiKey(keyId: string): Promise<any> {\n return this.delete(`/api-keys/${keyId}`);\n }\n\n // ─── Ephemeral Agents ──────────────────────────────────────────────\n\n /**\n * Register an ephemeral agent with W3C did:key identity.\n * The agent auto-expires after the specified TTL.\n *\n * Side effect: the returned private_key is cached in this SDK instance's\n * in-memory keyring (matching register()).\n */\n async registerEphemeral(input: {\n capabilities: string[];\n scopes: string[];\n ttl_seconds?: number;\n }): Promise<any> {\n const result = await this.post('/agents/ephemeral', input);\n const agentId: string | undefined = result?.agent?.agent_id ?? result?.did_key;\n if (agentId && result?.private_key) {\n this.agentKeys.set(agentId, result.private_key);\n }\n return result;\n }\n\n // ─── Credential Delegation ──────────────────────────────────────────\n\n /**\n * Register an OAuth provider for credential delegation.\n */\n async registerProvider(input: {\n provider_key: string;\n display_name?: string;\n client_id: string;\n client_secret: string;\n auth_url: string;\n token_url: string;\n revoke_url?: string;\n scopes_available?: string[];\n }): Promise<any> {\n return this.post('/credentials/providers', input);\n }\n\n /**\n * List registered OAuth providers.\n */\n async listProviders(): Promise<any[]> {\n return this.get('/credentials/providers');\n }\n\n /**\n * Store an OAuth connection (user's authorized tokens).\n */\n async storeConnection(input: {\n provider_id: string;\n refresh_token: string;\n scopes_granted: string[];\n external_account_id?: string;\n }): Promise<any> {\n return this.post('/credentials/connections', input);\n }\n\n /**\n * List OAuth connections.\n */\n async listConnections(): Promise<any[]> {\n return this.get('/credentials/connections');\n }\n\n /**\n * Delegate a credential to an agent.\n * The agent will be able to request short-lived access tokens\n * for the delegated scopes without ever seeing the refresh token.\n */\n async delegateCredential(input: {\n connection_id: string;\n agent_id: string;\n scopes_delegated: string[];\n max_requests_per_hr?: number;\n valid_until?: string;\n }): Promise<any> {\n return this.post('/credentials/delegations', input);\n }\n\n /**\n * List credential delegations for the organisation or a specific agent.\n */\n async listDelegations(agentId?: string): Promise<any[]> {\n const params = agentId ? `?agent_id=${agentId}` : '';\n return this.get(`/credentials/delegations${params}`);\n }\n\n /**\n * Revoke a credential delegation (cascades to delegation chains).\n */\n async revokeDelegation(delegationId: string): Promise<{ revoked: boolean }> {\n return this.delete(`/credentials/delegations/${delegationId}`);\n }\n\n /**\n * Request a short-lived access token for an agent.\n * The agent must have an active credential delegation.\n * The refresh token is never exposed — the platform proxies the request.\n */\n async requestToken(input: {\n agent_id: string;\n scope: string;\n provider_id?: string;\n }): Promise<{\n access_token: string;\n token_type: string;\n expires_in: number;\n scope: string;\n delegation_id: string;\n }> {\n return this.post('/credentials/token', input);\n }\n\n // ─── Signing ──────────────────────────────────────────────────────\n\n /**\n * Sign a payload with an Ed25519 private key (client-side).\n * Uses the Web Crypto API or Node.js crypto.\n */\n async sign(payload: string, privateKeyPem: string): Promise<string> {\n if (typeof globalThis.crypto?.subtle !== 'undefined') {\n // Web Crypto API\n const keyData = this.pemToArrayBuffer(privateKeyPem);\n const key = await globalThis.crypto.subtle.importKey(\n 'pkcs8', keyData, { name: 'Ed25519' }, false, ['sign']\n );\n const signature = await globalThis.crypto.subtle.sign(\n 'Ed25519', key, new TextEncoder().encode(payload)\n );\n return `ed25519:${this.arrayBufferToBase64(signature)}`;\n } else {\n // Node.js crypto fallback\n const crypto = await import('node:crypto');\n const sign = crypto.sign(null, Buffer.from(payload), privateKeyPem);\n return `ed25519:${sign.toString('base64')}`;\n }\n }\n\n // ─── HTTP Client ──────────────────────────────────────────────────\n\n private async get(path: string): Promise<any> {\n return this.request('GET', path);\n }\n\n private async post(path: string, body: any): Promise<any> {\n return this.request('POST', path, body);\n }\n\n private async patch(path: string, body: any): Promise<any> {\n return this.request('PATCH', path, body);\n }\n\n private async put(path: string, body: any): Promise<any> {\n return this.request('PUT', path, body);\n }\n\n private async delete(path: string): Promise<any> {\n return this.request('DELETE', path);\n }\n\n /** Like request() but returns the FULL { data, meta, ... } envelope\n * instead of unwrapping to `data`. Used where meta (pagination) matters. */\n private async requestEnvelope(method: string, path: string, body?: any): Promise<Record<string, any>> {\n return this.request(method, path, body, false);\n }\n\n private async request(method: string, path: string, body?: any, unwrap = true): Promise<any> {\n const controller = new AbortController();\n const timeoutId = setTimeout(() => controller.abort(), this.timeout);\n\n try {\n const response = await fetch(`${this.baseUrl}${path}`, {\n method,\n headers: {\n 'Authorization': `Bearer ${this.apiKey}`,\n 'Content-Type': 'application/json',\n 'User-Agent': USER_AGENT,\n },\n body: body ? JSON.stringify(body) : undefined,\n signal: controller.signal,\n });\n\n if (!response.ok) {\n const errBody = await response.json().catch(() => ({})) as Record<string, any>;\n throw new VorimError(\n response.status,\n errBody.error?.code || 'UNKNOWN_ERROR',\n errBody.error?.message || `HTTP ${response.status}`,\n errBody.error?.details\n );\n }\n\n // A 204 No Content (e.g. DELETE endpoints) is a legitimate success with\n // an empty body — there is nothing to parse. Return early so the JSON\n // guard below doesn't misclassify the empty body as MALFORMED_RESPONSE.\n if (response.status === 204) {\n return unwrap ? undefined : {};\n }\n\n // Guard the success-body parse: a 2xx whose body is not valid JSON\n // (e.g. a proxy/CDN/captive-portal \"200 OK\" HTML interstitial) would\n // otherwise throw a bare SyntaxError that beforeAction's catch\n // misclassifies as a status-0 transport failure and, under\n // runtimeFailOpen, silently ALLOWS the gated action. We throw a\n // VorimError with a distinct MALFORMED_RESPONSE code so the gate\n // fails closed (see the isTransport check in beforeAction). Python's\n // before_action catches json.JSONDecodeError the same way.\n let json: Record<string, any>;\n try {\n json = await response.json() as Record<string, any>;\n } catch {\n throw new VorimError(502, 'MALFORMED_RESPONSE', 'Control plane returned a non-JSON success response');\n }\n return unwrap ? json.data : json;\n } finally {\n clearTimeout(timeoutId);\n }\n }\n\n private pemToArrayBuffer(pem: string): ArrayBuffer {\n const b64 = pem.replace(/-----[^-]+-----/g, '').replace(/\\s/g, '');\n const binary = atob(b64);\n const bytes = new Uint8Array(binary.length);\n for (let i = 0; i < binary.length; i++) {\n bytes[i] = binary.charCodeAt(i);\n }\n return bytes.buffer;\n }\n\n private arrayBufferToBase64(buffer: ArrayBuffer): string {\n const bytes = new Uint8Array(buffer);\n let binary = '';\n for (const byte of bytes) {\n binary += String.fromCharCode(byte);\n }\n return btoa(binary);\n }\n}\n\nexport class VorimError extends Error {\n constructor(\n public status: number,\n public code: string,\n message: string,\n public details?: Record<string, unknown>\n ) {\n super(message);\n this.name = 'VorimError';\n }\n}\n\n/**\n * Thrown by {@link VorimSDK.beforeAction} when the runtime decision is\n * `deny` and `throwOnDeny` is left at its default (`true`). Carries the\n * full {@link RuntimeDecision} so the caller can inspect the reason and\n * the decision id. Status is 403 with code `DECISION_DENIED`.\n */\nexport class VorimDeniedError extends VorimError {\n constructor(public decision: RuntimeDecision) {\n super(403, 'DECISION_DENIED', decision.reason, { decisionId: decision.decisionId });\n this.name = 'VorimDeniedError';\n }\n}\n\n// ─── Convenience export ──────────────────────────────────────────────\n\nexport default function createVorim(config: VorimConfig): VorimSDK {\n return new VorimSDK(config);\n}\n\n// ─── Device-flow onboarding (RFC 8628) ──────────────────────────────────────\n\nexport interface DeviceLoginOptions {\n baseUrl?: string;\n /** Shown to the human on the consent screen, e.g. \"Cursor\" or \"my-app\". */\n clientName?: string;\n /** Scopes to request; the server clamps to its ceiling regardless. */\n requestedScopes?: string[];\n /**\n * Called once with the code + URL to show the human. The agent MUST surface\n * `user_code` and `verification_uri` verbatim — do not summarise or truncate.\n */\n onUserCode: (info: { user_code: string; verification_uri: string; verification_uri_complete: string; expires_in: number }) => void;\n /** Stop polling after this many ms (default: the server's expires_in). */\n timeoutMs?: number;\n /**\n * After the key is minted, also register a first agent identity with it, so\n * the agent is fully set up in one call. Pass `true` for a sensible default,\n * or an object to customise. The agent's one-time private key is returned in\n * `result.agent.private_key` — store it; it cannot be retrieved again.\n */\n registerAgent?: boolean | { name?: string; capabilities?: string[]; scopes?: string[] };\n}\n\nexport interface DeviceLoginResult {\n api_key: string;\n key_prefix: string;\n scopes: string[];\n org_id: string;\n /** Present only when `registerAgent` was set — the auto-registered identity. */\n agent?: AgentRegistrationResult;\n}\n\n/**\n * Onboard via the device-authorization flow: obtain a scoped Vorim API key by\n * polling, after a human approves in the browser. No existing key needed — this\n * is the pre-auth bootstrap. Resolves with the key once the human approves;\n * rejects on denial, expiry, or timeout (fail-closed — never returns a partial\n * or unverified credential).\n *\n * @example\n * const { api_key } = await deviceLogin({\n * clientName: 'my-app',\n * onUserCode: ({ user_code, verification_uri }) =>\n * console.log(`Approve at ${verification_uri} — code: ${user_code}`),\n * });\n */\nexport async function deviceLogin(opts: DeviceLoginOptions): Promise<DeviceLoginResult> {\n const base = (opts.baseUrl || 'https://api.vorim.ai').replace(/\\/$/, '') + '/v1';\n\n const startRes = await fetch(`${base}/auth/device`, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({ client_name: opts.clientName, requested_scopes: opts.requestedScopes }),\n signal: AbortSignal.timeout(10000),\n });\n if (!startRes.ok) throw new VorimError(startRes.status, 'DEVICE_START_FAILED', `Could not start device authorization (HTTP ${startRes.status})`);\n const start = ((await startRes.json()) as { data: any }).data;\n\n opts.onUserCode({\n user_code: start.user_code,\n verification_uri: start.verification_uri,\n verification_uri_complete: start.verification_uri_complete,\n expires_in: start.expires_in,\n });\n\n const intervalMs = Math.max(1, start.interval || 5) * 1000;\n const deadline = Date.now() + (opts.timeoutMs ?? start.expires_in * 1000);\n let wait = intervalMs;\n\n // Poll until approved, denied, expired, or our deadline.\n for (;;) {\n if (Date.now() >= deadline) throw new VorimError(408, 'DEVICE_TIMEOUT', 'Device authorization timed out before approval');\n await new Promise((r) => setTimeout(r, wait));\n\n const pollRes = await fetch(`${base}/auth/device/token`, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({ device_code: start.device_code }),\n signal: AbortSignal.timeout(10000),\n });\n const json = (await pollRes.json().catch(() => ({}))) as { data?: any; error?: { code?: string } };\n\n if (pollRes.ok && json.data?.api_key) {\n const result: DeviceLoginResult = {\n api_key: json.data.api_key, key_prefix: json.data.key_prefix, scopes: json.data.scopes, org_id: json.data.org_id,\n };\n // Optionally finish setup: register a first agent identity with the\n // freshly-minted key so the caller gets back a fully-wired agent. Done\n // SDK-side (a normal authenticated POST /agents) — the device endpoints\n // themselves never create agents, keeping that audited surface unchanged.\n if (opts.registerAgent) {\n const cfg = typeof opts.registerAgent === 'object' ? opts.registerAgent : {};\n const sdk = new VorimSDK({ apiKey: result.api_key, baseUrl: opts.baseUrl });\n result.agent = await sdk.register({\n name: cfg.name || opts.clientName || 'my-agent',\n capabilities: cfg.capabilities || [],\n scopes: (cfg.scopes as any) || ['agent:read', 'agent:execute'],\n });\n }\n return result;\n }\n const code = json.error?.code;\n if (code === 'access_denied') throw new VorimError(403, 'DEVICE_DENIED', 'The human denied the authorization request');\n if (code === 'expired_token') throw new VorimError(410, 'DEVICE_EXPIRED', 'The device code expired before approval');\n if (code === 'slow_down') { wait += intervalMs; continue; } // back off per RFC 8628\n // authorization_pending (or any transient): keep polling at the base interval.\n wait = intervalMs;\n }\n}\n\n// Re-export types for consumers\nexport type {\n Agent, AgentRegistrationInput, AgentRegistrationResult,\n TrustRecord, AuditEventInput, AuditEventType, AuditResult,\n PermissionScope, PermissionCheckResult, AgentStatus,\n AgentDelegationRecord, DelegationLinkClaims,\n BeforeActionInput, RuntimeDecision, DecisionVerdict,\n TokenBudget, TokenBudgetInput, BudgetSpend, BudgetScope, BudgetEnforcement,\n} from './types.js';\n\n// Re-export replayable evidence helpers (VAIP -02 schema field hashing)\nexport {\n hashTool, hashToolCatalogue, hashSystemPrompt, hashPreviousEvent,\n jcsCanonicalise, prepareReplayContext, CANONICAL_TOOL_CATALOGUE_VERSION,\n} from './replay.js';\nexport type { CatalogueTool, ReplayInputs, ReplayContext } from './replay.js';\n","/**\n * Replayable agent decision evidence helpers.\n *\n * Canonical-form hashing for the VAIP -02 schema fields that the SDK\n * attaches to audit events. The hashes recorded in audit_events.tool_catalogue_hash\n * and audit_events.system_prompt_hash use these functions, so the bytes\n * an auditor or counterparty reconstructs must match what the SDK produced.\n *\n * These helpers are intentionally separate from the signing path. The\n * v0 canonical signature form (event_type|action|resource|input_hash|\n * output_hash|result) does NOT cover model_version, tool_catalogue_hash,\n * or system_prompt_hash. They will enter the canonical bytes in v1\n * (RFC 8785 JCS) in a follow-up release.\n *\n * Stable across SDK versions: the canonical-form version is documented\n * in CANONICAL_TOOL_CATALOGUE_VERSION. Future changes get a v2 etc;\n * never edit the existing v1 logic, or already-recorded hashes lose\n * their meaning.\n */\n\n// ─── Versioning ───────────────────────────────────────────────────────────\n\n/**\n * Canonical-form version for tool catalogue hashes produced by this SDK.\n * Recorded in tool_catalogue_canon_version on the event metadata (when\n * the metadata field is used) so verifiers know which hash recipe to\n * reproduce. Increment ONLY if the algorithm changes in a way that\n * would change the hash for the same logical catalogue.\n */\nexport const CANONICAL_TOOL_CATALOGUE_VERSION = 'v1' as const;\n\n// ─── Types ────────────────────────────────────────────────────────────────\n\n/**\n * Minimum shape a tool needs for catalogue hashing. The framework\n * integrations adapt their native tool objects to this shape before\n * calling hashToolCatalogue.\n */\nexport interface CatalogueTool {\n /** The name the model sees and calls. Required. */\n name: string;\n /** Human-readable description shown to the model. Optional; absent ↔ empty string. */\n description?: string;\n /**\n * JSON Schema describing the tool's input parameters. Optional;\n * absent ↔ empty object `{}`. The schema gets RFC 8785 JCS-canonicalised\n * before hashing so semantically-equivalent variations (key order,\n * whitespace) produce the same hash.\n */\n schema?: Record<string, unknown> | null;\n}\n\n// ─── RFC 8785 JCS subset ──────────────────────────────────────────────────\n\n/**\n * RFC 8785 JSON Canonicalization Scheme, sufficient subset for tool\n * catalogue values.\n *\n * Rules:\n * - Object keys sorted lexicographically by UTF-16 code units (which\n * is what JS string comparison does naturally).\n * - No whitespace between tokens.\n * - Numbers: integers as integers, finite floats per ECMAScript\n * Number.prototype.toString. JCS forbids NaN and Infinity.\n * - Strings: JSON-escape using minimal set per RFC 8259 § 7.\n * - null, true, false, arrays: as JSON.stringify produces them, since\n * JSON.stringify already produces the canonical form for these.\n *\n * Not vendoring a full library because tool schemas don't carry\n * non-integer numbers in practice and the JS spec for Number.toString\n * happens to coincide with JCS § 3.2.2.2 for the integer case.\n */\nexport function jcsCanonicalise(value: unknown): string {\n if (value === null) return 'null';\n if (value === true) return 'true';\n if (value === false) return 'false';\n\n if (typeof value === 'number') {\n if (!Number.isFinite(value)) {\n throw new Error('jcsCanonicalise: NaN and Infinity are not JCS-valid');\n }\n // Fail closed on magnitudes where JS .toString() and CPython diverge, so TS\n // and Python sign byte-identical bytes (or both reject). JS switches\n // integer-valued floats to exponent form at |x| >= 1e21 (Python expands to\n // full digits); in [1e-7, 1e-4) JS keeps decimals while Python repr() uses\n // exponent form. Keeping both engines in lockstep preserves cross-SDK\n // signature verifiability.\n const abs = Math.abs(value);\n // Reject ranges where JS Number.toString() and CPython diverge: |x| >= 1e21,\n // |x| < 1e-4, and integer-valued |x| > 2^53 (JS shortest-round-trip vs\n // Python exact int expansion — they only coincide for \"round\" values there).\n if (value !== 0 && (abs >= 1e21 || abs < 1e-4 || (Number.isInteger(value) && abs > 2 ** 53))) {\n throw new Error(\n `jcsCanonicalise: numbers in the ranges where JS and Python formatting ` +\n `diverge (|x| >= 1e21, |x| < 1e-4, or integer |x| > 2^53) are not ` +\n `supported in signed metadata. Use a string instead of ${value}.`,\n );\n }\n // For integers in safe range and ordinary decimals, .toString() matches JCS\n // (modern JS engines use the shortest round-trip representation).\n return value.toString();\n }\n\n if (typeof value === 'string') {\n return JSON.stringify(value);\n }\n\n if (Array.isArray(value)) {\n return '[' + value.map(jcsCanonicalise).join(',') + ']';\n }\n\n if (typeof value === 'object') {\n const obj = value as Record<string, unknown>;\n // Filter undefined-valued fields, matching @vorim/verify and\n // @vorim/shared-types. Without this the SDK throws on { a: 1, b: undefined }\n // while the verifier silently drops b — a cross-module canonical-form\n // divergence that would break signature verification on such events.\n const keys = Object.keys(obj).filter(k => obj[k] !== undefined).sort();\n const parts = keys.map(k => JSON.stringify(k) + ':' + jcsCanonicalise(obj[k]));\n return '{' + parts.join(',') + '}';\n }\n\n // undefined, function, symbol, bigint — not JSON-representable\n throw new Error(`jcsCanonicalise: unsupported value type: ${typeof value}`);\n}\n\n// ─── SHA-256 ──────────────────────────────────────────────────────────────\n\nasync function sha256Hex(input: string | Uint8Array): Promise<string> {\n const bytes = typeof input === 'string' ? new TextEncoder().encode(input) : input;\n\n // Node.js Web Crypto (Node 18+) supports digest. Browser Web Crypto does too.\n // Fall back to node:crypto if Web Crypto is unavailable.\n const subtle = (globalThis as any).crypto?.subtle;\n if (subtle) {\n const buf = await subtle.digest('SHA-256', bytes);\n return Array.from(new Uint8Array(buf))\n .map(b => b.toString(16).padStart(2, '0'))\n .join('');\n }\n\n // Node fallback\n const nodeCrypto = await import('node:crypto');\n return nodeCrypto.createHash('sha256').update(bytes).digest('hex');\n}\n\n// ─── Public API ───────────────────────────────────────────────────────────\n\n/**\n * Hash a single tool definition. Returns `sha256:<hex>`.\n *\n * Canonical form (v1):\n * JCS-canonicalised JSON of `{name, description, schema}` where\n * absent fields substitute `description: \"\"` and `schema: {}`.\n */\nexport async function hashTool(tool: CatalogueTool): Promise<string> {\n const normalised = {\n name: tool.name,\n description: tool.description ?? '',\n schema: tool.schema ?? {},\n };\n const hex = await sha256Hex(jcsCanonicalise(normalised));\n return `sha256:${hex}`;\n}\n\n/**\n * Hash an entire tool catalogue. Returns `sha256:<hex>`.\n *\n * Reordering tools does NOT change the hash (tool hashes sorted\n * lexicographically before concatenation). Adding, removing, or\n * modifying a tool DOES change the hash.\n *\n * Per-tool hashing first means a verifier comparing two catalogue\n * hashes that differ can also be given the per-tool hashes to\n * identify which specific tool changed.\n */\nexport async function hashToolCatalogue(tools: CatalogueTool[]): Promise<string> {\n if (tools.length === 0) {\n // Empty catalogue has a deterministic, stable hash distinct from \"no field\"\n return `sha256:${await sha256Hex('[]')}`;\n }\n const perTool = await Promise.all(tools.map(hashTool));\n perTool.sort();\n const hex = await sha256Hex(perTool.join(''));\n return `sha256:${hex}`;\n}\n\n/**\n * Hash a system prompt. Returns `sha256:<hex>`.\n *\n * The prompt is UTF-8 encoded and hashed verbatim — no normalisation.\n * If a caller wants to ignore whitespace or comment differences, they\n * should normalise before calling. The intent here is deterministic\n * reproducibility, not semantic equivalence.\n */\nexport async function hashSystemPrompt(prompt: string): Promise<string> {\n const hex = await sha256Hex(prompt);\n return `sha256:${hex}`;\n}\n\n/**\n * Convenience: hash the previous event's canonical bytes for use in\n * the prev_event_hash field of hash-chained ingest. Caller provides\n * the canonical bytes (use canonicalPayloadV0 from the main module).\n */\nexport async function hashPreviousEvent(canonicalBytes: string): Promise<string> {\n const hex = await sha256Hex(canonicalBytes);\n return `sha256:${hex}`;\n}\n\n// ─── Replay context — framework integration helper ────────────────────────\n\n/**\n * Raw inputs the integration captures from the framework. Set by the\n * integration's config; turned into hashes by {@link prepareReplayContext}.\n */\nexport interface ReplayInputs {\n /** Stable identifier for the model. E.g. `\"anthropic:claude-opus-4-8\"`. */\n modelVersion?: string;\n /** Tools available to the agent at call time. Hashed via {@link hashToolCatalogue}. */\n tools?: CatalogueTool[];\n /** System prompt active at call time. Hashed via {@link hashSystemPrompt}. */\n systemPrompt?: string;\n}\n\n/**\n * Pre-computed hashes ready to attach to audit events. The three keys\n * match the audit_events column names.\n */\nexport interface ReplayContext {\n model_version?: string;\n tool_catalogue_hash?: string;\n system_prompt_hash?: string;\n}\n\n/**\n * Compute replay context once from raw inputs. Use at integration\n * setup time so each emit can attach the hashes without re-hashing.\n *\n * Returns an object suitable for spreading into an AuditEventInput:\n * `await vorim.emit({ ...event, ...replayContext })`\n *\n * If a field is absent in the inputs, it is absent in the result\n * (not the empty string). That keeps the event lean.\n */\nexport async function prepareReplayContext(\n inputs: ReplayInputs,\n): Promise<ReplayContext> {\n const ctx: ReplayContext = {};\n if (inputs.modelVersion) ctx.model_version = inputs.modelVersion;\n if (inputs.tools) ctx.tool_catalogue_hash = await hashToolCatalogue(inputs.tools);\n if (inputs.systemPrompt) ctx.system_prompt_hash = await hashSystemPrompt(inputs.systemPrompt);\n return ctx;\n}\n"],"mappings":";;;;;;;;;;;;;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AAa,MAAM,YAAY,CAAC;AAAA,MACvD,QAAQ,YAAY,QAAQ,GAAK;AAAA,IACnC,CAAC;AACD,UAAM,OAAQ,MAAM,QAAQ,KAAK,EAAE,MAAM,OAAO,CAAC,EAAE;AAEnD,QAAI,QAAQ,MAAM,KAAK,MAAM,SAAS;AACpC,YAAM,SAA4B;AAAA,QAChC,SAAS,KAAK,KAAK;AAAA,QAAS,YAAY,KAAK,KAAK;AAAA,QAAY,QAAQ,KAAK,KAAK;AAAA,QAAQ,QAAQ,KAAK,KAAK;AAAA,MAC5G;AAKA,UAAI,KAAK,eAAe;AACtB,cAAM,MAAM,OAAO,KAAK,kBAAkB,WAAW,KAAK,gBAAgB,CAAC;AAC3E,cAAM,MAAM,IAAI,SAAS,EAAE,QAAQ,OAAO,SAAS,SAAS,KAAK,QAAQ,CAAC;AAC1E,eAAO,QAAQ,MAAM,IAAI,SAAS;AAAA,UAChC,MAAM,IAAI,QAAQ,KAAK,cAAc;AAAA,UACrC,cAAc,IAAI,gBAAgB,CAAC;AAAA,UACnC,QAAS,IAAI,UAAkB,CAAC,cAAc,eAAe;AAAA,QAC/D,CAAC;AAAA,MACH;AACA,aAAO;AAAA,IACT;AACA,UAAM,OAAO,KAAK,OAAO;AACzB,QAAI,SAAS,gBAAiB,OAAM,IAAI,WAAW,KAAK,iBAAiB,4CAA4C;AACrH,QAAI,SAAS,gBAAiB,OAAM,IAAI,WAAW,KAAK,kBAAkB,yCAAyC;AACnH,QAAI,SAAS,aAAa;AAAE,cAAQ;AAAY;AAAA,IAAU;AAE1D,WAAO;AAAA,EACT;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/replay.ts"],"sourcesContent":["// ============================================================================\n// VORIM SDK — TypeScript\n// Thin client wrapping the Vorim AI REST API\n// ============================================================================\n\nimport type {\n Agent, AgentRegistrationInput, AgentRegistrationResult,\n TrustRecord, AuditEventInput, PermissionScope, PermissionCheckResult,\n AgentDelegationRecord, DelegationLinkClaims,\n BeforeActionInput, RuntimeDecision, DecisionVerdict,\n TokenBudget, TokenBudgetInput, BudgetSpend,\n WorkflowProvenanceGraph,\n SharedMemoryEntry, SharedMemoryWriteInput, SharedMemoryListResult,\n} from './types.js';\nimport { jcsCanonicalise, hashPreviousEvent } from './replay.js';\n\n// __SDK_VERSION__ is replaced at build time (tsup define) with the package.json\n// version, so the User-Agent always matches the published version. Falls back to\n// '0.0.0' when run un-bundled (e.g. ts-node / tests) where the define isn't applied.\ndeclare const __SDK_VERSION__: string | undefined;\nconst SDK_VERSION = typeof __SDK_VERSION__ !== 'undefined' ? __SDK_VERSION__ : '0.0.0';\nconst USER_AGENT = `vorim-sdk/${SDK_VERSION}`;\n\n/** Promise-based delay. Used by the runtime escalation poller. */\nfunction sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n}\n\nexport interface VorimConfig {\n apiKey: string;\n baseUrl?: string;\n timeout?: number;\n /**\n * Auto-sign audit events with the agent's private key at emit time.\n * Default true in v3.1+. Set to false to opt out globally.\n * When enabled, the SDK signs the event using whichever private key was\n * stored via {@link VorimSDK.useAgentKey} or returned by\n * {@link VorimSDK.register}. Events for unknown agents pass through unsigned.\n */\n autoSign?: boolean;\n /**\n * Canonical form to use when signing. Default `\"v0\"` for backward-compat\n * with `@vorim/sdk` 3.1.x. Set to `\"v1\"` to sign the full event object\n * via RFC 8785 JCS, covering replayable-evidence fields (model_version,\n * tool_catalogue_hash, system_prompt_hash, prev_event_hash). v1 events\n * carry `canonical_form: \"v1\"` on the wire so the server can dispatch\n * verification correctly.\n */\n canonicalForm?: 'v0' | 'v1';\n /**\n * Hash-chain events per agent so deletion of a single audit row\n * becomes detectable. Default `false` (no chaining). When enabled,\n * the SDK sets `prev_event_hash` on each event to SHA-256 of the\n * previous event's canonical bytes for the same agent. The first\n * event after enable / process restart has `prev_event_hash = null`,\n * which the verifier reports as `chain_restart` (informational, not\n * a failure). Chain integrity is checked by `@vorim/verify`.\n */\n chainEvents?: boolean;\n /**\n * Client-side fail-open behaviour for {@link VorimSDK.beforeAction} when\n * the runtime decision API itself is unreachable (network error, DNS,\n * timeout, or 5xx). Default `true`: a transport failure returns a\n * synthetic `fallback` decision (`decision: 'fallback'`, `isFallback:\n * true`) so a momentary control-plane outage does not block the agent.\n * Set `false` to fail closed — `beforeAction` re-throws the transport\n * error and the caller must decide.\n *\n * Note: this governs only transport failures. A reachable server that\n * returns `deny` still denies regardless of this flag.\n */\n runtimeFailOpen?: boolean;\n}\n\n/**\n * VAIP v0 canonical bytes used by Vorim's per-event signing.\n *\n * Pipe-joined content fields with empty-string substitution for missing values.\n * This is intentionally duplicated from `@vorim/shared-types` so the published\n * SDK ships with zero runtime dependencies. A parity test in this package\n * imports both definitions and asserts byte-exact equality across a fixture\n * matrix, so they cannot drift silently.\n *\n * To upgrade the format, version the function (`canonicalPayloadV1`) — never\n * edit this one. Old signatures must remain verifiable.\n */\nexport function canonicalPayloadV0(event: AuditEventInput): string {\n return [\n event.event_type,\n event.action,\n event.resource ?? '',\n event.input_hash ?? '',\n event.output_hash ?? '',\n event.result,\n ].join('|');\n}\n\n/**\n * VAIP v1 canonical bytes for audit-event signing (RFC 8785 JCS).\n *\n * Signs the full event object excluding `signature` (the field being\n * computed) and `canonical_form` (metadata about the recipe). Unlike v0,\n * v1 covers the replayable-evidence fields and the metadata field.\n *\n * Re-exports `jcsCanonicalise` from `./replay.js` which is the byte-exact\n * twin of `@vorim/shared-types`' implementation. The cross-language parity\n * script enforces equivalence with the Python SDK.\n */\nexport function canonicalPayloadV1(event: AuditEventInput): string {\n // Strip the fields that are out of scope for v1 signing.\n const { signature: _sig, canonical_form: _cf, ...rest } = event as Record<string, unknown> & AuditEventInput;\n return jcsCanonicalise(rest);\n}\n\nexport class VorimSDK {\n private apiKey: string;\n private baseUrl: string;\n private timeout: number;\n private autoSign: boolean;\n private canonicalForm: 'v0' | 'v1';\n private chainEvents: boolean;\n private runtimeFailOpen: boolean;\n /**\n * In-memory keyring mapping agent_id -> PEM-encoded Ed25519 private key.\n * Populated automatically by register() and registerEphemeral(), or\n * explicitly via useAgentKey(). Lost on process restart by design; the\n * caller is responsible for durable key storage.\n */\n private agentKeys: Map<string, string> = new Map();\n /**\n * Per-agent hash of the last emitted event's canonical bytes. Used to\n * populate prev_event_hash on the next emit when chainEvents is on.\n * Empty string ↔ no previous event (chain restart). Reset by\n * forgetAgentKey() so reusing an agent_id after revocation doesn't\n * link to the old chain.\n */\n private lastEventHash: Map<string, string> = new Map();\n /**\n * Per-agent monotonic counter incremented by `forgetAgentKey`. An\n * emit that started before `forgetAgentKey` ran will read a stale\n * epoch and refuse to repopulate the chain tail, preventing the\n * revoked agent from inheriting a new lastEventHash from the\n * in-flight emit that the lock was holding.\n */\n private agentForgetEpoch: Map<string, number> = new Map();\n /**\n * Per-agent emit promise. Each new emit awaits the previous one so\n * the chain is constructed in order. Concurrent emits to the same\n * agent are serialised (correct); concurrent emits to different\n * agents remain parallel (fast).\n */\n private chainLocks: Map<string, Promise<void>> = new Map();\n\n constructor(config: VorimConfig) {\n this.apiKey = config.apiKey;\n this.baseUrl = (config.baseUrl || 'https://api.vorim.ai').replace(/\\/$/, '') + '/v1';\n this.timeout = config.timeout || 10000;\n this.autoSign = config.autoSign !== false;\n // Default to v1 as of 3.4.0. v0 only covers six fields and any\n // metadata, delegation context, or replayable-evidence field is\n // unsigned tail — see crypto-1 in the deep-audit findings. Callers\n // who explicitly want v0 (legacy verifier compatibility) must opt\n // in. A console.warn fires the first time an SDK instance is\n // created with v0 so the deprecation is visible in dev.\n this.canonicalForm = config.canonicalForm ?? 'v1';\n if (config.canonicalForm === 'v0') {\n try {\n // eslint-disable-next-line no-console\n console.warn(\n '[@vorim/sdk] canonicalForm=v0 is deprecated: it leaves metadata, ' +\n 'delegation context, and replayable-evidence fields unsigned. ' +\n 'Switch to canonicalForm=v1 (the new default in 3.4.0+) for full ' +\n 'tamper coverage. Pass canonicalForm: \"v0\" explicitly only if you ' +\n 'need byte-compat with verifiers older than @vorim/verify 0.2.0.',\n );\n } catch { /* ignore logger errors */ }\n }\n this.chainEvents = config.chainEvents ?? false;\n this.runtimeFailOpen = config.runtimeFailOpen ?? true;\n }\n\n /**\n * Register a previously-issued agent keypair so this SDK instance can\n * auto-sign events for it on emit. Use this when restoring an agent across\n * process restarts: read the agent's private_key from durable storage,\n * call useAgentKey(agentId, privateKey), and emit() will sign automatically.\n */\n useAgentKey(agentId: string, privateKeyPem: string): void {\n this.agentKeys.set(agentId, privateKeyPem);\n }\n\n /**\n * Forget an agent's signing key (e.g. after revocation). Subsequent emit()\n * calls for that agent will pass through unsigned unless a key is provided\n * inline. Also clears the per-agent chain state — re-using the same\n * agent_id after revocation does NOT link to the old chain.\n */\n forgetAgentKey(agentId: string): void {\n this.agentKeys.delete(agentId);\n this.lastEventHash.delete(agentId);\n this.chainLocks.delete(agentId);\n // Bump the per-agent epoch so any in-flight emit that completes\n // after this call refuses to write back a chain tail. Without\n // this, an emit that holds the chain lock can repopulate\n // lastEventHash after forgetAgentKey clears it, defeating the\n // revoked-agent isolation guarantee.\n this.agentForgetEpoch.set(agentId, (this.agentForgetEpoch.get(agentId) ?? 0) + 1);\n }\n\n /**\n * Cap on how many per-agent chain entries the SDK tracks. Set\n * generously — most callers will hold ≤ 1k agent_ids in memory.\n * Long-running processes that cycle through many agent_ids without\n * calling forgetAgentKey (e.g. ephemeral test agents) would\n * otherwise grow chainLocks / agentForgetEpoch / lastEventHash\n * unbounded.\n */\n private static readonly MAX_CHAIN_STATE_ENTRIES = 10_000;\n\n /**\n * Evict the oldest chain state entries when we cross the cap. Called\n * lazily from prepareEventChained — no scheduled task, no background\n * timer; only runs when the cap is breached during a real emit.\n */\n private evictChainStateIfNeeded(): void {\n if (this.chainLocks.size <= VorimSDK.MAX_CHAIN_STATE_ENTRIES) return;\n const toRemove = Math.ceil(this.chainLocks.size * 0.1); // drop oldest 10 %\n let removed = 0;\n for (const agentId of this.chainLocks.keys()) {\n if (removed >= toRemove) break;\n // Skip agents with an active key — they're in use.\n if (this.agentKeys.has(agentId)) continue;\n this.chainLocks.delete(agentId);\n this.lastEventHash.delete(agentId);\n this.agentForgetEpoch.delete(agentId);\n removed++;\n }\n }\n\n // ─── Health Check ────────────────────────────────────────────────\n\n /**\n * Ping the Vorim API to verify connectivity and API key validity.\n * Returns { status, timestamp } on success, throws VorimError on failure.\n */\n async ping(): Promise<{ status: string; timestamp: string }> {\n const response = await fetch(`${this.baseUrl.replace('/v1', '')}/health`, {\n headers: { 'User-Agent': USER_AGENT },\n signal: AbortSignal.timeout(this.timeout),\n });\n if (!response.ok) throw new VorimError(response.status, 'UNREACHABLE', 'Vorim API is not reachable');\n return response.json() as Promise<{ status: string; timestamp: string }>;\n }\n\n // ─── Agent Identity ────────────────────────────────────────────────\n\n /**\n * Register a new agent with Vorim AI.\n * Returns the agent identity and a private key (shown once).\n *\n * Side effect: the returned private_key is cached in this SDK instance's\n * in-memory keyring so subsequent emit() calls for this agent auto-sign.\n * The keyring is process-local; the caller is responsible for persisting\n * the private_key to durable storage if the agent should survive restarts.\n */\n async register(input: AgentRegistrationInput): Promise<AgentRegistrationResult> {\n const result = await this.post('/agents', input) as AgentRegistrationResult;\n if (result?.agent?.agent_id && result?.private_key) {\n this.agentKeys.set(result.agent.agent_id, result.private_key);\n }\n return result;\n }\n\n /**\n * Verify an agent's identity via the public Trust API.\n */\n async verify(agentId: string): Promise<TrustRecord> {\n return this.get(`/trust/verify/${agentId}`);\n }\n\n /**\n * Get agent details.\n */\n async getAgent(agentId: string): Promise<Agent> {\n return this.get(`/agents/${agentId}`);\n }\n\n /**\n * List all agents in the organisation.\n */\n async listAgents(params?: { page?: number; per_page?: number; status?: string }): Promise<{ agents: Agent[]; meta: any }> {\n const qs = new URLSearchParams(params as any).toString();\n // The API returns { data: Agent[], meta }. The generic get() unwraps to\n // `data` only, dropping `meta`, so read the full envelope here to honour\n // the documented { agents, meta } return shape (pagination relies on meta).\n const env = await this.requestEnvelope('GET', `/agents${qs ? '?' + qs : ''}`);\n return { agents: (env.data ?? []) as Agent[], meta: env.meta ?? null };\n }\n\n /**\n * Update an agent's metadata.\n */\n async updateAgent(agentId: string, updates: Partial<Pick<Agent, 'name' | 'description' | 'status' | 'capabilities'>>): Promise<Agent> {\n return this.patch(`/agents/${agentId}`, updates);\n }\n\n /**\n * Revoke an agent (permanent deactivation).\n */\n async revoke(agentId: string): Promise<void> {\n await this.delete(`/agents/${agentId}`);\n }\n\n // ─── Permissions ──────────────────────────────────────────────────\n\n /**\n * Check if an agent has a specific permission scope.\n * Target: < 5ms response via Redis cache.\n */\n async check(agentId: string, scope: PermissionScope): Promise<PermissionCheckResult> {\n return this.post(`/agents/${agentId}/permissions/verify`, { scope });\n }\n\n /**\n * Grant a permission scope to an agent.\n */\n async grant(agentId: string, scope: PermissionScope, options?: {\n valid_until?: string;\n rate_limit?: { max: number; window: string };\n }): Promise<any> {\n return this.post(`/agents/${agentId}/permissions`, { scope, ...options });\n }\n\n /**\n * List all active permissions for an agent.\n */\n async listPermissions(agentId: string): Promise<any[]> {\n return this.get(`/agents/${agentId}/permissions`);\n }\n\n /**\n * Revoke a specific permission scope from an agent.\n */\n async revokePermission(agentId: string, scope: PermissionScope): Promise<any> {\n return this.delete(`/agents/${agentId}/permissions/${scope}`);\n }\n\n // ─── Agent-to-Agent Identity Delegation (VAIP -02 § 5) ──────────────\n\n /**\n * Delegate the right to act on this agent's behalf to another agent\n * in the same org, with a strict subset of this agent's scopes.\n *\n * Multi-hop chains are supported up to depth 32. Scope subset is\n * enforced at every hop. Audit events emitted by the delegate carry\n * the chain context (`on_behalf_of`, `delegator_agent_id`,\n * `delegation_chain_id`, `delegation_depth`) so a verifier walking\n * the bundle can reconstruct the full chain.\n *\n * @example\n * ```ts\n * const chain = await vorim.delegateToAgent('agid_parent_abc', {\n * delegate_agent_id: 'agid_child_xyz',\n * scopes_delegated: ['agent:read', 'agent:execute'],\n * max_chain_depth: 1, // delegate may make ONE further hop\n * valid_until: '2026-12-31T00:00:00Z',\n * });\n * console.log(chain.public_chain_id); // chain_<hex>\n * ```\n */\n async delegateToAgent(\n delegatorAgentId: string,\n input: {\n delegate_agent_id: string;\n scopes_delegated: PermissionScope[];\n max_chain_depth?: number;\n valid_until?: string;\n /**\n * Optional Ed25519-signed delegation link (VAIP -02 § 5). When\n * provided, the server verifies the signature against the\n * delegator's stored public key, persists alongside the chain\n * row, and exports it in bundle delegation_tokens so the chain\n * is offline-verifiable by `@vorim/verify`. Compute via\n * {@link signDelegationLink} or set manually if signing\n * elsewhere.\n */\n signed_link?: {\n claims: DelegationLinkClaims;\n signature: string;\n };\n },\n ): Promise<AgentDelegationRecord> {\n return this.post(`/agents/${delegatorAgentId}/delegations`, input);\n }\n\n /**\n * Sign a delegation link with the delegator's private key. Returns\n * the signed link in the shape accepted by `delegateToAgent`'s\n * `signed_link` field.\n *\n * The signature is Ed25519 over the RFC 8785 JCS-canonical bytes of\n * the claims object. Same algorithm the server and `@vorim/verify`\n * use for verification.\n *\n * Use this when the delegator's private key is in this SDK's\n * keyring (i.e. set via `register()` or `useAgentKey()`).\n *\n * @example\n * ```ts\n * const signed = await vorim.signDelegationLink({\n * v: 0,\n * type: 'vaip-delegation-link',\n * chain_id: 'chain_abc', // server returns this on first delegation\n * depth: 1,\n * delegator: 'agid_parent',\n * delegate: 'agid_child',\n * scopes: ['agent:read', 'agent:execute'],\n * max_chain_depth: 1,\n * valid_from: new Date().toISOString(),\n * valid_until: null,\n * parent_link_hash: null,\n * });\n * ```\n */\n async signDelegationLink(\n claims: DelegationLinkClaims,\n ): Promise<{ claims: DelegationLinkClaims; signature: string }> {\n const key = this.agentKeys.get(claims.delegator);\n if (!key) {\n throw new VorimError(\n 400,\n 'NO_AGENT_KEY',\n `SDK has no private key for delegator ${claims.delegator}`,\n );\n }\n // Sign using the SDK's local JCS. Byte-equivalent to shared-types\n // and the server (enforced by scripts/check-replay-parity.sh).\n const bytes = jcsCanonicalise(claims as unknown as Record<string, unknown>);\n const signature = await this.sign(bytes, key);\n return { claims, signature };\n }\n\n /**\n * List active identity-chain entries this agent participates in\n * (either as delegator or delegate). Useful for showing the current\n * delegation graph in an admin UI.\n */\n async listAgentDelegations(agentId: string): Promise<AgentDelegationRecord[]> {\n return this.get(`/agents/${agentId}/delegations`);\n }\n\n /**\n * Walk a delegation chain by its public id. Returns the full chain\n * in depth order.\n */\n async getDelegationChain(\n agentId: string,\n chainId: string,\n ): Promise<AgentDelegationRecord[]> {\n return this.get(`/agents/${agentId}/delegation-chain/${chainId}`);\n }\n\n /**\n * Revoke this agent's delegation in a given chain. Cascades to all\n * descendants beneath this agent's depth in the chain.\n */\n async revokeAgentDelegation(\n agentId: string,\n chainId: string,\n ): Promise<{ revoked: number }> {\n return this.delete(`/agents/${agentId}/delegations/${chainId}`);\n }\n\n /**\n * Reconstruct a multi-agent workflow's provenance graph from a delegation\n * chain id — the nodes (agents), edges (who delegated what to whom), and the\n * audit events attributed to each agent within that workflow. Multi-agent\n * fabric Phase 1: the tamper-evident answer to \"which agents did what, and\n * who authorised whom?\" across a whole hand-off chain.\n *\n * @example\n * ```ts\n * const graph = await vorim.getWorkflowGraph('chain_abc123');\n * for (const edge of graph.edges) {\n * console.log(`${edge.from} -> ${edge.to} [${edge.scopes.join(', ')}]`);\n * }\n * ```\n */\n async getWorkflowGraph(\n chainId: string,\n options?: { maxEvents?: number },\n ): Promise<WorkflowProvenanceGraph> {\n const qs = options?.maxEvents ? `?max_events=${options.maxEvents}` : '';\n return this.get(`/audit/workflow-graph/${chainId}${qs}`);\n }\n\n // ─── Governed Shared Memory (multi-agent fabric, Phase 2) ──────────\n\n /**\n * Write (upsert) an entry into the org's governed shared-memory pool,\n * attributed to `agentId`.\n *\n * If the SDK holds the agent's private key (via {@link VorimSDK.register}\n * or {@link VorimSDK.useAgentKey}), the entry is signed at source with the\n * agent's Ed25519 key over the canonical entry (namespace + key + value +\n * agent_id) before it leaves the process, and the server verifies it against\n * the agent's stored public key — rejecting a bad signature (fail closed).\n * This is what makes a poisoned entry always traceable to its source\n * (OWASP ASI06). If no key is available, the entry is written unsigned.\n *\n * A write to an existing (namespace, key) updates it, recording the new\n * writer + signature.\n *\n * Requires an API key with the `memory:write` scope.\n *\n * @example\n * ```ts\n * await vorim.writeMemory('agid_researcher_abc', {\n * namespace: 'research-run-42',\n * key: 'sources',\n * value: { urls: ['https://…'], gathered_at: '2026-07-25T…' },\n * });\n * ```\n */\n async writeMemory(\n agentId: string,\n entry: SharedMemoryWriteInput,\n ): Promise<SharedMemoryEntry> {\n const body: Record<string, unknown> = {\n agent_id: agentId,\n namespace: entry.namespace,\n key: entry.key,\n value: entry.value,\n };\n // Sign at source when we have the agent's key. Byte-identical canonical\n // form to the server's sharedMemoryCanonicalBytes (@vorim/shared-types)\n // and the Python SDK: JCS over { agent_id, key, namespace, value }.\n const key = this.agentKeys.get(agentId);\n if (key) {\n try {\n const canonical = jcsCanonicalise({\n agent_id: agentId,\n key: entry.key,\n namespace: entry.namespace,\n value: entry.value,\n });\n body.signature = await this.sign(canonical, key);\n } catch {\n // Signing failure is non-fatal: the entry still writes unsigned\n // rather than blocking the caller on a single bad key.\n }\n }\n return this.post('/memory', body);\n }\n\n /**\n * Read one shared-memory entry by (namespace, key). Throws a 404\n * {@link VorimError} if it does not exist. Returns the value plus\n * provenance (which agent wrote it, content hash, whether it is signed).\n *\n * Requires an API key with the `memory:read` scope.\n */\n async readMemory(input: { namespace: string; key: string }): Promise<SharedMemoryEntry> {\n return this.get(`/memory/${encodeURIComponent(input.namespace)}/${encodeURIComponent(input.key)}`);\n }\n\n /**\n * List entries in a namespace (paginated). Returns `{ entries, meta }`.\n *\n * Requires an API key with the `memory:read` scope.\n */\n async listMemory(input: {\n namespace: string;\n page?: number;\n perPage?: number;\n }): Promise<SharedMemoryListResult> {\n const params = new URLSearchParams();\n if (input.page) params.set('page', String(input.page));\n if (input.perPage) params.set('per_page', String(input.perPage));\n const qs = params.toString();\n const envelope = await this.requestEnvelope(\n 'GET',\n `/memory/${encodeURIComponent(input.namespace)}${qs ? `?${qs}` : ''}`,\n );\n return {\n entries: (envelope.data ?? []) as SharedMemoryEntry[],\n meta: envelope.meta,\n };\n }\n\n /**\n * Delete one shared-memory entry by (namespace, key).\n *\n * Requires an API key with the `memory:write` scope.\n */\n async deleteMemory(input: { namespace: string; key: string }): Promise<{ deleted: true; namespace: string; key: string }> {\n return this.delete(`/memory/${encodeURIComponent(input.namespace)}/${encodeURIComponent(input.key)}`);\n }\n\n /**\n * Verified hand-off — the multi-agent-fabric primitive.\n *\n * Hands a task from one agent to another as ONE auditable operation that\n * composes the three trust primitives:\n * 1. (optional) VERIFY the receiving agent is trusted + active before\n * handing it anything — fails closed if it is not (unless you opt out).\n * 2. DELEGATE a strictly-attenuated subset of the sender's scopes to the\n * receiver (the server enforces child ⊆ parent + depth; a scope the\n * sender doesn't hold is rejected). Optionally as a signed capability\n * link so the hand-off is offline-verifiable by `@vorim/verify`.\n * 3. EMIT a signed audit event recording the hand-off, stamped with the\n * delegation-chain provenance so the whole multi-agent workflow is\n * reconstructable later.\n *\n * Returns the delegation record (with `public_chain_id`) plus the verification\n * result. Throws before delegating if verification fails.\n *\n * @example\n * ```ts\n * const { chain } = await vorim.handOff('agid_planner', {\n * to: 'agid_researcher',\n * scopes: ['agent:read'], // researcher gets read-only, nothing more\n * task: 'gather sources for Q3 report',\n * maxChainDepth: 0, // researcher cannot re-delegate\n * minTrustScore: 60,\n * });\n * console.log(chain.publicChainId);\n * ```\n */\n async handOff(\n fromAgentId: string,\n input: {\n /** The receiving agent's id. */\n to: string;\n /** Scopes to delegate — must be a subset of what `fromAgentId` holds. */\n scopes: PermissionScope[];\n /** Human-readable description of the handed-off task (recorded in audit). */\n task?: string;\n /** How many further hops the receiver may make (default 0 — no re-delegation). */\n maxChainDepth?: number;\n /** Delegation expiry (ISO 8601). */\n validUntil?: string;\n /** Minimum trust score the receiver must have to accept the hand-off (default 50). */\n minTrustScore?: number;\n /** Skip the pre-delegation trust verification (NOT recommended). @default false */\n skipVerify?: boolean;\n /** Sign the delegation link so the hand-off is offline-verifiable. @default false */\n signed?: boolean;\n },\n ): Promise<{ chain: AgentDelegationRecord; verification?: TrustRecord }> {\n let verification: TrustRecord | undefined;\n\n // 1. Verify the receiver is trusted + active before delegating to it.\n if (!input.skipVerify) {\n try {\n verification = await this.verify(input.to);\n } catch {\n throw new VorimError(400, 'HANDOFF_VERIFY_FAILED',\n `Could not verify receiving agent ${input.to} — refusing hand-off (fail closed).`);\n }\n const minScore = input.minTrustScore ?? 50;\n if (verification.status !== 'active') {\n throw new VorimError(403, 'HANDOFF_RECEIVER_INACTIVE',\n `Receiving agent ${input.to} is ${verification.status}, not active — refusing hand-off.`);\n }\n if ((verification.trust_score ?? 0) < minScore) {\n throw new VorimError(403, 'HANDOFF_RECEIVER_UNTRUSTED',\n `Receiving agent ${input.to} trust score ${verification.trust_score} is below minimum ${minScore}.`);\n }\n }\n\n // 2. Delegate the attenuated scope subset (server enforces child ⊆ parent).\n const delegationInput: Parameters<VorimSDK['delegateToAgent']>[1] = {\n delegate_agent_id: input.to,\n scopes_delegated: input.scopes,\n max_chain_depth: input.maxChainDepth ?? 0,\n ...(input.validUntil ? { valid_until: input.validUntil } : {}),\n };\n if (input.signed) {\n // Sign the link so the hand-off is offline-verifiable end to end.\n const chainId = `chain_pending_${input.to}`; // server assigns the real id on root\n const signed = await this.signDelegationLink({\n v: 0,\n type: 'vaip-delegation-link',\n chain_id: chainId,\n depth: 1,\n delegator: fromAgentId,\n delegate: input.to,\n scopes: input.scopes,\n max_chain_depth: input.maxChainDepth ?? 0,\n valid_from: new Date().toISOString(),\n valid_until: input.validUntil ?? null,\n parent_link_hash: null,\n });\n delegationInput.signed_link = signed;\n }\n const chain = await this.delegateToAgent(fromAgentId, delegationInput);\n\n // 3. Emit a signed audit event recording the hand-off, carrying the\n // delegation-chain provenance so the whole workflow is reconstructable.\n try {\n await this.emit({\n agent_id: fromAgentId,\n event_type: 'permission_change',\n action: 'agent.handoff',\n resource: input.to,\n result: 'success',\n delegation_chain_id: chain.publicChainId,\n metadata: {\n framework: 'fabric',\n to: input.to,\n scopes: input.scopes,\n task: input.task,\n receiver_trust_score: verification?.trust_score,\n },\n }, { sign: true });\n } catch {\n // Audit failure never rolls back a completed delegation; it is logged\n // server-side. The hand-off itself succeeded.\n }\n\n return { chain, verification };\n }\n\n // ─── Runtime Control (Strategy A) ──────────────────────────────────\n\n /**\n * Gate an agent action BEFORE it is performed. Calls\n * `POST /v1/runtime/decisions` and returns a typed {@link RuntimeDecision}.\n *\n * By default (`throwOnDeny: true`) a `deny` verdict throws\n * {@link VorimDeniedError} — deny is carried in the response body, not\n * the HTTP status, so without this the caller would treat a denial as\n * success. Pass `{ throwOnDeny: false }` to handle deny yourself.\n *\n * On a `modify` verdict, use `decision.modifiedPayload` in place of your\n * original payload (e.g. a policy rule masked PII). This is\n * client-cooperative: Vorim returns the sanitised payload but does not sit\n * inline and does not enforce that you send it — carry `decisionId` into\n * the matching {@link emit} so the action stays auditable. On `escalate`,\n * poll {@link waitForDecisionResolution} for the human decision.\n *\n * Transport failures (network/DNS/timeout/5xx) respect the constructor's\n * `runtimeFailOpen` flag: when true (default) a synthetic `fallback`\n * decision is returned so a control-plane blip does not block the agent;\n * when false the underlying error is re-thrown.\n *\n * Always pass the public `agid_*` id as `agentId`.\n *\n * @example\n * const d = await vorim.beforeAction({\n * agentId: 'agid_acme_evilbot',\n * actionType: 'tool_call',\n * actionTarget: 'sendEmail',\n * requiredScope: 'agent:communicate',\n * payload: { to: 'customer@example.com' },\n * });\n * if (d.decision === 'allow') await sendEmail(d.modifiedPayload ?? payload);\n */\n async beforeAction(\n input: BeforeActionInput,\n options: { throwOnDeny?: boolean } = {},\n ): Promise<RuntimeDecision> {\n const throwOnDeny = options.throwOnDeny ?? true;\n const body = {\n agent_id: input.agentId,\n action_type: input.actionType,\n action_target: input.actionTarget,\n payload: input.payload,\n context: input.context,\n required_scope: input.requiredScope,\n idempotency_key: input.idempotencyKey,\n workflow_id: input.workflowId,\n estimated_tokens: input.estimatedTokens,\n };\n\n let raw: any;\n try {\n raw = await this.post('/runtime/decisions', body);\n } catch (err) {\n // Transport-level failure. A VorimError carrying a real HTTP status\n // < 500 (e.g. 400/403/404) is a genuine server verdict/validation\n // error and must propagate. Only network errors and 5xx are\n // candidates for client-side fail-open.\n const status = err instanceof VorimError ? err.status : 0;\n const code = err instanceof VorimError ? err.code : undefined;\n // A malformed (non-JSON) control-plane response is NOT eligible for\n // fail-open: we can't trust a policy plane we can't parse, so it must\n // fail closed (the error propagates and the gate denies). Everything\n // else with status 0 or >= 500 is a genuine transport failure.\n const isTransport = (status === 0 || status >= 500) && code !== 'MALFORMED_RESPONSE';\n if (isTransport && this.runtimeFailOpen) {\n return {\n decisionId: '',\n decision: 'fallback',\n reason: 'Runtime decision API unreachable; client fail-open applied',\n decisionRuleId: null,\n modifiedPayload: null,\n expiresAt: new Date(Date.now() + 60_000).toISOString(),\n latencyMs: 0,\n isFallback: true,\n policyVersion: 0,\n escalationResolution: null,\n };\n }\n throw err;\n }\n\n const decision = this.toRuntimeDecision(raw);\n if (decision.decision === 'deny' && throwOnDeny) {\n throw new VorimDeniedError(decision);\n }\n return decision;\n }\n\n // ─── Token Budgets (AI Cost Management) ──────────────────────────────\n //\n // A budget is a rolling token ceiling for one agent or one workflow.\n // Enforcement runs inside beforeAction(): pass `workflowId` + `estimatedTokens`\n // on the action, and a `token_budget_gt` policy rule denies the action before\n // the spend if it would breach the budget. These methods manage the budgets\n // and read live spend (dashboard-oriented; require a JWT-authed client or an\n // owner/admin API key).\n\n /**\n * Create or update a token budget for an agent or workflow. One budget per\n * (scope, scope_ref) — calling this again for the same target updates it.\n *\n * @example\n * await vorim.setBudget({\n * scope: 'workflow', scope_ref: 'nightly-summariser',\n * max_tokens: 2_000_000, window_seconds: 86400, enforcement: 'hard',\n * });\n */\n async setBudget(input: TokenBudgetInput): Promise<TokenBudget> {\n return this.put('/runtime/budgets', input);\n }\n\n /** List the org's configured token budgets. */\n async listBudgets(): Promise<TokenBudget[]> {\n return this.get('/runtime/budgets');\n }\n\n /** Live rolling spend, remaining allowance, and monthly total per budget. */\n async getBudgetSpend(): Promise<BudgetSpend[]> {\n return this.get('/runtime/budgets/spend');\n }\n\n /** Remove a token budget by id. */\n async deleteBudget(budgetId: string): Promise<void> {\n await this.delete(`/runtime/budgets/${encodeURIComponent(budgetId)}`);\n }\n\n /**\n * Poll a decision until it leaves the `escalate` state (a human\n * approved or denied it) or the timeout elapses. Returns the resolved\n * decision; throws {@link VorimError} `ESCALATION_TIMEOUT` (408) if the\n * decision is still pending when the timeout is reached.\n *\n * Requires an API key with the `runtime:decide` scope (the same scope\n * `beforeAction` uses).\n */\n async waitForDecisionResolution(\n decisionId: string,\n options: { timeoutMs?: number; pollIntervalMs?: number } = {},\n ): Promise<RuntimeDecision> {\n const timeoutMs = options.timeoutMs ?? 300_000;\n const pollIntervalMs = options.pollIntervalMs ?? 1000;\n const start = Date.now();\n // Always poll at least once even if timeoutMs is 0.\n do {\n const raw = await this.get(`/runtime/decisions/${decisionId}`);\n // The GET projection carries escalation_resolution; once set (or\n // once the verdict is no longer 'escalate') the escalation is done.\n if (raw?.decision !== 'escalate' || raw?.escalation_resolution) {\n return this.toRuntimeDecision(raw);\n }\n // Sleep only as long as the deadline allows, so the final throw\n // fires at ~timeoutMs rather than up to one pollIntervalMs late.\n const remaining = timeoutMs - (Date.now() - start);\n if (remaining <= 0) break;\n await sleep(Math.min(pollIntervalMs, remaining));\n } while (Date.now() - start < timeoutMs);\n throw new VorimError(\n 408,\n 'ESCALATION_TIMEOUT',\n `Decision ${decisionId} still pending after ${timeoutMs}ms`,\n { decisionId },\n );\n }\n\n /**\n * Map a snake_case decision row (from POST or GET) to the camelCase\n * {@link RuntimeDecision}. The SDK has no generic camelizer, so the\n * mapping is explicit — and tolerant of either the POST shape\n * (`decision_rule_id`, `latency_ms`) or absent fields on a GET row.\n *\n * Escalation-resolution translation (load-bearing): the server resolves\n * an escalation by setting `escalation_resolution` but does NOT flip the\n * row's `decision` column off `'escalate'`. If we returned that verbatim,\n * a caller's obvious `if (d.decision === 'deny')` check would never fire\n * on a human DENIAL — a silent fail-open. So when a resolution is present\n * we translate the verdict: approved → 'allow', denied → 'deny'. The raw\n * resolution is also surfaced as `escalationResolution` for callers that\n * want it explicitly.\n *\n * A missing verdict (malformed/truncated response) fails CLOSED to 'deny'\n * rather than 'fallback', so the deny check fires on garbled input.\n */\n private toRuntimeDecision(raw: any): RuntimeDecision {\n // Runtime-narrow to the enum: a surprise server value (e.g. 'pending')\n // collapses to null rather than leaking through the typed field, and\n // never drives a translation.\n const resolution: 'approved' | 'denied' | null =\n raw?.escalation_resolution === 'approved' ? 'approved'\n : raw?.escalation_resolution === 'denied' ? 'denied'\n : null;\n const hasVerdict = typeof raw?.decision === 'string';\n let decision: DecisionVerdict;\n // Only translate the resolution when the row is actually an escalation.\n // Gating on raw.decision==='escalate' means a resolution that somehow\n // appears on a payload-bearing 'modify' row can never override it and\n // drop modifiedPayload (defensive — the server only sets resolution on\n // escalate rows today).\n if (raw?.decision === 'escalate' && resolution === 'approved') decision = 'allow';\n else if (raw?.decision === 'escalate' && resolution === 'denied') decision = 'deny';\n else if (hasVerdict) decision = raw.decision as DecisionVerdict;\n else decision = 'deny'; // fail closed on a verdict-less response\n\n return {\n decisionId: raw?.decision_id ?? '',\n decision,\n reason: raw?.reason ?? (hasVerdict ? '' : 'Malformed decision response (no verdict); failing closed'),\n decisionRuleId: raw?.decision_rule_id ?? null,\n modifiedPayload: raw?.modified_payload ?? null,\n // Avoid '' (Date.parse('') === NaN). A verdict-less row gets an epoch\n // timestamp so any staleness check treats it as already expired.\n expiresAt: raw?.expires_at ?? (hasVerdict ? '' : new Date(0).toISOString()),\n latencyMs: raw?.latency_ms ?? 0,\n isFallback: raw?.is_fallback ?? !hasVerdict,\n policyVersion: raw?.policy_version ?? 0,\n escalationResolution: resolution,\n };\n }\n\n // ─── Audit ────────────────────────────────────────────────────────\n\n /**\n * Emit an audit event for an agent action.\n *\n * By default (autoSign=true on the SDK), the event is signed with the\n * agent's private key before it leaves the process. Set options.sign=false\n * to skip signing (e.g. for testing). If the SDK has no private key for the\n * event's agent_id, the event is sent unsigned and a warning is not\n * raised — callers that require signing should check event.signature on\n * the returned event after a round-trip, or use a server-side enforcement\n * flag (VORIM_VERIFY_AUDIT_SIGNATURES).\n */\n async emit(event: AuditEventInput, options?: { sign?: boolean }): Promise<{ ingested: number }> {\n const prepared = await this.prepareEvent(event, options?.sign);\n const result = await this.post('/audit/events', { events: [prepared] }) as { ingested: number };\n this.warnOnPartialIngest(1, result?.ingested ?? 0);\n return result;\n }\n\n /**\n * Emit a batch of audit events (up to 1,000). Each event is signed\n * independently using its agent_id to look up the signing key. See\n * {@link VorimSDK.emit} for signing behaviour and opt-out.\n *\n * When the server returns `ingested < events.length` (e.g. one event\n * referenced an unknown agent_id), a `console.warn` is emitted so\n * operators don't silently lose audit rows. The result is returned\n * unchanged so callers can inspect / act on it.\n */\n async emitBatch(events: AuditEventInput[], options?: { sign?: boolean }): Promise<{ ingested: number }> {\n const prepared = await Promise.all(events.map(e => this.prepareEvent(e, options?.sign)));\n const result = await this.post('/audit/events', { events: prepared }) as { ingested: number };\n this.warnOnPartialIngest(events.length, result?.ingested ?? 0);\n return result;\n }\n\n private warnOnPartialIngest(submitted: number, ingested: number): void {\n if (ingested < submitted) {\n try {\n // eslint-disable-next-line no-console\n console.warn(\n `[@vorim/sdk] partial ingest: server stored ${ingested}/${submitted} events. ` +\n `Common causes: unknown agent_id (cross-org or typo), invalid signature when strict ` +\n `verification is enabled.`,\n );\n } catch {\n /* ignore logger errors */\n }\n }\n }\n\n /**\n * Internal: prepare an event for transmission. Auto-signs if the SDK has a\n * key for this agent and signing isn't explicitly opted out. Pre-existing\n * signatures on the event are respected (caller-signed events are not\n * re-signed). Per-agent failure is non-fatal: if signing throws, the event\n * still sends unsigned so a single bad key doesn't break a batch.\n *\n * Canonical-form dispatch:\n * - If the event already carries `canonical_form`, that wins (caller\n * opted in/out for this specific event).\n * - Otherwise the SDK-level `canonicalForm` (config) applies.\n * - v0 signs the pipe-joined 6 fields. v1 signs the RFC 8785 JCS\n * bytes over the full event minus signature and canonical_form,\n * and the event goes on the wire with `canonical_form: \"v1\"`.\n *\n * Chain construction:\n * - When chainEvents is on, the event gets `prev_event_hash` set to\n * the SHA-256 of the previous event's canonical bytes for the\n * same agent, set BEFORE the signature is computed (so v1\n * signatures cover the chain link).\n * - Chain ops are serialised per-agent via `chainLocks` so\n * concurrent emits to the same agent build a deterministic chain.\n */\n private async prepareEvent(\n event: AuditEventInput,\n signOverride?: boolean\n ): Promise<AuditEventInput> {\n const shouldSign = signOverride ?? this.autoSign;\n if (!shouldSign && !this.chainEvents) return event;\n // Even unsigned events participate in the chain if chaining is on.\n if (this.chainEvents) {\n return this.prepareEventChained(event, shouldSign);\n }\n if (!shouldSign) return event;\n if (event.signature) return event;\n return this.signEventNow(event, shouldSign);\n }\n\n /** Serialise per-agent and apply chain hash + sign. */\n private async prepareEventChained(\n event: AuditEventInput,\n shouldSign: boolean,\n ): Promise<AuditEventInput> {\n const agentId = event.agent_id;\n this.evictChainStateIfNeeded();\n const prior = this.chainLocks.get(agentId) ?? Promise.resolve();\n let release!: () => void;\n const next = new Promise<void>(r => { release = r; });\n this.chainLocks.set(agentId, prior.then(() => next));\n await prior;\n // Snapshot the forget-epoch BEFORE doing any chain work. If the\n // epoch advances while we're in flight, forgetAgentKey() ran and\n // we must not write back the chain tail.\n const startEpoch = this.agentForgetEpoch.get(agentId) ?? 0;\n try {\n // Insert prev_event_hash if we have a previous bytes-hash for this\n // agent. First event after enable / process restart has none.\n const prev = this.lastEventHash.get(agentId);\n const eventWithPrev: AuditEventInput = prev\n ? { ...event, prev_event_hash: prev }\n : event;\n\n // Sign (or pass through unsigned if signing not requested / no\n // key). If forgetAgentKey runs between the lock acquisition and\n // the sign call, skip signing — the key has been revoked and\n // we must not use it. The event still ships unsigned, which the\n // caller can detect via the missing `signature` field.\n const epochBeforeSign = this.agentForgetEpoch.get(agentId) ?? 0;\n const keyStillPresent = this.agentKeys.has(agentId);\n const prepared = shouldSign && !eventWithPrev.signature && keyStillPresent && epochBeforeSign === startEpoch\n ? await this.signEventNow(eventWithPrev, shouldSign)\n : eventWithPrev;\n\n // Record this event's canonical bytes hash as the chain's new tail.\n // Use the v1 form for v1 events (matches what the signature covered);\n // v0 form for v0 events.\n const wireForm = (prepared.canonical_form as 'v0' | 'v1' | undefined) ?? 'v0';\n const bytes = wireForm === 'v1' ? canonicalPayloadV1(prepared) : canonicalPayloadV0(prepared);\n try {\n const tail = await hashPreviousEvent(bytes);\n // Only commit the tail if forgetAgentKey didn't run while we\n // were in flight. Without this guard the revoked agent\n // silently inherits a new chain tail from the in-flight emit.\n if ((this.agentForgetEpoch.get(agentId) ?? 0) === startEpoch) {\n this.lastEventHash.set(agentId, tail);\n }\n } catch {\n // hashing failure (extreme edge) is non-fatal; chain restarts next time\n this.lastEventHash.delete(agentId);\n }\n return prepared;\n } finally {\n release();\n }\n }\n\n /** Sign an event right now, no chain handling.\n *\n * On signing failure (malformed key, Web Crypto unavailable, etc.)\n * the event ships with its requested `canonical_form` preserved and\n * a one-line warning is logged via `console.warn`. The previous\n * behaviour silently dropped both the signature AND the\n * canonical_form marker, which caused the event to be classified\n * server-side as a v0 `signature_missing` instead of a v1 attempt\n * that failed — making the breakage invisible to operators. */\n private async signEventNow(\n event: AuditEventInput,\n _shouldSign: boolean,\n ): Promise<AuditEventInput> {\n const key = this.agentKeys.get(event.agent_id);\n if (!key) return event;\n const form: 'v0' | 'v1' = (event.canonical_form as 'v0' | 'v1' | undefined) ?? this.canonicalForm;\n const withForm: AuditEventInput = form === 'v0'\n ? event\n : { ...event, canonical_form: 'v1' };\n try {\n const payload = form === 'v1' ? canonicalPayloadV1(withForm) : canonicalPayloadV0(withForm);\n const signature = await this.sign(payload, key);\n return { ...withForm, signature };\n } catch (err) {\n // Surface the failure rather than silently dropping the signature.\n // Operators need a signal to detect broken signing at the SDK\n // boundary; verifiers will otherwise misattribute the cause.\n try {\n // eslint-disable-next-line no-console\n console.warn(\n // Don't echo agent_id verbatim — emit a short prefix for\n // correlation while keeping log-sink content lower-cardinality\n // and avoiding accidental leakage of org-identifiable ids.\n `[@vorim/sdk] signing failed for agent_id=${(event.agent_id ?? '').slice(0, 12)}... form=${form}: ${(err as Error)?.message ?? err}. ` +\n `Event will be emitted unsigned with canonical_form retained.`,\n );\n } catch {\n /* ignore logger errors */\n }\n // Preserve canonical_form so the event is still classified as a\n // v1 attempt server-side, just without a signature. This makes\n // the breakage detectable via the signature_missing + v1 form\n // pair instead of looking like a legacy v0 unsigned event.\n return withForm;\n }\n }\n\n /**\n * Export a signed audit bundle for a date range.\n * The server only supports 'json' (the sole allowed export format).\n */\n async exportAudit(from: string, to: string, format: 'json' = 'json'): Promise<any> {\n return this.post('/audit/export', { from, to, format });\n }\n\n // ─── API Keys ──────────────────────────────────────────────────────\n\n /**\n * List all API keys for the organisation.\n * Requires an API key with the apikeys:read scope (or a dashboard JWT).\n */\n async listApiKeys(): Promise<any[]> {\n return this.get('/api-keys');\n }\n\n /**\n * Create a new API key.\n * Requires an API key with the apikeys:write scope (or a dashboard JWT).\n */\n async createApiKey(name: string, options?: { scopes?: string[]; expires_at?: string }): Promise<any> {\n return this.post('/api-keys', { name, ...options });\n }\n\n /**\n * Revoke an API key. Returns the revoked key object (server returns the\n * formatted api_key record, not a { revoked } flag).\n * Requires an API key with the apikeys:write scope (or a dashboard JWT).\n */\n async deleteApiKey(keyId: string): Promise<any> {\n return this.delete(`/api-keys/${keyId}`);\n }\n\n // ─── Ephemeral Agents ──────────────────────────────────────────────\n\n /**\n * Register an ephemeral agent with W3C did:key identity.\n * The agent auto-expires after the specified TTL.\n *\n * Side effect: the returned private_key is cached in this SDK instance's\n * in-memory keyring (matching register()).\n */\n async registerEphemeral(input: {\n capabilities: string[];\n scopes: string[];\n ttl_seconds?: number;\n }): Promise<any> {\n const result = await this.post('/agents/ephemeral', input);\n const agentId: string | undefined = result?.agent?.agent_id ?? result?.did_key;\n if (agentId && result?.private_key) {\n this.agentKeys.set(agentId, result.private_key);\n }\n return result;\n }\n\n // ─── Credential Delegation ──────────────────────────────────────────\n\n /**\n * Register an OAuth provider for credential delegation.\n */\n async registerProvider(input: {\n provider_key: string;\n display_name?: string;\n client_id: string;\n client_secret: string;\n auth_url: string;\n token_url: string;\n revoke_url?: string;\n scopes_available?: string[];\n }): Promise<any> {\n return this.post('/credentials/providers', input);\n }\n\n /**\n * List registered OAuth providers.\n */\n async listProviders(): Promise<any[]> {\n return this.get('/credentials/providers');\n }\n\n /**\n * Store an OAuth connection (user's authorized tokens).\n */\n async storeConnection(input: {\n provider_id: string;\n refresh_token: string;\n scopes_granted: string[];\n external_account_id?: string;\n }): Promise<any> {\n return this.post('/credentials/connections', input);\n }\n\n /**\n * List OAuth connections.\n */\n async listConnections(): Promise<any[]> {\n return this.get('/credentials/connections');\n }\n\n /**\n * Delegate a credential to an agent.\n * The agent will be able to request short-lived access tokens\n * for the delegated scopes without ever seeing the refresh token.\n */\n async delegateCredential(input: {\n connection_id: string;\n agent_id: string;\n scopes_delegated: string[];\n max_requests_per_hr?: number;\n valid_until?: string;\n }): Promise<any> {\n return this.post('/credentials/delegations', input);\n }\n\n /**\n * List credential delegations for the organisation or a specific agent.\n */\n async listDelegations(agentId?: string): Promise<any[]> {\n const params = agentId ? `?agent_id=${agentId}` : '';\n return this.get(`/credentials/delegations${params}`);\n }\n\n /**\n * Revoke a credential delegation (cascades to delegation chains).\n */\n async revokeDelegation(delegationId: string): Promise<{ revoked: boolean }> {\n return this.delete(`/credentials/delegations/${delegationId}`);\n }\n\n /**\n * Request a short-lived access token for an agent.\n * The agent must have an active credential delegation.\n * The refresh token is never exposed — the platform proxies the request.\n */\n async requestToken(input: {\n agent_id: string;\n scope: string;\n provider_id?: string;\n }): Promise<{\n access_token: string;\n token_type: string;\n expires_in: number;\n scope: string;\n delegation_id: string;\n }> {\n return this.post('/credentials/token', input);\n }\n\n // ─── Signing ──────────────────────────────────────────────────────\n\n /**\n * Sign a payload with an Ed25519 private key (client-side).\n * Uses the Web Crypto API or Node.js crypto.\n */\n async sign(payload: string, privateKeyPem: string): Promise<string> {\n if (typeof globalThis.crypto?.subtle !== 'undefined') {\n // Web Crypto API\n const keyData = this.pemToArrayBuffer(privateKeyPem);\n const key = await globalThis.crypto.subtle.importKey(\n 'pkcs8', keyData, { name: 'Ed25519' }, false, ['sign']\n );\n const signature = await globalThis.crypto.subtle.sign(\n 'Ed25519', key, new TextEncoder().encode(payload)\n );\n return `ed25519:${this.arrayBufferToBase64(signature)}`;\n } else {\n // Node.js crypto fallback\n const crypto = await import('node:crypto');\n const sign = crypto.sign(null, Buffer.from(payload), privateKeyPem);\n return `ed25519:${sign.toString('base64')}`;\n }\n }\n\n // ─── HTTP Client ──────────────────────────────────────────────────\n\n private async get(path: string): Promise<any> {\n return this.request('GET', path);\n }\n\n private async post(path: string, body: any): Promise<any> {\n return this.request('POST', path, body);\n }\n\n private async patch(path: string, body: any): Promise<any> {\n return this.request('PATCH', path, body);\n }\n\n private async put(path: string, body: any): Promise<any> {\n return this.request('PUT', path, body);\n }\n\n private async delete(path: string): Promise<any> {\n return this.request('DELETE', path);\n }\n\n /** Like request() but returns the FULL { data, meta, ... } envelope\n * instead of unwrapping to `data`. Used where meta (pagination) matters. */\n private async requestEnvelope(method: string, path: string, body?: any): Promise<Record<string, any>> {\n return this.request(method, path, body, false);\n }\n\n private async request(method: string, path: string, body?: any, unwrap = true): Promise<any> {\n const controller = new AbortController();\n const timeoutId = setTimeout(() => controller.abort(), this.timeout);\n\n try {\n const response = await fetch(`${this.baseUrl}${path}`, {\n method,\n headers: {\n 'Authorization': `Bearer ${this.apiKey}`,\n 'Content-Type': 'application/json',\n 'User-Agent': USER_AGENT,\n },\n body: body ? JSON.stringify(body) : undefined,\n signal: controller.signal,\n });\n\n if (!response.ok) {\n const errBody = await response.json().catch(() => ({})) as Record<string, any>;\n throw new VorimError(\n response.status,\n errBody.error?.code || 'UNKNOWN_ERROR',\n errBody.error?.message || `HTTP ${response.status}`,\n errBody.error?.details\n );\n }\n\n // A 204 No Content (e.g. DELETE endpoints) is a legitimate success with\n // an empty body — there is nothing to parse. Return early so the JSON\n // guard below doesn't misclassify the empty body as MALFORMED_RESPONSE.\n if (response.status === 204) {\n return unwrap ? undefined : {};\n }\n\n // Guard the success-body parse: a 2xx whose body is not valid JSON\n // (e.g. a proxy/CDN/captive-portal \"200 OK\" HTML interstitial) would\n // otherwise throw a bare SyntaxError that beforeAction's catch\n // misclassifies as a status-0 transport failure and, under\n // runtimeFailOpen, silently ALLOWS the gated action. We throw a\n // VorimError with a distinct MALFORMED_RESPONSE code so the gate\n // fails closed (see the isTransport check in beforeAction). Python's\n // before_action catches json.JSONDecodeError the same way.\n let json: Record<string, any>;\n try {\n json = await response.json() as Record<string, any>;\n } catch {\n throw new VorimError(502, 'MALFORMED_RESPONSE', 'Control plane returned a non-JSON success response');\n }\n return unwrap ? json.data : json;\n } finally {\n clearTimeout(timeoutId);\n }\n }\n\n private pemToArrayBuffer(pem: string): ArrayBuffer {\n const b64 = pem.replace(/-----[^-]+-----/g, '').replace(/\\s/g, '');\n const binary = atob(b64);\n const bytes = new Uint8Array(binary.length);\n for (let i = 0; i < binary.length; i++) {\n bytes[i] = binary.charCodeAt(i);\n }\n return bytes.buffer;\n }\n\n private arrayBufferToBase64(buffer: ArrayBuffer): string {\n const bytes = new Uint8Array(buffer);\n let binary = '';\n for (const byte of bytes) {\n binary += String.fromCharCode(byte);\n }\n return btoa(binary);\n }\n}\n\nexport class VorimError extends Error {\n constructor(\n public status: number,\n public code: string,\n message: string,\n public details?: Record<string, unknown>\n ) {\n super(message);\n this.name = 'VorimError';\n }\n}\n\n/**\n * Thrown by {@link VorimSDK.beforeAction} when the runtime decision is\n * `deny` and `throwOnDeny` is left at its default (`true`). Carries the\n * full {@link RuntimeDecision} so the caller can inspect the reason and\n * the decision id. Status is 403 with code `DECISION_DENIED`.\n */\nexport class VorimDeniedError extends VorimError {\n constructor(public decision: RuntimeDecision) {\n super(403, 'DECISION_DENIED', decision.reason, { decisionId: decision.decisionId });\n this.name = 'VorimDeniedError';\n }\n}\n\n// ─── Convenience export ──────────────────────────────────────────────\n\nexport default function createVorim(config: VorimConfig): VorimSDK {\n return new VorimSDK(config);\n}\n\n// ─── Device-flow onboarding (RFC 8628) ──────────────────────────────────────\n\nexport interface DeviceLoginOptions {\n baseUrl?: string;\n /** Shown to the human on the consent screen, e.g. \"Cursor\" or \"my-app\". */\n clientName?: string;\n /** Scopes to request; the server clamps to its ceiling regardless. */\n requestedScopes?: string[];\n /**\n * Called once with the code + URL to show the human. The agent MUST surface\n * `user_code` and `verification_uri` verbatim — do not summarise or truncate.\n */\n onUserCode: (info: { user_code: string; verification_uri: string; verification_uri_complete: string; expires_in: number }) => void;\n /** Stop polling after this many ms (default: the server's expires_in). */\n timeoutMs?: number;\n /**\n * After the key is minted, also register a first agent identity with it, so\n * the agent is fully set up in one call. Pass `true` for a sensible default,\n * or an object to customise. The agent's one-time private key is returned in\n * `result.agent.private_key` — store it; it cannot be retrieved again.\n */\n registerAgent?: boolean | { name?: string; capabilities?: string[]; scopes?: string[] };\n}\n\nexport interface DeviceLoginResult {\n api_key: string;\n key_prefix: string;\n scopes: string[];\n org_id: string;\n /** Present only when `registerAgent` was set — the auto-registered identity. */\n agent?: AgentRegistrationResult;\n}\n\n/**\n * Onboard via the device-authorization flow: obtain a scoped Vorim API key by\n * polling, after a human approves in the browser. No existing key needed — this\n * is the pre-auth bootstrap. Resolves with the key once the human approves;\n * rejects on denial, expiry, or timeout (fail-closed — never returns a partial\n * or unverified credential).\n *\n * @example\n * const { api_key } = await deviceLogin({\n * clientName: 'my-app',\n * onUserCode: ({ user_code, verification_uri }) =>\n * console.log(`Approve at ${verification_uri} — code: ${user_code}`),\n * });\n */\nexport async function deviceLogin(opts: DeviceLoginOptions): Promise<DeviceLoginResult> {\n const base = (opts.baseUrl || 'https://api.vorim.ai').replace(/\\/$/, '') + '/v1';\n\n const startRes = await fetch(`${base}/auth/device`, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({ client_name: opts.clientName, requested_scopes: opts.requestedScopes }),\n signal: AbortSignal.timeout(10000),\n });\n if (!startRes.ok) throw new VorimError(startRes.status, 'DEVICE_START_FAILED', `Could not start device authorization (HTTP ${startRes.status})`);\n const start = ((await startRes.json()) as { data: any }).data;\n\n opts.onUserCode({\n user_code: start.user_code,\n verification_uri: start.verification_uri,\n verification_uri_complete: start.verification_uri_complete,\n expires_in: start.expires_in,\n });\n\n const intervalMs = Math.max(1, start.interval || 5) * 1000;\n const deadline = Date.now() + (opts.timeoutMs ?? start.expires_in * 1000);\n let wait = intervalMs;\n\n // Poll until approved, denied, expired, or our deadline.\n for (;;) {\n if (Date.now() >= deadline) throw new VorimError(408, 'DEVICE_TIMEOUT', 'Device authorization timed out before approval');\n await new Promise((r) => setTimeout(r, wait));\n\n const pollRes = await fetch(`${base}/auth/device/token`, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({ device_code: start.device_code }),\n signal: AbortSignal.timeout(10000),\n });\n const json = (await pollRes.json().catch(() => ({}))) as { data?: any; error?: { code?: string } };\n\n if (pollRes.ok && json.data?.api_key) {\n const result: DeviceLoginResult = {\n api_key: json.data.api_key, key_prefix: json.data.key_prefix, scopes: json.data.scopes, org_id: json.data.org_id,\n };\n // Optionally finish setup: register a first agent identity with the\n // freshly-minted key so the caller gets back a fully-wired agent. Done\n // SDK-side (a normal authenticated POST /agents) — the device endpoints\n // themselves never create agents, keeping that audited surface unchanged.\n if (opts.registerAgent) {\n const cfg = typeof opts.registerAgent === 'object' ? opts.registerAgent : {};\n const sdk = new VorimSDK({ apiKey: result.api_key, baseUrl: opts.baseUrl });\n result.agent = await sdk.register({\n name: cfg.name || opts.clientName || 'my-agent',\n capabilities: cfg.capabilities || [],\n scopes: (cfg.scopes as any) || ['agent:read', 'agent:execute'],\n });\n }\n return result;\n }\n const code = json.error?.code;\n if (code === 'access_denied') throw new VorimError(403, 'DEVICE_DENIED', 'The human denied the authorization request');\n if (code === 'expired_token') throw new VorimError(410, 'DEVICE_EXPIRED', 'The device code expired before approval');\n if (code === 'slow_down') { wait += intervalMs; continue; } // back off per RFC 8628\n // authorization_pending (or any transient): keep polling at the base interval.\n wait = intervalMs;\n }\n}\n\n// Re-export types for consumers\nexport type {\n Agent, AgentRegistrationInput, AgentRegistrationResult,\n TrustRecord, AuditEventInput, AuditEventType, AuditResult,\n PermissionScope, PermissionCheckResult, AgentStatus,\n AgentDelegationRecord, DelegationLinkClaims,\n BeforeActionInput, RuntimeDecision, DecisionVerdict,\n TokenBudget, TokenBudgetInput, BudgetSpend, BudgetScope, BudgetEnforcement,\n WorkflowProvenanceGraph, WorkflowGraphNode, WorkflowGraphEdge, WorkflowGraphEvent,\n SharedMemoryEntry, SharedMemoryWriteInput, SharedMemoryListResult,\n} from './types.js';\n\n// Re-export replayable evidence helpers (VAIP -02 schema field hashing)\nexport {\n hashTool, hashToolCatalogue, hashSystemPrompt, hashPreviousEvent,\n jcsCanonicalise, prepareReplayContext, CANONICAL_TOOL_CATALOGUE_VERSION,\n} from './replay.js';\nexport type { CatalogueTool, ReplayInputs, ReplayContext } from './replay.js';\n","/**\n * Replayable agent decision evidence helpers.\n *\n * Canonical-form hashing for the VAIP -02 schema fields that the SDK\n * attaches to audit events. The hashes recorded in audit_events.tool_catalogue_hash\n * and audit_events.system_prompt_hash use these functions, so the bytes\n * an auditor or counterparty reconstructs must match what the SDK produced.\n *\n * These helpers are intentionally separate from the signing path. The\n * v0 canonical signature form (event_type|action|resource|input_hash|\n * output_hash|result) does NOT cover model_version, tool_catalogue_hash,\n * or system_prompt_hash. They will enter the canonical bytes in v1\n * (RFC 8785 JCS) in a follow-up release.\n *\n * Stable across SDK versions: the canonical-form version is documented\n * in CANONICAL_TOOL_CATALOGUE_VERSION. Future changes get a v2 etc;\n * never edit the existing v1 logic, or already-recorded hashes lose\n * their meaning.\n */\n\n// ─── Versioning ───────────────────────────────────────────────────────────\n\n/**\n * Canonical-form version for tool catalogue hashes produced by this SDK.\n * Recorded in tool_catalogue_canon_version on the event metadata (when\n * the metadata field is used) so verifiers know which hash recipe to\n * reproduce. Increment ONLY if the algorithm changes in a way that\n * would change the hash for the same logical catalogue.\n */\nexport const CANONICAL_TOOL_CATALOGUE_VERSION = 'v1' as const;\n\n// ─── Types ────────────────────────────────────────────────────────────────\n\n/**\n * Minimum shape a tool needs for catalogue hashing. The framework\n * integrations adapt their native tool objects to this shape before\n * calling hashToolCatalogue.\n */\nexport interface CatalogueTool {\n /** The name the model sees and calls. Required. */\n name: string;\n /** Human-readable description shown to the model. Optional; absent ↔ empty string. */\n description?: string;\n /**\n * JSON Schema describing the tool's input parameters. Optional;\n * absent ↔ empty object `{}`. The schema gets RFC 8785 JCS-canonicalised\n * before hashing so semantically-equivalent variations (key order,\n * whitespace) produce the same hash.\n */\n schema?: Record<string, unknown> | null;\n}\n\n// ─── RFC 8785 JCS subset ──────────────────────────────────────────────────\n\n/**\n * RFC 8785 JSON Canonicalization Scheme, sufficient subset for tool\n * catalogue values.\n *\n * Rules:\n * - Object keys sorted lexicographically by UTF-16 code units (which\n * is what JS string comparison does naturally).\n * - No whitespace between tokens.\n * - Numbers: integers as integers, finite floats per ECMAScript\n * Number.prototype.toString. JCS forbids NaN and Infinity.\n * - Strings: JSON-escape using minimal set per RFC 8259 § 7.\n * - null, true, false, arrays: as JSON.stringify produces them, since\n * JSON.stringify already produces the canonical form for these.\n *\n * Not vendoring a full library because tool schemas don't carry\n * non-integer numbers in practice and the JS spec for Number.toString\n * happens to coincide with JCS § 3.2.2.2 for the integer case.\n */\nexport function jcsCanonicalise(value: unknown): string {\n if (value === null) return 'null';\n if (value === true) return 'true';\n if (value === false) return 'false';\n\n if (typeof value === 'number') {\n if (!Number.isFinite(value)) {\n throw new Error('jcsCanonicalise: NaN and Infinity are not JCS-valid');\n }\n // Fail closed on magnitudes where JS .toString() and CPython diverge, so TS\n // and Python sign byte-identical bytes (or both reject). JS switches\n // integer-valued floats to exponent form at |x| >= 1e21 (Python expands to\n // full digits); in [1e-7, 1e-4) JS keeps decimals while Python repr() uses\n // exponent form. Keeping both engines in lockstep preserves cross-SDK\n // signature verifiability.\n const abs = Math.abs(value);\n // Reject ranges where JS Number.toString() and CPython diverge: |x| >= 1e21,\n // |x| < 1e-4, and integer-valued |x| > 2^53 (JS shortest-round-trip vs\n // Python exact int expansion — they only coincide for \"round\" values there).\n if (value !== 0 && (abs >= 1e21 || abs < 1e-4 || (Number.isInteger(value) && abs > 2 ** 53))) {\n throw new Error(\n `jcsCanonicalise: numbers in the ranges where JS and Python formatting ` +\n `diverge (|x| >= 1e21, |x| < 1e-4, or integer |x| > 2^53) are not ` +\n `supported in signed metadata. Use a string instead of ${value}.`,\n );\n }\n // For integers in safe range and ordinary decimals, .toString() matches JCS\n // (modern JS engines use the shortest round-trip representation).\n return value.toString();\n }\n\n if (typeof value === 'string') {\n return JSON.stringify(value);\n }\n\n if (Array.isArray(value)) {\n return '[' + value.map(jcsCanonicalise).join(',') + ']';\n }\n\n if (typeof value === 'object') {\n const obj = value as Record<string, unknown>;\n // Filter undefined-valued fields, matching @vorim/verify and\n // @vorim/shared-types. Without this the SDK throws on { a: 1, b: undefined }\n // while the verifier silently drops b — a cross-module canonical-form\n // divergence that would break signature verification on such events.\n const keys = Object.keys(obj).filter(k => obj[k] !== undefined).sort();\n const parts = keys.map(k => JSON.stringify(k) + ':' + jcsCanonicalise(obj[k]));\n return '{' + parts.join(',') + '}';\n }\n\n // undefined, function, symbol, bigint — not JSON-representable\n throw new Error(`jcsCanonicalise: unsupported value type: ${typeof value}`);\n}\n\n// ─── SHA-256 ──────────────────────────────────────────────────────────────\n\nasync function sha256Hex(input: string | Uint8Array): Promise<string> {\n const bytes = typeof input === 'string' ? new TextEncoder().encode(input) : input;\n\n // Node.js Web Crypto (Node 18+) supports digest. Browser Web Crypto does too.\n // Fall back to node:crypto if Web Crypto is unavailable.\n const subtle = (globalThis as any).crypto?.subtle;\n if (subtle) {\n const buf = await subtle.digest('SHA-256', bytes);\n return Array.from(new Uint8Array(buf))\n .map(b => b.toString(16).padStart(2, '0'))\n .join('');\n }\n\n // Node fallback\n const nodeCrypto = await import('node:crypto');\n return nodeCrypto.createHash('sha256').update(bytes).digest('hex');\n}\n\n// ─── Public API ───────────────────────────────────────────────────────────\n\n/**\n * Hash a single tool definition. Returns `sha256:<hex>`.\n *\n * Canonical form (v1):\n * JCS-canonicalised JSON of `{name, description, schema}` where\n * absent fields substitute `description: \"\"` and `schema: {}`.\n */\nexport async function hashTool(tool: CatalogueTool): Promise<string> {\n const normalised = {\n name: tool.name,\n description: tool.description ?? '',\n schema: tool.schema ?? {},\n };\n const hex = await sha256Hex(jcsCanonicalise(normalised));\n return `sha256:${hex}`;\n}\n\n/**\n * Hash an entire tool catalogue. Returns `sha256:<hex>`.\n *\n * Reordering tools does NOT change the hash (tool hashes sorted\n * lexicographically before concatenation). Adding, removing, or\n * modifying a tool DOES change the hash.\n *\n * Per-tool hashing first means a verifier comparing two catalogue\n * hashes that differ can also be given the per-tool hashes to\n * identify which specific tool changed.\n */\nexport async function hashToolCatalogue(tools: CatalogueTool[]): Promise<string> {\n if (tools.length === 0) {\n // Empty catalogue has a deterministic, stable hash distinct from \"no field\"\n return `sha256:${await sha256Hex('[]')}`;\n }\n const perTool = await Promise.all(tools.map(hashTool));\n perTool.sort();\n const hex = await sha256Hex(perTool.join(''));\n return `sha256:${hex}`;\n}\n\n/**\n * Hash a system prompt. Returns `sha256:<hex>`.\n *\n * The prompt is UTF-8 encoded and hashed verbatim — no normalisation.\n * If a caller wants to ignore whitespace or comment differences, they\n * should normalise before calling. The intent here is deterministic\n * reproducibility, not semantic equivalence.\n */\nexport async function hashSystemPrompt(prompt: string): Promise<string> {\n const hex = await sha256Hex(prompt);\n return `sha256:${hex}`;\n}\n\n/**\n * Convenience: hash the previous event's canonical bytes for use in\n * the prev_event_hash field of hash-chained ingest. Caller provides\n * the canonical bytes (use canonicalPayloadV0 from the main module).\n */\nexport async function hashPreviousEvent(canonicalBytes: string): Promise<string> {\n const hex = await sha256Hex(canonicalBytes);\n return `sha256:${hex}`;\n}\n\n// ─── Replay context — framework integration helper ────────────────────────\n\n/**\n * Raw inputs the integration captures from the framework. Set by the\n * integration's config; turned into hashes by {@link prepareReplayContext}.\n */\nexport interface ReplayInputs {\n /** Stable identifier for the model. E.g. `\"anthropic:claude-opus-4-8\"`. */\n modelVersion?: string;\n /** Tools available to the agent at call time. Hashed via {@link hashToolCatalogue}. */\n tools?: CatalogueTool[];\n /** System prompt active at call time. Hashed via {@link hashSystemPrompt}. */\n systemPrompt?: string;\n}\n\n/**\n * Pre-computed hashes ready to attach to audit events. The three keys\n * match the audit_events column names.\n */\nexport interface ReplayContext {\n model_version?: string;\n tool_catalogue_hash?: string;\n system_prompt_hash?: string;\n}\n\n/**\n * Compute replay context once from raw inputs. Use at integration\n * setup time so each emit can attach the hashes without re-hashing.\n *\n * Returns an object suitable for spreading into an AuditEventInput:\n * `await vorim.emit({ ...event, ...replayContext })`\n *\n * If a field is absent in the inputs, it is absent in the result\n * (not the empty string). That keeps the event lean.\n */\nexport async function prepareReplayContext(\n inputs: ReplayInputs,\n): Promise<ReplayContext> {\n const ctx: ReplayContext = {};\n if (inputs.modelVersion) ctx.model_version = inputs.modelVersion;\n if (inputs.tools) ctx.tool_catalogue_hash = await hashToolCatalogue(inputs.tools);\n if (inputs.systemPrompt) ctx.system_prompt_hash = await hashSystemPrompt(inputs.systemPrompt);\n return ctx;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;AC6BO,IAAM,mCA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KAAK;AAAA,QAAS,YAAY,KAAK,KAAK;AAAA,QAAY,QAAQ,KAAK,KAAK;AAAA,QAAQ,QAAQ,KAAK,KAAK;AAAA,MAC5G;AAKA,UAAI,KAAK,eAAe;AACtB,cAAM,MAAM,OAAO,KAAK,kBAAkB,WAAW,KAAK,gBAAgB,CAAC;AAC3E,cAAM,MAAM,IAAI,SAAS,EAAE,QAAQ,OAAO,SAAS,SAAS,KAAK,QAAQ,CAAC;AAC1E,eAAO,QAAQ,MAAM,IAAI,SAAS;AAAA,UAChC,MAAM,IAAI,QAAQ,KAAK,cAAc;AAAA,UACrC,cAAc,IAAI,gBAAgB,CAAC;AAAA,UACnC,QAAS,IAAI,UAAkB,CAAC,cAAc,eAAe;AAAA,QAC/D,CAAC;AAAA,MACH;AACA,aAAO;AAAA,IACT;AACA,UAAM,OAAO,KAAK,OAAO;AACzB,QAAI,SAAS,gBAAiB,OAAM,IAAI,WAAW,KAAK,iBAAiB,4CAA4C;AACrH,QAAI,SAAS,gBAAiB,OAAM,IAAI,WAAW,KAAK,kBAAkB,yCAAyC;AACnH,QAAI,SAAS,aAAa;AAAE,cAAQ;AAAY;AAAA,IAAU;AAE1D,WAAO;AAAA,EACT;AACF;","names":[]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { A as AgentRegistrationResult, R as RuntimeDecision, b as AgentRegistrationInput, T as TrustRecord, c as Agent, P as PermissionScope, d as PermissionCheckResult, D as DelegationLinkClaims, e as AgentDelegationRecord, B as BeforeActionInput,
|
|
2
|
-
export {
|
|
1
|
+
import { A as AgentRegistrationResult, R as RuntimeDecision, b as AgentRegistrationInput, T as TrustRecord, c as Agent, P as PermissionScope, d as PermissionCheckResult, D as DelegationLinkClaims, e as AgentDelegationRecord, W as WorkflowProvenanceGraph, S as SharedMemoryWriteInput, f as SharedMemoryEntry, g as SharedMemoryListResult, B as BeforeActionInput, h as TokenBudgetInput, i as TokenBudget, j as BudgetSpend, a as AuditEventInput } from './types-zzK9YOoa.cjs';
|
|
2
|
+
export { k as AgentStatus, l as AuditEventType, m as AuditResult, n as BudgetEnforcement, o as BudgetScope, p as DecisionVerdict, q as WorkflowGraphEdge, r as WorkflowGraphEvent, s as WorkflowGraphNode } from './types-zzK9YOoa.cjs';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Replayable agent decision evidence helpers.
|
|
@@ -430,6 +430,136 @@ declare class VorimSDK {
|
|
|
430
430
|
revokeAgentDelegation(agentId: string, chainId: string): Promise<{
|
|
431
431
|
revoked: number;
|
|
432
432
|
}>;
|
|
433
|
+
/**
|
|
434
|
+
* Reconstruct a multi-agent workflow's provenance graph from a delegation
|
|
435
|
+
* chain id — the nodes (agents), edges (who delegated what to whom), and the
|
|
436
|
+
* audit events attributed to each agent within that workflow. Multi-agent
|
|
437
|
+
* fabric Phase 1: the tamper-evident answer to "which agents did what, and
|
|
438
|
+
* who authorised whom?" across a whole hand-off chain.
|
|
439
|
+
*
|
|
440
|
+
* @example
|
|
441
|
+
* ```ts
|
|
442
|
+
* const graph = await vorim.getWorkflowGraph('chain_abc123');
|
|
443
|
+
* for (const edge of graph.edges) {
|
|
444
|
+
* console.log(`${edge.from} -> ${edge.to} [${edge.scopes.join(', ')}]`);
|
|
445
|
+
* }
|
|
446
|
+
* ```
|
|
447
|
+
*/
|
|
448
|
+
getWorkflowGraph(chainId: string, options?: {
|
|
449
|
+
maxEvents?: number;
|
|
450
|
+
}): Promise<WorkflowProvenanceGraph>;
|
|
451
|
+
/**
|
|
452
|
+
* Write (upsert) an entry into the org's governed shared-memory pool,
|
|
453
|
+
* attributed to `agentId`.
|
|
454
|
+
*
|
|
455
|
+
* If the SDK holds the agent's private key (via {@link VorimSDK.register}
|
|
456
|
+
* or {@link VorimSDK.useAgentKey}), the entry is signed at source with the
|
|
457
|
+
* agent's Ed25519 key over the canonical entry (namespace + key + value +
|
|
458
|
+
* agent_id) before it leaves the process, and the server verifies it against
|
|
459
|
+
* the agent's stored public key — rejecting a bad signature (fail closed).
|
|
460
|
+
* This is what makes a poisoned entry always traceable to its source
|
|
461
|
+
* (OWASP ASI06). If no key is available, the entry is written unsigned.
|
|
462
|
+
*
|
|
463
|
+
* A write to an existing (namespace, key) updates it, recording the new
|
|
464
|
+
* writer + signature.
|
|
465
|
+
*
|
|
466
|
+
* Requires an API key with the `memory:write` scope.
|
|
467
|
+
*
|
|
468
|
+
* @example
|
|
469
|
+
* ```ts
|
|
470
|
+
* await vorim.writeMemory('agid_researcher_abc', {
|
|
471
|
+
* namespace: 'research-run-42',
|
|
472
|
+
* key: 'sources',
|
|
473
|
+
* value: { urls: ['https://…'], gathered_at: '2026-07-25T…' },
|
|
474
|
+
* });
|
|
475
|
+
* ```
|
|
476
|
+
*/
|
|
477
|
+
writeMemory(agentId: string, entry: SharedMemoryWriteInput): Promise<SharedMemoryEntry>;
|
|
478
|
+
/**
|
|
479
|
+
* Read one shared-memory entry by (namespace, key). Throws a 404
|
|
480
|
+
* {@link VorimError} if it does not exist. Returns the value plus
|
|
481
|
+
* provenance (which agent wrote it, content hash, whether it is signed).
|
|
482
|
+
*
|
|
483
|
+
* Requires an API key with the `memory:read` scope.
|
|
484
|
+
*/
|
|
485
|
+
readMemory(input: {
|
|
486
|
+
namespace: string;
|
|
487
|
+
key: string;
|
|
488
|
+
}): Promise<SharedMemoryEntry>;
|
|
489
|
+
/**
|
|
490
|
+
* List entries in a namespace (paginated). Returns `{ entries, meta }`.
|
|
491
|
+
*
|
|
492
|
+
* Requires an API key with the `memory:read` scope.
|
|
493
|
+
*/
|
|
494
|
+
listMemory(input: {
|
|
495
|
+
namespace: string;
|
|
496
|
+
page?: number;
|
|
497
|
+
perPage?: number;
|
|
498
|
+
}): Promise<SharedMemoryListResult>;
|
|
499
|
+
/**
|
|
500
|
+
* Delete one shared-memory entry by (namespace, key).
|
|
501
|
+
*
|
|
502
|
+
* Requires an API key with the `memory:write` scope.
|
|
503
|
+
*/
|
|
504
|
+
deleteMemory(input: {
|
|
505
|
+
namespace: string;
|
|
506
|
+
key: string;
|
|
507
|
+
}): Promise<{
|
|
508
|
+
deleted: true;
|
|
509
|
+
namespace: string;
|
|
510
|
+
key: string;
|
|
511
|
+
}>;
|
|
512
|
+
/**
|
|
513
|
+
* Verified hand-off — the multi-agent-fabric primitive.
|
|
514
|
+
*
|
|
515
|
+
* Hands a task from one agent to another as ONE auditable operation that
|
|
516
|
+
* composes the three trust primitives:
|
|
517
|
+
* 1. (optional) VERIFY the receiving agent is trusted + active before
|
|
518
|
+
* handing it anything — fails closed if it is not (unless you opt out).
|
|
519
|
+
* 2. DELEGATE a strictly-attenuated subset of the sender's scopes to the
|
|
520
|
+
* receiver (the server enforces child ⊆ parent + depth; a scope the
|
|
521
|
+
* sender doesn't hold is rejected). Optionally as a signed capability
|
|
522
|
+
* link so the hand-off is offline-verifiable by `@vorim/verify`.
|
|
523
|
+
* 3. EMIT a signed audit event recording the hand-off, stamped with the
|
|
524
|
+
* delegation-chain provenance so the whole multi-agent workflow is
|
|
525
|
+
* reconstructable later.
|
|
526
|
+
*
|
|
527
|
+
* Returns the delegation record (with `public_chain_id`) plus the verification
|
|
528
|
+
* result. Throws before delegating if verification fails.
|
|
529
|
+
*
|
|
530
|
+
* @example
|
|
531
|
+
* ```ts
|
|
532
|
+
* const { chain } = await vorim.handOff('agid_planner', {
|
|
533
|
+
* to: 'agid_researcher',
|
|
534
|
+
* scopes: ['agent:read'], // researcher gets read-only, nothing more
|
|
535
|
+
* task: 'gather sources for Q3 report',
|
|
536
|
+
* maxChainDepth: 0, // researcher cannot re-delegate
|
|
537
|
+
* minTrustScore: 60,
|
|
538
|
+
* });
|
|
539
|
+
* console.log(chain.publicChainId);
|
|
540
|
+
* ```
|
|
541
|
+
*/
|
|
542
|
+
handOff(fromAgentId: string, input: {
|
|
543
|
+
/** The receiving agent's id. */
|
|
544
|
+
to: string;
|
|
545
|
+
/** Scopes to delegate — must be a subset of what `fromAgentId` holds. */
|
|
546
|
+
scopes: PermissionScope[];
|
|
547
|
+
/** Human-readable description of the handed-off task (recorded in audit). */
|
|
548
|
+
task?: string;
|
|
549
|
+
/** How many further hops the receiver may make (default 0 — no re-delegation). */
|
|
550
|
+
maxChainDepth?: number;
|
|
551
|
+
/** Delegation expiry (ISO 8601). */
|
|
552
|
+
validUntil?: string;
|
|
553
|
+
/** Minimum trust score the receiver must have to accept the hand-off (default 50). */
|
|
554
|
+
minTrustScore?: number;
|
|
555
|
+
/** Skip the pre-delegation trust verification (NOT recommended). @default false */
|
|
556
|
+
skipVerify?: boolean;
|
|
557
|
+
/** Sign the delegation link so the hand-off is offline-verifiable. @default false */
|
|
558
|
+
signed?: boolean;
|
|
559
|
+
}): Promise<{
|
|
560
|
+
chain: AgentDelegationRecord;
|
|
561
|
+
verification?: TrustRecord;
|
|
562
|
+
}>;
|
|
433
563
|
/**
|
|
434
564
|
* Gate an agent action BEFORE it is performed. Calls
|
|
435
565
|
* `POST /v1/runtime/decisions` and returns a typed {@link RuntimeDecision}.
|
|
@@ -775,4 +905,4 @@ interface DeviceLoginResult {
|
|
|
775
905
|
*/
|
|
776
906
|
declare function deviceLogin(opts: DeviceLoginOptions): Promise<DeviceLoginResult>;
|
|
777
907
|
|
|
778
|
-
export { Agent, AgentDelegationRecord, AgentRegistrationInput, AgentRegistrationResult, AuditEventInput, BeforeActionInput, BudgetSpend, CANONICAL_TOOL_CATALOGUE_VERSION, type CatalogueTool, DelegationLinkClaims, type DeviceLoginOptions, type DeviceLoginResult, PermissionCheckResult, PermissionScope, type ReplayContext, type ReplayInputs, RuntimeDecision, TokenBudget, TokenBudgetInput, TrustRecord, type VorimConfig, VorimDeniedError, VorimError, VorimSDK, canonicalPayloadV0, canonicalPayloadV1, createVorim as default, deviceLogin, hashPreviousEvent, hashSystemPrompt, hashTool, hashToolCatalogue, jcsCanonicalise, prepareReplayContext };
|
|
908
|
+
export { Agent, AgentDelegationRecord, AgentRegistrationInput, AgentRegistrationResult, AuditEventInput, BeforeActionInput, BudgetSpend, CANONICAL_TOOL_CATALOGUE_VERSION, type CatalogueTool, DelegationLinkClaims, type DeviceLoginOptions, type DeviceLoginResult, PermissionCheckResult, PermissionScope, type ReplayContext, type ReplayInputs, RuntimeDecision, SharedMemoryEntry, SharedMemoryListResult, SharedMemoryWriteInput, TokenBudget, TokenBudgetInput, TrustRecord, type VorimConfig, VorimDeniedError, VorimError, VorimSDK, WorkflowProvenanceGraph, canonicalPayloadV0, canonicalPayloadV1, createVorim as default, deviceLogin, hashPreviousEvent, hashSystemPrompt, hashTool, hashToolCatalogue, jcsCanonicalise, prepareReplayContext };
|