Skip to content

Commit d021c47

Browse files
committed
chore: rename requestlog->apidump
Signed-off-by: Danny Kopping <[email protected]>
1 parent dece97c commit d021c47

File tree

5 files changed

+60
-60
lines changed

5 files changed

+60
-60
lines changed

bridge_integration_test.go

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"github.com/coder/aibridge/config"
2727
aibcontext "github.com/coder/aibridge/context"
2828
"github.com/coder/aibridge/fixtures"
29-
"github.com/coder/aibridge/intercept/requestlog"
29+
"github.com/coder/aibridge/intercept/apidump"
3030
"github.com/coder/aibridge/internal/testutil"
3131
"github.com/coder/aibridge/mcp"
3232
"github.com/coder/aibridge/provider"
@@ -1853,11 +1853,11 @@ func openaiCfg(url, key string) config.OpenAI {
18531853
}
18541854
}
18551855

1856-
func openaiCfgWithLogDir(url, key, logDir string) config.OpenAI {
1856+
func openaiCfgWithAPIDump(url, key, dumpDir string) config.OpenAI {
18571857
return config.OpenAI{
1858-
BaseURL: url,
1859-
Key: key,
1860-
RequestLogDir: logDir,
1858+
BaseURL: url,
1859+
Key: key,
1860+
APIDumpDir: dumpDir,
18611861
}
18621862
}
18631863

@@ -1868,15 +1868,15 @@ func anthropicCfg(url, key string) config.Anthropic {
18681868
}
18691869
}
18701870

