MCPcopy Index your code
hub / github.com/liuup/claude-code-analysis / insertMessageChain

Method insertMessageChain

src/utils/sessionStorage.ts:993–1083  ·  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

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