{"version":3,"file":"modelContextAdapter.cjs","names":[],"sources":["../../../src/web/lib/modelContextAdapter.ts"],"sourcesContent":["import type { ModelContext, ModelContextBinding, ModelContextToolDescriptor } from '@agimon-ai/po-core/web';\n\nimport type { WebMcpSimulator } from './webmcpSimulator';\n\ninterface ModelContextDocument {\n  readonly modelContext?: unknown;\n}\n\nlet bindingPromise: Promise<ModelContextBinding> | undefined;\nlet simulator: WebMcpSimulator | undefined;\nlet generation = 0;\n\nfunction hasMethod(value: object, name: keyof ModelContext): boolean {\n  return typeof (value as Record<keyof ModelContext, unknown>)[name] === 'function';\n}\n\nfunction isCompleteModelContext(value: unknown): value is ModelContext {\n  if (typeof value !== 'object' || value === null) return false;\n  return (\n    hasMethod(value, 'registerTool') &&\n    hasMethod(value, 'getTools') &&\n    hasMethod(value, 'executeTool') &&\n    hasMethod(value, 'addEventListener') &&\n    hasMethod(value, 'removeEventListener')\n  );\n}\n\nfunction cloneDescriptor(descriptor: ModelContextToolDescriptor): ModelContextToolDescriptor {\n  return {\n    name: descriptor.name,\n    description: descriptor.description,\n    inputSchema: structuredClone(descriptor.inputSchema),\n  };\n}\n\nfunction bindNative(modelContext: ModelContext): ModelContext {\n  return {\n    registerTool: modelContext.registerTool.bind(modelContext),\n    getTools: async () => (await modelContext.getTools()).map(cloneDescriptor),\n    executeTool: modelContext.executeTool.bind(modelContext),\n    addEventListener: modelContext.addEventListener.bind(modelContext),\n    removeEventListener: modelContext.removeEventListener.bind(modelContext),\n  };\n}\n\nfunction browserDocument(): ModelContextDocument {\n  if (typeof document === 'undefined') return {};\n  return document as unknown as ModelContextDocument;\n}\n\n/** Acquires one model-context binding for the lifetime of this page. */\nexport function acquireModelContext(host: ModelContextDocument = browserDocument()): Promise<ModelContextBinding> {\n  bindingPromise ??= (async () => {\n    const acquiredGeneration = generation;\n    const native = host.modelContext;\n    if (isCompleteModelContext(native)) return { kind: 'native', modelContext: bindNative(native) };\n\n    const { createWebMcpSimulator } = await import('./webmcpSimulator');\n    const created = createWebMcpSimulator();\n    if (acquiredGeneration === generation) simulator = created;\n    else created.dispose();\n    return { kind: 'simulator', modelContext: created };\n  })();\n  return bindingPromise;\n}\n\n/** Releases the fallback surface during root teardown. Native browser state remains browser-owned. */\nexport function disposeModelContextAdapter(): void {\n  generation += 1;\n  simulator?.dispose();\n  simulator = undefined;\n  bindingPromise = undefined;\n}\n"],"mappings":";AAQA,IAAI;AACJ,IAAI;AACJ,IAAI,aAAa;AAEjB,SAAS,UAAU,OAAe,MAAmC;CACnE,OAAO,OAAQ,MAA8C,UAAU;AACzE;AAEA,SAAS,uBAAuB,OAAuC;CACrE,IAAI,OAAO,UAAU,YAAY,UAAU,MAAM,OAAO;CACxD,OACE,UAAU,OAAO,cAAc,KAC/B,UAAU,OAAO,UAAU,KAC3B,UAAU,OAAO,aAAa,KAC9B,UAAU,OAAO,kBAAkB,KACnC,UAAU,OAAO,qBAAqB;AAE1C;AAEA,SAAS,gBAAgB,YAAoE;CAC3F,OAAO;EACL,MAAM,WAAW;EACjB,aAAa,WAAW;EACxB,aAAa,gBAAgB,WAAW,WAAW;CACrD;AACF;AAEA,SAAS,WAAW,cAA0C;CAC5D,OAAO;EACL,cAAc,aAAa,aAAa,KAAK,YAAY;EACzD,UAAU,aAAa,MAAM,aAAa,SAAS,EAAA,CAAG,IAAI,eAAe;EACzE,aAAa,aAAa,YAAY,KAAK,YAAY;EACvD,kBAAkB,aAAa,iBAAiB,KAAK,YAAY;EACjE,qBAAqB,aAAa,oBAAoB,KAAK,YAAY;CACzE;AACF;AAEA,SAAS,kBAAwC;CAC/C,IAAI,OAAO,aAAa,aAAa,OAAO,CAAC;CAC7C,OAAO;AACT;;AAGA,SAAgB,oBAAoB,OAA6B,gBAAgB,GAAiC;CAChH,oBAAoB,YAAY;EAC9B,MAAM,qBAAqB;EAC3B,MAAM,SAAS,KAAK;EACpB,IAAI,uBAAuB,MAAM,GAAG,OAAO;GAAE,MAAM;GAAU,cAAc,WAAW,MAAM;EAAE;EAE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