2025-06-10 16:00:13 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2025-10-27 16:05:11 -04:00
|
|
|
import type { AuthClient } from 'google-auth-library';
|
2025-08-26 00:04:53 +02:00
|
|
|
import type {
|
2025-06-27 12:07:38 -07:00
|
|
|
CodeAssistGlobalUserSettingResponse,
|
2025-09-05 19:55:33 -07:00
|
|
|
GoogleRpcResponse,
|
2025-06-10 16:00:13 -07:00
|
|
|
LoadCodeAssistRequest,
|
2025-06-27 12:07:38 -07:00
|
|
|
LoadCodeAssistResponse,
|
2025-07-21 17:54:44 -04:00
|
|
|
LongRunningOperationResponse,
|
2025-06-27 12:07:38 -07:00
|
|
|
OnboardUserRequest,
|
|
|
|
|
SetCodeAssistGlobalUserSettingRequest,
|
2025-11-04 15:09:53 -08:00
|
|
|
ClientMetadata,
|
2025-06-10 16:00:13 -07:00
|
|
|
} from './types.js';
|
2025-11-03 13:51:22 -08:00
|
|
|
import type {
|
|
|
|
|
ListExperimentsRequest,
|
|
|
|
|
ListExperimentsResponse,
|
|
|
|
|
} from './experiments/types.js';
|
2025-08-26 00:04:53 +02:00
|
|
|
import type {
|
2025-06-10 16:00:13 -07:00
|
|
|
CountTokensParameters,
|
|
|
|
|
CountTokensResponse,
|
|
|
|
|
EmbedContentParameters,
|
2025-06-27 12:07:38 -07:00
|
|
|
EmbedContentResponse,
|
|
|
|
|
GenerateContentParameters,
|
|
|
|
|
GenerateContentResponse,
|
2025-06-10 16:00:13 -07:00
|
|
|
} from '@google/genai';
|
2025-08-25 22:11:27 +02:00
|
|
|
import * as readline from 'node:readline';
|
2025-08-26 00:04:53 +02:00
|
|
|
import type { ContentGenerator } from '../core/contentGenerator.js';
|
2025-09-05 19:55:33 -07:00
|
|
|
import { UserTierId } from './types.js';
|
2025-08-26 00:04:53 +02:00
|
|
|
import type {
|
2025-06-27 12:07:38 -07:00
|
|
|
CaCountTokenResponse,
|
2025-06-18 10:29:42 -07:00
|
|
|
CaGenerateContentResponse,
|
2025-08-26 00:04:53 +02:00
|
|
|
} from './converter.js';
|
|
|
|
|
import {
|
2025-06-27 12:07:38 -07:00
|
|
|
fromCountTokenResponse,
|
2025-06-18 10:29:42 -07:00
|
|
|
fromGenerateContentResponse,
|
|
|
|
|
toCountTokenRequest,
|
2025-06-27 12:07:38 -07:00
|
|
|
toGenerateContentRequest,
|
2025-06-12 18:00:17 -07:00
|
|
|
} from './converter.js';
|
2025-06-10 16:00:13 -07:00
|
|
|
|
2025-06-16 12:03:06 -07:00
|
|
|
/** HTTP options to be used in each of the requests. */
|
|
|
|
|
export interface HttpOptions {
|
|
|
|
|
/** Additional HTTP headers to be sent with the request. */
|
|
|
|
|
headers?: Record<string, string>;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 15:41:14 -07:00
|
|
|
export const CODE_ASSIST_ENDPOINT = 'https://cloudcode-pa.googleapis.com';
|
2025-06-12 18:00:17 -07:00
|
|
|
export const CODE_ASSIST_API_VERSION = 'v1internal';
|
2025-06-10 16:00:13 -07:00
|
|
|
|
2025-06-12 18:00:17 -07:00
|
|
|
export class CodeAssistServer implements ContentGenerator {
|
2025-06-10 16:00:13 -07:00
|
|
|
constructor(
|
2025-10-27 16:05:11 -04:00
|
|
|
readonly client: AuthClient,
|
2025-06-10 16:00:13 -07:00
|
|
|
readonly projectId?: string,
|
2025-06-16 12:03:06 -07:00
|
|
|
readonly httpOptions: HttpOptions = {},
|
2025-07-01 19:16:09 -04:00
|
|
|
readonly sessionId?: string,
|
2025-07-21 13:44:43 -07:00
|
|
|
readonly userTier?: UserTierId,
|
2025-06-10 16:00:13 -07:00
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
async generateContentStream(
|
|
|
|
|
req: GenerateContentParameters,
|
2025-08-01 14:37:56 -05:00
|
|
|
userPromptId: string,
|
2025-06-10 16:00:13 -07:00
|
|
|
): Promise<AsyncGenerator<GenerateContentResponse>> {
|
2025-06-30 15:41:14 -07:00
|
|
|
const resps = await this.requestStreamingPost<CaGenerateContentResponse>(
|
2025-06-10 16:00:13 -07:00
|
|
|
'streamGenerateContent',
|
2025-08-01 14:37:56 -05:00
|
|
|
toGenerateContentRequest(
|
|
|
|
|
req,
|
|
|
|
|
userPromptId,
|
|
|
|
|
this.projectId,
|
|
|
|
|
this.sessionId,
|
|
|
|
|
),
|
2025-06-16 13:24:42 -07:00
|
|
|
req.config?.abortSignal,
|
2025-06-10 16:00:13 -07:00
|
|
|
);
|
2025-06-12 09:33:49 -07:00
|
|
|
return (async function* (): AsyncGenerator<GenerateContentResponse> {
|
|
|
|
|
for await (const resp of resps) {
|
2025-06-18 10:29:42 -07:00
|
|
|
yield fromGenerateContentResponse(resp);
|
2025-06-12 09:33:49 -07:00
|
|
|
}
|
|
|
|
|
})();
|
2025-06-10 16:00:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async generateContent(
|
|
|
|
|
req: GenerateContentParameters,
|
2025-08-01 14:37:56 -05:00
|
|
|
userPromptId: string,
|
2025-06-10 16:00:13 -07:00
|
|
|
): Promise<GenerateContentResponse> {
|
2025-06-30 15:41:14 -07:00
|
|
|
const resp = await this.requestPost<CaGenerateContentResponse>(
|
2025-06-10 16:00:13 -07:00
|
|
|
'generateContent',
|
2025-08-01 14:37:56 -05:00
|
|
|
toGenerateContentRequest(
|
|
|
|
|
req,
|
|
|
|
|
userPromptId,
|
|
|
|
|
this.projectId,
|
|
|
|
|
this.sessionId,
|
|
|
|
|
),
|
2025-06-16 13:24:42 -07:00
|
|
|
req.config?.abortSignal,
|
2025-06-10 16:00:13 -07:00
|
|
|
);
|
2025-06-18 10:29:42 -07:00
|
|
|
return fromGenerateContentResponse(resp);
|
2025-06-10 16:00:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async onboardUser(
|
|
|
|
|
req: OnboardUserRequest,
|
2025-07-21 17:54:44 -04:00
|
|
|
): Promise<LongRunningOperationResponse> {
|
|
|
|
|
return await this.requestPost<LongRunningOperationResponse>(
|
2025-06-10 16:00:13 -07:00
|
|
|
'onboardUser',
|
|
|
|
|
req,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async loadCodeAssist(
|
|
|
|
|
req: LoadCodeAssistRequest,
|
|
|
|
|
): Promise<LoadCodeAssistResponse> {
|
2025-09-05 19:55:33 -07:00
|
|
|
try {
|
|
|
|
|
return await this.requestPost<LoadCodeAssistResponse>(
|
|
|
|
|
'loadCodeAssist',
|
|
|
|
|
req,
|
|
|
|
|
);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (isVpcScAffectedUser(e)) {
|
|
|
|
|
return {
|
|
|
|
|
currentTier: { id: UserTierId.STANDARD },
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-10 16:00:13 -07:00
|
|
|
}
|
|
|
|
|
|
2025-06-27 12:07:38 -07:00
|
|
|
async getCodeAssistGlobalUserSetting(): Promise<CodeAssistGlobalUserSettingResponse> {
|
2025-06-30 15:41:14 -07:00
|
|
|
return await this.requestGet<CodeAssistGlobalUserSettingResponse>(
|
2025-06-27 12:07:38 -07:00
|
|
|
'getCodeAssistGlobalUserSetting',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async setCodeAssistGlobalUserSetting(
|
|
|
|
|
req: SetCodeAssistGlobalUserSettingRequest,
|
|
|
|
|
): Promise<CodeAssistGlobalUserSettingResponse> {
|
2025-06-30 15:41:14 -07:00
|
|
|
return await this.requestPost<CodeAssistGlobalUserSettingResponse>(
|
2025-06-27 12:07:38 -07:00
|
|
|
'setCodeAssistGlobalUserSetting',
|
|
|
|
|
req,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-18 10:29:42 -07:00
|
|
|
async countTokens(req: CountTokensParameters): Promise<CountTokensResponse> {
|
2025-06-30 15:41:14 -07:00
|
|
|
const resp = await this.requestPost<CaCountTokenResponse>(
|
2025-06-18 10:29:42 -07:00
|
|
|
'countTokens',
|
|
|
|
|
toCountTokenRequest(req),
|
|
|
|
|
);
|
|
|
|
|
return fromCountTokenResponse(resp);
|
2025-06-10 16:00:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async embedContent(
|
|
|
|
|
_req: EmbedContentParameters,
|
|
|
|
|
): Promise<EmbedContentResponse> {
|
|
|
|
|
throw Error();
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-03 13:51:22 -08:00
|
|
|
async listExperiments(
|
|
|
|
|
metadata: ClientMetadata,
|
|
|
|
|
): Promise<ListExperimentsResponse> {
|
|
|
|
|
if (!this.projectId) {
|
|
|
|
|
throw new Error('projectId is not defined for CodeAssistServer.');
|
|
|
|
|
}
|
|
|
|
|
const projectId = this.projectId;
|
|
|
|
|
const req: ListExperimentsRequest = {
|
|
|
|
|
project: projectId,
|
2025-11-04 15:09:53 -08:00
|
|
|
metadata: { ...metadata, duetProject: projectId },
|
2025-11-03 13:51:22 -08:00
|
|
|
};
|
|
|
|
|
return await this.requestPost<ListExperimentsResponse>(
|
|
|
|
|
'listExperiments',
|
|
|
|
|
req,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 15:41:14 -07:00
|
|
|
async requestPost<T>(
|
2025-06-16 13:24:42 -07:00
|
|
|
method: string,
|
|
|
|
|
req: object,
|
|
|
|
|
signal?: AbortSignal,
|
|
|
|
|
): Promise<T> {
|
2025-06-30 08:47:01 -07:00
|
|
|
const res = await this.client.request({
|
2025-06-30 15:41:14 -07:00
|
|
|
url: this.getMethodUrl(method),
|
2025-06-10 16:00:13 -07:00
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
2025-06-16 12:03:06 -07:00
|
|
|
...this.httpOptions.headers,
|
2025-06-10 16:00:13 -07:00
|
|
|
},
|
|
|
|
|
responseType: 'json',
|
|
|
|
|
body: JSON.stringify(req),
|
2025-06-16 13:24:42 -07:00
|
|
|
signal,
|
2025-06-10 16:00:13 -07:00
|
|
|
});
|
|
|
|
|
return res.data as T;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 15:41:14 -07:00
|
|
|
async requestGet<T>(method: string, signal?: AbortSignal): Promise<T> {
|
2025-06-30 08:47:01 -07:00
|
|
|
const res = await this.client.request({
|
2025-06-30 15:41:14 -07:00
|
|
|
url: this.getMethodUrl(method),
|
2025-06-27 12:07:38 -07:00
|
|
|
method: 'GET',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
...this.httpOptions.headers,
|
|
|
|
|
},
|
|
|
|
|
responseType: 'json',
|
|
|
|
|
signal,
|
|
|
|
|
});
|
|
|
|
|
return res.data as T;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 15:41:14 -07:00
|
|
|
async requestStreamingPost<T>(
|
2025-06-10 16:00:13 -07:00
|
|
|
method: string,
|
|
|
|
|
req: object,
|
2025-06-16 13:24:42 -07:00
|
|
|
signal?: AbortSignal,
|
2025-06-10 16:00:13 -07:00
|
|
|
): Promise<AsyncGenerator<T>> {
|
2025-06-30 08:47:01 -07:00
|
|
|
const res = await this.client.request({
|
2025-06-30 15:41:14 -07:00
|
|
|
url: this.getMethodUrl(method),
|
2025-06-10 16:00:13 -07:00
|
|
|
method: 'POST',
|
|
|
|
|
params: {
|
|
|
|
|
alt: 'sse',
|
|
|
|
|
},
|
2025-06-16 12:03:06 -07:00
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
...this.httpOptions.headers,
|
|
|
|
|
},
|
2025-06-10 16:00:13 -07:00
|
|
|
responseType: 'stream',
|
|
|
|
|
body: JSON.stringify(req),
|
2025-06-16 13:24:42 -07:00
|
|
|
signal,
|
2025-06-10 16:00:13 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (async function* (): AsyncGenerator<T> {
|
|
|
|
|
const rl = readline.createInterface({
|
2025-07-21 13:44:43 -07:00
|
|
|
input: res.data as NodeJS.ReadableStream,
|
2025-06-10 16:00:13 -07:00
|
|
|
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}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})();
|
|
|
|
|
}
|
2025-06-30 15:41:14 -07:00
|
|
|
|
|
|
|
|
getMethodUrl(method: string): string {
|
2025-08-17 12:43:21 -04:00
|
|
|
const endpoint =
|
|
|
|
|
process.env['CODE_ASSIST_ENDPOINT'] ?? CODE_ASSIST_ENDPOINT;
|
2025-06-30 15:41:14 -07:00
|
|
|
return `${endpoint}/${CODE_ASSIST_API_VERSION}:${method}`;
|
|
|
|
|
}
|
2025-06-10 16:00:13 -07:00
|
|
|
}
|
2025-09-05 19:55:33 -07:00
|
|
|
|
|
|
|
|
function isVpcScAffectedUser(error: unknown): boolean {
|
|
|
|
|
if (error && typeof error === 'object' && 'response' in error) {
|
|
|
|
|
const gaxiosError = error as {
|
|
|
|
|
response?: {
|
|
|
|
|
data?: unknown;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
const response = gaxiosError.response?.data as
|
|
|
|
|
| GoogleRpcResponse
|
|
|
|
|
| undefined;
|
|
|
|
|
if (Array.isArray(response?.error?.details)) {
|
|
|
|
|
return response.error.details.some(
|
|
|
|
|
(detail) => detail.reason === 'SECURITY_POLICY_VIOLATED',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|