68 lines
2.2 KiB
Go
68 lines
2.2 KiB
Go
package format
|
|
|
|
import (
|
|
"fmt"
|
|
"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.
|
|
// If blockID is non-empty, it is included in the block header.
|
|
func FormatEvent(role, name, content, outputFormat, blockID string) []byte {
|
|
switch role {
|
|
case "user":
|
|
if blockID != "" {
|
|
return []byte(fmt.Sprintf("[[[user#%s]]]\n", blockID) + content + "\n" + EndTag + "\n")
|
|
}
|
|
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), " ")
|
|
if blockID != "" {
|
|
return []byte(fmt.Sprintf("[[[call:%s#%s]]]\n", name, blockID) + "```\n" + args + "\n```\n" + EndTag + "\n")
|
|
}
|
|
return []byte(CallDelim(name) + "```\n" + args + "\n```\n" + EndTag + "\n")
|
|
case "tool":
|
|
fence := "```"
|
|
if outputFormat != "" {
|
|
fence += outputFormat
|
|
}
|
|
if blockID != "" {
|
|
return []byte(fmt.Sprintf("[[[tool:%s#%s]]]\n", name, blockID) + fence + "\n" + strings.TrimRight(content, "\n") + "\n```\n" + EndTag + "\n")
|
|
}
|
|
return []byte(ToolDelim(name) + fence + "\n" + strings.TrimRight(content, "\n") + "\n```\n" + EndTag + "\n")
|
|
case "error":
|
|
r := "error"
|
|
if name != "" {
|
|
r = "error:" + name
|
|
}
|
|
if blockID != "" {
|
|
return []byte(fmt.Sprintf("[[[%s#%s]]]\n", r, blockID) + content + "\n" + EndTag + "\n")
|
|
}
|
|
return []byte(RoleDelim(r) + "\n" + content + "\n" + EndTag + "\n")
|
|
case "info":
|
|
if blockID != "" {
|
|
return []byte(fmt.Sprintf("[[[info#%s]]]\n", blockID) + content + "\n" + EndTag + "\n")
|
|
}
|
|
return []byte(RoleDelim("info") + "\n" + content + "\n" + EndTag + "\n")
|
|
case "retry":
|
|
return []byte(content)
|
|
case "stalled":
|
|
if blockID != "" {
|
|
return []byte(fmt.Sprintf("[[[stalled#%s]]]\n", blockID) + EndTag + "\n")
|
|
}
|
|
return []byte(RoleDelim("stalled") + "\n" + EndTag + "\n")
|
|
case "maxsteps":
|
|
if blockID != "" {
|
|
return []byte(fmt.Sprintf("[[[maxsteps#%s]]]\n", blockID) + content + "\n" + EndTag + "\n")
|
|
}
|
|
return []byte(RoleDelim("maxsteps") + "\n" + content + "\n" + EndTag + "\n")
|
|
default:
|
|
return nil
|
|
}
|
|
}
|