attachment

package
v1.125.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package attachment provides MIME-aware routing for document attachments.

It defines how a chat.Document should be sent to a model: either dropped (unsupported), wrapped in a plain-text envelope (StrategyTXT), or encoded as inline base64 data (StrategyB64).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TXTEnvelope

func TXTEnvelope(name, mimeType, body string) string

TXTEnvelope wraps text content in an XML-like tag derived from the document name and MIME type.

Example: a document named "report.md" with MIME "text/markdown" produces:

<document-report-md-text-markdown>
…body…
</document-report-md-text-markdown>

Delimiter safety

The tag is a deterministic slug of the name and MIME type, so it is NOT a secret: both inputs are routinely attacker-influenced (a downloaded file, a fetched page), which means the tag is predictable to whoever supplied the content. The body is therefore defused — any occurrence of this envelope's own delimiter inside it is replaced — so content cannot close the region early and make injected text appear to come from outside it.

The tag is deliberately kept deterministic rather than randomised per call: a per-call nonce would change the prompt prefix on every request and defeat provider prompt caching for the attachment.

Types

type Strategy

type Strategy int

Strategy describes how an attachment should be handled before sending to the provider.

const (
	// StrategyDrop means the attachment is not supported by the model or has no
	// inline content, and should be silently skipped (with a log warning).
	StrategyDrop Strategy = iota

	// StrategyTXT means the attachment should be wrapped in a TXTEnvelope and
	// sent as plain text.  Used for text/* MIME types whose content is already
	// in Source.InlineText.
	StrategyTXT

	// StrategyB64 means the attachment content (Source.InlineData) should be
	// base64-encoded and sent as a native provider image/document block.
	StrategyB64
)

func Decide

Decide returns the routing Strategy for a document given the current model's capabilities.

Algorithm:

  1. If the model does not support the document's MIME type → (Drop, reason).
  2. If Source.InlineData is non-empty → (B64, "").
  3. If Source.InlineText is non-empty → (TXT, "").
  4. Otherwise → (Drop, "no inline content").

Jump to

Keyboard shortcuts

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