{"version":3,"sources":["../../src/adapters/onnx.ts"],"names":["onnxAdapter","ort","_task","config","device","onProgress","modelUrl","executionProviders","session","model","input","_options","feeds","outputNames","onChunk","options","result"],"mappings":"aAyCO,SAASA,GAAmC,CACjD,OAAO,CACL,IAAA,CAAM,OAEN,MAAM,MAAA,EAAgC,CAEpC,IAAMC,EAAM,MAAM,OAAO,iBAAiB,CAAA,CAE1C,OAAO,CACL,IAAA,CAAM,MAAA,CAEN,gBAAA,CAAiBC,EAAeC,CAAAA,CAAyC,CACvE,OAAQA,CAAAA,CAAO,mBAA4C,EAC7D,CAAA,CAEA,MAAM,IAAA,CACJD,EACAC,CAAAA,CACAC,CAAAA,CACAC,EACsB,CACtB,IAAMC,EAAWH,CAAAA,CAAO,KAAA,CAExBE,CAAAA,CAAW,CAAE,OAAQ,CAAA,CAAG,KAAA,CAAO,aAAA,CAAe,KAAA,CAAO,CAAE,CAAC,CAAA,CAExD,IAAME,CAAAA,CAAqBH,IAAW,QAAA,CAClC,CAAC,QAAA,CAAU,MAAM,EACjB,CAAC,MAAM,CAAA,CAELI,CAAAA,CAA4B,MAAMP,CAAAA,CAAI,gBAAA,CAAiB,MAAA,CAAOK,CAAAA,CAAU,CAC5E,kBAAA,CAAAC,CAAAA,CACA,sBAAA,CAAwB,KAC1B,CAAC,CAAA,CAED,OAAAF,EAAW,CAAE,MAAA,CAAQ,EAAG,KAAA,CAAO,MAAA,CAAQ,KAAA,CAAO,CAAE,CAAC,CAAA,CAE1C,CACL,QAAA,CAAUG,CAAAA,CACV,SAAWL,CAAAA,CAAO,iBAAA,EAA4C,EAChE,CACF,EAEA,MAAM,GAAA,CAAIM,CAAAA,CAAoBC,CAAAA,CAAgBC,EAAsC,CAClF,IAAMH,CAAAA,CAAUC,CAAAA,CAAM,SAChB,CAAE,KAAA,CAAAG,CAAAA,CAAO,WAAA,CAAAC,CAAY,CAAA,CAAIH,CAAAA,CAG/B,OADgB,MAAMF,EAAQ,GAAA,CAAII,CAAAA,CAAOC,CAAW,CAEtD,CAAA,CAEA,MAAM,MAAA,CACJJ,CAAAA,CACAC,CAAAA,CACAI,CAAAA,CACAC,EACe,CAGf,IAAMP,CAAAA,CAAUC,CAAAA,CAAM,SAChB,CAAE,KAAA,CAAAG,CAAAA,CAAO,WAAA,CAAAC,CAAY,CAAA,CAAIH,CAAAA,CACzBM,CAAAA,CAAS,MAAMR,EAAQ,GAAA,CAAII,CAAAA,CAAOC,CAAAA,CAAaE,CAAO,EAC5DD,CAAAA,CAAQE,CAAM,EAChB,CAAA,CAEA,MAAM,MAAA,CAAOP,CAAAA,CAAmC,CAE9C,MADgBA,EAAM,QAAA,CACR,OAAA,KAChB,CACF,CACF,CACF,CACF","file":"onnx.cjs","sourcesContent":["import type {\n  Device,\n  LoadedModel,\n  LoadProgressEvent,\n  ModelAdapter,\n  ModelAdapterFactory,\n} from '../core/types.js';\n\n// eslint-disable-next-line ts/no-explicit-any\ntype InferenceSession = any;\n// eslint-disable-next-line ts/no-explicit-any\ntype Tensor = any;\n\ninterface OnnxInput {\n  feeds: Record<string, Tensor>;\n  outputNames?: string[];\n}\n\n/**\n * Adapter for onnxruntime-web.\n *\n * @remarks\n * Provides low-level access to ONNX model inference.\n * Use for custom models not supported by transformers.js or web-llm.\n * Input must be pre-processed `OrtTensor` instances.\n *\n * @example\n * ```ts\n * import { createPool } from 'inferis-ml';\n * import { onnxAdapter } from 'inferis-ml/adapters/onnx';\n * import * as ort from 'onnxruntime-web';\n *\n * const pool = await createPool({ adapter: onnxAdapter() });\n * const model = await pool.load('custom', {\n *   model: 'https://example.com/model.onnx',\n * });\n *\n * const input = new ort.Tensor('float32', data, [1, 3, 224, 224]);\n * const output = await model.run({ feeds: { input } });\n * ```\n */\nexport function onnxAdapter(): ModelAdapterFactory {\n  return {\n    name: 'onnx',\n\n    async create(): Promise<ModelAdapter> {\n      // @ts-expect-error - optional peer dependency, resolved at runtime inside worker\n      const ort = await import('onnxruntime-web');\n\n      return {\n        name: 'onnx',\n\n        estimateMemoryMB(_task: string, config: Record<string, unknown>): number {\n          return (config.estimatedMemoryMB as number | undefined) ?? 50;\n        },\n\n        async load(\n          _task: string,\n          config: Record<string, unknown>,\n          device: Device,\n          onProgress: (event: LoadProgressEvent) => void,\n        ): Promise<LoadedModel> {\n          const modelUrl = config.model as string;\n\n          onProgress({ loaded: 0, phase: 'downloading', total: 0 });\n\n          const executionProviders = device === 'webgpu'\n            ? ['webgpu', 'wasm'] as const\n            : ['wasm'] as const;\n\n          const session: InferenceSession = await ort.InferenceSession.create(modelUrl, {\n            executionProviders,\n            graphOptimizationLevel: 'all',\n          });\n\n          onProgress({ loaded: 1, phase: 'done', total: 1 });\n\n          return {\n            instance: session,\n            memoryMB: (config.estimatedMemoryMB as number | undefined) ?? 50,\n          };\n        },\n\n        async run(model: LoadedModel, input: unknown, _options?: unknown): Promise<unknown> {\n          const session = model.instance as InferenceSession;\n          const { feeds, outputNames } = input as OnnxInput;\n\n          const results = await session.run(feeds, outputNames);\n          return results;\n        },\n\n        async stream(\n          model: LoadedModel,\n          input: unknown,\n          onChunk: (chunk: unknown) => void,\n          options?: unknown,\n        ): Promise<void> {\n          // ONNX Runtime Web doesn't natively support streaming inference.\n          // Run full inference and emit the result as a single chunk.\n          const session = model.instance as InferenceSession;\n          const { feeds, outputNames } = input as OnnxInput;\n          const result = await session.run(feeds, outputNames, options);\n          onChunk(result);\n        },\n\n        async unload(model: LoadedModel): Promise<void> {\n          const session = model.instance as InferenceSession;\n          await session.release?.();\n        },\n      };\n    },\n  };\n}\n"]}