2026-10-25 17:04:21 -07:00
// Plan2Code Web Console - the few messages this page sends the desktop app that frames it, and which links it must open another way there; defines only.
2026-10-25 12:46:52 -07:00
//
// Each one is a named request with no free text in it, so the app decides what
// it does: a page can ask for "continue" to be typed, never for anything else.
/** True inside a frame: the desktop app's window, the only place that frames this page. */
export function framed () {
return Boolean ( window . parent ) && window . parent !== window ;
}
2026-10-25 17:04:21 -07:00
/**
* True for a link that leaves the console for the web: http or https on
* another origin. Inside the app those open through /open-link, since the
* app's window opens no new ones; the console's own files stay put.
*/
export function linkLeavesPage ( href , origin ) {
let url ;
try {
url = new URL ( href , origin );
} catch {
return false ;
}
return ( url . protocol === "http:" || url . protocol === "https:" ) && url . origin !== origin ;
}
2026-10-26 06:36:45 -07:00
/**
* Call `handler(data)` for each `{ plan2code: kind, ... }` message the frame's
* parent sends, and for nothing from anywhere else (another frame, the page
2026-11-03 10:22:08 -08:00
* itself). The app sends `taskbar-state`, `terminal-state` and the loop's
* answers (wireLoopMessages).
2026-10-26 06:36:45 -07:00
*/
export function onAppMessage ( kind , handler ) {
window . addEventListener ( "message" , ( event ) => {
if ( ! framed () || event . source !== window . parent ) return ;
const data = event . data ;
if ( data && typeof data === "object" && data . plan2code === kind ) handler ( data );
});
}
2026-11-02 12:00:12 -08:00
/**
* Hear the app's answers to the Implementation Loop's named requests:
* `loop-facts` (what agent a loop would use), `loop-start-result`,
2026-11-28 21:09:02 -08:00
* `loop-notify-test-result` and the three stop-screen results, then the Review Loop's
* `review-loop-facts` and `review-loop-start-result`. `store` is the page object and `redraw` its
2026-11-02 12:00:12 -08:00
* render; they are handed in so this file keeps importing nothing.
*/
export function wireLoopMessages ( store , redraw ) {
onAppMessage ( "loop-facts" , ( data ) => {
store . loopFacts = data . facts && typeof data . facts === "object" ? data . facts : null ;
redraw ();
});
onAppMessage ( "loop-start-result" , ( data ) => {
// Cancel in the app's confirm: nothing started, so the setup is as it was.
if ( data . ok === true && data . started === false ) {
store . loopStart = null ;
if ( store . loopSetup ) store . loopSetup . starting = false ;
redraw ();
return ;
}
store . loopStart = { ok : data . ok === true , error : data . ok === true ? null : String ( data . error || "The app did not start the loop." ) };
redraw ();
});
2026-11-28 21:09:02 -08:00
// The Review Loop's own answers: its facts, and the result of its Start.
onAppMessage ( "review-loop-facts" , ( data ) => {
store . reviewFacts = data . facts && typeof data . facts === "object" ? data . facts : null ;
redraw ();
});
onAppMessage ( "review-loop-start-result" , ( data ) => {
// Cancel in the app's confirm: nothing started, so the setup is as it was.
if ( data . ok === true && data . started === false ) {
store . reviewStart = null ;
if ( store . reviewLoopSetup ) store . reviewLoopSetup . starting = false ;
redraw ();
return ;
}
store . reviewStart = { ok : data . ok === true , error : data . ok === true ? null : String ( data . error || "The app did not start the Review Loop." ) };
if ( ! store . reviewStart . ok && store . reviewLoopSetup ) store . reviewLoopSetup . starting = false ;
redraw ();
});
// The end screen's presses (Continue, Keep or Discard, Sign off, the finished
// screen's next step): the answer clears the pressed state and keeps any error.
for ( const kind of [ "review-loop-continue-result" , "review-loop-partial-result" , "review-loop-sign-off-result" , "review-loop-next-result" ]) {
onAppMessage ( kind , ( data ) => {
store . reviewAction = { kind , ok : data . ok === true , error : data . ok === true ? null : String ( data . error || "The app could not do that." ) };
redraw ();
});
}
2026-11-02 12:00:12 -08:00
onAppMessage ( "loop-notify-test-result" , ( data ) => {
store . loopNotifyTest = { waiting : false , ok : data . ok === true , sent : data . sent === true , error : data . ok === true ? null : String ( data . error || "The app could not send the test." ) };
redraw ();
});
2026-11-04 20:13:43 -08:00
for ( const kind of [ "loop-resume-result" , "loop-ask-result" , "loop-fix-result" , "loop-finalize-result" , "loop-dashboard-result" ]) {
2026-11-02 12:00:12 -08:00
onAppMessage ( kind , ( data ) => {
store . loopAction = { kind , ok : data . ok === true , error : data . ok === true ? null : String ( data . error || "The app could not do that." ) };
redraw ();
});
}
}
2026-10-26 17:05:36 -07:00
/**
* Ask the app for what its bar's New Session button does, confirm and all.
* `command`, when there is one, goes on the clipboard first so it can be
* pasted into the new terminal; a refused copy loses nothing, the page still
* shows it.
*/
export async function startNewSession ( command = "" ) {
if ( command ) {
try {
await navigator . clipboard . writeText ( command );
} catch {
/* the command stays on the page to copy by hand */
}
}
tellApp ( "new-session" );
}
2026-11-03 04:08:27 -08:00
/**
* Tell the app an agent was just found on the setup page: it returns to the
* screen it came from, rechecks, and picks that agent on the launch card.
*/
export function agentFound ( id ) {
tellApp ( "agent-found" , { id });
}
2026-11-17 18:08:01 -08:00
// The keys the desktop app's shell answers to (plan2code-app/ui/meter.js SHORTCUTS, held to the same
// table by scripts/test-app-meter.mjs): Cmd on a Mac, Ctrl elsewhere. A key pressed here, inside the
// frame, never reaches the shell's own listener, so the page forwards the id and nothing else.
export const SHORTCUT_KEYS = Object . freeze ([
{ id : "toggle-terminal" , key : "`" , shift : false },
{ id : "focus-console" , key : "1" , shift : false },
{ id : "focus-terminal" , key : "2" , shift : false },
{ id : "new-session" , key : "n" , shift : true },
{ id : "change-project" , key : "o" , shift : false },
{ id : "preferences" , key : "," , shift : false },
]);
/** The shortcut id a key press stands for, or null. `mac` picks Cmd over Ctrl. */
export function shortcutIdFor ( e , mac ) {
if ( e . altKey ) return null ;
const chord = mac ? e . metaKey && ! e . ctrlKey : e . ctrlKey && ! e . metaKey ;
if ( ! chord ) return null ;
const key = String ( e . key || "" ). toLowerCase ();
const hit = SHORTCUT_KEYS . find (( s ) => s . key === key && s . shift === Boolean ( e . shiftKey ));
return hit ? hit . id : null ;
}
2026-10-25 12:46:52 -07:00
/** Post `message` to the frame's parent, as `{ plan2code: kind, ...extra }`; nothing outside a frame. */
export function tellApp ( kind , extra = {}) {
if ( ! framed ()) return ;
try {
window . parent . postMessage ({ ... extra , plan2code : kind }, "*" );
} catch {
/* no parent to tell */
}
}