Merge branch 'main' into sgoedecke/use-openai-sdk
This commit is contained in:
15
src/main.ts
15
src/main.ts
@@ -5,7 +5,13 @@ 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 {
|
||||
loadPromptFile,
|
||||
parseTemplateVariables,
|
||||
isPromptYamlFile,
|
||||
PromptConfig,
|
||||
parseFileTemplateVariables,
|
||||
} from './prompt.js'
|
||||
|
||||
const RESPONSE_FILE = 'modelResponse.txt'
|
||||
|
||||
@@ -18,6 +24,7 @@ export async function run(): Promise<void> {
|
||||
try {
|
||||
const promptFilePath = core.getInput('prompt-file')
|
||||
const inputVariables = core.getInput('input')
|
||||
const fileInputVariables = core.getInput('file_input')
|
||||
|
||||
let promptConfig: PromptConfig | undefined = undefined
|
||||
let systemPrompt: string | undefined = undefined
|
||||
@@ -27,8 +34,10 @@ export async function run(): Promise<void> {
|
||||
if (promptFilePath && isPromptYamlFile(promptFilePath)) {
|
||||
core.info('Using prompt YAML file format')
|
||||
|
||||
// Parse template variables
|
||||
const templateVariables = parseTemplateVariables(inputVariables)
|
||||
// Parse template variables from both string inputs and file-based inputs
|
||||
const stringVars = parseTemplateVariables(inputVariables)
|
||||
const fileVars = parseFileTemplateVariables(fileInputVariables)
|
||||
const templateVariables = {...stringVars, ...fileVars}
|
||||
|
||||
// Load and process prompt file
|
||||
promptConfig = loadPromptFile(promptFilePath, templateVariables)
|
||||
|
||||
@@ -37,6 +37,47 @@ export function parseTemplateVariables(input: string): TemplateVariables {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse file-based template variables from YAML input string. The YAML should map
|
||||
* variable names to file paths. File contents are read and returned as variables.
|
||||
*/
|
||||
export function parseFileTemplateVariables(fileInput: string): TemplateVariables {
|
||||
if (!fileInput.trim()) {
|
||||
return {}
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = yaml.load(fileInput) as Record<string, unknown>
|
||||
if (typeof parsed !== 'object' || parsed === null) {
|
||||
throw new Error('File template variables must be a YAML object')
|
||||
}
|
||||
|
||||
const result: TemplateVariables = {}
|
||||
for (const [key, value] of Object.entries(parsed)) {
|
||||
if (typeof value !== 'string') {
|
||||
throw new Error(`File template variable '${key}' must be a string file path`)
|
||||
}
|
||||
const filePath = value
|
||||
if (!fs.existsSync(filePath)) {
|
||||
throw new Error(`File for template variable '${key}' was not found: ${filePath}`)
|
||||
}
|
||||
try {
|
||||
result[key] = fs.readFileSync(filePath, 'utf-8')
|
||||
} catch (err) {
|
||||
throw new Error(
|
||||
`Failed to read file for template variable '${key}' at path '${filePath}': ${err instanceof Error ? err.message : 'Unknown error'}`,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`Failed to parse file template variables: ${error instanceof Error ? error.message : 'Unknown error'}`,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace template variables in text using {{variable}} syntax
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user