openkbs 0.0.23 → 0.0.25

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/Docs.md CHANGED
@@ -475,7 +475,7 @@ Here's a breakdown of the key objects and utilities available within the OpenKBS
475
475
 
476
476
  * **`openkbs`:** The OpenKBS SDK, documented previously, provides utility functions for interacting with the OpenKBS platform and various external services.
477
477
 
478
- * **`AWS: AWS_SDK`:** The AWS SDK provides access to a wide range of AWS services directly within your event handlers. This allows integration with S3, DynamoDB, Lambda, and other AWS resources. Pre-configured and ready to use.
478
+ * **`AWS: AWS_SDK`:** The AWS SDK provides access to a wide range of AWS services
479
479
 
480
480
  * **`axios`:** Powerful HTTP client for making requests to external APIs and services. Simplifies handling responses and errors compared to the built-in `https` module.
481
481
 
package/README.md CHANGED
@@ -1,11 +1,7 @@
1
1
  # OpenKBS · [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://github.com/open-kbs/openkbs-chat/blob/main/LICENSE) [![npm version](https://img.shields.io/badge/npm-v0.0.20-orange.svg)](https://www.npmjs.com/package/openkbs)
2
2
 
3
- OpenKBS is an extendable open-source platform designed to build,
4
- deploy and integrate AI agents anywhere, from websites to IoT devices.
5
- Its event-driven architecture enables full customization of backend and
6
- frontend components, while the LLM abstraction layer allows seamless
7
- switching between language models. With its powerful CLI, OpenKBS turns
8
- complex tasks into simple prompt commands, letting developers focus on what matters.
3
+ OpenKBS is an extendable AI service designed to build,
4
+ deploy and integrate AI agents and applications.
9
5
 
10
6
  ## Table of Contents
11
7
 
