| import axios from 'axios' |
| import { getOauthConfig } from '../constants/oauth.js' |
| import type { SDKMessage } from '../entrypoints/agentSdkTypes.js' |
| import { logForDebugging } from '../utils/debug.js' |
| import { getOAuthHeaders, prepareApiRequest } from '../utils/teleport/api.js' |
|
|
| export const HISTORY_PAGE_SIZE = 100 |
|
|
| export type HistoryPage = { |
| |
| events: SDKMessage[] |
| |
| firstId: string | null |
| |
| hasMore: boolean |
| } |
|
|
| type SessionEventsResponse = { |
| data: SDKMessage[] |
| has_more: boolean |
| first_id: string | null |
| last_id: string | null |
| } |
|
|
| export type HistoryAuthCtx = { |
| baseUrl: string |
| headers: Record<string, string> |
| } |
|
|
| |
| export async function createHistoryAuthCtx( |
| sessionId: string, |
| ): Promise<HistoryAuthCtx> { |
| const { accessToken, orgUUID } = await prepareApiRequest() |
| return { |
| baseUrl: `${getOauthConfig().BASE_API_URL}/v1/sessions/${sessionId}/events`, |
| headers: { |
| ...getOAuthHeaders(accessToken), |
| 'anthropic-beta': 'ccr-byoc-2025-07-29', |
| 'x-organization-uuid': orgUUID, |
| }, |
| } |
| } |
|
|
| async function fetchPage( |
| ctx: HistoryAuthCtx, |
| params: Record<string, string | number | boolean>, |
| label: string, |
| ): Promise<HistoryPage | null> { |
| const resp = await axios |
| .get<SessionEventsResponse>(ctx.baseUrl, { |
| headers: ctx.headers, |
| params, |
| timeout: 15000, |
| validateStatus: () => true, |
| }) |
| .catch(() => null) |
| if (!resp || resp.status !== 200) { |
| logForDebugging(`[${label}] HTTP ${resp?.status ?? 'error'}`) |
| return null |
| } |
| return { |
| events: Array.isArray(resp.data.data) ? resp.data.data : [], |
| firstId: resp.data.first_id, |
| hasMore: resp.data.has_more, |
| } |
| } |
|
|
| |
| |
| |
| |
| export async function fetchLatestEvents( |
| ctx: HistoryAuthCtx, |
| limit = HISTORY_PAGE_SIZE, |
| ): Promise<HistoryPage | null> { |
| return fetchPage(ctx, { limit, anchor_to_latest: true }, 'fetchLatestEvents') |
| } |
|
|
| |
| export async function fetchOlderEvents( |
| ctx: HistoryAuthCtx, |
| beforeId: string, |
| limit = HISTORY_PAGE_SIZE, |
| ): Promise<HistoryPage | null> { |
| return fetchPage(ctx, { limit, before_id: beforeId }, 'fetchOlderEvents') |
| } |
|
|