Initiate the GeminiClient with a config
Also address the open readability improvement comments from #104.
This commit is contained in:
parent
3db2a796ec
commit
8e0fb9ee2f
|
@ -9,24 +9,9 @@ import { render } from 'ink';
|
||||||
import { App } from './ui/App.js';
|
import { App } from './ui/App.js';
|
||||||
import { loadCliConfig } from './config/config.js';
|
import { loadCliConfig } from './config/config.js';
|
||||||
import { readStdin } from './utils/readStdin.js';
|
import { readStdin } from './utils/readStdin.js';
|
||||||
import { GeminiClient, ServerTool } from '@gemini-code/server';
|
import { GeminiClient } from '@gemini-code/server';
|
||||||
|
|
||||||
import { PartListUnion } from '@google/genai';
|
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
let initialInput: string | undefined = undefined;
|
|
||||||
|
|
||||||
// Check if input is being piped
|
|
||||||
if (!process.stdin.isTTY) {
|
|
||||||
try {
|
|
||||||
initialInput = await readStdin();
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error reading from stdin:', error);
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load configuration
|
|
||||||
const config = loadCliConfig();
|
const config = loadCliConfig();
|
||||||
|
|
||||||
// Render UI, passing necessary config values and initial input
|
// Render UI, passing necessary config values and initial input
|
||||||
|
@ -34,44 +19,35 @@ async function main() {
|
||||||
render(
|
render(
|
||||||
React.createElement(App, {
|
React.createElement(App, {
|
||||||
config,
|
config,
|
||||||
initialInput,
|
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
} else if (initialInput) {
|
return;
|
||||||
// If not a TTY and we have initial input, process it directly
|
}
|
||||||
const geminiClient = new GeminiClient(
|
|
||||||
config.getApiKey(),
|
|
||||||
config.getModel(),
|
|
||||||
);
|
|
||||||
const toolRegistry = config.getToolRegistry();
|
|
||||||
const availableTools: ServerTool[] = toolRegistry.getAllTools();
|
|
||||||
const toolDeclarations = toolRegistry.getFunctionDeclarations();
|
|
||||||
const chat = await geminiClient.startChat(toolDeclarations);
|
|
||||||
|
|
||||||
const request: PartListUnion = [{ text: initialInput }];
|
const input = await readStdin();
|
||||||
|
if (!input) {
|
||||||
try {
|
|
||||||
for await (const event of geminiClient.sendMessageStream(
|
|
||||||
chat,
|
|
||||||
request,
|
|
||||||
availableTools,
|
|
||||||
)) {
|
|
||||||
if (event.type === 'content') {
|
|
||||||
process.stdout.write(event.value);
|
|
||||||
}
|
|
||||||
// We might need to handle other event types later, but for now, just content.
|
|
||||||
}
|
|
||||||
process.stdout.write('\n'); // Add a newline at the end
|
|
||||||
process.exit(0);
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error processing piped input:', error);
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// If not a TTY and no initial input, exit with an error
|
|
||||||
console.error('No input provided via stdin.');
|
console.error('No input provided via stdin.');
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If not a TTY and we have initial input, process it directly
|
||||||
|
const geminiClient = new GeminiClient(config);
|
||||||
|
const chat = await geminiClient.startChat();
|
||||||
|
try {
|
||||||
|
for await (const event of geminiClient.sendMessageStream(chat, [
|
||||||
|
{ text: input },
|
||||||
|
])) {
|
||||||
|
if (event.type === 'content') {
|
||||||
|
process.stdout.write(event.value);
|
||||||
|
}
|
||||||
|
// We might need to handle other event types later, but for now, just content.
|
||||||
|
}
|
||||||
|
process.stdout.write('\n'); // Add a newline at the end
|
||||||
|
process.exit(0);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error processing piped input:', error);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Global Unhandled Rejection Handler ---
|
// --- Global Unhandled Rejection Handler ---
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import React, { useState, useMemo, useEffect } from 'react'; // Added useEffect
|
import React, { useState, useMemo } from 'react';
|
||||||
import { Box, Text } from 'ink';
|
import { Box, Text } from 'ink';
|
||||||
import { StreamingState, type HistoryItem } from './types.js';
|
import { StreamingState, type HistoryItem } from './types.js';
|
||||||
import { useGeminiStream } from './hooks/useGeminiStream.js';
|
import { useGeminiStream } from './hooks/useGeminiStream.js';
|
||||||
|
@ -25,10 +25,9 @@ import { Colors } from './colors.js';
|
||||||
|
|
||||||
interface AppProps {
|
interface AppProps {
|
||||||
config: Config;
|
config: Config;
|
||||||
initialInput?: string; // Added optional prop
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const App = ({ config, initialInput }: AppProps) => {
|
export const App = ({ config }: AppProps) => {
|
||||||
// Destructured prop
|
// Destructured prop
|
||||||
const [history, setHistory] = useState<HistoryItem[]>([]);
|
const [history, setHistory] = useState<HistoryItem[]>([]);
|
||||||
const [startupWarnings, setStartupWarnings] = useState<string[]>([]);
|
const [startupWarnings, setStartupWarnings] = useState<string[]>([]);
|
||||||
|
@ -40,15 +39,6 @@ export const App = ({ config, initialInput }: AppProps) => {
|
||||||
useStartupWarnings(setStartupWarnings);
|
useStartupWarnings(setStartupWarnings);
|
||||||
useInitializationErrorEffect(initError, history, setHistory);
|
useInitializationErrorEffect(initError, history, setHistory);
|
||||||
|
|
||||||
// Effect to handle initial piped input
|
|
||||||
useEffect(() => {
|
|
||||||
if (initialInput && initialInput.trim() !== '') {
|
|
||||||
submitQuery(initialInput);
|
|
||||||
}
|
|
||||||
// Run only once on mount
|
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const userMessages = useMemo(
|
const userMessages = useMemo(
|
||||||
() =>
|
() =>
|
||||||
history
|
history
|
||||||
|
|
|
@ -7,13 +7,11 @@
|
||||||
import { exec as _exec } from 'child_process';
|
import { exec as _exec } from 'child_process';
|
||||||
import { useState, useRef, useCallback, useEffect } from 'react';
|
import { useState, useRef, useCallback, useEffect } from 'react';
|
||||||
import { useInput } from 'ink';
|
import { useInput } from 'ink';
|
||||||
// Import server-side client and types
|
|
||||||
import {
|
import {
|
||||||
GeminiClient,
|
GeminiClient,
|
||||||
GeminiEventType as ServerGeminiEventType, // Rename to avoid conflict
|
GeminiEventType as ServerGeminiEventType, // Rename to avoid conflict
|
||||||
getErrorMessage,
|
getErrorMessage,
|
||||||
isNodeError,
|
isNodeError,
|
||||||
ToolResult,
|
|
||||||
Config,
|
Config,
|
||||||
ToolCallConfirmationDetails,
|
ToolCallConfirmationDetails,
|
||||||
ToolCallResponseInfo,
|
ToolCallResponseInfo,
|
||||||
|
@ -23,12 +21,7 @@ import {
|
||||||
ToolEditConfirmationDetails,
|
ToolEditConfirmationDetails,
|
||||||
ToolExecuteConfirmationDetails,
|
ToolExecuteConfirmationDetails,
|
||||||
} from '@gemini-code/server';
|
} from '@gemini-code/server';
|
||||||
import {
|
import { type Chat, type PartListUnion, type Part } from '@google/genai';
|
||||||
type Chat,
|
|
||||||
type PartListUnion,
|
|
||||||
type FunctionDeclaration,
|
|
||||||
type Part,
|
|
||||||
} from '@google/genai';
|
|
||||||
import {
|
import {
|
||||||
StreamingState,
|
StreamingState,
|
||||||
HistoryItem,
|
HistoryItem,
|
||||||
|
@ -69,10 +62,7 @@ export const useGeminiStream = (
|
||||||
setInitError(null);
|
setInitError(null);
|
||||||
if (!geminiClientRef.current) {
|
if (!geminiClientRef.current) {
|
||||||
try {
|
try {
|
||||||
geminiClientRef.current = new GeminiClient(
|
geminiClientRef.current = new GeminiClient(config);
|
||||||
config.getApiKey(),
|
|
||||||
config.getModel(),
|
|
||||||
);
|
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
setInitError(
|
setInitError(
|
||||||
`Failed to initialize client: ${getErrorMessage(error) || 'Unknown error'}`,
|
`Failed to initialize client: ${getErrorMessage(error) || 'Unknown error'}`,
|
||||||
|
@ -166,9 +156,7 @@ export const useGeminiStream = (
|
||||||
|
|
||||||
if (!chatSessionRef.current) {
|
if (!chatSessionRef.current) {
|
||||||
try {
|
try {
|
||||||
// Use getFunctionDeclarations for startChat
|
chatSessionRef.current = await client.startChat();
|
||||||
const toolSchemas = toolRegistry.getFunctionDeclarations();
|
|
||||||
chatSessionRef.current = await client.startChat(toolSchemas);
|
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
setInitError(`Failed to start chat: ${getErrorMessage(err)}`);
|
setInitError(`Failed to start chat: ${getErrorMessage(err)}`);
|
||||||
setStreamingState(StreamingState.Idle);
|
setStreamingState(StreamingState.Idle);
|
||||||
|
@ -196,15 +184,7 @@ export const useGeminiStream = (
|
||||||
abortControllerRef.current = new AbortController();
|
abortControllerRef.current = new AbortController();
|
||||||
const signal = abortControllerRef.current.signal;
|
const signal = abortControllerRef.current.signal;
|
||||||
|
|
||||||
// Get ServerTool descriptions for the server call
|
const stream = client.sendMessageStream(chat, query, signal);
|
||||||
const serverTools: ServerTool[] = toolRegistry.getAllTools();
|
|
||||||
|
|
||||||
const stream = client.sendMessageStream(
|
|
||||||
chat,
|
|
||||||
query,
|
|
||||||
serverTools,
|
|
||||||
signal,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Process the stream events from the server logic
|
// Process the stream events from the server logic
|
||||||
let currentGeminiText = ''; // To accumulate message content
|
let currentGeminiText = ''; // To accumulate message content
|
||||||
|
@ -477,13 +457,3 @@ export const useGeminiStream = (
|
||||||
|
|
||||||
return { streamingState, submitQuery, initError, debugMessage };
|
return { streamingState, submitQuery, initError, debugMessage };
|
||||||
};
|
};
|
||||||
|
|
||||||
// Define ServerTool interface here if not importing from server (circular dep issue?)
|
|
||||||
interface ServerTool {
|
|
||||||
name: string;
|
|
||||||
schema: FunctionDeclaration;
|
|
||||||
shouldConfirmExecute(
|
|
||||||
params: Record<string, unknown>,
|
|
||||||
): Promise<ToolCallConfirmationDetails | false>;
|
|
||||||
execute(params: Record<string, unknown>): Promise<ToolResult>;
|
|
||||||
}
|
|
||||||
|
|
|
@ -12,15 +12,16 @@ import {
|
||||||
SchemaUnion,
|
SchemaUnion,
|
||||||
PartListUnion,
|
PartListUnion,
|
||||||
Content,
|
Content,
|
||||||
FunctionDeclaration,
|
|
||||||
Tool,
|
Tool,
|
||||||
} from '@google/genai';
|
} from '@google/genai';
|
||||||
import { CoreSystemPrompt } from './prompts.js';
|
import { CoreSystemPrompt } from './prompts.js';
|
||||||
import process from 'node:process';
|
import process from 'node:process';
|
||||||
import { getFolderStructure } from '../utils/getFolderStructure.js';
|
import { getFolderStructure } from '../utils/getFolderStructure.js';
|
||||||
import { Turn, ServerTool, ServerGeminiStreamEvent } from './turn.js';
|
import { Turn, ServerGeminiStreamEvent } from './turn.js';
|
||||||
|
import { Config } from '../config/config.js';
|
||||||
|
|
||||||
export class GeminiClient {
|
export class GeminiClient {
|
||||||
|
private config: Config;
|
||||||
private client: GoogleGenAI;
|
private client: GoogleGenAI;
|
||||||
private model: string;
|
private model: string;
|
||||||
private generateContentConfig: GenerateContentConfig = {
|
private generateContentConfig: GenerateContentConfig = {
|
||||||
|
@ -29,9 +30,10 @@ export class GeminiClient {
|
||||||
};
|
};
|
||||||
private readonly MAX_TURNS = 100;
|
private readonly MAX_TURNS = 100;
|
||||||
|
|
||||||
constructor(apiKey: string, model: string) {
|
constructor(config: Config) {
|
||||||
this.client = new GoogleGenAI({ apiKey: apiKey });
|
this.client = new GoogleGenAI({ apiKey: config.getApiKey() });
|
||||||
this.model = model;
|
this.config = config;
|
||||||
|
this.model = config.getModel();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getEnvironment(): Promise<Part> {
|
private async getEnvironment(): Promise<Part> {
|
||||||
|
@ -54,8 +56,11 @@ export class GeminiClient {
|
||||||
return { text: context };
|
return { text: context };
|
||||||
}
|
}
|
||||||
|
|
||||||
async startChat(toolDeclarations: FunctionDeclaration[]): Promise<Chat> {
|
async startChat(): Promise<Chat> {
|
||||||
const envPart = await this.getEnvironment();
|
const envPart = await this.getEnvironment();
|
||||||
|
const toolDeclarations = this.config
|
||||||
|
.getToolRegistry()
|
||||||
|
.getFunctionDeclarations();
|
||||||
const tools: Tool[] = [{ functionDeclarations: toolDeclarations }];
|
const tools: Tool[] = [{ functionDeclarations: toolDeclarations }];
|
||||||
try {
|
try {
|
||||||
return this.client.chats.create({
|
return this.client.chats.create({
|
||||||
|
@ -86,10 +91,10 @@ export class GeminiClient {
|
||||||
async *sendMessageStream(
|
async *sendMessageStream(
|
||||||
chat: Chat,
|
chat: Chat,
|
||||||
request: PartListUnion,
|
request: PartListUnion,
|
||||||
availableTools: ServerTool[],
|
|
||||||
signal?: AbortSignal,
|
signal?: AbortSignal,
|
||||||
): AsyncGenerator<ServerGeminiStreamEvent> {
|
): AsyncGenerator<ServerGeminiStreamEvent> {
|
||||||
let turns = 0;
|
let turns = 0;
|
||||||
|
const availableTools = this.config.getToolRegistry().getAllTools();
|
||||||
try {
|
try {
|
||||||
while (turns < this.MAX_TURNS) {
|
while (turns < this.MAX_TURNS) {
|
||||||
turns++;
|
turns++;
|
||||||
|
|
|
@ -78,10 +78,7 @@ export class BackgroundTerminalAnalyzer {
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
// Initialize Gemini client using config
|
// Initialize Gemini client using config
|
||||||
this.geminiClient = new GeminiClient(
|
this.geminiClient = new GeminiClient(config);
|
||||||
config.getApiKey(),
|
|
||||||
config.getModel(),
|
|
||||||
);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(
|
console.error(
|
||||||
'Failed to initialize GeminiClient in BackgroundTerminalAnalyzer:',
|
'Failed to initialize GeminiClient in BackgroundTerminalAnalyzer:',
|
||||||
|
|
Loading…
Reference in New Issue