cloud-code-ai-provider
Version:
Google Cloud Code provider for the AI SDK
153 lines (148 loc) • 5.5 kB
TypeScript
import { ProviderV1, LanguageModelV1 } from '@ai-sdk/provider';
import { Resolvable, FetchFunction } from '@ai-sdk/provider-utils';
import { OAuth2Client, Credentials } from 'google-auth-library';
type GoogleCloudCodeModelId = 'gemini-2.5-flash' | 'gemini-2.5-pro' | (string & {});
interface GoogleCloudCodeSettings {
/**
* Optional. The maximum number of tokens to consider when sampling.
*
* Models use nucleus sampling or combined Top-k and nucleus sampling.
* Top-k sampling considers the set of topK most probable tokens.
* Models running with nucleus sampling don't allow topK setting.
*/
topK?: number;
/**
* A list of unique safety settings for blocking unsafe content.
*/
safetySettings?: Array<{
category: string;
threshold: string;
}>;
/**
* Enable using the web grounding service and search for grounding sources in the response.
*
* Default: false (Google AI) or true (Vertex AI)
*/
useSearchGrounding?: boolean;
}
interface GoogleCloudCodeProvider extends ProviderV1 {
(modelId: GoogleCloudCodeModelId, settings?: GoogleCloudCodeSettings): LanguageModelV1;
languageModel(modelId: GoogleCloudCodeModelId, settings?: GoogleCloudCodeSettings): LanguageModelV1;
}
interface GoogleCloudCodeProviderSettings {
/**
* OAuth access token for authentication.
* If not provided, the provider will attempt to use OAuth authentication.
*/
accessToken?: Resolvable<string | undefined>;
/**
* Google Cloud project ID.
* If not provided, the provider will attempt to retrieve it automatically.
*/
projectId?: Resolvable<string | undefined>;
/**
* OAuth credentials for automatic authentication.
* If provided, the provider will use these credentials to obtain access tokens.
*/
credentials?: {
access_token: string;
refresh_token?: string;
expiry_date?: number;
};
/**
* Whether to use OAuth authentication.
* If true and no credentials are provided, you must authenticate separately.
* Defaults to true if no accessToken is provided.
*/
useOAuth?: boolean;
/**
* Custom directory for storing OAuth credentials.
* Defaults to ~/.gemini
* Can also be overridden with GOOGLE_APPLICATION_CREDENTIALS environment variable.
*/
credentialDirectory?: string;
/**
* Base URL for the Cloud Code API.
* Defaults to https://cloudcode-pa.googleapis.com
*/
baseURL?: string;
/**
* Custom headers to include in requests.
*/
headers?: Resolvable<Record<string, string | undefined>>;
/**
* Custom fetch implementation.
*/
fetch?: FetchFunction;
}
/**
* Create a Google Cloud Code provider instance.
*/
declare function createGoogleCloudCode(options?: GoogleCloudCodeProviderSettings): GoogleCloudCodeProvider;
/**
* Default Google Cloud Code provider instance.
*/
declare const googleCloudCode: GoogleCloudCodeProvider;
declare class GoogleCloudCodeAuth {
private static oauthClient;
private static cachedProjectId;
private static customCredentialDir;
static getOAuthClient(): Promise<OAuth2Client>;
static setCredentials(credentials: Credentials): Promise<void>;
static getAccessToken(): Promise<string | undefined>;
static generateAuthUrl(redirectUri: string, state: string): string;
private static callEndpoint;
private static loadCodeAssist;
private static onboardUser;
private static getClientMetadata;
private static getPlatform;
static setupUser(): Promise<string>;
static getProjectId(): Promise<string>;
static clearCache(): Promise<void>;
/**
* Complete OAuth authentication flow with browser
* Opens browser for authentication and handles the OAuth callback
* @param options - Optional configuration for the auth flow
* @returns Promise that resolves when authentication is complete
*/
static authenticate(options?: {
/** Force re-authentication even if already authenticated */
force?: boolean;
/** Custom success redirect URL */
successUrl?: string;
/** Custom failure redirect URL */
failureUrl?: string;
/** Skip browser opening */
skipBrowser?: boolean;
/** Custom directory for storing credentials */
credentialDirectory?: string;
}): Promise<void>;
/**
* Check if the user is authenticated
* @returns True if authenticated with valid credentials
*/
static isAuthenticated(): Promise<boolean>;
/**
* Get information about the authenticated user
* @returns User information including email
*/
static getUserInfo(): Promise<{
email?: string;
name?: string;
picture?: string;
}>;
/**
* Find an available port for the OAuth callback server
*/
private static getAvailablePort;
/**
* Create OAuth callback server
*/
private static createOAuthCallbackServer;
static setCredentialDirectory(directory: string): void;
static getCredentialDirectory(): string;
private static getCachedCredentialPath;
private static loadCachedCredentials;
private static cacheCredentials;
}
export { GoogleCloudCodeAuth, type GoogleCloudCodeModelId, type GoogleCloudCodeProvider, type GoogleCloudCodeProviderSettings, type GoogleCloudCodeSettings, createGoogleCloudCode, googleCloudCode };