Skip to content

Commit a3fe147

Browse files
committed
Merge branch 'main' into sgoedecke/mcp
2 parents 2f08c91 + 25a2129 commit a3fe147

File tree

4 files changed

+92
-48
lines changed

4 files changed

+92
-48
lines changed

dist/index.js

Lines changed: 53 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/helpers.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as core from '@actions/core'
2+
import { GetChatCompletionsDefaultResponse } from '@azure-rest/ai-inference'
23
import * as fs from 'fs'
34

45
/**
@@ -29,3 +30,37 @@ export function loadContentFromFileOrInput(
2930
throw new Error(`Neither ${filePathInput} nor ${contentInput} was set`)
3031
}
3132
}
33+
34+
/**
35+
* Helper function to handle unexpected responses from AI service
36+
* @param response - The response object from the AI service
37+
* @throws Error with appropriate error message based on response content
38+
*/
39+
export function handleUnexpectedResponse(
40+
response: GetChatCompletionsDefaultResponse
41+
): never {
42+
// Extract x-ms-error-code from headers if available
43+
const errorCode = response.headers['x-ms-error-code']
44+
const errorCodeMsg = errorCode ? ` (error code: ${errorCode})` : ''
45+
46+
// Check if response body exists and contains error details
47+
if (response.body && response.body.error) {
48+
throw response.body.error
49+
}
50+
51+
// Handle case where response body is missing
52+
if (!response.body) {
53+
throw new Error(
54+
`Failed to get response from AI service (status: ${response.status})${errorCodeMsg}. ` +
55+
'Please check network connection and endpoint configuration.'
56+
)
57+
}
58+
59+
// Handle other error cases
60+
throw new Error(
61+
`AI service returned error response (status: ${response.status})${errorCodeMsg}: ` +
62+
(typeof response.body === 'string'
63+
? response.body
64+
: JSON.stringify(response.body))
65+
)
66+
}

src/inference.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as core from '@actions/core'
22
import ModelClient, { isUnexpected } from '@azure-rest/ai-inference'
33
import { AzureKeyCredential } from '@azure/core-auth'
44
import { GitHubMCPClient, executeToolCalls } from './mcp.js'
5+
import { handleUnexpectedResponse } from './helpers.js'
56

67
export interface InferenceRequest {
78
systemPrompt: string
@@ -57,12 +58,7 @@ export async function simpleInference(
5758
})
5859

5960
if (isUnexpected(response)) {
60-
throw new Error(
61-
'An error occurred while fetching the response (' +
62-
response.status +
63-
'): ' +
64-
response.body
65-
)
61+
handleUnexpectedResponse(response)
6662
}
6763

6864
const modelResponse = response.body.choices[0].message.content
@@ -116,12 +112,7 @@ export async function mcpInference(
116112
})
117113

118114
if (isUnexpected(response)) {
119-
throw new Error(
120-
'An error occurred while fetching the response (' +
121-
response.status +
122-
'): ' +
123-
response.body
124-
)
115+
handleUnexpectedResponse(response)
125116
}
126117

127118
const assistantMessage = response.body.choices[0].message

0 commit comments

Comments
 (0)