MCPcopy Create free account
hub / github.com/astercloud/aster / createAgent

Function createAgent

examples/openrouter/main.go:174–225  ·  view source on GitHub ↗

createAgent 创建并配置 Agent

(apiKey, baseURL, model string, streaming bool)

Source from the content-addressed store, hash-verified

172
173// createAgent 创建并配置 Agent
174func createAgent(apiKey, baseURL, model string, streaming bool) (*agent.Agent, error) {
175 // 工具注册
176 toolRegistry := tools.NewRegistry()
177 builtin.RegisterAll(toolRegistry)
178
179 // 持久化存储
180 jsonStore, err := store.NewJSONStore(".aster")
181 if err != nil {
182 return nil, fmt.Errorf("创建 Store 失败: %w", err)
183 }
184
185 // Agent 模板
186 templateRegistry := agent.NewTemplateRegistry()
187 templateRegistry.Register(&types.AgentTemplateDefinition{
188 ID: "simple-assistant",
189 SystemPrompt: "你是一个有用的助手,可以读取和写入文件。当用户要求你读取或写入文件时,请使用可用的工具。",
190 Tools: []any{"Read", "Write", "Bash"},
191 })
192
193 // 依赖注入
194 deps := &agent.Dependencies{
195 Store: jsonStore,
196 SandboxFactory: sandbox.NewFactory(),
197 ToolRegistry: toolRegistry,
198 ProviderFactory: &provider.OpenRouterFactory{},
199 TemplateRegistry: templateRegistry,
200 }
201
202 // 执行模式
203 execMode := types.ExecutionModeNonStreaming
204 if streaming {
205 execMode = types.ExecutionModeStreaming
206 }
207
208 // Agent 配置
209 config := &types.AgentConfig{
210 TemplateID: "simple-assistant",
211 ModelConfig: &types.ModelConfig{
212 Provider: "openrouter",
213 Model: model,
214 APIKey: apiKey,
215 BaseURL: baseURL,
216 ExecutionMode: execMode,
217 },
218 Sandbox: &types.SandboxConfig{
219 Kind: types.SandboxKindLocal,
220 WorkDir: "./workspace",
221 },
222 }
223
224 return agent.Create(context.TODO(), config, deps)
225}
226
227// handleEvents 处理 Agent 事件流
228func handleEvents(eventCh <-chan types.AgentEventEnvelope) {

Callers 1

runFunction · 0.70

Calls 7

RegisterMethod · 0.95
NewRegistryFunction · 0.92
RegisterAllFunction · 0.92
NewJSONStoreFunction · 0.92
NewTemplateRegistryFunction · 0.92
NewFactoryFunction · 0.92
CreateFunction · 0.92

Tested by

no test coverage detected