@credal/actions 0.2.110 → 0.2.111

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.
@@ -9,6 +9,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  import { WebClient } from "@slack/web-api";
11
11
  import { MISSING_AUTH_TOKEN } from "../../util/missingAuthConstants.js";
12
+ import pLimit from "p-limit";
13
+ const HIT_ENRICH_POOL = 10;
14
+ const limitHit = pLimit(HIT_ENRICH_POOL);
12
15
  /* ===================== Helpers ===================== */
13
16
  function normalizeChannelOperand(ch) {
14
17
  const s = ch.trim();
@@ -32,17 +35,22 @@ function timeFilter(range) {
32
35
  }
33
36
  function lookupUserIdsByEmail(client, emails) {
34
37
  return __awaiter(this, void 0, void 0, function* () {
35
- var _a;
36
38
  const ids = [];
37
- for (const raw of emails) {
39
+ const tasks = emails.map((raw) => __awaiter(this, void 0, void 0, function* () {
40
+ var _a;
38
41
  const email = raw.trim();
39
42
  if (!email)
40
- continue;
43
+ return null;
41
44
  const res = yield client.users.lookupByEmail({ email });
42
45
  const id = (_a = res.user) === null || _a === void 0 ? void 0 : _a.id;
43
46
  if (id)
44
- ids.push(id);
45
- }
47
+ return id;
48
+ return null;
49
+ }));
50
+ const settled = yield Promise.allSettled(tasks);
51
+ for (const r of settled)
52
+ if (r.status === "fulfilled" && r.value)
53
+ ids.push(r.value);
46
54
  return ids;
47
55
  });
48
56
  }
@@ -119,7 +127,7 @@ function fetchContextWindow(client, channel, ts) {
119
127
  }
120
128
  /* ===================== Main Export ===================== */
121
129
  const searchSlack = (_a) => __awaiter(void 0, [_a], void 0, function* ({ params, authParams, }) {
122
- var _b, _c, _d, _e, _f, _g;
130
+ var _b, _c;
123
131
  if (!authParams.authToken) {
124
132
  throw new Error(MISSING_AUTH_TOKEN);
125
133
  }
@@ -128,13 +136,17 @@ const searchSlack = (_a) => __awaiter(void 0, [_a], void 0, function* ({ params,
128
136
  const parts = [];
129
137
  if (emails === null || emails === void 0 ? void 0 : emails.length) {
130
138
  const userIds = yield lookupUserIdsByEmail(client, emails);
131
- if (userIds.length === 0)
139
+ const { user_id: myUserId } = yield client.auth.test();
140
+ if (!myUserId)
141
+ throw new Error("Failed to get my user ID.");
142
+ const userIdsWithoutMe = userIds.filter(id => id !== myUserId);
143
+ if (userIdsWithoutMe.length === 0)
132
144
  throw new Error("No users resolved from emails.");
133
- if (userIds.length == 1) {
134
- parts.push(`in:<@${userIds[0]}>`);
145
+ if (userIdsWithoutMe.length == 1) {
146
+ parts.push(`in:<@${userIdsWithoutMe[0]}>`);
135
147
  }
136
148
  else {
137
- const convoName = yield getMPIMName(client, userIds);
149
+ const convoName = yield getMPIMName(client, userIdsWithoutMe);
138
150
  parts.push(`in:${convoName}`);
139
151
  }
140
152
  }
@@ -161,58 +173,62 @@ const searchSlack = (_a) => __awaiter(void 0, [_a], void 0, function* ({ params,
161
173
  userId: m.user,
162
174
  });
163
175
  });
164
- const results = [];
165
- for (const h of hits) {
176
+ const tasks = hits.map(h => limitHit(() => __awaiter(void 0, void 0, void 0, function* () {
177
+ var _a, _b, _c, _d;
166
178
  if (!h.ts)
167
- continue;
179
+ return null;
168
180
  try {
169
181
  const anchor = yield fetchOneMessage(client, h.channelId, h.ts);
170
182
  const rootTs = (anchor === null || anchor === void 0 ? void 0 : anchor.thread_ts) || h.ts;
171
183
  if (anchor === null || anchor === void 0 ? void 0 : anchor.thread_ts) {
172
- const thread = yield fetchThread(client, h.channelId, rootTs);
173
- const normalizedThreads = [];
174
- for (const t of thread) {
175
- if (!t.ts)
176
- continue;
177
- normalizedThreads.push({ ts: t.ts, text: t.text, userId: t.user });
178
- }
179
- results.push({
184
+ // thread: fetch thread + permalink concurrently
185
+ const [thread, permalink] = yield Promise.all([
186
+ fetchThread(client, h.channelId, rootTs),
187
+ getPermalink(client, h.channelId, rootTs),
188
+ ]);
189
+ const context = thread.filter(t => t.ts).map(t => ({ ts: t.ts, text: t.text, userId: t.user }));
190
+ return {
180
191
  channelId: h.channelId,
181
192
  ts: rootTs,
182
- text: (_d = anchor.text) !== null && _d !== void 0 ? _d : h.text,
183
- userId: (_e = anchor.user) !== null && _e !== void 0 ? _e : h.userId,
184
- context: normalizedThreads,
185
- permalink: yield getPermalink(client, h.channelId, rootTs),
186
- });
193
+ text: (_a = anchor.text) !== null && _a !== void 0 ? _a : h.text,
194
+ userId: (_b = anchor.user) !== null && _b !== void 0 ? _b : h.userId,
195
+ context,
196
+ permalink,
197
+ };
187
198
  }
188
199
  else {
189
- const ctx = yield fetchContextWindow(client, h.channelId, h.ts);
190
- const normalizedThreads = [];
191
- for (const t of ctx) {
192
- if (!t.ts)
193
- continue;
194
- normalizedThreads.push({ ts: t.ts, text: t.text, userId: t.user });
195
- }
196
- results.push({
200
+ // not a thread: fetch context window + permalink concurrently
201
+ const [ctx, permalink] = yield Promise.all([
202
+ fetchContextWindow(client, h.channelId, h.ts),
203
+ getPermalink(client, h.channelId, h.ts),
204
+ ]);
205
+ const context = ctx.filter(t => t.ts).map(t => ({ ts: t.ts, text: t.text, userId: t.user }));
206
+ return {
197
207
  channelId: h.channelId,
198
208
  ts: h.ts,
199
- text: (_f = anchor === null || anchor === void 0 ? void 0 : anchor.text) !== null && _f !== void 0 ? _f : h.text,
200
- userId: (_g = anchor === null || anchor === void 0 ? void 0 : anchor.user) !== null && _g !== void 0 ? _g : h.userId,
201
- context: normalizedThreads,
202
- permalink: yield getPermalink(client, h.channelId, h.ts),
203
- });
209
+ text: (_c = anchor === null || anchor === void 0 ? void 0 : anchor.text) !== null && _c !== void 0 ? _c : h.text,
210
+ userId: (_d = anchor === null || anchor === void 0 ? void 0 : anchor.user) !== null && _d !== void 0 ? _d : h.userId,
211
+ context,
212
+ permalink,
213
+ };
204
214
  }
205
215
  }
206
- catch (_h) {
207
- results.push({
216
+ catch (_e) {
217
+ // fallback minimal object; still in parallel
218
+ return {
208
219
  channelId: h.channelId,
209
220
  ts: h.ts,
210
221
  text: h.text,
211
222
  userId: h.userId,
212
223
  permalink: yield getPermalink(client, h.channelId, h.ts),
213
- });
224
+ };
214
225
  }
215
- }
226
+ })));
227
+ const settled = yield Promise.allSettled(tasks);
228
+ const results = [];
229
+ for (const r of settled)
230
+ if (r.status === "fulfilled" && r.value)
231
+ results.push(r.value);
216
232
  results.sort((a, b) => Number(b.ts) - Number(a.ts));
217
233
  return { query, results };
218
234
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@credal/actions",
3
- "version": "0.2.110",
3
+ "version": "0.2.111",
4
4
  "type": "module",
5
5
  "description": "AI Actions by Credal AI",
6
6
  "sideEffects": false,
@@ -66,6 +66,7 @@
66
66
  "mammoth": "^1.4.27",
67
67
  "mongodb": "^6.13.1",
68
68
  "node-forge": "^1.3.1",
69
+ "p-limit": "^7.1.1",
69
70
  "pdf2json": "^3.1.6",
70
71
  "resend": "^4.7.0",
71
72
  "snowflake-sdk": "^2.0.2",