{"version":3,"file":"wrap.cjs","sources":["@gensx/core/../../../../src/wrap.ts"],"sourcesContent":["import type { ComponentOpts } from \"./types.js\";\n\nimport { isProxy } from \"node:util/types\";\n\nimport { Component } from \"./component.js\";\n\n/**\n * Options for wrapping SDKs and functions.\n */\nexport interface WrapOptions {\n  /** Optional prefix for all generated component names. */\n  prefix?: string;\n  /** Optional function to generate component options based on the path and arguments. */\n  getComponentOpts?: (path: string[], args: unknown) => Partial<ComponentOpts>;\n  replacementImplementations?: Record<\n    string,\n    (origTarget: object, prop: object) => object\n  >;\n}\n\n/**\n * Recursively walks an SDK instance and returns a proxy whose *functions*\n * are GenSX components and whose *objects* are wrapped proxies.\n */\nexport function wrap<T extends object>(sdk: T, opts: WrapOptions = {}): T {\n  /**\n   * Internal helper that builds a proxy for `target` and keeps track of the\n   * path so we can generate sensible component names like:\n   *   \"OpenAI.chat.completions.create\"\n   */\n  const makeProxy = <U extends object>(target: U, path: string[]): U => {\n    if (isProxy(target)) {\n      return target;\n    }\n\n    const proxy = new Proxy(target, {\n      get(origTarget, propKey, receiver) {\n        const value = Reflect.get(origTarget, propKey, receiver);\n\n        const newPath = [...path, String(propKey)].join(\".\");\n        const replacementImplementation =\n          opts.replacementImplementations?.[newPath];\n\n        if (replacementImplementation) {\n          return replacementImplementation(origTarget, value as object);\n        }\n\n        // ----- Case 1: it's a function → return a GenSX component\n        if (typeof value === \"function\") {\n          // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n          if ((value as any).__gensxComponent) {\n            return value;\n          }\n\n          const componentName =\n            (opts.prefix ? `${opts.prefix}.` : \"\") +\n            [...path, String(propKey)].join(\".\");\n\n          // Bind the original `this` so SDK internals keep working\n          const boundFn = value.bind(origTarget) as (input?: object) => unknown;\n          const componentOpts = opts.getComponentOpts?.(path, boundFn);\n          return Component(componentName, boundFn, {\n            ...componentOpts,\n          });\n        }\n\n        // ----- Case 2: it's an object that might contain more functions\n        if (\n          typeof value === \"object\" &&\n          !Array.isArray(value) &&\n          !(value instanceof Date)\n        ) {\n          return makeProxy(value as object, [...path, String(propKey)]);\n        }\n\n        // ----- Case 3: primitive or unhandled → pass through untouched\n        return value;\n      },\n    });\n\n    return proxy;\n  };\n\n  // Kick things off with the SDK's constructor name as the first path element\n  const hasCustomConstructor =\n    \"constructor\" in sdk && sdk.constructor !== Object;\n  const rootName = hasCustomConstructor ? sdk.constructor.name : \"sdk\";\n\n  return makeProxy(sdk, [rootName]);\n}\n\n/**\n * A class decorator that wraps all methods of a class into GenSX components.\n * This allows you to use any class as a collection of GenSX components.\n *\n * Compatible with both legacy experimental decorators and the new ECMAScript decorator proposal.\n *\n * @param options Optional configuration for the wrapper\n * @returns A class decorator function\n *\n * @example\n * ```tsx\n * // Legacy syntax (experimentalDecorators)\n * @Wrap()\n * class Calculator {\n *   add(input: { a: number; b: number }) {\n *     return input.a + input.b;\n *   }\n * }\n *\n * // New syntax (ECMAScript decorators)\n * @Wrap()\n * class Calculator {\n *   add(input: { a: number; b: number }) {\n *     return input.a + input.b;\n *   }\n * }\n * ```\n */\nexport function Wrap(options: WrapOptions = {}) {\n  // Legacy decorator implementation\n  function legacyDecorator<T extends new (...args: unknown[]) => object>(\n    constructor: T,\n  ) {\n    // Create a new constructor function\n    const WrappedClass = function (this: object, ...args: unknown[]) {\n      // Call the original constructor\n      constructor.apply(this, args);\n      // Wrap the instance\n      return wrap(this, options);\n    };\n\n    // Copy prototype so instanceof operator still works\n    WrappedClass.prototype = constructor.prototype;\n    // Copy static properties\n    Object.assign(WrappedClass, constructor);\n\n    return WrappedClass as unknown as T;\n  }\n\n  // New decorator implementation\n  function newDecorator<T extends new (...args: unknown[]) => object>(\n    target: T,\n    context: ClassDecoratorContext,\n  ) {\n    // Add an initializer that wraps the instance\n    context.addInitializer(function (this: object) {\n      return wrap(this, options);\n    });\n\n    return target;\n  }\n\n  // Return the appropriate decorator based on the context\n  return function decorator<T extends new (...args: unknown[]) => object>(\n    target: T,\n    context?: ClassDecoratorContext,\n  ) {\n    // If context is provided, we're using the new decorator syntax\n    if (context) {\n      return newDecorator(target, context);\n    }\n    // Otherwise, we're using the legacy decorator syntax\n    return legacyDecorator(target);\n  };\n}\n"],"names":["isProxy","Component"],"mappings":";;;;;;;;;;;AAoBA;;;AAGG;SACa,IAAI,CAAmB,GAAM,EAAE,OAAoB,EAAE,EAAA;AACnE;;;;AAIG;AACH,IAAA,MAAM,SAAS,GAAG,CAAmB,MAAS,EAAE,IAAc,KAAO;AACnE,QAAA,IAAIA,aAAO,CAAC,MAAM,CAAC,EAAE;AACnB,YAAA,OAAO,MAAM;;AAGf,QAAA,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,MAAM,EAAE;AAC9B,YAAA,GAAG,CAAC,UAAU,EAAE,OAAO,EAAE,QAAQ,EAAA;AAC/B,gBAAA,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE,OAAO,EAAE,QAAQ,CAAC;AAExD,gBAAA,MAAM,OAAO,GAAG,CAAC,GAAG,IAAI,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC;gBACpD,MAAM,yBAAyB,GAC7B,IAAI,CAAC,0BAA0B,GAAG,OAAO,CAAC;gBAE5C,IAAI,yBAAyB,EAAE;AAC7B,oBAAA,OAAO,yBAAyB,CAAC,UAAU,EAAE,KAAe,CAAC;;;AAI/D,gBAAA,IAAI,OAAO,KAAK,KAAK,UAAU,EAAE;;AAE/B,oBAAA,IAAK,KAAa,CAAC,gBAAgB,EAAE;AACnC,wBAAA,OAAO,KAAK;;AAGd,oBAAA,MAAM,aAAa,GACjB,CAAC,IAAI,CAAC,MAAM,GAAG,CAAG,EAAA,IAAI,CAAC,MAAM,CAAA,CAAA,CAAG,GAAG,EAAE;AACrC,wBAAA,CAAC,GAAG,IAAI,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC;;oBAGtC,MAAM,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