1+ // @effect -diagnostics nodeBuiltinImport:off -- Pi extensions run outside Effect; these tests exercise their native filesystem boundary.
12import * as NodeModule from "node:module" ;
23import * as NodeVM from "node:vm" ;
4+ import * as NodeFSP from "node:fs/promises" ;
5+ import * as NodePath from "node:path" ;
6+ import * as NodeOS from "node:os" ;
37import { assert , describe , it } from "@effect/vitest" ;
48
59import { PI_T3_MCP_EXTENSION_SOURCE } from "./piT3McpExtensionSource.ts" ;
@@ -9,22 +13,52 @@ type RequestHook = (
913 ctx : { model : { provider : string } } ,
1014) => Record < string , unknown > | undefined ;
1115
12- async function loadRequestHook ( ) : Promise < RequestHook > {
13- const handlers = new Map < string , RequestHook > ( ) ;
16+ interface SkillCommand {
17+ readonly name : string ;
18+ readonly source : string ;
19+ readonly sourceInfo : { readonly path : string } ;
20+ }
21+
22+ type InputHook = (
23+ event : { text : string ; images ?: ReadonlyArray < unknown > } ,
24+ ctx : { ui : { notify : ( message : string , level : string ) => void } } ,
25+ ) => Promise < { action : string ; text : string ; images ?: ReadonlyArray < unknown > } | undefined > ;
26+
27+ async function loadHooks ( commands : ReadonlyArray < SkillCommand > = [ ] ) {
28+ const handlers = new Map < string , unknown > ( ) ;
1429 // Execute the shipped extension with MCP disabled; this path needs no Typebox.
1530 const source = NodeModule . stripTypeScriptTypes (
16- PI_T3_MCP_EXTENSION_SOURCE . replace ( ' import { Type } from "typebox";' , "" ) . replace (
31+ PI_T3_MCP_EXTENSION_SOURCE . replace ( / ^ i m p o r t . * ; $ / gm , "" ) . replace (
1732 "export default async function" ,
1833 "async function" ,
1934 ) ,
2035 ) ;
2136 await NodeVM . runInNewContext ( `${ source } \nt3McpExtension(pi)` , {
2237 process : { env : { } } ,
23- pi : { on : ( name : string , handler : RequestHook ) => handlers . set ( name , handler ) } ,
38+ NodeFSP,
39+ NodePath,
40+ // Frontmatter parsing belongs to Pi. These fixtures contain only skill bodies.
41+ stripFrontmatter : ( content : string ) => content ,
42+ pi : {
43+ on : ( name : string , handler : unknown ) => handlers . set ( name , handler ) ,
44+ getCommands : ( ) => commands ,
45+ } ,
2446 } ) ;
47+ return handlers ;
48+ }
49+
50+ async function loadRequestHook ( ) : Promise < RequestHook > {
51+ const handlers = await loadHooks ( ) ;
2552 const hook = handlers . get ( "before_provider_request" ) ;
2653 assert . isDefined ( hook ) ;
27- return hook ! ;
54+ return hook as RequestHook ;
55+ }
56+
57+ async function loadInputHook ( commands : ReadonlyArray < SkillCommand > ) {
58+ const handlers = await loadHooks ( commands ) ;
59+ const hook = handlers . get ( "input" ) ;
60+ assert . isDefined ( hook ) ;
61+ return hook as InputHook ;
2862}
2963
3064describe ( "Pi upstream output-budget workaround" , ( ) => {
@@ -57,3 +91,93 @@ describe("Pi upstream output-budget workaround", () => {
5791 ) ;
5892 } ) ;
5993} ) ;
94+
95+ describe ( "Pi skill references" , ( ) => {
96+ it ( "loads every selected skill once while preserving inline prose, whitespace, and images" , async ( ) => {
97+ const directory = await NodeFSP . mkdtemp ( NodePath . join ( NodeOS . tmpdir ( ) , "pi-skill-mentions-" ) ) ;
98+ try {
99+ const alpha = NodePath . join ( directory , "alpha.md" ) ;
100+ const beta = NodePath . join ( directory , "beta.md" ) ;
101+ await NodeFSP . writeFile ( alpha , "ALPHA_INSTRUCTIONS" ) ;
102+ await NodeFSP . writeFile ( beta , "BETA_INSTRUCTIONS" ) ;
103+ const hook = await loadInputHook ( [
104+ { name : "skill:alpha" , source : "skill" , sourceInfo : { path : alpha } } ,
105+ { name : "skill:beta" , source : "skill" , sourceInfo : { path : beta } } ,
106+ ] ) ;
107+ const text =
108+ "Please use the $alpha philosophy, then $beta and $alpha\n```ts\n const x = 1;\n```" ;
109+ const images = [ { type : "image" , data : "fixture" } ] ;
110+ const result = await hook ( { text, images } , { ui : { notify : assert . fail } } ) ;
111+ assert . equal ( result ?. action , "transform" ) ;
112+ assert . isTrue ( result ?. text . startsWith ( text + "\n\n" ) ) ;
113+ assert . equal ( result ?. text . split ( "ALPHA_INSTRUCTIONS" ) . length , 2 ) ;
114+ assert . equal ( result ?. text . split ( "BETA_INSTRUCTIONS" ) . length , 2 ) ;
115+ assert . include ( result ?. text ?? "" , `References are relative to ${ directory } .` ) ;
116+ assert . strictEqual ( result ?. images , images ) ;
117+ } finally {
118+ await NodeFSP . rm ( directory , { recursive : true } ) ;
119+ }
120+ } ) ;
121+
122+ it ( "leaves unknown references and native skill commands for Pi" , async ( ) => {
123+ const hook = await loadInputHook ( [
124+ { name : "skill:alpha" , source : "skill" , sourceInfo : { path : "/unused" } } ,
125+ ] ) ;
126+ for ( const text of [ "Explain $HOME" , "/skill:alpha use $alpha" , "Hello" , "$missing" ] ) {
127+ assert . isUndefined ( await hook ( { text } , { ui : { notify : assert . fail } } ) ) ;
128+ }
129+ } ) ;
130+
131+ it ( "loads additional chips without duplicating a leading native skill" , async ( ) => {
132+ const directory = await NodeFSP . mkdtemp ( NodePath . join ( NodeOS . tmpdir ( ) , "pi-skill-mentions-" ) ) ;
133+ try {
134+ const path = NodePath . join ( directory , "beta.md" ) ;
135+ await NodeFSP . writeFile ( path , "BETA_INSTRUCTIONS" ) ;
136+ const hook = await loadInputHook ( [
137+ { name : "skill:alpha" , source : "skill" , sourceInfo : { path : "/unused" } } ,
138+ { name : "skill:beta" , source : "skill" , sourceInfo : { path } } ,
139+ ] ) ;
140+ const text = "/skill:alpha use $alpha and $beta" ;
141+ const result = await hook ( { text } , { ui : { notify : assert . fail } } ) ;
142+ assert . isTrue ( result ?. text . startsWith ( text + "\n\n" ) ) ;
143+ assert . notInclude ( result ?. text ?? "" , '<skill name="alpha"' ) ;
144+ assert . include ( result ?. text ?? "" , "BETA_INSTRUCTIONS" ) ;
145+ } finally {
146+ await NodeFSP . rm ( directory , { recursive : true } ) ;
147+ }
148+ } ) ;
149+
150+ it ( "reports an unreadable skill without deleting its reference or other selected instructions" , async ( ) => {
151+ const directory = await NodeFSP . mkdtemp ( NodePath . join ( NodeOS . tmpdir ( ) , "pi-skill-mentions-" ) ) ;
152+ try {
153+ const path = NodePath . join ( directory , "readable.md" ) ;
154+ await NodeFSP . writeFile ( path , "READABLE_INSTRUCTIONS" ) ;
155+ const hook = await loadInputHook ( [
156+ {
157+ name : "skill:missing" ,
158+ source : "skill" ,
159+ sourceInfo : { path : NodePath . join ( directory , "missing.md" ) } ,
160+ } ,
161+ { name : "skill:readable" , source : "skill" , sourceInfo : { path } } ,
162+ ] ) ;
163+ const notices : string [ ] = [ ] ;
164+ const text = "Use $missing and $readable" ;
165+ const result = await hook (
166+ { text } ,
167+ {
168+ ui : {
169+ notify : ( message ) => {
170+ notices . push ( message ) ;
171+ } ,
172+ } ,
173+ } ,
174+ ) ;
175+ assert . isTrue ( result ?. text . startsWith ( text + "\n\n" ) ) ;
176+ assert . include ( result ?. text ?? "" , "READABLE_INSTRUCTIONS" ) ;
177+ assert . equal ( notices . length , 1 ) ;
178+ assert . include ( notices [ 0 ] ?? "" , "Could not load skill missing" ) ;
179+ } finally {
180+ await NodeFSP . rm ( directory , { recursive : true } ) ;
181+ }
182+ } ) ;
183+ } ) ;
0 commit comments