inputitem.go

  1// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2
  3package responses
  4
  5import (
  6	"context"
  7	"errors"
  8	"fmt"
  9	"net/http"
 10	"net/url"
 11
 12	"github.com/openai/openai-go/internal/apijson"
 13	"github.com/openai/openai-go/internal/apiquery"
 14	"github.com/openai/openai-go/internal/requestconfig"
 15	"github.com/openai/openai-go/option"
 16	"github.com/openai/openai-go/packages/pagination"
 17	"github.com/openai/openai-go/packages/param"
 18	"github.com/openai/openai-go/packages/resp"
 19	"github.com/openai/openai-go/shared/constant"
 20)
 21
 22// InputItemService contains methods and other services that help with interacting
 23// with the openai API.
 24//
 25// Note, unlike clients, this service does not read variables from the environment
 26// automatically. You should not instantiate this service directly, and instead use
 27// the [NewInputItemService] method instead.
 28type InputItemService struct {
 29	Options []option.RequestOption
 30}
 31
 32// NewInputItemService generates a new service that applies the given options to
 33// each request. These options are applied after the parent client's options (if
 34// there is one), and before any request-specific options.
 35func NewInputItemService(opts ...option.RequestOption) (r InputItemService) {
 36	r = InputItemService{}
 37	r.Options = opts
 38	return
 39}
 40
 41// Returns a list of input items for a given response.
 42func (r *InputItemService) List(ctx context.Context, responseID string, query InputItemListParams, opts ...option.RequestOption) (res *pagination.CursorPage[ResponseItemUnion], err error) {
 43	var raw *http.Response
 44	opts = append(r.Options[:], opts...)
 45	opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
 46	if responseID == "" {
 47		err = errors.New("missing required response_id parameter")
 48		return
 49	}
 50	path := fmt.Sprintf("responses/%s/input_items", responseID)
 51	cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, query, &res, opts...)
 52	if err != nil {
 53		return nil, err
 54	}
 55	err = cfg.Execute()
 56	if err != nil {
 57		return nil, err
 58	}
 59	res.SetPageConfig(cfg, raw)
 60	return res, nil
 61}
 62
 63// Returns a list of input items for a given response.
 64func (r *InputItemService) ListAutoPaging(ctx context.Context, responseID string, query InputItemListParams, opts ...option.RequestOption) *pagination.CursorPageAutoPager[ResponseItemUnion] {
 65	return pagination.NewCursorPageAutoPager(r.List(ctx, responseID, query, opts...))
 66}
 67
 68// A list of Response items.
 69type ResponseItemList struct {
 70	// A list of items used to generate this response.
 71	Data []ResponseItemUnion `json:"data,required"`
 72	// The ID of the first item in the list.
 73	FirstID string `json:"first_id,required"`
 74	// Whether there are more items available.
 75	HasMore bool `json:"has_more,required"`
 76	// The ID of the last item in the list.
 77	LastID string `json:"last_id,required"`
 78	// The type of object returned, must be `list`.
 79	Object constant.List `json:"object,required"`
 80	// Metadata for the response, check the presence of optional fields with the
 81	// [resp.Field.IsPresent] method.
 82	JSON struct {
 83		Data        resp.Field
 84		FirstID     resp.Field
 85		HasMore     resp.Field
 86		LastID      resp.Field
 87		Object      resp.Field
 88		ExtraFields map[string]resp.Field
 89		raw         string
 90	} `json:"-"`
 91}
 92
 93// Returns the unmodified JSON received from the API
 94func (r ResponseItemList) RawJSON() string { return r.JSON.raw }
 95func (r *ResponseItemList) UnmarshalJSON(data []byte) error {
 96	return apijson.UnmarshalRoot(data, r)
 97}
 98
 99type InputItemListParams struct {
100	// An item ID to list items after, used in pagination.
101	After param.Opt[string] `query:"after,omitzero" json:"-"`
102	// An item ID to list items before, used in pagination.
103	Before param.Opt[string] `query:"before,omitzero" json:"-"`
104	// A limit on the number of objects to be returned. Limit can range between 1 and
105	// 100, and the default is 20.
106	Limit param.Opt[int64] `query:"limit,omitzero" json:"-"`
107	// The order to return the input items in. Default is `asc`.
108	//
109	// - `asc`: Return the input items in ascending order.
110	// - `desc`: Return the input items in descending order.
111	//
112	// Any of "asc", "desc".
113	Order InputItemListParamsOrder `query:"order,omitzero" json:"-"`
114	paramObj
115}
116
117// IsPresent returns true if the field's value is not omitted and not the JSON
118// "null". To check if this field is omitted, use [param.IsOmitted].
119func (f InputItemListParams) IsPresent() bool { return !param.IsOmitted(f) && !f.IsNull() }
120
121// URLQuery serializes [InputItemListParams]'s query parameters as `url.Values`.
122func (r InputItemListParams) URLQuery() (v url.Values) {
123	return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
124		ArrayFormat:  apiquery.ArrayQueryFormatBrackets,
125		NestedFormat: apiquery.NestedQueryFormatBrackets,
126	})
127}
128
129// The order to return the input items in. Default is `asc`.
130//
131// - `asc`: Return the input items in ascending order.
132// - `desc`: Return the input items in descending order.
133type InputItemListParamsOrder string
134
135const (
136	InputItemListParamsOrderAsc  InputItemListParamsOrder = "asc"
137	InputItemListParamsOrderDesc InputItemListParamsOrder = "desc"
138)