secai

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2025 License: MIT Imports: 21 Imported by: 0

README

AI-gent Workflows

AI-gent Workflows (aka secai) is a platform for AI Agents with a local reasoning layer. It's implemented on top of a unified state graph and makes a solid foundation for complex, proactive, and long-lived AI Agents with deep and structured memory. It offers a dedicated set of devtools and is written in the Go programming language. By having graph-based flow, secai allows for precise behavior modeling of agents, including interruptions and fault tolerance.

User Demo

Screenshots and YouTube are also available.

[!NOTE] This user demo is a 7min captions-only presentation, showcasing a cooking assistant which helps to pick a recipe from ingredients AND cook it.

Platform Demo

Screenshots and YouTube are also available.

[!NOTE] This tech demo is a 5min captions-only screencast, showcasing all 9 ways an agent can be seen, in addition to the classic chat view.

Features

  • prompt atomicity on the state level
    • each state can have a prompt bound to it, with dedicated history and documents
  • atomic consensus with relations and negotiation
    • states excluding each other can't be active simultaneously
  • separate schema DSL layer
    • suitable for non-coding authors
  • declarative flow definitions
    • for non-linear flows
  • cancellation support (interrupts)
  • offer list / menu
  • prompt history
    • in SQL (embedded SQLite)
    • in JSONL (stdout)
  • proactive stories with actors
  • LLM triggers (orienting)
    • on prompts and timeouts
  • dynamic flow graph for the memory
    • LLM creates an actionable state machine
  • UI components
    • layouts (zellij)
    • chat (tview)
    • stories (cview)
    • clock (bubbletea)
  • platforms
    • SSH (all platforms)
    • Desktop PWA (all platforms)
    • Mobile PWA (basic)
Devtools

The following devtools are for the agent, the agent's dynamic memory, and tools (all of which are the same type of state machine).

  • REPL & CLI
  • TUI debugger (dashboards)
  • automatic diagrams (SVG, D2, mermaid)
  • automatic observability (Prometheus, Grafana, Jaeger)
Tools
Planned
  • lambda prompts (unbound)
    • based on langchaingo
  • MCP (both relay and tool)
  • history DSL with a vector format (WIP)
  • agent contracts
  • i18n
  • Gemini via direct SDK
  • ML triggers
    • based on local neural networks
  • mobile and WASM builds
  • support local LLMs (eg iOS)
  • desktop apps
  • dynamic tools
    • LLM creates tools on the fly
  • prompts as RSS

Implementation

  • pure Golang
  • typesafe state-machine and prompt schemas
  • asyncmachine-go for graphs and control flow
  • instructor-go for the LLM layer
    • OpenAI, DeepSeek, Anthropic, Cohere (soon Gemini)
  • network transparency (aRPC, debugger, REPL)
  • structured concurrency (multigraph-based)
  • tview, cview, and asciigraph for UIs
Components
  • Agent (actor)
    • state-machine schema
    • prompts
    • tools
  • Tool (actor)
    • state-machine schema
  • Memory
    • state-machine schema
  • Prompt (state)
    • params schema
    • result schema
    • history log
    • documents
  • Stories (state)
    • actors
    • state machines
  • Document
    • title
    • content

Comparison

Feature AI-gent Workflows AtomicAgents
Model unified state graph BaseAgent class
Debugger multi-client with time travel X
Diagrams customizable level of details X
Observability logging & Grafana & Otel X
REPL & CLI network-based X
History state-based and prompt-based prompt-based
Pkg manager Golang in-house
Control Flow declarative & fault tolerant imperative
CLI bubbletea, lipgloss rich
TUI tview, cview textual
Go vs Python
  • just works, batteries included, no magic
  • 1 package manager vs 4
  • single binary vs interpreted multi-file source
  • coherent static typing vs maybe
  • easy & stable vs easy
  • no ecosystem fragmentation
  • million times faster /s
  • relevant xkcd

Try It

