github.com/anthropics/anthropic-sdk-go@v1.70.1/skill.go (about)

     1  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
     2  
     3  package anthropic
     4  
     5  import (
     6  	"bytes"
     7  	"context"
     8  	"errors"
     9  	"fmt"
    10  	"io"
    11  	"mime/multipart"
    12  	"net/http"
    13  	"net/url"
    14  	"slices"
    15  	"time"
    16  
    17  	"github.com/anthropics/anthropic-sdk-go/internal/apiform"
    18  	"github.com/anthropics/anthropic-sdk-go/internal/apijson"
    19  	"github.com/anthropics/anthropic-sdk-go/internal/apiquery"
    20  	"github.com/anthropics/anthropic-sdk-go/internal/requestconfig"
    21  	"github.com/anthropics/anthropic-sdk-go/option"
    22  	"github.com/anthropics/anthropic-sdk-go/packages/pagination"
    23  	"github.com/anthropics/anthropic-sdk-go/packages/param"
    24  	"github.com/anthropics/anthropic-sdk-go/packages/respjson"
    25  	"github.com/anthropics/anthropic-sdk-go/shared/constant"
    26  )
    27  
    28  // SkillService contains methods and other services that help with interacting with
    29  // the anthropic API.
    30  //
    31  // Note, unlike clients, this service does not read variables from the environment
    32  // automatically. You should not instantiate this service directly, and instead use
    33  // the [NewSkillService] method instead.
    34  type SkillService struct {
    35  	Options  []option.RequestOption
    36  	Versions SkillVersionService
    37  }
    38  
    39  // NewSkillService generates a new service that applies the given options to each
    40  // request. These options are applied after the parent client's options (if there
    41  // is one), and before any request-specific options.
    42  func NewSkillService(opts ...option.RequestOption) (r SkillService) {
    43  	r = SkillService{}
    44  	r.Options = opts
    45  	r.Versions = NewSkillVersionService(opts...)
    46  	return
    47  }
    48  
    49  // Create Skill
    50  func (r *SkillService) New(ctx context.Context, params SkillNewParams, opts ...option.RequestOption) (res *Skill, err error) {
    51  	if !param.IsOmitted(params.WorkspaceID) {
    52  		opts = append(opts, option.WithHeader("anthropic-workspace-id", fmt.Sprintf("%v", params.WorkspaceID.Value)))
    53  	}
    54  	opts = slices.Concat(r.Options, opts)
    55  	path := "v1/skills"
    56  	err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &res, opts...)
    57  	return res, err
    58  }
    59  
    60  // Get Skill
    61  func (r *SkillService) Get(ctx context.Context, skillID string, query SkillGetParams, opts ...option.RequestOption) (res *Skill, err error) {
    62  	if !param.IsOmitted(query.WorkspaceID) {
    63  		opts = append(opts, option.WithHeader("anthropic-workspace-id", fmt.Sprintf("%v", query.WorkspaceID.Value)))
    64  	}
    65  	opts = slices.Concat(r.Options, opts)
    66  	if skillID == "" {
    67  		err = errors.New("missing required skill_id parameter")
    68  		return nil, err
    69  	}
    70  	path := fmt.Sprintf("v1/skills/%s", skillID)
    71  	err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
    72  	return res, err
    73  }
    74  
    75  // List Skills
    76  func (r *SkillService) List(ctx context.Context, params SkillListParams, opts ...option.RequestOption) (res *pagination.PageCursor[Skill], err error) {
    77  	var raw *http.Response
    78  	if !param.IsOmitted(params.WorkspaceID) {
    79  		opts = append(opts, option.WithHeader("anthropic-workspace-id", fmt.Sprintf("%v", params.WorkspaceID.Value)))
    80  	}
    81  	opts = slices.Concat(r.Options, opts)
    82  	opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
    83  	path := "v1/skills"
    84  	cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, params, &res, opts...)
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  	err = cfg.Execute()
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  	res.SetPageConfig(cfg, raw)
    93  	return res, nil
    94  }
    95  
    96  // List Skills
    97  func (r *SkillService) ListAutoPaging(ctx context.Context, params SkillListParams, opts ...option.RequestOption) *pagination.PageCursorAutoPager[Skill] {
    98  	return pagination.NewPageCursorAutoPager(r.List(ctx, params, opts...))
    99  }
   100  
   101  // Delete Skill
   102  func (r *SkillService) Delete(ctx context.Context, skillID string, body SkillDeleteParams, opts ...option.RequestOption) (res *DeletedSkill, err error) {
   103  	if !param.IsOmitted(body.WorkspaceID) {
   104  		opts = append(opts, option.WithHeader("anthropic-workspace-id", fmt.Sprintf("%v", body.WorkspaceID.Value)))
   105  	}
   106  	opts = slices.Concat(r.Options, opts)
   107  	if skillID == "" {
   108  		err = errors.New("missing required skill_id parameter")
   109  		return nil, err
   110  	}
   111  	path := fmt.Sprintf("v1/skills/%s", skillID)
   112  	err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, nil, &res, opts...)
   113  	return res, err
   114  }
   115  
   116  type DeletedSkill struct {
   117  	// Unique identifier for the skill.
   118  	//
   119  	// The format and length of IDs may change over time.
   120  	ID string `json:"id" api:"required"`
   121  	// Deleted object type.
   122  	//
   123  	// For Skills, this is always `"skill_deleted"`.
   124  	Type constant.SkillDeleted `json:"type" default:"skill_deleted"`
   125  	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
   126  	JSON struct {
   127  		ID          respjson.Field
   128  		Type        respjson.Field
   129  		ExtraFields map[string]respjson.Field
   130  		raw         string
   131  	} `json:"-"`
   132  }
   133  
   134  // Returns the unmodified JSON received from the API
   135  func (r DeletedSkill) RawJSON() string { return r.JSON.raw }
   136  func (r *DeletedSkill) UnmarshalJSON(data []byte) error {
   137  	return apijson.UnmarshalRoot(data, r)
   138  }
   139  
   140  type Skill struct {
   141  	// Unique identifier for the skill.
   142  	//
   143  	// The format and length of IDs may change over time.
   144  	ID string `json:"id" api:"required"`
   145  	// ISO 8601 timestamp of when the skill was created.
   146  	CreatedAt time.Time `json:"created_at" api:"required" format:"date-time"`
   147  	// Human-readable, single-line label for the Skill. Maximum 255 characters. Always
   148  	// set: derived from the SKILL.md frontmatter `name` when omitted at creation. Not
   149  	// unique.
   150  	DisplayName string `json:"display_name" api:"required"`
   151  	// ID of the newest Skill Version — what `latest` references resolve to. Always
   152  	// set: a Skill holds at least one version.
   153  	LatestVersionID string `json:"latest_version_id" api:"required"`
   154  	// Where the Skill comes from.
   155  	//
   156  	// Possible values:
   157  	//
   158  	// - `"custom"`: authored by the platform user; private to their workspace
   159  	// - `"anthropic"`: published by Anthropic; shared and read-only
   160  	// - `"anthropic_example"`: Anthropic-published sample Skill
   161  	// - `"plugin"`: resolved from an installed plugin
   162  	Source SkillSource `json:"source" api:"required"`
   163  	// Object type.
   164  	//
   165  	// For Skills, this is always `"skill"`.
   166  	Type constant.Skill `json:"type" default:"skill"`
   167  	// ISO 8601 timestamp of when the skill was last updated.
   168  	UpdatedAt time.Time `json:"updated_at" api:"required" format:"date-time"`
   169  	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
   170  	JSON struct {
   171  		ID              respjson.Field
   172  		CreatedAt       respjson.Field
   173  		DisplayName     respjson.Field
   174  		LatestVersionID respjson.Field
   175  		Source          respjson.Field
   176  		Type            respjson.Field
   177  		UpdatedAt       respjson.Field
   178  		ExtraFields     map[string]respjson.Field
   179  		raw             string
   180  	} `json:"-"`
   181  }
   182  
   183  // Returns the unmodified JSON received from the API
   184  func (r Skill) RawJSON() string { return r.JSON.raw }
   185  func (r *Skill) UnmarshalJSON(data []byte) error {
   186  	return apijson.UnmarshalRoot(data, r)
   187  }
   188  
   189  type SkillSource struct {
   190  	// Where the Skill comes from.
   191  	//
   192  	// Possible values:
   193  	//
   194  	// - `"custom"`: authored by the platform user; private to their workspace
   195  	// - `"anthropic"`: published by Anthropic; shared and read-only
   196  	// - `"anthropic_example"`: Anthropic-published sample Skill
   197  	// - `"plugin"`: resolved from an installed plugin
   198  	//
   199  	// Any of "custom", "anthropic", "anthropic_example", "plugin".
   200  	Type SkillSourceType `json:"type" api:"required"`
   201  	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
   202  	JSON struct {
   203  		Type        respjson.Field
   204  		ExtraFields map[string]respjson.Field
   205  		raw         string
   206  	} `json:"-"`
   207  }
   208  
   209  // Returns the unmodified JSON received from the API
   210  func (r SkillSource) RawJSON() string { return r.JSON.raw }
   211  func (r *SkillSource) UnmarshalJSON(data []byte) error {
   212  	return apijson.UnmarshalRoot(data, r)
   213  }
   214  
   215  // Where the Skill comes from.
   216  //
   217  // Possible values:
   218  //
   219  // - `"custom"`: authored by the platform user; private to their workspace
   220  // - `"anthropic"`: published by Anthropic; shared and read-only
   221  // - `"anthropic_example"`: Anthropic-published sample Skill
   222  // - `"plugin"`: resolved from an installed plugin
   223  type SkillSourceType string
   224  
   225  const (
   226  	SkillSourceTypeCustom           SkillSourceType = "custom"
   227  	SkillSourceTypeAnthropic        SkillSourceType = "anthropic"
   228  	SkillSourceTypeAnthropicExample SkillSourceType = "anthropic_example"
   229  	SkillSourceTypePlugin           SkillSourceType = "plugin"
   230  )
   231  
   232  type SkillNewParams struct {
   233  	// Files to upload for the skill.
   234  	//
   235  	// All files must be in the same top-level directory and must include a SKILL.md
   236  	// file at the root of that directory.
   237  	Files []io.Reader `json:"files,omitzero" api:"required" format:"binary"`
   238  	// Human-readable, single-line label for the Skill. Maximum 255 characters. Always
   239  	// set: derived from the SKILL.md frontmatter `name` when omitted at creation. Not
   240  	// unique.
   241  	DisplayName param.Opt[string] `json:"display_name,omitzero"`
   242  	WorkspaceID param.Opt[string] `header:"anthropic-workspace-id,omitzero" json:"-"`
   243  	paramObj
   244  }
   245  
   246  func (r SkillNewParams) MarshalMultipart() (data []byte, contentType string, err error) {
   247  	buf := bytes.NewBuffer(nil)
   248  	writer := multipart.NewWriter(buf)
   249  	err = apiform.MarshalRoot(r, writer)
   250  	if err == nil {
   251  		err = apiform.WriteExtras(writer, r.ExtraFields())
   252  	}
   253  	if err != nil {
   254  		writer.Close()
   255  		return nil, "", err
   256  	}
   257  	err = writer.Close()
   258  	if err != nil {
   259  		return nil, "", err
   260  	}
   261  	return buf.Bytes(), writer.FormDataContentType(), nil
   262  }
   263  
   264  type SkillGetParams struct {
   265  	WorkspaceID param.Opt[string] `header:"anthropic-workspace-id,omitzero" json:"-"`
   266  	paramObj
   267  }
   268  
   269  type SkillListParams struct {
   270  	// Pagination token for fetching a specific page of results.
   271  	//
   272  	// Pass the value from a previous response's `next_page` field to get the next page
   273  	// of results.
   274  	Page param.Opt[string] `query:"page,omitzero" json:"-"`
   275  	// Filter skills by source.
   276  	//
   277  	// If provided, only skills from the specified source will be returned:
   278  	//
   279  	// - `"custom"`: only return user-created skills
   280  	// - `"anthropic"`: only return Anthropic-created skills
   281  	Source param.Opt[string] `query:"source,omitzero" json:"-"`
   282  	// Number of results to return per page.
   283  	//
   284  	// Ranges from `1` to `1000`. Defaults to `20`.
   285  	Limit       param.Opt[int64]  `query:"limit,omitzero" json:"-"`
   286  	WorkspaceID param.Opt[string] `header:"anthropic-workspace-id,omitzero" json:"-"`
   287  	paramObj
   288  }
   289  
   290  // URLQuery serializes [SkillListParams]'s query parameters as `url.Values`.
   291  func (r SkillListParams) URLQuery() (v url.Values, err error) {
   292  	return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
   293  		ArrayFormat:  apiquery.ArrayQueryFormatBrackets,
   294  		NestedFormat: apiquery.NestedQueryFormatBrackets,
   295  	})
   296  }
   297  
   298  type SkillDeleteParams struct {
   299  	WorkspaceID param.Opt[string] `header:"anthropic-workspace-id,omitzero" json:"-"`
   300  	paramObj
   301  }