-
Notifications
You must be signed in to change notification settings - Fork 11
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
[WIP] Typed capability API #462
base: main
Are you sure you want to change the base?
Changes from all commits
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,164 @@ | ||
package v2 | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"github.com/invopop/jsonschema" | ||
jsonvalidate "github.com/santhosh-tekuri/jsonschema/v5" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/capabilities" | ||
"github.com/smartcontractkit/chainlink-common/pkg/values" | ||
) | ||
|
||
type CapabilityResponse[O any] struct { | ||
Value O | ||
Err error | ||
} | ||
|
||
type CapabilityRequest[I, C any] struct { | ||
Metadata capabilities.RequestMetadata | ||
Config C | ||
Inputs I | ||
} | ||
|
||
type RegisterToWorkflowRequest[C any] struct { | ||
Metadata capabilities.RegistrationMetadata | ||
Config C | ||
} | ||
|
||
type UnregisterFromWorkflowRequest[C any] struct { | ||
Metadata capabilities.RegistrationMetadata | ||
Config C | ||
} | ||
|
||
type Capability[I, O, C any] interface { | ||
RegisterToWorkflow(ctx context.Context, req RegisterToWorkflowRequest[C]) error | ||
UnregisterFromWorkflow(ctx context.Context, req UnregisterFromWorkflowRequest[C]) error | ||
Execute(ctx context.Context, callback chan<- CapabilityResponse[O], request CapabilityRequest[I, C]) error | ||
} | ||
|
||
type capability[I, O, C any] struct { | ||
inner Capability[I, O, C] | ||
} | ||
|
||
func (c *capability[I, O, C]) RegisterToWorkflow(ctx context.Context, req capabilities.RegisterToWorkflowRequest) error { | ||
var conf C | ||
err := c.validate(&conf, req.Config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = req.Config.UnwrapTo(&conf) | ||
if err != nil { | ||
return err | ||
} | ||
regReq := RegisterToWorkflowRequest[C]{ | ||
Metadata: req.Metadata, | ||
Config: conf, | ||
} | ||
return c.inner.RegisterToWorkflow(ctx, regReq) | ||
} | ||
|
||
func (c *capability[I, O, C]) UnregisterFromWorkflow(ctx context.Context, req capabilities.UnregisterFromWorkflowRequest) error { | ||
var conf C | ||
err := c.validate(&conf, req.Config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = req.Config.UnwrapTo(&conf) | ||
if err != nil { | ||
return err | ||
} | ||
regReq := UnregisterFromWorkflowRequest[C]{ | ||
Metadata: req.Metadata, | ||
Config: conf, | ||
} | ||
return c.inner.UnregisterFromWorkflow(ctx, regReq) | ||
} | ||
|
||
func (c *capability[I, O, C]) validate(str any, m *values.Map) error { | ||
sch := jsonschema.Reflect(str) | ||
schemab, err := json.Marshal(sch) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
mapping, err := values.Unwrap(m) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
schema, err := jsonvalidate.CompileString("<uriPrefix>", string(schemab)) | ||
if err != nil { | ||
return err | ||
} | ||
return schema.Validate(mapping) | ||
} | ||
|
||
func (c *capability[I, O, C]) Execute(ctx context.Context, callback chan<- capabilities.CapabilityResponse, request capabilities.CapabilityRequest) error { | ||
tcb := make(chan CapabilityResponse[O]) | ||
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. What does 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. I mean obviously it stands for Typed Capability Response :P |
||
go c.forwardResponses(ctx, callback, tcb) | ||
|
||
var conf C | ||
err := c.validate(&conf, request.Config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = request.Config.UnwrapTo(&conf) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var inp I | ||
err = c.validate(&inp, request.Inputs) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = request.Inputs.UnwrapTo(&inp) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
treq := CapabilityRequest[I, C]{ | ||
Metadata: request.Metadata, | ||
Config: conf, | ||
Inputs: inp, | ||
} | ||
|
||
return c.inner.Execute(ctx, tcb, treq) | ||
} | ||
|
||
func (c *capability[I, O, C]) forwardResponses(ctx context.Context, callback chan<- capabilities.CapabilityResponse, typedCallback chan CapabilityResponse[O]) { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case resp, isOpen := <-typedCallback: | ||
if !isOpen { | ||
close(callback) | ||
return | ||
} | ||
|
||
v, err := values.Wrap(resp.Value) | ||
if err != nil { | ||
callback <- capabilities.CapabilityResponse{ | ||
Err: err, | ||
} | ||
} | ||
|
||
callback <- capabilities.CapabilityResponse{ | ||
Value: v, | ||
} | ||
} | ||
} | ||
} | ||
|
||
func NewCapability[I, O, C any](cap Capability[I, O, C]) capabilities.CallbackExecutable { | ||
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. This looks great, and is the direction I wanted to take the Validation API efforts to :) I'd imagine there's a lot of value to be had by allowing the user to inject middlewares that are called during request/response lifecycle 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. Yes agreed :) We could use this for observability and logging for example :) |
||
return &capability[I, O, C]{ | ||
inner: cap, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package v2 | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/capabilities" | ||
"github.com/smartcontractkit/chainlink-common/pkg/values" | ||
) | ||
|
||
type typedCapability struct { | ||
Capability[AnInput, AnOutput, AConfig] | ||
inp AnInput | ||
conf AConfig | ||
} | ||
|
||
type AnInput struct { | ||
Foo string `json:"foo"` | ||
} | ||
|
||
type AConfig struct { | ||
Bar string `json:"bar"` | ||
} | ||
|
||
type AnOutput struct { | ||
Baz string `json:"baz"` | ||
} | ||
|
||
func (t *typedCapability) Execute(ctx context.Context, callback chan<- CapabilityResponse[AnOutput], req CapabilityRequest[AnInput, AConfig]) error { | ||
t.inp = req.Inputs | ||
t.conf = req.Config | ||
return nil | ||
} | ||
|
||
func TestCapabilityV2_Execute(t *testing.T) { | ||
c := &typedCapability{} | ||
cap := NewCapability(c) | ||
|
||
cb := make(chan capabilities.CapabilityResponse) | ||
|
||
conf, err := values.NewMap(map[string]any{ | ||
"bar": "config-string", | ||
}) | ||
require.NoError(t, err) | ||
|
||
inp, err := values.NewMap(map[string]any{ | ||
"foo": "input-string", | ||
}) | ||
require.NoError(t, err) | ||
|
||
req := capabilities.CapabilityRequest{ | ||
Metadata: capabilities.RequestMetadata{}, | ||
Config: conf, | ||
Inputs: inp, | ||
} | ||
err = cap.Execute(context.Background(), cb, req) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, AnInput{Foo: "input-string"}, c.inp) | ||
assert.Equal(t, AConfig{Bar: "config-string"}, c.conf) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do those work in a remote setting? Does the receiver always know what type to expect or is there some type info encoded too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd imagine it's the same, capabilities should have a input/config/output schema they abide to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bolekk This is purely a client-side feature (i.e. inside the capability), anything accessing the capability (either via the Remote API or via the workflow engine would continue to do so via the lower-level, values-based API.