1871-
func anthropicCfgWithLogDir(url, key, logDir string) config.Anthropic {
1871+
func anthropicCfgWithAPIDump(url, key, dumpDir string) config.Anthropic {
18721872
return config.Anthropic{
1873-
BaseURL: url,
1874-
Key: key,
1875-
RequestLogDir: logDir,
1873+
BaseURL: url,
1874+
Key: key,
1875+
APIDumpDir: dumpDir,
18761876
}
18771877
}
18781878

1879-
func TestRequestLogging(t *testing.T) {
1879+
func TestAPIDump(t *testing.T) {
18801880
t.Parallel()
18811881

18821882
cases := []struct {
@@ -1890,9 +1890,9 @@ func TestRequestLogging(t *testing.T) {
18901890
name: config.ProviderAnthropic,
18911891
fixture: fixtures.AntSimple,
18921892
providerName: config.ProviderAnthropic,
1893-
configureFunc: func(addr, logDir string, client aibridge.Recorder) (*aibridge.RequestBridge, error) {
1893+
configureFunc: func(addr, dumpDir string, client aibridge.Recorder) (*aibridge.RequestBridge, error) {
18941894
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: false}).Leveled(slog.LevelDebug)
1895-
providers := []aibridge.Provider{provider.NewAnthropic(anthropicCfgWithLogDir(addr, apiKey, logDir), nil)}
1895+
providers := []aibridge.Provider{provider.NewAnthropic(anthropicCfgWithAPIDump(addr, apiKey, dumpDir), nil)}
18961896
return aibridge.NewRequestBridge(t.Context(), providers, client, mcp.NewServerProxyManager(nil, testTracer), logger, nil, testTracer)
18971897
},
18981898
createRequestFunc: createAnthropicMessagesReq,
@@ -1901,9 +1901,9 @@ func TestRequestLogging(t *testing.T) {
19011901
name: config.ProviderOpenAI,
19021902
fixture: fixtures.OaiChatSimple,
19031903
providerName: config.ProviderOpenAI,
1904-
configureFunc: func(addr, logDir string, client aibridge.Recorder) (*aibridge.RequestBridge, error) {
1904+
configureFunc: func(addr, dumpDir string, client aibridge.Recorder) (*aibridge.RequestBridge, error) {
19051905
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: false}).Leveled(slog.LevelDebug)
1906-
providers := []aibridge.Provider{provider.NewOpenAI(openaiCfgWithLogDir(addr, apiKey, logDir))}
1906+
providers := []aibridge.Provider{provider.NewOpenAI(openaiCfgWithAPIDump(addr, apiKey, dumpDir))}
19071907
return aibridge.NewRequestBridge(t.Context(), providers, client, mcp.NewServerProxyManager(nil, testTracer), logger, nil, testTracer)
19081908
},
19091909
createRequestFunc: createOpenAIChatCompletionsReq,
@@ -1928,11 +1928,11 @@ func TestRequestLogging(t *testing.T) {
19281928
srv := newMockServer(ctx, t, files, nil)
19291929
t.Cleanup(srv.Close)
19301930

1931-
// Create temp dir for request logs.
1932-
logDir := t.TempDir()
1931+
// Create temp dir for API dumps.
1932+
dumpDir := t.TempDir()
19331933

19341934
recorderClient := &testutil.MockRecorder{}
1935-
b, err := tc.configureFunc(srv.URL, logDir, recorderClient)
1935+
b, err := tc.configureFunc(srv.URL, dumpDir, recorderClient)
19361936
require.NoError(t, err)
19371937

19381938
mockSrv := httptest.NewUnstartedServer(b)
@@ -1949,26 +1949,26 @@ func TestRequestLogging(t *testing.T) {
19491949
defer resp.Body.Close()
19501950
_, _ = io.ReadAll(resp.Body)
19511951

1952-
// Verify log files were created.
1952+
// Verify dump files were created.
19531953
interceptions := recorderClient.RecordedInterceptions()
19541954
require.Len(t, interceptions, 1)
19551955
interceptionID, err := uuid.Parse(interceptions[0].ID)
19561956
require.NoError(t, err)
19571957
model := interceptions[0].Model
19581958

1959-
reqLogFile := requestlog.LogPath(logDir, tc.providerName, model, interceptionID, "req")
1960-
respLogFile := requestlog.LogPath(logDir, tc.providerName, model, interceptionID, "resp")
1959+
reqDumpFile := apidump.DumpPath(dumpDir, tc.providerName, model, interceptionID, "req")
1960+
respDumpFile := apidump.DumpPath(dumpDir, tc.providerName, model, interceptionID, "resp")
19611961

1962-
// Verify request log exists and contains expected HTTP request format.
1963-
reqLogData, err := os.ReadFile(reqLogFile)
1964-
require.NoError(t, err, "request log file should exist")
1965-
require.Contains(t, string(reqLogData), "POST ")
1966-
require.Contains(t, string(reqLogData), "Host:")
1962+
// Verify request dump exists and contains expected HTTP request format.
1963+
reqDumpData, err := os.ReadFile(reqDumpFile)
1964+
require.NoError(t, err, "request dump file should exist")
1965+
require.Contains(t, string(reqDumpData), "POST ")
1966+
require.Contains(t, string(reqDumpData), "Host:")
19671967

1968-
// Verify response log exists and contains expected HTTP response format.
1969-
respLogData, err := os.ReadFile(respLogFile)
1970-
require.NoError(t, err, "response log file should exist")
1971-
require.Contains(t, string(respLogData), "200 OK")
1968+
// Verify response dump exists and contains expected HTTP response format.
1969+
respDumpData, err := os.ReadFile(respDumpFile)
1970+
require.NoError(t, err, "response dump file should exist")
1971+
require.Contains(t, string(respDumpData), "200 OK")
19721972

19731973
recorderClient.VerifyAllInterceptionsEnded(t)
19741974
})

config/config.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ const (
66
)
77

88
type Anthropic struct {
9-
BaseURL string
10-
Key string
11-
RequestLogDir string
9+
BaseURL string
10+
Key string
11+
APIDumpDir string
1212
}
1313

1414
type AWSBedrock struct {
@@ -21,7 +21,7 @@ type AWSBedrock struct {
2121
}
2222

2323
type OpenAI struct {
24-
BaseURL string
25-
Key string
26-
RequestLogDir string
24+
BaseURL string
25+
Key string
26+
APIDumpDir string
2727
}
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package requestlog
1+
package apidump
22

33
import (
44
"bytes"
@@ -19,57 +19,57 @@ type MiddlewareNext = func(*http.Request) (*http.Response, error)
1919
// Middleware is an HTTP middleware function compatible with SDK WithMiddleware options.
2020
type Middleware = func(*http.Request, MiddlewareNext) (*http.Response, error)
2121

22-
// LogPath returns the path to a request/response log file for a given interception.
22+
// DumpPath returns the path to a request/response dump file for a given interception.
2323
// kind should be "req" or "resp".
24-
func LogPath(baseDir, provider, model string, interceptionID uuid.UUID, kind string) string {
24+
func DumpPath(baseDir, provider, model string, interceptionID uuid.UUID, kind string) string {
2525
safeModel := strings.ReplaceAll(model, "/", "-")
2626
return filepath.Join(baseDir, provider, safeModel, fmt.Sprintf("%s.%s.txt", interceptionID, kind))
2727
}
2828

29-
// NewMiddleware returns a middleware function that logs requests and responses to files.
30-
// Files are written to the path returned by LogPath.
29+
// NewMiddleware returns a middleware function that dumps requests and responses to files.
30+
// Files are written to the path returned by DumpPath.
3131
// If baseDir is empty, returns nil (no middleware).
3232
func NewMiddleware(baseDir, provider, model string, interceptionID uuid.UUID) Middleware {
3333
if baseDir == "" {
3434
return nil
3535
}
3636

37-
logger := &logger{
37+
d := &dumper{
3838
baseDir: baseDir,
3939
provider: provider,
4040
model: model,
4141
interceptionID: interceptionID,
4242
}
4343

4444
return func(req *http.Request, next MiddlewareNext) (*http.Response, error) {
45-
if err := logger.logRequest(req); err != nil {
46-
fmt.Fprintf(os.Stderr, "requestlog: failed to log request: %v\n", err)
45+
if err := d.dumpRequest(req); err != nil {
46+
fmt.Fprintf(os.Stderr, "apidump: failed to dump request: %v\n", err)
4747
}
4848

4949
resp, err := next(req)
5050
if err != nil {
5151
return resp, err
5252
}
5353

54-
if err := logger.logResponse(resp); err != nil {
55-
fmt.Fprintf(os.Stderr, "requestlog: failed to log response: %v\n", err)
54+
if err := d.dumpResponse(resp); err != nil {
55+
fmt.Fprintf(os.Stderr, "apidump: failed to dump response: %v\n", err)
5656
}
5757

5858
return resp, nil
5959
}
6060
}
6161

62-
type logger struct {
62+
type dumper struct {
6363
baseDir string
6464
provider string
6565
model string
6666
interceptionID uuid.UUID
6767
}
6868

69-
func (l *logger) logRequest(req *http.Request) error {
70-
logPath := LogPath(l.baseDir, l.provider, l.model, l.interceptionID, "req")
71-
if err := os.MkdirAll(filepath.Dir(logPath), 0o755); err != nil {
72-
return fmt.Errorf("create log dir: %w", err)
69+
func (d *dumper) dumpRequest(req *http.Request) error {
70+
dumpPath := DumpPath(d.baseDir, d.provider, d.model, d.interceptionID, "req")
71+
if err := os.MkdirAll(filepath.Dir(dumpPath), 0o755); err != nil {
72+
return fmt.Errorf("create dump dir: %w", err)
7373
}
7474

7575
// Read and restore body
@@ -95,11 +95,11 @@ func (l *logger) logRequest(req *http.Request) error {
9595
fmt.Fprintf(&buf, "\r\n")
9696
buf.Write(prettyPrintJSON(bodyBytes))
9797

98-
return os.WriteFile(logPath, buf.Bytes(), 0o644)
98+
return os.WriteFile(dumpPath, buf.Bytes(), 0o644)
9999
}
100100

101-
func (l *logger) logResponse(resp *http.Response) error {
102-
logPath := LogPath(l.baseDir, l.provider, l.model, l.interceptionID, "resp")
101+
func (d *dumper) dumpResponse(resp *http.Response) error {
102+
dumpPath := DumpPath(d.baseDir, d.provider, d.model, d.interceptionID, "resp")
103103

104104
// Read and restore body
105105
var bodyBytes []byte
@@ -123,7 +123,7 @@ func (l *logger) logResponse(resp *http.Response) error {
123123
fmt.Fprintf(&buf, "\r\n")
124124
buf.Write(prettyPrintJSON(bodyBytes))
125125

126-
return os.WriteFile(logPath, buf.Bytes(), 0o644)
126+
return os.WriteFile(dumpPath, buf.Bytes(), 0o644)
127127
}
128128

129129
// prettyPrintJSON returns indented JSON if body is valid JSON, otherwise returns body as-is.

intercept/chatcompletions/base.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
aibcontext "github.com/coder/aibridge/context"
1212
"github.com/coder/aibridge/mcp"
1313
"github.com/coder/aibridge/recorder"
14-
"github.com/coder/aibridge/intercept/requestlog"
14+
"github.com/coder/aibridge/intercept/apidump"
1515
"github.com/coder/aibridge/tracing"
1616
"github.com/google/uuid"
1717
"github.com/openai/openai-go/v3"
@@ -38,8 +38,8 @@ type interceptionBase struct {
3838
func (i *interceptionBase) newCompletionsService() openai.ChatCompletionService {
3939
opts := []option.RequestOption{option.WithAPIKey(i.cfg.Key), option.WithBaseURL(i.cfg.BaseURL)}
4040

41-
// Add request logging if configured
42-
if mw := requestlog.NewMiddleware(i.cfg.RequestLogDir, config.ProviderOpenAI, i.Model(), i.id); mw != nil {
41+
// Add API dump middleware if configured
42+
if mw := apidump.NewMiddleware(i.cfg.APIDumpDir, config.ProviderOpenAI, i.Model(), i.id); mw != nil {
4343
opts = append(opts, option.WithMiddleware(mw))
4444
}
4545

intercept/messages/base.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
aibcontext "github.com/coder/aibridge/context"
2222
"github.com/coder/aibridge/mcp"
2323
"github.com/coder/aibridge/recorder"
24-
"github.com/coder/aibridge/intercept/requestlog"
24+
"github.com/coder/aibridge/intercept/apidump"
2525
"github.com/coder/aibridge/tracing"
2626

2727
"github.com/google/uuid"
@@ -154,8 +154,8 @@ func (i *interceptionBase) newMessagesService(ctx context.Context, opts ...optio
154154
opts = append(opts, option.WithAPIKey(i.cfg.Key))
155155
opts = append(opts, option.WithBaseURL(i.cfg.BaseURL))
156156

157-
// Add request logging if configured
158-
if mw := requestlog.NewMiddleware(i.cfg.RequestLogDir, aibconfig.ProviderAnthropic, i.Model(), i.id); mw != nil {
157+
// Add API dump middleware if configured
158+
if mw := apidump.NewMiddleware(i.cfg.APIDumpDir, aibconfig.ProviderAnthropic, i.Model(), i.id); mw != nil {
159159
opts = append(opts, option.WithMiddleware(mw))
160160
}
161161

0 commit comments

Comments
 (0)