22// A stand-in `claude` CLI for staged launch videos: T3's real Claude provider runs it, and it
33// plays the scripted turns in FAKE_CLAUDE_SCRIPT over the Agent SDK's stream-json protocol.
44// Script format: see the launch-video SKILL.md. FAKE_CLAUDE_LOG records every line.
5+ import * as NodeChildProcess from "node:child_process" ;
56import * as NodeCrypto from "node:crypto" ;
67import * as NodeFS from "node:fs" ;
8+ import * as NodePath from "node:path" ;
79import * as NodeReadline from "node:readline" ;
810
911const VERSION = "2.1.329" ;
@@ -28,10 +30,22 @@ const readJson = (file, fallback) => {
2830 return fallback ;
2931 }
3032} ;
31- const script = readJson ( process . env . FAKE_CLAUDE_SCRIPT ?? "" , [ ] ) ;
33+ // An array of turns, or { turns, servers } where servers are MCP servers that need a sign-in:
34+ // [{ name, authUrl }]. Sign-in follows the SDK's mcp_authenticate / mcp_oauth_callback_url.
35+ const loaded = readJson ( process . env . FAKE_CLAUDE_SCRIPT ?? "" , [ ] ) ;
36+ const script = Array . isArray ( loaded ) ? loaded : ( loaded . turns ?? [ ] ) ;
37+ // Sign-ins outlive the process, like the real CLI's stored credentials.
38+ const signedInFile = `${ process . env . FAKE_CLAUDE_SCRIPT } .signed-in.json` ;
39+ const signedIn = new Set ( readJson ( signedInFile , [ ] ) ) ;
40+ const signInServers = new Map (
41+ ( Array . isArray ( loaded ) ? [ ] : ( loaded . servers ?? [ ] ) ) . map ( ( server ) => [
42+ server . name ,
43+ { ...server , status : signedIn . has ( server . name ) ? "connected" : "needs-auth" } ,
44+ ] ) ,
45+ ) ;
3246const played = new Set ( ) ;
33- const fill = ( value ) => {
34- const vars = readJson ( process . env . FAKE_CLAUDE_VARS ?? "" , { } ) ;
47+ const fill = ( value , local = { } ) => {
48+ const vars = { ... readJson ( process . env . FAKE_CLAUDE_VARS ?? "" , { } ) , ... local } ;
3549 const walk = ( v ) =>
3650 typeof v === "string"
3751 ? v . replace ( / < v a r : ( [ \w - ] + ) > / g, ( m , name ) => vars [ name ] ?? m )
@@ -47,6 +61,33 @@ const argValue = (name) => {
4761 const i = process . argv . indexOf ( name ) ;
4862 return i === - 1 ? undefined : process . argv [ i + 1 ] ;
4963} ;
64+
65+ // `-p --json-schema` is T3's text generation (titles, commit messages, branch names). It answers
66+ // with the first `{ match, json }` turn whose regex matches the schema plus the prompt.
67+ if ( process . argv . includes ( "-p" ) ) {
68+ const schema = argValue ( "--json-schema" ) ?? "" ;
69+ let prompt = "" ;
70+ for await ( const chunk of process . stdin ) prompt += chunk ;
71+ log ( "print" , { schema, prompt : prompt . slice ( 0 , 2000 ) } ) ;
72+ const turn = script . find (
73+ ( t ) => t . json && new RegExp ( t . match ?? "" , "i" ) . test ( `${ schema } \n${ prompt } ` ) ,
74+ ) ;
75+ if ( ! turn ) {
76+ console . error ( `fake-claude: no json turn matches ${ schema } ` ) ;
77+ process . exit ( 1 ) ;
78+ }
79+ console . log (
80+ JSON . stringify ( {
81+ type : "result" ,
82+ subtype : "success" ,
83+ is_error : false ,
84+ result : "" ,
85+ structured_output : fill ( turn . json ) ,
86+ session_id : NodeCrypto . randomUUID ( ) ,
87+ } ) ,
88+ ) ;
89+ process . exit ( 0 ) ;
90+ }
5091const sessionId = argValue ( "--session-id" ) ?? argValue ( "--resume" ) ?? NodeCrypto . randomUUID ( ) ;
5192let model = argValue ( "--model" ) ?? "claude-opus-5-5" ;
5293const cwd = process . cwd ( ) ;
@@ -82,12 +123,12 @@ const assistant = (content, id = `msg_${uuid().replaceAll("-", "").slice(0, 24)}
82123 session_id : sessionId ,
83124 uuid : uuid ( ) ,
84125 } ) ;
85- const toolResult = ( toolUseId , content , extra = { } ) =>
126+ const toolResult = ( toolUseId , content , extra = { } , isError = false ) =>
86127 send ( {
87128 type : "user" ,
88129 message : {
89130 role : "user" ,
90- content : [ { tool_use_id : toolUseId , type : "tool_result" , content, is_error : false } ] ,
131+ content : [ { tool_use_id : toolUseId , type : "tool_result" , content, is_error : isError } ] ,
91132 } ,
92133 parent_tool_use_id : null ,
93134 session_id : sessionId ,
@@ -164,6 +205,11 @@ const streamText = async (text, type = "text") => {
164205 } ) ;
165206} ;
166207
208+ const serverStatuses = ( ) => [
209+ ...Object . keys ( mcpServers ) . map ( ( name ) => ( { name, status : "connected" , scope : "dynamic" } ) ) ,
210+ ...[ ...signInServers . values ( ) ] . map ( ( { name, status } ) => ( { name, status, scope : "user" } ) ) ,
211+ ] ;
212+
167213// One MCP tools/call over streamable HTTP against the server T3 registered.
168214async function callTool ( name , args ) {
169215 const [ serverName , server ] = Object . entries ( mcpServers ) . find ( ( [ , s ] ) => s . url ) ?? [ ] ;
@@ -205,9 +251,11 @@ async function callTool(name, args) {
205251
206252async function playTurn ( prompt ) {
207253 let index = script . findIndex (
208- ( turn , i ) => ! played . has ( i ) && turn . match && new RegExp ( turn . match , "i" ) . test ( prompt ) ,
254+ ( turn , i ) =>
255+ ! played . has ( i ) && ! turn . json && turn . match && new RegExp ( turn . match , "i" ) . test ( prompt ) ,
209256 ) ;
210- if ( index === - 1 ) index = script . findIndex ( ( turn , i ) => ! played . has ( i ) && ! turn . match ) ;
257+ if ( index === - 1 )
258+ index = script . findIndex ( ( turn , i ) => ! played . has ( i ) && ! turn . json && ! turn . match ) ;
211259 const turn = index === - 1 ? { steps : [ { text : "Done." } ] } : script [ index ] ;
212260 if ( index !== - 1 && ! turn . repeat ) played . add ( index ) ;
213261 send ( {
@@ -217,7 +265,7 @@ async function playTurn(prompt) {
217265 claude_code_version : VERSION ,
218266 cwd,
219267 tools : [ "Bash" , "Read" , "Edit" ] ,
220- mcp_servers : Object . keys ( mcpServers ) . map ( ( name ) => ( { name, status : "connected" } ) ) ,
268+ mcp_servers : serverStatuses ( ) . map ( ( { name, status } ) => ( { name, status } ) ) ,
221269 model,
222270 permissionMode : "bypassPermissions" ,
223271 slash_commands : [ ] ,
@@ -229,44 +277,121 @@ async function playTurn(prompt) {
229277 session_id : sessionId ,
230278 } ) ;
231279 let last = "" ;
232- for ( const step of fill ( turn . steps ) ) {
233- if ( step . wait ) await sleep ( step . wait ) ;
234- if ( step . think ) await streamText ( step . think , "thinking" ) ;
235- if ( step . text ) {
236- await streamText ( step . text ) ;
237- last = step . text ;
238- }
239- if ( step . bash ) {
240- const id = `toolu_${ uuid ( ) . replaceAll ( "-" , "" ) . slice ( 0 , 24 ) } ` ;
241- assistant ( [
242- {
280+ // `as` keeps a step's real output for later steps of the turn, as <var:name>.
281+ const local = { } ;
282+ // The tool call awaiting its result, so a failing step can still answer it.
283+ let pending ;
284+ const toolUse = ( block ) => {
285+ pending = block . id ;
286+ assistant ( [ block ] ) ;
287+ } ;
288+ const answer = ( ...args ) => {
289+ toolResult ( ...args ) ;
290+ pending = undefined ;
291+ } ;
292+ try {
293+ for ( const raw of turn . steps ) {
294+ const filled = fill ( raw , local ) ;
295+ // `submit` hands a workflow agent step its typed result through T3's own MCP.
296+ const step = filled . submit
297+ ? { ...filled , tool : "t3_submit_output" , input : { json : JSON . stringify ( filled . submit ) } }
298+ : filled ;
299+ if ( step . wait ) await sleep ( step . wait ) ;
300+ if ( step . think ) await streamText ( step . think , "thinking" ) ;
301+ if ( step . text ) {
302+ await streamText ( step . text ) ;
303+ last = step . text ;
304+ }
305+ if ( step . bash ) {
306+ const id = `toolu_${ uuid ( ) . replaceAll ( "-" , "" ) . slice ( 0 , 24 ) } ` ;
307+ toolUse ( {
243308 type : "tool_use" ,
244309 id,
245310 name : "Bash" ,
246311 input : { command : step . bash , description : step . description ?? step . bash } ,
247- } ,
248- ] ) ;
249- await sleep ( step . ms ?? 1200 ) ;
250- toolResult ( id , step . output ?? "" , {
251- tool_use_result : {
252- stdout : step . output ?? "" ,
253- stderr : "" ,
254- interrupted : false ,
255- isImage : false ,
256- } ,
257- } ) ;
258- }
259- if ( step . tool ) {
260- const id = `toolu_${ uuid ( ) . replaceAll ( "-" , "" ) . slice ( 0 , 24 ) } ` ;
261- const serverName = Object . keys ( mcpServers ) [ 0 ] ?? "t3-code" ;
262- assistant ( [
263- { type : "tool_use" , id, name : `mcp__${ serverName } __${ step . tool } ` , input : step . input ?? { } } ,
264- ] ) ;
265- const { text } = await callTool ( step . tool , step . input ?? { } ) . catch ( ( error ) => ( {
266- text : String ( error ) ,
267- } ) ) ;
268- toolResult ( id , [ { type : "text" , text } ] ) ;
312+ } ) ;
313+ // `run` executes the command for real in the session's cwd, e.g. to push and open a PR.
314+ let output = step . output ?? "" ;
315+ if ( step . run ) {
316+ const started = Date . now ( ) ;
317+ output = NodeChildProcess . execSync ( step . bash , {
318+ cwd,
319+ encoding : "utf8" ,
320+ stdio : [ "ignore" , "pipe" , "pipe" ] ,
321+ } ) . trim ( ) ;
322+ await sleep ( Math . max ( 0 , ( step . ms ?? 1200 ) - ( Date . now ( ) - started ) ) ) ;
323+ } else await sleep ( step . ms ?? 1200 ) ;
324+ if ( step . as )
325+ local [ step . as ] = step . pick ? ( new RegExp ( step . pick ) . exec ( output ) ?. [ 0 ] ?? output ) : output ;
326+ answer ( id , output , {
327+ tool_use_result : {
328+ stdout : output ,
329+ stderr : "" ,
330+ interrupted : false ,
331+ isImage : false ,
332+ } ,
333+ } ) ;
334+ }
335+ if ( step . tool ) {
336+ const id = `toolu_${ uuid ( ) . replaceAll ( "-" , "" ) . slice ( 0 , 24 ) } ` ;
337+ // `server` names a scripted server whose answer is `output`; otherwise T3's own MCP runs it.
338+ const serverName = step . server ?? Object . keys ( mcpServers ) [ 0 ] ?? "t3-code" ;
339+ toolUse ( {
340+ type : "tool_use" ,
341+ id,
342+ name : `mcp__${ serverName } __${ step . tool } ` ,
343+ input : step . input ?? { } ,
344+ } ) ;
345+ if ( step . server ) await sleep ( step . ms ?? 900 ) ;
346+ const { text, isError = false } = step . server
347+ ? { text : step . output ?? "" }
348+ : await callTool ( step . tool , step . input ?? { } ) . catch ( ( error ) => ( {
349+ text : String ( error ) ,
350+ isError : true ,
351+ } ) ) ;
352+ answer ( id , [ { type : "text" , text } ] , { } , isError ) ;
353+ }
354+ // Real edits, so checkpoints and diffs show them. Paths are relative to the session's cwd.
355+ if ( step . edit || step . write ) {
356+ const id = `toolu_${ uuid ( ) . replaceAll ( "-" , "" ) . slice ( 0 , 24 ) } ` ;
357+ const file = NodePath . resolve ( cwd , step . edit ?? step . write ) ;
358+ const input = step . edit
359+ ? { file_path : file , old_string : step . old , new_string : step . new }
360+ : { file_path : file , content : step . content } ;
361+ toolUse ( { type : "tool_use" , id, name : step . edit ? "Edit" : "Write" , input } ) ;
362+ await sleep ( step . ms ?? 700 ) ;
363+ if ( step . edit ) {
364+ const before = NodeFS . readFileSync ( file , "utf8" ) ;
365+ if ( ! before . includes ( step . old ) ) throw new Error ( `${ step . edit } : old text not found` ) ;
366+ NodeFS . writeFileSync ( file , before . replace ( step . old , step . new ) ) ;
367+ } else {
368+ NodeFS . mkdirSync ( NodePath . dirname ( file ) , { recursive : true } ) ;
369+ NodeFS . writeFileSync ( file , step . content ) ;
370+ }
371+ answer ( id , `The file ${ file } has been updated successfully.` ) ;
372+ }
269373 }
374+ } catch ( error ) {
375+ // A scripted step that fails (an edit whose text is gone, a command that exits non-zero)
376+ // still ends the turn, as a failed one, so the app does not wait forever.
377+ log ( "step failed" , String ( error ) ) ;
378+ if ( pending ) answer ( pending , String ( error . message ?? error ) , { } , true ) ;
379+ send ( {
380+ type : "result" ,
381+ subtype : "error_during_execution" ,
382+ is_error : true ,
383+ duration_ms : 4000 ,
384+ duration_api_ms : 3500 ,
385+ num_turns : 1 ,
386+ result : String ( error . message ?? error ) ,
387+ session_id : sessionId ,
388+ total_cost_usd : 0 ,
389+ usage,
390+ modelUsage : { } ,
391+ permission_denials : [ ] ,
392+ uuid : uuid ( ) ,
393+ } ) ;
394+ return ;
270395 }
271396 send ( {
272397 type : "result" ,
@@ -304,6 +429,29 @@ const controlResponse = (request) => {
304429 return { } ;
305430 case "get_usage" :
306431 return { rate_limits_available : false , rate_limits : [ ] } ;
432+ case "mcp_status" :
433+ return { mcpServers : serverStatuses ( ) } ;
434+ case "mcp_authenticate" : {
435+ const server = signInServers . get ( request . serverName ) ;
436+ if ( ! server ) throw new Error ( `no MCP server ${ request . serverName } ` ) ;
437+ if ( server . status === "connected" ) return { requiresUserAction : false } ;
438+ server . state = uuid ( ) ;
439+ const url = new URL ( server . authUrl ) ;
440+ url . searchParams . set ( "state" , server . state ) ;
441+ return { authUrl : url . toString ( ) , requiresUserAction : true } ;
442+ }
443+ case "mcp_oauth_callback_url" : {
444+ const server = signInServers . get ( request . serverName ) ;
445+ const state = new URL ( request . callbackUrl ) . searchParams . get ( "state" ) ;
446+ if ( ! server || ! server . state || state !== server . state )
447+ throw new Error ( "callback is not from this sign-in" ) ;
448+ server . status = "connected" ;
449+ signedIn . add ( server . name ) ;
450+ NodeFS . writeFileSync ( signedInFile , JSON . stringify ( [ ...signedIn ] ) ) ;
451+ return { } ;
452+ }
453+ case "mcp_reconnect" :
454+ return { } ;
307455 default :
308456 return { } ;
309457 }
@@ -321,14 +469,17 @@ input.on("line", (line) => {
321469 }
322470 log ( "in" , message ) ;
323471 if ( message . type === "control_request" ) {
324- send ( {
325- type : "control_response" ,
326- response : {
472+ let response ;
473+ try {
474+ response = {
327475 subtype : "success" ,
328476 request_id : message . request_id ,
329477 response : controlResponse ( message . request ?? { } ) ,
330- } ,
331- } ) ;
478+ } ;
479+ } catch ( error ) {
480+ response = { subtype : "error" , request_id : message . request_id , error : String ( error ) } ;
481+ }
482+ send ( { type : "control_response" , response } ) ;
332483 return ;
333484 }
334485 if ( message . type === "user" ) {
0 commit comments