41 lines
1.3 KiB
Go
41 lines
1.3 KiB
Go
package format
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
// FormatEvent converts event data to bytes for appending to a chat log.
|
|
// Streaming roles (assistant, reasoning, retry) return only the content chunk;
|
|
// the caller is responsible for writing the [[[role]]] header for those.
|
|
func FormatEvent(role, name, content, outputFormat string) []byte {
|
|
switch role {
|
|
case "user":
|
|
return []byte(UserTag + "\n" + content + "\n" + EndTag + "\n")
|
|
case "assistant":
|
|
return []byte(content)
|
|
case "reasoning":
|
|
return []byte(content)
|
|
case "call":
|
|
args := strings.Join(strings.Fields(content), " ")
|
|
return []byte(CallDelim(name) + "```\n" + args + "\n```\n" + EndTag + "\n")
|
|
case "tool":
|
|
fence := "```"
|
|
if outputFormat != "" {
|
|
fence += outputFormat
|
|
}
|
|
return []byte(ToolDelim(name) + fence + "\n" + strings.TrimRight(content, "\n") + "\n```\n" + EndTag + "\n")
|
|
case "error":
|
|
return []byte(RoleDelim("error") + "\n" + content + "\n" + EndTag + "\n")
|
|
case "info":
|
|
return []byte(RoleDelim("info") + "\n" + content + "\n" + EndTag + "\n")
|
|
case "retry":
|
|
return []byte(content)
|
|
case "stalled":
|
|
return []byte(RoleDelim("stalled") + "\n" + EndTag + "\n")
|
|
case "maxsteps":
|
|
return []byte(RoleDelim("maxsteps") + "\n" + content + "\n" + EndTag + "\n")
|
|
default:
|
|
return nil
|
|
}
|
|
}
|