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 ¶
- func Clone(resource schema.Resource) schema.Resource
- type Manager
- func (r *Manager) Clean(ctx context.Context, logger *slog.Logger)
- func (r *Manager) Copy(ctx context.Context, uri string, from, to uuid.UUID) (_ schema.Resource, err error)
- func (r *Manager) Create(ctx context.Context, meta schema.ResourceMeta, content io.Reader) (_ schema.Resource, _ bool, err error)
- func (r *Manager) Delete(ctx context.Context, key schema.ResourceKey) (_ schema.Resource, err error)
- func (r *Manager) DeleteSession(ctx context.Context, session uuid.UUID) (_ int, err error)
- func (r *Manager) Get(ctx context.Context, key schema.ResourceKey) (_ schema.Resource, err error)
- func (r *Manager) Link(ctx context.Context, uri string, session uuid.UUID) (_ schema.Resource, err error)
- func (r *Manager) List(ctx context.Context, req schema.ResourceListRequest) (_ schema.ResourceList, err error)
- func (r *Manager) Open(ctx context.Context, key schema.ResourceKey) (_ schema.Resource, _ io.ReadCloser, err error)
- func (r *Manager) Resource(ctx context.Context, uri string, session *uuid.UUID) (llm.Resource, error)
- func (r *Manager) Run(ctx context.Context, logger *slog.Logger) error
- type Opt
- type SessionFunc
- type Store
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
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 ¶
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) Copy ¶ added in v1.5.0
func (r *Manager) Copy(ctx context.Context, uri string, from, to uuid.UUID) (_ schema.Resource, err error)
Copy copies the resource with the URI, of the session it's from, to the other session, so it's kept with it too, and returns it, as the other session's, or the other session's, when it has it already. Its content isn't copied, as they have the one content. Returns ErrNotFound when the session it's from has no resource with it, or the other session doesn't exist.
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 ¶
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) Link ¶
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 on its own, to the session, so it's kept with it, and returns it, as the session's, or the session's, when it has it already. Returns ErrNotFound when there's no resource on its own with it, or the session doesn't exist.
func (*Manager) List ¶
func (r *Manager) List(ctx context.Context, req schema.ResourceListRequest) (_ schema.ResourceList, err error)
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 ¶
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 ¶
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 ¶
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 ¶
WithTracer sets the tracer which traces the resources' changes, or none, when it's nil
type SessionFunc ¶
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
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 |