-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add registry broker delegation client #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Registry Broker Delegation | ||
|
|
||
| This example calls the broker-native delegation planner and prints the ranked | ||
| opportunities plus the top candidate per opportunity. | ||
|
|
||
| Optional environment variables: | ||
|
|
||
| ```bash | ||
| export REGISTRY_BROKER_BASE_URL="https://hol.org/registry/api/v1" | ||
| export REGISTRY_BROKER_API_KEY="..." | ||
| export REGISTRY_BROKER_DELEGATION_TASK="Review an SDK PR and split out docs and verification subtasks." | ||
| export REGISTRY_BROKER_DELEGATION_CONTEXT="Need a docs-focused sidecar pass." | ||
| export REGISTRY_BROKER_DELEGATION_LIMIT="3" | ||
| ``` | ||
|
|
||
| ## Run | ||
|
|
||
| ```bash | ||
| go run ./examples/registry-broker-delegation | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/hashgraph-online/standards-sdk-go/pkg/registrybroker" | ||
| ) | ||
|
|
||
| const ( | ||
| defaultBaseURL = "https://hol.org/registry/api/v1" | ||
| defaultTask = "Review an SDK PR and split out docs and verification subtasks." | ||
| ) | ||
|
|
||
| func main() { | ||
| if err := run(); err != nil { | ||
| _, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| } | ||
|
|
||
| func run() error { | ||
| client, err := registrybroker.NewRegistryBrokerClient(registrybroker.RegistryBrokerClientOptions{ | ||
| BaseURL: strings.TrimSpace(getEnvOrDefault("REGISTRY_BROKER_BASE_URL", defaultBaseURL)), | ||
| APIKey: strings.TrimSpace(os.Getenv("REGISTRY_BROKER_API_KEY")), | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| limit, err := strconv.Atoi(strings.TrimSpace(getEnvOrDefault("REGISTRY_BROKER_DELEGATION_LIMIT", "3"))) | ||
| if err != nil || limit <= 0 { | ||
| limit = 3 | ||
| } | ||
|
|
||
| response, err := client.Delegate(context.Background(), registrybroker.DelegationPlanRequest{ | ||
| Task: strings.TrimSpace(getEnvOrDefault("REGISTRY_BROKER_DELEGATION_TASK", defaultTask)), | ||
| Context: strings.TrimSpace(os.Getenv("REGISTRY_BROKER_DELEGATION_CONTEXT")), | ||
| Limit: limit, | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| fmt.Printf("task=%s\n", response.Task) | ||
| fmt.Printf("shouldDelegate=%t\n", response.ShouldDelegate) | ||
| if response.LocalFirstReason != "" { | ||
| fmt.Printf("localFirstReason=%s\n", response.LocalFirstReason) | ||
| } | ||
| for index := range response.Opportunities { | ||
| opportunity := &response.Opportunities[index] | ||
| fmt.Printf("\nopportunity=%s title=%s\n", opportunity.ID, opportunity.Title) | ||
| fmt.Printf("reason=%s\n", opportunity.Reason) | ||
| if len(opportunity.Candidates) == 0 { | ||
| fmt.Println("topCandidate=<none>") | ||
| continue | ||
| } | ||
| candidate := opportunity.Candidates[0] | ||
| label := candidate.Label | ||
| if label == "" { | ||
| label = "<unknown>" | ||
| } | ||
| fmt.Printf("topCandidate=%s label=%s\n", candidate.UAID, label) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func getEnvOrDefault(key, fallback string) string { | ||
| value := strings.TrimSpace(os.Getenv(key)) | ||
| if value == "" { | ||
| return fallback | ||
| } | ||
| return value | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package main | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestGetEnvOrDefault(t *testing.T) { | ||
| t.Run("returns fallback when env var is not set", func(t *testing.T) { | ||
| if value := getEnvOrDefault("REGISTRY_BROKER_DELEGATION_EXAMPLE_MISSING", "fallback"); value != "fallback" { | ||
| t.Fatalf("expected fallback, got %s", value) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("returns env var value when set", func(t *testing.T) { | ||
| t.Setenv("REGISTRY_BROKER_DELEGATION_EXAMPLE_SET", "env-value") | ||
|
|
||
| if value := getEnvOrDefault("REGISTRY_BROKER_DELEGATION_EXAMPLE_SET", "fallback"); value != "env-value" { | ||
| t.Fatalf("expected env-value, got %s", value) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("returns trimmed env var value when set with whitespace", func(t *testing.T) { | ||
| t.Setenv("REGISTRY_BROKER_DELEGATION_EXAMPLE_SPACED", " env-value ") | ||
|
|
||
| if value := getEnvOrDefault("REGISTRY_BROKER_DELEGATION_EXAMPLE_SPACED", "fallback"); value != "env-value" { | ||
| t.Fatalf("expected env-value, got %s", value) | ||
| } | ||
| }) | ||
| } | ||
|
kantorcodes marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| package registrybroker | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
| ) | ||
|
|
||
| const preservedValue = "preserved" | ||
|
|
||
| func TestDelegate(t *testing.T) { | ||
| t.Helper() | ||
|
|
||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| if r.Method != http.MethodPost { | ||
| t.Fatalf("expected POST, got %s", r.Method) | ||
| } | ||
| if r.URL.Path != "/api/v1/delegate" { | ||
| t.Fatalf("expected /api/v1/delegate, got %s", r.URL.Path) | ||
| } | ||
|
|
||
| var request DelegationPlanRequest | ||
| if err := json.NewDecoder(r.Body).Decode(&request); err != nil { | ||
| t.Fatalf("decode request: %v", err) | ||
| } | ||
| if request.Task != "Review SDK PR feedback" { | ||
| t.Fatalf("expected task to round-trip, got %q", request.Task) | ||
| } | ||
| if request.Filter == nil || len(request.Filter.Protocols) != 1 || request.Filter.Protocols[0] != "mcp" { | ||
| t.Fatalf("expected protocols filter to round-trip, got %#v", request.Filter) | ||
| } | ||
|
|
||
| w.Header().Set("Content-Type", "application/json") | ||
| _, _ = fmt.Fprintf(w, `{ | ||
| "task": "Review SDK PR feedback", | ||
| "context": "Need a docs-focused sidecar pass.", | ||
| "summary": "Delegate documentation follow-up.", | ||
| "shouldDelegate": true, | ||
| "localFirstReason": "Main agent owns the implementation work.", | ||
| "recommendation": { "summary": "Delegate docs only", "mode": "parallel" }, | ||
| "opportunities": [ | ||
| { | ||
| "id": "docs", | ||
| "title": "Docs follow-up", | ||
| "reason": "Bounded copy update", | ||
| "role": "docs", | ||
| "type": "sidecar", | ||
| "suggestedMode": "parallel", | ||
| "searchQueries": ["docs markdown docusaurus"], | ||
| "extraOpportunityField": "%s", | ||
| "candidates": [ | ||
| { | ||
| "uaid": "uaid-1", | ||
| "label": "Docs Agent", | ||
| "registry": "hcs-11", | ||
| "score": 0.98, | ||
| "trustScore": 0.91, | ||
| "verified": true, | ||
| "communicationSupported": true, | ||
| "availability": "online", | ||
| "explanation": "Strong docs match", | ||
| "matchedRoles": ["docs"], | ||
| "reasons": ["Strong docs match"], | ||
| "suggestedMessage": "Update the docs tab set.", | ||
| "extraCandidateField": "%s", | ||
| "agent": { | ||
| "name": "Docs Agent", | ||
| "verified": true, | ||
| "extraAgentField": "%s" | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| ], | ||
| "extraRootField": "%s" | ||
| }`, preservedValue, preservedValue, preservedValue, preservedValue) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| client, err := NewRegistryBrokerClient(RegistryBrokerClientOptions{BaseURL: server.URL}) | ||
| if err != nil { | ||
| t.Fatalf("new client: %v", err) | ||
| } | ||
|
|
||
| response, err := client.Delegate(context.Background(), DelegationPlanRequest{ | ||
| Task: "Review SDK PR feedback", | ||
| Context: "Need a docs-focused sidecar pass.", | ||
| Limit: 2, | ||
| Filter: &DelegationPlanFilter{ | ||
| Protocols: []string{"mcp"}, | ||
| Type: "mcp_server", | ||
| }, | ||
| Workspace: JSONObject{"repo": "hashgraph-online/standards-sdk"}, | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("delegate: %v", err) | ||
| } | ||
|
|
||
| if !response.ShouldDelegate { | ||
| t.Fatal("expected delegation recommendation") | ||
| } | ||
| if response.Context != "Need a docs-focused sidecar pass." { | ||
| t.Fatalf("expected context to parse, got %q", response.Context) | ||
| } | ||
| if len(response.Opportunities) != 1 { | ||
| t.Fatalf("expected one opportunity, got %d", len(response.Opportunities)) | ||
| } | ||
| if response.Extras["extraRootField"] != preservedValue { | ||
| t.Fatalf("expected additive root field to survive, got %#v", response.Extras) | ||
| } | ||
| opportunity := response.Opportunities[0] | ||
| if opportunity.Extras["extraOpportunityField"] != preservedValue { | ||
| t.Fatalf("expected additive opportunity field to survive, got %#v", opportunity.Extras) | ||
| } | ||
| candidate := opportunity.Candidates[0] | ||
| if candidate.Registry != "hcs-11" { | ||
| t.Fatalf("expected registry to parse, got %q", candidate.Registry) | ||
| } | ||
| if candidate.TrustScore != 0.91 { | ||
| t.Fatalf("expected trust score 0.91, got %f", candidate.TrustScore) | ||
| } | ||
| if candidate.Verified == nil || !*candidate.Verified { | ||
| t.Fatal("expected verified candidate") | ||
| } | ||
| if candidate.Extras["extraCandidateField"] != preservedValue { | ||
| t.Fatalf("expected additive candidate field to survive, got %#v", candidate.Extras) | ||
| } | ||
| if candidate.Agent["extraAgentField"] != preservedValue { | ||
| t.Fatalf("expected additive agent field to survive, got %#v", candidate.Agent) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.