🔍 Code Extractor

function api_get_extensive_reference

Maturity: 51

Flask API endpoint that retrieves extensive search reference content by parsing a reference ID, fetching the corresponding block from the chat engine, and storing it as a document in the user's session.

File:
/tf/active/vicechatdev/vice_ai/app.py
Lines:
1582 - 1654
Complexity:
moderate

Purpose

This endpoint serves as part of a document reference system where extensive search results are stored in blocks. When a user requests a specific reference by ID, this function extracts the block and reference numbers, retrieves the comprehensive content from the chat engine, stores it as a document for the current user session, and returns metadata about the reference including a preview of the content.

Source Code

def api_get_extensive_reference(ref_id):
    """Retrieve an extensive search reference by reference ID"""
    try:
        if not chat_engine:
            return jsonify({'error': 'Chat engine not available'}), 500
        
        logger.info(f"🔍 Extensive reference request for ID: {ref_id}")
        
        # Parse the reference ID to extract block and reference numbers
        # Format: ref_extensive_<block_num>_<ref_num>
        if not ref_id.startswith('ref_extensive_'):
            return jsonify({'error': 'Invalid extensive reference ID format'}), 400
        
        try:
            parts = ref_id.split('_')
            if len(parts) >= 4:
                block_num = int(parts[2])
                ref_num = int(parts[3])
            else:
                return jsonify({'error': 'Invalid extensive reference ID format'}), 400
        except (ValueError, IndexError):
            return jsonify({'error': 'Invalid extensive reference ID format'}), 400
        
        # Get the comprehensive block that contains this reference
        block_info = chat_engine.get_block_by_number(block_num)
        
        if not block_info:
            logger.warning(f"⚠️  Extensive reference block not found: {block_num}")
            return jsonify({'error': 'Extensive reference block not found'}), 404
        
        # For extensive search references, we return the comprehensive summary
        # This represents the full content that was referenced
        content = block_info.get('content', '')
        title = f'Extensive Search Reference {ref_num} (from Block {block_num})'
        source = block_info.get('source', f'Extensive search from {block_info.get("documents_processed", 0)} documents')
        
        # Store the reference as a document in user session for use in chat
        user_email = session['user'].get('email', 'unknown')
        reference_doc_id = f"ref_extensive_{block_num}_{ref_num}_{int(time.time())}"
        
        # Create metadata for the extensive reference
        metadata = {
            'original_filename': f"{title}.txt",
            'source': source,
            'block_number': block_num,
            'reference_number': ref_num,
            'file_type': 'extensive_reference',
            'size': len(content),
            'reference_count': block_info.get('reference_count', 0),
            'documents_processed': block_info.get('documents_processed', 0)
        }
        
        # Store in user document storage
        store_document(user_email, reference_doc_id, None, content, metadata)
        
        logger.info(f"✅ Extensive reference stored: {title} ({len(content)} chars)")
        
        return jsonify({
            'document_id': reference_doc_id,
            'reference_id': ref_id,
            'block_number': block_num,
            'reference_number': ref_num,
            'name': title,
            'type': 'extensive_reference',
            'content_length': len(content),
            'source': source,
            'reference_count': block_info.get('reference_count', 0),
            'preview': content[:500] + '...' if len(content) > 500 else content
        })
        
    except Exception as e:
        logger.error(f"❌ Error retrieving extensive reference {ref_id}: {e}")
        return jsonify({'error': 'Failed to retrieve extensive reference'}), 500

Parameters

Name Type Default Kind
ref_id - - positional_or_keyword

Parameter Details

ref_id: String identifier for the extensive reference in format 'ref_extensive_<block_num>_<ref_num>' where block_num is the block number containing the reference and ref_num is the specific reference number within that block. Must start with 'ref_extensive_' prefix.

Return Value

Returns a Flask JSON response tuple. On success (200): JSON object containing document_id (unique identifier for stored document), reference_id (original ref_id), block_number, reference_number, name (descriptive title), type ('extensive_reference'), content_length (character count), source (origin description), reference_count (number of references in block), and preview (first 500 characters of content). On error: JSON object with 'error' key and appropriate HTTP status code (400 for invalid format, 404 for not found, 500 for server errors).

Dependencies

  • flask
  • logging
  • time
  • json
  • os
  • datetime
  • uuid
  • hashlib
  • threading
  • werkzeug
  • tempfile
  • re
  • traceback

Required Imports

from flask import Flask, jsonify, session
import logging
import time

Usage Example

# This is a Flask route handler, typically called via HTTP request
# Example HTTP request:
# GET /api/reference-extensive/ref_extensive_5_2
# Headers: Authorization token (handled by require_auth decorator)

# Example programmatic usage (within Flask app context):
from flask import Flask, session
import logging

app = Flask(__name__)
logger = logging.getLogger(__name__)

# Assuming chat_engine and store_document are configured
# Make request to endpoint:
# curl -X GET 'http://localhost:5000/api/reference-extensive/ref_extensive_5_2' \
#   -H 'Authorization: Bearer <token>'

# Expected response:
# {
#   "document_id": "ref_extensive_5_2_1234567890",
#   "reference_id": "ref_extensive_5_2",
#   "block_number": 5,
#   "reference_number": 2,
#   "name": "Extensive Search Reference 2 (from Block 5)",
#   "type": "extensive_reference",
#   "content_length": 1500,
#   "source": "Extensive search from 10 documents",
#   "reference_count": 5,
#   "preview": "Content preview..."
# }

Best Practices

  • Always validate the reference ID format before processing to prevent injection attacks
  • Ensure chat_engine is initialized before calling this endpoint
  • The function requires active user session with email field for document storage
  • Reference IDs must follow strict format: 'ref_extensive_<block_num>_<ref_num>'
  • Content preview is limited to 500 characters to prevent large response payloads
  • Error responses include appropriate HTTP status codes for different failure scenarios
  • Logging is used extensively for debugging and monitoring reference retrieval
  • The function stores retrieved references as documents for later use in chat context
  • Consider implementing rate limiting for this endpoint to prevent abuse
  • Ensure proper cleanup of stored documents to prevent session storage bloat

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function api_get_reference_block 81.8% similar

    API endpoint that retrieves a reference block from the most recent chat response by block number and stores it as a reference document in the user's session.

    From: /tf/active/vicechatdev/vice_ai/app.py
  • function api_get_reference_document 71.7% similar

    Flask API endpoint that retrieves a reference document from Neo4j database by UUID and stores it in the user's session for chat usage.

    From: /tf/active/vicechatdev/vice_ai/app.py
  • function api_add_reference_document 67.7% similar

    Flask API endpoint that accepts reference document text via POST request and stores it in the user's session for use as context in subsequent operations.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function api_get_chat_session 61.1% similar

    Flask API endpoint that retrieves a specific chat session by ID, verifying user access permissions before returning the session data.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function api_chat 60.3% similar

    Flask API endpoint that handles chat requests asynchronously, processing user queries through a RAG (Retrieval-Augmented Generation) engine with support for multiple modes, memory, web search, and custom configurations.

    From: /tf/active/vicechatdev/docchat/app.py
← Back to Browse