{
  "version": 3,
  "sources": ["../src/agent-guide.ts"],
  "sourcesContent": ["/** App-owned knowledge. Action authority and schemas come from the gateway. */\nexport interface AgentGuideManifest {\n  name: string;\n  version: string;\n  actions: Array<{ name: string; capabilityId: string; contextKeys?: string[] }>;\n}\nexport interface AgentContextRequest {\n  actionNames: string[];\n  knownRevision?: string;\n  maxBytes?: number;\n}\nexport interface AgentContextSelection {\n  actionNames: string[];\n  contextKeys: string[];\n}\nexport type AgentContextReader = (selection?: AgentContextSelection) => Record<string, unknown> | Promise<Record<string, unknown>>;\nexport interface ScopedAgentContext {\n  origin: string;\n  observedAt: string;\n  revision: string;\n  manifest?: AgentGuideManifest;\n  context: Record<string, unknown>;\n  omittedContextKeys: string[];\n}\nexport interface AgentGuideSnapshot extends AgentGuideManifest {\n  origin: string;\n  observedAt: string;\n  context: Record<string, unknown>;\n}\nexport interface AuthorizedActionCard {\n  id: string;\n  name: string;\n  description: string;\n  inputSchema: Record<string, unknown>;\n  permissions: string[];\n  sensitivity: string;\n  approval: { always: boolean; amountField?: string; above?: number };\n}\nexport interface AgentActionReceipt {\n  id: string;\n  status: string;\n  verification: { passed: boolean; verifiedAt: string | null };\n  nextAction: string;\n}\n\nfunction cloneJson<T>(value: T, maxBytes: number): T {\n  const text = JSON.stringify(value, (key, entry: unknown) => {\n    if (/(?:password|secret|credential|token|apikey|agentkey|licensekey|authorization|cookie)/i.test(key))\n      throw new Error(\"Credentials must not be placed in an agent guide\");\n    if (typeof entry === \"function\" || typeof entry === \"bigint\" || typeof entry === \"symbol\")\n      throw new Error(\"Agent guide must contain JSON data\");\n    return entry;\n  });\n  if (!text || new TextEncoder().encode(text).byteLength > maxBytes)\n    throw new Error(\"Agent guide exceeds its size limit\");\n  return JSON.parse(text) as T;\n}\n\nexport class AgentGuideRegistry {\n  private manifest: AgentGuideManifest | null = null;\n  private readContext: AgentContextReader = () => ({});\n\n  register(manifest: AgentGuideManifest, readContext?: typeof this.readContext) {\n    const copy = cloneJson(manifest, 8192);\n    if (typeof copy.name !== \"string\" || !copy.name.trim() || copy.name.length > 100 ||\n        typeof copy.version !== \"string\" || !copy.version || copy.version.length > 80 ||\n        !Array.isArray(copy.actions) || !copy.actions.length || copy.actions.length > 32)\n      throw new Error(\"Invalid app agent guide\");\n    const names = new Set<string>(), ids = new Set<string>();\n    for (const action of copy.actions) {\n      if (!/^smart365_[a-z][a-z0-9_]{0,54}$/.test(action.name) || action.name === \"smart365_verify_execution\" ||\n          typeof action.capabilityId !== \"string\" || !action.capabilityId || action.capabilityId.length > 100 ||\n          names.has(action.name) || ids.has(action.capabilityId))\n        throw new Error(\"Agent actions require unique names and capability IDs\");\n      names.add(action.name); ids.add(action.capabilityId);\n      if (action.contextKeys !== undefined && (!Array.isArray(action.contextKeys) || action.contextKeys.length > 16 ||\n          new Set(action.contextKeys).size !== action.contextKeys.length || action.contextKeys.some(key =>\n            typeof key !== \"string\" || !/^[a-zA-Z][a-zA-Z0-9_]{0,63}$/.test(key) || [\"constructor\", \"prototype\"].includes(key) ||\n            /(?:password|secret|credential|token|apikey|agentkey|licensekey|authorization|cookie)/i.test(key))))\n        throw new Error(\"Action context keys must be unique field names\");\n    }\n    this.manifest = { name: copy.name, version: copy.version, actions: copy.actions.map(({ name, capabilityId, contextKeys }) => ({ name, capabilityId, ...(contextKeys ? { contextKeys } : {}) })) };\n    this.readContext = readContext ?? (() => ({}));\n  }\n\n  async snapshot(origin: string): Promise<AgentGuideSnapshot> {\n    if (!this.manifest) throw new Error(\"This application has no registered agent guide\");\n    const context = await this.readContext();\n    if (!context || typeof context !== \"object\" || Array.isArray(context))\n      throw new Error(\"Agent context must be an object\");\n    return cloneJson({ ...this.manifest, origin, observedAt: new Date().toISOString(), context }, 16384);\n  }\n\n  /** Host-declared data only; this selection never grants action authority. */\n  async context(origin: string, request: AgentContextRequest): Promise<ScopedAgentContext> {\n    if (!this.manifest) throw new Error(\"This application has no registered agent guide\");\n    if (!Array.isArray(request.actionNames) || !request.actionNames.length || request.actionNames.length > 10 ||\n        new Set(request.actionNames).size !== request.actionNames.length)\n      throw new Error(\"Select between one and ten unique registered actions\");\n    const maxBytes = request.maxBytes ?? 4096;\n    if (!Number.isInteger(maxBytes) || maxBytes < 1024 || maxBytes > 16384)\n      throw new Error(\"Agent context budget must be between 1024 and 16384 bytes\");\n    const original = this.manifest;\n    const manifest = cloneJson(original, 8192);\n    manifest.actions = request.actionNames.map(name => {\n      const action = manifest.actions.find(candidate => candidate.name === name);\n      if (!action) throw new Error(\"Unknown registered agent action\");\n      return action;\n    });\n    const contextKeys = [...new Set(manifest.actions.flatMap(action => action.contextKeys ?? []))];\n    const bytes = new TextEncoder().encode(JSON.stringify(manifest));\n    const digest = await crypto.subtle.digest(\"SHA-256\", bytes);\n    const revision = Array.from(new Uint8Array(digest), byte => byte.toString(16).padStart(2, \"0\")).join(\"\");\n    const reader = this.readContext;\n    const fresh = await reader({ actionNames: [...request.actionNames], contextKeys: [...contextKeys] });\n    if (reader !== this.readContext || this.manifest !== original) throw new Error(\"The registered guide has changed\");\n    if (!fresh || typeof fresh !== \"object\" || Array.isArray(fresh)) throw new Error(\"Agent context must be an object\");\n    const response: ScopedAgentContext = {\n      origin, observedAt: new Date().toISOString(), revision,\n      ...(request.knownRevision === revision ? {} : { manifest }),\n      context: {}, omittedContextKeys: [...contextKeys],\n    };\n    const size = () => new TextEncoder().encode(JSON.stringify(response)).byteLength;\n    if (size() > maxBytes) throw new Error(\"The complete action manifest exceeds the context budget; increase maxBytes\");\n    for (const key of contextKeys) {\n      if (!Object.prototype.hasOwnProperty.call(fresh, key) || fresh[key] === undefined) continue;\n      // Never truncate an input or silently substitute another dropdown value.\n      const value = cloneJson(fresh[key], 16384);\n      response.context[key] = value;\n      response.omittedContextKeys = response.omittedContextKeys.filter(candidate => candidate !== key);\n      if (size() > maxBytes) {\n        delete response.context[key];\n        response.omittedContextKeys.push(key);\n      }\n    }\n    return response;\n  }\n\n  clear() { this.manifest = null; this.readContext = () => ({}); }\n}\n\n/** Compile only app-listed, currently authorized cards into typed agent tools. */\nexport function createAgentTools(guide: AgentGuideManifest, cards: AuthorizedActionCard[]) {\n  const registry = new AgentGuideRegistry(); registry.register(guide);\n  return guide.actions.flatMap((action) => {\n    const card = cards.find((candidate) => candidate.id === action.capabilityId);\n    if (!card) return [];\n    if (card.inputSchema.type !== \"object\") throw new Error(\"Action inputs must be an object schema\");\n    return [{\n      name: action.name,\n      capabilityId: card.id,\n      description: `${card.name}. ${card.description} Permissions: ${card.permissions.join(\", \")}. Sensitivity: ${card.sensitivity}. Approval policy: ${JSON.stringify(card.approval)}. Execute through Smart365; a verified receipt confirms completion.`,\n      inputSchema: cloneJson(card.inputSchema, 32768),\n    }];\n  });\n}\n\n/** One stable operation key per request/action; changed retry input is rejected by the gateway. */\nexport async function agentExecutionArguments(\n  action: { capabilityId: string }, input: Record<string, unknown>, requestId: string,\n) {\n  if (typeof requestId !== \"string\" || requestId.length < 8 || requestId.length > 200)\n    throw new Error(\"An agent action requires a unique business request ID\");\n  const bytes = new TextEncoder().encode(JSON.stringify([requestId, action.capabilityId]));\n  const hash = await crypto.subtle.digest(\"SHA-256\", bytes);\n  const key = Array.from(new Uint8Array(hash), byte => byte.toString(16).padStart(2, \"0\")).join(\"\");\n  return { id: action.capabilityId, input: cloneJson(input, 32768), idempotencyKey: \"s365_\" + key, detail: \"compact\" as const };\n}\n"],
  "mappings": "AA6CA,SAASA,EAAaC,EAAUC,EAAqB,CACnD,IAAMC,EAAO,KAAK,UAAUF,EAAO,CAACG,EAAKC,IAAmB,CAC1D,GAAI,wFAAwF,KAAKD,CAAG,EAClG,MAAM,IAAI,MAAM,kDAAkD,EACpE,GAAI,OAAOC,GAAU,YAAc,OAAOA,GAAU,UAAY,OAAOA,GAAU,SAC/E,MAAM,IAAI,MAAM,oCAAoC,EACtD,OAAOA,CACT,CAAC,EACD,GAAI,CAACF,GAAQ,IAAI,YAAY,EAAE,OAAOA,CAAI,EAAE,WAAaD,EACvD,MAAM,IAAI,MAA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GAAG,CAAC,EAAE,KAAK,EAAE,EAChG,MAAO,CAAE,GAAIZ,EAAO,aAAc,MAAOZ,EAAUkC,EAAO,KAAK,EAAG,eAAgB,QAAU9B,EAAK,OAAQ,SAAmB,CAC9H",
  "names": ["cloneJson", "value", "maxBytes", "text", "key", "entry", "AgentGuideRegistry", "manifest", "readContext", "copy", "names", "ids", "action", "name", "capabilityId", "contextKeys", "origin", "context", "request", "original", "candidate", "bytes", "digest", "revision", "byte", "reader", "fresh", "response", "size", "createAgentTools", "guide", "cards", "card", "agentExecutionArguments", "input", "requestId", "hash"]
}
