function text_chat_get_progress
Flask API endpoint that retrieves the progress status of an asynchronous text chat job, particularly for multi-cycle web search operations.
/tf/active/vicechatdev/vice_ai/new_app.py
2252 - 2264
simple
Purpose
This endpoint allows clients to poll the status of long-running text chat operations (especially those involving multi-cycle web searches) by providing a job_id. It returns progress information including status, and if completed, includes the cleaned result data. This is essential for implementing asynchronous chat operations with progress tracking in the UI.
Source Code
def text_chat_get_progress(job_id):
"""Get progress of text chat job (for multi-cycle web search)"""
if job_id not in text_chat_progress:
return jsonify({'error': 'Job not found'}), 404
progress_data = text_chat_progress[job_id].copy()
# If completed, include full result
if progress_data['status'] == 'completed' and 'result' in progress_data:
result = progress_data.pop('result')
progress_data['result'] = clean_for_json(result)
return jsonify(progress_data)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
job_id |
- | - | positional_or_keyword |
Parameter Details
job_id: String identifier for the text chat job. This ID is used to look up the job's progress in the text_chat_progress dictionary. Expected to be a unique identifier (likely UUID) generated when the job was initially created.
Return Value
Returns a Flask JSON response. On success (200): returns a dictionary containing progress data with keys like 'status' (e.g., 'pending', 'in_progress', 'completed'), and if completed, a 'result' key with cleaned JSON-serializable data. On error (404): returns {'error': 'Job not found'} if the job_id doesn't exist in the progress tracker.
Dependencies
flask
Required Imports
from flask import jsonify
Usage Example
# Client-side polling example
import requests
import time
# Assume job was started and returned job_id
job_id = 'abc-123-def-456'
# Poll for progress
while True:
response = requests.get(
f'http://localhost:5000/api/text-chat/progress/{job_id}',
headers={'Authorization': 'Bearer YOUR_TOKEN'}
)
if response.status_code == 404:
print('Job not found')
break
data = response.json()
print(f"Status: {data['status']}")
if data['status'] == 'completed':
result = data.get('result')
print(f"Result: {result}")
break
elif data['status'] == 'failed':
print(f"Error: {data.get('error')}")
break
time.sleep(2) # Poll every 2 seconds
Best Practices
- This endpoint is designed for polling; implement appropriate polling intervals on the client side to avoid overwhelming the server
- The function creates a copy of progress_data to avoid modifying the original dictionary during processing
- Ensure the text_chat_progress dictionary is properly cleaned up after jobs complete to prevent memory leaks
- Consider implementing job expiration/cleanup mechanisms for old or abandoned jobs
- The clean_for_json function is critical for ensuring all result data is JSON-serializable
- Authentication is required via the require_auth decorator; ensure proper token/session management
- Consider adding rate limiting to prevent abuse of the polling endpoint
- For production use, consider using a proper job queue system (like Celery with Redis) instead of an in-memory dictionary
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function smartstat_get_progress 72.8% similar
-
function api_task_status 72.0% similar
-
function api_index_progress 71.5% similar
-
function enhanced_workflow_progress 67.2% similar
-
function get_analysis_progress 65.8% similar