MCPcopy Create free account
hub / github.com/tiann/hapi / run

Method run

cli/src/api/apiSession.ts:398–449  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

396
397 const limit = 200
398 const run = async () => {
399 let cursor = startSeq
400 while (true) {
401 const response = await axios.get(
402 `${configuration.apiUrl}/cli/sessions/${encodeURIComponent(this.sessionId)}/messages`,
403 {
404 params: { afterSeq: cursor, limit },
405 headers: buildHubRequestHeaders({
406 Authorization: `Bearer ${this.token}`,
407 'Content-Type': 'application/json'
408 }),
409 timeout: 15_000
410 }
411 )
412
413 const parsed = CliMessagesResponseSchema.safeParse(response.data)
414 if (!parsed.success) {
415 throw apiValidationError('Invalid /cli/sessions/:id/messages response', response)
416 }
417
418 const messages = parsed.data.messages
419 if (messages.length === 0) {
420 break
421 }
422
423 let maxSeq = cursor
424 for (const message of messages) {
425 if (typeof message.seq === 'number') {
426 if (message.seq > maxSeq) {
427 maxSeq = message.seq
428 }
429 }
430 this.handleIncomingMessage(message)
431 }
432
433 const observedSeq = this.incomingFilter.cursorSeq() ?? maxSeq
434 const nextCursor = Math.max(maxSeq, observedSeq)
435 if (nextCursor <= cursor) {
436 logger.debug('[API] Backfill stopped due to non-advancing cursor', {
437 cursor,
438 maxSeq,
439 observedSeq
440 })
441 break
442 }
443
444 cursor = nextCursor
445 if (messages.length < limit) {
446 break
447 }
448 }
449 }
450
451 this.backfillInFlight = run().finally(() => {
452 this.backfillInFlight = null

Callers

nothing calls this directly

Calls 6

handleIncomingMessageMethod · 0.95
buildHubRequestHeadersFunction · 0.90
apiValidationErrorFunction · 0.90
getMethod · 0.80
cursorSeqMethod · 0.80
debugMethod · 0.80

Tested by

no test coverage detected