feat: bootstrap OpenChamber native Android client
Add a Go client for OpenChamber built with Asuka's native widgets. - internal/ocapi: health, password-to-device-token auth, session and message paging, prompt/interrupt/model/agent, permissions, SSE events, HTTP/SOCKS5 proxies and certificate pinning - internal/storage: connection settings plus a device token in the platform keystore, with a per-install id for server-side dedupe - internal/appui: connect, session list and chat screens with streaming Markdown, reasoning/tool folding, permission cards and session actions - netcfg: native module that enables cleartext HTTP in release builds - cmd/ocprobe: terminal probe for the API - headless UI tests (uitest) and the research/plan document
This commit is contained in:
commit
254d08b1bd
35 files changed
+4922
No files matched your search
@@ -0,0 +1,249 @@
|
||||
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
|
||||
}
|
||||
|
||||
// ModelSelection lists the models the server offers for a directory.
|
||||
func (c *Client) Models(ctx context.Context, directory string) ([]ModelRef, error) {
|
||||
q := url.Values{}
|
||||
if directory != "" {
|
||||
q.Set("location[directory]", directory)
|
||||
}
|
||||
var payload struct {
|
||||
Data []struct {
|
||||
ID string `json:"id"`
|
||||
ProviderID string `json:"providerID"`
|
||||
Name string `json:"name"`
|
||||
Variant string `json:"variant"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := c.do(ctx, http.MethodGet, "/api/model", requestOptions{query: q}, nil, &payload); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]ModelRef, 0, len(payload.Data))
|
||||
for _, m := range payload.Data {
|
||||
out = append(out, ModelRef{ID: m.ID, ProviderID: m.ProviderID, Variant: m.Variant})
|
||||
}
|
||||
return out, 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)
|
||||
}
|
||||
Reference in new issue
Block a user