Send mid-turn chat prompts to the harness and queue the rest durably on the server
Claude Code (stream-json stdin) and Codex (turn/steer pinned to the active turn) receive a prompt sent while a turn runs immediately. Other harnesses (OpenCode) get a durable chat_prompt_queue row owned by the session: it survives the last client detaching and a server restart, is delivered in order when the turn ends with or without a client, and is deduplicated by operation id. chat_subscribe carries the queue, chat_queue frames report delivered/removed/failed changes, and chat_unqueue removes a waiting prompt.
This commit is contained in:
@@ -457,6 +457,38 @@ func (c *Chat) PromptWithMessageIDModelFiles(ctx context.Context, id, text, mess
|
||||
return err
|
||||
}
|
||||
|
||||
// SteerPrompt adds a user message to the thread's active turn with the App
|
||||
// Server's turn/steer, which the agent reads at its next step. expectedTurnId
|
||||
// pins the turn: when it already ended (or none is active) the App Server
|
||||
// refuses and false is returned so the caller queues the prompt for the next
|
||||
// turn instead. A transport failure is an error: it may have been admitted and
|
||||
// must not be blindly repeated.
|
||||
func (c *Chat) SteerPrompt(ctx context.Context, id, text, messageID string, files []opencode.PromptFile) (bool, error) {
|
||||
c.mu.Lock()
|
||||
turn := c.turns[id]
|
||||
c.mu.Unlock()
|
||||
if turn == "" {
|
||||
return false, nil
|
||||
}
|
||||
input, err := promptInput(text, files)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
params := map[string]any{"threadId": id, "expectedTurnId": turn, "input": input}
|
||||
if messageID != "" {
|
||||
params["clientUserMessageId"] = messageID
|
||||
}
|
||||
_, err = c.call(ctx, "turn/steer", params)
|
||||
var refused *rpcCallError
|
||||
if errors.As(err, &refused) {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// ActivateSkill is an OpenCode-only chat concept.
|
||||
func (c *Chat) ActivateSkill(context.Context, string, string) error {
|
||||
return errors.New("Codex chat has no out-of-band skill activation")
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package codex
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// A prompt for a thread with an active turn is steered into that turn, pinned
|
||||
// by expectedTurnId; a refusal (the turn already ended) reports not delivered
|
||||
// so the server queues it, and a thread with no active turn is not steered.
|
||||
func TestSteerPromptTargetsTheActiveTurn(t *testing.T) {
|
||||
var steered []map[string]any
|
||||
refuse := false
|
||||
chat := modelTestChat(t, func(method string, raw json.RawMessage) any {
|
||||
if method != "turn/steer" {
|
||||
t.Errorf("unexpected method %s", method)
|
||||
return map[string]any{}
|
||||
}
|
||||
var params map[string]any
|
||||
if err := json.Unmarshal(raw, ¶ms); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
steered = append(steered, params)
|
||||
if refuse {
|
||||
return &rpcError{Code: -32600, Message: "expected turn is not active"}
|
||||
}
|
||||
return map[string]any{"turnId": params["expectedTurnId"]}
|
||||
})
|
||||
if delivered, err := chat.SteerPrompt(context.Background(), fixtureThread, "idle", "msg_idle", nil); err != nil || delivered {
|
||||
t.Fatalf("idle thread steered: delivered=%v err=%v", delivered, err)
|
||||
}
|
||||
chat.mu.Lock()
|
||||
chat.turns[fixtureThread] = "turn-7"
|
||||
chat.mu.Unlock()
|
||||
delivered, err := chat.SteerPrompt(context.Background(), fixtureThread, "also this", "msg_steer", nil)
|
||||
if err != nil || !delivered {
|
||||
t.Fatalf("active turn not steered: delivered=%v err=%v", delivered, err)
|
||||
}
|
||||
if len(steered) != 1 || steered[0]["expectedTurnId"] != "turn-7" || steered[0]["threadId"] != fixtureThread || steered[0]["clientUserMessageId"] != "msg_steer" {
|
||||
t.Fatalf("steer params = %+v", steered)
|
||||
}
|
||||
refuse = true
|
||||
if delivered, err := chat.SteerPrompt(context.Background(), fixtureThread, "late", "msg_late", nil); err != nil || delivered {
|
||||
t.Fatalf("refused steer reported delivered=%v err=%v", delivered, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user