MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / insertMessageChain

Method insertMessageChain

source code/utils/sessionStorage.ts:995–1085  ·  view source on GitHub ↗
(
    messages: Transcript,
    isSidechain: boolean = false,
    agentId?: string,
    startingParentUuid?: UUID | null,
    teamInfo?: { teamName?: string; agentName?: string },
  )

Source from the content-addressed store, hash-verified

993 }
994
995 async insertMessageChain(
996 messages: Transcript,
997 isSidechain: boolean = false,
998 agentId?: string,
999 startingParentUuid?: UUID | null,
1000 teamInfo?: { teamName?: string; agentName?: string },
1001 ) {
1002 return this.trackWrite(async () => {
1003 let parentUuid: UUID | null = startingParentUuid ?? null
1004
1005 // First user/assistant message materializes the session file.
1006 // Hook progress/attachment messages alone stay buffered.
1007 if (
1008 this.sessionFile === null &&
1009 messages.some(m => m.type === 'user' || m.type === 'assistant')
1010 ) {
1011 await this.materializeSessionFile()
1012 }
1013
1014 // Get current git branch once for this message chain
1015 let gitBranch: string | undefined
1016 try {
1017 gitBranch = await getBranch()
1018 } catch {
1019 // Not in a git repo or git command failed
1020 gitBranch = undefined
1021 }
1022
1023 // Get slug if one exists for this session (used for plan files, etc.)
1024 const sessionId = getSessionId()
1025 const slug = getPlanSlugCache().get(sessionId)
1026
1027 for (const message of messages) {
1028 const isCompactBoundary = isCompactBoundaryMessage(message)
1029
1030 // For tool_result messages, use the assistant message UUID from the message
1031 // if available (set at creation time), otherwise fall back to sequential parent
1032 let effectiveParentUuid = parentUuid
1033 if (
1034 message.type === 'user' &&
1035 'sourceToolAssistantUUID' in message &&
1036 message.sourceToolAssistantUUID
1037 ) {
1038 effectiveParentUuid = message.sourceToolAssistantUUID
1039 }
1040
1041 const transcriptMessage: TranscriptMessage = {
1042 parentUuid: isCompactBoundary ? null : effectiveParentUuid,
1043 logicalParentUuid: isCompactBoundary ? parentUuid : undefined,
1044 isSidechain,
1045 teamName: teamInfo?.teamName,
1046 agentName: teamInfo?.agentName,
1047 promptId:
1048 message.type === 'user' ? (getPromptId() ?? undefined) : undefined,
1049 agentId,
1050 ...message,
1051 // Session-stamp fields MUST come after the spread. On --fork-session
1052 // and --resume, messages arrive as SerializedMessage (carries source

Callers 2

recordTranscriptFunction · 0.80

Calls 14

trackWriteMethod · 0.95
appendEntryMethod · 0.95
getBranchFunction · 0.85
getSessionIdFunction · 0.85
getPlanSlugCacheFunction · 0.85
isCompactBoundaryMessageFunction · 0.85
getPromptIdFunction · 0.85
getUserTypeFunction · 0.85
getEntrypointFunction · 0.85
getCwdFunction · 0.85
isChainParticipantFunction · 0.85

Tested by

no test coverage detected