Line data Source code
1 : export type AgentApp = 'claude' | 'codex'
2 :
3 1 : const AGENT_APP_LINKS: Record<AgentApp, (encodedPrompt: string) => string> = {
4 : claude: (encodedPrompt) => `claude://code/new?q=${encodedPrompt}`,
5 : codex: (encodedPrompt) => `codex://new?prompt=${encodedPrompt}`
6 : }
7 :
8 1 : const SURROGATES = /([\uD800-\uDBFF][\uDC00-\uDFFF])|[\uD800-\uDFFF]/g
9 :
10 0 : function wellFormed(text: string): string {
11 0 : return text.replace(SURROGATES, (_, pair?: string) => pair ?? '\uFFFD')
12 : }
13 :
14 0 : function agentAppUrl(app: AgentApp, prompt: string): string {
15 0 : return AGENT_APP_LINKS[app](encodeURIComponent(wellFormed(prompt)))
16 : }
17 :
18 1 : const LAUNCHER_FRAME_ID = 'agent-app-launcher'
19 :
20 0 : function hiddenLauncherFrame(): HTMLIFrameElement {
21 0 : const existing = document.getElementById(LAUNCHER_FRAME_ID)
22 0 : if (existing instanceof HTMLIFrameElement) return existing
23 0 : const frame = document.createElement('iframe')
24 0 : frame.id = LAUNCHER_FRAME_ID
25 0 : frame.hidden = true
26 0 : return document.body.appendChild(frame)
27 : }
28 :
29 : /** Starts a new session in the desktop app with the prompt in its composer, unsent. */
30 0 : export function openAgentApp(app: AgentApp, prompt: string): void {
31 0 : hiddenLauncherFrame().src = agentAppUrl(app, prompt)
32 : }
|