Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utilities to dump Copilot traffic. #664

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/codegate/providers/copilot/provider.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import asyncio
import contextlib
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we use this import?

import datetime
import os
import re
import ssl
import tempfile
from dataclasses import dataclass
from typing import Dict, List, Optional, Tuple, Union
from urllib.parse import unquote, urljoin, urlparse
Expand All @@ -26,6 +30,41 @@
setup_logging()
logger = structlog.get_logger("codegate").bind(origin="copilot_proxy")


TEMPDIR = None
if os.getenv("CODEGATE_DUMP_DIR"):
basedir = os.getenv("CODEGATE_DUMP_DIR")
TEMPDIR = tempfile.TemporaryDirectory(prefix="codegate-", dir=basedir, delete=False)


def _dump_data(suffix, func):
if os.getenv("CODEGATE_DUMP_DIR"):
buf = bytearray(b"")

def inner(self, data: bytes):
nonlocal buf
func(self, data)
buf.extend(data)

if data == b"0\r\n\r\n":
ts = datetime.datetime.now()
fname = os.path.join(TEMPDIR.name, ts.strftime(f"{suffix}-%Y%m%dT%H%M%S%f.txt"))
with open(fname, mode="wb") as fd:
fd.write(buf)
buf = bytearray()

return inner
return func


def _dump_request(func):
return _dump_data("request", func)


def _dump_response(func):
return _dump_data("response", func)


# Constants
MAX_BUFFER_SIZE = 10 * 1024 * 1024 # 10MB
CHUNK_SIZE = 64 * 1024 # 64KB
Expand Down Expand Up @@ -911,6 +950,7 @@ def _process_chunk(self, chunk: bytes):

self.stream_queue.put_nowait(record)

@_dump_response
def _proxy_transport_write(self, data: bytes):
# For debugging only
# self.data_sent.append(data)
Expand Down
Loading