LLM Agent framework with hexagonal (ports-and-adapters) architecture. Defines 9 traits + Agent loop. Depends on moonbitlang/async.
| Port | 职责 |
|---|---|
| ModelPort | 普通或流式模型调用 |
| ToolProvider | 工具发现与执行 |
| SessionStore | session 加载与保存 |
| Observer | 只读 turn event 观察 |
| Compressor | 每轮模型调用前返回压缩指令 |
| PipelineHook | 修改、延迟或中止 pipeline stage |
| MemoryPort | 长期记忆存储与检索 |
| Lifecycle | async 资源清理 |
| CommandPort | 用户侧命令枚举与调用 |
let agent = @posoco.Agent::new(
model,
tools=[],
hooks=[],
memory=[],
compressors=[@posoco.NoopCompressor::{ }],
observers=[],
sessions=[store],
lifecycle=None,
config={
max_tool_rounds: 10,
temperature: None,
max_output_tokens: None,
tool_choice: None,
reasoning_effort: None,
model_context_window: None,
},
)
let result = agent.run_turn(input, "session_1")Layer 3 — Agent / composition
Agent::new + Agent::run_turn
Layer 2 — Runtime adapters
调用方提供 ModelPort、ToolProvider、SessionStore 等具体实现
Layer 1 — Effect interfaces and shared types
9 个 pub(open) trait + Message / ToolCall / Session / TurnEvent 等rtk moon check --target native --output-json
rtk moon test --target native --output-json
rtk moon fmt
rtk moon infopub(all) suberror HarnessFacadeError {
SessionLoad(detail~ : String)
SessionSave(detail~ : String)
CatalogComposition(failure~ : CatalogCompositionError)
HarnessRejected(error~ : HarnessError)
UnsupportedSuspension(run_id~ : RunId, suspension_id~ : String)
NonCompletedTerminal(outcome~ : RunOutcome)
ConversionFailed(detail~ : String)
}impl Show for HarnessFacadeErrorpub(all) suberror KernelFacadeError {
SessionLoad(detail~ : String)
SessionSave(detail~ : String)
CatalogComposition(failure~ : CatalogCompositionError)
ConversionFailed(detail~ : String)
UntranslatedInvariant(category~ : InvariantCategory)
DriverLoopExceeded(iterations~ : Int)
KernelFailed(reason~ : FailureReason)
KernelCancelled(reason~ : CancelReason)
}impl Show for KernelFacadeErrorpub(all) struct Agent {
model_port : &ModelPort
tool_routing : ToolRouting
pipeline_hook : &PipelineHook?
compressors : Array[&Compressor]
observers : Array[&Observer]
memories : Array[&MemoryPort]
sessions : Array[&SessionStore]
lifecycle : &Lifecycle?
config : AgentConfig
}fn Agent::new(model_port : &ModelPort, tools~ : Array[&ToolProvider], hooks~ : Array[&PipelineHook], memory~ : Array[&MemoryPort], compressors~ : Array[&Compressor], observers~ : Array[&Observer], sessions~ : Array[&SessionStore], lifecycle~ : &Lifecycle?, config~ : AgentConfig) -> Agent raise CompositionErrorasync fn Agent::run_turn(self : Agent, input : Message, session_id : String) -> TurnResult raise AgentErrorasync fn Agent::run_turn_via_harness(self : Agent, input : Message, session_id : String) -> TurnResult raise HarnessFacadeErrorasync fn Agent::run_turn_via_kernel(self : Agent, input : Message, session_id : String) -> TurnResult raise KernelFacadeErrorpub(all) struct AgentConfig {
max_tool_rounds : Int
temperature : Double?
max_output_tokens : Int?
tool_choice : ToolChoice?
reasoning_effort : ReasoningEffort?
model_context_window : Int?
}impl ToolProvider for CompositeToolProviderasync fn execute(_self : CompositeToolProvider, name : String, _call : ToolCall) -> ToolResult raise RuntimeErrorpub(all) struct LegacyHostRuntime {
model_port : &ModelPort
tool_providers : Map[String, &ToolProvider]
}impl HostRuntime for LegacyHostRuntimeasync fn call_model(self : LegacyHostRuntime, messages : Array[CanonicalMessage], tool_definitions : Array[CanonicalToolDef], call_options : Json, on_chunk : (Json) -> Unit) -> Result[AssistantCompletion, ModelFailure]async fn cancel_effects(self : LegacyHostRuntime, effect_ids : Array[EffectId], reason : CancelReason) -> Array[(EffectId, CancelDisposition)]async fn execute_tool(self : LegacyHostRuntime, owner_id : OwnerId, call_id : CallId, tool_name : ToolName, arguments : Json) -> ToolOutcomefn LegacyHostRuntime::new(model_port : &ModelPort, tool_bindings : Array[(String, &ToolProvider)]) -> LegacyHostRuntimepub(all) struct NoopCommandPort {
}impl CommandPort for NoopCommandPortasync fn invoke(_self : NoopCommandPort, id : String, _args : Json) -> CommandOutcome raise CommandErrorpub(all) struct NoopCompressor {
}impl Compressor for NoopCompressorfn compress(_self : NoopCompressor, _messages : Array[Message], _ctx : CompressContext) -> CompressActionpub(all) struct NoopHook {
}pub(all) struct RecordingHook {
records : Array[HookRecord]
abort_on : Map[String, String]
defer_on : Map[String, String]
}impl PipelineHook for RecordingHookimpl Observer for RecordingObserverimpl SessionStore for RecordingSessionStorefn RecordingSessionStore::fail_load(self : RecordingSessionStore, id : String, err : SessionError) -> Unitfn RecordingSessionStore::save_snapshots(self : RecordingSessionStore) -> Array[(String, Map[String, Json])]fn RecordingSessionStore::seed(self : RecordingSessionStore, id : String, session : Session) -> Unitpub(all) struct RecordingToolProvider {
tool_defs : Array[ToolDef]
outcomes : Map[String, ScriptedToolOutcome]
list_calls : Int
exec_records : Array[ToolExecRecord]
ops : Array[ToolProviderOp]
}impl ToolProvider for RecordingToolProviderasync fn execute(self : RecordingToolProvider, name : String, call : ToolCall) -> ToolResult raise RuntimeErrorasync fn RecordingToolProvider::execute_direct(self : RecordingToolProvider, name : String, call : ToolCall) -> ToolResult raise RuntimeErrorfn RecordingToolProvider::new(tool_defs : Array[ToolDef], outcomes : Map[String, ScriptedToolOutcome]) -> RecordingToolProviderpub(all) struct ScriptedModel {
steps : Array[ScriptedModelStep]
index : Int
calls : Int
received_messages : Array[Array[Message]]
received_tools : Array[Array[ToolDef]]
received_options : Array[ChatOptions]
received_chunks : Array[StreamChunk]
}impl ModelPort for ScriptedModelasync fn chat(self : ScriptedModel, messages : Array[Message], tools : Array[ToolDef], options : ChatOptions) -> ModelResponse raise ModelErrorasync fn chat_streaming(self : ScriptedModel, messages : Array[Message], tools : Array[ToolDef], options : ChatOptions, on_chunk : (StreamChunk) -> Unit) -> ModelResponse raise ModelErrorasync fn ScriptedModel::chat_direct(self : ScriptedModel, messages : Array[Message], tools : Array[ToolDef], options : ChatOptions) -> ModelResponse raise ModelErrorasync fn ScriptedModel::chat_streaming_direct(self : ScriptedModel, messages : Array[Message], tools : Array[ToolDef], options : ChatOptions, on_chunk : (StreamChunk) -> Unit) -> ModelResponse raise ModelErrorpub(all) enum ScriptedModelStep {
Respond(ModelResponse)
Stream(chunks~ : Array[StreamChunk], response~ : ModelResponse)
Fail(ModelError)
}pub(all) enum SessionOp {
OpLoad(id~ : String, result~ : Result[Session, SessionError])
OpSave(id~ : String, metadata~ : Map[String, Json], result~ : Result[Unit, SessionError])
}pub(all) struct ToolExecRecord {
requested_name : String
call_id : String
tool_name : String
arguments : Json
outcome : ScriptedToolOutcome
}pub(all) struct ToolRegistry {
tools : Map[String, ToolDef]
executors : Map[String, (ToolCall) -> ToolResult]
}impl ToolProvider for ToolRegistryasync fn execute(self : ToolRegistry, name : String, call : ToolCall) -> ToolResult raise RuntimeErrorfn ToolRegistry::register(self : ToolRegistry, tool : ToolDef, executor : (ToolCall) -> ToolResult) -> Unittype ToolRoutingInstall
Download zipLLM Agent framework with hexagonal (ports-and-adapters) architecture. Defines 9 traits + Agent loop. Depends on moonbitlang/async.