Skip to content
go-kratosPublic

Latest commit

 

History

289 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status GoDoc DeepWiki License

Blades

简体中文

Blades is an event-driven multimodal Agent framework for Go. It provides a small public Agent interface, a provider-agnostic model protocol, tool execution, session history, prompt construction, context compaction, policy checks, lifecycle hooks, and multi-agent flow composition.

The framework is designed for applications that need LLM agents as normal Go components: explicit inputs and outputs, cancellable runtime contexts, provider adapters at the edge, and extension points that stay outside the core protocol.

Why Blades

  • Event-first runtime: applications interact with Agents through event.Input and event.Output, including streaming text, tool lifecycle events, call-local assistant endings, turn endings, errors, and Done.
  • Provider-neutral core: OpenAI, Anthropic, Gemini, MCP, and observability integrations live in contrib/; the root module does not depend on provider SDKs.
  • Multimodal protocol: content.Part is the shared, extensible content interface used by events, model messages, and tool results.
  • Tool-ready Agent loop: tools are described by tools.ToolSpec, executed by tools.Tool, filtered by policy, observed by hooks, and committed back into session history.
  • Composable agents: use flow/ for sequential, parallel, loop, routing, and deep agent composition, or wrap any Agent as a tool with blades.NewAgentTool.

Install

go get github.com/go-kratos/blades
go get github.com/go-kratos/blades/contrib/openai

Each provider integration is its own Go module. Add only the contrib modules your application uses:

go get github.com/go-kratos/blades/contrib/anthropic
go get github.com/go-kratos/blades/contrib/gemini
go get github.com/go-kratos/blades/contrib/mcp

Quick Start

package main

import (
    "context"
    "log"
    "os"

    "github.com/go-kratos/blades"
    "github.com/go-kratos/blades/contrib/openai"
    "github.com/go-kratos/blades/event"
)

func main() {
    provider := openai.NewChat("gpt-5",
        openai.WithAPIKey(os.Getenv("OPENAI_API_KEY")),
    )

    agent, err := blades.NewAgent(
        "assistant",
        blades.WithModel(provider),
        blades.WithInstruction("You are a concise, accurate assistant."),
    )
    if err != nil {
        log.Fatal(err)
    }

    result, err := blades.NewRunner(agent).Run(
        context.Background(),
        event.NewPrompt("What is the capital of France?"),
    )
    if err != nil {
        log.Fatal(err)
    }

    log.Println(result.Text())
}

Agent Interface

Every runtime implements the same small interface:

type Agent interface {
    Name() string
    Description() string
    Run(context.Context, <-chan event.Input) (<-chan event.Output, error)
}

blades.NewAgent(name, opts...) builds the default LLM-backed Agent. If you need a custom runtime, implement Agent directly and it can still be used by Runner, flow/, or blades.NewAgentTool.

Some models return a completed assistant message containing only content.Thinking. Use blades.WithThinkingAsText(true) to expose that content as content.Text in event.AssistantMessageEnd. Mixed-content messages are not converted, and streaming deltas, turn results, and session history retain the original thinking parts.

Runtime Building Blocks

Package Purpose
content/ Shared multimodal Part union for text, blobs, thinking, tool use, and tool results.
event/ User-facing input and output events for prompts, steering, aborts, streaming, tools, assistant response endings, turn endings, errors, and completion.
model/ Provider-facing requests, messages, chunks, responses, usage, options, and the model.Provider interface.
tools/ Tool specs, execution interface, resolver/filter helpers, and tool context.
session/ Append-only model message history and context helpers.
prompt/ System prompt builders and ordered sections.
compact/ Context compaction strategies and model-backed summarization.
policy/ Tool invocation decisions such as allow, deny, ask, and modify.
hook/ Lifecycle callbacks around turns, model calls, and tool calls.
flow/ Agent composition primitives for sequential, parallel, loop, routing, and deep flows.
jsonrepair/ Provider-neutral recovery of malformed or truncated tool-call JSON.
contrib/ Provider and integration modules for OpenAI, Anthropic, Gemini, MCP, and OpenTelemetry.

Providers

Providers implement model.Provider:

type Provider interface {
    Name() string
    Generate(context.Context, *model.Request) (*model.Response, error)
    Stream(context.Context, *model.Request) iter.Seq2[*model.Chunk, error]
}

Provider options stay in the matching contrib module:

provider := openai.NewChat("gpt-5",
    openai.WithAPIKey(os.Getenv("OPENAI_API_KEY")),
    openai.WithParallelToolCalls(true),
)

Tool concurrency is driven by model output. If a provider returns multiple content.ToolUse parts in one assistant message, the Agent loop executes that tool wave concurrently. To request at most one tool call per turn, configure the provider, for example openai.WithParallelToolCalls(false) or anthropic.WithParallelToolCalls(false).

Each turn corresponds to exactly one primary Provider.Stream call and its optional tool wave. Every response accepted by AfterModel emits a mandatory event.AssistantMessageEnd with normalized call-local usage. AssistantMessageEnd.Usage and TurnEnd.Usage use model.Usage directly, including provider-specific Raw. With tools, the order is all ToolEnd events, then AssistantMessageEnd, then TurnEnd; without tools, it is AssistantMessageEnd, then TurnEnd.

Tools And Agents

Tools expose a schema and a handler:

type Tool interface {
    Spec() tools.ToolSpec
    Handle(context.Context, json.RawMessage) (*tools.Result, error)
}

Attach tools to an Agent with blades.WithTools(...), resolve them dynamically with blades.WithToolsResolver(...), and guard them with blades.WithPolicy(...). Any Agent can also become a tool:

researcher, _ := blades.NewAgent("researcher", blades.WithModel(provider))
writer, _ := blades.NewAgent(
    "writer",
    blades.WithModel(provider),
    blades.WithTools(blades.NewAgentTool(researcher)),
)
_ = writer

Skills

Skills keep specialized instructions out of the always-on system prompt. The Agent receives only a compact catalog, then uses load_skill and load_skill_resource when a task actually needs the full instructions.

skillList, err := skills.NewFromDir("./skills")
if err != nil {
    return err
}
agent, err := blades.NewAgent(
    "assistant",
    blades.WithModel(provider),
    blades.WithSkills(skillList...),
)

Each skill follows the SKILL.md layout and may include references/, assets/, and scripts/. Script files can be inspected as resources but are not executed by the built-in toolset. Tools named by allowed-tools stay hidden until that Skill is loaded; this controls disclosure only. Skill and business tools remain subject to WithPolicy, which is the authorization boundary.

Streaming

Use Runner.RunStream when the application needs incremental output:

out, err := blades.NewRunner(agent).RunStream(ctx, event.NewPrompt("Write a haiku."))
if err != nil {
    return err
}
for e := range out {
    switch v := e.(type) {
    case event.TextDelta:
        fmt.Print(v.Text)
    case event.AssistantMessageEnd:
        log.Printf("call usage: %+v", v.Usage)
    case event.Error:
        return v.Err
    }
}

Use Runner.RunLive when the application needs to send additional input, such as steering or abort events, while the Agent is running.

Documentation

Development

The root Makefile discovers all Go modules and runs checks per module:

make tidy
make build
make test
make all

For targeted iteration:

go test -race ./event
go test -race ./tests/agent
(cd contrib/openai && go test -race ./...)

License

Blades is licensed under the MIT License. See LICENSE.

Releases

Packages

Used by

Contributors

Languages