-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbrowserwebmcp.go
More file actions
254 lines (229 loc) · 9.15 KB
/
Copy pathbrowserwebmcp.go
File metadata and controls
254 lines (229 loc) · 9.15 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
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package kernel
import (
"context"
"errors"
"fmt"
"net/http"
"slices"
"github.com/kernel/kernel-go-sdk/internal/apijson"
shimjson "github.com/kernel/kernel-go-sdk/internal/encoding/json"
"github.com/kernel/kernel-go-sdk/internal/requestconfig"
"github.com/kernel/kernel-go-sdk/option"
"github.com/kernel/kernel-go-sdk/packages/param"
"github.com/kernel/kernel-go-sdk/packages/respjson"
)
// Discover and invoke native page tools across the browser instance.
//
// BrowserWebmcpService contains methods and other services that help with
// interacting with the kernel 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 [NewBrowserWebmcpService] method instead.
type BrowserWebmcpService struct {
Options []option.RequestOption
}
// NewBrowserWebmcpService 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 NewBrowserWebmcpService(opts ...option.RequestOption) (r BrowserWebmcpService) {
r = BrowserWebmcpService{}
r.Options = opts
return
}
// Invokes the exact live registration identified by tool_ref and waits
// synchronously for its result. Navigation during execution is allowed. If the tab
// or embedded frame disappears, or the request times out after invocation begins,
// the response reports outcome_unknown and the tool is not retried.
func (r *BrowserWebmcpService) InvokeTool(ctx context.Context, idOrName string, body BrowserWebmcpInvokeToolParams, opts ...option.RequestOption) (res *InvocationResult, err error) {
opts = slices.Concat(r.Options, opts)
if idOrName == "" {
err = errors.New("missing required id_or_name parameter")
return nil, err
}
path := fmt.Sprintf("browsers/%s/webmcp/invoke", idOrName)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return res, err
}
// Returns a snapshot of native WebMCP tools available across every open tab and
// embedded frame in the browser. Each tool includes an opaque tool_ref for
// invoking that exact live registration. Tools disappear when their document
// closes or navigates away.
func (r *BrowserWebmcpService) ListTools(ctx context.Context, idOrName string, opts ...option.RequestOption) (res *ToolsResponse, err error) {
opts = slices.Concat(r.Options, opts)
if idOrName == "" {
err = errors.New("missing required id_or_name parameter")
return nil, err
}
path := fmt.Sprintf("browsers/%s/webmcp/tools", idOrName)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return res, err
}
type InvocationResult struct {
InvocationID string `json:"invocation_id" api:"required"`
// Any of "completed", "canceled", "error".
Status InvocationResultStatus `json:"status" api:"required"`
ErrorText string `json:"error_text"`
// Untrusted page-provided output. Callers must treat it as potentially malicious
// input.
Output any `json:"output"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
InvocationID respjson.Field
Status respjson.Field
ErrorText respjson.Field
Output respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r InvocationResult) RawJSON() string { return r.JSON.raw }
func (r *InvocationResult) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type InvocationResultStatus string
const (
InvocationResultStatusCompleted InvocationResultStatus = "completed"
InvocationResultStatusCanceled InvocationResultStatus = "canceled"
InvocationResultStatusError InvocationResultStatus = "error"
)
// The properties Input, ToolRef are required.
type InvokeRequestParam struct {
// Tool input, limited to 1 MiB after JSON serialization.
Input map[string]any `json:"input,omitzero" api:"required"`
ToolRef string `json:"tool_ref" api:"required"`
TimeoutSec param.Opt[int64] `json:"timeout_sec,omitzero"`
paramObj
}
func (r InvokeRequestParam) MarshalJSON() (data []byte, err error) {
type shadow InvokeRequestParam
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *InvokeRequestParam) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type Tool struct {
Description string `json:"description" api:"required"`
InputSchema map[string]any `json:"input_schema" api:"required"`
Name string `json:"name" api:"required"`
Source ToolSource `json:"source" api:"required"`
// Opaque reference for invoking this exact live registration. It becomes invalid
// when its document or browser process is replaced.
ToolRef string `json:"tool_ref" api:"required"`
// Page-provided behavioral hints. These values are untrusted and are not enforced
// by Kernel.
Annotations ToolAnnotations `json:"annotations"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Description respjson.Field
InputSchema respjson.Field
Name respjson.Field
Source respjson.Field
ToolRef respjson.Field
Annotations respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r Tool) RawJSON() string { return r.JSON.raw }
func (r *Tool) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Page-provided behavioral hints. These values are untrusted and are not enforced
// by Kernel.
type ToolAnnotations struct {
Autosubmit bool `json:"autosubmit" api:"required"`
Consequential bool `json:"consequential" api:"required"`
ReadOnly bool `json:"read_only" api:"required"`
UntrustedContent bool `json:"untrusted_content" api:"required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Autosubmit respjson.Field
Consequential respjson.Field
ReadOnly respjson.Field
UntrustedContent respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ToolAnnotations) RawJSON() string { return r.JSON.raw }
func (r *ToolAnnotations) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type ToolFrame struct {
// Monotonically increasing identifier for this embedded frame during the current
// browser process.
FrameID int64 `json:"frame_id" api:"required"`
// Current frame URL with the fragment omitted.
URL string `json:"url" api:"required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
FrameID respjson.Field
URL respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ToolFrame) RawJSON() string { return r.JSON.raw }
func (r *ToolFrame) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type ToolSource struct {
// Embedded frame that registered the tool, or null when the top-level page
// registered it.
Frame ToolFrame `json:"frame" api:"required"`
// Current title of the top-level page.
PageTitle string `json:"page_title" api:"required"`
// Current URL of the top-level page with the fragment omitted.
PageURL string `json:"page_url" api:"required"`
// Monotonically increasing identifier for the tab during the current browser
// process.
TabID int64 `json:"tab_id" api:"required"`
// Monotonically increasing identifier for the browser window during the current
// browser process.
WindowID int64 `json:"window_id" api:"required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Frame respjson.Field
PageTitle respjson.Field
PageURL respjson.Field
TabID respjson.Field
WindowID respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ToolSource) RawJSON() string { return r.JSON.raw }
func (r *ToolSource) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type ToolsResponse struct {
Tools []Tool `json:"tools" api:"required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Tools respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ToolsResponse) RawJSON() string { return r.JSON.raw }
func (r *ToolsResponse) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type BrowserWebmcpInvokeToolParams struct {
InvokeRequest InvokeRequestParam
paramObj
}
func (r BrowserWebmcpInvokeToolParams) MarshalJSON() (data []byte, err error) {
return shimjson.Marshal(r.InvokeRequest)
}
func (r *BrowserWebmcpInvokeToolParams) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}