Files
ai-inference/src/inference.ts
T

158 lines
4.4 KiB
TypeScript
Raw Normal View History

2025-07-16 00:12:41 +00:00
import * as core from '@actions/core'
2025-07-24 19:11:15 +10:00
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'
2025-07-16 00:12:41 +00:00
2025-07-21 04:31:06 +00:00
interface ChatMessage {
role: string
content: string | null
tool_calls?: ToolCall[]
}
interface ChatCompletionsRequestBody {
messages: ChatMessage[]
max_tokens: number
model: string
2025-07-24 19:11:15 +10:00
response_format?: {type: 'json_schema'; json_schema: unknown}
2025-07-21 04:31:06 +00:00
tools?: MCPTool[]
}
2025-07-16 00:12:41 +00:00
export interface InferenceRequest {
2025-07-24 19:11:15 +10:00
messages: Array<{role: string; content: string}>
2025-07-16 00:12:41 +00:00
modelName: string
maxTokens: number
endpoint: string
token: string
2025-07-24 19:11:15 +10:00
responseFormat?: {type: 'json_schema'; json_schema: unknown} // Processed response format for the API
2025-07-16 00:12:41 +00:00
}
export interface InferenceResponse {
content: string | null
2025-07-16 02:19:49 +00:00
toolCalls?: Array<{
id: string
type: string
function: {
name: string
arguments: string
}
}>
2025-07-16 00:12:41 +00:00
}
/**
* Simple one-shot inference without tools
*/
2025-07-24 19:11:15 +10:00
export async function simpleInference(request: InferenceRequest): Promise<string | null> {
2025-07-16 00:12:41 +00:00
core.info('Running simple inference without tools')
2025-07-24 19:11:15 +10:00
const client = ModelClient(request.endpoint, new AzureKeyCredential(request.token), {
userAgentOptions: {userAgentPrefix: 'github-actions-ai-inference'},
})
2025-07-16 00:12:41 +00:00
2025-07-21 04:31:06 +00:00
const requestBody: ChatCompletionsRequestBody = {
2025-07-21 00:11:26 +00:00
messages: request.messages,
2025-07-16 00:12:41 +00:00
max_tokens: request.maxTokens,
2025-07-24 19:11:15 +10:00
model: request.modelName,
2025-07-16 00:12:41 +00:00
}
2025-07-21 00:11:26 +00:00
// Add response format if specified
if (request.responseFormat) {
requestBody.response_format = request.responseFormat
}
2025-07-16 00:12:41 +00:00
const response = await client.path('/chat/completions').post({
2025-07-24 19:11:15 +10:00
body: requestBody,
2025-07-16 00:12:41 +00:00
})
if (isUnexpected(response)) {
2025-07-16 02:56:55 +00:00
handleUnexpectedResponse(response)
2025-07-16 00:12:41 +00:00
}
const modelResponse = response.body.choices[0].message.content
core.info(`Model response: ${modelResponse || 'No response content'}`)
return modelResponse
}
/**
2025-07-16 02:19:49 +00:00
* GitHub MCP-enabled inference with tool execution loop
2025-07-16 00:12:41 +00:00
*/
export async function mcpInference(
request: InferenceRequest,
2025-07-24 19:11:15 +10:00
githubMcpClient: GitHubMCPClient,
2025-07-16 00:12:41 +00:00
): Promise<string | null> {
2025-07-16 02:19:49 +00:00
core.info('Running GitHub MCP inference with tools')
2025-07-16 00:12:41 +00:00
2025-07-24 19:11:15 +10:00
const client = ModelClient(request.endpoint, new AzureKeyCredential(request.token), {
userAgentOptions: {userAgentPrefix: 'github-actions-ai-inference'},
})
2025-07-16 00:12:41 +00:00
2025-07-21 00:11:26 +00:00
// Start with the pre-processed messages
2025-07-21 04:31:06 +00:00
const messages: ChatMessage[] = [...request.messages]
2025-07-16 00:12:41 +00:00
let iterationCount = 0
const maxIterations = 5 // Prevent infinite loops
while (iterationCount < maxIterations) {
iterationCount++
core.info(`MCP inference iteration ${iterationCount}`)
2025-07-21 04:31:06 +00:00
const requestBody: ChatCompletionsRequestBody = {
2025-07-16 00:12:41 +00:00
messages: messages,
max_tokens: request.maxTokens,
model: request.modelName,
2025-07-24 19:11:15 +10:00
tools: githubMcpClient.tools,
2025-07-16 00:12:41 +00:00
}
2025-07-21 00:11:26 +00:00
// Add response format if specified (only on first iteration to avoid conflicts)
if (iterationCount === 1 && request.responseFormat) {
requestBody.response_format = request.responseFormat
}
2025-07-16 00:12:41 +00:00
const response = await client.path('/chat/completions').post({
2025-07-24 19:11:15 +10:00
body: requestBody,
2025-07-16 00:12:41 +00:00
})
if (isUnexpected(response)) {
2025-07-16 02:56:55 +00:00
handleUnexpectedResponse(response)
2025-07-16 00:12:41 +00:00
}
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',
2025-07-16 02:19:49 +00:00
content: modelResponse || '',
2025-07-24 19:11:15 +10:00
...(toolCalls && {tool_calls: toolCalls}),
2025-07-16 00:12:41 +00:00
})
if (!toolCalls || toolCalls.length === 0) {
2025-07-16 02:19:49 +00:00
core.info('No tool calls requested, ending GitHub MCP inference loop')
2025-07-16 00:12:41 +00:00
return modelResponse
}
core.info(`Model requested ${toolCalls.length} tool calls`)
2025-07-16 02:19:49 +00:00
// Execute all tool calls via GitHub MCP
2025-07-24 19:11:15 +10:00
const toolResults = await executeToolCalls(githubMcpClient.client, toolCalls)
2025-07-16 02:19:49 +00:00
// Add tool results to the conversation
2025-07-16 00:12:41 +00:00
messages.push(...toolResults)
core.info('Tool results added, continuing conversation...')
}
2025-07-24 19:11:15 +10:00
core.warning(`GitHub MCP inference loop exceeded maximum iterations (${maxIterations})`)
2025-07-16 00:12:41 +00:00
// Return the last assistant message content
const lastAssistantMessage = messages
.slice()
.reverse()
2025-07-24 19:11:15 +10:00
.find(msg => msg.role === 'assistant')
2025-07-16 00:12:41 +00:00
return lastAssistantMessage?.content || null
}