summaryrefslogtreecommitdiff
path: root/packages/cli/src/nonInteractiveCli.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/cli/src/nonInteractiveCli.ts')
-rw-r--r--packages/cli/src/nonInteractiveCli.ts65
1 files changed, 18 insertions, 47 deletions
diff --git a/packages/cli/src/nonInteractiveCli.ts b/packages/cli/src/nonInteractiveCli.ts
index 7bc0f6aa..1d0a7f3d 100644
--- a/packages/cli/src/nonInteractiveCli.ts
+++ b/packages/cli/src/nonInteractiveCli.ts
@@ -11,38 +11,12 @@ import {
ToolRegistry,
shutdownTelemetry,
isTelemetrySdkInitialized,
+ GeminiEventType,
} from '@google/gemini-cli-core';
-import {
- Content,
- Part,
- FunctionCall,
- GenerateContentResponse,
-} from '@google/genai';
+import { Content, Part, FunctionCall } from '@google/genai';
import { parseAndFormatApiError } from './ui/utils/errorParsing.js';
-function getResponseText(response: GenerateContentResponse): string | null {
- if (response.candidates && response.candidates.length > 0) {
- const candidate = response.candidates[0];
- if (
- candidate.content &&
- candidate.content.parts &&
- candidate.content.parts.length > 0
- ) {
- // We are running in headless mode so we don't need to return thoughts to STDOUT.
- const thoughtPart = candidate.content.parts[0];
- if (thoughtPart?.thought) {
- return null;
- }
- return candidate.content.parts
- .filter((part) => part.text)
- .map((part) => part.text)
- .join('');
- }
- }
- return null;
-}
-
export async function runNonInteractive(
config: Config,
input: string,
@@ -60,7 +34,6 @@ export async function runNonInteractive(
const geminiClient = config.getGeminiClient();
const toolRegistry: ToolRegistry = await config.getToolRegistry();
- const chat = await geminiClient.getChat();
const abortController = new AbortController();
let currentMessages: Content[] = [{ role: 'user', parts: [{ text: input }] }];
let turnCount = 0;
@@ -68,7 +41,7 @@ export async function runNonInteractive(
while (true) {
turnCount++;
if (
- config.getMaxSessionTurns() > 0 &&
+ config.getMaxSessionTurns() >= 0 &&
turnCount > config.getMaxSessionTurns()
) {
console.error(
@@ -78,30 +51,28 @@ export async function runNonInteractive(
}
const functionCalls: FunctionCall[] = [];
- const responseStream = await chat.sendMessageStream(
- {
- message: currentMessages[0]?.parts || [], // Ensure parts are always provided
- config: {
- abortSignal: abortController.signal,
- tools: [
- { functionDeclarations: toolRegistry.getFunctionDeclarations() },
- ],
- },
- },
+ const responseStream = geminiClient.sendMessageStream(
+ currentMessages[0]?.parts || [],
+ abortController.signal,
prompt_id,
);
- for await (const resp of responseStream) {
+ for await (const event of responseStream) {
if (abortController.signal.aborted) {
console.error('Operation cancelled.');
return;
}
- const textPart = getResponseText(resp);
- if (textPart) {
- process.stdout.write(textPart);
- }
- if (resp.functionCalls) {
- functionCalls.push(...resp.functionCalls);
+
+ if (event.type === GeminiEventType.Content) {
+ process.stdout.write(event.value);
+ } else if (event.type === GeminiEventType.ToolCallRequest) {
+ const toolCallRequest = event.value;
+ const fc: FunctionCall = {
+ name: toolCallRequest.name,
+ args: toolCallRequest.args,
+ id: toolCallRequest.callId,
+ };
+ functionCalls.push(fc);
}
}