Skip to content

Comments

Refactor model interface to high-level idiomatic Go API#2863

Merged
asim merged 9 commits intomasterfrom
copilot/add-model-packager-interface
Feb 14, 2026
Merged

Refactor model interface to high-level idiomatic Go API#2863
asim merged 9 commits intomasterfrom
copilot/add-model-packager-interface

Conversation

Copy link
Contributor

Copilot AI commented Feb 14, 2026

The model interface exposed low-level methods (BuildRequest, ParseResponse, SetAuthHeaders, etc.) requiring manual HTTP handling, request building, and tool execution loops. It used Java-like terms ("Factory", "Executor") and didn't follow go-micro patterns.

Changes

Interface redesign following Registry/Client/Broker patterns:

  • Init(...Option) error - configure post-creation
  • Options() Options - get current config
  • String() string - provider name
  • Generate(ctx, req, opts...) (*Response, error) - single method replaces 6 low-level methods
  • Stream(ctx, req, opts...) (Stream, error) - future streaming support

Options pattern for configuration:

  • WithAPIKey(), WithModel(), WithBaseURL(), WithToolHandler()
  • Providers handle defaults internally

Automatic tool execution:

  • Optional ToolHandler executes tools and returns final answer in Response.Answer
  • No manual loop/follow-up requests needed

Terminology fixes:

  • ProviderFactoryNewFunc
  • ToolExecutorToolHandler

Provider implementations (Anthropic, OpenAI):

  • HTTP handling internalized
  • Single Generate() call handles full request/response/tool cycle

Example

Before:

modelProvider, _ := model.New(provider)
modelName := modelProvider.DefaultModel()
chatBody, _ := modelProvider.BuildRequest(prompt, system, tools, nil)
// Manual model override, HTTP call, parsing, tool loop, follow-up...

After:

m := model.New(provider,
    model.WithAPIKey(apiKey),
    model.WithModel(modelName),
    model.WithToolHandler(executeToolCall),
)
resp, _ := m.Generate(ctx, &model.Request{
    Prompt: prompt,
    SystemPrompt: system,
    Tools: tools,
})
// resp.Answer contains final result after automatic tool execution

Server code reduced from 184 to 118 lines (-36%).

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • api.anthropic.com
    • Triggering command: /tmp/go-build4006390183/b060/anthropic.test /tmp/go-build4006390183/b060/anthropic.test -test.testlogfile=/tmp/go-build4006390183/b060/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true (dns block)
    • Triggering command: /tmp/go-build3913422460/b060/anthropic.test /tmp/go-build3913422460/b060/anthropic.test -test.testlogfile=/tmp/go-build3913422460/b060/testlog.txt -test.paniconexit0 -test.timeout=10m0s ortc�� x64/src/runtime/-p x64/src/log/log.google.golang.org/protobuf/reflect/protodesc (dns block)
  • api.openai.com
    • Triggering command: /tmp/go-build4006390183/b197/openai.test /tmp/go-build4006390183/b197/openai.test -test.testlogfile=/tmp/go-build4006390183/b197/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true (dns block)
    • Triggering command: /tmp/go-build3913422460/b197/openai.test /tmp/go-build3913422460/b197/openai.test -test.testlogfile=/tmp/go-build3913422460/b197/testlog.txt -test.paniconexit0 -test.timeout=10m0s ortc�� x64/src/runtime/-p x64/src/vendor/ggolang.org/x/net/http2/h2c (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Add model packager with Model interface Add model provider abstraction layer Feb 14, 2026
Copilot AI requested a review from asim February 14, 2026 15:15
Copilot AI changed the title Add model provider abstraction layer Refactor model interface to high-level idiomatic Go API Feb 14, 2026
@asim asim marked this pull request as ready for review February 14, 2026 18:14
@asim asim merged commit e110ccb into master Feb 14, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants