/** * @license * Copyright 2025 Google LLC * SPDX-License-Identifier: Apache-2.0 */ import { OAuth2Client } from 'google-auth-library'; import { CodeAssistGlobalUserSettingResponse, LoadCodeAssistRequest, LoadCodeAssistResponse, LongRunningOperationResponse, OnboardUserRequest, SetCodeAssistGlobalUserSettingRequest, } from './types.js'; import { CountTokensParameters, CountTokensResponse, EmbedContentParameters, EmbedContentResponse, GenerateContentParameters, GenerateContentResponse, } from '@google/genai'; import * as readline from 'readline'; import { ContentGenerator } from '../core/contentGenerator.js'; import { UserTierId } from './types.js'; import { CaCountTokenResponse, CaGenerateContentResponse, fromCountTokenResponse, fromGenerateContentResponse, toCountTokenRequest, toGenerateContentRequest, } from './converter.js'; /** HTTP options to be used in each of the requests. */ export interface HttpOptions { /** Additional HTTP headers to be sent with the request. */ headers?: Record; } export const CODE_ASSIST_ENDPOINT = 'https://cloudcode-pa.googleapis.com'; export const CODE_ASSIST_API_VERSION = 'v1internal'; export class CodeAssistServer implements ContentGenerator { constructor( readonly client: OAuth2Client, readonly projectId?: string, readonly httpOptions: HttpOptions = {}, readonly sessionId?: string, readonly userTier?: UserTierId, ) {} async generateContentStream( req: GenerateContentParameters, userPromptId: string, ): Promise> { const resps = await this.requestStreamingPost( 'streamGenerateContent', toGenerateContentRequest( req, userPromptId, this.projectId, this.sessionId, ), req.config?.abortSignal, ); return (async function* (): AsyncGenerator { for await (const resp of resps) { yield fromGenerateContentResponse(resp); } })(); } async generateContent( req: GenerateContentParameters, userPromptId: string, ): Promise { const resp = await this.requestPost( 'generateContent', toGenerateContentRequest( req, userPromptId, this.projectId, this.sessionId, ), req.config?.abortSignal, ); return fromGenerateContentResponse(resp); } async onboardUser( req: OnboardUserRequest, ): Promise { return await this.requestPost( 'onboardUser', req, ); } async loadCodeAssist( req: LoadCodeAssistRequest, ): Promise { return await this.requestPost( 'loadCodeAssist', req, ); } async getCodeAssistGlobalUserSetting(): Promise { return await this.requestGet( 'getCodeAssistGlobalUserSetting', ); } async setCodeAssistGlobalUserSetting( req: SetCodeAssistGlobalUserSettingRequest, ): Promise { return await this.requestPost( 'setCodeAssistGlobalUserSetting', req, ); } async countTokens(req: CountTokensParameters): Promise { const resp = await this.requestPost( 'countTokens', toCountTokenRequest(req), ); return fromCountTokenResponse(resp); } async embedContent( _req: EmbedContentParameters, ): Promise { throw Error(); } async requestPost( method: string, req: object, signal?: AbortSignal, ): Promise { const res = await this.client.request({ url: this.getMethodUrl(method), method: 'POST', headers: { 'Content-Type': 'application/json', ...this.httpOptions.headers, }, responseType: 'json', body: JSON.stringify(req), signal, }); return res.data as T; } async requestGet(method: string, signal?: AbortSignal): Promise { const res = await this.client.request({ url: this.getMethodUrl(method), method: 'GET', headers: { 'Content-Type': 'application/json', ...this.httpOptions.headers, }, responseType: 'json', signal, }); return res.data as T; } async requestStreamingPost( method: string, req: object, signal?: AbortSignal, ): Promise> { const res = await this.client.request({ url: this.getMethodUrl(method), method: 'POST', params: { alt: 'sse', }, headers: { 'Content-Type': 'application/json', ...this.httpOptions.headers, }, responseType: 'stream', body: JSON.stringify(req), signal, }); return (async function* (): AsyncGenerator { const rl = readline.createInterface({ input: res.data as NodeJS.ReadableStream, crlfDelay: Infinity, // Recognizes '\r\n' and '\n' as line breaks }); let bufferedLines: string[] = []; for await (const line of rl) { // blank lines are used to separate JSON objects in the stream if (line === '') { if (bufferedLines.length === 0) { continue; // no data to yield } yield JSON.parse(bufferedLines.join('\n')) as T; bufferedLines = []; // Reset the buffer after yielding } else if (line.startsWith('data: ')) { bufferedLines.push(line.slice(6).trim()); } else { throw new Error(`Unexpected line format in response: ${line}`); } } })(); } getMethodUrl(method: string): string { const endpoint = process.env['CODE_ASSIST_ENDPOINT'] ?? CODE_ASSIST_ENDPOINT; return `${endpoint}/${CODE_ASSIST_API_VERSION}:${method}`; } }