Refactor the prompt reading logic

This commit is contained in:
Matthew Leibowitz
2025-05-25 19:08:36 +02:00
parent 96e0fda3bb
commit 3e924fe06b
2 changed files with 43 additions and 32 deletions

View File

@@ -132,7 +132,7 @@ describe('main.ts', () => {
await run()
// Verify that the action was marked as failed.
expect(core.setFailed).toHaveBeenNthCalledWith(1, 'prompt is not set')
expect(core.setFailed).toHaveBeenNthCalledWith(1, 'Neither prompt-file nor prompt was set')
})
it('uses prompt-file', async () => {
@@ -172,7 +172,7 @@ describe('main.ts', () => {
// Verify that the error was correctly reported
expect(core.setFailed).toHaveBeenCalledWith(
`Prompt file not found: ${promptFile}`
`File for prompt-file was not found: ${promptFile}`
)
})
@@ -254,7 +254,7 @@ describe('main.ts', () => {
// Verify that the error was correctly reported
expect(core.setFailed).toHaveBeenCalledWith(
`System prompt file not found: ${systemPromptFile}`
`File for system-prompt-file was not found: ${systemPromptFile}`
)
})

View File

@@ -7,6 +7,35 @@ import * as path from 'path'
const RESPONSE_FILE = 'modelResponse.txt'
/**
* Helper function to load content from a file or use fallback input
* @param filePathInput - Input name for the file path
* @param contentInput - Input name for the direct content
* @param defaultValue - Default value to use if neither file nor content is provided
* @returns The loaded content
*/
function loadContentFromFileOrInput(
filePathInput: string,
contentInput: string,
defaultValue?: string
): string {
const filePath = core.getInput(filePathInput)
const contentString = core.getInput(contentInput)
if (filePath !== undefined && filePath !== '') {
if (!fs.existsSync(filePath)) {
throw new Error(`File for ${filePathInput} was not found: ${filePath}`)
}
return fs.readFileSync(filePath, 'utf-8')
} else if (contentString !== undefined && contentString !== '') {
return contentString
} else if (defaultValue !== undefined) {
return defaultValue
} else {
throw new Error(`Neither ${filePathInput} nor ${contentInput} was set`)
}
}
/**
* The main function for the action.
*
@@ -14,36 +43,18 @@ const RESPONSE_FILE = 'modelResponse.txt'
*/
export async function run(): Promise<void> {
try {
const promptFile: string = core.getInput('prompt-file')
const promptString: string = core.getInput('prompt')
// Load prompt content - required
const prompt = loadContentFromFileOrInput(
'prompt-file',
'prompt'
)
let prompt: string
if (promptFile !== undefined && promptFile !== '') {
if (!fs.existsSync(promptFile)) {
throw new Error(`Prompt file not found: ${promptFile}`)
}
prompt = fs.readFileSync(promptFile, 'utf-8')
} else if (promptString !== undefined && promptString !== '') {
prompt = promptString
} else {
throw new Error('prompt is not set')
}
const systemPromptFile: string = core.getInput('system-prompt-file')
const systemPromptString: string = core.getInput('system-prompt')
let systemPrompt: string
if (systemPromptFile !== undefined && systemPromptFile !== '') {
if (!fs.existsSync(systemPromptFile)) {
throw new Error(`System prompt file not found: ${systemPromptFile}`)
}
systemPrompt = fs.readFileSync(systemPromptFile, 'utf-8')
} else if (systemPromptString !== undefined && systemPromptString !== '') {
systemPrompt = systemPromptString
} else {
// Use default system prompt
systemPrompt = 'You are a helpful assistant'
}
// Load system prompt with default value
const systemPrompt = loadContentFromFileOrInput(
'system-prompt-file',
'system-prompt',
'You are a helpful assistant'
)
const modelName: string = core.getInput('model')
const maxTokens: number = parseInt(core.getInput('max-tokens'), 10)