@intlayer/ai 8.1.2 → 8.1.3
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.
- package/dist/cjs/_virtual/_utils_asset.cjs +2 -97
- package/dist/cjs/aiSdk.cjs +1 -202
- package/dist/cjs/aiSdk.cjs.map +1 -1
- package/dist/cjs/auditDictionaryMetadata/index.cjs +2 -53
- package/dist/cjs/auditDictionaryMetadata/index.cjs.map +1 -1
- package/dist/cjs/customQuery.cjs +1 -24
- package/dist/cjs/customQuery.cjs.map +1 -1
- package/dist/cjs/index.cjs +1 -27
- package/dist/cjs/translateJSON/index.cjs +5 -117
- package/dist/cjs/translateJSON/index.cjs.map +1 -1
- package/dist/cjs/utils/extractJSON.cjs +1 -61
- package/dist/cjs/utils/extractJSON.cjs.map +1 -1
- package/dist/esm/_virtual/_utils_asset.mjs +2 -97
- package/dist/esm/aiSdk.mjs +1 -200
- package/dist/esm/aiSdk.mjs.map +1 -1
- package/dist/esm/auditDictionaryMetadata/index.mjs +2 -51
- package/dist/esm/auditDictionaryMetadata/index.mjs.map +1 -1
- package/dist/esm/customQuery.mjs +1 -22
- package/dist/esm/customQuery.mjs.map +1 -1
- package/dist/esm/index.mjs +1 -8
- package/dist/esm/translateJSON/index.mjs +5 -116
- package/dist/esm/translateJSON/index.mjs.map +1 -1
- package/dist/esm/utils/extractJSON.mjs +1 -59
- package/dist/esm/utils/extractJSON.mjs.map +1 -1
- package/package.json +5 -5
package/dist/esm/aiSdk.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aiSdk.mjs","names":[],"sources":["../../src/aiSdk.ts"],"sourcesContent":["import type { AlibabaProvider, createAlibaba } from '@ai-sdk/alibaba';\nimport type {\n AmazonBedrockProvider,\n createAmazonBedrock,\n} from '@ai-sdk/amazon-bedrock';\nimport type { AnthropicProvider, createAnthropic } from '@ai-sdk/anthropic';\nimport type { createDeepSeek, DeepSeekProvider } from '@ai-sdk/deepseek';\nimport type { createFireworks, FireworksProvider } from '@ai-sdk/fireworks';\nimport type {\n createGoogleGenerativeAI,\n GoogleGenerativeAIProvider,\n} from '@ai-sdk/google';\nimport type {\n createVertex,\n GoogleVertexProvider as VertexProvider,\n} from '@ai-sdk/google-vertex';\nimport type { createGroq, GroqProvider } from '@ai-sdk/groq';\nimport type {\n createHuggingFace,\n HuggingFaceProvider,\n} from '@ai-sdk/huggingface';\nimport type { createMistral, MistralProvider } from '@ai-sdk/mistral';\nimport type { createOpenAI, OpenAIProvider } from '@ai-sdk/openai';\nimport type { createTogetherAI, TogetherAIProvider } from '@ai-sdk/togetherai';\nimport { ANSIColors, colorize, logger, x } from '@intlayer/config';\nimport { AiProviders } from '@intlayer/types';\nimport type {\n createOpenRouter,\n OpenRouterProvider,\n} from '@openrouter/ai-sdk-provider';\nimport type {\n AssistantModelMessage,\n generateText,\n SystemModelMessage,\n ToolModelMessage,\n UserModelMessage,\n} from 'ai';\n\nexport { AiProviders as AIProvider };\n\ntype AnthropicModel = Parameters<AnthropicProvider>[0];\ntype DeepSeekModel = Parameters<DeepSeekProvider>[0];\ntype MistralModel = Parameters<MistralProvider>[0];\ntype OpenAIModel = Parameters<OpenAIProvider>[0];\ntype OpenRouterModel = Parameters<OpenRouterProvider>[0];\ntype GoogleModel = Parameters<GoogleGenerativeAIProvider>[0];\ntype VertexModel = Parameters<VertexProvider>[0];\ntype AlibabaModel = Parameters<AlibabaProvider>[0];\ntype AmazonBedrockModel = Parameters<AmazonBedrockProvider>[0];\ntype FireworksModel = Parameters<FireworksProvider>[0];\ntype GroqModel = Parameters<GroqProvider>[0];\ntype HuggingFaceModel = Parameters<HuggingFaceProvider>[0];\ntype TogetherAIModel = Parameters<TogetherAIProvider>[0];\n\nexport type OpenAIProviderOptions = Parameters<typeof createOpenAI>[0];\nexport type AnthropicProviderOptions = Parameters<typeof createAnthropic>[0];\nexport type MistralProviderOptions = Parameters<typeof createMistral>[0];\nexport type DeepSeekProviderOptions = Parameters<typeof createDeepSeek>[0];\nexport type GoogleProviderOptions = Parameters<\n typeof createGoogleGenerativeAI\n>[0];\nexport type VertexProviderOptions = Parameters<typeof createVertex>[0];\nexport type OpenRouterProviderOptions = Parameters<typeof createOpenRouter>[0];\nexport type AlibabaProviderOptions = Parameters<typeof createAlibaba>[0];\nexport type FireworksProviderOptions = Parameters<typeof createFireworks>[0];\nexport type GroqProviderOptions = Parameters<typeof createGroq>[0];\nexport type HuggingFaceProviderOptions = Parameters<\n typeof createHuggingFace\n>[0];\nexport type AmazonBedrockProviderOptions = Parameters<\n typeof createAmazonBedrock\n>[0];\nexport type TogetherAIProviderOptions = Parameters<typeof createTogetherAI>[0];\n\nexport type Messages = (\n | SystemModelMessage\n | UserModelMessage\n | AssistantModelMessage\n | ToolModelMessage\n)[];\n\n/**\n * Supported AI models\n */\nexport type Model =\n | AnthropicModel\n | DeepSeekModel\n | MistralModel\n | OpenAIModel\n | OpenRouterModel\n | GoogleModel\n | VertexModel\n | AlibabaModel\n | AmazonBedrockModel\n | FireworksModel\n | GroqModel\n | HuggingFaceModel\n | TogetherAIModel\n | (string & {});\n\n/**\n * Supported AI SDK providers\n */\n\nexport type ReasoningEffort = 'minimal' | 'low' | 'medium' | 'high' | 'none';\n\n/**\n * Common options for all AI providers\n */\ntype CommonAIOptions = {\n model?: Model;\n temperature?: number;\n baseURL?: string;\n apiKey?: string;\n applicationContext?: string;\n dataSerialization?: 'json' | 'toon';\n};\n\nexport type AIOptions = (\n | ({\n provider: AiProviders.OPENAI | `${AiProviders.OPENAI}`;\n } & OpenAIProviderOptions)\n | ({\n provider: AiProviders.ANTHROPIC | `${AiProviders.ANTHROPIC}`;\n } & AnthropicProviderOptions)\n | ({\n provider: AiProviders.MISTRAL | `${AiProviders.MISTRAL}`;\n } & MistralProviderOptions)\n | ({\n provider: AiProviders.DEEPSEEK | `${AiProviders.DEEPSEEK}`;\n } & DeepSeekProviderOptions)\n | ({\n provider: AiProviders.GEMINI | `${AiProviders.GEMINI}`;\n } & GoogleProviderOptions)\n | ({\n provider:\n | AiProviders.GOOGLEGENERATIVEAI\n | `${AiProviders.GOOGLEGENERATIVEAI}`;\n } & GoogleProviderOptions)\n | ({\n provider: AiProviders.OLLAMA | `${AiProviders.OLLAMA}`;\n } & OpenAIProviderOptions)\n | ({\n provider: AiProviders.OPENROUTER | `${AiProviders.OPENROUTER}`;\n } & OpenRouterProviderOptions)\n | ({\n provider: AiProviders.ALIBABA | `${AiProviders.ALIBABA}`;\n } & AlibabaProviderOptions)\n | ({\n provider: AiProviders.FIREWORKS | `${AiProviders.FIREWORKS}`;\n } & FireworksProviderOptions)\n | ({\n provider: AiProviders.GROQ | `${AiProviders.GROQ}`;\n } & GroqProviderOptions)\n | ({\n provider: AiProviders.HUGGINGFACE | `${AiProviders.HUGGINGFACE}`;\n } & HuggingFaceProviderOptions)\n | ({\n provider: AiProviders.BEDROCK | `${AiProviders.BEDROCK}`;\n } & AmazonBedrockProviderOptions)\n | ({\n provider: AiProviders.GOOGLEVERTEX | `${AiProviders.GOOGLEVERTEX}`;\n } & VertexProviderOptions)\n | ({\n provider: AiProviders.TOGETHERAI | `${AiProviders.TOGETHERAI}`;\n } & TogetherAIProviderOptions)\n | ({ provider?: undefined } & OpenAIProviderOptions)\n) &\n CommonAIOptions;\n\n// Define the structure of messages used in chat completions\nexport type ChatCompletionRequestMessage = {\n role: 'system' | 'user' | 'assistant'; // The role of the message sender\n content: string; // The text content of the message\n timestamp?: Date; // The timestamp of the message\n};\n\ntype AccessType = 'apiKey' | 'registered_user' | 'premium_user' | 'public';\n\nconst getAPIKey = (\n accessType: AccessType[],\n aiOptions?: AIOptions,\n isAuthenticated: boolean = false\n) => {\n const defaultApiKey = process.env.OPENAI_API_KEY;\n\n if (accessType.includes('public')) {\n return aiOptions?.apiKey ?? defaultApiKey;\n }\n\n if (accessType.includes('apiKey') && aiOptions?.apiKey) {\n return aiOptions?.apiKey;\n }\n\n if (accessType.includes('registered_user') && isAuthenticated) {\n return aiOptions?.apiKey ?? defaultApiKey;\n }\n\n // TODO: Implement premium user access\n if (accessType.includes('premium_user') && isAuthenticated) {\n return aiOptions?.apiKey ?? defaultApiKey;\n }\n\n return undefined;\n};\n\nconst getModelName = (\n provider: AiProviders,\n userApiKey: string | undefined,\n userModel?: Model,\n defaultModel: Model = 'gpt-5-mini'\n): Model => {\n // If the user uses their own API key, allow custom model selection\n if (userApiKey || provider === AiProviders.OLLAMA) {\n if (provider === AiProviders.OPENAI) {\n return userModel ?? defaultModel;\n }\n\n if (userModel) {\n return userModel;\n }\n\n switch (provider) {\n default:\n return '-';\n }\n }\n\n // Guard: Prevent custom model usage without a user API key\n if (userModel || provider) {\n throw new Error(\n 'The user should use his own API key to use a custom model'\n );\n }\n\n return defaultModel;\n};\n\nconst getLanguageModel = async (\n aiOptions: AIOptions,\n apiKey: string | undefined,\n defaultModel?: Model\n) => {\n const loadModule = async <T>(packageName: string): Promise<T> => {\n try {\n return (await import(packageName)) as T;\n } catch {\n logger(\n `${x} The package \"${colorize(packageName, ANSIColors.GREEN)}\" is required to use this AI provider. Please install it using: ${colorize(`npm install ${packageName}`, ANSIColors.GREY_DARK)}`,\n {\n level: 'error',\n }\n );\n process.exit();\n }\n };\n\n const provider = aiOptions.provider ?? AiProviders.OPENAI;\n const selectedModel = getModelName(\n provider as AiProviders,\n apiKey,\n aiOptions.model,\n defaultModel\n );\n\n const baseURL = aiOptions.baseURL;\n\n switch (provider) {\n case AiProviders.OPENAI: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createOpenAI } =\n await loadModule<typeof import('@ai-sdk/openai')>('@ai-sdk/openai');\n\n return createOpenAI({\n apiKey,\n baseURL,\n ...otherOptions,\n })(selectedModel);\n }\n\n case AiProviders.ANTHROPIC: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createAnthropic } =\n await loadModule<typeof import('@ai-sdk/anthropic')>(\n '@ai-sdk/anthropic'\n );\n\n return createAnthropic({\n apiKey,\n baseURL,\n ...otherOptions,\n })(selectedModel);\n }\n\n case AiProviders.MISTRAL: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createMistral } =\n await loadModule<typeof import('@ai-sdk/mistral')>('@ai-sdk/mistral');\n\n return createMistral({\n apiKey,\n baseURL,\n ...otherOptions,\n })(selectedModel);\n }\n\n case AiProviders.DEEPSEEK: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createDeepSeek } =\n await loadModule<typeof import('@ai-sdk/deepseek')>('@ai-sdk/deepseek');\n\n return createDeepSeek({\n apiKey,\n baseURL,\n ...otherOptions,\n })(selectedModel);\n }\n\n case AiProviders.GEMINI: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createGoogleGenerativeAI } =\n await loadModule<typeof import('@ai-sdk/google')>('@ai-sdk/google');\n\n return createGoogleGenerativeAI({\n apiKey,\n baseURL,\n ...otherOptions,\n })(selectedModel);\n }\n\n case AiProviders.GOOGLEVERTEX: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createVertex } = await loadModule<\n typeof import('@ai-sdk/google-vertex')\n >('@ai-sdk/google-vertex');\n\n return createVertex({\n apiKey,\n baseURL,\n ...otherOptions,\n })(selectedModel as string);\n }\n\n case AiProviders.GOOGLEGENERATIVEAI: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createGoogleGenerativeAI } =\n await loadModule<typeof import('@ai-sdk/google')>('@ai-sdk/google');\n\n return createGoogleGenerativeAI({\n apiKey,\n baseURL,\n ...otherOptions,\n })(selectedModel as string);\n }\n\n case AiProviders.OLLAMA: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createOpenAI } =\n await loadModule<typeof import('@ai-sdk/openai')>('@ai-sdk/openai');\n\n // Ollama compatible mode:\n const ollama = createOpenAI({\n baseURL: baseURL ?? 'http://localhost:11434/v1',\n apiKey: apiKey ?? 'ollama', // Required but unused by Ollama\n ...otherOptions,\n });\n\n return ollama.chat(selectedModel);\n }\n\n case AiProviders.OPENROUTER: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createOpenRouter } = await loadModule<\n typeof import('@openrouter/ai-sdk-provider')\n >('@openrouter/ai-sdk-provider');\n\n const openrouter = createOpenRouter({\n apiKey,\n baseURL,\n ...otherOptions,\n });\n\n return openrouter(selectedModel as string);\n }\n\n case AiProviders.ALIBABA: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createAlibaba } =\n await loadModule<typeof import('@ai-sdk/alibaba')>('@ai-sdk/alibaba');\n\n const alibaba = createAlibaba({\n apiKey,\n baseURL,\n ...otherOptions,\n });\n\n return alibaba(selectedModel as string);\n }\n\n case AiProviders.FIREWORKS: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createFireworks } =\n await loadModule<typeof import('@ai-sdk/fireworks')>(\n '@ai-sdk/fireworks'\n );\n\n const fireworks = createFireworks({\n apiKey,\n baseURL,\n ...otherOptions,\n });\n\n return fireworks(selectedModel as string);\n }\n\n case AiProviders.GROQ: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createGroq } =\n await loadModule<typeof import('@ai-sdk/groq')>('@ai-sdk/groq');\n\n const groq = createGroq({\n apiKey,\n baseURL,\n ...otherOptions,\n });\n\n return groq(selectedModel as string);\n }\n\n case AiProviders.HUGGINGFACE: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createHuggingFace } = await loadModule<\n typeof import('@ai-sdk/huggingface')\n >('@ai-sdk/huggingface');\n\n const huggingface = createHuggingFace({\n apiKey,\n baseURL,\n ...otherOptions,\n });\n\n return huggingface(selectedModel as string);\n }\n\n case AiProviders.BEDROCK: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createAmazonBedrock } = await loadModule<\n typeof import('@ai-sdk/amazon-bedrock')\n >('@ai-sdk/amazon-bedrock');\n\n const bedrock = createAmazonBedrock({\n accessKeyId: apiKey,\n baseURL,\n ...otherOptions,\n });\n\n return bedrock(selectedModel as string);\n }\n\n case AiProviders.TOGETHERAI: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createTogetherAI } =\n await loadModule<typeof import('@ai-sdk/togetherai')>(\n '@ai-sdk/togetherai'\n );\n\n const togetherai = createTogetherAI({\n apiKey,\n baseURL,\n ...otherOptions,\n });\n\n return togetherai(selectedModel as string);\n }\n\n default: {\n throw new Error(`Provider ${provider} not supported`);\n }\n }\n};\n\nexport type AIConfig = Omit<Parameters<typeof generateText>[0], 'prompt'> & {\n reasoningEffort?: ReasoningEffort;\n textVerbosity?: 'low' | 'medium' | 'high';\n dataSerialization?: 'json' | 'toon';\n};\n\nconst DEFAULT_PROVIDER: AiProviders = AiProviders.OPENAI as AiProviders;\n\nexport type AIConfigOptions = {\n userOptions?: AIOptions;\n projectOptions?: AIOptions;\n defaultOptions?: AIOptions;\n accessType?: AccessType[];\n};\n\n/**\n * Get AI model configuration based on the selected provider and options\n * This function handles the configuration for different AI providers\n *\n * @param options Configuration options including provider, API keys, models and temperature\n * @returns Configured AI model ready to use with generateText\n */\nexport const getAIConfig = async (\n options: AIConfigOptions,\n isAuthenticated: boolean = false\n): Promise<AIConfig> => {\n const {\n userOptions,\n projectOptions,\n defaultOptions,\n accessType = ['registered_user'],\n } = options;\n\n const aiOptions = {\n provider: DEFAULT_PROVIDER,\n ...defaultOptions,\n ...projectOptions,\n ...userOptions,\n } as AIOptions;\n\n const apiKey = getAPIKey(accessType, aiOptions, isAuthenticated);\n\n // Check if API key is provided\n if (!apiKey && aiOptions.provider !== AiProviders.OLLAMA) {\n throw new Error(`API key for ${aiOptions.provider} is missing`);\n }\n\n const languageModel = await getLanguageModel(\n aiOptions,\n apiKey,\n defaultOptions?.model\n );\n\n return {\n model: languageModel,\n temperature: aiOptions.temperature,\n dataSerialization: aiOptions.dataSerialization,\n };\n};\n"],"mappings":";;;;AAmLA,MAAM,aACJ,YACA,WACA,kBAA2B,UACxB;CACH,MAAM,gBAAgB,QAAQ,IAAI;AAElC,KAAI,WAAW,SAAS,SAAS,CAC/B,QAAO,WAAW,UAAU;AAG9B,KAAI,WAAW,SAAS,SAAS,IAAI,WAAW,OAC9C,QAAO,WAAW;AAGpB,KAAI,WAAW,SAAS,kBAAkB,IAAI,gBAC5C,QAAO,WAAW,UAAU;AAI9B,KAAI,WAAW,SAAS,eAAe,IAAI,gBACzC,QAAO,WAAW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;AAQ3B,UANgB,oBAAoB;IAClC,aAAa;IACb;IACA,GAAG;IACJ,CAAC,CAEa,cAAwB;;EAGzC,KAAK,YAAY,YAAY;GAC3B,MAAM,EACJ,UACA,OACA,aACA,oBACA,mBACA,QAAQ,SACR,SAAS,UACT,GAAG,iBACD;GAEJ,MAAM,EAAE,qBACN,MAAM,WACJ,qBACD;AAQH,UANmB,iBAAiB;IAClC;IACA;IACA,GAAG;IACJ,CAAC,CAEgB,cAAwB;;EAG5C,QACE,OAAM,IAAI,MAAM,YAAY,SAAS,gBAAgB;;;AAW3D,MAAM,mBAAgC,YAAY;;;;;;;;AAgBlD,MAAa,cAAc,OACzB,SACA,kBAA2B,UACL;CACtB,MAAM,EACJ,aACA,gBACA,gBACA,aAAa,CAAC,kBAAkB,KAC9B;CAEJ,MAAM,YAAY;EAChB,UAAU;EACV,GAAG;EACH,GAAG;EACH,GAAG;EACJ;CAED,MAAM,SAAS,UAAU,YAAY,WAAW,gBAAgB;AAGhE,KAAI,CAAC,UAAU,UAAU,aAAa,YAAY,OAChD,OAAM,IAAI,MAAM,eAAe,UAAU,SAAS,aAAa;AASjE,QAAO;EACL,OAPoB,MAAM,iBAC1B,WACA,QACA,gBAAgB,MACjB;EAIC,aAAa,UAAU;EACvB,mBAAmB,UAAU;EAC9B"}
|
|
1
|
+
{"version":3,"file":"aiSdk.mjs","names":[],"sources":["../../src/aiSdk.ts"],"sourcesContent":["import type { AlibabaProvider, createAlibaba } from '@ai-sdk/alibaba';\nimport type {\n AmazonBedrockProvider,\n createAmazonBedrock,\n} from '@ai-sdk/amazon-bedrock';\nimport type { AnthropicProvider, createAnthropic } from '@ai-sdk/anthropic';\nimport type { createDeepSeek, DeepSeekProvider } from '@ai-sdk/deepseek';\nimport type { createFireworks, FireworksProvider } from '@ai-sdk/fireworks';\nimport type {\n createGoogleGenerativeAI,\n GoogleGenerativeAIProvider,\n} from '@ai-sdk/google';\nimport type {\n createVertex,\n GoogleVertexProvider as VertexProvider,\n} from '@ai-sdk/google-vertex';\nimport type { createGroq, GroqProvider } from '@ai-sdk/groq';\nimport type {\n createHuggingFace,\n HuggingFaceProvider,\n} from '@ai-sdk/huggingface';\nimport type { createMistral, MistralProvider } from '@ai-sdk/mistral';\nimport type { createOpenAI, OpenAIProvider } from '@ai-sdk/openai';\nimport type { createTogetherAI, TogetherAIProvider } from '@ai-sdk/togetherai';\nimport { ANSIColors, colorize, logger, x } from '@intlayer/config/logger';\nimport { AiProviders } from '@intlayer/types';\nimport type {\n createOpenRouter,\n OpenRouterProvider,\n} from '@openrouter/ai-sdk-provider';\nimport type {\n AssistantModelMessage,\n generateText,\n SystemModelMessage,\n ToolModelMessage,\n UserModelMessage,\n} from 'ai';\n\nexport { AiProviders as AIProvider };\n\ntype AnthropicModel = Parameters<AnthropicProvider>[0];\ntype DeepSeekModel = Parameters<DeepSeekProvider>[0];\ntype MistralModel = Parameters<MistralProvider>[0];\ntype OpenAIModel = Parameters<OpenAIProvider>[0];\ntype OpenRouterModel = Parameters<OpenRouterProvider>[0];\ntype GoogleModel = Parameters<GoogleGenerativeAIProvider>[0];\ntype VertexModel = Parameters<VertexProvider>[0];\ntype AlibabaModel = Parameters<AlibabaProvider>[0];\ntype AmazonBedrockModel = Parameters<AmazonBedrockProvider>[0];\ntype FireworksModel = Parameters<FireworksProvider>[0];\ntype GroqModel = Parameters<GroqProvider>[0];\ntype HuggingFaceModel = Parameters<HuggingFaceProvider>[0];\ntype TogetherAIModel = Parameters<TogetherAIProvider>[0];\n\nexport type OpenAIProviderOptions = Parameters<typeof createOpenAI>[0];\nexport type AnthropicProviderOptions = Parameters<typeof createAnthropic>[0];\nexport type MistralProviderOptions = Parameters<typeof createMistral>[0];\nexport type DeepSeekProviderOptions = Parameters<typeof createDeepSeek>[0];\nexport type GoogleProviderOptions = Parameters<\n typeof createGoogleGenerativeAI\n>[0];\nexport type VertexProviderOptions = Parameters<typeof createVertex>[0];\nexport type OpenRouterProviderOptions = Parameters<typeof createOpenRouter>[0];\nexport type AlibabaProviderOptions = Parameters<typeof createAlibaba>[0];\nexport type FireworksProviderOptions = Parameters<typeof createFireworks>[0];\nexport type GroqProviderOptions = Parameters<typeof createGroq>[0];\nexport type HuggingFaceProviderOptions = Parameters<\n typeof createHuggingFace\n>[0];\nexport type AmazonBedrockProviderOptions = Parameters<\n typeof createAmazonBedrock\n>[0];\nexport type TogetherAIProviderOptions = Parameters<typeof createTogetherAI>[0];\n\nexport type Messages = (\n | SystemModelMessage\n | UserModelMessage\n | AssistantModelMessage\n | ToolModelMessage\n)[];\n\n/**\n * Supported AI models\n */\nexport type Model =\n | AnthropicModel\n | DeepSeekModel\n | MistralModel\n | OpenAIModel\n | OpenRouterModel\n | GoogleModel\n | VertexModel\n | AlibabaModel\n | AmazonBedrockModel\n | FireworksModel\n | GroqModel\n | HuggingFaceModel\n | TogetherAIModel\n | (string & {});\n\n/**\n * Supported AI SDK providers\n */\n\nexport type ReasoningEffort = 'minimal' | 'low' | 'medium' | 'high' | 'none';\n\n/**\n * Common options for all AI providers\n */\ntype CommonAIOptions = {\n model?: Model;\n temperature?: number;\n baseURL?: string;\n apiKey?: string;\n applicationContext?: string;\n dataSerialization?: 'json' | 'toon';\n};\n\nexport type AIOptions = (\n | ({\n provider: AiProviders.OPENAI | `${AiProviders.OPENAI}`;\n } & OpenAIProviderOptions)\n | ({\n provider: AiProviders.ANTHROPIC | `${AiProviders.ANTHROPIC}`;\n } & AnthropicProviderOptions)\n | ({\n provider: AiProviders.MISTRAL | `${AiProviders.MISTRAL}`;\n } & MistralProviderOptions)\n | ({\n provider: AiProviders.DEEPSEEK | `${AiProviders.DEEPSEEK}`;\n } & DeepSeekProviderOptions)\n | ({\n provider: AiProviders.GEMINI | `${AiProviders.GEMINI}`;\n } & GoogleProviderOptions)\n | ({\n provider:\n | AiProviders.GOOGLEGENERATIVEAI\n | `${AiProviders.GOOGLEGENERATIVEAI}`;\n } & GoogleProviderOptions)\n | ({\n provider: AiProviders.OLLAMA | `${AiProviders.OLLAMA}`;\n } & OpenAIProviderOptions)\n | ({\n provider: AiProviders.OPENROUTER | `${AiProviders.OPENROUTER}`;\n } & OpenRouterProviderOptions)\n | ({\n provider: AiProviders.ALIBABA | `${AiProviders.ALIBABA}`;\n } & AlibabaProviderOptions)\n | ({\n provider: AiProviders.FIREWORKS | `${AiProviders.FIREWORKS}`;\n } & FireworksProviderOptions)\n | ({\n provider: AiProviders.GROQ | `${AiProviders.GROQ}`;\n } & GroqProviderOptions)\n | ({\n provider: AiProviders.HUGGINGFACE | `${AiProviders.HUGGINGFACE}`;\n } & HuggingFaceProviderOptions)\n | ({\n provider: AiProviders.BEDROCK | `${AiProviders.BEDROCK}`;\n } & AmazonBedrockProviderOptions)\n | ({\n provider: AiProviders.GOOGLEVERTEX | `${AiProviders.GOOGLEVERTEX}`;\n } & VertexProviderOptions)\n | ({\n provider: AiProviders.TOGETHERAI | `${AiProviders.TOGETHERAI}`;\n } & TogetherAIProviderOptions)\n | ({ provider?: undefined } & OpenAIProviderOptions)\n) &\n CommonAIOptions;\n\n// Define the structure of messages used in chat completions\nexport type ChatCompletionRequestMessage = {\n role: 'system' | 'user' | 'assistant'; // The role of the message sender\n content: string; // The text content of the message\n timestamp?: Date; // The timestamp of the message\n};\n\ntype AccessType = 'apiKey' | 'registered_user' | 'premium_user' | 'public';\n\nconst getAPIKey = (\n accessType: AccessType[],\n aiOptions?: AIOptions,\n isAuthenticated: boolean = false\n) => {\n const defaultApiKey = process.env.OPENAI_API_KEY;\n\n if (accessType.includes('public')) {\n return aiOptions?.apiKey ?? defaultApiKey;\n }\n\n if (accessType.includes('apiKey') && aiOptions?.apiKey) {\n return aiOptions?.apiKey;\n }\n\n if (accessType.includes('registered_user') && isAuthenticated) {\n return aiOptions?.apiKey ?? defaultApiKey;\n }\n\n // TODO: Implement premium user access\n if (accessType.includes('premium_user') && isAuthenticated) {\n return aiOptions?.apiKey ?? defaultApiKey;\n }\n\n return undefined;\n};\n\nconst getModelName = (\n provider: AiProviders,\n userApiKey: string | undefined,\n userModel?: Model,\n defaultModel: Model = 'gpt-5-mini'\n): Model => {\n // If the user uses their own API key, allow custom model selection\n if (userApiKey || provider === AiProviders.OLLAMA) {\n if (provider === AiProviders.OPENAI) {\n return userModel ?? defaultModel;\n }\n\n if (userModel) {\n return userModel;\n }\n\n switch (provider) {\n default:\n return '-';\n }\n }\n\n // Guard: Prevent custom model usage without a user API key\n if (userModel || provider) {\n throw new Error(\n 'The user should use his own API key to use a custom model'\n );\n }\n\n return defaultModel;\n};\n\nconst getLanguageModel = async (\n aiOptions: AIOptions,\n apiKey: string | undefined,\n defaultModel?: Model\n) => {\n const loadModule = async <T>(packageName: string): Promise<T> => {\n try {\n return (await import(packageName)) as T;\n } catch {\n logger(\n `${x} The package \"${colorize(packageName, ANSIColors.GREEN)}\" is required to use this AI provider. Please install it using: ${colorize(`npm install ${packageName}`, ANSIColors.GREY_DARK)}`,\n {\n level: 'error',\n }\n );\n process.exit();\n }\n };\n\n const provider = aiOptions.provider ?? AiProviders.OPENAI;\n const selectedModel = getModelName(\n provider as AiProviders,\n apiKey,\n aiOptions.model,\n defaultModel\n );\n\n const baseURL = aiOptions.baseURL;\n\n switch (provider) {\n case AiProviders.OPENAI: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createOpenAI } =\n await loadModule<typeof import('@ai-sdk/openai')>('@ai-sdk/openai');\n\n return createOpenAI({\n apiKey,\n baseURL,\n ...otherOptions,\n })(selectedModel);\n }\n\n case AiProviders.ANTHROPIC: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createAnthropic } =\n await loadModule<typeof import('@ai-sdk/anthropic')>(\n '@ai-sdk/anthropic'\n );\n\n return createAnthropic({\n apiKey,\n baseURL,\n ...otherOptions,\n })(selectedModel);\n }\n\n case AiProviders.MISTRAL: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createMistral } =\n await loadModule<typeof import('@ai-sdk/mistral')>('@ai-sdk/mistral');\n\n return createMistral({\n apiKey,\n baseURL,\n ...otherOptions,\n })(selectedModel);\n }\n\n case AiProviders.DEEPSEEK: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createDeepSeek } =\n await loadModule<typeof import('@ai-sdk/deepseek')>('@ai-sdk/deepseek');\n\n return createDeepSeek({\n apiKey,\n baseURL,\n ...otherOptions,\n })(selectedModel);\n }\n\n case AiProviders.GEMINI: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createGoogleGenerativeAI } =\n await loadModule<typeof import('@ai-sdk/google')>('@ai-sdk/google');\n\n return createGoogleGenerativeAI({\n apiKey,\n baseURL,\n ...otherOptions,\n })(selectedModel);\n }\n\n case AiProviders.GOOGLEVERTEX: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createVertex } = await loadModule<\n typeof import('@ai-sdk/google-vertex')\n >('@ai-sdk/google-vertex');\n\n return createVertex({\n apiKey,\n baseURL,\n ...otherOptions,\n })(selectedModel as string);\n }\n\n case AiProviders.GOOGLEGENERATIVEAI: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createGoogleGenerativeAI } =\n await loadModule<typeof import('@ai-sdk/google')>('@ai-sdk/google');\n\n return createGoogleGenerativeAI({\n apiKey,\n baseURL,\n ...otherOptions,\n })(selectedModel as string);\n }\n\n case AiProviders.OLLAMA: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createOpenAI } =\n await loadModule<typeof import('@ai-sdk/openai')>('@ai-sdk/openai');\n\n // Ollama compatible mode:\n const ollama = createOpenAI({\n baseURL: baseURL ?? 'http://localhost:11434/v1',\n apiKey: apiKey ?? 'ollama', // Required but unused by Ollama\n ...otherOptions,\n });\n\n return ollama.chat(selectedModel);\n }\n\n case AiProviders.OPENROUTER: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createOpenRouter } = await loadModule<\n typeof import('@openrouter/ai-sdk-provider')\n >('@openrouter/ai-sdk-provider');\n\n const openrouter = createOpenRouter({\n apiKey,\n baseURL,\n ...otherOptions,\n });\n\n return openrouter(selectedModel as string);\n }\n\n case AiProviders.ALIBABA: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createAlibaba } =\n await loadModule<typeof import('@ai-sdk/alibaba')>('@ai-sdk/alibaba');\n\n const alibaba = createAlibaba({\n apiKey,\n baseURL,\n ...otherOptions,\n });\n\n return alibaba(selectedModel as string);\n }\n\n case AiProviders.FIREWORKS: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createFireworks } =\n await loadModule<typeof import('@ai-sdk/fireworks')>(\n '@ai-sdk/fireworks'\n );\n\n const fireworks = createFireworks({\n apiKey,\n baseURL,\n ...otherOptions,\n });\n\n return fireworks(selectedModel as string);\n }\n\n case AiProviders.GROQ: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createGroq } =\n await loadModule<typeof import('@ai-sdk/groq')>('@ai-sdk/groq');\n\n const groq = createGroq({\n apiKey,\n baseURL,\n ...otherOptions,\n });\n\n return groq(selectedModel as string);\n }\n\n case AiProviders.HUGGINGFACE: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createHuggingFace } = await loadModule<\n typeof import('@ai-sdk/huggingface')\n >('@ai-sdk/huggingface');\n\n const huggingface = createHuggingFace({\n apiKey,\n baseURL,\n ...otherOptions,\n });\n\n return huggingface(selectedModel as string);\n }\n\n case AiProviders.BEDROCK: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createAmazonBedrock } = await loadModule<\n typeof import('@ai-sdk/amazon-bedrock')\n >('@ai-sdk/amazon-bedrock');\n\n const bedrock = createAmazonBedrock({\n accessKeyId: apiKey,\n baseURL,\n ...otherOptions,\n });\n\n return bedrock(selectedModel as string);\n }\n\n case AiProviders.TOGETHERAI: {\n const {\n provider,\n model,\n temperature,\n applicationContext,\n dataSerialization,\n apiKey: _apiKey,\n baseURL: _baseURL,\n ...otherOptions\n } = aiOptions as any;\n\n const { createTogetherAI } =\n await loadModule<typeof import('@ai-sdk/togetherai')>(\n '@ai-sdk/togetherai'\n );\n\n const togetherai = createTogetherAI({\n apiKey,\n baseURL,\n ...otherOptions,\n });\n\n return togetherai(selectedModel as string);\n }\n\n default: {\n throw new Error(`Provider ${provider} not supported`);\n }\n }\n};\n\nexport type AIConfig = Omit<Parameters<typeof generateText>[0], 'prompt'> & {\n reasoningEffort?: ReasoningEffort;\n textVerbosity?: 'low' | 'medium' | 'high';\n dataSerialization?: 'json' | 'toon';\n};\n\nconst DEFAULT_PROVIDER: AiProviders = AiProviders.OPENAI as AiProviders;\n\nexport type AIConfigOptions = {\n userOptions?: AIOptions;\n projectOptions?: AIOptions;\n defaultOptions?: AIOptions;\n accessType?: AccessType[];\n};\n\n/**\n * Get AI model configuration based on the selected provider and options\n * This function handles the configuration for different AI providers\n *\n * @param options Configuration options including provider, API keys, models and temperature\n * @returns Configured AI model ready to use with generateText\n */\nexport const getAIConfig = async (\n options: AIConfigOptions,\n isAuthenticated: boolean = false\n): Promise<AIConfig> => {\n const {\n userOptions,\n projectOptions,\n defaultOptions,\n accessType = ['registered_user'],\n } = options;\n\n const aiOptions = {\n provider: DEFAULT_PROVIDER,\n ...defaultOptions,\n ...projectOptions,\n ...userOptions,\n } as AIOptions;\n\n const apiKey = getAPIKey(accessType, aiOptions, isAuthenticated);\n\n // Check if API key is provided\n if (!apiKey && aiOptions.provider !== AiProviders.OLLAMA) {\n throw new Error(`API key for ${aiOptions.provider} is missing`);\n }\n\n const languageModel = await getLanguageModel(\n aiOptions,\n apiKey,\n defaultOptions?.model\n );\n\n return {\n model: languageModel,\n temperature: aiOptions.temperature,\n dataSerialization: aiOptions.dataSerialization,\n };\n};\n"],"mappings":"oIAmLA,MAAM,GACJ,EACA,EACA,EAA2B,KACxB,CACH,IAAM,EAAgB,QAAQ,IAAI,eAElC,GAAI,EAAW,SAAS,SAAS,CAC/B,OAAO,GAAW,QAAU,EAG9B,GAAI,EAAW,SAAS,SAAS,EAAI,GAAW,OAC9C,OAAO,GAAW,OAQpB,GALI,EAAW,SAAS,kBAAkB,EAAI,GAK1C,EAAW,SAAS,eAAe,EAAI,EACzC,OAAO,GAAW,QAAU,GAM1B,GACJ,EACA,EACA,EACA,EAAsB,eACZ,CAEV,GAAI,GAAc,IAAa,EAAY,OAAQ,CACjD,GAAI,IAAa,EAAY,OAC3B,OAAO,GAAa,EAGtB,GAAI,EACF,OAAO,EAGT,OAAQ,EAAR,CACE,QACE,MAAO,KAKb,GAA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,CACtB,GAAM,CACJ,cACA,iBACA,iBACA,aAAa,CAAC,kBAAkB,EAC9B,EAEE,EAAY,CAChB,SAAU,EACV,GAAG,EACH,GAAG,EACH,GAAG,EACJ,CAEK,EAAS,EAAU,EAAY,EAAW,EAAgB,CAGhE,GAAI,CAAC,GAAU,EAAU,WAAa,EAAY,OAChD,MAAU,MAAM,eAAe,EAAU,SAAS,aAAa,CASjE,MAAO,CACL,MAPoB,MAAM,EAC1B,EACA,EACA,GAAgB,MACjB,CAIC,YAAa,EAAU,YACvB,kBAAmB,EAAU,kBAC9B"}
|
|
@@ -1,53 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { Output, generateText } from "ai";
|
|
3
|
-
import { z } from "zod";
|
|
1
|
+
import{readAsset as e}from"../_virtual/_utils_asset.mjs";import{Output as t,generateText as n}from"ai";import{z as r}from"zod";const i={},a=async({fileContent:i,tags:a,aiConfig:o,applicationContext:s})=>{let c=e(`./PROMPT.md`),l=e(`./EXAMPLE_REQUEST.md`),u=e(`./EXAMPLE_RESPONSE.md`),d=c.replace(`{{applicationContext}}`,s??``).replace(`{{tags}}`,a?JSON.stringify(a.map(({key:e,description:t})=>`- ${e}: ${t}`).join(`
|
|
4
2
|
|
|
5
|
-
|
|
6
|
-
const aiDefaultOptions = {};
|
|
7
|
-
/**
|
|
8
|
-
* Audits a content declaration file by constructing a prompt for AI models.
|
|
9
|
-
* The prompt includes details about the project's locales, file paths of content declarations,
|
|
10
|
-
* and requests for identifying issues or inconsistencies.
|
|
11
|
-
*/
|
|
12
|
-
const auditDictionaryMetadata = async ({ fileContent, tags, aiConfig, applicationContext }) => {
|
|
13
|
-
const CHAT_GPT_PROMPT = readAsset("./PROMPT.md");
|
|
14
|
-
const EXAMPLE_REQUEST = readAsset("./EXAMPLE_REQUEST.md");
|
|
15
|
-
const EXAMPLE_RESPONSE = readAsset("./EXAMPLE_RESPONSE.md");
|
|
16
|
-
const prompt = CHAT_GPT_PROMPT.replace("{{applicationContext}}", applicationContext ?? "").replace("{{tags}}", tags ? JSON.stringify(tags.map(({ key, description }) => `- ${key}: ${description}`).join("\n\n"), null, 2) : "");
|
|
17
|
-
const { dataSerialization, ...restAiConfig } = aiConfig;
|
|
18
|
-
const { output: _unusedOutput, ...validAiConfig } = restAiConfig;
|
|
19
|
-
const { output, usage } = await generateText({
|
|
20
|
-
...validAiConfig,
|
|
21
|
-
output: Output.object({ schema: z.object({
|
|
22
|
-
title: z.string(),
|
|
23
|
-
description: z.string(),
|
|
24
|
-
tags: z.array(z.string())
|
|
25
|
-
}) }),
|
|
26
|
-
messages: [
|
|
27
|
-
{
|
|
28
|
-
role: "system",
|
|
29
|
-
content: prompt
|
|
30
|
-
},
|
|
31
|
-
{
|
|
32
|
-
role: "user",
|
|
33
|
-
content: EXAMPLE_REQUEST
|
|
34
|
-
},
|
|
35
|
-
{
|
|
36
|
-
role: "assistant",
|
|
37
|
-
content: EXAMPLE_RESPONSE
|
|
38
|
-
},
|
|
39
|
-
{
|
|
40
|
-
role: "user",
|
|
41
|
-
content: fileContent
|
|
42
|
-
}
|
|
43
|
-
]
|
|
44
|
-
});
|
|
45
|
-
return {
|
|
46
|
-
fileContent: output,
|
|
47
|
-
tokenUsed: usage?.totalTokens ?? 0
|
|
48
|
-
};
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
//#endregion
|
|
52
|
-
export { aiDefaultOptions, auditDictionaryMetadata };
|
|
3
|
+
`),null,2):``),{dataSerialization:f,...p}=o,{output:m,...h}=p,{output:g,usage:_}=await n({...h,output:t.object({schema:r.object({title:r.string(),description:r.string(),tags:r.array(r.string())})}),messages:[{role:`system`,content:d},{role:`user`,content:l},{role:`assistant`,content:u},{role:`user`,content:i}]});return{fileContent:g,tokenUsed:_?.totalTokens??0}};export{i as aiDefaultOptions,a as auditDictionaryMetadata};
|
|
53
4
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../../../src/auditDictionaryMetadata/index.ts"],"sourcesContent":["import { readAsset } from 'utils:asset';\nimport { generateText, Output } from 'ai';\nimport { z } from 'zod';\nimport type { AIConfig, AIOptions } from '../aiSdk';\n\ntype Tag = {\n key: string;\n description?: string;\n};\n\nexport type AuditDictionaryMetadataOptions = {\n fileContent: string;\n tags?: Tag[];\n aiConfig: AIConfig;\n applicationContext?: string;\n};\n\nexport type AuditFileResultData = {\n fileContent: {\n title: string;\n description: string;\n tags: string[];\n };\n tokenUsed: number;\n};\n\nexport const aiDefaultOptions: AIOptions = {\n // Keep default options\n};\n\n/**\n * Audits a content declaration file by constructing a prompt for AI models.\n * The prompt includes details about the project's locales, file paths of content declarations,\n * and requests for identifying issues or inconsistencies.\n */\nexport const auditDictionaryMetadata = async ({\n fileContent,\n tags,\n aiConfig,\n applicationContext,\n}: AuditDictionaryMetadataOptions): Promise<\n AuditFileResultData | undefined\n> => {\n const CHAT_GPT_PROMPT = readAsset('./PROMPT.md');\n const EXAMPLE_REQUEST = readAsset('./EXAMPLE_REQUEST.md');\n const EXAMPLE_RESPONSE = readAsset('./EXAMPLE_RESPONSE.md');\n\n // Prepare the prompt for AI by replacing placeholders with actual values.\n const prompt = CHAT_GPT_PROMPT.replace(\n '{{applicationContext}}',\n applicationContext ?? ''\n ).replace(\n '{{tags}}',\n tags\n ? JSON.stringify(\n tags\n .map(({ key, description }) => `- ${key}: ${description}`)\n .join('\\n\\n'),\n null,\n 2\n )\n : ''\n );\n\n const { dataSerialization, ...restAiConfig } = aiConfig;\n const { output: _unusedOutput, ...validAiConfig } = restAiConfig;\n\n // Use the AI SDK to generate the completion\n const { output, usage } = await generateText({\n ...validAiConfig,\n output: Output.object({\n schema: z.object({\n title: z.string(),\n description: z.string(),\n tags: z.array(z.string()),\n }),\n }),\n messages: [\n { role: 'system', content: prompt },\n { role: 'user', content: EXAMPLE_REQUEST },\n { role: 'assistant', content: EXAMPLE_RESPONSE },\n {\n role: 'user',\n content: fileContent,\n },\n ],\n });\n\n return {\n fileContent: output,\n tokenUsed: usage?.totalTokens ?? 0,\n };\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../../src/auditDictionaryMetadata/index.ts"],"sourcesContent":["import { readAsset } from 'utils:asset';\nimport { generateText, Output } from 'ai';\nimport { z } from 'zod';\nimport type { AIConfig, AIOptions } from '../aiSdk';\n\ntype Tag = {\n key: string;\n description?: string;\n};\n\nexport type AuditDictionaryMetadataOptions = {\n fileContent: string;\n tags?: Tag[];\n aiConfig: AIConfig;\n applicationContext?: string;\n};\n\nexport type AuditFileResultData = {\n fileContent: {\n title: string;\n description: string;\n tags: string[];\n };\n tokenUsed: number;\n};\n\nexport const aiDefaultOptions: AIOptions = {\n // Keep default options\n};\n\n/**\n * Audits a content declaration file by constructing a prompt for AI models.\n * The prompt includes details about the project's locales, file paths of content declarations,\n * and requests for identifying issues or inconsistencies.\n */\nexport const auditDictionaryMetadata = async ({\n fileContent,\n tags,\n aiConfig,\n applicationContext,\n}: AuditDictionaryMetadataOptions): Promise<\n AuditFileResultData | undefined\n> => {\n const CHAT_GPT_PROMPT = readAsset('./PROMPT.md');\n const EXAMPLE_REQUEST = readAsset('./EXAMPLE_REQUEST.md');\n const EXAMPLE_RESPONSE = readAsset('./EXAMPLE_RESPONSE.md');\n\n // Prepare the prompt for AI by replacing placeholders with actual values.\n const prompt = CHAT_GPT_PROMPT.replace(\n '{{applicationContext}}',\n applicationContext ?? ''\n ).replace(\n '{{tags}}',\n tags\n ? JSON.stringify(\n tags\n .map(({ key, description }) => `- ${key}: ${description}`)\n .join('\\n\\n'),\n null,\n 2\n )\n : ''\n );\n\n const { dataSerialization, ...restAiConfig } = aiConfig;\n const { output: _unusedOutput, ...validAiConfig } = restAiConfig;\n\n // Use the AI SDK to generate the completion\n const { output, usage } = await generateText({\n ...validAiConfig,\n output: Output.object({\n schema: z.object({\n title: z.string(),\n description: z.string(),\n tags: z.array(z.string()),\n }),\n }),\n messages: [\n { role: 'system', content: prompt },\n { role: 'user', content: EXAMPLE_REQUEST },\n { role: 'assistant', content: EXAMPLE_RESPONSE },\n {\n role: 'user',\n content: fileContent,\n },\n ],\n });\n\n return {\n fileContent: output,\n tokenUsed: usage?.totalTokens ?? 0,\n };\n};\n"],"mappings":"+HA0BA,MAAa,EAA8B,EAE1C,CAOY,EAA0B,MAAO,CAC5C,cACA,OACA,WACA,wBAGG,CACH,IAAM,EAAkB,EAAU,cAAc,CAC1C,EAAkB,EAAU,uBAAuB,CACnD,EAAmB,EAAU,wBAAwB,CAGrD,EAAS,EAAgB,QAC7B,yBACA,GAAsB,GACvB,CAAC,QACA,WACA,EACI,KAAK,UACH,EACG,KAAK,CAAE,MAAK,iBAAkB,KAAK,EAAI,IAAI,IAAc,CACzD,KAAK;;EAAO,CACf,KACA,EACD,CACD,GACL,CAEK,CAAE,oBAAmB,GAAG,GAAiB,EACzC,CAAE,OAAQ,EAAe,GAAG,GAAkB,EAG9C,CAAE,SAAQ,SAAU,MAAM,EAAa,CAC3C,GAAG,EACH,OAAQ,EAAO,OAAO,CACpB,OAAQ,EAAE,OAAO,CACf,MAAO,EAAE,QAAQ,CACjB,YAAa,EAAE,QAAQ,CACvB,KAAM,EAAE,MAAM,EAAE,QAAQ,CAAC,CAC1B,CAAC,CACH,CAAC,CACF,SAAU,CACR,CAAE,KAAM,SAAU,QAAS,EAAQ,CACnC,CAAE,KAAM,OAAQ,QAAS,EAAiB,CAC1C,CAAE,KAAM,YAAa,QAAS,EAAkB,CAChD,CACE,KAAM,OACN,QAAS,EACV,CACF,CACF,CAAC,CAEF,MAAO,CACL,YAAa,EACb,UAAW,GAAO,aAAe,EAClC"}
|
package/dist/esm/customQuery.mjs
CHANGED
|
@@ -1,23 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
//#region src/customQuery.ts
|
|
4
|
-
const aiDefaultOptions = { model: "gpt-4o-mini" };
|
|
5
|
-
/**
|
|
6
|
-
* CustomQuery a content declaration file by constructing a prompt for AI models.
|
|
7
|
-
* The prompt includes details about the project's locales, file paths of content declarations,
|
|
8
|
-
* and requests for identifying issues or inconsistencies.
|
|
9
|
-
*/
|
|
10
|
-
const customQuery = async ({ messages, aiConfig }) => {
|
|
11
|
-
const { text: newContent, usage } = await generateText({
|
|
12
|
-
...aiConfig,
|
|
13
|
-
messages
|
|
14
|
-
});
|
|
15
|
-
return {
|
|
16
|
-
fileContent: newContent,
|
|
17
|
-
tokenUsed: usage?.totalTokens ?? 0
|
|
18
|
-
};
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
//#endregion
|
|
22
|
-
export { aiDefaultOptions, customQuery };
|
|
1
|
+
import{generateText as e}from"ai";const t={model:`gpt-4o-mini`},n=async({messages:t,aiConfig:n})=>{let{text:r,usage:i}=await e({...n,messages:t});return{fileContent:r,tokenUsed:i?.totalTokens??0}};export{t as aiDefaultOptions,n as customQuery};
|
|
23
2
|
//# sourceMappingURL=customQuery.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"customQuery.mjs","names":[],"sources":["../../src/customQuery.ts"],"sourcesContent":["import { generateText } from 'ai';\nimport type { AIConfig, AIOptions, Messages } from './aiSdk';\n\nexport type CustomQueryOptions = {\n messages: Messages;\n aiConfig: AIConfig;\n};\n\nexport type CustomQueryResultData = {\n fileContent: string;\n tokenUsed: number;\n};\n\nexport const aiDefaultOptions: AIOptions = {\n model: 'gpt-4o-mini',\n // Keep default options\n};\n\n/**\n * CustomQuery a content declaration file by constructing a prompt for AI models.\n * The prompt includes details about the project's locales, file paths of content declarations,\n * and requests for identifying issues or inconsistencies.\n */\nexport const customQuery = async ({\n messages,\n aiConfig,\n}: CustomQueryOptions): Promise<CustomQueryResultData | undefined> => {\n // Use the AI SDK to generate the completion\n const { text: newContent, usage } = await generateText({\n ...aiConfig,\n messages,\n });\n\n return {\n fileContent: newContent,\n tokenUsed: usage?.totalTokens ?? 0,\n };\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"customQuery.mjs","names":[],"sources":["../../src/customQuery.ts"],"sourcesContent":["import { generateText } from 'ai';\nimport type { AIConfig, AIOptions, Messages } from './aiSdk';\n\nexport type CustomQueryOptions = {\n messages: Messages;\n aiConfig: AIConfig;\n};\n\nexport type CustomQueryResultData = {\n fileContent: string;\n tokenUsed: number;\n};\n\nexport const aiDefaultOptions: AIOptions = {\n model: 'gpt-4o-mini',\n // Keep default options\n};\n\n/**\n * CustomQuery a content declaration file by constructing a prompt for AI models.\n * The prompt includes details about the project's locales, file paths of content declarations,\n * and requests for identifying issues or inconsistencies.\n */\nexport const customQuery = async ({\n messages,\n aiConfig,\n}: CustomQueryOptions): Promise<CustomQueryResultData | undefined> => {\n // Use the AI SDK to generate the completion\n const { text: newContent, usage } = await generateText({\n ...aiConfig,\n messages,\n });\n\n return {\n fileContent: newContent,\n tokenUsed: usage?.totalTokens ?? 0,\n };\n};\n"],"mappings":"kCAaA,MAAa,EAA8B,CACzC,MAAO,cAER,CAOY,EAAc,MAAO,CAChC,WACA,cACoE,CAEpE,GAAM,CAAE,KAAM,EAAY,SAAU,MAAM,EAAa,CACrD,GAAG,EACH,WACD,CAAC,CAEF,MAAO,CACL,YAAa,EACb,UAAW,GAAO,aAAe,EAClC"}
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,8 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { customQuery } from "./customQuery.mjs";
|
|
3
|
-
import { auditDictionaryMetadata } from "./auditDictionaryMetadata/index.mjs";
|
|
4
|
-
import { translateJSON } from "./translateJSON/index.mjs";
|
|
5
|
-
import { extractJson } from "./utils/extractJSON.mjs";
|
|
6
|
-
import { generateText, streamText } from "ai";
|
|
7
|
-
|
|
8
|
-
export { AiProviders as AIProvider, auditDictionaryMetadata, customQuery, extractJson, generateText, getAIConfig, streamText, translateJSON };
|
|
1
|
+
import{AIProvider as e,getAIConfig as t}from"./aiSdk.mjs";import{customQuery as n}from"./customQuery.mjs";import{auditDictionaryMetadata as r}from"./auditDictionaryMetadata/index.mjs";import{translateJSON as i}from"./translateJSON/index.mjs";import{extractJson as a}from"./utils/extractJSON.mjs";import{generateText as o,streamText as s}from"ai";export{e as AIProvider,r as auditDictionaryMetadata,n as customQuery,a as extractJson,o as generateText,t as getAIConfig,s as streamText,i as translateJSON};
|
|
@@ -1,119 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { readAsset } from "../_virtual/_utils_asset.mjs";
|
|
3
|
-
import { Locales } from "@intlayer/types";
|
|
4
|
-
import { Output, generateText } from "ai";
|
|
5
|
-
import { z } from "zod";
|
|
6
|
-
import { getLocaleName } from "@intlayer/core";
|
|
7
|
-
import { decode, encode } from "@toon-format/toon";
|
|
8
|
-
|
|
9
|
-
//#region src/translateJSON/index.ts
|
|
10
|
-
const aiDefaultOptions = {
|
|
11
|
-
provider: AiProviders.OPENAI,
|
|
12
|
-
model: "gpt-5-mini"
|
|
13
|
-
};
|
|
14
|
-
/**
|
|
15
|
-
* Format a locale with its name.
|
|
16
|
-
*
|
|
17
|
-
* @param locale - The locale to format.
|
|
18
|
-
* @returns A string in the format "locale: name", e.g. "en: English".
|
|
19
|
-
*/
|
|
20
|
-
const formatLocaleWithName = (locale) => `${locale}: ${getLocaleName(locale, Locales.ENGLISH)}`;
|
|
21
|
-
/**
|
|
22
|
-
* Formats tag instructions for the AI prompt.
|
|
23
|
-
* Creates a string with all available tags and their descriptions.
|
|
24
|
-
*
|
|
25
|
-
* @param tags - The list of tags to format.
|
|
26
|
-
* @returns A formatted string with tag instructions.
|
|
27
|
-
*/
|
|
28
|
-
const formatTagInstructions = (tags) => {
|
|
29
|
-
if (!tags || tags.length === 0) return "";
|
|
30
|
-
return `Based on the dictionary content, identify specific tags from the list below that would be relevant:
|
|
1
|
+
import{AIProvider as e}from"../aiSdk.mjs";import{readAsset as t}from"../_virtual/_utils_asset.mjs";import{Locales as n}from"@intlayer/types";import{Output as r,generateText as i}from"ai";import{z as a}from"zod";import{getLocaleName as o}from"@intlayer/core/localization";import{decode as s,encode as c}from"@toon-format/toon";const l={provider:e.OPENAI,model:`gpt-5-mini`},u=e=>`${e}: ${o(e,n.ENGLISH)}`,d=e=>!e||e.length===0?``:`Based on the dictionary content, identify specific tags from the list below that would be relevant:
|
|
31
2
|
|
|
32
|
-
${
|
|
33
|
-
};
|
|
34
|
-
const getModeInstructions = (mode) => {
|
|
35
|
-
if (mode === "complete") return "Mode: \"Complete\" - Enrich the preset content with the missing keys and values in the output locale. Do not update existing keys. Everything should be returned in the output.";
|
|
36
|
-
return "Mode: \"Review\" - Fill missing content and review existing keys from the preset content. If a key from the entry is missing in the output, it must be translated to the target language and added. If you detect misspelled content, or content that should be reformulated, correct it. If a translation is not coherent with the desired language, translate it.";
|
|
37
|
-
};
|
|
38
|
-
const jsonToZod = (content) => {
|
|
39
|
-
if (typeof content === "string") return z.string();
|
|
40
|
-
if (typeof content === "number") return z.number();
|
|
41
|
-
if (typeof content === "boolean") return z.boolean();
|
|
42
|
-
if (Array.isArray(content)) {
|
|
43
|
-
if (content.length === 0) return z.array(z.string());
|
|
44
|
-
return z.array(jsonToZod(content[0]));
|
|
45
|
-
}
|
|
46
|
-
if (typeof content === "object" && content !== null) {
|
|
47
|
-
const shape = {};
|
|
48
|
-
for (const key in content) shape[key] = jsonToZod(content[key]);
|
|
49
|
-
return z.object(shape);
|
|
50
|
-
}
|
|
51
|
-
return z.any();
|
|
52
|
-
};
|
|
53
|
-
/**
|
|
54
|
-
* TranslateJSONs a content declaration file by constructing a prompt for AI models.
|
|
55
|
-
* The prompt includes details about the project's locales, file paths of content declarations,
|
|
56
|
-
* and requests for identifying issues or inconsistencies.
|
|
57
|
-
*/
|
|
58
|
-
const translateJSON = async ({ entryFileContent, presetOutputContent, dictionaryDescription, aiConfig, entryLocale, outputLocale, tags, mode, applicationContext }) => {
|
|
59
|
-
const { dataSerialization, ...restAiConfig } = aiConfig;
|
|
60
|
-
const { output: _unusedOutput, ...validAiConfig } = restAiConfig;
|
|
61
|
-
const formattedEntryLocale = formatLocaleWithName(entryLocale);
|
|
62
|
-
const formattedOutputLocale = formatLocaleWithName(outputLocale);
|
|
63
|
-
const isToon = dataSerialization === "toon";
|
|
64
|
-
const promptFile = readAsset(isToon ? "./PROMPT_TOON.md" : "./PROMPT_JSON.md");
|
|
65
|
-
const entryContentStr = isToon ? encode(entryFileContent) : JSON.stringify(entryFileContent);
|
|
66
|
-
const presetContentStr = isToon ? encode(presetOutputContent) : JSON.stringify(presetOutputContent);
|
|
67
|
-
const prompt = promptFile.replace("{{entryLocale}}", formattedEntryLocale).replace("{{outputLocale}}", formattedOutputLocale).replace("{{presetOutputContent}}", presetContentStr).replace("{{dictionaryDescription}}", dictionaryDescription ?? "").replace("{{applicationContext}}", applicationContext ?? "").replace("{{tagsInstructions}}", formatTagInstructions(tags ?? [])).replace("{{modeInstructions}}", getModeInstructions(mode));
|
|
68
|
-
if (isToon) {
|
|
69
|
-
const { text, usage } = await generateText({
|
|
70
|
-
...aiConfig,
|
|
71
|
-
messages: [{
|
|
72
|
-
role: "system",
|
|
73
|
-
content: prompt
|
|
74
|
-
}, {
|
|
75
|
-
role: "user",
|
|
76
|
-
content: [
|
|
77
|
-
`# Translation Request`,
|
|
78
|
-
`Please translate the following TOON content.`,
|
|
79
|
-
`- **From:** ${formattedEntryLocale}`,
|
|
80
|
-
`- **To:** ${formattedOutputLocale}`,
|
|
81
|
-
``,
|
|
82
|
-
`## Entry Content:`,
|
|
83
|
-
entryContentStr
|
|
84
|
-
].join("\n")
|
|
85
|
-
}]
|
|
86
|
-
});
|
|
87
|
-
return {
|
|
88
|
-
fileContent: decode(text.replace(/^```(?:toon)?\n([\s\S]*?)\n```$/gm, "$1").trim()),
|
|
89
|
-
tokenUsed: usage?.totalTokens ?? 0
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
const { output, usage } = await generateText({
|
|
93
|
-
...validAiConfig,
|
|
94
|
-
output: Output.object({ schema: jsonToZod(entryFileContent) }),
|
|
95
|
-
messages: [{
|
|
96
|
-
role: "system",
|
|
97
|
-
content: prompt
|
|
98
|
-
}, {
|
|
99
|
-
role: "user",
|
|
100
|
-
content: [
|
|
101
|
-
`# Translation Request`,
|
|
102
|
-
`Please translate the following JSON content.`,
|
|
103
|
-
`- **From:** ${formattedEntryLocale}`,
|
|
104
|
-
`- **To:** ${formattedOutputLocale}`,
|
|
105
|
-
``,
|
|
106
|
-
`## Entry Content:`,
|
|
107
|
-
entryContentStr
|
|
108
|
-
].join("\n")
|
|
109
|
-
}]
|
|
110
|
-
});
|
|
111
|
-
return {
|
|
112
|
-
fileContent: output,
|
|
113
|
-
tokenUsed: usage?.totalTokens ?? 0
|
|
114
|
-
};
|
|
115
|
-
};
|
|
3
|
+
${e.map(({key:e,description:t})=>`- ${e}: ${t}`).join(`
|
|
116
4
|
|
|
117
|
-
|
|
118
|
-
|
|
5
|
+
`)}`,f=e=>e===`complete`?`Mode: "Complete" - Enrich the preset content with the missing keys and values in the output locale. Do not update existing keys. Everything should be returned in the output.`:`Mode: "Review" - Fill missing content and review existing keys from the preset content. If a key from the entry is missing in the output, it must be translated to the target language and added. If you detect misspelled content, or content that should be reformulated, correct it. If a translation is not coherent with the desired language, translate it.`,p=e=>{if(typeof e==`string`)return a.string();if(typeof e==`number`)return a.number();if(typeof e==`boolean`)return a.boolean();if(Array.isArray(e))return e.length===0?a.array(a.string()):a.array(p(e[0]));if(typeof e==`object`&&e){let t={};for(let n in e)t[n]=p(e[n]);return a.object(t)}return a.any()},m=async({entryFileContent:e,presetOutputContent:n,dictionaryDescription:a,aiConfig:o,entryLocale:l,outputLocale:m,tags:h,mode:g,applicationContext:_})=>{let{dataSerialization:v,...y}=o,{output:b,...x}=y,S=u(l),C=u(m),w=v===`toon`,T=t(w?`./PROMPT_TOON.md`:`./PROMPT_JSON.md`),E=w?c(e):JSON.stringify(e),D=w?c(n):JSON.stringify(n),O=T.replace(`{{entryLocale}}`,S).replace(`{{outputLocale}}`,C).replace(`{{presetOutputContent}}`,D).replace(`{{dictionaryDescription}}`,a??``).replace(`{{applicationContext}}`,_??``).replace(`{{tagsInstructions}}`,d(h??[])).replace(`{{modeInstructions}}`,f(g));if(w){let{text:e,usage:t}=await i({...o,messages:[{role:`system`,content:O},{role:`user`,content:[`# Translation Request`,`Please translate the following TOON content.`,`- **From:** ${S}`,`- **To:** ${C}`,``,`## Entry Content:`,E].join(`
|
|
6
|
+
`)}]});return{fileContent:s(e.replace(/^```(?:toon)?\n([\s\S]*?)\n```$/gm,`$1`).trim()),tokenUsed:t?.totalTokens??0}}let{output:k,usage:A}=await i({...x,output:r.object({schema:p(e)}),messages:[{role:`system`,content:O},{role:`user`,content:[`# Translation Request`,`Please translate the following JSON content.`,`- **From:** ${S}`,`- **To:** ${C}`,``,`## Entry Content:`,E].join(`
|
|
7
|
+
`)}]});return{fileContent:k,tokenUsed:A?.totalTokens??0}};export{l as aiDefaultOptions,m as translateJSON};
|
|
119
8
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["AIProvider"],"sources":["../../../src/translateJSON/index.ts"],"sourcesContent":["import { readAsset } from 'utils:asset';\nimport { getLocaleName } from '@intlayer/core';\nimport { type Locale, Locales } from '@intlayer/types';\nimport { decode, encode } from '@toon-format/toon';\nimport { generateText, Output } from 'ai';\nimport { z } from 'zod';\nimport { type AIConfig, type AIOptions, AIProvider } from '../aiSdk';\n\ntype Tag = {\n key: string;\n description?: string;\n};\n\nexport type TranslateJSONOptions<T = JSON> = {\n entryFileContent: T;\n presetOutputContent: Partial<T>;\n dictionaryDescription?: string;\n entryLocale: Locale;\n outputLocale: Locale;\n tags?: Tag[];\n aiConfig: AIConfig;\n mode: 'complete' | 'review';\n applicationContext?: string;\n};\n\nexport type TranslateJSONResultData<T = JSON> = {\n fileContent: T;\n tokenUsed: number;\n};\n\nexport const aiDefaultOptions: AIOptions = {\n provider: AIProvider.OPENAI,\n model: 'gpt-5-mini',\n};\n\n/**\n * Format a locale with its name.\n *\n * @param locale - The locale to format.\n * @returns A string in the format \"locale: name\", e.g. \"en: English\".\n */\nconst formatLocaleWithName = (locale: Locale): string =>\n `${locale}: ${getLocaleName(locale, Locales.ENGLISH)}`;\n\n/**\n * Formats tag instructions for the AI prompt.\n * Creates a string with all available tags and their descriptions.\n *\n * @param tags - The list of tags to format.\n * @returns A formatted string with tag instructions.\n */\nconst formatTagInstructions = (tags: Tag[]): string => {\n if (!tags || tags.length === 0) {\n return '';\n }\n\n // Prepare the tag instructions.\n return `Based on the dictionary content, identify specific tags from the list below that would be relevant:\n \n${tags.map(({ key, description }) => `- ${key}: ${description}`).join('\\n\\n')}`;\n};\n\nconst getModeInstructions = (mode: 'complete' | 'review'): string => {\n if (mode === 'complete') {\n return 'Mode: \"Complete\" - Enrich the preset content with the missing keys and values in the output locale. Do not update existing keys. Everything should be returned in the output.';\n }\n\n return 'Mode: \"Review\" - Fill missing content and review existing keys from the preset content. If a key from the entry is missing in the output, it must be translated to the target language and added. If you detect misspelled content, or content that should be reformulated, correct it. If a translation is not coherent with the desired language, translate it.';\n};\n\nconst jsonToZod = (content: any): z.ZodTypeAny => {\n // Base case: content is a string (the translation target)\n if (typeof content === 'string') {\n return z.string();\n }\n\n // Base cases: primitives often preserved in i18n files (e.g. strict numbers/booleans)\n if (typeof content === 'number') {\n return z.number();\n }\n if (typeof content === 'boolean') {\n return z.boolean();\n }\n\n // Recursive case: Array\n if (Array.isArray(content)) {\n // If array is empty, we assume array of strings as default for i18n\n if (content.length === 0) {\n return z.array(z.string());\n }\n // We assume all items in the array share the structure of the first item\n return z.array(jsonToZod(content[0]));\n }\n\n // Recursive case: Object\n if (typeof content === 'object' && content !== null) {\n const shape: Record<string, z.ZodTypeAny> = {};\n for (const key in content) {\n shape[key] = jsonToZod(content[key]);\n }\n return z.object(shape);\n }\n\n // Fallback\n return z.any();\n};\n\n/**\n * TranslateJSONs a content declaration file by constructing a prompt for AI models.\n * The prompt includes details about the project's locales, file paths of content declarations,\n * and requests for identifying issues or inconsistencies.\n */\nexport const translateJSON = async <T>({\n entryFileContent,\n presetOutputContent,\n dictionaryDescription,\n aiConfig,\n entryLocale,\n outputLocale,\n tags,\n mode,\n applicationContext,\n}: TranslateJSONOptions<T>): Promise<\n TranslateJSONResultData<T> | undefined\n> => {\n const { dataSerialization, ...restAiConfig } = aiConfig;\n // @ts-ignore\n const { output: _unusedOutput, ...validAiConfig } = restAiConfig;\n\n const formattedEntryLocale = formatLocaleWithName(entryLocale);\n const formattedOutputLocale = formatLocaleWithName(outputLocale);\n\n const isToon = dataSerialization === 'toon';\n const promptFile = readAsset(\n isToon ? './PROMPT_TOON.md' : './PROMPT_JSON.md'\n );\n const entryContentStr = isToon\n ? encode(entryFileContent)\n : JSON.stringify(entryFileContent);\n const presetContentStr = isToon\n ? encode(presetOutputContent)\n : JSON.stringify(presetOutputContent);\n\n // Prepare the prompt for AI by replacing placeholders with actual values.\n const prompt = promptFile\n .replace('{{entryLocale}}', formattedEntryLocale)\n .replace('{{outputLocale}}', formattedOutputLocale)\n .replace('{{presetOutputContent}}', presetContentStr)\n .replace('{{dictionaryDescription}}', dictionaryDescription ?? '')\n .replace('{{applicationContext}}', applicationContext ?? '')\n .replace('{{tagsInstructions}}', formatTagInstructions(tags ?? []))\n .replace('{{modeInstructions}}', getModeInstructions(mode));\n\n if (isToon) {\n const { text, usage } = await generateText({\n ...aiConfig,\n messages: [\n { role: 'system', content: prompt },\n {\n role: 'user',\n content: [\n `# Translation Request`,\n `Please translate the following TOON content.`,\n `- **From:** ${formattedEntryLocale}`,\n `- **To:** ${formattedOutputLocale}`,\n ``,\n `## Entry Content:`,\n entryContentStr,\n ].join('\\n'),\n },\n ],\n });\n\n // Strip markdown code blocks if present\n const cleanedText = text\n .replace(/^```(?:toon)?\\n([\\s\\S]*?)\\n```$/gm, '$1')\n .trim();\n\n return {\n fileContent: decode(cleanedText) as T,\n tokenUsed: usage?.totalTokens ?? 0,\n };\n }\n\n // Use the AI SDK to generate the completion\n const { output, usage } = await generateText({\n ...validAiConfig,\n output: Output.object({\n schema: jsonToZod(entryFileContent),\n }),\n messages: [\n { role: 'system', content: prompt },\n {\n role: 'user',\n // KEY CHANGE: Explicitly repeating instructions in the user message\n content: [\n `# Translation Request`,\n `Please translate the following JSON content.`,\n `- **From:** ${formattedEntryLocale}`,\n `- **To:** ${formattedOutputLocale}`,\n ``,\n `## Entry Content:`,\n entryContentStr,\n ].join('\\n'),\n },\n ],\n });\n\n return {\n fileContent: output as T,\n tokenUsed: usage?.totalTokens ?? 0,\n };\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["AIProvider"],"sources":["../../../src/translateJSON/index.ts"],"sourcesContent":["import { readAsset } from 'utils:asset';\nimport { getLocaleName } from '@intlayer/core/localization';\nimport { type Locale, Locales } from '@intlayer/types';\nimport { decode, encode } from '@toon-format/toon';\nimport { generateText, Output } from 'ai';\nimport { z } from 'zod';\nimport { type AIConfig, type AIOptions, AIProvider } from '../aiSdk';\n\ntype Tag = {\n key: string;\n description?: string;\n};\n\nexport type TranslateJSONOptions<T = JSON> = {\n entryFileContent: T;\n presetOutputContent: Partial<T>;\n dictionaryDescription?: string;\n entryLocale: Locale;\n outputLocale: Locale;\n tags?: Tag[];\n aiConfig: AIConfig;\n mode: 'complete' | 'review';\n applicationContext?: string;\n};\n\nexport type TranslateJSONResultData<T = JSON> = {\n fileContent: T;\n tokenUsed: number;\n};\n\nexport const aiDefaultOptions: AIOptions = {\n provider: AIProvider.OPENAI,\n model: 'gpt-5-mini',\n};\n\n/**\n * Format a locale with its name.\n *\n * @param locale - The locale to format.\n * @returns A string in the format \"locale: name\", e.g. \"en: English\".\n */\nconst formatLocaleWithName = (locale: Locale): string =>\n `${locale}: ${getLocaleName(locale, Locales.ENGLISH)}`;\n\n/**\n * Formats tag instructions for the AI prompt.\n * Creates a string with all available tags and their descriptions.\n *\n * @param tags - The list of tags to format.\n * @returns A formatted string with tag instructions.\n */\nconst formatTagInstructions = (tags: Tag[]): string => {\n if (!tags || tags.length === 0) {\n return '';\n }\n\n // Prepare the tag instructions.\n return `Based on the dictionary content, identify specific tags from the list below that would be relevant:\n \n${tags.map(({ key, description }) => `- ${key}: ${description}`).join('\\n\\n')}`;\n};\n\nconst getModeInstructions = (mode: 'complete' | 'review'): string => {\n if (mode === 'complete') {\n return 'Mode: \"Complete\" - Enrich the preset content with the missing keys and values in the output locale. Do not update existing keys. Everything should be returned in the output.';\n }\n\n return 'Mode: \"Review\" - Fill missing content and review existing keys from the preset content. If a key from the entry is missing in the output, it must be translated to the target language and added. If you detect misspelled content, or content that should be reformulated, correct it. If a translation is not coherent with the desired language, translate it.';\n};\n\nconst jsonToZod = (content: any): z.ZodTypeAny => {\n // Base case: content is a string (the translation target)\n if (typeof content === 'string') {\n return z.string();\n }\n\n // Base cases: primitives often preserved in i18n files (e.g. strict numbers/booleans)\n if (typeof content === 'number') {\n return z.number();\n }\n if (typeof content === 'boolean') {\n return z.boolean();\n }\n\n // Recursive case: Array\n if (Array.isArray(content)) {\n // If array is empty, we assume array of strings as default for i18n\n if (content.length === 0) {\n return z.array(z.string());\n }\n // We assume all items in the array share the structure of the first item\n return z.array(jsonToZod(content[0]));\n }\n\n // Recursive case: Object\n if (typeof content === 'object' && content !== null) {\n const shape: Record<string, z.ZodTypeAny> = {};\n for (const key in content) {\n shape[key] = jsonToZod(content[key]);\n }\n return z.object(shape);\n }\n\n // Fallback\n return z.any();\n};\n\n/**\n * TranslateJSONs a content declaration file by constructing a prompt for AI models.\n * The prompt includes details about the project's locales, file paths of content declarations,\n * and requests for identifying issues or inconsistencies.\n */\nexport const translateJSON = async <T>({\n entryFileContent,\n presetOutputContent,\n dictionaryDescription,\n aiConfig,\n entryLocale,\n outputLocale,\n tags,\n mode,\n applicationContext,\n}: TranslateJSONOptions<T>): Promise<\n TranslateJSONResultData<T> | undefined\n> => {\n const { dataSerialization, ...restAiConfig } = aiConfig;\n // @ts-ignore\n const { output: _unusedOutput, ...validAiConfig } = restAiConfig;\n\n const formattedEntryLocale = formatLocaleWithName(entryLocale);\n const formattedOutputLocale = formatLocaleWithName(outputLocale);\n\n const isToon = dataSerialization === 'toon';\n const promptFile = readAsset(\n isToon ? './PROMPT_TOON.md' : './PROMPT_JSON.md'\n );\n const entryContentStr = isToon\n ? encode(entryFileContent)\n : JSON.stringify(entryFileContent);\n const presetContentStr = isToon\n ? encode(presetOutputContent)\n : JSON.stringify(presetOutputContent);\n\n // Prepare the prompt for AI by replacing placeholders with actual values.\n const prompt = promptFile\n .replace('{{entryLocale}}', formattedEntryLocale)\n .replace('{{outputLocale}}', formattedOutputLocale)\n .replace('{{presetOutputContent}}', presetContentStr)\n .replace('{{dictionaryDescription}}', dictionaryDescription ?? '')\n .replace('{{applicationContext}}', applicationContext ?? '')\n .replace('{{tagsInstructions}}', formatTagInstructions(tags ?? []))\n .replace('{{modeInstructions}}', getModeInstructions(mode));\n\n if (isToon) {\n const { text, usage } = await generateText({\n ...aiConfig,\n messages: [\n { role: 'system', content: prompt },\n {\n role: 'user',\n content: [\n `# Translation Request`,\n `Please translate the following TOON content.`,\n `- **From:** ${formattedEntryLocale}`,\n `- **To:** ${formattedOutputLocale}`,\n ``,\n `## Entry Content:`,\n entryContentStr,\n ].join('\\n'),\n },\n ],\n });\n\n // Strip markdown code blocks if present\n const cleanedText = text\n .replace(/^```(?:toon)?\\n([\\s\\S]*?)\\n```$/gm, '$1')\n .trim();\n\n return {\n fileContent: decode(cleanedText) as T,\n tokenUsed: usage?.totalTokens ?? 0,\n };\n }\n\n // Use the AI SDK to generate the completion\n const { output, usage } = await generateText({\n ...validAiConfig,\n output: Output.object({\n schema: jsonToZod(entryFileContent),\n }),\n messages: [\n { role: 'system', content: prompt },\n {\n role: 'user',\n // KEY CHANGE: Explicitly repeating instructions in the user message\n content: [\n `# Translation Request`,\n `Please translate the following JSON content.`,\n `- **From:** ${formattedEntryLocale}`,\n `- **To:** ${formattedOutputLocale}`,\n ``,\n `##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AiB,CAC9B,EAAmB,EACrB,EAAO,EAAoB,CAC3B,KAAK,UAAU,EAAoB,CAGjC,EAAS,EACZ,QAAQ,kBAAmB,EAAqB,CAChD,QAAQ,mBAAoB,EAAsB,CAClD,QAAQ,0BAA2B,EAAiB,CACpD,QAAQ,4BAA6B,GAAyB,GAAG,CACjE,QAAQ,yBAA0B,GAAsB,GAAG,CAC3D,QAAQ,uBAAwB,EAAsB,GAAQ,EAAE,CAAC,CAAC,CAClE,QAAQ,uBAAwB,EAAoB,EAAK,CAAC,CAE7D,GAAI,EAAQ,CACV,GAAM,CAAE,OAAM,SAAU,MAAM,EAAa,CACzC,GAAG,EACH,SAAU,CACR,CAAE,KAAM,SAAU,QAAS,EAAQ,CACnC,CACE,KAAM,OACN,QAAS,CACP,wBACA,+CACA,eAAe,IACf,aAAa,IACb,GACA,oBACA,EACD,CAAC,KAAK;EAAK,CACb,CACF,CACF,CAAC,CAOF,MAAO,CACL,YAAa,EALK,EACjB,QAAQ,oCAAqC,KAAK,CAClD,MAAM,CAGyB,CAChC,UAAW,GAAO,aAAe,EAClC,CAIH,GAAM,CAAE,SAAQ,SAAU,MAAM,EAAa,CAC3C,GAAG,EACH,OAAQ,EAAO,OAAO,CACpB,OAAQ,EAAU,EAAiB,CACpC,CAAC,CACF,SAAU,CACR,CAAE,KAAM,SAAU,QAAS,EAAQ,CACnC,CACE,KAAM,OAEN,QAAS,CACP,wBACA,+CACA,eAAe,IACf,aAAa,IACb,GACA,oBACA,EACD,CAAC,KAAK;EAAK,CACb,CACF,CACF,CAAC,CAEF,MAAO,CACL,YAAa,EACb,UAAW,GAAO,aAAe,EAClC"}
|
|
@@ -1,60 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* Extracts and parses the first valid JSON value (object or array) from a string containing arbitrary text.
|
|
4
|
-
* This is used to safely extract JSON from LLM responses that may contain additional text or markdown.
|
|
5
|
-
*
|
|
6
|
-
* @example
|
|
7
|
-
* // Extracts JSON object from markdown response:
|
|
8
|
-
* ```json
|
|
9
|
-
* {
|
|
10
|
-
* "title": "Test content declarations",
|
|
11
|
-
* "description": "A comprehensive test dictionary...",
|
|
12
|
-
* "tags": ["test tag"]
|
|
13
|
-
* }
|
|
14
|
-
* ```
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* // Extracts JSON array:
|
|
18
|
-
* ```json
|
|
19
|
-
* ["item1", "item2", "item3"]
|
|
20
|
-
* ```
|
|
21
|
-
*
|
|
22
|
-
* @example
|
|
23
|
-
* // Extracts JSON from markdown:
|
|
24
|
-
* Here is the response:
|
|
25
|
-
* ```json
|
|
26
|
-
* {"key": "value"}
|
|
27
|
-
* ```
|
|
28
|
-
* End of response.
|
|
29
|
-
*
|
|
30
|
-
* @throws {Error} If no valid JSON object/array is found or if parsing fails
|
|
31
|
-
* @returns {T} The parsed JSON value cast to type T
|
|
32
|
-
*/
|
|
33
|
-
const extractJson = (input) => {
|
|
34
|
-
const opening = input.match(/[{[]/);
|
|
35
|
-
if (!opening) throw new Error("No JSON start character ({ or [) found.");
|
|
36
|
-
const startIdx = opening.index;
|
|
37
|
-
const openChar = input[startIdx];
|
|
38
|
-
const closeChar = openChar === "{" ? "}" : "]";
|
|
39
|
-
let depth = 0;
|
|
40
|
-
for (let i = startIdx; i < input.length; i++) {
|
|
41
|
-
const char = input[i];
|
|
42
|
-
if (char === openChar) depth++;
|
|
43
|
-
else if (char === closeChar) {
|
|
44
|
-
depth--;
|
|
45
|
-
if (depth === 0) {
|
|
46
|
-
const jsonSubstring = input.slice(startIdx, i + 1);
|
|
47
|
-
try {
|
|
48
|
-
return JSON.parse(jsonSubstring);
|
|
49
|
-
} catch (err) {
|
|
50
|
-
throw new Error(`Failed to parse JSON: ${err.message}`);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
throw new Error("Reached end of input without closing JSON bracket.");
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
//#endregion
|
|
59
|
-
export { extractJson };
|
|
1
|
+
const e=e=>{let t=e.match(/[{[]/);if(!t)throw Error(`No JSON start character ({ or [) found.`);let n=t.index,r=e[n],i=r===`{`?`}`:`]`,a=0;for(let t=n;t<e.length;t++){let o=e[t];if(o===r)a++;else if(o===i&&(a--,a===0)){let r=e.slice(n,t+1);try{return JSON.parse(r)}catch(e){throw Error(`Failed to parse JSON: ${e.message}`)}}}throw Error(`Reached end of input without closing JSON bracket.`)};export{e as extractJson};
|
|
60
2
|
//# sourceMappingURL=extractJSON.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extractJSON.mjs","names":[],"sources":["../../../src/utils/extractJSON.ts"],"sourcesContent":["/**\n * Extracts and parses the first valid JSON value (object or array) from a string containing arbitrary text.\n * This is used to safely extract JSON from LLM responses that may contain additional text or markdown.\n *\n * @example\n * // Extracts JSON object from markdown response:\n * ```json\n * {\n * \"title\": \"Test content declarations\",\n * \"description\": \"A comprehensive test dictionary...\",\n * \"tags\": [\"test tag\"]\n * }\n * ```\n *\n * @example\n * // Extracts JSON array:\n * ```json\n * [\"item1\", \"item2\", \"item3\"]\n * ```\n *\n * @example\n * // Extracts JSON from markdown:\n * Here is the response:\n * ```json\n * {\"key\": \"value\"}\n * ```\n * End of response.\n *\n * @throws {Error} If no valid JSON object/array is found or if parsing fails\n * @returns {T} The parsed JSON value cast to type T\n */\nexport const extractJson = <T = any>(input: string): T => {\n const opening = input.match(/[{[]/);\n if (!opening) throw new Error('No JSON start character ({ or [) found.');\n\n const startIdx = opening.index!;\n const openChar = input[startIdx];\n const closeChar = openChar === '{' ? '}' : ']';\n let depth = 0;\n\n for (let i = startIdx; i < input.length; i++) {\n const char = input[i];\n if (char === openChar) depth++;\n else if (char === closeChar) {\n depth--;\n if (depth === 0) {\n const jsonSubstring = input.slice(startIdx, i + 1);\n try {\n return JSON.parse(jsonSubstring) as T;\n } catch (err) {\n throw new Error(`Failed to parse JSON: ${(err as Error).message}`);\n }\n }\n }\n }\n\n throw new Error('Reached end of input without closing JSON bracket.');\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"extractJSON.mjs","names":[],"sources":["../../../src/utils/extractJSON.ts"],"sourcesContent":["/**\n * Extracts and parses the first valid JSON value (object or array) from a string containing arbitrary text.\n * This is used to safely extract JSON from LLM responses that may contain additional text or markdown.\n *\n * @example\n * // Extracts JSON object from markdown response:\n * ```json\n * {\n * \"title\": \"Test content declarations\",\n * \"description\": \"A comprehensive test dictionary...\",\n * \"tags\": [\"test tag\"]\n * }\n * ```\n *\n * @example\n * // Extracts JSON array:\n * ```json\n * [\"item1\", \"item2\", \"item3\"]\n * ```\n *\n * @example\n * // Extracts JSON from markdown:\n * Here is the response:\n * ```json\n * {\"key\": \"value\"}\n * ```\n * End of response.\n *\n * @throws {Error} If no valid JSON object/array is found or if parsing fails\n * @returns {T} The parsed JSON value cast to type T\n */\nexport const extractJson = <T = any>(input: string): T => {\n const opening = input.match(/[{[]/);\n if (!opening) throw new Error('No JSON start character ({ or [) found.');\n\n const startIdx = opening.index!;\n const openChar = input[startIdx];\n const closeChar = openChar === '{' ? '}' : ']';\n let depth = 0;\n\n for (let i = startIdx; i < input.length; i++) {\n const char = input[i];\n if (char === openChar) depth++;\n else if (char === closeChar) {\n depth--;\n if (depth === 0) {\n const jsonSubstring = input.slice(startIdx, i + 1);\n try {\n return JSON.parse(jsonSubstring) as T;\n } catch (err) {\n throw new Error(`Failed to parse JSON: ${(err as Error).message}`);\n }\n }\n }\n }\n\n throw new Error('Reached end of input without closing JSON bracket.');\n};\n"],"mappings":"AA+BA,MAAa,EAAwB,GAAqB,CACxD,IAAM,EAAU,EAAM,MAAM,OAAO,CACnC,GAAI,CAAC,EAAS,MAAU,MAAM,0CAA0C,CAExE,IAAM,EAAW,EAAQ,MACnB,EAAW,EAAM,GACjB,EAAY,IAAa,IAAM,IAAM,IACvC,EAAQ,EAEZ,IAAK,IAAI,EAAI,EAAU,EAAI,EAAM,OAAQ,IAAK,CAC5C,IAAM,EAAO,EAAM,GACnB,GAAI,IAAS,EAAU,YACd,IAAS,IAChB,IACI,IAAU,GAAG,CACf,IAAM,EAAgB,EAAM,MAAM,EAAU,EAAI,EAAE,CAClD,GAAI,CACF,OAAO,KAAK,MAAM,EAAc,OACzB,EAAK,CACZ,MAAU,MAAM,yBAA0B,EAAc,UAAU,GAM1E,MAAU,MAAM,qDAAqD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intlayer/ai",
|
|
3
|
-
"version": "8.1.
|
|
3
|
+
"version": "8.1.3",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "SDK that provides AI capabilities for Intlayer applications",
|
|
6
6
|
"keywords": [
|
|
@@ -76,10 +76,10 @@
|
|
|
76
76
|
"@ai-sdk/anthropic": "3.0.44",
|
|
77
77
|
"@ai-sdk/google": "3.0.29",
|
|
78
78
|
"@ai-sdk/openai": "3.0.29",
|
|
79
|
-
"@intlayer/api": "8.1.
|
|
80
|
-
"@intlayer/config": "8.1.
|
|
81
|
-
"@intlayer/core": "8.1.
|
|
82
|
-
"@intlayer/types": "8.1.
|
|
79
|
+
"@intlayer/api": "8.1.3",
|
|
80
|
+
"@intlayer/config": "8.1.3",
|
|
81
|
+
"@intlayer/core": "8.1.3",
|
|
82
|
+
"@intlayer/types": "8.1.3",
|
|
83
83
|
"@toon-format/toon": "2.1.0",
|
|
84
84
|
"ai": "6.0.86",
|
|
85
85
|
"zod": "4.3.6"
|