chore: use github's shared prettier-config

This commit is contained in:
Marais Rossouw
2025-07-24 19:11:15 +10:00
parent a2235c5511
commit 7e2aa19f3b
26 changed files with 351 additions and 559 deletions

View File

@@ -1,8 +1,8 @@
import * as core from '@actions/core'
import { GetChatCompletionsDefaultResponse } from '@azure-rest/ai-inference'
import {GetChatCompletionsDefaultResponse} from '@azure-rest/ai-inference'
import * as fs from 'fs'
import { PromptConfig } from './prompt.js'
import { InferenceRequest } from './inference.js'
import {PromptConfig} from './prompt.js'
import {InferenceRequest} from './inference.js'
/**
* Helper function to load content from a file or use fallback input
@@ -11,11 +11,7 @@ import { InferenceRequest } from './inference.js'
* @param defaultValue - Default value to use if neither file nor content is provided
* @returns The loaded content
*/
export function loadContentFromFileOrInput(
filePathInput: string,
contentInput: string,
defaultValue?: string
): string {
export function loadContentFromFileOrInput(filePathInput: string, contentInput: string, defaultValue?: string): string {
const filePath = core.getInput(filePathInput)
const contentString = core.getInput(contentInput)
@@ -38,9 +34,7 @@ export function loadContentFromFileOrInput(
* @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 {
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})` : ''
@@ -54,16 +48,14 @@ export function handleUnexpectedResponse(
if (!response.body) {
throw new Error(
`Failed to get response from AI service (status: ${response.status})${errorCodeMsg}. ` +
'Please check network connection and endpoint configuration.'
'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))
(typeof response.body === 'string' ? response.body : JSON.stringify(response.body)),
)
}
@@ -73,22 +65,22 @@ export function handleUnexpectedResponse(
export function buildMessages(
promptConfig?: PromptConfig,
systemPrompt?: string,
prompt?: string
): Array<{ role: string; content: string }> {
prompt?: string,
): Array<{role: string; content: string}> {
if (promptConfig?.messages && promptConfig.messages.length > 0) {
// Use new message format
return promptConfig.messages.map((msg) => ({
return promptConfig.messages.map(msg => ({
role: msg.role,
content: msg.content
content: msg.content,
}))
} else {
// Use legacy format
return [
{
role: 'system',
content: systemPrompt || 'You are a helpful assistant'
content: systemPrompt || 'You are a helpful assistant',
},
{ role: 'user', content: prompt || '' }
{role: 'user', content: prompt || ''},
]
}
}
@@ -97,22 +89,17 @@ export function buildMessages(
* Build response format object for API from prompt config
*/
export function buildResponseFormat(
promptConfig?: PromptConfig
): { type: 'json_schema'; json_schema: unknown } | undefined {
if (
promptConfig?.responseFormat === 'json_schema' &&
promptConfig.jsonSchema
) {
promptConfig?: PromptConfig,
): {type: 'json_schema'; json_schema: unknown} | undefined {
if (promptConfig?.responseFormat === 'json_schema' && promptConfig.jsonSchema) {
try {
const schema = JSON.parse(promptConfig.jsonSchema)
return {
type: 'json_schema',
json_schema: schema
json_schema: schema,
}
} catch (error) {
throw new Error(
`Invalid JSON schema: ${error instanceof Error ? error.message : 'Unknown error'}`
)
throw new Error(`Invalid JSON schema: ${error instanceof Error ? error.message : 'Unknown error'}`)
}
}
return undefined
@@ -128,7 +115,7 @@ export function buildInferenceRequest(
modelName: string,
maxTokens: number,
endpoint: string,
token: string
token: string,
): InferenceRequest {
const messages = buildMessages(promptConfig, systemPrompt, prompt)
const responseFormat = buildResponseFormat(promptConfig)
@@ -139,6 +126,6 @@ export function buildInferenceRequest(
maxTokens,
endpoint,
token,
responseFormat
responseFormat,
}
}

View File

@@ -2,7 +2,7 @@
* The entrypoint for the action. This file simply imports and runs the action's
* main logic.
*/
import { run } from './main.js'
import {run} from './main.js'
/* istanbul ignore next */
run()

View File

@@ -1,8 +1,8 @@
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 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'
interface ChatMessage {
role: string
@@ -14,17 +14,17 @@ interface ChatCompletionsRequestBody {
messages: ChatMessage[]
max_tokens: number
model: string
response_format?: { type: 'json_schema'; json_schema: unknown }
response_format?: {type: 'json_schema'; json_schema: unknown}
tools?: MCPTool[]
}
export interface InferenceRequest {
messages: Array<{ role: string; content: string }>
messages: Array<{role: string; content: string}>
modelName: string
maxTokens: number
endpoint: string
token: string
responseFormat?: { type: 'json_schema'; json_schema: unknown } // Processed response format for the API
responseFormat?: {type: 'json_schema'; json_schema: unknown} // Processed response format for the API
}
export interface InferenceResponse {
@@ -42,23 +42,17 @@ export interface InferenceResponse {
/**
* Simple one-shot inference without tools
*/
export async function simpleInference(
request: InferenceRequest
): Promise<string | null> {
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 = ModelClient(request.endpoint, new AzureKeyCredential(request.token), {
userAgentOptions: {userAgentPrefix: 'github-actions-ai-inference'},
})
const requestBody: ChatCompletionsRequestBody = {
messages: request.messages,
max_tokens: request.maxTokens,
model: request.modelName
model: request.modelName,
}
// Add response format if specified
@@ -67,7 +61,7 @@ export async function simpleInference(
}
const response = await client.path('/chat/completions').post({
body: requestBody
body: requestBody,
})
if (isUnexpected(response)) {
@@ -85,17 +79,13 @@ export async function simpleInference(
*/
export async function mcpInference(
request: InferenceRequest,
githubMcpClient: GitHubMCPClient
githubMcpClient: GitHubMCPClient,
): 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 = ModelClient(request.endpoint, new AzureKeyCredential(request.token), {
userAgentOptions: {userAgentPrefix: 'github-actions-ai-inference'},
})
// Start with the pre-processed messages
const messages: ChatMessage[] = [...request.messages]
@@ -111,7 +101,7 @@ export async function mcpInference(
messages: messages,
max_tokens: request.maxTokens,
model: request.modelName,
tools: githubMcpClient.tools
tools: githubMcpClient.tools,
}
// Add response format if specified (only on first iteration to avoid conflicts)
@@ -120,7 +110,7 @@ export async function mcpInference(
}
const response = await client.path('/chat/completions').post({
body: requestBody
body: requestBody,
})
if (isUnexpected(response)) {
@@ -136,7 +126,7 @@ export async function mcpInference(
messages.push({
role: 'assistant',
content: modelResponse || '',
...(toolCalls && { tool_calls: toolCalls })
...(toolCalls && {tool_calls: toolCalls}),
})
if (!toolCalls || toolCalls.length === 0) {
@@ -147,10 +137,7 @@ export async function mcpInference(
core.info(`Model requested ${toolCalls.length} tool calls`)
// Execute all tool calls via GitHub MCP
const toolResults = await executeToolCalls(
githubMcpClient.client,
toolCalls
)
const toolResults = await executeToolCalls(githubMcpClient.client, toolCalls)
// Add tool results to the conversation
messages.push(...toolResults)
@@ -158,15 +145,13 @@ export async function mcpInference(
core.info('Tool results added, continuing conversation...')
}
core.warning(
`GitHub MCP inference loop exceeded maximum iterations (${maxIterations})`
)
core.warning(`GitHub MCP inference loop exceeded maximum iterations (${maxIterations})`)
// Return the last assistant message content
const lastAssistantMessage = messages
.slice()
.reverse()
.find((msg) => msg.role === 'assistant')
.find(msg => msg.role === 'assistant')
return lastAssistantMessage?.content || null
}

View File

@@ -2,15 +2,10 @@ import * as core from '@actions/core'
import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
import { connectToGitHubMCP } from './mcp.js'
import { simpleInference, mcpInference } from './inference.js'
import { loadContentFromFileOrInput, buildInferenceRequest } from './helpers.js'
import {
loadPromptFile,
parseTemplateVariables,
isPromptYamlFile,
PromptConfig
} from './prompt.js'
import {connectToGitHubMCP} from './mcp.js'
import {simpleInference, mcpInference} from './inference.js'
import {loadContentFromFileOrInput, buildInferenceRequest} from './helpers.js'
import {loadPromptFile, parseTemplateVariables, isPromptYamlFile, PromptConfig} from './prompt.js'
const RESPONSE_FILE = 'modelResponse.txt'
@@ -42,11 +37,7 @@ export async function run(): Promise<void> {
core.info('Using legacy prompt format')
prompt = loadContentFromFileOrInput('prompt-file', 'prompt')
systemPrompt = loadContentFromFileOrInput(
'system-prompt-file',
'system-prompt',
'You are a helpful assistant'
)
systemPrompt = loadContentFromFileOrInput('system-prompt-file', 'system-prompt', 'You are a helpful assistant')
}
// Get common parameters
@@ -68,7 +59,7 @@ export async function run(): Promise<void> {
modelName,
maxTokens,
endpoint,
token
token,
)
const enableMcp = core.getBooleanInput('enable-github-mcp') || false

View File

@@ -1,6 +1,6 @@
import * as core from '@actions/core'
import { Client } from '@modelcontextprotocol/sdk/client/index.js'
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'
import {Client} from '@modelcontextprotocol/sdk/client/index.js'
import {StreamableHTTPClientTransport} from '@modelcontextprotocol/sdk/client/streamableHttp.js'
export interface ToolResult {
tool_call_id: string
@@ -35,9 +35,7 @@ export interface GitHubMCPClient {
/**
* Connect to the GitHub MCP server and retrieve available tools
*/
export async function connectToGitHubMCP(
token: string
): Promise<GitHubMCPClient | null> {
export async function connectToGitHubMCP(token: string): Promise<GitHubMCPClient | null> {
const githubMcpUrl = 'https://api.githubcopilot.com/mcp/'
core.info('Connecting to GitHub MCP server...')
@@ -46,15 +44,15 @@ export async function connectToGitHubMCP(
requestInit: {
headers: {
Authorization: `Bearer ${token}`,
'X-MCP-Readonly': 'true'
}
}
'X-MCP-Readonly': 'true',
},
},
})
const client = new Client({
name: 'ai-inference-action',
version: '1.0.0',
transport
transport,
})
try {
@@ -67,42 +65,35 @@ export async function connectToGitHubMCP(
core.info('Successfully connected to GitHub MCP server')
const toolsResponse = await client.listTools()
core.info(
`Retrieved ${toolsResponse.tools?.length || 0} tools from GitHub MCP server`
)
core.info(`Retrieved ${toolsResponse.tools?.length || 0} tools from GitHub MCP server`)
// Map GitHub MCP tools → Azure AI Inference tool definitions
const tools = (toolsResponse.tools || []).map((t) => ({
const tools = (toolsResponse.tools || []).map(t => ({
type: 'function' as const,
function: {
name: t.name,
description: t.description,
parameters: t.inputSchema
}
parameters: t.inputSchema,
},
}))
core.info(`Mapped ${tools.length} GitHub MCP tools for Azure AI Inference`)
return { client, tools }
return {client, tools}
}
/**
* Execute a single tool call via GitHub MCP
*/
export async function executeToolCall(
githubMcpClient: Client,
toolCall: ToolCall
): Promise<ToolResult> {
core.info(
`Executing GitHub MCP tool: ${toolCall.function.name} with args: ${toolCall.function.arguments}`
)
export async function executeToolCall(githubMcpClient: Client, toolCall: ToolCall): Promise<ToolResult> {
core.info(`Executing GitHub MCP tool: ${toolCall.function.name} with args: ${toolCall.function.arguments}`)
try {
const args = JSON.parse(toolCall.function.arguments)
const result = await githubMcpClient.callTool({
name: toolCall.function.name,
arguments: args
arguments: args,
})
core.info(`GitHub MCP tool ${toolCall.function.name} executed successfully`)
@@ -111,18 +102,16 @@ export async function executeToolCall(
tool_call_id: toolCall.id,
role: 'tool',
name: toolCall.function.name,
content: JSON.stringify(result.content)
content: JSON.stringify(result.content),
}
} catch (toolError) {
core.warning(
`Failed to execute GitHub MCP tool ${toolCall.function.name}: ${toolError}`
)
core.warning(`Failed to execute GitHub MCP tool ${toolCall.function.name}: ${toolError}`)
return {
tool_call_id: toolCall.id,
role: 'tool',
name: toolCall.function.name,
content: `Error: ${toolError}`
content: `Error: ${toolError}`,
}
}
}
@@ -130,10 +119,7 @@ export async function executeToolCall(
/**
* Execute all tool calls from a response via GitHub MCP
*/
export async function executeToolCalls(
githubMcpClient: Client,
toolCalls: ToolCall[]
): Promise<ToolResult[]> {
export async function executeToolCalls(githubMcpClient: Client, toolCalls: ToolCall[]): Promise<ToolResult[]> {
const toolResults: ToolResult[] = []
for (const toolCall of toolCalls) {

View File

@@ -33,26 +33,19 @@ export function parseTemplateVariables(input: string): TemplateVariables {
}
return parsed
} catch (error) {
throw new Error(
`Failed to parse template variables: ${error instanceof Error ? error.message : 'Unknown error'}`
)
throw new Error(`Failed to parse template variables: ${error instanceof Error ? error.message : 'Unknown error'}`)
}
}
/**
* Replace template variables in text using {{variable}} syntax
*/
export function replaceTemplateVariables(
text: string,
variables: TemplateVariables
): string {
export function replaceTemplateVariables(text: string, variables: TemplateVariables): string {
return text.replace(/\{\{([\w.-]+)\}\}/g, (match, variableName) => {
if (variableName in variables) {
return variables[variableName]
}
core.warning(
`Template variable '${variableName}' not found in input variables`
)
core.warning(`Template variable '${variableName}' not found in input variables`)
return match // Return the original placeholder if variable not found
})
}
@@ -60,10 +53,7 @@ export function replaceTemplateVariables(
/**
* Load and parse a prompt YAML file with template variable substitution
*/
export function loadPromptFile(
filePath: string,
templateVariables: TemplateVariables = {}
): PromptConfig {
export function loadPromptFile(filePath: string, templateVariables: TemplateVariables = {}): PromptConfig {
if (!fs.existsSync(filePath)) {
throw new Error(`Prompt file not found: ${filePath}`)
}
@@ -71,10 +61,7 @@ export function loadPromptFile(
const fileContent = fs.readFileSync(filePath, 'utf-8')
// Apply template variable substitution
const processedContent = replaceTemplateVariables(
fileContent,
templateVariables
)
const processedContent = replaceTemplateVariables(fileContent, templateVariables)
try {
const config = yaml.load(processedContent) as PromptConfig
@@ -86,9 +73,7 @@ export function loadPromptFile(
// Validate messages
for (const message of config.messages) {
if (!message.role || !message.content) {
throw new Error(
'Each message must have "role" and "content" properties'
)
throw new Error('Each message must have "role" and "content" properties')
}
if (!['system', 'user', 'assistant'].includes(message.role)) {
throw new Error(`Invalid message role: ${message.role}`)
@@ -97,9 +82,7 @@ export function loadPromptFile(
return config
} catch (error) {
throw new Error(
`Failed to parse prompt file: ${error instanceof Error ? error.message : 'Unknown error'}`
)
throw new Error(`Failed to parse prompt file: ${error instanceof Error ? error.message : 'Unknown error'}`)
}
}