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).
65 lines
2.3 KiB
Go
65 lines
2.3 KiB
Go
package ocapi
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
// PromptOptions configures Prompt.
|
|
type PromptOptions struct {
|
|
Directory string
|
|
// MessageID is an optional client-minted id (msg_…), which makes a
|
|
// retry after a lost response a no-op instead of a duplicate.
|
|
MessageID string
|
|
Text string
|
|
}
|
|
|
|
// Prompt sends a user message to a session. The reply arrives through the
|
|
// event stream, not this call.
|
|
func (c *Client) Prompt(ctx context.Context, sessionID string, o PromptOptions) error {
|
|
body := map[string]any{"text": o.Text}
|
|
if o.MessageID != "" {
|
|
body["id"] = o.MessageID
|
|
}
|
|
path := "/api/session/" + url.PathEscape(sessionID) + "/prompt"
|
|
return c.do(ctx, http.MethodPost, path, requestOptions{directory: o.Directory}, body, nil)
|
|
}
|
|
|
|
// ForkSession copies the session's history before a message into a new
|
|
// session. An empty before copies the whole history.
|
|
func (c *Client) ForkSession(ctx context.Context, sessionID, directory, before string) (Session, error) {
|
|
body := map[string]any{}
|
|
if before != "" {
|
|
body["before"] = before
|
|
}
|
|
var payload struct {
|
|
Data Session `json:"data"`
|
|
}
|
|
path := "/api/session/" + url.PathEscape(sessionID) + "/fork"
|
|
err := c.do(ctx, http.MethodPost, path, requestOptions{directory: directory}, body, &payload)
|
|
return payload.Data, err
|
|
}
|
|
|
|
// Interrupt stops the turn the session is running.
|
|
func (c *Client) Interrupt(ctx context.Context, sessionID, directory string) (bool, error) {
|
|
var payload struct {
|
|
Interrupted bool `json:"interrupted"`
|
|
}
|
|
path := "/api/session/" + url.PathEscape(sessionID) + "/interrupt"
|
|
err := c.do(ctx, http.MethodPost, path, requestOptions{directory: directory}, map[string]any{}, &payload)
|
|
return payload.Interrupted, err
|
|
}
|
|
|
|
// SwitchModel changes the model later turns run on.
|
|
func (c *Client) SwitchModel(ctx context.Context, sessionID, directory string, model ModelRef) error {
|
|
path := "/api/session/" + url.PathEscape(sessionID) + "/model"
|
|
return c.do(ctx, http.MethodPost, path, requestOptions{directory: directory}, map[string]any{"model": model}, nil)
|
|
}
|
|
|
|
// SwitchAgent changes the agent later turns run with.
|
|
func (c *Client) SwitchAgent(ctx context.Context, sessionID, directory, agent string) error {
|
|
path := "/api/session/" + url.PathEscape(sessionID) + "/agent"
|
|
return c.do(ctx, http.MethodPost, path, requestOptions{directory: directory}, map[string]any{"agent": agent}, nil)
|
|
}
|