File size: 22,985 Bytes
6cb5907
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
# MasterLLM API Response Schemas

This document outlines the COMPLETE JSON response structures for the `api_routes_v2.py` endpoints. All fields are shown with example data.

## 1. **Session Management**

### `GET /api/v2/sessions`

**Description:** Returns a list of all chat sessions.
**Parameters:** `limit` (int, default=100), `skip` (int, default=0), `include_stats` (bool, default=False)

**Response (Default - `include_stats=False`):**

```json
{
  "sessions": [
    {
      "session_id": "550e8400-e29b-41d4-a716-446655440000",
      "created_at": null,
      "last_activity": null
    },
    {
      "session_id": "7c9e6679-7425-40de-944b-e07fc1f90ae7",
      "created_at": null,
      "last_activity": null
    }
  ],
  "pagination": {
    "total": 2,
    "returned": 2,
    "limit": 100,
    "skip": 0,
    "has_more": false
  }
}
```

**Response (with `include_stats=True`):**

```json
{
  // The sessions needs to be sorted according to the last activity. The latest should come first key is last_activity
  "sessions": [
    {
      "session_id": "550e8400-e29b-41d4-a716-446655440000",
      "user_id": "user_12345",
      "created_at": "2023-11-15T08:30:00.000000Z",
      "last_activity": "2023-11-15T09:45:00.000000Z",
      // state should indicate the status of the pipeline or the conversation. If the pipeline is in pogress it should be in pogress if not then completed, initial, or failed
      "state": "initial",
      // current_file is not needed for session list
      "current_file": "s3://my-bucket/masterllm/550e8400-e29b-41d4-a716-446655440000/report.pdf",
      "chat_name": "Q3 Financial Report Analysis",
      "stats": {
        "tokens_used": 1500,
        "cost_estimate": 0.045
      },
      "total_messages": 12,
      "pipeline_executions_count": 3
    }
  ],
  "pagination": {
    "total": 1,
    "returned": 1,
    "limit": 100,
    "skip": 0,
    "has_more": false
  }
}
```

### `GET /api/v2/sessions/{session_id}/history`

**Description:** Returns the full message history and pipeline history for a specific session.
**Parameters:** `limit` (int, default=50)

**Response:**

```json
{
  "session_id": "550e8400-e29b-41d4-a716-446655440000",
  // This is the history of msg and chats
  "history": [
    {
      // Need message_id
      "role": "user",
      // content should always have basic conversation content, it should not have the output of the pipeline
      "content": "Uploaded file: report.pdf",
      "timestamp": "2023-11-15T08:30:00Z",
      "file_data": {
        "has_file": true,
        "file_name": "report.pdf",
        "file_url": "https://s3.amazonaws.com/bucket/key?signature=..."
      },
      // This should be proper, like if file was attached to this particular msg then true with file details like a presigned url with a validity of 7 days.
      // If Not then file should be false
      "file": true,
      "fileName": "report.pdf",
      "fileUrl": "https://s3.amazonaws.com/bucket/key?signature=..."
    },
    {
      "role": "assistant",
      "content": "File uploaded successfully.",
      "timestamp": "2023-11-15T08:30:05Z",
      "file_data": {
        "has_file": false
      },
      "file": false,
      "fileName": null,
      "fileUrl": null
    },
    {
      "role": "user",
      "content": "Summarize this document.",
      "timestamp": "2023-11-15T08:31:00Z",
      "file_data": {
        "has_file": false
      },
      "file": false,
      "fileName": null,
      "fileUrl": null
    }
  ],
  "count": 3,
  "limit": 50,
  "chat_name": "Q3 Financial Report Analysis",
  "pipelines_history": [
    {
      "pipeline_id": "pipe_987654321",
      "pipeline_name": "Document Summarization",
      // status should indicate the state of the pipeline, if success then completed, if not failed or in pogress.
      "status": "completed",
      "created_at": "2023-11-15T08:31:05Z",
      "created_from": "request",
      "model_provider": "bedrock",
      "model_name": "anthropic.claude-3-sonnet-20240229-v1:0",
      // No need for pipeline_s3_key store it but dont need in the output,
      "pipeline_s3_key": "masterllm/pipelines/pipe_987654321.json",
      // result_preview should be named as result. And in here if the pipeline has completed with success the output should be provided, if not or failed this should be null.
      "result_preview": "The Q3 report indicates a 15% growth in revenue...",
      // Please add another key called hasError, and error details where if the pipeline is failed or has error the error should be provided. If the pipeline is passed then this will be null and hasError false.
      "updated_at": "2023-11-15T08:32:00Z",
      "tools": ["extract_text", "summarize_text"],
      "component_count": 2,
      "components": [
        {
          // Please provide a component ID to unique identify each
          // Please provide the status of each component, like status: success, failed, or running
          // If success send the result of this component run. like component_output: This will have the output of the component
          // Please send hasError, and error details where if the component is failed or has error the error should be provided. If the component is passed then this will be null and hasError false
          // Send the component meta data if present like the duration run, token consumed, etc (This is optional)
          "step_id": 1,
          "tool_name": "extract_text",
          "description": "Extract text from the PDF",
          "parameters": {
            "file_path": "s3://..."
          }
        },
        {
          "step_id": 2,
          "tool_name": "summarize_text",
          "description": "Summarize the extracted content",
          "parameters": {
            "max_length": 500
          }
        }
      ]
    }
  ]
}
```

