@simple-ai-lab/sdk 0.4.0 → 0.6.0
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/README.md +54 -1
- package/dist/actions.cjs +438 -0
- package/dist/actions.d.cts +336 -0
- package/dist/actions.d.ts +336 -0
- package/dist/actions.js +429 -0
- package/dist/agents.cjs +1054 -0
- package/dist/agents.d.cts +1016 -0
- package/dist/agents.d.ts +1016 -0
- package/dist/agents.js +1044 -0
- package/dist/built-in-actions.cjs +356 -0
- package/dist/built-in-actions.d.cts +41 -0
- package/dist/built-in-actions.d.ts +41 -0
- package/dist/built-in-actions.js +353 -0
- package/dist/client.cjs +29 -0
- package/dist/client.d.cts +15 -0
- package/dist/client.d.ts +15 -0
- package/dist/client.js +25 -0
- package/dist/code-action/defaults.cjs +11 -0
- package/dist/code-action/defaults.d.cts +7 -0
- package/dist/code-action/defaults.d.ts +7 -0
- package/dist/code-action/defaults.js +8 -0
- package/dist/code-action/define-action.cjs +74 -0
- package/dist/code-action/define-action.d.cts +94 -0
- package/dist/code-action/define-action.d.ts +94 -0
- package/dist/code-action/define-action.js +69 -0
- package/dist/code-action/index.cjs +11 -0
- package/dist/code-action/index.d.cts +4 -0
- package/dist/code-action/index.d.ts +4 -0
- package/dist/code-action/index.js +3 -0
- package/dist/code-action/types.cjs +13 -0
- package/dist/code-action/types.d.cts +13 -0
- package/dist/code-action/types.d.ts +13 -0
- package/dist/code-action/types.js +10 -0
- package/dist/code-action/workflow.cjs +29 -0
- package/dist/code-action/workflow.d.cts +53 -0
- package/dist/code-action/workflow.d.ts +53 -0
- package/dist/code-action/workflow.js +26 -0
- package/dist/data.cjs +98 -0
- package/dist/data.d.cts +44 -0
- package/dist/data.d.ts +44 -0
- package/dist/data.js +94 -0
- package/dist/http.cjs +65 -0
- package/dist/http.d.cts +14 -0
- package/dist/http.d.ts +14 -0
- package/dist/http.js +62 -0
- package/dist/index.cjs +68 -1597
- package/dist/index.d.cts +12 -780
- package/dist/index.d.ts +12 -780
- package/dist/index.js +10 -1311
- package/dist/projects.cjs +234 -0
- package/dist/projects.d.cts +24 -0
- package/dist/projects.d.ts +24 -0
- package/dist/projects.js +228 -0
- package/dist/schemas.cjs +50 -0
- package/dist/schemas.d.cts +81 -0
- package/dist/schemas.d.ts +81 -0
- package/dist/schemas.js +47 -0
- package/dist/shared/errors.cjs +31 -0
- package/dist/shared/errors.d.cts +22 -0
- package/dist/shared/errors.d.ts +22 -0
- package/dist/shared/errors.js +26 -0
- package/package.json +27 -31
- package/dist/chunk-V4E5PSKB.js +0 -267
- package/dist/chunk-V4E5PSKB.js.map +0 -1
- package/dist/code-action-CDt77m7I.d.cts +0 -191
- package/dist/code-action-CDt77m7I.d.ts +0 -191
- package/dist/code-action.cjs +0 -282
- package/dist/code-action.cjs.map +0 -1
- package/dist/code-action.d.cts +0 -2
- package/dist/code-action.d.ts +0 -2
- package/dist/code-action.js +0 -9
- package/dist/code-action.js.map +0 -1
- package/dist/index.cjs.map +0 -1
- package/dist/index.js.map +0 -1
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
/*
|
|
3
|
+
* Agent v2 built-in Action schemas and output mappings, mirrored from
|
|
4
|
+
* `@simple-ai-lab/shared/public/built-in-actions`. Agent v2 requires these
|
|
5
|
+
* definitions to match when saving an Action version. SDK parity tests compare
|
|
6
|
+
* the definitions without adding a private dependency to the published SDK.
|
|
7
|
+
*/
|
|
8
|
+
const sendEmailOperationSchema = z.strictObject({
|
|
9
|
+
type: z.literal('send_email'),
|
|
10
|
+
sendingDomainUuid: z.uuid(),
|
|
11
|
+
fromLocalPart: z
|
|
12
|
+
.string()
|
|
13
|
+
.trim()
|
|
14
|
+
.min(1)
|
|
15
|
+
.max(64)
|
|
16
|
+
.regex(/^[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*$/, {
|
|
17
|
+
message: 'From address may only contain letters, digits, and . _ % + - characters',
|
|
18
|
+
}),
|
|
19
|
+
fromDisplayName: z
|
|
20
|
+
.string()
|
|
21
|
+
.trim()
|
|
22
|
+
.min(1)
|
|
23
|
+
.max(128)
|
|
24
|
+
.regex(/^[^\r\n]+$/, { message: 'Display name may not contain line breaks' })
|
|
25
|
+
.nullable(),
|
|
26
|
+
});
|
|
27
|
+
const sendTextMessageOperationSchema = z.strictObject({
|
|
28
|
+
type: z.literal('send_text_message'),
|
|
29
|
+
senderPhoneNumberUuid: z.uuid(),
|
|
30
|
+
});
|
|
31
|
+
const validateAddressOperationSchema = z.strictObject({
|
|
32
|
+
type: z.literal('validate_address'),
|
|
33
|
+
});
|
|
34
|
+
/** Mirrors Agent v2 operation boundary; no private runtime dependency. */
|
|
35
|
+
export const builtInOperationSchema = z.discriminatedUnion('type', [
|
|
36
|
+
sendTextMessageOperationSchema,
|
|
37
|
+
sendEmailOperationSchema,
|
|
38
|
+
validateAddressOperationSchema,
|
|
39
|
+
]);
|
|
40
|
+
export const SEND_EMAIL_INPUT_SCHEMA = {
|
|
41
|
+
type: 'object',
|
|
42
|
+
description: 'The recipient and content supplied each time the action runs. The sender is configured on the action.',
|
|
43
|
+
properties: {
|
|
44
|
+
to: {
|
|
45
|
+
type: 'string',
|
|
46
|
+
description: 'The recipient email address, or a comma-separated list of addresses.',
|
|
47
|
+
nullable: false,
|
|
48
|
+
},
|
|
49
|
+
subject: {
|
|
50
|
+
type: 'string',
|
|
51
|
+
description: 'The subject line. Input mappings may use Liquid templates.',
|
|
52
|
+
nullable: false,
|
|
53
|
+
},
|
|
54
|
+
body: {
|
|
55
|
+
type: 'string',
|
|
56
|
+
description: 'The plain-text body. Input mappings may use Liquid templates.',
|
|
57
|
+
nullable: false,
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
required: ['to', 'subject', 'body'],
|
|
61
|
+
additionalProperties: false,
|
|
62
|
+
};
|
|
63
|
+
export const SEND_EMAIL_OUTPUT_SCHEMA = {
|
|
64
|
+
type: 'object',
|
|
65
|
+
description: 'The email provider acceptance result.',
|
|
66
|
+
properties: {
|
|
67
|
+
message_id: {
|
|
68
|
+
type: 'string',
|
|
69
|
+
description: 'The opaque provider message identifier.',
|
|
70
|
+
nullable: false,
|
|
71
|
+
},
|
|
72
|
+
status: {
|
|
73
|
+
type: 'string',
|
|
74
|
+
description: 'The provider acceptance status.',
|
|
75
|
+
nullable: false,
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
required: ['message_id', 'status'],
|
|
79
|
+
additionalProperties: false,
|
|
80
|
+
};
|
|
81
|
+
export const SEND_EMAIL_OUTPUT_MAPPINGS = {
|
|
82
|
+
'/message_id': '{{ result.message_id }}',
|
|
83
|
+
'/status': '{{ result.status }}',
|
|
84
|
+
};
|
|
85
|
+
export const SEND_TEXT_MESSAGE_INPUT_SCHEMA = {
|
|
86
|
+
type: 'object',
|
|
87
|
+
description: 'The recipient and content supplied each time the action runs. The sender is configured on the action.',
|
|
88
|
+
properties: {
|
|
89
|
+
to: {
|
|
90
|
+
type: 'string',
|
|
91
|
+
description: 'The recipient phone number in E.164 format.',
|
|
92
|
+
nullable: false,
|
|
93
|
+
},
|
|
94
|
+
message: {
|
|
95
|
+
type: 'string',
|
|
96
|
+
description: 'The message to send. Input mappings may use Liquid templates.',
|
|
97
|
+
nullable: false,
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
required: ['to', 'message'],
|
|
101
|
+
additionalProperties: false,
|
|
102
|
+
};
|
|
103
|
+
export const SEND_TEXT_MESSAGE_OUTPUT_SCHEMA = {
|
|
104
|
+
type: 'object',
|
|
105
|
+
description: 'The messaging provider acceptance result.',
|
|
106
|
+
properties: {
|
|
107
|
+
message_id: {
|
|
108
|
+
type: 'string',
|
|
109
|
+
description: 'The opaque provider message identifier.',
|
|
110
|
+
nullable: false,
|
|
111
|
+
},
|
|
112
|
+
status: {
|
|
113
|
+
type: 'string',
|
|
114
|
+
description: 'The provider acceptance status.',
|
|
115
|
+
nullable: false,
|
|
116
|
+
},
|
|
117
|
+
channel: {
|
|
118
|
+
type: 'string',
|
|
119
|
+
description: "The delivery channel selected from the sender's messaging configuration.",
|
|
120
|
+
nullable: false,
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
required: ['message_id', 'status', 'channel'],
|
|
124
|
+
additionalProperties: false,
|
|
125
|
+
};
|
|
126
|
+
export const SEND_TEXT_MESSAGE_OUTPUT_MAPPINGS = {
|
|
127
|
+
'/message_id': '{{ result.message_id }}',
|
|
128
|
+
'/status': '{{ result.status }}',
|
|
129
|
+
'/channel': '{{ result.channel }}',
|
|
130
|
+
};
|
|
131
|
+
export const VALIDATE_ADDRESS_INPUT_SCHEMA = {
|
|
132
|
+
type: 'object',
|
|
133
|
+
description: 'The complete address to validate and standardize.',
|
|
134
|
+
properties: {
|
|
135
|
+
address_line_1: {
|
|
136
|
+
type: 'string',
|
|
137
|
+
description: 'The street address, or the complete unstructured address when no component hints are known.',
|
|
138
|
+
nullable: false,
|
|
139
|
+
},
|
|
140
|
+
address_line_2: {
|
|
141
|
+
type: 'string',
|
|
142
|
+
description: 'An apartment, suite, or secondary address line. Use null or an empty string when unknown.',
|
|
143
|
+
nullable: true,
|
|
144
|
+
},
|
|
145
|
+
city: {
|
|
146
|
+
type: 'string',
|
|
147
|
+
description: 'The city or locality used to narrow validation. Use null or an empty string when unknown.',
|
|
148
|
+
nullable: true,
|
|
149
|
+
},
|
|
150
|
+
region: {
|
|
151
|
+
type: 'string',
|
|
152
|
+
description: 'The state, province, or administrative area used to narrow validation. Use null or an empty string when unknown.',
|
|
153
|
+
nullable: true,
|
|
154
|
+
},
|
|
155
|
+
postal_code: {
|
|
156
|
+
type: 'string',
|
|
157
|
+
description: 'The postal code used to narrow validation. Use null or an empty string when unknown.',
|
|
158
|
+
nullable: true,
|
|
159
|
+
},
|
|
160
|
+
country_code: {
|
|
161
|
+
type: 'string',
|
|
162
|
+
description: 'The two-letter country code used to narrow validation. Use null or an empty string when unknown.',
|
|
163
|
+
nullable: true,
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
required: ['address_line_1', 'address_line_2', 'city', 'region', 'postal_code', 'country_code'],
|
|
167
|
+
additionalProperties: false,
|
|
168
|
+
};
|
|
169
|
+
const addressOutputProperties = {
|
|
170
|
+
formatted_address: {
|
|
171
|
+
type: 'string',
|
|
172
|
+
description: 'The complete standardized address formatted for display or confirmation.',
|
|
173
|
+
nullable: false,
|
|
174
|
+
},
|
|
175
|
+
address_line_1: {
|
|
176
|
+
type: 'string',
|
|
177
|
+
description: 'The standardized primary street-address line.',
|
|
178
|
+
nullable: false,
|
|
179
|
+
},
|
|
180
|
+
address_line_2: {
|
|
181
|
+
type: 'string',
|
|
182
|
+
description: 'The standardized apartment, suite, or secondary address line, or an empty string.',
|
|
183
|
+
nullable: false,
|
|
184
|
+
},
|
|
185
|
+
city: {
|
|
186
|
+
type: 'string',
|
|
187
|
+
description: 'The validated city.',
|
|
188
|
+
nullable: false,
|
|
189
|
+
},
|
|
190
|
+
region: {
|
|
191
|
+
type: 'string',
|
|
192
|
+
description: 'The standardized state, province, or administrative-area abbreviation.',
|
|
193
|
+
nullable: false,
|
|
194
|
+
},
|
|
195
|
+
postal_code: {
|
|
196
|
+
type: 'string',
|
|
197
|
+
description: 'The complete standardized postal code.',
|
|
198
|
+
nullable: false,
|
|
199
|
+
},
|
|
200
|
+
country_code: {
|
|
201
|
+
type: 'string',
|
|
202
|
+
description: 'The two-letter country code, or an empty string when unavailable.',
|
|
203
|
+
nullable: false,
|
|
204
|
+
},
|
|
205
|
+
latitude: {
|
|
206
|
+
type: 'number',
|
|
207
|
+
description: 'The validated latitude, or null when Google does not return a geocode.',
|
|
208
|
+
nullable: true,
|
|
209
|
+
},
|
|
210
|
+
longitude: {
|
|
211
|
+
type: 'number',
|
|
212
|
+
description: 'The validated longitude, or null when Google does not return a geocode.',
|
|
213
|
+
nullable: true,
|
|
214
|
+
},
|
|
215
|
+
validation_status: {
|
|
216
|
+
type: 'string',
|
|
217
|
+
description: 'Whether the address is confirmed, needs confirmation, is incomplete, or was not found.',
|
|
218
|
+
nullable: false,
|
|
219
|
+
},
|
|
220
|
+
recommended_action: {
|
|
221
|
+
type: 'string',
|
|
222
|
+
description: 'The next step the agent should take: use the address, confirm it, ask for missing information, or offer one suggestion and revalidate it after confirmation.',
|
|
223
|
+
nullable: false,
|
|
224
|
+
},
|
|
225
|
+
missing_components: {
|
|
226
|
+
type: 'array',
|
|
227
|
+
description: 'Address fields that are missing and should be requested from the user.',
|
|
228
|
+
nullable: false,
|
|
229
|
+
items: {
|
|
230
|
+
type: 'string',
|
|
231
|
+
description: 'A missing address field such as address_line_1, address_line_2, city, region, or postal_code.',
|
|
232
|
+
nullable: false,
|
|
233
|
+
},
|
|
234
|
+
},
|
|
235
|
+
unconfirmed_components: {
|
|
236
|
+
type: 'array',
|
|
237
|
+
description: 'Address fields that Google could not confidently confirm.',
|
|
238
|
+
nullable: false,
|
|
239
|
+
items: {
|
|
240
|
+
type: 'string',
|
|
241
|
+
description: 'An unconfirmed address field.',
|
|
242
|
+
nullable: false,
|
|
243
|
+
},
|
|
244
|
+
},
|
|
245
|
+
corrected_components: {
|
|
246
|
+
type: 'array',
|
|
247
|
+
description: 'Address fields that Google inferred, corrected, or replaced and that may need confirmation.',
|
|
248
|
+
nullable: false,
|
|
249
|
+
items: {
|
|
250
|
+
type: 'string',
|
|
251
|
+
description: 'A corrected address field.',
|
|
252
|
+
nullable: false,
|
|
253
|
+
},
|
|
254
|
+
},
|
|
255
|
+
unresolved_tokens: {
|
|
256
|
+
type: 'array',
|
|
257
|
+
description: 'Parts of the spoken or entered address that Google could not resolve.',
|
|
258
|
+
nullable: false,
|
|
259
|
+
items: {
|
|
260
|
+
type: 'string',
|
|
261
|
+
description: 'An unresolved address token.',
|
|
262
|
+
nullable: false,
|
|
263
|
+
},
|
|
264
|
+
},
|
|
265
|
+
suggestions: {
|
|
266
|
+
type: 'array',
|
|
267
|
+
description: 'Up to three ranked address suggestions to offer one at a time when validation fails. After confirmation, call this Action again with the selected formatted_address.',
|
|
268
|
+
nullable: false,
|
|
269
|
+
items: {
|
|
270
|
+
type: 'object',
|
|
271
|
+
description: 'A Google Places address suggestion.',
|
|
272
|
+
nullable: false,
|
|
273
|
+
properties: {
|
|
274
|
+
place_id: {
|
|
275
|
+
type: 'string',
|
|
276
|
+
description: 'The stable Google Place identifier for the suggestion.',
|
|
277
|
+
nullable: false,
|
|
278
|
+
},
|
|
279
|
+
formatted_address: {
|
|
280
|
+
type: 'string',
|
|
281
|
+
description: 'The complete suggestion text to read back to the user.',
|
|
282
|
+
nullable: false,
|
|
283
|
+
},
|
|
284
|
+
address_line_1: {
|
|
285
|
+
type: 'string',
|
|
286
|
+
description: 'The primary address text for the suggestion.',
|
|
287
|
+
nullable: false,
|
|
288
|
+
},
|
|
289
|
+
locality: {
|
|
290
|
+
type: 'string',
|
|
291
|
+
description: 'The city, region, postal code, or country context that distinguishes the suggestion.',
|
|
292
|
+
nullable: false,
|
|
293
|
+
},
|
|
294
|
+
},
|
|
295
|
+
required: ['place_id', 'formatted_address', 'address_line_1', 'locality'],
|
|
296
|
+
additionalProperties: false,
|
|
297
|
+
},
|
|
298
|
+
},
|
|
299
|
+
validation_issues: {
|
|
300
|
+
type: 'array',
|
|
301
|
+
description: 'Potential address issues to confirm with the user.',
|
|
302
|
+
nullable: false,
|
|
303
|
+
items: {
|
|
304
|
+
type: 'string',
|
|
305
|
+
description: 'A human-readable validation issue.',
|
|
306
|
+
nullable: false,
|
|
307
|
+
},
|
|
308
|
+
},
|
|
309
|
+
};
|
|
310
|
+
export const VALIDATE_ADDRESS_OUTPUT_SCHEMA = {
|
|
311
|
+
type: 'object',
|
|
312
|
+
description: 'A standardized address and provider-neutral validation assessment.',
|
|
313
|
+
properties: addressOutputProperties,
|
|
314
|
+
required: Object.keys(addressOutputProperties),
|
|
315
|
+
additionalProperties: false,
|
|
316
|
+
};
|
|
317
|
+
export const VALIDATE_ADDRESS_OUTPUT_MAPPINGS = {
|
|
318
|
+
'/formatted_address': '{{ result.formatted_address }}',
|
|
319
|
+
'/address_line_1': '{{ result.address_line_1 }}',
|
|
320
|
+
'/address_line_2': '{{ result.address_line_2 }}',
|
|
321
|
+
'/city': '{{ result.city }}',
|
|
322
|
+
'/region': '{{ result.region }}',
|
|
323
|
+
'/postal_code': '{{ result.postal_code }}',
|
|
324
|
+
'/country_code': '{{ result.country_code }}',
|
|
325
|
+
'/latitude': '{{ result.latitude }}',
|
|
326
|
+
'/longitude': '{{ result.longitude }}',
|
|
327
|
+
'/validation_status': '{{ result.validation_status }}',
|
|
328
|
+
'/recommended_action': '{{ result.recommended_action }}',
|
|
329
|
+
'/missing_components': '{{ result.missing_components }}',
|
|
330
|
+
'/unconfirmed_components': '{{ result.unconfirmed_components }}',
|
|
331
|
+
'/corrected_components': '{{ result.corrected_components }}',
|
|
332
|
+
'/unresolved_tokens': '{{ result.unresolved_tokens }}',
|
|
333
|
+
'/validation_issues': '{{ result.validation_issues }}',
|
|
334
|
+
'/suggestions': '{{ result.suggestions }}',
|
|
335
|
+
};
|
|
336
|
+
/** The fixed contract of each built-in operation, keyed by its wire `type`. */
|
|
337
|
+
export const BUILT_IN_ACTION_CONTRACTS = {
|
|
338
|
+
send_email: {
|
|
339
|
+
inputSchema: SEND_EMAIL_INPUT_SCHEMA,
|
|
340
|
+
outputSchema: SEND_EMAIL_OUTPUT_SCHEMA,
|
|
341
|
+
outputMappings: SEND_EMAIL_OUTPUT_MAPPINGS,
|
|
342
|
+
},
|
|
343
|
+
send_text_message: {
|
|
344
|
+
inputSchema: SEND_TEXT_MESSAGE_INPUT_SCHEMA,
|
|
345
|
+
outputSchema: SEND_TEXT_MESSAGE_OUTPUT_SCHEMA,
|
|
346
|
+
outputMappings: SEND_TEXT_MESSAGE_OUTPUT_MAPPINGS,
|
|
347
|
+
},
|
|
348
|
+
validate_address: {
|
|
349
|
+
inputSchema: VALIDATE_ADDRESS_INPUT_SCHEMA,
|
|
350
|
+
outputSchema: VALIDATE_ADDRESS_OUTPUT_SCHEMA,
|
|
351
|
+
outputMappings: VALIDATE_ADDRESS_OUTPUT_MAPPINGS,
|
|
352
|
+
},
|
|
353
|
+
};
|
package/dist/client.cjs
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SimpleAI = void 0;
|
|
4
|
+
const data_1 = require("./data.cjs");
|
|
5
|
+
const http_1 = require("./http.cjs");
|
|
6
|
+
const DEFAULT_BASE_URL = 'https://api.prod.usesimple.ai/api/v1';
|
|
7
|
+
function envVar(name) {
|
|
8
|
+
if (typeof process === 'undefined' || process.env === undefined) {
|
|
9
|
+
return undefined;
|
|
10
|
+
}
|
|
11
|
+
const value = process.env[name];
|
|
12
|
+
return value === undefined || value === '' ? undefined : value;
|
|
13
|
+
}
|
|
14
|
+
class SimpleAI {
|
|
15
|
+
/** Custom data tables — query, read, and write records. */
|
|
16
|
+
data;
|
|
17
|
+
constructor(options = {}) {
|
|
18
|
+
const apiKey = options.apiKey ?? envVar('SIMPLE_API_KEY');
|
|
19
|
+
if (apiKey === undefined) {
|
|
20
|
+
throw new Error('Missing API key. Pass `apiKey` to new SimpleAI(...) or set the SIMPLE_API_KEY environment variable. ' +
|
|
21
|
+
'Inside Simple AI code actions the key is injected automatically.');
|
|
22
|
+
}
|
|
23
|
+
const baseUrl = options.baseUrl ?? envVar('SIMPLE_BASE_URL') ?? DEFAULT_BASE_URL;
|
|
24
|
+
const fetchImplementation = options.fetch ?? ((url, init) => fetch(url, init));
|
|
25
|
+
const http = (0, http_1.createHttpClient)({ baseUrl, apiKey, fetchImplementation });
|
|
26
|
+
this.data = new data_1.DataClient(http);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.SimpleAI = SimpleAI;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { DataClient } from "./data.cjs";
|
|
2
|
+
import type { FetchLike } from "./http.cjs";
|
|
3
|
+
export type SimpleAIOptions = {
|
|
4
|
+
/** API key (dashboard: Settings → API Keys). Defaults to SIMPLE_API_KEY — inside code actions this is injected automatically. */
|
|
5
|
+
apiKey?: string;
|
|
6
|
+
/** API base URL. Defaults to SIMPLE_BASE_URL, then the production API. */
|
|
7
|
+
baseUrl?: string;
|
|
8
|
+
/** Custom fetch implementation (for testing or instrumentation). */
|
|
9
|
+
fetch?: FetchLike;
|
|
10
|
+
};
|
|
11
|
+
export declare class SimpleAI {
|
|
12
|
+
/** Custom data tables — query, read, and write records. */
|
|
13
|
+
readonly data: DataClient;
|
|
14
|
+
constructor(options?: SimpleAIOptions);
|
|
15
|
+
}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { DataClient } from "./data.js";
|
|
2
|
+
import type { FetchLike } from "./http.js";
|
|
3
|
+
export type SimpleAIOptions = {
|
|
4
|
+
/** API key (dashboard: Settings → API Keys). Defaults to SIMPLE_API_KEY — inside code actions this is injected automatically. */
|
|
5
|
+
apiKey?: string;
|
|
6
|
+
/** API base URL. Defaults to SIMPLE_BASE_URL, then the production API. */
|
|
7
|
+
baseUrl?: string;
|
|
8
|
+
/** Custom fetch implementation (for testing or instrumentation). */
|
|
9
|
+
fetch?: FetchLike;
|
|
10
|
+
};
|
|
11
|
+
export declare class SimpleAI {
|
|
12
|
+
/** Custom data tables — query, read, and write records. */
|
|
13
|
+
readonly data: DataClient;
|
|
14
|
+
constructor(options?: SimpleAIOptions);
|
|
15
|
+
}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { DataClient } from "./data.js";
|
|
2
|
+
import { createHttpClient } from "./http.js";
|
|
3
|
+
const DEFAULT_BASE_URL = 'https://api.prod.usesimple.ai/api/v1';
|
|
4
|
+
function envVar(name) {
|
|
5
|
+
if (typeof process === 'undefined' || process.env === undefined) {
|
|
6
|
+
return undefined;
|
|
7
|
+
}
|
|
8
|
+
const value = process.env[name];
|
|
9
|
+
return value === undefined || value === '' ? undefined : value;
|
|
10
|
+
}
|
|
11
|
+
export class SimpleAI {
|
|
12
|
+
/** Custom data tables — query, read, and write records. */
|
|
13
|
+
data;
|
|
14
|
+
constructor(options = {}) {
|
|
15
|
+
const apiKey = options.apiKey ?? envVar('SIMPLE_API_KEY');
|
|
16
|
+
if (apiKey === undefined) {
|
|
17
|
+
throw new Error('Missing API key. Pass `apiKey` to new SimpleAI(...) or set the SIMPLE_API_KEY environment variable. ' +
|
|
18
|
+
'Inside Simple AI code actions the key is injected automatically.');
|
|
19
|
+
}
|
|
20
|
+
const baseUrl = options.baseUrl ?? envVar('SIMPLE_BASE_URL') ?? DEFAULT_BASE_URL;
|
|
21
|
+
const fetchImplementation = options.fetch ?? ((url, init) => fetch(url, init));
|
|
22
|
+
const http = createHttpClient({ baseUrl, apiKey, fetchImplementation });
|
|
23
|
+
this.data = new DataClient(http);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ensureMetadata = ensureMetadata;
|
|
4
|
+
/**
|
|
5
|
+
* An action's compiled bundle carries the SDK version it was built with, so it may run on a
|
|
6
|
+
* runtime older than itself. Substituting an empty grant keeps `metadata` present for the
|
|
7
|
+
* handler in that window — an Action can only lose a capability this way, never gain one.
|
|
8
|
+
*/
|
|
9
|
+
function ensureMetadata(metadata) {
|
|
10
|
+
return metadata ?? { capabilities: [] };
|
|
11
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ActionInvocationMetadata } from "./types.cjs";
|
|
2
|
+
/**
|
|
3
|
+
* An action's compiled bundle carries the SDK version it was built with, so it may run on a
|
|
4
|
+
* runtime older than itself. Substituting an empty grant keeps `metadata` present for the
|
|
5
|
+
* handler in that window — an Action can only lose a capability this way, never gain one.
|
|
6
|
+
*/
|
|
7
|
+
export declare function ensureMetadata(metadata: ActionInvocationMetadata | undefined): ActionInvocationMetadata;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ActionInvocationMetadata } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* An action's compiled bundle carries the SDK version it was built with, so it may run on a
|
|
4
|
+
* runtime older than itself. Substituting an empty grant keeps `metadata` present for the
|
|
5
|
+
* handler in that window — an Action can only lose a capability this way, never gain one.
|
|
6
|
+
*/
|
|
7
|
+
export declare function ensureMetadata(metadata: ActionInvocationMetadata | undefined): ActionInvocationMetadata;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An action's compiled bundle carries the SDK version it was built with, so it may run on a
|
|
3
|
+
* runtime older than itself. Substituting an empty grant keeps `metadata` present for the
|
|
4
|
+
* handler in that window — an Action can only lose a capability this way, never gain one.
|
|
5
|
+
*/
|
|
6
|
+
export function ensureMetadata(metadata) {
|
|
7
|
+
return metadata ?? { capabilities: [] };
|
|
8
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SIMPLE_ACTION_BRAND = void 0;
|
|
4
|
+
exports.defineAction = defineAction;
|
|
5
|
+
exports.defineCustomDataTransform = defineCustomDataTransform;
|
|
6
|
+
const client_1 = require("../client.cjs");
|
|
7
|
+
const defaults_1 = require("./defaults.cjs");
|
|
8
|
+
const workflow_1 = require("./workflow.cjs");
|
|
9
|
+
/**
|
|
10
|
+
* Brand the code-action runner uses to recognize `defineAction` handlers. The
|
|
11
|
+
* runner calls the default export with `{ inputs, config }` either way; the
|
|
12
|
+
* wrapper adds the authenticated `client` to the handler's context.
|
|
13
|
+
*/
|
|
14
|
+
exports.SIMPLE_ACTION_BRAND = Symbol.for('simple-ai.action');
|
|
15
|
+
const SIMPLE_ACTION_RUNTIME = Symbol.for('simple-ai.action-runtime');
|
|
16
|
+
function authenticatedClient(runtime) {
|
|
17
|
+
return runtime === undefined ? new client_1.SimpleAI() : new client_1.SimpleAI(runtime);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Define a code action with a typed context and an org-authenticated SDK
|
|
21
|
+
* client. The returned value must be your module's default export.
|
|
22
|
+
*
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { defineAction } from '@simple-ai-lab/sdk';
|
|
25
|
+
*
|
|
26
|
+
* export default defineAction(async ({ inputs, config, client }) => {
|
|
27
|
+
* const { records } = await client.data.query({ tableName: 'customers', text: inputs.email });
|
|
28
|
+
* return { found: records.length > 0 };
|
|
29
|
+
* });
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
function defineAction(handler) {
|
|
33
|
+
const wrapped = async (invocation) => {
|
|
34
|
+
const { inputs, config } = invocation;
|
|
35
|
+
const runtime = invocation[SIMPLE_ACTION_RUNTIME];
|
|
36
|
+
// The Dynamic Worker runner supplies a non-secret API identity. The
|
|
37
|
+
// outbound Worker injects the real short-lived credential after the
|
|
38
|
+
// request has crossed the untrusted isolate boundary.
|
|
39
|
+
const client = authenticatedClient(runtime);
|
|
40
|
+
const metadata = (0, defaults_1.ensureMetadata)(invocation.metadata);
|
|
41
|
+
// The invocation object doubles as the channel back to the runtime: a
|
|
42
|
+
// directive the handler requests is recorded on it and read once the
|
|
43
|
+
// handler resolves, beside the value it returned.
|
|
44
|
+
return handler({
|
|
45
|
+
inputs,
|
|
46
|
+
config,
|
|
47
|
+
metadata,
|
|
48
|
+
client,
|
|
49
|
+
workflow: (0, workflow_1.createActionWorkflow)({ metadata, carrier: invocation }),
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
const brand = { [exports.SIMPLE_ACTION_BRAND]: true };
|
|
53
|
+
return Object.assign(wrapped, brand);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Define the optional result-formatting stage of a Custom Data Action.
|
|
57
|
+
* The first generic describes one item returned by the data operation.
|
|
58
|
+
*/
|
|
59
|
+
function defineCustomDataTransform(handler) {
|
|
60
|
+
const wrapped = async (invocation) => {
|
|
61
|
+
const { dataResult, inputs, operation, config } = invocation;
|
|
62
|
+
const client = authenticatedClient(invocation[SIMPLE_ACTION_RUNTIME]);
|
|
63
|
+
return handler({
|
|
64
|
+
dataResult,
|
|
65
|
+
inputs,
|
|
66
|
+
operation,
|
|
67
|
+
config,
|
|
68
|
+
metadata: (0, defaults_1.ensureMetadata)(invocation.metadata),
|
|
69
|
+
client,
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
const brand = { [exports.SIMPLE_ACTION_BRAND]: true };
|
|
73
|
+
return Object.assign(wrapped, brand);
|
|
74
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { SimpleAI } from "../client.cjs";
|
|
2
|
+
import type { ActionInvocationMetadata } from "./types.cjs";
|
|
3
|
+
import { type ActionDirectiveCarrier, type ActionWorkflow } from "./workflow.cjs";
|
|
4
|
+
/**
|
|
5
|
+
* Brand the code-action runner uses to recognize `defineAction` handlers. The
|
|
6
|
+
* runner calls the default export with `{ inputs, config }` either way; the
|
|
7
|
+
* wrapper adds the authenticated `client` to the handler's context.
|
|
8
|
+
*/
|
|
9
|
+
export declare const SIMPLE_ACTION_BRAND: unique symbol;
|
|
10
|
+
declare const SIMPLE_ACTION_RUNTIME: unique symbol;
|
|
11
|
+
type ActionRuntime = {
|
|
12
|
+
apiKey: string;
|
|
13
|
+
baseUrl: string;
|
|
14
|
+
};
|
|
15
|
+
export type CustomDataResult<TItem = Record<string, unknown>> = {
|
|
16
|
+
items: TItem[];
|
|
17
|
+
total: number;
|
|
18
|
+
truncated: boolean;
|
|
19
|
+
};
|
|
20
|
+
export type CustomDataTransformOperation = {
|
|
21
|
+
kind: 'agent_search';
|
|
22
|
+
table: string;
|
|
23
|
+
filters: {
|
|
24
|
+
column: string;
|
|
25
|
+
values: (string | number | boolean)[];
|
|
26
|
+
}[];
|
|
27
|
+
text: string | null;
|
|
28
|
+
} | {
|
|
29
|
+
kind: 'search';
|
|
30
|
+
table: string;
|
|
31
|
+
} | {
|
|
32
|
+
kind: 'query';
|
|
33
|
+
};
|
|
34
|
+
export type CustomDataTransformContext<TItem = Record<string, unknown>, TInputs = Record<string, unknown>> = {
|
|
35
|
+
dataResult: CustomDataResult<TItem>;
|
|
36
|
+
inputs: TInputs;
|
|
37
|
+
operation: CustomDataTransformOperation;
|
|
38
|
+
config: Record<string, string>;
|
|
39
|
+
/** What this invocation is allowed to do, decided by the use site that invoked it. */
|
|
40
|
+
metadata: ActionInvocationMetadata;
|
|
41
|
+
client: SimpleAI;
|
|
42
|
+
};
|
|
43
|
+
export type ActionContext<TInputs = Record<string, unknown>> = {
|
|
44
|
+
/** The Action's declared inputs, already validated against its input schema. */
|
|
45
|
+
inputs: TInputs;
|
|
46
|
+
/** Your organization's configuration variables. */
|
|
47
|
+
config: Record<string, string>;
|
|
48
|
+
/** What this invocation is allowed to do, decided by the use site that invoked it. */
|
|
49
|
+
metadata: ActionInvocationMetadata;
|
|
50
|
+
/** The Simple AI SDK, authenticated for your organization. */
|
|
51
|
+
client: SimpleAI;
|
|
52
|
+
/** Ask the workflow that invoked this Action to do something, e.g. run a different node next. */
|
|
53
|
+
workflow: ActionWorkflow;
|
|
54
|
+
};
|
|
55
|
+
export type ActionHandler<TInputs = Record<string, unknown>, TResult = unknown> = ((args: {
|
|
56
|
+
inputs: TInputs;
|
|
57
|
+
config: Record<string, string>;
|
|
58
|
+
/** Omitted by runtimes that predate invocation metadata; the handler still receives one. */
|
|
59
|
+
metadata?: ActionInvocationMetadata;
|
|
60
|
+
[SIMPLE_ACTION_RUNTIME]?: ActionRuntime;
|
|
61
|
+
} & ActionDirectiveCarrier) => Promise<TResult>) & {
|
|
62
|
+
[SIMPLE_ACTION_BRAND]: true;
|
|
63
|
+
};
|
|
64
|
+
export type CustomDataTransformHandler<TItem = Record<string, unknown>, TInputs = Record<string, unknown>, TResult = unknown> = ((args: {
|
|
65
|
+
dataResult: CustomDataResult<TItem>;
|
|
66
|
+
inputs: TInputs;
|
|
67
|
+
operation: CustomDataTransformOperation;
|
|
68
|
+
config: Record<string, string>;
|
|
69
|
+
/** Omitted by runtimes that predate invocation metadata; the handler still receives one. */
|
|
70
|
+
metadata?: ActionInvocationMetadata;
|
|
71
|
+
[SIMPLE_ACTION_RUNTIME]?: ActionRuntime;
|
|
72
|
+
}) => Promise<TResult>) & {
|
|
73
|
+
[SIMPLE_ACTION_BRAND]: true;
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* Define a code action with a typed context and an org-authenticated SDK
|
|
77
|
+
* client. The returned value must be your module's default export.
|
|
78
|
+
*
|
|
79
|
+
* ```ts
|
|
80
|
+
* import { defineAction } from '@simple-ai-lab/sdk';
|
|
81
|
+
*
|
|
82
|
+
* export default defineAction(async ({ inputs, config, client }) => {
|
|
83
|
+
* const { records } = await client.data.query({ tableName: 'customers', text: inputs.email });
|
|
84
|
+
* return { found: records.length > 0 };
|
|
85
|
+
* });
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
export declare function defineAction<TInputs = Record<string, unknown>, TResult = unknown>(handler: (context: ActionContext<TInputs>) => Promise<TResult> | TResult): ActionHandler<TInputs, TResult>;
|
|
89
|
+
/**
|
|
90
|
+
* Define the optional result-formatting stage of a Custom Data Action.
|
|
91
|
+
* The first generic describes one item returned by the data operation.
|
|
92
|
+
*/
|
|
93
|
+
export declare function defineCustomDataTransform<TItem = Record<string, unknown>, TInputs = Record<string, unknown>, TResult = unknown>(handler: (context: CustomDataTransformContext<TItem, TInputs>) => Promise<TResult> | TResult): CustomDataTransformHandler<TItem, TInputs, TResult>;
|
|
94
|
+
export {};
|