Compare commits

...

1 Commits

Author SHA1 Message Date
arafatkatze 77b9862989 Adding console logs 2025-07-09 00:45:29 -06:00
5 changed files with 196 additions and 50 deletions
+19 -1
View File
@@ -100,6 +100,16 @@ function createHandlerForProvider(apiProvider: string | undefined, options: any)
export function buildApiHandler(configuration: ApiConfiguration): ApiHandler {
const { apiProvider, ...options } = configuration
console.log("[buildApiHandler] Creating API handler for provider:", apiProvider)
console.log("[buildApiHandler] Configuration:", {
provider: apiProvider,
hasApiKey: !!options.apiKey,
hasVertexProjectId: !!options.vertexProjectId,
hasVertexRegion: !!options.vertexRegion,
modelId: options.apiModelId,
taskId: options.taskId,
})
// Validate thinking budget tokens against model's maxTokens to prevent API errors
// wrapped in a try-catch for safety, but this should never throw
try {
@@ -110,6 +120,12 @@ export function buildApiHandler(configuration: ApiConfiguration): ApiHandler {
if (modelInfo.maxTokens && options.thinkingBudgetTokens > modelInfo.maxTokens) {
const clippedValue = modelInfo.maxTokens - 1
options.thinkingBudgetTokens = clippedValue
console.log(
"[buildApiHandler] Adjusted thinking budget tokens from",
options.thinkingBudgetTokens,
"to",
clippedValue,
)
} else {
return handler // don't rebuild unless its necessary
}
@@ -118,5 +134,7 @@ export function buildApiHandler(configuration: ApiConfiguration): ApiHandler {
console.error("buildApiHandler error:", error)
}
return createHandlerForProvider(apiProvider, options)
const handler = createHandlerForProvider(apiProvider, options)
console.log("[buildApiHandler] Handler created successfully")
return handler
}
+49 -1
View File
@@ -54,13 +54,29 @@ export class GeminiHandler implements ApiHandler {
const project = this.options.vertexProjectId ?? "not-provided"
const location = this.options.vertexRegion ?? "not-provided"
console.log("[GeminiHandler] Creating Vertex AI client with:", {
vertexai: true,
project,
location,
hasVertexProjectId: !!this.options.vertexProjectId,
hasVertexRegion: !!this.options.vertexRegion,
})
try {
this.client = new GoogleGenAI({
vertexai: true,
project,
location,
})
} catch (error) {
console.log("[GeminiHandler] Vertex AI client created successfully")
} catch (error: any) {
console.error("[GeminiHandler] Error creating Vertex AI client:", error)
console.error("[GeminiHandler] Error details:", {
message: error.message,
name: error.name,
code: error.code,
stack: error.stack,
})
throw new Error(`Error creating Gemini Vertex AI client: ${error.message}`)
}
} else {
@@ -95,9 +111,13 @@ export class GeminiHandler implements ApiHandler {
maxDelay: 15000,
})
async *createMessage(systemPrompt: string, messages: Anthropic.Messages.MessageParam[]): ApiStream {
console.log("[GeminiHandler] createMessage called")
const client = this.ensureClient()
const { id: modelId, info } = this.getModel()
console.log("[GeminiHandler] Model selected:", { modelId, modelInfo: info })
const contents = messages.map(convertAnthropicMessageToGemini)
console.log("[GeminiHandler] Converted messages count:", contents.length)
// Configure thinking budget if supported
const thinkingBudget = this.options.thinkingBudgetTokens ?? 0
@@ -120,6 +140,15 @@ export class GeminiHandler implements ApiHandler {
}
}
console.log("[GeminiHandler] Request config:", {
hasBaseUrl: !!this.options.geminiBaseUrl,
baseUrl: this.options.geminiBaseUrl,
hasSystemInstruction: !!systemPrompt,
temperature: requestConfig.temperature,
hasThinkingConfig: !!requestConfig.thinkingConfig,
thinkingBudget,
})
// Generate content using the configured parameters
const sdkCallStartTime = Date.now()
let sdkFirstChunkTime: number | undefined
@@ -133,6 +162,12 @@ export class GeminiHandler implements ApiHandler {
let lastUsageMetadata: GenerateContentResponseUsageMetadata | undefined
try {
console.log("[GeminiHandler] Making API call to generateContentStream with:", {
model: modelId,
contentsLength: contents.length,
isVertex: !!(this.options as GeminiHandlerOptions).isVertex,
})
const result = await client.models.generateContentStream({
model: modelId,
contents: contents,
@@ -141,6 +176,8 @@ export class GeminiHandler implements ApiHandler {
},
})
console.log("[GeminiHandler] API call successful, streaming response")
let isFirstSdkChunk = true
for await (const chunk of result) {
if (isFirstSdkChunk) {
@@ -211,10 +248,19 @@ export class GeminiHandler implements ApiHandler {
}
} catch (error) {
apiSuccess = false
console.error("[GeminiHandler] Error during API call:", error)
// Let the error propagate to be handled by withRetry or Task.ts
// Telemetry will be sent in the finally block.
if (error instanceof Error) {
apiError = error.message
console.error("[GeminiHandler] Error details:", {
message: error.message,
name: error.name,
stack: error.stack,
isVertex: !!(this.options as GeminiHandlerOptions).isVertex,
model: modelId,
})
// Gemini doesn't include status codes in their errors
// https://github.com/googleapis/js-genai/blob/61f7f27b866c74333ca6331883882489bcb708b9/src/_api_client.ts#L569
@@ -229,6 +275,7 @@ export class GeminiHandler implements ApiHandler {
error.name === "ClientError" && rateLimitPatterns.some((pattern) => pattern.test(error.message))
if (isRateLimit) {
console.log("[GeminiHandler] Detected rate limit error")
const rateLimitError = Object.assign(new Error(error.message), {
...error,
status: 429,
@@ -237,6 +284,7 @@ export class GeminiHandler implements ApiHandler {
}
} else {
apiError = String(error)
console.error("[GeminiHandler] Non-Error object thrown:", error)
}
throw error
+109 -48
View File
@@ -12,18 +12,35 @@ export class VertexHandler implements ApiHandler {
private options: ApiHandlerOptions
constructor(options: ApiHandlerOptions) {
console.log("[VertexHandler] Constructor called with options:", {
vertexProjectId: options.vertexProjectId,
vertexRegion: options.vertexRegion,
apiModelId: options.apiModelId,
hasApiKey: !!options.apiKey,
taskId: options.taskId,
})
this.options = options
}
private ensureGeminiHandler(): GeminiHandler {
if (!this.geminiHandler) {
console.log("[VertexHandler] Creating GeminiHandler for Vertex AI")
console.log("[VertexHandler] Options being passed to GeminiHandler:", {
vertexProjectId: this.options.vertexProjectId,
vertexRegion: this.options.vertexRegion,
isVertex: true,
apiModelId: this.options.apiModelId,
})
try {
// Create a GeminiHandler with isVertex flag for Gemini models
this.geminiHandler = new GeminiHandler({
...this.options,
isVertex: true,
})
console.log("[VertexHandler] GeminiHandler created successfully")
} catch (error: any) {
console.error("[VertexHandler] Error creating GeminiHandler:", error)
console.error("[VertexHandler] Error stack:", error.stack)
throw new Error(`Error creating Vertex AI Gemini handler: ${error.message}`)
}
}
@@ -32,12 +49,22 @@ export class VertexHandler implements ApiHandler {
private ensureAnthropicClient(): AnthropicVertex {
if (!this.clientAnthropic) {
console.log("[VertexHandler] Creating AnthropicVertex client")
if (!this.options.vertexProjectId) {
console.error("[VertexHandler] Missing vertexProjectId")
throw new Error("Vertex AI project ID is required")
}
if (!this.options.vertexRegion) {
console.error("[VertexHandler] Missing vertexRegion")
throw new Error("Vertex AI region is required")
}
console.log("[VertexHandler] Creating AnthropicVertex with:", {
projectId: this.options.vertexProjectId,
region: this.options.vertexRegion,
})
try {
// Initialize Anthropic client for Claude models
this.clientAnthropic = new AnthropicVertex({
@@ -45,7 +72,15 @@ export class VertexHandler implements ApiHandler {
// https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude#regions
region: this.options.vertexRegion,
})
console.log("[VertexHandler] AnthropicVertex client created successfully")
} catch (error: any) {
console.error("[VertexHandler] Error creating AnthropicVertex client:", error)
console.error("[VertexHandler] Error stack:", error.stack)
console.error("[VertexHandler] Error details:", {
message: error.message,
name: error.name,
code: error.code,
})
throw new Error(`Error creating Vertex AI Anthropic client: ${error.message}`)
}
}
@@ -54,16 +89,20 @@ export class VertexHandler implements ApiHandler {
@withRetry()
async *createMessage(systemPrompt: string, messages: Anthropic.Messages.MessageParam[]): ApiStream {
console.log("[VertexHandler] createMessage called")
const model = this.getModel()
const modelId = model.id
console.log("[VertexHandler] Model selected:", { modelId, modelInfo: model.info })
// For Gemini models, use the GeminiHandler
if (!modelId.includes("claude")) {
console.log("[VertexHandler] Detected Gemini model, delegating to GeminiHandler")
const geminiHandler = this.ensureGeminiHandler()
yield* geminiHandler.createMessage(systemPrompt, messages)
return
}
console.log("[VertexHandler] Using Claude model, creating AnthropicVertex client")
const clientAnthropic = this.ensureAnthropicClient()
// Claude implementation
@@ -90,21 +129,56 @@ export class VertexHandler implements ApiHandler {
)
const lastUserMsgIndex = userMsgIndices[userMsgIndices.length - 1] ?? -1
const secondLastMsgUserIndex = userMsgIndices[userMsgIndices.length - 2] ?? -1
stream = await clientAnthropic.beta.messages.create(
{
model: modelId,
max_tokens: model.info.maxTokens || 8192,
thinking: reasoningOn ? { type: "enabled", budget_tokens: budget_tokens } : undefined,
temperature: reasoningOn ? undefined : 0,
system: [
{
text: systemPrompt,
type: "text",
cache_control: { type: "ephemeral" },
},
],
messages: messages.map((message, index) => {
if (index === lastUserMsgIndex || index === secondLastMsgUserIndex) {
console.log("[VertexHandler] Making Claude API call with:", {
model: modelId,
maxTokens: model.info.maxTokens || 8192,
reasoningOn,
budgetTokens: budget_tokens,
messageCount: messages.length,
})
try {
stream = await clientAnthropic.beta.messages.create(
{
model: modelId,
max_tokens: model.info.maxTokens || 8192,
thinking: reasoningOn ? { type: "enabled", budget_tokens: budget_tokens } : undefined,
temperature: reasoningOn ? undefined : 0,
system: [
{
text: systemPrompt,
type: "text",
cache_control: { type: "ephemeral" },
},
],
messages: messages.map((message, index) => {
if (index === lastUserMsgIndex || index === secondLastMsgUserIndex) {
return {
...message,
content:
typeof message.content === "string"
? [
{
type: "text",
text: message.content,
cache_control: {
type: "ephemeral",
},
},
]
: message.content.map((content, contentIndex) =>
contentIndex === message.content.length - 1
? {
...content,
cache_control: {
type: "ephemeral",
},
}
: content,
),
}
}
return {
...message,
content:
@@ -113,42 +187,29 @@ export class VertexHandler implements ApiHandler {
{
type: "text",
text: message.content,
cache_control: {
type: "ephemeral",
},
},
]
: message.content.map((content, contentIndex) =>
contentIndex === message.content.length - 1
? {
...content,
cache_control: {
type: "ephemeral",
},
}
: content,
),
: message.content,
}
}
return {
...message,
content:
typeof message.content === "string"
? [
{
type: "text",
text: message.content,
},
]
: message.content,
}
}),
stream: true,
},
{
headers: {},
},
)
}),
stream: true,
},
{
headers: {},
},
)
console.log("[VertexHandler] Claude API call successful, stream created")
} catch (error: any) {
console.error("[VertexHandler] Error making Claude API call:", error)
console.error("[VertexHandler] Error details:", {
message: error.message,
name: error.name,
code: error.code,
status: error.status,
stack: error.stack,
})
throw error
}
break
}
default: {
+10
View File
@@ -144,6 +144,16 @@ export class Controller {
taskHistory,
} = await getAllExtensionState(this.context)
console.log("[Controller.initTask] API Configuration:", {
provider: apiConfiguration.apiProvider,
hasApiKey: !!apiConfiguration.apiKey,
hasVertexProjectId: !!apiConfiguration.vertexProjectId,
hasVertexRegion: !!apiConfiguration.vertexRegion,
vertexProjectId: apiConfiguration.vertexProjectId,
vertexRegion: apiConfiguration.vertexRegion,
modelId: apiConfiguration.apiModelId,
})
// Get current mode using helper function
const currentMode = await this.getCurrentMode()
+9
View File
@@ -64,6 +64,7 @@ export async function getWorkspaceState(context: vscode.ExtensionContext, key: L
}
export async function getAllExtensionState(context: vscode.ExtensionContext) {
console.log("[getAllExtensionState] Starting to retrieve extension state")
const firstBatchStart = performance.now()
const [
isNewUser,
@@ -283,6 +284,14 @@ export async function getAllExtensionState(context: vscode.ExtensionContext) {
])
const processingStart = performance.now()
console.log("[getAllExtensionState] Retrieved Vertex AI credentials:", {
vertexProjectId,
vertexRegion,
storedApiProvider,
apiModelId,
})
let apiProvider: ApiProvider
if (storedApiProvider) {
apiProvider = storedApiProvider