### `DELETE /api/v2/sessions/{session_id}`

**Description:** Deletes a session and its S3 conversation history.

**Response:**

```json
{
  "status": "deleted",
  "session_id": "550e8400-e29b-41d4-a716-446655440000"
}
```

### `PUT /api/v2/sessions/{session_id}/rename`

**Description:** Renames a session.

**Response:**

```json
{
  "success": true,
  "session_id": "550e8400-e29b-41d4-a716-446655440000",
  "new_name": "New Chat Title"
}
```

### `GET /api/v2/sessions/{session_id}/pipelines`

**Description:** Get all pipeline executions for a session.

**Response:**

```json
{
  "session_id": "550e8400-e29b-41d4-a716-446655440000",
  "pipelines": [
    {
      "pipeline_id": "pipe_987654321",
      "pipeline_name": "Document Summarization",
      "status": "completed",
      "created_at": "2023-11-15T08:31:05Z",
      "final_output_url": "s3://bucket/results/pipe_987654321_output.json"
      // Please also send the components and its data as you are / will be sending in the `GET /api/v2/sessions/{session_id}/history`
    }
  ]
}
```

---

## 2. **Unified Chat (Non-Streaming)**

### `POST /api/v2/chat/unified`

**Description:** Main interaction endpoint.

#### **Scenario 1: Casual Chat / Question**

**Response:**

```json
{
  // This should also have the message id, as this is a msg from the LLM
  "assistant_response": "I am MasterLLM, an advanced AI agent designed to help you process documents.",
  "output": {},
  "final_output": null,
  "exception": null,
  "api_response": {
    "type": "informational_response",
    "message": "I am MasterLLM, an advanced AI agent designed to help you process documents.",
    "intent_classification": {
      "intent": "question",
      "confidence": 0.98,
      "requires_pipeline": false
    }
  },
  "intent": {
    "intent": "question",
    "confidence": 0.98,
    "requires_pipeline": false
  },
  "chat_id": "550e8400-e29b-41d4-a716-446655440000",
  "state": "initial",
  "file": false,
  "fileName": null,
  "fileUrl": null
}
```

#### **Scenario 2: Clarification Needed**

**Response:**

