mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-20 16:57:46 +00:00
Initiate the GeminiClient with a config
Also address the open readability improvement comments from #104.
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
* 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 { StreamingState, type HistoryItem } from './types.js';
|
||||
import { useGeminiStream } from './hooks/useGeminiStream.js';
|
||||
@@ -25,10 +25,9 @@ import { Colors } from './colors.js';
|
||||
|
||||
interface AppProps {
|
||||
config: Config;
|
||||
initialInput?: string; // Added optional prop
|
||||
}
|
||||
|
||||
export const App = ({ config, initialInput }: AppProps) => {
|
||||
export const App = ({ config }: AppProps) => {
|
||||
// Destructured prop
|
||||
const [history, setHistory] = useState<HistoryItem[]>([]);
|
||||
const [startupWarnings, setStartupWarnings] = useState<string[]>([]);
|
||||
@@ -40,15 +39,6 @@ export const App = ({ config, initialInput }: AppProps) => {
|
||||
useStartupWarnings(setStartupWarnings);
|
||||
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(
|
||||
() =>
|
||||
history
|
||||
|
||||
@@ -7,13 +7,11 @@
|
||||
import { exec as _exec } from 'child_process';
|
||||
import { useState, useRef, useCallback, useEffect } from 'react';
|
||||
import { useInput } from 'ink';
|
||||
// Import server-side client and types
|
||||
import {
|
||||
GeminiClient,
|
||||
GeminiEventType as ServerGeminiEventType, // Rename to avoid conflict
|
||||
getErrorMessage,
|
||||
isNodeError,
|
||||
ToolResult,
|
||||
Config,
|
||||
ToolCallConfirmationDetails,
|
||||
ToolCallResponseInfo,
|
||||
@@ -23,12 +21,7 @@ import {
|
||||
ToolEditConfirmationDetails,
|
||||
ToolExecuteConfirmationDetails,
|
||||
} from '@gemini-code/server';
|
||||
import {
|
||||
type Chat,
|
||||
type PartListUnion,
|
||||
type FunctionDeclaration,
|
||||
type Part,
|
||||
} from '@google/genai';
|
||||
import { type Chat, type PartListUnion, type Part } from '@google/genai';
|
||||
import {
|
||||
StreamingState,
|
||||
HistoryItem,
|
||||
@@ -69,10 +62,7 @@ export const useGeminiStream = (
|
||||
setInitError(null);
|
||||
if (!geminiClientRef.current) {
|
||||
try {
|
||||
geminiClientRef.current = new GeminiClient(
|
||||
config.getApiKey(),
|
||||
config.getModel(),
|
||||
);
|
||||
geminiClientRef.current = new GeminiClient(config);
|
||||
} catch (error: unknown) {
|
||||
setInitError(
|
||||
`Failed to initialize client: ${getErrorMessage(error) || 'Unknown error'}`,
|
||||
@@ -166,9 +156,7 @@ export const useGeminiStream = (
|
||||
|
||||
if (!chatSessionRef.current) {
|
||||
try {
|
||||
// Use getFunctionDeclarations for startChat
|
||||
const toolSchemas = toolRegistry.getFunctionDeclarations();
|
||||
chatSessionRef.current = await client.startChat(toolSchemas);
|
||||
chatSessionRef.current = await client.startChat();
|
||||
} catch (err: unknown) {
|
||||
setInitError(`Failed to start chat: ${getErrorMessage(err)}`);
|
||||
setStreamingState(StreamingState.Idle);
|
||||
@@ -196,15 +184,7 @@ export const useGeminiStream = (
|
||||
abortControllerRef.current = new AbortController();
|
||||
const signal = abortControllerRef.current.signal;
|
||||
|
||||
// Get ServerTool descriptions for the server call
|
||||
const serverTools: ServerTool[] = toolRegistry.getAllTools();
|
||||
|
||||
const stream = client.sendMessageStream(
|
||||
chat,
|
||||
query,
|
||||
serverTools,
|
||||
signal,
|
||||
);
|
||||
const stream = client.sendMessageStream(chat, query, signal);
|
||||
|
||||
// Process the stream events from the server logic
|
||||
let currentGeminiText = ''; // To accumulate message content
|
||||
@@ -477,13 +457,3 @@ export const useGeminiStream = (
|
||||
|
||||
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>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user