resource

package
v1.4.4 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: Apache-2.0 Imports: 25 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

Overview

Package resource has resources: content, such as images, documents and audio, which is uploaded, or which tools return, which is kept once, by its hash, and referred to as resource:///<hash>. A resource is in a session, which it's deleted with, or on its own, when it's deleted once it hasn't been used for a while.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clone

func Clone(resource schema.Resource) schema.Resource

Clone returns a copy of the resource, whose session is a copy, so changing it doesn't change the resource

Types

type Manager

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

Manager manages the resources, which are kept in a store. It's safe to use concurrently.

func New

func New(store Store, opts ...Opt) (*Manager, error)

New returns the resources in the store, with the options, such as the broadcaster they publish their changes to. Returns ErrBadParameter when there's no store, or an option isn't valid.

func (*Manager) Clean

func (r *Manager) Clean(ctx context.Context, logger *slog.Logger)

Clean deletes the resources on their own which weren't used for the retention, those whose session doesn't exist, with WithSessions, and then content which no resource refers to. What fails is logged.

func (*Manager) Create

func (r *Manager) Create(ctx context.Context, meta schema.ResourceMeta, content io.Reader) (_ schema.Resource, _ bool, err error)

Create keeps the content, as it's read, as a resource, and returns it, and whether it was created, rather than there being one with its content in its session already, which then has its name, description and type, when they're given, and is used again. Its type, when it isn't given, or is application/octet-stream, is its name's, or is detected from its content. Returns ErrNotFound when its session doesn't exist, *http.MaxBytesError when its content is larger than the most which is kept, ErrBadParameter when it isn't valid, and the store's errors.

func (*Manager) Delete

func (r *Manager) Delete(ctx context.Context, key schema.ResourceKey) (_ schema.Resource, err error)

Delete deletes the resource with the key, publishes that it was, and returns it, as it was. Its content is kept, as other resources may refer to it, until the resources are cleaned, when it's removed, once none does. Returns ErrNotFound when there's none.

func (*Manager) DeleteSession

func (r *Manager) DeleteSession(ctx context.Context, session uuid.UUID) (_ int, err error)

DeleteSession deletes the resources in the session, which was deleted, and returns how many were deleted. Their content is kept, until the resources are cleaned, as Delete keeps it.

func (*Manager) Get

func (r *Manager) Get(ctx context.Context, key schema.ResourceKey) (_ schema.Resource, err error)

Get returns the resource with the key, or ErrNotFound when there's none

func (r *Manager) Link(ctx context.Context, uri string, session uuid.UUID) (_ schema.Resource, err error)

Link links the resource with the URI, which is resource:///<hash>, which is on its own, to the session, as a resource of the session, with the one content, and what it is, so it's kept with the session, and returns it, or the session's, when it's linked to it already. Returns ErrBadParameter when the URI isn't a resource's, ErrNotFound when there's no resource on its own with it, or the session doesn't exist, and the store's errors.

func (*Manager) List

List returns the resources of the request's session, or every resource, in a session, or on its own, without one, the most recently used first, from its offset, and at most its limit of them, which is schema.DefaultListLimit, unless it says otherwise, up to schema.MaxListLimit, and how many there are. Returns ErrBadParameter when the session isn't a UUID.

func (*Manager) Open

func (r *Manager) Open(ctx context.Context, key schema.ResourceKey) (_ schema.Resource, _ io.ReadCloser, err error)

Open returns the resource with the key, and a reader of its content, which is closed when it's read, and it's used. Returns ErrNotFound when there's none.

func (*Manager) Resource

func (r *Manager) Resource(ctx context.Context, uri string, session *uuid.UUID) (llm.Resource, error)

Resource returns the resource with the URI, which is resource:///<hash>, in the session, or on its own, when it's nil, as an llm.Resource, whose content is read from the store, so a tool, or a model, can read it, and it's used. Returns ErrBadParameter when the URI isn't a resource's, and ErrNotFound when there's no resource with it in the session.

func (*Manager) Run

func (r *Manager) Run(ctx context.Context, logger *slog.Logger) error

Run deletes the resources of each session which is deleted, with a broadcaster, which unlinks their content, and deletes what isn't needed, when it starts, and every schema.CleanInterval: resources on their own which weren't used for the retention, those whose session doesn't exist, with WithSessions, as its event may have been missed, and content which no resource refers to, until the context is cancelled, when it returns nil. What fails is logged to the logger, when there is one.