@@ -34,6 +30,16 @@ complex tasks into simple prompt commands, letting developers focus on what matt
34
30
  - [Frontend Module Loading](#frontend-module-loading)
35
31
  - [Built-in UI Libraries](#built-in-ui-libraries)
36
32
  - [Common Frontend Components and Utilities](#common-frontend-components-and-utilities)
33
+ - [API](#api)
34
+ - [Keys and Authentication](#keys-and-authentication)
35
+ - [Encryption and Decryption](#encryption-and-decryption)
36
+ - [API Endpoints](#api-endpoints)
37
+ - [Agent newChat](#newChat)
38
+ - [Agent getChatMessages](#getChatMessages)
39
+ - [Agent chatAddMessages](#chatAddMessages)
40
+ - [Ledger signTransaction](#signTransaction)
41
+ - [Ledger accountBalances](#account-balances)
42
+ - [Ledger transactions](#transactions)
37
43
  - [License](#license)
38
44
  - [Contributing](#contributing)
39
45
  - [Contact](#contact)
@@ -979,6 +985,225 @@ const onRenderChatMessage = async (params) => {
979
985
  };
980
986
  ```
981
987
 
988
+ ## API
989
+
990
+ OpenKBS provides APIs to interact programmatically with your application. These APIs allow you to perform actions like starting new chats, retrieving chat messages, and managing data within your application. Data exchanged with these APIs is encrypted and decrypted using AES-256 encryption.
991
+
992
+ ### Keys and Authentication
993
+
994
+ - **kbId**: Agent ID from OpenKBS app > Access > kbId.
995
+ - **apiKey**: API key from OpenKBS app > Access > API Keys.
996
+ - **AESKey**: Encryption key from OpenKBS app > Access > AES Key.
997
+
998
+ ### Encryption and Decryption
999
+
1000
+ OpenKBS utilizes AES-256 encryption to secure sensitive data. The encryption process involves generating a salt, deriving a key and initialization vector (IV) from a passphrase using a key derivation function (KDF), and then encrypting the data using AES-256 in CBC mode. The encrypted data is then prepended with the salt and a marker, and finally base64 encoded. Decryption reverses this process.
1001
+
1002
+ **Example in JavaScript (using CryptoJS):**
1003
+
1004
+ ```javascript
1005
+ import CryptoJS from 'crypto-js';
1006
+
1007
+ export const encrypt = (text, AESKey) => {
1008
+ return CryptoJS.AES.encrypt(text, AESKey).toString();
1009
+ };
1010
+
1011
+ export const decrypt = (ciphertext, AESKey) => {
1012
+ const bytes = CryptoJS.AES.decrypt(ciphertext, AESKey);
1013
+ return bytes.toString(CryptoJS.enc.Utf8);
1014
+ };
1015
+ ```
1016
+
1017
+ ### API Endpoints
1018
+
1019
+ #### newChat
1020
+
1021
+ This endpoint initiates a new task (chat session) with a specific title and initial message.
1022
+
1023
+ - **Endpoint:** `https://chat.openkbs.com/`
1024
+ - **Method:** `POST`
1025
+ - **Request Body (JSON):**
1026
+
1027
+ ```json
1028
+ {
1029
+ "kbId": "YOUR_KB_ID",
1030
+ "apiKey": "YOUR_API_KEY",
1031
+ "chatTitle": "Chat Title",
1032
+ "encrypted": true,
1033
+ "message": "Initial task content (must be encrypted)"
1034
+ }
1035
+ ```
1036
+
1037
+ **Response (JSON):**
1038
+
1039
+ ```json
1040
+ [
1041
+ {
1042
+ "createdChatId": "1737908556342-603701"
1043
+ },
1044
+ { "id":1, "content": "other content"}
1045
+ ]
1046
+ ```
1047
+
1048
+
1049
+ #### getChatMessages
1050
+
1051
+ This endpoint retrieves messages from a specified chat.
1052
+
1053
+ - **Endpoint:** `https://chat.openkbs.com/`
1054
+ - **Method:** `POST`
1055
+ - **Request Body (JSON):**
1056
+
1057
+ ```json
1058
+ {
1059
+ "action": "getChatMessages",
1060
+ "kbId": "YOUR_KB_ID",
1061
+ "apiKey": "YOUR_API_KEY",
1062
+ "chatId": "createdChatId"
1063
+ }
1064
+ ```
1065
+
1066
+ **Response (JSON):**
1067
+
1068
+ ```json
1069
+ [
1070
+ {
1071
+ "data": {
1072
+ "messages": [
1073
+ {
1074
+ "content": "Message content (encrypted)",
1075
+ "role": "sender_role (e.g., 'user', 'assistant')",
1076
+ "msgId": "message_id"
1077
+ },
1078
+ // ... other messages
1079
+ ]
1080
+ }
1081
+ }
1082
+ ]
1083
+ ```
1084
+
1085
+ #### signTransaction
1086
+
1087
+ This endpoint is used to sign a JWT, which is necessary for accessing the ledger endpoint to retrieve transactions and account balances.
1088
+
1089
+ - **Endpoint:** `https://kb.openkbs.com/`
1090
+ - **Method:** `POST`
1091
+ - **Request Body (JSON):**
1092
+
1093
+ ```json
1094
+ {
1095
+ "kbId": "YOUR_KB_ID",
1096
+ "action": "signTransaction",
1097
+ "apiKey": "YOUR_API_KEY"
1098
+ }
1099
+ ```
1100
+
1101
+ - **Response:**
1102
+
1103
+ The response contains a signed JWT used for authenticating requests to the ledger and account balance endpoints.
1104
+
1105
+
1106
+ ```json
1107
+ {
1108
+ "transactionJWT": "xxx"
1109
+ }
1110
+ ```
1111
+
1112
+ #### accountBalances
1113
+
1114
+ This endpoint retrieves the account balance for a specific resource, such as credits.
1115
+
1116
+ - **Endpoint:** `https://ledger.openkbs.com/account-balances`
1117
+ - **Method:** `POST`
1118
+ - **Request Body (JSON):**
1119
+
1120
+ ```json
1121
+ {
1122
+ "apiKey": "YOUR_API_KEY",
1123
+ "transactionJWT": "SIGNED_JWT_FROM_SIGN_TRANSACTION"
1124
+ }
1125
+ ```
1126
+
1127
+ - **Response:**
1128
+
1129
+ The response is a JSON object containing account balance details:
1130
+
1131
+ ```json
1132
+ {
1133
+ "balance": 1000000, // 1000 credits === 1 cent
1134
+ "accountId": "0000da3b669a3426019ffc8ddae93c2e",
1135
+ "resourceId": "credits",
1136
+ "lastTransactionSubject": "kbId",
1137
+ "userId": "xxx646594380230f",
1138
+ "accountName": "User Account"
1139
+ }
1140
+ ```
1141
+
1142
+ #### transactions
1143
+
1144
+ This endpoint retrieves a list of transactions associated with the specified agent.
1145
+
1146
+ - **Endpoint:** `https://ledger.openkbs.com/transactions`
1147
+ - **Method:** `POST`
1148
+ - **Request Body (JSON):**
1149
+
1150
+ ```json
1151
+ {
1152
+ "lastEvaluatedKey": null,
1153
+ "limit": 25, // or any specified limit
1154
+ "apiKey": "YOUR_API_KEY",
1155
+ "transactionJWT": "SIGNED_JWT_FROM_SIGN_TRANSACTION"
1156
+ }
1157
+ ```
1158
+
1159
+ - **Response:**
1160
+
1161
+ The response is a JSON object containing a `transactions` array. Each transaction object includes:
1162
+
1163
+ ```json
1164
+ {
1165
+ "transactions": [
1166
+ {
1167
+ "subject": "kbId",
1168
+ "accountId": "0000da3b669a3426019ffc8ddae93c2e",
1169
+ "resourceId": "credits",
1170
+ "subjectId": "xxxx2sxeobc9",
1171
+ "amount": -6024,
1172
+ "message": "Input: 11816 tokens, Output: 649 tokens",
1173
+ "remoteAccount": "xxx1b21a831389b59bbc263face0e41d",
1174
+ "transactionId": "1737909703561-305239"
1175
+ }
1176
+ // ... other transactions
1177
+ ]
1178
+ }
1179
+ ```
1180
+
1181
+ #### chatAddMessages
1182
+
1183
+ This endpoint allows adding messages to a specified chat, which is useful for integrating with external systems or logging events. Combined with the `onAddMessages` event handler, these added messages can trigger actions within your OpenKBS application.
1184
+
1185
+ - **Endpoint:** `https://chat.openkbs.com/`
1186
+ - **Method:** `POST`
1187
+ - **Request Body (JSON):**
1188
+
1189
+ ```json
1190
+ {
1191
+ "action": "chatAddMessages",
1192
+ "chatId": "Chat ID",
1193
+ "messages": [
1194
+ {
1195
+ "role": "Sender role (e.g., 'user', 'assistant', 'system')",
1196
+ "content": "Message content",
1197
+ "msgId": "Unique message ID (recommended to include a timestamp)"
1198
+ },
1199
+ // ... more messages can be added to this array
1200
+ ],
1201
+ "apiKey": "YOUR_API_KEY", // Required for authentication
1202
+ "kbId": "YOUR_KB_ID"
1203
+ }
1204
+ ```
1205
+
1206
+
982
1207
 
983
1208
  ## License
984
1209
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openkbs",
3
- "version": "0.0.23",
3
+ "version": "0.0.25",
4
4
  "description": "OpenKBS - Command Line Interface",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/actions.js CHANGED
@@ -126,7 +126,7 @@ async function pullAction(location = 'origin', targetFile) {
126
126
  }
127
127
  }
128
128
  } catch (error) {
129
- console.red('Error during pull operation:', error.message);
129
+ console.error('Error during pull operation:', error.message);
130
130
  }
131
131
  }
132
132
 
@@ -241,7 +241,7 @@ async function pushAction(location = 'origin', targetFile, options) {
241
241
  }
242
242
  }
243
243
  } catch (error) {
244
- console.red('Error during push operation:', error.message);
244
+ console.error('Error during push operation:', error.message);
245
245
  }
246
246
  }
247
247
 
@@ -263,7 +263,7 @@ async function cloneAction(kbId) {
263
263
  await downloadFiles(['functions', 'frontend'], kbId, kbToken);
264
264
  console.green('Cloning complete!');
265
265
  } catch (error) {
266
- console.red('Error during clone operation:', error.message);
266
+ console.error('Error during clone operation:', error.message);
267
267
  }
268
268
  }
269
269
 
@@ -280,7 +280,7 @@ async function createByTemplateAction(name) {
280
280
 
281
281
  console.log(`Application ${name} created successfully.`);
282
282
  } catch (error) {
283
- console.red(`Error during create operation:`, error.message);
283
+ console.error(`Error during create operation:`, error.message);
284
284
  }
285
285
  }
286
286
 
package/src/utils.js CHANGED
@@ -506,14 +506,14 @@ async function signPayload(payload, accountId, publicKey, privateKey, expiresInS
506
506
  }
507
507
 
508
508
  const buildPackage = async (namespace, kbId, moduleName) => {
509
- const token = await getClientJWT();
509
+ const { kbToken } = await fetchKBJWT(kbId);
510
510
 
511
511
  return await makePostRequest('https://kb.openkbs.com/', {
512
- token,
512
+ token: kbToken,
513
513
  kbId,
514
514
  namespace,
515
515
  moduleName,
516
- action: namespace === 'frontend' ? 'buildWebpackPackage' : 'buildNodePackage'
516
+ action: namespace === 'frontend' ? 'buildWebpackPackageWithKBToken' : 'buildNodePackageWithKBToken'
517
517
  });
518
518
  }
519
519
 
@@ -1048,7 +1048,7 @@ async function initByTemplateAction(params) {
1048
1048
  }
1049
1049
  });
1050
1050
  } catch (error) {
1051
- console.red(`Error during create operation:`, error.message);
1051
+ console.error(`Error during create operation:`, error.message);
1052
1052
  }
1053
1053
  }
1054
1054