swarm

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2025 License: MIT Imports: 22 Imported by: 0

README

Swarm

A lightweight multi-agent orchestration.

Swarm Magentic-One

Documentation

Overview

Adapted from https://github.com/justinas/alice/blob/master/chain.go

Index

Constants

This section is empty.

Variables

View Source
var ErrHandlerTimeout = errors.New("Agent service timeout")

ErrHandlerTimeout is returned on [Response] in handlers which have timed out.

Functions

func MaxLogHandler

func MaxLogHandler(n int) func(Handler) Handler

MaxLogHandler returns a Handler that logs the request and response

func NewAgentHandler added in v0.1.0

func NewAgentHandler(sw *Swarm) func(*api.Vars, *api.Agent) Handler

func NewBlobID added in v0.4.0

func NewBlobID() string

func NewToolCaller added in v0.1.0

func NewToolCaller(sw *Swarm) api.ToolCaller

Types

type BlobStorage added in v0.4.0

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

a simple in memory object store

func NewBlobStorage added in v0.4.0

func NewBlobStorage(fs vfs.FileSystem) *BlobStorage

func (*BlobStorage) Get added in v0.4.0

func (r *BlobStorage) Get(ID string) (*api.Blob, error)

func (*BlobStorage) Put added in v0.4.0

func (r *BlobStorage) Put(ID string, blob *api.Blob) error

type Chain

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

Chain acts as a list of Handler constructors. Chain is effectively immutable: once created, it will always hold the same set of constructors in the same order.

func NewChain

func NewChain(constructors ...Constructor) Chain

New creates a new chain, memorizing the given list of middleware constructors. New serves no other function, constructors are only called upon a call to Then().

func (Chain) Append

func (c Chain) Append(constructors ...Constructor) Chain

Append extends a chain, adding the specified constructors as the last ones in the request flow.

Append returns a new chain, leaving the original one untouched.

stdChain := alice.New(m1, m2)
extChain := stdChain.Append(m3, m4)
// requests in stdChain go m1 -> m2
// requests in extChain go m1 -> m2 -> m3 -> m4

func (Chain) Extend

func (c Chain) Extend(chain Chain) Chain

Extend extends a chain by adding the specified chain as the last one in the request flow.

Extend returns a new chain, leaving the original one untouched.

stdChain := alice.New(m1, m2)
ext1Chain := alice.New(m3, m4)
ext2Chain := stdChain.Extend(ext1Chain)
// requests in stdChain go  m1 -> m2
// requests in ext1Chain go m3 -> m4
// requests in ext2Chain go m1 -> m2 -> m3 -> m4

Another example:

aHtmlAfterNosurf := alice.New(m2)
aHtml := alice.New(m1, func(h Handler) Handler {
	csrf := nosurf.New(h)
	csrf.SetFailureHandler(aHtmlAfterNosurf.ThenFunc(csrfFail))
	return csrf
}).Extend(aHtmlAfterNosurf)
// requests to aHtml hitting nosurfs success handler go m1 -> nosurf -> m2 -> target-handler
// requests to aHtml hitting nosurfs failure handler go m1 -> nosurf -> m2 -> csrfFail

func (Chain) Then

func (c Chain) Then(h Handler) Handler

Then chains the middleware and returns the final Handler.

New(m1, m2, m3).Then(h)

is equivalent to:

m1(m2(m3(h)))

When the request comes in, it will be passed to m1, then m2, then m3 and finally, the given handler (assuming every middleware calls the following one).

A chain can be safely reused by calling Then() several times.

stdStack := alice.New(ratelimitHandler, csrfHandler)
indexPipe = stdStack.Then(indexHandler)
authPipe = stdStack.Then(authHandler)

Note that constructors are called on every call to Then() and thus several instances of the same middleware will be created when a chain is reused in this way. For proper middleware, this should cause no problems.

Then() treats nil as DefaultServeMux.

func (Chain) ThenFunc

func (c Chain) ThenFunc(fn HandlerFunc) Handler

ThenFunc works identically to Then, but takes a HandlerFunc instead of a Handler.

The following two statements are equivalent:

c.Then(HandlerFunc(fn))
c.ThenFunc(fn)

ThenFunc provides all the guarantees of Then.

type Constructor

type Constructor func(Handler) Handler

A constructor for a piece of middleware. Some middleware use this constructor out of the box, so in most cases you can just pass somepackage.New

type Handler

type Handler = api.Handler

func TimeoutHandler

func TimeoutHandler(next Handler, dt time.Duration, msg string) Handler

TimeoutHandler returns a Handler that times out if the time limit is reached.

The new Handler calls thext next handler's Serve to handle each request, but if a call runs for longer than its time limit, the handler responds with a timeout error.

type HandlerFunc

type HandlerFunc func(*api.Request, *api.Response) error

The HandlerFunc type is an adapter to allow the use of ordinary functions as handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler that calls f.

func (HandlerFunc) Serve

func (f HandlerFunc) Serve(r *api.Request, w *api.Response) error

Serve calls f(w, r).

type Swarm

type Swarm struct {
	Vars *api.Vars

	User *api.User

	Secrets api.SecretStore
	Assets  api.AssetManager
	Tools   api.ToolSystem

	Adapters llm.AdapterRegistry

	Blobs api.BlobStore

	OS vos.System
	FS vfs.FileSystem
}

func (*Swarm) Run

func (r *Swarm) Run(req *api.Request, resp *api.Response) error

Directories

Path Synopsis
atm
llm
tool
web
web/ddg
https://github.com/tmc/langchaingo/blob/main/tools/duckduckgo/internal/client.go https://github.com/tmc/langchaingo?tab=MIT-1-ov-file#readme
https://github.com/tmc/langchaingo/blob/main/tools/duckduckgo/internal/client.go https://github.com/tmc/langchaingo?tab=MIT-1-ov-file#readme
web/scrape
https://github.com/tmc/langchaingo/blob/main/tools/scraper/scraper.go https://github.com/tmc/langchaingo?tab=MIT-1-ov-file#readme
https://github.com/tmc/langchaingo/blob/main/tools/scraper/scraper.go https://github.com/tmc/langchaingo?tab=MIT-1-ov-file#readme

Jump to

Keyboard shortcuts

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