Skip to content

Commit

Permalink
[Go] export Action.Name (#371)
Browse files Browse the repository at this point in the history
Export the name method on Action. It turns out to be useful
to identify multiple actions returned from an Init call.
  • Loading branch information
jba authored Jun 10, 2024
1 parent 8069f54 commit 465c2f6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
24 changes: 12 additions & 12 deletions go/core/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type streamingCallback[Stream any] func(context.Context, Stream) error
//
// Each time an Action is run, it results in a new trace span.
type Action[In, Out, Stream any] struct {
aname string
name string
atype atype.ActionType
fn Func[In, Out, Stream]
tstate *tracing.State
Expand Down Expand Up @@ -118,7 +118,7 @@ func newStreamingAction[In, Out, Stream any](name string, atype atype.ActionType
var i In
var o Out
return &Action[In, Out, Stream]{
aname: name,
name: name,
atype: atype,
fn: func(ctx context.Context, input In, sc func(context.Context, Stream) error) (Out, error) {
tracing.SetCustomMetadataAttr(ctx, "subtype", string(atype))
Expand All @@ -133,7 +133,7 @@ func newStreamingAction[In, Out, Stream any](name string, atype atype.ActionType
func newActionWithInputSchema[Out any](name string, atype atype.ActionType, metadata map[string]any, fn func(context.Context, any) (Out, error), inputSchema *jsonschema.Schema) *Action[any, Out, struct{}] {
var o Out
return &Action[any, Out, struct{}]{
aname: name,
name: name,
atype: atype,
fn: func(ctx context.Context, input any, sc func(context.Context, struct{}) error) (Out, error) {
tracing.SetCustomMetadataAttr(ctx, "subtype", string(atype))
Expand All @@ -145,8 +145,8 @@ func newActionWithInputSchema[Out any](name string, atype atype.ActionType, meta
}
}

// name returns the Action's name.
func (a *Action[In, Out, Stream]) name() string { return a.aname }
// Name returns the Action's Name.
func (a *Action[In, Out, Stream]) Name() string { return a.name }

func (a *Action[In, Out, Stream]) actionType() atype.ActionType { return a.atype }

Expand All @@ -156,11 +156,11 @@ func (a *Action[In, Out, Stream]) setTracingState(tstate *tracing.State) { a.tst
// Run executes the Action's function in a new trace span.
func (a *Action[In, Out, Stream]) Run(ctx context.Context, input In, cb func(context.Context, Stream) error) (output Out, err error) {
logger.FromContext(ctx).Debug("Action.Run",
"name", a.name,
"name", a.Name,
"input", fmt.Sprintf("%#v", input))
defer func() {
logger.FromContext(ctx).Debug("Action.Run",
"name", a.name,
"name", a.Name,
"output", fmt.Sprintf("%#v", output),
"err", err)
}()
Expand All @@ -169,7 +169,7 @@ func (a *Action[In, Out, Stream]) Run(ctx context.Context, input In, cb func(con
// This action has probably not been registered.
tstate = globalRegistry.tstate
}
return tracing.RunInNewSpan(ctx, tstate, a.aname, "action", false, input,
return tracing.RunInNewSpan(ctx, tstate, a.name, "action", false, input,
func(ctx context.Context, input In) (Out, error) {
start := time.Now()
var err error
Expand All @@ -187,10 +187,10 @@ func (a *Action[In, Out, Stream]) Run(ctx context.Context, input In, cb func(con
}
latency := time.Since(start)
if err != nil {
writeActionFailure(ctx, a.aname, latency, err)
writeActionFailure(ctx, a.name, latency, err)
return internal.Zero[Out](), err
}
writeActionSuccess(ctx, a.aname, latency)
writeActionSuccess(ctx, a.name, latency)
return output, nil
})
}
Expand Down Expand Up @@ -227,7 +227,7 @@ func (a *Action[In, Out, Stream]) runJSON(ctx context.Context, input json.RawMes

// action is the type that all Action[I, O, S] have in common.
type action interface {
name() string
Name() string
actionType() atype.ActionType

// runJSON uses encoding/json to unmarshal the input,
Expand Down Expand Up @@ -263,7 +263,7 @@ func (d1 actionDesc) equal(d2 actionDesc) bool {

func (a *Action[I, O, S]) desc() actionDesc {
ad := actionDesc{
Name: a.aname,
Name: a.name,
Description: a.description,
Metadata: a.metadata,
InputSchema: a.inputSchema,
Expand Down
4 changes: 2 additions & 2 deletions go/core/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const (
// It panics if an action with the same type, provider and name is already
// registered.
func (r *registry) registerAction(provider string, a action) {
key := fmt.Sprintf("/%s/%s/%s", a.actionType(), provider, a.name())
key := fmt.Sprintf("/%s/%s/%s", a.actionType(), provider, a.Name())
r.mu.Lock()
defer r.mu.Unlock()
if _, ok := r.actions[key]; ok {
Expand All @@ -106,7 +106,7 @@ func (r *registry) registerAction(provider string, a action) {
slog.Info("RegisterAction",
"type", a.actionType(),
"provider", provider,
"name", a.name())
"name", a.Name())
}

// lookupAction returns the action for the given key, or nil if there is none.
Expand Down

0 comments on commit 465c2f6

Please sign in to comment.