Skip to content

feat(platform): Add document_retrieve agent tool with pagination #659

Closed

Description

@larryro

Summary

Add a new document_retrieve agent tool that allows the AI agent (and workflow LLM nodes) to fetch the full or partial text content of a specific document. This complements rag_search (which returns relevance-ranked chunks) by providing sequential, structure-preserving document access.

Motivation

The existing rag_search tool returns top-k chunks ranked by relevance — ideal for Q&A but insufficient when the agent needs to:

  • Read a complete document to understand its structure
  • Process a document section by section (e.g., clause-by-clause analysis)
  • Use a full document as a template for generating new content

Large documents (50-100+ pages) easily exceed the LLM context window (~81K usable tokens ≈ ~324K chars). Paginated retrieval solves this by letting the agent request specific portions.

Proposed Tool Design

Tool name: document_retrieve
Registration: Add to agent_tools/tool_names.ts and agent_tools/tool_registry.ts

Input Schema

{
  documentId: string;        // Required: Convex document ID
  pageRange?: string;        // Optional: e.g., "1-10", "20-30" (defaults to first ~50K chars)
}

Output

{
  documentId: string;
  name: string;
  content: string;           // Document text for the requested range
  pageRange: { start: number; end: number };
  totalPages: number;
  truncated: boolean;        // True if content was capped
  totalChars: number;
}

Implementation Notes

  • Calls the RAG service endpoint from feat(rag): Add full document content retrieval endpoint #657 (GET /api/v1/documents/{doc_id}/content)
  • Cap output at ~50K chars to stay within context budget (tool results are further truncated to 8K chars in history, but the current turn sees the full result)
  • Resolve the document's RAG ID from Convex documents table using ragInfo
  • Pass team_id from tool context for access control

Key Files

  • services/platform/convex/agent_tools/documents/document_retrieve_tool.ts (new)
  • services/platform/convex/agent_tools/tool_names.ts (register name)
  • services/platform/convex/agent_tools/tool_registry.ts (register tool)
  • services/platform/convex/documents/ (existing document queries)

Workflow Integration Pattern

For large documents, use the workflow loop node to process in batches:

Step 1 (llm): document_retrieve(docId, pageRange="1-5") → get structure/TOC
Step 2 (loop): iterate over page ranges
  Each iteration (llm): document_retrieve(docId, pageRange="X-Y") → analyze section → JSON
Step 3 (llm): aggregate all iteration results → final output

Dependencies

Acceptance Criteria

  • New document_retrieve tool registered in tool registry
  • Supports pageRange parameter for paginated retrieval
  • Response capped at ~50K chars with truncated flag
  • Respects organization and team-based access control
  • Works both as a chat agent tool and in workflow LLM nodes
  • Unit tests covering: full retrieval, page ranges, truncation, access control, invalid document ID

Activity

  1. added
    priority: high
    feat
    service: platform
    on Mar 4, 2026
  2. yannickmonney commented on Apr 7, 2026

    @yannickmonney
    Contributor

    This feature is already implemented as document_retrieve tool at convex/agent_tools/documents/document_retrieve_tool.ts. It supports fileId lookup with chunk-based pagination. Tests exist at __tests__/document_retrieve_tool.test.ts (22 tests).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featpriority: highservice: platform

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions