File tree Expand file tree Collapse file tree 4 files changed +92
-48
lines changed Expand file tree Collapse file tree 4 files changed +92
-48
lines changed Original file line number Diff line number Diff line change 1
1
import * as core from '@actions/core'
2
+ import { GetChatCompletionsDefaultResponse } from '@azure-rest/ai-inference'
2
3
import * as fs from 'fs'
3
4
4
5
/**
@@ -29,3 +30,37 @@ export function loadContentFromFileOrInput(
29
30
throw new Error ( `Neither ${ filePathInput } nor ${ contentInput } was set` )
30
31
}
31
32
}
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
+ }
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import * as core from '@actions/core'
2
2
import ModelClient , { isUnexpected } from '@azure-rest/ai-inference'
3
3
import { AzureKeyCredential } from '@azure/core-auth'
4
4
import { GitHubMCPClient , executeToolCalls } from './mcp.js'
5
+ import { handleUnexpectedResponse } from './helpers.js'
5
6
6
7
export interface InferenceRequest {
7
8
systemPrompt : string
@@ -57,12 +58,7 @@ export async function simpleInference(
57
58
} )
58
59
59
60
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 )
66
62
}
67
63
68
64
const modelResponse = response . body . choices [ 0 ] . message . content
@@ -116,12 +112,7 @@ export async function mcpInference(
116
112
} )
117
113
118
114
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 )
125
116
}
126
117
127
118
const assistantMessage = response . body . choices [ 0 ] . message
You can’t perform that action at this time.
0 commit comments