files

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: BSD-2-Clause Imports: 19 Imported by: ? modules, 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

Overview

Package files resolves the file URLs in an LLM context into bytes the provider can consume, and stores the files clients upload to be referenced by URL later.

A file reaches the context as a URL: jargo:<id> from an upload a Storage saved, gs:// or s3:// from cloud storage, or plain http(s). Whether that URL can be passed to the LLM provider as it is, or must first be fetched by this server and inlined as bytes, depends on the provider (each adapter declares what it can consume) and on who can reach the URL. A Resolver is the fetching half of that decision.

Index

Constants

View Source
const (
	// ReachabilityPublic is a publicly routable host: safe to hand to a third
	// party to fetch itself.
	ReachabilityPublic = security.ReachabilityPublic
	// ReachabilityAllowed is a host within a trusted private network: safe for
	// this server to fetch directly, but not to hand off to a third party that
	// cannot reach it.
	ReachabilityAllowed = security.ReachabilityAllowed
	// ReachabilityBlocked is a host nobody should fetch.
	ReachabilityBlocked = security.ReachabilityBlocked
)
View Source
const (
	// DefaultMaxFetchBytes is the largest file a Resolver fetches unless told
	// otherwise.
	DefaultMaxFetchBytes = 50 * 1024 * 1024
	// DefaultFetchTimeout is the total time a Resolver gives an http(s) fetch
	// unless told otherwise.
	DefaultFetchTimeout = 30 * time.Second
)
View Source
const DefaultMaxFiles = 10

DefaultMaxFiles is how many uploads a LocalStorage keeps unless told otherwise.

Variables

This section is empty.

Functions

This section is empty.

Types

type LocalStorage

type LocalStorage struct {
	// contains filtered or unexported fields
}

LocalStorage is the default Storage: files on local disk, addressed as jargo:<hex>.

It suits local development and single-process deployments. The jargo: scheme makes the URLs recognizable in logs and client payloads; the random hex suffix cannot be used for path traversal and does not leak the original filename.

func NewLocalStorage

func NewLocalStorage(folder string, maxFiles int) *LocalStorage

NewLocalStorage stores files in folder, created on the first save if missing. It keeps the maxFiles most recent uploads, deleting the oldest (by modification time) beyond that; zero disables the trimming.

func (*LocalStorage) Delete

func (s *LocalStorage) Delete(_ context.Context, fileURL string) error

Delete deletes the file stored under fileURL, if it exists. A failure to remove it is logged.

func (*LocalStorage) DeleteAfterLoad

func (s *LocalStorage) DeleteAfterLoad() bool

DeleteAfterLoad reports true: every jargo: URL is an upload this backend owns, so the file is removed once its bytes have been consumed into the LLM context.

func (*LocalStorage) Load

func (s *LocalStorage) Load(_ context.Context, fileURL string) ([]byte, error)

Load reads and returns the contents stored under fileURL.

func (*LocalStorage) Save

func (s *LocalStorage) Save(_ context.Context, _ string, contents []byte) (string, error)

Save writes contents to a new randomly named file and returns its jargo: URL.

type Reachability

type Reachability = security.URLReachability

Reachability says who can safely reach a URL's host.

type Resolver

type Resolver struct {
	// contains filtered or unexported fields
}

Resolver fetches and caches the bytes behind the file URLs an LLM provider cannot fetch itself.

It handles three kinds of URL:

  • data: URLs are decoded directly.
  • http(s) URLs are fetched by this server, but only when the resolved address is publicly routable or falls within the allowed networks: a client-supplied URL must not be able to point this server at arbitrary private address space (SSRF). Redirects are refused rather than followed.
  • Any other scheme is delegated to the Storage, which resolves the URLs it minted (jargo:<id> for a LocalStorage; a custom backend resolves whatever its Save returned, gs:// with the deployment's own credentials, say).

The context itself is never modified: everything the resolver produces (downloaded bytes, base64 data URLs, reachability verdicts) is held in in-memory caches keyed by URL, where adapters read it during context conversion. The caches make resolved files provider-neutral: LLM services that share one resolver (services switched between mid-session, say) reuse each other's downloads, deciding again only whether a URL can be passed through to their own provider.

Each URL is fetched once and served from the cache afterwards: a URL is assumed to identify one immutable piece of content, the assumption the rest of the web's caches make. Content that changes should live at a new URL, or use Forget when the application knows a URL's content changed. The URLs a provider consumes directly are fetched by the provider itself, on its own schedule, so their freshness is out of this resolver's hands.

A stored file whose backend reports DeleteAfterLoad is deleted only after its bytes are cached, so the cache entry becomes the surviving copy. That is why LLM services that may consume the same files should share one resolver rather than each building their own. Scope a resolver to one session: one shared across sessions would hold every session's files in memory and stretch the one-fetch-per-URL assumption across all of them.

func NewResolver

func NewResolver(cfg ResolverConfig) (*Resolver, error)

NewResolver builds a resolver. It reports an error when an allowed network is not a valid CIDR range.

func (*Resolver) CachedBytes

func (r *Resolver) CachedBytes(url string) ([]byte, bool)

CachedBytes returns the cached bytes for url, and whether it has been fetched.

func (*Resolver) CachedDataURL

func (r *Resolver) CachedDataURL(url string) (string, bool)

CachedDataURL returns the cached data: form of url, and whether it has been prepared. A data: URL is its own data-URL form.

func (*Resolver) CachedReachability

func (r *Resolver) CachedReachability(url string) (Reachability, bool)

CachedReachability returns the cached reachability verdict for url, and whether it has been classified.

func (*Resolver) Classify

func (r *Resolver) Classify(ctx context.Context, url string) Reachability

Classify classifies who can reach an http(s) URL, honoring the allowed networks. Each URL is classified once; later calls return the cached verdict.

func (*Resolver) EnsureDataURL

func (r *Resolver) EnsureDataURL(ctx context.Context, url, mimeType string) (string, error)

EnsureDataURL returns the data: form of url's bytes, encoding and caching it on first use. It is for adapters whose provider consumes base64 rather than raw bytes, so the encoding happens once rather than on every context conversion. mimeType is the file's MIME type, used in the data URL.

func (*Resolver) Fetch

func (r *Resolver) Fetch(ctx context.Context, url string) ([]byte, error)

Fetch returns the bytes behind url, fetching and caching them on first use.

url is a data: URL, an http(s) URL, or a URL the storage backend minted. A stored file is deleted after a successful load when the backend reports DeleteAfterLoad; the bytes are cached first, so the cache entry becomes the surviving copy.

It reports a *ResolverError when the URL is refused by the reachability policy, exceeds the size limit, fails to download, or has a scheme nothing is configured to resolve.

func (*Resolver) Forget

func (r *Resolver) Forget(url string)

Forget drops everything cached for url, forcing a fetch again on its next use. It is for applications that know a URL's content has changed. A forgotten upload whose backend deletes after load cannot be fetched again, since its stored copy was deleted when first cached, so forgetting one makes it unresolvable.

func (*Resolver) Storage

func (r *Resolver) Storage() Storage

Storage is the backend used to resolve the URLs it minted, if any.

type ResolverConfig

type ResolverConfig struct {
	// Storage resolves the URLs it minted (jargo:<id> from a LocalStorage).
	// Without it, only data: and http(s) URLs can be resolved.
	Storage Storage
	// AllowedURLNetworks are CIDR ranges (for example "10.0.0.0/8") this server
	// is trusted to fetch from, in addition to the public internet. An http(s)
	// URL that resolves outside both is refused. Nil means the ranges the
	// JARGO_ALLOWED_FILE_URL_NETWORKS environment variable names
	// (comma-separated); an empty, non-nil slice trusts the public internet
	// only.
	AllowedURLNetworks []string
	// MaxFetchBytes is the largest file fetched; zero means
	// DefaultMaxFetchBytes.
	MaxFetchBytes int64
	// FetchTimeout is the total timeout of an http(s) fetch; zero means
	// DefaultFetchTimeout.
	FetchTimeout time.Duration
	// Fetch, when set, replaces how a URL not cached yet is fetched, to support
	// additional schemes or credentialed fetches beyond what the storage
	// backend provides. The bytes it returns are cached like any others.
	Fetch func(ctx context.Context, url string) ([]byte, error)
	// Classify, when set, replaces how an http(s) URL not classified yet is
	// classified, for a deployment that decides reachability its own way. The
	// verdict is cached like any other.
	Classify func(ctx context.Context, url string) Reachability
}

ResolverConfig configures a Resolver.

type ResolverError

type ResolverError struct {
	// contains filtered or unexported fields
}

ResolverError reports a file URL in the LLM context that cannot be resolved to bytes.

func (*ResolverError) Error

func (e *ResolverError) Error() string

func (*ResolverError) Unwrap

func (e *ResolverError) Unwrap() error

type Storage

type Storage interface {
	// Save stores a file and returns the URL later passed to Load or Delete.
	// filename is the original name, for backends that want to keep it (for
	// content-type sniffing, say); it should not be used to derive the URL.
	Save(ctx context.Context, filename string, contents []byte) (string, error)
	// Load returns the stored contents for fileURL, a URL Save returned. An
	// invalid URL, or one no longer stored, is reported with an error wrapping
	// fs.ErrNotExist.
	Load(ctx context.Context, fileURL string) ([]byte, error)
	// Delete removes the stored file for fileURL, if present.
	Delete(ctx context.Context, fileURL string) error
	// DeleteAfterLoad reports whether a file should be deleted once its bytes
	// have been consumed (loaded and inlined into the LLM context by a
	// Resolver, which then has no further use for the stored copy). It is safe
	// only when every URL the backend resolves is an upload it owns; a backend
	// whose URLs may point at shared data (arbitrary objects in a cloud bucket)
	// must report false, since reading a file into a conversation must not
	// destroy it.
	DeleteAfterLoad() bool
}

Storage is a storage backend for files uploaded by clients and referenced by URL later.

It is the interface shared between whatever saves uploads and hands the resulting URL back to the client, and the Resolver of the LLM service, which loads the bytes back when the client references that URL. Passing the same storage to both means they agree on where uploads live, and a deployment can swap in a backend other than local disk by implementing it.

The URL is the contract: Save returns one, callers pass it back unmodified, and each implementation mints whatever form it can later resolve: jargo:<id> for LocalStorage, or for example gs:// or s3:// for a cloud backend (which the LLM provider may then even fetch itself, without Load being involved at all).

Jump to

Keyboard shortcuts

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