type Opt

type Opt func(*opt) error

Opt is an option of the resources

func WithBroadcaster

func WithBroadcaster(b *broadcaster.Broadcaster) Opt

WithBroadcaster sets the broadcaster which the resources publish their changes to, whose schema is schema.Name, whose table is schema.ResourceTable, and whose rows are their hashes, with their sessions. Run watches it for the sessions which are deleted, whose resources are deleted with them.

func WithMaxSize

func WithMaxSize(size int64) Opt

WithMaxSize sets the largest content of a resource, in bytes, which is schema.DefaultMaxSize, unless it's set. Returns ErrBadParameter when it isn't positive.

func WithRetention

func WithRetention(d time.Duration) Opt

WithRetention sets how long a resource on its own, without a session, is kept, after it was last used, before it's deleted, which is a day, unless it's set. Returns ErrBadParameter when it isn't positive.

func WithSessions

func WithSessions(fn SessionFunc) Opt

WithSessions sets the function which says whether a session exists, so a resource is only created in one which does, and one whose session was deleted is deleted, such as when the event that it was deleted was missed

func WithTracer

func WithTracer(tracer trace.Tracer) Opt

WithTracer sets the tracer which traces the resources' changes, or none, when it's nil

type SessionFunc

type SessionFunc func(context.Context, uuid.UUID) error

SessionFunc returns nil when the session with the identifier exists, or ErrNotFound when it doesn't

type Store

type Store interface {
	// Write keeps the content, as it's read, and returns its hash, which is
	// its SHA-256, in hexadecimal, and its size. Content which is kept
	// already, by its hash, is kept once. Content which can't be read whole
	// isn't kept.
	Write(ctx context.Context, r io.Reader) (hash string, size int64, err error)

	// Open returns a reader of the content with the hash, which is closed
	// when it's read, or ErrNotFound when there's none
	Open(ctx context.Context, hash string) (io.ReadCloser, error)

	// Remove removes the content with the hash, or does nothing when there's
	// none
	Remove(ctx context.Context, hash string) error

	// Contents returns the hashes of the content which is kept, in any order
	Contents(ctx context.Context) ([]string, error)

	// Create adds the resource, or returns ErrConflict when there's one with
	// its key
	Create(context.Context, schema.Resource) error

	// Get returns the resource with the key, or ErrNotFound when there's none
	Get(context.Context, schema.ResourceKey) (schema.Resource, error)

	// List returns the resources, in any order
	List(context.Context) ([]schema.Resource, error)

	// Update calls the function with the resource with the key, which it
	// changes, and replaces it with what the function changed it to, apart
	// from its key, and returns it, or returns the function's error, without
	// changing it, or ErrNotFound when there's none
	Update(context.Context, schema.ResourceKey, func(*schema.Resource) error) (schema.Resource, error)

	// Delete deletes the resource with the key, and returns it, as it was,
	// or ErrNotFound when there's none. With a check, it's called with the
	// resource, as it is, and it's deleted only when the check returns nil,
	// so no other change, such as its being used, is made between them, or
	// the check's error is returned, without deleting it. Its content is
	// kept, until it's removed.
	Delete(ctx context.Context, key schema.ResourceKey, check func(schema.Resource) error) (schema.Resource, error)
}

Store keeps the resources, such as in files, or a database, so they're kept when the server restarts: their content, once, by its hash, and each resource, by its hash and session, which refers to its content

func NewStore

func NewStore() Store

NewStore returns a store which keeps the resources, and their content, in memory, so they aren't kept when the process ends, such as for tests, and examples

Directories

Path Synopsis
Package cmd has the resources' commands and flags
Package cmd has the resources' commands and flags
Package httpclient is a client of the resources' HTTP API
Package httpclient is a client of the resources' HTTP API
Package httphandler has the resources' HTTP API, which uploads, lists, gets, downloads and deletes them
Package httphandler has the resources' HTTP API, which uploads, lists, gets, downloads and deletes them
Package schema has the resources' schema: a resource's content, which is kept once, by its hash, and what it is, such as its name and type, which a session can have, or which is on its own
Package schema has the resources' schema: a resource's content, which is kept once, by its hash, and what it is, such as its name and type, which a session can have, or which is on its own

Jump to

Keyboard shortcuts

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