feat(session,llm): restore session runtime on resume; align Responses API details

This commit is contained in:
loveuer
2026-08-13 13:46:26 +08:00
parent 425f6ee5ee
commit 28195519d1
8 changed files with 274 additions and 19 deletions
+22 -1
View File
@@ -124,6 +124,9 @@ func buildResponsesRequest(req ChatRequest) map[string]any {
"input": buildResponsesInput(req.Messages),
"stream": true,
}
if instructions := responsesInstructions(req.Messages); instructions != "" {
payload["instructions"] = instructions
}
if len(req.Tools) > 0 {
payload["tools"] = buildResponsesTools(req.Tools)
}
@@ -153,7 +156,9 @@ func buildResponsesInput(messages []Message) []any {
for _, msg := range messages {
switch msg.Role {
case RoleSystem:
items = append(items, responsesMessageInput{Role: "system", Content: msg.Content})
// System instructions are sent as the top-level "instructions"
// field and must not be duplicated inside the input array.
continue
case RoleUser:
items = append(items, responsesMessageInput{Role: "user", Content: msg.Content})
case RoleAssistant:
@@ -179,6 +184,22 @@ func buildResponsesInput(messages []Message) []any {
return items
}
// responsesInstructions extracts system messages into a single instructions
// string, which is the OpenAI Responses API way to pass developer/system
// guidance (and the most prompt-cache friendly).
func responsesInstructions(messages []Message) string {
var instructions []string
for _, msg := range messages {
if msg.Role != RoleSystem {
continue
}
if text := strings.TrimSpace(msg.Content); text != "" {
instructions = append(instructions, text)
}
}
return strings.Join(instructions, "\n\n")
}
func buildResponsesTools(tools []Tool) []responsesTool {
out := make([]responsesTool, 0, len(tools))
for _, tool := range tools {