web: shared chat stars, pipeline mini graphs and agent questions
- Persist chat stars in the operator-session registry (chat_star) so every browser/device shows the same Starred group; migrate old local stars once. - Add a compact pipeline graph shape to list responses and draw a lightweight list miniature; replace the cryptic id/version line with the stage count. - Surface OpenCode form questions as a chat card (chat_answer), replay a pending form to late subscribers, and notify for background questions. - Make the mobile dock's Search cell equal to every destination, keep the tab row visible with 100svh, and let Source use the full pane height. - Let a fresh sign-in auto-connect to its own origin; use a Lax session cookie with Expires so Remember me survives app/link launches. - Keep shipped starters and the skill agnostic: no repository paths, project areas or personal device commands; add Diagnose/Fix and Research starters. - Refresh the installed skill and document the new browser conventions.
This commit is contained in:
@@ -108,6 +108,26 @@ func (c *Chat) PromptWithMessageID(ctx context.Context, id, text, messageID stri
|
||||
return nil
|
||||
}
|
||||
|
||||
// Answer replies to one OpenCode form. The agent's question tool pauses inside a
|
||||
// form.created event; replying lets the same turn continue. The answer object is
|
||||
// keyed by the form field keys, exactly as the OpenCode CLI sends it.
|
||||
func (c *Chat) Answer(ctx context.Context, request protocol.ChatAnswerRequest) error {
|
||||
if !sessionID.MatchString(request.SessionID) {
|
||||
return fmt.Errorf("invalid OpenCode session id")
|
||||
}
|
||||
if strings.TrimSpace(request.FormID) == "" {
|
||||
return fmt.Errorf("a form id is required")
|
||||
}
|
||||
body := map[string]any{"answer": request.Answers}
|
||||
if request.Answers == nil {
|
||||
body["answer"] = map[string]any{}
|
||||
}
|
||||
if _, err := c.call(ctx, "post", "/api/session/"+request.SessionID+"/form/"+request.FormID+"/reply", body); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Interrupt aborts the session's active run, reusing the same translated surface.
|
||||
func (c *Chat) Interrupt(ctx context.Context, id string) error {
|
||||
if !sessionID.MatchString(id) {
|
||||
@@ -635,12 +655,40 @@ func isChatEventType(t string) bool {
|
||||
"session.tool.input.started", "session.tool.input.ended",
|
||||
"session.tool.called", "session.tool.progress",
|
||||
"session.tool.success", "session.tool.failed",
|
||||
"form.created", "form.replied", "form.cancelled",
|
||||
"shell.created", "shell.exited":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// chatFormPayload is the form object nested under form.created. The question
|
||||
// tool uses metadata.kind == "question"; other forms are surfaced with their own
|
||||
// kind so the client can decide whether to render them as a question.
|
||||
type chatFormPayload struct {
|
||||
ID string `json:"id"`
|
||||
SessionID string `json:"sessionID"`
|
||||
Title string `json:"title"`
|
||||
Metadata map[string]any `json:"metadata"`
|
||||
Fields []chatFormField `json:"fields"`
|
||||
}
|
||||
|
||||
type chatFormField struct {
|
||||
Key string `json:"key"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Type string `json:"type"`
|
||||
Options []chatFormOption `json:"options"`
|
||||
Custom bool `json:"custom"`
|
||||
Required bool `json:"required"`
|
||||
}
|
||||
|
||||
type chatFormOption struct {
|
||||
Value string `json:"value"`
|
||||
Label string `json:"label"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
// chatEvent is the subset of the verified firehose envelope this adapter reads:
|
||||
// {"type":"session.text.delta","data":{...}}. Fields absent from a given event
|
||||
// stay zero so one struct covers every type.
|
||||
@@ -672,6 +720,9 @@ type chatEvent struct {
|
||||
Status string `json:"status"`
|
||||
Exit *int `json:"exit"`
|
||||
Info *chatShellInfo `json:"info"`
|
||||
// Form is the nested form.created payload; FormID is the bare id on
|
||||
// form.replied/form.cancelled.
|
||||
Form *chatFormPayload `json:"form"`
|
||||
}
|
||||
|
||||
// chatShellInfo is the Shell.Info envelope a shell.created event nests under
|
||||
@@ -711,10 +762,13 @@ func (i *chatShellInfo) shellSessionID() string {
|
||||
// seen exit. shell.exited carries no session id, so the id remembered here is
|
||||
// what attributes the exit to its session: another session's shell is an
|
||||
// unknown id and is ignored.
|
||||
// - Forms maps a pending form id to its session, so form.replied/cancelled
|
||||
// (which carry no session) can be attributed to the right chat.
|
||||
type ChatStreamState struct {
|
||||
mu sync.Mutex
|
||||
LastMessageID string
|
||||
Shells map[string]struct{}
|
||||
Forms map[string]string
|
||||
readGate chan struct{}
|
||||
readback *ShellSnapshot
|
||||
}
|
||||
@@ -833,6 +887,9 @@ func NormalizeChatEvent(payload []byte, sessionID string, stream *ChatStreamStat
|
||||
if outer.Type == "shell.created" || outer.Type == "shell.exited" {
|
||||
return normalizeShellEvent(outer.Type, data, sessionID, stream)
|
||||
}
|
||||
if strings.HasPrefix(outer.Type, "form.") {
|
||||
return normalizeFormEvent(outer.Type, data, sessionID, stream)
|
||||
}
|
||||
if data.SessionID != sessionID {
|
||||
return protocol.ChatEventFrame{}, false, nil
|
||||
}
|
||||
@@ -882,6 +939,88 @@ func NormalizeChatEvent(payload []byte, sessionID string, stream *ChatStreamStat
|
||||
return frame, true, nil
|
||||
}
|
||||
|
||||
// normalizeFormEvent maps one form.created / form.replied / form.cancelled
|
||||
// payload onto a ChatEventForm frame. Form.created carries its own session; the
|
||||
// terminal events carry only an id, so the stream remembers which session owns
|
||||
// each pending form. A form from another session is ignored, and an unknown
|
||||
// terminal id is another session's form rather than a malformed frame.
|
||||
func normalizeFormEvent(eventType string, data chatEvent, sessionID string, stream *ChatStreamState) (protocol.ChatEventFrame, bool, error) {
|
||||
if eventType == "form.created" {
|
||||
form := data.Form
|
||||
if form == nil || form.ID == "" {
|
||||
return protocol.ChatEventFrame{}, false, fmt.Errorf("malformed OpenCode form.created event")
|
||||
}
|
||||
if form.SessionID != "" && form.SessionID != sessionID {
|
||||
return protocol.ChatEventFrame{}, false, nil
|
||||
}
|
||||
kind := ""
|
||||
if form.Metadata != nil {
|
||||
kind, _ = form.Metadata["kind"].(string)
|
||||
}
|
||||
frame := protocol.ChatForm{
|
||||
ID: form.ID,
|
||||
SessionID: sessionID,
|
||||
Title: form.Title,
|
||||
Kind: kind,
|
||||
}
|
||||
for _, field := range form.Fields {
|
||||
out := protocol.ChatFormField{
|
||||
Key: field.Key,
|
||||
Title: field.Title,
|
||||
Description: field.Description,
|
||||
Type: field.Type,
|
||||
Custom: field.Custom,
|
||||
Required: field.Required,
|
||||
}
|
||||
for _, option := range field.Options {
|
||||
value := option.Value
|
||||
if value == "" {
|
||||
value = option.Label
|
||||
}
|
||||
out.Options = append(out.Options, protocol.ChatFormOption{
|
||||
Value: value, Label: option.Label, Description: option.Description,
|
||||
})
|
||||
}
|
||||
frame.Fields = append(frame.Fields, out)
|
||||
}
|
||||
if stream != nil {
|
||||
stream.mu.Lock()
|
||||
if stream.Forms == nil {
|
||||
stream.Forms = map[string]string{}
|
||||
}
|
||||
stream.Forms[form.ID] = sessionID
|
||||
stream.mu.Unlock()
|
||||
}
|
||||
return protocol.ChatEventFrame{SessionID: sessionID, Kind: protocol.ChatEventForm, Form: &frame}, true, nil
|
||||
}
|
||||
id := data.ID
|
||||
if id == "" && data.Form != nil {
|
||||
id = data.Form.ID
|
||||
}
|
||||
if id == "" {
|
||||
return protocol.ChatEventFrame{}, false, fmt.Errorf("malformed OpenCode %s event", eventType)
|
||||
}
|
||||
if stream == nil {
|
||||
return protocol.ChatEventFrame{}, false, nil
|
||||
}
|
||||
stream.mu.Lock()
|
||||
owner, known := stream.Forms[id]
|
||||
if known && owner == sessionID {
|
||||
delete(stream.Forms, id)
|
||||
}
|
||||
stream.mu.Unlock()
|
||||
if !known || owner != sessionID {
|
||||
return protocol.ChatEventFrame{}, false, nil
|
||||
}
|
||||
form := &protocol.ChatForm{ID: id, SessionID: sessionID}
|
||||
if eventType == "form.cancelled" {
|
||||
form.Cancelled = true
|
||||
} else {
|
||||
form.Resolved = true
|
||||
}
|
||||
return protocol.ChatEventFrame{SessionID: sessionID, Kind: protocol.ChatEventForm, Form: form}, true, nil
|
||||
}
|
||||
|
||||
// normalizeShellEvent maps one shell.created/shell.exited payload onto a
|
||||
// protocol.ChatEventShell frame. A shell belongs to the session whose id the
|
||||
// created event carries in info.metadata.sessionID, so a shell started by any
|
||||
|
||||
@@ -822,3 +822,41 @@ func TestChatV1ConnectionDrivesThroughCall(t *testing.T) {
|
||||
t.Fatalf("messages = %+v", page.Messages)
|
||||
}
|
||||
}
|
||||
|
||||
// TestNormalizeChatEventForm proves the question tool's form lifecycle is
|
||||
// surfaced: form.created carries the field structure and is remembered for the
|
||||
// session, while the terminal events (which carry only an id) resolve the same
|
||||
// form for that session and are ignored for another.
|
||||
func TestNormalizeChatEventForm(t *testing.T) {
|
||||
state := &ChatStreamState{}
|
||||
created := []byte(`{"type":"form.created","data":{"form":{"id":"form_1","sessionID":"ses_1","title":"Choose a colour","metadata":{"kind":"question"},"fields":[{"key":"colour","title":"Which colour?","description":"Pick one","type":"string","required":true,"options":[{"value":"red","label":"Red","description":"Warm"},{"value":"blue","label":"Blue"}]}]}}}`)
|
||||
frame, ok, err := NormalizeChatEvent(created, "ses_1", state)
|
||||
if err != nil || !ok {
|
||||
t.Fatalf("form.created: ok=%v err=%v", ok, err)
|
||||
}
|
||||
if frame.Kind != protocol.ChatEventForm || frame.Form == nil {
|
||||
t.Fatalf("form.created frame = %+v", frame)
|
||||
}
|
||||
if frame.Form.ID != "form_1" || frame.Form.Kind != "question" || len(frame.Form.Fields) != 1 {
|
||||
t.Fatalf("form.created form = %+v", frame.Form)
|
||||
}
|
||||
field := frame.Form.Fields[0]
|
||||
if field.Key != "colour" || field.Title != "Which colour?" || !field.Required || len(field.Options) != 2 {
|
||||
t.Fatalf("form.created field = %+v", field)
|
||||
}
|
||||
if field.Options[0].Value != "red" || field.Options[0].Label != "Red" || field.Options[0].Description != "Warm" {
|
||||
t.Fatalf("form option = %+v", field.Options[0])
|
||||
}
|
||||
|
||||
// Another session's terminal event is not this session's form.
|
||||
if _, ok, err := NormalizeChatEvent([]byte(`{"type":"form.replied","data":{"id":"form_1"}}`), "ses_2", state); err != nil || ok {
|
||||
t.Fatalf("foreign form.replied: ok=%v err=%v", ok, err)
|
||||
}
|
||||
resolved, ok, err := NormalizeChatEvent([]byte(`{"type":"form.replied","data":{"id":"form_1"}}`), "ses_1", state)
|
||||
if err != nil || !ok || resolved.Form == nil || !resolved.Form.Resolved || resolved.Form.ID != "form_1" {
|
||||
t.Fatalf("form.replied frame = %+v ok=%v err=%v", resolved, ok, err)
|
||||
}
|
||||
if _, ok, err := NormalizeChatEvent([]byte(`{"type":"form.cancelled","data":{"id":"form_1"}}`), "ses_1", state); err != nil || ok {
|
||||
t.Fatalf("duplicate/unknown form.cancelled: ok=%v err=%v", ok, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user