```json
{
  // This should also have the message id, as this is a msg from the LLM
  "assistant_response": "I'm not sure what you mean by 'do it'. Could you please specify if you want to summarize, extract text, or translate the document?",
  "output": {},
  "final_output": null,
  "exception": null,
  "api_response": {
    "type": "clarification_needed",
    "message": "I'm not sure what you mean by 'do it'. Could you please specify if you want to summarize, extract text, or translate the document?",
    "intent_classification": {
      "intent": "unclear",
      "confidence": 0.45,
      "requires_pipeline": false
    },
    "suggestions": [
      "Be more specific about what you want to do",
      "Use keywords like: extract, summarize, translate, etc.",
      "Type 'help' for examples"
    ]
  },
  "intent": {
    "intent": "unclear",
    "confidence": 0.45,
    "requires_pipeline": false
  },
  "chat_id": "550e8400-e29b-41d4-a716-446655440000",
  "state": "initial",
  "file": true,
  "fileName": "report.pdf",
  "fileUrl": "https://s3.amazonaws.com/..."
}
```

#### **Scenario 3: File Uploaded (Only)**

**Response:**

```json
{
  // This should also have the message id, as this is a msg from the LLM
  "assistant_response": "πŸ“ File uploaded successfully. Tell me what you'd like to do with it (e.g., extract text, get tables, summarize).",
  "output": {},
  "final_output": null,
  "exception": null,
  "api_response": {
    "type": "file_uploaded",
    "file": {
      "bucket": "my-s3-bucket",
      "key": "masterllm/550e8400-e29b-41d4-a716-446655440000/report.pdf",
      "s3_uri": "s3://my-s3-bucket/masterllm/550e8400-e29b-41d4-a716-446655440000/report.pdf",
      "presigned_url": "https://s3.amazonaws.com/my-s3-bucket/masterllm/550e8400-e29b-41d4-a716-446655440000/report.pdf?AMAZONPARAMS...",
      "presigned_expires_at": "2023-11-22T08:30:00.000000Z"
    },
    "next_action": "send_instruction"
  },
  "intent": {
    "intent": "file_uploaded"
  },
  "chat_id": "550e8400-e29b-41d4-a716-446655440000",
  "state": "initial",
  "file": true,
  "fileName": "report.pdf",
  "fileUrl": "https://s3.amazonaws.com/my-s3-bucket/..."
}
```

#### **Scenario 4: Pipeline Generated (Proposal)**

**Response:**

```json
{
  // This should also have the message id, as this is a msg from the LLM
  "assistant_response": "🎯 **Pipeline Created: Document Summarization**\nHere's what I'll do:\n  1. extract_text\n  2. summarize_text\n**Ready to proceed?**\n- Type 'approve' or 'yes' to execute\n- Type 'reject' or 'no' to cancel\n- Describe changes to modify the plan",
  "output": {
    "pipeline_id": "pipe_123abc",
    "pipeline_name": "Document Summarization",
    "steps_count": 2,
    "tools": ["extract_text", "summarize_text"]
  },
  "final_output": null,
  "exception": null,
  "api_response": {
    "type": "pipeline_generated",
    "message": "Pipeline successfully created",
    "pipeline": {
      "pipeline_id": "pipe_123abc",
      "pipeline_name": "Document Summarization",
      "session_id": "550e8400-e29b-41d4-a716-446655440000",
      "created_at": "2023-11-15T08:35:00Z",
      "_generator": "Claude 3.5 Sonnet",
      "_model": "claude-3-5-sonnet-20240620",
      // Please provide a component ID to unique identify each
      "components": [
        {
          "tool_name": "extract_text",
          "args": { "pages": "all" }
        },
        {
          "tool_name": "summarize_text",
          "args": { "style": "brief" }
        }
      ],
      "pipeline_steps": [
        {
          "tool_name": "extract_text",
          "args": { "pages": "all" }
        },
        {
          "tool_name": "summarize_text",
          "args": { "style": "brief" }
        }
      ]
    },
    "pipeline_summary": {
      "name": "Document Summarization",
      "total_steps": 2,
      "steps": [
        {
          "tool_name": "extract_text",
          "args": { "pages": "all" }
        },
        {
          "tool_name": "summarize_text",
          "args": { "style": "brief" }
        }
      ],
      "generator": "Claude 3.5 Sonnet",
      "model": "claude-3-5-sonnet-20240620"
    },
    "required_action": "approval",
    "next_steps": {
      "approve": "Type 'approve' or 'yes'",
      "reject": "Type 'reject' or 'no'",
      "modify": "Describe your changes"
    }
  },
  "intent": {
    "intent": "pipeline_request",
    "requires_pipeline": true,
    "confidence": 0.99
  },
  "chat_id": "550e8400-e29b-41d4-a716-446655440000",
  "state": "pipeline_proposed",
  "file": true,
  "fileName": "report.pdf",
  "fileUrl": "https://s3.amazonaws.com/..."
}
```

