@arcadeai/arcadejs 1.3.0 → 1.4.1
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/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 +3 -2
- 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/version.ts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./zod/types.js"), exports);
|
|
18
|
+
__exportStar(require("./zod/zod.js"), exports);
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/lib/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iDAA4B;AAC5B,+CAA0B"}
|
package/lib/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/lib/index.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { AuthorizationResponse } from "../../resources/shared.js";
|
|
2
|
+
import { ExecuteToolResponse } from "../../resources/tools/tools.js";
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
/**
|
|
5
|
+
* Response returned when a tool requires authorization before execution
|
|
6
|
+
*/
|
|
7
|
+
export interface ToolAuthorizationResponse {
|
|
8
|
+
authorization_required: true;
|
|
9
|
+
authorization_response: AuthorizationResponse;
|
|
10
|
+
url: string;
|
|
11
|
+
message: string;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Represents a tool with Zod schema validation and execution capabilities.
|
|
15
|
+
*/
|
|
16
|
+
export interface ZodTool {
|
|
17
|
+
name: string;
|
|
18
|
+
description: string | undefined;
|
|
19
|
+
parameters: z.ZodType;
|
|
20
|
+
output: z.ZodType | undefined;
|
|
21
|
+
execute: <T extends unknown>(input: T) => Promise<ExecuteToolResponse>;
|
|
22
|
+
executeOrAuthorize: <T extends unknown>(input: T) => Promise<ExecuteToolResponse | ToolAuthorizationResponse>;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Schema definition for an Arcade tool, containing the tool's name,
|
|
26
|
+
* description, and Zod-validated parameter structure.
|
|
27
|
+
*/
|
|
28
|
+
export interface ZodToolSchema {
|
|
29
|
+
name: string;
|
|
30
|
+
description: string | undefined;
|
|
31
|
+
parameters: z.ZodType;
|
|
32
|
+
output: z.ZodType | undefined;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/lib/zod/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAC/D,OAAO,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAC;AAClE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;GAEG;AACH,MAAM,WAAW,yBAAyB;IACxC,sBAAsB,EAAE,IAAI,CAAC;IAC7B,sBAAsB,EAAE,qBAAqB,CAAC;IAC9C,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,OAAO;IACtB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,GAAG,SAAS,CAAC;IAChC,UAAU,EAAE,CAAC,CAAC,OAAO,CAAC;IACtB,MAAM,EAAE,CAAC,CAAC,OAAO,GAAG,SAAS,CAAC;IAC9B,OAAO,EAAE,CAAC,CAAC,SAAS,OAAO,EAAE,KAAK,EAAE,CAAC,KAAK,OAAO,CAAC,mBAAmB,CAAC,CAAC;IACvE,kBAAkB,EAAE,CAAC,CAAC,SAAS,OAAO,EACpC,KAAK,EAAE,CAAC,KACL,OAAO,CAAC,mBAAmB,GAAG,yBAAyB,CAAC,CAAC;CAC/D;AAED;;;GAGG;AACH,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,GAAG,SAAS,CAAC;IAChC,UAAU,EAAE,CAAC,CAAC,OAAO,CAAC;IACtB,MAAM,EAAE,CAAC,CAAC,OAAO,GAAG,SAAS,CAAC;CAC/B"}
|
package/lib/zod/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/lib/zod/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.mjs","sourceRoot":"","sources":["../../src/lib/zod/types.ts"],"names":[],"mappings":""}
|
package/lib/zod/zod.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { ToolDefinition } from "../../resources/tools/tools.js";
|
|
2
|
+
import { ZodTool, ZodToolSchema } from "./types.js";
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { type Arcade } from '@arcadeai/arcadejs';
|
|
5
|
+
/**
|
|
6
|
+
* Checks if an error indicates that authorization for the tool is required
|
|
7
|
+
*/
|
|
8
|
+
export declare function isAuthorizationRequiredError(error: Error): boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Converts Arcade Tool Input to Zod schema
|
|
11
|
+
*/
|
|
12
|
+
export declare function convertParametersToZodSchema(parameters: ToolDefinition.Input): z.ZodType;
|
|
13
|
+
/**
|
|
14
|
+
* Converts Arcade Tool Output to Zod schema
|
|
15
|
+
*/
|
|
16
|
+
export declare function convertOutputToZodSchema(output: ToolDefinition.Output): z.ZodType;
|
|
17
|
+
/**
|
|
18
|
+
* Converts a single tool to a Zod-validated tool schema
|
|
19
|
+
* @param tool - The tool to convert
|
|
20
|
+
* @returns Zod-validated tool schema
|
|
21
|
+
* @throws ToolConversionError if the tool is invalid
|
|
22
|
+
*/
|
|
23
|
+
export declare function convertSingleToolToSchema(tool: ToolDefinition): ZodToolSchema;
|
|
24
|
+
/**
|
|
25
|
+
* Converts OpenAI formatted tools to Zod-validated tool schemas
|
|
26
|
+
* @param tools - Array of formatted tools
|
|
27
|
+
* @returns Array of Zod-validated tool schemas
|
|
28
|
+
*/
|
|
29
|
+
export declare function toZodSchema(tools: ToolDefinition[]): ZodToolSchema[];
|
|
30
|
+
/**
|
|
31
|
+
* Converts a single tool to a full tool with execution capabilities
|
|
32
|
+
* @param tool - The tool to convert
|
|
33
|
+
* @param client - Arcade client instance
|
|
34
|
+
* @param userId - User ID to use for the tool execution
|
|
35
|
+
* @returns Zod-validated tool with execution methods
|
|
36
|
+
* @throws ToolConversionError if the tool is invalid
|
|
37
|
+
*/
|
|
38
|
+
export declare function createZodTool({ tool, client, userId, }: {
|
|
39
|
+
tool: ToolDefinition;
|
|
40
|
+
client: Arcade;
|
|
41
|
+
userId: string;
|
|
42
|
+
}): ZodTool;
|
|
43
|
+
/**
|
|
44
|
+
* Converts formatted tools to full tools with execution capabilities
|
|
45
|
+
* @param tools - Array of formatted tools
|
|
46
|
+
* @param client - Arcade client instance
|
|
47
|
+
* @param userId - User ID to use for the tool execution
|
|
48
|
+
* @returns Array of Zod-validated tools with execution methods
|
|
49
|
+
*/
|
|
50
|
+
export declare function toZod({ tools, client, userId, }: {
|
|
51
|
+
tools: ToolDefinition[];
|
|
52
|
+
client: Arcade;
|
|
53
|
+
userId: string;
|
|
54
|
+
}): ZodTool[];
|
|
55
|
+
//# sourceMappingURL=zod.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod.d.ts","sourceRoot":"","sources":["../../src/lib/zod/zod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAClF,OAAO,EAA6B,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC5E,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAEjD;;GAEG;AACH,wBAAgB,4BAA4B,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAMlE;AAED;;GAEG;AACH,wBAAgB,4BAA4B,CAAC,UAAU,EAAE,cAAc,CAAC,KAAK,GAAG,CAAC,CAAC,OAAO,CAmBxF;AAyCD;;GAEG;AACH,wBAAgB,wBAAwB,CAAC,MAAM,EAAE,cAAc,CAAC,MAAM,GAAG,CAAC,CAAC,OAAO,CAMjF;AAED;;;;;GAKG;AACH,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,cAAc,GAAG,aAAa,CAU7E;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,cAAc,EAAE,GAAG,aAAa,EAAE,CAEpE;AAED;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,EAC5B,IAAI,EACJ,MAAM,EACN,MAAM,GACP,EAAE;IACD,IAAI,EAAE,cAAc,CAAC;IACrB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;CAChB,GAAG,OAAO,CAuDV;AAED;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,EACpB,KAAK,EACL,MAAM,EACN,MAAM,GACP,EAAE;IACD,KAAK,EAAE,cAAc,EAAE,CAAC;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;CAChB,GAAG,OAAO,EAAE,CAEZ"}
|
package/lib/zod/zod.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toZod = exports.createZodTool = exports.toZodSchema = exports.convertSingleToolToSchema = exports.convertOutputToZodSchema = exports.convertParametersToZodSchema = exports.isAuthorizationRequiredError = void 0;
|
|
4
|
+
const zod_1 = require("zod");
|
|
5
|
+
/**
|
|
6
|
+
* Checks if an error indicates that authorization for the tool is required
|
|
7
|
+
*/
|
|
8
|
+
function isAuthorizationRequiredError(error) {
|
|
9
|
+
return (error?.name === 'PermissionDeniedError' ||
|
|
10
|
+
error?.message?.includes('permission denied') ||
|
|
11
|
+
error?.message?.includes('authorization required'));
|
|
12
|
+
}
|
|
13
|
+
exports.isAuthorizationRequiredError = isAuthorizationRequiredError;
|
|
14
|
+
/**
|
|
15
|
+
* Converts Arcade Tool Input to Zod schema
|
|
16
|
+
*/
|
|
17
|
+
function convertParametersToZodSchema(parameters) {
|
|
18
|
+
if (!parameters.parameters || !Array.isArray(parameters.parameters)) {
|
|
19
|
+
return zod_1.z.object({});
|
|
20
|
+
}
|
|
21
|
+
const schemaObj = {};
|
|
22
|
+
for (const param of parameters.parameters) {
|
|
23
|
+
const { name, required, value_schema } = param;
|
|
24
|
+
let zodType = convertValueSchemaToZod(value_schema);
|
|
25
|
+
if (!required) {
|
|
26
|
+
zodType = zodType.optional();
|
|
27
|
+
}
|
|
28
|
+
schemaObj[name] = zodType;
|
|
29
|
+
}
|
|
30
|
+
return zod_1.z.object(schemaObj);
|
|
31
|
+
}
|
|
32
|
+
exports.convertParametersToZodSchema = convertParametersToZodSchema;
|
|
33
|
+
/**
|
|
34
|
+
* Converts a value schema to Zod type
|
|
35
|
+
*/
|
|
36
|
+
function convertValueSchemaToZod(schema) {
|
|
37
|
+
let baseType;
|
|
38
|
+
switch (schema.val_type) {
|
|
39
|
+
case 'string':
|
|
40
|
+
baseType = schema.enum ? zod_1.z.enum(schema.enum) : zod_1.z.string();
|
|
41
|
+
break;
|
|
42
|
+
case 'integer':
|
|
43
|
+
baseType = zod_1.z.number().int();
|
|
44
|
+
break;
|
|
45
|
+
case 'number':
|
|
46
|
+
baseType = zod_1.z.number();
|
|
47
|
+
break;
|
|
48
|
+
case 'boolean':
|
|
49
|
+
baseType = zod_1.z.boolean();
|
|
50
|
+
break;
|
|
51
|
+
case 'json':
|
|
52
|
+
baseType = zod_1.z.any();
|
|
53
|
+
break;
|
|
54
|
+
case 'array':
|
|
55
|
+
if (!schema.inner_val_type) {
|
|
56
|
+
throw new Error('Array type must have inner_val_type specified');
|
|
57
|
+
}
|
|
58
|
+
baseType = zod_1.z.array(convertValueSchemaToZod({ val_type: schema.inner_val_type }));
|
|
59
|
+
break;
|
|
60
|
+
default:
|
|
61
|
+
throw new Error(`Unsupported value type: ${schema.val_type}`);
|
|
62
|
+
}
|
|
63
|
+
return baseType;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Converts Arcade Tool Output to Zod schema
|
|
67
|
+
*/
|
|
68
|
+
function convertOutputToZodSchema(output) {
|
|
69
|
+
if (!output.value_schema) {
|
|
70
|
+
return zod_1.z.any();
|
|
71
|
+
}
|
|
72
|
+
return convertValueSchemaToZod(output.value_schema);
|
|
73
|
+
}
|
|
74
|
+
exports.convertOutputToZodSchema = convertOutputToZodSchema;
|
|
75
|
+
/**
|
|
76
|
+
* Converts a single tool to a Zod-validated tool schema
|
|
77
|
+
* @param tool - The tool to convert
|
|
78
|
+
* @returns Zod-validated tool schema
|
|
79
|
+
* @throws ToolConversionError if the tool is invalid
|
|
80
|
+
*/
|
|
81
|
+
function convertSingleToolToSchema(tool) {
|
|
82
|
+
const { qualified_name, description, input, output } = tool;
|
|
83
|
+
const zodParameters = convertParametersToZodSchema(input);
|
|
84
|
+
const zodOutput = output ? convertOutputToZodSchema(output) : undefined;
|
|
85
|
+
return {
|
|
86
|
+
name: qualified_name.replace(/\./g, '_'),
|
|
87
|
+
description,
|
|
88
|
+
parameters: zodParameters,
|
|
89
|
+
output: zodOutput,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
exports.convertSingleToolToSchema = convertSingleToolToSchema;
|
|
93
|
+
/**
|
|
94
|
+
* Converts OpenAI formatted tools to Zod-validated tool schemas
|
|
95
|
+
* @param tools - Array of formatted tools
|
|
96
|
+
* @returns Array of Zod-validated tool schemas
|
|
97
|
+
*/
|
|
98
|
+
function toZodSchema(tools) {
|
|
99
|
+
return tools.map(convertSingleToolToSchema);
|
|
100
|
+
}
|
|
101
|
+
exports.toZodSchema = toZodSchema;
|
|
102
|
+
/**
|
|
103
|
+
* Converts a single tool to a full tool with execution capabilities
|
|
104
|
+
* @param tool - The tool to convert
|
|
105
|
+
* @param client - Arcade client instance
|
|
106
|
+
* @param userId - User ID to use for the tool execution
|
|
107
|
+
* @returns Zod-validated tool with execution methods
|
|
108
|
+
* @throws ToolConversionError if the tool is invalid
|
|
109
|
+
*/
|
|
110
|
+
function createZodTool({ tool, client, userId, }) {
|
|
111
|
+
const schema = convertSingleToolToSchema(tool);
|
|
112
|
+
const { name, description, parameters, output } = schema;
|
|
113
|
+
return {
|
|
114
|
+
name,
|
|
115
|
+
description,
|
|
116
|
+
parameters,
|
|
117
|
+
output,
|
|
118
|
+
execute: async (input) => {
|
|
119
|
+
const validationResult = parameters.safeParse(input);
|
|
120
|
+
if (!validationResult.success) {
|
|
121
|
+
throw new Error(`Invalid input: ${validationResult.error.message}`);
|
|
122
|
+
}
|
|
123
|
+
return client.tools.execute({
|
|
124
|
+
tool_name: name,
|
|
125
|
+
input: validationResult.data,
|
|
126
|
+
user_id: userId,
|
|
127
|
+
});
|
|
128
|
+
},
|
|
129
|
+
executeOrAuthorize: async (input) => {
|
|
130
|
+
const validationResult = parameters.safeParse(input);
|
|
131
|
+
if (!validationResult.success) {
|
|
132
|
+
throw new Error(`Invalid input: ${validationResult.error.message}`);
|
|
133
|
+
}
|
|
134
|
+
try {
|
|
135
|
+
return await client.tools.execute({
|
|
136
|
+
tool_name: name,
|
|
137
|
+
input: validationResult.data,
|
|
138
|
+
user_id: userId,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
if (error instanceof Error && isAuthorizationRequiredError(error)) {
|
|
143
|
+
const response = (await client.tools.authorize({
|
|
144
|
+
tool_name: name,
|
|
145
|
+
user_id: userId,
|
|
146
|
+
}));
|
|
147
|
+
return {
|
|
148
|
+
authorization_required: true,
|
|
149
|
+
authorization_response: response,
|
|
150
|
+
url: response.url,
|
|
151
|
+
message: 'This tool requires authorization. Please visit the following URL to authorize the tool: ' +
|
|
152
|
+
response.url,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
throw error;
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
exports.createZodTool = createZodTool;
|
|
161
|
+
/**
|
|
162
|
+
* Converts formatted tools to full tools with execution capabilities
|
|
163
|
+
* @param tools - Array of formatted tools
|
|
164
|
+
* @param client - Arcade client instance
|
|
165
|
+
* @param userId - User ID to use for the tool execution
|
|
166
|
+
* @returns Array of Zod-validated tools with execution methods
|
|
167
|
+
*/
|
|
168
|
+
function toZod({ tools, client, userId, }) {
|
|
169
|
+
return tools.map((tool) => createZodTool({ tool, client, userId }));
|
|
170
|
+
}
|
|
171
|
+
exports.toZod = toZod;
|
|
172
|
+
//# sourceMappingURL=zod.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod.js","sourceRoot":"","sources":["../../src/lib/zod/zod.ts"],"names":[],"mappings":";;;AAEA,6BAAwB;AAGxB;;GAEG;AACH,SAAgB,4BAA4B,CAAC,KAAY;IACvD,OAAO,CACL,KAAK,EAAE,IAAI,KAAK,uBAAuB;QACvC,KAAK,EAAE,OAAO,EAAE,QAAQ,CAAC,mBAAmB,CAAC;QAC7C,KAAK,EAAE,OAAO,EAAE,QAAQ,CAAC,wBAAwB,CAAC,CACnD,CAAC;AACJ,CAAC;AAND,oEAMC;AAED;;GAEG;AACH,SAAgB,4BAA4B,CAAC,UAAgC;IAC3E,IAAI,CAAC,UAAU,CAAC,UAAU,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE;QACnE,OAAO,OAAC,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;KACrB;IAED,MAAM,SAAS,GAA8B,EAAE,CAAC;IAEhD,KAAK,MAAM,KAAK,I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BACjB,OAAO,EACL,0FAA0F;4BAC1F,QAAQ,CAAC,GAAG;qBACf,CAAC;iBACH;gBACD,MAAM,KAAK,CAAC;aACb;QACH,CAAC;KACF,CAAC;AACJ,CAAC;AA/DD,sCA+DC;AAED;;;;;;GAMG;AACH,SAAgB,KAAK,CAAC,EACpB,KAAK,EACL,MAAM,EACN,MAAM,GAKP;IACC,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,aAAa,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC;AACtE,CAAC;AAVD,sBAUC"}
|
package/lib/zod/zod.mjs
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* Checks if an error indicates that authorization for the tool is required
|
|
4
|
+
*/
|
|
5
|
+
export function isAuthorizationRequiredError(error) {
|
|
6
|
+
return (error?.name === 'PermissionDeniedError' ||
|
|
7
|
+
error?.message?.includes('permission denied') ||
|
|
8
|
+
error?.message?.includes('authorization required'));
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Converts Arcade Tool Input to Zod schema
|
|
12
|
+
*/
|
|
13
|
+
export function convertParametersToZodSchema(parameters) {
|
|
14
|
+
if (!parameters.parameters || !Array.isArray(parameters.parameters)) {
|
|
15
|
+
return z.object({});
|
|
16
|
+
}
|
|
17
|
+
const schemaObj = {};
|
|
18
|
+
for (const param of parameters.parameters) {
|
|
19
|
+
const { name, required, value_schema } = param;
|
|
20
|
+
let zodType = convertValueSchemaToZod(value_schema);
|
|
21
|
+
if (!required) {
|
|
22
|
+
zodType = zodType.optional();
|
|
23
|
+
}
|
|
24
|
+
schemaObj[name] = zodType;
|
|
25
|
+
}
|
|
26
|
+
return z.object(schemaObj);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Converts a value schema to Zod type
|
|
30
|
+
*/
|
|
31
|
+
function convertValueSchemaToZod(schema) {
|
|
32
|
+
let baseType;
|
|
33
|
+
switch (schema.val_type) {
|
|
34
|
+
case 'string':
|
|
35
|
+
baseType = schema.enum ? z.enum(schema.enum) : z.string();
|
|
36
|
+
break;
|
|
37
|
+
case 'integer':
|
|
38
|
+
baseType = z.number().int();
|
|
39
|
+
break;
|
|
40
|
+
case 'number':
|
|
41
|
+
baseType = z.number();
|
|
42
|
+
break;
|
|
43
|
+
case 'boolean':
|
|
44
|
+
baseType = z.boolean();
|
|
45
|
+
break;
|
|
46
|
+
case 'json':
|
|
47
|
+
baseType = z.any();
|
|
48
|
+
break;
|
|
49
|
+
case 'array':
|
|
50
|
+
if (!schema.inner_val_type) {
|
|
51
|
+
throw new Error('Array type must have inner_val_type specified');
|
|
52
|
+
}
|
|
53
|
+
baseType = z.array(convertValueSchemaToZod({ val_type: schema.inner_val_type }));
|
|
54
|
+
break;
|
|
55
|
+
default:
|
|
56
|
+
throw new Error(`Unsupported value type: ${schema.val_type}`);
|
|
57
|
+
}
|
|
58
|
+
return baseType;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Converts Arcade Tool Output to Zod schema
|
|
62
|
+
*/
|
|
63
|
+
export function convertOutputToZodSchema(output) {
|
|
64
|
+
if (!output.value_schema) {
|
|
65
|
+
return z.any();
|
|
66
|
+
}
|
|
67
|
+
return convertValueSchemaToZod(output.value_schema);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Converts a single tool to a Zod-validated tool schema
|
|
71
|
+
* @param tool - The tool to convert
|
|
72
|
+
* @returns Zod-validated tool schema
|
|
73
|
+
* @throws ToolConversionError if the tool is invalid
|
|
74
|
+
*/
|
|
75
|
+
export function convertSingleToolToSchema(tool) {
|
|
76
|
+
const { qualified_name, description, input, output } = tool;
|
|
77
|
+
const zodParameters = convertParametersToZodSchema(input);
|
|
78
|
+
const zodOutput = output ? convertOutputToZodSchema(output) : undefined;
|
|
79
|
+
return {
|
|
80
|
+
name: qualified_name.replace(/\./g, '_'),
|
|
81
|
+
description,
|
|
82
|
+
parameters: zodParameters,
|
|
83
|
+
output: zodOutput,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Converts OpenAI formatted tools to Zod-validated tool schemas
|
|
88
|
+
* @param tools - Array of formatted tools
|
|
89
|
+
* @returns Array of Zod-validated tool schemas
|
|
90
|
+
*/
|
|
91
|
+
export function toZodSchema(tools) {
|
|
92
|
+
return tools.map(convertSingleToolToSchema);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Converts a single tool to a full tool with execution capabilities
|
|
96
|
+
* @param tool - The tool to convert
|
|
97
|
+
* @param client - Arcade client instance
|
|
98
|
+
* @param userId - User ID to use for the tool execution
|
|
99
|
+
* @returns Zod-validated tool with execution methods
|
|
100
|
+
* @throws ToolConversionError if the tool is invalid
|
|
101
|
+
*/
|
|
102
|
+
export function createZodTool({ tool, client, userId, }) {
|
|
103
|
+
const schema = convertSingleToolToSchema(tool);
|
|
104
|
+
const { name, description, parameters, output } = schema;
|
|
105
|
+
return {
|
|
106
|
+
name,
|
|
107
|
+
description,
|
|
108
|
+
parameters,
|
|
109
|
+
output,
|
|
110
|
+
execute: async (input) => {
|
|
111
|
+
const validationResult = parameters.safeParse(input);
|
|
112
|
+
if (!validationResult.success) {
|
|
113
|
+
throw new Error(`Invalid input: ${validationResult.error.message}`);
|
|
114
|
+
}
|
|
115
|
+
return client.tools.execute({
|
|
116
|
+
tool_name: name,
|
|
117
|
+
input: validationResult.data,
|
|
118
|
+
user_id: userId,
|
|
119
|
+
});
|
|
120
|
+
},
|
|
121
|
+
executeOrAuthorize: async (input) => {
|
|
122
|
+
const validationResult = parameters.safeParse(input);
|
|
123
|
+
if (!validationResult.success) {
|
|
124
|
+
throw new Error(`Invalid input: ${validationResult.error.message}`);
|
|
125
|
+
}
|
|
126
|
+
try {
|
|
127
|
+
return await client.tools.execute({
|
|
128
|
+
tool_name: name,
|
|
129
|
+
input: validationResult.data,
|
|
130
|
+
user_id: userId,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
if (error instanceof Error && isAuthorizationRequiredError(error)) {
|
|
135
|
+
const response = (await client.tools.authorize({
|
|
136
|
+
tool_name: name,
|
|
137
|
+
user_id: userId,
|
|
138
|
+
}));
|
|
139
|
+
return {
|
|
140
|
+
authorization_required: true,
|
|
141
|
+
authorization_response: response,
|
|
142
|
+
url: response.url,
|
|
143
|
+
message: 'This tool requires authorization. Please visit the following URL to authorize the tool: ' +
|
|
144
|
+
response.url,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
throw error;
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Converts formatted tools to full tools with execution capabilities
|
|
154
|
+
* @param tools - Array of formatted tools
|
|
155
|
+
* @param client - Arcade client instance
|
|
156
|
+
* @param userId - User ID to use for the tool execution
|
|
157
|
+
* @returns Array of Zod-validated tools with execution methods
|
|
158
|
+
*/
|
|
159
|
+
export function toZod({ tools, client, userId, }) {
|
|
160
|
+
return tools.map((tool) => createZodTool({ tool, client, userId }));
|
|
161
|
+
}
|
|
162
|
+
//# sourceMappingURL=zod.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod.mjs","sourceRoot":"","sources":["../../src/lib/zod/zod.ts"],"names":[],"mappings":"OAEO,EAAE,CAAC,EAAE,MAAM,KAAK;AAGvB;;GAEG;AACH,MAAM,UAAU,4BAA4B,CAAC,KAAY;IACvD,OAAO,CACL,KAAK,EAAE,IAAI,KAAK,uBAAuB;QACvC,KAAK,EAAE,OAAO,EAAE,QAAQ,CAAC,mBAAmB,CAAC;QAC7C,KAAK,EAAE,OAAO,EAAE,QAAQ,CAAC,wBAAwB,CAAC,CACnD,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,4BAA4B,CAAC,UAAgC;IAC3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BAC7C,SAAS,EAAE,IAAI;wBACf,OAAO,EAAE,MAAM;qBAChB,CAAC,CAAoB,CAAC;oBAEvB,OAAO;wBACL,sBAAsB,EAAE,IAAI;wBAC5B,sBAAsB,EAAE,QAAQ;wBAChC,GAAG,EAAE,QAAQ,CAAC,GAAG;wBACjB,OAAO,EACL,0FAA0F;4BAC1F,QAAQ,CAAC,GAAG;qBACf,CAAC;iBACH;gBACD,MAAM,KAAK,CAAC;aACb;QACH,CAAC;KACF,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,KAAK,CAAC,EACpB,KAAK,EACL,MAAM,EACN,MAAM,GAKP;IACC,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,aAAa,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC;AACtE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arcadeai/arcadejs",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.1",
|
|
4
4
|
"description": "The official TypeScript library for the Arcade API",
|
|
5
5
|
"author": "Arcade <dev@arcade.dev>",
|
|
6
6
|
"types": "./index.d.ts",
|
|
@@ -28,7 +28,8 @@
|
|
|
28
28
|
"agentkeepalive": "^4.2.1",
|
|
29
29
|
"form-data-encoder": "1.7.2",
|
|
30
30
|
"formdata-node": "^4.3.2",
|
|
31
|
-
"node-fetch": "^2.6.7"
|
|
31
|
+
"node-fetch": "^2.6.7",
|
|
32
|
+
"zod": "^3.24.2"
|
|
32
33
|
},
|
|
33
34
|
"sideEffects": [
|
|
34
35
|
"./_shims/index.js",
|
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 '@arcadeai/arcadejs';
|
|
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/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = '1.
|
|
1
|
+
export const VERSION = '1.4.1'; // x-release-please-version
|
package/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const VERSION = "1.
|
|
1
|
+
export declare const VERSION = "1.4.1";
|
|
2
2
|
//# sourceMappingURL=version.d.ts.map
|
package/version.js
CHANGED
package/version.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export const VERSION = '1.
|
|
1
|
+
export const VERSION = '1.4.1'; // x-release-please-version
|
|
2
2
|
//# sourceMappingURL=version.mjs.map
|