diff options
| author | Tommaso Sciortino <[email protected]> | 2025-06-12 09:33:49 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-06-12 09:33:49 -0700 |
| commit | 47ce39c46fa95e04442affa004a8a1dc69764c83 (patch) | |
| tree | e3c8a183319c3887c0d4c2a1274e1823ebdf7ea3 /packages/core/src/code_assist/ccpaServer.ts | |
| parent | f2ab6d08c4cd0ca9a5b3900a6bb66a083c0577ee (diff) | |
Convert CCPA requests to proper format (#981)
CCPA uses a different format than GenAi. This adds conversion code to get it to the right format.
Note that this doesn't work against the current ccpa staging server, The changes it needs are in cl/770266927
Diffstat (limited to 'packages/core/src/code_assist/ccpaServer.ts')
| -rw-r--r-- | packages/core/src/code_assist/ccpaServer.ts | 30 |
1 files changed, 13 insertions, 17 deletions
diff --git a/packages/core/src/code_assist/ccpaServer.ts b/packages/core/src/code_assist/ccpaServer.ts index 7a542db4..3ef8b084 100644 --- a/packages/core/src/code_assist/ccpaServer.ts +++ b/packages/core/src/code_assist/ccpaServer.ts @@ -19,10 +19,10 @@ import { CountTokensResponse, EmbedContentParameters, } from '@google/genai'; -import { Readable } from 'stream'; import * as readline from 'readline'; -import type { ReadableStream } from 'node:stream/web'; import { ContentGenerator } from '../core/contentGenerator.js'; +import { CcpaResponse, toCcpaRequest, fromCcpaResponse } from './converter.js'; +import { PassThrough } from 'node:stream'; // TODO: Use production endpoint once it supports our methods. export const CCPA_ENDPOINT = @@ -38,19 +38,25 @@ export class CcpaServer implements ContentGenerator { async generateContentStream( req: GenerateContentParameters, ): Promise<AsyncGenerator<GenerateContentResponse>> { - return await this.streamEndpoint<GenerateContentResponse>( + const resps = await this.streamEndpoint<CcpaResponse>( 'streamGenerateContent', - req, + toCcpaRequest(req, this.projectId), ); + return (async function* (): AsyncGenerator<GenerateContentResponse> { + for await (const resp of resps) { + yield fromCcpaResponse(resp); + } + })(); } async generateContent( req: GenerateContentParameters, ): Promise<GenerateContentResponse> { - return await this.callEndpoint<GenerateContentResponse>( + const resp = await this.callEndpoint<CcpaResponse>( 'generateContent', - req, + toCcpaRequest(req, this.projectId), ); + return fromCcpaResponse(resp); } async onboardUser( @@ -92,11 +98,6 @@ export class CcpaServer implements ContentGenerator { responseType: 'json', body: JSON.stringify(req), }); - if (res.status !== 200) { - throw new Error( - `Failed to fetch from ${method}: ${res.status} ${res.data}`, - ); - } return res.data as T; } @@ -114,15 +115,10 @@ export class CcpaServer implements ContentGenerator { responseType: 'stream', body: JSON.stringify(req), }); - if (res.status !== 200) { - throw new Error( - `Failed to fetch from ${method}: ${res.status} ${res.data}`, - ); - } return (async function* (): AsyncGenerator<T> { const rl = readline.createInterface({ - input: Readable.fromWeb(res.data as ReadableStream<Uint8Array>), + input: res.data as PassThrough, crlfDelay: Infinity, // Recognizes '\r\n' and '\n' as line breaks }); |
