Match the first-party chat footer on touch screens: after a turn ends, its last assistant message gets a row with the model name (resolved from /api/model with the id as fallback), the agent and how long the turn took, plus a "more" menu. The menu shows the completion timestamp and offers copy answer and fork from here; forking copies history before the next prompt (or the whole history at the end) and opens the new session. Bump version to 0.1.7 (build 8).
291 lines
8.4 KiB
Go
291 lines
8.4 KiB
Go
package ocapi
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
)
|
|
|
|
// ListSessionsOptions configures ListSessions.
|
|
type ListSessionsOptions struct {
|
|
// Directory scopes the list to one absolute path. Empty means all
|
|
// projects (each Session carries its own Location).
|
|
Directory string
|
|
Limit int
|
|
Cursor string
|
|
Order string // "asc" or "desc"
|
|
Search string
|
|
ParentID string
|
|
Archived *bool
|
|
}
|
|
|
|
// ListSessions lists sessions, newest first by default.
|
|
func (c *Client) ListSessions(ctx context.Context, o ListSessionsOptions) (SessionPage, error) {
|
|
q := url.Values{}
|
|
if o.Limit > 0 {
|
|
q.Set("limit", strconv.Itoa(o.Limit))
|
|
}
|
|
if o.Directory != "" {
|
|
q.Set("directory", o.Directory)
|
|
}
|
|
if o.Cursor != "" {
|
|
q.Set("cursor", o.Cursor)
|
|
}
|
|
if o.Order != "" {
|
|
q.Set("order", o.Order)
|
|
}
|
|
if o.Search != "" {
|
|
q.Set("search", o.Search)
|
|
}
|
|
if o.ParentID != "" {
|
|
q.Set("parentID", o.ParentID)
|
|
}
|
|
if o.Archived != nil {
|
|
q.Set("archived", strconv.FormatBool(*o.Archived))
|
|
}
|
|
var page SessionPage
|
|
err := c.do(ctx, http.MethodGet, "/api/session", requestOptions{directory: o.Directory, query: q}, nil, &page)
|
|
return page, err
|
|
}
|
|
|
|
// ActiveSessions returns the sessions running right now, keyed by id.
|
|
func (c *Client) ActiveSessions(ctx context.Context) (map[string]string, error) {
|
|
var payload struct {
|
|
Data map[string]struct {
|
|
Type string `json:"type"`
|
|
} `json:"data"`
|
|
}
|
|
if err := c.do(ctx, http.MethodGet, "/api/session/active", requestOptions{}, nil, &payload); err != nil {
|
|
return nil, err
|
|
}
|
|
out := make(map[string]string, len(payload.Data))
|
|
for id, v := range payload.Data {
|
|
out[id] = v.Type
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// GetSession reads one session.
|
|
func (c *Client) GetSession(ctx context.Context, id, directory string) (Session, error) {
|
|
var payload struct {
|
|
Data Session `json:"data"`
|
|
}
|
|
err := c.do(ctx, http.MethodGet, "/api/session/"+url.PathEscape(id), requestOptions{directory: directory}, nil, &payload)
|
|
return payload.Data, err
|
|
}
|
|
|
|
// CreateSessionOptions configures CreateSession.
|
|
type CreateSessionOptions struct {
|
|
Directory string
|
|
Title string
|
|
Agent string
|
|
Model *ModelRef
|
|
}
|
|
|
|
// CreateSession makes a new session, optionally in a directory.
|
|
func (c *Client) CreateSession(ctx context.Context, o CreateSessionOptions) (Session, error) {
|
|
body := map[string]any{}
|
|
if o.Title != "" {
|
|
body["title"] = o.Title
|
|
}
|
|
if o.Agent != "" {
|
|
body["agent"] = o.Agent
|
|
}
|
|
if o.Model != nil {
|
|
body["model"] = o.Model
|
|
}
|
|
if o.Directory != "" {
|
|
body["location"] = map[string]string{"directory": o.Directory}
|
|
}
|
|
var payload struct {
|
|
Data Session `json:"data"`
|
|
}
|
|
err := c.do(ctx, http.MethodPost, "/api/session", requestOptions{directory: o.Directory}, body, &payload)
|
|
return payload.Data, err
|
|
}
|
|
|
|
// SessionPatch is PATCH /api/session/{id}: both fields are optional.
|
|
type SessionPatch struct {
|
|
Title *string `json:"title,omitempty"`
|
|
Archived *bool `json:"archived,omitempty"`
|
|
}
|
|
|
|
// UpdateSession renames or archives one session.
|
|
func (c *Client) UpdateSession(ctx context.Context, id, directory string, patch SessionPatch) error {
|
|
return c.do(ctx, http.MethodPatch, "/api/session/"+url.PathEscape(id), requestOptions{directory: directory}, patch, nil)
|
|
}
|
|
|
|
// DeleteSession removes a session and its children.
|
|
func (c *Client) DeleteSession(ctx context.Context, id, directory string) error {
|
|
return c.do(ctx, http.MethodDelete, "/api/session/"+url.PathEscape(id), requestOptions{directory: directory}, nil, nil)
|
|
}
|
|
|
|
// ListMessagesOptions configures ListMessages.
|
|
type ListMessagesOptions struct {
|
|
Directory string
|
|
Limit int
|
|
Cursor string
|
|
Order string // "asc" or "desc"
|
|
}
|
|
|
|
// ListMessages reads one page of a session's messages. Newest first by
|
|
// default, which is what a chat pages backwards with.
|
|
func (c *Client) ListMessages(ctx context.Context, id string, o ListMessagesOptions) (MessagePage, error) {
|
|
q := url.Values{}
|
|
if o.Limit > 0 {
|
|
q.Set("limit", strconv.Itoa(o.Limit))
|
|
}
|
|
if o.Cursor != "" {
|
|
q.Set("cursor", o.Cursor)
|
|
}
|
|
if o.Order != "" {
|
|
q.Set("order", o.Order)
|
|
}
|
|
var page MessagePage
|
|
err := c.do(ctx, http.MethodGet, "/api/session/"+url.PathEscape(id)+"/message",
|
|
requestOptions{directory: o.Directory, query: q}, nil, &page)
|
|
return page, err
|
|
}
|
|
|
|
// ModelInfo is one model the server offers.
|
|
type ModelInfo struct {
|
|
ID string `json:"id"`
|
|
ProviderID string `json:"providerID"`
|
|
Name string `json:"name"`
|
|
Variant string `json:"variant"`
|
|
}
|
|
|
|
// Models lists the models the server offers for a directory.
|
|
func (c *Client) Models(ctx context.Context, directory string) ([]ModelInfo, error) {
|
|
q := url.Values{}
|
|
if directory != "" {
|
|
q.Set("location[directory]", directory)
|
|
}
|
|
var payload struct {
|
|
Data []ModelInfo `json:"data"`
|
|
}
|
|
if err := c.do(ctx, http.MethodGet, "/api/model", requestOptions{query: q}, nil, &payload); err != nil {
|
|
return nil, err
|
|
}
|
|
return payload.Data, nil
|
|
}
|
|
|
|
// ProviderInfo is one provider the server offers, with its display name.
|
|
type ProviderInfo struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
// Providers lists the providers available for a directory.
|
|
func (c *Client) Providers(ctx context.Context, directory string) ([]ProviderInfo, error) {
|
|
q := url.Values{}
|
|
if directory != "" {
|
|
q.Set("location[directory]", directory)
|
|
}
|
|
var payload struct {
|
|
Data []ProviderInfo `json:"data"`
|
|
}
|
|
if err := c.do(ctx, http.MethodGet, "/api/provider", requestOptions{query: q}, nil, &payload); err != nil {
|
|
return nil, err
|
|
}
|
|
return payload.Data, nil
|
|
}
|
|
|
|
// ServerLocation is the server's default working directory.
|
|
type ServerLocation struct {
|
|
Directory string `json:"directory"`
|
|
ProjectID string `json:"projectID"`
|
|
}
|
|
|
|
// Location reads the server's default location, which a new session uses
|
|
// when the user does not pick one.
|
|
func (c *Client) Location(ctx context.Context) (ServerLocation, error) {
|
|
var payload struct {
|
|
Directory string `json:"directory"`
|
|
Project struct {
|
|
ID string `json:"id"`
|
|
} `json:"project"`
|
|
}
|
|
if err := c.do(ctx, http.MethodGet, "/api/location", requestOptions{}, nil, &payload); err != nil {
|
|
return ServerLocation{}, err
|
|
}
|
|
return ServerLocation{Directory: payload.Directory, ProjectID: payload.Project.ID}, nil
|
|
}
|
|
|
|
// AgentInfo is one entry of GET /api/agent.
|
|
type AgentInfo struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
Mode string `json:"mode"`
|
|
Hidden bool `json:"hidden"`
|
|
}
|
|
|
|
// Agents lists the agents available for a directory.
|
|
func (c *Client) Agents(ctx context.Context, directory string) ([]AgentInfo, error) {
|
|
q := url.Values{}
|
|
if directory != "" {
|
|
q.Set("location[directory]", directory)
|
|
}
|
|
var payload struct {
|
|
Data []AgentInfo `json:"data"`
|
|
}
|
|
if err := c.do(ctx, http.MethodGet, "/api/agent", requestOptions{query: q}, nil, &payload); err != nil {
|
|
return nil, err
|
|
}
|
|
return payload.Data, nil
|
|
}
|
|
|
|
// PermissionRequest is a pending tool permission.
|
|
type PermissionRequest struct {
|
|
ID string `json:"id"`
|
|
SessionID string `json:"sessionID"`
|
|
Action string `json:"action"`
|
|
Resources []string `json:"resources"`
|
|
Message string `json:"message,omitempty"`
|
|
}
|
|
|
|
// PendingPermissions lists the pending permission requests for a directory.
|
|
func (c *Client) PendingPermissions(ctx context.Context, directory string) ([]PermissionRequest, error) {
|
|
q := url.Values{}
|
|
if directory != "" {
|
|
q.Set("location[directory]", directory)
|
|
}
|
|
var payload struct {
|
|
Data []PermissionRequest `json:"data"`
|
|
}
|
|
if err := c.do(ctx, http.MethodGet, "/api/permission/request", requestOptions{query: q}, nil, &payload); err != nil {
|
|
return nil, err
|
|
}
|
|
return payload.Data, nil
|
|
}
|
|
|
|
// PermissionDecision is how a permission request is answered.
|
|
type PermissionDecision string
|
|
|
|
const (
|
|
PermissionOnce PermissionDecision = "once"
|
|
PermissionAlways PermissionDecision = "always"
|
|
PermissionReject PermissionDecision = "reject"
|
|
)
|
|
|
|
// ReplyPermission answers a pending permission request.
|
|
func (c *Client) ReplyPermission(ctx context.Context, sessionID, requestID string, decision PermissionDecision, message string) error {
|
|
body := map[string]any{"decision": decision}
|
|
if message != "" {
|
|
body["message"] = message
|
|
}
|
|
path := "/api/session/" + url.PathEscape(sessionID) + "/permission/" + url.PathEscape(requestID) + "/reply"
|
|
return c.do(ctx, http.MethodPost, path, requestOptions{}, body, nil)
|
|
}
|
|
|
|
// EventData unmarshals an event's payload into v.
|
|
func (e Event) UnmarshalData(v any) error {
|
|
if len(e.Data) == 0 {
|
|
return nil
|
|
}
|
|
return json.Unmarshal(e.Data, v)
|
|
}
|