#### **Scenario 5: Pipeline Completed (Execution Success)**

**Response:**

```json
{
  // This should also have the message id, as this is a msg from the LLM
  "assistant_response": "πŸŽ‰ Pipeline completed successfully!",
  // send hasError, if it has then the error should be provide in the exception if not this will be null and hasError will be false
  "output": {
    "component_summary": "Pipeline executed successfully",
    "steps": 2
  },
  // final_output should have data only if the LLM has run the pipeline and completed with success. Then populate the final output here
  "final_output": {
    // Please add output id for which the user can download the output if he wants. The output id will be used to downalod using a separate endpoint.
    // Optional: check if you can send the download url in here as well for downalod output. if not the output id will work as of now.
    "text": "The Q3 financial report highlights a strong performance with a 15% revenue increase driven by the new product line launch. Operating expenses were reduced by 5% due to efficiency improvements. The outlook for Q4 is positive with projected growth of 10%.",

    // This result is not needed as this should be in the component level
    "result": {
      "summary": "The Q3 financial report highlights a strong performance with a 15% revenue increase driven by the new product line launch. Operating expenses were reduced by 5% due to efficiency improvements. The outlook for Q4 is positive with projected growth of 10%.",
      "metadata": {
        "source_file": "report.pdf",
        "processed_at": "2023-11-15T08:36:00Z"
      }
    }
  },
  "exception": null,
  "api_response": {
    "type": "pipeline_completed",
    "result": {
      "summary": "The Q3 financial report highlights a strong performance with a 15% revenue increase driven by the new product line launch. Operating expenses were reduced by 5% due to efficiency improvements. The outlook for Q4 is positive with projected growth of 10%.",
      "metadata": {
        "source_file": "report.pdf",
        "processed_at": "2023-11-15T08:36:00Z"
      }
    },
    "pipeline": {
      "pipeline_id": "pipe_123abc",
      "pipeline_name": "Document Summarization",
      // Please provide a component ID to unique identify each
      // Please provide the status of each component, like status: success, failed, or running
      // If success send the result of this component run. like component_output: This will have the output of the component
      // Please send hasError, and error details where if the component is failed or has error the error should be provided. If the component is passed then this will be null and hasError false
      // Send the component meta data if present like the duration run, token consumed, etc (This is optional)
      "components": [
        { "tool_name": "extract_text" },
        { "tool_name": "summarize_text" }
      ]
    }
  },
  "intent": {
    "intent": "pipeline_execute"
  },
  "chat_id": "550e8400-e29b-41d4-a716-446655440000",
  "state": "initial",
  "file": true,
  "fileName": "report.pdf",
  "fileUrl": "https://s3.amazonaws.com/..."
}
```

#### **Scenario 6: Error**

**Response:**

