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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import logging
import re
import sys
import zlib
from typing import Any, Dict, List, Optional, Tuple, Union
from urllib.parse import unquote

from config.global_state import GlobalState
from config.settings import FUNCTION_CALLING_DEBUG
from logging_utils.fc_debug import get_fc_logger
from logging_utils.grid_logger import GridFormatter

# FC debug logger for wire format parsing
fc_logger = get_fc_logger()


class HttpInterceptor:
    """
    Class to intercept and process HTTP requests and responses
    """

    def __init__(self, log_dir: str = "logs"):
        self.log_dir = log_dir
        self.logger = logging.getLogger("http_interceptor")
        self.response_buffer = ""  # Persistent buffer for accumulating response data
        # Accumulate unique function calls across streaming chunks
        # Key: (function_name, params_hash) - Value: {"name": str, "params": dict}
        self._accumulated_function_calls: dict[tuple[str, str], dict] = {}
        self.setup_logging()

    @staticmethod
    def setup_logging():
        """Set up logging configuration with colored output"""
        console_handler = logging.StreamHandler(sys.stderr)
        console_handler.setFormatter(
            GridFormatter(show_tree=False, colorize=True, burst_suppression=False)
        )
        console_handler.setLevel(logging.INFO)

        root_logger = logging.getLogger()
        root_logger.setLevel(logging.INFO)
        root_logger.addHandler(console_handler)

        logging.getLogger("asyncio").setLevel(logging.ERROR)
        logging.getLogger("websockets").setLevel(logging.ERROR)
        # Silence http_interceptor by default (too verbose)
        logging.getLogger("http_interceptor").setLevel(logging.WARNING)

    def reset_for_new_request(self) -> None:
        """Reset interceptor state for a new request.

        This clears the response buffer and function call accumulation state.
        Should be called at the start of each new GenerateContent request to
        ensure clean state.
        """
        self.response_buffer = ""
        self._accumulated_function_calls.clear()

    @staticmethod
    def should_intercept(host: str, path: str):
        """
        Determine if the request should be intercepted based on host and path
        """
        # Check if the endpoint contains GenerateContent
        if "GenerateContent" in path or "generateContent" in path:
            return True

        # Check for jserror logging endpoint
        if "jserror" in path:
            return True

        return False

    async def process_request(
        self, request_data: Union[int, bytes], host: str, path: str
    ) -> Union[int, bytes]:
        """
        Process the request data before sending to the server
        """
        if not self.should_intercept(host, path):
            return request_data

        # Log the request
        self.logger.debug(f"[Network] Intercepted request: {host}{path}")

        # Check for Quota Exceeded errors in jserror requests
        if "jserror" in path:
            try:
                decoded_path = unquote(path)
                if any(
                    keyword in decoded_path
                    for keyword in [
                        "exceeded quota",
                        "RESOURCE_EXHAUSTED",
                        "Failed to generate content",
                    ]
                ):
                    self.logger.critical(
                        f"🚨 CRITICAL: Detected Quota Exceeded error in network traffic! URL: {path}"
                    )

                    from api_utils.server_state import state

                    model_id = state.current_ai_studio_model_id
                    GlobalState.set_quota_exceeded(
                        message=decoded_path, model_id=model_id or ""
                    )
            except Exception as e:
                self.logger.error(f"Error parsing jserror path: {e}")

        return request_data

    async def process_response(
        self,
        response_data: Union[int, bytes],
        host: str,
        path: str,
        headers: Dict[Any, Any],
    ) -> Dict[str, Any]:
        """
        Process the response data before sending to the client using persistent buffering
        """
        try:
            # Handle chunked encoding
            decoded_data, is_done = self._decode_chunked(bytes(response_data))
            # Handle gzip encoding
            decoded_data = self._decompress_zlib_stream(decoded_data)

            # Convert to string and accumulate in persistent buffer
            try:
                decoded_str = decoded_data.decode("utf-8")
                self.response_buffer += decoded_str
            except UnicodeDecodeError:
                # Not UTF-8 data, return empty result
                return {"reason": "", "body": "", "function": [], "done": is_done}

            # Try to parse complete JSON objects from the buffer
            result = self.parse_response_from_buffer(is_done)
            return result
        except Exception as e:
            self.logger.debug(f"Error processing response: {e}")
            return {"reason": "", "body": "", "function": [], "done": False}

    def parse_response_from_buffer(self, is_done=False):
        """
        Parse complete JSON objects from the persistent response buffer.

        Function calls are deduplicated across streaming chunks to prevent
        the same function call from being returned multiple times. AI Studio's
        wire format sometimes sends the same function call data in multiple
        stream chunks.
        """
        resp = {"reason": "", "body": "", "function": [], "done": is_done}

        try:
            # Check buffer size to prevent memory leaks
            if len(self.response_buffer) > 10 * 1024 * 1024:  # 10MB limit
                self.logger.warning(
                    "Response buffer exceeded 10MB, clearing to prevent memory leak"
                )
                self.response_buffer = ""
                return resp

            # Look for complete JSON objects in the buffer
            pattern = rb'\[\[\[null,.*?]],"model"]'

            # Convert buffer to bytes for pattern matching
            buffer_bytes = self.response_buffer.encode("utf-8")
            matches = list(re.finditer(pattern, buffer_bytes))

            # Debug: Log match count when processing is done
            if is_done and matches and FUNCTION_CALLING_DEBUG:
                self.logger.debug(
                    f"[FC:Wire] Found {len(matches)} wire format matches in buffer"
                )

            if matches:
                # Process all complete matches found in buffer
                for match in matches:
                    try:
                        json_data = json.loads(match.group(0))
                        payload = json_data[0][0]

                        # Debug: Log payload structure for function call detection
                        if len(payload) >= 10 and FUNCTION_CALLING_DEBUG:
                            self.logger.debug(
                                f"[FC:Wire] Payload len={len(payload)}, [1]={payload[1]}, "
                                f"has [10]={len(payload) > 10 and isinstance(payload[10], list)}"
                            )

                        if len(payload) == 2:  # body
                            resp["body"] += payload[1]
                        elif (
                            len(payload) == 11
                            and payload[1] is None
                            and isinstance(payload[10], list)
                        ):  # function
                            array_tool_calls = payload[10]
                            func_name = array_tool_calls[0]
                            raw_args = array_tool_calls[1]
                            # Log raw wire format for debugging
                            if FUNCTION_CALLING_DEBUG:
                                self.logger.debug(
                                    f"[FC:Wire] Raw args for '{func_name}': {json.dumps(raw_args)[:500]}"
                                )
                            params = self.parse_toolcall_params(raw_args)

                            # Accumulate unique function calls across streaming chunks.
                            # AI Studio's wire format sends duplicate function call data
                            # in multiple stream chunks. We accumulate and deduplicate them,
                            # returning the complete list only when done=True.
                            try:
                                params_str = json.dumps(params, sort_keys=True)
                            except (TypeError, ValueError):
                                params_str = str(params)
                            dedup_key = (func_name, params_str)

                            if dedup_key in self._accumulated_function_calls:
                                if FUNCTION_CALLING_DEBUG:
                                    self.logger.debug(
                                        f"[FC:Wire] Skipping duplicate function call: {func_name}"
                                    )
                                continue

                            # Store this function call in accumulator
                            func_call_data = {"name": func_name, "params": params}
                            self._accumulated_function_calls[dedup_key] = func_call_data

                            # Log warning if params are empty for tracking potential parse failures
                            if not params:
                                if FUNCTION_CALLING_DEBUG:
                                    self.logger.warning(
                                        f"[FC:Wire] Function '{func_name}' parsed with empty args - "
                                        f"may indicate wire format parsing failure. Raw: {array_tool_calls[1][:200] if array_tool_calls[1] else 'None'}..."
                                    )
                                    fc_logger.log_wire_parse(
                                        req_id="",
                                        func_name=func_name,
                                        params=params,
                                        success=False,
                                    )
                            else:
                                if FUNCTION_CALLING_DEBUG:
                                    fc_logger.log_wire_parse(
                                        req_id="",
                                        func_name=func_name,
                                        params=params,
                                        success=True,
                                    )
                        elif len(payload) > 2:  # reason
                            resp["reason"] += payload[1]

                    except (json.JSONDecodeError, IndexError, TypeError) as e:
                        self.logger.debug(f"Failed to parse JSON chunk: {e}")
                        continue

                # Remove processed data from buffer
                last_match_end = matches[-1].end()
                if last_match_end < len(buffer_bytes):
                    remaining_bytes = buffer_bytes[last_match_end:]
                    self.response_buffer = remaining_bytes.decode(
                        "utf-8", errors="ignore"
                    )
                else:
                    self.response_buffer = ""

                # When stream is done, return ALL accumulated unique function calls
                # During streaming (not done), we return empty list to avoid duplicates
                # The complete list is returned only with done=True
                if is_done and self._accumulated_function_calls:
                    resp["function"] = list(self._accumulated_function_calls.values())
                    if FUNCTION_CALLING_DEBUG:
                        self.logger.debug(
                            f"[FC:Wire] Returning {len(resp['function'])} unique function call(s) on done"
                        )
                    # Clear accumulator for next request
                    self._accumulated_function_calls.clear()
                elif self._accumulated_function_calls:
                    # During streaming, still return the current accumulated list
                    # so that has_seen_functions can be set correctly
                    resp["function"] = list(self._accumulated_function_calls.values())
            else:
                self.logger.debug("Buffering incomplete JSON data...")

        except UnicodeDecodeError as e:
            self.logger.debug(f"Unicode decode error in buffer parsing: {e}")
            self.response_buffer = ""
        except Exception as e:
            self.logger.debug(f"Error in buffer parsing: {e}")

        return resp

    def _unwrap_to_param_list(
        self, args: Any, max_depth: int = 10
    ) -> Optional[List[Any]]:
        """Unwrap nested lists until we find the actual parameter list.

        A parameter list is a list where each element is a [name, value] tuple
        and the name (first element) is a string.

        Args:
            args: The nested structure to unwrap.
            max_depth: Maximum unwrap depth to prevent infinite loops.

        Returns:
            The parameter list, or None if not found.
        """
        current = args
        for _ in range(max_depth):
            if not isinstance(current, list) or len(current) == 0:
                return None

            # Check if current is already a param list
            # A param list is a list of [string_name, value] tuples
            first_elem = current[0]
            if isinstance(first_elem, list) and len(first_elem) >= 2:
                # Check if first element of first tuple is a string (param name)
                if isinstance(first_elem[0], str):
                    # This is the param list!
                    return current

            # Not a param list yet, unwrap one level
            if isinstance(current[0], list):
                current = current[0]
            else:
                # Can't unwrap further
                return None

        self.logger.warning(f"Max unwrap depth reached for args: {args}")
        return None

    def parse_toolcall_params(self, args: Any) -> Dict[str, Any]:
        """Parse function call parameters from AI Studio's wire format.

        AI Studio uses a type-length encoding:
        - Length 1: null
        - Length 2: number/integer
        - Length 3: string
        - Length 4: boolean
        - Length 5: object (nested structure)
        - Length 6: array

        The wire format has variable nesting levels. We unwrap until we find
        the actual parameter tuples (where first element is a string).
        """
        try:
            # Unwrap nested lists until we find the parameter list
            # A parameter list is a list of [name, value] tuples where name is a string
            params = self._unwrap_to_param_list(args)
            if params is None:
                self.logger.warning(f"Could not find param list in args: {args}")
                return {}

            func_params = {}
            for param in params:
                param_name = param[0]
                param_value = param[1]

                # Debug: log raw param_value structure
                self.logger.debug(
                    f"Parsing param '{param_name}': type={type(param_value).__name__}, "
                    f"len={len(param_value) if isinstance(param_value, list) else 'N/A'}, "
                    f"value={str(param_value)[:100]}"
                )

                if isinstance(param_value, list):
                    if len(param_value) == 1:  # null
                        func_params[param_name] = None
                    elif len(param_value) == 2:  # number and integer
                        func_params[param_name] = param_value[1]
                    elif len(param_value) == 3:  # string
                        func_params[param_name] = param_value[2]
                    elif len(param_value) == 4:  # boolean
                        func_params[param_name] = param_value[3] == 1
                    elif len(param_value) == 5:  # object
                        func_params[param_name] = self.parse_toolcall_params(
                            param_value[4]
                        )
                    elif len(param_value) == 6:  # array
                        # Arrays are at index 5, containing list of encoded items
                        array_items = param_value[5]
                        if isinstance(array_items, list):
                            func_params[param_name] = self._parse_array_items(
                                array_items
                            )
                        else:
                            func_params[param_name] = []
                    else:
                        # Unknown type - log and store raw value
                        self.logger.debug(
                            f"Unknown param type length {len(param_value)} for {param_name}"
                        )
                        func_params[param_name] = param_value
                else:
                    # Non-list value - store directly
                    self.logger.debug(
                        f"Non-list param value for {param_name}: {type(param_value)}"
                    )
                    func_params[param_name] = param_value
            return func_params
        except Exception as e:
            self.logger.debug(f"Error parsing toolcall params: {e}")
            raise e

    def _parse_array_items(self, array_items: List[Any]) -> List[Any]:
        """Parse array items from AI Studio's wire format.

        Each item in the array follows the same type encoding as parameters.
        The wire format uses variable nesting levels, so we must handle:
        - Direct type-encoded items: [null], [null, num], [null, null, str], etc.
        - Wrapped items: [[...actual item...]] - extra nesting layer
        - Object items with param lists inside
        """
        self.logger.debug(
            f"_parse_array_items input (len={len(array_items)}): {array_items[:3] if len(array_items) > 3 else array_items}"
        )
        result = []
        for i, item in enumerate(array_items):
            self.logger.debug(f"  Array item[{i}] raw: {item}")
            parsed = self._parse_single_array_item(item)
            self.logger.debug(f"  Array item[{i}] parsed: {parsed}")
            result.append(parsed)
        return result

    def _parse_single_array_item(self, item: Any) -> Any:
        """Parse a single array item, handling variable nesting depth.

        This method recursively unwraps nested structures until it finds
        a recognizable type-encoded value or a parameter list (object).
        """
        if not isinstance(item, list):
            return item

        if len(item) == 0:
            return None

        # PRIORITY CHECK: Is this a param list (object)?
        # A param list is [[name, value], [name, value], ...]
        # This must be checked BEFORE length-based type decoding
        if self._looks_like_param_list(item):
            return self.parse_toolcall_params([item])

        # Check for type-encoded values based on structure
        # Type-encoded values have None/value patterns at specific positions

        # Length 1: Could be null OR a wrapper containing nested data
        if len(item) == 1:
            inner = item[0]
            # If inner is a list, this is a wrapper - unwrap and recurse
            if isinstance(inner, list):
                return self._parse_single_array_item(inner)
            # If inner is None or non-list, this is a null value
            return None

        # Length 2: number/integer - [null, value]
        if len(item) == 2:
            if item[0] is None and item[1] is not None:
                return item[1]
            # Could be a 2-element wrapper or 2-element param list (already handled above)
            if isinstance(item[0], list):
                return self._parse_single_array_item(item[0])
            return item[1]

        # Length 3: string - [null, null, value]
        if len(item) == 3:
            if item[0] is None and item[1] is None:
                return item[2]
            # Could be wrapper
            if isinstance(item[0], list):
                return self._parse_single_array_item(item[0])
            return item[2]

        # Length 4: boolean - [null, null, null, 0|1]
        if len(item) == 4:
            if item[0] is None and item[1] is None and item[2] is None:
                return item[3] == 1
            # Could be wrapper
            if isinstance(item[0], list):
                return self._parse_single_array_item(item[0])
            return item[3] == 1

        # Length 5: object - [null, null, null, null, params]
        if len(item) == 5:
            if item[4] is not None:
                return self.parse_toolcall_params(item[4])
            return {}

        # Length 6: nested array - [null, null, null, null, null, items]
        if len(item) == 6:
            nested_items = item[5]
            if isinstance(nested_items, list):
                return self._parse_array_items(nested_items)
            return []

        # Unknown structure - try to unwrap first element if it's a list
        if isinstance(item[0], list):
            return self._parse_single_array_item(item[0])

        # Fallback: return as-is
        self.logger.debug(
            f"Unknown array item structure (len={len(item)}): {item[:3]}..."
        )
        return item

    def _looks_like_param_list(self, data: Any) -> bool:
        """Check if data looks like a parameter list (list of [name, value] tuples).

        A param list is a list where elements are [string_name, encoded_value] tuples.
        """
        if not isinstance(data, list) or len(data) == 0:
            return False

        # Check first element - should be a list with string as first element
        first = data[0]
        if isinstance(first, list) and len(first) >= 2:
            if isinstance(first[0], str):
                return True

        return False

    @staticmethod
    def _decompress_zlib_stream(compressed_stream: Union[bytearray, bytes]) -> bytes:
        decompressor = zlib.decompressobj(wbits=zlib.MAX_WBITS | 32)
        decompressed = decompressor.decompress(compressed_stream)
        return decompressed

    @staticmethod
    def _decode_chunked(response_body: bytes) -> Tuple[bytes, bool]:
        chunked_data = bytearray()
        while True:
            length_crlf_idx = response_body.find(b"\r\n")
            if length_crlf_idx == -1:
                break

            hex_length = response_body[:length_crlf_idx]
            try:
                length = int(hex_length, 16)
            except ValueError as e:
                logging.error(f"Parsing chunked length failed: {e}")
                break

            if length == 0:
                length_crlf_idx = response_body.find(b"0\r\n\r\n")
                if length_crlf_idx != -1:
                    return bytes(chunked_data), True

            if length + 2 > len(response_body):
                break

            chunked_data.extend(
                response_body[length_crlf_idx + 2 : length_crlf_idx + 2 + length]
            )
            if length_crlf_idx + 2 + length + 2 > len(response_body):
                break

            response_body = response_body[length_crlf_idx + 2 + length + 2 :]
        return bytes(chunked_data), False