@vybestack/llxprt-code-core 0.1.23-nightly.250904.97906524 → 0.1.23-nightly.250905.67589d14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/adapters/IStreamAdapter.d.ts +3 -3
- package/dist/src/auth/types.d.ts +4 -4
- package/dist/src/config/index.d.ts +7 -0
- package/dist/src/config/index.js +8 -0
- package/dist/src/config/index.js.map +1 -0
- package/dist/src/core/client.d.ts +9 -21
- package/dist/src/core/client.js +46 -144
- package/dist/src/core/client.js.map +1 -1
- package/dist/src/core/compression-config.d.ts +1 -1
- package/dist/src/core/compression-config.js +4 -5
- package/dist/src/core/compression-config.js.map +1 -1
- package/dist/src/core/coreToolScheduler.js +50 -15
- package/dist/src/core/coreToolScheduler.js.map +1 -1
- package/dist/src/core/geminiChat.d.ts +51 -2
- package/dist/src/core/geminiChat.js +592 -93
- package/dist/src/core/geminiChat.js.map +1 -1
- package/dist/src/core/nonInteractiveToolExecutor.js +70 -19
- package/dist/src/core/nonInteractiveToolExecutor.js.map +1 -1
- package/dist/src/index.d.ts +1 -2
- package/dist/src/index.js +2 -2
- package/dist/src/index.js.map +1 -1
- package/dist/src/providers/BaseProvider.d.ts +8 -3
- package/dist/src/providers/BaseProvider.js.map +1 -1
- package/dist/src/providers/IProvider.d.ts +9 -3
- package/dist/src/providers/LoggingProviderWrapper.d.ts +10 -3
- package/dist/src/providers/LoggingProviderWrapper.js +33 -27
- package/dist/src/providers/LoggingProviderWrapper.js.map +1 -1
- package/dist/src/providers/ProviderContentGenerator.d.ts +2 -2
- package/dist/src/providers/ProviderContentGenerator.js +9 -6
- package/dist/src/providers/ProviderContentGenerator.js.map +1 -1
- package/dist/src/providers/anthropic/AnthropicProvider.d.ts +12 -17
- package/dist/src/providers/anthropic/AnthropicProvider.js +238 -447
- package/dist/src/providers/anthropic/AnthropicProvider.js.map +1 -1
- package/dist/src/providers/gemini/GeminiProvider.d.ts +12 -6
- package/dist/src/providers/gemini/GeminiProvider.js +184 -458
- package/dist/src/providers/gemini/GeminiProvider.js.map +1 -1
- package/dist/src/providers/openai/ConversationCache.d.ts +3 -3
- package/dist/src/providers/openai/IChatGenerateParams.d.ts +9 -4
- package/dist/src/providers/openai/OpenAIProvider.d.ts +14 -61
- package/dist/src/providers/openai/OpenAIProvider.js +270 -575
- package/dist/src/providers/openai/OpenAIProvider.js.map +1 -1
- package/dist/src/providers/openai/buildResponsesRequest.d.ts +3 -3
- package/dist/src/providers/openai/buildResponsesRequest.js +67 -37
- package/dist/src/providers/openai/buildResponsesRequest.js.map +1 -1
- package/dist/src/providers/openai/estimateRemoteTokens.d.ts +2 -2
- package/dist/src/providers/openai/estimateRemoteTokens.js +21 -8
- package/dist/src/providers/openai/estimateRemoteTokens.js.map +1 -1
- package/dist/src/providers/openai/parseResponsesStream.d.ts +6 -2
- package/dist/src/providers/openai/parseResponsesStream.js +99 -391
- package/dist/src/providers/openai/parseResponsesStream.js.map +1 -1
- package/dist/src/providers/openai/syntheticToolResponses.d.ts +5 -5
- package/dist/src/providers/openai/syntheticToolResponses.js +102 -91
- package/dist/src/providers/openai/syntheticToolResponses.js.map +1 -1
- package/dist/src/providers/openai-responses/OpenAIResponsesProvider.d.ts +16 -17
- package/dist/src/providers/openai-responses/OpenAIResponsesProvider.js +222 -224
- package/dist/src/providers/openai-responses/OpenAIResponsesProvider.js.map +1 -1
- package/dist/src/providers/types.d.ts +1 -1
- package/dist/src/services/history/ContentConverters.d.ts +6 -1
- package/dist/src/services/history/ContentConverters.js +155 -18
- package/dist/src/services/history/ContentConverters.js.map +1 -1
- package/dist/src/services/history/HistoryService.d.ts +52 -0
- package/dist/src/services/history/HistoryService.js +245 -93
- package/dist/src/services/history/HistoryService.js.map +1 -1
- package/dist/src/services/history/IContent.d.ts +4 -0
- package/dist/src/services/history/IContent.js.map +1 -1
- package/dist/src/telemetry/types.d.ts +16 -4
- package/dist/src/telemetry/types.js.map +1 -1
- package/dist/src/tools/IToolFormatter.d.ts +2 -2
- package/dist/src/tools/ToolFormatter.d.ts +3 -3
- package/dist/src/tools/ToolFormatter.js +80 -37
- package/dist/src/tools/ToolFormatter.js.map +1 -1
- package/dist/src/tools/todo-schemas.d.ts +4 -4
- package/package.json +8 -7
- package/dist/src/core/ContentGeneratorAdapter.d.ts +0 -37
- package/dist/src/core/ContentGeneratorAdapter.js +0 -58
- package/dist/src/core/ContentGeneratorAdapter.js.map +0 -1
- package/dist/src/providers/IMessage.d.ts +0 -38
- package/dist/src/providers/IMessage.js +0 -17
- package/dist/src/providers/IMessage.js.map +0 -1
- package/dist/src/providers/adapters/GeminiCompatibleWrapper.d.ts +0 -69
- package/dist/src/providers/adapters/GeminiCompatibleWrapper.js +0 -577
- package/dist/src/providers/adapters/GeminiCompatibleWrapper.js.map +0 -1
@@ -4,7 +4,8 @@
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
5
5
|
*/
|
6
6
|
import { DebugLogger } from '../../debug/index.js';
|
7
|
-
import { Config, AuthType,
|
7
|
+
import { Config, AuthType, AuthenticationRequiredError, getCoreSystemPromptAsync, createCodeAssistContentGenerator, } from '@vybestack/llxprt-code-core';
|
8
|
+
import { Type, } from '@google/genai';
|
8
9
|
import { BaseProvider } from '../BaseProvider.js';
|
9
10
|
import { getSettingsService } from '../../settings/settingsServiceInstance.js';
|
10
11
|
export class GeminiProvider extends BaseProvider {
|
@@ -15,7 +16,6 @@ export class GeminiProvider extends BaseProvider {
|
|
15
16
|
modelExplicitlySet = false;
|
16
17
|
baseURL;
|
17
18
|
modelParams;
|
18
|
-
toolSchemas;
|
19
19
|
geminiOAuthManager;
|
20
20
|
constructor(apiKey, baseURL, config, oauthManager) {
|
21
21
|
// Initialize base provider with auth configuration
|
@@ -260,462 +260,6 @@ export class GeminiProvider extends BaseProvider {
|
|
260
260
|
},
|
261
261
|
];
|
262
262
|
}
|
263
|
-
async *generateChatCompletion(messages, tools, _toolFormat) {
|
264
|
-
// Comprehensive debug logging
|
265
|
-
this.logger.debug(() => `generateChatCompletion called with ${messages.length} messages`);
|
266
|
-
this.logger.debug(() => `Messages: ${JSON.stringify(messages, null, 2)}`);
|
267
|
-
this.logger.debug(() => `First message: ${messages[0] ? JSON.stringify(messages[0], null, 2) : 'NO FIRST MESSAGE'}`);
|
268
|
-
this.logger.debug(() => `Tools: ${tools ? JSON.stringify(tools.map((t) => t.function.name)) : 'NO TOOLS'}`);
|
269
|
-
// Lazily determine the best auth method now that it's needed.
|
270
|
-
// This implements lazy OAuth triggering - OAuth is only triggered when making API calls
|
271
|
-
let authToken;
|
272
|
-
try {
|
273
|
-
authToken = await this.determineBestAuth();
|
274
|
-
}
|
275
|
-
catch (error) {
|
276
|
-
if (error instanceof AuthenticationRequiredError) {
|
277
|
-
throw error;
|
278
|
-
}
|
279
|
-
throw new AuthenticationRequiredError('Failed to resolve authentication for Gemini provider', this.authMode, ['GEMINI_API_KEY', 'GOOGLE_API_KEY']);
|
280
|
-
}
|
281
|
-
// Authentication has already been resolved by determineBestAuth()
|
282
|
-
// No need for additional validation since the auth token is already obtained
|
283
|
-
// Import the necessary modules dynamically to avoid circular dependencies
|
284
|
-
const { GoogleGenAI } = await import('@google/genai');
|
285
|
-
// Create the appropriate client based on auth mode
|
286
|
-
let genAI;
|
287
|
-
const httpOptions = {
|
288
|
-
headers: {
|
289
|
-
'User-Agent': `LLxprt-Code/${process.env.CLI_VERSION || process.version} (${process.platform}; ${process.arch})`,
|
290
|
-
},
|
291
|
-
};
|
292
|
-
switch (this.authMode) {
|
293
|
-
case 'gemini-api-key':
|
294
|
-
genAI = new GoogleGenAI({
|
295
|
-
apiKey: authToken,
|
296
|
-
httpOptions: this.baseURL
|
297
|
-
? {
|
298
|
-
...httpOptions,
|
299
|
-
baseUrl: this.baseURL,
|
300
|
-
}
|
301
|
-
: httpOptions,
|
302
|
-
});
|
303
|
-
break;
|
304
|
-
case 'vertex-ai':
|
305
|
-
genAI = new GoogleGenAI({
|
306
|
-
apiKey: authToken,
|
307
|
-
vertexai: true,
|
308
|
-
httpOptions: this.baseURL
|
309
|
-
? {
|
310
|
-
...httpOptions,
|
311
|
-
baseUrl: this.baseURL,
|
312
|
-
}
|
313
|
-
: httpOptions,
|
314
|
-
});
|
315
|
-
break;
|
316
|
-
case 'oauth': {
|
317
|
-
// For OAuth, create a minimal config-like object if we don't have one
|
318
|
-
const configForOAuth = this.geminiConfig || {
|
319
|
-
getProxy: () => undefined, // OAuth only needs this from config
|
320
|
-
};
|
321
|
-
// For OAuth, we need to use the code assist server
|
322
|
-
const contentGenerator = await createCodeAssistContentGenerator(httpOptions, AuthType.LOGIN_WITH_GOOGLE, configForOAuth, this.baseURL);
|
323
|
-
// Convert messages to Gemini request format
|
324
|
-
// Use config model in OAuth mode to ensure synchronization
|
325
|
-
const oauthModel = this.modelExplicitlySet
|
326
|
-
? this.currentModel
|
327
|
-
: this.geminiConfig?.getModel() || this.currentModel;
|
328
|
-
// Generate systemInstruction using getCoreSystemPrompt
|
329
|
-
// Get user memory from config if available
|
330
|
-
const userMemory = this.geminiConfig?.getUserMemory
|
331
|
-
? this.geminiConfig.getUserMemory()
|
332
|
-
: '';
|
333
|
-
const systemInstruction = await getCoreSystemPromptAsync(userMemory, oauthModel);
|
334
|
-
// Store tools if provided
|
335
|
-
if (tools && tools.length > 0) {
|
336
|
-
this.toolSchemas = this.convertToolsToGeminiFormat(tools);
|
337
|
-
}
|
338
|
-
// Use provided tools or stored tools
|
339
|
-
let geminiTools = tools
|
340
|
-
? this.convertToolsToGeminiFormat(tools)
|
341
|
-
: this.toolSchemas;
|
342
|
-
// For Flash models, always include tools if available
|
343
|
-
if (oauthModel.includes('flash') && !geminiTools && this.toolSchemas) {
|
344
|
-
geminiTools = this.toolSchemas;
|
345
|
-
}
|
346
|
-
const request = {
|
347
|
-
model: oauthModel,
|
348
|
-
contents: this.convertMessagesToGeminiFormat(messages),
|
349
|
-
systemInstruction,
|
350
|
-
config: {
|
351
|
-
tools: geminiTools,
|
352
|
-
...this.modelParams,
|
353
|
-
},
|
354
|
-
};
|
355
|
-
// Use the content generator stream
|
356
|
-
// PRIVACY FIX: Removed sessionId to prevent transmission to Google servers
|
357
|
-
const streamResult = await contentGenerator.generateContentStream(request, 'oauth-session');
|
358
|
-
// Reset global state variables after successful authentication
|
359
|
-
/**
|
360
|
-
* @plan PLAN-20250822-GEMINIFALLBACK.P12
|
361
|
-
* @requirement REQ-003.3
|
362
|
-
* @pseudocode lines 17-18, 25-26
|
363
|
-
*/
|
364
|
-
global.__oauth_needs_code = false;
|
365
|
-
global.__oauth_provider = undefined;
|
366
|
-
// Convert the stream to our format
|
367
|
-
for await (const response of streamResult) {
|
368
|
-
// Extract text from the response
|
369
|
-
const text = response.candidates?.[0]?.content?.parts
|
370
|
-
?.filter((part) => 'text' in part)
|
371
|
-
?.map((part) => part.text)
|
372
|
-
?.join('') || '';
|
373
|
-
// Extract function calls from the response
|
374
|
-
const functionCalls = response.candidates?.[0]?.content?.parts
|
375
|
-
?.filter((part) => 'functionCall' in part)
|
376
|
-
?.map((part) => part.functionCall) || [];
|
377
|
-
// Build response message
|
378
|
-
const message = {
|
379
|
-
role: ContentGeneratorRole.ASSISTANT,
|
380
|
-
content: text,
|
381
|
-
};
|
382
|
-
// Add function calls if any
|
383
|
-
if (functionCalls && functionCalls.length > 0) {
|
384
|
-
message.tool_calls = functionCalls.map((call) => ({
|
385
|
-
id: call.id ||
|
386
|
-
`call_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
|
387
|
-
type: 'function',
|
388
|
-
function: {
|
389
|
-
name: call.name || 'unknown_function',
|
390
|
-
arguments: JSON.stringify(call.args || {}),
|
391
|
-
},
|
392
|
-
}));
|
393
|
-
}
|
394
|
-
// Only yield if there's content or tool calls
|
395
|
-
if (text || (functionCalls && functionCalls.length > 0)) {
|
396
|
-
yield message;
|
397
|
-
}
|
398
|
-
}
|
399
|
-
return;
|
400
|
-
}
|
401
|
-
case 'none':
|
402
|
-
// For 'none' mode, use the resolved auth token
|
403
|
-
genAI = new GoogleGenAI({
|
404
|
-
apiKey: authToken,
|
405
|
-
vertexai: this.hasVertexAICredentials(),
|
406
|
-
httpOptions: this.baseURL
|
407
|
-
? {
|
408
|
-
...httpOptions,
|
409
|
-
baseUrl: this.baseURL,
|
410
|
-
}
|
411
|
-
: httpOptions,
|
412
|
-
});
|
413
|
-
break;
|
414
|
-
default:
|
415
|
-
throw new Error(`Unsupported auth mode: ${this.authMode}`);
|
416
|
-
}
|
417
|
-
// Get the models interface (which is a ContentGenerator)
|
418
|
-
const contentGenerator = genAI.models;
|
419
|
-
// Store tools if provided
|
420
|
-
if (tools && tools.length > 0) {
|
421
|
-
this.toolSchemas = this.convertToolsToGeminiFormat(tools);
|
422
|
-
}
|
423
|
-
// Convert IMessage[] to Gemini format - do this after storing tools so priming can access them
|
424
|
-
const contents = this.convertMessagesToGeminiFormat(messages);
|
425
|
-
// Use provided tools or stored tools
|
426
|
-
let geminiTools = tools
|
427
|
-
? this.convertToolsToGeminiFormat(tools)
|
428
|
-
: this.toolSchemas;
|
429
|
-
// Create the request - ContentGenerator expects model in the request
|
430
|
-
// Use explicit model if set, otherwise fall back to config model
|
431
|
-
const modelToUse = this.modelExplicitlySet
|
432
|
-
? this.currentModel
|
433
|
-
: this.geminiConfig?.getModel() || this.currentModel;
|
434
|
-
// For Flash models, always include tools if available
|
435
|
-
if (modelToUse.includes('flash') && !geminiTools && this.toolSchemas) {
|
436
|
-
geminiTools = this.toolSchemas;
|
437
|
-
}
|
438
|
-
// Generate systemInstruction using getCoreSystemPrompt
|
439
|
-
// Get user memory from config if available
|
440
|
-
const userMemory = this.geminiConfig?.getUserMemory
|
441
|
-
? this.geminiConfig.getUserMemory()
|
442
|
-
: '';
|
443
|
-
const systemInstruction = await getCoreSystemPromptAsync(userMemory, modelToUse);
|
444
|
-
const request = {
|
445
|
-
model: modelToUse,
|
446
|
-
contents,
|
447
|
-
systemInstruction,
|
448
|
-
config: {
|
449
|
-
tools: geminiTools,
|
450
|
-
...this.modelParams,
|
451
|
-
},
|
452
|
-
};
|
453
|
-
// Generate content stream using the ContentGenerator interface
|
454
|
-
const stream = await contentGenerator.generateContentStream(request);
|
455
|
-
// Stream the response
|
456
|
-
for await (const response of stream) {
|
457
|
-
// Extract text from the response
|
458
|
-
const text = response.candidates?.[0]?.content?.parts
|
459
|
-
?.filter((part) => 'text' in part)
|
460
|
-
?.map((part) => part.text)
|
461
|
-
?.join('') || '';
|
462
|
-
// Extract function calls from the response
|
463
|
-
const functionCalls = response.candidates?.[0]?.content?.parts
|
464
|
-
?.filter((part) => 'functionCall' in part)
|
465
|
-
?.map((part) => part.functionCall) || [];
|
466
|
-
// Build response message
|
467
|
-
const message = {
|
468
|
-
role: ContentGeneratorRole.ASSISTANT,
|
469
|
-
content: text || '',
|
470
|
-
};
|
471
|
-
// Add function calls if any
|
472
|
-
if (functionCalls && functionCalls.length > 0) {
|
473
|
-
message.tool_calls = functionCalls.map((call) => ({
|
474
|
-
id: call.id ||
|
475
|
-
`call_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
|
476
|
-
type: 'function',
|
477
|
-
function: {
|
478
|
-
name: call.name || 'unknown_function',
|
479
|
-
arguments: JSON.stringify(call.args || {}),
|
480
|
-
},
|
481
|
-
}));
|
482
|
-
}
|
483
|
-
// Only yield if there's content or tool calls
|
484
|
-
if (text || (functionCalls && functionCalls.length > 0)) {
|
485
|
-
yield message;
|
486
|
-
}
|
487
|
-
}
|
488
|
-
}
|
489
|
-
convertMessagesToGeminiFormat(messages) {
|
490
|
-
const contents = [];
|
491
|
-
// Enhanced tracking with more details
|
492
|
-
const functionCalls = new Map();
|
493
|
-
const functionResponses = new Map();
|
494
|
-
for (let i = 0; i < messages.length; i++) {
|
495
|
-
const msg = messages[i];
|
496
|
-
// Handle tool responses - each in its own Content object
|
497
|
-
if (msg.role === ContentGeneratorRole.TOOL) {
|
498
|
-
if (!msg.tool_call_id) {
|
499
|
-
this.logger.debug(() => `Tool response at index ${i} missing tool_call_id, skipping: ${JSON.stringify(msg)}`);
|
500
|
-
continue;
|
501
|
-
}
|
502
|
-
functionResponses.set(msg.tool_call_id, {
|
503
|
-
name: msg.tool_name || 'unknown_function',
|
504
|
-
contentIndex: contents.length,
|
505
|
-
messageIndex: i,
|
506
|
-
});
|
507
|
-
// Add each tool response as a separate content immediately
|
508
|
-
contents.push({
|
509
|
-
role: 'user',
|
510
|
-
parts: [
|
511
|
-
{
|
512
|
-
functionResponse: {
|
513
|
-
id: msg.tool_call_id,
|
514
|
-
name: msg.tool_name || 'unknown_function',
|
515
|
-
response: {
|
516
|
-
output: msg.content || '',
|
517
|
-
},
|
518
|
-
},
|
519
|
-
},
|
520
|
-
],
|
521
|
-
});
|
522
|
-
continue;
|
523
|
-
}
|
524
|
-
// For non-tool messages, convert normally
|
525
|
-
const parts = [];
|
526
|
-
// Check for parts first (for messages with PDF/image parts but no text content)
|
527
|
-
if (msg.parts && msg.parts.length > 0) {
|
528
|
-
parts.push(...msg.parts);
|
529
|
-
}
|
530
|
-
else if (msg.content) {
|
531
|
-
// Handle PartListUnion: string | Part | Part[]
|
532
|
-
// In practice, content can be PartListUnion even though IMessage types it as string
|
533
|
-
const content = msg.content;
|
534
|
-
if (typeof content === 'string') {
|
535
|
-
// Try to parse string in case it's a stringified Part or Part[]
|
536
|
-
if ((content.startsWith('{') && content.endsWith('}')) ||
|
537
|
-
(content.startsWith('[') && content.endsWith(']'))) {
|
538
|
-
try {
|
539
|
-
const parsed = JSON.parse(content);
|
540
|
-
if (Array.isArray(parsed)) {
|
541
|
-
parts.push(...parsed);
|
542
|
-
}
|
543
|
-
else {
|
544
|
-
parts.push(parsed);
|
545
|
-
}
|
546
|
-
}
|
547
|
-
catch (_e) {
|
548
|
-
// Not valid JSON, treat as text
|
549
|
-
parts.push({ text: content });
|
550
|
-
}
|
551
|
-
}
|
552
|
-
else {
|
553
|
-
parts.push({ text: content });
|
554
|
-
}
|
555
|
-
}
|
556
|
-
else if (Array.isArray(content)) {
|
557
|
-
// Content is Part[]
|
558
|
-
parts.push(...content);
|
559
|
-
}
|
560
|
-
else {
|
561
|
-
// Content is a single Part
|
562
|
-
parts.push(content);
|
563
|
-
}
|
564
|
-
}
|
565
|
-
// Handle tool calls
|
566
|
-
if (msg.tool_calls && msg.tool_calls.length > 0) {
|
567
|
-
// Check if function calls were already added via parts
|
568
|
-
const existingFunctionCallIds = new Set();
|
569
|
-
if (msg.parts && msg.parts.length > 0) {
|
570
|
-
for (const part of parts) {
|
571
|
-
if ('functionCall' in part) {
|
572
|
-
const fc = part;
|
573
|
-
if (fc.functionCall.id) {
|
574
|
-
existingFunctionCallIds.add(fc.functionCall.id);
|
575
|
-
}
|
576
|
-
}
|
577
|
-
}
|
578
|
-
}
|
579
|
-
for (const toolCall of msg.tool_calls) {
|
580
|
-
// Skip if this function call was already added via parts
|
581
|
-
if (toolCall.id && existingFunctionCallIds.has(toolCall.id)) {
|
582
|
-
continue;
|
583
|
-
}
|
584
|
-
// Ensure tool call has an ID
|
585
|
-
if (!toolCall.id) {
|
586
|
-
this.logger.debug(() => `Tool call at message ${i} missing ID, generating one: ${JSON.stringify(toolCall)}`);
|
587
|
-
// Generate a unique ID for the function call
|
588
|
-
toolCall.id = `generated_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
589
|
-
}
|
590
|
-
const partIndex = parts.length;
|
591
|
-
parts.push({
|
592
|
-
functionCall: {
|
593
|
-
id: toolCall.id,
|
594
|
-
name: toolCall.function.name,
|
595
|
-
args: JSON.parse(toolCall.function.arguments),
|
596
|
-
},
|
597
|
-
});
|
598
|
-
// Track this function call with its position
|
599
|
-
functionCalls.set(toolCall.id, {
|
600
|
-
name: toolCall.function.name,
|
601
|
-
contentIndex: contents.length,
|
602
|
-
partIndex,
|
603
|
-
messageIndex: i,
|
604
|
-
});
|
605
|
-
}
|
606
|
-
}
|
607
|
-
// Map roles
|
608
|
-
let role = 'user';
|
609
|
-
if (msg.role === ContentGeneratorRole.ASSISTANT) {
|
610
|
-
role = 'model';
|
611
|
-
}
|
612
|
-
else if (msg.role === ContentGeneratorRole.USER) {
|
613
|
-
role = 'user';
|
614
|
-
}
|
615
|
-
else if (msg.role === 'system') {
|
616
|
-
// Gemini doesn't have system role in contents, handle separately
|
617
|
-
role = 'user';
|
618
|
-
}
|
619
|
-
if (parts.length > 0) {
|
620
|
-
contents.push({
|
621
|
-
role,
|
622
|
-
parts,
|
623
|
-
});
|
624
|
-
}
|
625
|
-
}
|
626
|
-
// Validate and add missing function responses
|
627
|
-
for (const [callId, callInfo] of Array.from(functionCalls.entries())) {
|
628
|
-
if (!functionResponses.has(callId)) {
|
629
|
-
// Create a placeholder response for missing function response
|
630
|
-
this.logger.debug(() => `Function call ${callInfo.name} (id: ${callId}) has no matching response, adding placeholder`);
|
631
|
-
// Add each function response as a separate content object (same as regular tool responses)
|
632
|
-
contents.push({
|
633
|
-
role: 'user',
|
634
|
-
parts: [
|
635
|
-
{
|
636
|
-
functionResponse: {
|
637
|
-
id: callId,
|
638
|
-
name: callInfo.name,
|
639
|
-
response: {
|
640
|
-
output: JSON.stringify({
|
641
|
-
error: 'Function call was interrupted or no response received',
|
642
|
-
message: `The function "${callInfo.name}" was called but did not receive a response. This may occur if the function execution was interrupted, requires authentication, or encountered an error.`,
|
643
|
-
callId,
|
644
|
-
functionName: callInfo.name,
|
645
|
-
}),
|
646
|
-
},
|
647
|
-
},
|
648
|
-
},
|
649
|
-
],
|
650
|
-
});
|
651
|
-
// Mark this response as added
|
652
|
-
functionResponses.set(callId, {
|
653
|
-
name: callInfo.name,
|
654
|
-
contentIndex: contents.length - 1,
|
655
|
-
messageIndex: -1, // Placeholder response doesn't have original message index
|
656
|
-
});
|
657
|
-
}
|
658
|
-
}
|
659
|
-
// Final validation - count function calls and responses
|
660
|
-
let totalFunctionCalls = 0;
|
661
|
-
let totalFunctionResponses = 0;
|
662
|
-
const callsDetail = [];
|
663
|
-
const responsesDetail = [];
|
664
|
-
const unmatchedCalls = new Set();
|
665
|
-
const unmatchedResponses = new Set();
|
666
|
-
// First pass: collect all function calls and responses with their IDs
|
667
|
-
for (let i = 0; i < contents.length; i++) {
|
668
|
-
const content = contents[i];
|
669
|
-
for (const part of content.parts) {
|
670
|
-
if ('functionCall' in part) {
|
671
|
-
totalFunctionCalls++;
|
672
|
-
const fc = part;
|
673
|
-
callsDetail.push(`${i}: ${fc.functionCall.name} (${fc.functionCall.id})`);
|
674
|
-
if (fc.functionCall.id) {
|
675
|
-
unmatchedCalls.add(fc.functionCall.id);
|
676
|
-
}
|
677
|
-
}
|
678
|
-
else if ('functionResponse' in part) {
|
679
|
-
totalFunctionResponses++;
|
680
|
-
const fr = part;
|
681
|
-
responsesDetail.push(`${i}: ${fr.functionResponse.name} (${fr.functionResponse.id})`);
|
682
|
-
if (fr.functionResponse.id) {
|
683
|
-
unmatchedResponses.add(fr.functionResponse.id);
|
684
|
-
}
|
685
|
-
}
|
686
|
-
}
|
687
|
-
}
|
688
|
-
// Second pass: match calls with responses
|
689
|
-
for (const id of unmatchedCalls) {
|
690
|
-
if (unmatchedResponses.has(id)) {
|
691
|
-
unmatchedCalls.delete(id);
|
692
|
-
unmatchedResponses.delete(id);
|
693
|
-
}
|
694
|
-
}
|
695
|
-
if (totalFunctionCalls !== totalFunctionResponses) {
|
696
|
-
this.logger.debug(() => `Function parts count mismatch: ${totalFunctionCalls} calls vs ${totalFunctionResponses} responses`);
|
697
|
-
this.logger.debug(() => `Function calls: ${JSON.stringify(callsDetail)}`);
|
698
|
-
this.logger.debug(() => `Function responses: ${JSON.stringify(responsesDetail)}`);
|
699
|
-
this.logger.debug(() => `Unmatched call IDs: ${JSON.stringify(Array.from(unmatchedCalls))}`);
|
700
|
-
this.logger.debug(() => `Unmatched response IDs: ${JSON.stringify(Array.from(unmatchedResponses))}`);
|
701
|
-
// This is now just a warning, not an error, since we've added placeholders
|
702
|
-
// The Gemini API should handle this gracefully
|
703
|
-
}
|
704
|
-
return contents;
|
705
|
-
}
|
706
|
-
convertToolsToGeminiFormat(tools) {
|
707
|
-
const result = [
|
708
|
-
{
|
709
|
-
functionDeclarations: tools.map((tool) => ({
|
710
|
-
name: tool.function.name,
|
711
|
-
description: tool.function.description,
|
712
|
-
parameters: tool.function.parameters,
|
713
|
-
})),
|
714
|
-
},
|
715
|
-
];
|
716
|
-
this.logger.debug(() => `Converted tools to Gemini format: ${JSON.stringify(result, null, 2)}`);
|
717
|
-
return result;
|
718
|
-
}
|
719
263
|
setApiKey(apiKey) {
|
720
264
|
// Call base provider implementation
|
721
265
|
super.setApiKey(apiKey);
|
@@ -1096,5 +640,187 @@ export class GeminiProvider extends BaseProvider {
|
|
1096
640
|
throw new Error(`Unknown server tool: ${toolName}`);
|
1097
641
|
}
|
1098
642
|
}
|
643
|
+
/**
|
644
|
+
* Generate chat completion with IContent interface
|
645
|
+
* Direct implementation for Gemini API with IContent interface
|
646
|
+
*/
|
647
|
+
async *generateChatCompletion(content, tools, _toolFormat) {
|
648
|
+
// Determine best auth method
|
649
|
+
const authToken = await this.determineBestAuth();
|
650
|
+
// Import necessary modules
|
651
|
+
const { GoogleGenAI } = await import('@google/genai');
|
652
|
+
// Convert IContent directly to Gemini format
|
653
|
+
const contents = [];
|
654
|
+
let systemMessage;
|
655
|
+
for (const c of content) {
|
656
|
+
if (c.speaker === 'human') {
|
657
|
+
const textBlock = c.blocks.find((b) => b.type === 'text');
|
658
|
+
if (textBlock?.text?.includes('You are') && contents.length === 0) {
|
659
|
+
// First message might be system prompt
|
660
|
+
systemMessage = textBlock.text;
|
661
|
+
continue;
|
662
|
+
}
|
663
|
+
const parts = [];
|
664
|
+
for (const block of c.blocks) {
|
665
|
+
if (block.type === 'text') {
|
666
|
+
parts.push({ text: block.text });
|
667
|
+
}
|
668
|
+
}
|
669
|
+
if (parts.length > 0) {
|
670
|
+
contents.push({ role: 'user', parts });
|
671
|
+
}
|
672
|
+
}
|
673
|
+
else if (c.speaker === 'ai') {
|
674
|
+
const parts = [];
|
675
|
+
for (const block of c.blocks) {
|
676
|
+
if (block.type === 'text') {
|
677
|
+
parts.push({ text: block.text });
|
678
|
+
}
|
679
|
+
else if (block.type === 'tool_call') {
|
680
|
+
const tc = block;
|
681
|
+
parts.push({
|
682
|
+
functionCall: {
|
683
|
+
id: tc.id,
|
684
|
+
name: tc.name,
|
685
|
+
args: tc.parameters,
|
686
|
+
},
|
687
|
+
});
|
688
|
+
}
|
689
|
+
}
|
690
|
+
if (parts.length > 0) {
|
691
|
+
contents.push({ role: 'model', parts });
|
692
|
+
}
|
693
|
+
}
|
694
|
+
else if (c.speaker === 'tool') {
|
695
|
+
const toolResponseBlock = c.blocks.find((b) => b.type === 'tool_response');
|
696
|
+
if (!toolResponseBlock) {
|
697
|
+
throw new Error('Tool content must have a tool_response block');
|
698
|
+
}
|
699
|
+
contents.push({
|
700
|
+
role: 'user',
|
701
|
+
parts: [
|
702
|
+
{
|
703
|
+
functionResponse: {
|
704
|
+
id: toolResponseBlock.callId,
|
705
|
+
name: toolResponseBlock.toolName,
|
706
|
+
response: {
|
707
|
+
output: JSON.stringify(toolResponseBlock.result),
|
708
|
+
},
|
709
|
+
},
|
710
|
+
},
|
711
|
+
],
|
712
|
+
});
|
713
|
+
}
|
714
|
+
}
|
715
|
+
// Ensure tools have proper type: 'object' for Gemini
|
716
|
+
const geminiTools = tools
|
717
|
+
? [
|
718
|
+
{
|
719
|
+
functionDeclarations: tools[0].functionDeclarations.map((decl) => {
|
720
|
+
let parameters = decl.parameters;
|
721
|
+
if (parameters &&
|
722
|
+
typeof parameters === 'object' &&
|
723
|
+
!('type' in parameters)) {
|
724
|
+
parameters = { type: Type.OBJECT, ...parameters };
|
725
|
+
}
|
726
|
+
else if (!parameters) {
|
727
|
+
parameters = { type: Type.OBJECT, properties: {} };
|
728
|
+
}
|
729
|
+
return {
|
730
|
+
name: decl.name,
|
731
|
+
description: decl.description,
|
732
|
+
parameters: parameters,
|
733
|
+
};
|
734
|
+
}),
|
735
|
+
},
|
736
|
+
]
|
737
|
+
: undefined;
|
738
|
+
// Create appropriate client and generate content
|
739
|
+
const httpOptions = {
|
740
|
+
headers: {
|
741
|
+
'User-Agent': `LLxprt-Code/${process.env.CLI_VERSION || process.version} (${process.platform}; ${process.arch})`,
|
742
|
+
},
|
743
|
+
};
|
744
|
+
let stream;
|
745
|
+
if (this.authMode === 'oauth') {
|
746
|
+
// OAuth mode
|
747
|
+
const configForOAuth = this.geminiConfig || {
|
748
|
+
getProxy: () => undefined,
|
749
|
+
};
|
750
|
+
const contentGenerator = await createCodeAssistContentGenerator(httpOptions, AuthType.LOGIN_WITH_GOOGLE, configForOAuth, this.baseURL);
|
751
|
+
const userMemory = this.geminiConfig?.getUserMemory
|
752
|
+
? this.geminiConfig.getUserMemory()
|
753
|
+
: '';
|
754
|
+
const systemInstruction = systemMessage ||
|
755
|
+
(await getCoreSystemPromptAsync(userMemory, this.currentModel));
|
756
|
+
const request = {
|
757
|
+
model: this.currentModel,
|
758
|
+
contents,
|
759
|
+
systemInstruction,
|
760
|
+
config: {
|
761
|
+
tools: geminiTools,
|
762
|
+
...this.modelParams,
|
763
|
+
},
|
764
|
+
};
|
765
|
+
stream = await contentGenerator.generateContentStream(request, 'oauth-session');
|
766
|
+
}
|
767
|
+
else {
|
768
|
+
// API key mode
|
769
|
+
const genAI = new GoogleGenAI({
|
770
|
+
apiKey: authToken,
|
771
|
+
vertexai: this.authMode === 'vertex-ai',
|
772
|
+
httpOptions: this.baseURL
|
773
|
+
? { ...httpOptions, baseUrl: this.baseURL }
|
774
|
+
: httpOptions,
|
775
|
+
});
|
776
|
+
const contentGenerator = genAI.models;
|
777
|
+
const userMemory = this.geminiConfig?.getUserMemory
|
778
|
+
? this.geminiConfig.getUserMemory()
|
779
|
+
: '';
|
780
|
+
const systemInstruction = systemMessage ||
|
781
|
+
(await getCoreSystemPromptAsync(userMemory, this.currentModel));
|
782
|
+
const request = {
|
783
|
+
model: this.currentModel,
|
784
|
+
contents,
|
785
|
+
systemInstruction,
|
786
|
+
config: {
|
787
|
+
tools: geminiTools,
|
788
|
+
...this.modelParams,
|
789
|
+
},
|
790
|
+
};
|
791
|
+
stream = await contentGenerator.generateContentStream(request);
|
792
|
+
}
|
793
|
+
// Stream responses as IContent
|
794
|
+
for await (const response of stream) {
|
795
|
+
const text = response.candidates?.[0]?.content?.parts
|
796
|
+
?.filter((part) => 'text' in part)
|
797
|
+
?.map((part) => part.text)
|
798
|
+
?.join('') || '';
|
799
|
+
const functionCalls = response.candidates?.[0]?.content?.parts
|
800
|
+
?.filter((part) => 'functionCall' in part)
|
801
|
+
?.map((part) => part.functionCall) || [];
|
802
|
+
// Yield text if present
|
803
|
+
if (text) {
|
804
|
+
yield {
|
805
|
+
speaker: 'ai',
|
806
|
+
blocks: [{ type: 'text', text }],
|
807
|
+
};
|
808
|
+
}
|
809
|
+
// Yield tool calls if present
|
810
|
+
if (functionCalls.length > 0) {
|
811
|
+
const blocks = functionCalls.map((call) => ({
|
812
|
+
type: 'tool_call',
|
813
|
+
id: call.id ||
|
814
|
+
`call_${Date.now()}_${Math.random().toString(36).substring(2, 11)}`,
|
815
|
+
name: call.name || 'unknown_function',
|
816
|
+
parameters: call.args || {},
|
817
|
+
}));
|
818
|
+
yield {
|
819
|
+
speaker: 'ai',
|
820
|
+
blocks,
|
821
|
+
};
|
822
|
+
}
|
823
|
+
}
|
824
|
+
}
|
1099
825
|
}
|
1100
826
|
//# sourceMappingURL=GeminiProvider.js.map
|