Repository navigation
Expand file tree
/
Copy pathcontext.go
More file actions
326 lines (285 loc) · 10.5 KB
/
Copy pathcontext.go
File metadata and controls
326 lines (285 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package cercago
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"slices"
"github.com/matrices/cerca-go/internal/apijson"
"github.com/matrices/cerca-go/internal/apiquery"
"github.com/matrices/cerca-go/internal/param"
"github.com/matrices/cerca-go/internal/requestconfig"
"github.com/matrices/cerca-go/option"
"github.com/matrices/cerca-go/packages/pagination"
)
// ContextService contains methods and other services that help with interacting
// with the cerca API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
// the [NewContextService] method instead.
type ContextService struct {
Options []option.RequestOption
}
// NewContextService generates a new service that applies the given options to each
// request. These options are applied after the parent client's options (if there
// is one), and before any request-specific options.
func NewContextService(opts ...option.RequestOption) (r *ContextService) {
r = &ContextService{}
r.Options = opts
return
}
// Retrieve context entry
func (r *ContextService) Get(ctx context.Context, agentID string, query ContextGetParams, opts ...option.RequestOption) (res *Entry, err error) {
opts = slices.Concat(r.Options, opts)
if agentID == "" {
err = errors.New("missing required agentId parameter")
return nil, err
}
path := fmt.Sprintf("agents/%s/context/entry", agentID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...)
return res, err
}
// List context entries
func (r *ContextService) List(ctx context.Context, agentID string, query ContextListParams, opts ...option.RequestOption) (res *pagination.EntriesCursorPage[EntrySummary], err error) {
var raw *http.Response
opts = slices.Concat(r.Options, opts)
opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
if agentID == "" {
err = errors.New("missing required agentId parameter")
return nil, err
}
path := fmt.Sprintf("agents/%s/context", agentID)
cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, query, &res, opts...)
if err != nil {
return nil, err
}
err = cfg.Execute()
if err != nil {
return nil, err
}
res.SetPageConfig(cfg, raw)
return res, nil
}
// List context entries
func (r *ContextService) ListAutoPaging(ctx context.Context, agentID string, query ContextListParams, opts ...option.RequestOption) *pagination.EntriesCursorPageAutoPager[EntrySummary] {
return pagination.NewEntriesCursorPageAutoPager(r.List(ctx, agentID, query, opts...))
}
// Delete context entry
func (r *ContextService) Delete(ctx context.Context, agentID string, body ContextDeleteParams, opts ...option.RequestOption) (res *ContextDeleteResponse, err error) {
opts = slices.Concat(r.Options, opts)
if agentID == "" {
err = errors.New("missing required agentId parameter")
return nil, err
}
path := fmt.Sprintf("agents/%s/context/entry", agentID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, body, &res, opts...)
return res, err
}
// Search context
func (r *ContextService) Search(ctx context.Context, agentID string, query ContextSearchParams, opts ...option.RequestOption) (res *pagination.ResultsCursorPage[SearchResult], err error) {
var raw *http.Response
opts = slices.Concat(r.Options, opts)
opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
if agentID == "" {
err = errors.New("missing required agentId parameter")
return nil, err
}
path := fmt.Sprintf("agents/%s/context/search", agentID)
cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, query, &res, opts...)
if err != nil {
return nil, err
}
err = cfg.Execute()
if err != nil {
return nil, err
}
res.SetPageConfig(cfg, raw)
return res, nil
}
// Search context
func (r *ContextService) SearchAutoPaging(ctx context.Context, agentID string, query ContextSearchParams, opts ...option.RequestOption) *pagination.ResultsCursorPageAutoPager[SearchResult] {
return pagination.NewResultsCursorPageAutoPager(r.Search(ctx, agentID, query, opts...))
}
// Update context entry
func (r *ContextService) Write(ctx context.Context, agentID string, body ContextWriteParams, opts ...option.RequestOption) (res *Entry, err error) {
opts = slices.Concat(r.Options, opts)
if agentID == "" {
err = errors.New("missing required agentId parameter")
return nil, err
}
path := fmt.Sprintf("agents/%s/context/entry", agentID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPut, path, body, &res, opts...)
return res, err
}
type Entry struct {
Content string `json:"content" api:"required"`
Key string `json:"key" api:"required"`
UpdatedAt string `json:"updatedAt" api:"required"`
Version float64 `json:"version" api:"required"`
MimeType string `json:"mimeType"`
JSON entryJSON `json:"-"`
}
// entryJSON contains the JSON metadata for the struct [Entry]
type entryJSON struct {
Content apijson.Field
Key apijson.Field
UpdatedAt apijson.Field
Version apijson.Field
MimeType apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *Entry) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r entryJSON) RawJSON() string {
return r.raw
}
type EntrySummary struct {
ByteLength float64 `json:"byteLength" api:"required"`
Key string `json:"key" api:"required"`
UpdatedAt string `json:"updatedAt" api:"required"`
Version float64 `json:"version" api:"required"`
MimeType string `json:"mimeType"`
JSON entrySummaryJSON `json:"-"`
}
// entrySummaryJSON contains the JSON metadata for the struct [EntrySummary]
type entrySummaryJSON struct {
ByteLength apijson.Field
Key apijson.Field
UpdatedAt apijson.Field
Version apijson.Field
MimeType apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *EntrySummary) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r entrySummaryJSON) RawJSON() string {
return r.raw
}
type SearchResult struct {
ByteLength float64 `json:"byteLength" api:"required"`
Key string `json:"key" api:"required"`
Score float64 `json:"score" api:"required"`
Snippet string `json:"snippet" api:"required"`
UpdatedAt string `json:"updatedAt" api:"required"`
Version float64 `json:"version" api:"required"`
MimeType string `json:"mimeType"`
JSON searchResultJSON `json:"-"`
}
// searchResultJSON contains the JSON metadata for the struct [SearchResult]
type searchResultJSON struct {
ByteLength apijson.Field
Key apijson.Field
Score apijson.Field
Snippet apijson.Field
UpdatedAt apijson.Field
Version apijson.Field
MimeType apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *SearchResult) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r searchResultJSON) RawJSON() string {
return r.raw
}
type ContextDeleteResponse struct {
Success ContextDeleteResponseSuccess `json:"success" api:"required"`
JSON contextDeleteResponseJSON `json:"-"`
}
// contextDeleteResponseJSON contains the JSON metadata for the struct
// [ContextDeleteResponse]
type contextDeleteResponseJSON struct {
Success apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *ContextDeleteResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r contextDeleteResponseJSON) RawJSON() string {
return r.raw
}
type ContextDeleteResponseSuccess bool
const (
ContextDeleteResponseSuccessTrue ContextDeleteResponseSuccess = true
)
func (r ContextDeleteResponseSuccess) IsKnown() bool {
switch r {
case ContextDeleteResponseSuccessTrue:
return true
}
return false
}
type ContextGetParams struct {
// Context entry key.
Key param.Field[string] `query:"key" api:"required"`
}
// URLQuery serializes [ContextGetParams]'s query parameters as `url.Values`.
func (r ContextGetParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatComma,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}
type ContextListParams struct {
// Opaque pagination cursor returned by a previous request.
Cursor param.Field[string] `query:"cursor"`
// Maximum number of items to return. Defaults to 20 and preserves parseInt
// semantics.
Limit param.Field[string] `query:"limit"`
// Optional key prefix filter.
Prefix param.Field[string] `query:"prefix"`
}
// URLQuery serializes [ContextListParams]'s query parameters as `url.Values`.
func (r ContextListParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatComma,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}
type ContextDeleteParams struct {
// Context entry key.
Key param.Field[string] `query:"key" api:"required"`
}
// URLQuery serializes [ContextDeleteParams]'s query parameters as `url.Values`.
func (r ContextDeleteParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatComma,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}
type ContextSearchParams struct {
// Search query.
Q param.Field[string] `query:"q" api:"required"`
// Opaque pagination cursor returned by a previous request.
Cursor param.Field[string] `query:"cursor"`
// Maximum number of items to return. Defaults to 20 and preserves parseInt
// semantics.
Limit param.Field[string] `query:"limit"`
// Optional key prefix filter.
Prefix param.Field[string] `query:"prefix"`
}
// URLQuery serializes [ContextSearchParams]'s query parameters as `url.Values`.
func (r ContextSearchParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatComma,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}
type ContextWriteParams struct {
Content param.Field[string] `json:"content" api:"required"`
Key param.Field[string] `json:"key" api:"required"`
ExpectedVersion param.Field[float64] `json:"expectedVersion"`
MimeType param.Field[string] `json:"mimeType"`
}
func (r ContextWriteParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}