-
Notifications
You must be signed in to change notification settings - Fork 17
feat: add fragments support for custom agent instructions #4
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
404dc25
feat: add fragments support for custom agent instructions
vtemian 7643814
fix: inject fragments at agent source, not just config hook
vtemian 21cf763
docs: add fragments configuration to README
vtemian 9758f1b
fix: remove duplicate fragment injection in config hook
vtemian 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
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 |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| export type { AgentOverride, CustomConfig, OcttoConfig } from "./loader"; | ||
| export type { AgentOverride, CustomConfig, Fragments, OcttoConfig } from "./loader"; | ||
| export { loadCustomConfig, resolvePort } from "./loader"; | ||
| export { AgentOverrideSchema, OcttoConfigSchema, PortSchema } from "./schema"; | ||
| export { AgentOverrideSchema, FragmentsSchema, OcttoConfigSchema, PortSchema } from "./schema"; |
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
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
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,171 @@ | ||
| // src/hooks/fragment-injector.ts | ||
| import { readFile } from "node:fs/promises"; | ||
| import { join } from "node:path"; | ||
|
|
||
| import * as v from "valibot"; | ||
|
|
||
| import { AGENTS } from "@/agents"; | ||
|
|
||
| type FragmentsRecord = Record<string, string[]> | undefined; | ||
|
|
||
| const VALID_AGENT_NAMES = Object.values(AGENTS); | ||
|
|
||
| const ProjectFragmentsSchema = v.record(v.string(), v.array(v.string())); | ||
|
|
||
| /** | ||
| * Format fragments array as an XML block to prepend to agent prompts. | ||
| */ | ||
| export function formatFragmentsBlock(fragments: string[] | undefined): string { | ||
| if (!fragments || fragments.length === 0) { | ||
| return ""; | ||
| } | ||
|
|
||
| const bulletPoints = fragments.map((f) => `- ${f}`).join("\n"); | ||
| return `<user-instructions>\n${bulletPoints}\n</user-instructions>\n\n`; | ||
| } | ||
|
|
||
| /** | ||
| * Merge global and project fragments. | ||
| * Global fragments come first, project fragments append. | ||
| */ | ||
| export function mergeFragments(global: FragmentsRecord, project: FragmentsRecord): Record<string, string[]> { | ||
| const result: Record<string, string[]> = {}; | ||
|
|
||
| if (global) { | ||
| for (const [agent, frags] of Object.entries(global)) { | ||
| result[agent] = [...frags]; | ||
| } | ||
| } | ||
|
|
||
| if (project) { | ||
| for (const [agent, frags] of Object.entries(project)) { | ||
| if (result[agent]) { | ||
| result[agent].push(...frags); | ||
| } else { | ||
| result[agent] = [...frags]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Load project-level fragments from .octto/fragments.json | ||
| */ | ||
| export async function loadProjectFragments(projectDir: string): Promise<Record<string, string[]> | undefined> { | ||
| const fragmentsPath = join(projectDir, ".octto", "fragments.json"); | ||
|
|
||
| try { | ||
| const content = await readFile(fragmentsPath, "utf-8"); | ||
| const parsed = JSON.parse(content); | ||
|
|
||
| const result = v.safeParse(ProjectFragmentsSchema, parsed); | ||
| if (!result.success) { | ||
| console.warn(`[octto] Invalid fragments.json schema in ${fragmentsPath}`); | ||
| return undefined; | ||
| } | ||
|
|
||
| return result.output; | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Calculate Levenshtein distance between two strings. | ||
| * Used for suggesting similar agent names for typos. | ||
| */ | ||
| export function levenshteinDistance(a: string, b: string): number { | ||
| if (a.length === 0) return b.length; | ||
| if (b.length === 0) return a.length; | ||
|
|
||
| const matrix: number[][] = []; | ||
|
|
||
| for (let i = 0; i <= b.length; i++) { | ||
| matrix[i] = [i]; | ||
| } | ||
|
|
||
| for (let j = 0; j <= a.length; j++) { | ||
| matrix[0][j] = j; | ||
| } | ||
|
|
||
| for (let i = 1; i <= b.length; i++) { | ||
| for (let j = 1; j <= a.length; j++) { | ||
| if (b.charAt(i - 1) === a.charAt(j - 1)) { | ||
| matrix[i][j] = matrix[i - 1][j - 1]; | ||
| } else { | ||
| matrix[i][j] = Math.min( | ||
| matrix[i - 1][j - 1] + 1, // substitution | ||
| matrix[i][j - 1] + 1, // insertion | ||
| matrix[i - 1][j] + 1, // deletion | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return matrix[b.length][a.length]; | ||
| } | ||
|
|
||
| /** | ||
| * Warn about unknown agent names in fragments config. | ||
| * Suggests similar valid agent names for likely typos. | ||
| */ | ||
| export function warnUnknownAgents(fragments: Record<string, string[]> | undefined): void { | ||
| if (!fragments) return; | ||
|
|
||
| for (const agentName of Object.keys(fragments)) { | ||
| if (VALID_AGENT_NAMES.includes(agentName as AGENTS)) { | ||
| continue; | ||
| } | ||
|
|
||
| // Find closest valid agent name | ||
| let closest: string | undefined; | ||
| let minDistance = Infinity; | ||
|
|
||
| for (const validName of VALID_AGENT_NAMES) { | ||
| const distance = levenshteinDistance(agentName, validName); | ||
| if (distance < minDistance && distance <= 3) { | ||
| minDistance = distance; | ||
| closest = validName; | ||
| } | ||
| } | ||
|
|
||
| let message = `[octto] Unknown agent "${agentName}" in fragments config.`; | ||
| if (closest) { | ||
| message += ` Did you mean "${closest}"?`; | ||
| } | ||
| message += ` Valid agents: ${VALID_AGENT_NAMES.join(", ")}`; | ||
|
|
||
| console.warn(message); | ||
| } | ||
| } | ||
|
|
||
| export interface FragmentInjectorContext { | ||
| projectDir: string; | ||
| } | ||
|
|
||
| /** | ||
| * Create a fragment injector that can modify agent system prompts. | ||
| * Returns merged fragments from global config and project config. | ||
| */ | ||
| export async function createFragmentInjector( | ||
| ctx: FragmentInjectorContext, | ||
| globalFragments: FragmentsRecord, | ||
| ): Promise<Record<string, string[]>> { | ||
| const projectFragments = await loadProjectFragments(ctx.projectDir); | ||
| const merged = mergeFragments(globalFragments, projectFragments); | ||
|
|
||
| // Warn about unknown agents in both global and project fragments | ||
| warnUnknownAgents(globalFragments); | ||
| warnUnknownAgents(projectFragments); | ||
|
|
||
| return merged; | ||
| } | ||
|
|
||
| /** | ||
| * Get the system prompt prefix for a specific agent. | ||
| */ | ||
| export function getAgentSystemPromptPrefix(fragments: Record<string, string[]>, agentName: string): string { | ||
| return formatFragmentsBlock(fragments[agentName]); | ||
| } |
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,11 @@ | ||
| // src/hooks/index.ts | ||
| export { | ||
| createFragmentInjector, | ||
| type FragmentInjectorContext, | ||
| formatFragmentsBlock, | ||
| getAgentSystemPromptPrefix, | ||
| levenshteinDistance, | ||
| loadProjectFragments, | ||
| mergeFragments, | ||
| warnUnknownAgents, | ||
| } from "./fragment-injector"; |
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
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.
Uh oh!
There was an error while loading. Please reload this page.