From 47ce39c46fa95e04442affa004a8a1dc69764c83 Mon Sep 17 00:00:00 2001 From: Tommaso Sciortino Date: Thu, 12 Jun 2025 09:33:49 -0700 Subject: 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 --- packages/core/src/code_assist/ccpaServer.ts | 30 +++++++++++++---------------- 1 file changed, 13 insertions(+), 17 deletions(-) (limited to 'packages/core/src/code_assist/ccpaServer.ts') 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> { - return await this.streamEndpoint( + const resps = await this.streamEndpoint( 'streamGenerateContent', - req, + toCcpaRequest(req, this.projectId), ); + return (async function* (): AsyncGenerator { + for await (const resp of resps) { + yield fromCcpaResponse(resp); + } + })(); } async generateContent( req: GenerateContentParameters, ): Promise { - return await this.callEndpoint( + const resp = await this.callEndpoint( '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 { const rl = readline.createInterface({ - input: Readable.fromWeb(res.data as ReadableStream), + input: res.data as PassThrough, crlfDelay: Infinity, // Recognizes '\r\n' and '\n' as line breaks }); -- cgit v1.2.3