fix(opencode): x-opencode-directory, Content-Length, warmup /api/model; serve без явного Env
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
@@ -24,10 +23,11 @@ import (
|
||||
// Prompt не блокирует: вердикт собирается поллингом из content[].type=="text"
|
||||
// новых assistant-сообщений (см. Runner.awaitVerdict).
|
||||
type Client struct {
|
||||
BaseURL string // http://host:port (без завершающего слеша)
|
||||
Password string // basic auth (username "opencode")
|
||||
Debug bool // включать отладочные логи API-вызовов (log.level=debug)
|
||||
http *http.Client // единый клиент: все операции быстрые (нет блокирующего Send)
|
||||
BaseURL string // http://host:port (без завершающего слеша)
|
||||
Password string // basic auth (username "opencode")
|
||||
Directory string // каталог сервера — идёт заголовком x-opencode-directory
|
||||
Debug bool // включать отладочные логи API-вызовов (log.level=debug)
|
||||
http *http.Client // единый клиент: все операции быстрые (нет блокирующего Send)
|
||||
}
|
||||
|
||||
// ClientErr — классы ошибок клиента.
|
||||
@@ -56,14 +56,15 @@ func (c *Client) do(ctx context.Context, method, path, op string, body []byte) (
|
||||
if err != nil {
|
||||
return nil, &ClientErr{Op: "connect", Err: err}
|
||||
}
|
||||
if body != nil {
|
||||
req.ContentLength = int64(len(body))
|
||||
}
|
||||
if c.Password != "" {
|
||||
req.SetBasicAuth("opencode", c.Password)
|
||||
}
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
if c.Debug {
|
||||
log.Printf("opencode api debug: %s -> %s %s%s", op, method, c.BaseURL, path)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if c.Directory != "" {
|
||||
req.Header.Set("x-opencode-directory", c.Directory)
|
||||
}
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
@@ -75,14 +76,8 @@ func (c *Client) do(ctx context.Context, method, path, op string, body []byte) (
|
||||
return nil, &ClientErr{Op: "connect", Err: err}
|
||||
}
|
||||
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
||||
if c.Debug {
|
||||
log.Printf("opencode api debug: %s response: status %d", op, resp.StatusCode)
|
||||
}
|
||||
return nil, &ClientErr{Op: op, Err: fmt.Errorf("status %d: %s", resp.StatusCode, truncateStr(string(b), 300))}
|
||||
}
|
||||
if c.Debug {
|
||||
log.Printf("opencode api debug: %s response (%d bytes)", op, len(b))
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
@@ -130,6 +125,12 @@ func (c *Client) CreateSession(ctx context.Context, agent string) (string, error
|
||||
if out.Data.ID == "" {
|
||||
return "", &ClientErr{Op: "create", Err: fmt.Errorf("пустой id сессии")}
|
||||
}
|
||||
time.Sleep(2 * time.Second)
|
||||
// Холостой вызов /api/model: прогревает сервер (модель/провайдеры),
|
||||
// чтобы первый промпт не спотыкался о «тёплый» старт.
|
||||
_, _ = c.do(ctx, http.MethodGet, "/api/model", "models-warmup", nil)
|
||||
time.Sleep(2 * time.Second)
|
||||
_, _ = c.do(ctx, http.MethodGet, "/api/model", "models-warmup", nil)
|
||||
return out.Data.ID, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user