From b8459ef36c5d5a1767ed506211a92fc6d3d04788 Mon Sep 17 00:00:00 2001 From: zhuyongxin Date: Thu, 14 May 2026 15:36:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=85=E7=90=86=20main.go=20=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=E4=BB=A3=E7=A0=81=E5=B9=B6=E8=A1=A5=E5=85=85=20README?= =?UTF-8?q?=20=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除 main.go 中的 GoLand 自动生成模板代码,替换为项目实际入口 - 补充空的 README.md,添加项目说明、架构图、模块表和快速开始指南 --- README.md | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 62 ++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 118 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index e69de29..5e82243 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,68 @@ +# go-tiny-claw + +一个轻量级的 AI Agent 引擎,基于 Go 语言实现标准的 **ReAct(Reasoning + Acting)** 循环模式。 + +## 项目背景 + +go-tiny-claw 受 Claude / OpenAI 的 Agent 概念启发,旨在提供一个简洁、可扩展的微型 Agent 框架核心。它定义了 Agent 与大模型交互的标准契约,让开发者可以快速搭建自己的 AI 助手应用。 + +## 核心架构 + +``` +┌─────────────────────────────────────────────┐ +│ AgentEngine │ +│ ┌──────────┐ ┌──────────┐ │ +│ │LLMProvider│ │ Registry │ │ +│ └─────┬────┘ └─────┬────┘ │ +│ │ │ │ +│ ▼ ▼ │ +│ 大模型推理服务 本地工具执行 │ +└─────────────────────────────────────────────┘ +``` + +### 模块说明 + +| 包 | 说明 | +|---|---| +| `internal/engine` | **AgentEngine** — 核心驱动,实现 ReAct 主循环 | +| `internal/provider` | **LLMProvider** 接口 — 与大模型通信的统一契约 | +| `internal/schema` | 核心数据结构定义(Message、ToolCall、ToolResult) | +| `internal/tools` | **Registry** 接口 — 工具的注册与分发执行 | + +### ReAct 循环流程 + +1. 系统初始化 Context,注入用户指令 +2. 向大模型发起推理请求(含可用工具列表) +3. 模型返回文本回复和/或工具调用请求 +4. 如果模型未请求工具调用 → 任务完成,退出循环 +5. 执行模型请求的工具,获取 Observation +6. 将 Observation 追加到上下文,回到步骤 2 + +## 快速开始 + +```go +package main + +import ( + "context" + "go-tiny-claw/internal/engine" + "go-tiny-claw/internal/schema" +) + +func main() { + provider := &mockProvider{} + registry := &mockRegistry{} + eng := engine.NewAgentEngine(provider, registry, "/path/to/workdir") + eng.Run(context.Background(), "你的指令") +} +``` + +## 扩展 + +- **替换 Provider**: 实现 `LLMProvider` 接口即可接入任意大模型(OpenAI、Claude、本地模型等) +- **注册工具**: 实现 `Registry` 接口即可挂载自定义工具集(bash、文件操作、代码搜索等) +- **自定义 Schema**: 基于 `schema` 包的数据结构可灵活扩展消息格式和工具定义 + +## License + +MIT diff --git a/main.go b/main.go index 8918222..6263e13 100644 --- a/main.go +++ b/main.go @@ -1,21 +1,59 @@ package main import ( - "fmt" + "context" + "log" + "os" + + "go-tiny-claw/internal/engine" + "go-tiny-claw/internal/schema" ) -//TIP

To run your code, right-click the code and select Run.

Alternatively, click -// the icon in the gutter and select the Run menu item from here.

+// mockProvider 模拟大模型推理 +type mockProvider struct { + turn int +} -func main() { - //TIP

Press when your caret is at the underlined text - // to see how GoLand suggests fixing the warning.

Alternatively, if available, click the lightbulb to view possible fixes.

- s := "gopher" - fmt.Printf("Hello and welcome, %s!\n", s) +func (m *mockProvider) Generate(ctx context.Context, msgs []schema.Message, _ []schema.ToolDefinition) (*schema.Message, error) { + m.turn++ + if m.turn == 1 { + return &schema.Message{ + Role: schema.RoleAssistant, + Content: "让我来看看当前目录下有什么文件。", + ToolCalls: []schema.ToolCall{ + {ID: "call_123", Name: "bash", Arguments: []byte(`{"command": "ls -la"}`)}, + }, + }, nil + } + return &schema.Message{ + Role: schema.RoleAssistant, + Content: "我看到了文件列表,里面包含 main.go,任务完成!", + }, nil +} - for i := 1; i <= 5; i++ { - //TIP

To start your debugging session, right-click your code in the editor and select the Debug option.

We have set one breakpoint - // for you, but you can always add more by pressing .

- fmt.Println("i =", 100/i) +// mockRegistry 模拟工具执行 +type mockRegistry struct{} + +func (m *mockRegistry) GetAvailableTools() []schema.ToolDefinition { return nil } + +func (m *mockRegistry) Execute(ctx context.Context, call schema.ToolCall) schema.ToolResult { + return schema.ToolResult{ + ToolCallID: call.ID, + Output: "-rw-r--r-- 1 user group 234 Oct 24 10:00 main.go\n", + IsError: false, + } +} + +func main() { + workDir, _ := os.Getwd() + + p := &mockProvider{} + r := &mockRegistry{} + + eng := engine.NewAgentEngine(p, r, workDir) + + err := eng.Run(context.Background(), "帮我检查当前目录的文件") + if err != nil { + log.Fatalf("引擎崩溃: %v", err) } }