Simplify codebase per audit of 8b89f14
This commit is contained in:
@@ -17,6 +17,35 @@ type Tool struct {
|
||||
Rich func(ctx context.Context, args json.RawMessage) Result
|
||||
}
|
||||
|
||||
func decode[A any](raw json.RawMessage) (A, error) {
|
||||
args := *new(A)
|
||||
err := json.Unmarshal(raw, &args)
|
||||
if err != nil {
|
||||
return args, fmt.Errorf("malformed call: %w", err)
|
||||
}
|
||||
return args, nil
|
||||
}
|
||||
|
||||
func Typed[A any](run func(ctx context.Context, args A) (string, error)) func(ctx context.Context, raw json.RawMessage) (string, error) {
|
||||
return func(ctx context.Context, raw json.RawMessage) (string, error) {
|
||||
args, err := decode[A](raw)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return run(ctx, args)
|
||||
}
|
||||
}
|
||||
|
||||
func TypedRich[A any](run func(ctx context.Context, args A) Result) func(ctx context.Context, raw json.RawMessage) Result {
|
||||
return func(ctx context.Context, raw json.RawMessage) Result {
|
||||
args, err := decode[A](raw)
|
||||
if err != nil {
|
||||
return TextResult(err.Error(), true)
|
||||
}
|
||||
return run(ctx, args)
|
||||
}
|
||||
}
|
||||
|
||||
type Content struct {
|
||||
Type string `json:"type"`
|
||||
Text string `json:"text"`
|
||||
|
||||
Reference in New Issue
Block a user