Skip to content

Commit 0bba994

Browse files
committed
refactor
1 parent 0272867 commit 0bba994

File tree

9 files changed

+236
-306
lines changed

9 files changed

+236
-306
lines changed

go/ai/resource.go

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,69 @@ import (
2121
"encoding/json"
2222
"fmt"
2323
"maps"
24+
"net/url"
25+
"strings"
2426

2527
"github.com/firebase/genkit/go/core"
26-
coreresource "github.com/firebase/genkit/go/core/resource"
2728
"github.com/firebase/genkit/go/internal/registry"
29+
"github.com/yosida95/uritemplate/v3"
2830
)
2931

32+
// normalizeURI normalizes a URI for template matching by removing query parameters,
33+
// fragments, and trailing slashes from the path.
34+
func normalizeURI(rawURI string) string {
35+
// Parse the URI
36+
u, err := url.Parse(rawURI)
37+
if err != nil {
38+
// If parsing fails, return the original URI
39+
return rawURI
40+
}
41+
42+
// Remove query parameters and fragment
43+
u.RawQuery = ""
44+
u.Fragment = ""
45+
46+
// Remove trailing slash from path (but not from the root path)
47+
if len(u.Path) > 1 && strings.HasSuffix(u.Path, "/") {
48+
u.Path = strings.TrimSuffix(u.Path, "/")
49+
}
50+
51+
return u.String()
52+
}
53+
54+
// matches checks if a URI matches the given URI template.
55+
func matches(templateStr, uri string) (bool, error) {
56+
template, err := uritemplate.New(templateStr)
57+
if err != nil {
58+
return false, fmt.Errorf("invalid URI template %q: %w", templateStr, err)
59+
}
60+
61+
normalizedURI := normalizeURI(uri)
62+
values := template.Match(normalizedURI)
63+
return len(values) > 0, nil
64+
}
65+
66+
// extractVariables extracts variables from a URI using the given URI template.
67+
func extractVariables(templateStr, uri string) (map[string]string, error) {
68+
template, err := uritemplate.New(templateStr)
69+
if err != nil {
70+
return nil, fmt.Errorf("invalid URI template %q: %w", templateStr, err)
71+
}
72+
73+
normalizedURI := normalizeURI(uri)
74+
values := template.Match(normalizedURI)
75+
if len(values) == 0 {
76+
return nil, fmt.Errorf("URI %q does not match template", uri)
77+
}
78+
79+
// Convert uritemplate.Values to string map
80+
result := make(map[string]string)
81+
for name, value := range values {
82+
result[name] = value.String()
83+
}
84+
return result, nil
85+
}
86+
3087
// ResourceInput represents the input to a resource function.
3188
type ResourceInput struct {
3289
URI string `json:"uri"` // The resource URI
@@ -75,10 +132,6 @@ func DefineResource(r *registry.Registry, name string, opts *ResourceOptions, fn
75132
metadata := implementResource(name, opts)
76133
resourceAction := core.DefineAction(r, name, core.ActionTypeResource, metadata, fn)
77134
resource := &resource{Action: resourceAction}
78-
79-
// Register the resource wrapper so ListResources can find it
80-
r.RegisterValue(fmt.Sprintf("resource/%s", name), resource)
81-
82135
return resource
83136
}
84137

@@ -143,11 +196,11 @@ func (r *resource) Matches(uri string) bool {
143196

144197
// Check template
145198
if template, ok := resourceMeta["template"].(string); ok && template != "" {
146-
matcher, err := coreresource.NewTemplateMatcher(template)
199+
matches, err := matches(template, uri)
147200
if err != nil {
148201
return false
149202
}
150-
return matcher.Matches(uri)
203+
return matches
151204
}
152205

153206
return false
@@ -171,11 +224,7 @@ func (r *resource) ExtractVariables(uri string) (map[string]string, error) {
171224

172225
// Extract from template
173226
if template, ok := resourceMeta["template"].(string); ok && template != "" {
174-
matcher, err := coreresource.NewTemplateMatcher(template)
175-
if err != nil {
176-
return nil, fmt.Errorf("invalid template %q: %w", template, err)
177-
}
178-
return matcher.ExtractVariables(uri)
227+
return extractVariables(template, uri)
179228
}
180229

181230
return nil, fmt.Errorf("no URI or template found in resource metadata")

go/core/resource.go

Lines changed: 0 additions & 68 deletions
This file was deleted.

go/core/resource/matcher.go

Lines changed: 0 additions & 110 deletions
This file was deleted.

go/genkit/genkit.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"os"
2626
"os/signal"
2727
"path/filepath"
28-
"strings"
2928
"syscall"
3029

3130
"github.com/firebase/genkit/go/ai"
@@ -373,11 +372,9 @@ func ListTools(g *Genkit) []ai.Tool {
373372
continue
374373
}
375374
actionDesc := action.Desc()
376-
if strings.HasPrefix(actionDesc.Key, "/"+string(core.ActionTypeTool)+"/") {
377-
// Extract tool name from key
378-
toolName := strings.TrimPrefix(actionDesc.Key, "/"+string(core.ActionTypeTool)+"/")
379-
// Lookup the actual tool
380-
tool := LookupTool(g, toolName)
375+
if actionDesc.Type == core.ActionTypeTool {
376+
// Lookup the actual tool using the action name
377+
tool := LookupTool(g, actionDesc.Name)
381378
if tool != nil {
382379
tools = append(tools, tool)
383380
}
@@ -1038,11 +1035,10 @@ func ListResources(g *Genkit) []ai.Resource {
10381035
}
10391036
actionDesc := action.Desc()
10401037
if actionDesc.Type == core.ActionTypeResource {
1041-
// Look up the resource wrapper
1042-
if resourceValue := g.reg.LookupValue(fmt.Sprintf("resource/%s", actionDesc.Name)); resourceValue != nil {
1043-
if resource, ok := resourceValue.(ai.Resource); ok {
1044-
resources = append(resources, resource)
1045-
}
1038+
// Lookup the actual resource using the action name
1039+
resource := ai.LookupResource(g.reg, actionDesc.Name)
1040+
if resource != nil {
1041+
resources = append(resources, resource)
10461042
}
10471043
}
10481044
}

go/plugins/mcp/host.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,6 @@ func (h *MCPHost) Connect(ctx context.Context, g *genkit.Genkit, serverName stri
102102

103103
h.clients[serverName] = client
104104

105-
// Auto-register tools and prompts from the connected server
106-
if _, err := client.GetActiveTools(ctx, g); err != nil {
107-
logger.FromContext(ctx).Warn("Failed to register tools from server", "server", serverName, "error", err)
108-
}
109-
110105
return nil
111106
}
112107

go/plugins/mcp/integration_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"time"
2424

2525
"github.com/firebase/genkit/go/ai"
26-
"github.com/firebase/genkit/go/core"
2726
"github.com/firebase/genkit/go/genkit"
2827
"github.com/stretchr/testify/assert"
2928
)
@@ -263,7 +262,7 @@ func TestMCPContentFetch(t *testing.T) {
263262

264263
// Find resource that matches our test URI
265264
testURI := "file://data/example.txt"
266-
var matchingResource core.DetachedResourceAction
265+
var matchingResource ai.Resource
267266
for _, res := range resources {
268267
if res.Matches(testURI) {
269268
matchingResource = res

0 commit comments

Comments
 (0)