Compare commits
135 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c70fa679c0 | |||
| b45c94e201 | |||
| 23051459d8 | |||
| 4c7e6e96eb | |||
| ef21ac6494 | |||
| 53193fbbe7 | |||
| 105cc9d2fb | |||
| d369cf290c | |||
| 949542fa65 | |||
| 1421ccbea6 | |||
| 7a0ca65b68 | |||
| 28433f2db8 | |||
| cb61ccb9d7 | |||
| fbb5fd46ae | |||
| ff6af5d46b | |||
| 89ae0a10eb | |||
| 666e1f765e | |||
| 5a0df7991e | |||
| 631c58cb75 | |||
| 57e83ca9a0 | |||
| 8d59779465 | |||
| 3d0c5237f0 | |||
| cf1f42d15b | |||
| 728ea5eae7 | |||
| 5e9accfd45 | |||
| 0adb4f0aac | |||
| ee44926191 | |||
| 09cf00b5ec | |||
| da5fa1f27c | |||
| a8ea60120e | |||
| d7050e9cc2 | |||
| 536806119e | |||
| ab2c092693 | |||
| 7144cddc00 | |||
| 9f28cc4c86 | |||
| 8a19dc2d7c | |||
| daacd4830f | |||
| 844ab4a14b | |||
| 156b051191 | |||
| 21a6746d86 | |||
| 2c04f61331 | |||
| 634fc9bced | |||
| a503628ef1 | |||
| 3f40028a7f | |||
| b9665ad196 | |||
| 053103fb7f | |||
| 0ba2253357 | |||
| 2339155ff2 | |||
| 3e885308a0 | |||
| 4311b73361 | |||
| c3542127dd | |||
| 06659032a9 | |||
| 8cad95cc29 | |||
| 2cbd6264d7 | |||
| aa7cd6dc3b | |||
| 456ff18176 | |||
| cdc6ee9aa1 | |||
| e5bf72abd8 | |||
| 232126476a | |||
| 358b2ffd27 | |||
| 45346337e2 | |||
| 4b8b4d7bab | |||
| 97f932a2e7 | |||
| ca87efef2f | |||
| d54cfab1bd | |||
| 46c56ef7df | |||
| c7657f73f4 | |||
| b9586f920b | |||
| f42c9d1d69 | |||
| 307b94aab7 | |||
| 7ee32e8721 | |||
| 9fdcc24cce | |||
| 6b4ce55a7c | |||
| ce0741a80c | |||
| 5e5d253abd | |||
| e079e083da | |||
| a1a57799c3 | |||
| e4d2a41c32 | |||
| 9deac487cb | |||
| 6eec83a045 | |||
| 9dae4e34b7 | |||
| b41dfa02f2 | |||
| 0b41c71681 | |||
| 550033dcce | |||
| 584a9f2132 | |||
| 6488d8402d | |||
| d3be610367 | |||
| 35bc58e5d2 | |||
| e416ea4a75 | |||
| 830c8d84c2 | |||
| 6a73608e9c | |||
| cb33decd97 | |||
| 9bed04ced0 | |||
| 2e89961386 | |||
| 39e8dc7311 | |||
| c93bcdf4d3 | |||
| cd760a29f1 | |||
| e45d3df0ad | |||
| 6731ee2f0c | |||
| f7c042ef26 | |||
| 1bc886fa82 | |||
| 844ac5bc52 | |||
| bc570d674f | |||
| e59d056f97 | |||
| e13ef7eed5 | |||
| 23f682181c | |||
| bc1092b18a | |||
| 16918669ca | |||
| a357d1870d | |||
| 91a545c6f2 | |||
| 00392aa1f2 | |||
| b54507710d | |||
| d541625e32 | |||
| 7662b26b07 | |||
| 397010a82a | |||
| d3c4981326 | |||
| 96280678ee | |||
| 69fc228c73 | |||
| 5d7609027b | |||
| 5601be009a | |||
| 888dbbca4a | |||
| f48d81c760 | |||
| 3acf74a29a | |||
| c4b4cbef25 | |||
| a9421e5b7e | |||
| 03f912f68a | |||
| 594ee107b7 | |||
| 2f164595f2 | |||
| 740980a9cc | |||
| 3d2d9acd68 | |||
| 88aa0cc666 | |||
| 601c67675f | |||
| b97f45106d | |||
| 28fa44b2ca | |||
| 985621f4b4 |
@@ -1 +1,2 @@
|
||||
github: asim
|
||||
custom: ["https://go-micro.dev/support"]
|
||||
|
||||
@@ -48,4 +48,4 @@ Add any other context about the problem here.
|
||||
- [Troubleshooting Guide](https://github.com/micro/go-micro/tree/master/internal/website/docs/getting-started.md)
|
||||
- [Examples](https://github.com/micro/go-micro/tree/master/examples)
|
||||
- [API Reference](https://pkg.go.dev/go-micro.dev/v5)
|
||||
- [Discord Community](https://discord.gg/jwTYuUVAGh)
|
||||
- [Discord Community](https://discord.gg/WeMU5AGxD)
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
---
|
||||
name: Commercial Support / Consulting
|
||||
about: Inquire about paid support, consulting, training, or a retainer
|
||||
title: '[SUPPORT] '
|
||||
labels: commercial-support
|
||||
assignees: asim
|
||||
---
|
||||
|
||||
## What are you building?
|
||||
A short description of your project and how you're using (or planning to use) Go Micro.
|
||||
|
||||
## What do you need?
|
||||
- [ ] Production support / retainer (priority fixes, direct line, response SLA)
|
||||
- [ ] Consulting (integration, architecture, agent design)
|
||||
- [ ] Training / onboarding for a team
|
||||
- [ ] Sponsored feature or fix
|
||||
- [ ] Not sure yet — let's talk
|
||||
|
||||
## Scale & timeline
|
||||
Team size, where you're running it, and any timeline that matters.
|
||||
|
||||
## Anything else?
|
||||
Links, context, constraints. For anything you'd rather keep private, become a [sponsor](https://github.com/sponsors/asim) and message directly.
|
||||
@@ -0,0 +1,8 @@
|
||||
blank_issues_enabled: true
|
||||
contact_links:
|
||||
- name: 💖 Sponsor Go Micro
|
||||
url: https://github.com/sponsors/asim
|
||||
about: Fund ongoing development and see your name or logo on the project.
|
||||
- name: 📖 Documentation
|
||||
url: https://go-micro.dev/docs
|
||||
about: Guides, examples, and the full reference.
|
||||
@@ -39,4 +39,4 @@ Add any other context, code examples, or screenshots about the feature request h
|
||||
- [Roadmap](https://github.com/micro/go-micro/blob/master/ROADMAP.md)
|
||||
- [Contributing Guide](https://github.com/micro/go-micro/blob/master/CONTRIBUTING.md)
|
||||
- [Architecture Docs](https://github.com/micro/go-micro/tree/master/internal/website/docs/architecture.md)
|
||||
- [Discord Community](https://discord.gg/jwTYuUVAGh)
|
||||
- [Discord Community](https://discord.gg/WeMU5AGxD)
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
name: Auto-merge Codex PRs
|
||||
|
||||
# Part of the autonomous improvement loop (internal/docs/CONTINUOUS_IMPROVEMENT.md).
|
||||
# Merges Codex's PRs once CI is green — no human involvement; CI (build, test,
|
||||
# golangci-lint, harnesses) is the only gate. Scoped to PRs that are BOTH
|
||||
# codex-labelled AND from a codex/* branch, so nothing else can auto-merge.
|
||||
|
||||
on:
|
||||
schedule:
|
||||
- cron: "*/15 * * * *" # sweep every 15 min
|
||||
workflow_dispatch: {}
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
pull-requests: write
|
||||
|
||||
concurrency:
|
||||
group: auto-merge-codex
|
||||
cancel-in-progress: false
|
||||
|
||||
jobs:
|
||||
merge:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Merge green Codex PRs
|
||||
env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
REPO: ${{ github.repository }}
|
||||
run: |
|
||||
gh pr list --repo "$REPO" --label codex --state open \
|
||||
--json number,headRefName \
|
||||
--jq '.[] | select(.headRefName | startswith("codex/")) | .number' \
|
||||
| while read -r pr; do
|
||||
[ -z "$pr" ] && continue
|
||||
if gh pr checks "$pr" --repo "$REPO" >/dev/null 2>&1; then
|
||||
echo "Checks green on #$pr — merging."
|
||||
gh pr merge "$pr" --repo "$REPO" --squash --delete-branch \
|
||||
|| echo "skip #$pr (not mergeable — conflicts?)"
|
||||
else
|
||||
echo "skip #$pr (checks pending/failing)"
|
||||
fi
|
||||
done
|
||||
@@ -0,0 +1,62 @@
|
||||
name: Continuous Improvement
|
||||
|
||||
# Durable backbone for the autonomous improvement loop
|
||||
# (see internal/docs/CONTINUOUS_IMPROVEMENT.md).
|
||||
#
|
||||
# A Claude Max subscription provides no API key for CI, so the loop is driven by
|
||||
# Codex rather than Claude Code: on a cadence this opens a fresh tracking issue and
|
||||
# posts an @codex instruction on it, and Codex runs one improvement increment + opens
|
||||
# a PR. (See the per-issue rationale below.)
|
||||
#
|
||||
# Codex does NOT respond to comments authored by the github-actions bot, so the
|
||||
# dispatch is GATED on a CODEX_TRIGGER_TOKEN secret (a PAT for a user account Codex
|
||||
# follows). Until that secret is set the workflow runs but no-ops — this avoids
|
||||
# piling up @codex comments that Codex silently ignores. The moment the secret is
|
||||
# added the loop activates with no further change.
|
||||
#
|
||||
# Each run opens a FRESH issue and dispatches Codex there, rather than re-commenting
|
||||
# on one tracker issue. Codex derives its PR branch name from the triggering issue's
|
||||
# context, so repeated dispatches on a single issue all collapse onto one branch name
|
||||
# (codex/github-mention-<that-issue-slug>) — the first increment opens a PR, the rest
|
||||
# collide on the occupied branch and silently fail to open one. A unique issue per
|
||||
# run gives each increment its own branch and a clean PR. The dispatch asks Codex to
|
||||
# "Closes #<issue>" so each tracking issue auto-closes when its PR merges.
|
||||
|
||||
on:
|
||||
workflow_dispatch: {}
|
||||
schedule:
|
||||
- cron: "29 * * * *" # hourly, off-minute (tune as needed)
|
||||
|
||||
permissions:
|
||||
issues: write
|
||||
|
||||
concurrency:
|
||||
group: continuous-improvement
|
||||
cancel-in-progress: false
|
||||
|
||||
jobs:
|
||||
dispatch:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Open a fresh increment issue and dispatch Codex
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.CODEX_TRIGGER_TOKEN || github.token }}
|
||||
HAS_TRIGGER_TOKEN: ${{ secrets.CODEX_TRIGGER_TOKEN != '' }}
|
||||
REPO: ${{ github.repository }}
|
||||
RUN_NUMBER: ${{ github.run_number }}
|
||||
run: |
|
||||
if [ "$HAS_TRIGGER_TOKEN" != "true" ]; then
|
||||
echo "CODEX_TRIGGER_TOKEN is not set — skipping dispatch."
|
||||
echo "Codex ignores comments from the github-actions bot, so posting now"
|
||||
echo "would only create noise. Add a CODEX_TRIGGER_TOKEN secret (a PAT for"
|
||||
echo "a user account Codex follows) to activate the loop."
|
||||
exit 0
|
||||
fi
|
||||
# A unique issue per run → Codex derives a unique branch → no collisions.
|
||||
ISSUE_URL=$(gh issue create --repo "$REPO" \
|
||||
--title "Continuous improvement increment #$RUN_NUMBER" \
|
||||
--body "Autonomous continuous-improvement increment. North Star: internal/docs/THESIS.md; charter: internal/docs/CONTINUOUS_IMPROVEMENT.md. Tracker: #3024.")
|
||||
ISSUE_NUM="${ISSUE_URL##*/}"
|
||||
echo "Opened issue #$ISSUE_NUM — dispatching Codex."
|
||||
gh issue comment "$ISSUE_NUM" --repo "$REPO" --body \
|
||||
"@codex Run one continuous-improvement increment per internal/docs/CONTINUOUS_IMPROVEMENT.md, aligned to the North Star in internal/docs/THESIS.md (the holistic services → agents → workflows lifecycle). Pick the single highest-value roadmap/issue/improvement-radar item that advances that thesis, implement it, and verify \`go build ./...\`, \`go test ./...\`, and \`golangci-lint run ./...\`. Then open the PR YOURSELF from the shell — do NOT use the make_pr tool (in this environment it only records metadata and never creates a PR). Run \`git push -u origin HEAD\` then \`gh pr create --base master --title \"<title>\" --body \"<body, including 'Closes #$ISSUE_NUM'>\"\`; the gh CLI is installed and authenticated and origin points to $REPO. One concern per PR; stay out of brand/positioning copy and breaking public API."
|
||||
@@ -0,0 +1,59 @@
|
||||
name: Harness (E2E)
|
||||
|
||||
# Runs the end-to-end harnesses for agents, services, flows, and provider
|
||||
# conformance. The default job uses deterministic mock LLMs and needs no
|
||||
# secrets. A second job runs the same harnesses against any live providers
|
||||
# whose API key secrets are configured.
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: ["**"]
|
||||
pull_request:
|
||||
branches: ["**"]
|
||||
schedule:
|
||||
- cron: "17 6 * * *" # daily, so the world is exercised even without changes
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
harness:
|
||||
name: Harnesses (mock LLM)
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: stable
|
||||
cache: true
|
||||
- name: Build
|
||||
run: go build ./...
|
||||
- name: Universe end-to-end (asserts; exits non-zero on failure)
|
||||
run: go run ./internal/harness/universe
|
||||
- name: Agent-flow harness
|
||||
run: go run ./internal/harness/agent-flow
|
||||
- name: Plan-delegate harness
|
||||
run: go run ./internal/harness/plan-delegate
|
||||
|
||||
harness-live:
|
||||
name: Provider harnesses (live LLM, if keys present)
|
||||
runs-on: ubuntu-latest
|
||||
# Only on the daily schedule or a manual run — never automatically on
|
||||
# every push/PR, so changes don't quietly burn API credits. Trigger it
|
||||
# by hand (Actions → Harness → Run workflow) when changing the agent,
|
||||
# flow, or AI internals and you want a real-model check.
|
||||
if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: stable
|
||||
cache: true
|
||||
- name: Provider conformance against configured live models
|
||||
env:
|
||||
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
|
||||
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
|
||||
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
|
||||
GROQ_API_KEY: ${{ secrets.GROQ_API_KEY }}
|
||||
MISTRAL_API_KEY: ${{ secrets.MISTRAL_API_KEY }}
|
||||
TOGETHER_API_KEY: ${{ secrets.TOGETHER_API_KEY }}
|
||||
ATLASCLOUD_API_KEY: ${{ secrets.ATLASCLOUD_API_KEY }}
|
||||
run: go run ./internal/harness/provider-conformance
|
||||
@@ -0,0 +1,30 @@
|
||||
name: Lint
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- "**"
|
||||
pull_request:
|
||||
types:
|
||||
- opened
|
||||
- reopened
|
||||
- synchronize
|
||||
branches:
|
||||
- "**"
|
||||
jobs:
|
||||
golangci:
|
||||
name: golangci-lint
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: 1.24
|
||||
check-latest: true
|
||||
cache: true
|
||||
- name: golangci-lint
|
||||
uses: golangci/golangci-lint-action@v8
|
||||
with:
|
||||
version: v2.5.0
|
||||
+10
@@ -57,3 +57,13 @@ examples/mcp/hello/hello
|
||||
# IDE-specific files
|
||||
.DS_Store
|
||||
/micro
|
||||
|
||||
# Built example/harness binaries (go build ./path/... drops these at repo root)
|
||||
/plan-delegate
|
||||
/agent-plan-delegate
|
||||
/micro-mcp-gateway
|
||||
|
||||
# Local Jekyll / Bundler artifacts
|
||||
internal/website/.bundle/
|
||||
internal/website/_site/
|
||||
internal/website/.jekyll-cache/
|
||||
|
||||
+57
-245
@@ -1,251 +1,63 @@
|
||||
# This file contains all available configuration options
|
||||
# with their default values.
|
||||
version: "2"
|
||||
|
||||
# options for analysis running
|
||||
run:
|
||||
# go: '1.18'
|
||||
# default concurrency is a available CPU number
|
||||
# concurrency: 4
|
||||
|
||||
# timeout for analysis, e.g. 30s, 5m, default is 1m
|
||||
deadline: 10m
|
||||
|
||||
# exit code when at least one issue was found, default is 1
|
||||
issues-exit-code: 1
|
||||
|
||||
# include test files or not, default is true
|
||||
timeout: 5m
|
||||
tests: true
|
||||
|
||||
# which files to skip: they will be analyzed, but issues from them
|
||||
# won't be reported. Default value is empty list, but there is
|
||||
# no need to include all autogenerated files, we confidently recognize
|
||||
# autogenerated files. If it's not please let us know.
|
||||
skip-files:
|
||||
[]
|
||||
# - .*\\.pb\\.go$
|
||||
|
||||
allow-parallel-runners: true
|
||||
|
||||
# list of build tags, all linters use it. Default is empty list.
|
||||
build-tags: []
|
||||
|
||||
# output configuration options
|
||||
output:
|
||||
# Format: colored-line-number|line-number|json|tab|checkstyle|code-climate|junit-xml|github-actions
|
||||
#
|
||||
# Multiple can be specified by separating them by comma, output can be provided
|
||||
# for each of them by separating format name and path by colon symbol.
|
||||
# Output path can be either `stdout`, `stderr` or path to the file to write to.
|
||||
# Example: "checkstyle:report.json,colored-line-number"
|
||||
#
|
||||
# Default: colored-line-number
|
||||
format: colored-line-number
|
||||
# Print lines of code with issue.
|
||||
# Default: true
|
||||
print-issued-lines: true
|
||||
# Print linter name in the end of issue text.
|
||||
# Default: true
|
||||
print-linter-name: true
|
||||
# Make issues output unique by line.
|
||||
# Default: true
|
||||
uniq-by-line: true
|
||||
# Add a prefix to the output file references.
|
||||
# Default is no prefix.
|
||||
path-prefix: ""
|
||||
# Sort results by: filepath, line and column.
|
||||
sort-results: true
|
||||
|
||||
# all available settings of specific linters
|
||||
linters-settings:
|
||||
wsl:
|
||||
allow-cuddle-with-calls: ["Lock", "RLock", "defer"]
|
||||
funlen:
|
||||
lines: 80
|
||||
statements: 60
|
||||
varnamelen:
|
||||
# The longest distance, in source lines, that is being considered a "small scope".
|
||||
# Variables used in at most this many lines will be ignored.
|
||||
# Default: 5
|
||||
max-distance: 26
|
||||
ignore-names:
|
||||
- err
|
||||
- id
|
||||
- ch
|
||||
- wg
|
||||
- mu
|
||||
ignore-decls:
|
||||
- c echo.Context
|
||||
- t testing.T
|
||||
- f *foo.Bar
|
||||
- e error
|
||||
- i int
|
||||
- const C
|
||||
- T any
|
||||
- m map[string]int
|
||||
errcheck:
|
||||
# report about not checking of errors in type assetions: `a := b.(MyStruct)`;
|
||||
# default is false: such cases aren't reported by default.
|
||||
check-type-assertions: true
|
||||
|
||||
# report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`;
|
||||
# default is false: such cases aren't reported by default.
|
||||
check-blank: true
|
||||
govet:
|
||||
# report about shadowed variables
|
||||
check-shadowing: false
|
||||
gofmt:
|
||||
# simplify code: gofmt with `-s` option, true by default
|
||||
simplify: true
|
||||
gocyclo:
|
||||
# minimal code complexity to report, 30 by default (but we recommend 10-20)
|
||||
min-complexity: 15
|
||||
maligned:
|
||||
# print struct with more effective memory layout or not, false by default
|
||||
suggest-new: true
|
||||
dupl:
|
||||
# tokens count to trigger issue, 150 by default
|
||||
threshold: 100
|
||||
goconst:
|
||||
# minimal length of string constant, 3 by default
|
||||
min-len: 3
|
||||
# minimal occurrences count to trigger, 3 by default
|
||||
min-occurrences: 3
|
||||
depguard:
|
||||
list-type: blacklist
|
||||
# Packages listed here will reported as error if imported
|
||||
packages:
|
||||
- github.com/golang/protobuf/proto
|
||||
misspell:
|
||||
# Correct spellings using locale preferences for US or UK.
|
||||
# Default is to use a neutral variety of English.
|
||||
# Setting locale to US will correct the British spelling of 'colour' to 'color'.
|
||||
locale: US
|
||||
lll:
|
||||
# max line length, lines longer will be reported. Default is 120.
|
||||
# '\t' is counted as 1 character by default, and can be changed with the tab-width option
|
||||
line-length: 120
|
||||
# tab width in spaces. Default to 1.
|
||||
tab-width: 1
|
||||
unused:
|
||||
# treat code as a program (not a library) and report unused exported identifiers; default is false.
|
||||
# XXX: if you enable this setting, unused will report a lot of false-positives in text editors:
|
||||
# if it's called for subdir of a project it can't find funcs usages. All text editor integrations
|
||||
# with golangci-lint call it on a directory with the changed file.
|
||||
check-exported: false
|
||||
unparam:
|
||||
# call graph construction algorithm (cha, rta). In general, use cha for libraries,
|
||||
# and rta for programs with main packages. Default is cha.
|
||||
algo: cha
|
||||
|
||||
# Inspect exported functions, default is false. Set to true if no external program/library imports your code.
|
||||
# XXX: if you enable this setting, unparam will report a lot of false-positives in text editors:
|
||||
# if it's called for subdir of a project it can't find external interfaces. All text editor integrations
|
||||
# with golangci-lint call it on a directory with the changed file.
|
||||
check-exported: false
|
||||
nakedret:
|
||||
# make an issue if func has more lines of code than this setting and it has naked returns; default is 30
|
||||
max-func-lines: 60
|
||||
nolintlint:
|
||||
allow-unused: false
|
||||
allow-leading-space: false
|
||||
allow-no-explanation: []
|
||||
require-explanation: false
|
||||
require-specific: true
|
||||
prealloc:
|
||||
# XXX: we don't recommend using this linter before doing performance profiling.
|
||||
# For most programs usage of prealloc will be a premature optimization.
|
||||
|
||||
# Report preallocation suggestions only on simple loops that have no returns/breaks/continues/gotos in them.
|
||||
# True by default.
|
||||
simple: true
|
||||
range-loops: true # Report preallocation suggestions on range loops, true by default
|
||||
for-loops: false # Report preallocation suggestions on for loops, false by default
|
||||
cyclop:
|
||||
# the maximal code complexity to report
|
||||
max-complexity: 20
|
||||
gomoddirectives:
|
||||
replace-local: true
|
||||
retract-allow-no-explanation: false
|
||||
exclude-forbidden: true
|
||||
|
||||
linters:
|
||||
enable-all: true
|
||||
disable-all: false
|
||||
fast: false
|
||||
disable:
|
||||
- golint
|
||||
- varcheck
|
||||
- ifshort
|
||||
- structcheck
|
||||
- deadcode
|
||||
# - nosnakecase
|
||||
- interfacer
|
||||
- maligned
|
||||
- scopelint
|
||||
- exhaustivestruct
|
||||
- testpackage
|
||||
- promlinter
|
||||
- nonamedreturns
|
||||
- makezero
|
||||
- gofumpt
|
||||
- nlreturn
|
||||
- thelper
|
||||
# Start from the standard set (errcheck, govet, ineffassign, staticcheck,
|
||||
# unused) and add a few low-noise, high-value linters on top.
|
||||
default: standard
|
||||
enable:
|
||||
- bodyclose
|
||||
- misspell
|
||||
- unconvert
|
||||
- usestdlibvars
|
||||
settings:
|
||||
errcheck:
|
||||
exclude-functions:
|
||||
- (*encoding/json.Encoder).Encode
|
||||
- (net/http.ResponseWriter).Write
|
||||
- fmt.Fprintf
|
||||
- fmt.Fprint
|
||||
- fmt.Fprintln
|
||||
misspell:
|
||||
locale: US
|
||||
staticcheck:
|
||||
checks:
|
||||
- all
|
||||
# Deprecations are tracked separately; some (e.g. gRPC dial options)
|
||||
# are kept intentionally for compatibility. Migrate them on their own.
|
||||
- -SA1019
|
||||
# Initialism/naming convention (Id->ID, Http->HTTP, ...). The remaining
|
||||
# offenders are exported identifiers (e.g. web.Id, web.DefaultId) whose
|
||||
# rename is a breaking API change; not worth it in a lint-bootstrap pass.
|
||||
- -ST1003
|
||||
exclusions:
|
||||
# Use golangci-lint's built-in sensible exclusions and skip generated code.
|
||||
generated: lax
|
||||
presets:
|
||||
- comments
|
||||
- common-false-positives
|
||||
- legacy
|
||||
- std-error-handling
|
||||
rules:
|
||||
# The protobuf code generator is a port of upstream protoc-gen-go and
|
||||
# keeps its structure; don't flag its unused legacy helpers.
|
||||
- path: cmd/protoc-gen-micro/generator/
|
||||
linters:
|
||||
- unused
|
||||
# Tests are held to a looser standard.
|
||||
- path: _test\.go
|
||||
linters:
|
||||
- bodyclose
|
||||
- errcheck
|
||||
# Demo/harness code: fire-and-forget calls are fine and `_ =` noise hurts
|
||||
# readability of examples.
|
||||
- path: (^|/)(examples|internal/harness)/
|
||||
linters:
|
||||
- errcheck
|
||||
|
||||
# Can be considered to be enabled
|
||||
- gochecknoinits
|
||||
- gochecknoglobals # RIP
|
||||
- dogsled
|
||||
- wrapcheck
|
||||
- paralleltest
|
||||
- ireturn
|
||||
- gomnd
|
||||
- goerr113
|
||||
- exhaustruct
|
||||
- containedctx
|
||||
- godox
|
||||
- forcetypeassert
|
||||
- gci
|
||||
- lll
|
||||
|
||||
issues:
|
||||
# List of regexps of issue texts to exclude, empty list by default.
|
||||
# But independently from this option we use default exclude patterns,
|
||||
# it can be disabled by `exclude-use-default: false`. To list all
|
||||
# excluded by default patterns execute `golangci-lint run --help`
|
||||
# exclude:
|
||||
# - package comment should be of the form "Package services ..." # revive
|
||||
# - ^ST1000 # ST1000: at least one file in a package should have a package comment (stylecheck)
|
||||
|
||||
# exclude-rules:
|
||||
# - path: internal/app/machined/pkg/system/services
|
||||
# linters:
|
||||
# - dupl
|
||||
exclude-rules:
|
||||
- path: _test\.go
|
||||
linters:
|
||||
- gocyclo
|
||||
- dupl
|
||||
- gosec
|
||||
- funlen
|
||||
- varnamelen
|
||||
- wsl
|
||||
|
||||
# Independently from option `exclude` we use default exclude patterns,
|
||||
# it can be disabled by this option. To list all
|
||||
# excluded by default patterns execute `golangci-lint run --help`.
|
||||
# Default value for this option is true.
|
||||
exclude-use-default: false
|
||||
|
||||
# Maximum issues count per one linter. Set to 0 to disable. Default is 50.
|
||||
max-issues-per-linter: 0
|
||||
|
||||
# Maximum count of issues with the same text. Set to 0 to disable. Default is 3.
|
||||
max-same-issues: 0
|
||||
|
||||
# Show only new issues: if there are unstaged changes or untracked files,
|
||||
# only those changes are analyzed, else only changes in HEAD~ are analyzed.
|
||||
# It's a super-useful option for integration of golangci-lint into existing
|
||||
# large codebase. It's not practical to fix all existing issues at the moment
|
||||
# of integration: much better don't allow issues in new code.
|
||||
# Default is false.
|
||||
new: false
|
||||
formatters:
|
||||
enable:
|
||||
- gofmt
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
# Repository agent instructions
|
||||
|
||||
These instructions apply to the entire repository.
|
||||
|
||||
## Pull requests from Codex tasks
|
||||
|
||||
When a Codex task makes repository changes and the requested outcome is a PR:
|
||||
|
||||
1. Keep the change focused on the assigned issue or prompt.
|
||||
2. Run the relevant verification commands and capture their results.
|
||||
3. Check `git status --short` and review the diff before finishing.
|
||||
4. Stage the intended files and create a local git commit on the current branch.
|
||||
5. Use the Codex `make_pr` tool to open the pull request with a concise title and a body that summarizes the change and testing.
|
||||
|
||||
Do not just say that a PR was opened. If local changes exist, the task is not complete until the changes are committed and the `make_pr` tool has been called. A GitHub token in the shell environment is not a substitute for the Codex `make_pr` tool in this environment.
|
||||
+24
-3
@@ -7,9 +7,30 @@ calendar-based versions (YYYY.MM) for the AI-native era.
|
||||
|
||||
---
|
||||
|
||||
## [Unreleased]
|
||||
## [6.0.0] - June 2026
|
||||
|
||||
The AI-native major release. Breaking changes are listed first; everything
|
||||
else is additive. See the [v5 → v6 migration guide](internal/website/docs/guides/migration/v5-to-v6.md) — it's a small upgrade.
|
||||
|
||||
### Changed (breaking)
|
||||
- **Module path is now `go-micro.dev/v6`.** Update imports (`go-micro.dev/v5/...` → `go-micro.dev/v6/...`) and `go install go-micro.dev/v6/cmd/micro@v6`.
|
||||
- **TLS verification is on by default.** v5 skipped verification unless `MICRO_TLS_SECURE=true`; v6 verifies by default. `MICRO_TLS_SECURE` is removed — set `MICRO_TLS_INSECURE=true` (or call `tls.InsecureConfig()`) for self-signed/dev certs.
|
||||
- **`micro.NewService(name, opts...)` is the service constructor**, symmetric with `NewAgent`/`NewFlow`. `micro.New(name, opts...)` remains as a deprecated alias; the old name-less `micro.NewService(opts...)` form is removed (pass the name positionally). Generators emit the new form.
|
||||
- **JWT auth ported in-module.** The external `github.com/micro/plugins/v5/auth/jwt` (pinned to v5) is replaced by `go-micro.dev/v6/auth/jwt/token`, now on the maintained `golang-jwt/jwt/v5`; the deprecated `dgrijalva/jwt-go` dependency is dropped.
|
||||
|
||||
### Added
|
||||
- **A2A protocol — both directions** — `gateway/a2a` exposes registered agents over the open Agent2Agent (A2A) protocol so agents on other frameworks can discover and call them: Agent Cards are generated from registry metadata (the same way the MCP gateway derives tools), and incoming tasks are translated to the agent's existing `Agent.Chat` RPC, with no per-agent code (`micro a2a serve`). The outbound `a2a.Client` calls external A2A agents by URL, wired into `flow.A2A(url)` (a workflow step) and `delegate` to an `http(s)` URL (from inside an agent). An agent can also serve A2A **directly** without a gateway via `AgentA2A(addr)` (`a2a.NewAgentHandler`), handling tasks in-process. v1 is the synchronous JSON-RPC binding (`message/send`, `tasks/get`, card discovery); streaming and push notifications are advertised as unsupported. (`gateway/a2a/`, `cmd/micro/a2a/`)
|
||||
- **Agents (`micro.NewAgent`)** — an agent is a service with an LLM inside: it discovers its assigned services as tools, runs the model's tool loop, registers a `Chat` RPC endpoint, and is reachable like any service. `Ask` for programmatic use; `micro chat` discovers and routes to agents; `micro agent list`/`describe`. (`agent/`)
|
||||
- **Plan & delegate** — two built-in agent tools added to every agent: `plan` (an ordered, store-persisted plan surfaced back in the prompt) and `delegate` (hand a self-contained subtask to a registered agent over RPC, otherwise to an ephemeral sub-agent). No harness or graph — they're plain tools. (`agent/builtin.go`, `examples/agent-plan-delegate/`)
|
||||
- **Agent guardrails** — `MaxSteps` (stop on count), `LoopLimit` (stop repeated no-progress calls; on by default), and `ApproveTool` (human-in-the-loop / policy gate before each action), enforced at the one point every tool call passes through. (`agent/`, guide + blog)
|
||||
- **Pluggable agent memory & custom tools** — durable store-backed conversation memory by default, swappable via `AgentMemory`; register any function as a tool with `AgentTool`.
|
||||
- **Workflows (`micro.NewFlow`)** — event-driven orchestration that maps to Anthropic's workflow/agent split: an event triggers a deterministic step (or ordered durable steps), or dispatches to an agent with `FlowAgent`. (`flow/`)
|
||||
- **Flow loops (`FlowLoop`)** — a flow step that runs a body step repeatedly, carrying state across passes, until a stop condition is met or a hard iteration cap is hit. Stop on a code-defined predicate (`FlowUntil`) or let the model judge it done (`FlowUntilLLM` — the supervised "Ralph" loop); `FlowLoopMax` is the guardrail that guarantees termination, and `FlowOnIteration` reports progress. (`flow/loop.go`, `examples/flow-loop/`, guide)
|
||||
- **x402 payments** — opt-in per-call payments for tools via the x402 standard, with a pluggable facilitator and a consumer-side client + budget; the MCP gateway can advertise and require payment per tool. (`wrapper/x402/`, guide + blog)
|
||||
- **Scoped store state** — `store.Scope(s, database, table)` returns a store handle that confines every operation to a database/table without mutating the shared store (unlike `Init(Table(...))`, which is process-global and races between co-located components). Services, agents, and flows now each keep their state in their own table (`service/{name}`, `agent/{name}`, `flow/{name}`); the service path replaces the old `Init(store.Table(name))` global mutation with a scoped handle.
|
||||
- **Flow discovery & history CLI** — running flows now register in the registry as `type=flow` (and deregister on `Stop`), so they're discoverable like agents: `micro flow list` shows running flows, `micro flow runs <name>` shows a flow's durable run history from the store, and `micro agent history <name>` shows an agent's stored conversation. Live state comes from the registry; durable history from the scoped store.
|
||||
- **Durable workflows** — a flow can now be an ordered list of steps (a task with stages) that is checkpointed before and after each step, so a run survives a crash and resumes where it stopped without re-running completed steps. State carries a typed payload plus a `Stage` marker; flow-level `Retry` with a per-step override; runs retained for audit unless `DeleteOnSuccess`. Step actions: `Call` (RPC), `LLM` (model turn), `Dispatch` (to an agent), or any `StepFunc`. Durability is a pluggable `Checkpoint` (store-backed by default; implement the interface for Temporal/Restate). Runnable example: `examples/flow-durable/`. Blog: "Durable Workflows" (`internal/website/blog/24.md`).
|
||||
- **Agent tool-execution wrappers** — `AgentWrapTool` registers middleware around an agent's tool calls, the tool-side analogue of `client.CallWrapper`/`server.HandlerWrapper`. Use it for logging, metrics, retries, or policy; wrappers compose outermost-first and run outside the built-in guardrails. Includes a runnable example with observe + retry wrappers (`examples/agent-wrap-tool/`).
|
||||
- **Agent platform showcase** — full platform example (Users, Posts, Comments, Mail) mirroring [micro/blog](https://github.com/micro/blog), demonstrating how existing microservices become agent-accessible with zero code changes (`examples/mcp/platform/`).
|
||||
- **Blog post: "Your Microservices Are Already an AI Platform"** — walkthrough of agent-service interaction patterns using real-world services (`internal/website/blog/7.md`).
|
||||
- **Circuit breakers for MCP gateway** — per-tool circuit breakers protect downstream services from cascading failures. Configurable max failures, open-state timeout, and half-open probing. Available via `Options.CircuitBreaker` and `--circuit-breaker` CLI flag (`gateway/mcp/circuitbreaker.go`).
|
||||
@@ -26,10 +47,10 @@ calendar-based versions (YYYY.MM) for the AI-native era.
|
||||
|
||||
#### Developer Experience
|
||||
- **`micro new` MCP templates** — `micro new myservice` generates MCP-enabled services with doc comments, `@example` tags, and `WithMCP()` wired in. Use `--no-mcp` to opt out.
|
||||
- **`micro.New("name")` unified API** — single way to create services: `micro.New("greeter")` or `micro.New("greeter", micro.Address(":8080"))`. Replaces `micro.NewService()` + `service.New()` dual API.
|
||||
- **`micro.NewService("name")` unified API** — single way to create services: `micro.NewService("greeter")` or `micro.NewService("greeter", micro.Address(":8080"))`. Replaces `micro.NewService()` + `service.New()` dual API.
|
||||
- **`service.Handle()` simplified registration** — register handlers with `service.Handle(new(Greeter))` instead of manual `server.NewHandler` + `server.Handle`.
|
||||
- **`micro.NewGroup()` modular monoliths** — run multiple services in one binary with shared lifecycle: `micro.NewGroup(users, orders).Run()`.
|
||||
- **`mcp.WithMCP()` one-liner** — add MCP to any service with a single option: `micro.New("name", mcp.WithMCP(":3001"))`.
|
||||
- **`mcp.WithMCP()` one-liner** — add MCP to any service with a single option: `micro.NewService("name", mcp.WithMCP(":3001"))`.
|
||||
- **CRUD example** — contact book service with 6 operations, rich agent docs, and validation patterns (`examples/mcp/crud/`).
|
||||
|
||||
#### MCP Gateway
|
||||
|
||||
@@ -34,7 +34,8 @@ micro run
|
||||
|
||||
```
|
||||
go-micro/
|
||||
├── ai/ # AI model providers (Anthropic, OpenAI)
|
||||
├── agent/ # Agent abstraction (intelligent service management)
|
||||
├── ai/ # AI model providers (Anthropic, OpenAI, Gemini, etc.)
|
||||
├── auth/ # Authentication (JWT, no-op)
|
||||
├── broker/ # Message broker (NATS, RabbitMQ)
|
||||
├── cache/ # Caching (Redis)
|
||||
@@ -44,6 +45,7 @@ go-micro/
|
||||
├── config/ # Dynamic config (env, file, etcd, NATS)
|
||||
├── errors/ # Error handling
|
||||
├── events/ # Event system (NATS JetStream)
|
||||
├── flow/ # Event-driven LLM orchestration
|
||||
├── gateway/
|
||||
│ ├── api/ # REST API gateway
|
||||
│ └── mcp/ # MCP gateway (core AI integration)
|
||||
@@ -98,6 +100,7 @@ Build compelling demos showing agents interacting with go-micro services in real
|
||||
- Kubernetes Operator with CRDs
|
||||
|
||||
### Recently Completed
|
||||
- **Agent Plan & Delegate** - Two built-in agent tools: `plan` (ordered plan persisted to store-backed memory, surfaced in the prompt) and `delegate` (hand a subtask to another agent — RPC to a registered agent, else an ephemeral sub-agent with isolated context). Added automatically to every agent; no harness or graph. (`agent/builtin.go`, `examples/agent-plan-delegate/`)
|
||||
- **`micro new` MCP Templates** - Scaffolds MCP-enabled services with doc comments, `@example` tags, `WithMCP()`. `--no-mcp` to opt out.
|
||||
- **CRUD Example** - Contact book service with 6 operations, rich agent docs (`examples/mcp/crud/`)
|
||||
- **Migration Guide** - "Add MCP to Existing Services" guide with 3 approaches
|
||||
@@ -122,20 +125,30 @@ Build compelling demos showing agents interacting with go-micro services in real
|
||||
| CLI Entry | `cmd/micro/main.go` |
|
||||
| MCP CLI | `cmd/micro/mcp/` |
|
||||
| Server (run/server) | `cmd/micro/server/server.go` |
|
||||
| Roadmap | `internal/docs/ROADMAP_2026.md` |
|
||||
| Status | `internal/docs/CURRENT_STATUS_SUMMARY.md` |
|
||||
| Roadmap | `ROADMAP.md` (full: `internal/website/docs/roadmap.md`) |
|
||||
| Status | `CHANGELOG.md` |
|
||||
| Changelog | `CHANGELOG.md` |
|
||||
| Docs Site | `internal/website/docs/` |
|
||||
|
||||
## Roadmap & Status Documents
|
||||
|
||||
- **[ROADMAP.md](ROADMAP.md)** - General framework roadmap
|
||||
- **[internal/docs/ROADMAP_2026.md](internal/docs/ROADMAP_2026.md)** - AI-native era roadmap with business model
|
||||
- **[internal/docs/CURRENT_STATUS_SUMMARY.md](internal/docs/CURRENT_STATUS_SUMMARY.md)** - Quick status overview
|
||||
- **[internal/docs/PROJECT_STATUS_2026.md](internal/docs/PROJECT_STATUS_2026.md)** - Detailed technical status
|
||||
- **[ROADMAP.md](ROADMAP.md)** - the single, current roadmap (agentic development + DX). Full version at `internal/website/docs/roadmap.md`.
|
||||
- **[CHANGELOG.md](CHANGELOG.md)** - what shipped and when (the source of truth for status).
|
||||
- **[internal/docs/IMPLEMENTATION_SUMMARY.md](internal/docs/IMPLEMENTATION_SUMMARY.md)** - Implementation notes
|
||||
- **[CHANGELOG.md](CHANGELOG.md)** - What changed and when
|
||||
|
||||
## Coordination with Codex
|
||||
|
||||
Go Micro is maintained by two AI tools — **Claude Code** (you) and **Codex** (its playbook is [CODEX.md](CODEX.md)) — plus the human maintainer, who routes work and owns every merge. To work side by side without collisions:
|
||||
|
||||
- **Lanes / branches.** You work on `claude/*` branches; Codex on `codex/*`. Never push to Codex's branch, and never have both agents committing the same branch at once.
|
||||
- **Base PRs on `master`; don't stack on Codex's in-flight branch.** If that base squash-merges, your commit gets orphaned (this happened — the #3007 fixes had to be re-landed). If the code you need isn't merged yet, wait for it, then branch off `master`. To improve an *open* Codex PR, fix it in place (once Codex is done with the branch, or via an `@codex` comment on the PR) rather than a separate stacked PR.
|
||||
- **One concern per PR.** Single-purpose PRs; don't bundle (e.g.) a feature with a docs change.
|
||||
- **Cross-review.** Review Codex's PRs before merge — mechanical fixes you can land yourself (based on `master`), but design/scope/positioning calls go to the human; don't silently rewrite Codex's intent. Codex reviews yours via `@codex review`.
|
||||
- **Dispatching Codex.** Start a task by commenting `@codex <instruction>` on an issue/PR (that issue/PR is its context). `@codex review` is reserved for review; any other instruction starts a *task*. It's consequential (spends a Codex task slot, pushes commits) and **serial** (one task at a time) — so dispatch one task at a time, only on the human's go-ahead, and never write a literal `@codex` in a comment unless you intend to trigger it (write "Codex" in prose otherwise).
|
||||
- **CI is the gate.** `go build`, `go test`, `golangci-lint` (blocking), and `make harness` must pass before merge. `internal/harness/` and `examples/` are excluded from errcheck; everything else gets the full set.
|
||||
- **Backlog = GitHub issues**, each a scoped, self-contained brief with acceptance criteria.
|
||||
|
||||
## Contributing
|
||||
|
||||
See [CONTRIBUTING.md](CONTRIBUTING.md) for full guidelines. Key points:
|
||||
|
||||
@@ -0,0 +1,197 @@
|
||||
# Codex Maintainer Playbook
|
||||
|
||||
Go Micro has six months of Codex access through OpenAI's Codex for Open Source
|
||||
program. Use it to increase maintainer throughput without changing the project's
|
||||
bar for review, tests, or design taste.
|
||||
|
||||
## Operating principles
|
||||
|
||||
1. **Humans set direction; Codex accelerates execution.** Maintainers choose the
|
||||
issue, constraints, and acceptance criteria. Codex drafts, investigates, and
|
||||
verifies.
|
||||
2. **Small, reviewable changes win.** Prefer focused PRs that can be understood
|
||||
in one sitting over large speculative rewrites.
|
||||
3. **Keep the contract green.** Every Codex-assisted change should preserve the
|
||||
CLI-first getting-started flow, the harnesses, `make test`, and `make lint`.
|
||||
4. **Document while coding.** If behavior changes, ask Codex to update examples,
|
||||
guides, and release notes in the same branch.
|
||||
5. **No blind merges.** Codex output is treated like any contributor output:
|
||||
reviewed by a maintainer, backed by tests, and checked for public API impact.
|
||||
|
||||
## Coordination with Claude Code
|
||||
|
||||
Go Micro is maintained by two AI tools — **Codex** (you) and **Claude Code** (its guide is [CLAUDE.md](CLAUDE.md)) — plus the human maintainer, who routes work and owns every merge.
|
||||
|
||||
- **Lanes / branches.** You work on `codex/*` branches; Claude Code on `claude/*`. Never push to a branch the other owns, and never have both agents on one branch at once.
|
||||
- **Base PRs on `master`.** Don't stack a PR on another agent's in-flight branch — if that base squash-merges, your changes can be orphaned. If the code you need isn't merged yet, wait, then branch off `master`. To improve a PR that hasn't merged, push to that PR's branch rather than opening a separate stacked PR — keep the change one mergeable unit.
|
||||
- **One concern per PR.** Keep each PR single-purpose so a reviewer can read it in one sitting; don't bundle unrelated changes (e.g. a feature plus a docs rebrand).
|
||||
- **Cross-review before merge.** Claude Code reviews your PRs; you review its with `@codex review`. A fresh pass from the other model catches what the author misses.
|
||||
- **Dispatch.** Maintainers (or Claude Code) start your tasks with `@codex <instruction>` on the relevant issue/PR — that's your context. `@codex review` is review; any other instruction is a *task*. You run one task at a time: take the current one to a clean, green PR before the next is dispatched.
|
||||
- **CI is the gate.** `go build`, `go test`, `golangci-lint` (blocking), and `make harness` must pass; never merge red. `internal/harness/` and `examples/` are excluded from errcheck; everything else gets the full set.
|
||||
- **Backlog = GitHub issues**, each a scoped brief with acceptance criteria.
|
||||
|
||||
## Best uses
|
||||
|
||||
### 1. PR review and triage
|
||||
|
||||
- Summarize a PR: changed surface area, public API impact, tests added or missing.
|
||||
- Ask for targeted review passes: concurrency, cancellation, security, backwards
|
||||
compatibility, docs drift, and examples.
|
||||
- Convert review findings into small patch suggestions or issue comments.
|
||||
|
||||
### 2. Issue reproduction
|
||||
|
||||
- Turn bug reports into failing tests or runnable reproduction scripts.
|
||||
- Minimize flakes by isolating registry, broker, store, transport, and AI-provider
|
||||
dependencies behind deterministic fakes where possible.
|
||||
- Attach the exact command that reproduces the failure to the issue.
|
||||
|
||||
### 3. Release support
|
||||
|
||||
- Draft changelog entries from merged commits, grouped by feature, fix, docs, and
|
||||
compatibility notes.
|
||||
- Check that `README.md`, `ROADMAP.md`, website docs, examples, and `CHANGELOG.md`
|
||||
agree before tagging.
|
||||
- Run dry-run release commands and summarize blockers.
|
||||
|
||||
### 4. Docs and examples
|
||||
|
||||
- Keep the 0→1 path current: scaffold, run, call, chat, inspect.
|
||||
- Keep the 0→hero example current: a realistic multi-agent system that exercises
|
||||
agents, services, flows, MCP, A2A, and observability.
|
||||
- Add runnable examples for new primitives before adding broad prose.
|
||||
|
||||
### 5. Hardening backlog
|
||||
|
||||
Use Codex to break roadmap items into small PRs, especially:
|
||||
|
||||
- cross-provider conformance scenarios for all supported AI providers;
|
||||
- timeout, cancellation, retry, and rate-limit behavior;
|
||||
- durable agent loops on top of the existing checkpoint model;
|
||||
- streaming across `ai.Stream` and A2A;
|
||||
- agent run metadata mapped to OpenTelemetry spans.
|
||||
|
||||
## Suggested weekly loop
|
||||
|
||||
1. Pick one maintenance lane: reviews, bugs, release prep, docs, or hardening.
|
||||
2. Ask Codex for a branch-sized plan with acceptance criteria and test commands.
|
||||
3. Have Codex implement the smallest valuable slice.
|
||||
4. Run the relevant checks locally and in CI.
|
||||
5. Review the diff as maintainer-owned code, then merge or send it back.
|
||||
6. Record any recurring prompt, check, or failure mode in this playbook.
|
||||
|
||||
|
||||
## First two weeks
|
||||
|
||||
Do not start with a giant feature. Start by making Codex pay rent on maintenance
|
||||
work that is already on the roadmap and easy to review.
|
||||
|
||||
### Day 1: set up the review loop
|
||||
|
||||
1. Pick three recent PRs or commits: one feature, one bug fix, and one docs-only
|
||||
change.
|
||||
2. Ask Codex to review each using the PR review template below.
|
||||
3. Compare Codex findings with maintainer judgment. Keep the checks that found
|
||||
real issues; delete the noisy ones.
|
||||
4. Turn the final review prompt into a saved project note or issue comment
|
||||
template.
|
||||
|
||||
Success means Codex can produce a useful first-pass review in under ten minutes
|
||||
without blocking a maintainer on false positives.
|
||||
|
||||
### Days 2-3: make bugs reproducible
|
||||
|
||||
1. Pick one open bug or flaky area.
|
||||
2. Ask Codex for a failing test only. Do not allow a fix in the first pass.
|
||||
3. Review the test for whether it captures the real contract.
|
||||
4. In a second branch, ask Codex to fix the failure with the smallest patch.
|
||||
|
||||
Success means every accepted bug fix starts with a regression test or deterministic
|
||||
harness case.
|
||||
|
||||
### Days 4-5: audit the getting-started contract
|
||||
|
||||
Run through the 0→1 path from a clean checkout and ask Codex to patch only the
|
||||
first broken or confusing step. The target is not new prose; it is a runnable
|
||||
path that works exactly as documented.
|
||||
|
||||
Candidate checks:
|
||||
|
||||
```sh
|
||||
make test
|
||||
make harness
|
||||
make lint
|
||||
go run ./examples/hello-world
|
||||
go run ./internal/harness/universe
|
||||
```
|
||||
|
||||
### Week 2: choose one roadmap slice
|
||||
|
||||
Pick one hardening item and break it into PRs that each land independently. The
|
||||
best first slice is usually test infrastructure, not product code.
|
||||
|
||||
Recommended order:
|
||||
|
||||
1. **Provider conformance skeleton**: define one deterministic agent scenario and
|
||||
gate real-provider runs on credentials.
|
||||
2. **Cancellation audit**: trace `context.Context` propagation through one package
|
||||
at a time.
|
||||
3. **Docs drift audit**: compare `README.md`, `ROADMAP.md`, website docs, and
|
||||
examples for one shipped feature.
|
||||
4. **Release checklist dry run**: have Codex build a release-blocker list from the
|
||||
diff since the previous tag.
|
||||
|
||||
## Standing task queue
|
||||
|
||||
Keep Codex busy on tasks with clear acceptance criteria:
|
||||
|
||||
| Priority | Task | Acceptance criteria |
|
||||
| --- | --- | --- |
|
||||
| P0 | PR first-pass review | Summary, risks, required changes, and exact verification commands. |
|
||||
| P0 | Bug reproduction | A failing test or harness case committed before the fix. |
|
||||
| P0 | 0→1 docs check | Fresh-checkout commands work as written or a patch fixes the first break. |
|
||||
| P1 | Cross-provider conformance | One scenario runs against fakes by default and real providers when keys exist. |
|
||||
| P1 | Cancellation hardening | Tests prove timeout/cancel behavior for the touched package. |
|
||||
| P1 | Release audit | Changelog, docs, examples, and migration notes agree before tagging. |
|
||||
| P2 | Example polish | Example is runnable, linked from docs, and covered by a lightweight check. |
|
||||
|
||||
## What not to use Codex for yet
|
||||
|
||||
- Broad rewrites without a failing test, benchmark, or public design note.
|
||||
- Public API changes before a maintainer writes the compatibility story.
|
||||
- Large generated docs that nobody has run.
|
||||
- Provider-specific behavior that is not checked against the shared `ai.Model`
|
||||
contract.
|
||||
|
||||
## Prompt templates
|
||||
|
||||
### PR review
|
||||
|
||||
```text
|
||||
Review this PR for Go Micro. Focus on public API compatibility, cancellation and
|
||||
context propagation, concurrency safety, tests, and docs drift. Return: summary,
|
||||
risks, required changes, optional improvements, and exact commands to verify.
|
||||
```
|
||||
|
||||
### Bug reproduction
|
||||
|
||||
```text
|
||||
Reproduce this issue in the smallest Go test or harness change possible. Do not
|
||||
fix it yet. Explain the failing path and provide the exact command that fails.
|
||||
```
|
||||
|
||||
### Branch implementation
|
||||
|
||||
```text
|
||||
Implement the smallest branch that satisfies this issue. Keep the API compatible
|
||||
unless explicitly required, update docs/examples when behavior changes, and run
|
||||
`make test`, `make harness`, and `make lint` or explain any environment blocker.
|
||||
```
|
||||
|
||||
### Release audit
|
||||
|
||||
```text
|
||||
Audit this release branch. Compare CHANGELOG, README, ROADMAP, website docs, and
|
||||
examples against the diff since the last tag. List inconsistencies, missing
|
||||
migration notes, and checks to run before tagging.
|
||||
```
|
||||
@@ -8,7 +8,7 @@ LDFLAGS = -X $(GIT_IMPORT).BuildDate=$(BUILD_DATE) -X $(GIT_IMPORT).GitCommit=$(
|
||||
# GORELEASER_DOCKER_IMAGE = ghcr.io/goreleaser/goreleaser-cross:v1.25.7
|
||||
GORELEASER_DOCKER_IMAGE = ghcr.io/goreleaser/goreleaser:latest
|
||||
|
||||
.PHONY: test test-race test-coverage lint fmt install-tools proto clean help gorelease-dry-run gorelease-dry-run-docker
|
||||
.PHONY: test test-race test-coverage harness provider-conformance lint fmt install-tools proto clean help gorelease-dry-run gorelease-dry-run-docker
|
||||
|
||||
# Default target
|
||||
help:
|
||||
@@ -18,6 +18,8 @@ help:
|
||||
@echo " make test-race - Run tests with race detector"
|
||||
@echo " make test-coverage - Run tests with coverage"
|
||||
@echo " make lint - Run linter"
|
||||
@echo " make harness - Run deterministic end-to-end harnesses"
|
||||
@echo " make provider-conformance - Run harnesses against configured live providers"
|
||||
@echo " make fmt - Format code"
|
||||
@echo " make install-tools - Install development tools"
|
||||
@echo " make proto - Generate protobuf code"
|
||||
@@ -40,6 +42,18 @@ test-coverage:
|
||||
go tool cover -html=coverage.out -o coverage.html
|
||||
@echo "Coverage report: coverage.html"
|
||||
|
||||
# Run the end-to-end harnesses (deterministic, mock LLM — no API key).
|
||||
# The universe harness exits non-zero on assertion failure.
|
||||
harness:
|
||||
go run ./internal/harness/universe
|
||||
go run ./internal/harness/agent-flow
|
||||
go run ./internal/harness/plan-delegate
|
||||
|
||||
# Run the same harnesses against every configured live provider. Providers
|
||||
# without API keys are skipped; configured providers must pass.
|
||||
provider-conformance:
|
||||
go run ./internal/harness/provider-conformance
|
||||
|
||||
# Run linter
|
||||
lint:
|
||||
golangci-lint run
|
||||
|
||||
@@ -1,454 +1,426 @@
|
||||
# Go Micro [](https://pkg.go.dev/go-micro.dev/v5?tab=doc) [](https://goreportcard.com/report/github.com/go-micro/go-micro)
|
||||
# Go Micro [](https://pkg.go.dev/go-micro.dev/v6?tab=doc) [](https://goreportcard.com/report/github.com/go-micro/go-micro)
|
||||
|
||||
Go Micro is a framework for distributed systems development.
|
||||
Go Micro is an **agent harness** and service framework for Go.
|
||||
|
||||
A harness is the runtime around an agent: the tools it can call, the memory it keeps, the guardrails that bound it, the workflows that trigger it, the services it depends on, and the protocols other agents use to reach it. Go Micro gives you that harness as Go code. Build an agent and it gets a model, memory, tools, planning, delegation, guardrails, and service discovery; it is reachable over [MCP](https://modelcontextprotocol.io/) and [A2A](https://a2a-protocol.org). Write services and every endpoint becomes an AI-callable tool. Orchestrate the deterministic parts with durable flows. Agents, services, and flows share one runtime because an agent is a distributed system, and building one is building a service.
|
||||
|
||||
## Sponsors
|
||||
|
||||
<a href="https://go-micro.dev/blog/3"><img src="https://upload.wikimedia.org/wikipedia/commons/7/78/Anthropic_logo.svg" height="26" /></a>
|
||||
|
||||
|
||||
<a href="https://go-micro.dev/blog/29"><img src="https://upload.wikimedia.org/wikipedia/commons/4/4d/OpenAI_Logo.svg" height="26" /></a>
|
||||
|
||||
<a href="https://go-micro.dev/blog/8"><img src="https://www.atlascloud.ai/logo.svg" height="26" /></a>
|
||||
|
||||
## Overview
|
||||
**Want to support Go Micro and see your logo here?** [Become a sponsor](https://discord.gg/WeMU5AGxD) — reach out on Discord.
|
||||
|
||||
<img src="internal/website/images/generated/hero.png" alt="Go Micro microservices architecture" width="100%" />
|
||||
## Commercial Support
|
||||
|
||||
Go Micro provides the core requirements for distributed systems development including RPC and Event driven communication.
|
||||
The Go Micro philosophy is sane defaults with a pluggable architecture. We provide defaults to get you started quickly
|
||||
but everything can be easily swapped out.
|
||||
Running Go Micro in production, or building on it and want help? Paid **support, consulting, training, and retainers** are available directly from the maintainer — and they're what keep the project maintained. See [**Support**](SUPPORT.md) for the tiers, or [open a request](https://github.com/micro/go-micro/issues/new?template=commercial_support.md).
|
||||
|
||||
## Features
|
||||
## Contents
|
||||
|
||||
Go Micro abstracts away the details of distributed systems. Here are the main features.
|
||||
- [Quick Start](#quick-start)
|
||||
- [Why an Agent Harness](#why-an-agent-harness)
|
||||
- [Writing Services](#writing-services)
|
||||
- [Building Agents](#building-agents) — [Plan & Delegate](#plan--delegate), [Pluggable](#batteries-included-pluggable), [Paid tools (x402)](#paid-tools-x402), [A2A](#reachable-by-other-agents-a2a)
|
||||
- [Features](#features)
|
||||
- [CLI](#cli)
|
||||
- [Multi-Service Projects](#multi-service-projects)
|
||||
- [Data Model](#data-model)
|
||||
- [AI Providers](#ai-providers)
|
||||
- [Examples](#examples)
|
||||
- [Commercial Support](#commercial-support)
|
||||
- [Docs](#docs)
|
||||
|
||||
- **Authentication** - Auth is built in as a first class citizen. Authentication and authorization enable secure
|
||||
zero trust networking by providing every service an identity and certificates. This additionally includes rule
|
||||
based access control.
|
||||
## Quick Start
|
||||
|
||||
- **Dynamic Config** - Load and hot reload dynamic config from anywhere. The config interface provides a way to load application
|
||||
level config from any source such as env vars, file, etcd. You can merge the sources and even define fallbacks.
|
||||
|
||||
- **Data Storage** - A simple data store interface to read, write and delete records. It includes support for many storage backends
|
||||
in the plugins repo. State and persistence becomes a core requirement beyond prototyping and Micro looks to build that into the framework.
|
||||
|
||||
- **Data Model** - A typed data model layer with CRUD operations, queries, and multiple backends (memory, SQLite, Postgres). Define Go
|
||||
structs with tags and get type-safe Create/Read/Update/Delete/List/Count operations. Accessible via `service.Model()` alongside
|
||||
`service.Client()` and `service.Server()` for a complete service experience: call services, handle requests, save and query data.
|
||||
|
||||
- **Service Discovery** - Automatic service registration and name resolution. Service discovery is at the core of micro service
|
||||
development. When service A needs to speak to service B it needs the location of that service. The default discovery mechanism is
|
||||
multicast DNS (mdns), a zeroconf system.
|
||||
|
||||
- **Load Balancing** - Client side load balancing built on service discovery. Once we have the addresses of any number of instances
|
||||
of a service we now need a way to decide which node to route to. We use random hashed load balancing to provide even distribution
|
||||
across the services and retry a different node if there's a problem.
|
||||
|
||||
- **Message Encoding** - Dynamic message encoding based on content-type. The client and server will use codecs along with content-type
|
||||
to seamlessly encode and decode Go types for you. Any variety of messages could be encoded and sent from different clients. The client
|
||||
and server handle this by default. This includes protobuf and json by default.
|
||||
|
||||
- **RPC Client/Server** - RPC based request/response with support for bidirectional streaming. We provide an abstraction for synchronous
|
||||
communication. A request made to a service will be automatically resolved, load balanced, dialled and streamed.
|
||||
|
||||
- **Async Messaging** - PubSub is built in as a first class citizen for asynchronous communication and event driven architectures.
|
||||
Event notifications are a core pattern in micro service development. The default messaging system is a HTTP event message broker.
|
||||
|
||||
- **MCP Integration** - An MCP gateway you can integrate as a library, server or CLI command which automatically exposes services
|
||||
as tools for agents or other AI applications. Every service/endpoint get's converted into a callable tool.
|
||||
|
||||
- **Multi-Service Binaries** - Run multiple services in a single process with isolated state per service. Start as a modular monolith,
|
||||
split into separate deployments when you need independent scaling. Each service gets its own server, client, and store while sharing
|
||||
the registry and broker for inter-service communication.
|
||||
|
||||
- **Pluggable Interfaces** - Go Micro makes use of Go interfaces for each distributed system abstraction. Because of this these interfaces
|
||||
are pluggable and allows Go Micro to be runtime agnostic. You can plugin any underlying technology.
|
||||
|
||||
## Getting Started
|
||||
|
||||
To make use of Go Micro
|
||||
Install the CLI:
|
||||
|
||||
```bash
|
||||
go get go-micro.dev/v5@v5.16.0
|
||||
# Binary (no Go required)
|
||||
curl -fsSL https://go-micro.dev/install.sh | sh
|
||||
|
||||
# Or with Go
|
||||
go install go-micro.dev/v6/cmd/micro@v6
|
||||
```
|
||||
|
||||
Create a service and register a handler
|
||||
### Fastest start — no API key
|
||||
|
||||
Scaffold a service, run it, call it:
|
||||
|
||||
```bash
|
||||
micro new helloworld
|
||||
cd helloworld
|
||||
micro run
|
||||
```
|
||||
|
||||
Then in another terminal:
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:8080/api/helloworld/Helloworld.Call \
|
||||
-H 'Content-Type: application/json' -d '{"name":"World"}'
|
||||
```
|
||||
|
||||
### Generate from a prompt — with an LLM key
|
||||
|
||||
Set a provider key, describe what you want, and the AI designs services, writes handlers, compiles, and starts them:
|
||||
|
||||
```bash
|
||||
export ANTHROPIC_API_KEY=sk-ant-... # or OPENAI_API_KEY, GEMINI_API_KEY, ...
|
||||
micro run --prompt "a task management system with categories" --provider anthropic
|
||||
```
|
||||
|
||||
The AI designs the architecture, you review it, then it generates handlers with real business logic, compiles them, and starts them:
|
||||
|
||||
```
|
||||
Services:
|
||||
● task — Task management with status tracking
|
||||
● project — Project organization
|
||||
|
||||
Generate? [Y/n]
|
||||
|
||||
Micro
|
||||
Services:
|
||||
● task
|
||||
● project
|
||||
Agents:
|
||||
◆ agent
|
||||
```
|
||||
|
||||
Then talk to your services from the console:
|
||||
|
||||
```
|
||||
> Create a project called Launch, then add three tasks to it
|
||||
|
||||
→ project_Project_Create({"name":"Launch"})
|
||||
← {"record":{"id":"p1..."},"success":true}
|
||||
→ task_Task_Create({"title":"Design specs","project_id":"p1..."})
|
||||
→ task_Task_Create({"title":"Write code","project_id":"p1..."})
|
||||
→ task_Task_Create({"title":"Ship it","project_id":"p1..."})
|
||||
|
||||
Created project Launch and added three tasks to it.
|
||||
```
|
||||
|
||||
When you need a capability that doesn't exist, the agent generates a new service mid-conversation:
|
||||
|
||||
```
|
||||
> I need to track shipping. Create a shipment for order 123 to London.
|
||||
|
||||
⚡ generating shipping service...
|
||||
✓ shipping
|
||||
→ shipping_Shipping_Create({"order_id":"123","destination":"London"})
|
||||
← {"record":{"id":"xyz...","status":"pending"}}
|
||||
|
||||
Created shipment for order 123 going to London.
|
||||
```
|
||||
|
||||
Edit the generated code by hand at any time — re-running preserves your changes. [Read more](https://go-micro.dev/blog/13).
|
||||
|
||||
## Why an Agent Harness
|
||||
|
||||
The first wave of agent frameworks helped developers put a model in a loop. The next problem is operating that loop: connecting it to real tools, scoping what it can touch, preserving state, routing work to specialists, recovering from failures, observing what happened, and letting other agents call it. That is harness work.
|
||||
|
||||
Go Micro's answer is to make the harness the same thing you already deploy:
|
||||
|
||||
- **Tools are services** — endpoint metadata becomes tool schema; RPC executes the call.
|
||||
- **Agents are services** — they register, discover, load-balance, and expose `Agent.Chat`.
|
||||
- **Workflows are durable code paths** — use flows when the path is known; dispatch to agents when it is not.
|
||||
- **Safety lives at execution** — `MaxSteps`, `LoopLimit`, `ApproveTool`, and tool wrappers run where actions happen.
|
||||
- **Interop is built in** — MCP for tools, A2A for agents, x402 for paid tools.
|
||||
|
||||
Use Go Micro when the agent has to operate a system, not just answer a prompt.
|
||||
|
||||
## Writing Services
|
||||
|
||||
Under the hood, a service is a struct with methods. Doc comments and `@example` tags become tool descriptions for AI agents automatically.
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"go-micro.dev/v5"
|
||||
"context"
|
||||
|
||||
"go-micro.dev/v6"
|
||||
)
|
||||
|
||||
type Request struct {
|
||||
Name string `json:"name"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Message string `json:"message"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type Say struct{}
|
||||
|
||||
func (h *Say) Hello(ctx context.Context, req *Request, rsp *Response) error {
|
||||
rsp.Message = "Hello " + req.Name
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
// create the service
|
||||
service := micro.New("helloworld")
|
||||
|
||||
// register handler
|
||||
service.Handle(new(Say))
|
||||
|
||||
// run the service
|
||||
service.Run()
|
||||
}
|
||||
```
|
||||
|
||||
Set a fixed address
|
||||
|
||||
```go
|
||||
service := micro.New("helloworld", micro.Address(":8080"))
|
||||
```
|
||||
|
||||
Call it via curl
|
||||
|
||||
```bash
|
||||
curl -XPOST \
|
||||
-H 'Content-Type: application/json' \
|
||||
-H 'Micro-Endpoint: Say.Hello' \
|
||||
-d '{"name": "alice"}' \
|
||||
http://localhost:8080
|
||||
```
|
||||
|
||||
## MCP & AI Agents
|
||||
|
||||
<img src="internal/website/images/generated/mcp-agent.png" alt="AI agent calling microservices via MCP" width="100%" />
|
||||
|
||||
Go Micro is designed for an **agent-first** workflow. Every service you build automatically becomes a tool that AI agents can discover and use via the [Model Context Protocol (MCP)](https://modelcontextprotocol.io/).
|
||||
|
||||
- **[🤖 Agent Playground](https://go-micro.dev/docs/mcp.html)** — Chat with your services through an interactive AI agent at `/agent`
|
||||
- **[🔧 MCP Tools Registry](https://go-micro.dev/docs/mcp.html)** — Browse all services exposed as AI-callable tools at `/mcp/tools`
|
||||
- **[📖 MCP Documentation](https://go-micro.dev/docs/mcp.html)** — Full guide to MCP integration, auth, and scopes
|
||||
|
||||
### Services as Tools
|
||||
|
||||
Write a normal Go Micro service and it's instantly available as an MCP tool:
|
||||
|
||||
```go
|
||||
// SayHello greets a person by name.
|
||||
// Hello greets a person by name.
|
||||
// @example {"name": "Alice"}
|
||||
func (g *GreeterService) SayHello(ctx context.Context, req *HelloRequest, rsp *HelloResponse) error {
|
||||
func (h *Say) Hello(ctx context.Context, req *Request, rsp *Response) error {
|
||||
rsp.Message = "Hello " + req.Name
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
service := micro.NewService("greeter")
|
||||
service.Handle(new(Say))
|
||||
service.Run()
|
||||
}
|
||||
```
|
||||
|
||||
Run with `micro run` and the agent playground and MCP tools registry are ready:
|
||||
Run it and everything is accessible — REST, gRPC, MCP, agent playground:
|
||||
|
||||
```bash
|
||||
micro run
|
||||
# Agent Playground: http://localhost:8080/agent
|
||||
# MCP Tools: http://localhost:8080/mcp/tools
|
||||
# Dashboard: http://localhost:8080
|
||||
# API: http://localhost:8080/api/{service}/{method}
|
||||
# Agent: http://localhost:8080/agent
|
||||
# MCP Tools: http://localhost:8080/mcp/tools
|
||||
```
|
||||
|
||||
Use `micro mcp serve` for local AI tools like Claude Code, or connect any MCP-compatible agent to the HTTP endpoint.
|
||||
|
||||
### micro chat
|
||||
|
||||
For an interactive terminal session that lets you talk to your services through an LLM:
|
||||
You can also scaffold a service from a template:
|
||||
|
||||
```bash
|
||||
ANTHROPIC_API_KEY=sk-ant-... micro chat --provider anthropic
|
||||
> list all users
|
||||
> create an order for product 42
|
||||
micro new helloworld
|
||||
micro new contacts --template crud
|
||||
```
|
||||
|
||||
`micro chat` discovers every service in the registry, exposes each endpoint as a tool, and lets the model orchestrate calls. The same building blocks (`ai/tools`) work from your own services:
|
||||
## Building Agents
|
||||
|
||||
An Agent is a service with an LLM inside it. It has a proto-defined `Agent.Chat` RPC endpoint, registers in the registry, and is callable like any service:
|
||||
|
||||
```go
|
||||
import "go-micro.dev/v5/ai/tools"
|
||||
|
||||
set := tools.New(service.Registry())
|
||||
discovered, _ := set.Discover()
|
||||
|
||||
m := ai.New("anthropic",
|
||||
ai.WithAPIKey(key),
|
||||
ai.WithToolHandler(set.Handler(service.Client())),
|
||||
agent := micro.NewAgent("task-mgr",
|
||||
micro.AgentServices("task", "project"),
|
||||
micro.AgentPrompt("You manage tasks and projects. You understand deadlines and priorities."),
|
||||
micro.AgentProvider("anthropic"),
|
||||
)
|
||||
resp, _ := m.Generate(ctx, &ai.Request{
|
||||
Prompt: userInput,
|
||||
Tools: discovered,
|
||||
})
|
||||
agent.Run()
|
||||
```
|
||||
|
||||
See the [MCP guide](https://go-micro.dev/docs/mcp.html) for authentication, scopes, and advanced usage.
|
||||
|
||||
## Multi-Service Binaries
|
||||
|
||||
Run multiple services in a single binary — start as a modular monolith, split into separate deployments later when you actually need to.
|
||||
The agent discovers its services from the registry, scopes its tools to their endpoints, and maintains conversation memory in the store. It registers itself so `micro chat` and other agents can find it.
|
||||
|
||||
```go
|
||||
users := micro.New("users", micro.Address(":9001"))
|
||||
orders := micro.New("orders", micro.Address(":9002"))
|
||||
// Programmatic interaction
|
||||
resp, _ := agent.Ask(ctx, "What tasks are overdue?")
|
||||
fmt.Println(resp.Reply)
|
||||
```
|
||||
|
||||
Multiple agents coordinate via RPC — each is a service with an `Agent.Chat` endpoint. `micro chat` routes to the right one.
|
||||
|
||||
```bash
|
||||
micro agent list # list registered agents
|
||||
micro call task-mgr Agent.Chat '{"message": "What tasks are overdue?"}'
|
||||
```
|
||||
|
||||
### Plan & Delegate
|
||||
|
||||
Every agent gets two built-in harness capabilities, exposed as tools — no extra setup or separate graph runtime:
|
||||
|
||||
- **`plan`** — for multi-step work, the agent records an ordered plan in its store-backed memory and stays oriented across turns.
|
||||
- **`delegate`** — the agent hands a self-contained subtask to another agent. If a registered agent already owns the relevant services, the hand-off goes over RPC to that agent; otherwise a focused, short-lived sub-agent is created for the subtask with its own isolated context.
|
||||
|
||||
This keeps intelligence distributed: an agent doesn't need to know *how* to do everything, only *who* does. See [examples/agent-plan-delegate](examples/agent-plan-delegate/).
|
||||
|
||||
```go
|
||||
// A sub-agent is just an agent — created with New, talked to with Ask.
|
||||
// delegate-first: reuse a registered agent, or spin up a focused one.
|
||||
resp, _ := agent.Ask(ctx, "Plan the launch, create the tasks, and have comms notify the owner.")
|
||||
```
|
||||
|
||||
### Batteries included, pluggable
|
||||
|
||||
Just as a service composes pluggable abstractions (registry, broker, store), an agent composes a **model**, **memory**, and **tools** — sane defaults out of the box, each swappable.
|
||||
|
||||
```go
|
||||
agent := micro.NewAgent("assistant",
|
||||
micro.AgentProvider("anthropic"), // model — swap the provider
|
||||
micro.AgentMemory(micro.NewInMemory(50)), // memory — default is store-backed & durable
|
||||
micro.AgentTool("weather", "Get the weather for a city",
|
||||
map[string]any{"city": map[string]any{"type": "string"}},
|
||||
func(ctx context.Context, in map[string]any) (string, error) {
|
||||
return getWeather(in["city"].(string)) // tools beyond your services — any function
|
||||
}),
|
||||
micro.AgentMaxSteps(8), // guardrails
|
||||
)
|
||||
```
|
||||
|
||||
**Memory** is durable and store-backed by default (Postgres, NATS KV, or file), so an agent picks up where it left off after a restart — or supply your own with `AgentMemory`. **Tools** are your services automatically, plus any function you register with `AgentTool`.
|
||||
|
||||
### Paid tools (x402)
|
||||
|
||||
Every endpoint is an AI-callable tool — and it can be a *paid* tool. Go Micro supports [x402](https://x402.org), the HTTP 402 payment standard for agents, so a tool can require a stablecoin payment and an agent can settle it autonomously. It's opt-in and carries no crypto in the framework: verification is delegated to a pluggable facilitator (Coinbase, Alchemy, self-hosted), so Base and Solana are just different facilitators.
|
||||
|
||||
```bash
|
||||
# Charge for tool calls at the MCP gateway (off unless you set a pay-to address)
|
||||
micro mcp serve --x402-pay-to 0xYourAddress --x402-network solana --x402-amount 10000
|
||||
# Per-tool amounts via a config file
|
||||
micro mcp serve --x402-config x402.json
|
||||
```
|
||||
|
||||
See the [Payments (x402) guide](internal/website/docs/guides/x402-payments.md).
|
||||
|
||||
### Reachable by other agents (A2A)
|
||||
|
||||
Within a Go Micro system, agents reach each other over RPC. To make them reachable by agents on *other* frameworks, Go Micro speaks the [Agent2Agent (A2A) protocol](https://a2a-protocol.org). The A2A gateway discovers your agents from the registry, generates an Agent Card for each from its metadata — the same way the MCP gateway derives tools from service endpoints — and translates incoming A2A tasks to the agent's `Agent.Chat` RPC. No per-agent code: register an agent and it's reachable over A2A.
|
||||
|
||||
```bash
|
||||
micro a2a serve --address :4000 # gateway: expose every registered agent over A2A
|
||||
micro a2a list # agents and their Agent Card URLs
|
||||
```
|
||||
|
||||
Or skip the gateway entirely — an agent can serve its own A2A endpoint directly, handling tasks in-process:
|
||||
|
||||
```go
|
||||
micro.NewAgent("task-mgr", micro.AgentServices("task"), micro.AgentA2A(":4000"))
|
||||
```
|
||||
|
||||
It works both ways. To call an agent on another framework, an `a2a.Client` is wired into the two places that hand off work: `flow.A2A(url)` as a workflow step (the cross-framework `Dispatch`), and `delegate` to an `http(s)` URL from inside an agent.
|
||||
|
||||
MCP exposes your services as tools; A2A exposes your agents as agents. See the [A2A guide](internal/website/docs/guides/a2a-protocol.md).
|
||||
|
||||
## Features
|
||||
|
||||
### AI
|
||||
|
||||
| Feature | Details |
|
||||
|---------|---------|
|
||||
| Agents | `micro.NewAgent()` — intelligent layer that manages services |
|
||||
| Plan & delegate | Built-in agent tools — plan multi-step work, delegate subtasks to other agents |
|
||||
| Pluggable memory | Durable store-backed conversation memory by default; swap with `AgentMemory` |
|
||||
| Custom tools | `AgentTool` — give an agent any function as a tool, beyond its services |
|
||||
| Guardrails | `MaxSteps` (stop on count), `LoopLimit` (stop repeated no-progress calls), `ApproveTool` (human-in-the-loop) |
|
||||
| Tool middleware | `AgentWrapTool` — wrap tool execution for logging, metrics, or retries (like client/server wrappers) |
|
||||
| Workflows | `micro.NewFlow()` — event-driven; one step, ordered durable steps, or triggers an agent |
|
||||
| Durable execution | Checkpointed flow steps survive a crash and resume where they stopped; store-backed by default, pluggable backend |
|
||||
| MCP gateway | Every endpoint is an AI tool automatically |
|
||||
| A2A gateway | Every agent is reachable over the Agent2Agent protocol; cards generated from the registry (`micro a2a`) |
|
||||
| Payments (x402) | Opt-in per-call payments for tools via the x402 standard; pluggable facilitator (Base, Solana, …) |
|
||||
| 7 LLM providers | Anthropic, OpenAI, Gemini, Groq, Mistral, Together, Atlas Cloud |
|
||||
| Interactive console | `micro run` includes a chat console for talking to services |
|
||||
| Service generation | `micro run --prompt` — describe a system, get running services |
|
||||
|
||||
### Framework
|
||||
|
||||
| Feature | Details |
|
||||
|---------|---------|
|
||||
| Service registry | mDNS (default), Consul, etcd |
|
||||
| RPC client/server | gRPC transport, load balancing, streaming |
|
||||
| Pub/sub events | NATS, RabbitMQ, HTTP broker |
|
||||
| Key-value store | File (bbolt), Postgres, NATS KV |
|
||||
| Typed model layer | CRUD + queries, SQLite/Postgres backends |
|
||||
| Everything swappable | All abstractions are Go interfaces |
|
||||
|
||||
### Developer experience & deployment
|
||||
|
||||
| Feature | Details |
|
||||
|---------|---------|
|
||||
| Hot reload | `micro run` watches files, rebuilds on change |
|
||||
| Templates | `micro new --template crud/pubsub/api` |
|
||||
| One-command deploy | `micro deploy user@server` — SSH + systemd, no Docker |
|
||||
|
||||
## CLI
|
||||
|
||||
| Command | Purpose |
|
||||
|---------|---------|
|
||||
| `micro run --prompt "..."` | Generate services + agent, start with interactive console |
|
||||
| `micro run` | Dev mode: hot reload, gateway, interactive console |
|
||||
| `micro run -d` | Detached mode (no console) |
|
||||
| `micro chat` | Standalone chat (when not using micro run) |
|
||||
| `micro agent list` | List registered agents |
|
||||
| `micro new myservice` | Scaffold a service |
|
||||
| `micro call service endpoint '{}'` | Call a service or agent from the CLI |
|
||||
| `micro build` | Compile production binaries |
|
||||
| `micro deploy user@server` | Deploy via SSH + systemd |
|
||||
|
||||
## Multi-Service Projects
|
||||
|
||||
Run multiple services together:
|
||||
|
||||
```go
|
||||
users := micro.NewService("users", micro.Address(":9001"))
|
||||
orders := micro.NewService("orders", micro.Address(":9002"))
|
||||
|
||||
users.Handle(new(Users))
|
||||
orders.Handle(new(Orders))
|
||||
|
||||
// Run all services together with shared lifecycle
|
||||
g := micro.NewGroup(users, orders)
|
||||
g.Run()
|
||||
```
|
||||
|
||||
Each service gets its own server, client, store, and cache while sharing the registry, broker, and transport — so they can discover and call each other within the same process.
|
||||
|
||||
See the [multi-service example](examples/multi-service/) for a working demo.
|
||||
|
||||
## Data Model
|
||||
|
||||
Go Micro includes a typed data model layer for persistence. Define a struct, tag a key field, and get type-safe CRUD and query operations backed by memory, SQLite, or Postgres.
|
||||
|
||||
```go
|
||||
import (
|
||||
"go-micro.dev/v5/model"
|
||||
"go-micro.dev/v5/model/sqlite"
|
||||
)
|
||||
|
||||
// Define your data type
|
||||
type User struct {
|
||||
ID string `json:"id" model:"key"`
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email" model:"index"`
|
||||
Age int `json:"age"`
|
||||
}
|
||||
```
|
||||
|
||||
Register your types and use the model:
|
||||
|
||||
```go
|
||||
service := micro.New("users")
|
||||
|
||||
// Register and use the service's model backend
|
||||
db := service.Model()
|
||||
db.Register(&User{})
|
||||
|
||||
// CRUD operations
|
||||
db.Create(ctx, &User{ID: "1", Name: "Alice", Email: "alice@example.com", Age: 30})
|
||||
|
||||
user := &User{}
|
||||
db.Read(ctx, "1", user)
|
||||
|
||||
user.Name = "Alice Smith"
|
||||
db.Update(ctx, user)
|
||||
|
||||
db.Delete(ctx, "1", &User{})
|
||||
```
|
||||
|
||||
Query with filters, ordering, and pagination:
|
||||
|
||||
```go
|
||||
var results []*User
|
||||
|
||||
// Find users by field
|
||||
db.List(ctx, &results, model.Where("email", "alice@example.com"))
|
||||
|
||||
// Complex queries
|
||||
db.List(ctx, &results,
|
||||
model.WhereOp("age", ">=", 18),
|
||||
model.OrderDesc("name"),
|
||||
model.Limit(10),
|
||||
model.Offset(20),
|
||||
)
|
||||
|
||||
count, _ := users.Count(ctx, model.Where("age", 30))
|
||||
```
|
||||
|
||||
Swap backends with an option:
|
||||
|
||||
```go
|
||||
// Development: in-memory (default)
|
||||
service := micro.New("users")
|
||||
|
||||
// Production: SQLite or Postgres
|
||||
db, _ := sqlite.New(model.WithDSN("file:app.db"))
|
||||
service := micro.New("users", micro.Model(db))
|
||||
```
|
||||
|
||||
Every service gets `Client()`, `Server()`, and `Model()` — call services, handle requests, and save data all from the same interface.
|
||||
|
||||
## Examples
|
||||
|
||||
Check out [/examples](examples/) for runnable code:
|
||||
- [hello-world](examples/hello-world/) - Basic RPC service
|
||||
- [grpc-interop](examples/grpc-interop/) - Call go-micro from any gRPC client
|
||||
- [web-service](examples/web-service/) - HTTP REST API
|
||||
- [multi-service](examples/multi-service/) - Multiple services in one binary
|
||||
- [mcp](examples/mcp/) - MCP integration with AI agents
|
||||
|
||||
See [all examples](examples/README.md) for more.
|
||||
|
||||
## Protobuf
|
||||
|
||||
Install the code generator and see usage in the docs:
|
||||
|
||||
```bash
|
||||
go install go-micro.dev/v5/cmd/protoc-gen-micro@v5.16.0
|
||||
```
|
||||
|
||||
> **Note:** Use a specific version instead of `@latest` to avoid module path conflicts. See [releases](https://github.com/micro/go-micro/releases) for the latest version.
|
||||
|
||||
Docs: [`internal/website/docs/getting-started.md`](internal/website/docs/getting-started.md)
|
||||
|
||||
## Command Line
|
||||
|
||||
Install the CLI:
|
||||
|
||||
```
|
||||
go install go-micro.dev/v5/cmd/micro@v5.16.0
|
||||
```
|
||||
|
||||
> **Note:** Use a specific version instead of `@latest` to avoid module path conflicts. See [releases](https://github.com/micro/go-micro/releases) for the latest version.
|
||||
|
||||
### Quick Start
|
||||
|
||||
```bash
|
||||
micro new helloworld # Create a new service
|
||||
cd helloworld
|
||||
micro run # Run with API gateway and hot reload
|
||||
```
|
||||
|
||||
Then open http://localhost:8080 to see your service and call it from the browser.
|
||||
|
||||
### Development Workflow
|
||||
|
||||
| Stage | Command | Purpose |
|
||||
|-------|---------|---------|
|
||||
| **Develop** | `micro run` | Local dev with hot reload and API gateway |
|
||||
| **Build** | `micro build` | Compile production binaries |
|
||||
| **Deploy** | `micro deploy` | Push to a remote Linux server via SSH + systemd |
|
||||
| **Dashboard** | `micro server` | Optional production web UI with JWT auth |
|
||||
|
||||
### micro run
|
||||
|
||||
`micro run` starts your services with:
|
||||
- **Web Dashboard** - Browse and call services at `/`
|
||||
- **Agent Playground** - AI chat with MCP tools at `/agent`
|
||||
- **API Explorer** - Browse endpoints and schemas at `/api`
|
||||
- **API Gateway** - HTTP to RPC proxy at `/api/{service}/{method}` (no auth in dev mode)
|
||||
- **MCP Tools** - Services as AI tools at `/mcp/tools`
|
||||
- **Health Checks** - Aggregated health at `/health`
|
||||
- **Hot Reload** - Auto-rebuild on file changes
|
||||
|
||||
> **Note:** `micro run` and `micro server` use a unified gateway architecture. See [Gateway Architecture](cmd/micro/README.md#gateway-architecture) for details.
|
||||
|
||||
```bash
|
||||
micro run # Gateway on :8080
|
||||
micro run --address :3000 # Custom gateway port
|
||||
micro run --no-gateway # Services only
|
||||
micro run --env production # Use production environment
|
||||
```
|
||||
|
||||
### Configuration
|
||||
|
||||
For multi-service projects, create a `micro.mu` file:
|
||||
Or use a `micro.mu` config file:
|
||||
|
||||
```
|
||||
service users
|
||||
path ./users
|
||||
port 8081
|
||||
|
||||
service posts
|
||||
path ./posts
|
||||
port 8082
|
||||
service orders
|
||||
path ./orders
|
||||
depends users
|
||||
|
||||
env development
|
||||
DATABASE_URL sqlite://./dev.db
|
||||
```
|
||||
|
||||
The gateway runs on :8080 by default, so services should use other ports.
|
||||
## Data Model
|
||||
|
||||
### Deployment
|
||||
|
||||
Deploy to any Linux server with systemd:
|
||||
|
||||
```bash
|
||||
# On your server (one-time setup)
|
||||
curl -fsSL https://go-micro.dev/install.sh | sh
|
||||
sudo micro init --server
|
||||
|
||||
# From your laptop
|
||||
micro deploy user@your-server
|
||||
```
|
||||
|
||||
The deploy command:
|
||||
1. Builds binaries for Linux
|
||||
2. Copies via SSH to the server
|
||||
3. Sets up systemd services
|
||||
4. Verifies services are healthy
|
||||
|
||||
Optionally run `micro server` on the deployed machine for a production web dashboard with JWT auth, user management, and API explorer.
|
||||
|
||||
Manage deployed services:
|
||||
```bash
|
||||
micro status --remote user@server # Check status
|
||||
micro logs --remote user@server # View logs
|
||||
micro logs myservice --remote user@server -f # Follow specific service
|
||||
```
|
||||
|
||||
No Docker required. No Kubernetes. Just systemd.
|
||||
|
||||
See [internal/website/docs/deployment.md](internal/website/docs/deployment.md) for full deployment guide.
|
||||
|
||||
See [cmd/micro/README.md](cmd/micro/README.md) for full CLI documentation.
|
||||
|
||||
Docs: [`internal/website/docs`](internal/website/docs)
|
||||
|
||||
Package reference: https://pkg.go.dev/go-micro.dev/v5
|
||||
|
||||
**User Guides:**
|
||||
- [Getting Started](internal/website/docs/getting-started.md)
|
||||
- [Data Model](internal/website/docs/model.md)
|
||||
- [MCP & AI Agents](internal/website/docs/mcp.md)
|
||||
- [AI Provider Integration](internal/website/docs/guides/ai-provider-guide.md)
|
||||
- [Plugins Overview](internal/website/docs/plugins.md)
|
||||
- [Learn by Example](internal/website/docs/examples/index.md)
|
||||
- [Deployment Guide](internal/website/docs/deployment.md)
|
||||
|
||||
**Architecture & Performance:**
|
||||
- [Performance Considerations](internal/website/docs/performance.md)
|
||||
- [Reflection Usage & Philosophy](internal/website/docs/REFLECTION-EVALUATION-SUMMARY.md)
|
||||
|
||||
**Security:**
|
||||
- [TLS Security Migration](internal/website/docs/TLS_SECURITY_UPDATE.md)
|
||||
- [Security Migration Guide](internal/website/docs/SECURITY_MIGRATION.md)
|
||||
|
||||
## Supported AI Providers
|
||||
|
||||
Go Micro’s `ai` package gives every provider the same interface: `Init`, `Generate`, `Stream`, and functional options. Swap providers with a single import.
|
||||
|
||||
| Provider | Import | Default Model |
|
||||
|----------|--------|---------------|
|
||||
| **Anthropic** | `go-micro.dev/v5/ai/anthropic` | `claude-sonnet-4-20250514` |
|
||||
| **Google Gemini** | `go-micro.dev/v5/ai/gemini` | `gemini-2.5-flash` |
|
||||
| **Groq** | `go-micro.dev/v5/ai/groq` | `llama-3.3-70b-versatile` |
|
||||
| **Mistral** | `go-micro.dev/v5/ai/mistral` | `mistral-large-latest` |
|
||||
| **OpenAI** | `go-micro.dev/v5/ai/openai` | `gpt-4o` |
|
||||
| **Together AI** | `go-micro.dev/v5/ai/together` | `Llama-3.3-70B-Instruct-Turbo` |
|
||||
| **Atlas Cloud** | `go-micro.dev/v5/ai/atlascloud` | `llama-3.3-70b` |
|
||||
|
||||
Any provider that exposes an OpenAI-compatible API can also be used directly:
|
||||
Typed persistence with CRUD and queries:
|
||||
|
||||
```go
|
||||
m := ai.New("openai",
|
||||
ai.WithAPIKey("your-key"),
|
||||
ai.WithBaseURL("https://api.yourprovider.com"),
|
||||
)
|
||||
type User struct {
|
||||
ID string `json:"id" model:"key"`
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email" model:"index"`
|
||||
}
|
||||
|
||||
db := service.Model()
|
||||
db.Register(&User{})
|
||||
db.Create(ctx, &User{ID: "1", Name: "Alice", Email: "alice@example.com"})
|
||||
|
||||
var results []*User
|
||||
db.List(ctx, &results, model.Where("email", "alice@example.com"))
|
||||
```
|
||||
|
||||
**Want to add your platform?** See the [AI Provider Integration Guide](internal/website/docs/guides/ai-provider-guide.md) for how to implement `ai.Model` and submit a PR. We welcome both code contributions and sponsorships from AI infrastructure companies — reach out via a [GitHub issue](https://github.com/micro/go-micro/issues).
|
||||
Backends: memory (default), SQLite, Postgres.
|
||||
|
||||
## Adopters
|
||||
## AI Providers
|
||||
|
||||
- [Sourse](https://sourse.eu) - Work in the field of earth observation, including embedded Kubernetes running onboard aircraft, and we’ve built a mission management SaaS platform using Go Micro.
|
||||
Swap providers with a single import — same interface everywhere:
|
||||
|
||||
| Provider | Default Model |
|
||||
|----------|---------------|
|
||||
| Anthropic | `claude-sonnet-4-20250514` |
|
||||
| OpenAI | `gpt-4o` |
|
||||
| Google Gemini | `gemini-2.5-flash` |
|
||||
| Groq | `llama-3.3-70b-versatile` |
|
||||
| Mistral | `mistral-large-latest` |
|
||||
| Together AI | `Llama-3.3-70B-Instruct-Turbo` |
|
||||
| Atlas Cloud | `llama-3.3-70b` |
|
||||
|
||||
```go
|
||||
m := ai.New("anthropic", ai.WithAPIKey(key))
|
||||
resp, _ := m.Generate(ctx, &ai.Request{Prompt: "hello"})
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
- [hello-world](examples/hello-world/) — Basic RPC service
|
||||
- [multi-service](examples/multi-service/) — Multiple services in one binary
|
||||
- [mcp](examples/mcp/) — MCP integration with AI agents
|
||||
- [agent-plan-delegate](examples/agent-plan-delegate/) — Agent planning and multi-agent delegation
|
||||
- [grpc-interop](examples/grpc-interop/) — Call go-micro from any gRPC client
|
||||
|
||||
See [all examples](examples/README.md).
|
||||
|
||||
## Docs
|
||||
|
||||
- [Getting Started](internal/website/docs/getting-started.md)
|
||||
- [AI Integration](internal/website/docs/ai-integration.md)
|
||||
- [Agents and Workflows](internal/website/docs/guides/agents-and-workflows.md)
|
||||
- [Agent Design](internal/docs/AGENT_DESIGN.md)
|
||||
- [Plan & Delegate](internal/website/docs/guides/plan-delegate.md)
|
||||
- [Agent Guardrails](internal/website/docs/guides/agent-guardrails.md)
|
||||
- [Payments (x402)](internal/website/docs/guides/x402-payments.md)
|
||||
- [MCP & AI Agents](internal/website/docs/mcp.md)
|
||||
- [Data Model](internal/website/docs/model.md)
|
||||
- [Deployment](internal/website/docs/deployment.md)
|
||||
- [Plugins](internal/website/docs/plugins.md)
|
||||
|
||||
Package reference: https://pkg.go.dev/go-micro.dev/v6
|
||||
|
||||
+56
-160
@@ -1,182 +1,78 @@
|
||||
# Go Micro Roadmap
|
||||
|
||||
This roadmap outlines the planned features and improvements for Go Micro. Community feedback and contributions are welcome!
|
||||
Go Micro is an **agent harness** and service framework for Go. A harness is the
|
||||
runtime around an agent — the tools, memory, guardrails, workflows, state,
|
||||
discovery, and protocols it needs to operate a system rather than just answer a
|
||||
prompt. An agent is a distributed system — it discovers services, calls them,
|
||||
holds state, and recovers from failure — so the harness is the runtime services
|
||||
already have, and building an agent is building a service. The roadmap has two
|
||||
jobs: make **agentic development** excellent, and make the **developer experience**
|
||||
around it excellent.
|
||||
|
||||
> **See [internal/docs/ROADMAP_2026.md](internal/docs/ROADMAP_2026.md) for the AI-Native Era roadmap** focused on MCP integration, agent-first development, and business sustainability. This document covers general framework improvements.
|
||||
The full, current roadmap lives at **[go-micro.dev/docs/roadmap](https://go-micro.dev/docs/roadmap)**
|
||||
([source](internal/website/docs/roadmap.md)). The highlights:
|
||||
|
||||
## Current Focus (Q1 2026) - COMPLETE
|
||||
## Where we are (v6)
|
||||
|
||||
### Documentation & Developer Experience
|
||||
- [x] Modernize documentation structure
|
||||
- [x] Add learn-by-example guides
|
||||
- [x] Update issue templates
|
||||
- [x] MCP integration documentation
|
||||
- [x] Agent playground and MCP tools registry
|
||||
- [ ] Create video tutorials
|
||||
- [ ] Interactive documentation site
|
||||
- [ ] Plugin discovery dashboard
|
||||
Services, agents (`plan`/`delegate`, guardrails, memory, tool middleware), durable
|
||||
flows, the MCP and A2A gateways (both directions), x402 paid tools, secure by
|
||||
default.
|
||||
|
||||
### AI & Model Integration
|
||||
- [x] AI package with provider abstraction (`ai.Model` interface)
|
||||
- [x] Anthropic Claude provider (`ai/anthropic`)
|
||||
- [x] OpenAI GPT provider (`ai/openai`)
|
||||
- [x] Tool execution with auto-calling support
|
||||
- [x] Streaming support via `ai.Stream`
|
||||
## Principles
|
||||
|
||||
### Observability
|
||||
- [ ] OpenTelemetry native support
|
||||
- [ ] Auto-instrumentation for handlers
|
||||
- [ ] Metrics export standardization
|
||||
- [ ] Distributed tracing examples
|
||||
- [ ] Integration with popular observability platforms
|
||||
1. Build into what people run, never a separate product (no hosted platform, no
|
||||
enterprise edition, no VC).
|
||||
2. CLI-first — the CLI is the experience; UI must earn its place, never bloat.
|
||||
3. The getting-started flow is a contract: *0→1* (scaffold → run → call) and
|
||||
*0→hero* (a working multi-agent system) must always work and are verified on
|
||||
every change.
|
||||
4. Interaction matters as much as running — chatting with agents, inspecting runs
|
||||
and history, end to end.
|
||||
5. Battle-tested: works across every provider, fails safely, observable.
|
||||
|
||||
### Developer Tools
|
||||
- [x] `micro run` with hot reload and unified gateway
|
||||
- [x] `micro deploy` with SSH + systemd deployment
|
||||
- [x] `micro mcp` command suite (serve, list, test, docs, export)
|
||||
- [ ] `micro dev` with enhanced hot reload
|
||||
- [ ] Service templates (`micro new --template`)
|
||||
- [ ] Better error messages with suggestions
|
||||
- [ ] Debug tooling improvements
|
||||
- [ ] VS Code extension for Go Micro
|
||||
## Now — hardening
|
||||
|
||||
## Q2 2026
|
||||
- **Cross-provider conformance** — the same agent scenario across all seven
|
||||
providers, gated on keys, on a schedule.
|
||||
- **Failure & resilience** — timeouts, rate limits, cancellation, deadline/context
|
||||
propagation, retry/backoff.
|
||||
- **Getting-started contract** — define and CI-verify the 0→1 and 0→hero flows.
|
||||
|
||||
### Production Readiness
|
||||
- [x] Health check standardization
|
||||
- [x] Graceful shutdown improvements
|
||||
- [ ] Resource cleanup best practices
|
||||
- [ ] Load testing framework integration
|
||||
- [ ] Performance benchmarking suite
|
||||
## Next — agentic depth
|
||||
|
||||
### Cloud Native
|
||||
- [ ] Kubernetes operator
|
||||
- [ ] Helm charts for common setups
|
||||
- [ ] Service mesh integration guides (Istio, Linkerd)
|
||||
- [ ] Cloud provider quickstarts (AWS, GCP, Azure)
|
||||
- [ ] Multi-cluster patterns
|
||||
- **Durable agent loop** — resume a long run via `Checkpoint` (flows already do).
|
||||
- **Streaming** — `ai.Stream` + A2A `message/stream`, end to end.
|
||||
- **Agent observability** — `RunInfo` → OpenTelemetry spans.
|
||||
|
||||
### Security
|
||||
- [x] Bearer token authentication for MCP
|
||||
- [x] Per-tool scope enforcement
|
||||
- [x] Audit logging
|
||||
- [x] Rate limiting
|
||||
- [ ] mTLS by default option
|
||||
- [ ] Secret management integration (Vault, AWS Secrets Manager)
|
||||
- [ ] RBAC improvements
|
||||
- [ ] Security audit and hardening
|
||||
- [ ] CVE scanning and response process
|
||||
## Later
|
||||
|
||||
## Q3 2026
|
||||
- Memory management (summarization, retrieval/RAG); human-in-the-loop pause/resume;
|
||||
x402 live-facilitator conformance and paid remote tools with spend caps; A2A
|
||||
streaming, push notifications, and multi-turn tasks.
|
||||
|
||||
### Plugin Ecosystem
|
||||
- [ ] Plugin marketplace/registry
|
||||
- [ ] Plugin quality standards
|
||||
- [ ] Community plugin contributions
|
||||
- [ ] Plugin compatibility matrix
|
||||
- [ ] Auto-discovery of available plugins
|
||||
## Developer experience (ongoing)
|
||||
|
||||
### Streaming & Async
|
||||
- [ ] Improved streaming support
|
||||
- [x] Server-sent events (SSE) support (via MCP gateway)
|
||||
- [ ] WebSocket plugin
|
||||
- [ ] Event sourcing patterns
|
||||
- [ ] CQRS examples
|
||||
- A seamless CLI inner loop (scaffold → run → chat → inspect → deploy); UI
|
||||
discipline (trim what isn't great); a maintained real-world example that doubles
|
||||
as the 0→hero reference; docs kept in lockstep with the code.
|
||||
|
||||
### Testing
|
||||
- [ ] Mock generation tooling
|
||||
- [ ] Integration test helpers
|
||||
- [ ] Contract testing support
|
||||
- [ ] Chaos engineering examples
|
||||
- [ ] E2E testing framework
|
||||
## How it's sustained
|
||||
|
||||
## Q4 2026
|
||||
The framework is the product, funded by sponsorship from those who run it — not a
|
||||
hosted service, enterprise tier, or venture funding. See
|
||||
[the v6 story](https://go-micro.dev/blog/27).
|
||||
|
||||
### Performance
|
||||
- [ ] Connection pooling optimizations
|
||||
- [ ] Zero-allocation paths
|
||||
- [ ] gRPC performance improvements
|
||||
- [ ] Caching strategies guide
|
||||
- [ ] Performance profiling tools
|
||||
## Contributing & feedback
|
||||
|
||||
### Developer Productivity
|
||||
- [ ] Code generation improvements
|
||||
- [ ] Better IDE support
|
||||
- [ ] Debugging tools
|
||||
- [ ] Migration automation tools
|
||||
- [ ] Upgrade helpers
|
||||
Pick an item, open an issue to discuss the approach, and submit a PR. Or join the
|
||||
[Discord](https://discord.gg/WeMU5AGxD). Include tests, run `make test` and
|
||||
`make lint`.
|
||||
|
||||
### Community
|
||||
- [ ] Regular blog posts and case studies
|
||||
- [ ] Community spotlight program
|
||||
- [ ] Contribution rewards
|
||||
- [ ] Monthly community calls
|
||||
- [ ] Conference presence
|
||||
## Version support
|
||||
|
||||
## Long-term Vision
|
||||
- **v6** — active development (current).
|
||||
- **v5** — security fixes only.
|
||||
- **v4 and earlier** — end of life.
|
||||
|
||||
### Core Framework
|
||||
- Maintain backward compatibility (Go Micro v5+)
|
||||
- Progressive disclosure of complexity
|
||||
- Best-in-class developer experience
|
||||
- Production-grade reliability
|
||||
- Comprehensive plugin ecosystem
|
||||
|
||||
### Ecosystem Goals
|
||||
- 100+ production deployments documented
|
||||
- 50+ community plugins
|
||||
- Active contributor community
|
||||
- Regular releases (monthly patches, quarterly features)
|
||||
- Comprehensive benchmarks vs alternatives
|
||||
|
||||
### Differentiation
|
||||
- **Batteries included, fully swappable** - Start simple, scale complex
|
||||
- **Zero-config local development** - No infrastructure required to start
|
||||
- **AI-native by default** - Every service is an MCP tool automatically
|
||||
- **Plugin ecosystem in-repo** - No version compatibility hell
|
||||
- **Progressive complexity** - Learn as you grow
|
||||
- **Cloud-native first** - Built for Kubernetes and containers
|
||||
|
||||
## Contributing
|
||||
|
||||
We welcome contributions to any roadmap items! See [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
|
||||
|
||||
### High Priority Areas
|
||||
1. Documentation improvements (guides, tutorials)
|
||||
2. Multi-protocol MCP support (WebSocket, gRPC)
|
||||
3. Agent SDK integrations (LlamaIndex, AutoGPT)
|
||||
4. OpenTelemetry integration
|
||||
5. Kubernetes operator and Helm charts
|
||||
|
||||
### How to Contribute
|
||||
- Pick an item from the roadmap
|
||||
- Open an issue to discuss approach
|
||||
- Submit a PR with implementation
|
||||
- Help review others' contributions
|
||||
|
||||
## Feedback
|
||||
|
||||
Have suggestions for the roadmap?
|
||||
|
||||
- Open a [feature request](.github/ISSUE_TEMPLATE/feature_request.md)
|
||||
- Start a discussion in GitHub Discussions
|
||||
- Comment on existing roadmap issues
|
||||
|
||||
## Version Compatibility
|
||||
|
||||
We follow semantic versioning:
|
||||
- Major versions (v5 → v6): Breaking changes
|
||||
- Minor versions (v5.3 → v5.4): New features, backward compatible
|
||||
- Patch versions (v5.3.0 → v5.3.1): Bug fixes, no API changes
|
||||
|
||||
## Support Timeline
|
||||
|
||||
- v5: Active development (current)
|
||||
- v4: Security fixes only (until v6 release)
|
||||
- v3: End of life
|
||||
|
||||
---
|
||||
|
||||
Last updated: March 2026
|
||||
|
||||
This roadmap is subject to change based on community needs and priorities.
|
||||
Major versions (v5 → v6) carry breaking changes; minors are backward-compatible.
|
||||
See the [v5 → v6 migration guide](https://go-micro.dev/docs/guides/migration/v5-to-v6).
|
||||
|
||||
+1
-1
@@ -174,6 +174,6 @@ We currently do not offer a bug bounty program, but we greatly appreciate respon
|
||||
|
||||
For security questions that are not vulnerabilities, please:
|
||||
- Open a discussion: https://github.com/micro/go-micro/discussions
|
||||
- Join Discord: https://discord.gg/jwTYuUVAGh
|
||||
- Join Discord: https://discord.gg/WeMU5AGxD
|
||||
- Email: support@go-micro.dev
|
||||
|
||||
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
# Support
|
||||
|
||||
Go Micro is free and open source. There are two ways to get help: the community, and commercial support.
|
||||
|
||||
## Community support (free)
|
||||
|
||||
- **Documentation** — https://go-micro.dev/docs
|
||||
- **Examples** — https://github.com/micro/go-micro/tree/master/examples
|
||||
- **Bugs & features** — https://github.com/micro/go-micro/issues
|
||||
- **Questions** — open a [Question](https://github.com/micro/go-micro/issues/new?template=question.md) issue
|
||||
|
||||
Community support is best-effort, from maintainers and contributors, with no response-time guarantees.
|
||||
|
||||
## Commercial support
|
||||
|
||||
If you're running Go Micro in production — or building agents and services on it and want a hand — paid support and consulting are available directly from the maintainer. This is what keeps the project maintained.
|
||||
|
||||
| Tier | For | What you get | How |
|
||||
|------|-----|--------------|-----|
|
||||
| **Community** | Everyone | Docs, examples, issues — best-effort | Free |
|
||||
| **Sponsor** | Individuals & companies who rely on Go Micro | Back ongoing development; your name/logo in the README and on the site; a voice in priorities | [GitHub Sponsors](https://github.com/sponsors/asim) |
|
||||
| **Support** | Teams running Go Micro in production | Priority responses, a direct line to the maintainer, prioritized bug fixes, upgrade & integration help | [Open a request](#get-in-touch) |
|
||||
| **Consulting** | Teams building on Go Micro | Hands-on integration, architecture & agent-design review, training & onboarding, sponsored features | [Open a request](#get-in-touch) |
|
||||
|
||||
Recurring amounts are set on the [Sponsors page](https://github.com/sponsors/asim); support and consulting are scoped and quoted per engagement.
|
||||
|
||||
## Get in touch
|
||||
|
||||
Open a [**Commercial Support / Consulting**](https://github.com/micro/go-micro/issues/new?template=commercial_support.md) request — tell us what you're building, what you need, and your timeline, and we'll follow up. For anything you'd rather not discuss in public, become a [sponsor](https://github.com/sponsors/asim) and message privately.
|
||||
+365
@@ -0,0 +1,365 @@
|
||||
// Package agent provides the Agent abstraction for Go Micro.
|
||||
//
|
||||
// An Agent is a service with an LLM inside it. It registers a Chat
|
||||
// RPC endpoint, discovers its assigned services' tools, and
|
||||
// orchestrates them intelligently.
|
||||
//
|
||||
// agent := micro.NewAgent("task-mgr",
|
||||
// micro.AgentServices("task"),
|
||||
// micro.AgentPrompt("You manage tasks."),
|
||||
// micro.AgentProvider("anthropic"),
|
||||
// )
|
||||
// agent.Run()
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/google/uuid"
|
||||
pb "go-micro.dev/v6/agent/proto"
|
||||
"go-micro.dev/v6/ai"
|
||||
"go-micro.dev/v6/gateway/a2a"
|
||||
"go-micro.dev/v6/server"
|
||||
"go-micro.dev/v6/store"
|
||||
|
||||
_ "go-micro.dev/v6/ai/anthropic"
|
||||
_ "go-micro.dev/v6/ai/atlascloud"
|
||||
_ "go-micro.dev/v6/ai/gemini"
|
||||
_ "go-micro.dev/v6/ai/groq"
|
||||
_ "go-micro.dev/v6/ai/mistral"
|
||||
_ "go-micro.dev/v6/ai/openai"
|
||||
_ "go-micro.dev/v6/ai/together"
|
||||
)
|
||||
|
||||
// Agent is the interface for an AI agent that manages services.
|
||||
type Agent interface {
|
||||
Name() string
|
||||
Init(...Option)
|
||||
Options() Options
|
||||
Ask(ctx context.Context, message string) (*Response, error)
|
||||
Run() error
|
||||
Stop() error
|
||||
String() string
|
||||
}
|
||||
|
||||
// Response is what an agent returns from Chat.
|
||||
type Response struct {
|
||||
Reply string
|
||||
ToolCalls []ai.ToolCall
|
||||
Agent string
|
||||
|
||||
// RunID correlates this Ask with tool calls, trace spans, and the
|
||||
// persisted run timeline. ParentID is set when this response belongs
|
||||
// to a delegated sub-agent run.
|
||||
RunID string
|
||||
ParentID string
|
||||
}
|
||||
|
||||
type agentImpl struct {
|
||||
opts Options
|
||||
model ai.Model
|
||||
tools *ai.Tools
|
||||
mem Memory
|
||||
server server.Server
|
||||
mu sync.Mutex
|
||||
|
||||
// ephemeral marks a short-lived sub-agent created by delegation.
|
||||
// Ephemeral agents run with an isolated context: they load and
|
||||
// persist no history, and have no built-in tools (so they cannot
|
||||
// plan or re-delegate).
|
||||
ephemeral bool
|
||||
|
||||
// steps counts tool executions in the current Ask, for MaxSteps.
|
||||
steps int
|
||||
// calls counts identical tool calls (name+args) in the current Ask,
|
||||
// for LoopLimit.
|
||||
calls map[string]int
|
||||
|
||||
// runID correlates the tool calls of the current Ask; parentRunID is
|
||||
// the run that delegated to this one (set on ephemeral sub-agents).
|
||||
// Both are surfaced to tool wrappers via ai.RunInfo on the context.
|
||||
runID string
|
||||
parentRunID string
|
||||
}
|
||||
|
||||
// New creates a new Agent.
|
||||
func New(opts ...Option) Agent {
|
||||
return &agentImpl{
|
||||
opts: newOptions(opts...),
|
||||
}
|
||||
}
|
||||
|
||||
// newEphemeral creates a short-lived sub-agent for a delegated subtask.
|
||||
// It shares the parent's provider, model, and infrastructure but runs
|
||||
// with an isolated context: it loads and persists no history and has no
|
||||
// built-in tools (so it can neither plan nor re-delegate). Returns the
|
||||
// concrete type because ephemeral is an internal construction detail,
|
||||
// not a public option.
|
||||
func newEphemeral(opts ...Option) *agentImpl {
|
||||
return &agentImpl{
|
||||
opts: newOptions(opts...),
|
||||
ephemeral: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *agentImpl) Name() string {
|
||||
return a.opts.Name
|
||||
}
|
||||
|
||||
func (a *agentImpl) Init(opts ...Option) {
|
||||
for _, o := range opts {
|
||||
o(&a.opts)
|
||||
}
|
||||
a.setup()
|
||||
}
|
||||
|
||||
func (a *agentImpl) Options() Options {
|
||||
return a.opts
|
||||
}
|
||||
|
||||
func (a *agentImpl) String() string {
|
||||
return "agent"
|
||||
}
|
||||
|
||||
func (a *agentImpl) setup() {
|
||||
var modelOpts []ai.Option
|
||||
modelOpts = append(modelOpts, ai.WithAPIKey(a.opts.APIKey))
|
||||
if a.opts.Model != "" {
|
||||
modelOpts = append(modelOpts, ai.WithModel(a.opts.Model))
|
||||
}
|
||||
|
||||
a.tools = ai.NewTools(a.opts.Registry, ai.ToolClient(a.opts.Client))
|
||||
modelOpts = append(modelOpts, ai.WithToolHandler(a.toolHandler()))
|
||||
a.model = ai.New(a.opts.Provider, modelOpts...)
|
||||
if a.opts.TraceProvider != nil && a.model != nil {
|
||||
a.model = a.tracedModel(a.model)
|
||||
}
|
||||
|
||||
// Memory is pluggable. Use the configured one, otherwise the default
|
||||
// store-backed memory — except ephemeral sub-agents, which keep an
|
||||
// isolated, non-persistent context.
|
||||
switch {
|
||||
case a.opts.Memory != nil:
|
||||
a.mem = a.opts.Memory
|
||||
case a.ephemeral:
|
||||
a.mem = NewInMemory(a.opts.HistoryLimit)
|
||||
default:
|
||||
a.mem = NewMemory(a.stateStore(), "history", a.opts.HistoryLimit)
|
||||
}
|
||||
}
|
||||
|
||||
// stateStore returns the agent's own state store, scoped to its name so
|
||||
// memory and plan live in their own table ("agent/{name}") rather than a
|
||||
// shared global one. The scoped handle injects the database/table per
|
||||
// operation without mutating the underlying store.
|
||||
func (a *agentImpl) stateStore() store.Store {
|
||||
s := a.opts.Store
|
||||
if s == nil {
|
||||
s = store.DefaultStore
|
||||
}
|
||||
return store.Scope(s, "agent", a.opts.Name)
|
||||
}
|
||||
|
||||
// Ask sends a message and returns the agent's response.
|
||||
// This is the programmatic API for direct use.
|
||||
func (a *agentImpl) Ask(ctx context.Context, message string) (*Response, error) {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
|
||||
if a.model == nil {
|
||||
a.setup()
|
||||
}
|
||||
|
||||
toolList, err := a.discoverTools()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("discover tools: %w", err)
|
||||
}
|
||||
|
||||
a.mem.Add("user", message)
|
||||
a.steps = 0
|
||||
a.calls = map[string]int{}
|
||||
|
||||
// Correlate this run's tool calls and surface lineage to wrappers.
|
||||
a.runID = uuid.New().String()
|
||||
ctx = ai.WithRunInfo(ctx, ai.RunInfo{
|
||||
RunID: a.runID,
|
||||
ParentID: a.parentRunID,
|
||||
Agent: a.opts.Name,
|
||||
})
|
||||
ctx, endRun := a.startRun(ctx, message)
|
||||
defer func() { endRun(err) }()
|
||||
|
||||
resp, err := ai.GenerateWithRetry(ctx, a.model, &ai.Request{
|
||||
Prompt: message,
|
||||
SystemPrompt: a.buildPrompt(),
|
||||
Tools: toolList,
|
||||
Messages: a.mem.Messages(),
|
||||
}, ai.GeneratePolicy{
|
||||
Timeout: a.opts.ModelTimeout,
|
||||
MaxAttempts: a.opts.ModelMaxAttempts,
|
||||
Backoff: a.opts.ModelRetryBackoff,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.Reply != "" {
|
||||
a.mem.Add("assistant", resp.Reply)
|
||||
}
|
||||
if resp.Answer != "" {
|
||||
a.mem.Add("assistant", resp.Answer)
|
||||
}
|
||||
|
||||
reply := resp.Reply
|
||||
if resp.Answer != "" {
|
||||
if reply != "" {
|
||||
reply += "\n\n"
|
||||
}
|
||||
reply += resp.Answer
|
||||
}
|
||||
|
||||
return &Response{
|
||||
Reply: reply,
|
||||
ToolCalls: resp.ToolCalls,
|
||||
Agent: a.opts.Name,
|
||||
RunID: a.runID,
|
||||
ParentID: a.parentRunID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Chat implements the proto AgentHandler interface for RPC.
|
||||
// @example {"message": "What tasks are overdue?"}
|
||||
func (a *agentImpl) Chat(ctx context.Context, req *pb.ChatRequest, rsp *pb.ChatResponse) error {
|
||||
resp, err := a.Ask(ctx, req.Message)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rsp.Reply = resp.Reply
|
||||
rsp.Agent = resp.Agent
|
||||
for _, tc := range resp.ToolCalls {
|
||||
input, _ := json.Marshal(tc.Input)
|
||||
rsp.ToolCalls = append(rsp.ToolCalls, &pb.ToolCall{
|
||||
Id: tc.ID,
|
||||
Name: tc.Name,
|
||||
Input: string(input),
|
||||
Result: tc.Result,
|
||||
})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Run starts the agent as a service with a Chat RPC endpoint.
|
||||
func (a *agentImpl) Run() error {
|
||||
if a.model == nil {
|
||||
a.setup()
|
||||
}
|
||||
|
||||
a.server = server.NewServer(
|
||||
server.Name(a.opts.Name),
|
||||
server.Registry(a.opts.Registry),
|
||||
server.Metadata(map[string]string{
|
||||
"type": "agent",
|
||||
"services": strings.Join(a.opts.Services, ","),
|
||||
}),
|
||||
)
|
||||
|
||||
_ = pb.RegisterAgentHandler(a.server, a)
|
||||
|
||||
if err := a.server.Start(); err != nil {
|
||||
return fmt.Errorf("failed to start agent: %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Agent %s registered (manages: %s)\n", a.opts.Name, strings.Join(a.opts.Services, ", "))
|
||||
|
||||
// Optionally serve the agent directly over the A2A protocol, calling
|
||||
// Ask in-process — no separate gateway needed to be queried by URL.
|
||||
if a.opts.A2AAddress != "" {
|
||||
card := a2a.Card(a.opts.Name, "http://localhost"+a.opts.A2AAddress, "", a.opts.Services)
|
||||
handler := a2a.NewAgentHandler(card, func(ctx context.Context, text string) (string, error) {
|
||||
resp, err := a.Ask(ctx, text)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return resp.Reply, nil
|
||||
})
|
||||
go func() {
|
||||
if err := http.ListenAndServe(a.opts.A2AAddress, handler); err != nil {
|
||||
fmt.Printf("agent %s A2A server: %v\n", a.opts.Name, err)
|
||||
}
|
||||
}()
|
||||
fmt.Printf("Agent %s serving A2A on %s\n", a.opts.Name, a.opts.A2AAddress)
|
||||
}
|
||||
|
||||
ch := make(chan struct{})
|
||||
<-ch
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *agentImpl) Stop() error {
|
||||
if a.server != nil {
|
||||
return a.server.Stop()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *agentImpl) discoverTools() ([]ai.Tool, error) {
|
||||
all, err := a.tools.Discover()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var scoped []ai.Tool
|
||||
for _, t := range all {
|
||||
if strings.HasPrefix(t.OriginalName, a.opts.Name+".") {
|
||||
continue
|
||||
}
|
||||
if len(a.opts.Services) == 0 {
|
||||
scoped = append(scoped, t)
|
||||
continue
|
||||
}
|
||||
for _, svc := range a.opts.Services {
|
||||
if strings.HasPrefix(t.OriginalName, svc+".") {
|
||||
scoped = append(scoped, t)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Developer-registered custom tools (WithTool).
|
||||
for i := range a.opts.tools {
|
||||
scoped = append(scoped, a.opts.tools[i].def)
|
||||
}
|
||||
|
||||
// Expose the agent's own capabilities (plan, delegate) as tools.
|
||||
// Ephemeral sub-agents don't get them.
|
||||
if !a.ephemeral {
|
||||
scoped = append(scoped, builtinTools()...)
|
||||
}
|
||||
return scoped, nil
|
||||
}
|
||||
|
||||
func (a *agentImpl) buildPrompt() string {
|
||||
var base string
|
||||
switch {
|
||||
case a.opts.Prompt != "":
|
||||
base = a.opts.Prompt
|
||||
case len(a.opts.Services) > 0:
|
||||
base = fmt.Sprintf("You are the %s agent. You manage these services: %s. Use the available tools to fulfill requests.",
|
||||
a.opts.Name, strings.Join(a.opts.Services, ", "))
|
||||
default:
|
||||
base = fmt.Sprintf("You are the %s agent. Use the available tools to fulfill requests.", a.opts.Name)
|
||||
}
|
||||
|
||||
// Keep the agent oriented: surface its saved plan, if any.
|
||||
if !a.ephemeral {
|
||||
if plan := a.loadPlan(); plan != "" {
|
||||
base += "\n\nYour current plan (update it with the plan tool as you make progress):\n" + plan
|
||||
}
|
||||
}
|
||||
return base
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
a := New(
|
||||
Name("test-agent"),
|
||||
Services("task", "project"),
|
||||
Prompt("You manage tasks."),
|
||||
Provider("anthropic"),
|
||||
)
|
||||
|
||||
if a.Name() != "test-agent" {
|
||||
t.Errorf("Name() = %q, want %q", a.Name(), "test-agent")
|
||||
}
|
||||
|
||||
opts := a.Options()
|
||||
if opts.Provider != "anthropic" {
|
||||
t.Errorf("Provider = %q, want %q", opts.Provider, "anthropic")
|
||||
}
|
||||
if len(opts.Services) != 2 {
|
||||
t.Fatalf("Services = %v, want 2 items", opts.Services)
|
||||
}
|
||||
if opts.Services[0] != "task" || opts.Services[1] != "project" {
|
||||
t.Errorf("Services = %v, want [task project]", opts.Services)
|
||||
}
|
||||
if opts.Prompt != "You manage tasks." {
|
||||
t.Errorf("Prompt = %q, want %q", opts.Prompt, "You manage tasks.")
|
||||
}
|
||||
if opts.HistoryLimit != 50 {
|
||||
t.Errorf("HistoryLimit = %d, want 50", opts.HistoryLimit)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildPrompt(t *testing.T) {
|
||||
// Custom prompt
|
||||
a := New(Name("test"), Prompt("custom prompt")).(*agentImpl)
|
||||
if got := a.buildPrompt(); got != "custom prompt" {
|
||||
t.Errorf("buildPrompt() = %q, want %q", got, "custom prompt")
|
||||
}
|
||||
|
||||
// Auto-generated prompt with services
|
||||
a = New(Name("test"), Services("task", "project")).(*agentImpl)
|
||||
got := a.buildPrompt()
|
||||
if got == "" {
|
||||
t.Error("buildPrompt() returned empty")
|
||||
}
|
||||
if !contains(got, "task") || !contains(got, "project") {
|
||||
t.Errorf("buildPrompt() = %q, should mention services", got)
|
||||
}
|
||||
|
||||
// Auto-generated prompt without services
|
||||
a = New(Name("test")).(*agentImpl)
|
||||
got = a.buildPrompt()
|
||||
if !contains(got, "test") {
|
||||
t.Errorf("buildPrompt() = %q, should mention agent name", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaults(t *testing.T) {
|
||||
a := New(Name("test"))
|
||||
opts := a.Options()
|
||||
|
||||
if opts.Registry == nil {
|
||||
t.Error("Registry should default to DefaultRegistry")
|
||||
}
|
||||
if opts.Client == nil {
|
||||
t.Error("Client should default to DefaultClient")
|
||||
}
|
||||
if opts.Store == nil {
|
||||
t.Error("Store should default to DefaultStore")
|
||||
}
|
||||
}
|
||||
|
||||
func contains(s, sub string) bool {
|
||||
return len(s) >= len(sub) && (s == sub || len(s) > 0 && containsStr(s, sub))
|
||||
}
|
||||
|
||||
func containsStr(s, sub string) bool {
|
||||
for i := 0; i <= len(s)-len(sub); i++ {
|
||||
if s[i:i+len(sub)] == sub {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,342 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"go-micro.dev/v6/ai"
|
||||
codecBytes "go-micro.dev/v6/codec/bytes"
|
||||
"go-micro.dev/v6/gateway/a2a"
|
||||
"go-micro.dev/v6/store"
|
||||
)
|
||||
|
||||
// Built-in agent tools. These are not service endpoints — they are
|
||||
// capabilities the agent has over itself: maintaining a plan in its
|
||||
// memory, and delegating a subtask to another agent.
|
||||
//
|
||||
// They are plain tools, wired into the agent's tool handler alongside
|
||||
// the discovered service tools. There is no separate harness or graph:
|
||||
// the LLM calls them like any other tool.
|
||||
const (
|
||||
toolPlan = "plan"
|
||||
toolDelegate = "delegate"
|
||||
)
|
||||
|
||||
// builtinTools returns the tool definitions exposed to the model in
|
||||
// addition to the agent's scoped service tools.
|
||||
func builtinTools() []ai.Tool {
|
||||
return []ai.Tool{
|
||||
{
|
||||
Name: toolPlan,
|
||||
OriginalName: toolPlan,
|
||||
Description: "Record or update your plan as an ordered list of steps before doing multi-step work. " +
|
||||
"Call this whenever the plan changes. The plan is saved to your memory and shown back to you on later turns.",
|
||||
Properties: map[string]any{
|
||||
"steps": map[string]any{
|
||||
"type": "array",
|
||||
"description": "Ordered plan steps. Each step has a 'task' (string) and a " +
|
||||
"'status' (one of: pending, in_progress, done).",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: toolDelegate,
|
||||
OriginalName: toolDelegate,
|
||||
Description: "Delegate a self-contained subtask to another agent. If 'to' names an agent that already " +
|
||||
"manages the relevant services, that agent handles it; otherwise a focused sub-agent is created for the " +
|
||||
"subtask. The sub-agent works in an isolated context and returns only its result. Use this to keep your " +
|
||||
"own context focused and to let domain experts handle their own services.",
|
||||
Properties: map[string]any{
|
||||
"task": map[string]any{
|
||||
"type": "string",
|
||||
"description": "The subtask to delegate, described completely and self-contained.",
|
||||
},
|
||||
"to": map[string]any{
|
||||
"type": "string",
|
||||
"description": "Optional. The agent or service name best suited to the subtask, or the URL of an external agent that speaks the A2A protocol.",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Builtins returns the built-in agent tools (plan, delegate) together
|
||||
// with a handler for them, so the same capabilities can be wired into a
|
||||
// tool loop that isn't a running Agent — for example the `micro chat`
|
||||
// fallback. The handler's third return value is false when the name is
|
||||
// not a built-in, so callers can fall through to their own tools.
|
||||
//
|
||||
// Configure it with the same options as an Agent (Name, Provider,
|
||||
// WithStore, WithRegistry, WithClient, ...); these back plan's memory
|
||||
// and delegate's RPC/sub-agent behavior.
|
||||
func Builtins(opts ...Option) (tools []ai.Tool, handle func(name string, input map[string]any) (result any, content string, ok bool)) {
|
||||
a := &agentImpl{opts: newOptions(opts...)}
|
||||
handle = func(name string, input map[string]any) (any, string, bool) {
|
||||
switch name {
|
||||
case toolPlan:
|
||||
r := a.handlePlan(ai.ToolCall{Name: name, Input: input})
|
||||
return r.Value, r.Content, true
|
||||
case toolDelegate:
|
||||
r := a.handleDelegate(context.Background(), ai.ToolCall{Name: name, Input: input})
|
||||
return r.Value, r.Content, true
|
||||
}
|
||||
return nil, "", false
|
||||
}
|
||||
return builtinTools(), handle
|
||||
}
|
||||
|
||||
// toolHandler returns the agent's tool-call handler, composed as a stack
|
||||
// of wrappers around a base handler — the same middleware shape as
|
||||
// client/server wrappers. The base executes the call (custom tools,
|
||||
// delegate, or RPC); the built-in guardrails wrap it; developer wrappers
|
||||
// (WrapTool) wrap those, outermost, so they observe every call and its
|
||||
// result including guardrail refusals. Ephemeral sub-agents get the bare
|
||||
// service handler so they can neither plan nor re-delegate (which
|
||||
// prevents runaway recursion).
|
||||
func (a *agentImpl) toolHandler() ai.ToolHandler {
|
||||
if a.ephemeral {
|
||||
return a.tools.Handler()
|
||||
}
|
||||
|
||||
// Innermost first: base, then guardrails (approve → loop → step →
|
||||
// plan), then developer wrappers outermost. Wrapping reverses order,
|
||||
// so the result runs plan → step → loop → approve → base.
|
||||
h := a.baseHandler()
|
||||
h = a.approveWrap(h)
|
||||
h = a.loopWrap(h)
|
||||
h = a.stepWrap(h)
|
||||
h = a.planWrap(h)
|
||||
h = a.traceTool(h)
|
||||
for i := len(a.opts.wrappers) - 1; i >= 0; i-- {
|
||||
h = a.opts.wrappers[i](h)
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
// baseHandler executes a tool call: a developer custom tool, the built-in
|
||||
// delegate, or an RPC to the service. It is the innermost handler.
|
||||
func (a *agentImpl) baseHandler() ai.ToolHandler {
|
||||
rpc := a.tools.Handler()
|
||||
return func(ctx context.Context, call ai.ToolCall) ai.ToolResult {
|
||||
for i := range a.opts.tools {
|
||||
if a.opts.tools[i].def.Name == call.Name {
|
||||
out, err := a.opts.tools[i].handler(ctx, call.Input)
|
||||
if err != nil {
|
||||
return errResult(call.ID, err.Error())
|
||||
}
|
||||
return ai.ToolResult{ID: call.ID, Value: out, Content: out}
|
||||
}
|
||||
}
|
||||
if call.Name == toolDelegate {
|
||||
return a.handleDelegate(ctx, call)
|
||||
}
|
||||
return rpc(ctx, call)
|
||||
}
|
||||
}
|
||||
|
||||
// planWrap handles the plan tool inline. plan is internal bookkeeping,
|
||||
// not an action — it is never counted, loop-checked, or gated.
|
||||
func (a *agentImpl) planWrap(next ai.ToolHandler) ai.ToolHandler {
|
||||
return func(ctx context.Context, call ai.ToolCall) ai.ToolResult {
|
||||
if call.Name == toolPlan {
|
||||
return a.handlePlan(call)
|
||||
}
|
||||
return next(ctx, call)
|
||||
}
|
||||
}
|
||||
|
||||
// stepWrap bounds the number of actions per Ask (MaxSteps).
|
||||
func (a *agentImpl) stepWrap(next ai.ToolHandler) ai.ToolHandler {
|
||||
return func(ctx context.Context, call ai.ToolCall) ai.ToolResult {
|
||||
if a.opts.MaxSteps > 0 {
|
||||
a.steps++
|
||||
if a.steps > a.opts.MaxSteps {
|
||||
return refused(call.ID, ai.RefusedMaxSteps, fmt.Sprintf(
|
||||
"step limit reached (%d). Do not call any more tools; stop and summarize what you have so far.",
|
||||
a.opts.MaxSteps))
|
||||
}
|
||||
}
|
||||
return next(ctx, call)
|
||||
}
|
||||
}
|
||||
|
||||
// loopWrap stops the agent repeating an identical action that makes no
|
||||
// progress (which the step count alone won't catch).
|
||||
func (a *agentImpl) loopWrap(next ai.ToolHandler) ai.ToolHandler {
|
||||
return func(ctx context.Context, call ai.ToolCall) ai.ToolResult {
|
||||
if a.opts.LoopLimit > 0 {
|
||||
if a.calls == nil {
|
||||
a.calls = map[string]int{}
|
||||
}
|
||||
args, _ := json.Marshal(call.Input)
|
||||
fp := call.Name + ":" + string(args)
|
||||
a.calls[fp]++
|
||||
if a.calls[fp] > a.opts.LoopLimit {
|
||||
return refused(call.ID, ai.RefusedLoop, fmt.Sprintf(
|
||||
"loop detected: you have already called %q with the same arguments %d times and the result will not change. Stop repeating it — try a different approach, or finish with what you have.",
|
||||
call.Name, a.opts.LoopLimit))
|
||||
}
|
||||
}
|
||||
return next(ctx, call)
|
||||
}
|
||||
}
|
||||
|
||||
// approveWrap gates each action before it runs (ApproveTool).
|
||||
func (a *agentImpl) approveWrap(next ai.ToolHandler) ai.ToolHandler {
|
||||
return func(ctx context.Context, call ai.ToolCall) ai.ToolResult {
|
||||
if a.opts.Approve != nil {
|
||||
if ok, reason := a.opts.Approve(call.Name, call.Input); !ok {
|
||||
msg := "tool call was not approved"
|
||||
if reason != "" {
|
||||
msg += ": " + reason
|
||||
}
|
||||
return refused(call.ID, ai.RefusedApproval, msg)
|
||||
}
|
||||
}
|
||||
return next(ctx, call)
|
||||
}
|
||||
}
|
||||
|
||||
// handlePlan persists the supplied plan to the agent's memory and
|
||||
// echoes it back so the model can see the stored state.
|
||||
func (a *agentImpl) handlePlan(call ai.ToolCall) ai.ToolResult {
|
||||
data, err := json.Marshal(call.Input)
|
||||
if err != nil {
|
||||
return errResult(call.ID, "invalid plan: "+err.Error())
|
||||
}
|
||||
_ = a.stateStore().Write(&store.Record{Key: planKey, Value: data})
|
||||
return ai.ToolResult{ID: call.ID, Value: call.Input, Content: string(data)}
|
||||
}
|
||||
|
||||
// handleDelegate hands a subtask to another agent. Delegate-first:
|
||||
// if 'to' names a registered agent, it is called via RPC. Otherwise an
|
||||
// ephemeral sub-agent is created with a fresh, isolated context, asked
|
||||
// the subtask, and its reply returned.
|
||||
func (a *agentImpl) handleDelegate(ctx context.Context, call ai.ToolCall) ai.ToolResult {
|
||||
input := call.Input
|
||||
task, _ := input["task"].(string)
|
||||
if task == "" {
|
||||
return errResult(call.ID, "task is required")
|
||||
}
|
||||
to, _ := input["to"].(string)
|
||||
|
||||
// An external agent on another framework, addressed by A2A URL.
|
||||
if strings.HasPrefix(to, "http://") || strings.HasPrefix(to, "https://") {
|
||||
reply, err := a2a.NewClient(to).Send(ctx, task)
|
||||
if err != nil {
|
||||
return errResult(call.ID, "delegate to A2A agent "+to+": "+err.Error())
|
||||
}
|
||||
out := map[string]any{"agent": to, "reply": reply}
|
||||
b, _ := json.Marshal(out)
|
||||
return ai.ToolResult{ID: call.ID, Value: out, Content: string(b)}
|
||||
}
|
||||
|
||||
// Delegate-first: an existing agent that owns the domain handles it.
|
||||
if to != "" && a.isAgent(to) {
|
||||
reply, err := a.callAgentRPC(ctx, to, task)
|
||||
if err != nil {
|
||||
return errResult(call.ID, "delegate to agent "+to+": "+err.Error())
|
||||
}
|
||||
out := map[string]any{"agent": to, "reply": reply}
|
||||
b, _ := json.Marshal(out)
|
||||
return ai.ToolResult{ID: call.ID, Value: out, Content: string(b)}
|
||||
}
|
||||
|
||||
// Otherwise create a focused, ephemeral sub-agent. Fresh context:
|
||||
// it loads no history and persists none.
|
||||
var svcs []string
|
||||
if to != "" {
|
||||
svcs = []string{to}
|
||||
}
|
||||
sub := newEphemeral(
|
||||
Name(a.opts.Name+".sub"),
|
||||
Services(svcs...),
|
||||
Prompt("You are a sub-agent handling a single delegated subtask. "+
|
||||
"Complete it using the available tools and report the result concisely."),
|
||||
Provider(a.opts.Provider),
|
||||
Model(a.opts.Model),
|
||||
APIKey(a.opts.APIKey),
|
||||
WithRegistry(a.opts.Registry),
|
||||
WithClient(a.opts.Client),
|
||||
WithStore(a.opts.Store),
|
||||
TraceProvider(a.opts.TraceProvider),
|
||||
)
|
||||
// Record lineage so the sub-agent's tool calls carry this run as parent.
|
||||
sub.parentRunID = a.runID
|
||||
|
||||
resp, err := sub.Ask(ctx, task)
|
||||
if err != nil {
|
||||
return errResult(call.ID, "sub-agent: "+err.Error())
|
||||
}
|
||||
out := map[string]any{"reply": resp.Reply}
|
||||
b, _ := json.Marshal(out)
|
||||
return ai.ToolResult{ID: call.ID, Value: out, Content: string(b)}
|
||||
}
|
||||
|
||||
// isAgent reports whether name resolves to a registered agent (a
|
||||
// service advertising type=agent in its metadata).
|
||||
func (a *agentImpl) isAgent(name string) bool {
|
||||
if a.opts.Registry == nil {
|
||||
return false
|
||||
}
|
||||
recs, err := a.opts.Registry.GetService(name)
|
||||
if err != nil || len(recs) == 0 {
|
||||
return false
|
||||
}
|
||||
if recs[0].Metadata != nil && recs[0].Metadata["type"] == "agent" {
|
||||
return true
|
||||
}
|
||||
for _, n := range recs[0].Nodes {
|
||||
if n.Metadata != nil && n.Metadata["type"] == "agent" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// callAgentRPC calls another agent's Agent.Chat endpoint and returns
|
||||
// its reply.
|
||||
func (a *agentImpl) callAgentRPC(ctx context.Context, name, msg string) (string, error) {
|
||||
body, _ := json.Marshal(map[string]string{"message": msg})
|
||||
req := a.opts.Client.NewRequest(name, "Agent.Chat", &codecBytes.Frame{Data: body})
|
||||
var rsp codecBytes.Frame
|
||||
if err := a.opts.Client.Call(ctx, req, &rsp); err != nil {
|
||||
return "", err
|
||||
}
|
||||
var out struct {
|
||||
Reply string `json:"reply"`
|
||||
}
|
||||
if err := json.Unmarshal(rsp.Data, &out); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return out.Reply, nil
|
||||
}
|
||||
|
||||
// planKey is the record key for an agent's plan within its scoped store.
|
||||
const planKey = "plan"
|
||||
|
||||
// loadPlan returns the stored plan as a JSON string, or "" if none.
|
||||
func (a *agentImpl) loadPlan() string {
|
||||
recs, err := a.stateStore().Read(planKey)
|
||||
if err != nil || len(recs) == 0 {
|
||||
return ""
|
||||
}
|
||||
return string(recs[0].Value)
|
||||
}
|
||||
|
||||
func errResult(id, msg string) ai.ToolResult {
|
||||
m := map[string]string{"error": msg}
|
||||
b, _ := json.Marshal(m)
|
||||
return ai.ToolResult{ID: id, Value: m, Content: string(b)}
|
||||
}
|
||||
|
||||
// refused is an error result a guardrail returns, tagged with a structured
|
||||
// reason (ai.Refused*) so a tool wrapper can react to it without parsing
|
||||
// the message.
|
||||
func refused(id, reason, msg string) ai.ToolResult {
|
||||
r := errResult(id, msg)
|
||||
r.Refused = reason
|
||||
return r
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"go-micro.dev/v6/ai"
|
||||
"go-micro.dev/v6/registry"
|
||||
"go-micro.dev/v6/store"
|
||||
)
|
||||
|
||||
func TestBuiltinTools(t *testing.T) {
|
||||
tools := builtinTools()
|
||||
if len(tools) != 2 {
|
||||
t.Fatalf("builtinTools() = %d tools, want 2", len(tools))
|
||||
}
|
||||
names := map[string]bool{}
|
||||
for _, tl := range tools {
|
||||
names[tl.Name] = true
|
||||
}
|
||||
if !names[toolPlan] || !names[toolDelegate] {
|
||||
t.Errorf("builtin tools = %v, want plan and delegate", names)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlePlanPersists(t *testing.T) {
|
||||
mem := store.NewMemoryStore()
|
||||
a := New(Name("planner"), WithStore(mem)).(*agentImpl)
|
||||
|
||||
steps := map[string]any{
|
||||
"steps": []any{
|
||||
map[string]any{"task": "gather requirements", "status": "done"},
|
||||
map[string]any{"task": "write code", "status": "in_progress"},
|
||||
},
|
||||
}
|
||||
content := a.handlePlan(ai.ToolCall{Name: "plan", Input: steps}).Content
|
||||
if content == "" {
|
||||
t.Fatal("handlePlan returned empty content")
|
||||
}
|
||||
|
||||
// The plan must be retrievable from memory.
|
||||
got := a.loadPlan()
|
||||
if got == "" {
|
||||
t.Fatal("loadPlan() returned empty after handlePlan")
|
||||
}
|
||||
var decoded map[string]any
|
||||
if err := json.Unmarshal([]byte(got), &decoded); err != nil {
|
||||
t.Fatalf("stored plan is not valid JSON: %v", err)
|
||||
}
|
||||
if _, ok := decoded["steps"]; !ok {
|
||||
t.Errorf("stored plan missing steps: %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanShowsInPrompt(t *testing.T) {
|
||||
mem := store.NewMemoryStore()
|
||||
a := New(Name("planner"), Prompt("base prompt"), WithStore(mem)).(*agentImpl)
|
||||
|
||||
if got := a.buildPrompt(); got != "base prompt" {
|
||||
t.Errorf("buildPrompt() with no plan = %q, want %q", got, "base prompt")
|
||||
}
|
||||
|
||||
a.handlePlan(ai.ToolCall{Name: "plan", Input: map[string]any{"steps": []any{map[string]any{"task": "do it", "status": "pending"}}}})
|
||||
|
||||
got := a.buildPrompt()
|
||||
if got == "base prompt" {
|
||||
t.Error("buildPrompt() should include the plan once one is saved")
|
||||
}
|
||||
if !containsStr(got, "do it") {
|
||||
t.Errorf("buildPrompt() = %q, should contain the saved plan", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiscoverToolsIncludesBuiltins(t *testing.T) {
|
||||
reg := registry.NewMemoryRegistry()
|
||||
a := New(Name("a"), WithRegistry(reg), WithStore(store.NewMemoryStore())).(*agentImpl)
|
||||
a.setup()
|
||||
|
||||
tools, err := a.discoverTools()
|
||||
if err != nil {
|
||||
t.Fatalf("discoverTools: %v", err)
|
||||
}
|
||||
// No services registered, so the only tools should be the builtins.
|
||||
if len(tools) != len(builtinTools()) {
|
||||
t.Fatalf("discoverTools() = %d tools, want %d builtins", len(tools), len(builtinTools()))
|
||||
}
|
||||
}
|
||||
|
||||
func TestEphemeralAgentHasNoBuiltins(t *testing.T) {
|
||||
reg := registry.NewMemoryRegistry()
|
||||
a := New(Name("a.sub"), WithRegistry(reg), WithStore(store.NewMemoryStore())).(*agentImpl)
|
||||
a.ephemeral = true
|
||||
a.setup()
|
||||
|
||||
tools, err := a.discoverTools()
|
||||
if err != nil {
|
||||
t.Fatalf("discoverTools: %v", err)
|
||||
}
|
||||
if len(tools) != 0 {
|
||||
t.Errorf("ephemeral agent discoverTools() = %d tools, want 0", len(tools))
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuiltinsAccessor(t *testing.T) {
|
||||
mem := store.NewMemoryStore()
|
||||
tools, handle := Builtins(
|
||||
Name("chat"),
|
||||
WithStore(mem),
|
||||
WithRegistry(registry.NewMemoryRegistry()),
|
||||
)
|
||||
|
||||
if len(tools) != 2 {
|
||||
t.Fatalf("Builtins() returned %d tools, want 2", len(tools))
|
||||
}
|
||||
|
||||
// A name that isn't a built-in falls through (ok == false).
|
||||
if _, _, ok := handle("not_a_builtin", nil); ok {
|
||||
t.Error("handle(non-builtin) ok = true, want false")
|
||||
}
|
||||
|
||||
// plan is handled and persisted under the configured name.
|
||||
_, content, ok := handle(toolPlan, map[string]any{
|
||||
"steps": []any{map[string]any{"task": "x", "status": "pending"}},
|
||||
})
|
||||
if !ok {
|
||||
t.Fatal("handle(plan) ok = false, want true")
|
||||
}
|
||||
if content == "" {
|
||||
t.Fatal("handle(plan) returned empty content")
|
||||
}
|
||||
scoped := store.Scope(mem, "agent", "chat")
|
||||
if recs, err := scoped.Read(planKey); err != nil || len(recs) == 0 {
|
||||
t.Errorf("plan not persisted in the agent's scoped store: err=%v recs=%d", err, len(recs))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsAgent(t *testing.T) {
|
||||
reg := registry.NewMemoryRegistry()
|
||||
|
||||
// A plain service.
|
||||
if err := reg.Register(®istry.Service{
|
||||
Name: "task",
|
||||
Nodes: []*registry.Node{{Id: "task-1", Address: "127.0.0.1:0"}},
|
||||
}); err != nil {
|
||||
t.Fatalf("register service: %v", err)
|
||||
}
|
||||
// An agent (advertises type=agent).
|
||||
if err := reg.Register(®istry.Service{
|
||||
Name: "task-mgr",
|
||||
Metadata: map[string]string{"type": "agent"},
|
||||
Nodes: []*registry.Node{{Id: "task-mgr-1", Address: "127.0.0.1:0"}},
|
||||
}); err != nil {
|
||||
t.Fatalf("register agent: %v", err)
|
||||
}
|
||||
|
||||
a := New(Name("root"), WithRegistry(reg)).(*agentImpl)
|
||||
|
||||
if a.isAgent("task") {
|
||||
t.Error("isAgent(task) = true, want false (plain service)")
|
||||
}
|
||||
if !a.isAgent("task-mgr") {
|
||||
t.Error("isAgent(task-mgr) = false, want true (agent)")
|
||||
}
|
||||
if a.isAgent("nonexistent") {
|
||||
t.Error("isAgent(nonexistent) = true, want false")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"go-micro.dev/v6/ai"
|
||||
"go-micro.dev/v6/registry"
|
||||
"go-micro.dev/v6/store"
|
||||
)
|
||||
|
||||
// toolContent runs a tool call through a handler and returns the content
|
||||
// shown to the model — the part these tests assert on.
|
||||
func toolContent(h ai.ToolHandler, name string, input map[string]any) string {
|
||||
return h(context.Background(), ai.ToolCall{Name: name, Input: input}).Content
|
||||
}
|
||||
|
||||
// MaxSteps refuses tool calls once the per-Ask limit is exceeded; plan
|
||||
// is bookkeeping and is never counted.
|
||||
func TestMaxStepsStopsActions(t *testing.T) {
|
||||
a := newTestAgent(Name("limited"), MaxSteps(2))
|
||||
|
||||
h := a.toolHandler()
|
||||
|
||||
// plan must not consume a step.
|
||||
a.steps = 0
|
||||
toolContent(h, toolPlan, map[string]any{"steps": []any{}})
|
||||
if a.steps != 0 {
|
||||
t.Fatalf("plan consumed a step: steps=%d", a.steps)
|
||||
}
|
||||
|
||||
// First two actions are allowed (they fall through to RPC, which
|
||||
// fails harmlessly — we only care they weren't refused by the limit).
|
||||
for i := 1; i <= 2; i++ {
|
||||
content := toolContent(h, "demo_Svc_Do", map[string]any{})
|
||||
if strings.Contains(content, "step limit") {
|
||||
t.Fatalf("action %d wrongly hit the step limit", i)
|
||||
}
|
||||
}
|
||||
|
||||
// Third action exceeds MaxSteps(2) and must be refused.
|
||||
content := toolContent(h, "demo_Svc_Do", map[string]any{})
|
||||
if !strings.Contains(content, "step limit") {
|
||||
t.Errorf("third action should hit the step limit; got %q", content)
|
||||
}
|
||||
}
|
||||
|
||||
// ApproveTool blocks an action when the hook denies it, and the denial
|
||||
// reason is surfaced to the model.
|
||||
func TestApproveToolBlocks(t *testing.T) {
|
||||
var sawTool string
|
||||
a := newTestAgent(Name("gated"),
|
||||
ApproveTool(func(tool string, input map[string]any) (bool, string) {
|
||||
sawTool = tool
|
||||
return false, "needs sign-off"
|
||||
}),
|
||||
)
|
||||
|
||||
content := toolContent(a.toolHandler(), "demo_Svc_Do", map[string]any{})
|
||||
if sawTool != "demo_Svc_Do" {
|
||||
t.Errorf("approver saw %q, want demo_Svc_Do", sawTool)
|
||||
}
|
||||
if !strings.Contains(content, "not approved") || !strings.Contains(content, "needs sign-off") {
|
||||
t.Errorf("blocked call should surface the reason; got %q", content)
|
||||
}
|
||||
}
|
||||
|
||||
// A denying approver must not gate the internal plan tool.
|
||||
func TestApproveToolDoesNotGatePlan(t *testing.T) {
|
||||
mem := store.NewMemoryStore()
|
||||
a := New(
|
||||
Name("gated"),
|
||||
Provider("fake"),
|
||||
WithRegistry(registry.NewMemoryRegistry()),
|
||||
WithStore(mem),
|
||||
ApproveTool(func(tool string, input map[string]any) (bool, string) {
|
||||
return false, "deny everything"
|
||||
}),
|
||||
).(*agentImpl)
|
||||
a.setup()
|
||||
|
||||
content := toolContent(a.toolHandler(), toolPlan, map[string]any{
|
||||
"steps": []any{map[string]any{"task": "x", "status": "pending"}},
|
||||
})
|
||||
if strings.Contains(content, "not approved") {
|
||||
t.Errorf("plan must not be gated by ApproveTool; got %q", content)
|
||||
}
|
||||
if recs, _ := store.Scope(mem, "agent", "gated").Read(planKey); len(recs) == 0 {
|
||||
t.Error("plan should have been persisted despite the denying approver")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"go-micro.dev/v6/ai"
|
||||
"go-micro.dev/v6/client"
|
||||
codecBytes "go-micro.dev/v6/codec/bytes"
|
||||
"go-micro.dev/v6/registry"
|
||||
"go-micro.dev/v6/store"
|
||||
)
|
||||
|
||||
// fakeGen drives the fake provider's Generate. Tests set it and reset
|
||||
// it with a deferred cleanup. Tests in this package are not parallel,
|
||||
// so a package-level hook is safe.
|
||||
var fakeGen func(ctx context.Context, opts ai.Options, req *ai.Request) (*ai.Response, error)
|
||||
|
||||
type fakeModel struct{ opts ai.Options }
|
||||
|
||||
func (m *fakeModel) Init(opts ...ai.Option) error {
|
||||
for _, o := range opts {
|
||||
o(&m.opts)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (m *fakeModel) Options() ai.Options { return m.opts }
|
||||
func (m *fakeModel) Generate(ctx context.Context, req *ai.Request, _ ...ai.GenerateOption) (*ai.Response, error) {
|
||||
if fakeGen != nil {
|
||||
return fakeGen(ctx, m.opts, req)
|
||||
}
|
||||
return &ai.Response{Reply: "ok"}, nil
|
||||
}
|
||||
func (m *fakeModel) Stream(ctx context.Context, req *ai.Request, _ ...ai.GenerateOption) (ai.Stream, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (m *fakeModel) String() string { return "fake" }
|
||||
|
||||
func init() {
|
||||
ai.Register("fake", func(opts ...ai.Option) ai.Model {
|
||||
m := &fakeModel{}
|
||||
_ = m.Init(opts...)
|
||||
return m
|
||||
})
|
||||
}
|
||||
|
||||
// fakeClient embeds the default client (so NewRequest works) and
|
||||
// overrides Call with a test-supplied function.
|
||||
type fakeClient struct {
|
||||
client.Client
|
||||
callFn func(ctx context.Context, req client.Request, rsp interface{}) error
|
||||
}
|
||||
|
||||
func (c *fakeClient) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
||||
return c.callFn(ctx, req, rsp)
|
||||
}
|
||||
|
||||
func newTestAgent(opts ...Option) *agentImpl {
|
||||
base := []Option{
|
||||
Provider("fake"),
|
||||
WithRegistry(registry.NewMemoryRegistry()),
|
||||
WithStore(store.NewMemoryStore()),
|
||||
}
|
||||
a := New(append(base, opts...)...).(*agentImpl)
|
||||
a.setup()
|
||||
return a
|
||||
}
|
||||
|
||||
// The model is offered the plan and delegate tools, and calling the
|
||||
// plan tool persists the plan to memory.
|
||||
func TestAskExposesAndRunsPlan(t *testing.T) {
|
||||
var sawPlan, sawDelegate bool
|
||||
fakeGen = func(ctx context.Context, opts ai.Options, req *ai.Request) (*ai.Response, error) {
|
||||
for _, tl := range req.Tools {
|
||||
switch tl.Name {
|
||||
case toolPlan:
|
||||
sawPlan = true
|
||||
case toolDelegate:
|
||||
sawDelegate = true
|
||||
}
|
||||
}
|
||||
// Simulate the model recording a plan.
|
||||
if opts.ToolHandler != nil {
|
||||
opts.ToolHandler(context.Background(), ai.ToolCall{
|
||||
Name: toolPlan,
|
||||
Input: map[string]any{
|
||||
"steps": []any{map[string]any{"task": "step one", "status": "pending"}},
|
||||
},
|
||||
})
|
||||
}
|
||||
return &ai.Response{Answer: "done"}, nil
|
||||
}
|
||||
defer func() { fakeGen = nil }()
|
||||
|
||||
a := newTestAgent(Name("worker"))
|
||||
resp, err := a.Ask(context.Background(), "do some multi-step work")
|
||||
if err != nil {
|
||||
t.Fatalf("Ask: %v", err)
|
||||
}
|
||||
if !sawPlan || !sawDelegate {
|
||||
t.Errorf("model should be offered plan and delegate tools: plan=%v delegate=%v", sawPlan, sawDelegate)
|
||||
}
|
||||
if resp.Reply == "" {
|
||||
t.Error("Ask returned empty reply")
|
||||
}
|
||||
if plan := a.loadPlan(); !strings.Contains(plan, "step one") {
|
||||
t.Errorf("plan tool result not persisted; loadPlan() = %q", plan)
|
||||
}
|
||||
}
|
||||
|
||||
// Delegating with no matching agent creates an ephemeral sub-agent with
|
||||
// a fresh, isolated context (no builtin tools) and returns its reply.
|
||||
func TestDelegateEphemeral(t *testing.T) {
|
||||
fakeGen = func(ctx context.Context, opts ai.Options, req *ai.Request) (*ai.Response, error) {
|
||||
if strings.Contains(req.SystemPrompt, "sub-agent") {
|
||||
for _, tl := range req.Tools {
|
||||
if tl.Name == toolPlan || tl.Name == toolDelegate {
|
||||
t.Errorf("ephemeral sub-agent must not have builtin tool %q", tl.Name)
|
||||
}
|
||||
}
|
||||
return &ai.Response{Reply: "subtask complete"}, nil
|
||||
}
|
||||
return &ai.Response{Reply: "parent"}, nil
|
||||
}
|
||||
defer func() { fakeGen = nil }()
|
||||
|
||||
a := newTestAgent(Name("root"))
|
||||
content := a.handleDelegate(context.Background(), ai.ToolCall{Name: "delegate", Input: map[string]any{"task": "summarize the report"}}).Content
|
||||
if !strings.Contains(content, "subtask complete") {
|
||||
t.Errorf("delegate should return the sub-agent's reply; got %q", content)
|
||||
}
|
||||
}
|
||||
|
||||
// Delegating to a name that resolves to a registered agent goes over
|
||||
// RPC to that agent rather than spawning a sub-agent.
|
||||
func TestDelegateToRegisteredAgent(t *testing.T) {
|
||||
reg := registry.NewMemoryRegistry()
|
||||
if err := reg.Register(®istry.Service{
|
||||
Name: "comms",
|
||||
Metadata: map[string]string{"type": "agent"},
|
||||
Nodes: []*registry.Node{{Id: "comms-1", Address: "127.0.0.1:0"}},
|
||||
}); err != nil {
|
||||
t.Fatalf("register agent: %v", err)
|
||||
}
|
||||
|
||||
var calledService, calledEndpoint string
|
||||
fc := &fakeClient{Client: client.DefaultClient}
|
||||
fc.callFn = func(ctx context.Context, req client.Request, rsp interface{}) error {
|
||||
calledService, calledEndpoint = req.Service(), req.Endpoint()
|
||||
frame := rsp.(*codecBytes.Frame)
|
||||
frame.Data = []byte(`{"reply":"notified alice","agent":"comms"}`)
|
||||
return nil
|
||||
}
|
||||
|
||||
// fakeGen guards against the ephemeral path being taken by mistake.
|
||||
fakeGen = func(ctx context.Context, opts ai.Options, req *ai.Request) (*ai.Response, error) {
|
||||
t.Error("delegate to a registered agent must not spawn a sub-agent")
|
||||
return &ai.Response{}, nil
|
||||
}
|
||||
defer func() { fakeGen = nil }()
|
||||
|
||||
a := newTestAgent(Name("root"), WithRegistry(reg), WithClient(fc))
|
||||
content := a.handleDelegate(context.Background(), ai.ToolCall{Name: "delegate", Input: map[string]any{"task": "notify alice", "to": "comms"}}).Content
|
||||
|
||||
if calledService != "comms" || calledEndpoint != "Agent.Chat" {
|
||||
t.Errorf("expected RPC to comms Agent.Chat, got %s %s", calledService, calledEndpoint)
|
||||
}
|
||||
if !strings.Contains(content, "notified alice") {
|
||||
t.Errorf("delegate-first result missing agent reply; got %q", content)
|
||||
}
|
||||
}
|
||||
|
||||
// Delegate requires a task.
|
||||
func TestDelegateRequiresTask(t *testing.T) {
|
||||
a := newTestAgent(Name("root"))
|
||||
content := a.handleDelegate(context.Background(), ai.ToolCall{Name: "delegate", Input: map[string]any{}}).Content
|
||||
if !strings.Contains(content, "error") {
|
||||
t.Errorf("delegate with no task should error; got %q", content)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Repeating the same tool call with the same arguments is refused once it
|
||||
// exceeds LoopLimit, and the model is told to change approach.
|
||||
func TestLoopDetectionStopsRepeats(t *testing.T) {
|
||||
a := newTestAgent(Name("looper"), LoopLimit(3))
|
||||
h := a.toolHandler()
|
||||
|
||||
// First 3 identical calls are allowed (they fall through to RPC,
|
||||
// which fails harmlessly — we only care they weren't refused as loops).
|
||||
for i := 1; i <= 3; i++ {
|
||||
content := toolContent(h, "demo_Svc_Do", map[string]any{"q": "x"})
|
||||
if strings.Contains(content, "loop detected") {
|
||||
t.Fatalf("call %d wrongly flagged as a loop", i)
|
||||
}
|
||||
}
|
||||
|
||||
// The 4th identical call is refused as a loop.
|
||||
content := toolContent(h, "demo_Svc_Do", map[string]any{"q": "x"})
|
||||
if !strings.Contains(content, "loop detected") {
|
||||
t.Errorf("4th identical call should be refused as a loop; got %q", content)
|
||||
}
|
||||
}
|
||||
|
||||
// Different arguments are not a loop, even past the limit.
|
||||
func TestLoopDetectionAllowsDistinctCalls(t *testing.T) {
|
||||
a := newTestAgent(Name("distinct"), LoopLimit(2))
|
||||
h := a.toolHandler()
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
content := toolContent(h, "demo_Svc_Do", map[string]any{"q": i}) // distinct args each time
|
||||
if strings.Contains(content, "loop detected") {
|
||||
t.Fatalf("distinct call %d wrongly flagged as a loop", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// LoopLimit(0) disables detection.
|
||||
func TestLoopDetectionDisabled(t *testing.T) {
|
||||
a := newTestAgent(Name("noloop"), LoopLimit(0))
|
||||
h := a.toolHandler()
|
||||
for i := 0; i < 6; i++ {
|
||||
content := toolContent(h, "demo_Svc_Do", map[string]any{"q": "same"})
|
||||
if strings.Contains(content, "loop detected") {
|
||||
t.Fatalf("loop detection should be disabled with LoopLimit(0)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// It defaults on (lenient) so repeated identical calls are caught without
|
||||
// any configuration.
|
||||
func TestLoopDetectionDefaultOn(t *testing.T) {
|
||||
a := New(Name("d"), Provider("fake")).(*agentImpl)
|
||||
a.setup()
|
||||
if a.opts.LoopLimit <= 0 {
|
||||
t.Fatalf("LoopLimit should default on, got %d", a.opts.LoopLimit)
|
||||
}
|
||||
h := a.toolHandler()
|
||||
var lastContent string
|
||||
for i := 0; i < a.opts.LoopLimit+1; i++ {
|
||||
lastContent = toolContent(h, "demo_Svc_Do", map[string]any{})
|
||||
}
|
||||
if !strings.Contains(lastContent, "loop detected") {
|
||||
t.Errorf("default loop detection should catch repeated calls; got %q", lastContent)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sync"
|
||||
|
||||
"go-micro.dev/v6/ai"
|
||||
"go-micro.dev/v6/store"
|
||||
)
|
||||
|
||||
// Memory is an agent's conversation memory. Like the rest of the
|
||||
// framework it is pluggable: the default is store-backed and durable
|
||||
// across restarts, but any implementation can be supplied with
|
||||
// WithMemory — in-process, a database, or a semantic/vector store.
|
||||
type Memory interface {
|
||||
// Add appends a message to the conversation.
|
||||
Add(role, content string)
|
||||
// Messages returns the retained conversation, oldest first.
|
||||
Messages() []ai.Message
|
||||
// Clear resets the conversation.
|
||||
Clear()
|
||||
}
|
||||
|
||||
// NewMemory returns the default store-backed memory: an in-process
|
||||
// conversation buffer (truncated to limit) that persists to the store
|
||||
// under key, so an agent picks up where it left off after a restart.
|
||||
// A nil store or empty key yields non-persistent memory.
|
||||
func NewMemory(s store.Store, key string, limit int) Memory {
|
||||
m := &storeMemory{store: s, key: key, hist: ai.NewHistory(limit)}
|
||||
m.load()
|
||||
return m
|
||||
}
|
||||
|
||||
// NewInMemory returns conversation memory that is not persisted.
|
||||
func NewInMemory(limit int) Memory {
|
||||
return &storeMemory{hist: ai.NewHistory(limit)}
|
||||
}
|
||||
|
||||
// storeMemory is the default Memory: an ai.History buffer optionally
|
||||
// persisted to a store.
|
||||
type storeMemory struct {
|
||||
mu sync.Mutex
|
||||
store store.Store
|
||||
key string
|
||||
hist *ai.History
|
||||
}
|
||||
|
||||
func (m *storeMemory) Add(role, content string) {
|
||||
m.mu.Lock()
|
||||
m.hist.Add(role, content)
|
||||
m.mu.Unlock()
|
||||
m.save()
|
||||
}
|
||||
|
||||
func (m *storeMemory) Messages() []ai.Message {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
return m.hist.Messages()
|
||||
}
|
||||
|
||||
func (m *storeMemory) Clear() {
|
||||
m.mu.Lock()
|
||||
m.hist.Reset()
|
||||
m.mu.Unlock()
|
||||
m.save()
|
||||
}
|
||||
|
||||
func (m *storeMemory) load() {
|
||||
if m.store == nil || m.key == "" {
|
||||
return
|
||||
}
|
||||
recs, err := m.store.Read(m.key)
|
||||
if err != nil || len(recs) == 0 {
|
||||
return
|
||||
}
|
||||
var msgs []ai.Message
|
||||
if err := json.Unmarshal(recs[0].Value, &msgs); err != nil {
|
||||
return
|
||||
}
|
||||
m.mu.Lock()
|
||||
for _, msg := range msgs {
|
||||
m.hist.Add(msg.Role, msg.Content)
|
||||
}
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
func (m *storeMemory) save() {
|
||||
if m.store == nil || m.key == "" {
|
||||
return
|
||||
}
|
||||
m.mu.Lock()
|
||||
data, err := json.Marshal(m.hist.Messages())
|
||||
m.mu.Unlock()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_ = m.store.Write(&store.Record{Key: m.key, Value: data})
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"go-micro.dev/v6/registry"
|
||||
"go-micro.dev/v6/store"
|
||||
)
|
||||
|
||||
func TestStoreMemoryPersists(t *testing.T) {
|
||||
st := store.NewMemoryStore()
|
||||
m := NewMemory(st, "agent/x/history", 10)
|
||||
m.Add("user", "hello")
|
||||
m.Add("assistant", "hi there")
|
||||
|
||||
// A fresh memory over the same store/key restores the conversation.
|
||||
reloaded := NewMemory(st, "agent/x/history", 10)
|
||||
if got := len(reloaded.Messages()); got != 2 {
|
||||
t.Fatalf("restored %d messages, want 2", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInMemoryNotPersisted(t *testing.T) {
|
||||
m := NewInMemory(10)
|
||||
m.Add("user", "x")
|
||||
if got := len(m.Messages()); got != 1 {
|
||||
t.Fatalf("got %d messages, want 1", got)
|
||||
}
|
||||
if got := len(NewInMemory(10).Messages()); got != 0 {
|
||||
t.Errorf("a separate in-memory should be empty, got %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMemoryClearPersists(t *testing.T) {
|
||||
st := store.NewMemoryStore()
|
||||
m := NewMemory(st, "agent/y/history", 10)
|
||||
m.Add("user", "x")
|
||||
m.Clear()
|
||||
if got := len(m.Messages()); got != 0 {
|
||||
t.Errorf("after Clear got %d messages, want 0", got)
|
||||
}
|
||||
if got := len(NewMemory(st, "agent/y/history", 10).Messages()); got != 0 {
|
||||
t.Errorf("cleared state should persist, reload got %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithMemoryUsed(t *testing.T) {
|
||||
custom := NewInMemory(5)
|
||||
a := New(
|
||||
Name("z"),
|
||||
Provider("fake"),
|
||||
WithRegistry(registry.NewMemoryRegistry()),
|
||||
WithStore(store.NewMemoryStore()),
|
||||
WithMemory(custom),
|
||||
).(*agentImpl)
|
||||
a.setup()
|
||||
if a.mem != custom {
|
||||
t.Error("WithMemory should make the agent use the supplied memory")
|
||||
}
|
||||
}
|
||||
|
||||
// A custom tool is offered to the model and dispatched to its handler.
|
||||
func TestWithToolExposedAndDispatched(t *testing.T) {
|
||||
var got map[string]any
|
||||
a := newTestAgent(Name("calc-agent"),
|
||||
WithTool("calc", "adds two numbers",
|
||||
map[string]any{
|
||||
"a": map[string]any{"type": "number"},
|
||||
"b": map[string]any{"type": "number"},
|
||||
},
|
||||
func(ctx context.Context, input map[string]any) (string, error) {
|
||||
got = input
|
||||
return `{"sum":3}`, nil
|
||||
}))
|
||||
|
||||
tools, err := a.discoverTools()
|
||||
if err != nil {
|
||||
t.Fatalf("discoverTools: %v", err)
|
||||
}
|
||||
found := false
|
||||
for _, tl := range tools {
|
||||
if tl.Name == "calc" {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("custom tool 'calc' was not offered to the model")
|
||||
}
|
||||
|
||||
content := toolContent(a.toolHandler(), "calc", map[string]any{"a": 1.0, "b": 2.0})
|
||||
if got == nil {
|
||||
t.Fatal("custom tool handler was not called")
|
||||
}
|
||||
if !strings.Contains(content, "sum") {
|
||||
t.Errorf("custom tool result not returned: %q", content)
|
||||
}
|
||||
}
|
||||
|
||||
// A custom tool returning an error surfaces it to the model.
|
||||
func TestWithToolError(t *testing.T) {
|
||||
a := newTestAgent(Name("err-agent"),
|
||||
WithTool("boom", "always fails", nil,
|
||||
func(ctx context.Context, input map[string]any) (string, error) {
|
||||
return "", errors.New("kaboom")
|
||||
}))
|
||||
|
||||
content := toolContent(a.toolHandler(), "boom", nil)
|
||||
if !strings.Contains(content, "kaboom") {
|
||||
t.Errorf("tool error not surfaced: %q", content)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,249 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go-micro.dev/v6/ai"
|
||||
"go-micro.dev/v6/client"
|
||||
"go-micro.dev/v6/registry"
|
||||
"go-micro.dev/v6/store"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
// Option configures an Agent.
|
||||
type Option func(*Options)
|
||||
|
||||
// ApproveFunc decides whether an agent may execute a tool call before it
|
||||
// runs. Returning false blocks the call; the reason is shown to the
|
||||
// model so it can adapt. Use it for human-in-the-loop approval or policy
|
||||
// checks. It is called for actions (service tools and delegate), not for
|
||||
// the internal plan tool.
|
||||
type ApproveFunc func(tool string, input map[string]any) (approved bool, reason string)
|
||||
|
||||
// ToolFunc handles a custom tool call. Return the result as a string
|
||||
// (often JSON); return an error to report failure back to the model.
|
||||
type ToolFunc func(ctx context.Context, input map[string]any) (string, error)
|
||||
|
||||
// customTool is a developer-registered tool beyond the agent's services.
|
||||
type customTool struct {
|
||||
def ai.Tool
|
||||
handler ToolFunc
|
||||
}
|
||||
|
||||
// Options holds agent configuration.
|
||||
type Options struct {
|
||||
Name string
|
||||
Services []string
|
||||
Prompt string
|
||||
Provider string
|
||||
Model string
|
||||
APIKey string
|
||||
Registry registry.Registry
|
||||
Client client.Client
|
||||
Store store.Store
|
||||
HistoryLimit int
|
||||
|
||||
// ModelTimeout bounds each provider Generate call (0 disables).
|
||||
ModelTimeout time.Duration
|
||||
// ModelMaxAttempts bounds provider Generate attempts including the first
|
||||
// call. Default 1 — retries are opt-in (enable with ModelRetry). A Generate
|
||||
// runs the whole tool-execution turn, so auto-retrying it would re-run
|
||||
// already-executed, possibly side-effecting tool calls; keep it explicit.
|
||||
ModelMaxAttempts int
|
||||
// ModelRetryBackoff is the base delay between transient provider failures
|
||||
// (grows exponentially per attempt when retries are enabled).
|
||||
ModelRetryBackoff time.Duration
|
||||
|
||||
// Memory is the agent's conversation memory. Nil = the default
|
||||
// store-backed memory (durable across restarts).
|
||||
Memory Memory
|
||||
|
||||
// MaxSteps bounds the number of tool executions per Ask (0 =
|
||||
// unbounded). Once exceeded, further tool calls are refused and the
|
||||
// model is told to stop and summarize. A stopping condition.
|
||||
MaxSteps int
|
||||
// LoopLimit bounds how many times the agent may call the same tool
|
||||
// with the same arguments in one Ask before the call is refused as a
|
||||
// no-progress loop (0 = disabled). Catches the agent repeating an
|
||||
// identical action — which MaxSteps only bounds by total count.
|
||||
LoopLimit int
|
||||
// Approve gates each action before it runs. Nil = allow all.
|
||||
Approve ApproveFunc
|
||||
|
||||
// A2AAddress, if set, makes Run serve this agent over the A2A protocol
|
||||
// on that address directly (no separate gateway), e.g. ":4000".
|
||||
A2AAddress string
|
||||
|
||||
// TraceProvider enables OpenTelemetry spans for agent runs, model calls,
|
||||
// and tool calls. Nil disables instrumentation.
|
||||
TraceProvider trace.TracerProvider
|
||||
|
||||
// tools are developer-registered custom tools (see WithTool).
|
||||
tools []customTool
|
||||
// wrappers are developer-registered tool-execution wrappers
|
||||
// (see WrapTool), applied outside the built-in guardrails.
|
||||
wrappers []ai.ToolWrapper
|
||||
}
|
||||
|
||||
func newOptions(opts ...Option) Options {
|
||||
o := Options{
|
||||
Registry: registry.DefaultRegistry,
|
||||
Client: client.DefaultClient,
|
||||
Store: store.DefaultStore,
|
||||
HistoryLimit: 50,
|
||||
ModelTimeout: 30 * time.Second,
|
||||
ModelMaxAttempts: 1, // retries opt-in via ModelRetry (see field doc)
|
||||
ModelRetryBackoff: 100 * time.Millisecond,
|
||||
// On by default and lenient: identical repeated calls are a
|
||||
// no-progress loop, never useful. Set LoopLimit(0) to disable.
|
||||
LoopLimit: 3,
|
||||
}
|
||||
for _, opt := range opts {
|
||||
opt(&o)
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
// Name sets the agent name.
|
||||
func Name(n string) Option {
|
||||
return func(o *Options) { o.Name = n }
|
||||
}
|
||||
|
||||
// Services sets which services this agent manages.
|
||||
func Services(names ...string) Option {
|
||||
return func(o *Options) { o.Services = names }
|
||||
}
|
||||
|
||||
// Prompt sets the system prompt.
|
||||
func Prompt(p string) Option {
|
||||
return func(o *Options) { o.Prompt = p }
|
||||
}
|
||||
|
||||
// Provider sets the LLM provider.
|
||||
func Provider(p string) Option {
|
||||
return func(o *Options) { o.Provider = p }
|
||||
}
|
||||
|
||||
// Model sets the LLM model name.
|
||||
func Model(m string) Option {
|
||||
return func(o *Options) { o.Model = m }
|
||||
}
|
||||
|
||||
// APIKey sets the API key for the LLM provider.
|
||||
func APIKey(k string) Option {
|
||||
return func(o *Options) { o.APIKey = k }
|
||||
}
|
||||
|
||||
// WithRegistry sets the service registry.
|
||||
func WithRegistry(r registry.Registry) Option {
|
||||
return func(o *Options) { o.Registry = r }
|
||||
}
|
||||
|
||||
// WithClient sets the RPC client.
|
||||
func WithClient(c client.Client) Option {
|
||||
return func(o *Options) { o.Client = c }
|
||||
}
|
||||
|
||||
// WithStore sets the store for agent memory.
|
||||
func WithStore(s store.Store) Option {
|
||||
return func(o *Options) { o.Store = s }
|
||||
}
|
||||
|
||||
// HistoryLimit sets the max conversation messages to retain.
|
||||
func HistoryLimit(n int) Option {
|
||||
return func(o *Options) { o.HistoryLimit = n }
|
||||
}
|
||||
|
||||
// MaxSteps bounds tool executions per Ask (0 = unbounded). A stopping
|
||||
// condition: beyond the limit, tool calls are refused and the model is
|
||||
// told to stop and summarize.
|
||||
func MaxSteps(n int) Option {
|
||||
return func(o *Options) { o.MaxSteps = n }
|
||||
}
|
||||
|
||||
// ApproveTool sets a human-in-the-loop / policy hook called before each
|
||||
// action (service tools and delegate). Returning false blocks the call.
|
||||
func ApproveTool(fn ApproveFunc) Option {
|
||||
return func(o *Options) { o.Approve = fn }
|
||||
}
|
||||
|
||||
// LoopLimit sets how many times the agent may repeat the same tool call
|
||||
// (same name and arguments) in one Ask before it is refused as a
|
||||
// no-progress loop. 0 disables loop detection.
|
||||
func LoopLimit(n int) Option {
|
||||
return func(o *Options) { o.LoopLimit = n }
|
||||
}
|
||||
|
||||
// ModelCallTimeout sets the timeout for each provider Generate call.
|
||||
func ModelCallTimeout(d time.Duration) Option {
|
||||
return func(o *Options) { o.ModelTimeout = d }
|
||||
}
|
||||
|
||||
// ModelRetry sets the provider retry budget and backoff for transient failures.
|
||||
func ModelRetry(maxAttempts int, backoff time.Duration) Option {
|
||||
return func(o *Options) {
|
||||
o.ModelMaxAttempts = maxAttempts
|
||||
o.ModelRetryBackoff = backoff
|
||||
}
|
||||
}
|
||||
|
||||
// WithA2A makes Run serve the agent over the A2A protocol on addr (e.g.
|
||||
// ":4000"), so other agents can reach it directly by URL without a
|
||||
// separate gateway. The agent stays a normal go-micro service as well;
|
||||
// this adds a second, A2A-native HTTP endpoint that calls it in-process.
|
||||
func WithA2A(addr string) Option {
|
||||
return func(o *Options) { o.A2AAddress = addr }
|
||||
}
|
||||
|
||||
// WithMemory sets the agent's conversation memory. The default is
|
||||
// store-backed memory keyed by agent name; supply your own to use an
|
||||
// in-process, database, or semantic store.
|
||||
func WithMemory(m Memory) Option {
|
||||
return func(o *Options) { o.Memory = m }
|
||||
}
|
||||
|
||||
// WrapTool registers a tool-execution wrapper, the tool-side analog of
|
||||
// a client/server middleware wrapper. Each wrapper takes the next handler
|
||||
// and returns a new one; code before the next(...) call runs before the
|
||||
// tool executes, code after runs after. Use it for logging, metrics,
|
||||
// retries, or custom policy. Wrappers run outside the built-in guardrails
|
||||
// (MaxSteps, LoopLimit, ApproveTool), so they observe every call and its
|
||||
// result, including refusals. Multiple wrappers compose outermost-first.
|
||||
//
|
||||
// micro.NewAgent("worker", micro.AgentWrapTool(
|
||||
// func(next ai.ToolHandler) ai.ToolHandler {
|
||||
// return func(ctx context.Context, call ai.ToolCall) ai.ToolResult {
|
||||
// res := next(ctx, call)
|
||||
// log.Printf("id=%s tool=%s", call.ID, call.Name)
|
||||
// return res
|
||||
// }
|
||||
// }))
|
||||
func WrapTool(w ...ai.ToolWrapper) Option {
|
||||
return func(o *Options) {
|
||||
o.wrappers = append(o.wrappers, w...)
|
||||
}
|
||||
}
|
||||
|
||||
// WithTool registers a custom tool the agent can call, beyond the
|
||||
// services it discovers — a local function, an external API, anything.
|
||||
// properties is the JSON-schema map for the tool's parameters.
|
||||
func WithTool(name, description string, properties map[string]any, handler ToolFunc) Option {
|
||||
return func(o *Options) {
|
||||
o.tools = append(o.tools, customTool{
|
||||
def: ai.Tool{
|
||||
Name: name,
|
||||
OriginalName: name,
|
||||
Description: description,
|
||||
Properties: properties,
|
||||
},
|
||||
handler: handler,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TraceProvider enables OpenTelemetry tracing for agent runs. When nil,
|
||||
// agent tracing and run timeline recording are disabled.
|
||||
func TraceProvider(tp trace.TracerProvider) Option {
|
||||
return func(o *Options) { o.TraceProvider = tp }
|
||||
}
|
||||
+295
@@ -0,0 +1,295 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go-micro.dev/v6/ai"
|
||||
"go-micro.dev/v6/store"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
const agentInstrumentationName = "go-micro.dev/v6/agent"
|
||||
|
||||
const (
|
||||
spanNameRun = "agent.run"
|
||||
spanNameModelCall = "agent.model.call"
|
||||
spanNameToolCall = "agent.tool.call"
|
||||
|
||||
AttrRunID = "agent.run.id"
|
||||
AttrParentRunID = "agent.run.parent_id"
|
||||
AttrAgentName = "agent.name"
|
||||
AttrProvider = "agent.model.provider"
|
||||
AttrModel = "agent.model.name"
|
||||
AttrLatencyMS = "agent.latency_ms"
|
||||
AttrInputTokens = "agent.tokens.input"
|
||||
AttrOutputTokens = "agent.tokens.output"
|
||||
AttrTotalTokens = "agent.tokens.total"
|
||||
AttrToolName = "agent.tool.name"
|
||||
AttrDelegate = "agent.delegate"
|
||||
AttrGuardrailBlock = "agent.guardrail.block"
|
||||
AttrRefusal = "agent.refusal"
|
||||
)
|
||||
|
||||
type RunEvent struct {
|
||||
Time time.Time `json:"time"`
|
||||
RunID string `json:"run_id"`
|
||||
ParentID string `json:"parent_id,omitempty"`
|
||||
TraceID string `json:"trace_id,omitempty"`
|
||||
SpanID string `json:"span_id,omitempty"`
|
||||
Agent string `json:"agent"`
|
||||
Kind string `json:"kind"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Provider string `json:"provider,omitempty"`
|
||||
Model string `json:"model,omitempty"`
|
||||
LatencyMS int64 `json:"latency_ms,omitempty"`
|
||||
Tokens Usage `json:"tokens,omitempty"`
|
||||
Refused string `json:"refused,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type Usage = ai.Usage
|
||||
|
||||
// RunSummary is a compact index entry for a recorded agent run.
|
||||
type RunSummary struct {
|
||||
RunID string `json:"run_id"`
|
||||
Agent string `json:"agent"`
|
||||
ParentID string `json:"parent_id,omitempty"`
|
||||
TraceID string `json:"trace_id,omitempty"`
|
||||
SpanID string `json:"span_id,omitempty"`
|
||||
StartedAt time.Time `json:"started_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Events int `json:"events"`
|
||||
LastKind string `json:"last_kind,omitempty"`
|
||||
LastError string `json:"last_error,omitempty"`
|
||||
}
|
||||
|
||||
func (a *agentImpl) tracer() trace.Tracer {
|
||||
return a.opts.TraceProvider.Tracer(agentInstrumentationName)
|
||||
}
|
||||
|
||||
func (a *agentImpl) startRun(ctx context.Context, message string) (context.Context, func(error)) {
|
||||
if a.opts.TraceProvider == nil {
|
||||
return ctx, func(error) {}
|
||||
}
|
||||
info, _ := ai.RunInfoFrom(ctx)
|
||||
ctx, span := a.tracer().Start(ctx, spanNameRun, trace.WithSpanKind(trace.SpanKindInternal), trace.WithAttributes(
|
||||
attribute.String(AttrRunID, info.RunID), attribute.String(AttrParentRunID, info.ParentID), attribute.String(AttrAgentName, info.Agent)))
|
||||
start := time.Now()
|
||||
a.recordSpanEvent(span, RunEvent{Time: start, RunID: info.RunID, ParentID: info.ParentID, Agent: info.Agent, Kind: "run", Name: message})
|
||||
return ctx, func(err error) {
|
||||
latency := time.Since(start).Milliseconds()
|
||||
span.SetAttributes(attribute.Int64(AttrLatencyMS, latency))
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
a.recordSpanEvent(span, RunEvent{Time: time.Now(), RunID: info.RunID, ParentID: info.ParentID, Agent: info.Agent, Kind: "error", LatencyMS: latency, Error: err.Error()})
|
||||
} else {
|
||||
span.SetStatus(codes.Ok, "")
|
||||
a.recordSpanEvent(span, RunEvent{Time: time.Now(), RunID: info.RunID, ParentID: info.ParentID, Agent: info.Agent, Kind: "done", LatencyMS: latency})
|
||||
}
|
||||
span.End()
|
||||
}
|
||||
}
|
||||
|
||||
type tracedModel struct {
|
||||
ai.Model
|
||||
a *agentImpl
|
||||
}
|
||||
|
||||
func (a *agentImpl) tracedModel(m ai.Model) ai.Model { return &tracedModel{Model: m, a: a} }
|
||||
func (m *tracedModel) Generate(ctx context.Context, req *ai.Request, opts ...ai.GenerateOption) (*ai.Response, error) {
|
||||
if m.a.opts.TraceProvider == nil {
|
||||
return m.Model.Generate(ctx, req, opts...)
|
||||
}
|
||||
info, _ := ai.RunInfoFrom(ctx)
|
||||
provider := m.String()
|
||||
model := m.Options().Model
|
||||
ctx, span := m.a.tracer().Start(ctx, spanNameModelCall, trace.WithAttributes(attribute.String(AttrProvider, provider), attribute.String(AttrModel, model)))
|
||||
start := time.Now()
|
||||
resp, err := m.Model.Generate(ctx, req, opts...)
|
||||
dur := time.Since(start).Milliseconds()
|
||||
attrs := []attribute.KeyValue{attribute.Int64(AttrLatencyMS, dur)}
|
||||
usage := ai.Usage{}
|
||||
if resp != nil {
|
||||
usage = resp.Usage
|
||||
attrs = appendUsage(attrs, usage)
|
||||
}
|
||||
span.SetAttributes(attrs...)
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
} else {
|
||||
span.SetStatus(codes.Ok, "")
|
||||
}
|
||||
span.End()
|
||||
e := RunEvent{Time: time.Now(), RunID: info.RunID, ParentID: info.ParentID, Agent: info.Agent, Kind: "model", Provider: provider, Model: model, LatencyMS: dur, Tokens: usage}
|
||||
if err != nil {
|
||||
e.Error = err.Error()
|
||||
}
|
||||
m.a.recordSpanEvent(span, e)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func appendUsage(attrs []attribute.KeyValue, u ai.Usage) []attribute.KeyValue {
|
||||
if u.InputTokens > 0 {
|
||||
attrs = append(attrs, attribute.Int(AttrInputTokens, u.InputTokens))
|
||||
}
|
||||
if u.OutputTokens > 0 {
|
||||
attrs = append(attrs, attribute.Int(AttrOutputTokens, u.OutputTokens))
|
||||
}
|
||||
if u.TotalTokens > 0 {
|
||||
attrs = append(attrs, attribute.Int(AttrTotalTokens, u.TotalTokens))
|
||||
}
|
||||
return attrs
|
||||
}
|
||||
|
||||
func (a *agentImpl) traceTool(next ai.ToolHandler) ai.ToolHandler {
|
||||
if a.opts.TraceProvider == nil {
|
||||
return next
|
||||
}
|
||||
return func(ctx context.Context, call ai.ToolCall) ai.ToolResult {
|
||||
info, _ := ai.RunInfoFrom(ctx)
|
||||
ctx, span := a.tracer().Start(ctx, spanNameToolCall, trace.WithAttributes(attribute.String(AttrToolName, call.Name), attribute.Bool(AttrDelegate, call.Name == toolDelegate)))
|
||||
start := time.Now()
|
||||
res := next(ctx, call)
|
||||
dur := time.Since(start).Milliseconds()
|
||||
attrs := []attribute.KeyValue{attribute.Int64(AttrLatencyMS, dur)}
|
||||
if res.Refused != "" {
|
||||
attrs = append(attrs, attribute.Bool(AttrGuardrailBlock, true), attribute.String(AttrRefusal, res.Refused))
|
||||
}
|
||||
span.SetAttributes(attrs...)
|
||||
resErr := resultError(res)
|
||||
if res.Refused != "" {
|
||||
span.SetStatus(codes.Error, res.Refused)
|
||||
} else if resErr != "" {
|
||||
span.SetStatus(codes.Error, resErr)
|
||||
} else {
|
||||
span.SetStatus(codes.Ok, "")
|
||||
}
|
||||
span.End()
|
||||
a.recordSpanEvent(span, RunEvent{Time: time.Now(), RunID: info.RunID, ParentID: info.ParentID, Agent: info.Agent, Kind: "tool", Name: call.Name, LatencyMS: dur, Refused: res.Refused, Error: resErr})
|
||||
return res
|
||||
}
|
||||
}
|
||||
|
||||
func resultError(res ai.ToolResult) string {
|
||||
if m, ok := res.Value.(map[string]string); ok {
|
||||
return m["error"]
|
||||
}
|
||||
if m, ok := res.Value.(map[string]any); ok {
|
||||
if err, _ := m["error"].(string); err != "" {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (a *agentImpl) recordSpanEvent(span trace.Span, e RunEvent) {
|
||||
if sc := span.SpanContext(); sc.IsValid() {
|
||||
e.TraceID = sc.TraceID().String()
|
||||
e.SpanID = sc.SpanID().String()
|
||||
}
|
||||
a.recordRunEvent(e)
|
||||
}
|
||||
|
||||
func (a *agentImpl) recordRunEvent(e RunEvent) {
|
||||
if a.opts.TraceProvider == nil || e.RunID == "" {
|
||||
return
|
||||
}
|
||||
b, _ := json.Marshal(e)
|
||||
key := fmt.Sprintf("runs/%s/%020d-%s", e.RunID, e.Time.UnixNano(), e.Kind)
|
||||
_ = a.stateStore().Write(&store.Record{Key: key, Value: b})
|
||||
}
|
||||
|
||||
// ListRunSummaries returns a deterministic summary of recorded runs for agentName.
|
||||
func ListRunSummaries(s store.Store, agentName string) ([]RunSummary, error) {
|
||||
st := store.Scope(s, "agent", agentName)
|
||||
keys, err := st.List(store.ListPrefix("runs/"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
runs := map[string]bool{}
|
||||
for _, k := range keys {
|
||||
parts := strings.Split(k, "/")
|
||||
if len(parts) >= 2 && parts[1] != "" {
|
||||
runs[parts[1]] = true
|
||||
}
|
||||
}
|
||||
ids := make([]string, 0, len(runs))
|
||||
for id := range runs {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
sort.Strings(ids)
|
||||
|
||||
summaries := make([]RunSummary, 0, len(ids))
|
||||
for _, id := range ids {
|
||||
events, err := LoadRunEvents(s, agentName, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(events) == 0 {
|
||||
continue
|
||||
}
|
||||
first := events[0]
|
||||
last := events[len(events)-1]
|
||||
summary := RunSummary{
|
||||
RunID: id,
|
||||
Agent: first.Agent,
|
||||
ParentID: first.ParentID,
|
||||
TraceID: first.TraceID,
|
||||
SpanID: first.SpanID,
|
||||
StartedAt: first.Time,
|
||||
UpdatedAt: last.Time,
|
||||
Events: len(events),
|
||||
LastKind: last.Kind,
|
||||
LastError: last.Error,
|
||||
}
|
||||
for _, e := range events {
|
||||
if e.Agent != "" {
|
||||
summary.Agent = e.Agent
|
||||
}
|
||||
if e.ParentID != "" {
|
||||
summary.ParentID = e.ParentID
|
||||
}
|
||||
if e.TraceID != "" {
|
||||
summary.TraceID = e.TraceID
|
||||
}
|
||||
if e.SpanID != "" {
|
||||
summary.SpanID = e.SpanID
|
||||
}
|
||||
if e.Error != "" {
|
||||
summary.LastError = e.Error
|
||||
}
|
||||
}
|
||||
summaries = append(summaries, summary)
|
||||
}
|
||||
return summaries, nil
|
||||
}
|
||||
|
||||
func LoadRunEvents(s store.Store, agentName, runID string) ([]RunEvent, error) {
|
||||
st := store.Scope(s, "agent", agentName)
|
||||
keys, err := st.List(store.ListPrefix("runs/" + runID + "/"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sort.Strings(keys)
|
||||
events := make([]RunEvent, 0, len(keys))
|
||||
for _, k := range keys {
|
||||
recs, err := st.Read(k)
|
||||
if err != nil || len(recs) == 0 {
|
||||
continue
|
||||
}
|
||||
var e RunEvent
|
||||
if json.Unmarshal(recs[0].Value, &e) == nil {
|
||||
events = append(events, e)
|
||||
}
|
||||
}
|
||||
return events, nil
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go-micro.dev/v6/ai"
|
||||
"go-micro.dev/v6/store"
|
||||
"go.opentelemetry.io/otel/sdk/trace"
|
||||
"go.opentelemetry.io/otel/sdk/trace/tracetest"
|
||||
)
|
||||
|
||||
type otelTestModel struct{ opts ai.Options }
|
||||
|
||||
func (m *otelTestModel) Init(opts ...ai.Option) error {
|
||||
for _, o := range opts {
|
||||
o(&m.opts)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (m *otelTestModel) Options() ai.Options { return m.opts }
|
||||
func (m *otelTestModel) String() string { return "oteltest" }
|
||||
func (m *otelTestModel) Stream(context.Context, *ai.Request, ...ai.GenerateOption) (ai.Stream, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (m *otelTestModel) Generate(ctx context.Context, req *ai.Request, opts ...ai.GenerateOption) (*ai.Response, error) {
|
||||
if m.opts.ToolHandler != nil {
|
||||
_ = m.opts.ToolHandler(ctx, ai.ToolCall{ID: "call-1", Name: "probe", Input: map[string]any{"ok": true}})
|
||||
}
|
||||
return &ai.Response{Reply: "done", Usage: ai.Usage{InputTokens: 2, OutputTokens: 3, TotalTokens: 5}}, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
ai.Register("oteltest", func(opts ...ai.Option) ai.Model { return &otelTestModel{opts: ai.NewOptions(opts...)} })
|
||||
}
|
||||
|
||||
func TestAgentOpenTelemetrySpans(t *testing.T) {
|
||||
exp := tracetest.NewInMemoryExporter()
|
||||
tp := trace.NewTracerProvider(trace.WithSyncer(exp))
|
||||
st := store.NewMemoryStore()
|
||||
a := New(Name("runner"), Provider("oteltest"), Model("unit-model"), WithStore(st), TraceProvider(tp), WithTool("probe", "probe", nil, func(context.Context, map[string]any) (string, error) { return "ok", nil }))
|
||||
if _, err := a.Ask(context.Background(), "hello"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
spans := exp.GetSpans().Snapshots()
|
||||
want := map[string]bool{spanNameRun: false, spanNameModelCall: false, spanNameToolCall: false}
|
||||
for _, s := range spans {
|
||||
if _, ok := want[s.Name()]; ok {
|
||||
want[s.Name()] = true
|
||||
}
|
||||
}
|
||||
for name, seen := range want {
|
||||
if !seen {
|
||||
t.Fatalf("span %s not emitted; got %d spans", name, len(spans))
|
||||
}
|
||||
}
|
||||
keys, err := store.Scope(st, "agent", "runner").List(store.ListPrefix("runs/"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(keys) == 0 {
|
||||
t.Fatal("expected run events to be recorded")
|
||||
}
|
||||
summaries, err := ListRunSummaries(st, "runner")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(summaries) != 1 {
|
||||
t.Fatalf("got %d summaries, want 1", len(summaries))
|
||||
}
|
||||
if summaries[0].LastKind != "done" {
|
||||
t.Fatalf("LastKind = %q, want done", summaries[0].LastKind)
|
||||
}
|
||||
if summaries[0].TraceID == "" || summaries[0].SpanID == "" {
|
||||
t.Fatalf("summary missing trace correlation: %#v", summaries[0])
|
||||
}
|
||||
events, err := LoadRunEvents(st, "runner", summaries[0].RunID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(events) == 0 || events[0].TraceID == "" || events[0].SpanID == "" {
|
||||
t.Fatalf("events missing trace correlation: %#v", events)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgentOpenTelemetryNoopWhenUnconfigured(t *testing.T) {
|
||||
st := store.NewMemoryStore()
|
||||
a := New(Name("runner-noop"), Provider("oteltest"), WithStore(st), WithTool("probe", "probe", nil, func(context.Context, map[string]any) (string, error) { return "ok", nil }))
|
||||
if _, err := a.Ask(context.Background(), "hello"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
keys, err := store.Scope(st, "agent", "runner-noop").List(store.ListPrefix("runs/"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(keys) != 0 {
|
||||
t.Fatalf("expected no run timeline without TraceProvider, got %v", keys)
|
||||
}
|
||||
if _, ok := a.(*agentImpl).model.(*tracedModel); ok {
|
||||
t.Fatal("model should not be wrapped when TraceProvider is nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadRunEventsSortsTimelineKeys(t *testing.T) {
|
||||
st := store.NewMemoryStore()
|
||||
scoped := store.Scope(st, "agent", "runner")
|
||||
runID := "run-1"
|
||||
events := []RunEvent{
|
||||
{Time: time.Unix(0, 3), RunID: runID, Agent: "runner", Kind: "tool", Name: "third"},
|
||||
{Time: time.Unix(0, 1), RunID: runID, Agent: "runner", Kind: "run", Name: "first"},
|
||||
{Time: time.Unix(0, 2), RunID: runID, Agent: "runner", Kind: "model", Name: "second"},
|
||||
}
|
||||
for _, e := range events {
|
||||
b, err := json.Marshal(e)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
key := "runs/" + runID + "/" + e.Time.Format("20060102150405.000000000") + "-" + e.Kind
|
||||
if err := scoped.Write(&store.Record{Key: key, Value: b}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
got, err := LoadRunEvents(st, "runner", runID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != 3 {
|
||||
t.Fatalf("got %d events, want 3", len(got))
|
||||
}
|
||||
for i, want := range []string{"first", "second", "third"} {
|
||||
if got[i].Name != want {
|
||||
t.Fatalf("event %d = %q, want %q (timeline: %#v)", i, got[i].Name, want, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestListRunSummaries(t *testing.T) {
|
||||
st := store.NewMemoryStore()
|
||||
scoped := store.Scope(st, "agent", "runner")
|
||||
events := []RunEvent{
|
||||
{Time: time.Unix(0, 1), RunID: "run-a", Agent: "runner", TraceID: "trace-a", SpanID: "span-a", Kind: "run", Name: "first"},
|
||||
{Time: time.Unix(0, 2), RunID: "run-a", Agent: "runner", Kind: "tool", Name: "probe"},
|
||||
{Time: time.Unix(0, 3), RunID: "run-b", Agent: "runner", ParentID: "parent", Kind: "run", Name: "second"},
|
||||
{Time: time.Unix(0, 4), RunID: "run-b", Agent: "runner", ParentID: "parent", Kind: "error", Error: "boom"},
|
||||
}
|
||||
for _, e := range events {
|
||||
b, err := json.Marshal(e)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
key := "runs/" + e.RunID + "/" + e.Time.Format("20060102150405.000000000") + "-" + e.Kind
|
||||
if err := scoped.Write(&store.Record{Key: key, Value: b}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
got, err := ListRunSummaries(st, "runner")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != 2 {
|
||||
t.Fatalf("got %d summaries, want 2: %#v", len(got), got)
|
||||
}
|
||||
if got[0].RunID != "run-a" || got[0].TraceID != "trace-a" || got[0].SpanID != "span-a" || got[0].Events != 2 || got[0].LastKind != "tool" || !got[0].UpdatedAt.Equal(time.Unix(0, 2)) {
|
||||
t.Fatalf("unexpected run-a summary: %#v", got[0])
|
||||
}
|
||||
if got[1].RunID != "run-b" || got[1].ParentID != "parent" || got[1].Events != 2 || got[1].LastKind != "error" || got[1].LastError != "boom" {
|
||||
t.Fatalf("unexpected run-b summary: %#v", got[1])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,267 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.36.11
|
||||
// protoc v3.21.12
|
||||
// source: proto/agent.proto
|
||||
|
||||
package agent
|
||||
|
||||
import (
|
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||
reflect "reflect"
|
||||
sync "sync"
|
||||
unsafe "unsafe"
|
||||
)
|
||||
|
||||
const (
|
||||
// Verify that this generated code is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||
)
|
||||
|
||||
type ChatRequest struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *ChatRequest) Reset() {
|
||||
*x = ChatRequest{}
|
||||
mi := &file_proto_agent_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *ChatRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*ChatRequest) ProtoMessage() {}
|
||||
|
||||
func (x *ChatRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_proto_agent_proto_msgTypes[0]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use ChatRequest.ProtoReflect.Descriptor instead.
|
||||
func (*ChatRequest) Descriptor() ([]byte, []int) {
|
||||
return file_proto_agent_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
func (x *ChatRequest) GetMessage() string {
|
||||
if x != nil {
|
||||
return x.Message
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type ChatResponse struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Reply string `protobuf:"bytes,1,opt,name=reply,proto3" json:"reply,omitempty"`
|
||||
Agent string `protobuf:"bytes,2,opt,name=agent,proto3" json:"agent,omitempty"`
|
||||
ToolCalls []*ToolCall `protobuf:"bytes,3,rep,name=tool_calls,json=toolCalls,proto3" json:"tool_calls,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *ChatResponse) Reset() {
|
||||
*x = ChatResponse{}
|
||||
mi := &file_proto_agent_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *ChatResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*ChatResponse) ProtoMessage() {}
|
||||
|
||||
func (x *ChatResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_proto_agent_proto_msgTypes[1]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use ChatResponse.ProtoReflect.Descriptor instead.
|
||||
func (*ChatResponse) Descriptor() ([]byte, []int) {
|
||||
return file_proto_agent_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
func (x *ChatResponse) GetReply() string {
|
||||
if x != nil {
|
||||
return x.Reply
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *ChatResponse) GetAgent() string {
|
||||
if x != nil {
|
||||
return x.Agent
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *ChatResponse) GetToolCalls() []*ToolCall {
|
||||
if x != nil {
|
||||
return x.ToolCalls
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type ToolCall struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||
Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
|
||||
Input string `protobuf:"bytes,3,opt,name=input,proto3" json:"input,omitempty"`
|
||||
Result string `protobuf:"bytes,4,opt,name=result,proto3" json:"result,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *ToolCall) Reset() {
|
||||
*x = ToolCall{}
|
||||
mi := &file_proto_agent_proto_msgTypes[2]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *ToolCall) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*ToolCall) ProtoMessage() {}
|
||||
|
||||
func (x *ToolCall) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_proto_agent_proto_msgTypes[2]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use ToolCall.ProtoReflect.Descriptor instead.
|
||||
func (*ToolCall) Descriptor() ([]byte, []int) {
|
||||
return file_proto_agent_proto_rawDescGZIP(), []int{2}
|
||||
}
|
||||
|
||||
func (x *ToolCall) GetId() string {
|
||||
if x != nil {
|
||||
return x.Id
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *ToolCall) GetName() string {
|
||||
if x != nil {
|
||||
return x.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *ToolCall) GetInput() string {
|
||||
if x != nil {
|
||||
return x.Input
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *ToolCall) GetResult() string {
|
||||
if x != nil {
|
||||
return x.Result
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
var File_proto_agent_proto protoreflect.FileDescriptor
|
||||
|
||||
const file_proto_agent_proto_rawDesc = "" +
|
||||
"\n" +
|
||||
"\x11proto/agent.proto\x12\x05agent\"'\n" +
|
||||
"\vChatRequest\x12\x18\n" +
|
||||
"\amessage\x18\x01 \x01(\tR\amessage\"j\n" +
|
||||
"\fChatResponse\x12\x14\n" +
|
||||
"\x05reply\x18\x01 \x01(\tR\x05reply\x12\x14\n" +
|
||||
"\x05agent\x18\x02 \x01(\tR\x05agent\x12.\n" +
|
||||
"\n" +
|
||||
"tool_calls\x18\x03 \x03(\v2\x0f.agent.ToolCallR\ttoolCalls\"\\\n" +
|
||||
"\bToolCall\x12\x0e\n" +
|
||||
"\x02id\x18\x01 \x01(\tR\x02id\x12\x12\n" +
|
||||
"\x04name\x18\x02 \x01(\tR\x04name\x12\x14\n" +
|
||||
"\x05input\x18\x03 \x01(\tR\x05input\x12\x16\n" +
|
||||
"\x06result\x18\x04 \x01(\tR\x06result2:\n" +
|
||||
"\x05Agent\x121\n" +
|
||||
"\x04Chat\x12\x12.agent.ChatRequest\x1a\x13.agent.ChatResponse\"\x00B\x0fZ\r./proto;agentb\x06proto3"
|
||||
|
||||
var (
|
||||
file_proto_agent_proto_rawDescOnce sync.Once
|
||||
file_proto_agent_proto_rawDescData []byte
|
||||
)
|
||||
|
||||
func file_proto_agent_proto_rawDescGZIP() []byte {
|
||||
file_proto_agent_proto_rawDescOnce.Do(func() {
|
||||
file_proto_agent_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_proto_agent_proto_rawDesc), len(file_proto_agent_proto_rawDesc)))
|
||||
})
|
||||
return file_proto_agent_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_proto_agent_proto_msgTypes = make([]protoimpl.MessageInfo, 3)
|
||||
var file_proto_agent_proto_goTypes = []any{
|
||||
(*ChatRequest)(nil), // 0: agent.ChatRequest
|
||||
(*ChatResponse)(nil), // 1: agent.ChatResponse
|
||||
(*ToolCall)(nil), // 2: agent.ToolCall
|
||||
}
|
||||
var file_proto_agent_proto_depIdxs = []int32{
|
||||
2, // 0: agent.ChatResponse.tool_calls:type_name -> agent.ToolCall
|
||||
0, // 1: agent.Agent.Chat:input_type -> agent.ChatRequest
|
||||
1, // 2: agent.Agent.Chat:output_type -> agent.ChatResponse
|
||||
2, // [2:3] is the sub-list for method output_type
|
||||
1, // [1:2] is the sub-list for method input_type
|
||||
1, // [1:1] is the sub-list for extension type_name
|
||||
1, // [1:1] is the sub-list for extension extendee
|
||||
0, // [0:1] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_proto_agent_proto_init() }
|
||||
func file_proto_agent_proto_init() {
|
||||
if File_proto_agent_proto != nil {
|
||||
return
|
||||
}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: unsafe.Slice(unsafe.StringData(file_proto_agent_proto_rawDesc), len(file_proto_agent_proto_rawDesc)),
|
||||
NumEnums: 0,
|
||||
NumMessages: 3,
|
||||
NumExtensions: 0,
|
||||
NumServices: 1,
|
||||
},
|
||||
GoTypes: file_proto_agent_proto_goTypes,
|
||||
DependencyIndexes: file_proto_agent_proto_depIdxs,
|
||||
MessageInfos: file_proto_agent_proto_msgTypes,
|
||||
}.Build()
|
||||
File_proto_agent_proto = out.File
|
||||
file_proto_agent_proto_goTypes = nil
|
||||
file_proto_agent_proto_depIdxs = nil
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
// Code generated by protoc-gen-micro. DO NOT EDIT.
|
||||
// source: proto/agent.proto
|
||||
|
||||
package agent
|
||||
|
||||
import (
|
||||
fmt "fmt"
|
||||
proto "google.golang.org/protobuf/proto"
|
||||
math "math"
|
||||
)
|
||||
|
||||
import (
|
||||
context "context"
|
||||
client "go-micro.dev/v6/client"
|
||||
server "go-micro.dev/v6/server"
|
||||
)
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ = proto.Marshal
|
||||
var _ = fmt.Errorf
|
||||
var _ = math.Inf
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ context.Context
|
||||
var _ client.Option
|
||||
var _ server.Option
|
||||
|
||||
// Client API for Agent service
|
||||
|
||||
type AgentService interface {
|
||||
Chat(ctx context.Context, in *ChatRequest, opts ...client.CallOption) (*ChatResponse, error)
|
||||
}
|
||||
|
||||
type agentService struct {
|
||||
c client.Client
|
||||
name string
|
||||
}
|
||||
|
||||
func NewAgentService(name string, c client.Client) AgentService {
|
||||
return &agentService{
|
||||
c: c,
|
||||
name: name,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *agentService) Chat(ctx context.Context, in *ChatRequest, opts ...client.CallOption) (*ChatResponse, error) {
|
||||
req := c.c.NewRequest(c.name, "Agent.Chat", in)
|
||||
out := new(ChatResponse)
|
||||
err := c.c.Call(ctx, req, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Server API for Agent service
|
||||
|
||||
type AgentHandler interface {
|
||||
Chat(context.Context, *ChatRequest, *ChatResponse) error
|
||||
}
|
||||
|
||||
func RegisterAgentHandler(s server.Server, hdlr AgentHandler, opts ...server.HandlerOption) error {
|
||||
type agent interface {
|
||||
Chat(ctx context.Context, in *ChatRequest, out *ChatResponse) error
|
||||
}
|
||||
type Agent struct {
|
||||
agent
|
||||
}
|
||||
h := &agentHandler{hdlr}
|
||||
return s.Handle(s.NewHandler(&Agent{h}, opts...))
|
||||
}
|
||||
|
||||
type agentHandler struct {
|
||||
AgentHandler
|
||||
}
|
||||
|
||||
func (h *agentHandler) Chat(ctx context.Context, in *ChatRequest, out *ChatResponse) error {
|
||||
return h.AgentHandler.Chat(ctx, in, out)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package agent;
|
||||
|
||||
option go_package = "./proto;agent";
|
||||
|
||||
// Agent is the RPC interface for an AI agent.
|
||||
service Agent {
|
||||
rpc Chat(ChatRequest) returns (ChatResponse) {}
|
||||
}
|
||||
|
||||
message ChatRequest {
|
||||
string message = 1;
|
||||
}
|
||||
|
||||
message ChatResponse {
|
||||
string reply = 1;
|
||||
string agent = 2;
|
||||
repeated ToolCall tool_calls = 3;
|
||||
}
|
||||
|
||||
message ToolCall {
|
||||
string id = 1;
|
||||
string name = 2;
|
||||
string input = 3;
|
||||
string result = 4;
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go-micro.dev/v6/ai"
|
||||
)
|
||||
|
||||
func TestAskCancellationAbortsPromptly(t *testing.T) {
|
||||
fakeGen = func(ctx context.Context, opts ai.Options, req *ai.Request) (*ai.Response, error) {
|
||||
<-ctx.Done()
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
defer func() { fakeGen = nil }()
|
||||
|
||||
a := newTestAgent(Name("cancel"), ModelCallTimeout(time.Second), ModelRetry(3, time.Millisecond))
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
|
||||
start := time.Now()
|
||||
_, err := a.Ask(ctx, "stop")
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("Ask error = %v, want context canceled", err)
|
||||
}
|
||||
if elapsed := time.Since(start); elapsed > 100*time.Millisecond {
|
||||
t.Fatalf("Ask took %s after cancellation, want prompt abort", elapsed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAskRetriesTransientErrorsThenSucceeds(t *testing.T) {
|
||||
attempts := 0
|
||||
fakeGen = func(ctx context.Context, opts ai.Options, req *ai.Request) (*ai.Response, error) {
|
||||
attempts++
|
||||
if attempts < 3 {
|
||||
return nil, context.DeadlineExceeded
|
||||
}
|
||||
return &ai.Response{Reply: "ok"}, nil
|
||||
}
|
||||
defer func() { fakeGen = nil }()
|
||||
|
||||
a := newTestAgent(Name("retry-success"), ModelRetry(3, time.Millisecond))
|
||||
resp, err := a.Ask(context.Background(), "hello")
|
||||
if err != nil {
|
||||
t.Fatalf("Ask returned error: %v", err)
|
||||
}
|
||||
if resp.Reply != "ok" {
|
||||
t.Fatalf("reply = %q, want ok", resp.Reply)
|
||||
}
|
||||
if attempts != 3 {
|
||||
t.Fatalf("attempts = %d, want 3", attempts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAskRetriesTransientErrorsThenSurfacesStructuredError(t *testing.T) {
|
||||
attempts := 0
|
||||
fakeGen = func(ctx context.Context, opts ai.Options, req *ai.Request) (*ai.Response, error) {
|
||||
attempts++
|
||||
return nil, context.DeadlineExceeded
|
||||
}
|
||||
defer func() { fakeGen = nil }()
|
||||
|
||||
a := newTestAgent(Name("retry-fail"), ModelRetry(2, time.Millisecond))
|
||||
_, err := a.Ask(context.Background(), "hello")
|
||||
var retryErr *ai.RetryError
|
||||
if !errors.As(err, &retryErr) {
|
||||
t.Fatalf("Ask error = %T %v, want *ai.RetryError", err, err)
|
||||
}
|
||||
if retryErr.Attempts != 2 {
|
||||
t.Fatalf("retry attempts = %d, want 2", retryErr.Attempts)
|
||||
}
|
||||
if attempts != 2 {
|
||||
t.Fatalf("model attempts = %d, want 2", attempts)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"go-micro.dev/v6/ai"
|
||||
"go-micro.dev/v6/registry"
|
||||
"go-micro.dev/v6/store"
|
||||
)
|
||||
|
||||
// A registered wrapper runs around every tool call and can observe and
|
||||
// modify the result.
|
||||
func TestWrapToolWraps(t *testing.T) {
|
||||
var saw string
|
||||
wrap := func(next ai.ToolHandler) ai.ToolHandler {
|
||||
return func(ctx context.Context, call ai.ToolCall) ai.ToolResult {
|
||||
saw = call.Name
|
||||
res := next(ctx, call)
|
||||
res.Content = "wrapped:" + res.Content
|
||||
return res
|
||||
}
|
||||
}
|
||||
|
||||
a := newTestAgent(Name("wrapped"), WrapTool(wrap))
|
||||
content := toolContent(a.toolHandler(), "demo_Svc_Do", map[string]any{})
|
||||
|
||||
if saw != "demo_Svc_Do" {
|
||||
t.Errorf("wrapper saw %q, want demo_Svc_Do", saw)
|
||||
}
|
||||
if !strings.HasPrefix(content, "wrapped:") {
|
||||
t.Errorf("wrapper did not modify the result; got %q", content)
|
||||
}
|
||||
}
|
||||
|
||||
// Multiple wrappers compose outermost-first: the first registered wrapper
|
||||
// is the outer layer, so it runs first on the way in and last on the way
|
||||
// out.
|
||||
func TestWrapToolOrder(t *testing.T) {
|
||||
var order []string
|
||||
mk := func(tag string) ai.ToolWrapper {
|
||||
return func(next ai.ToolHandler) ai.ToolHandler {
|
||||
return func(ctx context.Context, call ai.ToolCall) ai.ToolResult {
|
||||
order = append(order, "in:"+tag)
|
||||
res := next(ctx, call)
|
||||
order = append(order, "out:"+tag)
|
||||
return res
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
a := newTestAgent(Name("ordered"), WrapTool(mk("a"), mk("b")))
|
||||
toolContent(a.toolHandler(), "demo_Svc_Do", map[string]any{})
|
||||
|
||||
want := "in:a in:b out:b out:a"
|
||||
if got := strings.Join(order, " "); got != want {
|
||||
t.Errorf("wrapper order = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// Wrappers run outside the built-in guardrails, so they observe a refused
|
||||
// call and its refusal result rather than being short-circuited.
|
||||
func TestWrapToolSeesGuardrailRefusal(t *testing.T) {
|
||||
var sawResult string
|
||||
wrap := func(next ai.ToolHandler) ai.ToolHandler {
|
||||
return func(ctx context.Context, call ai.ToolCall) ai.ToolResult {
|
||||
res := next(ctx, call)
|
||||
sawResult = res.Content
|
||||
return res
|
||||
}
|
||||
}
|
||||
|
||||
a := newTestAgent(Name("gated-wrap"),
|
||||
ApproveTool(func(tool string, input map[string]any) (bool, string) {
|
||||
return false, "denied"
|
||||
}),
|
||||
WrapTool(wrap),
|
||||
)
|
||||
toolContent(a.toolHandler(), "demo_Svc_Do", map[string]any{})
|
||||
|
||||
if !strings.Contains(sawResult, "not approved") {
|
||||
t.Errorf("wrapper should observe the guardrail refusal; got %q", sawResult)
|
||||
}
|
||||
}
|
||||
|
||||
// A guardrail refusal carries a structured reason a wrapper can switch on,
|
||||
// so reliability tooling (e.g. loop handling) needn't parse the message.
|
||||
func TestWrapToolSeesRefusedReason(t *testing.T) {
|
||||
a := newTestAgent(Name("looper"), LoopLimit(2))
|
||||
h := a.toolHandler()
|
||||
|
||||
var last ai.ToolResult
|
||||
for i := 0; i < 3; i++ {
|
||||
last = h(context.Background(), ai.ToolCall{ID: "x", Name: "demo_Svc_Do", Input: map[string]any{"q": "same"}})
|
||||
}
|
||||
if last.Refused != ai.RefusedLoop {
|
||||
t.Errorf("Refused = %q, want %q", last.Refused, ai.RefusedLoop)
|
||||
}
|
||||
}
|
||||
|
||||
// ctxMock is a model that forwards the Generate context to the tool
|
||||
// handler (as real providers do), so a wrapper can read ai.RunInfo.
|
||||
type ctxMock struct{ opts ai.Options }
|
||||
|
||||
func (m *ctxMock) Init(opts ...ai.Option) error {
|
||||
for _, o := range opts {
|
||||
o(&m.opts)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (m *ctxMock) Options() ai.Options { return m.opts }
|
||||
func (m *ctxMock) String() string { return "ctxmock" }
|
||||
func (m *ctxMock) Stream(context.Context, *ai.Request, ...ai.GenerateOption) (ai.Stream, error) {
|
||||
return nil, fmt.Errorf("no stream")
|
||||
}
|
||||
func (m *ctxMock) Generate(ctx context.Context, _ *ai.Request, _ ...ai.GenerateOption) (*ai.Response, error) {
|
||||
if m.opts.ToolHandler != nil {
|
||||
m.opts.ToolHandler(ctx, ai.ToolCall{ID: "c1", Name: "demo_Svc_Do", Input: map[string]any{}})
|
||||
}
|
||||
return &ai.Response{Answer: "done"}, nil
|
||||
}
|
||||
|
||||
// During an Ask, a wrapper sees RunInfo on the context: a correlation id
|
||||
// for the run and the agent's name.
|
||||
func TestWrapToolSeesRunInfo(t *testing.T) {
|
||||
ai.Register("ctxmock", func(opts ...ai.Option) ai.Model {
|
||||
m := &ctxMock{}
|
||||
_ = m.Init(opts...)
|
||||
return m
|
||||
})
|
||||
|
||||
var got ai.RunInfo
|
||||
var ok bool
|
||||
a := New(
|
||||
Name("runner"),
|
||||
Provider("ctxmock"),
|
||||
WithRegistry(registry.NewMemoryRegistry()),
|
||||
WithStore(store.NewMemoryStore()),
|
||||
WrapTool(func(next ai.ToolHandler) ai.ToolHandler {
|
||||
return func(ctx context.Context, call ai.ToolCall) ai.ToolResult {
|
||||
got, ok = ai.RunInfoFrom(ctx)
|
||||
return next(ctx, call)
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
||||
resp, err := a.Ask(context.Background(), "go")
|
||||
if err != nil {
|
||||
t.Fatalf("Ask: %v", err)
|
||||
}
|
||||
if !ok {
|
||||
t.Fatal("wrapper did not see RunInfo on the context")
|
||||
}
|
||||
if got.Agent != "runner" {
|
||||
t.Errorf("RunInfo.Agent = %q, want runner", got.Agent)
|
||||
}
|
||||
if got.RunID == "" {
|
||||
t.Error("RunInfo.RunID is empty")
|
||||
}
|
||||
if resp.RunID != got.RunID {
|
||||
t.Errorf("Response.RunID = %q, want wrapper RunID %q", resp.RunID, got.RunID)
|
||||
}
|
||||
if resp.ParentID != "" {
|
||||
t.Errorf("Response.ParentID = %q, want empty", resp.ParentID)
|
||||
}
|
||||
}
|
||||
|
||||
// call.Scan decodes a tool call's input into a typed struct.
|
||||
func TestToolCallScan(t *testing.T) {
|
||||
call := ai.ToolCall{Input: map[string]any{"query": "hello", "limit": 5}}
|
||||
var args struct {
|
||||
Query string `json:"query"`
|
||||
Limit int `json:"limit"`
|
||||
}
|
||||
if err := call.Scan(&args); err != nil {
|
||||
t.Fatalf("Scan: %v", err)
|
||||
}
|
||||
if args.Query != "hello" || args.Limit != 5 {
|
||||
t.Errorf("Scan decoded %+v, want {hello 5}", args)
|
||||
}
|
||||
}
|
||||
+6
-5
@@ -133,14 +133,15 @@ m.Init(
|
||||
The model can automatically execute tool calls when provided with a tool handler:
|
||||
|
||||
```go
|
||||
// Define a tool handler
|
||||
toolHandler := func(name string, input map[string]any) (result any, content string) {
|
||||
// Define a tool handler. It mirrors a go-micro RPC handler: context
|
||||
// first, the call in, a result out.
|
||||
toolHandler := func(ctx context.Context, call ai.ToolCall) ai.ToolResult {
|
||||
// Execute the tool and return results
|
||||
switch name {
|
||||
switch call.Name {
|
||||
case "get_weather":
|
||||
return map[string]string{"temp": "72F"}, `{"temp": "72F"}`
|
||||
return ai.ToolResult{ID: call.ID, Value: map[string]string{"temp": "72F"}, Content: `{"temp": "72F"}`}
|
||||
default:
|
||||
return nil, `{"error": "unknown tool"}`
|
||||
return ai.ToolResult{ID: call.ID, Content: `{"error": "unknown tool"}`}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+77
-32
@@ -10,7 +10,7 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"go-micro.dev/v5/ai"
|
||||
"go-micro.dev/v6/ai"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -77,7 +77,7 @@ func (p *Provider) Generate(ctx context.Context, req *ai.Request, opts ...ai.Gen
|
||||
// Build initial request
|
||||
apiReq := map[string]any{
|
||||
"model": p.opts.Model,
|
||||
"max_tokens": 4096,
|
||||
"max_tokens": 8192,
|
||||
"system": req.SystemPrompt,
|
||||
"messages": []map[string]any{
|
||||
{"role": "user", "content": req.Prompt},
|
||||
@@ -94,48 +94,66 @@ func (p *Provider) Generate(ctx context.Context, req *ai.Request, opts ...ai.Gen
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// If no tool calls, return response
|
||||
if len(resp.ToolCalls) == 0 {
|
||||
// If no tool calls or no handler, return as-is
|
||||
if len(resp.ToolCalls) == 0 || p.opts.ToolHandler == nil {
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// If tool handler is provided, execute tools and get final answer
|
||||
if p.opts.ToolHandler != nil {
|
||||
var toolResults []ai.ToolResult
|
||||
for _, tc := range resp.ToolCalls {
|
||||
_, content := p.opts.ToolHandler(tc.Name, tc.Input)
|
||||
toolResults = append(toolResults, ai.ToolResult{
|
||||
ID: tc.ID,
|
||||
Content: content,
|
||||
// Tool execution loop: execute tools, send results back, repeat
|
||||
// until the model responds with text only (no more tool calls)
|
||||
messages := []map[string]any{
|
||||
{"role": "user", "content": req.Prompt},
|
||||
{"role": "assistant", "content": cleanContent(rawContent)},
|
||||
}
|
||||
|
||||
pendingCalls := resp.ToolCalls
|
||||
|
||||
for rounds := 0; rounds < 10; rounds++ {
|
||||
var toolResultBlocks []map[string]any
|
||||
for i := range pendingCalls {
|
||||
content := p.opts.ToolHandler(ctx, pendingCalls[i]).Content
|
||||
pendingCalls[i].Result = content
|
||||
toolResultBlocks = append(toolResultBlocks, map[string]any{
|
||||
"type": "tool_result",
|
||||
"tool_use_id": pendingCalls[i].ID,
|
||||
"content": content,
|
||||
})
|
||||
}
|
||||
|
||||
// Build follow-up request with tool results
|
||||
var toolResultBlocks []map[string]any
|
||||
for _, tr := range toolResults {
|
||||
toolResultBlocks = append(toolResultBlocks, map[string]any{
|
||||
"type": "tool_result",
|
||||
"tool_use_id": tr.ID,
|
||||
"content": tr.Content,
|
||||
})
|
||||
}
|
||||
messages = append(messages, map[string]any{
|
||||
"role": "user",
|
||||
"content": toolResultBlocks,
|
||||
})
|
||||
|
||||
followUpReq := map[string]any{
|
||||
"model": p.opts.Model,
|
||||
"max_tokens": 4096,
|
||||
"max_tokens": 8192,
|
||||
"system": req.SystemPrompt,
|
||||
"messages": []map[string]any{
|
||||
{"role": "user", "content": req.Prompt},
|
||||
{"role": "assistant", "content": rawContent},
|
||||
{"role": "user", "content": toolResultBlocks},
|
||||
},
|
||||
"messages": messages,
|
||||
}
|
||||
if len(anthropicTools) > 0 {
|
||||
followUpReq["tools"] = anthropicTools
|
||||
}
|
||||
|
||||
// Make follow-up API call
|
||||
followUpResp, _, err := p.callAPI(ctx, followUpReq)
|
||||
if err == nil && followUpResp.Reply != "" {
|
||||
followUpResp, followUpRaw, err := p.callAPI(ctx, followUpReq)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
if len(followUpResp.ToolCalls) > 0 {
|
||||
resp.ToolCalls = append(resp.ToolCalls, followUpResp.ToolCalls...)
|
||||
pendingCalls = followUpResp.ToolCalls
|
||||
messages = append(messages, map[string]any{
|
||||
"role": "assistant",
|
||||
"content": cleanContent(followUpRaw),
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
if followUpResp.Reply != "" {
|
||||
resp.Answer = followUpResp.Reply
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
@@ -156,7 +174,7 @@ func (p *Provider) callAPI(ctx context.Context, req map[string]any) (*ai.Respons
|
||||
|
||||
// Build HTTP request
|
||||
apiURL := strings.TrimRight(p.opts.BaseURL, "/") + "/v1/messages"
|
||||
httpReq, err := http.NewRequestWithContext(ctx, "POST", apiURL, bytes.NewReader(reqBody))
|
||||
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(reqBody))
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
@@ -175,7 +193,7 @@ func (p *Provider) callAPI(ctx context.Context, req map[string]any) (*ai.Respons
|
||||
|
||||
// Read response
|
||||
respBody, _ := io.ReadAll(httpResp.Body)
|
||||
if httpResp.StatusCode != 200 {
|
||||
if httpResp.StatusCode != http.StatusOK {
|
||||
return nil, nil, fmt.Errorf("API error (%s): %s", httpResp.Status, string(respBody))
|
||||
}
|
||||
|
||||
@@ -225,3 +243,30 @@ func (p *Provider) callAPI(ctx context.Context, req map[string]any) (*ai.Respons
|
||||
|
||||
return response, anthropicResp.Content, nil
|
||||
}
|
||||
|
||||
// cleanContent strips fields from response content blocks that Anthropic
|
||||
// rejects when sent back as assistant message content (e.g. "id" on text blocks).
|
||||
func cleanContent(raw any) any {
|
||||
blocks, ok := raw.([]struct {
|
||||
Type string `json:"type"`
|
||||
Text string `json:"text"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Input json.RawMessage `json:"input"`
|
||||
})
|
||||
if !ok {
|
||||
return raw
|
||||
}
|
||||
var cleaned []map[string]any
|
||||
for _, b := range blocks {
|
||||
switch b.Type {
|
||||
case "text":
|
||||
cleaned = append(cleaned, map[string]any{"type": "text", "text": b.Text})
|
||||
case "tool_use":
|
||||
var input any
|
||||
_ = json.Unmarshal(b.Input, &input)
|
||||
cleaned = append(cleaned, map[string]any{"type": "tool_use", "id": b.ID, "name": b.Name, "input": input})
|
||||
}
|
||||
}
|
||||
return cleaned
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"go-micro.dev/v5/ai"
|
||||
"go-micro.dev/v6/ai"
|
||||
)
|
||||
|
||||
func TestProvider_String(t *testing.T) {
|
||||
|
||||
+11
-11
@@ -7,7 +7,7 @@
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// import _ "go-micro.dev/v5/ai/atlascloud"
|
||||
// import _ "go-micro.dev/v6/ai/atlascloud"
|
||||
//
|
||||
// m := ai.New("atlascloud",
|
||||
// ai.WithAPIKey("your-api-key"),
|
||||
@@ -29,7 +29,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go-micro.dev/v5/ai"
|
||||
"go-micro.dev/v6/ai"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -120,7 +120,7 @@ func (p *Provider) Generate(ctx context.Context, req *ai.Request, opts ...ai.Gen
|
||||
})
|
||||
|
||||
for _, tc := range resp.ToolCalls {
|
||||
_, content := p.opts.ToolHandler(tc.Name, tc.Input)
|
||||
content := p.opts.ToolHandler(ctx, tc).Content
|
||||
followUpMessages = append(followUpMessages, map[string]any{
|
||||
"role": "tool",
|
||||
"tool_call_id": tc.ID,
|
||||
@@ -153,7 +153,7 @@ func (p *Provider) callAPI(ctx context.Context, req map[string]any) (*ai.Respons
|
||||
}
|
||||
|
||||
apiURL := strings.TrimRight(p.opts.BaseURL, "/") + "/v1/chat/completions"
|
||||
httpReq, err := http.NewRequestWithContext(ctx, "POST", apiURL, bytes.NewReader(reqBody))
|
||||
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(reqBody))
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
@@ -168,7 +168,7 @@ func (p *Provider) callAPI(ctx context.Context, req map[string]any) (*ai.Respons
|
||||
defer httpResp.Body.Close()
|
||||
|
||||
respBody, _ := io.ReadAll(httpResp.Body)
|
||||
if httpResp.StatusCode != 200 {
|
||||
if httpResp.StatusCode != http.StatusOK {
|
||||
return nil, nil, fmt.Errorf("API error (%s): %s", httpResp.Status, string(respBody))
|
||||
}
|
||||
|
||||
@@ -259,7 +259,7 @@ func (p *Provider) GenerateImage(ctx context.Context, req *ai.ImageRequest, opts
|
||||
}
|
||||
|
||||
apiURL := strings.TrimRight(p.opts.BaseURL, "/") + "/api/v1/model/generateImage"
|
||||
httpReq, err := http.NewRequestWithContext(ctx, "POST", apiURL, bytes.NewReader(reqBody))
|
||||
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(reqBody))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
@@ -273,7 +273,7 @@ func (p *Provider) GenerateImage(ctx context.Context, req *ai.ImageRequest, opts
|
||||
defer httpResp.Body.Close()
|
||||
|
||||
respBody, _ := io.ReadAll(httpResp.Body)
|
||||
if httpResp.StatusCode != 200 {
|
||||
if httpResp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("API error (%s): %s", httpResp.Status, string(respBody))
|
||||
}
|
||||
|
||||
@@ -315,7 +315,7 @@ func (p *Provider) GenerateImage(ctx context.Context, req *ai.ImageRequest, opts
|
||||
}
|
||||
|
||||
func (p *Provider) pollPrediction(ctx context.Context, url string) (*ai.ImageResponse, error) {
|
||||
httpReq, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
||||
httpReq, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -395,7 +395,7 @@ func (p *Provider) GenerateVideo(ctx context.Context, req *ai.VideoRequest, opts
|
||||
}
|
||||
|
||||
apiURL := strings.TrimRight(p.opts.BaseURL, "/") + "/api/v1/model/generateVideo"
|
||||
httpReq, err := http.NewRequestWithContext(ctx, "POST", apiURL, bytes.NewReader(reqBody))
|
||||
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(reqBody))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
@@ -409,7 +409,7 @@ func (p *Provider) GenerateVideo(ctx context.Context, req *ai.VideoRequest, opts
|
||||
defer httpResp.Body.Close()
|
||||
|
||||
respBody, _ := io.ReadAll(httpResp.Body)
|
||||
if httpResp.StatusCode != 200 {
|
||||
if httpResp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("API error (%s): %s", httpResp.Status, string(respBody))
|
||||
}
|
||||
|
||||
@@ -450,7 +450,7 @@ func (p *Provider) GenerateVideo(ctx context.Context, req *ai.VideoRequest, opts
|
||||
}
|
||||
|
||||
func (p *Provider) pollVideo(ctx context.Context, url string) (*ai.VideoResponse, error) {
|
||||
httpReq, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
||||
httpReq, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"go-micro.dev/v5/ai"
|
||||
"go-micro.dev/v6/ai"
|
||||
)
|
||||
|
||||
func TestProvider_String(t *testing.T) {
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
// Package flow is maintained for backward compatibility.
|
||||
// The canonical import is go-micro.dev/v6/flow.
|
||||
package flow
|
||||
|
||||
import "go-micro.dev/v6/flow"
|
||||
|
||||
// Re-export types for backward compatibility.
|
||||
type Flow = flow.Flow
|
||||
type Options = flow.Options
|
||||
type Option = flow.Option
|
||||
type Result = flow.Result
|
||||
|
||||
var New = flow.New
|
||||
var Trigger = flow.Trigger
|
||||
var Prompt = flow.Prompt
|
||||
var SystemPrompt = flow.SystemPrompt
|
||||
var Provider = flow.Provider
|
||||
var APIKey = flow.APIKey
|
||||
var Model = flow.Model
|
||||
var BaseURL = flow.BaseURL
|
||||
var HistoryLimit = flow.HistoryLimit
|
||||
var OnResult = flow.OnResult
|
||||
+5
-5
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// import _ "go-micro.dev/v5/ai/gemini"
|
||||
// import _ "go-micro.dev/v6/ai/gemini"
|
||||
//
|
||||
// m := ai.New("gemini",
|
||||
// ai.WithAPIKey("your-api-key"),
|
||||
@@ -18,7 +18,7 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"go-micro.dev/v5/ai"
|
||||
"go-micro.dev/v6/ai"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -101,7 +101,7 @@ func (p *Provider) Generate(ctx context.Context, req *ai.Request, opts ...ai.Gen
|
||||
if p.opts.ToolHandler != nil {
|
||||
var resultParts []map[string]any
|
||||
for _, tc := range resp.ToolCalls {
|
||||
result, _ := p.opts.ToolHandler(tc.Name, tc.Input)
|
||||
result := p.opts.ToolHandler(ctx, tc).Value
|
||||
resultParts = append(resultParts, map[string]any{
|
||||
"functionResponse": map[string]any{
|
||||
"name": tc.Name,
|
||||
@@ -147,7 +147,7 @@ func (p *Provider) callAPI(ctx context.Context, req map[string]any) (*ai.Respons
|
||||
apiURL := strings.TrimRight(p.opts.BaseURL, "/") +
|
||||
"/v1beta/models/" + p.opts.Model + ":generateContent"
|
||||
|
||||
httpReq, err := http.NewRequestWithContext(ctx, "POST", apiURL, bytes.NewReader(reqBody))
|
||||
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(reqBody))
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
@@ -162,7 +162,7 @@ func (p *Provider) callAPI(ctx context.Context, req map[string]any) (*ai.Respons
|
||||
defer httpResp.Body.Close()
|
||||
|
||||
respBody, _ := io.ReadAll(httpResp.Body)
|
||||
if httpResp.StatusCode != 200 {
|
||||
if httpResp.StatusCode != http.StatusOK {
|
||||
return nil, nil, fmt.Errorf("API error (%s): %s", httpResp.Status, string(respBody))
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"go-micro.dev/v5/ai"
|
||||
"go-micro.dev/v6/ai"
|
||||
)
|
||||
|
||||
func TestProvider_String(t *testing.T) {
|
||||
|
||||
+5
-5
@@ -5,7 +5,7 @@
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// import _ "go-micro.dev/v5/ai/groq"
|
||||
// import _ "go-micro.dev/v6/ai/groq"
|
||||
//
|
||||
// m := ai.New("groq",
|
||||
// ai.WithAPIKey("your-api-key"),
|
||||
@@ -21,7 +21,7 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"go-micro.dev/v5/ai"
|
||||
"go-micro.dev/v6/ai"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -99,7 +99,7 @@ func (p *Provider) Generate(ctx context.Context, req *ai.Request, opts ...ai.Gen
|
||||
"tool_calls": rawMessage["tool_calls"],
|
||||
})
|
||||
for _, tc := range resp.ToolCalls {
|
||||
_, content := p.opts.ToolHandler(tc.Name, tc.Input)
|
||||
content := p.opts.ToolHandler(ctx, tc).Content
|
||||
followUpMessages = append(followUpMessages, map[string]any{
|
||||
"role": "tool",
|
||||
"tool_call_id": tc.ID,
|
||||
@@ -129,7 +129,7 @@ func (p *Provider) callAPI(ctx context.Context, req map[string]any) (*ai.Respons
|
||||
}
|
||||
|
||||
apiURL := strings.TrimRight(p.opts.BaseURL, "/") + "/v1/chat/completions"
|
||||
httpReq, err := http.NewRequestWithContext(ctx, "POST", apiURL, bytes.NewReader(reqBody))
|
||||
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(reqBody))
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
@@ -144,7 +144,7 @@ func (p *Provider) callAPI(ctx context.Context, req map[string]any) (*ai.Respons
|
||||
defer httpResp.Body.Close()
|
||||
|
||||
respBody, _ := io.ReadAll(httpResp.Body)
|
||||
if httpResp.StatusCode != 200 {
|
||||
if httpResp.StatusCode != http.StatusOK {
|
||||
return nil, nil, fmt.Errorf("API error (%s): %s", httpResp.Status, string(respBody))
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"go-micro.dev/v5/ai"
|
||||
"go-micro.dev/v6/ai"
|
||||
)
|
||||
|
||||
func TestProvider_String(t *testing.T) {
|
||||
|
||||
+28
-75
@@ -1,92 +1,45 @@
|
||||
package ai
|
||||
|
||||
import "context"
|
||||
|
||||
// History accumulates conversation messages and feeds them into
|
||||
// Generate calls. It handles the bookkeeping of adding user prompts,
|
||||
// assistant replies, and tool call/result pairs so callers don't
|
||||
// have to manage the message list manually.
|
||||
// History is a convenience for accumulating conversation messages
|
||||
// with automatic truncation. Use it to build Request.Messages for
|
||||
// multi-turn conversations.
|
||||
//
|
||||
// When the message count exceeds Limit, the oldest messages (after
|
||||
// the system prompt) are dropped. Set Limit to 0 for unlimited.
|
||||
// hist := ai.NewHistory(50)
|
||||
// hist.Add("user", "hello")
|
||||
// resp, _ := m.Generate(ctx, &ai.Request{Messages: hist.Messages(), Prompt: "next"})
|
||||
// hist.Add("assistant", resp.Reply)
|
||||
type History struct {
|
||||
messages []Message
|
||||
systemPrompt string
|
||||
limit int
|
||||
messages []Message
|
||||
limit int
|
||||
}
|
||||
|
||||
// NewHistory creates an empty History. limit controls the maximum
|
||||
// number of messages retained (0 = unlimited). The system prompt is
|
||||
// always preserved regardless of truncation.
|
||||
func NewHistory(systemPrompt string, limit int) *History {
|
||||
return &History{
|
||||
systemPrompt: systemPrompt,
|
||||
limit: limit,
|
||||
}
|
||||
// number of messages retained (0 = unlimited).
|
||||
func NewHistory(limit int) *History {
|
||||
return &History{limit: limit}
|
||||
}
|
||||
|
||||
// Generate sends a user prompt through the model with the full
|
||||
// conversation history. The user message, assistant reply, and any
|
||||
// tool call/result pairs are appended to the history automatically.
|
||||
func (h *History) Generate(ctx context.Context, m Model, prompt string, tools []Tool) (*Response, error) {
|
||||
h.add("user", prompt)
|
||||
|
||||
resp, err := m.Generate(ctx, &Request{
|
||||
Prompt: prompt,
|
||||
SystemPrompt: h.systemPrompt,
|
||||
Tools: tools,
|
||||
Messages: h.snapshot(),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Record the assistant's reply.
|
||||
if resp.Reply != "" {
|
||||
h.add("assistant", resp.Reply)
|
||||
}
|
||||
|
||||
// Record tool calls and results so subsequent turns have context.
|
||||
for _, tc := range resp.ToolCalls {
|
||||
h.add("assistant", tc)
|
||||
}
|
||||
if resp.Answer != "" {
|
||||
h.add("assistant", resp.Answer)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// Messages returns a copy of the current message history.
|
||||
func (h *History) Messages() []Message {
|
||||
return h.snapshot()
|
||||
}
|
||||
|
||||
// Len returns the number of messages in the history.
|
||||
func (h *History) Len() int {
|
||||
return len(h.messages)
|
||||
}
|
||||
|
||||
// Reset clears all messages but keeps the system prompt and limit.
|
||||
func (h *History) Reset() {
|
||||
h.messages = nil
|
||||
}
|
||||
|
||||
func (h *History) add(role string, content any) {
|
||||
// Add appends a message and truncates if over limit.
|
||||
func (h *History) Add(role string, content any) {
|
||||
h.messages = append(h.messages, Message{Role: role, Content: content})
|
||||
h.truncate()
|
||||
}
|
||||
|
||||
func (h *History) truncate() {
|
||||
if h.limit <= 0 || len(h.messages) <= h.limit {
|
||||
return
|
||||
if h.limit > 0 && len(h.messages) > h.limit {
|
||||
h.messages = h.messages[len(h.messages)-h.limit:]
|
||||
}
|
||||
drop := len(h.messages) - h.limit
|
||||
h.messages = h.messages[drop:]
|
||||
}
|
||||
|
||||
func (h *History) snapshot() []Message {
|
||||
// Messages returns a copy of the accumulated messages.
|
||||
func (h *History) Messages() []Message {
|
||||
out := make([]Message, len(h.messages))
|
||||
copy(out, h.messages)
|
||||
return out
|
||||
}
|
||||
|
||||
// Len returns the number of messages.
|
||||
func (h *History) Len() int {
|
||||
return len(h.messages)
|
||||
}
|
||||
|
||||
// Reset clears all messages.
|
||||
func (h *History) Reset() {
|
||||
h.messages = nil
|
||||
}
|
||||
|
||||
+27
-101
@@ -1,90 +1,40 @@
|
||||
package ai
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
import "testing"
|
||||
|
||||
type mockModel struct {
|
||||
reply string
|
||||
calls int
|
||||
}
|
||||
func TestHistory_Add(t *testing.T) {
|
||||
h := NewHistory(0)
|
||||
h.Add("user", "hello")
|
||||
h.Add("assistant", "hi")
|
||||
|
||||
func (m *mockModel) Init(...Option) error { return nil }
|
||||
func (m *mockModel) Options() Options { return Options{} }
|
||||
func (m *mockModel) String() string { return "mock" }
|
||||
func (m *mockModel) Stream(context.Context, *Request, ...GenerateOption) (Stream, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m *mockModel) Generate(_ context.Context, req *Request, _ ...GenerateOption) (*Response, error) {
|
||||
m.calls++
|
||||
return &Response{Reply: m.reply}, nil
|
||||
}
|
||||
|
||||
func TestHistory_AccumulatesMessages(t *testing.T) {
|
||||
m := &mockModel{reply: "hi"}
|
||||
h := NewHistory("system", 0)
|
||||
|
||||
resp, err := h.Generate(context.Background(), m, "hello", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp.Reply != "hi" {
|
||||
t.Errorf("reply = %q", resp.Reply)
|
||||
}
|
||||
// user + assistant = 2
|
||||
if h.Len() != 2 {
|
||||
t.Errorf("len = %d, want 2", h.Len())
|
||||
}
|
||||
|
||||
h.Generate(context.Background(), m, "again", nil)
|
||||
// 2 + user + assistant = 4
|
||||
if h.Len() != 4 {
|
||||
t.Errorf("len = %d, want 4", h.Len())
|
||||
}
|
||||
|
||||
msgs := h.Messages()
|
||||
if msgs[0].Role != "user" || msgs[0].Content != "hello" {
|
||||
t.Errorf("first message = %+v", msgs[0])
|
||||
t.Errorf("first = %+v", msgs[0])
|
||||
}
|
||||
if msgs[1].Role != "assistant" || msgs[1].Content != "hi" {
|
||||
t.Errorf("second message = %+v", msgs[1])
|
||||
}
|
||||
if msgs[2].Role != "user" || msgs[2].Content != "again" {
|
||||
t.Errorf("third message = %+v", msgs[2])
|
||||
t.Errorf("second = %+v", msgs[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestHistory_Truncation(t *testing.T) {
|
||||
m := &mockModel{reply: "ok"}
|
||||
h := NewHistory("system", 4)
|
||||
|
||||
h.Generate(context.Background(), m, "one", nil)
|
||||
h.Generate(context.Background(), m, "two", nil)
|
||||
h.Generate(context.Background(), m, "three", nil)
|
||||
|
||||
// 3 turns x 2 messages = 6, but limit is 4
|
||||
if h.Len() != 4 {
|
||||
t.Errorf("len = %d, want 4", h.Len())
|
||||
h := NewHistory(3)
|
||||
for _, m := range []string{"a", "b", "c", "d", "e"} {
|
||||
h.Add("user", m)
|
||||
}
|
||||
|
||||
msgs := h.Messages()
|
||||
// oldest messages should have been dropped
|
||||
if msgs[0].Role != "user" || msgs[0].Content != "two" {
|
||||
t.Errorf("first retained message = %+v, want user/two", msgs[0])
|
||||
if h.Len() != 3 {
|
||||
t.Errorf("len = %d, want 3", h.Len())
|
||||
}
|
||||
if h.Messages()[0].Content != "c" {
|
||||
t.Errorf("first retained = %+v", h.Messages()[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestHistory_Reset(t *testing.T) {
|
||||
m := &mockModel{reply: "ok"}
|
||||
h := NewHistory("system", 0)
|
||||
|
||||
h.Generate(context.Background(), m, "hello", nil)
|
||||
if h.Len() == 0 {
|
||||
t.Fatal("expected messages")
|
||||
}
|
||||
|
||||
h := NewHistory(0)
|
||||
h.Add("user", "hello")
|
||||
h.Reset()
|
||||
if h.Len() != 0 {
|
||||
t.Errorf("len after reset = %d", h.Len())
|
||||
@@ -92,45 +42,21 @@ func TestHistory_Reset(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestHistory_SnapshotIsCopy(t *testing.T) {
|
||||
m := &mockModel{reply: "ok"}
|
||||
h := NewHistory("system", 0)
|
||||
|
||||
h.Generate(context.Background(), m, "hello", nil)
|
||||
h := NewHistory(0)
|
||||
h.Add("user", "hello")
|
||||
msgs := h.Messages()
|
||||
msgs[0].Content = "mutated"
|
||||
|
||||
if h.Messages()[0].Content == "mutated" {
|
||||
t.Error("Messages() returned a reference, not a copy")
|
||||
t.Error("snapshot returned reference, not copy")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHistory_ToolCallsRecorded(t *testing.T) {
|
||||
m := &mockModel{}
|
||||
m2 := &toolModel{}
|
||||
h := NewHistory("system", 0)
|
||||
|
||||
h.Generate(context.Background(), m2, "do something", nil)
|
||||
|
||||
// user + assistant (tool call) + assistant (answer) = 3
|
||||
if h.Len() != 3 {
|
||||
t.Errorf("len = %d, want 3", h.Len())
|
||||
func TestHistory_Unlimited(t *testing.T) {
|
||||
h := NewHistory(0)
|
||||
for i := 0; i < 100; i++ {
|
||||
h.Add("user", "msg")
|
||||
}
|
||||
if h.Len() != 100 {
|
||||
t.Errorf("len = %d, want 100", h.Len())
|
||||
}
|
||||
|
||||
_ = m // avoid unused
|
||||
}
|
||||
|
||||
type toolModel struct{}
|
||||
|
||||
func (m *toolModel) Init(...Option) error { return nil }
|
||||
func (m *toolModel) Options() Options { return Options{} }
|
||||
func (m *toolModel) String() string { return "tool-mock" }
|
||||
func (m *toolModel) Stream(context.Context, *Request, ...GenerateOption) (Stream, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m *toolModel) Generate(_ context.Context, _ *Request, _ ...GenerateOption) (*Response, error) {
|
||||
return &Response{
|
||||
ToolCalls: []ToolCall{{ID: "1", Name: "test", Input: map[string]any{}}},
|
||||
Answer: "done",
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// import _ "go-micro.dev/v5/ai/mistral"
|
||||
// import _ "go-micro.dev/v6/ai/mistral"
|
||||
//
|
||||
// m := ai.New("mistral",
|
||||
// ai.WithAPIKey("your-api-key"),
|
||||
@@ -21,7 +21,7 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"go-micro.dev/v5/ai"
|
||||
"go-micro.dev/v6/ai"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -99,7 +99,7 @@ func (p *Provider) Generate(ctx context.Context, req *ai.Request, opts ...ai.Gen
|
||||
"tool_calls": rawMessage["tool_calls"],
|
||||
})
|
||||
for _, tc := range resp.ToolCalls {
|
||||
_, content := p.opts.ToolHandler(tc.Name, tc.Input)
|
||||
content := p.opts.ToolHandler(ctx, tc).Content
|
||||
followUpMessages = append(followUpMessages, map[string]any{
|
||||
"role": "tool",
|
||||
"tool_call_id": tc.ID,
|
||||
@@ -129,7 +129,7 @@ func (p *Provider) callAPI(ctx context.Context, req map[string]any) (*ai.Respons
|
||||
}
|
||||
|
||||
apiURL := strings.TrimRight(p.opts.BaseURL, "/") + "/v1/chat/completions"
|
||||
httpReq, err := http.NewRequestWithContext(ctx, "POST", apiURL, bytes.NewReader(reqBody))
|
||||
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(reqBody))
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
@@ -144,7 +144,7 @@ func (p *Provider) callAPI(ctx context.Context, req map[string]any) (*ai.Respons
|
||||
defer httpResp.Body.Close()
|
||||
|
||||
respBody, _ := io.ReadAll(httpResp.Body)
|
||||
if httpResp.StatusCode != 200 {
|
||||
if httpResp.StatusCode != http.StatusOK {
|
||||
return nil, nil, fmt.Errorf("API error (%s): %s", httpResp.Status, string(respBody))
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"go-micro.dev/v5/ai"
|
||||
"go-micro.dev/v6/ai"
|
||||
)
|
||||
|
||||
func TestProvider_String(t *testing.T) {
|
||||
|
||||
+82
-9
@@ -3,6 +3,7 @@ package ai
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -36,7 +37,8 @@ type Request struct {
|
||||
SystemPrompt string
|
||||
// Tools available for the model to use
|
||||
Tools []Tool
|
||||
// Messages for continuing a conversation (optional)
|
||||
// Messages for continuing a conversation (optional).
|
||||
// Use ai.History to accumulate these across turns.
|
||||
Messages []Message
|
||||
}
|
||||
|
||||
@@ -46,6 +48,13 @@ type Message struct {
|
||||
Content any // Can be string or structured content
|
||||
}
|
||||
|
||||
// Usage describes token counts returned by model providers.
|
||||
type Usage struct {
|
||||
InputTokens int `json:"input_tokens,omitempty"`
|
||||
OutputTokens int `json:"output_tokens,omitempty"`
|
||||
TotalTokens int `json:"total_tokens,omitempty"`
|
||||
}
|
||||
|
||||
// Response represents the response from a model
|
||||
type Response struct {
|
||||
// Reply is the text response from the model
|
||||
@@ -54,19 +63,74 @@ type Response struct {
|
||||
ToolCalls []ToolCall
|
||||
// Answer is the final answer after tool execution (if tools were used)
|
||||
Answer string
|
||||
// Usage contains provider token usage when available.
|
||||
Usage Usage
|
||||
}
|
||||
|
||||
// ToolCall represents a request to call a tool
|
||||
// ToolCall represents a request to call a tool and its result
|
||||
type ToolCall struct {
|
||||
ID string // Tool call ID (for correlation)
|
||||
Name string // Tool name
|
||||
Input map[string]any // Tool input arguments
|
||||
ID string // Tool call ID (for correlation)
|
||||
Name string // Tool name
|
||||
Input map[string]any // Tool input arguments
|
||||
Result string // Tool execution result (populated after execution)
|
||||
Error string // Tool execution error (populated after execution)
|
||||
}
|
||||
|
||||
// Scan decodes the call's Input into v (a pointer to a struct or map),
|
||||
// the same way a codec decodes an RPC request body. Use it when a tool
|
||||
// wants typed arguments instead of the raw map:
|
||||
//
|
||||
// var args struct{ Query string `json:"query"` }
|
||||
// if err := call.Scan(&args); err != nil { ... }
|
||||
func (c ToolCall) Scan(v any) error {
|
||||
b, err := json.Marshal(c.Input)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.Unmarshal(b, v)
|
||||
}
|
||||
|
||||
// ToolResult represents the result of a tool execution
|
||||
type ToolResult struct {
|
||||
ID string // Tool call ID (for correlation)
|
||||
Content string // Tool execution result (JSON string)
|
||||
Value any // Structured result (optional)
|
||||
Content string // Tool execution result (JSON string), shown to the model
|
||||
// Refused names the reason a guardrail blocked the call before it ran
|
||||
// ("max_steps", "loop", "approval"); empty when the call executed. A
|
||||
// tool wrapper can switch on it to build reliability tooling — react to
|
||||
// a detected loop, audit refusals — without parsing the message.
|
||||
Refused string `json:"refused,omitempty"`
|
||||
}
|
||||
|
||||
// Refusal reason codes set on ToolResult.Refused by the agent's guardrails.
|
||||
const (
|
||||
RefusedMaxSteps = "max_steps"
|
||||
RefusedLoop = "loop"
|
||||
RefusedApproval = "approval"
|
||||
)
|
||||
|
||||
// RunInfo describes the agent run a tool call belongs to. The agent
|
||||
// attaches it to the context passed to a ToolHandler, so a wrapper can
|
||||
// correlate calls within a run and across delegation without coupling to
|
||||
// the agent package. Per-call detail (tool name, id) is on the ToolCall;
|
||||
// step and attempt counts are naturally counted by the wrapper itself.
|
||||
type RunInfo struct {
|
||||
RunID string // correlation id for this agent run (one per Ask)
|
||||
ParentID string // the run that delegated to this one, if any
|
||||
Agent string // the agent's name
|
||||
}
|
||||
|
||||
type runInfoKey struct{}
|
||||
|
||||
// WithRunInfo attaches run info to ctx.
|
||||
func WithRunInfo(ctx context.Context, r RunInfo) context.Context {
|
||||
return context.WithValue(ctx, runInfoKey{}, r)
|
||||
}
|
||||
|
||||
// RunInfoFrom returns the run info attached to ctx, and whether it was set.
|
||||
func RunInfoFrom(ctx context.Context) (RunInfo, bool) {
|
||||
r, ok := ctx.Value(runInfoKey{}).(RunInfo)
|
||||
return r, ok
|
||||
}
|
||||
|
||||
// Stream is the interface for streaming responses (future implementation)
|
||||
@@ -77,8 +141,17 @@ type Stream interface {
|
||||
Close() error
|
||||
}
|
||||
|
||||
// ToolHandler is a function that handles tool calls
|
||||
type ToolHandler func(name string, input map[string]any) (result any, content string)
|
||||
// ToolHandler executes a tool call and returns its result. It mirrors a
|
||||
// go-micro RPC handler — context first, a request in, a result out — so
|
||||
// the same mental model carries over from services to tools.
|
||||
type ToolHandler func(ctx context.Context, call ToolCall) ToolResult
|
||||
|
||||
// ToolWrapper wraps a ToolHandler to add behavior around execution —
|
||||
// logging, metrics, retries, guardrails. It is the tool-side analog of
|
||||
// client.CallWrapper and server.HandlerWrapper: a wrapper takes the next
|
||||
// handler and returns a new one, and code before the next(...) call runs
|
||||
// before the tool, code after runs after.
|
||||
type ToolWrapper func(ToolHandler) ToolHandler
|
||||
|
||||
// NewFunc creates a new Model instance
|
||||
type NewFunc func(...Option) Model
|
||||
@@ -132,7 +205,7 @@ func AutoDetectProvider(baseURL string) string {
|
||||
// DefaultModel is a default model instance
|
||||
var DefaultModel Model
|
||||
|
||||
// Generate generates a response using the default model
|
||||
// Generate generates a response using the default model.
|
||||
func Generate(ctx context.Context, req *Request, opts ...GenerateOption) (*Response, error) {
|
||||
if DefaultModel == nil {
|
||||
return nil, nil
|
||||
|
||||
+12
-6
@@ -10,7 +10,7 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"go-micro.dev/v5/ai"
|
||||
"go-micro.dev/v6/ai"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -117,7 +117,7 @@ func (p *Provider) Generate(ctx context.Context, req *ai.Request, opts ...ai.Gen
|
||||
})
|
||||
|
||||
for _, tc := range resp.ToolCalls {
|
||||
_, content := p.opts.ToolHandler(tc.Name, tc.Input)
|
||||
content := p.opts.ToolHandler(ctx, tc).Content
|
||||
followUpMessages = append(followUpMessages, map[string]any{
|
||||
"role": "tool",
|
||||
"tool_call_id": tc.ID,
|
||||
@@ -155,7 +155,7 @@ func (p *Provider) callAPI(ctx context.Context, req map[string]any) (*ai.Respons
|
||||
|
||||
// Build HTTP request
|
||||
apiURL := strings.TrimRight(p.opts.BaseURL, "/") + "/v1/chat/completions"
|
||||
httpReq, err := http.NewRequestWithContext(ctx, "POST", apiURL, bytes.NewReader(reqBody))
|
||||
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(reqBody))
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
@@ -173,12 +173,17 @@ func (p *Provider) callAPI(ctx context.Context, req map[string]any) (*ai.Respons
|
||||
|
||||
// Read response
|
||||
respBody, _ := io.ReadAll(httpResp.Body)
|
||||
if httpResp.StatusCode != 200 {
|
||||
if httpResp.StatusCode != http.StatusOK {
|
||||
return nil, nil, fmt.Errorf("API error (%s): %s", httpResp.Status, string(respBody))
|
||||
}
|
||||
|
||||
// Parse response
|
||||
var chatResp struct {
|
||||
Usage struct {
|
||||
PromptTokens int `json:"prompt_tokens"`
|
||||
CompletionTokens int `json:"completion_tokens"`
|
||||
TotalTokens int `json:"total_tokens"`
|
||||
} `json:"usage"`
|
||||
Choices []struct {
|
||||
Message struct {
|
||||
Content string `json:"content"`
|
||||
@@ -204,6 +209,7 @@ func (p *Provider) callAPI(ctx context.Context, req map[string]any) (*ai.Respons
|
||||
choice := chatResp.Choices[0]
|
||||
response := &ai.Response{
|
||||
Reply: choice.Message.Content,
|
||||
Usage: ai.Usage{InputTokens: chatResp.Usage.PromptTokens, OutputTokens: chatResp.Usage.CompletionTokens, TotalTokens: chatResp.Usage.TotalTokens},
|
||||
}
|
||||
|
||||
// Extract tool calls
|
||||
@@ -255,7 +261,7 @@ func (p *Provider) GenerateImage(ctx context.Context, req *ai.ImageRequest, opts
|
||||
}
|
||||
|
||||
apiURL := strings.TrimRight(p.opts.BaseURL, "/") + "/v1/images/generations"
|
||||
httpReq, err := http.NewRequestWithContext(ctx, "POST", apiURL, bytes.NewReader(reqBody))
|
||||
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(reqBody))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
@@ -270,7 +276,7 @@ func (p *Provider) GenerateImage(ctx context.Context, req *ai.ImageRequest, opts
|
||||
defer httpResp.Body.Close()
|
||||
|
||||
respBody, _ := io.ReadAll(httpResp.Body)
|
||||
if httpResp.StatusCode != 200 {
|
||||
if httpResp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("API error (%s): %s", httpResp.Status, string(respBody))
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"go-micro.dev/v5/ai"
|
||||
"go-micro.dev/v6/ai"
|
||||
)
|
||||
|
||||
func TestProvider_String(t *testing.T) {
|
||||
|
||||
@@ -75,3 +75,19 @@ func WithToolHandler(handler ToolHandler) Option {
|
||||
o.ToolHandler = handler
|
||||
}
|
||||
}
|
||||
|
||||
// WithTools wires a Tools instance into the model, setting the tool
|
||||
// handler so the model can execute discovered service endpoints. The
|
||||
// tool list itself is passed per-request via Request.Tools.
|
||||
//
|
||||
// tools := ai.NewTools(service.Registry())
|
||||
// list, _ := tools.Discover()
|
||||
// m := ai.New("anthropic", ai.WithAPIKey(key), ai.WithTools(tools))
|
||||
// resp, _ := m.Generate(ctx, &ai.Request{Prompt: input, Tools: list})
|
||||
func WithTools(t *Tools) Option {
|
||||
return func(o *Options) {
|
||||
if t != nil {
|
||||
o.ToolHandler = t.Handler()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
package ai
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// StatusCoder is implemented by provider errors that expose an HTTP-like status code.
|
||||
type StatusCoder interface {
|
||||
StatusCode() int
|
||||
}
|
||||
|
||||
// RetryError is returned when Generate is retried and still fails.
|
||||
type RetryError struct {
|
||||
Attempts int
|
||||
Err error
|
||||
}
|
||||
|
||||
func (e *RetryError) Error() string {
|
||||
if e == nil {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf("ai generate failed after %d attempt(s): %v", e.Attempts, e.Err)
|
||||
}
|
||||
|
||||
func (e *RetryError) Unwrap() error {
|
||||
if e == nil {
|
||||
return nil
|
||||
}
|
||||
return e.Err
|
||||
}
|
||||
|
||||
// GeneratePolicy controls timeout and retry behavior for a model call.
|
||||
type GeneratePolicy struct {
|
||||
Timeout time.Duration
|
||||
MaxAttempts int
|
||||
Backoff time.Duration
|
||||
}
|
||||
|
||||
// GenerateWithRetry calls m.Generate with per-attempt timeout and bounded retry.
|
||||
func GenerateWithRetry(ctx context.Context, m Model, req *Request, policy GeneratePolicy, opts ...GenerateOption) (*Response, error) {
|
||||
if policy.MaxAttempts <= 0 {
|
||||
policy.MaxAttempts = 1
|
||||
}
|
||||
if m == nil {
|
||||
return nil, errors.New("ai model is nil")
|
||||
}
|
||||
|
||||
var last error
|
||||
for attempt := 1; attempt <= policy.MaxAttempts; attempt++ {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
callCtx := ctx
|
||||
cancel := func() {}
|
||||
if policy.Timeout > 0 {
|
||||
callCtx, cancel = context.WithTimeout(ctx, policy.Timeout)
|
||||
}
|
||||
resp, err := m.Generate(callCtx, req, opts...)
|
||||
cancel()
|
||||
if err == nil {
|
||||
return resp, nil
|
||||
}
|
||||
last = err
|
||||
|
||||
// Caller cancellation/deadline always wins and is not retried.
|
||||
if ctx.Err() != nil {
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
if attempt == policy.MaxAttempts || !IsTransientError(err) {
|
||||
if attempt > 1 || IsTransientError(err) {
|
||||
return nil, &RetryError{Attempts: attempt, Err: err}
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Always back off between retries — exponential and capped — so an
|
||||
// opt-in retry can never become a tight loop hammering the provider,
|
||||
// even if Backoff was left at zero.
|
||||
backoff := policy.Backoff
|
||||
if backoff <= 0 {
|
||||
backoff = 200 * time.Millisecond
|
||||
}
|
||||
if shift := attempt - 1; shift > 0 {
|
||||
backoff <<= shift
|
||||
}
|
||||
if backoff > 30*time.Second {
|
||||
backoff = 30 * time.Second
|
||||
}
|
||||
t := time.NewTimer(backoff)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
if !t.Stop() {
|
||||
<-t.C
|
||||
}
|
||||
return nil, ctx.Err()
|
||||
case <-t.C:
|
||||
}
|
||||
}
|
||||
return nil, &RetryError{Attempts: policy.MaxAttempts, Err: last}
|
||||
}
|
||||
|
||||
// IsTransientError reports whether err is worth retrying at the provider boundary.
|
||||
func IsTransientError(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
if errors.Is(err, context.Canceled) {
|
||||
return false
|
||||
}
|
||||
if errors.Is(err, context.DeadlineExceeded) {
|
||||
return true
|
||||
}
|
||||
var sc StatusCoder
|
||||
if errors.As(err, &sc) {
|
||||
code := sc.StatusCode()
|
||||
return code == 429 || code >= 500
|
||||
}
|
||||
msg := strings.ToLower(err.Error())
|
||||
return strings.Contains(msg, "rate limit") || strings.Contains(msg, "too many requests") || strings.Contains(msg, "timeout") || strings.Contains(msg, "temporar")
|
||||
}
|
||||
@@ -5,7 +5,7 @@
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// import _ "go-micro.dev/v5/ai/together"
|
||||
// import _ "go-micro.dev/v6/ai/together"
|
||||
//
|
||||
// m := ai.New("together",
|
||||
// ai.WithAPIKey("your-api-key"),
|
||||
@@ -21,7 +21,7 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"go-micro.dev/v5/ai"
|
||||
"go-micro.dev/v6/ai"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -99,7 +99,7 @@ func (p *Provider) Generate(ctx context.Context, req *ai.Request, opts ...ai.Gen
|
||||
"tool_calls": rawMessage["tool_calls"],
|
||||
})
|
||||
for _, tc := range resp.ToolCalls {
|
||||
_, content := p.opts.ToolHandler(tc.Name, tc.Input)
|
||||
content := p.opts.ToolHandler(ctx, tc).Content
|
||||
followUpMessages = append(followUpMessages, map[string]any{
|
||||
"role": "tool",
|
||||
"tool_call_id": tc.ID,
|
||||
@@ -129,7 +129,7 @@ func (p *Provider) callAPI(ctx context.Context, req map[string]any) (*ai.Respons
|
||||
}
|
||||
|
||||
apiURL := strings.TrimRight(p.opts.BaseURL, "/") + "/v1/chat/completions"
|
||||
httpReq, err := http.NewRequestWithContext(ctx, "POST", apiURL, bytes.NewReader(reqBody))
|
||||
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(reqBody))
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
@@ -144,7 +144,7 @@ func (p *Provider) callAPI(ctx context.Context, req map[string]any) (*ai.Respons
|
||||
defer httpResp.Body.Close()
|
||||
|
||||
respBody, _ := io.ReadAll(httpResp.Body)
|
||||
if httpResp.StatusCode != 200 {
|
||||
if httpResp.StatusCode != http.StatusOK {
|
||||
return nil, nil, fmt.Errorf("API error (%s): %s", httpResp.Status, string(respBody))
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"go-micro.dev/v5/ai"
|
||||
"go-micro.dev/v6/ai"
|
||||
)
|
||||
|
||||
func TestProvider_String(t *testing.T) {
|
||||
|
||||
+185
@@ -0,0 +1,185 @@
|
||||
package ai
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"go-micro.dev/v6/client"
|
||||
codecBytes "go-micro.dev/v6/codec/bytes"
|
||||
"go-micro.dev/v6/registry"
|
||||
)
|
||||
|
||||
type toolNameMap struct {
|
||||
mu sync.RWMutex
|
||||
m map[string]string
|
||||
}
|
||||
|
||||
func (n *toolNameMap) put(safe, original string) {
|
||||
n.mu.Lock()
|
||||
n.m[safe] = original
|
||||
n.mu.Unlock()
|
||||
}
|
||||
|
||||
func (n *toolNameMap) get(safe string) (string, bool) {
|
||||
n.mu.RLock()
|
||||
v, ok := n.m[safe]
|
||||
n.mu.RUnlock()
|
||||
return v, ok
|
||||
}
|
||||
|
||||
// Tools discovers go-micro services from a registry and converts their
|
||||
// endpoints into Tool definitions. It also executes tool calls via RPC.
|
||||
//
|
||||
// Create with NewTools, discover the tool list with Discover, and wire
|
||||
// execution into a model with WithTools:
|
||||
//
|
||||
// tools := ai.NewTools(service.Registry())
|
||||
// list, _ := tools.Discover()
|
||||
// m := ai.New("anthropic", ai.WithAPIKey(key), ai.WithTools(tools))
|
||||
// resp, _ := m.Generate(ctx, &ai.Request{Prompt: input, Tools: list})
|
||||
type Tools struct {
|
||||
registry registry.Registry
|
||||
client client.Client
|
||||
names *toolNameMap
|
||||
}
|
||||
|
||||
// ToolOption configures a Tools instance.
|
||||
type ToolOption func(*Tools)
|
||||
|
||||
// ToolClient sets the client used to execute tool calls. Defaults to
|
||||
// client.DefaultClient.
|
||||
func ToolClient(c client.Client) ToolOption {
|
||||
return func(t *Tools) {
|
||||
if c != nil {
|
||||
t.client = c
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// NewTools creates a Tools bound to the given registry.
|
||||
func NewTools(reg registry.Registry, opts ...ToolOption) *Tools {
|
||||
t := &Tools{
|
||||
registry: reg,
|
||||
client: client.DefaultClient,
|
||||
names: &toolNameMap{m: map[string]string{}},
|
||||
}
|
||||
for _, o := range opts {
|
||||
o(t)
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
// Discover walks the registry and returns one Tool per service
|
||||
// endpoint. Tool names are LLM-safe (dots replaced with underscores).
|
||||
func (t *Tools) Discover() ([]Tool, error) {
|
||||
services, err := t.registry.ListServices()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var out []Tool
|
||||
for _, svc := range services {
|
||||
full, err := t.registry.GetService(svc.Name)
|
||||
if err != nil || len(full) == 0 {
|
||||
continue
|
||||
}
|
||||
for _, ep := range full[0].Endpoints {
|
||||
original := fmt.Sprintf("%s.%s", svc.Name, ep.Name)
|
||||
safe := strings.ReplaceAll(original, ".", "_")
|
||||
t.names.put(safe, original)
|
||||
|
||||
desc := fmt.Sprintf("Call %s on %s service", ep.Name, svc.Name)
|
||||
if ep.Metadata != nil {
|
||||
if d, ok := ep.Metadata["description"]; ok && d != "" {
|
||||
desc = d
|
||||
}
|
||||
}
|
||||
|
||||
props := map[string]any{}
|
||||
if ep.Request != nil {
|
||||
for _, field := range ep.Request.Values {
|
||||
props[field.Name] = map[string]any{
|
||||
"type": toolJSONType(field.Type),
|
||||
"description": fmt.Sprintf("%s (%s)", field.Name, field.Type),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
out = append(out, Tool{
|
||||
Name: safe,
|
||||
OriginalName: original,
|
||||
Description: desc,
|
||||
Properties: props,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Handler returns a ToolHandler that executes tool calls via RPC using
|
||||
// the configured client. Tool names may be LLM-safe (underscored) or
|
||||
// original (dotted). WithTools uses this internally.
|
||||
func (t *Tools) Handler() ToolHandler {
|
||||
c := t.client
|
||||
if c == nil {
|
||||
c = client.DefaultClient
|
||||
}
|
||||
return func(ctx context.Context, call ToolCall) ToolResult {
|
||||
name := call.Name
|
||||
if orig, ok := t.names.get(name); ok {
|
||||
name = orig
|
||||
}
|
||||
parts := strings.SplitN(name, ".", 2)
|
||||
if len(parts) != 2 {
|
||||
return toolErrResult(call.ID, "invalid tool name: "+name)
|
||||
}
|
||||
|
||||
inputBytes, err := json.Marshal(call.Input)
|
||||
if err != nil {
|
||||
return toolErrResult(call.ID, "failed to marshal input: "+err.Error())
|
||||
}
|
||||
|
||||
req := c.NewRequest(parts[0], parts[1], &codecBytes.Frame{Data: inputBytes})
|
||||
var rsp codecBytes.Frame
|
||||
if err := c.Call(ctx, req, &rsp); err != nil {
|
||||
return toolErrResult(call.ID, err.Error())
|
||||
}
|
||||
|
||||
var result any
|
||||
if err := json.Unmarshal(rsp.Data, &result); err != nil {
|
||||
result = string(rsp.Data)
|
||||
}
|
||||
return ToolResult{ID: call.ID, Value: result, Content: string(rsp.Data)}
|
||||
}
|
||||
}
|
||||
|
||||
// DiscoverTools is a convenience that discovers tools from a registry
|
||||
// without creating a Tools instance. For paired discovery + execution,
|
||||
// create a Tools with NewTools instead.
|
||||
func DiscoverTools(reg registry.Registry) ([]Tool, error) {
|
||||
return NewTools(reg).Discover()
|
||||
}
|
||||
|
||||
func toolErrResult(id, msg string) ToolResult {
|
||||
encoded, _ := json.Marshal(map[string]string{"error": msg})
|
||||
return ToolResult{ID: id, Value: map[string]string{"error": msg}, Content: string(encoded)}
|
||||
}
|
||||
|
||||
func toolJSONType(goType string) string {
|
||||
switch goType {
|
||||
case "string":
|
||||
return "string"
|
||||
case "int", "int32", "int64", "uint", "uint32", "uint64":
|
||||
return "integer"
|
||||
case "float32", "float64":
|
||||
return "number"
|
||||
case "bool":
|
||||
return "boolean"
|
||||
default:
|
||||
return "object"
|
||||
}
|
||||
}
|
||||
@@ -1,197 +0,0 @@
|
||||
// Package tools turns go-micro services into ai.Tool definitions and
|
||||
// provides an ai.ToolHandler that executes tool calls by issuing RPCs
|
||||
// to the corresponding service.
|
||||
//
|
||||
// This is the building block that lets any go-micro service reason
|
||||
// about and call other services through an LLM:
|
||||
//
|
||||
// m := ai.New("anthropic",
|
||||
// ai.WithAPIKey(key),
|
||||
// ai.WithToolHandler(tools.Handler(service.Client())),
|
||||
// )
|
||||
// resp, _ := m.Generate(ctx, &ai.Request{
|
||||
// Prompt: userInput,
|
||||
// Tools: tools.FromRegistry(service.Registry()),
|
||||
// })
|
||||
package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"go-micro.dev/v5/ai"
|
||||
"go-micro.dev/v5/client"
|
||||
codecBytes "go-micro.dev/v5/codec/bytes"
|
||||
"go-micro.dev/v5/registry"
|
||||
)
|
||||
|
||||
// nameMap holds the mapping between LLM-safe tool names (no dots) and the
|
||||
// original `service.Endpoint` names used by the registry/client. Many
|
||||
// providers reject dots in tool names, so we substitute underscores when
|
||||
// presenting the tool and restore the original when executing.
|
||||
type nameMap struct {
|
||||
mu sync.RWMutex
|
||||
m map[string]string
|
||||
}
|
||||
|
||||
func (n *nameMap) put(safe, original string) {
|
||||
n.mu.Lock()
|
||||
n.m[safe] = original
|
||||
n.mu.Unlock()
|
||||
}
|
||||
|
||||
func (n *nameMap) get(safe string) (string, bool) {
|
||||
n.mu.RLock()
|
||||
v, ok := n.m[safe]
|
||||
n.mu.RUnlock()
|
||||
return v, ok
|
||||
}
|
||||
|
||||
// Set is the shared discovery state between FromRegistry and Handler.
|
||||
// Use New, then Discover + Handler together when you want the handler
|
||||
// to recognise LLM-safe tool names that were emitted by Discover.
|
||||
//
|
||||
// FromRegistry+Handler are convenience wrappers that create their own
|
||||
// internal Set; Set is exposed for callers that want to control the
|
||||
// lifecycle (e.g. cache the tool list and reuse it across turns).
|
||||
type Set struct {
|
||||
registry registry.Registry
|
||||
names *nameMap
|
||||
}
|
||||
|
||||
// New creates an empty Set bound to the given registry. Call Discover
|
||||
// to populate it. The registry is only used by Discover; Handler does
|
||||
// not need it.
|
||||
func New(reg registry.Registry) *Set {
|
||||
return &Set{
|
||||
registry: reg,
|
||||
names: &nameMap{m: map[string]string{}},
|
||||
}
|
||||
}
|
||||
|
||||
// Discover walks the registry and returns one ai.Tool per service
|
||||
// endpoint. The returned tools have LLM-safe names (dots replaced with
|
||||
// underscores); the Set remembers the mapping so Handler can route
|
||||
// calls back to the right service.
|
||||
func (s *Set) Discover() ([]ai.Tool, error) {
|
||||
services, err := s.registry.ListServices()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tools []ai.Tool
|
||||
for _, svc := range services {
|
||||
full, err := s.registry.GetService(svc.Name)
|
||||
if err != nil || len(full) == 0 {
|
||||
continue
|
||||
}
|
||||
for _, ep := range full[0].Endpoints {
|
||||
original := fmt.Sprintf("%s.%s", svc.Name, ep.Name)
|
||||
safe := strings.ReplaceAll(original, ".", "_")
|
||||
s.names.put(safe, original)
|
||||
|
||||
desc := fmt.Sprintf("Call %s on %s service", ep.Name, svc.Name)
|
||||
if ep.Metadata != nil {
|
||||
if d, ok := ep.Metadata["description"]; ok && d != "" {
|
||||
desc = d
|
||||
}
|
||||
}
|
||||
|
||||
props := map[string]any{}
|
||||
if ep.Request != nil {
|
||||
for _, field := range ep.Request.Values {
|
||||
props[field.Name] = map[string]any{
|
||||
"type": jsonType(field.Type),
|
||||
"description": fmt.Sprintf("%s (%s)", field.Name, field.Type),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tools = append(tools, ai.Tool{
|
||||
Name: safe,
|
||||
OriginalName: original,
|
||||
Description: desc,
|
||||
Properties: props,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return tools, nil
|
||||
}
|
||||
|
||||
// Handler returns an ai.ToolHandler that executes tool calls against
|
||||
// the given client. Tool names may be the LLM-safe form (with
|
||||
// underscores) emitted by Discover or the original dotted form; both
|
||||
// resolve to the same RPC.
|
||||
func (s *Set) Handler(c client.Client) ai.ToolHandler {
|
||||
if c == nil {
|
||||
c = client.DefaultClient
|
||||
}
|
||||
return func(name string, input map[string]any) (any, string) {
|
||||
if orig, ok := s.names.get(name); ok {
|
||||
name = orig
|
||||
}
|
||||
parts := strings.SplitN(name, ".", 2)
|
||||
if len(parts) != 2 {
|
||||
return errResult("invalid tool name: " + name)
|
||||
}
|
||||
|
||||
inputBytes, err := json.Marshal(input)
|
||||
if err != nil {
|
||||
return errResult("failed to marshal input: " + err.Error())
|
||||
}
|
||||
|
||||
req := c.NewRequest(parts[0], parts[1], &codecBytes.Frame{Data: inputBytes})
|
||||
var rsp codecBytes.Frame
|
||||
if err := c.Call(context.Background(), req, &rsp); err != nil {
|
||||
return errResult(err.Error())
|
||||
}
|
||||
|
||||
var result any
|
||||
if err := json.Unmarshal(rsp.Data, &result); err != nil {
|
||||
result = string(rsp.Data)
|
||||
}
|
||||
return result, string(rsp.Data)
|
||||
}
|
||||
}
|
||||
|
||||
// FromRegistry is a convenience that builds a one-shot Set, discovers
|
||||
// tools, and returns just the tool list. Use NewSet directly if you
|
||||
// need to also wire up Handler against the same name mapping.
|
||||
func FromRegistry(reg registry.Registry) ([]ai.Tool, error) {
|
||||
return New(reg).Discover()
|
||||
}
|
||||
|
||||
// Handler is a convenience that returns an ai.ToolHandler bound to the
|
||||
// given client. It only resolves dotted "service.Endpoint" names — it
|
||||
// has no awareness of any LLM-safe name mapping. For full round-tripping
|
||||
// of underscored names emitted by FromRegistry, construct a Set with
|
||||
// New and call Set.Handler.
|
||||
func Handler(c client.Client) ai.ToolHandler {
|
||||
return (&Set{names: &nameMap{m: map[string]string{}}}).Handler(c)
|
||||
}
|
||||
|
||||
func errResult(msg string) (any, string) {
|
||||
encoded, _ := json.Marshal(map[string]string{"error": msg})
|
||||
return map[string]string{"error": msg}, string(encoded)
|
||||
}
|
||||
|
||||
// jsonType maps Go types to JSON schema types. Anything that isn't a
|
||||
// recognised primitive becomes "object".
|
||||
func jsonType(goType string) string {
|
||||
switch goType {
|
||||
case "string":
|
||||
return "string"
|
||||
case "int", "int32", "int64", "uint", "uint32", "uint64":
|
||||
return "integer"
|
||||
case "float32", "float64":
|
||||
return "number"
|
||||
case "bool":
|
||||
return "boolean"
|
||||
default:
|
||||
return "object"
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,13 @@
|
||||
package tools
|
||||
package ai
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"go-micro.dev/v5/registry"
|
||||
"go-micro.dev/v6/registry"
|
||||
)
|
||||
|
||||
func TestJSONType(t *testing.T) {
|
||||
func TestToolJSONType(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"string": "string",
|
||||
"int": "integer",
|
||||
@@ -17,24 +18,24 @@ func TestJSONType(t *testing.T) {
|
||||
"": "object",
|
||||
}
|
||||
for in, want := range cases {
|
||||
if got := jsonType(in); got != want {
|
||||
t.Errorf("jsonType(%q) = %q, want %q", in, got, want)
|
||||
if got := toolJSONType(in); got != want {
|
||||
t.Errorf("toolJSONType(%q) = %q, want %q", in, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromRegistry_Empty(t *testing.T) {
|
||||
func TestDiscoverTools_Empty(t *testing.T) {
|
||||
reg := registry.NewMemoryRegistry()
|
||||
tools, err := FromRegistry(reg)
|
||||
tools, err := DiscoverTools(reg)
|
||||
if err != nil {
|
||||
t.Fatalf("FromRegistry: %v", err)
|
||||
t.Fatalf("DiscoverTools: %v", err)
|
||||
}
|
||||
if len(tools) != 0 {
|
||||
t.Errorf("expected 0 tools, got %d", len(tools))
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromRegistry_DiscoversEndpoints(t *testing.T) {
|
||||
func TestDiscoverTools_DiscoversEndpoints(t *testing.T) {
|
||||
reg := registry.NewMemoryRegistry()
|
||||
svc := ®istry.Service{
|
||||
Name: "users",
|
||||
@@ -63,9 +64,9 @@ func TestFromRegistry_DiscoversEndpoints(t *testing.T) {
|
||||
t.Fatalf("Register: %v", err)
|
||||
}
|
||||
|
||||
tools, err := FromRegistry(reg)
|
||||
tools, err := DiscoverTools(reg)
|
||||
if err != nil {
|
||||
t.Fatalf("FromRegistry: %v", err)
|
||||
t.Fatalf("DiscoverTools: %v", err)
|
||||
}
|
||||
if len(tools) != 1 {
|
||||
t.Fatalf("expected 1 tool, got %d", len(tools))
|
||||
@@ -73,7 +74,7 @@ func TestFromRegistry_DiscoversEndpoints(t *testing.T) {
|
||||
|
||||
tool := tools[0]
|
||||
if tool.Name != "users_Users_Get" {
|
||||
t.Errorf("safe name = %q, want users_Users_Get", tool.Name)
|
||||
t.Errorf("safe name = %q", tool.Name)
|
||||
}
|
||||
if tool.OriginalName != "users.Users.Get" {
|
||||
t.Errorf("original = %q", tool.OriginalName)
|
||||
@@ -81,43 +82,35 @@ func TestFromRegistry_DiscoversEndpoints(t *testing.T) {
|
||||
if tool.Description != "Fetch a user by ID" {
|
||||
t.Errorf("description = %q", tool.Description)
|
||||
}
|
||||
|
||||
id, ok := tool.Properties["id"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatal("missing id property")
|
||||
}
|
||||
if id["type"] != "string" {
|
||||
t.Errorf("id type = %v", id["type"])
|
||||
}
|
||||
expand, ok := tool.Properties["expand"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatal("missing expand property")
|
||||
}
|
||||
if expand["type"] != "boolean" {
|
||||
t.Errorf("expand type = %v", expand["type"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestSet_HandlerResolvesSafeName(t *testing.T) {
|
||||
s := New(registry.NewMemoryRegistry())
|
||||
s.names.put("users_Users_Get", "users.Users.Get")
|
||||
func TestTools_HandlerResolvesSafeName(t *testing.T) {
|
||||
tools := NewTools(registry.NewMemoryRegistry())
|
||||
tools.names.put("users_Users_Get", "users.Users.Get")
|
||||
|
||||
resolved, ok := s.names.get("users_Users_Get")
|
||||
resolved, ok := tools.names.get("users_Users_Get")
|
||||
if !ok || resolved != "users.Users.Get" {
|
||||
t.Errorf("name map lookup = (%q, %v)", resolved, ok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSet_HandlerInvalidName(t *testing.T) {
|
||||
s := New(registry.NewMemoryRegistry())
|
||||
h := s.Handler(nil)
|
||||
func TestTools_HandlerInvalidName(t *testing.T) {
|
||||
tools := NewTools(registry.NewMemoryRegistry())
|
||||
h := tools.Handler()
|
||||
|
||||
// "foo" has no dot, no mapping entry — should error cleanly.
|
||||
result, content := h("foo", map[string]any{})
|
||||
if result == nil {
|
||||
res := h(context.Background(), ToolCall{Name: "foo", Input: map[string]any{}})
|
||||
if res.Value == nil {
|
||||
t.Fatal("expected error result")
|
||||
}
|
||||
if content == "" {
|
||||
if res.Content == "" {
|
||||
t.Error("expected non-empty content")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithTools(t *testing.T) {
|
||||
tools := NewTools(registry.NewMemoryRegistry())
|
||||
opts := NewOptions(WithTools(tools))
|
||||
if opts.ToolHandler == nil {
|
||||
t.Error("WithTools did not set a ToolHandler")
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -4,9 +4,9 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
jwtToken "github.com/micro/plugins/v5/auth/jwt/token"
|
||||
"go-micro.dev/v5/auth"
|
||||
"go-micro.dev/v5/cmd"
|
||||
"go-micro.dev/v6/auth"
|
||||
jwtToken "go-micro.dev/v6/auth/jwt/token"
|
||||
"go-micro.dev/v6/cmd"
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -4,8 +4,8 @@ import (
|
||||
"encoding/base64"
|
||||
"time"
|
||||
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"go-micro.dev/v5/auth"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"go-micro.dev/v6/auth"
|
||||
)
|
||||
|
||||
// authClaims to be encoded in the JWT.
|
||||
@@ -14,7 +14,7 @@ type authClaims struct {
|
||||
Scopes []string `json:"scopes"`
|
||||
Metadata map[string]string `json:"metadata"`
|
||||
|
||||
jwt.StandardClaims
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
// JWT implementation of token provider.
|
||||
@@ -49,10 +49,10 @@ func (j *JWT) Generate(acc *auth.Account, opts ...GenerateOption) (*Token, error
|
||||
// generate the JWT
|
||||
expiry := time.Now().Add(options.Expiry)
|
||||
t := jwt.NewWithClaims(jwt.SigningMethodRS256, authClaims{
|
||||
acc.Type, acc.Scopes, acc.Metadata, jwt.StandardClaims{
|
||||
acc.Type, acc.Scopes, acc.Metadata, jwt.RegisteredClaims{
|
||||
Subject: acc.ID,
|
||||
Issuer: acc.Issuer,
|
||||
ExpiresAt: expiry.Unix(),
|
||||
ExpiresAt: jwt.NewNumericDate(expiry),
|
||||
},
|
||||
})
|
||||
tok, err := t.SignedString(key)
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go-micro.dev/v5/auth"
|
||||
"go-micro.dev/v6/auth"
|
||||
)
|
||||
|
||||
func TestGenerate(t *testing.T) {
|
||||
|
||||
@@ -3,7 +3,7 @@ package token
|
||||
import (
|
||||
"time"
|
||||
|
||||
"go-micro.dev/v5/store"
|
||||
"go-micro.dev/v6/store"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"go-micro.dev/v5/auth"
|
||||
"go-micro.dev/v6/auth"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@
|
||||
package noop
|
||||
|
||||
import (
|
||||
"go-micro.dev/v5/auth"
|
||||
"go-micro.dev/v6/auth"
|
||||
)
|
||||
|
||||
// NewAuth returns a new noop auth provider.
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go-micro.dev/v5/logger"
|
||||
"go-micro.dev/v6/logger"
|
||||
)
|
||||
|
||||
func NewOptions(opts ...Option) Options {
|
||||
|
||||
+16
-16
@@ -15,14 +15,14 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"go-micro.dev/v5/codec/json"
|
||||
merr "go-micro.dev/v5/errors"
|
||||
"go-micro.dev/v5/registry"
|
||||
"go-micro.dev/v5/registry/cache"
|
||||
"go-micro.dev/v5/transport/headers"
|
||||
maddr "go-micro.dev/v5/internal/util/addr"
|
||||
mnet "go-micro.dev/v5/internal/util/net"
|
||||
mls "go-micro.dev/v5/internal/util/tls"
|
||||
"go-micro.dev/v6/codec/json"
|
||||
merr "go-micro.dev/v6/errors"
|
||||
maddr "go-micro.dev/v6/internal/util/addr"
|
||||
mnet "go-micro.dev/v6/internal/util/net"
|
||||
mls "go-micro.dev/v6/internal/util/tls"
|
||||
"go-micro.dev/v6/registry"
|
||||
"go-micro.dev/v6/registry/cache"
|
||||
"go-micro.dev/v6/transport/headers"
|
||||
"golang.org/x/net/http2"
|
||||
)
|
||||
|
||||
@@ -100,7 +100,7 @@ func newTransport(config *tls.Config) *http.Transport {
|
||||
})
|
||||
|
||||
// setup http2
|
||||
http2.ConfigureTransport(t)
|
||||
_ = http2.ConfigureTransport(t)
|
||||
|
||||
return t
|
||||
}
|
||||
@@ -288,19 +288,19 @@ func (h *httpBroker) run(l net.Listener) {
|
||||
}
|
||||
|
||||
func (h *httpBroker) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
if req.Method != "POST" {
|
||||
if req.Method != http.MethodPost {
|
||||
err := merr.BadRequest("go.micro.broker", "Method not allowed")
|
||||
http.Error(w, err.Error(), http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
defer req.Body.Close()
|
||||
|
||||
req.ParseForm()
|
||||
_ = req.ParseForm()
|
||||
|
||||
b, err := io.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
errr := merr.InternalServerError("go.micro.broker", "Error reading request body: %v", err)
|
||||
w.WriteHeader(500)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(errr.Error()))
|
||||
return
|
||||
}
|
||||
@@ -308,7 +308,7 @@ func (h *httpBroker) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
var m *Message
|
||||
if err = h.opts.Codec.Unmarshal(b, &m); err != nil {
|
||||
errr := merr.InternalServerError("go.micro.broker", "Error parsing request body: %v", err)
|
||||
w.WriteHeader(500)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(errr.Error()))
|
||||
return
|
||||
}
|
||||
@@ -318,7 +318,7 @@ func (h *httpBroker) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
if len(topic) == 0 {
|
||||
errr := merr.InternalServerError("go.micro.broker", "Topic not found")
|
||||
w.WriteHeader(500)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(errr.Error()))
|
||||
return
|
||||
}
|
||||
@@ -406,7 +406,7 @@ func (h *httpBroker) Connect() error {
|
||||
addr := h.address
|
||||
h.address = l.Addr().String()
|
||||
|
||||
go http.Serve(l, h.mux)
|
||||
go func() { _ = http.Serve(l, h.mux) }()
|
||||
go func() {
|
||||
h.run(l)
|
||||
h.Lock()
|
||||
@@ -555,7 +555,7 @@ func (h *httpBroker) Publish(topic string, msg *Message, opts ...PublishOption)
|
||||
}
|
||||
|
||||
// discard response body
|
||||
io.Copy(io.Discard, r.Body)
|
||||
_, _ = io.Copy(io.Discard, r.Body)
|
||||
r.Body.Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
+5
-3
@@ -6,8 +6,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"go-micro.dev/v5/broker"
|
||||
"go-micro.dev/v5/registry"
|
||||
"go-micro.dev/v6/broker"
|
||||
"go-micro.dev/v6/registry"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -161,7 +161,9 @@ func pub(b *testing.B, c int) {
|
||||
go func() {
|
||||
for range ch {
|
||||
if err := brk.Publish(topic, msg); err != nil {
|
||||
b.Fatalf("Unexpected publish error: %v", err)
|
||||
b.Errorf("Unexpected publish error: %v", err)
|
||||
wg.Done()
|
||||
return
|
||||
}
|
||||
select {
|
||||
case <-done:
|
||||
|
||||
+4
-4
@@ -7,9 +7,9 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/google/uuid"
|
||||
log "go-micro.dev/v5/logger"
|
||||
maddr "go-micro.dev/v5/internal/util/addr"
|
||||
mnet "go-micro.dev/v5/internal/util/net"
|
||||
maddr "go-micro.dev/v6/internal/util/addr"
|
||||
mnet "go-micro.dev/v6/internal/util/net"
|
||||
log "go-micro.dev/v6/logger"
|
||||
)
|
||||
|
||||
type memoryBroker struct {
|
||||
@@ -122,7 +122,7 @@ func (m *memoryBroker) Publish(topic string, msg *Message, opts ...PublishOption
|
||||
if err := sub.handler(p); err != nil {
|
||||
p.err = err
|
||||
if eh := m.opts.ErrorHandler; eh != nil {
|
||||
eh(p)
|
||||
_ = eh(p)
|
||||
continue
|
||||
}
|
||||
return err
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"go-micro.dev/v5/broker"
|
||||
"go-micro.dev/v6/broker"
|
||||
)
|
||||
|
||||
func TestMemoryBroker(t *testing.T) {
|
||||
|
||||
@@ -3,7 +3,7 @@ package nats
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go-micro.dev/v5/broker"
|
||||
"go-micro.dev/v6/broker"
|
||||
)
|
||||
|
||||
// setBrokerOption returns a function to setup a context with given value.
|
||||
|
||||
+12
-12
@@ -9,10 +9,10 @@ import (
|
||||
"time"
|
||||
|
||||
natsp "github.com/nats-io/nats.go"
|
||||
"go-micro.dev/v5/broker"
|
||||
"go-micro.dev/v5/codec/json"
|
||||
"go-micro.dev/v5/logger"
|
||||
"go-micro.dev/v5/registry"
|
||||
"go-micro.dev/v6/broker"
|
||||
"go-micro.dev/v6/codec/json"
|
||||
"go-micro.dev/v6/logger"
|
||||
"go-micro.dev/v6/registry"
|
||||
)
|
||||
|
||||
type natsBroker struct {
|
||||
@@ -194,7 +194,7 @@ func (n *natsBroker) Disconnect() error {
|
||||
if n.conn != nil {
|
||||
// drain the connection if specified
|
||||
if n.drain {
|
||||
n.conn.Drain()
|
||||
_ = n.conn.Drain()
|
||||
n.closeCh <- nil
|
||||
}
|
||||
|
||||
@@ -233,7 +233,7 @@ func (n *natsBroker) Publish(topic string, msg *broker.Message, opts ...broker.P
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer n.pool.Put(poolConn)
|
||||
defer func() { _ = n.pool.Put(poolConn) }()
|
||||
|
||||
conn := poolConn.Conn()
|
||||
if conn == nil {
|
||||
@@ -279,7 +279,7 @@ func (n *natsBroker) Subscribe(topic string, handler broker.Handler, opts ...bro
|
||||
m.Body = msg.Data
|
||||
n.opts.Logger.Log(logger.ErrorLevel, err)
|
||||
if eh != nil {
|
||||
eh(pub)
|
||||
_ = eh(pub)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -287,7 +287,7 @@ func (n *natsBroker) Subscribe(topic string, handler broker.Handler, opts ...bro
|
||||
pub.err = err
|
||||
n.opts.Logger.Log(logger.ErrorLevel, err)
|
||||
if eh != nil {
|
||||
eh(pub)
|
||||
_ = eh(pub)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -304,7 +304,7 @@ func (n *natsBroker) Subscribe(topic string, handler broker.Handler, opts ...bro
|
||||
|
||||
conn := poolConn.Conn()
|
||||
if conn == nil {
|
||||
n.pool.Put(poolConn)
|
||||
_ = n.pool.Put(poolConn)
|
||||
return nil, errors.New("invalid connection from pool")
|
||||
}
|
||||
|
||||
@@ -315,13 +315,13 @@ func (n *natsBroker) Subscribe(topic string, handler broker.Handler, opts ...bro
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
n.pool.Put(poolConn)
|
||||
_ = n.pool.Put(poolConn)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Return connection to pool after subscription is created
|
||||
// The subscription keeps the connection alive
|
||||
n.pool.Put(poolConn)
|
||||
_ = n.pool.Put(poolConn)
|
||||
|
||||
return &subscriber{s: sub, opts: opt}, nil
|
||||
}
|
||||
@@ -349,7 +349,7 @@ func (n *natsBroker) setOption(opts ...broker.Option) {
|
||||
o(&n.opts)
|
||||
}
|
||||
|
||||
n.Once.Do(func() {
|
||||
n.Do(func() {
|
||||
n.nopts = natsp.GetDefaultOptions()
|
||||
n.poolSize = 1 // Default to single connection (no pooling)
|
||||
n.poolIdleTimeout = 5 * time.Minute
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"testing"
|
||||
|
||||
natsp "github.com/nats-io/nats.go"
|
||||
"go-micro.dev/v5/broker"
|
||||
"go-micro.dev/v6/broker"
|
||||
)
|
||||
|
||||
var addrTestCases = []struct {
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"time"
|
||||
|
||||
natsp "github.com/nats-io/nats.go"
|
||||
"go-micro.dev/v5/broker"
|
||||
"go-micro.dev/v6/broker"
|
||||
)
|
||||
|
||||
type optionsKey struct{}
|
||||
|
||||
+3
-3
@@ -4,9 +4,9 @@ import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
|
||||
"go-micro.dev/v5/codec"
|
||||
"go-micro.dev/v5/logger"
|
||||
"go-micro.dev/v5/registry"
|
||||
"go-micro.dev/v6/codec"
|
||||
"go-micro.dev/v6/logger"
|
||||
"go-micro.dev/v6/registry"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
|
||||
@@ -61,14 +61,14 @@ func (r *rabbitMQChannel) Connect(prefetchCount int, prefetchGlobal bool, confir
|
||||
|
||||
func (r *rabbitMQChannel) Close() error {
|
||||
if r.channel == nil {
|
||||
return errors.New("Channel is nil")
|
||||
return errors.New("channel is nil")
|
||||
}
|
||||
return r.channel.Close()
|
||||
}
|
||||
|
||||
func (r *rabbitMQChannel) Publish(exchange, key string, message amqp.Publishing) error {
|
||||
if r.channel == nil {
|
||||
return errors.New("Channel is nil")
|
||||
return errors.New("channel is nil")
|
||||
}
|
||||
|
||||
if r.confirmPublish != nil {
|
||||
@@ -84,11 +84,11 @@ func (r *rabbitMQChannel) Publish(exchange, key string, message amqp.Publishing)
|
||||
if r.confirmPublish != nil {
|
||||
confirmation, ok := <-r.confirmPublish
|
||||
if !ok {
|
||||
return errors.New("Channel closed before could receive confirmation of publish")
|
||||
return errors.New("channel closed before could receive confirmation of publish")
|
||||
}
|
||||
|
||||
if !confirmation.Ack {
|
||||
return errors.New("Could not publish message, received nack from broker on confirmation")
|
||||
return errors.New("could not publish message, received nack from broker on confirmation")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,16 +11,16 @@ import (
|
||||
"time"
|
||||
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
"go-micro.dev/v5/logger"
|
||||
mtls "go-micro.dev/v5/internal/util/tls"
|
||||
mtls "go-micro.dev/v6/internal/util/tls"
|
||||
"go-micro.dev/v6/logger"
|
||||
)
|
||||
|
||||
type MQExchangeType string
|
||||
|
||||
const (
|
||||
ExchangeTypeFanout MQExchangeType = "fanout"
|
||||
ExchangeTypeTopic = "topic"
|
||||
ExchangeTypeDirect = "direct"
|
||||
ExchangeTypeTopic MQExchangeType = "topic"
|
||||
ExchangeTypeDirect MQExchangeType = "direct"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -46,8 +46,6 @@ var (
|
||||
Locale: defaultLocale,
|
||||
}
|
||||
|
||||
dial = amqp.Dial
|
||||
dialTLS = amqp.DialTLS
|
||||
dialConfig = amqp.DialConfig
|
||||
)
|
||||
|
||||
@@ -251,9 +249,9 @@ func (r *rabbitMQConn) tryConnect(secure bool, config *amqp.Config) error {
|
||||
|
||||
if !r.withoutExchange {
|
||||
if r.exchange.Durable {
|
||||
r.Channel.DeclareDurableExchange(r.exchange)
|
||||
_ = r.Channel.DeclareDurableExchange(r.exchange)
|
||||
} else {
|
||||
r.Channel.DeclareExchange(r.exchange)
|
||||
_ = r.Channel.DeclareExchange(r.exchange)
|
||||
}
|
||||
r.ExchangeChannel, err = newRabbitChannel(r.Connection, r.prefetchCount, r.prefetchGlobal, r.confirmPublish)
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"testing"
|
||||
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
"go-micro.dev/v5/logger"
|
||||
"go-micro.dev/v6/logger"
|
||||
)
|
||||
|
||||
func TestNewRabbitMQConnURL(t *testing.T) {
|
||||
|
||||
@@ -3,8 +3,8 @@ package rabbitmq
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go-micro.dev/v5/broker"
|
||||
"go-micro.dev/v5/server"
|
||||
"go-micro.dev/v6/broker"
|
||||
"go-micro.dev/v6/server"
|
||||
)
|
||||
|
||||
// setSubscribeOption returns a function to setup a context with given value.
|
||||
|
||||
@@ -4,9 +4,9 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go-micro.dev/v5/broker"
|
||||
"go-micro.dev/v5/client"
|
||||
"go-micro.dev/v5/server"
|
||||
"go-micro.dev/v6/broker"
|
||||
"go-micro.dev/v6/client"
|
||||
"go-micro.dev/v6/server"
|
||||
)
|
||||
|
||||
type durableQueueKey struct{}
|
||||
|
||||
@@ -10,18 +10,16 @@ import (
|
||||
"time"
|
||||
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
"go-micro.dev/v5/broker"
|
||||
"go-micro.dev/v5/logger"
|
||||
"go-micro.dev/v6/broker"
|
||||
"go-micro.dev/v6/logger"
|
||||
)
|
||||
|
||||
type rbroker struct {
|
||||
conn *rabbitMQConn
|
||||
addrs []string
|
||||
opts broker.Options
|
||||
prefetchCount int
|
||||
prefetchGlobal bool
|
||||
mtx sync.Mutex
|
||||
wg sync.WaitGroup
|
||||
conn *rabbitMQConn
|
||||
addrs []string
|
||||
opts broker.Options
|
||||
mtx sync.Mutex
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
type subscriber struct {
|
||||
@@ -304,9 +302,9 @@ func (r *rbroker) Subscribe(topic string, handler broker.Handler, opts ...broker
|
||||
p := &publication{d: msg, m: m, t: msg.RoutingKey}
|
||||
p.err = handler(p)
|
||||
if p.err == nil && ackSuccess && !opt.AutoAck {
|
||||
msg.Ack(false)
|
||||
_ = msg.Ack(false)
|
||||
} else if p.err != nil && !opt.AutoAck {
|
||||
msg.Nack(false, requeueOnError)
|
||||
_ = msg.Nack(false, requeueOnError)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,12 +7,12 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go-micro.dev/v5/logger"
|
||||
"go-micro.dev/v6/logger"
|
||||
|
||||
micro "go-micro.dev/v5"
|
||||
broker "go-micro.dev/v5/broker"
|
||||
rabbitmq "go-micro.dev/v5/broker/rabbitmq"
|
||||
server "go-micro.dev/v5/server"
|
||||
micro "go-micro.dev/v6"
|
||||
broker "go-micro.dev/v6/broker"
|
||||
rabbitmq "go-micro.dev/v6/broker/rabbitmq"
|
||||
server "go-micro.dev/v6/server"
|
||||
)
|
||||
|
||||
type Example struct{}
|
||||
@@ -50,8 +50,7 @@ func TestDurable(t *testing.T) {
|
||||
|
||||
s := server.NewServer(server.Broker(b))
|
||||
|
||||
service := micro.NewService(
|
||||
micro.Server(s),
|
||||
service := micro.NewService("test", micro.Server(s),
|
||||
micro.Broker(b),
|
||||
)
|
||||
h := &Example{}
|
||||
@@ -82,8 +81,7 @@ func TestWithoutExchange(t *testing.T) {
|
||||
|
||||
s := server.NewServer(server.Broker(b))
|
||||
|
||||
service := micro.NewService(
|
||||
micro.Server(s),
|
||||
service := micro.NewService("test", micro.Server(s),
|
||||
micro.Broker(b),
|
||||
)
|
||||
brkrSub := broker.NewSubscribeOptions(
|
||||
@@ -119,7 +117,7 @@ func TestWithoutExchange(t *testing.T) {
|
||||
rabbitmq.DeliveryMode(2),
|
||||
rabbitmq.ContentType("application/json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
t.Errorf("%v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -140,8 +138,7 @@ func TestFanoutExchange(t *testing.T) {
|
||||
|
||||
s := server.NewServer(server.Broker(b))
|
||||
|
||||
service := micro.NewService(
|
||||
micro.Server(s),
|
||||
service := micro.NewService("test", micro.Server(s),
|
||||
micro.Broker(b),
|
||||
)
|
||||
brkrSub := broker.NewSubscribeOptions(
|
||||
@@ -177,7 +174,7 @@ func TestFanoutExchange(t *testing.T) {
|
||||
rabbitmq.DeliveryMode(2),
|
||||
rabbitmq.ContentType("application/json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
t.Errorf("%v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -198,8 +195,7 @@ func TestDirectExchange(t *testing.T) {
|
||||
|
||||
s := server.NewServer(server.Broker(b))
|
||||
|
||||
service := micro.NewService(
|
||||
micro.Server(s),
|
||||
service := micro.NewService("test", micro.Server(s),
|
||||
micro.Broker(b),
|
||||
)
|
||||
brkrSub := broker.NewSubscribeOptions(
|
||||
@@ -235,7 +231,7 @@ func TestDirectExchange(t *testing.T) {
|
||||
rabbitmq.DeliveryMode(2),
|
||||
rabbitmq.ContentType("application/json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
t.Errorf("%v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -256,8 +252,7 @@ func TestTopicExchange(t *testing.T) {
|
||||
|
||||
s := server.NewServer(server.Broker(b))
|
||||
|
||||
service := micro.NewService(
|
||||
micro.Server(s),
|
||||
service := micro.NewService("test", micro.Server(s),
|
||||
micro.Broker(b),
|
||||
)
|
||||
brkrSub := broker.NewSubscribeOptions(
|
||||
@@ -293,7 +288,7 @@ func TestTopicExchange(t *testing.T) {
|
||||
rabbitmq.DeliveryMode(2),
|
||||
rabbitmq.ContentType("application/json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
t.Errorf("%v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
|
||||
Vendored
+7
-7
@@ -14,8 +14,8 @@ type memCache struct {
|
||||
}
|
||||
|
||||
func (c *memCache) Get(ctx context.Context, key string) (interface{}, time.Time, error) {
|
||||
c.RWMutex.RLock()
|
||||
defer c.RWMutex.RUnlock()
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
item, found := c.items[key]
|
||||
if !found {
|
||||
@@ -37,8 +37,8 @@ func (c *memCache) Put(ctx context.Context, key string, val interface{}, d time.
|
||||
e = time.Now().Add(d).UnixNano()
|
||||
}
|
||||
|
||||
c.RWMutex.Lock()
|
||||
defer c.RWMutex.Unlock()
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
c.items[key] = Item{
|
||||
Value: val,
|
||||
@@ -49,8 +49,8 @@ func (c *memCache) Put(ctx context.Context, key string, val interface{}, d time.
|
||||
}
|
||||
|
||||
func (c *memCache) Delete(ctx context.Context, key string) error {
|
||||
c.RWMutex.Lock()
|
||||
defer c.RWMutex.Unlock()
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
_, found := c.items[key]
|
||||
if !found {
|
||||
@@ -61,6 +61,6 @@ func (c *memCache) Delete(ctx context.Context, key string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *memCache) String() string {
|
||||
func (c *memCache) String() string {
|
||||
return "memory"
|
||||
}
|
||||
|
||||
Vendored
+1
-1
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go-micro.dev/v5/logger"
|
||||
"go-micro.dev/v6/logger"
|
||||
)
|
||||
|
||||
// Options represents the options for the cache.
|
||||
|
||||
Vendored
+1
-1
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
|
||||
rclient "github.com/go-redis/redis/v8"
|
||||
"go-micro.dev/v5/cache"
|
||||
"go-micro.dev/v6/cache"
|
||||
)
|
||||
|
||||
type redisOptionsContextKey struct{}
|
||||
|
||||
Vendored
+1
-1
@@ -6,7 +6,7 @@ import (
|
||||
"testing"
|
||||
|
||||
rclient "github.com/go-redis/redis/v8"
|
||||
"go-micro.dev/v5/cache"
|
||||
"go-micro.dev/v6/cache"
|
||||
)
|
||||
|
||||
func Test_newUniversalClient(t *testing.T) {
|
||||
|
||||
Vendored
+2
-2
@@ -5,7 +5,7 @@ import (
|
||||
"time"
|
||||
|
||||
rclient "github.com/go-redis/redis/v8"
|
||||
"go-micro.dev/v5/cache"
|
||||
"go-micro.dev/v6/cache"
|
||||
)
|
||||
|
||||
// NewRedisCache returns a new redis cache.
|
||||
@@ -52,6 +52,6 @@ func (c *redisCache) Delete(ctx context.Context, key string) error {
|
||||
return c.client.Del(ctx, key).Err()
|
||||
}
|
||||
|
||||
func (m *redisCache) String() string {
|
||||
func (c *redisCache) String() string {
|
||||
return "redis"
|
||||
}
|
||||
|
||||
Vendored
+1
-1
@@ -6,7 +6,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go-micro.dev/v5/cache"
|
||||
"go-micro.dev/v6/cache"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go-micro.dev/v5/internal/util/backoff"
|
||||
"go-micro.dev/v6/internal/util/backoff"
|
||||
)
|
||||
|
||||
type BackoffFunc func(ctx context.Context, req Request, attempts int) (time.Duration, error)
|
||||
|
||||
+2
-2
@@ -9,8 +9,8 @@ import (
|
||||
|
||||
cache "github.com/patrickmn/go-cache"
|
||||
|
||||
"go-micro.dev/v5/metadata"
|
||||
"go-micro.dev/v5/transport/headers"
|
||||
"go-micro.dev/v6/metadata"
|
||||
"go-micro.dev/v6/transport/headers"
|
||||
)
|
||||
|
||||
// NewCache returns an initialized cache.
|
||||
|
||||
@@ -5,8 +5,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go-micro.dev/v5/metadata"
|
||||
"go-micro.dev/v5/transport/headers"
|
||||
"go-micro.dev/v6/metadata"
|
||||
"go-micro.dev/v6/transport/headers"
|
||||
)
|
||||
|
||||
func TestCache(t *testing.T) {
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ package client
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go-micro.dev/v5/codec"
|
||||
"go-micro.dev/v6/codec"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"go-micro.dev/v5/registry"
|
||||
"go-micro.dev/v6/registry"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"go-micro.dev/v5/codec"
|
||||
"go-micro.dev/v5/codec/bytes"
|
||||
"go-micro.dev/v6/codec"
|
||||
"go-micro.dev/v6/codec/bytes"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/encoding"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
@@ -42,7 +42,7 @@ func UseNumber() {
|
||||
}
|
||||
|
||||
func (w wrapCodec) String() string {
|
||||
return w.Codec.Name()
|
||||
return w.Name()
|
||||
}
|
||||
|
||||
func (w wrapCodec) Marshal(v interface{}) ([]byte, error) {
|
||||
|
||||
@@ -3,7 +3,7 @@ package grpc
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"go-micro.dev/v5/errors"
|
||||
"go-micro.dev/v6/errors"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
+9
-9
@@ -11,15 +11,15 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"go-micro.dev/v5/broker"
|
||||
"go-micro.dev/v5/client"
|
||||
"go-micro.dev/v5/cmd"
|
||||
raw "go-micro.dev/v5/codec/bytes"
|
||||
"go-micro.dev/v5/errors"
|
||||
"go-micro.dev/v5/metadata"
|
||||
"go-micro.dev/v5/registry"
|
||||
"go-micro.dev/v5/selector"
|
||||
pnet "go-micro.dev/v5/internal/util/net"
|
||||
"go-micro.dev/v6/broker"
|
||||
"go-micro.dev/v6/client"
|
||||
"go-micro.dev/v6/cmd"
|
||||
raw "go-micro.dev/v6/codec/bytes"
|
||||
"go-micro.dev/v6/errors"
|
||||
pnet "go-micro.dev/v6/internal/util/net"
|
||||
"go-micro.dev/v6/metadata"
|
||||
"go-micro.dev/v6/registry"
|
||||
"go-micro.dev/v6/selector"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials"
|
||||
"google.golang.org/grpc/encoding"
|
||||
|
||||
@@ -202,7 +202,6 @@ func removeConn(conn *poolConn) {
|
||||
conn.next = nil
|
||||
conn.in = false
|
||||
conn.sp.count--
|
||||
return
|
||||
}
|
||||
|
||||
func addConnAfter(conn *poolConn, after *poolConn) {
|
||||
@@ -214,5 +213,4 @@ func addConnAfter(conn *poolConn, after *poolConn) {
|
||||
after.next = conn
|
||||
conn.in = true
|
||||
conn.sp.count++
|
||||
return
|
||||
}
|
||||
|
||||
@@ -5,10 +5,10 @@ import (
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"go-micro.dev/v5/client"
|
||||
"go-micro.dev/v5/errors"
|
||||
"go-micro.dev/v5/registry"
|
||||
"go-micro.dev/v5/selector"
|
||||
"go-micro.dev/v6/client"
|
||||
"go-micro.dev/v6/errors"
|
||||
"go-micro.dev/v6/registry"
|
||||
"go-micro.dev/v6/selector"
|
||||
pgrpc "google.golang.org/grpc"
|
||||
pb "google.golang.org/grpc/examples/helloworld/helloworld"
|
||||
)
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user