-
Notifications
You must be signed in to change notification settings - Fork 40
feat(ai): PR 4 — agent config + validation [4/7] #410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ianwhitedeveloper
merged 2 commits into
ai-testing-framework-implementation-consolidation
from
pr/ai-config-validation
Feb 23, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import { readFile } from 'fs/promises'; | ||
| import { z } from 'zod'; | ||
| import { createError } from 'error-causes'; | ||
| import { ValidationError } from './ai-errors.js'; | ||
| import { parseOpenCodeNDJSON } from './agent-parser.js'; | ||
|
|
||
| /** | ||
| * Format Zod validation errors into a human-readable message. | ||
| * @param {any} zodError - Zod validation error | ||
| * @returns {string} Formatted error message | ||
| */ | ||
| export const formatZodError = (zodError) => { | ||
| const issues = zodError.issues || zodError.errors; | ||
| return issues | ||
| ? issues.map(e => `${e.path.join('.')}: ${e.message}`).join('; ') | ||
| : zodError.message || 'Validation failed'; | ||
| }; | ||
|
|
||
| /** | ||
| * Get agent configuration based on agent name. | ||
| * Supports 'claude', 'opencode', and 'cursor' agents. | ||
| * All agents use their standard OAuth authentication flows. | ||
| * @param {string} agentName - Name of the agent ('claude', 'opencode', 'cursor') | ||
| * @returns {Object} Agent configuration with command and args | ||
| */ | ||
| export const getAgentConfig = (agentName = 'claude') => { | ||
| const agentConfigs = { | ||
| claude: { | ||
| command: 'claude', | ||
| args: ['-p', '--output-format', 'json', '--no-session-persistence'] | ||
| }, | ||
| opencode: { | ||
| command: 'opencode', | ||
| args: ['run', '--format', 'json'], | ||
| parseOutput: (stdout, logger) => parseOpenCodeNDJSON(stdout, logger) | ||
| }, | ||
| cursor: { | ||
| command: 'agent', | ||
| args: ['--print', '--output-format', 'json', '--trust'] | ||
| } | ||
| }; | ||
|
|
||
| const config = agentConfigs[agentName.toLowerCase()]; | ||
| if (!config) { | ||
| throw createError({ | ||
| ...ValidationError, | ||
| message: `Unknown agent: ${agentName}. Supported agents: ${Object.keys(agentConfigs).join(', ')}` | ||
| }); | ||
| } | ||
|
|
||
| return config; | ||
| }; | ||
|
|
||
| // YAGNI: only command + args — parseOutput is a runtime function, not a serializable config field | ||
| const agentConfigFileSchema = z.object({ | ||
| command: z.string().min(1, { error: 'command is required' }), | ||
| args: z.array(z.string()).default([]) | ||
| }); | ||
|
|
||
| const readAgentConfigFile = async ({ configPath }) => { | ||
| try { | ||
| return await readFile(configPath, 'utf-8'); | ||
| } catch (err) { | ||
| throw createError({ | ||
| ...ValidationError, | ||
| message: `Failed to read agent config file: ${configPath}`, | ||
| code: 'AGENT_CONFIG_READ_ERROR', | ||
| cause: err | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| const parseJson = ({ configPath, raw }) => { | ||
| try { | ||
| return JSON.parse(raw); | ||
| } catch (err) { | ||
| throw createError({ | ||
| ...ValidationError, | ||
| message: `Agent config file is not valid JSON: ${configPath}`, | ||
| code: 'AGENT_CONFIG_PARSE_ERROR', | ||
| cause: err | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| const validateAgentConfig = (parsed) => { | ||
| try { | ||
| return agentConfigFileSchema.parse(parsed); | ||
| } catch (zodError) { | ||
| throw createError({ | ||
| ...ValidationError, | ||
| message: `Invalid agent config: ${formatZodError(zodError)}`, | ||
| code: 'AGENT_CONFIG_VALIDATION_ERROR', | ||
| cause: zodError | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Load and validate an agent configuration from a JSON file. | ||
| * @param {string} configPath - Path to the JSON config file | ||
| * @returns {Promise<Object>} Validated agent config with command and args | ||
| */ | ||
| export const loadAgentConfig = async (configPath) => { | ||
| const raw = await readAgentConfigFile({ configPath }); | ||
| const parsed = parseJson({ configPath, raw }); | ||
| return validateAgentConfig(parsed); | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: These should use custom error types so handlers can automatically route them individually using a
handleErrorspattern match. This is probably fine as-is, but we should write an error-causes skill so agents know how to properly use this library.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reference - partially addressed in #415