import { Type } from "typebox"; import { ChunkingManager, getChunkSize } from "../shared/chunking"; import { serializeResponseWithMeta, type SerializedToolResult } from "../shared/serialize"; export interface GetNextChunkDeps { chunkingManager: ChunkingManager; } export function createGetNextChunkTool(deps: GetNextChunkDeps) { return { name: "get_next_chunk", label: "Get Next Chunk", description: "Get next chunk of large response using continuation token from previous chunked response.", promptSnippet: "Fetch the next chunk of a previously chunked rtfd response by continuation token", parameters: Type.Object({ continuation_token: Type.String({ description: "Token from the continuation_token field in a chunked response", }), }), async execute( _toolCallId: string, params: { continuation_token: string }, ): Promise { const chunkSize = getChunkSize(); if (chunkSize === 0) { return serializeResponseWithMeta({ error: "Chunking is disabled (RTFD_CHUNK_TOKENS=0)", }); } const result = deps.chunkingManager.getNextChunk(params.continuation_token, chunkSize); if (result === null) { return serializeResponseWithMeta({ error: "Invalid or expired continuation token. Tokens expire after 10 minutes.", }); } const chunkData: Record = { ...result }; if (chunkData.has_more) { chunkData.hint = `Call get_next_chunk('${chunkData.continuation_token}') for more content`; } return serializeResponseWithMeta(chunkData); }, }; }