-
Notifications
You must be signed in to change notification settings - Fork 38
feat: Add A2A parser plugin for inbound request inspection #363
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
Changes from all commits
18572e0
3a958e5
32d699d
182cebe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package plugins | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "log/slog" | ||
|
|
||
| "github.com/kagenti/kagenti-extensions/authbridge/authlib/pipeline" | ||
| ) | ||
|
|
||
| // A2AParser parses A2A JSON-RPC 2.0 request bodies and populates | ||
| // pctx.Extensions.A2A with the parsed method, session ID, message parts, | ||
| // and role for downstream policy plugins (e.g., guardrails). | ||
| type A2AParser struct{} | ||
|
|
||
| func NewA2AParser() *A2AParser { return &A2AParser{} } | ||
|
|
||
| func (p *A2AParser) Name() string { return "a2a-parser" } | ||
|
|
||
| func (p *A2AParser) Capabilities() pipeline.PluginCapabilities { | ||
| return pipeline.PluginCapabilities{ | ||
| Writes: []string{"a2a"}, | ||
| BodyAccess: true, | ||
| } | ||
| } | ||
|
|
||
| func (p *A2AParser) OnRequest(_ context.Context, pctx *pipeline.Context) pipeline.Action { | ||
| if len(pctx.Body) == 0 { | ||
| slog.Debug("a2a-parser: no body, skipping") | ||
| return pipeline.Action{Type: pipeline.Continue} | ||
| } | ||
|
|
||
| var rpc jsonRPCRequest | ||
| if err := json.Unmarshal(pctx.Body, &rpc); err != nil { | ||
| slog.Debug("a2a-parser: invalid JSON-RPC", "error", err, "bodyLen", len(pctx.Body)) | ||
| return pipeline.Action{Type: pipeline.Continue} | ||
| } | ||
|
|
||
| ext := &pipeline.A2AExtension{ | ||
| Method: rpc.Method, | ||
| RPCID: rpc.ID, | ||
| } | ||
|
|
||
| // Extract message fields generically — any method with params.message | ||
| // gets full extraction (forward-compatible with future A2A methods). | ||
| ext.SessionID = rpc.stringParam("sessionId") | ||
| if msg := rpc.mapParam("message"); msg != nil { | ||
| if role, ok := msg["role"].(string); ok { | ||
| ext.Role = role | ||
| } | ||
| if messageID, ok := msg["messageId"].(string); ok { | ||
| ext.MessageID = messageID | ||
| } | ||
| if rawParts, ok := msg["parts"].([]any); ok { | ||
| ext.Parts = parseA2AParts(rawParts) | ||
| } | ||
| } | ||
|
|
||
| pctx.Extensions.A2A = ext | ||
|
|
||
| slog.Info("a2a-parser", "method", rpc.Method) | ||
| slog.Debug("a2a-parser: extracted", | ||
| "method", rpc.Method, | ||
| "sessionId", ext.SessionID, | ||
| "role", ext.Role, | ||
| "messageId", ext.MessageID, | ||
| "parts", len(ext.Parts), | ||
| ) | ||
| return pipeline.Action{Type: pipeline.Continue} | ||
| } | ||
|
|
||
| func (p *A2AParser) OnResponse(_ context.Context, _ *pipeline.Context) pipeline.Action { | ||
| return pipeline.Action{Type: pipeline.Continue} | ||
| } | ||
|
|
||
| func parseA2AParts(rawParts []any) []pipeline.A2APart { | ||
| parts := make([]pipeline.A2APart, 0, len(rawParts)) | ||
| for _, raw := range rawParts { | ||
| partMap, ok := raw.(map[string]any) | ||
| if !ok { | ||
| continue | ||
| } | ||
| kind, ok := partMap["kind"].(string) | ||
| if !ok || kind == "" { | ||
| continue | ||
| } | ||
| var content string | ||
| switch kind { | ||
| case "text": | ||
| content, _ = partMap["text"].(string) | ||
| case "file": | ||
| // TODO: update when A2A spec stabilizes — canonical Part uses mediaType + content field presence, not "kind". | ||
| content, _ = partMap["data"].(string) | ||
| if content == "" { | ||
| content, _ = partMap["uri"].(string) | ||
| } | ||
|
Comment on lines
+91
to
+96
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ref https://a2a-protocol.org/latest/definitions/#protobuf I'm curious how this case maps. Does another part of Kagenti form file data?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question. Looking at the A2A spec, the current spec (June 2025 draft) uses a unified The older SDK samples (which Kagenti UI currently uses) still send As the spec stabilizes, we should update this to match the canonical Part structure (no |
||
| case "data": | ||
| if dataVal, ok := partMap["data"]; ok && dataVal != nil { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: This logs at |
||
| if b, err := json.Marshal(dataVal); err == nil { | ||
| content = string(b) | ||
| } | ||
| } | ||
| } | ||
| parts = append(parts, pipeline.A2APart{Kind: kind, Content: content}) | ||
| } | ||
| return parts | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.