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
220 lines
7.5 KiB
Go
220 lines
7.5 KiB
Go
// Package ocapi is a small HTTP client for the OpenChamber server API
|
|
// (which proxies OpenCode v2 under /api/*). It has no UI dependencies, so
|
|
// it can be tested with httptest and reused by the CLI probe.
|
|
package ocapi
|
|
|
|
import "encoding/json"
|
|
|
|
// Health is GET /health.
|
|
type Health struct {
|
|
Status string `json:"status"`
|
|
Timestamp string `json:"timestamp"`
|
|
OpenChamberVersion string `json:"openchamberVersion"`
|
|
Runtime string `json:"runtime"`
|
|
ServerID string `json:"serverId"`
|
|
IsOpenCodeReady bool `json:"isOpenCodeReady"`
|
|
OpenCodePort int `json:"openCodePort"`
|
|
Compatibility struct {
|
|
APIVersion int `json:"apiVersion"`
|
|
MinClientAPIVersion int `json:"minClientApiVersion"`
|
|
Capabilities []string `json:"capabilities"`
|
|
} `json:"compatibility"`
|
|
}
|
|
|
|
// AuthStatus is GET /auth/session.
|
|
type AuthStatus struct {
|
|
Authenticated bool `json:"authenticated"`
|
|
Disabled bool `json:"disabled"`
|
|
Locked bool `json:"locked"`
|
|
Scope string `json:"scope"`
|
|
ClientAuthRequired bool `json:"clientAuthRequired"`
|
|
}
|
|
|
|
// LoginRequest is POST /auth/session. Password is the only secret; the rest
|
|
// names this device so repeated logins collapse into one server-side record.
|
|
type LoginRequest struct {
|
|
Password string `json:"password"`
|
|
TrustDevice bool `json:"trustDevice"`
|
|
IssueClientToken bool `json:"issueClientToken"`
|
|
ClientLabel string `json:"clientLabel,omitempty"`
|
|
ClientKind string `json:"clientKind,omitempty"`
|
|
DeviceName string `json:"deviceName,omitempty"`
|
|
DevicePlatform string `json:"devicePlatform,omitempty"`
|
|
DeviceModel string `json:"deviceModel,omitempty"`
|
|
AppVersion string `json:"appVersion,omitempty"`
|
|
DedupeKey string `json:"dedupeKey,omitempty"`
|
|
}
|
|
|
|
// LoginResult is the answer to POST /auth/session.
|
|
type LoginResult struct {
|
|
Authenticated bool `json:"authenticated"`
|
|
ClientToken string `json:"clientToken"`
|
|
Client json.RawMessage `json:"client"`
|
|
}
|
|
|
|
// ModelRef identifies a model as OpenCode names it.
|
|
type ModelRef struct {
|
|
ID string `json:"id"`
|
|
ProviderID string `json:"providerID"`
|
|
Variant string `json:"variant,omitempty"`
|
|
}
|
|
|
|
// TokenUsage is a session's or message's token counters.
|
|
type TokenUsage struct {
|
|
Input int64 `json:"input"`
|
|
Output int64 `json:"output"`
|
|
Reasoning int64 `json:"reasoning"`
|
|
Cache struct {
|
|
Read int64 `json:"read"`
|
|
Write int64 `json:"write"`
|
|
} `json:"cache"`
|
|
}
|
|
|
|
// Location is where a session lives.
|
|
type Location struct {
|
|
Directory string `json:"directory"`
|
|
ProjectID string `json:"projectID"`
|
|
Subpath string `json:"subpath,omitempty"`
|
|
}
|
|
|
|
// SessionTime carries the millisecond timestamps OpenCode reports.
|
|
type SessionTime struct {
|
|
Created int64 `json:"created"`
|
|
Updated int64 `json:"updated"`
|
|
Busy int64 `json:"busy,omitempty"`
|
|
Idle int64 `json:"idle,omitempty"`
|
|
Viewed int64 `json:"viewed,omitempty"`
|
|
Archived int64 `json:"archived,omitempty"`
|
|
}
|
|
|
|
// Session is one entry of GET /api/session.
|
|
type Session struct {
|
|
ID string `json:"id"`
|
|
ParentID string `json:"parentID,omitempty"`
|
|
ProjectID string `json:"projectID"`
|
|
Title string `json:"title"`
|
|
Agent string `json:"agent,omitempty"`
|
|
Model *ModelRef `json:"model,omitempty"`
|
|
Cost float64 `json:"cost"`
|
|
Tokens *TokenUsage `json:"tokens,omitempty"`
|
|
Time SessionTime `json:"time"`
|
|
Location *Location `json:"location,omitempty"`
|
|
Metadata map[string]any `json:"metadata,omitempty"`
|
|
}
|
|
|
|
// PageCursor is the opaque pagination cursor of session and message lists.
|
|
type PageCursor struct {
|
|
Next string `json:"next,omitempty"`
|
|
Previous string `json:"previous,omitempty"`
|
|
}
|
|
|
|
// SessionPage is GET /api/session.
|
|
type SessionPage struct {
|
|
Data []Session `json:"data"`
|
|
Cursor PageCursor `json:"cursor"`
|
|
}
|
|
|
|
// Message is one entry of GET /api/session/{id}/message. The server's union
|
|
// is wide; fields the client does not need stay zero.
|
|
type Message struct {
|
|
ID string `json:"id"`
|
|
Type string `json:"type"`
|
|
Time MessageTime `json:"time"`
|
|
Agent string `json:"agent,omitempty"`
|
|
Model *ModelRef `json:"model,omitempty"`
|
|
Content []ContentPart `json:"content,omitempty"`
|
|
Text string `json:"text,omitempty"`
|
|
Files []FileAttachment `json:"files,omitempty"`
|
|
Finish string `json:"finish,omitempty"`
|
|
Cost float64 `json:"cost,omitempty"`
|
|
Tokens *TokenUsage `json:"tokens,omitempty"`
|
|
Error json.RawMessage `json:"error,omitempty"`
|
|
|
|
// Shell messages
|
|
Command string `json:"command,omitempty"`
|
|
Status string `json:"status,omitempty"`
|
|
Exit *int `json:"exit,omitempty"`
|
|
Output string `json:"output,omitempty"`
|
|
}
|
|
|
|
// MessageTime carries the millisecond timestamps of a message.
|
|
type MessageTime struct {
|
|
Created int64 `json:"created"`
|
|
Streamed int64 `json:"streamed,omitempty"`
|
|
Completed int64 `json:"completed,omitempty"`
|
|
}
|
|
|
|
// ContentPart is a block of an assistant message.
|
|
type ContentPart struct {
|
|
Type string `json:"type"`
|
|
Text string `json:"text,omitempty"`
|
|
ID string `json:"id,omitempty"`
|
|
Name string `json:"name,omitempty"`
|
|
State *ToolState `json:"state,omitempty"`
|
|
Time *PartTime `json:"time,omitempty"`
|
|
}
|
|
|
|
// PartTime is when a reasoning/text block was written.
|
|
type PartTime struct {
|
|
Created int64 `json:"created,omitempty"`
|
|
Completed int64 `json:"completed,omitempty"`
|
|
}
|
|
|
|
// ToolState is a tool call's state machine.
|
|
type ToolState struct {
|
|
Status string `json:"status"`
|
|
Input map[string]any `json:"input,omitempty"`
|
|
Content []ContentPart `json:"content,omitempty"`
|
|
Metadata map[string]any `json:"metadata,omitempty"`
|
|
Error json.RawMessage `json:"error,omitempty"`
|
|
}
|
|
|
|
// FileAttachment is a file attached to a user prompt.
|
|
type FileAttachment struct {
|
|
URI string `json:"uri"`
|
|
Name string `json:"name,omitempty"`
|
|
Description string `json:"description,omitempty"`
|
|
MIME string `json:"mime,omitempty"`
|
|
}
|
|
|
|
// MessagePage is GET /api/session/{id}/message.
|
|
type MessagePage struct {
|
|
Data []Message `json:"data"`
|
|
Cursor PageCursor `json:"cursor"`
|
|
}
|
|
|
|
// Event is one server-sent event of GET /api/event.
|
|
type Event struct {
|
|
ID string `json:"id"`
|
|
Created int64 `json:"created"`
|
|
Type string `json:"type"`
|
|
Location *Location `json:"location,omitempty"`
|
|
Durable *EventDurable `json:"durable,omitempty"`
|
|
Data json.RawMessage `json:"data"`
|
|
}
|
|
|
|
// EventDurable is the durable envelope some events carry.
|
|
type EventDurable struct {
|
|
AggregateID string `json:"aggregateID"`
|
|
Seq int64 `json:"seq"`
|
|
Version int `json:"version"`
|
|
}
|
|
|
|
// SessionEventData covers the payload fields this client reads. Use
|
|
// Event.UnmarshalData into a wider struct when more is needed.
|
|
type SessionEventData struct {
|
|
SessionID string `json:"sessionID"`
|
|
AssistantMessageID string `json:"assistantMessageID"`
|
|
Ordinal int `json:"ordinal"`
|
|
Delta string `json:"delta"`
|
|
Text string `json:"text"`
|
|
MessageID string `json:"messageID"`
|
|
PartID string `json:"partID"`
|
|
Agent string `json:"agent"`
|
|
Model *ModelRef `json:"model"`
|
|
RequestID string `json:"requestID"`
|
|
Reply string `json:"reply"`
|
|
Action string `json:"action"`
|
|
Resources []string `json:"resources"`
|
|
}
|