@slates/client 1.0.0-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +409 -0
- package/dist/index.d.cts +764 -0
- package/dist/index.d.ts +764 -0
- package/dist/index.module.js +383 -0
- package/package.json +43 -0
- package/src/client.test.ts +569 -0
- package/src/client.ts +367 -0
- package/src/error.ts +157 -0
- package/src/index.ts +10 -0
- package/src/transport.ts +57 -0
- package/src/types.ts +36 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
SlateProtocolError: () => SlateProtocolError,
|
|
24
|
+
SlatesProtocolClient: () => SlatesProtocolClient,
|
|
25
|
+
createLocalSlateTransport: () => createLocalSlateTransport,
|
|
26
|
+
createSlatesClient: () => createSlatesClient
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(index_exports);
|
|
29
|
+
|
|
30
|
+
// src/client.ts
|
|
31
|
+
var import_proto = require("@slates/proto");
|
|
32
|
+
var import_crypto = require("crypto");
|
|
33
|
+
|
|
34
|
+
// src/error.ts
|
|
35
|
+
var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
36
|
+
var isSlateErrorResponse = (error) => isRecord(error) && typeof error.code === "string" && typeof error.message === "string";
|
|
37
|
+
var inferKindFromCode = (code) => {
|
|
38
|
+
if (code.startsWith("declaration.")) return "declaration";
|
|
39
|
+
if (code.startsWith("input.")) return "validation";
|
|
40
|
+
if (code.startsWith("request.")) return "request";
|
|
41
|
+
if (code.startsWith("config.")) return "config";
|
|
42
|
+
if (code.startsWith("auth.") || code.startsWith("permission.")) return "auth";
|
|
43
|
+
if (code.startsWith("resource.")) return "resource";
|
|
44
|
+
if (code.startsWith("payment.")) return "payment";
|
|
45
|
+
if (code.startsWith("transport.")) return "transport";
|
|
46
|
+
if (code.startsWith("upstream.")) return "upstream";
|
|
47
|
+
return "internal";
|
|
48
|
+
};
|
|
49
|
+
var normalizeResponse = (error, source, defaults = {}) => {
|
|
50
|
+
if (isSlateErrorResponse(error)) {
|
|
51
|
+
return {
|
|
52
|
+
...defaults,
|
|
53
|
+
...error,
|
|
54
|
+
kind: error.kind ?? inferKindFromCode(error.code)
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
if (isRecord(error) && typeof error.message === "string") {
|
|
58
|
+
let code2 = typeof error.code === "string" ? error.code : defaults.code ?? (source === "transport" ? "transport.invoke_failed" : "internal.unexpected");
|
|
59
|
+
return {
|
|
60
|
+
...defaults,
|
|
61
|
+
...error,
|
|
62
|
+
code: code2,
|
|
63
|
+
message: error.message,
|
|
64
|
+
kind: (typeof error.kind === "string" ? error.kind : defaults.kind) ?? inferKindFromCode(code2)
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
let code = defaults.code ?? (source === "transport" ? "transport.invoke_failed" : "internal.unexpected");
|
|
68
|
+
return {
|
|
69
|
+
...defaults,
|
|
70
|
+
code,
|
|
71
|
+
message: error instanceof Error ? error.message : defaults.message ?? "The slate returned an unexpected error.",
|
|
72
|
+
kind: defaults.kind ?? inferKindFromCode(code),
|
|
73
|
+
baggage: {
|
|
74
|
+
...defaults.baggage ?? {},
|
|
75
|
+
...error instanceof Error ? { originalName: error.name } : { originalValue: error }
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
var SlateProtocolError = class _SlateProtocolError extends Error {
|
|
80
|
+
cause;
|
|
81
|
+
data;
|
|
82
|
+
source;
|
|
83
|
+
constructor(data, source = "provider", cause) {
|
|
84
|
+
super(data.message);
|
|
85
|
+
this.name = "SlateProtocolError";
|
|
86
|
+
this.data = data;
|
|
87
|
+
this.source = source;
|
|
88
|
+
this.cause = cause;
|
|
89
|
+
}
|
|
90
|
+
get code() {
|
|
91
|
+
return this.data.code;
|
|
92
|
+
}
|
|
93
|
+
get kind() {
|
|
94
|
+
return this.data.kind;
|
|
95
|
+
}
|
|
96
|
+
get retryable() {
|
|
97
|
+
return this.data.retryable;
|
|
98
|
+
}
|
|
99
|
+
get status() {
|
|
100
|
+
return this.data.status;
|
|
101
|
+
}
|
|
102
|
+
toJSON() {
|
|
103
|
+
return {
|
|
104
|
+
...this.data,
|
|
105
|
+
source: this.source
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
static is(error) {
|
|
109
|
+
return error instanceof _SlateProtocolError;
|
|
110
|
+
}
|
|
111
|
+
static fromResponse(error, source = "provider") {
|
|
112
|
+
if (_SlateProtocolError.is(error)) return error;
|
|
113
|
+
return new _SlateProtocolError(normalizeResponse(error, source), source, error);
|
|
114
|
+
}
|
|
115
|
+
static fromUnknown(error, defaults = {}, source = "transport") {
|
|
116
|
+
if (_SlateProtocolError.is(error)) return error;
|
|
117
|
+
return new _SlateProtocolError(normalizeResponse(error, source, defaults), source, error);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
// src/client.ts
|
|
122
|
+
var createDefaultParticipants = () => [
|
|
123
|
+
{
|
|
124
|
+
type: "consumer",
|
|
125
|
+
id: "slates-client",
|
|
126
|
+
name: "Slates Client"
|
|
127
|
+
}
|
|
128
|
+
];
|
|
129
|
+
var SlatesProtocolClient = class {
|
|
130
|
+
transport;
|
|
131
|
+
state;
|
|
132
|
+
constructor(opts) {
|
|
133
|
+
this.transport = opts.transport;
|
|
134
|
+
this.state = {
|
|
135
|
+
protocol: import_proto.SLATES_PROTOCOL_VERSION,
|
|
136
|
+
participants: opts.participants ?? createDefaultParticipants(),
|
|
137
|
+
config: opts.state?.config ?? null,
|
|
138
|
+
auth: opts.state?.auth ?? null,
|
|
139
|
+
session: opts.state?.session ?? null
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
setParticipants(participants) {
|
|
143
|
+
this.state.participants = participants;
|
|
144
|
+
return this;
|
|
145
|
+
}
|
|
146
|
+
setConfig(config) {
|
|
147
|
+
this.state.config = config;
|
|
148
|
+
return this;
|
|
149
|
+
}
|
|
150
|
+
setAuth(auth) {
|
|
151
|
+
this.state.auth = auth;
|
|
152
|
+
return this;
|
|
153
|
+
}
|
|
154
|
+
clearAuth() {
|
|
155
|
+
this.state.auth = null;
|
|
156
|
+
return this;
|
|
157
|
+
}
|
|
158
|
+
setSession(session) {
|
|
159
|
+
this.state.session = session;
|
|
160
|
+
return this;
|
|
161
|
+
}
|
|
162
|
+
ensureSession() {
|
|
163
|
+
if (!this.state.session) {
|
|
164
|
+
this.state.session = {
|
|
165
|
+
id: (0, import_crypto.randomUUID)(),
|
|
166
|
+
state: {}
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
return this.state.session;
|
|
170
|
+
}
|
|
171
|
+
buildStateMessages() {
|
|
172
|
+
return [
|
|
173
|
+
{
|
|
174
|
+
jsonrpc: "2.0",
|
|
175
|
+
method: "slates/hello",
|
|
176
|
+
params: { protocol: this.state.protocol }
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
jsonrpc: "2.0",
|
|
180
|
+
method: "slates/participant.set",
|
|
181
|
+
params: { participants: this.state.participants }
|
|
182
|
+
},
|
|
183
|
+
...this.state.config ? [
|
|
184
|
+
{
|
|
185
|
+
jsonrpc: "2.0",
|
|
186
|
+
method: "slates/config.set",
|
|
187
|
+
params: { config: this.state.config }
|
|
188
|
+
}
|
|
189
|
+
] : [],
|
|
190
|
+
...this.state.auth ? [
|
|
191
|
+
{
|
|
192
|
+
jsonrpc: "2.0",
|
|
193
|
+
method: "slates/auth.set",
|
|
194
|
+
params: {
|
|
195
|
+
authenticationMethodId: this.state.auth.authenticationMethodId,
|
|
196
|
+
output: this.state.auth.output
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
] : [],
|
|
200
|
+
...this.state.session ? [
|
|
201
|
+
{
|
|
202
|
+
jsonrpc: "2.0",
|
|
203
|
+
method: "slates/session.start",
|
|
204
|
+
params: {
|
|
205
|
+
sessionId: this.state.session.id,
|
|
206
|
+
state: this.state.session.state
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
] : []
|
|
210
|
+
];
|
|
211
|
+
}
|
|
212
|
+
async request(method, params) {
|
|
213
|
+
let id = (0, import_crypto.randomUUID)();
|
|
214
|
+
let responses = await this.transport.send([
|
|
215
|
+
...this.buildStateMessages(),
|
|
216
|
+
{
|
|
217
|
+
jsonrpc: "2.0",
|
|
218
|
+
id,
|
|
219
|
+
method,
|
|
220
|
+
params
|
|
221
|
+
}
|
|
222
|
+
]);
|
|
223
|
+
let response = responses.find((message) => "id" in message && message.id === id);
|
|
224
|
+
if (!response) {
|
|
225
|
+
throw new Error(`No response was returned for method ${String(method)}.`);
|
|
226
|
+
}
|
|
227
|
+
if (response.error) {
|
|
228
|
+
throw SlateProtocolError.fromResponse(response.error);
|
|
229
|
+
}
|
|
230
|
+
return response.result;
|
|
231
|
+
}
|
|
232
|
+
async identify() {
|
|
233
|
+
return this.request("slates/provider.identify", {});
|
|
234
|
+
}
|
|
235
|
+
async listActions() {
|
|
236
|
+
return this.request("slates/actions.list", {});
|
|
237
|
+
}
|
|
238
|
+
async listTools() {
|
|
239
|
+
let result = await this.listActions();
|
|
240
|
+
return result.actions.filter((action) => action.type === "action.tool");
|
|
241
|
+
}
|
|
242
|
+
async listTriggers() {
|
|
243
|
+
let result = await this.listActions();
|
|
244
|
+
return result.actions.filter((action) => action.type === "action.trigger");
|
|
245
|
+
}
|
|
246
|
+
async getAction(actionId) {
|
|
247
|
+
return this.request("slates/action.get", { actionId });
|
|
248
|
+
}
|
|
249
|
+
async getTool(actionId) {
|
|
250
|
+
let result = await this.getAction(actionId);
|
|
251
|
+
if (result.action.type !== "action.tool") {
|
|
252
|
+
throw new Error(`Action ${actionId} is not a tool.`);
|
|
253
|
+
}
|
|
254
|
+
return result.action;
|
|
255
|
+
}
|
|
256
|
+
async getTrigger(actionId) {
|
|
257
|
+
let result = await this.getAction(actionId);
|
|
258
|
+
if (result.action.type !== "action.trigger") {
|
|
259
|
+
throw new Error(`Action ${actionId} is not a trigger.`);
|
|
260
|
+
}
|
|
261
|
+
return result.action;
|
|
262
|
+
}
|
|
263
|
+
async getConfigSchema() {
|
|
264
|
+
return this.request("slates/config.schema.get", {});
|
|
265
|
+
}
|
|
266
|
+
async getDefaultConfig() {
|
|
267
|
+
return this.request("slates/config.get_default", {});
|
|
268
|
+
}
|
|
269
|
+
async updateConfig(previousConfig, newConfig) {
|
|
270
|
+
return this.request("slates/config.changed", {
|
|
271
|
+
previousConfig,
|
|
272
|
+
newConfig
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
async listAuthMethods() {
|
|
276
|
+
return this.request("slates/auth.methods.list", {});
|
|
277
|
+
}
|
|
278
|
+
async getAuthMethod(authenticationMethodId) {
|
|
279
|
+
return this.request("slates/auth.method.get", {
|
|
280
|
+
authenticationMethodId
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
async getDefaultAuthInput(authenticationMethodId) {
|
|
284
|
+
return this.request("slates/auth.input.get_default", {
|
|
285
|
+
authenticationMethodId
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
async updateAuthInput(d) {
|
|
289
|
+
return this.request("slates/auth.input.changed", {
|
|
290
|
+
authenticationMethodId: d.authenticationMethodId,
|
|
291
|
+
previousInput: d.previousInput,
|
|
292
|
+
newInput: d.newInput
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
async getAuthOutput(d) {
|
|
296
|
+
return this.request("slates/auth.output.get", {
|
|
297
|
+
authenticationMethodId: d.authenticationMethodId,
|
|
298
|
+
input: d.input
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
async getAuthorizationUrl(d) {
|
|
302
|
+
return this.request("slates/auth.authorization_url.get", d);
|
|
303
|
+
}
|
|
304
|
+
async handleAuthorizationCallback(d) {
|
|
305
|
+
return this.request("slates/auth.authorization_callback.handle", d);
|
|
306
|
+
}
|
|
307
|
+
async refreshToken(d) {
|
|
308
|
+
return this.request("slates/auth.token_refresh.handle", d);
|
|
309
|
+
}
|
|
310
|
+
async getAuthProfile(d) {
|
|
311
|
+
return this.request("slates/auth.profile.get", d);
|
|
312
|
+
}
|
|
313
|
+
async invokeTool(actionId, input) {
|
|
314
|
+
this.ensureSession();
|
|
315
|
+
return this.request("slates/action.tool.invoke", {
|
|
316
|
+
actionId,
|
|
317
|
+
input
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
async mapTriggerEvent(actionId, input) {
|
|
321
|
+
this.ensureSession();
|
|
322
|
+
return this.request("slates/action.trigger.map_event", {
|
|
323
|
+
actionId,
|
|
324
|
+
input
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
async registerTriggerWebhook(actionId, webhookBaseUrl) {
|
|
328
|
+
this.ensureSession();
|
|
329
|
+
return this.request("slates/action.trigger.webhook_register", {
|
|
330
|
+
actionId,
|
|
331
|
+
webhookBaseUrl
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
async handleTriggerWebhook(d) {
|
|
335
|
+
this.ensureSession();
|
|
336
|
+
let encodedBody = typeof d.body === "string" ? Buffer.from(d.body, "utf-8").toString("base64") : d.body ? Buffer.from(d.body).toString("base64") : null;
|
|
337
|
+
return this.request("slates/action.trigger.webhook_handle", {
|
|
338
|
+
actionId: d.actionId,
|
|
339
|
+
url: d.url,
|
|
340
|
+
method: d.method,
|
|
341
|
+
headers: d.headers ?? {},
|
|
342
|
+
body: encodedBody ? {
|
|
343
|
+
encoding: "base64",
|
|
344
|
+
content: encodedBody
|
|
345
|
+
} : null,
|
|
346
|
+
state: d.state ?? null
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
async unregisterTriggerWebhook(d) {
|
|
350
|
+
this.ensureSession();
|
|
351
|
+
return this.request("slates/action.trigger.webhook_unregister", {
|
|
352
|
+
actionId: d.actionId,
|
|
353
|
+
webhookBaseUrl: d.webhookBaseUrl,
|
|
354
|
+
registrationDetails: d.registrationDetails,
|
|
355
|
+
state: d.state ?? null
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
async close() {
|
|
359
|
+
await this.transport.close?.();
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
// src/transport.ts
|
|
364
|
+
var import_proto2 = require("@slates/proto");
|
|
365
|
+
var import_provider_handler = require("@slates/provider-handler");
|
|
366
|
+
var toTransportError = (value, defaultMessage) => SlateProtocolError.fromUnknown(
|
|
367
|
+
value,
|
|
368
|
+
{
|
|
369
|
+
code: "transport.invoke_failed",
|
|
370
|
+
kind: "transport",
|
|
371
|
+
message: defaultMessage,
|
|
372
|
+
retryable: true,
|
|
373
|
+
baggage: {
|
|
374
|
+
response: value
|
|
375
|
+
}
|
|
376
|
+
},
|
|
377
|
+
"transport"
|
|
378
|
+
);
|
|
379
|
+
var createLocalSlateTransport = (d) => {
|
|
380
|
+
let managerPromise = (0, import_provider_handler.createProviderHandler)(d.slate, d.listeners ?? []).run();
|
|
381
|
+
return {
|
|
382
|
+
async send(messages) {
|
|
383
|
+
let manager = await managerPromise;
|
|
384
|
+
let responses = [];
|
|
385
|
+
for (let message of messages) {
|
|
386
|
+
let response;
|
|
387
|
+
try {
|
|
388
|
+
response = await import_proto2.SlatesProviderProtoHandlerManager.handleInput(manager, message);
|
|
389
|
+
} catch (error) {
|
|
390
|
+
throw toTransportError(error, "Local slate invocation failed");
|
|
391
|
+
}
|
|
392
|
+
if (response) {
|
|
393
|
+
responses.push(response);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
return responses;
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
// src/index.ts
|
|
402
|
+
var createSlatesClient = (opts) => new SlatesProtocolClient(opts);
|
|
403
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
404
|
+
0 && (module.exports = {
|
|
405
|
+
SlateProtocolError,
|
|
406
|
+
SlatesProtocolClient,
|
|
407
|
+
createLocalSlateTransport,
|
|
408
|
+
createSlatesClient
|
|
409
|
+
});
|