MCPcopy
hub / github.com/codeaashu/claude-code / createSessionSpawner

Function createSessionSpawner

src/bridge/sessionRunner.ts:248–548  ·  view source on GitHub ↗
(deps: SessionSpawnerDeps)

Source from the content-addressed store, hash-verified

246}
247
248export function createSessionSpawner(deps: SessionSpawnerDeps): SessionSpawner {
249 return {
250 spawn(opts: SessionSpawnOpts, dir: string): SessionHandle {
251 // Debug file resolution:
252 // 1. If deps.debugFile is provided, use it with session ID suffix for uniqueness
253 // 2. If verbose or ant build, auto-generate a temp file path
254 // 3. Otherwise, no debug file
255 const safeId = safeFilenameId(opts.sessionId)
256 let debugFile: string | undefined
257 if (deps.debugFile) {
258 const ext = deps.debugFile.lastIndexOf('.')
259 if (ext > 0) {
260 debugFile = `${deps.debugFile.slice(0, ext)}-${safeId}${deps.debugFile.slice(ext)}`
261 } else {
262 debugFile = `${deps.debugFile}-${safeId}`
263 }
264 } else if (deps.verbose || process.env.USER_TYPE === 'ant') {
265 debugFile = join(tmpdir(), 'claude', `bridge-session-${safeId}.log`)
266 }
267
268 // Transcript file: write raw NDJSON lines for post-hoc analysis.
269 // Placed alongside the debug file when one is configured.
270 let transcriptStream: WriteStream | null = null
271 let transcriptPath: string | undefined
272 if (deps.debugFile) {
273 transcriptPath = join(
274 dirname(deps.debugFile),
275 `bridge-transcript-${safeId}.jsonl`,
276 )
277 transcriptStream = createWriteStream(transcriptPath, { flags: 'a' })
278 transcriptStream.on('error', err => {
279 deps.onDebug(
280 `[bridge:session] Transcript write error: ${err.message}`,
281 )
282 transcriptStream = null
283 })
284 deps.onDebug(`[bridge:session] Transcript log: ${transcriptPath}`)
285 }
286
287 const args = [
288 ...deps.scriptArgs,
289 '--print',
290 '--sdk-url',
291 opts.sdkUrl,
292 '--session-id',
293 opts.sessionId,
294 '--input-format',
295 'stream-json',
296 '--output-format',
297 'stream-json',
298 '--replay-user-messages',
299 ...(deps.verbose ? ['--verbose'] : []),
300 ...(debugFile ? ['--debug-file', debugFile] : []),
301 ...(deps.permissionMode
302 ? ['--permission-mode', deps.permissionMode]
303 : []),
304 ]
305

Callers 2

bridgeMainFunction · 0.85
runBridgeHeadlessFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected