openkbs 0.0.66 → 0.0.70

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 (63) hide show
  1. package/README.md +1 -0
  2. package/elastic/README.md +1 -1
  3. package/elastic/functions.md +5 -5
  4. package/elastic/pulse.md +2 -2
  5. package/package.json +2 -2
  6. package/scripts/deploy.js +68 -0
  7. package/src/actions.js +59 -0
  8. package/src/index.js +8 -6
  9. package/templates/.claude/skills/openkbs/SKILL.md +37 -8
  10. package/templates/.claude/skills/openkbs/examples/monitoring-bot/README.md +55 -0
  11. package/templates/.claude/skills/openkbs/examples/monitoring-bot/app/instructions.txt +40 -0
  12. package/templates/.claude/skills/openkbs/examples/monitoring-bot/app/settings.json +41 -0
  13. package/templates/.claude/skills/openkbs/examples/monitoring-bot/openkbs.json +3 -0
  14. package/templates/.claude/skills/openkbs/examples/monitoring-bot/src/Events/actions.js +141 -0
  15. package/templates/.claude/skills/openkbs/examples/monitoring-bot/src/Events/handler.js +32 -0
  16. package/templates/.claude/skills/openkbs/examples/monitoring-bot/src/Events/memoryHelpers.js +91 -0
  17. package/templates/.claude/skills/openkbs/examples/monitoring-bot/src/Events/onCronjob.js +105 -0
  18. package/templates/.claude/skills/openkbs/examples/monitoring-bot/src/Events/onPublicAPIRequest.js +165 -0
  19. package/templates/.claude/skills/openkbs/examples/monitoring-bot/src/Events/onRequest.js +2 -0
  20. package/templates/.claude/skills/openkbs/examples/monitoring-bot/src/Events/onResponse.js +2 -0
  21. package/templates/.claude/skills/openkbs/examples/monitoring-bot/src/Frontend/contentRender.js +74 -0
  22. package/templates/.claude/skills/openkbs/examples/monitoring-bot/src/Frontend/contentRender.json +3 -0
  23. package/templates/.claude/skills/openkbs/examples/nodejs-demo/functions/auth/index.mjs +228 -0
  24. package/templates/.claude/skills/openkbs/examples/nodejs-demo/functions/auth/package.json +7 -0
  25. package/templates/.claude/skills/openkbs/examples/nodejs-demo/functions/posts/index.mjs +287 -0
  26. package/templates/.claude/skills/openkbs/examples/nodejs-demo/functions/posts/package.json +10 -0
  27. package/templates/.claude/skills/openkbs/examples/nodejs-demo/functions/settings.json +4 -0
  28. package/templates/.claude/skills/openkbs/examples/nodejs-demo/openkbs.json +16 -0
  29. package/templates/.claude/skills/openkbs/examples/nodejs-demo/site/index.html +658 -0
  30. package/templates/.claude/skills/openkbs/examples/nodejs-demo/site/settings.json +4 -0
  31. package/templates/.claude/skills/openkbs/patterns/cronjob-batch-processing.md +278 -0
  32. package/templates/.claude/skills/openkbs/patterns/cronjob-monitoring.md +341 -0
  33. package/templates/.claude/skills/openkbs/patterns/file-upload.md +205 -0
  34. package/templates/.claude/skills/openkbs/patterns/image-generation.md +139 -0
  35. package/templates/.claude/skills/openkbs/patterns/memory-system.md +264 -0
  36. package/templates/.claude/skills/openkbs/patterns/public-api-item-proxy.md +254 -0
  37. package/templates/.claude/skills/openkbs/patterns/scheduled-tasks.md +157 -0
  38. package/templates/.claude/skills/openkbs/patterns/telegram-webhook.md +424 -0
  39. package/templates/.claude/skills/openkbs/patterns/telegram.md +222 -0
  40. package/templates/.claude/skills/openkbs/patterns/vectordb-archive.md +231 -0
  41. package/templates/.claude/skills/openkbs/patterns/video-generation.md +145 -0
  42. package/templates/.claude/skills/openkbs/patterns/web-publishing.md +257 -0
  43. package/templates/.claude/skills/openkbs/reference/backend-sdk.md +13 -2
  44. package/templates/.claude/skills/openkbs/reference/elastic-services.md +61 -29
  45. package/templates/platform/README.md +35 -0
  46. package/templates/platform/agents/assistant/app/icon.png +0 -0
  47. package/templates/platform/agents/assistant/app/instructions.txt +13 -0
  48. package/templates/platform/agents/assistant/app/settings.json +13 -0
  49. package/templates/platform/agents/assistant/src/Events/actions.js +50 -0
  50. package/templates/platform/agents/assistant/src/Events/handler.js +54 -0
  51. package/templates/platform/agents/assistant/src/Events/onRequest.js +3 -0
  52. package/templates/platform/agents/assistant/src/Events/onRequest.json +5 -0
  53. package/templates/platform/agents/assistant/src/Events/onResponse.js +3 -0
  54. package/templates/platform/agents/assistant/src/Events/onResponse.json +5 -0
  55. package/templates/platform/agents/assistant/src/Frontend/contentRender.js +27 -0
  56. package/templates/platform/agents/assistant/src/Frontend/contentRender.json +10 -0
  57. package/templates/platform/functions/api/index.mjs +63 -0
  58. package/templates/platform/functions/api/package.json +4 -0
  59. package/templates/platform/openkbs.json +19 -0
  60. package/templates/platform/site/index.html +75 -0
  61. package/version.json +3 -3
  62. package/templates/.claude/skills/openkbs/examples/ai-copywriter-agent/scripts/run_job.js +0 -26
  63. package/templates/.claude/skills/openkbs/examples/ai-copywriter-agent/scripts/utils/agent_client.js +0 -265
