@juspay/neurolink
Version:
Universal AI Development Platform with working MCP integration, multi-provider support, and professional CLI. Built-in tools operational, 58+ external MCP servers discoverable. Connect to filesystem, GitHub, database operations, and more. Build, test, and
128 lines (127 loc) • 5.28 kB
JavaScript
import { createGoogleGenerativeAI } from "@ai-sdk/google";
import { streamText, Output } from "ai";
import { GoogleAIModels } from "../core/types.js";
import { BaseProvider } from "../core/baseProvider.js";
import { logger } from "../utils/logger.js";
import { createTimeoutController, TimeoutError, getDefaultTimeout, } from "../utils/timeout.js";
import { DEFAULT_MAX_TOKENS } from "../core/constants.js";
import { createProxyFetch } from "../proxy/proxyFetch.js";
import { streamAnalyticsCollector } from "../core/streamAnalytics.js";
// Environment variable setup
if (!process.env.GOOGLE_GENERATIVE_AI_API_KEY &&
process.env.GOOGLE_AI_API_KEY) {
process.env.GOOGLE_GENERATIVE_AI_API_KEY = process.env.GOOGLE_AI_API_KEY;
}
/**
* Google AI Studio provider implementation using BaseProvider
* Migrated from original GoogleAIStudio class to new factory pattern
*/
export class GoogleAIStudioProvider extends BaseProvider {
constructor(modelName, sdk) {
super(modelName, "google-ai", sdk);
logger.debug("GoogleAIStudioProvider initialized", {
model: this.modelName,
provider: this.providerName,
sdkProvided: !!sdk,
});
}
// ===================
// ABSTRACT METHOD IMPLEMENTATIONS
// ===================
getProviderName() {
return "google-ai";
}
getDefaultModel() {
return process.env.GOOGLE_AI_MODEL || GoogleAIModels.GEMINI_2_5_FLASH;
}
/**
* 🔧 PHASE 2: Return AI SDK model instance for tool calling
*/
getAISDKModel() {
const apiKey = this.getApiKey();
const google = createGoogleGenerativeAI({ apiKey });
return google(this.modelName);
}
handleProviderError(error) {
if (error instanceof TimeoutError) {
return new Error(`Google AI request timed out: ${error.message}`);
}
const errorRecord = error;
if (typeof errorRecord?.message === "string" &&
errorRecord.message.includes("API_KEY_INVALID")) {
return new Error("Invalid Google AI API key. Please check your GOOGLE_AI_API_KEY environment variable.");
}
if (typeof errorRecord?.message === "string" &&
errorRecord.message.includes("RATE_LIMIT_EXCEEDED")) {
return new Error("Google AI rate limit exceeded. Please try again later.");
}
const message = typeof errorRecord?.message === "string"
? errorRecord.message
: "Unknown error";
return new Error(`Google AI error: ${message}`);
}
// executeGenerate removed - BaseProvider handles all generation with tools
async executeStream(options, analysisSchema) {
this.validateStreamOptions(options);
const startTime = Date.now();
const apiKey = this.getApiKey();
const google = createGoogleGenerativeAI({ apiKey });
const model = google(this.modelName);
const timeout = this.getTimeout(options);
const timeoutController = createTimeoutController(timeout, this.providerName, "stream");
try {
const result = await streamText({
model,
prompt: options.input.text,
system: options.systemPrompt,
temperature: options.temperature,
maxTokens: options.maxTokens || DEFAULT_MAX_TOKENS,
tools: options.tools,
toolChoice: "auto",
abortSignal: timeoutController?.controller.signal,
});
timeoutController?.cleanup();
// Transform string stream to content object stream
const transformedStream = async function* () {
for await (const chunk of result.textStream) {
yield { content: chunk };
}
};
// Create analytics promise that resolves after stream completion
const analyticsPromise = streamAnalyticsCollector.createAnalytics(this.providerName, this.modelName, result, Date.now() - startTime, {
requestId: `google-ai-stream-${Date.now()}`,
streamingMode: true,
});
return {
stream: transformedStream(),
provider: this.providerName,
model: this.modelName,
analytics: analyticsPromise,
metadata: {
startTime,
streamId: `google-ai-${Date.now()}`,
},
};
}
catch (error) {
timeoutController?.cleanup();
throw this.handleProviderError(error);
}
}
// ===================
// HELPER METHODS
// ===================
getApiKey() {
const apiKey = process.env.GOOGLE_AI_API_KEY || process.env.GOOGLE_GENERATIVE_AI_API_KEY;
if (!apiKey) {
throw new Error("GOOGLE_AI_API_KEY or GOOGLE_GENERATIVE_AI_API_KEY environment variable is not set");
}
return apiKey;
}
validateStreamOptions(options) {
if (!options.input?.text || options.input.text.trim().length === 0) {
throw new Error("Input text is required and cannot be empty");
}
}
}
export default GoogleAIStudioProvider;