2026-11-16 09:09:09 +08:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 18:05:15 +08:00
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 09:09:09 +08:00
|
|
|
// 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)
|
|
|
|
|
}
|