@imgly/plugin-ai-text-generation-web 0.2.9 → 0.2.11

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.
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../plugin-ai-generation-web/src/ui/common/renderImageUrlProperty.ts", "../../../plugin-utils/src/icons/formats.ts", "../../../plugin-utils/src/assetSources/CustomAssetSource.ts", "../../../plugin-utils/src/assetSources/IndexedDBAssetSource.ts", "../../../plugin-utils/src/assetSources/AggregatedAssetSource.ts", "../../../plugin-utils/src/metadata/Metadata.ts", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_freeGlobal.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_root.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_Symbol.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_getRawTag.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_objectToString.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_baseGetTag.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/isObjectLike.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/isArray.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/isObject.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/isFunction.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_coreJsData.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_isMasked.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_toSource.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_baseIsNative.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_getValue.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_getNative.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_WeakMap.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_isIndex.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/eq.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/isLength.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/isArrayLike.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_isPrototype.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_baseTimes.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_baseIsArguments.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/isArguments.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/stubFalse.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/isBuffer.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_baseIsTypedArray.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_baseUnary.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_nodeUtil.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/isTypedArray.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_arrayLikeKeys.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_overArg.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_nativeKeys.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_baseKeys.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/keys.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_nativeCreate.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_hashClear.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_hashDelete.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_hashGet.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_hashHas.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_hashSet.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_Hash.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_listCacheClear.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_assocIndexOf.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_listCacheDelete.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_listCacheGet.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_listCacheHas.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_listCacheSet.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_ListCache.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_Map.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_mapCacheClear.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_isKeyable.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_getMapData.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_mapCacheDelete.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_mapCacheGet.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_mapCacheHas.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_mapCacheSet.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_MapCache.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_arrayPush.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_stackClear.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_stackDelete.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_stackGet.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_stackHas.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_stackSet.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_Stack.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_arrayFilter.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/stubArray.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_getSymbols.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_baseGetAllKeys.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_getAllKeys.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_DataView.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_Promise.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_Set.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_getTag.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_Uint8Array.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_setCacheAdd.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_setCacheHas.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_SetCache.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_arraySome.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_cacheHas.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_equalArrays.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_mapToArray.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_setToArray.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_equalByTag.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_equalObjects.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_baseIsEqualDeep.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_baseIsEqual.js", "../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/isEqual.js", "../../../plugin-utils/src/metadata/FillProcessingMetadata.ts", "../../../plugin-utils/src/processing/fillProcessing.ts", "../../../plugin-utils/src/processing/constants.ts", "../../../plugin-utils/src/processing/initializeFillProcessing.ts", "../../../plugin-utils/src/processing/registerFillProcessingComponents.ts", "../../../plugin-utils/src/utils/colors.ts", "../../../plugin-utils/src/utils/upload.ts", "../../../plugin-utils/src/utils/uuid.ts", "../../../plugin-utils/src/utils/images.ts", "../../../plugin-utils/src/utils/isDefined.ts", "../../../plugin-utils/src/utils/toArray.ts", "../../../plugin-utils/src/translationHelpers.ts", "../../../plugin-utils/src/index.ts", "../../../plugin-ai-generation-web/src/ui/common/renderStyleTransferProperty.ts", "../../../plugin-ai-generation-web/src/utils/propertyContext.ts", "../../../plugin-ai-generation-web/src/utils/propertyResolver.ts", "../../../plugin-ai-generation-web/src/assets/integrateIntoDefaultAssetLibraryEntry.ts", "../../../plugin-ai-generation-web/src/core/ActionRegistry.ts", "../../../plugin-ai-generation-web/src/core/ProviderRegistry.ts", "../../../plugin-ai-generation-web/src/middleware/middleware.ts", "../../../plugin-ai-generation-web/src/middleware/loggingMiddleware.ts", "../../../plugin-ai-generation-web/src/utils/utils.ts", "../../../plugin-ai-generation-web/src/middleware/uploadMiddleware.ts", "../../../plugin-ai-generation-web/src/utils/mergeQuickActionsConfig.ts", "../../../plugin-ai-generation-web/src/middleware/rateLimitMiddleware.ts", "../../../plugin-ai-generation-web/src/utils/checkAiPluginVersion.ts", "../../../plugin-ai-generation-web/src/ui/components/registerDockComponent.ts", "../../../plugin-ai-generation-web/src/ui/quickActions/enableImageFill.ts", "../../../plugin-ai-generation-web/src/generation/handleGenerationError.ts", "../../../plugin-ai-generation-web/src/assets/previewUri.ts", "../../../plugin-ai-generation-web/src/assets/getAssetResultForPlaceholder.ts", "../../../plugin-ai-generation-web/src/assets/getAssetResultForGenerated.ts", "../../../plugin-ai-generation-web/src/generation/handleGenerateFromPanel.ts", "../../../plugin-ai-generation-web/src/ui/components/renderGenerationComponents.ts", "../../../plugin-ai-generation-web/src/ui/panels/createPanelRenderFunctionFromCustom.ts", "../../../plugin-ai-generation-web/src/openapi/dereferenceDocument.ts", "../../../plugin-ai-generation-web/src/openapi/isOpenAPISchema.ts", "../../../plugin-ai-generation-web/src/openapi/getProperties.ts", "../../../plugin-ai-generation-web/src/openapi/renderProperty.ts", "../../../plugin-ai-generation-web/src/openapi/defaultTranslations.ts", "../../../plugin-ai-generation-web/src/openapi/extractSchemaTranslations.ts", "../../../plugin-ai-generation-web/src/ui/panels/createPanelRenderFunctionFromSchema.ts", "../../../plugin-ai-generation-web/src/ui/panels/createPanelRenderFunction.ts", "../../../plugin-ai-generation-web/src/assets/initializeHistoryAssetSource.ts", "../../../plugin-ai-generation-web/src/assets/initializeHistoryAssetLibraryEntry.ts", "../../../plugin-ai-generation-web/src/ui/icons.ts", "../../../plugin-ai-generation-web/src/middleware/dryRunMiddleware.ts", "../../../plugin-ai-generation-web/src/core/constants.ts", "../../../plugin-ai-generation-web/src/generation/createGenerateFunction.ts", "../../../plugin-ai-generation-web/src/providers/initializeProvider.ts", "../../../plugin-ai-generation-web/src/assets/initializeHistoryCompositeAssetSource.ts", "../../../plugin-ai-generation-web/src/providers/initializeProviders.ts", "../../../plugin-ai-generation-web/src/ui/quickActions/utils.ts", "../../../plugin-ai-generation-web/src/ui/panels/createConfirmationRenderFunction.ts", "../../../plugin-ai-generation-web/src/utils/compactSeparators.ts", "../../../plugin-ai-generation-web/src/providers/getCanvasMenuComponentId.ts", "../../../plugin-ai-generation-web/src/ui/quickActions/getQuickActionOrder.ts", "../../../plugin-ai-generation-web/src/providers/getApplyCallbacks.ts", "../../../plugin-ai-generation-web/src/utils/lockSelectionToEditMode.ts", "../../../plugin-ai-generation-web/src/generation/CallbacksRegistry.ts", "../../../plugin-ai-generation-web/src/generation/handleGenerateFromQuickAction.ts", "../../../plugin-ai-generation-web/src/ui/quickActions/createQuickActionMenuRenderFunction.ts", "../../../plugin-ai-generation-web/src/ui/quickActions/initializeQuickActionComponents.ts", "../../../plugin-ai-generation-web/src/utils/translationHelpers.ts", "../../../plugin-ai-generation-web/src/index.ts", "../../../../node_modules/.pnpm/@anthropic-ai+sdk@0.39.0/node_modules/@anthropic-ai/sdk/src/version.ts", "../../../../node_modules/.pnpm/@anthropic-ai+sdk@0.39.0/node_modules/@anthropic-ai/sdk/src/_shims/registry.ts", "../../../../node_modules/.pnpm/@anthropic-ai+sdk@0.39.0/node_modules/@anthropic-ai/sdk/src/_shims/MultipartBody.ts", "../../../../node_modules/.pnpm/@anthropic-ai+sdk@0.39.0/node_modules/@anthropic-ai/sdk/src/_shims/web-runtime.ts", "../../../../node_modules/.pnpm/@anthropic-ai+sdk@0.39.0/node_modules/@anthropic-ai/sdk/_shims/index.mjs", "../../../../node_modules/.pnpm/@anthropic-ai+sdk@0.39.0/node_modules/@anthropic-ai/sdk/src/error.ts", "../../../../node_modules/.pnpm/@anthropic-ai+sdk@0.39.0/node_modules/@anthropic-ai/sdk/src/internal/decoders/line.ts", "../../../../node_modules/.pnpm/@anthropic-ai+sdk@0.39.0/node_modules/@anthropic-ai/sdk/src/internal/stream-utils.ts", "../../../../node_modules/.pnpm/@anthropic-ai+sdk@0.39.0/node_modules/@anthropic-ai/sdk/src/streaming.ts", "../../../../node_modules/.pnpm/@anthropic-ai+sdk@0.39.0/node_modules/@anthropic-ai/sdk/src/uploads.ts", "../../../../node_modules/.pnpm/@anthropic-ai+sdk@0.39.0/node_modules/@anthropic-ai/sdk/src/core.ts", "../../../../node_modules/.pnpm/@anthropic-ai+sdk@0.39.0/node_modules/@anthropic-ai/sdk/src/pagination.ts", "../../../../node_modules/.pnpm/@anthropic-ai+sdk@0.39.0/node_modules/@anthropic-ai/sdk/src/resource.ts", "../../../../node_modules/.pnpm/@anthropic-ai+sdk@0.39.0/node_modules/@anthropic-ai/sdk/src/resources/beta/models.ts", "../../../../node_modules/.pnpm/@anthropic-ai+sdk@0.39.0/node_modules/@anthropic-ai/sdk/src/internal/decoders/jsonl.ts", "../../../../node_modules/.pnpm/@anthropic-ai+sdk@0.39.0/node_modules/@anthropic-ai/sdk/src/resources/beta/messages/batches.ts", "../../../../node_modules/.pnpm/@anthropic-ai+sdk@0.39.0/node_modules/@anthropic-ai/sdk/src/_vendor/partial-json-parser/parser.ts", "../../../../node_modules/.pnpm/@anthropic-ai+sdk@0.39.0/node_modules/@anthropic-ai/sdk/src/lib/BetaMessageStream.ts", "../../../../node_modules/.pnpm/@anthropic-ai+sdk@0.39.0/node_modules/@anthropic-ai/sdk/src/resources/beta/messages/messages.ts", "../../../../node_modules/.pnpm/@anthropic-ai+sdk@0.39.0/node_modules/@anthropic-ai/sdk/src/resources/beta/beta.ts", "../../../../node_modules/.pnpm/@anthropic-ai+sdk@0.39.0/node_modules/@anthropic-ai/sdk/src/resources/completions.ts", "../../../../node_modules/.pnpm/@anthropic-ai+sdk@0.39.0/node_modules/@anthropic-ai/sdk/src/resources/messages/batches.ts", "../../../../node_modules/.pnpm/@anthropic-ai+sdk@0.39.0/node_modules/@anthropic-ai/sdk/src/lib/MessageStream.ts", "../../../../node_modules/.pnpm/@anthropic-ai+sdk@0.39.0/node_modules/@anthropic-ai/sdk/src/resources/messages/messages.ts", "../../../../node_modules/.pnpm/@anthropic-ai+sdk@0.39.0/node_modules/@anthropic-ai/sdk/src/resources/models.ts", "../../../../node_modules/.pnpm/@anthropic-ai+sdk@0.39.0/node_modules/@anthropic-ai/sdk/src/index.ts", "../../src/anthropic/sendPrompt.ts", "../../src/anthropic/AnthropicProvider.ts", "../../src/anthropic/index.ts"],
4
- "sourcesContent": ["import CreativeEditorSDK, { AssetResult } from '@cesdk/cesdk-js';\nimport { RenderCustomProperty } from '../../core/provider';\n\n/**\n * Provides render function for a image url property that allows\n * to select an image from the library with a MediaPreview\n *\n * By default this expects the property key to be `image_url`. This can be changed in the options.\n */\nfunction renderImageUrlProperty(\n provderId: string,\n options: {\n cesdk: CreativeEditorSDK;\n propertyKey?: string;\n defaultUrl?: string;\n }\n): RenderCustomProperty {\n const { cesdk } = options;\n const propertyKey = options.propertyKey ?? 'image_url';\n const panelIdForImageSelection = getImageSelectionPanelId(provderId);\n\n cesdk.i18n.setTranslations({\n en: {\n [`panel.${panelIdForImageSelection}`]: 'Select Image To Change',\n 'ly.img.ai.imageSelection.selectImage.label': 'Select Image',\n 'ly.img.ai.imageSelection.error.svg':\n 'SVG images are not supported. Please choose a different image.',\n 'ly.img.ai.imageSelection.error.invalidType':\n \"Only images are supported. Found '{mimeType}'. Please choose a different image.\"\n }\n });\n\n createPanels(provderId, cesdk);\n\n const customProperties: RenderCustomProperty = {\n [propertyKey]: (context, property) => {\n const {\n builder,\n experimental: { global },\n payload\n } = context;\n\n // Check for provider configuration defaults\n let configuredDefault: string | undefined;\n const providerConfig = (context as any).providerConfig;\n const pluginConfig = (context as any).config;\n\n // Check provider config first, then plugin config\n const propertyConfig =\n providerConfig?.properties?.[property.id] ??\n (pluginConfig as any)?.properties?.[property.id];\n\n if (propertyConfig?.default) {\n if (typeof propertyConfig.default === 'function') {\n // If it's a function, call it with a basic context\n configuredDefault = propertyConfig.default({}) as string;\n } else {\n configuredDefault = propertyConfig.default as string;\n }\n }\n\n // Use configured default, then payload url, then static default\n const defaultUrl =\n configuredDefault ?? payload?.url ?? options.defaultUrl;\n const stateValue = global<string>(\n `${provderId}.${property.id}`,\n defaultUrl\n );\n\n builder.MediaPreview(property.id, {\n preview: {\n type: 'image',\n uri: stateValue.value\n },\n action: {\n label: 'ly.img.ai.imageSelection.selectImage.label',\n onClick: () => {\n if (cesdk == null) return;\n\n cesdk.ui.openPanel(panelIdForImageSelection, {\n payload: {\n onSelect: (assetResult: AssetResult) => {\n if (assetResult.meta?.uri != null) {\n stateValue.setValue(assetResult.meta?.uri);\n }\n }\n }\n });\n }\n }\n });\n\n return () => {\n return {\n id: property.id,\n type: 'string',\n value: stateValue.value\n };\n };\n }\n };\n\n return customProperties;\n}\n\nfunction createPanels(providerId: string, cesdk?: CreativeEditorSDK) {\n if (cesdk == null) return;\n\n cesdk.ui.registerPanel<{\n onSelect: (assetResult: AssetResult) => void;\n }>(getImageSelectionPanelId(providerId), ({ builder, payload }) => {\n builder.Library(`${providerId}.library.image`, {\n entries: ['ly.img.image'],\n onSelect: async (asset) => {\n const uri = asset?.meta?.uri;\n if (uri == null) return;\n\n const mimeType = await cesdk.engine.editor.getMimeType(uri);\n if (mimeType === 'image/svg+xml') {\n cesdk.ui.showNotification({\n type: 'warning',\n message: 'ly.img.ai.imageSelection.error.svg'\n });\n } else if (mimeType.startsWith('image/')) {\n payload?.onSelect(asset);\n cesdk?.ui.closePanel(getImageSelectionPanelId(providerId));\n } else {\n cesdk.ui.showNotification({\n type: 'warning',\n message: `ly.img.ai.imageSelection.error.invalidType`\n });\n }\n }\n });\n });\n}\n\nfunction getImageSelectionPanelId(providerId: string) {\n return `ly.img.ai.${providerId}.imageSelection`;\n}\n\nexport default renderImageUrlProperty;\n", "export default `\n<svg>\n <symbol\n fill=\"none\"\n xmlns=\"http://www.w3.org/2000/svg\"\n viewBox=\"0 0 24 24\"\n id=\"@imgly/plugin/formats/ratioFree\"\n >\n <path d=\"M7 6C6.44772 6 6 6.44772 6 7V9.22222H4V7C4 5.34315 5.34315 4 7 4H9.22222V6H7Z\" fill=\"currentColor\"/>\n <path d=\"M17 6H14.7778V4H17C18.6569 4 20 5.34315 20 7V9.22222H18V7C18 6.44772 17.5523 6 17 6Z\" fill=\"currentColor\"/>\n <path d=\"M6 14.7778V17C6 17.5523 6.44772 18 7 18H9.22222V20H7C5.34315 20 4 18.6569 4 17V14.7778H6Z\" fill=\"currentColor\"/>\n <path d=\"M18 17V14.7778H20V17C20 18.6569 18.6569 20 17 20H14.7778V18H17C17.5523 18 18 17.5523 18 17Z\" fill=\"currentColor\"/>\n </symbol>\n <symbol\n fill=\"none\"\n xmlns=\"http://www.w3.org/2000/svg\"\n viewBox=\"0 0 24 24\"\n id=\"@imgly/plugin/formats/ratio4by3\"\n >\n <path d=\"M6.5 13H8V15H10V16.5H6.5V13Z\" fill=\"currentColor\"/>\n <path d=\"M14 9V7.5H17.5V11H16V9H14Z\" fill=\"currentColor\"/>\n <path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M6 4C4.34315 4 3 5.34315 3 7V17C3 18.6569 4.34315 20 6 20H18C19.6569 20 21 18.6569 21 17V7C21 5.34315 19.6569 4 18 4H6ZM5 7C5 6.44772 5.44772 6 6 6H18C18.5523 6 19 6.44772 19 7V17C19 17.5523 18.5523 18 18 18H6C5.44772 18 5 17.5523 5 17V7Z\" fill=\"currentColor\"/>\n </symbol>\n <symbol\n fill=\"none\"\n xmlns=\"http://www.w3.org/2000/svg\"\n viewBox=\"0 0 24 24\"\n id=\"@imgly/plugin/formats/ratio16by9\"\n >\n <path d=\"M4.5 13H6V15H8V16.5H4.5V13Z\" fill=\"currentColor\"/>\n <path d=\"M16 9V7.5H19.5V11H18V9H16Z\" fill=\"currentColor\"/>\n <path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M4 4C2.34315 4 1 5.34315 1 7V17C1 18.6569 2.34315 20 4 20H20C21.6569 20 23 18.6569 23 17V7C23 5.34315 21.6569 4 20 4H4ZM3 7C3 6.44772 3.44772 6 4 6H20C20.5523 6 21 6.44772 21 7V17C21 17.5523 20.5523 18 20 18H4C3.44772 18 3 17.5523 3 17V7Z\" fill=\"currentColor\"/>\n </symbol>\n <symbol\n fill=\"none\"\n xmlns=\"http://www.w3.org/2000/svg\"\n viewBox=\"0 0 24 24\"\n id=\"@imgly/plugin/formats/ratio9by16\"\n >\n <path d=\"M7.5 16H9V18H11V19.5H7.5V16Z\" fill=\"currentColor\"/>\n <path d=\"M13 6V4.5H16.5V8H15V6H13Z\" fill=\"currentColor\"/>\n <path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M4 20C4 21.6569 5.34315 23 7 23H17C18.6569 23 20 21.6569 20 20V4C20 2.34315 18.6569 1 17 1H7C5.34315 1 4 2.34315 4 4V20ZM7 21C6.44772 21 6 20.5523 6 20V4C6 3.44772 6.44772 3 7 3H17C17.5523 3 18 3.44772 18 4V20C18 20.5523 17.5523 21 17 21H7Z\" fill=\"currentColor\"/>\n </symbol>\n <symbol\n fill=\"none\"\n xmlns=\"http://www.w3.org/2000/svg\"\n viewBox=\"0 0 24 24\"\n id=\"@imgly/plugin/formats/ratio3by4\"\n >\n <path d=\"M11 17.5V16H9V14H7.5V17.5H11Z\" fill=\"currentColor\"/>\n <path d=\"M15 10H16.5V6.5H13V8H15V10Z\" fill=\"currentColor\"/>\n <path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M20 18C20 19.6569 18.6569 21 17 21H7C5.34315 21 4 19.6569 4 18V6C4 4.34315 5.34315 3 7 3H17C18.6569 3 20 4.34315 20 6V18ZM17 19C17.5523 19 18 18.5523 18 18V6C18 5.44772 17.5523 5 17 5H7C6.44771 5 6 5.44771 6 6V18C6 18.5523 6.44772 19 7 19H17Z\" fill=\"currentColor\"/>\n </symbol>\n <symbol\n fill=\"none\"\n xmlns=\"http://www.w3.org/2000/svg\"\n viewBox=\"0 0 24 24\"\n id=\"@imgly/plugin/formats/ratio1by1\"\n >\n <path d=\"M17.4142 8.00009L16 6.58587L14.2929 8.29298L15.7071 9.70719L17.4142 8.00009Z\" fill=\"currentColor\"/>\n <path d=\"M13.0404 12.3739L15.0404 10.3739L13.6262 8.95965L11.6262 10.9596L13.0404 12.3739Z\" fill=\"currentColor\"/>\n <path d=\"M10.3737 15.0405L12.3737 13.0405L10.9595 11.6263L8.95953 13.6263L10.3737 15.0405Z\" fill=\"currentColor\"/>\n <path d=\"M9.70708 15.7072L8.29286 14.293L6.58576 16.0001L7.99997 17.4143L9.70708 15.7072Z\" fill=\"currentColor\"/>\n <path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M7 4C5.34315 4 4 5.34315 4 7V17C4 18.6569 5.34315 20 7 20H17C18.6569 20 20 18.6569 20 17V7C20 5.34315 18.6569 4 17 4H7ZM6 7C6 6.44772 6.44772 6 7 6H17C17.5523 6 18 6.44772 18 7V17C18 17.5523 17.5523 18 17 18H7C6.44772 18 6 17.5523 6 17V7Z\" fill=\"currentColor\"/>\n </symbol>\n</svg>\n`;\n", "import type {\n AssetDefinition,\n AssetQueryData,\n AssetResult,\n AssetSource,\n AssetsQueryResult\n} from '@cesdk/engine';\n\n/**\n * Simplified value type for select inputs that can be converted to asset definitions\n */\nexport type SelectValue = {\n id: string;\n label: string;\n thumbUri?: string;\n meta?: { [key: string]: any };\n};\n\n/**\n * Options for CustomAssetSource constructor\n */\nexport interface CustomAssetSourceOptions {\n /**\n * Optional callback function to translate asset labels\n * @param assetId - The ID of the asset to translate\n * @param fallbackLabel - The fallback label to use if translation is not available\n * @param locale - The current locale\n * @returns The translated label or fallback\n */\n translateLabel?: (\n assetId: string,\n fallbackLabel: string,\n locale: string\n ) => string;\n}\n\n/**\n * A custom AssetSource implementation that manages assets from an array\n * and provides additional functionality like to mark assets as active or changing\n * labels.\n */\nexport class CustomAssetSource implements AssetSource {\n /** The unique id of the asset source */\n id: string;\n\n /** Array of assets to be served by this source */\n private assets: AssetDefinition[];\n\n /** Set of IDs for active assets */\n private activeAssetIds: Set<string>;\n\n /** Optional translation callback function */\n private translateLabel?: (\n assetId: string,\n fallbackLabel: string,\n locale: string\n ) => string;\n\n /**\n * Creates a new instance of CustomAssetSource\n *\n * @param id - The unique identifier for this asset source\n * @param assets - Array of asset definitions or SelectValue objects to include in this source\n * @param options - Optional configuration for the asset source\n */\n constructor(\n id: string,\n assets: (AssetDefinition | SelectValue)[] = [],\n options?: CustomAssetSourceOptions\n ) {\n this.id = id;\n this.translateLabel = options?.translateLabel;\n this.assets = assets.map((asset) => {\n // Check if the asset is a SelectValue by looking for the label property as a string\n if (\n typeof (asset as SelectValue).label === 'string' &&\n !(\n (asset as AssetDefinition).label &&\n typeof (asset as AssetDefinition).label === 'object'\n )\n ) {\n const selectValue = asset as SelectValue;\n // Convert SelectValue to AssetDefinition\n return {\n id: selectValue.id,\n label: { en: selectValue.label },\n meta: selectValue.thumbUri\n ? { ...(selectValue.meta ?? {}), thumbUri: selectValue.thumbUri }\n : selectValue.meta\n } as AssetDefinition;\n }\n return asset as AssetDefinition;\n });\n this.activeAssetIds = new Set<string>();\n\n // Automatically set first asset as active if available\n if (this.assets.length > 0) {\n this.activeAssetIds.add(this.assets[0].id);\n }\n }\n\n /**\n * Find assets based on the provided query data\n * Supports pagination, searching, filtering, and active-first sorting\n *\n * @param queryData - Query parameters to filter and sort assets\n * @returns Promise with the query results\n */\n async findAssets(\n queryData: AssetQueryData\n ): Promise<AssetsQueryResult | undefined> {\n const {\n page,\n perPage,\n locale = 'en',\n sortActiveFirst,\n query,\n tags,\n groups,\n excludeGroups,\n sortingOrder,\n sortKey\n } = queryData;\n\n // Start with all assets\n let filteredAssets = [...this.assets];\n\n // Filter by groups if provided\n if (groups && groups.length > 0) {\n filteredAssets = filteredAssets.filter(\n (asset) =>\n asset.groups && groups.some((group) => asset.groups?.includes(group))\n );\n }\n\n // Filter out excluded groups if provided\n if (excludeGroups && excludeGroups.length > 0) {\n filteredAssets = filteredAssets.filter(\n (asset) =>\n !asset.groups ||\n !excludeGroups.some((group) => asset.groups?.includes(group))\n );\n }\n\n // Filter by query (search in label and tags)\n if (query) {\n const lowerQuery = query.toLowerCase();\n filteredAssets = filteredAssets.filter((asset) => {\n const label = asset.label?.[locale]?.toLowerCase();\n const assetTags = asset.tags?.[locale] || [];\n\n return (\n (label && label.includes(lowerQuery)) ||\n assetTags.some((tag) => tag.toLowerCase().includes(lowerQuery))\n );\n });\n }\n\n // Filter by exact tags if provided\n if (tags && tags.length > 0) {\n const tagArray = Array.isArray(tags) ? tags : [tags];\n filteredAssets = filteredAssets.filter((asset) => {\n const assetTags = asset.tags?.[locale] || [];\n return tagArray.some((tag) => assetTags.includes(tag));\n });\n }\n\n // Sort by active first if requested\n if (sortActiveFirst) {\n filteredAssets.sort((a, b) => {\n const aActive = this.activeAssetIds.has(a.id);\n const bActive = this.activeAssetIds.has(b.id);\n\n if (aActive && !bActive) return -1;\n if (!aActive && bActive) return 1;\n return 0;\n });\n }\n\n // Sort by sortKey if provided\n if (sortKey && sortKey !== 'id') {\n filteredAssets.sort((a, b) => {\n // Sort by metadata field\n const aValue = a.meta?.[sortKey];\n const bValue = b.meta?.[sortKey];\n\n if (aValue === undefined) return 1;\n if (bValue === undefined) return -1;\n\n if (typeof aValue === 'string' && typeof bValue === 'string') {\n return sortingOrder === 'Descending'\n ? bValue.localeCompare(aValue)\n : aValue.localeCompare(bValue);\n }\n\n if (typeof aValue === 'number' && typeof bValue === 'number') {\n return sortingOrder === 'Descending'\n ? bValue - aValue\n : aValue - bValue;\n }\n\n return 0;\n });\n } else if (sortKey === 'id') {\n // Sort by id\n filteredAssets.sort((a, b) => {\n return sortingOrder === 'Descending'\n ? b.id.localeCompare(a.id)\n : a.id.localeCompare(b.id);\n });\n }\n\n // Calculate pagination\n const total = filteredAssets.length;\n const startIndex = page * perPage;\n const endIndex = startIndex + perPage;\n const paginatedAssets = filteredAssets.slice(startIndex, endIndex);\n\n // Transform AssetDefinition objects to AssetResult objects\n const resultAssets: AssetResult[] = paginatedAssets.map((asset) => {\n // Use translation callback if provided, otherwise use default label\n const fallbackLabel = asset.label?.[locale] || '';\n const label = this.translateLabel\n ? this.translateLabel(asset.id, fallbackLabel, locale)\n : fallbackLabel;\n\n return {\n id: asset.id,\n groups: asset.groups,\n meta: asset.meta,\n payload: asset.payload,\n locale,\n label,\n tags: asset.tags?.[locale],\n active: this.activeAssetIds.has(asset.id)\n };\n });\n\n // Calculate next page if there are more assets\n const nextPage = endIndex < total ? page + 1 : undefined;\n\n return {\n assets: resultAssets,\n currentPage: page,\n nextPage,\n total\n };\n }\n\n updateLabel(assetId: string, label: string, locale: string): void {\n this.assets.forEach((asset) => {\n if (asset.id === assetId) {\n asset.label = asset.label || {};\n asset.label[locale] = label;\n }\n });\n }\n\n /**\n * Get the asset select value by its ID\n *\n * @param assetId - The ID of the asset to retrieve\n * @returns The SelectValue object for the asset or undefined if not found\n */\n getAssetSelectValue(assetId: string): SelectValue | undefined {\n const asset = this.assets.find(({ id }) => id === assetId);\n if (asset) {\n // Get translated label if translation callback is available\n const label = this.translateLabel\n ? this.translateLabel(asset.id, asset.label?.en || '', '')\n : asset.label?.en || '';\n\n return {\n id: asset.id,\n label,\n thumbUri: asset.meta?.thumbUri\n };\n }\n return undefined;\n }\n\n /**\n * Get the currently active asset as a SelectValue\n *\n * @returns The SelectValue object for the active asset or undefined if no asset is active\n */\n getActiveSelectValue(): SelectValue | undefined {\n const activeIds = this.getActiveAssetIds();\n if (activeIds.length > 0) {\n const asset = this.assets.find(({ id }) => id === activeIds[0]);\n if (asset) {\n // Get translated label if translation callback is available\n const label = this.translateLabel\n ? this.translateLabel(asset.id, asset.label?.en || '', '')\n : asset.label?.en || '';\n\n return {\n id: asset.id,\n label,\n thumbUri: asset.meta?.thumbUri\n };\n }\n }\n return undefined;\n }\n\n /**\n * Get the translated label for an asset by its ID\n *\n * @param assetId - The ID of the asset\n * @returns The translated label or undefined if asset not found\n */\n getTranslatedLabel(assetId: string): string | undefined {\n const asset = this.assets.find(({ id }) => id === assetId);\n if (asset) {\n return this.translateLabel\n ? this.translateLabel(asset.id, asset.label?.en || '', '')\n : asset.label?.en || '';\n }\n return undefined;\n }\n\n /**\n * Get an asset by its ID\n */\n getAsset(id: string): AssetDefinition | undefined {\n return this.assets.find((asset) => asset.id === id);\n }\n\n /**\n * Set an asset as active by its ID\n *\n * @param assetId - The ID of the asset to mark as active\n */\n setAssetActive(assetId: string): void {\n this.activeAssetIds.add(assetId);\n }\n\n /**\n * Get all active asset IDs\n *\n * @returns Array of active asset IDs\n */\n getActiveAssetIds(): string[] {\n return Array.from(this.activeAssetIds);\n }\n\n /**\n * Set multiple assets as active by their IDs\n *\n * @param assetIds - Array of asset IDs to mark as active\n */\n setAssetsActive(assetIds: string[]): void {\n assetIds.forEach((id) => this.activeAssetIds.add(id));\n }\n\n /**\n * Set an asset as inactive by its ID\n *\n * @param assetId - The ID of the asset to mark as inactive\n */\n setAssetInactive(assetId: string): void {\n this.activeAssetIds.delete(assetId);\n }\n\n /**\n * Clear all active assets\n */\n clearActiveAssets(): void {\n this.activeAssetIds.clear();\n }\n\n /**\n * Check if an asset is marked as active\n *\n * @param assetId - The ID of the asset to check\n * @returns True if the asset is active, false otherwise\n */\n isAssetActive(assetId: string): boolean {\n return this.activeAssetIds.has(assetId);\n }\n\n /**\n * Add an asset to this source\n *\n * @param asset - The asset definition to add\n */\n addAsset(asset: AssetDefinition): void {\n // Check if asset with this ID already exists\n const existingIndex = this.assets.findIndex((a) => a.id === asset.id);\n if (existingIndex >= 0) {\n // Replace existing asset\n this.assets[existingIndex] = asset;\n } else {\n // Add new asset\n this.assets.push(asset);\n }\n }\n\n /**\n * Remove an asset from this source\n *\n * @param assetId - The ID of the asset to remove\n */\n removeAsset(assetId: string): void {\n const index = this.assets.findIndex((asset) => asset.id === assetId);\n if (index !== -1) {\n this.assets.splice(index, 1);\n this.activeAssetIds.delete(assetId);\n }\n }\n\n /**\n * Get all available groups from the assets\n *\n * @returns Array of unique group names\n */\n async getGroups(): Promise<string[]> {\n const groups = new Set<string>();\n this.assets.forEach((asset) => {\n if (asset.groups) {\n asset.groups.forEach((group) => groups.add(group));\n }\n });\n return Array.from(groups);\n }\n\n /**\n * Returns the supported MIME types for this asset source\n *\n * @returns Array of supported MIME types\n */\n getSupportedMimeTypes(): string[] {\n return [\n 'image/jpeg',\n 'image/png',\n 'image/svg+xml',\n 'image/webp',\n 'video/mp4',\n 'audio/mpeg'\n ];\n }\n}\n\n/**\n * Helper function to create a CustomAssetSource instance\n *\n * @param id - The unique identifier for this asset source\n * @param assets - Array of asset definitions or SelectValue objects to include in this source\n * @param options - Optional configuration for the asset source\n * @returns A new CustomAssetSource instance\n */\nexport function createCustomAssetSource(\n id: string,\n assets: (AssetDefinition | SelectValue)[] = [],\n options?: CustomAssetSourceOptions\n): CustomAssetSource {\n return new CustomAssetSource(id, assets, options);\n}\n\nexport default CustomAssetSource;\n", "/* eslint-disable no-console */\nimport {\n CreativeEngine,\n type AssetDefinition,\n type AssetQueryData,\n type AssetResult,\n type AssetSource,\n type AssetsQueryResult\n} from '@cesdk/cesdk-js';\n\ntype BlobEntry = {\n id: string;\n blob: Blob;\n};\n\n// Asset definition with meta containing insertedAt timestamp\ntype AssetEntryWithMeta = AssetDefinition;\n\n/**\n * IndexedDBAssetSource implements the AssetSource interface using IndexedDB as the storage backend.\n */\nexport class IndexedDBAssetSource implements AssetSource {\n /** The unique id of the API */\n public readonly id: string;\n\n public readonly engine: CreativeEngine;\n\n private readonly dbName: string;\n\n private readonly dbVersion: number;\n\n private readonly assetStoreName: string = 'assets';\n\n private readonly blobStoreName: string = 'blobs';\n\n private db: IDBDatabase | null = null;\n\n /**\n * Creates a new IndexedDBAssetSource\n *\n * @param id - The unique identifier for this asset source\n * @param {Object} [options] - Optional configuration options.\n * @param {string} [options.dbName] - The name of the database.\n * @param {number} [options.dbVersion] - The version number of the database.\n */\n constructor(\n id: string,\n engine: CreativeEngine,\n options?: {\n dbName?: string;\n dbVersion?: number;\n }\n ) {\n this.id = id;\n this.engine = engine;\n this.dbName = options?.dbName ?? `ly.img.assetSource/${id}`;\n this.dbVersion = options?.dbVersion ?? 1;\n }\n\n /**\n * Initialize the database connection and create object stores if needed\n */\n public async initialize(): Promise<void> {\n if (this.db) {\n return;\n }\n\n return new Promise((resolve, reject) => {\n const request = indexedDB.open(this.dbName, this.dbVersion);\n\n request.onerror = (event) => {\n reject(\n new Error(\n `Failed to open IndexedDB: ${(event.target as IDBRequest).error}`\n )\n );\n };\n\n request.onupgradeneeded = (event) => {\n const db = (event.target as IDBOpenDBRequest).result;\n\n // Create asset store if it doesn't exist\n if (!db.objectStoreNames.contains(this.assetStoreName)) {\n db.createObjectStore(this.assetStoreName, {\n keyPath: 'id'\n });\n }\n // Create blob store if it doesn't exist\n if (!db.objectStoreNames.contains(this.blobStoreName)) {\n db.createObjectStore(this.blobStoreName, {\n keyPath: 'id'\n });\n }\n };\n\n request.onsuccess = (event) => {\n this.db = (event.target as IDBOpenDBRequest).result;\n resolve();\n };\n });\n }\n\n /**\n * Close the database connection\n */\n public close(): void {\n if (this.db) {\n this.db.close();\n this.db = null;\n }\n }\n\n /**\n * Find all assets for the given type and the provided query data.\n *\n * @param queryData - The query parameters for filtering assets\n * @param insertionSortOrder - Optional parameter to sort by insertion time: 'asc' for oldest first, 'desc' for newest first (default)\n * @returns A promise that resolves to the query results or undefined if there was an error\n */\n public async findAssets(\n queryData: AssetQueryData\n ): Promise<AssetsQueryResult | undefined> {\n await this.initialize();\n\n if (!this.db) {\n throw new Error('Database not initialized');\n }\n\n try {\n // Get all assets from the store with specified insertion order\n const assetDefinitions = await this.getAllAssets('asc');\n\n let assetResults = assetDefinitions.reduce((acc, assetDefinition) => {\n const locale = queryData.locale ?? 'en';\n let label = '';\n let tags: string[] = [];\n\n // Handle localized label if available\n if (\n assetDefinition.label != null &&\n typeof assetDefinition.label === 'object' &&\n assetDefinition.label[locale]\n ) {\n label = assetDefinition.label[locale];\n }\n\n // Handle localized tags if available\n if (\n assetDefinition.tags != null &&\n typeof assetDefinition.tags === 'object' &&\n assetDefinition.tags[locale]\n ) {\n tags = assetDefinition.tags[locale];\n }\n\n const result: AssetResult = {\n ...assetDefinition,\n label,\n tags\n };\n\n if (this.filterAsset(result, queryData)) {\n acc.push(result);\n }\n\n return acc;\n }, [] as AssetResult[]);\n\n assetResults = await this.restoreBlobUrls(assetResults);\n\n // Apply sorting\n assetResults = this.sortAssets(assetResults, queryData);\n\n // Apply pagination\n const { page, perPage } = queryData;\n const startIndex = page * perPage;\n const endIndex = startIndex + perPage;\n const paginatedAssets = assetResults.slice(startIndex, endIndex);\n\n // Determine if there's a next page\n const nextPage = endIndex < assetResults.length ? page + 1 : undefined;\n\n const result = {\n assets: paginatedAssets,\n currentPage: page,\n nextPage,\n total: assetResults.length\n };\n\n return result;\n } catch (error) {\n console.error('Error finding assets:', error);\n return undefined;\n }\n }\n\n public async getGroups(): Promise<string[]> {\n await this.initialize();\n\n if (!this.db) {\n throw new Error('Database not initialized');\n }\n\n return new Promise((resolve, reject) => {\n const transaction = this.db!.transaction(this.assetStoreName, 'readonly');\n const store = transaction.objectStore(this.assetStoreName);\n const request = store.getAll();\n\n request.onsuccess = () => {\n const allGroups = new Set<string>();\n\n // Extract all groups from all assets\n (request.result as AssetResult[]).forEach((asset) => {\n if (asset.groups && Array.isArray(asset.groups)) {\n asset.groups.forEach((group) => allGroups.add(group));\n }\n });\n\n const uniqueGroups = [...allGroups];\n resolve(uniqueGroups);\n };\n\n request.onerror = () => {\n reject(new Error(`Failed to get groups: ${request.error}`));\n };\n });\n }\n\n /**\n * Adds the given asset to this source. Part of the AssetSource interface.\n *\n * @param asset - The asset definition to add\n */\n public addAsset(asset: AssetDefinition): void {\n this.initialize()\n .then(async () => {\n if (!this.db) {\n throw new Error('Database not initialized');\n }\n\n const transaction = this.db.transaction(\n this.assetStoreName,\n 'readwrite'\n );\n const assetStore = transaction.objectStore(this.assetStoreName);\n\n const blobsToStore = new Set<string>();\n processBlobUrls(asset, (value) => {\n blobsToStore.add(value);\n });\n\n setTimeout(() => {\n this.storeBlobUrls([...blobsToStore]);\n });\n\n // Ensure asset has meta object with insertedAt timestamp\n const assetWithMeta: AssetEntryWithMeta = {\n ...asset,\n meta: {\n ...asset.meta,\n insertedAt: asset.meta?.insertedAt || Date.now()\n }\n };\n\n // Store the asset in the database\n assetStore.put(assetWithMeta);\n\n transaction.onerror = () => {\n console.error(`Failed to add asset: ${transaction.error}`);\n };\n })\n .catch((error) => {\n console.error('Error initializing database:', error);\n });\n }\n\n /**\n * Removes the given asset from this source. Part of the AssetSource interface.\n *\n * @param assetId - The ID of the asset to remove\n */\n public async removeAsset(assetId: string): Promise<void> {\n const asset = await this.getAsset(assetId);\n\n return this.initialize()\n .then(() => {\n if (!this.db) {\n throw new Error('Database not initialized');\n }\n\n const transaction = this.db.transaction(\n this.assetStoreName,\n 'readwrite'\n );\n const store = transaction.objectStore(this.assetStoreName);\n store.delete(assetId);\n\n transaction.oncomplete = () => {\n processBlobUrls(asset, (value) => {\n this.removeBlob(value);\n });\n this.engine.asset.assetSourceContentsChanged(this.id);\n };\n\n transaction.onerror = () => {\n console.error(`Failed to remove asset: ${transaction.error}`);\n };\n })\n .catch((error) => {\n console.error('Error initializing database:', error);\n });\n }\n\n /**\n * Removes the given asset from this source. Part of the AssetSource interface.\n *\n * @param assetId - The ID of the asset to remove\n */\n public async removeBlob(blobId: string): Promise<void> {\n return this.initialize()\n .then(() => {\n if (!this.db) {\n throw new Error('Database not initialized');\n }\n\n const transaction = this.db.transaction(\n this.blobStoreName,\n 'readwrite'\n );\n const store = transaction.objectStore(this.blobStoreName);\n store.delete(blobId);\n\n transaction.onerror = () => {\n console.error(`Failed to remove blob: ${transaction.error}`);\n };\n })\n .catch((error) => {\n console.error('Error initializing database:', error);\n });\n }\n\n /**\n * Get all assets from the database sorted by insertion order (newest to oldest)\n *\n * @param sortOrder - Optional parameter to specify sort order: 'asc' for oldest first, 'desc' for newest first (default)\n * @returns A promise that resolves to an array of all assets\n */\n private async getAllAssets(\n sortOrder: 'asc' | 'desc' = 'desc'\n ): Promise<AssetDefinition[]> {\n return new Promise((resolve, reject) => {\n const transaction = this.db!.transaction(this.assetStoreName, 'readonly');\n const store = transaction.objectStore(this.assetStoreName);\n const request = store.getAll();\n\n request.onsuccess = () => {\n const assets = request.result as AssetEntryWithMeta[];\n\n // Sort by insertion timestamp\n assets.sort((a, b) => {\n // Default to current time if insertedAt is missing (for backward compatibility)\n // First check in meta.insertedAt, then fallback to legacy _insertedAt for backward compatibility\n const timeA =\n a.meta?.insertedAt || (a as any)._insertedAt || Date.now();\n const timeB =\n b.meta?.insertedAt || (b as any)._insertedAt || Date.now();\n\n // Sort based on requested order\n return sortOrder === 'asc'\n ? timeA - timeB // oldest first\n : timeB - timeA; // newest first (default)\n });\n\n resolve(assets);\n };\n\n request.onerror = () => {\n reject(new Error(`Failed to get assets: ${request.error}`));\n };\n });\n }\n\n // Retrieve a blob by ID\n async getAsset(id: string): Promise<AssetResult | undefined> {\n return new Promise((resolve, reject) => {\n const transaction = this.db!.transaction(this.assetStoreName, 'readonly');\n const store = transaction.objectStore(this.assetStoreName);\n const request = store.get(id);\n\n request.onsuccess = () => {\n resolve(request.result as AssetResult);\n };\n\n request.onerror = () => {\n reject(new Error(`Failed to get blob: ${request.error}`));\n };\n });\n }\n\n // Retrieve a blob by ID\n async getBlob(id: string): Promise<BlobEntry | undefined> {\n return new Promise((resolve, reject) => {\n const transaction = this.db!.transaction(this.blobStoreName, 'readonly');\n const store = transaction.objectStore(this.blobStoreName);\n const request = store.get(id);\n\n request.onsuccess = () => {\n resolve(request.result as BlobEntry);\n };\n\n request.onerror = () => {\n reject(new Error(`Failed to get blob: ${request.error}`));\n };\n });\n }\n\n async createBlobUrlFromStore(blobUrl: string): Promise<string> {\n const blobEntry = await this.getBlob(blobUrl);\n if (blobEntry != null) {\n return URL.createObjectURL(blobEntry.blob);\n }\n return blobUrl;\n }\n\n async storeBlobUrls(urls: string[]): Promise<void> {\n const blobsToStore: { [key: string]: Blob } = {};\n await Promise.all(\n urls.map(async (blobUrl) => {\n const blobResponse = await fetch(blobUrl);\n const blob = await blobResponse.blob();\n blobsToStore[blobUrl] = blob;\n })\n );\n\n return this.initialize()\n .then(async () => {\n if (!this.db) {\n throw new Error('Database not initialized');\n }\n\n const transaction = this.db.transaction(\n this.blobStoreName,\n 'readwrite'\n );\n const blobStore = transaction.objectStore(this.blobStoreName);\n\n // Store the asset in the database\n Object.entries(blobsToStore).forEach(([key, blob]) => {\n const asset: BlobEntry = { id: key, blob };\n blobStore.put(asset);\n });\n\n transaction.onerror = () => {\n console.error(`Failed to add blobs: ${transaction.error}`);\n };\n })\n .catch((error) => {\n console.error('Error initializing database:', error);\n });\n }\n\n async restoreBlobUrls(assets: AssetResult[]): Promise<AssetResult[]> {\n const blobReplaced: { [key: string]: string } = {};\n const blobUrls: Set<string> = new Set();\n processBlobUrls(assets, (value) => {\n blobUrls.add(value);\n });\n\n await Promise.all(\n [...blobUrls].map(async (value) => {\n const newUrl = await this.createBlobUrlFromStore(value);\n blobReplaced[value] = newUrl;\n })\n );\n\n return processBlobUrls(assets, (value) => {\n return blobReplaced[value] ?? value;\n });\n }\n\n /**\n * Returns if the given asset should be filtered based on query data\n *\n * @param asset - The asset to filter\n * @param queryData - The query parameters to filter by\n * @returns true if the asset should be included, false otherwise\n */\n private filterAsset(asset: AssetResult, queryData: AssetQueryData): boolean {\n const { query, tags, groups, excludeGroups } = queryData;\n\n // Filter by query string (search on label and tags)\n if (query && query.trim() !== '') {\n const lowerQuery = query.trim().toLowerCase().split(' ');\n\n const lowerLabel = asset.label?.toLowerCase() ?? '';\n const lowerTags = asset.tags?.map((tag) => tag.toLowerCase()) ?? [];\n\n const matchLabelOrTag = lowerQuery.every((word) => {\n return (\n lowerLabel.includes(word) ||\n lowerTags.some((tag) => tag.includes(word))\n );\n });\n\n if (!matchLabelOrTag) {\n return false;\n }\n }\n\n // Filter by exact tags if provided by the query\n if (tags) {\n const tagList = Array.isArray(tags) ? tags : [tags];\n if (\n tagList.length > 0 &&\n (!asset.tags || !tagList.every((tag) => asset.tags?.includes(tag)))\n ) {\n return false;\n }\n }\n\n // Filter by groups\n if (groups && groups.length > 0) {\n if (\n !asset.groups ||\n !groups.some((group) => asset.groups?.includes(group))\n ) {\n return false;\n }\n }\n\n // Filter by excluded groups\n if (excludeGroups && excludeGroups.length > 0) {\n if (\n asset.groups &&\n asset.groups.some((group) => excludeGroups.includes(group))\n ) {\n return false;\n }\n }\n\n return true;\n }\n\n /**\n * Sort assets based on query data\n *\n * @param assets - The assets to sort\n * @param queryData - The query parameters with sorting information\n * @returns The sorted assets\n */\n private sortAssets(\n assets: AssetResult[],\n queryData: AssetQueryData\n ): AssetResult[] {\n const { sortingOrder, sortKey, sortActiveFirst } = queryData;\n\n // Clone the array to avoid modifying the original\n const sortedAssets = [...assets];\n\n // If no sorting order specified or set to 'None', return the current order\n if (!sortingOrder || sortingOrder === 'None') {\n return sortedAssets;\n }\n\n // Sort by the specified key\n if (sortKey) {\n sortedAssets.sort((a, b) => {\n let valueA;\n let valueB;\n\n if (sortKey === 'id') {\n valueA = a.id;\n valueB = b.id;\n } else {\n // Handle metadata sorting (assuming metadata is stored in a 'metadata' field)\n valueA = a.meta?.[sortKey] ?? null;\n valueB = b.meta?.[sortKey] ?? null;\n }\n\n // Handle null/undefined values\n if (valueA === null || valueA === undefined)\n return sortingOrder === 'Ascending' ? -1 : 1;\n if (valueB === null || valueB === undefined)\n return sortingOrder === 'Ascending' ? 1 : -1;\n\n // Compare values based on sorting order\n if (typeof valueA === 'string' && typeof valueB === 'string') {\n return sortingOrder === 'Ascending'\n ? valueA.localeCompare(valueB)\n : valueB.localeCompare(valueA);\n } else {\n return sortingOrder === 'Ascending'\n ? valueA < valueB\n ? -1\n : valueA > valueB\n ? 1\n : 0\n : valueA > valueB\n ? -1\n : valueA < valueB\n ? 1\n : 0;\n }\n });\n } else if (sortingOrder === 'Descending') {\n // If no sort key is specified, and sorting order set\n // to Descending, just reverse original order\n sortedAssets.reverse();\n }\n\n // Sort by active first if requested\n if (sortActiveFirst) {\n sortedAssets.sort((a, b) => {\n if (a.active && !b.active) return -1;\n if (!a.active && b.active) return 1;\n return 0;\n });\n }\n\n return sortedAssets;\n }\n}\n\n/**\n * Goes through an object and calls a callback for every string value that starts with 'blob:'\n * The callback can return a string to replace the original value\n * @param obj The object to traverse\n * @param callback Function to call when a blob URL is found, can return a replacement value\n * @param path Current path in the object (used for recursion)\n * @returns The modified object (or the original if no replacements were made)\n */\nfunction processBlobUrls<T>(\n obj: T,\n callback: (value: string, path: string) => string | void,\n path: string = ''\n): T {\n // Return the object as is if it's null or not an object\n if (obj === null || typeof obj !== 'object') {\n return obj;\n }\n\n // Handle arrays\n if (Array.isArray(obj)) {\n for (let index = 0; index < obj.length; index++) {\n const currentPath = path ? `${path}[${index}]` : `[${index}]`;\n\n if (typeof obj[index] === 'string' && obj[index].startsWith('blob:')) {\n const replacement = callback(obj[index], currentPath);\n if (typeof replacement === 'string') {\n obj[index] = replacement;\n }\n } else {\n obj[index] = processBlobUrls(obj[index], callback, currentPath);\n }\n }\n return obj;\n }\n\n // Handle regular objects\n for (const key in obj) {\n if (Object.prototype.hasOwnProperty.call(obj, key)) {\n const value = obj[key];\n const currentPath = path ? `${path}.${key}` : key;\n\n if (typeof value === 'string' && value.startsWith('blob:')) {\n const replacement = callback(value, currentPath);\n if (typeof replacement === 'string') {\n // @ts-ignore\n obj[key] = replacement;\n }\n } else {\n obj[key] = processBlobUrls(value, callback, currentPath);\n }\n }\n }\n\n return obj;\n}\n\nexport default IndexedDBAssetSource;\n", "/* eslint-disable @typescript-eslint/no-unused-vars */\nimport type {\n AssetDefinition,\n AssetQueryData,\n AssetResult,\n AssetSource,\n AssetsQueryResult\n} from '@cesdk/cesdk-js';\nimport CreativeEditorSDK from '@cesdk/cesdk-js';\n\n/**\n * AggregatedAssetSource implements the AssetSource interface by aggregating\n * multiple asset sources via the cesdk API and combining their results.\n *\n * This asset source is read-only - it does not support adding or removing assets.\n */\nexport class AggregatedAssetSource implements AssetSource {\n /** The unique id of the asset source */\n public readonly id: string;\n\n /** The Creative Editor SDK instance */\n private cesdk: CreativeEditorSDK;\n\n /** The IDs of asset sources to aggregate */\n private assetSourceIds: string[];\n\n /**\n * Creates a new AggregatedAssetSource\n *\n * @param id - The unique identifier for this asset source\n * @param cesdk - The Creative Editor SDK instance\n * @param assetSourceIds - The IDs of asset sources to aggregate\n */\n constructor(id: string, cesdk: CreativeEditorSDK, assetSourceIds: string[]) {\n this.id = id;\n this.cesdk = cesdk;\n this.assetSourceIds = assetSourceIds;\n }\n\n /**\n * Find assets across all aggregated sources based on the provided query data\n * Results are sorted by the insertedAt timestamp in meta field\n *\n * @param queryData - Query parameters to filter and sort assets\n * @returns Promise with the query results\n */\n async findAssets(\n queryData: AssetQueryData\n ): Promise<AssetsQueryResult | undefined> {\n try {\n // Query all asset sources via cesdk\n const queryPromises = this.assetSourceIds.map((sourceId) =>\n this.cesdk.engine.asset.findAssets(sourceId, {\n ...queryData,\n // Increase page size to get all assets from each source\n // We'll handle pagination after merging\n perPage: 9999,\n page: 0\n })\n );\n\n // Wait for all queries to complete\n const results = await Promise.all(queryPromises);\n\n // Combine all assets from all sources\n let allAssets: AssetResult[] = [];\n results.forEach((result) => {\n if (result?.assets) {\n allAssets = allAssets.concat(result.assets);\n }\n });\n\n // Sort by insertedAt timestamp\n allAssets.sort((a, b) => {\n const timeA = (a.meta?.insertedAt as number) || 0;\n const timeB = (b.meta?.insertedAt as number) || 0;\n\n // Sort newest first (descending)\n return timeB - timeA;\n });\n\n // Apply pagination after merging\n const { page, perPage } = queryData;\n const startIndex = page * perPage;\n const endIndex = startIndex + perPage;\n const paginatedAssets = allAssets.slice(startIndex, endIndex);\n\n // Calculate if there is a next page\n const nextPage = endIndex < allAssets.length ? page + 1 : undefined;\n\n return {\n assets: paginatedAssets,\n currentPage: page,\n nextPage,\n total: allAssets.length\n };\n } catch (error) {\n // eslint-disable-next-line no-console\n console.error('Error finding assets:', error);\n return undefined;\n }\n }\n\n /**\n * Retrieves all groups from all aggregated asset sources using cesdk\n * @returns Promise with an array of unique group names\n */\n async getGroups(): Promise<string[]> {\n const groupPromises = this.assetSourceIds.map((sourceId) =>\n this.cesdk.engine.asset.getGroups(sourceId)\n );\n\n const groupArrays = await Promise.all(groupPromises);\n\n // Combine and deduplicate groups\n const uniqueGroups = new Set<string>();\n groupArrays.forEach((groups) => {\n groups.forEach((group) => uniqueGroups.add(group));\n });\n\n return Array.from(uniqueGroups);\n }\n\n /**\n * This operation is not supported in AggregatedAssetSource\n * @throws Error - This method is not supported\n */\n addAsset(_asset: AssetDefinition): void {\n throw new Error('AggregatedAssetSource does not support adding assets');\n }\n\n /**\n * This operation is not supported in AggregatedAssetSource\n * @throws Error - This method is not supported\n */\n removeAsset(_assetId: string): void {\n throw new Error('AggregatedAssetSource does not support removing assets');\n }\n}\n\n/**\n * Helper function to create an AggregatedAssetSource instance\n *\n * @param id - The unique identifier for this asset source\n * @param cesdk - The Creative Editor SDK instance\n * @param assetSourceIds - The IDs of asset sources to aggregate\n * @returns A new AggregatedAssetSource instance\n */\nexport function createAggregatedAssetSource(\n id: string,\n cesdk: CreativeEditorSDK,\n assetSourceIds: string[]\n): AggregatedAssetSource {\n return new AggregatedAssetSource(id, cesdk, assetSourceIds);\n}\n\nexport default AggregatedAssetSource;\n", "import { type CreativeEngine } from '@cesdk/cesdk-js';\n\nclass Metadata<V> {\n engine: CreativeEngine;\n\n key: string;\n\n constructor(engine: CreativeEngine, key: string) {\n this.engine = engine;\n this.key = key;\n }\n\n hasData(blockId: number): boolean {\n return (\n this.engine.block.isValid(blockId) &&\n this.engine.block.hasMetadata(blockId, this.key)\n );\n }\n\n get(blockId: number): V | undefined {\n if (this.hasData(blockId)) {\n return JSON.parse(this.engine.block.getMetadata(blockId, this.key));\n }\n return undefined;\n }\n\n set(blockId: number, value: V) {\n this.engine.block.setMetadata(blockId, this.key, JSON.stringify(value));\n }\n\n clear(blockId: number) {\n if (this.engine.block.hasMetadata(blockId, this.key)) {\n this.engine.block.removeMetadata(blockId, this.key);\n }\n }\n}\n\nexport default Metadata;\n", "/** Detect free variable `global` from Node.js. */\nvar freeGlobal = typeof global == 'object' && global && global.Object === Object && global;\n\nexport default freeGlobal;\n", "import freeGlobal from './_freeGlobal.js';\n\n/** Detect free variable `self`. */\nvar freeSelf = typeof self == 'object' && self && self.Object === Object && self;\n\n/** Used as a reference to the global object. */\nvar root = freeGlobal || freeSelf || Function('return this')();\n\nexport default root;\n", "import root from './_root.js';\n\n/** Built-in value references. */\nvar Symbol = root.Symbol;\n\nexport default Symbol;\n", "import Symbol from './_Symbol.js';\n\n/** Used for built-in method references. */\nvar objectProto = Object.prototype;\n\n/** Used to check objects for own properties. */\nvar hasOwnProperty = objectProto.hasOwnProperty;\n\n/**\n * Used to resolve the\n * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)\n * of values.\n */\nvar nativeObjectToString = objectProto.toString;\n\n/** Built-in value references. */\nvar symToStringTag = Symbol ? Symbol.toStringTag : undefined;\n\n/**\n * A specialized version of `baseGetTag` which ignores `Symbol.toStringTag` values.\n *\n * @private\n * @param {*} value The value to query.\n * @returns {string} Returns the raw `toStringTag`.\n */\nfunction getRawTag(value) {\n var isOwn = hasOwnProperty.call(value, symToStringTag),\n tag = value[symToStringTag];\n\n try {\n value[symToStringTag] = undefined;\n var unmasked = true;\n } catch (e) {}\n\n var result = nativeObjectToString.call(value);\n if (unmasked) {\n if (isOwn) {\n value[symToStringTag] = tag;\n } else {\n delete value[symToStringTag];\n }\n }\n return result;\n}\n\nexport default getRawTag;\n", "/** Used for built-in method references. */\nvar objectProto = Object.prototype;\n\n/**\n * Used to resolve the\n * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)\n * of values.\n */\nvar nativeObjectToString = objectProto.toString;\n\n/**\n * Converts `value` to a string using `Object.prototype.toString`.\n *\n * @private\n * @param {*} value The value to convert.\n * @returns {string} Returns the converted string.\n */\nfunction objectToString(value) {\n return nativeObjectToString.call(value);\n}\n\nexport default objectToString;\n", "import Symbol from './_Symbol.js';\nimport getRawTag from './_getRawTag.js';\nimport objectToString from './_objectToString.js';\n\n/** `Object#toString` result references. */\nvar nullTag = '[object Null]',\n undefinedTag = '[object Undefined]';\n\n/** Built-in value references. */\nvar symToStringTag = Symbol ? Symbol.toStringTag : undefined;\n\n/**\n * The base implementation of `getTag` without fallbacks for buggy environments.\n *\n * @private\n * @param {*} value The value to query.\n * @returns {string} Returns the `toStringTag`.\n */\nfunction baseGetTag(value) {\n if (value == null) {\n return value === undefined ? undefinedTag : nullTag;\n }\n return (symToStringTag && symToStringTag in Object(value))\n ? getRawTag(value)\n : objectToString(value);\n}\n\nexport default baseGetTag;\n", "/**\n * Checks if `value` is object-like. A value is object-like if it's not `null`\n * and has a `typeof` result of \"object\".\n *\n * @static\n * @memberOf _\n * @since 4.0.0\n * @category Lang\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is object-like, else `false`.\n * @example\n *\n * _.isObjectLike({});\n * // => true\n *\n * _.isObjectLike([1, 2, 3]);\n * // => true\n *\n * _.isObjectLike(_.noop);\n * // => false\n *\n * _.isObjectLike(null);\n * // => false\n */\nfunction isObjectLike(value) {\n return value != null && typeof value == 'object';\n}\n\nexport default isObjectLike;\n", "/**\n * Checks if `value` is classified as an `Array` object.\n *\n * @static\n * @memberOf _\n * @since 0.1.0\n * @category Lang\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is an array, else `false`.\n * @example\n *\n * _.isArray([1, 2, 3]);\n * // => true\n *\n * _.isArray(document.body.children);\n * // => false\n *\n * _.isArray('abc');\n * // => false\n *\n * _.isArray(_.noop);\n * // => false\n */\nvar isArray = Array.isArray;\n\nexport default isArray;\n", "/**\n * Checks if `value` is the\n * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types)\n * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)\n *\n * @static\n * @memberOf _\n * @since 0.1.0\n * @category Lang\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is an object, else `false`.\n * @example\n *\n * _.isObject({});\n * // => true\n *\n * _.isObject([1, 2, 3]);\n * // => true\n *\n * _.isObject(_.noop);\n * // => true\n *\n * _.isObject(null);\n * // => false\n */\nfunction isObject(value) {\n var type = typeof value;\n return value != null && (type == 'object' || type == 'function');\n}\n\nexport default isObject;\n", "import baseGetTag from './_baseGetTag.js';\nimport isObject from './isObject.js';\n\n/** `Object#toString` result references. */\nvar asyncTag = '[object AsyncFunction]',\n funcTag = '[object Function]',\n genTag = '[object GeneratorFunction]',\n proxyTag = '[object Proxy]';\n\n/**\n * Checks if `value` is classified as a `Function` object.\n *\n * @static\n * @memberOf _\n * @since 0.1.0\n * @category Lang\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is a function, else `false`.\n * @example\n *\n * _.isFunction(_);\n * // => true\n *\n * _.isFunction(/abc/);\n * // => false\n */\nfunction isFunction(value) {\n if (!isObject(value)) {\n return false;\n }\n // The use of `Object#toString` avoids issues with the `typeof` operator\n // in Safari 9 which returns 'object' for typed arrays and other constructors.\n var tag = baseGetTag(value);\n return tag == funcTag || tag == genTag || tag == asyncTag || tag == proxyTag;\n}\n\nexport default isFunction;\n", "import root from './_root.js';\n\n/** Used to detect overreaching core-js shims. */\nvar coreJsData = root['__core-js_shared__'];\n\nexport default coreJsData;\n", "import coreJsData from './_coreJsData.js';\n\n/** Used to detect methods masquerading as native. */\nvar maskSrcKey = (function() {\n var uid = /[^.]+$/.exec(coreJsData && coreJsData.keys && coreJsData.keys.IE_PROTO || '');\n return uid ? ('Symbol(src)_1.' + uid) : '';\n}());\n\n/**\n * Checks if `func` has its source masked.\n *\n * @private\n * @param {Function} func The function to check.\n * @returns {boolean} Returns `true` if `func` is masked, else `false`.\n */\nfunction isMasked(func) {\n return !!maskSrcKey && (maskSrcKey in func);\n}\n\nexport default isMasked;\n", "/** Used for built-in method references. */\nvar funcProto = Function.prototype;\n\n/** Used to resolve the decompiled source of functions. */\nvar funcToString = funcProto.toString;\n\n/**\n * Converts `func` to its source code.\n *\n * @private\n * @param {Function} func The function to convert.\n * @returns {string} Returns the source code.\n */\nfunction toSource(func) {\n if (func != null) {\n try {\n return funcToString.call(func);\n } catch (e) {}\n try {\n return (func + '');\n } catch (e) {}\n }\n return '';\n}\n\nexport default toSource;\n", "import isFunction from './isFunction.js';\nimport isMasked from './_isMasked.js';\nimport isObject from './isObject.js';\nimport toSource from './_toSource.js';\n\n/**\n * Used to match `RegExp`\n * [syntax characters](http://ecma-international.org/ecma-262/7.0/#sec-patterns).\n */\nvar reRegExpChar = /[\\\\^$.*+?()[\\]{}|]/g;\n\n/** Used to detect host constructors (Safari). */\nvar reIsHostCtor = /^\\[object .+?Constructor\\]$/;\n\n/** Used for built-in method references. */\nvar funcProto = Function.prototype,\n objectProto = Object.prototype;\n\n/** Used to resolve the decompiled source of functions. */\nvar funcToString = funcProto.toString;\n\n/** Used to check objects for own properties. */\nvar hasOwnProperty = objectProto.hasOwnProperty;\n\n/** Used to detect if a method is native. */\nvar reIsNative = RegExp('^' +\n funcToString.call(hasOwnProperty).replace(reRegExpChar, '\\\\$&')\n .replace(/hasOwnProperty|(function).*?(?=\\\\\\()| for .+?(?=\\\\\\])/g, '$1.*?') + '$'\n);\n\n/**\n * The base implementation of `_.isNative` without bad shim checks.\n *\n * @private\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is a native function,\n * else `false`.\n */\nfunction baseIsNative(value) {\n if (!isObject(value) || isMasked(value)) {\n return false;\n }\n var pattern = isFunction(value) ? reIsNative : reIsHostCtor;\n return pattern.test(toSource(value));\n}\n\nexport default baseIsNative;\n", "/**\n * Gets the value at `key` of `object`.\n *\n * @private\n * @param {Object} [object] The object to query.\n * @param {string} key The key of the property to get.\n * @returns {*} Returns the property value.\n */\nfunction getValue(object, key) {\n return object == null ? undefined : object[key];\n}\n\nexport default getValue;\n", "import baseIsNative from './_baseIsNative.js';\nimport getValue from './_getValue.js';\n\n/**\n * Gets the native function at `key` of `object`.\n *\n * @private\n * @param {Object} object The object to query.\n * @param {string} key The key of the method to get.\n * @returns {*} Returns the function if it's native, else `undefined`.\n */\nfunction getNative(object, key) {\n var value = getValue(object, key);\n return baseIsNative(value) ? value : undefined;\n}\n\nexport default getNative;\n", "import getNative from './_getNative.js';\nimport root from './_root.js';\n\n/* Built-in method references that are verified to be native. */\nvar WeakMap = getNative(root, 'WeakMap');\n\nexport default WeakMap;\n", "/** Used as references for various `Number` constants. */\nvar MAX_SAFE_INTEGER = 9007199254740991;\n\n/** Used to detect unsigned integer values. */\nvar reIsUint = /^(?:0|[1-9]\\d*)$/;\n\n/**\n * Checks if `value` is a valid array-like index.\n *\n * @private\n * @param {*} value The value to check.\n * @param {number} [length=MAX_SAFE_INTEGER] The upper bounds of a valid index.\n * @returns {boolean} Returns `true` if `value` is a valid index, else `false`.\n */\nfunction isIndex(value, length) {\n var type = typeof value;\n length = length == null ? MAX_SAFE_INTEGER : length;\n\n return !!length &&\n (type == 'number' ||\n (type != 'symbol' && reIsUint.test(value))) &&\n (value > -1 && value % 1 == 0 && value < length);\n}\n\nexport default isIndex;\n", "/**\n * Performs a\n * [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero)\n * comparison between two values to determine if they are equivalent.\n *\n * @static\n * @memberOf _\n * @since 4.0.0\n * @category Lang\n * @param {*} value The value to compare.\n * @param {*} other The other value to compare.\n * @returns {boolean} Returns `true` if the values are equivalent, else `false`.\n * @example\n *\n * var object = { 'a': 1 };\n * var other = { 'a': 1 };\n *\n * _.eq(object, object);\n * // => true\n *\n * _.eq(object, other);\n * // => false\n *\n * _.eq('a', 'a');\n * // => true\n *\n * _.eq('a', Object('a'));\n * // => false\n *\n * _.eq(NaN, NaN);\n * // => true\n */\nfunction eq(value, other) {\n return value === other || (value !== value && other !== other);\n}\n\nexport default eq;\n", "/** Used as references for various `Number` constants. */\nvar MAX_SAFE_INTEGER = 9007199254740991;\n\n/**\n * Checks if `value` is a valid array-like length.\n *\n * **Note:** This method is loosely based on\n * [`ToLength`](http://ecma-international.org/ecma-262/7.0/#sec-tolength).\n *\n * @static\n * @memberOf _\n * @since 4.0.0\n * @category Lang\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is a valid length, else `false`.\n * @example\n *\n * _.isLength(3);\n * // => true\n *\n * _.isLength(Number.MIN_VALUE);\n * // => false\n *\n * _.isLength(Infinity);\n * // => false\n *\n * _.isLength('3');\n * // => false\n */\nfunction isLength(value) {\n return typeof value == 'number' &&\n value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;\n}\n\nexport default isLength;\n", "import isFunction from './isFunction.js';\nimport isLength from './isLength.js';\n\n/**\n * Checks if `value` is array-like. A value is considered array-like if it's\n * not a function and has a `value.length` that's an integer greater than or\n * equal to `0` and less than or equal to `Number.MAX_SAFE_INTEGER`.\n *\n * @static\n * @memberOf _\n * @since 4.0.0\n * @category Lang\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is array-like, else `false`.\n * @example\n *\n * _.isArrayLike([1, 2, 3]);\n * // => true\n *\n * _.isArrayLike(document.body.children);\n * // => true\n *\n * _.isArrayLike('abc');\n * // => true\n *\n * _.isArrayLike(_.noop);\n * // => false\n */\nfunction isArrayLike(value) {\n return value != null && isLength(value.length) && !isFunction(value);\n}\n\nexport default isArrayLike;\n", "/** Used for built-in method references. */\nvar objectProto = Object.prototype;\n\n/**\n * Checks if `value` is likely a prototype object.\n *\n * @private\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is a prototype, else `false`.\n */\nfunction isPrototype(value) {\n var Ctor = value && value.constructor,\n proto = (typeof Ctor == 'function' && Ctor.prototype) || objectProto;\n\n return value === proto;\n}\n\nexport default isPrototype;\n", "/**\n * The base implementation of `_.times` without support for iteratee shorthands\n * or max array length checks.\n *\n * @private\n * @param {number} n The number of times to invoke `iteratee`.\n * @param {Function} iteratee The function invoked per iteration.\n * @returns {Array} Returns the array of results.\n */\nfunction baseTimes(n, iteratee) {\n var index = -1,\n result = Array(n);\n\n while (++index < n) {\n result[index] = iteratee(index);\n }\n return result;\n}\n\nexport default baseTimes;\n", "import baseGetTag from './_baseGetTag.js';\nimport isObjectLike from './isObjectLike.js';\n\n/** `Object#toString` result references. */\nvar argsTag = '[object Arguments]';\n\n/**\n * The base implementation of `_.isArguments`.\n *\n * @private\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is an `arguments` object,\n */\nfunction baseIsArguments(value) {\n return isObjectLike(value) && baseGetTag(value) == argsTag;\n}\n\nexport default baseIsArguments;\n", "import baseIsArguments from './_baseIsArguments.js';\nimport isObjectLike from './isObjectLike.js';\n\n/** Used for built-in method references. */\nvar objectProto = Object.prototype;\n\n/** Used to check objects for own properties. */\nvar hasOwnProperty = objectProto.hasOwnProperty;\n\n/** Built-in value references. */\nvar propertyIsEnumerable = objectProto.propertyIsEnumerable;\n\n/**\n * Checks if `value` is likely an `arguments` object.\n *\n * @static\n * @memberOf _\n * @since 0.1.0\n * @category Lang\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is an `arguments` object,\n * else `false`.\n * @example\n *\n * _.isArguments(function() { return arguments; }());\n * // => true\n *\n * _.isArguments([1, 2, 3]);\n * // => false\n */\nvar isArguments = baseIsArguments(function() { return arguments; }()) ? baseIsArguments : function(value) {\n return isObjectLike(value) && hasOwnProperty.call(value, 'callee') &&\n !propertyIsEnumerable.call(value, 'callee');\n};\n\nexport default isArguments;\n", "/**\n * This method returns `false`.\n *\n * @static\n * @memberOf _\n * @since 4.13.0\n * @category Util\n * @returns {boolean} Returns `false`.\n * @example\n *\n * _.times(2, _.stubFalse);\n * // => [false, false]\n */\nfunction stubFalse() {\n return false;\n}\n\nexport default stubFalse;\n", "import root from './_root.js';\nimport stubFalse from './stubFalse.js';\n\n/** Detect free variable `exports`. */\nvar freeExports = typeof exports == 'object' && exports && !exports.nodeType && exports;\n\n/** Detect free variable `module`. */\nvar freeModule = freeExports && typeof module == 'object' && module && !module.nodeType && module;\n\n/** Detect the popular CommonJS extension `module.exports`. */\nvar moduleExports = freeModule && freeModule.exports === freeExports;\n\n/** Built-in value references. */\nvar Buffer = moduleExports ? root.Buffer : undefined;\n\n/* Built-in method references for those with the same name as other `lodash` methods. */\nvar nativeIsBuffer = Buffer ? Buffer.isBuffer : undefined;\n\n/**\n * Checks if `value` is a buffer.\n *\n * @static\n * @memberOf _\n * @since 4.3.0\n * @category Lang\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is a buffer, else `false`.\n * @example\n *\n * _.isBuffer(new Buffer(2));\n * // => true\n *\n * _.isBuffer(new Uint8Array(2));\n * // => false\n */\nvar isBuffer = nativeIsBuffer || stubFalse;\n\nexport default isBuffer;\n", "import baseGetTag from './_baseGetTag.js';\nimport isLength from './isLength.js';\nimport isObjectLike from './isObjectLike.js';\n\n/** `Object#toString` result references. */\nvar argsTag = '[object Arguments]',\n arrayTag = '[object Array]',\n boolTag = '[object Boolean]',\n dateTag = '[object Date]',\n errorTag = '[object Error]',\n funcTag = '[object Function]',\n mapTag = '[object Map]',\n numberTag = '[object Number]',\n objectTag = '[object Object]',\n regexpTag = '[object RegExp]',\n setTag = '[object Set]',\n stringTag = '[object String]',\n weakMapTag = '[object WeakMap]';\n\nvar arrayBufferTag = '[object ArrayBuffer]',\n dataViewTag = '[object DataView]',\n float32Tag = '[object Float32Array]',\n float64Tag = '[object Float64Array]',\n int8Tag = '[object Int8Array]',\n int16Tag = '[object Int16Array]',\n int32Tag = '[object Int32Array]',\n uint8Tag = '[object Uint8Array]',\n uint8ClampedTag = '[object Uint8ClampedArray]',\n uint16Tag = '[object Uint16Array]',\n uint32Tag = '[object Uint32Array]';\n\n/** Used to identify `toStringTag` values of typed arrays. */\nvar typedArrayTags = {};\ntypedArrayTags[float32Tag] = typedArrayTags[float64Tag] =\ntypedArrayTags[int8Tag] = typedArrayTags[int16Tag] =\ntypedArrayTags[int32Tag] = typedArrayTags[uint8Tag] =\ntypedArrayTags[uint8ClampedTag] = typedArrayTags[uint16Tag] =\ntypedArrayTags[uint32Tag] = true;\ntypedArrayTags[argsTag] = typedArrayTags[arrayTag] =\ntypedArrayTags[arrayBufferTag] = typedArrayTags[boolTag] =\ntypedArrayTags[dataViewTag] = typedArrayTags[dateTag] =\ntypedArrayTags[errorTag] = typedArrayTags[funcTag] =\ntypedArrayTags[mapTag] = typedArrayTags[numberTag] =\ntypedArrayTags[objectTag] = typedArrayTags[regexpTag] =\ntypedArrayTags[setTag] = typedArrayTags[stringTag] =\ntypedArrayTags[weakMapTag] = false;\n\n/**\n * The base implementation of `_.isTypedArray` without Node.js optimizations.\n *\n * @private\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is a typed array, else `false`.\n */\nfunction baseIsTypedArray(value) {\n return isObjectLike(value) &&\n isLength(value.length) && !!typedArrayTags[baseGetTag(value)];\n}\n\nexport default baseIsTypedArray;\n", "/**\n * The base implementation of `_.unary` without support for storing metadata.\n *\n * @private\n * @param {Function} func The function to cap arguments for.\n * @returns {Function} Returns the new capped function.\n */\nfunction baseUnary(func) {\n return function(value) {\n return func(value);\n };\n}\n\nexport default baseUnary;\n", "import freeGlobal from './_freeGlobal.js';\n\n/** Detect free variable `exports`. */\nvar freeExports = typeof exports == 'object' && exports && !exports.nodeType && exports;\n\n/** Detect free variable `module`. */\nvar freeModule = freeExports && typeof module == 'object' && module && !module.nodeType && module;\n\n/** Detect the popular CommonJS extension `module.exports`. */\nvar moduleExports = freeModule && freeModule.exports === freeExports;\n\n/** Detect free variable `process` from Node.js. */\nvar freeProcess = moduleExports && freeGlobal.process;\n\n/** Used to access faster Node.js helpers. */\nvar nodeUtil = (function() {\n try {\n // Use `util.types` for Node.js 10+.\n var types = freeModule && freeModule.require && freeModule.require('util').types;\n\n if (types) {\n return types;\n }\n\n // Legacy `process.binding('util')` for Node.js < 10.\n return freeProcess && freeProcess.binding && freeProcess.binding('util');\n } catch (e) {}\n}());\n\nexport default nodeUtil;\n", "import baseIsTypedArray from './_baseIsTypedArray.js';\nimport baseUnary from './_baseUnary.js';\nimport nodeUtil from './_nodeUtil.js';\n\n/* Node.js helper references. */\nvar nodeIsTypedArray = nodeUtil && nodeUtil.isTypedArray;\n\n/**\n * Checks if `value` is classified as a typed array.\n *\n * @static\n * @memberOf _\n * @since 3.0.0\n * @category Lang\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is a typed array, else `false`.\n * @example\n *\n * _.isTypedArray(new Uint8Array);\n * // => true\n *\n * _.isTypedArray([]);\n * // => false\n */\nvar isTypedArray = nodeIsTypedArray ? baseUnary(nodeIsTypedArray) : baseIsTypedArray;\n\nexport default isTypedArray;\n", "import baseTimes from './_baseTimes.js';\nimport isArguments from './isArguments.js';\nimport isArray from './isArray.js';\nimport isBuffer from './isBuffer.js';\nimport isIndex from './_isIndex.js';\nimport isTypedArray from './isTypedArray.js';\n\n/** Used for built-in method references. */\nvar objectProto = Object.prototype;\n\n/** Used to check objects for own properties. */\nvar hasOwnProperty = objectProto.hasOwnProperty;\n\n/**\n * Creates an array of the enumerable property names of the array-like `value`.\n *\n * @private\n * @param {*} value The value to query.\n * @param {boolean} inherited Specify returning inherited property names.\n * @returns {Array} Returns the array of property names.\n */\nfunction arrayLikeKeys(value, inherited) {\n var isArr = isArray(value),\n isArg = !isArr && isArguments(value),\n isBuff = !isArr && !isArg && isBuffer(value),\n isType = !isArr && !isArg && !isBuff && isTypedArray(value),\n skipIndexes = isArr || isArg || isBuff || isType,\n result = skipIndexes ? baseTimes(value.length, String) : [],\n length = result.length;\n\n for (var key in value) {\n if ((inherited || hasOwnProperty.call(value, key)) &&\n !(skipIndexes && (\n // Safari 9 has enumerable `arguments.length` in strict mode.\n key == 'length' ||\n // Node.js 0.10 has enumerable non-index properties on buffers.\n (isBuff && (key == 'offset' || key == 'parent')) ||\n // PhantomJS 2 has enumerable non-index properties on typed arrays.\n (isType && (key == 'buffer' || key == 'byteLength' || key == 'byteOffset')) ||\n // Skip index properties.\n isIndex(key, length)\n ))) {\n result.push(key);\n }\n }\n return result;\n}\n\nexport default arrayLikeKeys;\n", "/**\n * Creates a unary function that invokes `func` with its argument transformed.\n *\n * @private\n * @param {Function} func The function to wrap.\n * @param {Function} transform The argument transform.\n * @returns {Function} Returns the new function.\n */\nfunction overArg(func, transform) {\n return function(arg) {\n return func(transform(arg));\n };\n}\n\nexport default overArg;\n", "import overArg from './_overArg.js';\n\n/* Built-in method references for those with the same name as other `lodash` methods. */\nvar nativeKeys = overArg(Object.keys, Object);\n\nexport default nativeKeys;\n", "import isPrototype from './_isPrototype.js';\nimport nativeKeys from './_nativeKeys.js';\n\n/** Used for built-in method references. */\nvar objectProto = Object.prototype;\n\n/** Used to check objects for own properties. */\nvar hasOwnProperty = objectProto.hasOwnProperty;\n\n/**\n * The base implementation of `_.keys` which doesn't treat sparse arrays as dense.\n *\n * @private\n * @param {Object} object The object to query.\n * @returns {Array} Returns the array of property names.\n */\nfunction baseKeys(object) {\n if (!isPrototype(object)) {\n return nativeKeys(object);\n }\n var result = [];\n for (var key in Object(object)) {\n if (hasOwnProperty.call(object, key) && key != 'constructor') {\n result.push(key);\n }\n }\n return result;\n}\n\nexport default baseKeys;\n", "import arrayLikeKeys from './_arrayLikeKeys.js';\nimport baseKeys from './_baseKeys.js';\nimport isArrayLike from './isArrayLike.js';\n\n/**\n * Creates an array of the own enumerable property names of `object`.\n *\n * **Note:** Non-object values are coerced to objects. See the\n * [ES spec](http://ecma-international.org/ecma-262/7.0/#sec-object.keys)\n * for more details.\n *\n * @static\n * @since 0.1.0\n * @memberOf _\n * @category Object\n * @param {Object} object The object to query.\n * @returns {Array} Returns the array of property names.\n * @example\n *\n * function Foo() {\n * this.a = 1;\n * this.b = 2;\n * }\n *\n * Foo.prototype.c = 3;\n *\n * _.keys(new Foo);\n * // => ['a', 'b'] (iteration order is not guaranteed)\n *\n * _.keys('hi');\n * // => ['0', '1']\n */\nfunction keys(object) {\n return isArrayLike(object) ? arrayLikeKeys(object) : baseKeys(object);\n}\n\nexport default keys;\n", "import getNative from './_getNative.js';\n\n/* Built-in method references that are verified to be native. */\nvar nativeCreate = getNative(Object, 'create');\n\nexport default nativeCreate;\n", "import nativeCreate from './_nativeCreate.js';\n\n/**\n * Removes all key-value entries from the hash.\n *\n * @private\n * @name clear\n * @memberOf Hash\n */\nfunction hashClear() {\n this.__data__ = nativeCreate ? nativeCreate(null) : {};\n this.size = 0;\n}\n\nexport default hashClear;\n", "/**\n * Removes `key` and its value from the hash.\n *\n * @private\n * @name delete\n * @memberOf Hash\n * @param {Object} hash The hash to modify.\n * @param {string} key The key of the value to remove.\n * @returns {boolean} Returns `true` if the entry was removed, else `false`.\n */\nfunction hashDelete(key) {\n var result = this.has(key) && delete this.__data__[key];\n this.size -= result ? 1 : 0;\n return result;\n}\n\nexport default hashDelete;\n", "import nativeCreate from './_nativeCreate.js';\n\n/** Used to stand-in for `undefined` hash values. */\nvar HASH_UNDEFINED = '__lodash_hash_undefined__';\n\n/** Used for built-in method references. */\nvar objectProto = Object.prototype;\n\n/** Used to check objects for own properties. */\nvar hasOwnProperty = objectProto.hasOwnProperty;\n\n/**\n * Gets the hash value for `key`.\n *\n * @private\n * @name get\n * @memberOf Hash\n * @param {string} key The key of the value to get.\n * @returns {*} Returns the entry value.\n */\nfunction hashGet(key) {\n var data = this.__data__;\n if (nativeCreate) {\n var result = data[key];\n return result === HASH_UNDEFINED ? undefined : result;\n }\n return hasOwnProperty.call(data, key) ? data[key] : undefined;\n}\n\nexport default hashGet;\n", "import nativeCreate from './_nativeCreate.js';\n\n/** Used for built-in method references. */\nvar objectProto = Object.prototype;\n\n/** Used to check objects for own properties. */\nvar hasOwnProperty = objectProto.hasOwnProperty;\n\n/**\n * Checks if a hash value for `key` exists.\n *\n * @private\n * @name has\n * @memberOf Hash\n * @param {string} key The key of the entry to check.\n * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.\n */\nfunction hashHas(key) {\n var data = this.__data__;\n return nativeCreate ? (data[key] !== undefined) : hasOwnProperty.call(data, key);\n}\n\nexport default hashHas;\n", "import nativeCreate from './_nativeCreate.js';\n\n/** Used to stand-in for `undefined` hash values. */\nvar HASH_UNDEFINED = '__lodash_hash_undefined__';\n\n/**\n * Sets the hash `key` to `value`.\n *\n * @private\n * @name set\n * @memberOf Hash\n * @param {string} key The key of the value to set.\n * @param {*} value The value to set.\n * @returns {Object} Returns the hash instance.\n */\nfunction hashSet(key, value) {\n var data = this.__data__;\n this.size += this.has(key) ? 0 : 1;\n data[key] = (nativeCreate && value === undefined) ? HASH_UNDEFINED : value;\n return this;\n}\n\nexport default hashSet;\n", "import hashClear from './_hashClear.js';\nimport hashDelete from './_hashDelete.js';\nimport hashGet from './_hashGet.js';\nimport hashHas from './_hashHas.js';\nimport hashSet from './_hashSet.js';\n\n/**\n * Creates a hash object.\n *\n * @private\n * @constructor\n * @param {Array} [entries] The key-value pairs to cache.\n */\nfunction Hash(entries) {\n var index = -1,\n length = entries == null ? 0 : entries.length;\n\n this.clear();\n while (++index < length) {\n var entry = entries[index];\n this.set(entry[0], entry[1]);\n }\n}\n\n// Add methods to `Hash`.\nHash.prototype.clear = hashClear;\nHash.prototype['delete'] = hashDelete;\nHash.prototype.get = hashGet;\nHash.prototype.has = hashHas;\nHash.prototype.set = hashSet;\n\nexport default Hash;\n", "/**\n * Removes all key-value entries from the list cache.\n *\n * @private\n * @name clear\n * @memberOf ListCache\n */\nfunction listCacheClear() {\n this.__data__ = [];\n this.size = 0;\n}\n\nexport default listCacheClear;\n", "import eq from './eq.js';\n\n/**\n * Gets the index at which the `key` is found in `array` of key-value pairs.\n *\n * @private\n * @param {Array} array The array to inspect.\n * @param {*} key The key to search for.\n * @returns {number} Returns the index of the matched value, else `-1`.\n */\nfunction assocIndexOf(array, key) {\n var length = array.length;\n while (length--) {\n if (eq(array[length][0], key)) {\n return length;\n }\n }\n return -1;\n}\n\nexport default assocIndexOf;\n", "import assocIndexOf from './_assocIndexOf.js';\n\n/** Used for built-in method references. */\nvar arrayProto = Array.prototype;\n\n/** Built-in value references. */\nvar splice = arrayProto.splice;\n\n/**\n * Removes `key` and its value from the list cache.\n *\n * @private\n * @name delete\n * @memberOf ListCache\n * @param {string} key The key of the value to remove.\n * @returns {boolean} Returns `true` if the entry was removed, else `false`.\n */\nfunction listCacheDelete(key) {\n var data = this.__data__,\n index = assocIndexOf(data, key);\n\n if (index < 0) {\n return false;\n }\n var lastIndex = data.length - 1;\n if (index == lastIndex) {\n data.pop();\n } else {\n splice.call(data, index, 1);\n }\n --this.size;\n return true;\n}\n\nexport default listCacheDelete;\n", "import assocIndexOf from './_assocIndexOf.js';\n\n/**\n * Gets the list cache value for `key`.\n *\n * @private\n * @name get\n * @memberOf ListCache\n * @param {string} key The key of the value to get.\n * @returns {*} Returns the entry value.\n */\nfunction listCacheGet(key) {\n var data = this.__data__,\n index = assocIndexOf(data, key);\n\n return index < 0 ? undefined : data[index][1];\n}\n\nexport default listCacheGet;\n", "import assocIndexOf from './_assocIndexOf.js';\n\n/**\n * Checks if a list cache value for `key` exists.\n *\n * @private\n * @name has\n * @memberOf ListCache\n * @param {string} key The key of the entry to check.\n * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.\n */\nfunction listCacheHas(key) {\n return assocIndexOf(this.__data__, key) > -1;\n}\n\nexport default listCacheHas;\n", "import assocIndexOf from './_assocIndexOf.js';\n\n/**\n * Sets the list cache `key` to `value`.\n *\n * @private\n * @name set\n * @memberOf ListCache\n * @param {string} key The key of the value to set.\n * @param {*} value The value to set.\n * @returns {Object} Returns the list cache instance.\n */\nfunction listCacheSet(key, value) {\n var data = this.__data__,\n index = assocIndexOf(data, key);\n\n if (index < 0) {\n ++this.size;\n data.push([key, value]);\n } else {\n data[index][1] = value;\n }\n return this;\n}\n\nexport default listCacheSet;\n", "import listCacheClear from './_listCacheClear.js';\nimport listCacheDelete from './_listCacheDelete.js';\nimport listCacheGet from './_listCacheGet.js';\nimport listCacheHas from './_listCacheHas.js';\nimport listCacheSet from './_listCacheSet.js';\n\n/**\n * Creates an list cache object.\n *\n * @private\n * @constructor\n * @param {Array} [entries] The key-value pairs to cache.\n */\nfunction ListCache(entries) {\n var index = -1,\n length = entries == null ? 0 : entries.length;\n\n this.clear();\n while (++index < length) {\n var entry = entries[index];\n this.set(entry[0], entry[1]);\n }\n}\n\n// Add methods to `ListCache`.\nListCache.prototype.clear = listCacheClear;\nListCache.prototype['delete'] = listCacheDelete;\nListCache.prototype.get = listCacheGet;\nListCache.prototype.has = listCacheHas;\nListCache.prototype.set = listCacheSet;\n\nexport default ListCache;\n", "import getNative from './_getNative.js';\nimport root from './_root.js';\n\n/* Built-in method references that are verified to be native. */\nvar Map = getNative(root, 'Map');\n\nexport default Map;\n", "import Hash from './_Hash.js';\nimport ListCache from './_ListCache.js';\nimport Map from './_Map.js';\n\n/**\n * Removes all key-value entries from the map.\n *\n * @private\n * @name clear\n * @memberOf MapCache\n */\nfunction mapCacheClear() {\n this.size = 0;\n this.__data__ = {\n 'hash': new Hash,\n 'map': new (Map || ListCache),\n 'string': new Hash\n };\n}\n\nexport default mapCacheClear;\n", "/**\n * Checks if `value` is suitable for use as unique object key.\n *\n * @private\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is suitable, else `false`.\n */\nfunction isKeyable(value) {\n var type = typeof value;\n return (type == 'string' || type == 'number' || type == 'symbol' || type == 'boolean')\n ? (value !== '__proto__')\n : (value === null);\n}\n\nexport default isKeyable;\n", "import isKeyable from './_isKeyable.js';\n\n/**\n * Gets the data for `map`.\n *\n * @private\n * @param {Object} map The map to query.\n * @param {string} key The reference key.\n * @returns {*} Returns the map data.\n */\nfunction getMapData(map, key) {\n var data = map.__data__;\n return isKeyable(key)\n ? data[typeof key == 'string' ? 'string' : 'hash']\n : data.map;\n}\n\nexport default getMapData;\n", "import getMapData from './_getMapData.js';\n\n/**\n * Removes `key` and its value from the map.\n *\n * @private\n * @name delete\n * @memberOf MapCache\n * @param {string} key The key of the value to remove.\n * @returns {boolean} Returns `true` if the entry was removed, else `false`.\n */\nfunction mapCacheDelete(key) {\n var result = getMapData(this, key)['delete'](key);\n this.size -= result ? 1 : 0;\n return result;\n}\n\nexport default mapCacheDelete;\n", "import getMapData from './_getMapData.js';\n\n/**\n * Gets the map value for `key`.\n *\n * @private\n * @name get\n * @memberOf MapCache\n * @param {string} key The key of the value to get.\n * @returns {*} Returns the entry value.\n */\nfunction mapCacheGet(key) {\n return getMapData(this, key).get(key);\n}\n\nexport default mapCacheGet;\n", "import getMapData from './_getMapData.js';\n\n/**\n * Checks if a map value for `key` exists.\n *\n * @private\n * @name has\n * @memberOf MapCache\n * @param {string} key The key of the entry to check.\n * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.\n */\nfunction mapCacheHas(key) {\n return getMapData(this, key).has(key);\n}\n\nexport default mapCacheHas;\n", "import getMapData from './_getMapData.js';\n\n/**\n * Sets the map `key` to `value`.\n *\n * @private\n * @name set\n * @memberOf MapCache\n * @param {string} key The key of the value to set.\n * @param {*} value The value to set.\n * @returns {Object} Returns the map cache instance.\n */\nfunction mapCacheSet(key, value) {\n var data = getMapData(this, key),\n size = data.size;\n\n data.set(key, value);\n this.size += data.size == size ? 0 : 1;\n return this;\n}\n\nexport default mapCacheSet;\n", "import mapCacheClear from './_mapCacheClear.js';\nimport mapCacheDelete from './_mapCacheDelete.js';\nimport mapCacheGet from './_mapCacheGet.js';\nimport mapCacheHas from './_mapCacheHas.js';\nimport mapCacheSet from './_mapCacheSet.js';\n\n/**\n * Creates a map cache object to store key-value pairs.\n *\n * @private\n * @constructor\n * @param {Array} [entries] The key-value pairs to cache.\n */\nfunction MapCache(entries) {\n var index = -1,\n length = entries == null ? 0 : entries.length;\n\n this.clear();\n while (++index < length) {\n var entry = entries[index];\n this.set(entry[0], entry[1]);\n }\n}\n\n// Add methods to `MapCache`.\nMapCache.prototype.clear = mapCacheClear;\nMapCache.prototype['delete'] = mapCacheDelete;\nMapCache.prototype.get = mapCacheGet;\nMapCache.prototype.has = mapCacheHas;\nMapCache.prototype.set = mapCacheSet;\n\nexport default MapCache;\n", "/**\n * Appends the elements of `values` to `array`.\n *\n * @private\n * @param {Array} array The array to modify.\n * @param {Array} values The values to append.\n * @returns {Array} Returns `array`.\n */\nfunction arrayPush(array, values) {\n var index = -1,\n length = values.length,\n offset = array.length;\n\n while (++index < length) {\n array[offset + index] = values[index];\n }\n return array;\n}\n\nexport default arrayPush;\n", "import ListCache from './_ListCache.js';\n\n/**\n * Removes all key-value entries from the stack.\n *\n * @private\n * @name clear\n * @memberOf Stack\n */\nfunction stackClear() {\n this.__data__ = new ListCache;\n this.size = 0;\n}\n\nexport default stackClear;\n", "/**\n * Removes `key` and its value from the stack.\n *\n * @private\n * @name delete\n * @memberOf Stack\n * @param {string} key The key of the value to remove.\n * @returns {boolean} Returns `true` if the entry was removed, else `false`.\n */\nfunction stackDelete(key) {\n var data = this.__data__,\n result = data['delete'](key);\n\n this.size = data.size;\n return result;\n}\n\nexport default stackDelete;\n", "/**\n * Gets the stack value for `key`.\n *\n * @private\n * @name get\n * @memberOf Stack\n * @param {string} key The key of the value to get.\n * @returns {*} Returns the entry value.\n */\nfunction stackGet(key) {\n return this.__data__.get(key);\n}\n\nexport default stackGet;\n", "/**\n * Checks if a stack value for `key` exists.\n *\n * @private\n * @name has\n * @memberOf Stack\n * @param {string} key The key of the entry to check.\n * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.\n */\nfunction stackHas(key) {\n return this.__data__.has(key);\n}\n\nexport default stackHas;\n", "import ListCache from './_ListCache.js';\nimport Map from './_Map.js';\nimport MapCache from './_MapCache.js';\n\n/** Used as the size to enable large array optimizations. */\nvar LARGE_ARRAY_SIZE = 200;\n\n/**\n * Sets the stack `key` to `value`.\n *\n * @private\n * @name set\n * @memberOf Stack\n * @param {string} key The key of the value to set.\n * @param {*} value The value to set.\n * @returns {Object} Returns the stack cache instance.\n */\nfunction stackSet(key, value) {\n var data = this.__data__;\n if (data instanceof ListCache) {\n var pairs = data.__data__;\n if (!Map || (pairs.length < LARGE_ARRAY_SIZE - 1)) {\n pairs.push([key, value]);\n this.size = ++data.size;\n return this;\n }\n data = this.__data__ = new MapCache(pairs);\n }\n data.set(key, value);\n this.size = data.size;\n return this;\n}\n\nexport default stackSet;\n", "import ListCache from './_ListCache.js';\nimport stackClear from './_stackClear.js';\nimport stackDelete from './_stackDelete.js';\nimport stackGet from './_stackGet.js';\nimport stackHas from './_stackHas.js';\nimport stackSet from './_stackSet.js';\n\n/**\n * Creates a stack cache object to store key-value pairs.\n *\n * @private\n * @constructor\n * @param {Array} [entries] The key-value pairs to cache.\n */\nfunction Stack(entries) {\n var data = this.__data__ = new ListCache(entries);\n this.size = data.size;\n}\n\n// Add methods to `Stack`.\nStack.prototype.clear = stackClear;\nStack.prototype['delete'] = stackDelete;\nStack.prototype.get = stackGet;\nStack.prototype.has = stackHas;\nStack.prototype.set = stackSet;\n\nexport default Stack;\n", "/**\n * A specialized version of `_.filter` for arrays without support for\n * iteratee shorthands.\n *\n * @private\n * @param {Array} [array] The array to iterate over.\n * @param {Function} predicate The function invoked per iteration.\n * @returns {Array} Returns the new filtered array.\n */\nfunction arrayFilter(array, predicate) {\n var index = -1,\n length = array == null ? 0 : array.length,\n resIndex = 0,\n result = [];\n\n while (++index < length) {\n var value = array[index];\n if (predicate(value, index, array)) {\n result[resIndex++] = value;\n }\n }\n return result;\n}\n\nexport default arrayFilter;\n", "/**\n * This method returns a new empty array.\n *\n * @static\n * @memberOf _\n * @since 4.13.0\n * @category Util\n * @returns {Array} Returns the new empty array.\n * @example\n *\n * var arrays = _.times(2, _.stubArray);\n *\n * console.log(arrays);\n * // => [[], []]\n *\n * console.log(arrays[0] === arrays[1]);\n * // => false\n */\nfunction stubArray() {\n return [];\n}\n\nexport default stubArray;\n", "import arrayFilter from './_arrayFilter.js';\nimport stubArray from './stubArray.js';\n\n/** Used for built-in method references. */\nvar objectProto = Object.prototype;\n\n/** Built-in value references. */\nvar propertyIsEnumerable = objectProto.propertyIsEnumerable;\n\n/* Built-in method references for those with the same name as other `lodash` methods. */\nvar nativeGetSymbols = Object.getOwnPropertySymbols;\n\n/**\n * Creates an array of the own enumerable symbols of `object`.\n *\n * @private\n * @param {Object} object The object to query.\n * @returns {Array} Returns the array of symbols.\n */\nvar getSymbols = !nativeGetSymbols ? stubArray : function(object) {\n if (object == null) {\n return [];\n }\n object = Object(object);\n return arrayFilter(nativeGetSymbols(object), function(symbol) {\n return propertyIsEnumerable.call(object, symbol);\n });\n};\n\nexport default getSymbols;\n", "import arrayPush from './_arrayPush.js';\nimport isArray from './isArray.js';\n\n/**\n * The base implementation of `getAllKeys` and `getAllKeysIn` which uses\n * `keysFunc` and `symbolsFunc` to get the enumerable property names and\n * symbols of `object`.\n *\n * @private\n * @param {Object} object The object to query.\n * @param {Function} keysFunc The function to get the keys of `object`.\n * @param {Function} symbolsFunc The function to get the symbols of `object`.\n * @returns {Array} Returns the array of property names and symbols.\n */\nfunction baseGetAllKeys(object, keysFunc, symbolsFunc) {\n var result = keysFunc(object);\n return isArray(object) ? result : arrayPush(result, symbolsFunc(object));\n}\n\nexport default baseGetAllKeys;\n", "import baseGetAllKeys from './_baseGetAllKeys.js';\nimport getSymbols from './_getSymbols.js';\nimport keys from './keys.js';\n\n/**\n * Creates an array of own enumerable property names and symbols of `object`.\n *\n * @private\n * @param {Object} object The object to query.\n * @returns {Array} Returns the array of property names and symbols.\n */\nfunction getAllKeys(object) {\n return baseGetAllKeys(object, keys, getSymbols);\n}\n\nexport default getAllKeys;\n", "import getNative from './_getNative.js';\nimport root from './_root.js';\n\n/* Built-in method references that are verified to be native. */\nvar DataView = getNative(root, 'DataView');\n\nexport default DataView;\n", "import getNative from './_getNative.js';\nimport root from './_root.js';\n\n/* Built-in method references that are verified to be native. */\nvar Promise = getNative(root, 'Promise');\n\nexport default Promise;\n", "import getNative from './_getNative.js';\nimport root from './_root.js';\n\n/* Built-in method references that are verified to be native. */\nvar Set = getNative(root, 'Set');\n\nexport default Set;\n", "import DataView from './_DataView.js';\nimport Map from './_Map.js';\nimport Promise from './_Promise.js';\nimport Set from './_Set.js';\nimport WeakMap from './_WeakMap.js';\nimport baseGetTag from './_baseGetTag.js';\nimport toSource from './_toSource.js';\n\n/** `Object#toString` result references. */\nvar mapTag = '[object Map]',\n objectTag = '[object Object]',\n promiseTag = '[object Promise]',\n setTag = '[object Set]',\n weakMapTag = '[object WeakMap]';\n\nvar dataViewTag = '[object DataView]';\n\n/** Used to detect maps, sets, and weakmaps. */\nvar dataViewCtorString = toSource(DataView),\n mapCtorString = toSource(Map),\n promiseCtorString = toSource(Promise),\n setCtorString = toSource(Set),\n weakMapCtorString = toSource(WeakMap);\n\n/**\n * Gets the `toStringTag` of `value`.\n *\n * @private\n * @param {*} value The value to query.\n * @returns {string} Returns the `toStringTag`.\n */\nvar getTag = baseGetTag;\n\n// Fallback for data views, maps, sets, and weak maps in IE 11 and promises in Node.js < 6.\nif ((DataView && getTag(new DataView(new ArrayBuffer(1))) != dataViewTag) ||\n (Map && getTag(new Map) != mapTag) ||\n (Promise && getTag(Promise.resolve()) != promiseTag) ||\n (Set && getTag(new Set) != setTag) ||\n (WeakMap && getTag(new WeakMap) != weakMapTag)) {\n getTag = function(value) {\n var result = baseGetTag(value),\n Ctor = result == objectTag ? value.constructor : undefined,\n ctorString = Ctor ? toSource(Ctor) : '';\n\n if (ctorString) {\n switch (ctorString) {\n case dataViewCtorString: return dataViewTag;\n case mapCtorString: return mapTag;\n case promiseCtorString: return promiseTag;\n case setCtorString: return setTag;\n case weakMapCtorString: return weakMapTag;\n }\n }\n return result;\n };\n}\n\nexport default getTag;\n", "import root from './_root.js';\n\n/** Built-in value references. */\nvar Uint8Array = root.Uint8Array;\n\nexport default Uint8Array;\n", "/** Used to stand-in for `undefined` hash values. */\nvar HASH_UNDEFINED = '__lodash_hash_undefined__';\n\n/**\n * Adds `value` to the array cache.\n *\n * @private\n * @name add\n * @memberOf SetCache\n * @alias push\n * @param {*} value The value to cache.\n * @returns {Object} Returns the cache instance.\n */\nfunction setCacheAdd(value) {\n this.__data__.set(value, HASH_UNDEFINED);\n return this;\n}\n\nexport default setCacheAdd;\n", "/**\n * Checks if `value` is in the array cache.\n *\n * @private\n * @name has\n * @memberOf SetCache\n * @param {*} value The value to search for.\n * @returns {number} Returns `true` if `value` is found, else `false`.\n */\nfunction setCacheHas(value) {\n return this.__data__.has(value);\n}\n\nexport default setCacheHas;\n", "import MapCache from './_MapCache.js';\nimport setCacheAdd from './_setCacheAdd.js';\nimport setCacheHas from './_setCacheHas.js';\n\n/**\n *\n * Creates an array cache object to store unique values.\n *\n * @private\n * @constructor\n * @param {Array} [values] The values to cache.\n */\nfunction SetCache(values) {\n var index = -1,\n length = values == null ? 0 : values.length;\n\n this.__data__ = new MapCache;\n while (++index < length) {\n this.add(values[index]);\n }\n}\n\n// Add methods to `SetCache`.\nSetCache.prototype.add = SetCache.prototype.push = setCacheAdd;\nSetCache.prototype.has = setCacheHas;\n\nexport default SetCache;\n", "/**\n * A specialized version of `_.some` for arrays without support for iteratee\n * shorthands.\n *\n * @private\n * @param {Array} [array] The array to iterate over.\n * @param {Function} predicate The function invoked per iteration.\n * @returns {boolean} Returns `true` if any element passes the predicate check,\n * else `false`.\n */\nfunction arraySome(array, predicate) {\n var index = -1,\n length = array == null ? 0 : array.length;\n\n while (++index < length) {\n if (predicate(array[index], index, array)) {\n return true;\n }\n }\n return false;\n}\n\nexport default arraySome;\n", "/**\n * Checks if a `cache` value for `key` exists.\n *\n * @private\n * @param {Object} cache The cache to query.\n * @param {string} key The key of the entry to check.\n * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.\n */\nfunction cacheHas(cache, key) {\n return cache.has(key);\n}\n\nexport default cacheHas;\n", "import SetCache from './_SetCache.js';\nimport arraySome from './_arraySome.js';\nimport cacheHas from './_cacheHas.js';\n\n/** Used to compose bitmasks for value comparisons. */\nvar COMPARE_PARTIAL_FLAG = 1,\n COMPARE_UNORDERED_FLAG = 2;\n\n/**\n * A specialized version of `baseIsEqualDeep` for arrays with support for\n * partial deep comparisons.\n *\n * @private\n * @param {Array} array The array to compare.\n * @param {Array} other The other array to compare.\n * @param {number} bitmask The bitmask flags. See `baseIsEqual` for more details.\n * @param {Function} customizer The function to customize comparisons.\n * @param {Function} equalFunc The function to determine equivalents of values.\n * @param {Object} stack Tracks traversed `array` and `other` objects.\n * @returns {boolean} Returns `true` if the arrays are equivalent, else `false`.\n */\nfunction equalArrays(array, other, bitmask, customizer, equalFunc, stack) {\n var isPartial = bitmask & COMPARE_PARTIAL_FLAG,\n arrLength = array.length,\n othLength = other.length;\n\n if (arrLength != othLength && !(isPartial && othLength > arrLength)) {\n return false;\n }\n // Check that cyclic values are equal.\n var arrStacked = stack.get(array);\n var othStacked = stack.get(other);\n if (arrStacked && othStacked) {\n return arrStacked == other && othStacked == array;\n }\n var index = -1,\n result = true,\n seen = (bitmask & COMPARE_UNORDERED_FLAG) ? new SetCache : undefined;\n\n stack.set(array, other);\n stack.set(other, array);\n\n // Ignore non-index properties.\n while (++index < arrLength) {\n var arrValue = array[index],\n othValue = other[index];\n\n if (customizer) {\n var compared = isPartial\n ? customizer(othValue, arrValue, index, other, array, stack)\n : customizer(arrValue, othValue, index, array, other, stack);\n }\n if (compared !== undefined) {\n if (compared) {\n continue;\n }\n result = false;\n break;\n }\n // Recursively compare arrays (susceptible to call stack limits).\n if (seen) {\n if (!arraySome(other, function(othValue, othIndex) {\n if (!cacheHas(seen, othIndex) &&\n (arrValue === othValue || equalFunc(arrValue, othValue, bitmask, customizer, stack))) {\n return seen.push(othIndex);\n }\n })) {\n result = false;\n break;\n }\n } else if (!(\n arrValue === othValue ||\n equalFunc(arrValue, othValue, bitmask, customizer, stack)\n )) {\n result = false;\n break;\n }\n }\n stack['delete'](array);\n stack['delete'](other);\n return result;\n}\n\nexport default equalArrays;\n", "/**\n * Converts `map` to its key-value pairs.\n *\n * @private\n * @param {Object} map The map to convert.\n * @returns {Array} Returns the key-value pairs.\n */\nfunction mapToArray(map) {\n var index = -1,\n result = Array(map.size);\n\n map.forEach(function(value, key) {\n result[++index] = [key, value];\n });\n return result;\n}\n\nexport default mapToArray;\n", "/**\n * Converts `set` to an array of its values.\n *\n * @private\n * @param {Object} set The set to convert.\n * @returns {Array} Returns the values.\n */\nfunction setToArray(set) {\n var index = -1,\n result = Array(set.size);\n\n set.forEach(function(value) {\n result[++index] = value;\n });\n return result;\n}\n\nexport default setToArray;\n", "import Symbol from './_Symbol.js';\nimport Uint8Array from './_Uint8Array.js';\nimport eq from './eq.js';\nimport equalArrays from './_equalArrays.js';\nimport mapToArray from './_mapToArray.js';\nimport setToArray from './_setToArray.js';\n\n/** Used to compose bitmasks for value comparisons. */\nvar COMPARE_PARTIAL_FLAG = 1,\n COMPARE_UNORDERED_FLAG = 2;\n\n/** `Object#toString` result references. */\nvar boolTag = '[object Boolean]',\n dateTag = '[object Date]',\n errorTag = '[object Error]',\n mapTag = '[object Map]',\n numberTag = '[object Number]',\n regexpTag = '[object RegExp]',\n setTag = '[object Set]',\n stringTag = '[object String]',\n symbolTag = '[object Symbol]';\n\nvar arrayBufferTag = '[object ArrayBuffer]',\n dataViewTag = '[object DataView]';\n\n/** Used to convert symbols to primitives and strings. */\nvar symbolProto = Symbol ? Symbol.prototype : undefined,\n symbolValueOf = symbolProto ? symbolProto.valueOf : undefined;\n\n/**\n * A specialized version of `baseIsEqualDeep` for comparing objects of\n * the same `toStringTag`.\n *\n * **Note:** This function only supports comparing values with tags of\n * `Boolean`, `Date`, `Error`, `Number`, `RegExp`, or `String`.\n *\n * @private\n * @param {Object} object The object to compare.\n * @param {Object} other The other object to compare.\n * @param {string} tag The `toStringTag` of the objects to compare.\n * @param {number} bitmask The bitmask flags. See `baseIsEqual` for more details.\n * @param {Function} customizer The function to customize comparisons.\n * @param {Function} equalFunc The function to determine equivalents of values.\n * @param {Object} stack Tracks traversed `object` and `other` objects.\n * @returns {boolean} Returns `true` if the objects are equivalent, else `false`.\n */\nfunction equalByTag(object, other, tag, bitmask, customizer, equalFunc, stack) {\n switch (tag) {\n case dataViewTag:\n if ((object.byteLength != other.byteLength) ||\n (object.byteOffset != other.byteOffset)) {\n return false;\n }\n object = object.buffer;\n other = other.buffer;\n\n case arrayBufferTag:\n if ((object.byteLength != other.byteLength) ||\n !equalFunc(new Uint8Array(object), new Uint8Array(other))) {\n return false;\n }\n return true;\n\n case boolTag:\n case dateTag:\n case numberTag:\n // Coerce booleans to `1` or `0` and dates to milliseconds.\n // Invalid dates are coerced to `NaN`.\n return eq(+object, +other);\n\n case errorTag:\n return object.name == other.name && object.message == other.message;\n\n case regexpTag:\n case stringTag:\n // Coerce regexes to strings and treat strings, primitives and objects,\n // as equal. See http://www.ecma-international.org/ecma-262/7.0/#sec-regexp.prototype.tostring\n // for more details.\n return object == (other + '');\n\n case mapTag:\n var convert = mapToArray;\n\n case setTag:\n var isPartial = bitmask & COMPARE_PARTIAL_FLAG;\n convert || (convert = setToArray);\n\n if (object.size != other.size && !isPartial) {\n return false;\n }\n // Assume cyclic values are equal.\n var stacked = stack.get(object);\n if (stacked) {\n return stacked == other;\n }\n bitmask |= COMPARE_UNORDERED_FLAG;\n\n // Recursively compare objects (susceptible to call stack limits).\n stack.set(object, other);\n var result = equalArrays(convert(object), convert(other), bitmask, customizer, equalFunc, stack);\n stack['delete'](object);\n return result;\n\n case symbolTag:\n if (symbolValueOf) {\n return symbolValueOf.call(object) == symbolValueOf.call(other);\n }\n }\n return false;\n}\n\nexport default equalByTag;\n", "import getAllKeys from './_getAllKeys.js';\n\n/** Used to compose bitmasks for value comparisons. */\nvar COMPARE_PARTIAL_FLAG = 1;\n\n/** Used for built-in method references. */\nvar objectProto = Object.prototype;\n\n/** Used to check objects for own properties. */\nvar hasOwnProperty = objectProto.hasOwnProperty;\n\n/**\n * A specialized version of `baseIsEqualDeep` for objects with support for\n * partial deep comparisons.\n *\n * @private\n * @param {Object} object The object to compare.\n * @param {Object} other The other object to compare.\n * @param {number} bitmask The bitmask flags. See `baseIsEqual` for more details.\n * @param {Function} customizer The function to customize comparisons.\n * @param {Function} equalFunc The function to determine equivalents of values.\n * @param {Object} stack Tracks traversed `object` and `other` objects.\n * @returns {boolean} Returns `true` if the objects are equivalent, else `false`.\n */\nfunction equalObjects(object, other, bitmask, customizer, equalFunc, stack) {\n var isPartial = bitmask & COMPARE_PARTIAL_FLAG,\n objProps = getAllKeys(object),\n objLength = objProps.length,\n othProps = getAllKeys(other),\n othLength = othProps.length;\n\n if (objLength != othLength && !isPartial) {\n return false;\n }\n var index = objLength;\n while (index--) {\n var key = objProps[index];\n if (!(isPartial ? key in other : hasOwnProperty.call(other, key))) {\n return false;\n }\n }\n // Check that cyclic values are equal.\n var objStacked = stack.get(object);\n var othStacked = stack.get(other);\n if (objStacked && othStacked) {\n return objStacked == other && othStacked == object;\n }\n var result = true;\n stack.set(object, other);\n stack.set(other, object);\n\n var skipCtor = isPartial;\n while (++index < objLength) {\n key = objProps[index];\n var objValue = object[key],\n othValue = other[key];\n\n if (customizer) {\n var compared = isPartial\n ? customizer(othValue, objValue, key, other, object, stack)\n : customizer(objValue, othValue, key, object, other, stack);\n }\n // Recursively compare objects (susceptible to call stack limits).\n if (!(compared === undefined\n ? (objValue === othValue || equalFunc(objValue, othValue, bitmask, customizer, stack))\n : compared\n )) {\n result = false;\n break;\n }\n skipCtor || (skipCtor = key == 'constructor');\n }\n if (result && !skipCtor) {\n var objCtor = object.constructor,\n othCtor = other.constructor;\n\n // Non `Object` object instances with different constructors are not equal.\n if (objCtor != othCtor &&\n ('constructor' in object && 'constructor' in other) &&\n !(typeof objCtor == 'function' && objCtor instanceof objCtor &&\n typeof othCtor == 'function' && othCtor instanceof othCtor)) {\n result = false;\n }\n }\n stack['delete'](object);\n stack['delete'](other);\n return result;\n}\n\nexport default equalObjects;\n", "import Stack from './_Stack.js';\nimport equalArrays from './_equalArrays.js';\nimport equalByTag from './_equalByTag.js';\nimport equalObjects from './_equalObjects.js';\nimport getTag from './_getTag.js';\nimport isArray from './isArray.js';\nimport isBuffer from './isBuffer.js';\nimport isTypedArray from './isTypedArray.js';\n\n/** Used to compose bitmasks for value comparisons. */\nvar COMPARE_PARTIAL_FLAG = 1;\n\n/** `Object#toString` result references. */\nvar argsTag = '[object Arguments]',\n arrayTag = '[object Array]',\n objectTag = '[object Object]';\n\n/** Used for built-in method references. */\nvar objectProto = Object.prototype;\n\n/** Used to check objects for own properties. */\nvar hasOwnProperty = objectProto.hasOwnProperty;\n\n/**\n * A specialized version of `baseIsEqual` for arrays and objects which performs\n * deep comparisons and tracks traversed objects enabling objects with circular\n * references to be compared.\n *\n * @private\n * @param {Object} object The object to compare.\n * @param {Object} other The other object to compare.\n * @param {number} bitmask The bitmask flags. See `baseIsEqual` for more details.\n * @param {Function} customizer The function to customize comparisons.\n * @param {Function} equalFunc The function to determine equivalents of values.\n * @param {Object} [stack] Tracks traversed `object` and `other` objects.\n * @returns {boolean} Returns `true` if the objects are equivalent, else `false`.\n */\nfunction baseIsEqualDeep(object, other, bitmask, customizer, equalFunc, stack) {\n var objIsArr = isArray(object),\n othIsArr = isArray(other),\n objTag = objIsArr ? arrayTag : getTag(object),\n othTag = othIsArr ? arrayTag : getTag(other);\n\n objTag = objTag == argsTag ? objectTag : objTag;\n othTag = othTag == argsTag ? objectTag : othTag;\n\n var objIsObj = objTag == objectTag,\n othIsObj = othTag == objectTag,\n isSameTag = objTag == othTag;\n\n if (isSameTag && isBuffer(object)) {\n if (!isBuffer(other)) {\n return false;\n }\n objIsArr = true;\n objIsObj = false;\n }\n if (isSameTag && !objIsObj) {\n stack || (stack = new Stack);\n return (objIsArr || isTypedArray(object))\n ? equalArrays(object, other, bitmask, customizer, equalFunc, stack)\n : equalByTag(object, other, objTag, bitmask, customizer, equalFunc, stack);\n }\n if (!(bitmask & COMPARE_PARTIAL_FLAG)) {\n var objIsWrapped = objIsObj && hasOwnProperty.call(object, '__wrapped__'),\n othIsWrapped = othIsObj && hasOwnProperty.call(other, '__wrapped__');\n\n if (objIsWrapped || othIsWrapped) {\n var objUnwrapped = objIsWrapped ? object.value() : object,\n othUnwrapped = othIsWrapped ? other.value() : other;\n\n stack || (stack = new Stack);\n return equalFunc(objUnwrapped, othUnwrapped, bitmask, customizer, stack);\n }\n }\n if (!isSameTag) {\n return false;\n }\n stack || (stack = new Stack);\n return equalObjects(object, other, bitmask, customizer, equalFunc, stack);\n}\n\nexport default baseIsEqualDeep;\n", "import baseIsEqualDeep from './_baseIsEqualDeep.js';\nimport isObjectLike from './isObjectLike.js';\n\n/**\n * The base implementation of `_.isEqual` which supports partial comparisons\n * and tracks traversed objects.\n *\n * @private\n * @param {*} value The value to compare.\n * @param {*} other The other value to compare.\n * @param {boolean} bitmask The bitmask flags.\n * 1 - Unordered comparison\n * 2 - Partial comparison\n * @param {Function} [customizer] The function to customize comparisons.\n * @param {Object} [stack] Tracks traversed `value` and `other` objects.\n * @returns {boolean} Returns `true` if the values are equivalent, else `false`.\n */\nfunction baseIsEqual(value, other, bitmask, customizer, stack) {\n if (value === other) {\n return true;\n }\n if (value == null || other == null || (!isObjectLike(value) && !isObjectLike(other))) {\n return value !== value && other !== other;\n }\n return baseIsEqualDeep(value, other, bitmask, customizer, baseIsEqual, stack);\n}\n\nexport default baseIsEqual;\n", "import baseIsEqual from './_baseIsEqual.js';\n\n/**\n * Performs a deep comparison between two values to determine if they are\n * equivalent.\n *\n * **Note:** This method supports comparing arrays, array buffers, booleans,\n * date objects, error objects, maps, numbers, `Object` objects, regexes,\n * sets, strings, symbols, and typed arrays. `Object` objects are compared\n * by their own, not inherited, enumerable properties. Functions and DOM\n * nodes are compared by strict equality, i.e. `===`.\n *\n * @static\n * @memberOf _\n * @since 0.1.0\n * @category Lang\n * @param {*} value The value to compare.\n * @param {*} other The other value to compare.\n * @returns {boolean} Returns `true` if the values are equivalent, else `false`.\n * @example\n *\n * var object = { 'a': 1 };\n * var other = { 'a': 1 };\n *\n * _.isEqual(object, other);\n * // => true\n *\n * object === other;\n * // => false\n */\nfunction isEqual(value, other) {\n return baseIsEqual(value, other);\n}\n\nexport default isEqual;\n", "import { isEqual } from 'lodash-es';\n\nimport Metadata from './Metadata';\nimport {\n PluginStatusError,\n PluginStatusMetadata,\n PluginStatusProcessed,\n PluginStatusProcessing\n} from './types';\n\nclass ImageProcessingMetadata extends Metadata<PluginStatusMetadata> {\n get(blockId: number): PluginStatusMetadata {\n return super.get(blockId) ?? { status: 'IDLE' };\n }\n\n /**\n * Detect if the block has been duplicated with processed or processing\n * fill processing. In that case the processing state is still\n * valid, but blockId and fillId have changed.\n */\n isDuplicate(blockId: number): boolean {\n if (!this.engine.block.isValid(blockId)) return false;\n\n const metadata = this.get(blockId);\n\n if (\n metadata.status === 'IDLE' ||\n metadata.status === 'PENDING' ||\n metadata.status === 'ERROR'\n )\n return false;\n\n if (!this.engine.block.hasFill(blockId)) return false;\n const fillId = this.engine.block.getFill(blockId);\n\n // It cannot be a duplicate if the blockId or fillId are the same\n if (metadata.blockId === blockId || metadata.fillId === fillId)\n return false;\n\n return true;\n }\n\n /**\n * Fixes the metadata if the block has been duplicated, i.e. the blockId and\n * fillId will be updated to the current block/fill.\n *\n * Please note: Call this method only on duplicates (see isDuplicate).\n */\n fixDuplicate(blockId: number) {\n const fillId = this.engine.block.getFill(blockId);\n const metadata = this.get(blockId);\n if (\n metadata.status === 'IDLE' ||\n metadata.status === 'PENDING' ||\n metadata.status === 'ERROR'\n )\n return;\n this.set(blockId, {\n ...metadata,\n blockId,\n fillId\n });\n\n // If it is currently processing the best we can do is to just recover\n // the initial image data, since no processing will update this block and\n // it will be stuck in the processing state.\n if (metadata.status === 'PROCESSING') {\n this.recoverInitialImageData(blockId);\n this.clear(blockId);\n }\n }\n\n /**\n * Check if the image has a consisten metadata state. A inconsistent state is\n * caused by outside changes of the fill data.\n *\n * @returns true if the metadata is consistent, false otherwise\n */\n isConsistent(blockId: number): boolean {\n // In case the block was removed, we just abort and mark it\n // as reset by returning true\n if (!this.engine.block.isValid(blockId)) return false;\n const metadata = this.get(blockId);\n if (metadata.status === 'IDLE' || metadata.status === 'PENDING')\n return true;\n\n if (!this.engine.block.hasFill(blockId)) return false;\n const fillId = this.engine.block.getFill(blockId);\n if (fillId == null) return false;\n\n if (blockId !== metadata.blockId || fillId !== metadata.fillId)\n return false;\n\n const sourceSet = this.engine.block.getSourceSet(\n fillId,\n 'fill/image/sourceSet'\n );\n const imageFileURI = this.engine.block.getString(\n fillId,\n 'fill/image/imageFileURI'\n );\n\n if (\n sourceSet.length === 0 &&\n !imageFileURI &&\n metadata.status === 'PROCESSING'\n ) {\n // While we process it is OK to have no image file URI and no source set\n // (which we cleared to show the spinning loader)\n return true;\n }\n\n // Source sets have precedence over imageFileURI so if we have a source set,\n // we only need to check if it has changed to something else.\n if (sourceSet?.length > 0) {\n const initialSourceSet = metadata.initialSourceSet;\n // If we have already processed the image, we need to check if the source set\n // we need to check against both source sets, the removed and the initial\n if (metadata.status !== 'PROCESSED') {\n if (!isEqual(sourceSet, initialSourceSet)) {\n return false;\n }\n }\n } else {\n if (metadata.status !== 'PROCESSED') {\n if (imageFileURI !== metadata.initialImageFileURI) {\n return false;\n }\n }\n }\n return true;\n }\n\n /**\n * Recover the initial values to avoid the loading spinner and have the same\n * state as before the fill processing was started.\n */\n recoverInitialImageData(blockId: number) {\n const blockApi = this.engine.block;\n if (!blockApi.hasFill(blockId)) return; // Nothing to recover (no fill anymore)\n\n const metadata = this.get(blockId);\n\n if (metadata.status === 'PENDING' || metadata.status === 'IDLE') {\n return;\n }\n\n const initialSourceSet = metadata.initialSourceSet;\n const initialImageFileURI = metadata.initialImageFileURI;\n const initialPreviewFileURI = metadata.initialPreviewFileURI;\n\n const fillId = this.getValidFill(blockId, metadata);\n if (fillId == null) return;\n\n if (initialImageFileURI) {\n this.engine.block.setString(\n fillId,\n 'fill/image/imageFileURI',\n initialImageFileURI\n );\n }\n if (initialPreviewFileURI) {\n this.engine.block.setString(\n fillId,\n 'fill/image/previewFileURI',\n initialPreviewFileURI\n );\n }\n if (initialSourceSet.length > 0) {\n this.engine.block.setSourceSet(\n fillId,\n 'fill/image/sourceSet',\n initialSourceSet\n );\n }\n }\n\n /**\n * Returns the fill id of the block if it has a valid fill that was used for\n * fill processing. Returns undefined otherwise.\n */\n private getValidFill(\n blockId: number,\n metadata: PluginStatusProcessing | PluginStatusError | PluginStatusProcessed\n ): number | undefined {\n if (\n !this.engine.block.isValid(blockId) ||\n !this.engine.block.hasFill(blockId) ||\n blockId !== metadata.blockId\n ) {\n return undefined;\n }\n const fillId = this.engine.block.getFill(blockId);\n if (fillId !== metadata.fillId) {\n return undefined;\n }\n\n return fillId;\n }\n}\n\nexport default ImageProcessingMetadata;\n", "import CreativeEditorSDK from '@cesdk/cesdk-js';\nimport { FillProcessingMetadata } from '..';\nimport {\n PluginStatusProcessed,\n PluginStatusProcessing\n} from '../metadata/types';\n\ninterface FillProcessor<T> {\n processFill(metadataState: PluginStatusProcessing): Promise<T>;\n\n commitProcessing(\n data: T,\n metadataState: PluginStatusProcessed\n ): number | void;\n}\n\nasync function fillProcessing<T>(\n blockId: number,\n cesdk: CreativeEditorSDK,\n metadata: FillProcessingMetadata,\n processor: FillProcessor<T>\n) {\n const blockApi = cesdk.engine.block;\n if (!blockApi.hasFill(blockId))\n throw new Error('Block does not support fill');\n\n const fillId = blockApi.getFill(blockId);\n\n // Get the current image URI and source set as initial values.\n const initialSourceSet = blockApi.getSourceSet(\n fillId,\n 'fill/image/sourceSet'\n );\n const initialImageFileURI = blockApi.getString(\n fillId,\n 'fill/image/imageFileURI'\n );\n const initialPreviewFileURI = blockApi.getString(\n fillId,\n 'fill/image/previewFileURI'\n );\n try {\n cesdk.engine.block.setState(fillId, {\n type: 'Pending',\n progress: 0\n });\n\n const status: PluginStatusProcessing = {\n ...metadata.get(blockId),\n version: PLUGIN_VERSION,\n initialSourceSet,\n initialImageFileURI,\n initialPreviewFileURI,\n blockId,\n fillId,\n status: 'PROCESSING'\n };\n\n metadata.set(blockId, status);\n\n const processedData = await processor.processFill(status);\n // Check for externally changed state while we were applying the mask and\n // do not proceed if the state was reset.\n if (\n metadata.get(blockId).status !== 'PROCESSING' ||\n !metadata.isConsistent(blockId)\n )\n return;\n\n // Check for externally changed state while we were uploading and\n // do not proceed if the state was reset.\n if (\n metadata.get(blockId).status !== 'PROCESSING' ||\n !metadata.isConsistent(blockId)\n )\n return;\n\n if (processedData == null) return;\n\n const metadataStateProcessed: PluginStatusProcessed = {\n version: PLUGIN_VERSION,\n initialSourceSet,\n initialImageFileURI,\n initialPreviewFileURI,\n blockId,\n fillId,\n status: 'PROCESSED'\n };\n\n const blockIdOrVoid = processor.commitProcessing(\n processedData,\n metadataStateProcessed\n );\n\n // If a new block was created, we do not update the metadata.\n if (blockIdOrVoid == null || blockIdOrVoid === blockId) {\n metadata.set(blockId, metadataStateProcessed);\n }\n\n // Finally, create an undo step\n cesdk.engine.editor.addUndoStep();\n } catch (error) {\n if (cesdk.engine.block.isValid(blockId)) {\n metadata.set(blockId, {\n version: PLUGIN_VERSION,\n initialSourceSet,\n initialImageFileURI,\n initialPreviewFileURI,\n blockId,\n fillId,\n status: 'ERROR'\n });\n\n metadata.recoverInitialImageData(blockId);\n }\n\n if (\n error != null &&\n typeof error === 'object' &&\n 'message' in error &&\n typeof error.message === 'string'\n ) {\n const message =\n error.message === 'signal timed out'\n ? 'Processing canceled due to timeout'\n : error.message;\n cesdk.ui.showNotification({\n type: 'error',\n message\n });\n }\n\n // eslint-disable-next-line no-console\n console.log(error);\n } finally {\n if (cesdk.engine.block.isValid(fillId)) {\n cesdk.engine.block.setState(fillId, { type: 'Ready' });\n }\n }\n}\n\nexport default fillProcessing;\n", "export function getFeatureId(pluginId: string): string {\n return `${pluginId}.fillProcessing.feature`;\n}\n\nexport function getCanvasMenuComponentIds(pluginId: string): string[] {\n return [`${pluginId}.canvasMenu`, `${pluginId}.fillProcessing.canvasMenu`];\n}\n\nexport function getDockComponentIds(pluginId: string): string[] {\n return [`${pluginId}.dock`, `${pluginId}.fillProcessing.dock`];\n}\n\nexport function getInspectorBarComponentIds(pluginId: string): string[] {\n return [\n `${pluginId}.inspectorBar`,\n `${pluginId}.fillProcessing.inspectorBar`\n ];\n}\n\nexport function getNavigationBarComponentIds(pluginId: string): string[] {\n return [\n `${pluginId}.navigationBar`,\n `${pluginId}.fillProcessing.navigationBar`\n ];\n}\n\nexport function getCanvasBarComponentIds(pluginId: string): string[] {\n return [`${pluginId}.canvasBar`, `${pluginId}.fillProcessing.canvasBar`];\n}\n\nexport function getI18nCanvasMenuLabel(pluginId: string): string {\n return `plugin.${pluginId}.fillProcessing.canvasMenu.button.label`;\n}\n\nexport function getI18nDockLabel(pluginId: string): string {\n return `plugin.${pluginId}.fillProcessing.dock.button.label`;\n}\n\nexport function getI18nInspectorBarLabel(pluginId: string): string {\n return `plugin.${pluginId}.fillProcessing.inspectorBar.button.label`;\n}\n\nexport function getI18nNavigationBarLabel(pluginId: string): string {\n return `plugin.${pluginId}.fillProcessing.navigationBar.button.label`;\n}\n\nexport function getI18nCanvasBarLabel(pluginId: string): string {\n return `plugin.${pluginId}.fillProcessing.canvasBar.button.label`;\n}\n", "import type CreativeEditorSDK from '@cesdk/cesdk-js';\n\nimport { FillProcessingMetadata } from '..';\nimport { getFeatureId } from './constants';\n\nexport default function handleFillProcessing(\n cesdk: CreativeEditorSDK,\n {\n pluginId,\n process\n }: {\n pluginId: string;\n icon?: string;\n process: (blockId: number, metadata: FillProcessingMetadata) => void;\n }\n): {\n featureId: string;\n} {\n const featureId = getFeatureId(pluginId);\n\n const metadata = new FillProcessingMetadata(cesdk.engine, pluginId);\n\n enableFeatures(cesdk, metadata, featureId);\n\n cesdk.engine.event.subscribe([], async (events) => {\n events.forEach((e) => {\n const id = e.block;\n if (!cesdk.engine.block.isValid(id) || !metadata.hasData(id)) {\n return;\n }\n\n if (e.type === 'Created') {\n if (metadata.isDuplicate(id)) {\n metadata.fixDuplicate(id);\n }\n } else if (e.type === 'Updated') {\n switch (metadata.get(id).status) {\n case 'PENDING': {\n if (\n cesdk.feature.isEnabled(featureId, {\n engine: cesdk.engine\n }) &&\n cesdk.engine.block.isAllowedByScope(id, 'fill/change') &&\n cesdk.engine.block.getState(id).type !== 'Pending'\n ) {\n process(id, metadata);\n }\n break;\n }\n\n case 'PROCESSING':\n case 'PROCESSED': {\n if (!metadata.isConsistent(id)) {\n metadata.clear(id);\n }\n break;\n }\n\n default: {\n // We do not care about other states\n }\n }\n }\n });\n });\n\n return { featureId };\n}\n\n/**\n * Defines the feature that determines in which context (on which block)\n * fill processing is allowed/enabled.\n */\nfunction enableFeatures(\n cesdk: CreativeEditorSDK,\n metadata: FillProcessingMetadata,\n featureId: string\n) {\n cesdk.feature.enable(featureId, ({ engine }) => {\n const selectedIds = engine.block.findAllSelected();\n if (selectedIds.length !== 1) {\n return false;\n }\n const [selectedId] = selectedIds;\n\n if (!cesdk.engine.block.isVisible(selectedId)) return false;\n\n if (cesdk.engine.block.hasFill(selectedId)) {\n const kind = cesdk.engine.block.getKind(selectedId);\n if (kind === 'sticker') return false;\n\n const fillId = cesdk.engine.block.getFill(selectedId);\n const fillType = cesdk.engine.block.getType(fillId);\n\n if (fillType !== '//ly.img.ubq/fill/image') {\n return false;\n }\n\n const fileUri = engine.block.getString(fillId, 'fill/image/imageFileURI');\n const sourceSet = engine.block.getSourceSet(\n fillId,\n 'fill/image/sourceSet'\n );\n\n if (sourceSet.length > 0 || fileUri !== '') return true;\n\n // If we are in a processing state we do not have a imageFileURI or\n // source set set (to show the loading spinner), but the feature is still\n // enabled.\n return metadata.get(selectedId).status === 'PROCESSING';\n }\n\n return false;\n });\n}\n", "import CreativeEditorSDK from '@cesdk/cesdk-js';\nimport { FillProcessingMetadata } from '..';\nimport {\n getCanvasBarComponentIds,\n getCanvasMenuComponentIds,\n getDockComponentIds,\n getFeatureId,\n getI18nCanvasBarLabel,\n getI18nCanvasMenuLabel,\n getI18nDockLabel,\n getI18nInspectorBarLabel,\n getI18nNavigationBarLabel,\n getInspectorBarComponentIds,\n getNavigationBarComponentIds\n} from './constants';\n\nexport type Location =\n | 'inspectorBar'\n | 'navigationBar'\n | 'canvasBarTop'\n | 'canvasBarBottom'\n | 'canvasMenu'\n | 'dock';\n\n/**\n * Registers the components that can be used to process the fill of\n * a block.\n */\nexport default function registerFillProcessingComponents(\n cesdk: CreativeEditorSDK,\n options: {\n pluginId: string;\n icon?: string;\n locations?: Location | Location[];\n }\n): {\n canvasMenuComponentId: string;\n dockComponentId: string;\n\n translationsKeys: {\n inspectorBarLabel: string;\n navigationBarLabel: string;\n canvasBarLabel: string;\n canvasMenuLabel: string;\n dockLabel: string;\n };\n} {\n const { pluginId, locations } = options;\n const metadata = new FillProcessingMetadata(cesdk.engine, pluginId);\n\n const canvasMenuLabel = getI18nCanvasMenuLabel(pluginId);\n const canvasMenuComponentIds = getCanvasMenuComponentIds(pluginId);\n const canvasMenuComponentId = canvasMenuComponentIds[0];\n\n const dockLabel = getI18nDockLabel(pluginId);\n const dockComponentIds = getDockComponentIds(pluginId);\n const dockComponentId = dockComponentIds[0];\n\n const inspectorBarLabel = getI18nInspectorBarLabel(pluginId);\n const inspectorBarComponentIds = getInspectorBarComponentIds(pluginId);\n const inspectorBarComponentId = inspectorBarComponentIds[0];\n\n const navigationBarLabel = getI18nNavigationBarLabel(pluginId);\n const navigationBarComponentIds = getNavigationBarComponentIds(pluginId);\n const navigationBarComponentId = navigationBarComponentIds[0];\n\n const canvasBarLabel = getI18nCanvasBarLabel(pluginId);\n const canvasBarComponentIds = getCanvasBarComponentIds(pluginId);\n const canvasBarComponentId = canvasBarComponentIds[0];\n\n const featureId = getFeatureId(pluginId);\n\n if (locations?.includes('inspectorBar')) {\n cesdk.ui.setInspectorBarOrder([\n inspectorBarComponentId,\n ...cesdk.ui.getInspectorBarOrder()\n ]);\n }\n\n if (locations?.includes('navigationBar')) {\n cesdk.ui.setNavigationBarOrder([\n navigationBarComponentId,\n ...cesdk.ui.getNavigationBarOrder()\n ]);\n }\n\n if (locations?.includes('canvasBarTop')) {\n cesdk.ui.setCanvasBarOrder(\n [canvasBarComponentId, ...cesdk.ui.getCanvasBarOrder('top')],\n 'top'\n );\n }\n\n if (locations?.includes('canvasBarBottom')) {\n cesdk.ui.setCanvasBarOrder(\n [canvasBarComponentId, ...cesdk.ui.getCanvasBarOrder('bottom')],\n 'bottom'\n );\n }\n\n if (locations?.includes('canvasMenu')) {\n cesdk.ui.setCanvasMenuOrder([\n canvasMenuComponentId,\n ...cesdk.ui.getCanvasMenuOrder()\n ]);\n }\n\n if (locations?.includes('dock')) {\n cesdk.ui.setDockOrder([...cesdk.ui.getDockOrder(), dockComponentId]);\n }\n\n cesdk.ui.registerComponent(\n dockComponentIds,\n ({ builder: { Button }, engine }) => {\n const [id] = engine.block.findAllSelected();\n\n let isDisabled = false;\n let isLoading = false;\n let loadingProgress: number | undefined;\n\n if (id == null) {\n isDisabled = true;\n }\n\n if (\n !isDisabled &&\n !cesdk.feature.isEnabled(featureId, {\n engine\n })\n ) {\n isDisabled = true;\n }\n\n if (\n !isDisabled &&\n !cesdk.engine.block.isAllowedByScope(id, 'fill/change')\n ) {\n isDisabled = true;\n }\n\n if (!isDisabled && engine.block.getState(id)?.type === 'Pending') {\n isDisabled = true;\n }\n\n if (!isDisabled) {\n const currentMetadata = metadata.get(id);\n\n isLoading = currentMetadata.status === 'PROCESSING';\n isDisabled =\n currentMetadata.status === 'PENDING' ||\n currentMetadata.status === 'PROCESSING';\n\n if (\n currentMetadata.status === 'PROCESSING' &&\n currentMetadata.progress\n ) {\n const { current, total } = currentMetadata.progress;\n loadingProgress = (current / total) * 100;\n }\n }\n\n const buttonId = `${dockComponentId}.button`;\n Button(buttonId, {\n label: dockLabel,\n icon: options.icon,\n isLoading,\n isDisabled,\n loadingProgress,\n onClick: () => {\n const currentMetadata = metadata.get(id);\n\n if (\n currentMetadata.status === 'IDLE' ||\n currentMetadata.status === 'ERROR' ||\n currentMetadata.status === 'PROCESSED'\n ) {\n metadata.set(id, {\n status: 'PENDING'\n });\n }\n }\n });\n }\n );\n\n const buttonComponents: {\n componentIds: string[];\n label: string;\n variant: 'plain' | 'regular';\n }[] = [\n {\n componentIds: inspectorBarComponentIds,\n variant: 'plain',\n label: inspectorBarLabel\n },\n {\n componentIds: navigationBarComponentIds,\n variant: 'regular',\n label: navigationBarLabel\n },\n {\n componentIds: canvasBarComponentIds,\n variant: 'regular',\n label: canvasBarLabel\n },\n {\n componentIds: canvasMenuComponentIds,\n variant: 'plain',\n label: canvasMenuLabel\n }\n ];\n\n buttonComponents.forEach(({ componentIds, label, variant }) => {\n const componentId = componentIds[0];\n cesdk.ui.registerComponent(\n componentIds,\n ({ builder: { Button }, engine }) => {\n if (\n !cesdk.feature.isEnabled(featureId, {\n engine\n })\n ) {\n return;\n }\n\n const [id] = engine.block.findAllSelected();\n\n if (!cesdk.engine.block.isAllowedByScope(id, 'fill/change')) return;\n\n const currentMetadata = metadata.get(id);\n\n const isLoading = currentMetadata.status === 'PROCESSING';\n const isDisabled =\n currentMetadata.status === 'PENDING' ||\n currentMetadata.status === 'PROCESSING' ||\n engine.block.getState(id)?.type === 'Pending';\n\n let loadingProgress: number | undefined;\n if (isLoading && currentMetadata.progress) {\n const { current, total } = currentMetadata.progress;\n loadingProgress = (current / total) * 100;\n }\n\n const buttonId = `${componentId}.button`;\n Button(buttonId, {\n icon: options.icon,\n label,\n variant,\n isLoading,\n isDisabled,\n loadingProgress,\n onClick: () => {\n if (\n currentMetadata.status === 'IDLE' ||\n currentMetadata.status === 'ERROR' ||\n currentMetadata.status === 'PROCESSED'\n ) {\n metadata.set(id, {\n status: 'PENDING'\n });\n }\n }\n });\n }\n );\n });\n\n return {\n canvasMenuComponentId,\n dockComponentId,\n translationsKeys: {\n inspectorBarLabel,\n navigationBarLabel,\n canvasBarLabel,\n canvasMenuLabel,\n dockLabel\n }\n };\n}\n", "import { type RGBAColor } from '@cesdk/cesdk-js';\n\nconst HEX_COLOR_PATTERN = new RegExp(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/, 'i');\nconst HEX_SINGLE_CHAR_COMPONENTS_PATTERN = new RegExp(/[A-Fa-f0-9]{1}/, 'g');\nconst HEX_DOUBLE_CHAR_COMPONENTS_PATTERN = new RegExp(/[A-Fa-f0-9]{2}/, 'g');\n\n/** @public */\nexport const rgbaToHex = (rgba: RGBAColor, includeAlpha = false): string => {\n const { r, g, b, a } = rgba;\n const rByte = Math.round(255 * r);\n const gByte = Math.round(255 * g);\n const bByte = Math.round(255 * b);\n const aByte = Math.round(255 * a);\n const byteToHex = (byte: number) => {\n return byte.toString(16).padStart(2, '0');\n };\n return `#${byteToHex(rByte)}${byteToHex(gByte)}${byteToHex(bByte)}${\n includeAlpha ? byteToHex(aByte) : ''\n }`;\n};\n\n/** @public */\nexport const hexToRgba = (hexString: string): RGBAColor => {\n const rgbaHexToColor = (\n r: string,\n g: string,\n b: string,\n a: string | undefined,\n max: number\n ) => {\n return {\n r: parseInt(r, 16) / max,\n g: parseInt(g, 16) / max,\n b: parseInt(b, 16) / max,\n a: a === undefined ? 1 : parseInt(a, 16) / max\n };\n };\n\n if (hexString.startsWith('#')) {\n if (hexString.length === 4 || hexString.length === 5) {\n const hexMatch = hexString.match(HEX_SINGLE_CHAR_COMPONENTS_PATTERN);\n if (hexMatch) {\n const [r, g, b, a] = hexMatch;\n return rgbaHexToColor(r, g, b, a, 15);\n }\n }\n if (hexString.length === 7 || hexString.length === 8) {\n const hexMatch = hexString.match(HEX_DOUBLE_CHAR_COMPONENTS_PATTERN);\n if (hexMatch) {\n const [r, g, b, a] = hexMatch;\n return rgbaHexToColor(r, g, b, a, 255);\n }\n }\n }\n\n throw new Error(\n 'Invalid hex string! Allowed RGB formats are \"#FFF\" and \"#FFFFFF\". Allowed RGBA formats are \"#FFFF\" and \"#FFFFFFFF'\n );\n};\n\n/** @public */\nexport const isValidHexColor = (hexString: string): boolean => {\n return HEX_COLOR_PATTERN.test(hexString);\n};\n", "import type CreativeEditorSDK from '@cesdk/cesdk-js';\nimport { CreativeEngine } from '@cesdk/cesdk-js';\n\n/**\n * Uploads a blob with the help of CE.SDK\n */\nexport async function uploadBlob(\n blob: Blob,\n initialUri: string,\n cesdk: CreativeEditorSDK\n) {\n const pathname = new URL(initialUri).pathname;\n const parts = pathname.split('/');\n const extension = mimeTypeToExtension(blob.type);\n const filename = parts[parts.length - 1]?.split('.')?.[0] ?? 'asset';\n const filenameWithExtension = `${filename}.${extension}`;\n\n const uploadedAssets = await cesdk.unstable_upload(\n new File([blob], filenameWithExtension, { type: blob.type }),\n () => {\n // TODO Delegate process to UI component\n }\n );\n\n const url = uploadedAssets.meta?.uri;\n if (url == null) {\n throw new Error('Could not upload processed fill');\n }\n return url;\n}\n\n/**\n * Returns the file extension for a given mime type.\n */\nexport function mimeTypeToExtension(mimeType: string): string {\n const extensions: Record<string, string> = {\n 'image/png': 'png',\n 'image/jpeg': 'jpg',\n 'image/webp': 'webp',\n 'image/gif': 'gif',\n 'image/svg+xml': 'svg'\n };\n return extensions[mimeType] ?? 'png';\n}\n\nexport async function fetchImageBlob(uri: string): Promise<Blob> {\n return fetch(uri).then((response) => response.blob());\n}\n\n/**\n * Converts a buffer URI to a object url.\n */\nexport async function bufferURIToObjectURL(\n uri: string,\n engine: CreativeEngine\n): Promise<string> {\n if (uri.startsWith('buffer:')) {\n const mimeType = await engine.editor.getMimeType(uri);\n const length = engine.editor.getBufferLength(uri);\n const data = engine.editor.getBufferData(uri, 0, length);\n const buffer = new Uint8Array(data);\n const blob = new Blob([buffer], { type: mimeType });\n return URL.createObjectURL(blob);\n } else {\n return uri;\n }\n}\n", "function uuid4() {\n /* eslint-disable no-bitwise */\n return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {\n const r = (Math.random() * 16) | 0;\n const v = c === 'x' ? r : (r & 0x3) | 0x8;\n return v.toString(16);\n /* eslint-enable no-bitwise */\n });\n}\n\nexport default uuid4;\n", "import { bufferURIToObjectURL } from './upload';\nimport { type CreativeEngine } from '@cesdk/cesdk-js';\n\n/**\n * Get the dimensions of an image from its URL\n *\n * @param url - The URL of the image\n * @returns A promise that resolves to an object containing the width and height of the image\n */\nexport async function getImageDimensionsFromURL(\n url: string,\n engine: CreativeEngine\n): Promise<{ width: number; height: number }> {\n const resolvedUrl = await bufferURIToObjectURL(url, engine);\n return new Promise((resolve, reject) => {\n const img = new Image();\n img.onload = () => {\n resolve({ width: img.width, height: img.height });\n };\n img.onerror = reject;\n img.src = resolvedUrl;\n });\n}\n\n/**\n * Get the URI of an image from a block ID, either the first source of a source set or\n * the image file URI. Will handle buffer URIs and convert them to object URLs.\n */\nexport async function getImageUri(\n blockId: number,\n engine: CreativeEngine,\n options?: { throwErrorIfSvg?: boolean }\n): Promise<string> {\n let uri;\n const fillBlock = engine.block.getFill(blockId);\n const sourceSet = engine.block.getSourceSet(\n fillBlock,\n 'fill/image/sourceSet'\n );\n const [source] = sourceSet;\n if (source == null) {\n uri = engine.block.getString(fillBlock, 'fill/image/imageFileURI');\n if (uri == null) throw new Error('No image source/uri found');\n } else {\n uri = source.uri;\n }\n\n // Check if the image is SVG (not supported)\n if (options?.throwErrorIfSvg) {\n const mimeType = await engine.editor.getMimeType(uri);\n if (mimeType === 'image/svg+xml') {\n throw new Error('SVG images are not supported');\n }\n }\n\n return bufferURIToObjectURL(uri, engine);\n}\n\n/**\n * Returns if the given fill block is an SVG image fill.\n */\nexport async function isSvgFill(\n fillBlock: number,\n engine: CreativeEngine\n): Promise<boolean> {\n if (engine.block.getType(fillBlock) !== '//ly.img.ubq/fill/image') {\n return false;\n }\n\n let uri;\n const sourceSet = engine.block.getSourceSet(\n fillBlock,\n 'fill/image/sourceSet'\n );\n const [source] = sourceSet;\n if (source == null) {\n uri = engine.block.getString(fillBlock, 'fill/image/imageFileURI');\n if (uri == null) return false;\n } else {\n uri = source.uri;\n }\n\n const mimeType = await engine.editor.getMimeType(uri);\n return mimeType === 'image/svg+xml';\n}\n", "/**\n * Checks if a value is defined (not undefined).\n *\n * Helpful to filter out undefined values from an array or collection\n * while keeping the type information intact.\n *\n * ```\n * array.filter(isDefined)\n * ```\n */\nfunction isDefined<T>(value: T | undefined): value is T {\n return value !== undefined;\n}\n\nexport default isDefined;\n", "/**\n * Converts a value to an array format.\n *\n * @template T The type of the array elements\n * @param value - The value to convert. Can be a single item, an array, null, or undefined\n * @returns An array containing the value(s). Returns empty array if value is null/undefined,\n * the original array if value is already an array, or a single-element array if value is a single item\n *\n * @example\n * ```typescript\n * toArray('hello') // ['hello']\n * toArray(['a', 'b']) // ['a', 'b']\n * toArray(null) // []\n * toArray(undefined) // []\n * toArray(42) // [42]\n * ```\n */\nfunction toArray<T>(value?: T | T[]): T[] {\n if (value == null) return [];\n return Array.isArray(value) ? value : [value];\n}\n\nexport default toArray;\n", "/**\n * Interface for objects that have translation capabilities\n */\ninterface TranslationAPI {\n translate(key: string | string[]): string;\n}\n\n/**\n * Check if CE.SDK supports the translate API (version 1.59.0 or higher)\n * @param cesdk - The CE.SDK instance to check\n * @returns True if the translate API is supported\n */\nexport function supportsTranslateAPI(cesdk: any): boolean {\n if (!cesdk?.version) return false;\n\n // Use localeCompare for semantic version comparison\n // Returns >= 0 when cesdk.version is 1.59.0 or higher\n const comparison = cesdk.version.localeCompare('1.59.0', undefined, {\n numeric: true,\n sensitivity: 'base'\n });\n\n return comparison >= 0 && typeof cesdk.i18n?.translate === 'function';\n}\n\n/**\n * Type guard to check if an object has the translate API\n * @param i18n - The object to check\n * @returns True if the object has a translate function\n */\nexport function hasTranslateAPI(i18n: any): i18n is TranslationAPI {\n return typeof i18n?.translate === 'function';\n}\n\n/**\n * Safely translate a key with CE.SDK version compatibility check.\n * Returns the translation if supported (CE.SDK >= 1.59.0), otherwise returns the fallback.\n *\n * @param cesdk - The CE.SDK instance\n * @param translationKey - The translation key to translate\n * @param fallback - The fallback value to use if translation is not supported or key is not found\n * @returns The translated string or fallback\n */\nexport function translateWithFallback(\n cesdk: any,\n translationKey: string | string[],\n fallback: string\n): string {\n if (!cesdk) {\n return fallback;\n }\n\n // Check if CE.SDK supports translation API (version 1.59.0+)\n if (supportsTranslateAPI(cesdk) && hasTranslateAPI(cesdk.i18n)) {\n return cesdk.i18n.translate(translationKey);\n }\n\n return fallback;\n}\n", "import formatsIconSprite from './icons/formats';\n\nconst Icons = {\n Formats: formatsIconSprite\n};\n\nexport { Icons };\n\nexport {\n CustomAssetSource,\n type CustomAssetSourceOptions,\n type SelectValue\n} from './assetSources/CustomAssetSource';\n\nexport { IndexedDBAssetSource } from './assetSources/IndexedDBAssetSource';\n\nexport { AggregatedAssetSource } from './assetSources/AggregatedAssetSource';\n\nexport { default as Metadata } from './metadata/Metadata';\n\nexport { default as FillProcessingMetadata } from './metadata/FillProcessingMetadata';\n\nexport { default as fillProcessing } from './processing/fillProcessing';\n\nexport { default as initializeFillProcessing } from './processing/initializeFillProcessing';\n\nexport { default as registerFillProcessingComponents } from './processing/registerFillProcessingComponents';\n\nexport { type Optional } from './types/Optional';\n\nexport {\n type Location,\n type UserInterfaceConfiguration\n} from './types/UserInterfaceConfiguration';\n\nexport { hexToRgba, isValidHexColor, rgbaToHex } from './utils/colors';\n\nexport {\n uploadBlob,\n fetchImageBlob,\n bufferURIToObjectURL,\n mimeTypeToExtension\n} from './utils/upload';\n\nexport { default as uuid } from './utils/uuid';\n\nexport {\n getImageDimensionsFromURL,\n getImageUri,\n isSvgFill\n} from './utils/images';\n\nexport { default as isDefined } from './utils/isDefined';\n\nexport { default as toArray } from './utils/toArray';\n\nexport {\n supportsTranslateAPI,\n hasTranslateAPI,\n translateWithFallback\n} from './translationHelpers';\n", "import CreativeEditorSDK, { AssetResult } from '@cesdk/cesdk-js';\nimport { RenderCustomProperty } from '../../core/provider';\nimport {\n CustomAssetSource,\n isDefined,\n translateWithFallback\n} from '@imgly/plugin-utils';\nimport { SelectValue } from '@imgly/plugin-utils/dist/assetSources/CustomAssetSource';\n\ntype StyleSelectionPayload = {\n onSelect: (asset: AssetResult) => Promise<void>;\n};\n\ntype Style = {\n id: 'none' | (string & {});\n label: string;\n prompt: string;\n thumbUri: string;\n};\n\n/**\n * Provides render function for a style transfer property that allows\n * to change a style (of an image) from a library.\n *\n * The style will be appended to the prompt property, so the model does\n * not need to support style transfer directly.\n *\n * By default this expects the property key to be `style`. This can be changed with the option\n * `propertyKey`.\n */\nfunction renderStyleTransferProperty(\n providerId: string,\n options: {\n cesdk?: CreativeEditorSDK;\n /**\n * Base URL used for the UI assets used in the plugin.\n */\n baseURL?: string;\n\n /**\n * What property key to use for the style property.\n */\n propertyKey?: string;\n\n /**\n * What property key to use for the prompt property.\n */\n propertyKeyForPrompt?: string;\n\n /**\n * Override the default styles\n */\n styles?: Style[] | ((defaultStyles: Style[]) => Style[]);\n\n /**\n * Overrides the default i18n translations for the prompt input.\n */\n i18n?: {\n prompt?: {\n inputLabel?: string;\n placeholder?: string;\n };\n };\n }\n): RenderCustomProperty {\n const { cesdk } = options;\n if (cesdk == null) return {};\n\n const propertyKey = options.propertyKey ?? 'style';\n const panelIdForStyleSelection = getStyleSelectionPanelId(providerId);\n\n cesdk.i18n.setTranslations({\n en: {\n [`panel.${panelIdForStyleSelection}`]: 'Select Style',\n [`${providerId}.${propertyKey}`]: 'Style',\n 'ly.img.ai.styleTransfer.none': 'None',\n 'ly.img.ai.styleTransfer.anime': 'Anime',\n 'ly.img.ai.styleTransfer.cyberpunk': 'Cyberpunk',\n 'ly.img.ai.styleTransfer.kodak400': 'Kodak 400',\n 'ly.img.ai.styleTransfer.watercolor': 'Watercolor',\n 'ly.img.ai.styleTransfer.darkFantasy': 'Dark Fantasy',\n 'ly.img.ai.styleTransfer.vaporwave': 'Vaporwave',\n 'ly.img.ai.styleTransfer.vectorFlat': 'Vector Flat',\n 'ly.img.ai.styleTransfer.3dAnimation': '3D Animation',\n 'ly.img.ai.styleTransfer.ukiyoe': 'Ukiyo\u2011e',\n 'ly.img.ai.styleTransfer.surreal': 'Surreal',\n 'ly.img.ai.styleTransfer.steampunk': 'Steampunk',\n 'ly.img.ai.styleTransfer.nightBokeh': 'Night Bokeh',\n 'ly.img.ai.styleTransfer.popArt': 'Pop Art'\n }\n });\n\n const defaultStyles = getDefaultStyles({\n baseURL:\n options.baseURL ??\n 'https://cdn.img.ly/assets/plugins/plugin-ai-image-generation-web/v1/gpt-image-1/',\n includeNone: true,\n cesdk\n });\n let styles = defaultStyles;\n if (options.styles != null) {\n if (Array.isArray(options.styles)) {\n styles = options.styles;\n } else if (typeof options.styles === 'function') {\n styles = options.styles(defaultStyles);\n }\n }\n const styleAssetSource = createStyleAssetSource({\n cesdk,\n providerId,\n styles\n });\n const styleAssetSourceId = styleAssetSource.id;\n addStyleAssetSource(styleAssetSource, { cesdk });\n\n createPanels({\n providerId,\n cesdk,\n panelId: panelIdForStyleSelection,\n entryId: styleAssetSourceId\n });\n\n const customProperties: RenderCustomProperty = {\n [options.propertyKeyForPrompt ?? 'prompt']: (context, property) => {\n const promptState = context.state<string>('prompt', '');\n context.builder.TextArea(`${property.id}`, {\n inputLabel:\n options.i18n?.prompt?.inputLabel ??\n options.propertyKeyForPrompt ??\n 'prompt',\n placeholder: options.i18n?.prompt?.placeholder,\n ...promptState\n });\n\n return () => {\n const [activeAssetId] = styleAssetSource.getActiveAssetIds();\n const asset = styleAssetSource.getAsset(activeAssetId);\n return {\n id: property.id,\n type: 'string',\n value:\n asset?.meta?.prompt == null\n ? promptState.value\n : `${promptState.value}; ${asset.meta.prompt}`\n };\n };\n },\n [propertyKey]: (context, property) => {\n const { builder, state } = context;\n\n if (styles.length > 0) {\n const styleState = state<{\n id: string;\n label: string;\n }>('style', styles[0]);\n\n builder.Button(`${property.id}`, {\n inputLabel: `${providerId}.${property.id}`,\n icon: '@imgly/Appearance',\n isDisabled: styles.length === 0,\n trailingIcon: '@imgly/ChevronRight',\n label: styleState.value.label,\n labelAlignment: 'left',\n onClick: () => {\n const payload: StyleSelectionPayload = {\n onSelect: async (asset) => {\n styleAssetSource.clearActiveAssets();\n styleAssetSource.setAssetActive(asset.id);\n styleState.setValue({\n id: asset.id,\n label: asset.label ?? asset.id\n });\n cesdk.ui.closePanel(panelIdForStyleSelection);\n }\n };\n\n cesdk.ui.openPanel(panelIdForStyleSelection, {\n payload\n });\n }\n });\n return () => {\n return {\n id: property.id,\n type: 'string',\n value: styleState.value.id\n };\n };\n }\n\n return () => {\n return {\n id: property.id,\n type: 'string',\n value: 'none'\n };\n };\n }\n };\n\n return customProperties;\n}\n\nfunction createPanels(options: {\n providerId: string;\n panelId: string;\n entryId: string;\n cesdk?: CreativeEditorSDK;\n}) {\n const { providerId, cesdk, panelId, entryId } = options;\n if (cesdk == null) return;\n\n cesdk.ui.registerPanel<{\n onSelect: (assetResult: AssetResult) => void;\n }>(panelId, ({ builder, payload }) => {\n if (payload?.onSelect == null) {\n builder.Section(`${providerId}.error`, {\n children: () => {\n builder.Text('error', {\n content:\n 'No onSelect function provided for the style selection panel.'\n });\n }\n });\n }\n builder.Library(`${providerId}.library.image`, {\n entries: [entryId],\n onSelect: async (asset) => {\n payload?.onSelect?.(asset);\n }\n });\n });\n}\n\nfunction getStyleSelectionPanelId(providerId: string) {\n return `ly.img.ai.${providerId}.styleSelection`;\n}\n\nconst STYLES: (Omit<Style, 'thumbUri' | 'label'> & { labelKey: string })[] = [\n {\n id: 'none',\n labelKey: 'ly.img.ai.styleTransfer.none',\n prompt: ''\n },\n {\n id: 'anime-celshaded',\n labelKey: 'ly.img.ai.styleTransfer.anime',\n prompt:\n 'anime cel\u2011shaded, bright pastel palette, expressive eyes, clean line art '\n },\n {\n id: 'cyberpunk-neon',\n labelKey: 'ly.img.ai.styleTransfer.cyberpunk',\n prompt:\n 'cyberpunk cityscape, glowing neon signage, reflective puddles, dark atmosphere'\n },\n {\n id: 'kodak-portra-400',\n labelKey: 'ly.img.ai.styleTransfer.kodak400',\n prompt:\n 'shot on Kodak Portra 400, soft grain, golden\u2011hour warmth, 35 mm photo'\n },\n {\n id: 'watercolor-storybook',\n labelKey: 'ly.img.ai.styleTransfer.watercolor',\n prompt: 'loose watercolor washes, gentle gradients, dreamy storybook feel'\n },\n {\n id: 'dark-fantasy-realism',\n labelKey: 'ly.img.ai.styleTransfer.darkFantasy',\n prompt:\n 'dark fantasy realm, moody chiaroscuro lighting, hyper\u2011real textures'\n },\n {\n id: 'vaporwave-retrofuturism',\n labelKey: 'ly.img.ai.styleTransfer.vaporwave',\n prompt:\n 'retro\u2011futuristic vaporwave, pastel sunset gradient, chrome text, VHS scanlines'\n },\n {\n id: 'minimal-vector-flat',\n labelKey: 'ly.img.ai.styleTransfer.vectorFlat',\n prompt:\n 'minimalist flat vector illustration, bold geometry, two\u2011tone palette'\n },\n {\n id: 'pixarstyle-3d-render',\n labelKey: 'ly.img.ai.styleTransfer.3dAnimation',\n prompt:\n 'Pixar\u2011style 3D render, oversized eyes, subtle subsurface scattering, cinematic lighting'\n },\n {\n id: 'ukiyoe-woodblock',\n labelKey: 'ly.img.ai.styleTransfer.ukiyoe',\n prompt:\n 'ukiyo\u2011e woodblock print, Edo\u2011period style, visible washi texture, limited color ink'\n },\n {\n id: 'surreal-dreamscape',\n labelKey: 'ly.img.ai.styleTransfer.surreal',\n prompt:\n 'surreal dreamscape, floating objects, impossible architecture, vivid clouds'\n },\n {\n id: 'steampunk-victorian',\n labelKey: 'ly.img.ai.styleTransfer.steampunk',\n prompt:\n 'Victorian steampunk world, ornate brass gears, leather attire, atmospheric fog'\n },\n {\n id: 'nightstreet-photo-bokeh',\n labelKey: 'ly.img.ai.styleTransfer.nightBokeh',\n prompt:\n 'night\u2011time street shot, large aperture bokeh lights, candid urban mood'\n },\n {\n id: 'comicbook-pop-art',\n labelKey: 'ly.img.ai.styleTransfer.popArt',\n prompt:\n 'classic comic\u2011book panel, halftone shading, exaggerated action lines, CMYK pop colors'\n }\n];\n\nfunction getDefaultStyles(options: {\n baseURL: string;\n includeNone?: boolean;\n cesdk?: CreativeEditorSDK;\n}): Style[] {\n return STYLES.map((style) => {\n if (style.id === 'none') {\n if (!options.includeNone) {\n return undefined;\n }\n return {\n id: style.id,\n label: translateWithFallback(\n options.cesdk,\n style.labelKey,\n style.labelKey\n ),\n prompt: style.prompt,\n thumbUri: `${options.baseURL}/thumbnails/None.svg`\n };\n }\n return {\n id: style.id,\n label: translateWithFallback(\n options.cesdk,\n style.labelKey,\n style.labelKey\n ),\n prompt: style.prompt,\n thumbUri: `${options.baseURL}/thumbnails/${style.id}.jpeg`\n };\n }).filter(isDefined);\n}\n\nconst createStyleAssetSource = (options: {\n cesdk: CreativeEditorSDK;\n providerId: string;\n styles: Style[];\n}) => {\n const styleValues: SelectValue[] = options.styles.map((style) => {\n return {\n ...style,\n meta: { prompt: style.prompt }\n };\n });\n\n const allSourceIds = options.cesdk.engine.asset.findAllSources();\n let assetSourceId = `${options.providerId}/styles`;\n while (allSourceIds.includes(assetSourceId)) {\n assetSourceId += `-${Math.random().toString(36).substring(2, 5)}`;\n }\n const styleAssetSource = new CustomAssetSource(assetSourceId, styleValues);\n\n const defaultStyle = options.styles[0];\n styleAssetSource.setAssetActive(defaultStyle.id);\n\n return styleAssetSource;\n};\n\nconst addStyleAssetSource = (\n styleAssetSource: CustomAssetSource,\n options: {\n cesdk: CreativeEditorSDK;\n }\n) => {\n options.cesdk.engine.asset.addSource(styleAssetSource);\n options.cesdk.ui.addAssetLibraryEntry({\n id: styleAssetSource.id,\n sourceIds: [styleAssetSource.id],\n gridItemHeight: 'square',\n gridBackgroundType: 'cover',\n cardLabel: ({ label }) => label,\n cardLabelPosition: () => 'below'\n });\n};\n\nexport default renderStyleTransferProperty;\n", "import type { CreativeEngine } from '@cesdk/cesdk-js';\nimport type CreativeEditorSDK from '@cesdk/cesdk-js';\nimport type { PropertyContext } from '../core/propertyConfiguration';\n\n/**\n * Build the base property context from available sources\n */\nexport function buildPropertyContext(\n engine: CreativeEngine,\n cesdk?: CreativeEditorSDK\n): PropertyContext {\n // Get locale from cesdk or default to 'en'\n const locale = cesdk?.i18n?.getLocale?.() || 'en';\n\n return {\n engine,\n cesdk,\n locale\n };\n}\n\n/**\n * Context cache for performance optimization\n */\nexport class PropertyContextCache {\n private cache: PropertyContext | null = null;\n\n /**\n * Get cached context or build new one\n */\n getContext(\n engine: CreativeEngine,\n cesdk?: CreativeEditorSDK\n ): PropertyContext {\n if (!this.cache) {\n this.cache = buildPropertyContext(engine, cesdk);\n }\n return this.cache;\n }\n\n /**\n * Clear the cache (e.g., when locale changes)\n */\n clear(): void {\n this.cache = null;\n }\n}\n", "import type {\n PropertyConfig,\n PropertyContext\n} from '../core/propertyConfiguration';\n\n/**\n * Resolve the default value for a property\n * @template T - The property value type\n * @template C - The context type\n */\nexport function resolvePropertyDefault<\n T,\n C extends PropertyContext = PropertyContext\n>(\n propertyId: string,\n propertyConfig: PropertyConfig<T, C> | undefined,\n context: C,\n schemaDefault?: T,\n fallback?: T\n): T | undefined {\n // 1. Check property configuration\n if (propertyConfig?.default !== undefined) {\n const defaultValue = propertyConfig.default;\n\n // 2a. Static value\n if (typeof defaultValue !== 'function') {\n return defaultValue;\n }\n\n // 2b. Dynamic value - call function with context\n return (defaultValue as (context: C) => T)(context);\n }\n\n // 3. Schema default\n if (schemaDefault !== undefined) {\n return schemaDefault;\n }\n\n // 4. Fallback\n return fallback;\n}\n\n/**\n * Batch resolve multiple property defaults\n */\nexport function resolvePropertyDefaults<\n I,\n C extends PropertyContext = PropertyContext\n>(\n properties: Array<{\n id: keyof I;\n config?: PropertyConfig<I[keyof I], C>;\n schemaDefault?: I[keyof I];\n fallback?: I[keyof I];\n }>,\n context: C\n): Partial<I> {\n const resolved: Partial<I> = {};\n\n for (const prop of properties) {\n const value = resolvePropertyDefault(\n prop.id as string,\n prop.config,\n context,\n prop.schemaDefault,\n prop.fallback\n );\n\n if (value !== undefined) {\n resolved[prop.id] = value;\n }\n }\n\n return resolved;\n}\n", "import { OutputKind } from '../core/provider';\nimport CreativeEditorSDK from '@cesdk/cesdk-js';\n\n/**\n * Integrates the asset sources into the default asset library entry for the\n * given kind.\n */\nfunction integrateIntoDefaultAssetLibraryEntry<K extends OutputKind>(\n kind: K,\n historAssetSourceIds: string[],\n cesdk: CreativeEditorSDK\n): string | undefined {\n const entryId = `ly.img.${kind}`;\n const entry = cesdk.ui.getAssetLibraryEntry(entryId);\n if (entry != null) {\n cesdk.ui.updateAssetLibraryEntry(entryId, {\n sourceIds: [...entry.sourceIds, ...historAssetSourceIds]\n });\n return entry.id;\n }\n}\n\nexport default integrateIntoDefaultAssetLibraryEntry;\n", "import {\n BuilderRenderFunctionContext,\n CreativeEngine,\n SceneMode,\n Scope\n} from '@cesdk/cesdk-js';\nimport { OutputKind, Output } from './provider';\nimport { Result } from '../generation/createGenerateFunction';\n\n/**\n * Base properties shared by all action definitions.\n */\ninterface BaseActionDefinition {\n /** Unique identifier for the action */\n id: string;\n /** Human-readable label for the action */\n label?: string;\n /** Detailed description of what the action does */\n description?: string;\n /** Optional metadata for additional information */\n meta?: Record<string, any>;\n /** The scene mode for which this action is only applicable */\n sceneMode?: SceneMode;\n}\n\n/**\n * Definition for a plugin action - a global action that can be invoked from apps, command palettes, etc.\n */\nexport interface PluginActionDefinition extends BaseActionDefinition {\n /** Action type discriminator */\n type: 'plugin';\n /** ID of the plugin that registered this action */\n pluginId: string;\n /** Function to execute the action */\n execute: () => void;\n}\n\n/**\n * Render context for quick actions with generation capability.\n */\nexport interface QuickActionRenderContext<Q = Record<string, any>> {\n /** Toggle between collapsed and expanded state */\n toggleExpand: () => void;\n /** Whether the quick action is currently expanded */\n isExpanded: boolean;\n /** Close the entire quick action popover */\n close: () => void;\n /** Generate output using the quick action input */\n generate: (\n input: Q,\n options?: { blockIds?: number[] }\n ) => Promise<Result<Output>>;\n /** The ID of the provider used for this quick action */\n providerId: string;\n}\n\n/**\n * Definition for a quick action - a context-sensitive action that operates on selected blocks.\n */\nexport interface QuickActionDefinition<Q extends Record<string, any>>\n extends BaseActionDefinition {\n /** Action type discriminator */\n type: 'quick';\n /** The kind of block this action operates on */\n kind: OutputKind;\n /**\n * Defines if the quick action is enabled or not by using the\n * feature api.\n */\n enable: boolean | ((context: { engine: CreativeEngine }) => boolean);\n /**\n * Define the necessary scopes for this quick action.\n */\n scopes?: Scope[];\n\n /**\n * Overrides the defaults for this quick action.\n */\n defaults?: {\n /** Should the generation be directly applied or does it need confirmation */\n confirmation?: boolean;\n /** Whether the block should be locked */\n lock?: boolean;\n };\n\n /** Render function for the quick action UI */\n render: (\n context: BuilderRenderFunctionContext<any> & QuickActionRenderContext<Q>\n ) => void;\n}\n\n/**\n * Union type of all supported action definitions.\n */\nexport type ActionDefinition =\n | PluginActionDefinition\n | QuickActionDefinition<Record<string, any>>;\n\n/**\n * Event types for ActionRegistry subscriptions.\n */\nexport type ActionRegistryEventType = 'registered' | 'unregistered';\n\n/**\n * Callback function for ActionRegistry subscriptions.\n */\nexport type ActionRegistrySubscriberCallback = (\n action: ActionDefinition,\n event: ActionRegistryEventType\n) => void;\n\n/**\n * Filters for querying and subscribing to specific types of actions.\n * Used by both getBy() and subscribeBy() methods.\n */\nexport interface ActionRegistryFilters {\n /** Filter by action type */\n type?: ActionDefinition['type'];\n /** Filter by plugin ID */\n pluginId?: string;\n /** Filter by action ID */\n id?: string;\n /** Filter by kind (only applicable for quick actions) */\n kind?: OutputKind;\n}\n\n/**\n * Global registry for managing plugin actions and quick actions.\n * Uses singleton pattern to ensure a single source of truth across the application.\n */\nexport class ActionRegistry {\n /** Map storing all registered actions by their ID */\n private actions: Map<string, ActionDefinition> = new Map();\n\n /** Map storing subscribers with their filters (null = subscribe to all) */\n private subscribers: Map<\n ActionRegistrySubscriberCallback,\n ActionRegistryFilters | null\n > = new Map();\n\n // eslint-disable-next-line @typescript-eslint/no-empty-function\n private constructor() {}\n\n /**\n * Gets the singleton instance of the ActionRegistry.\n * Uses global object storage to ensure singleton across different bundle contexts.\n * @returns The ActionRegistry instance\n */\n public static get(): ActionRegistry {\n const globalKey = '__imgly_action_registry__';\n const globalObj = (\n typeof window !== 'undefined' ? window : globalThis\n ) as any;\n\n if (!globalObj[globalKey]) {\n globalObj[globalKey] = new ActionRegistry();\n }\n return globalObj[globalKey];\n }\n\n /**\n * Registers an action in the registry.\n * @param action The action definition to register\n * @returns A disposer function that unregisters the action when called\n */\n public register(action: ActionDefinition): () => void {\n this.actions.set(action.id, action);\n this.notifySubscribers(action, 'registered');\n\n return () => {\n if (this.actions.get(action.id) === action) {\n this.actions.delete(action.id);\n this.notifySubscribers(action, 'unregistered');\n }\n };\n }\n\n /**\n * Gets all registered actions.\n * @returns Array of all action definitions\n */\n public getAll(): ActionDefinition[] {\n return Array.from(this.actions.values());\n }\n\n /**\n * Gets actions matching the specified filters with full type safety.\n * @param filters Object containing optional filters for type, pluginId, and id\n * @returns Array of matching actions, typed based on the type filter\n * @example\n * // Get all plugin actions\n * registry.getBy({ type: 'plugin' }) // Returns PluginActionDefinition[]\n *\n * // Get all actions from a specific plugin\n * registry.getBy({ pluginId: 'ai-image-generation' }) // Returns ActionDefinition[]\n *\n * // Get specific action by ID\n * registry.getBy({ id: 'generate-image' }) // Returns ActionDefinition[]\n *\n * // Combine filters\n * registry.getBy({ type: 'quick', pluginId: 'ai-text' }) // Returns QuickActionDefinition[]\n *\n * // Filter quick actions by kind\n * registry.getBy({ type: 'quick', kind: 'image' }) // Returns QuickActionDefinition[]\n */\n public getBy<\n T extends ActionDefinition['type'] | undefined = undefined\n >(filters: {\n /** Filter by action type */\n type?: T;\n /** Filter by plugin ID */\n pluginId?: string;\n /** Filter by action ID */\n id?: string;\n /** Filter by kind (only applicable for quick actions) */\n kind?: OutputKind;\n }): T extends ActionDefinition['type']\n ? Extract<ActionDefinition, { type: T }>[]\n : ActionDefinition[] {\n const results = this.getAll().filter((action) =>\n this.matchesFilters(action, filters)\n );\n\n return results as T extends ActionDefinition['type']\n ? Extract<ActionDefinition, { type: T }>[]\n : ActionDefinition[];\n }\n\n /**\n * Subscribes to all registry events (register/unregister).\n * @param callback Function to call when any action is registered or unregistered\n * @returns Unsubscribe function\n */\n public subscribe(callback: ActionRegistrySubscriberCallback): () => void {\n this.subscribers.set(callback, null);\n return () => {\n this.subscribers.delete(callback);\n };\n }\n\n /**\n * Subscribes to registry events for actions matching the specified filters.\n * @param filters Filters to match actions against\n * @param callback Function to call when matching actions are registered or unregistered\n * @returns Unsubscribe function\n * @example\n * // Subscribe to plugin actions only\n * registry.subscribeBy({ type: 'plugin' }, (action, event) => {\n * console.log(`Plugin action ${action.id} was ${event}`);\n * });\n *\n * // Subscribe to actions from specific plugin\n * registry.subscribeBy({ pluginId: 'ai-image' }, (action, event) => {\n * updateUIForPlugin(action, event);\n * });\n */\n public subscribeBy(\n filters: ActionRegistryFilters,\n callback: ActionRegistrySubscriberCallback\n ): () => void {\n this.subscribers.set(callback, filters);\n return () => {\n this.subscribers.delete(callback);\n };\n }\n\n /**\n * Notifies all relevant subscribers about an action event.\n * @param action The action that was registered or unregistered\n * @param event The type of event that occurred\n */\n private notifySubscribers(\n action: ActionDefinition,\n event: ActionRegistryEventType\n ): void {\n this.subscribers.forEach((filters, callback) => {\n // If no filters (null), notify for all actions\n if (filters === null) {\n callback(action, event);\n return;\n }\n\n // Check if action matches the filters\n if (this.matchesFilters(action, filters)) {\n callback(action, event);\n }\n });\n }\n\n /**\n * Checks if an action matches the given filters.\n * Used by both getBy() and subscribeBy() methods.\n * @param action The action to check\n * @param filters The filters to match against\n * @returns True if the action matches all filters\n */\n private matchesFilters(\n action: ActionDefinition,\n filters: ActionRegistryFilters\n ): boolean {\n if (filters.type && action.type !== filters.type) return false;\n if (\n filters.pluginId &&\n action.type === 'plugin' &&\n action.pluginId !== filters.pluginId\n )\n return false;\n if (filters.id && action.id !== filters.id) return false;\n if (filters.kind) {\n // Kind filter only applies to quick actions\n if (action.type !== 'quick') return false;\n if (action.kind !== filters.kind) return false;\n }\n return true;\n }\n}\n", "import { ProviderInitializationResult } from '../providers/initializeProvider';\nimport { OutputKind } from './provider';\n\n/**\n * Global registry for managing AI generation providers across all plugins.\n * Uses singleton pattern to ensure cross-plugin provider discovery.\n */\nexport class ProviderRegistry {\n /** Map storing all registered providers by their ID */\n private providers: Map<string, ProviderInitializationResult<any, any, any>> =\n new Map();\n\n // eslint-disable-next-line @typescript-eslint/no-empty-function\n private constructor() {}\n\n /**\n * Gets the singleton instance of the ProviderRegistry.\n * Uses global object storage to ensure singleton across different bundle contexts.\n * @returns The ProviderRegistry instance\n */\n public static get(): ProviderRegistry {\n const globalKey = '__imgly_provider_registry__';\n const globalObj = (\n typeof window !== 'undefined' ? window : globalThis\n ) as any;\n\n if (!globalObj[globalKey]) {\n globalObj[globalKey] = new ProviderRegistry();\n }\n return globalObj[globalKey];\n }\n\n /**\n * Registers a provider in the registry.\n * @param providerInitializationResult The provider to register\n * @returns A disposer function that unregisters the provider when called\n */\n public register(\n providerInitializationResult: ProviderInitializationResult<any, any, any>\n ): () => void {\n if (this.providers.has(providerInitializationResult.provider.id)) {\n // eslint-disable-next-line no-console\n console.warn(\n `Provider with ID \"${providerInitializationResult.provider.id}\" is already registered`\n );\n }\n\n this.providers.set(\n providerInitializationResult.provider.id,\n providerInitializationResult\n );\n\n return () => {\n if (\n this.providers.get(providerInitializationResult.provider.id) ===\n providerInitializationResult\n ) {\n this.providers.delete(providerInitializationResult.provider.id);\n }\n };\n }\n\n /**\n * Gets all registered providers.\n * @returns Array of all provider instances\n */\n public getAll(): ProviderInitializationResult<any, any, any>[] {\n return Array.from(this.providers.values());\n }\n\n /**\n * Gets a provider by its ID.\n * @param id The provider ID to look up\n * @returns The provider instance or undefined if not found\n */\n public getById(\n id: string\n ): ProviderInitializationResult<any, any, any> | undefined {\n return this.providers.get(id);\n }\n\n /**\n * Gets all providers of a specific kind.\n * @param kind The output kind to filter by\n * @returns Array of providers matching the specified kind\n */\n public getByKind<K extends OutputKind>(\n kind: K\n ): ProviderInitializationResult<K, any, any>[] {\n return this.getAll().filter(({ provider }) => provider.kind === kind);\n }\n}\n", "import { GenerationOptions, GenerationResult, Output } from '../core/provider';\n\n/**\n * Result of the generation with a dispose function to clean up\n */\nexport interface DisposableGenerationResult<O extends Output> {\n /**\n * The actual generation result\n */\n result: GenerationResult<O>;\n\n /**\n * Function to dispose/clean up resources created during generation\n * This should be called when the generation is cancelled or completely\n * finished including the confirmation of the generation if applicable.\n */\n dispose: () => Promise<void>;\n}\n\n/**\n * Define the type for middleware functions\n */\nexport type Middleware<I, O extends Output> = (\n input: I,\n options: GenerationOptions & {\n /**\n * Adds a disposer function to this generation which is called\n * when the generation is cancelled or completely finished\n * including the confirmation of the generation if applicable.\n */\n addDisposer: (dispose: () => Promise<void>) => void;\n },\n next: (input: I, options: GenerationOptions) => Promise<GenerationResult<O>>\n) => Promise<GenerationResult<O>>;\n\nexport function composeMiddlewares<I, O extends Output>(\n middlewares: (Middleware<I, O> | false | undefined | null)[]\n) {\n // Filter out false, undefined, and null middlewares\n const validMiddlewares = middlewares.filter(\n (middleware): middleware is Middleware<I, O> => !!middleware\n );\n // Start with the base handler\n return (\n baseHandler: (\n input: I,\n options: GenerationOptions\n ) => Promise<GenerationResult<O>>\n ) => {\n // We need to build a chain where each step is a function with the signature:\n // (input, options) => Promise<Result>\n\n // The composed function that will be returned\n return async (\n input: I,\n options: GenerationOptions\n ): Promise<DisposableGenerationResult<O>> => {\n // Store disposer functions that will be called when dispose() is called\n const disposers: Array<() => Promise<void>> = [];\n\n // Function to add a disposer\n const addDisposer = (dispose: () => Promise<void>) => {\n disposers.push(dispose);\n };\n\n // Define a function to process each middleware in sequence\n const runMiddleware = async (\n index: number,\n currentInput: I,\n currentOptions: GenerationOptions\n ): Promise<GenerationResult<O>> => {\n // If we've processed all middlewares, call the base handler\n if (index >= validMiddlewares.length) {\n return baseHandler(currentInput, currentOptions);\n }\n\n // Get the current middleware\n const currentMiddleware = validMiddlewares[index];\n\n // Create a next function for this middleware that calls the next middleware in line\n const next = async (\n nextInput: I,\n nextOptions: GenerationOptions\n ): Promise<GenerationResult<O>> => {\n return runMiddleware(index + 1, nextInput, nextOptions);\n };\n\n // Enhanced options with addDisposer\n const enhancedOptions = {\n ...currentOptions,\n addDisposer\n };\n\n // Call the current middleware with the input, enhanced options, and next function\n return currentMiddleware(currentInput, enhancedOptions, next);\n };\n\n // Create enhanced options with addDisposer for base handler as well\n const enhancedOptions = {\n ...options,\n addDisposer\n };\n\n // Run the middleware chain\n const result = await runMiddleware(0, input, enhancedOptions);\n\n // Create the dispose function that will call all collected disposers in reverse order\n const dispose = async (): Promise<void> => {\n // Execute disposers in reverse order (last added, first disposed)\n /* eslint-disable no-await-in-loop */\n for (let i = disposers.length - 1; i >= 0; i--) {\n try {\n await disposers[i]();\n } catch (error) {\n // eslint-disable-next-line no-console\n console.error('Error in disposer:', error);\n }\n }\n // Clear the disposers array after all are executed\n disposers.length = 0;\n };\n\n // Return both the result and the dispose function\n return {\n result,\n dispose\n };\n };\n };\n}\n", "/* eslint-disable no-console */\nimport { GenerationResult, Output } from '../core/provider';\nimport { Middleware } from './middleware';\n\nfunction loggingMiddleware<I, O extends Output>({\n enable = true\n}: {\n enable: boolean | undefined;\n}) {\n const middleware: Middleware<I, O> = async (input, options, next) => {\n if (!enable) return next(input, options);\n\n console.group('[GENERATION]');\n console.log(`Generating with input:`, JSON.stringify(input, null, 2));\n let result: GenerationResult<O> | undefined;\n const start = Date.now();\n try {\n result = await next(input, options);\n return result;\n } finally {\n if (result != null) {\n console.log(`Generation took ${Date.now() - start}ms`);\n console.log(`Generation result:`, JSON.stringify(result, null, 2));\n }\n console.groupEnd();\n }\n };\n\n return middleware;\n}\n\nexport default loggingMiddleware;\n", "import CreativeEditorSDK, { AssetResult } from '@cesdk/cesdk-js';\nimport { GenerationResult, Output } from '../core/provider';\n\nexport const AI_PANEL_ID_PREFIX = 'ly.img.ai';\n\nconst TEMP_ASSET_SOURCE_ID = 'ly.img.ai.temp';\n\n/**\n * Adding asset to the scene.\n *\n * NOTE: Will use a temporary asset source so that\n * our asset source middleware trigger. This is necessary since there is\n * a lot of extra logic in the video middlewares regarding trim, position etc.\n *\n * These will only trigger via an asset source, not by calling\n * `defaultApplyAsset` directly.\n */\nexport async function addAssetToScene(\n cesdk: CreativeEditorSDK,\n assetResult: AssetResult\n) {\n if (!cesdk.engine.asset.findAllSources().includes(TEMP_ASSET_SOURCE_ID)) {\n cesdk.engine.asset.addLocalSource(TEMP_ASSET_SOURCE_ID);\n }\n\n return cesdk.engine.asset.apply(TEMP_ASSET_SOURCE_ID, assetResult);\n}\n\n/**\n * Returns a consistent panel ID for a provider ID\n */\nexport function getPanelId(providerId: string): string {\n return `${AI_PANEL_ID_PREFIX}.${providerId}`;\n}\n\nexport default getPanelId;\n/**\n * Extracts a readable error message from an unknown error\n *\n * @param error The error caught in a try/catch block\n * @param fallbackMessage Optional fallback message if error is not an Error object\n * @returns A string representation of the error\n */\nexport function extractErrorMessage(\n error: unknown,\n fallbackMessage = 'We encountered an unknown error while generating the asset. Please try again.'\n): string {\n if (error === null) {\n return fallbackMessage;\n }\n\n if (error instanceof Error) {\n return error.message;\n }\n\n if (typeof error === 'object') {\n // Try to get message property if it exists\n const errorObj = error as Record<string, unknown>;\n if ('message' in errorObj && typeof errorObj.message === 'string') {\n return errorObj.message;\n }\n if ('cause' in errorObj && typeof errorObj.cause === 'string') {\n return errorObj.cause;\n }\n\n /*\n * Elevenlabs for instance uses the following structure for errors:\n * {\n * \"detail\": {\n * \"status\": \"error_code\",\n * \"message\": \"Explanation of the rate limit issue.\"\n * }\n * }\n */\n if (\n 'detail' in errorObj &&\n typeof errorObj.detail === 'object' &&\n errorObj.detail !== null &&\n 'message' in errorObj.detail &&\n typeof errorObj.detail.message === 'string'\n ) {\n return errorObj.detail.message;\n }\n\n /*\n * Used by e.g. Anthropic\n */\n if (\n 'error' in errorObj &&\n typeof errorObj.error === 'object' &&\n errorObj.error !== null &&\n 'message' in errorObj.error &&\n typeof errorObj.error.message === 'string'\n ) {\n return errorObj.error.message;\n }\n\n return fallbackMessage;\n }\n\n if (typeof error === 'string') {\n return error;\n }\n\n // For any other type, convert to string\n return String(error) || fallbackMessage;\n}\n\n/**\n * Generates a random UUID v4\n */\nexport function uuid4() {\n /* eslint-disable no-bitwise */\n return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {\n const r = (Math.random() * 16) | 0;\n const v = c === 'x' ? r : (r & 0x3) | 0x8;\n return v.toString(16);\n /* eslint-enable no-bitwise */\n });\n}\n\n/**\n * Gets the duration of a video from a URL\n * @param url - The URL of the video\n * @returns A promise that resolves to the duration of the video in seconds\n */\nexport function getDurationForVideo(url: string): Promise<number> {\n return new Promise((resolve, reject) => {\n try {\n const video = document.createElement('video');\n video.style.display = 'none';\n\n // Set up event handlers\n video.addEventListener('loadedmetadata', () => {\n if (video.duration === Infinity) {\n // Some videos might initially report Infinity\n video.currentTime = 1e101;\n // Wait for currentTime to update\n setTimeout(() => {\n video.currentTime = 0;\n resolve(video.duration);\n document.body.removeChild(video);\n }, 50);\n } else {\n resolve(video.duration);\n document.body.removeChild(video);\n }\n });\n\n video.addEventListener('error', () => {\n document.body.removeChild(video);\n reject(new Error(`Failed to load video from ${url}`));\n });\n\n // Set source and begin loading\n video.src = url;\n document.body.appendChild(video);\n } catch (error) {\n reject(error);\n }\n });\n}\n\n/**\n * Gets a thumbnail image from a video URL\n * @param url - The URL of the video\n * @param seekTime - Time in seconds to capture the thumbnail (default: 0)\n * @param format - Image format for the thumbnail (default: 'image/jpeg')\n * @param quality - Image quality between 0 and 1 (default: 0.8)\n * @returns A promise that resolves to the thumbnail data URL\n */\nexport function getThumbnailForVideo(\n url: string,\n seekTime = 0,\n format = 'image/jpeg',\n quality = 0.8\n): Promise<string> {\n return new Promise((resolve, reject) => {\n try {\n const video = document.createElement('video');\n // Set crossOrigin to anonymous to prevent tainted canvas issues\n video.crossOrigin = 'anonymous';\n video.style.display = 'none';\n\n // Set up event handlers\n video.addEventListener('loadedmetadata', () => {\n // Seek to the specified time\n video.currentTime = Math.min(seekTime, video.duration);\n\n video.addEventListener(\n 'seeked',\n () => {\n // Create a canvas to draw the video frame\n const canvas = document.createElement('canvas');\n canvas.width = video.videoWidth;\n canvas.height = video.videoHeight;\n\n // Draw the video frame to the canvas\n const ctx = canvas.getContext('2d');\n if (!ctx) {\n document.body.removeChild(video);\n reject(new Error('Failed to create canvas context'));\n return;\n }\n\n ctx.drawImage(video, 0, 0, canvas.width, canvas.height);\n\n try {\n // Convert canvas to data URL\n const dataURL = canvas.toDataURL(format, quality);\n // Clean up\n document.body.removeChild(video);\n resolve(dataURL);\n } catch (e) {\n document.body.removeChild(video);\n reject(\n new Error(\n `Failed to create thumbnail: ${\n e instanceof Error ? e.message : String(e)\n }`\n )\n );\n }\n },\n { once: true }\n );\n });\n\n video.addEventListener('error', () => {\n document.body.removeChild(video);\n reject(new Error(`Failed to load video from ${url}`));\n });\n\n // Set source and begin loading\n video.src = url;\n document.body.appendChild(video);\n } catch (error) {\n reject(error);\n }\n });\n}\n\n/**\n * Converts an ID string to a human-readable label\n * @param id - The ID string to convert\n * @returns A human-readable label derived from the ID\n *\n * Examples:\n * - snake_case_id \u2192 Snake Case Id\n * - kebab-case-id \u2192 Kebab Case Id\n * - camelCaseId \u2192 Camel Case Id\n * - PascalCaseId \u2192 Pascal Case Id\n */\nexport function getLabelFromId(id: string): string {\n if (!id) return '';\n\n // Handle snake_case, kebab-case, camelCase, and PascalCase\n return (\n id\n // Replace underscores and hyphens with spaces (for snake_case and kebab-case)\n .replace(/[_-]/g, ' ')\n // Add spaces before uppercase letters (for camelCase and PascalCase)\n .replace(/([A-Z])/g, ' $1')\n // Trim any extra spaces and ensure first letter is capitalized\n .trim()\n .split(' ')\n .filter((word) => word.length > 0) // Remove empty strings from multiple spaces\n .map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())\n .join(' ')\n );\n}\n\n/**\n * Type guard to check if a value is an AsyncGenerator rather than a Promise\n *\n * @param value - Value of type Promise<O> | AsyncGenerator<O, C>\n * @returns Boolean indicating if the value is an AsyncGenerator\n */\nexport function isAsyncGenerator<O extends Output, C>(\n value: GenerationResult<O, C>\n): value is AsyncGenerator<O, C> {\n return (\n typeof value === 'object' &&\n value !== null &&\n 'next' in value &&\n 'return' in value &&\n 'throw' in value &&\n typeof value.next === 'function' &&\n typeof value.return === 'function' &&\n typeof value.throw === 'function' &&\n Symbol.asyncIterator in value &&\n typeof value[Symbol.asyncIterator] === 'function'\n );\n}\n\nexport function isAbortError(error: unknown): error is Error {\n return error instanceof Error && error.name === 'AbortError';\n}\n\n/**\n * Adds an icon set to the CreativeEditorSDK UI only once. Marks\n * it as added in the global state to prevent multiple additions.\n */\nexport function addIconSetOnce(\n cesdk: CreativeEditorSDK,\n id: string,\n icons: string\n): void {\n const globalStateIconSetAddedId = `${id}.iconSetAdded`;\n if (!cesdk.ui.experimental.hasGlobalStateValue(globalStateIconSetAddedId)) {\n cesdk.ui.addIconSet(id, icons);\n cesdk.ui.experimental.setGlobalStateValue(globalStateIconSetAddedId, true);\n }\n}\n", "import { isAsyncGenerator } from '../utils/utils';\nimport { GenerationResult, Output } from '../core/provider';\nimport { Middleware } from './middleware';\n\n/**\n * Middleware to upload the output of a generation process.\n *\n * Sometimes it is not possible to use the result of a provider or\n * not even allowed to use it directly. This middleware allows you to upload\n * the result of a generation process to a server or a cloud storage.\n *\n * @param upload The function to upload the output. It should return a promise\n */\nfunction uploadMiddleware<I, O extends Output>(\n upload: (output: O) => Promise<O>\n) {\n const middleware: Middleware<I, O> = async (input, options, next) => {\n const result: GenerationResult<O> | undefined = await next(input, options);\n if (isAsyncGenerator(result)) {\n // No reupload needed, just return the async generator\n return result;\n }\n\n const uploaded = await upload(result);\n return uploaded;\n };\n\n return middleware;\n}\n\nexport default uploadMiddleware;\n", "/**\n * Merge quick actions configuration by combining provider defaults with user configuration overrides\n *\n * @param providerDefaults - The default quick actions from the provider\n * @param userConfig - The user's configuration overrides\n * @returns Merged quick actions configuration\n */\nexport function mergeQuickActionsConfig<T extends Record<string, any>>(\n providerDefaults: T,\n userConfig?: {\n [quickActionId: string]: any | false | null;\n }\n): T {\n // Always return a copy to avoid mutating the original\n const result: any = { ...providerDefaults };\n\n if (!userConfig) return result as T;\n\n for (const [actionId, config] of Object.entries(userConfig)) {\n if (config === false || config === null || config === undefined) {\n // Remove the quick action\n delete result[actionId];\n } else if (config === true) {\n // Keep provider's default if it exists, otherwise add true\n if (!(actionId in providerDefaults)) {\n result[actionId] = true;\n }\n // If it exists in defaults, we already have it from the spread above\n } else {\n // Override with user configuration\n result[actionId] = config;\n }\n }\n\n return result as T;\n}\n", "import { GenerationOptions, Output } from '../core/provider';\nimport { Middleware } from './middleware';\n\nexport interface RateLimitOptions<I> {\n /**\n * Maximum number of requests allowed in the time window\n */\n maxRequests: number;\n\n /**\n * Time window in milliseconds\n */\n timeWindowMs: number;\n\n /**\n * Optional key function or string to create different rate limits for different inputs\n * - If not provided, all requests share the same 'global' rate limit\n * - If a string is provided, that string is used as a static key\n * - If a function is provided, it generates a key based on input and options\n */\n keyFn?: string | ((input: I, options: GenerationOptions) => string);\n\n /**\n * Callback function that is called when rate limit is exceeded\n * Return value determines if the operation should be rejected (throw error) or allowed\n * If true is returned, the operation will proceed despite exceeding the rate limit\n * If false or undefined is returned, the operation will be rejected with a default error\n */\n onRateLimitExceeded?: (\n input: I,\n options: GenerationOptions,\n rateLimitInfo: {\n key: string;\n currentCount: number;\n maxRequests: number;\n timeWindowMs: number;\n remainingTimeMs: number;\n }\n ) => boolean | Promise<boolean> | void;\n\n /**\n * Optional database name for the IndexedDB store\n * If not provided, a default name will be used\n */\n dbName?: string;\n\n /**\n * Disable the rate limit middleware\n */\n disable?: boolean | (() => boolean);\n}\n\n// Store for tracking requests per key\ninterface RequestTracker {\n timestamps: number[];\n lastCleanup: number;\n}\n\n// In-memory fallback store for rate limits for environments without IndexedDB\n// Export for testing purposes\n// This is now a Map of middleware instances to their respective stores\n// The key can be either a symbol (for unique instances with prefix) or a string (for shared instances without prefix)\nexport const inMemoryStores: Map<\n symbol | string,\n Record<string, RequestTracker>\n> = new Map();\n\n/**\n * IndexedDB store for rate limiting\n */\nclass RateLimitStore {\n private db: IDBDatabase | null = null;\n\n private readonly dbName: string;\n\n private readonly storeName: string;\n\n private readonly dbVersion: number = 1;\n\n private isInitializing: boolean = false;\n\n private initPromise: Promise<void> | null = null;\n\n private readonly instanceId: symbol | string;\n\n constructor(\n instanceId: symbol | string,\n dbName?: string,\n storeName?: string\n ) {\n this.instanceId = instanceId;\n this.dbName = dbName ?? 'ly.img.ai.rateLimit';\n this.storeName = storeName ?? 'rateLimits';\n }\n\n /**\n * Initialize the database connection\n */\n async initialize(): Promise<void> {\n if (this.db) {\n return;\n }\n\n if (this.isInitializing) {\n return this.initPromise!;\n }\n\n this.isInitializing = true;\n this.initPromise = new Promise<void>((resolve, reject) => {\n try {\n const request = indexedDB.open(this.dbName, this.dbVersion);\n\n request.onerror = (event) => {\n this.isInitializing = false;\n\n // eslint-disable-next-line no-console\n console.error('Failed to open IndexedDB for rate limiting:', event);\n reject(new Error('Failed to open IndexedDB for rate limiting'));\n };\n\n request.onupgradeneeded = (event) => {\n const db = (event.target as IDBOpenDBRequest).result;\n if (!db.objectStoreNames.contains(this.storeName)) {\n db.createObjectStore(this.storeName, { keyPath: 'id' });\n }\n };\n\n request.onsuccess = (event) => {\n this.db = (event.target as IDBOpenDBRequest).result;\n this.isInitializing = false;\n resolve();\n };\n } catch (error) {\n this.isInitializing = false;\n // eslint-disable-next-line no-console\n console.error('Error initializing IndexedDB:', error);\n reject(error);\n }\n });\n\n return this.initPromise;\n }\n\n /**\n * Get a request tracker by key\n */\n async getTracker(key: string): Promise<RequestTracker | null> {\n try {\n await this.initialize();\n\n // Create a combined key that includes the instance ID to ensure isolation\n const instanceIdStr =\n typeof this.instanceId === 'symbol'\n ? this.instanceId.description || ''\n : this.instanceId;\n const combinedKey = `${instanceIdStr}_${key}`;\n\n return await new Promise((resolve, reject) => {\n const transaction = this.db!.transaction(this.storeName, 'readonly');\n const store = transaction.objectStore(this.storeName);\n const request = store.get(combinedKey);\n\n request.onsuccess = () => {\n if (request.result) {\n resolve(request.result.data);\n } else {\n resolve(null);\n }\n };\n\n request.onerror = () => {\n // eslint-disable-next-line no-console\n console.error(\n `Failed to get tracker for key ${combinedKey}:`,\n request.error\n );\n reject(request.error);\n };\n });\n } catch (error) {\n // eslint-disable-next-line no-console\n console.error('Error getting tracker from IndexedDB:', error);\n return Promise.reject(error);\n }\n }\n\n /**\n * Save a request tracker by key\n */\n async saveTracker(key: string, tracker: RequestTracker): Promise<void> {\n try {\n await this.initialize();\n\n // Create a combined key that includes the instance ID to ensure isolation\n const instanceIdStr =\n typeof this.instanceId === 'symbol'\n ? this.instanceId.description || ''\n : this.instanceId;\n const combinedKey = `${instanceIdStr}_${key}`;\n\n return await new Promise((resolve, reject) => {\n const transaction = this.db!.transaction(this.storeName, 'readwrite');\n const store = transaction.objectStore(this.storeName);\n store.put({ id: combinedKey, data: tracker });\n\n transaction.oncomplete = () => {\n resolve();\n };\n\n transaction.onerror = () => {\n // eslint-disable-next-line no-console\n console.error(\n `Failed to save tracker for key ${combinedKey}:`,\n transaction.error\n );\n reject(transaction.error);\n };\n });\n } catch (error) {\n // eslint-disable-next-line no-console\n console.error('Error saving tracker to IndexedDB:', error);\n return Promise.reject(error);\n }\n }\n\n /**\n * Check if IndexedDB is available\n */\n static isAvailable(): boolean {\n return typeof indexedDB !== 'undefined';\n }\n\n /**\n * Close the database connection\n */\n close(): void {\n if (this.db) {\n this.db.close();\n this.db = null;\n }\n }\n}\n\n/**\n * Middleware that implements rate limiting for AI generation requests\n * Uses IndexedDB for storage when available, with in-memory fallback\n * Each middleware instance has its own isolated set of rate limits\n */\nfunction rateLimitMiddleware<I, O extends Output>(\n middlewareOptions: RateLimitOptions<I>\n) {\n const {\n maxRequests,\n timeWindowMs,\n keyFn = () => 'global',\n onRateLimitExceeded,\n dbName\n } = middlewareOptions;\n\n // Create an identifier for this middleware instance based on its configuration\n // This ensures persistence across page reloads and sharing between identical configurations\n const configStr = `rate-limit-middleware-${maxRequests}-${timeWindowMs}`;\n\n // Use a string key for the configuration so identical configurations share the same limits\n const instanceId = configStr;\n\n // Create the store with the instance ID\n const useIndexedDB = RateLimitStore.isAvailable();\n const store = useIndexedDB ? new RateLimitStore(instanceId, dbName) : null;\n\n // Initialize this middleware's in-memory store\n if (!inMemoryStores.has(instanceId)) {\n inMemoryStores.set(instanceId, {});\n }\n // Get this middleware's in-memory store\n const inMemoryStore = inMemoryStores.get(instanceId)!;\n\n const middleware: Middleware<I, O> = async (input, options, next) => {\n if (\n typeof middlewareOptions.disable === 'function'\n ? middlewareOptions.disable()\n : middlewareOptions.disable\n ) {\n return next(input, options);\n }\n // Get rate limit key based on input using the provided keyFn\n // If keyFn is a string, use it directly; otherwise call the function\n const key = typeof keyFn === 'string' ? keyFn : keyFn(input, options);\n const now = Date.now();\n\n let tracker: RequestTracker;\n\n // Get tracker from IndexedDB or create a new one\n if (useIndexedDB && store) {\n try {\n const storedTracker = await store.getTracker(key);\n if (storedTracker) {\n tracker = storedTracker;\n } else {\n tracker = {\n timestamps: [],\n lastCleanup: now\n };\n }\n } catch (error) {\n // Fallback to in-memory store if IndexedDB fails\n // eslint-disable-next-line no-console\n console.error(\n 'IndexedDB access failed, using in-memory fallback:',\n error\n );\n if (!inMemoryStore[key]) {\n inMemoryStore[key] = {\n timestamps: [],\n lastCleanup: now\n };\n }\n tracker = inMemoryStore[key];\n }\n } else {\n // Use in-memory store if IndexedDB is not available\n if (!inMemoryStore[key]) {\n inMemoryStore[key] = {\n timestamps: [],\n lastCleanup: now\n };\n }\n tracker = inMemoryStore[key];\n }\n\n // Clean up old timestamps that are outside the time window\n if (now - tracker.lastCleanup > timeWindowMs) {\n tracker.timestamps = tracker.timestamps.filter(\n (timestamp) => now - timestamp < timeWindowMs\n );\n tracker.lastCleanup = now;\n }\n\n // Check if rate limit is exceeded\n if (tracker.timestamps.length >= maxRequests) {\n // Calculate the time until the oldest request expires\n const oldestTimestamp = Math.min(...tracker.timestamps);\n const remainingTimeMs = Math.max(\n 0,\n timeWindowMs - (now - oldestTimestamp)\n );\n\n // Call the rate limit exceeded callback if provided\n if (onRateLimitExceeded) {\n const rateLimitInfo = {\n key, // This is the baseKey from keyFn\n currentCount: tracker.timestamps.length,\n maxRequests,\n timeWindowMs,\n remainingTimeMs\n };\n\n // If callback returns true, allow the request to proceed\n const shouldProceed = await onRateLimitExceeded(\n input,\n options,\n rateLimitInfo\n );\n if (!shouldProceed) {\n // The callback should indicate the error, so we throw\n // an abort error instead of a generic error to indicate\n // the generation was aborted and we do not show any further\n // error notification.\n throw new DOMException(\n 'Operation aborted: Rate limit exceeded',\n 'AbortError'\n );\n }\n } else {\n // Default behavior: throw an error\n throw new Error('Rate limit exceeded. Please try again later.');\n }\n }\n\n // Add current timestamp to the tracker\n tracker.timestamps.push(now);\n\n // Save the updated tracker\n if (useIndexedDB && store) {\n try {\n await store.saveTracker(key, tracker);\n } catch (error) {\n // eslint-disable-next-line no-console\n console.error('Failed to save tracker to IndexedDB:', error);\n // In case of IndexedDB failure, update the in-memory store as fallback\n inMemoryStore[key] = tracker;\n }\n } else {\n inMemoryStore[key] = tracker;\n }\n\n // Continue with the next middleware\n return next(input, options);\n };\n\n return middleware;\n}\n\nexport default rateLimitMiddleware;\n", "import type CreativeEditorSDK from '@cesdk/cesdk-js';\n\n/**\n * Checks if the current AI plugin version matches the shared version across all AI plugins.\n * Issues a console warning if versions don't match.\n *\n * @param cesdk - The CreativeEditorSDK instance\n * @param pluginId - The ID of the current plugin\n * @param currentVersion - The version of the current plugin\n */\nexport function checkAiPluginVersion(\n cesdk: CreativeEditorSDK,\n pluginId: string,\n currentVersion: string\n): void {\n const AI_PLUGIN_VERSION_KEY = 'ai-plugin-version';\n const AI_PLUGIN_VERSION_WARNING_KEY = 'ai-plugin-version-warning-shown';\n\n try {\n const sharedVersion = cesdk.ui.experimental.getGlobalStateValue<string>(\n AI_PLUGIN_VERSION_KEY\n );\n\n if (!sharedVersion) {\n // First AI plugin sets the shared version\n cesdk.ui.experimental.setGlobalStateValue(\n AI_PLUGIN_VERSION_KEY,\n currentVersion\n );\n } else if (sharedVersion !== currentVersion) {\n // Version mismatch detected\n const warningShown = cesdk.ui.experimental.getGlobalStateValue<boolean>(\n AI_PLUGIN_VERSION_WARNING_KEY,\n false\n );\n\n if (!warningShown) {\n // eslint-disable-next-line no-console\n console.warn(\n `[IMG.LY AI Plugins] Version mismatch detected!\\n` +\n `Plugin \"${pluginId}\" is using version ${currentVersion}, but other AI plugins are using version ${sharedVersion}.\\n` +\n `This may cause compatibility issues. Please ensure all AI plugins (@imgly/plugin-ai-*) use the same version.\\n` +\n `Consider updating all AI plugins to the same version for optimal compatibility.`\n );\n\n // Set flag to prevent duplicate warnings\n cesdk.ui.experimental.setGlobalStateValue(\n AI_PLUGIN_VERSION_WARNING_KEY,\n true\n );\n }\n }\n } catch (error) {\n // Fail silently if global state access fails\n // eslint-disable-next-line no-console\n console.debug(\n '[IMG.LY AI Plugins] Could not check plugin version consistency:',\n error\n );\n }\n}\n", "import CreativeEditorSDK from '@cesdk/cesdk-js';\n\n/**\n * Registers a dock component for AI generation that opens\n * the AI generation panel and closes any other AI panels.\n */\nfunction registerDockComponent(options: {\n cesdk: CreativeEditorSDK;\n panelId: string;\n}) {\n const { cesdk, panelId } = options;\n if (!panelId.startsWith('ly.img.ai.')) {\n // eslint-disable-next-line no-console\n console.warn(\n `Dock components for AI generation should open a panel with an id starting with \"ly.img.ai.\" \u2013 \"${panelId}\" was provided.`\n );\n }\n\n const dockComponentId = `${panelId}.dock`;\n cesdk.ui.registerComponent(dockComponentId, ({ builder }) => {\n const isOpen = cesdk.ui.isPanelOpen(panelId);\n\n builder.Button(`${panelId}.dock.button`, {\n label: `${panelId}.dock.label`,\n isSelected: isOpen,\n icon: '@imgly/Sparkle',\n onClick: () => {\n cesdk.ui.findAllPanels().forEach((panel) => {\n if (panel.startsWith('ly.img.ai.')) {\n cesdk.ui.closePanel(panel);\n }\n if (!isOpen && panel === '//ly.img.panel/assetLibrary') {\n cesdk.ui.closePanel(panel);\n }\n });\n\n if (!isOpen) {\n cesdk.ui.openPanel(panelId);\n } else {\n cesdk.ui.closePanel(panelId);\n }\n }\n });\n });\n}\n\nexport default registerDockComponent;\n", "import type CreativeEngine from '@cesdk/engine';\n\n/**\n * Enable function for a single image fill block selected.\n */\nfunction enableImageFill() {\n return ({ engine }: { engine: CreativeEngine }) => {\n const blockIds = engine.block.findAllSelected();\n if (blockIds == null || blockIds.length !== 1) return false;\n\n const [blockId] = blockIds;\n\n if (!engine.block.supportsFill(blockId)) return false;\n\n if (engine.block.getKind(blockId) === 'sticker') return false;\n\n if (\n !['//ly.img.ubq/graphic', '//ly.img.ubq/page'].includes(\n engine.block.getType(blockId)\n )\n ) {\n return false;\n }\n\n const fillBlock = engine.block.getFill(blockId);\n return engine.block.getType(fillBlock) === '//ly.img.ubq/fill/image';\n };\n}\n\nexport default enableImageFill;\n", "import CreativeEditorSDK from '@cesdk/cesdk-js';\nimport Provider, {\n GenerationOptions,\n GetInput,\n Output,\n OutputKind\n} from '../core/provider';\nimport { extractErrorMessage } from '../utils/utils';\n\nfunction handleGenerationError<K extends OutputKind, I, O extends Output>(\n error: unknown,\n options: {\n cesdk: CreativeEditorSDK;\n provider: Provider<K, I, O>;\n getInput?: GetInput<I>;\n middlewareOptions?: GenerationOptions;\n }\n) {\n const { cesdk, provider, getInput, middlewareOptions } = options;\n\n // Check if default was prevented\n if (middlewareOptions?.defaultPrevented()) {\n return; // Skip all default behavior (notifications + console logging)\n }\n\n // eslint-disable-next-line no-console\n console.error('Generation failed:', error);\n const shown = showErrorNotification(\n cesdk,\n provider.output.notification,\n () => ({\n input: getInput?.().input,\n error\n })\n );\n if (!shown) {\n cesdk.ui.showNotification({\n type: 'error',\n message: extractErrorMessage(error)\n });\n }\n}\n\nfunction showErrorNotification<I, O extends Output>(\n cesdk: CreativeEditorSDK,\n notifications: Provider<any, I, O>['output']['notification'],\n createContext: () => { input?: I; error: unknown }\n): boolean {\n const errorNotification = notifications?.error;\n if (errorNotification == null) return false;\n\n const showOnSuccess =\n typeof errorNotification.show === 'function'\n ? errorNotification.show(createContext())\n : errorNotification.show;\n\n if (!showOnSuccess) return false;\n\n const message =\n typeof errorNotification.message === 'function'\n ? errorNotification.message(createContext())\n : errorNotification.message ?? 'common.ai-generation.failed';\n\n const action =\n errorNotification.action != null\n ? {\n label:\n typeof errorNotification.action.label === 'function'\n ? errorNotification.action.label(createContext())\n : errorNotification.action.label,\n onClick: () => {\n errorNotification?.action?.onClick(createContext());\n }\n }\n : undefined;\n\n cesdk.ui.showNotification({\n type: 'error',\n message,\n action\n });\n return true;\n}\n\nexport default handleGenerationError;\n", "const previewUri =\n 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzIzIiBoZWlnaHQ9IjMyMyIgdmlld0JveD0iMCAwIDMyMyAzMjMiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxyZWN0IHdpZHRoPSIzMjMiIGhlaWdodD0iMzIzIiBmaWxsPSIjRTlFQkVEIi8+CjxnIG9wYWNpdHk9IjAuMyI+CjxwYXRoIGQ9Ik0xMTYgMTg0VjE5MS41QzExNiAxOTkuNzg0IDEyMi43MTYgMjA2LjUgMTMxIDIwNi41SDE5MUMxOTkuMjg0IDIwNi41IDIwNiAxOTkuNzg0IDIwNiAxOTEuNVYxMzEuNUMyMDYgMTIzLjIxNiAxOTkuMjg0IDExNi41IDE5MSAxMTYuNUwxOTAuOTk1IDEyNi41QzE5My43NTcgMTI2LjUgMTk2IDEyOC43MzkgMTk2IDEzMS41VjE5MS41Qz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i45MzIgMTQzLjEyNkwxNTYuNTc3IDE0OC41MDhDMTU4LjE2IDE0OS4xMzIgMTU4LjE2IDE1MS4zNzIgMTU2LjU3NyAxNTEuOTk2TDE0Mi45MzIgMTU3LjM3OEMxNDIuNDQ4IDE1Ny41NjggMTQyLjA2NiAxNTcuOTUxIDE0MS44NzUgMTU4LjQzNEwxMzYuNDk0IDE3Mi4wNzlDMTM1Ljg2OSAxNzMuNjYyIDEzMy42MjkgMTczLjY2MiAxMzMuMDA1IDE3Mi4wNzlMMTI3LjYyMyAxNTguNDM0QzEyNy40MzMgMTU3Ljk1MSAxMjcuMDUgMTU3LjU2OCAxMjYuNTY3IDE1Ny4zNzhMMTEyLjkyMiAxNTEuOTk2QzExMS4zMzkgMTUxLjM3MiAxMTEuMzM5IDE0OS4xMzIgMTEyLjkyMiAxNDguNTA4TDEyNi41NjcgMTQzLjEyNkMxMjcuMDUgMTQyLjkzNSAxMjcuNDMzIDE0Mi41NTMgMTI3LjYyMyAxNDIuMDdMMTMzLjAwNSAxMjguNDI0WiIgZmlsbD0iIzhGOEY4RiIvPgo8cGF0aCBkPSJNMTk1Ljk5OSAxODQuMDA0VjE5MS41MDJDMTk1Ljk5OSAxOTQuMjYzIDE5My43NjEgMTk2LjUwMiAxOTAuOTk5IDE5Ni41MDJIMTQ3LjY2OEwxNzIuODc5IDE1OC42ODRDMTc0LjM2MyAxNTYuNDU4IDE3Ny42MzUgMTU2LjQ1OCAxNzkuMTIgMTU4LjY4NEwxOTUuOTk5IDE4NC4wMDRaIiBmaWxsPSIjOEY4RjhGIi8+CjwvZz4KPC9zdmc+Cg==';\n\nexport default previewUri;\n", "import { type AssetResult } from '@cesdk/cesdk-js';\nimport {\n type OutputKind,\n GetBlockInputResult,\n InputByKind\n} from '../core/provider';\nimport previewUri from './previewUri';\n\nfunction getAssetResultForPlaceholder<K extends OutputKind>(\n id: string,\n kind: K,\n blockInput: GetBlockInputResult<K>\n): AssetResult {\n switch (kind) {\n case 'image': {\n return getImageAssetResultForPlaceholder(\n id,\n blockInput[kind] as InputByKind['image']\n );\n }\n case 'video': {\n return getVideoAssetResultForPlaceholder(\n id,\n blockInput[kind] as InputByKind['video']\n );\n }\n case 'sticker': {\n return getStickerAssetResultForPlaceholder(\n id,\n blockInput[kind] as InputByKind['sticker']\n );\n }\n\n default: {\n throw new Error(\n `Unsupported output kind for creating placeholder block: ${kind}`\n );\n }\n }\n}\n\nfunction getImageAssetResultForPlaceholder(\n id: string,\n input: InputByKind['image']\n): AssetResult {\n const width = input.width;\n const height = input.height;\n return {\n id,\n meta: {\n previewUri,\n fillType: '//ly.img.ubq/fill/image',\n kind: 'image',\n\n width,\n height\n }\n };\n}\n\nfunction getVideoAssetResultForPlaceholder(\n id: string,\n input: InputByKind['video']\n): AssetResult {\n const width = input.width;\n const height = input.height;\n return {\n id,\n label: input.label,\n meta: {\n previewUri,\n mimeType: 'video/mp4',\n kind: 'video',\n fillType: '//ly.img.ubq/fill/video',\n\n duration: input.duration.toString(),\n\n width,\n height\n }\n };\n}\n\nfunction getStickerAssetResultForPlaceholder(\n id: string,\n input: InputByKind['sticker']\n): AssetResult {\n const width = input.width;\n const height = input.height;\n return {\n id,\n meta: {\n previewUri,\n fillType: '//ly.img.ubq/fill/image',\n kind: 'sticker',\n\n width,\n height\n }\n };\n}\n\nexport default getAssetResultForPlaceholder;\n", "import { type AssetResult } from '@cesdk/cesdk-js';\nimport {\n type OutputKind,\n AudioOutput,\n GetBlockInputResult,\n ImageOutput,\n InputByKind,\n Output,\n StickerOutput,\n VideoOutput\n} from '../core/provider';\nimport { getThumbnailForVideo } from '../utils/utils';\n\nasync function getAssetResultForGenerated<K extends OutputKind>(\n id: string,\n kind: K,\n blockInputs: GetBlockInputResult<K>,\n output: Output\n): Promise<AssetResult> {\n switch (kind) {\n case 'image': {\n if (output.kind !== 'image') {\n throw new Error(\n `Output kind does not match the expected type: ${output.kind} (expected: image)`\n );\n }\n\n return getImageAssetResultForGenerated(\n id,\n blockInputs[kind] as InputByKind['image'],\n output\n );\n }\n\n case 'video': {\n if (output.kind !== 'video') {\n throw new Error(\n `Output kind does not match the expected type: ${output.kind} (expected: video)`\n );\n }\n\n return getVideoAssetResultForGenerated(\n id,\n blockInputs[kind] as InputByKind['video'],\n output\n );\n }\n\n case 'audio': {\n if (output.kind !== 'audio') {\n throw new Error(\n `Output kind does not match the expected type: ${output.kind} (expected: audio)`\n );\n }\n\n return getAudioAssetResultForGenerated(\n id,\n blockInputs[kind] as InputByKind['audio'],\n output\n );\n }\n\n case 'sticker': {\n if (output.kind !== 'sticker') {\n throw new Error(\n `Output kind does not match the expected type: ${output.kind} (expected: sticker)`\n );\n }\n\n return getStickerAssetResultForGenerated(\n id,\n blockInputs[kind] as InputByKind['sticker'],\n output\n );\n }\n\n default: {\n throw new Error(\n `Unsupported output kind for creating placeholder block: ${kind}`\n );\n }\n }\n}\n\nfunction getImageAssetResultForGenerated(\n id: string,\n input: InputByKind['image'],\n output: ImageOutput\n): AssetResult {\n const width = input.width;\n const height = input.height;\n return {\n id,\n label: input.label,\n meta: {\n uri: output.url,\n thumbUri: output.url,\n fillType: '//ly.img.ubq/fill/image',\n kind: 'image',\n\n width,\n height\n },\n payload: {\n sourceSet: [\n {\n uri: output.url,\n width,\n height\n }\n ]\n }\n };\n}\n\nasync function getVideoAssetResultForGenerated(\n id: string,\n input: InputByKind['video'],\n output: VideoOutput\n): Promise<AssetResult> {\n const width = input.width;\n const height = input.height;\n\n const thumbUri = await getThumbnailForVideo(output.url, 0);\n\n return {\n id,\n label: input.label,\n meta: {\n uri: output.url,\n thumbUri,\n\n mimeType: 'video/mp4',\n kind: 'video',\n fillType: '//ly.img.ubq/fill/video',\n\n duration: input.duration.toString(),\n\n width,\n height\n }\n };\n}\n\nfunction getAudioAssetResultForGenerated(\n id: string,\n input: InputByKind['audio'],\n output: AudioOutput\n): AssetResult {\n return {\n id,\n label: input.label,\n meta: {\n uri: output.url,\n thumbUri: output.thumbnailUrl,\n blockType: '//ly.img.ubq/audio',\n mimeType: 'audio/x-m4a',\n duration: output.duration.toString()\n }\n };\n}\n\nfunction getStickerAssetResultForGenerated(\n id: string,\n input: InputByKind['sticker'],\n output: StickerOutput\n): AssetResult {\n const width = input.width;\n const height = input.height;\n return {\n id,\n label: input.label,\n meta: {\n uri: output.url,\n thumbUri: output.url,\n fillType: '//ly.img.ubq/fill/image',\n kind: 'sticker',\n\n width,\n height\n },\n payload: {\n sourceSet: [\n {\n uri: output.url,\n width,\n height\n }\n ]\n }\n };\n}\n\nexport default getAssetResultForGenerated;\n", "import type { AssetDefinition } from '@cesdk/cesdk-js';\nimport { addAssetToScene, isAbortError, uuid4 } from '../utils/utils';\nimport { type GetBlockInput, OutputKind, type Output } from '../core/provider';\nimport { Generate, Result } from './createGenerateFunction';\nimport getAssetResultForPlaceholder from '../assets/getAssetResultForPlaceholder';\nimport CreativeEditorSDK from '@cesdk/cesdk-js';\nimport { Middleware } from '../middleware/middleware';\nimport getAssetResultForGenerated from '../assets/getAssetResultForGenerated';\n\ntype PanelGenerationOptions<K extends OutputKind, I, O extends Output> = {\n /**\n * The kind to generate.\n */\n kind: K;\n\n /**\n * Initialized generate function\n */\n generate: Generate<I, O>;\n\n /**\n * The user flow for the generation process.\n */\n userFlow: 'placeholder' | 'generation-only';\n\n /**\n * Function to get block input from the generated input.\n */\n getBlockInput: GetBlockInput<K, I>;\n\n /**\n * Asset source id of the history library where a generated asset\n * will be added.\n */\n historyAssetSourceId?: string;\n\n /**\n * Additional middlewares added to the generation process.\n */\n middlewares?: Middleware<I, O>[];\n\n /**\n * Print debug information to the console.\n */\n debug?: boolean;\n\n /**\n * Enable dry run mode for testing.\n */\n dryRun?: boolean;\n\n /**\n * Signal to check if process was aborted.\n */\n abortSignal: AbortSignal;\n\n cesdk: CreativeEditorSDK;\n};\n\n/**\n * Handler for generating content from a panel interface.\n * Creates placeholder blocks and manages the generation process.\n */\nfunction handleGenerateFromPanel<K extends OutputKind, I, O extends Output>(\n options: PanelGenerationOptions<K, I, O>\n): (input: I) => Promise<Result<O>> {\n switch (options.userFlow) {\n case 'placeholder':\n return handleGeneratePlaceholderUserFlow(options);\n case 'generation-only':\n return handleGenerateGenerationOnlyUserFlow(options);\n default:\n throw new Error(\n `Unknown user flow: ${options.userFlow}. Expected 'placeholder' or 'generation-only'.`\n );\n }\n}\n\nfunction handleGenerateGenerationOnlyUserFlow<\n K extends OutputKind,\n I,\n O extends Output\n>(options: PanelGenerationOptions<K, I, O>): (input: I) => Promise<Result<O>> {\n const { cesdk, abortSignal } = options;\n\n return async (input: I) => {\n try {\n const kind = options.kind;\n const blockInputs = await options.getBlockInput(input);\n\n if (checkAbortSignal(cesdk, abortSignal)) return { status: 'aborted' };\n\n const result = await options.generate(input, {\n middlewares: [...(options.middlewares ?? [])],\n debug: options.debug,\n dryRun: options.dryRun,\n abortSignal\n });\n\n if (checkAbortSignal(cesdk, abortSignal)) return { status: 'aborted' };\n\n if (result.status !== 'success') {\n return result;\n }\n\n if (result.type === 'async') {\n // If the result is an async generator, we need to handle it differently.\n // This is a placeholder for handling sync results.\n // You might want to implement logic to handle async results here.\n throw new Error(\n 'Async generation is not supported in this context yet.'\n );\n }\n\n if (checkAbortSignal(cesdk, abortSignal)) return { status: 'aborted' };\n\n if (options.historyAssetSourceId != null) {\n const assetId = uuid4();\n const generatedAssetResult = await getAssetResultForGenerated(\n assetId,\n kind,\n blockInputs,\n result.output\n );\n const assetDefinition: AssetDefinition = {\n ...generatedAssetResult,\n id: `${Date.now()}-${generatedAssetResult.id}`,\n label:\n generatedAssetResult.label != null\n ? {\n en: generatedAssetResult.label\n }\n : {},\n tags: {}\n };\n cesdk.engine.asset.addAssetToSource(\n options.historyAssetSourceId,\n assetDefinition\n );\n } else {\n if (options.debug) {\n // eslint-disable-next-line no-console\n console.log(\n 'No asset source ID found in history and generation only was requested. Doing nothing. If no middleware is adding functionality this could be a bug.'\n );\n }\n }\n\n return result;\n } catch (error) {\n return {\n status: 'error',\n message: error instanceof Error ? error.message : String(error)\n };\n }\n };\n}\n\n/**\n * Handles generation from a panel with a placeholder block.\n */\nfunction handleGeneratePlaceholderUserFlow<\n K extends OutputKind,\n I,\n O extends Output\n>(options: PanelGenerationOptions<K, I, O>): (input: I) => Promise<Result<O>> {\n const { cesdk, abortSignal } = options;\n\n let placeholderBlock: number | undefined;\n return async (input: I) => {\n try {\n const kind = options.kind;\n const blockInputs = await options.getBlockInput(input);\n\n if (checkAbortSignal(cesdk, abortSignal)) return { status: 'aborted' };\n\n const assetId = uuid4();\n const assetResult = getAssetResultForPlaceholder(\n assetId,\n kind,\n blockInputs\n );\n\n placeholderBlock = await addAssetToScene(cesdk, assetResult);\n if (checkAbortSignal(cesdk, abortSignal, placeholderBlock))\n return { status: 'aborted' };\n // This is a workaround. The middleware in the video timeline\n // is calling APIs that will render the block in an error\n // state if it does not have an URI set. It's difficult to\n // recover from that. A bug report has been created for this.\n // As a workaround: Duplicating the block will remove the error state\n // but you will still see an error in the web console.\n // TODO: Remove this workaround when the bug is fixed.\n if (placeholderBlock != null && options.kind === 'video') {\n const positionX = cesdk.engine.block.getPositionX(placeholderBlock);\n const positionY = cesdk.engine.block.getPositionY(placeholderBlock);\n const duplicated = cesdk.engine.block.duplicate(placeholderBlock);\n cesdk.engine.block.setPositionX(duplicated, positionX);\n cesdk.engine.block.setPositionY(duplicated, positionY);\n cesdk.engine.block.destroy(placeholderBlock);\n placeholderBlock = duplicated;\n }\n\n if (placeholderBlock == null)\n throw new Error('Could not create placeholder block');\n\n cesdk.engine.block.setState(placeholderBlock, {\n type: 'Pending',\n progress: 0\n });\n\n const result = await options.generate(input, {\n blockIds: [placeholderBlock],\n middlewares: [...(options.middlewares ?? [])],\n debug: options.debug,\n dryRun: options.dryRun,\n abortSignal\n });\n\n if (checkAbortSignal(cesdk, abortSignal, placeholderBlock))\n return { status: 'aborted' };\n\n if (result.status !== 'success') {\n // Update block state before returning to prevent stuck in Pending state\n // Check if default was prevented\n if (!result.middlewareOptions?.defaultPrevented()) {\n if (\n placeholderBlock != null &&\n cesdk.engine.block.isValid(placeholderBlock)\n ) {\n if (result.status === 'aborted') {\n cesdk.engine.block.destroy(placeholderBlock);\n } else {\n cesdk.engine.block.setState(placeholderBlock, {\n type: 'Error',\n error: 'Unknown'\n });\n }\n }\n }\n return result;\n }\n\n if (result.type === 'async') {\n // If the result is an async generator, we need to handle it differently.\n // This is a placeholder for handling sync results.\n // You might want to implement logic to handle async results here.\n throw new Error(\n 'Async generation is not supported in this context yet.'\n );\n }\n\n if (!cesdk.engine.block.isValid(placeholderBlock)) {\n return {\n status: 'aborted',\n message:\n 'Placeholder block was destroyed before generation completed.'\n };\n }\n\n const generatedAssetResult = await getAssetResultForGenerated(\n assetId,\n kind,\n blockInputs,\n result.output\n );\n\n if (checkAbortSignal(cesdk, abortSignal, placeholderBlock))\n return { status: 'aborted' };\n\n if (options.debug)\n // eslint-disable-next-line no-console\n console.log(\n 'Updating placeholder in scene:',\n JSON.stringify(generatedAssetResult, undefined, 2)\n );\n\n await cesdk.engine.asset.defaultApplyAssetToBlock(\n generatedAssetResult,\n placeholderBlock\n );\n\n if (checkAbortSignal(cesdk, abortSignal, placeholderBlock))\n return { status: 'aborted' };\n\n if (options.historyAssetSourceId != null) {\n const assetDefinition: AssetDefinition = {\n ...generatedAssetResult,\n id: `${Date.now()}-${generatedAssetResult.id}`,\n label:\n generatedAssetResult.label != null\n ? {\n en: generatedAssetResult.label\n }\n : {},\n tags: {}\n };\n cesdk.engine.asset.addAssetToSource(\n options.historyAssetSourceId,\n assetDefinition\n );\n }\n\n if (cesdk.engine.block.isValid(placeholderBlock)) {\n cesdk.engine.block.setState(placeholderBlock, {\n type: 'Ready'\n });\n }\n\n return result;\n } catch (error) {\n // Note: For exceptions thrown in middleware, we don't have access to middlewareOptions\n // so we can't check defaultPrevented here. Middleware should handle errors in catch blocks\n // and re-throw to use preventDefault properly.\n if (\n placeholderBlock != null &&\n cesdk.engine.block.isValid(placeholderBlock)\n ) {\n if (isAbortError(error)) {\n cesdk.engine.block.destroy(placeholderBlock);\n } else {\n cesdk.engine.block.setState(placeholderBlock, {\n type: 'Error',\n error: 'Unknown'\n });\n }\n }\n return {\n status: 'error',\n message: error instanceof Error ? error.message : String(error)\n };\n }\n };\n}\n\n/**\n * Check the given abort signal and destroy the placeholder block if it is aborted.\n * @returns `true` if the signal is aborted, `false` otherwise.\n */\nfunction checkAbortSignal(\n cesdk: CreativeEditorSDK,\n abortSignal: AbortSignal,\n placeholderBlock?: number\n) {\n if (abortSignal.aborted) {\n if (\n placeholderBlock != null &&\n cesdk.engine.block.isValid(placeholderBlock)\n ) {\n cesdk.engine.block.destroy(placeholderBlock);\n }\n return true;\n }\n return false;\n}\n\nexport default handleGenerateFromPanel;\n", "import CreativeEditorSDK, {\n type BuilderRenderFunctionContext\n} from '@cesdk/cesdk-js';\nimport type Provider from '../../core/provider';\nimport {\n type GetInput,\n type OutputKind,\n type Output,\n type GetBlockInput\n} from '../../core/provider';\nimport { UIOptions, CommonConfiguration } from '../../types';\nimport { isAbortError } from '../../utils/utils';\nimport handleGenerationError from '../../generation/handleGenerationError';\nimport handleGenerateFromPanel from '../../generation/handleGenerateFromPanel';\nimport { Generate } from '../../generation/createGenerateFunction';\n\nexport function isGeneratingStateKey(providerId: string): string {\n return `${providerId}.generating`;\n}\n\nexport function abortGenerationStateKey(providerId: string): string {\n return `${providerId}.abort`;\n}\n\n/**\n * Renders the generation UI components and sets up event handlers\n */\nfunction renderGenerationComponents<K extends OutputKind, I, O extends Output>(\n context: BuilderRenderFunctionContext<any>,\n provider: Provider<K, I, O>,\n generate: Generate<I, O>,\n getInput: GetInput<I>,\n getBlockInput: GetBlockInput<K, I>,\n options: UIOptions & {\n createPlaceholderBlock?: boolean;\n includeHistoryLibrary?: boolean;\n requiredInputs?: string[];\n },\n config: CommonConfiguration<I, O>\n): void {\n const { builder, experimental } = context;\n const { cesdk, includeHistoryLibrary = true } = options;\n const {\n id: providerId,\n output: { abortable }\n } = provider;\n const abortState = experimental.global<() => void>(\n abortGenerationStateKey(providerId),\n () => {}\n );\n const generatingState = experimental.global<boolean>(\n isGeneratingStateKey(providerId),\n false\n );\n\n let abortController: AbortController | undefined;\n const canAbortNow = generatingState.value && abortable;\n const abort = () => {\n if (canAbortNow) {\n abortState.value();\n generatingState.setValue(false);\n abortState.setValue(() => {});\n }\n };\n\n let isDisabled: boolean | undefined;\n if (options.requiredInputs != null && options.requiredInputs.length > 0) {\n const inputs = getInput();\n isDisabled = options.requiredInputs.every((input) => {\n // @ts-ignore\n const hasInput = !inputs.input[input];\n return hasInput;\n });\n }\n\n const confirmCancelDialogId = experimental.global<string | undefined>(\n `${providerId}.confirmationDialogId`,\n undefined\n );\n\n builder.Section(`${providerId}.generate.section`, {\n children: () => {\n builder.Button(`${providerId}.generate`, {\n label: ['common.generate', `panel.${providerId}.generate`],\n isLoading: generatingState.value,\n color: 'accent',\n isDisabled,\n suffix: canAbortNow\n ? {\n icon: '@imgly/Cross',\n color: 'danger',\n tooltip: [`panel.${providerId}.abort`, 'common.cancel'],\n onClick: () => {\n const confirmationDialogId = cesdk.ui.showDialog({\n type: 'warning',\n content: 'panel.ly.img.ai.generation.confirmCancel.content',\n cancel: {\n label: 'common.close',\n onClick: ({ id }) => {\n cesdk.ui.closeDialog(id);\n confirmCancelDialogId.setValue(undefined);\n }\n },\n actions: {\n label: 'panel.ly.img.ai.generation.confirmCancel.confirm',\n color: 'danger',\n onClick: ({ id }) => {\n abort();\n cesdk.ui.closeDialog(id);\n confirmCancelDialogId.setValue(undefined);\n }\n }\n });\n confirmCancelDialogId.setValue(confirmationDialogId);\n }\n }\n : undefined,\n onClick: async () => {\n abortController = new AbortController();\n const abortSignal = abortController.signal;\n\n const triggerGeneration = async () => {\n try {\n generatingState.setValue(true);\n abortState.setValue(() => {\n if (config.debug)\n // eslint-disable-next-line no-console\n console.log('Aborting generation');\n abortController?.abort();\n });\n\n const result = await handleGenerateFromPanel({\n kind: provider.kind,\n generate,\n historyAssetSourceId: options.historyAssetSourceId,\n // TODO: Replace with a merged configuration\n userFlow: options.createPlaceholderBlock\n ? 'placeholder'\n : 'generation-only',\n getBlockInput,\n abortSignal,\n cesdk,\n debug: config.debug,\n dryRun: config.dryRun\n })(getInput().input);\n\n if (result.status === 'aborted') {\n return;\n }\n\n if (result.status === 'error') {\n handleGenerationError(result.message, {\n cesdk,\n provider,\n getInput,\n middlewareOptions: result.middlewareOptions\n });\n return;\n }\n\n if (result.status === 'success' && result.type === 'sync') {\n // Check if default was prevented\n if (!result.middlewareOptions?.defaultPrevented()) {\n const notification = provider.output.notification;\n showSuccessNotification(cesdk, notification, () => ({\n input: getInput().input,\n output: result.output\n }));\n }\n }\n } catch (error) {\n // Do not treat abort errors as errors\n if (isAbortError(error)) {\n return;\n }\n\n // Note: For exceptions thrown in middleware, we don't have access to middlewareOptions\n // so we can't check defaultPrevented here. Middleware should handle errors in catch blocks\n // and re-throw to use preventDefault properly.\n handleGenerationError(error, {\n cesdk,\n provider,\n getInput\n });\n } finally {\n abortController = undefined;\n generatingState.setValue(false);\n abortState.setValue(() => {});\n\n if (confirmCancelDialogId.value != null) {\n cesdk.ui.closeDialog(confirmCancelDialogId.value);\n confirmCancelDialogId.setValue(undefined);\n }\n }\n };\n\n await triggerGeneration();\n }\n });\n if (provider.output.generationHintText != null) {\n builder.Text(`${providerId}.generation-hint`, {\n align: 'center',\n content: provider.output.generationHintText\n });\n }\n }\n });\n\n if (includeHistoryLibrary && options.historyAssetLibraryEntryId != null) {\n builder.Library(`${providerId}.history.library`, {\n entries: [options.historyAssetLibraryEntryId]\n });\n }\n}\n\nfunction showSuccessNotification<I, O extends Output>(\n cesdk: CreativeEditorSDK,\n notifications: Provider<any, I, O>['output']['notification'],\n createContext: () => { input: I; output: O }\n): boolean {\n const successNotification = notifications?.success;\n if (successNotification == null) return false;\n\n const showOnSuccess =\n typeof successNotification.show === 'function'\n ? successNotification.show(createContext())\n : successNotification.show;\n\n if (!showOnSuccess) return false;\n\n const message =\n typeof successNotification.message === 'function'\n ? successNotification.message(createContext())\n : successNotification.message ?? 'common.ai-generation.success';\n\n const action =\n successNotification.action != null\n ? {\n label:\n typeof successNotification.action.label === 'function'\n ? successNotification.action.label(createContext())\n : successNotification.action.label,\n onClick: () => {\n successNotification?.action?.onClick(createContext());\n }\n }\n : undefined;\n\n cesdk.ui.showNotification({\n type: 'success',\n message,\n action,\n duration: successNotification.duration\n });\n return true;\n}\n\nexport default renderGenerationComponents;\n", "import { BuilderRenderFunction } from '@cesdk/cesdk-js';\nimport { OutputKind, PanelInputCustom, type Output } from '../../core/provider';\nimport renderGenerationComponents, {\n isGeneratingStateKey\n} from '../components/renderGenerationComponents';\nimport { InitializationContext } from '../../types';\nimport { Generate } from '../../generation/createGenerateFunction';\n\nasync function createPanelRenderFunctionFromCustom<\n K extends OutputKind,\n I,\n O extends Output\n>(\n {\n options,\n provider,\n panelInput,\n config\n }: InitializationContext<K, I, O, PanelInputCustom<K, I>>,\n\n generate: Generate<I, O>\n): Promise<BuilderRenderFunction<any> | undefined> {\n if (panelInput == null) {\n return undefined;\n }\n\n const { cesdk } = options;\n const { id: providerId } = provider;\n\n const render = panelInput.render;\n\n const builderRenderFunction: BuilderRenderFunction<any> = (context) => {\n const { state } = context;\n\n const isGenerating = state(isGeneratingStateKey(providerId), {\n isGenerating: false,\n abort: () => {}\n }).value.isGenerating;\n\n const { getInput, getBlockInput } = render(context, {\n cesdk,\n isGenerating\n });\n renderGenerationComponents(\n context,\n provider,\n generate,\n getInput,\n getBlockInput,\n {\n ...options,\n includeHistoryLibrary: panelInput.includeHistoryLibrary ?? true,\n createPlaceholderBlock: panelInput.userFlow === 'placeholder'\n },\n config\n );\n\n return getInput;\n };\n\n return builderRenderFunction;\n}\n\nexport default createPanelRenderFunctionFromCustom;\n", "import { type OpenAPIV3 } from 'openapi-types';\n\n/**\n * Resolves a JSON reference path within a document\n * @param document The OpenAPI document\n * @param refPath The reference path (e.g. \"#/components/schemas/MySchema\")\n * @returns The resolved object from the document\n */\nexport function resolveReference(\n document: OpenAPIV3.Document,\n refPath: string\n): unknown {\n // Only handle internal references\n if (!refPath.startsWith('#/')) {\n throw new Error(`External references are not supported: ${refPath}`);\n }\n\n // Remove the leading #/\n const path = refPath.substring(2).split('/');\n let current: any = document;\n\n // Navigate through the path\n for (const segment of path) {\n if (current === undefined || current === null) {\n throw new Error(`Invalid reference path: ${refPath}`);\n }\n current = current[segment];\n }\n\n if (current === undefined) {\n throw new Error(`Reference not found: ${refPath}`);\n }\n\n return current;\n}\n\n/**\n * Recursively dereferences all $ref properties in an object\n * @param document The original document for resolving references\n * @param obj The object to dereference\n * @param visited Set of visited objects to prevent circular references\n * @returns The dereferenced object\n */\nfunction dereferenceObject(\n document: OpenAPIV3.Document,\n obj: any,\n visited = new Set<any>()\n): any {\n // Handle null or undefined\n if (obj === null || obj === undefined) {\n return obj;\n }\n\n // If we've seen this object before, return it to avoid circular references\n if (visited.has(obj)) {\n return obj;\n }\n\n // Add the current object to the visited set\n visited.add(obj);\n\n // Handle $ref\n if (obj.$ref && typeof obj.$ref === 'string') {\n // Get the referenced object\n const referenced = resolveReference(document, obj.$ref);\n // Dereference the referenced object\n const dereferenced = dereferenceObject(document, referenced, visited);\n\n // Merge other properties from the original object\n const result = { ...dereferenced };\n for (const key in obj) {\n if (Object.prototype.hasOwnProperty.call(obj, key) && key !== '$ref') {\n result[key] = dereferenceObject(document, obj[key], visited);\n }\n }\n return result;\n }\n\n // Handle arrays\n if (Array.isArray(obj)) {\n return obj.map((item) => dereferenceObject(document, item, visited));\n }\n\n // Handle objects\n if (typeof obj === 'object') {\n const result: any = {};\n for (const key in obj) {\n if (Object.prototype.hasOwnProperty.call(obj, key)) {\n result[key] = dereferenceObject(document, obj[key], visited);\n }\n }\n return result;\n }\n\n // Return primitives as is\n return obj;\n}\n\n/**\n * Dereferences all $ref properties in an OpenAPI document\n * @param document The OpenAPI document to dereference\n * @returns A new document with all references resolved\n */\nexport default function dereferenceDocument(\n document: OpenAPIV3.Document\n): OpenAPIV3.Document {\n return dereferenceObject(document, { ...document }) as OpenAPIV3.Document;\n}\n", "import { OpenAPIV3 } from 'openapi-types';\n\n/**\n * Checks if an unknown object is an OpenAPI schema (first level only)\n *\n * @param obj - The object to check (potentially undefined)\n * @param debug - If true, log the reason when validation fails\n * @returns A boolean indicating whether the object is an OpenAPI schema\n */\nexport function isOpenAPISchema(\n obj: unknown,\n debug: boolean = false\n): obj is OpenAPIV3.SchemaObject {\n // Helper function to log debug messages and return false\n const fail = (reason: string): false => {\n if (debug) {\n // eslint-disable-next-line no-console\n console.log(`OpenAPI Schema validation failed: ${reason}`);\n }\n return false;\n };\n\n // Check if obj is an object and not null\n if (typeof obj !== 'object' || obj === null) {\n return fail(\n `Input is ${obj === null ? 'null' : typeof obj}, not an object`\n );\n }\n\n const schema = obj as Record<string, any>;\n\n // Basic property validation - most schemas have at least one of these properties\n const hasSchemaTypeProperties =\n typeof schema.type === 'string' ||\n Array.isArray(schema.enum) ||\n typeof schema.properties === 'object' ||\n typeof schema.items === 'object' ||\n typeof schema.allOf === 'object' ||\n typeof schema.anyOf === 'object' ||\n typeof schema.oneOf === 'object' ||\n typeof schema.not === 'object';\n\n if (!hasSchemaTypeProperties) {\n return fail(\n 'Missing required schema-defining properties (type, enum, properties, items, allOf, anyOf, oneOf, not)'\n );\n }\n\n // If it has a type, validate it's one of the allowed OpenAPI schema types\n if (schema.type !== undefined) {\n const validTypes = [\n 'string',\n 'number',\n 'integer',\n 'boolean',\n 'array',\n 'object',\n 'null'\n ];\n\n // Type can be a string or an array of strings\n if (typeof schema.type === 'string') {\n if (!validTypes.includes(schema.type)) {\n return fail(\n `Invalid type: ${schema.type}. Must be one of ${validTypes.join(\n ', '\n )}`\n );\n }\n } else if (Array.isArray(schema.type)) {\n for (const type of schema.type) {\n if (typeof type !== 'string' || !validTypes.includes(type)) {\n return fail(\n `Array of types contains invalid value: ${type}. Must be one of ${validTypes.join(\n ', '\n )}`\n );\n }\n }\n } else {\n return fail(\n `Type must be a string or array of strings, got ${typeof schema.type}`\n );\n }\n }\n\n // If it has items (for array type), validate that items is an object (without recursion)\n if (schema.items !== undefined) {\n if (typeof schema.items !== 'object' || schema.items === null) {\n return fail(\n `Items must be an object, got ${\n schema.items === null ? 'null' : typeof schema.items\n }`\n );\n }\n }\n\n // If it has properties (for object type), validate that properties is an object (without recursion)\n if (schema.properties !== undefined) {\n if (typeof schema.properties !== 'object' || schema.properties === null) {\n return fail(\n `Properties must be an object, got ${\n schema.properties === null ? 'null' : typeof schema.properties\n }`\n );\n }\n }\n\n // Check if advanced schema constructs are valid (without recursion)\n const schemaArrays = ['allOf', 'anyOf', 'oneOf'];\n for (const arrayType of schemaArrays) {\n if (schema[arrayType] !== undefined) {\n if (!Array.isArray(schema[arrayType])) {\n return fail(\n `${arrayType} must be an array, got ${typeof schema[arrayType]}`\n );\n }\n\n // Just check that each item is an object (without recursion)\n for (let i = 0; i < schema[arrayType].length; i++) {\n const subSchema = schema[arrayType][i];\n if (typeof subSchema !== 'object' || subSchema === null) {\n return fail(\n `Item ${i} in ${arrayType} must be an object, got ${\n subSchema === null ? 'null' : typeof subSchema\n }`\n );\n }\n }\n }\n }\n\n // Check if 'not' is an object\n if (schema.not !== undefined) {\n if (typeof schema.not !== 'object' || schema.not === null) {\n return fail(\n `'not' must be an object, got ${\n schema.not === null ? 'null' : typeof schema.not\n }`\n );\n }\n }\n\n // If we have additionalProperties, make sure it's a boolean or an object\n if (schema.additionalProperties !== undefined) {\n if (\n typeof schema.additionalProperties !== 'boolean' &&\n (typeof schema.additionalProperties !== 'object' ||\n schema.additionalProperties === null)\n ) {\n return fail(\n `additionalProperties must be a boolean or an object, got ${\n schema.additionalProperties === null\n ? 'null'\n : typeof schema.additionalProperties\n }`\n );\n }\n }\n\n // Check basic format if present (for string type)\n if (schema.format !== undefined && typeof schema.format !== 'string') {\n return fail(`format must be a string, got ${typeof schema.format}`);\n }\n\n // Check number constraints\n const numberConstraints = [\n 'minimum',\n 'maximum',\n 'exclusiveMinimum',\n 'exclusiveMaximum',\n 'multipleOf'\n ];\n for (const constraint of numberConstraints) {\n if (\n schema[constraint] !== undefined &&\n typeof schema[constraint] !== 'number'\n ) {\n return fail(\n `${constraint} must be a number, got ${typeof schema[constraint]}`\n );\n }\n }\n\n // Check string constraints\n if (\n schema.minLength !== undefined &&\n (typeof schema.minLength !== 'number' || schema.minLength < 0)\n ) {\n return fail(\n `minLength must be a non-negative number, got ${\n typeof schema.minLength === 'number'\n ? schema.minLength\n : typeof schema.minLength\n }`\n );\n }\n if (\n schema.maxLength !== undefined &&\n (typeof schema.maxLength !== 'number' || schema.maxLength < 0)\n ) {\n return fail(\n `maxLength must be a non-negative number, got ${\n typeof schema.maxLength === 'number'\n ? schema.maxLength\n : typeof schema.maxLength\n }`\n );\n }\n if (schema.pattern !== undefined && typeof schema.pattern !== 'string') {\n return fail(`pattern must be a string, got ${typeof schema.pattern}`);\n }\n\n // Check array constraints\n if (\n schema.minItems !== undefined &&\n (typeof schema.minItems !== 'number' || schema.minItems < 0)\n ) {\n return fail(\n `minItems must be a non-negative number, got ${\n typeof schema.minItems === 'number'\n ? schema.minItems\n : typeof schema.minItems\n }`\n );\n }\n if (\n schema.maxItems !== undefined &&\n (typeof schema.maxItems !== 'number' || schema.maxItems < 0)\n ) {\n return fail(\n `maxItems must be a non-negative number, got ${\n typeof schema.maxItems === 'number'\n ? schema.maxItems\n : typeof schema.maxItems\n }`\n );\n }\n if (\n schema.uniqueItems !== undefined &&\n typeof schema.uniqueItems !== 'boolean'\n ) {\n return fail(\n `uniqueItems must be a boolean, got ${typeof schema.uniqueItems}`\n );\n }\n\n // Check object constraints\n if (\n schema.minProperties !== undefined &&\n (typeof schema.minProperties !== 'number' || schema.minProperties < 0)\n ) {\n return fail(\n `minProperties must be a non-negative number, got ${\n typeof schema.minProperties === 'number'\n ? schema.minProperties\n : typeof schema.minProperties\n }`\n );\n }\n if (\n schema.maxProperties !== undefined &&\n (typeof schema.maxProperties !== 'number' || schema.maxProperties < 0)\n ) {\n return fail(\n `maxProperties must be a non-negative number, got ${\n typeof schema.maxProperties === 'number'\n ? schema.maxProperties\n : typeof schema.maxProperties\n }`\n );\n }\n if (schema.required !== undefined) {\n if (!Array.isArray(schema.required)) {\n return fail(`required must be an array, got ${typeof schema.required}`);\n }\n for (let i = 0; i < schema.required.length; i++) {\n const prop = schema.required[i];\n if (typeof prop !== 'string') {\n return fail(\n `Item ${i} in required array must be a string, got ${typeof prop}`\n );\n }\n }\n }\n\n // It has passed all the first-level checks\n return true;\n}\n", "import { OpenAPIV3 } from 'openapi-types';\nimport { OutputKind, PanelInputSchema } from '../core/provider';\nimport { Property } from './types';\n\nfunction getProperties<K extends OutputKind, I>(\n inputSchema: OpenAPIV3.SchemaObject,\n panelInput: PanelInputSchema<K, I>\n): Property[] {\n if (inputSchema.properties == null) {\n throw new Error('Input schema must have properties');\n }\n const propertiesFromSchema = inputSchema.properties;\n const properties: Property[] = [];\n\n const order = getOrder(inputSchema, panelInput);\n order.forEach((propertyKey) => {\n const id = propertyKey;\n const schema =\n (propertiesFromSchema[propertyKey] as OpenAPIV3.SchemaObject) ??\n undefined;\n properties.push({ id, schema });\n });\n\n return properties;\n}\n\nfunction getOrder<K extends OutputKind, I>(\n inputSchema: OpenAPIV3.SchemaObject,\n panelInput: PanelInputSchema<K, I>\n): string[] {\n const panelInputOrder = panelInput.order;\n if (panelInputOrder != null && Array.isArray(panelInputOrder)) {\n return panelInputOrder;\n }\n\n if (inputSchema.properties == null) {\n throw new Error('Input schema must have properties');\n }\n const propertiesFromSchema = inputSchema.properties;\n const orderFromKeys = Object.keys(propertiesFromSchema);\n const orderFromExtensionKeyword = getOrderFromExtensionKeyword(\n inputSchema,\n panelInput\n );\n\n let order = orderFromExtensionKeyword ?? orderFromKeys;\n\n if (panelInputOrder != null && typeof panelInputOrder === 'function') {\n order = panelInputOrder(order);\n }\n\n // Return order with no duplicates\n return [...new Set(order)];\n}\n\n/**\n * Get the order from an extension keyword in the input schema (e.g. x-order) if it exists.\n */\nfunction getOrderFromExtensionKeyword<K extends OutputKind, I>(\n inputSchema: OpenAPIV3.SchemaObject,\n panelInput: PanelInputSchema<K, I>\n): string[] | undefined {\n if (panelInput.orderExtensionKeyword == null) {\n return undefined;\n }\n\n if (\n typeof panelInput.orderExtensionKeyword !== 'string' &&\n !Array.isArray(panelInput.orderExtensionKeyword)\n ) {\n throw new Error(\n 'orderExtensionKeyword must be a string or an array of strings'\n );\n }\n const orderExtensionKeywords =\n typeof panelInput.orderExtensionKeyword === 'string'\n ? [panelInput.orderExtensionKeyword]\n : panelInput.orderExtensionKeyword;\n\n const orderExtensionKeyword = orderExtensionKeywords.find(\n (extensionKeyword) => {\n return extensionKeyword in inputSchema;\n }\n );\n\n if (orderExtensionKeyword == null) {\n return undefined;\n }\n const order =\n // @ts-ignore\n inputSchema[orderExtensionKeyword] as string[];\n\n return order;\n}\n\nexport default getProperties;\n", "/* eslint-disable @typescript-eslint/no-unused-vars */\nimport { BuilderRenderFunctionContext } from '@cesdk/cesdk-js';\nimport {\n EnumValue,\n ExtensionImglyBuilder,\n GetPropertyInput,\n Property,\n PropertyInput\n} from './types';\nimport Provider, {\n Output,\n OutputKind,\n PanelInputSchema\n} from '../core/provider';\nimport { UIOptions, CommonConfiguration } from '../types';\nimport { OpenAPIV3 } from 'openapi-types';\nimport getProperties from './getProperties';\nimport { getLabelFromId } from '../utils/utils';\nimport { buildPropertyContext } from '../utils/propertyContext';\nimport { resolvePropertyDefault } from '../utils/propertyResolver';\n\nfunction createInputLabelArray<K extends OutputKind, I, O extends Output>(\n property: Property,\n provider: Provider<K, I, O>,\n kind: K,\n valueId?: string\n): string[] {\n const baseKey = `property.${property.id}${valueId ? `.${valueId}` : ''}`;\n return [\n `ly.img.plugin-ai-${kind}-generation-web.${provider.id}.${baseKey}`,\n `ly.img.plugin-ai-generation-web.${baseKey}`,\n `ly.img.plugin-ai-${kind}-generation-web.${provider.id}.defaults.${baseKey}`,\n `ly.img.plugin-ai-generation-web.defaults.${baseKey}`\n ];\n}\n\nfunction extractEnumMetadata(schema: any): {\n labels: Record<string, string>;\n icons: Record<string, string>;\n} {\n const labels =\n 'x-imgly-enum-labels' in schema &&\n typeof schema['x-imgly-enum-labels'] === 'object'\n ? (schema['x-imgly-enum-labels'] as Record<string, string>)\n : {};\n\n const icons =\n 'x-imgly-enum-icons' in schema &&\n typeof schema['x-imgly-enum-icons'] === 'object'\n ? (schema['x-imgly-enum-icons'] as Record<string, string>)\n : {};\n\n return { labels, icons };\n}\n\nfunction renderProperty<K extends OutputKind, I, O extends Output>(\n context: BuilderRenderFunctionContext<any>,\n property: Property,\n provider: Provider<K, I, O>,\n panelInput: PanelInputSchema<K, I>,\n options: UIOptions,\n config: CommonConfiguration<I, O>,\n kind: K,\n providerConfig?: any\n): GetPropertyInput | undefined {\n if (property.schema == null) {\n if (\n panelInput.renderCustomProperty != null &&\n panelInput.renderCustomProperty[property.id] != null\n ) {\n // Extend context with provider configuration for custom properties\n const extendedContext = {\n ...context,\n providerConfig,\n config\n };\n return panelInput.renderCustomProperty[property.id](\n extendedContext,\n property\n );\n } else {\n return undefined;\n }\n }\n const propertyWithSchema: Required<Property> = property as Required<Property>;\n const type = property.schema.type;\n\n if (\n panelInput.renderCustomProperty != null &&\n panelInput.renderCustomProperty[property.id] != null\n ) {\n // Extend context with provider configuration for custom properties\n const extendedContext = {\n ...context,\n providerConfig,\n config\n };\n return panelInput.renderCustomProperty[property.id](\n extendedContext,\n property\n );\n }\n\n switch (type) {\n case 'string': {\n if (property.schema.enum != null) {\n return renderEnumProperty(\n context,\n propertyWithSchema,\n provider,\n panelInput,\n options,\n config,\n kind,\n providerConfig\n );\n } else {\n return renderStringProperty(\n context,\n propertyWithSchema,\n provider,\n panelInput,\n options,\n config,\n kind,\n providerConfig\n );\n }\n }\n\n case 'boolean': {\n return renderBooleanProperty(\n context,\n propertyWithSchema,\n provider,\n panelInput,\n options,\n config,\n kind,\n providerConfig\n );\n }\n\n case 'number':\n case 'integer': {\n return renderIntegerProperty(\n context,\n propertyWithSchema,\n provider,\n panelInput,\n options,\n config,\n kind,\n providerConfig\n );\n }\n\n case 'object': {\n return renderObjectProperty(\n context,\n propertyWithSchema,\n provider,\n panelInput,\n options,\n config,\n kind,\n providerConfig\n );\n }\n\n case 'array': {\n // not supported yet\n break;\n }\n\n case undefined: {\n if (\n property.schema.anyOf != null &&\n Array.isArray(property.schema.anyOf)\n ) {\n return renderAnyOfProperty(\n context,\n propertyWithSchema,\n provider,\n panelInput,\n options,\n config,\n kind,\n providerConfig\n );\n }\n break;\n }\n\n default: {\n // eslint-disable-next-line no-console\n console.error(`Unsupported property type: ${type}`);\n }\n }\n}\n\nfunction renderObjectProperty<K extends OutputKind, I, O extends Output>(\n context: BuilderRenderFunctionContext<any>,\n property: Required<Property>,\n provider: Provider<K, I, O>,\n panelInput: PanelInputSchema<K, I>,\n options: UIOptions,\n config: CommonConfiguration<I, O>,\n kind: K,\n providerConfig?: any\n): GetPropertyInput {\n const properties = getProperties(property.schema ?? {}, panelInput);\n\n const childInputs = properties.reduce((acc, childProperty) => {\n const getInput = renderProperty(\n context,\n childProperty,\n provider,\n panelInput,\n options,\n config,\n kind,\n providerConfig\n );\n if (getInput != null) {\n acc[childProperty.id] = getInput();\n }\n return acc;\n }, {} as Record<string, PropertyInput>);\n\n return () => ({\n id: property.id,\n type: 'object',\n value: childInputs\n });\n}\n\nfunction renderStringProperty<K extends OutputKind, I, O extends Output>(\n context: BuilderRenderFunctionContext<any>,\n property: Required<Property>,\n provider: Provider<K, I, O>,\n panelInput: PanelInputSchema<K, I>,\n options: UIOptions,\n config: CommonConfiguration<I, O>,\n kind: K,\n providerConfig?: any\n): GetPropertyInput {\n const {\n builder,\n experimental: { global },\n engine\n } = context;\n const { id: propertyId } = property;\n\n const id = `${provider.id}.${propertyId}`;\n const inputLabel = createInputLabelArray(property, provider, kind);\n\n // Resolve default value from property configuration\n const propertyContext = buildPropertyContext(engine, options.cesdk);\n const propertyConfig =\n providerConfig?.properties?.[propertyId] ??\n (config as any).properties?.[propertyId];\n const defaultValue = resolvePropertyDefault(\n propertyId,\n propertyConfig,\n propertyContext,\n property.schema.default,\n ''\n );\n\n const propertyState = global(id, defaultValue);\n\n const extension = getImglyExtensionBuilder(property.schema);\n const builderComponent =\n extension?.component != null && extension?.component === 'TextArea'\n ? 'TextArea'\n : 'TextInput';\n\n builder[builderComponent](id, {\n inputLabel,\n placeholder: options.i18n?.prompt,\n ...propertyState\n });\n\n return () => ({\n id: property.id,\n type: 'string',\n value: propertyState.value\n });\n}\n\nfunction renderEnumProperty<K extends OutputKind, I, O extends Output>(\n context: BuilderRenderFunctionContext<any>,\n property: Required<Property>,\n provider: Provider<K, I, O>,\n panelInput: PanelInputSchema<K, I>,\n options: UIOptions,\n config: CommonConfiguration<I, O>,\n kind: K,\n providerConfig?: any\n): GetPropertyInput {\n const {\n builder,\n experimental: { global },\n engine\n } = context;\n const { id: propertyId } = property;\n\n const id = `${provider.id}.${propertyId}`;\n const inputLabel = createInputLabelArray(property, provider, kind);\n\n const { labels: enumLabels, icons } = extractEnumMetadata(property.schema);\n\n const values: EnumValue[] = (property.schema.enum ?? []).map((valueId) => ({\n id: valueId,\n label: createInputLabelArray(property, provider, kind, valueId),\n icon: icons[valueId]\n }));\n\n // Resolve default value from property configuration\n const propertyContext = buildPropertyContext(engine, options.cesdk);\n const propertyConfig =\n providerConfig?.properties?.[propertyId] ??\n (config as any).properties?.[propertyId];\n const resolvedDefault = resolvePropertyDefault(\n propertyId,\n propertyConfig,\n propertyContext,\n property.schema.default,\n values[0]?.id\n );\n\n const defaultValue =\n resolvedDefault != null\n ? values.find((v) => v.id === resolvedDefault) ?? values[0]\n : values[0];\n\n const propertyState = global<EnumValue>(id, defaultValue);\n\n builder.Select(id, {\n inputLabel,\n values,\n ...propertyState\n });\n\n return () => ({\n id: property.id,\n type: 'string',\n value: propertyState.value.id\n });\n}\n\nfunction renderBooleanProperty<K extends OutputKind, I, O extends Output>(\n context: BuilderRenderFunctionContext<any>,\n property: Required<Property>,\n provider: Provider<K, I, O>,\n panelInput: PanelInputSchema<K, I>,\n options: UIOptions,\n config: CommonConfiguration<I, O>,\n kind: K,\n providerConfig?: any\n): GetPropertyInput {\n const {\n builder,\n experimental: { global },\n engine\n } = context;\n const { id: propertyId } = property;\n\n const id = `${provider.id}.${propertyId}`;\n const inputLabel = createInputLabelArray(property, provider, kind);\n\n // Resolve default value from property configuration\n const propertyContext = buildPropertyContext(engine, options.cesdk);\n const propertyConfig =\n providerConfig?.properties?.[propertyId] ??\n (config as any).properties?.[propertyId];\n const defaultValue = !!resolvePropertyDefault(\n propertyId,\n propertyConfig,\n propertyContext,\n property.schema.default,\n false\n );\n const propertyState = global<boolean>(id, defaultValue);\n\n builder.Checkbox(id, {\n inputLabel,\n ...propertyState\n });\n\n return () => ({\n id: property.id,\n type: 'boolean',\n value: propertyState.value\n });\n}\n\nfunction renderIntegerProperty<K extends OutputKind, I, O extends Output>(\n context: BuilderRenderFunctionContext<any>,\n property: Required<Property>,\n provider: Provider<K, I, O>,\n panelInput: PanelInputSchema<K, I>,\n options: UIOptions,\n config: CommonConfiguration<I, O>,\n kind: K,\n providerConfig?: any\n): GetPropertyInput {\n const {\n builder,\n experimental: { global },\n engine\n } = context;\n const { id: propertyId } = property;\n\n const id = `${provider.id}.${propertyId}`;\n const inputLabel = createInputLabelArray(property, provider, kind);\n\n const minValue = property.schema.minimum;\n const maxValue = property.schema.maximum;\n\n // Resolve default value from property configuration\n const propertyContext = buildPropertyContext(engine, options.cesdk);\n const propertyConfig =\n providerConfig?.properties?.[propertyId] ??\n (config as any).properties?.[propertyId];\n let schemaDefault = property.schema.default;\n if (schemaDefault == null) {\n if (minValue != null) {\n schemaDefault = minValue;\n } else if (maxValue != null) {\n schemaDefault = maxValue;\n } else {\n schemaDefault = 0;\n }\n }\n const defaultValue = resolvePropertyDefault(\n propertyId,\n propertyConfig,\n propertyContext,\n schemaDefault,\n schemaDefault\n );\n\n const propertyState = global<number>(id, defaultValue);\n\n if (minValue != null && maxValue != null) {\n let step = property.schema.type === 'number' ? 0.1 : 1;\n if (\n 'x-imgly-step' in property.schema &&\n typeof property.schema['x-imgly-step'] === 'number'\n ) {\n step = property.schema['x-imgly-step'];\n }\n\n builder.Slider(id, {\n inputLabel,\n min: minValue,\n max: maxValue,\n step,\n ...propertyState\n });\n } else {\n builder.NumberInput(id, {\n inputLabel,\n min: minValue,\n max: maxValue,\n ...propertyState\n });\n }\n\n return () => ({\n id: property.id,\n type: 'integer',\n value: propertyState.value\n });\n}\n\nfunction renderAnyOfProperty<K extends OutputKind, I, O extends Output>(\n context: BuilderRenderFunctionContext<any>,\n property: Required<Property>,\n provider: Provider<K, I, O>,\n panelInput: PanelInputSchema<K, I>,\n options: UIOptions,\n config: CommonConfiguration<I, O>,\n kind: K,\n providerConfig?: any\n): GetPropertyInput | undefined {\n const {\n builder,\n experimental: { global },\n engine\n } = context;\n const { id: propertyId } = property;\n\n const id = `${provider.id}.${propertyId}`;\n const inputLabel = createInputLabelArray(property, provider, kind);\n\n const anyOf = (property.schema.anyOf ?? []) as OpenAPIV3.SchemaObject[];\n const values: EnumValue[] = [];\n const conditionalRender: Record<string, () => GetPropertyInput> = {};\n const conditionalInputs: Record<string, () => PropertyInput> = {};\n const { labels, icons } = extractEnumMetadata(property.schema);\n\n // Resolve default value from property configuration\n const propertyContext = buildPropertyContext(engine, options.cesdk);\n const propertyConfig =\n providerConfig?.properties?.[propertyId] ??\n (config as any).properties?.[propertyId];\n\n const renderFunctionMap: Record<string, Function> = {\n string: renderStringProperty,\n boolean: renderBooleanProperty,\n integer: renderIntegerProperty,\n object: renderObjectProperty\n };\n\n const extractValueId = (anySchema: any, schemaId: string): string =>\n (anySchema as any).$ref\n ? (anySchema as any).$ref.split('/').pop()\n : schemaId.split('.').pop() ?? schemaId;\n\n const createEnumValue = (enumId: string, valueId: string): EnumValue => ({\n id: enumId,\n label: createInputLabelArray(property, provider, kind, valueId),\n icon: icons[valueId] ?? icons[enumId]\n });\n\n anyOf.forEach((anySchema, index) => {\n const schemaId = `${provider.id}.${propertyId}.anyOf[${index}]`;\n\n if ((anySchema as any).$ref || anySchema.title) {\n const refName = (anySchema as any).$ref\n ? (anySchema as any).$ref.split('/').pop()\n : anySchema.title;\n\n conditionalRender[schemaId] = () =>\n renderObjectProperty(\n context,\n {\n id: schemaId,\n schema: { ...anySchema, title: labels[refName] || refName }\n },\n provider,\n panelInput,\n options,\n config,\n kind,\n providerConfig\n );\n\n values.push(createEnumValue(schemaId, refName));\n } else if (anySchema.type === 'string' && anySchema.enum) {\n anySchema.enum.forEach((valueId) => {\n values.push(createEnumValue(valueId, valueId));\n });\n } else if (anySchema.type && renderFunctionMap[anySchema.type]) {\n const renderFunction = renderFunctionMap[anySchema.type];\n conditionalRender[schemaId] = () =>\n renderFunction(\n context,\n { id: schemaId, schema: { ...anySchema, title: anySchema.title } },\n provider,\n panelInput,\n options,\n config,\n kind,\n providerConfig\n );\n\n const valueId = extractValueId(anySchema, schemaId);\n values.push(\n anySchema.type === 'string' && !anySchema.enum\n ? {\n id: schemaId,\n label: anySchema.title || valueId,\n icon:\n (anySchema.title && icons[anySchema.title]) || icons[valueId]\n }\n : createEnumValue(schemaId, valueId)\n );\n }\n });\n\n // Resolve the default using property configuration\n const resolvedDefault = resolvePropertyDefault(\n propertyId,\n propertyConfig,\n propertyContext,\n property.schema.default,\n null\n );\n\n const defaultValue =\n resolvedDefault != null\n ? values.find((value) => value.id === resolvedDefault) ?? values[0]\n : values[0];\n\n const propertyState = global<EnumValue>(id, defaultValue);\n\n builder.Select(id, {\n inputLabel,\n values,\n ...propertyState\n });\n\n if (propertyState.value.id in conditionalRender) {\n const inputs = conditionalRender[propertyState.value.id]();\n conditionalInputs[propertyState.value.id] = inputs;\n }\n\n return () => {\n const conditionalInput = conditionalInputs[propertyState.value.id];\n\n if (conditionalInput != null) {\n return {\n ...conditionalInput(),\n id: property.id\n };\n }\n\n return {\n id: property.id,\n type: 'string',\n value: propertyState.value.id\n };\n };\n}\n\nfunction getImglyExtensionBuilder(\n schema: OpenAPIV3.SchemaObject\n): ExtensionImglyBuilder | undefined {\n if ('x-imgly-builder' in schema) {\n const extension = schema['x-imgly-builder'] as ExtensionImglyBuilder;\n return extension;\n }\n\n return undefined;\n}\n\nexport default renderProperty;\n", "/**\n * Default translations for AI generation properties and enum values.\n * These are automatically applied as fallback translations for all AI providers.\n *\n * Structure:\n * - Property translations: `ly.img.plugin-ai-generation-web.defaults.property.${property.id}`\n * - Enum value translations: `ly.img.plugin-ai-generation-web.defaults.property.${property.id}.${enumValue}`\n *\n * Based on actual OpenAPI Input schemas from all AI provider packages.\n */\nexport const defaultTranslations: Record<string, string> = {\n // Core generation properties (found in all Input schemas)\n 'ly.img.plugin-ai-generation-web.defaults.property.prompt': 'Prompt',\n 'ly.img.plugin-ai-generation-web.defaults.property.style': 'Style',\n\n // Common properties\n 'ly.img.plugin-ai-generation-web.defaults.property.image_size': 'Image Size',\n 'ly.img.plugin-ai-generation-web.defaults.property.size': 'Image Size',\n 'ly.img.plugin-ai-generation-web.defaults.property.colors': 'Colors',\n 'ly.img.plugin-ai-generation-web.defaults.property.background': 'Background',\n\n // Common dimension properties\n 'ly.img.plugin-ai-generation-web.defaults.property.width': 'Width',\n 'ly.img.plugin-ai-generation-web.defaults.property.height': 'Height',\n 'ly.img.plugin-ai-generation-web.defaults.property.aspect_ratio':\n 'Aspect Ratio',\n\n // Common temporal properties\n 'ly.img.plugin-ai-generation-web.defaults.property.duration': 'Duration',\n 'ly.img.plugin-ai-generation-web.defaults.property.resolution': 'Resolution',\n 'ly.img.plugin-ai-generation-web.defaults.property.generate_audio':\n 'Generate Audio',\n\n // Common audio properties\n 'ly.img.plugin-ai-generation-web.defaults.property.voice_id': 'Voice',\n 'ly.img.plugin-ai-generation-web.defaults.property.speed': 'Speed',\n 'ly.img.plugin-ai-generation-web.defaults.property.text': 'Text',\n 'ly.img.plugin-ai-generation-web.defaults.property.duration_seconds':\n 'Duration (seconds)',\n\n // Custom renderer translations for Recraft providers\n 'ly.img.plugin-ai-generation-web.defaults.property.style.type': 'Type',\n 'ly.img.plugin-ai-generation-web.defaults.property.style.type.image': 'Image',\n 'ly.img.plugin-ai-generation-web.defaults.property.style.type.vector':\n 'Vector',\n 'ly.img.plugin-ai-generation-web.defaults.property.style.type.icon': 'Icon',\n\n // Enum value translations - common formats\n 'ly.img.plugin-ai-generation-web.defaults.property.image_size.square':\n 'Square',\n 'ly.img.plugin-ai-generation-web.defaults.property.image_size.portrait':\n 'Portrait',\n 'ly.img.plugin-ai-generation-web.defaults.property.image_size.landscape':\n 'Landscape',\n\n // Enum value translations - background\n 'ly.img.plugin-ai-generation-web.defaults.property.background.auto': 'Auto',\n 'ly.img.plugin-ai-generation-web.defaults.property.background.transparent':\n 'Transparent',\n\n // Enum value translations - common aspect ratios\n 'ly.img.plugin-ai-generation-web.defaults.property.aspect_ratio.1:1':\n '1:1 (Square)',\n 'ly.img.plugin-ai-generation-web.defaults.property.aspect_ratio.16:9':\n '16:9 (Widescreen)',\n 'ly.img.plugin-ai-generation-web.defaults.property.aspect_ratio.9:16':\n '9:16 (Vertical)',\n 'ly.img.plugin-ai-generation-web.defaults.property.aspect_ratio.4:3': '4:3',\n 'ly.img.plugin-ai-generation-web.defaults.property.aspect_ratio.3:4': '3:4',\n\n // Enum value translations - common resolutions\n 'ly.img.plugin-ai-generation-web.defaults.property.resolution.720p':\n '720p HD',\n 'ly.img.plugin-ai-generation-web.defaults.property.resolution.1080p':\n '1080p Full HD'\n};\n", "import { Property } from './types';\nimport Provider, { Output, OutputKind } from '../core/provider';\nimport { UIOptions } from '../types';\nimport { defaultTranslations } from './defaultTranslations';\n\nfunction formatEnumLabel(enumValue: string): string {\n return (\n enumValue\n // Replace underscores with spaces\n .replace(/_/g, ' ')\n // Handle specific cases first\n .replace(/\\b3d\\b/gi, '3D')\n .replace(/\\b2d\\b/gi, '2D')\n // Capitalize each word\n .replace(/\\b\\w/g, (char) => char.toUpperCase())\n );\n}\n\n/**\n * Extracts translations from OpenAPI schema properties and sets them via cesdk.i18n\n * This includes:\n * - Schema property titles as `ly.img.plugin-ai-${kind}-generation-web.${provider.id}.defaults.property.${property.id}`\n * - Enum value labels as `ly.img.plugin-ai-${kind}-generation-web.${provider.id}.defaults.property.${property.id}.${valueId}`\n * - AnyOf enum value labels with the same pattern\n */\nexport function extractAndSetSchemaTranslations<\n K extends OutputKind,\n I,\n O extends Output\n>(\n properties: Property[],\n provider: Provider<K, I, O>,\n options: UIOptions,\n kind: K\n): void {\n const translations: Record<string, string> = {};\n\n const createTranslationKey = (propertyId: string, valueId?: string): string =>\n `ly.img.plugin-ai-${kind}-generation-web.${\n provider.id\n }.defaults.property.${propertyId}${valueId ? `.${valueId}` : ''}`;\n\n const extractEnumLabels = (schema: any): Record<string, string> =>\n 'x-imgly-enum-labels' in schema &&\n typeof schema['x-imgly-enum-labels'] === 'object'\n ? (schema['x-imgly-enum-labels'] as Record<string, string>)\n : {};\n\n const addEnumTranslations = (\n enumValues: any[],\n propertyId: string,\n enumLabels: Record<string, string>\n ): void => {\n enumValues.forEach((enumValue) => {\n const valueId = String(enumValue);\n const labelValue = enumLabels[valueId] || formatEnumLabel(valueId);\n translations[createTranslationKey(propertyId, valueId)] = labelValue;\n });\n };\n\n properties.forEach((property) => {\n if (property.schema?.title) {\n translations[createTranslationKey(property.id)] = property.schema.title;\n }\n\n if (property.schema?.enum) {\n const enumLabels = extractEnumLabels(property.schema);\n addEnumTranslations(property.schema.enum, property.id, enumLabels);\n }\n\n if (property.schema?.anyOf && Array.isArray(property.schema.anyOf)) {\n const enumLabels = extractEnumLabels(property.schema);\n\n property.schema.anyOf.forEach((anySchema) => {\n const schema = anySchema as any;\n if (schema.enum && Array.isArray(schema.enum)) {\n addEnumTranslations(schema.enum, property.id, enumLabels);\n } else if (schema.$ref) {\n const refName = schema.$ref.split('/').pop();\n if (refName && enumLabels[refName]) {\n translations[createTranslationKey(property.id, refName)] =\n enumLabels[refName];\n }\n } else if (schema.title) {\n const refName = schema.title;\n const labelValue = enumLabels[refName] || formatEnumLabel(refName);\n translations[createTranslationKey(property.id, refName)] = labelValue;\n }\n });\n }\n });\n\n const allTranslations = { ...defaultTranslations, ...translations };\n\n if (Object.keys(allTranslations).length > 0) {\n options.cesdk.i18n.setTranslations({\n en: allTranslations\n });\n }\n}\n", "import { BuilderRenderFunction } from '@cesdk/cesdk-js';\nimport { OutputKind, PanelInputSchema, type Output } from '../../core/provider';\nimport renderGenerationComponents from '../components/renderGenerationComponents';\nimport { InitializationContext } from '../../types';\nimport dereferenceDocument, {\n resolveReference\n} from '../../openapi/dereferenceDocument';\nimport { isOpenAPISchema } from '../../openapi/isOpenAPISchema';\nimport { OpenAPIV3 } from 'openapi-types';\nimport getProperties from '../../openapi/getProperties';\nimport { GetPropertyInput, PropertyInput } from '../../openapi/types';\nimport renderProperty from '../../openapi/renderProperty';\nimport { Generate } from '../../generation/createGenerateFunction';\nimport { extractAndSetSchemaTranslations } from '../../openapi/extractSchemaTranslations';\n\n/**\n * Creates a panel render function based on the schema definition in the provider.\n */\nasync function createPanelRenderFunctionFromSchema<\n K extends OutputKind,\n I,\n O extends Output\n>(\n {\n options,\n provider,\n panelInput,\n config,\n providerConfig\n }: InitializationContext<K, I, O, PanelInputSchema<K, I>>,\n generate: Generate<I, O>\n): Promise<BuilderRenderFunction<any> | undefined> {\n const { id: providerId } = provider;\n\n if (panelInput == null) {\n return undefined;\n }\n\n if (config.debug) {\n // eslint-disable-next-line no-console\n console.log(\n `Registering schema-based panel input for provider ${providerId}`\n );\n }\n\n const schemaDocument = dereferenceDocument(panelInput.document);\n const resolvedInputReference = resolveReference(\n schemaDocument,\n panelInput.inputReference\n );\n\n if (!isOpenAPISchema(resolvedInputReference, config.debug)) {\n throw new Error(\n `Input reference '${panelInput.inputReference}' does not resolve to a valid OpenAPI schema`\n );\n }\n\n const inputSchema: OpenAPIV3.SchemaObject = resolvedInputReference;\n const properties = getProperties(inputSchema, panelInput);\n\n // Extract and set translations from schema\n extractAndSetSchemaTranslations(properties, provider, options, provider.kind);\n\n const builderRenderFunction: BuilderRenderFunction<any> = (context) => {\n const { builder } = context;\n\n const getInputs: GetPropertyInput[] = [];\n builder.Section(`${providerId}.schema.section`, {\n children: () => {\n properties.forEach((property) => {\n const getInput = renderProperty(\n context,\n property,\n provider,\n panelInput,\n options,\n config,\n provider.kind,\n providerConfig\n );\n if (getInput != null) {\n if (Array.isArray(getInput)) {\n getInputs.push(...getInput);\n } else {\n getInputs.push(getInput);\n }\n }\n });\n }\n });\n\n const inputs = getInputs.map((getInput) => {\n const input = getInput();\n return input;\n });\n\n const resolveInput = (input: PropertyInput) => {\n if (input.type === 'object') {\n return Object.entries(input.value).reduce((acc, [key, value]) => {\n acc[key] = resolveInput(value);\n\n return acc;\n }, {} as Record<string, any>);\n }\n\n return input.value;\n };\n const input = inputs.reduce((acc, propertyInput) => {\n acc[propertyInput.id] = resolveInput(propertyInput);\n return acc;\n }, {} as Record<string, any>) as I;\n\n renderGenerationComponents(\n context,\n provider,\n generate,\n () => {\n return { input };\n },\n () => {\n return panelInput.getBlockInput(input);\n },\n {\n ...options,\n requiredInputs: inputSchema.required,\n createPlaceholderBlock: panelInput.userFlow === 'placeholder'\n },\n config\n );\n };\n\n return builderRenderFunction;\n}\n\nexport default createPanelRenderFunctionFromSchema;\n", "import { BuilderRenderFunction } from '@cesdk/cesdk-js';\nimport {\n Output,\n OutputKind,\n PanelInputCustom,\n PanelInputSchema\n} from '../../core/provider';\nimport { InitializationContext } from '../../types';\nimport createPanelRenderFunctionFromCustom from './createPanelRenderFunctionFromCustom';\nimport createPanelRenderFunctionFromSchema from './createPanelRenderFunctionFromSchema';\nimport { Generate } from '../../generation/createGenerateFunction';\n\n/**\n * Creates a panel render function based on the provided context, i.e. on the provider.\n */\nasync function createPanelRenderFunction<\n K extends OutputKind,\n I,\n O extends Output\n>(\n context: InitializationContext<K, I, O>,\n generate: Generate<I, O>\n): Promise<BuilderRenderFunction<any> | undefined> {\n if (context.panelInput == null) {\n return undefined;\n }\n\n switch (context.panelInput.type) {\n case 'custom': {\n return createPanelRenderFunctionFromCustom<K, I, O>(\n context as InitializationContext<K, I, O, PanelInputCustom<K, I>>,\n generate\n );\n }\n\n case 'schema': {\n return createPanelRenderFunctionFromSchema<K, I, O>(\n context as InitializationContext<K, I, O, PanelInputSchema<K, I>>,\n generate\n );\n }\n\n default: {\n if (context.config.debug) {\n // eslint-disable-next-line no-console\n console.warn(\n // @ts-ignore\n `Invalid panel input type '${panelInput.type}' - skipping`\n );\n }\n }\n }\n}\n\nexport default createPanelRenderFunction;\n", "import { IndexedDBAssetSource } from '@imgly/plugin-utils';\nimport { OutputKind, Output } from '../core/provider';\nimport { InitializationContext } from '../types';\n\n/**\n * Initializes the history asset source for the given provider.\n */\nfunction initializeHistoryAssetSource<\n K extends OutputKind,\n I,\n O extends Output\n>(context: InitializationContext<K, I, O>): string | undefined {\n const {\n provider,\n options: { engine }\n } = context;\n\n const history = provider.output.history ?? '@imgly/local';\n if (history == null || history === false) return undefined;\n\n const currentAssetSourceIds = engine.asset.findAllSources();\n\n function getUniqueHistoryId(): string {\n let id = `${provider.id}.history`;\n while (currentAssetSourceIds.includes(id)) {\n id += `-${Math.random().toString(36).substring(2, 6)}`;\n }\n return id;\n }\n\n if (history === '@imgly/local') {\n const historyId = getUniqueHistoryId();\n engine.asset.addLocalSource(historyId);\n return historyId;\n }\n if (history === '@imgly/indexedDB') {\n const historyId = getUniqueHistoryId();\n engine.asset.addSource(new IndexedDBAssetSource(historyId, engine));\n return historyId;\n }\n\n return history;\n}\n\nexport default initializeHistoryAssetSource;\n", "import { OutputKind, Output } from '../core/provider';\nimport { InitializationContext } from '../types';\n\n/**\n * Initializes the history asset source for the given provider.\n */\nfunction initializeHistoryAssetLibraryEntry<\n K extends OutputKind,\n I,\n O extends Output\n>(\n context: InitializationContext<K, I, O>,\n historyAssetSourceId?: string\n): string | undefined {\n if (historyAssetSourceId == null || !historyAssetSourceId) return;\n\n const historyAssetLibraryEntryId = `${context.provider.id}.history`;\n\n context.options.cesdk.ui.addAssetLibraryEntry({\n id: historyAssetLibraryEntryId,\n sourceIds: [historyAssetSourceId],\n sortBy: {\n sortKey: 'insertedAt',\n sortingOrder: 'Descending'\n },\n canRemove: true,\n gridItemHeight: 'square',\n gridBackgroundType: 'cover'\n });\n\n return historyAssetLibraryEntryId;\n}\n\nexport default initializeHistoryAssetLibraryEntry;\n", "export const PLUGIN_ICON_SET_ID = '@imgly/plugin-ai-generation';\n\nexport const ICON_SPRITE = `\n<svg>\n <symbol\n fill=\"none\"\n xmlns=\"http://www.w3.org/2000/svg\"\n viewBox=\"0 0 24 24\"\n id=\"@imgly/Sparkle\"\n >\n <path d=\"M5.35545 2.06745C5.24149 1.72556 4.7579 1.72556 4.64394 2.06745L4.05898 3.82232C4.02166 3.93429 3.9338 4.02215 3.82184 4.05948L2.06694 4.64459C1.72506 4.75858 1.72509 5.24217 2.06699 5.3561L3.82179 5.9409C3.93378 5.97822 4.02166 6.06609 4.05899 6.17808L4.64394 7.93291C4.7579 8.2748 5.24149 8.2748 5.35545 7.93291L5.9404 6.17806C5.97773 6.06608 6.06559 5.97821 6.17757 5.94089L7.93242 5.35594C8.27431 5.24198 8.27431 4.75839 7.93242 4.64442L6.17757 4.05947C6.06559 4.02215 5.97773 3.93428 5.9404 3.8223L5.35545 2.06745Z\" fill=\"currentColor\"/>\n<path d=\"M17.9632 3.23614C17.8026 2.80788 17.1968 2.80788 17.0362 3.23614L16.0787 5.78951C16.0285 5.92337 15.9229 6.02899 15.789 6.07918L13.2356 7.0367C12.8074 7.19729 12.8074 7.80307 13.2356 7.96366L15.789 8.92118C15.9229 8.97138 16.0285 9.077 16.0787 9.21085L17.0362 11.7642C17.1968 12.1925 17.8026 12.1925 17.9632 11.7642L18.9207 9.21086C18.9709 9.077 19.0765 8.97138 19.2104 8.92118L21.7637 7.96366C22.192 7.80307 22.192 7.1973 21.7637 7.0367L19.2104 6.07918C19.0765 6.02899 18.9709 5.92337 18.9207 5.78951L17.9632 3.23614Z\" fill=\"currentColor\"/>\n<path d=\"M9.30058 7.82012C9.54712 7.1791 10.454 7.1791 10.7006 7.82012L12.3809 12.189C12.4571 12.3871 12.6136 12.5436 12.8117 12.6198L17.1806 14.3001C17.8216 14.5466 17.8216 15.4536 17.1806 15.7001L12.8117 17.3804C12.6136 17.4566 12.4571 17.6131 12.3809 17.8112L10.7006 22.1801C10.454 22.8211 9.54712 22.8211 9.30058 22.1801L7.62024 17.8112C7.54406 17.6131 7.38754 17.4566 7.18947 17.3804L2.82061 15.7001C2.17959 15.4536 2.17959 14.5466 2.82061 14.3001L7.18947 12.6198C7.38754 12.5436 7.54406 12.3871 7.62024 12.189L9.30058 7.82012Z\" fill=\"currentColor\"/>\n\n </symbol>\n\n <symbol\n fill=\"none\"\n xmlns=\"http://www.w3.org/2000/svg\"\n viewBox=\"0 0 24 24\"\n id=\"${PLUGIN_ICON_SET_ID}/image\"\n >\n <path d=\"M3 16.5V18C3 19.6569 4.34315 21 6 21H18C19.6569 21 21 19.6569 21 18V6C21 4.34315 19.6569 3 18 3L17.999 5C18.5513 5 19 5.44772 19 6V18C19 18.5523 18.5523 19 18 19H6C5.44772 19 5 18.5523 5 18V16.5H3Z\" fill=\"currentColor\"/>\n<path d=\"M13.0982 0.884877C12.9734 0.568323 12.5254 0.568322 12.4005 0.884876L11.7485 2.53819C11.7104 2.63483 11.6339 2.71134 11.5372 2.74945L9.8839 3.40151C9.56735 3.52636 9.56734 3.97436 9.8839 4.09921L11.5372 4.75126C11.6339 4.78938 11.7104 4.86588 11.7485 4.96253L12.4005 6.61584C12.5254 6.93239 12.9734 6.9324 13.0982 6.61584L13.7503 4.96253C13.7884 4.86588 13.8649 4.78938 13.9616 4.75126L15.6149 4.09921C15.9314 3.97436 15.9314 3.52636 15.6149 3.40151L13.9616 2.74945C13.8649 2.71134 13.7884 2.63483 13.7503 2.53819L13.0982 0.884877Z\" fill=\"currentColor\"/>\n<path d=\"M6.40053 5.38488C6.52538 5.06832 6.97338 5.06832 7.09823 5.38488L8.17455 8.11392C8.21267 8.21057 8.28917 8.28707 8.38582 8.32519L11.1149 9.40151C11.4314 9.52636 11.4314 9.97436 11.1149 10.0992L8.38582 11.1755C8.28917 11.2136 8.21267 11.2901 8.17455 11.3868L7.09823 14.1158C6.97338 14.4324 6.52538 14.4324 6.40053 14.1158L5.32421 11.3868C5.2861 11.2901 5.20959 11.2136 5.11295 11.1755L2.3839 10.0992C2.06735 9.97436 2.06735 9.52636 2.3839 9.40151L5.11295 8.32519C5.20959 8.28707 5.2861 8.21057 5.32421 8.11392L6.40053 5.38488Z\" fill=\"currentColor\"/>\n<path d=\"M18.9994 16.5008V18.0004C18.9994 18.5526 18.5517 19.0004 17.9994 19.0004H9.33302L14.3753 11.4369C14.6722 10.9916 15.3266 10.9916 15.6234 11.4369L18.9994 16.5008Z\" fill=\"currentColor\"/>\n\n </symbol>\n <symbol\n fill=\"none\"\n xmlns=\"http://www.w3.org/2000/svg\"\n viewBox=\"0 0 24 24\"\n id=\"${PLUGIN_ICON_SET_ID}/video\"\n >\n<path d=\"M6 3C4.34315 3 3 4.34315 3 6V18C3 19.6569 4.34315 21 6 21H18C19.6569 21 21 19.6569 21 18V16.5H19V18C19 18.5523 18.5523 19 18 19H6C5.44772 19 5 18.5523 5 18V6C5 5.44772 5.44772 5 6 5V3Z\" fill=\"currentColor\"/>\n<path d=\"M10.9025 0.8839C11.0273 0.567345 11.4753 0.567346 11.6002 0.883901L12.2522 2.53721C12.2904 2.63386 12.3669 2.71036 12.4635 2.74848L14.1168 3.40053C14.4334 3.52538 14.4334 3.97338 14.1168 4.09823L12.4635 4.75029C12.3669 4.7884 12.2904 4.86491 12.2522 4.96155L11.6002 6.61486C11.4753 6.93142 11.0273 6.93142 10.9025 6.61486L10.2504 4.96155C10.2123 4.86491 10.1358 4.7884 10.0392 4.75029L8.38585 4.09823C8.0693 3.97338 8.0693 3.52538 8.38585 3.40053L10.0392 2.74848C10.1358 2.71036 10.2123 2.63386 10.2504 2.53721L10.9025 0.8839Z\" fill=\"currentColor\"/>\n<path d=\"M18.9019 3.3845C19.0267 3.06795 19.4747 3.06795 19.5996 3.3845L20.6759 6.11355C20.714 6.2102 20.7905 6.2867 20.8872 6.32482L23.6162 7.40114C23.9328 7.52598 23.9328 7.97399 23.6162 8.09883L20.8872 9.17515C20.7905 9.21327 20.714 9.28977 20.6759 9.38642L19.5996 12.1155C19.4747 12.432 19.0267 12.432 18.9019 12.1155L17.8255 9.38642C17.7874 9.28977 17.7109 9.21327 17.6143 9.17515L14.8852 8.09883C14.5687 7.97399 14.5687 7.52598 14.8852 7.40114L17.6143 6.32482C17.7109 6.2867 17.7874 6.2102 17.8255 6.11355L18.9019 3.3845Z\" fill=\"currentColor\"/>\n<path d=\"M14.9994 13.2862C15.5089 12.8859 15.5089 12.1141 14.9995 11.7137L10.618 8.27047C9.96188 7.75485 9.00011 8.22225 9.00011 9.05673L9.00011 15.9429C9.00011 16.7773 9.96185 17.2448 10.618 16.7292L14.9994 13.2862Z\" fill=\"currentColor\"/>\n </symbol>\n <symbol\n fill=\"none\"\n xmlns=\"http://www.w3.org/2000/svg\"\n viewBox=\"0 0 24 24\"\n id=\"${PLUGIN_ICON_SET_ID}/audio\"\n >\n <path d=\"M6 3.80273C4.2066 4.84016 3 6.77919 3 9.00004V12.8153C3 15.931 5.39501 18.4873 8.44444 18.7436V20.9645C8.44444 22.2198 9.89427 22.9198 10.8773 22.1392L15.1265 18.7647H15.5C17.8285 18.7647 19.8472 17.4384 20.8417 15.5H18.4187C17.6889 16.2784 16.6512 16.7647 15.5 16.7647H14.9522C14.6134 16.7647 14.2846 16.8794 14.0193 17.0901L10.4444 19.929V18.2597C10.4444 17.4341 9.77513 16.7647 8.9495 16.7647C7.80494 16.7647 6.77409 16.2779 6.05276 15.5H6V15.4419C5.37798 14.7439 5 13.8237 5 12.8153V9.00004C5 7.98559 5.37764 7.05935 6 6.35422V3.80273Z\" fill=\"currentColor\"/>\n<path d=\"M11.6002 1.8839C11.4753 1.56735 11.0273 1.56735 10.9025 1.8839L10.2504 3.53721C10.2123 3.63386 10.1358 3.71036 10.0392 3.74848L8.38585 4.40053C8.0693 4.52538 8.0693 4.97338 8.38585 5.09823L10.0392 5.75029C10.1358 5.7884 10.2123 5.86491 10.2504 5.96155L10.9025 7.61486C11.0273 7.93142 11.4753 7.93142 11.6002 7.61486L12.2522 5.96155C12.2904 5.86491 12.3669 5.7884 12.4635 5.75029L14.1168 5.09823C14.4334 4.97338 14.4334 4.52538 14.1168 4.40053L12.4635 3.74848C12.3669 3.71036 12.2904 3.63386 12.2522 3.53721L11.6002 1.8839Z\" fill=\"currentColor\"/>\n<path d=\"M19.5996 4.3845C19.4747 4.06795 19.0267 4.06795 18.9019 4.3845L17.8255 7.11355C17.7874 7.2102 17.7109 7.2867 17.6143 7.32482L14.8852 8.40114C14.5687 8.52598 14.5687 8.97399 14.8852 9.09883L17.6143 10.1752C17.7109 10.2133 17.7874 10.2898 17.8255 10.3864L18.9019 13.1155C19.0267 13.432 19.4747 13.432 19.5996 13.1155L20.6759 10.3864C20.714 10.2898 20.7905 10.2133 20.8872 10.1752L23.6162 9.09883C23.9328 8.97399 23.9328 8.52598 23.6162 8.40114L20.8872 7.32482C20.7905 7.2867 20.714 7.2102 20.6759 7.11355L19.5996 4.3845Z\" fill=\"currentColor\"/>\n </symbol>\n <symbol\n fill=\"none\"\n xmlns=\"http://www.w3.org/2000/svg\"\n viewBox=\"0 0 24 24\"\n id=\"@imgly/MixingPlate\"\n >\n <path d=\"M9.75 9C10.5784 9 11.25 8.32843 11.25 7.5C11.25 6.67157 10.5784 6 9.75 6C8.92157 6 8.25 6.67157 8.25 7.5C8.25 8.32843 8.92157 9 9.75 9Z\" fill=\"currentColor\"/>\n<path d=\"M7 13C7.82843 13 8.5 12.3284 8.5 11.5C8.5 10.6716 7.82843 10 7 10C6.17157 10 5.5 10.6716 5.5 11.5C5.5 12.3284 6.17157 13 7 13Z\" fill=\"currentColor\"/>\n<path d=\"M15.75 7.5C15.75 8.32843 15.0784 9 14.25 9C13.4216 9 12.75 8.32843 12.75 7.5C12.75 6.67157 13.4216 6 14.25 6C15.0784 6 15.75 6.67157 15.75 7.5Z\" fill=\"currentColor\"/>\n<path d=\"M17 13C17.8284 13 18.5 12.3284 18.5 11.5C18.5 10.6716 17.8284 10 17 10C16.1716 10 15.5 10.6716 15.5 11.5C15.5 12.3284 16.1716 13 17 13Z\" fill=\"currentColor\"/>\n<path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M8.26309 2.77709C10.6681 1.77921 13.4829 1.7322 15.9209 2.64297C18.1572 3.47923 20.0876 5.09285 21.1766 7.28598C22.3395 9.62772 22.4889 13.1077 20.3864 15.2982C19.2693 16.4621 17.7657 16.9982 16.0026 16.9997C15.7897 16.9997 15.5555 16.9864 15.3497 16.9745C15.309 16.9722 15.2694 16.9699 15.2313 16.9679C14.9817 16.9542 14.761 16.9455 14.5569 16.9539C14.124 16.9718 13.9598 17.0612 13.89 17.1324C13.718 17.3081 13.6946 17.6672 13.8854 17.8895C14.2899 18.3608 14.5016 18.9277 14.5016 19.5497C14.5016 20.2206 14.3086 20.9011 13.7542 21.3896C13.2471 21.837 12.6082 21.9997 11.9635 21.9997C10.6049 21.9997 9.31155 21.7367 8.0934 21.2067C6.89058 20.6831 5.84501 19.9687 4.94363 19.0666C4.04281 18.1651 3.31836 17.107 2.79369 15.8978C1.72761 13.4409 1.72662 10.5261 2.81247 8.07034C3.88024 5.65548 5.84206 3.78161 8.26309 2.77709ZM15.2207 4.51639C13.2556 3.78239 10.9651 3.82132 9.02956 4.62439C7.06888 5.43791 5.49559 6.94785 4.64163 8.87914C3.78373 10.8194 3.78253 13.1522 4.62841 15.1017C5.05312 16.0805 5.63511 16.9291 6.35838 17.6529C7.08102 18.3761 7.91671 18.9484 8.89123 19.3728C9.8492 19.7895 10.87 19.9997 11.9635 19.9997C12.2815 19.9997 12.394 19.9225 12.431 19.8899L12.4319 19.8891C12.4367 19.8849 12.4487 19.8743 12.4631 19.8359C12.4799 19.7911 12.5016 19.7024 12.5016 19.5497C12.5016 19.4091 12.4633 19.3034 12.3677 19.192C11.5353 18.222 11.5272 16.6868 12.4611 15.7331C13.0741 15.1071 13.8844 14.98 14.4745 14.9556C14.7819 14.943 15.085 14.9568 15.3409 14.9709C15.3906 14.9736 15.4379 14.9763 15.4832 14.9788C15.6876 14.9904 15.8508 14.9997 16.0009 14.9997C17.3405 14.9986 18.2792 14.6054 18.9435 13.9133C20.2633 12.5382 20.3186 10.055 19.3853 8.1755C18.5436 6.48051 17.0293 5.19281 15.2207 4.51639Z\" fill=\"currentColor\"/>\n\n </symbol>\n</svg>\n`;\n\nexport default ICON_SPRITE;\n", "/* eslint-disable no-console */\nimport { getImageDimensionsFromURL, getImageUri } from '@imgly/plugin-utils';\nimport {\n type Output,\n type OutputKind,\n type GetBlockInputResult,\n VideoOutput,\n ImageOutput,\n TextOutput,\n AudioOutput,\n GenerationOptions,\n GenerationResult\n} from '../core/provider';\nimport { Middleware } from './middleware';\n\ninterface DryRunOptions<K extends OutputKind> {\n enable?: boolean;\n kind: K;\n\n // Is only defined with generation from a panel where we create a complete new block\n blockInputs?: GetBlockInputResult<K>;\n\n // Is only defined with quick action generation on a given block(s).\n blockIds?: number[];\n}\n\nfunction dryRunMiddleware<I, K extends OutputKind, O extends Output>(\n options: DryRunOptions<K>\n) {\n const middleware: Middleware<I, O> = async (\n generationInput,\n generationOptions,\n next\n ) => {\n if (!options.enable) {\n return next(generationInput, generationOptions);\n }\n console.log(\n `[DRY RUN]: Requesting dummy AI generation for kind '${options.kind}' with inputs: `,\n JSON.stringify(generationInput, undefined, 2)\n );\n await wait(2000);\n const output = await getDryRunOutput(\n generationInput,\n options,\n generationOptions\n );\n return output as O;\n };\n\n return middleware;\n}\n\nasync function getDryRunOutput<K extends OutputKind, I>(\n generationInput: I,\n options: DryRunOptions<K>,\n generationOptions: GenerationOptions\n): Promise<GenerationResult<Output>> {\n switch (options.kind) {\n case 'image': {\n return getImageDryRunOutput(\n generationInput,\n options as DryRunOptions<'image'>,\n generationOptions\n );\n }\n case 'video': {\n return getVideoDryRunOutput(\n generationInput,\n options as DryRunOptions<'video'>,\n generationOptions\n );\n }\n case 'text': {\n return getTextDryRunOutput(\n generationInput,\n options as DryRunOptions<'text'>,\n generationOptions\n );\n }\n case 'audio': {\n return getAudioDryRunOutput(\n generationInput,\n options as DryRunOptions<'audio'>,\n generationOptions\n );\n }\n\n default: {\n throw new Error(\n `Unsupported output kind for creating dry run output: ${options.kind}`\n );\n }\n }\n}\n\nasync function getImageDryRunOutput<I>(\n generationInput: I,\n options: DryRunOptions<'image'>,\n { engine }: GenerationOptions\n): Promise<ImageOutput> {\n let width;\n let height;\n\n const prompt: string =\n generationInput != null &&\n typeof generationInput === 'object' &&\n 'prompt' in generationInput &&\n typeof generationInput.prompt === 'string'\n ? generationInput.prompt\n : 'AI Generated Image';\n\n // If prompt includes something that looks like a dimension\n // e.g. 512x512, 1024x768, etc. than we will use this as the\n // output image size for testing purposes.\n const promptDimension = prompt.match(/(\\d+)x(\\d+)/);\n if (promptDimension != null) {\n width = parseInt(promptDimension[1], 10);\n height = parseInt(promptDimension[2], 10);\n } else {\n if (options.blockInputs != null) {\n width = options.blockInputs.image.width;\n height = options.blockInputs.image.height;\n }\n if (\n options.blockIds != null &&\n Array.isArray(options.blockIds) &&\n options.blockIds.length > 0\n ) {\n const [blockId] = options.blockIds;\n const url = await getImageUri(blockId, engine);\n const dimension = await getImageDimensionsFromURL(url, engine);\n width = dimension.width;\n height = dimension.height;\n } else {\n width = 512;\n height = 512;\n }\n }\n\n const url = `https://placehold.co/${width}x${height}/000000/FFF?text=${prompt\n .replace(' ', '+')\n .replace('\\n', '+')}`;\n\n return {\n kind: 'image',\n url\n };\n}\n\nasync function getVideoDryRunOutput<I>(\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n _generationInput: I,\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n _options: DryRunOptions<'video'>,\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n _generationOptions: GenerationOptions\n): Promise<VideoOutput> {\n return Promise.resolve({\n kind: 'video',\n url: 'https://storage.googleapis.com/gtv-videos-bucket/sample/ForBiggerBlazes.mp4'\n });\n}\n\nasync function getTextDryRunOutput<I>(\n generationInput: I,\n options: DryRunOptions<'text'>,\n generationOptions: GenerationOptions\n): Promise<AsyncGenerator<TextOutput>> {\n // Extract original text from the blocks or prompt\n let originalText = '';\n\n // Try to get original text from blocks first\n if (options.blockIds && options.blockIds.length > 0) {\n const [blockId] = options.blockIds;\n if (generationOptions.engine.block.isValid(blockId)) {\n originalText = generationOptions.engine.block.getString(\n blockId,\n 'text/text'\n );\n }\n }\n\n // If no original text from blocks, try to extract from prompt\n if (!originalText) {\n if (\n generationInput != null &&\n typeof generationInput === 'object' &&\n 'prompt' in generationInput &&\n typeof generationInput.prompt === 'string'\n ) {\n // Try to extract original text from prompt patterns\n const promptStr = generationInput.prompt;\n const textMatch =\n promptStr.match(/text:\\s*\"([^\"]+)\"/i) ||\n promptStr.match(/content:\\s*\"([^\"]+)\"/i) ||\n promptStr.match(/\"([^\"]+)\"/);\n if (textMatch && textMatch[1]) {\n originalText = textMatch[1];\n }\n }\n }\n\n // Generate dummy text with similar length\n const targetLength = originalText.length || 50; // Default to 50 chars if no original text\n let dryRunText = '';\n\n // Analyze input for specific text generation types and create appropriate dummy text\n if (generationInput != null && typeof generationInput === 'object') {\n if (\n 'language' in generationInput &&\n typeof generationInput.language === 'string'\n ) {\n dryRunText = generateDummyText(targetLength, 'translation');\n } else if (\n 'type' in generationInput &&\n typeof generationInput.type === 'string'\n ) {\n const tone = generationInput.type;\n dryRunText = generateDummyText(targetLength, tone);\n } else if ('customPrompt' in generationInput) {\n dryRunText = generateDummyText(targetLength, 'custom');\n } else {\n dryRunText = generateDummyText(targetLength, 'improved');\n }\n } else {\n dryRunText = generateDummyText(targetLength, 'generated');\n }\n\n // Return an async generator that streams the text in chunks\n return createStreamingTextGenerator(\n dryRunText,\n generationOptions.abortSignal\n );\n}\n\nasync function* createStreamingTextGenerator(\n finalText: string,\n abortSignal?: AbortSignal\n): AsyncGenerator<TextOutput> {\n const chunkSize = Math.max(1, Math.ceil(finalText.length / 20)); // Split into ~20 chunks\n\n // Generate and yield text progressively\n let currentLength = 0;\n\n while (currentLength < finalText.length) {\n if (abortSignal?.aborted) {\n return;\n }\n\n // Calculate next chunk end\n const nextLength = Math.min(currentLength + chunkSize, finalText.length);\n const currentText = finalText.substring(0, nextLength);\n\n yield {\n kind: 'text',\n text: currentText\n };\n\n currentLength = nextLength;\n\n // Only add delay if there are more chunks to come\n if (currentLength < finalText.length) {\n // eslint-disable-next-line no-await-in-loop\n await wait(100);\n }\n }\n\n // Return the final complete text\n return {\n kind: 'text',\n text: finalText\n };\n}\n\nfunction generateDummyText(targetLength: number, style: string): string {\n const prefix = '[DRY RUN - Dummy Text] ';\n const prefixLength = prefix.length;\n\n // If target length is shorter than the prefix, just return truncated prefix\n if (targetLength <= prefixLength) {\n return prefix.substring(0, targetLength);\n }\n\n const remainingLength = targetLength - prefixLength;\n\n const baseTexts = {\n translation:\n 'Ceci est un texte fictif traduit qui maintient la longueur approximative.',\n professional:\n 'Enhanced professional content with improved clarity and structure.',\n casual: 'Relaxed, friendly text that keeps things simple and approachable.',\n formal: 'Refined formal documentation that preserves original structure.',\n humorous: 'Amusing content that brings lighthearted fun to the text.',\n improved: 'Enhanced text that demonstrates better clarity and readability.',\n custom: 'Customized content reflecting the requested modifications.',\n generated: 'AI-generated content maintaining original length and structure.'\n };\n\n const baseText =\n baseTexts[style as keyof typeof baseTexts] || baseTexts.generated;\n\n let contentText = '';\n if (remainingLength <= baseText.length) {\n contentText = baseText.substring(0, remainingLength);\n } else {\n // For longer text, repeat and vary the content\n contentText = baseText;\n const variations = [\n ' Additional content continues with similar phrasing.',\n ' Further elaboration maintains the established tone.',\n ' Extended content preserves the original style.',\n ' Continued text follows the same pattern.'\n ];\n\n let variationIndex = 0;\n while (contentText.length < remainingLength) {\n const nextVariation = variations[variationIndex % variations.length];\n if (contentText.length + nextVariation.length <= remainingLength) {\n contentText += nextVariation;\n } else {\n contentText += nextVariation.substring(\n 0,\n remainingLength - contentText.length\n );\n break;\n }\n variationIndex++;\n }\n }\n\n return prefix + contentText;\n}\n\nasync function getAudioDryRunOutput<I>(\n generationInput: I,\n options: DryRunOptions<'audio'>,\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n generationOptions: GenerationOptions\n): Promise<AudioOutput> {\n // Extract duration from generation input or use default\n let duration = 3; // Default to 3 seconds\n\n // Try to extract duration from input\n if (generationInput != null && typeof generationInput === 'object') {\n if (\n 'duration' in generationInput &&\n typeof generationInput.duration === 'number'\n ) {\n duration = generationInput.duration;\n } else if (\n 'prompt' in generationInput &&\n typeof generationInput.prompt === 'string'\n ) {\n // Try to extract duration from prompt (e.g., \"5 seconds\", \"10s\", etc.)\n const promptStr = generationInput.prompt;\n const durationMatch = promptStr.match(/(\\d+)\\s*(?:seconds?|secs?|s)\\b/i);\n if (durationMatch) {\n duration = parseInt(durationMatch[1], 10);\n }\n }\n }\n\n // Try to get duration from block inputs\n if (options.blockInputs?.audio?.duration) {\n duration = options.blockInputs.audio.duration;\n }\n\n // Generate tone audio data\n const audioUrl = generateTone(220, duration); // 220Hz tone (A3 - deeper, more pleasant)\n\n return {\n kind: 'audio',\n url: audioUrl,\n duration,\n thumbnailUrl: undefined\n };\n}\n\nfunction generateTone(frequency: number, duration: number): string {\n const sampleRate = 44100;\n const numSamples = Math.floor(sampleRate * duration);\n\n // Generate sine wave PCM data\n const pcmData = new Float32Array(numSamples);\n for (let i = 0; i < numSamples; i++) {\n pcmData[i] = Math.sin((2 * Math.PI * frequency * i) / sampleRate);\n }\n\n return createWAVDataURI(pcmData, sampleRate);\n}\n\nfunction createWAVDataURI(pcmData: Float32Array, sampleRate: number): string {\n const numSamples = pcmData.length;\n const bytesPerSample = 2; // 16-bit\n const numChannels = 1; // Mono\n const blockAlign = numChannels * bytesPerSample;\n const byteRate = sampleRate * blockAlign;\n const dataSize = numSamples * bytesPerSample;\n const fileSize = 44 + dataSize; // 44 bytes for WAV header\n\n // Create WAV file buffer\n const buffer = new ArrayBuffer(fileSize);\n const view = new DataView(buffer);\n\n // WAV header\n const writeString = (offset: number, str: string) => {\n for (let i = 0; i < str.length; i++) {\n view.setUint8(offset + i, str.charCodeAt(i));\n }\n };\n\n writeString(0, 'RIFF');\n view.setUint32(4, fileSize - 8, true);\n writeString(8, 'WAVE');\n writeString(12, 'fmt ');\n view.setUint32(16, 16, true); // PCM chunk size\n view.setUint16(20, 1, true); // PCM format\n view.setUint16(22, numChannels, true);\n view.setUint32(24, sampleRate, true);\n view.setUint32(28, byteRate, true);\n view.setUint16(32, blockAlign, true);\n view.setUint16(34, 16, true); // Bits per sample\n writeString(36, 'data');\n view.setUint32(40, dataSize, true);\n\n // Convert float32 PCM to int16 and write to buffer\n let offset = 44;\n for (let i = 0; i < numSamples; i++) {\n const sample = Math.max(-1, Math.min(1, pcmData[i]));\n const intSample = Math.round(sample * 32767);\n view.setInt16(offset, intSample, true);\n offset += 2;\n }\n\n // Convert to base64\n const uint8Array = new Uint8Array(buffer);\n let binaryString = '';\n for (let i = 0; i < uint8Array.length; i++) {\n binaryString += String.fromCharCode(uint8Array[i]);\n }\n const base64 = btoa(binaryString);\n\n return `data:audio/wav;base64,${base64}`;\n}\n\nasync function wait(ms: number) {\n return new Promise((resolve) => {\n setTimeout(resolve, ms);\n });\n}\n\nexport default dryRunMiddleware;\n", "export const ABORT_REASON_USER_CANCEL = 'USER_CANCEL';\n", "import Provider, {\n GenerationOptions,\n Output,\n OutputKind\n} from '../core/provider';\nimport { composeMiddlewares, Middleware } from '../middleware/middleware';\nimport loggingMiddleware from '../middleware/loggingMiddleware';\nimport dryRunMiddleware from '../middleware/dryRunMiddleware';\nimport CreativeEditorSDK, { CreativeEngine } from '@cesdk/cesdk-js';\nimport { isAbortError, isAsyncGenerator } from '../utils/utils';\nimport { ABORT_REASON_USER_CANCEL } from '../core/constants';\n\nexport type ResultSuccess<O> =\n | {\n status: 'success';\n type: 'async';\n output: AsyncGenerator<O>;\n middlewareOptions?: GenerationOptions;\n }\n | {\n status: 'success';\n type: 'sync';\n output: O;\n middlewareOptions?: GenerationOptions;\n };\n\nexport type Result<O> =\n | ResultSuccess<O>\n | { status: 'error'; message: string; middlewareOptions?: GenerationOptions }\n | { status: 'aborted'; middlewareOptions?: GenerationOptions };\n\nexport type Generate<I, O extends Output> = (\n input: I,\n options?: {\n /**\n * The block IDs that this generation is operating on.\n * - undefined: Middleware will fall back to selected blocks\n * - []: Explicitly target no blocks\n * - [1, 2, 3]: Target specific blocks (e.g., placeholder block)\n */\n blockIds?: number[];\n abortSignal?: AbortSignal;\n middlewares?: Middleware<I, O>[];\n debug?: boolean;\n dryRun?: boolean;\n }\n) => Promise<Result<O>>;\n\nfunction createGenerateFunction<\n K extends OutputKind,\n I,\n O extends Output\n>(context: {\n provider: Provider<K, I, O>;\n cesdk: CreativeEditorSDK;\n engine: CreativeEngine;\n}): Generate<I, O> {\n return async (input: I, options) => {\n if (options?.abortSignal?.aborted) return { status: 'aborted' };\n\n const composedMiddlewares = composeMiddlewares<I, O>([\n ...(context.provider.output.middleware ?? []),\n ...(options?.middlewares ?? []),\n loggingMiddleware({ enable: options?.debug }),\n dryRunMiddleware({\n enable: options?.dryRun,\n kind: context.provider.kind\n })\n ]);\n\n // Create middleware options with preventDefault implementation\n // Using closure instead of 'this' to ensure state is shared across middleware chain\n const preventDefaultState = { prevented: false };\n const middlewareOptions: GenerationOptions = {\n blockIds: options?.blockIds,\n abortSignal: options?.abortSignal,\n engine: context.engine,\n cesdk: context.cesdk,\n preventDefault: () => {\n preventDefaultState.prevented = true;\n },\n defaultPrevented: () => {\n return preventDefaultState.prevented;\n }\n };\n\n // Trigger the generation\n try {\n const { result: output } = await composedMiddlewares(\n context.provider.output.generate\n )(input, middlewareOptions);\n if (options?.abortSignal?.aborted)\n return { status: 'aborted', middlewareOptions };\n if (output instanceof Error)\n return {\n status: 'error',\n message: output.message,\n middlewareOptions\n };\n if (output == null)\n return {\n status: 'error',\n message: 'No output generated',\n middlewareOptions\n };\n\n if (isAsyncGenerator(output)) {\n return { status: 'success', type: 'async', output, middlewareOptions };\n } else {\n return { status: 'success', type: 'sync', output, middlewareOptions };\n }\n } catch (error) {\n if (isAbortError(error)) {\n return {\n status: 'aborted',\n message: error.message,\n middlewareOptions\n };\n }\n if (error === ABORT_REASON_USER_CANCEL) {\n return { status: 'aborted', message: error, middlewareOptions };\n }\n return {\n status: 'error',\n message: error instanceof Error ? error.message : String(error),\n middlewareOptions\n };\n }\n };\n}\n\nexport default createGenerateFunction;\n", "import CreativeEditorSDK, { BuilderRenderFunction } from '@cesdk/cesdk-js';\nimport Provider, { Output, OutputKind } from '../core/provider';\nimport createPanelRenderFunction from '../ui/panels/createPanelRenderFunction';\nimport {\n CommonPluginConfiguration,\n InternalPluginConfiguration,\n InitializationContext\n} from '../types';\nimport initializeHistoryAssetSource from '../assets/initializeHistoryAssetSource';\nimport initializeHistoryAssetLibraryEntry from '../assets/initializeHistoryAssetLibraryEntry';\nimport icons from '../ui/icons';\nimport createGenerateFunction, {\n type Generate\n} from '../generation/createGenerateFunction';\nimport { ProviderRegistry } from '../core/ProviderRegistry';\nimport { ActionRegistry } from '../core/ActionRegistry';\nimport { addIconSetOnce } from '../utils/utils';\n\nexport type ProviderInitializationResult<\n K extends OutputKind,\n I,\n O extends Output\n> = {\n provider: Provider<K, I, O>;\n\n panel: {\n builderRenderFunction?: BuilderRenderFunction;\n };\n\n history: {\n assetSourceId?: string;\n assetLibraryEntryId?: string;\n };\n\n generate: Generate<I, O>;\n};\n\n/**\n * Initializes a provider with the given configuration and options.\n */\nasync function initializeProvider<K extends OutputKind, I, O extends Output>(\n _kind: K,\n provider: Provider<K, I, O>,\n options: {\n cesdk: CreativeEditorSDK;\n },\n config: CommonPluginConfiguration<K, I, O>\n): Promise<ProviderInitializationResult<K, I, O>> {\n // Create internal config with provider\n const internalConfig: InternalPluginConfiguration<K, I, O> = {\n ...config,\n provider\n };\n\n const context: InitializationContext<K, I, O> = {\n provider,\n panelInput: provider.input?.panel,\n options: {\n cesdk: options.cesdk,\n engine: options.cesdk.engine\n },\n config: internalConfig,\n providerConfig: provider.configuration\n };\n\n await provider.initialize?.({ ...options, engine: options.cesdk.engine });\n\n // Enable features for custom quick actions defined by this provider\n // Only enable features for quick actions that are not already known/registered by plugins\n if (provider.input?.quickActions?.supported) {\n const kind = provider.kind;\n const actionRegistry = ActionRegistry.get();\n\n Object.keys(provider.input.quickActions.supported).forEach(\n (quickActionId) => {\n // Check if this is a custom quick action (not already registered by a plugin)\n const existingAction = actionRegistry.getBy({\n id: quickActionId,\n type: 'quick'\n })[0];\n if (!existingAction) {\n // This is a custom quick action from the provider, enable its feature\n const featureId = `ly.img.plugin-ai-${kind}-generation-web.quickAction.${quickActionId}`;\n options.cesdk.feature.enable(featureId, true);\n }\n }\n );\n }\n\n const historyAssetSourceId = initializeHistoryAssetSource(context);\n const historyAssetLibraryEntryId = initializeHistoryAssetLibraryEntry(\n context,\n historyAssetSourceId\n );\n\n context.options.historyAssetSourceId = historyAssetSourceId;\n context.options.historyAssetLibraryEntryId = historyAssetLibraryEntryId;\n\n const generate = createGenerateFunction({\n provider,\n cesdk: options.cesdk,\n engine: options.cesdk.engine\n });\n\n const builderRenderFunction: BuilderRenderFunction | undefined =\n await createPanelRenderFunction(context, generate);\n\n addIconSetOnce(options.cesdk, '@imgly/plugin-ai-generation', icons);\n\n const providerInitializationResult: ProviderInitializationResult<K, I, O> = {\n provider,\n panel: {\n builderRenderFunction\n },\n history: {\n assetSourceId: historyAssetSourceId,\n assetLibraryEntryId: historyAssetLibraryEntryId\n },\n generate\n };\n\n ProviderRegistry.get().register(providerInitializationResult);\n\n return providerInitializationResult;\n}\n\nexport default initializeProvider;\n", "import { AggregatedAssetSource } from '@imgly/plugin-utils';\nimport { OutputKind } from '../core/provider';\nimport CreativeEditorSDK from '@cesdk/cesdk-js';\n\n/**\n * Initializes the combined history asset source for the given asset sources.\n */\nfunction initializeHistoryCompositeAssetSource<K extends OutputKind>(options: {\n kind: K;\n cesdk: CreativeEditorSDK;\n historAssetSourceIds: string[];\n}): string | undefined {\n const { kind, cesdk, historAssetSourceIds } = options;\n const compositeAssetSourceId = `ly.img.ai.${kind}-generation.history`;\n\n if (cesdk.engine.asset.findAllSources().includes(compositeAssetSourceId)) {\n return compositeAssetSourceId;\n }\n\n const aggregatedImageAssetSource = new AggregatedAssetSource(\n compositeAssetSourceId,\n cesdk,\n historAssetSourceIds\n );\n cesdk.engine.asset.addSource(aggregatedImageAssetSource);\n\n return aggregatedImageAssetSource.id;\n}\n\nexport default initializeHistoryCompositeAssetSource;\n", "import CreativeEditorSDK, {\n BuilderRenderFunction,\n BuilderRenderFunctionContext,\n SelectValue\n} from '@cesdk/cesdk-js';\nimport Provider, { Output, OutputKind } from '../core/provider';\nimport initializeProvider, {\n ProviderInitializationResult\n} from './initializeProvider';\nimport { isGeneratingStateKey } from '../ui/components/renderGenerationComponents';\nimport { CommonPluginConfiguration } from '../types';\nimport initializeHistoryCompositeAssetSource from '../assets/initializeHistoryCompositeAssetSource';\nimport { isDefined } from '@imgly/plugin-utils';\n\nfunction createLabelArray<K extends OutputKind>(\n kind: K,\n key: string\n): string[] {\n return [\n `ly.img.plugin-ai-${kind}-generation-web.${key}`,\n `ly.img.plugin-ai-generation-web.${key}`,\n `ly.img.plugin-ai-generation-web.defaults.${key}`\n ];\n}\n\nexport type ProvidersInitializationResult<\n K extends OutputKind,\n I,\n O extends Output\n> = {\n /**\n * Combined panel render function for all providers.\n */\n panel: {\n builderRenderFunction?: BuilderRenderFunction;\n };\n\n /**\n * Combined history asset source and library entry IDs for the providers.\n */\n history?: {\n assetSourceId?: string;\n assetLibraryEntryId?: string;\n };\n\n /**\n * All individual initialization results of the providers, i.e.\n * the result of `initializeProvider` for every provider.\n */\n providerInitializationResults: ProviderInitializationResult<K, I, O>[];\n};\n\n/**\n * Initializes the given providers for the specified output kind.\n *\n * - It will create a combined render function for all providers\n * that can be used in a panel\n *\n */\nasync function initializeProviders<K extends OutputKind, I, O extends Output>(\n kind: K,\n providers:\n | {\n fromText: Provider<K, I, O>[];\n fromImage: Provider<K, I, O>[];\n }\n | Provider<K, I, O>[],\n options: {\n cesdk: CreativeEditorSDK;\n },\n config: CommonPluginConfiguration<K, I, O>\n): Promise<ProvidersInitializationResult<K, I, O>> {\n // Set default translations\n const { cesdk } = options;\n cesdk.setTranslations({\n en: {\n 'ly.img.plugin-ai-generation-web.defaults.fromType.label': 'Input',\n 'ly.img.plugin-ai-generation-web.defaults.providerSelect.label':\n 'Provider',\n 'ly.img.plugin-ai-generation-web.defaults.fromText.label': 'Text',\n 'ly.img.plugin-ai-generation-web.defaults.fromImage.label': 'Image'\n }\n });\n\n let builderRenderFunction: BuilderRenderFunction | undefined;\n\n const providerResults: ProviderInitializationResult<K, I, O>[] = [];\n if (!Array.isArray(providers)) {\n const initializedFromTextProviders = await Promise.all(\n providers.fromText.map((provider) => {\n return initializeProvider(kind, provider, options, config);\n })\n );\n providerResults.push(...initializedFromTextProviders);\n\n const initializedFromImageProviders = await Promise.all(\n providers.fromImage.map((provider) => {\n return initializeProvider(kind, provider, options, config);\n })\n );\n providerResults.push(...initializedFromImageProviders);\n\n builderRenderFunction = getBuilderRenderFunctionByFromType({\n kind,\n prefix: `ly.img.ai.${kind}-generation`,\n initializedFromTextProviders,\n initializedFromImageProviders,\n cesdk\n });\n } else {\n const results = await Promise.all(\n providers.map((provider) => {\n return initializeProvider(kind, provider, options, config);\n })\n );\n providerResults.push(...results);\n\n builderRenderFunction = getBuilderRenderFunctionByProvider({\n kind,\n prefix: kind,\n providerInitializationResults: providerResults,\n cesdk\n });\n }\n\n const compositeHistoryAssetSourceId = initializeHistoryCompositeAssetSource({\n kind,\n cesdk: options.cesdk,\n historAssetSourceIds: providerResults\n .map((result) => result.history?.assetSourceId)\n .filter(isDefined)\n });\n\n let compositeHistoryAssetLibraryEntryId: string | undefined;\n\n if (compositeHistoryAssetSourceId != null) {\n compositeHistoryAssetLibraryEntryId = compositeHistoryAssetSourceId;\n options.cesdk.ui.addAssetLibraryEntry({\n id: compositeHistoryAssetLibraryEntryId,\n sourceIds: [compositeHistoryAssetSourceId],\n sortBy: {\n sortKey: 'insertedAt',\n sortingOrder: 'Descending'\n },\n canRemove: true,\n gridItemHeight: 'square',\n gridBackgroundType: 'cover'\n });\n }\n\n return {\n panel: {\n builderRenderFunction\n },\n history: {\n assetSourceId: compositeHistoryAssetSourceId,\n assetLibraryEntryId: compositeHistoryAssetLibraryEntryId\n },\n providerInitializationResults: providerResults\n };\n}\n\n/**\n * Combines the render functions of the initialized providers into a single\n * render function that can be used in a panel. Will add select components\n * to switch between the different providers and input types.\n */\nfunction getBuilderRenderFunctionByFromType<\n K extends OutputKind,\n I,\n O extends Output\n>({\n kind,\n prefix,\n initializedFromTextProviders,\n initializedFromImageProviders,\n cesdk\n}: {\n kind: K;\n prefix: string;\n initializedFromTextProviders: ProviderInitializationResult<K, I, O>[];\n initializedFromImageProviders: ProviderInitializationResult<K, I, O>[];\n cesdk: CreativeEditorSDK;\n}): BuilderRenderFunction<{}> {\n const includeFromSwitch =\n initializedFromTextProviders.length > 0 &&\n initializedFromImageProviders.length > 0;\n\n const builderRenderFunction: BuilderRenderFunction = (context) => {\n const { builder, experimental, engine } = context;\n\n // Check if text and image input are enabled via Feature API\n const textInputFeatureId = `ly.img.plugin-ai-${kind}-generation-web.fromText`;\n const isTextInputEnabled = cesdk.feature.isEnabled(textInputFeatureId, {\n engine\n });\n const imageInputFeatureId = `ly.img.plugin-ai-${kind}-generation-web.fromImage`;\n const isImageInputEnabled = cesdk.feature.isEnabled(imageInputFeatureId, {\n engine\n });\n\n // Determine default input type based on what's enabled\n let defaultInputType: 'fromText' | 'fromImage' | undefined;\n if (\n isTextInputEnabled &&\n initializedFromTextProviders.length > 0 &&\n (!isImageInputEnabled || initializedFromImageProviders.length === 0)\n ) {\n defaultInputType = 'fromText';\n } else if (\n isImageInputEnabled &&\n initializedFromImageProviders.length > 0 &&\n (!isTextInputEnabled || initializedFromTextProviders.length === 0)\n ) {\n defaultInputType = 'fromImage';\n } else if (isTextInputEnabled && isImageInputEnabled && includeFromSwitch) {\n defaultInputType = 'fromText';\n }\n\n const inputTypeState = experimental.global<\n 'fromText' | 'fromImage' | undefined\n >(`${prefix}.fromType`, defaultInputType);\n\n const providerInitializationResults: ProviderInitializationResult<\n K,\n I,\n O\n >[] = [];\n if (inputTypeState.value === 'fromText') {\n providerInitializationResults.push(...initializedFromTextProviders);\n } else if (inputTypeState.value === 'fromImage') {\n providerInitializationResults.push(...initializedFromImageProviders);\n } else {\n providerInitializationResults.push(\n ...initializedFromTextProviders,\n ...initializedFromImageProviders\n );\n }\n\n const providerValuesFromText: (SelectValue & {\n builderRenderFunction?: BuilderRenderFunction;\n })[] = initializedFromTextProviders.map(({ provider, panel }) => ({\n id: provider.id,\n label: provider.name ?? provider.id,\n builderRenderFunction: panel?.builderRenderFunction\n }));\n\n const providerValuesFromImage: (SelectValue & {\n builderRenderFunction?: BuilderRenderFunction;\n })[] = initializedFromImageProviders.map(({ provider, panel }) => ({\n id: provider.id,\n label: provider.name ?? provider.id,\n builderRenderFunction: panel?.builderRenderFunction\n }));\n\n const providerStateFromText = context.experimental.global(\n `${prefix}.selectedProvider.fromText`,\n providerValuesFromText[0]\n );\n const providerStateFromImage = context.experimental.global(\n `${prefix}.selectedProvider.fromImage`,\n providerValuesFromImage[0]\n );\n\n const providerState =\n inputTypeState.value === 'fromText'\n ? providerStateFromText\n : inputTypeState.value === 'fromImage'\n ? providerStateFromImage\n : undefined;\n\n // Check if provider selector is enabled via Feature API\n const providerFeatureId = `ly.img.plugin-ai-${kind}-generation-web.providerSelect`;\n const isProviderSelectorEnabled = cesdk.feature.isEnabled(\n providerFeatureId,\n { engine }\n );\n\n // Check if neither text nor image input is enabled\n if (!isTextInputEnabled && !isImageInputEnabled) {\n builder.Section(`${prefix}.noInputWarning.section`, {\n children: () => {\n builder.Text(`${prefix}.noInputWarning.text`, {\n content:\n 'No input types are enabled. Please enable at least one input type (text or image) via the Feature API.'\n });\n }\n });\n return; // Exit early, don't render anything else\n }\n\n // Determine if we need to show the input selector\n // Only show if both types are enabled AND both have providers\n const bothInputsEnabled =\n isTextInputEnabled &&\n isImageInputEnabled &&\n initializedFromTextProviders.length > 0 &&\n initializedFromImageProviders.length > 0;\n const shouldShowInputSelector = includeFromSwitch && bothInputsEnabled;\n const shouldShowProviderSelector =\n providerInitializationResults.length > 1 && isProviderSelectorEnabled;\n\n if (shouldShowInputSelector || shouldShowProviderSelector) {\n builder.Section(`${prefix}.providerSelection.section`, {\n children: () => {\n // RENDER FROM SELECTION - only if both input types are enabled\n if (shouldShowInputSelector) {\n builder.ButtonGroup(`${prefix}.fromType.buttonGroup`, {\n inputLabel: createLabelArray(kind, 'fromType.label'),\n children: () => {\n if (\n isTextInputEnabled &&\n initializedFromTextProviders.length > 0\n ) {\n builder.Button(`${prefix}.fromType.buttonGroup.fromText`, {\n label: createLabelArray(kind, 'fromText.label'),\n icon:\n inputTypeState.value !== 'fromText' &&\n isSomeProviderGenerating(\n initializedFromTextProviders,\n context\n )\n ? '@imgly/LoadingSpinner'\n : undefined,\n isActive: inputTypeState.value === 'fromText',\n onClick: () => {\n inputTypeState.setValue('fromText');\n }\n });\n }\n\n if (\n isImageInputEnabled &&\n initializedFromImageProviders.length > 0\n ) {\n builder.Button(`${prefix}.fromType.buttonGroup.fromImage`, {\n label: createLabelArray(kind, 'fromImage.label'),\n icon:\n inputTypeState.value !== 'fromImage' &&\n isSomeProviderGenerating(\n initializedFromImageProviders,\n context\n )\n ? '@imgly/LoadingSpinner'\n : undefined,\n isActive: inputTypeState.value === 'fromImage',\n onClick: () => {\n inputTypeState.setValue('fromImage');\n }\n });\n }\n }\n });\n }\n\n // RENDER PROVIDER SELECT\n if (shouldShowProviderSelector) {\n const providerValues =\n inputTypeState.value === 'fromText'\n ? providerValuesFromText\n : inputTypeState.value === 'fromImage'\n ? providerValuesFromImage\n : [...providerValuesFromText, ...providerValuesFromImage];\n\n if (providerState != null) {\n builder.Select(`${prefix}.providerSelect.select`, {\n inputLabel: createLabelArray(kind, 'providerSelect.label'),\n values: providerValues,\n ...providerState\n });\n }\n }\n }\n });\n }\n\n // Render the provider content\n if (providerInitializationResults.length > 1) {\n providerState?.value.builderRenderFunction?.(context);\n } else {\n const providerInitializationResult = providerInitializationResults[0];\n if (providerInitializationResult) {\n providerInitializationResult.panel?.builderRenderFunction?.(context);\n }\n }\n };\n\n return builderRenderFunction;\n}\n\n/**\n * Combines the render functions of the initialized providers into a single\n * render function that can be used in a panel. Will add select components\n * to switch between the different providers and input types.\n */\nfunction getBuilderRenderFunctionByProvider<\n K extends OutputKind,\n I,\n O extends Output\n>({\n kind,\n prefix,\n providerInitializationResults,\n cesdk\n}: {\n kind: K;\n prefix: string;\n providerInitializationResults: ProviderInitializationResult<K, I, O>[];\n cesdk: CreativeEditorSDK;\n}): BuilderRenderFunction<{}> {\n const builderRenderFunction: BuilderRenderFunction = (context) => {\n const { builder, engine } = context;\n if (providerInitializationResults.length === 0) return;\n\n // Check if provider selector is enabled via Feature API\n // Audio plugin has special cases for speech and sound providers\n let providerFeatureId = `ly.img.plugin-ai-${kind}-generation-web.providerSelect`;\n // For audio, we check prefix to determine if it's speech or sound\n if (kind === 'audio' && prefix) {\n if (prefix.includes('speech')) {\n providerFeatureId = `ly.img.plugin-ai-audio-generation-web.speech.providerSelect`;\n } else if (prefix.includes('sound')) {\n providerFeatureId = `ly.img.plugin-ai-audio-generation-web.sound.providerSelect`;\n }\n }\n const isProviderSelectorEnabled = cesdk.feature.isEnabled(\n providerFeatureId,\n { engine }\n );\n\n const providerValues: (SelectValue & {\n builderRenderFunction?: BuilderRenderFunction;\n })[] = providerInitializationResults.map(({ provider, panel }) => ({\n id: provider.id,\n label: provider.name ?? provider.id,\n builderRenderFunction: panel?.builderRenderFunction\n }));\n\n const providerState = context.state(\n `${prefix}.selectedProvider`,\n providerValues[0]\n );\n\n if (providerInitializationResults.length > 1 && isProviderSelectorEnabled) {\n if (providerState != null) {\n builder.Section(`${prefix}.providerSelection.section`, {\n children: () => {\n builder.Select(`${prefix}.providerSelect.select`, {\n inputLabel: createLabelArray(kind, 'providerSelect.label'),\n values: providerValues,\n ...providerState\n });\n }\n });\n }\n }\n\n // Render the provider content\n providerState.value.builderRenderFunction?.(context);\n };\n\n return builderRenderFunction;\n}\n\n/**\n * Queries the global state to check if any provider from the given\n * list is currently generating.\n */\nfunction isSomeProviderGenerating<K extends OutputKind, I, O extends Output>(\n providerInitializationResults: ProviderInitializationResult<K, I, O>[],\n context: BuilderRenderFunctionContext<any>\n): boolean {\n if (providerInitializationResults.length === 0) return false;\n return providerInitializationResults.some(({ provider }) => {\n if (provider.id == null) return false;\n return context.experimental.global(isGeneratingStateKey(provider.id), false)\n .value;\n });\n}\n\nexport default initializeProviders;\n", "export const AI_EDIT_MODE = 'ly.img.ai.editMode';\n\nexport const AI_METADATA_KEY = 'ly.img.ai.metadata';\n\nexport const AI_CONFIRMATION_COMPONENT_ID = 'ly.img.ai.confirmation.canvasMenu';\n\nexport function getFeatureIdForQuickAction(options: {\n quickActionId: string;\n quickActionMenuId: string;\n}) {\n return `ly.img.ai.quickAction.${options.quickActionMenuId}.${options.quickActionId}`;\n}\n", "import CreativeEditorSDK, { BuilderRenderFunction } from '@cesdk/cesdk-js';\nimport { InferenceMetadata } from '../quickActions/types';\nimport { Metadata } from '@imgly/plugin-utils';\nimport { AI_METADATA_KEY } from '../quickActions/utils';\nimport { OutputKind } from '../../core/provider';\nimport { Callbacks } from '../../generation/CallbacksRegistry';\n\n/**\n * Creates a render function for the AI inference confirmation component.\n *\n * It will render 'cancel', 'before', 'after', and 'apply' buttons once the\n * inference is done. Until then a loading spinner is shown with a cancel button.\n *\n * The callbacks are provided by the payload from the context.\n */\nasync function createConfirmationRenderFunction<K extends OutputKind>(context: {\n kind: K;\n\n cesdk: CreativeEditorSDK;\n}): Promise<BuilderRenderFunction<Callbacks>> {\n const prefix = `ly.img.ai.${context.kind}.confirmation`;\n context.cesdk?.i18n.setTranslations({\n en: {\n 'ly.img.ai.processing': 'Generating...',\n [`${prefix}.cancel`]: 'Cancel Generation',\n [`${prefix}.apply`]: 'Apply Generation',\n [`${prefix}.before`]: 'Before',\n [`${prefix}.after`]: 'After'\n }\n });\n const builderRenderFunction: BuilderRenderFunction<Callbacks> = (\n builderContext\n ) => {\n const { engine, builder, state, payload } = builderContext;\n if (payload == null) return;\n\n const blockIds = engine.block.findAllSelected();\n if (blockIds.length === 0) return null;\n\n const md = new Metadata<InferenceMetadata>(engine, AI_METADATA_KEY);\n\n // All blocks must have the same metadata\n const metadata = md.get(blockIds[0]);\n if (metadata == null) return null;\n\n const clearMetadata = () => {\n blockIds.forEach((blockId) => {\n md.clear(blockId);\n });\n };\n\n switch (metadata.status) {\n case 'processing': {\n builder.Button(`${prefix}.spinner`, {\n label: [\n `ly.img.ai.${metadata.quickActionId}.processing`,\n `ly.img.ai.processing`\n ],\n isLoading: true\n });\n builder.Separator(`${prefix}.separator`);\n builder.Button(`${prefix}.cancel`, {\n icon: '@imgly/Cross',\n tooltip: `${prefix}.cancel`,\n onClick: () => {\n payload.onCancelGeneration?.();\n clearMetadata();\n }\n });\n\n break;\n }\n\n case 'confirmation': {\n const comparingState = state<'before' | 'after'>(\n `${prefix}.comparing`,\n 'after'\n );\n\n const onCancel = payload.applyCallbacks?.onCancel;\n if (onCancel != null) {\n builder.Button(`${prefix}.cancel`, {\n icon: '@imgly/Cross',\n tooltip: `${prefix}.cancel`,\n onClick: () => {\n onCancel();\n clearMetadata();\n }\n });\n }\n\n const onBefore = payload.applyCallbacks?.onBefore;\n const onAfter = payload.applyCallbacks?.onAfter;\n\n if (onBefore != null && onAfter != null) {\n builder.ButtonGroup(`${prefix}.compare`, {\n children: () => {\n builder.Button(`${prefix}.compare.before`, {\n label: `${prefix}.before`,\n variant: 'regular',\n isActive: comparingState.value === 'before',\n onClick: () => {\n onBefore();\n comparingState.setValue('before');\n }\n });\n builder.Button(`${prefix}.compare.after`, {\n label: `${prefix}.after`,\n variant: 'regular',\n isActive: comparingState.value === 'after',\n onClick: () => {\n onAfter();\n comparingState.setValue('after');\n }\n });\n }\n });\n }\n\n const onApply = payload.applyCallbacks?.onApply;\n if (onApply != null) {\n builder.Button(`${prefix}.apply`, {\n icon: '@imgly/Checkmark',\n tooltip: `${prefix}.apply`,\n color: 'accent',\n isDisabled: comparingState.value !== 'after',\n onClick: () => {\n clearMetadata();\n\n const editor = context.cesdk.engine.editor as any;\n if (typeof editor._update === 'function') {\n // Activating the old history happens in the next update lop.\n editor._update();\n }\n\n onApply();\n }\n });\n }\n\n break;\n }\n\n default: {\n // noop\n }\n }\n };\n return Promise.resolve(builderRenderFunction);\n}\n\nexport default createConfirmationRenderFunction;\n", "/**\n * For the given array that contains strings `ly.img.separator` and other items,\n * this function will remove all leading and trailing separators,\n * as well as any consecutive separators in between.\n */\nfunction compactSeparators<T>(\n quickActions: (T | 'ly.img.separator')[]\n): (T | 'ly.img.separator')[] {\n if (quickActions.length === 0) {\n return [];\n }\n\n // Create a new array for the result\n const result = [...quickActions];\n\n // Remove separators at the beginning\n while (result.length > 0 && result[0] === 'ly.img.separator') {\n result.shift();\n }\n\n // Remove separators at the end\n while (\n result.length > 0 &&\n result[result.length - 1] === 'ly.img.separator'\n ) {\n result.pop();\n }\n\n // Remove consecutive separators\n return result.reduce<(T | 'ly.img.separator')[]>((acc, current) => {\n // Skip if current is a separator and previous was also a separator\n if (\n current === 'ly.img.separator' &&\n acc.length > 0 &&\n acc[acc.length - 1] === 'ly.img.separator'\n ) {\n return acc;\n }\n\n acc.push(current);\n return acc;\n }, []);\n}\n\nexport default compactSeparators;\n", "import { OutputKind } from '../core/provider';\n\nfunction getQuickActionCanvasMenuComponentId<K extends OutputKind>(\n kind: K\n): string {\n return `ly.img.ai.${kind}.canvasMenu`;\n}\n\nexport default getQuickActionCanvasMenuComponentId;\n", "import CreativeEditorSDK, { ComponentPayload } from '@cesdk/cesdk-js';\nimport getQuickActionCanvasMenuComponentId from '../../providers/getCanvasMenuComponentId';\nimport { OutputKind } from '../../core/provider';\n\n/**\n * Returns the current order of quick actions for a particular kind.\n */\nfunction getQuickActionOrder<K extends OutputKind>(context: {\n kind: K;\n cesdk: CreativeEditorSDK;\n payload?: ComponentPayload;\n defaultOrder?: string[];\n}): string[] {\n const { kind, cesdk, payload, defaultOrder } = context;\n const canvasMenuComponentId = getQuickActionCanvasMenuComponentId(kind);\n\n if (payload == null || !Array.isArray(payload.children)) {\n // Fallback to get the children order from the canvas menu order.\n // Happens e.g. for CE.SDK versions < 1.53.0 because the payload\n // is not passed correctly to the render function.\n const canvasMenuOrder = cesdk.ui.getCanvasMenuOrder();\n const component = canvasMenuOrder.find(({ id }) => {\n return id === canvasMenuComponentId;\n });\n\n if (component != null && Array.isArray(component.children)) {\n return component.children;\n } else {\n // Use provided default order or empty array for backwards compatibility\n return defaultOrder ?? [];\n }\n }\n return payload.children.filter(\n (child) => typeof child === 'string'\n ) as string[];\n}\n\nexport default getQuickActionOrder;\n", "import type CreativeEditorSDK from '@cesdk/cesdk-js';\nimport {\n getImageDimensionsFromURL,\n mimeTypeToExtension\n} from '@imgly/plugin-utils';\nimport {\n ImageOutput,\n TextOutput,\n type Output,\n type OutputKind\n} from '../core/provider';\nimport { ResultSuccess } from '../generation/createGenerateFunction';\n\nexport type ApplyCallbacks = {\n onBefore: () => void;\n onAfter: () => void;\n onCancel: () => void;\n onApply: () => void;\n};\n\ntype GetApplyCallbacksOptions = {\n kind: OutputKind;\n blockIds: number[];\n cesdk: CreativeEditorSDK;\n abortSignal?: AbortSignal;\n};\n\ntype ReturnValue = {\n applyCallbacks: ApplyCallbacks;\n};\n\n/**\n * This method is used in the quick action menu to handle the result of the generation\n * and providing methods for the comparision of the result.\n *\n * Different output kinds require different handling. E.g. the text generation\n * is streamed to the text block, while the image generation is applied to the fill\n * block with a source set and the same crop applied.\n */\nfunction getApplyCallbacks<O extends Output>(\n result: ResultSuccess<O>,\n options: GetApplyCallbacksOptions\n): Promise<ReturnValue> {\n if (options.blockIds.some((id) => !options.cesdk.engine.block.isValid(id))) {\n return Promise.resolve({\n applyCallbacks: {\n onBefore: () => {},\n onAfter: () => {},\n onCancel: () => {},\n onApply: () => {}\n }\n });\n }\n switch (options.kind) {\n case 'text':\n return getApplyCallbacksForText(result, options);\n case 'image':\n return getApplyCallbacksForImage(result, options);\n case 'video':\n return getApplyCallbacksForVideo(result, options);\n case 'audio':\n return getApplyCallbacksForAudio(result, options);\n default:\n throw new Error(\n `Unsupported output kind for quick actions: ${options.kind}`\n );\n }\n}\n\nasync function getApplyCallbacksForText<O extends Output>(\n result: ResultSuccess<O>,\n options: GetApplyCallbacksOptions\n): Promise<ReturnValue> {\n const { cesdk, blockIds, abortSignal } = options;\n\n const beforeTexts = blockIds.map((blockId) => {\n return cesdk.engine.block.getString(blockId, 'text/text');\n });\n\n let output: O | undefined;\n if (result.type === 'async') {\n let inferredText = '';\n for await (const chunk of result.output) {\n if (abortSignal?.aborted) {\n break;\n }\n if (typeof chunk === 'string') {\n inferredText = chunk;\n } else if (chunk.kind === 'text') {\n inferredText = chunk.text;\n }\n\n // eslint-disable-next-line @typescript-eslint/no-loop-func\n blockIds.forEach((blockId) => {\n cesdk.engine.block.setString(blockId, 'text/text', inferredText);\n });\n\n const textOutput: TextOutput = {\n kind: 'text',\n text: inferredText\n };\n output = textOutput as O;\n }\n } else {\n output = result.output;\n }\n\n if (output == null || output.kind !== 'text') {\n throw new Error('Output kind from generation is not text');\n }\n\n // For sync results, apply the text immediately (like streaming does)\n if (result.type === 'sync') {\n blockIds.forEach((blockId) => {\n cesdk.engine.block.setString(blockId, 'text/text', output.text);\n });\n }\n\n const onAfter = () => {\n options.blockIds.forEach((blockId) => {\n options.cesdk.engine.block.setString(blockId, 'text/text', output.text);\n });\n };\n const onBefore = () => {\n options.blockIds.forEach((blockId, i) => {\n options.cesdk.engine.block.setString(\n blockId,\n 'text/text',\n beforeTexts[i]\n );\n });\n };\n const onCancel = onBefore;\n const onApply = () => {\n onAfter();\n options.cesdk.engine.editor.addUndoStep();\n };\n\n return {\n applyCallbacks: {\n onBefore,\n onAfter,\n onCancel,\n onApply\n }\n };\n}\n\nasync function getApplyCallbacksForImage<O extends Output>(\n result: ResultSuccess<O>,\n options: GetApplyCallbacksOptions\n): Promise<ReturnValue> {\n const { cesdk, blockIds, abortSignal } = options;\n if (blockIds.length !== 1) {\n throw new Error('Only one block is supported for image generation');\n }\n\n const [block] = blockIds;\n const fillBlock = cesdk.engine.block.getFill(block);\n const sourceSetBefore = cesdk.engine.block.getSourceSet(\n fillBlock,\n 'fill/image/sourceSet'\n );\n const [sourceBefore] = sourceSetBefore;\n let uriBefore: string | undefined;\n if (sourceBefore == null) {\n uriBefore = cesdk.engine.block.getString(\n fillBlock,\n 'fill/image/imageFileURI'\n );\n }\n\n const mimeType = await cesdk.engine.editor.getMimeType(\n sourceBefore?.uri ?? uriBefore\n );\n const originalDimension = await getImageDimensionsFromURL(\n sourceBefore?.uri ?? uriBefore,\n options.cesdk.engine\n );\n const originalAspectRatio =\n originalDimension.width / originalDimension.height;\n abortSignal?.throwIfAborted();\n\n if (mimeType === 'image/svg+xml') {\n throw new Error('SVG images are not supported');\n }\n\n const cropScaleX = cesdk.engine.block.getCropScaleX(block);\n const cropScaleY = cesdk.engine.block.getCropScaleY(block);\n const cropTranslationX = cesdk.engine.block.getCropTranslationX(block);\n const cropTranslationY = cesdk.engine.block.getCropTranslationY(block);\n const cropRotation = cesdk.engine.block.getCropRotation(block);\n\n const applyCrop = () => {\n cesdk.engine.block.setCropScaleX(block, cropScaleX);\n cesdk.engine.block.setCropScaleY(block, cropScaleY);\n cesdk.engine.block.setCropTranslationX(block, cropTranslationX);\n cesdk.engine.block.setCropTranslationY(block, cropTranslationY);\n cesdk.engine.block.setCropRotation(block, cropRotation);\n };\n\n if (result.type === 'async') {\n throw new Error('Streaming generation is not supported yet from a panel');\n }\n\n if (result.output.kind !== 'image' || typeof result.output.url !== 'string') {\n throw new Error('Output kind from generation is not an image');\n }\n\n const url = (result.output as ImageOutput).url;\n const generatedMimeType = await cesdk.engine.editor.getMimeType(url);\n\n const uri = await reuploadImage(cesdk, url, generatedMimeType);\n const generatedDimension = await getImageDimensionsFromURL(\n uri,\n options.cesdk.engine\n );\n const generatedAspectRatio =\n generatedDimension.width / generatedDimension.height;\n\n const differentAspectRatio =\n Math.abs(originalAspectRatio - generatedAspectRatio) > 0.001;\n\n const sourceSetAfter = sourceBefore\n ? [\n {\n uri,\n width: generatedDimension.width,\n height: generatedDimension.height\n }\n ]\n : undefined;\n const uriAfter = uri;\n\n if (sourceSetAfter == null) {\n cesdk.engine.block.setString(\n fillBlock,\n 'fill/image/imageFileURI',\n uriAfter\n );\n } else {\n cesdk.engine.block.setString(fillBlock, 'fill/image/imageFileURI', '');\n cesdk.engine.block.setSourceSet(\n fillBlock,\n 'fill/image/sourceSet',\n sourceSetAfter\n );\n }\n\n await cesdk.engine.block.forceLoadResources([fillBlock]);\n\n if (differentAspectRatio) {\n cesdk.engine.block.setContentFillMode(block, 'Cover');\n } else {\n applyCrop();\n }\n\n const onBefore = () => {\n if (sourceSetBefore == null || sourceSetBefore.length === 0) {\n if (uriBefore == null) {\n throw new Error('No image URI found');\n }\n cesdk.engine.block.setString(\n fillBlock,\n 'fill/image/imageFileURI',\n uriBefore\n );\n } else {\n cesdk.engine.block.setSourceSet(\n fillBlock,\n 'fill/image/sourceSet',\n sourceSetBefore\n );\n }\n applyCrop();\n };\n const onAfter = () => {\n if (sourceSetAfter == null) {\n if (uriAfter == null) {\n throw new Error('No image URI found');\n }\n cesdk.engine.block.setString(\n fillBlock,\n 'fill/image/imageFileURI',\n uriAfter\n );\n } else {\n cesdk.engine.block.setSourceSet(\n fillBlock,\n 'fill/image/sourceSet',\n sourceSetAfter\n );\n }\n if (differentAspectRatio) {\n cesdk.engine.block.setContentFillMode(block, 'Cover');\n } else {\n applyCrop();\n }\n };\n const onCancel = () => {\n onBefore();\n };\n const onApply = () => {\n onAfter();\n options.cesdk.engine.editor.addUndoStep();\n };\n\n return {\n applyCallbacks: {\n onBefore,\n onAfter,\n onCancel,\n onApply\n }\n };\n}\n\nasync function getApplyCallbacksForVideo<O extends Output>(\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n _result: ResultSuccess<O>,\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n _options: GetApplyCallbacksOptions\n): Promise<ReturnValue> {\n throw new Error('Function not implemented.');\n}\n\nasync function getApplyCallbacksForAudio<O extends Output>(\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n _result: ResultSuccess<O>,\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n _options: GetApplyCallbacksOptions\n): Promise<ReturnValue> {\n throw new Error('Function not implemented.');\n}\n\nasync function reuploadImage(\n cesdk: CreativeEditorSDK,\n url: string,\n mimeType: string\n): Promise<string> {\n const response = await fetch(url);\n const blob = await response.blob();\n const file = new File([blob], `image.${mimeTypeToExtension(mimeType)}`, {\n type: mimeType\n });\n const assetDefinition = await cesdk.unstable_upload(file, () => {});\n const uploadedUri = assetDefinition?.meta?.uri;\n if (uploadedUri != null) return uploadedUri;\n // eslint-disable-next-line no-console\n console.warn('Failed to upload image:', assetDefinition);\n return url;\n}\n\nexport default getApplyCallbacks;\n", "import { CreativeEngine } from '@cesdk/cesdk-js';\n\n/**\n * Locks the selection to the given block ids with the given edit mode.\n *\n * @returns A function to unlock the selection. Will set the edit mode back to what it was before the lock.\n */\nfunction lockSelectionToEditMode(options: {\n engine: CreativeEngine;\n blockIdsToLock: number[];\n editModeToLockTo: string;\n}) {\n const { engine, blockIdsToLock, editModeToLockTo } = options;\n\n function isBoundBlockSelected() {\n const currentlySelectedBlockIds = engine.block.findAllSelected();\n return blockIdsToLock.some((blockId) => {\n return currentlySelectedBlockIds.includes(blockId);\n });\n }\n\n const stateChangeDisposer = engine.editor.onStateChanged(() => {\n const editMode = engine.editor.getEditMode();\n if (editMode !== editModeToLockTo && isBoundBlockSelected()) {\n engine.editor.setEditMode(editModeToLockTo);\n }\n });\n\n const selectionDisposer = engine.block.onSelectionChanged(() => {\n if (isBoundBlockSelected()) {\n engine.editor.setEditMode(editModeToLockTo);\n } else {\n engine.editor.setEditMode('Transform');\n }\n });\n\n if (isBoundBlockSelected()) {\n engine.editor.setEditMode(editModeToLockTo);\n }\n\n let disposed = false;\n const dispose = () => {\n if (disposed) return;\n selectionDisposer();\n stateChangeDisposer();\n\n engine.editor.setEditMode('Transform');\n disposed = true;\n };\n\n return dispose;\n}\n\nexport default lockSelectionToEditMode;\n", "import { ApplyCallbacks } from '../providers/getApplyCallbacks';\n\nexport type Callbacks = {\n /**\n * Callbacks for the confirmation process.\n */\n applyCallbacks?: ApplyCallbacks;\n\n /**\n * Callback when generation is cancelled.\n */\n onCancelGeneration?: () => void;\n};\n\n/**\n * Global registry for managing callbacks for quick action menus across different\n * components.\n */\nexport class CallbacksRegistry {\n /** Map storing all registered callbacks by their ID */\n private actions: Map<number, Callbacks> = new Map();\n\n // eslint-disable-next-line @typescript-eslint/no-empty-function\n private constructor() {}\n\n /**\n * Gets the singleton instance of the CallbacksRegistry.\n * Uses global object storage to ensure singleton across different bundle contexts.\n */\n public static get(): CallbacksRegistry {\n const globalKey = '__imgly_callbacks_registry__';\n const globalObj = (\n typeof window !== 'undefined' ? window : globalThis\n ) as any;\n\n if (!globalObj[globalKey]) {\n globalObj[globalKey] = new CallbacksRegistry();\n }\n return globalObj[globalKey];\n }\n\n /**\n * Registers apply callbacks in the registry.\n */\n public register(blockId: number, callbacks: Partial<Callbacks>) {\n const currentCallbacks = this.actions.get(blockId) ?? {};\n this.actions.set(blockId, {\n ...currentCallbacks,\n ...callbacks\n });\n }\n\n /**\n * Gets the callbacks for the given block id.\n */\n public get(blockId: number): Callbacks {\n return this.actions.get(blockId) ?? {};\n }\n}\n\nexport default CallbacksRegistry;\n", "import { isDefined, Metadata } from '@imgly/plugin-utils';\nimport { QuickActionDefinition } from '../core/ActionRegistry';\nimport { Result } from './createGenerateFunction';\nimport { ProviderInitializationResult } from '../providers/initializeProvider';\nimport { Middleware } from '../middleware/middleware';\nimport { Output, OutputKind } from '../core/provider';\nimport { AI_EDIT_MODE, AI_METADATA_KEY } from '../ui/quickActions/utils';\nimport { InferenceMetadata } from '../ui/quickActions/types';\nimport CreativeEditorSDK from '@cesdk/cesdk-js';\nimport getApplyCallbacks from '../providers/getApplyCallbacks';\nimport lockSelectionToEditMode from '../utils/lockSelectionToEditMode';\nimport CallbacksRegistry from './CallbacksRegistry';\nimport { ABORT_REASON_USER_CANCEL } from '../core/constants';\n\ntype GenerationOptions<\n Q extends Record<string, any>,\n K extends OutputKind,\n I,\n O extends Output\n> = {\n /**\n * The blocks that this quick action is running on.\n */\n blockIds: number[];\n\n /**\n * The initlaized provider that is used to generate the output.\n */\n providerInitializationResult?: ProviderInitializationResult<K, I, O>;\n\n /**\n * The quick action definition that is used to generate input for the provider\n */\n quickAction: QuickActionDefinition<Q>;\n\n /**\n * Shall the generation be confirmed before applying the result? Or directly applied?\n */\n confirmation?: boolean;\n\n /**\n * Should the blocks be locked to edit mode while the generation is running?\n */\n lock?: boolean;\n\n /**\n * Additional middlewares added to the generation process.\n */\n middlewares?: Middleware<I, O>[];\n\n /**\n * Print debug information to the console.\n */\n debug?: boolean;\n\n /**\n * Enable dry run mode for testing.\n */\n dryRun?: boolean;\n\n /**\n * Signal to check if process was aborted\n */\n abortSignal?: AbortSignal;\n\n /**\n * Close the quick action menu\n */\n close: () => void;\n\n cesdk: CreativeEditorSDK;\n};\n\n/**\n * Handler for generating output from a quick action.\n */\nfunction handleGenerateFromQuickAction<\n Q extends Record<string, any>,\n K extends OutputKind,\n I,\n O extends Output\n>(\n options: GenerationOptions<Q, K, I, O>\n): (input: Q, generateOptions?: { blockIds?: number[] }) => Promise<Result<O>> {\n return async (input: Q, generateOptions?: { blockIds?: number[] }) => {\n // Use provided blockIds or fall back to default selection\n const targetBlockIds = generateOptions?.blockIds ?? options.blockIds;\n if (options.providerInitializationResult == null) {\n if (options.debug) {\n // eslint-disable-next-line no-console\n console.warn(\n '[Generate] No provider initialization result found for quick action. Returning abortion status.'\n );\n }\n return { status: 'aborted' };\n }\n\n const supportValue =\n options.providerInitializationResult?.provider.input?.quickActions\n ?.supported?.[options.quickAction.id];\n\n if (supportValue == null) {\n throw new Error(\n '[Generate] Quick action input mapping failed. Ensure the provider supports this quick action.'\n );\n }\n\n // Handle both `true` and `{ mapInput: ... }` cases\n const mapInput =\n typeof supportValue === 'object' &&\n supportValue !== null &&\n 'mapInput' in supportValue\n ? supportValue.mapInput\n : (i: Q) => i as unknown as I; // Identity function when types are compatible\n\n options.close();\n\n const abortController = new AbortController();\n const abortSignal = AbortSignal.any(\n [options.abortSignal, abortController.signal].filter(isDefined)\n );\n\n const metadata = new Metadata<InferenceMetadata>(\n options.cesdk.engine,\n AI_METADATA_KEY\n );\n\n const unlockFromEditMode =\n options.lock ?? true\n ? lockSelectionToEditMode({\n engine: options.cesdk.engine,\n editModeToLockTo: AI_EDIT_MODE,\n blockIdsToLock: targetBlockIds\n })\n : () => {\n /* No-op if not locking to edit mode */\n };\n targetBlockIds.forEach((blockId) => {\n CallbacksRegistry.get().register(blockId, {\n onCancelGeneration: () => {\n abortController.abort(ABORT_REASON_USER_CANCEL);\n unlockFromEditMode();\n\n if (options.cesdk.engine.block.isValid(blockId)) {\n if (options.cesdk.engine.block.isValid(blockId))\n options.cesdk.engine.block.setState(blockId, { type: 'Ready' });\n metadata.clear(blockId);\n }\n }\n });\n });\n\n targetBlockIds?.forEach((blockId) => {\n metadata.set(blockId, {\n status: 'processing',\n quickActionId: options.quickAction.id\n });\n });\n targetBlockIds.forEach((blockId) => {\n if (options.cesdk.engine.block.isValid(blockId))\n options.cesdk.engine.block.setState(blockId, {\n type: 'Pending',\n progress: 0\n });\n });\n\n try {\n const result = await options.providerInitializationResult.generate(\n mapInput(input),\n {\n blockIds: targetBlockIds,\n middlewares: [...(options.middlewares ?? [])],\n debug: options.debug,\n dryRun: options.dryRun,\n abortSignal\n }\n );\n\n const resultStatus = result.status;\n switch (resultStatus) {\n case 'success': {\n const { applyCallbacks: initialApplyCallbacks } =\n await getApplyCallbacks(result, {\n kind: options.providerInitializationResult.provider.kind,\n blockIds: targetBlockIds,\n cesdk: options.cesdk,\n abortSignal\n });\n\n // Set the blocks to a ready state AFTER `getApplyCallbacks` was called since\n // it will set the block to the \"after\" image and until then we still want\n // the spinner spinning.\n targetBlockIds.forEach((blockId) => {\n if (options.cesdk.engine.block.isValid(blockId))\n options.cesdk.engine.block.setState(blockId, { type: 'Ready' });\n });\n\n if (options.confirmation) {\n // For the same as the Ready state, we do this AFTER `getApplyCallbacks` was called\n targetBlockIds.forEach((blockId) => {\n // Metadata will be cleared in the confirmation component\n // see createConfirmationRenderFunction.ts\n metadata.set(blockId, {\n status: 'confirmation',\n quickActionId: options.quickAction.id\n });\n });\n\n const applyCallbacks = {\n ...initialApplyCallbacks,\n onApply: async () => {\n initialApplyCallbacks.onApply();\n unlockFromEditMode();\n },\n onCancel: () => {\n initialApplyCallbacks.onCancel();\n unlockFromEditMode();\n }\n };\n\n targetBlockIds.forEach((blockId) => {\n CallbacksRegistry.get().register(blockId, { applyCallbacks });\n });\n } else {\n targetBlockIds.forEach((blockId) => {\n if (options.cesdk.engine.block.isValid(blockId)) {\n metadata.clear(blockId);\n }\n });\n // Apply the result directly\n initialApplyCallbacks.onApply();\n unlockFromEditMode();\n }\n\n return result;\n }\n\n case 'aborted': {\n if (options.debug) {\n // eslint-disable-next-line no-console\n console.log('Generation was aborted');\n }\n unlockFromEditMode();\n targetBlockIds.forEach((blockId) => {\n if (options.cesdk.engine.block.isValid(blockId)) {\n metadata.clear(blockId);\n }\n });\n return result;\n }\n\n case 'error': {\n unlockFromEditMode();\n targetBlockIds.forEach((blockId) => {\n if (options.cesdk.engine.block.isValid(blockId)) {\n metadata.clear(blockId);\n }\n });\n // Check if default was prevented\n if (!result.middlewareOptions?.defaultPrevented()) {\n // eslint-disable-next-line no-console\n console.error(`[Generate]: ${result.message}`);\n }\n return result;\n }\n\n default: {\n throw new Error(`[Generate] Unknown result status ${resultStatus}.`);\n }\n }\n } catch (error) {\n // Note: For exceptions thrown in middleware, we don't have access to middlewareOptions\n // so we can't check defaultPrevented here. Middleware should handle errors in catch blocks\n // and re-throw to use preventDefault properly.\n options.cesdk.ui.showNotification({\n type: 'error',\n message: 'A technical issue has occurred.'\n });\n\n unlockFromEditMode();\n targetBlockIds.forEach((blockId) => {\n if (options.cesdk.engine.block.isValid(blockId)) {\n metadata.clear(blockId);\n }\n });\n throw error;\n } finally {\n targetBlockIds.forEach((blockId) => {\n if (options.cesdk.engine.block.isValid(blockId))\n options.cesdk.engine.block.setState(blockId, { type: 'Ready' });\n });\n }\n };\n}\n\nexport default handleGenerateFromQuickAction;\n", "import CreativeEditorSDK, {\n BuilderRenderFunction,\n CreativeEngine,\n SelectValue\n} from '@cesdk/cesdk-js';\nimport {\n ActionRegistry,\n QuickActionDefinition\n} from '../../core/ActionRegistry';\nimport { Output, OutputKind } from '../../core/provider';\nimport { isDefined } from '@imgly/plugin-utils';\nimport { AI_EDIT_MODE } from './utils';\nimport compactSeparators from '../../utils/compactSeparators';\nimport getQuickActionOrder from './getQuickActionOrder';\nimport { ProviderInitializationResult } from '../../providers/initializeProvider';\nimport handleGenerateFromQuickAction from '../../generation/handleGenerateFromQuickAction';\nimport { Middleware } from '../../middleware/middleware';\nimport { ProviderRegistry } from '../../core/ProviderRegistry';\n\ntype SupportedQuickAction<K extends OutputKind, I, O extends Output> = {\n definition: QuickActionDefinition<any>;\n /**\n * If defined this provider is used to render the quick action.\n * Otherwise the quick action is rendered with the main provider\n * defined one level up.\n *\n * Used for quick actions that have been defined from a provider\n * that is not the main kind/provider, e.g. a video plugin\n * that defines a quick action for the image provider.\n */\n providerInitializationResult?: ProviderInitializationResult<K, I, O>;\n};\n\ntype SupportedProviderQuickActions<\n K extends OutputKind,\n I,\n O extends Output\n> = {\n /**\n * The main provider from the provider selection\n */\n providerInitializationResult: ProviderInitializationResult<K, I, O>;\n quickActions: (SupportedQuickAction<K, I, O> | 'ly.img.separator')[];\n}[];\n\nfunction createQuickActionMenuRenderFunction<\n K extends OutputKind,\n I,\n O extends Output\n>(context: {\n kind: K;\n providerInitializationResults: ProviderInitializationResult<K, I, O>[];\n\n cesdk: CreativeEditorSDK;\n engine: CreativeEngine;\n\n debug?: boolean;\n dryRun?: boolean;\n defaultOrder?: string[];\n}): Promise<BuilderRenderFunction<any>> {\n const prefix = `ly.img.ai.${context.kind}}`;\n\n context.cesdk.i18n.setTranslations({\n en: {\n [`ly.img.plugin-ai-generation-web.defaults.quickAction.providerSelect.label`]:\n 'Provider'\n }\n });\n\n const builderRenderFunction: BuilderRenderFunction<{\n children: ('ly.img.separator' | (string & {}))[];\n }> = (builderContext) => {\n if (builderContext.engine.editor.getEditMode() === AI_EDIT_MODE) {\n return;\n }\n\n const blockIds = builderContext.engine.block.findAllSelected();\n if (blockIds.length === 0) return;\n\n // Check if the general quickAction feature is enabled\n const quickActionFeatureId = `ly.img.plugin-ai-${context.kind}-generation-web.quickAction`;\n const isQuickActionFeatureEnabled = context.cesdk.feature.isEnabled(\n quickActionFeatureId,\n {\n engine: context.engine\n }\n );\n\n if (!isQuickActionFeatureEnabled) {\n // Quick actions are disabled for this plugin\n return;\n }\n\n const { payload } = builderContext;\n const order =\n getQuickActionOrder({\n kind: context.kind,\n cesdk: context.cesdk,\n payload,\n defaultOrder: context.defaultOrder\n }) ?? [];\n\n if (order.length === 0) return;\n\n // Get ordered and filtered list of defined quick actions\n const orderedQuickActions = getOrderedQuickActionDefinitions(order).filter(\n (quickActionDefinition) => {\n if (quickActionDefinition === 'ly.img.separator') return true;\n\n // Check if this individual quick action is enabled via Feature API\n // Remove the 'ly.img.' prefix from the action ID for the feature key\n const actionName = quickActionDefinition.id.replace('ly.img.', '');\n const individualQuickActionFeatureId = `ly.img.plugin-ai-${context.kind}-generation-web.quickAction.${actionName}`;\n const isIndividualQuickActionEnabled = context.cesdk.feature.isEnabled(\n individualQuickActionFeatureId,\n {\n engine: context.engine\n }\n );\n\n if (!isIndividualQuickActionEnabled) {\n return false;\n }\n\n const scopes = quickActionDefinition.scopes;\n if (scopes != null && scopes.length > 0) {\n const isAllowedByScopes = blockIds.every((blockId) => {\n return scopes.every((scope) => {\n return context.engine.block.isAllowedByScope(blockId, scope);\n });\n });\n if (!isAllowedByScopes) return false;\n }\n\n if (quickActionDefinition.enable != null) {\n if (typeof quickActionDefinition.enable === 'function') {\n return quickActionDefinition.enable({\n engine: context.engine\n });\n } else if (typeof quickActionDefinition.enable === 'boolean') {\n return quickActionDefinition.enable;\n }\n }\n\n return true;\n }\n );\n\n // Collect quick actions that are defined by the main provider\n // in case the main provider does not support any quick actions\n // and we just want to render these.\n const quickActionsFromOtherProviders: SupportedQuickAction<K, I, O>[] = [];\n\n // Collect all provider with their supported quick actions\n const supportedProviderQuickActions =\n context.providerInitializationResults.reduce(\n (\n acc: SupportedProviderQuickActions<K, I, O>,\n providerInitializationResult\n ) => {\n if (providerInitializationResult.provider.kind !== context.kind)\n return acc;\n\n let quickActions = orderedQuickActions\n .map((quickAction) => {\n if (quickAction === 'ly.img.separator') return quickAction;\n\n // Check if the main provider supports this quick action\n if (\n providerInitializationResult.provider.input?.quickActions\n ?.supported?.[quickAction.id] != null\n ) {\n return {\n definition: quickAction\n };\n } else {\n // Check if this quick action comes from another provider that\n // is not the main provider.\n const otherProviderInitializationResult = ProviderRegistry.get()\n .getAll()\n .filter((registeredProvider) => {\n return (\n // We are looking for provider from another kind\n // and assume that all provider from the same kind\n // have been passed as main provider\n registeredProvider.provider.kind !== context.kind &&\n registeredProvider.provider.id !==\n providerInitializationResult.provider.id\n );\n })\n .find((registeredProvider) => {\n return (\n registeredProvider.provider.input?.quickActions\n ?.supported?.[quickAction.id] != null\n );\n });\n if (otherProviderInitializationResult != null) {\n const quickActionSupport: SupportedQuickAction<K, I, O> = {\n definition: quickAction,\n providerInitializationResult:\n otherProviderInitializationResult\n };\n quickActionsFromOtherProviders.push(quickActionSupport);\n return quickActionSupport;\n } else {\n return undefined;\n }\n }\n })\n .filter(isDefined);\n // Clean up the quick action list so we can render it directly\n quickActions = compactSeparators(quickActions);\n\n if (\n quickActions.length === 0 ||\n quickActions.every((entry) => entry === 'ly.img.separator')\n )\n return acc;\n\n if (\n quickActions.every(\n (quickAction) =>\n quickAction === 'ly.img.separator' ||\n quickAction.providerInitializationResult != null\n )\n ) {\n // This provider has no quick actions from the current main provider.\n // We do not want to add the main provider to the selection\n // list with just actions from other providers.\n return acc;\n }\n\n acc.push({\n providerInitializationResult,\n quickActions\n });\n\n return acc;\n },\n []\n );\n\n if (supportedProviderQuickActions.length === 0) {\n if (quickActionsFromOtherProviders.length > 0) {\n // Remove duplicates from quickActionsFromOtherProviders\n const seen = new Map<string, SupportedQuickAction<K, I, O>>();\n quickActionsFromOtherProviders.forEach((quickAction) => {\n const id = quickAction.definition.id;\n if (!seen.has(id)) {\n seen.set(id, quickAction);\n }\n });\n const uniqueQuickActions = Array.from(seen.values());\n\n // If we have quick actions from other providers, we can render them\n supportedProviderQuickActions.push({\n // Use the first provider as main but since we do not want to\n // render the provider selection, we do not care what provider it is.\n // The actual provider used is the one defined in the object.\n providerInitializationResult:\n uniqueQuickActions[0].providerInitializationResult ??\n context.providerInitializationResults[0],\n quickActions: uniqueQuickActions\n });\n } else {\n // Noting to do\n return;\n }\n }\n\n const providerValues: SelectValue[] = supportedProviderQuickActions.map(\n ({ providerInitializationResult }) => ({\n id: providerInitializationResult.provider.id,\n label:\n providerInitializationResult.provider.name ??\n providerInitializationResult.provider.id\n })\n );\n\n const currentProviderState = builderContext.experimental.global(\n `${prefix}.currentProvider`,\n providerValues[0]\n );\n const currentSupportedQuickActions = supportedProviderQuickActions.find(\n ({\n providerInitializationResult: {\n provider: { id }\n }\n }) => id === currentProviderState.value?.id\n );\n\n const isEveryBlockInReadyState = blockIds.every((blockId) => {\n return builderContext.engine.block.getState(blockId).type === 'Ready';\n });\n\n const { builder, experimental, state } = builderContext;\n\n const isGeneratingState = state<boolean>(`${prefix}.isGenerating`, false);\n const toggleExpandedState = state<string | undefined>(\n `${prefix}.toggleExpandedState`,\n undefined\n );\n\n // Middleware to track generation status\n const isGeneratingMiddleware: Middleware<I, O> = async (\n input,\n options,\n next\n ) => {\n isGeneratingState.setValue(true);\n try {\n const result = await next(input, options);\n return result;\n } finally {\n isGeneratingState.setValue(false);\n }\n };\n\n experimental.builder.Popover(`${prefix}.popover`, {\n icon: '@imgly/Sparkle',\n variant: 'plain',\n isDisabled: !isEveryBlockInReadyState,\n isLoading: isGeneratingState.value,\n trailingIcon: null,\n children: ({ close }) => {\n if (toggleExpandedState.value !== undefined) {\n // ==========================================\n // === RENDER EXPANDED QUICK ACTION STATE ===\n // ==========================================\n\n const expandedQuickAction =\n currentSupportedQuickActions?.quickActions.find(\n (quickAction) =>\n quickAction !== 'ly.img.separator' &&\n quickAction.definition.id === toggleExpandedState.value\n ) as SupportedQuickAction<K, I, O> | undefined;\n\n if (\n expandedQuickAction == null ||\n expandedQuickAction.definition.render == null\n ) {\n return;\n }\n\n // Use only providers that support the current expanded quick action\n const providerValuesForExpandedQuickAction: SelectValue[] =\n supportedProviderQuickActions\n .filter(({ quickActions }) =>\n quickActions.some(\n (qa) =>\n qa !== 'ly.img.separator' &&\n qa.definition.id === expandedQuickAction.definition.id\n )\n )\n .map(({ providerInitializationResult }) => ({\n id: providerInitializationResult.provider.id,\n label:\n providerInitializationResult.provider.name ??\n providerInitializationResult.provider.id\n }));\n\n // Check if provider selector is enabled via Feature API\n const providerFeatureId = `ly.img.plugin-ai-${context.kind}-generation-web.quickAction.providerSelect`;\n const isProviderSelectorEnabled = context.cesdk.feature.isEnabled(\n providerFeatureId,\n {\n engine: context.engine\n }\n );\n\n if (\n providerValuesForExpandedQuickAction.length > 1 &&\n isProviderSelectorEnabled\n ) {\n builder.Section(`${prefix}.popover.expanded.header`, {\n children: () => {\n builder.Select(`${prefix}.expanded.providerSelect.select`, {\n inputLabel: [\n `ly.img.plugin-ai-${context.kind}-generation-web.quickAction.providerSelect.label`,\n `ly.img.plugin-ai-generation-web.defaults.quickAction.providerSelect.label`\n ],\n values: providerValuesForExpandedQuickAction,\n ...currentProviderState\n });\n }\n });\n }\n builder.Section(`${prefix}.popover.expanded.section`, {\n children: () => {\n return expandedQuickAction.definition.render({\n ...builderContext,\n toggleExpand: () => {\n toggleExpandedState.setValue(undefined);\n },\n isExpanded: true,\n generate: handleGenerateFromQuickAction({\n blockIds,\n providerInitializationResult:\n expandedQuickAction.providerInitializationResult ??\n currentSupportedQuickActions?.providerInitializationResult,\n quickAction: expandedQuickAction.definition,\n middlewares: [isGeneratingMiddleware],\n\n confirmation:\n expandedQuickAction.definition.defaults?.confirmation ??\n true,\n\n lock: expandedQuickAction.definition.defaults?.lock ?? true,\n\n close,\n cesdk: context.cesdk,\n debug: context.debug,\n dryRun: context.dryRun\n }),\n close,\n providerId: currentProviderState.value.id\n });\n }\n });\n } else {\n // =========================================\n // === RENDER REGULAR QUICK ACTIONS MENU ===\n // =========================================\n // Check if provider selector is enabled via Feature API\n const providerFeatureId = `ly.img.plugin-ai-${context.kind}-generation-web.quickAction.providerSelect`;\n const isProviderSelectorEnabled = context.cesdk.feature.isEnabled(\n providerFeatureId,\n {\n engine: context.engine\n }\n );\n\n if (providerValues.length > 1 && isProviderSelectorEnabled) {\n builder.Section(`${prefix}.popover.header`, {\n children: () => {\n builder.Select(`${prefix}.providerSelect.select`, {\n inputLabel: [\n `ly.img.plugin-ai-${context.kind}-generation-web.quickAction.providerSelect.label`,\n `ly.img.plugin-ai-generation-web.defaults.quickAction.providerSelect.label`\n ],\n values: providerValues,\n ...currentProviderState\n });\n }\n });\n }\n builder.Section(`${prefix}.popover.section`, {\n children: () => {\n experimental.builder.Menu(`${prefix}.menu`, {\n children: () => {\n currentSupportedQuickActions?.quickActions.forEach(\n (quickAction) => {\n if (quickAction === 'ly.img.separator') {\n builder.Separator(\n `${prefix}.separator.${Math.random().toString()}`\n );\n return;\n }\n if (quickAction.definition.render == null) return;\n quickAction.definition.render({\n ...builderContext,\n toggleExpand: () => {\n toggleExpandedState.setValue(\n quickAction.definition.id\n );\n },\n isExpanded: false,\n generate: handleGenerateFromQuickAction({\n blockIds,\n providerInitializationResult:\n quickAction.providerInitializationResult ??\n currentSupportedQuickActions?.providerInitializationResult,\n quickAction: quickAction.definition,\n middlewares: [isGeneratingMiddleware],\n\n confirmation:\n quickAction.definition.defaults?.confirmation ??\n true,\n\n close,\n cesdk: context.cesdk,\n debug: context.debug,\n dryRun: context.dryRun\n }),\n close,\n providerId: currentProviderState.value.id\n });\n }\n );\n }\n });\n }\n });\n }\n }\n });\n };\n return Promise.resolve(builderRenderFunction);\n}\n\nfunction getOrderedQuickActionDefinitions(\n order: string[]\n): (QuickActionDefinition<any> | 'ly.img.separator')[] {\n return order\n .map((quickActionId) => {\n if (quickActionId === 'ly.img.separator')\n return quickActionId as 'ly.img.separator';\n\n const quickAction = ActionRegistry.get().getBy({\n id: quickActionId,\n type: 'quick'\n })[0];\n\n return quickAction;\n })\n .filter(isDefined);\n}\n\nexport default createQuickActionMenuRenderFunction;\n", "import CreativeEditorSDK, {\n BuilderRenderFunction,\n CreativeEngine\n} from '@cesdk/cesdk-js';\nimport { Output, OutputKind } from '../../core/provider';\nimport createConfirmationRenderFunction from '../panels/createConfirmationRenderFunction';\nimport createQuickActionMenuRenderFunction from './createQuickActionMenuRenderFunction';\nimport { AI_EDIT_MODE } from './utils';\nimport { ProviderInitializationResult } from '../../providers/initializeProvider';\nimport CallbacksRegistry from '../../generation/CallbacksRegistry';\n\nasync function initializeQuickActionComponents<\n K extends OutputKind,\n I,\n O extends Output\n>(context: {\n kind: K;\n providerInitializationResults: ProviderInitializationResult<K, I, O>[];\n\n cesdk: CreativeEditorSDK;\n engine: CreativeEngine;\n debug?: boolean;\n dryRun?: boolean;\n defaultOrder?: string[];\n}) {\n const menuRenderFunction = await createQuickActionMenuRenderFunction({\n kind: context.kind,\n providerInitializationResults: context.providerInitializationResults,\n\n cesdk: context.cesdk,\n engine: context.engine,\n debug: context.debug,\n dryRun: context.dryRun,\n defaultOrder: context.defaultOrder\n });\n const confirmationRenderFunction = await createConfirmationRenderFunction({\n kind: context.kind,\n\n cesdk: context.cesdk\n });\n\n const builderRenderFunction: BuilderRenderFunction<any> = (\n builderContext\n ) => {\n const { engine } = builderContext;\n if (engine.editor.getEditMode() === AI_EDIT_MODE) {\n const blockIds = builderContext.engine.block.findAllSelected();\n confirmationRenderFunction({\n ...builderContext,\n payload: {\n ...(builderContext.payload ?? {}),\n applyCallbacks: {\n onBefore: () => {\n blockIds.forEach((blockId) => {\n CallbacksRegistry.get()\n .get(blockId)\n .applyCallbacks?.onBefore?.();\n });\n },\n onAfter: () => {\n blockIds.forEach((blockId) => {\n CallbacksRegistry.get()\n .get(blockId)\n .applyCallbacks?.onAfter?.();\n });\n },\n onCancel: () => {\n blockIds.forEach((blockId) => {\n CallbacksRegistry.get()\n .get(blockId)\n .applyCallbacks?.onCancel?.();\n });\n },\n onApply: () => {\n blockIds.forEach((blockId) => {\n CallbacksRegistry.get()\n .get(blockId)\n .applyCallbacks?.onApply?.();\n });\n }\n },\n onCancelGeneration: () => {\n blockIds.forEach((blockId) => {\n CallbacksRegistry.get().get(blockId).onCancelGeneration?.();\n });\n }\n }\n });\n return;\n }\n\n menuRenderFunction(builderContext);\n };\n\n return {\n renderFunction: builderRenderFunction\n };\n}\n\nexport default initializeQuickActionComponents;\n", "import type CreativeEditorSDK from '@cesdk/cesdk-js';\nimport { supportsTranslateAPI, hasTranslateAPI } from '@imgly/plugin-utils';\n\n/**\n * Creates a translation callback function for AI asset sources\n * @param cesdk - The CE.SDK instance\n * @param modelKey - The model/provider key (e.g., 'fal-ai/recraft-v3')\n * @param propertyName - The property name (e.g., 'style', 'aspect_ratio')\n * @param pluginType - The plugin type (e.g., 'image', 'video', 'sticker')\n * @returns A translation callback function for use with CustomAssetSource\n */\nexport function createTranslationCallback(\n cesdk: CreativeEditorSDK,\n modelKey: string,\n propertyName: string = 'style',\n pluginType: string = 'image'\n): (assetId: string, fallbackLabel: string, locale: string) => string {\n return (assetId: string, fallbackLabel: string): string => {\n // Check if CE.SDK supports translation API\n if (!supportsTranslateAPI(cesdk)) {\n return fallbackLabel;\n }\n\n // Build translation keys following established AI plugin pattern\n const translationKeys = buildTranslationKeys(\n modelKey,\n propertyName,\n assetId,\n pluginType\n );\n\n // Use CE.SDK's translate method with fallback array\n if (hasTranslateAPI(cesdk.i18n)) {\n const translated = cesdk.i18n.translate(translationKeys);\n\n // Return translated label or fallback if no translation found\n // (CE.SDK returns the last key if no translation is found)\n return translated !== translationKeys[translationKeys.length - 1]\n ? translated\n : fallbackLabel;\n }\n\n return fallbackLabel;\n };\n}\n\n/**\n * Build translation keys array for AI plugin property values\n * @param modelKey - The model/provider key\n * @param propertyName - The property name\n * @param value - The property value\n * @param pluginType - The plugin type (image, video, sticker, etc.)\n * @returns Array of translation keys in fallback order\n */\nexport function buildTranslationKeys(\n modelKey: string,\n propertyName: string,\n value: string,\n pluginType: string = 'image'\n): string[] {\n return [\n `ly.img.plugin-ai-${pluginType}-generation-web.${modelKey}.property.${propertyName}.${value}`,\n `ly.img.plugin-ai-generation-web.property.${propertyName}.${value}`,\n `ly.img.plugin-ai-${pluginType}-generation-web.${modelKey}.defaults.property.${propertyName}.${value}`,\n `ly.img.plugin-ai-generation-web.defaults.property.${propertyName}.${value}`\n ];\n}\n", "import renderImageUrlProperty from './ui/common/renderImageUrlProperty';\nimport renderStyleTransferProperty from './ui/common/renderStyleTransferProperty';\n\nconst CommonProperties = {\n ImageUrl: renderImageUrlProperty,\n StyleTransfer: renderStyleTransferProperty\n};\n\nexport { CommonProperties };\n\nexport {\n type default as Provider,\n type ImageOutput,\n type VideoOutput,\n type TextOutput,\n type AudioOutput,\n type StickerOutput,\n type Output,\n type OutputKind,\n type PanelInputSchema,\n type RenderCustomProperty,\n type GetBlockInput,\n type GetBlockInputResult,\n type GetInput\n} from './core/provider';\nexport { type GetPropertyInput, type Property } from './openapi/types';\nexport {\n type GetProvider,\n type CommonProviderConfiguration,\n type CommonPluginConfiguration,\n type CommonConfiguration,\n type InternalPluginConfiguration\n} from './types';\n\nexport type {\n PropertyContext,\n PropertyConfig,\n PropertiesConfiguration,\n ExtendPropertyContexts\n} from './core/propertyConfiguration';\n\nexport {\n buildPropertyContext,\n PropertyContextCache\n} from './utils/propertyContext';\n\nexport {\n resolvePropertyDefault,\n resolvePropertyDefaults\n} from './utils/propertyResolver';\n\nexport { default as integrateIntoDefaultAssetLibraryEntry } from './assets/integrateIntoDefaultAssetLibraryEntry';\nexport {\n ActionRegistry,\n type PluginActionDefinition,\n type QuickActionDefinition,\n type ActionDefinition,\n type ActionRegistryEventType,\n type ActionRegistrySubscriberCallback,\n type ActionRegistryFilters\n} from './core/ActionRegistry';\nexport { ProviderRegistry } from './core/ProviderRegistry';\n\n// Export middleware\nexport { composeMiddlewares, type Middleware } from './middleware/middleware';\nexport { default as loggingMiddleware } from './middleware/loggingMiddleware';\nexport { default as uploadMiddleware } from './middleware/uploadMiddleware';\n\n// Export utilities\nexport { mergeQuickActionsConfig } from './utils/mergeQuickActionsConfig';\nexport {\n default as rateLimitMiddleware,\n type RateLimitOptions\n} from './middleware/rateLimitMiddleware';\n\nexport {\n getPanelId,\n getDurationForVideo,\n getThumbnailForVideo,\n getLabelFromId,\n isAsyncGenerator,\n addIconSetOnce\n} from './utils/utils';\n\nexport { checkAiPluginVersion } from './utils/checkAiPluginVersion';\n\nexport { default as registerDockComponent } from './ui/components/registerDockComponent';\n\nexport { default as enableQuickActionForImageFill } from './ui/quickActions/enableImageFill';\n\nexport {\n isGeneratingStateKey,\n abortGenerationStateKey\n} from './ui/components/renderGenerationComponents';\n\nexport { default as initializeProviders } from './providers/initializeProviders';\nexport { default as initializeProvider } from './providers/initializeProvider';\nexport { default as initializeQuickActionComponents } from './ui/quickActions/initializeQuickActionComponents';\n\nexport { extractAndSetSchemaTranslations } from './openapi/extractSchemaTranslations';\n\nexport { AI_EDIT_MODE, AI_METADATA_KEY } from './ui/quickActions/utils';\n\n// Export AI-specific translation helpers\nexport {\n createTranslationCallback,\n buildTranslationKeys\n} from './utils/translationHelpers';\n", "export const VERSION = '0.39.0'; // x-release-please-version\n", "/**\n * Disclaimer: modules in _shims aren't intended to be imported by SDK users.\n */\nimport { type RequestOptions } from \"../core.js\";\n\nexport interface Shims {\n kind: string;\n fetch: any;\n Request: any;\n Response: any;\n Headers: any;\n FormData: any;\n Blob: any;\n File: any;\n ReadableStream: any;\n getMultipartRequestOptions: <T = Record<string, unknown>>(\n form: Shims['FormData'],\n opts: RequestOptions<T>,\n ) => Promise<RequestOptions<T>>;\n getDefaultAgent: (url: string) => any;\n fileFromPath:\n | ((path: string, filename?: string, options?: {}) => Promise<Shims['File']>)\n | ((path: string, options?: {}) => Promise<Shims['File']>);\n isFsReadStream: (value: any) => boolean;\n}\n\nexport let auto = false;\nexport let kind: Shims['kind'] | undefined = undefined;\nexport let fetch: Shims['fetch'] | undefined = undefined;\nexport let Request: Shims['Request'] | undefined = undefined;\nexport let Response: Shims['Response'] | undefined = undefined;\nexport let Headers: Shims['Headers'] | undefined = undefined;\nexport let FormData: Shims['FormData'] | undefined = undefined;\nexport let Blob: Shims['Blob'] | undefined = undefined;\nexport let File: Shims['File'] | undefined = undefined;\nexport let ReadableStream: Shims['ReadableStream'] | undefined = undefined;\nexport let getMultipartRequestOptions: Shims['getMultipartRequestOptions'] | undefined = undefined;\nexport let getDefaultAgent: Shims['getDefaultAgent'] | undefined = undefined;\nexport let fileFromPath: Shims['fileFromPath'] | undefined = undefined;\nexport let isFsReadStream: Shims['isFsReadStream'] | undefined = undefined;\n\nexport function setShims(shims: Shims, options: { auto: boolean } = { auto: false }) {\n if (auto) {\n throw new Error(\n `you must \\`import '@anthropic-ai/sdk/shims/${shims.kind}'\\` before importing anything else from @anthropic-ai/sdk`,\n );\n }\n if (kind) {\n throw new Error(\n `can't \\`import '@anthropic-ai/sdk/shims/${shims.kind}'\\` after \\`import '@anthropic-ai/sdk/shims/${kind}'\\``,\n );\n }\n auto = options.auto;\n kind = shims.kind;\n fetch = shims.fetch;\n Request = shims.Request;\n Response = shims.Response;\n Headers = shims.Headers;\n FormData = shims.FormData;\n Blob = shims.Blob;\n File = shims.File;\n ReadableStream = shims.ReadableStream;\n getMultipartRequestOptions = shims.getMultipartRequestOptions;\n getDefaultAgent = shims.getDefaultAgent;\n fileFromPath = shims.fileFromPath;\n isFsReadStream = shims.isFsReadStream;\n}\n", "/**\n * Disclaimer: modules in _shims aren't intended to be imported by SDK users.\n */\nexport class MultipartBody {\n constructor(public body: any) {}\n get [Symbol.toStringTag](): string {\n return 'MultipartBody';\n }\n}\n", "/**\n * Disclaimer: modules in _shims aren't intended to be imported by SDK users.\n */\nimport { MultipartBody } from \"./MultipartBody.js\";\nimport { type RequestOptions } from \"../core.js\";\nimport { type Shims } from \"./registry.js\";\n\nexport function getRuntime({ manuallyImported }: { manuallyImported?: boolean } = {}): Shims {\n const recommendation =\n manuallyImported ?\n `You may need to use polyfills`\n : `Add one of these imports before your first \\`import \u2026 from '@anthropic-ai/sdk'\\`:\n- \\`import '@anthropic-ai/sdk/shims/node'\\` (if you're running on Node)\n- \\`import '@anthropic-ai/sdk/shims/web'\\` (otherwise)\n`;\n\n let _fetch, _Request, _Response, _Headers;\n try {\n // @ts-ignore\n _fetch = fetch;\n // @ts-ignore\n _Request = Request;\n // @ts-ignore\n _Response = Response;\n // @ts-ignore\n _Headers = Headers;\n } catch (error) {\n throw new Error(\n `this environment is missing the following Web Fetch API type: ${\n (error as any).message\n }. ${recommendation}`,\n );\n }\n\n return {\n kind: 'web',\n fetch: _fetch,\n Request: _Request,\n Response: _Response,\n Headers: _Headers,\n FormData:\n // @ts-ignore\n typeof FormData !== 'undefined' ? FormData : (\n class FormData {\n // @ts-ignore\n constructor() {\n throw new Error(\n `file uploads aren't supported in this environment yet as 'FormData' is undefined. ${recommendation}`,\n );\n }\n }\n ),\n Blob:\n typeof Blob !== 'undefined' ? Blob : (\n class Blob {\n constructor() {\n throw new Error(\n `file uploads aren't supported in this environment yet as 'Blob' is undefined. ${recommendation}`,\n );\n }\n }\n ),\n File:\n // @ts-ignore\n typeof File !== 'undefined' ? File : (\n class File {\n // @ts-ignore\n constructor() {\n throw new Error(\n `file uploads aren't supported in this environment yet as 'File' is undefined. ${recommendation}`,\n );\n }\n }\n ),\n ReadableStream:\n // @ts-ignore\n typeof ReadableStream !== 'undefined' ? ReadableStream : (\n class ReadableStream {\n // @ts-ignore\n constructor() {\n throw new Error(\n `streaming isn't supported in this environment yet as 'ReadableStream' is undefined. ${recommendation}`,\n );\n }\n }\n ),\n getMultipartRequestOptions: async <T = Record<string, unknown>>(\n // @ts-ignore\n form: FormData,\n opts: RequestOptions<T>,\n ): Promise<RequestOptions<T>> => ({\n ...opts,\n body: new MultipartBody(form) as any,\n }),\n getDefaultAgent: (url: string) => undefined,\n fileFromPath: () => {\n throw new Error(\n 'The `fileFromPath` function is only supported in Node. See the README for more details: https://www.github.com/anthropics/anthropic-sdk-typescript#file-uploads',\n );\n },\n isFsReadStream: (value: any) => false,\n };\n}\n", "/**\n * Disclaimer: modules in _shims aren't intended to be imported by SDK users.\n */\nimport * as shims from './registry.mjs';\nimport * as auto from '@anthropic-ai/sdk/_shims/auto/runtime';\nif (!shims.kind) shims.setShims(auto.getRuntime(), { auto: true });\nexport * from './registry.mjs';\n", "// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\n\nimport { castToError, Headers } from \"./core.js\";\n\nexport class AnthropicError extends Error {}\n\nexport class APIError<\n TStatus extends number | undefined = number | undefined,\n THeaders extends Headers | undefined = Headers | undefined,\n TError extends Object | undefined = Object | undefined,\n> extends AnthropicError {\n /** HTTP status for the response that caused the error */\n readonly status: TStatus;\n /** HTTP headers for the response that caused the error */\n readonly headers: THeaders;\n /** JSON body of the response that caused the error */\n readonly error: TError;\n\n readonly request_id: string | null | undefined;\n\n constructor(status: TStatus, error: TError, message: string | undefined, headers: THeaders) {\n super(`${APIError.makeMessage(status, error, message)}`);\n this.status = status;\n this.headers = headers;\n this.request_id = headers?.['request-id'];\n this.error = error;\n }\n\n private static makeMessage(status: number | undefined, error: any, message: string | undefined) {\n const msg =\n error?.message ?\n typeof error.message === 'string' ?\n error.message\n : JSON.stringify(error.message)\n : error ? JSON.stringify(error)\n : message;\n\n if (status && msg) {\n return `${status} ${msg}`;\n }\n if (status) {\n return `${status} status code (no body)`;\n }\n if (msg) {\n return msg;\n }\n return '(no status code or body)';\n }\n\n static generate(\n status: number | undefined,\n errorResponse: Object | undefined,\n message: string | undefined,\n headers: Headers | undefined,\n ): APIError {\n if (!status || !headers) {\n return new APIConnectionError({ message, cause: castToError(errorResponse) });\n }\n\n const error = errorResponse as Record<string, any>;\n\n if (status === 400) {\n return new BadRequestError(status, error, message, headers);\n }\n\n if (status === 401) {\n return new AuthenticationError(status, error, message, headers);\n }\n\n if (status === 403) {\n return new PermissionDeniedError(status, error, message, headers);\n }\n\n if (status === 404) {\n return new NotFoundError(status, error, message, headers);\n }\n\n if (status === 409) {\n return new ConflictError(status, error, message, headers);\n }\n\n if (status === 422) {\n return new UnprocessableEntityError(status, error, message, headers);\n }\n\n if (status === 429) {\n return new RateLimitError(status, error, message, headers);\n }\n\n if (status >= 500) {\n return new InternalServerError(status, error, message, headers);\n }\n\n return new APIError(status, error, message, headers);\n }\n}\n\nexport class APIUserAbortError extends APIError<undefined, undefined, undefined> {\n constructor({ message }: { message?: string } = {}) {\n super(undefined, undefined, message || 'Request was aborted.', undefined);\n }\n}\n\nexport class APIConnectionError extends APIError<undefined, undefined, undefined> {\n constructor({ message, cause }: { message?: string | undefined; cause?: Error | undefined }) {\n super(undefined, undefined, message || 'Connection error.', undefined);\n // in some environments the 'cause' property is already declared\n // @ts-ignore\n if (cause) this.cause = cause;\n }\n}\n\nexport class APIConnectionTimeoutError extends APIConnectionError {\n constructor({ message }: { message?: string } = {}) {\n super({ message: message ?? 'Request timed out.' });\n }\n}\n\nexport class BadRequestError extends APIError<400, Headers> {}\n\nexport class AuthenticationError extends APIError<401, Headers> {}\n\nexport class PermissionDeniedError extends APIError<403, Headers> {}\n\nexport class NotFoundError extends APIError<404, Headers> {}\n\nexport class ConflictError extends APIError<409, Headers> {}\n\nexport class UnprocessableEntityError extends APIError<422, Headers> {}\n\nexport class RateLimitError extends APIError<429, Headers> {}\n\nexport class InternalServerError extends APIError<number, Headers> {}\n", "import { AnthropicError } from \"../../error.js\";\n\nexport type Bytes = string | ArrayBuffer | Uint8Array | Buffer | null | undefined;\n\n/**\n * A re-implementation of httpx's `LineDecoder` in Python that handles incrementally\n * reading lines from text.\n *\n * https://github.com/encode/httpx/blob/920333ea98118e9cf617f246905d7b202510941c/httpx/_decoders.py#L258\n */\nexport class LineDecoder {\n // prettier-ignore\n static NEWLINE_CHARS = new Set(['\\n', '\\r']);\n static NEWLINE_REGEXP = /\\r\\n|[\\n\\r]/g;\n\n buffer: Uint8Array;\n #carriageReturnIndex: number | null;\n textDecoder: any; // TextDecoder found in browsers; not typed to avoid pulling in either \"dom\" or \"node\" types.\n\n constructor() {\n this.buffer = new Uint8Array();\n this.#carriageReturnIndex = null;\n }\n\n decode(chunk: Bytes): string[] {\n if (chunk == null) {\n return [];\n }\n\n const binaryChunk =\n chunk instanceof ArrayBuffer ? new Uint8Array(chunk)\n : typeof chunk === 'string' ? new TextEncoder().encode(chunk)\n : chunk;\n\n let newData = new Uint8Array(this.buffer.length + binaryChunk.length);\n newData.set(this.buffer);\n newData.set(binaryChunk, this.buffer.length);\n this.buffer = newData;\n\n const lines: string[] = [];\n let patternIndex;\n while ((patternIndex = findNewlineIndex(this.buffer, this.#carriageReturnIndex)) != null) {\n if (patternIndex.carriage && this.#carriageReturnIndex == null) {\n // skip until we either get a corresponding `\\n`, a new `\\r` or nothing\n this.#carriageReturnIndex = patternIndex.index;\n continue;\n }\n\n // we got double \\r or \\rtext\\n\n if (\n this.#carriageReturnIndex != null &&\n (patternIndex.index !== this.#carriageReturnIndex + 1 || patternIndex.carriage)\n ) {\n lines.push(this.decodeText(this.buffer.slice(0, this.#carriageReturnIndex - 1)));\n this.buffer = this.buffer.slice(this.#carriageReturnIndex);\n this.#carriageReturnIndex = null;\n continue;\n }\n\n const endIndex =\n this.#carriageReturnIndex !== null ? patternIndex.preceding - 1 : patternIndex.preceding;\n\n const line = this.decodeText(this.buffer.slice(0, endIndex));\n lines.push(line);\n\n this.buffer = this.buffer.slice(patternIndex.index);\n this.#carriageReturnIndex = null;\n }\n\n return lines;\n }\n\n decodeText(bytes: Bytes): string {\n if (bytes == null) return '';\n if (typeof bytes === 'string') return bytes;\n\n // Node:\n if (typeof Buffer !== 'undefined') {\n if (bytes instanceof Buffer) {\n return bytes.toString();\n }\n if (bytes instanceof Uint8Array) {\n return Buffer.from(bytes).toString();\n }\n\n throw new AnthropicError(\n `Unexpected: received non-Uint8Array (${bytes.constructor.name}) stream chunk in an environment with a global \"Buffer\" defined, which this library assumes to be Node. Please report this error.`,\n );\n }\n\n // Browser\n if (typeof TextDecoder !== 'undefined') {\n if (bytes instanceof Uint8Array || bytes instanceof ArrayBuffer) {\n this.textDecoder ??= new TextDecoder('utf8');\n return this.textDecoder.decode(bytes);\n }\n\n throw new AnthropicError(\n `Unexpected: received non-Uint8Array/ArrayBuffer (${\n (bytes as any).constructor.name\n }) in a web platform. Please report this error.`,\n );\n }\n\n throw new AnthropicError(\n `Unexpected: neither Buffer nor TextDecoder are available as globals. Please report this error.`,\n );\n }\n\n flush(): string[] {\n if (!this.buffer.length) {\n return [];\n }\n return this.decode('\\n');\n }\n}\n\n/**\n * This function searches the buffer for the end patterns, (\\r or \\n)\n * and returns an object with the index preceding the matched newline and the\n * index after the newline char. `null` is returned if no new line is found.\n *\n * ```ts\n * findNewLineIndex('abc\\ndef') -> { preceding: 2, index: 3 }\n * ```\n */\nfunction findNewlineIndex(\n buffer: Uint8Array,\n startIndex: number | null,\n): { preceding: number; index: number; carriage: boolean } | null {\n const newline = 0x0a; // \\n\n const carriage = 0x0d; // \\r\n\n for (let i = startIndex ?? 0; i < buffer.length; i++) {\n if (buffer[i] === newline) {\n return { preceding: i, index: i + 1, carriage: false };\n }\n\n if (buffer[i] === carriage) {\n return { preceding: i, index: i + 1, carriage: true };\n }\n }\n\n return null;\n}\n\nexport function findDoubleNewlineIndex(buffer: Uint8Array): number {\n // This function searches the buffer for the end patterns (\\r\\r, \\n\\n, \\r\\n\\r\\n)\n // and returns the index right after the first occurrence of any pattern,\n // or -1 if none of the patterns are found.\n const newline = 0x0a; // \\n\n const carriage = 0x0d; // \\r\n\n for (let i = 0; i < buffer.length - 1; i++) {\n if (buffer[i] === newline && buffer[i + 1] === newline) {\n // \\n\\n\n return i + 2;\n }\n if (buffer[i] === carriage && buffer[i + 1] === carriage) {\n // \\r\\r\n return i + 2;\n }\n if (\n buffer[i] === carriage &&\n buffer[i + 1] === newline &&\n i + 3 < buffer.length &&\n buffer[i + 2] === carriage &&\n buffer[i + 3] === newline\n ) {\n // \\r\\n\\r\\n\n return i + 4;\n }\n }\n\n return -1;\n}\n", "/**\n * Most browsers don't yet have async iterable support for ReadableStream,\n * and Node has a very different way of reading bytes from its \"ReadableStream\".\n *\n * This polyfill was pulled from https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1627354490\n */\nexport function ReadableStreamToAsyncIterable<T>(stream: any): AsyncIterableIterator<T> {\n if (stream[Symbol.asyncIterator]) return stream;\n\n const reader = stream.getReader();\n return {\n async next() {\n try {\n const result = await reader.read();\n if (result?.done) reader.releaseLock(); // release lock when stream becomes closed\n return result;\n } catch (e) {\n reader.releaseLock(); // release lock when stream becomes errored\n throw e;\n }\n },\n async return() {\n const cancelPromise = reader.cancel();\n reader.releaseLock();\n await cancelPromise;\n return { done: true, value: undefined };\n },\n [Symbol.asyncIterator]() {\n return this;\n },\n };\n}\n", "import { ReadableStream, type Response } from \"./_shims/index.js\";\nimport { AnthropicError } from \"./error.js\";\nimport { findDoubleNewlineIndex, LineDecoder } from \"./internal/decoders/line.js\";\nimport { ReadableStreamToAsyncIterable } from \"./internal/stream-utils.js\";\n\nimport { createResponseHeaders } from \"./core.js\";\nimport { APIError } from \"./error.js\";\n\ntype Bytes = string | ArrayBuffer | Uint8Array | Buffer | null | undefined;\n\nexport type ServerSentEvent = {\n event: string | null;\n data: string;\n raw: string[];\n};\n\nexport class Stream<Item> implements AsyncIterable<Item> {\n controller: AbortController;\n\n constructor(\n private iterator: () => AsyncIterator<Item>,\n controller: AbortController,\n ) {\n this.controller = controller;\n }\n\n static fromSSEResponse<Item>(response: Response, controller: AbortController): Stream<Item> {\n let consumed = false;\n\n async function* iterator(): AsyncIterator<Item, any, undefined> {\n if (consumed) {\n throw new Error('Cannot iterate over a consumed stream, use `.tee()` to split the stream.');\n }\n consumed = true;\n let done = false;\n try {\n for await (const sse of _iterSSEMessages(response, controller)) {\n if (sse.event === 'completion') {\n try {\n yield JSON.parse(sse.data);\n } catch (e) {\n console.error(`Could not parse message into JSON:`, sse.data);\n console.error(`From chunk:`, sse.raw);\n throw e;\n }\n }\n\n if (\n sse.event === 'message_start' ||\n sse.event === 'message_delta' ||\n sse.event === 'message_stop' ||\n sse.event === 'content_block_start' ||\n sse.event === 'content_block_delta' ||\n sse.event === 'content_block_stop'\n ) {\n try {\n yield JSON.parse(sse.data);\n } catch (e) {\n console.error(`Could not parse message into JSON:`, sse.data);\n console.error(`From chunk:`, sse.raw);\n throw e;\n }\n }\n\n if (sse.event === 'ping') {\n continue;\n }\n\n if (sse.event === 'error') {\n throw APIError.generate(\n undefined,\n `SSE Error: ${sse.data}`,\n sse.data,\n createResponseHeaders(response.headers),\n );\n }\n }\n done = true;\n } catch (e) {\n // If the user calls `stream.controller.abort()`, we should exit without throwing.\n if (e instanceof Error && e.name === 'AbortError') return;\n throw e;\n } finally {\n // If the user `break`s, abort the ongoing request.\n if (!done) controller.abort();\n }\n }\n\n return new Stream(iterator, controller);\n }\n\n /**\n * Generates a Stream from a newline-separated ReadableStream\n * where each item is a JSON value.\n */\n static fromReadableStream<Item>(readableStream: ReadableStream, controller: AbortController): Stream<Item> {\n let consumed = false;\n\n async function* iterLines(): AsyncGenerator<string, void, unknown> {\n const lineDecoder = new LineDecoder();\n\n const iter = ReadableStreamToAsyncIterable<Bytes>(readableStream);\n for await (const chunk of iter) {\n for (const line of lineDecoder.decode(chunk)) {\n yield line;\n }\n }\n\n for (const line of lineDecoder.flush()) {\n yield line;\n }\n }\n\n async function* iterator(): AsyncIterator<Item, any, undefined> {\n if (consumed) {\n throw new Error('Cannot iterate over a consumed stream, use `.tee()` to split the stream.');\n }\n consumed = true;\n let done = false;\n try {\n for await (const line of iterLines()) {\n if (done) continue;\n if (line) yield JSON.parse(line);\n }\n done = true;\n } catch (e) {\n // If the user calls `stream.controller.abort()`, we should exit without throwing.\n if (e instanceof Error && e.name === 'AbortError') return;\n throw e;\n } finally {\n // If the user `break`s, abort the ongoing request.\n if (!done) controller.abort();\n }\n }\n\n return new Stream(iterator, controller);\n }\n\n [Symbol.asyncIterator](): AsyncIterator<Item> {\n return this.iterator();\n }\n\n /**\n * Splits the stream into two streams which can be\n * independently read from at different speeds.\n */\n tee(): [Stream<Item>, Stream<Item>] {\n const left: Array<Promise<IteratorResult<Item>>> = [];\n const right: Array<Promise<IteratorResult<Item>>> = [];\n const iterator = this.iterator();\n\n const teeIterator = (queue: Array<Promise<IteratorResult<Item>>>): AsyncIterator<Item> => {\n return {\n next: () => {\n if (queue.length === 0) {\n const result = iterator.next();\n left.push(result);\n right.push(result);\n }\n return queue.shift()!;\n },\n };\n };\n\n return [\n new Stream(() => teeIterator(left), this.controller),\n new Stream(() => teeIterator(right), this.controller),\n ];\n }\n\n /**\n * Converts this stream to a newline-separated ReadableStream of\n * JSON stringified values in the stream\n * which can be turned back into a Stream with `Stream.fromReadableStream()`.\n */\n toReadableStream(): ReadableStream {\n const self = this;\n let iter: AsyncIterator<Item>;\n const encoder = new TextEncoder();\n\n return new ReadableStream({\n async start() {\n iter = self[Symbol.asyncIterator]();\n },\n async pull(ctrl: any) {\n try {\n const { value, done } = await iter.next();\n if (done) return ctrl.close();\n\n const bytes = encoder.encode(JSON.stringify(value) + '\\n');\n\n ctrl.enqueue(bytes);\n } catch (err) {\n ctrl.error(err);\n }\n },\n async cancel() {\n await iter.return?.();\n },\n });\n }\n}\n\nexport async function* _iterSSEMessages(\n response: Response,\n controller: AbortController,\n): AsyncGenerator<ServerSentEvent, void, unknown> {\n if (!response.body) {\n controller.abort();\n throw new AnthropicError(`Attempted to iterate over a response with no body`);\n }\n\n const sseDecoder = new SSEDecoder();\n const lineDecoder = new LineDecoder();\n\n const iter = ReadableStreamToAsyncIterable<Bytes>(response.body);\n for await (const sseChunk of iterSSEChunks(iter)) {\n for (const line of lineDecoder.decode(sseChunk)) {\n const sse = sseDecoder.decode(line);\n if (sse) yield sse;\n }\n }\n\n for (const line of lineDecoder.flush()) {\n const sse = sseDecoder.decode(line);\n if (sse) yield sse;\n }\n}\n\n/**\n * Given an async iterable iterator, iterates over it and yields full\n * SSE chunks, i.e. yields when a double new-line is encountered.\n */\nasync function* iterSSEChunks(iterator: AsyncIterableIterator<Bytes>): AsyncGenerator<Uint8Array> {\n let data = new Uint8Array();\n\n for await (const chunk of iterator) {\n if (chunk == null) {\n continue;\n }\n\n const binaryChunk =\n chunk instanceof ArrayBuffer ? new Uint8Array(chunk)\n : typeof chunk === 'string' ? new TextEncoder().encode(chunk)\n : chunk;\n\n let newData = new Uint8Array(data.length + binaryChunk.length);\n newData.set(data);\n newData.set(binaryChunk, data.length);\n data = newData;\n\n let patternIndex;\n while ((patternIndex = findDoubleNewlineIndex(data)) !== -1) {\n yield data.slice(0, patternIndex);\n data = data.slice(patternIndex);\n }\n }\n\n if (data.length > 0) {\n yield data;\n }\n}\n\nclass SSEDecoder {\n private data: string[];\n private event: string | null;\n private chunks: string[];\n\n constructor() {\n this.event = null;\n this.data = [];\n this.chunks = [];\n }\n\n decode(line: string) {\n if (line.endsWith('\\r')) {\n line = line.substring(0, line.length - 1);\n }\n\n if (!line) {\n // empty line and we didn't previously encounter any messages\n if (!this.event && !this.data.length) return null;\n\n const sse: ServerSentEvent = {\n event: this.event,\n data: this.data.join('\\n'),\n raw: this.chunks,\n };\n\n this.event = null;\n this.data = [];\n this.chunks = [];\n\n return sse;\n }\n\n this.chunks.push(line);\n\n if (line.startsWith(':')) {\n return null;\n }\n\n let [fieldname, _, value] = partition(line, ':');\n\n if (value.startsWith(' ')) {\n value = value.substring(1);\n }\n\n if (fieldname === 'event') {\n this.event = value;\n } else if (fieldname === 'data') {\n this.data.push(value);\n }\n\n return null;\n }\n}\n\nfunction partition(str: string, delimiter: string): [string, string, string] {\n const index = str.indexOf(delimiter);\n if (index !== -1) {\n return [str.substring(0, index), delimiter, str.substring(index + delimiter.length)];\n }\n\n return [str, '', ''];\n}\n", "import { type RequestOptions } from \"./core.js\";\nimport {\n FormData,\n File,\n type Blob,\n type FilePropertyBag,\n getMultipartRequestOptions,\n type FsReadStream,\n isFsReadStream,\n} from \"./_shims/index.js\";\nimport { MultipartBody } from \"./_shims/MultipartBody.js\";\nexport { fileFromPath } from \"./_shims/index.js\";\n\ntype BlobLikePart = string | ArrayBuffer | ArrayBufferView | BlobLike | Uint8Array | DataView;\nexport type BlobPart = string | ArrayBuffer | ArrayBufferView | Blob | Uint8Array | DataView;\n\n/**\n * Typically, this is a native \"File\" class.\n *\n * We provide the {@link toFile} utility to convert a variety of objects\n * into the File class.\n *\n * For convenience, you can also pass a fetch Response, or in Node,\n * the result of fs.createReadStream().\n */\nexport type Uploadable = FileLike | ResponseLike | FsReadStream;\n\n/**\n * Intended to match web.Blob, node.Blob, node-fetch.Blob, etc.\n */\nexport interface BlobLike {\n /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/size) */\n readonly size: number;\n /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/type) */\n readonly type: string;\n /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/text) */\n text(): Promise<string>;\n /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/slice) */\n slice(start?: number, end?: number): BlobLike;\n // unfortunately @types/node-fetch@^2.6.4 doesn't type the arrayBuffer method\n}\n\n/**\n * Intended to match web.File, node.File, node-fetch.File, etc.\n */\nexport interface FileLike extends BlobLike {\n /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/lastModified) */\n readonly lastModified: number;\n /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/name) */\n readonly name: string;\n}\n\n/**\n * Intended to match web.Response, node.Response, node-fetch.Response, etc.\n */\nexport interface ResponseLike {\n url: string;\n blob(): Promise<BlobLike>;\n}\n\nexport const isResponseLike = (value: any): value is ResponseLike =>\n value != null &&\n typeof value === 'object' &&\n typeof value.url === 'string' &&\n typeof value.blob === 'function';\n\nexport const isFileLike = (value: any): value is FileLike =>\n value != null &&\n typeof value === 'object' &&\n typeof value.name === 'string' &&\n typeof value.lastModified === 'number' &&\n isBlobLike(value);\n\n/**\n * The BlobLike type omits arrayBuffer() because @types/node-fetch@^2.6.4 lacks it; but this check\n * adds the arrayBuffer() method type because it is available and used at runtime\n */\nexport const isBlobLike = (value: any): value is BlobLike & { arrayBuffer(): Promise<ArrayBuffer> } =>\n value != null &&\n typeof value === 'object' &&\n typeof value.size === 'number' &&\n typeof value.type === 'string' &&\n typeof value.text === 'function' &&\n typeof value.slice === 'function' &&\n typeof value.arrayBuffer === 'function';\n\nexport const isUploadable = (value: any): value is Uploadable => {\n return isFileLike(value) || isResponseLike(value) || isFsReadStream(value);\n};\n\nexport type ToFileInput = Uploadable | Exclude<BlobLikePart, string> | AsyncIterable<BlobLikePart>;\n\n/**\n * Helper for creating a {@link File} to pass to an SDK upload method from a variety of different data formats\n * @param value the raw content of the file. Can be an {@link Uploadable}, {@link BlobLikePart}, or {@link AsyncIterable} of {@link BlobLikePart}s\n * @param {string=} name the name of the file. If omitted, toFile will try to determine a file name from bits if possible\n * @param {Object=} options additional properties\n * @param {string=} options.type the MIME type of the content\n * @param {number=} options.lastModified the last modified timestamp\n * @returns a {@link File} with the given properties\n */\nexport async function toFile(\n value: ToFileInput | PromiseLike<ToFileInput>,\n name?: string | null | undefined,\n options?: FilePropertyBag | undefined,\n): Promise<FileLike> {\n // If it's a promise, resolve it.\n value = await value;\n\n // If we've been given a `File` we don't need to do anything\n if (isFileLike(value)) {\n return value;\n }\n\n if (isResponseLike(value)) {\n const blob = await value.blob();\n name ||= new URL(value.url).pathname.split(/[\\\\/]/).pop() ?? 'unknown_file';\n\n // we need to convert the `Blob` into an array buffer because the `Blob` class\n // that `node-fetch` defines is incompatible with the web standard which results\n // in `new File` interpreting it as a string instead of binary data.\n const data = isBlobLike(blob) ? [(await blob.arrayBuffer()) as any] : [blob];\n\n return new File(data, name, options);\n }\n\n const bits = await getBytes(value);\n\n name ||= getName(value) ?? 'unknown_file';\n\n if (!options?.type) {\n const type = (bits[0] as any)?.type;\n if (typeof type === 'string') {\n options = { ...options, type };\n }\n }\n\n return new File(bits, name, options);\n}\n\nasync function getBytes(value: ToFileInput): Promise<Array<BlobPart>> {\n let parts: Array<BlobPart> = [];\n if (\n typeof value === 'string' ||\n ArrayBuffer.isView(value) || // includes Uint8Array, Buffer, etc.\n value instanceof ArrayBuffer\n ) {\n parts.push(value);\n } else if (isBlobLike(value)) {\n parts.push(await value.arrayBuffer());\n } else if (\n isAsyncIterableIterator(value) // includes Readable, ReadableStream, etc.\n ) {\n for await (const chunk of value) {\n parts.push(chunk as BlobPart); // TODO, consider validating?\n }\n } else {\n throw new Error(\n `Unexpected data type: ${typeof value}; constructor: ${value?.constructor\n ?.name}; props: ${propsForError(value)}`,\n );\n }\n\n return parts;\n}\n\nfunction propsForError(value: any): string {\n const props = Object.getOwnPropertyNames(value);\n return `[${props.map((p) => `\"${p}\"`).join(', ')}]`;\n}\n\nfunction getName(value: any): string | undefined {\n return (\n getStringFromMaybeBuffer(value.name) ||\n getStringFromMaybeBuffer(value.filename) ||\n // For fs.ReadStream\n getStringFromMaybeBuffer(value.path)?.split(/[\\\\/]/).pop()\n );\n}\n\nconst getStringFromMaybeBuffer = (x: string | Buffer | unknown): string | undefined => {\n if (typeof x === 'string') return x;\n if (typeof Buffer !== 'undefined' && x instanceof Buffer) return String(x);\n return undefined;\n};\n\nconst isAsyncIterableIterator = (value: any): value is AsyncIterableIterator<unknown> =>\n value != null && typeof value === 'object' && typeof value[Symbol.asyncIterator] === 'function';\n\nexport const isMultipartBody = (body: any): body is MultipartBody =>\n body && typeof body === 'object' && body.body && body[Symbol.toStringTag] === 'MultipartBody';\n\n/**\n * Returns a multipart/form-data request if any part of the given request body contains a File / Blob value.\n * Otherwise returns the request as is.\n */\nexport const maybeMultipartFormRequestOptions = async <T = Record<string, unknown>>(\n opts: RequestOptions<T>,\n): Promise<RequestOptions<T | MultipartBody>> => {\n if (!hasUploadableValue(opts.body)) return opts;\n\n const form = await createForm(opts.body);\n return getMultipartRequestOptions(form, opts);\n};\n\nexport const multipartFormRequestOptions = async <T = Record<string, unknown>>(\n opts: RequestOptions<T>,\n): Promise<RequestOptions<T | MultipartBody>> => {\n const form = await createForm(opts.body);\n return getMultipartRequestOptions(form, opts);\n};\n\nexport const createForm = async <T = Record<string, unknown>>(body: T | undefined): Promise<FormData> => {\n const form = new FormData();\n await Promise.all(Object.entries(body || {}).map(([key, value]) => addFormValue(form, key, value)));\n return form;\n};\n\nconst hasUploadableValue = (value: unknown): boolean => {\n if (isUploadable(value)) return true;\n if (Array.isArray(value)) return value.some(hasUploadableValue);\n if (value && typeof value === 'object') {\n for (const k in value) {\n if (hasUploadableValue((value as any)[k])) return true;\n }\n }\n return false;\n};\n\nconst addFormValue = async (form: FormData, key: string, value: unknown): Promise<void> => {\n if (value === undefined) return;\n if (value == null) {\n throw new TypeError(\n `Received null for \"${key}\"; to pass null in FormData, you must use the string 'null'`,\n );\n }\n\n // TODO: make nested formats configurable\n if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {\n form.append(key, String(value));\n } else if (isUploadable(value)) {\n const file = await toFile(value);\n form.append(key, file as File);\n } else if (Array.isArray(value)) {\n await Promise.all(value.map((entry) => addFormValue(form, key + '[]', entry)));\n } else if (typeof value === 'object') {\n await Promise.all(\n Object.entries(value).map(([name, prop]) => addFormValue(form, `${key}[${name}]`, prop)),\n );\n } else {\n throw new TypeError(\n `Invalid value given to form, expected a string, number, boolean, object, Array, File or Blob but got ${value} instead`,\n );\n }\n};\n", "import { VERSION } from \"./version.js\";\nimport { Stream } from \"./streaming.js\";\nimport {\n AnthropicError,\n APIError,\n APIConnectionError,\n APIConnectionTimeoutError,\n APIUserAbortError,\n} from \"./error.js\";\nimport {\n kind as shimsKind,\n type Readable,\n getDefaultAgent,\n type Agent,\n fetch,\n type RequestInfo,\n type RequestInit,\n type Response,\n type HeadersInit,\n} from \"./_shims/index.js\";\nexport { type Response };\nimport { BlobLike, isBlobLike, isMultipartBody } from \"./uploads.js\";\nexport {\n maybeMultipartFormRequestOptions,\n multipartFormRequestOptions,\n createForm,\n type Uploadable,\n} from \"./uploads.js\";\n\nexport type Fetch = (url: RequestInfo, init?: RequestInit) => Promise<Response>;\n\ntype PromiseOrValue<T> = T | Promise<T>;\n\ntype APIResponseProps = {\n response: Response;\n options: FinalRequestOptions;\n controller: AbortController;\n};\n\nasync function defaultParseResponse<T>(props: APIResponseProps): Promise<WithRequestID<T>> {\n const { response } = props;\n if (props.options.stream) {\n debug('response', response.status, response.url, response.headers, response.body);\n\n // Note: there is an invariant here that isn't represented in the type system\n // that if you set `stream: true` the response type must also be `Stream<T>`\n\n if (props.options.__streamClass) {\n return props.options.__streamClass.fromSSEResponse(response, props.controller) as any;\n }\n\n return Stream.fromSSEResponse(response, props.controller) as any;\n }\n\n // fetch refuses to read the body when the status code is 204.\n if (response.status === 204) {\n return null as WithRequestID<T>;\n }\n\n if (props.options.__binaryResponse) {\n return response as unknown as WithRequestID<T>;\n }\n\n const contentType = response.headers.get('content-type');\n const isJSON =\n contentType?.includes('application/json') || contentType?.includes('application/vnd.api+json');\n if (isJSON) {\n const json = await response.json();\n\n debug('response', response.status, response.url, response.headers, json);\n\n return _addRequestID(json as T, response);\n }\n\n const text = await response.text();\n debug('response', response.status, response.url, response.headers, text);\n\n // TODO handle blob, arraybuffer, other content types, etc.\n return text as unknown as WithRequestID<T>;\n}\n\ntype WithRequestID<T> =\n T extends Array<any> | Response | AbstractPage<any> ? T\n : T extends Record<string, any> ? T & { _request_id?: string | null }\n : T;\n\nfunction _addRequestID<T>(value: T, response: Response): WithRequestID<T> {\n if (!value || typeof value !== 'object' || Array.isArray(value)) {\n return value as WithRequestID<T>;\n }\n\n return Object.defineProperty(value, '_request_id', {\n value: response.headers.get('request-id'),\n enumerable: false,\n }) as WithRequestID<T>;\n}\n\n/**\n * A subclass of `Promise` providing additional helper methods\n * for interacting with the SDK.\n */\nexport class APIPromise<T> extends Promise<WithRequestID<T>> {\n private parsedPromise: Promise<WithRequestID<T>> | undefined;\n\n constructor(\n private responsePromise: Promise<APIResponseProps>,\n private parseResponse: (\n props: APIResponseProps,\n ) => PromiseOrValue<WithRequestID<T>> = defaultParseResponse,\n ) {\n super((resolve) => {\n // this is maybe a bit weird but this has to be a no-op to not implicitly\n // parse the response body; instead .then, .catch, .finally are overridden\n // to parse the response\n resolve(null as any);\n });\n }\n\n _thenUnwrap<U>(transform: (data: T, props: APIResponseProps) => U): APIPromise<U> {\n return new APIPromise(this.responsePromise, async (props) =>\n _addRequestID(transform(await this.parseResponse(props), props), props.response),\n );\n }\n\n /**\n * Gets the raw `Response` instance instead of parsing the response\n * data.\n *\n * If you want to parse the response body but still get the `Response`\n * instance, you can use {@link withResponse()}.\n *\n * \uD83D\uDC4B Getting the wrong TypeScript type for `Response`?\n * Try setting `\"moduleResolution\": \"NodeNext\"` if you can,\n * or add one of these imports before your first `import \u2026 from '@anthropic-ai/sdk'`:\n * - `import '@anthropic-ai/sdk/shims/node'` (if you're running on Node)\n * - `import '@anthropic-ai/sdk/shims/web'` (otherwise)\n */\n asResponse(): Promise<Response> {\n return this.responsePromise.then((p) => p.response);\n }\n\n /**\n * Gets the parsed response data, the raw `Response` instance and the ID of the request,\n * returned vie the `request-id` header which is useful for debugging requests and resporting\n * issues to Anthropic.\n *\n * If you just want to get the raw `Response` instance without parsing it,\n * you can use {@link asResponse()}.\n *\n * \uD83D\uDC4B Getting the wrong TypeScript type for `Response`?\n * Try setting `\"moduleResolution\": \"NodeNext\"` if you can,\n * or add one of these imports before your first `import \u2026 from '@anthropic-ai/sdk'`:\n * - `import '@anthropic-ai/sdk/shims/node'` (if you're running on Node)\n * - `import '@anthropic-ai/sdk/shims/web'` (otherwise)\n */\n async withResponse(): Promise<{ data: T; response: Response; request_id: string | null | undefined }> {\n const [data, response] = await Promise.all([this.parse(), this.asResponse()]);\n return { data, response, request_id: response.headers.get('request-id') };\n }\n\n private parse(): Promise<WithRequestID<T>> {\n if (!this.parsedPromise) {\n this.parsedPromise = this.responsePromise.then(this.parseResponse) as any as Promise<WithRequestID<T>>;\n }\n return this.parsedPromise;\n }\n\n override then<TResult1 = WithRequestID<T>, TResult2 = never>(\n onfulfilled?: ((value: WithRequestID<T>) => TResult1 | PromiseLike<TResult1>) | undefined | null,\n onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null,\n ): Promise<TResult1 | TResult2> {\n return this.parse().then(onfulfilled, onrejected);\n }\n\n override catch<TResult = never>(\n onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null,\n ): Promise<WithRequestID<T> | TResult> {\n return this.parse().catch(onrejected);\n }\n\n override finally(onfinally?: (() => void) | undefined | null): Promise<WithRequestID<T>> {\n return this.parse().finally(onfinally);\n }\n}\n\nexport abstract class APIClient {\n baseURL: string;\n maxRetries: number;\n timeout: number;\n httpAgent: Agent | undefined;\n\n private fetch: Fetch;\n protected idempotencyHeader?: string;\n\n constructor({\n baseURL,\n maxRetries = 2,\n timeout = 600000, // 10 minutes\n httpAgent,\n fetch: overriddenFetch,\n }: {\n baseURL: string;\n maxRetries?: number | undefined;\n timeout: number | undefined;\n httpAgent: Agent | undefined;\n fetch: Fetch | undefined;\n }) {\n this.baseURL = baseURL;\n this.maxRetries = validatePositiveInteger('maxRetries', maxRetries);\n this.timeout = validatePositiveInteger('timeout', timeout);\n this.httpAgent = httpAgent;\n\n this.fetch = overriddenFetch ?? fetch;\n }\n\n protected authHeaders(opts: FinalRequestOptions): Headers {\n return {};\n }\n\n /**\n * Override this to add your own default headers, for example:\n *\n * {\n * ...super.defaultHeaders(),\n * Authorization: 'Bearer 123',\n * }\n */\n protected defaultHeaders(opts: FinalRequestOptions): Headers {\n return {\n Accept: 'application/json',\n 'Content-Type': 'application/json',\n 'User-Agent': this.getUserAgent(),\n ...getPlatformHeaders(),\n ...this.authHeaders(opts),\n };\n }\n\n protected abstract defaultQuery(): DefaultQuery | undefined;\n\n /**\n * Override this to add your own headers validation:\n */\n protected validateHeaders(headers: Headers, customHeaders: Headers) {}\n\n protected defaultIdempotencyKey(): string {\n return `stainless-node-retry-${uuid4()}`;\n }\n\n get<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {\n return this.methodRequest('get', path, opts);\n }\n\n post<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {\n return this.methodRequest('post', path, opts);\n }\n\n patch<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {\n return this.methodRequest('patch', path, opts);\n }\n\n put<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {\n return this.methodRequest('put', path, opts);\n }\n\n delete<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {\n return this.methodRequest('delete', path, opts);\n }\n\n private methodRequest<Req, Rsp>(\n method: HTTPMethod,\n path: string,\n opts?: PromiseOrValue<RequestOptions<Req>>,\n ): APIPromise<Rsp> {\n return this.request(\n Promise.resolve(opts).then(async (opts) => {\n const body =\n opts && isBlobLike(opts?.body) ? new DataView(await opts.body.arrayBuffer())\n : opts?.body instanceof DataView ? opts.body\n : opts?.body instanceof ArrayBuffer ? new DataView(opts.body)\n : opts && ArrayBuffer.isView(opts?.body) ? new DataView(opts.body.buffer)\n : opts?.body;\n return { method, path, ...opts, body };\n }),\n );\n }\n\n getAPIList<Item, PageClass extends AbstractPage<Item> = AbstractPage<Item>>(\n path: string,\n Page: new (...args: any[]) => PageClass,\n opts?: RequestOptions<any>,\n ): PagePromise<PageClass, Item> {\n return this.requestAPIList(Page, { method: 'get', path, ...opts });\n }\n\n private calculateContentLength(body: unknown): string | null {\n if (typeof body === 'string') {\n if (typeof Buffer !== 'undefined') {\n return Buffer.byteLength(body, 'utf8').toString();\n }\n\n if (typeof TextEncoder !== 'undefined') {\n const encoder = new TextEncoder();\n const encoded = encoder.encode(body);\n return encoded.length.toString();\n }\n } else if (ArrayBuffer.isView(body)) {\n return body.byteLength.toString();\n }\n\n return null;\n }\n\n buildRequest<Req>(\n options: FinalRequestOptions<Req>,\n { retryCount = 0 }: { retryCount?: number } = {},\n ): { req: RequestInit; url: string; timeout: number } {\n options = { ...options };\n const { method, path, query, headers: headers = {} } = options;\n\n const body =\n ArrayBuffer.isView(options.body) || (options.__binaryRequest && typeof options.body === 'string') ?\n options.body\n : isMultipartBody(options.body) ? options.body.body\n : options.body ? JSON.stringify(options.body, null, 2)\n : null;\n const contentLength = this.calculateContentLength(body);\n\n const url = this.buildURL(path!, query);\n if ('timeout' in options) validatePositiveInteger('timeout', options.timeout);\n options.timeout = options.timeout ?? this.timeout;\n const httpAgent = options.httpAgent ?? this.httpAgent ?? getDefaultAgent(url);\n const minAgentTimeout = options.timeout + 1000;\n if (\n typeof (httpAgent as any)?.options?.timeout === 'number' &&\n minAgentTimeout > ((httpAgent as any).options.timeout ?? 0)\n ) {\n // Allow any given request to bump our agent active socket timeout.\n // This may seem strange, but leaking active sockets should be rare and not particularly problematic,\n // and without mutating agent we would need to create more of them.\n // This tradeoff optimizes for performance.\n (httpAgent as any).options.timeout = minAgentTimeout;\n }\n\n if (this.idempotencyHeader && method !== 'get') {\n if (!options.idempotencyKey) options.idempotencyKey = this.defaultIdempotencyKey();\n headers[this.idempotencyHeader] = options.idempotencyKey;\n }\n\n const reqHeaders = this.buildHeaders({ options, headers, contentLength, retryCount });\n\n const req: RequestInit = {\n method,\n ...(body && { body: body as any }),\n headers: reqHeaders,\n ...(httpAgent && { agent: httpAgent }),\n // @ts-ignore node-fetch uses a custom AbortSignal type that is\n // not compatible with standard web types\n signal: options.signal ?? null,\n };\n\n return { req, url, timeout: options.timeout };\n }\n\n private buildHeaders({\n options,\n headers,\n contentLength,\n retryCount,\n }: {\n options: FinalRequestOptions;\n headers: Record<string, string | null | undefined>;\n contentLength: string | null | undefined;\n retryCount: number;\n }): Record<string, string> {\n const reqHeaders: Record<string, string> = {};\n if (contentLength) {\n reqHeaders['content-length'] = contentLength;\n }\n\n const defaultHeaders = this.defaultHeaders(options);\n applyHeadersMut(reqHeaders, defaultHeaders);\n applyHeadersMut(reqHeaders, headers);\n\n // let builtin fetch set the Content-Type for multipart bodies\n if (isMultipartBody(options.body) && shimsKind !== 'node') {\n delete reqHeaders['content-type'];\n }\n\n // Don't set theses headers if they were already set or removed through default headers or by the caller.\n // We check `defaultHeaders` and `headers`, which can contain nulls, instead of `reqHeaders` to account\n // for the removal case.\n if (\n getHeader(defaultHeaders, 'x-stainless-retry-count') === undefined &&\n getHeader(headers, 'x-stainless-retry-count') === undefined\n ) {\n reqHeaders['x-stainless-retry-count'] = String(retryCount);\n }\n if (\n getHeader(defaultHeaders, 'x-stainless-timeout') === undefined &&\n getHeader(headers, 'x-stainless-timeout') === undefined &&\n options.timeout\n ) {\n reqHeaders['x-stainless-timeout'] = String(options.timeout);\n }\n\n this.validateHeaders(reqHeaders, headers);\n\n return reqHeaders;\n }\n\n _calculateNonstreamingTimeout(maxTokens: number): number {\n const defaultTimeout = 10 * 60;\n const expectedTimeout = (60 * 60 * maxTokens) / 128_000;\n if (expectedTimeout > defaultTimeout) {\n throw new AnthropicError(\n 'Streaming is strongly recommended for operations that may take longer than 10 minutes. ' +\n 'See https://github.com/anthropics/anthropic-sdk-python#streaming-responses for more details',\n );\n }\n return defaultTimeout * 1000;\n }\n\n /**\n * Used as a callback for mutating the given `FinalRequestOptions` object.\n */\n protected async prepareOptions(options: FinalRequestOptions): Promise<void> {}\n\n /**\n * Used as a callback for mutating the given `RequestInit` object.\n *\n * This is useful for cases where you want to add certain headers based off of\n * the request properties, e.g. `method` or `url`.\n */\n protected async prepareRequest(\n request: RequestInit,\n { url, options }: { url: string; options: FinalRequestOptions },\n ): Promise<void> {}\n\n protected parseHeaders(headers: HeadersInit | null | undefined): Record<string, string> {\n return (\n !headers ? {}\n : Symbol.iterator in headers ?\n Object.fromEntries(Array.from(headers as Iterable<string[]>).map((header) => [...header]))\n : { ...headers }\n );\n }\n\n protected makeStatusError(\n status: number | undefined,\n error: Object | undefined,\n message: string | undefined,\n headers: Headers | undefined,\n ): APIError {\n return APIError.generate(status, error, message, headers);\n }\n\n request<Req, Rsp>(\n options: PromiseOrValue<FinalRequestOptions<Req>>,\n remainingRetries: number | null = null,\n ): APIPromise<Rsp> {\n return new APIPromise(this.makeRequest(options, remainingRetries));\n }\n\n private async makeRequest<Req>(\n optionsInput: PromiseOrValue<FinalRequestOptions<Req>>,\n retriesRemaining: number | null,\n ): Promise<APIResponseProps> {\n const options = await optionsInput;\n const maxRetries = options.maxRetries ?? this.maxRetries;\n if (retriesRemaining == null) {\n retriesRemaining = maxRetries;\n }\n\n await this.prepareOptions(options);\n\n const { req, url, timeout } = this.buildRequest(options, { retryCount: maxRetries - retriesRemaining });\n\n await this.prepareRequest(req, { url, options });\n\n debug('request', url, options, req.headers);\n\n if (options.signal?.aborted) {\n throw new APIUserAbortError();\n }\n\n const controller = new AbortController();\n const response = await this.fetchWithTimeout(url, req, timeout, controller).catch(castToError);\n\n if (response instanceof Error) {\n if (options.signal?.aborted) {\n throw new APIUserAbortError();\n }\n if (retriesRemaining) {\n return this.retryRequest(options, retriesRemaining);\n }\n if (response.name === 'AbortError') {\n throw new APIConnectionTimeoutError();\n }\n throw new APIConnectionError({ cause: response });\n }\n\n const responseHeaders = createResponseHeaders(response.headers);\n\n if (!response.ok) {\n if (retriesRemaining && this.shouldRetry(response)) {\n const retryMessage = `retrying, ${retriesRemaining} attempts remaining`;\n debug(`response (error; ${retryMessage})`, response.status, url, responseHeaders);\n return this.retryRequest(options, retriesRemaining, responseHeaders);\n }\n\n const errText = await response.text().catch((e) => castToError(e).message);\n const errJSON = safeJSON(errText);\n const errMessage = errJSON ? undefined : errText;\n const retryMessage = retriesRemaining ? `(error; no more retries left)` : `(error; not retryable)`;\n\n debug(`response (error; ${retryMessage})`, response.status, url, responseHeaders, errMessage);\n\n const err = this.makeStatusError(response.status, errJSON, errMessage, responseHeaders);\n throw err;\n }\n\n return { response, options, controller };\n }\n\n requestAPIList<Item = unknown, PageClass extends AbstractPage<Item> = AbstractPage<Item>>(\n Page: new (...args: ConstructorParameters<typeof AbstractPage>) => PageClass,\n options: FinalRequestOptions,\n ): PagePromise<PageClass, Item> {\n const request = this.makeRequest(options, null);\n return new PagePromise<PageClass, Item>(this, request, Page);\n }\n\n buildURL<Req>(path: string, query: Req | null | undefined): string {\n const url =\n isAbsoluteURL(path) ?\n new URL(path)\n : new URL(this.baseURL + (this.baseURL.endsWith('/') && path.startsWith('/') ? path.slice(1) : path));\n\n const defaultQuery = this.defaultQuery();\n if (!isEmptyObj(defaultQuery)) {\n query = { ...defaultQuery, ...query } as Req;\n }\n\n if (typeof query === 'object' && query && !Array.isArray(query)) {\n url.search = this.stringifyQuery(query as Record<string, unknown>);\n }\n\n return url.toString();\n }\n\n protected stringifyQuery(query: Record<string, unknown>): string {\n return Object.entries(query)\n .filter(([_, value]) => typeof value !== 'undefined')\n .map(([key, value]) => {\n if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {\n return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;\n }\n if (value === null) {\n return `${encodeURIComponent(key)}=`;\n }\n throw new AnthropicError(\n `Cannot stringify type ${typeof value}; Expected string, number, boolean, or null. If you need to pass nested query parameters, you can manually encode them, e.g. { query: { 'foo[key1]': value1, 'foo[key2]': value2 } }, and please open a GitHub issue requesting better support for your use case.`,\n );\n })\n .join('&');\n }\n\n async fetchWithTimeout(\n url: RequestInfo,\n init: RequestInit | undefined,\n ms: number,\n controller: AbortController,\n ): Promise<Response> {\n const { signal, ...options } = init || {};\n if (signal) signal.addEventListener('abort', () => controller.abort());\n\n const timeout = setTimeout(() => controller.abort(), ms);\n\n const fetchOptions = {\n signal: controller.signal as any,\n ...options,\n };\n if (fetchOptions.method) {\n // Custom methods like 'patch' need to be uppercased\n // See https://github.com/nodejs/undici/issues/2294\n fetchOptions.method = fetchOptions.method.toUpperCase();\n }\n\n // turn on TCP keep-alive for the sockets, if the runtime supports it\n const socketKeepAliveInterval = 60 * 1000;\n const keepAliveTimeout = setTimeout(() => {\n if (fetchOptions && (fetchOptions as any)?.agent?.sockets) {\n for (const socket of Object.values((fetchOptions as any)?.agent?.sockets).flat()) {\n if ((socket as any)?.setKeepAlive) {\n (socket as any).setKeepAlive(true, socketKeepAliveInterval);\n }\n }\n }\n }, socketKeepAliveInterval);\n\n return (\n // use undefined this binding; fetch errors if bound to something else in browser/cloudflare\n this.fetch.call(undefined, url, fetchOptions).finally(() => {\n clearTimeout(timeout);\n clearTimeout(keepAliveTimeout);\n })\n );\n }\n\n private shouldRetry(response: Response): boolean {\n // Note this is not a standard header.\n const shouldRetryHeader = response.headers.get('x-should-retry');\n\n // If the server explicitly says whether or not to retry, obey.\n if (shouldRetryHeader === 'true') return true;\n if (shouldRetryHeader === 'false') return false;\n\n // Retry on request timeouts.\n if (response.status === 408) return true;\n\n // Retry on lock timeouts.\n if (response.status === 409) return true;\n\n // Retry on rate limits.\n if (response.status === 429) return true;\n\n // Retry internal errors.\n if (response.status >= 500) return true;\n\n return false;\n }\n\n private async retryRequest(\n options: FinalRequestOptions,\n retriesRemaining: number,\n responseHeaders?: Headers | undefined,\n ): Promise<APIResponseProps> {\n let timeoutMillis: number | undefined;\n\n // Note the `retry-after-ms` header may not be standard, but is a good idea and we'd like proactive support for it.\n const retryAfterMillisHeader = responseHeaders?.['retry-after-ms'];\n if (retryAfterMillisHeader) {\n const timeoutMs = parseFloat(retryAfterMillisHeader);\n if (!Number.isNaN(timeoutMs)) {\n timeoutMillis = timeoutMs;\n }\n }\n\n // About the Retry-After header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After\n const retryAfterHeader = responseHeaders?.['retry-after'];\n if (retryAfterHeader && !timeoutMillis) {\n const timeoutSeconds = parseFloat(retryAfterHeader);\n if (!Number.isNaN(timeoutSeconds)) {\n timeoutMillis = timeoutSeconds * 1000;\n } else {\n timeoutMillis = Date.parse(retryAfterHeader) - Date.now();\n }\n }\n\n // If the API asks us to wait a certain amount of time (and it's a reasonable amount),\n // just do what it says, but otherwise calculate a default\n if (!(timeoutMillis && 0 <= timeoutMillis && timeoutMillis < 60 * 1000)) {\n const maxRetries = options.maxRetries ?? this.maxRetries;\n timeoutMillis = this.calculateDefaultRetryTimeoutMillis(retriesRemaining, maxRetries);\n }\n await sleep(timeoutMillis);\n\n return this.makeRequest(options, retriesRemaining - 1);\n }\n\n private calculateDefaultRetryTimeoutMillis(retriesRemaining: number, maxRetries: number): number {\n const initialRetryDelay = 0.5;\n const maxRetryDelay = 8.0;\n\n const numRetries = maxRetries - retriesRemaining;\n\n // Apply exponential backoff, but not more than the max.\n const sleepSeconds = Math.min(initialRetryDelay * Math.pow(2, numRetries), maxRetryDelay);\n\n // Apply some jitter, take up to at most 25 percent of the retry time.\n const jitter = 1 - Math.random() * 0.25;\n\n return sleepSeconds * jitter * 1000;\n }\n\n private getUserAgent(): string {\n return `${this.constructor.name}/JS ${VERSION}`;\n }\n}\n\nexport type PageInfo = { url: URL } | { params: Record<string, unknown> | null };\n\nexport abstract class AbstractPage<Item> implements AsyncIterable<Item> {\n #client: APIClient;\n protected options: FinalRequestOptions;\n\n protected response: Response;\n protected body: unknown;\n\n constructor(client: APIClient, response: Response, body: unknown, options: FinalRequestOptions) {\n this.#client = client;\n this.options = options;\n this.response = response;\n this.body = body;\n }\n\n /**\n * @deprecated Use nextPageInfo instead\n */\n abstract nextPageParams(): Partial<Record<string, unknown>> | null;\n abstract nextPageInfo(): PageInfo | null;\n\n abstract getPaginatedItems(): Item[];\n\n hasNextPage(): boolean {\n const items = this.getPaginatedItems();\n if (!items.length) return false;\n return this.nextPageInfo() != null;\n }\n\n async getNextPage(): Promise<this> {\n const nextInfo = this.nextPageInfo();\n if (!nextInfo) {\n throw new AnthropicError(\n 'No next page expected; please check `.hasNextPage()` before calling `.getNextPage()`.',\n );\n }\n const nextOptions = { ...this.options };\n if ('params' in nextInfo && typeof nextOptions.query === 'object') {\n nextOptions.query = { ...nextOptions.query, ...nextInfo.params };\n } else if ('url' in nextInfo) {\n const params = [...Object.entries(nextOptions.query || {}), ...nextInfo.url.searchParams.entries()];\n for (const [key, value] of params) {\n nextInfo.url.searchParams.set(key, value as any);\n }\n nextOptions.query = undefined;\n nextOptions.path = nextInfo.url.toString();\n }\n return await this.#client.requestAPIList(this.constructor as any, nextOptions);\n }\n\n async *iterPages(): AsyncGenerator<this> {\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n let page: this = this;\n yield page;\n while (page.hasNextPage()) {\n page = await page.getNextPage();\n yield page;\n }\n }\n\n async *[Symbol.asyncIterator](): AsyncGenerator<Item> {\n for await (const page of this.iterPages()) {\n for (const item of page.getPaginatedItems()) {\n yield item;\n }\n }\n }\n}\n\n/**\n * This subclass of Promise will resolve to an instantiated Page once the request completes.\n *\n * It also implements AsyncIterable to allow auto-paginating iteration on an unawaited list call, eg:\n *\n * for await (const item of client.items.list()) {\n * console.log(item)\n * }\n */\nexport class PagePromise<\n PageClass extends AbstractPage<Item>,\n Item = ReturnType<PageClass['getPaginatedItems']>[number],\n >\n extends APIPromise<PageClass>\n implements AsyncIterable<Item>\n{\n constructor(\n client: APIClient,\n request: Promise<APIResponseProps>,\n Page: new (...args: ConstructorParameters<typeof AbstractPage>) => PageClass,\n ) {\n super(\n request,\n async (props) =>\n new Page(\n client,\n props.response,\n await defaultParseResponse(props),\n props.options,\n ) as WithRequestID<PageClass>,\n );\n }\n\n /**\n * Allow auto-paginating iteration on an unawaited list call, eg:\n *\n * for await (const item of client.items.list()) {\n * console.log(item)\n * }\n */\n async *[Symbol.asyncIterator](): AsyncGenerator<Item> {\n const page = await this;\n for await (const item of page) {\n yield item;\n }\n }\n}\n\nexport const createResponseHeaders = (\n headers: Awaited<ReturnType<Fetch>>['headers'],\n): Record<string, string> => {\n return new Proxy(\n Object.fromEntries(\n // @ts-ignore\n headers.entries(),\n ),\n {\n get(target, name) {\n const key = name.toString();\n return target[key.toLowerCase()] || target[key];\n },\n },\n );\n};\n\ntype HTTPMethod = 'get' | 'post' | 'put' | 'patch' | 'delete';\n\nexport type RequestClient = { fetch: Fetch };\nexport type Headers = Record<string, string | null | undefined>;\nexport type DefaultQuery = Record<string, string | undefined>;\nexport type KeysEnum<T> = { [P in keyof Required<T>]: true };\n\nexport type RequestOptions<\n Req = unknown | Record<string, unknown> | Readable | BlobLike | ArrayBufferView | ArrayBuffer,\n> = {\n method?: HTTPMethod;\n path?: string;\n query?: Req | undefined;\n body?: Req | null | undefined;\n headers?: Headers | undefined;\n\n maxRetries?: number;\n stream?: boolean | undefined;\n timeout?: number;\n httpAgent?: Agent;\n signal?: AbortSignal | undefined | null;\n idempotencyKey?: string;\n\n __binaryRequest?: boolean | undefined;\n __binaryResponse?: boolean | undefined;\n __streamClass?: typeof Stream;\n};\n\n// This is required so that we can determine if a given object matches the RequestOptions\n// type at runtime. While this requires duplication, it is enforced by the TypeScript\n// compiler such that any missing / extraneous keys will cause an error.\nconst requestOptionsKeys: KeysEnum<RequestOptions> = {\n method: true,\n path: true,\n query: true,\n body: true,\n headers: true,\n\n maxRetries: true,\n stream: true,\n timeout: true,\n httpAgent: true,\n signal: true,\n idempotencyKey: true,\n\n __binaryRequest: true,\n __binaryResponse: true,\n __streamClass: true,\n};\n\nexport const isRequestOptions = (obj: unknown): obj is RequestOptions => {\n return (\n typeof obj === 'object' &&\n obj !== null &&\n !isEmptyObj(obj) &&\n Object.keys(obj).every((k) => hasOwn(requestOptionsKeys, k))\n );\n};\n\nexport type FinalRequestOptions<Req = unknown | Record<string, unknown> | Readable | DataView> =\n RequestOptions<Req> & {\n method: HTTPMethod;\n path: string;\n };\n\ndeclare const Deno: any;\ndeclare const EdgeRuntime: any;\ntype Arch = 'x32' | 'x64' | 'arm' | 'arm64' | `other:${string}` | 'unknown';\ntype PlatformName =\n | 'MacOS'\n | 'Linux'\n | 'Windows'\n | 'FreeBSD'\n | 'OpenBSD'\n | 'iOS'\n | 'Android'\n | `Other:${string}`\n | 'Unknown';\ntype Browser = 'ie' | 'edge' | 'chrome' | 'firefox' | 'safari';\ntype PlatformProperties = {\n 'X-Stainless-Lang': 'js';\n 'X-Stainless-Package-Version': string;\n 'X-Stainless-OS': PlatformName;\n 'X-Stainless-Arch': Arch;\n 'X-Stainless-Runtime': 'node' | 'deno' | 'edge' | `browser:${Browser}` | 'unknown';\n 'X-Stainless-Runtime-Version': string;\n};\nconst getPlatformProperties = (): PlatformProperties => {\n if (typeof Deno !== 'undefined' && Deno.build != null) {\n return {\n 'X-Stainless-Lang': 'js',\n 'X-Stainless-Package-Version': VERSION,\n 'X-Stainless-OS': normalizePlatform(Deno.build.os),\n 'X-Stainless-Arch': normalizeArch(Deno.build.arch),\n 'X-Stainless-Runtime': 'deno',\n 'X-Stainless-Runtime-Version':\n typeof Deno.version === 'string' ? Deno.version : Deno.version?.deno ?? 'unknown',\n };\n }\n if (typeof EdgeRuntime !== 'undefined') {\n return {\n 'X-Stainless-Lang': 'js',\n 'X-Stainless-Package-Version': VERSION,\n 'X-Stainless-OS': 'Unknown',\n 'X-Stainless-Arch': `other:${EdgeRuntime}`,\n 'X-Stainless-Runtime': 'edge',\n 'X-Stainless-Runtime-Version': process.version,\n };\n }\n // Check if Node.js\n if (Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]') {\n return {\n 'X-Stainless-Lang': 'js',\n 'X-Stainless-Package-Version': VERSION,\n 'X-Stainless-OS': normalizePlatform(process.platform),\n 'X-Stainless-Arch': normalizeArch(process.arch),\n 'X-Stainless-Runtime': 'node',\n 'X-Stainless-Runtime-Version': process.version,\n };\n }\n\n const browserInfo = getBrowserInfo();\n if (browserInfo) {\n return {\n 'X-Stainless-Lang': 'js',\n 'X-Stainless-Package-Version': VERSION,\n 'X-Stainless-OS': 'Unknown',\n 'X-Stainless-Arch': 'unknown',\n 'X-Stainless-Runtime': `browser:${browserInfo.browser}`,\n 'X-Stainless-Runtime-Version': browserInfo.version,\n };\n }\n\n // TODO add support for Cloudflare workers, etc.\n return {\n 'X-Stainless-Lang': 'js',\n 'X-Stainless-Package-Version': VERSION,\n 'X-Stainless-OS': 'Unknown',\n 'X-Stainless-Arch': 'unknown',\n 'X-Stainless-Runtime': 'unknown',\n 'X-Stainless-Runtime-Version': 'unknown',\n };\n};\n\ntype BrowserInfo = {\n browser: Browser;\n version: string;\n};\n\ndeclare const navigator: { userAgent: string } | undefined;\n\n// Note: modified from https://github.com/JS-DevTools/host-environment/blob/b1ab79ecde37db5d6e163c050e54fe7d287d7c92/src/isomorphic.browser.ts\nfunction getBrowserInfo(): BrowserInfo | null {\n if (typeof navigator === 'undefined' || !navigator) {\n return null;\n }\n\n // NOTE: The order matters here!\n const browserPatterns = [\n { key: 'edge' as const, pattern: /Edge(?:\\W+(\\d+)\\.(\\d+)(?:\\.(\\d+))?)?/ },\n { key: 'ie' as const, pattern: /MSIE(?:\\W+(\\d+)\\.(\\d+)(?:\\.(\\d+))?)?/ },\n { key: 'ie' as const, pattern: /Trident(?:.*rv\\:(\\d+)\\.(\\d+)(?:\\.(\\d+))?)?/ },\n { key: 'chrome' as const, pattern: /Chrome(?:\\W+(\\d+)\\.(\\d+)(?:\\.(\\d+))?)?/ },\n { key: 'firefox' as const, pattern: /Firefox(?:\\W+(\\d+)\\.(\\d+)(?:\\.(\\d+))?)?/ },\n { key: 'safari' as const, pattern: /(?:Version\\W+(\\d+)\\.(\\d+)(?:\\.(\\d+))?)?(?:\\W+Mobile\\S*)?\\W+Safari/ },\n ];\n\n // Find the FIRST matching browser\n for (const { key, pattern } of browserPatterns) {\n const match = pattern.exec(navigator.userAgent);\n if (match) {\n const major = match[1] || 0;\n const minor = match[2] || 0;\n const patch = match[3] || 0;\n\n return { browser: key, version: `${major}.${minor}.${patch}` };\n }\n }\n\n return null;\n}\n\nconst normalizeArch = (arch: string): Arch => {\n // Node docs:\n // - https://nodejs.org/api/process.html#processarch\n // Deno docs:\n // - https://doc.deno.land/deno/stable/~/Deno.build\n if (arch === 'x32') return 'x32';\n if (arch === 'x86_64' || arch === 'x64') return 'x64';\n if (arch === 'arm') return 'arm';\n if (arch === 'aarch64' || arch === 'arm64') return 'arm64';\n if (arch) return `other:${arch}`;\n return 'unknown';\n};\n\nconst normalizePlatform = (platform: string): PlatformName => {\n // Node platforms:\n // - https://nodejs.org/api/process.html#processplatform\n // Deno platforms:\n // - https://doc.deno.land/deno/stable/~/Deno.build\n // - https://github.com/denoland/deno/issues/14799\n\n platform = platform.toLowerCase();\n\n // NOTE: this iOS check is untested and may not work\n // Node does not work natively on IOS, there is a fork at\n // https://github.com/nodejs-mobile/nodejs-mobile\n // however it is unknown at the time of writing how to detect if it is running\n if (platform.includes('ios')) return 'iOS';\n if (platform === 'android') return 'Android';\n if (platform === 'darwin') return 'MacOS';\n if (platform === 'win32') return 'Windows';\n if (platform === 'freebsd') return 'FreeBSD';\n if (platform === 'openbsd') return 'OpenBSD';\n if (platform === 'linux') return 'Linux';\n if (platform) return `Other:${platform}`;\n return 'Unknown';\n};\n\nlet _platformHeaders: PlatformProperties;\nconst getPlatformHeaders = () => {\n return (_platformHeaders ??= getPlatformProperties());\n};\n\nexport const safeJSON = (text: string) => {\n try {\n return JSON.parse(text);\n } catch (err) {\n return undefined;\n }\n};\n\n// https://url.spec.whatwg.org/#url-scheme-string\nconst startsWithSchemeRegexp = /^[a-z][a-z0-9+.-]*:/i;\nconst isAbsoluteURL = (url: string): boolean => {\n return startsWithSchemeRegexp.test(url);\n};\n\nexport const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));\n\nconst validatePositiveInteger = (name: string, n: unknown): number => {\n if (typeof n !== 'number' || !Number.isInteger(n)) {\n throw new AnthropicError(`${name} must be an integer`);\n }\n if (n < 0) {\n throw new AnthropicError(`${name} must be a positive integer`);\n }\n return n;\n};\n\nexport const castToError = (err: any): Error => {\n if (err instanceof Error) return err;\n if (typeof err === 'object' && err !== null) {\n try {\n return new Error(JSON.stringify(err));\n } catch {}\n }\n return new Error(String(err));\n};\n\nexport const ensurePresent = <T>(value: T | null | undefined): T => {\n if (value == null) throw new AnthropicError(`Expected a value to be given but received ${value} instead.`);\n return value;\n};\n\n/**\n * Read an environment variable.\n *\n * Trims beginning and trailing whitespace.\n *\n * Will return undefined if the environment variable doesn't exist or cannot be accessed.\n */\nexport const readEnv = (env: string): string | undefined => {\n if (typeof process !== 'undefined') {\n return process.env?.[env]?.trim() ?? undefined;\n }\n if (typeof Deno !== 'undefined') {\n return Deno.env?.get?.(env)?.trim();\n }\n return undefined;\n};\n\nexport const coerceInteger = (value: unknown): number => {\n if (typeof value === 'number') return Math.round(value);\n if (typeof value === 'string') return parseInt(value, 10);\n\n throw new AnthropicError(`Could not coerce ${value} (type: ${typeof value}) into a number`);\n};\n\nexport const coerceFloat = (value: unknown): number => {\n if (typeof value === 'number') return value;\n if (typeof value === 'string') return parseFloat(value);\n\n throw new AnthropicError(`Could not coerce ${value} (type: ${typeof value}) into a number`);\n};\n\nexport const coerceBoolean = (value: unknown): boolean => {\n if (typeof value === 'boolean') return value;\n if (typeof value === 'string') return value === 'true';\n return Boolean(value);\n};\n\nexport const maybeCoerceInteger = (value: unknown): number | undefined => {\n if (value === undefined) {\n return undefined;\n }\n return coerceInteger(value);\n};\n\nexport const maybeCoerceFloat = (value: unknown): number | undefined => {\n if (value === undefined) {\n return undefined;\n }\n return coerceFloat(value);\n};\n\nexport const maybeCoerceBoolean = (value: unknown): boolean | undefined => {\n if (value === undefined) {\n return undefined;\n }\n return coerceBoolean(value);\n};\n\n// https://stackoverflow.com/a/34491287\nexport function isEmptyObj(obj: Object | null | undefined): boolean {\n if (!obj) return true;\n for (const _k in obj) return false;\n return true;\n}\n\n// https://eslint.org/docs/latest/rules/no-prototype-builtins\nexport function hasOwn(obj: Object, key: string): boolean {\n return Object.prototype.hasOwnProperty.call(obj, key);\n}\n\n/**\n * Copies headers from \"newHeaders\" onto \"targetHeaders\",\n * using lower-case for all properties,\n * ignoring any keys with undefined values,\n * and deleting any keys with null values.\n */\nfunction applyHeadersMut(targetHeaders: Headers, newHeaders: Headers): void {\n for (const k in newHeaders) {\n if (!hasOwn(newHeaders, k)) continue;\n const lowerKey = k.toLowerCase();\n if (!lowerKey) continue;\n\n const val = newHeaders[k];\n\n if (val === null) {\n delete targetHeaders[lowerKey];\n } else if (val !== undefined) {\n targetHeaders[lowerKey] = val;\n }\n }\n}\n\nexport function debug(action: string, ...args: any[]) {\n if (typeof process !== 'undefined' && process?.env?.['DEBUG'] === 'true') {\n console.log(`Anthropic:DEBUG:${action}`, ...args);\n }\n}\n\n/**\n * https://stackoverflow.com/a/2117523\n */\nconst uuid4 = () => {\n return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {\n const r = (Math.random() * 16) | 0;\n const v = c === 'x' ? r : (r & 0x3) | 0x8;\n return v.toString(16);\n });\n};\n\nexport const isRunningInBrowser = () => {\n return (\n // @ts-ignore\n typeof window !== 'undefined' &&\n // @ts-ignore\n typeof window.document !== 'undefined' &&\n // @ts-ignore\n typeof navigator !== 'undefined'\n );\n};\n\nexport interface HeadersProtocol {\n get: (header: string) => string | null | undefined;\n}\nexport type HeadersLike = Record<string, string | string[] | undefined> | HeadersProtocol;\n\nexport const isHeadersProtocol = (headers: any): headers is HeadersProtocol => {\n return typeof headers?.get === 'function';\n};\n\nexport const getRequiredHeader = (headers: HeadersLike | Headers, header: string): string => {\n const foundHeader = getHeader(headers, header);\n if (foundHeader === undefined) {\n throw new Error(`Could not find ${header} header`);\n }\n return foundHeader;\n};\n\nexport const getHeader = (headers: HeadersLike | Headers, header: string): string | undefined => {\n const lowerCasedHeader = header.toLowerCase();\n if (isHeadersProtocol(headers)) {\n // to deal with the case where the header looks like Stainless-Event-Id\n const intercapsHeader =\n header[0]?.toUpperCase() +\n header.substring(1).replace(/([^\\w])(\\w)/g, (_m, g1, g2) => g1 + g2.toUpperCase());\n for (const key of [header, lowerCasedHeader, header.toUpperCase(), intercapsHeader]) {\n const value = headers.get(key);\n if (value) {\n return value;\n }\n }\n }\n\n for (const [key, value] of Object.entries(headers)) {\n if (key.toLowerCase() === lowerCasedHeader) {\n if (Array.isArray(value)) {\n if (value.length <= 1) return value[0];\n console.warn(`Received ${value.length} entries for the ${header} header, using the first entry.`);\n return value[0];\n }\n return value;\n }\n }\n\n return undefined;\n};\n\n/**\n * Encodes a string to Base64 format.\n */\nexport const toBase64 = (str: string | null | undefined): string => {\n if (!str) return '';\n if (typeof Buffer !== 'undefined') {\n return Buffer.from(str).toString('base64');\n }\n\n if (typeof btoa !== 'undefined') {\n return btoa(str);\n }\n\n throw new AnthropicError('Cannot generate b64 string; Expected `Buffer` or `btoa` to be defined');\n};\n\nexport function isObj(obj: unknown): obj is Record<string, unknown> {\n return obj != null && typeof obj === 'object' && !Array.isArray(obj);\n}\n", "// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\n\nimport { AbstractPage, Response, APIClient, FinalRequestOptions, PageInfo } from \"./core.js\";\n\nexport interface PageResponse<Item> {\n data: Array<Item>;\n\n has_more: boolean;\n\n first_id: string | null;\n\n last_id: string | null;\n}\n\nexport interface PageParams {\n /**\n * Number of items per page.\n */\n limit?: number;\n\n before_id?: string;\n\n after_id?: string;\n}\n\nexport class Page<Item> extends AbstractPage<Item> implements PageResponse<Item> {\n data: Array<Item>;\n\n has_more: boolean;\n\n first_id: string | null;\n\n last_id: string | null;\n\n constructor(client: APIClient, response: Response, body: PageResponse<Item>, options: FinalRequestOptions) {\n super(client, response, body, options);\n\n this.data = body.data || [];\n this.has_more = body.has_more || false;\n this.first_id = body.first_id || null;\n this.last_id = body.last_id || null;\n }\n\n getPaginatedItems(): Item[] {\n return this.data ?? [];\n }\n\n override hasNextPage(): boolean {\n if (this.has_more === false) {\n return false;\n }\n\n return super.hasNextPage();\n }\n\n // @deprecated Please use `nextPageInfo()` instead\n nextPageParams(): Partial<PageParams> | null {\n const info = this.nextPageInfo();\n if (!info) return null;\n if ('params' in info) return info.params;\n const params = Object.fromEntries(info.url.searchParams);\n if (!Object.keys(params).length) return null;\n return params;\n }\n\n nextPageInfo(): PageInfo | null {\n if ((this.options.query as Record<string, unknown>)?.['before_id']) {\n // in reverse\n const firstId = this.first_id;\n if (!firstId) {\n return null;\n }\n\n return {\n params: {\n before_id: firstId,\n },\n };\n }\n\n const cursor = this.last_id;\n if (!cursor) {\n return null;\n }\n\n return {\n params: {\n after_id: cursor,\n },\n };\n }\n}\n", "// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\n\nimport * as Core from \"./core.js\";\n\nexport class APIResource {\n protected _client: Core.APIClient;\n\n constructor(client: Core.APIClient) {\n this._client = client;\n }\n}\n", "// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\n\nimport { APIResource } from \"../../resource.js\";\nimport { isRequestOptions } from \"../../core.js\";\nimport * as Core from \"../../core.js\";\nimport { Page, type PageParams } from \"../../pagination.js\";\n\nexport class Models extends APIResource {\n /**\n * Get a specific model.\n *\n * The Models API response can be used to determine information about a specific\n * model or resolve a model alias to a model ID.\n */\n retrieve(modelId: string, options?: Core.RequestOptions): Core.APIPromise<BetaModelInfo> {\n return this._client.get(`/v1/models/${modelId}?beta=true`, options);\n }\n\n /**\n * List available models.\n *\n * The Models API response can be used to determine which models are available for\n * use in the API. More recently released models are listed first.\n */\n list(\n query?: ModelListParams,\n options?: Core.RequestOptions,\n ): Core.PagePromise<BetaModelInfosPage, BetaModelInfo>;\n list(options?: Core.RequestOptions): Core.PagePromise<BetaModelInfosPage, BetaModelInfo>;\n list(\n query: ModelListParams | Core.RequestOptions = {},\n options?: Core.RequestOptions,\n ): Core.PagePromise<BetaModelInfosPage, BetaModelInfo> {\n if (isRequestOptions(query)) {\n return this.list({}, query);\n }\n return this._client.getAPIList('/v1/models?beta=true', BetaModelInfosPage, { query, ...options });\n }\n}\n\nexport class BetaModelInfosPage extends Page<BetaModelInfo> {}\n\nexport interface BetaModelInfo {\n /**\n * Unique model identifier.\n */\n id: string;\n\n /**\n * RFC 3339 datetime string representing the time at which the model was released.\n * May be set to an epoch value if the release date is unknown.\n */\n created_at: string;\n\n /**\n * A human-readable name for the model.\n */\n display_name: string;\n\n /**\n * Object type.\n *\n * For Models, this is always `\"model\"`.\n */\n type: 'model';\n}\n\nexport interface ModelListParams extends PageParams {}\n\nModels.BetaModelInfosPage = BetaModelInfosPage;\n\nexport declare namespace Models {\n export {\n type BetaModelInfo as BetaModelInfo,\n BetaModelInfosPage as BetaModelInfosPage,\n type ModelListParams as ModelListParams,\n };\n}\n", "import { AnthropicError } from \"../../error.js\";\nimport { ReadableStreamToAsyncIterable } from \"../stream-utils.js\";\nimport { type Response } from \"../../_shims/index.js\";\nimport { LineDecoder, type Bytes } from \"./line.js\";\n\nexport class JSONLDecoder<T> {\n controller: AbortController;\n\n constructor(\n private iterator: AsyncIterableIterator<Bytes>,\n controller: AbortController,\n ) {\n this.controller = controller;\n }\n\n private async *decoder(): AsyncIterator<T, any, undefined> {\n const lineDecoder = new LineDecoder();\n for await (const chunk of this.iterator) {\n for (const line of lineDecoder.decode(chunk)) {\n yield JSON.parse(line);\n }\n }\n\n for (const line of lineDecoder.flush()) {\n yield JSON.parse(line);\n }\n }\n\n [Symbol.asyncIterator](): AsyncIterator<T> {\n return this.decoder();\n }\n\n static fromResponse<T>(response: Response, controller: AbortController): JSONLDecoder<T> {\n if (!response.body) {\n controller.abort();\n throw new AnthropicError(`Attempted to iterate over a response with no body`);\n }\n\n return new JSONLDecoder(ReadableStreamToAsyncIterable<Bytes>(response.body), controller);\n }\n}\n", "// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\n\nimport { APIResource } from \"../../../resource.js\";\nimport { isRequestOptions } from \"../../../core.js\";\nimport * as Core from \"../../../core.js\";\nimport * as BetaAPI from \"../beta.js\";\nimport * as BetaMessagesAPI from \"./messages.js\";\nimport { Page, type PageParams } from \"../../../pagination.js\";\nimport { JSONLDecoder } from \"../../../internal/decoders/jsonl.js\";\nimport { AnthropicError } from \"../../../error.js\";\n\nexport class Batches extends APIResource {\n /**\n * Send a batch of Message creation requests.\n *\n * The Message Batches API can be used to process multiple Messages API requests at\n * once. Once a Message Batch is created, it begins processing immediately. Batches\n * can take up to 24 hours to complete.\n *\n * Learn more about the Message Batches API in our\n * [user guide](/en/docs/build-with-claude/batch-processing)\n */\n create(params: BatchCreateParams, options?: Core.RequestOptions): Core.APIPromise<BetaMessageBatch> {\n const { betas, ...body } = params;\n return this._client.post('/v1/messages/batches?beta=true', {\n body,\n ...options,\n headers: {\n 'anthropic-beta': [...(betas ?? []), 'message-batches-2024-09-24'].toString(),\n ...options?.headers,\n },\n });\n }\n\n /**\n * This endpoint is idempotent and can be used to poll for Message Batch\n * completion. To access the results of a Message Batch, make a request to the\n * `results_url` field in the response.\n *\n * Learn more about the Message Batches API in our\n * [user guide](/en/docs/build-with-claude/batch-processing)\n */\n retrieve(\n messageBatchId: string,\n params?: BatchRetrieveParams,\n options?: Core.RequestOptions,\n ): Core.APIPromise<BetaMessageBatch>;\n retrieve(messageBatchId: string, options?: Core.RequestOptions): Core.APIPromise<BetaMessageBatch>;\n retrieve(\n messageBatchId: string,\n params: BatchRetrieveParams | Core.RequestOptions = {},\n options?: Core.RequestOptions,\n ): Core.APIPromise<BetaMessageBatch> {\n if (isRequestOptions(params)) {\n return this.retrieve(messageBatchId, {}, params);\n }\n const { betas } = params;\n return this._client.get(`/v1/messages/batches/${messageBatchId}?beta=true`, {\n ...options,\n headers: {\n 'anthropic-beta': [...(betas ?? []), 'message-batches-2024-09-24'].toString(),\n ...options?.headers,\n },\n });\n }\n\n /**\n * List all Message Batches within a Workspace. Most recently created batches are\n * returned first.\n *\n * Learn more about the Message Batches API in our\n * [user guide](/en/docs/build-with-claude/batch-processing)\n */\n list(\n params?: BatchListParams,\n options?: Core.RequestOptions,\n ): Core.PagePromise<BetaMessageBatchesPage, BetaMessageBatch>;\n list(options?: Core.RequestOptions): Core.PagePromise<BetaMessageBatchesPage, BetaMessageBatch>;\n list(\n params: BatchListParams | Core.RequestOptions = {},\n options?: Core.RequestOptions,\n ): Core.PagePromise<BetaMessageBatchesPage, BetaMessageBatch> {\n if (isRequestOptions(params)) {\n return this.list({}, params);\n }\n const { betas, ...query } = params;\n return this._client.getAPIList('/v1/messages/batches?beta=true', BetaMessageBatchesPage, {\n query,\n ...options,\n headers: {\n 'anthropic-beta': [...(betas ?? []), 'message-batches-2024-09-24'].toString(),\n ...options?.headers,\n },\n });\n }\n\n /**\n * Delete a Message Batch.\n *\n * Message Batches can only be deleted once they've finished processing. If you'd\n * like to delete an in-progress batch, you must first cancel it.\n *\n * Learn more about the Message Batches API in our\n * [user guide](/en/docs/build-with-claude/batch-processing)\n */\n delete(\n messageBatchId: string,\n params?: BatchDeleteParams,\n options?: Core.RequestOptions,\n ): Core.APIPromise<BetaDeletedMessageBatch>;\n delete(messageBatchId: string, options?: Core.RequestOptions): Core.APIPromise<BetaDeletedMessageBatch>;\n delete(\n messageBatchId: string,\n params: BatchDeleteParams | Core.RequestOptions = {},\n options?: Core.RequestOptions,\n ): Core.APIPromise<BetaDeletedMessageBatch> {\n if (isRequestOptions(params)) {\n return this.delete(messageBatchId, {}, params);\n }\n const { betas } = params;\n return this._client.delete(`/v1/messages/batches/${messageBatchId}?beta=true`, {\n ...options,\n headers: {\n 'anthropic-beta': [...(betas ?? []), 'message-batches-2024-09-24'].toString(),\n ...options?.headers,\n },\n });\n }\n\n /**\n * Batches may be canceled any time before processing ends. Once cancellation is\n * initiated, the batch enters a `canceling` state, at which time the system may\n * complete any in-progress, non-interruptible requests before finalizing\n * cancellation.\n *\n * The number of canceled requests is specified in `request_counts`. To determine\n * which requests were canceled, check the individual results within the batch.\n * Note that cancellation may not result in any canceled requests if they were\n * non-interruptible.\n *\n * Learn more about the Message Batches API in our\n * [user guide](/en/docs/build-with-claude/batch-processing)\n */\n cancel(\n messageBatchId: string,\n params?: BatchCancelParams,\n options?: Core.RequestOptions,\n ): Core.APIPromise<BetaMessageBatch>;\n cancel(messageBatchId: string, options?: Core.RequestOptions): Core.APIPromise<BetaMessageBatch>;\n cancel(\n messageBatchId: string,\n params: BatchCancelParams | Core.RequestOptions = {},\n options?: Core.RequestOptions,\n ): Core.APIPromise<BetaMessageBatch> {\n if (isRequestOptions(params)) {\n return this.cancel(messageBatchId, {}, params);\n }\n const { betas } = params;\n return this._client.post(`/v1/messages/batches/${messageBatchId}/cancel?beta=true`, {\n ...options,\n headers: {\n 'anthropic-beta': [...(betas ?? []), 'message-batches-2024-09-24'].toString(),\n ...options?.headers,\n },\n });\n }\n\n /**\n * Streams the results of a Message Batch as a `.jsonl` file.\n *\n * Each line in the file is a JSON object containing the result of a single request\n * in the Message Batch. Results are not guaranteed to be in the same order as\n * requests. Use the `custom_id` field to match results to requests.\n *\n * Learn more about the Message Batches API in our\n * [user guide](/en/docs/build-with-claude/batch-processing)\n */\n async results(\n messageBatchId: string,\n params?: BatchResultsParams,\n options?: Core.RequestOptions,\n ): Promise<JSONLDecoder<BetaMessageBatchIndividualResponse>>;\n async results(\n messageBatchId: string,\n options?: Core.RequestOptions,\n ): Promise<JSONLDecoder<BetaMessageBatchIndividualResponse>>;\n async results(\n messageBatchId: string,\n params: BatchResultsParams | Core.RequestOptions = {},\n options?: Core.RequestOptions,\n ): Promise<JSONLDecoder<BetaMessageBatchIndividualResponse>> {\n if (isRequestOptions(params)) {\n return this.results(messageBatchId, {}, params);\n }\n\n const batch = await this.retrieve(messageBatchId);\n if (!batch.results_url) {\n throw new AnthropicError(\n `No batch \\`results_url\\`; Has it finished processing? ${batch.processing_status} - ${batch.id}`,\n );\n }\n\n const { betas } = params;\n return this._client\n .get(batch.results_url, {\n ...options,\n headers: {\n 'anthropic-beta': [...(betas ?? []), 'message-batches-2024-09-24'].toString(),\n Accept: 'application/binary',\n ...options?.headers,\n },\n __binaryResponse: true,\n })\n ._thenUnwrap((_, props) => JSONLDecoder.fromResponse(props.response, props.controller));\n }\n}\n\nexport class BetaMessageBatchesPage extends Page<BetaMessageBatch> {}\n\nexport interface BetaDeletedMessageBatch {\n /**\n * ID of the Message Batch.\n */\n id: string;\n\n /**\n * Deleted object type.\n *\n * For Message Batches, this is always `\"message_batch_deleted\"`.\n */\n type: 'message_batch_deleted';\n}\n\nexport interface BetaMessageBatch {\n /**\n * Unique object identifier.\n *\n * The format and length of IDs may change over time.\n */\n id: string;\n\n /**\n * RFC 3339 datetime string representing the time at which the Message Batch was\n * archived and its results became unavailable.\n */\n archived_at: string | null;\n\n /**\n * RFC 3339 datetime string representing the time at which cancellation was\n * initiated for the Message Batch. Specified only if cancellation was initiated.\n */\n cancel_initiated_at: string | null;\n\n /**\n * RFC 3339 datetime string representing the time at which the Message Batch was\n * created.\n */\n created_at: string;\n\n /**\n * RFC 3339 datetime string representing the time at which processing for the\n * Message Batch ended. Specified only once processing ends.\n *\n * Processing ends when every request in a Message Batch has either succeeded,\n * errored, canceled, or expired.\n */\n ended_at: string | null;\n\n /**\n * RFC 3339 datetime string representing the time at which the Message Batch will\n * expire and end processing, which is 24 hours after creation.\n */\n expires_at: string;\n\n /**\n * Processing status of the Message Batch.\n */\n processing_status: 'in_progress' | 'canceling' | 'ended';\n\n /**\n * Tallies requests within the Message Batch, categorized by their status.\n *\n * Requests start as `processing` and move to one of the other statuses only once\n * processing of the entire batch ends. The sum of all values always matches the\n * total number of requests in the batch.\n */\n request_counts: BetaMessageBatchRequestCounts;\n\n /**\n * URL to a `.jsonl` file containing the results of the Message Batch requests.\n * Specified only once processing ends.\n *\n * Results in the file are not guaranteed to be in the same order as requests. Use\n * the `custom_id` field to match results to requests.\n */\n results_url: string | null;\n\n /**\n * Object type.\n *\n * For Message Batches, this is always `\"message_batch\"`.\n */\n type: 'message_batch';\n}\n\nexport interface BetaMessageBatchCanceledResult {\n type: 'canceled';\n}\n\nexport interface BetaMessageBatchErroredResult {\n error: BetaAPI.BetaErrorResponse;\n\n type: 'errored';\n}\n\nexport interface BetaMessageBatchExpiredResult {\n type: 'expired';\n}\n\n/**\n * This is a single line in the response `.jsonl` file and does not represent the\n * response as a whole.\n */\nexport interface BetaMessageBatchIndividualResponse {\n /**\n * Developer-provided ID created for each request in a Message Batch. Useful for\n * matching results to requests, as results may be given out of request order.\n *\n * Must be unique for each request within the Message Batch.\n */\n custom_id: string;\n\n /**\n * Processing result for this request.\n *\n * Contains a Message output if processing was successful, an error response if\n * processing failed, or the reason why processing was not attempted, such as\n * cancellation or expiration.\n */\n result: BetaMessageBatchResult;\n}\n\nexport interface BetaMessageBatchRequestCounts {\n /**\n * Number of requests in the Message Batch that have been canceled.\n *\n * This is zero until processing of the entire Message Batch has ended.\n */\n canceled: number;\n\n /**\n * Number of requests in the Message Batch that encountered an error.\n *\n * This is zero until processing of the entire Message Batch has ended.\n */\n errored: number;\n\n /**\n * Number of requests in the Message Batch that have expired.\n *\n * This is zero until processing of the entire Message Batch has ended.\n */\n expired: number;\n\n /**\n * Number of requests in the Message Batch that are processing.\n */\n processing: number;\n\n /**\n * Number of requests in the Message Batch that have completed successfully.\n *\n * This is zero until processing of the entire Message Batch has ended.\n */\n succeeded: number;\n}\n\n/**\n * Processing result for this request.\n *\n * Contains a Message output if processing was successful, an error response if\n * processing failed, or the reason why processing was not attempted, such as\n * cancellation or expiration.\n */\nexport type BetaMessageBatchResult =\n | BetaMessageBatchSucceededResult\n | BetaMessageBatchErroredResult\n | BetaMessageBatchCanceledResult\n | BetaMessageBatchExpiredResult;\n\nexport interface BetaMessageBatchSucceededResult {\n message: BetaMessagesAPI.BetaMessage;\n\n type: 'succeeded';\n}\n\nexport interface BatchCreateParams {\n /**\n * Body param: List of requests for prompt completion. Each is an individual\n * request to create a Message.\n */\n requests: Array<BatchCreateParams.Request>;\n\n /**\n * Header param: Optional header to specify the beta version(s) you want to use.\n */\n betas?: Array<BetaAPI.AnthropicBeta>;\n}\n\nexport namespace BatchCreateParams {\n export interface Request {\n /**\n * Developer-provided ID created for each request in a Message Batch. Useful for\n * matching results to requests, as results may be given out of request order.\n *\n * Must be unique for each request within the Message Batch.\n */\n custom_id: string;\n\n /**\n * Messages API creation parameters for the individual request.\n *\n * See the [Messages API reference](/en/api/messages) for full documentation on\n * available parameters.\n */\n params: Omit<BetaMessagesAPI.MessageCreateParamsNonStreaming, 'betas'>;\n }\n}\n\nexport interface BatchRetrieveParams {\n /**\n * Optional header to specify the beta version(s) you want to use.\n */\n betas?: Array<BetaAPI.AnthropicBeta>;\n}\n\nexport interface BatchListParams extends PageParams {\n /**\n * Header param: Optional header to specify the beta version(s) you want to use.\n */\n betas?: Array<BetaAPI.AnthropicBeta>;\n}\n\nexport interface BatchDeleteParams {\n /**\n * Optional header to specify the beta version(s) you want to use.\n */\n betas?: Array<BetaAPI.AnthropicBeta>;\n}\n\nexport interface BatchCancelParams {\n /**\n * Optional header to specify the beta version(s) you want to use.\n */\n betas?: Array<BetaAPI.AnthropicBeta>;\n}\n\nexport interface BatchResultsParams {\n /**\n * Optional header to specify the beta version(s) you want to use.\n */\n betas?: Array<BetaAPI.AnthropicBeta>;\n}\n\nBatches.BetaMessageBatchesPage = BetaMessageBatchesPage;\n\nexport declare namespace Batches {\n export {\n type BetaDeletedMessageBatch as BetaDeletedMessageBatch,\n type BetaMessageBatch as BetaMessageBatch,\n type BetaMessageBatchCanceledResult as BetaMessageBatchCanceledResult,\n type BetaMessageBatchErroredResult as BetaMessageBatchErroredResult,\n type BetaMessageBatchExpiredResult as BetaMessageBatchExpiredResult,\n type BetaMessageBatchIndividualResponse as BetaMessageBatchIndividualResponse,\n type BetaMessageBatchRequestCounts as BetaMessageBatchRequestCounts,\n type BetaMessageBatchResult as BetaMessageBatchResult,\n type BetaMessageBatchSucceededResult as BetaMessageBatchSucceededResult,\n BetaMessageBatchesPage as BetaMessageBatchesPage,\n type BatchCreateParams as BatchCreateParams,\n type BatchRetrieveParams as BatchRetrieveParams,\n type BatchListParams as BatchListParams,\n type BatchDeleteParams as BatchDeleteParams,\n type BatchCancelParams as BatchCancelParams,\n type BatchResultsParams as BatchResultsParams,\n };\n}\n", "type Token = {\n type: string;\n value: string;\n};\n\nconst tokenize = (input: string): Token[] => {\n let current = 0;\n let tokens: Token[] = [];\n\n while (current < input.length) {\n let char = input[current];\n\n if (char === '\\\\') {\n current++;\n continue;\n }\n\n if (char === '{') {\n tokens.push({\n type: 'brace',\n value: '{',\n });\n\n current++;\n continue;\n }\n\n if (char === '}') {\n tokens.push({\n type: 'brace',\n value: '}',\n });\n\n current++;\n continue;\n }\n\n if (char === '[') {\n tokens.push({\n type: 'paren',\n value: '[',\n });\n\n current++;\n continue;\n }\n\n if (char === ']') {\n tokens.push({\n type: 'paren',\n value: ']',\n });\n\n current++;\n continue;\n }\n\n if (char === ':') {\n tokens.push({\n type: 'separator',\n value: ':',\n });\n\n current++;\n continue;\n }\n\n if (char === ',') {\n tokens.push({\n type: 'delimiter',\n value: ',',\n });\n\n current++;\n continue;\n }\n\n if (char === '\"') {\n let value = '';\n let danglingQuote = false;\n\n char = input[++current];\n\n while (char !== '\"') {\n if (current === input.length) {\n danglingQuote = true;\n break;\n }\n\n if (char === '\\\\') {\n current++;\n if (current === input.length) {\n danglingQuote = true;\n break;\n }\n value += char + input[current];\n char = input[++current];\n } else {\n value += char;\n char = input[++current];\n }\n }\n\n char = input[++current];\n\n if (!danglingQuote) {\n tokens.push({\n type: 'string',\n value,\n });\n }\n continue;\n }\n\n let WHITESPACE = /\\s/;\n if (char && WHITESPACE.test(char)) {\n current++;\n continue;\n }\n\n let NUMBERS = /[0-9]/;\n if ((char && NUMBERS.test(char)) || char === '-' || char === '.') {\n let value = '';\n\n if (char === '-') {\n value += char;\n char = input[++current];\n }\n\n while ((char && NUMBERS.test(char)) || char === '.') {\n value += char;\n char = input[++current];\n }\n\n tokens.push({\n type: 'number',\n value,\n });\n continue;\n }\n\n let LETTERS = /[a-z]/i;\n if (char && LETTERS.test(char)) {\n let value = '';\n\n while (char && LETTERS.test(char)) {\n if (current === input.length) {\n break;\n }\n value += char;\n char = input[++current];\n }\n\n if (value == 'true' || value == 'false' || value === 'null') {\n tokens.push({\n type: 'name',\n value,\n });\n } else {\n // unknown token, e.g. `nul` which isn't quite `null`\n current++;\n continue;\n }\n continue;\n }\n\n current++;\n }\n\n return tokens;\n },\n strip = (tokens: Token[]): Token[] => {\n if (tokens.length === 0) {\n return tokens;\n }\n\n let lastToken = tokens[tokens.length - 1]!;\n\n switch (lastToken.type) {\n case 'separator':\n tokens = tokens.slice(0, tokens.length - 1);\n return strip(tokens);\n break;\n case 'number':\n let lastCharacterOfLastToken = lastToken.value[lastToken.value.length - 1];\n if (lastCharacterOfLastToken === '.' || lastCharacterOfLastToken === '-') {\n tokens = tokens.slice(0, tokens.length - 1);\n return strip(tokens);\n }\n case 'string':\n let tokenBeforeTheLastToken = tokens[tokens.length - 2];\n if (tokenBeforeTheLastToken?.type === 'delimiter') {\n tokens = tokens.slice(0, tokens.length - 1);\n return strip(tokens);\n } else if (tokenBeforeTheLastToken?.type === 'brace' && tokenBeforeTheLastToken.value === '{') {\n tokens = tokens.slice(0, tokens.length - 1);\n return strip(tokens);\n }\n break;\n case 'delimiter':\n tokens = tokens.slice(0, tokens.length - 1);\n return strip(tokens);\n break;\n }\n\n return tokens;\n },\n unstrip = (tokens: Token[]): Token[] => {\n let tail: string[] = [];\n\n tokens.map((token) => {\n if (token.type === 'brace') {\n if (token.value === '{') {\n tail.push('}');\n } else {\n tail.splice(tail.lastIndexOf('}'), 1);\n }\n }\n if (token.type === 'paren') {\n if (token.value === '[') {\n tail.push(']');\n } else {\n tail.splice(tail.lastIndexOf(']'), 1);\n }\n }\n });\n\n if (tail.length > 0) {\n tail.reverse().map((item) => {\n if (item === '}') {\n tokens.push({\n type: 'brace',\n value: '}',\n });\n } else if (item === ']') {\n tokens.push({\n type: 'paren',\n value: ']',\n });\n }\n });\n }\n\n return tokens;\n },\n generate = (tokens: Token[]): string => {\n let output = '';\n\n tokens.map((token) => {\n switch (token.type) {\n case 'string':\n output += '\"' + token.value + '\"';\n break;\n default:\n output += token.value;\n break;\n }\n });\n\n return output;\n },\n partialParse = (input: string): unknown => JSON.parse(generate(unstrip(strip(tokenize(input)))));\n\nexport { partialParse };\n", "import * as Core from \"../core.js\";\nimport { AnthropicError, APIUserAbortError } from \"../error.js\";\nimport {\n type BetaContentBlock,\n Messages as BetaMessages,\n type BetaMessage,\n type BetaRawMessageStreamEvent as BetaMessageStreamEvent,\n type BetaMessageParam,\n type MessageCreateParams as BetaMessageCreateParams,\n type MessageCreateParamsBase as BetaMessageCreateParamsBase,\n type BetaTextBlock,\n type BetaTextCitation,\n} from \"../resources/beta/messages/messages.js\";\nimport { type ReadableStream, type Response } from \"../_shims/index.js\";\nimport { Stream } from \"../streaming.js\";\nimport { partialParse } from \"../_vendor/partial-json-parser/parser.js\";\n\nexport interface MessageStreamEvents {\n connect: () => void;\n streamEvent: (event: BetaMessageStreamEvent, snapshot: BetaMessage) => void;\n text: (textDelta: string, textSnapshot: string) => void;\n citation: (citation: BetaTextCitation, citationsSnapshot: BetaTextCitation[]) => void;\n inputJson: (partialJson: string, jsonSnapshot: unknown) => void;\n thinking: (thinkingDelta: string, thinkingSnapshot: string) => void;\n signature: (signature: string) => void;\n message: (message: BetaMessage) => void;\n contentBlock: (content: BetaContentBlock) => void;\n finalMessage: (message: BetaMessage) => void;\n error: (error: AnthropicError) => void;\n abort: (error: APIUserAbortError) => void;\n end: () => void;\n}\n\ntype MessageStreamEventListeners<Event extends keyof MessageStreamEvents> = {\n listener: MessageStreamEvents[Event];\n once?: boolean;\n}[];\n\nconst JSON_BUF_PROPERTY = '__json_buf';\n\nexport class BetaMessageStream implements AsyncIterable<BetaMessageStreamEvent> {\n messages: BetaMessageParam[] = [];\n receivedMessages: BetaMessage[] = [];\n #currentMessageSnapshot: BetaMessage | undefined;\n\n controller: AbortController = new AbortController();\n\n #connectedPromise: Promise<Response | null>;\n #resolveConnectedPromise: (response: Response | null) => void = () => {};\n #rejectConnectedPromise: (error: AnthropicError) => void = () => {};\n\n #endPromise: Promise<void>;\n #resolveEndPromise: () => void = () => {};\n #rejectEndPromise: (error: AnthropicError) => void = () => {};\n\n #listeners: { [Event in keyof MessageStreamEvents]?: MessageStreamEventListeners<Event> } = {};\n\n #ended = false;\n #errored = false;\n #aborted = false;\n #catchingPromiseCreated = false;\n #response: Response | null | undefined;\n #request_id: string | null | undefined;\n\n constructor() {\n this.#connectedPromise = new Promise<Response | null>((resolve, reject) => {\n this.#resolveConnectedPromise = resolve;\n this.#rejectConnectedPromise = reject;\n });\n\n this.#endPromise = new Promise<void>((resolve, reject) => {\n this.#resolveEndPromise = resolve;\n this.#rejectEndPromise = reject;\n });\n\n // Don't let these promises cause unhandled rejection errors.\n // we will manually cause an unhandled rejection error later\n // if the user hasn't registered any error listener or called\n // any promise-returning method.\n this.#connectedPromise.catch(() => {});\n this.#endPromise.catch(() => {});\n }\n\n get response(): Response | null | undefined {\n return this.#response;\n }\n\n get request_id(): string | null | undefined {\n return this.#request_id;\n }\n\n /**\n * Returns the `MessageStream` data, the raw `Response` instance and the ID of the request,\n * returned vie the `request-id` header which is useful for debugging requests and resporting\n * issues to Anthropic.\n *\n * This is the same as the `APIPromise.withResponse()` method.\n *\n * This method will raise an error if you created the stream using `MessageStream.fromReadableStream`\n * as no `Response` is available.\n */\n async withResponse(): Promise<{\n data: BetaMessageStream;\n response: Response;\n request_id: string | null | undefined;\n }> {\n const response = await this.#connectedPromise;\n if (!response) {\n throw new Error('Could not resolve a `Response` object');\n }\n\n return {\n data: this,\n response,\n request_id: response.headers.get('request-id'),\n };\n }\n\n /**\n * Intended for use on the frontend, consuming a stream produced with\n * `.toReadableStream()` on the backend.\n *\n * Note that messages sent to the model do not appear in `.on('message')`\n * in this context.\n */\n static fromReadableStream(stream: ReadableStream): BetaMessageStream {\n const runner = new BetaMessageStream();\n runner._run(() => runner._fromReadableStream(stream));\n return runner;\n }\n\n static createMessage(\n messages: BetaMessages,\n params: BetaMessageCreateParamsBase,\n options?: Core.RequestOptions,\n ): BetaMessageStream {\n const runner = new BetaMessageStream();\n for (const message of params.messages) {\n runner._addMessageParam(message);\n }\n runner._run(() =>\n runner._createMessage(\n messages,\n { ...params, stream: true },\n { ...options, headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'stream' } },\n ),\n );\n return runner;\n }\n\n protected _run(executor: () => Promise<any>) {\n executor().then(() => {\n this._emitFinal();\n this._emit('end');\n }, this.#handleError);\n }\n\n protected _addMessageParam(message: BetaMessageParam) {\n this.messages.push(message);\n }\n\n protected _addMessage(message: BetaMessage, emit = true) {\n this.receivedMessages.push(message);\n if (emit) {\n this._emit('message', message);\n }\n }\n\n protected async _createMessage(\n messages: BetaMessages,\n params: BetaMessageCreateParams,\n options?: Core.RequestOptions,\n ): Promise<void> {\n const signal = options?.signal;\n if (signal) {\n if (signal.aborted) this.controller.abort();\n signal.addEventListener('abort', () => this.controller.abort());\n }\n this.#beginRequest();\n const { response, data: stream } = await messages\n .create({ ...params, stream: true }, { ...options, signal: this.controller.signal })\n .withResponse();\n this._connected(response);\n for await (const event of stream) {\n this.#addStreamEvent(event);\n }\n if (stream.controller.signal?.aborted) {\n throw new APIUserAbortError();\n }\n this.#endRequest();\n }\n\n protected _connected(response: Response | null) {\n if (this.ended) return;\n this.#response = response;\n this.#request_id = response?.headers.get('request-id');\n this.#resolveConnectedPromise(response);\n this._emit('connect');\n }\n\n get ended(): boolean {\n return this.#ended;\n }\n\n get errored(): boolean {\n return this.#errored;\n }\n\n get aborted(): boolean {\n return this.#aborted;\n }\n\n abort() {\n this.controller.abort();\n }\n\n /**\n * Adds the listener function to the end of the listeners array for the event.\n * No checks are made to see if the listener has already been added. Multiple calls passing\n * the same combination of event and listener will result in the listener being added, and\n * called, multiple times.\n * @returns this MessageStream, so that calls can be chained\n */\n on<Event extends keyof MessageStreamEvents>(event: Event, listener: MessageStreamEvents[Event]): this {\n const listeners: MessageStreamEventListeners<Event> =\n this.#listeners[event] || (this.#listeners[event] = []);\n listeners.push({ listener });\n return this;\n }\n\n /**\n * Removes the specified listener from the listener array for the event.\n * off() will remove, at most, one instance of a listener from the listener array. If any single\n * listener has been added multiple times to the listener array for the specified event, then\n * off() must be called multiple times to remove each instance.\n * @returns this MessageStream, so that calls can be chained\n */\n off<Event extends keyof MessageStreamEvents>(event: Event, listener: MessageStreamEvents[Event]): this {\n const listeners = this.#listeners[event];\n if (!listeners) return this;\n const index = listeners.findIndex((l) => l.listener === listener);\n if (index >= 0) listeners.splice(index, 1);\n return this;\n }\n\n /**\n * Adds a one-time listener function for the event. The next time the event is triggered,\n * this listener is removed and then invoked.\n * @returns this MessageStream, so that calls can be chained\n */\n once<Event extends keyof MessageStreamEvents>(event: Event, listener: MessageStreamEvents[Event]): this {\n const listeners: MessageStreamEventListeners<Event> =\n this.#listeners[event] || (this.#listeners[event] = []);\n listeners.push({ listener, once: true });\n return this;\n }\n\n /**\n * This is similar to `.once()`, but returns a Promise that resolves the next time\n * the event is triggered, instead of calling a listener callback.\n * @returns a Promise that resolves the next time given event is triggered,\n * or rejects if an error is emitted. (If you request the 'error' event,\n * returns a promise that resolves with the error).\n *\n * Example:\n *\n * const message = await stream.emitted('message') // rejects if the stream errors\n */\n emitted<Event extends keyof MessageStreamEvents>(\n event: Event,\n ): Promise<\n Parameters<MessageStreamEvents[Event]> extends [infer Param] ? Param\n : Parameters<MessageStreamEvents[Event]> extends [] ? void\n : Parameters<MessageStreamEvents[Event]>\n > {\n return new Promise((resolve, reject) => {\n this.#catchingPromiseCreated = true;\n if (event !== 'error') this.once('error', reject);\n this.once(event, resolve as any);\n });\n }\n\n async done(): Promise<void> {\n this.#catchingPromiseCreated = true;\n await this.#endPromise;\n }\n\n get currentMessage(): BetaMessage | undefined {\n return this.#currentMessageSnapshot;\n }\n\n #getFinalMessage(): BetaMessage {\n if (this.receivedMessages.length === 0) {\n throw new AnthropicError('stream ended without producing a Message with role=assistant');\n }\n return this.receivedMessages.at(-1)!;\n }\n\n /**\n * @returns a promise that resolves with the the final assistant Message response,\n * or rejects if an error occurred or the stream ended prematurely without producing a Message.\n */\n async finalMessage(): Promise<BetaMessage> {\n await this.done();\n return this.#getFinalMessage();\n }\n\n #getFinalText(): string {\n if (this.receivedMessages.length === 0) {\n throw new AnthropicError('stream ended without producing a Message with role=assistant');\n }\n const textBlocks = this.receivedMessages\n .at(-1)!\n .content.filter((block): block is BetaTextBlock => block.type === 'text')\n .map((block) => block.text);\n if (textBlocks.length === 0) {\n throw new AnthropicError('stream ended without producing a content block with type=text');\n }\n return textBlocks.join(' ');\n }\n\n /**\n * @returns a promise that resolves with the the final assistant Message's text response, concatenated\n * together if there are more than one text blocks.\n * Rejects if an error occurred or the stream ended prematurely without producing a Message.\n */\n async finalText(): Promise<string> {\n await this.done();\n return this.#getFinalText();\n }\n\n #handleError = (error: unknown) => {\n this.#errored = true;\n if (error instanceof Error && error.name === 'AbortError') {\n error = new APIUserAbortError();\n }\n if (error instanceof APIUserAbortError) {\n this.#aborted = true;\n return this._emit('abort', error);\n }\n if (error instanceof AnthropicError) {\n return this._emit('error', error);\n }\n if (error instanceof Error) {\n const anthropicError: AnthropicError = new AnthropicError(error.message);\n // @ts-ignore\n anthropicError.cause = error;\n return this._emit('error', anthropicError);\n }\n return this._emit('error', new AnthropicError(String(error)));\n };\n\n protected _emit<Event extends keyof MessageStreamEvents>(\n event: Event,\n ...args: Parameters<MessageStreamEvents[Event]>\n ) {\n // make sure we don't emit any MessageStreamEvents after end\n if (this.#ended) return;\n\n if (event === 'end') {\n this.#ended = true;\n this.#resolveEndPromise();\n }\n\n const listeners: MessageStreamEventListeners<Event> | undefined = this.#listeners[event];\n if (listeners) {\n this.#listeners[event] = listeners.filter((l) => !l.once) as any;\n listeners.forEach(({ listener }: any) => listener(...args));\n }\n\n if (event === 'abort') {\n const error = args[0] as APIUserAbortError;\n if (!this.#catchingPromiseCreated && !listeners?.length) {\n Promise.reject(error);\n }\n this.#rejectConnectedPromise(error);\n this.#rejectEndPromise(error);\n this._emit('end');\n return;\n }\n\n if (event === 'error') {\n // NOTE: _emit('error', error) should only be called from #handleError().\n\n const error = args[0] as AnthropicError;\n if (!this.#catchingPromiseCreated && !listeners?.length) {\n // Trigger an unhandled rejection if the user hasn't registered any error handlers.\n // If you are seeing stack traces here, make sure to handle errors via either:\n // - runner.on('error', () => ...)\n // - await runner.done()\n // - await runner.final...()\n // - etc.\n Promise.reject(error);\n }\n this.#rejectConnectedPromise(error);\n this.#rejectEndPromise(error);\n this._emit('end');\n }\n }\n\n protected _emitFinal() {\n const finalMessage = this.receivedMessages.at(-1);\n if (finalMessage) {\n this._emit('finalMessage', this.#getFinalMessage());\n }\n }\n\n #beginRequest() {\n if (this.ended) return;\n this.#currentMessageSnapshot = undefined;\n }\n #addStreamEvent(event: BetaMessageStreamEvent) {\n if (this.ended) return;\n const messageSnapshot = this.#accumulateMessage(event);\n this._emit('streamEvent', event, messageSnapshot);\n\n switch (event.type) {\n case 'content_block_delta': {\n const content = messageSnapshot.content.at(-1)!;\n switch (event.delta.type) {\n case 'text_delta': {\n if (content.type === 'text') {\n this._emit('text', event.delta.text, content.text || '');\n }\n break;\n }\n case 'citations_delta': {\n if (content.type === 'text') {\n this._emit('citation', event.delta.citation, content.citations ?? []);\n }\n break;\n }\n case 'input_json_delta': {\n if (content.type === 'tool_use' && content.input) {\n this._emit('inputJson', event.delta.partial_json, content.input);\n }\n break;\n }\n case 'thinking_delta': {\n if (content.type === 'thinking') {\n this._emit('thinking', event.delta.thinking, content.thinking);\n }\n break;\n }\n case 'signature_delta': {\n if (content.type === 'thinking') {\n this._emit('signature', content.signature);\n }\n break;\n }\n default:\n checkNever(event.delta);\n }\n break;\n }\n case 'message_stop': {\n this._addMessageParam(messageSnapshot);\n this._addMessage(messageSnapshot, true);\n break;\n }\n case 'content_block_stop': {\n this._emit('contentBlock', messageSnapshot.content.at(-1)!);\n break;\n }\n case 'message_start': {\n this.#currentMessageSnapshot = messageSnapshot;\n break;\n }\n case 'content_block_start':\n case 'message_delta':\n break;\n }\n }\n #endRequest(): BetaMessage {\n if (this.ended) {\n throw new AnthropicError(`stream has ended, this shouldn't happen`);\n }\n const snapshot = this.#currentMessageSnapshot;\n if (!snapshot) {\n throw new AnthropicError(`request ended without sending any chunks`);\n }\n this.#currentMessageSnapshot = undefined;\n return snapshot;\n }\n\n protected async _fromReadableStream(\n readableStream: ReadableStream,\n options?: Core.RequestOptions,\n ): Promise<void> {\n const signal = options?.signal;\n if (signal) {\n if (signal.aborted) this.controller.abort();\n signal.addEventListener('abort', () => this.controller.abort());\n }\n this.#beginRequest();\n this._connected(null);\n const stream = Stream.fromReadableStream<BetaMessageStreamEvent>(readableStream, this.controller);\n for await (const event of stream) {\n this.#addStreamEvent(event);\n }\n if (stream.controller.signal?.aborted) {\n throw new APIUserAbortError();\n }\n this.#endRequest();\n }\n\n /**\n * Mutates this.#currentMessage with the current event. Handling the accumulation of multiple messages\n * will be needed to be handled by the caller, this method will throw if you try to accumulate for multiple\n * messages.\n */\n #accumulateMessage(event: BetaMessageStreamEvent): BetaMessage {\n let snapshot = this.#currentMessageSnapshot;\n\n if (event.type === 'message_start') {\n if (snapshot) {\n throw new AnthropicError(`Unexpected event order, got ${event.type} before receiving \"message_stop\"`);\n }\n return event.message;\n }\n\n if (!snapshot) {\n throw new AnthropicError(`Unexpected event order, got ${event.type} before \"message_start\"`);\n }\n\n switch (event.type) {\n case 'message_stop':\n return snapshot;\n case 'message_delta':\n snapshot.stop_reason = event.delta.stop_reason;\n snapshot.stop_sequence = event.delta.stop_sequence;\n snapshot.usage.output_tokens = event.usage.output_tokens;\n return snapshot;\n case 'content_block_start':\n snapshot.content.push(event.content_block);\n return snapshot;\n case 'content_block_delta': {\n const snapshotContent = snapshot.content.at(event.index);\n\n switch (event.delta.type) {\n case 'text_delta': {\n if (snapshotContent?.type === 'text') {\n snapshotContent.text += event.delta.text;\n }\n break;\n }\n case 'citations_delta': {\n if (snapshotContent?.type === 'text') {\n snapshotContent.citations ??= [];\n snapshotContent.citations.push(event.delta.citation);\n }\n break;\n }\n case 'input_json_delta': {\n if (snapshotContent?.type === 'tool_use') {\n // we need to keep track of the raw JSON string as well so that we can\n // re-parse it for each delta, for now we just store it as an untyped\n // non-enumerable property on the snapshot\n let jsonBuf = (snapshotContent as any)[JSON_BUF_PROPERTY] || '';\n jsonBuf += event.delta.partial_json;\n\n Object.defineProperty(snapshotContent, JSON_BUF_PROPERTY, {\n value: jsonBuf,\n enumerable: false,\n writable: true,\n });\n\n if (jsonBuf) {\n snapshotContent.input = partialParse(jsonBuf);\n }\n }\n break;\n }\n case 'thinking_delta': {\n if (snapshotContent?.type === 'thinking') {\n snapshotContent.thinking += event.delta.thinking;\n }\n break;\n }\n case 'signature_delta': {\n if (snapshotContent?.type === 'thinking') {\n snapshotContent.signature = event.delta.signature;\n }\n break;\n }\n default:\n checkNever(event.delta);\n }\n return snapshot;\n }\n case 'content_block_stop':\n return snapshot;\n }\n }\n\n [Symbol.asyncIterator](): AsyncIterator<BetaMessageStreamEvent> {\n const pushQueue: BetaMessageStreamEvent[] = [];\n const readQueue: {\n resolve: (chunk: BetaMessageStreamEvent | undefined) => void;\n reject: (error: unknown) => void;\n }[] = [];\n let done = false;\n\n this.on('streamEvent', (event) => {\n const reader = readQueue.shift();\n if (reader) {\n reader.resolve(event);\n } else {\n pushQueue.push(event);\n }\n });\n\n this.on('end', () => {\n done = true;\n for (const reader of readQueue) {\n reader.resolve(undefined);\n }\n readQueue.length = 0;\n });\n\n this.on('abort', (err) => {\n done = true;\n for (const reader of readQueue) {\n reader.reject(err);\n }\n readQueue.length = 0;\n });\n\n this.on('error', (err) => {\n done = true;\n for (const reader of readQueue) {\n reader.reject(err);\n }\n readQueue.length = 0;\n });\n\n return {\n next: async (): Promise<IteratorResult<BetaMessageStreamEvent>> => {\n if (!pushQueue.length) {\n if (done) {\n return { value: undefined, done: true };\n }\n return new Promise<BetaMessageStreamEvent | undefined>((resolve, reject) =>\n readQueue.push({ resolve, reject }),\n ).then((chunk) => (chunk ? { value: chunk, done: false } : { value: undefined, done: true }));\n }\n const chunk = pushQueue.shift()!;\n return { value: chunk, done: false };\n },\n return: async () => {\n this.abort();\n return { value: undefined, done: true };\n },\n };\n }\n\n toReadableStream(): ReadableStream {\n const stream = new Stream(this[Symbol.asyncIterator].bind(this), this.controller);\n return stream.toReadableStream();\n }\n}\n\n// used to ensure exhaustive case matching without throwing a runtime error\nfunction checkNever(x: never) {}\n", "// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\n\nimport { APIResource } from \"../../../resource.js\";\nimport { APIPromise } from \"../../../core.js\";\nimport * as Core from \"../../../core.js\";\nimport * as MessagesMessagesAPI from \"./messages.js\";\nimport * as BetaAPI from \"../beta.js\";\nimport * as MessagesAPI from \"../../messages/messages.js\";\nimport * as BatchesAPI from \"./batches.js\";\nimport {\n BatchCancelParams,\n BatchCreateParams,\n BatchDeleteParams,\n BatchListParams,\n BatchResultsParams,\n BatchRetrieveParams,\n Batches,\n BetaDeletedMessageBatch,\n BetaMessageBatch,\n BetaMessageBatchCanceledResult,\n BetaMessageBatchErroredResult,\n BetaMessageBatchExpiredResult,\n BetaMessageBatchIndividualResponse,\n BetaMessageBatchRequestCounts,\n BetaMessageBatchResult,\n BetaMessageBatchSucceededResult,\n BetaMessageBatchesPage,\n} from \"./batches.js\";\nimport { Stream } from \"../../../streaming.js\";\nimport { BetaMessageStream } from \"../../../lib/BetaMessageStream.js\";\nimport type { Model } from \"../../messages/messages.js\";\n\nconst DEPRECATED_MODELS: {\n [K in Model]?: string;\n} = {\n 'claude-1.3': 'November 6th, 2024',\n 'claude-1.3-100k': 'November 6th, 2024',\n 'claude-instant-1.1': 'November 6th, 2024',\n 'claude-instant-1.1-100k': 'November 6th, 2024',\n 'claude-instant-1.2': 'November 6th, 2024',\n 'claude-3-sonnet-20240229': 'July 21st, 2025',\n 'claude-2.1': 'July 21st, 2025',\n 'claude-2.0': 'July 21st, 2025',\n};\n\nexport class Messages extends APIResource {\n batches: BatchesAPI.Batches = new BatchesAPI.Batches(this._client);\n\n /**\n * Send a structured list of input messages with text and/or image content, and the\n * model will generate the next message in the conversation.\n *\n * The Messages API can be used for either single queries or stateless multi-turn\n * conversations.\n *\n * Learn more about the Messages API in our [user guide](/en/docs/initial-setup)\n */\n create(params: MessageCreateParamsNonStreaming, options?: Core.RequestOptions): APIPromise<BetaMessage>;\n create(\n params: MessageCreateParamsStreaming,\n options?: Core.RequestOptions,\n ): APIPromise<Stream<BetaRawMessageStreamEvent>>;\n create(\n params: MessageCreateParamsBase,\n options?: Core.RequestOptions,\n ): APIPromise<Stream<BetaRawMessageStreamEvent> | BetaMessage>;\n create(\n params: MessageCreateParams,\n options?: Core.RequestOptions,\n ): APIPromise<BetaMessage> | APIPromise<Stream<BetaRawMessageStreamEvent>> {\n const { betas, ...body } = params;\n\n if (body.model in DEPRECATED_MODELS) {\n console.warn(\n `The model '${body.model}' is deprecated and will reach end-of-life on ${\n DEPRECATED_MODELS[body.model]\n }\\nPlease migrate to a newer model. Visit https://docs.anthropic.com/en/docs/resources/model-deprecations for more information.`,\n );\n }\n\n return this._client.post('/v1/messages?beta=true', {\n body,\n timeout:\n (this._client as any)._options.timeout ??\n (body.stream ? 600000 : this._client._calculateNonstreamingTimeout(body.max_tokens)),\n ...options,\n headers: {\n ...(betas?.toString() != null ? { 'anthropic-beta': betas?.toString() } : undefined),\n ...options?.headers,\n },\n stream: params.stream ?? false,\n }) as APIPromise<BetaMessage> | APIPromise<Stream<BetaRawMessageStreamEvent>>;\n }\n\n /**\n * Create a Message stream\n */\n stream(body: BetaMessageStreamParams, options?: Core.RequestOptions): BetaMessageStream {\n return BetaMessageStream.createMessage(this, body, options);\n }\n\n /**\n * Count the number of tokens in a Message.\n *\n * The Token Count API can be used to count the number of tokens in a Message,\n * including tools, images, and documents, without creating it.\n *\n * Learn more about token counting in our\n * [user guide](/en/docs/build-with-claude/token-counting)\n */\n countTokens(\n params: MessageCountTokensParams,\n options?: Core.RequestOptions,\n ): Core.APIPromise<BetaMessageTokensCount> {\n const { betas, ...body } = params;\n return this._client.post('/v1/messages/count_tokens?beta=true', {\n body,\n ...options,\n headers: {\n 'anthropic-beta': [...(betas ?? []), 'token-counting-2024-11-01'].toString(),\n ...options?.headers,\n },\n });\n }\n}\n\nexport type BetaMessageStreamParams = MessageCreateParamsBase;\n\nexport interface BetaBase64ImageSource {\n data: string;\n\n media_type: 'image/jpeg' | 'image/png' | 'image/gif' | 'image/webp';\n\n type: 'base64';\n}\n\nexport interface BetaBase64PDFBlock {\n source: BetaBase64PDFSource | BetaPlainTextSource | BetaContentBlockSource | BetaURLPDFSource;\n\n type: 'document';\n\n cache_control?: BetaCacheControlEphemeral | null;\n\n citations?: BetaCitationsConfigParam;\n\n context?: string | null;\n\n title?: string | null;\n}\n\nexport interface BetaBase64PDFSource {\n data: string;\n\n media_type: 'application/pdf';\n\n type: 'base64';\n}\n\nexport interface BetaCacheControlEphemeral {\n type: 'ephemeral';\n}\n\nexport interface BetaCitationCharLocation {\n cited_text: string;\n\n document_index: number;\n\n document_title: string | null;\n\n end_char_index: number;\n\n start_char_index: number;\n\n type: 'char_location';\n}\n\nexport interface BetaCitationCharLocationParam {\n cited_text: string;\n\n document_index: number;\n\n document_title: string | null;\n\n end_char_index: number;\n\n start_char_index: number;\n\n type: 'char_location';\n}\n\nexport interface BetaCitationContentBlockLocation {\n cited_text: string;\n\n document_index: number;\n\n document_title: string | null;\n\n end_block_index: number;\n\n start_block_index: number;\n\n type: 'content_block_location';\n}\n\nexport interface BetaCitationContentBlockLocationParam {\n cited_text: string;\n\n document_index: number;\n\n document_title: string | null;\n\n end_block_index: number;\n\n start_block_index: number;\n\n type: 'content_block_location';\n}\n\nexport interface BetaCitationPageLocation {\n cited_text: string;\n\n document_index: number;\n\n document_title: string | null;\n\n end_page_number: number;\n\n start_page_number: number;\n\n type: 'page_location';\n}\n\nexport interface BetaCitationPageLocationParam {\n cited_text: string;\n\n document_index: number;\n\n document_title: string | null;\n\n end_page_number: number;\n\n start_page_number: number;\n\n type: 'page_location';\n}\n\nexport interface BetaCitationsConfigParam {\n enabled?: boolean;\n}\n\nexport interface BetaCitationsDelta {\n citation: BetaCitationCharLocation | BetaCitationPageLocation | BetaCitationContentBlockLocation;\n\n type: 'citations_delta';\n}\n\nexport type BetaContentBlock =\n | BetaTextBlock\n | BetaToolUseBlock\n | BetaThinkingBlock\n | BetaRedactedThinkingBlock;\n\nexport type BetaContentBlockParam =\n | BetaTextBlockParam\n | BetaImageBlockParam\n | BetaToolUseBlockParam\n | BetaToolResultBlockParam\n | BetaBase64PDFBlock\n | BetaThinkingBlockParam\n | BetaRedactedThinkingBlockParam;\n\nexport interface BetaContentBlockSource {\n content: string | Array<BetaContentBlockSourceContent>;\n\n type: 'content';\n}\n\nexport type BetaContentBlockSourceContent = BetaTextBlockParam | BetaImageBlockParam;\n\nexport interface BetaImageBlockParam {\n source: BetaBase64ImageSource | BetaURLImageSource;\n\n type: 'image';\n\n cache_control?: BetaCacheControlEphemeral | null;\n}\n\nexport interface BetaInputJSONDelta {\n partial_json: string;\n\n type: 'input_json_delta';\n}\n\nexport interface BetaMessage {\n /**\n * Unique object identifier.\n *\n * The format and length of IDs may change over time.\n */\n id: string;\n\n /**\n * Content generated by the model.\n *\n * This is an array of content blocks, each of which has a `type` that determines\n * its shape.\n *\n * Example:\n *\n * ```json\n * [{ \"type\": \"text\", \"text\": \"Hi, I'm Claude.\" }]\n * ```\n *\n * If the request input `messages` ended with an `assistant` turn, then the\n * response `content` will continue directly from that last turn. You can use this\n * to constrain the model's output.\n *\n * For example, if the input `messages` were:\n *\n * ```json\n * [\n * {\n * \"role\": \"user\",\n * \"content\": \"What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun\"\n * },\n * { \"role\": \"assistant\", \"content\": \"The best answer is (\" }\n * ]\n * ```\n *\n * Then the response `content` might be:\n *\n * ```json\n * [{ \"type\": \"text\", \"text\": \"B)\" }]\n * ```\n */\n content: Array<BetaContentBlock>;\n\n /**\n * The model that will complete your prompt.\\n\\nSee\n * [models](https://docs.anthropic.com/en/docs/models-overview) for additional\n * details and options.\n */\n model: MessagesAPI.Model;\n\n /**\n * Conversational role of the generated message.\n *\n * This will always be `\"assistant\"`.\n */\n role: 'assistant';\n\n /**\n * The reason that we stopped.\n *\n * This may be one the following values:\n *\n * - `\"end_turn\"`: the model reached a natural stopping point\n * - `\"max_tokens\"`: we exceeded the requested `max_tokens` or the model's maximum\n * - `\"stop_sequence\"`: one of your provided custom `stop_sequences` was generated\n * - `\"tool_use\"`: the model invoked one or more tools\n *\n * In non-streaming mode this value is always non-null. In streaming mode, it is\n * null in the `message_start` event and non-null otherwise.\n */\n stop_reason: 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use' | null;\n\n /**\n * Which custom stop sequence was generated, if any.\n *\n * This value will be a non-null string if one of your custom stop sequences was\n * generated.\n */\n stop_sequence: string | null;\n\n /**\n * Object type.\n *\n * For Messages, this is always `\"message\"`.\n */\n type: 'message';\n\n /**\n * Billing and rate-limit usage.\n *\n * Anthropic's API bills and rate-limits by token counts, as tokens represent the\n * underlying cost to our systems.\n *\n * Under the hood, the API transforms requests into a format suitable for the\n * model. The model's output then goes through a parsing stage before becoming an\n * API response. As a result, the token counts in `usage` will not match one-to-one\n * with the exact visible content of an API request or response.\n *\n * For example, `output_tokens` will be non-zero, even for an empty string response\n * from Claude.\n *\n * Total input tokens in a request is the summation of `input_tokens`,\n * `cache_creation_input_tokens`, and `cache_read_input_tokens`.\n */\n usage: BetaUsage;\n}\n\nexport interface BetaMessageDeltaUsage {\n /**\n * The cumulative number of output tokens which were used.\n */\n output_tokens: number;\n}\n\nexport interface BetaMessageParam {\n content: string | Array<BetaContentBlockParam>;\n\n role: 'user' | 'assistant';\n}\n\nexport interface BetaMessageTokensCount {\n /**\n * The total number of tokens across the provided list of messages, system prompt,\n * and tools.\n */\n input_tokens: number;\n}\n\nexport interface BetaMetadata {\n /**\n * An external identifier for the user who is associated with the request.\n *\n * This should be a uuid, hash value, or other opaque identifier. Anthropic may use\n * this id to help detect abuse. Do not include any identifying information such as\n * name, email address, or phone number.\n */\n user_id?: string | null;\n}\n\nexport interface BetaPlainTextSource {\n data: string;\n\n media_type: 'text/plain';\n\n type: 'text';\n}\n\nexport interface BetaRawContentBlockDeltaEvent {\n delta: BetaTextDelta | BetaInputJSONDelta | BetaCitationsDelta | BetaThinkingDelta | BetaSignatureDelta;\n\n index: number;\n\n type: 'content_block_delta';\n}\n\nexport interface BetaRawContentBlockStartEvent {\n content_block: BetaTextBlock | BetaToolUseBlock | BetaThinkingBlock | BetaRedactedThinkingBlock;\n\n index: number;\n\n type: 'content_block_start';\n}\n\nexport interface BetaRawContentBlockStopEvent {\n index: number;\n\n type: 'content_block_stop';\n}\n\nexport interface BetaRawMessageDeltaEvent {\n delta: BetaRawMessageDeltaEvent.Delta;\n\n type: 'message_delta';\n\n /**\n * Billing and rate-limit usage.\n *\n * Anthropic's API bills and rate-limits by token counts, as tokens represent the\n * underlying cost to our systems.\n *\n * Under the hood, the API transforms requests into a format suitable for the\n * model. The model's output then goes through a parsing stage before becoming an\n * API response. As a result, the token counts in `usage` will not match one-to-one\n * with the exact visible content of an API request or response.\n *\n * For example, `output_tokens` will be non-zero, even for an empty string response\n * from Claude.\n *\n * Total input tokens in a request is the summation of `input_tokens`,\n * `cache_creation_input_tokens`, and `cache_read_input_tokens`.\n */\n usage: BetaMessageDeltaUsage;\n}\n\nexport namespace BetaRawMessageDeltaEvent {\n export interface Delta {\n stop_reason: 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use' | null;\n\n stop_sequence: string | null;\n }\n}\n\nexport interface BetaRawMessageStartEvent {\n message: BetaMessage;\n\n type: 'message_start';\n}\n\nexport interface BetaRawMessageStopEvent {\n type: 'message_stop';\n}\n\nexport type BetaRawMessageStreamEvent =\n | BetaRawMessageStartEvent\n | BetaRawMessageDeltaEvent\n | BetaRawMessageStopEvent\n | BetaRawContentBlockStartEvent\n | BetaRawContentBlockDeltaEvent\n | BetaRawContentBlockStopEvent;\n\nexport interface BetaRedactedThinkingBlock {\n data: string;\n\n type: 'redacted_thinking';\n}\n\nexport interface BetaRedactedThinkingBlockParam {\n data: string;\n\n type: 'redacted_thinking';\n}\n\nexport interface BetaSignatureDelta {\n signature: string;\n\n type: 'signature_delta';\n}\n\nexport interface BetaTextBlock {\n /**\n * Citations supporting the text block.\n *\n * The type of citation returned will depend on the type of document being cited.\n * Citing a PDF results in `page_location`, plain text results in `char_location`,\n * and content document results in `content_block_location`.\n */\n citations: Array<BetaTextCitation> | null;\n\n text: string;\n\n type: 'text';\n}\n\nexport interface BetaTextBlockParam {\n text: string;\n\n type: 'text';\n\n cache_control?: BetaCacheControlEphemeral | null;\n\n citations?: Array<BetaTextCitationParam> | null;\n}\n\nexport type BetaTextCitation =\n | BetaCitationCharLocation\n | BetaCitationPageLocation\n | BetaCitationContentBlockLocation;\n\nexport type BetaTextCitationParam =\n | BetaCitationCharLocationParam\n | BetaCitationPageLocationParam\n | BetaCitationContentBlockLocationParam;\n\nexport interface BetaTextDelta {\n text: string;\n\n type: 'text_delta';\n}\n\nexport interface BetaThinkingBlock {\n signature: string;\n\n thinking: string;\n\n type: 'thinking';\n}\n\nexport interface BetaThinkingBlockParam {\n signature: string;\n\n thinking: string;\n\n type: 'thinking';\n}\n\nexport interface BetaThinkingConfigDisabled {\n type: 'disabled';\n}\n\nexport interface BetaThinkingConfigEnabled {\n /**\n * Determines how many tokens Claude can use for its internal reasoning process.\n * Larger budgets can enable more thorough analysis for complex problems, improving\n * response quality.\n *\n * Must be \u22651024 and less than `max_tokens`.\n *\n * See\n * [extended thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking)\n * for details.\n */\n budget_tokens: number;\n\n type: 'enabled';\n}\n\n/**\n * Configuration for enabling Claude's extended thinking.\n *\n * When enabled, responses include `thinking` content blocks showing Claude's\n * thinking process before the final answer. Requires a minimum budget of 1,024\n * tokens and counts towards your `max_tokens` limit.\n *\n * See\n * [extended thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking)\n * for details.\n */\nexport type BetaThinkingConfigParam = BetaThinkingConfigEnabled | BetaThinkingConfigDisabled;\n\nexport interface BetaThinkingDelta {\n thinking: string;\n\n type: 'thinking_delta';\n}\n\nexport interface BetaTool {\n /**\n * [JSON schema](https://json-schema.org/draft/2020-12) for this tool's input.\n *\n * This defines the shape of the `input` that your tool accepts and that the model\n * will produce.\n */\n input_schema: BetaTool.InputSchema;\n\n /**\n * Name of the tool.\n *\n * This is how the tool will be called by the model and in tool_use blocks.\n */\n name: string;\n\n cache_control?: BetaCacheControlEphemeral | null;\n\n /**\n * Description of what this tool does.\n *\n * Tool descriptions should be as detailed as possible. The more information that\n * the model has about what the tool is and how to use it, the better it will\n * perform. You can use natural language descriptions to reinforce important\n * aspects of the tool input JSON schema.\n */\n description?: string;\n\n type?: 'custom' | null;\n}\n\nexport namespace BetaTool {\n /**\n * [JSON schema](https://json-schema.org/draft/2020-12) for this tool's input.\n *\n * This defines the shape of the `input` that your tool accepts and that the model\n * will produce.\n */\n export interface InputSchema {\n type: 'object';\n\n properties?: unknown | null;\n [k: string]: unknown;\n }\n}\n\nexport interface BetaToolBash20241022 {\n /**\n * Name of the tool.\n *\n * This is how the tool will be called by the model and in tool_use blocks.\n */\n name: 'bash';\n\n type: 'bash_20241022';\n\n cache_control?: BetaCacheControlEphemeral | null;\n}\n\nexport interface BetaToolBash20250124 {\n /**\n * Name of the tool.\n *\n * This is how the tool will be called by the model and in tool_use blocks.\n */\n name: 'bash';\n\n type: 'bash_20250124';\n\n cache_control?: BetaCacheControlEphemeral | null;\n}\n\n/**\n * How the model should use the provided tools. The model can use a specific tool,\n * any available tool, decide by itself, or not use tools at all.\n */\nexport type BetaToolChoice = BetaToolChoiceAuto | BetaToolChoiceAny | BetaToolChoiceTool | BetaToolChoiceNone;\n\n/**\n * The model will use any available tools.\n */\nexport interface BetaToolChoiceAny {\n type: 'any';\n\n /**\n * Whether to disable parallel tool use.\n *\n * Defaults to `false`. If set to `true`, the model will output exactly one tool\n * use.\n */\n disable_parallel_tool_use?: boolean;\n}\n\n/**\n * The model will automatically decide whether to use tools.\n */\nexport interface BetaToolChoiceAuto {\n type: 'auto';\n\n /**\n * Whether to disable parallel tool use.\n *\n * Defaults to `false`. If set to `true`, the model will output at most one tool\n * use.\n */\n disable_parallel_tool_use?: boolean;\n}\n\n/**\n * The model will not be allowed to use tools.\n */\nexport interface BetaToolChoiceNone {\n type: 'none';\n}\n\n/**\n * The model will use the specified tool with `tool_choice.name`.\n */\nexport interface BetaToolChoiceTool {\n /**\n * The name of the tool to use.\n */\n name: string;\n\n type: 'tool';\n\n /**\n * Whether to disable parallel tool use.\n *\n * Defaults to `false`. If set to `true`, the model will output exactly one tool\n * use.\n */\n disable_parallel_tool_use?: boolean;\n}\n\nexport interface BetaToolComputerUse20241022 {\n /**\n * The height of the display in pixels.\n */\n display_height_px: number;\n\n /**\n * The width of the display in pixels.\n */\n display_width_px: number;\n\n /**\n * Name of the tool.\n *\n * This is how the tool will be called by the model and in tool_use blocks.\n */\n name: 'computer';\n\n type: 'computer_20241022';\n\n cache_control?: BetaCacheControlEphemeral | null;\n\n /**\n * The X11 display number (e.g. 0, 1) for the display.\n */\n display_number?: number | null;\n}\n\nexport interface BetaToolComputerUse20250124 {\n /**\n * The height of the display in pixels.\n */\n display_height_px: number;\n\n /**\n * The width of the display in pixels.\n */\n display_width_px: number;\n\n /**\n * Name of the tool.\n *\n * This is how the tool will be called by the model and in tool_use blocks.\n */\n name: 'computer';\n\n type: 'computer_20250124';\n\n cache_control?: BetaCacheControlEphemeral | null;\n\n /**\n * The X11 display number (e.g. 0, 1) for the display.\n */\n display_number?: number | null;\n}\n\nexport interface BetaToolResultBlockParam {\n tool_use_id: string;\n\n type: 'tool_result';\n\n cache_control?: BetaCacheControlEphemeral | null;\n\n content?: string | Array<BetaTextBlockParam | BetaImageBlockParam>;\n\n is_error?: boolean;\n}\n\nexport interface BetaToolTextEditor20241022 {\n /**\n * Name of the tool.\n *\n * This is how the tool will be called by the model and in tool_use blocks.\n */\n name: 'str_replace_editor';\n\n type: 'text_editor_20241022';\n\n cache_control?: BetaCacheControlEphemeral | null;\n}\n\nexport interface BetaToolTextEditor20250124 {\n /**\n * Name of the tool.\n *\n * This is how the tool will be called by the model and in tool_use blocks.\n */\n name: 'str_replace_editor';\n\n type: 'text_editor_20250124';\n\n cache_control?: BetaCacheControlEphemeral | null;\n}\n\nexport type BetaToolUnion =\n | BetaTool\n | BetaToolComputerUse20241022\n | BetaToolBash20241022\n | BetaToolTextEditor20241022\n | BetaToolComputerUse20250124\n | BetaToolBash20250124\n | BetaToolTextEditor20250124;\n\nexport interface BetaToolUseBlock {\n id: string;\n\n input: unknown;\n\n name: string;\n\n type: 'tool_use';\n}\n\nexport interface BetaToolUseBlockParam {\n id: string;\n\n input: unknown;\n\n name: string;\n\n type: 'tool_use';\n\n cache_control?: BetaCacheControlEphemeral | null;\n}\n\nexport interface BetaURLImageSource {\n type: 'url';\n\n url: string;\n}\n\nexport interface BetaURLPDFSource {\n type: 'url';\n\n url: string;\n}\n\nexport interface BetaUsage {\n /**\n * The number of input tokens used to create the cache entry.\n */\n cache_creation_input_tokens: number | null;\n\n /**\n * The number of input tokens read from the cache.\n */\n cache_read_input_tokens: number | null;\n\n /**\n * The number of input tokens which were used.\n */\n input_tokens: number;\n\n /**\n * The number of output tokens which were used.\n */\n output_tokens: number;\n}\n\nexport type MessageCreateParams = MessageCreateParamsNonStreaming | MessageCreateParamsStreaming;\n\nexport interface MessageCreateParamsBase {\n /**\n * Body param: The maximum number of tokens to generate before stopping.\n *\n * Note that our models may stop _before_ reaching this maximum. This parameter\n * only specifies the absolute maximum number of tokens to generate.\n *\n * Different models have different maximum values for this parameter. See\n * [models](https://docs.anthropic.com/en/docs/models-overview) for details.\n */\n max_tokens: number;\n\n /**\n * Body param: Input messages.\n *\n * Our models are trained to operate on alternating `user` and `assistant`\n * conversational turns. When creating a new `Message`, you specify the prior\n * conversational turns with the `messages` parameter, and the model then generates\n * the next `Message` in the conversation. Consecutive `user` or `assistant` turns\n * in your request will be combined into a single turn.\n *\n * Each input message must be an object with a `role` and `content`. You can\n * specify a single `user`-role message, or you can include multiple `user` and\n * `assistant` messages.\n *\n * If the final message uses the `assistant` role, the response content will\n * continue immediately from the content in that message. This can be used to\n * constrain part of the model's response.\n *\n * Example with a single `user` message:\n *\n * ```json\n * [{ \"role\": \"user\", \"content\": \"Hello, Claude\" }]\n * ```\n *\n * Example with multiple conversational turns:\n *\n * ```json\n * [\n * { \"role\": \"user\", \"content\": \"Hello there.\" },\n * { \"role\": \"assistant\", \"content\": \"Hi, I'm Claude. How can I help you?\" },\n * { \"role\": \"user\", \"content\": \"Can you explain LLMs in plain English?\" }\n * ]\n * ```\n *\n * Example with a partially-filled response from Claude:\n *\n * ```json\n * [\n * {\n * \"role\": \"user\",\n * \"content\": \"What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun\"\n * },\n * { \"role\": \"assistant\", \"content\": \"The best answer is (\" }\n * ]\n * ```\n *\n * Each input message `content` may be either a single `string` or an array of\n * content blocks, where each block has a specific `type`. Using a `string` for\n * `content` is shorthand for an array of one content block of type `\"text\"`. The\n * following input messages are equivalent:\n *\n * ```json\n * { \"role\": \"user\", \"content\": \"Hello, Claude\" }\n * ```\n *\n * ```json\n * { \"role\": \"user\", \"content\": [{ \"type\": \"text\", \"text\": \"Hello, Claude\" }] }\n * ```\n *\n * Starting with Claude 3 models, you can also send image content blocks:\n *\n * ```json\n * {\n * \"role\": \"user\",\n * \"content\": [\n * {\n * \"type\": \"image\",\n * \"source\": {\n * \"type\": \"base64\",\n * \"media_type\": \"image/jpeg\",\n * \"data\": \"/9j/4AAQSkZJRg...\"\n * }\n * },\n * { \"type\": \"text\", \"text\": \"What is in this image?\" }\n * ]\n * }\n * ```\n *\n * We currently support the `base64` source type for images, and the `image/jpeg`,\n * `image/png`, `image/gif`, and `image/webp` media types.\n *\n * See [examples](https://docs.anthropic.com/en/api/messages-examples#vision) for\n * more input examples.\n *\n * Note that if you want to include a\n * [system prompt](https://docs.anthropic.com/en/docs/system-prompts), you can use\n * the top-level `system` parameter \u2014 there is no `\"system\"` role for input\n * messages in the Messages API.\n */\n messages: Array<BetaMessageParam>;\n\n /**\n * Body param: The model that will complete your prompt.\\n\\nSee\n * [models](https://docs.anthropic.com/en/docs/models-overview) for additional\n * details and options.\n */\n model: MessagesAPI.Model;\n\n /**\n * Body param: An object describing metadata about the request.\n */\n metadata?: BetaMetadata;\n\n /**\n * Body param: Custom text sequences that will cause the model to stop generating.\n *\n * Our models will normally stop when they have naturally completed their turn,\n * which will result in a response `stop_reason` of `\"end_turn\"`.\n *\n * If you want the model to stop generating when it encounters custom strings of\n * text, you can use the `stop_sequences` parameter. If the model encounters one of\n * the custom sequences, the response `stop_reason` value will be `\"stop_sequence\"`\n * and the response `stop_sequence` value will contain the matched stop sequence.\n */\n stop_sequences?: Array<string>;\n\n /**\n * Body param: Whether to incrementally stream the response using server-sent\n * events.\n *\n * See [streaming](https://docs.anthropic.com/en/api/messages-streaming) for\n * details.\n */\n stream?: boolean;\n\n /**\n * Body param: System prompt.\n *\n * A system prompt is a way of providing context and instructions to Claude, such\n * as specifying a particular goal or role. See our\n * [guide to system prompts](https://docs.anthropic.com/en/docs/system-prompts).\n */\n system?: string | Array<BetaTextBlockParam>;\n\n /**\n * Body param: Amount of randomness injected into the response.\n *\n * Defaults to `1.0`. Ranges from `0.0` to `1.0`. Use `temperature` closer to `0.0`\n * for analytical / multiple choice, and closer to `1.0` for creative and\n * generative tasks.\n *\n * Note that even with `temperature` of `0.0`, the results will not be fully\n * deterministic.\n */\n temperature?: number;\n\n /**\n * Body param: Configuration for enabling Claude's extended thinking.\n *\n * When enabled, responses include `thinking` content blocks showing Claude's\n * thinking process before the final answer. Requires a minimum budget of 1,024\n * tokens and counts towards your `max_tokens` limit.\n *\n * See\n * [extended thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking)\n * for details.\n */\n thinking?: BetaThinkingConfigParam;\n\n /**\n * Body param: How the model should use the provided tools. The model can use a\n * specific tool, any available tool, decide by itself, or not use tools at all.\n */\n tool_choice?: BetaToolChoice;\n\n /**\n * Body param: Definitions of tools that the model may use.\n *\n * If you include `tools` in your API request, the model may return `tool_use`\n * content blocks that represent the model's use of those tools. You can then run\n * those tools using the tool input generated by the model and then optionally\n * return results back to the model using `tool_result` content blocks.\n *\n * Each tool definition includes:\n *\n * - `name`: Name of the tool.\n * - `description`: Optional, but strongly-recommended description of the tool.\n * - `input_schema`: [JSON schema](https://json-schema.org/draft/2020-12) for the\n * tool `input` shape that the model will produce in `tool_use` output content\n * blocks.\n *\n * For example, if you defined `tools` as:\n *\n * ```json\n * [\n * {\n * \"name\": \"get_stock_price\",\n * \"description\": \"Get the current stock price for a given ticker symbol.\",\n * \"input_schema\": {\n * \"type\": \"object\",\n * \"properties\": {\n * \"ticker\": {\n * \"type\": \"string\",\n * \"description\": \"The stock ticker symbol, e.g. AAPL for Apple Inc.\"\n * }\n * },\n * \"required\": [\"ticker\"]\n * }\n * }\n * ]\n * ```\n *\n * And then asked the model \"What's the S&P 500 at today?\", the model might produce\n * `tool_use` content blocks in the response like this:\n *\n * ```json\n * [\n * {\n * \"type\": \"tool_use\",\n * \"id\": \"toolu_01D7FLrfh4GYq7yT1ULFeyMV\",\n * \"name\": \"get_stock_price\",\n * \"input\": { \"ticker\": \"^GSPC\" }\n * }\n * ]\n * ```\n *\n * You might then run your `get_stock_price` tool with `{\"ticker\": \"^GSPC\"}` as an\n * input, and return the following back to the model in a subsequent `user`\n * message:\n *\n * ```json\n * [\n * {\n * \"type\": \"tool_result\",\n * \"tool_use_id\": \"toolu_01D7FLrfh4GYq7yT1ULFeyMV\",\n * \"content\": \"259.75 USD\"\n * }\n * ]\n * ```\n *\n * Tools can be used for workflows that include running client-side tools and\n * functions, or more generally whenever you want the model to produce a particular\n * JSON structure of output.\n *\n * See our [guide](https://docs.anthropic.com/en/docs/tool-use) for more details.\n */\n tools?: Array<BetaToolUnion>;\n\n /**\n * Body param: Only sample from the top K options for each subsequent token.\n *\n * Used to remove \"long tail\" low probability responses.\n * [Learn more technical details here](https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277).\n *\n * Recommended for advanced use cases only. You usually only need to use\n * `temperature`.\n */\n top_k?: number;\n\n /**\n * Body param: Use nucleus sampling.\n *\n * In nucleus sampling, we compute the cumulative distribution over all the options\n * for each subsequent token in decreasing probability order and cut it off once it\n * reaches a particular probability specified by `top_p`. You should either alter\n * `temperature` or `top_p`, but not both.\n *\n * Recommended for advanced use cases only. You usually only need to use\n * `temperature`.\n */\n top_p?: number;\n\n /**\n * Header param: Optional header to specify the beta version(s) you want to use.\n */\n betas?: Array<BetaAPI.AnthropicBeta>;\n}\n\nexport namespace MessageCreateParams {\n export type MessageCreateParamsNonStreaming = MessagesMessagesAPI.MessageCreateParamsNonStreaming;\n export type MessageCreateParamsStreaming = MessagesMessagesAPI.MessageCreateParamsStreaming;\n}\n\nexport interface MessageCreateParamsNonStreaming extends MessageCreateParamsBase {\n /**\n * Body param: Whether to incrementally stream the response using server-sent\n * events.\n *\n * See [streaming](https://docs.anthropic.com/en/api/messages-streaming) for\n * details.\n */\n stream?: false;\n}\n\nexport interface MessageCreateParamsStreaming extends MessageCreateParamsBase {\n /**\n * Body param: Whether to incrementally stream the response using server-sent\n * events.\n *\n * See [streaming](https://docs.anthropic.com/en/api/messages-streaming) for\n * details.\n */\n stream: true;\n}\n\nexport interface MessageCountTokensParams {\n /**\n * Body param: Input messages.\n *\n * Our models are trained to operate on alternating `user` and `assistant`\n * conversational turns. When creating a new `Message`, you specify the prior\n * conversational turns with the `messages` parameter, and the model then generates\n * the next `Message` in the conversation. Consecutive `user` or `assistant` turns\n * in your request will be combined into a single turn.\n *\n * Each input message must be an object with a `role` and `content`. You can\n * specify a single `user`-role message, or you can include multiple `user` and\n * `assistant` messages.\n *\n * If the final message uses the `assistant` role, the response content will\n * continue immediately from the content in that message. This can be used to\n * constrain part of the model's response.\n *\n * Example with a single `user` message:\n *\n * ```json\n * [{ \"role\": \"user\", \"content\": \"Hello, Claude\" }]\n * ```\n *\n * Example with multiple conversational turns:\n *\n * ```json\n * [\n * { \"role\": \"user\", \"content\": \"Hello there.\" },\n * { \"role\": \"assistant\", \"content\": \"Hi, I'm Claude. How can I help you?\" },\n * { \"role\": \"user\", \"content\": \"Can you explain LLMs in plain English?\" }\n * ]\n * ```\n *\n * Example with a partially-filled response from Claude:\n *\n * ```json\n * [\n * {\n * \"role\": \"user\",\n * \"content\": \"What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun\"\n * },\n * { \"role\": \"assistant\", \"content\": \"The best answer is (\" }\n * ]\n * ```\n *\n * Each input message `content` may be either a single `string` or an array of\n * content blocks, where each block has a specific `type`. Using a `string` for\n * `content` is shorthand for an array of one content block of type `\"text\"`. The\n * following input messages are equivalent:\n *\n * ```json\n * { \"role\": \"user\", \"content\": \"Hello, Claude\" }\n * ```\n *\n * ```json\n * { \"role\": \"user\", \"content\": [{ \"type\": \"text\", \"text\": \"Hello, Claude\" }] }\n * ```\n *\n * Starting with Claude 3 models, you can also send image content blocks:\n *\n * ```json\n * {\n * \"role\": \"user\",\n * \"content\": [\n * {\n * \"type\": \"image\",\n * \"source\": {\n * \"type\": \"base64\",\n * \"media_type\": \"image/jpeg\",\n * \"data\": \"/9j/4AAQSkZJRg...\"\n * }\n * },\n * { \"type\": \"text\", \"text\": \"What is in this image?\" }\n * ]\n * }\n * ```\n *\n * We currently support the `base64` source type for images, and the `image/jpeg`,\n * `image/png`, `image/gif`, and `image/webp` media types.\n *\n * See [examples](https://docs.anthropic.com/en/api/messages-examples#vision) for\n * more input examples.\n *\n * Note that if you want to include a\n * [system prompt](https://docs.anthropic.com/en/docs/system-prompts), you can use\n * the top-level `system` parameter \u2014 there is no `\"system\"` role for input\n * messages in the Messages API.\n */\n messages: Array<BetaMessageParam>;\n\n /**\n * Body param: The model that will complete your prompt.\\n\\nSee\n * [models](https://docs.anthropic.com/en/docs/models-overview) for additional\n * details and options.\n */\n model: MessagesAPI.Model;\n\n /**\n * Body param: System prompt.\n *\n * A system prompt is a way of providing context and instructions to Claude, such\n * as specifying a particular goal or role. See our\n * [guide to system prompts](https://docs.anthropic.com/en/docs/system-prompts).\n */\n system?: string | Array<BetaTextBlockParam>;\n\n /**\n * Body param: Configuration for enabling Claude's extended thinking.\n *\n * When enabled, responses include `thinking` content blocks showing Claude's\n * thinking process before the final answer. Requires a minimum budget of 1,024\n * tokens and counts towards your `max_tokens` limit.\n *\n * See\n * [extended thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking)\n * for details.\n */\n thinking?: BetaThinkingConfigParam;\n\n /**\n * Body param: How the model should use the provided tools. The model can use a\n * specific tool, any available tool, decide by itself, or not use tools at all.\n */\n tool_choice?: BetaToolChoice;\n\n /**\n * Body param: Definitions of tools that the model may use.\n *\n * If you include `tools` in your API request, the model may return `tool_use`\n * content blocks that represent the model's use of those tools. You can then run\n * those tools using the tool input generated by the model and then optionally\n * return results back to the model using `tool_result` content blocks.\n *\n * Each tool definition includes:\n *\n * - `name`: Name of the tool.\n * - `description`: Optional, but strongly-recommended description of the tool.\n * - `input_schema`: [JSON schema](https://json-schema.org/draft/2020-12) for the\n * tool `input` shape that the model will produce in `tool_use` output content\n * blocks.\n *\n * For example, if you defined `tools` as:\n *\n * ```json\n * [\n * {\n * \"name\": \"get_stock_price\",\n * \"description\": \"Get the current stock price for a given ticker symbol.\",\n * \"input_schema\": {\n * \"type\": \"object\",\n * \"properties\": {\n * \"ticker\": {\n * \"type\": \"string\",\n * \"description\": \"The stock ticker symbol, e.g. AAPL for Apple Inc.\"\n * }\n * },\n * \"required\": [\"ticker\"]\n * }\n * }\n * ]\n * ```\n *\n * And then asked the model \"What's the S&P 500 at today?\", the model might produce\n * `tool_use` content blocks in the response like this:\n *\n * ```json\n * [\n * {\n * \"type\": \"tool_use\",\n * \"id\": \"toolu_01D7FLrfh4GYq7yT1ULFeyMV\",\n * \"name\": \"get_stock_price\",\n * \"input\": { \"ticker\": \"^GSPC\" }\n * }\n * ]\n * ```\n *\n * You might then run your `get_stock_price` tool with `{\"ticker\": \"^GSPC\"}` as an\n * input, and return the following back to the model in a subsequent `user`\n * message:\n *\n * ```json\n * [\n * {\n * \"type\": \"tool_result\",\n * \"tool_use_id\": \"toolu_01D7FLrfh4GYq7yT1ULFeyMV\",\n * \"content\": \"259.75 USD\"\n * }\n * ]\n * ```\n *\n * Tools can be used for workflows that include running client-side tools and\n * functions, or more generally whenever you want the model to produce a particular\n * JSON structure of output.\n *\n * See our [guide](https://docs.anthropic.com/en/docs/tool-use) for more details.\n */\n tools?: Array<\n | BetaTool\n | BetaToolComputerUse20241022\n | BetaToolBash20241022\n | BetaToolTextEditor20241022\n | BetaToolComputerUse20250124\n | BetaToolBash20250124\n | BetaToolTextEditor20250124\n >;\n\n /**\n * Header param: Optional header to specify the beta version(s) you want to use.\n */\n betas?: Array<BetaAPI.AnthropicBeta>;\n}\n\nMessages.Batches = Batches;\nMessages.BetaMessageBatchesPage = BetaMessageBatchesPage;\n\nexport declare namespace Messages {\n export {\n type BetaBase64ImageSource as BetaBase64ImageSource,\n type BetaBase64PDFBlock as BetaBase64PDFBlock,\n type BetaBase64PDFSource as BetaBase64PDFSource,\n type BetaCacheControlEphemeral as BetaCacheControlEphemeral,\n type BetaCitationCharLocation as BetaCitationCharLocation,\n type BetaCitationCharLocationParam as BetaCitationCharLocationParam,\n type BetaCitationContentBlockLocation as BetaCitationContentBlockLocation,\n type BetaCitationContentBlockLocationParam as BetaCitationContentBlockLocationParam,\n type BetaCitationPageLocation as BetaCitationPageLocation,\n type BetaCitationPageLocationParam as BetaCitationPageLocationParam,\n type BetaCitationsConfigParam as BetaCitationsConfigParam,\n type BetaCitationsDelta as BetaCitationsDelta,\n type BetaContentBlock as BetaContentBlock,\n type BetaContentBlockParam as BetaContentBlockParam,\n type BetaContentBlockSource as BetaContentBlockSource,\n type BetaContentBlockSourceContent as BetaContentBlockSourceContent,\n type BetaImageBlockParam as BetaImageBlockParam,\n type BetaInputJSONDelta as BetaInputJSONDelta,\n type BetaMessage as BetaMessage,\n type BetaMessageDeltaUsage as BetaMessageDeltaUsage,\n type BetaMessageParam as BetaMessageParam,\n type BetaMessageTokensCount as BetaMessageTokensCount,\n type BetaMetadata as BetaMetadata,\n type BetaPlainTextSource as BetaPlainTextSource,\n type BetaRawContentBlockDeltaEvent as BetaRawContentBlockDeltaEvent,\n type BetaRawContentBlockStartEvent as BetaRawContentBlockStartEvent,\n type BetaRawContentBlockStopEvent as BetaRawContentBlockStopEvent,\n type BetaRawMessageDeltaEvent as BetaRawMessageDeltaEvent,\n type BetaRawMessageStartEvent as BetaRawMessageStartEvent,\n type BetaRawMessageStopEvent as BetaRawMessageStopEvent,\n type BetaRawMessageStreamEvent as BetaRawMessageStreamEvent,\n type BetaRedactedThinkingBlock as BetaRedactedThinkingBlock,\n type BetaRedactedThinkingBlockParam as BetaRedactedThinkingBlockParam,\n type BetaSignatureDelta as BetaSignatureDelta,\n type BetaTextBlock as BetaTextBlock,\n type BetaTextBlockParam as BetaTextBlockParam,\n type BetaTextCitation as BetaTextCitation,\n type BetaTextCitationParam as BetaTextCitationParam,\n type BetaTextDelta as BetaTextDelta,\n type BetaThinkingBlock as BetaThinkingBlock,\n type BetaThinkingBlockParam as BetaThinkingBlockParam,\n type BetaThinkingConfigDisabled as BetaThinkingConfigDisabled,\n type BetaThinkingConfigEnabled as BetaThinkingConfigEnabled,\n type BetaThinkingConfigParam as BetaThinkingConfigParam,\n type BetaThinkingDelta as BetaThinkingDelta,\n type BetaTool as BetaTool,\n type BetaToolBash20241022 as BetaToolBash20241022,\n type BetaToolBash20250124 as BetaToolBash20250124,\n type BetaToolChoice as BetaToolChoice,\n type BetaToolChoiceAny as BetaToolChoiceAny,\n type BetaToolChoiceAuto as BetaToolChoiceAuto,\n type BetaToolChoiceNone as BetaToolChoiceNone,\n type BetaToolChoiceTool as BetaToolChoiceTool,\n type BetaToolComputerUse20241022 as BetaToolComputerUse20241022,\n type BetaToolComputerUse20250124 as BetaToolComputerUse20250124,\n type BetaToolResultBlockParam as BetaToolResultBlockParam,\n type BetaToolTextEditor20241022 as BetaToolTextEditor20241022,\n type BetaToolTextEditor20250124 as BetaToolTextEditor20250124,\n type BetaToolUnion as BetaToolUnion,\n type BetaToolUseBlock as BetaToolUseBlock,\n type BetaToolUseBlockParam as BetaToolUseBlockParam,\n type BetaURLImageSource as BetaURLImageSource,\n type BetaURLPDFSource as BetaURLPDFSource,\n type BetaUsage as BetaUsage,\n type MessageCreateParams as MessageCreateParams,\n type MessageCreateParamsNonStreaming as MessageCreateParamsNonStreaming,\n type MessageCreateParamsStreaming as MessageCreateParamsStreaming,\n type MessageCountTokensParams as MessageCountTokensParams,\n };\n\n export {\n Batches as Batches,\n type BetaDeletedMessageBatch as BetaDeletedMessageBatch,\n type BetaMessageBatch as BetaMessageBatch,\n type BetaMessageBatchCanceledResult as BetaMessageBatchCanceledResult,\n type BetaMessageBatchErroredResult as BetaMessageBatchErroredResult,\n type BetaMessageBatchExpiredResult as BetaMessageBatchExpiredResult,\n type BetaMessageBatchIndividualResponse as BetaMessageBatchIndividualResponse,\n type BetaMessageBatchRequestCounts as BetaMessageBatchRequestCounts,\n type BetaMessageBatchResult as BetaMessageBatchResult,\n type BetaMessageBatchSucceededResult as BetaMessageBatchSucceededResult,\n BetaMessageBatchesPage as BetaMessageBatchesPage,\n type BatchCreateParams as BatchCreateParams,\n type BatchRetrieveParams as BatchRetrieveParams,\n type BatchListParams as BatchListParams,\n type BatchDeleteParams as BatchDeleteParams,\n type BatchCancelParams as BatchCancelParams,\n type BatchResultsParams as BatchResultsParams,\n };\n}\n", "// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\n\nimport { APIResource } from \"../../resource.js\";\nimport * as ModelsAPI from \"./models.js\";\nimport { BetaModelInfo, BetaModelInfosPage, ModelListParams, Models } from \"./models.js\";\nimport * as MessagesAPI from \"./messages/messages.js\";\nimport {\n BetaBase64ImageSource,\n BetaBase64PDFBlock,\n BetaBase64PDFSource,\n BetaCacheControlEphemeral,\n BetaCitationCharLocation,\n BetaCitationCharLocationParam,\n BetaCitationContentBlockLocation,\n BetaCitationContentBlockLocationParam,\n BetaCitationPageLocation,\n BetaCitationPageLocationParam,\n BetaCitationsConfigParam,\n BetaCitationsDelta,\n BetaContentBlock,\n BetaContentBlockParam,\n BetaContentBlockSource,\n BetaContentBlockSourceContent,\n BetaImageBlockParam,\n BetaInputJSONDelta,\n BetaMessage,\n BetaMessageDeltaUsage,\n BetaMessageParam,\n BetaMessageTokensCount,\n BetaMetadata,\n BetaPlainTextSource,\n BetaRawContentBlockDeltaEvent,\n BetaRawContentBlockStartEvent,\n BetaRawContentBlockStopEvent,\n BetaRawMessageDeltaEvent,\n BetaRawMessageStartEvent,\n BetaRawMessageStopEvent,\n BetaRawMessageStreamEvent,\n BetaRedactedThinkingBlock,\n BetaRedactedThinkingBlockParam,\n BetaSignatureDelta,\n BetaTextBlock,\n BetaTextBlockParam,\n BetaTextCitation,\n BetaTextCitationParam,\n BetaTextDelta,\n BetaThinkingBlock,\n BetaThinkingBlockParam,\n BetaThinkingConfigDisabled,\n BetaThinkingConfigEnabled,\n BetaThinkingConfigParam,\n BetaThinkingDelta,\n BetaTool,\n BetaToolBash20241022,\n BetaToolBash20250124,\n BetaToolChoice,\n BetaToolChoiceAny,\n BetaToolChoiceAuto,\n BetaToolChoiceNone,\n BetaToolChoiceTool,\n BetaToolComputerUse20241022,\n BetaToolComputerUse20250124,\n BetaToolResultBlockParam,\n BetaToolTextEditor20241022,\n BetaToolTextEditor20250124,\n BetaToolUnion,\n BetaToolUseBlock,\n BetaToolUseBlockParam,\n BetaURLImageSource,\n BetaURLPDFSource,\n BetaUsage,\n MessageCountTokensParams,\n MessageCreateParams,\n MessageCreateParamsNonStreaming,\n MessageCreateParamsStreaming,\n Messages,\n} from \"./messages/messages.js\";\n\nexport class Beta extends APIResource {\n models: ModelsAPI.Models = new ModelsAPI.Models(this._client);\n messages: MessagesAPI.Messages = new MessagesAPI.Messages(this._client);\n}\n\nexport type AnthropicBeta =\n | (string & {})\n | 'message-batches-2024-09-24'\n | 'prompt-caching-2024-07-31'\n | 'computer-use-2024-10-22'\n | 'computer-use-2025-01-24'\n | 'pdfs-2024-09-25'\n | 'token-counting-2024-11-01'\n | 'token-efficient-tools-2025-02-19'\n | 'output-128k-2025-02-19';\n\nexport interface BetaAPIError {\n message: string;\n\n type: 'api_error';\n}\n\nexport interface BetaAuthenticationError {\n message: string;\n\n type: 'authentication_error';\n}\n\nexport interface BetaBillingError {\n message: string;\n\n type: 'billing_error';\n}\n\nexport type BetaError =\n | BetaInvalidRequestError\n | BetaAuthenticationError\n | BetaBillingError\n | BetaPermissionError\n | BetaNotFoundError\n | BetaRateLimitError\n | BetaGatewayTimeoutError\n | BetaAPIError\n | BetaOverloadedError;\n\nexport interface BetaErrorResponse {\n error: BetaError;\n\n type: 'error';\n}\n\nexport interface BetaGatewayTimeoutError {\n message: string;\n\n type: 'timeout_error';\n}\n\nexport interface BetaInvalidRequestError {\n message: string;\n\n type: 'invalid_request_error';\n}\n\nexport interface BetaNotFoundError {\n message: string;\n\n type: 'not_found_error';\n}\n\nexport interface BetaOverloadedError {\n message: string;\n\n type: 'overloaded_error';\n}\n\nexport interface BetaPermissionError {\n message: string;\n\n type: 'permission_error';\n}\n\nexport interface BetaRateLimitError {\n message: string;\n\n type: 'rate_limit_error';\n}\n\nBeta.Models = Models;\nBeta.BetaModelInfosPage = BetaModelInfosPage;\nBeta.Messages = Messages;\n\nexport declare namespace Beta {\n export {\n type AnthropicBeta as AnthropicBeta,\n type BetaAPIError as BetaAPIError,\n type BetaAuthenticationError as BetaAuthenticationError,\n type BetaBillingError as BetaBillingError,\n type BetaError as BetaError,\n type BetaErrorResponse as BetaErrorResponse,\n type BetaGatewayTimeoutError as BetaGatewayTimeoutError,\n type BetaInvalidRequestError as BetaInvalidRequestError,\n type BetaNotFoundError as BetaNotFoundError,\n type BetaOverloadedError as BetaOverloadedError,\n type BetaPermissionError as BetaPermissionError,\n type BetaRateLimitError as BetaRateLimitError,\n };\n\n export {\n Models as Models,\n type BetaModelInfo as BetaModelInfo,\n BetaModelInfosPage as BetaModelInfosPage,\n type ModelListParams as ModelListParams,\n };\n\n export {\n Messages as Messages,\n type BetaBase64ImageSource as BetaBase64ImageSource,\n type BetaBase64PDFBlock as BetaBase64PDFBlock,\n type BetaBase64PDFSource as BetaBase64PDFSource,\n type BetaCacheControlEphemeral as BetaCacheControlEphemeral,\n type BetaCitationCharLocation as BetaCitationCharLocation,\n type BetaCitationCharLocationParam as BetaCitationCharLocationParam,\n type BetaCitationContentBlockLocation as BetaCitationContentBlockLocation,\n type BetaCitationContentBlockLocationParam as BetaCitationContentBlockLocationParam,\n type BetaCitationPageLocation as BetaCitationPageLocation,\n type BetaCitationPageLocationParam as BetaCitationPageLocationParam,\n type BetaCitationsConfigParam as BetaCitationsConfigParam,\n type BetaCitationsDelta as BetaCitationsDelta,\n type BetaContentBlock as BetaContentBlock,\n type BetaContentBlockParam as BetaContentBlockParam,\n type BetaContentBlockSource as BetaContentBlockSource,\n type BetaContentBlockSourceContent as BetaContentBlockSourceContent,\n type BetaImageBlockParam as BetaImageBlockParam,\n type BetaInputJSONDelta as BetaInputJSONDelta,\n type BetaMessage as BetaMessage,\n type BetaMessageDeltaUsage as BetaMessageDeltaUsage,\n type BetaMessageParam as BetaMessageParam,\n type BetaMessageTokensCount as BetaMessageTokensCount,\n type BetaMetadata as BetaMetadata,\n type BetaPlainTextSource as BetaPlainTextSource,\n type BetaRawContentBlockDeltaEvent as BetaRawContentBlockDeltaEvent,\n type BetaRawContentBlockStartEvent as BetaRawContentBlockStartEvent,\n type BetaRawContentBlockStopEvent as BetaRawContentBlockStopEvent,\n type BetaRawMessageDeltaEvent as BetaRawMessageDeltaEvent,\n type BetaRawMessageStartEvent as BetaRawMessageStartEvent,\n type BetaRawMessageStopEvent as BetaRawMessageStopEvent,\n type BetaRawMessageStreamEvent as BetaRawMessageStreamEvent,\n type BetaRedactedThinkingBlock as BetaRedactedThinkingBlock,\n type BetaRedactedThinkingBlockParam as BetaRedactedThinkingBlockParam,\n type BetaSignatureDelta as BetaSignatureDelta,\n type BetaTextBlock as BetaTextBlock,\n type BetaTextBlockParam as BetaTextBlockParam,\n type BetaTextCitation as BetaTextCitation,\n type BetaTextCitationParam as BetaTextCitationParam,\n type BetaTextDelta as BetaTextDelta,\n type BetaThinkingBlock as BetaThinkingBlock,\n type BetaThinkingBlockParam as BetaThinkingBlockParam,\n type BetaThinkingConfigDisabled as BetaThinkingConfigDisabled,\n type BetaThinkingConfigEnabled as BetaThinkingConfigEnabled,\n type BetaThinkingConfigParam as BetaThinkingConfigParam,\n type BetaThinkingDelta as BetaThinkingDelta,\n type BetaTool as BetaTool,\n type BetaToolBash20241022 as BetaToolBash20241022,\n type BetaToolBash20250124 as BetaToolBash20250124,\n type BetaToolChoice as BetaToolChoice,\n type BetaToolChoiceAny as BetaToolChoiceAny,\n type BetaToolChoiceAuto as BetaToolChoiceAuto,\n type BetaToolChoiceNone as BetaToolChoiceNone,\n type BetaToolChoiceTool as BetaToolChoiceTool,\n type BetaToolComputerUse20241022 as BetaToolComputerUse20241022,\n type BetaToolComputerUse20250124 as BetaToolComputerUse20250124,\n type BetaToolResultBlockParam as BetaToolResultBlockParam,\n type BetaToolTextEditor20241022 as BetaToolTextEditor20241022,\n type BetaToolTextEditor20250124 as BetaToolTextEditor20250124,\n type BetaToolUnion as BetaToolUnion,\n type BetaToolUseBlock as BetaToolUseBlock,\n type BetaToolUseBlockParam as BetaToolUseBlockParam,\n type BetaURLImageSource as BetaURLImageSource,\n type BetaURLPDFSource as BetaURLPDFSource,\n type BetaUsage as BetaUsage,\n type MessageCreateParams as MessageCreateParams,\n type MessageCreateParamsNonStreaming as MessageCreateParamsNonStreaming,\n type MessageCreateParamsStreaming as MessageCreateParamsStreaming,\n type MessageCountTokensParams as MessageCountTokensParams,\n };\n}\n", "// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\n\nimport { APIResource } from \"../resource.js\";\nimport { APIPromise } from \"../core.js\";\nimport * as Core from \"../core.js\";\nimport * as CompletionsAPI from \"./completions.js\";\nimport * as MessagesAPI from \"./messages/messages.js\";\nimport { Stream } from \"../streaming.js\";\n\nexport class Completions extends APIResource {\n /**\n * [Legacy] Create a Text Completion.\n *\n * The Text Completions API is a legacy API. We recommend using the\n * [Messages API](https://docs.anthropic.com/en/api/messages) going forward.\n *\n * Future models and features will not be compatible with Text Completions. See our\n * [migration guide](https://docs.anthropic.com/en/api/migrating-from-text-completions-to-messages)\n * for guidance in migrating from Text Completions to Messages.\n */\n create(body: CompletionCreateParamsNonStreaming, options?: Core.RequestOptions): APIPromise<Completion>;\n create(\n body: CompletionCreateParamsStreaming,\n options?: Core.RequestOptions,\n ): APIPromise<Stream<Completion>>;\n create(\n body: CompletionCreateParamsBase,\n options?: Core.RequestOptions,\n ): APIPromise<Stream<Completion> | Completion>;\n create(\n body: CompletionCreateParams,\n options?: Core.RequestOptions,\n ): APIPromise<Completion> | APIPromise<Stream<Completion>> {\n return this._client.post('/v1/complete', {\n body,\n timeout: (this._client as any)._options.timeout ?? 600000,\n ...options,\n stream: body.stream ?? false,\n }) as APIPromise<Completion> | APIPromise<Stream<Completion>>;\n }\n}\n\nexport interface Completion {\n /**\n * Unique object identifier.\n *\n * The format and length of IDs may change over time.\n */\n id: string;\n\n /**\n * The resulting completion up to and excluding the stop sequences.\n */\n completion: string;\n\n /**\n * The model that will complete your prompt.\\n\\nSee\n * [models](https://docs.anthropic.com/en/docs/models-overview) for additional\n * details and options.\n */\n model: MessagesAPI.Model;\n\n /**\n * The reason that we stopped.\n *\n * This may be one the following values:\n *\n * - `\"stop_sequence\"`: we reached a stop sequence \u2014 either provided by you via the\n * `stop_sequences` parameter, or a stop sequence built into the model\n * - `\"max_tokens\"`: we exceeded `max_tokens_to_sample` or the model's maximum\n */\n stop_reason: string | null;\n\n /**\n * Object type.\n *\n * For Text Completions, this is always `\"completion\"`.\n */\n type: 'completion';\n}\n\nexport type CompletionCreateParams = CompletionCreateParamsNonStreaming | CompletionCreateParamsStreaming;\n\nexport interface CompletionCreateParamsBase {\n /**\n * The maximum number of tokens to generate before stopping.\n *\n * Note that our models may stop _before_ reaching this maximum. This parameter\n * only specifies the absolute maximum number of tokens to generate.\n */\n max_tokens_to_sample: number;\n\n /**\n * The model that will complete your prompt.\\n\\nSee\n * [models](https://docs.anthropic.com/en/docs/models-overview) for additional\n * details and options.\n */\n model: MessagesAPI.Model;\n\n /**\n * The prompt that you want Claude to complete.\n *\n * For proper response generation you will need to format your prompt using\n * alternating `\\n\\nHuman:` and `\\n\\nAssistant:` conversational turns. For example:\n *\n * ```\n * \"\\n\\nHuman: {userQuestion}\\n\\nAssistant:\"\n * ```\n *\n * See [prompt validation](https://docs.anthropic.com/en/api/prompt-validation) and\n * our guide to\n * [prompt design](https://docs.anthropic.com/en/docs/intro-to-prompting) for more\n * details.\n */\n prompt: string;\n\n /**\n * An object describing metadata about the request.\n */\n metadata?: MessagesAPI.Metadata;\n\n /**\n * Sequences that will cause the model to stop generating.\n *\n * Our models stop on `\"\\n\\nHuman:\"`, and may include additional built-in stop\n * sequences in the future. By providing the stop_sequences parameter, you may\n * include additional strings that will cause the model to stop generating.\n */\n stop_sequences?: Array<string>;\n\n /**\n * Whether to incrementally stream the response using server-sent events.\n *\n * See [streaming](https://docs.anthropic.com/en/api/streaming) for details.\n */\n stream?: boolean;\n\n /**\n * Amount of randomness injected into the response.\n *\n * Defaults to `1.0`. Ranges from `0.0` to `1.0`. Use `temperature` closer to `0.0`\n * for analytical / multiple choice, and closer to `1.0` for creative and\n * generative tasks.\n *\n * Note that even with `temperature` of `0.0`, the results will not be fully\n * deterministic.\n */\n temperature?: number;\n\n /**\n * Only sample from the top K options for each subsequent token.\n *\n * Used to remove \"long tail\" low probability responses.\n * [Learn more technical details here](https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277).\n *\n * Recommended for advanced use cases only. You usually only need to use\n * `temperature`.\n */\n top_k?: number;\n\n /**\n * Use nucleus sampling.\n *\n * In nucleus sampling, we compute the cumulative distribution over all the options\n * for each subsequent token in decreasing probability order and cut it off once it\n * reaches a particular probability specified by `top_p`. You should either alter\n * `temperature` or `top_p`, but not both.\n *\n * Recommended for advanced use cases only. You usually only need to use\n * `temperature`.\n */\n top_p?: number;\n}\n\nexport namespace CompletionCreateParams {\n /**\n * @deprecated use `Anthropic.Messages.Metadata` instead\n */\n export type Metadata = MessagesAPI.Metadata;\n\n export type CompletionCreateParamsNonStreaming = CompletionsAPI.CompletionCreateParamsNonStreaming;\n export type CompletionCreateParamsStreaming = CompletionsAPI.CompletionCreateParamsStreaming;\n}\n\nexport interface CompletionCreateParamsNonStreaming extends CompletionCreateParamsBase {\n /**\n * Whether to incrementally stream the response using server-sent events.\n *\n * See [streaming](https://docs.anthropic.com/en/api/streaming) for details.\n */\n stream?: false;\n}\n\nexport interface CompletionCreateParamsStreaming extends CompletionCreateParamsBase {\n /**\n * Whether to incrementally stream the response using server-sent events.\n *\n * See [streaming](https://docs.anthropic.com/en/api/streaming) for details.\n */\n stream: true;\n}\n\nexport declare namespace Completions {\n export {\n type Completion as Completion,\n type CompletionCreateParams as CompletionCreateParams,\n type CompletionCreateParamsNonStreaming as CompletionCreateParamsNonStreaming,\n type CompletionCreateParamsStreaming as CompletionCreateParamsStreaming,\n };\n}\n", "// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\n\nimport { APIResource } from \"../../resource.js\";\nimport { isRequestOptions } from \"../../core.js\";\nimport * as Core from \"../../core.js\";\nimport * as Shared from \"../shared.js\";\nimport * as MessagesAPI from \"./messages.js\";\nimport { Page, type PageParams } from \"../../pagination.js\";\nimport { JSONLDecoder } from \"../../internal/decoders/jsonl.js\";\nimport { AnthropicError } from \"../../error.js\";\n\nexport class Batches extends APIResource {\n /**\n * Send a batch of Message creation requests.\n *\n * The Message Batches API can be used to process multiple Messages API requests at\n * once. Once a Message Batch is created, it begins processing immediately. Batches\n * can take up to 24 hours to complete.\n *\n * Learn more about the Message Batches API in our\n * [user guide](/en/docs/build-with-claude/batch-processing)\n */\n create(body: BatchCreateParams, options?: Core.RequestOptions): Core.APIPromise<MessageBatch> {\n return this._client.post('/v1/messages/batches', { body, ...options });\n }\n\n /**\n * This endpoint is idempotent and can be used to poll for Message Batch\n * completion. To access the results of a Message Batch, make a request to the\n * `results_url` field in the response.\n *\n * Learn more about the Message Batches API in our\n * [user guide](/en/docs/build-with-claude/batch-processing)\n */\n retrieve(messageBatchId: string, options?: Core.RequestOptions): Core.APIPromise<MessageBatch> {\n return this._client.get(`/v1/messages/batches/${messageBatchId}`, options);\n }\n\n /**\n * List all Message Batches within a Workspace. Most recently created batches are\n * returned first.\n *\n * Learn more about the Message Batches API in our\n * [user guide](/en/docs/build-with-claude/batch-processing)\n */\n list(\n query?: BatchListParams,\n options?: Core.RequestOptions,\n ): Core.PagePromise<MessageBatchesPage, MessageBatch>;\n list(options?: Core.RequestOptions): Core.PagePromise<MessageBatchesPage, MessageBatch>;\n list(\n query: BatchListParams | Core.RequestOptions = {},\n options?: Core.RequestOptions,\n ): Core.PagePromise<MessageBatchesPage, MessageBatch> {\n if (isRequestOptions(query)) {\n return this.list({}, query);\n }\n return this._client.getAPIList('/v1/messages/batches', MessageBatchesPage, { query, ...options });\n }\n\n /**\n * Delete a Message Batch.\n *\n * Message Batches can only be deleted once they've finished processing. If you'd\n * like to delete an in-progress batch, you must first cancel it.\n *\n * Learn more about the Message Batches API in our\n * [user guide](/en/docs/build-with-claude/batch-processing)\n */\n delete(messageBatchId: string, options?: Core.RequestOptions): Core.APIPromise<DeletedMessageBatch> {\n return this._client.delete(`/v1/messages/batches/${messageBatchId}`, options);\n }\n\n /**\n * Batches may be canceled any time before processing ends. Once cancellation is\n * initiated, the batch enters a `canceling` state, at which time the system may\n * complete any in-progress, non-interruptible requests before finalizing\n * cancellation.\n *\n * The number of canceled requests is specified in `request_counts`. To determine\n * which requests were canceled, check the individual results within the batch.\n * Note that cancellation may not result in any canceled requests if they were\n * non-interruptible.\n *\n * Learn more about the Message Batches API in our\n * [user guide](/en/docs/build-with-claude/batch-processing)\n */\n cancel(messageBatchId: string, options?: Core.RequestOptions): Core.APIPromise<MessageBatch> {\n return this._client.post(`/v1/messages/batches/${messageBatchId}/cancel`, options);\n }\n\n /**\n * Streams the results of a Message Batch as a `.jsonl` file.\n *\n * Each line in the file is a JSON object containing the result of a single request\n * in the Message Batch. Results are not guaranteed to be in the same order as\n * requests. Use the `custom_id` field to match results to requests.\n *\n * Learn more about the Message Batches API in our\n * [user guide](/en/docs/build-with-claude/batch-processing)\n */\n async results(\n messageBatchId: string,\n options?: Core.RequestOptions,\n ): Promise<JSONLDecoder<MessageBatchIndividualResponse>> {\n const batch = await this.retrieve(messageBatchId);\n if (!batch.results_url) {\n throw new AnthropicError(\n `No batch \\`results_url\\`; Has it finished processing? ${batch.processing_status} - ${batch.id}`,\n );\n }\n\n return this._client\n .get(batch.results_url, {\n ...options,\n headers: {\n Accept: 'application/binary',\n ...options?.headers,\n },\n __binaryResponse: true,\n })\n ._thenUnwrap((_, props) => JSONLDecoder.fromResponse(props.response, props.controller));\n }\n}\n\nexport class MessageBatchesPage extends Page<MessageBatch> {}\n\nexport interface DeletedMessageBatch {\n /**\n * ID of the Message Batch.\n */\n id: string;\n\n /**\n * Deleted object type.\n *\n * For Message Batches, this is always `\"message_batch_deleted\"`.\n */\n type: 'message_batch_deleted';\n}\n\nexport interface MessageBatch {\n /**\n * Unique object identifier.\n *\n * The format and length of IDs may change over time.\n */\n id: string;\n\n /**\n * RFC 3339 datetime string representing the time at which the Message Batch was\n * archived and its results became unavailable.\n */\n archived_at: string | null;\n\n /**\n * RFC 3339 datetime string representing the time at which cancellation was\n * initiated for the Message Batch. Specified only if cancellation was initiated.\n */\n cancel_initiated_at: string | null;\n\n /**\n * RFC 3339 datetime string representing the time at which the Message Batch was\n * created.\n */\n created_at: string;\n\n /**\n * RFC 3339 datetime string representing the time at which processing for the\n * Message Batch ended. Specified only once processing ends.\n *\n * Processing ends when every request in a Message Batch has either succeeded,\n * errored, canceled, or expired.\n */\n ended_at: string | null;\n\n /**\n * RFC 3339 datetime string representing the time at which the Message Batch will\n * expire and end processing, which is 24 hours after creation.\n */\n expires_at: string;\n\n /**\n * Processing status of the Message Batch.\n */\n processing_status: 'in_progress' | 'canceling' | 'ended';\n\n /**\n * Tallies requests within the Message Batch, categorized by their status.\n *\n * Requests start as `processing` and move to one of the other statuses only once\n * processing of the entire batch ends. The sum of all values always matches the\n * total number of requests in the batch.\n */\n request_counts: MessageBatchRequestCounts;\n\n /**\n * URL to a `.jsonl` file containing the results of the Message Batch requests.\n * Specified only once processing ends.\n *\n * Results in the file are not guaranteed to be in the same order as requests. Use\n * the `custom_id` field to match results to requests.\n */\n results_url: string | null;\n\n /**\n * Object type.\n *\n * For Message Batches, this is always `\"message_batch\"`.\n */\n type: 'message_batch';\n}\n\nexport interface MessageBatchCanceledResult {\n type: 'canceled';\n}\n\nexport interface MessageBatchErroredResult {\n error: Shared.ErrorResponse;\n\n type: 'errored';\n}\n\nexport interface MessageBatchExpiredResult {\n type: 'expired';\n}\n\n/**\n * This is a single line in the response `.jsonl` file and does not represent the\n * response as a whole.\n */\nexport interface MessageBatchIndividualResponse {\n /**\n * Developer-provided ID created for each request in a Message Batch. Useful for\n * matching results to requests, as results may be given out of request order.\n *\n * Must be unique for each request within the Message Batch.\n */\n custom_id: string;\n\n /**\n * Processing result for this request.\n *\n * Contains a Message output if processing was successful, an error response if\n * processing failed, or the reason why processing was not attempted, such as\n * cancellation or expiration.\n */\n result: MessageBatchResult;\n}\n\nexport interface MessageBatchRequestCounts {\n /**\n * Number of requests in the Message Batch that have been canceled.\n *\n * This is zero until processing of the entire Message Batch has ended.\n */\n canceled: number;\n\n /**\n * Number of requests in the Message Batch that encountered an error.\n *\n * This is zero until processing of the entire Message Batch has ended.\n */\n errored: number;\n\n /**\n * Number of requests in the Message Batch that have expired.\n *\n * This is zero until processing of the entire Message Batch has ended.\n */\n expired: number;\n\n /**\n * Number of requests in the Message Batch that are processing.\n */\n processing: number;\n\n /**\n * Number of requests in the Message Batch that have completed successfully.\n *\n * This is zero until processing of the entire Message Batch has ended.\n */\n succeeded: number;\n}\n\n/**\n * Processing result for this request.\n *\n * Contains a Message output if processing was successful, an error response if\n * processing failed, or the reason why processing was not attempted, such as\n * cancellation or expiration.\n */\nexport type MessageBatchResult =\n | MessageBatchSucceededResult\n | MessageBatchErroredResult\n | MessageBatchCanceledResult\n | MessageBatchExpiredResult;\n\nexport interface MessageBatchSucceededResult {\n message: MessagesAPI.Message;\n\n type: 'succeeded';\n}\n\nexport interface BatchCreateParams {\n /**\n * List of requests for prompt completion. Each is an individual request to create\n * a Message.\n */\n requests: Array<BatchCreateParams.Request>;\n}\n\nexport namespace BatchCreateParams {\n export interface Request {\n /**\n * Developer-provided ID created for each request in a Message Batch. Useful for\n * matching results to requests, as results may be given out of request order.\n *\n * Must be unique for each request within the Message Batch.\n */\n custom_id: string;\n\n /**\n * Messages API creation parameters for the individual request.\n *\n * See the [Messages API reference](/en/api/messages) for full documentation on\n * available parameters.\n */\n params: MessagesAPI.MessageCreateParamsNonStreaming;\n }\n}\n\nexport interface BatchListParams extends PageParams {}\n\nBatches.MessageBatchesPage = MessageBatchesPage;\n\nexport declare namespace Batches {\n export {\n type DeletedMessageBatch as DeletedMessageBatch,\n type MessageBatch as MessageBatch,\n type MessageBatchCanceledResult as MessageBatchCanceledResult,\n type MessageBatchErroredResult as MessageBatchErroredResult,\n type MessageBatchExpiredResult as MessageBatchExpiredResult,\n type MessageBatchIndividualResponse as MessageBatchIndividualResponse,\n type MessageBatchRequestCounts as MessageBatchRequestCounts,\n type MessageBatchResult as MessageBatchResult,\n type MessageBatchSucceededResult as MessageBatchSucceededResult,\n MessageBatchesPage as MessageBatchesPage,\n type BatchCreateParams as BatchCreateParams,\n type BatchListParams as BatchListParams,\n };\n}\n", "import * as Core from \"../core.js\";\nimport { AnthropicError, APIUserAbortError } from \"../error.js\";\nimport {\n type ContentBlock,\n Messages,\n type Message,\n type MessageStreamEvent,\n type MessageParam,\n type MessageCreateParams,\n type MessageCreateParamsBase,\n type TextBlock,\n type TextCitation,\n} from \"../resources/messages.js\";\nimport { type ReadableStream, type Response } from \"../_shims/index.js\";\nimport { Stream } from \"../streaming.js\";\nimport { partialParse } from \"../_vendor/partial-json-parser/parser.js\";\n\nexport interface MessageStreamEvents {\n connect: () => void;\n streamEvent: (event: MessageStreamEvent, snapshot: Message) => void;\n text: (textDelta: string, textSnapshot: string) => void;\n citation: (citation: TextCitation, citationsSnapshot: TextCitation[]) => void;\n inputJson: (partialJson: string, jsonSnapshot: unknown) => void;\n thinking: (thinkingDelta: string, thinkingSnapshot: string) => void;\n signature: (signature: string) => void;\n message: (message: Message) => void;\n contentBlock: (content: ContentBlock) => void;\n finalMessage: (message: Message) => void;\n error: (error: AnthropicError) => void;\n abort: (error: APIUserAbortError) => void;\n end: () => void;\n}\n\ntype MessageStreamEventListeners<Event extends keyof MessageStreamEvents> = {\n listener: MessageStreamEvents[Event];\n once?: boolean;\n}[];\n\nconst JSON_BUF_PROPERTY = '__json_buf';\n\nexport class MessageStream implements AsyncIterable<MessageStreamEvent> {\n messages: MessageParam[] = [];\n receivedMessages: Message[] = [];\n #currentMessageSnapshot: Message | undefined;\n\n controller: AbortController = new AbortController();\n\n #connectedPromise: Promise<Response | null>;\n #resolveConnectedPromise: (response: Response | null) => void = () => {};\n #rejectConnectedPromise: (error: AnthropicError) => void = () => {};\n\n #endPromise: Promise<void>;\n #resolveEndPromise: () => void = () => {};\n #rejectEndPromise: (error: AnthropicError) => void = () => {};\n\n #listeners: { [Event in keyof MessageStreamEvents]?: MessageStreamEventListeners<Event> } = {};\n\n #ended = false;\n #errored = false;\n #aborted = false;\n #catchingPromiseCreated = false;\n #response: Response | null | undefined;\n #request_id: string | null | undefined;\n\n constructor() {\n this.#connectedPromise = new Promise<Response | null>((resolve, reject) => {\n this.#resolveConnectedPromise = resolve;\n this.#rejectConnectedPromise = reject;\n });\n\n this.#endPromise = new Promise<void>((resolve, reject) => {\n this.#resolveEndPromise = resolve;\n this.#rejectEndPromise = reject;\n });\n\n // Don't let these promises cause unhandled rejection errors.\n // we will manually cause an unhandled rejection error later\n // if the user hasn't registered any error listener or called\n // any promise-returning method.\n this.#connectedPromise.catch(() => {});\n this.#endPromise.catch(() => {});\n }\n\n get response(): Response | null | undefined {\n return this.#response;\n }\n\n get request_id(): string | null | undefined {\n return this.#request_id;\n }\n\n /**\n * Returns the `MessageStream` data, the raw `Response` instance and the ID of the request,\n * returned vie the `request-id` header which is useful for debugging requests and resporting\n * issues to Anthropic.\n *\n * This is the same as the `APIPromise.withResponse()` method.\n *\n * This method will raise an error if you created the stream using `MessageStream.fromReadableStream`\n * as no `Response` is available.\n */\n async withResponse(): Promise<{\n data: MessageStream;\n response: Response;\n request_id: string | null | undefined;\n }> {\n const response = await this.#connectedPromise;\n if (!response) {\n throw new Error('Could not resolve a `Response` object');\n }\n\n return {\n data: this,\n response,\n request_id: response.headers.get('request-id'),\n };\n }\n\n /**\n * Intended for use on the frontend, consuming a stream produced with\n * `.toReadableStream()` on the backend.\n *\n * Note that messages sent to the model do not appear in `.on('message')`\n * in this context.\n */\n static fromReadableStream(stream: ReadableStream): MessageStream {\n const runner = new MessageStream();\n runner._run(() => runner._fromReadableStream(stream));\n return runner;\n }\n\n static createMessage(\n messages: Messages,\n params: MessageCreateParamsBase,\n options?: Core.RequestOptions,\n ): MessageStream {\n const runner = new MessageStream();\n for (const message of params.messages) {\n runner._addMessageParam(message);\n }\n runner._run(() =>\n runner._createMessage(\n messages,\n { ...params, stream: true },\n { ...options, headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'stream' } },\n ),\n );\n return runner;\n }\n\n protected _run(executor: () => Promise<any>) {\n executor().then(() => {\n this._emitFinal();\n this._emit('end');\n }, this.#handleError);\n }\n\n protected _addMessageParam(message: MessageParam) {\n this.messages.push(message);\n }\n\n protected _addMessage(message: Message, emit = true) {\n this.receivedMessages.push(message);\n if (emit) {\n this._emit('message', message);\n }\n }\n\n protected async _createMessage(\n messages: Messages,\n params: MessageCreateParams,\n options?: Core.RequestOptions,\n ): Promise<void> {\n const signal = options?.signal;\n if (signal) {\n if (signal.aborted) this.controller.abort();\n signal.addEventListener('abort', () => this.controller.abort());\n }\n this.#beginRequest();\n const { response, data: stream } = await messages\n .create({ ...params, stream: true }, { ...options, signal: this.controller.signal })\n .withResponse();\n this._connected(response);\n for await (const event of stream) {\n this.#addStreamEvent(event);\n }\n if (stream.controller.signal?.aborted) {\n throw new APIUserAbortError();\n }\n this.#endRequest();\n }\n\n protected _connected(response: Response | null) {\n if (this.ended) return;\n this.#response = response;\n this.#request_id = response?.headers.get('request-id');\n this.#resolveConnectedPromise(response);\n this._emit('connect');\n }\n\n get ended(): boolean {\n return this.#ended;\n }\n\n get errored(): boolean {\n return this.#errored;\n }\n\n get aborted(): boolean {\n return this.#aborted;\n }\n\n abort() {\n this.controller.abort();\n }\n\n /**\n * Adds the listener function to the end of the listeners array for the event.\n * No checks are made to see if the listener has already been added. Multiple calls passing\n * the same combination of event and listener will result in the listener being added, and\n * called, multiple times.\n * @returns this MessageStream, so that calls can be chained\n */\n on<Event extends keyof MessageStreamEvents>(event: Event, listener: MessageStreamEvents[Event]): this {\n const listeners: MessageStreamEventListeners<Event> =\n this.#listeners[event] || (this.#listeners[event] = []);\n listeners.push({ listener });\n return this;\n }\n\n /**\n * Removes the specified listener from the listener array for the event.\n * off() will remove, at most, one instance of a listener from the listener array. If any single\n * listener has been added multiple times to the listener array for the specified event, then\n * off() must be called multiple times to remove each instance.\n * @returns this MessageStream, so that calls can be chained\n */\n off<Event extends keyof MessageStreamEvents>(event: Event, listener: MessageStreamEvents[Event]): this {\n const listeners = this.#listeners[event];\n if (!listeners) return this;\n const index = listeners.findIndex((l) => l.listener === listener);\n if (index >= 0) listeners.splice(index, 1);\n return this;\n }\n\n /**\n * Adds a one-time listener function for the event. The next time the event is triggered,\n * this listener is removed and then invoked.\n * @returns this MessageStream, so that calls can be chained\n */\n once<Event extends keyof MessageStreamEvents>(event: Event, listener: MessageStreamEvents[Event]): this {\n const listeners: MessageStreamEventListeners<Event> =\n this.#listeners[event] || (this.#listeners[event] = []);\n listeners.push({ listener, once: true });\n return this;\n }\n\n /**\n * This is similar to `.once()`, but returns a Promise that resolves the next time\n * the event is triggered, instead of calling a listener callback.\n * @returns a Promise that resolves the next time given event is triggered,\n * or rejects if an error is emitted. (If you request the 'error' event,\n * returns a promise that resolves with the error).\n *\n * Example:\n *\n * const message = await stream.emitted('message') // rejects if the stream errors\n */\n emitted<Event extends keyof MessageStreamEvents>(\n event: Event,\n ): Promise<\n Parameters<MessageStreamEvents[Event]> extends [infer Param] ? Param\n : Parameters<MessageStreamEvents[Event]> extends [] ? void\n : Parameters<MessageStreamEvents[Event]>\n > {\n return new Promise((resolve, reject) => {\n this.#catchingPromiseCreated = true;\n if (event !== 'error') this.once('error', reject);\n this.once(event, resolve as any);\n });\n }\n\n async done(): Promise<void> {\n this.#catchingPromiseCreated = true;\n await this.#endPromise;\n }\n\n get currentMessage(): Message | undefined {\n return this.#currentMessageSnapshot;\n }\n\n #getFinalMessage(): Message {\n if (this.receivedMessages.length === 0) {\n throw new AnthropicError('stream ended without producing a Message with role=assistant');\n }\n return this.receivedMessages.at(-1)!;\n }\n\n /**\n * @returns a promise that resolves with the the final assistant Message response,\n * or rejects if an error occurred or the stream ended prematurely without producing a Message.\n */\n async finalMessage(): Promise<Message> {\n await this.done();\n return this.#getFinalMessage();\n }\n\n #getFinalText(): string {\n if (this.receivedMessages.length === 0) {\n throw new AnthropicError('stream ended without producing a Message with role=assistant');\n }\n const textBlocks = this.receivedMessages\n .at(-1)!\n .content.filter((block): block is TextBlock => block.type === 'text')\n .map((block) => block.text);\n if (textBlocks.length === 0) {\n throw new AnthropicError('stream ended without producing a content block with type=text');\n }\n return textBlocks.join(' ');\n }\n\n /**\n * @returns a promise that resolves with the the final assistant Message's text response, concatenated\n * together if there are more than one text blocks.\n * Rejects if an error occurred or the stream ended prematurely without producing a Message.\n */\n async finalText(): Promise<string> {\n await this.done();\n return this.#getFinalText();\n }\n\n #handleError = (error: unknown) => {\n this.#errored = true;\n if (error instanceof Error && error.name === 'AbortError') {\n error = new APIUserAbortError();\n }\n if (error instanceof APIUserAbortError) {\n this.#aborted = true;\n return this._emit('abort', error);\n }\n if (error instanceof AnthropicError) {\n return this._emit('error', error);\n }\n if (error instanceof Error) {\n const anthropicError: AnthropicError = new AnthropicError(error.message);\n // @ts-ignore\n anthropicError.cause = error;\n return this._emit('error', anthropicError);\n }\n return this._emit('error', new AnthropicError(String(error)));\n };\n\n protected _emit<Event extends keyof MessageStreamEvents>(\n event: Event,\n ...args: Parameters<MessageStreamEvents[Event]>\n ) {\n // make sure we don't emit any MessageStreamEvents after end\n if (this.#ended) return;\n\n if (event === 'end') {\n this.#ended = true;\n this.#resolveEndPromise();\n }\n\n const listeners: MessageStreamEventListeners<Event> | undefined = this.#listeners[event];\n if (listeners) {\n this.#listeners[event] = listeners.filter((l) => !l.once) as any;\n listeners.forEach(({ listener }: any) => listener(...args));\n }\n\n if (event === 'abort') {\n const error = args[0] as APIUserAbortError;\n if (!this.#catchingPromiseCreated && !listeners?.length) {\n Promise.reject(error);\n }\n this.#rejectConnectedPromise(error);\n this.#rejectEndPromise(error);\n this._emit('end');\n return;\n }\n\n if (event === 'error') {\n // NOTE: _emit('error', error) should only be called from #handleError().\n\n const error = args[0] as AnthropicError;\n if (!this.#catchingPromiseCreated && !listeners?.length) {\n // Trigger an unhandled rejection if the user hasn't registered any error handlers.\n // If you are seeing stack traces here, make sure to handle errors via either:\n // - runner.on('error', () => ...)\n // - await runner.done()\n // - await runner.final...()\n // - etc.\n Promise.reject(error);\n }\n this.#rejectConnectedPromise(error);\n this.#rejectEndPromise(error);\n this._emit('end');\n }\n }\n\n protected _emitFinal() {\n const finalMessage = this.receivedMessages.at(-1);\n if (finalMessage) {\n this._emit('finalMessage', this.#getFinalMessage());\n }\n }\n\n #beginRequest() {\n if (this.ended) return;\n this.#currentMessageSnapshot = undefined;\n }\n #addStreamEvent(event: MessageStreamEvent) {\n if (this.ended) return;\n const messageSnapshot = this.#accumulateMessage(event);\n this._emit('streamEvent', event, messageSnapshot);\n\n switch (event.type) {\n case 'content_block_delta': {\n const content = messageSnapshot.content.at(-1)!;\n switch (event.delta.type) {\n case 'text_delta': {\n if (content.type === 'text') {\n this._emit('text', event.delta.text, content.text || '');\n }\n break;\n }\n case 'citations_delta': {\n if (content.type === 'text') {\n this._emit('citation', event.delta.citation, content.citations ?? []);\n }\n break;\n }\n case 'input_json_delta': {\n if (content.type === 'tool_use' && content.input) {\n this._emit('inputJson', event.delta.partial_json, content.input);\n }\n break;\n }\n case 'thinking_delta': {\n if (content.type === 'thinking') {\n this._emit('thinking', event.delta.thinking, content.thinking);\n }\n break;\n }\n case 'signature_delta': {\n if (content.type === 'thinking') {\n this._emit('signature', content.signature);\n }\n break;\n }\n default:\n checkNever(event.delta);\n }\n break;\n }\n case 'message_stop': {\n this._addMessageParam(messageSnapshot);\n this._addMessage(messageSnapshot, true);\n break;\n }\n case 'content_block_stop': {\n this._emit('contentBlock', messageSnapshot.content.at(-1)!);\n break;\n }\n case 'message_start': {\n this.#currentMessageSnapshot = messageSnapshot;\n break;\n }\n case 'content_block_start':\n case 'message_delta':\n break;\n }\n }\n #endRequest(): Message {\n if (this.ended) {\n throw new AnthropicError(`stream has ended, this shouldn't happen`);\n }\n const snapshot = this.#currentMessageSnapshot;\n if (!snapshot) {\n throw new AnthropicError(`request ended without sending any chunks`);\n }\n this.#currentMessageSnapshot = undefined;\n return snapshot;\n }\n\n protected async _fromReadableStream(\n readableStream: ReadableStream,\n options?: Core.RequestOptions,\n ): Promise<void> {\n const signal = options?.signal;\n if (signal) {\n if (signal.aborted) this.controller.abort();\n signal.addEventListener('abort', () => this.controller.abort());\n }\n this.#beginRequest();\n this._connected(null);\n const stream = Stream.fromReadableStream<MessageStreamEvent>(readableStream, this.controller);\n for await (const event of stream) {\n this.#addStreamEvent(event);\n }\n if (stream.controller.signal?.aborted) {\n throw new APIUserAbortError();\n }\n this.#endRequest();\n }\n\n /**\n * Mutates this.#currentMessage with the current event. Handling the accumulation of multiple messages\n * will be needed to be handled by the caller, this method will throw if you try to accumulate for multiple\n * messages.\n */\n #accumulateMessage(event: MessageStreamEvent): Message {\n let snapshot = this.#currentMessageSnapshot;\n\n if (event.type === 'message_start') {\n if (snapshot) {\n throw new AnthropicError(`Unexpected event order, got ${event.type} before receiving \"message_stop\"`);\n }\n return event.message;\n }\n\n if (!snapshot) {\n throw new AnthropicError(`Unexpected event order, got ${event.type} before \"message_start\"`);\n }\n\n switch (event.type) {\n case 'message_stop':\n return snapshot;\n case 'message_delta':\n snapshot.stop_reason = event.delta.stop_reason;\n snapshot.stop_sequence = event.delta.stop_sequence;\n snapshot.usage.output_tokens = event.usage.output_tokens;\n return snapshot;\n case 'content_block_start':\n snapshot.content.push(event.content_block);\n return snapshot;\n case 'content_block_delta': {\n const snapshotContent = snapshot.content.at(event.index);\n\n switch (event.delta.type) {\n case 'text_delta': {\n if (snapshotContent?.type === 'text') {\n snapshotContent.text += event.delta.text;\n }\n break;\n }\n case 'citations_delta': {\n if (snapshotContent?.type === 'text') {\n snapshotContent.citations ??= [];\n snapshotContent.citations.push(event.delta.citation);\n }\n break;\n }\n case 'input_json_delta': {\n if (snapshotContent?.type === 'tool_use') {\n // we need to keep track of the raw JSON string as well so that we can\n // re-parse it for each delta, for now we just store it as an untyped\n // non-enumerable property on the snapshot\n let jsonBuf = (snapshotContent as any)[JSON_BUF_PROPERTY] || '';\n jsonBuf += event.delta.partial_json;\n\n Object.defineProperty(snapshotContent, JSON_BUF_PROPERTY, {\n value: jsonBuf,\n enumerable: false,\n writable: true,\n });\n\n if (jsonBuf) {\n snapshotContent.input = partialParse(jsonBuf);\n }\n }\n break;\n }\n case 'thinking_delta': {\n if (snapshotContent?.type === 'thinking') {\n snapshotContent.thinking += event.delta.thinking;\n }\n break;\n }\n case 'signature_delta': {\n if (snapshotContent?.type === 'thinking') {\n snapshotContent.signature = event.delta.signature;\n }\n break;\n }\n default:\n checkNever(event.delta);\n }\n\n return snapshot;\n }\n case 'content_block_stop':\n return snapshot;\n }\n }\n\n [Symbol.asyncIterator](): AsyncIterator<MessageStreamEvent> {\n const pushQueue: MessageStreamEvent[] = [];\n const readQueue: {\n resolve: (chunk: MessageStreamEvent | undefined) => void;\n reject: (error: unknown) => void;\n }[] = [];\n let done = false;\n\n this.on('streamEvent', (event) => {\n const reader = readQueue.shift();\n if (reader) {\n reader.resolve(event);\n } else {\n pushQueue.push(event);\n }\n });\n\n this.on('end', () => {\n done = true;\n for (const reader of readQueue) {\n reader.resolve(undefined);\n }\n readQueue.length = 0;\n });\n\n this.on('abort', (err) => {\n done = true;\n for (const reader of readQueue) {\n reader.reject(err);\n }\n readQueue.length = 0;\n });\n\n this.on('error', (err) => {\n done = true;\n for (const reader of readQueue) {\n reader.reject(err);\n }\n readQueue.length = 0;\n });\n\n return {\n next: async (): Promise<IteratorResult<MessageStreamEvent>> => {\n if (!pushQueue.length) {\n if (done) {\n return { value: undefined, done: true };\n }\n return new Promise<MessageStreamEvent | undefined>((resolve, reject) =>\n readQueue.push({ resolve, reject }),\n ).then((chunk) => (chunk ? { value: chunk, done: false } : { value: undefined, done: true }));\n }\n const chunk = pushQueue.shift()!;\n return { value: chunk, done: false };\n },\n return: async () => {\n this.abort();\n return { value: undefined, done: true };\n },\n };\n }\n\n toReadableStream(): ReadableStream {\n const stream = new Stream(this[Symbol.asyncIterator].bind(this), this.controller);\n return stream.toReadableStream();\n }\n}\n\n// used to ensure exhaustive case matching without throwing a runtime error\nfunction checkNever(x: never) {}\n", "// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\n\nimport { APIResource } from \"../../resource.js\";\nimport { APIPromise } from \"../../core.js\";\nimport * as Core from \"../../core.js\";\nimport * as MessagesAPI from \"./messages.js\";\nimport * as BatchesAPI from \"./batches.js\";\nimport {\n BatchCreateParams,\n BatchListParams,\n Batches,\n DeletedMessageBatch,\n MessageBatch,\n MessageBatchCanceledResult,\n MessageBatchErroredResult,\n MessageBatchExpiredResult,\n MessageBatchIndividualResponse,\n MessageBatchRequestCounts,\n MessageBatchResult,\n MessageBatchSucceededResult,\n MessageBatchesPage,\n} from \"./batches.js\";\nimport { Stream } from \"../../streaming.js\";\nimport { MessageStream } from \"../../lib/MessageStream.js\";\n\nexport { MessageStream } from \"../../lib/MessageStream.js\";\n\nexport class Messages extends APIResource {\n batches: BatchesAPI.Batches = new BatchesAPI.Batches(this._client);\n\n /**\n * Send a structured list of input messages with text and/or image content, and the\n * model will generate the next message in the conversation.\n *\n * The Messages API can be used for either single queries or stateless multi-turn\n * conversations.\n *\n * Learn more about the Messages API in our [user guide](/en/docs/initial-setup)\n */\n create(body: MessageCreateParamsNonStreaming, options?: Core.RequestOptions): APIPromise<Message>;\n create(\n body: MessageCreateParamsStreaming,\n options?: Core.RequestOptions,\n ): APIPromise<Stream<RawMessageStreamEvent>>;\n create(\n body: MessageCreateParamsBase,\n options?: Core.RequestOptions,\n ): APIPromise<Stream<RawMessageStreamEvent> | Message>;\n create(\n body: MessageCreateParams,\n options?: Core.RequestOptions,\n ): APIPromise<Message> | APIPromise<Stream<RawMessageStreamEvent>> {\n if (body.model in DEPRECATED_MODELS) {\n console.warn(\n `The model '${body.model}' is deprecated and will reach end-of-life on ${\n DEPRECATED_MODELS[body.model]\n }\\nPlease migrate to a newer model. Visit https://docs.anthropic.com/en/docs/resources/model-deprecations for more information.`,\n );\n }\n return this._client.post('/v1/messages', {\n body,\n timeout:\n (this._client as any)._options.timeout ??\n (body.stream ? 600000 : this._client._calculateNonstreamingTimeout(body.max_tokens)),\n ...options,\n stream: body.stream ?? false,\n }) as APIPromise<Message> | APIPromise<Stream<RawMessageStreamEvent>>;\n }\n\n /**\n * Create a Message stream\n */\n stream(body: MessageStreamParams, options?: Core.RequestOptions): MessageStream {\n return MessageStream.createMessage(this, body, options);\n }\n\n /**\n * Count the number of tokens in a Message.\n *\n * The Token Count API can be used to count the number of tokens in a Message,\n * including tools, images, and documents, without creating it.\n *\n * Learn more about token counting in our\n * [user guide](/en/docs/build-with-claude/token-counting)\n */\n countTokens(\n body: MessageCountTokensParams,\n options?: Core.RequestOptions,\n ): Core.APIPromise<MessageTokensCount> {\n return this._client.post('/v1/messages/count_tokens', { body, ...options });\n }\n}\n\nexport interface Base64ImageSource {\n data: string;\n\n media_type: 'image/jpeg' | 'image/png' | 'image/gif' | 'image/webp';\n\n type: 'base64';\n}\n\nexport interface Base64PDFSource {\n data: string;\n\n media_type: 'application/pdf';\n\n type: 'base64';\n}\n\nexport interface CacheControlEphemeral {\n type: 'ephemeral';\n}\n\nexport interface CitationCharLocation {\n cited_text: string;\n\n document_index: number;\n\n document_title: string | null;\n\n end_char_index: number;\n\n start_char_index: number;\n\n type: 'char_location';\n}\n\nexport interface CitationCharLocationParam {\n cited_text: string;\n\n document_index: number;\n\n document_title: string | null;\n\n end_char_index: number;\n\n start_char_index: number;\n\n type: 'char_location';\n}\n\nexport interface CitationContentBlockLocation {\n cited_text: string;\n\n document_index: number;\n\n document_title: string | null;\n\n end_block_index: number;\n\n start_block_index: number;\n\n type: 'content_block_location';\n}\n\nexport interface CitationContentBlockLocationParam {\n cited_text: string;\n\n document_index: number;\n\n document_title: string | null;\n\n end_block_index: number;\n\n start_block_index: number;\n\n type: 'content_block_location';\n}\n\nexport interface CitationPageLocation {\n cited_text: string;\n\n document_index: number;\n\n document_title: string | null;\n\n end_page_number: number;\n\n start_page_number: number;\n\n type: 'page_location';\n}\n\nexport interface CitationPageLocationParam {\n cited_text: string;\n\n document_index: number;\n\n document_title: string | null;\n\n end_page_number: number;\n\n start_page_number: number;\n\n type: 'page_location';\n}\n\nexport interface CitationsConfigParam {\n enabled?: boolean;\n}\n\nexport interface CitationsDelta {\n citation: CitationCharLocation | CitationPageLocation | CitationContentBlockLocation;\n\n type: 'citations_delta';\n}\n\nexport type ContentBlock = TextBlock | ToolUseBlock | ThinkingBlock | RedactedThinkingBlock;\n\nexport type ContentBlockDeltaEvent = RawContentBlockDeltaEvent;\n\nexport type ContentBlockParam =\n | TextBlockParam\n | ImageBlockParam\n | ToolUseBlockParam\n | ToolResultBlockParam\n | DocumentBlockParam\n | ThinkingBlockParam\n | RedactedThinkingBlockParam;\n\nexport interface ContentBlockSource {\n content: string | Array<ContentBlockSourceContent>;\n\n type: 'content';\n}\n\nexport type ContentBlockSourceContent = TextBlockParam | ImageBlockParam;\n\nexport type ContentBlockStartEvent = RawContentBlockStartEvent;\n\nexport type ContentBlockStopEvent = RawContentBlockStopEvent;\n\nexport interface DocumentBlockParam {\n source: Base64PDFSource | PlainTextSource | ContentBlockSource | URLPDFSource;\n\n type: 'document';\n\n cache_control?: CacheControlEphemeral | null;\n\n citations?: CitationsConfigParam;\n\n context?: string | null;\n\n title?: string | null;\n}\n\nexport interface ImageBlockParam {\n source: Base64ImageSource | URLImageSource;\n\n type: 'image';\n\n cache_control?: CacheControlEphemeral | null;\n}\n\nexport type InputJsonDelta = InputJSONDelta;\n\nexport interface InputJSONDelta {\n partial_json: string;\n\n type: 'input_json_delta';\n}\n\nexport interface Message {\n /**\n * Unique object identifier.\n *\n * The format and length of IDs may change over time.\n */\n id: string;\n\n /**\n * Content generated by the model.\n *\n * This is an array of content blocks, each of which has a `type` that determines\n * its shape.\n *\n * Example:\n *\n * ```json\n * [{ \"type\": \"text\", \"text\": \"Hi, I'm Claude.\" }]\n * ```\n *\n * If the request input `messages` ended with an `assistant` turn, then the\n * response `content` will continue directly from that last turn. You can use this\n * to constrain the model's output.\n *\n * For example, if the input `messages` were:\n *\n * ```json\n * [\n * {\n * \"role\": \"user\",\n * \"content\": \"What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun\"\n * },\n * { \"role\": \"assistant\", \"content\": \"The best answer is (\" }\n * ]\n * ```\n *\n * Then the response `content` might be:\n *\n * ```json\n * [{ \"type\": \"text\", \"text\": \"B)\" }]\n * ```\n */\n content: Array<ContentBlock>;\n\n /**\n * The model that will complete your prompt.\\n\\nSee\n * [models](https://docs.anthropic.com/en/docs/models-overview) for additional\n * details and options.\n */\n model: Model;\n\n /**\n * Conversational role of the generated message.\n *\n * This will always be `\"assistant\"`.\n */\n role: 'assistant';\n\n /**\n * The reason that we stopped.\n *\n * This may be one the following values:\n *\n * - `\"end_turn\"`: the model reached a natural stopping point\n * - `\"max_tokens\"`: we exceeded the requested `max_tokens` or the model's maximum\n * - `\"stop_sequence\"`: one of your provided custom `stop_sequences` was generated\n * - `\"tool_use\"`: the model invoked one or more tools\n *\n * In non-streaming mode this value is always non-null. In streaming mode, it is\n * null in the `message_start` event and non-null otherwise.\n */\n stop_reason: 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use' | null;\n\n /**\n * Which custom stop sequence was generated, if any.\n *\n * This value will be a non-null string if one of your custom stop sequences was\n * generated.\n */\n stop_sequence: string | null;\n\n /**\n * Object type.\n *\n * For Messages, this is always `\"message\"`.\n */\n type: 'message';\n\n /**\n * Billing and rate-limit usage.\n *\n * Anthropic's API bills and rate-limits by token counts, as tokens represent the\n * underlying cost to our systems.\n *\n * Under the hood, the API transforms requests into a format suitable for the\n * model. The model's output then goes through a parsing stage before becoming an\n * API response. As a result, the token counts in `usage` will not match one-to-one\n * with the exact visible content of an API request or response.\n *\n * For example, `output_tokens` will be non-zero, even for an empty string response\n * from Claude.\n *\n * Total input tokens in a request is the summation of `input_tokens`,\n * `cache_creation_input_tokens`, and `cache_read_input_tokens`.\n */\n usage: Usage;\n}\n\nexport type MessageCountTokensTool = Tool | ToolBash20250124 | ToolTextEditor20250124;\n\nexport type MessageDeltaEvent = RawMessageDeltaEvent;\n\nexport interface MessageDeltaUsage {\n /**\n * The cumulative number of output tokens which were used.\n */\n output_tokens: number;\n}\n\nexport interface MessageParam {\n content: string | Array<ContentBlockParam>;\n\n role: 'user' | 'assistant';\n}\n\nexport type MessageStartEvent = RawMessageStartEvent;\n\nexport type MessageStopEvent = RawMessageStopEvent;\n\nexport type MessageStreamEvent = RawMessageStreamEvent;\n\nexport interface MessageTokensCount {\n /**\n * The total number of tokens across the provided list of messages, system prompt,\n * and tools.\n */\n input_tokens: number;\n}\n\nexport interface Metadata {\n /**\n * An external identifier for the user who is associated with the request.\n *\n * This should be a uuid, hash value, or other opaque identifier. Anthropic may use\n * this id to help detect abuse. Do not include any identifying information such as\n * name, email address, or phone number.\n */\n user_id?: string | null;\n}\n\n/**\n * The model that will complete your prompt.\\n\\nSee\n * [models](https://docs.anthropic.com/en/docs/models-overview) for additional\n * details and options.\n */\nexport type Model =\n | 'claude-3-7-sonnet-latest'\n | 'claude-3-7-sonnet-20250219'\n | 'claude-3-5-haiku-latest'\n | 'claude-3-5-haiku-20241022'\n | 'claude-3-5-sonnet-latest'\n | 'claude-3-5-sonnet-20241022'\n | 'claude-3-5-sonnet-20240620'\n | 'claude-3-opus-latest'\n | 'claude-3-opus-20240229'\n | 'claude-3-sonnet-20240229'\n | 'claude-3-haiku-20240307'\n | 'claude-2.1'\n | 'claude-2.0'\n | (string & {});\n\nconst DEPRECATED_MODELS: {\n [K in Model]?: string;\n} = {\n 'claude-1.3': 'November 6th, 2024',\n 'claude-1.3-100k': 'November 6th, 2024',\n 'claude-instant-1.1': 'November 6th, 2024',\n 'claude-instant-1.1-100k': 'November 6th, 2024',\n 'claude-instant-1.2': 'November 6th, 2024',\n 'claude-3-sonnet-20240229': 'July 21st, 2025',\n 'claude-2.1': 'July 21st, 2025',\n 'claude-2.0': 'July 21st, 2025',\n};\n\nexport interface PlainTextSource {\n data: string;\n\n media_type: 'text/plain';\n\n type: 'text';\n}\n\nexport interface RawContentBlockDeltaEvent {\n delta: TextDelta | InputJSONDelta | CitationsDelta | ThinkingDelta | SignatureDelta;\n\n index: number;\n\n type: 'content_block_delta';\n}\n\nexport interface RawContentBlockStartEvent {\n content_block: TextBlock | ToolUseBlock | ThinkingBlock | RedactedThinkingBlock;\n\n index: number;\n\n type: 'content_block_start';\n}\n\nexport interface RawContentBlockStopEvent {\n index: number;\n\n type: 'content_block_stop';\n}\n\nexport interface RawMessageDeltaEvent {\n delta: RawMessageDeltaEvent.Delta;\n\n type: 'message_delta';\n\n /**\n * Billing and rate-limit usage.\n *\n * Anthropic's API bills and rate-limits by token counts, as tokens represent the\n * underlying cost to our systems.\n *\n * Under the hood, the API transforms requests into a format suitable for the\n * model. The model's output then goes through a parsing stage before becoming an\n * API response. As a result, the token counts in `usage` will not match one-to-one\n * with the exact visible content of an API request or response.\n *\n * For example, `output_tokens` will be non-zero, even for an empty string response\n * from Claude.\n *\n * Total input tokens in a request is the summation of `input_tokens`,\n * `cache_creation_input_tokens`, and `cache_read_input_tokens`.\n */\n usage: MessageDeltaUsage;\n}\n\nexport namespace RawMessageDeltaEvent {\n export interface Delta {\n stop_reason: 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use' | null;\n\n stop_sequence: string | null;\n }\n}\n\nexport interface RawMessageStartEvent {\n message: Message;\n\n type: 'message_start';\n}\n\nexport interface RawMessageStopEvent {\n type: 'message_stop';\n}\n\nexport type RawMessageStreamEvent =\n | RawMessageStartEvent\n | RawMessageDeltaEvent\n | RawMessageStopEvent\n | RawContentBlockStartEvent\n | RawContentBlockDeltaEvent\n | RawContentBlockStopEvent;\n\nexport interface RedactedThinkingBlock {\n data: string;\n\n type: 'redacted_thinking';\n}\n\nexport interface RedactedThinkingBlockParam {\n data: string;\n\n type: 'redacted_thinking';\n}\n\nexport interface SignatureDelta {\n signature: string;\n\n type: 'signature_delta';\n}\n\nexport interface TextBlock {\n /**\n * Citations supporting the text block.\n *\n * The type of citation returned will depend on the type of document being cited.\n * Citing a PDF results in `page_location`, plain text results in `char_location`,\n * and content document results in `content_block_location`.\n */\n citations: Array<TextCitation> | null;\n\n text: string;\n\n type: 'text';\n}\n\nexport interface TextBlockParam {\n text: string;\n\n type: 'text';\n\n cache_control?: CacheControlEphemeral | null;\n\n citations?: Array<TextCitationParam> | null;\n}\n\nexport type TextCitation = CitationCharLocation | CitationPageLocation | CitationContentBlockLocation;\n\nexport type TextCitationParam =\n | CitationCharLocationParam\n | CitationPageLocationParam\n | CitationContentBlockLocationParam;\n\nexport interface TextDelta {\n text: string;\n\n type: 'text_delta';\n}\n\nexport interface ThinkingBlock {\n signature: string;\n\n thinking: string;\n\n type: 'thinking';\n}\n\nexport interface ThinkingBlockParam {\n signature: string;\n\n thinking: string;\n\n type: 'thinking';\n}\n\nexport interface ThinkingConfigDisabled {\n type: 'disabled';\n}\n\nexport interface ThinkingConfigEnabled {\n /**\n * Determines how many tokens Claude can use for its internal reasoning process.\n * Larger budgets can enable more thorough analysis for complex problems, improving\n * response quality.\n *\n * Must be \u22651024 and less than `max_tokens`.\n *\n * See\n * [extended thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking)\n * for details.\n */\n budget_tokens: number;\n\n type: 'enabled';\n}\n\n/**\n * Configuration for enabling Claude's extended thinking.\n *\n * When enabled, responses include `thinking` content blocks showing Claude's\n * thinking process before the final answer. Requires a minimum budget of 1,024\n * tokens and counts towards your `max_tokens` limit.\n *\n * See\n * [extended thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking)\n * for details.\n */\nexport type ThinkingConfigParam = ThinkingConfigEnabled | ThinkingConfigDisabled;\n\nexport interface ThinkingDelta {\n thinking: string;\n\n type: 'thinking_delta';\n}\n\nexport interface Tool {\n /**\n * [JSON schema](https://json-schema.org/draft/2020-12) for this tool's input.\n *\n * This defines the shape of the `input` that your tool accepts and that the model\n * will produce.\n */\n input_schema: Tool.InputSchema;\n\n /**\n * Name of the tool.\n *\n * This is how the tool will be called by the model and in tool_use blocks.\n */\n name: string;\n\n cache_control?: CacheControlEphemeral | null;\n\n /**\n * Description of what this tool does.\n *\n * Tool descriptions should be as detailed as possible. The more information that\n * the model has about what the tool is and how to use it, the better it will\n * perform. You can use natural language descriptions to reinforce important\n * aspects of the tool input JSON schema.\n */\n description?: string;\n}\n\nexport namespace Tool {\n /**\n * [JSON schema](https://json-schema.org/draft/2020-12) for this tool's input.\n *\n * This defines the shape of the `input` that your tool accepts and that the model\n * will produce.\n */\n export interface InputSchema {\n type: 'object';\n\n properties?: unknown | null;\n [k: string]: unknown;\n }\n}\n\nexport interface ToolBash20250124 {\n /**\n * Name of the tool.\n *\n * This is how the tool will be called by the model and in tool_use blocks.\n */\n name: 'bash';\n\n type: 'bash_20250124';\n\n cache_control?: CacheControlEphemeral | null;\n}\n\n/**\n * How the model should use the provided tools. The model can use a specific tool,\n * any available tool, decide by itself, or not use tools at all.\n */\nexport type ToolChoice = ToolChoiceAuto | ToolChoiceAny | ToolChoiceTool | ToolChoiceNone;\n\n/**\n * The model will use any available tools.\n */\nexport interface ToolChoiceAny {\n type: 'any';\n\n /**\n * Whether to disable parallel tool use.\n *\n * Defaults to `false`. If set to `true`, the model will output exactly one tool\n * use.\n */\n disable_parallel_tool_use?: boolean;\n}\n\n/**\n * The model will automatically decide whether to use tools.\n */\nexport interface ToolChoiceAuto {\n type: 'auto';\n\n /**\n * Whether to disable parallel tool use.\n *\n * Defaults to `false`. If set to `true`, the model will output at most one tool\n * use.\n */\n disable_parallel_tool_use?: boolean;\n}\n\n/**\n * The model will not be allowed to use tools.\n */\nexport interface ToolChoiceNone {\n type: 'none';\n}\n\n/**\n * The model will use the specified tool with `tool_choice.name`.\n */\nexport interface ToolChoiceTool {\n /**\n * The name of the tool to use.\n */\n name: string;\n\n type: 'tool';\n\n /**\n * Whether to disable parallel tool use.\n *\n * Defaults to `false`. If set to `true`, the model will output exactly one tool\n * use.\n */\n disable_parallel_tool_use?: boolean;\n}\n\nexport interface ToolResultBlockParam {\n tool_use_id: string;\n\n type: 'tool_result';\n\n cache_control?: CacheControlEphemeral | null;\n\n content?: string | Array<TextBlockParam | ImageBlockParam>;\n\n is_error?: boolean;\n}\n\nexport interface ToolTextEditor20250124 {\n /**\n * Name of the tool.\n *\n * This is how the tool will be called by the model and in tool_use blocks.\n */\n name: 'str_replace_editor';\n\n type: 'text_editor_20250124';\n\n cache_control?: CacheControlEphemeral | null;\n}\n\nexport type ToolUnion = Tool | ToolBash20250124 | ToolTextEditor20250124;\n\nexport interface ToolUseBlock {\n id: string;\n\n input: unknown;\n\n name: string;\n\n type: 'tool_use';\n}\n\nexport interface ToolUseBlockParam {\n id: string;\n\n input: unknown;\n\n name: string;\n\n type: 'tool_use';\n\n cache_control?: CacheControlEphemeral | null;\n}\n\nexport interface URLImageSource {\n type: 'url';\n\n url: string;\n}\n\nexport interface URLPDFSource {\n type: 'url';\n\n url: string;\n}\n\nexport interface Usage {\n /**\n * The number of input tokens used to create the cache entry.\n */\n cache_creation_input_tokens: number | null;\n\n /**\n * The number of input tokens read from the cache.\n */\n cache_read_input_tokens: number | null;\n\n /**\n * The number of input tokens which were used.\n */\n input_tokens: number;\n\n /**\n * The number of output tokens which were used.\n */\n output_tokens: number;\n}\n\nexport type MessageCreateParams = MessageCreateParamsNonStreaming | MessageCreateParamsStreaming;\n\nexport interface MessageCreateParamsBase {\n /**\n * The maximum number of tokens to generate before stopping.\n *\n * Note that our models may stop _before_ reaching this maximum. This parameter\n * only specifies the absolute maximum number of tokens to generate.\n *\n * Different models have different maximum values for this parameter. See\n * [models](https://docs.anthropic.com/en/docs/models-overview) for details.\n */\n max_tokens: number;\n\n /**\n * Input messages.\n *\n * Our models are trained to operate on alternating `user` and `assistant`\n * conversational turns. When creating a new `Message`, you specify the prior\n * conversational turns with the `messages` parameter, and the model then generates\n * the next `Message` in the conversation. Consecutive `user` or `assistant` turns\n * in your request will be combined into a single turn.\n *\n * Each input message must be an object with a `role` and `content`. You can\n * specify a single `user`-role message, or you can include multiple `user` and\n * `assistant` messages.\n *\n * If the final message uses the `assistant` role, the response content will\n * continue immediately from the content in that message. This can be used to\n * constrain part of the model's response.\n *\n * Example with a single `user` message:\n *\n * ```json\n * [{ \"role\": \"user\", \"content\": \"Hello, Claude\" }]\n * ```\n *\n * Example with multiple conversational turns:\n *\n * ```json\n * [\n * { \"role\": \"user\", \"content\": \"Hello there.\" },\n * { \"role\": \"assistant\", \"content\": \"Hi, I'm Claude. How can I help you?\" },\n * { \"role\": \"user\", \"content\": \"Can you explain LLMs in plain English?\" }\n * ]\n * ```\n *\n * Example with a partially-filled response from Claude:\n *\n * ```json\n * [\n * {\n * \"role\": \"user\",\n * \"content\": \"What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun\"\n * },\n * { \"role\": \"assistant\", \"content\": \"The best answer is (\" }\n * ]\n * ```\n *\n * Each input message `content` may be either a single `string` or an array of\n * content blocks, where each block has a specific `type`. Using a `string` for\n * `content` is shorthand for an array of one content block of type `\"text\"`. The\n * following input messages are equivalent:\n *\n * ```json\n * { \"role\": \"user\", \"content\": \"Hello, Claude\" }\n * ```\n *\n * ```json\n * { \"role\": \"user\", \"content\": [{ \"type\": \"text\", \"text\": \"Hello, Claude\" }] }\n * ```\n *\n * Starting with Claude 3 models, you can also send image content blocks:\n *\n * ```json\n * {\n * \"role\": \"user\",\n * \"content\": [\n * {\n * \"type\": \"image\",\n * \"source\": {\n * \"type\": \"base64\",\n * \"media_type\": \"image/jpeg\",\n * \"data\": \"/9j/4AAQSkZJRg...\"\n * }\n * },\n * { \"type\": \"text\", \"text\": \"What is in this image?\" }\n * ]\n * }\n * ```\n *\n * We currently support the `base64` source type for images, and the `image/jpeg`,\n * `image/png`, `image/gif`, and `image/webp` media types.\n *\n * See [examples](https://docs.anthropic.com/en/api/messages-examples#vision) for\n * more input examples.\n *\n * Note that if you want to include a\n * [system prompt](https://docs.anthropic.com/en/docs/system-prompts), you can use\n * the top-level `system` parameter \u2014 there is no `\"system\"` role for input\n * messages in the Messages API.\n */\n messages: Array<MessageParam>;\n\n /**\n * The model that will complete your prompt.\\n\\nSee\n * [models](https://docs.anthropic.com/en/docs/models-overview) for additional\n * details and options.\n */\n model: Model;\n\n /**\n * An object describing metadata about the request.\n */\n metadata?: Metadata;\n\n /**\n * Custom text sequences that will cause the model to stop generating.\n *\n * Our models will normally stop when they have naturally completed their turn,\n * which will result in a response `stop_reason` of `\"end_turn\"`.\n *\n * If you want the model to stop generating when it encounters custom strings of\n * text, you can use the `stop_sequences` parameter. If the model encounters one of\n * the custom sequences, the response `stop_reason` value will be `\"stop_sequence\"`\n * and the response `stop_sequence` value will contain the matched stop sequence.\n */\n stop_sequences?: Array<string>;\n\n /**\n * Whether to incrementally stream the response using server-sent events.\n *\n * See [streaming](https://docs.anthropic.com/en/api/messages-streaming) for\n * details.\n */\n stream?: boolean;\n\n /**\n * System prompt.\n *\n * A system prompt is a way of providing context and instructions to Claude, such\n * as specifying a particular goal or role. See our\n * [guide to system prompts](https://docs.anthropic.com/en/docs/system-prompts).\n */\n system?: string | Array<TextBlockParam>;\n\n /**\n * Amount of randomness injected into the response.\n *\n * Defaults to `1.0`. Ranges from `0.0` to `1.0`. Use `temperature` closer to `0.0`\n * for analytical / multiple choice, and closer to `1.0` for creative and\n * generative tasks.\n *\n * Note that even with `temperature` of `0.0`, the results will not be fully\n * deterministic.\n */\n temperature?: number;\n\n /**\n * Configuration for enabling Claude's extended thinking.\n *\n * When enabled, responses include `thinking` content blocks showing Claude's\n * thinking process before the final answer. Requires a minimum budget of 1,024\n * tokens and counts towards your `max_tokens` limit.\n *\n * See\n * [extended thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking)\n * for details.\n */\n thinking?: ThinkingConfigParam;\n\n /**\n * How the model should use the provided tools. The model can use a specific tool,\n * any available tool, decide by itself, or not use tools at all.\n */\n tool_choice?: ToolChoice;\n\n /**\n * Definitions of tools that the model may use.\n *\n * If you include `tools` in your API request, the model may return `tool_use`\n * content blocks that represent the model's use of those tools. You can then run\n * those tools using the tool input generated by the model and then optionally\n * return results back to the model using `tool_result` content blocks.\n *\n * Each tool definition includes:\n *\n * - `name`: Name of the tool.\n * - `description`: Optional, but strongly-recommended description of the tool.\n * - `input_schema`: [JSON schema](https://json-schema.org/draft/2020-12) for the\n * tool `input` shape that the model will produce in `tool_use` output content\n * blocks.\n *\n * For example, if you defined `tools` as:\n *\n * ```json\n * [\n * {\n * \"name\": \"get_stock_price\",\n * \"description\": \"Get the current stock price for a given ticker symbol.\",\n * \"input_schema\": {\n * \"type\": \"object\",\n * \"properties\": {\n * \"ticker\": {\n * \"type\": \"string\",\n * \"description\": \"The stock ticker symbol, e.g. AAPL for Apple Inc.\"\n * }\n * },\n * \"required\": [\"ticker\"]\n * }\n * }\n * ]\n * ```\n *\n * And then asked the model \"What's the S&P 500 at today?\", the model might produce\n * `tool_use` content blocks in the response like this:\n *\n * ```json\n * [\n * {\n * \"type\": \"tool_use\",\n * \"id\": \"toolu_01D7FLrfh4GYq7yT1ULFeyMV\",\n * \"name\": \"get_stock_price\",\n * \"input\": { \"ticker\": \"^GSPC\" }\n * }\n * ]\n * ```\n *\n * You might then run your `get_stock_price` tool with `{\"ticker\": \"^GSPC\"}` as an\n * input, and return the following back to the model in a subsequent `user`\n * message:\n *\n * ```json\n * [\n * {\n * \"type\": \"tool_result\",\n * \"tool_use_id\": \"toolu_01D7FLrfh4GYq7yT1ULFeyMV\",\n * \"content\": \"259.75 USD\"\n * }\n * ]\n * ```\n *\n * Tools can be used for workflows that include running client-side tools and\n * functions, or more generally whenever you want the model to produce a particular\n * JSON structure of output.\n *\n * See our [guide](https://docs.anthropic.com/en/docs/tool-use) for more details.\n */\n tools?: Array<ToolUnion>;\n\n /**\n * Only sample from the top K options for each subsequent token.\n *\n * Used to remove \"long tail\" low probability responses.\n * [Learn more technical details here](https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277).\n *\n * Recommended for advanced use cases only. You usually only need to use\n * `temperature`.\n */\n top_k?: number;\n\n /**\n * Use nucleus sampling.\n *\n * In nucleus sampling, we compute the cumulative distribution over all the options\n * for each subsequent token in decreasing probability order and cut it off once it\n * reaches a particular probability specified by `top_p`. You should either alter\n * `temperature` or `top_p`, but not both.\n *\n * Recommended for advanced use cases only. You usually only need to use\n * `temperature`.\n */\n top_p?: number;\n}\n\nexport namespace MessageCreateParams {\n /**\n * @deprecated use `Anthropic.Messages.ToolChoiceAuto` instead\n */\n export type Metadata = MessagesAPI.Metadata;\n\n /**\n * @deprecated use `Anthropic.Messages.ToolChoiceAuto` instead\n */\n export type ToolChoiceAuto = MessagesAPI.ToolChoiceAuto;\n\n /**\n * @deprecated use `Anthropic.Messages.ToolChoiceAny` instead\n */\n export type ToolChoiceAny = MessagesAPI.ToolChoiceAny;\n\n /**\n * @deprecated use `Anthropic.Messages.ToolChoiceTool` instead\n */\n export type ToolChoiceTool = MessagesAPI.ToolChoiceTool;\n\n export type MessageCreateParamsNonStreaming = MessagesAPI.MessageCreateParamsNonStreaming;\n export type MessageCreateParamsStreaming = MessagesAPI.MessageCreateParamsStreaming;\n}\n\nexport interface MessageCreateParamsNonStreaming extends MessageCreateParamsBase {\n /**\n * Whether to incrementally stream the response using server-sent events.\n *\n * See [streaming](https://docs.anthropic.com/en/api/messages-streaming) for\n * details.\n */\n stream?: false;\n}\n\nexport interface MessageCreateParamsStreaming extends MessageCreateParamsBase {\n /**\n * Whether to incrementally stream the response using server-sent events.\n *\n * See [streaming](https://docs.anthropic.com/en/api/messages-streaming) for\n * details.\n */\n stream: true;\n}\n\nexport type MessageStreamParams = MessageCreateParamsBase;\n\nexport interface MessageCountTokensParams {\n /**\n * Input messages.\n *\n * Our models are trained to operate on alternating `user` and `assistant`\n * conversational turns. When creating a new `Message`, you specify the prior\n * conversational turns with the `messages` parameter, and the model then generates\n * the next `Message` in the conversation. Consecutive `user` or `assistant` turns\n * in your request will be combined into a single turn.\n *\n * Each input message must be an object with a `role` and `content`. You can\n * specify a single `user`-role message, or you can include multiple `user` and\n * `assistant` messages.\n *\n * If the final message uses the `assistant` role, the response content will\n * continue immediately from the content in that message. This can be used to\n * constrain part of the model's response.\n *\n * Example with a single `user` message:\n *\n * ```json\n * [{ \"role\": \"user\", \"content\": \"Hello, Claude\" }]\n * ```\n *\n * Example with multiple conversational turns:\n *\n * ```json\n * [\n * { \"role\": \"user\", \"content\": \"Hello there.\" },\n * { \"role\": \"assistant\", \"content\": \"Hi, I'm Claude. How can I help you?\" },\n * { \"role\": \"user\", \"content\": \"Can you explain LLMs in plain English?\" }\n * ]\n * ```\n *\n * Example with a partially-filled response from Claude:\n *\n * ```json\n * [\n * {\n * \"role\": \"user\",\n * \"content\": \"What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun\"\n * },\n * { \"role\": \"assistant\", \"content\": \"The best answer is (\" }\n * ]\n * ```\n *\n * Each input message `content` may be either a single `string` or an array of\n * content blocks, where each block has a specific `type`. Using a `string` for\n * `content` is shorthand for an array of one content block of type `\"text\"`. The\n * following input messages are equivalent:\n *\n * ```json\n * { \"role\": \"user\", \"content\": \"Hello, Claude\" }\n * ```\n *\n * ```json\n * { \"role\": \"user\", \"content\": [{ \"type\": \"text\", \"text\": \"Hello, Claude\" }] }\n * ```\n *\n * Starting with Claude 3 models, you can also send image content blocks:\n *\n * ```json\n * {\n * \"role\": \"user\",\n * \"content\": [\n * {\n * \"type\": \"image\",\n * \"source\": {\n * \"type\": \"base64\",\n * \"media_type\": \"image/jpeg\",\n * \"data\": \"/9j/4AAQSkZJRg...\"\n * }\n * },\n * { \"type\": \"text\", \"text\": \"What is in this image?\" }\n * ]\n * }\n * ```\n *\n * We currently support the `base64` source type for images, and the `image/jpeg`,\n * `image/png`, `image/gif`, and `image/webp` media types.\n *\n * See [examples](https://docs.anthropic.com/en/api/messages-examples#vision) for\n * more input examples.\n *\n * Note that if you want to include a\n * [system prompt](https://docs.anthropic.com/en/docs/system-prompts), you can use\n * the top-level `system` parameter \u2014 there is no `\"system\"` role for input\n * messages in the Messages API.\n */\n messages: Array<MessageParam>;\n\n /**\n * The model that will complete your prompt.\\n\\nSee\n * [models](https://docs.anthropic.com/en/docs/models-overview) for additional\n * details and options.\n */\n model: Model;\n\n /**\n * System prompt.\n *\n * A system prompt is a way of providing context and instructions to Claude, such\n * as specifying a particular goal or role. See our\n * [guide to system prompts](https://docs.anthropic.com/en/docs/system-prompts).\n */\n system?: string | Array<TextBlockParam>;\n\n /**\n * Configuration for enabling Claude's extended thinking.\n *\n * When enabled, responses include `thinking` content blocks showing Claude's\n * thinking process before the final answer. Requires a minimum budget of 1,024\n * tokens and counts towards your `max_tokens` limit.\n *\n * See\n * [extended thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking)\n * for details.\n */\n thinking?: ThinkingConfigParam;\n\n /**\n * How the model should use the provided tools. The model can use a specific tool,\n * any available tool, decide by itself, or not use tools at all.\n */\n tool_choice?: ToolChoice;\n\n /**\n * Definitions of tools that the model may use.\n *\n * If you include `tools` in your API request, the model may return `tool_use`\n * content blocks that represent the model's use of those tools. You can then run\n * those tools using the tool input generated by the model and then optionally\n * return results back to the model using `tool_result` content blocks.\n *\n * Each tool definition includes:\n *\n * - `name`: Name of the tool.\n * - `description`: Optional, but strongly-recommended description of the tool.\n * - `input_schema`: [JSON schema](https://json-schema.org/draft/2020-12) for the\n * tool `input` shape that the model will produce in `tool_use` output content\n * blocks.\n *\n * For example, if you defined `tools` as:\n *\n * ```json\n * [\n * {\n * \"name\": \"get_stock_price\",\n * \"description\": \"Get the current stock price for a given ticker symbol.\",\n * \"input_schema\": {\n * \"type\": \"object\",\n * \"properties\": {\n * \"ticker\": {\n * \"type\": \"string\",\n * \"description\": \"The stock ticker symbol, e.g. AAPL for Apple Inc.\"\n * }\n * },\n * \"required\": [\"ticker\"]\n * }\n * }\n * ]\n * ```\n *\n * And then asked the model \"What's the S&P 500 at today?\", the model might produce\n * `tool_use` content blocks in the response like this:\n *\n * ```json\n * [\n * {\n * \"type\": \"tool_use\",\n * \"id\": \"toolu_01D7FLrfh4GYq7yT1ULFeyMV\",\n * \"name\": \"get_stock_price\",\n * \"input\": { \"ticker\": \"^GSPC\" }\n * }\n * ]\n * ```\n *\n * You might then run your `get_stock_price` tool with `{\"ticker\": \"^GSPC\"}` as an\n * input, and return the following back to the model in a subsequent `user`\n * message:\n *\n * ```json\n * [\n * {\n * \"type\": \"tool_result\",\n * \"tool_use_id\": \"toolu_01D7FLrfh4GYq7yT1ULFeyMV\",\n * \"content\": \"259.75 USD\"\n * }\n * ]\n * ```\n *\n * Tools can be used for workflows that include running client-side tools and\n * functions, or more generally whenever you want the model to produce a particular\n * JSON structure of output.\n *\n * See our [guide](https://docs.anthropic.com/en/docs/tool-use) for more details.\n */\n tools?: Array<MessageCountTokensTool>;\n}\n\nMessages.Batches = Batches;\nMessages.MessageBatchesPage = MessageBatchesPage;\n\nexport declare namespace Messages {\n export {\n type Base64ImageSource as Base64ImageSource,\n type Base64PDFSource as Base64PDFSource,\n type CacheControlEphemeral as CacheControlEphemeral,\n type CitationCharLocation as CitationCharLocation,\n type CitationCharLocationParam as CitationCharLocationParam,\n type CitationContentBlockLocation as CitationContentBlockLocation,\n type CitationContentBlockLocationParam as CitationContentBlockLocationParam,\n type CitationPageLocation as CitationPageLocation,\n type CitationPageLocationParam as CitationPageLocationParam,\n type CitationsConfigParam as CitationsConfigParam,\n type CitationsDelta as CitationsDelta,\n type ContentBlock as ContentBlock,\n type ContentBlockDeltaEvent as ContentBlockDeltaEvent,\n type ContentBlockParam as ContentBlockParam,\n type ContentBlockSource as ContentBlockSource,\n type ContentBlockSourceContent as ContentBlockSourceContent,\n type ContentBlockStartEvent as ContentBlockStartEvent,\n type ContentBlockStopEvent as ContentBlockStopEvent,\n type DocumentBlockParam as DocumentBlockParam,\n type ImageBlockParam as ImageBlockParam,\n type InputJsonDelta as InputJsonDelta,\n type InputJSONDelta as InputJSONDelta,\n type Message as Message,\n type MessageCountTokensTool as MessageCountTokensTool,\n type MessageDeltaEvent as MessageDeltaEvent,\n type MessageDeltaUsage as MessageDeltaUsage,\n type MessageParam as MessageParam,\n type MessageStartEvent as MessageStartEvent,\n type MessageStopEvent as MessageStopEvent,\n type MessageStreamEvent as MessageStreamEvent,\n type MessageTokensCount as MessageTokensCount,\n type Metadata as Metadata,\n type Model as Model,\n type PlainTextSource as PlainTextSource,\n type RawContentBlockDeltaEvent as RawContentBlockDeltaEvent,\n type RawContentBlockStartEvent as RawContentBlockStartEvent,\n type RawContentBlockStopEvent as RawContentBlockStopEvent,\n type RawMessageDeltaEvent as RawMessageDeltaEvent,\n type RawMessageStartEvent as RawMessageStartEvent,\n type RawMessageStopEvent as RawMessageStopEvent,\n type RawMessageStreamEvent as RawMessageStreamEvent,\n type RedactedThinkingBlock as RedactedThinkingBlock,\n type RedactedThinkingBlockParam as RedactedThinkingBlockParam,\n type SignatureDelta as SignatureDelta,\n type TextBlock as TextBlock,\n type TextBlockParam as TextBlockParam,\n type TextCitation as TextCitation,\n type TextCitationParam as TextCitationParam,\n type TextDelta as TextDelta,\n type ThinkingBlock as ThinkingBlock,\n type ThinkingBlockParam as ThinkingBlockParam,\n type ThinkingConfigDisabled as ThinkingConfigDisabled,\n type ThinkingConfigEnabled as ThinkingConfigEnabled,\n type ThinkingConfigParam as ThinkingConfigParam,\n type ThinkingDelta as ThinkingDelta,\n type Tool as Tool,\n type ToolBash20250124 as ToolBash20250124,\n type ToolChoice as ToolChoice,\n type ToolChoiceAny as ToolChoiceAny,\n type ToolChoiceAuto as ToolChoiceAuto,\n type ToolChoiceNone as ToolChoiceNone,\n type ToolChoiceTool as ToolChoiceTool,\n type ToolResultBlockParam as ToolResultBlockParam,\n type ToolTextEditor20250124 as ToolTextEditor20250124,\n type ToolUnion as ToolUnion,\n type ToolUseBlock as ToolUseBlock,\n type ToolUseBlockParam as ToolUseBlockParam,\n type URLImageSource as URLImageSource,\n type URLPDFSource as URLPDFSource,\n type Usage as Usage,\n type MessageCreateParams as MessageCreateParams,\n type MessageCreateParamsNonStreaming as MessageCreateParamsNonStreaming,\n type MessageCreateParamsStreaming as MessageCreateParamsStreaming,\n type MessageStreamParams as MessageStreamParams,\n type MessageCountTokensParams as MessageCountTokensParams,\n };\n\n export {\n Batches as Batches,\n type DeletedMessageBatch as DeletedMessageBatch,\n type MessageBatch as MessageBatch,\n type MessageBatchCanceledResult as MessageBatchCanceledResult,\n type MessageBatchErroredResult as MessageBatchErroredResult,\n type MessageBatchExpiredResult as MessageBatchExpiredResult,\n type MessageBatchIndividualResponse as MessageBatchIndividualResponse,\n type MessageBatchRequestCounts as MessageBatchRequestCounts,\n type MessageBatchResult as MessageBatchResult,\n type MessageBatchSucceededResult as MessageBatchSucceededResult,\n MessageBatchesPage as MessageBatchesPage,\n type BatchCreateParams as BatchCreateParams,\n type BatchListParams as BatchListParams,\n };\n}\n", "// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\n\nimport { APIResource } from \"../resource.js\";\nimport { isRequestOptions } from \"../core.js\";\nimport * as Core from \"../core.js\";\nimport { Page, type PageParams } from \"../pagination.js\";\n\nexport class Models extends APIResource {\n /**\n * Get a specific model.\n *\n * The Models API response can be used to determine information about a specific\n * model or resolve a model alias to a model ID.\n */\n retrieve(modelId: string, options?: Core.RequestOptions): Core.APIPromise<ModelInfo> {\n return this._client.get(`/v1/models/${modelId}`, options);\n }\n\n /**\n * List available models.\n *\n * The Models API response can be used to determine which models are available for\n * use in the API. More recently released models are listed first.\n */\n list(query?: ModelListParams, options?: Core.RequestOptions): Core.PagePromise<ModelInfosPage, ModelInfo>;\n list(options?: Core.RequestOptions): Core.PagePromise<ModelInfosPage, ModelInfo>;\n list(\n query: ModelListParams | Core.RequestOptions = {},\n options?: Core.RequestOptions,\n ): Core.PagePromise<ModelInfosPage, ModelInfo> {\n if (isRequestOptions(query)) {\n return this.list({}, query);\n }\n return this._client.getAPIList('/v1/models', ModelInfosPage, { query, ...options });\n }\n}\n\nexport class ModelInfosPage extends Page<ModelInfo> {}\n\nexport interface ModelInfo {\n /**\n * Unique model identifier.\n */\n id: string;\n\n /**\n * RFC 3339 datetime string representing the time at which the model was released.\n * May be set to an epoch value if the release date is unknown.\n */\n created_at: string;\n\n /**\n * A human-readable name for the model.\n */\n display_name: string;\n\n /**\n * Object type.\n *\n * For Models, this is always `\"model\"`.\n */\n type: 'model';\n}\n\nexport interface ModelListParams extends PageParams {}\n\nModels.ModelInfosPage = ModelInfosPage;\n\nexport declare namespace Models {\n export {\n type ModelInfo as ModelInfo,\n ModelInfosPage as ModelInfosPage,\n type ModelListParams as ModelListParams,\n };\n}\n", "// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\n\nimport { type Agent } from \"./_shims/index.js\";\nimport * as Core from \"./core.js\";\nimport * as Errors from \"./error.js\";\nimport * as Pagination from \"./pagination.js\";\nimport { type PageParams, PageResponse } from \"./pagination.js\";\nimport * as Uploads from \"./uploads.js\";\nimport * as API from \"./resources/index.js\";\nimport {\n Completion,\n CompletionCreateParams,\n CompletionCreateParamsNonStreaming,\n CompletionCreateParamsStreaming,\n Completions,\n} from \"./resources/completions.js\";\nimport { ModelInfo, ModelInfosPage, ModelListParams, Models } from \"./resources/models.js\";\nimport {\n AnthropicBeta,\n Beta,\n BetaAPIError,\n BetaAuthenticationError,\n BetaBillingError,\n BetaError,\n BetaErrorResponse,\n BetaGatewayTimeoutError,\n BetaInvalidRequestError,\n BetaNotFoundError,\n BetaOverloadedError,\n BetaPermissionError,\n BetaRateLimitError,\n} from \"./resources/beta/beta.js\";\nimport {\n Base64ImageSource,\n Base64PDFSource,\n CacheControlEphemeral,\n CitationCharLocation,\n CitationCharLocationParam,\n CitationContentBlockLocation,\n CitationContentBlockLocationParam,\n CitationPageLocation,\n CitationPageLocationParam,\n CitationsConfigParam,\n CitationsDelta,\n ContentBlock,\n ContentBlockDeltaEvent,\n ContentBlockParam,\n ContentBlockSource,\n ContentBlockSourceContent,\n ContentBlockStartEvent,\n ContentBlockStopEvent,\n DocumentBlockParam,\n ImageBlockParam,\n InputJSONDelta,\n Message,\n MessageCountTokensParams,\n MessageCountTokensTool,\n MessageCreateParams,\n MessageCreateParamsNonStreaming,\n MessageCreateParamsStreaming,\n MessageDeltaEvent,\n MessageDeltaUsage,\n MessageParam,\n MessageStartEvent,\n MessageStopEvent,\n MessageStreamEvent,\n MessageStreamParams,\n MessageTokensCount,\n Messages,\n Metadata,\n Model,\n PlainTextSource,\n RawContentBlockDeltaEvent,\n RawContentBlockStartEvent,\n RawContentBlockStopEvent,\n RawMessageDeltaEvent,\n RawMessageStartEvent,\n RawMessageStopEvent,\n RawMessageStreamEvent,\n RedactedThinkingBlock,\n RedactedThinkingBlockParam,\n SignatureDelta,\n TextBlock,\n TextBlockParam,\n TextCitation,\n TextCitationParam,\n TextDelta,\n ThinkingBlock,\n ThinkingBlockParam,\n ThinkingConfigDisabled,\n ThinkingConfigEnabled,\n ThinkingConfigParam,\n ThinkingDelta,\n Tool,\n ToolBash20250124,\n ToolChoice,\n ToolChoiceAny,\n ToolChoiceAuto,\n ToolChoiceNone,\n ToolChoiceTool,\n ToolResultBlockParam,\n ToolTextEditor20250124,\n ToolUnion,\n ToolUseBlock,\n ToolUseBlockParam,\n URLImageSource,\n URLPDFSource,\n Usage,\n} from \"./resources/messages/messages.js\";\n\nexport interface ClientOptions {\n /**\n * Defaults to process.env['ANTHROPIC_API_KEY'].\n */\n apiKey?: string | null | undefined;\n\n /**\n * Defaults to process.env['ANTHROPIC_AUTH_TOKEN'].\n */\n authToken?: string | null | undefined;\n\n /**\n * Override the default base URL for the API, e.g., \"https://api.example.com/v2/\"\n *\n * Defaults to process.env['ANTHROPIC_BASE_URL'].\n */\n baseURL?: string | null | undefined;\n\n /**\n * The maximum amount of time (in milliseconds) that the client should wait for a response\n * from the server before timing out a single request.\n *\n * Note that request timeouts are retried by default, so in a worst-case scenario you may wait\n * much longer than this timeout before the promise succeeds or fails.\n */\n timeout?: number | undefined;\n\n /**\n * An HTTP agent used to manage HTTP(S) connections.\n *\n * If not provided, an agent will be constructed by default in the Node.js environment,\n * otherwise no agent is used.\n */\n httpAgent?: Agent | undefined;\n\n /**\n * Specify a custom `fetch` function implementation.\n *\n * If not provided, we use `node-fetch` on Node.js and otherwise expect that `fetch` is\n * defined globally.\n */\n fetch?: Core.Fetch | undefined;\n\n /**\n * The maximum number of times that the client will retry a request in case of a\n * temporary failure, like a network error or a 5XX error from the server.\n *\n * @default 2\n */\n maxRetries?: number | undefined;\n\n /**\n * Default headers to include with every request to the API.\n *\n * These can be removed in individual requests by explicitly setting the\n * header to `undefined` or `null` in request options.\n */\n defaultHeaders?: Core.Headers | undefined;\n\n /**\n * Default query parameters to include with every request to the API.\n *\n * These can be removed in individual requests by explicitly setting the\n * param to `undefined` in request options.\n */\n defaultQuery?: Core.DefaultQuery | undefined;\n\n /**\n * By default, client-side use of this library is not allowed, as it risks exposing your secret API credentials to attackers.\n * Only set this option to `true` if you understand the risks and have appropriate mitigations in place.\n */\n dangerouslyAllowBrowser?: boolean | undefined;\n}\n\n/**\n * API Client for interfacing with the Anthropic API.\n */\nexport class Anthropic extends Core.APIClient {\n apiKey: string | null;\n authToken: string | null;\n\n private _options: ClientOptions;\n\n /**\n * API Client for interfacing with the Anthropic API.\n *\n * @param {string | null | undefined} [opts.apiKey=process.env['ANTHROPIC_API_KEY'] ?? null]\n * @param {string | null | undefined} [opts.authToken=process.env['ANTHROPIC_AUTH_TOKEN'] ?? null]\n * @param {string} [opts.baseURL=process.env['ANTHROPIC_BASE_URL'] ?? https://api.anthropic.com] - Override the default base URL for the API.\n * @param {number} [opts.timeout=10 minutes] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out.\n * @param {number} [opts.httpAgent] - An HTTP agent used to manage HTTP(s) connections.\n * @param {Core.Fetch} [opts.fetch] - Specify a custom `fetch` function implementation.\n * @param {number} [opts.maxRetries=2] - The maximum number of times the client will retry a request.\n * @param {Core.Headers} opts.defaultHeaders - Default headers to include with every request to the API.\n * @param {Core.DefaultQuery} opts.defaultQuery - Default query parameters to include with every request to the API.\n * @param {boolean} [opts.dangerouslyAllowBrowser=false] - By default, client-side use of this library is not allowed, as it risks exposing your secret API credentials to attackers.\n */\n constructor({\n baseURL = Core.readEnv('ANTHROPIC_BASE_URL'),\n apiKey = Core.readEnv('ANTHROPIC_API_KEY') ?? null,\n authToken = Core.readEnv('ANTHROPIC_AUTH_TOKEN') ?? null,\n ...opts\n }: ClientOptions = {}) {\n const options: ClientOptions = {\n apiKey,\n authToken,\n ...opts,\n baseURL: baseURL || `https://api.anthropic.com`,\n };\n\n if (!options.dangerouslyAllowBrowser && Core.isRunningInBrowser()) {\n throw new Errors.AnthropicError(\n \"It looks like you're running in a browser-like environment.\\n\\nThis is disabled by default, as it risks exposing your secret API credentials to attackers.\\nIf you understand the risks and have appropriate mitigations in place,\\nyou can set the `dangerouslyAllowBrowser` option to `true`, e.g.,\\n\\nnew Anthropic({ apiKey, dangerouslyAllowBrowser: true });\\n\",\n );\n }\n\n super({\n baseURL: options.baseURL!,\n timeout: options.timeout ?? 600000 /* 10 minutes */,\n httpAgent: options.httpAgent,\n maxRetries: options.maxRetries,\n fetch: options.fetch,\n });\n\n this._options = options;\n\n this.apiKey = apiKey;\n this.authToken = authToken;\n }\n\n completions: API.Completions = new API.Completions(this);\n messages: API.Messages = new API.Messages(this);\n models: API.Models = new API.Models(this);\n beta: API.Beta = new API.Beta(this);\n\n protected override defaultQuery(): Core.DefaultQuery | undefined {\n return this._options.defaultQuery;\n }\n\n protected override defaultHeaders(opts: Core.FinalRequestOptions): Core.Headers {\n return {\n ...super.defaultHeaders(opts),\n ...(this._options.dangerouslyAllowBrowser ?\n { 'anthropic-dangerous-direct-browser-access': 'true' }\n : undefined),\n 'anthropic-version': '2023-06-01',\n ...this._options.defaultHeaders,\n };\n }\n\n protected override validateHeaders(headers: Core.Headers, customHeaders: Core.Headers) {\n if (this.apiKey && headers['x-api-key']) {\n return;\n }\n if (customHeaders['x-api-key'] === null) {\n return;\n }\n\n if (this.authToken && headers['authorization']) {\n return;\n }\n if (customHeaders['authorization'] === null) {\n return;\n }\n\n throw new Error(\n 'Could not resolve authentication method. Expected either apiKey or authToken to be set. Or for one of the \"X-Api-Key\" or \"Authorization\" headers to be explicitly omitted',\n );\n }\n\n protected override authHeaders(opts: Core.FinalRequestOptions): Core.Headers {\n const apiKeyAuth = this.apiKeyAuth(opts);\n const bearerAuth = this.bearerAuth(opts);\n\n if (apiKeyAuth != null && !Core.isEmptyObj(apiKeyAuth)) {\n return apiKeyAuth;\n }\n\n if (bearerAuth != null && !Core.isEmptyObj(bearerAuth)) {\n return bearerAuth;\n }\n return {};\n }\n\n protected apiKeyAuth(opts: Core.FinalRequestOptions): Core.Headers {\n if (this.apiKey == null) {\n return {};\n }\n return { 'X-Api-Key': this.apiKey };\n }\n\n protected bearerAuth(opts: Core.FinalRequestOptions): Core.Headers {\n if (this.authToken == null) {\n return {};\n }\n return { Authorization: `Bearer ${this.authToken}` };\n }\n\n static Anthropic = this;\n static HUMAN_PROMPT = '\\n\\nHuman:';\n static AI_PROMPT = '\\n\\nAssistant:';\n static DEFAULT_TIMEOUT = 600000; // 10 minutes\n\n static AnthropicError = Errors.AnthropicError;\n static APIError = Errors.APIError;\n static APIConnectionError = Errors.APIConnectionError;\n static APIConnectionTimeoutError = Errors.APIConnectionTimeoutError;\n static APIUserAbortError = Errors.APIUserAbortError;\n static NotFoundError = Errors.NotFoundError;\n static ConflictError = Errors.ConflictError;\n static RateLimitError = Errors.RateLimitError;\n static BadRequestError = Errors.BadRequestError;\n static AuthenticationError = Errors.AuthenticationError;\n static InternalServerError = Errors.InternalServerError;\n static PermissionDeniedError = Errors.PermissionDeniedError;\n static UnprocessableEntityError = Errors.UnprocessableEntityError;\n\n static toFile = Uploads.toFile;\n static fileFromPath = Uploads.fileFromPath;\n}\n\nAnthropic.Completions = Completions;\nAnthropic.Messages = Messages;\nAnthropic.Models = Models;\nAnthropic.ModelInfosPage = ModelInfosPage;\nAnthropic.Beta = Beta;\nexport declare namespace Anthropic {\n export type RequestOptions = Core.RequestOptions;\n\n export import Page = Pagination.Page;\n export { type PageParams as PageParams, type PageResponse as PageResponse };\n\n export {\n Completions as Completions,\n type Completion as Completion,\n type CompletionCreateParams as CompletionCreateParams,\n type CompletionCreateParamsNonStreaming as CompletionCreateParamsNonStreaming,\n type CompletionCreateParamsStreaming as CompletionCreateParamsStreaming,\n };\n\n export {\n Messages as Messages,\n type Base64ImageSource as Base64ImageSource,\n type Base64PDFSource as Base64PDFSource,\n type CacheControlEphemeral as CacheControlEphemeral,\n type CitationCharLocation as CitationCharLocation,\n type CitationCharLocationParam as CitationCharLocationParam,\n type CitationContentBlockLocation as CitationContentBlockLocation,\n type CitationContentBlockLocationParam as CitationContentBlockLocationParam,\n type CitationPageLocation as CitationPageLocation,\n type CitationPageLocationParam as CitationPageLocationParam,\n type CitationsConfigParam as CitationsConfigParam,\n type CitationsDelta as CitationsDelta,\n type ContentBlock as ContentBlock,\n type ContentBlockDeltaEvent as ContentBlockDeltaEvent,\n type ContentBlockParam as ContentBlockParam,\n type ContentBlockSource as ContentBlockSource,\n type ContentBlockSourceContent as ContentBlockSourceContent,\n type ContentBlockStartEvent as ContentBlockStartEvent,\n type ContentBlockStopEvent as ContentBlockStopEvent,\n type DocumentBlockParam as DocumentBlockParam,\n type ImageBlockParam as ImageBlockParam,\n type InputJSONDelta as InputJSONDelta,\n type Message as Message,\n type MessageCountTokensTool as MessageCountTokensTool,\n type MessageDeltaEvent as MessageDeltaEvent,\n type MessageDeltaUsage as MessageDeltaUsage,\n type MessageParam as MessageParam,\n type MessageStartEvent as MessageStartEvent,\n type MessageStopEvent as MessageStopEvent,\n type MessageStreamEvent as MessageStreamEvent,\n type MessageTokensCount as MessageTokensCount,\n type Metadata as Metadata,\n type Model as Model,\n type PlainTextSource as PlainTextSource,\n type RawContentBlockDeltaEvent as RawContentBlockDeltaEvent,\n type RawContentBlockStartEvent as RawContentBlockStartEvent,\n type RawContentBlockStopEvent as RawContentBlockStopEvent,\n type RawMessageDeltaEvent as RawMessageDeltaEvent,\n type RawMessageStartEvent as RawMessageStartEvent,\n type RawMessageStopEvent as RawMessageStopEvent,\n type RawMessageStreamEvent as RawMessageStreamEvent,\n type RedactedThinkingBlock as RedactedThinkingBlock,\n type RedactedThinkingBlockParam as RedactedThinkingBlockParam,\n type SignatureDelta as SignatureDelta,\n type TextBlock as TextBlock,\n type TextBlockParam as TextBlockParam,\n type TextCitation as TextCitation,\n type TextCitationParam as TextCitationParam,\n type TextDelta as TextDelta,\n type ThinkingBlock as ThinkingBlock,\n type ThinkingBlockParam as ThinkingBlockParam,\n type ThinkingConfigDisabled as ThinkingConfigDisabled,\n type ThinkingConfigEnabled as ThinkingConfigEnabled,\n type ThinkingConfigParam as ThinkingConfigParam,\n type ThinkingDelta as ThinkingDelta,\n type Tool as Tool,\n type ToolBash20250124 as ToolBash20250124,\n type ToolChoice as ToolChoice,\n type ToolChoiceAny as ToolChoiceAny,\n type ToolChoiceAuto as ToolChoiceAuto,\n type ToolChoiceNone as ToolChoiceNone,\n type ToolChoiceTool as ToolChoiceTool,\n type ToolResultBlockParam as ToolResultBlockParam,\n type ToolTextEditor20250124 as ToolTextEditor20250124,\n type ToolUnion as ToolUnion,\n type ToolUseBlock as ToolUseBlock,\n type ToolUseBlockParam as ToolUseBlockParam,\n type URLImageSource as URLImageSource,\n type URLPDFSource as URLPDFSource,\n type Usage as Usage,\n type MessageCreateParams as MessageCreateParams,\n type MessageCreateParamsNonStreaming as MessageCreateParamsNonStreaming,\n type MessageCreateParamsStreaming as MessageCreateParamsStreaming,\n type MessageStreamParams as MessageStreamParams,\n type MessageCountTokensParams as MessageCountTokensParams,\n };\n\n export {\n Models as Models,\n type ModelInfo as ModelInfo,\n ModelInfosPage as ModelInfosPage,\n type ModelListParams as ModelListParams,\n };\n\n export {\n Beta as Beta,\n type AnthropicBeta as AnthropicBeta,\n type BetaAPIError as BetaAPIError,\n type BetaAuthenticationError as BetaAuthenticationError,\n type BetaBillingError as BetaBillingError,\n type BetaError as BetaError,\n type BetaErrorResponse as BetaErrorResponse,\n type BetaGatewayTimeoutError as BetaGatewayTimeoutError,\n type BetaInvalidRequestError as BetaInvalidRequestError,\n type BetaNotFoundError as BetaNotFoundError,\n type BetaOverloadedError as BetaOverloadedError,\n type BetaPermissionError as BetaPermissionError,\n type BetaRateLimitError as BetaRateLimitError,\n };\n\n export type APIErrorObject = API.APIErrorObject;\n export type AuthenticationError = API.AuthenticationError;\n export type BillingError = API.BillingError;\n export type ErrorObject = API.ErrorObject;\n export type ErrorResponse = API.ErrorResponse;\n export type GatewayTimeoutError = API.GatewayTimeoutError;\n export type InvalidRequestError = API.InvalidRequestError;\n export type NotFoundError = API.NotFoundError;\n export type OverloadedError = API.OverloadedError;\n export type PermissionError = API.PermissionError;\n export type RateLimitError = API.RateLimitError;\n}\nexport const { HUMAN_PROMPT, AI_PROMPT } = Anthropic;\n\nexport { toFile, fileFromPath } from \"./uploads.js\";\nexport {\n AnthropicError,\n APIError,\n APIConnectionError,\n APIConnectionTimeoutError,\n APIUserAbortError,\n NotFoundError,\n ConflictError,\n RateLimitError,\n BadRequestError,\n AuthenticationError,\n InternalServerError,\n PermissionDeniedError,\n UnprocessableEntityError,\n} from \"./error.js\";\n\nexport default Anthropic;\n", "import type Anthropic from '@anthropic-ai/sdk';\n\nexport const DEFAULT_ANTHROPIC_PARAMS = {\n // model: 'claude-3-5-sonnet-20241022',\n model: 'claude-3-7-sonnet-20250219',\n max_tokens: 8192,\n temperature: 0.1\n};\n\nexport const DEFAULT_ANTHROPIC_OPTIONS = {\n headers: {\n 'x-api-key': null, // Ensuring headers are omitted\n authorization: null\n }\n};\n\nasync function sendPrompt(\n anthropic: Anthropic,\n config: {\n proxyUrl: string;\n headers?: Record<string, string | null | undefined>;\n\n model?: string;\n maxTokens?: number;\n temperature?: number;\n },\n prompt: string,\n signal?: AbortSignal\n): Promise<AsyncGenerator<string, void, unknown>> {\n const customOptions: Partial<typeof DEFAULT_ANTHROPIC_PARAMS> = {};\n if (config.model) customOptions.model = config.model;\n if (config.maxTokens) customOptions.max_tokens = config.maxTokens;\n if (config.temperature) customOptions.temperature = config.temperature;\n\n const msg = await anthropic.messages.create(\n {\n ...DEFAULT_ANTHROPIC_PARAMS,\n ...customOptions,\n stream: true,\n messages: [\n {\n role: 'user',\n content: [\n {\n type: 'text',\n text: prompt\n }\n ]\n }\n ]\n },\n {\n signal,\n ...DEFAULT_ANTHROPIC_OPTIONS,\n headers: {\n ...(DEFAULT_ANTHROPIC_OPTIONS.headers ?? {}),\n ...config.headers\n }\n }\n );\n\n // Return an async generator that yields only the text chunks\n async function* textStreamGenerator() {\n try {\n for await (const chunk of msg) {\n if (\n chunk.type === 'content_block_delta' &&\n chunk.delta.type === 'text_delta'\n ) {\n yield chunk.delta.text;\n }\n }\n } catch (error) {\n // eslint-disable-next-line no-console\n console.error('Stream error:', error);\n throw error; // Re-throw to allow consumer to handle\n }\n }\n\n return textStreamGenerator();\n}\n\nexport default sendPrompt;\n", "import CreativeEditorSDK from '@cesdk/cesdk-js';\nimport {\n type CommonProviderConfiguration,\n mergeQuickActionsConfig\n} from '@imgly/plugin-ai-generation-web';\nimport { TextProvider } from '../types';\nimport Anthropic from '@anthropic-ai/sdk';\nimport sendPrompt from './sendPrompt';\n\ntype AnthropicInput = {\n prompt: string;\n temperature?: number;\n maxTokens?: number;\n\n blockId?: number;\n initialText?: string;\n};\n\ntype AnthropicOutput = {\n kind: 'text';\n text: string;\n};\n\nexport interface AnthropicProviderConfig\n extends CommonProviderConfiguration<AnthropicInput, AnthropicOutput> {\n model?: string;\n}\n\nexport function AnthropicProvider(\n config: AnthropicProviderConfig\n): (context: {\n cesdk: CreativeEditorSDK;\n}) => Promise<TextProvider<AnthropicInput>> {\n return () => {\n let anthropic: Anthropic | null = null;\n\n // Process quick actions configuration\n const defaultQuickActions: any = {\n 'ly.img.improve': true,\n 'ly.img.fix': {}, // Test new empty object syntax\n 'ly.img.shorter': true,\n 'ly.img.longer': {}, // Test new empty object syntax\n 'ly.img.changeTone': true,\n 'ly.img.translate': true,\n 'ly.img.changeTextTo': {} // Test new empty object syntax\n };\n\n const supportedQuickActions = mergeQuickActionsConfig(\n defaultQuickActions,\n config.supportedQuickActions\n );\n\n const provider: TextProvider<AnthropicInput> = {\n kind: 'text',\n id: 'anthropic',\n name: 'Anthropic',\n initialize: async () => {\n anthropic = new Anthropic({\n dangerouslyAllowBrowser: true,\n baseURL: config.proxyUrl,\n // Will be injected by the proxy\n apiKey: null,\n authToken: null\n });\n },\n input: {\n quickActions: {\n supported: supportedQuickActions\n }\n },\n output: {\n middleware: config.middlewares ?? config.middleware ?? [],\n generate: async (\n { prompt, blockId },\n { engine, abortSignal }\n ): Promise<AsyncGenerator<AnthropicOutput, AnthropicOutput>> => {\n if (anthropic == null)\n throw new Error('Anthropic SDK is not initialized');\n\n if (\n blockId != null &&\n engine.block.getType(blockId) !== '//ly.img.ubq/text'\n ) {\n throw new Error(\n 'If a block is provided to this generation, it most be a text block'\n );\n }\n\n if (config.debug)\n // eslint-disable-next-line no-console\n console.log(\n 'Sending prompt to Anthropic:',\n JSON.stringify(prompt, undefined, 2)\n );\n\n const stream = await sendPrompt(\n anthropic,\n {\n proxyUrl: config.proxyUrl,\n headers: config.headers,\n model: config.model ?? 'claude-3-7-sonnet-20250219' // Default\n },\n prompt,\n abortSignal\n );\n\n // Create a new AsyncGenerator that yields AnthropicOutput objects\n async function* outputGenerator(): AsyncGenerator<\n AnthropicOutput,\n AnthropicOutput\n > {\n let inferredText: string = '';\n for await (const chunk of stream) {\n if (abortSignal?.aborted) {\n break;\n }\n inferredText += chunk;\n yield {\n kind: 'text',\n text: inferredText\n };\n }\n // Return the final result\n return {\n kind: 'text',\n text: inferredText\n };\n }\n\n return outputGenerator();\n }\n }\n };\n\n return Promise.resolve(provider);\n };\n}\n", "import { AnthropicProvider } from './AnthropicProvider';\n\nconst Anthropic = {\n AnthropicProvider\n};\n\nexport default Anthropic;\n"],
4
+ "sourcesContent": ["import CreativeEditorSDK, { AssetResult } from '@cesdk/cesdk-js';\nimport { RenderCustomProperty } from '../../core/provider';\n\n/**\n * Provides render function for a image url property that allows\n * to select an image from the library with a MediaPreview\n *\n * By default this expects the property key to be `image_url`. This can be changed in the options.\n */\nfunction renderImageUrlProperty(\n provderId: string,\n options: {\n cesdk: CreativeEditorSDK;\n propertyKey?: string;\n defaultUrl?: string;\n }\n): RenderCustomProperty {\n const { cesdk } = options;\n const propertyKey = options.propertyKey ?? 'image_url';\n const panelIdForImageSelection = getImageSelectionPanelId(provderId);\n\n cesdk.i18n.setTranslations({\n en: {\n [`panel.${panelIdForImageSelection}`]: 'Select Image To Change',\n 'ly.img.ai.imageSelection.selectImage.label': 'Select Image',\n 'ly.img.ai.imageSelection.error.svg':\n 'SVG images are not supported. Please choose a different image.',\n 'ly.img.ai.imageSelection.error.invalidType':\n \"Only images are supported. Found '{mimeType}'. Please choose a different image.\"\n }\n });\n\n createPanels(provderId, cesdk);\n\n const customProperties: RenderCustomProperty = {\n [propertyKey]: (context, property) => {\n const {\n builder,\n experimental: { global },\n payload\n } = context;\n\n // Check for provider configuration defaults\n let configuredDefault: string | undefined;\n const providerConfig = (context as any).providerConfig;\n const pluginConfig = (context as any).config;\n\n // Check provider config first, then plugin config\n const propertyConfig =\n providerConfig?.properties?.[property.id] ??\n (pluginConfig as any)?.properties?.[property.id];\n\n if (propertyConfig?.default) {\n if (typeof propertyConfig.default === 'function') {\n // If it's a function, call it with a basic context\n configuredDefault = propertyConfig.default({}) as string;\n } else {\n configuredDefault = propertyConfig.default as string;\n }\n }\n\n // Use configured default, then payload url, then static default\n const defaultUrl =\n configuredDefault ?? payload?.url ?? options.defaultUrl;\n const stateValue = global<string>(\n `${provderId}.${property.id}`,\n defaultUrl\n );\n\n builder.MediaPreview(property.id, {\n preview: {\n type: 'image',\n uri: stateValue.value\n },\n action: {\n label: 'ly.img.ai.imageSelection.selectImage.label',\n onClick: () => {\n if (cesdk == null) return;\n\n cesdk.ui.openPanel(panelIdForImageSelection, {\n payload: {\n onSelect: (assetResult: AssetResult) => {\n if (assetResult.meta?.uri != null) {\n stateValue.setValue(assetResult.meta?.uri);\n }\n }\n }\n });\n }\n }\n });\n\n return () => {\n return {\n id: property.id,\n type: 'string',\n value: stateValue.value\n };\n };\n }\n };\n\n return customProperties;\n}\n\nfunction createPanels(providerId: string, cesdk?: CreativeEditorSDK) {\n if (cesdk == null) return;\n\n cesdk.ui.registerPanel<{\n onSelect: (assetResult: AssetResult) => void;\n }>(getImageSelectionPanelId(providerId), ({ builder, payload }) => {\n builder.Library(`${providerId}.library.image`, {\n entries: ['ly.img.image'],\n onSelect: async (asset) => {\n const uri = asset?.meta?.uri;\n if (uri == null) return;\n\n const mimeType = await cesdk.engine.editor.getMimeType(uri);\n if (mimeType === 'image/svg+xml') {\n cesdk.ui.showNotification({\n type: 'warning',\n message: 'ly.img.ai.imageSelection.error.svg'\n });\n } else if (mimeType.startsWith('image/')) {\n payload?.onSelect(asset);\n cesdk?.ui.closePanel(getImageSelectionPanelId(providerId));\n } else {\n cesdk.ui.showNotification({\n type: 'warning',\n message: `ly.img.ai.imageSelection.error.invalidType`\n });\n }\n }\n });\n });\n}\n\nfunction getImageSelectionPanelId(providerId: string) {\n return `ly.img.ai.${providerId}.imageSelection`;\n}\n\nexport default renderImageUrlProperty;\n", "export default `\n<svg>\n <symbol\n fill=\"none\"\n xmlns=\"http://www.w3.org/2000/svg\"\n viewBox=\"0 0 24 24\"\n id=\"@imgly/plugin/formats/ratioFree\"\n >\n <path d=\"M7 6C6.44772 6 6 6.44772 6 7V9.22222H4V7C4 5.34315 5.34315 4 7 4H9.22222V6H7Z\" fill=\"currentColor\"/>\n <path d=\"M17 6H14.7778V4H17C18.6569 4 20 5.34315 20 7V9.22222H18V7C18 6.44772 17.5523 6 17 6Z\" fill=\"currentColor\"/>\n <path d=\"M6 14.7778V17C6 17.5523 6.44772 18 7 18H9.22222V20H7C5.34315 20 4 18.6569 4 17V14.7778H6Z\" fill=\"currentColor\"/>\n <path d=\"M18 17V14.7778H20V17C20 18.6569 18.6569 20 17 20H14.7778V18H17C17.5523 18 18 17.5523 18 17Z\" fill=\"currentColor\"/>\n </symbol>\n <symbol\n fill=\"none\"\n xmlns=\"http://www.w3.org/2000/svg\"\n viewBox=\"0 0 24 24\"\n id=\"@imgly/plugin/formats/ratio4by3\"\n >\n <path d=\"M6.5 13H8V15H10V16.5H6.5V13Z\" fill=\"currentColor\"/>\n <path d=\"M14 9V7.5H17.5V11H16V9H14Z\" fill=\"currentColor\"/>\n <path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M6 4C4.34315 4 3 5.34315 3 7V17C3 18.6569 4.34315 20 6 20H18C19.6569 20 21 18.6569 21 17V7C21 5.34315 19.6569 4 18 4H6ZM5 7C5 6.44772 5.44772 6 6 6H18C18.5523 6 19 6.44772 19 7V17C19 17.5523 18.5523 18 18 18H6C5.44772 18 5 17.5523 5 17V7Z\" fill=\"currentColor\"/>\n </symbol>\n <symbol\n fill=\"none\"\n xmlns=\"http://www.w3.org/2000/svg\"\n viewBox=\"0 0 24 24\"\n id=\"@imgly/plugin/formats/ratio16by9\"\n >\n <path d=\"M4.5 13H6V15H8V16.5H4.5V13Z\" fill=\"currentColor\"/>\n <path d=\"M16 9V7.5H19.5V11H18V9H16Z\" fill=\"currentColor\"/>\n <path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M4 4C2.34315 4 1 5.34315 1 7V17C1 18.6569 2.34315 20 4 20H20C21.6569 20 23 18.6569 23 17V7C23 5.34315 21.6569 4 20 4H4ZM3 7C3 6.44772 3.44772 6 4 6H20C20.5523 6 21 6.44772 21 7V17C21 17.5523 20.5523 18 20 18H4C3.44772 18 3 17.5523 3 17V7Z\" fill=\"currentColor\"/>\n </symbol>\n <symbol\n fill=\"none\"\n xmlns=\"http://www.w3.org/2000/svg\"\n viewBox=\"0 0 24 24\"\n id=\"@imgly/plugin/formats/ratio9by16\"\n >\n <path d=\"M7.5 16H9V18H11V19.5H7.5V16Z\" fill=\"currentColor\"/>\n <path d=\"M13 6V4.5H16.5V8H15V6H13Z\" fill=\"currentColor\"/>\n <path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M4 20C4 21.6569 5.34315 23 7 23H17C18.6569 23 20 21.6569 20 20V4C20 2.34315 18.6569 1 17 1H7C5.34315 1 4 2.34315 4 4V20ZM7 21C6.44772 21 6 20.5523 6 20V4C6 3.44772 6.44772 3 7 3H17C17.5523 3 18 3.44772 18 4V20C18 20.5523 17.5523 21 17 21H7Z\" fill=\"currentColor\"/>\n </symbol>\n <symbol\n fill=\"none\"\n xmlns=\"http://www.w3.org/2000/svg\"\n viewBox=\"0 0 24 24\"\n id=\"@imgly/plugin/formats/ratio3by4\"\n >\n <path d=\"M11 17.5V16H9V14H7.5V17.5H11Z\" fill=\"currentColor\"/>\n <path d=\"M15 10H16.5V6.5H13V8H15V10Z\" fill=\"currentColor\"/>\n <path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M20 18C20 19.6569 18.6569 21 17 21H7C5.34315 21 4 19.6569 4 18V6C4 4.34315 5.34315 3 7 3H17C18.6569 3 20 4.34315 20 6V18ZM17 19C17.5523 19 18 18.5523 18 18V6C18 5.44772 17.5523 5 17 5H7C6.44771 5 6 5.44771 6 6V18C6 18.5523 6.44772 19 7 19H17Z\" fill=\"currentColor\"/>\n </symbol>\n <symbol\n fill=\"none\"\n xmlns=\"http://www.w3.org/2000/svg\"\n viewBox=\"0 0 24 24\"\n id=\"@imgly/plugin/formats/ratio1by1\"\n >\n <path d=\"M17.4142 8.00009L16 6.58587L14.2929 8.29298L15.7071 9.70719L17.4142 8.00009Z\" fill=\"currentColor\"/>\n <path d=\"M13.0404 12.3739L15.0404 10.3739L13.6262 8.95965L11.6262 10.9596L13.0404 12.3739Z\" fill=\"currentColor\"/>\n <path d=\"M10.3737 15.0405L12.3737 13.0405L10.9595 11.6263L8.95953 13.6263L10.3737 15.0405Z\" fill=\"currentColor\"/>\n <path d=\"M9.70708 15.7072L8.29286 14.293L6.58576 16.0001L7.99997 17.4143L9.70708 15.7072Z\" fill=\"currentColor\"/>\n <path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M7 4C5.34315 4 4 5.34315 4 7V17C4 18.6569 5.34315 20 7 20H17C18.6569 20 20 18.6569 20 17V7C20 5.34315 18.6569 4 17 4H7ZM6 7C6 6.44772 6.44772 6 7 6H17C17.5523 6 18 6.44772 18 7V17C18 17.5523 17.5523 18 17 18H7C6.44772 18 6 17.5523 6 17V7Z\" fill=\"currentColor\"/>\n </symbol>\n</svg>\n`;\n", "import type {\n AssetDefinition,\n AssetQueryData,\n AssetResult,\n AssetSource,\n AssetsQueryResult\n} from '@cesdk/engine';\n\n/**\n * Simplified value type for select inputs that can be converted to asset definitions\n */\nexport type SelectValue = {\n id: string;\n label: string;\n thumbUri?: string;\n meta?: { [key: string]: any };\n};\n\n/**\n * Options for CustomAssetSource constructor\n */\nexport interface CustomAssetSourceOptions {\n /**\n * Optional callback function to translate asset labels\n * @param assetId - The ID of the asset to translate\n * @param fallbackLabel - The fallback label to use if translation is not available\n * @param locale - The current locale\n * @returns The translated label or fallback\n */\n translateLabel?: (\n assetId: string,\n fallbackLabel: string,\n locale: string\n ) => string;\n}\n\n/**\n * A custom AssetSource implementation that manages assets from an array\n * and provides additional functionality like to mark assets as active or changing\n * labels.\n */\nexport class CustomAssetSource implements AssetSource {\n /** The unique id of the asset source */\n id: string;\n\n /** Array of assets to be served by this source */\n private assets: AssetDefinition[];\n\n /** Set of IDs for active assets */\n private activeAssetIds: Set<string>;\n\n /** Optional translation callback function */\n private translateLabel?: (\n assetId: string,\n fallbackLabel: string,\n locale: string\n ) => string;\n\n /**\n * Creates a new instance of CustomAssetSource\n *\n * @param id - The unique identifier for this asset source\n * @param assets - Array of asset definitions or SelectValue objects to include in this source\n * @param options - Optional configuration for the asset source\n */\n constructor(\n id: string,\n assets: (AssetDefinition | SelectValue)[] = [],\n options?: CustomAssetSourceOptions\n ) {\n this.id = id;\n this.translateLabel = options?.translateLabel;\n this.assets = assets.map((asset) => {\n // Check if the asset is a SelectValue by looking for the label property as a string\n if (\n typeof (asset as SelectValue).label === 'string' &&\n !(\n (asset as AssetDefinition).label &&\n typeof (asset as AssetDefinition).label === 'object'\n )\n ) {\n const selectValue = asset as SelectValue;\n // Convert SelectValue to AssetDefinition\n return {\n id: selectValue.id,\n label: { en: selectValue.label },\n meta: selectValue.thumbUri\n ? { ...(selectValue.meta ?? {}), thumbUri: selectValue.thumbUri }\n : selectValue.meta\n } as AssetDefinition;\n }\n return asset as AssetDefinition;\n });\n this.activeAssetIds = new Set<string>();\n\n // Automatically set first asset as active if available\n if (this.assets.length > 0) {\n this.activeAssetIds.add(this.assets[0].id);\n }\n }\n\n /**\n * Find assets based on the provided query data\n * Supports pagination, searching, filtering, and active-first sorting\n *\n * @param queryData - Query parameters to filter and sort assets\n * @returns Promise with the query results\n */\n async findAssets(\n queryData: AssetQueryData\n ): Promise<AssetsQueryResult | undefined> {\n const {\n page,\n perPage,\n locale = 'en',\n sortActiveFirst,\n query,\n tags,\n groups,\n excludeGroups,\n sortingOrder,\n sortKey\n } = queryData;\n\n // Start with all assets\n let filteredAssets = [...this.assets];\n\n // Filter by groups if provided\n if (groups && groups.length > 0) {\n filteredAssets = filteredAssets.filter(\n (asset) =>\n asset.groups && groups.some((group) => asset.groups?.includes(group))\n );\n }\n\n // Filter out excluded groups if provided\n if (excludeGroups && excludeGroups.length > 0) {\n filteredAssets = filteredAssets.filter(\n (asset) =>\n !asset.groups ||\n !excludeGroups.some((group) => asset.groups?.includes(group))\n );\n }\n\n // Filter by query (search in label and tags)\n if (query) {\n const lowerQuery = query.toLowerCase();\n filteredAssets = filteredAssets.filter((asset) => {\n const label = asset.label?.[locale]?.toLowerCase();\n const assetTags = asset.tags?.[locale] || [];\n\n return (\n (label && label.includes(lowerQuery)) ||\n assetTags.some((tag) => tag.toLowerCase().includes(lowerQuery))\n );\n });\n }\n\n // Filter by exact tags if provided\n if (tags && tags.length > 0) {\n const tagArray = Array.isArray(tags) ? tags : [tags];\n filteredAssets = filteredAssets.filter((asset) => {\n const assetTags = asset.tags?.[locale] || [];\n return tagArray.some((tag) => assetTags.includes(tag));\n });\n }\n\n // Sort by active first if requested\n if (sortActiveFirst) {\n filteredAssets.sort((a, b) => {\n const aActive = this.activeAssetIds.has(a.id);\n const bActive = this.activeAssetIds.has(b.id);\n\n if (aActive && !bActive) return -1;\n if (!aActive && bActive) return 1;\n return 0;\n });\n }\n\n // Sort by sortKey if provided\n if (sortKey && sortKey !== 'id') {\n filteredAssets.sort((a, b) => {\n // Sort by metadata field\n const aValue = a.meta?.[sortKey];\n const bValue = b.meta?.[sortKey];\n\n if (aValue === undefined) return 1;\n if (bValue === undefined) return -1;\n\n if (typeof aValue === 'string' && typeof bValue === 'string') {\n return sortingOrder === 'Descending'\n ? bValue.localeCompare(aValue)\n : aValue.localeCompare(bValue);\n }\n\n if (typeof aValue === 'number' && typeof bValue === 'number') {\n return sortingOrder === 'Descending'\n ? bValue - aValue\n : aValue - bValue;\n }\n\n return 0;\n });\n } else if (sortKey === 'id') {\n // Sort by id\n filteredAssets.sort((a, b) => {\n return sortingOrder === 'Descending'\n ? b.id.localeCompare(a.id)\n : a.id.localeCompare(b.id);\n });\n }\n\n // Calculate pagination\n const total = filteredAssets.length;\n const startIndex = page * perPage;\n const endIndex = startIndex + perPage;\n const paginatedAssets = filteredAssets.slice(startIndex, endIndex);\n\n // Transform AssetDefinition objects to AssetResult objects\n const resultAssets: AssetResult[] = paginatedAssets.map((asset) => {\n // Use translation callback if provided, otherwise use default label\n const fallbackLabel = asset.label?.[locale] || '';\n const label = this.translateLabel\n ? this.translateLabel(asset.id, fallbackLabel, locale)\n : fallbackLabel;\n\n return {\n id: asset.id,\n groups: asset.groups,\n meta: asset.meta,\n payload: asset.payload,\n locale,\n label,\n tags: asset.tags?.[locale],\n active: this.activeAssetIds.has(asset.id)\n };\n });\n\n // Calculate next page if there are more assets\n const nextPage = endIndex < total ? page + 1 : undefined;\n\n return {\n assets: resultAssets,\n currentPage: page,\n nextPage,\n total\n };\n }\n\n updateLabel(assetId: string, label: string, locale: string): void {\n this.assets.forEach((asset) => {\n if (asset.id === assetId) {\n asset.label = asset.label || {};\n asset.label[locale] = label;\n }\n });\n }\n\n /**\n * Get the asset select value by its ID\n *\n * @param assetId - The ID of the asset to retrieve\n * @returns The SelectValue object for the asset or undefined if not found\n */\n getAssetSelectValue(assetId: string): SelectValue | undefined {\n const asset = this.assets.find(({ id }) => id === assetId);\n if (asset) {\n // Get translated label if translation callback is available\n const label = this.translateLabel\n ? this.translateLabel(asset.id, asset.label?.en || '', '')\n : asset.label?.en || '';\n\n return {\n id: asset.id,\n label,\n thumbUri: asset.meta?.thumbUri\n };\n }\n return undefined;\n }\n\n /**\n * Get the currently active asset as a SelectValue\n *\n * @returns The SelectValue object for the active asset or undefined if no asset is active\n */\n getActiveSelectValue(): SelectValue | undefined {\n const activeIds = this.getActiveAssetIds();\n if (activeIds.length > 0) {\n const asset = this.assets.find(({ id }) => id === activeIds[0]);\n if (asset) {\n // Get translated label if translation callback is available\n const label = this.translateLabel\n ? this.translateLabel(asset.id, asset.label?.en || '', '')\n : asset.label?.en || '';\n\n return {\n id: asset.id,\n label,\n thumbUri: asset.meta?.thumbUri\n };\n }\n }\n return undefined;\n }\n\n /**\n * Get the translated label for an asset by its ID\n *\n * @param assetId - The ID of the asset\n * @returns The translated label or undefined if asset not found\n */\n getTranslatedLabel(assetId: string): string | undefined {\n const asset = this.assets.find(({ id }) => id === assetId);\n if (asset) {\n return this.translateLabel\n ? this.translateLabel(asset.id, asset.label?.en || '', '')\n : asset.label?.en || '';\n }\n return undefined;\n }\n\n /**\n * Get an asset by its ID\n */\n getAsset(id: string): AssetDefinition | undefined {\n return this.assets.find((asset) => asset.id === id);\n }\n\n /**\n * Set an asset as active by its ID\n *\n * @param assetId - The ID of the asset to mark as active\n */\n setAssetActive(assetId: string): void {\n this.activeAssetIds.add(assetId);\n }\n\n /**\n * Get all active asset IDs\n *\n * @returns Array of active asset IDs\n */\n getActiveAssetIds(): string[] {\n return Array.from(this.activeAssetIds);\n }\n\n /**\n * Set multiple assets as active by their IDs\n *\n * @param assetIds - Array of asset IDs to mark as active\n */\n setAssetsActive(assetIds: string[]): void {\n assetIds.forEach((id) => this.activeAssetIds.add(id));\n }\n\n /**\n * Set an asset as inactive by its ID\n *\n * @param assetId - The ID of the asset to mark as inactive\n */\n setAssetInactive(assetId: string): void {\n this.activeAssetIds.delete(assetId);\n }\n\n /**\n * Clear all active assets\n */\n clearActiveAssets(): void {\n this.activeAssetIds.clear();\n }\n\n /**\n * Check if an asset is marked as active\n *\n * @param assetId - The ID of the asset to check\n * @returns True if the asset is active, false otherwise\n */\n isAssetActive(assetId: string): boolean {\n return this.activeAssetIds.has(assetId);\n }\n\n /**\n * Add an asset to this source\n *\n * @param asset - The asset definition to add\n */\n addAsset(asset: AssetDefinition): void {\n // Check if asset with this ID already exists\n const existingIndex = this.assets.findIndex((a) => a.id === asset.id);\n if (existingIndex >= 0) {\n // Replace existing asset\n this.assets[existingIndex] = asset;\n } else {\n // Add new asset\n this.assets.push(asset);\n }\n }\n\n /**\n * Remove an asset from this source\n *\n * @param assetId - The ID of the asset to remove\n */\n removeAsset(assetId: string): void {\n const index = this.assets.findIndex((asset) => asset.id === assetId);\n if (index !== -1) {\n this.assets.splice(index, 1);\n this.activeAssetIds.delete(assetId);\n }\n }\n\n /**\n * Get all available groups from the assets\n *\n * @returns Array of unique group names\n */\n async getGroups(): Promise<string[]> {\n const groups = new Set<string>();\n this.assets.forEach((asset) => {\n if (asset.groups) {\n asset.groups.forEach((group) => groups.add(group));\n }\n });\n return Array.from(groups);\n }\n\n /**\n * Returns the supported MIME types for this asset source\n *\n * @returns Array of supported MIME types\n */\n getSupportedMimeTypes(): string[] {\n return [\n 'image/jpeg',\n 'image/png',\n 'image/svg+xml',\n 'image/webp',\n 'video/mp4',\n 'audio/mpeg'\n ];\n }\n}\n\n/**\n * Helper function to create a CustomAssetSource instance\n *\n * @param id - The unique identifier for this asset source\n * @param assets - Array of asset definitions or SelectValue objects to include in this source\n * @param options - Optional configuration for the asset source\n * @returns A new CustomAssetSource instance\n */\nexport function createCustomAssetSource(\n id: string,\n assets: (AssetDefinition | SelectValue)[] = [],\n options?: CustomAssetSourceOptions\n): CustomAssetSource {\n return new CustomAssetSource(id, assets, options);\n}\n\nexport default CustomAssetSource;\n", "/* eslint-disable no-console */\nimport {\n CreativeEngine,\n type AssetDefinition,\n type AssetQueryData,\n type AssetResult,\n type AssetSource,\n type AssetsQueryResult\n} from '@cesdk/cesdk-js';\n\ntype BlobEntry = {\n id: string;\n blob: Blob;\n};\n\n// Asset definition with meta containing insertedAt timestamp\ntype AssetEntryWithMeta = AssetDefinition;\n\n/**\n * IndexedDBAssetSource implements the AssetSource interface using IndexedDB as the storage backend.\n */\nexport class IndexedDBAssetSource implements AssetSource {\n /** The unique id of the API */\n public readonly id: string;\n\n public readonly engine: CreativeEngine;\n\n private readonly dbName: string;\n\n private readonly dbVersion: number;\n\n private readonly assetStoreName: string = 'assets';\n\n private readonly blobStoreName: string = 'blobs';\n\n private db: IDBDatabase | null = null;\n\n /**\n * Creates a new IndexedDBAssetSource\n *\n * @param id - The unique identifier for this asset source\n * @param {Object} [options] - Optional configuration options.\n * @param {string} [options.dbName] - The name of the database.\n * @param {number} [options.dbVersion] - The version number of the database.\n */\n constructor(\n id: string,\n engine: CreativeEngine,\n options?: {\n dbName?: string;\n dbVersion?: number;\n }\n ) {\n this.id = id;\n this.engine = engine;\n this.dbName = options?.dbName ?? `ly.img.assetSource/${id}`;\n this.dbVersion = options?.dbVersion ?? 1;\n }\n\n /**\n * Initialize the database connection and create object stores if needed\n */\n public async initialize(): Promise<void> {\n if (this.db) {\n return;\n }\n\n return new Promise((resolve, reject) => {\n const request = indexedDB.open(this.dbName, this.dbVersion);\n\n request.onerror = (event) => {\n reject(\n new Error(\n `Failed to open IndexedDB: ${(event.target as IDBRequest).error}`\n )\n );\n };\n\n request.onupgradeneeded = (event) => {\n const db = (event.target as IDBOpenDBRequest).result;\n\n // Create asset store if it doesn't exist\n if (!db.objectStoreNames.contains(this.assetStoreName)) {\n db.createObjectStore(this.assetStoreName, {\n keyPath: 'id'\n });\n }\n // Create blob store if it doesn't exist\n if (!db.objectStoreNames.contains(this.blobStoreName)) {\n db.createObjectStore(this.blobStoreName, {\n keyPath: 'id'\n });\n }\n };\n\n request.onsuccess = (event) => {\n this.db = (event.target as IDBOpenDBRequest).result;\n resolve();\n };\n });\n }\n\n /**\n * Close the database connection\n */\n public close(): void {\n if (this.db) {\n this.db.close();\n this.db = null;\n }\n }\n\n /**\n * Find all assets for the given type and the provided query data.\n *\n * @param queryData - The query parameters for filtering assets\n * @param insertionSortOrder - Optional parameter to sort by insertion time: 'asc' for oldest first, 'desc' for newest first (default)\n * @returns A promise that resolves to the query results or undefined if there was an error\n */\n public async findAssets(\n queryData: AssetQueryData\n ): Promise<AssetsQueryResult | undefined> {\n await this.initialize();\n\n if (!this.db) {\n throw new Error('Database not initialized');\n }\n\n try {\n // Get all assets from the store with specified insertion order\n const assetDefinitions = await this.getAllAssets('asc');\n\n let assetResults = assetDefinitions.reduce((acc, assetDefinition) => {\n const locale = queryData.locale ?? 'en';\n let label = '';\n let tags: string[] = [];\n\n // Handle localized label if available\n if (\n assetDefinition.label != null &&\n typeof assetDefinition.label === 'object' &&\n assetDefinition.label[locale]\n ) {\n label = assetDefinition.label[locale];\n }\n\n // Handle localized tags if available\n if (\n assetDefinition.tags != null &&\n typeof assetDefinition.tags === 'object' &&\n assetDefinition.tags[locale]\n ) {\n tags = assetDefinition.tags[locale];\n }\n\n const result: AssetResult = {\n ...assetDefinition,\n label,\n tags\n };\n\n if (this.filterAsset(result, queryData)) {\n acc.push(result);\n }\n\n return acc;\n }, [] as AssetResult[]);\n\n assetResults = await this.restoreBlobUrls(assetResults);\n\n // Apply sorting\n assetResults = this.sortAssets(assetResults, queryData);\n\n // Apply pagination\n const { page, perPage } = queryData;\n const startIndex = page * perPage;\n const endIndex = startIndex + perPage;\n const paginatedAssets = assetResults.slice(startIndex, endIndex);\n\n // Determine if there's a next page\n const nextPage = endIndex < assetResults.length ? page + 1 : undefined;\n\n const result = {\n assets: paginatedAssets,\n currentPage: page,\n nextPage,\n total: assetResults.length\n };\n\n return result;\n } catch (error) {\n console.error('Error finding assets:', error);\n return undefined;\n }\n }\n\n public async getGroups(): Promise<string[]> {\n await this.initialize();\n\n if (!this.db) {\n throw new Error('Database not initialized');\n }\n\n return new Promise((resolve, reject) => {\n const transaction = this.db!.transaction(this.assetStoreName, 'readonly');\n const store = transaction.objectStore(this.assetStoreName);\n const request = store.getAll();\n\n request.onsuccess = () => {\n const allGroups = new Set<string>();\n\n // Extract all groups from all assets\n (request.result as AssetResult[]).forEach((asset) => {\n if (asset.groups && Array.isArray(asset.groups)) {\n asset.groups.forEach((group) => allGroups.add(group));\n }\n });\n\n const uniqueGroups = [...allGroups];\n resolve(uniqueGroups);\n };\n\n request.onerror = () => {\n reject(new Error(`Failed to get groups: ${request.error}`));\n };\n });\n }\n\n /**\n * Adds the given asset to this source. Part of the AssetSource interface.\n *\n * @param asset - The asset definition to add\n */\n public addAsset(asset: AssetDefinition): void {\n this.initialize()\n .then(async () => {\n if (!this.db) {\n throw new Error('Database not initialized');\n }\n\n const transaction = this.db.transaction(\n this.assetStoreName,\n 'readwrite'\n );\n const assetStore = transaction.objectStore(this.assetStoreName);\n\n const blobsToStore = new Set<string>();\n processBlobUrls(asset, (value) => {\n blobsToStore.add(value);\n });\n\n setTimeout(() => {\n this.storeBlobUrls([...blobsToStore]);\n });\n\n // Ensure asset has meta object with insertedAt timestamp\n const assetWithMeta: AssetEntryWithMeta = {\n ...asset,\n meta: {\n ...asset.meta,\n insertedAt: asset.meta?.insertedAt || Date.now()\n }\n };\n\n // Store the asset in the database\n assetStore.put(assetWithMeta);\n\n transaction.onerror = () => {\n console.error(`Failed to add asset: ${transaction.error}`);\n };\n })\n .catch((error) => {\n console.error('Error initializing database:', error);\n });\n }\n\n /**\n * Removes the given asset from this source. Part of the AssetSource interface.\n *\n * @param assetId - The ID of the asset to remove\n */\n public async removeAsset(assetId: string): Promise<void> {\n const asset = await this.getAsset(assetId);\n\n return this.initialize()\n .then(() => {\n if (!this.db) {\n throw new Error('Database not initialized');\n }\n\n const transaction = this.db.transaction(\n this.assetStoreName,\n 'readwrite'\n );\n const store = transaction.objectStore(this.assetStoreName);\n store.delete(assetId);\n\n transaction.oncomplete = () => {\n processBlobUrls(asset, (value) => {\n this.removeBlob(value);\n });\n this.engine.asset.assetSourceContentsChanged(this.id);\n };\n\n transaction.onerror = () => {\n console.error(`Failed to remove asset: ${transaction.error}`);\n };\n })\n .catch((error) => {\n console.error('Error initializing database:', error);\n });\n }\n\n /**\n * Removes the given asset from this source. Part of the AssetSource interface.\n *\n * @param assetId - The ID of the asset to remove\n */\n public async removeBlob(blobId: string): Promise<void> {\n return this.initialize()\n .then(() => {\n if (!this.db) {\n throw new Error('Database not initialized');\n }\n\n const transaction = this.db.transaction(\n this.blobStoreName,\n 'readwrite'\n );\n const store = transaction.objectStore(this.blobStoreName);\n store.delete(blobId);\n\n transaction.onerror = () => {\n console.error(`Failed to remove blob: ${transaction.error}`);\n };\n })\n .catch((error) => {\n console.error('Error initializing database:', error);\n });\n }\n\n /**\n * Get all assets from the database sorted by insertion order (newest to oldest)\n *\n * @param sortOrder - Optional parameter to specify sort order: 'asc' for oldest first, 'desc' for newest first (default)\n * @returns A promise that resolves to an array of all assets\n */\n private async getAllAssets(\n sortOrder: 'asc' | 'desc' = 'desc'\n ): Promise<AssetDefinition[]> {\n return new Promise((resolve, reject) => {\n const transaction = this.db!.transaction(this.assetStoreName, 'readonly');\n const store = transaction.objectStore(this.assetStoreName);\n const request = store.getAll();\n\n request.onsuccess = () => {\n const assets = request.result as AssetEntryWithMeta[];\n\n // Sort by insertion timestamp\n assets.sort((a, b) => {\n // Default to current time if insertedAt is missing (for backward compatibility)\n // First check in meta.insertedAt, then fallback to legacy _insertedAt for backward compatibility\n const timeA =\n a.meta?.insertedAt || (a as any)._insertedAt || Date.now();\n const timeB =\n b.meta?.insertedAt || (b as any)._insertedAt || Date.now();\n\n // Sort based on requested order\n return sortOrder === 'asc'\n ? timeA - timeB // oldest first\n : timeB - timeA; // newest first (default)\n });\n\n resolve(assets);\n };\n\n request.onerror = () => {\n reject(new Error(`Failed to get assets: ${request.error}`));\n };\n });\n }\n\n // Retrieve a blob by ID\n async getAsset(id: string): Promise<AssetResult | undefined> {\n return new Promise((resolve, reject) => {\n const transaction = this.db!.transaction(this.assetStoreName, 'readonly');\n const store = transaction.objectStore(this.assetStoreName);\n const request = store.get(id);\n\n request.onsuccess = () => {\n resolve(request.result as AssetResult);\n };\n\n request.onerror = () => {\n reject(new Error(`Failed to get blob: ${request.error}`));\n };\n });\n }\n\n // Retrieve a blob by ID\n async getBlob(id: string): Promise<BlobEntry | undefined> {\n return new Promise((resolve, reject) => {\n const transaction = this.db!.transaction(this.blobStoreName, 'readonly');\n const store = transaction.objectStore(this.blobStoreName);\n const request = store.get(id);\n\n request.onsuccess = () => {\n resolve(request.result as BlobEntry);\n };\n\n request.onerror = () => {\n reject(new Error(`Failed to get blob: ${request.error}`));\n };\n });\n }\n\n async createBlobUrlFromStore(blobUrl: string): Promise<string> {\n const blobEntry = await this.getBlob(blobUrl);\n if (blobEntry != null) {\n return URL.createObjectURL(blobEntry.blob);\n }\n return blobUrl;\n }\n\n async storeBlobUrls(urls: string[]): Promise<void> {\n const blobsToStore: { [key: string]: Blob } = {};\n await Promise.all(\n urls.map(async (blobUrl) => {\n const blobResponse = await fetch(blobUrl);\n const blob = await blobResponse.blob();\n blobsToStore[blobUrl] = blob;\n })\n );\n\n return this.initialize()\n .then(async () => {\n if (!this.db) {\n throw new Error('Database not initialized');\n }\n\n const transaction = this.db.transaction(\n this.blobStoreName,\n 'readwrite'\n );\n const blobStore = transaction.objectStore(this.blobStoreName);\n\n // Store the asset in the database\n Object.entries(blobsToStore).forEach(([key, blob]) => {\n const asset: BlobEntry = { id: key, blob };\n blobStore.put(asset);\n });\n\n transaction.onerror = () => {\n console.error(`Failed to add blobs: ${transaction.error}`);\n };\n })\n .catch((error) => {\n console.error('Error initializing database:', error);\n });\n }\n\n async restoreBlobUrls(assets: AssetResult[]): Promise<AssetResult[]> {\n const blobReplaced: { [key: string]: string } = {};\n const blobUrls: Set<string> = new Set();\n processBlobUrls(assets, (value) => {\n blobUrls.add(value);\n });\n\n await Promise.all(\n [...blobUrls].map(async (value) => {\n const newUrl = await this.createBlobUrlFromStore(value);\n blobReplaced[value] = newUrl;\n })\n );\n\n return processBlobUrls(assets, (value) => {\n return blobReplaced[value] ?? value;\n });\n }\n\n /**\n * Returns if the given asset should be filtered based on query data\n *\n * @param asset - The asset to filter\n * @param queryData - The query parameters to filter by\n * @returns true if the asset should be included, false otherwise\n */\n private filterAsset(asset: AssetResult, queryData: AssetQueryData): boolean {\n const { query, tags, groups, excludeGroups } = queryData;\n\n // Filter by query string (search on label and tags)\n if (query && query.trim() !== '') {\n const lowerQuery = query.trim().toLowerCase().split(' ');\n\n const lowerLabel = asset.label?.toLowerCase() ?? '';\n const lowerTags = asset.tags?.map((tag) => tag.toLowerCase()) ?? [];\n\n const matchLabelOrTag = lowerQuery.every((word) => {\n return (\n lowerLabel.includes(word) ||\n lowerTags.some((tag) => tag.includes(word))\n );\n });\n\n if (!matchLabelOrTag) {\n return false;\n }\n }\n\n // Filter by exact tags if provided by the query\n if (tags) {\n const tagList = Array.isArray(tags) ? tags : [tags];\n if (\n tagList.length > 0 &&\n (!asset.tags || !tagList.every((tag) => asset.tags?.includes(tag)))\n ) {\n return false;\n }\n }\n\n // Filter by groups\n if (groups && groups.length > 0) {\n if (\n !asset.groups ||\n !groups.some((group) => asset.groups?.includes(group))\n ) {\n return false;\n }\n }\n\n // Filter by excluded groups\n if (excludeGroups && excludeGroups.length > 0) {\n if (\n asset.groups &&\n asset.groups.some((group) => excludeGroups.includes(group))\n ) {\n return false;\n }\n }\n\n return true;\n }\n\n /**\n * Sort assets based on query data\n *\n * @param assets - The assets to sort\n * @param queryData - The query parameters with sorting information\n * @returns The sorted assets\n */\n private sortAssets(\n assets: AssetResult[],\n queryData: AssetQueryData\n ): AssetResult[] {\n const { sortingOrder, sortKey, sortActiveFirst } = queryData;\n\n // Clone the array to avoid modifying the original\n const sortedAssets = [...assets];\n\n // If no sorting order specified or set to 'None', return the current order\n if (!sortingOrder || sortingOrder === 'None') {\n return sortedAssets;\n }\n\n // Sort by the specified key\n if (sortKey) {\n sortedAssets.sort((a, b) => {\n let valueA;\n let valueB;\n\n if (sortKey === 'id') {\n valueA = a.id;\n valueB = b.id;\n } else {\n // Handle metadata sorting (assuming metadata is stored in a 'metadata' field)\n valueA = a.meta?.[sortKey] ?? null;\n valueB = b.meta?.[sortKey] ?? null;\n }\n\n // Handle null/undefined values\n if (valueA === null || valueA === undefined)\n return sortingOrder === 'Ascending' ? -1 : 1;\n if (valueB === null || valueB === undefined)\n return sortingOrder === 'Ascending' ? 1 : -1;\n\n // Compare values based on sorting order\n if (typeof valueA === 'string' && typeof valueB === 'string') {\n return sortingOrder === 'Ascending'\n ? valueA.localeCompare(valueB)\n : valueB.localeCompare(valueA);\n } else {\n return sortingOrder === 'Ascending'\n ? valueA < valueB\n ? -1\n : valueA > valueB\n ? 1\n : 0\n : valueA > valueB\n ? -1\n : valueA < valueB\n ? 1\n : 0;\n }\n });\n } else if (sortingOrder === 'Descending') {\n // If no sort key is specified, and sorting order set\n // to Descending, just reverse original order\n sortedAssets.reverse();\n }\n\n // Sort by active first if requested\n if (sortActiveFirst) {\n sortedAssets.sort((a, b) => {\n if (a.active && !b.active) return -1;\n if (!a.active && b.active) return 1;\n return 0;\n });\n }\n\n return sortedAssets;\n }\n}\n\n/**\n * Goes through an object and calls a callback for every string value that starts with 'blob:'\n * The callback can return a string to replace the original value\n * @param obj The object to traverse\n * @param callback Function to call when a blob URL is found, can return a replacement value\n * @param path Current path in the object (used for recursion)\n * @returns The modified object (or the original if no replacements were made)\n */\nfunction processBlobUrls<T>(\n obj: T,\n callback: (value: string, path: string) => string | void,\n path: string = ''\n): T {\n // Return the object as is if it's null or not an object\n if (obj === null || typeof obj !== 'object') {\n return obj;\n }\n\n // Handle arrays\n if (Array.isArray(obj)) {\n for (let index = 0; index < obj.length; index++) {\n const currentPath = path ? `${path}[${index}]` : `[${index}]`;\n\n if (typeof obj[index] === 'string' && obj[index].startsWith('blob:')) {\n const replacement = callback(obj[index], currentPath);\n if (typeof replacement === 'string') {\n obj[index] = replacement;\n }\n } else {\n obj[index] = processBlobUrls(obj[index], callback, currentPath);\n }\n }\n return obj;\n }\n\n // Handle regular objects\n for (const key in obj) {\n if (Object.prototype.hasOwnProperty.call(obj, key)) {\n const value = obj[key];\n const currentPath = path ? `${path}.${key}` : key;\n\n if (typeof value === 'string' && value.startsWith('blob:')) {\n const replacement = callback(value, currentPath);\n if (typeof replacement === 'string') {\n // @ts-ignore\n obj[key] = replacement;\n }\n } else {\n obj[key] = processBlobUrls(value, callback, currentPath);\n }\n }\n }\n\n return obj;\n}\n\nexport default IndexedDBAssetSource;\n", "/* eslint-disable @typescript-eslint/no-unused-vars */\nimport type {\n AssetDefinition,\n AssetQueryData,\n AssetResult,\n AssetSource,\n AssetsQueryResult\n} from '@cesdk/cesdk-js';\nimport CreativeEditorSDK from '@cesdk/cesdk-js';\n\n/**\n * AggregatedAssetSource implements the AssetSource interface by aggregating\n * multiple asset sources via the cesdk API and combining their results.\n *\n * This asset source is read-only - it does not support adding or removing assets.\n */\nexport class AggregatedAssetSource implements AssetSource {\n /** The unique id of the asset source */\n public readonly id: string;\n\n /** The Creative Editor SDK instance */\n private cesdk: CreativeEditorSDK;\n\n /** The IDs of asset sources to aggregate */\n private assetSourceIds: string[];\n\n /**\n * Creates a new AggregatedAssetSource\n *\n * @param id - The unique identifier for this asset source\n * @param cesdk - The Creative Editor SDK instance\n * @param assetSourceIds - The IDs of asset sources to aggregate\n */\n constructor(id: string, cesdk: CreativeEditorSDK, assetSourceIds: string[]) {\n this.id = id;\n this.cesdk = cesdk;\n this.assetSourceIds = assetSourceIds;\n }\n\n /**\n * Find assets across all aggregated sources based on the provided query data\n * Results are sorted by the insertedAt timestamp in meta field\n *\n * @param queryData - Query parameters to filter and sort assets\n * @returns Promise with the query results\n */\n async findAssets(\n queryData: AssetQueryData\n ): Promise<AssetsQueryResult | undefined> {\n try {\n // Query all asset sources via cesdk\n const queryPromises = this.assetSourceIds.map((sourceId) =>\n this.cesdk.engine.asset.findAssets(sourceId, {\n ...queryData,\n // Increase page size to get all assets from each source\n // We'll handle pagination after merging\n perPage: 9999,\n page: 0\n })\n );\n\n // Wait for all queries to complete\n const results = await Promise.all(queryPromises);\n\n // Combine all assets from all sources\n let allAssets: AssetResult[] = [];\n results.forEach((result) => {\n if (result?.assets) {\n allAssets = allAssets.concat(result.assets);\n }\n });\n\n // Sort by insertedAt timestamp\n allAssets.sort((a, b) => {\n const timeA = (a.meta?.insertedAt as number) || 0;\n const timeB = (b.meta?.insertedAt as number) || 0;\n\n // Sort newest first (descending)\n return timeB - timeA;\n });\n\n // Apply pagination after merging\n const { page, perPage } = queryData;\n const startIndex = page * perPage;\n const endIndex = startIndex + perPage;\n const paginatedAssets = allAssets.slice(startIndex, endIndex);\n\n // Calculate if there is a next page\n const nextPage = endIndex < allAssets.length ? page + 1 : undefined;\n\n return {\n assets: paginatedAssets,\n currentPage: page,\n nextPage,\n total: allAssets.length\n };\n } catch (error) {\n // eslint-disable-next-line no-console\n console.error('Error finding assets:', error);\n return undefined;\n }\n }\n\n /**\n * Retrieves all groups from all aggregated asset sources using cesdk\n * @returns Promise with an array of unique group names\n */\n async getGroups(): Promise<string[]> {\n const groupPromises = this.assetSourceIds.map((sourceId) =>\n this.cesdk.engine.asset.getGroups(sourceId)\n );\n\n const groupArrays = await Promise.all(groupPromises);\n\n // Combine and deduplicate groups\n const uniqueGroups = new Set<string>();\n groupArrays.forEach((groups) => {\n groups.forEach((group) => uniqueGroups.add(group));\n });\n\n return Array.from(uniqueGroups);\n }\n\n /**\n * This operation is not supported in AggregatedAssetSource\n * @throws Error - This method is not supported\n */\n addAsset(_asset: AssetDefinition): void {\n throw new Error('AggregatedAssetSource does not support adding assets');\n }\n\n /**\n * This operation is not supported in AggregatedAssetSource\n * @throws Error - This method is not supported\n */\n removeAsset(_assetId: string): void {\n throw new Error('AggregatedAssetSource does not support removing assets');\n }\n}\n\n/**\n * Helper function to create an AggregatedAssetSource instance\n *\n * @param id - The unique identifier for this asset source\n * @param cesdk - The Creative Editor SDK instance\n * @param assetSourceIds - The IDs of asset sources to aggregate\n * @returns A new AggregatedAssetSource instance\n */\nexport function createAggregatedAssetSource(\n id: string,\n cesdk: CreativeEditorSDK,\n assetSourceIds: string[]\n): AggregatedAssetSource {\n return new AggregatedAssetSource(id, cesdk, assetSourceIds);\n}\n\nexport default AggregatedAssetSource;\n", "import { type CreativeEngine } from '@cesdk/cesdk-js';\n\nclass Metadata<V> {\n engine: CreativeEngine;\n\n key: string;\n\n constructor(engine: CreativeEngine, key: string) {\n this.engine = engine;\n this.key = key;\n }\n\n hasData(blockId: number): boolean {\n return (\n this.engine.block.isValid(blockId) &&\n this.engine.block.hasMetadata(blockId, this.key)\n );\n }\n\n get(blockId: number): V | undefined {\n if (this.hasData(blockId)) {\n return JSON.parse(this.engine.block.getMetadata(blockId, this.key));\n }\n return undefined;\n }\n\n set(blockId: number, value: V) {\n this.engine.block.setMetadata(blockId, this.key, JSON.stringify(value));\n }\n\n clear(blockId: number) {\n if (this.engine.block.hasMetadata(blockId, this.key)) {\n this.engine.block.removeMetadata(blockId, this.key);\n }\n }\n}\n\nexport default Metadata;\n", "/** Detect free variable `global` from Node.js. */\nvar freeGlobal = typeof global == 'object' && global && global.Object === Object && global;\n\nexport default freeGlobal;\n", "import freeGlobal from './_freeGlobal.js';\n\n/** Detect free variable `self`. */\nvar freeSelf = typeof self == 'object' && self && self.Object === Object && self;\n\n/** Used as a reference to the global object. */\nvar root = freeGlobal || freeSelf || Function('return this')();\n\nexport default root;\n", "import root from './_root.js';\n\n/** Built-in value references. */\nvar Symbol = root.Symbol;\n\nexport default Symbol;\n", "import Symbol from './_Symbol.js';\n\n/** Used for built-in method references. */\nvar objectProto = Object.prototype;\n\n/** Used to check objects for own properties. */\nvar hasOwnProperty = objectProto.hasOwnProperty;\n\n/**\n * Used to resolve the\n * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)\n * of values.\n */\nvar nativeObjectToString = objectProto.toString;\n\n/** Built-in value references. */\nvar symToStringTag = Symbol ? Symbol.toStringTag : undefined;\n\n/**\n * A specialized version of `baseGetTag` which ignores `Symbol.toStringTag` values.\n *\n * @private\n * @param {*} value The value to query.\n * @returns {string} Returns the raw `toStringTag`.\n */\nfunction getRawTag(value) {\n var isOwn = hasOwnProperty.call(value, symToStringTag),\n tag = value[symToStringTag];\n\n try {\n value[symToStringTag] = undefined;\n var unmasked = true;\n } catch (e) {}\n\n var result = nativeObjectToString.call(value);\n if (unmasked) {\n if (isOwn) {\n value[symToStringTag] = tag;\n } else {\n delete value[symToStringTag];\n }\n }\n return result;\n}\n\nexport default getRawTag;\n", "/** Used for built-in method references. */\nvar objectProto = Object.prototype;\n\n/**\n * Used to resolve the\n * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)\n * of values.\n */\nvar nativeObjectToString = objectProto.toString;\n\n/**\n * Converts `value` to a string using `Object.prototype.toString`.\n *\n * @private\n * @param {*} value The value to convert.\n * @returns {string} Returns the converted string.\n */\nfunction objectToString(value) {\n return nativeObjectToString.call(value);\n}\n\nexport default objectToString;\n", "import Symbol from './_Symbol.js';\nimport getRawTag from './_getRawTag.js';\nimport objectToString from './_objectToString.js';\n\n/** `Object#toString` result references. */\nvar nullTag = '[object Null]',\n undefinedTag = '[object Undefined]';\n\n/** Built-in value references. */\nvar symToStringTag = Symbol ? Symbol.toStringTag : undefined;\n\n/**\n * The base implementation of `getTag` without fallbacks for buggy environments.\n *\n * @private\n * @param {*} value The value to query.\n * @returns {string} Returns the `toStringTag`.\n */\nfunction baseGetTag(value) {\n if (value == null) {\n return value === undefined ? undefinedTag : nullTag;\n }\n return (symToStringTag && symToStringTag in Object(value))\n ? getRawTag(value)\n : objectToString(value);\n}\n\nexport default baseGetTag;\n", "/**\n * Checks if `value` is object-like. A value is object-like if it's not `null`\n * and has a `typeof` result of \"object\".\n *\n * @static\n * @memberOf _\n * @since 4.0.0\n * @category Lang\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is object-like, else `false`.\n * @example\n *\n * _.isObjectLike({});\n * // => true\n *\n * _.isObjectLike([1, 2, 3]);\n * // => true\n *\n * _.isObjectLike(_.noop);\n * // => false\n *\n * _.isObjectLike(null);\n * // => false\n */\nfunction isObjectLike(value) {\n return value != null && typeof value == 'object';\n}\n\nexport default isObjectLike;\n", "/**\n * Checks if `value` is classified as an `Array` object.\n *\n * @static\n * @memberOf _\n * @since 0.1.0\n * @category Lang\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is an array, else `false`.\n * @example\n *\n * _.isArray([1, 2, 3]);\n * // => true\n *\n * _.isArray(document.body.children);\n * // => false\n *\n * _.isArray('abc');\n * // => false\n *\n * _.isArray(_.noop);\n * // => false\n */\nvar isArray = Array.isArray;\n\nexport default isArray;\n", "/**\n * Checks if `value` is the\n * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types)\n * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)\n *\n * @static\n * @memberOf _\n * @since 0.1.0\n * @category Lang\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is an object, else `false`.\n * @example\n *\n * _.isObject({});\n * // => true\n *\n * _.isObject([1, 2, 3]);\n * // => true\n *\n * _.isObject(_.noop);\n * // => true\n *\n * _.isObject(null);\n * // => false\n */\nfunction isObject(value) {\n var type = typeof value;\n return value != null && (type == 'object' || type == 'function');\n}\n\nexport default isObject;\n", "import baseGetTag from './_baseGetTag.js';\nimport isObject from './isObject.js';\n\n/** `Object#toString` result references. */\nvar asyncTag = '[object AsyncFunction]',\n funcTag = '[object Function]',\n genTag = '[object GeneratorFunction]',\n proxyTag = '[object Proxy]';\n\n/**\n * Checks if `value` is classified as a `Function` object.\n *\n * @static\n * @memberOf _\n * @since 0.1.0\n * @category Lang\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is a function, else `false`.\n * @example\n *\n * _.isFunction(_);\n * // => true\n *\n * _.isFunction(/abc/);\n * // => false\n */\nfunction isFunction(value) {\n if (!isObject(value)) {\n return false;\n }\n // The use of `Object#toString` avoids issues with the `typeof` operator\n // in Safari 9 which returns 'object' for typed arrays and other constructors.\n var tag = baseGetTag(value);\n return tag == funcTag || tag == genTag || tag == asyncTag || tag == proxyTag;\n}\n\nexport default isFunction;\n", "import root from './_root.js';\n\n/** Used to detect overreaching core-js shims. */\nvar coreJsData = root['__core-js_shared__'];\n\nexport default coreJsData;\n", "import coreJsData from './_coreJsData.js';\n\n/** Used to detect methods masquerading as native. */\nvar maskSrcKey = (function() {\n var uid = /[^.]+$/.exec(coreJsData && coreJsData.keys && coreJsData.keys.IE_PROTO || '');\n return uid ? ('Symbol(src)_1.' + uid) : '';\n}());\n\n/**\n * Checks if `func` has its source masked.\n *\n * @private\n * @param {Function} func The function to check.\n * @returns {boolean} Returns `true` if `func` is masked, else `false`.\n */\nfunction isMasked(func) {\n return !!maskSrcKey && (maskSrcKey in func);\n}\n\nexport default isMasked;\n", "/** Used for built-in method references. */\nvar funcProto = Function.prototype;\n\n/** Used to resolve the decompiled source of functions. */\nvar funcToString = funcProto.toString;\n\n/**\n * Converts `func` to its source code.\n *\n * @private\n * @param {Function} func The function to convert.\n * @returns {string} Returns the source code.\n */\nfunction toSource(func) {\n if (func != null) {\n try {\n return funcToString.call(func);\n } catch (e) {}\n try {\n return (func + '');\n } catch (e) {}\n }\n return '';\n}\n\nexport default toSource;\n", "import isFunction from './isFunction.js';\nimport isMasked from './_isMasked.js';\nimport isObject from './isObject.js';\nimport toSource from './_toSource.js';\n\n/**\n * Used to match `RegExp`\n * [syntax characters](http://ecma-international.org/ecma-262/7.0/#sec-patterns).\n */\nvar reRegExpChar = /[\\\\^$.*+?()[\\]{}|]/g;\n\n/** Used to detect host constructors (Safari). */\nvar reIsHostCtor = /^\\[object .+?Constructor\\]$/;\n\n/** Used for built-in method references. */\nvar funcProto = Function.prototype,\n objectProto = Object.prototype;\n\n/** Used to resolve the decompiled source of functions. */\nvar funcToString = funcProto.toString;\n\n/** Used to check objects for own properties. */\nvar hasOwnProperty = objectProto.hasOwnProperty;\n\n/** Used to detect if a method is native. */\nvar reIsNative = RegExp('^' +\n funcToString.call(hasOwnProperty).replace(reRegExpChar, '\\\\$&')\n .replace(/hasOwnProperty|(function).*?(?=\\\\\\()| for .+?(?=\\\\\\])/g, '$1.*?') + '$'\n);\n\n/**\n * The base implementation of `_.isNative` without bad shim checks.\n *\n * @private\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is a native function,\n * else `false`.\n */\nfunction baseIsNative(value) {\n if (!isObject(value) || isMasked(value)) {\n return false;\n }\n var pattern = isFunction(value) ? reIsNative : reIsHostCtor;\n return pattern.test(toSource(value));\n}\n\nexport default baseIsNative;\n", "/**\n * Gets the value at `key` of `object`.\n *\n * @private\n * @param {Object} [object] The object to query.\n * @param {string} key The key of the property to get.\n * @returns {*} Returns the property value.\n */\nfunction getValue(object, key) {\n return object == null ? undefined : object[key];\n}\n\nexport default getValue;\n", "import baseIsNative from './_baseIsNative.js';\nimport getValue from './_getValue.js';\n\n/**\n * Gets the native function at `key` of `object`.\n *\n * @private\n * @param {Object} object The object to query.\n * @param {string} key The key of the method to get.\n * @returns {*} Returns the function if it's native, else `undefined`.\n */\nfunction getNative(object, key) {\n var value = getValue(object, key);\n return baseIsNative(value) ? value : undefined;\n}\n\nexport default getNative;\n", "import getNative from './_getNative.js';\nimport root from './_root.js';\n\n/* Built-in method references that are verified to be native. */\nvar WeakMap = getNative(root, 'WeakMap');\n\nexport default WeakMap;\n", "/** Used as references for various `Number` constants. */\nvar MAX_SAFE_INTEGER = 9007199254740991;\n\n/** Used to detect unsigned integer values. */\nvar reIsUint = /^(?:0|[1-9]\\d*)$/;\n\n/**\n * Checks if `value` is a valid array-like index.\n *\n * @private\n * @param {*} value The value to check.\n * @param {number} [length=MAX_SAFE_INTEGER] The upper bounds of a valid index.\n * @returns {boolean} Returns `true` if `value` is a valid index, else `false`.\n */\nfunction isIndex(value, length) {\n var type = typeof value;\n length = length == null ? MAX_SAFE_INTEGER : length;\n\n return !!length &&\n (type == 'number' ||\n (type != 'symbol' && reIsUint.test(value))) &&\n (value > -1 && value % 1 == 0 && value < length);\n}\n\nexport default isIndex;\n", "/**\n * Performs a\n * [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero)\n * comparison between two values to determine if they are equivalent.\n *\n * @static\n * @memberOf _\n * @since 4.0.0\n * @category Lang\n * @param {*} value The value to compare.\n * @param {*} other The other value to compare.\n * @returns {boolean} Returns `true` if the values are equivalent, else `false`.\n * @example\n *\n * var object = { 'a': 1 };\n * var other = { 'a': 1 };\n *\n * _.eq(object, object);\n * // => true\n *\n * _.eq(object, other);\n * // => false\n *\n * _.eq('a', 'a');\n * // => true\n *\n * _.eq('a', Object('a'));\n * // => false\n *\n * _.eq(NaN, NaN);\n * // => true\n */\nfunction eq(value, other) {\n return value === other || (value !== value && other !== other);\n}\n\nexport default eq;\n", "/** Used as references for various `Number` constants. */\nvar MAX_SAFE_INTEGER = 9007199254740991;\n\n/**\n * Checks if `value` is a valid array-like length.\n *\n * **Note:** This method is loosely based on\n * [`ToLength`](http://ecma-international.org/ecma-262/7.0/#sec-tolength).\n *\n * @static\n * @memberOf _\n * @since 4.0.0\n * @category Lang\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is a valid length, else `false`.\n * @example\n *\n * _.isLength(3);\n * // => true\n *\n * _.isLength(Number.MIN_VALUE);\n * // => false\n *\n * _.isLength(Infinity);\n * // => false\n *\n * _.isLength('3');\n * // => false\n */\nfunction isLength(value) {\n return typeof value == 'number' &&\n value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;\n}\n\nexport default isLength;\n", "import isFunction from './isFunction.js';\nimport isLength from './isLength.js';\n\n/**\n * Checks if `value` is array-like. A value is considered array-like if it's\n * not a function and has a `value.length` that's an integer greater than or\n * equal to `0` and less than or equal to `Number.MAX_SAFE_INTEGER`.\n *\n * @static\n * @memberOf _\n * @since 4.0.0\n * @category Lang\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is array-like, else `false`.\n * @example\n *\n * _.isArrayLike([1, 2, 3]);\n * // => true\n *\n * _.isArrayLike(document.body.children);\n * // => true\n *\n * _.isArrayLike('abc');\n * // => true\n *\n * _.isArrayLike(_.noop);\n * // => false\n */\nfunction isArrayLike(value) {\n return value != null && isLength(value.length) && !isFunction(value);\n}\n\nexport default isArrayLike;\n", "/** Used for built-in method references. */\nvar objectProto = Object.prototype;\n\n/**\n * Checks if `value` is likely a prototype object.\n *\n * @private\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is a prototype, else `false`.\n */\nfunction isPrototype(value) {\n var Ctor = value && value.constructor,\n proto = (typeof Ctor == 'function' && Ctor.prototype) || objectProto;\n\n return value === proto;\n}\n\nexport default isPrototype;\n", "/**\n * The base implementation of `_.times` without support for iteratee shorthands\n * or max array length checks.\n *\n * @private\n * @param {number} n The number of times to invoke `iteratee`.\n * @param {Function} iteratee The function invoked per iteration.\n * @returns {Array} Returns the array of results.\n */\nfunction baseTimes(n, iteratee) {\n var index = -1,\n result = Array(n);\n\n while (++index < n) {\n result[index] = iteratee(index);\n }\n return result;\n}\n\nexport default baseTimes;\n", "import baseGetTag from './_baseGetTag.js';\nimport isObjectLike from './isObjectLike.js';\n\n/** `Object#toString` result references. */\nvar argsTag = '[object Arguments]';\n\n/**\n * The base implementation of `_.isArguments`.\n *\n * @private\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is an `arguments` object,\n */\nfunction baseIsArguments(value) {\n return isObjectLike(value) && baseGetTag(value) == argsTag;\n}\n\nexport default baseIsArguments;\n", "import baseIsArguments from './_baseIsArguments.js';\nimport isObjectLike from './isObjectLike.js';\n\n/** Used for built-in method references. */\nvar objectProto = Object.prototype;\n\n/** Used to check objects for own properties. */\nvar hasOwnProperty = objectProto.hasOwnProperty;\n\n/** Built-in value references. */\nvar propertyIsEnumerable = objectProto.propertyIsEnumerable;\n\n/**\n * Checks if `value` is likely an `arguments` object.\n *\n * @static\n * @memberOf _\n * @since 0.1.0\n * @category Lang\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is an `arguments` object,\n * else `false`.\n * @example\n *\n * _.isArguments(function() { return arguments; }());\n * // => true\n *\n * _.isArguments([1, 2, 3]);\n * // => false\n */\nvar isArguments = baseIsArguments(function() { return arguments; }()) ? baseIsArguments : function(value) {\n return isObjectLike(value) && hasOwnProperty.call(value, 'callee') &&\n !propertyIsEnumerable.call(value, 'callee');\n};\n\nexport default isArguments;\n", "/**\n * This method returns `false`.\n *\n * @static\n * @memberOf _\n * @since 4.13.0\n * @category Util\n * @returns {boolean} Returns `false`.\n * @example\n *\n * _.times(2, _.stubFalse);\n * // => [false, false]\n */\nfunction stubFalse() {\n return false;\n}\n\nexport default stubFalse;\n", "import root from './_root.js';\nimport stubFalse from './stubFalse.js';\n\n/** Detect free variable `exports`. */\nvar freeExports = typeof exports == 'object' && exports && !exports.nodeType && exports;\n\n/** Detect free variable `module`. */\nvar freeModule = freeExports && typeof module == 'object' && module && !module.nodeType && module;\n\n/** Detect the popular CommonJS extension `module.exports`. */\nvar moduleExports = freeModule && freeModule.exports === freeExports;\n\n/** Built-in value references. */\nvar Buffer = moduleExports ? root.Buffer : undefined;\n\n/* Built-in method references for those with the same name as other `lodash` methods. */\nvar nativeIsBuffer = Buffer ? Buffer.isBuffer : undefined;\n\n/**\n * Checks if `value` is a buffer.\n *\n * @static\n * @memberOf _\n * @since 4.3.0\n * @category Lang\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is a buffer, else `false`.\n * @example\n *\n * _.isBuffer(new Buffer(2));\n * // => true\n *\n * _.isBuffer(new Uint8Array(2));\n * // => false\n */\nvar isBuffer = nativeIsBuffer || stubFalse;\n\nexport default isBuffer;\n", "import baseGetTag from './_baseGetTag.js';\nimport isLength from './isLength.js';\nimport isObjectLike from './isObjectLike.js';\n\n/** `Object#toString` result references. */\nvar argsTag = '[object Arguments]',\n arrayTag = '[object Array]',\n boolTag = '[object Boolean]',\n dateTag = '[object Date]',\n errorTag = '[object Error]',\n funcTag = '[object Function]',\n mapTag = '[object Map]',\n numberTag = '[object Number]',\n objectTag = '[object Object]',\n regexpTag = '[object RegExp]',\n setTag = '[object Set]',\n stringTag = '[object String]',\n weakMapTag = '[object WeakMap]';\n\nvar arrayBufferTag = '[object ArrayBuffer]',\n dataViewTag = '[object DataView]',\n float32Tag = '[object Float32Array]',\n float64Tag = '[object Float64Array]',\n int8Tag = '[object Int8Array]',\n int16Tag = '[object Int16Array]',\n int32Tag = '[object Int32Array]',\n uint8Tag = '[object Uint8Array]',\n uint8ClampedTag = '[object Uint8ClampedArray]',\n uint16Tag = '[object Uint16Array]',\n uint32Tag = '[object Uint32Array]';\n\n/** Used to identify `toStringTag` values of typed arrays. */\nvar typedArrayTags = {};\ntypedArrayTags[float32Tag] = typedArrayTags[float64Tag] =\ntypedArrayTags[int8Tag] = typedArrayTags[int16Tag] =\ntypedArrayTags[int32Tag] = typedArrayTags[uint8Tag] =\ntypedArrayTags[uint8ClampedTag] = typedArrayTags[uint16Tag] =\ntypedArrayTags[uint32Tag] = true;\ntypedArrayTags[argsTag] = typedArrayTags[arrayTag] =\ntypedArrayTags[arrayBufferTag] = typedArrayTags[boolTag] =\ntypedArrayTags[dataViewTag] = typedArrayTags[dateTag] =\ntypedArrayTags[errorTag] = typedArrayTags[funcTag] =\ntypedArrayTags[mapTag] = typedArrayTags[numberTag] =\ntypedArrayTags[objectTag] = typedArrayTags[regexpTag] =\ntypedArrayTags[setTag] = typedArrayTags[stringTag] =\ntypedArrayTags[weakMapTag] = false;\n\n/**\n * The base implementation of `_.isTypedArray` without Node.js optimizations.\n *\n * @private\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is a typed array, else `false`.\n */\nfunction baseIsTypedArray(value) {\n return isObjectLike(value) &&\n isLength(value.length) && !!typedArrayTags[baseGetTag(value)];\n}\n\nexport default baseIsTypedArray;\n", "/**\n * The base implementation of `_.unary` without support for storing metadata.\n *\n * @private\n * @param {Function} func The function to cap arguments for.\n * @returns {Function} Returns the new capped function.\n */\nfunction baseUnary(func) {\n return function(value) {\n return func(value);\n };\n}\n\nexport default baseUnary;\n", "import freeGlobal from './_freeGlobal.js';\n\n/** Detect free variable `exports`. */\nvar freeExports = typeof exports == 'object' && exports && !exports.nodeType && exports;\n\n/** Detect free variable `module`. */\nvar freeModule = freeExports && typeof module == 'object' && module && !module.nodeType && module;\n\n/** Detect the popular CommonJS extension `module.exports`. */\nvar moduleExports = freeModule && freeModule.exports === freeExports;\n\n/** Detect free variable `process` from Node.js. */\nvar freeProcess = moduleExports && freeGlobal.process;\n\n/** Used to access faster Node.js helpers. */\nvar nodeUtil = (function() {\n try {\n // Use `util.types` for Node.js 10+.\n var types = freeModule && freeModule.require && freeModule.require('util').types;\n\n if (types) {\n return types;\n }\n\n // Legacy `process.binding('util')` for Node.js < 10.\n return freeProcess && freeProcess.binding && freeProcess.binding('util');\n } catch (e) {}\n}());\n\nexport default nodeUtil;\n", "import baseIsTypedArray from './_baseIsTypedArray.js';\nimport baseUnary from './_baseUnary.js';\nimport nodeUtil from './_nodeUtil.js';\n\n/* Node.js helper references. */\nvar nodeIsTypedArray = nodeUtil && nodeUtil.isTypedArray;\n\n/**\n * Checks if `value` is classified as a typed array.\n *\n * @static\n * @memberOf _\n * @since 3.0.0\n * @category Lang\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is a typed array, else `false`.\n * @example\n *\n * _.isTypedArray(new Uint8Array);\n * // => true\n *\n * _.isTypedArray([]);\n * // => false\n */\nvar isTypedArray = nodeIsTypedArray ? baseUnary(nodeIsTypedArray) : baseIsTypedArray;\n\nexport default isTypedArray;\n", "import baseTimes from './_baseTimes.js';\nimport isArguments from './isArguments.js';\nimport isArray from './isArray.js';\nimport isBuffer from './isBuffer.js';\nimport isIndex from './_isIndex.js';\nimport isTypedArray from './isTypedArray.js';\n\n/** Used for built-in method references. */\nvar objectProto = Object.prototype;\n\n/** Used to check objects for own properties. */\nvar hasOwnProperty = objectProto.hasOwnProperty;\n\n/**\n * Creates an array of the enumerable property names of the array-like `value`.\n *\n * @private\n * @param {*} value The value to query.\n * @param {boolean} inherited Specify returning inherited property names.\n * @returns {Array} Returns the array of property names.\n */\nfunction arrayLikeKeys(value, inherited) {\n var isArr = isArray(value),\n isArg = !isArr && isArguments(value),\n isBuff = !isArr && !isArg && isBuffer(value),\n isType = !isArr && !isArg && !isBuff && isTypedArray(value),\n skipIndexes = isArr || isArg || isBuff || isType,\n result = skipIndexes ? baseTimes(value.length, String) : [],\n length = result.length;\n\n for (var key in value) {\n if ((inherited || hasOwnProperty.call(value, key)) &&\n !(skipIndexes && (\n // Safari 9 has enumerable `arguments.length` in strict mode.\n key == 'length' ||\n // Node.js 0.10 has enumerable non-index properties on buffers.\n (isBuff && (key == 'offset' || key == 'parent')) ||\n // PhantomJS 2 has enumerable non-index properties on typed arrays.\n (isType && (key == 'buffer' || key == 'byteLength' || key == 'byteOffset')) ||\n // Skip index properties.\n isIndex(key, length)\n ))) {\n result.push(key);\n }\n }\n return result;\n}\n\nexport default arrayLikeKeys;\n", "/**\n * Creates a unary function that invokes `func` with its argument transformed.\n *\n * @private\n * @param {Function} func The function to wrap.\n * @param {Function} transform The argument transform.\n * @returns {Function} Returns the new function.\n */\nfunction overArg(func, transform) {\n return function(arg) {\n return func(transform(arg));\n };\n}\n\nexport default overArg;\n", "import overArg from './_overArg.js';\n\n/* Built-in method references for those with the same name as other `lodash` methods. */\nvar nativeKeys = overArg(Object.keys, Object);\n\nexport default nativeKeys;\n", "import isPrototype from './_isPrototype.js';\nimport nativeKeys from './_nativeKeys.js';\n\n/** Used for built-in method references. */\nvar objectProto = Object.prototype;\n\n/** Used to check objects for own properties. */\nvar hasOwnProperty = objectProto.hasOwnProperty;\n\n/**\n * The base implementation of `_.keys` which doesn't treat sparse arrays as dense.\n *\n * @private\n * @param {Object} object The object to query.\n * @returns {Array} Returns the array of property names.\n */\nfunction baseKeys(object) {\n if (!isPrototype(object)) {\n return nativeKeys(object);\n }\n var result = [];\n for (var key in Object(object)) {\n if (hasOwnProperty.call(object, key) && key != 'constructor') {\n result.push(key);\n }\n }\n return result;\n}\n\nexport default baseKeys;\n", "import arrayLikeKeys from './_arrayLikeKeys.js';\nimport baseKeys from './_baseKeys.js';\nimport isArrayLike from './isArrayLike.js';\n\n/**\n * Creates an array of the own enumerable property names of `object`.\n *\n * **Note:** Non-object values are coerced to objects. See the\n * [ES spec](http://ecma-international.org/ecma-262/7.0/#sec-object.keys)\n * for more details.\n *\n * @static\n * @since 0.1.0\n * @memberOf _\n * @category Object\n * @param {Object} object The object to query.\n * @returns {Array} Returns the array of property names.\n * @example\n *\n * function Foo() {\n * this.a = 1;\n * this.b = 2;\n * }\n *\n * Foo.prototype.c = 3;\n *\n * _.keys(new Foo);\n * // => ['a', 'b'] (iteration order is not guaranteed)\n *\n * _.keys('hi');\n * // => ['0', '1']\n */\nfunction keys(object) {\n return isArrayLike(object) ? arrayLikeKeys(object) : baseKeys(object);\n}\n\nexport default keys;\n", "import getNative from './_getNative.js';\n\n/* Built-in method references that are verified to be native. */\nvar nativeCreate = getNative(Object, 'create');\n\nexport default nativeCreate;\n", "import nativeCreate from './_nativeCreate.js';\n\n/**\n * Removes all key-value entries from the hash.\n *\n * @private\n * @name clear\n * @memberOf Hash\n */\nfunction hashClear() {\n this.__data__ = nativeCreate ? nativeCreate(null) : {};\n this.size = 0;\n}\n\nexport default hashClear;\n", "/**\n * Removes `key` and its value from the hash.\n *\n * @private\n * @name delete\n * @memberOf Hash\n * @param {Object} hash The hash to modify.\n * @param {string} key The key of the value to remove.\n * @returns {boolean} Returns `true` if the entry was removed, else `false`.\n */\nfunction hashDelete(key) {\n var result = this.has(key) && delete this.__data__[key];\n this.size -= result ? 1 : 0;\n return result;\n}\n\nexport default hashDelete;\n", "import nativeCreate from './_nativeCreate.js';\n\n/** Used to stand-in for `undefined` hash values. */\nvar HASH_UNDEFINED = '__lodash_hash_undefined__';\n\n/** Used for built-in method references. */\nvar objectProto = Object.prototype;\n\n/** Used to check objects for own properties. */\nvar hasOwnProperty = objectProto.hasOwnProperty;\n\n/**\n * Gets the hash value for `key`.\n *\n * @private\n * @name get\n * @memberOf Hash\n * @param {string} key The key of the value to get.\n * @returns {*} Returns the entry value.\n */\nfunction hashGet(key) {\n var data = this.__data__;\n if (nativeCreate) {\n var result = data[key];\n return result === HASH_UNDEFINED ? undefined : result;\n }\n return hasOwnProperty.call(data, key) ? data[key] : undefined;\n}\n\nexport default hashGet;\n", "import nativeCreate from './_nativeCreate.js';\n\n/** Used for built-in method references. */\nvar objectProto = Object.prototype;\n\n/** Used to check objects for own properties. */\nvar hasOwnProperty = objectProto.hasOwnProperty;\n\n/**\n * Checks if a hash value for `key` exists.\n *\n * @private\n * @name has\n * @memberOf Hash\n * @param {string} key The key of the entry to check.\n * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.\n */\nfunction hashHas(key) {\n var data = this.__data__;\n return nativeCreate ? (data[key] !== undefined) : hasOwnProperty.call(data, key);\n}\n\nexport default hashHas;\n", "import nativeCreate from './_nativeCreate.js';\n\n/** Used to stand-in for `undefined` hash values. */\nvar HASH_UNDEFINED = '__lodash_hash_undefined__';\n\n/**\n * Sets the hash `key` to `value`.\n *\n * @private\n * @name set\n * @memberOf Hash\n * @param {string} key The key of the value to set.\n * @param {*} value The value to set.\n * @returns {Object} Returns the hash instance.\n */\nfunction hashSet(key, value) {\n var data = this.__data__;\n this.size += this.has(key) ? 0 : 1;\n data[key] = (nativeCreate && value === undefined) ? HASH_UNDEFINED : value;\n return this;\n}\n\nexport default hashSet;\n", "import hashClear from './_hashClear.js';\nimport hashDelete from './_hashDelete.js';\nimport hashGet from './_hashGet.js';\nimport hashHas from './_hashHas.js';\nimport hashSet from './_hashSet.js';\n\n/**\n * Creates a hash object.\n *\n * @private\n * @constructor\n * @param {Array} [entries] The key-value pairs to cache.\n */\nfunction Hash(entries) {\n var index = -1,\n length = entries == null ? 0 : entries.length;\n\n this.clear();\n while (++index < length) {\n var entry = entries[index];\n this.set(entry[0], entry[1]);\n }\n}\n\n// Add methods to `Hash`.\nHash.prototype.clear = hashClear;\nHash.prototype['delete'] = hashDelete;\nHash.prototype.get = hashGet;\nHash.prototype.has = hashHas;\nHash.prototype.set = hashSet;\n\nexport default Hash;\n", "/**\n * Removes all key-value entries from the list cache.\n *\n * @private\n * @name clear\n * @memberOf ListCache\n */\nfunction listCacheClear() {\n this.__data__ = [];\n this.size = 0;\n}\n\nexport default listCacheClear;\n", "import eq from './eq.js';\n\n/**\n * Gets the index at which the `key` is found in `array` of key-value pairs.\n *\n * @private\n * @param {Array} array The array to inspect.\n * @param {*} key The key to search for.\n * @returns {number} Returns the index of the matched value, else `-1`.\n */\nfunction assocIndexOf(array, key) {\n var length = array.length;\n while (length--) {\n if (eq(array[length][0], key)) {\n return length;\n }\n }\n return -1;\n}\n\nexport default assocIndexOf;\n", "import assocIndexOf from './_assocIndexOf.js';\n\n/** Used for built-in method references. */\nvar arrayProto = Array.prototype;\n\n/** Built-in value references. */\nvar splice = arrayProto.splice;\n\n/**\n * Removes `key` and its value from the list cache.\n *\n * @private\n * @name delete\n * @memberOf ListCache\n * @param {string} key The key of the value to remove.\n * @returns {boolean} Returns `true` if the entry was removed, else `false`.\n */\nfunction listCacheDelete(key) {\n var data = this.__data__,\n index = assocIndexOf(data, key);\n\n if (index < 0) {\n return false;\n }\n var lastIndex = data.length - 1;\n if (index == lastIndex) {\n data.pop();\n } else {\n splice.call(data, index, 1);\n }\n --this.size;\n return true;\n}\n\nexport default listCacheDelete;\n", "import assocIndexOf from './_assocIndexOf.js';\n\n/**\n * Gets the list cache value for `key`.\n *\n * @private\n * @name get\n * @memberOf ListCache\n * @param {string} key The key of the value to get.\n * @returns {*} Returns the entry value.\n */\nfunction listCacheGet(key) {\n var data = this.__data__,\n index = assocIndexOf(data, key);\n\n return index < 0 ? undefined : data[index][1];\n}\n\nexport default listCacheGet;\n", "import assocIndexOf from './_assocIndexOf.js';\n\n/**\n * Checks if a list cache value for `key` exists.\n *\n * @private\n * @name has\n * @memberOf ListCache\n * @param {string} key The key of the entry to check.\n * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.\n */\nfunction listCacheHas(key) {\n return assocIndexOf(this.__data__, key) > -1;\n}\n\nexport default listCacheHas;\n", "import assocIndexOf from './_assocIndexOf.js';\n\n/**\n * Sets the list cache `key` to `value`.\n *\n * @private\n * @name set\n * @memberOf ListCache\n * @param {string} key The key of the value to set.\n * @param {*} value The value to set.\n * @returns {Object} Returns the list cache instance.\n */\nfunction listCacheSet(key, value) {\n var data = this.__data__,\n index = assocIndexOf(data, key);\n\n if (index < 0) {\n ++this.size;\n data.push([key, value]);\n } else {\n data[index][1] = value;\n }\n return this;\n}\n\nexport default listCacheSet;\n", "import listCacheClear from './_listCacheClear.js';\nimport listCacheDelete from './_listCacheDelete.js';\nimport listCacheGet from './_listCacheGet.js';\nimport listCacheHas from './_listCacheHas.js';\nimport listCacheSet from './_listCacheSet.js';\n\n/**\n * Creates an list cache object.\n *\n * @private\n * @constructor\n * @param {Array} [entries] The key-value pairs to cache.\n */\nfunction ListCache(entries) {\n var index = -1,\n length = entries == null ? 0 : entries.length;\n\n this.clear();\n while (++index < length) {\n var entry = entries[index];\n this.set(entry[0], entry[1]);\n }\n}\n\n// Add methods to `ListCache`.\nListCache.prototype.clear = listCacheClear;\nListCache.prototype['delete'] = listCacheDelete;\nListCache.prototype.get = listCacheGet;\nListCache.prototype.has = listCacheHas;\nListCache.prototype.set = listCacheSet;\n\nexport default ListCache;\n", "import getNative from './_getNative.js';\nimport root from './_root.js';\n\n/* Built-in method references that are verified to be native. */\nvar Map = getNative(root, 'Map');\n\nexport default Map;\n", "import Hash from './_Hash.js';\nimport ListCache from './_ListCache.js';\nimport Map from './_Map.js';\n\n/**\n * Removes all key-value entries from the map.\n *\n * @private\n * @name clear\n * @memberOf MapCache\n */\nfunction mapCacheClear() {\n this.size = 0;\n this.__data__ = {\n 'hash': new Hash,\n 'map': new (Map || ListCache),\n 'string': new Hash\n };\n}\n\nexport default mapCacheClear;\n", "/**\n * Checks if `value` is suitable for use as unique object key.\n *\n * @private\n * @param {*} value The value to check.\n * @returns {boolean} Returns `true` if `value` is suitable, else `false`.\n */\nfunction isKeyable(value) {\n var type = typeof value;\n return (type == 'string' || type == 'number' || type == 'symbol' || type == 'boolean')\n ? (value !== '__proto__')\n : (value === null);\n}\n\nexport default isKeyable;\n", "import isKeyable from './_isKeyable.js';\n\n/**\n * Gets the data for `map`.\n *\n * @private\n * @param {Object} map The map to query.\n * @param {string} key The reference key.\n * @returns {*} Returns the map data.\n */\nfunction getMapData(map, key) {\n var data = map.__data__;\n return isKeyable(key)\n ? data[typeof key == 'string' ? 'string' : 'hash']\n : data.map;\n}\n\nexport default getMapData;\n", "import getMapData from './_getMapData.js';\n\n/**\n * Removes `key` and its value from the map.\n *\n * @private\n * @name delete\n * @memberOf MapCache\n * @param {string} key The key of the value to remove.\n * @returns {boolean} Returns `true` if the entry was removed, else `false`.\n */\nfunction mapCacheDelete(key) {\n var result = getMapData(this, key)['delete'](key);\n this.size -= result ? 1 : 0;\n return result;\n}\n\nexport default mapCacheDelete;\n", "import getMapData from './_getMapData.js';\n\n/**\n * Gets the map value for `key`.\n *\n * @private\n * @name get\n * @memberOf MapCache\n * @param {string} key The key of the value to get.\n * @returns {*} Returns the entry value.\n */\nfunction mapCacheGet(key) {\n return getMapData(this, key).get(key);\n}\n\nexport default mapCacheGet;\n", "import getMapData from './_getMapData.js';\n\n/**\n * Checks if a map value for `key` exists.\n *\n * @private\n * @name has\n * @memberOf MapCache\n * @param {string} key The key of the entry to check.\n * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.\n */\nfunction mapCacheHas(key) {\n return getMapData(this, key).has(key);\n}\n\nexport default mapCacheHas;\n", "import getMapData from './_getMapData.js';\n\n/**\n * Sets the map `key` to `value`.\n *\n * @private\n * @name set\n * @memberOf MapCache\n * @param {string} key The key of the value to set.\n * @param {*} value The value to set.\n * @returns {Object} Returns the map cache instance.\n */\nfunction mapCacheSet(key, value) {\n var data = getMapData(this, key),\n size = data.size;\n\n data.set(key, value);\n this.size += data.size == size ? 0 : 1;\n return this;\n}\n\nexport default mapCacheSet;\n", "import mapCacheClear from './_mapCacheClear.js';\nimport mapCacheDelete from './_mapCacheDelete.js';\nimport mapCacheGet from './_mapCacheGet.js';\nimport mapCacheHas from './_mapCacheHas.js';\nimport mapCacheSet from './_mapCacheSet.js';\n\n/**\n * Creates a map cache object to store key-value pairs.\n *\n * @private\n * @constructor\n * @param {Array} [entries] The key-value pairs to cache.\n */\nfunction MapCache(entries) {\n var index = -1,\n length = entries == null ? 0 : entries.length;\n\n this.clear();\n while (++index < length) {\n var entry = entries[index];\n this.set(entry[0], entry[1]);\n }\n}\n\n// Add methods to `MapCache`.\nMapCache.prototype.clear = mapCacheClear;\nMapCache.prototype['delete'] = mapCacheDelete;\nMapCache.prototype.get = mapCacheGet;\nMapCache.prototype.has = mapCacheHas;\nMapCache.prototype.set = mapCacheSet;\n\nexport default MapCache;\n", "/**\n * Appends the elements of `values` to `array`.\n *\n * @private\n * @param {Array} array The array to modify.\n * @param {Array} values The values to append.\n * @returns {Array} Returns `array`.\n */\nfunction arrayPush(array, values) {\n var index = -1,\n length = values.length,\n offset = array.length;\n\n while (++index < length) {\n array[offset + index] = values[index];\n }\n return array;\n}\n\nexport default arrayPush;\n", "import ListCache from './_ListCache.js';\n\n/**\n * Removes all key-value entries from the stack.\n *\n * @private\n * @name clear\n * @memberOf Stack\n */\nfunction stackClear() {\n this.__data__ = new ListCache;\n this.size = 0;\n}\n\nexport default stackClear;\n", "/**\n * Removes `key` and its value from the stack.\n *\n * @private\n * @name delete\n * @memberOf Stack\n * @param {string} key The key of the value to remove.\n * @returns {boolean} Returns `true` if the entry was removed, else `false`.\n */\nfunction stackDelete(key) {\n var data = this.__data__,\n result = data['delete'](key);\n\n this.size = data.size;\n return result;\n}\n\nexport default stackDelete;\n", "/**\n * Gets the stack value for `key`.\n *\n * @private\n * @name get\n * @memberOf Stack\n * @param {string} key The key of the value to get.\n * @returns {*} Returns the entry value.\n */\nfunction stackGet(key) {\n return this.__data__.get(key);\n}\n\nexport default stackGet;\n", "/**\n * Checks if a stack value for `key` exists.\n *\n * @private\n * @name has\n * @memberOf Stack\n * @param {string} key The key of the entry to check.\n * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.\n */\nfunction stackHas(key) {\n return this.__data__.has(key);\n}\n\nexport default stackHas;\n", "import ListCache from './_ListCache.js';\nimport Map from './_Map.js';\nimport MapCache from './_MapCache.js';\n\n/** Used as the size to enable large array optimizations. */\nvar LARGE_ARRAY_SIZE = 200;\n\n/**\n * Sets the stack `key` to `value`.\n *\n * @private\n * @name set\n * @memberOf Stack\n * @param {string} key The key of the value to set.\n * @param {*} value The value to set.\n * @returns {Object} Returns the stack cache instance.\n */\nfunction stackSet(key, value) {\n var data = this.__data__;\n if (data instanceof ListCache) {\n var pairs = data.__data__;\n if (!Map || (pairs.length < LARGE_ARRAY_SIZE - 1)) {\n pairs.push([key, value]);\n this.size = ++data.size;\n return this;\n }\n data = this.__data__ = new MapCache(pairs);\n }\n data.set(key, value);\n this.size = data.size;\n return this;\n}\n\nexport default stackSet;\n", "import ListCache from './_ListCache.js';\nimport stackClear from './_stackClear.js';\nimport stackDelete from './_stackDelete.js';\nimport stackGet from './_stackGet.js';\nimport stackHas from './_stackHas.js';\nimport stackSet from './_stackSet.js';\n\n/**\n * Creates a stack cache object to store key-value pairs.\n *\n * @private\n * @constructor\n * @param {Array} [entries] The key-value pairs to cache.\n */\nfunction Stack(entries) {\n var data = this.__data__ = new ListCache(entries);\n this.size = data.size;\n}\n\n// Add methods to `Stack`.\nStack.prototype.clear = stackClear;\nStack.prototype['delete'] = stackDelete;\nStack.prototype.get = stackGet;\nStack.prototype.has = stackHas;\nStack.prototype.set = stackSet;\n\nexport default Stack;\n", "/**\n * A specialized version of `_.filter` for arrays without support for\n * iteratee shorthands.\n *\n * @private\n * @param {Array} [array] The array to iterate over.\n * @param {Function} predicate The function invoked per iteration.\n * @returns {Array} Returns the new filtered array.\n */\nfunction arrayFilter(array, predicate) {\n var index = -1,\n length = array == null ? 0 : array.length,\n resIndex = 0,\n result = [];\n\n while (++index < length) {\n var value = array[index];\n if (predicate(value, index, array)) {\n result[resIndex++] = value;\n }\n }\n return result;\n}\n\nexport default arrayFilter;\n", "/**\n * This method returns a new empty array.\n *\n * @static\n * @memberOf _\n * @since 4.13.0\n * @category Util\n * @returns {Array} Returns the new empty array.\n * @example\n *\n * var arrays = _.times(2, _.stubArray);\n *\n * console.log(arrays);\n * // => [[], []]\n *\n * console.log(arrays[0] === arrays[1]);\n * // => false\n */\nfunction stubArray() {\n return [];\n}\n\nexport default stubArray;\n", "import arrayFilter from './_arrayFilter.js';\nimport stubArray from './stubArray.js';\n\n/** Used for built-in method references. */\nvar objectProto = Object.prototype;\n\n/** Built-in value references. */\nvar propertyIsEnumerable = objectProto.propertyIsEnumerable;\n\n/* Built-in method references for those with the same name as other `lodash` methods. */\nvar nativeGetSymbols = Object.getOwnPropertySymbols;\n\n/**\n * Creates an array of the own enumerable symbols of `object`.\n *\n * @private\n * @param {Object} object The object to query.\n * @returns {Array} Returns the array of symbols.\n */\nvar getSymbols = !nativeGetSymbols ? stubArray : function(object) {\n if (object == null) {\n return [];\n }\n object = Object(object);\n return arrayFilter(nativeGetSymbols(object), function(symbol) {\n return propertyIsEnumerable.call(object, symbol);\n });\n};\n\nexport default getSymbols;\n", "import arrayPush from './_arrayPush.js';\nimport isArray from './isArray.js';\n\n/**\n * The base implementation of `getAllKeys` and `getAllKeysIn` which uses\n * `keysFunc` and `symbolsFunc` to get the enumerable property names and\n * symbols of `object`.\n *\n * @private\n * @param {Object} object The object to query.\n * @param {Function} keysFunc The function to get the keys of `object`.\n * @param {Function} symbolsFunc The function to get the symbols of `object`.\n * @returns {Array} Returns the array of property names and symbols.\n */\nfunction baseGetAllKeys(object, keysFunc, symbolsFunc) {\n var result = keysFunc(object);\n return isArray(object) ? result : arrayPush(result, symbolsFunc(object));\n}\n\nexport default baseGetAllKeys;\n", "import baseGetAllKeys from './_baseGetAllKeys.js';\nimport getSymbols from './_getSymbols.js';\nimport keys from './keys.js';\n\n/**\n * Creates an array of own enumerable property names and symbols of `object`.\n *\n * @private\n * @param {Object} object The object to query.\n * @returns {Array} Returns the array of property names and symbols.\n */\nfunction getAllKeys(object) {\n return baseGetAllKeys(object, keys, getSymbols);\n}\n\nexport default getAllKeys;\n", "import getNative from './_getNative.js';\nimport root from './_root.js';\n\n/* Built-in method references that are verified to be native. */\nvar DataView = getNative(root, 'DataView');\n\nexport default DataView;\n", "import getNative from './_getNative.js';\nimport root from './_root.js';\n\n/* Built-in method references that are verified to be native. */\nvar Promise = getNative(root, 'Promise');\n\nexport default Promise;\n", "import getNative from './_getNative.js';\nimport root from './_root.js';\n\n/* Built-in method references that are verified to be native. */\nvar Set = getNative(root, 'Set');\n\nexport default Set;\n", "import DataView from './_DataView.js';\nimport Map from './_Map.js';\nimport Promise from './_Promise.js';\nimport Set from './_Set.js';\nimport WeakMap from './_WeakMap.js';\nimport baseGetTag from './_baseGetTag.js';\nimport toSource from './_toSource.js';\n\n/** `Object#toString` result references. */\nvar mapTag = '[object Map]',\n objectTag = '[object Object]',\n promiseTag = '[object Promise]',\n setTag = '[object Set]',\n weakMapTag = '[object WeakMap]';\n\nvar dataViewTag = '[object DataView]';\n\n/** Used to detect maps, sets, and weakmaps. */\nvar dataViewCtorString = toSource(DataView),\n mapCtorString = toSource(Map),\n promiseCtorString = toSource(Promise),\n setCtorString = toSource(Set),\n weakMapCtorString = toSource(WeakMap);\n\n/**\n * Gets the `toStringTag` of `value`.\n *\n * @private\n * @param {*} value The value to query.\n * @returns {string} Returns the `toStringTag`.\n */\nvar getTag = baseGetTag;\n\n// Fallback for data views, maps, sets, and weak maps in IE 11 and promises in Node.js < 6.\nif ((DataView && getTag(new DataView(new ArrayBuffer(1))) != dataViewTag) ||\n (Map && getTag(new Map) != mapTag) ||\n (Promise && getTag(Promise.resolve()) != promiseTag) ||\n (Set && getTag(new Set) != setTag) ||\n (WeakMap && getTag(new WeakMap) != weakMapTag)) {\n getTag = function(value) {\n var result = baseGetTag(value),\n Ctor = result == objectTag ? value.constructor : undefined,\n ctorString = Ctor ? toSource(Ctor) : '';\n\n if (ctorString) {\n switch (ctorString) {\n case dataViewCtorString: return dataViewTag;\n case mapCtorString: return mapTag;\n case promiseCtorString: return promiseTag;\n case setCtorString: return setTag;\n case weakMapCtorString: return weakMapTag;\n }\n }\n return result;\n };\n}\n\nexport default getTag;\n", "import root from './_root.js';\n\n/** Built-in value references. */\nvar Uint8Array = root.Uint8Array;\n\nexport default Uint8Array;\n", "/** Used to stand-in for `undefined` hash values. */\nvar HASH_UNDEFINED = '__lodash_hash_undefined__';\n\n/**\n * Adds `value` to the array cache.\n *\n * @private\n * @name add\n * @memberOf SetCache\n * @alias push\n * @param {*} value The value to cache.\n * @returns {Object} Returns the cache instance.\n */\nfunction setCacheAdd(value) {\n this.__data__.set(value, HASH_UNDEFINED);\n return this;\n}\n\nexport default setCacheAdd;\n", "/**\n * Checks if `value` is in the array cache.\n *\n * @private\n * @name has\n * @memberOf SetCache\n * @param {*} value The value to search for.\n * @returns {number} Returns `true` if `value` is found, else `false`.\n */\nfunction setCacheHas(value) {\n return this.__data__.has(value);\n}\n\nexport default setCacheHas;\n", "import MapCache from './_MapCache.js';\nimport setCacheAdd from './_setCacheAdd.js';\nimport setCacheHas from './_setCacheHas.js';\n\n/**\n *\n * Creates an array cache object to store unique values.\n *\n * @private\n * @constructor\n * @param {Array} [values] The values to cache.\n */\nfunction SetCache(values) {\n var index = -1,\n length = values == null ? 0 : values.length;\n\n this.__data__ = new MapCache;\n while (++index < length) {\n this.add(values[index]);\n }\n}\n\n// Add methods to `SetCache`.\nSetCache.prototype.add = SetCache.prototype.push = setCacheAdd;\nSetCache.prototype.has = setCacheHas;\n\nexport default SetCache;\n", "/**\n * A specialized version of `_.some` for arrays without support for iteratee\n * shorthands.\n *\n * @private\n * @param {Array} [array] The array to iterate over.\n * @param {Function} predicate The function invoked per iteration.\n * @returns {boolean} Returns `true` if any element passes the predicate check,\n * else `false`.\n */\nfunction arraySome(array, predicate) {\n var index = -1,\n length = array == null ? 0 : array.length;\n\n while (++index < length) {\n if (predicate(array[index], index, array)) {\n return true;\n }\n }\n return false;\n}\n\nexport default arraySome;\n", "/**\n * Checks if a `cache` value for `key` exists.\n *\n * @private\n * @param {Object} cache The cache to query.\n * @param {string} key The key of the entry to check.\n * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.\n */\nfunction cacheHas(cache, key) {\n return cache.has(key);\n}\n\nexport default cacheHas;\n", "import SetCache from './_SetCache.js';\nimport arraySome from './_arraySome.js';\nimport cacheHas from './_cacheHas.js';\n\n/** Used to compose bitmasks for value comparisons. */\nvar COMPARE_PARTIAL_FLAG = 1,\n COMPARE_UNORDERED_FLAG = 2;\n\n/**\n * A specialized version of `baseIsEqualDeep` for arrays with support for\n * partial deep comparisons.\n *\n * @private\n * @param {Array} array The array to compare.\n * @param {Array} other The other array to compare.\n * @param {number} bitmask The bitmask flags. See `baseIsEqual` for more details.\n * @param {Function} customizer The function to customize comparisons.\n * @param {Function} equalFunc The function to determine equivalents of values.\n * @param {Object} stack Tracks traversed `array` and `other` objects.\n * @returns {boolean} Returns `true` if the arrays are equivalent, else `false`.\n */\nfunction equalArrays(array, other, bitmask, customizer, equalFunc, stack) {\n var isPartial = bitmask & COMPARE_PARTIAL_FLAG,\n arrLength = array.length,\n othLength = other.length;\n\n if (arrLength != othLength && !(isPartial && othLength > arrLength)) {\n return false;\n }\n // Check that cyclic values are equal.\n var arrStacked = stack.get(array);\n var othStacked = stack.get(other);\n if (arrStacked && othStacked) {\n return arrStacked == other && othStacked == array;\n }\n var index = -1,\n result = true,\n seen = (bitmask & COMPARE_UNORDERED_FLAG) ? new SetCache : undefined;\n\n stack.set(array, other);\n stack.set(other, array);\n\n // Ignore non-index properties.\n while (++index < arrLength) {\n var arrValue = array[index],\n othValue = other[index];\n\n if (customizer) {\n var compared = isPartial\n ? customizer(othValue, arrValue, index, other, array, stack)\n : customizer(arrValue, othValue, index, array, other, stack);\n }\n if (compared !== undefined) {\n if (compared) {\n continue;\n }\n result = false;\n break;\n }\n // Recursively compare arrays (susceptible to call stack limits).\n if (seen) {\n if (!arraySome(other, function(othValue, othIndex) {\n if (!cacheHas(seen, othIndex) &&\n (arrValue === othValue || equalFunc(arrValue, othValue, bitmask, customizer, stack))) {\n return seen.push(othIndex);\n }\n })) {\n result = false;\n break;\n }\n } else if (!(\n arrValue === othValue ||\n equalFunc(arrValue, othValue, bitmask, customizer, stack)\n )) {\n result = false;\n break;\n }\n }\n stack['delete'](array);\n stack['delete'](other);\n return result;\n}\n\nexport default equalArrays;\n", "/**\n * Converts `map` to its key-value pairs.\n *\n * @private\n * @param {Object} map The map to convert.\n * @returns {Array} Returns the key-value pairs.\n */\nfunction mapToArray(map) {\n var index = -1,\n result = Array(map.size);\n\n map.forEach(function(value, key) {\n result[++index] = [key, value];\n });\n return result;\n}\n\nexport default mapToArray;\n", "/**\n * Converts `set` to an array of its values.\n *\n * @private\n * @param {Object} set The set to convert.\n * @returns {Array} Returns the values.\n */\nfunction setToArray(set) {\n var index = -1,\n result = Array(set.size);\n\n set.forEach(function(value) {\n result[++index] = value;\n });\n return result;\n}\n\nexport default setToArray;\n", "import Symbol from './_Symbol.js';\nimport Uint8Array from './_Uint8Array.js';\nimport eq from './eq.js';\nimport equalArrays from './_equalArrays.js';\nimport mapToArray from './_mapToArray.js';\nimport setToArray from './_setToArray.js';\n\n/** Used to compose bitmasks for value comparisons. */\nvar COMPARE_PARTIAL_FLAG = 1,\n COMPARE_UNORDERED_FLAG = 2;\n\n/** `Object#toString` result references. */\nvar boolTag = '[object Boolean]',\n dateTag = '[object Date]',\n errorTag = '[object Error]',\n mapTag = '[object Map]',\n numberTag = '[object Number]',\n regexpTag = '[object RegExp]',\n setTag = '[object Set]',\n stringTag = '[object String]',\n symbolTag = '[object Symbol]';\n\nvar arrayBufferTag = '[object ArrayBuffer]',\n dataViewTag = '[object DataView]';\n\n/** Used to convert symbols to primitives and strings. */\nvar symbolProto = Symbol ? Symbol.prototype : undefined,\n symbolValueOf = symbolProto ? symbolProto.valueOf : undefined;\n\n/**\n * A specialized version of `baseIsEqualDeep` for comparing objects of\n * the same `toStringTag`.\n *\n * **Note:** This function only supports comparing values with tags of\n * `Boolean`, `Date`, `Error`, `Number`, `RegExp`, or `String`.\n *\n * @private\n * @param {Object} object The object to compare.\n * @param {Object} other The other object to compare.\n * @param {string} tag The `toStringTag` of the objects to compare.\n * @param {number} bitmask The bitmask flags. See `baseIsEqual` for more details.\n * @param {Function} customizer The function to customize comparisons.\n * @param {Function} equalFunc The function to determine equivalents of values.\n * @param {Object} stack Tracks traversed `object` and `other` objects.\n * @returns {boolean} Returns `true` if the objects are equivalent, else `false`.\n */\nfunction equalByTag(object, other, tag, bitmask, customizer, equalFunc, stack) {\n switch (tag) {\n case dataViewTag:\n if ((object.byteLength != other.byteLength) ||\n (object.byteOffset != other.byteOffset)) {\n return false;\n }\n object = object.buffer;\n other = other.buffer;\n\n case arrayBufferTag:\n if ((object.byteLength != other.byteLength) ||\n !equalFunc(new Uint8Array(object), new Uint8Array(other))) {\n return false;\n }\n return true;\n\n case boolTag:\n case dateTag:\n case numberTag:\n // Coerce booleans to `1` or `0` and dates to milliseconds.\n // Invalid dates are coerced to `NaN`.\n return eq(+object, +other);\n\n case errorTag:\n return object.name == other.name && object.message == other.message;\n\n case regexpTag:\n case stringTag:\n // Coerce regexes to strings and treat strings, primitives and objects,\n // as equal. See http://www.ecma-international.org/ecma-262/7.0/#sec-regexp.prototype.tostring\n // for more details.\n return object == (other + '');\n\n case mapTag:\n var convert = mapToArray;\n\n case setTag:\n var isPartial = bitmask & COMPARE_PARTIAL_FLAG;\n convert || (convert = setToArray);\n\n if (object.size != other.size && !isPartial) {\n return false;\n }\n // Assume cyclic values are equal.\n var stacked = stack.get(object);\n if (stacked) {\n return stacked == other;\n }\n bitmask |= COMPARE_UNORDERED_FLAG;\n\n // Recursively compare objects (susceptible to call stack limits).\n stack.set(object, other);\n var result = equalArrays(convert(object), convert(other), bitmask, customizer, equalFunc, stack);\n stack['delete'](object);\n return result;\n\n case symbolTag:\n if (symbolValueOf) {\n return symbolValueOf.call(object) == symbolValueOf.call(other);\n }\n }\n return false;\n}\n\nexport default equalByTag;\n", "import getAllKeys from './_getAllKeys.js';\n\n/** Used to compose bitmasks for value comparisons. */\nvar COMPARE_PARTIAL_FLAG = 1;\n\n/** Used for built-in method references. */\nvar objectProto = Object.prototype;\n\n/** Used to check objects for own properties. */\nvar hasOwnProperty = objectProto.hasOwnProperty;\n\n/**\n * A specialized version of `baseIsEqualDeep` for objects with support for\n * partial deep comparisons.\n *\n * @private\n * @param {Object} object The object to compare.\n * @param {Object} other The other object to compare.\n * @param {number} bitmask The bitmask flags. See `baseIsEqual` for more details.\n * @param {Function} customizer The function to customize comparisons.\n * @param {Function} equalFunc The function to determine equivalents of values.\n * @param {Object} stack Tracks traversed `object` and `other` objects.\n * @returns {boolean} Returns `true` if the objects are equivalent, else `false`.\n */\nfunction equalObjects(object, other, bitmask, customizer, equalFunc, stack) {\n var isPartial = bitmask & COMPARE_PARTIAL_FLAG,\n objProps = getAllKeys(object),\n objLength = objProps.length,\n othProps = getAllKeys(other),\n othLength = othProps.length;\n\n if (objLength != othLength && !isPartial) {\n return false;\n }\n var index = objLength;\n while (index--) {\n var key = objProps[index];\n if (!(isPartial ? key in other : hasOwnProperty.call(other, key))) {\n return false;\n }\n }\n // Check that cyclic values are equal.\n var objStacked = stack.get(object);\n var othStacked = stack.get(other);\n if (objStacked && othStacked) {\n return objStacked == other && othStacked == object;\n }\n var result = true;\n stack.set(object, other);\n stack.set(other, object);\n\n var skipCtor = isPartial;\n while (++index < objLength) {\n key = objProps[index];\n var objValue = object[key],\n othValue = other[key];\n\n if (customizer) {\n var compared = isPartial\n ? customizer(othValue, objValue, key, other, object, stack)\n : customizer(objValue, othValue, key, object, other, stack);\n }\n // Recursively compare objects (susceptible to call stack limits).\n if (!(compared === undefined\n ? (objValue === othValue || equalFunc(objValue, othValue, bitmask, customizer, stack))\n : compared\n )) {\n result = false;\n break;\n }\n skipCtor || (skipCtor = key == 'constructor');\n }\n if (result && !skipCtor) {\n var objCtor = object.constructor,\n othCtor = other.constructor;\n\n // Non `Object` object instances with different constructors are not equal.\n if (objCtor != othCtor &&\n ('constructor' in object && 'constructor' in other) &&\n !(typeof objCtor == 'function' && objCtor instanceof objCtor &&\n typeof othCtor == 'function' && othCtor instanceof othCtor)) {\n result = false;\n }\n }\n stack['delete'](object);\n stack['delete'](other);\n return result;\n}\n\nexport default equalObjects;\n", "import Stack from './_Stack.js';\nimport equalArrays from './_equalArrays.js';\nimport equalByTag from './_equalByTag.js';\nimport equalObjects from './_equalObjects.js';\nimport getTag from './_getTag.js';\nimport isArray from './isArray.js';\nimport isBuffer from './isBuffer.js';\nimport isTypedArray from './isTypedArray.js';\n\n/** Used to compose bitmasks for value comparisons. */\nvar COMPARE_PARTIAL_FLAG = 1;\n\n/** `Object#toString` result references. */\nvar argsTag = '[object Arguments]',\n arrayTag = '[object Array]',\n objectTag = '[object Object]';\n\n/** Used for built-in method references. */\nvar objectProto = Object.prototype;\n\n/** Used to check objects for own properties. */\nvar hasOwnProperty = objectProto.hasOwnProperty;\n\n/**\n * A specialized version of `baseIsEqual` for arrays and objects which performs\n * deep comparisons and tracks traversed objects enabling objects with circular\n * references to be compared.\n *\n * @private\n * @param {Object} object The object to compare.\n * @param {Object} other The other object to compare.\n * @param {number} bitmask The bitmask flags. See `baseIsEqual` for more details.\n * @param {Function} customizer The function to customize comparisons.\n * @param {Function} equalFunc The function to determine equivalents of values.\n * @param {Object} [stack] Tracks traversed `object` and `other` objects.\n * @returns {boolean} Returns `true` if the objects are equivalent, else `false`.\n */\nfunction baseIsEqualDeep(object, other, bitmask, customizer, equalFunc, stack) {\n var objIsArr = isArray(object),\n othIsArr = isArray(other),\n objTag = objIsArr ? arrayTag : getTag(object),\n othTag = othIsArr ? arrayTag : getTag(other);\n\n objTag = objTag == argsTag ? objectTag : objTag;\n othTag = othTag == argsTag ? objectTag : othTag;\n\n var objIsObj = objTag == objectTag,\n othIsObj = othTag == objectTag,\n isSameTag = objTag == othTag;\n\n if (isSameTag && isBuffer(object)) {\n if (!isBuffer(other)) {\n return false;\n }\n objIsArr = true;\n objIsObj = false;\n }\n if (isSameTag && !objIsObj) {\n stack || (stack = new Stack);\n return (objIsArr || isTypedArray(object))\n ? equalArrays(object, other, bitmask, customizer, equalFunc, stack)\n : equalByTag(object, other, objTag, bitmask, customizer, equalFunc, stack);\n }\n if (!(bitmask & COMPARE_PARTIAL_FLAG)) {\n var objIsWrapped = objIsObj && hasOwnProperty.call(object, '__wrapped__'),\n othIsWrapped = othIsObj && hasOwnProperty.call(other, '__wrapped__');\n\n if (objIsWrapped || othIsWrapped) {\n var objUnwrapped = objIsWrapped ? object.value() : object,\n othUnwrapped = othIsWrapped ? other.value() : other;\n\n stack || (stack = new Stack);\n return equalFunc(objUnwrapped, othUnwrapped, bitmask, customizer, stack);\n }\n }\n if (!isSameTag) {\n return false;\n }\n stack || (stack = new Stack);\n return equalObjects(object, other, bitmask, customizer, equalFunc, stack);\n}\n\nexport default baseIsEqualDeep;\n", "import baseIsEqualDeep from './_baseIsEqualDeep.js';\nimport isObjectLike from './isObjectLike.js';\n\n/**\n * The base implementation of `_.isEqual` which supports partial comparisons\n * and tracks traversed objects.\n *\n * @private\n * @param {*} value The value to compare.\n * @param {*} other The other value to compare.\n * @param {boolean} bitmask The bitmask flags.\n * 1 - Unordered comparison\n * 2 - Partial comparison\n * @param {Function} [customizer] The function to customize comparisons.\n * @param {Object} [stack] Tracks traversed `value` and `other` objects.\n * @returns {boolean} Returns `true` if the values are equivalent, else `false`.\n */\nfunction baseIsEqual(value, other, bitmask, customizer, stack) {\n if (value === other) {\n return true;\n }\n if (value == null || other == null || (!isObjectLike(value) && !isObjectLike(other))) {\n return value !== value && other !== other;\n }\n return baseIsEqualDeep(value, other, bitmask, customizer, baseIsEqual, stack);\n}\n\nexport default baseIsEqual;\n", "import baseIsEqual from './_baseIsEqual.js';\n\n/**\n * Performs a deep comparison between two values to determine if they are\n * equivalent.\n *\n * **Note:** This method supports comparing arrays, array buffers, booleans,\n * date objects, error objects, maps, numbers, `Object` objects, regexes,\n * sets, strings, symbols, and typed arrays. `Object` objects are compared\n * by their own, not inherited, enumerable properties. Functions and DOM\n * nodes are compared by strict equality, i.e. `===`.\n *\n * @static\n * @memberOf _\n * @since 0.1.0\n * @category Lang\n * @param {*} value The value to compare.\n * @param {*} other The other value to compare.\n * @returns {boolean} Returns `true` if the values are equivalent, else `false`.\n * @example\n *\n * var object = { 'a': 1 };\n * var other = { 'a': 1 };\n *\n * _.isEqual(object, other);\n * // => true\n *\n * object === other;\n * // => false\n */\nfunction isEqual(value, other) {\n return baseIsEqual(value, other);\n}\n\nexport default isEqual;\n", "import { isEqual } from 'lodash-es';\n\nimport Metadata from './Metadata';\nimport {\n PluginStatusError,\n PluginStatusMetadata,\n PluginStatusProcessed,\n PluginStatusProcessing\n} from './types';\n\nclass ImageProcessingMetadata extends Metadata<PluginStatusMetadata> {\n get(blockId: number): PluginStatusMetadata {\n return super.get(blockId) ?? { status: 'IDLE' };\n }\n\n /**\n * Detect if the block has been duplicated with processed or processing\n * fill processing. In that case the processing state is still\n * valid, but blockId and fillId have changed.\n */\n isDuplicate(blockId: number): boolean {\n if (!this.engine.block.isValid(blockId)) return false;\n\n const metadata = this.get(blockId);\n\n if (\n metadata.status === 'IDLE' ||\n metadata.status === 'PENDING' ||\n metadata.status === 'ERROR'\n )\n return false;\n\n if (!this.engine.block.hasFill(blockId)) return false;\n const fillId = this.engine.block.getFill(blockId);\n\n // It cannot be a duplicate if the blockId or fillId are the same\n if (metadata.blockId === blockId || metadata.fillId === fillId)\n return false;\n\n return true;\n }\n\n /**\n * Fixes the metadata if the block has been duplicated, i.e. the blockId and\n * fillId will be updated to the current block/fill.\n *\n * Please note: Call this method only on duplicates (see isDuplicate).\n */\n fixDuplicate(blockId: number) {\n const fillId = this.engine.block.getFill(blockId);\n const metadata = this.get(blockId);\n if (\n metadata.status === 'IDLE' ||\n metadata.status === 'PENDING' ||\n metadata.status === 'ERROR'\n )\n return;\n this.set(blockId, {\n ...metadata,\n blockId,\n fillId\n });\n\n // If it is currently processing the best we can do is to just recover\n // the initial image data, since no processing will update this block and\n // it will be stuck in the processing state.\n if (metadata.status === 'PROCESSING') {\n this.recoverInitialImageData(blockId);\n this.clear(blockId);\n }\n }\n\n /**\n * Check if the image has a consisten metadata state. A inconsistent state is\n * caused by outside changes of the fill data.\n *\n * @returns true if the metadata is consistent, false otherwise\n */\n isConsistent(blockId: number): boolean {\n // In case the block was removed, we just abort and mark it\n // as reset by returning true\n if (!this.engine.block.isValid(blockId)) return false;\n const metadata = this.get(blockId);\n if (metadata.status === 'IDLE' || metadata.status === 'PENDING')\n return true;\n\n if (!this.engine.block.hasFill(blockId)) return false;\n const fillId = this.engine.block.getFill(blockId);\n if (fillId == null) return false;\n\n if (blockId !== metadata.blockId || fillId !== metadata.fillId)\n return false;\n\n const sourceSet = this.engine.block.getSourceSet(\n fillId,\n 'fill/image/sourceSet'\n );\n const imageFileURI = this.engine.block.getString(\n fillId,\n 'fill/image/imageFileURI'\n );\n\n if (\n sourceSet.length === 0 &&\n !imageFileURI &&\n metadata.status === 'PROCESSING'\n ) {\n // While we process it is OK to have no image file URI and no source set\n // (which we cleared to show the spinning loader)\n return true;\n }\n\n // Source sets have precedence over imageFileURI so if we have a source set,\n // we only need to check if it has changed to something else.\n if (sourceSet?.length > 0) {\n const initialSourceSet = metadata.initialSourceSet;\n // If we have already processed the image, we need to check if the source set\n // we need to check against both source sets, the removed and the initial\n if (metadata.status !== 'PROCESSED') {\n if (!isEqual(sourceSet, initialSourceSet)) {\n return false;\n }\n }\n } else {\n if (metadata.status !== 'PROCESSED') {\n if (imageFileURI !== metadata.initialImageFileURI) {\n return false;\n }\n }\n }\n return true;\n }\n\n /**\n * Recover the initial values to avoid the loading spinner and have the same\n * state as before the fill processing was started.\n */\n recoverInitialImageData(blockId: number) {\n const blockApi = this.engine.block;\n if (!blockApi.hasFill(blockId)) return; // Nothing to recover (no fill anymore)\n\n const metadata = this.get(blockId);\n\n if (metadata.status === 'PENDING' || metadata.status === 'IDLE') {\n return;\n }\n\n const initialSourceSet = metadata.initialSourceSet;\n const initialImageFileURI = metadata.initialImageFileURI;\n const initialPreviewFileURI = metadata.initialPreviewFileURI;\n\n const fillId = this.getValidFill(blockId, metadata);\n if (fillId == null) return;\n\n if (initialImageFileURI) {\n this.engine.block.setString(\n fillId,\n 'fill/image/imageFileURI',\n initialImageFileURI\n );\n }\n if (initialPreviewFileURI) {\n this.engine.block.setString(\n fillId,\n 'fill/image/previewFileURI',\n initialPreviewFileURI\n );\n }\n if (initialSourceSet.length > 0) {\n this.engine.block.setSourceSet(\n fillId,\n 'fill/image/sourceSet',\n initialSourceSet\n );\n }\n }\n\n /**\n * Returns the fill id of the block if it has a valid fill that was used for\n * fill processing. Returns undefined otherwise.\n */\n private getValidFill(\n blockId: number,\n metadata: PluginStatusProcessing | PluginStatusError | PluginStatusProcessed\n ): number | undefined {\n if (\n !this.engine.block.isValid(blockId) ||\n !this.engine.block.hasFill(blockId) ||\n blockId !== metadata.blockId\n ) {\n return undefined;\n }\n const fillId = this.engine.block.getFill(blockId);\n if (fillId !== metadata.fillId) {\n return undefined;\n }\n\n return fillId;\n }\n}\n\nexport default ImageProcessingMetadata;\n", "import CreativeEditorSDK from '@cesdk/cesdk-js';\nimport { FillProcessingMetadata } from '..';\nimport {\n PluginStatusProcessed,\n PluginStatusProcessing\n} from '../metadata/types';\n\ninterface FillProcessor<T> {\n processFill(metadataState: PluginStatusProcessing): Promise<T>;\n\n commitProcessing(\n data: T,\n metadataState: PluginStatusProcessed\n ): number | void;\n}\n\nasync function fillProcessing<T>(\n blockId: number,\n cesdk: CreativeEditorSDK,\n metadata: FillProcessingMetadata,\n processor: FillProcessor<T>\n) {\n const blockApi = cesdk.engine.block;\n if (!blockApi.hasFill(blockId))\n throw new Error('Block does not support fill');\n\n const fillId = blockApi.getFill(blockId);\n\n // Get the current image URI and source set as initial values.\n const initialSourceSet = blockApi.getSourceSet(\n fillId,\n 'fill/image/sourceSet'\n );\n const initialImageFileURI = blockApi.getString(\n fillId,\n 'fill/image/imageFileURI'\n );\n const initialPreviewFileURI = blockApi.getString(\n fillId,\n 'fill/image/previewFileURI'\n );\n try {\n cesdk.engine.block.setState(fillId, {\n type: 'Pending',\n progress: 0\n });\n\n const status: PluginStatusProcessing = {\n ...metadata.get(blockId),\n version: PLUGIN_VERSION,\n initialSourceSet,\n initialImageFileURI,\n initialPreviewFileURI,\n blockId,\n fillId,\n status: 'PROCESSING'\n };\n\n metadata.set(blockId, status);\n\n const processedData = await processor.processFill(status);\n // Check for externally changed state while we were applying the mask and\n // do not proceed if the state was reset.\n if (\n metadata.get(blockId).status !== 'PROCESSING' ||\n !metadata.isConsistent(blockId)\n )\n return;\n\n // Check for externally changed state while we were uploading and\n // do not proceed if the state was reset.\n if (\n metadata.get(blockId).status !== 'PROCESSING' ||\n !metadata.isConsistent(blockId)\n )\n return;\n\n if (processedData == null) return;\n\n const metadataStateProcessed: PluginStatusProcessed = {\n version: PLUGIN_VERSION,\n initialSourceSet,\n initialImageFileURI,\n initialPreviewFileURI,\n blockId,\n fillId,\n status: 'PROCESSED'\n };\n\n const blockIdOrVoid = processor.commitProcessing(\n processedData,\n metadataStateProcessed\n );\n\n // If a new block was created, we do not update the metadata.\n if (blockIdOrVoid == null || blockIdOrVoid === blockId) {\n metadata.set(blockId, metadataStateProcessed);\n }\n\n // Finally, create an undo step\n cesdk.engine.editor.addUndoStep();\n } catch (error) {\n if (cesdk.engine.block.isValid(blockId)) {\n metadata.set(blockId, {\n version: PLUGIN_VERSION,\n initialSourceSet,\n initialImageFileURI,\n initialPreviewFileURI,\n blockId,\n fillId,\n status: 'ERROR'\n });\n\n metadata.recoverInitialImageData(blockId);\n }\n\n if (\n error != null &&\n typeof error === 'object' &&\n 'message' in error &&\n typeof error.message === 'string'\n ) {\n const message =\n error.message === 'signal timed out'\n ? 'Processing canceled due to timeout'\n : error.message;\n cesdk.ui.showNotification({\n type: 'error',\n message\n });\n }\n\n // eslint-disable-next-line no-console\n console.log(error);\n } finally {\n if (cesdk.engine.block.isValid(fillId)) {\n cesdk.engine.block.setState(fillId, { type: 'Ready' });\n }\n }\n}\n\nexport default fillProcessing;\n", "export function getFeatureId(pluginId: string): string {\n return `${pluginId}.fillProcessing.feature`;\n}\n\nexport function getCanvasMenuComponentIds(pluginId: string): string[] {\n return [`${pluginId}.canvasMenu`, `${pluginId}.fillProcessing.canvasMenu`];\n}\n\nexport function getDockComponentIds(pluginId: string): string[] {\n return [`${pluginId}.dock`, `${pluginId}.fillProcessing.dock`];\n}\n\nexport function getInspectorBarComponentIds(pluginId: string): string[] {\n return [\n `${pluginId}.inspectorBar`,\n `${pluginId}.fillProcessing.inspectorBar`\n ];\n}\n\nexport function getNavigationBarComponentIds(pluginId: string): string[] {\n return [\n `${pluginId}.navigationBar`,\n `${pluginId}.fillProcessing.navigationBar`\n ];\n}\n\nexport function getCanvasBarComponentIds(pluginId: string): string[] {\n return [`${pluginId}.canvasBar`, `${pluginId}.fillProcessing.canvasBar`];\n}\n\nexport function getI18nCanvasMenuLabel(pluginId: string): string {\n return `plugin.${pluginId}.fillProcessing.canvasMenu.button.label`;\n}\n\nexport function getI18nDockLabel(pluginId: string): string {\n return `plugin.${pluginId}.fillProcessing.dock.button.label`;\n}\n\nexport function getI18nInspectorBarLabel(pluginId: string): string {\n return `plugin.${pluginId}.fillProcessing.inspectorBar.button.label`;\n}\n\nexport function getI18nNavigationBarLabel(pluginId: string): string {\n return `plugin.${pluginId}.fillProcessing.navigationBar.button.label`;\n}\n\nexport function getI18nCanvasBarLabel(pluginId: string): string {\n return `plugin.${pluginId}.fillProcessing.canvasBar.button.label`;\n}\n", "import type CreativeEditorSDK from '@cesdk/cesdk-js';\n\nimport { FillProcessingMetadata } from '..';\nimport { getFeatureId } from './constants';\n\nexport default function handleFillProcessing(\n cesdk: CreativeEditorSDK,\n {\n pluginId,\n process\n }: {\n pluginId: string;\n icon?: string;\n process: (blockId: number, metadata: FillProcessingMetadata) => void;\n }\n): {\n featureId: string;\n} {\n const featureId = getFeatureId(pluginId);\n\n const metadata = new FillProcessingMetadata(cesdk.engine, pluginId);\n\n enableFeatures(cesdk, metadata, featureId);\n\n cesdk.engine.event.subscribe([], async (events) => {\n events.forEach((e) => {\n const id = e.block;\n if (!cesdk.engine.block.isValid(id) || !metadata.hasData(id)) {\n return;\n }\n\n if (e.type === 'Created') {\n if (metadata.isDuplicate(id)) {\n metadata.fixDuplicate(id);\n }\n } else if (e.type === 'Updated') {\n switch (metadata.get(id).status) {\n case 'PENDING': {\n if (\n cesdk.feature.isEnabled(featureId, {\n engine: cesdk.engine\n }) &&\n cesdk.engine.block.isAllowedByScope(id, 'fill/change') &&\n cesdk.engine.block.getState(id).type !== 'Pending'\n ) {\n process(id, metadata);\n }\n break;\n }\n\n case 'PROCESSING':\n case 'PROCESSED': {\n if (!metadata.isConsistent(id)) {\n metadata.clear(id);\n }\n break;\n }\n\n default: {\n // We do not care about other states\n }\n }\n }\n });\n });\n\n return { featureId };\n}\n\n/**\n * Defines the feature that determines in which context (on which block)\n * fill processing is allowed/enabled.\n */\nfunction enableFeatures(\n cesdk: CreativeEditorSDK,\n metadata: FillProcessingMetadata,\n featureId: string\n) {\n cesdk.feature.enable(featureId, ({ engine }) => {\n const selectedIds = engine.block.findAllSelected();\n if (selectedIds.length !== 1) {\n return false;\n }\n const [selectedId] = selectedIds;\n\n if (!cesdk.engine.block.isVisible(selectedId)) return false;\n\n if (cesdk.engine.block.hasFill(selectedId)) {\n const kind = cesdk.engine.block.getKind(selectedId);\n if (kind === 'sticker') return false;\n\n const fillId = cesdk.engine.block.getFill(selectedId);\n const fillType = cesdk.engine.block.getType(fillId);\n\n if (fillType !== '//ly.img.ubq/fill/image') {\n return false;\n }\n\n const fileUri = engine.block.getString(fillId, 'fill/image/imageFileURI');\n const sourceSet = engine.block.getSourceSet(\n fillId,\n 'fill/image/sourceSet'\n );\n\n if (sourceSet.length > 0 || fileUri !== '') return true;\n\n // If we are in a processing state we do not have a imageFileURI or\n // source set set (to show the loading spinner), but the feature is still\n // enabled.\n return metadata.get(selectedId).status === 'PROCESSING';\n }\n\n return false;\n });\n}\n", "import CreativeEditorSDK from '@cesdk/cesdk-js';\nimport { FillProcessingMetadata } from '..';\nimport {\n getCanvasBarComponentIds,\n getCanvasMenuComponentIds,\n getDockComponentIds,\n getFeatureId,\n getI18nCanvasBarLabel,\n getI18nCanvasMenuLabel,\n getI18nDockLabel,\n getI18nInspectorBarLabel,\n getI18nNavigationBarLabel,\n getInspectorBarComponentIds,\n getNavigationBarComponentIds\n} from './constants';\n\nexport type Location =\n | 'inspectorBar'\n | 'navigationBar'\n | 'canvasBarTop'\n | 'canvasBarBottom'\n | 'canvasMenu'\n | 'dock';\n\n/**\n * Registers the components that can be used to process the fill of\n * a block.\n */\nexport default function registerFillProcessingComponents(\n cesdk: CreativeEditorSDK,\n options: {\n pluginId: string;\n icon?: string;\n locations?: Location | Location[];\n }\n): {\n canvasMenuComponentId: string;\n dockComponentId: string;\n\n translationsKeys: {\n inspectorBarLabel: string;\n navigationBarLabel: string;\n canvasBarLabel: string;\n canvasMenuLabel: string;\n dockLabel: string;\n };\n} {\n const { pluginId, locations } = options;\n const metadata = new FillProcessingMetadata(cesdk.engine, pluginId);\n\n const canvasMenuLabel = getI18nCanvasMenuLabel(pluginId);\n const canvasMenuComponentIds = getCanvasMenuComponentIds(pluginId);\n const canvasMenuComponentId = canvasMenuComponentIds[0];\n\n const dockLabel = getI18nDockLabel(pluginId);\n const dockComponentIds = getDockComponentIds(pluginId);\n const dockComponentId = dockComponentIds[0];\n\n const inspectorBarLabel = getI18nInspectorBarLabel(pluginId);\n const inspectorBarComponentIds = getInspectorBarComponentIds(pluginId);\n const inspectorBarComponentId = inspectorBarComponentIds[0];\n\n const navigationBarLabel = getI18nNavigationBarLabel(pluginId);\n const navigationBarComponentIds = getNavigationBarComponentIds(pluginId);\n const navigationBarComponentId = navigationBarComponentIds[0];\n\n const canvasBarLabel = getI18nCanvasBarLabel(pluginId);\n const canvasBarComponentIds = getCanvasBarComponentIds(pluginId);\n const canvasBarComponentId = canvasBarComponentIds[0];\n\n const featureId = getFeatureId(pluginId);\n\n if (locations?.includes('inspectorBar')) {\n cesdk.ui.setInspectorBarOrder([\n inspectorBarComponentId,\n ...cesdk.ui.getInspectorBarOrder()\n ]);\n }\n\n if (locations?.includes('navigationBar')) {\n cesdk.ui.setNavigationBarOrder([\n navigationBarComponentId,\n ...cesdk.ui.getNavigationBarOrder()\n ]);\n }\n\n if (locations?.includes('canvasBarTop')) {\n cesdk.ui.setCanvasBarOrder(\n [canvasBarComponentId, ...cesdk.ui.getCanvasBarOrder('top')],\n 'top'\n );\n }\n\n if (locations?.includes('canvasBarBottom')) {\n cesdk.ui.setCanvasBarOrder(\n [canvasBarComponentId, ...cesdk.ui.getCanvasBarOrder('bottom')],\n 'bottom'\n );\n }\n\n if (locations?.includes('canvasMenu')) {\n cesdk.ui.setCanvasMenuOrder([\n canvasMenuComponentId,\n ...cesdk.ui.getCanvasMenuOrder()\n ]);\n }\n\n if (locations?.includes('dock')) {\n cesdk.ui.setDockOrder([...cesdk.ui.getDockOrder(), dockComponentId]);\n }\n\n cesdk.ui.registerComponent(\n dockComponentIds,\n ({ builder: { Button }, engine }) => {\n const [id] = engine.block.findAllSelected();\n\n let isDisabled = false;\n let isLoading = false;\n let loadingProgress: number | undefined;\n\n if (id == null) {\n isDisabled = true;\n }\n\n if (\n !isDisabled &&\n !cesdk.feature.isEnabled(featureId, {\n engine\n })\n ) {\n isDisabled = true;\n }\n\n if (\n !isDisabled &&\n !cesdk.engine.block.isAllowedByScope(id, 'fill/change')\n ) {\n isDisabled = true;\n }\n\n if (!isDisabled && engine.block.getState(id)?.type === 'Pending') {\n isDisabled = true;\n }\n\n if (!isDisabled) {\n const currentMetadata = metadata.get(id);\n\n isLoading = currentMetadata.status === 'PROCESSING';\n isDisabled =\n currentMetadata.status === 'PENDING' ||\n currentMetadata.status === 'PROCESSING';\n\n if (\n currentMetadata.status === 'PROCESSING' &&\n currentMetadata.progress\n ) {\n const { current, total } = currentMetadata.progress;\n loadingProgress = (current / total) * 100;\n }\n }\n\n const buttonId = `${dockComponentId}.button`;\n Button(buttonId, {\n label: dockLabel,\n icon: options.icon,\n isLoading,\n isDisabled,\n loadingProgress,\n onClick: () => {\n const currentMetadata = metadata.get(id);\n\n if (\n currentMetadata.status === 'IDLE' ||\n currentMetadata.status === 'ERROR' ||\n currentMetadata.status === 'PROCESSED'\n ) {\n metadata.set(id, {\n status: 'PENDING'\n });\n }\n }\n });\n }\n );\n\n const buttonComponents: {\n componentIds: string[];\n label: string;\n variant: 'plain' | 'regular';\n }[] = [\n {\n componentIds: inspectorBarComponentIds,\n variant: 'plain',\n label: inspectorBarLabel\n },\n {\n componentIds: navigationBarComponentIds,\n variant: 'regular',\n label: navigationBarLabel\n },\n {\n componentIds: canvasBarComponentIds,\n variant: 'regular',\n label: canvasBarLabel\n },\n {\n componentIds: canvasMenuComponentIds,\n variant: 'plain',\n label: canvasMenuLabel\n }\n ];\n\n buttonComponents.forEach(({ componentIds, label, variant }) => {\n const componentId = componentIds[0];\n cesdk.ui.registerComponent(\n componentIds,\n ({ builder: { Button }, engine }) => {\n if (\n !cesdk.feature.isEnabled(featureId, {\n engine\n })\n ) {\n return;\n }\n\n const [id] = engine.block.findAllSelected();\n\n if (!cesdk.engine.block.isAllowedByScope(id, 'fill/change')) return;\n\n const currentMetadata = metadata.get(id);\n\n const isLoading = currentMetadata.status === 'PROCESSING';\n const isDisabled =\n currentMetadata.status === 'PENDING' ||\n currentMetadata.status === 'PROCESSING' ||\n engine.block.getState(id)?.type === 'Pending';\n\n let loadingProgress: number | undefined;\n if (isLoading && currentMetadata.progress) {\n const { current, total } = currentMetadata.progress;\n loadingProgress = (current / total) * 100;\n }\n\n const buttonId = `${componentId}.button`;\n Button(buttonId, {\n icon: options.icon,\n label,\n variant,\n isLoading,\n isDisabled,\n loadingProgress,\n onClick: () => {\n if (\n currentMetadata.status === 'IDLE' ||\n currentMetadata.status === 'ERROR' ||\n currentMetadata.status === 'PROCESSED'\n ) {\n metadata.set(id, {\n status: 'PENDING'\n });\n }\n }\n });\n }\n );\n });\n\n return {\n canvasMenuComponentId,\n dockComponentId,\n translationsKeys: {\n inspectorBarLabel,\n navigationBarLabel,\n canvasBarLabel,\n canvasMenuLabel,\n dockLabel\n }\n };\n}\n", "import { type RGBAColor } from '@cesdk/cesdk-js';\n\nconst HEX_COLOR_PATTERN = new RegExp(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/, 'i');\nconst HEX_SINGLE_CHAR_COMPONENTS_PATTERN = new RegExp(/[A-Fa-f0-9]{1}/, 'g');\nconst HEX_DOUBLE_CHAR_COMPONENTS_PATTERN = new RegExp(/[A-Fa-f0-9]{2}/, 'g');\n\n/** @public */\nexport const rgbaToHex = (rgba: RGBAColor, includeAlpha = false): string => {\n const { r, g, b, a } = rgba;\n const rByte = Math.round(255 * r);\n const gByte = Math.round(255 * g);\n const bByte = Math.round(255 * b);\n const aByte = Math.round(255 * a);\n const byteToHex = (byte: number) => {\n return byte.toString(16).padStart(2, '0');\n };\n return `#${byteToHex(rByte)}${byteToHex(gByte)}${byteToHex(bByte)}${\n includeAlpha ? byteToHex(aByte) : ''\n }`;\n};\n\n/** @public */\nexport const hexToRgba = (hexString: string): RGBAColor => {\n const rgbaHexToColor = (\n r: string,\n g: string,\n b: string,\n a: string | undefined,\n max: number\n ) => {\n return {\n r: parseInt(r, 16) / max,\n g: parseInt(g, 16) / max,\n b: parseInt(b, 16) / max,\n a: a === undefined ? 1 : parseInt(a, 16) / max\n };\n };\n\n if (hexString.startsWith('#')) {\n if (hexString.length === 4 || hexString.length === 5) {\n const hexMatch = hexString.match(HEX_SINGLE_CHAR_COMPONENTS_PATTERN);\n if (hexMatch) {\n const [r, g, b, a] = hexMatch;\n return rgbaHexToColor(r, g, b, a, 15);\n }\n }\n if (hexString.length === 7 || hexString.length === 8) {\n const hexMatch = hexString.match(HEX_DOUBLE_CHAR_COMPONENTS_PATTERN);\n if (hexMatch) {\n const [r, g, b, a] = hexMatch;\n return rgbaHexToColor(r, g, b, a, 255);\n }\n }\n }\n\n throw new Error(\n 'Invalid hex string! Allowed RGB formats are \"#FFF\" and \"#FFFFFF\". Allowed RGBA formats are \"#FFFF\" and \"#FFFFFFFF'\n );\n};\n\n/** @public */\nexport const isValidHexColor = (hexString: string): boolean => {\n return HEX_COLOR_PATTERN.test(hexString);\n};\n", "import type CreativeEditorSDK from '@cesdk/cesdk-js';\nimport { CreativeEngine } from '@cesdk/cesdk-js';\n\n/**\n * Uploads a blob with the help of CE.SDK\n */\nexport async function uploadBlob(\n blob: Blob,\n initialUri: string,\n cesdk: CreativeEditorSDK\n) {\n const pathname = new URL(initialUri).pathname;\n const parts = pathname.split('/');\n const extension = mimeTypeToExtension(blob.type);\n const filename = parts[parts.length - 1]?.split('.')?.[0] ?? 'asset';\n const filenameWithExtension = `${filename}.${extension}`;\n\n const uploadedAssets = await cesdk.unstable_upload(\n new File([blob], filenameWithExtension, { type: blob.type }),\n () => {\n // TODO Delegate process to UI component\n }\n );\n\n const url = uploadedAssets.meta?.uri;\n if (url == null) {\n throw new Error('Could not upload processed fill');\n }\n return url;\n}\n\n/**\n * Returns the file extension for a given mime type.\n */\nexport function mimeTypeToExtension(mimeType: string): string {\n const extensions: Record<string, string> = {\n 'image/png': 'png',\n 'image/jpeg': 'jpg',\n 'image/webp': 'webp',\n 'image/gif': 'gif',\n 'image/svg+xml': 'svg'\n };\n return extensions[mimeType] ?? 'png';\n}\n\nexport async function fetchImageBlob(uri: string): Promise<Blob> {\n return fetch(uri).then((response) => response.blob());\n}\n\n/**\n * Converts a buffer URI to a object url.\n */\nexport async function bufferURIToObjectURL(\n uri: string,\n engine: CreativeEngine\n): Promise<string> {\n if (uri.startsWith('buffer:')) {\n const mimeType = await engine.editor.getMimeType(uri);\n const length = engine.editor.getBufferLength(uri);\n const data = engine.editor.getBufferData(uri, 0, length);\n const buffer = new Uint8Array(data);\n const blob = new Blob([buffer], { type: mimeType });\n return URL.createObjectURL(blob);\n } else {\n return uri;\n }\n}\n", "function uuid4() {\n /* eslint-disable no-bitwise */\n return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {\n const r = (Math.random() * 16) | 0;\n const v = c === 'x' ? r : (r & 0x3) | 0x8;\n return v.toString(16);\n /* eslint-enable no-bitwise */\n });\n}\n\nexport default uuid4;\n", "import { bufferURIToObjectURL } from './upload';\nimport { type CreativeEngine } from '@cesdk/cesdk-js';\n\n/**\n * Get the dimensions of an image from its URL\n *\n * @param url - The URL of the image\n * @returns A promise that resolves to an object containing the width and height of the image\n */\nexport async function getImageDimensionsFromURL(\n url: string,\n engine: CreativeEngine\n): Promise<{ width: number; height: number }> {\n const resolvedUrl = await bufferURIToObjectURL(url, engine);\n return new Promise((resolve, reject) => {\n const img = new Image();\n img.onload = () => {\n resolve({ width: img.width, height: img.height });\n };\n img.onerror = reject;\n img.src = resolvedUrl;\n });\n}\n\n/**\n * Get the URI of an image from a block ID, either the first source of a source set or\n * the image file URI. Will handle buffer URIs and convert them to object URLs.\n */\nexport async function getImageUri(\n blockId: number,\n engine: CreativeEngine,\n options?: { throwErrorIfSvg?: boolean }\n): Promise<string> {\n let uri;\n const fillBlock = engine.block.getFill(blockId);\n const sourceSet = engine.block.getSourceSet(\n fillBlock,\n 'fill/image/sourceSet'\n );\n const [source] = sourceSet;\n if (source == null) {\n uri = engine.block.getString(fillBlock, 'fill/image/imageFileURI');\n if (uri == null) throw new Error('No image source/uri found');\n } else {\n uri = source.uri;\n }\n\n // Check if the image is SVG (not supported)\n if (options?.throwErrorIfSvg) {\n const mimeType = await engine.editor.getMimeType(uri);\n if (mimeType === 'image/svg+xml') {\n throw new Error('SVG images are not supported');\n }\n }\n\n return bufferURIToObjectURL(uri, engine);\n}\n\n/**\n * Returns if the given fill block is an SVG image fill.\n */\nexport async function isSvgFill(\n fillBlock: number,\n engine: CreativeEngine\n): Promise<boolean> {\n if (engine.block.getType(fillBlock) !== '//ly.img.ubq/fill/image') {\n return false;\n }\n\n let uri;\n const sourceSet = engine.block.getSourceSet(\n fillBlock,\n 'fill/image/sourceSet'\n );\n const [source] = sourceSet;\n if (source == null) {\n uri = engine.block.getString(fillBlock, 'fill/image/imageFileURI');\n if (uri == null) return false;\n } else {\n uri = source.uri;\n }\n\n const mimeType = await engine.editor.getMimeType(uri);\n return mimeType === 'image/svg+xml';\n}\n", "/**\n * Checks if a value is defined (not undefined).\n *\n * Helpful to filter out undefined values from an array or collection\n * while keeping the type information intact.\n *\n * ```\n * array.filter(isDefined)\n * ```\n */\nfunction isDefined<T>(value: T | undefined): value is T {\n return value !== undefined;\n}\n\nexport default isDefined;\n", "/**\n * Converts a value to an array format.\n *\n * @template T The type of the array elements\n * @param value - The value to convert. Can be a single item, an array, null, or undefined\n * @returns An array containing the value(s). Returns empty array if value is null/undefined,\n * the original array if value is already an array, or a single-element array if value is a single item\n *\n * @example\n * ```typescript\n * toArray('hello') // ['hello']\n * toArray(['a', 'b']) // ['a', 'b']\n * toArray(null) // []\n * toArray(undefined) // []\n * toArray(42) // [42]\n * ```\n */\nfunction toArray<T>(value?: T | T[]): T[] {\n if (value == null) return [];\n return Array.isArray(value) ? value : [value];\n}\n\nexport default toArray;\n", "/**\n * Interface for objects that have translation capabilities\n */\ninterface TranslationAPI {\n translate(key: string | string[]): string;\n}\n\n/**\n * Check if CE.SDK supports the translate API (version 1.59.0 or higher)\n * @param cesdk - The CE.SDK instance to check\n * @returns True if the translate API is supported\n */\nexport function supportsTranslateAPI(cesdk: any): boolean {\n if (!cesdk?.version) return false;\n\n // Use localeCompare for semantic version comparison\n // Returns >= 0 when cesdk.version is 1.59.0 or higher\n const comparison = cesdk.version.localeCompare('1.59.0', undefined, {\n numeric: true,\n sensitivity: 'base'\n });\n\n return comparison >= 0 && typeof cesdk.i18n?.translate === 'function';\n}\n\n/**\n * Type guard to check if an object has the translate API\n * @param i18n - The object to check\n * @returns True if the object has a translate function\n */\nexport function hasTranslateAPI(i18n: any): i18n is TranslationAPI {\n return typeof i18n?.translate === 'function';\n}\n\n/**\n * Safely translate a key with CE.SDK version compatibility check.\n * Returns the translation if supported (CE.SDK >= 1.59.0), otherwise returns the fallback.\n *\n * @param cesdk - The CE.SDK instance\n * @param translationKey - The translation key to translate\n * @param fallback - The fallback value to use if translation is not supported or key is not found\n * @returns The translated string or fallback\n */\nexport function translateWithFallback(\n cesdk: any,\n translationKey: string | string[],\n fallback: string\n): string {\n if (!cesdk) {\n return fallback;\n }\n\n // Check if CE.SDK supports translation API (version 1.59.0+)\n if (supportsTranslateAPI(cesdk) && hasTranslateAPI(cesdk.i18n)) {\n return cesdk.i18n.translate(translationKey);\n }\n\n return fallback;\n}\n", "import formatsIconSprite from './icons/formats';\n\nconst Icons = {\n Formats: formatsIconSprite\n};\n\nexport { Icons };\n\nexport {\n CustomAssetSource,\n type CustomAssetSourceOptions,\n type SelectValue\n} from './assetSources/CustomAssetSource';\n\nexport { IndexedDBAssetSource } from './assetSources/IndexedDBAssetSource';\n\nexport { AggregatedAssetSource } from './assetSources/AggregatedAssetSource';\n\nexport { default as Metadata } from './metadata/Metadata';\n\nexport { default as FillProcessingMetadata } from './metadata/FillProcessingMetadata';\n\nexport { default as fillProcessing } from './processing/fillProcessing';\n\nexport { default as initializeFillProcessing } from './processing/initializeFillProcessing';\n\nexport { default as registerFillProcessingComponents } from './processing/registerFillProcessingComponents';\n\nexport { type Optional } from './types/Optional';\n\nexport {\n type Location,\n type UserInterfaceConfiguration\n} from './types/UserInterfaceConfiguration';\n\nexport { hexToRgba, isValidHexColor, rgbaToHex } from './utils/colors';\n\nexport {\n uploadBlob,\n fetchImageBlob,\n bufferURIToObjectURL,\n mimeTypeToExtension\n} from './utils/upload';\n\nexport { default as uuid } from './utils/uuid';\n\nexport {\n getImageDimensionsFromURL,\n getImageUri,\n isSvgFill\n} from './utils/images';\n\nexport { default as isDefined } from './utils/isDefined';\n\nexport { default as toArray } from './utils/toArray';\n\nexport {\n supportsTranslateAPI,\n hasTranslateAPI,\n translateWithFallback\n} from './translationHelpers';\n", "import CreativeEditorSDK, { AssetResult } from '@cesdk/cesdk-js';\nimport { RenderCustomProperty } from '../../core/provider';\nimport {\n CustomAssetSource,\n isDefined,\n translateWithFallback\n} from '@imgly/plugin-utils';\nimport { SelectValue } from '@imgly/plugin-utils/dist/assetSources/CustomAssetSource';\n\ntype StyleSelectionPayload = {\n onSelect: (asset: AssetResult) => Promise<void>;\n};\n\ntype Style = {\n id: 'none' | (string & {});\n label: string;\n prompt: string;\n thumbUri: string;\n};\n\n/**\n * Provides render function for a style transfer property that allows\n * to change a style (of an image) from a library.\n *\n * The style will be appended to the prompt property, so the model does\n * not need to support style transfer directly.\n *\n * By default this expects the property key to be `style`. This can be changed with the option\n * `propertyKey`.\n */\nfunction renderStyleTransferProperty(\n providerId: string,\n options: {\n cesdk?: CreativeEditorSDK;\n /**\n * Base URL used for the UI assets used in the plugin.\n */\n baseURL?: string;\n\n /**\n * What property key to use for the style property.\n */\n propertyKey?: string;\n\n /**\n * What property key to use for the prompt property.\n */\n propertyKeyForPrompt?: string;\n\n /**\n * Override the default styles\n */\n styles?: Style[] | ((defaultStyles: Style[]) => Style[]);\n\n /**\n * Overrides the default i18n translations for the prompt input.\n */\n i18n?: {\n prompt?: {\n inputLabel?: string;\n placeholder?: string;\n };\n };\n }\n): RenderCustomProperty {\n const { cesdk } = options;\n if (cesdk == null) return {};\n\n const propertyKey = options.propertyKey ?? 'style';\n const panelIdForStyleSelection = getStyleSelectionPanelId(providerId);\n\n cesdk.i18n.setTranslations({\n en: {\n [`panel.${panelIdForStyleSelection}`]: 'Select Style',\n [`${providerId}.${propertyKey}`]: 'Style',\n 'ly.img.ai.styleTransfer.none': 'None',\n 'ly.img.ai.styleTransfer.anime': 'Anime',\n 'ly.img.ai.styleTransfer.cyberpunk': 'Cyberpunk',\n 'ly.img.ai.styleTransfer.kodak400': 'Kodak 400',\n 'ly.img.ai.styleTransfer.watercolor': 'Watercolor',\n 'ly.img.ai.styleTransfer.darkFantasy': 'Dark Fantasy',\n 'ly.img.ai.styleTransfer.vaporwave': 'Vaporwave',\n 'ly.img.ai.styleTransfer.vectorFlat': 'Vector Flat',\n 'ly.img.ai.styleTransfer.3dAnimation': '3D Animation',\n 'ly.img.ai.styleTransfer.ukiyoe': 'Ukiyo\u2011e',\n 'ly.img.ai.styleTransfer.surreal': 'Surreal',\n 'ly.img.ai.styleTransfer.steampunk': 'Steampunk',\n 'ly.img.ai.styleTransfer.nightBokeh': 'Night Bokeh',\n 'ly.img.ai.styleTransfer.popArt': 'Pop Art'\n }\n });\n\n const defaultStyles = getDefaultStyles({\n baseURL:\n options.baseURL ??\n 'https://cdn.img.ly/assets/plugins/plugin-ai-image-generation-web/v1/gpt-image-1/',\n includeNone: true,\n cesdk\n });\n let styles = defaultStyles;\n if (options.styles != null) {\n if (Array.isArray(options.styles)) {\n styles = options.styles;\n } else if (typeof options.styles === 'function') {\n styles = options.styles(defaultStyles);\n }\n }\n const styleAssetSource = createStyleAssetSource({\n cesdk,\n providerId,\n styles\n });\n const styleAssetSourceId = styleAssetSource.id;\n addStyleAssetSource(styleAssetSource, { cesdk });\n\n createPanels({\n providerId,\n cesdk,\n panelId: panelIdForStyleSelection,\n entryId: styleAssetSourceId\n });\n\n const customProperties: RenderCustomProperty = {\n [options.propertyKeyForPrompt ?? 'prompt']: (context, property) => {\n const promptState = context.state<string>('prompt', '');\n context.builder.TextArea(`${property.id}`, {\n inputLabel:\n options.i18n?.prompt?.inputLabel ??\n options.propertyKeyForPrompt ??\n 'prompt',\n placeholder: options.i18n?.prompt?.placeholder,\n ...promptState\n });\n\n return () => {\n const [activeAssetId] = styleAssetSource.getActiveAssetIds();\n const asset = styleAssetSource.getAsset(activeAssetId);\n return {\n id: property.id,\n type: 'string',\n value:\n asset?.meta?.prompt == null\n ? promptState.value\n : `${promptState.value}; ${asset.meta.prompt}`\n };\n };\n },\n [propertyKey]: (context, property) => {\n const { builder, state } = context;\n\n if (styles.length > 0) {\n const styleState = state<{\n id: string;\n label: string;\n }>('style', styles[0]);\n\n builder.Button(`${property.id}`, {\n inputLabel: `${providerId}.${property.id}`,\n icon: '@imgly/Appearance',\n isDisabled: styles.length === 0,\n trailingIcon: '@imgly/ChevronRight',\n label: styleState.value.label,\n labelAlignment: 'left',\n onClick: () => {\n const payload: StyleSelectionPayload = {\n onSelect: async (asset) => {\n styleAssetSource.clearActiveAssets();\n styleAssetSource.setAssetActive(asset.id);\n styleState.setValue({\n id: asset.id,\n label: asset.label ?? asset.id\n });\n cesdk.ui.closePanel(panelIdForStyleSelection);\n }\n };\n\n cesdk.ui.openPanel(panelIdForStyleSelection, {\n payload\n });\n }\n });\n return () => {\n return {\n id: property.id,\n type: 'string',\n value: styleState.value.id\n };\n };\n }\n\n return () => {\n return {\n id: property.id,\n type: 'string',\n value: 'none'\n };\n };\n }\n };\n\n return customProperties;\n}\n\nfunction createPanels(options: {\n providerId: string;\n panelId: string;\n entryId: string;\n cesdk?: CreativeEditorSDK;\n}) {\n const { providerId, cesdk, panelId, entryId } = options;\n if (cesdk == null) return;\n\n cesdk.ui.registerPanel<{\n onSelect: (assetResult: AssetResult) => void;\n }>(panelId, ({ builder, payload }) => {\n if (payload?.onSelect == null) {\n builder.Section(`${providerId}.error`, {\n children: () => {\n builder.Text('error', {\n content:\n 'No onSelect function provided for the style selection panel.'\n });\n }\n });\n }\n builder.Library(`${providerId}.library.image`, {\n entries: [entryId],\n onSelect: async (asset) => {\n payload?.onSelect?.(asset);\n }\n });\n });\n}\n\nfunction getStyleSelectionPanelId(providerId: string) {\n return `ly.img.ai.${providerId}.styleSelection`;\n}\n\nconst STYLES: (Omit<Style, 'thumbUri' | 'label'> & { labelKey: string })[] = [\n {\n id: 'none',\n labelKey: 'ly.img.ai.styleTransfer.none',\n prompt: ''\n },\n {\n id: 'anime-celshaded',\n labelKey: 'ly.img.ai.styleTransfer.anime',\n prompt:\n 'anime cel\u2011shaded, bright pastel palette, expressive eyes, clean line art '\n },\n {\n id: 'cyberpunk-neon',\n labelKey: 'ly.img.ai.styleTransfer.cyberpunk',\n prompt:\n 'cyberpunk cityscape, glowing neon signage, reflective puddles, dark atmosphere'\n },\n {\n id: 'kodak-portra-400',\n labelKey: 'ly.img.ai.styleTransfer.kodak400',\n prompt:\n 'shot on Kodak Portra 400, soft grain, golden\u2011hour warmth, 35 mm photo'\n },\n {\n id: 'watercolor-storybook',\n labelKey: 'ly.img.ai.styleTransfer.watercolor',\n prompt: 'loose watercolor washes, gentle gradients, dreamy storybook feel'\n },\n {\n id: 'dark-fantasy-realism',\n labelKey: 'ly.img.ai.styleTransfer.darkFantasy',\n prompt:\n 'dark fantasy realm, moody chiaroscuro lighting, hyper\u2011real textures'\n },\n {\n id: 'vaporwave-retrofuturism',\n labelKey: 'ly.img.ai.styleTransfer.vaporwave',\n prompt:\n 'retro\u2011futuristic vaporwave, pastel sunset gradient, chrome text, VHS scanlines'\n },\n {\n id: 'minimal-vector-flat',\n labelKey: 'ly.img.ai.styleTransfer.vectorFlat',\n prompt:\n 'minimalist flat vector illustration, bold geometry, two\u2011tone palette'\n },\n {\n id: 'pixarstyle-3d-render',\n labelKey: 'ly.img.ai.styleTransfer.3dAnimation',\n prompt:\n 'Pixar\u2011style 3D render, oversized eyes, subtle subsurface scattering, cinematic lighting'\n },\n {\n id: 'ukiyoe-woodblock',\n labelKey: 'ly.img.ai.styleTransfer.ukiyoe',\n prompt:\n 'ukiyo\u2011e woodblock print, Edo\u2011period style, visible washi texture, limited color ink'\n },\n {\n id: 'surreal-dreamscape',\n labelKey: 'ly.img.ai.styleTransfer.surreal',\n prompt:\n 'surreal dreamscape, floating objects, impossible architecture, vivid clouds'\n },\n {\n id: 'steampunk-victorian',\n labelKey: 'ly.img.ai.styleTransfer.steampunk',\n prompt:\n 'Victorian steampunk world, ornate brass gears, leather attire, atmospheric fog'\n },\n {\n id: 'nightstreet-photo-bokeh',\n labelKey: 'ly.img.ai.styleTransfer.nightBokeh',\n prompt:\n 'night\u2011time street shot, large aperture bokeh lights, candid urban mood'\n },\n {\n id: 'comicbook-pop-art',\n labelKey: 'ly.img.ai.styleTransfer.popArt',\n prompt:\n 'classic comic\u2011book panel, halftone shading, exaggerated action lines, CMYK pop colors'\n }\n];\n\nfunction getDefaultStyles(options: {\n baseURL: string;\n includeNone?: boolean;\n cesdk?: CreativeEditorSDK;\n}): Style[] {\n return STYLES.map((style) => {\n if (style.id === 'none') {\n if (!options.includeNone) {\n return undefined;\n }\n return {\n id: style.id,\n label: translateWithFallback(\n options.cesdk,\n style.labelKey,\n style.labelKey\n ),\n prompt: style.prompt,\n thumbUri: `${options.baseURL}/thumbnails/None.svg`\n };\n }\n return {\n id: style.id,\n label: translateWithFallback(\n options.cesdk,\n style.labelKey,\n style.labelKey\n ),\n prompt: style.prompt,\n thumbUri: `${options.baseURL}/thumbnails/${style.id}.jpeg`\n };\n }).filter(isDefined);\n}\n\nconst createStyleAssetSource = (options: {\n cesdk: CreativeEditorSDK;\n providerId: string;\n styles: Style[];\n}) => {\n const styleValues: SelectValue[] = options.styles.map((style) => {\n return {\n ...style,\n meta: { prompt: style.prompt }\n };\n });\n\n const allSourceIds = options.cesdk.engine.asset.findAllSources();\n let assetSourceId = `${options.providerId}/styles`;\n while (allSourceIds.includes(assetSourceId)) {\n assetSourceId += `-${Math.random().toString(36).substring(2, 5)}`;\n }\n const styleAssetSource = new CustomAssetSource(assetSourceId, styleValues);\n\n const defaultStyle = options.styles[0];\n styleAssetSource.setAssetActive(defaultStyle.id);\n\n return styleAssetSource;\n};\n\nconst addStyleAssetSource = (\n styleAssetSource: CustomAssetSource,\n options: {\n cesdk: CreativeEditorSDK;\n }\n) => {\n options.cesdk.engine.asset.addSource(styleAssetSource);\n options.cesdk.ui.addAssetLibraryEntry({\n id: styleAssetSource.id,\n sourceIds: [styleAssetSource.id],\n gridItemHeight: 'square',\n gridBackgroundType: 'cover',\n cardLabel: ({ label }) => label,\n cardLabelPosition: () => 'below'\n });\n};\n\nexport default renderStyleTransferProperty;\n", "import type { CreativeEngine } from '@cesdk/cesdk-js';\nimport type CreativeEditorSDK from '@cesdk/cesdk-js';\nimport type { PropertyContext } from '../core/propertyConfiguration';\n\n/**\n * Build the base property context from available sources\n */\nexport function buildPropertyContext(\n engine: CreativeEngine,\n cesdk?: CreativeEditorSDK\n): PropertyContext {\n // Get locale from cesdk or default to 'en'\n const locale = cesdk?.i18n?.getLocale?.() || 'en';\n\n return {\n engine,\n cesdk,\n locale\n };\n}\n\n/**\n * Context cache for performance optimization\n */\nexport class PropertyContextCache {\n private cache: PropertyContext | null = null;\n\n /**\n * Get cached context or build new one\n */\n getContext(\n engine: CreativeEngine,\n cesdk?: CreativeEditorSDK\n ): PropertyContext {\n if (!this.cache) {\n this.cache = buildPropertyContext(engine, cesdk);\n }\n return this.cache;\n }\n\n /**\n * Clear the cache (e.g., when locale changes)\n */\n clear(): void {\n this.cache = null;\n }\n}\n", "import type {\n PropertyConfig,\n PropertyContext\n} from '../core/propertyConfiguration';\n\n/**\n * Resolve the default value for a property\n * @template T - The property value type\n * @template C - The context type\n */\nexport function resolvePropertyDefault<\n T,\n C extends PropertyContext = PropertyContext\n>(\n propertyId: string,\n propertyConfig: PropertyConfig<T, C> | undefined,\n context: C,\n schemaDefault?: T,\n fallback?: T\n): T | undefined {\n // 1. Check property configuration\n if (propertyConfig?.default !== undefined) {\n const defaultValue = propertyConfig.default;\n\n // 2a. Static value\n if (typeof defaultValue !== 'function') {\n return defaultValue;\n }\n\n // 2b. Dynamic value - call function with context\n return (defaultValue as (context: C) => T)(context);\n }\n\n // 3. Schema default\n if (schemaDefault !== undefined) {\n return schemaDefault;\n }\n\n // 4. Fallback\n return fallback;\n}\n\n/**\n * Batch resolve multiple property defaults\n */\nexport function resolvePropertyDefaults<\n I,\n C extends PropertyContext = PropertyContext\n>(\n properties: Array<{\n id: keyof I;\n config?: PropertyConfig<I[keyof I], C>;\n schemaDefault?: I[keyof I];\n fallback?: I[keyof I];\n }>,\n context: C\n): Partial<I> {\n const resolved: Partial<I> = {};\n\n for (const prop of properties) {\n const value = resolvePropertyDefault(\n prop.id as string,\n prop.config,\n context,\n prop.schemaDefault,\n prop.fallback\n );\n\n if (value !== undefined) {\n resolved[prop.id] = value;\n }\n }\n\n return resolved;\n}\n", "import { OutputKind } from '../core/provider';\nimport CreativeEditorSDK from '@cesdk/cesdk-js';\n\n/**\n * Integrates the asset sources into the default asset library entry for the\n * given kind.\n */\nfunction integrateIntoDefaultAssetLibraryEntry<K extends OutputKind>(\n kind: K,\n historAssetSourceIds: string[],\n cesdk: CreativeEditorSDK\n): string | undefined {\n const entryId = `ly.img.${kind}`;\n const entry = cesdk.ui.getAssetLibraryEntry(entryId);\n if (entry != null) {\n cesdk.ui.updateAssetLibraryEntry(entryId, {\n sourceIds: [...entry.sourceIds, ...historAssetSourceIds]\n });\n return entry.id;\n }\n}\n\nexport default integrateIntoDefaultAssetLibraryEntry;\n", "import {\n BuilderRenderFunctionContext,\n CreativeEngine,\n SceneMode,\n Scope\n} from '@cesdk/cesdk-js';\nimport { OutputKind, Output } from './provider';\nimport { Result } from '../generation/createGenerateFunction';\n\n/**\n * Base properties shared by all action definitions.\n */\ninterface BaseActionDefinition {\n /** Unique identifier for the action */\n id: string;\n /** Human-readable label for the action */\n label?: string;\n /** Detailed description of what the action does */\n description?: string;\n /** Optional metadata for additional information */\n meta?: Record<string, any>;\n /** The scene mode for which this action is only applicable */\n sceneMode?: SceneMode;\n}\n\n/**\n * Definition for a plugin action - a global action that can be invoked from apps, command palettes, etc.\n */\nexport interface PluginActionDefinition extends BaseActionDefinition {\n /** Action type discriminator */\n type: 'plugin';\n /** ID of the plugin that registered this action */\n pluginId: string;\n /** Function to execute the action */\n execute: () => void;\n}\n\n/**\n * Render context for quick actions with generation capability.\n */\nexport interface QuickActionRenderContext<Q = Record<string, any>> {\n /** Toggle between collapsed and expanded state */\n toggleExpand: () => void;\n /** Whether the quick action is currently expanded */\n isExpanded: boolean;\n /** Close the entire quick action popover */\n close: () => void;\n /** Generate output using the quick action input */\n generate: (\n input: Q,\n options?: { blockIds?: number[] }\n ) => Promise<Result<Output>>;\n /** The ID of the provider used for this quick action */\n providerId: string;\n}\n\n/**\n * Definition for a quick action - a context-sensitive action that operates on selected blocks.\n */\nexport interface QuickActionDefinition<Q extends Record<string, any>>\n extends BaseActionDefinition {\n /** Action type discriminator */\n type: 'quick';\n /** The kind of block this action operates on */\n kind: OutputKind;\n /**\n * Defines if the quick action is enabled or not by using the\n * feature api.\n */\n enable: boolean | ((context: { engine: CreativeEngine }) => boolean);\n /**\n * Define the necessary scopes for this quick action.\n */\n scopes?: Scope[];\n\n /**\n * Overrides the defaults for this quick action.\n */\n defaults?: {\n /** Should the generation be directly applied or does it need confirmation */\n confirmation?: boolean;\n /** Whether the block should be locked */\n lock?: boolean;\n };\n\n /** Render function for the quick action UI */\n render: (\n context: BuilderRenderFunctionContext<any> & QuickActionRenderContext<Q>\n ) => void;\n}\n\n/**\n * Union type of all supported action definitions.\n */\nexport type ActionDefinition =\n | PluginActionDefinition\n | QuickActionDefinition<Record<string, any>>;\n\n/**\n * Event types for ActionRegistry subscriptions.\n */\nexport type ActionRegistryEventType = 'registered' | 'unregistered';\n\n/**\n * Callback function for ActionRegistry subscriptions.\n */\nexport type ActionRegistrySubscriberCallback = (\n action: ActionDefinition,\n event: ActionRegistryEventType\n) => void;\n\n/**\n * Filters for querying and subscribing to specific types of actions.\n * Used by both getBy() and subscribeBy() methods.\n */\nexport interface ActionRegistryFilters {\n /** Filter by action type */\n type?: ActionDefinition['type'];\n /** Filter by plugin ID */\n pluginId?: string;\n /** Filter by action ID */\n id?: string;\n /** Filter by kind (only applicable for quick actions) */\n kind?: OutputKind;\n}\n\n/**\n * Global registry for managing plugin actions and quick actions.\n * Uses singleton pattern to ensure a single source of truth across the application.\n */\nexport class ActionRegistry {\n /** Map storing all registered actions by their ID */\n private actions: Map<string, ActionDefinition> = new Map();\n\n /** Map storing subscribers with their filters (null = subscribe to all) */\n private subscribers: Map<\n ActionRegistrySubscriberCallback,\n ActionRegistryFilters | null\n > = new Map();\n\n // eslint-disable-next-line @typescript-eslint/no-empty-function\n private constructor() {}\n\n /**\n * Gets the singleton instance of the ActionRegistry.\n * Uses global object storage to ensure singleton across different bundle contexts.\n * @returns The ActionRegistry instance\n */\n public static get(): ActionRegistry {\n const globalKey = '__imgly_action_registry__';\n const globalObj = (\n typeof window !== 'undefined' ? window : globalThis\n ) as any;\n\n if (!globalObj[globalKey]) {\n globalObj[globalKey] = new ActionRegistry();\n }\n return globalObj[globalKey];\n }\n\n /**\n * Registers an action in the registry.\n * @param action The action definition to register\n * @returns A disposer function that unregisters the action when called\n */\n public register(action: ActionDefinition): () => void {\n this.actions.set(action.id, action);\n this.notifySubscribers(action, 'registered');\n\n return () => {\n if (this.actions.get(action.id) === action) {\n this.actions.delete(action.id);\n this.notifySubscribers(action, 'unregistered');\n }\n };\n }\n\n /**\n * Gets all registered actions.\n * @returns Array of all action definitions\n */\n public getAll(): ActionDefinition[] {\n return Array.from(this.actions.values());\n }\n\n /**\n * Gets actions matching the specified filters with full type safety.\n * @param filters Object containing optional filters for type, pluginId, and id\n * @returns Array of matching actions, typed based on the type filter\n * @example\n * // Get all plugin actions\n * registry.getBy({ type: 'plugin' }) // Returns PluginActionDefinition[]\n *\n * // Get all actions from a specific plugin\n * registry.getBy({ pluginId: 'ai-image-generation' }) // Returns ActionDefinition[]\n *\n * // Get specific action by ID\n * registry.getBy({ id: 'generate-image' }) // Returns ActionDefinition[]\n *\n * // Combine filters\n * registry.getBy({ type: 'quick', pluginId: 'ai-text' }) // Returns QuickActionDefinition[]\n *\n * // Filter quick actions by kind\n * registry.getBy({ type: 'quick', kind: 'image' }) // Returns QuickActionDefinition[]\n */\n public getBy<\n T extends ActionDefinition['type'] | undefined = undefined\n >(filters: {\n /** Filter by action type */\n type?: T;\n /** Filter by plugin ID */\n pluginId?: string;\n /** Filter by action ID */\n id?: string;\n /** Filter by kind (only applicable for quick actions) */\n kind?: OutputKind;\n }): T extends ActionDefinition['type']\n ? Extract<ActionDefinition, { type: T }>[]\n : ActionDefinition[] {\n const results = this.getAll().filter((action) =>\n this.matchesFilters(action, filters)\n );\n\n return results as T extends ActionDefinition['type']\n ? Extract<ActionDefinition, { type: T }>[]\n : ActionDefinition[];\n }\n\n /**\n * Subscribes to all registry events (register/unregister).\n * @param callback Function to call when any action is registered or unregistered\n * @returns Unsubscribe function\n */\n public subscribe(callback: ActionRegistrySubscriberCallback): () => void {\n this.subscribers.set(callback, null);\n return () => {\n this.subscribers.delete(callback);\n };\n }\n\n /**\n * Subscribes to registry events for actions matching the specified filters.\n * @param filters Filters to match actions against\n * @param callback Function to call when matching actions are registered or unregistered\n * @returns Unsubscribe function\n * @example\n * // Subscribe to plugin actions only\n * registry.subscribeBy({ type: 'plugin' }, (action, event) => {\n * console.log(`Plugin action ${action.id} was ${event}`);\n * });\n *\n * // Subscribe to actions from specific plugin\n * registry.subscribeBy({ pluginId: 'ai-image' }, (action, event) => {\n * updateUIForPlugin(action, event);\n * });\n */\n public subscribeBy(\n filters: ActionRegistryFilters,\n callback: ActionRegistrySubscriberCallback\n ): () => void {\n this.subscribers.set(callback, filters);\n return () => {\n this.subscribers.delete(callback);\n };\n }\n\n /**\n * Notifies all relevant subscribers about an action event.\n * @param action The action that was registered or unregistered\n * @param event The type of event that occurred\n */\n private notifySubscribers(\n action: ActionDefinition,\n event: ActionRegistryEventType\n ): void {\n this.subscribers.forEach((filters, callback) => {\n // If no filters (null), notify for all actions\n if (filters === null) {\n callback(action, event);\n return;\n }\n\n // Check if action matches the filters\n if (this.matchesFilters(action, filters)) {\n callback(action, event);\n }\n });\n }\n\n /**\n * Checks if an action matches the given filters.\n * Used by both getBy() and subscribeBy() methods.\n * @param action The action to check\n * @param filters The filters to match against\n * @returns True if the action matches all filters\n */\n private matchesFilters(\n action: ActionDefinition,\n filters: ActionRegistryFilters\n ): boolean {\n if (filters.type && action.type !== filters.type) return false;\n if (\n filters.pluginId &&\n action.type === 'plugin' &&\n action.pluginId !== filters.pluginId\n )\n return false;\n if (filters.id && action.id !== filters.id) return false;\n if (filters.kind) {\n // Kind filter only applies to quick actions\n if (action.type !== 'quick') return false;\n if (action.kind !== filters.kind) return false;\n }\n return true;\n }\n}\n", "import { ProviderInitializationResult } from '../providers/initializeProvider';\nimport { OutputKind } from './provider';\n\n/**\n * Global registry for managing AI generation providers across all plugins.\n * Uses singleton pattern to ensure cross-plugin provider discovery.\n */\nexport class ProviderRegistry {\n /** Map storing all registered providers by their ID */\n private providers: Map<string, ProviderInitializationResult<any, any, any>> =\n new Map();\n\n // eslint-disable-next-line @typescript-eslint/no-empty-function\n private constructor() {}\n\n /**\n * Gets the singleton instance of the ProviderRegistry.\n * Uses global object storage to ensure singleton across different bundle contexts.\n * @returns The ProviderRegistry instance\n */\n public static get(): ProviderRegistry {\n const globalKey = '__imgly_provider_registry__';\n const globalObj = (\n typeof window !== 'undefined' ? window : globalThis\n ) as any;\n\n if (!globalObj[globalKey]) {\n globalObj[globalKey] = new ProviderRegistry();\n }\n return globalObj[globalKey];\n }\n\n /**\n * Registers a provider in the registry.\n * @param providerInitializationResult The provider to register\n * @returns A disposer function that unregisters the provider when called\n */\n public register(\n providerInitializationResult: ProviderInitializationResult<any, any, any>\n ): () => void {\n if (this.providers.has(providerInitializationResult.provider.id)) {\n // eslint-disable-next-line no-console\n console.warn(\n `Provider with ID \"${providerInitializationResult.provider.id}\" is already registered`\n );\n }\n\n this.providers.set(\n providerInitializationResult.provider.id,\n providerInitializationResult\n );\n\n return () => {\n if (\n this.providers.get(providerInitializationResult.provider.id) ===\n providerInitializationResult\n ) {\n this.providers.delete(providerInitializationResult.provider.id);\n }\n };\n }\n\n /**\n * Gets all registered providers.\n * @returns Array of all provider instances\n */\n public getAll(): ProviderInitializationResult<any, any, any>[] {\n return Array.from(this.providers.values());\n }\n\n /**\n * Gets a provider by its ID.\n * @param id The provider ID to look up\n * @returns The provider instance or undefined if not found\n */\n public getById(\n id: string\n ): ProviderInitializationResult<any, any, any> | undefined {\n return this.providers.get(id);\n }\n\n /**\n * Gets all providers of a specific kind.\n * @param kind The output kind to filter by\n * @returns Array of providers matching the specified kind\n */\n public getByKind<K extends OutputKind>(\n kind: K\n ): ProviderInitializationResult<K, any, any>[] {\n return this.getAll().filter(({ provider }) => provider.kind === kind);\n }\n}\n", "import { GenerationOptions, GenerationResult, Output } from '../core/provider';\n\n/**\n * Result of the generation with a dispose function to clean up\n */\nexport interface DisposableGenerationResult<O extends Output> {\n /**\n * The actual generation result\n */\n result: GenerationResult<O>;\n\n /**\n * Function to dispose/clean up resources created during generation\n * This should be called when the generation is cancelled or completely\n * finished including the confirmation of the generation if applicable.\n */\n dispose: () => Promise<void>;\n}\n\n/**\n * Define the type for middleware functions\n */\nexport type Middleware<I, O extends Output> = (\n input: I,\n options: GenerationOptions & {\n /**\n * Adds a disposer function to this generation which is called\n * when the generation is cancelled or completely finished\n * including the confirmation of the generation if applicable.\n */\n addDisposer: (dispose: () => Promise<void>) => void;\n },\n next: (input: I, options: GenerationOptions) => Promise<GenerationResult<O>>\n) => Promise<GenerationResult<O>>;\n\nexport function composeMiddlewares<I, O extends Output>(\n middlewares: (Middleware<I, O> | false | undefined | null)[]\n) {\n // Filter out false, undefined, and null middlewares\n const validMiddlewares = middlewares.filter(\n (middleware): middleware is Middleware<I, O> => !!middleware\n );\n // Start with the base handler\n return (\n baseHandler: (\n input: I,\n options: GenerationOptions\n ) => Promise<GenerationResult<O>>\n ) => {\n // We need to build a chain where each step is a function with the signature:\n // (input, options) => Promise<Result>\n\n // The composed function that will be returned\n return async (\n input: I,\n options: GenerationOptions\n ): Promise<DisposableGenerationResult<O>> => {\n // Store disposer functions that will be called when dispose() is called\n const disposers: Array<() => Promise<void>> = [];\n\n // Function to add a disposer\n const addDisposer = (dispose: () => Promise<void>) => {\n disposers.push(dispose);\n };\n\n // Define a function to process each middleware in sequence\n const runMiddleware = async (\n index: number,\n currentInput: I,\n currentOptions: GenerationOptions\n ): Promise<GenerationResult<O>> => {\n // If we've processed all middlewares, call the base handler\n if (index >= validMiddlewares.length) {\n return baseHandler(currentInput, currentOptions);\n }\n\n // Get the current middleware\n const currentMiddleware = validMiddlewares[index];\n\n // Create a next function for this middleware that calls the next middleware in line\n const next = async (\n nextInput: I,\n nextOptions: GenerationOptions\n ): Promise<GenerationResult<O>> => {\n return runMiddleware(index + 1, nextInput, nextOptions);\n };\n\n // Enhanced options with addDisposer\n const enhancedOptions = {\n ...currentOptions,\n addDisposer\n };\n\n // Call the current middleware with the input, enhanced options, and next function\n return currentMiddleware(currentInput, enhancedOptions, next);\n };\n\n // Create enhanced options with addDisposer for base handler as well\n const enhancedOptions = {\n ...options,\n addDisposer\n };\n\n // Run the middleware chain\n const result = await runMiddleware(0, input, enhancedOptions);\n\n // Create the dispose function that will call all collected disposers in reverse order\n const dispose = async (): Promise<void> => {\n // Execute disposers in reverse order (last added, first disposed)\n /* eslint-disable no-await-in-loop */\n for (let i = disposers.length - 1; i >= 0; i--) {\n try {\n await disposers[i]();\n } catch (error) {\n // eslint-disable-next-line no-console\n console.error('Error in disposer:', error);\n }\n }\n // Clear the disposers array after all are executed\n disposers.length = 0;\n };\n\n // Return both the result and the dispose function\n return {\n result,\n dispose\n };\n };\n };\n}\n", "/* eslint-disable no-console */\nimport { GenerationResult, Output } from '../core/provider';\nimport { Middleware } from './middleware';\n\nfunction loggingMiddleware<I, O extends Output>({\n enable = true\n}: {\n enable: boolean | undefined;\n}) {\n const middleware: Middleware<I, O> = async (input, options, next) => {\n if (!enable) return next(input, options);\n\n console.group('[GENERATION]');\n console.log(`Generating with input:`, JSON.stringify(input, null, 2));\n let result: GenerationResult<O> | undefined;\n const start = Date.now();\n try {\n result = await next(input, options);\n return result;\n } finally {\n if (result != null) {\n console.log(`Generation took ${Date.now() - start}ms`);\n console.log(`Generation result:`, JSON.stringify(result, null, 2));\n }\n console.groupEnd();\n }\n };\n\n return middleware;\n}\n\nexport default loggingMiddleware;\n", "import CreativeEditorSDK, { AssetResult } from '@cesdk/cesdk-js';\nimport { GenerationResult, Output } from '../core/provider';\n\nexport const AI_PANEL_ID_PREFIX = 'ly.img.ai';\n\nconst TEMP_ASSET_SOURCE_ID = 'ly.img.ai.temp';\n\n/**\n * Adding asset to the scene.\n *\n * NOTE: Will use a temporary asset source so that\n * our asset source middleware trigger. This is necessary since there is\n * a lot of extra logic in the video middlewares regarding trim, position etc.\n *\n * These will only trigger via an asset source, not by calling\n * `defaultApplyAsset` directly.\n */\nexport async function addAssetToScene(\n cesdk: CreativeEditorSDK,\n assetResult: AssetResult\n) {\n if (!cesdk.engine.asset.findAllSources().includes(TEMP_ASSET_SOURCE_ID)) {\n cesdk.engine.asset.addLocalSource(TEMP_ASSET_SOURCE_ID);\n }\n\n return cesdk.engine.asset.apply(TEMP_ASSET_SOURCE_ID, assetResult);\n}\n\n/**\n * Returns a consistent panel ID for a provider ID\n */\nexport function getPanelId(providerId: string): string {\n return `${AI_PANEL_ID_PREFIX}.${providerId}`;\n}\n\nexport default getPanelId;\n/**\n * Extracts a readable error message from an unknown error\n *\n * @param error The error caught in a try/catch block\n * @param fallbackMessage Optional fallback message if error is not an Error object\n * @returns A string representation of the error\n */\nexport function extractErrorMessage(\n error: unknown,\n fallbackMessage = 'We encountered an unknown error while generating the asset. Please try again.'\n): string {\n if (error === null) {\n return fallbackMessage;\n }\n\n if (error instanceof Error) {\n return error.message;\n }\n\n if (typeof error === 'object') {\n // Try to get message property if it exists\n const errorObj = error as Record<string, unknown>;\n if ('message' in errorObj && typeof errorObj.message === 'string') {\n return errorObj.message;\n }\n if ('cause' in errorObj && typeof errorObj.cause === 'string') {\n return errorObj.cause;\n }\n\n /*\n * Elevenlabs for instance uses the following structure for errors:\n * {\n * \"detail\": {\n * \"status\": \"error_code\",\n * \"message\": \"Explanation of the rate limit issue.\"\n * }\n * }\n */\n if (\n 'detail' in errorObj &&\n typeof errorObj.detail === 'object' &&\n errorObj.detail !== null &&\n 'message' in errorObj.detail &&\n typeof errorObj.detail.message === 'string'\n ) {\n return errorObj.detail.message;\n }\n\n /*\n * Used by e.g. Anthropic\n */\n if (\n 'error' in errorObj &&\n typeof errorObj.error === 'object' &&\n errorObj.error !== null &&\n 'message' in errorObj.error &&\n typeof errorObj.error.message === 'string'\n ) {\n return errorObj.error.message;\n }\n\n return fallbackMessage;\n }\n\n if (typeof error === 'string') {\n return error;\n }\n\n // For any other type, convert to string\n return String(error) || fallbackMessage;\n}\n\n/**\n * Generates a random UUID v4\n */\nexport function uuid4() {\n /* eslint-disable no-bitwise */\n return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {\n const r = (Math.random() * 16) | 0;\n const v = c === 'x' ? r : (r & 0x3) | 0x8;\n return v.toString(16);\n /* eslint-enable no-bitwise */\n });\n}\n\n/**\n * Gets the duration of a video from a URL\n * @param url - The URL of the video\n * @returns A promise that resolves to the duration of the video in seconds\n */\nexport function getDurationForVideo(url: string): Promise<number> {\n return new Promise((resolve, reject) => {\n try {\n const video = document.createElement('video');\n video.style.display = 'none';\n\n // Set up event handlers\n video.addEventListener('loadedmetadata', () => {\n if (video.duration === Infinity) {\n // Some videos might initially report Infinity\n video.currentTime = 1e101;\n // Wait for currentTime to update\n setTimeout(() => {\n video.currentTime = 0;\n resolve(video.duration);\n document.body.removeChild(video);\n }, 50);\n } else {\n resolve(video.duration);\n document.body.removeChild(video);\n }\n });\n\n video.addEventListener('error', () => {\n document.body.removeChild(video);\n reject(new Error(`Failed to load video from ${url}`));\n });\n\n // Set source and begin loading\n video.src = url;\n document.body.appendChild(video);\n } catch (error) {\n reject(error);\n }\n });\n}\n\n/**\n * Gets a thumbnail image from a video URL\n * @param url - The URL of the video\n * @param seekTime - Time in seconds to capture the thumbnail (default: 0)\n * @param format - Image format for the thumbnail (default: 'image/jpeg')\n * @param quality - Image quality between 0 and 1 (default: 0.8)\n * @returns A promise that resolves to the thumbnail data URL\n */\nexport function getThumbnailForVideo(\n url: string,\n seekTime = 0,\n format = 'image/jpeg',\n quality = 0.8\n): Promise<string> {\n return new Promise((resolve, reject) => {\n try {\n const video = document.createElement('video');\n // Set crossOrigin to anonymous to prevent tainted canvas issues\n video.crossOrigin = 'anonymous';\n video.style.display = 'none';\n\n // Set up event handlers\n video.addEventListener('loadedmetadata', () => {\n // Seek to the specified time\n video.currentTime = Math.min(seekTime, video.duration);\n\n video.addEventListener(\n 'seeked',\n () => {\n // Create a canvas to draw the video frame\n const canvas = document.createElement('canvas');\n canvas.width = video.videoWidth;\n canvas.height = video.videoHeight;\n\n // Draw the video frame to the canvas\n const ctx = canvas.getContext('2d');\n if (!ctx) {\n document.body.removeChild(video);\n reject(new Error('Failed to create canvas context'));\n return;\n }\n\n ctx.drawImage(video, 0, 0, canvas.width, canvas.height);\n\n try {\n // Convert canvas to data URL\n const dataURL = canvas.toDataURL(format, quality);\n // Clean up\n document.body.removeChild(video);\n resolve(dataURL);\n } catch (e) {\n document.body.removeChild(video);\n reject(\n new Error(\n `Failed to create thumbnail: ${\n e instanceof Error ? e.message : String(e)\n }`\n )\n );\n }\n },\n { once: true }\n );\n });\n\n video.addEventListener('error', () => {\n document.body.removeChild(video);\n reject(new Error(`Failed to load video from ${url}`));\n });\n\n // Set source and begin loading\n video.src = url;\n document.body.appendChild(video);\n } catch (error) {\n reject(error);\n }\n });\n}\n\n/**\n * Converts an ID string to a human-readable label\n * @param id - The ID string to convert\n * @returns A human-readable label derived from the ID\n *\n * Examples:\n * - snake_case_id \u2192 Snake Case Id\n * - kebab-case-id \u2192 Kebab Case Id\n * - camelCaseId \u2192 Camel Case Id\n * - PascalCaseId \u2192 Pascal Case Id\n */\nexport function getLabelFromId(id: string): string {\n if (!id) return '';\n\n // Handle snake_case, kebab-case, camelCase, and PascalCase\n return (\n id\n // Replace underscores and hyphens with spaces (for snake_case and kebab-case)\n .replace(/[_-]/g, ' ')\n // Add spaces before uppercase letters (for camelCase and PascalCase)\n .replace(/([A-Z])/g, ' $1')\n // Trim any extra spaces and ensure first letter is capitalized\n .trim()\n .split(' ')\n .filter((word) => word.length > 0) // Remove empty strings from multiple spaces\n .map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())\n .join(' ')\n );\n}\n\n/**\n * Type guard to check if a value is an AsyncGenerator rather than a Promise\n *\n * @param value - Value of type Promise<O> | AsyncGenerator<O, C>\n * @returns Boolean indicating if the value is an AsyncGenerator\n */\nexport function isAsyncGenerator<O extends Output, C>(\n value: GenerationResult<O, C>\n): value is AsyncGenerator<O, C> {\n return (\n typeof value === 'object' &&\n value !== null &&\n 'next' in value &&\n 'return' in value &&\n 'throw' in value &&\n typeof value.next === 'function' &&\n typeof value.return === 'function' &&\n typeof value.throw === 'function' &&\n Symbol.asyncIterator in value &&\n typeof value[Symbol.asyncIterator] === 'function'\n );\n}\n\nexport function isAbortError(error: unknown): error is Error {\n return error instanceof Error && error.name === 'AbortError';\n}\n\n/**\n * Adds an icon set to the CreativeEditorSDK UI only once. Marks\n * it as added in the global state to prevent multiple additions.\n */\nexport function addIconSetOnce(\n cesdk: CreativeEditorSDK,\n id: string,\n icons: string\n): void {\n const globalStateIconSetAddedId = `${id}.iconSetAdded`;\n if (!cesdk.ui.experimental.hasGlobalStateValue(globalStateIconSetAddedId)) {\n cesdk.ui.addIconSet(id, icons);\n cesdk.ui.experimental.setGlobalStateValue(globalStateIconSetAddedId, true);\n }\n}\n", "import { isAsyncGenerator } from '../utils/utils';\nimport { GenerationResult, Output } from '../core/provider';\nimport { Middleware } from './middleware';\n\n/**\n * Middleware to upload the output of a generation process.\n *\n * Sometimes it is not possible to use the result of a provider or\n * not even allowed to use it directly. This middleware allows you to upload\n * the result of a generation process to a server or a cloud storage.\n *\n * @param upload The function to upload the output. It should return a promise\n */\nfunction uploadMiddleware<I, O extends Output>(\n upload: (output: O) => Promise<O>\n) {\n const middleware: Middleware<I, O> = async (input, options, next) => {\n const result: GenerationResult<O> | undefined = await next(input, options);\n if (isAsyncGenerator(result)) {\n // No reupload needed, just return the async generator\n return result;\n }\n\n const uploaded = await upload(result);\n return uploaded;\n };\n\n return middleware;\n}\n\nexport default uploadMiddleware;\n", "/**\n * Merge quick actions configuration by combining provider defaults with user configuration overrides\n *\n * @param providerDefaults - The default quick actions from the provider\n * @param userConfig - The user's configuration overrides\n * @returns Merged quick actions configuration\n */\nexport function mergeQuickActionsConfig<T extends Record<string, any>>(\n providerDefaults: T,\n userConfig?: {\n [quickActionId: string]: any | false | null;\n }\n): T {\n // Always return a copy to avoid mutating the original\n const result: any = { ...providerDefaults };\n\n if (!userConfig) return result as T;\n\n for (const [actionId, config] of Object.entries(userConfig)) {\n if (config === false || config === null || config === undefined) {\n // Remove the quick action\n delete result[actionId];\n } else if (config === true) {\n // Keep provider's default if it exists, otherwise add true\n if (!(actionId in providerDefaults)) {\n result[actionId] = true;\n }\n // If it exists in defaults, we already have it from the spread above\n } else {\n // Override with user configuration\n result[actionId] = config;\n }\n }\n\n return result as T;\n}\n", "import { GenerationOptions, Output } from '../core/provider';\nimport { Middleware } from './middleware';\n\nexport interface RateLimitOptions<I> {\n /**\n * Maximum number of requests allowed in the time window\n */\n maxRequests: number;\n\n /**\n * Time window in milliseconds\n */\n timeWindowMs: number;\n\n /**\n * Optional key function or string to create different rate limits for different inputs\n * - If not provided, all requests share the same 'global' rate limit\n * - If a string is provided, that string is used as a static key\n * - If a function is provided, it generates a key based on input and options\n */\n keyFn?: string | ((input: I, options: GenerationOptions) => string);\n\n /**\n * Callback function that is called when rate limit is exceeded\n * Return value determines if the operation should be rejected (throw error) or allowed\n * If true is returned, the operation will proceed despite exceeding the rate limit\n * If false or undefined is returned, the operation will be rejected with a default error\n */\n onRateLimitExceeded?: (\n input: I,\n options: GenerationOptions,\n rateLimitInfo: {\n key: string;\n currentCount: number;\n maxRequests: number;\n timeWindowMs: number;\n remainingTimeMs: number;\n }\n ) => boolean | Promise<boolean> | void;\n\n /**\n * Optional database name for the IndexedDB store\n * If not provided, a default name will be used\n */\n dbName?: string;\n\n /**\n * Disable the rate limit middleware\n */\n disable?: boolean | (() => boolean);\n}\n\n// Store for tracking requests per key\ninterface RequestTracker {\n timestamps: number[];\n lastCleanup: number;\n}\n\n// In-memory fallback store for rate limits for environments without IndexedDB\n// Export for testing purposes\n// This is now a Map of middleware instances to their respective stores\n// The key can be either a symbol (for unique instances with prefix) or a string (for shared instances without prefix)\nexport const inMemoryStores: Map<\n symbol | string,\n Record<string, RequestTracker>\n> = new Map();\n\n/**\n * IndexedDB store for rate limiting\n */\nclass RateLimitStore {\n private db: IDBDatabase | null = null;\n\n private readonly dbName: string;\n\n private readonly storeName: string;\n\n private readonly dbVersion: number = 1;\n\n private isInitializing: boolean = false;\n\n private initPromise: Promise<void> | null = null;\n\n private readonly instanceId: symbol | string;\n\n constructor(\n instanceId: symbol | string,\n dbName?: string,\n storeName?: string\n ) {\n this.instanceId = instanceId;\n this.dbName = dbName ?? 'ly.img.ai.rateLimit';\n this.storeName = storeName ?? 'rateLimits';\n }\n\n /**\n * Initialize the database connection\n */\n async initialize(): Promise<void> {\n if (this.db) {\n return;\n }\n\n if (this.isInitializing) {\n return this.initPromise!;\n }\n\n this.isInitializing = true;\n this.initPromise = new Promise<void>((resolve, reject) => {\n try {\n const request = indexedDB.open(this.dbName, this.dbVersion);\n\n request.onerror = (event) => {\n this.isInitializing = false;\n\n // eslint-disable-next-line no-console\n console.error('Failed to open IndexedDB for rate limiting:', event);\n reject(new Error('Failed to open IndexedDB for rate limiting'));\n };\n\n request.onupgradeneeded = (event) => {\n const db = (event.target as IDBOpenDBRequest).result;\n if (!db.objectStoreNames.contains(this.storeName)) {\n db.createObjectStore(this.storeName, { keyPath: 'id' });\n }\n };\n\n request.onsuccess = (event) => {\n this.db = (event.target as IDBOpenDBRequest).result;\n this.isInitializing = false;\n resolve();\n };\n } catch (error) {\n this.isInitializing = false;\n // eslint-disable-next-line no-console\n console.error('Error initializing IndexedDB:', error);\n reject(error);\n }\n });\n\n return this.initPromise;\n }\n\n /**\n * Get a request tracker by key\n */\n async getTracker(key: string): Promise<RequestTracker | null> {\n try {\n await this.initialize();\n\n // Create a combined key that includes the instance ID to ensure isolation\n const instanceIdStr =\n typeof this.instanceId === 'symbol'\n ? this.instanceId.description || ''\n : this.instanceId;\n const combinedKey = `${instanceIdStr}_${key}`;\n\n return await new Promise((resolve, reject) => {\n const transaction = this.db!.transaction(this.storeName, 'readonly');\n const store = transaction.objectStore(this.storeName);\n const request = store.get(combinedKey);\n\n request.onsuccess = () => {\n if (request.result) {\n resolve(request.result.data);\n } else {\n resolve(null);\n }\n };\n\n request.onerror = () => {\n // eslint-disable-next-line no-console\n console.error(\n `Failed to get tracker for key ${combinedKey}:`,\n request.error\n );\n reject(request.error);\n };\n });\n } catch (error) {\n // eslint-disable-next-line no-console\n console.error('Error getting tracker from IndexedDB:', error);\n return Promise.reject(error);\n }\n }\n\n /**\n * Save a request tracker by key\n */\n async saveTracker(key: string, tracker: RequestTracker): Promise<void> {\n try {\n await this.initialize();\n\n // Create a combined key that includes the instance ID to ensure isolation\n const instanceIdStr =\n typeof this.instanceId === 'symbol'\n ? this.instanceId.description || ''\n : this.instanceId;\n const combinedKey = `${instanceIdStr}_${key}`;\n\n return await new Promise((resolve, reject) => {\n const transaction = this.db!.transaction(this.storeName, 'readwrite');\n const store = transaction.objectStore(this.storeName);\n store.put({ id: combinedKey, data: tracker });\n\n transaction.oncomplete = () => {\n resolve();\n };\n\n transaction.onerror = () => {\n // eslint-disable-next-line no-console\n console.error(\n `Failed to save tracker for key ${combinedKey}:`,\n transaction.error\n );\n reject(transaction.error);\n };\n });\n } catch (error) {\n // eslint-disable-next-line no-console\n console.error('Error saving tracker to IndexedDB:', error);\n return Promise.reject(error);\n }\n }\n\n /**\n * Check if IndexedDB is available\n */\n static isAvailable(): boolean {\n return typeof indexedDB !== 'undefined';\n }\n\n /**\n * Close the database connection\n */\n close(): void {\n if (this.db) {\n this.db.close();\n this.db = null;\n }\n }\n}\n\n/**\n * Middleware that implements rate limiting for AI generation requests\n * Uses IndexedDB for storage when available, with in-memory fallback\n * Each middleware instance has its own isolated set of rate limits\n */\nfunction rateLimitMiddleware<I, O extends Output>(\n middlewareOptions: RateLimitOptions<I>\n) {\n const {\n maxRequests,\n timeWindowMs,\n keyFn = () => 'global',\n onRateLimitExceeded,\n dbName\n } = middlewareOptions;\n\n // Create an identifier for this middleware instance based on its configuration\n // This ensures persistence across page reloads and sharing between identical configurations\n const configStr = `rate-limit-middleware-${maxRequests}-${timeWindowMs}`;\n\n // Use a string key for the configuration so identical configurations share the same limits\n const instanceId = configStr;\n\n // Create the store with the instance ID\n const useIndexedDB = RateLimitStore.isAvailable();\n const store = useIndexedDB ? new RateLimitStore(instanceId, dbName) : null;\n\n // Initialize this middleware's in-memory store\n if (!inMemoryStores.has(instanceId)) {\n inMemoryStores.set(instanceId, {});\n }\n // Get this middleware's in-memory store\n const inMemoryStore = inMemoryStores.get(instanceId)!;\n\n const middleware: Middleware<I, O> = async (input, options, next) => {\n if (\n typeof middlewareOptions.disable === 'function'\n ? middlewareOptions.disable()\n : middlewareOptions.disable\n ) {\n return next(input, options);\n }\n // Get rate limit key based on input using the provided keyFn\n // If keyFn is a string, use it directly; otherwise call the function\n const key = typeof keyFn === 'string' ? keyFn : keyFn(input, options);\n const now = Date.now();\n\n let tracker: RequestTracker;\n\n // Get tracker from IndexedDB or create a new one\n if (useIndexedDB && store) {\n try {\n const storedTracker = await store.getTracker(key);\n if (storedTracker) {\n tracker = storedTracker;\n } else {\n tracker = {\n timestamps: [],\n lastCleanup: now\n };\n }\n } catch (error) {\n // Fallback to in-memory store if IndexedDB fails\n // eslint-disable-next-line no-console\n console.error(\n 'IndexedDB access failed, using in-memory fallback:',\n error\n );\n if (!inMemoryStore[key]) {\n inMemoryStore[key] = {\n timestamps: [],\n lastCleanup: now\n };\n }\n tracker = inMemoryStore[key];\n }\n } else {\n // Use in-memory store if IndexedDB is not available\n if (!inMemoryStore[key]) {\n inMemoryStore[key] = {\n timestamps: [],\n lastCleanup: now\n };\n }\n tracker = inMemoryStore[key];\n }\n\n // Clean up old timestamps that are outside the time window\n if (now - tracker.lastCleanup > timeWindowMs) {\n tracker.timestamps = tracker.timestamps.filter(\n (timestamp) => now - timestamp < timeWindowMs\n );\n tracker.lastCleanup = now;\n }\n\n // Check if rate limit is exceeded\n if (tracker.timestamps.length >= maxRequests) {\n // Calculate the time until the oldest request expires\n const oldestTimestamp = Math.min(...tracker.timestamps);\n const remainingTimeMs = Math.max(\n 0,\n timeWindowMs - (now - oldestTimestamp)\n );\n\n // Call the rate limit exceeded callback if provided\n if (onRateLimitExceeded) {\n const rateLimitInfo = {\n key, // This is the baseKey from keyFn\n currentCount: tracker.timestamps.length,\n maxRequests,\n timeWindowMs,\n remainingTimeMs\n };\n\n // If callback returns true, allow the request to proceed\n const shouldProceed = await onRateLimitExceeded(\n input,\n options,\n rateLimitInfo\n );\n if (!shouldProceed) {\n // The callback should indicate the error, so we throw\n // an abort error instead of a generic error to indicate\n // the generation was aborted and we do not show any further\n // error notification.\n throw new DOMException(\n 'Operation aborted: Rate limit exceeded',\n 'AbortError'\n );\n }\n } else {\n // Default behavior: throw an error\n throw new Error('Rate limit exceeded. Please try again later.');\n }\n }\n\n // Add current timestamp to the tracker\n tracker.timestamps.push(now);\n\n // Save the updated tracker\n if (useIndexedDB && store) {\n try {\n await store.saveTracker(key, tracker);\n } catch (error) {\n // eslint-disable-next-line no-console\n console.error('Failed to save tracker to IndexedDB:', error);\n // In case of IndexedDB failure, update the in-memory store as fallback\n inMemoryStore[key] = tracker;\n }\n } else {\n inMemoryStore[key] = tracker;\n }\n\n // Continue with the next middleware\n return next(input, options);\n };\n\n return middleware;\n}\n\nexport default rateLimitMiddleware;\n", "import type CreativeEditorSDK from '@cesdk/cesdk-js';\n\n/**\n * Checks if the current AI plugin version matches the shared version across all AI plugins.\n * Issues a console warning if versions don't match.\n *\n * @param cesdk - The CreativeEditorSDK instance\n * @param pluginId - The ID of the current plugin\n * @param currentVersion - The version of the current plugin\n */\nexport function checkAiPluginVersion(\n cesdk: CreativeEditorSDK,\n pluginId: string,\n currentVersion: string\n): void {\n const AI_PLUGIN_VERSION_KEY = 'ai-plugin-version';\n const AI_PLUGIN_VERSION_WARNING_KEY = 'ai-plugin-version-warning-shown';\n\n try {\n const sharedVersion = cesdk.ui.experimental.getGlobalStateValue<string>(\n AI_PLUGIN_VERSION_KEY\n );\n\n if (!sharedVersion) {\n // First AI plugin sets the shared version\n cesdk.ui.experimental.setGlobalStateValue(\n AI_PLUGIN_VERSION_KEY,\n currentVersion\n );\n } else if (sharedVersion !== currentVersion) {\n // Version mismatch detected\n const warningShown = cesdk.ui.experimental.getGlobalStateValue<boolean>(\n AI_PLUGIN_VERSION_WARNING_KEY,\n false\n );\n\n if (!warningShown) {\n // eslint-disable-next-line no-console\n console.warn(\n `[IMG.LY AI Plugins] Version mismatch detected!\\n` +\n `Plugin \"${pluginId}\" is using version ${currentVersion}, but other AI plugins are using version ${sharedVersion}.\\n` +\n `This may cause compatibility issues. Please ensure all AI plugins (@imgly/plugin-ai-*) use the same version.\\n` +\n `Consider updating all AI plugins to the same version for optimal compatibility.`\n );\n\n // Set flag to prevent duplicate warnings\n cesdk.ui.experimental.setGlobalStateValue(\n AI_PLUGIN_VERSION_WARNING_KEY,\n true\n );\n }\n }\n } catch (error) {\n // Fail silently if global state access fails\n // eslint-disable-next-line no-console\n console.debug(\n '[IMG.LY AI Plugins] Could not check plugin version consistency:',\n error\n );\n }\n}\n", "import CreativeEditorSDK from '@cesdk/cesdk-js';\n\n/**\n * Registers a dock component for AI generation that opens\n * the AI generation panel and closes any other AI panels.\n */\nfunction registerDockComponent(options: {\n cesdk: CreativeEditorSDK;\n panelId: string;\n}) {\n const { cesdk, panelId } = options;\n if (!panelId.startsWith('ly.img.ai.')) {\n // eslint-disable-next-line no-console\n console.warn(\n `Dock components for AI generation should open a panel with an id starting with \"ly.img.ai.\" \u2013 \"${panelId}\" was provided.`\n );\n }\n\n const dockComponentId = `${panelId}.dock`;\n cesdk.ui.registerComponent(dockComponentId, ({ builder }) => {\n const isOpen = cesdk.ui.isPanelOpen(panelId);\n\n builder.Button(`${panelId}.dock.button`, {\n label: `${panelId}.dock.label`,\n isSelected: isOpen,\n icon: '@imgly/Sparkle',\n onClick: () => {\n cesdk.ui.findAllPanels().forEach((panel) => {\n if (panel.startsWith('ly.img.ai.')) {\n cesdk.ui.closePanel(panel);\n }\n if (!isOpen && panel === '//ly.img.panel/assetLibrary') {\n cesdk.ui.closePanel(panel);\n }\n });\n\n if (!isOpen) {\n cesdk.ui.openPanel(panelId);\n } else {\n cesdk.ui.closePanel(panelId);\n }\n }\n });\n });\n}\n\nexport default registerDockComponent;\n", "import type CreativeEngine from '@cesdk/engine';\n\n/**\n * Enable function for a single image fill block selected.\n */\nfunction enableImageFill() {\n return ({ engine }: { engine: CreativeEngine }) => {\n const blockIds = engine.block.findAllSelected();\n if (blockIds == null || blockIds.length !== 1) return false;\n\n const [blockId] = blockIds;\n\n if (!engine.block.supportsFill(blockId)) return false;\n\n if (engine.block.getKind(blockId) === 'sticker') return false;\n\n if (\n !['//ly.img.ubq/graphic', '//ly.img.ubq/page'].includes(\n engine.block.getType(blockId)\n )\n ) {\n return false;\n }\n\n const fillBlock = engine.block.getFill(blockId);\n return engine.block.getType(fillBlock) === '//ly.img.ubq/fill/image';\n };\n}\n\nexport default enableImageFill;\n", "import CreativeEditorSDK from '@cesdk/cesdk-js';\nimport Provider, {\n GenerationOptions,\n GetInput,\n Output,\n OutputKind\n} from '../core/provider';\nimport { extractErrorMessage } from '../utils/utils';\n\nfunction handleGenerationError<K extends OutputKind, I, O extends Output>(\n error: unknown,\n options: {\n cesdk: CreativeEditorSDK;\n provider: Provider<K, I, O>;\n getInput?: GetInput<I>;\n middlewareOptions?: GenerationOptions;\n }\n) {\n const { cesdk, provider, getInput, middlewareOptions } = options;\n\n // Check if default was prevented\n if (middlewareOptions?.defaultPrevented()) {\n return; // Skip all default behavior (notifications + console logging)\n }\n\n // eslint-disable-next-line no-console\n console.error('Generation failed:', error);\n const shown = showErrorNotification(\n cesdk,\n provider.output.notification,\n () => ({\n input: getInput?.().input,\n error\n })\n );\n if (!shown) {\n cesdk.ui.showNotification({\n type: 'error',\n message: extractErrorMessage(error)\n });\n }\n}\n\nfunction showErrorNotification<I, O extends Output>(\n cesdk: CreativeEditorSDK,\n notifications: Provider<any, I, O>['output']['notification'],\n createContext: () => { input?: I; error: unknown }\n): boolean {\n const errorNotification = notifications?.error;\n if (errorNotification == null) return false;\n\n const showOnSuccess =\n typeof errorNotification.show === 'function'\n ? errorNotification.show(createContext())\n : errorNotification.show;\n\n if (!showOnSuccess) return false;\n\n const message =\n typeof errorNotification.message === 'function'\n ? errorNotification.message(createContext())\n : errorNotification.message ?? 'common.ai-generation.failed';\n\n const action =\n errorNotification.action != null\n ? {\n label:\n typeof errorNotification.action.label === 'function'\n ? errorNotification.action.label(createContext())\n : errorNotification.action.label,\n onClick: () => {\n errorNotification?.action?.onClick(createContext());\n }\n }\n : undefined;\n\n cesdk.ui.showNotification({\n type: 'error',\n message,\n action\n });\n return true;\n}\n\nexport default handleGenerationError;\n", "const previewUri =\n 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzIzIiBoZWlnaHQ9IjMyMyIgdmlld0JveD0iMCAwIDMyMyAzMjMiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxyZWN0IHdpZHRoPSIzMjMiIGhlaWdodD0iMzIzI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IDE3Ny42MzUgMTU2LjQ1OCAxNzkuMTIgMTU4LjY4NEwxOTUuOTk5IDE4NC4wMDRaIiBmaWxsPSIjOEY4RjhGIi8+CjwvZz4KPC9zdmc+Cg==';\n\nexport default previewUri;\n", "import { type AssetResult } from '@cesdk/cesdk-js';\nimport {\n type OutputKind,\n GetBlockInputResult,\n InputByKind\n} from '../core/provider';\nimport previewUri from './previewUri';\n\nfunction getAssetResultForPlaceholder<K extends OutputKind>(\n id: string,\n kind: K,\n blockInput: GetBlockInputResult<K>\n): AssetResult {\n switch (kind) {\n case 'image': {\n return getImageAssetResultForPlaceholder(\n id,\n blockInput[kind] as InputByKind['image']\n );\n }\n case 'video': {\n return getVideoAssetResultForPlaceholder(\n id,\n blockInput[kind] as InputByKind['video']\n );\n }\n case 'sticker': {\n return getStickerAssetResultForPlaceholder(\n id,\n blockInput[kind] as InputByKind['sticker']\n );\n }\n\n default: {\n throw new Error(\n `Unsupported output kind for creating placeholder block: ${kind}`\n );\n }\n }\n}\n\nfunction getImageAssetResultForPlaceholder(\n id: string,\n input: InputByKind['image']\n): AssetResult {\n const width = input.width;\n const height = input.height;\n return {\n id,\n meta: {\n previewUri,\n fillType: '//ly.img.ubq/fill/image',\n kind: 'image',\n\n width,\n height\n }\n };\n}\n\nfunction getVideoAssetResultForPlaceholder(\n id: string,\n input: InputByKind['video']\n): AssetResult {\n const width = input.width;\n const height = input.height;\n return {\n id,\n label: input.label,\n meta: {\n previewUri,\n mimeType: 'video/mp4',\n kind: 'video',\n fillType: '//ly.img.ubq/fill/video',\n\n duration: input.duration.toString(),\n\n width,\n height\n }\n };\n}\n\nfunction getStickerAssetResultForPlaceholder(\n id: string,\n input: InputByKind['sticker']\n): AssetResult {\n const width = input.width;\n const height = input.height;\n return {\n id,\n meta: {\n previewUri,\n fillType: '//ly.img.ubq/fill/image',\n kind: 'sticker',\n\n width,\n height\n }\n };\n}\n\nexport default getAssetResultForPlaceholder;\n", "import { type AssetResult } from '@cesdk/cesdk-js';\nimport {\n type OutputKind,\n AudioOutput,\n GetBlockInputResult,\n ImageOutput,\n InputByKind,\n Output,\n StickerOutput,\n VideoOutput\n} from '../core/provider';\nimport { getThumbnailForVideo } from '../utils/utils';\n\nasync function getAssetResultForGenerated<K extends OutputKind>(\n id: string,\n kind: K,\n blockInputs: GetBlockInputResult<K>,\n output: Output\n): Promise<AssetResult> {\n switch (kind) {\n case 'image': {\n if (output.kind !== 'image') {\n throw new Error(\n `Output kind does not match the expected type: ${output.kind} (expected: image)`\n );\n }\n\n return getImageAssetResultForGenerated(\n id,\n blockInputs[kind] as InputByKind['image'],\n output\n );\n }\n\n case 'video': {\n if (output.kind !== 'video') {\n throw new Error(\n `Output kind does not match the expected type: ${output.kind} (expected: video)`\n );\n }\n\n return getVideoAssetResultForGenerated(\n id,\n blockInputs[kind] as InputByKind['video'],\n output\n );\n }\n\n case 'audio': {\n if (output.kind !== 'audio') {\n throw new Error(\n `Output kind does not match the expected type: ${output.kind} (expected: audio)`\n );\n }\n\n return getAudioAssetResultForGenerated(\n id,\n blockInputs[kind] as InputByKind['audio'],\n output\n );\n }\n\n case 'sticker': {\n if (output.kind !== 'sticker') {\n throw new Error(\n `Output kind does not match the expected type: ${output.kind} (expected: sticker)`\n );\n }\n\n return getStickerAssetResultForGenerated(\n id,\n blockInputs[kind] as InputByKind['sticker'],\n output\n );\n }\n\n default: {\n throw new Error(\n `Unsupported output kind for creating placeholder block: ${kind}`\n );\n }\n }\n}\n\nfunction getImageAssetResultForGenerated(\n id: string,\n input: InputByKind['image'],\n output: ImageOutput\n): AssetResult {\n const width = input.width;\n const height = input.height;\n return {\n id,\n label: input.label,\n meta: {\n uri: output.url,\n thumbUri: output.url,\n fillType: '//ly.img.ubq/fill/image',\n kind: 'image',\n\n width,\n height\n },\n payload: {\n sourceSet: [\n {\n uri: output.url,\n width,\n height\n }\n ]\n }\n };\n}\n\nasync function getVideoAssetResultForGenerated(\n id: string,\n input: InputByKind['video'],\n output: VideoOutput\n): Promise<AssetResult> {\n const width = input.width;\n const height = input.height;\n\n const thumbUri = await getThumbnailForVideo(output.url, 0);\n\n return {\n id,\n label: input.label,\n meta: {\n uri: output.url,\n thumbUri,\n\n mimeType: 'video/mp4',\n kind: 'video',\n fillType: '//ly.img.ubq/fill/video',\n\n duration: input.duration.toString(),\n\n width,\n height\n }\n };\n}\n\nfunction getAudioAssetResultForGenerated(\n id: string,\n input: InputByKind['audio'],\n output: AudioOutput\n): AssetResult {\n return {\n id,\n label: input.label,\n meta: {\n uri: output.url,\n thumbUri: output.thumbnailUrl,\n blockType: '//ly.img.ubq/audio',\n mimeType: 'audio/x-m4a',\n duration: output.duration.toString()\n }\n };\n}\n\nfunction getStickerAssetResultForGenerated(\n id: string,\n input: InputByKind['sticker'],\n output: StickerOutput\n): AssetResult {\n const width = input.width;\n const height = input.height;\n return {\n id,\n label: input.label,\n meta: {\n uri: output.url,\n thumbUri: output.url,\n fillType: '//ly.img.ubq/fill/image',\n kind: 'sticker',\n\n width,\n height\n },\n payload: {\n sourceSet: [\n {\n uri: output.url,\n width,\n height\n }\n ]\n }\n };\n}\n\nexport default getAssetResultForGenerated;\n", "import type { AssetDefinition } from '@cesdk/cesdk-js';\nimport { addAssetToScene, isAbortError, uuid4 } from '../utils/utils';\nimport { type GetBlockInput, OutputKind, type Output } from '../core/provider';\nimport { Generate, Result } from './createGenerateFunction';\nimport getAssetResultForPlaceholder from '../assets/getAssetResultForPlaceholder';\nimport CreativeEditorSDK from '@cesdk/cesdk-js';\nimport { Middleware } from '../middleware/middleware';\nimport getAssetResultForGenerated from '../assets/getAssetResultForGenerated';\n\ntype PanelGenerationOptions<K extends OutputKind, I, O extends Output> = {\n /**\n * The kind to generate.\n */\n kind: K;\n\n /**\n * Initialized generate function\n */\n generate: Generate<I, O>;\n\n /**\n * The user flow for the generation process.\n */\n userFlow: 'placeholder' | 'generation-only';\n\n /**\n * Function to get block input from the generated input.\n */\n getBlockInput: GetBlockInput<K, I>;\n\n /**\n * Asset source id of the history library where a generated asset\n * will be added.\n */\n historyAssetSourceId?: string;\n\n /**\n * Additional middlewares added to the generation process.\n */\n middlewares?: Middleware<I, O>[];\n\n /**\n * Print debug information to the console.\n */\n debug?: boolean;\n\n /**\n * Enable dry run mode for testing.\n */\n dryRun?: boolean;\n\n /**\n * Signal to check if process was aborted.\n */\n abortSignal: AbortSignal;\n\n cesdk: CreativeEditorSDK;\n};\n\n/**\n * Handler for generating content from a panel interface.\n * Creates placeholder blocks and manages the generation process.\n */\nfunction handleGenerateFromPanel<K extends OutputKind, I, O extends Output>(\n options: PanelGenerationOptions<K, I, O>\n): (input: I) => Promise<Result<O>> {\n switch (options.userFlow) {\n case 'placeholder':\n return handleGeneratePlaceholderUserFlow(options);\n case 'generation-only':\n return handleGenerateGenerationOnlyUserFlow(options);\n default:\n throw new Error(\n `Unknown user flow: ${options.userFlow}. Expected 'placeholder' or 'generation-only'.`\n );\n }\n}\n\nfunction handleGenerateGenerationOnlyUserFlow<\n K extends OutputKind,\n I,\n O extends Output\n>(options: PanelGenerationOptions<K, I, O>): (input: I) => Promise<Result<O>> {\n const { cesdk, abortSignal } = options;\n\n return async (input: I) => {\n try {\n const kind = options.kind;\n const blockInputs = await options.getBlockInput(input);\n\n if (checkAbortSignal(cesdk, abortSignal)) return { status: 'aborted' };\n\n const result = await options.generate(input, {\n middlewares: [...(options.middlewares ?? [])],\n debug: options.debug,\n dryRun: options.dryRun,\n abortSignal\n });\n\n if (checkAbortSignal(cesdk, abortSignal)) return { status: 'aborted' };\n\n if (result.status !== 'success') {\n return result;\n }\n\n if (result.type === 'async') {\n // If the result is an async generator, we need to handle it differently.\n // This is a placeholder for handling sync results.\n // You might want to implement logic to handle async results here.\n throw new Error(\n 'Async generation is not supported in this context yet.'\n );\n }\n\n if (checkAbortSignal(cesdk, abortSignal)) return { status: 'aborted' };\n\n if (options.historyAssetSourceId != null) {\n const assetId = uuid4();\n const generatedAssetResult = await getAssetResultForGenerated(\n assetId,\n kind,\n blockInputs,\n result.output\n );\n const assetDefinition: AssetDefinition = {\n ...generatedAssetResult,\n id: `${Date.now()}-${generatedAssetResult.id}`,\n label:\n generatedAssetResult.label != null\n ? {\n en: generatedAssetResult.label\n }\n : {},\n tags: {}\n };\n cesdk.engine.asset.addAssetToSource(\n options.historyAssetSourceId,\n assetDefinition\n );\n } else {\n if (options.debug) {\n // eslint-disable-next-line no-console\n console.log(\n 'No asset source ID found in history and generation only was requested. Doing nothing. If no middleware is adding functionality this could be a bug.'\n );\n }\n }\n\n return result;\n } catch (error) {\n return {\n status: 'error',\n message: error instanceof Error ? error.message : String(error)\n };\n }\n };\n}\n\n/**\n * Handles generation from a panel with a placeholder block.\n */\nfunction handleGeneratePlaceholderUserFlow<\n K extends OutputKind,\n I,\n O extends Output\n>(options: PanelGenerationOptions<K, I, O>): (input: I) => Promise<Result<O>> {\n const { cesdk, abortSignal } = options;\n\n let placeholderBlock: number | undefined;\n return async (input: I) => {\n try {\n const kind = options.kind;\n const blockInputs = await options.getBlockInput(input);\n\n if (checkAbortSignal(cesdk, abortSignal)) return { status: 'aborted' };\n\n const assetId = uuid4();\n const assetResult = getAssetResultForPlaceholder(\n assetId,\n kind,\n blockInputs\n );\n\n placeholderBlock = await addAssetToScene(cesdk, assetResult);\n if (checkAbortSignal(cesdk, abortSignal, placeholderBlock))\n return { status: 'aborted' };\n // This is a workaround. The middleware in the video timeline\n // is calling APIs that will render the block in an error\n // state if it does not have an URI set. It's difficult to\n // recover from that. A bug report has been created for this.\n // As a workaround: Duplicating the block will remove the error state\n // but you will still see an error in the web console.\n // TODO: Remove this workaround when the bug is fixed.\n if (placeholderBlock != null && options.kind === 'video') {\n const positionX = cesdk.engine.block.getPositionX(placeholderBlock);\n const positionY = cesdk.engine.block.getPositionY(placeholderBlock);\n const duplicated = cesdk.engine.block.duplicate(placeholderBlock);\n cesdk.engine.block.setPositionX(duplicated, positionX);\n cesdk.engine.block.setPositionY(duplicated, positionY);\n cesdk.engine.block.destroy(placeholderBlock);\n placeholderBlock = duplicated;\n }\n\n if (placeholderBlock == null)\n throw new Error('Could not create placeholder block');\n\n cesdk.engine.block.setState(placeholderBlock, {\n type: 'Pending',\n progress: 0\n });\n\n const result = await options.generate(input, {\n blockIds: [placeholderBlock],\n middlewares: [...(options.middlewares ?? [])],\n debug: options.debug,\n dryRun: options.dryRun,\n abortSignal\n });\n\n if (checkAbortSignal(cesdk, abortSignal, placeholderBlock))\n return { status: 'aborted' };\n\n if (result.status !== 'success') {\n // Update block state before returning to prevent stuck in Pending state\n // Check if default was prevented\n if (!result.middlewareOptions?.defaultPrevented()) {\n if (\n placeholderBlock != null &&\n cesdk.engine.block.isValid(placeholderBlock)\n ) {\n if (result.status === 'aborted') {\n cesdk.engine.block.destroy(placeholderBlock);\n } else {\n cesdk.engine.block.setState(placeholderBlock, {\n type: 'Error',\n error: 'Unknown'\n });\n }\n }\n }\n return result;\n }\n\n if (result.type === 'async') {\n // If the result is an async generator, we need to handle it differently.\n // This is a placeholder for handling sync results.\n // You might want to implement logic to handle async results here.\n throw new Error(\n 'Async generation is not supported in this context yet.'\n );\n }\n\n if (!cesdk.engine.block.isValid(placeholderBlock)) {\n return {\n status: 'aborted',\n message:\n 'Placeholder block was destroyed before generation completed.'\n };\n }\n\n const generatedAssetResult = await getAssetResultForGenerated(\n assetId,\n kind,\n blockInputs,\n result.output\n );\n\n if (checkAbortSignal(cesdk, abortSignal, placeholderBlock))\n return { status: 'aborted' };\n\n if (options.debug)\n // eslint-disable-next-line no-console\n console.log(\n 'Updating placeholder in scene:',\n JSON.stringify(generatedAssetResult, undefined, 2)\n );\n\n await cesdk.engine.asset.defaultApplyAssetToBlock(\n generatedAssetResult,\n placeholderBlock\n );\n\n if (checkAbortSignal(cesdk, abortSignal, placeholderBlock))\n return { status: 'aborted' };\n\n if (options.historyAssetSourceId != null) {\n const assetDefinition: AssetDefinition = {\n ...generatedAssetResult,\n id: `${Date.now()}-${generatedAssetResult.id}`,\n label:\n generatedAssetResult.label != null\n ? {\n en: generatedAssetResult.label\n }\n : {},\n tags: {}\n };\n cesdk.engine.asset.addAssetToSource(\n options.historyAssetSourceId,\n assetDefinition\n );\n }\n\n if (cesdk.engine.block.isValid(placeholderBlock)) {\n cesdk.engine.block.setState(placeholderBlock, {\n type: 'Ready'\n });\n }\n\n return result;\n } catch (error) {\n // Note: For exceptions thrown in middleware, we don't have access to middlewareOptions\n // so we can't check defaultPrevented here. Middleware should handle errors in catch blocks\n // and re-throw to use preventDefault properly.\n if (\n placeholderBlock != null &&\n cesdk.engine.block.isValid(placeholderBlock)\n ) {\n if (isAbortError(error)) {\n cesdk.engine.block.destroy(placeholderBlock);\n } else {\n cesdk.engine.block.setState(placeholderBlock, {\n type: 'Error',\n error: 'Unknown'\n });\n }\n }\n return {\n status: 'error',\n message: error instanceof Error ? error.message : String(error)\n };\n }\n };\n}\n\n/**\n * Check the given abort signal and destroy the placeholder block if it is aborted.\n * @returns `true` if the signal is aborted, `false` otherwise.\n */\nfunction checkAbortSignal(\n cesdk: CreativeEditorSDK,\n abortSignal: AbortSignal,\n placeholderBlock?: number\n) {\n if (abortSignal.aborted) {\n if (\n placeholderBlock != null &&\n cesdk.engine.block.isValid(placeholderBlock)\n ) {\n cesdk.engine.block.destroy(placeholderBlock);\n }\n return true;\n }\n return false;\n}\n\nexport default handleGenerateFromPanel;\n", "import CreativeEditorSDK, {\n type BuilderRenderFunctionContext\n} from '@cesdk/cesdk-js';\nimport type Provider from '../../core/provider';\nimport {\n type GetInput,\n type OutputKind,\n type Output,\n type GetBlockInput\n} from '../../core/provider';\nimport { UIOptions, CommonConfiguration } from '../../types';\nimport { isAbortError } from '../../utils/utils';\nimport handleGenerationError from '../../generation/handleGenerationError';\nimport handleGenerateFromPanel from '../../generation/handleGenerateFromPanel';\nimport { Generate } from '../../generation/createGenerateFunction';\n\nexport function isGeneratingStateKey(providerId: string): string {\n return `${providerId}.generating`;\n}\n\nexport function abortGenerationStateKey(providerId: string): string {\n return `${providerId}.abort`;\n}\n\n/**\n * Renders the generation UI components and sets up event handlers\n */\nfunction renderGenerationComponents<K extends OutputKind, I, O extends Output>(\n context: BuilderRenderFunctionContext<any>,\n provider: Provider<K, I, O>,\n generate: Generate<I, O>,\n getInput: GetInput<I>,\n getBlockInput: GetBlockInput<K, I>,\n options: UIOptions & {\n createPlaceholderBlock?: boolean;\n includeHistoryLibrary?: boolean;\n requiredInputs?: string[];\n },\n config: CommonConfiguration<I, O>\n): void {\n const { builder, experimental } = context;\n const { cesdk, includeHistoryLibrary = true } = options;\n const {\n id: providerId,\n output: { abortable }\n } = provider;\n const abortState = experimental.global<() => void>(\n abortGenerationStateKey(providerId),\n () => {}\n );\n const generatingState = experimental.global<boolean>(\n isGeneratingStateKey(providerId),\n false\n );\n\n let abortController: AbortController | undefined;\n const canAbortNow = generatingState.value && abortable;\n const abort = () => {\n if (canAbortNow) {\n abortState.value();\n generatingState.setValue(false);\n abortState.setValue(() => {});\n }\n };\n\n let isDisabled: boolean | undefined;\n if (options.requiredInputs != null && options.requiredInputs.length > 0) {\n const inputs = getInput();\n isDisabled = options.requiredInputs.every((input) => {\n // @ts-ignore\n const hasInput = !inputs.input[input];\n return hasInput;\n });\n }\n\n const confirmCancelDialogId = experimental.global<string | undefined>(\n `${providerId}.confirmationDialogId`,\n undefined\n );\n\n builder.Section(`${providerId}.generate.section`, {\n children: () => {\n builder.Button(`${providerId}.generate`, {\n label: ['common.generate', `panel.${providerId}.generate`],\n isLoading: generatingState.value,\n color: 'accent',\n isDisabled,\n suffix: canAbortNow\n ? {\n icon: '@imgly/Cross',\n color: 'danger',\n tooltip: [`panel.${providerId}.abort`, 'common.cancel'],\n onClick: () => {\n const confirmationDialogId = cesdk.ui.showDialog({\n type: 'warning',\n content: 'panel.ly.img.ai.generation.confirmCancel.content',\n cancel: {\n label: 'common.close',\n onClick: ({ id }) => {\n cesdk.ui.closeDialog(id);\n confirmCancelDialogId.setValue(undefined);\n }\n },\n actions: {\n label: 'panel.ly.img.ai.generation.confirmCancel.confirm',\n color: 'danger',\n onClick: ({ id }) => {\n abort();\n cesdk.ui.closeDialog(id);\n confirmCancelDialogId.setValue(undefined);\n }\n }\n });\n confirmCancelDialogId.setValue(confirmationDialogId);\n }\n }\n : undefined,\n onClick: async () => {\n abortController = new AbortController();\n const abortSignal = abortController.signal;\n\n const triggerGeneration = async () => {\n try {\n generatingState.setValue(true);\n abortState.setValue(() => {\n if (config.debug)\n // eslint-disable-next-line no-console\n console.log('Aborting generation');\n abortController?.abort();\n });\n\n const result = await handleGenerateFromPanel({\n kind: provider.kind,\n generate,\n historyAssetSourceId: options.historyAssetSourceId,\n // TODO: Replace with a merged configuration\n userFlow: options.createPlaceholderBlock\n ? 'placeholder'\n : 'generation-only',\n getBlockInput,\n abortSignal,\n cesdk,\n debug: config.debug,\n dryRun: config.dryRun\n })(getInput().input);\n\n if (result.status === 'aborted') {\n return;\n }\n\n if (result.status === 'error') {\n handleGenerationError(result.message, {\n cesdk,\n provider,\n getInput,\n middlewareOptions: result.middlewareOptions\n });\n return;\n }\n\n if (result.status === 'success' && result.type === 'sync') {\n // Check if default was prevented\n if (!result.middlewareOptions?.defaultPrevented()) {\n const notification = provider.output.notification;\n showSuccessNotification(cesdk, notification, () => ({\n input: getInput().input,\n output: result.output\n }));\n }\n }\n } catch (error) {\n // Do not treat abort errors as errors\n if (isAbortError(error)) {\n return;\n }\n\n // Note: For exceptions thrown in middleware, we don't have access to middlewareOptions\n // so we can't check defaultPrevented here. Middleware should handle errors in catch blocks\n // and re-throw to use preventDefault properly.\n handleGenerationError(error, {\n cesdk,\n provider,\n getInput\n });\n } finally {\n abortController = undefined;\n generatingState.setValue(false);\n abortState.setValue(() => {});\n\n if (confirmCancelDialogId.value != null) {\n cesdk.ui.closeDialog(confirmCancelDialogId.value);\n confirmCancelDialogId.setValue(undefined);\n }\n }\n };\n\n await triggerGeneration();\n }\n });\n if (provider.output.generationHintText != null) {\n builder.Text(`${providerId}.generation-hint`, {\n align: 'center',\n content: provider.output.generationHintText\n });\n }\n }\n });\n\n if (includeHistoryLibrary && options.historyAssetLibraryEntryId != null) {\n builder.Library(`${providerId}.history.library`, {\n entries: [options.historyAssetLibraryEntryId]\n });\n }\n}\n\nfunction showSuccessNotification<I, O extends Output>(\n cesdk: CreativeEditorSDK,\n notifications: Provider<any, I, O>['output']['notification'],\n createContext: () => { input: I; output: O }\n): boolean {\n const successNotification = notifications?.success;\n if (successNotification == null) return false;\n\n const showOnSuccess =\n typeof successNotification.show === 'function'\n ? successNotification.show(createContext())\n : successNotification.show;\n\n if (!showOnSuccess) return false;\n\n const message =\n typeof successNotification.message === 'function'\n ? successNotification.message(createContext())\n : successNotification.message ?? 'common.ai-generation.success';\n\n const action =\n successNotification.action != null\n ? {\n label:\n typeof successNotification.action.label === 'function'\n ? successNotification.action.label(createContext())\n : successNotification.action.label,\n onClick: () => {\n successNotification?.action?.onClick(createContext());\n }\n }\n : undefined;\n\n cesdk.ui.showNotification({\n type: 'success',\n message,\n action,\n duration: successNotification.duration\n });\n return true;\n}\n\nexport default renderGenerationComponents;\n", "import { BuilderRenderFunction } from '@cesdk/cesdk-js';\nimport { OutputKind, PanelInputCustom, type Output } from '../../core/provider';\nimport renderGenerationComponents, {\n isGeneratingStateKey\n} from '../components/renderGenerationComponents';\nimport { InitializationContext } from '../../types';\nimport { Generate } from '../../generation/createGenerateFunction';\n\nasync function createPanelRenderFunctionFromCustom<\n K extends OutputKind,\n I,\n O extends Output\n>(\n {\n options,\n provider,\n panelInput,\n config\n }: InitializationContext<K, I, O, PanelInputCustom<K, I>>,\n\n generate: Generate<I, O>\n): Promise<BuilderRenderFunction<any> | undefined> {\n if (panelInput == null) {\n return undefined;\n }\n\n const { cesdk } = options;\n const { id: providerId } = provider;\n\n if (config.debug) {\n // eslint-disable-next-line no-console\n console.log(`Provider: ${providerId} (custom)`);\n }\n\n const render = panelInput.render;\n\n const builderRenderFunction: BuilderRenderFunction<any> = (context) => {\n const { state } = context;\n\n const isGenerating = state(isGeneratingStateKey(providerId), {\n isGenerating: false,\n abort: () => {}\n }).value.isGenerating;\n\n const { getInput, getBlockInput } = render(context, {\n cesdk,\n isGenerating\n });\n renderGenerationComponents(\n context,\n provider,\n generate,\n getInput,\n getBlockInput,\n {\n ...options,\n includeHistoryLibrary: panelInput.includeHistoryLibrary ?? true,\n createPlaceholderBlock: panelInput.userFlow === 'placeholder'\n },\n config\n );\n\n return getInput;\n };\n\n return builderRenderFunction;\n}\n\nexport default createPanelRenderFunctionFromCustom;\n", "import { type OpenAPIV3 } from 'openapi-types';\n\n/**\n * Resolves a JSON reference path within a document\n * @param document The OpenAPI document\n * @param refPath The reference path (e.g. \"#/components/schemas/MySchema\")\n * @returns The resolved object from the document\n */\nexport function resolveReference(\n document: OpenAPIV3.Document,\n refPath: string\n): unknown {\n // Only handle internal references\n if (!refPath.startsWith('#/')) {\n throw new Error(`External references are not supported: ${refPath}`);\n }\n\n // Remove the leading #/\n const path = refPath.substring(2).split('/');\n let current: any = document;\n\n // Navigate through the path\n for (const segment of path) {\n if (current === undefined || current === null) {\n throw new Error(`Invalid reference path: ${refPath}`);\n }\n current = current[segment];\n }\n\n if (current === undefined) {\n throw new Error(`Reference not found: ${refPath}`);\n }\n\n return current;\n}\n\n/**\n * Recursively dereferences all $ref properties in an object\n * @param document The original document for resolving references\n * @param obj The object to dereference\n * @param visited Set of visited objects to prevent circular references\n * @returns The dereferenced object\n */\nfunction dereferenceObject(\n document: OpenAPIV3.Document,\n obj: any,\n visited = new Set<any>()\n): any {\n // Handle null or undefined\n if (obj === null || obj === undefined) {\n return obj;\n }\n\n // If we've seen this object before, return it to avoid circular references\n if (visited.has(obj)) {\n return obj;\n }\n\n // Add the current object to the visited set\n visited.add(obj);\n\n // Handle $ref\n if (obj.$ref && typeof obj.$ref === 'string') {\n // Get the referenced object\n const referenced = resolveReference(document, obj.$ref);\n // Dereference the referenced object\n const dereferenced = dereferenceObject(document, referenced, visited);\n\n // Merge other properties from the original object\n const result = { ...dereferenced };\n for (const key in obj) {\n if (Object.prototype.hasOwnProperty.call(obj, key) && key !== '$ref') {\n result[key] = dereferenceObject(document, obj[key], visited);\n }\n }\n return result;\n }\n\n // Handle arrays\n if (Array.isArray(obj)) {\n return obj.map((item) => dereferenceObject(document, item, visited));\n }\n\n // Handle objects\n if (typeof obj === 'object') {\n const result: any = {};\n for (const key in obj) {\n if (Object.prototype.hasOwnProperty.call(obj, key)) {\n result[key] = dereferenceObject(document, obj[key], visited);\n }\n }\n return result;\n }\n\n // Return primitives as is\n return obj;\n}\n\n/**\n * Dereferences all $ref properties in an OpenAPI document\n * @param document The OpenAPI document to dereference\n * @returns A new document with all references resolved\n */\nexport default function dereferenceDocument(\n document: OpenAPIV3.Document\n): OpenAPIV3.Document {\n return dereferenceObject(document, { ...document }) as OpenAPIV3.Document;\n}\n", "import { OpenAPIV3 } from 'openapi-types';\n\n/**\n * Checks if an unknown object is an OpenAPI schema (first level only)\n *\n * @param obj - The object to check (potentially undefined)\n * @param debug - If true, log the reason when validation fails\n * @returns A boolean indicating whether the object is an OpenAPI schema\n */\nexport function isOpenAPISchema(\n obj: unknown,\n debug: boolean = false\n): obj is OpenAPIV3.SchemaObject {\n // Helper function to log debug messages and return false\n const fail = (reason: string): false => {\n if (debug) {\n // eslint-disable-next-line no-console\n console.log(`OpenAPI Schema validation failed: ${reason}`);\n }\n return false;\n };\n\n // Check if obj is an object and not null\n if (typeof obj !== 'object' || obj === null) {\n return fail(\n `Input is ${obj === null ? 'null' : typeof obj}, not an object`\n );\n }\n\n const schema = obj as Record<string, any>;\n\n // Basic property validation - most schemas have at least one of these properties\n const hasSchemaTypeProperties =\n typeof schema.type === 'string' ||\n Array.isArray(schema.enum) ||\n typeof schema.properties === 'object' ||\n typeof schema.items === 'object' ||\n typeof schema.allOf === 'object' ||\n typeof schema.anyOf === 'object' ||\n typeof schema.oneOf === 'object' ||\n typeof schema.not === 'object';\n\n if (!hasSchemaTypeProperties) {\n return fail(\n 'Missing required schema-defining properties (type, enum, properties, items, allOf, anyOf, oneOf, not)'\n );\n }\n\n // If it has a type, validate it's one of the allowed OpenAPI schema types\n if (schema.type !== undefined) {\n const validTypes = [\n 'string',\n 'number',\n 'integer',\n 'boolean',\n 'array',\n 'object',\n 'null'\n ];\n\n // Type can be a string or an array of strings\n if (typeof schema.type === 'string') {\n if (!validTypes.includes(schema.type)) {\n return fail(\n `Invalid type: ${schema.type}. Must be one of ${validTypes.join(\n ', '\n )}`\n );\n }\n } else if (Array.isArray(schema.type)) {\n for (const type of schema.type) {\n if (typeof type !== 'string' || !validTypes.includes(type)) {\n return fail(\n `Array of types contains invalid value: ${type}. Must be one of ${validTypes.join(\n ', '\n )}`\n );\n }\n }\n } else {\n return fail(\n `Type must be a string or array of strings, got ${typeof schema.type}`\n );\n }\n }\n\n // If it has items (for array type), validate that items is an object (without recursion)\n if (schema.items !== undefined) {\n if (typeof schema.items !== 'object' || schema.items === null) {\n return fail(\n `Items must be an object, got ${\n schema.items === null ? 'null' : typeof schema.items\n }`\n );\n }\n }\n\n // If it has properties (for object type), validate that properties is an object (without recursion)\n if (schema.properties !== undefined) {\n if (typeof schema.properties !== 'object' || schema.properties === null) {\n return fail(\n `Properties must be an object, got ${\n schema.properties === null ? 'null' : typeof schema.properties\n }`\n );\n }\n }\n\n // Check if advanced schema constructs are valid (without recursion)\n const schemaArrays = ['allOf', 'anyOf', 'oneOf'];\n for (const arrayType of schemaArrays) {\n if (schema[arrayType] !== undefined) {\n if (!Array.isArray(schema[arrayType])) {\n return fail(\n `${arrayType} must be an array, got ${typeof schema[arrayType]}`\n );\n }\n\n // Just check that each item is an object (without recursion)\n for (let i = 0; i < schema[arrayType].length; i++) {\n const subSchema = schema[arrayType][i];\n if (typeof subSchema !== 'object' || subSchema === null) {\n return fail(\n `Item ${i} in ${arrayType} must be an object, got ${\n subSchema === null ? 'null' : typeof subSchema\n }`\n );\n }\n }\n }\n }\n\n // Check if 'not' is an object\n if (schema.not !== undefined) {\n if (typeof schema.not !== 'object' || schema.not === null) {\n return fail(\n `'not' must be an object, got ${\n schema.not === null ? 'null' : typeof schema.not\n }`\n );\n }\n }\n\n // If we have additionalProperties, make sure it's a boolean or an object\n if (schema.additionalProperties !== undefined) {\n if (\n typeof schema.additionalProperties !== 'boolean' &&\n (typeof schema.additionalProperties !== 'object' ||\n schema.additionalProperties === null)\n ) {\n return fail(\n `additionalProperties must be a boolean or an object, got ${\n schema.additionalProperties === null\n ? 'null'\n : typeof schema.additionalProperties\n }`\n );\n }\n }\n\n // Check basic format if present (for string type)\n if (schema.format !== undefined && typeof schema.format !== 'string') {\n return fail(`format must be a string, got ${typeof schema.format}`);\n }\n\n // Check number constraints\n const numberConstraints = [\n 'minimum',\n 'maximum',\n 'exclusiveMinimum',\n 'exclusiveMaximum',\n 'multipleOf'\n ];\n for (const constraint of numberConstraints) {\n if (\n schema[constraint] !== undefined &&\n typeof schema[constraint] !== 'number'\n ) {\n return fail(\n `${constraint} must be a number, got ${typeof schema[constraint]}`\n );\n }\n }\n\n // Check string constraints\n if (\n schema.minLength !== undefined &&\n (typeof schema.minLength !== 'number' || schema.minLength < 0)\n ) {\n return fail(\n `minLength must be a non-negative number, got ${\n typeof schema.minLength === 'number'\n ? schema.minLength\n : typeof schema.minLength\n }`\n );\n }\n if (\n schema.maxLength !== undefined &&\n (typeof schema.maxLength !== 'number' || schema.maxLength < 0)\n ) {\n return fail(\n `maxLength must be a non-negative number, got ${\n typeof schema.maxLength === 'number'\n ? schema.maxLength\n : typeof schema.maxLength\n }`\n );\n }\n if (schema.pattern !== undefined && typeof schema.pattern !== 'string') {\n return fail(`pattern must be a string, got ${typeof schema.pattern}`);\n }\n\n // Check array constraints\n if (\n schema.minItems !== undefined &&\n (typeof schema.minItems !== 'number' || schema.minItems < 0)\n ) {\n return fail(\n `minItems must be a non-negative number, got ${\n typeof schema.minItems === 'number'\n ? schema.minItems\n : typeof schema.minItems\n }`\n );\n }\n if (\n schema.maxItems !== undefined &&\n (typeof schema.maxItems !== 'number' || schema.maxItems < 0)\n ) {\n return fail(\n `maxItems must be a non-negative number, got ${\n typeof schema.maxItems === 'number'\n ? schema.maxItems\n : typeof schema.maxItems\n }`\n );\n }\n if (\n schema.uniqueItems !== undefined &&\n typeof schema.uniqueItems !== 'boolean'\n ) {\n return fail(\n `uniqueItems must be a boolean, got ${typeof schema.uniqueItems}`\n );\n }\n\n // Check object constraints\n if (\n schema.minProperties !== undefined &&\n (typeof schema.minProperties !== 'number' || schema.minProperties < 0)\n ) {\n return fail(\n `minProperties must be a non-negative number, got ${\n typeof schema.minProperties === 'number'\n ? schema.minProperties\n : typeof schema.minProperties\n }`\n );\n }\n if (\n schema.maxProperties !== undefined &&\n (typeof schema.maxProperties !== 'number' || schema.maxProperties < 0)\n ) {\n return fail(\n `maxProperties must be a non-negative number, got ${\n typeof schema.maxProperties === 'number'\n ? schema.maxProperties\n : typeof schema.maxProperties\n }`\n );\n }\n if (schema.required !== undefined) {\n if (!Array.isArray(schema.required)) {\n return fail(`required must be an array, got ${typeof schema.required}`);\n }\n for (let i = 0; i < schema.required.length; i++) {\n const prop = schema.required[i];\n if (typeof prop !== 'string') {\n return fail(\n `Item ${i} in required array must be a string, got ${typeof prop}`\n );\n }\n }\n }\n\n // It has passed all the first-level checks\n return true;\n}\n", "import { OpenAPIV3 } from 'openapi-types';\nimport { OutputKind, PanelInputSchema } from '../core/provider';\nimport { Property } from './types';\n\nfunction getProperties<K extends OutputKind, I>(\n inputSchema: OpenAPIV3.SchemaObject,\n panelInput: PanelInputSchema<K, I>\n): Property[] {\n if (inputSchema.properties == null) {\n throw new Error('Input schema must have properties');\n }\n const propertiesFromSchema = inputSchema.properties;\n const properties: Property[] = [];\n\n const order = getOrder(inputSchema, panelInput);\n order.forEach((propertyKey) => {\n const id = propertyKey;\n const schema =\n (propertiesFromSchema[propertyKey] as OpenAPIV3.SchemaObject) ??\n undefined;\n properties.push({ id, schema });\n });\n\n return properties;\n}\n\nfunction getOrder<K extends OutputKind, I>(\n inputSchema: OpenAPIV3.SchemaObject,\n panelInput: PanelInputSchema<K, I>\n): string[] {\n const panelInputOrder = panelInput.order;\n if (panelInputOrder != null && Array.isArray(panelInputOrder)) {\n return panelInputOrder;\n }\n\n if (inputSchema.properties == null) {\n throw new Error('Input schema must have properties');\n }\n const propertiesFromSchema = inputSchema.properties;\n const orderFromKeys = Object.keys(propertiesFromSchema);\n const orderFromExtensionKeyword = getOrderFromExtensionKeyword(\n inputSchema,\n panelInput\n );\n\n let order = orderFromExtensionKeyword ?? orderFromKeys;\n\n if (panelInputOrder != null && typeof panelInputOrder === 'function') {\n order = panelInputOrder(order);\n }\n\n // Return order with no duplicates\n return [...new Set(order)];\n}\n\n/**\n * Get the order from an extension keyword in the input schema (e.g. x-order) if it exists.\n */\nfunction getOrderFromExtensionKeyword<K extends OutputKind, I>(\n inputSchema: OpenAPIV3.SchemaObject,\n panelInput: PanelInputSchema<K, I>\n): string[] | undefined {\n if (panelInput.orderExtensionKeyword == null) {\n return undefined;\n }\n\n if (\n typeof panelInput.orderExtensionKeyword !== 'string' &&\n !Array.isArray(panelInput.orderExtensionKeyword)\n ) {\n throw new Error(\n 'orderExtensionKeyword must be a string or an array of strings'\n );\n }\n const orderExtensionKeywords =\n typeof panelInput.orderExtensionKeyword === 'string'\n ? [panelInput.orderExtensionKeyword]\n : panelInput.orderExtensionKeyword;\n\n const orderExtensionKeyword = orderExtensionKeywords.find(\n (extensionKeyword) => {\n return extensionKeyword in inputSchema;\n }\n );\n\n if (orderExtensionKeyword == null) {\n return undefined;\n }\n const order =\n // @ts-ignore\n inputSchema[orderExtensionKeyword] as string[];\n\n return order;\n}\n\nexport default getProperties;\n", "/* eslint-disable @typescript-eslint/no-unused-vars */\nimport { BuilderRenderFunctionContext } from '@cesdk/cesdk-js';\nimport {\n EnumValue,\n ExtensionImglyBuilder,\n GetPropertyInput,\n Property,\n PropertyInput\n} from './types';\nimport Provider, {\n Output,\n OutputKind,\n PanelInputSchema\n} from '../core/provider';\nimport { UIOptions, CommonConfiguration } from '../types';\nimport { OpenAPIV3 } from 'openapi-types';\nimport getProperties from './getProperties';\nimport { getLabelFromId } from '../utils/utils';\nimport { buildPropertyContext } from '../utils/propertyContext';\nimport { resolvePropertyDefault } from '../utils/propertyResolver';\n\n/**\n * Creates a translation key array with fallback chain for property-related translations.\n * Used for input labels, placeholders, enum values, etc.\n *\n * @param property - The property being translated\n * @param provider - The AI provider\n * @param kind - The output kind (image, video, audio, etc.)\n * @param valueId - Optional suffix for the translation key (e.g., 'placeholder', enum value)\n * @returns Array of translation keys in priority order, with generic fallback for placeholders\n */\nfunction createPropertyTranslationKeys<\n K extends OutputKind,\n I,\n O extends Output\n>(\n property: Property,\n provider: Provider<K, I, O>,\n kind: K,\n valueId?: string\n): string[] {\n const baseKey = `property.${property.id}${valueId ? `.${valueId}` : ''}`;\n const keys = [\n `ly.img.plugin-ai-${kind}-generation-web.${provider.id}.${baseKey}`,\n `ly.img.plugin-ai-generation-web.${baseKey}`,\n `ly.img.plugin-ai-${kind}-generation-web.${provider.id}.defaults.${baseKey}`,\n `ly.img.plugin-ai-generation-web.defaults.${baseKey}`\n ];\n\n // For placeholder keys, append generic property placeholder fallback\n // This ensures that if no placeholder translation exists, an empty string is used\n // instead of showing the translation key itself\n if (valueId === 'placeholder') {\n keys.push('ly.img.plugin-ai-generation-web.fallback.property.placeholder');\n }\n\n return keys;\n}\n\nfunction extractEnumMetadata(schema: any): {\n labels: Record<string, string>;\n icons: Record<string, string>;\n} {\n const labels =\n 'x-imgly-enum-labels' in schema &&\n typeof schema['x-imgly-enum-labels'] === 'object'\n ? (schema['x-imgly-enum-labels'] as Record<string, string>)\n : {};\n\n const icons =\n 'x-imgly-enum-icons' in schema &&\n typeof schema['x-imgly-enum-icons'] === 'object'\n ? (schema['x-imgly-enum-icons'] as Record<string, string>)\n : {};\n\n return { labels, icons };\n}\n\nfunction renderProperty<K extends OutputKind, I, O extends Output>(\n context: BuilderRenderFunctionContext<any>,\n property: Property,\n provider: Provider<K, I, O>,\n panelInput: PanelInputSchema<K, I>,\n options: UIOptions,\n config: CommonConfiguration<I, O>,\n kind: K,\n providerConfig?: any\n): GetPropertyInput | undefined {\n if (property.schema == null) {\n if (\n panelInput.renderCustomProperty != null &&\n panelInput.renderCustomProperty[property.id] != null\n ) {\n // Extend context with provider configuration for custom properties\n const extendedContext = {\n ...context,\n providerConfig,\n config\n };\n return panelInput.renderCustomProperty[property.id](\n extendedContext,\n property\n );\n } else {\n return undefined;\n }\n }\n const propertyWithSchema: Required<Property> = property as Required<Property>;\n const type = property.schema.type;\n\n if (\n panelInput.renderCustomProperty != null &&\n panelInput.renderCustomProperty[property.id] != null\n ) {\n // Extend context with provider configuration for custom properties\n const extendedContext = {\n ...context,\n providerConfig,\n config\n };\n return panelInput.renderCustomProperty[property.id](\n extendedContext,\n property\n );\n }\n\n switch (type) {\n case 'string': {\n if (property.schema.enum != null) {\n return renderEnumProperty(\n context,\n propertyWithSchema,\n provider,\n panelInput,\n options,\n config,\n kind,\n providerConfig\n );\n } else {\n return renderStringProperty(\n context,\n propertyWithSchema,\n provider,\n panelInput,\n options,\n config,\n kind,\n providerConfig\n );\n }\n }\n\n case 'boolean': {\n return renderBooleanProperty(\n context,\n propertyWithSchema,\n provider,\n panelInput,\n options,\n config,\n kind,\n providerConfig\n );\n }\n\n case 'number':\n case 'integer': {\n return renderIntegerProperty(\n context,\n propertyWithSchema,\n provider,\n panelInput,\n options,\n config,\n kind,\n providerConfig\n );\n }\n\n case 'object': {\n return renderObjectProperty(\n context,\n propertyWithSchema,\n provider,\n panelInput,\n options,\n config,\n kind,\n providerConfig\n );\n }\n\n case 'array': {\n // not supported yet\n break;\n }\n\n case undefined: {\n if (\n property.schema.anyOf != null &&\n Array.isArray(property.schema.anyOf)\n ) {\n return renderAnyOfProperty(\n context,\n propertyWithSchema,\n provider,\n panelInput,\n options,\n config,\n kind,\n providerConfig\n );\n }\n break;\n }\n\n default: {\n // eslint-disable-next-line no-console\n console.error(`Unsupported property type: ${type}`);\n }\n }\n}\n\nfunction renderObjectProperty<K extends OutputKind, I, O extends Output>(\n context: BuilderRenderFunctionContext<any>,\n property: Required<Property>,\n provider: Provider<K, I, O>,\n panelInput: PanelInputSchema<K, I>,\n options: UIOptions,\n config: CommonConfiguration<I, O>,\n kind: K,\n providerConfig?: any\n): GetPropertyInput {\n const properties = getProperties(property.schema ?? {}, panelInput);\n\n const childInputs = properties.reduce((acc, childProperty) => {\n const getInput = renderProperty(\n context,\n childProperty,\n provider,\n panelInput,\n options,\n config,\n kind,\n providerConfig\n );\n if (getInput != null) {\n acc[childProperty.id] = getInput();\n }\n return acc;\n }, {} as Record<string, PropertyInput>);\n\n return () => ({\n id: property.id,\n type: 'object',\n value: childInputs\n });\n}\n\nfunction renderStringProperty<K extends OutputKind, I, O extends Output>(\n context: BuilderRenderFunctionContext<any>,\n property: Required<Property>,\n provider: Provider<K, I, O>,\n panelInput: PanelInputSchema<K, I>,\n options: UIOptions,\n config: CommonConfiguration<I, O>,\n kind: K,\n providerConfig?: any\n): GetPropertyInput {\n const {\n builder,\n experimental: { global },\n engine\n } = context;\n const { id: propertyId } = property;\n\n const id = `${provider.id}.${propertyId}`;\n const inputLabel = createPropertyTranslationKeys(property, provider, kind);\n\n // Create placeholder i18n key array with fallback to global placeholder\n const placeholderKeys = createPropertyTranslationKeys(\n property,\n provider,\n kind,\n 'placeholder'\n );\n\n // Resolve default value from property configuration\n const propertyContext = buildPropertyContext(engine, options.cesdk);\n const propertyConfig =\n providerConfig?.properties?.[propertyId] ??\n (config as any).properties?.[propertyId];\n const defaultValue = resolvePropertyDefault(\n propertyId,\n propertyConfig,\n propertyContext,\n property.schema.default,\n ''\n );\n\n const propertyState = global(id, defaultValue);\n\n const extension = getImglyExtensionBuilder(property.schema);\n const builderComponent =\n extension?.component != null && extension?.component === 'TextArea'\n ? 'TextArea'\n : 'TextInput';\n\n // Build placeholder with i18n keys\n // Note: CE.SDK supports string arrays for i18n fallback at runtime,\n // but TypeScript types only declare string. Using type assertion as a temporary workaround.\n // TODO: Remove type assertion once CE.SDK types are fixed to accept string | string[] for placeholder\n // See: https://github.com/imgly/ubq/pull/10593\n const placeholder =\n placeholderKeys.length > 0 ? (placeholderKeys as any as string) : undefined;\n\n builder[builderComponent](id, {\n inputLabel,\n ...(placeholder && { placeholder }),\n ...propertyState\n });\n\n return () => ({\n id: property.id,\n type: 'string',\n value: propertyState.value\n });\n}\n\nfunction renderEnumProperty<K extends OutputKind, I, O extends Output>(\n context: BuilderRenderFunctionContext<any>,\n property: Required<Property>,\n provider: Provider<K, I, O>,\n panelInput: PanelInputSchema<K, I>,\n options: UIOptions,\n config: CommonConfiguration<I, O>,\n kind: K,\n providerConfig?: any\n): GetPropertyInput {\n const {\n builder,\n experimental: { global },\n engine\n } = context;\n const { id: propertyId } = property;\n\n const id = `${provider.id}.${propertyId}`;\n const inputLabel = createPropertyTranslationKeys(property, provider, kind);\n\n const { labels: enumLabels, icons } = extractEnumMetadata(property.schema);\n\n const values: EnumValue[] = (property.schema.enum ?? []).map((valueId) => ({\n id: valueId,\n label: createPropertyTranslationKeys(property, provider, kind, valueId),\n icon: icons[valueId]\n }));\n\n // Resolve default value from property configuration\n const propertyContext = buildPropertyContext(engine, options.cesdk);\n const propertyConfig =\n providerConfig?.properties?.[propertyId] ??\n (config as any).properties?.[propertyId];\n const resolvedDefault = resolvePropertyDefault(\n propertyId,\n propertyConfig,\n propertyContext,\n property.schema.default,\n values[0]?.id\n );\n\n const defaultValue =\n resolvedDefault != null\n ? values.find((v) => v.id === resolvedDefault) ?? values[0]\n : values[0];\n\n const propertyState = global<EnumValue>(id, defaultValue);\n\n builder.Select(id, {\n inputLabel,\n values,\n ...propertyState\n });\n\n return () => ({\n id: property.id,\n type: 'string',\n value: propertyState.value.id\n });\n}\n\nfunction renderBooleanProperty<K extends OutputKind, I, O extends Output>(\n context: BuilderRenderFunctionContext<any>,\n property: Required<Property>,\n provider: Provider<K, I, O>,\n panelInput: PanelInputSchema<K, I>,\n options: UIOptions,\n config: CommonConfiguration<I, O>,\n kind: K,\n providerConfig?: any\n): GetPropertyInput {\n const {\n builder,\n experimental: { global },\n engine\n } = context;\n const { id: propertyId } = property;\n\n const id = `${provider.id}.${propertyId}`;\n const inputLabel = createPropertyTranslationKeys(property, provider, kind);\n\n // Resolve default value from property configuration\n const propertyContext = buildPropertyContext(engine, options.cesdk);\n const propertyConfig =\n providerConfig?.properties?.[propertyId] ??\n (config as any).properties?.[propertyId];\n const defaultValue = !!resolvePropertyDefault(\n propertyId,\n propertyConfig,\n propertyContext,\n property.schema.default,\n false\n );\n const propertyState = global<boolean>(id, defaultValue);\n\n builder.Checkbox(id, {\n inputLabel,\n ...propertyState\n });\n\n return () => ({\n id: property.id,\n type: 'boolean',\n value: propertyState.value\n });\n}\n\nfunction renderIntegerProperty<K extends OutputKind, I, O extends Output>(\n context: BuilderRenderFunctionContext<any>,\n property: Required<Property>,\n provider: Provider<K, I, O>,\n panelInput: PanelInputSchema<K, I>,\n options: UIOptions,\n config: CommonConfiguration<I, O>,\n kind: K,\n providerConfig?: any\n): GetPropertyInput {\n const {\n builder,\n experimental: { global },\n engine\n } = context;\n const { id: propertyId } = property;\n\n const id = `${provider.id}.${propertyId}`;\n const inputLabel = createPropertyTranslationKeys(property, provider, kind);\n\n const minValue = property.schema.minimum;\n const maxValue = property.schema.maximum;\n\n // Resolve default value from property configuration\n const propertyContext = buildPropertyContext(engine, options.cesdk);\n const propertyConfig =\n providerConfig?.properties?.[propertyId] ??\n (config as any).properties?.[propertyId];\n let schemaDefault = property.schema.default;\n if (schemaDefault == null) {\n if (minValue != null) {\n schemaDefault = minValue;\n } else if (maxValue != null) {\n schemaDefault = maxValue;\n } else {\n schemaDefault = 0;\n }\n }\n const defaultValue = resolvePropertyDefault(\n propertyId,\n propertyConfig,\n propertyContext,\n schemaDefault,\n schemaDefault\n );\n\n const propertyState = global<number>(id, defaultValue);\n\n if (minValue != null && maxValue != null) {\n let step = property.schema.type === 'number' ? 0.1 : 1;\n if (\n 'x-imgly-step' in property.schema &&\n typeof property.schema['x-imgly-step'] === 'number'\n ) {\n step = property.schema['x-imgly-step'];\n }\n\n builder.Slider(id, {\n inputLabel,\n min: minValue,\n max: maxValue,\n step,\n ...propertyState\n });\n } else {\n builder.NumberInput(id, {\n inputLabel,\n min: minValue,\n max: maxValue,\n ...propertyState\n });\n }\n\n return () => ({\n id: property.id,\n type: 'integer',\n value: propertyState.value\n });\n}\n\nfunction renderAnyOfProperty<K extends OutputKind, I, O extends Output>(\n context: BuilderRenderFunctionContext<any>,\n property: Required<Property>,\n provider: Provider<K, I, O>,\n panelInput: PanelInputSchema<K, I>,\n options: UIOptions,\n config: CommonConfiguration<I, O>,\n kind: K,\n providerConfig?: any\n): GetPropertyInput | undefined {\n const {\n builder,\n experimental: { global },\n engine\n } = context;\n const { id: propertyId } = property;\n\n const id = `${provider.id}.${propertyId}`;\n const inputLabel = createPropertyTranslationKeys(property, provider, kind);\n\n const anyOf = (property.schema.anyOf ?? []) as OpenAPIV3.SchemaObject[];\n const values: EnumValue[] = [];\n const conditionalRender: Record<string, () => GetPropertyInput> = {};\n const conditionalInputs: Record<string, () => PropertyInput> = {};\n const { labels, icons } = extractEnumMetadata(property.schema);\n\n // Resolve default value from property configuration\n const propertyContext = buildPropertyContext(engine, options.cesdk);\n const propertyConfig =\n providerConfig?.properties?.[propertyId] ??\n (config as any).properties?.[propertyId];\n\n const renderFunctionMap: Record<string, Function> = {\n string: renderStringProperty,\n boolean: renderBooleanProperty,\n integer: renderIntegerProperty,\n object: renderObjectProperty\n };\n\n const extractValueId = (anySchema: any, schemaId: string): string =>\n (anySchema as any).$ref\n ? (anySchema as any).$ref.split('/').pop()\n : schemaId.split('.').pop() ?? schemaId;\n\n const createEnumValue = (enumId: string, valueId: string): EnumValue => ({\n id: enumId,\n label: createPropertyTranslationKeys(property, provider, kind, valueId),\n icon: icons[valueId] ?? icons[enumId]\n });\n\n anyOf.forEach((anySchema, index) => {\n const schemaId = `${provider.id}.${propertyId}.anyOf[${index}]`;\n\n if ((anySchema as any).$ref || anySchema.title) {\n const refName = (anySchema as any).$ref\n ? (anySchema as any).$ref.split('/').pop()\n : anySchema.title;\n\n conditionalRender[schemaId] = () =>\n renderObjectProperty(\n context,\n {\n id: schemaId,\n schema: { ...anySchema, title: labels[refName] || refName }\n },\n provider,\n panelInput,\n options,\n config,\n kind,\n providerConfig\n );\n\n values.push(createEnumValue(schemaId, refName));\n } else if (anySchema.type === 'string' && anySchema.enum) {\n anySchema.enum.forEach((valueId) => {\n values.push(createEnumValue(valueId, valueId));\n });\n } else if (anySchema.type && renderFunctionMap[anySchema.type]) {\n const renderFunction = renderFunctionMap[anySchema.type];\n conditionalRender[schemaId] = () =>\n renderFunction(\n context,\n { id: schemaId, schema: { ...anySchema, title: anySchema.title } },\n provider,\n panelInput,\n options,\n config,\n kind,\n providerConfig\n );\n\n const valueId = extractValueId(anySchema, schemaId);\n values.push(\n anySchema.type === 'string' && !anySchema.enum\n ? {\n id: schemaId,\n label: anySchema.title || valueId,\n icon:\n (anySchema.title && icons[anySchema.title]) || icons[valueId]\n }\n : createEnumValue(schemaId, valueId)\n );\n }\n });\n\n // Resolve the default using property configuration\n const resolvedDefault = resolvePropertyDefault(\n propertyId,\n propertyConfig,\n propertyContext,\n property.schema.default,\n null\n );\n\n const defaultValue =\n resolvedDefault != null\n ? values.find((value) => value.id === resolvedDefault) ?? values[0]\n : values[0];\n\n const propertyState = global<EnumValue>(id, defaultValue);\n\n builder.Select(id, {\n inputLabel,\n values,\n ...propertyState\n });\n\n if (propertyState.value.id in conditionalRender) {\n const inputs = conditionalRender[propertyState.value.id]();\n conditionalInputs[propertyState.value.id] = inputs;\n }\n\n return () => {\n const conditionalInput = conditionalInputs[propertyState.value.id];\n\n if (conditionalInput != null) {\n return {\n ...conditionalInput(),\n id: property.id\n };\n }\n\n return {\n id: property.id,\n type: 'string',\n value: propertyState.value.id\n };\n };\n}\n\nfunction getImglyExtensionBuilder(\n schema: OpenAPIV3.SchemaObject\n): ExtensionImglyBuilder | undefined {\n if ('x-imgly-builder' in schema) {\n const extension = schema['x-imgly-builder'] as ExtensionImglyBuilder;\n return extension;\n }\n\n return undefined;\n}\n\nexport default renderProperty;\n", "/**\n * Default translations for AI generation properties and enum values.\n * These are automatically applied as fallback translations for all AI providers.\n *\n * Structure:\n * - Property translations: `ly.img.plugin-ai-generation-web.defaults.property.${property.id}`\n * - Enum value translations: `ly.img.plugin-ai-generation-web.defaults.property.${property.id}.${enumValue}`\n * - Generic fallbacks: `ly.img.plugin-ai-generation-web.fallback.*`\n *\n * Based on actual OpenAPI Input schemas from all AI provider packages.\n */\nexport const defaultTranslations: Record<string, string> = {\n // Generic fallback for property placeholders (applies to ALL properties)\n // This ensures that if no specific placeholder translation exists, an empty string is shown\n // instead of displaying the translation key itself\n 'ly.img.plugin-ai-generation-web.fallback.property.placeholder': '',\n\n // Core generation properties (found in all Input schemas)\n 'ly.img.plugin-ai-generation-web.defaults.property.prompt': 'Prompt',\n 'ly.img.plugin-ai-generation-web.defaults.property.style': 'Style',\n\n // Common properties\n 'ly.img.plugin-ai-generation-web.defaults.property.image_size': 'Image Size',\n 'ly.img.plugin-ai-generation-web.defaults.property.size': 'Image Size',\n 'ly.img.plugin-ai-generation-web.defaults.property.colors': 'Colors',\n 'ly.img.plugin-ai-generation-web.defaults.property.background': 'Background',\n\n // Common dimension properties\n 'ly.img.plugin-ai-generation-web.defaults.property.width': 'Width',\n 'ly.img.plugin-ai-generation-web.defaults.property.height': 'Height',\n 'ly.img.plugin-ai-generation-web.defaults.property.aspect_ratio':\n 'Aspect Ratio',\n\n // Common temporal properties\n 'ly.img.plugin-ai-generation-web.defaults.property.duration': 'Duration',\n 'ly.img.plugin-ai-generation-web.defaults.property.resolution': 'Resolution',\n 'ly.img.plugin-ai-generation-web.defaults.property.generate_audio':\n 'Generate Audio',\n\n // Common audio properties\n 'ly.img.plugin-ai-generation-web.defaults.property.voice_id': 'Voice',\n 'ly.img.plugin-ai-generation-web.defaults.property.speed': 'Speed',\n 'ly.img.plugin-ai-generation-web.defaults.property.text': 'Text',\n 'ly.img.plugin-ai-generation-web.defaults.property.duration_seconds':\n 'Duration (seconds)',\n\n // Custom renderer translations for Recraft providers\n 'ly.img.plugin-ai-generation-web.defaults.property.style.type': 'Type',\n 'ly.img.plugin-ai-generation-web.defaults.property.style.type.image': 'Image',\n 'ly.img.plugin-ai-generation-web.defaults.property.style.type.vector':\n 'Vector',\n 'ly.img.plugin-ai-generation-web.defaults.property.style.type.icon': 'Icon',\n\n // Enum value translations - common formats\n 'ly.img.plugin-ai-generation-web.defaults.property.image_size.square':\n 'Square',\n 'ly.img.plugin-ai-generation-web.defaults.property.image_size.portrait':\n 'Portrait',\n 'ly.img.plugin-ai-generation-web.defaults.property.image_size.landscape':\n 'Landscape',\n\n // Enum value translations - background\n 'ly.img.plugin-ai-generation-web.defaults.property.background.auto': 'Auto',\n 'ly.img.plugin-ai-generation-web.defaults.property.background.transparent':\n 'Transparent',\n\n // Enum value translations - common aspect ratios\n 'ly.img.plugin-ai-generation-web.defaults.property.aspect_ratio.1:1':\n '1:1 (Square)',\n 'ly.img.plugin-ai-generation-web.defaults.property.aspect_ratio.16:9':\n '16:9 (Widescreen)',\n 'ly.img.plugin-ai-generation-web.defaults.property.aspect_ratio.9:16':\n '9:16 (Vertical)',\n 'ly.img.plugin-ai-generation-web.defaults.property.aspect_ratio.4:3': '4:3',\n 'ly.img.plugin-ai-generation-web.defaults.property.aspect_ratio.3:4': '3:4',\n\n // Enum value translations - common resolutions\n 'ly.img.plugin-ai-generation-web.defaults.property.resolution.720p':\n '720p HD',\n 'ly.img.plugin-ai-generation-web.defaults.property.resolution.1080p':\n '1080p Full HD'\n};\n", "import { Property } from './types';\nimport Provider, { Output, OutputKind } from '../core/provider';\nimport { UIOptions } from '../types';\nimport { defaultTranslations } from './defaultTranslations';\n\nfunction formatEnumLabel(enumValue: string): string {\n return (\n enumValue\n // Replace underscores with spaces\n .replace(/_/g, ' ')\n // Handle specific cases first\n .replace(/\\b3d\\b/gi, '3D')\n .replace(/\\b2d\\b/gi, '2D')\n // Capitalize each word\n .replace(/\\b\\w/g, (char) => char.toUpperCase())\n );\n}\n\n/**\n * Extracts translations from OpenAPI schema properties and sets them via cesdk.i18n\n * This includes:\n * - Schema property titles as `ly.img.plugin-ai-${kind}-generation-web.${provider.id}.defaults.property.${property.id}`\n * - Enum value labels as `ly.img.plugin-ai-${kind}-generation-web.${provider.id}.defaults.property.${property.id}.${valueId}`\n * - AnyOf enum value labels with the same pattern\n */\nexport function extractAndSetSchemaTranslations<\n K extends OutputKind,\n I,\n O extends Output\n>(\n properties: Property[],\n provider: Provider<K, I, O>,\n options: UIOptions,\n kind: K\n): void {\n const translations: Record<string, string> = {};\n\n const createTranslationKey = (propertyId: string, valueId?: string): string =>\n `ly.img.plugin-ai-${kind}-generation-web.${\n provider.id\n }.defaults.property.${propertyId}${valueId ? `.${valueId}` : ''}`;\n\n const extractEnumLabels = (schema: any): Record<string, string> =>\n 'x-imgly-enum-labels' in schema &&\n typeof schema['x-imgly-enum-labels'] === 'object'\n ? (schema['x-imgly-enum-labels'] as Record<string, string>)\n : {};\n\n const addEnumTranslations = (\n enumValues: any[],\n propertyId: string,\n enumLabels: Record<string, string>\n ): void => {\n enumValues.forEach((enumValue) => {\n const valueId = String(enumValue);\n const labelValue = enumLabels[valueId] || formatEnumLabel(valueId);\n translations[createTranslationKey(propertyId, valueId)] = labelValue;\n });\n };\n\n properties.forEach((property) => {\n if (property.schema?.title) {\n translations[createTranslationKey(property.id)] = property.schema.title;\n }\n\n if (property.schema?.enum) {\n const enumLabels = extractEnumLabels(property.schema);\n addEnumTranslations(property.schema.enum, property.id, enumLabels);\n }\n\n if (property.schema?.anyOf && Array.isArray(property.schema.anyOf)) {\n const enumLabels = extractEnumLabels(property.schema);\n\n property.schema.anyOf.forEach((anySchema) => {\n const schema = anySchema as any;\n if (schema.enum && Array.isArray(schema.enum)) {\n addEnumTranslations(schema.enum, property.id, enumLabels);\n } else if (schema.$ref) {\n const refName = schema.$ref.split('/').pop();\n if (refName && enumLabels[refName]) {\n translations[createTranslationKey(property.id, refName)] =\n enumLabels[refName];\n }\n } else if (schema.title) {\n const refName = schema.title;\n const labelValue = enumLabels[refName] || formatEnumLabel(refName);\n translations[createTranslationKey(property.id, refName)] = labelValue;\n }\n });\n }\n });\n\n const allTranslations = { ...defaultTranslations, ...translations };\n\n if (Object.keys(allTranslations).length > 0) {\n options.cesdk.i18n.setTranslations({\n en: allTranslations\n });\n }\n}\n", "import { BuilderRenderFunction } from '@cesdk/cesdk-js';\nimport { OutputKind, PanelInputSchema, type Output } from '../../core/provider';\nimport renderGenerationComponents from '../components/renderGenerationComponents';\nimport { InitializationContext } from '../../types';\nimport dereferenceDocument, {\n resolveReference\n} from '../../openapi/dereferenceDocument';\nimport { isOpenAPISchema } from '../../openapi/isOpenAPISchema';\nimport { OpenAPIV3 } from 'openapi-types';\nimport getProperties from '../../openapi/getProperties';\nimport { GetPropertyInput, PropertyInput } from '../../openapi/types';\nimport renderProperty from '../../openapi/renderProperty';\nimport { Generate } from '../../generation/createGenerateFunction';\nimport { extractAndSetSchemaTranslations } from '../../openapi/extractSchemaTranslations';\n\n/**\n * Creates a panel render function based on the schema definition in the provider.\n */\nasync function createPanelRenderFunctionFromSchema<\n K extends OutputKind,\n I,\n O extends Output\n>(\n {\n options,\n provider,\n panelInput,\n config,\n providerConfig\n }: InitializationContext<K, I, O, PanelInputSchema<K, I>>,\n generate: Generate<I, O>\n): Promise<BuilderRenderFunction<any> | undefined> {\n const { id: providerId } = provider;\n\n if (panelInput == null) {\n return undefined;\n }\n\n if (config.debug) {\n // eslint-disable-next-line no-console\n console.log(`Provider: ${providerId} (schema-based)`);\n }\n\n const schemaDocument = dereferenceDocument(panelInput.document);\n const resolvedInputReference = resolveReference(\n schemaDocument,\n panelInput.inputReference\n );\n\n if (!isOpenAPISchema(resolvedInputReference, config.debug)) {\n throw new Error(\n `Input reference '${panelInput.inputReference}' does not resolve to a valid OpenAPI schema`\n );\n }\n\n const inputSchema: OpenAPIV3.SchemaObject = resolvedInputReference;\n const properties = getProperties(inputSchema, panelInput);\n\n // Extract and set translations from schema\n extractAndSetSchemaTranslations(properties, provider, options, provider.kind);\n\n const builderRenderFunction: BuilderRenderFunction<any> = (context) => {\n const { builder } = context;\n\n const getInputs: GetPropertyInput[] = [];\n builder.Section(`${providerId}.schema.section`, {\n children: () => {\n properties.forEach((property) => {\n const getInput = renderProperty(\n context,\n property,\n provider,\n panelInput,\n options,\n config,\n provider.kind,\n providerConfig\n );\n if (getInput != null) {\n if (Array.isArray(getInput)) {\n getInputs.push(...getInput);\n } else {\n getInputs.push(getInput);\n }\n }\n });\n }\n });\n\n const inputs = getInputs.map((getInput) => {\n const input = getInput();\n return input;\n });\n\n const resolveInput = (input: PropertyInput) => {\n if (input.type === 'object') {\n return Object.entries(input.value).reduce((acc, [key, value]) => {\n acc[key] = resolveInput(value);\n\n return acc;\n }, {} as Record<string, any>);\n }\n\n return input.value;\n };\n const input = inputs.reduce((acc, propertyInput) => {\n acc[propertyInput.id] = resolveInput(propertyInput);\n return acc;\n }, {} as Record<string, any>) as I;\n\n renderGenerationComponents(\n context,\n provider,\n generate,\n () => {\n return { input };\n },\n () => {\n return panelInput.getBlockInput(input);\n },\n {\n ...options,\n requiredInputs: inputSchema.required,\n createPlaceholderBlock: panelInput.userFlow === 'placeholder'\n },\n config\n );\n };\n\n return builderRenderFunction;\n}\n\nexport default createPanelRenderFunctionFromSchema;\n", "import { BuilderRenderFunction } from '@cesdk/cesdk-js';\nimport {\n Output,\n OutputKind,\n PanelInputCustom,\n PanelInputSchema\n} from '../../core/provider';\nimport { InitializationContext } from '../../types';\nimport createPanelRenderFunctionFromCustom from './createPanelRenderFunctionFromCustom';\nimport createPanelRenderFunctionFromSchema from './createPanelRenderFunctionFromSchema';\nimport { Generate } from '../../generation/createGenerateFunction';\n\n/**\n * Creates a panel render function based on the provided context, i.e. on the provider.\n */\nasync function createPanelRenderFunction<\n K extends OutputKind,\n I,\n O extends Output\n>(\n context: InitializationContext<K, I, O>,\n generate: Generate<I, O>\n): Promise<BuilderRenderFunction<any> | undefined> {\n if (context.panelInput == null) {\n return undefined;\n }\n\n switch (context.panelInput.type) {\n case 'custom': {\n return createPanelRenderFunctionFromCustom<K, I, O>(\n context as InitializationContext<K, I, O, PanelInputCustom<K, I>>,\n generate\n );\n }\n\n case 'schema': {\n return createPanelRenderFunctionFromSchema<K, I, O>(\n context as InitializationContext<K, I, O, PanelInputSchema<K, I>>,\n generate\n );\n }\n\n default: {\n if (context.config.debug) {\n // eslint-disable-next-line no-console\n console.warn(\n // @ts-ignore\n `Invalid panel input type '${panelInput.type}' - skipping`\n );\n }\n }\n }\n}\n\nexport default createPanelRenderFunction;\n", "import { IndexedDBAssetSource } from '@imgly/plugin-utils';\nimport { OutputKind, Output } from '../core/provider';\nimport { InitializationContext } from '../types';\n\n/**\n * Initializes the history asset source for the given provider.\n */\nfunction initializeHistoryAssetSource<\n K extends OutputKind,\n I,\n O extends Output\n>(context: InitializationContext<K, I, O>): string | undefined {\n const {\n provider,\n options: { engine }\n } = context;\n\n const history = provider.output.history ?? '@imgly/local';\n if (history == null || history === false) return undefined;\n\n const currentAssetSourceIds = engine.asset.findAllSources();\n\n function getUniqueHistoryId(): string {\n let id = `${provider.id}.history`;\n while (currentAssetSourceIds.includes(id)) {\n id += `-${Math.random().toString(36).substring(2, 6)}`;\n }\n return id;\n }\n\n if (history === '@imgly/local') {\n const historyId = getUniqueHistoryId();\n engine.asset.addLocalSource(historyId);\n return historyId;\n }\n if (history === '@imgly/indexedDB') {\n const historyId = getUniqueHistoryId();\n engine.asset.addSource(new IndexedDBAssetSource(historyId, engine));\n return historyId;\n }\n\n return history;\n}\n\nexport default initializeHistoryAssetSource;\n", "import { OutputKind, Output } from '../core/provider';\nimport { InitializationContext } from '../types';\n\n/**\n * Initializes the history asset source for the given provider.\n */\nfunction initializeHistoryAssetLibraryEntry<\n K extends OutputKind,\n I,\n O extends Output\n>(\n context: InitializationContext<K, I, O>,\n historyAssetSourceId?: string\n): string | undefined {\n if (historyAssetSourceId == null || !historyAssetSourceId) return;\n\n const historyAssetLibraryEntryId = `${context.provider.id}.history`;\n\n context.options.cesdk.ui.addAssetLibraryEntry({\n id: historyAssetLibraryEntryId,\n sourceIds: [historyAssetSourceId],\n sortBy: {\n sortKey: 'insertedAt',\n sortingOrder: 'Descending'\n },\n canRemove: true,\n gridItemHeight: 'square',\n gridBackgroundType: 'cover'\n });\n\n return historyAssetLibraryEntryId;\n}\n\nexport default initializeHistoryAssetLibraryEntry;\n", "export const PLUGIN_ICON_SET_ID = '@imgly/plugin-ai-generation';\n\nexport const ICON_SPRITE = `\n<svg>\n <symbol\n fill=\"none\"\n xmlns=\"http://www.w3.org/2000/svg\"\n viewBox=\"0 0 24 24\"\n id=\"@imgly/Sparkle\"\n >\n <path d=\"M5.35545 2.06745C5.24149 1.72556 4.7579 1.72556 4.64394 2.06745L4.05898 3.82232C4.02166 3.93429 3.9338 4.02215 3.82184 4.05948L2.06694 4.64459C1.72506 4.75858 1.72509 5.24217 2.06699 5.3561L3.82179 5.9409C3.93378 5.97822 4.02166 6.06609 4.05899 6.17808L4.64394 7.93291C4.7579 8.2748 5.24149 8.2748 5.35545 7.93291L5.9404 6.17806C5.97773 6.06608 6.06559 5.97821 6.17757 5.94089L7.93242 5.35594C8.27431 5.24198 8.27431 4.75839 7.93242 4.64442L6.17757 4.05947C6.06559 4.02215 5.97773 3.93428 5.9404 3.8223L5.35545 2.06745Z\" fill=\"currentColor\"/>\n<path d=\"M17.9632 3.23614C17.8026 2.80788 17.1968 2.80788 17.0362 3.23614L16.0787 5.78951C16.0285 5.92337 15.9229 6.02899 15.789 6.07918L13.2356 7.0367C12.8074 7.19729 12.8074 7.80307 13.2356 7.96366L15.789 8.92118C15.9229 8.97138 16.0285 9.077 16.0787 9.21085L17.0362 11.7642C17.1968 12.1925 17.8026 12.1925 17.9632 11.7642L18.9207 9.21086C18.9709 9.077 19.0765 8.97138 19.2104 8.92118L21.7637 7.96366C22.192 7.80307 22.192 7.1973 21.7637 7.0367L19.2104 6.07918C19.0765 6.02899 18.9709 5.92337 18.9207 5.78951L17.9632 3.23614Z\" fill=\"currentColor\"/>\n<path d=\"M9.30058 7.82012C9.54712 7.1791 10.454 7.1791 10.7006 7.82012L12.3809 12.189C12.4571 12.3871 12.6136 12.5436 12.8117 12.6198L17.1806 14.3001C17.8216 14.5466 17.8216 15.4536 17.1806 15.7001L12.8117 17.3804C12.6136 17.4566 12.4571 17.6131 12.3809 17.8112L10.7006 22.1801C10.454 22.8211 9.54712 22.8211 9.30058 22.1801L7.62024 17.8112C7.54406 17.6131 7.38754 17.4566 7.18947 17.3804L2.82061 15.7001C2.17959 15.4536 2.17959 14.5466 2.82061 14.3001L7.18947 12.6198C7.38754 12.5436 7.54406 12.3871 7.62024 12.189L9.30058 7.82012Z\" fill=\"currentColor\"/>\n\n </symbol>\n\n <symbol\n fill=\"none\"\n xmlns=\"http://www.w3.org/2000/svg\"\n viewBox=\"0 0 24 24\"\n id=\"${PLUGIN_ICON_SET_ID}/image\"\n >\n <path d=\"M3 16.5V18C3 19.6569 4.34315 21 6 21H18C19.6569 21 21 19.6569 21 18V6C21 4.34315 19.6569 3 18 3L17.999 5C18.5513 5 19 5.44772 19 6V18C19 18.5523 18.5523 19 18 19H6C5.44772 19 5 18.5523 5 18V16.5H3Z\" fill=\"currentColor\"/>\n<path d=\"M13.0982 0.884877C12.9734 0.568323 12.5254 0.568322 12.4005 0.884876L11.7485 2.53819C11.7104 2.63483 11.6339 2.71134 11.5372 2.74945L9.8839 3.40151C9.56735 3.52636 9.56734 3.97436 9.8839 4.09921L11.5372 4.75126C11.6339 4.78938 11.7104 4.86588 11.7485 4.96253L12.4005 6.61584C12.5254 6.93239 12.9734 6.9324 13.0982 6.61584L13.7503 4.96253C13.7884 4.86588 13.8649 4.78938 13.9616 4.75126L15.6149 4.09921C15.9314 3.97436 15.9314 3.52636 15.6149 3.40151L13.9616 2.74945C13.8649 2.71134 13.7884 2.63483 13.7503 2.53819L13.0982 0.884877Z\" fill=\"currentColor\"/>\n<path d=\"M6.40053 5.38488C6.52538 5.06832 6.97338 5.06832 7.09823 5.38488L8.17455 8.11392C8.21267 8.21057 8.28917 8.28707 8.38582 8.32519L11.1149 9.40151C11.4314 9.52636 11.4314 9.97436 11.1149 10.0992L8.38582 11.1755C8.28917 11.2136 8.21267 11.2901 8.17455 11.3868L7.09823 14.1158C6.97338 14.4324 6.52538 14.4324 6.40053 14.1158L5.32421 11.3868C5.2861 11.2901 5.20959 11.2136 5.11295 11.1755L2.3839 10.0992C2.06735 9.97436 2.06735 9.52636 2.3839 9.40151L5.11295 8.32519C5.20959 8.28707 5.2861 8.21057 5.32421 8.11392L6.40053 5.38488Z\" fill=\"currentColor\"/>\n<path d=\"M18.9994 16.5008V18.0004C18.9994 18.5526 18.5517 19.0004 17.9994 19.0004H9.33302L14.3753 11.4369C14.6722 10.9916 15.3266 10.9916 15.6234 11.4369L18.9994 16.5008Z\" fill=\"currentColor\"/>\n\n </symbol>\n <symbol\n fill=\"none\"\n xmlns=\"http://www.w3.org/2000/svg\"\n viewBox=\"0 0 24 24\"\n id=\"${PLUGIN_ICON_SET_ID}/video\"\n >\n<path d=\"M6 3C4.34315 3 3 4.34315 3 6V18C3 19.6569 4.34315 21 6 21H18C19.6569 21 21 19.6569 21 18V16.5H19V18C19 18.5523 18.5523 19 18 19H6C5.44772 19 5 18.5523 5 18V6C5 5.44772 5.44772 5 6 5V3Z\" fill=\"currentColor\"/>\n<path d=\"M10.9025 0.8839C11.0273 0.567345 11.4753 0.567346 11.6002 0.883901L12.2522 2.53721C12.2904 2.63386 12.3669 2.71036 12.4635 2.74848L14.1168 3.40053C14.4334 3.52538 14.4334 3.97338 14.1168 4.09823L12.4635 4.75029C12.3669 4.7884 12.2904 4.86491 12.2522 4.96155L11.6002 6.61486C11.4753 6.93142 11.0273 6.93142 10.9025 6.61486L10.2504 4.96155C10.2123 4.86491 10.1358 4.7884 10.0392 4.75029L8.38585 4.09823C8.0693 3.97338 8.0693 3.52538 8.38585 3.40053L10.0392 2.74848C10.1358 2.71036 10.2123 2.63386 10.2504 2.53721L10.9025 0.8839Z\" fill=\"currentColor\"/>\n<path d=\"M18.9019 3.3845C19.0267 3.06795 19.4747 3.06795 19.5996 3.3845L20.6759 6.11355C20.714 6.2102 20.7905 6.2867 20.8872 6.32482L23.6162 7.40114C23.9328 7.52598 23.9328 7.97399 23.6162 8.09883L20.8872 9.17515C20.7905 9.21327 20.714 9.28977 20.6759 9.38642L19.5996 12.1155C19.4747 12.432 19.0267 12.432 18.9019 12.1155L17.8255 9.38642C17.7874 9.28977 17.7109 9.21327 17.6143 9.17515L14.8852 8.09883C14.5687 7.97399 14.5687 7.52598 14.8852 7.40114L17.6143 6.32482C17.7109 6.2867 17.7874 6.2102 17.8255 6.11355L18.9019 3.3845Z\" fill=\"currentColor\"/>\n<path d=\"M14.9994 13.2862C15.5089 12.8859 15.5089 12.1141 14.9995 11.7137L10.618 8.27047C9.96188 7.75485 9.00011 8.22225 9.00011 9.05673L9.00011 15.9429C9.00011 16.7773 9.96185 17.2448 10.618 16.7292L14.9994 13.2862Z\" fill=\"currentColor\"/>\n </symbol>\n <symbol\n fill=\"none\"\n xmlns=\"http://www.w3.org/2000/svg\"\n viewBox=\"0 0 24 24\"\n id=\"${PLUGIN_ICON_SET_ID}/audio\"\n >\n <path d=\"M6 3.80273C4.2066 4.84016 3 6.77919 3 9.00004V12.8153C3 15.931 5.39501 18.4873 8.44444 18.7436V20.9645C8.44444 22.2198 9.89427 22.9198 10.8773 22.1392L15.1265 18.7647H15.5C17.8285 18.7647 19.8472 17.4384 20.8417 15.5H18.4187C17.6889 16.2784 16.6512 16.7647 15.5 16.7647H14.9522C14.6134 16.7647 14.2846 16.8794 14.0193 17.0901L10.4444 19.929V18.2597C10.4444 17.4341 9.77513 16.7647 8.9495 16.7647C7.80494 16.7647 6.77409 16.2779 6.05276 15.5H6V15.4419C5.37798 14.7439 5 13.8237 5 12.8153V9.00004C5 7.98559 5.37764 7.05935 6 6.35422V3.80273Z\" fill=\"currentColor\"/>\n<path d=\"M11.6002 1.8839C11.4753 1.56735 11.0273 1.56735 10.9025 1.8839L10.2504 3.53721C10.2123 3.63386 10.1358 3.71036 10.0392 3.74848L8.38585 4.40053C8.0693 4.52538 8.0693 4.97338 8.38585 5.09823L10.0392 5.75029C10.1358 5.7884 10.2123 5.86491 10.2504 5.96155L10.9025 7.61486C11.0273 7.93142 11.4753 7.93142 11.6002 7.61486L12.2522 5.96155C12.2904 5.86491 12.3669 5.7884 12.4635 5.75029L14.1168 5.09823C14.4334 4.97338 14.4334 4.52538 14.1168 4.40053L12.4635 3.74848C12.3669 3.71036 12.2904 3.63386 12.2522 3.53721L11.6002 1.8839Z\" fill=\"currentColor\"/>\n<path d=\"M19.5996 4.3845C19.4747 4.06795 19.0267 4.06795 18.9019 4.3845L17.8255 7.11355C17.7874 7.2102 17.7109 7.2867 17.6143 7.32482L14.8852 8.40114C14.5687 8.52598 14.5687 8.97399 14.8852 9.09883L17.6143 10.1752C17.7109 10.2133 17.7874 10.2898 17.8255 10.3864L18.9019 13.1155C19.0267 13.432 19.4747 13.432 19.5996 13.1155L20.6759 10.3864C20.714 10.2898 20.7905 10.2133 20.8872 10.1752L23.6162 9.09883C23.9328 8.97399 23.9328 8.52598 23.6162 8.40114L20.8872 7.32482C20.7905 7.2867 20.714 7.2102 20.6759 7.11355L19.5996 4.3845Z\" fill=\"currentColor\"/>\n </symbol>\n <symbol\n fill=\"none\"\n xmlns=\"http://www.w3.org/2000/svg\"\n viewBox=\"0 0 24 24\"\n id=\"@imgly/MixingPlate\"\n >\n <path d=\"M9.75 9C10.5784 9 11.25 8.32843 11.25 7.5C11.25 6.67157 10.5784 6 9.75 6C8.92157 6 8.25 6.67157 8.25 7.5C8.25 8.32843 8.92157 9 9.75 9Z\" fill=\"currentColor\"/>\n<path d=\"M7 13C7.82843 13 8.5 12.3284 8.5 11.5C8.5 10.6716 7.82843 10 7 10C6.17157 10 5.5 10.6716 5.5 11.5C5.5 12.3284 6.17157 13 7 13Z\" fill=\"currentColor\"/>\n<path d=\"M15.75 7.5C15.75 8.32843 15.0784 9 14.25 9C13.4216 9 12.75 8.32843 12.75 7.5C12.75 6.67157 13.4216 6 14.25 6C15.0784 6 15.75 6.67157 15.75 7.5Z\" fill=\"currentColor\"/>\n<path d=\"M17 13C17.8284 13 18.5 12.3284 18.5 11.5C18.5 10.6716 17.8284 10 17 10C16.1716 10 15.5 10.6716 15.5 11.5C15.5 12.3284 16.1716 13 17 13Z\" fill=\"currentColor\"/>\n<path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M8.26309 2.77709C10.6681 1.77921 13.4829 1.7322 15.9209 2.64297C18.1572 3.47923 20.0876 5.09285 21.1766 7.28598C22.3395 9.62772 22.4889 13.1077 20.3864 15.2982C19.2693 16.4621 17.7657 16.9982 16.0026 16.9997C15.7897 16.9997 15.5555 16.9864 15.3497 16.9745C15.309 16.9722 15.2694 16.9699 15.2313 16.9679C14.9817 16.9542 14.761 16.9455 14.5569 16.9539C14.124 16.9718 13.9598 17.0612 13.89 17.1324C13.718 17.3081 13.6946 17.6672 13.8854 17.8895C14.2899 18.3608 14.5016 18.9277 14.5016 19.5497C14.5016 20.2206 14.3086 20.9011 13.7542 21.3896C13.2471 21.837 12.6082 21.9997 11.9635 21.9997C10.6049 21.9997 9.31155 21.7367 8.0934 21.2067C6.89058 20.6831 5.84501 19.9687 4.94363 19.0666C4.04281 18.1651 3.31836 17.107 2.79369 15.8978C1.72761 13.4409 1.72662 10.5261 2.81247 8.07034C3.88024 5.65548 5.84206 3.78161 8.26309 2.77709ZM15.2207 4.51639C13.2556 3.78239 10.9651 3.82132 9.02956 4.62439C7.06888 5.43791 5.49559 6.94785 4.64163 8.87914C3.78373 10.8194 3.78253 13.1522 4.62841 15.1017C5.05312 16.0805 5.63511 16.9291 6.35838 17.6529C7.08102 18.3761 7.91671 18.9484 8.89123 19.3728C9.8492 19.7895 10.87 19.9997 11.9635 19.9997C12.2815 19.9997 12.394 19.9225 12.431 19.8899L12.4319 19.8891C12.4367 19.8849 12.4487 19.8743 12.4631 19.8359C12.4799 19.7911 12.5016 19.7024 12.5016 19.5497C12.5016 19.4091 12.4633 19.3034 12.3677 19.192C11.5353 18.222 11.5272 16.6868 12.4611 15.7331C13.0741 15.1071 13.8844 14.98 14.4745 14.9556C14.7819 14.943 15.085 14.9568 15.3409 14.9709C15.3906 14.9736 15.4379 14.9763 15.4832 14.9788C15.6876 14.9904 15.8508 14.9997 16.0009 14.9997C17.3405 14.9986 18.2792 14.6054 18.9435 13.9133C20.2633 12.5382 20.3186 10.055 19.3853 8.1755C18.5436 6.48051 17.0293 5.19281 15.2207 4.51639Z\" fill=\"currentColor\"/>\n\n </symbol>\n</svg>\n`;\n\nexport default ICON_SPRITE;\n", "/* eslint-disable no-console */\nimport { getImageDimensionsFromURL, getImageUri } from '@imgly/plugin-utils';\nimport {\n type Output,\n type OutputKind,\n type GetBlockInputResult,\n VideoOutput,\n ImageOutput,\n TextOutput,\n AudioOutput,\n GenerationOptions,\n GenerationResult\n} from '../core/provider';\nimport { Middleware } from './middleware';\n\ninterface DryRunOptions<K extends OutputKind> {\n enable?: boolean;\n kind: K;\n\n // Is only defined with generation from a panel where we create a complete new block\n blockInputs?: GetBlockInputResult<K>;\n\n // Is only defined with quick action generation on a given block(s).\n blockIds?: number[];\n}\n\nfunction dryRunMiddleware<I, K extends OutputKind, O extends Output>(\n options: DryRunOptions<K>\n) {\n const middleware: Middleware<I, O> = async (\n generationInput,\n generationOptions,\n next\n ) => {\n if (!options.enable) {\n return next(generationInput, generationOptions);\n }\n console.log(\n `[DRY RUN]: Requesting dummy AI generation for kind '${options.kind}' with inputs: `,\n JSON.stringify(generationInput, undefined, 2)\n );\n await wait(2000);\n const output = await getDryRunOutput(\n generationInput,\n options,\n generationOptions\n );\n return output as O;\n };\n\n return middleware;\n}\n\nasync function getDryRunOutput<K extends OutputKind, I>(\n generationInput: I,\n options: DryRunOptions<K>,\n generationOptions: GenerationOptions\n): Promise<GenerationResult<Output>> {\n switch (options.kind) {\n case 'image': {\n return getImageDryRunOutput(\n generationInput,\n options as DryRunOptions<'image'>,\n generationOptions\n );\n }\n case 'video': {\n return getVideoDryRunOutput(\n generationInput,\n options as DryRunOptions<'video'>,\n generationOptions\n );\n }\n case 'text': {\n return getTextDryRunOutput(\n generationInput,\n options as DryRunOptions<'text'>,\n generationOptions\n );\n }\n case 'audio': {\n return getAudioDryRunOutput(\n generationInput,\n options as DryRunOptions<'audio'>,\n generationOptions\n );\n }\n\n default: {\n throw new Error(\n `Unsupported output kind for creating dry run output: ${options.kind}`\n );\n }\n }\n}\n\nasync function getImageDryRunOutput<I>(\n generationInput: I,\n options: DryRunOptions<'image'>,\n { engine }: GenerationOptions\n): Promise<ImageOutput> {\n let width;\n let height;\n\n const prompt: string =\n generationInput != null &&\n typeof generationInput === 'object' &&\n 'prompt' in generationInput &&\n typeof generationInput.prompt === 'string'\n ? generationInput.prompt\n : 'AI Generated Image';\n\n // If prompt includes something that looks like a dimension\n // e.g. 512x512, 1024x768, etc. than we will use this as the\n // output image size for testing purposes.\n const promptDimension = prompt.match(/(\\d+)x(\\d+)/);\n if (promptDimension != null) {\n width = parseInt(promptDimension[1], 10);\n height = parseInt(promptDimension[2], 10);\n } else {\n if (options.blockInputs != null) {\n width = options.blockInputs.image.width;\n height = options.blockInputs.image.height;\n }\n if (\n options.blockIds != null &&\n Array.isArray(options.blockIds) &&\n options.blockIds.length > 0\n ) {\n const [blockId] = options.blockIds;\n const url = await getImageUri(blockId, engine);\n const dimension = await getImageDimensionsFromURL(url, engine);\n width = dimension.width;\n height = dimension.height;\n } else {\n width = 512;\n height = 512;\n }\n }\n\n const url = `https://placehold.co/${width}x${height}/000000/FFF?text=${prompt\n .replace(' ', '+')\n .replace('\\n', '+')}`;\n\n return {\n kind: 'image',\n url\n };\n}\n\nasync function getVideoDryRunOutput<I>(\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n _generationInput: I,\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n _options: DryRunOptions<'video'>,\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n _generationOptions: GenerationOptions\n): Promise<VideoOutput> {\n return Promise.resolve({\n kind: 'video',\n url: 'https://storage.googleapis.com/gtv-videos-bucket/sample/ForBiggerBlazes.mp4'\n });\n}\n\nasync function getTextDryRunOutput<I>(\n generationInput: I,\n options: DryRunOptions<'text'>,\n generationOptions: GenerationOptions\n): Promise<AsyncGenerator<TextOutput>> {\n // Extract original text from the blocks or prompt\n let originalText = '';\n\n // Try to get original text from blocks first\n if (options.blockIds && options.blockIds.length > 0) {\n const [blockId] = options.blockIds;\n if (generationOptions.engine.block.isValid(blockId)) {\n originalText = generationOptions.engine.block.getString(\n blockId,\n 'text/text'\n );\n }\n }\n\n // If no original text from blocks, try to extract from prompt\n if (!originalText) {\n if (\n generationInput != null &&\n typeof generationInput === 'object' &&\n 'prompt' in generationInput &&\n typeof generationInput.prompt === 'string'\n ) {\n // Try to extract original text from prompt patterns\n const promptStr = generationInput.prompt;\n const textMatch =\n promptStr.match(/text:\\s*\"([^\"]+)\"/i) ||\n promptStr.match(/content:\\s*\"([^\"]+)\"/i) ||\n promptStr.match(/\"([^\"]+)\"/);\n if (textMatch && textMatch[1]) {\n originalText = textMatch[1];\n }\n }\n }\n\n // Generate dummy text with similar length\n const targetLength = originalText.length || 50; // Default to 50 chars if no original text\n let dryRunText = '';\n\n // Analyze input for specific text generation types and create appropriate dummy text\n if (generationInput != null && typeof generationInput === 'object') {\n if (\n 'language' in generationInput &&\n typeof generationInput.language === 'string'\n ) {\n dryRunText = generateDummyText(targetLength, 'translation');\n } else if (\n 'type' in generationInput &&\n typeof generationInput.type === 'string'\n ) {\n const tone = generationInput.type;\n dryRunText = generateDummyText(targetLength, tone);\n } else if ('customPrompt' in generationInput) {\n dryRunText = generateDummyText(targetLength, 'custom');\n } else {\n dryRunText = generateDummyText(targetLength, 'improved');\n }\n } else {\n dryRunText = generateDummyText(targetLength, 'generated');\n }\n\n // Return an async generator that streams the text in chunks\n return createStreamingTextGenerator(\n dryRunText,\n generationOptions.abortSignal\n );\n}\n\nasync function* createStreamingTextGenerator(\n finalText: string,\n abortSignal?: AbortSignal\n): AsyncGenerator<TextOutput> {\n const chunkSize = Math.max(1, Math.ceil(finalText.length / 20)); // Split into ~20 chunks\n\n // Generate and yield text progressively\n let currentLength = 0;\n\n while (currentLength < finalText.length) {\n if (abortSignal?.aborted) {\n return;\n }\n\n // Calculate next chunk end\n const nextLength = Math.min(currentLength + chunkSize, finalText.length);\n const currentText = finalText.substring(0, nextLength);\n\n yield {\n kind: 'text',\n text: currentText\n };\n\n currentLength = nextLength;\n\n // Only add delay if there are more chunks to come\n if (currentLength < finalText.length) {\n // eslint-disable-next-line no-await-in-loop\n await wait(100);\n }\n }\n\n // Return the final complete text\n return {\n kind: 'text',\n text: finalText\n };\n}\n\nfunction generateDummyText(targetLength: number, style: string): string {\n const prefix = '[DRY RUN - Dummy Text] ';\n const prefixLength = prefix.length;\n\n // If target length is shorter than the prefix, just return truncated prefix\n if (targetLength <= prefixLength) {\n return prefix.substring(0, targetLength);\n }\n\n const remainingLength = targetLength - prefixLength;\n\n const baseTexts = {\n translation:\n 'Ceci est un texte fictif traduit qui maintient la longueur approximative.',\n professional:\n 'Enhanced professional content with improved clarity and structure.',\n casual: 'Relaxed, friendly text that keeps things simple and approachable.',\n formal: 'Refined formal documentation that preserves original structure.',\n humorous: 'Amusing content that brings lighthearted fun to the text.',\n improved: 'Enhanced text that demonstrates better clarity and readability.',\n custom: 'Customized content reflecting the requested modifications.',\n generated: 'AI-generated content maintaining original length and structure.'\n };\n\n const baseText =\n baseTexts[style as keyof typeof baseTexts] || baseTexts.generated;\n\n let contentText = '';\n if (remainingLength <= baseText.length) {\n contentText = baseText.substring(0, remainingLength);\n } else {\n // For longer text, repeat and vary the content\n contentText = baseText;\n const variations = [\n ' Additional content continues with similar phrasing.',\n ' Further elaboration maintains the established tone.',\n ' Extended content preserves the original style.',\n ' Continued text follows the same pattern.'\n ];\n\n let variationIndex = 0;\n while (contentText.length < remainingLength) {\n const nextVariation = variations[variationIndex % variations.length];\n if (contentText.length + nextVariation.length <= remainingLength) {\n contentText += nextVariation;\n } else {\n contentText += nextVariation.substring(\n 0,\n remainingLength - contentText.length\n );\n break;\n }\n variationIndex++;\n }\n }\n\n return prefix + contentText;\n}\n\nasync function getAudioDryRunOutput<I>(\n generationInput: I,\n options: DryRunOptions<'audio'>,\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n generationOptions: GenerationOptions\n): Promise<AudioOutput> {\n // Extract duration from generation input or use default\n let duration = 3; // Default to 3 seconds\n\n // Try to extract duration from input\n if (generationInput != null && typeof generationInput === 'object') {\n if (\n 'duration' in generationInput &&\n typeof generationInput.duration === 'number'\n ) {\n duration = generationInput.duration;\n } else if (\n 'prompt' in generationInput &&\n typeof generationInput.prompt === 'string'\n ) {\n // Try to extract duration from prompt (e.g., \"5 seconds\", \"10s\", etc.)\n const promptStr = generationInput.prompt;\n const durationMatch = promptStr.match(/(\\d+)\\s*(?:seconds?|secs?|s)\\b/i);\n if (durationMatch) {\n duration = parseInt(durationMatch[1], 10);\n }\n }\n }\n\n // Try to get duration from block inputs\n if (options.blockInputs?.audio?.duration) {\n duration = options.blockInputs.audio.duration;\n }\n\n // Generate tone audio data\n const audioUrl = generateTone(220, duration); // 220Hz tone (A3 - deeper, more pleasant)\n\n return {\n kind: 'audio',\n url: audioUrl,\n duration,\n thumbnailUrl: undefined\n };\n}\n\nfunction generateTone(frequency: number, duration: number): string {\n const sampleRate = 44100;\n const numSamples = Math.floor(sampleRate * duration);\n\n // Generate sine wave PCM data\n const pcmData = new Float32Array(numSamples);\n for (let i = 0; i < numSamples; i++) {\n pcmData[i] = Math.sin((2 * Math.PI * frequency * i) / sampleRate);\n }\n\n return createWAVDataURI(pcmData, sampleRate);\n}\n\nfunction createWAVDataURI(pcmData: Float32Array, sampleRate: number): string {\n const numSamples = pcmData.length;\n const bytesPerSample = 2; // 16-bit\n const numChannels = 1; // Mono\n const blockAlign = numChannels * bytesPerSample;\n const byteRate = sampleRate * blockAlign;\n const dataSize = numSamples * bytesPerSample;\n const fileSize = 44 + dataSize; // 44 bytes for WAV header\n\n // Create WAV file buffer\n const buffer = new ArrayBuffer(fileSize);\n const view = new DataView(buffer);\n\n // WAV header\n const writeString = (offset: number, str: string) => {\n for (let i = 0; i < str.length; i++) {\n view.setUint8(offset + i, str.charCodeAt(i));\n }\n };\n\n writeString(0, 'RIFF');\n view.setUint32(4, fileSize - 8, true);\n writeString(8, 'WAVE');\n writeString(12, 'fmt ');\n view.setUint32(16, 16, true); // PCM chunk size\n view.setUint16(20, 1, true); // PCM format\n view.setUint16(22, numChannels, true);\n view.setUint32(24, sampleRate, true);\n view.setUint32(28, byteRate, true);\n view.setUint16(32, blockAlign, true);\n view.setUint16(34, 16, true); // Bits per sample\n writeString(36, 'data');\n view.setUint32(40, dataSize, true);\n\n // Convert float32 PCM to int16 and write to buffer\n let offset = 44;\n for (let i = 0; i < numSamples; i++) {\n const sample = Math.max(-1, Math.min(1, pcmData[i]));\n const intSample = Math.round(sample * 32767);\n view.setInt16(offset, intSample, true);\n offset += 2;\n }\n\n // Convert to base64\n const uint8Array = new Uint8Array(buffer);\n let binaryString = '';\n for (let i = 0; i < uint8Array.length; i++) {\n binaryString += String.fromCharCode(uint8Array[i]);\n }\n const base64 = btoa(binaryString);\n\n return `data:audio/wav;base64,${base64}`;\n}\n\nasync function wait(ms: number) {\n return new Promise((resolve) => {\n setTimeout(resolve, ms);\n });\n}\n\nexport default dryRunMiddleware;\n", "export const ABORT_REASON_USER_CANCEL = 'USER_CANCEL';\n", "import Provider, {\n GenerationOptions,\n Output,\n OutputKind\n} from '../core/provider';\nimport { composeMiddlewares, Middleware } from '../middleware/middleware';\nimport loggingMiddleware from '../middleware/loggingMiddleware';\nimport dryRunMiddleware from '../middleware/dryRunMiddleware';\nimport CreativeEditorSDK, { CreativeEngine } from '@cesdk/cesdk-js';\nimport { isAbortError, isAsyncGenerator } from '../utils/utils';\nimport { ABORT_REASON_USER_CANCEL } from '../core/constants';\n\nexport type ResultSuccess<O> =\n | {\n status: 'success';\n type: 'async';\n output: AsyncGenerator<O>;\n middlewareOptions?: GenerationOptions;\n }\n | {\n status: 'success';\n type: 'sync';\n output: O;\n middlewareOptions?: GenerationOptions;\n };\n\nexport type Result<O> =\n | ResultSuccess<O>\n | { status: 'error'; message: string; middlewareOptions?: GenerationOptions }\n | { status: 'aborted'; middlewareOptions?: GenerationOptions };\n\nexport type Generate<I, O extends Output> = (\n input: I,\n options?: {\n /**\n * The block IDs that this generation is operating on.\n * - undefined: Middleware will fall back to selected blocks\n * - []: Explicitly target no blocks\n * - [1, 2, 3]: Target specific blocks (e.g., placeholder block)\n */\n blockIds?: number[];\n abortSignal?: AbortSignal;\n middlewares?: Middleware<I, O>[];\n debug?: boolean;\n dryRun?: boolean;\n }\n) => Promise<Result<O>>;\n\nfunction createGenerateFunction<\n K extends OutputKind,\n I,\n O extends Output\n>(context: {\n provider: Provider<K, I, O>;\n cesdk: CreativeEditorSDK;\n engine: CreativeEngine;\n}): Generate<I, O> {\n return async (input: I, options) => {\n if (options?.abortSignal?.aborted) return { status: 'aborted' };\n\n const composedMiddlewares = composeMiddlewares<I, O>([\n ...(context.provider.output.middleware ?? []),\n ...(options?.middlewares ?? []),\n loggingMiddleware({ enable: options?.debug }),\n dryRunMiddleware({\n enable: options?.dryRun,\n kind: context.provider.kind\n })\n ]);\n\n // Create middleware options with preventDefault implementation\n // Using closure instead of 'this' to ensure state is shared across middleware chain\n const preventDefaultState = { prevented: false };\n const middlewareOptions: GenerationOptions = {\n blockIds: options?.blockIds,\n abortSignal: options?.abortSignal,\n engine: context.engine,\n cesdk: context.cesdk,\n preventDefault: () => {\n preventDefaultState.prevented = true;\n },\n defaultPrevented: () => {\n return preventDefaultState.prevented;\n }\n };\n\n // Trigger the generation\n try {\n const { result: output } = await composedMiddlewares(\n context.provider.output.generate\n )(input, middlewareOptions);\n if (options?.abortSignal?.aborted)\n return { status: 'aborted', middlewareOptions };\n if (output instanceof Error)\n return {\n status: 'error',\n message: output.message,\n middlewareOptions\n };\n if (output == null)\n return {\n status: 'error',\n message: 'No output generated',\n middlewareOptions\n };\n\n if (isAsyncGenerator(output)) {\n return { status: 'success', type: 'async', output, middlewareOptions };\n } else {\n return { status: 'success', type: 'sync', output, middlewareOptions };\n }\n } catch (error) {\n if (isAbortError(error)) {\n return {\n status: 'aborted',\n message: error.message,\n middlewareOptions\n };\n }\n if (error === ABORT_REASON_USER_CANCEL) {\n return { status: 'aborted', message: error, middlewareOptions };\n }\n return {\n status: 'error',\n message: error instanceof Error ? error.message : String(error),\n middlewareOptions\n };\n }\n };\n}\n\nexport default createGenerateFunction;\n", "import CreativeEditorSDK, { BuilderRenderFunction } from '@cesdk/cesdk-js';\nimport Provider, { Output, OutputKind } from '../core/provider';\nimport createPanelRenderFunction from '../ui/panels/createPanelRenderFunction';\nimport {\n CommonPluginConfiguration,\n InternalPluginConfiguration,\n InitializationContext\n} from '../types';\nimport initializeHistoryAssetSource from '../assets/initializeHistoryAssetSource';\nimport initializeHistoryAssetLibraryEntry from '../assets/initializeHistoryAssetLibraryEntry';\nimport icons from '../ui/icons';\nimport createGenerateFunction, {\n type Generate\n} from '../generation/createGenerateFunction';\nimport { ProviderRegistry } from '../core/ProviderRegistry';\nimport { ActionRegistry } from '../core/ActionRegistry';\nimport { addIconSetOnce } from '../utils/utils';\n\nexport type ProviderInitializationResult<\n K extends OutputKind,\n I,\n O extends Output\n> = {\n provider: Provider<K, I, O>;\n\n panel: {\n builderRenderFunction?: BuilderRenderFunction;\n };\n\n history: {\n assetSourceId?: string;\n assetLibraryEntryId?: string;\n };\n\n generate: Generate<I, O>;\n};\n\n/**\n * Initializes a provider with the given configuration and options.\n */\nasync function initializeProvider<K extends OutputKind, I, O extends Output>(\n _kind: K,\n provider: Provider<K, I, O>,\n options: {\n cesdk: CreativeEditorSDK;\n },\n config: CommonPluginConfiguration<K, I, O>\n): Promise<ProviderInitializationResult<K, I, O>> {\n // Create internal config with provider\n const internalConfig: InternalPluginConfiguration<K, I, O> = {\n ...config,\n provider\n };\n\n const context: InitializationContext<K, I, O> = {\n provider,\n panelInput: provider.input?.panel,\n options: {\n cesdk: options.cesdk,\n engine: options.cesdk.engine\n },\n config: internalConfig,\n providerConfig: provider.configuration\n };\n\n await provider.initialize?.({ ...options, engine: options.cesdk.engine });\n\n // Enable features for custom quick actions defined by this provider\n // Only enable features for quick actions that are not already known/registered by plugins\n if (provider.input?.quickActions?.supported) {\n const kind = provider.kind;\n const actionRegistry = ActionRegistry.get();\n\n Object.keys(provider.input.quickActions.supported).forEach(\n (quickActionId) => {\n // Check if this is a custom quick action (not already registered by a plugin)\n const existingAction = actionRegistry.getBy({\n id: quickActionId,\n type: 'quick'\n })[0];\n if (!existingAction) {\n // This is a custom quick action from the provider, enable its feature\n const featureId = `ly.img.plugin-ai-${kind}-generation-web.quickAction.${quickActionId}`;\n options.cesdk.feature.enable(featureId, true);\n }\n }\n );\n }\n\n const historyAssetSourceId = initializeHistoryAssetSource(context);\n const historyAssetLibraryEntryId = initializeHistoryAssetLibraryEntry(\n context,\n historyAssetSourceId\n );\n\n context.options.historyAssetSourceId = historyAssetSourceId;\n context.options.historyAssetLibraryEntryId = historyAssetLibraryEntryId;\n\n const generate = createGenerateFunction({\n provider,\n cesdk: options.cesdk,\n engine: options.cesdk.engine\n });\n\n const builderRenderFunction: BuilderRenderFunction | undefined =\n await createPanelRenderFunction(context, generate);\n\n addIconSetOnce(options.cesdk, '@imgly/plugin-ai-generation', icons);\n\n const providerInitializationResult: ProviderInitializationResult<K, I, O> = {\n provider,\n panel: {\n builderRenderFunction\n },\n history: {\n assetSourceId: historyAssetSourceId,\n assetLibraryEntryId: historyAssetLibraryEntryId\n },\n generate\n };\n\n ProviderRegistry.get().register(providerInitializationResult);\n\n return providerInitializationResult;\n}\n\nexport default initializeProvider;\n", "import { AggregatedAssetSource } from '@imgly/plugin-utils';\nimport { OutputKind } from '../core/provider';\nimport CreativeEditorSDK from '@cesdk/cesdk-js';\n\n/**\n * Initializes the combined history asset source for the given asset sources.\n */\nfunction initializeHistoryCompositeAssetSource<K extends OutputKind>(options: {\n kind: K;\n cesdk: CreativeEditorSDK;\n historAssetSourceIds: string[];\n}): string | undefined {\n const { kind, cesdk, historAssetSourceIds } = options;\n const compositeAssetSourceId = `ly.img.ai.${kind}-generation.history`;\n\n if (cesdk.engine.asset.findAllSources().includes(compositeAssetSourceId)) {\n return compositeAssetSourceId;\n }\n\n const aggregatedImageAssetSource = new AggregatedAssetSource(\n compositeAssetSourceId,\n cesdk,\n historAssetSourceIds\n );\n cesdk.engine.asset.addSource(aggregatedImageAssetSource);\n\n return aggregatedImageAssetSource.id;\n}\n\nexport default initializeHistoryCompositeAssetSource;\n", "import CreativeEditorSDK, {\n BuilderRenderFunction,\n BuilderRenderFunctionContext,\n SelectValue\n} from '@cesdk/cesdk-js';\nimport Provider, { Output, OutputKind } from '../core/provider';\nimport initializeProvider, {\n ProviderInitializationResult\n} from './initializeProvider';\nimport { isGeneratingStateKey } from '../ui/components/renderGenerationComponents';\nimport { CommonPluginConfiguration } from '../types';\nimport initializeHistoryCompositeAssetSource from '../assets/initializeHistoryCompositeAssetSource';\nimport { isDefined } from '@imgly/plugin-utils';\n\nfunction createLabelArray<K extends OutputKind>(\n kind: K,\n key: string\n): string[] {\n return [\n `ly.img.plugin-ai-${kind}-generation-web.${key}`,\n `ly.img.plugin-ai-generation-web.${key}`,\n `ly.img.plugin-ai-generation-web.defaults.${key}`\n ];\n}\n\nexport type ProvidersInitializationResult<\n K extends OutputKind,\n I,\n O extends Output\n> = {\n /**\n * Combined panel render function for all providers.\n */\n panel: {\n builderRenderFunction?: BuilderRenderFunction;\n };\n\n /**\n * Combined history asset source and library entry IDs for the providers.\n */\n history?: {\n assetSourceId?: string;\n assetLibraryEntryId?: string;\n };\n\n /**\n * All individual initialization results of the providers, i.e.\n * the result of `initializeProvider` for every provider.\n */\n providerInitializationResults: ProviderInitializationResult<K, I, O>[];\n};\n\n/**\n * Initializes the given providers for the specified output kind.\n *\n * - It will create a combined render function for all providers\n * that can be used in a panel\n *\n */\nasync function initializeProviders<K extends OutputKind, I, O extends Output>(\n kind: K,\n providers:\n | {\n fromText: Provider<K, I, O>[];\n fromImage: Provider<K, I, O>[];\n }\n | Provider<K, I, O>[],\n options: {\n cesdk: CreativeEditorSDK;\n },\n config: CommonPluginConfiguration<K, I, O>\n): Promise<ProvidersInitializationResult<K, I, O>> {\n // Set default translations\n const { cesdk } = options;\n cesdk.setTranslations({\n en: {\n 'ly.img.plugin-ai-generation-web.defaults.fromType.label': 'Input',\n 'ly.img.plugin-ai-generation-web.defaults.providerSelect.label':\n 'Provider',\n 'ly.img.plugin-ai-generation-web.defaults.fromText.label': 'Text',\n 'ly.img.plugin-ai-generation-web.defaults.fromImage.label': 'Image'\n }\n });\n\n let builderRenderFunction: BuilderRenderFunction | undefined;\n\n const providerResults: ProviderInitializationResult<K, I, O>[] = [];\n\n // Group provider initialization logs for cleaner console output\n if (config.debug) {\n // eslint-disable-next-line no-console\n console.groupCollapsed(`Initializing ${kind} generation providers`);\n }\n\n if (!Array.isArray(providers)) {\n const initializedFromTextProviders = await Promise.all(\n providers.fromText.map((provider) => {\n return initializeProvider(kind, provider, options, config);\n })\n );\n providerResults.push(...initializedFromTextProviders);\n\n const initializedFromImageProviders = await Promise.all(\n providers.fromImage.map((provider) => {\n return initializeProvider(kind, provider, options, config);\n })\n );\n providerResults.push(...initializedFromImageProviders);\n\n builderRenderFunction = getBuilderRenderFunctionByFromType({\n kind,\n prefix: `ly.img.ai.${kind}-generation`,\n initializedFromTextProviders,\n initializedFromImageProviders,\n cesdk\n });\n } else {\n const results = await Promise.all(\n providers.map((provider) => {\n return initializeProvider(kind, provider, options, config);\n })\n );\n providerResults.push(...results);\n\n builderRenderFunction = getBuilderRenderFunctionByProvider({\n kind,\n prefix: kind,\n providerInitializationResults: providerResults,\n cesdk\n });\n }\n\n // Close the console group\n if (config.debug) {\n // eslint-disable-next-line no-console\n console.groupEnd();\n }\n\n const compositeHistoryAssetSourceId = initializeHistoryCompositeAssetSource({\n kind,\n cesdk: options.cesdk,\n historAssetSourceIds: providerResults\n .map((result) => result.history?.assetSourceId)\n .filter(isDefined)\n });\n\n let compositeHistoryAssetLibraryEntryId: string | undefined;\n\n if (compositeHistoryAssetSourceId != null) {\n compositeHistoryAssetLibraryEntryId = compositeHistoryAssetSourceId;\n options.cesdk.ui.addAssetLibraryEntry({\n id: compositeHistoryAssetLibraryEntryId,\n sourceIds: [compositeHistoryAssetSourceId],\n sortBy: {\n sortKey: 'insertedAt',\n sortingOrder: 'Descending'\n },\n canRemove: true,\n gridItemHeight: 'square',\n gridBackgroundType: 'cover'\n });\n }\n\n return {\n panel: {\n builderRenderFunction\n },\n history: {\n assetSourceId: compositeHistoryAssetSourceId,\n assetLibraryEntryId: compositeHistoryAssetLibraryEntryId\n },\n providerInitializationResults: providerResults\n };\n}\n\n/**\n * Combines the render functions of the initialized providers into a single\n * render function that can be used in a panel. Will add select components\n * to switch between the different providers and input types.\n */\nfunction getBuilderRenderFunctionByFromType<\n K extends OutputKind,\n I,\n O extends Output\n>({\n kind,\n prefix,\n initializedFromTextProviders,\n initializedFromImageProviders,\n cesdk\n}: {\n kind: K;\n prefix: string;\n initializedFromTextProviders: ProviderInitializationResult<K, I, O>[];\n initializedFromImageProviders: ProviderInitializationResult<K, I, O>[];\n cesdk: CreativeEditorSDK;\n}): BuilderRenderFunction<{}> {\n const includeFromSwitch =\n initializedFromTextProviders.length > 0 &&\n initializedFromImageProviders.length > 0;\n\n const builderRenderFunction: BuilderRenderFunction = (context) => {\n const { builder, experimental, engine } = context;\n\n // Check if text and image input are enabled via Feature API\n const textInputFeatureId = `ly.img.plugin-ai-${kind}-generation-web.fromText`;\n const isTextInputEnabled = cesdk.feature.isEnabled(textInputFeatureId, {\n engine\n });\n const imageInputFeatureId = `ly.img.plugin-ai-${kind}-generation-web.fromImage`;\n const isImageInputEnabled = cesdk.feature.isEnabled(imageInputFeatureId, {\n engine\n });\n\n // Determine default input type based on what's enabled\n let defaultInputType: 'fromText' | 'fromImage' | undefined;\n if (\n isTextInputEnabled &&\n initializedFromTextProviders.length > 0 &&\n (!isImageInputEnabled || initializedFromImageProviders.length === 0)\n ) {\n defaultInputType = 'fromText';\n } else if (\n isImageInputEnabled &&\n initializedFromImageProviders.length > 0 &&\n (!isTextInputEnabled || initializedFromTextProviders.length === 0)\n ) {\n defaultInputType = 'fromImage';\n } else if (isTextInputEnabled && isImageInputEnabled && includeFromSwitch) {\n defaultInputType = 'fromText';\n }\n\n const inputTypeState = experimental.global<\n 'fromText' | 'fromImage' | undefined\n >(`${prefix}.fromType`, defaultInputType);\n\n const providerInitializationResults: ProviderInitializationResult<\n K,\n I,\n O\n >[] = [];\n if (inputTypeState.value === 'fromText') {\n providerInitializationResults.push(...initializedFromTextProviders);\n } else if (inputTypeState.value === 'fromImage') {\n providerInitializationResults.push(...initializedFromImageProviders);\n } else {\n providerInitializationResults.push(\n ...initializedFromTextProviders,\n ...initializedFromImageProviders\n );\n }\n\n const providerValuesFromText: (SelectValue & {\n builderRenderFunction?: BuilderRenderFunction;\n })[] = initializedFromTextProviders.map(({ provider, panel }) => ({\n id: provider.id,\n label: provider.name ?? provider.id,\n builderRenderFunction: panel?.builderRenderFunction\n }));\n\n const providerValuesFromImage: (SelectValue & {\n builderRenderFunction?: BuilderRenderFunction;\n })[] = initializedFromImageProviders.map(({ provider, panel }) => ({\n id: provider.id,\n label: provider.name ?? provider.id,\n builderRenderFunction: panel?.builderRenderFunction\n }));\n\n // Store only the provider ID in global state (not the full object with label and render function)\n // This allows external code to change the selected provider by just setting the ID\n const providerIdStateFromText = context.experimental.global(\n `${prefix}.selectedProvider.fromText`,\n providerValuesFromText[0]?.id\n );\n const providerIdStateFromImage = context.experimental.global(\n `${prefix}.selectedProvider.fromImage`,\n providerValuesFromImage[0]?.id\n );\n\n // Derive the full provider value by looking up the ID in the values array\n const providerFromText =\n providerValuesFromText.find(\n (p) => p.id === providerIdStateFromText.value\n ) ?? providerValuesFromText[0];\n\n const providerFromImage =\n providerValuesFromImage.find(\n (p) => p.id === providerIdStateFromImage.value\n ) ?? providerValuesFromImage[0];\n\n const providerIdState =\n inputTypeState.value === 'fromText'\n ? providerIdStateFromText\n : inputTypeState.value === 'fromImage'\n ? providerIdStateFromImage\n : undefined;\n\n const providerValue =\n inputTypeState.value === 'fromText'\n ? providerFromText\n : inputTypeState.value === 'fromImage'\n ? providerFromImage\n : undefined;\n\n // Check if provider selector is enabled via Feature API\n const providerFeatureId = `ly.img.plugin-ai-${kind}-generation-web.providerSelect`;\n const isProviderSelectorEnabled = cesdk.feature.isEnabled(\n providerFeatureId,\n { engine }\n );\n\n // Check if neither text nor image input is enabled\n if (!isTextInputEnabled && !isImageInputEnabled) {\n builder.Section(`${prefix}.noInputWarning.section`, {\n children: () => {\n builder.Text(`${prefix}.noInputWarning.text`, {\n content:\n 'No input types are enabled. Please enable at least one input type (text or image) via the Feature API.'\n });\n }\n });\n return; // Exit early, don't render anything else\n }\n\n // Determine if we need to show the input selector\n // Only show if both types are enabled AND both have providers\n const bothInputsEnabled =\n isTextInputEnabled &&\n isImageInputEnabled &&\n initializedFromTextProviders.length > 0 &&\n initializedFromImageProviders.length > 0;\n const shouldShowInputSelector = includeFromSwitch && bothInputsEnabled;\n const shouldShowProviderSelector =\n providerInitializationResults.length > 1 && isProviderSelectorEnabled;\n\n if (shouldShowInputSelector || shouldShowProviderSelector) {\n builder.Section(`${prefix}.providerSelection.section`, {\n children: () => {\n // RENDER FROM SELECTION - only if both input types are enabled\n if (shouldShowInputSelector) {\n builder.ButtonGroup(`${prefix}.fromType.buttonGroup`, {\n inputLabel: createLabelArray(kind, 'fromType.label'),\n children: () => {\n if (\n isTextInputEnabled &&\n initializedFromTextProviders.length > 0\n ) {\n builder.Button(`${prefix}.fromType.buttonGroup.fromText`, {\n label: createLabelArray(kind, 'fromText.label'),\n icon:\n inputTypeState.value !== 'fromText' &&\n isSomeProviderGenerating(\n initializedFromTextProviders,\n context\n )\n ? '@imgly/LoadingSpinner'\n : undefined,\n isActive: inputTypeState.value === 'fromText',\n onClick: () => {\n inputTypeState.setValue('fromText');\n }\n });\n }\n\n if (\n isImageInputEnabled &&\n initializedFromImageProviders.length > 0\n ) {\n builder.Button(`${prefix}.fromType.buttonGroup.fromImage`, {\n label: createLabelArray(kind, 'fromImage.label'),\n icon:\n inputTypeState.value !== 'fromImage' &&\n isSomeProviderGenerating(\n initializedFromImageProviders,\n context\n )\n ? '@imgly/LoadingSpinner'\n : undefined,\n isActive: inputTypeState.value === 'fromImage',\n onClick: () => {\n inputTypeState.setValue('fromImage');\n }\n });\n }\n }\n });\n }\n\n // RENDER PROVIDER SELECT\n if (shouldShowProviderSelector) {\n const providerValues =\n inputTypeState.value === 'fromText'\n ? providerValuesFromText\n : inputTypeState.value === 'fromImage'\n ? providerValuesFromImage\n : [...providerValuesFromText, ...providerValuesFromImage];\n\n if (providerIdState != null && providerValue != null) {\n builder.Select(`${prefix}.providerSelect.select`, {\n inputLabel: createLabelArray(kind, 'providerSelect.label'),\n values: providerValues,\n value: providerValue,\n setValue: (newValue: SelectValue) => {\n providerIdState.setValue(newValue.id);\n }\n });\n }\n }\n }\n });\n }\n\n // Render the provider content\n if (providerInitializationResults.length > 1) {\n providerValue?.builderRenderFunction?.(context);\n } else {\n const providerInitializationResult = providerInitializationResults[0];\n if (providerInitializationResult) {\n providerInitializationResult.panel?.builderRenderFunction?.(context);\n }\n }\n };\n\n return builderRenderFunction;\n}\n\n/**\n * Combines the render functions of the initialized providers into a single\n * render function that can be used in a panel. Will add select components\n * to switch between the different providers and input types.\n */\nfunction getBuilderRenderFunctionByProvider<\n K extends OutputKind,\n I,\n O extends Output\n>({\n kind,\n prefix,\n providerInitializationResults,\n cesdk\n}: {\n kind: K;\n prefix: string;\n providerInitializationResults: ProviderInitializationResult<K, I, O>[];\n cesdk: CreativeEditorSDK;\n}): BuilderRenderFunction<{}> {\n const builderRenderFunction: BuilderRenderFunction = (context) => {\n const { builder, engine } = context;\n if (providerInitializationResults.length === 0) return;\n\n // Check if provider selector is enabled via Feature API\n // Audio plugin has special cases for speech and sound providers\n let providerFeatureId = `ly.img.plugin-ai-${kind}-generation-web.providerSelect`;\n // For audio, we check prefix to determine if it's speech or sound\n if (kind === 'audio' && prefix) {\n if (prefix.includes('speech')) {\n providerFeatureId = `ly.img.plugin-ai-audio-generation-web.speech.providerSelect`;\n } else if (prefix.includes('sound')) {\n providerFeatureId = `ly.img.plugin-ai-audio-generation-web.sound.providerSelect`;\n }\n }\n const isProviderSelectorEnabled = cesdk.feature.isEnabled(\n providerFeatureId,\n { engine }\n );\n\n const providerValues: (SelectValue & {\n builderRenderFunction?: BuilderRenderFunction;\n })[] = providerInitializationResults.map(({ provider, panel }) => ({\n id: provider.id,\n label: provider.name ?? provider.id,\n builderRenderFunction: panel?.builderRenderFunction\n }));\n\n // Store only the provider ID in state (not the full object with label and render function)\n // This allows external code to change the selected provider by just setting the ID\n const providerIdState = context.state(\n `${prefix}.selectedProvider`,\n providerValues[0]?.id\n );\n\n // Derive the full provider value by looking up the ID in the values array\n const providerValue =\n providerValues.find((p) => p.id === providerIdState.value) ??\n providerValues[0];\n\n if (providerInitializationResults.length > 1 && isProviderSelectorEnabled) {\n if (providerIdState != null && providerValue != null) {\n builder.Section(`${prefix}.providerSelection.section`, {\n children: () => {\n builder.Select(`${prefix}.providerSelect.select`, {\n inputLabel: createLabelArray(kind, 'providerSelect.label'),\n values: providerValues,\n value: providerValue,\n setValue: (newValue: SelectValue) => {\n providerIdState.setValue(newValue.id);\n }\n });\n }\n });\n }\n }\n\n // Render the provider content\n providerValue?.builderRenderFunction?.(context);\n };\n\n return builderRenderFunction;\n}\n\n/**\n * Queries the global state to check if any provider from the given\n * list is currently generating.\n */\nfunction isSomeProviderGenerating<K extends OutputKind, I, O extends Output>(\n providerInitializationResults: ProviderInitializationResult<K, I, O>[],\n context: BuilderRenderFunctionContext<any>\n): boolean {\n if (providerInitializationResults.length === 0) return false;\n return providerInitializationResults.some(({ provider }) => {\n if (provider.id == null) return false;\n return context.experimental.global(isGeneratingStateKey(provider.id), false)\n .value;\n });\n}\n\nexport default initializeProviders;\n", "export const AI_EDIT_MODE = 'ly.img.ai.editMode';\n\nexport const AI_METADATA_KEY = 'ly.img.ai.metadata';\n\nexport const AI_CONFIRMATION_COMPONENT_ID = 'ly.img.ai.confirmation.canvasMenu';\n\nexport function getFeatureIdForQuickAction(options: {\n quickActionId: string;\n quickActionMenuId: string;\n}) {\n return `ly.img.ai.quickAction.${options.quickActionMenuId}.${options.quickActionId}`;\n}\n", "import CreativeEditorSDK, { BuilderRenderFunction } from '@cesdk/cesdk-js';\nimport { InferenceMetadata } from '../quickActions/types';\nimport { Metadata } from '@imgly/plugin-utils';\nimport { AI_METADATA_KEY } from '../quickActions/utils';\nimport { OutputKind } from '../../core/provider';\nimport { Callbacks } from '../../generation/CallbacksRegistry';\n\n/**\n * Creates a render function for the AI inference confirmation component.\n *\n * It will render 'cancel', 'before', 'after', and 'apply' buttons once the\n * inference is done. Until then a loading spinner is shown with a cancel button.\n *\n * The callbacks are provided by the payload from the context.\n */\nasync function createConfirmationRenderFunction<K extends OutputKind>(context: {\n kind: K;\n\n cesdk: CreativeEditorSDK;\n}): Promise<BuilderRenderFunction<Callbacks>> {\n const prefix = `ly.img.ai.${context.kind}.confirmation`;\n context.cesdk?.i18n.setTranslations({\n en: {\n 'ly.img.ai.processing': 'Generating...',\n [`${prefix}.cancel`]: 'Cancel Generation',\n [`${prefix}.apply`]: 'Apply Generation',\n [`${prefix}.before`]: 'Before',\n [`${prefix}.after`]: 'After'\n }\n });\n const builderRenderFunction: BuilderRenderFunction<Callbacks> = (\n builderContext\n ) => {\n const { engine, builder, state, payload } = builderContext;\n if (payload == null) return;\n\n const blockIds = engine.block.findAllSelected();\n if (blockIds.length === 0) return null;\n\n const md = new Metadata<InferenceMetadata>(engine, AI_METADATA_KEY);\n\n // All blocks must have the same metadata\n const metadata = md.get(blockIds[0]);\n if (metadata == null) return null;\n\n const clearMetadata = () => {\n blockIds.forEach((blockId) => {\n md.clear(blockId);\n });\n };\n\n switch (metadata.status) {\n case 'processing': {\n builder.Button(`${prefix}.spinner`, {\n label: [\n `ly.img.ai.${metadata.quickActionId}.processing`,\n `ly.img.ai.processing`\n ],\n isLoading: true\n });\n builder.Separator(`${prefix}.separator`);\n builder.Button(`${prefix}.cancel`, {\n icon: '@imgly/Cross',\n tooltip: `${prefix}.cancel`,\n onClick: () => {\n payload.onCancelGeneration?.();\n clearMetadata();\n }\n });\n\n break;\n }\n\n case 'confirmation': {\n const comparingState = state<'before' | 'after'>(\n `${prefix}.comparing`,\n 'after'\n );\n\n const onCancel = payload.applyCallbacks?.onCancel;\n if (onCancel != null) {\n builder.Button(`${prefix}.cancel`, {\n icon: '@imgly/Cross',\n tooltip: `${prefix}.cancel`,\n onClick: () => {\n onCancel();\n clearMetadata();\n }\n });\n }\n\n const onBefore = payload.applyCallbacks?.onBefore;\n const onAfter = payload.applyCallbacks?.onAfter;\n\n if (onBefore != null && onAfter != null) {\n builder.ButtonGroup(`${prefix}.compare`, {\n children: () => {\n builder.Button(`${prefix}.compare.before`, {\n label: `${prefix}.before`,\n variant: 'regular',\n isActive: comparingState.value === 'before',\n onClick: () => {\n onBefore();\n comparingState.setValue('before');\n }\n });\n builder.Button(`${prefix}.compare.after`, {\n label: `${prefix}.after`,\n variant: 'regular',\n isActive: comparingState.value === 'after',\n onClick: () => {\n onAfter();\n comparingState.setValue('after');\n }\n });\n }\n });\n }\n\n const onApply = payload.applyCallbacks?.onApply;\n if (onApply != null) {\n builder.Button(`${prefix}.apply`, {\n icon: '@imgly/Checkmark',\n tooltip: `${prefix}.apply`,\n color: 'accent',\n isDisabled: comparingState.value !== 'after',\n onClick: () => {\n clearMetadata();\n\n const editor = context.cesdk.engine.editor as any;\n if (typeof editor._update === 'function') {\n // Activating the old history happens in the next update lop.\n editor._update();\n }\n\n onApply();\n }\n });\n }\n\n break;\n }\n\n default: {\n // noop\n }\n }\n };\n return Promise.resolve(builderRenderFunction);\n}\n\nexport default createConfirmationRenderFunction;\n", "/**\n * For the given array that contains strings `ly.img.separator` and other items,\n * this function will remove all leading and trailing separators,\n * as well as any consecutive separators in between.\n */\nfunction compactSeparators<T>(\n quickActions: (T | 'ly.img.separator')[]\n): (T | 'ly.img.separator')[] {\n if (quickActions.length === 0) {\n return [];\n }\n\n // Create a new array for the result\n const result = [...quickActions];\n\n // Remove separators at the beginning\n while (result.length > 0 && result[0] === 'ly.img.separator') {\n result.shift();\n }\n\n // Remove separators at the end\n while (\n result.length > 0 &&\n result[result.length - 1] === 'ly.img.separator'\n ) {\n result.pop();\n }\n\n // Remove consecutive separators\n return result.reduce<(T | 'ly.img.separator')[]>((acc, current) => {\n // Skip if current is a separator and previous was also a separator\n if (\n current === 'ly.img.separator' &&\n acc.length > 0 &&\n acc[acc.length - 1] === 'ly.img.separator'\n ) {\n return acc;\n }\n\n acc.push(current);\n return acc;\n }, []);\n}\n\nexport default compactSeparators;\n", "import { OutputKind } from '../core/provider';\n\nfunction getQuickActionCanvasMenuComponentId<K extends OutputKind>(\n kind: K\n): string {\n return `ly.img.ai.${kind}.canvasMenu`;\n}\n\nexport default getQuickActionCanvasMenuComponentId;\n", "import CreativeEditorSDK, { ComponentPayload } from '@cesdk/cesdk-js';\nimport getQuickActionCanvasMenuComponentId from '../../providers/getCanvasMenuComponentId';\nimport { OutputKind } from '../../core/provider';\n\n/**\n * Returns the current order of quick actions for a particular kind.\n */\nfunction getQuickActionOrder<K extends OutputKind>(context: {\n kind: K;\n cesdk: CreativeEditorSDK;\n payload?: ComponentPayload;\n defaultOrder?: string[];\n}): string[] {\n const { kind, cesdk, payload, defaultOrder } = context;\n const canvasMenuComponentId = getQuickActionCanvasMenuComponentId(kind);\n\n if (payload == null || !Array.isArray(payload.children)) {\n // Fallback to get the children order from the canvas menu order.\n // Happens e.g. for CE.SDK versions < 1.53.0 because the payload\n // is not passed correctly to the render function.\n const canvasMenuOrder = cesdk.ui.getCanvasMenuOrder();\n const component = canvasMenuOrder.find(({ id }) => {\n return id === canvasMenuComponentId;\n });\n\n if (component != null && Array.isArray(component.children)) {\n return component.children;\n } else {\n // Use provided default order or empty array for backwards compatibility\n return defaultOrder ?? [];\n }\n }\n return payload.children.filter(\n (child) => typeof child === 'string'\n ) as string[];\n}\n\nexport default getQuickActionOrder;\n", "import type CreativeEditorSDK from '@cesdk/cesdk-js';\nimport {\n getImageDimensionsFromURL,\n mimeTypeToExtension\n} from '@imgly/plugin-utils';\nimport {\n ImageOutput,\n TextOutput,\n type Output,\n type OutputKind\n} from '../core/provider';\nimport { ResultSuccess } from '../generation/createGenerateFunction';\n\nexport type ApplyCallbacks = {\n onBefore: () => void;\n onAfter: () => void;\n onCancel: () => void;\n onApply: () => void;\n};\n\ntype GetApplyCallbacksOptions = {\n kind: OutputKind;\n blockIds: number[];\n cesdk: CreativeEditorSDK;\n abortSignal?: AbortSignal;\n};\n\ntype ReturnValue = {\n applyCallbacks: ApplyCallbacks;\n};\n\n/**\n * This method is used in the quick action menu to handle the result of the generation\n * and providing methods for the comparision of the result.\n *\n * Different output kinds require different handling. E.g. the text generation\n * is streamed to the text block, while the image generation is applied to the fill\n * block with a source set and the same crop applied.\n */\nfunction getApplyCallbacks<O extends Output>(\n result: ResultSuccess<O>,\n options: GetApplyCallbacksOptions\n): Promise<ReturnValue> {\n if (options.blockIds.some((id) => !options.cesdk.engine.block.isValid(id))) {\n return Promise.resolve({\n applyCallbacks: {\n onBefore: () => {},\n onAfter: () => {},\n onCancel: () => {},\n onApply: () => {}\n }\n });\n }\n switch (options.kind) {\n case 'text':\n return getApplyCallbacksForText(result, options);\n case 'image':\n return getApplyCallbacksForImage(result, options);\n case 'video':\n return getApplyCallbacksForVideo(result, options);\n case 'audio':\n return getApplyCallbacksForAudio(result, options);\n default:\n throw new Error(\n `Unsupported output kind for quick actions: ${options.kind}`\n );\n }\n}\n\nasync function getApplyCallbacksForText<O extends Output>(\n result: ResultSuccess<O>,\n options: GetApplyCallbacksOptions\n): Promise<ReturnValue> {\n const { cesdk, blockIds, abortSignal } = options;\n\n const beforeTexts = blockIds.map((blockId) => {\n return cesdk.engine.block.getString(blockId, 'text/text');\n });\n\n let output: O | undefined;\n if (result.type === 'async') {\n let inferredText = '';\n for await (const chunk of result.output) {\n if (abortSignal?.aborted) {\n break;\n }\n if (typeof chunk === 'string') {\n inferredText = chunk;\n } else if (chunk.kind === 'text') {\n inferredText = chunk.text;\n }\n\n // eslint-disable-next-line @typescript-eslint/no-loop-func\n blockIds.forEach((blockId) => {\n cesdk.engine.block.setString(blockId, 'text/text', inferredText);\n });\n\n const textOutput: TextOutput = {\n kind: 'text',\n text: inferredText\n };\n output = textOutput as O;\n }\n } else {\n output = result.output;\n }\n\n if (output == null || output.kind !== 'text') {\n throw new Error('Output kind from generation is not text');\n }\n\n // For sync results, apply the text immediately (like streaming does)\n if (result.type === 'sync') {\n blockIds.forEach((blockId) => {\n cesdk.engine.block.setString(blockId, 'text/text', output.text);\n });\n }\n\n const onAfter = () => {\n options.blockIds.forEach((blockId) => {\n options.cesdk.engine.block.setString(blockId, 'text/text', output.text);\n });\n };\n const onBefore = () => {\n options.blockIds.forEach((blockId, i) => {\n options.cesdk.engine.block.setString(\n blockId,\n 'text/text',\n beforeTexts[i]\n );\n });\n };\n const onCancel = onBefore;\n const onApply = () => {\n onAfter();\n options.cesdk.engine.editor.addUndoStep();\n };\n\n return {\n applyCallbacks: {\n onBefore,\n onAfter,\n onCancel,\n onApply\n }\n };\n}\n\nasync function getApplyCallbacksForImage<O extends Output>(\n result: ResultSuccess<O>,\n options: GetApplyCallbacksOptions\n): Promise<ReturnValue> {\n const { cesdk, blockIds, abortSignal } = options;\n if (blockIds.length !== 1) {\n throw new Error('Only one block is supported for image generation');\n }\n\n const [block] = blockIds;\n const fillBlock = cesdk.engine.block.getFill(block);\n const sourceSetBefore = cesdk.engine.block.getSourceSet(\n fillBlock,\n 'fill/image/sourceSet'\n );\n const [sourceBefore] = sourceSetBefore;\n let uriBefore: string | undefined;\n if (sourceBefore == null) {\n uriBefore = cesdk.engine.block.getString(\n fillBlock,\n 'fill/image/imageFileURI'\n );\n }\n\n const mimeType = await cesdk.engine.editor.getMimeType(\n sourceBefore?.uri ?? uriBefore\n );\n const originalDimension = await getImageDimensionsFromURL(\n sourceBefore?.uri ?? uriBefore,\n options.cesdk.engine\n );\n const originalAspectRatio =\n originalDimension.width / originalDimension.height;\n abortSignal?.throwIfAborted();\n\n if (mimeType === 'image/svg+xml') {\n throw new Error('SVG images are not supported');\n }\n\n const cropScaleX = cesdk.engine.block.getCropScaleX(block);\n const cropScaleY = cesdk.engine.block.getCropScaleY(block);\n const cropTranslationX = cesdk.engine.block.getCropTranslationX(block);\n const cropTranslationY = cesdk.engine.block.getCropTranslationY(block);\n const cropRotation = cesdk.engine.block.getCropRotation(block);\n\n const applyCrop = () => {\n cesdk.engine.block.setCropScaleX(block, cropScaleX);\n cesdk.engine.block.setCropScaleY(block, cropScaleY);\n cesdk.engine.block.setCropTranslationX(block, cropTranslationX);\n cesdk.engine.block.setCropTranslationY(block, cropTranslationY);\n cesdk.engine.block.setCropRotation(block, cropRotation);\n };\n\n if (result.type === 'async') {\n throw new Error('Streaming generation is not supported yet from a panel');\n }\n\n if (result.output.kind !== 'image' || typeof result.output.url !== 'string') {\n throw new Error('Output kind from generation is not an image');\n }\n\n const url = (result.output as ImageOutput).url;\n const generatedMimeType = await cesdk.engine.editor.getMimeType(url);\n\n const uri = await reuploadImage(cesdk, url, generatedMimeType);\n const generatedDimension = await getImageDimensionsFromURL(\n uri,\n options.cesdk.engine\n );\n const generatedAspectRatio =\n generatedDimension.width / generatedDimension.height;\n\n const differentAspectRatio =\n Math.abs(originalAspectRatio - generatedAspectRatio) > 0.001;\n\n const sourceSetAfter = sourceBefore\n ? [\n {\n uri,\n width: generatedDimension.width,\n height: generatedDimension.height\n }\n ]\n : undefined;\n const uriAfter = uri;\n\n if (sourceSetAfter == null) {\n cesdk.engine.block.setString(\n fillBlock,\n 'fill/image/imageFileURI',\n uriAfter\n );\n } else {\n cesdk.engine.block.setString(fillBlock, 'fill/image/imageFileURI', '');\n cesdk.engine.block.setSourceSet(\n fillBlock,\n 'fill/image/sourceSet',\n sourceSetAfter\n );\n }\n\n await cesdk.engine.block.forceLoadResources([fillBlock]);\n\n if (differentAspectRatio) {\n cesdk.engine.block.setContentFillMode(block, 'Cover');\n } else {\n applyCrop();\n }\n\n const onBefore = () => {\n if (sourceSetBefore == null || sourceSetBefore.length === 0) {\n if (uriBefore == null) {\n throw new Error('No image URI found');\n }\n cesdk.engine.block.setString(\n fillBlock,\n 'fill/image/imageFileURI',\n uriBefore\n );\n } else {\n cesdk.engine.block.setSourceSet(\n fillBlock,\n 'fill/image/sourceSet',\n sourceSetBefore\n );\n }\n applyCrop();\n };\n const onAfter = () => {\n if (sourceSetAfter == null) {\n if (uriAfter == null) {\n throw new Error('No image URI found');\n }\n cesdk.engine.block.setString(\n fillBlock,\n 'fill/image/imageFileURI',\n uriAfter\n );\n } else {\n cesdk.engine.block.setSourceSet(\n fillBlock,\n 'fill/image/sourceSet',\n sourceSetAfter\n );\n }\n if (differentAspectRatio) {\n cesdk.engine.block.setContentFillMode(block, 'Cover');\n } else {\n applyCrop();\n }\n };\n const onCancel = () => {\n onBefore();\n };\n const onApply = () => {\n onAfter();\n options.cesdk.engine.editor.addUndoStep();\n };\n\n return {\n applyCallbacks: {\n onBefore,\n onAfter,\n onCancel,\n onApply\n }\n };\n}\n\nasync function getApplyCallbacksForVideo<O extends Output>(\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n _result: ResultSuccess<O>,\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n _options: GetApplyCallbacksOptions\n): Promise<ReturnValue> {\n throw new Error('Function not implemented.');\n}\n\nasync function getApplyCallbacksForAudio<O extends Output>(\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n _result: ResultSuccess<O>,\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n _options: GetApplyCallbacksOptions\n): Promise<ReturnValue> {\n throw new Error('Function not implemented.');\n}\n\nasync function reuploadImage(\n cesdk: CreativeEditorSDK,\n url: string,\n mimeType: string\n): Promise<string> {\n const response = await fetch(url);\n const blob = await response.blob();\n const file = new File([blob], `image.${mimeTypeToExtension(mimeType)}`, {\n type: mimeType\n });\n const assetDefinition = await cesdk.unstable_upload(file, () => {});\n const uploadedUri = assetDefinition?.meta?.uri;\n if (uploadedUri != null) return uploadedUri;\n // eslint-disable-next-line no-console\n console.warn('Failed to upload image:', assetDefinition);\n return url;\n}\n\nexport default getApplyCallbacks;\n", "import { CreativeEngine } from '@cesdk/cesdk-js';\n\n/**\n * Locks the selection to the given block ids with the given edit mode.\n *\n * @returns A function to unlock the selection. Will set the edit mode back to what it was before the lock.\n */\nfunction lockSelectionToEditMode(options: {\n engine: CreativeEngine;\n blockIdsToLock: number[];\n editModeToLockTo: string;\n}) {\n const { engine, blockIdsToLock, editModeToLockTo } = options;\n\n function isBoundBlockSelected() {\n const currentlySelectedBlockIds = engine.block.findAllSelected();\n return blockIdsToLock.some((blockId) => {\n return currentlySelectedBlockIds.includes(blockId);\n });\n }\n\n const stateChangeDisposer = engine.editor.onStateChanged(() => {\n const editMode = engine.editor.getEditMode();\n if (editMode !== editModeToLockTo && isBoundBlockSelected()) {\n engine.editor.setEditMode(editModeToLockTo);\n }\n });\n\n const selectionDisposer = engine.block.onSelectionChanged(() => {\n if (isBoundBlockSelected()) {\n engine.editor.setEditMode(editModeToLockTo);\n } else {\n engine.editor.setEditMode('Transform');\n }\n });\n\n if (isBoundBlockSelected()) {\n engine.editor.setEditMode(editModeToLockTo);\n }\n\n let disposed = false;\n const dispose = () => {\n if (disposed) return;\n selectionDisposer();\n stateChangeDisposer();\n\n engine.editor.setEditMode('Transform');\n disposed = true;\n };\n\n return dispose;\n}\n\nexport default lockSelectionToEditMode;\n", "import { ApplyCallbacks } from '../providers/getApplyCallbacks';\n\nexport type Callbacks = {\n /**\n * Callbacks for the confirmation process.\n */\n applyCallbacks?: ApplyCallbacks;\n\n /**\n * Callback when generation is cancelled.\n */\n onCancelGeneration?: () => void;\n};\n\n/**\n * Global registry for managing callbacks for quick action menus across different\n * components.\n */\nexport class CallbacksRegistry {\n /** Map storing all registered callbacks by their ID */\n private actions: Map<number, Callbacks> = new Map();\n\n // eslint-disable-next-line @typescript-eslint/no-empty-function\n private constructor() {}\n\n /**\n * Gets the singleton instance of the CallbacksRegistry.\n * Uses global object storage to ensure singleton across different bundle contexts.\n */\n public static get(): CallbacksRegistry {\n const globalKey = '__imgly_callbacks_registry__';\n const globalObj = (\n typeof window !== 'undefined' ? window : globalThis\n ) as any;\n\n if (!globalObj[globalKey]) {\n globalObj[globalKey] = new CallbacksRegistry();\n }\n return globalObj[globalKey];\n }\n\n /**\n * Registers apply callbacks in the registry.\n */\n public register(blockId: number, callbacks: Partial<Callbacks>) {\n const currentCallbacks = this.actions.get(blockId) ?? {};\n this.actions.set(blockId, {\n ...currentCallbacks,\n ...callbacks\n });\n }\n\n /**\n * Gets the callbacks for the given block id.\n */\n public get(blockId: number): Callbacks {\n return this.actions.get(blockId) ?? {};\n }\n}\n\nexport default CallbacksRegistry;\n", "import { isDefined, Metadata } from '@imgly/plugin-utils';\nimport { QuickActionDefinition } from '../core/ActionRegistry';\nimport { Result } from './createGenerateFunction';\nimport { ProviderInitializationResult } from '../providers/initializeProvider';\nimport { Middleware } from '../middleware/middleware';\nimport { Output, OutputKind } from '../core/provider';\nimport { AI_EDIT_MODE, AI_METADATA_KEY } from '../ui/quickActions/utils';\nimport { InferenceMetadata } from '../ui/quickActions/types';\nimport CreativeEditorSDK from '@cesdk/cesdk-js';\nimport getApplyCallbacks from '../providers/getApplyCallbacks';\nimport lockSelectionToEditMode from '../utils/lockSelectionToEditMode';\nimport CallbacksRegistry from './CallbacksRegistry';\nimport { ABORT_REASON_USER_CANCEL } from '../core/constants';\n\ntype GenerationOptions<\n Q extends Record<string, any>,\n K extends OutputKind,\n I,\n O extends Output\n> = {\n /**\n * The blocks that this quick action is running on.\n */\n blockIds: number[];\n\n /**\n * The initlaized provider that is used to generate the output.\n */\n providerInitializationResult?: ProviderInitializationResult<K, I, O>;\n\n /**\n * The quick action definition that is used to generate input for the provider\n */\n quickAction: QuickActionDefinition<Q>;\n\n /**\n * Shall the generation be confirmed before applying the result? Or directly applied?\n */\n confirmation?: boolean;\n\n /**\n * Should the blocks be locked to edit mode while the generation is running?\n */\n lock?: boolean;\n\n /**\n * Additional middlewares added to the generation process.\n */\n middlewares?: Middleware<I, O>[];\n\n /**\n * Print debug information to the console.\n */\n debug?: boolean;\n\n /**\n * Enable dry run mode for testing.\n */\n dryRun?: boolean;\n\n /**\n * Signal to check if process was aborted\n */\n abortSignal?: AbortSignal;\n\n /**\n * Close the quick action menu\n */\n close: () => void;\n\n cesdk: CreativeEditorSDK;\n};\n\n/**\n * Handler for generating output from a quick action.\n */\nfunction handleGenerateFromQuickAction<\n Q extends Record<string, any>,\n K extends OutputKind,\n I,\n O extends Output\n>(\n options: GenerationOptions<Q, K, I, O>\n): (input: Q, generateOptions?: { blockIds?: number[] }) => Promise<Result<O>> {\n return async (input: Q, generateOptions?: { blockIds?: number[] }) => {\n // Use provided blockIds or fall back to default selection\n const targetBlockIds = generateOptions?.blockIds ?? options.blockIds;\n if (options.providerInitializationResult == null) {\n if (options.debug) {\n // eslint-disable-next-line no-console\n console.warn(\n '[Generate] No provider initialization result found for quick action. Returning abortion status.'\n );\n }\n return { status: 'aborted' };\n }\n\n const supportValue =\n options.providerInitializationResult?.provider.input?.quickActions\n ?.supported?.[options.quickAction.id];\n\n if (supportValue == null) {\n throw new Error(\n '[Generate] Quick action input mapping failed. Ensure the provider supports this quick action.'\n );\n }\n\n // Handle both `true` and `{ mapInput: ... }` cases\n const mapInput =\n typeof supportValue === 'object' &&\n supportValue !== null &&\n 'mapInput' in supportValue\n ? supportValue.mapInput\n : (i: Q) => i as unknown as I; // Identity function when types are compatible\n\n options.close();\n\n const abortController = new AbortController();\n const abortSignal = AbortSignal.any(\n [options.abortSignal, abortController.signal].filter(isDefined)\n );\n\n const metadata = new Metadata<InferenceMetadata>(\n options.cesdk.engine,\n AI_METADATA_KEY\n );\n\n const unlockFromEditMode =\n options.lock ?? true\n ? lockSelectionToEditMode({\n engine: options.cesdk.engine,\n editModeToLockTo: AI_EDIT_MODE,\n blockIdsToLock: targetBlockIds\n })\n : () => {\n /* No-op if not locking to edit mode */\n };\n targetBlockIds.forEach((blockId) => {\n CallbacksRegistry.get().register(blockId, {\n onCancelGeneration: () => {\n abortController.abort(ABORT_REASON_USER_CANCEL);\n unlockFromEditMode();\n\n if (options.cesdk.engine.block.isValid(blockId)) {\n if (options.cesdk.engine.block.isValid(blockId))\n options.cesdk.engine.block.setState(blockId, { type: 'Ready' });\n metadata.clear(blockId);\n }\n }\n });\n });\n\n targetBlockIds?.forEach((blockId) => {\n metadata.set(blockId, {\n status: 'processing',\n quickActionId: options.quickAction.id\n });\n });\n targetBlockIds.forEach((blockId) => {\n if (options.cesdk.engine.block.isValid(blockId))\n options.cesdk.engine.block.setState(blockId, {\n type: 'Pending',\n progress: 0\n });\n });\n\n try {\n const result = await options.providerInitializationResult.generate(\n mapInput(input),\n {\n blockIds: targetBlockIds,\n middlewares: [...(options.middlewares ?? [])],\n debug: options.debug,\n dryRun: options.dryRun,\n abortSignal\n }\n );\n\n const resultStatus = result.status;\n switch (resultStatus) {\n case 'success': {\n const { applyCallbacks: initialApplyCallbacks } =\n await getApplyCallbacks(result, {\n kind: options.providerInitializationResult.provider.kind,\n blockIds: targetBlockIds,\n cesdk: options.cesdk,\n abortSignal\n });\n\n // Set the blocks to a ready state AFTER `getApplyCallbacks` was called since\n // it will set the block to the \"after\" image and until then we still want\n // the spinner spinning.\n targetBlockIds.forEach((blockId) => {\n if (options.cesdk.engine.block.isValid(blockId))\n options.cesdk.engine.block.setState(blockId, { type: 'Ready' });\n });\n\n if (options.confirmation) {\n // For the same as the Ready state, we do this AFTER `getApplyCallbacks` was called\n targetBlockIds.forEach((blockId) => {\n // Metadata will be cleared in the confirmation component\n // see createConfirmationRenderFunction.ts\n metadata.set(blockId, {\n status: 'confirmation',\n quickActionId: options.quickAction.id\n });\n });\n\n const applyCallbacks = {\n ...initialApplyCallbacks,\n onApply: async () => {\n initialApplyCallbacks.onApply();\n unlockFromEditMode();\n },\n onCancel: () => {\n initialApplyCallbacks.onCancel();\n unlockFromEditMode();\n }\n };\n\n targetBlockIds.forEach((blockId) => {\n CallbacksRegistry.get().register(blockId, { applyCallbacks });\n });\n } else {\n targetBlockIds.forEach((blockId) => {\n if (options.cesdk.engine.block.isValid(blockId)) {\n metadata.clear(blockId);\n }\n });\n // Apply the result directly\n initialApplyCallbacks.onApply();\n unlockFromEditMode();\n }\n\n return result;\n }\n\n case 'aborted': {\n if (options.debug) {\n // eslint-disable-next-line no-console\n console.log('Generation was aborted');\n }\n unlockFromEditMode();\n targetBlockIds.forEach((blockId) => {\n if (options.cesdk.engine.block.isValid(blockId)) {\n metadata.clear(blockId);\n }\n });\n return result;\n }\n\n case 'error': {\n unlockFromEditMode();\n targetBlockIds.forEach((blockId) => {\n if (options.cesdk.engine.block.isValid(blockId)) {\n metadata.clear(blockId);\n }\n });\n // Check if default was prevented\n if (!result.middlewareOptions?.defaultPrevented()) {\n // eslint-disable-next-line no-console\n console.error(`[Generate]: ${result.message}`);\n }\n return result;\n }\n\n default: {\n throw new Error(`[Generate] Unknown result status ${resultStatus}.`);\n }\n }\n } catch (error) {\n // Note: For exceptions thrown in middleware, we don't have access to middlewareOptions\n // so we can't check defaultPrevented here. Middleware should handle errors in catch blocks\n // and re-throw to use preventDefault properly.\n options.cesdk.ui.showNotification({\n type: 'error',\n message: 'A technical issue has occurred.'\n });\n\n unlockFromEditMode();\n targetBlockIds.forEach((blockId) => {\n if (options.cesdk.engine.block.isValid(blockId)) {\n metadata.clear(blockId);\n }\n });\n throw error;\n } finally {\n targetBlockIds.forEach((blockId) => {\n if (options.cesdk.engine.block.isValid(blockId))\n options.cesdk.engine.block.setState(blockId, { type: 'Ready' });\n });\n }\n };\n}\n\nexport default handleGenerateFromQuickAction;\n", "import CreativeEditorSDK, {\n BuilderRenderFunction,\n CreativeEngine,\n SelectValue\n} from '@cesdk/cesdk-js';\nimport {\n ActionRegistry,\n QuickActionDefinition\n} from '../../core/ActionRegistry';\nimport { Output, OutputKind } from '../../core/provider';\nimport { isDefined } from '@imgly/plugin-utils';\nimport { AI_EDIT_MODE } from './utils';\nimport compactSeparators from '../../utils/compactSeparators';\nimport getQuickActionOrder from './getQuickActionOrder';\nimport { ProviderInitializationResult } from '../../providers/initializeProvider';\nimport handleGenerateFromQuickAction from '../../generation/handleGenerateFromQuickAction';\nimport { Middleware } from '../../middleware/middleware';\nimport { ProviderRegistry } from '../../core/ProviderRegistry';\n\ntype SupportedQuickAction<K extends OutputKind, I, O extends Output> = {\n definition: QuickActionDefinition<any>;\n /**\n * If defined this provider is used to render the quick action.\n * Otherwise the quick action is rendered with the main provider\n * defined one level up.\n *\n * Used for quick actions that have been defined from a provider\n * that is not the main kind/provider, e.g. a video plugin\n * that defines a quick action for the image provider.\n */\n providerInitializationResult?: ProviderInitializationResult<K, I, O>;\n};\n\ntype SupportedProviderQuickActions<\n K extends OutputKind,\n I,\n O extends Output\n> = {\n /**\n * The main provider from the provider selection\n */\n providerInitializationResult: ProviderInitializationResult<K, I, O>;\n quickActions: (SupportedQuickAction<K, I, O> | 'ly.img.separator')[];\n}[];\n\nfunction createQuickActionMenuRenderFunction<\n K extends OutputKind,\n I,\n O extends Output\n>(context: {\n kind: K;\n providerInitializationResults: ProviderInitializationResult<K, I, O>[];\n\n cesdk: CreativeEditorSDK;\n engine: CreativeEngine;\n\n debug?: boolean;\n dryRun?: boolean;\n defaultOrder?: string[];\n}): Promise<BuilderRenderFunction<any>> {\n const prefix = `ly.img.ai.${context.kind}}`;\n\n context.cesdk.i18n.setTranslations({\n en: {\n [`ly.img.plugin-ai-generation-web.defaults.quickAction.providerSelect.label`]:\n 'Provider'\n }\n });\n\n const builderRenderFunction: BuilderRenderFunction<{\n children: ('ly.img.separator' | (string & {}))[];\n }> = (builderContext) => {\n if (builderContext.engine.editor.getEditMode() === AI_EDIT_MODE) {\n return;\n }\n\n const blockIds = builderContext.engine.block.findAllSelected();\n if (blockIds.length === 0) return;\n\n // Check if the general quickAction feature is enabled\n const quickActionFeatureId = `ly.img.plugin-ai-${context.kind}-generation-web.quickAction`;\n const isQuickActionFeatureEnabled = context.cesdk.feature.isEnabled(\n quickActionFeatureId,\n {\n engine: context.engine\n }\n );\n\n if (!isQuickActionFeatureEnabled) {\n // Quick actions are disabled for this plugin\n return;\n }\n\n const { payload } = builderContext;\n const order =\n getQuickActionOrder({\n kind: context.kind,\n cesdk: context.cesdk,\n payload,\n defaultOrder: context.defaultOrder\n }) ?? [];\n\n if (order.length === 0) return;\n\n // Get ordered and filtered list of defined quick actions\n const orderedQuickActions = getOrderedQuickActionDefinitions(order).filter(\n (quickActionDefinition) => {\n if (quickActionDefinition === 'ly.img.separator') return true;\n\n // Check if this individual quick action is enabled via Feature API\n // Remove the 'ly.img.' prefix from the action ID for the feature key\n const actionName = quickActionDefinition.id.replace('ly.img.', '');\n const individualQuickActionFeatureId = `ly.img.plugin-ai-${context.kind}-generation-web.quickAction.${actionName}`;\n const isIndividualQuickActionEnabled = context.cesdk.feature.isEnabled(\n individualQuickActionFeatureId,\n {\n engine: context.engine\n }\n );\n\n if (!isIndividualQuickActionEnabled) {\n return false;\n }\n\n const scopes = quickActionDefinition.scopes;\n if (scopes != null && scopes.length > 0) {\n const isAllowedByScopes = blockIds.every((blockId) => {\n return scopes.every((scope) => {\n return context.engine.block.isAllowedByScope(blockId, scope);\n });\n });\n if (!isAllowedByScopes) return false;\n }\n\n if (quickActionDefinition.enable != null) {\n if (typeof quickActionDefinition.enable === 'function') {\n return quickActionDefinition.enable({\n engine: context.engine\n });\n } else if (typeof quickActionDefinition.enable === 'boolean') {\n return quickActionDefinition.enable;\n }\n }\n\n return true;\n }\n );\n\n // Collect quick actions that are defined by the main provider\n // in case the main provider does not support any quick actions\n // and we just want to render these.\n const quickActionsFromOtherProviders: SupportedQuickAction<K, I, O>[] = [];\n\n // Collect all provider with their supported quick actions\n const supportedProviderQuickActions =\n context.providerInitializationResults.reduce(\n (\n acc: SupportedProviderQuickActions<K, I, O>,\n providerInitializationResult\n ) => {\n if (providerInitializationResult.provider.kind !== context.kind)\n return acc;\n\n let quickActions = orderedQuickActions\n .map((quickAction) => {\n if (quickAction === 'ly.img.separator') return quickAction;\n\n // Check if the main provider supports this quick action\n if (\n providerInitializationResult.provider.input?.quickActions\n ?.supported?.[quickAction.id] != null\n ) {\n return {\n definition: quickAction\n };\n } else {\n // Check if this quick action comes from another provider that\n // is not the main provider.\n const otherProviderInitializationResult = ProviderRegistry.get()\n .getAll()\n .filter((registeredProvider) => {\n return (\n // We are looking for provider from another kind\n // and assume that all provider from the same kind\n // have been passed as main provider\n registeredProvider.provider.kind !== context.kind &&\n registeredProvider.provider.id !==\n providerInitializationResult.provider.id\n );\n })\n .find((registeredProvider) => {\n return (\n registeredProvider.provider.input?.quickActions\n ?.supported?.[quickAction.id] != null\n );\n });\n if (otherProviderInitializationResult != null) {\n const quickActionSupport: SupportedQuickAction<K, I, O> = {\n definition: quickAction,\n providerInitializationResult:\n otherProviderInitializationResult\n };\n quickActionsFromOtherProviders.push(quickActionSupport);\n return quickActionSupport;\n } else {\n return undefined;\n }\n }\n })\n .filter(isDefined);\n // Clean up the quick action list so we can render it directly\n quickActions = compactSeparators(quickActions);\n\n if (\n quickActions.length === 0 ||\n quickActions.every((entry) => entry === 'ly.img.separator')\n )\n return acc;\n\n if (\n quickActions.every(\n (quickAction) =>\n quickAction === 'ly.img.separator' ||\n quickAction.providerInitializationResult != null\n )\n ) {\n // This provider has no quick actions from the current main provider.\n // We do not want to add the main provider to the selection\n // list with just actions from other providers.\n return acc;\n }\n\n acc.push({\n providerInitializationResult,\n quickActions\n });\n\n return acc;\n },\n []\n );\n\n if (supportedProviderQuickActions.length === 0) {\n if (quickActionsFromOtherProviders.length > 0) {\n // Remove duplicates from quickActionsFromOtherProviders\n const seen = new Map<string, SupportedQuickAction<K, I, O>>();\n quickActionsFromOtherProviders.forEach((quickAction) => {\n const id = quickAction.definition.id;\n if (!seen.has(id)) {\n seen.set(id, quickAction);\n }\n });\n const uniqueQuickActions = Array.from(seen.values());\n\n // If we have quick actions from other providers, we can render them\n supportedProviderQuickActions.push({\n // Use the first provider as main but since we do not want to\n // render the provider selection, we do not care what provider it is.\n // The actual provider used is the one defined in the object.\n providerInitializationResult:\n uniqueQuickActions[0].providerInitializationResult ??\n context.providerInitializationResults[0],\n quickActions: uniqueQuickActions\n });\n } else {\n // Noting to do\n return;\n }\n }\n\n const providerValues: SelectValue[] = supportedProviderQuickActions.map(\n ({ providerInitializationResult }) => ({\n id: providerInitializationResult.provider.id,\n label:\n providerInitializationResult.provider.name ??\n providerInitializationResult.provider.id\n })\n );\n\n const currentProviderState = builderContext.experimental.global(\n `${prefix}.currentProvider`,\n providerValues[0]\n );\n const currentSupportedQuickActions = supportedProviderQuickActions.find(\n ({\n providerInitializationResult: {\n provider: { id }\n }\n }) => id === currentProviderState.value?.id\n );\n\n const isEveryBlockInReadyState = blockIds.every((blockId) => {\n return builderContext.engine.block.getState(blockId).type === 'Ready';\n });\n\n const { builder, experimental, state } = builderContext;\n\n const isGeneratingState = state<boolean>(`${prefix}.isGenerating`, false);\n const toggleExpandedState = state<string | undefined>(\n `${prefix}.toggleExpandedState`,\n undefined\n );\n\n // Middleware to track generation status\n const isGeneratingMiddleware: Middleware<I, O> = async (\n input,\n options,\n next\n ) => {\n isGeneratingState.setValue(true);\n try {\n const result = await next(input, options);\n return result;\n } finally {\n isGeneratingState.setValue(false);\n }\n };\n\n experimental.builder.Popover(`${prefix}.popover`, {\n icon: '@imgly/Sparkle',\n variant: 'plain',\n isDisabled: !isEveryBlockInReadyState,\n isLoading: isGeneratingState.value,\n trailingIcon: null,\n children: ({ close }) => {\n if (toggleExpandedState.value !== undefined) {\n // ==========================================\n // === RENDER EXPANDED QUICK ACTION STATE ===\n // ==========================================\n\n const expandedQuickAction =\n currentSupportedQuickActions?.quickActions.find(\n (quickAction) =>\n quickAction !== 'ly.img.separator' &&\n quickAction.definition.id === toggleExpandedState.value\n ) as SupportedQuickAction<K, I, O> | undefined;\n\n if (\n expandedQuickAction == null ||\n expandedQuickAction.definition.render == null\n ) {\n return;\n }\n\n // Use only providers that support the current expanded quick action\n const providerValuesForExpandedQuickAction: SelectValue[] =\n supportedProviderQuickActions\n .filter(({ quickActions }) =>\n quickActions.some(\n (qa) =>\n qa !== 'ly.img.separator' &&\n qa.definition.id === expandedQuickAction.definition.id\n )\n )\n .map(({ providerInitializationResult }) => ({\n id: providerInitializationResult.provider.id,\n label:\n providerInitializationResult.provider.name ??\n providerInitializationResult.provider.id\n }));\n\n // Check if provider selector is enabled via Feature API\n const providerFeatureId = `ly.img.plugin-ai-${context.kind}-generation-web.quickAction.providerSelect`;\n const isProviderSelectorEnabled = context.cesdk.feature.isEnabled(\n providerFeatureId,\n {\n engine: context.engine\n }\n );\n\n if (\n providerValuesForExpandedQuickAction.length > 1 &&\n isProviderSelectorEnabled\n ) {\n builder.Section(`${prefix}.popover.expanded.header`, {\n children: () => {\n builder.Select(`${prefix}.expanded.providerSelect.select`, {\n inputLabel: [\n `ly.img.plugin-ai-${context.kind}-generation-web.quickAction.providerSelect.label`,\n `ly.img.plugin-ai-generation-web.defaults.quickAction.providerSelect.label`\n ],\n values: providerValuesForExpandedQuickAction,\n ...currentProviderState\n });\n }\n });\n }\n builder.Section(`${prefix}.popover.expanded.section`, {\n children: () => {\n return expandedQuickAction.definition.render({\n ...builderContext,\n toggleExpand: () => {\n toggleExpandedState.setValue(undefined);\n },\n isExpanded: true,\n generate: handleGenerateFromQuickAction({\n blockIds,\n providerInitializationResult:\n expandedQuickAction.providerInitializationResult ??\n currentSupportedQuickActions?.providerInitializationResult,\n quickAction: expandedQuickAction.definition,\n middlewares: [isGeneratingMiddleware],\n\n confirmation:\n expandedQuickAction.definition.defaults?.confirmation ??\n true,\n\n lock: expandedQuickAction.definition.defaults?.lock ?? true,\n\n close,\n cesdk: context.cesdk,\n debug: context.debug,\n dryRun: context.dryRun\n }),\n close,\n providerId: currentProviderState.value.id\n });\n }\n });\n } else {\n // =========================================\n // === RENDER REGULAR QUICK ACTIONS MENU ===\n // =========================================\n // Check if provider selector is enabled via Feature API\n const providerFeatureId = `ly.img.plugin-ai-${context.kind}-generation-web.quickAction.providerSelect`;\n const isProviderSelectorEnabled = context.cesdk.feature.isEnabled(\n providerFeatureId,\n {\n engine: context.engine\n }\n );\n\n if (providerValues.length > 1 && isProviderSelectorEnabled) {\n builder.Section(`${prefix}.popover.header`, {\n children: () => {\n builder.Select(`${prefix}.providerSelect.select`, {\n inputLabel: [\n `ly.img.plugin-ai-${context.kind}-generation-web.quickAction.providerSelect.label`,\n `ly.img.plugin-ai-generation-web.defaults.quickAction.providerSelect.label`\n ],\n values: providerValues,\n ...currentProviderState\n });\n }\n });\n }\n builder.Section(`${prefix}.popover.section`, {\n children: () => {\n experimental.builder.Menu(`${prefix}.menu`, {\n children: () => {\n currentSupportedQuickActions?.quickActions.forEach(\n (quickAction) => {\n if (quickAction === 'ly.img.separator') {\n builder.Separator(\n `${prefix}.separator.${Math.random().toString()}`\n );\n return;\n }\n if (quickAction.definition.render == null) return;\n quickAction.definition.render({\n ...builderContext,\n toggleExpand: () => {\n toggleExpandedState.setValue(\n quickAction.definition.id\n );\n },\n isExpanded: false,\n generate: handleGenerateFromQuickAction({\n blockIds,\n providerInitializationResult:\n quickAction.providerInitializationResult ??\n currentSupportedQuickActions?.providerInitializationResult,\n quickAction: quickAction.definition,\n middlewares: [isGeneratingMiddleware],\n\n confirmation:\n quickAction.definition.defaults?.confirmation ??\n true,\n\n close,\n cesdk: context.cesdk,\n debug: context.debug,\n dryRun: context.dryRun\n }),\n close,\n providerId: currentProviderState.value.id\n });\n }\n );\n }\n });\n }\n });\n }\n }\n });\n };\n return Promise.resolve(builderRenderFunction);\n}\n\nfunction getOrderedQuickActionDefinitions(\n order: string[]\n): (QuickActionDefinition<any> | 'ly.img.separator')[] {\n return order\n .map((quickActionId) => {\n if (quickActionId === 'ly.img.separator')\n return quickActionId as 'ly.img.separator';\n\n const quickAction = ActionRegistry.get().getBy({\n id: quickActionId,\n type: 'quick'\n })[0];\n\n return quickAction;\n })\n .filter(isDefined);\n}\n\nexport default createQuickActionMenuRenderFunction;\n", "import CreativeEditorSDK, {\n BuilderRenderFunction,\n CreativeEngine\n} from '@cesdk/cesdk-js';\nimport { Output, OutputKind } from '../../core/provider';\nimport createConfirmationRenderFunction from '../panels/createConfirmationRenderFunction';\nimport createQuickActionMenuRenderFunction from './createQuickActionMenuRenderFunction';\nimport { AI_EDIT_MODE } from './utils';\nimport { ProviderInitializationResult } from '../../providers/initializeProvider';\nimport CallbacksRegistry from '../../generation/CallbacksRegistry';\n\nasync function initializeQuickActionComponents<\n K extends OutputKind,\n I,\n O extends Output\n>(context: {\n kind: K;\n providerInitializationResults: ProviderInitializationResult<K, I, O>[];\n\n cesdk: CreativeEditorSDK;\n engine: CreativeEngine;\n debug?: boolean;\n dryRun?: boolean;\n defaultOrder?: string[];\n}) {\n const menuRenderFunction = await createQuickActionMenuRenderFunction({\n kind: context.kind,\n providerInitializationResults: context.providerInitializationResults,\n\n cesdk: context.cesdk,\n engine: context.engine,\n debug: context.debug,\n dryRun: context.dryRun,\n defaultOrder: context.defaultOrder\n });\n const confirmationRenderFunction = await createConfirmationRenderFunction({\n kind: context.kind,\n\n cesdk: context.cesdk\n });\n\n const builderRenderFunction: BuilderRenderFunction<any> = (\n builderContext\n ) => {\n const { engine } = builderContext;\n if (engine.editor.getEditMode() === AI_EDIT_MODE) {\n const blockIds = builderContext.engine.block.findAllSelected();\n confirmationRenderFunction({\n ...builderContext,\n payload: {\n ...(builderContext.payload ?? {}),\n applyCallbacks: {\n onBefore: () => {\n blockIds.forEach((blockId) => {\n CallbacksRegistry.get()\n .get(blockId)\n .applyCallbacks?.onBefore?.();\n });\n },\n onAfter: () => {\n blockIds.forEach((blockId) => {\n CallbacksRegistry.get()\n .get(blockId)\n .applyCallbacks?.onAfter?.();\n });\n },\n onCancel: () => {\n blockIds.forEach((blockId) => {\n CallbacksRegistry.get()\n .get(blockId)\n .applyCallbacks?.onCancel?.();\n });\n },\n onApply: () => {\n blockIds.forEach((blockId) => {\n CallbacksRegistry.get()\n .get(blockId)\n .applyCallbacks?.onApply?.();\n });\n }\n },\n onCancelGeneration: () => {\n blockIds.forEach((blockId) => {\n CallbacksRegistry.get().get(blockId).onCancelGeneration?.();\n });\n }\n }\n });\n return;\n }\n\n menuRenderFunction(builderContext);\n };\n\n return {\n renderFunction: builderRenderFunction\n };\n}\n\nexport default initializeQuickActionComponents;\n", "import type CreativeEditorSDK from '@cesdk/cesdk-js';\nimport { supportsTranslateAPI, hasTranslateAPI } from '@imgly/plugin-utils';\n\n/**\n * Creates a translation callback function for AI asset sources\n * @param cesdk - The CE.SDK instance\n * @param modelKey - The model/provider key (e.g., 'fal-ai/recraft-v3')\n * @param propertyName - The property name (e.g., 'style', 'aspect_ratio')\n * @param pluginType - The plugin type (e.g., 'image', 'video', 'sticker')\n * @returns A translation callback function for use with CustomAssetSource\n */\nexport function createTranslationCallback(\n cesdk: CreativeEditorSDK,\n modelKey: string,\n propertyName: string = 'style',\n pluginType: string = 'image'\n): (assetId: string, fallbackLabel: string, locale: string) => string {\n return (assetId: string, fallbackLabel: string): string => {\n // Check if CE.SDK supports translation API\n if (!supportsTranslateAPI(cesdk)) {\n return fallbackLabel;\n }\n\n // Build translation keys following established AI plugin pattern\n const translationKeys = buildTranslationKeys(\n modelKey,\n propertyName,\n assetId,\n pluginType\n );\n\n // Use CE.SDK's translate method with fallback array\n if (hasTranslateAPI(cesdk.i18n)) {\n const translated = cesdk.i18n.translate(translationKeys);\n\n // Return translated label or fallback if no translation found\n // (CE.SDK returns the last key if no translation is found)\n return translated !== translationKeys[translationKeys.length - 1]\n ? translated\n : fallbackLabel;\n }\n\n return fallbackLabel;\n };\n}\n\n/**\n * Build translation keys array for AI plugin property values\n * @param modelKey - The model/provider key\n * @param propertyName - The property name\n * @param value - The property value\n * @param pluginType - The plugin type (image, video, sticker, etc.)\n * @returns Array of translation keys in fallback order\n */\nexport function buildTranslationKeys(\n modelKey: string,\n propertyName: string,\n value: string,\n pluginType: string = 'image'\n): string[] {\n return [\n `ly.img.plugin-ai-${pluginType}-generation-web.${modelKey}.property.${propertyName}.${value}`,\n `ly.img.plugin-ai-generation-web.property.${propertyName}.${value}`,\n `ly.img.plugin-ai-${pluginType}-generation-web.${modelKey}.defaults.property.${propertyName}.${value}`,\n `ly.img.plugin-ai-generation-web.defaults.property.${propertyName}.${value}`\n ];\n}\n", "import renderImageUrlProperty from './ui/common/renderImageUrlProperty';\nimport renderStyleTransferProperty from './ui/common/renderStyleTransferProperty';\n\nconst CommonProperties = {\n ImageUrl: renderImageUrlProperty,\n StyleTransfer: renderStyleTransferProperty\n};\n\nexport { CommonProperties };\n\nexport {\n type default as Provider,\n type ImageOutput,\n type VideoOutput,\n type TextOutput,\n type AudioOutput,\n type StickerOutput,\n type Output,\n type OutputKind,\n type PanelInputSchema,\n type RenderCustomProperty,\n type GetBlockInput,\n type GetBlockInputResult,\n type GetInput\n} from './core/provider';\nexport { type GetPropertyInput, type Property } from './openapi/types';\nexport {\n type GetProvider,\n type CommonProviderConfiguration,\n type CommonPluginConfiguration,\n type CommonConfiguration,\n type InternalPluginConfiguration\n} from './types';\n\nexport type {\n PropertyContext,\n PropertyConfig,\n PropertiesConfiguration,\n ExtendPropertyContexts\n} from './core/propertyConfiguration';\n\nexport {\n buildPropertyContext,\n PropertyContextCache\n} from './utils/propertyContext';\n\nexport {\n resolvePropertyDefault,\n resolvePropertyDefaults\n} from './utils/propertyResolver';\n\nexport { default as integrateIntoDefaultAssetLibraryEntry } from './assets/integrateIntoDefaultAssetLibraryEntry';\nexport {\n ActionRegistry,\n type PluginActionDefinition,\n type QuickActionDefinition,\n type ActionDefinition,\n type ActionRegistryEventType,\n type ActionRegistrySubscriberCallback,\n type ActionRegistryFilters\n} from './core/ActionRegistry';\nexport { ProviderRegistry } from './core/ProviderRegistry';\n\n// Export middleware\nexport { composeMiddlewares, type Middleware } from './middleware/middleware';\nexport { default as loggingMiddleware } from './middleware/loggingMiddleware';\nexport { default as uploadMiddleware } from './middleware/uploadMiddleware';\n\n// Export utilities\nexport { mergeQuickActionsConfig } from './utils/mergeQuickActionsConfig';\nexport {\n default as rateLimitMiddleware,\n type RateLimitOptions\n} from './middleware/rateLimitMiddleware';\n\nexport {\n getPanelId,\n getDurationForVideo,\n getThumbnailForVideo,\n getLabelFromId,\n isAsyncGenerator,\n addIconSetOnce\n} from './utils/utils';\n\nexport { checkAiPluginVersion } from './utils/checkAiPluginVersion';\n\nexport { default as registerDockComponent } from './ui/components/registerDockComponent';\n\nexport { default as enableQuickActionForImageFill } from './ui/quickActions/enableImageFill';\n\nexport {\n isGeneratingStateKey,\n abortGenerationStateKey\n} from './ui/components/renderGenerationComponents';\n\nexport { default as initializeProviders } from './providers/initializeProviders';\nexport { default as initializeProvider } from './providers/initializeProvider';\nexport { default as initializeQuickActionComponents } from './ui/quickActions/initializeQuickActionComponents';\n\nexport { extractAndSetSchemaTranslations } from './openapi/extractSchemaTranslations';\n\nexport { AI_EDIT_MODE, AI_METADATA_KEY } from './ui/quickActions/utils';\n\n// Export AI-specific translation helpers\nexport {\n createTranslationCallback,\n buildTranslationKeys\n} from './utils/translationHelpers';\n", "export const VERSION = '0.39.0'; // x-release-please-version\n", "/**\n * Disclaimer: modules in _shims aren't intended to be imported by SDK users.\n */\nimport { type RequestOptions } from \"../core.js\";\n\nexport interface Shims {\n kind: string;\n fetch: any;\n Request: any;\n Response: any;\n Headers: any;\n FormData: any;\n Blob: any;\n File: any;\n ReadableStream: any;\n getMultipartRequestOptions: <T = Record<string, unknown>>(\n form: Shims['FormData'],\n opts: RequestOptions<T>,\n ) => Promise<RequestOptions<T>>;\n getDefaultAgent: (url: string) => any;\n fileFromPath:\n | ((path: string, filename?: string, options?: {}) => Promise<Shims['File']>)\n | ((path: string, options?: {}) => Promise<Shims['File']>);\n isFsReadStream: (value: any) => boolean;\n}\n\nexport let auto = false;\nexport let kind: Shims['kind'] | undefined = undefined;\nexport let fetch: Shims['fetch'] | undefined = undefined;\nexport let Request: Shims['Request'] | undefined = undefined;\nexport let Response: Shims['Response'] | undefined = undefined;\nexport let Headers: Shims['Headers'] | undefined = undefined;\nexport let FormData: Shims['FormData'] | undefined = undefined;\nexport let Blob: Shims['Blob'] | undefined = undefined;\nexport let File: Shims['File'] | undefined = undefined;\nexport let ReadableStream: Shims['ReadableStream'] | undefined = undefined;\nexport let getMultipartRequestOptions: Shims['getMultipartRequestOptions'] | undefined = undefined;\nexport let getDefaultAgent: Shims['getDefaultAgent'] | undefined = undefined;\nexport let fileFromPath: Shims['fileFromPath'] | undefined = undefined;\nexport let isFsReadStream: Shims['isFsReadStream'] | undefined = undefined;\n\nexport function setShims(shims: Shims, options: { auto: boolean } = { auto: false }) {\n if (auto) {\n throw new Error(\n `you must \\`import '@anthropic-ai/sdk/shims/${shims.kind}'\\` before importing anything else from @anthropic-ai/sdk`,\n );\n }\n if (kind) {\n throw new Error(\n `can't \\`import '@anthropic-ai/sdk/shims/${shims.kind}'\\` after \\`import '@anthropic-ai/sdk/shims/${kind}'\\``,\n );\n }\n auto = options.auto;\n kind = shims.kind;\n fetch = shims.fetch;\n Request = shims.Request;\n Response = shims.Response;\n Headers = shims.Headers;\n FormData = shims.FormData;\n Blob = shims.Blob;\n File = shims.File;\n ReadableStream = shims.ReadableStream;\n getMultipartRequestOptions = shims.getMultipartRequestOptions;\n getDefaultAgent = shims.getDefaultAgent;\n fileFromPath = shims.fileFromPath;\n isFsReadStream = shims.isFsReadStream;\n}\n", "/**\n * Disclaimer: modules in _shims aren't intended to be imported by SDK users.\n */\nexport class MultipartBody {\n constructor(public body: any) {}\n get [Symbol.toStringTag](): string {\n return 'MultipartBody';\n }\n}\n", "/**\n * Disclaimer: modules in _shims aren't intended to be imported by SDK users.\n */\nimport { MultipartBody } from \"./MultipartBody.js\";\nimport { type RequestOptions } from \"../core.js\";\nimport { type Shims } from \"./registry.js\";\n\nexport function getRuntime({ manuallyImported }: { manuallyImported?: boolean } = {}): Shims {\n const recommendation =\n manuallyImported ?\n `You may need to use polyfills`\n : `Add one of these imports before your first \\`import \u2026 from '@anthropic-ai/sdk'\\`:\n- \\`import '@anthropic-ai/sdk/shims/node'\\` (if you're running on Node)\n- \\`import '@anthropic-ai/sdk/shims/web'\\` (otherwise)\n`;\n\n let _fetch, _Request, _Response, _Headers;\n try {\n // @ts-ignore\n _fetch = fetch;\n // @ts-ignore\n _Request = Request;\n // @ts-ignore\n _Response = Response;\n // @ts-ignore\n _Headers = Headers;\n } catch (error) {\n throw new Error(\n `this environment is missing the following Web Fetch API type: ${\n (error as any).message\n }. ${recommendation}`,\n );\n }\n\n return {\n kind: 'web',\n fetch: _fetch,\n Request: _Request,\n Response: _Response,\n Headers: _Headers,\n FormData:\n // @ts-ignore\n typeof FormData !== 'undefined' ? FormData : (\n class FormData {\n // @ts-ignore\n constructor() {\n throw new Error(\n `file uploads aren't supported in this environment yet as 'FormData' is undefined. ${recommendation}`,\n );\n }\n }\n ),\n Blob:\n typeof Blob !== 'undefined' ? Blob : (\n class Blob {\n constructor() {\n throw new Error(\n `file uploads aren't supported in this environment yet as 'Blob' is undefined. ${recommendation}`,\n );\n }\n }\n ),\n File:\n // @ts-ignore\n typeof File !== 'undefined' ? File : (\n class File {\n // @ts-ignore\n constructor() {\n throw new Error(\n `file uploads aren't supported in this environment yet as 'File' is undefined. ${recommendation}`,\n );\n }\n }\n ),\n ReadableStream:\n // @ts-ignore\n typeof ReadableStream !== 'undefined' ? ReadableStream : (\n class ReadableStream {\n // @ts-ignore\n constructor() {\n throw new Error(\n `streaming isn't supported in this environment yet as 'ReadableStream' is undefined. ${recommendation}`,\n );\n }\n }\n ),\n getMultipartRequestOptions: async <T = Record<string, unknown>>(\n // @ts-ignore\n form: FormData,\n opts: RequestOptions<T>,\n ): Promise<RequestOptions<T>> => ({\n ...opts,\n body: new MultipartBody(form) as any,\n }),\n getDefaultAgent: (url: string) => undefined,\n fileFromPath: () => {\n throw new Error(\n 'The `fileFromPath` function is only supported in Node. See the README for more details: https://www.github.com/anthropics/anthropic-sdk-typescript#file-uploads',\n );\n },\n isFsReadStream: (value: any) => false,\n };\n}\n", "/**\n * Disclaimer: modules in _shims aren't intended to be imported by SDK users.\n */\nimport * as shims from './registry.mjs';\nimport * as auto from '@anthropic-ai/sdk/_shims/auto/runtime';\nif (!shims.kind) shims.setShims(auto.getRuntime(), { auto: true });\nexport * from './registry.mjs';\n", "// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\n\nimport { castToError, Headers } from \"./core.js\";\n\nexport class AnthropicError extends Error {}\n\nexport class APIError<\n TStatus extends number | undefined = number | undefined,\n THeaders extends Headers | undefined = Headers | undefined,\n TError extends Object | undefined = Object | undefined,\n> extends AnthropicError {\n /** HTTP status for the response that caused the error */\n readonly status: TStatus;\n /** HTTP headers for the response that caused the error */\n readonly headers: THeaders;\n /** JSON body of the response that caused the error */\n readonly error: TError;\n\n readonly request_id: string | null | undefined;\n\n constructor(status: TStatus, error: TError, message: string | undefined, headers: THeaders) {\n super(`${APIError.makeMessage(status, error, message)}`);\n this.status = status;\n this.headers = headers;\n this.request_id = headers?.['request-id'];\n this.error = error;\n }\n\n private static makeMessage(status: number | undefined, error: any, message: string | undefined) {\n const msg =\n error?.message ?\n typeof error.message === 'string' ?\n error.message\n : JSON.stringify(error.message)\n : error ? JSON.stringify(error)\n : message;\n\n if (status && msg) {\n return `${status} ${msg}`;\n }\n if (status) {\n return `${status} status code (no body)`;\n }\n if (msg) {\n return msg;\n }\n return '(no status code or body)';\n }\n\n static generate(\n status: number | undefined,\n errorResponse: Object | undefined,\n message: string | undefined,\n headers: Headers | undefined,\n ): APIError {\n if (!status || !headers) {\n return new APIConnectionError({ message, cause: castToError(errorResponse) });\n }\n\n const error = errorResponse as Record<string, any>;\n\n if (status === 400) {\n return new BadRequestError(status, error, message, headers);\n }\n\n if (status === 401) {\n return new AuthenticationError(status, error, message, headers);\n }\n\n if (status === 403) {\n return new PermissionDeniedError(status, error, message, headers);\n }\n\n if (status === 404) {\n return new NotFoundError(status, error, message, headers);\n }\n\n if (status === 409) {\n return new ConflictError(status, error, message, headers);\n }\n\n if (status === 422) {\n return new UnprocessableEntityError(status, error, message, headers);\n }\n\n if (status === 429) {\n return new RateLimitError(status, error, message, headers);\n }\n\n if (status >= 500) {\n return new InternalServerError(status, error, message, headers);\n }\n\n return new APIError(status, error, message, headers);\n }\n}\n\nexport class APIUserAbortError extends APIError<undefined, undefined, undefined> {\n constructor({ message }: { message?: string } = {}) {\n super(undefined, undefined, message || 'Request was aborted.', undefined);\n }\n}\n\nexport class APIConnectionError extends APIError<undefined, undefined, undefined> {\n constructor({ message, cause }: { message?: string | undefined; cause?: Error | undefined }) {\n super(undefined, undefined, message || 'Connection error.', undefined);\n // in some environments the 'cause' property is already declared\n // @ts-ignore\n if (cause) this.cause = cause;\n }\n}\n\nexport class APIConnectionTimeoutError extends APIConnectionError {\n constructor({ message }: { message?: string } = {}) {\n super({ message: message ?? 'Request timed out.' });\n }\n}\n\nexport class BadRequestError extends APIError<400, Headers> {}\n\nexport class AuthenticationError extends APIError<401, Headers> {}\n\nexport class PermissionDeniedError extends APIError<403, Headers> {}\n\nexport class NotFoundError extends APIError<404, Headers> {}\n\nexport class ConflictError extends APIError<409, Headers> {}\n\nexport class UnprocessableEntityError extends APIError<422, Headers> {}\n\nexport class RateLimitError extends APIError<429, Headers> {}\n\nexport class InternalServerError extends APIError<number, Headers> {}\n", "import { AnthropicError } from \"../../error.js\";\n\nexport type Bytes = string | ArrayBuffer | Uint8Array | Buffer | null | undefined;\n\n/**\n * A re-implementation of httpx's `LineDecoder` in Python that handles incrementally\n * reading lines from text.\n *\n * https://github.com/encode/httpx/blob/920333ea98118e9cf617f246905d7b202510941c/httpx/_decoders.py#L258\n */\nexport class LineDecoder {\n // prettier-ignore\n static NEWLINE_CHARS = new Set(['\\n', '\\r']);\n static NEWLINE_REGEXP = /\\r\\n|[\\n\\r]/g;\n\n buffer: Uint8Array;\n #carriageReturnIndex: number | null;\n textDecoder: any; // TextDecoder found in browsers; not typed to avoid pulling in either \"dom\" or \"node\" types.\n\n constructor() {\n this.buffer = new Uint8Array();\n this.#carriageReturnIndex = null;\n }\n\n decode(chunk: Bytes): string[] {\n if (chunk == null) {\n return [];\n }\n\n const binaryChunk =\n chunk instanceof ArrayBuffer ? new Uint8Array(chunk)\n : typeof chunk === 'string' ? new TextEncoder().encode(chunk)\n : chunk;\n\n let newData = new Uint8Array(this.buffer.length + binaryChunk.length);\n newData.set(this.buffer);\n newData.set(binaryChunk, this.buffer.length);\n this.buffer = newData;\n\n const lines: string[] = [];\n let patternIndex;\n while ((patternIndex = findNewlineIndex(this.buffer, this.#carriageReturnIndex)) != null) {\n if (patternIndex.carriage && this.#carriageReturnIndex == null) {\n // skip until we either get a corresponding `\\n`, a new `\\r` or nothing\n this.#carriageReturnIndex = patternIndex.index;\n continue;\n }\n\n // we got double \\r or \\rtext\\n\n if (\n this.#carriageReturnIndex != null &&\n (patternIndex.index !== this.#carriageReturnIndex + 1 || patternIndex.carriage)\n ) {\n lines.push(this.decodeText(this.buffer.slice(0, this.#carriageReturnIndex - 1)));\n this.buffer = this.buffer.slice(this.#carriageReturnIndex);\n this.#carriageReturnIndex = null;\n continue;\n }\n\n const endIndex =\n this.#carriageReturnIndex !== null ? patternIndex.preceding - 1 : patternIndex.preceding;\n\n const line = this.decodeText(this.buffer.slice(0, endIndex));\n lines.push(line);\n\n this.buffer = this.buffer.slice(patternIndex.index);\n this.#carriageReturnIndex = null;\n }\n\n return lines;\n }\n\n decodeText(bytes: Bytes): string {\n if (bytes == null) return '';\n if (typeof bytes === 'string') return bytes;\n\n // Node:\n if (typeof Buffer !== 'undefined') {\n if (bytes instanceof Buffer) {\n return bytes.toString();\n }\n if (bytes instanceof Uint8Array) {\n return Buffer.from(bytes).toString();\n }\n\n throw new AnthropicError(\n `Unexpected: received non-Uint8Array (${bytes.constructor.name}) stream chunk in an environment with a global \"Buffer\" defined, which this library assumes to be Node. Please report this error.`,\n );\n }\n\n // Browser\n if (typeof TextDecoder !== 'undefined') {\n if (bytes instanceof Uint8Array || bytes instanceof ArrayBuffer) {\n this.textDecoder ??= new TextDecoder('utf8');\n return this.textDecoder.decode(bytes);\n }\n\n throw new AnthropicError(\n `Unexpected: received non-Uint8Array/ArrayBuffer (${\n (bytes as any).constructor.name\n }) in a web platform. Please report this error.`,\n );\n }\n\n throw new AnthropicError(\n `Unexpected: neither Buffer nor TextDecoder are available as globals. Please report this error.`,\n );\n }\n\n flush(): string[] {\n if (!this.buffer.length) {\n return [];\n }\n return this.decode('\\n');\n }\n}\n\n/**\n * This function searches the buffer for the end patterns, (\\r or \\n)\n * and returns an object with the index preceding the matched newline and the\n * index after the newline char. `null` is returned if no new line is found.\n *\n * ```ts\n * findNewLineIndex('abc\\ndef') -> { preceding: 2, index: 3 }\n * ```\n */\nfunction findNewlineIndex(\n buffer: Uint8Array,\n startIndex: number | null,\n): { preceding: number; index: number; carriage: boolean } | null {\n const newline = 0x0a; // \\n\n const carriage = 0x0d; // \\r\n\n for (let i = startIndex ?? 0; i < buffer.length; i++) {\n if (buffer[i] === newline) {\n return { preceding: i, index: i + 1, carriage: false };\n }\n\n if (buffer[i] === carriage) {\n return { preceding: i, index: i + 1, carriage: true };\n }\n }\n\n return null;\n}\n\nexport function findDoubleNewlineIndex(buffer: Uint8Array): number {\n // This function searches the buffer for the end patterns (\\r\\r, \\n\\n, \\r\\n\\r\\n)\n // and returns the index right after the first occurrence of any pattern,\n // or -1 if none of the patterns are found.\n const newline = 0x0a; // \\n\n const carriage = 0x0d; // \\r\n\n for (let i = 0; i < buffer.length - 1; i++) {\n if (buffer[i] === newline && buffer[i + 1] === newline) {\n // \\n\\n\n return i + 2;\n }\n if (buffer[i] === carriage && buffer[i + 1] === carriage) {\n // \\r\\r\n return i + 2;\n }\n if (\n buffer[i] === carriage &&\n buffer[i + 1] === newline &&\n i + 3 < buffer.length &&\n buffer[i + 2] === carriage &&\n buffer[i + 3] === newline\n ) {\n // \\r\\n\\r\\n\n return i + 4;\n }\n }\n\n return -1;\n}\n", "/**\n * Most browsers don't yet have async iterable support for ReadableStream,\n * and Node has a very different way of reading bytes from its \"ReadableStream\".\n *\n * This polyfill was pulled from https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1627354490\n */\nexport function ReadableStreamToAsyncIterable<T>(stream: any): AsyncIterableIterator<T> {\n if (stream[Symbol.asyncIterator]) return stream;\n\n const reader = stream.getReader();\n return {\n async next() {\n try {\n const result = await reader.read();\n if (result?.done) reader.releaseLock(); // release lock when stream becomes closed\n return result;\n } catch (e) {\n reader.releaseLock(); // release lock when stream becomes errored\n throw e;\n }\n },\n async return() {\n const cancelPromise = reader.cancel();\n reader.releaseLock();\n await cancelPromise;\n return { done: true, value: undefined };\n },\n [Symbol.asyncIterator]() {\n return this;\n },\n };\n}\n", "import { ReadableStream, type Response } from \"./_shims/index.js\";\nimport { AnthropicError } from \"./error.js\";\nimport { findDoubleNewlineIndex, LineDecoder } from \"./internal/decoders/line.js\";\nimport { ReadableStreamToAsyncIterable } from \"./internal/stream-utils.js\";\n\nimport { createResponseHeaders } from \"./core.js\";\nimport { APIError } from \"./error.js\";\n\ntype Bytes = string | ArrayBuffer | Uint8Array | Buffer | null | undefined;\n\nexport type ServerSentEvent = {\n event: string | null;\n data: string;\n raw: string[];\n};\n\nexport class Stream<Item> implements AsyncIterable<Item> {\n controller: AbortController;\n\n constructor(\n private iterator: () => AsyncIterator<Item>,\n controller: AbortController,\n ) {\n this.controller = controller;\n }\n\n static fromSSEResponse<Item>(response: Response, controller: AbortController): Stream<Item> {\n let consumed = false;\n\n async function* iterator(): AsyncIterator<Item, any, undefined> {\n if (consumed) {\n throw new Error('Cannot iterate over a consumed stream, use `.tee()` to split the stream.');\n }\n consumed = true;\n let done = false;\n try {\n for await (const sse of _iterSSEMessages(response, controller)) {\n if (sse.event === 'completion') {\n try {\n yield JSON.parse(sse.data);\n } catch (e) {\n console.error(`Could not parse message into JSON:`, sse.data);\n console.error(`From chunk:`, sse.raw);\n throw e;\n }\n }\n\n if (\n sse.event === 'message_start' ||\n sse.event === 'message_delta' ||\n sse.event === 'message_stop' ||\n sse.event === 'content_block_start' ||\n sse.event === 'content_block_delta' ||\n sse.event === 'content_block_stop'\n ) {\n try {\n yield JSON.parse(sse.data);\n } catch (e) {\n console.error(`Could not parse message into JSON:`, sse.data);\n console.error(`From chunk:`, sse.raw);\n throw e;\n }\n }\n\n if (sse.event === 'ping') {\n continue;\n }\n\n if (sse.event === 'error') {\n throw APIError.generate(\n undefined,\n `SSE Error: ${sse.data}`,\n sse.data,\n createResponseHeaders(response.headers),\n );\n }\n }\n done = true;\n } catch (e) {\n // If the user calls `stream.controller.abort()`, we should exit without throwing.\n if (e instanceof Error && e.name === 'AbortError') return;\n throw e;\n } finally {\n // If the user `break`s, abort the ongoing request.\n if (!done) controller.abort();\n }\n }\n\n return new Stream(iterator, controller);\n }\n\n /**\n * Generates a Stream from a newline-separated ReadableStream\n * where each item is a JSON value.\n */\n static fromReadableStream<Item>(readableStream: ReadableStream, controller: AbortController): Stream<Item> {\n let consumed = false;\n\n async function* iterLines(): AsyncGenerator<string, void, unknown> {\n const lineDecoder = new LineDecoder();\n\n const iter = ReadableStreamToAsyncIterable<Bytes>(readableStream);\n for await (const chunk of iter) {\n for (const line of lineDecoder.decode(chunk)) {\n yield line;\n }\n }\n\n for (const line of lineDecoder.flush()) {\n yield line;\n }\n }\n\n async function* iterator(): AsyncIterator<Item, any, undefined> {\n if (consumed) {\n throw new Error('Cannot iterate over a consumed stream, use `.tee()` to split the stream.');\n }\n consumed = true;\n let done = false;\n try {\n for await (const line of iterLines()) {\n if (done) continue;\n if (line) yield JSON.parse(line);\n }\n done = true;\n } catch (e) {\n // If the user calls `stream.controller.abort()`, we should exit without throwing.\n if (e instanceof Error && e.name === 'AbortError') return;\n throw e;\n } finally {\n // If the user `break`s, abort the ongoing request.\n if (!done) controller.abort();\n }\n }\n\n return new Stream(iterator, controller);\n }\n\n [Symbol.asyncIterator](): AsyncIterator<Item> {\n return this.iterator();\n }\n\n /**\n * Splits the stream into two streams which can be\n * independently read from at different speeds.\n */\n tee(): [Stream<Item>, Stream<Item>] {\n const left: Array<Promise<IteratorResult<Item>>> = [];\n const right: Array<Promise<IteratorResult<Item>>> = [];\n const iterator = this.iterator();\n\n const teeIterator = (queue: Array<Promise<IteratorResult<Item>>>): AsyncIterator<Item> => {\n return {\n next: () => {\n if (queue.length === 0) {\n const result = iterator.next();\n left.push(result);\n right.push(result);\n }\n return queue.shift()!;\n },\n };\n };\n\n return [\n new Stream(() => teeIterator(left), this.controller),\n new Stream(() => teeIterator(right), this.controller),\n ];\n }\n\n /**\n * Converts this stream to a newline-separated ReadableStream of\n * JSON stringified values in the stream\n * which can be turned back into a Stream with `Stream.fromReadableStream()`.\n */\n toReadableStream(): ReadableStream {\n const self = this;\n let iter: AsyncIterator<Item>;\n const encoder = new TextEncoder();\n\n return new ReadableStream({\n async start() {\n iter = self[Symbol.asyncIterator]();\n },\n async pull(ctrl: any) {\n try {\n const { value, done } = await iter.next();\n if (done) return ctrl.close();\n\n const bytes = encoder.encode(JSON.stringify(value) + '\\n');\n\n ctrl.enqueue(bytes);\n } catch (err) {\n ctrl.error(err);\n }\n },\n async cancel() {\n await iter.return?.();\n },\n });\n }\n}\n\nexport async function* _iterSSEMessages(\n response: Response,\n controller: AbortController,\n): AsyncGenerator<ServerSentEvent, void, unknown> {\n if (!response.body) {\n controller.abort();\n throw new AnthropicError(`Attempted to iterate over a response with no body`);\n }\n\n const sseDecoder = new SSEDecoder();\n const lineDecoder = new LineDecoder();\n\n const iter = ReadableStreamToAsyncIterable<Bytes>(response.body);\n for await (const sseChunk of iterSSEChunks(iter)) {\n for (const line of lineDecoder.decode(sseChunk)) {\n const sse = sseDecoder.decode(line);\n if (sse) yield sse;\n }\n }\n\n for (const line of lineDecoder.flush()) {\n const sse = sseDecoder.decode(line);\n if (sse) yield sse;\n }\n}\n\n/**\n * Given an async iterable iterator, iterates over it and yields full\n * SSE chunks, i.e. yields when a double new-line is encountered.\n */\nasync function* iterSSEChunks(iterator: AsyncIterableIterator<Bytes>): AsyncGenerator<Uint8Array> {\n let data = new Uint8Array();\n\n for await (const chunk of iterator) {\n if (chunk == null) {\n continue;\n }\n\n const binaryChunk =\n chunk instanceof ArrayBuffer ? new Uint8Array(chunk)\n : typeof chunk === 'string' ? new TextEncoder().encode(chunk)\n : chunk;\n\n let newData = new Uint8Array(data.length + binaryChunk.length);\n newData.set(data);\n newData.set(binaryChunk, data.length);\n data = newData;\n\n let patternIndex;\n while ((patternIndex = findDoubleNewlineIndex(data)) !== -1) {\n yield data.slice(0, patternIndex);\n data = data.slice(patternIndex);\n }\n }\n\n if (data.length > 0) {\n yield data;\n }\n}\n\nclass SSEDecoder {\n private data: string[];\n private event: string | null;\n private chunks: string[];\n\n constructor() {\n this.event = null;\n this.data = [];\n this.chunks = [];\n }\n\n decode(line: string) {\n if (line.endsWith('\\r')) {\n line = line.substring(0, line.length - 1);\n }\n\n if (!line) {\n // empty line and we didn't previously encounter any messages\n if (!this.event && !this.data.length) return null;\n\n const sse: ServerSentEvent = {\n event: this.event,\n data: this.data.join('\\n'),\n raw: this.chunks,\n };\n\n this.event = null;\n this.data = [];\n this.chunks = [];\n\n return sse;\n }\n\n this.chunks.push(line);\n\n if (line.startsWith(':')) {\n return null;\n }\n\n let [fieldname, _, value] = partition(line, ':');\n\n if (value.startsWith(' ')) {\n value = value.substring(1);\n }\n\n if (fieldname === 'event') {\n this.event = value;\n } else if (fieldname === 'data') {\n this.data.push(value);\n }\n\n return null;\n }\n}\n\nfunction partition(str: string, delimiter: string): [string, string, string] {\n const index = str.indexOf(delimiter);\n if (index !== -1) {\n return [str.substring(0, index), delimiter, str.substring(index + delimiter.length)];\n }\n\n return [str, '', ''];\n}\n", "import { type RequestOptions } from \"./core.js\";\nimport {\n FormData,\n File,\n type Blob,\n type FilePropertyBag,\n getMultipartRequestOptions,\n type FsReadStream,\n isFsReadStream,\n} from \"./_shims/index.js\";\nimport { MultipartBody } from \"./_shims/MultipartBody.js\";\nexport { fileFromPath } from \"./_shims/index.js\";\n\ntype BlobLikePart = string | ArrayBuffer | ArrayBufferView | BlobLike | Uint8Array | DataView;\nexport type BlobPart = string | ArrayBuffer | ArrayBufferView | Blob | Uint8Array | DataView;\n\n/**\n * Typically, this is a native \"File\" class.\n *\n * We provide the {@link toFile} utility to convert a variety of objects\n * into the File class.\n *\n * For convenience, you can also pass a fetch Response, or in Node,\n * the result of fs.createReadStream().\n */\nexport type Uploadable = FileLike | ResponseLike | FsReadStream;\n\n/**\n * Intended to match web.Blob, node.Blob, node-fetch.Blob, etc.\n */\nexport interface BlobLike {\n /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/size) */\n readonly size: number;\n /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/type) */\n readonly type: string;\n /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/text) */\n text(): Promise<string>;\n /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/slice) */\n slice(start?: number, end?: number): BlobLike;\n // unfortunately @types/node-fetch@^2.6.4 doesn't type the arrayBuffer method\n}\n\n/**\n * Intended to match web.File, node.File, node-fetch.File, etc.\n */\nexport interface FileLike extends BlobLike {\n /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/lastModified) */\n readonly lastModified: number;\n /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/name) */\n readonly name: string;\n}\n\n/**\n * Intended to match web.Response, node.Response, node-fetch.Response, etc.\n */\nexport interface ResponseLike {\n url: string;\n blob(): Promise<BlobLike>;\n}\n\nexport const isResponseLike = (value: any): value is ResponseLike =>\n value != null &&\n typeof value === 'object' &&\n typeof value.url === 'string' &&\n typeof value.blob === 'function';\n\nexport const isFileLike = (value: any): value is FileLike =>\n value != null &&\n typeof value === 'object' &&\n typeof value.name === 'string' &&\n typeof value.lastModified === 'number' &&\n isBlobLike(value);\n\n/**\n * The BlobLike type omits arrayBuffer() because @types/node-fetch@^2.6.4 lacks it; but this check\n * adds the arrayBuffer() method type because it is available and used at runtime\n */\nexport const isBlobLike = (value: any): value is BlobLike & { arrayBuffer(): Promise<ArrayBuffer> } =>\n value != null &&\n typeof value === 'object' &&\n typeof value.size === 'number' &&\n typeof value.type === 'string' &&\n typeof value.text === 'function' &&\n typeof value.slice === 'function' &&\n typeof value.arrayBuffer === 'function';\n\nexport const isUploadable = (value: any): value is Uploadable => {\n return isFileLike(value) || isResponseLike(value) || isFsReadStream(value);\n};\n\nexport type ToFileInput = Uploadable | Exclude<BlobLikePart, string> | AsyncIterable<BlobLikePart>;\n\n/**\n * Helper for creating a {@link File} to pass to an SDK upload method from a variety of different data formats\n * @param value the raw content of the file. Can be an {@link Uploadable}, {@link BlobLikePart}, or {@link AsyncIterable} of {@link BlobLikePart}s\n * @param {string=} name the name of the file. If omitted, toFile will try to determine a file name from bits if possible\n * @param {Object=} options additional properties\n * @param {string=} options.type the MIME type of the content\n * @param {number=} options.lastModified the last modified timestamp\n * @returns a {@link File} with the given properties\n */\nexport async function toFile(\n value: ToFileInput | PromiseLike<ToFileInput>,\n name?: string | null | undefined,\n options?: FilePropertyBag | undefined,\n): Promise<FileLike> {\n // If it's a promise, resolve it.\n value = await value;\n\n // If we've been given a `File` we don't need to do anything\n if (isFileLike(value)) {\n return value;\n }\n\n if (isResponseLike(value)) {\n const blob = await value.blob();\n name ||= new URL(value.url).pathname.split(/[\\\\/]/).pop() ?? 'unknown_file';\n\n // we need to convert the `Blob` into an array buffer because the `Blob` class\n // that `node-fetch` defines is incompatible with the web standard which results\n // in `new File` interpreting it as a string instead of binary data.\n const data = isBlobLike(blob) ? [(await blob.arrayBuffer()) as any] : [blob];\n\n return new File(data, name, options);\n }\n\n const bits = await getBytes(value);\n\n name ||= getName(value) ?? 'unknown_file';\n\n if (!options?.type) {\n const type = (bits[0] as any)?.type;\n if (typeof type === 'string') {\n options = { ...options, type };\n }\n }\n\n return new File(bits, name, options);\n}\n\nasync function getBytes(value: ToFileInput): Promise<Array<BlobPart>> {\n let parts: Array<BlobPart> = [];\n if (\n typeof value === 'string' ||\n ArrayBuffer.isView(value) || // includes Uint8Array, Buffer, etc.\n value instanceof ArrayBuffer\n ) {\n parts.push(value);\n } else if (isBlobLike(value)) {\n parts.push(await value.arrayBuffer());\n } else if (\n isAsyncIterableIterator(value) // includes Readable, ReadableStream, etc.\n ) {\n for await (const chunk of value) {\n parts.push(chunk as BlobPart); // TODO, consider validating?\n }\n } else {\n throw new Error(\n `Unexpected data type: ${typeof value}; constructor: ${value?.constructor\n ?.name}; props: ${propsForError(value)}`,\n );\n }\n\n return parts;\n}\n\nfunction propsForError(value: any): string {\n const props = Object.getOwnPropertyNames(value);\n return `[${props.map((p) => `\"${p}\"`).join(', ')}]`;\n}\n\nfunction getName(value: any): string | undefined {\n return (\n getStringFromMaybeBuffer(value.name) ||\n getStringFromMaybeBuffer(value.filename) ||\n // For fs.ReadStream\n getStringFromMaybeBuffer(value.path)?.split(/[\\\\/]/).pop()\n );\n}\n\nconst getStringFromMaybeBuffer = (x: string | Buffer | unknown): string | undefined => {\n if (typeof x === 'string') return x;\n if (typeof Buffer !== 'undefined' && x instanceof Buffer) return String(x);\n return undefined;\n};\n\nconst isAsyncIterableIterator = (value: any): value is AsyncIterableIterator<unknown> =>\n value != null && typeof value === 'object' && typeof value[Symbol.asyncIterator] === 'function';\n\nexport const isMultipartBody = (body: any): body is MultipartBody =>\n body && typeof body === 'object' && body.body && body[Symbol.toStringTag] === 'MultipartBody';\n\n/**\n * Returns a multipart/form-data request if any part of the given request body contains a File / Blob value.\n * Otherwise returns the request as is.\n */\nexport const maybeMultipartFormRequestOptions = async <T = Record<string, unknown>>(\n opts: RequestOptions<T>,\n): Promise<RequestOptions<T | MultipartBody>> => {\n if (!hasUploadableValue(opts.body)) return opts;\n\n const form = await createForm(opts.body);\n return getMultipartRequestOptions(form, opts);\n};\n\nexport const multipartFormRequestOptions = async <T = Record<string, unknown>>(\n opts: RequestOptions<T>,\n): Promise<RequestOptions<T | MultipartBody>> => {\n const form = await createForm(opts.body);\n return getMultipartRequestOptions(form, opts);\n};\n\nexport const createForm = async <T = Record<string, unknown>>(body: T | undefined): Promise<FormData> => {\n const form = new FormData();\n await Promise.all(Object.entries(body || {}).map(([key, value]) => addFormValue(form, key, value)));\n return form;\n};\n\nconst hasUploadableValue = (value: unknown): boolean => {\n if (isUploadable(value)) return true;\n if (Array.isArray(value)) return value.some(hasUploadableValue);\n if (value && typeof value === 'object') {\n for (const k in value) {\n if (hasUploadableValue((value as any)[k])) return true;\n }\n }\n return false;\n};\n\nconst addFormValue = async (form: FormData, key: string, value: unknown): Promise<void> => {\n if (value === undefined) return;\n if (value == null) {\n throw new TypeError(\n `Received null for \"${key}\"; to pass null in FormData, you must use the string 'null'`,\n );\n }\n\n // TODO: make nested formats configurable\n if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {\n form.append(key, String(value));\n } else if (isUploadable(value)) {\n const file = await toFile(value);\n form.append(key, file as File);\n } else if (Array.isArray(value)) {\n await Promise.all(value.map((entry) => addFormValue(form, key + '[]', entry)));\n } else if (typeof value === 'object') {\n await Promise.all(\n Object.entries(value).map(([name, prop]) => addFormValue(form, `${key}[${name}]`, prop)),\n );\n } else {\n throw new TypeError(\n `Invalid value given to form, expected a string, number, boolean, object, Array, File or Blob but got ${value} instead`,\n );\n }\n};\n", "import { VERSION } from \"./version.js\";\nimport { Stream } from \"./streaming.js\";\nimport {\n AnthropicError,\n APIError,\n APIConnectionError,\n APIConnectionTimeoutError,\n APIUserAbortError,\n} from \"./error.js\";\nimport {\n kind as shimsKind,\n type Readable,\n getDefaultAgent,\n type Agent,\n fetch,\n type RequestInfo,\n type RequestInit,\n type Response,\n type HeadersInit,\n} from \"./_shims/index.js\";\nexport { type Response };\nimport { BlobLike, isBlobLike, isMultipartBody } from \"./uploads.js\";\nexport {\n maybeMultipartFormRequestOptions,\n multipartFormRequestOptions,\n createForm,\n type Uploadable,\n} from \"./uploads.js\";\n\nexport type Fetch = (url: RequestInfo, init?: RequestInit) => Promise<Response>;\n\ntype PromiseOrValue<T> = T | Promise<T>;\n\ntype APIResponseProps = {\n response: Response;\n options: FinalRequestOptions;\n controller: AbortController;\n};\n\nasync function defaultParseResponse<T>(props: APIResponseProps): Promise<WithRequestID<T>> {\n const { response } = props;\n if (props.options.stream) {\n debug('response', response.status, response.url, response.headers, response.body);\n\n // Note: there is an invariant here that isn't represented in the type system\n // that if you set `stream: true` the response type must also be `Stream<T>`\n\n if (props.options.__streamClass) {\n return props.options.__streamClass.fromSSEResponse(response, props.controller) as any;\n }\n\n return Stream.fromSSEResponse(response, props.controller) as any;\n }\n\n // fetch refuses to read the body when the status code is 204.\n if (response.status === 204) {\n return null as WithRequestID<T>;\n }\n\n if (props.options.__binaryResponse) {\n return response as unknown as WithRequestID<T>;\n }\n\n const contentType = response.headers.get('content-type');\n const isJSON =\n contentType?.includes('application/json') || contentType?.includes('application/vnd.api+json');\n if (isJSON) {\n const json = await response.json();\n\n debug('response', response.status, response.url, response.headers, json);\n\n return _addRequestID(json as T, response);\n }\n\n const text = await response.text();\n debug('response', response.status, response.url, response.headers, text);\n\n // TODO handle blob, arraybuffer, other content types, etc.\n return text as unknown as WithRequestID<T>;\n}\n\ntype WithRequestID<T> =\n T extends Array<any> | Response | AbstractPage<any> ? T\n : T extends Record<string, any> ? T & { _request_id?: string | null }\n : T;\n\nfunction _addRequestID<T>(value: T, response: Response): WithRequestID<T> {\n if (!value || typeof value !== 'object' || Array.isArray(value)) {\n return value as WithRequestID<T>;\n }\n\n return Object.defineProperty(value, '_request_id', {\n value: response.headers.get('request-id'),\n enumerable: false,\n }) as WithRequestID<T>;\n}\n\n/**\n * A subclass of `Promise` providing additional helper methods\n * for interacting with the SDK.\n */\nexport class APIPromise<T> extends Promise<WithRequestID<T>> {\n private parsedPromise: Promise<WithRequestID<T>> | undefined;\n\n constructor(\n private responsePromise: Promise<APIResponseProps>,\n private parseResponse: (\n props: APIResponseProps,\n ) => PromiseOrValue<WithRequestID<T>> = defaultParseResponse,\n ) {\n super((resolve) => {\n // this is maybe a bit weird but this has to be a no-op to not implicitly\n // parse the response body; instead .then, .catch, .finally are overridden\n // to parse the response\n resolve(null as any);\n });\n }\n\n _thenUnwrap<U>(transform: (data: T, props: APIResponseProps) => U): APIPromise<U> {\n return new APIPromise(this.responsePromise, async (props) =>\n _addRequestID(transform(await this.parseResponse(props), props), props.response),\n );\n }\n\n /**\n * Gets the raw `Response` instance instead of parsing the response\n * data.\n *\n * If you want to parse the response body but still get the `Response`\n * instance, you can use {@link withResponse()}.\n *\n * \uD83D\uDC4B Getting the wrong TypeScript type for `Response`?\n * Try setting `\"moduleResolution\": \"NodeNext\"` if you can,\n * or add one of these imports before your first `import \u2026 from '@anthropic-ai/sdk'`:\n * - `import '@anthropic-ai/sdk/shims/node'` (if you're running on Node)\n * - `import '@anthropic-ai/sdk/shims/web'` (otherwise)\n */\n asResponse(): Promise<Response> {\n return this.responsePromise.then((p) => p.response);\n }\n\n /**\n * Gets the parsed response data, the raw `Response` instance and the ID of the request,\n * returned vie the `request-id` header which is useful for debugging requests and resporting\n * issues to Anthropic.\n *\n * If you just want to get the raw `Response` instance without parsing it,\n * you can use {@link asResponse()}.\n *\n * \uD83D\uDC4B Getting the wrong TypeScript type for `Response`?\n * Try setting `\"moduleResolution\": \"NodeNext\"` if you can,\n * or add one of these imports before your first `import \u2026 from '@anthropic-ai/sdk'`:\n * - `import '@anthropic-ai/sdk/shims/node'` (if you're running on Node)\n * - `import '@anthropic-ai/sdk/shims/web'` (otherwise)\n */\n async withResponse(): Promise<{ data: T; response: Response; request_id: string | null | undefined }> {\n const [data, response] = await Promise.all([this.parse(), this.asResponse()]);\n return { data, response, request_id: response.headers.get('request-id') };\n }\n\n private parse(): Promise<WithRequestID<T>> {\n if (!this.parsedPromise) {\n this.parsedPromise = this.responsePromise.then(this.parseResponse) as any as Promise<WithRequestID<T>>;\n }\n return this.parsedPromise;\n }\n\n override then<TResult1 = WithRequestID<T>, TResult2 = never>(\n onfulfilled?: ((value: WithRequestID<T>) => TResult1 | PromiseLike<TResult1>) | undefined | null,\n onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null,\n ): Promise<TResult1 | TResult2> {\n return this.parse().then(onfulfilled, onrejected);\n }\n\n override catch<TResult = never>(\n onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null,\n ): Promise<WithRequestID<T> | TResult> {\n return this.parse().catch(onrejected);\n }\n\n override finally(onfinally?: (() => void) | undefined | null): Promise<WithRequestID<T>> {\n return this.parse().finally(onfinally);\n }\n}\n\nexport abstract class APIClient {\n baseURL: string;\n maxRetries: number;\n timeout: number;\n httpAgent: Agent | undefined;\n\n private fetch: Fetch;\n protected idempotencyHeader?: string;\n\n constructor({\n baseURL,\n maxRetries = 2,\n timeout = 600000, // 10 minutes\n httpAgent,\n fetch: overriddenFetch,\n }: {\n baseURL: string;\n maxRetries?: number | undefined;\n timeout: number | undefined;\n httpAgent: Agent | undefined;\n fetch: Fetch | undefined;\n }) {\n this.baseURL = baseURL;\n this.maxRetries = validatePositiveInteger('maxRetries', maxRetries);\n this.timeout = validatePositiveInteger('timeout', timeout);\n this.httpAgent = httpAgent;\n\n this.fetch = overriddenFetch ?? fetch;\n }\n\n protected authHeaders(opts: FinalRequestOptions): Headers {\n return {};\n }\n\n /**\n * Override this to add your own default headers, for example:\n *\n * {\n * ...super.defaultHeaders(),\n * Authorization: 'Bearer 123',\n * }\n */\n protected defaultHeaders(opts: FinalRequestOptions): Headers {\n return {\n Accept: 'application/json',\n 'Content-Type': 'application/json',\n 'User-Agent': this.getUserAgent(),\n ...getPlatformHeaders(),\n ...this.authHeaders(opts),\n };\n }\n\n protected abstract defaultQuery(): DefaultQuery | undefined;\n\n /**\n * Override this to add your own headers validation:\n */\n protected validateHeaders(headers: Headers, customHeaders: Headers) {}\n\n protected defaultIdempotencyKey(): string {\n return `stainless-node-retry-${uuid4()}`;\n }\n\n get<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {\n return this.methodRequest('get', path, opts);\n }\n\n post<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {\n return this.methodRequest('post', path, opts);\n }\n\n patch<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {\n return this.methodRequest('patch', path, opts);\n }\n\n put<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {\n return this.methodRequest('put', path, opts);\n }\n\n delete<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {\n return this.methodRequest('delete', path, opts);\n }\n\n private methodRequest<Req, Rsp>(\n method: HTTPMethod,\n path: string,\n opts?: PromiseOrValue<RequestOptions<Req>>,\n ): APIPromise<Rsp> {\n return this.request(\n Promise.resolve(opts).then(async (opts) => {\n const body =\n opts && isBlobLike(opts?.body) ? new DataView(await opts.body.arrayBuffer())\n : opts?.body instanceof DataView ? opts.body\n : opts?.body instanceof ArrayBuffer ? new DataView(opts.body)\n : opts && ArrayBuffer.isView(opts?.body) ? new DataView(opts.body.buffer)\n : opts?.body;\n return { method, path, ...opts, body };\n }),\n );\n }\n\n getAPIList<Item, PageClass extends AbstractPage<Item> = AbstractPage<Item>>(\n path: string,\n Page: new (...args: any[]) => PageClass,\n opts?: RequestOptions<any>,\n ): PagePromise<PageClass, Item> {\n return this.requestAPIList(Page, { method: 'get', path, ...opts });\n }\n\n private calculateContentLength(body: unknown): string | null {\n if (typeof body === 'string') {\n if (typeof Buffer !== 'undefined') {\n return Buffer.byteLength(body, 'utf8').toString();\n }\n\n if (typeof TextEncoder !== 'undefined') {\n const encoder = new TextEncoder();\n const encoded = encoder.encode(body);\n return encoded.length.toString();\n }\n } else if (ArrayBuffer.isView(body)) {\n return body.byteLength.toString();\n }\n\n return null;\n }\n\n buildRequest<Req>(\n options: FinalRequestOptions<Req>,\n { retryCount = 0 }: { retryCount?: number } = {},\n ): { req: RequestInit; url: string; timeout: number } {\n options = { ...options };\n const { method, path, query, headers: headers = {} } = options;\n\n const body =\n ArrayBuffer.isView(options.body) || (options.__binaryRequest && typeof options.body === 'string') ?\n options.body\n : isMultipartBody(options.body) ? options.body.body\n : options.body ? JSON.stringify(options.body, null, 2)\n : null;\n const contentLength = this.calculateContentLength(body);\n\n const url = this.buildURL(path!, query);\n if ('timeout' in options) validatePositiveInteger('timeout', options.timeout);\n options.timeout = options.timeout ?? this.timeout;\n const httpAgent = options.httpAgent ?? this.httpAgent ?? getDefaultAgent(url);\n const minAgentTimeout = options.timeout + 1000;\n if (\n typeof (httpAgent as any)?.options?.timeout === 'number' &&\n minAgentTimeout > ((httpAgent as any).options.timeout ?? 0)\n ) {\n // Allow any given request to bump our agent active socket timeout.\n // This may seem strange, but leaking active sockets should be rare and not particularly problematic,\n // and without mutating agent we would need to create more of them.\n // This tradeoff optimizes for performance.\n (httpAgent as any).options.timeout = minAgentTimeout;\n }\n\n if (this.idempotencyHeader && method !== 'get') {\n if (!options.idempotencyKey) options.idempotencyKey = this.defaultIdempotencyKey();\n headers[this.idempotencyHeader] = options.idempotencyKey;\n }\n\n const reqHeaders = this.buildHeaders({ options, headers, contentLength, retryCount });\n\n const req: RequestInit = {\n method,\n ...(body && { body: body as any }),\n headers: reqHeaders,\n ...(httpAgent && { agent: httpAgent }),\n // @ts-ignore node-fetch uses a custom AbortSignal type that is\n // not compatible with standard web types\n signal: options.signal ?? null,\n };\n\n return { req, url, timeout: options.timeout };\n }\n\n private buildHeaders({\n options,\n headers,\n contentLength,\n retryCount,\n }: {\n options: FinalRequestOptions;\n headers: Record<string, string | null | undefined>;\n contentLength: string | null | undefined;\n retryCount: number;\n }): Record<string, string> {\n const reqHeaders: Record<string, string> = {};\n if (contentLength) {\n reqHeaders['content-length'] = contentLength;\n }\n\n const defaultHeaders = this.defaultHeaders(options);\n applyHeadersMut(reqHeaders, defaultHeaders);\n applyHeadersMut(reqHeaders, headers);\n\n // let builtin fetch set the Content-Type for multipart bodies\n if (isMultipartBody(options.body) && shimsKind !== 'node') {\n delete reqHeaders['content-type'];\n }\n\n // Don't set theses headers if they were already set or removed through default headers or by the caller.\n // We check `defaultHeaders` and `headers`, which can contain nulls, instead of `reqHeaders` to account\n // for the removal case.\n if (\n getHeader(defaultHeaders, 'x-stainless-retry-count') === undefined &&\n getHeader(headers, 'x-stainless-retry-count') === undefined\n ) {\n reqHeaders['x-stainless-retry-count'] = String(retryCount);\n }\n if (\n getHeader(defaultHeaders, 'x-stainless-timeout') === undefined &&\n getHeader(headers, 'x-stainless-timeout') === undefined &&\n options.timeout\n ) {\n reqHeaders['x-stainless-timeout'] = String(options.timeout);\n }\n\n this.validateHeaders(reqHeaders, headers);\n\n return reqHeaders;\n }\n\n _calculateNonstreamingTimeout(maxTokens: number): number {\n const defaultTimeout = 10 * 60;\n const expectedTimeout = (60 * 60 * maxTokens) / 128_000;\n if (expectedTimeout > defaultTimeout) {\n throw new AnthropicError(\n 'Streaming is strongly recommended for operations that may take longer than 10 minutes. ' +\n 'See https://github.com/anthropics/anthropic-sdk-python#streaming-responses for more details',\n );\n }\n return defaultTimeout * 1000;\n }\n\n /**\n * Used as a callback for mutating the given `FinalRequestOptions` object.\n */\n protected async prepareOptions(options: FinalRequestOptions): Promise<void> {}\n\n /**\n * Used as a callback for mutating the given `RequestInit` object.\n *\n * This is useful for cases where you want to add certain headers based off of\n * the request properties, e.g. `method` or `url`.\n */\n protected async prepareRequest(\n request: RequestInit,\n { url, options }: { url: string; options: FinalRequestOptions },\n ): Promise<void> {}\n\n protected parseHeaders(headers: HeadersInit | null | undefined): Record<string, string> {\n return (\n !headers ? {}\n : Symbol.iterator in headers ?\n Object.fromEntries(Array.from(headers as Iterable<string[]>).map((header) => [...header]))\n : { ...headers }\n );\n }\n\n protected makeStatusError(\n status: number | undefined,\n error: Object | undefined,\n message: string | undefined,\n headers: Headers | undefined,\n ): APIError {\n return APIError.generate(status, error, message, headers);\n }\n\n request<Req, Rsp>(\n options: PromiseOrValue<FinalRequestOptions<Req>>,\n remainingRetries: number | null = null,\n ): APIPromise<Rsp> {\n return new APIPromise(this.makeRequest(options, remainingRetries));\n }\n\n private async makeRequest<Req>(\n optionsInput: PromiseOrValue<FinalRequestOptions<Req>>,\n retriesRemaining: number | null,\n ): Promise<APIResponseProps> {\n const options = await optionsInput;\n const maxRetries = options.maxRetries ?? this.maxRetries;\n if (retriesRemaining == null) {\n retriesRemaining = maxRetries;\n }\n\n await this.prepareOptions(options);\n\n const { req, url, timeout } = this.buildRequest(options, { retryCount: maxRetries - retriesRemaining });\n\n await this.prepareRequest(req, { url, options });\n\n debug('request', url, options, req.headers);\n\n if (options.signal?.aborted) {\n throw new APIUserAbortError();\n }\n\n const controller = new AbortController();\n const response = await this.fetchWithTimeout(url, req, timeout, controller).catch(castToError);\n\n if (response instanceof Error) {\n if (options.signal?.aborted) {\n throw new APIUserAbortError();\n }\n if (retriesRemaining) {\n return this.retryRequest(options, retriesRemaining);\n }\n if (response.name === 'AbortError') {\n throw new APIConnectionTimeoutError();\n }\n throw new APIConnectionError({ cause: response });\n }\n\n const responseHeaders = createResponseHeaders(response.headers);\n\n if (!response.ok) {\n if (retriesRemaining && this.shouldRetry(response)) {\n const retryMessage = `retrying, ${retriesRemaining} attempts remaining`;\n debug(`response (error; ${retryMessage})`, response.status, url, responseHeaders);\n return this.retryRequest(options, retriesRemaining, responseHeaders);\n }\n\n const errText = await response.text().catch((e) => castToError(e).message);\n const errJSON = safeJSON(errText);\n const errMessage = errJSON ? undefined : errText;\n const retryMessage = retriesRemaining ? `(error; no more retries left)` : `(error; not retryable)`;\n\n debug(`response (error; ${retryMessage})`, response.status, url, responseHeaders, errMessage);\n\n const err = this.makeStatusError(response.status, errJSON, errMessage, responseHeaders);\n throw err;\n }\n\n return { response, options, controller };\n }\n\n requestAPIList<Item = unknown, PageClass extends AbstractPage<Item> = AbstractPage<Item>>(\n Page: new (...args: ConstructorParameters<typeof AbstractPage>) => PageClass,\n options: FinalRequestOptions,\n ): PagePromise<PageClass, Item> {\n const request = this.makeRequest(options, null);\n return new PagePromise<PageClass, Item>(this, request, Page);\n }\n\n buildURL<Req>(path: string, query: Req | null | undefined): string {\n const url =\n isAbsoluteURL(path) ?\n new URL(path)\n : new URL(this.baseURL + (this.baseURL.endsWith('/') && path.startsWith('/') ? path.slice(1) : path));\n\n const defaultQuery = this.defaultQuery();\n if (!isEmptyObj(defaultQuery)) {\n query = { ...defaultQuery, ...query } as Req;\n }\n\n if (typeof query === 'object' && query && !Array.isArray(query)) {\n url.search = this.stringifyQuery(query as Record<string, unknown>);\n }\n\n return url.toString();\n }\n\n protected stringifyQuery(query: Record<string, unknown>): string {\n return Object.entries(query)\n .filter(([_, value]) => typeof value !== 'undefined')\n .map(([key, value]) => {\n if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {\n return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;\n }\n if (value === null) {\n return `${encodeURIComponent(key)}=`;\n }\n throw new AnthropicError(\n `Cannot stringify type ${typeof value}; Expected string, number, boolean, or null. If you need to pass nested query parameters, you can manually encode them, e.g. { query: { 'foo[key1]': value1, 'foo[key2]': value2 } }, and please open a GitHub issue requesting better support for your use case.`,\n );\n })\n .join('&');\n }\n\n async fetchWithTimeout(\n url: RequestInfo,\n init: RequestInit | undefined,\n ms: number,\n controller: AbortController,\n ): Promise<Response> {\n const { signal, ...options } = init || {};\n if (signal) signal.addEventListener('abort', () => controller.abort());\n\n const timeout = setTimeout(() => controller.abort(), ms);\n\n const fetchOptions = {\n signal: controller.signal as any,\n ...options,\n };\n if (fetchOptions.method) {\n // Custom methods like 'patch' need to be uppercased\n // See https://github.com/nodejs/undici/issues/2294\n fetchOptions.method = fetchOptions.method.toUpperCase();\n }\n\n // turn on TCP keep-alive for the sockets, if the runtime supports it\n const socketKeepAliveInterval = 60 * 1000;\n const keepAliveTimeout = setTimeout(() => {\n if (fetchOptions && (fetchOptions as any)?.agent?.sockets) {\n for (const socket of Object.values((fetchOptions as any)?.agent?.sockets).flat()) {\n if ((socket as any)?.setKeepAlive) {\n (socket as any).setKeepAlive(true, socketKeepAliveInterval);\n }\n }\n }\n }, socketKeepAliveInterval);\n\n return (\n // use undefined this binding; fetch errors if bound to something else in browser/cloudflare\n this.fetch.call(undefined, url, fetchOptions).finally(() => {\n clearTimeout(timeout);\n clearTimeout(keepAliveTimeout);\n })\n );\n }\n\n private shouldRetry(response: Response): boolean {\n // Note this is not a standard header.\n const shouldRetryHeader = response.headers.get('x-should-retry');\n\n // If the server explicitly says whether or not to retry, obey.\n if (shouldRetryHeader === 'true') return true;\n if (shouldRetryHeader === 'false') return false;\n\n // Retry on request timeouts.\n if (response.status === 408) return true;\n\n // Retry on lock timeouts.\n if (response.status === 409) return true;\n\n // Retry on rate limits.\n if (response.status === 429) return true;\n\n // Retry internal errors.\n if (response.status >= 500) return true;\n\n return false;\n }\n\n private async retryRequest(\n options: FinalRequestOptions,\n retriesRemaining: number,\n responseHeaders?: Headers | undefined,\n ): Promise<APIResponseProps> {\n let timeoutMillis: number | undefined;\n\n // Note the `retry-after-ms` header may not be standard, but is a good idea and we'd like proactive support for it.\n const retryAfterMillisHeader = responseHeaders?.['retry-after-ms'];\n if (retryAfterMillisHeader) {\n const timeoutMs = parseFloat(retryAfterMillisHeader);\n if (!Number.isNaN(timeoutMs)) {\n timeoutMillis = timeoutMs;\n }\n }\n\n // About the Retry-After header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After\n const retryAfterHeader = responseHeaders?.['retry-after'];\n if (retryAfterHeader && !timeoutMillis) {\n const timeoutSeconds = parseFloat(retryAfterHeader);\n if (!Number.isNaN(timeoutSeconds)) {\n timeoutMillis = timeoutSeconds * 1000;\n } else {\n timeoutMillis = Date.parse(retryAfterHeader) - Date.now();\n }\n }\n\n // If the API asks us to wait a certain amount of time (and it's a reasonable amount),\n // just do what it says, but otherwise calculate a default\n if (!(timeoutMillis && 0 <= timeoutMillis && timeoutMillis < 60 * 1000)) {\n const maxRetries = options.maxRetries ?? this.maxRetries;\n timeoutMillis = this.calculateDefaultRetryTimeoutMillis(retriesRemaining, maxRetries);\n }\n await sleep(timeoutMillis);\n\n return this.makeRequest(options, retriesRemaining - 1);\n }\n\n private calculateDefaultRetryTimeoutMillis(retriesRemaining: number, maxRetries: number): number {\n const initialRetryDelay = 0.5;\n const maxRetryDelay = 8.0;\n\n const numRetries = maxRetries - retriesRemaining;\n\n // Apply exponential backoff, but not more than the max.\n const sleepSeconds = Math.min(initialRetryDelay * Math.pow(2, numRetries), maxRetryDelay);\n\n // Apply some jitter, take up to at most 25 percent of the retry time.\n const jitter = 1 - Math.random() * 0.25;\n\n return sleepSeconds * jitter * 1000;\n }\n\n private getUserAgent(): string {\n return `${this.constructor.name}/JS ${VERSION}`;\n }\n}\n\nexport type PageInfo = { url: URL } | { params: Record<string, unknown> | null };\n\nexport abstract class AbstractPage<Item> implements AsyncIterable<Item> {\n #client: APIClient;\n protected options: FinalRequestOptions;\n\n protected response: Response;\n protected body: unknown;\n\n constructor(client: APIClient, response: Response, body: unknown, options: FinalRequestOptions) {\n this.#client = client;\n this.options = options;\n this.response = response;\n this.body = body;\n }\n\n /**\n * @deprecated Use nextPageInfo instead\n */\n abstract nextPageParams(): Partial<Record<string, unknown>> | null;\n abstract nextPageInfo(): PageInfo | null;\n\n abstract getPaginatedItems(): Item[];\n\n hasNextPage(): boolean {\n const items = this.getPaginatedItems();\n if (!items.length) return false;\n return this.nextPageInfo() != null;\n }\n\n async getNextPage(): Promise<this> {\n const nextInfo = this.nextPageInfo();\n if (!nextInfo) {\n throw new AnthropicError(\n 'No next page expected; please check `.hasNextPage()` before calling `.getNextPage()`.',\n );\n }\n const nextOptions = { ...this.options };\n if ('params' in nextInfo && typeof nextOptions.query === 'object') {\n nextOptions.query = { ...nextOptions.query, ...nextInfo.params };\n } else if ('url' in nextInfo) {\n const params = [...Object.entries(nextOptions.query || {}), ...nextInfo.url.searchParams.entries()];\n for (const [key, value] of params) {\n nextInfo.url.searchParams.set(key, value as any);\n }\n nextOptions.query = undefined;\n nextOptions.path = nextInfo.url.toString();\n }\n return await this.#client.requestAPIList(this.constructor as any, nextOptions);\n }\n\n async *iterPages(): AsyncGenerator<this> {\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n let page: this = this;\n yield page;\n while (page.hasNextPage()) {\n page = await page.getNextPage();\n yield page;\n }\n }\n\n async *[Symbol.asyncIterator](): AsyncGenerator<Item> {\n for await (const page of this.iterPages()) {\n for (const item of page.getPaginatedItems()) {\n yield item;\n }\n }\n }\n}\n\n/**\n * This subclass of Promise will resolve to an instantiated Page once the request completes.\n *\n * It also implements AsyncIterable to allow auto-paginating iteration on an unawaited list call, eg:\n *\n * for await (const item of client.items.list()) {\n * console.log(item)\n * }\n */\nexport class PagePromise<\n PageClass extends AbstractPage<Item>,\n Item = ReturnType<PageClass['getPaginatedItems']>[number],\n >\n extends APIPromise<PageClass>\n implements AsyncIterable<Item>\n{\n constructor(\n client: APIClient,\n request: Promise<APIResponseProps>,\n Page: new (...args: ConstructorParameters<typeof AbstractPage>) => PageClass,\n ) {\n super(\n request,\n async (props) =>\n new Page(\n client,\n props.response,\n await defaultParseResponse(props),\n props.options,\n ) as WithRequestID<PageClass>,\n );\n }\n\n /**\n * Allow auto-paginating iteration on an unawaited list call, eg:\n *\n * for await (const item of client.items.list()) {\n * console.log(item)\n * }\n */\n async *[Symbol.asyncIterator](): AsyncGenerator<Item> {\n const page = await this;\n for await (const item of page) {\n yield item;\n }\n }\n}\n\nexport const createResponseHeaders = (\n headers: Awaited<ReturnType<Fetch>>['headers'],\n): Record<string, string> => {\n return new Proxy(\n Object.fromEntries(\n // @ts-ignore\n headers.entries(),\n ),\n {\n get(target, name) {\n const key = name.toString();\n return target[key.toLowerCase()] || target[key];\n },\n },\n );\n};\n\ntype HTTPMethod = 'get' | 'post' | 'put' | 'patch' | 'delete';\n\nexport type RequestClient = { fetch: Fetch };\nexport type Headers = Record<string, string | null | undefined>;\nexport type DefaultQuery = Record<string, string | undefined>;\nexport type KeysEnum<T> = { [P in keyof Required<T>]: true };\n\nexport type RequestOptions<\n Req = unknown | Record<string, unknown> | Readable | BlobLike | ArrayBufferView | ArrayBuffer,\n> = {\n method?: HTTPMethod;\n path?: string;\n query?: Req | undefined;\n body?: Req | null | undefined;\n headers?: Headers | undefined;\n\n maxRetries?: number;\n stream?: boolean | undefined;\n timeout?: number;\n httpAgent?: Agent;\n signal?: AbortSignal | undefined | null;\n idempotencyKey?: string;\n\n __binaryRequest?: boolean | undefined;\n __binaryResponse?: boolean | undefined;\n __streamClass?: typeof Stream;\n};\n\n// This is required so that we can determine if a given object matches the RequestOptions\n// type at runtime. While this requires duplication, it is enforced by the TypeScript\n// compiler such that any missing / extraneous keys will cause an error.\nconst requestOptionsKeys: KeysEnum<RequestOptions> = {\n method: true,\n path: true,\n query: true,\n body: true,\n headers: true,\n\n maxRetries: true,\n stream: true,\n timeout: true,\n httpAgent: true,\n signal: true,\n idempotencyKey: true,\n\n __binaryRequest: true,\n __binaryResponse: true,\n __streamClass: true,\n};\n\nexport const isRequestOptions = (obj: unknown): obj is RequestOptions => {\n return (\n typeof obj === 'object' &&\n obj !== null &&\n !isEmptyObj(obj) &&\n Object.keys(obj).every((k) => hasOwn(requestOptionsKeys, k))\n );\n};\n\nexport type FinalRequestOptions<Req = unknown | Record<string, unknown> | Readable | DataView> =\n RequestOptions<Req> & {\n method: HTTPMethod;\n path: string;\n };\n\ndeclare const Deno: any;\ndeclare const EdgeRuntime: any;\ntype Arch = 'x32' | 'x64' | 'arm' | 'arm64' | `other:${string}` | 'unknown';\ntype PlatformName =\n | 'MacOS'\n | 'Linux'\n | 'Windows'\n | 'FreeBSD'\n | 'OpenBSD'\n | 'iOS'\n | 'Android'\n | `Other:${string}`\n | 'Unknown';\ntype Browser = 'ie' | 'edge' | 'chrome' | 'firefox' | 'safari';\ntype PlatformProperties = {\n 'X-Stainless-Lang': 'js';\n 'X-Stainless-Package-Version': string;\n 'X-Stainless-OS': PlatformName;\n 'X-Stainless-Arch': Arch;\n 'X-Stainless-Runtime': 'node' | 'deno' | 'edge' | `browser:${Browser}` | 'unknown';\n 'X-Stainless-Runtime-Version': string;\n};\nconst getPlatformProperties = (): PlatformProperties => {\n if (typeof Deno !== 'undefined' && Deno.build != null) {\n return {\n 'X-Stainless-Lang': 'js',\n 'X-Stainless-Package-Version': VERSION,\n 'X-Stainless-OS': normalizePlatform(Deno.build.os),\n 'X-Stainless-Arch': normalizeArch(Deno.build.arch),\n 'X-Stainless-Runtime': 'deno',\n 'X-Stainless-Runtime-Version':\n typeof Deno.version === 'string' ? Deno.version : Deno.version?.deno ?? 'unknown',\n };\n }\n if (typeof EdgeRuntime !== 'undefined') {\n return {\n 'X-Stainless-Lang': 'js',\n 'X-Stainless-Package-Version': VERSION,\n 'X-Stainless-OS': 'Unknown',\n 'X-Stainless-Arch': `other:${EdgeRuntime}`,\n 'X-Stainless-Runtime': 'edge',\n 'X-Stainless-Runtime-Version': process.version,\n };\n }\n // Check if Node.js\n if (Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]') {\n return {\n 'X-Stainless-Lang': 'js',\n 'X-Stainless-Package-Version': VERSION,\n 'X-Stainless-OS': normalizePlatform(process.platform),\n 'X-Stainless-Arch': normalizeArch(process.arch),\n 'X-Stainless-Runtime': 'node',\n 'X-Stainless-Runtime-Version': process.version,\n };\n }\n\n const browserInfo = getBrowserInfo();\n if (browserInfo) {\n return {\n 'X-Stainless-Lang': 'js',\n 'X-Stainless-Package-Version': VERSION,\n 'X-Stainless-OS': 'Unknown',\n 'X-Stainless-Arch': 'unknown',\n 'X-Stainless-Runtime': `browser:${browserInfo.browser}`,\n 'X-Stainless-Runtime-Version': browserInfo.version,\n };\n }\n\n // TODO add support for Cloudflare workers, etc.\n return {\n 'X-Stainless-Lang': 'js',\n 'X-Stainless-Package-Version': VERSION,\n 'X-Stainless-OS': 'Unknown',\n 'X-Stainless-Arch': 'unknown',\n 'X-Stainless-Runtime': 'unknown',\n 'X-Stainless-Runtime-Version': 'unknown',\n };\n};\n\ntype BrowserInfo = {\n browser: Browser;\n version: string;\n};\n\ndeclare const navigator: { userAgent: string } | undefined;\n\n// Note: modified from https://github.com/JS-DevTools/host-environment/blob/b1ab79ecde37db5d6e163c050e54fe7d287d7c92/src/isomorphic.browser.ts\nfunction getBrowserInfo(): BrowserInfo | null {\n if (typeof navigator === 'undefined' || !navigator) {\n return null;\n }\n\n // NOTE: The order matters here!\n const browserPatterns = [\n { key: 'edge' as const, pattern: /Edge(?:\\W+(\\d+)\\.(\\d+)(?:\\.(\\d+))?)?/ },\n { key: 'ie' as const, pattern: /MSIE(?:\\W+(\\d+)\\.(\\d+)(?:\\.(\\d+))?)?/ },\n { key: 'ie' as const, pattern: /Trident(?:.*rv\\:(\\d+)\\.(\\d+)(?:\\.(\\d+))?)?/ },\n { key: 'chrome' as const, pattern: /Chrome(?:\\W+(\\d+)\\.(\\d+)(?:\\.(\\d+))?)?/ },\n { key: 'firefox' as const, pattern: /Firefox(?:\\W+(\\d+)\\.(\\d+)(?:\\.(\\d+))?)?/ },\n { key: 'safari' as const, pattern: /(?:Version\\W+(\\d+)\\.(\\d+)(?:\\.(\\d+))?)?(?:\\W+Mobile\\S*)?\\W+Safari/ },\n ];\n\n // Find the FIRST matching browser\n for (const { key, pattern } of browserPatterns) {\n const match = pattern.exec(navigator.userAgent);\n if (match) {\n const major = match[1] || 0;\n const minor = match[2] || 0;\n const patch = match[3] || 0;\n\n return { browser: key, version: `${major}.${minor}.${patch}` };\n }\n }\n\n return null;\n}\n\nconst normalizeArch = (arch: string): Arch => {\n // Node docs:\n // - https://nodejs.org/api/process.html#processarch\n // Deno docs:\n // - https://doc.deno.land/deno/stable/~/Deno.build\n if (arch === 'x32') return 'x32';\n if (arch === 'x86_64' || arch === 'x64') return 'x64';\n if (arch === 'arm') return 'arm';\n if (arch === 'aarch64' || arch === 'arm64') return 'arm64';\n if (arch) return `other:${arch}`;\n return 'unknown';\n};\n\nconst normalizePlatform = (platform: string): PlatformName => {\n // Node platforms:\n // - https://nodejs.org/api/process.html#processplatform\n // Deno platforms:\n // - https://doc.deno.land/deno/stable/~/Deno.build\n // - https://github.com/denoland/deno/issues/14799\n\n platform = platform.toLowerCase();\n\n // NOTE: this iOS check is untested and may not work\n // Node does not work natively on IOS, there is a fork at\n // https://github.com/nodejs-mobile/nodejs-mobile\n // however it is unknown at the time of writing how to detect if it is running\n if (platform.includes('ios')) return 'iOS';\n if (platform === 'android') return 'Android';\n if (platform === 'darwin') return 'MacOS';\n if (platform === 'win32') return 'Windows';\n if (platform === 'freebsd') return 'FreeBSD';\n if (platform === 'openbsd') return 'OpenBSD';\n if (platform === 'linux') return 'Linux';\n if (platform) return `Other:${platform}`;\n return 'Unknown';\n};\n\nlet _platformHeaders: PlatformProperties;\nconst getPlatformHeaders = () => {\n return (_platformHeaders ??= getPlatformProperties());\n};\n\nexport const safeJSON = (text: string) => {\n try {\n return JSON.parse(text);\n } catch (err) {\n return undefined;\n }\n};\n\n// https://url.spec.whatwg.org/#url-scheme-string\nconst startsWithSchemeRegexp = /^[a-z][a-z0-9+.-]*:/i;\nconst isAbsoluteURL = (url: string): boolean => {\n return startsWithSchemeRegexp.test(url);\n};\n\nexport const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));\n\nconst validatePositiveInteger = (name: string, n: unknown): number => {\n if (typeof n !== 'number' || !Number.isInteger(n)) {\n throw new AnthropicError(`${name} must be an integer`);\n }\n if (n < 0) {\n throw new AnthropicError(`${name} must be a positive integer`);\n }\n return n;\n};\n\nexport const castToError = (err: any): Error => {\n if (err instanceof Error) return err;\n if (typeof err === 'object' && err !== null) {\n try {\n return new Error(JSON.stringify(err));\n } catch {}\n }\n return new Error(String(err));\n};\n\nexport const ensurePresent = <T>(value: T | null | undefined): T => {\n if (value == null) throw new AnthropicError(`Expected a value to be given but received ${value} instead.`);\n return value;\n};\n\n/**\n * Read an environment variable.\n *\n * Trims beginning and trailing whitespace.\n *\n * Will return undefined if the environment variable doesn't exist or cannot be accessed.\n */\nexport const readEnv = (env: string): string | undefined => {\n if (typeof process !== 'undefined') {\n return process.env?.[env]?.trim() ?? undefined;\n }\n if (typeof Deno !== 'undefined') {\n return Deno.env?.get?.(env)?.trim();\n }\n return undefined;\n};\n\nexport const coerceInteger = (value: unknown): number => {\n if (typeof value === 'number') return Math.round(value);\n if (typeof value === 'string') return parseInt(value, 10);\n\n throw new AnthropicError(`Could not coerce ${value} (type: ${typeof value}) into a number`);\n};\n\nexport const coerceFloat = (value: unknown): number => {\n if (typeof value === 'number') return value;\n if (typeof value === 'string') return parseFloat(value);\n\n throw new AnthropicError(`Could not coerce ${value} (type: ${typeof value}) into a number`);\n};\n\nexport const coerceBoolean = (value: unknown): boolean => {\n if (typeof value === 'boolean') return value;\n if (typeof value === 'string') return value === 'true';\n return Boolean(value);\n};\n\nexport const maybeCoerceInteger = (value: unknown): number | undefined => {\n if (value === undefined) {\n return undefined;\n }\n return coerceInteger(value);\n};\n\nexport const maybeCoerceFloat = (value: unknown): number | undefined => {\n if (value === undefined) {\n return undefined;\n }\n return coerceFloat(value);\n};\n\nexport const maybeCoerceBoolean = (value: unknown): boolean | undefined => {\n if (value === undefined) {\n return undefined;\n }\n return coerceBoolean(value);\n};\n\n// https://stackoverflow.com/a/34491287\nexport function isEmptyObj(obj: Object | null | undefined): boolean {\n if (!obj) return true;\n for (const _k in obj) return false;\n return true;\n}\n\n// https://eslint.org/docs/latest/rules/no-prototype-builtins\nexport function hasOwn(obj: Object, key: string): boolean {\n return Object.prototype.hasOwnProperty.call(obj, key);\n}\n\n/**\n * Copies headers from \"newHeaders\" onto \"targetHeaders\",\n * using lower-case for all properties,\n * ignoring any keys with undefined values,\n * and deleting any keys with null values.\n */\nfunction applyHeadersMut(targetHeaders: Headers, newHeaders: Headers): void {\n for (const k in newHeaders) {\n if (!hasOwn(newHeaders, k)) continue;\n const lowerKey = k.toLowerCase();\n if (!lowerKey) continue;\n\n const val = newHeaders[k];\n\n if (val === null) {\n delete targetHeaders[lowerKey];\n } else if (val !== undefined) {\n targetHeaders[lowerKey] = val;\n }\n }\n}\n\nexport function debug(action: string, ...args: any[]) {\n if (typeof process !== 'undefined' && process?.env?.['DEBUG'] === 'true') {\n console.log(`Anthropic:DEBUG:${action}`, ...args);\n }\n}\n\n/**\n * https://stackoverflow.com/a/2117523\n */\nconst uuid4 = () => {\n return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {\n const r = (Math.random() * 16) | 0;\n const v = c === 'x' ? r : (r & 0x3) | 0x8;\n return v.toString(16);\n });\n};\n\nexport const isRunningInBrowser = () => {\n return (\n // @ts-ignore\n typeof window !== 'undefined' &&\n // @ts-ignore\n typeof window.document !== 'undefined' &&\n // @ts-ignore\n typeof navigator !== 'undefined'\n );\n};\n\nexport interface HeadersProtocol {\n get: (header: string) => string | null | undefined;\n}\nexport type HeadersLike = Record<string, string | string[] | undefined> | HeadersProtocol;\n\nexport const isHeadersProtocol = (headers: any): headers is HeadersProtocol => {\n return typeof headers?.get === 'function';\n};\n\nexport const getRequiredHeader = (headers: HeadersLike | Headers, header: string): string => {\n const foundHeader = getHeader(headers, header);\n if (foundHeader === undefined) {\n throw new Error(`Could not find ${header} header`);\n }\n return foundHeader;\n};\n\nexport const getHeader = (headers: HeadersLike | Headers, header: string): string | undefined => {\n const lowerCasedHeader = header.toLowerCase();\n if (isHeadersProtocol(headers)) {\n // to deal with the case where the header looks like Stainless-Event-Id\n const intercapsHeader =\n header[0]?.toUpperCase() +\n header.substring(1).replace(/([^\\w])(\\w)/g, (_m, g1, g2) => g1 + g2.toUpperCase());\n for (const key of [header, lowerCasedHeader, header.toUpperCase(), intercapsHeader]) {\n const value = headers.get(key);\n if (value) {\n return value;\n }\n }\n }\n\n for (const [key, value] of Object.entries(headers)) {\n if (key.toLowerCase() === lowerCasedHeader) {\n if (Array.isArray(value)) {\n if (value.length <= 1) return value[0];\n console.warn(`Received ${value.length} entries for the ${header} header, using the first entry.`);\n return value[0];\n }\n return value;\n }\n }\n\n return undefined;\n};\n\n/**\n * Encodes a string to Base64 format.\n */\nexport const toBase64 = (str: string | null | undefined): string => {\n if (!str) return '';\n if (typeof Buffer !== 'undefined') {\n return Buffer.from(str).toString('base64');\n }\n\n if (typeof btoa !== 'undefined') {\n return btoa(str);\n }\n\n throw new AnthropicError('Cannot generate b64 string; Expected `Buffer` or `btoa` to be defined');\n};\n\nexport function isObj(obj: unknown): obj is Record<string, unknown> {\n return obj != null && typeof obj === 'object' && !Array.isArray(obj);\n}\n", "// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\n\nimport { AbstractPage, Response, APIClient, FinalRequestOptions, PageInfo } from \"./core.js\";\n\nexport interface PageResponse<Item> {\n data: Array<Item>;\n\n has_more: boolean;\n\n first_id: string | null;\n\n last_id: string | null;\n}\n\nexport interface PageParams {\n /**\n * Number of items per page.\n */\n limit?: number;\n\n before_id?: string;\n\n after_id?: string;\n}\n\nexport class Page<Item> extends AbstractPage<Item> implements PageResponse<Item> {\n data: Array<Item>;\n\n has_more: boolean;\n\n first_id: string | null;\n\n last_id: string | null;\n\n constructor(client: APIClient, response: Response, body: PageResponse<Item>, options: FinalRequestOptions) {\n super(client, response, body, options);\n\n this.data = body.data || [];\n this.has_more = body.has_more || false;\n this.first_id = body.first_id || null;\n this.last_id = body.last_id || null;\n }\n\n getPaginatedItems(): Item[] {\n return this.data ?? [];\n }\n\n override hasNextPage(): boolean {\n if (this.has_more === false) {\n return false;\n }\n\n return super.hasNextPage();\n }\n\n // @deprecated Please use `nextPageInfo()` instead\n nextPageParams(): Partial<PageParams> | null {\n const info = this.nextPageInfo();\n if (!info) return null;\n if ('params' in info) return info.params;\n const params = Object.fromEntries(info.url.searchParams);\n if (!Object.keys(params).length) return null;\n return params;\n }\n\n nextPageInfo(): PageInfo | null {\n if ((this.options.query as Record<string, unknown>)?.['before_id']) {\n // in reverse\n const firstId = this.first_id;\n if (!firstId) {\n return null;\n }\n\n return {\n params: {\n before_id: firstId,\n },\n };\n }\n\n const cursor = this.last_id;\n if (!cursor) {\n return null;\n }\n\n return {\n params: {\n after_id: cursor,\n },\n };\n }\n}\n", "// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\n\nimport * as Core from \"./core.js\";\n\nexport class APIResource {\n protected _client: Core.APIClient;\n\n constructor(client: Core.APIClient) {\n this._client = client;\n }\n}\n", "// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\n\nimport { APIResource } from \"../../resource.js\";\nimport { isRequestOptions } from \"../../core.js\";\nimport * as Core from \"../../core.js\";\nimport { Page, type PageParams } from \"../../pagination.js\";\n\nexport class Models extends APIResource {\n /**\n * Get a specific model.\n *\n * The Models API response can be used to determine information about a specific\n * model or resolve a model alias to a model ID.\n */\n retrieve(modelId: string, options?: Core.RequestOptions): Core.APIPromise<BetaModelInfo> {\n return this._client.get(`/v1/models/${modelId}?beta=true`, options);\n }\n\n /**\n * List available models.\n *\n * The Models API response can be used to determine which models are available for\n * use in the API. More recently released models are listed first.\n */\n list(\n query?: ModelListParams,\n options?: Core.RequestOptions,\n ): Core.PagePromise<BetaModelInfosPage, BetaModelInfo>;\n list(options?: Core.RequestOptions): Core.PagePromise<BetaModelInfosPage, BetaModelInfo>;\n list(\n query: ModelListParams | Core.RequestOptions = {},\n options?: Core.RequestOptions,\n ): Core.PagePromise<BetaModelInfosPage, BetaModelInfo> {\n if (isRequestOptions(query)) {\n return this.list({}, query);\n }\n return this._client.getAPIList('/v1/models?beta=true', BetaModelInfosPage, { query, ...options });\n }\n}\n\nexport class BetaModelInfosPage extends Page<BetaModelInfo> {}\n\nexport interface BetaModelInfo {\n /**\n * Unique model identifier.\n */\n id: string;\n\n /**\n * RFC 3339 datetime string representing the time at which the model was released.\n * May be set to an epoch value if the release date is unknown.\n */\n created_at: string;\n\n /**\n * A human-readable name for the model.\n */\n display_name: string;\n\n /**\n * Object type.\n *\n * For Models, this is always `\"model\"`.\n */\n type: 'model';\n}\n\nexport interface ModelListParams extends PageParams {}\n\nModels.BetaModelInfosPage = BetaModelInfosPage;\n\nexport declare namespace Models {\n export {\n type BetaModelInfo as BetaModelInfo,\n BetaModelInfosPage as BetaModelInfosPage,\n type ModelListParams as ModelListParams,\n };\n}\n", "import { AnthropicError } from \"../../error.js\";\nimport { ReadableStreamToAsyncIterable } from \"../stream-utils.js\";\nimport { type Response } from \"../../_shims/index.js\";\nimport { LineDecoder, type Bytes } from \"./line.js\";\n\nexport class JSONLDecoder<T> {\n controller: AbortController;\n\n constructor(\n private iterator: AsyncIterableIterator<Bytes>,\n controller: AbortController,\n ) {\n this.controller = controller;\n }\n\n private async *decoder(): AsyncIterator<T, any, undefined> {\n const lineDecoder = new LineDecoder();\n for await (const chunk of this.iterator) {\n for (const line of lineDecoder.decode(chunk)) {\n yield JSON.parse(line);\n }\n }\n\n for (const line of lineDecoder.flush()) {\n yield JSON.parse(line);\n }\n }\n\n [Symbol.asyncIterator](): AsyncIterator<T> {\n return this.decoder();\n }\n\n static fromResponse<T>(response: Response, controller: AbortController): JSONLDecoder<T> {\n if (!response.body) {\n controller.abort();\n throw new AnthropicError(`Attempted to iterate over a response with no body`);\n }\n\n return new JSONLDecoder(ReadableStreamToAsyncIterable<Bytes>(response.body), controller);\n }\n}\n", "// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\n\nimport { APIResource } from \"../../../resource.js\";\nimport { isRequestOptions } from \"../../../core.js\";\nimport * as Core from \"../../../core.js\";\nimport * as BetaAPI from \"../beta.js\";\nimport * as BetaMessagesAPI from \"./messages.js\";\nimport { Page, type PageParams } from \"../../../pagination.js\";\nimport { JSONLDecoder } from \"../../../internal/decoders/jsonl.js\";\nimport { AnthropicError } from \"../../../error.js\";\n\nexport class Batches extends APIResource {\n /**\n * Send a batch of Message creation requests.\n *\n * The Message Batches API can be used to process multiple Messages API requests at\n * once. Once a Message Batch is created, it begins processing immediately. Batches\n * can take up to 24 hours to complete.\n *\n * Learn more about the Message Batches API in our\n * [user guide](/en/docs/build-with-claude/batch-processing)\n */\n create(params: BatchCreateParams, options?: Core.RequestOptions): Core.APIPromise<BetaMessageBatch> {\n const { betas, ...body } = params;\n return this._client.post('/v1/messages/batches?beta=true', {\n body,\n ...options,\n headers: {\n 'anthropic-beta': [...(betas ?? []), 'message-batches-2024-09-24'].toString(),\n ...options?.headers,\n },\n });\n }\n\n /**\n * This endpoint is idempotent and can be used to poll for Message Batch\n * completion. To access the results of a Message Batch, make a request to the\n * `results_url` field in the response.\n *\n * Learn more about the Message Batches API in our\n * [user guide](/en/docs/build-with-claude/batch-processing)\n */\n retrieve(\n messageBatchId: string,\n params?: BatchRetrieveParams,\n options?: Core.RequestOptions,\n ): Core.APIPromise<BetaMessageBatch>;\n retrieve(messageBatchId: string, options?: Core.RequestOptions): Core.APIPromise<BetaMessageBatch>;\n retrieve(\n messageBatchId: string,\n params: BatchRetrieveParams | Core.RequestOptions = {},\n options?: Core.RequestOptions,\n ): Core.APIPromise<BetaMessageBatch> {\n if (isRequestOptions(params)) {\n return this.retrieve(messageBatchId, {}, params);\n }\n const { betas } = params;\n return this._client.get(`/v1/messages/batches/${messageBatchId}?beta=true`, {\n ...options,\n headers: {\n 'anthropic-beta': [...(betas ?? []), 'message-batches-2024-09-24'].toString(),\n ...options?.headers,\n },\n });\n }\n\n /**\n * List all Message Batches within a Workspace. Most recently created batches are\n * returned first.\n *\n * Learn more about the Message Batches API in our\n * [user guide](/en/docs/build-with-claude/batch-processing)\n */\n list(\n params?: BatchListParams,\n options?: Core.RequestOptions,\n ): Core.PagePromise<BetaMessageBatchesPage, BetaMessageBatch>;\n list(options?: Core.RequestOptions): Core.PagePromise<BetaMessageBatchesPage, BetaMessageBatch>;\n list(\n params: BatchListParams | Core.RequestOptions = {},\n options?: Core.RequestOptions,\n ): Core.PagePromise<BetaMessageBatchesPage, BetaMessageBatch> {\n if (isRequestOptions(params)) {\n return this.list({}, params);\n }\n const { betas, ...query } = params;\n return this._client.getAPIList('/v1/messages/batches?beta=true', BetaMessageBatchesPage, {\n query,\n ...options,\n headers: {\n 'anthropic-beta': [...(betas ?? []), 'message-batches-2024-09-24'].toString(),\n ...options?.headers,\n },\n });\n }\n\n /**\n * Delete a Message Batch.\n *\n * Message Batches can only be deleted once they've finished processing. If you'd\n * like to delete an in-progress batch, you must first cancel it.\n *\n * Learn more about the Message Batches API in our\n * [user guide](/en/docs/build-with-claude/batch-processing)\n */\n delete(\n messageBatchId: string,\n params?: BatchDeleteParams,\n options?: Core.RequestOptions,\n ): Core.APIPromise<BetaDeletedMessageBatch>;\n delete(messageBatchId: string, options?: Core.RequestOptions): Core.APIPromise<BetaDeletedMessageBatch>;\n delete(\n messageBatchId: string,\n params: BatchDeleteParams | Core.RequestOptions = {},\n options?: Core.RequestOptions,\n ): Core.APIPromise<BetaDeletedMessageBatch> {\n if (isRequestOptions(params)) {\n return this.delete(messageBatchId, {}, params);\n }\n const { betas } = params;\n return this._client.delete(`/v1/messages/batches/${messageBatchId}?beta=true`, {\n ...options,\n headers: {\n 'anthropic-beta': [...(betas ?? []), 'message-batches-2024-09-24'].toString(),\n ...options?.headers,\n },\n });\n }\n\n /**\n * Batches may be canceled any time before processing ends. Once cancellation is\n * initiated, the batch enters a `canceling` state, at which time the system may\n * complete any in-progress, non-interruptible requests before finalizing\n * cancellation.\n *\n * The number of canceled requests is specified in `request_counts`. To determine\n * which requests were canceled, check the individual results within the batch.\n * Note that cancellation may not result in any canceled requests if they were\n * non-interruptible.\n *\n * Learn more about the Message Batches API in our\n * [user guide](/en/docs/build-with-claude/batch-processing)\n */\n cancel(\n messageBatchId: string,\n params?: BatchCancelParams,\n options?: Core.RequestOptions,\n ): Core.APIPromise<BetaMessageBatch>;\n cancel(messageBatchId: string, options?: Core.RequestOptions): Core.APIPromise<BetaMessageBatch>;\n cancel(\n messageBatchId: string,\n params: BatchCancelParams | Core.RequestOptions = {},\n options?: Core.RequestOptions,\n ): Core.APIPromise<BetaMessageBatch> {\n if (isRequestOptions(params)) {\n return this.cancel(messageBatchId, {}, params);\n }\n const { betas } = params;\n return this._client.post(`/v1/messages/batches/${messageBatchId}/cancel?beta=true`, {\n ...options,\n headers: {\n 'anthropic-beta': [...(betas ?? []), 'message-batches-2024-09-24'].toString(),\n ...options?.headers,\n },\n });\n }\n\n /**\n * Streams the results of a Message Batch as a `.jsonl` file.\n *\n * Each line in the file is a JSON object containing the result of a single request\n * in the Message Batch. Results are not guaranteed to be in the same order as\n * requests. Use the `custom_id` field to match results to requests.\n *\n * Learn more about the Message Batches API in our\n * [user guide](/en/docs/build-with-claude/batch-processing)\n */\n async results(\n messageBatchId: string,\n params?: BatchResultsParams,\n options?: Core.RequestOptions,\n ): Promise<JSONLDecoder<BetaMessageBatchIndividualResponse>>;\n async results(\n messageBatchId: string,\n options?: Core.RequestOptions,\n ): Promise<JSONLDecoder<BetaMessageBatchIndividualResponse>>;\n async results(\n messageBatchId: string,\n params: BatchResultsParams | Core.RequestOptions = {},\n options?: Core.RequestOptions,\n ): Promise<JSONLDecoder<BetaMessageBatchIndividualResponse>> {\n if (isRequestOptions(params)) {\n return this.results(messageBatchId, {}, params);\n }\n\n const batch = await this.retrieve(messageBatchId);\n if (!batch.results_url) {\n throw new AnthropicError(\n `No batch \\`results_url\\`; Has it finished processing? ${batch.processing_status} - ${batch.id}`,\n );\n }\n\n const { betas } = params;\n return this._client\n .get(batch.results_url, {\n ...options,\n headers: {\n 'anthropic-beta': [...(betas ?? []), 'message-batches-2024-09-24'].toString(),\n Accept: 'application/binary',\n ...options?.headers,\n },\n __binaryResponse: true,\n })\n ._thenUnwrap((_, props) => JSONLDecoder.fromResponse(props.response, props.controller));\n }\n}\n\nexport class BetaMessageBatchesPage extends Page<BetaMessageBatch> {}\n\nexport interface BetaDeletedMessageBatch {\n /**\n * ID of the Message Batch.\n */\n id: string;\n\n /**\n * Deleted object type.\n *\n * For Message Batches, this is always `\"message_batch_deleted\"`.\n */\n type: 'message_batch_deleted';\n}\n\nexport interface BetaMessageBatch {\n /**\n * Unique object identifier.\n *\n * The format and length of IDs may change over time.\n */\n id: string;\n\n /**\n * RFC 3339 datetime string representing the time at which the Message Batch was\n * archived and its results became unavailable.\n */\n archived_at: string | null;\n\n /**\n * RFC 3339 datetime string representing the time at which cancellation was\n * initiated for the Message Batch. Specified only if cancellation was initiated.\n */\n cancel_initiated_at: string | null;\n\n /**\n * RFC 3339 datetime string representing the time at which the Message Batch was\n * created.\n */\n created_at: string;\n\n /**\n * RFC 3339 datetime string representing the time at which processing for the\n * Message Batch ended. Specified only once processing ends.\n *\n * Processing ends when every request in a Message Batch has either succeeded,\n * errored, canceled, or expired.\n */\n ended_at: string | null;\n\n /**\n * RFC 3339 datetime string representing the time at which the Message Batch will\n * expire and end processing, which is 24 hours after creation.\n */\n expires_at: string;\n\n /**\n * Processing status of the Message Batch.\n */\n processing_status: 'in_progress' | 'canceling' | 'ended';\n\n /**\n * Tallies requests within the Message Batch, categorized by their status.\n *\n * Requests start as `processing` and move to one of the other statuses only once\n * processing of the entire batch ends. The sum of all values always matches the\n * total number of requests in the batch.\n */\n request_counts: BetaMessageBatchRequestCounts;\n\n /**\n * URL to a `.jsonl` file containing the results of the Message Batch requests.\n * Specified only once processing ends.\n *\n * Results in the file are not guaranteed to be in the same order as requests. Use\n * the `custom_id` field to match results to requests.\n */\n results_url: string | null;\n\n /**\n * Object type.\n *\n * For Message Batches, this is always `\"message_batch\"`.\n */\n type: 'message_batch';\n}\n\nexport interface BetaMessageBatchCanceledResult {\n type: 'canceled';\n}\n\nexport interface BetaMessageBatchErroredResult {\n error: BetaAPI.BetaErrorResponse;\n\n type: 'errored';\n}\n\nexport interface BetaMessageBatchExpiredResult {\n type: 'expired';\n}\n\n/**\n * This is a single line in the response `.jsonl` file and does not represent the\n * response as a whole.\n */\nexport interface BetaMessageBatchIndividualResponse {\n /**\n * Developer-provided ID created for each request in a Message Batch. Useful for\n * matching results to requests, as results may be given out of request order.\n *\n * Must be unique for each request within the Message Batch.\n */\n custom_id: string;\n\n /**\n * Processing result for this request.\n *\n * Contains a Message output if processing was successful, an error response if\n * processing failed, or the reason why processing was not attempted, such as\n * cancellation or expiration.\n */\n result: BetaMessageBatchResult;\n}\n\nexport interface BetaMessageBatchRequestCounts {\n /**\n * Number of requests in the Message Batch that have been canceled.\n *\n * This is zero until processing of the entire Message Batch has ended.\n */\n canceled: number;\n\n /**\n * Number of requests in the Message Batch that encountered an error.\n *\n * This is zero until processing of the entire Message Batch has ended.\n */\n errored: number;\n\n /**\n * Number of requests in the Message Batch that have expired.\n *\n * This is zero until processing of the entire Message Batch has ended.\n */\n expired: number;\n\n /**\n * Number of requests in the Message Batch that are processing.\n */\n processing: number;\n\n /**\n * Number of requests in the Message Batch that have completed successfully.\n *\n * This is zero until processing of the entire Message Batch has ended.\n */\n succeeded: number;\n}\n\n/**\n * Processing result for this request.\n *\n * Contains a Message output if processing was successful, an error response if\n * processing failed, or the reason why processing was not attempted, such as\n * cancellation or expiration.\n */\nexport type BetaMessageBatchResult =\n | BetaMessageBatchSucceededResult\n | BetaMessageBatchErroredResult\n | BetaMessageBatchCanceledResult\n | BetaMessageBatchExpiredResult;\n\nexport interface BetaMessageBatchSucceededResult {\n message: BetaMessagesAPI.BetaMessage;\n\n type: 'succeeded';\n}\n\nexport interface BatchCreateParams {\n /**\n * Body param: List of requests for prompt completion. Each is an individual\n * request to create a Message.\n */\n requests: Array<BatchCreateParams.Request>;\n\n /**\n * Header param: Optional header to specify the beta version(s) you want to use.\n */\n betas?: Array<BetaAPI.AnthropicBeta>;\n}\n\nexport namespace BatchCreateParams {\n export interface Request {\n /**\n * Developer-provided ID created for each request in a Message Batch. Useful for\n * matching results to requests, as results may be given out of request order.\n *\n * Must be unique for each request within the Message Batch.\n */\n custom_id: string;\n\n /**\n * Messages API creation parameters for the individual request.\n *\n * See the [Messages API reference](/en/api/messages) for full documentation on\n * available parameters.\n */\n params: Omit<BetaMessagesAPI.MessageCreateParamsNonStreaming, 'betas'>;\n }\n}\n\nexport interface BatchRetrieveParams {\n /**\n * Optional header to specify the beta version(s) you want to use.\n */\n betas?: Array<BetaAPI.AnthropicBeta>;\n}\n\nexport interface BatchListParams extends PageParams {\n /**\n * Header param: Optional header to specify the beta version(s) you want to use.\n */\n betas?: Array<BetaAPI.AnthropicBeta>;\n}\n\nexport interface BatchDeleteParams {\n /**\n * Optional header to specify the beta version(s) you want to use.\n */\n betas?: Array<BetaAPI.AnthropicBeta>;\n}\n\nexport interface BatchCancelParams {\n /**\n * Optional header to specify the beta version(s) you want to use.\n */\n betas?: Array<BetaAPI.AnthropicBeta>;\n}\n\nexport interface BatchResultsParams {\n /**\n * Optional header to specify the beta version(s) you want to use.\n */\n betas?: Array<BetaAPI.AnthropicBeta>;\n}\n\nBatches.BetaMessageBatchesPage = BetaMessageBatchesPage;\n\nexport declare namespace Batches {\n export {\n type BetaDeletedMessageBatch as BetaDeletedMessageBatch,\n type BetaMessageBatch as BetaMessageBatch,\n type BetaMessageBatchCanceledResult as BetaMessageBatchCanceledResult,\n type BetaMessageBatchErroredResult as BetaMessageBatchErroredResult,\n type BetaMessageBatchExpiredResult as BetaMessageBatchExpiredResult,\n type BetaMessageBatchIndividualResponse as BetaMessageBatchIndividualResponse,\n type BetaMessageBatchRequestCounts as BetaMessageBatchRequestCounts,\n type BetaMessageBatchResult as BetaMessageBatchResult,\n type BetaMessageBatchSucceededResult as BetaMessageBatchSucceededResult,\n BetaMessageBatchesPage as BetaMessageBatchesPage,\n type BatchCreateParams as BatchCreateParams,\n type BatchRetrieveParams as BatchRetrieveParams,\n type BatchListParams as BatchListParams,\n type BatchDeleteParams as BatchDeleteParams,\n type BatchCancelParams as BatchCancelParams,\n type BatchResultsParams as BatchResultsParams,\n };\n}\n", "type Token = {\n type: string;\n value: string;\n};\n\nconst tokenize = (input: string): Token[] => {\n let current = 0;\n let tokens: Token[] = [];\n\n while (current < input.length) {\n let char = input[current];\n\n if (char === '\\\\') {\n current++;\n continue;\n }\n\n if (char === '{') {\n tokens.push({\n type: 'brace',\n value: '{',\n });\n\n current++;\n continue;\n }\n\n if (char === '}') {\n tokens.push({\n type: 'brace',\n value: '}',\n });\n\n current++;\n continue;\n }\n\n if (char === '[') {\n tokens.push({\n type: 'paren',\n value: '[',\n });\n\n current++;\n continue;\n }\n\n if (char === ']') {\n tokens.push({\n type: 'paren',\n value: ']',\n });\n\n current++;\n continue;\n }\n\n if (char === ':') {\n tokens.push({\n type: 'separator',\n value: ':',\n });\n\n current++;\n continue;\n }\n\n if (char === ',') {\n tokens.push({\n type: 'delimiter',\n value: ',',\n });\n\n current++;\n continue;\n }\n\n if (char === '\"') {\n let value = '';\n let danglingQuote = false;\n\n char = input[++current];\n\n while (char !== '\"') {\n if (current === input.length) {\n danglingQuote = true;\n break;\n }\n\n if (char === '\\\\') {\n current++;\n if (current === input.length) {\n danglingQuote = true;\n break;\n }\n value += char + input[current];\n char = input[++current];\n } else {\n value += char;\n char = input[++current];\n }\n }\n\n char = input[++current];\n\n if (!danglingQuote) {\n tokens.push({\n type: 'string',\n value,\n });\n }\n continue;\n }\n\n let WHITESPACE = /\\s/;\n if (char && WHITESPACE.test(char)) {\n current++;\n continue;\n }\n\n let NUMBERS = /[0-9]/;\n if ((char && NUMBERS.test(char)) || char === '-' || char === '.') {\n let value = '';\n\n if (char === '-') {\n value += char;\n char = input[++current];\n }\n\n while ((char && NUMBERS.test(char)) || char === '.') {\n value += char;\n char = input[++current];\n }\n\n tokens.push({\n type: 'number',\n value,\n });\n continue;\n }\n\n let LETTERS = /[a-z]/i;\n if (char && LETTERS.test(char)) {\n let value = '';\n\n while (char && LETTERS.test(char)) {\n if (current === input.length) {\n break;\n }\n value += char;\n char = input[++current];\n }\n\n if (value == 'true' || value == 'false' || value === 'null') {\n tokens.push({\n type: 'name',\n value,\n });\n } else {\n // unknown token, e.g. `nul` which isn't quite `null`\n current++;\n continue;\n }\n continue;\n }\n\n current++;\n }\n\n return tokens;\n },\n strip = (tokens: Token[]): Token[] => {\n if (tokens.length === 0) {\n return tokens;\n }\n\n let lastToken = tokens[tokens.length - 1]!;\n\n switch (lastToken.type) {\n case 'separator':\n tokens = tokens.slice(0, tokens.length - 1);\n return strip(tokens);\n break;\n case 'number':\n let lastCharacterOfLastToken = lastToken.value[lastToken.value.length - 1];\n if (lastCharacterOfLastToken === '.' || lastCharacterOfLastToken === '-') {\n tokens = tokens.slice(0, tokens.length - 1);\n return strip(tokens);\n }\n case 'string':\n let tokenBeforeTheLastToken = tokens[tokens.length - 2];\n if (tokenBeforeTheLastToken?.type === 'delimiter') {\n tokens = tokens.slice(0, tokens.length - 1);\n return strip(tokens);\n } else if (tokenBeforeTheLastToken?.type === 'brace' && tokenBeforeTheLastToken.value === '{') {\n tokens = tokens.slice(0, tokens.length - 1);\n return strip(tokens);\n }\n break;\n case 'delimiter':\n tokens = tokens.slice(0, tokens.length - 1);\n return strip(tokens);\n break;\n }\n\n return tokens;\n },\n unstrip = (tokens: Token[]): Token[] => {\n let tail: string[] = [];\n\n tokens.map((token) => {\n if (token.type === 'brace') {\n if (token.value === '{') {\n tail.push('}');\n } else {\n tail.splice(tail.lastIndexOf('}'), 1);\n }\n }\n if (token.type === 'paren') {\n if (token.value === '[') {\n tail.push(']');\n } else {\n tail.splice(tail.lastIndexOf(']'), 1);\n }\n }\n });\n\n if (tail.length > 0) {\n tail.reverse().map((item) => {\n if (item === '}') {\n tokens.push({\n type: 'brace',\n value: '}',\n });\n } else if (item === ']') {\n tokens.push({\n type: 'paren',\n value: ']',\n });\n }\n });\n }\n\n return tokens;\n },\n generate = (tokens: Token[]): string => {\n let output = '';\n\n tokens.map((token) => {\n switch (token.type) {\n case 'string':\n output += '\"' + token.value + '\"';\n break;\n default:\n output += token.value;\n break;\n }\n });\n\n return output;\n },\n partialParse = (input: string): unknown => JSON.parse(generate(unstrip(strip(tokenize(input)))));\n\nexport { partialParse };\n", "import * as Core from \"../core.js\";\nimport { AnthropicError, APIUserAbortError } from \"../error.js\";\nimport {\n type BetaContentBlock,\n Messages as BetaMessages,\n type BetaMessage,\n type BetaRawMessageStreamEvent as BetaMessageStreamEvent,\n type BetaMessageParam,\n type MessageCreateParams as BetaMessageCreateParams,\n type MessageCreateParamsBase as BetaMessageCreateParamsBase,\n type BetaTextBlock,\n type BetaTextCitation,\n} from \"../resources/beta/messages/messages.js\";\nimport { type ReadableStream, type Response } from \"../_shims/index.js\";\nimport { Stream } from \"../streaming.js\";\nimport { partialParse } from \"../_vendor/partial-json-parser/parser.js\";\n\nexport interface MessageStreamEvents {\n connect: () => void;\n streamEvent: (event: BetaMessageStreamEvent, snapshot: BetaMessage) => void;\n text: (textDelta: string, textSnapshot: string) => void;\n citation: (citation: BetaTextCitation, citationsSnapshot: BetaTextCitation[]) => void;\n inputJson: (partialJson: string, jsonSnapshot: unknown) => void;\n thinking: (thinkingDelta: string, thinkingSnapshot: string) => void;\n signature: (signature: string) => void;\n message: (message: BetaMessage) => void;\n contentBlock: (content: BetaContentBlock) => void;\n finalMessage: (message: BetaMessage) => void;\n error: (error: AnthropicError) => void;\n abort: (error: APIUserAbortError) => void;\n end: () => void;\n}\n\ntype MessageStreamEventListeners<Event extends keyof MessageStreamEvents> = {\n listener: MessageStreamEvents[Event];\n once?: boolean;\n}[];\n\nconst JSON_BUF_PROPERTY = '__json_buf';\n\nexport class BetaMessageStream implements AsyncIterable<BetaMessageStreamEvent> {\n messages: BetaMessageParam[] = [];\n receivedMessages: BetaMessage[] = [];\n #currentMessageSnapshot: BetaMessage | undefined;\n\n controller: AbortController = new AbortController();\n\n #connectedPromise: Promise<Response | null>;\n #resolveConnectedPromise: (response: Response | null) => void = () => {};\n #rejectConnectedPromise: (error: AnthropicError) => void = () => {};\n\n #endPromise: Promise<void>;\n #resolveEndPromise: () => void = () => {};\n #rejectEndPromise: (error: AnthropicError) => void = () => {};\n\n #listeners: { [Event in keyof MessageStreamEvents]?: MessageStreamEventListeners<Event> } = {};\n\n #ended = false;\n #errored = false;\n #aborted = false;\n #catchingPromiseCreated = false;\n #response: Response | null | undefined;\n #request_id: string | null | undefined;\n\n constructor() {\n this.#connectedPromise = new Promise<Response | null>((resolve, reject) => {\n this.#resolveConnectedPromise = resolve;\n this.#rejectConnectedPromise = reject;\n });\n\n this.#endPromise = new Promise<void>((resolve, reject) => {\n this.#resolveEndPromise = resolve;\n this.#rejectEndPromise = reject;\n });\n\n // Don't let these promises cause unhandled rejection errors.\n // we will manually cause an unhandled rejection error later\n // if the user hasn't registered any error listener or called\n // any promise-returning method.\n this.#connectedPromise.catch(() => {});\n this.#endPromise.catch(() => {});\n }\n\n get response(): Response | null | undefined {\n return this.#response;\n }\n\n get request_id(): string | null | undefined {\n return this.#request_id;\n }\n\n /**\n * Returns the `MessageStream` data, the raw `Response` instance and the ID of the request,\n * returned vie the `request-id` header which is useful for debugging requests and resporting\n * issues to Anthropic.\n *\n * This is the same as the `APIPromise.withResponse()` method.\n *\n * This method will raise an error if you created the stream using `MessageStream.fromReadableStream`\n * as no `Response` is available.\n */\n async withResponse(): Promise<{\n data: BetaMessageStream;\n response: Response;\n request_id: string | null | undefined;\n }> {\n const response = await this.#connectedPromise;\n if (!response) {\n throw new Error('Could not resolve a `Response` object');\n }\n\n return {\n data: this,\n response,\n request_id: response.headers.get('request-id'),\n };\n }\n\n /**\n * Intended for use on the frontend, consuming a stream produced with\n * `.toReadableStream()` on the backend.\n *\n * Note that messages sent to the model do not appear in `.on('message')`\n * in this context.\n */\n static fromReadableStream(stream: ReadableStream): BetaMessageStream {\n const runner = new BetaMessageStream();\n runner._run(() => runner._fromReadableStream(stream));\n return runner;\n }\n\n static createMessage(\n messages: BetaMessages,\n params: BetaMessageCreateParamsBase,\n options?: Core.RequestOptions,\n ): BetaMessageStream {\n const runner = new BetaMessageStream();\n for (const message of params.messages) {\n runner._addMessageParam(message);\n }\n runner._run(() =>\n runner._createMessage(\n messages,\n { ...params, stream: true },\n { ...options, headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'stream' } },\n ),\n );\n return runner;\n }\n\n protected _run(executor: () => Promise<any>) {\n executor().then(() => {\n this._emitFinal();\n this._emit('end');\n }, this.#handleError);\n }\n\n protected _addMessageParam(message: BetaMessageParam) {\n this.messages.push(message);\n }\n\n protected _addMessage(message: BetaMessage, emit = true) {\n this.receivedMessages.push(message);\n if (emit) {\n this._emit('message', message);\n }\n }\n\n protected async _createMessage(\n messages: BetaMessages,\n params: BetaMessageCreateParams,\n options?: Core.RequestOptions,\n ): Promise<void> {\n const signal = options?.signal;\n if (signal) {\n if (signal.aborted) this.controller.abort();\n signal.addEventListener('abort', () => this.controller.abort());\n }\n this.#beginRequest();\n const { response, data: stream } = await messages\n .create({ ...params, stream: true }, { ...options, signal: this.controller.signal })\n .withResponse();\n this._connected(response);\n for await (const event of stream) {\n this.#addStreamEvent(event);\n }\n if (stream.controller.signal?.aborted) {\n throw new APIUserAbortError();\n }\n this.#endRequest();\n }\n\n protected _connected(response: Response | null) {\n if (this.ended) return;\n this.#response = response;\n this.#request_id = response?.headers.get('request-id');\n this.#resolveConnectedPromise(response);\n this._emit('connect');\n }\n\n get ended(): boolean {\n return this.#ended;\n }\n\n get errored(): boolean {\n return this.#errored;\n }\n\n get aborted(): boolean {\n return this.#aborted;\n }\n\n abort() {\n this.controller.abort();\n }\n\n /**\n * Adds the listener function to the end of the listeners array for the event.\n * No checks are made to see if the listener has already been added. Multiple calls passing\n * the same combination of event and listener will result in the listener being added, and\n * called, multiple times.\n * @returns this MessageStream, so that calls can be chained\n */\n on<Event extends keyof MessageStreamEvents>(event: Event, listener: MessageStreamEvents[Event]): this {\n const listeners: MessageStreamEventListeners<Event> =\n this.#listeners[event] || (this.#listeners[event] = []);\n listeners.push({ listener });\n return this;\n }\n\n /**\n * Removes the specified listener from the listener array for the event.\n * off() will remove, at most, one instance of a listener from the listener array. If any single\n * listener has been added multiple times to the listener array for the specified event, then\n * off() must be called multiple times to remove each instance.\n * @returns this MessageStream, so that calls can be chained\n */\n off<Event extends keyof MessageStreamEvents>(event: Event, listener: MessageStreamEvents[Event]): this {\n const listeners = this.#listeners[event];\n if (!listeners) return this;\n const index = listeners.findIndex((l) => l.listener === listener);\n if (index >= 0) listeners.splice(index, 1);\n return this;\n }\n\n /**\n * Adds a one-time listener function for the event. The next time the event is triggered,\n * this listener is removed and then invoked.\n * @returns this MessageStream, so that calls can be chained\n */\n once<Event extends keyof MessageStreamEvents>(event: Event, listener: MessageStreamEvents[Event]): this {\n const listeners: MessageStreamEventListeners<Event> =\n this.#listeners[event] || (this.#listeners[event] = []);\n listeners.push({ listener, once: true });\n return this;\n }\n\n /**\n * This is similar to `.once()`, but returns a Promise that resolves the next time\n * the event is triggered, instead of calling a listener callback.\n * @returns a Promise that resolves the next time given event is triggered,\n * or rejects if an error is emitted. (If you request the 'error' event,\n * returns a promise that resolves with the error).\n *\n * Example:\n *\n * const message = await stream.emitted('message') // rejects if the stream errors\n */\n emitted<Event extends keyof MessageStreamEvents>(\n event: Event,\n ): Promise<\n Parameters<MessageStreamEvents[Event]> extends [infer Param] ? Param\n : Parameters<MessageStreamEvents[Event]> extends [] ? void\n : Parameters<MessageStreamEvents[Event]>\n > {\n return new Promise((resolve, reject) => {\n this.#catchingPromiseCreated = true;\n if (event !== 'error') this.once('error', reject);\n this.once(event, resolve as any);\n });\n }\n\n async done(): Promise<void> {\n this.#catchingPromiseCreated = true;\n await this.#endPromise;\n }\n\n get currentMessage(): BetaMessage | undefined {\n return this.#currentMessageSnapshot;\n }\n\n #getFinalMessage(): BetaMessage {\n if (this.receivedMessages.length === 0) {\n throw new AnthropicError('stream ended without producing a Message with role=assistant');\n }\n return this.receivedMessages.at(-1)!;\n }\n\n /**\n * @returns a promise that resolves with the the final assistant Message response,\n * or rejects if an error occurred or the stream ended prematurely without producing a Message.\n */\n async finalMessage(): Promise<BetaMessage> {\n await this.done();\n return this.#getFinalMessage();\n }\n\n #getFinalText(): string {\n if (this.receivedMessages.length === 0) {\n throw new AnthropicError('stream ended without producing a Message with role=assistant');\n }\n const textBlocks = this.receivedMessages\n .at(-1)!\n .content.filter((block): block is BetaTextBlock => block.type === 'text')\n .map((block) => block.text);\n if (textBlocks.length === 0) {\n throw new AnthropicError('stream ended without producing a content block with type=text');\n }\n return textBlocks.join(' ');\n }\n\n /**\n * @returns a promise that resolves with the the final assistant Message's text response, concatenated\n * together if there are more than one text blocks.\n * Rejects if an error occurred or the stream ended prematurely without producing a Message.\n */\n async finalText(): Promise<string> {\n await this.done();\n return this.#getFinalText();\n }\n\n #handleError = (error: unknown) => {\n this.#errored = true;\n if (error instanceof Error && error.name === 'AbortError') {\n error = new APIUserAbortError();\n }\n if (error instanceof APIUserAbortError) {\n this.#aborted = true;\n return this._emit('abort', error);\n }\n if (error instanceof AnthropicError) {\n return this._emit('error', error);\n }\n if (error instanceof Error) {\n const anthropicError: AnthropicError = new AnthropicError(error.message);\n // @ts-ignore\n anthropicError.cause = error;\n return this._emit('error', anthropicError);\n }\n return this._emit('error', new AnthropicError(String(error)));\n };\n\n protected _emit<Event extends keyof MessageStreamEvents>(\n event: Event,\n ...args: Parameters<MessageStreamEvents[Event]>\n ) {\n // make sure we don't emit any MessageStreamEvents after end\n if (this.#ended) return;\n\n if (event === 'end') {\n this.#ended = true;\n this.#resolveEndPromise();\n }\n\n const listeners: MessageStreamEventListeners<Event> | undefined = this.#listeners[event];\n if (listeners) {\n this.#listeners[event] = listeners.filter((l) => !l.once) as any;\n listeners.forEach(({ listener }: any) => listener(...args));\n }\n\n if (event === 'abort') {\n const error = args[0] as APIUserAbortError;\n if (!this.#catchingPromiseCreated && !listeners?.length) {\n Promise.reject(error);\n }\n this.#rejectConnectedPromise(error);\n this.#rejectEndPromise(error);\n this._emit('end');\n return;\n }\n\n if (event === 'error') {\n // NOTE: _emit('error', error) should only be called from #handleError().\n\n const error = args[0] as AnthropicError;\n if (!this.#catchingPromiseCreated && !listeners?.length) {\n // Trigger an unhandled rejection if the user hasn't registered any error handlers.\n // If you are seeing stack traces here, make sure to handle errors via either:\n // - runner.on('error', () => ...)\n // - await runner.done()\n // - await runner.final...()\n // - etc.\n Promise.reject(error);\n }\n this.#rejectConnectedPromise(error);\n this.#rejectEndPromise(error);\n this._emit('end');\n }\n }\n\n protected _emitFinal() {\n const finalMessage = this.receivedMessages.at(-1);\n if (finalMessage) {\n this._emit('finalMessage', this.#getFinalMessage());\n }\n }\n\n #beginRequest() {\n if (this.ended) return;\n this.#currentMessageSnapshot = undefined;\n }\n #addStreamEvent(event: BetaMessageStreamEvent) {\n if (this.ended) return;\n const messageSnapshot = this.#accumulateMessage(event);\n this._emit('streamEvent', event, messageSnapshot);\n\n switch (event.type) {\n case 'content_block_delta': {\n const content = messageSnapshot.content.at(-1)!;\n switch (event.delta.type) {\n case 'text_delta': {\n if (content.type === 'text') {\n this._emit('text', event.delta.text, content.text || '');\n }\n break;\n }\n case 'citations_delta': {\n if (content.type === 'text') {\n this._emit('citation', event.delta.citation, content.citations ?? []);\n }\n break;\n }\n case 'input_json_delta': {\n if (content.type === 'tool_use' && content.input) {\n this._emit('inputJson', event.delta.partial_json, content.input);\n }\n break;\n }\n case 'thinking_delta': {\n if (content.type === 'thinking') {\n this._emit('thinking', event.delta.thinking, content.thinking);\n }\n break;\n }\n case 'signature_delta': {\n if (content.type === 'thinking') {\n this._emit('signature', content.signature);\n }\n break;\n }\n default:\n checkNever(event.delta);\n }\n break;\n }\n case 'message_stop': {\n this._addMessageParam(messageSnapshot);\n this._addMessage(messageSnapshot, true);\n break;\n }\n case 'content_block_stop': {\n this._emit('contentBlock', messageSnapshot.content.at(-1)!);\n break;\n }\n case 'message_start': {\n this.#currentMessageSnapshot = messageSnapshot;\n break;\n }\n case 'content_block_start':\n case 'message_delta':\n break;\n }\n }\n #endRequest(): BetaMessage {\n if (this.ended) {\n throw new AnthropicError(`stream has ended, this shouldn't happen`);\n }\n const snapshot = this.#currentMessageSnapshot;\n if (!snapshot) {\n throw new AnthropicError(`request ended without sending any chunks`);\n }\n this.#currentMessageSnapshot = undefined;\n return snapshot;\n }\n\n protected async _fromReadableStream(\n readableStream: ReadableStream,\n options?: Core.RequestOptions,\n ): Promise<void> {\n const signal = options?.signal;\n if (signal) {\n if (signal.aborted) this.controller.abort();\n signal.addEventListener('abort', () => this.controller.abort());\n }\n this.#beginRequest();\n this._connected(null);\n const stream = Stream.fromReadableStream<BetaMessageStreamEvent>(readableStream, this.controller);\n for await (const event of stream) {\n this.#addStreamEvent(event);\n }\n if (stream.controller.signal?.aborted) {\n throw new APIUserAbortError();\n }\n this.#endRequest();\n }\n\n /**\n * Mutates this.#currentMessage with the current event. Handling the accumulation of multiple messages\n * will be needed to be handled by the caller, this method will throw if you try to accumulate for multiple\n * messages.\n */\n #accumulateMessage(event: BetaMessageStreamEvent): BetaMessage {\n let snapshot = this.#currentMessageSnapshot;\n\n if (event.type === 'message_start') {\n if (snapshot) {\n throw new AnthropicError(`Unexpected event order, got ${event.type} before receiving \"message_stop\"`);\n }\n return event.message;\n }\n\n if (!snapshot) {\n throw new AnthropicError(`Unexpected event order, got ${event.type} before \"message_start\"`);\n }\n\n switch (event.type) {\n case 'message_stop':\n return snapshot;\n case 'message_delta':\n snapshot.stop_reason = event.delta.stop_reason;\n snapshot.stop_sequence = event.delta.stop_sequence;\n snapshot.usage.output_tokens = event.usage.output_tokens;\n return snapshot;\n case 'content_block_start':\n snapshot.content.push(event.content_block);\n return snapshot;\n case 'content_block_delta': {\n const snapshotContent = snapshot.content.at(event.index);\n\n switch (event.delta.type) {\n case 'text_delta': {\n if (snapshotContent?.type === 'text') {\n snapshotContent.text += event.delta.text;\n }\n break;\n }\n case 'citations_delta': {\n if (snapshotContent?.type === 'text') {\n snapshotContent.citations ??= [];\n snapshotContent.citations.push(event.delta.citation);\n }\n break;\n }\n case 'input_json_delta': {\n if (snapshotContent?.type === 'tool_use') {\n // we need to keep track of the raw JSON string as well so that we can\n // re-parse it for each delta, for now we just store it as an untyped\n // non-enumerable property on the snapshot\n let jsonBuf = (snapshotContent as any)[JSON_BUF_PROPERTY] || '';\n jsonBuf += event.delta.partial_json;\n\n Object.defineProperty(snapshotContent, JSON_BUF_PROPERTY, {\n value: jsonBuf,\n enumerable: false,\n writable: true,\n });\n\n if (jsonBuf) {\n snapshotContent.input = partialParse(jsonBuf);\n }\n }\n break;\n }\n case 'thinking_delta': {\n if (snapshotContent?.type === 'thinking') {\n snapshotContent.thinking += event.delta.thinking;\n }\n break;\n }\n case 'signature_delta': {\n if (snapshotContent?.type === 'thinking') {\n snapshotContent.signature = event.delta.signature;\n }\n break;\n }\n default:\n checkNever(event.delta);\n }\n return snapshot;\n }\n case 'content_block_stop':\n return snapshot;\n }\n }\n\n [Symbol.asyncIterator](): AsyncIterator<BetaMessageStreamEvent> {\n const pushQueue: BetaMessageStreamEvent[] = [];\n const readQueue: {\n resolve: (chunk: BetaMessageStreamEvent | undefined) => void;\n reject: (error: unknown) => void;\n }[] = [];\n let done = false;\n\n this.on('streamEvent', (event) => {\n const reader = readQueue.shift();\n if (reader) {\n reader.resolve(event);\n } else {\n pushQueue.push(event);\n }\n });\n\n this.on('end', () => {\n done = true;\n for (const reader of readQueue) {\n reader.resolve(undefined);\n }\n readQueue.length = 0;\n });\n\n this.on('abort', (err) => {\n done = true;\n for (const reader of readQueue) {\n reader.reject(err);\n }\n readQueue.length = 0;\n });\n\n this.on('error', (err) => {\n done = true;\n for (const reader of readQueue) {\n reader.reject(err);\n }\n readQueue.length = 0;\n });\n\n return {\n next: async (): Promise<IteratorResult<BetaMessageStreamEvent>> => {\n if (!pushQueue.length) {\n if (done) {\n return { value: undefined, done: true };\n }\n return new Promise<BetaMessageStreamEvent | undefined>((resolve, reject) =>\n readQueue.push({ resolve, reject }),\n ).then((chunk) => (chunk ? { value: chunk, done: false } : { value: undefined, done: true }));\n }\n const chunk = pushQueue.shift()!;\n return { value: chunk, done: false };\n },\n return: async () => {\n this.abort();\n return { value: undefined, done: true };\n },\n };\n }\n\n toReadableStream(): ReadableStream {\n const stream = new Stream(this[Symbol.asyncIterator].bind(this), this.controller);\n return stream.toReadableStream();\n }\n}\n\n// used to ensure exhaustive case matching without throwing a runtime error\nfunction checkNever(x: never) {}\n", "// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\n\nimport { APIResource } from \"../../../resource.js\";\nimport { APIPromise } from \"../../../core.js\";\nimport * as Core from \"../../../core.js\";\nimport * as MessagesMessagesAPI from \"./messages.js\";\nimport * as BetaAPI from \"../beta.js\";\nimport * as MessagesAPI from \"../../messages/messages.js\";\nimport * as BatchesAPI from \"./batches.js\";\nimport {\n BatchCancelParams,\n BatchCreateParams,\n BatchDeleteParams,\n BatchListParams,\n BatchResultsParams,\n BatchRetrieveParams,\n Batches,\n BetaDeletedMessageBatch,\n BetaMessageBatch,\n BetaMessageBatchCanceledResult,\n BetaMessageBatchErroredResult,\n BetaMessageBatchExpiredResult,\n BetaMessageBatchIndividualResponse,\n BetaMessageBatchRequestCounts,\n BetaMessageBatchResult,\n BetaMessageBatchSucceededResult,\n BetaMessageBatchesPage,\n} from \"./batches.js\";\nimport { Stream } from \"../../../streaming.js\";\nimport { BetaMessageStream } from \"../../../lib/BetaMessageStream.js\";\nimport type { Model } from \"../../messages/messages.js\";\n\nconst DEPRECATED_MODELS: {\n [K in Model]?: string;\n} = {\n 'claude-1.3': 'November 6th, 2024',\n 'claude-1.3-100k': 'November 6th, 2024',\n 'claude-instant-1.1': 'November 6th, 2024',\n 'claude-instant-1.1-100k': 'November 6th, 2024',\n 'claude-instant-1.2': 'November 6th, 2024',\n 'claude-3-sonnet-20240229': 'July 21st, 2025',\n 'claude-2.1': 'July 21st, 2025',\n 'claude-2.0': 'July 21st, 2025',\n};\n\nexport class Messages extends APIResource {\n batches: BatchesAPI.Batches = new BatchesAPI.Batches(this._client);\n\n /**\n * Send a structured list of input messages with text and/or image content, and the\n * model will generate the next message in the conversation.\n *\n * The Messages API can be used for either single queries or stateless multi-turn\n * conversations.\n *\n * Learn more about the Messages API in our [user guide](/en/docs/initial-setup)\n */\n create(params: MessageCreateParamsNonStreaming, options?: Core.RequestOptions): APIPromise<BetaMessage>;\n create(\n params: MessageCreateParamsStreaming,\n options?: Core.RequestOptions,\n ): APIPromise<Stream<BetaRawMessageStreamEvent>>;\n create(\n params: MessageCreateParamsBase,\n options?: Core.RequestOptions,\n ): APIPromise<Stream<BetaRawMessageStreamEvent> | BetaMessage>;\n create(\n params: MessageCreateParams,\n options?: Core.RequestOptions,\n ): APIPromise<BetaMessage> | APIPromise<Stream<BetaRawMessageStreamEvent>> {\n const { betas, ...body } = params;\n\n if (body.model in DEPRECATED_MODELS) {\n console.warn(\n `The model '${body.model}' is deprecated and will reach end-of-life on ${\n DEPRECATED_MODELS[body.model]\n }\\nPlease migrate to a newer model. Visit https://docs.anthropic.com/en/docs/resources/model-deprecations for more information.`,\n );\n }\n\n return this._client.post('/v1/messages?beta=true', {\n body,\n timeout:\n (this._client as any)._options.timeout ??\n (body.stream ? 600000 : this._client._calculateNonstreamingTimeout(body.max_tokens)),\n ...options,\n headers: {\n ...(betas?.toString() != null ? { 'anthropic-beta': betas?.toString() } : undefined),\n ...options?.headers,\n },\n stream: params.stream ?? false,\n }) as APIPromise<BetaMessage> | APIPromise<Stream<BetaRawMessageStreamEvent>>;\n }\n\n /**\n * Create a Message stream\n */\n stream(body: BetaMessageStreamParams, options?: Core.RequestOptions): BetaMessageStream {\n return BetaMessageStream.createMessage(this, body, options);\n }\n\n /**\n * Count the number of tokens in a Message.\n *\n * The Token Count API can be used to count the number of tokens in a Message,\n * including tools, images, and documents, without creating it.\n *\n * Learn more about token counting in our\n * [user guide](/en/docs/build-with-claude/token-counting)\n */\n countTokens(\n params: MessageCountTokensParams,\n options?: Core.RequestOptions,\n ): Core.APIPromise<BetaMessageTokensCount> {\n const { betas, ...body } = params;\n return this._client.post('/v1/messages/count_tokens?beta=true', {\n body,\n ...options,\n headers: {\n 'anthropic-beta': [...(betas ?? []), 'token-counting-2024-11-01'].toString(),\n ...options?.headers,\n },\n });\n }\n}\n\nexport type BetaMessageStreamParams = MessageCreateParamsBase;\n\nexport interface BetaBase64ImageSource {\n data: string;\n\n media_type: 'image/jpeg' | 'image/png' | 'image/gif' | 'image/webp';\n\n type: 'base64';\n}\n\nexport interface BetaBase64PDFBlock {\n source: BetaBase64PDFSource | BetaPlainTextSource | BetaContentBlockSource | BetaURLPDFSource;\n\n type: 'document';\n\n cache_control?: BetaCacheControlEphemeral | null;\n\n citations?: BetaCitationsConfigParam;\n\n context?: string | null;\n\n title?: string | null;\n}\n\nexport interface BetaBase64PDFSource {\n data: string;\n\n media_type: 'application/pdf';\n\n type: 'base64';\n}\n\nexport interface BetaCacheControlEphemeral {\n type: 'ephemeral';\n}\n\nexport interface BetaCitationCharLocation {\n cited_text: string;\n\n document_index: number;\n\n document_title: string | null;\n\n end_char_index: number;\n\n start_char_index: number;\n\n type: 'char_location';\n}\n\nexport interface BetaCitationCharLocationParam {\n cited_text: string;\n\n document_index: number;\n\n document_title: string | null;\n\n end_char_index: number;\n\n start_char_index: number;\n\n type: 'char_location';\n}\n\nexport interface BetaCitationContentBlockLocation {\n cited_text: string;\n\n document_index: number;\n\n document_title: string | null;\n\n end_block_index: number;\n\n start_block_index: number;\n\n type: 'content_block_location';\n}\n\nexport interface BetaCitationContentBlockLocationParam {\n cited_text: string;\n\n document_index: number;\n\n document_title: string | null;\n\n end_block_index: number;\n\n start_block_index: number;\n\n type: 'content_block_location';\n}\n\nexport interface BetaCitationPageLocation {\n cited_text: string;\n\n document_index: number;\n\n document_title: string | null;\n\n end_page_number: number;\n\n start_page_number: number;\n\n type: 'page_location';\n}\n\nexport interface BetaCitationPageLocationParam {\n cited_text: string;\n\n document_index: number;\n\n document_title: string | null;\n\n end_page_number: number;\n\n start_page_number: number;\n\n type: 'page_location';\n}\n\nexport interface BetaCitationsConfigParam {\n enabled?: boolean;\n}\n\nexport interface BetaCitationsDelta {\n citation: BetaCitationCharLocation | BetaCitationPageLocation | BetaCitationContentBlockLocation;\n\n type: 'citations_delta';\n}\n\nexport type BetaContentBlock =\n | BetaTextBlock\n | BetaToolUseBlock\n | BetaThinkingBlock\n | BetaRedactedThinkingBlock;\n\nexport type BetaContentBlockParam =\n | BetaTextBlockParam\n | BetaImageBlockParam\n | BetaToolUseBlockParam\n | BetaToolResultBlockParam\n | BetaBase64PDFBlock\n | BetaThinkingBlockParam\n | BetaRedactedThinkingBlockParam;\n\nexport interface BetaContentBlockSource {\n content: string | Array<BetaContentBlockSourceContent>;\n\n type: 'content';\n}\n\nexport type BetaContentBlockSourceContent = BetaTextBlockParam | BetaImageBlockParam;\n\nexport interface BetaImageBlockParam {\n source: BetaBase64ImageSource | BetaURLImageSource;\n\n type: 'image';\n\n cache_control?: BetaCacheControlEphemeral | null;\n}\n\nexport interface BetaInputJSONDelta {\n partial_json: string;\n\n type: 'input_json_delta';\n}\n\nexport interface BetaMessage {\n /**\n * Unique object identifier.\n *\n * The format and length of IDs may change over time.\n */\n id: string;\n\n /**\n * Content generated by the model.\n *\n * This is an array of content blocks, each of which has a `type` that determines\n * its shape.\n *\n * Example:\n *\n * ```json\n * [{ \"type\": \"text\", \"text\": \"Hi, I'm Claude.\" }]\n * ```\n *\n * If the request input `messages` ended with an `assistant` turn, then the\n * response `content` will continue directly from that last turn. You can use this\n * to constrain the model's output.\n *\n * For example, if the input `messages` were:\n *\n * ```json\n * [\n * {\n * \"role\": \"user\",\n * \"content\": \"What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun\"\n * },\n * { \"role\": \"assistant\", \"content\": \"The best answer is (\" }\n * ]\n * ```\n *\n * Then the response `content` might be:\n *\n * ```json\n * [{ \"type\": \"text\", \"text\": \"B)\" }]\n * ```\n */\n content: Array<BetaContentBlock>;\n\n /**\n * The model that will complete your prompt.\\n\\nSee\n * [models](https://docs.anthropic.com/en/docs/models-overview) for additional\n * details and options.\n */\n model: MessagesAPI.Model;\n\n /**\n * Conversational role of the generated message.\n *\n * This will always be `\"assistant\"`.\n */\n role: 'assistant';\n\n /**\n * The reason that we stopped.\n *\n * This may be one the following values:\n *\n * - `\"end_turn\"`: the model reached a natural stopping point\n * - `\"max_tokens\"`: we exceeded the requested `max_tokens` or the model's maximum\n * - `\"stop_sequence\"`: one of your provided custom `stop_sequences` was generated\n * - `\"tool_use\"`: the model invoked one or more tools\n *\n * In non-streaming mode this value is always non-null. In streaming mode, it is\n * null in the `message_start` event and non-null otherwise.\n */\n stop_reason: 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use' | null;\n\n /**\n * Which custom stop sequence was generated, if any.\n *\n * This value will be a non-null string if one of your custom stop sequences was\n * generated.\n */\n stop_sequence: string | null;\n\n /**\n * Object type.\n *\n * For Messages, this is always `\"message\"`.\n */\n type: 'message';\n\n /**\n * Billing and rate-limit usage.\n *\n * Anthropic's API bills and rate-limits by token counts, as tokens represent the\n * underlying cost to our systems.\n *\n * Under the hood, the API transforms requests into a format suitable for the\n * model. The model's output then goes through a parsing stage before becoming an\n * API response. As a result, the token counts in `usage` will not match one-to-one\n * with the exact visible content of an API request or response.\n *\n * For example, `output_tokens` will be non-zero, even for an empty string response\n * from Claude.\n *\n * Total input tokens in a request is the summation of `input_tokens`,\n * `cache_creation_input_tokens`, and `cache_read_input_tokens`.\n */\n usage: BetaUsage;\n}\n\nexport interface BetaMessageDeltaUsage {\n /**\n * The cumulative number of output tokens which were used.\n */\n output_tokens: number;\n}\n\nexport interface BetaMessageParam {\n content: string | Array<BetaContentBlockParam>;\n\n role: 'user' | 'assistant';\n}\n\nexport interface BetaMessageTokensCount {\n /**\n * The total number of tokens across the provided list of messages, system prompt,\n * and tools.\n */\n input_tokens: number;\n}\n\nexport interface BetaMetadata {\n /**\n * An external identifier for the user who is associated with the request.\n *\n * This should be a uuid, hash value, or other opaque identifier. Anthropic may use\n * this id to help detect abuse. Do not include any identifying information such as\n * name, email address, or phone number.\n */\n user_id?: string | null;\n}\n\nexport interface BetaPlainTextSource {\n data: string;\n\n media_type: 'text/plain';\n\n type: 'text';\n}\n\nexport interface BetaRawContentBlockDeltaEvent {\n delta: BetaTextDelta | BetaInputJSONDelta | BetaCitationsDelta | BetaThinkingDelta | BetaSignatureDelta;\n\n index: number;\n\n type: 'content_block_delta';\n}\n\nexport interface BetaRawContentBlockStartEvent {\n content_block: BetaTextBlock | BetaToolUseBlock | BetaThinkingBlock | BetaRedactedThinkingBlock;\n\n index: number;\n\n type: 'content_block_start';\n}\n\nexport interface BetaRawContentBlockStopEvent {\n index: number;\n\n type: 'content_block_stop';\n}\n\nexport interface BetaRawMessageDeltaEvent {\n delta: BetaRawMessageDeltaEvent.Delta;\n\n type: 'message_delta';\n\n /**\n * Billing and rate-limit usage.\n *\n * Anthropic's API bills and rate-limits by token counts, as tokens represent the\n * underlying cost to our systems.\n *\n * Under the hood, the API transforms requests into a format suitable for the\n * model. The model's output then goes through a parsing stage before becoming an\n * API response. As a result, the token counts in `usage` will not match one-to-one\n * with the exact visible content of an API request or response.\n *\n * For example, `output_tokens` will be non-zero, even for an empty string response\n * from Claude.\n *\n * Total input tokens in a request is the summation of `input_tokens`,\n * `cache_creation_input_tokens`, and `cache_read_input_tokens`.\n */\n usage: BetaMessageDeltaUsage;\n}\n\nexport namespace BetaRawMessageDeltaEvent {\n export interface Delta {\n stop_reason: 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use' | null;\n\n stop_sequence: string | null;\n }\n}\n\nexport interface BetaRawMessageStartEvent {\n message: BetaMessage;\n\n type: 'message_start';\n}\n\nexport interface BetaRawMessageStopEvent {\n type: 'message_stop';\n}\n\nexport type BetaRawMessageStreamEvent =\n | BetaRawMessageStartEvent\n | BetaRawMessageDeltaEvent\n | BetaRawMessageStopEvent\n | BetaRawContentBlockStartEvent\n | BetaRawContentBlockDeltaEvent\n | BetaRawContentBlockStopEvent;\n\nexport interface BetaRedactedThinkingBlock {\n data: string;\n\n type: 'redacted_thinking';\n}\n\nexport interface BetaRedactedThinkingBlockParam {\n data: string;\n\n type: 'redacted_thinking';\n}\n\nexport interface BetaSignatureDelta {\n signature: string;\n\n type: 'signature_delta';\n}\n\nexport interface BetaTextBlock {\n /**\n * Citations supporting the text block.\n *\n * The type of citation returned will depend on the type of document being cited.\n * Citing a PDF results in `page_location`, plain text results in `char_location`,\n * and content document results in `content_block_location`.\n */\n citations: Array<BetaTextCitation> | null;\n\n text: string;\n\n type: 'text';\n}\n\nexport interface BetaTextBlockParam {\n text: string;\n\n type: 'text';\n\n cache_control?: BetaCacheControlEphemeral | null;\n\n citations?: Array<BetaTextCitationParam> | null;\n}\n\nexport type BetaTextCitation =\n | BetaCitationCharLocation\n | BetaCitationPageLocation\n | BetaCitationContentBlockLocation;\n\nexport type BetaTextCitationParam =\n | BetaCitationCharLocationParam\n | BetaCitationPageLocationParam\n | BetaCitationContentBlockLocationParam;\n\nexport interface BetaTextDelta {\n text: string;\n\n type: 'text_delta';\n}\n\nexport interface BetaThinkingBlock {\n signature: string;\n\n thinking: string;\n\n type: 'thinking';\n}\n\nexport interface BetaThinkingBlockParam {\n signature: string;\n\n thinking: string;\n\n type: 'thinking';\n}\n\nexport interface BetaThinkingConfigDisabled {\n type: 'disabled';\n}\n\nexport interface BetaThinkingConfigEnabled {\n /**\n * Determines how many tokens Claude can use for its internal reasoning process.\n * Larger budgets can enable more thorough analysis for complex problems, improving\n * response quality.\n *\n * Must be \u22651024 and less than `max_tokens`.\n *\n * See\n * [extended thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking)\n * for details.\n */\n budget_tokens: number;\n\n type: 'enabled';\n}\n\n/**\n * Configuration for enabling Claude's extended thinking.\n *\n * When enabled, responses include `thinking` content blocks showing Claude's\n * thinking process before the final answer. Requires a minimum budget of 1,024\n * tokens and counts towards your `max_tokens` limit.\n *\n * See\n * [extended thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking)\n * for details.\n */\nexport type BetaThinkingConfigParam = BetaThinkingConfigEnabled | BetaThinkingConfigDisabled;\n\nexport interface BetaThinkingDelta {\n thinking: string;\n\n type: 'thinking_delta';\n}\n\nexport interface BetaTool {\n /**\n * [JSON schema](https://json-schema.org/draft/2020-12) for this tool's input.\n *\n * This defines the shape of the `input` that your tool accepts and that the model\n * will produce.\n */\n input_schema: BetaTool.InputSchema;\n\n /**\n * Name of the tool.\n *\n * This is how the tool will be called by the model and in tool_use blocks.\n */\n name: string;\n\n cache_control?: BetaCacheControlEphemeral | null;\n\n /**\n * Description of what this tool does.\n *\n * Tool descriptions should be as detailed as possible. The more information that\n * the model has about what the tool is and how to use it, the better it will\n * perform. You can use natural language descriptions to reinforce important\n * aspects of the tool input JSON schema.\n */\n description?: string;\n\n type?: 'custom' | null;\n}\n\nexport namespace BetaTool {\n /**\n * [JSON schema](https://json-schema.org/draft/2020-12) for this tool's input.\n *\n * This defines the shape of the `input` that your tool accepts and that the model\n * will produce.\n */\n export interface InputSchema {\n type: 'object';\n\n properties?: unknown | null;\n [k: string]: unknown;\n }\n}\n\nexport interface BetaToolBash20241022 {\n /**\n * Name of the tool.\n *\n * This is how the tool will be called by the model and in tool_use blocks.\n */\n name: 'bash';\n\n type: 'bash_20241022';\n\n cache_control?: BetaCacheControlEphemeral | null;\n}\n\nexport interface BetaToolBash20250124 {\n /**\n * Name of the tool.\n *\n * This is how the tool will be called by the model and in tool_use blocks.\n */\n name: 'bash';\n\n type: 'bash_20250124';\n\n cache_control?: BetaCacheControlEphemeral | null;\n}\n\n/**\n * How the model should use the provided tools. The model can use a specific tool,\n * any available tool, decide by itself, or not use tools at all.\n */\nexport type BetaToolChoice = BetaToolChoiceAuto | BetaToolChoiceAny | BetaToolChoiceTool | BetaToolChoiceNone;\n\n/**\n * The model will use any available tools.\n */\nexport interface BetaToolChoiceAny {\n type: 'any';\n\n /**\n * Whether to disable parallel tool use.\n *\n * Defaults to `false`. If set to `true`, the model will output exactly one tool\n * use.\n */\n disable_parallel_tool_use?: boolean;\n}\n\n/**\n * The model will automatically decide whether to use tools.\n */\nexport interface BetaToolChoiceAuto {\n type: 'auto';\n\n /**\n * Whether to disable parallel tool use.\n *\n * Defaults to `false`. If set to `true`, the model will output at most one tool\n * use.\n */\n disable_parallel_tool_use?: boolean;\n}\n\n/**\n * The model will not be allowed to use tools.\n */\nexport interface BetaToolChoiceNone {\n type: 'none';\n}\n\n/**\n * The model will use the specified tool with `tool_choice.name`.\n */\nexport interface BetaToolChoiceTool {\n /**\n * The name of the tool to use.\n */\n name: string;\n\n type: 'tool';\n\n /**\n * Whether to disable parallel tool use.\n *\n * Defaults to `false`. If set to `true`, the model will output exactly one tool\n * use.\n */\n disable_parallel_tool_use?: boolean;\n}\n\nexport interface BetaToolComputerUse20241022 {\n /**\n * The height of the display in pixels.\n */\n display_height_px: number;\n\n /**\n * The width of the display in pixels.\n */\n display_width_px: number;\n\n /**\n * Name of the tool.\n *\n * This is how the tool will be called by the model and in tool_use blocks.\n */\n name: 'computer';\n\n type: 'computer_20241022';\n\n cache_control?: BetaCacheControlEphemeral | null;\n\n /**\n * The X11 display number (e.g. 0, 1) for the display.\n */\n display_number?: number | null;\n}\n\nexport interface BetaToolComputerUse20250124 {\n /**\n * The height of the display in pixels.\n */\n display_height_px: number;\n\n /**\n * The width of the display in pixels.\n */\n display_width_px: number;\n\n /**\n * Name of the tool.\n *\n * This is how the tool will be called by the model and in tool_use blocks.\n */\n name: 'computer';\n\n type: 'computer_20250124';\n\n cache_control?: BetaCacheControlEphemeral | null;\n\n /**\n * The X11 display number (e.g. 0, 1) for the display.\n */\n display_number?: number | null;\n}\n\nexport interface BetaToolResultBlockParam {\n tool_use_id: string;\n\n type: 'tool_result';\n\n cache_control?: BetaCacheControlEphemeral | null;\n\n content?: string | Array<BetaTextBlockParam | BetaImageBlockParam>;\n\n is_error?: boolean;\n}\n\nexport interface BetaToolTextEditor20241022 {\n /**\n * Name of the tool.\n *\n * This is how the tool will be called by the model and in tool_use blocks.\n */\n name: 'str_replace_editor';\n\n type: 'text_editor_20241022';\n\n cache_control?: BetaCacheControlEphemeral | null;\n}\n\nexport interface BetaToolTextEditor20250124 {\n /**\n * Name of the tool.\n *\n * This is how the tool will be called by the model and in tool_use blocks.\n */\n name: 'str_replace_editor';\n\n type: 'text_editor_20250124';\n\n cache_control?: BetaCacheControlEphemeral | null;\n}\n\nexport type BetaToolUnion =\n | BetaTool\n | BetaToolComputerUse20241022\n | BetaToolBash20241022\n | BetaToolTextEditor20241022\n | BetaToolComputerUse20250124\n | BetaToolBash20250124\n | BetaToolTextEditor20250124;\n\nexport interface BetaToolUseBlock {\n id: string;\n\n input: unknown;\n\n name: string;\n\n type: 'tool_use';\n}\n\nexport interface BetaToolUseBlockParam {\n id: string;\n\n input: unknown;\n\n name: string;\n\n type: 'tool_use';\n\n cache_control?: BetaCacheControlEphemeral | null;\n}\n\nexport interface BetaURLImageSource {\n type: 'url';\n\n url: string;\n}\n\nexport interface BetaURLPDFSource {\n type: 'url';\n\n url: string;\n}\n\nexport interface BetaUsage {\n /**\n * The number of input tokens used to create the cache entry.\n */\n cache_creation_input_tokens: number | null;\n\n /**\n * The number of input tokens read from the cache.\n */\n cache_read_input_tokens: number | null;\n\n /**\n * The number of input tokens which were used.\n */\n input_tokens: number;\n\n /**\n * The number of output tokens which were used.\n */\n output_tokens: number;\n}\n\nexport type MessageCreateParams = MessageCreateParamsNonStreaming | MessageCreateParamsStreaming;\n\nexport interface MessageCreateParamsBase {\n /**\n * Body param: The maximum number of tokens to generate before stopping.\n *\n * Note that our models may stop _before_ reaching this maximum. This parameter\n * only specifies the absolute maximum number of tokens to generate.\n *\n * Different models have different maximum values for this parameter. See\n * [models](https://docs.anthropic.com/en/docs/models-overview) for details.\n */\n max_tokens: number;\n\n /**\n * Body param: Input messages.\n *\n * Our models are trained to operate on alternating `user` and `assistant`\n * conversational turns. When creating a new `Message`, you specify the prior\n * conversational turns with the `messages` parameter, and the model then generates\n * the next `Message` in the conversation. Consecutive `user` or `assistant` turns\n * in your request will be combined into a single turn.\n *\n * Each input message must be an object with a `role` and `content`. You can\n * specify a single `user`-role message, or you can include multiple `user` and\n * `assistant` messages.\n *\n * If the final message uses the `assistant` role, the response content will\n * continue immediately from the content in that message. This can be used to\n * constrain part of the model's response.\n *\n * Example with a single `user` message:\n *\n * ```json\n * [{ \"role\": \"user\", \"content\": \"Hello, Claude\" }]\n * ```\n *\n * Example with multiple conversational turns:\n *\n * ```json\n * [\n * { \"role\": \"user\", \"content\": \"Hello there.\" },\n * { \"role\": \"assistant\", \"content\": \"Hi, I'm Claude. How can I help you?\" },\n * { \"role\": \"user\", \"content\": \"Can you explain LLMs in plain English?\" }\n * ]\n * ```\n *\n * Example with a partially-filled response from Claude:\n *\n * ```json\n * [\n * {\n * \"role\": \"user\",\n * \"content\": \"What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun\"\n * },\n * { \"role\": \"assistant\", \"content\": \"The best answer is (\" }\n * ]\n * ```\n *\n * Each input message `content` may be either a single `string` or an array of\n * content blocks, where each block has a specific `type`. Using a `string` for\n * `content` is shorthand for an array of one content block of type `\"text\"`. The\n * following input messages are equivalent:\n *\n * ```json\n * { \"role\": \"user\", \"content\": \"Hello, Claude\" }\n * ```\n *\n * ```json\n * { \"role\": \"user\", \"content\": [{ \"type\": \"text\", \"text\": \"Hello, Claude\" }] }\n * ```\n *\n * Starting with Claude 3 models, you can also send image content blocks:\n *\n * ```json\n * {\n * \"role\": \"user\",\n * \"content\": [\n * {\n * \"type\": \"image\",\n * \"source\": {\n * \"type\": \"base64\",\n * \"media_type\": \"image/jpeg\",\n * \"data\": \"/9j/4AAQSkZJRg...\"\n * }\n * },\n * { \"type\": \"text\", \"text\": \"What is in this image?\" }\n * ]\n * }\n * ```\n *\n * We currently support the `base64` source type for images, and the `image/jpeg`,\n * `image/png`, `image/gif`, and `image/webp` media types.\n *\n * See [examples](https://docs.anthropic.com/en/api/messages-examples#vision) for\n * more input examples.\n *\n * Note that if you want to include a\n * [system prompt](https://docs.anthropic.com/en/docs/system-prompts), you can use\n * the top-level `system` parameter \u2014 there is no `\"system\"` role for input\n * messages in the Messages API.\n */\n messages: Array<BetaMessageParam>;\n\n /**\n * Body param: The model that will complete your prompt.\\n\\nSee\n * [models](https://docs.anthropic.com/en/docs/models-overview) for additional\n * details and options.\n */\n model: MessagesAPI.Model;\n\n /**\n * Body param: An object describing metadata about the request.\n */\n metadata?: BetaMetadata;\n\n /**\n * Body param: Custom text sequences that will cause the model to stop generating.\n *\n * Our models will normally stop when they have naturally completed their turn,\n * which will result in a response `stop_reason` of `\"end_turn\"`.\n *\n * If you want the model to stop generating when it encounters custom strings of\n * text, you can use the `stop_sequences` parameter. If the model encounters one of\n * the custom sequences, the response `stop_reason` value will be `\"stop_sequence\"`\n * and the response `stop_sequence` value will contain the matched stop sequence.\n */\n stop_sequences?: Array<string>;\n\n /**\n * Body param: Whether to incrementally stream the response using server-sent\n * events.\n *\n * See [streaming](https://docs.anthropic.com/en/api/messages-streaming) for\n * details.\n */\n stream?: boolean;\n\n /**\n * Body param: System prompt.\n *\n * A system prompt is a way of providing context and instructions to Claude, such\n * as specifying a particular goal or role. See our\n * [guide to system prompts](https://docs.anthropic.com/en/docs/system-prompts).\n */\n system?: string | Array<BetaTextBlockParam>;\n\n /**\n * Body param: Amount of randomness injected into the response.\n *\n * Defaults to `1.0`. Ranges from `0.0` to `1.0`. Use `temperature` closer to `0.0`\n * for analytical / multiple choice, and closer to `1.0` for creative and\n * generative tasks.\n *\n * Note that even with `temperature` of `0.0`, the results will not be fully\n * deterministic.\n */\n temperature?: number;\n\n /**\n * Body param: Configuration for enabling Claude's extended thinking.\n *\n * When enabled, responses include `thinking` content blocks showing Claude's\n * thinking process before the final answer. Requires a minimum budget of 1,024\n * tokens and counts towards your `max_tokens` limit.\n *\n * See\n * [extended thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking)\n * for details.\n */\n thinking?: BetaThinkingConfigParam;\n\n /**\n * Body param: How the model should use the provided tools. The model can use a\n * specific tool, any available tool, decide by itself, or not use tools at all.\n */\n tool_choice?: BetaToolChoice;\n\n /**\n * Body param: Definitions of tools that the model may use.\n *\n * If you include `tools` in your API request, the model may return `tool_use`\n * content blocks that represent the model's use of those tools. You can then run\n * those tools using the tool input generated by the model and then optionally\n * return results back to the model using `tool_result` content blocks.\n *\n * Each tool definition includes:\n *\n * - `name`: Name of the tool.\n * - `description`: Optional, but strongly-recommended description of the tool.\n * - `input_schema`: [JSON schema](https://json-schema.org/draft/2020-12) for the\n * tool `input` shape that the model will produce in `tool_use` output content\n * blocks.\n *\n * For example, if you defined `tools` as:\n *\n * ```json\n * [\n * {\n * \"name\": \"get_stock_price\",\n * \"description\": \"Get the current stock price for a given ticker symbol.\",\n * \"input_schema\": {\n * \"type\": \"object\",\n * \"properties\": {\n * \"ticker\": {\n * \"type\": \"string\",\n * \"description\": \"The stock ticker symbol, e.g. AAPL for Apple Inc.\"\n * }\n * },\n * \"required\": [\"ticker\"]\n * }\n * }\n * ]\n * ```\n *\n * And then asked the model \"What's the S&P 500 at today?\", the model might produce\n * `tool_use` content blocks in the response like this:\n *\n * ```json\n * [\n * {\n * \"type\": \"tool_use\",\n * \"id\": \"toolu_01D7FLrfh4GYq7yT1ULFeyMV\",\n * \"name\": \"get_stock_price\",\n * \"input\": { \"ticker\": \"^GSPC\" }\n * }\n * ]\n * ```\n *\n * You might then run your `get_stock_price` tool with `{\"ticker\": \"^GSPC\"}` as an\n * input, and return the following back to the model in a subsequent `user`\n * message:\n *\n * ```json\n * [\n * {\n * \"type\": \"tool_result\",\n * \"tool_use_id\": \"toolu_01D7FLrfh4GYq7yT1ULFeyMV\",\n * \"content\": \"259.75 USD\"\n * }\n * ]\n * ```\n *\n * Tools can be used for workflows that include running client-side tools and\n * functions, or more generally whenever you want the model to produce a particular\n * JSON structure of output.\n *\n * See our [guide](https://docs.anthropic.com/en/docs/tool-use) for more details.\n */\n tools?: Array<BetaToolUnion>;\n\n /**\n * Body param: Only sample from the top K options for each subsequent token.\n *\n * Used to remove \"long tail\" low probability responses.\n * [Learn more technical details here](https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277).\n *\n * Recommended for advanced use cases only. You usually only need to use\n * `temperature`.\n */\n top_k?: number;\n\n /**\n * Body param: Use nucleus sampling.\n *\n * In nucleus sampling, we compute the cumulative distribution over all the options\n * for each subsequent token in decreasing probability order and cut it off once it\n * reaches a particular probability specified by `top_p`. You should either alter\n * `temperature` or `top_p`, but not both.\n *\n * Recommended for advanced use cases only. You usually only need to use\n * `temperature`.\n */\n top_p?: number;\n\n /**\n * Header param: Optional header to specify the beta version(s) you want to use.\n */\n betas?: Array<BetaAPI.AnthropicBeta>;\n}\n\nexport namespace MessageCreateParams {\n export type MessageCreateParamsNonStreaming = MessagesMessagesAPI.MessageCreateParamsNonStreaming;\n export type MessageCreateParamsStreaming = MessagesMessagesAPI.MessageCreateParamsStreaming;\n}\n\nexport interface MessageCreateParamsNonStreaming extends MessageCreateParamsBase {\n /**\n * Body param: Whether to incrementally stream the response using server-sent\n * events.\n *\n * See [streaming](https://docs.anthropic.com/en/api/messages-streaming) for\n * details.\n */\n stream?: false;\n}\n\nexport interface MessageCreateParamsStreaming extends MessageCreateParamsBase {\n /**\n * Body param: Whether to incrementally stream the response using server-sent\n * events.\n *\n * See [streaming](https://docs.anthropic.com/en/api/messages-streaming) for\n * details.\n */\n stream: true;\n}\n\nexport interface MessageCountTokensParams {\n /**\n * Body param: Input messages.\n *\n * Our models are trained to operate on alternating `user` and `assistant`\n * conversational turns. When creating a new `Message`, you specify the prior\n * conversational turns with the `messages` parameter, and the model then generates\n * the next `Message` in the conversation. Consecutive `user` or `assistant` turns\n * in your request will be combined into a single turn.\n *\n * Each input message must be an object with a `role` and `content`. You can\n * specify a single `user`-role message, or you can include multiple `user` and\n * `assistant` messages.\n *\n * If the final message uses the `assistant` role, the response content will\n * continue immediately from the content in that message. This can be used to\n * constrain part of the model's response.\n *\n * Example with a single `user` message:\n *\n * ```json\n * [{ \"role\": \"user\", \"content\": \"Hello, Claude\" }]\n * ```\n *\n * Example with multiple conversational turns:\n *\n * ```json\n * [\n * { \"role\": \"user\", \"content\": \"Hello there.\" },\n * { \"role\": \"assistant\", \"content\": \"Hi, I'm Claude. How can I help you?\" },\n * { \"role\": \"user\", \"content\": \"Can you explain LLMs in plain English?\" }\n * ]\n * ```\n *\n * Example with a partially-filled response from Claude:\n *\n * ```json\n * [\n * {\n * \"role\": \"user\",\n * \"content\": \"What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun\"\n * },\n * { \"role\": \"assistant\", \"content\": \"The best answer is (\" }\n * ]\n * ```\n *\n * Each input message `content` may be either a single `string` or an array of\n * content blocks, where each block has a specific `type`. Using a `string` for\n * `content` is shorthand for an array of one content block of type `\"text\"`. The\n * following input messages are equivalent:\n *\n * ```json\n * { \"role\": \"user\", \"content\": \"Hello, Claude\" }\n * ```\n *\n * ```json\n * { \"role\": \"user\", \"content\": [{ \"type\": \"text\", \"text\": \"Hello, Claude\" }] }\n * ```\n *\n * Starting with Claude 3 models, you can also send image content blocks:\n *\n * ```json\n * {\n * \"role\": \"user\",\n * \"content\": [\n * {\n * \"type\": \"image\",\n * \"source\": {\n * \"type\": \"base64\",\n * \"media_type\": \"image/jpeg\",\n * \"data\": \"/9j/4AAQSkZJRg...\"\n * }\n * },\n * { \"type\": \"text\", \"text\": \"What is in this image?\" }\n * ]\n * }\n * ```\n *\n * We currently support the `base64` source type for images, and the `image/jpeg`,\n * `image/png`, `image/gif`, and `image/webp` media types.\n *\n * See [examples](https://docs.anthropic.com/en/api/messages-examples#vision) for\n * more input examples.\n *\n * Note that if you want to include a\n * [system prompt](https://docs.anthropic.com/en/docs/system-prompts), you can use\n * the top-level `system` parameter \u2014 there is no `\"system\"` role for input\n * messages in the Messages API.\n */\n messages: Array<BetaMessageParam>;\n\n /**\n * Body param: The model that will complete your prompt.\\n\\nSee\n * [models](https://docs.anthropic.com/en/docs/models-overview) for additional\n * details and options.\n */\n model: MessagesAPI.Model;\n\n /**\n * Body param: System prompt.\n *\n * A system prompt is a way of providing context and instructions to Claude, such\n * as specifying a particular goal or role. See our\n * [guide to system prompts](https://docs.anthropic.com/en/docs/system-prompts).\n */\n system?: string | Array<BetaTextBlockParam>;\n\n /**\n * Body param: Configuration for enabling Claude's extended thinking.\n *\n * When enabled, responses include `thinking` content blocks showing Claude's\n * thinking process before the final answer. Requires a minimum budget of 1,024\n * tokens and counts towards your `max_tokens` limit.\n *\n * See\n * [extended thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking)\n * for details.\n */\n thinking?: BetaThinkingConfigParam;\n\n /**\n * Body param: How the model should use the provided tools. The model can use a\n * specific tool, any available tool, decide by itself, or not use tools at all.\n */\n tool_choice?: BetaToolChoice;\n\n /**\n * Body param: Definitions of tools that the model may use.\n *\n * If you include `tools` in your API request, the model may return `tool_use`\n * content blocks that represent the model's use of those tools. You can then run\n * those tools using the tool input generated by the model and then optionally\n * return results back to the model using `tool_result` content blocks.\n *\n * Each tool definition includes:\n *\n * - `name`: Name of the tool.\n * - `description`: Optional, but strongly-recommended description of the tool.\n * - `input_schema`: [JSON schema](https://json-schema.org/draft/2020-12) for the\n * tool `input` shape that the model will produce in `tool_use` output content\n * blocks.\n *\n * For example, if you defined `tools` as:\n *\n * ```json\n * [\n * {\n * \"name\": \"get_stock_price\",\n * \"description\": \"Get the current stock price for a given ticker symbol.\",\n * \"input_schema\": {\n * \"type\": \"object\",\n * \"properties\": {\n * \"ticker\": {\n * \"type\": \"string\",\n * \"description\": \"The stock ticker symbol, e.g. AAPL for Apple Inc.\"\n * }\n * },\n * \"required\": [\"ticker\"]\n * }\n * }\n * ]\n * ```\n *\n * And then asked the model \"What's the S&P 500 at today?\", the model might produce\n * `tool_use` content blocks in the response like this:\n *\n * ```json\n * [\n * {\n * \"type\": \"tool_use\",\n * \"id\": \"toolu_01D7FLrfh4GYq7yT1ULFeyMV\",\n * \"name\": \"get_stock_price\",\n * \"input\": { \"ticker\": \"^GSPC\" }\n * }\n * ]\n * ```\n *\n * You might then run your `get_stock_price` tool with `{\"ticker\": \"^GSPC\"}` as an\n * input, and return the following back to the model in a subsequent `user`\n * message:\n *\n * ```json\n * [\n * {\n * \"type\": \"tool_result\",\n * \"tool_use_id\": \"toolu_01D7FLrfh4GYq7yT1ULFeyMV\",\n * \"content\": \"259.75 USD\"\n * }\n * ]\n * ```\n *\n * Tools can be used for workflows that include running client-side tools and\n * functions, or more generally whenever you want the model to produce a particular\n * JSON structure of output.\n *\n * See our [guide](https://docs.anthropic.com/en/docs/tool-use) for more details.\n */\n tools?: Array<\n | BetaTool\n | BetaToolComputerUse20241022\n | BetaToolBash20241022\n | BetaToolTextEditor20241022\n | BetaToolComputerUse20250124\n | BetaToolBash20250124\n | BetaToolTextEditor20250124\n >;\n\n /**\n * Header param: Optional header to specify the beta version(s) you want to use.\n */\n betas?: Array<BetaAPI.AnthropicBeta>;\n}\n\nMessages.Batches = Batches;\nMessages.BetaMessageBatchesPage = BetaMessageBatchesPage;\n\nexport declare namespace Messages {\n export {\n type BetaBase64ImageSource as BetaBase64ImageSource,\n type BetaBase64PDFBlock as BetaBase64PDFBlock,\n type BetaBase64PDFSource as BetaBase64PDFSource,\n type BetaCacheControlEphemeral as BetaCacheControlEphemeral,\n type BetaCitationCharLocation as BetaCitationCharLocation,\n type BetaCitationCharLocationParam as BetaCitationCharLocationParam,\n type BetaCitationContentBlockLocation as BetaCitationContentBlockLocation,\n type BetaCitationContentBlockLocationParam as BetaCitationContentBlockLocationParam,\n type BetaCitationPageLocation as BetaCitationPageLocation,\n type BetaCitationPageLocationParam as BetaCitationPageLocationParam,\n type BetaCitationsConfigParam as BetaCitationsConfigParam,\n type BetaCitationsDelta as BetaCitationsDelta,\n type BetaContentBlock as BetaContentBlock,\n type BetaContentBlockParam as BetaContentBlockParam,\n type BetaContentBlockSource as BetaContentBlockSource,\n type BetaContentBlockSourceContent as BetaContentBlockSourceContent,\n type BetaImageBlockParam as BetaImageBlockParam,\n type BetaInputJSONDelta as BetaInputJSONDelta,\n type BetaMessage as BetaMessage,\n type BetaMessageDeltaUsage as BetaMessageDeltaUsage,\n type BetaMessageParam as BetaMessageParam,\n type BetaMessageTokensCount as BetaMessageTokensCount,\n type BetaMetadata as BetaMetadata,\n type BetaPlainTextSource as BetaPlainTextSource,\n type BetaRawContentBlockDeltaEvent as BetaRawContentBlockDeltaEvent,\n type BetaRawContentBlockStartEvent as BetaRawContentBlockStartEvent,\n type BetaRawContentBlockStopEvent as BetaRawContentBlockStopEvent,\n type BetaRawMessageDeltaEvent as BetaRawMessageDeltaEvent,\n type BetaRawMessageStartEvent as BetaRawMessageStartEvent,\n type BetaRawMessageStopEvent as BetaRawMessageStopEvent,\n type BetaRawMessageStreamEvent as BetaRawMessageStreamEvent,\n type BetaRedactedThinkingBlock as BetaRedactedThinkingBlock,\n type BetaRedactedThinkingBlockParam as BetaRedactedThinkingBlockParam,\n type BetaSignatureDelta as BetaSignatureDelta,\n type BetaTextBlock as BetaTextBlock,\n type BetaTextBlockParam as BetaTextBlockParam,\n type BetaTextCitation as BetaTextCitation,\n type BetaTextCitationParam as BetaTextCitationParam,\n type BetaTextDelta as BetaTextDelta,\n type BetaThinkingBlock as BetaThinkingBlock,\n type BetaThinkingBlockParam as BetaThinkingBlockParam,\n type BetaThinkingConfigDisabled as BetaThinkingConfigDisabled,\n type BetaThinkingConfigEnabled as BetaThinkingConfigEnabled,\n type BetaThinkingConfigParam as BetaThinkingConfigParam,\n type BetaThinkingDelta as BetaThinkingDelta,\n type BetaTool as BetaTool,\n type BetaToolBash20241022 as BetaToolBash20241022,\n type BetaToolBash20250124 as BetaToolBash20250124,\n type BetaToolChoice as BetaToolChoice,\n type BetaToolChoiceAny as BetaToolChoiceAny,\n type BetaToolChoiceAuto as BetaToolChoiceAuto,\n type BetaToolChoiceNone as BetaToolChoiceNone,\n type BetaToolChoiceTool as BetaToolChoiceTool,\n type BetaToolComputerUse20241022 as BetaToolComputerUse20241022,\n type BetaToolComputerUse20250124 as BetaToolComputerUse20250124,\n type BetaToolResultBlockParam as BetaToolResultBlockParam,\n type BetaToolTextEditor20241022 as BetaToolTextEditor20241022,\n type BetaToolTextEditor20250124 as BetaToolTextEditor20250124,\n type BetaToolUnion as BetaToolUnion,\n type BetaToolUseBlock as BetaToolUseBlock,\n type BetaToolUseBlockParam as BetaToolUseBlockParam,\n type BetaURLImageSource as BetaURLImageSource,\n type BetaURLPDFSource as BetaURLPDFSource,\n type BetaUsage as BetaUsage,\n type MessageCreateParams as MessageCreateParams,\n type MessageCreateParamsNonStreaming as MessageCreateParamsNonStreaming,\n type MessageCreateParamsStreaming as MessageCreateParamsStreaming,\n type MessageCountTokensParams as MessageCountTokensParams,\n };\n\n export {\n Batches as Batches,\n type BetaDeletedMessageBatch as BetaDeletedMessageBatch,\n type BetaMessageBatch as BetaMessageBatch,\n type BetaMessageBatchCanceledResult as BetaMessageBatchCanceledResult,\n type BetaMessageBatchErroredResult as BetaMessageBatchErroredResult,\n type BetaMessageBatchExpiredResult as BetaMessageBatchExpiredResult,\n type BetaMessageBatchIndividualResponse as BetaMessageBatchIndividualResponse,\n type BetaMessageBatchRequestCounts as BetaMessageBatchRequestCounts,\n type BetaMessageBatchResult as BetaMessageBatchResult,\n type BetaMessageBatchSucceededResult as BetaMessageBatchSucceededResult,\n BetaMessageBatchesPage as BetaMessageBatchesPage,\n type BatchCreateParams as BatchCreateParams,\n type BatchRetrieveParams as BatchRetrieveParams,\n type BatchListParams as BatchListParams,\n type BatchDeleteParams as BatchDeleteParams,\n type BatchCancelParams as BatchCancelParams,\n type BatchResultsParams as BatchResultsParams,\n };\n}\n", "// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\n\nimport { APIResource } from \"../../resource.js\";\nimport * as ModelsAPI from \"./models.js\";\nimport { BetaModelInfo, BetaModelInfosPage, ModelListParams, Models } from \"./models.js\";\nimport * as MessagesAPI from \"./messages/messages.js\";\nimport {\n BetaBase64ImageSource,\n BetaBase64PDFBlock,\n BetaBase64PDFSource,\n BetaCacheControlEphemeral,\n BetaCitationCharLocation,\n BetaCitationCharLocationParam,\n BetaCitationContentBlockLocation,\n BetaCitationContentBlockLocationParam,\n BetaCitationPageLocation,\n BetaCitationPageLocationParam,\n BetaCitationsConfigParam,\n BetaCitationsDelta,\n BetaContentBlock,\n BetaContentBlockParam,\n BetaContentBlockSource,\n BetaContentBlockSourceContent,\n BetaImageBlockParam,\n BetaInputJSONDelta,\n BetaMessage,\n BetaMessageDeltaUsage,\n BetaMessageParam,\n BetaMessageTokensCount,\n BetaMetadata,\n BetaPlainTextSource,\n BetaRawContentBlockDeltaEvent,\n BetaRawContentBlockStartEvent,\n BetaRawContentBlockStopEvent,\n BetaRawMessageDeltaEvent,\n BetaRawMessageStartEvent,\n BetaRawMessageStopEvent,\n BetaRawMessageStreamEvent,\n BetaRedactedThinkingBlock,\n BetaRedactedThinkingBlockParam,\n BetaSignatureDelta,\n BetaTextBlock,\n BetaTextBlockParam,\n BetaTextCitation,\n BetaTextCitationParam,\n BetaTextDelta,\n BetaThinkingBlock,\n BetaThinkingBlockParam,\n BetaThinkingConfigDisabled,\n BetaThinkingConfigEnabled,\n BetaThinkingConfigParam,\n BetaThinkingDelta,\n BetaTool,\n BetaToolBash20241022,\n BetaToolBash20250124,\n BetaToolChoice,\n BetaToolChoiceAny,\n BetaToolChoiceAuto,\n BetaToolChoiceNone,\n BetaToolChoiceTool,\n BetaToolComputerUse20241022,\n BetaToolComputerUse20250124,\n BetaToolResultBlockParam,\n BetaToolTextEditor20241022,\n BetaToolTextEditor20250124,\n BetaToolUnion,\n BetaToolUseBlock,\n BetaToolUseBlockParam,\n BetaURLImageSource,\n BetaURLPDFSource,\n BetaUsage,\n MessageCountTokensParams,\n MessageCreateParams,\n MessageCreateParamsNonStreaming,\n MessageCreateParamsStreaming,\n Messages,\n} from \"./messages/messages.js\";\n\nexport class Beta extends APIResource {\n models: ModelsAPI.Models = new ModelsAPI.Models(this._client);\n messages: MessagesAPI.Messages = new MessagesAPI.Messages(this._client);\n}\n\nexport type AnthropicBeta =\n | (string & {})\n | 'message-batches-2024-09-24'\n | 'prompt-caching-2024-07-31'\n | 'computer-use-2024-10-22'\n | 'computer-use-2025-01-24'\n | 'pdfs-2024-09-25'\n | 'token-counting-2024-11-01'\n | 'token-efficient-tools-2025-02-19'\n | 'output-128k-2025-02-19';\n\nexport interface BetaAPIError {\n message: string;\n\n type: 'api_error';\n}\n\nexport interface BetaAuthenticationError {\n message: string;\n\n type: 'authentication_error';\n}\n\nexport interface BetaBillingError {\n message: string;\n\n type: 'billing_error';\n}\n\nexport type BetaError =\n | BetaInvalidRequestError\n | BetaAuthenticationError\n | BetaBillingError\n | BetaPermissionError\n | BetaNotFoundError\n | BetaRateLimitError\n | BetaGatewayTimeoutError\n | BetaAPIError\n | BetaOverloadedError;\n\nexport interface BetaErrorResponse {\n error: BetaError;\n\n type: 'error';\n}\n\nexport interface BetaGatewayTimeoutError {\n message: string;\n\n type: 'timeout_error';\n}\n\nexport interface BetaInvalidRequestError {\n message: string;\n\n type: 'invalid_request_error';\n}\n\nexport interface BetaNotFoundError {\n message: string;\n\n type: 'not_found_error';\n}\n\nexport interface BetaOverloadedError {\n message: string;\n\n type: 'overloaded_error';\n}\n\nexport interface BetaPermissionError {\n message: string;\n\n type: 'permission_error';\n}\n\nexport interface BetaRateLimitError {\n message: string;\n\n type: 'rate_limit_error';\n}\n\nBeta.Models = Models;\nBeta.BetaModelInfosPage = BetaModelInfosPage;\nBeta.Messages = Messages;\n\nexport declare namespace Beta {\n export {\n type AnthropicBeta as AnthropicBeta,\n type BetaAPIError as BetaAPIError,\n type BetaAuthenticationError as BetaAuthenticationError,\n type BetaBillingError as BetaBillingError,\n type BetaError as BetaError,\n type BetaErrorResponse as BetaErrorResponse,\n type BetaGatewayTimeoutError as BetaGatewayTimeoutError,\n type BetaInvalidRequestError as BetaInvalidRequestError,\n type BetaNotFoundError as BetaNotFoundError,\n type BetaOverloadedError as BetaOverloadedError,\n type BetaPermissionError as BetaPermissionError,\n type BetaRateLimitError as BetaRateLimitError,\n };\n\n export {\n Models as Models,\n type BetaModelInfo as BetaModelInfo,\n BetaModelInfosPage as BetaModelInfosPage,\n type ModelListParams as ModelListParams,\n };\n\n export {\n Messages as Messages,\n type BetaBase64ImageSource as BetaBase64ImageSource,\n type BetaBase64PDFBlock as BetaBase64PDFBlock,\n type BetaBase64PDFSource as BetaBase64PDFSource,\n type BetaCacheControlEphemeral as BetaCacheControlEphemeral,\n type BetaCitationCharLocation as BetaCitationCharLocation,\n type BetaCitationCharLocationParam as BetaCitationCharLocationParam,\n type BetaCitationContentBlockLocation as BetaCitationContentBlockLocation,\n type BetaCitationContentBlockLocationParam as BetaCitationContentBlockLocationParam,\n type BetaCitationPageLocation as BetaCitationPageLocation,\n type BetaCitationPageLocationParam as BetaCitationPageLocationParam,\n type BetaCitationsConfigParam as BetaCitationsConfigParam,\n type BetaCitationsDelta as BetaCitationsDelta,\n type BetaContentBlock as BetaContentBlock,\n type BetaContentBlockParam as BetaContentBlockParam,\n type BetaContentBlockSource as BetaContentBlockSource,\n type BetaContentBlockSourceContent as BetaContentBlockSourceContent,\n type BetaImageBlockParam as BetaImageBlockParam,\n type BetaInputJSONDelta as BetaInputJSONDelta,\n type BetaMessage as BetaMessage,\n type BetaMessageDeltaUsage as BetaMessageDeltaUsage,\n type BetaMessageParam as BetaMessageParam,\n type BetaMessageTokensCount as BetaMessageTokensCount,\n type BetaMetadata as BetaMetadata,\n type BetaPlainTextSource as BetaPlainTextSource,\n type BetaRawContentBlockDeltaEvent as BetaRawContentBlockDeltaEvent,\n type BetaRawContentBlockStartEvent as BetaRawContentBlockStartEvent,\n type BetaRawContentBlockStopEvent as BetaRawContentBlockStopEvent,\n type BetaRawMessageDeltaEvent as BetaRawMessageDeltaEvent,\n type BetaRawMessageStartEvent as BetaRawMessageStartEvent,\n type BetaRawMessageStopEvent as BetaRawMessageStopEvent,\n type BetaRawMessageStreamEvent as BetaRawMessageStreamEvent,\n type BetaRedactedThinkingBlock as BetaRedactedThinkingBlock,\n type BetaRedactedThinkingBlockParam as BetaRedactedThinkingBlockParam,\n type BetaSignatureDelta as BetaSignatureDelta,\n type BetaTextBlock as BetaTextBlock,\n type BetaTextBlockParam as BetaTextBlockParam,\n type BetaTextCitation as BetaTextCitation,\n type BetaTextCitationParam as BetaTextCitationParam,\n type BetaTextDelta as BetaTextDelta,\n type BetaThinkingBlock as BetaThinkingBlock,\n type BetaThinkingBlockParam as BetaThinkingBlockParam,\n type BetaThinkingConfigDisabled as BetaThinkingConfigDisabled,\n type BetaThinkingConfigEnabled as BetaThinkingConfigEnabled,\n type BetaThinkingConfigParam as BetaThinkingConfigParam,\n type BetaThinkingDelta as BetaThinkingDelta,\n type BetaTool as BetaTool,\n type BetaToolBash20241022 as BetaToolBash20241022,\n type BetaToolBash20250124 as BetaToolBash20250124,\n type BetaToolChoice as BetaToolChoice,\n type BetaToolChoiceAny as BetaToolChoiceAny,\n type BetaToolChoiceAuto as BetaToolChoiceAuto,\n type BetaToolChoiceNone as BetaToolChoiceNone,\n type BetaToolChoiceTool as BetaToolChoiceTool,\n type BetaToolComputerUse20241022 as BetaToolComputerUse20241022,\n type BetaToolComputerUse20250124 as BetaToolComputerUse20250124,\n type BetaToolResultBlockParam as BetaToolResultBlockParam,\n type BetaToolTextEditor20241022 as BetaToolTextEditor20241022,\n type BetaToolTextEditor20250124 as BetaToolTextEditor20250124,\n type BetaToolUnion as BetaToolUnion,\n type BetaToolUseBlock as BetaToolUseBlock,\n type BetaToolUseBlockParam as BetaToolUseBlockParam,\n type BetaURLImageSource as BetaURLImageSource,\n type BetaURLPDFSource as BetaURLPDFSource,\n type BetaUsage as BetaUsage,\n type MessageCreateParams as MessageCreateParams,\n type MessageCreateParamsNonStreaming as MessageCreateParamsNonStreaming,\n type MessageCreateParamsStreaming as MessageCreateParamsStreaming,\n type MessageCountTokensParams as MessageCountTokensParams,\n };\n}\n", "// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\n\nimport { APIResource } from \"../resource.js\";\nimport { APIPromise } from \"../core.js\";\nimport * as Core from \"../core.js\";\nimport * as CompletionsAPI from \"./completions.js\";\nimport * as MessagesAPI from \"./messages/messages.js\";\nimport { Stream } from \"../streaming.js\";\n\nexport class Completions extends APIResource {\n /**\n * [Legacy] Create a Text Completion.\n *\n * The Text Completions API is a legacy API. We recommend using the\n * [Messages API](https://docs.anthropic.com/en/api/messages) going forward.\n *\n * Future models and features will not be compatible with Text Completions. See our\n * [migration guide](https://docs.anthropic.com/en/api/migrating-from-text-completions-to-messages)\n * for guidance in migrating from Text Completions to Messages.\n */\n create(body: CompletionCreateParamsNonStreaming, options?: Core.RequestOptions): APIPromise<Completion>;\n create(\n body: CompletionCreateParamsStreaming,\n options?: Core.RequestOptions,\n ): APIPromise<Stream<Completion>>;\n create(\n body: CompletionCreateParamsBase,\n options?: Core.RequestOptions,\n ): APIPromise<Stream<Completion> | Completion>;\n create(\n body: CompletionCreateParams,\n options?: Core.RequestOptions,\n ): APIPromise<Completion> | APIPromise<Stream<Completion>> {\n return this._client.post('/v1/complete', {\n body,\n timeout: (this._client as any)._options.timeout ?? 600000,\n ...options,\n stream: body.stream ?? false,\n }) as APIPromise<Completion> | APIPromise<Stream<Completion>>;\n }\n}\n\nexport interface Completion {\n /**\n * Unique object identifier.\n *\n * The format and length of IDs may change over time.\n */\n id: string;\n\n /**\n * The resulting completion up to and excluding the stop sequences.\n */\n completion: string;\n\n /**\n * The model that will complete your prompt.\\n\\nSee\n * [models](https://docs.anthropic.com/en/docs/models-overview) for additional\n * details and options.\n */\n model: MessagesAPI.Model;\n\n /**\n * The reason that we stopped.\n *\n * This may be one the following values:\n *\n * - `\"stop_sequence\"`: we reached a stop sequence \u2014 either provided by you via the\n * `stop_sequences` parameter, or a stop sequence built into the model\n * - `\"max_tokens\"`: we exceeded `max_tokens_to_sample` or the model's maximum\n */\n stop_reason: string | null;\n\n /**\n * Object type.\n *\n * For Text Completions, this is always `\"completion\"`.\n */\n type: 'completion';\n}\n\nexport type CompletionCreateParams = CompletionCreateParamsNonStreaming | CompletionCreateParamsStreaming;\n\nexport interface CompletionCreateParamsBase {\n /**\n * The maximum number of tokens to generate before stopping.\n *\n * Note that our models may stop _before_ reaching this maximum. This parameter\n * only specifies the absolute maximum number of tokens to generate.\n */\n max_tokens_to_sample: number;\n\n /**\n * The model that will complete your prompt.\\n\\nSee\n * [models](https://docs.anthropic.com/en/docs/models-overview) for additional\n * details and options.\n */\n model: MessagesAPI.Model;\n\n /**\n * The prompt that you want Claude to complete.\n *\n * For proper response generation you will need to format your prompt using\n * alternating `\\n\\nHuman:` and `\\n\\nAssistant:` conversational turns. For example:\n *\n * ```\n * \"\\n\\nHuman: {userQuestion}\\n\\nAssistant:\"\n * ```\n *\n * See [prompt validation](https://docs.anthropic.com/en/api/prompt-validation) and\n * our guide to\n * [prompt design](https://docs.anthropic.com/en/docs/intro-to-prompting) for more\n * details.\n */\n prompt: string;\n\n /**\n * An object describing metadata about the request.\n */\n metadata?: MessagesAPI.Metadata;\n\n /**\n * Sequences that will cause the model to stop generating.\n *\n * Our models stop on `\"\\n\\nHuman:\"`, and may include additional built-in stop\n * sequences in the future. By providing the stop_sequences parameter, you may\n * include additional strings that will cause the model to stop generating.\n */\n stop_sequences?: Array<string>;\n\n /**\n * Whether to incrementally stream the response using server-sent events.\n *\n * See [streaming](https://docs.anthropic.com/en/api/streaming) for details.\n */\n stream?: boolean;\n\n /**\n * Amount of randomness injected into the response.\n *\n * Defaults to `1.0`. Ranges from `0.0` to `1.0`. Use `temperature` closer to `0.0`\n * for analytical / multiple choice, and closer to `1.0` for creative and\n * generative tasks.\n *\n * Note that even with `temperature` of `0.0`, the results will not be fully\n * deterministic.\n */\n temperature?: number;\n\n /**\n * Only sample from the top K options for each subsequent token.\n *\n * Used to remove \"long tail\" low probability responses.\n * [Learn more technical details here](https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277).\n *\n * Recommended for advanced use cases only. You usually only need to use\n * `temperature`.\n */\n top_k?: number;\n\n /**\n * Use nucleus sampling.\n *\n * In nucleus sampling, we compute the cumulative distribution over all the options\n * for each subsequent token in decreasing probability order and cut it off once it\n * reaches a particular probability specified by `top_p`. You should either alter\n * `temperature` or `top_p`, but not both.\n *\n * Recommended for advanced use cases only. You usually only need to use\n * `temperature`.\n */\n top_p?: number;\n}\n\nexport namespace CompletionCreateParams {\n /**\n * @deprecated use `Anthropic.Messages.Metadata` instead\n */\n export type Metadata = MessagesAPI.Metadata;\n\n export type CompletionCreateParamsNonStreaming = CompletionsAPI.CompletionCreateParamsNonStreaming;\n export type CompletionCreateParamsStreaming = CompletionsAPI.CompletionCreateParamsStreaming;\n}\n\nexport interface CompletionCreateParamsNonStreaming extends CompletionCreateParamsBase {\n /**\n * Whether to incrementally stream the response using server-sent events.\n *\n * See [streaming](https://docs.anthropic.com/en/api/streaming) for details.\n */\n stream?: false;\n}\n\nexport interface CompletionCreateParamsStreaming extends CompletionCreateParamsBase {\n /**\n * Whether to incrementally stream the response using server-sent events.\n *\n * See [streaming](https://docs.anthropic.com/en/api/streaming) for details.\n */\n stream: true;\n}\n\nexport declare namespace Completions {\n export {\n type Completion as Completion,\n type CompletionCreateParams as CompletionCreateParams,\n type CompletionCreateParamsNonStreaming as CompletionCreateParamsNonStreaming,\n type CompletionCreateParamsStreaming as CompletionCreateParamsStreaming,\n };\n}\n", "// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\n\nimport { APIResource } from \"../../resource.js\";\nimport { isRequestOptions } from \"../../core.js\";\nimport * as Core from \"../../core.js\";\nimport * as Shared from \"../shared.js\";\nimport * as MessagesAPI from \"./messages.js\";\nimport { Page, type PageParams } from \"../../pagination.js\";\nimport { JSONLDecoder } from \"../../internal/decoders/jsonl.js\";\nimport { AnthropicError } from \"../../error.js\";\n\nexport class Batches extends APIResource {\n /**\n * Send a batch of Message creation requests.\n *\n * The Message Batches API can be used to process multiple Messages API requests at\n * once. Once a Message Batch is created, it begins processing immediately. Batches\n * can take up to 24 hours to complete.\n *\n * Learn more about the Message Batches API in our\n * [user guide](/en/docs/build-with-claude/batch-processing)\n */\n create(body: BatchCreateParams, options?: Core.RequestOptions): Core.APIPromise<MessageBatch> {\n return this._client.post('/v1/messages/batches', { body, ...options });\n }\n\n /**\n * This endpoint is idempotent and can be used to poll for Message Batch\n * completion. To access the results of a Message Batch, make a request to the\n * `results_url` field in the response.\n *\n * Learn more about the Message Batches API in our\n * [user guide](/en/docs/build-with-claude/batch-processing)\n */\n retrieve(messageBatchId: string, options?: Core.RequestOptions): Core.APIPromise<MessageBatch> {\n return this._client.get(`/v1/messages/batches/${messageBatchId}`, options);\n }\n\n /**\n * List all Message Batches within a Workspace. Most recently created batches are\n * returned first.\n *\n * Learn more about the Message Batches API in our\n * [user guide](/en/docs/build-with-claude/batch-processing)\n */\n list(\n query?: BatchListParams,\n options?: Core.RequestOptions,\n ): Core.PagePromise<MessageBatchesPage, MessageBatch>;\n list(options?: Core.RequestOptions): Core.PagePromise<MessageBatchesPage, MessageBatch>;\n list(\n query: BatchListParams | Core.RequestOptions = {},\n options?: Core.RequestOptions,\n ): Core.PagePromise<MessageBatchesPage, MessageBatch> {\n if (isRequestOptions(query)) {\n return this.list({}, query);\n }\n return this._client.getAPIList('/v1/messages/batches', MessageBatchesPage, { query, ...options });\n }\n\n /**\n * Delete a Message Batch.\n *\n * Message Batches can only be deleted once they've finished processing. If you'd\n * like to delete an in-progress batch, you must first cancel it.\n *\n * Learn more about the Message Batches API in our\n * [user guide](/en/docs/build-with-claude/batch-processing)\n */\n delete(messageBatchId: string, options?: Core.RequestOptions): Core.APIPromise<DeletedMessageBatch> {\n return this._client.delete(`/v1/messages/batches/${messageBatchId}`, options);\n }\n\n /**\n * Batches may be canceled any time before processing ends. Once cancellation is\n * initiated, the batch enters a `canceling` state, at which time the system may\n * complete any in-progress, non-interruptible requests before finalizing\n * cancellation.\n *\n * The number of canceled requests is specified in `request_counts`. To determine\n * which requests were canceled, check the individual results within the batch.\n * Note that cancellation may not result in any canceled requests if they were\n * non-interruptible.\n *\n * Learn more about the Message Batches API in our\n * [user guide](/en/docs/build-with-claude/batch-processing)\n */\n cancel(messageBatchId: string, options?: Core.RequestOptions): Core.APIPromise<MessageBatch> {\n return this._client.post(`/v1/messages/batches/${messageBatchId}/cancel`, options);\n }\n\n /**\n * Streams the results of a Message Batch as a `.jsonl` file.\n *\n * Each line in the file is a JSON object containing the result of a single request\n * in the Message Batch. Results are not guaranteed to be in the same order as\n * requests. Use the `custom_id` field to match results to requests.\n *\n * Learn more about the Message Batches API in our\n * [user guide](/en/docs/build-with-claude/batch-processing)\n */\n async results(\n messageBatchId: string,\n options?: Core.RequestOptions,\n ): Promise<JSONLDecoder<MessageBatchIndividualResponse>> {\n const batch = await this.retrieve(messageBatchId);\n if (!batch.results_url) {\n throw new AnthropicError(\n `No batch \\`results_url\\`; Has it finished processing? ${batch.processing_status} - ${batch.id}`,\n );\n }\n\n return this._client\n .get(batch.results_url, {\n ...options,\n headers: {\n Accept: 'application/binary',\n ...options?.headers,\n },\n __binaryResponse: true,\n })\n ._thenUnwrap((_, props) => JSONLDecoder.fromResponse(props.response, props.controller));\n }\n}\n\nexport class MessageBatchesPage extends Page<MessageBatch> {}\n\nexport interface DeletedMessageBatch {\n /**\n * ID of the Message Batch.\n */\n id: string;\n\n /**\n * Deleted object type.\n *\n * For Message Batches, this is always `\"message_batch_deleted\"`.\n */\n type: 'message_batch_deleted';\n}\n\nexport interface MessageBatch {\n /**\n * Unique object identifier.\n *\n * The format and length of IDs may change over time.\n */\n id: string;\n\n /**\n * RFC 3339 datetime string representing the time at which the Message Batch was\n * archived and its results became unavailable.\n */\n archived_at: string | null;\n\n /**\n * RFC 3339 datetime string representing the time at which cancellation was\n * initiated for the Message Batch. Specified only if cancellation was initiated.\n */\n cancel_initiated_at: string | null;\n\n /**\n * RFC 3339 datetime string representing the time at which the Message Batch was\n * created.\n */\n created_at: string;\n\n /**\n * RFC 3339 datetime string representing the time at which processing for the\n * Message Batch ended. Specified only once processing ends.\n *\n * Processing ends when every request in a Message Batch has either succeeded,\n * errored, canceled, or expired.\n */\n ended_at: string | null;\n\n /**\n * RFC 3339 datetime string representing the time at which the Message Batch will\n * expire and end processing, which is 24 hours after creation.\n */\n expires_at: string;\n\n /**\n * Processing status of the Message Batch.\n */\n processing_status: 'in_progress' | 'canceling' | 'ended';\n\n /**\n * Tallies requests within the Message Batch, categorized by their status.\n *\n * Requests start as `processing` and move to one of the other statuses only once\n * processing of the entire batch ends. The sum of all values always matches the\n * total number of requests in the batch.\n */\n request_counts: MessageBatchRequestCounts;\n\n /**\n * URL to a `.jsonl` file containing the results of the Message Batch requests.\n * Specified only once processing ends.\n *\n * Results in the file are not guaranteed to be in the same order as requests. Use\n * the `custom_id` field to match results to requests.\n */\n results_url: string | null;\n\n /**\n * Object type.\n *\n * For Message Batches, this is always `\"message_batch\"`.\n */\n type: 'message_batch';\n}\n\nexport interface MessageBatchCanceledResult {\n type: 'canceled';\n}\n\nexport interface MessageBatchErroredResult {\n error: Shared.ErrorResponse;\n\n type: 'errored';\n}\n\nexport interface MessageBatchExpiredResult {\n type: 'expired';\n}\n\n/**\n * This is a single line in the response `.jsonl` file and does not represent the\n * response as a whole.\n */\nexport interface MessageBatchIndividualResponse {\n /**\n * Developer-provided ID created for each request in a Message Batch. Useful for\n * matching results to requests, as results may be given out of request order.\n *\n * Must be unique for each request within the Message Batch.\n */\n custom_id: string;\n\n /**\n * Processing result for this request.\n *\n * Contains a Message output if processing was successful, an error response if\n * processing failed, or the reason why processing was not attempted, such as\n * cancellation or expiration.\n */\n result: MessageBatchResult;\n}\n\nexport interface MessageBatchRequestCounts {\n /**\n * Number of requests in the Message Batch that have been canceled.\n *\n * This is zero until processing of the entire Message Batch has ended.\n */\n canceled: number;\n\n /**\n * Number of requests in the Message Batch that encountered an error.\n *\n * This is zero until processing of the entire Message Batch has ended.\n */\n errored: number;\n\n /**\n * Number of requests in the Message Batch that have expired.\n *\n * This is zero until processing of the entire Message Batch has ended.\n */\n expired: number;\n\n /**\n * Number of requests in the Message Batch that are processing.\n */\n processing: number;\n\n /**\n * Number of requests in the Message Batch that have completed successfully.\n *\n * This is zero until processing of the entire Message Batch has ended.\n */\n succeeded: number;\n}\n\n/**\n * Processing result for this request.\n *\n * Contains a Message output if processing was successful, an error response if\n * processing failed, or the reason why processing was not attempted, such as\n * cancellation or expiration.\n */\nexport type MessageBatchResult =\n | MessageBatchSucceededResult\n | MessageBatchErroredResult\n | MessageBatchCanceledResult\n | MessageBatchExpiredResult;\n\nexport interface MessageBatchSucceededResult {\n message: MessagesAPI.Message;\n\n type: 'succeeded';\n}\n\nexport interface BatchCreateParams {\n /**\n * List of requests for prompt completion. Each is an individual request to create\n * a Message.\n */\n requests: Array<BatchCreateParams.Request>;\n}\n\nexport namespace BatchCreateParams {\n export interface Request {\n /**\n * Developer-provided ID created for each request in a Message Batch. Useful for\n * matching results to requests, as results may be given out of request order.\n *\n * Must be unique for each request within the Message Batch.\n */\n custom_id: string;\n\n /**\n * Messages API creation parameters for the individual request.\n *\n * See the [Messages API reference](/en/api/messages) for full documentation on\n * available parameters.\n */\n params: MessagesAPI.MessageCreateParamsNonStreaming;\n }\n}\n\nexport interface BatchListParams extends PageParams {}\n\nBatches.MessageBatchesPage = MessageBatchesPage;\n\nexport declare namespace Batches {\n export {\n type DeletedMessageBatch as DeletedMessageBatch,\n type MessageBatch as MessageBatch,\n type MessageBatchCanceledResult as MessageBatchCanceledResult,\n type MessageBatchErroredResult as MessageBatchErroredResult,\n type MessageBatchExpiredResult as MessageBatchExpiredResult,\n type MessageBatchIndividualResponse as MessageBatchIndividualResponse,\n type MessageBatchRequestCounts as MessageBatchRequestCounts,\n type MessageBatchResult as MessageBatchResult,\n type MessageBatchSucceededResult as MessageBatchSucceededResult,\n MessageBatchesPage as MessageBatchesPage,\n type BatchCreateParams as BatchCreateParams,\n type BatchListParams as BatchListParams,\n };\n}\n", "import * as Core from \"../core.js\";\nimport { AnthropicError, APIUserAbortError } from \"../error.js\";\nimport {\n type ContentBlock,\n Messages,\n type Message,\n type MessageStreamEvent,\n type MessageParam,\n type MessageCreateParams,\n type MessageCreateParamsBase,\n type TextBlock,\n type TextCitation,\n} from \"../resources/messages.js\";\nimport { type ReadableStream, type Response } from \"../_shims/index.js\";\nimport { Stream } from \"../streaming.js\";\nimport { partialParse } from \"../_vendor/partial-json-parser/parser.js\";\n\nexport interface MessageStreamEvents {\n connect: () => void;\n streamEvent: (event: MessageStreamEvent, snapshot: Message) => void;\n text: (textDelta: string, textSnapshot: string) => void;\n citation: (citation: TextCitation, citationsSnapshot: TextCitation[]) => void;\n inputJson: (partialJson: string, jsonSnapshot: unknown) => void;\n thinking: (thinkingDelta: string, thinkingSnapshot: string) => void;\n signature: (signature: string) => void;\n message: (message: Message) => void;\n contentBlock: (content: ContentBlock) => void;\n finalMessage: (message: Message) => void;\n error: (error: AnthropicError) => void;\n abort: (error: APIUserAbortError) => void;\n end: () => void;\n}\n\ntype MessageStreamEventListeners<Event extends keyof MessageStreamEvents> = {\n listener: MessageStreamEvents[Event];\n once?: boolean;\n}[];\n\nconst JSON_BUF_PROPERTY = '__json_buf';\n\nexport class MessageStream implements AsyncIterable<MessageStreamEvent> {\n messages: MessageParam[] = [];\n receivedMessages: Message[] = [];\n #currentMessageSnapshot: Message | undefined;\n\n controller: AbortController = new AbortController();\n\n #connectedPromise: Promise<Response | null>;\n #resolveConnectedPromise: (response: Response | null) => void = () => {};\n #rejectConnectedPromise: (error: AnthropicError) => void = () => {};\n\n #endPromise: Promise<void>;\n #resolveEndPromise: () => void = () => {};\n #rejectEndPromise: (error: AnthropicError) => void = () => {};\n\n #listeners: { [Event in keyof MessageStreamEvents]?: MessageStreamEventListeners<Event> } = {};\n\n #ended = false;\n #errored = false;\n #aborted = false;\n #catchingPromiseCreated = false;\n #response: Response | null | undefined;\n #request_id: string | null | undefined;\n\n constructor() {\n this.#connectedPromise = new Promise<Response | null>((resolve, reject) => {\n this.#resolveConnectedPromise = resolve;\n this.#rejectConnectedPromise = reject;\n });\n\n this.#endPromise = new Promise<void>((resolve, reject) => {\n this.#resolveEndPromise = resolve;\n this.#rejectEndPromise = reject;\n });\n\n // Don't let these promises cause unhandled rejection errors.\n // we will manually cause an unhandled rejection error later\n // if the user hasn't registered any error listener or called\n // any promise-returning method.\n this.#connectedPromise.catch(() => {});\n this.#endPromise.catch(() => {});\n }\n\n get response(): Response | null | undefined {\n return this.#response;\n }\n\n get request_id(): string | null | undefined {\n return this.#request_id;\n }\n\n /**\n * Returns the `MessageStream` data, the raw `Response` instance and the ID of the request,\n * returned vie the `request-id` header which is useful for debugging requests and resporting\n * issues to Anthropic.\n *\n * This is the same as the `APIPromise.withResponse()` method.\n *\n * This method will raise an error if you created the stream using `MessageStream.fromReadableStream`\n * as no `Response` is available.\n */\n async withResponse(): Promise<{\n data: MessageStream;\n response: Response;\n request_id: string | null | undefined;\n }> {\n const response = await this.#connectedPromise;\n if (!response) {\n throw new Error('Could not resolve a `Response` object');\n }\n\n return {\n data: this,\n response,\n request_id: response.headers.get('request-id'),\n };\n }\n\n /**\n * Intended for use on the frontend, consuming a stream produced with\n * `.toReadableStream()` on the backend.\n *\n * Note that messages sent to the model do not appear in `.on('message')`\n * in this context.\n */\n static fromReadableStream(stream: ReadableStream): MessageStream {\n const runner = new MessageStream();\n runner._run(() => runner._fromReadableStream(stream));\n return runner;\n }\n\n static createMessage(\n messages: Messages,\n params: MessageCreateParamsBase,\n options?: Core.RequestOptions,\n ): MessageStream {\n const runner = new MessageStream();\n for (const message of params.messages) {\n runner._addMessageParam(message);\n }\n runner._run(() =>\n runner._createMessage(\n messages,\n { ...params, stream: true },\n { ...options, headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'stream' } },\n ),\n );\n return runner;\n }\n\n protected _run(executor: () => Promise<any>) {\n executor().then(() => {\n this._emitFinal();\n this._emit('end');\n }, this.#handleError);\n }\n\n protected _addMessageParam(message: MessageParam) {\n this.messages.push(message);\n }\n\n protected _addMessage(message: Message, emit = true) {\n this.receivedMessages.push(message);\n if (emit) {\n this._emit('message', message);\n }\n }\n\n protected async _createMessage(\n messages: Messages,\n params: MessageCreateParams,\n options?: Core.RequestOptions,\n ): Promise<void> {\n const signal = options?.signal;\n if (signal) {\n if (signal.aborted) this.controller.abort();\n signal.addEventListener('abort', () => this.controller.abort());\n }\n this.#beginRequest();\n const { response, data: stream } = await messages\n .create({ ...params, stream: true }, { ...options, signal: this.controller.signal })\n .withResponse();\n this._connected(response);\n for await (const event of stream) {\n this.#addStreamEvent(event);\n }\n if (stream.controller.signal?.aborted) {\n throw new APIUserAbortError();\n }\n this.#endRequest();\n }\n\n protected _connected(response: Response | null) {\n if (this.ended) return;\n this.#response = response;\n this.#request_id = response?.headers.get('request-id');\n this.#resolveConnectedPromise(response);\n this._emit('connect');\n }\n\n get ended(): boolean {\n return this.#ended;\n }\n\n get errored(): boolean {\n return this.#errored;\n }\n\n get aborted(): boolean {\n return this.#aborted;\n }\n\n abort() {\n this.controller.abort();\n }\n\n /**\n * Adds the listener function to the end of the listeners array for the event.\n * No checks are made to see if the listener has already been added. Multiple calls passing\n * the same combination of event and listener will result in the listener being added, and\n * called, multiple times.\n * @returns this MessageStream, so that calls can be chained\n */\n on<Event extends keyof MessageStreamEvents>(event: Event, listener: MessageStreamEvents[Event]): this {\n const listeners: MessageStreamEventListeners<Event> =\n this.#listeners[event] || (this.#listeners[event] = []);\n listeners.push({ listener });\n return this;\n }\n\n /**\n * Removes the specified listener from the listener array for the event.\n * off() will remove, at most, one instance of a listener from the listener array. If any single\n * listener has been added multiple times to the listener array for the specified event, then\n * off() must be called multiple times to remove each instance.\n * @returns this MessageStream, so that calls can be chained\n */\n off<Event extends keyof MessageStreamEvents>(event: Event, listener: MessageStreamEvents[Event]): this {\n const listeners = this.#listeners[event];\n if (!listeners) return this;\n const index = listeners.findIndex((l) => l.listener === listener);\n if (index >= 0) listeners.splice(index, 1);\n return this;\n }\n\n /**\n * Adds a one-time listener function for the event. The next time the event is triggered,\n * this listener is removed and then invoked.\n * @returns this MessageStream, so that calls can be chained\n */\n once<Event extends keyof MessageStreamEvents>(event: Event, listener: MessageStreamEvents[Event]): this {\n const listeners: MessageStreamEventListeners<Event> =\n this.#listeners[event] || (this.#listeners[event] = []);\n listeners.push({ listener, once: true });\n return this;\n }\n\n /**\n * This is similar to `.once()`, but returns a Promise that resolves the next time\n * the event is triggered, instead of calling a listener callback.\n * @returns a Promise that resolves the next time given event is triggered,\n * or rejects if an error is emitted. (If you request the 'error' event,\n * returns a promise that resolves with the error).\n *\n * Example:\n *\n * const message = await stream.emitted('message') // rejects if the stream errors\n */\n emitted<Event extends keyof MessageStreamEvents>(\n event: Event,\n ): Promise<\n Parameters<MessageStreamEvents[Event]> extends [infer Param] ? Param\n : Parameters<MessageStreamEvents[Event]> extends [] ? void\n : Parameters<MessageStreamEvents[Event]>\n > {\n return new Promise((resolve, reject) => {\n this.#catchingPromiseCreated = true;\n if (event !== 'error') this.once('error', reject);\n this.once(event, resolve as any);\n });\n }\n\n async done(): Promise<void> {\n this.#catchingPromiseCreated = true;\n await this.#endPromise;\n }\n\n get currentMessage(): Message | undefined {\n return this.#currentMessageSnapshot;\n }\n\n #getFinalMessage(): Message {\n if (this.receivedMessages.length === 0) {\n throw new AnthropicError('stream ended without producing a Message with role=assistant');\n }\n return this.receivedMessages.at(-1)!;\n }\n\n /**\n * @returns a promise that resolves with the the final assistant Message response,\n * or rejects if an error occurred or the stream ended prematurely without producing a Message.\n */\n async finalMessage(): Promise<Message> {\n await this.done();\n return this.#getFinalMessage();\n }\n\n #getFinalText(): string {\n if (this.receivedMessages.length === 0) {\n throw new AnthropicError('stream ended without producing a Message with role=assistant');\n }\n const textBlocks = this.receivedMessages\n .at(-1)!\n .content.filter((block): block is TextBlock => block.type === 'text')\n .map((block) => block.text);\n if (textBlocks.length === 0) {\n throw new AnthropicError('stream ended without producing a content block with type=text');\n }\n return textBlocks.join(' ');\n }\n\n /**\n * @returns a promise that resolves with the the final assistant Message's text response, concatenated\n * together if there are more than one text blocks.\n * Rejects if an error occurred or the stream ended prematurely without producing a Message.\n */\n async finalText(): Promise<string> {\n await this.done();\n return this.#getFinalText();\n }\n\n #handleError = (error: unknown) => {\n this.#errored = true;\n if (error instanceof Error && error.name === 'AbortError') {\n error = new APIUserAbortError();\n }\n if (error instanceof APIUserAbortError) {\n this.#aborted = true;\n return this._emit('abort', error);\n }\n if (error instanceof AnthropicError) {\n return this._emit('error', error);\n }\n if (error instanceof Error) {\n const anthropicError: AnthropicError = new AnthropicError(error.message);\n // @ts-ignore\n anthropicError.cause = error;\n return this._emit('error', anthropicError);\n }\n return this._emit('error', new AnthropicError(String(error)));\n };\n\n protected _emit<Event extends keyof MessageStreamEvents>(\n event: Event,\n ...args: Parameters<MessageStreamEvents[Event]>\n ) {\n // make sure we don't emit any MessageStreamEvents after end\n if (this.#ended) return;\n\n if (event === 'end') {\n this.#ended = true;\n this.#resolveEndPromise();\n }\n\n const listeners: MessageStreamEventListeners<Event> | undefined = this.#listeners[event];\n if (listeners) {\n this.#listeners[event] = listeners.filter((l) => !l.once) as any;\n listeners.forEach(({ listener }: any) => listener(...args));\n }\n\n if (event === 'abort') {\n const error = args[0] as APIUserAbortError;\n if (!this.#catchingPromiseCreated && !listeners?.length) {\n Promise.reject(error);\n }\n this.#rejectConnectedPromise(error);\n this.#rejectEndPromise(error);\n this._emit('end');\n return;\n }\n\n if (event === 'error') {\n // NOTE: _emit('error', error) should only be called from #handleError().\n\n const error = args[0] as AnthropicError;\n if (!this.#catchingPromiseCreated && !listeners?.length) {\n // Trigger an unhandled rejection if the user hasn't registered any error handlers.\n // If you are seeing stack traces here, make sure to handle errors via either:\n // - runner.on('error', () => ...)\n // - await runner.done()\n // - await runner.final...()\n // - etc.\n Promise.reject(error);\n }\n this.#rejectConnectedPromise(error);\n this.#rejectEndPromise(error);\n this._emit('end');\n }\n }\n\n protected _emitFinal() {\n const finalMessage = this.receivedMessages.at(-1);\n if (finalMessage) {\n this._emit('finalMessage', this.#getFinalMessage());\n }\n }\n\n #beginRequest() {\n if (this.ended) return;\n this.#currentMessageSnapshot = undefined;\n }\n #addStreamEvent(event: MessageStreamEvent) {\n if (this.ended) return;\n const messageSnapshot = this.#accumulateMessage(event);\n this._emit('streamEvent', event, messageSnapshot);\n\n switch (event.type) {\n case 'content_block_delta': {\n const content = messageSnapshot.content.at(-1)!;\n switch (event.delta.type) {\n case 'text_delta': {\n if (content.type === 'text') {\n this._emit('text', event.delta.text, content.text || '');\n }\n break;\n }\n case 'citations_delta': {\n if (content.type === 'text') {\n this._emit('citation', event.delta.citation, content.citations ?? []);\n }\n break;\n }\n case 'input_json_delta': {\n if (content.type === 'tool_use' && content.input) {\n this._emit('inputJson', event.delta.partial_json, content.input);\n }\n break;\n }\n case 'thinking_delta': {\n if (content.type === 'thinking') {\n this._emit('thinking', event.delta.thinking, content.thinking);\n }\n break;\n }\n case 'signature_delta': {\n if (content.type === 'thinking') {\n this._emit('signature', content.signature);\n }\n break;\n }\n default:\n checkNever(event.delta);\n }\n break;\n }\n case 'message_stop': {\n this._addMessageParam(messageSnapshot);\n this._addMessage(messageSnapshot, true);\n break;\n }\n case 'content_block_stop': {\n this._emit('contentBlock', messageSnapshot.content.at(-1)!);\n break;\n }\n case 'message_start': {\n this.#currentMessageSnapshot = messageSnapshot;\n break;\n }\n case 'content_block_start':\n case 'message_delta':\n break;\n }\n }\n #endRequest(): Message {\n if (this.ended) {\n throw new AnthropicError(`stream has ended, this shouldn't happen`);\n }\n const snapshot = this.#currentMessageSnapshot;\n if (!snapshot) {\n throw new AnthropicError(`request ended without sending any chunks`);\n }\n this.#currentMessageSnapshot = undefined;\n return snapshot;\n }\n\n protected async _fromReadableStream(\n readableStream: ReadableStream,\n options?: Core.RequestOptions,\n ): Promise<void> {\n const signal = options?.signal;\n if (signal) {\n if (signal.aborted) this.controller.abort();\n signal.addEventListener('abort', () => this.controller.abort());\n }\n this.#beginRequest();\n this._connected(null);\n const stream = Stream.fromReadableStream<MessageStreamEvent>(readableStream, this.controller);\n for await (const event of stream) {\n this.#addStreamEvent(event);\n }\n if (stream.controller.signal?.aborted) {\n throw new APIUserAbortError();\n }\n this.#endRequest();\n }\n\n /**\n * Mutates this.#currentMessage with the current event. Handling the accumulation of multiple messages\n * will be needed to be handled by the caller, this method will throw if you try to accumulate for multiple\n * messages.\n */\n #accumulateMessage(event: MessageStreamEvent): Message {\n let snapshot = this.#currentMessageSnapshot;\n\n if (event.type === 'message_start') {\n if (snapshot) {\n throw new AnthropicError(`Unexpected event order, got ${event.type} before receiving \"message_stop\"`);\n }\n return event.message;\n }\n\n if (!snapshot) {\n throw new AnthropicError(`Unexpected event order, got ${event.type} before \"message_start\"`);\n }\n\n switch (event.type) {\n case 'message_stop':\n return snapshot;\n case 'message_delta':\n snapshot.stop_reason = event.delta.stop_reason;\n snapshot.stop_sequence = event.delta.stop_sequence;\n snapshot.usage.output_tokens = event.usage.output_tokens;\n return snapshot;\n case 'content_block_start':\n snapshot.content.push(event.content_block);\n return snapshot;\n case 'content_block_delta': {\n const snapshotContent = snapshot.content.at(event.index);\n\n switch (event.delta.type) {\n case 'text_delta': {\n if (snapshotContent?.type === 'text') {\n snapshotContent.text += event.delta.text;\n }\n break;\n }\n case 'citations_delta': {\n if (snapshotContent?.type === 'text') {\n snapshotContent.citations ??= [];\n snapshotContent.citations.push(event.delta.citation);\n }\n break;\n }\n case 'input_json_delta': {\n if (snapshotContent?.type === 'tool_use') {\n // we need to keep track of the raw JSON string as well so that we can\n // re-parse it for each delta, for now we just store it as an untyped\n // non-enumerable property on the snapshot\n let jsonBuf = (snapshotContent as any)[JSON_BUF_PROPERTY] || '';\n jsonBuf += event.delta.partial_json;\n\n Object.defineProperty(snapshotContent, JSON_BUF_PROPERTY, {\n value: jsonBuf,\n enumerable: false,\n writable: true,\n });\n\n if (jsonBuf) {\n snapshotContent.input = partialParse(jsonBuf);\n }\n }\n break;\n }\n case 'thinking_delta': {\n if (snapshotContent?.type === 'thinking') {\n snapshotContent.thinking += event.delta.thinking;\n }\n break;\n }\n case 'signature_delta': {\n if (snapshotContent?.type === 'thinking') {\n snapshotContent.signature = event.delta.signature;\n }\n break;\n }\n default:\n checkNever(event.delta);\n }\n\n return snapshot;\n }\n case 'content_block_stop':\n return snapshot;\n }\n }\n\n [Symbol.asyncIterator](): AsyncIterator<MessageStreamEvent> {\n const pushQueue: MessageStreamEvent[] = [];\n const readQueue: {\n resolve: (chunk: MessageStreamEvent | undefined) => void;\n reject: (error: unknown) => void;\n }[] = [];\n let done = false;\n\n this.on('streamEvent', (event) => {\n const reader = readQueue.shift();\n if (reader) {\n reader.resolve(event);\n } else {\n pushQueue.push(event);\n }\n });\n\n this.on('end', () => {\n done = true;\n for (const reader of readQueue) {\n reader.resolve(undefined);\n }\n readQueue.length = 0;\n });\n\n this.on('abort', (err) => {\n done = true;\n for (const reader of readQueue) {\n reader.reject(err);\n }\n readQueue.length = 0;\n });\n\n this.on('error', (err) => {\n done = true;\n for (const reader of readQueue) {\n reader.reject(err);\n }\n readQueue.length = 0;\n });\n\n return {\n next: async (): Promise<IteratorResult<MessageStreamEvent>> => {\n if (!pushQueue.length) {\n if (done) {\n return { value: undefined, done: true };\n }\n return new Promise<MessageStreamEvent | undefined>((resolve, reject) =>\n readQueue.push({ resolve, reject }),\n ).then((chunk) => (chunk ? { value: chunk, done: false } : { value: undefined, done: true }));\n }\n const chunk = pushQueue.shift()!;\n return { value: chunk, done: false };\n },\n return: async () => {\n this.abort();\n return { value: undefined, done: true };\n },\n };\n }\n\n toReadableStream(): ReadableStream {\n const stream = new Stream(this[Symbol.asyncIterator].bind(this), this.controller);\n return stream.toReadableStream();\n }\n}\n\n// used to ensure exhaustive case matching without throwing a runtime error\nfunction checkNever(x: never) {}\n", "// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\n\nimport { APIResource } from \"../../resource.js\";\nimport { APIPromise } from \"../../core.js\";\nimport * as Core from \"../../core.js\";\nimport * as MessagesAPI from \"./messages.js\";\nimport * as BatchesAPI from \"./batches.js\";\nimport {\n BatchCreateParams,\n BatchListParams,\n Batches,\n DeletedMessageBatch,\n MessageBatch,\n MessageBatchCanceledResult,\n MessageBatchErroredResult,\n MessageBatchExpiredResult,\n MessageBatchIndividualResponse,\n MessageBatchRequestCounts,\n MessageBatchResult,\n MessageBatchSucceededResult,\n MessageBatchesPage,\n} from \"./batches.js\";\nimport { Stream } from \"../../streaming.js\";\nimport { MessageStream } from \"../../lib/MessageStream.js\";\n\nexport { MessageStream } from \"../../lib/MessageStream.js\";\n\nexport class Messages extends APIResource {\n batches: BatchesAPI.Batches = new BatchesAPI.Batches(this._client);\n\n /**\n * Send a structured list of input messages with text and/or image content, and the\n * model will generate the next message in the conversation.\n *\n * The Messages API can be used for either single queries or stateless multi-turn\n * conversations.\n *\n * Learn more about the Messages API in our [user guide](/en/docs/initial-setup)\n */\n create(body: MessageCreateParamsNonStreaming, options?: Core.RequestOptions): APIPromise<Message>;\n create(\n body: MessageCreateParamsStreaming,\n options?: Core.RequestOptions,\n ): APIPromise<Stream<RawMessageStreamEvent>>;\n create(\n body: MessageCreateParamsBase,\n options?: Core.RequestOptions,\n ): APIPromise<Stream<RawMessageStreamEvent> | Message>;\n create(\n body: MessageCreateParams,\n options?: Core.RequestOptions,\n ): APIPromise<Message> | APIPromise<Stream<RawMessageStreamEvent>> {\n if (body.model in DEPRECATED_MODELS) {\n console.warn(\n `The model '${body.model}' is deprecated and will reach end-of-life on ${\n DEPRECATED_MODELS[body.model]\n }\\nPlease migrate to a newer model. Visit https://docs.anthropic.com/en/docs/resources/model-deprecations for more information.`,\n );\n }\n return this._client.post('/v1/messages', {\n body,\n timeout:\n (this._client as any)._options.timeout ??\n (body.stream ? 600000 : this._client._calculateNonstreamingTimeout(body.max_tokens)),\n ...options,\n stream: body.stream ?? false,\n }) as APIPromise<Message> | APIPromise<Stream<RawMessageStreamEvent>>;\n }\n\n /**\n * Create a Message stream\n */\n stream(body: MessageStreamParams, options?: Core.RequestOptions): MessageStream {\n return MessageStream.createMessage(this, body, options);\n }\n\n /**\n * Count the number of tokens in a Message.\n *\n * The Token Count API can be used to count the number of tokens in a Message,\n * including tools, images, and documents, without creating it.\n *\n * Learn more about token counting in our\n * [user guide](/en/docs/build-with-claude/token-counting)\n */\n countTokens(\n body: MessageCountTokensParams,\n options?: Core.RequestOptions,\n ): Core.APIPromise<MessageTokensCount> {\n return this._client.post('/v1/messages/count_tokens', { body, ...options });\n }\n}\n\nexport interface Base64ImageSource {\n data: string;\n\n media_type: 'image/jpeg' | 'image/png' | 'image/gif' | 'image/webp';\n\n type: 'base64';\n}\n\nexport interface Base64PDFSource {\n data: string;\n\n media_type: 'application/pdf';\n\n type: 'base64';\n}\n\nexport interface CacheControlEphemeral {\n type: 'ephemeral';\n}\n\nexport interface CitationCharLocation {\n cited_text: string;\n\n document_index: number;\n\n document_title: string | null;\n\n end_char_index: number;\n\n start_char_index: number;\n\n type: 'char_location';\n}\n\nexport interface CitationCharLocationParam {\n cited_text: string;\n\n document_index: number;\n\n document_title: string | null;\n\n end_char_index: number;\n\n start_char_index: number;\n\n type: 'char_location';\n}\n\nexport interface CitationContentBlockLocation {\n cited_text: string;\n\n document_index: number;\n\n document_title: string | null;\n\n end_block_index: number;\n\n start_block_index: number;\n\n type: 'content_block_location';\n}\n\nexport interface CitationContentBlockLocationParam {\n cited_text: string;\n\n document_index: number;\n\n document_title: string | null;\n\n end_block_index: number;\n\n start_block_index: number;\n\n type: 'content_block_location';\n}\n\nexport interface CitationPageLocation {\n cited_text: string;\n\n document_index: number;\n\n document_title: string | null;\n\n end_page_number: number;\n\n start_page_number: number;\n\n type: 'page_location';\n}\n\nexport interface CitationPageLocationParam {\n cited_text: string;\n\n document_index: number;\n\n document_title: string | null;\n\n end_page_number: number;\n\n start_page_number: number;\n\n type: 'page_location';\n}\n\nexport interface CitationsConfigParam {\n enabled?: boolean;\n}\n\nexport interface CitationsDelta {\n citation: CitationCharLocation | CitationPageLocation | CitationContentBlockLocation;\n\n type: 'citations_delta';\n}\n\nexport type ContentBlock = TextBlock | ToolUseBlock | ThinkingBlock | RedactedThinkingBlock;\n\nexport type ContentBlockDeltaEvent = RawContentBlockDeltaEvent;\n\nexport type ContentBlockParam =\n | TextBlockParam\n | ImageBlockParam\n | ToolUseBlockParam\n | ToolResultBlockParam\n | DocumentBlockParam\n | ThinkingBlockParam\n | RedactedThinkingBlockParam;\n\nexport interface ContentBlockSource {\n content: string | Array<ContentBlockSourceContent>;\n\n type: 'content';\n}\n\nexport type ContentBlockSourceContent = TextBlockParam | ImageBlockParam;\n\nexport type ContentBlockStartEvent = RawContentBlockStartEvent;\n\nexport type ContentBlockStopEvent = RawContentBlockStopEvent;\n\nexport interface DocumentBlockParam {\n source: Base64PDFSource | PlainTextSource | ContentBlockSource | URLPDFSource;\n\n type: 'document';\n\n cache_control?: CacheControlEphemeral | null;\n\n citations?: CitationsConfigParam;\n\n context?: string | null;\n\n title?: string | null;\n}\n\nexport interface ImageBlockParam {\n source: Base64ImageSource | URLImageSource;\n\n type: 'image';\n\n cache_control?: CacheControlEphemeral | null;\n}\n\nexport type InputJsonDelta = InputJSONDelta;\n\nexport interface InputJSONDelta {\n partial_json: string;\n\n type: 'input_json_delta';\n}\n\nexport interface Message {\n /**\n * Unique object identifier.\n *\n * The format and length of IDs may change over time.\n */\n id: string;\n\n /**\n * Content generated by the model.\n *\n * This is an array of content blocks, each of which has a `type` that determines\n * its shape.\n *\n * Example:\n *\n * ```json\n * [{ \"type\": \"text\", \"text\": \"Hi, I'm Claude.\" }]\n * ```\n *\n * If the request input `messages` ended with an `assistant` turn, then the\n * response `content` will continue directly from that last turn. You can use this\n * to constrain the model's output.\n *\n * For example, if the input `messages` were:\n *\n * ```json\n * [\n * {\n * \"role\": \"user\",\n * \"content\": \"What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun\"\n * },\n * { \"role\": \"assistant\", \"content\": \"The best answer is (\" }\n * ]\n * ```\n *\n * Then the response `content` might be:\n *\n * ```json\n * [{ \"type\": \"text\", \"text\": \"B)\" }]\n * ```\n */\n content: Array<ContentBlock>;\n\n /**\n * The model that will complete your prompt.\\n\\nSee\n * [models](https://docs.anthropic.com/en/docs/models-overview) for additional\n * details and options.\n */\n model: Model;\n\n /**\n * Conversational role of the generated message.\n *\n * This will always be `\"assistant\"`.\n */\n role: 'assistant';\n\n /**\n * The reason that we stopped.\n *\n * This may be one the following values:\n *\n * - `\"end_turn\"`: the model reached a natural stopping point\n * - `\"max_tokens\"`: we exceeded the requested `max_tokens` or the model's maximum\n * - `\"stop_sequence\"`: one of your provided custom `stop_sequences` was generated\n * - `\"tool_use\"`: the model invoked one or more tools\n *\n * In non-streaming mode this value is always non-null. In streaming mode, it is\n * null in the `message_start` event and non-null otherwise.\n */\n stop_reason: 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use' | null;\n\n /**\n * Which custom stop sequence was generated, if any.\n *\n * This value will be a non-null string if one of your custom stop sequences was\n * generated.\n */\n stop_sequence: string | null;\n\n /**\n * Object type.\n *\n * For Messages, this is always `\"message\"`.\n */\n type: 'message';\n\n /**\n * Billing and rate-limit usage.\n *\n * Anthropic's API bills and rate-limits by token counts, as tokens represent the\n * underlying cost to our systems.\n *\n * Under the hood, the API transforms requests into a format suitable for the\n * model. The model's output then goes through a parsing stage before becoming an\n * API response. As a result, the token counts in `usage` will not match one-to-one\n * with the exact visible content of an API request or response.\n *\n * For example, `output_tokens` will be non-zero, even for an empty string response\n * from Claude.\n *\n * Total input tokens in a request is the summation of `input_tokens`,\n * `cache_creation_input_tokens`, and `cache_read_input_tokens`.\n */\n usage: Usage;\n}\n\nexport type MessageCountTokensTool = Tool | ToolBash20250124 | ToolTextEditor20250124;\n\nexport type MessageDeltaEvent = RawMessageDeltaEvent;\n\nexport interface MessageDeltaUsage {\n /**\n * The cumulative number of output tokens which were used.\n */\n output_tokens: number;\n}\n\nexport interface MessageParam {\n content: string | Array<ContentBlockParam>;\n\n role: 'user' | 'assistant';\n}\n\nexport type MessageStartEvent = RawMessageStartEvent;\n\nexport type MessageStopEvent = RawMessageStopEvent;\n\nexport type MessageStreamEvent = RawMessageStreamEvent;\n\nexport interface MessageTokensCount {\n /**\n * The total number of tokens across the provided list of messages, system prompt,\n * and tools.\n */\n input_tokens: number;\n}\n\nexport interface Metadata {\n /**\n * An external identifier for the user who is associated with the request.\n *\n * This should be a uuid, hash value, or other opaque identifier. Anthropic may use\n * this id to help detect abuse. Do not include any identifying information such as\n * name, email address, or phone number.\n */\n user_id?: string | null;\n}\n\n/**\n * The model that will complete your prompt.\\n\\nSee\n * [models](https://docs.anthropic.com/en/docs/models-overview) for additional\n * details and options.\n */\nexport type Model =\n | 'claude-3-7-sonnet-latest'\n | 'claude-3-7-sonnet-20250219'\n | 'claude-3-5-haiku-latest'\n | 'claude-3-5-haiku-20241022'\n | 'claude-3-5-sonnet-latest'\n | 'claude-3-5-sonnet-20241022'\n | 'claude-3-5-sonnet-20240620'\n | 'claude-3-opus-latest'\n | 'claude-3-opus-20240229'\n | 'claude-3-sonnet-20240229'\n | 'claude-3-haiku-20240307'\n | 'claude-2.1'\n | 'claude-2.0'\n | (string & {});\n\nconst DEPRECATED_MODELS: {\n [K in Model]?: string;\n} = {\n 'claude-1.3': 'November 6th, 2024',\n 'claude-1.3-100k': 'November 6th, 2024',\n 'claude-instant-1.1': 'November 6th, 2024',\n 'claude-instant-1.1-100k': 'November 6th, 2024',\n 'claude-instant-1.2': 'November 6th, 2024',\n 'claude-3-sonnet-20240229': 'July 21st, 2025',\n 'claude-2.1': 'July 21st, 2025',\n 'claude-2.0': 'July 21st, 2025',\n};\n\nexport interface PlainTextSource {\n data: string;\n\n media_type: 'text/plain';\n\n type: 'text';\n}\n\nexport interface RawContentBlockDeltaEvent {\n delta: TextDelta | InputJSONDelta | CitationsDelta | ThinkingDelta | SignatureDelta;\n\n index: number;\n\n type: 'content_block_delta';\n}\n\nexport interface RawContentBlockStartEvent {\n content_block: TextBlock | ToolUseBlock | ThinkingBlock | RedactedThinkingBlock;\n\n index: number;\n\n type: 'content_block_start';\n}\n\nexport interface RawContentBlockStopEvent {\n index: number;\n\n type: 'content_block_stop';\n}\n\nexport interface RawMessageDeltaEvent {\n delta: RawMessageDeltaEvent.Delta;\n\n type: 'message_delta';\n\n /**\n * Billing and rate-limit usage.\n *\n * Anthropic's API bills and rate-limits by token counts, as tokens represent the\n * underlying cost to our systems.\n *\n * Under the hood, the API transforms requests into a format suitable for the\n * model. The model's output then goes through a parsing stage before becoming an\n * API response. As a result, the token counts in `usage` will not match one-to-one\n * with the exact visible content of an API request or response.\n *\n * For example, `output_tokens` will be non-zero, even for an empty string response\n * from Claude.\n *\n * Total input tokens in a request is the summation of `input_tokens`,\n * `cache_creation_input_tokens`, and `cache_read_input_tokens`.\n */\n usage: MessageDeltaUsage;\n}\n\nexport namespace RawMessageDeltaEvent {\n export interface Delta {\n stop_reason: 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use' | null;\n\n stop_sequence: string | null;\n }\n}\n\nexport interface RawMessageStartEvent {\n message: Message;\n\n type: 'message_start';\n}\n\nexport interface RawMessageStopEvent {\n type: 'message_stop';\n}\n\nexport type RawMessageStreamEvent =\n | RawMessageStartEvent\n | RawMessageDeltaEvent\n | RawMessageStopEvent\n | RawContentBlockStartEvent\n | RawContentBlockDeltaEvent\n | RawContentBlockStopEvent;\n\nexport interface RedactedThinkingBlock {\n data: string;\n\n type: 'redacted_thinking';\n}\n\nexport interface RedactedThinkingBlockParam {\n data: string;\n\n type: 'redacted_thinking';\n}\n\nexport interface SignatureDelta {\n signature: string;\n\n type: 'signature_delta';\n}\n\nexport interface TextBlock {\n /**\n * Citations supporting the text block.\n *\n * The type of citation returned will depend on the type of document being cited.\n * Citing a PDF results in `page_location`, plain text results in `char_location`,\n * and content document results in `content_block_location`.\n */\n citations: Array<TextCitation> | null;\n\n text: string;\n\n type: 'text';\n}\n\nexport interface TextBlockParam {\n text: string;\n\n type: 'text';\n\n cache_control?: CacheControlEphemeral | null;\n\n citations?: Array<TextCitationParam> | null;\n}\n\nexport type TextCitation = CitationCharLocation | CitationPageLocation | CitationContentBlockLocation;\n\nexport type TextCitationParam =\n | CitationCharLocationParam\n | CitationPageLocationParam\n | CitationContentBlockLocationParam;\n\nexport interface TextDelta {\n text: string;\n\n type: 'text_delta';\n}\n\nexport interface ThinkingBlock {\n signature: string;\n\n thinking: string;\n\n type: 'thinking';\n}\n\nexport interface ThinkingBlockParam {\n signature: string;\n\n thinking: string;\n\n type: 'thinking';\n}\n\nexport interface ThinkingConfigDisabled {\n type: 'disabled';\n}\n\nexport interface ThinkingConfigEnabled {\n /**\n * Determines how many tokens Claude can use for its internal reasoning process.\n * Larger budgets can enable more thorough analysis for complex problems, improving\n * response quality.\n *\n * Must be \u22651024 and less than `max_tokens`.\n *\n * See\n * [extended thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking)\n * for details.\n */\n budget_tokens: number;\n\n type: 'enabled';\n}\n\n/**\n * Configuration for enabling Claude's extended thinking.\n *\n * When enabled, responses include `thinking` content blocks showing Claude's\n * thinking process before the final answer. Requires a minimum budget of 1,024\n * tokens and counts towards your `max_tokens` limit.\n *\n * See\n * [extended thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking)\n * for details.\n */\nexport type ThinkingConfigParam = ThinkingConfigEnabled | ThinkingConfigDisabled;\n\nexport interface ThinkingDelta {\n thinking: string;\n\n type: 'thinking_delta';\n}\n\nexport interface Tool {\n /**\n * [JSON schema](https://json-schema.org/draft/2020-12) for this tool's input.\n *\n * This defines the shape of the `input` that your tool accepts and that the model\n * will produce.\n */\n input_schema: Tool.InputSchema;\n\n /**\n * Name of the tool.\n *\n * This is how the tool will be called by the model and in tool_use blocks.\n */\n name: string;\n\n cache_control?: CacheControlEphemeral | null;\n\n /**\n * Description of what this tool does.\n *\n * Tool descriptions should be as detailed as possible. The more information that\n * the model has about what the tool is and how to use it, the better it will\n * perform. You can use natural language descriptions to reinforce important\n * aspects of the tool input JSON schema.\n */\n description?: string;\n}\n\nexport namespace Tool {\n /**\n * [JSON schema](https://json-schema.org/draft/2020-12) for this tool's input.\n *\n * This defines the shape of the `input` that your tool accepts and that the model\n * will produce.\n */\n export interface InputSchema {\n type: 'object';\n\n properties?: unknown | null;\n [k: string]: unknown;\n }\n}\n\nexport interface ToolBash20250124 {\n /**\n * Name of the tool.\n *\n * This is how the tool will be called by the model and in tool_use blocks.\n */\n name: 'bash';\n\n type: 'bash_20250124';\n\n cache_control?: CacheControlEphemeral | null;\n}\n\n/**\n * How the model should use the provided tools. The model can use a specific tool,\n * any available tool, decide by itself, or not use tools at all.\n */\nexport type ToolChoice = ToolChoiceAuto | ToolChoiceAny | ToolChoiceTool | ToolChoiceNone;\n\n/**\n * The model will use any available tools.\n */\nexport interface ToolChoiceAny {\n type: 'any';\n\n /**\n * Whether to disable parallel tool use.\n *\n * Defaults to `false`. If set to `true`, the model will output exactly one tool\n * use.\n */\n disable_parallel_tool_use?: boolean;\n}\n\n/**\n * The model will automatically decide whether to use tools.\n */\nexport interface ToolChoiceAuto {\n type: 'auto';\n\n /**\n * Whether to disable parallel tool use.\n *\n * Defaults to `false`. If set to `true`, the model will output at most one tool\n * use.\n */\n disable_parallel_tool_use?: boolean;\n}\n\n/**\n * The model will not be allowed to use tools.\n */\nexport interface ToolChoiceNone {\n type: 'none';\n}\n\n/**\n * The model will use the specified tool with `tool_choice.name`.\n */\nexport interface ToolChoiceTool {\n /**\n * The name of the tool to use.\n */\n name: string;\n\n type: 'tool';\n\n /**\n * Whether to disable parallel tool use.\n *\n * Defaults to `false`. If set to `true`, the model will output exactly one tool\n * use.\n */\n disable_parallel_tool_use?: boolean;\n}\n\nexport interface ToolResultBlockParam {\n tool_use_id: string;\n\n type: 'tool_result';\n\n cache_control?: CacheControlEphemeral | null;\n\n content?: string | Array<TextBlockParam | ImageBlockParam>;\n\n is_error?: boolean;\n}\n\nexport interface ToolTextEditor20250124 {\n /**\n * Name of the tool.\n *\n * This is how the tool will be called by the model and in tool_use blocks.\n */\n name: 'str_replace_editor';\n\n type: 'text_editor_20250124';\n\n cache_control?: CacheControlEphemeral | null;\n}\n\nexport type ToolUnion = Tool | ToolBash20250124 | ToolTextEditor20250124;\n\nexport interface ToolUseBlock {\n id: string;\n\n input: unknown;\n\n name: string;\n\n type: 'tool_use';\n}\n\nexport interface ToolUseBlockParam {\n id: string;\n\n input: unknown;\n\n name: string;\n\n type: 'tool_use';\n\n cache_control?: CacheControlEphemeral | null;\n}\n\nexport interface URLImageSource {\n type: 'url';\n\n url: string;\n}\n\nexport interface URLPDFSource {\n type: 'url';\n\n url: string;\n}\n\nexport interface Usage {\n /**\n * The number of input tokens used to create the cache entry.\n */\n cache_creation_input_tokens: number | null;\n\n /**\n * The number of input tokens read from the cache.\n */\n cache_read_input_tokens: number | null;\n\n /**\n * The number of input tokens which were used.\n */\n input_tokens: number;\n\n /**\n * The number of output tokens which were used.\n */\n output_tokens: number;\n}\n\nexport type MessageCreateParams = MessageCreateParamsNonStreaming | MessageCreateParamsStreaming;\n\nexport interface MessageCreateParamsBase {\n /**\n * The maximum number of tokens to generate before stopping.\n *\n * Note that our models may stop _before_ reaching this maximum. This parameter\n * only specifies the absolute maximum number of tokens to generate.\n *\n * Different models have different maximum values for this parameter. See\n * [models](https://docs.anthropic.com/en/docs/models-overview) for details.\n */\n max_tokens: number;\n\n /**\n * Input messages.\n *\n * Our models are trained to operate on alternating `user` and `assistant`\n * conversational turns. When creating a new `Message`, you specify the prior\n * conversational turns with the `messages` parameter, and the model then generates\n * the next `Message` in the conversation. Consecutive `user` or `assistant` turns\n * in your request will be combined into a single turn.\n *\n * Each input message must be an object with a `role` and `content`. You can\n * specify a single `user`-role message, or you can include multiple `user` and\n * `assistant` messages.\n *\n * If the final message uses the `assistant` role, the response content will\n * continue immediately from the content in that message. This can be used to\n * constrain part of the model's response.\n *\n * Example with a single `user` message:\n *\n * ```json\n * [{ \"role\": \"user\", \"content\": \"Hello, Claude\" }]\n * ```\n *\n * Example with multiple conversational turns:\n *\n * ```json\n * [\n * { \"role\": \"user\", \"content\": \"Hello there.\" },\n * { \"role\": \"assistant\", \"content\": \"Hi, I'm Claude. How can I help you?\" },\n * { \"role\": \"user\", \"content\": \"Can you explain LLMs in plain English?\" }\n * ]\n * ```\n *\n * Example with a partially-filled response from Claude:\n *\n * ```json\n * [\n * {\n * \"role\": \"user\",\n * \"content\": \"What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun\"\n * },\n * { \"role\": \"assistant\", \"content\": \"The best answer is (\" }\n * ]\n * ```\n *\n * Each input message `content` may be either a single `string` or an array of\n * content blocks, where each block has a specific `type`. Using a `string` for\n * `content` is shorthand for an array of one content block of type `\"text\"`. The\n * following input messages are equivalent:\n *\n * ```json\n * { \"role\": \"user\", \"content\": \"Hello, Claude\" }\n * ```\n *\n * ```json\n * { \"role\": \"user\", \"content\": [{ \"type\": \"text\", \"text\": \"Hello, Claude\" }] }\n * ```\n *\n * Starting with Claude 3 models, you can also send image content blocks:\n *\n * ```json\n * {\n * \"role\": \"user\",\n * \"content\": [\n * {\n * \"type\": \"image\",\n * \"source\": {\n * \"type\": \"base64\",\n * \"media_type\": \"image/jpeg\",\n * \"data\": \"/9j/4AAQSkZJRg...\"\n * }\n * },\n * { \"type\": \"text\", \"text\": \"What is in this image?\" }\n * ]\n * }\n * ```\n *\n * We currently support the `base64` source type for images, and the `image/jpeg`,\n * `image/png`, `image/gif`, and `image/webp` media types.\n *\n * See [examples](https://docs.anthropic.com/en/api/messages-examples#vision) for\n * more input examples.\n *\n * Note that if you want to include a\n * [system prompt](https://docs.anthropic.com/en/docs/system-prompts), you can use\n * the top-level `system` parameter \u2014 there is no `\"system\"` role for input\n * messages in the Messages API.\n */\n messages: Array<MessageParam>;\n\n /**\n * The model that will complete your prompt.\\n\\nSee\n * [models](https://docs.anthropic.com/en/docs/models-overview) for additional\n * details and options.\n */\n model: Model;\n\n /**\n * An object describing metadata about the request.\n */\n metadata?: Metadata;\n\n /**\n * Custom text sequences that will cause the model to stop generating.\n *\n * Our models will normally stop when they have naturally completed their turn,\n * which will result in a response `stop_reason` of `\"end_turn\"`.\n *\n * If you want the model to stop generating when it encounters custom strings of\n * text, you can use the `stop_sequences` parameter. If the model encounters one of\n * the custom sequences, the response `stop_reason` value will be `\"stop_sequence\"`\n * and the response `stop_sequence` value will contain the matched stop sequence.\n */\n stop_sequences?: Array<string>;\n\n /**\n * Whether to incrementally stream the response using server-sent events.\n *\n * See [streaming](https://docs.anthropic.com/en/api/messages-streaming) for\n * details.\n */\n stream?: boolean;\n\n /**\n * System prompt.\n *\n * A system prompt is a way of providing context and instructions to Claude, such\n * as specifying a particular goal or role. See our\n * [guide to system prompts](https://docs.anthropic.com/en/docs/system-prompts).\n */\n system?: string | Array<TextBlockParam>;\n\n /**\n * Amount of randomness injected into the response.\n *\n * Defaults to `1.0`. Ranges from `0.0` to `1.0`. Use `temperature` closer to `0.0`\n * for analytical / multiple choice, and closer to `1.0` for creative and\n * generative tasks.\n *\n * Note that even with `temperature` of `0.0`, the results will not be fully\n * deterministic.\n */\n temperature?: number;\n\n /**\n * Configuration for enabling Claude's extended thinking.\n *\n * When enabled, responses include `thinking` content blocks showing Claude's\n * thinking process before the final answer. Requires a minimum budget of 1,024\n * tokens and counts towards your `max_tokens` limit.\n *\n * See\n * [extended thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking)\n * for details.\n */\n thinking?: ThinkingConfigParam;\n\n /**\n * How the model should use the provided tools. The model can use a specific tool,\n * any available tool, decide by itself, or not use tools at all.\n */\n tool_choice?: ToolChoice;\n\n /**\n * Definitions of tools that the model may use.\n *\n * If you include `tools` in your API request, the model may return `tool_use`\n * content blocks that represent the model's use of those tools. You can then run\n * those tools using the tool input generated by the model and then optionally\n * return results back to the model using `tool_result` content blocks.\n *\n * Each tool definition includes:\n *\n * - `name`: Name of the tool.\n * - `description`: Optional, but strongly-recommended description of the tool.\n * - `input_schema`: [JSON schema](https://json-schema.org/draft/2020-12) for the\n * tool `input` shape that the model will produce in `tool_use` output content\n * blocks.\n *\n * For example, if you defined `tools` as:\n *\n * ```json\n * [\n * {\n * \"name\": \"get_stock_price\",\n * \"description\": \"Get the current stock price for a given ticker symbol.\",\n * \"input_schema\": {\n * \"type\": \"object\",\n * \"properties\": {\n * \"ticker\": {\n * \"type\": \"string\",\n * \"description\": \"The stock ticker symbol, e.g. AAPL for Apple Inc.\"\n * }\n * },\n * \"required\": [\"ticker\"]\n * }\n * }\n * ]\n * ```\n *\n * And then asked the model \"What's the S&P 500 at today?\", the model might produce\n * `tool_use` content blocks in the response like this:\n *\n * ```json\n * [\n * {\n * \"type\": \"tool_use\",\n * \"id\": \"toolu_01D7FLrfh4GYq7yT1ULFeyMV\",\n * \"name\": \"get_stock_price\",\n * \"input\": { \"ticker\": \"^GSPC\" }\n * }\n * ]\n * ```\n *\n * You might then run your `get_stock_price` tool with `{\"ticker\": \"^GSPC\"}` as an\n * input, and return the following back to the model in a subsequent `user`\n * message:\n *\n * ```json\n * [\n * {\n * \"type\": \"tool_result\",\n * \"tool_use_id\": \"toolu_01D7FLrfh4GYq7yT1ULFeyMV\",\n * \"content\": \"259.75 USD\"\n * }\n * ]\n * ```\n *\n * Tools can be used for workflows that include running client-side tools and\n * functions, or more generally whenever you want the model to produce a particular\n * JSON structure of output.\n *\n * See our [guide](https://docs.anthropic.com/en/docs/tool-use) for more details.\n */\n tools?: Array<ToolUnion>;\n\n /**\n * Only sample from the top K options for each subsequent token.\n *\n * Used to remove \"long tail\" low probability responses.\n * [Learn more technical details here](https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277).\n *\n * Recommended for advanced use cases only. You usually only need to use\n * `temperature`.\n */\n top_k?: number;\n\n /**\n * Use nucleus sampling.\n *\n * In nucleus sampling, we compute the cumulative distribution over all the options\n * for each subsequent token in decreasing probability order and cut it off once it\n * reaches a particular probability specified by `top_p`. You should either alter\n * `temperature` or `top_p`, but not both.\n *\n * Recommended for advanced use cases only. You usually only need to use\n * `temperature`.\n */\n top_p?: number;\n}\n\nexport namespace MessageCreateParams {\n /**\n * @deprecated use `Anthropic.Messages.ToolChoiceAuto` instead\n */\n export type Metadata = MessagesAPI.Metadata;\n\n /**\n * @deprecated use `Anthropic.Messages.ToolChoiceAuto` instead\n */\n export type ToolChoiceAuto = MessagesAPI.ToolChoiceAuto;\n\n /**\n * @deprecated use `Anthropic.Messages.ToolChoiceAny` instead\n */\n export type ToolChoiceAny = MessagesAPI.ToolChoiceAny;\n\n /**\n * @deprecated use `Anthropic.Messages.ToolChoiceTool` instead\n */\n export type ToolChoiceTool = MessagesAPI.ToolChoiceTool;\n\n export type MessageCreateParamsNonStreaming = MessagesAPI.MessageCreateParamsNonStreaming;\n export type MessageCreateParamsStreaming = MessagesAPI.MessageCreateParamsStreaming;\n}\n\nexport interface MessageCreateParamsNonStreaming extends MessageCreateParamsBase {\n /**\n * Whether to incrementally stream the response using server-sent events.\n *\n * See [streaming](https://docs.anthropic.com/en/api/messages-streaming) for\n * details.\n */\n stream?: false;\n}\n\nexport interface MessageCreateParamsStreaming extends MessageCreateParamsBase {\n /**\n * Whether to incrementally stream the response using server-sent events.\n *\n * See [streaming](https://docs.anthropic.com/en/api/messages-streaming) for\n * details.\n */\n stream: true;\n}\n\nexport type MessageStreamParams = MessageCreateParamsBase;\n\nexport interface MessageCountTokensParams {\n /**\n * Input messages.\n *\n * Our models are trained to operate on alternating `user` and `assistant`\n * conversational turns. When creating a new `Message`, you specify the prior\n * conversational turns with the `messages` parameter, and the model then generates\n * the next `Message` in the conversation. Consecutive `user` or `assistant` turns\n * in your request will be combined into a single turn.\n *\n * Each input message must be an object with a `role` and `content`. You can\n * specify a single `user`-role message, or you can include multiple `user` and\n * `assistant` messages.\n *\n * If the final message uses the `assistant` role, the response content will\n * continue immediately from the content in that message. This can be used to\n * constrain part of the model's response.\n *\n * Example with a single `user` message:\n *\n * ```json\n * [{ \"role\": \"user\", \"content\": \"Hello, Claude\" }]\n * ```\n *\n * Example with multiple conversational turns:\n *\n * ```json\n * [\n * { \"role\": \"user\", \"content\": \"Hello there.\" },\n * { \"role\": \"assistant\", \"content\": \"Hi, I'm Claude. How can I help you?\" },\n * { \"role\": \"user\", \"content\": \"Can you explain LLMs in plain English?\" }\n * ]\n * ```\n *\n * Example with a partially-filled response from Claude:\n *\n * ```json\n * [\n * {\n * \"role\": \"user\",\n * \"content\": \"What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun\"\n * },\n * { \"role\": \"assistant\", \"content\": \"The best answer is (\" }\n * ]\n * ```\n *\n * Each input message `content` may be either a single `string` or an array of\n * content blocks, where each block has a specific `type`. Using a `string` for\n * `content` is shorthand for an array of one content block of type `\"text\"`. The\n * following input messages are equivalent:\n *\n * ```json\n * { \"role\": \"user\", \"content\": \"Hello, Claude\" }\n * ```\n *\n * ```json\n * { \"role\": \"user\", \"content\": [{ \"type\": \"text\", \"text\": \"Hello, Claude\" }] }\n * ```\n *\n * Starting with Claude 3 models, you can also send image content blocks:\n *\n * ```json\n * {\n * \"role\": \"user\",\n * \"content\": [\n * {\n * \"type\": \"image\",\n * \"source\": {\n * \"type\": \"base64\",\n * \"media_type\": \"image/jpeg\",\n * \"data\": \"/9j/4AAQSkZJRg...\"\n * }\n * },\n * { \"type\": \"text\", \"text\": \"What is in this image?\" }\n * ]\n * }\n * ```\n *\n * We currently support the `base64` source type for images, and the `image/jpeg`,\n * `image/png`, `image/gif`, and `image/webp` media types.\n *\n * See [examples](https://docs.anthropic.com/en/api/messages-examples#vision) for\n * more input examples.\n *\n * Note that if you want to include a\n * [system prompt](https://docs.anthropic.com/en/docs/system-prompts), you can use\n * the top-level `system` parameter \u2014 there is no `\"system\"` role for input\n * messages in the Messages API.\n */\n messages: Array<MessageParam>;\n\n /**\n * The model that will complete your prompt.\\n\\nSee\n * [models](https://docs.anthropic.com/en/docs/models-overview) for additional\n * details and options.\n */\n model: Model;\n\n /**\n * System prompt.\n *\n * A system prompt is a way of providing context and instructions to Claude, such\n * as specifying a particular goal or role. See our\n * [guide to system prompts](https://docs.anthropic.com/en/docs/system-prompts).\n */\n system?: string | Array<TextBlockParam>;\n\n /**\n * Configuration for enabling Claude's extended thinking.\n *\n * When enabled, responses include `thinking` content blocks showing Claude's\n * thinking process before the final answer. Requires a minimum budget of 1,024\n * tokens and counts towards your `max_tokens` limit.\n *\n * See\n * [extended thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking)\n * for details.\n */\n thinking?: ThinkingConfigParam;\n\n /**\n * How the model should use the provided tools. The model can use a specific tool,\n * any available tool, decide by itself, or not use tools at all.\n */\n tool_choice?: ToolChoice;\n\n /**\n * Definitions of tools that the model may use.\n *\n * If you include `tools` in your API request, the model may return `tool_use`\n * content blocks that represent the model's use of those tools. You can then run\n * those tools using the tool input generated by the model and then optionally\n * return results back to the model using `tool_result` content blocks.\n *\n * Each tool definition includes:\n *\n * - `name`: Name of the tool.\n * - `description`: Optional, but strongly-recommended description of the tool.\n * - `input_schema`: [JSON schema](https://json-schema.org/draft/2020-12) for the\n * tool `input` shape that the model will produce in `tool_use` output content\n * blocks.\n *\n * For example, if you defined `tools` as:\n *\n * ```json\n * [\n * {\n * \"name\": \"get_stock_price\",\n * \"description\": \"Get the current stock price for a given ticker symbol.\",\n * \"input_schema\": {\n * \"type\": \"object\",\n * \"properties\": {\n * \"ticker\": {\n * \"type\": \"string\",\n * \"description\": \"The stock ticker symbol, e.g. AAPL for Apple Inc.\"\n * }\n * },\n * \"required\": [\"ticker\"]\n * }\n * }\n * ]\n * ```\n *\n * And then asked the model \"What's the S&P 500 at today?\", the model might produce\n * `tool_use` content blocks in the response like this:\n *\n * ```json\n * [\n * {\n * \"type\": \"tool_use\",\n * \"id\": \"toolu_01D7FLrfh4GYq7yT1ULFeyMV\",\n * \"name\": \"get_stock_price\",\n * \"input\": { \"ticker\": \"^GSPC\" }\n * }\n * ]\n * ```\n *\n * You might then run your `get_stock_price` tool with `{\"ticker\": \"^GSPC\"}` as an\n * input, and return the following back to the model in a subsequent `user`\n * message:\n *\n * ```json\n * [\n * {\n * \"type\": \"tool_result\",\n * \"tool_use_id\": \"toolu_01D7FLrfh4GYq7yT1ULFeyMV\",\n * \"content\": \"259.75 USD\"\n * }\n * ]\n * ```\n *\n * Tools can be used for workflows that include running client-side tools and\n * functions, or more generally whenever you want the model to produce a particular\n * JSON structure of output.\n *\n * See our [guide](https://docs.anthropic.com/en/docs/tool-use) for more details.\n */\n tools?: Array<MessageCountTokensTool>;\n}\n\nMessages.Batches = Batches;\nMessages.MessageBatchesPage = MessageBatchesPage;\n\nexport declare namespace Messages {\n export {\n type Base64ImageSource as Base64ImageSource,\n type Base64PDFSource as Base64PDFSource,\n type CacheControlEphemeral as CacheControlEphemeral,\n type CitationCharLocation as CitationCharLocation,\n type CitationCharLocationParam as CitationCharLocationParam,\n type CitationContentBlockLocation as CitationContentBlockLocation,\n type CitationContentBlockLocationParam as CitationContentBlockLocationParam,\n type CitationPageLocation as CitationPageLocation,\n type CitationPageLocationParam as CitationPageLocationParam,\n type CitationsConfigParam as CitationsConfigParam,\n type CitationsDelta as CitationsDelta,\n type ContentBlock as ContentBlock,\n type ContentBlockDeltaEvent as ContentBlockDeltaEvent,\n type ContentBlockParam as ContentBlockParam,\n type ContentBlockSource as ContentBlockSource,\n type ContentBlockSourceContent as ContentBlockSourceContent,\n type ContentBlockStartEvent as ContentBlockStartEvent,\n type ContentBlockStopEvent as ContentBlockStopEvent,\n type DocumentBlockParam as DocumentBlockParam,\n type ImageBlockParam as ImageBlockParam,\n type InputJsonDelta as InputJsonDelta,\n type InputJSONDelta as InputJSONDelta,\n type Message as Message,\n type MessageCountTokensTool as MessageCountTokensTool,\n type MessageDeltaEvent as MessageDeltaEvent,\n type MessageDeltaUsage as MessageDeltaUsage,\n type MessageParam as MessageParam,\n type MessageStartEvent as MessageStartEvent,\n type MessageStopEvent as MessageStopEvent,\n type MessageStreamEvent as MessageStreamEvent,\n type MessageTokensCount as MessageTokensCount,\n type Metadata as Metadata,\n type Model as Model,\n type PlainTextSource as PlainTextSource,\n type RawContentBlockDeltaEvent as RawContentBlockDeltaEvent,\n type RawContentBlockStartEvent as RawContentBlockStartEvent,\n type RawContentBlockStopEvent as RawContentBlockStopEvent,\n type RawMessageDeltaEvent as RawMessageDeltaEvent,\n type RawMessageStartEvent as RawMessageStartEvent,\n type RawMessageStopEvent as RawMessageStopEvent,\n type RawMessageStreamEvent as RawMessageStreamEvent,\n type RedactedThinkingBlock as RedactedThinkingBlock,\n type RedactedThinkingBlockParam as RedactedThinkingBlockParam,\n type SignatureDelta as SignatureDelta,\n type TextBlock as TextBlock,\n type TextBlockParam as TextBlockParam,\n type TextCitation as TextCitation,\n type TextCitationParam as TextCitationParam,\n type TextDelta as TextDelta,\n type ThinkingBlock as ThinkingBlock,\n type ThinkingBlockParam as ThinkingBlockParam,\n type ThinkingConfigDisabled as ThinkingConfigDisabled,\n type ThinkingConfigEnabled as ThinkingConfigEnabled,\n type ThinkingConfigParam as ThinkingConfigParam,\n type ThinkingDelta as ThinkingDelta,\n type Tool as Tool,\n type ToolBash20250124 as ToolBash20250124,\n type ToolChoice as ToolChoice,\n type ToolChoiceAny as ToolChoiceAny,\n type ToolChoiceAuto as ToolChoiceAuto,\n type ToolChoiceNone as ToolChoiceNone,\n type ToolChoiceTool as ToolChoiceTool,\n type ToolResultBlockParam as ToolResultBlockParam,\n type ToolTextEditor20250124 as ToolTextEditor20250124,\n type ToolUnion as ToolUnion,\n type ToolUseBlock as ToolUseBlock,\n type ToolUseBlockParam as ToolUseBlockParam,\n type URLImageSource as URLImageSource,\n type URLPDFSource as URLPDFSource,\n type Usage as Usage,\n type MessageCreateParams as MessageCreateParams,\n type MessageCreateParamsNonStreaming as MessageCreateParamsNonStreaming,\n type MessageCreateParamsStreaming as MessageCreateParamsStreaming,\n type MessageStreamParams as MessageStreamParams,\n type MessageCountTokensParams as MessageCountTokensParams,\n };\n\n export {\n Batches as Batches,\n type DeletedMessageBatch as DeletedMessageBatch,\n type MessageBatch as MessageBatch,\n type MessageBatchCanceledResult as MessageBatchCanceledResult,\n type MessageBatchErroredResult as MessageBatchErroredResult,\n type MessageBatchExpiredResult as MessageBatchExpiredResult,\n type MessageBatchIndividualResponse as MessageBatchIndividualResponse,\n type MessageBatchRequestCounts as MessageBatchRequestCounts,\n type MessageBatchResult as MessageBatchResult,\n type MessageBatchSucceededResult as MessageBatchSucceededResult,\n MessageBatchesPage as MessageBatchesPage,\n type BatchCreateParams as BatchCreateParams,\n type BatchListParams as BatchListParams,\n };\n}\n", "// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\n\nimport { APIResource } from \"../resource.js\";\nimport { isRequestOptions } from \"../core.js\";\nimport * as Core from \"../core.js\";\nimport { Page, type PageParams } from \"../pagination.js\";\n\nexport class Models extends APIResource {\n /**\n * Get a specific model.\n *\n * The Models API response can be used to determine information about a specific\n * model or resolve a model alias to a model ID.\n */\n retrieve(modelId: string, options?: Core.RequestOptions): Core.APIPromise<ModelInfo> {\n return this._client.get(`/v1/models/${modelId}`, options);\n }\n\n /**\n * List available models.\n *\n * The Models API response can be used to determine which models are available for\n * use in the API. More recently released models are listed first.\n */\n list(query?: ModelListParams, options?: Core.RequestOptions): Core.PagePromise<ModelInfosPage, ModelInfo>;\n list(options?: Core.RequestOptions): Core.PagePromise<ModelInfosPage, ModelInfo>;\n list(\n query: ModelListParams | Core.RequestOptions = {},\n options?: Core.RequestOptions,\n ): Core.PagePromise<ModelInfosPage, ModelInfo> {\n if (isRequestOptions(query)) {\n return this.list({}, query);\n }\n return this._client.getAPIList('/v1/models', ModelInfosPage, { query, ...options });\n }\n}\n\nexport class ModelInfosPage extends Page<ModelInfo> {}\n\nexport interface ModelInfo {\n /**\n * Unique model identifier.\n */\n id: string;\n\n /**\n * RFC 3339 datetime string representing the time at which the model was released.\n * May be set to an epoch value if the release date is unknown.\n */\n created_at: string;\n\n /**\n * A human-readable name for the model.\n */\n display_name: string;\n\n /**\n * Object type.\n *\n * For Models, this is always `\"model\"`.\n */\n type: 'model';\n}\n\nexport interface ModelListParams extends PageParams {}\n\nModels.ModelInfosPage = ModelInfosPage;\n\nexport declare namespace Models {\n export {\n type ModelInfo as ModelInfo,\n ModelInfosPage as ModelInfosPage,\n type ModelListParams as ModelListParams,\n };\n}\n", "// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\n\nimport { type Agent } from \"./_shims/index.js\";\nimport * as Core from \"./core.js\";\nimport * as Errors from \"./error.js\";\nimport * as Pagination from \"./pagination.js\";\nimport { type PageParams, PageResponse } from \"./pagination.js\";\nimport * as Uploads from \"./uploads.js\";\nimport * as API from \"./resources/index.js\";\nimport {\n Completion,\n CompletionCreateParams,\n CompletionCreateParamsNonStreaming,\n CompletionCreateParamsStreaming,\n Completions,\n} from \"./resources/completions.js\";\nimport { ModelInfo, ModelInfosPage, ModelListParams, Models } from \"./resources/models.js\";\nimport {\n AnthropicBeta,\n Beta,\n BetaAPIError,\n BetaAuthenticationError,\n BetaBillingError,\n BetaError,\n BetaErrorResponse,\n BetaGatewayTimeoutError,\n BetaInvalidRequestError,\n BetaNotFoundError,\n BetaOverloadedError,\n BetaPermissionError,\n BetaRateLimitError,\n} from \"./resources/beta/beta.js\";\nimport {\n Base64ImageSource,\n Base64PDFSource,\n CacheControlEphemeral,\n CitationCharLocation,\n CitationCharLocationParam,\n CitationContentBlockLocation,\n CitationContentBlockLocationParam,\n CitationPageLocation,\n CitationPageLocationParam,\n CitationsConfigParam,\n CitationsDelta,\n ContentBlock,\n ContentBlockDeltaEvent,\n ContentBlockParam,\n ContentBlockSource,\n ContentBlockSourceContent,\n ContentBlockStartEvent,\n ContentBlockStopEvent,\n DocumentBlockParam,\n ImageBlockParam,\n InputJSONDelta,\n Message,\n MessageCountTokensParams,\n MessageCountTokensTool,\n MessageCreateParams,\n MessageCreateParamsNonStreaming,\n MessageCreateParamsStreaming,\n MessageDeltaEvent,\n MessageDeltaUsage,\n MessageParam,\n MessageStartEvent,\n MessageStopEvent,\n MessageStreamEvent,\n MessageStreamParams,\n MessageTokensCount,\n Messages,\n Metadata,\n Model,\n PlainTextSource,\n RawContentBlockDeltaEvent,\n RawContentBlockStartEvent,\n RawContentBlockStopEvent,\n RawMessageDeltaEvent,\n RawMessageStartEvent,\n RawMessageStopEvent,\n RawMessageStreamEvent,\n RedactedThinkingBlock,\n RedactedThinkingBlockParam,\n SignatureDelta,\n TextBlock,\n TextBlockParam,\n TextCitation,\n TextCitationParam,\n TextDelta,\n ThinkingBlock,\n ThinkingBlockParam,\n ThinkingConfigDisabled,\n ThinkingConfigEnabled,\n ThinkingConfigParam,\n ThinkingDelta,\n Tool,\n ToolBash20250124,\n ToolChoice,\n ToolChoiceAny,\n ToolChoiceAuto,\n ToolChoiceNone,\n ToolChoiceTool,\n ToolResultBlockParam,\n ToolTextEditor20250124,\n ToolUnion,\n ToolUseBlock,\n ToolUseBlockParam,\n URLImageSource,\n URLPDFSource,\n Usage,\n} from \"./resources/messages/messages.js\";\n\nexport interface ClientOptions {\n /**\n * Defaults to process.env['ANTHROPIC_API_KEY'].\n */\n apiKey?: string | null | undefined;\n\n /**\n * Defaults to process.env['ANTHROPIC_AUTH_TOKEN'].\n */\n authToken?: string | null | undefined;\n\n /**\n * Override the default base URL for the API, e.g., \"https://api.example.com/v2/\"\n *\n * Defaults to process.env['ANTHROPIC_BASE_URL'].\n */\n baseURL?: string | null | undefined;\n\n /**\n * The maximum amount of time (in milliseconds) that the client should wait for a response\n * from the server before timing out a single request.\n *\n * Note that request timeouts are retried by default, so in a worst-case scenario you may wait\n * much longer than this timeout before the promise succeeds or fails.\n */\n timeout?: number | undefined;\n\n /**\n * An HTTP agent used to manage HTTP(S) connections.\n *\n * If not provided, an agent will be constructed by default in the Node.js environment,\n * otherwise no agent is used.\n */\n httpAgent?: Agent | undefined;\n\n /**\n * Specify a custom `fetch` function implementation.\n *\n * If not provided, we use `node-fetch` on Node.js and otherwise expect that `fetch` is\n * defined globally.\n */\n fetch?: Core.Fetch | undefined;\n\n /**\n * The maximum number of times that the client will retry a request in case of a\n * temporary failure, like a network error or a 5XX error from the server.\n *\n * @default 2\n */\n maxRetries?: number | undefined;\n\n /**\n * Default headers to include with every request to the API.\n *\n * These can be removed in individual requests by explicitly setting the\n * header to `undefined` or `null` in request options.\n */\n defaultHeaders?: Core.Headers | undefined;\n\n /**\n * Default query parameters to include with every request to the API.\n *\n * These can be removed in individual requests by explicitly setting the\n * param to `undefined` in request options.\n */\n defaultQuery?: Core.DefaultQuery | undefined;\n\n /**\n * By default, client-side use of this library is not allowed, as it risks exposing your secret API credentials to attackers.\n * Only set this option to `true` if you understand the risks and have appropriate mitigations in place.\n */\n dangerouslyAllowBrowser?: boolean | undefined;\n}\n\n/**\n * API Client for interfacing with the Anthropic API.\n */\nexport class Anthropic extends Core.APIClient {\n apiKey: string | null;\n authToken: string | null;\n\n private _options: ClientOptions;\n\n /**\n * API Client for interfacing with the Anthropic API.\n *\n * @param {string | null | undefined} [opts.apiKey=process.env['ANTHROPIC_API_KEY'] ?? null]\n * @param {string | null | undefined} [opts.authToken=process.env['ANTHROPIC_AUTH_TOKEN'] ?? null]\n * @param {string} [opts.baseURL=process.env['ANTHROPIC_BASE_URL'] ?? https://api.anthropic.com] - Override the default base URL for the API.\n * @param {number} [opts.timeout=10 minutes] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out.\n * @param {number} [opts.httpAgent] - An HTTP agent used to manage HTTP(s) connections.\n * @param {Core.Fetch} [opts.fetch] - Specify a custom `fetch` function implementation.\n * @param {number} [opts.maxRetries=2] - The maximum number of times the client will retry a request.\n * @param {Core.Headers} opts.defaultHeaders - Default headers to include with every request to the API.\n * @param {Core.DefaultQuery} opts.defaultQuery - Default query parameters to include with every request to the API.\n * @param {boolean} [opts.dangerouslyAllowBrowser=false] - By default, client-side use of this library is not allowed, as it risks exposing your secret API credentials to attackers.\n */\n constructor({\n baseURL = Core.readEnv('ANTHROPIC_BASE_URL'),\n apiKey = Core.readEnv('ANTHROPIC_API_KEY') ?? null,\n authToken = Core.readEnv('ANTHROPIC_AUTH_TOKEN') ?? null,\n ...opts\n }: ClientOptions = {}) {\n const options: ClientOptions = {\n apiKey,\n authToken,\n ...opts,\n baseURL: baseURL || `https://api.anthropic.com`,\n };\n\n if (!options.dangerouslyAllowBrowser && Core.isRunningInBrowser()) {\n throw new Errors.AnthropicError(\n \"It looks like you're running in a browser-like environment.\\n\\nThis is disabled by default, as it risks exposing your secret API credentials to attackers.\\nIf you understand the risks and have appropriate mitigations in place,\\nyou can set the `dangerouslyAllowBrowser` option to `true`, e.g.,\\n\\nnew Anthropic({ apiKey, dangerouslyAllowBrowser: true });\\n\",\n );\n }\n\n super({\n baseURL: options.baseURL!,\n timeout: options.timeout ?? 600000 /* 10 minutes */,\n httpAgent: options.httpAgent,\n maxRetries: options.maxRetries,\n fetch: options.fetch,\n });\n\n this._options = options;\n\n this.apiKey = apiKey;\n this.authToken = authToken;\n }\n\n completions: API.Completions = new API.Completions(this);\n messages: API.Messages = new API.Messages(this);\n models: API.Models = new API.Models(this);\n beta: API.Beta = new API.Beta(this);\n\n protected override defaultQuery(): Core.DefaultQuery | undefined {\n return this._options.defaultQuery;\n }\n\n protected override defaultHeaders(opts: Core.FinalRequestOptions): Core.Headers {\n return {\n ...super.defaultHeaders(opts),\n ...(this._options.dangerouslyAllowBrowser ?\n { 'anthropic-dangerous-direct-browser-access': 'true' }\n : undefined),\n 'anthropic-version': '2023-06-01',\n ...this._options.defaultHeaders,\n };\n }\n\n protected override validateHeaders(headers: Core.Headers, customHeaders: Core.Headers) {\n if (this.apiKey && headers['x-api-key']) {\n return;\n }\n if (customHeaders['x-api-key'] === null) {\n return;\n }\n\n if (this.authToken && headers['authorization']) {\n return;\n }\n if (customHeaders['authorization'] === null) {\n return;\n }\n\n throw new Error(\n 'Could not resolve authentication method. Expected either apiKey or authToken to be set. Or for one of the \"X-Api-Key\" or \"Authorization\" headers to be explicitly omitted',\n );\n }\n\n protected override authHeaders(opts: Core.FinalRequestOptions): Core.Headers {\n const apiKeyAuth = this.apiKeyAuth(opts);\n const bearerAuth = this.bearerAuth(opts);\n\n if (apiKeyAuth != null && !Core.isEmptyObj(apiKeyAuth)) {\n return apiKeyAuth;\n }\n\n if (bearerAuth != null && !Core.isEmptyObj(bearerAuth)) {\n return bearerAuth;\n }\n return {};\n }\n\n protected apiKeyAuth(opts: Core.FinalRequestOptions): Core.Headers {\n if (this.apiKey == null) {\n return {};\n }\n return { 'X-Api-Key': this.apiKey };\n }\n\n protected bearerAuth(opts: Core.FinalRequestOptions): Core.Headers {\n if (this.authToken == null) {\n return {};\n }\n return { Authorization: `Bearer ${this.authToken}` };\n }\n\n static Anthropic = this;\n static HUMAN_PROMPT = '\\n\\nHuman:';\n static AI_PROMPT = '\\n\\nAssistant:';\n static DEFAULT_TIMEOUT = 600000; // 10 minutes\n\n static AnthropicError = Errors.AnthropicError;\n static APIError = Errors.APIError;\n static APIConnectionError = Errors.APIConnectionError;\n static APIConnectionTimeoutError = Errors.APIConnectionTimeoutError;\n static APIUserAbortError = Errors.APIUserAbortError;\n static NotFoundError = Errors.NotFoundError;\n static ConflictError = Errors.ConflictError;\n static RateLimitError = Errors.RateLimitError;\n static BadRequestError = Errors.BadRequestError;\n static AuthenticationError = Errors.AuthenticationError;\n static InternalServerError = Errors.InternalServerError;\n static PermissionDeniedError = Errors.PermissionDeniedError;\n static UnprocessableEntityError = Errors.UnprocessableEntityError;\n\n static toFile = Uploads.toFile;\n static fileFromPath = Uploads.fileFromPath;\n}\n\nAnthropic.Completions = Completions;\nAnthropic.Messages = Messages;\nAnthropic.Models = Models;\nAnthropic.ModelInfosPage = ModelInfosPage;\nAnthropic.Beta = Beta;\nexport declare namespace Anthropic {\n export type RequestOptions = Core.RequestOptions;\n\n export import Page = Pagination.Page;\n export { type PageParams as PageParams, type PageResponse as PageResponse };\n\n export {\n Completions as Completions,\n type Completion as Completion,\n type CompletionCreateParams as CompletionCreateParams,\n type CompletionCreateParamsNonStreaming as CompletionCreateParamsNonStreaming,\n type CompletionCreateParamsStreaming as CompletionCreateParamsStreaming,\n };\n\n export {\n Messages as Messages,\n type Base64ImageSource as Base64ImageSource,\n type Base64PDFSource as Base64PDFSource,\n type CacheControlEphemeral as CacheControlEphemeral,\n type CitationCharLocation as CitationCharLocation,\n type CitationCharLocationParam as CitationCharLocationParam,\n type CitationContentBlockLocation as CitationContentBlockLocation,\n type CitationContentBlockLocationParam as CitationContentBlockLocationParam,\n type CitationPageLocation as CitationPageLocation,\n type CitationPageLocationParam as CitationPageLocationParam,\n type CitationsConfigParam as CitationsConfigParam,\n type CitationsDelta as CitationsDelta,\n type ContentBlock as ContentBlock,\n type ContentBlockDeltaEvent as ContentBlockDeltaEvent,\n type ContentBlockParam as ContentBlockParam,\n type ContentBlockSource as ContentBlockSource,\n type ContentBlockSourceContent as ContentBlockSourceContent,\n type ContentBlockStartEvent as ContentBlockStartEvent,\n type ContentBlockStopEvent as ContentBlockStopEvent,\n type DocumentBlockParam as DocumentBlockParam,\n type ImageBlockParam as ImageBlockParam,\n type InputJSONDelta as InputJSONDelta,\n type Message as Message,\n type MessageCountTokensTool as MessageCountTokensTool,\n type MessageDeltaEvent as MessageDeltaEvent,\n type MessageDeltaUsage as MessageDeltaUsage,\n type MessageParam as MessageParam,\n type MessageStartEvent as MessageStartEvent,\n type MessageStopEvent as MessageStopEvent,\n type MessageStreamEvent as MessageStreamEvent,\n type MessageTokensCount as MessageTokensCount,\n type Metadata as Metadata,\n type Model as Model,\n type PlainTextSource as PlainTextSource,\n type RawContentBlockDeltaEvent as RawContentBlockDeltaEvent,\n type RawContentBlockStartEvent as RawContentBlockStartEvent,\n type RawContentBlockStopEvent as RawContentBlockStopEvent,\n type RawMessageDeltaEvent as RawMessageDeltaEvent,\n type RawMessageStartEvent as RawMessageStartEvent,\n type RawMessageStopEvent as RawMessageStopEvent,\n type RawMessageStreamEvent as RawMessageStreamEvent,\n type RedactedThinkingBlock as RedactedThinkingBlock,\n type RedactedThinkingBlockParam as RedactedThinkingBlockParam,\n type SignatureDelta as SignatureDelta,\n type TextBlock as TextBlock,\n type TextBlockParam as TextBlockParam,\n type TextCitation as TextCitation,\n type TextCitationParam as TextCitationParam,\n type TextDelta as TextDelta,\n type ThinkingBlock as ThinkingBlock,\n type ThinkingBlockParam as ThinkingBlockParam,\n type ThinkingConfigDisabled as ThinkingConfigDisabled,\n type ThinkingConfigEnabled as ThinkingConfigEnabled,\n type ThinkingConfigParam as ThinkingConfigParam,\n type ThinkingDelta as ThinkingDelta,\n type Tool as Tool,\n type ToolBash20250124 as ToolBash20250124,\n type ToolChoice as ToolChoice,\n type ToolChoiceAny as ToolChoiceAny,\n type ToolChoiceAuto as ToolChoiceAuto,\n type ToolChoiceNone as ToolChoiceNone,\n type ToolChoiceTool as ToolChoiceTool,\n type ToolResultBlockParam as ToolResultBlockParam,\n type ToolTextEditor20250124 as ToolTextEditor20250124,\n type ToolUnion as ToolUnion,\n type ToolUseBlock as ToolUseBlock,\n type ToolUseBlockParam as ToolUseBlockParam,\n type URLImageSource as URLImageSource,\n type URLPDFSource as URLPDFSource,\n type Usage as Usage,\n type MessageCreateParams as MessageCreateParams,\n type MessageCreateParamsNonStreaming as MessageCreateParamsNonStreaming,\n type MessageCreateParamsStreaming as MessageCreateParamsStreaming,\n type MessageStreamParams as MessageStreamParams,\n type MessageCountTokensParams as MessageCountTokensParams,\n };\n\n export {\n Models as Models,\n type ModelInfo as ModelInfo,\n ModelInfosPage as ModelInfosPage,\n type ModelListParams as ModelListParams,\n };\n\n export {\n Beta as Beta,\n type AnthropicBeta as AnthropicBeta,\n type BetaAPIError as BetaAPIError,\n type BetaAuthenticationError as BetaAuthenticationError,\n type BetaBillingError as BetaBillingError,\n type BetaError as BetaError,\n type BetaErrorResponse as BetaErrorResponse,\n type BetaGatewayTimeoutError as BetaGatewayTimeoutError,\n type BetaInvalidRequestError as BetaInvalidRequestError,\n type BetaNotFoundError as BetaNotFoundError,\n type BetaOverloadedError as BetaOverloadedError,\n type BetaPermissionError as BetaPermissionError,\n type BetaRateLimitError as BetaRateLimitError,\n };\n\n export type APIErrorObject = API.APIErrorObject;\n export type AuthenticationError = API.AuthenticationError;\n export type BillingError = API.BillingError;\n export type ErrorObject = API.ErrorObject;\n export type ErrorResponse = API.ErrorResponse;\n export type GatewayTimeoutError = API.GatewayTimeoutError;\n export type InvalidRequestError = API.InvalidRequestError;\n export type NotFoundError = API.NotFoundError;\n export type OverloadedError = API.OverloadedError;\n export type PermissionError = API.PermissionError;\n export type RateLimitError = API.RateLimitError;\n}\nexport const { HUMAN_PROMPT, AI_PROMPT } = Anthropic;\n\nexport { toFile, fileFromPath } from \"./uploads.js\";\nexport {\n AnthropicError,\n APIError,\n APIConnectionError,\n APIConnectionTimeoutError,\n APIUserAbortError,\n NotFoundError,\n ConflictError,\n RateLimitError,\n BadRequestError,\n AuthenticationError,\n InternalServerError,\n PermissionDeniedError,\n UnprocessableEntityError,\n} from \"./error.js\";\n\nexport default Anthropic;\n", "import type Anthropic from '@anthropic-ai/sdk';\n\nexport const DEFAULT_ANTHROPIC_PARAMS = {\n // model: 'claude-3-5-sonnet-20241022',\n model: 'claude-3-7-sonnet-20250219',\n max_tokens: 8192,\n temperature: 0.1\n};\n\nexport const DEFAULT_ANTHROPIC_OPTIONS = {\n headers: {\n 'x-api-key': null, // Ensuring headers are omitted\n authorization: null\n }\n};\n\nasync function sendPrompt(\n anthropic: Anthropic,\n config: {\n proxyUrl: string;\n headers?: Record<string, string | null | undefined>;\n\n model?: string;\n maxTokens?: number;\n temperature?: number;\n },\n prompt: string,\n signal?: AbortSignal\n): Promise<AsyncGenerator<string, void, unknown>> {\n const customOptions: Partial<typeof DEFAULT_ANTHROPIC_PARAMS> = {};\n if (config.model) customOptions.model = config.model;\n if (config.maxTokens) customOptions.max_tokens = config.maxTokens;\n if (config.temperature) customOptions.temperature = config.temperature;\n\n const msg = await anthropic.messages.create(\n {\n ...DEFAULT_ANTHROPIC_PARAMS,\n ...customOptions,\n stream: true,\n messages: [\n {\n role: 'user',\n content: [\n {\n type: 'text',\n text: prompt\n }\n ]\n }\n ]\n },\n {\n signal,\n ...DEFAULT_ANTHROPIC_OPTIONS,\n headers: {\n ...(DEFAULT_ANTHROPIC_OPTIONS.headers ?? {}),\n ...config.headers\n }\n }\n );\n\n // Return an async generator that yields only the text chunks\n async function* textStreamGenerator() {\n try {\n for await (const chunk of msg) {\n if (\n chunk.type === 'content_block_delta' &&\n chunk.delta.type === 'text_delta'\n ) {\n yield chunk.delta.text;\n }\n }\n } catch (error) {\n // eslint-disable-next-line no-console\n console.error('Stream error:', error);\n throw error; // Re-throw to allow consumer to handle\n }\n }\n\n return textStreamGenerator();\n}\n\nexport default sendPrompt;\n", "import CreativeEditorSDK from '@cesdk/cesdk-js';\nimport {\n type CommonProviderConfiguration,\n mergeQuickActionsConfig\n} from '@imgly/plugin-ai-generation-web';\nimport { TextProvider } from '../types';\nimport Anthropic from '@anthropic-ai/sdk';\nimport sendPrompt from './sendPrompt';\n\ntype AnthropicInput = {\n prompt: string;\n temperature?: number;\n maxTokens?: number;\n\n blockId?: number;\n initialText?: string;\n};\n\ntype AnthropicOutput = {\n kind: 'text';\n text: string;\n};\n\nexport interface AnthropicProviderConfig\n extends CommonProviderConfiguration<AnthropicInput, AnthropicOutput> {\n model?: string;\n}\n\nexport function AnthropicProvider(\n config: AnthropicProviderConfig\n): (context: {\n cesdk: CreativeEditorSDK;\n}) => Promise<TextProvider<AnthropicInput>> {\n return () => {\n let anthropic: Anthropic | null = null;\n\n // Process quick actions configuration\n const defaultQuickActions: any = {\n 'ly.img.improve': true,\n 'ly.img.fix': {}, // Test new empty object syntax\n 'ly.img.shorter': true,\n 'ly.img.longer': {}, // Test new empty object syntax\n 'ly.img.changeTone': true,\n 'ly.img.translate': true,\n 'ly.img.changeTextTo': {} // Test new empty object syntax\n };\n\n const supportedQuickActions = mergeQuickActionsConfig(\n defaultQuickActions,\n config.supportedQuickActions\n );\n\n const provider: TextProvider<AnthropicInput> = {\n kind: 'text',\n id: 'anthropic',\n name: 'Anthropic',\n initialize: async () => {\n anthropic = new Anthropic({\n dangerouslyAllowBrowser: true,\n baseURL: config.proxyUrl,\n // Will be injected by the proxy\n apiKey: null,\n authToken: null\n });\n },\n input: {\n quickActions: {\n supported: supportedQuickActions\n }\n },\n output: {\n middleware: config.middlewares ?? config.middleware ?? [],\n generate: async (\n { prompt, blockId },\n { engine, abortSignal }\n ): Promise<AsyncGenerator<AnthropicOutput, AnthropicOutput>> => {\n if (anthropic == null)\n throw new Error('Anthropic SDK is not initialized');\n\n if (\n blockId != null &&\n engine.block.getType(blockId) !== '//ly.img.ubq/text'\n ) {\n throw new Error(\n 'If a block is provided to this generation, it most be a text block'\n );\n }\n\n if (config.debug)\n // eslint-disable-next-line no-console\n console.log(\n 'Sending prompt to Anthropic:',\n JSON.stringify(prompt, undefined, 2)\n );\n\n const stream = await sendPrompt(\n anthropic,\n {\n proxyUrl: config.proxyUrl,\n headers: config.headers,\n model: config.model ?? 'claude-3-7-sonnet-20250219' // Default\n },\n prompt,\n abortSignal\n );\n\n // Create a new AsyncGenerator that yields AnthropicOutput objects\n async function* outputGenerator(): AsyncGenerator<\n AnthropicOutput,\n AnthropicOutput\n > {\n let inferredText: string = '';\n for await (const chunk of stream) {\n if (abortSignal?.aborted) {\n break;\n }\n inferredText += chunk;\n yield {\n kind: 'text',\n text: inferredText\n };\n }\n // Return the final result\n return {\n kind: 'text',\n text: inferredText\n };\n }\n\n return outputGenerator();\n }\n }\n };\n\n return Promise.resolve(provider);\n };\n}\n", "import { AnthropicProvider } from './AnthropicProvider';\n\nconst Anthropic = {\n AnthropicProvider\n};\n\nexport default Anthropic;\n"],
5
5
  "mappings": "AIgBO,IEfHA,GAAa,OAAO,QAAU,UAAY,QAAU,OAAO,SAAW,QAAU,OAE7EC,GAAQD,GCAXE,GAAW,OAAO,MAAQ,UAAY,MAAQ,KAAK,SAAW,QAAU,KAGxEC,GAAOF,IAAcC,IAAY,SAAS,aAAa,EAAE,EAEtDE,EAAQD,GCLXE,GAASD,EAAK,OAEXE,EAAQD,GCFXE,GAAc,OAAO,UAGrBC,GAAiBD,GAAY,eAO7BE,GAAuBF,GAAY,SAGnCG,GAAiBJ,EAASA,EAAO,YAAc,OASnD,SAASK,GAAUC,EAAO,CACxB,IAAIC,EAAQL,GAAe,KAAKI,EAAOF,EAAc,EACjDI,EAAMF,EAAMF,EAAc,EAE9B,GAAI,CACFE,EAAMF,EAAc,EAAI,OACxB,IAAIK,EAAW,EACjB,MAAY,CAAC,CAEb,IAAIC,EAASP,GAAqB,KAAKG,CAAK,EAC5C,OAAIG,IACEF,EACFD,EAAMF,EAAc,EAAII,EAExB,OAAOF,EAAMF,EAAc,GAGxBM,CACT,CAEA,IAAOC,GAAQN,GC5CXJ,GAAc,OAAO,UAOrBE,GAAuBF,GAAY,SASvC,SAASW,GAAeN,EAAO,CAC7B,OAAOH,GAAqB,KAAKG,CAAK,CACxC,CAEA,IAAOO,GAAQD,GChBXE,GAAU,gBACVC,GAAe,qBAGfX,GAAiBJ,EAASA,EAAO,YAAc,OASnD,SAASgB,GAAWV,EAAO,CACzB,OAAIA,GAAS,KACJA,IAAU,OAAYS,GAAeD,GAEtCV,IA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,GAC3B,cAAiBC,KAASJ,EAAQ,CAChC,GAAID,GAAa,QACf,MAEFI,GAAgBC,EAChB,KAAM,CACJ,KAAM,OACN,KAAMD,CACR,CACF,CAEA,MAAO,CACL,KAAM,OACN,KAAMA,CACR,CACF,CAEA,OAAOD,EAAgB,CACzB,CACF,CACF,EAEA,OAAO,QAAQ,QAAQR,CAAQ,CACjC,CACF,CCtIA,IAAMW,GAAY,CAChB,kBAAAC,EACF,EAEOC,GAAQF",
6
6
  "names": ["freeGlobal", "freeGlobal_default", "freeSelf", "root", "root_default", "Symbol", "Symbol_default", "objectProto", "hasOwnProperty", "nativeObjectToString", "symToStringTag", "getRawTag", "value", "isOwn", "tag", "unmasked", "result", "getRawTag_default", "objectToString", "objectToString_default", "nullTag", "undefinedTag", "baseGetTag", "baseGetTag_default", "isObjectLike", "isObjectLike_default", "isArray", "isObject", "type", "isObject_default", "asyncTag", "funcTag", "genTag", "proxyTag", "isFunction", "isFunction_default", "coreJsData", "coreJsData_default", "maskSrcKey", "uid", "isMasked", "func", "isMasked_default", "funcProto", "funcToString", "toSource", "toSource_default", "reRegExpChar", "reIsHostCtor", "reIsNative", "baseIsNative", "pattern", "baseIsNative_default", "getValue", "object", "key", "getValue_default", "getNative", "getNative_default", "WeakMap", "WeakMap_default", "eq", "other", "eq_default", "MAX_SAFE_INTEGER", "isLength", "isLength_default", "argsTag", "baseIsArguments", "baseIsArguments_default", "propertyIsEnumerable", "isArguments", "freeExports", "freeModule", "moduleExports", "Buffer", "nativeIsBuffer", "arrayTag", "boolTag", "dateTag", "errorTag", "mapTag", "numberTag", "objectTag", "regexpTag", "setTag", "stringTag", "weakMapTag", "arrayBufferTag", "dataViewTag", "float32Tag", "float64Tag", "int8Tag", "int16Tag", "int32Tag", "uint8Tag", "uint8ClampedTag", "uint16Tag", "uint32Tag", "typedArrayTags", "baseIsTypedArray", "baseIsTypedArray_default", "baseUnary", "baseUnary_default", "freeProcess", "nodeUtil", "types", "nodeUtil_default", "nodeIsTypedArray", "isTypedArray", "overArg", "transform", "arg", "overArg_default", "nativeKeys", "nativeCreate", "nativeCreate_default", "hashClear", "hashClear_default", "hashDelete", "hashDelete_default", "HASH_UNDEFINED", "hashGet", "data", "hashGet_default", "hashHas", "hashHas_default", "hashSet", "hashSet_default", "Hash", "entries", "index", "length", "entry", "Hash_default", "listCacheClear", "listCacheClear_default", "assocIndexOf", "array", "assocIndexOf_default", "arrayProto", "splice", "listCacheDelete", "lastIndex", "listCacheDelete_default", "listCacheGet", "listCacheGet_default", "listCacheHas", "listCacheHas_default", "listCacheSet", "listCacheSet_default", "ListCache", "ListCache_default", "Map", "Map_default", "mapCacheClear", "mapCacheClear_default", "isKeyable", "isKeyable_default", "getMapData", "map", "getMapData_default", "mapCacheDelete", "mapCacheDelete_default", "mapCacheGet", "mapCacheGet_default", "mapCacheHas", "mapCacheHas_default", "mapCacheSet", "size", "mapCacheSet_default", "MapCache", "MapCache_default", "stackClear", "stackClear_default", "stackDelete", "stackDelete_default", "stackGet", "stackGet_default", "stackHas", "stackHas_default", "LARGE_ARRAY_SIZE", "stackSet", "pairs", "stackSet_default", "Stack", "DataView", "DataView_default", "Promise", "Promise_default", "Set", "Set_default", "promiseTag", "dataViewCtorString", "mapCtorString", "promiseCtorString", "setCtorString", "weakMapCtorString", "getTag", "Ctor", "ctorString", "Uint8Array", "setCacheAdd", "setCacheAdd_default", "setCacheHas", "setCacheHas_default", "SetCache", "values", "symbolProto", "symbolValueOf", "HEX_COLOR_PATTERN", "HEX_SINGLE_CHAR_COMPONENTS_PATTERN", "HEX_DOUBLE_CHAR_COMPONENTS_PATTERN", "mergeQuickActionsConfig", "providerDefaults", "userConfig", "result", "actionId", "config", "PLUGIN_ICON_SET_ID", "ICON_SPRITE", "VERSION", "auto", "kind", "fetch", "Request", "Response", "Headers", "FormData", "Blob", "File", "ReadableStream", "getMultipartRequestOptions", "getDefaultAgent", "fileFromPath", "isFsReadStream", "setShims", "shims", "options", "MultipartBody", "body", "getRuntime", "manuallyImported", "recommendation", "_fetch", "_Request", "_Response", "_Headers", "error", "form", "opts", "MultipartBody", "url", "value", "kind", "setShims", "getRuntime", "AnthropicError", "APIError", "_APIError", "status", "error", "message", "headers", "msg", "errorResponse", "APIConnectionError", "castToError", "BadRequestError", "AuthenticationError", "PermissionDeniedError", "NotFoundError", "ConflictError", "UnprocessableEntityError", "RateLimitError", "InternalServerError", "APIUserAbortError", "cause", "APIConnectionTimeoutError", "LineDecoder", "_LineDecoder_carriageReturnIndex", "__classPrivateFieldSet", "chunk", "binaryChunk", "newData", "lines", "patternIndex", "findNewlineIndex", "__classPrivateFieldGet", "endIndex", "line", "bytes", "AnthropicError", "buffer", "startIndex", "i", "findDoubleNewlineIndex", "ReadableStreamToAsyncIterable", "stream", "reader", "result", "e", "cancelPromise", "Stream", "_Stream", "iterator", "controller", "response", "consumed", "done", "sse", "_iterSSEMessages", "e", "APIError", "createResponseHeaders", "readableStream", "iterLines", "lineDecoder", "LineDecoder", "iter", "ReadableStreamToAsyncIterable", "chunk", "line", "left", "right", "teeIterator", "queue", "result", "self", "encoder", "ReadableStream", "ctrl", "value", "bytes", "err", "AnthropicError", "sseDecoder", "SSEDecoder", "sseChunk", "iterSSEChunks", "data", "binaryChunk", "newData", "patternIndex", "findDoubleNewlineIndex", "fieldname", "_", "partition", "str", "delimiter", "index", "isResponseLike", "value", "isFileLike", "isBlobLike", "toFile", "value", "name", "options", "isFileLike", "isResponseLike", "blob", "data", "isBlobLike", "File", "bits", "getBytes", "getName", "type", "parts", "isAsyncIterableIterator", "chunk", "propsForError", "p", "getStringFromMaybeBuffer", "x", "isMultipartBody", "body", "defaultParseResponse", "props", "response", "debug", "Stream", "contentType", "json", "_addRequestID", "text", "value", "APIPromise", "_APIPromise", "responsePromise", "parseResponse", "resolve", "transform", "p", "data", "onfulfilled", "onrejected", "onfinally", "APIClient", "baseURL", "maxRetries", "timeout", "httpAgent", "overriddenFetch", "validatePositiveInteger", "fetch", "opts", "getPlatformHeaders", "headers", "customHeaders", "uuid4", "path", "method", "body", "isBlobLike", "Page", "options", "retryCount", "query", "isMultipartBody", "contentLength", "url", "getDefaultAgent", "minAgentTimeout", "reqHeaders", "defaultHeaders", "applyHeadersMut", "kind", "getHeader", "maxTokens", "AnthropicError", "request", "header", "status", "error", "message", "APIError", "remainingRetries", "optionsInput", "retriesRemaining", "req", "APIUserAbortError", "controller", "castToError", "APIConnectionTimeoutError", "APIConnectionError", "responseHeaders", "createResponseHeaders", "retryMessage", "errText", "e", "errJSON", "safeJSON", "errMessage", "PagePromise", "isAbsoluteURL", "defaultQuery", "isEmptyObj", "_", "key", "init", "ms", "signal", "fetchOptions", "socketKeepAliveInterval", "keepAliveTimeout", "socket", "shouldRetryHeader", "timeoutMillis", "retryAfterMillisHeader", "timeoutMs", "retryAfterHeader", "timeoutSeconds", "sleep", "numRetries", "sleepSeconds", "jitter", "VERSION", "AbstractPage", "client", "_AbstractPage_client", "__classPrivateFieldSet", "nextInfo", "nextOptions", "params", "__classPrivateFieldGet", "page", "item", "target", "name", "requestOptionsKeys", "isRequestOptions", "obj", "k", "hasOwn", "getPlatformProperties", "normalizePlatform", "normalizeArch", "browserInfo", "getBrowserInfo", "browserPatterns", "pattern", "match", "major", "minor", "patch", "arch", "platform", "_platformHeaders", "startsWithSchemeRegexp", "n", "err", "readEnv", "env", "isEmptyObj", "obj", "_k", "hasOwn", "key", "applyHeadersMut", "targetHeaders", "newHeaders", "k", "lowerKey", "val", "debug", "action", "args", "uuid4", "c", "r", "isRunningInBrowser", "isHeadersProtocol", "headers", "getHeader", "headers", "header", "lowerCasedHeader", "isHeadersProtocol", "intercapsHeader", "_m", "g1", "g2", "key", "value", "Page", "AbstractPage", "client", "response", "body", "options", "info", "params", "firstId", "cursor", "APIResource", "client", "Models", "APIResource", "modelId", "options", "query", "isRequestOptions", "BetaModelInfosPage", "Page", "JSONLDecoder", "_JSONLDecoder", "iterator", "controller", "lineDecoder", "LineDecoder", "chunk", "line", "response", "AnthropicError", "ReadableStreamToAsyncIterable", "Batches", "APIResource", "params", "options", "betas", "body", "messageBatchId", "isRequestOptions", "query", "BetaMessageBatchesPage", "batch", "AnthropicError", "_", "props", "JSONLDecoder", "Page", "tokenize", "input", "current", "tokens", "char", "value", "danglingQuote", "NUMBERS", "LETTERS", "strip", "lastToken", "lastCharacterOfLastToken", "tokenBeforeTheLastToken", "unstrip", "tail", "token", "item", "generate", "output", "partialParse", "JSON_BUF_PROPERTY", "BetaMessageStream", "_BetaMessageStream", "_BetaMessageStream_currentMessageSnapshot", "_BetaMessageStream_connectedPromise", "_BetaMessageStream_resolveConnectedPromise", "_BetaMessageStream_rejectConnectedPromise", "_BetaMessageStream_endPromise", "_BetaMessageStream_resolveEndPromise", "_BetaMessageStream_rejectEndPromise", "_BetaMessageStream_listeners", "_BetaMessageStream_ended", "_BetaMessageStream_errored", "_BetaMessageStream_aborted", "_BetaMessageStream_catchingPromiseCreated", "_BetaMessageStream_response", "_BetaMessageStream_request_id", "_BetaMessageStream_handleError", "error", "__classPrivateFieldSet", "APIUserAbortError", "AnthropicError", "anthropicError", "resolve", "reject", "__classPrivateFieldGet", "response", "stream", "runner", "messages", "params", "options", "message", "executor", "emit", "signal", "_BetaMessageStream_instances", "_BetaMessageStream_beginRequest", "event", "_BetaMessageStream_addStreamEvent", "_BetaMessageStream_endRequest", "listener", "listeners", "index", "l", "_BetaMessageStream_getFinalMessage", "_BetaMessageStream_getFinalText", "args", "readableStream", "Stream", "textBlocks", "block", "messageSnapshot", "_BetaMessageStream_accumulateMessage", "content", "snapshot", "snapshotContent", "jsonBuf", "partialParse", "pushQueue", "readQueue", "done", "reader", "err", "chunk", "DEPRECATED_MODELS", "Messages", "APIResource", "Batches", "params", "options", "betas", "body", "BetaMessageStream", "BetaMessageBatchesPage", "Beta", "APIResource", "Models", "Messages", "BetaModelInfosPage", "Completions", "APIResource", "body", "options", "Batches", "APIResource", "body", "options", "messageBatchId", "query", "isRequestOptions", "MessageBatchesPage", "batch", "AnthropicError", "_", "props", "JSONLDecoder", "Page", "JSON_BUF_PROPERTY", "MessageStream", "_MessageStream", "_MessageStream_currentMessageSnapshot", "_MessageStream_connectedPromise", "_MessageStream_resolveConnectedPromise", "_MessageStream_rejectConnectedPromise", "_MessageStream_endPromise", "_MessageStream_resolveEndPromise", "_MessageStream_rejectEndPromise", "_MessageStream_listeners", "_MessageStream_ended", "_MessageStream_errored", "_MessageStream_aborted", "_MessageStream_catchingPromiseCreated", "_MessageStream_response", "_MessageStream_request_id", "_MessageStream_handleError", "error", "__classPrivateFieldSet", "APIUserAbortError", "AnthropicError", "anthropicError", "resolve", "reject", "__classPrivateFieldGet", "response", "stream", "runner", "messages", "params", "options", "message", "executor", "emit", "signal", "_MessageStream_instances", "_MessageStream_beginRequest", "event", "_MessageStream_addStreamEvent", "_MessageStream_endRequest", "listener", "listeners", "index", "l", "_MessageStream_getFinalMessage", "_MessageStream_getFinalText", "args", "readableStream", "Stream", "textBlocks", "block", "messageSnapshot", "_MessageStream_accumulateMessage", "content", "snapshot", "snapshotContent", "jsonBuf", "partialParse", "pushQueue", "readQueue", "done", "reader", "err", "chunk", "Messages", "APIResource", "Batches", "body", "options", "DEPRECATED_MODELS", "MessageStream", "MessageBatchesPage", "Models", "APIResource", "modelId", "options", "query", "isRequestOptions", "ModelInfosPage", "Page", "Anthropic", "APIClient", "baseURL", "readEnv", "apiKey", "authToken", "opts", "options", "isRunningInBrowser", "AnthropicError", "Completions", "Messages", "Models", "Beta", "headers", "customHeaders", "apiKeyAuth", "bearerAuth", "isEmptyObj", "_a", "APIError", "APIConnectionError", "APIConnectionTimeoutError", "APIUserAbortError", "NotFoundError", "ConflictError", "RateLimitError", "BadRequestError", "AuthenticationError", "InternalServerError", "PermissionDeniedError", "UnprocessableEntityError", "toFile", "fileFromPath", "ModelInfosPage", "HUMAN_PROMPT", "AI_PROMPT", "sdk_default", "DEFAULT_ANTHROPIC_PARAMS", "DEFAULT_ANTHROPIC_OPTIONS", "sendPrompt", "anthropic", "config", "prompt", "signal", "customOptions", "msg", "textStreamGenerator", "chunk", "error", "sendPrompt_default", "AnthropicProvider", "config", "anthropic", "supportedQuickActions", "Co", "provider", "sdk_default", "prompt", "blockId", "engine", "abortSignal", "stream", "sendPrompt_default", "outputGenerator", "inferredText", "chunk", "Anthropic", "AnthropicProvider", "anthropic_default"]
7
7
  }