mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 08:02:53 +00:00
95 lines
2.3 KiB
Go
95 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"runtime"
|
|
"strings"
|
|
)
|
|
|
|
type requestIDs struct {
|
|
Session string
|
|
Request string
|
|
Project string
|
|
}
|
|
|
|
func deriveRequestIDs(r *http.Request, body map[string]any) requestIDs {
|
|
signal := firstString(
|
|
r.Header.Get("x-opencode-session"),
|
|
r.Header.Get("x-session-id"),
|
|
r.Header.Get("conversation-id"),
|
|
stringAt(body, "conversation_id"),
|
|
stringAt(body, "metadata", "session_id"),
|
|
)
|
|
if signal == "" {
|
|
// Using the first user turn keeps a multi-turn conversation stable as its
|
|
// history grows while separating conversations with different beginnings.
|
|
signal = conversationSeed(body)
|
|
}
|
|
if signal == "" {
|
|
signal = stringAt(body, "previous_response_id")
|
|
}
|
|
if signal == "" || signal == `{}` {
|
|
signal = randomID("fallback", 16)
|
|
}
|
|
session := stableID("ses", signal)
|
|
projectSignal := firstString(r.Header.Get("x-opencode-project"), stringAt(body, "metadata", "project_id"))
|
|
if projectSignal == "" {
|
|
projectSignal = "opencode2api:default-project"
|
|
}
|
|
return requestIDs{
|
|
Session: session,
|
|
Request: randomID("req", 16),
|
|
Project: stableID("prj", projectSignal),
|
|
}
|
|
}
|
|
|
|
func conversationSeed(body map[string]any) string {
|
|
if input, ok := body["input"].(string); ok && input != "" {
|
|
return input
|
|
}
|
|
for _, field := range []string{"messages", "input"} {
|
|
for _, raw := range sliceAt(body, field) {
|
|
item, ok := raw.(map[string]any)
|
|
if !ok || stringAt(item, "role") != "user" {
|
|
continue
|
|
}
|
|
encoded, _ := json.Marshal(item["content"])
|
|
if len(encoded) > 0 && string(encoded) != "null" {
|
|
return string(encoded)
|
|
}
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func stableID(prefix, value string) string {
|
|
sum := sha256.Sum256([]byte(prefix + "\x00" + value))
|
|
return prefix + "_" + hex.EncodeToString(sum[:12])
|
|
}
|
|
|
|
func randomID(prefix string, size int) string {
|
|
buf := make([]byte, size)
|
|
if _, err := rand.Read(buf); err != nil {
|
|
panic(fmt.Sprintf("crypto/rand failed: %v", err))
|
|
}
|
|
return prefix + "_" + hex.EncodeToString(buf)
|
|
}
|
|
|
|
func firstString(values ...string) string {
|
|
for _, value := range values {
|
|
if value = strings.TrimSpace(value); value != "" {
|
|
return value
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func opencodeUserAgent() string {
|
|
return fmt.Sprintf("opencode/1.18.18 (%s %s; %s)", runtime.GOOS, runtime.GOARCH, runtime.Version())
|
|
}
|