chatprompt

package
v2.37.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: AGPL-3.0 Imports: 19 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
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
)
View Source
const TitlePasteBytePrefix = 4 * syntheticPasteTitleBudget

TitlePasteBytePrefix caps, in bytes, how much of a pasted-text blob feeds title derivation: four bytes per rune (the UTF-8 maximum) covers syntheticPasteTitleBudget runes. Database callers pass it to GetChatFileDataPrefixesByIDs to bound the fetch itself.

Variables

This section is empty.

Functions

func ConvertMessagesWithFiles

func ConvertMessagesWithFiles(
	ctx context.Context,
	messages []database.ChatMessage,
	resolver FileResolver,
	logger slog.Logger,
	acceptsFilePart func(mediaType string) bool,
) ([]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. acceptsFilePart, when non-nil, gates whether text-family file parts are inlined as text for providers that would drop them; nil preserves them as FilePart.

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 FallbackTitle added in v2.36.0

func FallbackTitle(message string) string

FallbackTitle derives a deterministic chat title from title text: the first six words, ellipsized when truncated, capped at 80 runes. Empty input yields "New Chat".

func FileIDs added in v2.37.0

func FileIDs(parts []codersdk.ChatMessagePart) []uuid.UUID

FileIDs returns the valid file IDs referenced by file parts.

func InsertSystem

func InsertSystem(prompt []fantasy.Message, instruction string) []fantasy.Message

InsertSystem inserts a system message after the existing system block and before the first non-system message.

func IsSyntheticPaste added in v2.36.0

func IsSyntheticPaste(name string, mediaType string) bool

IsSyntheticPaste reports whether a file name and media type identify a synthetic pasted-text attachment created by the chat UI.

TODO: Replace filename-based detection with explicit origin metadata.

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 free-form and opaque fields are encoded as PUA sentinel pairs (U+E000 U+E001) so PostgreSQL jsonb can store them. Structured fields such as identifiers, paths, and media types reject NUL because their downstream representations cannot consume it.

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 SyntheticPasteFileIDs added in v2.36.0

func SyntheticPasteFileIDs(parts []codersdk.ChatMessagePart) []uuid.UUID

SyntheticPasteFileIDs returns the file IDs of file parts that are synthetic pasted-text attachments created by the chat UI. Callers resolve these to file content and pass the result to TitleText.

func TitlePasteText added in v2.36.0

func TitlePasteText(data []byte) string

TitlePasteText converts pasted-text content to TitleText input, copying at most TitlePasteBytePrefix bytes. Every caller that builds a pasteText map must use it so all derivation paths feed TitleText identical strings.

func TitleText added in v2.36.0

func TitleText(parts []codersdk.ChatMessagePart, pasteText map[uuid.UUID]string) string

TitleText derives title-generation input from message parts. Text and file-reference parts are joined in part order. When they yield nothing, the content of synthetic pasted-text attachments is used instead, looked up in pasteText by file ID and truncated to syntheticPasteTitleBudget runes per file.

The chat-creation fallback title and both title-generation paths must derive their input through this function: auto-titling only proceeds when the current title equals FallbackTitle of this exact string, so a drift between derivations silently disables it.

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.

Types

type FileData

type FileData struct {
	Name      string
	Data      []byte
	MediaType string
}

FileData holds resolved file content for LLM prompt building.

type FileResolver

type FileResolver func(ctx context.Context, ids []uuid.UUID) (map[uuid.UUID]FileData, error)

FileResolver fetches file content by ID for LLM prompt building.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL