Skip to content

Commit ce25e31

Browse files
authored
cleanup Go Client implementation (#321)
1 parent 53669cd commit ce25e31

File tree

2 files changed

+27
-36
lines changed

2 files changed

+27
-36
lines changed

go/client.go

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ type Client struct {
7070
sessions map[string]*Session
7171
sessionsMux sync.Mutex
7272
isExternalServer bool
73-
conn any // stores net.Conn for external TCP connections
74-
useStdio bool // resolved value from options
75-
autoStart bool // resolved value from options
76-
autoRestart bool // resolved value from options
73+
conn net.Conn // stores net.Conn for external TCP connections
74+
useStdio bool // resolved value from options
75+
autoStart bool // resolved value from options
76+
autoRestart bool // resolved value from options
7777
modelsCache []ModelInfo
7878
modelsCacheMux sync.Mutex
7979
}
@@ -187,29 +187,26 @@ func NewClient(options *ClientOptions) *Client {
187187
// Panics if the URL format is invalid or the port is out of range.
188188
func parseCliUrl(url string) (string, int) {
189189
// Remove protocol if present
190-
cleanUrl := regexp.MustCompile(`^https?://`).ReplaceAllString(url, "")
191-
192-
// Check if it's just a port number
193-
if matched, _ := regexp.MatchString(`^\d+$`, cleanUrl); matched {
194-
port, err := strconv.Atoi(cleanUrl)
195-
if err != nil || port <= 0 || port > 65535 {
196-
panic(fmt.Sprintf("Invalid port in CLIUrl: %s", url))
197-
}
198-
return "localhost", port
199-
}
200-
201-
// Parse host:port format
202-
parts := regexp.MustCompile(`:`).Split(cleanUrl, 2)
203-
if len(parts) != 2 {
204-
panic(fmt.Sprintf("Invalid CLIUrl format: %s. Expected 'host:port', 'http://host:port', or 'port'", url))
190+
cleanUrl, _ := strings.CutPrefix(url, "https://")
191+
cleanUrl, _ = strings.CutPrefix(cleanUrl, "http://")
192+
193+
// Parse host:port or port format
194+
var host string
195+
var portStr string
196+
if before, after, found := strings.Cut(cleanUrl, ":"); found {
197+
host = before
198+
portStr = after
199+
} else {
200+
// Only port provided
201+
portStr = before
205202
}
206203

207-
host := parts[0]
208204
if host == "" {
209205
host = "localhost"
210206
}
211207

212-
port, err := strconv.Atoi(parts[1])
208+
// Validate port
209+
port, err := strconv.Atoi(portStr)
213210
if err != nil || port <= 0 || port > 65535 {
214211
panic(fmt.Sprintf("Invalid port in CLIUrl: %s", url))
215212
}
@@ -312,10 +309,8 @@ func (c *Client) Stop() []error {
312309

313310
// Close external TCP connection if exists
314311
if c.isExternalServer && c.conn != nil {
315-
if closer, ok := c.conn.(interface{ Close() error }); ok {
316-
if err := closer.Close(); err != nil {
317-
errors = append(errors, fmt.Errorf("failed to close socket: %w", err))
318-
}
312+
if err := c.conn.Close(); err != nil {
313+
errors = append(errors, fmt.Errorf("failed to close socket: %w", err))
319314
}
320315
c.conn = nil
321316
}
@@ -375,9 +370,7 @@ func (c *Client) ForceStop() {
375370

376371
// Close external TCP connection if exists
377372
if c.isExternalServer && c.conn != nil {
378-
if closer, ok := c.conn.(interface{ Close() error }); ok {
379-
closer.Close() // Ignore errors
380-
}
373+
_ = c.conn.Close() // Ignore errors
381374
c.conn = nil
382375
}
383376

@@ -1329,18 +1322,16 @@ func (c *Client) executeToolCall(
13291322

13301323
defer func() {
13311324
if r := recover(); r != nil {
1332-
fmt.Printf("Tool handler panic (%s): %v\n", toolName, r)
13331325
result = buildFailedToolResult(fmt.Sprintf("tool panic: %v", r))
13341326
}
13351327
}()
13361328

1337-
var err error
13381329
if handler != nil {
1330+
var err error
13391331
result, err = handler(invocation)
1340-
}
1341-
1342-
if err != nil {
1343-
return buildFailedToolResult(err.Error())
1332+
if err != nil {
1333+
result = buildFailedToolResult(err.Error())
1334+
}
13441335
}
13451336

13461337
return result

go/client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ func TestClient_URLParsing(t *testing.T) {
118118
if r := recover(); r == nil {
119119
t.Error("Expected panic for invalid URL format")
120120
} else {
121-
matched, _ := regexp.MatchString("Invalid CLIUrl format", r.(string))
121+
matched, _ := regexp.MatchString("Invalid port in CLIUrl", r.(string))
122122
if !matched {
123-
t.Errorf("Expected panic message to contain 'Invalid CLIUrl format', got: %v", r)
123+
t.Errorf("Expected panic message to contain 'Invalid port in CLIUrl', got: %v", r)
124124
}
125125
}
126126
}()

0 commit comments

Comments
 (0)