|
| 1 | +import * as core from '@actions/core' |
| 2 | +import ModelClient, { isUnexpected } from '@azure-rest/ai-inference' |
| 3 | +import { AzureKeyCredential } from '@azure/core-auth' |
| 4 | +import { GitHubMCPClient, executeToolCalls, MCPTool, ToolCall } from './mcp.js' |
| 5 | +import { handleUnexpectedResponse } from './helpers.js' |
| 6 | + |
| 7 | +interface ChatMessage { |
| 8 | + role: string |
| 9 | + content: string | null |
| 10 | + tool_calls?: ToolCall[] |
| 11 | +} |
| 12 | + |
| 13 | +interface ChatCompletionsRequestBody { |
| 14 | + messages: ChatMessage[] |
| 15 | + max_tokens: number |
| 16 | + model: string |
| 17 | + response_format?: { type: 'json_schema'; json_schema: unknown } |
| 18 | + tools?: MCPTool[] |
| 19 | +} |
| 20 | + |
| 21 | +export interface InferenceRequest { |
| 22 | + messages: Array<{ role: string; content: string }> |
| 23 | + modelName: string |
| 24 | + maxTokens: number |
| 25 | + endpoint: string |
| 26 | + token: string |
| 27 | + responseFormat?: { type: 'json_schema'; json_schema: unknown } // Processed response format for the API |
| 28 | +} |
| 29 | + |
| 30 | +export interface InferenceResponse { |
| 31 | + content: string | null |
| 32 | + toolCalls?: Array<{ |
| 33 | + id: string |
| 34 | + type: string |
| 35 | + function: { |
| 36 | + name: string |
| 37 | + arguments: string |
| 38 | + } |
| 39 | + }> |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Simple one-shot inference without tools |
| 44 | + */ |
| 45 | +export async function simpleInference( |
| 46 | + request: InferenceRequest |
| 47 | +): Promise<string | null> { |
| 48 | + core.info('Running simple inference without tools') |
| 49 | + |
| 50 | + const client = ModelClient( |
| 51 | + request.endpoint, |
| 52 | + new AzureKeyCredential(request.token), |
| 53 | + { |
| 54 | + userAgentOptions: { userAgentPrefix: 'github-actions-ai-inference' } |
| 55 | + } |
| 56 | + ) |
| 57 | + |
| 58 | + const requestBody: ChatCompletionsRequestBody = { |
| 59 | + messages: request.messages, |
| 60 | + max_tokens: request.maxTokens, |
| 61 | + model: request.modelName |
| 62 | + } |
| 63 | + |
| 64 | + // Add response format if specified |
| 65 | + if (request.responseFormat) { |
| 66 | + requestBody.response_format = request.responseFormat |
| 67 | + } |
| 68 | + |
| 69 | + const response = await client.path('/chat/completions').post({ |
| 70 | + body: requestBody |
| 71 | + }) |
| 72 | + |
| 73 | + if (isUnexpected(response)) { |
| 74 | + handleUnexpectedResponse(response) |
| 75 | + } |
| 76 | + |
| 77 | + const modelResponse = response.body.choices[0].message.content |
| 78 | + core.info(`Model response: ${modelResponse || 'No response content'}`) |
| 79 | + |
| 80 | + return modelResponse |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * GitHub MCP-enabled inference with tool execution loop |
| 85 | + */ |
| 86 | +export async function mcpInference( |
| 87 | + request: InferenceRequest, |
| 88 | + githubMcpClient: GitHubMCPClient |
| 89 | +): Promise<string | null> { |
| 90 | + core.info('Running GitHub MCP inference with tools') |
| 91 | + |
| 92 | + const client = ModelClient( |
| 93 | + request.endpoint, |
| 94 | + new AzureKeyCredential(request.token), |
| 95 | + { |
| 96 | + userAgentOptions: { userAgentPrefix: 'github-actions-ai-inference' } |
| 97 | + } |
| 98 | + ) |
| 99 | + |
| 100 | + // Start with the pre-processed messages |
| 101 | + const messages: ChatMessage[] = [...request.messages] |
| 102 | + |
| 103 | + let iterationCount = 0 |
| 104 | + const maxIterations = 5 // Prevent infinite loops |
| 105 | + |
| 106 | + while (iterationCount < maxIterations) { |
| 107 | + iterationCount++ |
| 108 | + core.info(`MCP inference iteration ${iterationCount}`) |
| 109 | + |
| 110 | + const requestBody: ChatCompletionsRequestBody = { |
| 111 | + messages: messages, |
| 112 | + max_tokens: request.maxTokens, |
| 113 | + model: request.modelName, |
| 114 | + tools: githubMcpClient.tools |
| 115 | + } |
| 116 | + |
| 117 | + // Add response format if specified (only on first iteration to avoid conflicts) |
| 118 | + if (iterationCount === 1 && request.responseFormat) { |
| 119 | + requestBody.response_format = request.responseFormat |
| 120 | + } |
| 121 | + |
| 122 | + const response = await client.path('/chat/completions').post({ |
| 123 | + body: requestBody |
| 124 | + }) |
| 125 | + |
| 126 | + if (isUnexpected(response)) { |
| 127 | + handleUnexpectedResponse(response) |
| 128 | + } |
| 129 | + |
| 130 | + const assistantMessage = response.body.choices[0].message |
| 131 | + const modelResponse = assistantMessage.content |
| 132 | + const toolCalls = assistantMessage.tool_calls |
| 133 | + |
| 134 | + core.info(`Model response: ${modelResponse || 'No response content'}`) |
| 135 | + |
| 136 | + messages.push({ |
| 137 | + role: 'assistant', |
| 138 | + content: modelResponse || '', |
| 139 | + ...(toolCalls && { tool_calls: toolCalls }) |
| 140 | + }) |
| 141 | + |
| 142 | + if (!toolCalls || toolCalls.length === 0) { |
| 143 | + core.info('No tool calls requested, ending GitHub MCP inference loop') |
| 144 | + return modelResponse |
| 145 | + } |
| 146 | + |
| 147 | + core.info(`Model requested ${toolCalls.length} tool calls`) |
| 148 | + |
| 149 | + // Execute all tool calls via GitHub MCP |
| 150 | + const toolResults = await executeToolCalls( |
| 151 | + githubMcpClient.client, |
| 152 | + toolCalls |
| 153 | + ) |
| 154 | + |
| 155 | + // Add tool results to the conversation |
| 156 | + messages.push(...toolResults) |
| 157 | + |
| 158 | + core.info('Tool results added, continuing conversation...') |
| 159 | + } |
| 160 | + |
| 161 | + core.warning( |
| 162 | + `GitHub MCP inference loop exceeded maximum iterations (${maxIterations})` |
| 163 | + ) |
| 164 | + |
| 165 | + // Return the last assistant message content |
| 166 | + const lastAssistantMessage = messages |
| 167 | + .slice() |
| 168 | + .reverse() |
| 169 | + .find((msg) => msg.role === 'assistant') |
| 170 | + |
| 171 | + return lastAssistantMessage?.content || null |
| 172 | +} |
0 commit comments