@@ -2,7 +2,13 @@ import path from 'node:path';
22import { promises as fs } from 'node:fs' ;
33import { stringify as toFrontmatter } from '../frontmatter.mjs' ;
44import { writeFile , writeJson } from '../fs-utils.mjs' ;
5- import { targetOverride } from '../source.mjs' ;
5+ import { instructionsForTarget , supplementalInstructionsForTarget , targetOverride } from '../source.mjs' ;
6+
7+ function mergeInstructionRefs ( existing , extra ) {
8+ const base = Array . isArray ( existing ) ? existing : existing ? [ existing ] : [ ] ;
9+ if ( ! extra ) return base ;
10+ return [ ...new Set ( [ ...base , extra ] ) ] ;
11+ }
612
713async function readJsonIfExists ( p ) {
814 try {
@@ -34,9 +40,10 @@ export async function emitOpenCode(src, outDir, opts = {}) {
3440 written . push ( { path : p , bytes } ) ;
3541 } ;
3642
37- if ( src . instructions ) {
43+ const instructions = instructionsForTarget ( src , 'opencode' ) ;
44+ if ( instructions ) {
3845 const p = path . join ( outDir , 'AGENTS.md' ) ;
39- await push ( p , src . instructions . endsWith ( '\n' ) ? src . instructions : src . instructions + '\n' ) ;
46+ await push ( p , instructions . endsWith ( '\n' ) ? instructions : instructions + '\n' ) ;
4047 }
4148
4249 // Load iso-route's opencode.json once so we can (a) fall back to its
@@ -46,6 +53,15 @@ export async function emitOpenCode(src, outDir, opts = {}) {
4653 // below with the later merge-write on opencode.json.
4754 const opencodeJsonPath = path . join ( outDir , 'opencode.json' ) ;
4855 const existingConfig = opts . dryRun ? { } : await readJsonIfExists ( opencodeJsonPath ) ;
56+ const opencodeSupplement = supplementalInstructionsForTarget ( src , 'opencode' ) ;
57+ const opencodeSupplementPath = path . join ( outDir , '.opencode' , 'instructions.md' ) ;
58+
59+ if ( opencodeSupplement ) {
60+ await push (
61+ opencodeSupplementPath ,
62+ opencodeSupplement . endsWith ( '\n' ) ? opencodeSupplement : opencodeSupplement + '\n' ,
63+ ) ;
64+ }
4965
5066 for ( const agent of src . agents ) {
5167 const { skip, override } = targetOverride ( agent , 'opencode' ) ;
@@ -97,7 +113,8 @@ export async function emitOpenCode(src, outDir, opts = {}) {
97113 const opencodeExtras = src . config ?. targets ?. opencode ?? { } ;
98114 const hasMcp = Object . keys ( src . mcp . servers ) . length > 0 ;
99115 const hasExtras = Object . keys ( opencodeExtras ) . length > 0 ;
100- if ( hasMcp || hasExtras ) {
116+ const needsInstructionRef = Boolean ( opencodeSupplement ) ;
117+ if ( hasMcp || hasExtras || needsInstructionRef ) {
101118 // Reuse the `existingConfig` loaded at the top — re-reading could race
102119 // with intermediate per-agent file writes on slower filesystems and is
103120 // wasted I/O. `@razroo/iso-route` writes model routing fields to
@@ -122,6 +139,9 @@ export async function emitOpenCode(src, outDir, opts = {}) {
122139 for ( const [ k , v ] of Object . entries ( opencodeExtras ) ) {
123140 output [ k ] = v ;
124141 }
142+ if ( needsInstructionRef ) {
143+ output . instructions = mergeInstructionRefs ( output . instructions , '.opencode/instructions.md' ) ;
144+ }
125145 await push ( opencodeJsonPath , output , writeJson ) ;
126146 }
127147
0 commit comments