MCPcopy Create free account
hub / github.com/Noumena-Network/code / dumpRequest

Function dumpRequest

src/services/api/dumpPrompts.ts:94–148  ·  view source on GitHub ↗
(
  body: string,
  ts: string,
  state: DumpState,
  filePath: string,
)

Source from the content-addressed store, hash-verified

92}
93
94function dumpRequest(
95 body: string,
96 ts: string,
97 state: DumpState,
98 filePath: string,
99): void {
100 try {
101 const req = jsonParse(body) as Record<string, unknown>
102 addApiRequestToCache(req)
103
104 if ((process.env.NCODE_BUILD_MODE !== 'noumena' && process.env.USER_TYPE !== 'ant')) return
105 const entries: string[] = []
106 const messages = (req.messages ?? []) as Array<{ role?: string }>
107
108 // Write init data (system, tools, metadata) on first request,
109 // and a system_update entry whenever it changes.
110 // Cheap fingerprint first: system+tools don't change between turns,
111 // so skip the 300ms stringify when the shape is unchanged.
112 const fingerprint = initFingerprint(req)
113 if (!state.initialized || fingerprint !== state.lastInitFingerprint) {
114 const { messages: _, ...initData } = req
115 const initDataStr = jsonStringify(initData)
116 const initDataHash = hashString(initDataStr)
117 state.lastInitFingerprint = fingerprint
118 if (!state.initialized) {
119 state.initialized = true
120 state.lastInitDataHash = initDataHash
121 // Reuse initDataStr rather than re-serializing initData inside a wrapper.
122 // timestamp from toISOString() contains no chars needing JSON escaping.
123 entries.push(
124 `{"type":"init","timestamp":"${ts}","data":${initDataStr}}`,
125 )
126 } else if (initDataHash !== state.lastInitDataHash) {
127 state.lastInitDataHash = initDataHash
128 entries.push(
129 `{"type":"system_update","timestamp":"${ts}","data":${initDataStr}}`,
130 )
131 }
132 }
133
134 // Write only new user messages (assistant messages captured in response)
135 for (const msg of messages.slice(state.messageCountSeen)) {
136 if (msg.role === 'user') {
137 entries.push(
138 jsonStringify({ type: 'message', timestamp: ts, data: msg }),
139 )
140 }
141 }
142 state.messageCountSeen = messages.length
143
144 appendToFile(filePath, entries)
145 } catch {
146 // Ignore parsing errors
147 }
148}
149
150export function createDumpPromptsFetch(
151 agentIdOrSessionId: string,

Callers

nothing calls this directly

Calls 6

jsonParseFunction · 0.85
addApiRequestToCacheFunction · 0.85
initFingerprintFunction · 0.85
jsonStringifyFunction · 0.85
appendToFileFunction · 0.85
hashStringFunction · 0.70

Tested by

no test coverage detected