```json
{
  // This should also have the message id, as this is a msg from the LLM
  "assistant_response": "❌ Pipeline execution failed: ValueError: Input file is empty.",
  "output": {},
  "final_output": null,
  // send hasError, if it has then the error should be provide in the exception if not this will be null and hasError will be false
  "exception": "ValueError: Input file is empty.",
  "api_response": {
    // Dont change the api_response format, keep it same
    // If the error is in a particular component then send error in the component level
    "type": "error",
    "error_code": "PIPELINE_EXECUTION_FAILED",
    "message": "ValueError: Input file is empty."
  },
  "intent": {
    "intent": "pipeline_execute"
  },
  "chat_id": "550e8400-e29b-41d4-a716-446655440000",
  "state": "initial",
  "file": true,
  "fileName": "report.pdf",
  "fileUrl": "https://s3.amazonaws.com/..."
}
```

---

## 3. **Unified Chat (Streaming)**

### `POST /api/v2/chat/unified/stream`

**Description:** Streaming version of the unified chat. Returns **NDJSON** (Newlines Delimited JSON). Each line is a JSON object.

**Event Type: `status`**

```json
{
  "type": "status",
  "message": "Analyzing request and creating a pipeline...",
  "chat_id": "550e8400-e29b-41d4-a716-446655440000",
  "state": "initial"
}
```

**Event Type: `assistant_final` (Typical Chat Response)**

```json
{
  "type": "assistant_final",
  "content": "Hello! How can I help you today?",
  "intent": {
    "intent": "casual_chat",
    "confidence": 0.99
  },
  "chat_id": "550e8400-e29b-41d4-a716-446655440000",
  "state": "initial"
}
```

**Event Type: `assistant_final` (Pipeline Proposed)**

```json
{
  "type": "assistant_final",
  "content": "🎯 **Pipeline Created: Document Summarization**\nHere's what I'll do:\n  1. extract_text\n  2. summarize_text\n...",
  "pipeline": {
    "pipeline_id": "pipe_123abc",
    "pipeline_name": "Document Summarization",
    "components": [
      { "tool_name": "extract_text" },
      { "tool_name": "summarize_text" }
    ]
  },
  "output": {
    "pipeline_id": "pipe_123abc",
    "pipeline_name": "Document Summarization",
    "steps_count": 2,
    "tools": ["extract_text", "summarize_text"]
  },
  "chat_id": "550e8400-e29b-41d4-a716-446655440000",
  "state": "pipeline_proposed"
}
```

**Event Type: `assistant_delta` (Streaming Execution Output)**

```json
{
  "type": "assistant_delta",
  "content": "βœ… Approved! Starting execution of: **Document Summarization**\n\nπŸš€ Processing, please wait...\n\n⏳ Step 1: extract_text - Processing...",
  "chat_id": "550e8400-e29b-41d4-a716-446655440000",
  "state": "executing"
}
```

**Event Type: `assistant_final` (Execution Completed)**

```json
{
  "type": "assistant_final",
  "content": "πŸŽ‰ Pipeline Completed Successfully!\n- Pipeline: Document Summarization\n- Total Steps: 2\n- Successful: 2\n- Executor: Bedrock Agent\n\nThe Q3 financial report highlights...",
  "result": {
    "summary": "The Q3 financial report highlights...",
    "full_text": "..."
  },
  "output": {
    "component_summary": "Executed 2 steps successfully",
    "steps_completed": 2,
    "total_steps": 2
  },
  "final_output": {
    "text": "The Q3 financial report highlights...",
    "result": {
      "summary": "The Q3 financial report highlights...",
      "full_text": "..."
    }
  },
  "chat_id": "550e8400-e29b-41d4-a716-446655440000",
  "state": "initial"
}
```

**Event Type: `assistant_final` (Error)**

```json
{
  "type": "assistant_final",
  "content": "❌ Pipeline Execution Failed\n\nError: Service Unavailable",
  "error": "Service Unavailable",
  "exception": "Service Unavailable",
  "chat_id": "550e8400-e29b-41d4-a716-446655440000",
  "state": "initial"
}
```

---

## 4. **File Management**