Unlike Python apps, you can start it with a single command:

  • Download a binary release (Linux, MacOS, Windows)
  • Set either of the API keys:
    • export OPENAI_API_KEY=myapikey
    • export DEEPSEEK_API_KEY=myapikey
  • Run ./aigent-cook or ./aigent-research to start the server
    • then copy-paste-run the TUI Desktop line in another terminal
    • you'll see files being created in ./tmp
aigent-cook v0.2

TUI Chat:
$ ssh chat@localhost -p 7854 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no

TUI Stories:
$ ssh stories@localhost -p 7854 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no

TUI Clock:
$ ssh clock@localhost -p 7854 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no

TUI Desktop:
$ bash <(curl -L https://zellij.dev/launch) --layout $(./aigent-cook desktop-layout) attach secai-aigent-cook --create

https://ai-gents.work

{"time":"2025-06-25T11:59:28.421964349+02:00","level":"INFO","msg":"SSH UI listening","addr":"localhost:7854"}
{"time":"2025-06-25T11:59:29.779618008+02:00","level":"INFO","msg":"output phrase","key":"IngredientsPicking"}

Example

Code snippets from /examples/research (ported from AtomicAgents). Both the state and prompt schemas are pure and debuggable Golang code.

State Schema
// ResearchStatesDef contains all the states of the Research state machine.
type ResearchStatesDef struct {
    *am.StatesBase

    CheckingInfo string
    NeedMoreInfo string

    SearchingLLM string
    SearchingWeb string
    Scraping     string

    Answering string
    Answered  string

    *ss.AgentStatesDef
}

// ResearchGroupsDef contains all the state groups Research state machine.
type ResearchGroupsDef struct {
    Info    S
    Search  S
    Answers S
}

// ResearchSchema represents all relations and properties of ResearchStates.
var ResearchSchema = SchemaMerge(
    // inherit from Agent
    ss.AgentSchema,

    am.Schema{

        // Choice "agent"
        ssR.CheckingInfo: {
            Require: S{ssR.Start, ssR.Prompt},
            Remove:  sgR.Info,
        },
        ssR.NeedMoreInfo: {
            Require: S{ssR.Start},
            Add:     S{ssR.SearchingLLM},
            Remove:  sgR.Info,
        },

        // Query "agent"
        ssR.SearchingLLM: {
            Require: S{ssR.NeedMoreInfo, ssR.Prompt},
            Remove:  sgR.Search,
        },
        ssR.SearchingWeb: {
            Require: S{ssR.NeedMoreInfo, ssR.Prompt},
            Remove:  sgR.Search,
        },
        ssR.Scraping: {
            Require: S{ssR.NeedMoreInfo, ssR.Prompt},
            Remove:  sgR.Search,
        },

        // Q&A "agent"
        ssR.Answering: {
            Require: S{ssR.Start, ssR.Prompt},
            Remove:  SAdd(sgR.Info, sgR.Answers),
        },
        ssR.Answered: {
            Require: S{ssR.Start},
            Remove:  SAdd(sgR.Info, sgR.Answers, S{ssR.Prompt}),
        },
    })

var sgR = am.NewStateGroups(ResearchGroupsDef{
    Info:    S{ssR.CheckingInfo, ssR.NeedMoreInfo},
    Search:  S{ssR.SearchingLLM, ssR.SearchingWeb, ssR.Scraping},
    Answers: S{ssR.Answering, ssR.Answered},
})
Prompt Schema
func NewCheckingInfoPrompt(agent secai.AgentApi) *secai.Prompt[ParamsCheckingInfo, ResultCheckingInfo] {
    return secai.NewPrompt[ParamsCheckingInfo, ResultCheckingInfo](
        agent, ssR.CheckingInfo, `
            - You are a decision-making agent that determines whether a new web search is needed to answer the user's question.
            - Your primary role is to analyze whether the existing context contains sufficient, up-to-date information to
            answer the question.
            - You must output a clear TRUE/FALSE decision - TRUE if a new search is needed, FALSE if existing context is
            sufficient.
        `, `
            1. Analyze the user's question to determine whether or not an answer warrants a new search
            2. Review the available web search results 
            3. Determine if existing information is sufficient and relevant
            4. Make a binary decision: TRUE for new search, FALSE for using existing context
        `, `
            Your reasoning must clearly state WHY you need or don't need new information
            If the web search context is empty or irrelevant, always decide TRUE for new search
            If the question is time-sensitive, check the current date to ensure context is recent
            For ambiguous cases, prefer to gather fresh information
            Your decision must match your reasoning - don't contradict yourself
        `)
}

// CheckingInfo (Choice "agent")

type ParamsCheckingInfo struct {
    UserMessage  string
    DecisionType string
}

type ResultCheckingInfo struct {
    Reasoning string `jsonschema:"description=Detailed explanation of the decision-making process"`
    Decision  bool   `jsonschema:"description=The final decision based on the analysis"`
}

Read the schema file in full.

Screenshots

Screenshots User Demo
Intro AI-gent Cook Debugger 1 Debugger 2 Memory & Stories
User Interfaces Outro      
     
State Schema
Screenshots Platform Demo
SVG graph am-dbg Grafana Jaeger REPL
SQL IDE Bash Prompts  
 
State Schema
Screenshots Dashboards
Dashboard 1
Dashboard 2

Documentation

Getting Started

We can use one of the examples as a starting template. It allows for further semver updates of the base framework.

  1. Choose the source example
    • export SECAI_EXAMPLE=cook
    • export SECAI_EXAMPLE=research
  2. git clone https://github.com/pancsta/secai.git
  3. install task ./secai/scripts/deps.sh
  4. copy the agent cp -R secai/examples/$SECAI_EXAMPLE MYAGENT
  5. cd MYAGENT && go mod init github.com/USER/MYAGENT
  6. get fresh configs
    1. task sync-taskfile
    2. task sync-configs
  7. start it task start
  8. look around task --list-all
  9. configure cp template.env .env

User Interfaces

Several TUIs with dedicated UI states are included in /tui:

Chat TUI
  • senders & msgs scrollable view with links
  • multiline prompt with blocking and progress
  • send / stop button
Stories TUI
  • list of stories with activity status, non-actionable
  • dynamic buttons and progress bars, actionable
Clockmoji TUI

Bash Scripts

arpc offers CLI access to remote agents, including subscription. It's perfect for quick and simple integrations, scripts, or experiments.

Example: arpc -f tmp/research.addr -- when . Requesting && echo "REQUESTING"

  1. Connect to the address from tmp/research.addr
  2. When the last connected agent (.) goes into state Requesting
  3. Print "REQUESTING" and exit

Acknowledgements

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ParseArgs = shared.ParseArgs
View Source
var Pass = shared.Pass

Functions

func ToolAddToPrompts

func ToolAddToPrompts(t ToolApi, prompts ...PromptApi)

Types

type A added in v0.2.0

type A = shared.A

type Agent

type Agent struct {
	*am.ExceptionHandler
	*ssam.DisposedHandlers

	// UserInput is a prompt submitted the user, owned by [schema.AgentStatesDef.Prompt].
	UserInput string
	// OfferList is a list of choices for the user.
	// TODO atomic?
	OfferList []string

	// Messages
	Msgs []*shared.Msg
	// contains filtered or unexported fields
}

func NewAgent added in v0.2.0

func NewAgent(
	ctx context.Context, id string, states am.S, machSchema am.Schema,
) *Agent

TODO config

func (*Agent) BaseDBReadyEnd added in v0.2.0

func (a *Agent) BaseDBReadyEnd(e *am.Event)

func (*Agent) BaseDBSavingEnter added in v0.2.0

func (a *Agent) BaseDBSavingEnter(e *am.Event) bool

func (*Agent) BaseDBSavingState added in v0.2.0

func (a *Agent) BaseDBSavingState(e *am.Event)

func (*Agent) BaseDBStartingState added in v0.2.0

func (a *Agent) BaseDBStartingState(e *am.Event)

func (*Agent) BaseQueries added in v0.2.0

func (a *Agent) BaseQueries() *db.Queries

func (*Agent) BuildOffer added in v0.2.0

func (a *Agent) BuildOffer() string

func (*Agent) CheckingOfferRefsEnter added in v0.2.0

func (a *Agent) CheckingOfferRefsEnter(e *am.Event) bool

func (*Agent) CheckingOfferRefsState added in v0.2.0

func (a *Agent) CheckingOfferRefsState(e *am.Event)

func (*Agent) Init added in v0.2.0

func (a *Agent) Init(agent AgentAPI) error

Init initializes the Agent and returns an error. It does not block.

func (*Agent) InterruptedState added in v0.2.0

func (a *Agent) InterruptedState(e *am.Event)

func (*Agent) Log

func (a *Agent) Log(txt string, args ...any)

Log will push a log entry to Logger as Info() and optionally the machine log with SECAI_AM_LOG. Log accepts the same convention of arguments as slog.Info.

func (*Agent) Logger added in v0.2.0

func (a *Agent) Logger() *slog.Logger

func (*Agent) Mach

func (a *Agent) Mach() *am.Machine

func (*Agent) MsgEnter added in v0.2.0

func (a *Agent) MsgEnter(e *am.Event) bool

func (*Agent) OpenAI

func (a *Agent) OpenAI() *instructor.InstructorOpenAI

func (*Agent) Output

func (a *Agent) Output(txt string, from shared.From) am.Result

Output is a sugar for adding a schema.AgentStatesDef.Msg mutation.

func (*Agent) PromptEnd

func (a *Agent) PromptEnd(e *am.Event)

func (*Agent) PromptEnter added in v0.2.0

func (a *Agent) PromptEnter(e *am.Event) bool

func (*Agent) PromptState

func (a *Agent) PromptState(e *am.Event)

func (*Agent) RequestingExit

func (a *Agent) RequestingExit(e *am.Event) bool

func (*Agent) RequestingLLMEnd

func (a *Agent) RequestingLLMEnd(e *am.Event)

func (*Agent) RequestingLLMEnter

func (a *Agent) RequestingLLMEnter(e *am.Event) bool

func (*Agent) RequestingLLMExit

func (a *Agent) RequestingLLMExit(e *am.Event) bool

func (*Agent) RequestingToolEnd

func (a *Agent) RequestingToolEnd(e *am.Event)

func (*Agent) RequestingToolEnter

func (a *Agent) RequestingToolEnter(e *am.Event) bool

func (*Agent) RequestingToolExit

func (a *Agent) RequestingToolExit(e *am.Event) bool

func (*Agent) ResumeState added in v0.2.0

func (a *Agent) ResumeState(e *am.Event)

func (*Agent) SetMach

func (a *Agent) SetMach(m *am.Machine)

func (*Agent) SetOpenAI

func (a *Agent) SetOpenAI(c *instructor.InstructorOpenAI)

func (*Agent) Start

func (a *Agent) Start() am.Result

Start is a sugar for adding a schema.AgentStatesDef.Start mutation.

func (*Agent) StartEnter

func (a *Agent) StartEnter(e *am.Event) bool

func (*Agent) StartState

func (a *Agent) StartState(e *am.Event)

func (*Agent) Stop

func (a *Agent) Stop(disposeCtx context.Context) am.Result

type AgentAPI added in v0.2.0

type AgentAPI interface {
	Output(txt string, from shared.From) am.Result

	Mach() *am.Machine
	SetMach(*am.Machine)

	SetOpenAI(c *instructor.InstructorOpenAI)
	OpenAI() *instructor.InstructorOpenAI

	Start() am.Result
	Stop(disposeCtx context.Context) am.Result
	Log(txt string, args ...any)
	Logger() *slog.Logger

	BaseQueries() *db.Queries
}

AgentAPI is the top-level public API for all agents to overwrite.

type Document

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

func NewDocument

func NewDocument(title string, content ...string) *Document

func (*Document) AddPart

func (d *Document) AddPart(parts ...string) *Document

func (*Document) AddToPrompts added in v0.2.0

func (d *Document) AddToPrompts(prompts ...PromptApi)

func (*Document) Clear

func (d *Document) Clear() *Document

func (*Document) Clone

func (d *Document) Clone() Document

func (*Document) Parts

func (d *Document) Parts() []string

func (*Document) Title

func (d *Document) Title() string

type Prompt

type Prompt[P any, R any] struct {
	Conditions   string
	Steps        string
	Result       string
	SchemaParams P
	SchemaResult R

	// number of previous messages to include
	HistoryMsgLen int

	State string
	A     AgentAPI
	// contains filtered or unexported fields
}

func NewPrompt

func NewPrompt[P any, R any](agent AgentAPI, state, condition, steps, results string) *Prompt[P, R]

func (*Prompt[P, R]) AddDoc added in v0.2.0

func (p *Prompt[P, R]) AddDoc(doc *Document)

func (*Prompt[P, R]) AddTool

func (p *Prompt[P, R]) AddTool(tool ToolApi)

func (*Prompt[P, R]) AppendHistOpenAI

func (p *Prompt[P, R]) AppendHistOpenAI(msg *openai.ChatCompletionMessage)

func (*Prompt[P, R]) Generate

func (p *Prompt[P, R]) Generate() string

func (*Prompt[P, R]) HistCleanOpenAI added in v0.2.0

func (p *Prompt[P, R]) HistCleanOpenAI()

func (*Prompt[P, R]) HistOpenAI

func (p *Prompt[P, R]) HistOpenAI() []openai.ChatCompletionMessage

func (*Prompt[P, R]) MsgsOpenAI

func (p *Prompt[P, R]) MsgsOpenAI() []openai.ChatCompletionMessage

func (*Prompt[P, R]) Run

func (p *Prompt[P, R]) Run(e *am.Event, params P, model string) (*R, error)

TODO accept model as general opts obj TODO rename to Exec?

type PromptApi

type PromptApi interface {
	AddTool(tool ToolApi)
	AddDoc(doc *Document)

	HistOpenAI() []openai.ChatCompletionMessage
	AppendHistOpenAI(msg *openai.ChatCompletionMessage)
	HistCleanOpenAI()
}

type PromptSchemaless

type PromptSchemaless = Prompt[any, any]

type S

type S = am.S

type Tool

type Tool struct {
	Doc *Document
	// contains filtered or unexported fields
}

func NewTool

func NewTool(
	agent AgentAPI, idSuffix, title string, states am.S, stateSchema am.Schema,
) (*Tool, error)

func (*Tool) Mach

func (t *Tool) Mach() *am.Machine

func (*Tool) SetMach

func (t *Tool) SetMach(m *am.Machine)

type ToolApi

type ToolApi interface {
	Mach() *am.Machine
	SetMach(*am.Machine)
	Document() *Document
}

Directories

Path Synopsis
examples
cook
Package cook is a recipe-choosing and cooking agent with a gen-ai character.
Package cook is a recipe-choosing and cooking agent with a gen-ai character.
cook/cmd command
research
Package deepresearch is a port of atomic-agents/deepresearch to secai.
Package deepresearch is a port of atomic-agents/deepresearch to secai.
research/cmd command
Package llmagent is a base agent extended with common LLM prompts.
Package llmagent is a base agent extended with common LLM prompts.
Package schema contains a stateful schema-v2 for Agent.
Package schema contains a stateful schema-v2 for Agent.
tools
tui

Jump to

Keyboard shortcuts

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