Restrict chat_sessions to server-owned sessions
chat_sessions now brokers GET /api/session but filters the global list to the sessions this server recorded in run_steps, deduped by session id, and fails closed to an empty list when no ownership source is configured. A session another client holds (in-server claim, released on stream end or disconnect) or an active run is marked busy; a second client's chat_prompt for a held session is rejected before any OpenCode call. chat_create stays unbrokered.
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
@@ -109,6 +110,22 @@ func (c *Chat) Messages(ctx context.Context, id string, limit int) ([]protocol.C
|
||||
return decodeChatMessages(id, data)
|
||||
}
|
||||
|
||||
// Sessions lists the OpenCode sessions on the endpoint. On the V2 HTTP API that
|
||||
// is GET /api/session; the native v1 serve translation maps it to GET /session.
|
||||
// It decodes the verified {"data":[...]} envelope (tolerating a bare array like
|
||||
// chatMessageRecords), lifts the nested location.directory and
|
||||
// time{created,updated} into protocol.ChatSession, drops records without a valid
|
||||
// session id, and orders newest-updated first so the list is deterministic. The
|
||||
// service's list is global; the caller is responsible for restricting it to the
|
||||
// sessions this server owns.
|
||||
func (c *Chat) Sessions(ctx context.Context) ([]protocol.ChatSession, error) {
|
||||
data, err := c.call(ctx, "get", "/api/session", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return decodeChatSessions(data)
|
||||
}
|
||||
|
||||
// Reply posts a permission decision, mirroring the documented endpoint.
|
||||
func (c *Chat) Reply(ctx context.Context, request protocol.ChatReplyRequest) error {
|
||||
if !sessionID.MatchString(request.SessionID) {
|
||||
@@ -548,3 +565,72 @@ func decodeChatMessages(sessionID string, data []byte) ([]protocol.ChatMessage,
|
||||
}
|
||||
return messages, nil
|
||||
}
|
||||
|
||||
// nativeChatSession is one listed session in the verified V2 shape
|
||||
// ({id,title,agent,model,cost,tokens,outcome,time,title,location.directory,
|
||||
// parentID,permissions}); only the fields the protocol surfaces are read. The
|
||||
// native v1 serve readback of the same list is not covered by the research, so
|
||||
// its records decode through the same tolerant envelope but any additional
|
||||
// nesting is not asserted.
|
||||
type nativeChatSession struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Agent string `json:"agent"`
|
||||
Model nativeChatModel `json:"model"`
|
||||
ParentID string `json:"parentID"`
|
||||
Location struct {
|
||||
Directory string `json:"directory"`
|
||||
} `json:"location"`
|
||||
Time struct {
|
||||
Created int64 `json:"created"`
|
||||
Updated int64 `json:"updated"`
|
||||
} `json:"time"`
|
||||
}
|
||||
|
||||
// chatSessionRecords accepts both session-list envelopes: the V2 HTTP API wraps
|
||||
// them in {"data":[...]} while the native serve readback returns a bare array.
|
||||
// An unrecognised body is an error, never an empty list.
|
||||
func chatSessionRecords(data []byte) ([]nativeChatSession, error) {
|
||||
if records, ok := chatEnvelope[nativeChatSession](data); ok {
|
||||
return records, nil
|
||||
}
|
||||
var records []nativeChatSession
|
||||
if err := json.Unmarshal(data, &records); err != nil {
|
||||
return nil, fmt.Errorf("invalid OpenCode session response")
|
||||
}
|
||||
return records, nil
|
||||
}
|
||||
|
||||
// decodeChatSessions maps the listed sessions onto protocol.ChatSession, dropping
|
||||
// every record whose id is not a valid OpenCode session id (an empty or foreign
|
||||
// id cannot be addressed) and ordering by UpdatedAt then id so the result is
|
||||
// deterministic.
|
||||
func decodeChatSessions(data []byte) ([]protocol.ChatSession, error) {
|
||||
records, err := chatSessionRecords(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sessions := make([]protocol.ChatSession, 0, len(records))
|
||||
for _, record := range records {
|
||||
if !sessionID.MatchString(record.ID) {
|
||||
continue
|
||||
}
|
||||
sessions = append(sessions, protocol.ChatSession{
|
||||
ID: record.ID,
|
||||
Title: record.Title,
|
||||
Agent: record.Agent,
|
||||
Model: record.Model.ID,
|
||||
Directory: record.Location.Directory,
|
||||
ParentID: record.ParentID,
|
||||
CreatedAt: record.Time.Created,
|
||||
UpdatedAt: record.Time.Updated,
|
||||
})
|
||||
}
|
||||
sort.SliceStable(sessions, func(i, j int) bool {
|
||||
if sessions[i].UpdatedAt != sessions[j].UpdatedAt {
|
||||
return sessions[i].UpdatedAt > sessions[j].UpdatedAt
|
||||
}
|
||||
return sessions[i].ID < sessions[j].ID
|
||||
})
|
||||
return sessions, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user