Documentation
¶
Index ¶
- Constants
- func AppendUser(prompt []fantasy.Message, instruction string) []fantasy.Message
- func ConvertMessagesWithFiles(ctx context.Context, messages []database.ChatMessage, resolver FileResolver, ...) ([]fantasy.Message, error)
- func ExtractFileID(raw json.RawMessage) (uuid.UUID, error)
- func ExtractToolCalls(parts []fantasy.MessagePart) []fantasy.ToolCallContent
- func InsertSystem(prompt []fantasy.Message, instruction string) []fantasy.Message
- func MarshalContent(blocks []fantasy.Content, fileIDs map[int]uuid.UUID) (pqtype.NullRawMessage, error)
- func MarshalParts(parts []codersdk.ChatMessagePart) (pqtype.NullRawMessage, error)
- func MarshalToolResult(toolCallID, toolName string, result json.RawMessage, isError bool, ...) (pqtype.NullRawMessage, error)
- func ParseContent(msg database.ChatMessage) ([]codersdk.ChatMessagePart, error)
- func PartFromContent(block fantasy.Content) codersdk.ChatMessagePart
- func PartFromContentWithLogger(ctx context.Context, logger slog.Logger, block fantasy.Content) codersdk.ChatMessagePart
- func PrependSystem(prompt []fantasy.Message, instruction string) []fantasy.Message
- func ToolResultToPart(toolCallID, toolName string, result json.RawMessage, isError bool, ...) codersdk.ChatMessagePart
- type FileData
- type FileResolver
Constants ¶
const ( // ContentVersionV0 is the legacy content format. Parsing uses // role-aware heuristics to distinguish fantasy envelope format // from SDK parts. ContentVersionV0 int16 = 0 // ContentVersionV1 stores content as []codersdk.ChatMessagePart // JSON for all roles. ContentVersionV1 int16 = 1 // CurrentContentVersion is the version used for new inserts. CurrentContentVersion = ContentVersionV1 )
Variables ¶
This section is empty.
Functions ¶
func AppendUser ¶
AppendUser appends an instruction as a user message at the end of the prompt.
func ConvertMessagesWithFiles ¶
func ConvertMessagesWithFiles( ctx context.Context, messages []database.ChatMessage, resolver FileResolver, logger slog.Logger, ) ([]fantasy.Message, error)
ConvertMessagesWithFiles converts persisted chat messages into LLM prompt messages, resolving user file references via the provided resolver. Missing-data placeholders are emitted only for replayed user uploads; assistant-side and tool-side file metadata without bytes is dropped from later model turns.
func ExtractFileID ¶
func ExtractFileID(raw json.RawMessage) (uuid.UUID, error)
ExtractFileID parses the file_id from a serialized file content block envelope. Returns uuid.Nil and an error when the block is not a file-type block or has no file_id.
func ExtractToolCalls ¶
func ExtractToolCalls(parts []fantasy.MessagePart) []fantasy.ToolCallContent
ExtractToolCalls returns all tool call parts as content blocks.
func InsertSystem ¶
InsertSystem inserts a system message after the existing system block and before the first non-system message.
func MarshalContent ¶
func MarshalContent(blocks []fantasy.Content, fileIDs map[int]uuid.UUID) (pqtype.NullRawMessage, error)
MarshalContent encodes message content blocks in legacy fantasy envelope format. Retained for backward-compatible test fixtures that create legacy-format DB rows. Production write paths use MarshalParts instead.
func MarshalParts ¶
func MarshalParts(parts []codersdk.ChatMessagePart) (pqtype.NullRawMessage, error)
MarshalParts encodes SDK chat message parts for persistence. NUL characters in string fields are encoded as PUA sentinel pairs (U+E000 U+E001) before marshaling so the resulting JSON never contains \u0000 (rejected by PostgreSQL jsonb). The encoding operates on Go string values, not JSON bytes, so it survives jsonb text normalization.
func MarshalToolResult ¶
func MarshalToolResult(toolCallID, toolName string, result json.RawMessage, isError bool, isMedia bool, providerExecuted bool, providerMetadata fantasy.ProviderMetadata) (pqtype.NullRawMessage, error)
MarshalToolResult encodes a single tool result in the legacy tool-row format. Retained for test fixtures that create legacy-format DB rows. Production write paths use MarshalParts. The stored shape is [{"tool_call_id":…,"tool_name":…,"result":…,"is_error":…,"is_media":…}].
func ParseContent ¶
func ParseContent(msg database.ChatMessage) ([]codersdk.ChatMessagePart, error)
ParseContent decodes persisted chat message content blocks into SDK parts. Dispatches on content version: version 0 (legacy) uses a role-aware heuristic to distinguish fantasy envelope format from SDK parts, version 1 (current) unmarshals SDK-format []ChatMessagePart directly.
func PartFromContent ¶
func PartFromContent(block fantasy.Content) codersdk.ChatMessagePart
PartFromContent converts fantasy content into a SDK chat message part, preserving ProviderMetadata and ProviderExecuted fields.
func PartFromContentWithLogger ¶ added in v2.33.0
func PartFromContentWithLogger( ctx context.Context, logger slog.Logger, block fantasy.Content, ) codersdk.ChatMessagePart
PartFromContentWithLogger is for call sites that can surface malformed attachment metadata immediately instead of dropping it silently.
func PrependSystem ¶
PrependSystem prepends a system message unless an existing system message already mentions create_workspace guidance.
func ToolResultToPart ¶
func ToolResultToPart(toolCallID, toolName string, result json.RawMessage, isError bool, isMedia bool) codersdk.ChatMessagePart
ToolResultToPart converts a tool call ID, raw result, error flag, and media flag into a ChatMessagePart. This is the minimal conversion used both during streaming and when reading from the database.