@chatbotkit/cli 1.8.0 → 1.9.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.
Files changed (45) hide show
  1. package/README.md +1 -0
  2. package/bin/cbk.js +7 -0
  3. package/dist/cjs/command/api/conversation/index.cjs +9 -4
  4. package/dist/cjs/command/api/conversation/message/index.cjs +9 -4
  5. package/dist/cjs/command/api/dataset/index.cjs +9 -4
  6. package/dist/cjs/command/api/partner/user/index.cjs +9 -4
  7. package/dist/cjs/command/api/skillset/index.cjs +9 -4
  8. package/dist/cjs/command/chat/index.cjs +7 -4
  9. package/dist/cjs/command/solution/index.cjs +53 -0
  10. package/dist/cjs/command/solution/index.d.ts +7 -0
  11. package/dist/cjs/command/solution/resource/index.cjs +44 -0
  12. package/dist/cjs/command/solution/resource/index.d.ts +5 -0
  13. package/dist/cjs/index.cjs +4 -2
  14. package/dist/cjs/index.d.ts +1 -1
  15. package/dist/cjs/input.cjs +18 -0
  16. package/dist/cjs/input.d.ts +1 -0
  17. package/dist/cjs/output.cjs +10 -2
  18. package/dist/cjs/output.d.ts +2 -0
  19. package/dist/cjs/proxy.cjs +22 -0
  20. package/dist/cjs/proxy.d.ts +7 -0
  21. package/dist/cjs/solution/index.cjs +325 -0
  22. package/dist/cjs/solution/index.d.ts +822 -0
  23. package/dist/esm/command/api/conversation/index.js +9 -4
  24. package/dist/esm/command/api/conversation/message/index.js +9 -4
  25. package/dist/esm/command/api/dataset/index.js +9 -4
  26. package/dist/esm/command/api/partner/user/index.js +9 -4
  27. package/dist/esm/command/api/skillset/index.js +9 -4
  28. package/dist/esm/command/chat/index.js +7 -4
  29. package/dist/esm/command/solution/index.d.ts +7 -0
  30. package/dist/esm/command/solution/index.js +49 -0
  31. package/dist/esm/command/solution/resource/index.d.ts +5 -0
  32. package/dist/esm/command/solution/resource/index.js +40 -0
  33. package/dist/esm/index.d.ts +1 -1
  34. package/dist/esm/index.js +4 -2
  35. package/dist/esm/input.d.ts +1 -0
  36. package/dist/esm/input.js +13 -0
  37. package/dist/esm/output.d.ts +2 -0
  38. package/dist/esm/output.js +8 -1
  39. package/dist/esm/proxy.d.ts +7 -0
  40. package/dist/esm/proxy.js +17 -0
  41. package/dist/esm/solution/index.d.ts +822 -0
  42. package/dist/esm/solution/index.js +307 -0
  43. package/dist/tsconfig.cjs.tsbuildinfo +1 -1
  44. package/dist/tsconfig.esm.tsbuildinfo +1 -1
  45. package/package.json +135 -2
