@arcadeai/arcadejs 1.2.1 → 1.4.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/CHANGELOG.md +53 -0
- package/README.md +2 -2
- package/_shims/index.d.ts +2 -0
- package/_shims/index.js +5 -1
- package/_shims/index.mjs +5 -1
- package/core.d.ts +12 -1
- package/core.d.ts.map +1 -1
- package/core.js +10 -7
- package/core.js.map +1 -1
- package/core.mjs +11 -8
- package/core.mjs.map +1 -1
- package/index.d.mts +6 -2
- package/index.d.ts +6 -2
- package/index.d.ts.map +1 -1
- package/index.js +8 -0
- package/index.js.map +1 -1
- package/index.mjs +8 -0
- package/index.mjs.map +1 -1
- package/internal/qs/formats.d.ts +6 -0
- package/internal/qs/formats.d.ts.map +1 -0
- package/internal/qs/formats.js +11 -0
- package/internal/qs/formats.js.map +1 -0
- package/internal/qs/formats.mjs +8 -0
- package/internal/qs/formats.mjs.map +1 -0
- package/internal/qs/index.d.ts +10 -0
- package/internal/qs/index.d.ts.map +1 -0
- package/internal/qs/index.js +14 -0
- package/internal/qs/index.js.map +1 -0
- package/internal/qs/index.mjs +10 -0
- package/internal/qs/index.mjs.map +1 -0
- package/internal/qs/stringify.d.ts +3 -0
- package/internal/qs/stringify.d.ts.map +1 -0
- package/internal/qs/stringify.js +280 -0
- package/internal/qs/stringify.js.map +1 -0
- package/internal/qs/stringify.mjs +276 -0
- package/internal/qs/stringify.mjs.map +1 -0
- package/internal/qs/types.d.ts +57 -0
- package/internal/qs/types.d.ts.map +1 -0
- package/internal/qs/types.js +3 -0
- package/internal/qs/types.js.map +1 -0
- package/internal/qs/types.mjs +2 -0
- package/internal/qs/types.mjs.map +1 -0
- package/internal/qs/utils.d.ts +14 -0
- package/internal/qs/utils.d.ts.map +1 -0
- package/internal/qs/utils.js +229 -0
- package/internal/qs/utils.js.map +1 -0
- package/internal/qs/utils.mjs +217 -0
- package/internal/qs/utils.mjs.map +1 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +19 -0
- package/lib/index.js.map +1 -0
- package/lib/index.mjs +3 -0
- package/lib/index.mjs.map +1 -0
- package/lib/zod/types.d.ts +34 -0
- package/lib/zod/types.d.ts.map +1 -0
- package/lib/zod/types.js +3 -0
- package/lib/zod/types.js.map +1 -0
- package/lib/zod/types.mjs +2 -0
- package/lib/zod/types.mjs.map +1 -0
- package/lib/zod/zod.d.ts +55 -0
- package/lib/zod/zod.d.ts.map +1 -0
- package/lib/zod/zod.js +172 -0
- package/lib/zod/zod.js.map +1 -0
- package/lib/zod/zod.mjs +162 -0
- package/lib/zod/zod.mjs.map +1 -0
- package/package.json +10 -30
- package/resources/chat.d.ts +2 -0
- package/resources/chat.d.ts.map +1 -0
- package/resources/chat.js +19 -0
- package/resources/chat.js.map +1 -0
- package/resources/chat.mjs +3 -0
- package/resources/chat.mjs.map +1 -0
- package/resources/index.d.ts +2 -1
- package/resources/index.d.ts.map +1 -1
- package/resources/index.js +4 -1
- package/resources/index.js.map +1 -1
- package/resources/index.mjs +1 -0
- package/resources/index.mjs.map +1 -1
- package/resources/tools/index.d.ts +1 -1
- package/resources/tools/index.d.ts.map +1 -1
- package/resources/tools/index.js.map +1 -1
- package/resources/tools/index.mjs.map +1 -1
- package/resources/tools/tools.d.ts +21 -4
- package/resources/tools/tools.d.ts.map +1 -1
- package/resources/tools/tools.js +5 -5
- package/resources/tools/tools.js.map +1 -1
- package/resources/tools/tools.mjs +5 -5
- package/resources/tools/tools.mjs.map +1 -1
- package/resources/tools.d.ts +1 -0
- package/resources/tools.d.ts.map +1 -1
- package/resources/tools.js +17 -0
- package/resources/tools.js.map +1 -1
- package/resources/tools.mjs +2 -1
- package/resources/tools.mjs.map +1 -1
- package/resources/workers.d.ts +180 -0
- package/resources/workers.d.ts.map +1 -0
- package/resources/workers.js +65 -0
- package/resources/workers.js.map +1 -0
- package/resources/workers.mjs +60 -0
- package/resources/workers.mjs.map +1 -0
- package/resources.d.ts +2 -0
- package/resources.d.ts.map +1 -0
- package/resources.js +18 -0
- package/resources.js.map +1 -0
- package/resources.mjs +2 -0
- package/resources.mjs.map +1 -0
- package/src/_shims/index.d.ts +2 -0
- package/src/_shims/index.js +5 -1
- package/src/_shims/index.mjs +5 -1
- package/src/core.ts +27 -8
- package/src/index.ts +35 -0
- package/src/internal/qs/LICENSE.md +13 -0
- package/src/internal/qs/README.md +3 -0
- package/src/internal/qs/formats.ts +9 -0
- package/src/internal/qs/index.ts +13 -0
- package/src/internal/qs/stringify.ts +388 -0
- package/src/internal/qs/types.ts +71 -0
- package/src/internal/qs/utils.ts +265 -0
- package/src/lib/index.ts +2 -0
- package/src/lib/zod/types.ts +38 -0
- package/src/lib/zod/zod.ts +208 -0
- package/src/resources/chat.ts +3 -0
- package/src/resources/index.ts +13 -0
- package/src/resources/tools/index.ts +1 -0
- package/src/resources/tools/tools.ts +37 -5
- package/src/resources/tools.ts +3 -0
- package/src/resources/workers.ts +330 -0
- package/src/resources.ts +1 -0
- package/src/version.ts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { RFC1738 } from './formats';
|
|
2
|
+
import type { DefaultEncoder, Format } from './types';
|
|
3
|
+
|
|
4
|
+
const has = Object.prototype.hasOwnProperty;
|
|
5
|
+
const is_array = Array.isArray;
|
|
6
|
+
|
|
7
|
+
const hex_table = (() => {
|
|
8
|
+
const array = [];
|
|
9
|
+
for (let i = 0; i < 256; ++i) {
|
|
10
|
+
array.push('%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase());
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return array;
|
|
14
|
+
})();
|
|
15
|
+
|
|
16
|
+
function compact_queue<T extends Record<string, any>>(queue: Array<{ obj: T; prop: string }>) {
|
|
17
|
+
while (queue.length > 1) {
|
|
18
|
+
const item = queue.pop();
|
|
19
|
+
if (!item) continue;
|
|
20
|
+
|
|
21
|
+
const obj = item.obj[item.prop];
|
|
22
|
+
|
|
23
|
+
if (is_array(obj)) {
|
|
24
|
+
const compacted: unknown[] = [];
|
|
25
|
+
|
|
26
|
+
for (let j = 0; j < obj.length; ++j) {
|
|
27
|
+
if (typeof obj[j] !== 'undefined') {
|
|
28
|
+
compacted.push(obj[j]);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// @ts-ignore
|
|
33
|
+
item.obj[item.prop] = compacted;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function array_to_object(source: any[], options: { plainObjects: boolean }) {
|
|
39
|
+
const obj = options && options.plainObjects ? Object.create(null) : {};
|
|
40
|
+
for (let i = 0; i < source.length; ++i) {
|
|
41
|
+
if (typeof source[i] !== 'undefined') {
|
|
42
|
+
obj[i] = source[i];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return obj;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function merge(
|
|
50
|
+
target: any,
|
|
51
|
+
source: any,
|
|
52
|
+
options: { plainObjects?: boolean; allowPrototypes?: boolean } = {},
|
|
53
|
+
) {
|
|
54
|
+
if (!source) {
|
|
55
|
+
return target;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (typeof source !== 'object') {
|
|
59
|
+
if (is_array(target)) {
|
|
60
|
+
target.push(source);
|
|
61
|
+
} else if (target && typeof target === 'object') {
|
|
62
|
+
if (
|
|
63
|
+
(options && (options.plainObjects || options.allowPrototypes)) ||
|
|
64
|
+
!has.call(Object.prototype, source)
|
|
65
|
+
) {
|
|
66
|
+
target[source] = true;
|
|
67
|
+
}
|
|
68
|
+
} else {
|
|
69
|
+
return [target, source];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return target;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!target || typeof target !== 'object') {
|
|
76
|
+
return [target].concat(source);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
let mergeTarget = target;
|
|
80
|
+
if (is_array(target) && !is_array(source)) {
|
|
81
|
+
// @ts-ignore
|
|
82
|
+
mergeTarget = array_to_object(target, options);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (is_array(target) && is_array(source)) {
|
|
86
|
+
source.forEach(function (item, i) {
|
|
87
|
+
if (has.call(target, i)) {
|
|
88
|
+
const targetItem = target[i];
|
|
89
|
+
if (targetItem && typeof targetItem === 'object' && item && typeof item === 'object') {
|
|
90
|
+
target[i] = merge(targetItem, item, options);
|
|
91
|
+
} else {
|
|
92
|
+
target.push(item);
|
|
93
|
+
}
|
|
94
|
+
} else {
|
|
95
|
+
target[i] = item;
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
return target;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return Object.keys(source).reduce(function (acc, key) {
|
|
102
|
+
const value = source[key];
|
|
103
|
+
|
|
104
|
+
if (has.call(acc, key)) {
|
|
105
|
+
acc[key] = merge(acc[key], value, options);
|
|
106
|
+
} else {
|
|
107
|
+
acc[key] = value;
|
|
108
|
+
}
|
|
109
|
+
return acc;
|
|
110
|
+
}, mergeTarget);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function assign_single_source(target: any, source: any) {
|
|
114
|
+
return Object.keys(source).reduce(function (acc, key) {
|
|
115
|
+
acc[key] = source[key];
|
|
116
|
+
return acc;
|
|
117
|
+
}, target);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function decode(str: string, _: any, charset: string) {
|
|
121
|
+
const strWithoutPlus = str.replace(/\+/g, ' ');
|
|
122
|
+
if (charset === 'iso-8859-1') {
|
|
123
|
+
// unescape never throws, no try...catch needed:
|
|
124
|
+
return strWithoutPlus.replace(/%[0-9a-f]{2}/gi, unescape);
|
|
125
|
+
}
|
|
126
|
+
// utf-8
|
|
127
|
+
try {
|
|
128
|
+
return decodeURIComponent(strWithoutPlus);
|
|
129
|
+
} catch (e) {
|
|
130
|
+
return strWithoutPlus;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const limit = 1024;
|
|
135
|
+
|
|
136
|
+
export const encode: (
|
|
137
|
+
str: any,
|
|
138
|
+
defaultEncoder: DefaultEncoder,
|
|
139
|
+
charset: string,
|
|
140
|
+
type: 'key' | 'value',
|
|
141
|
+
format: Format,
|
|
142
|
+
) => string = (str, _defaultEncoder, charset, _kind, format: Format) => {
|
|
143
|
+
// This code was originally written by Brian White for the io.js core querystring library.
|
|
144
|
+
// It has been adapted here for stricter adherence to RFC 3986
|
|
145
|
+
if (str.length === 0) {
|
|
146
|
+
return str;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
let string = str;
|
|
150
|
+
if (typeof str === 'symbol') {
|
|
151
|
+
string = Symbol.prototype.toString.call(str);
|
|
152
|
+
} else if (typeof str !== 'string') {
|
|
153
|
+
string = String(str);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (charset === 'iso-8859-1') {
|
|
157
|
+
return escape(string).replace(/%u[0-9a-f]{4}/gi, function ($0) {
|
|
158
|
+
return '%26%23' + parseInt($0.slice(2), 16) + '%3B';
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
let out = '';
|
|
163
|
+
for (let j = 0; j < string.length; j += limit) {
|
|
164
|
+
const segment = string.length >= limit ? string.slice(j, j + limit) : string;
|
|
165
|
+
const arr = [];
|
|
166
|
+
|
|
167
|
+
for (let i = 0; i < segment.length; ++i) {
|
|
168
|
+
let c = segment.charCodeAt(i);
|
|
169
|
+
if (
|
|
170
|
+
c === 0x2d || // -
|
|
171
|
+
c === 0x2e || // .
|
|
172
|
+
c === 0x5f || // _
|
|
173
|
+
c === 0x7e || // ~
|
|
174
|
+
(c >= 0x30 && c <= 0x39) || // 0-9
|
|
175
|
+
(c >= 0x41 && c <= 0x5a) || // a-z
|
|
176
|
+
(c >= 0x61 && c <= 0x7a) || // A-Z
|
|
177
|
+
(format === RFC1738 && (c === 0x28 || c === 0x29)) // ( )
|
|
178
|
+
) {
|
|
179
|
+
arr[arr.length] = segment.charAt(i);
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (c < 0x80) {
|
|
184
|
+
arr[arr.length] = hex_table[c];
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (c < 0x800) {
|
|
189
|
+
arr[arr.length] = hex_table[0xc0 | (c >> 6)]! + hex_table[0x80 | (c & 0x3f)];
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (c < 0xd800 || c >= 0xe000) {
|
|
194
|
+
arr[arr.length] =
|
|
195
|
+
hex_table[0xe0 | (c >> 12)]! + hex_table[0x80 | ((c >> 6) & 0x3f)] + hex_table[0x80 | (c & 0x3f)];
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
i += 1;
|
|
200
|
+
c = 0x10000 + (((c & 0x3ff) << 10) | (segment.charCodeAt(i) & 0x3ff));
|
|
201
|
+
|
|
202
|
+
arr[arr.length] =
|
|
203
|
+
hex_table[0xf0 | (c >> 18)]! +
|
|
204
|
+
hex_table[0x80 | ((c >> 12) & 0x3f)] +
|
|
205
|
+
hex_table[0x80 | ((c >> 6) & 0x3f)] +
|
|
206
|
+
hex_table[0x80 | (c & 0x3f)];
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
out += arr.join('');
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return out;
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
export function compact(value: any) {
|
|
216
|
+
const queue = [{ obj: { o: value }, prop: 'o' }];
|
|
217
|
+
const refs = [];
|
|
218
|
+
|
|
219
|
+
for (let i = 0; i < queue.length; ++i) {
|
|
220
|
+
const item = queue[i];
|
|
221
|
+
// @ts-ignore
|
|
222
|
+
const obj = item.obj[item.prop];
|
|
223
|
+
|
|
224
|
+
const keys = Object.keys(obj);
|
|
225
|
+
for (let j = 0; j < keys.length; ++j) {
|
|
226
|
+
const key = keys[j]!;
|
|
227
|
+
const val = obj[key];
|
|
228
|
+
if (typeof val === 'object' && val !== null && refs.indexOf(val) === -1) {
|
|
229
|
+
queue.push({ obj: obj, prop: key });
|
|
230
|
+
refs.push(val);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
compact_queue(queue);
|
|
236
|
+
|
|
237
|
+
return value;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export function is_regexp(obj: any) {
|
|
241
|
+
return Object.prototype.toString.call(obj) === '[object RegExp]';
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export function is_buffer(obj: any) {
|
|
245
|
+
if (!obj || typeof obj !== 'object') {
|
|
246
|
+
return false;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export function combine(a: any, b: any) {
|
|
253
|
+
return [].concat(a, b);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export function maybe_map<T>(val: T[], fn: (v: T) => T) {
|
|
257
|
+
if (is_array(val)) {
|
|
258
|
+
const mapped = [];
|
|
259
|
+
for (let i = 0; i < val.length; i += 1) {
|
|
260
|
+
mapped.push(fn(val[i]!));
|
|
261
|
+
}
|
|
262
|
+
return mapped;
|
|
263
|
+
}
|
|
264
|
+
return fn(val);
|
|
265
|
+
}
|
package/src/lib/index.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { AuthorizationResponse } from '../../resources/shared';
|
|
2
|
+
import { ExecuteToolResponse } from '../../resources/tools/tools';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Response returned when a tool requires authorization before execution
|
|
7
|
+
*/
|
|
8
|
+
export interface ToolAuthorizationResponse {
|
|
9
|
+
authorization_required: true;
|
|
10
|
+
authorization_response: AuthorizationResponse;
|
|
11
|
+
url: string;
|
|
12
|
+
message: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Represents a tool with Zod schema validation and execution capabilities.
|
|
17
|
+
*/
|
|
18
|
+
export interface ZodTool {
|
|
19
|
+
name: string;
|
|
20
|
+
description: string | undefined;
|
|
21
|
+
parameters: z.ZodType;
|
|
22
|
+
output: z.ZodType | undefined;
|
|
23
|
+
execute: <T extends unknown>(input: T) => Promise<ExecuteToolResponse>;
|
|
24
|
+
executeOrAuthorize: <T extends unknown>(
|
|
25
|
+
input: T,
|
|
26
|
+
) => Promise<ExecuteToolResponse | ToolAuthorizationResponse>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Schema definition for an Arcade tool, containing the tool's name,
|
|
31
|
+
* description, and Zod-validated parameter structure.
|
|
32
|
+
*/
|
|
33
|
+
export interface ZodToolSchema {
|
|
34
|
+
name: string;
|
|
35
|
+
description: string | undefined;
|
|
36
|
+
parameters: z.ZodType;
|
|
37
|
+
output: z.ZodType | undefined;
|
|
38
|
+
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { ExecuteToolResponse, ToolDefinition } from '../../resources/tools/tools';
|
|
2
|
+
import { ToolAuthorizationResponse, ZodTool, ZodToolSchema } from './types';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { type Arcade } from '../../index';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Checks if an error indicates that authorization for the tool is required
|
|
8
|
+
*/
|
|
9
|
+
export function isAuthorizationRequiredError(error: Error): boolean {
|
|
10
|
+
return (
|
|
11
|
+
error?.name === 'PermissionDeniedError' ||
|
|
12
|
+
error?.message?.includes('permission denied') ||
|
|
13
|
+
error?.message?.includes('authorization required')
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Converts Arcade Tool Input to Zod schema
|
|
19
|
+
*/
|
|
20
|
+
export function convertParametersToZodSchema(parameters: ToolDefinition.Input): z.ZodType {
|
|
21
|
+
if (!parameters.parameters || !Array.isArray(parameters.parameters)) {
|
|
22
|
+
return z.object({});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const schemaObj: Record<string, z.ZodType> = {};
|
|
26
|
+
|
|
27
|
+
for (const param of parameters.parameters) {
|
|
28
|
+
const { name, required, value_schema } = param;
|
|
29
|
+
let zodType = convertValueSchemaToZod(value_schema);
|
|
30
|
+
|
|
31
|
+
if (!required) {
|
|
32
|
+
zodType = zodType.optional();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
schemaObj[name] = zodType;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return z.object(schemaObj);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Converts a value schema to Zod type
|
|
43
|
+
*/
|
|
44
|
+
function convertValueSchemaToZod(schema: {
|
|
45
|
+
val_type: string;
|
|
46
|
+
inner_val_type?: string | null;
|
|
47
|
+
enum?: string[] | null;
|
|
48
|
+
}): z.ZodType {
|
|
49
|
+
let baseType: z.ZodType;
|
|
50
|
+
|
|
51
|
+
switch (schema.val_type) {
|
|
52
|
+
case 'string':
|
|
53
|
+
baseType = schema.enum ? z.enum(schema.enum as [string, ...string[]]) : z.string();
|
|
54
|
+
break;
|
|
55
|
+
case 'integer':
|
|
56
|
+
baseType = z.number().int();
|
|
57
|
+
break;
|
|
58
|
+
case 'number':
|
|
59
|
+
baseType = z.number();
|
|
60
|
+
break;
|
|
61
|
+
case 'boolean':
|
|
62
|
+
baseType = z.boolean();
|
|
63
|
+
break;
|
|
64
|
+
case 'json':
|
|
65
|
+
baseType = z.any();
|
|
66
|
+
break;
|
|
67
|
+
case 'array':
|
|
68
|
+
if (!schema.inner_val_type) {
|
|
69
|
+
throw new Error('Array type must have inner_val_type specified');
|
|
70
|
+
}
|
|
71
|
+
baseType = z.array(convertValueSchemaToZod({ val_type: schema.inner_val_type }));
|
|
72
|
+
break;
|
|
73
|
+
default:
|
|
74
|
+
throw new Error(`Unsupported value type: ${schema.val_type}`);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return baseType;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Converts Arcade Tool Output to Zod schema
|
|
82
|
+
*/
|
|
83
|
+
export function convertOutputToZodSchema(output: ToolDefinition.Output): z.ZodType {
|
|
84
|
+
if (!output.value_schema) {
|
|
85
|
+
return z.any();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return convertValueSchemaToZod(output.value_schema);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Converts a single tool to a Zod-validated tool schema
|
|
93
|
+
* @param tool - The tool to convert
|
|
94
|
+
* @returns Zod-validated tool schema
|
|
95
|
+
* @throws ToolConversionError if the tool is invalid
|
|
96
|
+
*/
|
|
97
|
+
export function convertSingleToolToSchema(tool: ToolDefinition): ZodToolSchema {
|
|
98
|
+
const { qualified_name, description, input, output } = tool;
|
|
99
|
+
const zodParameters = convertParametersToZodSchema(input);
|
|
100
|
+
const zodOutput = output ? convertOutputToZodSchema(output) : undefined;
|
|
101
|
+
return {
|
|
102
|
+
name: qualified_name.replace(/\./g, '_'),
|
|
103
|
+
description,
|
|
104
|
+
parameters: zodParameters,
|
|
105
|
+
output: zodOutput,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Converts OpenAI formatted tools to Zod-validated tool schemas
|
|
111
|
+
* @param tools - Array of formatted tools
|
|
112
|
+
* @returns Array of Zod-validated tool schemas
|
|
113
|
+
*/
|
|
114
|
+
export function toZodSchema(tools: ToolDefinition[]): ZodToolSchema[] {
|
|
115
|
+
return tools.map(convertSingleToolToSchema);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Converts a single tool to a full tool with execution capabilities
|
|
120
|
+
* @param tool - The tool to convert
|
|
121
|
+
* @param client - Arcade client instance
|
|
122
|
+
* @param userId - User ID to use for the tool execution
|
|
123
|
+
* @returns Zod-validated tool with execution methods
|
|
124
|
+
* @throws ToolConversionError if the tool is invalid
|
|
125
|
+
*/
|
|
126
|
+
export function createZodTool({
|
|
127
|
+
tool,
|
|
128
|
+
client,
|
|
129
|
+
userId,
|
|
130
|
+
}: {
|
|
131
|
+
tool: ToolDefinition;
|
|
132
|
+
client: Arcade;
|
|
133
|
+
userId: string;
|
|
134
|
+
}): ZodTool {
|
|
135
|
+
const schema = convertSingleToolToSchema(tool);
|
|
136
|
+
const { name, description, parameters, output } = schema;
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
name,
|
|
140
|
+
description,
|
|
141
|
+
parameters,
|
|
142
|
+
output,
|
|
143
|
+
execute: async (input: z.infer<typeof parameters>): Promise<ExecuteToolResponse> => {
|
|
144
|
+
const validationResult = parameters.safeParse(input);
|
|
145
|
+
if (!validationResult.success) {
|
|
146
|
+
throw new Error(`Invalid input: ${validationResult.error.message}`);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return client.tools.execute({
|
|
150
|
+
tool_name: name,
|
|
151
|
+
input: validationResult.data,
|
|
152
|
+
user_id: userId,
|
|
153
|
+
});
|
|
154
|
+
},
|
|
155
|
+
executeOrAuthorize: async (
|
|
156
|
+
input: z.infer<typeof parameters>,
|
|
157
|
+
): Promise<ExecuteToolResponse | ToolAuthorizationResponse> => {
|
|
158
|
+
const validationResult = parameters.safeParse(input);
|
|
159
|
+
if (!validationResult.success) {
|
|
160
|
+
throw new Error(`Invalid input: ${validationResult.error.message}`);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
try {
|
|
164
|
+
return await client.tools.execute({
|
|
165
|
+
tool_name: name,
|
|
166
|
+
input: validationResult.data,
|
|
167
|
+
user_id: userId,
|
|
168
|
+
});
|
|
169
|
+
} catch (error) {
|
|
170
|
+
if (error instanceof Error && isAuthorizationRequiredError(error)) {
|
|
171
|
+
const response = (await client.tools.authorize({
|
|
172
|
+
tool_name: name,
|
|
173
|
+
user_id: userId,
|
|
174
|
+
})) as { url: string };
|
|
175
|
+
|
|
176
|
+
return {
|
|
177
|
+
authorization_required: true,
|
|
178
|
+
authorization_response: response,
|
|
179
|
+
url: response.url,
|
|
180
|
+
message:
|
|
181
|
+
'This tool requires authorization. Please visit the following URL to authorize the tool: ' +
|
|
182
|
+
response.url,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
throw error;
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Converts formatted tools to full tools with execution capabilities
|
|
193
|
+
* @param tools - Array of formatted tools
|
|
194
|
+
* @param client - Arcade client instance
|
|
195
|
+
* @param userId - User ID to use for the tool execution
|
|
196
|
+
* @returns Array of Zod-validated tools with execution methods
|
|
197
|
+
*/
|
|
198
|
+
export function toZod({
|
|
199
|
+
tools,
|
|
200
|
+
client,
|
|
201
|
+
userId,
|
|
202
|
+
}: {
|
|
203
|
+
tools: ToolDefinition[];
|
|
204
|
+
client: Arcade;
|
|
205
|
+
userId: string;
|
|
206
|
+
}): ZodTool[] {
|
|
207
|
+
return tools.map((tool) => createZodTool({ tool, client, userId }));
|
|
208
|
+
}
|
package/src/resources/index.ts
CHANGED
|
@@ -25,4 +25,17 @@ export {
|
|
|
25
25
|
type ToolListParams,
|
|
26
26
|
type ToolAuthorizeParams,
|
|
27
27
|
type ToolExecuteParams,
|
|
28
|
+
type ToolGetParams,
|
|
28
29
|
} from './tools/tools';
|
|
30
|
+
export {
|
|
31
|
+
WorkerResponsesOffsetPage,
|
|
32
|
+
Workers,
|
|
33
|
+
type CreateWorkerRequest,
|
|
34
|
+
type UpdateWorkerRequest,
|
|
35
|
+
type WorkerHealthResponse,
|
|
36
|
+
type WorkerResponse,
|
|
37
|
+
type WorkerCreateParams,
|
|
38
|
+
type WorkerUpdateParams,
|
|
39
|
+
type WorkerListParams,
|
|
40
|
+
type WorkerToolsParams,
|
|
41
|
+
} from './workers';
|
|
@@ -61,8 +61,17 @@ export class Tools extends APIResource {
|
|
|
61
61
|
/**
|
|
62
62
|
* Returns the arcade tool specification for a specific tool
|
|
63
63
|
*/
|
|
64
|
-
get(name: string, options?: Core.RequestOptions): Core.APIPromise<ToolDefinition
|
|
65
|
-
|
|
64
|
+
get(name: string, query?: ToolGetParams, options?: Core.RequestOptions): Core.APIPromise<ToolDefinition>;
|
|
65
|
+
get(name: string, options?: Core.RequestOptions): Core.APIPromise<ToolDefinition>;
|
|
66
|
+
get(
|
|
67
|
+
name: string,
|
|
68
|
+
query: ToolGetParams | Core.RequestOptions = {},
|
|
69
|
+
options?: Core.RequestOptions,
|
|
70
|
+
): Core.APIPromise<ToolDefinition> {
|
|
71
|
+
if (isRequestOptions(query)) {
|
|
72
|
+
return this.get(name, {}, query);
|
|
73
|
+
}
|
|
74
|
+
return this._client.get(`/v1/tools/${name}`, { query, ...options });
|
|
66
75
|
}
|
|
67
76
|
}
|
|
68
77
|
|
|
@@ -94,7 +103,7 @@ export interface ExecuteToolRequest {
|
|
|
94
103
|
|
|
95
104
|
/**
|
|
96
105
|
* The time at which the tool should be run (optional). If not provided, the tool
|
|
97
|
-
* is run immediately
|
|
106
|
+
* is run immediately. Format ISO 8601: YYYY-MM-DDTHH:MM:SS
|
|
98
107
|
*/
|
|
99
108
|
run_at?: string;
|
|
100
109
|
|
|
@@ -166,15 +175,19 @@ export namespace ExecuteToolResponse {
|
|
|
166
175
|
}
|
|
167
176
|
|
|
168
177
|
export interface ToolDefinition {
|
|
178
|
+
fully_qualified_name: string;
|
|
179
|
+
|
|
169
180
|
input: ToolDefinition.Input;
|
|
170
181
|
|
|
171
182
|
name: string;
|
|
172
183
|
|
|
184
|
+
qualified_name: string;
|
|
185
|
+
|
|
173
186
|
toolkit: ToolDefinition.Toolkit;
|
|
174
187
|
|
|
175
188
|
description?: string;
|
|
176
189
|
|
|
177
|
-
|
|
190
|
+
formatted_schema?: Record<string, unknown>;
|
|
178
191
|
|
|
179
192
|
output?: ToolDefinition.Output;
|
|
180
193
|
|
|
@@ -218,6 +231,8 @@ export namespace ToolDefinition {
|
|
|
218
231
|
|
|
219
232
|
export interface Requirements {
|
|
220
233
|
authorization?: Requirements.Authorization;
|
|
234
|
+
|
|
235
|
+
secrets?: Array<Requirements.Secret>;
|
|
221
236
|
}
|
|
222
237
|
|
|
223
238
|
export namespace Requirements {
|
|
@@ -236,6 +251,10 @@ export namespace ToolDefinition {
|
|
|
236
251
|
scopes?: Array<string>;
|
|
237
252
|
}
|
|
238
253
|
}
|
|
254
|
+
|
|
255
|
+
export interface Secret {
|
|
256
|
+
key: string;
|
|
257
|
+
}
|
|
239
258
|
}
|
|
240
259
|
}
|
|
241
260
|
|
|
@@ -322,6 +341,11 @@ export interface ValueSchema {
|
|
|
322
341
|
}
|
|
323
342
|
|
|
324
343
|
export interface ToolListParams extends OffsetPageParams {
|
|
344
|
+
/**
|
|
345
|
+
* Comma separated tool formats that will be included in the response.
|
|
346
|
+
*/
|
|
347
|
+
include_format?: Array<'arcade' | 'openai' | 'anthropic'>;
|
|
348
|
+
|
|
325
349
|
/**
|
|
326
350
|
* Toolkit name
|
|
327
351
|
*/
|
|
@@ -352,7 +376,7 @@ export interface ToolExecuteParams {
|
|
|
352
376
|
|
|
353
377
|
/**
|
|
354
378
|
* The time at which the tool should be run (optional). If not provided, the tool
|
|
355
|
-
* is run immediately
|
|
379
|
+
* is run immediately. Format ISO 8601: YYYY-MM-DDTHH:MM:SS
|
|
356
380
|
*/
|
|
357
381
|
run_at?: string;
|
|
358
382
|
|
|
@@ -364,6 +388,13 @@ export interface ToolExecuteParams {
|
|
|
364
388
|
user_id?: string;
|
|
365
389
|
}
|
|
366
390
|
|
|
391
|
+
export interface ToolGetParams {
|
|
392
|
+
/**
|
|
393
|
+
* Comma separated tool formats that will be included in the response.
|
|
394
|
+
*/
|
|
395
|
+
include_format?: Array<'arcade' | 'openai' | 'anthropic'>;
|
|
396
|
+
}
|
|
397
|
+
|
|
367
398
|
Tools.ToolDefinitionsOffsetPage = ToolDefinitionsOffsetPage;
|
|
368
399
|
Tools.Scheduled = Scheduled;
|
|
369
400
|
Tools.Formatted = Formatted;
|
|
@@ -382,6 +413,7 @@ export declare namespace Tools {
|
|
|
382
413
|
type ToolListParams as ToolListParams,
|
|
383
414
|
type ToolAuthorizeParams as ToolAuthorizeParams,
|
|
384
415
|
type ToolExecuteParams as ToolExecuteParams,
|
|
416
|
+
type ToolGetParams as ToolGetParams,
|
|
385
417
|
};
|
|
386
418
|
|
|
387
419
|
export {
|
package/src/resources/tools.ts
CHANGED