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

     1  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
     2  
     3  package anthropic
     4  
     5  import (
     6  	"context"
     7  	"errors"
     8  	"fmt"
     9  	"net/http"
    10  	"net/url"
    11  	"slices"
    12  
    13  	"github.com/anthropics/anthropic-sdk-go/internal/apiquery"
    14  	"github.com/anthropics/anthropic-sdk-go/internal/requestconfig"
    15  	"github.com/anthropics/anthropic-sdk-go/option"
    16  	"github.com/anthropics/anthropic-sdk-go/packages/pagination"
    17  	"github.com/anthropics/anthropic-sdk-go/packages/param"
    18  	"github.com/anthropics/anthropic-sdk-go/packages/ssestream"
    19  )
    20  
    21  // BetaSessionThreadEventService contains methods and other services that help with
    22  // interacting with the anthropic API.
    23  //
    24  // Note, unlike clients, this service does not read variables from the environment
    25  // automatically. You should not instantiate this service directly, and instead use
    26  // the [NewBetaSessionThreadEventService] method instead.
    27  type BetaSessionThreadEventService struct {
    28  	Options []option.RequestOption
    29  }
    30  
    31  // NewBetaSessionThreadEventService generates a new service that applies the given
    32  // options to each request. These options are applied after the parent client's
    33  // options (if there is one), and before any request-specific options.
    34  func NewBetaSessionThreadEventService(opts ...option.RequestOption) (r BetaSessionThreadEventService) {
    35  	r = BetaSessionThreadEventService{}
    36  	r.Options = opts
    37  	return
    38  }
    39  
    40  // List Session Thread Events
    41  func (r *BetaSessionThreadEventService) List(ctx context.Context, threadID string, params BetaSessionThreadEventListParams, opts ...option.RequestOption) (res *pagination.PageCursor[BetaManagedAgentsSessionEventUnion], err error) {
    42  	var raw *http.Response
    43  	for _, v := range params.Betas {
    44  		opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%v", v)))
    45  	}
    46  	if !param.IsOmitted(params.WorkspaceID) {
    47  		opts = append(opts, option.WithHeader("anthropic-workspace-id", fmt.Sprintf("%v", params.WorkspaceID.Value)))
    48  	}
    49  	opts = slices.Concat(r.Options, opts)
    50  	opts = append([]option.RequestOption{option.WithHeader("anthropic-beta", "managed-agents-2026-04-01"), option.WithResponseInto(&raw)}, opts...)
    51  	if params.SessionID == "" {
    52  		err = errors.New("missing required session_id parameter")
    53  		return nil, err
    54  	}
    55  	if threadID == "" {
    56  		err = errors.New("missing required thread_id parameter")
    57  		return nil, err
    58  	}
    59  	path := fmt.Sprintf("v1/sessions/%s/threads/%s/events?beta=true", params.SessionID, threadID)
    60  	cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, params, &res, opts...)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	err = cfg.Execute()
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	res.SetPageConfig(cfg, raw)
    69  	return res, nil
    70  }
    71  
    72  // List Session Thread Events
    73  func (r *BetaSessionThreadEventService) ListAutoPaging(ctx context.Context, threadID string, params BetaSessionThreadEventListParams, opts ...option.RequestOption) *pagination.PageCursorAutoPager[BetaManagedAgentsSessionEventUnion] {
    74  	return pagination.NewPageCursorAutoPager(r.List(ctx, threadID, params, opts...))
    75  }
    76  
    77  // Stream Session Thread Events
    78  func (r *BetaSessionThreadEventService) StreamEvents(ctx context.Context, threadID string, params BetaSessionThreadEventStreamParams, opts ...option.RequestOption) (stream *ssestream.Stream[BetaManagedAgentsStreamSessionThreadEventsUnion]) {
    79  	var (
    80  		raw *http.Response
    81  		err error
    82  	)
    83  	for _, v := range params.Betas {
    84  		opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%v", v)))
    85  	}
    86  	if !param.IsOmitted(params.WorkspaceID) {
    87  		opts = append(opts, option.WithHeader("anthropic-workspace-id", fmt.Sprintf("%v", params.WorkspaceID.Value)))
    88  	}
    89  	opts = slices.Concat(r.Options, opts)
    90  	opts = append([]option.RequestOption{option.WithHeader("anthropic-beta", "managed-agents-2026-04-01")}, opts...)
    91  	if params.SessionID == "" {
    92  		err = errors.New("missing required session_id parameter")
    93  		return ssestream.NewStream[BetaManagedAgentsStreamSessionThreadEventsUnion](nil, err)
    94  	}
    95  	if threadID == "" {
    96  		err = errors.New("missing required thread_id parameter")
    97  		return ssestream.NewStream[BetaManagedAgentsStreamSessionThreadEventsUnion](nil, err)
    98  	}
    99  	path := fmt.Sprintf("v1/sessions/%s/threads/%s/stream?beta=true", params.SessionID, threadID)
   100  	err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, params, &raw, opts...)
   101  	return ssestream.NewStream[BetaManagedAgentsStreamSessionThreadEventsUnion](ssestream.NewDecoder(raw), err)
   102  }
   103  
   104  type BetaSessionThreadEventListParams struct {
   105  	SessionID string `path:"session_id" api:"required" json:"-"`
   106  	// Query parameter for limit
   107  	Limit param.Opt[int64] `query:"limit,omitzero" json:"-"`
   108  	// Query parameter for page
   109  	Page        param.Opt[string] `query:"page,omitzero" json:"-"`
   110  	WorkspaceID param.Opt[string] `header:"anthropic-workspace-id,omitzero" json:"-"`
   111  	// Optional header to specify the beta version(s) you want to use.
   112  	Betas []AnthropicBeta `header:"anthropic-beta,omitzero" json:"-"`
   113  	paramObj
   114  }
   115  
   116  // URLQuery serializes [BetaSessionThreadEventListParams]'s query parameters as
   117  // `url.Values`.
   118  func (r BetaSessionThreadEventListParams) URLQuery() (v url.Values, err error) {
   119  	return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
   120  		ArrayFormat:  apiquery.ArrayQueryFormatBrackets,
   121  		NestedFormat: apiquery.NestedQueryFormatBrackets,
   122  	})
   123  }
   124  
   125  type BetaSessionThreadEventStreamParams struct {
   126  	SessionID   string            `path:"session_id" api:"required" json:"-"`
   127  	WorkspaceID param.Opt[string] `header:"anthropic-workspace-id,omitzero" json:"-"`
   128  	// When set, this connection also receives streaming deltas (`event_start`,
   129  	// `event_delta`) while an event is being produced, before the event itself
   130  	// arrives. Deltas are best-effort; when the final event is produced it carries the
   131  	// complete content. A model request that ends early (an error or interrupt)
   132  	// produces no final event — its terminal `span.model_request_end` closes the
   133  	// preview. Accepts one or more event types to preview and may be repeated:
   134  	// `agent.message` streams `content_delta` fragments; `agent.thinking` is
   135  	// start-only — a signal that the agent has begun extended thinking, concluded by
   136  	// the `agent.thinking` event itself. Only previews of the requested event types
   137  	// are sent.
   138  	EventDeltas []BetaManagedAgentsDeltaType `query:"event_deltas,omitzero" json:"-"`
   139  	// Optional header to specify the beta version(s) you want to use.
   140  	Betas []AnthropicBeta `header:"anthropic-beta,omitzero" json:"-"`
   141  	paramObj
   142  }
   143  
   144  // URLQuery serializes [BetaSessionThreadEventStreamParams]'s query parameters as
   145  // `url.Values`.
   146  func (r BetaSessionThreadEventStreamParams) URLQuery() (v url.Values, err error) {
   147  	return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
   148  		ArrayFormat:  apiquery.ArrayQueryFormatBrackets,
   149  		NestedFormat: apiquery.NestedQueryFormatBrackets,
   150  	})
   151  }