@corva/chat-core 0.1.18 → 0.1.20

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/dist/index.d.ts CHANGED
@@ -78,7 +78,6 @@ interface AskCorvaData {
78
78
  companyId?: number;
79
79
  isTruncated?: boolean;
80
80
  assetId?: number;
81
- agentName?: string;
82
81
  note?: string;
83
82
  }
84
83
 
@@ -175,7 +174,6 @@ interface AskCorvaWirePayload {
175
174
  cId?: number;
176
175
  trunc?: boolean;
177
176
  assetId?: number;
178
- agentName?: string;
179
177
  n?: string;
180
178
  }
181
179
  /**
@@ -831,7 +829,7 @@ interface ParsedCorvaMessage {
831
829
  /**
832
830
  * Builds the wire-format content string for an AskCorva response.
833
831
  */
834
- declare function buildAskCorvaMessageContent(requestId: string, answer: string, answerRef?: string, isTruncated?: boolean, assetId?: number, agentName?: string, companyId?: number, note?: string): string;
832
+ declare function buildAskCorvaMessageContent(requestId: string, answer: string, answerRef?: string, isTruncated?: boolean, assetId?: number, companyId?: number, note?: string): string;
835
833
  /**
836
834
  * Builds the wire-format content string for a forwarded message.
837
835
  */
@@ -982,6 +980,7 @@ declare function createHttpClient(config: HttpClientConfig): {
982
980
  type HttpClient = ReturnType<typeof createHttpClient>;
983
981
 
984
982
  declare function createRevoltAPI(baseUrl: string, platform: ChatPlatform): {
983
+ getServerMembers: (serverId: string) => Promise<User[]>;
985
984
  getBaseUrl: () => string;
986
985
  seedUserCache: (users: User[]) => void;
987
986
  clearUserCache: () => void;
@@ -993,7 +992,6 @@ declare function createRevoltAPI(baseUrl: string, platform: ChatPlatform): {
993
992
  addUserToGroup: (groupId: string, userId: string) => Promise<boolean>;
994
993
  removeUserFromGroup: (groupId: string, userId: string) => Promise<boolean>;
995
994
  getGroupMembers: (groupId: string) => Promise<User[]>;
996
- getServerMembers: (serverId: string) => Promise<User[]>;
997
995
  getOrCreateDMFromUser: (userId: string) => Promise<{
998
996
  _id: string;
999
997
  }>;
@@ -1026,6 +1024,7 @@ declare function createRevoltAPI(baseUrl: string, platform: ChatPlatform): {
1026
1024
  getUser: (userId: string) => Promise<User | null>;
1027
1025
  getFriends: () => Promise<User[]>;
1028
1026
  searchUsers: (_query: string) => Promise<User[]>;
1027
+ trackDirectoryLoad: <T extends User[]>(load: Promise<T>) => Promise<T>;
1029
1028
  login: (credentials: LoginCredentials) => Promise<{
1030
1029
  token: string;
1031
1030
  me: User;
package/dist/index.js CHANGED
@@ -517,7 +517,6 @@ function mapAskCorvaPayload(d) {
517
517
  companyId: d.cId,
518
518
  isTruncated: d.trunc,
519
519
  assetId: d.assetId,
520
- agentName: d.agentName,
521
520
  note: d.n || void 0
522
521
  };
523
522
  }
@@ -549,7 +548,7 @@ function mapAppCommentPayload(d) {
549
548
  isTruncated: d.trunc
550
549
  };
551
550
  }
552
- function buildAskCorvaMessageContent(requestId, answer, answerRef, isTruncated, assetId, agentName, companyId, note) {
551
+ function buildAskCorvaMessageContent(requestId, answer, answerRef, isTruncated, assetId, companyId, note) {
553
552
  const payload = {
554
553
  id: requestId,
555
554
  a: answerRef ? "" : answer,
@@ -557,7 +556,6 @@ function buildAskCorvaMessageContent(requestId, answer, answerRef, isTruncated,
557
556
  ...answerRef && companyId && { cId: companyId },
558
557
  ...isTruncated && { trunc: true },
559
558
  ...assetId && { assetId },
560
- ...agentName && { agentName },
561
559
  ...note?.trim() && { n: note.trim() }
562
560
  };
563
561
  return `${CORVA_SYS_PREFIX}${JSON.stringify({ t: CORVA_MESSAGE_TYPES.ASK_CORVA, v: 1, d: payload })}`;
@@ -4156,6 +4154,17 @@ function createAuthAPI(http, tokenStore) {
4156
4154
  // src/api/modules/users.ts
4157
4155
  var userCache = {};
4158
4156
  var pendingRequests = {};
4157
+ var DIRECTORY_WAIT_MS = 2e3;
4158
+ var pendingDirectoryLoads = /* @__PURE__ */ new Set();
4159
+ async function waitForDirectoryLoads() {
4160
+ if (pendingDirectoryLoads.size === 0) return;
4161
+ let timer;
4162
+ const timeout = new Promise((resolve) => {
4163
+ timer = setTimeout(resolve, DIRECTORY_WAIT_MS);
4164
+ });
4165
+ await Promise.race([Promise.allSettled([...pendingDirectoryLoads]), timeout]);
4166
+ clearTimeout(timer);
4167
+ }
4159
4168
  function createUsersAPI(http) {
4160
4169
  async function getUser(userId) {
4161
4170
  if (userCache[userId]) {
@@ -4166,6 +4175,8 @@ function createUsersAPI(http) {
4166
4175
  }
4167
4176
  const request = (async () => {
4168
4177
  try {
4178
+ await waitForDirectoryLoads();
4179
+ if (userCache[userId]) return userCache[userId];
4169
4180
  const user = await http.request(`/users/${userId}`);
4170
4181
  const result = {
4171
4182
  _id: user._id,
@@ -4193,9 +4204,23 @@ function createUsersAPI(http) {
4193
4204
  }
4194
4205
  });
4195
4206
  }
4207
+ function trackDirectoryLoad(load) {
4208
+ const seeded = load.then(async (users) => {
4209
+ await new Promise((resolve) => {
4210
+ setTimeout(resolve, 0);
4211
+ });
4212
+ if (pendingDirectoryLoads.has(seeded)) seedUserCache(users);
4213
+ }).catch(() => {
4214
+ }).finally(() => {
4215
+ pendingDirectoryLoads.delete(seeded);
4216
+ });
4217
+ pendingDirectoryLoads.add(seeded);
4218
+ return load;
4219
+ }
4196
4220
  function clearUserCache() {
4197
4221
  Object.keys(userCache).forEach((key) => delete userCache[key]);
4198
4222
  Object.keys(pendingRequests).forEach((key) => delete pendingRequests[key]);
4223
+ pendingDirectoryLoads.clear();
4199
4224
  }
4200
4225
  async function getFriends() {
4201
4226
  try {
@@ -4249,6 +4274,7 @@ function createUsersAPI(http) {
4249
4274
  getFriends,
4250
4275
  searchUsers: searchUsers2,
4251
4276
  seedUserCache,
4277
+ trackDirectoryLoad,
4252
4278
  clearUserCache
4253
4279
  };
4254
4280
  }
@@ -4747,9 +4773,23 @@ function createAttachmentsAPI(autumnUrl, getToken, platform) {
4747
4773
  }
4748
4774
 
4749
4775
  // src/api/modules/directMessages.ts
4776
+ var isMissingSendPermission = (error) => {
4777
+ if (!(error instanceof HttpError) || error.status !== 403) return false;
4778
+ const body = error.body;
4779
+ return body?.type === "MissingPermission" && body.permission === "SendMessage";
4780
+ };
4750
4781
  function createDirectMessagesAPI(http, getUserFn, getMessagesFn) {
4751
4782
  async function getOrCreateDMFromUser(userId) {
4752
- return http.request(`/users/${userId}/dm`);
4783
+ const openDm = () => http.request(`/users/${userId}/dm`);
4784
+ try {
4785
+ return await openDm();
4786
+ } catch (error) {
4787
+ if (!isMissingSendPermission(error)) throw error;
4788
+ await getChatHost().corvaApi.post("/v2/integration/revolt_chat_friendships", {
4789
+ revolt_user_id: userId
4790
+ });
4791
+ return openDm();
4792
+ }
4753
4793
  }
4754
4794
  async function getDMRecipient(dmChannelId, currentUserId, cachedChannel) {
4755
4795
  try {
@@ -4891,6 +4931,7 @@ function createRevoltAPI(baseUrl, platform) {
4891
4931
  ...attachments,
4892
4932
  ...directMessages,
4893
4933
  ...groups,
4934
+ getServerMembers: (serverId) => users.trackDirectoryLoad(groups.getServerMembers(serverId)),
4894
4935
  getBaseUrl: () => normalizedBaseUrl,
4895
4936
  seedUserCache: users.seedUserCache,
4896
4937
  clearUserCache: users.clearUserCache,