feat(session,llm): restore session runtime on resume; align Responses API details
This commit is contained in:
+22
-1
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user