bkper-js 1.33.1 → 1.34.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 CHANGED
@@ -1477,16 +1477,18 @@ export declare class Conversation {
1477
1477
  */
1478
1478
  getUpdatedAt(): Date | undefined;
1479
1479
  /**
1480
+ * Gets the Messages that compose this Conversation
1480
1481
  *
1481
1482
  * @returns The Messages in this Conversation
1482
1483
  */
1483
1484
  getMessages(): Promise<Message[]>;
1485
+
1484
1486
  /**
1485
- * @param message The Message to send to the Conversation
1487
+ * Performs create Conversation
1486
1488
  *
1487
- * @returns The updated Conversation object
1489
+ * @returns The created Conversation object
1488
1490
  */
1489
- send(message: Message): Promise<Conversation>;
1491
+ create(): Promise<Conversation>;
1490
1492
  }
1491
1493
 
1492
1494
  /**
@@ -2005,6 +2007,7 @@ export declare class Integration {
2005
2007
  export declare class Message {
2006
2008
  payload: bkper.Message;
2007
2009
 
2010
+
2008
2011
  constructor(conversation: Conversation, payload?: bkper.Message);
2009
2012
  /**
2010
2013
  * @returns The wrapped plain json object
@@ -2017,12 +2020,12 @@ export declare class Message {
2017
2020
  getId(): string | undefined;
2018
2021
  /**
2019
2022
  *
2020
- * @returns The Agent who created the Message, if any
2023
+ * @returns The Agent associated with the Message, in any
2021
2024
  */
2022
2025
  getAgent(): Agent | undefined;
2023
2026
  /**
2024
2027
  *
2025
- * @returns The User who created the Message, if any
2028
+ * @returns The User associated with the Message
2026
2029
  */
2027
2030
  getUser(): User | undefined;
2028
2031
  /**
@@ -2083,6 +2086,12 @@ export declare class Message {
2083
2086
  * @returns This Message, for chainning.
2084
2087
  */
2085
2088
  deleteProperty(key: string): Message;
2089
+ /**
2090
+ * Creates the Message and receives the synchronous Agent response.
2091
+ *
2092
+ * @returns The Agent response Message, with the created Message as its parent
2093
+ */
2094
+ create(): Promise<Message>;
2086
2095
  }
2087
2096
 
2088
2097
  /**
@@ -10,6 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  import { Book } from "./Book.js";
11
11
  import { App } from "./App.js";
12
12
  import * as AppService from '../service/app-service.js';
13
+ import * as ConversationService from '../service/conversation-service.js';
13
14
  import * as BookService from '../service/book-service.js';
14
15
  import * as CollectionService from '../service/collection-service.js';
15
16
  import * as UserService from '../service/user-service.js';
@@ -93,7 +94,7 @@ export class Bkper {
93
94
  */