@@ -0,0 +1,325 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getArrayBackedObject = exports.ArrayBackedObject = exports.Solution = exports.SitemapIntegrationResource = exports.WidgetIntegrationResource = exports.SkillsetResource = exports.DatasetResource = exports.BotResource = exports.Resource = exports.SolutionConfigSchema = exports.ResourceConfigSchema = exports.SitemapIntegrationResourceConfigSchema = exports.WidgetIntegrationResourceConfigSchema = exports.SkillsetResourceConfigSchema = exports.DatasetResourceConfigSchema = exports.BotResourceConfigSchema = exports.BasicResourceConfigSchema = exports.replaceEnvVars = exports.getSolutionFileNameAndPath = exports.getSolutionFilePath = exports.getSolutionFileName = exports.getSolutionFolderPath = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const env_js_1 = require("../env.cjs");
6
+ const output_js_1 = require("../output.cjs");
7
+ const sdk_1 = tslib_1.__importDefault(require("@chatbotkit/sdk"));
8
+ const promises_1 = tslib_1.__importDefault(require("fs/promises"));
9
+ const path_1 = tslib_1.__importDefault(require("path"));
10
+ const zod_1 = require("zod");
11
+ function getSolutionFolderPath() {
12
+ const folderPath = path_1.default.join('.chatbotkit', 'solutions');
13
+ return folderPath;
14
+ }
15
+ exports.getSolutionFolderPath = getSolutionFolderPath;
16
+ function getSolutionFileName(name) {
17
+ const fileName = name.toLowerCase().replace(/\W/g, '_').replace(/_+/g, '_') + '.json';
18
+ return fileName;
19
+ }
20
+ exports.getSolutionFileName = getSolutionFileName;
21
+ function getSolutionFilePath(name) {
22
+ const fileName = getSolutionFileName(name);
23
+ const filePath = path_1.default.join(getSolutionFolderPath(), fileName);
24
+ return filePath;
25
+ }
26
+ exports.getSolutionFilePath = getSolutionFilePath;
27
+ function getSolutionFileNameAndPath(name) {
28
+ const fileName = getSolutionFileName(name);
29
+ const filePath = getSolutionFilePath(name);
30
+ return { fileName, filePath };
31
+ }
32
+ exports.getSolutionFileNameAndPath = getSolutionFileNameAndPath;
33
+ function replaceEnvVars(value) {
34
+ if (!value) {
35
+ return value;
36
+ }
37
+ if (typeof value === 'string') {
38
+ return value.replace(/\${env\.([A-Z_]+)}/g, (_, name) => {
39
+ const envValue = process.env[name];
40
+ if (envValue === undefined) {
41
+ throw new output_js_1.CommandError(`Environment variable ${name} is not set`);
42
+ }
43
+ return envValue;
44
+ });
45
+ }
46
+ if (Array.isArray(value)) {
47
+ return value.map(replaceEnvVars);
48
+ }
49
+ if (typeof value === 'object') {
50
+ return Object.fromEntries(Object.entries(value).map(([key, value]) => [key, replaceEnvVars(value)]));
51
+ }
52
+ return value;
53
+ }
54
+ exports.replaceEnvVars = replaceEnvVars;
55
+ exports.BasicResourceConfigSchema = zod_1.z.object({
56
+ type: zod_1.z.string(),
57
+ slug: zod_1.z.string().optional(),
58
+ id: zod_1.z.string().optional(),
59
+ name: zod_1.z.string(),
60
+ description: zod_1.z.string().optional(),
61
+ properties: zod_1.z.record(zod_1.z.unknown()),
62
+ });
63
+ exports.BotResourceConfigSchema = exports.BasicResourceConfigSchema.extend({
64
+ type: zod_1.z.literal('bot'),
65
+ properties: zod_1.z.object({
66
+ model: zod_1.z.string().optional(),
67
+ backstory: zod_1.z.string().optional(),
68
+ datasetId: zod_1.z.string().optional(),
69
+ skillsetId: zod_1.z.string().optional(),
70
+ moderation: zod_1.z.boolean().optional(),
71
+ privacy: zod_1.z.boolean().optional(),
72
+ }),
73
+ });
74
+ exports.DatasetResourceConfigSchema = exports.BasicResourceConfigSchema.extend({
75
+ type: zod_1.z.literal('dataset'),
76
+ properties: zod_1.z.object({}),
77
+ });
78
+ exports.SkillsetResourceConfigSchema = exports.BasicResourceConfigSchema.extend({
79
+ type: zod_1.z.literal('skillset'),
80
+ properties: zod_1.z.object({}),
81
+ });
82
+ exports.WidgetIntegrationResourceConfigSchema = exports.BasicResourceConfigSchema.extend({
83
+ type: zod_1.z.literal('widgetIntegration'),
84
+ properties: zod_1.z.object({}),
85
+ });
86
+ exports.SitemapIntegrationResourceConfigSchema = exports.BasicResourceConfigSchema.extend({
87
+ type: zod_1.z.literal('sitemapIntegration'),
88
+ properties: zod_1.z.object({
89
+ url: zod_1.z.string().url(),
90
+ datasetId: zod_1.z.string(),
91
+ }),
92
+ });
93
+ exports.ResourceConfigSchema = zod_1.z.union([
94
+ exports.BotResourceConfigSchema,
95
+ exports.DatasetResourceConfigSchema,
96
+ exports.SkillsetResourceConfigSchema,
97
+ exports.WidgetIntegrationResourceConfigSchema,
98
+ exports.SitemapIntegrationResourceConfigSchema,
99
+ ]);
100
+ exports.SolutionConfigSchema = zod_1.z.object({
101
+ version: zod_1.z.literal(1),
102
+ resources: zod_1.z.array(exports.ResourceConfigSchema),
103
+ });
104
+ class Resource {
105
+ constructor(config) {
106
+ this.config = config;
107
+ }
108
+ get type() {
109
+ return this.config.type;
110
+ }
111
+ get slug() {
112
+ var _a;
113
+ return ((_a = this.config.slug) !== null && _a !== void 0 ? _a : this.config.name.toLowerCase().replace(/\W/g, '_').replace(/_+/g, '_'));
114
+ }
115
+ get id() {
116
+ return this.config.id;
117
+ }
118
+ get name() {
119
+ return this.config.name;
120
+ }
121
+ get description() {
122
+ return this.config.description;
123
+ }
124
+ get baseClient() {
125
+ const client = new sdk_1.default({
126
+ secret: (0, env_js_1.getSECRET)(),
127
+ runAsUserId: (0, env_js_1.getRUNAS_USERID)(),
128
+ });
129
+ return client;
130
+ }
131
+ get client() {
132
+ throw new Error('Not implemented');
133
+ }
134
+ async sync() {
135
+ if (this.config.id) {
136
+ await this.client.update(this.config.id, {
137
+ ...this.config.properties,
138
+ name: this.config.name,
139
+ description: this.config.description,
140
+ });
141
+ }
142
+ else {
143
+ const { id } = await this.client.create({
144
+ ...this.config.properties,
145
+ name: this.config.name,
146
+ description: this.config.description,
147
+ });
148
+ this.config.id = id;
149
+ }
150
+ }
151
+ }
152
+ exports.Resource = Resource;
153
+ class BotResource extends Resource {
154
+ get client() {
155
+ return this.baseClient.bot;
156
+ }
157
+ }
158
+ exports.BotResource = BotResource;
159
+ class DatasetResource extends Resource {
160
+ get client() {
161
+ return this.baseClient.dataset;
162
+ }
163
+ }
164
+ exports.DatasetResource = DatasetResource;
165
+ class SkillsetResource extends Resource {
166
+ get client() {
167
+ return this.baseClient.skillset;
168
+ }
169
+ }
170
+ exports.SkillsetResource = SkillsetResource;
171
+ class WidgetIntegrationResource extends Resource {
172
+ get client() {
173
+ return this.baseClient.integration.widget;
174
+ }
175
+ }
176
+ exports.WidgetIntegrationResource = WidgetIntegrationResource;
177
+ class SitemapIntegrationResource extends Resource {
178
+ get client() {
179
+ return this.baseClient.integration.sitemap;
180
+ }
181
+ }
182
+ exports.SitemapIntegrationResource = SitemapIntegrationResource;
183
+ class Solution {
184
+ constructor(config) {
185
+ this.config = config;
186
+ }
187
+ get baseClient() {
188
+ const client = new sdk_1.default({
189
+ secret: (0, env_js_1.getSECRET)(),
190
+ runAsUserId: (0, env_js_1.getRUNAS_USERID)(),
191
+ });
192
+ return client;
193
+ }
194
+ get resources() {
195
+ return this.config.resources.map((resource) => {
196
+ if (resource.type === 'bot') {
197
+ return new BotResource(resource);
198
+ }
199
+ else if (resource.type === 'dataset') {
200
+ return new DatasetResource(resource);
201
+ }
202
+ else if (resource.type === 'skillset') {
203
+ return new SkillsetResource(resource);
204
+ }
205
+ else if (resource.type === 'widgetIntegration') {
206
+ return new WidgetIntegrationResource(resource);
207
+ }
208
+ else if (resource.type === 'sitemapIntegration') {
209
+ return new SitemapIntegrationResource(resource);
210
+ }
211
+ else {
212
+ throw new Error(`Unknown resource type: ${resource.type}`);
213
+ }
214
+ });
215
+ }
216
+ get bots() {
217
+ return (this.resources.filter((resource) => resource instanceof BotResource));
218
+ }
219
+ get bot() {
220
+ return getArrayBackedObject(this.bots);
221
+ }
222
+ get datasets() {
223
+ return (this.resources.filter((resource) => resource instanceof DatasetResource));
224
+ }
225
+ get dataset() {
226
+ return getArrayBackedObject(this.datasets);
227
+ }
228
+ get skillsets() {
229
+ return (this.resources.filter((resource) => resource instanceof SkillsetResource));
230
+ }
231
+ get skillset() {
232
+ return getArrayBackedObject(this.skillsets);
233
+ }
234
+ get widgetIntegrations() {
235
+ return (this.resources.filter((resource) => resource instanceof WidgetIntegrationResource));
236
+ }
237
+ get widgetIntegration() {
238
+ return getArrayBackedObject(this.widgetIntegrations);
239
+ }
240
+ get sitemapIntegrations() {
241
+ return (this.resources.filter((resource) => resource instanceof SitemapIntegrationResource));
242
+ }
243
+ get sitemapIntegration() {
244
+ return getArrayBackedObject(this.sitemapIntegrations);
245
+ }
246
+ async sync() {
247
+ await Promise.all(this.resources.map((resource) => resource.sync()));
248
+ }
249
+ }
250
+ exports.Solution = Solution;
251
+ Solution.list = async function () {
252
+ await promises_1.default.mkdir(getSolutionFolderPath(), { recursive: true });
253
+ const files = await promises_1.default.readdir('.chatbotkit/solutions');
254
+ return files
255
+ .filter((file) => file.endsWith('.json'))
256
+ .map((file) => file.replace(/\.json$/, ''));
257
+ };
258
+ Solution.create = async function (name) {
259
+ await promises_1.default.mkdir('.chatbotkit/solutions', { recursive: true });
260
+ const fileName = getSolutionFileName(name);
261
+ const filePath = getSolutionFilePath(name);
262
+ if (await promises_1.default.stat(filePath).catch(() => null)) {
263
+ throw new output_js_1.CommandError(`Solution ${fileName} already exists`);
264
+ }
265
+ const config = {
266
+ version: 1,
267
+ resources: [],
268
+ };
269
+ await promises_1.default.writeFile(filePath, JSON.stringify(config, null, 2));
270
+ };
271
+ Solution.delete = async function (name) {
272
+ const fileName = getSolutionFileName(name);
273
+ const filePath = getSolutionFilePath(name);
274
+ if (!(await promises_1.default.stat(filePath).catch(() => null))) {
275
+ throw new output_js_1.CommandError(`Solution ${fileName} does not exist`);
276
+ }
277
+ await promises_1.default.unlink(filePath);
278
+ };
279
+ Solution.load = async function (config) {
280
+ let solutionConfig;
281
+ if (typeof config === 'string') {
282
+ const { fileName, filePath } = getSolutionFileNameAndPath(config);
283
+ if (!(await promises_1.default.stat(filePath).catch(() => null))) {
284
+ throw new output_js_1.CommandError(`Solution ${fileName} does not exist`);
285
+ }
286
+ try {
287
+ solutionConfig = JSON.parse(await promises_1.default.readFile(filePath, 'utf8'));
288
+ }
289
+ catch (error) {
290
+ throw new output_js_1.CommandError(`Failed to load solution ${fileName}: ${error.message}`);
291
+ }
292
+ }
293
+ else {
294
+ solutionConfig = config;
295
+ }
296
+ const parsedConfig = exports.SolutionConfigSchema.safeParse(solutionConfig);
297
+ if (!parsedConfig.success) {
298
+ throw new output_js_1.CommandError(`Invalid solution configuration`);
299
+ }
300
+ parsedConfig.data = replaceEnvVars(parsedConfig.data);
301
+ return new Solution(parsedConfig.data);
302
+ };
303
+ Solution.save = async function (name, solution) {
304
+ const filePath = getSolutionFilePath(name);
305
+ await promises_1.default.writeFile(filePath, JSON.stringify(solution.config, null, 2));
306
+ };
307
+ class ArrayBackedObject {
308
+ constructor(array) {
309
+ this.array = array;
310
+ return new Proxy(this, {
311
+ get: (target, property) => {
312
+ const item = target.array.find((item) => { var _a; return ((_a = (item)) === null || _a === void 0 ? void 0 : _a.slug) === property; });
313
+ if (item) {
314
+ return item;
315
+ }
316
+ return undefined;
317
+ },
318
+ });
319
+ }
320
+ }
321
+ exports.ArrayBackedObject = ArrayBackedObject;
322
+ function getArrayBackedObject(array) {
323
+ return new ArrayBackedObject(array);
324
+ }
325
+ exports.getArrayBackedObject = getArrayBackedObject;