Skip to content

Commit 51977e3

Browse files
authored
Merge pull request #13 from PinataCloud/feat/cli-user-agent
feat: identify CLI requests with a pinata-cli User-Agent
2 parents 083622f + 860fbd4 commit 51977e3

18 files changed

Lines changed: 190 additions & 50 deletions

File tree

.goreleaser.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ builds:
1919
- env:
2020
- CGO_ENABLED=0
2121
main: ./main.go
22+
ldflags:
23+
- -s -w -X pinata/internal/version.Version={{.Version}}
2224
goos:
2325
- linux
2426
- windows

internal/agents/chat/chat.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"pinata/internal/common"
1313
"pinata/internal/config"
14+
"pinata/internal/httpclient"
1415

1516
"charm.land/bubbles/v2/spinner"
1617
"charm.land/bubbles/v2/viewport"
@@ -530,7 +531,6 @@ func (m ChatModel) handleStreamEvent(event StreamEvent) (tea.Model, tea.Cmd) {
530531
return m, nil
531532
}
532533

533-
534534
// allowTool approves the pending tool call.
535535
func (m ChatModel) allowTool(always bool) (tea.Model, tea.Cmd) {
536536
if m.pendingTool == nil {
@@ -989,7 +989,7 @@ func fetchAgentInfo(agentID string) (*AgentInfo, error) {
989989
req.Header.Set("Authorization", "Bearer "+string(jwt))
990990
req.Header.Set("Content-Type", "application/json")
991991

992-
client := &http.Client{}
992+
client := httpclient.Client
993993
resp, err := client.Do(req)
994994
if err != nil {
995995
return nil, err

internal/agents/chat/stream.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package chat
33
import (
44
"context"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"net/http"
89
"strings"
910
"sync"
1011
"time"
1112

1213
"pinata/internal/config"
14+
"pinata/internal/version"
1315

1416
"github.com/google/uuid"
1517
"github.com/gorilla/websocket"
@@ -146,6 +148,7 @@ func StreamChat(ctx context.Context, agentID, token, model, session string, mess
146148
// Connect to WebSocket with proper headers and timeout
147149
header := http.Header{}
148150
header.Set("Origin", "https://"+config.GetAgentsHost())
151+
header.Set("User-Agent", version.UserAgent())
149152
if token != "" {
150153
header.Set("Authorization", "Bearer "+token)
151154
}
@@ -237,7 +240,7 @@ func StreamChat(ctx context.Context, agentID, token, model, session string, mess
237240
if connectResp.Error != nil {
238241
errMsg = connectResp.Error.Message
239242
}
240-
events <- StreamEvent{Type: StreamEventError, Error: fmt.Errorf(errMsg)}
243+
events <- StreamEvent{Type: StreamEventError, Error: errors.New(errMsg)}
241244
return
242245
}
243246

@@ -380,7 +383,7 @@ func StreamChat(ctx context.Context, agentID, token, model, session string, mess
380383
if reason, ok := payload.Data["reason"].(string); ok {
381384
errMsg = reason
382385
}
383-
events <- StreamEvent{Type: StreamEventError, Error: fmt.Errorf(errMsg)}
386+
events <- StreamEvent{Type: StreamEventError, Error: errors.New(errMsg)}
384387
return
385388
}
386389

@@ -413,7 +416,7 @@ func StreamChat(ctx context.Context, agentID, token, model, session string, mess
413416
if msg.Error != nil {
414417
errMsg = msg.Error.Message
415418
}
416-
events <- StreamEvent{Type: StreamEventError, Error: fmt.Errorf(errMsg)}
419+
events <- StreamEvent{Type: StreamEventError, Error: errors.New(errMsg)}
417420
return
418421
}
419422
// Request accepted, continue waiting for events

internal/agents/client.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import (
77
"fmt"
88
"io"
99
"net/http"
10+
1011
"pinata/internal/common"
1112
"pinata/internal/config"
13+
"pinata/internal/httpclient"
1214
)
1315

1416
const apiVersion = "v0"
@@ -43,7 +45,7 @@ func doRequest(method, path string, body interface{}) (*http.Response, error) {
4345
req.Header.Set("Authorization", "Bearer "+string(jwt))
4446
req.Header.Set("Content-Type", "application/json")
4547

46-
client := &http.Client{}
48+
client := httpclient.Client
4749
resp, err := client.Do(req)
4850
if err != nil {
4951
return nil, errors.Join(err, errors.New("failed to send the request"))
@@ -130,7 +132,7 @@ func doRequestURL(method, url string, body interface{}) (*http.Response, error)
130132
req.Header.Set("Authorization", "Bearer "+string(jwt))
131133
req.Header.Set("Content-Type", "application/json")
132134

133-
client := &http.Client{}
135+
client := httpclient.Client
134136
resp, err := client.Do(req)
135137
if err != nil {
136138
return nil, errors.Join(err, errors.New("failed to send the request"))

internal/agents/codex_oauth.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"os/exec"
1414
"runtime"
1515
"time"
16+
17+
"pinata/internal/httpclient"
1618
)
1719

1820
const (
@@ -91,7 +93,7 @@ func exchangeCodexToken(code, verifier string) (*codexTokenResponse, error) {
9193
"client_id": {codexClientID},
9294
"code_verifier": {verifier},
9395
}
94-
resp, err := http.PostForm(codexTokenURL, params)
96+
resp, err := httpclient.Client.PostForm(codexTokenURL, params)
9597
if err != nil {
9698
return nil, fmt.Errorf("token exchange request failed: %w", err)
9799
}

internal/agents/feedback.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import (
77
"fmt"
88
"io"
99
"net/http"
10+
1011
"pinata/internal/common"
1112
"pinata/internal/config"
13+
"pinata/internal/httpclient"
1214
)
1315

1416
// buildFeedbackURL constructs the full URL for the feedback endpoint.
@@ -42,7 +44,7 @@ func SubmitFeedback(message string) error {
4244
req.Header.Set("Authorization", "Bearer "+string(jwt))
4345
req.Header.Set("Content-Type", "application/json")
4446

45-
client := &http.Client{}
47+
client := httpclient.Client
4648
resp, err := client.Do(req)
4749
if err != nil {
4850
return errors.Join(err, errors.New("failed to send the request"))

internal/agents/secrets.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import (
77
"fmt"
88
"io"
99
"net/http"
10+
1011
"pinata/internal/common"
1112
"pinata/internal/config"
13+
"pinata/internal/httpclient"
1214
)
1315

1416
// buildSecretsURL constructs the full URL for a Secrets API endpoint
@@ -41,7 +43,7 @@ func doSecretsRequest(method, path string, body interface{}) (*http.Response, er
4143
req.Header.Set("Authorization", "Bearer "+string(jwt))
4244
req.Header.Set("Content-Type", "application/json")
4345

44-
client := &http.Client{}
46+
client := httpclient.Client
4547
resp, err := client.Do(req)
4648
if err != nil {
4749
return nil, errors.Join(err, errors.New("failed to send the request"))

internal/agents/skills.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import (
77
"fmt"
88
"io"
99
"net/http"
10+
1011
"pinata/internal/common"
1112
"pinata/internal/config"
13+
"pinata/internal/httpclient"
1214
)
1315

1416
// buildSkillsURL constructs the full URL for a Skills API endpoint
@@ -41,7 +43,7 @@ func doSkillsRequest(method, path string, body interface{}) (*http.Response, err
4143
req.Header.Set("Authorization", "Bearer "+string(jwt))
4244
req.Header.Set("Content-Type", "application/json")
4345

44-
client := &http.Client{}
46+
client := httpclient.Client
4547
resp, err := client.Do(req)
4648
if err != nil {
4749
return nil, errors.Join(err, errors.New("failed to send the request"))

internal/auth/auth.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import (
66
"net/http"
77
"os"
88
"path/filepath"
9+
"time"
10+
911
"pinata/internal/config"
1012
"pinata/internal/gateways"
13+
"pinata/internal/httpclient"
1114
"pinata/internal/utils"
12-
"time"
1315
)
1416

1517
func SaveJWT() error {
@@ -39,9 +41,7 @@ func SaveJWT() error {
3941

4042
req.Header.Set("Authorization", "Bearer "+jwt)
4143

42-
client := &http.Client{
43-
Timeout: time.Duration(time.Second * 3),
44-
}
44+
client := httpclient.WithTimeout(time.Second * 3)
4545
resp, err := client.Do(req)
4646
if err != nil {
4747
return err

internal/files/files.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import (
66
"errors"
77
"fmt"
88
"net/http"
9+
"strings"
10+
911
"pinata/internal/common"
1012
"pinata/internal/config"
1113
"pinata/internal/gateways"
14+
"pinata/internal/httpclient"
1215
"pinata/internal/types"
13-
"strings"
1416
)
1517

1618
func DeleteFile(id string, network string) error {
@@ -32,7 +34,7 @@ func DeleteFile(id string, network string) error {
3234
req.Header.Set("Authorization", "Bearer "+string(jwt))
3335
req.Header.Set("content-type", "application/json")
3436

35-
client := &http.Client{}
37+
client := httpclient.Client
3638
resp, err := client.Do(req)
3739
if err != nil {
3840
return errors.Join(err, errors.New("failed to send the request"))
@@ -69,7 +71,7 @@ func GetFile(id string, network string) (types.GetFileResponse, error) {
6971
req.Header.Set("Authorization", "Bearer "+string(jwt))
7072
req.Header.Set("content-type", "application/json")
7173

72-
client := &http.Client{}
74+
client := httpclient.Client
7375
resp, err := client.Do(req)
7476
if err != nil {
7577
return types.GetFileResponse{}, errors.Join(err, errors.New("failed to send the request"))
@@ -125,7 +127,7 @@ func UpdateFile(id string, name string, network string) (types.GetFileResponse,
125127
req.Header.Set("Authorization", "Bearer "+string(jwt))
126128
req.Header.Set("content-type", "application/json")
127129

128-
client := &http.Client{}
130+
client := httpclient.Client
129131
resp, err := client.Do(req)
130132
if err != nil {
131133
return types.GetFileResponse{}, errors.Join(err, errors.New("failed to send the request"))
@@ -212,7 +214,7 @@ func ListFiles(amount string, pageToken string, cidPending bool, name string, ci
212214
req.Header.Set("Authorization", "Bearer "+string(jwt))
213215
req.Header.Set("content-type", "application/json")
214216

215-
client := &http.Client{}
217+
client := httpclient.Client
216218
resp, err := client.Do(req)
217219
if err != nil {
218220
return types.ListResponse{}, errors.Join(err, errors.New("failed to send the request"))
@@ -274,7 +276,7 @@ func GetSwapHistory(cid string, domain string, network string) (types.GetSwapHis
274276
req.Header.Set("Authorization", "Bearer "+string(jwt))
275277
req.Header.Set("content-type", "application/json")
276278

277-
client := &http.Client{}
279+
client := httpclient.Client
278280
resp, err := client.Do(req)
279281
if err != nil {
280282
return types.GetSwapHistoryResponse{}, errors.Join(err, errors.New("failed to send the request"))
@@ -332,7 +334,7 @@ func AddSwap(cid string, swapCid string, network string) (types.AddSwapResponse,
332334
req.Header.Set("Authorization", "Bearer "+string(jwt))
333335
req.Header.Set("content-type", "application/json")
334336

335-
client := &http.Client{}
337+
client := httpclient.Client
336338
resp, err := client.Do(req)
337339
if err != nil {
338340
return types.AddSwapResponse{}, errors.Join(err, errors.New("failed to send the request"))
@@ -379,7 +381,7 @@ func RemoveSwap(cid string, network string) error {
379381
req.Header.Set("Authorization", "Bearer "+string(jwt))
380382
req.Header.Set("content-type", "application/json")
381383

382-
client := &http.Client{}
384+
client := httpclient.Client
383385
resp, err := client.Do(req)
384386
if err != nil {
385387
return errors.Join(err, errors.New("failed to send the request"))

0 commit comments

Comments
 (0)