WIP preserve large chat history recovery

This commit is contained in:
2026-10-18 15:03:18 +01:00
parent f91145001a
commit 998da0e7af
21 changed files with 1088 additions and 75 deletions
+45
View File
@@ -321,6 +321,51 @@ func (c *Connection) Native(ctx context.Context, method, path string, body any)
return c.nativeRequest(ctx, method, path, body, c.Directory)
}
// HistoryNative performs an authenticated history read without Connection.Call's
// general 1 MiB response guard. Chat history is paged and projected before it is
// sent to a client; ordinary API calls keep their existing safeguard.
func (c *Connection) HistoryNative(ctx context.Context, path string) (io.ReadCloser, error) {
c.LastHTTPStatus = 0
c.LastHTTPOperation = "GET " + path
u, err := url.Parse(c.URL + path)
if err != nil {
return nil, err
}
if c.Directory != "" {
query := u.Query()
query.Set("directory", c.Directory)
u.RawQuery = query.Encode()
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
req.SetBasicAuth(c.serverUser(), c.password)
resp, err := http.DefaultClient.Do(req)
if err != nil {
if ctx.Err() != nil {
return nil, ctx.Err()
}
return nil, fmt.Errorf("OpenCode history request failed")
}
c.LastHTTPStatus = resp.StatusCode
c.LastHTTPOperation = "GET " + path
if resp.StatusCode == http.StatusUnauthorized {
c.unauthorized = true
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
defer resp.Body.Close()
_, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, 16<<10))
return nil, fmt.Errorf("OpenCode GET %s failed (HTTP %d)", path, resp.StatusCode)
}
if !strings.HasPrefix(resp.Header.Get("Content-Type"), "application/json") {
resp.Body.Close()
return nil, fmt.Errorf("OpenCode GET %s returned a non-JSON API response", path)
}
return resp.Body, nil
}
// nativeRequest is the shared Native request path. An empty directory omits the
// ?directory= query parameter; the V2 session list then returns sessions across
// every directory instead of only the connection's own.