| 20 | return True # Require API key |
| 21 | |
| 22 | async def process(self, input: dict, request: Request) -> dict | Response: |
| 23 | # Extract parameters (support both query params for GET and body for POST) |
| 24 | if request.method == "GET": |
| 25 | context_id = request.args.get("context_id", "") |
| 26 | length = int(request.args.get("length", 100)) |
| 27 | else: |
| 28 | context_id = input.get("context_id", "") |
| 29 | length = input.get("length", 100) |
| 30 | |
| 31 | if not context_id: |
| 32 | return Response('{"error": "context_id is required"}', status=400, mimetype="application/json") |
| 33 | |
| 34 | # Get context |
| 35 | context = AgentContext.use(context_id) |
| 36 | if not context: |
| 37 | return Response('{"error": "Context not found"}', status=404, mimetype="application/json") |
| 38 | |
| 39 | try: |
| 40 | # Get total number of log items |
| 41 | total_items = len(context.log.logs) |
| 42 | |
| 43 | # Calculate start position (from newest, so we work backwards) |
| 44 | start_pos = max(0, total_items - length) |
| 45 | |
| 46 | # Get log items from the calculated start position |
| 47 | log_output = context.log.output(start=start_pos) |
| 48 | log_items = log_output.items |
| 49 | |
| 50 | # Return log data with metadata |
| 51 | return { |
| 52 | "context_id": context_id, |
| 53 | "log": { |
| 54 | "guid": context.log.guid, |
| 55 | "total_items": total_items, |
| 56 | "returned_items": len(log_items), |
| 57 | "start_position": start_pos, |
| 58 | "progress": context.log.progress, |
| 59 | "progress_active": bool(context.log.progress_active), |
| 60 | "items": log_items |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | except Exception as e: |
| 65 | return Response(f'{{"error": "{str(e)}"}}', status=500, mimetype="application/json") |