@intlayer/ai 8.0.5-canary.0 → 8.1.0
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/aiSdk.cjs +157 -57
- package/dist/cjs/aiSdk.cjs.map +1 -1
- package/dist/cjs/index.cjs +2 -1
- package/dist/cjs/translateJSON/index.cjs +2 -3
- package/dist/cjs/translateJSON/index.cjs.map +1 -1
- package/dist/esm/aiSdk.mjs +157 -57
- package/dist/esm/aiSdk.mjs.map +1 -1
- package/dist/esm/index.mjs +2 -2
- package/dist/esm/translateJSON/index.mjs +3 -3
- package/dist/esm/translateJSON/index.mjs.map +1 -1
- package/dist/types/aiSdk.d.ts +76 -22
- package/dist/types/aiSdk.d.ts.map +1 -1
- package/dist/types/index.d.ts +24 -2
- package/dist/types/index.d.ts.map +1 -0
- package/package.json +70 -13
package/dist/cjs/aiSdk.cjs
CHANGED
|
@@ -1,23 +1,8 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
let
|
|
3
|
-
let
|
|
4
|
-
let _ai_sdk_google = require("@ai-sdk/google");
|
|
5
|
-
let _ai_sdk_mistral = require("@ai-sdk/mistral");
|
|
6
|
-
let _ai_sdk_openai = require("@ai-sdk/openai");
|
|
2
|
+
let _intlayer_config = require("@intlayer/config");
|
|
3
|
+
let _intlayer_types = require("@intlayer/types");
|
|
7
4
|
|
|
8
5
|
//#region src/aiSdk.ts
|
|
9
|
-
/**
|
|
10
|
-
* Supported AI SDK providers
|
|
11
|
-
*/
|
|
12
|
-
let AIProvider = /* @__PURE__ */ function(AIProvider) {
|
|
13
|
-
AIProvider["OPENAI"] = "openai";
|
|
14
|
-
AIProvider["ANTHROPIC"] = "anthropic";
|
|
15
|
-
AIProvider["MISTRAL"] = "mistral";
|
|
16
|
-
AIProvider["DEEPSEEK"] = "deepseek";
|
|
17
|
-
AIProvider["GEMINI"] = "gemini";
|
|
18
|
-
AIProvider["OLLAMA"] = "ollama";
|
|
19
|
-
return AIProvider;
|
|
20
|
-
}({});
|
|
21
6
|
const getAPIKey = (accessType, aiOptions, isAuthenticated = false) => {
|
|
22
7
|
const defaultApiKey = process.env.OPENAI_API_KEY;
|
|
23
8
|
if (accessType.includes("public")) return aiOptions?.apiKey ?? defaultApiKey;
|
|
@@ -26,53 +11,168 @@ const getAPIKey = (accessType, aiOptions, isAuthenticated = false) => {
|
|
|
26
11
|
if (accessType.includes("premium_user") && isAuthenticated) return aiOptions?.apiKey ?? defaultApiKey;
|
|
27
12
|
};
|
|
28
13
|
const getModelName = (provider, userApiKey, userModel, defaultModel = "gpt-5-mini") => {
|
|
29
|
-
if (userApiKey || provider ===
|
|
30
|
-
if (provider ===
|
|
14
|
+
if (userApiKey || provider === _intlayer_types.AiProviders.OLLAMA) {
|
|
15
|
+
if (provider === _intlayer_types.AiProviders.OPENAI) return userModel ?? defaultModel;
|
|
31
16
|
if (userModel) return userModel;
|
|
32
17
|
switch (provider) {
|
|
33
|
-
|
|
34
|
-
case AIProvider.MISTRAL: return "mistral-large-latest";
|
|
35
|
-
case AIProvider.DEEPSEEK: return "deepseek-coder";
|
|
36
|
-
case AIProvider.GEMINI: return "gemini-2.5-flash";
|
|
37
|
-
case AIProvider.OLLAMA: return "";
|
|
38
|
-
default: return defaultModel;
|
|
18
|
+
default: return "-";
|
|
39
19
|
}
|
|
40
20
|
}
|
|
41
21
|
if (userModel || provider) throw new Error("The user should use his own API key to use a custom model");
|
|
42
22
|
return defaultModel;
|
|
43
23
|
};
|
|
44
|
-
const getLanguageModel = (aiOptions, apiKey, defaultModel) => {
|
|
45
|
-
const
|
|
24
|
+
const getLanguageModel = async (aiOptions, apiKey, defaultModel) => {
|
|
25
|
+
const loadModule = async (packageName) => {
|
|
26
|
+
try {
|
|
27
|
+
return await import(packageName);
|
|
28
|
+
} catch {
|
|
29
|
+
(0, _intlayer_config.logger)(`${_intlayer_config.x} The package "${(0, _intlayer_config.colorize)(packageName, _intlayer_config.ANSIColors.GREEN)}" is required to use this AI provider. Please install it using: ${(0, _intlayer_config.colorize)(`npm install ${packageName}`, _intlayer_config.ANSIColors.GREY_DARK)}`, { level: "error" });
|
|
30
|
+
process.exit();
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const provider = aiOptions.provider ?? _intlayer_types.AiProviders.OPENAI;
|
|
34
|
+
const selectedModel = getModelName(provider, apiKey, aiOptions.model, defaultModel);
|
|
46
35
|
const baseURL = aiOptions.baseURL;
|
|
47
|
-
switch (
|
|
48
|
-
case
|
|
49
|
-
apiKey,
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
baseURL
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
36
|
+
switch (provider) {
|
|
37
|
+
case _intlayer_types.AiProviders.OPENAI: {
|
|
38
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
39
|
+
const { createOpenAI } = await loadModule("@ai-sdk/openai");
|
|
40
|
+
return createOpenAI({
|
|
41
|
+
apiKey,
|
|
42
|
+
baseURL,
|
|
43
|
+
...otherOptions
|
|
44
|
+
})(selectedModel);
|
|
45
|
+
}
|
|
46
|
+
case _intlayer_types.AiProviders.ANTHROPIC: {
|
|
47
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
48
|
+
const { createAnthropic } = await loadModule("@ai-sdk/anthropic");
|
|
49
|
+
return createAnthropic({
|
|
50
|
+
apiKey,
|
|
51
|
+
baseURL,
|
|
52
|
+
...otherOptions
|
|
53
|
+
})(selectedModel);
|
|
54
|
+
}
|
|
55
|
+
case _intlayer_types.AiProviders.MISTRAL: {
|
|
56
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
57
|
+
const { createMistral } = await loadModule("@ai-sdk/mistral");
|
|
58
|
+
return createMistral({
|
|
59
|
+
apiKey,
|
|
60
|
+
baseURL,
|
|
61
|
+
...otherOptions
|
|
62
|
+
})(selectedModel);
|
|
63
|
+
}
|
|
64
|
+
case _intlayer_types.AiProviders.DEEPSEEK: {
|
|
65
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
66
|
+
const { createDeepSeek } = await loadModule("@ai-sdk/deepseek");
|
|
67
|
+
return createDeepSeek({
|
|
68
|
+
apiKey,
|
|
69
|
+
baseURL,
|
|
70
|
+
...otherOptions
|
|
71
|
+
})(selectedModel);
|
|
72
|
+
}
|
|
73
|
+
case _intlayer_types.AiProviders.GEMINI: {
|
|
74
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
75
|
+
const { createGoogleGenerativeAI } = await loadModule("@ai-sdk/google");
|
|
76
|
+
return createGoogleGenerativeAI({
|
|
77
|
+
apiKey,
|
|
78
|
+
baseURL,
|
|
79
|
+
...otherOptions
|
|
80
|
+
})(selectedModel);
|
|
81
|
+
}
|
|
82
|
+
case _intlayer_types.AiProviders.GOOGLEVERTEX: {
|
|
83
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
84
|
+
const { createVertex } = await loadModule("@ai-sdk/google-vertex");
|
|
85
|
+
return createVertex({
|
|
86
|
+
apiKey,
|
|
87
|
+
baseURL,
|
|
88
|
+
...otherOptions
|
|
89
|
+
})(selectedModel);
|
|
90
|
+
}
|
|
91
|
+
case _intlayer_types.AiProviders.GOOGLEGENERATIVEAI: {
|
|
92
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
93
|
+
const { createGoogleGenerativeAI } = await loadModule("@ai-sdk/google");
|
|
94
|
+
return createGoogleGenerativeAI({
|
|
95
|
+
apiKey,
|
|
96
|
+
baseURL,
|
|
97
|
+
...otherOptions
|
|
98
|
+
})(selectedModel);
|
|
99
|
+
}
|
|
100
|
+
case _intlayer_types.AiProviders.OLLAMA: {
|
|
101
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
102
|
+
const { createOpenAI } = await loadModule("@ai-sdk/openai");
|
|
103
|
+
return createOpenAI({
|
|
104
|
+
baseURL: baseURL ?? "http://localhost:11434/v1",
|
|
105
|
+
apiKey: apiKey ?? "ollama",
|
|
106
|
+
...otherOptions
|
|
107
|
+
}).chat(selectedModel);
|
|
108
|
+
}
|
|
109
|
+
case _intlayer_types.AiProviders.OPENROUTER: {
|
|
110
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
111
|
+
const { createOpenRouter } = await loadModule("@openrouter/ai-sdk-provider");
|
|
112
|
+
return createOpenRouter({
|
|
113
|
+
apiKey,
|
|
114
|
+
baseURL,
|
|
115
|
+
...otherOptions
|
|
116
|
+
})(selectedModel);
|
|
117
|
+
}
|
|
118
|
+
case _intlayer_types.AiProviders.ALIBABA: {
|
|
119
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
120
|
+
const { createAlibaba } = await loadModule("@ai-sdk/alibaba");
|
|
121
|
+
return createAlibaba({
|
|
122
|
+
apiKey,
|
|
123
|
+
baseURL,
|
|
124
|
+
...otherOptions
|
|
125
|
+
})(selectedModel);
|
|
126
|
+
}
|
|
127
|
+
case _intlayer_types.AiProviders.FIREWORKS: {
|
|
128
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
129
|
+
const { createFireworks } = await loadModule("@ai-sdk/fireworks");
|
|
130
|
+
return createFireworks({
|
|
131
|
+
apiKey,
|
|
132
|
+
baseURL,
|
|
133
|
+
...otherOptions
|
|
134
|
+
})(selectedModel);
|
|
135
|
+
}
|
|
136
|
+
case _intlayer_types.AiProviders.GROQ: {
|
|
137
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
138
|
+
const { createGroq } = await loadModule("@ai-sdk/groq");
|
|
139
|
+
return createGroq({
|
|
140
|
+
apiKey,
|
|
141
|
+
baseURL,
|
|
142
|
+
...otherOptions
|
|
143
|
+
})(selectedModel);
|
|
144
|
+
}
|
|
145
|
+
case _intlayer_types.AiProviders.HUGGINGFACE: {
|
|
146
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
147
|
+
const { createHuggingFace } = await loadModule("@ai-sdk/huggingface");
|
|
148
|
+
return createHuggingFace({
|
|
149
|
+
apiKey,
|
|
150
|
+
baseURL,
|
|
151
|
+
...otherOptions
|
|
152
|
+
})(selectedModel);
|
|
153
|
+
}
|
|
154
|
+
case _intlayer_types.AiProviders.BEDROCK: {
|
|
155
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
156
|
+
const { createAmazonBedrock } = await loadModule("@ai-sdk/amazon-bedrock");
|
|
157
|
+
return createAmazonBedrock({
|
|
158
|
+
accessKeyId: apiKey,
|
|
159
|
+
baseURL,
|
|
160
|
+
...otherOptions
|
|
161
|
+
})(selectedModel);
|
|
162
|
+
}
|
|
163
|
+
case _intlayer_types.AiProviders.TOGETHERAI: {
|
|
164
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
165
|
+
const { createTogetherAI } = await loadModule("@ai-sdk/togetherai");
|
|
166
|
+
return createTogetherAI({
|
|
167
|
+
apiKey,
|
|
168
|
+
baseURL,
|
|
169
|
+
...otherOptions
|
|
170
|
+
})(selectedModel);
|
|
171
|
+
}
|
|
172
|
+
default: throw new Error(`Provider ${provider} not supported`);
|
|
73
173
|
}
|
|
74
174
|
};
|
|
75
|
-
const DEFAULT_PROVIDER =
|
|
175
|
+
const DEFAULT_PROVIDER = _intlayer_types.AiProviders.OPENAI;
|
|
76
176
|
/**
|
|
77
177
|
* Get AI model configuration based on the selected provider and options
|
|
78
178
|
* This function handles the configuration for different AI providers
|
|
@@ -89,15 +189,15 @@ const getAIConfig = async (options, isAuthenticated = false) => {
|
|
|
89
189
|
...userOptions
|
|
90
190
|
};
|
|
91
191
|
const apiKey = getAPIKey(accessType, aiOptions, isAuthenticated);
|
|
92
|
-
if (!apiKey && aiOptions.provider !==
|
|
192
|
+
if (!apiKey && aiOptions.provider !== _intlayer_types.AiProviders.OLLAMA) throw new Error(`API key for ${aiOptions.provider} is missing`);
|
|
93
193
|
return {
|
|
94
|
-
model: getLanguageModel(aiOptions, apiKey, defaultOptions?.model),
|
|
194
|
+
model: await getLanguageModel(aiOptions, apiKey, defaultOptions?.model),
|
|
95
195
|
temperature: aiOptions.temperature,
|
|
96
196
|
dataSerialization: aiOptions.dataSerialization
|
|
97
197
|
};
|
|
98
198
|
};
|
|
99
199
|
|
|
100
200
|
//#endregion
|
|
101
|
-
exports.AIProvider =
|
|
201
|
+
exports.AIProvider = _intlayer_types.AiProviders;
|
|
102
202
|
exports.getAIConfig = getAIConfig;
|
|
103
203
|
//# sourceMappingURL=aiSdk.cjs.map
|
package/dist/cjs/aiSdk.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aiSdk.cjs","names":[],"sources":["../../src/aiSdk.ts"],"sourcesContent":["import { type anthropic, createAnthropic } from '@ai-sdk/anthropic';\nimport { createDeepSeek, type deepseek } from '@ai-sdk/deepseek';\nimport { createGoogleGenerativeAI, type google } from '@ai-sdk/google';\nimport { createMistral, type mistral } from '@ai-sdk/mistral';\nimport { createOpenAI, type openai } from '@ai-sdk/openai';\nimport type {\n AssistantModelMessage,\n generateText,\n SystemModelMessage,\n ToolModelMessage,\n UserModelMessage,\n} from 'ai';\n\ntype AnthropicModel = Parameters<typeof anthropic>[0];\ntype DeepSeekModel = Parameters<typeof deepseek>[0];\ntype MistralModel = Parameters<typeof mistral>[0];\ntype OpenAIModel = Parameters<typeof openai>[0];\ntype GoogleModel = Parameters<typeof google>[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 | GoogleModel\n | (string & {});\n\n/**\n * Supported AI SDK providers\n */\nexport enum AIProvider {\n OPENAI = 'openai',\n ANTHROPIC = 'anthropic',\n MISTRAL = 'mistral',\n DEEPSEEK = 'deepseek',\n GEMINI = 'gemini',\n OLLAMA = 'ollama',\n}\n\nexport type ReasoningEffort = 'minimal' | 'low' | 'medium' | 'high' | 'none';\n\n/**\n * Common options for all AI providers\n */\nexport type AIOptions = {\n provider?: AIProvider;\n model?: Model;\n temperature?: number;\n baseURL?: string;\n apiKey?: string;\n applicationContext?: string;\n dataSerialization?: 'json' | 'toon';\n};\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: AIProvider,\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 === AIProvider.OLLAMA) {\n if (provider === AIProvider.OPENAI) {\n return userModel ?? defaultModel;\n }\n\n if (userModel) {\n return userModel;\n }\n\n switch (provider) {\n case AIProvider.ANTHROPIC:\n return 'claude-sonnet-4-5-20250929';\n case AIProvider.MISTRAL:\n return 'mistral-large-latest';\n case AIProvider.DEEPSEEK:\n return 'deepseek-coder';\n case AIProvider.GEMINI:\n return 'gemini-2.5-flash';\n case AIProvider.OLLAMA:\n return '';\n default:\n return defaultModel;\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 = (\n aiOptions: AIOptions,\n apiKey: string | undefined,\n defaultModel?: Model\n) => {\n const selectedModel = getModelName(\n aiOptions.provider as AIProvider,\n apiKey,\n aiOptions.model,\n defaultModel\n );\n\n const baseURL = aiOptions.baseURL;\n\n switch (aiOptions.provider) {\n case AIProvider.OPENAI: {\n return createOpenAI({\n apiKey,\n baseURL,\n })(selectedModel);\n }\n\n case AIProvider.ANTHROPIC: {\n return createAnthropic({\n apiKey,\n baseURL,\n })(selectedModel);\n }\n\n case AIProvider.MISTRAL: {\n return createMistral({\n apiKey,\n baseURL,\n })(selectedModel);\n }\n\n case AIProvider.DEEPSEEK: {\n return createDeepSeek({\n apiKey,\n baseURL,\n })(selectedModel);\n }\n\n case AIProvider.GEMINI: {\n return createGoogleGenerativeAI({\n apiKey,\n baseURL,\n })(selectedModel);\n }\n\n case AIProvider.OLLAMA: {\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 });\n\n return ollama.chat(selectedModel);\n }\n\n default: {\n throw new Error(`Provider ${aiOptions.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: AIProvider = AIProvider.OPENAI as AIProvider;\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 } satisfies AIOptions;\n\n const apiKey = getAPIKey(accessType, aiOptions, isAuthenticated);\n\n // Check if API key is provided\n if (!apiKey && aiOptions.provider !== AIProvider.OLLAMA) {\n throw new Error(`API key for ${aiOptions.provider} is missing`);\n }\n\n const languageModel = 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"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;GACD,CAAC,CAAC,cAAc;EAGnB,KAAK,WAAW,OAOd,yCAL4B;GAC1B,SAAS,WAAW;GACpB,QAAQ,UAAU;GACnB,CAAC,CAEY,KAAK,cAAc;EAGnC,QACE,OAAM,IAAI,MAAM,YAAY,UAAU,SAAS,gBAAgB;;;AAWrE,MAAM,mBAA+B,WAAW;;;;;;;;AAgBhD,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,WAAW,OAC/C,OAAM,IAAI,MAAM,eAAe,UAAU,SAAS,aAAa;AASjE,QAAO;EACL,OAPoB,iBACpB,WACA,QACA,gBAAgB,MACjB;EAIC,aAAa,UAAU;EACvB,mBAAmB,UAAU;EAC9B"}
|
|
1
|
+
{"version":3,"file":"aiSdk.cjs","names":["AiProviders","x","ANSIColors"],"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,UAAU;;AAMhC,MAAM,gBACJ,UACA,YACA,WACA,eAAsB,iBACZ;AAEV,KAAI,cAAc,aAAaA,4BAAY,QAAQ;AACjD,MAAI,aAAaA,4BAAY,OAC3B,QAAO,aAAa;AAGtB,MAAI,UACF,QAAO;AAGT,UAAQ,UAAR;GACE,QACE,QAAO;;;AAKb,KAAI,aAAa,SACf,OAAM,IAAI,MACR,4DACD;AAGH,QAAO;;AAGT,MAAM,mBAAmB,OACvB,WACA,QACA,iBACG;CACH,MA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,CAAC,CAEgB,cAAwB;;EAG5C,QACE,OAAM,IAAI,MAAM,YAAY,SAAS,gBAAgB;;;AAW3D,MAAM,mBAAgCA,4BAAY;;;;;;;;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,aAAaA,4BAAY,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"}
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -5,8 +5,9 @@ const require_auditDictionaryMetadata_index = require('./auditDictionaryMetadata
|
|
|
5
5
|
const require_translateJSON_index = require('./translateJSON/index.cjs');
|
|
6
6
|
const require_utils_extractJSON = require('./utils/extractJSON.cjs');
|
|
7
7
|
let ai = require("ai");
|
|
8
|
+
let _intlayer_types = require("@intlayer/types");
|
|
8
9
|
|
|
9
|
-
exports.AIProvider =
|
|
10
|
+
exports.AIProvider = _intlayer_types.AiProviders;
|
|
10
11
|
exports.auditDictionaryMetadata = require_auditDictionaryMetadata_index.auditDictionaryMetadata;
|
|
11
12
|
exports.customQuery = require_customQuery.customQuery;
|
|
12
13
|
exports.extractJson = require_utils_extractJSON.extractJson;
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_aiSdk = require('../aiSdk.cjs');
|
|
3
2
|
const require__utils_asset = require('../_virtual/_utils_asset.cjs');
|
|
3
|
+
let _intlayer_types = require("@intlayer/types");
|
|
4
4
|
let ai = require("ai");
|
|
5
5
|
let zod = require("zod");
|
|
6
6
|
let _intlayer_core = require("@intlayer/core");
|
|
7
|
-
let _intlayer_types = require("@intlayer/types");
|
|
8
7
|
let _toon_format_toon = require("@toon-format/toon");
|
|
9
8
|
|
|
10
9
|
//#region src/translateJSON/index.ts
|
|
11
10
|
const aiDefaultOptions = {
|
|
12
|
-
provider:
|
|
11
|
+
provider: _intlayer_types.AiProviders.OPENAI,
|
|
13
12
|
model: "gpt-5-mini"
|
|
14
13
|
};
|
|
15
14
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["AIProvider","Locales","z","readAsset","Output"],"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.cjs","names":["AIProvider","Locales","z","readAsset","Output"],"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":";;;;;;;;;AA8BA,MAAa,mBAA8B;CACzC,UAAUA,4BAAW;CACrB,OAAO;CACR;;;;;;;AAQD,MAAM,wBAAwB,WAC5B,GAAG,OAAO,sCAAkB,QAAQC,wBAAQ,QAAQ;;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B;CAGvC,MAAM,SAAS,WACZ,QAAQ,mBAAmB,qBAAqB,CAChD,QAAQ,oBAAoB,sBAAsB,CAClD,QAAQ,2BAA2B,iBAAiB,CACpD,QAAQ,6BAA6B,yBAAyB,GAAG,CACjE,QAAQ,0BAA0B,sBAAsB,GAAG,CAC3D,QAAQ,wBAAwB,sBAAsB,QAAQ,EAAE,CAAC,CAAC,CAClE,QAAQ,wBAAwB,oBAAoB,KAAK,CAAC;AAE7D,KAAI,QAAQ;EACV,MAAM,EAAE,MAAM,UAAU,2BAAmB;GACzC,GAAG;GACH,UAAU,CACR;IAAE,MAAM;IAAU,SAAS;IAAQ,EACnC;IACE,MAAM;IACN,SAAS;KACP;KACA;KACA,eAAe;KACf,aAAa;KACb;KACA;KACA;KACD,CAAC,KAAK,KAAK;IACb,CACF;GACF,CAAC;AAOF,SAAO;GACL,2CALkB,KACjB,QAAQ,qCAAqC,KAAK,CAClD,MAAM,CAGyB;GAChC,WAAW,OAAO,eAAe;GAClC;;CAIH,MAAM,EAAE,QAAQ,UAAU,2BAAmB;EAC3C,GAAG;EACH,QAAQC,UAAO,OAAO,EACpB,QAAQ,UAAU,iBAAiB,EACpC,CAAC;EACF,UAAU,CACR;GAAE,MAAM;GAAU,SAAS;GAAQ,EACnC;GACE,MAAM;GAEN,SAAS;IACP;IACA;IACA,eAAe;IACf,aAAa;IACb;IACA;IACA;IACD,CAAC,KAAK,KAAK;GACb,CACF;EACF,CAAC;AAEF,QAAO;EACL,aAAa;EACb,WAAW,OAAO,eAAe;EAClC"}
|
package/dist/esm/aiSdk.mjs
CHANGED
|
@@ -1,22 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { createGoogleGenerativeAI } from "@ai-sdk/google";
|
|
4
|
-
import { createMistral } from "@ai-sdk/mistral";
|
|
5
|
-
import { createOpenAI } from "@ai-sdk/openai";
|
|
1
|
+
import { ANSIColors, colorize, logger, x } from "@intlayer/config";
|
|
2
|
+
import { AiProviders } from "@intlayer/types";
|
|
6
3
|
|
|
7
4
|
//#region src/aiSdk.ts
|
|
8
|
-
/**
|
|
9
|
-
* Supported AI SDK providers
|
|
10
|
-
*/
|
|
11
|
-
let AIProvider = /* @__PURE__ */ function(AIProvider) {
|
|
12
|
-
AIProvider["OPENAI"] = "openai";
|
|
13
|
-
AIProvider["ANTHROPIC"] = "anthropic";
|
|
14
|
-
AIProvider["MISTRAL"] = "mistral";
|
|
15
|
-
AIProvider["DEEPSEEK"] = "deepseek";
|
|
16
|
-
AIProvider["GEMINI"] = "gemini";
|
|
17
|
-
AIProvider["OLLAMA"] = "ollama";
|
|
18
|
-
return AIProvider;
|
|
19
|
-
}({});
|
|
20
5
|
const getAPIKey = (accessType, aiOptions, isAuthenticated = false) => {
|
|
21
6
|
const defaultApiKey = process.env.OPENAI_API_KEY;
|
|
22
7
|
if (accessType.includes("public")) return aiOptions?.apiKey ?? defaultApiKey;
|
|
@@ -25,53 +10,168 @@ const getAPIKey = (accessType, aiOptions, isAuthenticated = false) => {
|
|
|
25
10
|
if (accessType.includes("premium_user") && isAuthenticated) return aiOptions?.apiKey ?? defaultApiKey;
|
|
26
11
|
};
|
|
27
12
|
const getModelName = (provider, userApiKey, userModel, defaultModel = "gpt-5-mini") => {
|
|
28
|
-
if (userApiKey || provider ===
|
|
29
|
-
if (provider ===
|
|
13
|
+
if (userApiKey || provider === AiProviders.OLLAMA) {
|
|
14
|
+
if (provider === AiProviders.OPENAI) return userModel ?? defaultModel;
|
|
30
15
|
if (userModel) return userModel;
|
|
31
16
|
switch (provider) {
|
|
32
|
-
|
|
33
|
-
case AIProvider.MISTRAL: return "mistral-large-latest";
|
|
34
|
-
case AIProvider.DEEPSEEK: return "deepseek-coder";
|
|
35
|
-
case AIProvider.GEMINI: return "gemini-2.5-flash";
|
|
36
|
-
case AIProvider.OLLAMA: return "";
|
|
37
|
-
default: return defaultModel;
|
|
17
|
+
default: return "-";
|
|
38
18
|
}
|
|
39
19
|
}
|
|
40
20
|
if (userModel || provider) throw new Error("The user should use his own API key to use a custom model");
|
|
41
21
|
return defaultModel;
|
|
42
22
|
};
|
|
43
|
-
const getLanguageModel = (aiOptions, apiKey, defaultModel) => {
|
|
44
|
-
const
|
|
23
|
+
const getLanguageModel = async (aiOptions, apiKey, defaultModel) => {
|
|
24
|
+
const loadModule = async (packageName) => {
|
|
25
|
+
try {
|
|
26
|
+
return await import(packageName);
|
|
27
|
+
} catch {
|
|
28
|
+
logger(`${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)}`, { level: "error" });
|
|
29
|
+
process.exit();
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
const provider = aiOptions.provider ?? AiProviders.OPENAI;
|
|
33
|
+
const selectedModel = getModelName(provider, apiKey, aiOptions.model, defaultModel);
|
|
45
34
|
const baseURL = aiOptions.baseURL;
|
|
46
|
-
switch (
|
|
47
|
-
case
|
|
48
|
-
apiKey,
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
baseURL
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
35
|
+
switch (provider) {
|
|
36
|
+
case AiProviders.OPENAI: {
|
|
37
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
38
|
+
const { createOpenAI } = await loadModule("@ai-sdk/openai");
|
|
39
|
+
return createOpenAI({
|
|
40
|
+
apiKey,
|
|
41
|
+
baseURL,
|
|
42
|
+
...otherOptions
|
|
43
|
+
})(selectedModel);
|
|
44
|
+
}
|
|
45
|
+
case AiProviders.ANTHROPIC: {
|
|
46
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
47
|
+
const { createAnthropic } = await loadModule("@ai-sdk/anthropic");
|
|
48
|
+
return createAnthropic({
|
|
49
|
+
apiKey,
|
|
50
|
+
baseURL,
|
|
51
|
+
...otherOptions
|
|
52
|
+
})(selectedModel);
|
|
53
|
+
}
|
|
54
|
+
case AiProviders.MISTRAL: {
|
|
55
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
56
|
+
const { createMistral } = await loadModule("@ai-sdk/mistral");
|
|
57
|
+
return createMistral({
|
|
58
|
+
apiKey,
|
|
59
|
+
baseURL,
|
|
60
|
+
...otherOptions
|
|
61
|
+
})(selectedModel);
|
|
62
|
+
}
|
|
63
|
+
case AiProviders.DEEPSEEK: {
|
|
64
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
65
|
+
const { createDeepSeek } = await loadModule("@ai-sdk/deepseek");
|
|
66
|
+
return createDeepSeek({
|
|
67
|
+
apiKey,
|
|
68
|
+
baseURL,
|
|
69
|
+
...otherOptions
|
|
70
|
+
})(selectedModel);
|
|
71
|
+
}
|
|
72
|
+
case AiProviders.GEMINI: {
|
|
73
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
74
|
+
const { createGoogleGenerativeAI } = await loadModule("@ai-sdk/google");
|
|
75
|
+
return createGoogleGenerativeAI({
|
|
76
|
+
apiKey,
|
|
77
|
+
baseURL,
|
|
78
|
+
...otherOptions
|
|
79
|
+
})(selectedModel);
|
|
80
|
+
}
|
|
81
|
+
case AiProviders.GOOGLEVERTEX: {
|
|
82
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
83
|
+
const { createVertex } = await loadModule("@ai-sdk/google-vertex");
|
|
84
|
+
return createVertex({
|
|
85
|
+
apiKey,
|
|
86
|
+
baseURL,
|
|
87
|
+
...otherOptions
|
|
88
|
+
})(selectedModel);
|
|
89
|
+
}
|
|
90
|
+
case AiProviders.GOOGLEGENERATIVEAI: {
|
|
91
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
92
|
+
const { createGoogleGenerativeAI } = await loadModule("@ai-sdk/google");
|
|
93
|
+
return createGoogleGenerativeAI({
|
|
94
|
+
apiKey,
|
|
95
|
+
baseURL,
|
|
96
|
+
...otherOptions
|
|
97
|
+
})(selectedModel);
|
|
98
|
+
}
|
|
99
|
+
case AiProviders.OLLAMA: {
|
|
100
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
101
|
+
const { createOpenAI } = await loadModule("@ai-sdk/openai");
|
|
102
|
+
return createOpenAI({
|
|
103
|
+
baseURL: baseURL ?? "http://localhost:11434/v1",
|
|
104
|
+
apiKey: apiKey ?? "ollama",
|
|
105
|
+
...otherOptions
|
|
106
|
+
}).chat(selectedModel);
|
|
107
|
+
}
|
|
108
|
+
case AiProviders.OPENROUTER: {
|
|
109
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
110
|
+
const { createOpenRouter } = await loadModule("@openrouter/ai-sdk-provider");
|
|
111
|
+
return createOpenRouter({
|
|
112
|
+
apiKey,
|
|
113
|
+
baseURL,
|
|
114
|
+
...otherOptions
|
|
115
|
+
})(selectedModel);
|
|
116
|
+
}
|
|
117
|
+
case AiProviders.ALIBABA: {
|
|
118
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
119
|
+
const { createAlibaba } = await loadModule("@ai-sdk/alibaba");
|
|
120
|
+
return createAlibaba({
|
|
121
|
+
apiKey,
|
|
122
|
+
baseURL,
|
|
123
|
+
...otherOptions
|
|
124
|
+
})(selectedModel);
|
|
125
|
+
}
|
|
126
|
+
case AiProviders.FIREWORKS: {
|
|
127
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
128
|
+
const { createFireworks } = await loadModule("@ai-sdk/fireworks");
|
|
129
|
+
return createFireworks({
|
|
130
|
+
apiKey,
|
|
131
|
+
baseURL,
|
|
132
|
+
...otherOptions
|
|
133
|
+
})(selectedModel);
|
|
134
|
+
}
|
|
135
|
+
case AiProviders.GROQ: {
|
|
136
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
137
|
+
const { createGroq } = await loadModule("@ai-sdk/groq");
|
|
138
|
+
return createGroq({
|
|
139
|
+
apiKey,
|
|
140
|
+
baseURL,
|
|
141
|
+
...otherOptions
|
|
142
|
+
})(selectedModel);
|
|
143
|
+
}
|
|
144
|
+
case AiProviders.HUGGINGFACE: {
|
|
145
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
146
|
+
const { createHuggingFace } = await loadModule("@ai-sdk/huggingface");
|
|
147
|
+
return createHuggingFace({
|
|
148
|
+
apiKey,
|
|
149
|
+
baseURL,
|
|
150
|
+
...otherOptions
|
|
151
|
+
})(selectedModel);
|
|
152
|
+
}
|
|
153
|
+
case AiProviders.BEDROCK: {
|
|
154
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
155
|
+
const { createAmazonBedrock } = await loadModule("@ai-sdk/amazon-bedrock");
|
|
156
|
+
return createAmazonBedrock({
|
|
157
|
+
accessKeyId: apiKey,
|
|
158
|
+
baseURL,
|
|
159
|
+
...otherOptions
|
|
160
|
+
})(selectedModel);
|
|
161
|
+
}
|
|
162
|
+
case AiProviders.TOGETHERAI: {
|
|
163
|
+
const { provider, model, temperature, applicationContext, dataSerialization, apiKey: _apiKey, baseURL: _baseURL, ...otherOptions } = aiOptions;
|
|
164
|
+
const { createTogetherAI } = await loadModule("@ai-sdk/togetherai");
|
|
165
|
+
return createTogetherAI({
|
|
166
|
+
apiKey,
|
|
167
|
+
baseURL,
|
|
168
|
+
...otherOptions
|
|
169
|
+
})(selectedModel);
|
|
170
|
+
}
|
|
171
|
+
default: throw new Error(`Provider ${provider} not supported`);
|
|
72
172
|
}
|
|
73
173
|
};
|
|
74
|
-
const DEFAULT_PROVIDER =
|
|
174
|
+
const DEFAULT_PROVIDER = AiProviders.OPENAI;
|
|
75
175
|
/**
|
|
76
176
|
* Get AI model configuration based on the selected provider and options
|
|
77
177
|
* This function handles the configuration for different AI providers
|
|
@@ -88,14 +188,14 @@ const getAIConfig = async (options, isAuthenticated = false) => {
|
|
|
88
188
|
...userOptions
|
|
89
189
|
};
|
|
90
190
|
const apiKey = getAPIKey(accessType, aiOptions, isAuthenticated);
|
|
91
|
-
if (!apiKey && aiOptions.provider !==
|
|
191
|
+
if (!apiKey && aiOptions.provider !== AiProviders.OLLAMA) throw new Error(`API key for ${aiOptions.provider} is missing`);
|
|
92
192
|
return {
|
|
93
|
-
model: getLanguageModel(aiOptions, apiKey, defaultOptions?.model),
|
|
193
|
+
model: await getLanguageModel(aiOptions, apiKey, defaultOptions?.model),
|
|
94
194
|
temperature: aiOptions.temperature,
|
|
95
195
|
dataSerialization: aiOptions.dataSerialization
|
|
96
196
|
};
|
|
97
197
|
};
|
|
98
198
|
|
|
99
199
|
//#endregion
|
|
100
|
-
export { AIProvider, getAIConfig };
|
|
200
|
+
export { AiProviders as AIProvider, getAIConfig };
|
|
101
201
|
//# sourceMappingURL=aiSdk.mjs.map
|
package/dist/esm/aiSdk.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aiSdk.mjs","names":[],"sources":["../../src/aiSdk.ts"],"sourcesContent":["import { type anthropic, createAnthropic } from '@ai-sdk/anthropic';\nimport { createDeepSeek, type deepseek } from '@ai-sdk/deepseek';\nimport { createGoogleGenerativeAI, type google } from '@ai-sdk/google';\nimport { createMistral, type mistral } from '@ai-sdk/mistral';\nimport { createOpenAI, type openai } from '@ai-sdk/openai';\nimport type {\n AssistantModelMessage,\n generateText,\n SystemModelMessage,\n ToolModelMessage,\n UserModelMessage,\n} from 'ai';\n\ntype AnthropicModel = Parameters<typeof anthropic>[0];\ntype DeepSeekModel = Parameters<typeof deepseek>[0];\ntype MistralModel = Parameters<typeof mistral>[0];\ntype OpenAIModel = Parameters<typeof openai>[0];\ntype GoogleModel = Parameters<typeof google>[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 | GoogleModel\n | (string & {});\n\n/**\n * Supported AI SDK providers\n */\nexport enum AIProvider {\n OPENAI = 'openai',\n ANTHROPIC = 'anthropic',\n MISTRAL = 'mistral',\n DEEPSEEK = 'deepseek',\n GEMINI = 'gemini',\n OLLAMA = 'ollama',\n}\n\nexport type ReasoningEffort = 'minimal' | 'low' | 'medium' | 'high' | 'none';\n\n/**\n * Common options for all AI providers\n */\nexport type AIOptions = {\n provider?: AIProvider;\n model?: Model;\n temperature?: number;\n baseURL?: string;\n apiKey?: string;\n applicationContext?: string;\n dataSerialization?: 'json' | 'toon';\n};\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: AIProvider,\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 === AIProvider.OLLAMA) {\n if (provider === AIProvider.OPENAI) {\n return userModel ?? defaultModel;\n }\n\n if (userModel) {\n return userModel;\n }\n\n switch (provider) {\n case AIProvider.ANTHROPIC:\n return 'claude-sonnet-4-5-20250929';\n case AIProvider.MISTRAL:\n return 'mistral-large-latest';\n case AIProvider.DEEPSEEK:\n return 'deepseek-coder';\n case AIProvider.GEMINI:\n return 'gemini-2.5-flash';\n case AIProvider.OLLAMA:\n return '';\n default:\n return defaultModel;\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 = (\n aiOptions: AIOptions,\n apiKey: string | undefined,\n defaultModel?: Model\n) => {\n const selectedModel = getModelName(\n aiOptions.provider as AIProvider,\n apiKey,\n aiOptions.model,\n defaultModel\n );\n\n const baseURL = aiOptions.baseURL;\n\n switch (aiOptions.provider) {\n case AIProvider.OPENAI: {\n return createOpenAI({\n apiKey,\n baseURL,\n })(selectedModel);\n }\n\n case AIProvider.ANTHROPIC: {\n return createAnthropic({\n apiKey,\n baseURL,\n })(selectedModel);\n }\n\n case AIProvider.MISTRAL: {\n return createMistral({\n apiKey,\n baseURL,\n })(selectedModel);\n }\n\n case AIProvider.DEEPSEEK: {\n return createDeepSeek({\n apiKey,\n baseURL,\n })(selectedModel);\n }\n\n case AIProvider.GEMINI: {\n return createGoogleGenerativeAI({\n apiKey,\n baseURL,\n })(selectedModel);\n }\n\n case AIProvider.OLLAMA: {\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 });\n\n return ollama.chat(selectedModel);\n }\n\n default: {\n throw new Error(`Provider ${aiOptions.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: AIProvider = AIProvider.OPENAI as AIProvider;\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 } satisfies AIOptions;\n\n const apiKey = getAPIKey(accessType, aiOptions, isAuthenticated);\n\n // Check if API key is provided\n if (!apiKey && aiOptions.provider !== AIProvider.OLLAMA) {\n throw new Error(`API key for ${aiOptions.provider} is missing`);\n }\n\n const languageModel = 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":";;;;;;;;;;AAwCA,IAAY,kDAAL;AACL;AACA;AACA;AACA;AACA;AACA;;;AA2BF,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,UAAU;;AAMhC,MAAM,gBACJ,UACA,YACA,WACA,eAAsB,iBACZ;AAEV,KAAI,cAAc,aAAa,WAAW,QAAQ;AAChD,MAAI,aAAa,WAAW,OAC1B,QAAO,aAAa;AAGtB,MAAI,UACF,QAAO;AAGT,UAAQ,UAAR;GACE,KAAK,WAAW,UACd,QAAO;GACT,KAAK,WAAW,QACd,QAAO;GACT,KAAK,WAAW,SACd,QAAO;GACT,KAAK,WAAW,OACd,QAAO;GACT,KAAK,WAAW,OACd,QAAO;GACT,QACE,QAAO;;;AAKb,KAAI,aAAa,SACf,OAAM,IAAI,MACR,4DACD;AAGH,QAAO;;AAGT,MAAM,oBACJ,WACA,QACA,iBACG;CACH,MAAM,gBAAgB,aACpB,UAAU,UACV,QACA,UAAU,OACV,aACD;CAED,MAAM,UAAU,UAAU;AAE1B,SAAQ,UAAU,UAAlB;EACE,KAAK,WAAW,OACd,QAAO,aAAa;GAClB;GACA;GACD,CAAC,CAAC,cAAc;EAGnB,KAAK,WAAW,UACd,QAAO,gBAAgB;GACrB;GACA;GACD,CAAC,CAAC,cAAc;EAGnB,KAAK,WAAW,QACd,QAAO,cAAc;GACnB;GACA;GACD,CAAC,CAAC,cAAc;EAGnB,KAAK,WAAW,SACd,QAAO,eAAe;GACpB;GACA;GACD,CAAC,CAAC,cAAc;EAGnB,KAAK,WAAW,OACd,QAAO,yBAAyB;GAC9B;GACA;GACD,CAAC,CAAC,cAAc;EAGnB,KAAK,WAAW,OAOd,QALe,aAAa;GAC1B,SAAS,WAAW;GACpB,QAAQ,UAAU;GACnB,CAAC,CAEY,KAAK,cAAc;EAGnC,QACE,OAAM,IAAI,MAAM,YAAY,UAAU,SAAS,gBAAgB;;;AAWrE,MAAM,mBAA+B,WAAW;;;;;;;;AAgBhD,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,WAAW,OAC/C,OAAM,IAAI,MAAM,eAAe,UAAU,SAAS,aAAa;AASjE,QAAO;EACL,OAPoB,iBACpB,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';\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,UAAU;;AAMhC,MAAM,gBACJ,UACA,YACA,WACA,eAAsB,iBACZ;AAEV,KAAI,cAAc,aAAa,YAAY,QAAQ;AACjD,MAAI,aAAa,YAAY,OAC3B,QAAO,aAAa;AAGtB,MAAI,UACF,QAAO;AAGT,UAAQ,UAAR;GACE,QACE,QAAO;;;AAKb,KAAI,aAAa,SACf,OAAM,IAAI,MACR,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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"}
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { AIProvider, getAIConfig } from "./aiSdk.mjs";
|
|
1
|
+
import { AIProvider as AiProviders, getAIConfig } from "./aiSdk.mjs";
|
|
2
2
|
import { customQuery } from "./customQuery.mjs";
|
|
3
3
|
import { auditDictionaryMetadata } from "./auditDictionaryMetadata/index.mjs";
|
|
4
4
|
import { translateJSON } from "./translateJSON/index.mjs";
|
|
5
5
|
import { extractJson } from "./utils/extractJSON.mjs";
|
|
6
6
|
import { generateText, streamText } from "ai";
|
|
7
7
|
|
|
8
|
-
export { AIProvider, auditDictionaryMetadata, customQuery, extractJson, generateText, getAIConfig, streamText, translateJSON };
|
|
8
|
+
export { AiProviders as AIProvider, auditDictionaryMetadata, customQuery, extractJson, generateText, getAIConfig, streamText, translateJSON };
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { AIProvider } from "../aiSdk.mjs";
|
|
1
|
+
import { AIProvider as AiProviders } from "../aiSdk.mjs";
|
|
2
2
|
import { readAsset } from "../_virtual/_utils_asset.mjs";
|
|
3
|
+
import { Locales } from "@intlayer/types";
|
|
3
4
|
import { Output, generateText } from "ai";
|
|
4
5
|
import { z } from "zod";
|
|
5
6
|
import { getLocaleName } from "@intlayer/core";
|
|
6
|
-
import { Locales } from "@intlayer/types";
|
|
7
7
|
import { decode, encode } from "@toon-format/toon";
|
|
8
8
|
|
|
9
9
|
//#region src/translateJSON/index.ts
|
|
10
10
|
const aiDefaultOptions = {
|
|
11
|
-
provider:
|
|
11
|
+
provider: AiProviders.OPENAI,
|
|
12
12
|
model: "gpt-5-mini"
|
|
13
13
|
};
|
|
14
14
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"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":";;;;;;;;;AA8BA,MAAa,mBAA8B;CACzC,
|
|
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":";;;;;;;;;AA8BA,MAAa,mBAA8B;CACzC,UAAUA,YAAW;CACrB,OAAO;CACR;;;;;;;AAQD,MAA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ACpC,MAAM,mBAAmB,SACrB,OAAO,oBAAoB,GAC3B,KAAK,UAAU,oBAAoB;CAGvC,MAAM,SAAS,WACZ,QAAQ,mBAAmB,qBAAqB,CAChD,QAAQ,oBAAoB,sBAAsB,CAClD,QAAQ,2BAA2B,iBAAiB,CACpD,QAAQ,6BAA6B,yBAAyB,GAAG,CACjE,QAAQ,0BAA0B,sBAAsB,GAAG,CAC3D,QAAQ,wBAAwB,sBAAsB,QAAQ,EAAE,CAAC,CAAC,CAClE,QAAQ,wBAAwB,oBAAoB,KAAK,CAAC;AAE7D,KAAI,QAAQ;EACV,MAAM,EAAE,MAAM,UAAU,MAAM,aAAa;GACzC,GAAG;GACH,UAAU,CACR;IAAE,MAAM;IAAU,SAAS;IAAQ,EACnC;IACE,MAAM;IACN,SAAS;KACP;KACA;KACA,eAAe;KACf,aAAa;KACb;KACA;KACA;KACD,CAAC,KAAK,KAAK;IACb,CACF;GACF,CAAC;AAOF,SAAO;GACL,aAAa,OALK,KACjB,QAAQ,qCAAqC,KAAK,CAClD,MAAM,CAGyB;GAChC,WAAW,OAAO,eAAe;GAClC;;CAIH,MAAM,EAAE,QAAQ,UAAU,MAAM,aAAa;EAC3C,GAAG;EACH,QAAQ,OAAO,OAAO,EACpB,QAAQ,UAAU,iBAAiB,EACpC,CAAC;EACF,UAAU,CACR;GAAE,MAAM;GAAU,SAAS;GAAQ,EACnC;GACE,MAAM;GAEN,SAAS;IACP;IACA;IACA,eAAe;IACf,aAAa;IACb;IACA;IACA;IACD,CAAC,KAAK,KAAK;GACb,CACF;EACF,CAAC;AAEF,QAAO;EACL,aAAa;EACb,WAAW,OAAO,eAAe;EAClC"}
|
package/dist/types/aiSdk.d.ts
CHANGED
|
@@ -1,38 +1,59 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
1
|
+
import { AlibabaProvider, createAlibaba } from "@ai-sdk/alibaba";
|
|
2
|
+
import { AmazonBedrockProvider, createAmazonBedrock } from "@ai-sdk/amazon-bedrock";
|
|
3
|
+
import { AnthropicProvider, createAnthropic } from "@ai-sdk/anthropic";
|
|
4
|
+
import { DeepSeekProvider, createDeepSeek } from "@ai-sdk/deepseek";
|
|
5
|
+
import { FireworksProvider, createFireworks } from "@ai-sdk/fireworks";
|
|
6
|
+
import { GoogleGenerativeAIProvider, createGoogleGenerativeAI } from "@ai-sdk/google";
|
|
7
|
+
import { GoogleVertexProvider, createVertex } from "@ai-sdk/google-vertex";
|
|
8
|
+
import { GroqProvider, createGroq } from "@ai-sdk/groq";
|
|
9
|
+
import { HuggingFaceProvider, createHuggingFace } from "@ai-sdk/huggingface";
|
|
10
|
+
import { MistralProvider, createMistral } from "@ai-sdk/mistral";
|
|
11
|
+
import { OpenAIProvider, createOpenAI } from "@ai-sdk/openai";
|
|
12
|
+
import { TogetherAIProvider, createTogetherAI } from "@ai-sdk/togetherai";
|
|
13
|
+
import { AiProviders } from "@intlayer/types";
|
|
14
|
+
import { OpenRouterProvider, createOpenRouter } from "@openrouter/ai-sdk-provider";
|
|
6
15
|
import { AssistantModelMessage, SystemModelMessage, ToolModelMessage, UserModelMessage, generateText } from "ai";
|
|
7
16
|
|
|
8
17
|
//#region src/aiSdk.d.ts
|
|
9
|
-
type AnthropicModel = Parameters<
|
|
10
|
-
type DeepSeekModel = Parameters<
|
|
11
|
-
type MistralModel = Parameters<
|
|
12
|
-
type OpenAIModel = Parameters<
|
|
13
|
-
type
|
|
18
|
+
type AnthropicModel = Parameters<AnthropicProvider>[0];
|
|
19
|
+
type DeepSeekModel = Parameters<DeepSeekProvider>[0];
|
|
20
|
+
type MistralModel = Parameters<MistralProvider>[0];
|
|
21
|
+
type OpenAIModel = Parameters<OpenAIProvider>[0];
|
|
22
|
+
type OpenRouterModel = Parameters<OpenRouterProvider>[0];
|
|
23
|
+
type GoogleModel = Parameters<GoogleGenerativeAIProvider>[0];
|
|
24
|
+
type VertexModel = Parameters<GoogleVertexProvider>[0];
|
|
25
|
+
type AlibabaModel = Parameters<AlibabaProvider>[0];
|
|
26
|
+
type AmazonBedrockModel = Parameters<AmazonBedrockProvider>[0];
|
|
27
|
+
type FireworksModel = Parameters<FireworksProvider>[0];
|
|
28
|
+
type GroqModel = Parameters<GroqProvider>[0];
|
|
29
|
+
type HuggingFaceModel = Parameters<HuggingFaceProvider>[0];
|
|
30
|
+
type TogetherAIModel = Parameters<TogetherAIProvider>[0];
|
|
31
|
+
type OpenAIProviderOptions = Parameters<typeof createOpenAI>[0];
|
|
32
|
+
type AnthropicProviderOptions = Parameters<typeof createAnthropic>[0];
|
|
33
|
+
type MistralProviderOptions = Parameters<typeof createMistral>[0];
|
|
34
|
+
type DeepSeekProviderOptions = Parameters<typeof createDeepSeek>[0];
|
|
35
|
+
type GoogleProviderOptions = Parameters<typeof createGoogleGenerativeAI>[0];
|
|
36
|
+
type VertexProviderOptions = Parameters<typeof createVertex>[0];
|
|
37
|
+
type OpenRouterProviderOptions = Parameters<typeof createOpenRouter>[0];
|
|
38
|
+
type AlibabaProviderOptions = Parameters<typeof createAlibaba>[0];
|
|
39
|
+
type FireworksProviderOptions = Parameters<typeof createFireworks>[0];
|
|
40
|
+
type GroqProviderOptions = Parameters<typeof createGroq>[0];
|
|
41
|
+
type HuggingFaceProviderOptions = Parameters<typeof createHuggingFace>[0];
|
|
42
|
+
type AmazonBedrockProviderOptions = Parameters<typeof createAmazonBedrock>[0];
|
|
43
|
+
type TogetherAIProviderOptions = Parameters<typeof createTogetherAI>[0];
|
|
14
44
|
type Messages = (SystemModelMessage | UserModelMessage | AssistantModelMessage | ToolModelMessage)[];
|
|
15
45
|
/**
|
|
16
46
|
* Supported AI models
|
|
17
47
|
*/
|
|
18
|
-
type Model = AnthropicModel | DeepSeekModel | MistralModel | OpenAIModel | GoogleModel | (string & {});
|
|
48
|
+
type Model = AnthropicModel | DeepSeekModel | MistralModel | OpenAIModel | OpenRouterModel | GoogleModel | VertexModel | AlibabaModel | AmazonBedrockModel | FireworksModel | GroqModel | HuggingFaceModel | TogetherAIModel | (string & {});
|
|
19
49
|
/**
|
|
20
50
|
* Supported AI SDK providers
|
|
21
51
|
*/
|
|
22
|
-
declare enum AIProvider {
|
|
23
|
-
OPENAI = "openai",
|
|
24
|
-
ANTHROPIC = "anthropic",
|
|
25
|
-
MISTRAL = "mistral",
|
|
26
|
-
DEEPSEEK = "deepseek",
|
|
27
|
-
GEMINI = "gemini",
|
|
28
|
-
OLLAMA = "ollama"
|
|
29
|
-
}
|
|
30
52
|
type ReasoningEffort = 'minimal' | 'low' | 'medium' | 'high' | 'none';
|
|
31
53
|
/**
|
|
32
54
|
* Common options for all AI providers
|
|
33
55
|
*/
|
|
34
|
-
type
|
|
35
|
-
provider?: AIProvider;
|
|
56
|
+
type CommonAIOptions = {
|
|
36
57
|
model?: Model;
|
|
37
58
|
temperature?: number;
|
|
38
59
|
baseURL?: string;
|
|
@@ -40,6 +61,39 @@ type AIOptions = {
|
|
|
40
61
|
applicationContext?: string;
|
|
41
62
|
dataSerialization?: 'json' | 'toon';
|
|
42
63
|
};
|
|
64
|
+
type AIOptions = (({
|
|
65
|
+
provider: AiProviders.OPENAI | `${AiProviders.OPENAI}`;
|
|
66
|
+
} & OpenAIProviderOptions) | ({
|
|
67
|
+
provider: AiProviders.ANTHROPIC | `${AiProviders.ANTHROPIC}`;
|
|
68
|
+
} & AnthropicProviderOptions) | ({
|
|
69
|
+
provider: AiProviders.MISTRAL | `${AiProviders.MISTRAL}`;
|
|
70
|
+
} & MistralProviderOptions) | ({
|
|
71
|
+
provider: AiProviders.DEEPSEEK | `${AiProviders.DEEPSEEK}`;
|
|
72
|
+
} & DeepSeekProviderOptions) | ({
|
|
73
|
+
provider: AiProviders.GEMINI | `${AiProviders.GEMINI}`;
|
|
74
|
+
} & GoogleProviderOptions) | ({
|
|
75
|
+
provider: AiProviders.GOOGLEGENERATIVEAI | `${AiProviders.GOOGLEGENERATIVEAI}`;
|
|
76
|
+
} & GoogleProviderOptions) | ({
|
|
77
|
+
provider: AiProviders.OLLAMA | `${AiProviders.OLLAMA}`;
|
|
78
|
+
} & OpenAIProviderOptions) | ({
|
|
79
|
+
provider: AiProviders.OPENROUTER | `${AiProviders.OPENROUTER}`;
|
|
80
|
+
} & OpenRouterProviderOptions) | ({
|
|
81
|
+
provider: AiProviders.ALIBABA | `${AiProviders.ALIBABA}`;
|
|
82
|
+
} & AlibabaProviderOptions) | ({
|
|
83
|
+
provider: AiProviders.FIREWORKS | `${AiProviders.FIREWORKS}`;
|
|
84
|
+
} & FireworksProviderOptions) | ({
|
|
85
|
+
provider: AiProviders.GROQ | `${AiProviders.GROQ}`;
|
|
86
|
+
} & GroqProviderOptions) | ({
|
|
87
|
+
provider: AiProviders.HUGGINGFACE | `${AiProviders.HUGGINGFACE}`;
|
|
88
|
+
} & HuggingFaceProviderOptions) | ({
|
|
89
|
+
provider: AiProviders.BEDROCK | `${AiProviders.BEDROCK}`;
|
|
90
|
+
} & AmazonBedrockProviderOptions) | ({
|
|
91
|
+
provider: AiProviders.GOOGLEVERTEX | `${AiProviders.GOOGLEVERTEX}`;
|
|
92
|
+
} & VertexProviderOptions) | ({
|
|
93
|
+
provider: AiProviders.TOGETHERAI | `${AiProviders.TOGETHERAI}`;
|
|
94
|
+
} & TogetherAIProviderOptions) | ({
|
|
95
|
+
provider?: undefined;
|
|
96
|
+
} & OpenAIProviderOptions)) & CommonAIOptions;
|
|
43
97
|
type ChatCompletionRequestMessage = {
|
|
44
98
|
role: 'system' | 'user' | 'assistant';
|
|
45
99
|
content: string;
|
|
@@ -66,5 +120,5 @@ type AIConfigOptions = {
|
|
|
66
120
|
*/
|
|
67
121
|
declare const getAIConfig: (options: AIConfigOptions, isAuthenticated?: boolean) => Promise<AIConfig>;
|
|
68
122
|
//#endregion
|
|
69
|
-
export { AIConfig, AIConfigOptions, AIOptions, AIProvider, ChatCompletionRequestMessage, Messages, Model, ReasoningEffort, getAIConfig };
|
|
123
|
+
export { AIConfig, AIConfigOptions, AIOptions, AiProviders as AIProvider, AlibabaProviderOptions, AmazonBedrockProviderOptions, AnthropicProviderOptions, ChatCompletionRequestMessage, DeepSeekProviderOptions, FireworksProviderOptions, GoogleProviderOptions, GroqProviderOptions, HuggingFaceProviderOptions, Messages, MistralProviderOptions, Model, OpenAIProviderOptions, OpenRouterProviderOptions, ReasoningEffort, TogetherAIProviderOptions, VertexProviderOptions, getAIConfig };
|
|
70
124
|
//# sourceMappingURL=aiSdk.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aiSdk.d.ts","names":[],"sources":["../../src/aiSdk.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"aiSdk.d.ts","names":[],"sources":["../../src/aiSdk.ts"],"mappings":";;;;;;;;;;;;;;;;;KAwCK,cAAA,GAAiB,UAAA,CAAW,iBAAA;AAAA,KAC5B,aAAA,GAAgB,UAAA,CAAW,gBAAA;AAAA,KAC3B,YAAA,GAAe,UAAA,CAAW,eAAA;AAAA,KAC1B,WAAA,GAAc,UAAA,CAAW,cAAA;AAAA,KACzB,eAAA,GAAkB,UAAA,CAAW,kBAAA;AAAA,KAC7B,WAAA,GAAc,UAAA,CAAW,0BAAA;AAAA,KACzB,WAAA,GAAc,UAAA,CAAW,oBAAA;AAAA,KACzB,YAAA,GAAe,UAAA,CAAW,eAAA;AAAA,KAC1B,kBAAA,GAAqB,UAAA,CAAW,qBAAA;AAAA,KAChC,cAAA,GAAiB,UAAA,CAAW,iBAAA;AAAA,KAC5B,SAAA,GAAY,UAAA,CAAW,YAAA;AAAA,KACvB,gBAAA,GAAmB,UAAA,CAAW,mBAAA;AAAA,KAC9B,eAAA,GAAkB,UAAA,CAAW,kBAAA;AAAA,KAEtB,qBAAA,GAAwB,UAAA,QAAkB,YAAA;AAAA,KAC1C,wBAAA,GAA2B,UAAA,QAAkB,eAAA;AAAA,KAC7C,sBAAA,GAAyB,UAAA,QAAkB,aAAA;AAAA,KAC3C,uBAAA,GAA0B,UA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AAA,eACC,OAAA,CAAQ,QAAA"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,7 +1,29 @@
|
|
|
1
|
-
import { AIConfig, AIConfigOptions, AIOptions, AIProvider, ChatCompletionRequestMessage, Messages, Model, ReasoningEffort, getAIConfig } from "./aiSdk.js";
|
|
1
|
+
import { AIConfig, AIConfigOptions, AIOptions, AIProvider as AiProviders, AlibabaProviderOptions, AmazonBedrockProviderOptions, AnthropicProviderOptions, ChatCompletionRequestMessage, DeepSeekProviderOptions, FireworksProviderOptions, GoogleProviderOptions, GroqProviderOptions, HuggingFaceProviderOptions, Messages, MistralProviderOptions, Model, OpenAIProviderOptions, OpenRouterProviderOptions, ReasoningEffort, TogetherAIProviderOptions, VertexProviderOptions, getAIConfig } from "./aiSdk.js";
|
|
2
2
|
import { AuditDictionaryMetadataOptions, AuditFileResultData, auditDictionaryMetadata } from "./auditDictionaryMetadata/index.js";
|
|
3
3
|
import { CustomQueryOptions, CustomQueryResultData, customQuery } from "./customQuery.js";
|
|
4
4
|
import { TranslateJSONOptions, TranslateJSONResultData, translateJSON } from "./translateJSON/index.js";
|
|
5
5
|
import { extractJson } from "./utils/extractJSON.js";
|
|
6
6
|
import { generateText, streamText } from "ai";
|
|
7
|
-
|
|
7
|
+
|
|
8
|
+
//#region src/index.d.ts
|
|
9
|
+
declare module '@intlayer/types' {
|
|
10
|
+
interface AiProviderConfigMap {
|
|
11
|
+
openai: OpenAIProviderOptions;
|
|
12
|
+
anthropic: AnthropicProviderOptions;
|
|
13
|
+
mistral: MistralProviderOptions;
|
|
14
|
+
deepseek: DeepSeekProviderOptions;
|
|
15
|
+
gemini: GoogleProviderOptions;
|
|
16
|
+
googlevertex: VertexProviderOptions;
|
|
17
|
+
ollama: OpenAIProviderOptions;
|
|
18
|
+
openrouter: OpenRouterProviderOptions;
|
|
19
|
+
alibaba: AlibabaProviderOptions;
|
|
20
|
+
fireworks: FireworksProviderOptions;
|
|
21
|
+
groq: GroqProviderOptions;
|
|
22
|
+
huggingface: HuggingFaceProviderOptions;
|
|
23
|
+
bedrock: AmazonBedrockProviderOptions;
|
|
24
|
+
togetherai: TogetherAIProviderOptions;
|
|
25
|
+
}
|
|
26
|
+
} //# sourceMappingURL=index.d.ts.map
|
|
27
|
+
//#endregion
|
|
28
|
+
export { AIConfig, AIConfigOptions, AIOptions, AiProviders as AIProvider, AlibabaProviderOptions, AmazonBedrockProviderOptions, AnthropicProviderOptions, type AuditDictionaryMetadataOptions, type AuditFileResultData, ChatCompletionRequestMessage, type CustomQueryOptions, type CustomQueryResultData, DeepSeekProviderOptions, FireworksProviderOptions, GoogleProviderOptions, GroqProviderOptions, HuggingFaceProviderOptions, Messages, MistralProviderOptions, Model, OpenAIProviderOptions, OpenRouterProviderOptions, ReasoningEffort, TogetherAIProviderOptions, type TranslateJSONOptions, type TranslateJSONResultData, VertexProviderOptions, auditDictionaryMetadata, customQuery, extractJson, generateText, getAIConfig, streamText, translateJSON };
|
|
29
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/index.ts"],"mappings":";;;;;;;;;YAqCY,mBAAA;IACR,MAAA,EAAQ,qBAAA;IACR,SAAA,EAAW,wBAAA;IACX,OAAA,EAAS,sBAAA;IACT,QAAA,EAAU,uBAAA;IACV,MAAA,EAAQ,qBAAA;IACR,YAAA,EAAc,qBAAA;IACd,MAAA,EAAQ,qBAAA;IACR,UAAA,EAAY,yBAAA;IACZ,OAAA,EAAS,sBAAA;IACT,SAAA,EAAW,wBAAA;IACX,IAAA,EAAM,mBAAA;IACN,WAAA,EAAa,0BAAA;IACb,OAAA,EAAS,4BAAA;IACT,UAAA,EAAY,yBAAA;EAAA;AAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intlayer/ai",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.1.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "SDK that provides AI capabilities for Intlayer applications",
|
|
6
6
|
"keywords": [
|
|
@@ -73,21 +73,30 @@
|
|
|
73
73
|
"typecheck": "tsc --noEmit --project tsconfig.types.json"
|
|
74
74
|
},
|
|
75
75
|
"dependencies": {
|
|
76
|
-
"@ai-sdk/anthropic": "3.0.
|
|
77
|
-
"@ai-sdk/
|
|
78
|
-
"@ai-sdk/
|
|
79
|
-
"@
|
|
80
|
-
"@
|
|
81
|
-
"@intlayer/
|
|
82
|
-
"@intlayer/
|
|
83
|
-
"@
|
|
84
|
-
"
|
|
85
|
-
"@toon-format/toon": "^2.1.0",
|
|
86
|
-
"ai": "6.0.31",
|
|
76
|
+
"@ai-sdk/anthropic": "3.0.43",
|
|
77
|
+
"@ai-sdk/google": "3.0.29",
|
|
78
|
+
"@ai-sdk/openai": "3.0.28",
|
|
79
|
+
"@intlayer/api": "8.1.0",
|
|
80
|
+
"@intlayer/config": "8.1.0",
|
|
81
|
+
"@intlayer/core": "8.1.0",
|
|
82
|
+
"@intlayer/types": "8.1.0",
|
|
83
|
+
"@toon-format/toon": "2.1.0",
|
|
84
|
+
"ai": "6.0.84",
|
|
87
85
|
"zod": "4.3.6"
|
|
88
86
|
},
|
|
89
87
|
"devDependencies": {
|
|
90
|
-
"@
|
|
88
|
+
"@ai-sdk/alibaba": "1.0.4",
|
|
89
|
+
"@ai-sdk/amazon-bedrock": "4.0.59",
|
|
90
|
+
"@ai-sdk/deepseek": "2.0.20",
|
|
91
|
+
"@ai-sdk/fireworks": "2.0.34",
|
|
92
|
+
"@ai-sdk/google": "3.0.29",
|
|
93
|
+
"@ai-sdk/google-vertex": "^4.0.57",
|
|
94
|
+
"@ai-sdk/groq": "3.0.24",
|
|
95
|
+
"@ai-sdk/huggingface": "1.0.32",
|
|
96
|
+
"@ai-sdk/mistral": "3.0.20",
|
|
97
|
+
"@ai-sdk/togetherai": "2.0.33",
|
|
98
|
+
"@openrouter/ai-sdk-provider": "2.2.3",
|
|
99
|
+
"@types/node": "25.2.3",
|
|
91
100
|
"@utils/ts-config": "1.0.4",
|
|
92
101
|
"@utils/ts-config-types": "1.0.4",
|
|
93
102
|
"@utils/tsdown-config": "1.0.4",
|
|
@@ -96,6 +105,54 @@
|
|
|
96
105
|
"typescript": "5.9.3",
|
|
97
106
|
"vitest": "4.0.18"
|
|
98
107
|
},
|
|
108
|
+
"peerDependencies": {
|
|
109
|
+
"@ai-sdk/alibaba": "1.0.4",
|
|
110
|
+
"@ai-sdk/amazon-bedrock": "4.0.59",
|
|
111
|
+
"@ai-sdk/deepseek": "2.0.20",
|
|
112
|
+
"@ai-sdk/fireworks": "2.0.34",
|
|
113
|
+
"@ai-sdk/google": "3.0.29",
|
|
114
|
+
"@ai-sdk/google-vertex": "^4.0.57",
|
|
115
|
+
"@ai-sdk/groq": "3.0.24",
|
|
116
|
+
"@ai-sdk/huggingface": "1.0.32",
|
|
117
|
+
"@ai-sdk/mistral": "3.0.20",
|
|
118
|
+
"@ai-sdk/togetherai": "2.0.33",
|
|
119
|
+
"@openrouter/ai-sdk-provider": "2.2.3"
|
|
120
|
+
},
|
|
121
|
+
"peerDependenciesMeta": {
|
|
122
|
+
"@ai-sdk/alibaba": {
|
|
123
|
+
"optional": true
|
|
124
|
+
},
|
|
125
|
+
"@ai-sdk/amazon-bedrock": {
|
|
126
|
+
"optional": true
|
|
127
|
+
},
|
|
128
|
+
"@ai-sdk/deepseek": {
|
|
129
|
+
"optional": true
|
|
130
|
+
},
|
|
131
|
+
"@ai-sdk/fireworks": {
|
|
132
|
+
"optional": true
|
|
133
|
+
},
|
|
134
|
+
"@ai-sdk/google": {
|
|
135
|
+
"optional": true
|
|
136
|
+
},
|
|
137
|
+
"@ai-sdk/google-vertex": {
|
|
138
|
+
"optional": true
|
|
139
|
+
},
|
|
140
|
+
"@ai-sdk/groq": {
|
|
141
|
+
"optional": true
|
|
142
|
+
},
|
|
143
|
+
"@ai-sdk/huggingface": {
|
|
144
|
+
"optional": true
|
|
145
|
+
},
|
|
146
|
+
"@ai-sdk/mistral": {
|
|
147
|
+
"optional": true
|
|
148
|
+
},
|
|
149
|
+
"@ai-sdk/togetherai": {
|
|
150
|
+
"optional": true
|
|
151
|
+
},
|
|
152
|
+
"@openrouter/ai-sdk-provider": {
|
|
153
|
+
"optional": true
|
|
154
|
+
}
|
|
155
|
+
},
|
|
99
156
|
"engines": {
|
|
100
157
|
"node": ">=14.18"
|
|
101
158
|
},
|