171 lines
6.8 KiB
Go
171 lines
6.8 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// LargeChatFixtureSessionID is the throwaway operator session exposed by the
|
|
// foreground large-history fixture. It is not a model or production session.
|
|
const LargeChatFixtureSessionID = "ses_large_chat_fixture"
|
|
|
|
// LargeChatFixturePassword authenticates the disposable fixture endpoint. It is
|
|
// fixed fixture data, not an operator credential.
|
|
const LargeChatFixturePassword = "large-chat-fixture"
|
|
const LargeChatFixtureShellID = "sh_large_chat_fixture"
|
|
const LargeChatFixtureShellOutput = "fixture shell output marker\n"
|
|
const LargeChatFixtureForeignSessionID = "ses_foreign_fixture"
|
|
const LargeChatFixtureForeignShellID = "sh_foreign_fixture"
|
|
const LargeChatFixtureForeignShellOutput = "FOREIGN SHELL MARKER MUST NOT LEAK\n"
|
|
|
|
const (
|
|
LargeChatFixtureNewestAnswer = "Controlled newest answer: large history fixture"
|
|
LargeChatFixtureOlderAnswer = "Controlled older answer: large history fixture"
|
|
)
|
|
|
|
// LargeChatFixtureHandler implements the small authenticated V2 OpenCode surface
|
|
// required to drive the real Pipelines chat broker during a visual history check.
|
|
// It deliberately serves fixed, paged data: no model is invoked and no production
|
|
// history is read or written.
|
|
func LargeChatFixtureHandler(workspace string) http.Handler {
|
|
return LargeChatFixtureHandlerWithShell(workspace, false)
|
|
}
|
|
|
|
// LargeChatFixtureHandlerWithShell is the foreground fixture surface used by
|
|
// the readback check. exited selects the provider's observed terminal state;
|
|
// the output remains fixed and bounded in either state.
|
|
func LargeChatFixtureHandlerWithShell(workspace string, exited bool) http.Handler {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("GET /api/info", func(w http.ResponseWriter, r *http.Request) {
|
|
if !largeChatFixtureAuthenticated(w, r) {
|
|
return
|
|
}
|
|
writeLargeChatFixtureJSON(w, `{"version":"fixture","urls":[]}`)
|
|
})
|
|
mux.HandleFunc("GET /api/event", func(w http.ResponseWriter, r *http.Request) {
|
|
if !largeChatFixtureAuthenticated(w, r) {
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
if flush, ok := w.(http.Flusher); ok {
|
|
flush.Flush()
|
|
}
|
|
})
|
|
mux.HandleFunc("GET /api/session", func(w http.ResponseWriter, r *http.Request) {
|
|
if !largeChatFixtureAuthenticated(w, r) {
|
|
return
|
|
}
|
|
writeLargeChatFixtureJSON(w, fmt.Sprintf(`{"data":[{"id":%q,"title":"Controlled large chat history","agent":"fixture","model":{"id":"fixture"},"location":{"directory":%q},"time":{"created":1,"updated":1}}]}`, LargeChatFixtureSessionID, workspace))
|
|
})
|
|
mux.HandleFunc("GET /api/session/"+LargeChatFixtureSessionID+"/message", func(w http.ResponseWriter, r *http.Request) {
|
|
if !largeChatFixtureAuthenticated(w, r) {
|
|
return
|
|
}
|
|
page := 0
|
|
switch r.URL.Query().Get("cursor") {
|
|
case "":
|
|
case "older+/=&":
|
|
page = 1
|
|
case "last":
|
|
page = 2
|
|
default:
|
|
http.Error(w, "unknown opaque cursor", http.StatusBadRequest)
|
|
return
|
|
}
|
|
writeLargeChatFixturePage(w, page)
|
|
})
|
|
status := "running"
|
|
if exited {
|
|
status = "exited"
|
|
}
|
|
mux.HandleFunc("GET /api/shell", func(w http.ResponseWriter, r *http.Request) {
|
|
if !largeChatFixtureAuthenticated(w, r) {
|
|
return
|
|
}
|
|
writeLargeChatFixtureJSON(w, fmt.Sprintf(`{"data":[{"id":%q,"status":%q,"command":"printf fixture","metadata":{"sessionID":%q}},{"id":%q,"status":"running","command":"printf foreign","metadata":{"sessionID":%q}}]}`, LargeChatFixtureShellID, status, LargeChatFixtureSessionID, LargeChatFixtureForeignShellID, LargeChatFixtureForeignSessionID))
|
|
})
|
|
mux.HandleFunc("GET /api/shell/"+LargeChatFixtureShellID, func(w http.ResponseWriter, r *http.Request) {
|
|
if !largeChatFixtureAuthenticated(w, r) {
|
|
return
|
|
}
|
|
exit := "null"
|
|
if exited {
|
|
exit = "0"
|
|
}
|
|
writeLargeChatFixtureJSON(w, fmt.Sprintf(`{"data":{"id":%q,"status":%q,"command":"printf fixture","exit":%s,"metadata":{"sessionID":%q}}}`, LargeChatFixtureShellID, status, exit, LargeChatFixtureSessionID))
|
|
})
|
|
mux.HandleFunc("GET /api/shell/"+LargeChatFixtureForeignShellID, func(w http.ResponseWriter, r *http.Request) {
|
|
if !largeChatFixtureAuthenticated(w, r) {
|
|
return
|
|
}
|
|
writeLargeChatFixtureJSON(w, fmt.Sprintf(`{"data":{"id":%q,"status":"running","command":"printf foreign","metadata":{"sessionID":%q}}}`, LargeChatFixtureForeignShellID, LargeChatFixtureForeignSessionID))
|
|
})
|
|
mux.HandleFunc("GET /api/shell/"+LargeChatFixtureShellID+"/output", func(w http.ResponseWriter, r *http.Request) {
|
|
if !largeChatFixtureAuthenticated(w, r) {
|
|
return
|
|
}
|
|
cursor := int64(len(LargeChatFixtureShellOutput))
|
|
writeLargeChatFixtureJSON(w, fmt.Sprintf(`{"data":{"output":%q,"cursor":%d,"size":%d,"truncated":false}}`, LargeChatFixtureShellOutput, cursor, cursor))
|
|
})
|
|
mux.HandleFunc("GET /api/shell/"+LargeChatFixtureForeignShellID+"/output", func(w http.ResponseWriter, r *http.Request) {
|
|
if !largeChatFixtureAuthenticated(w, r) {
|
|
return
|
|
}
|
|
cursor := int64(len(LargeChatFixtureForeignShellOutput))
|
|
writeLargeChatFixtureJSON(w, fmt.Sprintf(`{"data":{"output":%q,"cursor":%d,"size":%d,"truncated":false}}`, LargeChatFixtureForeignShellOutput, cursor, cursor))
|
|
})
|
|
return mux
|
|
}
|
|
|
|
func largeChatFixtureAuthenticated(w http.ResponseWriter, r *http.Request) bool {
|
|
user, password, ok := r.BasicAuth()
|
|
if !ok || user != "opencode" || password != LargeChatFixturePassword {
|
|
w.Header().Set("WWW-Authenticate", `Basic realm="large-chat-fixture"`)
|
|
http.Error(w, "authentication required", http.StatusUnauthorized)
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func writeLargeChatFixtureJSON(w http.ResponseWriter, body string) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(body))
|
|
}
|
|
|
|
func writeLargeChatFixturePage(w http.ResponseWriter, page int) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"data":[`))
|
|
for i := 0; i < 2; i++ {
|
|
if i != 0 {
|
|
_, _ = w.Write([]byte(","))
|
|
}
|
|
id := 6 - page*2 - i
|
|
// Equal timestamps ensure the broker's verified descending V2 sequence,
|
|
// not guessed timestamp ordering, determines transcript order.
|
|
_, _ = fmt.Fprintf(w, `{"id":"msg_%d","type":"assistant","time":{"created":1},"content":[`, id)
|
|
if page == 0 && i == 0 {
|
|
_, _ = w.Write([]byte(`{"type":"tool","id":"huge","state":{"content":[{"type":"text","text":"`))
|
|
chunk := strings.Repeat("x", 32<<10)
|
|
for n := 0; n < 65; n++ {
|
|
_, _ = w.Write([]byte(chunk))
|
|
}
|
|
_, _ = w.Write([]byte(`"}]}},`))
|
|
}
|
|
if page == 1 && i == 0 {
|
|
for n := 0; n < 25; n++ {
|
|
_, _ = fmt.Fprintf(w, `{"type":"tool","id":"tool_%d","state":{"status":"completed","content":[{"type":"text","text":%q}]}},`, n, strings.Repeat("y", 48<<10))
|
|
}
|
|
}
|
|
answer := fmt.Sprintf("fixture answer %d", id)
|
|
if id == 6 {
|
|
answer = LargeChatFixtureNewestAnswer
|
|
} else if id == 4 {
|
|
answer = LargeChatFixtureOlderAnswer
|
|
}
|
|
_, _ = fmt.Fprintf(w, `{"type":"text","text":%q}]}`, answer)
|
|
}
|
|
next := []string{`"older+/=&"`, `"last"`, "null"}[page]
|
|
_, _ = fmt.Fprintf(w, `],"cursor":{"next":%s}}`, next)
|
|
}
|