fix: add httpOptions with headers field to CCPA client and set User-Agent header (#1103)
This commit is contained in:
parent
bf62c3b21a
commit
42329e0258
|
@ -7,13 +7,15 @@
|
||||||
import { ContentGenerator } from '../core/contentGenerator.js';
|
import { ContentGenerator } from '../core/contentGenerator.js';
|
||||||
import { getOauthClient } from './oauth2.js';
|
import { getOauthClient } from './oauth2.js';
|
||||||
import { setupUser } from './setup.js';
|
import { setupUser } from './setup.js';
|
||||||
import { CodeAssistServer } from './server.js';
|
import { CodeAssistServer, HttpOptions } from './server.js';
|
||||||
|
|
||||||
export async function createCodeAssistContentGenerator(): Promise<ContentGenerator> {
|
export async function createCodeAssistContentGenerator(
|
||||||
|
httpOptions: HttpOptions,
|
||||||
|
): Promise<ContentGenerator> {
|
||||||
const oauth2Client = await getOauthClient();
|
const oauth2Client = await getOauthClient();
|
||||||
const projectId = await setupUser(
|
const projectId = await setupUser(
|
||||||
oauth2Client,
|
oauth2Client,
|
||||||
process.env.GOOGLE_CLOUD_PROJECT,
|
process.env.GOOGLE_CLOUD_PROJECT,
|
||||||
);
|
);
|
||||||
return new CodeAssistServer(oauth2Client, projectId);
|
return new CodeAssistServer(oauth2Client, projectId, httpOptions);
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,6 +28,12 @@ import {
|
||||||
} from './converter.js';
|
} from './converter.js';
|
||||||
import { PassThrough } from 'node:stream';
|
import { PassThrough } from 'node:stream';
|
||||||
|
|
||||||
|
/** 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>;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Use production endpoint once it supports our methods.
|
// TODO: Use production endpoint once it supports our methods.
|
||||||
export const CODE_ASSIST_ENDPOINT =
|
export const CODE_ASSIST_ENDPOINT =
|
||||||
process.env.CODE_ASSIST_ENDPOINT ??
|
process.env.CODE_ASSIST_ENDPOINT ??
|
||||||
|
@ -38,6 +44,7 @@ export class CodeAssistServer implements ContentGenerator {
|
||||||
constructor(
|
constructor(
|
||||||
readonly auth: OAuth2Client,
|
readonly auth: OAuth2Client,
|
||||||
readonly projectId?: string,
|
readonly projectId?: string,
|
||||||
|
readonly httpOptions: HttpOptions = {},
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async generateContentStream(
|
async generateContentStream(
|
||||||
|
@ -98,6 +105,7 @@ export class CodeAssistServer implements ContentGenerator {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
|
...this.httpOptions.headers,
|
||||||
},
|
},
|
||||||
responseType: 'json',
|
responseType: 'json',
|
||||||
body: JSON.stringify(req),
|
body: JSON.stringify(req),
|
||||||
|
@ -115,7 +123,10 @@ export class CodeAssistServer implements ContentGenerator {
|
||||||
params: {
|
params: {
|
||||||
alt: 'sse',
|
alt: 'sse',
|
||||||
},
|
},
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...this.httpOptions.headers,
|
||||||
|
},
|
||||||
responseType: 'stream',
|
responseType: 'stream',
|
||||||
body: JSON.stringify(req),
|
body: JSON.stringify(req),
|
||||||
});
|
});
|
||||||
|
|
|
@ -42,18 +42,19 @@ export type ContentGeneratorConfig = {
|
||||||
export async function createContentGenerator(
|
export async function createContentGenerator(
|
||||||
config: ContentGeneratorConfig,
|
config: ContentGeneratorConfig,
|
||||||
): Promise<ContentGenerator> {
|
): Promise<ContentGenerator> {
|
||||||
if (config.codeAssist) {
|
|
||||||
return createCodeAssistContentGenerator();
|
|
||||||
}
|
|
||||||
const version = process.env.CLI_VERSION || process.version;
|
const version = process.env.CLI_VERSION || process.version;
|
||||||
|
const httpOptions = {
|
||||||
|
headers: {
|
||||||
|
'User-Agent': `GeminiCLI/${version}/(${process.platform}; ${process.arch})`,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
if (config.codeAssist) {
|
||||||
|
return createCodeAssistContentGenerator(httpOptions);
|
||||||
|
}
|
||||||
const googleGenAI = new GoogleGenAI({
|
const googleGenAI = new GoogleGenAI({
|
||||||
apiKey: config.apiKey === '' ? undefined : config.apiKey,
|
apiKey: config.apiKey === '' ? undefined : config.apiKey,
|
||||||
vertexai: config.vertexai,
|
vertexai: config.vertexai,
|
||||||
httpOptions: {
|
httpOptions,
|
||||||
headers: {
|
|
||||||
'User-Agent': `GeminiCLI/${version}/(${process.platform}; ${process.arch})`,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
return googleGenAI.models;
|
return googleGenAI.models;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue