github.com/anthropics/anthropic-sdk-go@v1.70.1/skillversion.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  // SkillVersionService contains methods and other services that help with
    29  // interacting with 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 [NewSkillVersionService] method instead.
    34  type SkillVersionService struct {
    35  	Options []option.RequestOption
    36  }
    37  
    38  // NewSkillVersionService generates a new service that applies the given options to
    39  // each request. These options are applied after the parent client's options (if
    40  // there is one), and before any request-specific options.
    41  func NewSkillVersionService(opts ...option.RequestOption) (r SkillVersionService) {
    42  	r = SkillVersionService{}
    43  	r.Options = opts
    44  	return
    45  }
    46  
    47  // Create Skill Version
    48  func (r *SkillVersionService) New(ctx context.Context, skillID string, params SkillVersionNewParams, opts ...option.RequestOption) (res *SkillVersion, err error) {
    49  	if !param.IsOmitted(params.WorkspaceID) {
    50  		opts = append(opts, option.WithHeader("anthropic-workspace-id", fmt.Sprintf("%v", params.WorkspaceID.Value)))
    51  	}
    52  	opts = slices.Concat(r.Options, opts)
    53  	if skillID == "" {
    54  		err = errors.New("missing required skill_id parameter")
    55  		return nil, err
    56  	}
    57  	path := fmt.Sprintf("v1/skills/%s/versions", skillID)
    58  	err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &res, opts...)
    59  	return res, err
    60  }
    61  
    62  // Get Skill Version
    63  func (r *SkillVersionService) Get(ctx context.Context, version string, params SkillVersionGetParams, opts ...option.RequestOption) (res *SkillVersion, err error) {
    64  	if !param.IsOmitted(params.WorkspaceID) {
    65  		opts = append(opts, option.WithHeader("anthropic-workspace-id", fmt.Sprintf("%v", params.WorkspaceID.Value)))
    66  	}
    67  	opts = slices.Concat(r.Options, opts)
    68  	if params.SkillID == "" {
    69  		err = errors.New("missing required skill_id parameter")
    70  		return nil, err
    71  	}
    72  	if version == "" {
    73  		err = errors.New("missing required version parameter")
    74  		return nil, err
    75  	}
    76  	path := fmt.Sprintf("v1/skills/%s/versions/%s", params.SkillID, version)
    77  	err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
    78  	return res, err
    79  }
    80  
    81  // List Skill Versions
    82  func (r *SkillVersionService) List(ctx context.Context, skillID string, params SkillVersionListParams, opts ...option.RequestOption) (res *pagination.PageCursor[SkillVersion], err error) {
    83  	var raw *http.Response
    84  	if !param.IsOmitted(params.WorkspaceID) {
    85  		opts = append(opts, option.WithHeader("anthropic-workspace-id", fmt.Sprintf("%v", params.WorkspaceID.Value)))
    86  	}
    87  	opts = slices.Concat(r.Options, opts)
    88  	opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
    89  	if skillID == "" {
    90  		err = errors.New("missing required skill_id parameter")
    91  		return nil, err
    92  	}
    93  	path := fmt.Sprintf("v1/skills/%s/versions", skillID)
    94  	cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, params, &res, opts...)
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  	err = cfg.Execute()
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  	res.SetPageConfig(cfg, raw)
   103  	return res, nil
   104  }
   105  
   106  // List Skill Versions
   107  func (r *SkillVersionService) ListAutoPaging(ctx context.Context, skillID string, params SkillVersionListParams, opts ...option.RequestOption) *pagination.PageCursorAutoPager[SkillVersion] {
   108  	return pagination.NewPageCursorAutoPager(r.List(ctx, skillID, params, opts...))
   109  }
   110  
   111  // Delete Skill Version
   112  func (r *SkillVersionService) Delete(ctx context.Context, version string, params SkillVersionDeleteParams, opts ...option.RequestOption) (res *DeletedSkillVersion, err error) {
   113  	if !param.IsOmitted(params.WorkspaceID) {
   114  		opts = append(opts, option.WithHeader("anthropic-workspace-id", fmt.Sprintf("%v", params.WorkspaceID.Value)))
   115  	}
   116  	opts = slices.Concat(r.Options, opts)
   117  	if params.SkillID == "" {
   118  		err = errors.New("missing required skill_id parameter")
   119  		return nil, err
   120  	}
   121  	if version == "" {
   122  		err = errors.New("missing required version parameter")
   123  		return nil, err
   124  	}
   125  	path := fmt.Sprintf("v1/skills/%s/versions/%s", params.SkillID, version)
   126  	err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, nil, &res, opts...)
   127  	return res, err
   128  }
   129  
   130  type DeletedSkillVersion struct {
   131  	// Unique identifier for this Skill Version. The id addresses the version in paths
   132  	// and pins it in references.
   133  	ID string `json:"id" api:"required"`
   134  	// Deleted object type.
   135  	//
   136  	// For Skill Versions, this is always `"skill_version_deleted"`.
   137  	Type constant.SkillVersionDeleted `json:"type" default:"skill_version_deleted"`
   138  	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
   139  	JSON struct {
   140  		ID          respjson.Field
   141  		Type        respjson.Field
   142  		ExtraFields map[string]respjson.Field
   143  		raw         string
   144  	} `json:"-"`
   145  }
   146  
   147  // Returns the unmodified JSON received from the API
   148  func (r DeletedSkillVersion) RawJSON() string { return r.JSON.raw }
   149  func (r *DeletedSkillVersion) UnmarshalJSON(data []byte) error {
   150  	return apijson.UnmarshalRoot(data, r)
   151  }
   152  
   153  type SkillVersion struct {
   154  	// Unique identifier for this Skill Version. The id addresses the version in paths
   155  	// and pins it in references.
   156  	ID string `json:"id" api:"required"`
   157  	// ISO 8601 timestamp of when the skill was created.
   158  	CreatedAt time.Time `json:"created_at" api:"required" format:"date-time"`
   159  	// Description of the skill version.
   160  	//
   161  	// This is extracted from the SKILL.md file in the skill upload.
   162  	Description string `json:"description" api:"required"`
   163  	// The Skill's immutable kebab-case slug, set at creation from the first upload's
   164  	// SKILL.md frontmatter `name` (or its enclosing directory). Every later upload
   165  	// must resolve to the same value. Also the top-level directory of the Skill's
   166  	// mounted files and the base name of a downloaded archive.
   167  	Name string `json:"name" api:"required"`
   168  	// Unique identifier for the skill.
   169  	//
   170  	// The format and length of IDs may change over time.
   171  	SkillID string `json:"skill_id" api:"required"`
   172  	// Object type.
   173  	//
   174  	// For Skill Versions, this is always `"skill_version"`.
   175  	Type constant.SkillVersion `json:"type" default:"skill_version"`
   176  	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
   177  	JSON struct {
   178  		ID          respjson.Field
   179  		CreatedAt   respjson.Field
   180  		Description respjson.Field
   181  		Name        respjson.Field
   182  		SkillID     respjson.Field
   183  		Type        respjson.Field
   184  		ExtraFields map[string]respjson.Field
   185  		raw         string
   186  	} `json:"-"`
   187  }
   188  
   189  // Returns the unmodified JSON received from the API
   190  func (r SkillVersion) RawJSON() string { return r.JSON.raw }
   191  func (r *SkillVersion) UnmarshalJSON(data []byte) error {
   192  	return apijson.UnmarshalRoot(data, r)
   193  }
   194  
   195  type SkillVersionNewParams struct {
   196  	// Files to upload for the skill.
   197  	//
   198  	// All files must be in the same top-level directory and must include a SKILL.md
   199  	// file at the root of that directory.
   200  	Files       []io.Reader       `json:"files,omitzero" api:"required" format:"binary"`
   201  	WorkspaceID param.Opt[string] `header:"anthropic-workspace-id,omitzero" json:"-"`
   202  	paramObj
   203  }
   204  
   205  func (r SkillVersionNewParams) MarshalMultipart() (data []byte, contentType string, err error) {
   206  	buf := bytes.NewBuffer(nil)
   207  	writer := multipart.NewWriter(buf)
   208  	err = apiform.MarshalRoot(r, writer)
   209  	if err == nil {
   210  		err = apiform.WriteExtras(writer, r.ExtraFields())
   211  	}
   212  	if err != nil {
   213  		writer.Close()
   214  		return nil, "", err
   215  	}
   216  	err = writer.Close()
   217  	if err != nil {
   218  		return nil, "", err
   219  	}
   220  	return buf.Bytes(), writer.FormDataContentType(), nil
   221  }
   222  
   223  type SkillVersionGetParams struct {
   224  	// Unique identifier for the skill.
   225  	//
   226  	// The format and length of IDs may change over time.
   227  	SkillID     string            `path:"skill_id" api:"required" json:"-"`
   228  	WorkspaceID param.Opt[string] `header:"anthropic-workspace-id,omitzero" json:"-"`
   229  	paramObj
   230  }
   231  
   232  type SkillVersionListParams struct {
   233  	// Number of results to return per page.
   234  	//
   235  	// Ranges from `1` to `1000`. Defaults to `20`.
   236  	Limit param.Opt[int64] `query:"limit,omitzero" json:"-"`
   237  	// Optionally set to the `next_page` token from the previous response.
   238  	Page        param.Opt[string] `query:"page,omitzero" json:"-"`
   239  	WorkspaceID param.Opt[string] `header:"anthropic-workspace-id,omitzero" json:"-"`
   240  	paramObj
   241  }
   242  
   243  // URLQuery serializes [SkillVersionListParams]'s query parameters as `url.Values`.
   244  func (r SkillVersionListParams) URLQuery() (v url.Values, err error) {
   245  	return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
   246  		ArrayFormat:  apiquery.ArrayQueryFormatBrackets,
   247  		NestedFormat: apiquery.NestedQueryFormatBrackets,
   248  	})
   249  }
   250  
   251  type SkillVersionDeleteParams struct {
   252  	// Unique identifier for the skill.
   253  	//
   254  	// The format and length of IDs may change over time.
   255  	SkillID     string            `path:"skill_id" api:"required" json:"-"`
   256  	WorkspaceID param.Opt[string] `header:"anthropic-workspace-id,omitzero" json:"-"`
   257  	paramObj
   258  }