2025-04-04 07:27:58 +11:00
|
|
|
import * as core from '@actions/core'
|
2025-04-17 17:41:35 +00:00
|
|
|
import * as fs from 'fs'
|
|
|
|
|
import * as os from 'os'
|
|
|
|
|
import * as path from 'path'
|
2025-07-16 02:19:49 +00:00
|
|
|
import { connectToGitHubMCP } from './mcp.js'
|
2025-07-16 00:12:41 +00:00
|
|
|
import { simpleInference, mcpInference, InferenceRequest } from './inference.js'
|
|
|
|
|
import { loadContentFromFileOrInput } from './helpers.js'
|
2025-04-17 17:41:35 +00:00
|
|
|
|
|
|
|
|
const RESPONSE_FILE = 'modelResponse.txt'
|
2025-04-04 07:27:58 +11:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The main function for the action.
|
|
|
|
|
*
|
|
|
|
|
* @returns Resolves when the action is complete.
|
|
|
|
|
*/
|
|
|
|
|
export async function run(): Promise<void> {
|
|
|
|
|
try {
|
2025-05-26 03:46:39 +02:00
|
|
|
const prompt = loadContentFromFileOrInput('prompt-file', 'prompt')
|
2025-04-17 17:41:35 +00:00
|
|
|
|
2025-05-25 19:08:36 +02:00
|
|
|
const systemPrompt = loadContentFromFileOrInput(
|
|
|
|
|
'system-prompt-file',
|
|
|
|
|
'system-prompt',
|
|
|
|
|
'You are a helpful assistant'
|
|
|
|
|
)
|
2025-05-24 03:50:15 +02:00
|
|
|
|
2025-04-07 00:09:42 +00:00
|
|
|
const modelName: string = core.getInput('model')
|
2025-04-07 04:53:19 +00:00
|
|
|
const maxTokens: number = parseInt(core.getInput('max-tokens'), 10)
|
2025-04-04 07:27:58 +11:00
|
|
|
|
2025-07-15 23:23:39 +00:00
|
|
|
const token = process.env['GITHUB_TOKEN'] || core.getInput('token')
|
2025-04-03 21:55:32 +00:00
|
|
|
if (token === undefined) {
|
|
|
|
|
throw new Error('GITHUB_TOKEN is not set')
|
|
|
|
|
}
|
2025-04-08 04:55:27 +00:00
|
|
|
|
2025-04-08 20:22:47 +00:00
|
|
|
const endpoint = core.getInput('endpoint')
|
2025-07-16 02:19:49 +00:00
|
|
|
const enableMcp = core.getBooleanInput('enable-github-mcp') || false
|
2025-07-15 23:23:39 +00:00
|
|
|
|
2025-07-16 00:12:41 +00:00
|
|
|
const inferenceRequest: InferenceRequest = {
|
|
|
|
|
systemPrompt,
|
|
|
|
|
prompt,
|
|
|
|
|
modelName,
|
|
|
|
|
maxTokens,
|
|
|
|
|
endpoint,
|
|
|
|
|
token
|
2025-07-15 23:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
2025-07-16 00:12:41 +00:00
|
|
|
let modelResponse: string | null = null
|
2025-07-15 23:23:39 +00:00
|
|
|
|
2025-07-16 00:12:41 +00:00
|
|
|
if (enableMcp) {
|
2025-07-16 02:19:49 +00:00
|
|
|
const mcpClient = await connectToGitHubMCP(token)
|
2025-04-03 21:55:32 +00:00
|
|
|
|
2025-07-16 00:12:41 +00:00
|
|
|
if (mcpClient) {
|
|
|
|
|
modelResponse = await mcpInference(inferenceRequest, mcpClient)
|
|
|
|
|
} else {
|
|
|
|
|
core.warning('MCP connection failed, falling back to simple inference')
|
|
|
|
|
modelResponse = await simpleInference(inferenceRequest)
|
2025-07-15 23:23:39 +00:00
|
|
|
}
|
2025-07-16 00:12:41 +00:00
|
|
|
} else {
|
|
|
|
|
modelResponse = await simpleInference(inferenceRequest)
|
2025-07-15 23:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
2025-04-03 21:55:32 +00:00
|
|
|
core.setOutput('response', modelResponse || '')
|
2025-04-17 17:41:35 +00:00
|
|
|
|
|
|
|
|
const responseFilePath = path.join(tempDir(), RESPONSE_FILE)
|
2025-04-17 20:23:07 +00:00
|
|
|
core.setOutput('response-file', responseFilePath)
|
2025-04-17 17:41:35 +00:00
|
|
|
|
|
|
|
|
if (modelResponse && modelResponse !== '') {
|
|
|
|
|
fs.writeFileSync(responseFilePath, modelResponse, 'utf-8')
|
|
|
|
|
}
|
2025-04-04 07:27:58 +11:00
|
|
|
} catch (error) {
|
2025-04-07 23:52:33 +00:00
|
|
|
if (error instanceof Error) {
|
|
|
|
|
core.setFailed(error.message)
|
|
|
|
|
} else {
|
|
|
|
|
core.setFailed('An unexpected error occurred')
|
|
|
|
|
}
|
2025-04-04 07:27:58 +11:00
|
|
|
}
|
|
|
|
|
}
|
2025-04-17 17:41:35 +00:00
|
|
|
|
|
|
|
|
function tempDir(): string {
|
|
|
|
|
const tempDirectory = process.env['RUNNER_TEMP'] || os.tmpdir()
|
|
|
|
|
return tempDirectory
|
|
|
|
|
}
|