summaryrefslogtreecommitdiff
path: root/packages/core/src/code_assist/codeAssist.ts
blob: 92b53104ed5b870a1b12a82943330c278c6e5005 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { GoogleAuth, AuthClient } from 'google-auth-library';
import { ContentGenerator } from '../core/contentGenerator.js';
import { getOauthClient } from './oauth2.js';
import { setupUser } from './setup.js';
import { CodeAssistServer, HttpOptions } from './server.js';

export async function createCodeAssistContentGenerator(
  httpOptions: HttpOptions,
): Promise<ContentGenerator> {
  const authClient = await getAuthClient();
  const projectId = await setupUser(authClient);
  return new CodeAssistServer(authClient, projectId, httpOptions);
}

async function getAuthClient(): Promise<AuthClient> {
  try {
    // Try for Application Default Credentials.
    return await new GoogleAuth().getClient();
  } catch (_) {
    // No Application Default Credentials so try Oauth.
    return await getOauthClient();
  }
}