read prompt from file and print output to file

This commit is contained in:
Aiqiao Yan
2025-04-17 17:41:35 +00:00
parent c7105a4c1e
commit 43f6a3831f
8 changed files with 123 additions and 34 deletions
+28 -1
View File
@@ -1,6 +1,11 @@
import * as core from '@actions/core'
import ModelClient, { isUnexpected } from '@azure-rest/ai-inference'
import { AzureKeyCredential } from '@azure/core-auth'
import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
const RESPONSE_FILE = 'modelResponse.txt'
/**
* The main function for the action.
@@ -9,7 +14,16 @@ import { AzureKeyCredential } from '@azure/core-auth'
*/
export async function run(): Promise<void> {
try {
const prompt: string = core.getInput('prompt')
const promptFile: string = core.getInput('prompt-file')
let prompt: string = core.getInput('prompt')
if (promptFile !== undefined && promptFile !== '') {
if (!fs.existsSync(promptFile)) {
throw new Error(`Prompt file not found: ${promptFile}`)
}
prompt = fs.readFileSync(promptFile, 'utf-8')
}
if (prompt === undefined || prompt === '') {
throw new Error('prompt is not set')
}
@@ -60,6 +74,14 @@ export async function run(): Promise<void> {
// Set outputs for other workflow steps to use
core.setOutput('response', modelResponse || '')
// Save the response to a file in case the response overflow the output limit
const responseFilePath = path.join(tempDir(), RESPONSE_FILE)
core.setOutput('response-path', responseFilePath)
if (modelResponse && modelResponse !== '') {
fs.writeFileSync(responseFilePath, modelResponse, 'utf-8')
}
} catch (error) {
// Fail the workflow run if an error occurs
if (error instanceof Error) {
@@ -69,3 +91,8 @@ export async function run(): Promise<void> {
}
}
}
function tempDir(): string {
const tempDirectory = process.env['RUNNER_TEMP'] || os.tmpdir()
return tempDirectory
}