94
95
  static getConversations() {
95
96
  return __awaiter(this, void 0, void 0, function* () {
96
- const conversationPayloads = yield AppService.getConversations();
97
+ const conversationPayloads = yield ConversationService.getConversations();
97
98
  let conversations = [];
98
99
  for (const payload of conversationPayloads) {
99
100
  const agent = new Agent(payload.agent);
@@ -64,54 +64,54 @@ export class Conversation {
64
64
  return this.payload.updatedAt ? new Date(this.payload.updatedAt) : undefined;
65
65
  }
66
66
  /**
67
+ * Gets the Messages that compose this Conversation
67
68
  *
68
69
  * @returns The Messages in this Conversation
69
70
  */
70
71
  getMessages() {
71
72
  return __awaiter(this, void 0, void 0, function* () {
72
- if (this.messages != null) {
73
- return this.messages;
73
+ if (this.messagesMap != null) {
74
+ return Array.from(this.messagesMap.values());
74
75
  }
75
76
  const conversationId = this.getId();
76
- if (conversationId) {
77
- const messagePayloads = yield ConversationService.getMessages(conversationId);
78
- this.payload.messages = messagePayloads;
79
- this.messages = messagePayloads.map(message => new Message(this, message));
77
+ if (!conversationId) {
78
+ throw new Error('Conversation id null!');
80
79
  }
81
- return this.messages || [];
80
+ if (!this.messagesMap) {
81
+ this.messagesMap = new Map();
82
+ }
83
+ const messagePayloads = yield ConversationService.getMessages(conversationId);
84
+ for (const payload of messagePayloads) {
85
+ this.updateMessagesCache(new Message(this, payload));
86
+ }
87
+ return Array.from(this.messagesMap.values());
82
88
  });
83
89
  }
90
+ /** @internal */
91
+ updateMessagesCache(message) {
92
+ const messageId = message.getId();
93
+ if (messageId) {
94
+ if (!this.messagesMap) {
95
+ this.messagesMap = new Map();
96
+ }
97
+ this.messagesMap.set(messageId, message);
98
+ }
99
+ }
84
100
  /**
85
- * @param message The Message to send to the Conversation
101
+ * Performs create Conversation
86
102
  *
87
- * @returns The updated Conversation object
103
+ * @returns The created Conversation object
88
104
  */
89
- send(message) {
105
+ create() {
90
106
  return __awaiter(this, void 0, void 0, function* () {
91
107
  const agentId = this.agent.getId();
92
108
  if (!agentId) {
93
109
  throw new Error('Agent id null!');
94
110
  }
95
- // Add message to payload
96
- if (!this.payload.messages) {
97
- this.payload.messages = [];
98
- }
99
- this.payload.messages.push(message.json());
100
- // Send conversation
101
- const updatedPayload = yield ConversationService.send(agentId, this.payload);
102
- this.payload = updatedPayload;
103
- // Update agent and messages
104
- if (updatedPayload.agent) {
105
- this.agent = new Agent(updatedPayload.agent);
106
- }
107
- if (updatedPayload.messages) {
108
- if (!this.messages) {
109
- this.messages = [];
110
- }
111
- for (const messagePayload of updatedPayload.messages) {
112
- this.messages.push(new Message(this, messagePayload));
113
- }
114
- this.payload.messages = this.messages.map(m => m.json());
111
+ this.payload = yield ConversationService.createConversation(agentId, this.payload);
112
+ // Update agent
113
+ if (this.payload.agent) {
114
+ this.agent = new Agent(this.payload.agent);
115
115
  }
116
116
  return this;
117
117
  });
@@ -1,3 +1,13 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import * as ConversationService from "../service/conversation-service.js";
1
11
  import { Agent } from "./Agent.js";
2
12
  import { User } from "./User.js";
3
13
  /**
@@ -27,14 +37,14 @@ export class Message {
27
37
  }
28
38
  /**
29
39
  *
30
- * @returns The Agent who created the Message, if any
40
+ * @returns The Agent associated with the Message, in any
31
41
  */
32
42
  getAgent() {
33
43
  return this.payload.agent ? new Agent(this.payload.agent) : undefined;
34
44
  }
35
45
  /**
36
46
  *
37
- * @returns The User who created the Message, if any
47
+ * @returns The User associated with the Message
38
48
  */
39
49
  getUser() {
40
50
  return this.payload.user ? new User(this.payload.user) : undefined;
@@ -129,5 +139,26 @@ export class Message {
129
139
  this.setProperty(key, null);
130
140
  return this;
131
141
  }
142
+ /**
143
+ * Creates the Message and receives the synchronous Agent response.
144
+ *
145
+ * @returns The Agent response Message, with the created Message as its parent
146
+ */
147
+ create() {
148
+ return __awaiter(this, void 0, void 0, function* () {
149
+ const conversationId = this.conversation.getId();
150
+ if (!conversationId) {
151
+ throw new Error('Conversation id null!');
152
+ }
153
+ const responsePayload = yield ConversationService.createMessage(conversationId, this.payload);
154
+ if (responsePayload.parent) {
155
+ this.payload = responsePayload.parent;
156
+ }
157
+ const responseMessage = new Message(this.conversation, responsePayload);
158
+ this.conversation.updateMessagesCache(this);
159
+ this.conversation.updateMessagesCache(responseMessage);
160
+ return this;
161
+ });
162
+ }
132
163
  }
133
164
  //# sourceMappingURL=Message.js.map
@@ -22,13 +22,6 @@ export function getApps() {
22
22
  return appsJson;
23
23
  });
24
24
  }
25
- export function getConversations() {
26
- return __awaiter(this, void 0, void 0, function* () {
27
- var _a;
28
- const response = yield new HttpApiRequest(`v5/apps/conversations`).setMethod('GET').fetch();
29
- return ((_a = response.data) === null || _a === void 0 ? void 0 : _a.items) || [];
30
- });
31
- }
32
25
  export function createApp(app) {
33
26
  return __awaiter(this, void 0, void 0, function* () {
34
27
  var response = yield new HttpApiRequest(`v5/apps`).setMethod('POST').setPayload(app).fetch();
@@ -8,6 +8,19 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { HttpApiRequest } from "./http-api-request.js";
11
+ export function getConversations() {
12
+ return __awaiter(this, void 0, void 0, function* () {
13
+ var _a;
14
+ const response = yield new HttpApiRequest(`v5/apps/conversations`).setMethod('GET').fetch();
15
+ return ((_a = response.data) === null || _a === void 0 ? void 0 : _a.items) || [];
16
+ });
17
+ }
18
+ export function createConversation(agentId, conversation) {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ const response = yield new HttpApiRequest(`v5/apps/${agentId}/conversations`).setMethod('POST').setPayload(conversation).fetch();
21
+ return response.data;
22
+ });
23
+ }
11
24
  export function getMessages(conversationId) {
12
25
  return __awaiter(this, void 0, void 0, function* () {
13
26
  var _a;
@@ -15,9 +28,9 @@ export function getMessages(conversationId) {
15
28
  return ((_a = response.data) === null || _a === void 0 ? void 0 : _a.items) || [];
16
29
  });
17
30
  }
18
- export function send(agentId, conversation) {
31
+ export function createMessage(conversationId, message) {
19
32
  return __awaiter(this, void 0, void 0, function* () {
20
- const response = yield new HttpApiRequest(`v5/apps/${agentId}/conversations`).setMethod('POST').setPayload(conversation).fetch();
33
+ const response = yield new HttpApiRequest(`v5/apps/conversations/${conversationId}/messages`).setMethod('POST').setPayload(message).fetch();
21
34
  return response.data;
22
35
  });
23
36
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bkper-js",
3
- "version": "1.33.1",
3
+ "version": "1.34.1",
4
4
  "description": "Javascript client for Bkper REST API",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib/index.js",
@@ -34,7 +34,7 @@
34
34
  "postversion": "git push --tags && yarn publish --new-version $npm_package_version && git push && echo \"Successfully released version $npm_package_version!\""
35
35
  },
36
36
  "peerDependencies": {
37
- "@bkper/bkper-api-types": "^5.16.0"
37
+ "@bkper/bkper-api-types": "^5.17.0"
38
38
  },
39
39
  "dependencies": {
40
40
  "@google-cloud/local-auth": "^3.0.1",