chat: reconcile history by canonical message identity
This commit is contained in:
@@ -82,6 +82,13 @@ func (c *Chat) call(ctx context.Context, method, path string, body any) ([]byte,
|
||||
// (authenticated, bounded, directory-scoped). The endpoint returns the inbox
|
||||
// envelope immediately; the reply arrives on Stream.
|
||||
func (c *Chat) Prompt(ctx context.Context, id, text string) error {
|
||||
return c.PromptWithMessageID(ctx, id, text, "")
|
||||
}
|
||||
|
||||
// PromptWithMessageID forwards the caller's canonical message id to OpenCode.
|
||||
// OpenCode persists this id in history, which is the only safe bridge between
|
||||
// the live user echo and an authoritative history page.
|
||||
func (c *Chat) PromptWithMessageID(ctx context.Context, id, text, messageID string) error {
|
||||
if !sessionID.MatchString(id) {
|
||||
return fmt.Errorf("invalid OpenCode session id")
|
||||
}
|
||||
@@ -92,6 +99,9 @@ func (c *Chat) Prompt(ctx context.Context, id, text string) error {
|
||||
// the same "text" field, so one shape works on both backends without a
|
||||
// second request format.
|
||||
body := map[string]any{"text": text}
|
||||
if messageID != "" {
|
||||
body["id"] = messageID
|
||||
}
|
||||
if _, err := c.call(ctx, "post", "/api/session/"+id+"/prompt", body); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -782,7 +782,7 @@ func TestChatV1ConnectionDrivesThroughCall(t *testing.T) {
|
||||
chat := NewChat(&Connection{Backend: "v1", URL: srv.URL, Directory: "/repo", models: map[string]map[string]string{}, responses: map[string][]byte{}})
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err := chat.Prompt(ctx, "ses_1", "hello"); err != nil {
|
||||
if err := chat.PromptWithMessageID(ctx, "ses_1", "hello", "msg_canonical"); err != nil {
|
||||
t.Fatalf("prompt: %v", err)
|
||||
}
|
||||
if !sawPrompt {
|
||||
@@ -792,6 +792,7 @@ func TestChatV1ConnectionDrivesThroughCall(t *testing.T) {
|
||||
// V2 `parts` shape and the v1 translation has to preserve it, or the native
|
||||
// serve process receives an empty prompt.
|
||||
var sent struct {
|
||||
ID string `json:"id"`
|
||||
Parts []struct {
|
||||
Type string `json:"type"`
|
||||
Text string `json:"text"`
|
||||
@@ -800,7 +801,7 @@ func TestChatV1ConnectionDrivesThroughCall(t *testing.T) {
|
||||
if err := json.Unmarshal([]byte(promptBody), &sent); err != nil {
|
||||
t.Fatalf("prompt body: %v (%s)", err, promptBody)
|
||||
}
|
||||
if len(sent.Parts) != 1 || sent.Parts[0].Type != protocol.ChatPartText || sent.Parts[0].Text != "hello" {
|
||||
if sent.ID != "msg_canonical" || len(sent.Parts) != 1 || sent.Parts[0].Type != protocol.ChatPartText || sent.Parts[0].Text != "hello" {
|
||||
t.Fatalf("prompt text not carried through the v1 translation: %s", promptBody)
|
||||
}
|
||||
var got []protocol.ChatEventFrame
|
||||
|
||||
@@ -64,6 +64,25 @@ func TestChatV2PromptShapeAndAuth(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatV2PromptWithMessageID(t *testing.T) {
|
||||
var body map[string]string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"data":{"id":"msg_canonical","sessionID":"ses_1","type":"user"}}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
|
||||
if err := chat.PromptWithMessageID(context.Background(), "ses_1", "same text", "msg_canonical"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if body["id"] != "msg_canonical" || body["text"] != "same text" {
|
||||
t.Fatalf("prompt body = %+v", body)
|
||||
}
|
||||
}
|
||||
|
||||
// TestChatStreamRealV2Shapes replays the exact event frames the installed 2.0.20
|
||||
// service emitted on /api/event during a live turn (captured, not documented):
|
||||
// a step start, token text delta, usage and a step end whose data carries no
|
||||
|
||||
@@ -195,6 +195,7 @@ func (c *Connection) Call(ctx context.Context, method, path string, body any) ([
|
||||
var request struct {
|
||||
Prompt string `json:"prompt"`
|
||||
Text string `json:"text"`
|
||||
ID string `json:"id"`
|
||||
Format json.RawMessage `json:"format"`
|
||||
Parts []struct {
|
||||
Text string `json:"text"`
|
||||
@@ -219,6 +220,9 @@ func (c *Connection) Call(ctx context.Context, method, path string, body any) ([
|
||||
}
|
||||
}
|
||||
payload := map[string]any{"model": c.models[id], "parts": []map[string]string{{"type": "text", "text": text}}}
|
||||
if request.ID != "" {
|
||||
payload["id"] = request.ID
|
||||
}
|
||||
if len(request.Format) > 0 {
|
||||
supported, err := c.supportsFormat(ctx, request.Format)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user