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