MCPcopy Create free account
hub / github.com/Kilo-Org/cloud / streamSessionItems

Function streamSessionItems

services/session-ingest/src/queue-consumer.ts:256–296  ·  view source on GitHub ↗
(
  r2Key: string,
  body: ReadableStream<Uint8Array>,
  onItem: (rawItem: Record<string, unknown>) => Promise<void>
)

Source from the content-addressed store, hash-verified

254 return;
255 }
256 for (const change of ingestResult.changes) {
257 mergedChanges.set(change.name, change.value);
258 }
259 mergedAttentionSignals.push(...(ingestResult.attentionSignals ?? []));
260 };
261
262 const stage = async (rawItem: Record<string, unknown>): Promise<boolean> => {
263 if (!accepted) return false;
264 const parsed = SessionItemSchema.safeParse(rawItem);
265 if (!parsed.success) {
266 console.warn('Skipping invalid item in queue consumer', {
267 r2Key,
268 type: rawItem['type'],
269 errors: parsed.error.issues.map(i => i.message),
270 });
271 return true;
272 }
273
274 const item = parsed.data;
275 const { item_id } = getItemIdentity(item);
276
277 const itemDataJson = JSON.stringify(item.data);
278 const itemDataBytes = encoder.encode(itemDataJson).byteLength;
279
280 if (chunkItemIds.has(item_id)) {
281 await flushChunkToSessionDO();
282 if (!accepted) return false;
283 }
284
285 // Offload data above the DO SQLite row limit to R2; the DO stores a
286 // reference and an empty inline blob. Send only identity fields over RPC so
287 // a single oversized item cannot exceed Cloudflare's RPC payload limit.
288 const itemForRpc = itemDataBytes > MAX_INGEST_ITEM_BYTES ? slimItemForR2Reference(item) : item;
289 const itemForRpcDataBytes =
290 itemDataBytes > MAX_INGEST_ITEM_BYTES
291 ? encoder.encode(JSON.stringify(itemForRpc.data)).byteLength
292 : itemDataBytes;
293 if (itemDataBytes > MAX_INGEST_ITEM_BYTES) {
294 const itemR2Key = `items/${kiloUserId}/${sessionId}/${item_id}/${ingestedAt}`;
295 const staged = await withDORetry(
296 () => getSessionIngestDO(env, { kiloUserId, sessionId }),
297 stub =>
298 stub.stageR2Object(
299 { kiloUserId, sessionId, key: itemR2Key },

Callers 1

ingestStagedSessionItemsFunction · 0.85

Calls 6

createItemExtractorFunction · 0.85
writeMethod · 0.80
getReaderMethod · 0.65
endMethod · 0.65
cancelMethod · 0.65
warnMethod · 0.65

Tested by

no test coverage detected