@@ -1,265 +0,0 @@
1
- const https = require('https');
2
- const fs = require('fs');
3
- const readline = require('readline');
4
- const path = require('path');
5
- const { URL } = require('url');
6
-
7
- class OpenKBSAgentClient {
8
- constructor() {
9
- this.settings = this.findSettings();
10
- this.secretsPath = this.findSecretsPath();
11
- this.apiKey = null;
12
- }
13
-
14
- findSettings() {
15
- let currentDir = __dirname;
16
-
17
- while (currentDir !== path.parse(currentDir).root) {
18
- const settingsPath = path.join(currentDir, 'app', 'settings.json');
19
- if (fs.existsSync(settingsPath)) {
20
- return JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
21
- }
22
- currentDir = path.dirname(currentDir);
23
- }
24
-
25
- throw new Error('Could not find app/settings.json in parent directories');
26
- }
27
-
28
- findSecretsPath() {
29
- let currentDir = __dirname;
30
-
31
- while (currentDir !== path.parse(currentDir).root) {
32
- const settingsPath = path.join(currentDir, 'app', 'settings.json');
33
- if (fs.existsSync(settingsPath)) {
34
- const secretsPath = path.join(currentDir, '.openkbs', 'secrets.json');
35
- return secretsPath;
36
- }
37
- currentDir = path.dirname(currentDir);
38
- }
39
-
40
- throw new Error('Could not find agent directory with app/settings.json');
41
- }
42
-
43
- async getApiKey() {
44
- if (this.apiKey) return this.apiKey;
45
-
46
- if (fs.existsSync(this.secretsPath)) {
47
- const secrets = JSON.parse(fs.readFileSync(this.secretsPath, 'utf8'));
48
- this.apiKey = secrets.apiKey;
49
- return this.apiKey;
50
- }
51
-
52
- this.apiKey = await this.promptForApiKey();
53
- this.saveApiKey(this.apiKey);
54
- return this.apiKey;
55
- }
56
-
57
- async init() {
58
- await this.getApiKey();
59
- }
60
-
61
- async promptForApiKey() {
62
- return new Promise((resolve) => {
63
- const rl = readline.createInterface({
64
- input: process.stdin,
65
- output: process.stdout
66
- });
67
-
68
- console.log(`Please generate an API key from: https://${this.settings.kbId}.apps.openkbs.com/?tab=access&createAPIKey=api-${+new Date()}`);
69
-
70
- rl.question('Enter your API key: ', (key) => {
71
- rl.close();
72
- resolve(key);
73
- });
74
-
75
- rl._writeToOutput = (str) => {
76
- if (str === '\n' || str === '\r\n') {
77
- rl.output.write(str);
78
- } else if (str.match(/[\x08\x7f]/)) {
79
- rl.output.write(str);
80
- } else if (rl.line && str.length === 1) {
81
- rl.output.write('*');
82
- } else {
83
- rl.output.write(str);
84
- }
85
- };
86
- });
87
- }
88
-
89
- saveApiKey(key) {
90
- const secretsDir = path.dirname(this.secretsPath);
91
- if (!fs.existsSync(secretsDir)) {
92
- fs.mkdirSync(secretsDir, { recursive: true });
93
- }
94
- fs.writeFileSync(this.secretsPath, JSON.stringify({ apiKey: key }, null, 2));
95
- }
96
-
97
- /**
98
- * Run a job and get response from the agent
99
- *
100
- * @param {string|Array} message - Either:
101
- * - String: "Do something ..."
102
- * - Array: [
103
- * {type:"text", text:"Process this invoice"},
104
- * {type:"image_url", image_url:{url:"https://files.openkbs.com/invoice.png"}}
105
- * ]
106
- *
107
- * @returns {Promise<Object>} Response structure:
108
- * {
109
- * data: {
110
- * type: 'TEXT' | 'CUSTOM_TYPE',
111
- * content: '...' | data: {...}
112
- * },
113
- * chatId: 'xxx-xxx',
114
- * msgId: 'msg_xxx'
115
- * }
116
- */
117
- async runJob(message, options = {}) {
118
- const apiKey = await this.getApiKey();
119
- if (!this.settings.kbId) throw new Error('First use: "openkbs push" to create the agent');
120
-
121
- const payload = { message };
122
- Object.keys(options).forEach(key => {
123
- if (key === 'historyLimit') {
124
- payload[key] = Math.min(Math.max(1, options[key]), 100);
125
- } else if (options[key] !== undefined) {
126
- payload[key] = options[key];
127
- }
128
- });
129
-
130
- const response = await this.request(
131
- `https://${this.settings.kbId}.apps.openkbs.com/api`,
132
- payload,
133
- { Authorization: `Bearer ${apiKey}` }
134
- );
135
-
136
- if (response.chatId) this.lastChatId = response.chatId;
137
- return response;
138
- }
139
-
140
- async continueChat(message, chatId = null, options = {}) {
141
- const targetChatId = chatId || this.lastChatId;
142
- if (!targetChatId) throw new Error('No chatId provided and no previous chat to continue');
143
-
144
- return this.runJob(message, {
145
- ...options,
146
- chatId: targetChatId,
147
- includeHistory: options.includeHistory !== false
148
- });
149
- }
150
-
151
- async uploadFile(filePath, options = {}) {
152
- const apiKey = await this.getApiKey();
153
- if (!fs.existsSync(filePath)) throw new Error(`File not found: ${filePath}`);
154
-
155
- const fileContent = fs.readFileSync(filePath);
156
- const ext = path.extname(filePath);
157
- const fileName = options.fileName || `file-${Date.now()}-${Math.random().toString(36).substring(7)}${ext}`;
158
- const fileType = options.fileType || this.getMimeType(filePath);
159
-
160
- const presignedResponse = await this.request('https://kb.openkbs.com/', {
161
- apiKey,
162
- kbId: this.settings.kbId,
163
- namespace: 'files',
164
- presignedOperation: 'putObject',
165
- action: 'createPresignedURL',
166
- fileName,
167
- fileType
168
- }, { Origin: `https://${this.settings.kbId}.apps.openkbs.com` });
169
-
170
- const presignedUrl = presignedResponse.presignedUrl || presignedResponse;
171
-
172
- await this.requestRaw(presignedUrl, fileContent, {
173
- 'Content-Type': fileType,
174
- 'Content-Length': fileContent.length
175
- }, 'PUT');
176
-
177
- return {
178
- fileName,
179
- uploaded: true,
180
- url: `https://file.openkbs.com/files/${this.settings.kbId}/${fileName}`
181
- };
182
- }
183
-
184
- getMimeType(filePath) {
185
- const ext = path.extname(filePath).toLowerCase();
186
- const mimeTypes = {
187
- '.pdf': 'application/pdf',
188
- '.txt': 'text/plain',
189
- '.json': 'application/json',
190
- '.jpg': 'image/jpeg',
191
- '.jpeg': 'image/jpeg',
192
- '.png': 'image/png',
193
- '.gif': 'image/gif',
194
- '.doc': 'application/msword',
195
- '.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
196
- '.xls': 'application/vnd.ms-excel',
197
- '.xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
198
- '.csv': 'text/csv',
199
- '.html': 'text/html',
200
- '.xml': 'application/xml',
201
- '.zip': 'application/zip'
202
- };
203
- return mimeTypes[ext] || 'application/octet-stream';
204
- }
205
-
206
- // Unified HTTP request helper
207
- async request(url, payload, headers = {}, method = 'POST') {
208
- return new Promise((resolve, reject) => {
209
- const { hostname, pathname, search } = new URL(url);
210
- const req = https.request({
211
- hostname,
212
- path: pathname + (search || ''),
213
- method,
214
- headers: { 'Content-Type': 'application/json', ...headers }
215
- }, res => {
216
- let data = '';
217
- res.on('data', chunk => data += chunk);
218
- res.on('end', () => {
219
- try {
220
- const result = JSON.parse(data);
221
- if (res.statusCode === 401) {
222
- if (fs.existsSync(this.secretsPath)) fs.unlinkSync(this.secretsPath);
223
- reject(new Error('Authentication failed. Please run "node scripts/run_job.js init" to reconfigure.'));
224
- } else if (res.statusCode !== 200) {
225
- reject(new Error(`Request failed with status ${res.statusCode}: ${data}`));
226
- } else {
227
- resolve(result);
228
- }
229
- } catch (error) {
230
- reject(new Error(`Failed to parse response: ${error.message}`));
231
- }
232
- });
233
- }).on('error', reject);
234
-
235
- req.write(typeof payload === 'string' ? payload : JSON.stringify(payload));
236
- req.end();
237
- });
238
- }
239
-
240
- // Raw request for binary data
241
- async requestRaw(url, data, headers = {}, method = 'PUT') {
242
- return new Promise((resolve, reject) => {
243
- const { hostname, pathname, search } = new URL(url);
244
- const req = https.request({
245
- hostname,
246
- path: pathname + (search || ''),
247
- method,
248
- headers
249
- }, res => {
250
- if (res.statusCode === 200 || res.statusCode === 204) {
251
- resolve();
252
- } else {
253
- let responseData = '';
254
- res.on('data', chunk => responseData += chunk);
255
- res.on('end', () => reject(new Error(`Request failed with status ${res.statusCode}: ${responseData}`)));
256
- }
257
- }).on('error', reject);
258
-
259
- req.write(data);
260
- req.end();
261
- });
262
- }
263
- }
264
-
265
- module.exports = OpenKBSAgentClient;