Merge branch 'main' into sgoedecke/mcp

This commit is contained in:
Sean Goedecke
2025-07-16 02:56:55 +00:00
4 changed files with 92 additions and 48 deletions
+35
View File
@@ -1,4 +1,5 @@
import * as core from '@actions/core'
import { GetChatCompletionsDefaultResponse } from '@azure-rest/ai-inference'
import * as fs from 'fs'
/**
@@ -29,3 +30,37 @@ export function loadContentFromFileOrInput(
throw new Error(`Neither ${filePathInput} nor ${contentInput} was set`)
}
}
/**
* Helper function to handle unexpected responses from AI service
* @param response - The response object from the AI service
* @throws Error with appropriate error message based on response content
*/
export function handleUnexpectedResponse(
response: GetChatCompletionsDefaultResponse
): never {
// Extract x-ms-error-code from headers if available
const errorCode = response.headers['x-ms-error-code']
const errorCodeMsg = errorCode ? ` (error code: ${errorCode})` : ''
// Check if response body exists and contains error details
if (response.body && response.body.error) {
throw response.body.error
}
// Handle case where response body is missing
if (!response.body) {
throw new Error(
`Failed to get response from AI service (status: ${response.status})${errorCodeMsg}. ` +
'Please check network connection and endpoint configuration.'
)
}
// Handle other error cases
throw new Error(
`AI service returned error response (status: ${response.status})${errorCodeMsg}: ` +
(typeof response.body === 'string'
? response.body
: JSON.stringify(response.body))
)
}
+3 -12
View File
@@ -2,6 +2,7 @@ import * as core from '@actions/core'
import ModelClient, { isUnexpected } from '@azure-rest/ai-inference'
import { AzureKeyCredential } from '@azure/core-auth'
import { GitHubMCPClient, executeToolCalls } from './mcp.js'
import { handleUnexpectedResponse } from './helpers.js'
export interface InferenceRequest {
systemPrompt: string
@@ -57,12 +58,7 @@ export async function simpleInference(
})
if (isUnexpected(response)) {
throw new Error(
'An error occurred while fetching the response (' +
response.status +
'): ' +
response.body
)
handleUnexpectedResponse(response)
}
const modelResponse = response.body.choices[0].message.content
@@ -116,12 +112,7 @@ export async function mcpInference(
})
if (isUnexpected(response)) {
throw new Error(
'An error occurred while fetching the response (' +
response.status +
'): ' +
response.body
)
handleUnexpectedResponse(response)
}
const assistantMessage = response.body.choices[0].message