github.com/anthropics/anthropic-sdk-go@v1.70.1/betamemorystore.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  	"time"
    13  
    14  	"github.com/anthropics/anthropic-sdk-go/internal/apijson"
    15  	"github.com/anthropics/anthropic-sdk-go/internal/apiquery"
    16  	"github.com/anthropics/anthropic-sdk-go/internal/requestconfig"
    17  	"github.com/anthropics/anthropic-sdk-go/option"
    18  	"github.com/anthropics/anthropic-sdk-go/packages/pagination"
    19  	"github.com/anthropics/anthropic-sdk-go/packages/param"
    20  	"github.com/anthropics/anthropic-sdk-go/packages/respjson"
    21  )
    22  
    23  // BetaMemoryStoreService contains methods and other services that help with
    24  // interacting with the anthropic API.
    25  //
    26  // Note, unlike clients, this service does not read variables from the environment
    27  // automatically. You should not instantiate this service directly, and instead use
    28  // the [NewBetaMemoryStoreService] method instead.
    29  type BetaMemoryStoreService struct {
    30  	Options        []option.RequestOption
    31  	Memories       BetaMemoryStoreMemoryService
    32  	MemoryVersions BetaMemoryStoreMemoryVersionService
    33  }
    34  
    35  // NewBetaMemoryStoreService generates a new service that applies the given options
    36  // to each request. These options are applied after the parent client's options (if
    37  // there is one), and before any request-specific options.
    38  func NewBetaMemoryStoreService(opts ...option.RequestOption) (r BetaMemoryStoreService) {
    39  	r = BetaMemoryStoreService{}
    40  	r.Options = opts
    41  	r.Memories = NewBetaMemoryStoreMemoryService(opts...)
    42  	r.MemoryVersions = NewBetaMemoryStoreMemoryVersionService(opts...)
    43  	return
    44  }
    45  
    46  // Create a memory store
    47  func (r *BetaMemoryStoreService) New(ctx context.Context, params BetaMemoryStoreNewParams, opts ...option.RequestOption) (res *BetaManagedAgentsMemoryStore, err error) {
    48  	for _, v := range params.Betas {
    49  		opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%v", v)))
    50  	}
    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  	opts = append([]option.RequestOption{option.WithHeader("anthropic-beta", "agent-memory-2026-07-22")}, opts...)
    56  	path := "v1/memory_stores?beta=true"
    57  	err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &res, opts...)
    58  	return res, err
    59  }
    60  
    61  // Retrieve a memory store
    62  func (r *BetaMemoryStoreService) Get(ctx context.Context, memoryStoreID string, query BetaMemoryStoreGetParams, opts ...option.RequestOption) (res *BetaManagedAgentsMemoryStore, err error) {
    63  	for _, v := range query.Betas {
    64  		opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%v", v)))
    65  	}
    66  	if !param.IsOmitted(query.WorkspaceID) {
    67  		opts = append(opts, option.WithHeader("anthropic-workspace-id", fmt.Sprintf("%v", query.WorkspaceID.Value)))
    68  	}
    69  	opts = slices.Concat(r.Options, opts)
    70  	opts = append([]option.RequestOption{option.WithHeader("anthropic-beta", "agent-memory-2026-07-22")}, opts...)
    71  	if memoryStoreID == "" {
    72  		err = errors.New("missing required memory_store_id parameter")
    73  		return nil, err
    74  	}
    75  	path := fmt.Sprintf("v1/memory_stores/%s?beta=true", memoryStoreID)
    76  	err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
    77  	return res, err
    78  }
    79  
    80  // Update a memory store
    81  func (r *BetaMemoryStoreService) Update(ctx context.Context, memoryStoreID string, params BetaMemoryStoreUpdateParams, opts ...option.RequestOption) (res *BetaManagedAgentsMemoryStore, err error) {
    82  	for _, v := range params.Betas {
    83  		opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%v", v)))
    84  	}
    85  	if !param.IsOmitted(params.WorkspaceID) {
    86  		opts = append(opts, option.WithHeader("anthropic-workspace-id", fmt.Sprintf("%v", params.WorkspaceID.Value)))
    87  	}
    88  	opts = slices.Concat(r.Options, opts)
    89  	opts = append([]option.RequestOption{option.WithHeader("anthropic-beta", "agent-memory-2026-07-22")}, opts...)
    90  	if memoryStoreID == "" {
    91  		err = errors.New("missing required memory_store_id parameter")
    92  		return nil, err
    93  	}
    94  	path := fmt.Sprintf("v1/memory_stores/%s?beta=true", memoryStoreID)
    95  	err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &res, opts...)
    96  	return res, err
    97  }
    98  
    99  // List memory stores
   100  func (r *BetaMemoryStoreService) List(ctx context.Context, params BetaMemoryStoreListParams, opts ...option.RequestOption) (res *pagination.PageCursor[BetaManagedAgentsMemoryStore], err error) {
   101  	var raw *http.Response
   102  	for _, v := range params.Betas {
   103  		opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%v", v)))
   104  	}
   105  	if !param.IsOmitted(params.WorkspaceID) {
   106  		opts = append(opts, option.WithHeader("anthropic-workspace-id", fmt.Sprintf("%v", params.WorkspaceID.Value)))
   107  	}
   108  	opts = slices.Concat(r.Options, opts)
   109  	opts = append([]option.RequestOption{option.WithHeader("anthropic-beta", "agent-memory-2026-07-22"), option.WithResponseInto(&raw)}, opts...)
   110  	path := "v1/memory_stores?beta=true"
   111  	cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, params, &res, opts...)
   112  	if err != nil {
   113  		return nil, err
   114  	}
   115  	err = cfg.Execute()
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  	res.SetPageConfig(cfg, raw)
   120  	return res, nil
   121  }
   122  
   123  // List memory stores
   124  func (r *BetaMemoryStoreService) ListAutoPaging(ctx context.Context, params BetaMemoryStoreListParams, opts ...option.RequestOption) *pagination.PageCursorAutoPager[BetaManagedAgentsMemoryStore] {
   125  	return pagination.NewPageCursorAutoPager(r.List(ctx, params, opts...))
   126  }
   127  
   128  // Delete a memory store
   129  func (r *BetaMemoryStoreService) Delete(ctx context.Context, memoryStoreID string, body BetaMemoryStoreDeleteParams, opts ...option.RequestOption) (res *BetaManagedAgentsDeletedMemoryStore, err error) {
   130  	for _, v := range body.Betas {
   131  		opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%v", v)))
   132  	}
   133  	if !param.IsOmitted(body.WorkspaceID) {
   134  		opts = append(opts, option.WithHeader("anthropic-workspace-id", fmt.Sprintf("%v", body.WorkspaceID.Value)))
   135  	}
   136  	opts = slices.Concat(r.Options, opts)
   137  	opts = append([]option.RequestOption{option.WithHeader("anthropic-beta", "agent-memory-2026-07-22")}, opts...)
   138  	if memoryStoreID == "" {
   139  		err = errors.New("missing required memory_store_id parameter")
   140  		return nil, err
   141  	}
   142  	path := fmt.Sprintf("v1/memory_stores/%s?beta=true", memoryStoreID)
   143  	err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, nil, &res, opts...)
   144  	return res, err
   145  }
   146  
   147  // Archive a memory store
   148  func (r *BetaMemoryStoreService) Archive(ctx context.Context, memoryStoreID string, body BetaMemoryStoreArchiveParams, opts ...option.RequestOption) (res *BetaManagedAgentsMemoryStore, err error) {
   149  	for _, v := range body.Betas {
   150  		opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%v", v)))
   151  	}
   152  	if !param.IsOmitted(body.WorkspaceID) {
   153  		opts = append(opts, option.WithHeader("anthropic-workspace-id", fmt.Sprintf("%v", body.WorkspaceID.Value)))
   154  	}
   155  	opts = slices.Concat(r.Options, opts)
   156  	opts = append([]option.RequestOption{option.WithHeader("anthropic-beta", "agent-memory-2026-07-22")}, opts...)
   157  	if memoryStoreID == "" {
   158  		err = errors.New("missing required memory_store_id parameter")
   159  		return nil, err
   160  	}
   161  	path := fmt.Sprintf("v1/memory_stores/%s/archive?beta=true", memoryStoreID)
   162  	err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, nil, &res, opts...)
   163  	return res, err
   164  }
   165  
   166  // Confirmation that a `memory_store` was deleted.
   167  type BetaManagedAgentsDeletedMemoryStore struct {
   168  	// ID of the deleted memory store (a `memstore_...` identifier). The store and all
   169  	// its memories and versions are no longer retrievable.
   170  	ID string `json:"id" api:"required"`
   171  	// Any of "memory_store_deleted".
   172  	Type BetaManagedAgentsDeletedMemoryStoreType `json:"type" api:"required"`
   173  	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
   174  	JSON struct {
   175  		ID          respjson.Field
   176  		Type        respjson.Field
   177  		ExtraFields map[string]respjson.Field
   178  		raw         string
   179  	} `json:"-"`
   180  }
   181  
   182  // Returns the unmodified JSON received from the API
   183  func (r BetaManagedAgentsDeletedMemoryStore) RawJSON() string { return r.JSON.raw }
   184  func (r *BetaManagedAgentsDeletedMemoryStore) UnmarshalJSON(data []byte) error {
   185  	return apijson.UnmarshalRoot(data, r)
   186  }
   187  
   188  type BetaManagedAgentsDeletedMemoryStoreType string
   189  
   190  const (
   191  	BetaManagedAgentsDeletedMemoryStoreTypeMemoryStoreDeleted BetaManagedAgentsDeletedMemoryStoreType = "memory_store_deleted"
   192  )
   193  
   194  // A `memory_store`: a named container for agent memories, scoped to a workspace.
   195  // Attach a store to a session via `resources[]` to mount it as a directory the
   196  // agent can read and write.
   197  type BetaManagedAgentsMemoryStore struct {
   198  	// Unique identifier for the memory store (a `memstore_...` tagged ID). Use this
   199  	// when attaching the store to a session, or in the `{memory_store_id}` path
   200  	// parameter of subsequent calls.
   201  	ID string `json:"id" api:"required"`
   202  	// A timestamp in RFC 3339 format
   203  	CreatedAt time.Time `json:"created_at" api:"required" format:"date-time"`
   204  	// Human-readable name for the store. 1–255 characters. The store's mount-path slug
   205  	// under `/mnt/memory/` is derived from this name.
   206  	Name string `json:"name" api:"required"`
   207  	// Any of "memory_store".
   208  	Type BetaManagedAgentsMemoryStoreType `json:"type" api:"required"`
   209  	// A timestamp in RFC 3339 format
   210  	UpdatedAt time.Time `json:"updated_at" api:"required" format:"date-time"`
   211  	// A timestamp in RFC 3339 format
   212  	ArchivedAt time.Time `json:"archived_at" api:"nullable" format:"date-time"`
   213  	// Free-text description of what the store contains, up to 1024 characters.
   214  	// Included in the agent's system prompt when the store is attached, so word it to
   215  	// be useful to the agent. Empty string when unset.
   216  	Description string `json:"description"`
   217  	// Arbitrary key-value tags for your own bookkeeping (such as the end user a store
   218  	// belongs to). Up to 16 pairs; keys 1–64 characters; values up to 512 characters.
   219  	// Returned on retrieve/list but not filterable.
   220  	Metadata map[string]string `json:"metadata"`
   221  	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
   222  	JSON struct {
   223  		ID          respjson.Field
   224  		CreatedAt   respjson.Field
   225  		Name        respjson.Field
   226  		Type        respjson.Field
   227  		UpdatedAt   respjson.Field
   228  		ArchivedAt  respjson.Field
   229  		Description respjson.Field
   230  		Metadata    respjson.Field
   231  		ExtraFields map[string]respjson.Field
   232  		raw         string
   233  	} `json:"-"`
   234  }
   235  
   236  // Returns the unmodified JSON received from the API
   237  func (r BetaManagedAgentsMemoryStore) RawJSON() string { return r.JSON.raw }
   238  func (r *BetaManagedAgentsMemoryStore) UnmarshalJSON(data []byte) error {
   239  	return apijson.UnmarshalRoot(data, r)
   240  }
   241  
   242  type BetaManagedAgentsMemoryStoreType string
   243  
   244  const (
   245  	BetaManagedAgentsMemoryStoreTypeMemoryStore BetaManagedAgentsMemoryStoreType = "memory_store"
   246  )
   247  
   248  type BetaMemoryStoreNewParams struct {
   249  	// Human-readable name for the store. Required; 1–255 characters; no control
   250  	// characters. The mount-path slug under `/mnt/memory/` is derived from this name
   251  	// (lowercased, non-alphanumeric runs collapsed to a hyphen). Names need not be
   252  	// unique within a workspace.
   253  	Name string `json:"name" api:"required"`
   254  	// Free-text description of what the store contains, up to 1024 characters.
   255  	// Included in the agent's system prompt when the store is attached, so word it to
   256  	// be useful to the agent.
   257  	Description param.Opt[string] `json:"description,omitzero"`
   258  	WorkspaceID param.Opt[string] `header:"anthropic-workspace-id,omitzero" json:"-"`
   259  	// Arbitrary key-value tags for your own bookkeeping (such as the end user a store
   260  	// belongs to). Up to 16 pairs; keys 1–64 characters; values up to 512 characters.
   261  	// Not visible to the agent.
   262  	Metadata map[string]string `json:"metadata,omitzero"`
   263  	// Optional header to specify the beta version(s) you want to use.
   264  	Betas []AnthropicBeta `header:"anthropic-beta,omitzero" json:"-"`
   265  	paramObj
   266  }
   267  
   268  func (r BetaMemoryStoreNewParams) MarshalJSON() (data []byte, err error) {
   269  	type shadow BetaMemoryStoreNewParams
   270  	return param.MarshalObject(r, (*shadow)(&r))
   271  }
   272  func (r *BetaMemoryStoreNewParams) UnmarshalJSON(data []byte) error {
   273  	return apijson.UnmarshalRoot(data, r)
   274  }
   275  
   276  type BetaMemoryStoreGetParams struct {
   277  	WorkspaceID param.Opt[string] `header:"anthropic-workspace-id,omitzero" json:"-"`
   278  	// Optional header to specify the beta version(s) you want to use.
   279  	Betas []AnthropicBeta `header:"anthropic-beta,omitzero" json:"-"`
   280  	paramObj
   281  }
   282  
   283  type BetaMemoryStoreUpdateParams struct {
   284  	// New description for the store, up to 1024 characters. Pass an empty string to
   285  	// clear it.
   286  	Description param.Opt[string] `json:"description,omitzero"`
   287  	// New human-readable name for the store. 1–255 characters; no control characters.
   288  	// Renaming changes the slug used for the store's `mount_path` in sessions created
   289  	// after the update.
   290  	Name        param.Opt[string] `json:"name,omitzero"`
   291  	WorkspaceID param.Opt[string] `header:"anthropic-workspace-id,omitzero" json:"-"`
   292  	// Metadata patch. Set a key to a string to upsert it, or to null to delete it.
   293  	// Omit the field to preserve. The stored bag is limited to 16 keys (up to 64 chars
   294  	// each) with values up to 512 chars.
   295  	Metadata map[string]string `json:"metadata,omitzero"`
   296  	// Optional header to specify the beta version(s) you want to use.
   297  	Betas []AnthropicBeta `header:"anthropic-beta,omitzero" json:"-"`
   298  	paramObj
   299  }
   300  
   301  func (r BetaMemoryStoreUpdateParams) MarshalJSON() (data []byte, err error) {
   302  	type shadow BetaMemoryStoreUpdateParams
   303  	return param.MarshalObject(r, (*shadow)(&r))
   304  }
   305  func (r *BetaMemoryStoreUpdateParams) UnmarshalJSON(data []byte) error {
   306  	return apijson.UnmarshalRoot(data, r)
   307  }
   308  
   309  type BetaMemoryStoreListParams struct {
   310  	// Return only stores whose `created_at` is at or after this time (inclusive). Sent
   311  	// on the wire as `created_at[gte]`.
   312  	CreatedAtGte param.Opt[time.Time] `query:"created_at[gte],omitzero" format:"date-time" json:"-"`
   313  	// Return only stores whose `created_at` is at or before this time (inclusive).
   314  	// Sent on the wire as `created_at[lte]`.
   315  	CreatedAtLte param.Opt[time.Time] `query:"created_at[lte],omitzero" format:"date-time" json:"-"`
   316  	// When `true`, archived stores are included in the results. Defaults to `false`
   317  	// (archived stores are excluded).
   318  	IncludeArchived param.Opt[bool] `query:"include_archived,omitzero" json:"-"`
   319  	// Maximum number of stores to return per page. Must be between 1 and 100. Defaults
   320  	// to 20 when omitted.
   321  	Limit param.Opt[int64] `query:"limit,omitzero" json:"-"`
   322  	// Opaque pagination cursor (a `page_...` value). Pass the `next_page` value from a
   323  	// previous response to fetch the next page; omit for the first page.
   324  	Page        param.Opt[string] `query:"page,omitzero" json:"-"`
   325  	WorkspaceID param.Opt[string] `header:"anthropic-workspace-id,omitzero" json:"-"`
   326  	// Optional header to specify the beta version(s) you want to use.
   327  	Betas []AnthropicBeta `header:"anthropic-beta,omitzero" json:"-"`
   328  	paramObj
   329  }
   330  
   331  // URLQuery serializes [BetaMemoryStoreListParams]'s query parameters as
   332  // `url.Values`.
   333  func (r BetaMemoryStoreListParams) URLQuery() (v url.Values, err error) {
   334  	return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
   335  		ArrayFormat:  apiquery.ArrayQueryFormatBrackets,
   336  		NestedFormat: apiquery.NestedQueryFormatBrackets,
   337  	})
   338  }
   339  
   340  type BetaMemoryStoreDeleteParams struct {
   341  	WorkspaceID param.Opt[string] `header:"anthropic-workspace-id,omitzero" json:"-"`
   342  	// Optional header to specify the beta version(s) you want to use.
   343  	Betas []AnthropicBeta `header:"anthropic-beta,omitzero" json:"-"`
   344  	paramObj
   345  }
   346  
   347  type BetaMemoryStoreArchiveParams struct {
   348  	WorkspaceID param.Opt[string] `header:"anthropic-workspace-id,omitzero" json:"-"`
   349  	// Optional header to specify the beta version(s) you want to use.
   350  	Betas []AnthropicBeta `header:"anthropic-beta,omitzero" json:"-"`
   351  	paramObj
   352  }