Use OpenAI SDK to avoid setting apiVersion manually
This commit is contained in:
@@ -2,17 +2,15 @@ import {vi, type MockedFunction, beforeEach, expect, describe, it} from 'vitest'
|
||||
import * as core from '../__fixtures__/core.js'
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const mockPost = vi.fn() as MockedFunction<any>
|
||||
const mockPath = vi.fn(() => ({post: mockPost}))
|
||||
const mockClient = vi.fn(() => ({path: mockPath}))
|
||||
|
||||
vi.mock('@azure-rest/ai-inference', () => ({
|
||||
default: mockClient,
|
||||
isUnexpected: vi.fn(() => false),
|
||||
const mockCreate = vi.fn() as MockedFunction<any>
|
||||
const mockCompletions = {create: mockCreate}
|
||||
const mockChat = {completions: mockCompletions}
|
||||
const mockOpenAIClient = vi.fn(() => ({
|
||||
chat: mockChat,
|
||||
}))
|
||||
|
||||
vi.mock('@azure/core-auth', () => ({
|
||||
AzureKeyCredential: vi.fn(),
|
||||
vi.mock('openai', () => ({
|
||||
default: mockOpenAIClient,
|
||||
}))
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
@@ -29,8 +27,8 @@ const {simpleInference, mcpInference} = await import('../src/inference.js')
|
||||
describe('inference.ts', () => {
|
||||
const mockRequest = {
|
||||
messages: [
|
||||
{role: 'system', content: 'You are a test assistant'},
|
||||
{role: 'user', content: 'Hello, AI!'},
|
||||
{role: 'system' as const, content: 'You are a test assistant'},
|
||||
{role: 'user' as const, content: 'Hello, AI!'},
|
||||
],
|
||||
modelName: 'gpt-4',
|
||||
maxTokens: 100,
|
||||
@@ -45,18 +43,16 @@ describe('inference.ts', () => {
|
||||
describe('simpleInference', () => {
|
||||
it('performs simple inference without tools', async () => {
|
||||
const mockResponse = {
|
||||
body: {
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: 'Hello, user!',
|
||||
},
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: 'Hello, user!',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
mockPost.mockResolvedValue(mockResponse)
|
||||
mockCreate.mockResolvedValue(mockResponse)
|
||||
|
||||
const result = await simpleInference(mockRequest)
|
||||
|
||||
@@ -65,38 +61,34 @@ describe('inference.ts', () => {
|
||||
expect(core.info).toHaveBeenCalledWith('Model response: Hello, user!')
|
||||
|
||||
// Verify the request structure
|
||||
expect(mockPost).toHaveBeenCalledWith({
|
||||
body: {
|
||||
messages: [
|
||||
{
|
||||
role: 'system',
|
||||
content: 'You are a test assistant',
|
||||
},
|
||||
{
|
||||
role: 'user',
|
||||
content: 'Hello, AI!',
|
||||
},
|
||||
],
|
||||
max_tokens: 100,
|
||||
model: 'gpt-4',
|
||||
},
|
||||
expect(mockCreate).toHaveBeenCalledWith({
|
||||
messages: [
|
||||
{
|
||||
role: 'system',
|
||||
content: 'You are a test assistant',
|
||||
},
|
||||
{
|
||||
role: 'user',
|
||||
content: 'Hello, AI!',
|
||||
},
|
||||
],
|
||||
max_tokens: 100,
|
||||
model: 'gpt-4',
|
||||
})
|
||||
})
|
||||
|
||||
it('handles null response content', async () => {
|
||||
const mockResponse = {
|
||||
body: {
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: null,
|
||||
},
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: null,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
mockPost.mockResolvedValue(mockResponse)
|
||||
mockCreate.mockResolvedValue(mockResponse)
|
||||
|
||||
const result = await simpleInference(mockRequest)
|
||||
|
||||
@@ -123,19 +115,17 @@ describe('inference.ts', () => {
|
||||
|
||||
it('performs inference without tool calls', async () => {
|
||||
const mockResponse = {
|
||||
body: {
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: 'Hello, user!',
|
||||
tool_calls: null,
|
||||
},
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: 'Hello, user!',
|
||||
tool_calls: null,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
mockPost.mockResolvedValue(mockResponse)
|
||||
mockCreate.mockResolvedValue(mockResponse)
|
||||
|
||||
const result = await mcpInference(mockRequest, mockMcpClient)
|
||||
|
||||
@@ -146,12 +136,12 @@ describe('inference.ts', () => {
|
||||
|
||||
// The MCP inference loop will always add the assistant message, even when there are no tool calls
|
||||
// So we don't check the exact messages, just that tools were included
|
||||
expect(mockPost).toHaveBeenCalledTimes(1)
|
||||
expect(mockCreate).toHaveBeenCalledTimes(1)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const callArgs = mockPost.mock.calls[0][0] as any
|
||||
expect(callArgs.body.tools).toEqual(mockMcpClient.tools)
|
||||
expect(callArgs.body.model).toBe('gpt-4')
|
||||
expect(callArgs.body.max_tokens).toBe(100)
|
||||
const callArgs = mockCreate.mock.calls[0][0] as any
|
||||
expect(callArgs.tools).toEqual(mockMcpClient.tools)
|
||||
expect(callArgs.model).toBe('gpt-4')
|
||||
expect(callArgs.max_tokens).toBe(100)
|
||||
})
|
||||
|
||||
it('executes tool calls and continues conversation', async () => {
|
||||
@@ -176,33 +166,29 @@ describe('inference.ts', () => {
|
||||
|
||||
// First response with tool calls
|
||||
const firstResponse = {
|
||||
body: {
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: 'I need to use a tool.',
|
||||
tool_calls: toolCalls,
|
||||
},
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: 'I need to use a tool.',
|
||||
tool_calls: toolCalls,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
// Second response after tool execution
|
||||
const secondResponse = {
|
||||
body: {
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: 'Here is the final answer.',
|
||||
tool_calls: null,
|
||||
},
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: 'Here is the final answer.',
|
||||
tool_calls: null,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
mockPost.mockResolvedValueOnce(firstResponse).mockResolvedValueOnce(secondResponse)
|
||||
mockCreate.mockResolvedValueOnce(firstResponse).mockResolvedValueOnce(secondResponse)
|
||||
|
||||
mockExecuteToolCalls.mockResolvedValue(toolResults)
|
||||
|
||||
@@ -210,15 +196,15 @@ describe('inference.ts', () => {
|
||||
|
||||
expect(result).toBe('Here is the final answer.')
|
||||
expect(mockExecuteToolCalls).toHaveBeenCalledWith(mockMcpClient.client, toolCalls)
|
||||
expect(mockPost).toHaveBeenCalledTimes(2)
|
||||
expect(mockCreate).toHaveBeenCalledTimes(2)
|
||||
|
||||
// Verify the second call includes the conversation history
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const secondCall = mockPost.mock.calls[1][0] as any
|
||||
expect(secondCall.body.messages).toHaveLength(5) // system, user, assistant, tool, assistant
|
||||
expect(secondCall.body.messages[2].role).toBe('assistant')
|
||||
expect(secondCall.body.messages[2].tool_calls).toEqual(toolCalls)
|
||||
expect(secondCall.body.messages[3]).toEqual(toolResults[0])
|
||||
const secondCall = mockCreate.mock.calls[1][0] as any
|
||||
expect(secondCall.messages).toHaveLength(5) // system, user, assistant, tool, assistant
|
||||
expect(secondCall.messages[2].role).toBe('assistant')
|
||||
expect(secondCall.messages[2].tool_calls).toEqual(toolCalls)
|
||||
expect(secondCall.messages[3]).toEqual(toolResults[0])
|
||||
})
|
||||
|
||||
it('handles maximum iteration limit', async () => {
|
||||
@@ -243,43 +229,39 @@ describe('inference.ts', () => {
|
||||
|
||||
// Always respond with tool calls to trigger infinite loop
|
||||
const responseWithToolCalls = {
|
||||
body: {
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: 'Using tool again.',
|
||||
tool_calls: toolCalls,
|
||||
},
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: 'Using tool again.',
|
||||
tool_calls: toolCalls,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
mockPost.mockResolvedValue(responseWithToolCalls)
|
||||
mockCreate.mockResolvedValue(responseWithToolCalls)
|
||||
mockExecuteToolCalls.mockResolvedValue(toolResults)
|
||||
|
||||
const result = await mcpInference(mockRequest, mockMcpClient)
|
||||
|
||||
expect(mockPost).toHaveBeenCalledTimes(5) // Max iterations reached
|
||||
expect(mockCreate).toHaveBeenCalledTimes(5) // Max iterations reached
|
||||
expect(core.warning).toHaveBeenCalledWith('GitHub MCP inference loop exceeded maximum iterations (5)')
|
||||
expect(result).toBe('Using tool again.') // Last assistant message
|
||||
})
|
||||
|
||||
it('handles empty tool calls array', async () => {
|
||||
const mockResponse = {
|
||||
body: {
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: 'Hello, user!',
|
||||
tool_calls: [],
|
||||
},
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: 'Hello, user!',
|
||||
tool_calls: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
mockPost.mockResolvedValue(mockResponse)
|
||||
mockCreate.mockResolvedValue(mockResponse)
|
||||
|
||||
const result = await mcpInference(mockRequest, mockMcpClient)
|
||||
|
||||
@@ -297,32 +279,28 @@ describe('inference.ts', () => {
|
||||
]
|
||||
|
||||
const firstResponse = {
|
||||
body: {
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: 'First message',
|
||||
tool_calls: toolCalls,
|
||||
},
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: 'First message',
|
||||
tool_calls: toolCalls,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const secondResponse = {
|
||||
body: {
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: 'Second message',
|
||||
tool_calls: toolCalls,
|
||||
},
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: 'Second message',
|
||||
tool_calls: toolCalls,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
mockPost.mockResolvedValueOnce(firstResponse).mockResolvedValue(secondResponse)
|
||||
mockCreate.mockResolvedValueOnce(firstResponse).mockResolvedValue(secondResponse)
|
||||
|
||||
mockExecuteToolCalls.mockResolvedValue([
|
||||
{
|
||||
|
||||
13732
dist/index.js
generated
vendored
13732
dist/index.js
generated
vendored
File diff suppressed because it is too large
Load Diff
2
dist/index.js.map
generated
vendored
2
dist/index.js.map
generated
vendored
File diff suppressed because one or more lines are too long
77
package-lock.json
generated
77
package-lock.json
generated
@@ -11,14 +11,11 @@
|
||||
"dependencies": {
|
||||
"@actions/core": "^1.11.1",
|
||||
"@modelcontextprotocol/sdk": "^1.15.1",
|
||||
"@rollup/rollup-linux-x64-gnu": "*",
|
||||
"js-yaml": "^4.1.0",
|
||||
"openai": "^5.11.0",
|
||||
"pkce-challenge": "^5.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@azure-rest/ai-inference": "*",
|
||||
"@azure/core-auth": "*",
|
||||
"@azure/core-sse": "*",
|
||||
"@eslint/compat": "^1.3.0",
|
||||
"@github/local-action": "^5.1.0",
|
||||
"@github/prettier-config": "^0.0.6",
|
||||
@@ -519,44 +516,6 @@
|
||||
"integrity": "sha512-wi9JjgKLYS7U/z8PPbco+PvTb/nRWjeoFlJ1Qer83k/3C5PHQi28hiVdeE2kHXmIL99mQFawx8qt/JPjZilJ8Q==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@azure-rest/ai-inference": {
|
||||
"version": "1.0.0-beta.6",
|
||||
"resolved": "https://registry.npmjs.org/@azure-rest/ai-inference/-/ai-inference-1.0.0-beta.6.tgz",
|
||||
"integrity": "sha512-j5FrJDTHu2P2+zwFVe5j2edasOIhqkFj+VkDjbhGkQuOoIAByF0egRkgs0G1k03HyJ7bOOT9BkRF7MIgr/afhw==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@azure-rest/core-client": "^2.1.0",
|
||||
"@azure/abort-controller": "^2.1.2",
|
||||
"@azure/core-auth": "^1.9.0",
|
||||
"@azure/core-lro": "^2.7.2",
|
||||
"@azure/core-rest-pipeline": "^1.18.2",
|
||||
"@azure/core-tracing": "^1.2.0",
|
||||
"@azure/logger": "^1.1.4",
|
||||
"tslib": "^2.8.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@azure-rest/core-client": {
|
||||
"version": "2.4.0",
|
||||
"resolved": "https://registry.npmjs.org/@azure-rest/core-client/-/core-client-2.4.0.tgz",
|
||||
"integrity": "sha512-CjMFBcmnt0YNdRcxSSoZbtZNXudLlicdml7UrPsV03nHiWB+Bq5cu5ctieyaCuRtU7jm7+SOFtiE/g4pBFPKKA==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@azure/abort-controller": "^2.0.0",
|
||||
"@azure/core-auth": "^1.3.0",
|
||||
"@azure/core-rest-pipeline": "^1.5.0",
|
||||
"@azure/core-tracing": "^1.0.1",
|
||||
"@typespec/ts-http-runtime": "^0.2.2",
|
||||
"tslib": "^2.6.2"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@azure/abort-controller": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/@azure/abort-controller/-/abort-controller-2.1.2.tgz",
|
||||
@@ -667,19 +626,6 @@
|
||||
"node": ">=18.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@azure/core-sse": {
|
||||
"version": "2.3.0",
|
||||
"resolved": "https://registry.npmjs.org/@azure/core-sse/-/core-sse-2.3.0.tgz",
|
||||
"integrity": "sha512-jKhPpdDbVS5GlpadSKIC7V6Q4P2vEcwXi1c4CLTXs01Q/PAITES9v5J/S73+RtCMqQpsX0jGa2yPWwXi9JzdgA==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"tslib": "^2.6.2"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=20.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@azure/core-tracing": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://registry.npmjs.org/@azure/core-tracing/-/core-tracing-1.2.0.tgz",
|
||||
@@ -7035,6 +6981,27 @@
|
||||
"wrappy": "1"
|
||||
}
|
||||
},
|
||||
"node_modules/openai": {
|
||||
"version": "5.11.0",
|
||||
"resolved": "https://registry.npmjs.org/openai/-/openai-5.11.0.tgz",
|
||||
"integrity": "sha512-+AuTc5pVjlnTuA9zvn8rA/k+1RluPIx9AD4eDcnutv6JNwHHZxIhkFy+tmMKCvmMFDQzfA/r1ujvPWB19DQkYg==",
|
||||
"license": "Apache-2.0",
|
||||
"bin": {
|
||||
"openai": "bin/cli"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"ws": "^8.18.0",
|
||||
"zod": "^3.23.8"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"ws": {
|
||||
"optional": true
|
||||
},
|
||||
"zod": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/optionator": {
|
||||
"version": "0.9.4",
|
||||
"resolved": "https://registry.npmjs.org/optionator/-/optionator-0.9.4.tgz",
|
||||
|
||||
@@ -26,12 +26,10 @@
|
||||
"@actions/core": "^1.11.1",
|
||||
"@modelcontextprotocol/sdk": "^1.15.1",
|
||||
"js-yaml": "^4.1.0",
|
||||
"openai": "^5.11.0",
|
||||
"pkce-challenge": "^5.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@azure-rest/ai-inference": "latest",
|
||||
"@azure/core-auth": "latest",
|
||||
"@azure/core-sse": "latest",
|
||||
"@eslint/compat": "^1.3.0",
|
||||
"@github/local-action": "^5.1.0",
|
||||
"@github/prettier-config": "^0.0.6",
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import * as core from '@actions/core'
|
||||
import {GetChatCompletionsDefaultResponse} from '@azure-rest/ai-inference'
|
||||
import * as fs from 'fs'
|
||||
import {PromptConfig} from './prompt.js'
|
||||
import {InferenceRequest} from './inference.js'
|
||||
@@ -29,36 +28,6 @@ export function loadContentFromFileOrInput(filePathInput: string, contentInput:
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)),
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Build messages array from either prompt config or legacy format
|
||||
*/
|
||||
@@ -66,11 +35,11 @@ export function buildMessages(
|
||||
promptConfig?: PromptConfig,
|
||||
systemPrompt?: string,
|
||||
prompt?: string,
|
||||
): Array<{role: string; content: string}> {
|
||||
): Array<{role: 'system' | 'user' | 'assistant' | 'tool'; content: string}> {
|
||||
if (promptConfig?.messages && promptConfig.messages.length > 0) {
|
||||
// Use new message format
|
||||
return promptConfig.messages.map(msg => ({
|
||||
role: msg.role,
|
||||
role: msg.role as 'system' | 'user' | 'assistant' | 'tool',
|
||||
content: msg.content,
|
||||
}))
|
||||
} else {
|
||||
|
||||
136
src/inference.ts
136
src/inference.ts
@@ -1,25 +1,16 @@
|
||||
import * as core from '@actions/core'
|
||||
import ModelClient, {isUnexpected} from '@azure-rest/ai-inference'
|
||||
import {AzureKeyCredential} from '@azure/core-auth'
|
||||
import {GitHubMCPClient, executeToolCalls, MCPTool, ToolCall} from './mcp.js'
|
||||
import {handleUnexpectedResponse} from './helpers.js'
|
||||
import OpenAI from 'openai'
|
||||
import {GitHubMCPClient, executeToolCalls, ToolCall} from './mcp.js'
|
||||
|
||||
interface ChatMessage {
|
||||
role: string
|
||||
role: 'system' | 'user' | 'assistant' | 'tool'
|
||||
content: string | null
|
||||
tool_calls?: ToolCall[]
|
||||
}
|
||||
|
||||
interface ChatCompletionsRequestBody {
|
||||
messages: ChatMessage[]
|
||||
max_tokens: number
|
||||
model: string
|
||||
response_format?: {type: 'json_schema'; json_schema: unknown}
|
||||
tools?: MCPTool[]
|
||||
tool_call_id?: string
|
||||
}
|
||||
|
||||
export interface InferenceRequest {
|
||||
messages: Array<{role: string; content: string}>
|
||||
messages: Array<{role: 'system' | 'user' | 'assistant' | 'tool'; content: string}>
|
||||
modelName: string
|
||||
maxTokens: number
|
||||
endpoint: string
|
||||
@@ -45,33 +36,38 @@ export interface InferenceResponse {
|
||||
export async function simpleInference(request: InferenceRequest): Promise<string | null> {
|
||||
core.info('Running simple inference without tools')
|
||||
|
||||
const client = ModelClient(request.endpoint, new AzureKeyCredential(request.token), {
|
||||
userAgentOptions: {userAgentPrefix: 'github-actions-ai-inference'},
|
||||
const client = new OpenAI({
|
||||
apiKey: request.token,
|
||||
baseURL: request.endpoint,
|
||||
})
|
||||
|
||||
const requestBody: ChatCompletionsRequestBody = {
|
||||
messages: request.messages,
|
||||
const chatCompletionRequest: OpenAI.Chat.Completions.ChatCompletionCreateParams = {
|
||||
messages: request.messages as OpenAI.Chat.Completions.ChatCompletionMessageParam[],
|
||||
max_tokens: request.maxTokens,
|
||||
model: request.modelName,
|
||||
}
|
||||
|
||||
// Add response format if specified
|
||||
if (request.responseFormat) {
|
||||
requestBody.response_format = request.responseFormat
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
chatCompletionRequest.response_format = request.responseFormat as any
|
||||
}
|
||||
|
||||
const response = await client.path('/chat/completions').post({
|
||||
body: requestBody,
|
||||
})
|
||||
try {
|
||||
const response = await client.chat.completions.create(chatCompletionRequest)
|
||||
|
||||
if (isUnexpected(response)) {
|
||||
handleUnexpectedResponse(response)
|
||||
if ('choices' in response) {
|
||||
const modelResponse = response.choices[0]?.message?.content
|
||||
core.info(`Model response: ${modelResponse || 'No response content'}`)
|
||||
return modelResponse || null
|
||||
} else {
|
||||
core.error('Unexpected response format from OpenAI API')
|
||||
return null
|
||||
}
|
||||
} catch (error) {
|
||||
core.error(`OpenAI API error: ${error}`)
|
||||
throw error
|
||||
}
|
||||
|
||||
const modelResponse = response.body.choices[0].message.content
|
||||
core.info(`Model response: ${modelResponse || 'No response content'}`)
|
||||
|
||||
return modelResponse
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,8 +79,9 @@ export async function mcpInference(
|
||||
): Promise<string | null> {
|
||||
core.info('Running GitHub MCP inference with tools')
|
||||
|
||||
const client = ModelClient(request.endpoint, new AzureKeyCredential(request.token), {
|
||||
userAgentOptions: {userAgentPrefix: 'github-actions-ai-inference'},
|
||||
const client = new OpenAI({
|
||||
apiKey: request.token,
|
||||
baseURL: request.endpoint,
|
||||
})
|
||||
|
||||
// Start with the pre-processed messages
|
||||
@@ -97,52 +94,57 @@ export async function mcpInference(
|
||||
iterationCount++
|
||||
core.info(`MCP inference iteration ${iterationCount}`)
|
||||
|
||||
const requestBody: ChatCompletionsRequestBody = {
|
||||
messages: messages,
|
||||
const chatCompletionRequest: OpenAI.Chat.Completions.ChatCompletionCreateParams = {
|
||||
messages: messages as OpenAI.Chat.Completions.ChatCompletionMessageParam[],
|
||||
max_tokens: request.maxTokens,
|
||||
model: request.modelName,
|
||||
tools: githubMcpClient.tools,
|
||||
tools: githubMcpClient.tools as OpenAI.Chat.Completions.ChatCompletionTool[],
|
||||
}
|
||||
|
||||
// Add response format if specified (only on first iteration to avoid conflicts)
|
||||
if (iterationCount === 1 && request.responseFormat) {
|
||||
requestBody.response_format = request.responseFormat
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
chatCompletionRequest.response_format = request.responseFormat as any
|
||||
}
|
||||
|
||||
const response = await client.path('/chat/completions').post({
|
||||
body: requestBody,
|
||||
})
|
||||
try {
|
||||
const response = await client.chat.completions.create(chatCompletionRequest)
|
||||
|
||||
if (isUnexpected(response)) {
|
||||
handleUnexpectedResponse(response)
|
||||
if (!('choices' in response)) {
|
||||
core.error('Unexpected response format from OpenAI API')
|
||||
return null
|
||||
}
|
||||
|
||||
const assistantMessage = response.choices[0]?.message
|
||||
const modelResponse = assistantMessage?.content
|
||||
const toolCalls = assistantMessage?.tool_calls
|
||||
|
||||
core.info(`Model response: ${modelResponse || 'No response content'}`)
|
||||
|
||||
messages.push({
|
||||
role: 'assistant',
|
||||
content: modelResponse || '',
|
||||
...(toolCalls && {tool_calls: toolCalls as ToolCall[]}),
|
||||
})
|
||||
|
||||
if (!toolCalls || toolCalls.length === 0) {
|
||||
core.info('No tool calls requested, ending GitHub MCP inference loop')
|
||||
return modelResponse || null
|
||||
}
|
||||
|
||||
core.info(`Model requested ${toolCalls.length} tool calls`)
|
||||
|
||||
// Execute all tool calls via GitHub MCP
|
||||
const toolResults = await executeToolCalls(githubMcpClient.client, toolCalls as ToolCall[])
|
||||
|
||||
// Add tool results to the conversation
|
||||
messages.push(...toolResults)
|
||||
|
||||
core.info('Tool results added, continuing conversation...')
|
||||
} catch (error) {
|
||||
core.error(`OpenAI API error: ${error}`)
|
||||
throw error
|
||||
}
|
||||
|
||||
const assistantMessage = response.body.choices[0].message
|
||||
const modelResponse = assistantMessage.content
|
||||
const toolCalls = assistantMessage.tool_calls
|
||||
|
||||
core.info(`Model response: ${modelResponse || 'No response content'}`)
|
||||
|
||||
messages.push({
|
||||
role: 'assistant',
|
||||
content: modelResponse || '',
|
||||
...(toolCalls && {tool_calls: toolCalls}),
|
||||
})
|
||||
|
||||
if (!toolCalls || toolCalls.length === 0) {
|
||||
core.info('No tool calls requested, ending GitHub MCP inference loop')
|
||||
return modelResponse
|
||||
}
|
||||
|
||||
core.info(`Model requested ${toolCalls.length} tool calls`)
|
||||
|
||||
// Execute all tool calls via GitHub MCP
|
||||
const toolResults = await executeToolCalls(githubMcpClient.client, toolCalls)
|
||||
|
||||
// Add tool results to the conversation
|
||||
messages.push(...toolResults)
|
||||
|
||||
core.info('Tool results added, continuing conversation...')
|
||||
}
|
||||
|
||||
core.warning(`GitHub MCP inference loop exceeded maximum iterations (${maxIterations})`)
|
||||
|
||||
Reference in New Issue
Block a user