### `POST /api/v2/chats/{chat_id}/upload`

**Description:** Direct file upload endpoint.

**Response:**

```json
{
  "status": "success",
  "message": "File uploaded to S3",
  "file": {
    "bucket": "my-s3-bucket",
    "key": "masterllm/550e8400-e29b-41d4-a716-446655440000/report.pdf",
    "s3_uri": "s3://my-s3-bucket/masterllm/550e8400-e29b-41d4-a716-446655440000/report.pdf",
    "presigned_url": "https://s3.amazonaws.com/my-s3-bucket/masterllm/550e8400-e29b-41d4-a716-446655440000/report.pdf?AMAZONPARAMS...",
    "presigned_expires_at": "2023-11-22T08:30:00.000000Z"
  },
  "chat_id": "550e8400-e29b-41d4-a716-446655440000",
  "next_action": "πŸ’¬ Now tell me what you'd like to do with this document"
}
```

### `GET /api/v2/files/{file_id}/url`

**Description:** Regenerate a temporary presigned URL for a file.

**Response:**

```json
{
  "file_id": "masterllm/550e8400-e29b-41d4-a716-446655440000/report.pdf",
  "presigned_url": "https://s3.amazonaws.com/my-s3-bucket/masterllm/550e8400-e29b-41d4-a716-446655440000/report.pdf?AMAZONPARAMS...",
  "expires_at": "2023-11-23T08:30:00.000000Z"
}
```

---

## 5. **Workflows**

### `GET /api/v2/workflows`

**Description:** List saved workflows.

**Response:**

```json
{
  "workflows": [
    {
      "workflow_id": "flow_888",
      "name": "Quarterly Report Analyzer",
      "created_at": "2023-10-01T12:00:00Z",
      "last_used": "2023-11-01T10:00:00Z",
      "steps_count": 3
    }
  ],
  "count": 1,
  "total": 1
}
```

### `GET /api/v2/workflows/{workflow_id}`

**Description:** Get a single workflow definition.

**Response:**

```json
{
  "workflow_id": "flow_888",
  "name": "Quarterly Report Analyzer",
  "description": "Extracts text, summarizes key points, and extracts financial tables.",
  "created_at": "2023-10-01T12:00:00Z",
  "updated_at": "2023-10-05T09:00:00Z",
  "definition": {
    "pipeline_name": "Quarterly Report Analyzer",
    // Please provide a component ID to unique identify each
    // Please provide the status of each component, like status: success, failed, or running
    // If success send the result of this component run. like component_output: This will have the output of the component
    // Please send hasError, and error details where if the component is failed or has error the error should be provided. If the component is passed then this will be null and hasError false
    // Send the component meta data if present like the duration run, token consumed, etc (This is optional)
    "components": [
      { "tool_name": "extract_text", "args": {} },
      { "tool_name": "summarize", "args": { "focus": "financials" } },
      { "tool_name": "extract_tables", "args": {} }
    ]
  },
  "source_pipeline_id": "pipe_old_123",
  "usage_count": 15
}
```

### `POST /api/v2/workflows/save`

**Description:** Save an existing pipeline as a reusable workflow.
**Parameters:** `pipeline_id` (form), `workflow_name` (form, optional)

**Response:**

```json
{
  "workflow_id": "flow_999",
  "pipeline_name": "New Saved Workflow",
  "source_pipeline_id": "pipe_123abc",
  "pipeline_status": "completed",
  "message": "Workflow saved successfully"
}
```

---

## 6. **System**

### `POST /api/v2/chats`

**Description:** Create a new empty chat session.

**Response:**

```json
{
  "chat_id": "550e8400-e29b-41d4-a716-446655440000"
}
```

### `GET /api/v2/health`

**Description:** Health check.

**Response:**

```json
{
  "status": "ok",
  "service": "MasterLLM v2.0",
  "time": "2023-11-15T08:30:00.000000Z"
}
```