fix: add involves edge from task to agent:nomos at creation
Plus sync vendor directory for Docker build compatibility.
This commit is contained in:
223
vendor/github.com/openai/openai-go/packages/pagination/pagination.go
generated
vendored
Normal file
223
vendor/github.com/openai/openai-go/packages/pagination/pagination.go
generated
vendored
Normal file
@@ -0,0 +1,223 @@
|
||||
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
||||
|
||||
package pagination
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
"github.com/openai/openai-go/internal/apijson"
|
||||
"github.com/openai/openai-go/internal/requestconfig"
|
||||
"github.com/openai/openai-go/option"
|
||||
"github.com/openai/openai-go/packages/param"
|
||||
"github.com/openai/openai-go/packages/respjson"
|
||||
)
|
||||
|
||||
// aliased to make [param.APIUnion] private when embedding
|
||||
type paramUnion = param.APIUnion
|
||||
|
||||
// aliased to make [param.APIObject] private when embedding
|
||||
type paramObj = param.APIObject
|
||||
|
||||
type Page[T any] struct {
|
||||
Data []T `json:"data"`
|
||||
Object string `json:"object,required"`
|
||||
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
|
||||
JSON struct {
|
||||
Data respjson.Field
|
||||
Object respjson.Field
|
||||
ExtraFields map[string]respjson.Field
|
||||
raw string
|
||||
} `json:"-"`
|
||||
cfg *requestconfig.RequestConfig
|
||||
res *http.Response
|
||||
}
|
||||
|
||||
// Returns the unmodified JSON received from the API
|
||||
func (r Page[T]) RawJSON() string { return r.JSON.raw }
|
||||
func (r *Page[T]) UnmarshalJSON(data []byte) error {
|
||||
return apijson.UnmarshalRoot(data, r)
|
||||
}
|
||||
|
||||
// GetNextPage returns the next page as defined by this pagination style. When
|
||||
// there is no next page, this function will return a 'nil' for the page value, but
|
||||
// will not return an error
|
||||
func (r *Page[T]) GetNextPage() (res *Page[T], err error) {
|
||||
if len(r.Data) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
// This page represents a response that isn't actually paginated at the API level
|
||||
// so there will never be a next page.
|
||||
cfg := (*requestconfig.RequestConfig)(nil)
|
||||
if cfg == nil {
|
||||
return nil, nil
|
||||
}
|
||||
var raw *http.Response
|
||||
cfg.ResponseInto = &raw
|
||||
cfg.ResponseBodyInto = &res
|
||||
err = cfg.Execute()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res.SetPageConfig(cfg, raw)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (r *Page[T]) SetPageConfig(cfg *requestconfig.RequestConfig, res *http.Response) {
|
||||
if r == nil {
|
||||
r = &Page[T]{}
|
||||
}
|
||||
r.cfg = cfg
|
||||
r.res = res
|
||||
}
|
||||
|
||||
type PageAutoPager[T any] struct {
|
||||
page *Page[T]
|
||||
cur T
|
||||
idx int
|
||||
run int
|
||||
err error
|
||||
paramObj
|
||||
}
|
||||
|
||||
func NewPageAutoPager[T any](page *Page[T], err error) *PageAutoPager[T] {
|
||||
return &PageAutoPager[T]{
|
||||
page: page,
|
||||
err: err,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *PageAutoPager[T]) Next() bool {
|
||||
if r.page == nil || len(r.page.Data) == 0 {
|
||||
return false
|
||||
}
|
||||
if r.idx >= len(r.page.Data) {
|
||||
r.idx = 0
|
||||
r.page, r.err = r.page.GetNextPage()
|
||||
if r.err != nil || r.page == nil || len(r.page.Data) == 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
r.cur = r.page.Data[r.idx]
|
||||
r.run += 1
|
||||
r.idx += 1
|
||||
return true
|
||||
}
|
||||
|
||||
func (r *PageAutoPager[T]) Current() T {
|
||||
return r.cur
|
||||
}
|
||||
|
||||
func (r *PageAutoPager[T]) Err() error {
|
||||
return r.err
|
||||
}
|
||||
|
||||
func (r *PageAutoPager[T]) Index() int {
|
||||
return r.run
|
||||
}
|
||||
|
||||
type CursorPage[T any] struct {
|
||||
Data []T `json:"data"`
|
||||
HasMore bool `json:"has_more"`
|
||||
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
|
||||
JSON struct {
|
||||
Data respjson.Field
|
||||
HasMore respjson.Field
|
||||
ExtraFields map[string]respjson.Field
|
||||
raw string
|
||||
} `json:"-"`
|
||||
cfg *requestconfig.RequestConfig
|
||||
res *http.Response
|
||||
}
|
||||
|
||||
// Returns the unmodified JSON received from the API
|
||||
func (r CursorPage[T]) RawJSON() string { return r.JSON.raw }
|
||||
func (r *CursorPage[T]) UnmarshalJSON(data []byte) error {
|
||||
return apijson.UnmarshalRoot(data, r)
|
||||
}
|
||||
|
||||
// GetNextPage returns the next page as defined by this pagination style. When
|
||||
// there is no next page, this function will return a 'nil' for the page value, but
|
||||
// will not return an error
|
||||
func (r *CursorPage[T]) GetNextPage() (res *CursorPage[T], err error) {
|
||||
if len(r.Data) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if r.JSON.HasMore.Valid() && r.HasMore == false {
|
||||
return nil, nil
|
||||
}
|
||||
items := r.Data
|
||||
if items == nil || len(items) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
cfg := r.cfg.Clone(r.cfg.Context)
|
||||
value := reflect.ValueOf(items[len(items)-1])
|
||||
field := value.FieldByName("ID")
|
||||
err = cfg.Apply(option.WithQuery("after", field.Interface().(string)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var raw *http.Response
|
||||
cfg.ResponseInto = &raw
|
||||
cfg.ResponseBodyInto = &res
|
||||
err = cfg.Execute()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res.SetPageConfig(cfg, raw)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (r *CursorPage[T]) SetPageConfig(cfg *requestconfig.RequestConfig, res *http.Response) {
|
||||
if r == nil {
|
||||
r = &CursorPage[T]{}
|
||||
}
|
||||
r.cfg = cfg
|
||||
r.res = res
|
||||
}
|
||||
|
||||
type CursorPageAutoPager[T any] struct {
|
||||
page *CursorPage[T]
|
||||
cur T
|
||||
idx int
|
||||
run int
|
||||
err error
|
||||
paramObj
|
||||
}
|
||||
|
||||
func NewCursorPageAutoPager[T any](page *CursorPage[T], err error) *CursorPageAutoPager[T] {
|
||||
return &CursorPageAutoPager[T]{
|
||||
page: page,
|
||||
err: err,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *CursorPageAutoPager[T]) Next() bool {
|
||||
if r.page == nil || len(r.page.Data) == 0 {
|
||||
return false
|
||||
}
|
||||
if r.idx >= len(r.page.Data) {
|
||||
r.idx = 0
|
||||
r.page, r.err = r.page.GetNextPage()
|
||||
if r.err != nil || r.page == nil || len(r.page.Data) == 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
r.cur = r.page.Data[r.idx]
|
||||
r.run += 1
|
||||
r.idx += 1
|
||||
return true
|
||||
}
|
||||
|
||||
func (r *CursorPageAutoPager[T]) Current() T {
|
||||
return r.cur
|
||||
}
|
||||
|
||||
func (r *CursorPageAutoPager[T]) Err() error {
|
||||
return r.err
|
||||
}
|
||||
|
||||
func (r *CursorPageAutoPager[T]) Index() int {
|
||||
return r.run
|
||||
}
|
||||
101
vendor/github.com/openai/openai-go/packages/param/encoder.go
generated
vendored
Normal file
101
vendor/github.com/openai/openai-go/packages/param/encoder.go
generated
vendored
Normal file
@@ -0,0 +1,101 @@
|
||||
package param
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
shimjson "github.com/openai/openai-go/internal/encoding/json"
|
||||
|
||||
"github.com/tidwall/sjson"
|
||||
)
|
||||
|
||||
// EncodedAsDate is not be stable and shouldn't be relied upon
|
||||
type EncodedAsDate Opt[time.Time]
|
||||
|
||||
type forceOmit int
|
||||
|
||||
func (m EncodedAsDate) MarshalJSON() ([]byte, error) {
|
||||
underlying := Opt[time.Time](m)
|
||||
bytes := underlying.MarshalJSONWithTimeLayout("2006-01-02")
|
||||
if len(bytes) > 0 {
|
||||
return bytes, nil
|
||||
}
|
||||
return underlying.MarshalJSON()
|
||||
}
|
||||
|
||||
// MarshalObject uses a shimmed 'encoding/json' from Go 1.24, to support the 'omitzero' tag
|
||||
//
|
||||
// Stability for the API of MarshalObject is not guaranteed.
|
||||
func MarshalObject[T ParamStruct](f T, underlying any) ([]byte, error) {
|
||||
return MarshalWithExtras(f, underlying, f.extraFields())
|
||||
}
|
||||
|
||||
// MarshalWithExtras is used to marshal a struct with additional properties.
|
||||
//
|
||||
// Stability for the API of MarshalWithExtras is not guaranteed.
|
||||
func MarshalWithExtras[T ParamStruct, R any](f T, underlying any, extras map[string]R) ([]byte, error) {
|
||||
if f.null() {
|
||||
return []byte("null"), nil
|
||||
} else if len(extras) > 0 {
|
||||
bytes, err := shimjson.Marshal(underlying)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for k, v := range extras {
|
||||
var a any = v
|
||||
if a == Omit {
|
||||
// Errors when handling ForceOmitted are ignored.
|
||||
if b, e := sjson.DeleteBytes(bytes, k); e == nil {
|
||||
bytes = b
|
||||
}
|
||||
continue
|
||||
}
|
||||
bytes, err = sjson.SetBytes(bytes, k, v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return bytes, nil
|
||||
} else if ovr, ok := f.Overrides(); ok {
|
||||
return shimjson.Marshal(ovr)
|
||||
} else {
|
||||
return shimjson.Marshal(underlying)
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalUnion uses a shimmed 'encoding/json' from Go 1.24, to support the 'omitzero' tag
|
||||
//
|
||||
// Stability for the API of MarshalUnion is not guaranteed.
|
||||
func MarshalUnion[T ParamStruct](metadata T, variants ...any) ([]byte, error) {
|
||||
nPresent := 0
|
||||
presentIdx := -1
|
||||
for i, variant := range variants {
|
||||
if !IsOmitted(variant) {
|
||||
nPresent++
|
||||
presentIdx = i
|
||||
}
|
||||
}
|
||||
if nPresent == 0 || presentIdx == -1 {
|
||||
if ovr, ok := metadata.Overrides(); ok {
|
||||
return shimjson.Marshal(ovr)
|
||||
}
|
||||
return []byte(`null`), nil
|
||||
} else if nPresent > 1 {
|
||||
return nil, &json.MarshalerError{
|
||||
Type: typeFor[T](),
|
||||
Err: fmt.Errorf("expected union to have only one present variant, got %d", nPresent),
|
||||
}
|
||||
}
|
||||
return shimjson.Marshal(variants[presentIdx])
|
||||
}
|
||||
|
||||
// typeFor is shimmed from Go 1.23 "reflect" package
|
||||
func typeFor[T any]() reflect.Type {
|
||||
var v T
|
||||
if t := reflect.TypeOf(v); t != nil {
|
||||
return t // optimize for T being a non-interface kind
|
||||
}
|
||||
return reflect.TypeOf((*T)(nil)).Elem() // only for an interface kind
|
||||
}
|
||||
19
vendor/github.com/openai/openai-go/packages/param/null.go
generated
vendored
Normal file
19
vendor/github.com/openai/openai-go/packages/param/null.go
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
package param
|
||||
|
||||
import "github.com/openai/openai-go/internal/encoding/json/sentinel"
|
||||
|
||||
// NullMap returns a non-nil map with a length of 0.
|
||||
// When used with [MarshalObject] or [MarshalUnion], it will be marshaled as null.
|
||||
//
|
||||
// It is unspecified behavior to mutate the map returned by [NullMap].
|
||||
func NullMap[MapT ~map[string]T, T any]() MapT {
|
||||
return sentinel.NewNullSentinel(func() MapT { return make(MapT, 1) })
|
||||
}
|
||||
|
||||
// NullSlice returns a non-nil slice with a length of 0.
|
||||
// When used with [MarshalObject] or [MarshalUnion], it will be marshaled as null.
|
||||
//
|
||||
// It is unspecified behavior to mutate the slice returned by [NullSlice].
|
||||
func NullSlice[SliceT ~[]T, T any]() SliceT {
|
||||
return sentinel.NewNullSentinel(func() SliceT { return make(SliceT, 0, 1) })
|
||||
}
|
||||
121
vendor/github.com/openai/openai-go/packages/param/option.go
generated
vendored
Normal file
121
vendor/github.com/openai/openai-go/packages/param/option.go
generated
vendored
Normal file
@@ -0,0 +1,121 @@
|
||||
package param
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
shimjson "github.com/openai/openai-go/internal/encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
func NewOpt[T comparable](v T) Opt[T] {
|
||||
return Opt[T]{Value: v, status: included}
|
||||
}
|
||||
|
||||
// Null creates optional field with the JSON value "null".
|
||||
//
|
||||
// To set a struct to null, use [NullStruct].
|
||||
func Null[T comparable]() Opt[T] { return Opt[T]{status: null} }
|
||||
|
||||
type status int8
|
||||
|
||||
const (
|
||||
omitted status = iota
|
||||
null
|
||||
included
|
||||
)
|
||||
|
||||
// Opt represents an optional parameter of type T. Use
|
||||
// the [Opt.Valid] method to confirm.
|
||||
type Opt[T comparable] struct {
|
||||
Value T
|
||||
// indicates whether the field should be omitted, null, or valid
|
||||
status status
|
||||
opt
|
||||
}
|
||||
|
||||
// Valid returns true if the value is not "null" or omitted.
|
||||
//
|
||||
// To check if explicitly null, use [Opt.Null].
|
||||
func (o Opt[T]) Valid() bool {
|
||||
var empty Opt[T]
|
||||
return o.status == included || o != empty && o.status != null
|
||||
}
|
||||
|
||||
func (o Opt[T]) Or(v T) T {
|
||||
if o.Valid() {
|
||||
return o.Value
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (o Opt[T]) String() string {
|
||||
if o.null() {
|
||||
return "null"
|
||||
}
|
||||
if s, ok := any(o.Value).(fmt.Stringer); ok {
|
||||
return s.String()
|
||||
}
|
||||
return fmt.Sprintf("%v", o.Value)
|
||||
}
|
||||
|
||||
func (o Opt[T]) MarshalJSON() ([]byte, error) {
|
||||
if !o.Valid() {
|
||||
return []byte("null"), nil
|
||||
}
|
||||
return json.Marshal(o.Value)
|
||||
}
|
||||
|
||||
func (o *Opt[T]) UnmarshalJSON(data []byte) error {
|
||||
if string(data) == "null" {
|
||||
o.status = null
|
||||
return nil
|
||||
}
|
||||
|
||||
var value *T
|
||||
if err := json.Unmarshal(data, &value); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if value == nil {
|
||||
o.status = omitted
|
||||
return nil
|
||||
}
|
||||
|
||||
o.status = included
|
||||
o.Value = *value
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalJSONWithTimeLayout is necessary to bypass the internal caching performed
|
||||
// by [json.Marshal]. Prefer to use [Opt.MarshalJSON] instead.
|
||||
//
|
||||
// This function requires that the generic type parameter of [Opt] is not [time.Time].
|
||||
func (o Opt[T]) MarshalJSONWithTimeLayout(format string) []byte {
|
||||
t, ok := any(o.Value).(time.Time)
|
||||
if !ok || o.null() {
|
||||
return nil
|
||||
}
|
||||
|
||||
b, err := json.Marshal(t.Format(shimjson.TimeLayout(format)))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func (o Opt[T]) null() bool { return o.status == null }
|
||||
func (o Opt[T]) isZero() bool { return o == Opt[T]{} }
|
||||
|
||||
// opt helps limit the [Optional] interface to only types in this package
|
||||
type opt struct{}
|
||||
|
||||
func (opt) implOpt() {}
|
||||
|
||||
// This interface is useful for internal purposes.
|
||||
type Optional interface {
|
||||
Valid() bool
|
||||
null() bool
|
||||
|
||||
isZero() bool
|
||||
implOpt()
|
||||
}
|
||||
168
vendor/github.com/openai/openai-go/packages/param/param.go
generated
vendored
Normal file
168
vendor/github.com/openai/openai-go/packages/param/param.go
generated
vendored
Normal file
@@ -0,0 +1,168 @@
|
||||
package param
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/openai/openai-go/internal/encoding/json/sentinel"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// NullStruct is used to set a struct to the JSON value null.
|
||||
// Check for null structs with [IsNull].
|
||||
//
|
||||
// Only the first type parameter should be provided,
|
||||
// the type PtrT will be inferred.
|
||||
//
|
||||
// json.Marshal(param.NullStruct[MyStruct]()) -> 'null'
|
||||
//
|
||||
// To send null to an [Opt] field use [Null].
|
||||
func NullStruct[T ParamStruct, PtrT InferPtr[T]]() T {
|
||||
var t T
|
||||
pt := PtrT(&t)
|
||||
pt.setMetadata(nil)
|
||||
return *pt
|
||||
}
|
||||
|
||||
// Override replaces the value of a struct with any type.
|
||||
//
|
||||
// Only the first type parameter should be provided,
|
||||
// the type PtrT will be inferred.
|
||||
//
|
||||
// It's often useful for providing raw JSON
|
||||
//
|
||||
// param.Override[MyStruct](json.RawMessage(`{"foo": "bar"}`))
|
||||
//
|
||||
// The public fields of the returned struct T will be unset.
|
||||
//
|
||||
// To override a specific field in a struct, use its [SetExtraFields] method.
|
||||
func Override[T ParamStruct, PtrT InferPtr[T]](v any) T {
|
||||
var t T
|
||||
pt := PtrT(&t)
|
||||
pt.setMetadata(v)
|
||||
return *pt
|
||||
}
|
||||
|
||||
// IsOmitted returns true if v is the zero value of its type.
|
||||
//
|
||||
// If IsOmitted is true, and the field uses a `json:"...,omitzero"` tag,
|
||||
// the field will be omitted from the request.
|
||||
//
|
||||
// If v is set explicitly to the JSON value "null", IsOmitted returns false.
|
||||
func IsOmitted(v any) bool {
|
||||
if v == nil {
|
||||
return false
|
||||
}
|
||||
if o, ok := v.(Optional); ok {
|
||||
return o.isZero()
|
||||
}
|
||||
return reflect.ValueOf(v).IsZero()
|
||||
}
|
||||
|
||||
// IsNull returns true if v was set to the JSON value null.
|
||||
//
|
||||
// To set a param to null use [NullStruct], [Null], [NullMap], or [NullSlice]
|
||||
// depending on the type of v.
|
||||
//
|
||||
// IsNull returns false if the value is omitted.
|
||||
func IsNull[T any](v T) bool {
|
||||
if nullable, ok := any(v).(ParamNullable); ok {
|
||||
return nullable.null()
|
||||
}
|
||||
|
||||
switch reflect.TypeOf(v).Kind() {
|
||||
case reflect.Slice, reflect.Map:
|
||||
return sentinel.IsNull(v)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// ParamNullable encapsulates all structs in parameters,
|
||||
// and all [Opt] types in parameters.
|
||||
type ParamNullable interface {
|
||||
null() bool
|
||||
}
|
||||
|
||||
// ParamStruct represents the set of all structs that are
|
||||
// used in API parameters, by convention these usually end in
|
||||
// "Params" or "Param".
|
||||
type ParamStruct interface {
|
||||
Overrides() (any, bool)
|
||||
null() bool
|
||||
extraFields() map[string]any
|
||||
}
|
||||
|
||||
// This is an implementation detail and should never be explicitly set.
|
||||
type InferPtr[T ParamStruct] interface {
|
||||
setMetadata(any)
|
||||
*T
|
||||
}
|
||||
|
||||
// APIObject should be embedded in api object fields, preferably using an alias to make private
|
||||
type APIObject struct{ metadata }
|
||||
|
||||
// APIUnion should be embedded in all api unions fields, preferably using an alias to make private
|
||||
type APIUnion struct{ metadata }
|
||||
|
||||
// Overrides returns the value of the struct when it is created with
|
||||
// [Override], the second argument helps differentiate an explicit null.
|
||||
func (m metadata) Overrides() (any, bool) {
|
||||
if _, ok := m.any.(metadataExtraFields); ok {
|
||||
return nil, false
|
||||
}
|
||||
return m.any, m.any != nil
|
||||
}
|
||||
|
||||
// ExtraFields returns the extra fields added to the JSON object.
|
||||
func (m metadata) ExtraFields() map[string]any {
|
||||
if extras, ok := m.any.(metadataExtraFields); ok {
|
||||
return extras
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Omit can be used with [metadata.SetExtraFields] to ensure that a
|
||||
// required field is omitted. This is useful as an escape hatch for
|
||||
// when a required is unwanted for some unexpected reason.
|
||||
const Omit forceOmit = -1
|
||||
|
||||
// SetExtraFields adds extra fields to the JSON object.
|
||||
//
|
||||
// SetExtraFields will override any existing fields with the same key.
|
||||
// For security reasons, ensure this is only used with trusted input data.
|
||||
//
|
||||
// To intentionally omit a required field, use [Omit].
|
||||
//
|
||||
// foo.SetExtraFields(map[string]any{"bar": Omit})
|
||||
//
|
||||
// If the struct already contains the field ExtraFields, then this
|
||||
// method will have no effect.
|
||||
func (m *metadata) SetExtraFields(extraFields map[string]any) {
|
||||
m.any = metadataExtraFields(extraFields)
|
||||
}
|
||||
|
||||
// extraFields aliases [metadata.ExtraFields] to avoid name collisions.
|
||||
func (m metadata) extraFields() map[string]any { return m.ExtraFields() }
|
||||
|
||||
func (m metadata) null() bool {
|
||||
if _, ok := m.any.(metadataNull); ok {
|
||||
return true
|
||||
}
|
||||
|
||||
if msg, ok := m.any.(json.RawMessage); ok {
|
||||
return string(msg) == "null"
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
type metadata struct{ any }
|
||||
type metadataNull struct{}
|
||||
type metadataExtraFields map[string]any
|
||||
|
||||
func (m *metadata) setMetadata(override any) {
|
||||
if override == nil {
|
||||
m.any = metadataNull{}
|
||||
return
|
||||
}
|
||||
m.any = override
|
||||
}
|
||||
88
vendor/github.com/openai/openai-go/packages/respjson/respjson.go
generated
vendored
Normal file
88
vendor/github.com/openai/openai-go/packages/respjson/respjson.go
generated
vendored
Normal file
@@ -0,0 +1,88 @@
|
||||
package respjson
|
||||
|
||||
// A Field provides metadata to indicate the presence of a value.
|
||||
//
|
||||
// Use [Field.Valid] to check if an optional value was null or omitted.
|
||||
//
|
||||
// A Field will always occur in the following structure, where it
|
||||
// mirrors the original field in it's parent struct:
|
||||
//
|
||||
// type ExampleObject struct {
|
||||
// Foo bool `json:"foo"`
|
||||
// Bar int `json:"bar"`
|
||||
// // ...
|
||||
//
|
||||
// // JSON provides metadata about the object.
|
||||
// JSON struct {
|
||||
// Foo Field
|
||||
// Bar Field
|
||||
// // ...
|
||||
// } `json:"-"`
|
||||
// }
|
||||
//
|
||||
// To differentiate a "nullish" value from the zero value,
|
||||
// use the [Field.Valid] method.
|
||||
//
|
||||
// if !example.JSON.Foo.Valid() {
|
||||
// println("Foo is null or omitted")
|
||||
// }
|
||||
//
|
||||
// if example.Foo {
|
||||
// println("Foo is true")
|
||||
// } else {
|
||||
// println("Foo is false")
|
||||
// }
|
||||
//
|
||||
// To differentiate if a field was omitted or the JSON value "null",
|
||||
// use the [Field.Raw] method.
|
||||
//
|
||||
// if example.JSON.Foo.Raw() == "null" {
|
||||
// println("Foo is null")
|
||||
// }
|
||||
//
|
||||
// if example.JSON.Foo.Raw() == "" {
|
||||
// println("Foo was omitted")
|
||||
// }
|
||||
//
|
||||
// Otherwise, if the field was invalid and couldn't be marshalled successfully,
|
||||
// [Field.Valid] will be false and [Field.Raw] will not be empty.
|
||||
type Field struct {
|
||||
status
|
||||
raw string
|
||||
}
|
||||
|
||||
const (
|
||||
omitted status = iota
|
||||
null
|
||||
invalid
|
||||
valid
|
||||
)
|
||||
|
||||
type status int8
|
||||
|
||||
// Valid returns true if the parent field was set.
|
||||
// Valid returns false if the value doesn't exist, is JSON null, or
|
||||
// is an unexpected type.
|
||||
func (j Field) Valid() bool { return j.status > invalid }
|
||||
|
||||
const Null string = "null"
|
||||
const Omitted string = ""
|
||||
|
||||
// Returns the raw JSON value of the field.
|
||||
func (j Field) Raw() string {
|
||||
if j.status == omitted {
|
||||
return ""
|
||||
}
|
||||
return j.raw
|
||||
}
|
||||
|
||||
func NewField(raw string) Field {
|
||||
if raw == "null" {
|
||||
return Field{status: null, raw: Null}
|
||||
}
|
||||
return Field{status: valid, raw: raw}
|
||||
}
|
||||
|
||||
func NewInvalidField(raw string) Field {
|
||||
return Field{status: invalid, raw: raw}
|
||||
}
|
||||
217
vendor/github.com/openai/openai-go/packages/ssestream/ssestream.go
generated
vendored
Normal file
217
vendor/github.com/openai/openai-go/packages/ssestream/ssestream.go
generated
vendored
Normal file
@@ -0,0 +1,217 @@
|
||||
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
||||
|
||||
package ssestream
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
type Decoder interface {
|
||||
Event() Event
|
||||
Next() bool
|
||||
Close() error
|
||||
Err() error
|
||||
}
|
||||
|
||||
func NewDecoder(res *http.Response) Decoder {
|
||||
if res == nil || res.Body == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var decoder Decoder
|
||||
contentType := res.Header.Get("content-type")
|
||||
if t, ok := decoderTypes[contentType]; ok {
|
||||
decoder = t(res.Body)
|
||||
} else {
|
||||
scn := bufio.NewScanner(res.Body)
|
||||
scn.Buffer(nil, bufio.MaxScanTokenSize<<9)
|
||||
decoder = &eventStreamDecoder{rc: res.Body, scn: scn}
|
||||
}
|
||||
return decoder
|
||||
}
|
||||
|
||||
var decoderTypes = map[string](func(io.ReadCloser) Decoder){}
|
||||
|
||||
func RegisterDecoder(contentType string, decoder func(io.ReadCloser) Decoder) {
|
||||
decoderTypes[strings.ToLower(contentType)] = decoder
|
||||
}
|
||||
|
||||
type Event struct {
|
||||
Type string
|
||||
Data []byte
|
||||
}
|
||||
|
||||
// A base implementation of a Decoder for text/event-stream.
|
||||
type eventStreamDecoder struct {
|
||||
evt Event
|
||||
rc io.ReadCloser
|
||||
scn *bufio.Scanner
|
||||
err error
|
||||
}
|
||||
|
||||
func (s *eventStreamDecoder) Next() bool {
|
||||
if s.err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
event := ""
|
||||
data := bytes.NewBuffer(nil)
|
||||
|
||||
for s.scn.Scan() {
|
||||
txt := s.scn.Bytes()
|
||||
|
||||
// Dispatch event on an empty line
|
||||
if len(txt) == 0 {
|
||||
s.evt = Event{
|
||||
Type: event,
|
||||
Data: data.Bytes(),
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Split a string like "event: bar" into name="event" and value=" bar".
|
||||
name, value, _ := bytes.Cut(txt, []byte(":"))
|
||||
|
||||
// Consume an optional space after the colon if it exists.
|
||||
if len(value) > 0 && value[0] == ' ' {
|
||||
value = value[1:]
|
||||
}
|
||||
|
||||
switch string(name) {
|
||||
case "":
|
||||
// An empty line in the for ": something" is a comment and should be ignored.
|
||||
continue
|
||||
case "event":
|
||||
event = string(value)
|
||||
case "data":
|
||||
_, s.err = data.Write(value)
|
||||
if s.err != nil {
|
||||
break
|
||||
}
|
||||
_, s.err = data.WriteRune('\n')
|
||||
if s.err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if s.scn.Err() != nil {
|
||||
s.err = s.scn.Err()
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *eventStreamDecoder) Event() Event {
|
||||
return s.evt
|
||||
}
|
||||
|
||||
func (s *eventStreamDecoder) Close() error {
|
||||
return s.rc.Close()
|
||||
}
|
||||
|
||||
func (s *eventStreamDecoder) Err() error {
|
||||
return s.err
|
||||
}
|
||||
|
||||
type Stream[T any] struct {
|
||||
decoder Decoder
|
||||
cur T
|
||||
err error
|
||||
done bool
|
||||
}
|
||||
|
||||
func NewStream[T any](decoder Decoder, err error) *Stream[T] {
|
||||
return &Stream[T]{
|
||||
decoder: decoder,
|
||||
err: err,
|
||||
}
|
||||
}
|
||||
|
||||
// Next returns false if the stream has ended or an error occurred.
|
||||
// Call Stream.Current() to get the current value.
|
||||
// Call Stream.Err() to get the error.
|
||||
//
|
||||
// for stream.Next() {
|
||||
// data := stream.Current()
|
||||
// }
|
||||
//
|
||||
// if stream.Err() != nil {
|
||||
// ...
|
||||
// }
|
||||
func (s *Stream[T]) Next() bool {
|
||||
if s.err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
for s.decoder.Next() {
|
||||
if s.done {
|
||||
continue
|
||||
}
|
||||
|
||||
if bytes.HasPrefix(s.decoder.Event().Data, []byte("[DONE]")) {
|
||||
// In this case we don't break because we still want to iterate through the full stream.
|
||||
s.done = true
|
||||
continue
|
||||
}
|
||||
|
||||
var nxt T
|
||||
|
||||
if s.decoder.Event().Type == "" || !strings.HasPrefix(s.decoder.Event().Type, "thread.") {
|
||||
ep := gjson.GetBytes(s.decoder.Event().Data, "error")
|
||||
if ep.Exists() {
|
||||
s.err = fmt.Errorf("received error while streaming: %s", ep.String())
|
||||
return false
|
||||
}
|
||||
s.err = json.Unmarshal(s.decoder.Event().Data, &nxt)
|
||||
if s.err != nil {
|
||||
return false
|
||||
}
|
||||
s.cur = nxt
|
||||
return true
|
||||
} else {
|
||||
ep := gjson.GetBytes(s.decoder.Event().Data, "error")
|
||||
if ep.Exists() {
|
||||
s.err = fmt.Errorf("received error while streaming: %s", ep.String())
|
||||
return false
|
||||
}
|
||||
event := s.decoder.Event().Type
|
||||
data := s.decoder.Event().Data
|
||||
s.err = json.Unmarshal([]byte(fmt.Sprintf(`{ "event": %q, "data": %s }`, event, data)), &nxt)
|
||||
if s.err != nil {
|
||||
return false
|
||||
}
|
||||
s.cur = nxt
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// decoder.Next() may be false because of an error
|
||||
s.err = s.decoder.Err()
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *Stream[T]) Current() T {
|
||||
return s.cur
|
||||
}
|
||||
|
||||
func (s *Stream[T]) Err() error {
|
||||
return s.err
|
||||
}
|
||||
|
||||
func (s *Stream[T]) Close() error {
|
||||
if s.decoder == nil {
|
||||
// already closed
|
||||
return nil
|
||||
}
|
||||
return s.decoder.Close()
|
||||
}
|
||||
Reference in New Issue
Block a user