Skip to content
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

[Go] export Action.Name #371

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading