Files
openchamber-android-native/internal/ocapi/types.go
T
blumia 72119b73e8 feat: search sessions, attach files, read aloud, jump to latest
Reach the first-party mobile client's everyday features:

- sessions: a search button opens a field; typing asks the server with
  its search parameter (350 ms settle) and clearing restores the list
- composer: attach photos or files (up to five, 10 MB each) as data
  URLs with the prompt; attachments show as removable chips and are
  named under the sent message
- turn menu: read the answer aloud with the system voice, or stop it
- chat: a "latest" button appears after scrolling up and returns to the
  end of the conversation
- reset a live search when a connection ends

Bump version to 0.2.0 (build 14).
2026-11-17 21:52:06 +08:00

227 lines
7.7 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"`
}
// PromptFile is a file riding with a prompt: a data URL or a URI the
// server can read, and its name.
type PromptFile struct {
URI string `json:"uri"`
Name string `json:"name,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"`
}