Fixup types and tests
This commit is contained in:
@@ -1,31 +1,42 @@
|
|||||||
import { describe, it, expect, beforeEach, jest } from '@jest/globals'
|
import { describe, it, expect, beforeEach, jest } from '@jest/globals'
|
||||||
import * as core from '@actions/core'
|
import * as core from '../__fixtures__/core.js'
|
||||||
import * as fs from 'fs'
|
|
||||||
import * as path from 'path'
|
|
||||||
import { fileURLToPath } from 'url'
|
|
||||||
import { run } from '../src/main'
|
|
||||||
|
|
||||||
const __filename = fileURLToPath(import.meta.url)
|
// Create fs mocks
|
||||||
const __dirname = path.dirname(__filename)
|
const mockExistsSync = jest.fn()
|
||||||
|
const mockReadFileSync = jest.fn()
|
||||||
|
const mockWriteFileSync = jest.fn()
|
||||||
|
|
||||||
// Mock the action toolkit functions
|
// Create inference mocks
|
||||||
jest.mock('@actions/core')
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
const mockSimpleInference = jest.fn() as jest.MockedFunction<any>
|
||||||
|
const mockMcpInference = jest.fn()
|
||||||
|
|
||||||
// Mock fs to handle temporary file creation
|
// Create MCP mocks
|
||||||
jest.mock('fs')
|
const mockConnectToGitHubMCP = jest.fn()
|
||||||
|
|
||||||
|
// Mock fs module
|
||||||
|
jest.unstable_mockModule('fs', () => ({
|
||||||
|
existsSync: mockExistsSync,
|
||||||
|
readFileSync: mockReadFileSync,
|
||||||
|
writeFileSync: mockWriteFileSync
|
||||||
|
}))
|
||||||
|
|
||||||
// Mock the inference functions
|
// Mock the inference functions
|
||||||
jest.mock('../src/inference', () => ({
|
jest.unstable_mockModule('../src/inference.js', () => ({
|
||||||
simpleInference: jest.fn(),
|
simpleInference: mockSimpleInference,
|
||||||
mcpInference: jest.fn()
|
mcpInference: mockMcpInference
|
||||||
}))
|
}))
|
||||||
|
|
||||||
// Mock the MCP connection
|
// Mock the MCP connection
|
||||||
jest.mock('../src/mcp', () => ({
|
jest.unstable_mockModule('../src/mcp.js', () => ({
|
||||||
connectToGitHubMCP: jest.fn()
|
connectToGitHubMCP: mockConnectToGitHubMCP
|
||||||
}))
|
}))
|
||||||
|
|
||||||
import { simpleInference } from '../src/inference'
|
jest.unstable_mockModule('@actions/core', () => core)
|
||||||
|
|
||||||
|
// The module being tested should be imported dynamically. This ensures that the
|
||||||
|
// mocks are used in place of any actual dependencies.
|
||||||
|
const { run } = await import('../src/main.js')
|
||||||
|
|
||||||
describe('main.ts - prompt.yml integration', () => {
|
describe('main.ts - prompt.yml integration', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@@ -35,8 +46,7 @@ describe('main.ts - prompt.yml integration', () => {
|
|||||||
process.env['GITHUB_TOKEN'] = 'test-token'
|
process.env['GITHUB_TOKEN'] = 'test-token'
|
||||||
|
|
||||||
// Mock core.getInput to return appropriate values
|
// Mock core.getInput to return appropriate values
|
||||||
const mockGetInput = core.getInput as jest.Mock
|
core.getInput.mockImplementation((name: string) => {
|
||||||
mockGetInput.mockImplementation((name: string) => {
|
|
||||||
switch (name) {
|
switch (name) {
|
||||||
case 'model':
|
case 'model':
|
||||||
return 'openai/gpt-4o'
|
return 'openai/gpt-4o'
|
||||||
@@ -55,12 +65,7 @@ describe('main.ts - prompt.yml integration', () => {
|
|||||||
const mockGetBooleanInput = core.getBooleanInput as jest.Mock
|
const mockGetBooleanInput = core.getBooleanInput as jest.Mock
|
||||||
mockGetBooleanInput.mockReturnValue(false)
|
mockGetBooleanInput.mockReturnValue(false)
|
||||||
|
|
||||||
// Mock fs.existsSync
|
|
||||||
const mockExistsSync = fs.existsSync as jest.Mock
|
|
||||||
mockExistsSync.mockReturnValue(true)
|
|
||||||
|
|
||||||
// Mock fs.readFileSync for prompt file
|
// Mock fs.readFileSync for prompt file
|
||||||
const mockReadFileSync = fs.readFileSync as jest.Mock
|
|
||||||
mockReadFileSync.mockReturnValue(`
|
mockReadFileSync.mockReturnValue(`
|
||||||
messages:
|
messages:
|
||||||
- role: system
|
- role: system
|
||||||
@@ -71,17 +76,15 @@ model: openai/gpt-4o
|
|||||||
`)
|
`)
|
||||||
|
|
||||||
// Mock fs.writeFileSync
|
// Mock fs.writeFileSync
|
||||||
const mockWriteFileSync = fs.writeFileSync as jest.Mock
|
|
||||||
mockWriteFileSync.mockImplementation(() => {})
|
mockWriteFileSync.mockImplementation(() => {})
|
||||||
|
|
||||||
// Mock simpleInference
|
// Mock simpleInference
|
||||||
const mockSimpleInference = simpleInference as jest.Mock
|
|
||||||
mockSimpleInference.mockResolvedValue('Mocked AI response')
|
mockSimpleInference.mockResolvedValue('Mocked AI response')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should handle prompt YAML files with template variables', async () => {
|
it('should handle prompt YAML files with template variables', async () => {
|
||||||
const mockGetInput = core.getInput as jest.Mock
|
mockExistsSync.mockReturnValue(true)
|
||||||
mockGetInput.mockImplementation((name: string) => {
|
core.getInput.mockImplementation((name: string) => {
|
||||||
switch (name) {
|
switch (name) {
|
||||||
case 'prompt-file':
|
case 'prompt-file':
|
||||||
return 'test.prompt.yml'
|
return 'test.prompt.yml'
|
||||||
@@ -103,7 +106,6 @@ model: openai/gpt-4o
|
|||||||
await run()
|
await run()
|
||||||
|
|
||||||
// Verify simpleInference was called with the correct message structure
|
// Verify simpleInference was called with the correct message structure
|
||||||
const mockSimpleInference = simpleInference as jest.Mock
|
|
||||||
expect(mockSimpleInference).toHaveBeenCalledWith(
|
expect(mockSimpleInference).toHaveBeenCalledWith(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
messages: [
|
messages: [
|
||||||
@@ -135,8 +137,8 @@ model: openai/gpt-4o
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should fall back to legacy format when not using prompt YAML', async () => {
|
it('should fall back to legacy format when not using prompt YAML', async () => {
|
||||||
const mockGetInput = core.getInput as jest.Mock
|
mockExistsSync.mockReturnValue(false)
|
||||||
mockGetInput.mockImplementation((name: string) => {
|
core.getInput.mockImplementation((name: string) => {
|
||||||
switch (name) {
|
switch (name) {
|
||||||
case 'prompt':
|
case 'prompt':
|
||||||
return 'Hello, world!'
|
return 'Hello, world!'
|
||||||
@@ -157,12 +159,19 @@ model: openai/gpt-4o
|
|||||||
|
|
||||||
await run()
|
await run()
|
||||||
|
|
||||||
// Verify simpleInference was called with legacy format
|
// Verify simpleInference was called with converted message format
|
||||||
const mockSimpleInference = simpleInference as jest.Mock
|
|
||||||
expect(mockSimpleInference).toHaveBeenCalledWith(
|
expect(mockSimpleInference).toHaveBeenCalledWith(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
systemPrompt: 'You are helpful',
|
messages: [
|
||||||
prompt: 'Hello, world!',
|
{
|
||||||
|
role: 'system',
|
||||||
|
content: 'You are helpful'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
role: 'user',
|
||||||
|
content: 'Hello, world!'
|
||||||
|
}
|
||||||
|
],
|
||||||
modelName: 'openai/gpt-4o',
|
modelName: 'openai/gpt-4o',
|
||||||
maxTokens: 200,
|
maxTokens: 200,
|
||||||
endpoint: 'https://models.github.ai/inference',
|
endpoint: 'https://models.github.ai/inference',
|
||||||
|
|||||||
+3
-1
@@ -96,7 +96,9 @@ export function buildMessages(
|
|||||||
/**
|
/**
|
||||||
* Build response format object for API from prompt config
|
* Build response format object for API from prompt config
|
||||||
*/
|
*/
|
||||||
export function buildResponseFormat(promptConfig?: PromptConfig): any {
|
export function buildResponseFormat(
|
||||||
|
promptConfig?: PromptConfig
|
||||||
|
): { type: 'json_schema'; json_schema: unknown } | undefined {
|
||||||
if (
|
if (
|
||||||
promptConfig?.responseFormat === 'json_schema' &&
|
promptConfig?.responseFormat === 'json_schema' &&
|
||||||
promptConfig.jsonSchema
|
promptConfig.jsonSchema
|
||||||
|
|||||||
+19
-5
@@ -1,16 +1,30 @@
|
|||||||
import * as core from '@actions/core'
|
import * as core from '@actions/core'
|
||||||
import ModelClient, { isUnexpected } from '@azure-rest/ai-inference'
|
import ModelClient, { isUnexpected } from '@azure-rest/ai-inference'
|
||||||
import { AzureKeyCredential } from '@azure/core-auth'
|
import { AzureKeyCredential } from '@azure/core-auth'
|
||||||
import { GitHubMCPClient, executeToolCalls } from './mcp.js'
|
import { GitHubMCPClient, executeToolCalls, MCPTool, ToolCall } from './mcp.js'
|
||||||
import { handleUnexpectedResponse } from './helpers.js'
|
import { handleUnexpectedResponse } from './helpers.js'
|
||||||
|
|
||||||
|
interface ChatMessage {
|
||||||
|
role: string
|
||||||
|
content: string | null
|
||||||
|
tool_calls?: ToolCall[]
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ChatCompletionsRequestBody {
|
||||||
|
messages: ChatMessage[]
|
||||||
|
max_tokens: number
|
||||||
|
model: string
|
||||||
|
response_format?: { type: 'json_schema'; json_schema: unknown }
|
||||||
|
tools?: MCPTool[]
|
||||||
|
}
|
||||||
|
|
||||||
export interface InferenceRequest {
|
export interface InferenceRequest {
|
||||||
messages: Array<{ role: string; content: string }>
|
messages: Array<{ role: string; content: string }>
|
||||||
modelName: string
|
modelName: string
|
||||||
maxTokens: number
|
maxTokens: number
|
||||||
endpoint: string
|
endpoint: string
|
||||||
token: string
|
token: string
|
||||||
responseFormat?: any // Will contain the processed response format for the API
|
responseFormat?: { type: 'json_schema'; json_schema: unknown } // Processed response format for the API
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface InferenceResponse {
|
export interface InferenceResponse {
|
||||||
@@ -41,7 +55,7 @@ export async function simpleInference(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
const requestBody: any = {
|
const requestBody: ChatCompletionsRequestBody = {
|
||||||
messages: request.messages,
|
messages: request.messages,
|
||||||
max_tokens: request.maxTokens,
|
max_tokens: request.maxTokens,
|
||||||
model: request.modelName
|
model: request.modelName
|
||||||
@@ -84,7 +98,7 @@ export async function mcpInference(
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Start with the pre-processed messages
|
// Start with the pre-processed messages
|
||||||
const messages: Array<any> = [...request.messages]
|
const messages: ChatMessage[] = [...request.messages]
|
||||||
|
|
||||||
let iterationCount = 0
|
let iterationCount = 0
|
||||||
const maxIterations = 5 // Prevent infinite loops
|
const maxIterations = 5 // Prevent infinite loops
|
||||||
@@ -93,7 +107,7 @@ export async function mcpInference(
|
|||||||
iterationCount++
|
iterationCount++
|
||||||
core.info(`MCP inference iteration ${iterationCount}`)
|
core.info(`MCP inference iteration ${iterationCount}`)
|
||||||
|
|
||||||
const requestBody: any = {
|
const requestBody: ChatCompletionsRequestBody = {
|
||||||
messages: messages,
|
messages: messages,
|
||||||
max_tokens: request.maxTokens,
|
max_tokens: request.maxTokens,
|
||||||
model: request.modelName,
|
model: request.modelName,
|
||||||
|
|||||||
+3
-2
@@ -8,7 +8,8 @@ import { loadContentFromFileOrInput, buildInferenceRequest } from './helpers.js'
|
|||||||
import {
|
import {
|
||||||
loadPromptFile,
|
loadPromptFile,
|
||||||
parseTemplateVariables,
|
parseTemplateVariables,
|
||||||
isPromptYamlFile
|
isPromptYamlFile,
|
||||||
|
PromptConfig
|
||||||
} from './prompt.js'
|
} from './prompt.js'
|
||||||
|
|
||||||
const RESPONSE_FILE = 'modelResponse.txt'
|
const RESPONSE_FILE = 'modelResponse.txt'
|
||||||
@@ -23,7 +24,7 @@ export async function run(): Promise<void> {
|
|||||||
const promptFilePath = core.getInput('prompt-file')
|
const promptFilePath = core.getInput('prompt-file')
|
||||||
const inputVariables = core.getInput('input')
|
const inputVariables = core.getInput('input')
|
||||||
|
|
||||||
let promptConfig: any = undefined
|
let promptConfig: PromptConfig | undefined = undefined
|
||||||
let systemPrompt: string | undefined = undefined
|
let systemPrompt: string | undefined = undefined
|
||||||
let prompt: string | undefined = undefined
|
let prompt: string | undefined = undefined
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user