@credal/actions 0.2.109 → 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.
- package/dist/actions/actionMapper.js +9 -1
- package/dist/actions/autogen/templates.d.ts +1 -0
- package/dist/actions/autogen/templates.js +116 -2
- package/dist/actions/autogen/types.d.ts +116 -20
- package/dist/actions/autogen/types.js +44 -1
- package/dist/actions/providers/confluence/updatePage.js +15 -14
- package/dist/actions/providers/jamf/types.d.ts +8 -0
- package/dist/actions/providers/jamf/types.js +7 -0
- package/dist/actions/providers/slackUser/searchSlack.d.ts +17 -0
- package/dist/actions/providers/slackUser/searchSlack.js +235 -0
- package/package.json +2 -1
- package/dist/actions/providers/generic/fillTemplateAction.d.ts +0 -7
- package/dist/actions/providers/generic/fillTemplateAction.js +0 -18
- package/dist/actions/providers/generic/genericApiCall.d.ts +0 -3
- package/dist/actions/providers/generic/genericApiCall.js +0 -38
- package/dist/actions/providers/google-oauth/getDriveContentById.d.ts +0 -3
- package/dist/actions/providers/google-oauth/getDriveContentById.js +0 -161
- package/dist/actions/providers/google-oauth/searchAndGetDriveContentByKeywords.d.ts +0 -3
- package/dist/actions/providers/google-oauth/searchAndGetDriveContentByKeywords.js +0 -47
- package/dist/actions/providers/google-oauth/searchDriveAndGetContentByKeywords.d.ts +0 -3
- package/dist/actions/providers/google-oauth/searchDriveAndGetContentByKeywords.js +0 -110
- package/dist/actions/providers/google-oauth/searchDriveAndGetContentByQuery.d.ts +0 -3
- package/dist/actions/providers/google-oauth/searchDriveAndGetContentByQuery.js +0 -78
- package/dist/actions/providers/google-oauth/utils/extractContentFromDriveFileId.d.ts +0 -15
- package/dist/actions/providers/google-oauth/utils/extractContentFromDriveFileId.js +0 -129
- package/dist/actions/providers/googlemaps/nearbysearch.d.ts +0 -3
- package/dist/actions/providers/googlemaps/nearbysearch.js +0 -96
- package/dist/actions/providers/snowflake/runSnowflakeQueryWriteResultsToS3.d.ts +0 -3
- package/dist/actions/providers/snowflake/runSnowflakeQueryWriteResultsToS3.js +0 -154
- package/dist/actions/providers/x/scrapeTweetDataWithNitter.d.ts +0 -3
- package/dist/actions/providers/x/scrapeTweetDataWithNitter.js +0 -45
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type slackUserSearchSlackFunction } from "../../autogen/types.js";
|
|
2
|
+
export type TimeRange = "latest" | "today" | "yesterday" | "last_7d" | "last_30d" | "all";
|
|
3
|
+
export interface SlackSearchMessage {
|
|
4
|
+
channelId: string;
|
|
5
|
+
ts: string;
|
|
6
|
+
text?: string;
|
|
7
|
+
userId?: string;
|
|
8
|
+
permalink?: string;
|
|
9
|
+
/** If thread: full thread (root first). If not thread: small context window around the hit. */
|
|
10
|
+
context?: Array<{
|
|
11
|
+
ts: string;
|
|
12
|
+
text?: string;
|
|
13
|
+
userId?: string;
|
|
14
|
+
}>;
|
|
15
|
+
}
|
|
16
|
+
declare const searchSlack: slackUserSearchSlackFunction;
|
|
17
|
+
export default searchSlack;
|
|
@@ -0,0 +1,235 @@
|
|
|
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 { WebClient } from "@slack/web-api";
|
|
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);
|
|
15
|
+
/* ===================== Helpers ===================== */
|
|
16
|
+
function normalizeChannelOperand(ch) {
|
|
17
|
+
const s = ch.trim();
|
|
18
|
+
if (/^[CGD][A-Z0-9]/i.test(s))
|
|
19
|
+
return s;
|
|
20
|
+
return s.replace(/^#/, "");
|
|
21
|
+
}
|
|
22
|
+
function timeFilter(range) {
|
|
23
|
+
switch (range) {
|
|
24
|
+
case "today":
|
|
25
|
+
return "after:today";
|
|
26
|
+
case "yesterday":
|
|
27
|
+
return "after:yesterday";
|
|
28
|
+
case "last_7d":
|
|
29
|
+
return "after:7 days ago";
|
|
30
|
+
case "last_30d":
|
|
31
|
+
return "after:30 days ago";
|
|
32
|
+
default:
|
|
33
|
+
return "";
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function lookupUserIdsByEmail(client, emails) {
|
|
37
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
38
|
+
const ids = [];
|
|
39
|
+
const tasks = emails.map((raw) => __awaiter(this, void 0, void 0, function* () {
|
|
40
|
+
var _a;
|
|
41
|
+
const email = raw.trim();
|
|
42
|
+
if (!email)
|
|
43
|
+
return null;
|
|
44
|
+
const res = yield client.users.lookupByEmail({ email });
|
|
45
|
+
const id = (_a = res.user) === null || _a === void 0 ? void 0 : _a.id;
|
|
46
|
+
if (id)
|
|
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);
|
|
54
|
+
return ids;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
function getMPIMName(client, userIds) {
|
|
58
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
59
|
+
var _a, _b;
|
|
60
|
+
const res = yield client.conversations.open({ users: userIds.join(",") });
|
|
61
|
+
const id = (_a = res.channel) === null || _a === void 0 ? void 0 : _a.id;
|
|
62
|
+
if (!id)
|
|
63
|
+
throw new Error("Failed to open conversation for provided users.");
|
|
64
|
+
const info = yield client.conversations.info({ channel: id });
|
|
65
|
+
if (!((_b = info.channel) === null || _b === void 0 ? void 0 : _b.name))
|
|
66
|
+
throw new Error("Failed to open conversation for provided users.");
|
|
67
|
+
return info.channel.name;
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
function getPermalink(client, channel, ts) {
|
|
71
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
72
|
+
try {
|
|
73
|
+
const res = yield client.chat.getPermalink({ channel, message_ts: ts });
|
|
74
|
+
return res.permalink;
|
|
75
|
+
}
|
|
76
|
+
catch (_a) {
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
function fetchOneMessage(client, channel, ts) {
|
|
82
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
83
|
+
const r = yield client.conversations.history({
|
|
84
|
+
channel,
|
|
85
|
+
latest: ts,
|
|
86
|
+
inclusive: true,
|
|
87
|
+
limit: 1,
|
|
88
|
+
});
|
|
89
|
+
return (r.messages && r.messages[0]) || undefined;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
function fetchThread(client, channel, threadTs) {
|
|
93
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
94
|
+
var _a;
|
|
95
|
+
const r = yield client.conversations.replies({
|
|
96
|
+
channel,
|
|
97
|
+
ts: threadTs,
|
|
98
|
+
limit: 50,
|
|
99
|
+
});
|
|
100
|
+
return (_a = r.messages) !== null && _a !== void 0 ? _a : [];
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
function fetchContextWindow(client, channel, ts) {
|
|
104
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
105
|
+
var _a, _b;
|
|
106
|
+
const out = [];
|
|
107
|
+
const anchor = yield fetchOneMessage(client, channel, ts);
|
|
108
|
+
if (!anchor)
|
|
109
|
+
return out;
|
|
110
|
+
const beforeRes = yield client.conversations.history({
|
|
111
|
+
channel,
|
|
112
|
+
latest: ts,
|
|
113
|
+
inclusive: false,
|
|
114
|
+
limit: 4,
|
|
115
|
+
});
|
|
116
|
+
out.push(...((_a = beforeRes.messages) !== null && _a !== void 0 ? _a : []).reverse());
|
|
117
|
+
out.push(anchor);
|
|
118
|
+
const afterRes = yield client.conversations.history({
|
|
119
|
+
channel,
|
|
120
|
+
oldest: ts,
|
|
121
|
+
inclusive: false,
|
|
122
|
+
limit: 5,
|
|
123
|
+
});
|
|
124
|
+
out.push(...((_b = afterRes.messages) !== null && _b !== void 0 ? _b : []));
|
|
125
|
+
return out;
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
/* ===================== Main Export ===================== */
|
|
129
|
+
const searchSlack = (_a) => __awaiter(void 0, [_a], void 0, function* ({ params, authParams, }) {
|
|
130
|
+
var _b, _c;
|
|
131
|
+
if (!authParams.authToken) {
|
|
132
|
+
throw new Error(MISSING_AUTH_TOKEN);
|
|
133
|
+
}
|
|
134
|
+
const client = new WebClient(authParams.authToken);
|
|
135
|
+
const { emails, channel, topic, timeRange, limit } = params;
|
|
136
|
+
const parts = [];
|
|
137
|
+
if (emails === null || emails === void 0 ? void 0 : emails.length) {
|
|
138
|
+
const userIds = yield lookupUserIdsByEmail(client, emails);
|
|
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)
|
|
144
|
+
throw new Error("No users resolved from emails.");
|
|
145
|
+
if (userIdsWithoutMe.length == 1) {
|
|
146
|
+
parts.push(`in:<@${userIdsWithoutMe[0]}>`);
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
const convoName = yield getMPIMName(client, userIdsWithoutMe);
|
|
150
|
+
parts.push(`in:${convoName}`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
else if (channel) {
|
|
154
|
+
parts.push(`in:${normalizeChannelOperand(channel)}`);
|
|
155
|
+
}
|
|
156
|
+
if (topic && topic.trim())
|
|
157
|
+
parts.push(topic.trim());
|
|
158
|
+
const tf = timeFilter(timeRange);
|
|
159
|
+
if (tf)
|
|
160
|
+
parts.push(tf);
|
|
161
|
+
const query = parts.join(" ").trim();
|
|
162
|
+
if (!query)
|
|
163
|
+
throw new Error("No query built — provide emails, channel, or topic.");
|
|
164
|
+
const count = Math.max(1, Math.min(100, limit));
|
|
165
|
+
const searchRes = yield client.search.messages({ query, count, highlight: true });
|
|
166
|
+
const matches = (_c = (_b = searchRes.messages) === null || _b === void 0 ? void 0 : _b.matches) !== null && _c !== void 0 ? _c : [];
|
|
167
|
+
const hits = matches.slice(0, limit).map(m => {
|
|
168
|
+
var _a, _b;
|
|
169
|
+
return ({
|
|
170
|
+
channelId: ((_a = m.channel) === null || _a === void 0 ? void 0 : _a.id) || ((_b = m.channel) === null || _b === void 0 ? void 0 : _b.name) || "",
|
|
171
|
+
ts: m.ts,
|
|
172
|
+
text: m.text,
|
|
173
|
+
userId: m.user,
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
const tasks = hits.map(h => limitHit(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
177
|
+
var _a, _b, _c, _d;
|
|
178
|
+
if (!h.ts)
|
|
179
|
+
return null;
|
|
180
|
+
try {
|
|
181
|
+
const anchor = yield fetchOneMessage(client, h.channelId, h.ts);
|
|
182
|
+
const rootTs = (anchor === null || anchor === void 0 ? void 0 : anchor.thread_ts) || h.ts;
|
|
183
|
+
if (anchor === null || anchor === void 0 ? void 0 : anchor.thread_ts) {
|
|
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 {
|
|
191
|
+
channelId: h.channelId,
|
|
192
|
+
ts: rootTs,
|
|
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
|
+
};
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
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 {
|
|
207
|
+
channelId: h.channelId,
|
|
208
|
+
ts: h.ts,
|
|
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
|
+
};
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
catch (_e) {
|
|
217
|
+
// fallback minimal object; still in parallel
|
|
218
|
+
return {
|
|
219
|
+
channelId: h.channelId,
|
|
220
|
+
ts: h.ts,
|
|
221
|
+
text: h.text,
|
|
222
|
+
userId: h.userId,
|
|
223
|
+
permalink: yield getPermalink(client, h.channelId, h.ts),
|
|
224
|
+
};
|
|
225
|
+
}
|
|
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);
|
|
232
|
+
results.sort((a, b) => Number(b.ts) - Number(a.ts));
|
|
233
|
+
return { query, results };
|
|
234
|
+
});
|
|
235
|
+
export default searchSlack;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@credal/actions",
|
|
3
|
-
"version": "0.2.
|
|
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",
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
const fillTemplateAction = (_a) => __awaiter(void 0, [_a], void 0, function* ({ template }) {
|
|
13
|
-
// Simply return the template without any modification
|
|
14
|
-
return {
|
|
15
|
-
result: template,
|
|
16
|
-
};
|
|
17
|
-
});
|
|
18
|
-
exports.default = fillTemplateAction;
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
const axios_1 = __importDefault(require("axios"));
|
|
16
|
-
const genericApiCall = (_a) => __awaiter(void 0, [_a], void 0, function* ({ params, }) {
|
|
17
|
-
try {
|
|
18
|
-
const { endpoint, method, headers, body } = params;
|
|
19
|
-
const response = yield (0, axios_1.default)({
|
|
20
|
-
url: endpoint,
|
|
21
|
-
method,
|
|
22
|
-
headers,
|
|
23
|
-
data: method !== "GET" ? body : undefined,
|
|
24
|
-
});
|
|
25
|
-
return {
|
|
26
|
-
statusCode: response.status,
|
|
27
|
-
headers: response.headers,
|
|
28
|
-
data: response.data,
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
catch (error) {
|
|
32
|
-
if (axios_1.default.isAxiosError(error)) {
|
|
33
|
-
throw Error("Axios Error: " + (error.message || "Failed to make API call"));
|
|
34
|
-
}
|
|
35
|
-
throw Error("Error: " + (error || "Failed to make API call"));
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
exports.default = genericApiCall;
|
|
@@ -1,161 +0,0 @@
|
|
|
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 pdf from "pdf-parse/lib/pdf-parse.js";
|
|
11
|
-
import { axiosClient } from "../../util/axiosClient.js";
|
|
12
|
-
import mammoth from "mammoth";
|
|
13
|
-
import { MISSING_AUTH_TOKEN } from "../../util/missingAuthConstants.js";
|
|
14
|
-
const getDriveFileContentByID = (_a) => __awaiter(void 0, [_a], void 0, function* ({ params, authParams, }) {
|
|
15
|
-
if (!authParams.authToken) {
|
|
16
|
-
return { success: false, error: MISSING_AUTH_TOKEN };
|
|
17
|
-
}
|
|
18
|
-
const { fileId, limit } = params;
|
|
19
|
-
try {
|
|
20
|
-
// First, get file metadata to determine the file type
|
|
21
|
-
const metadataUrl = `https://www.googleapis.com/drive/v3/files/${encodeURIComponent(fileId)}?fields=name,mimeType,size`;
|
|
22
|
-
const metadataRes = yield axiosClient.get(metadataUrl, {
|
|
23
|
-
headers: {
|
|
24
|
-
Authorization: `Bearer ${authParams.authToken}`,
|
|
25
|
-
},
|
|
26
|
-
});
|
|
27
|
-
const { name: fileName, mimeType, size } = metadataRes.data;
|
|
28
|
-
// Check if file is too large (50MB limit for safety)
|
|
29
|
-
if (size && parseInt(size) > 50 * 1024 * 1024) {
|
|
30
|
-
return {
|
|
31
|
-
success: false,
|
|
32
|
-
error: "File too large (>50MB)",
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
let content = "";
|
|
36
|
-
// Handle different file types - read content directly
|
|
37
|
-
if (mimeType === "application/vnd.google-apps.document") {
|
|
38
|
-
// Google Docs - download as plain text
|
|
39
|
-
const downloadUrl = `https://www.googleapis.com/drive/v3/files/${encodeURIComponent(fileId)}?alt=media&format=txt`;
|
|
40
|
-
const downloadRes = yield axiosClient.get(downloadUrl, {
|
|
41
|
-
headers: {
|
|
42
|
-
Authorization: `Bearer ${authParams.authToken}`,
|
|
43
|
-
},
|
|
44
|
-
responseType: 'text',
|
|
45
|
-
});
|
|
46
|
-
content = downloadRes.data;
|
|
47
|
-
}
|
|
48
|
-
else if (mimeType === "application/vnd.google-apps.spreadsheet") {
|
|
49
|
-
// Google Sheets - download as CSV
|
|
50
|
-
const downloadUrl = `https://www.googleapis.com/drive/v3/files/${encodeURIComponent(fileId)}?alt=media&format=csv`;
|
|
51
|
-
const downloadRes = yield axiosClient.get(downloadUrl, {
|
|
52
|
-
headers: {
|
|
53
|
-
Authorization: `Bearer ${authParams.authToken}`,
|
|
54
|
-
},
|
|
55
|
-
responseType: 'text',
|
|
56
|
-
});
|
|
57
|
-
content = downloadRes.data;
|
|
58
|
-
}
|
|
59
|
-
else if (mimeType === "application/vnd.google-apps.presentation") {
|
|
60
|
-
// Google Slides - download as plain text
|
|
61
|
-
const downloadUrl = `https://www.googleapis.com/drive/v3/files/${encodeURIComponent(fileId)}?alt=media&format=txt`;
|
|
62
|
-
const downloadRes = yield axiosClient.get(downloadUrl, {
|
|
63
|
-
headers: {
|
|
64
|
-
Authorization: `Bearer ${authParams.authToken}`,
|
|
65
|
-
},
|
|
66
|
-
responseType: 'text',
|
|
67
|
-
});
|
|
68
|
-
content = downloadRes.data;
|
|
69
|
-
}
|
|
70
|
-
else if (mimeType === "application/pdf") {
|
|
71
|
-
// PDF files - use pdf-parse
|
|
72
|
-
const downloadUrl = `https://www.googleapis.com/drive/v3/files/${encodeURIComponent(fileId)}?alt=media`;
|
|
73
|
-
const downloadRes = yield axiosClient.get(downloadUrl, {
|
|
74
|
-
headers: {
|
|
75
|
-
Authorization: `Bearer ${authParams.authToken}`,
|
|
76
|
-
},
|
|
77
|
-
responseType: 'arraybuffer',
|
|
78
|
-
});
|
|
79
|
-
try {
|
|
80
|
-
const pdfData = yield pdf(downloadRes.data);
|
|
81
|
-
content = pdfData.text;
|
|
82
|
-
}
|
|
83
|
-
catch (pdfError) {
|
|
84
|
-
return {
|
|
85
|
-
success: false,
|
|
86
|
-
error: `Failed to parse PDF: ${pdfError instanceof Error ? pdfError.message : 'Unknown PDF error'}`,
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
else if (mimeType === "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ||
|
|
91
|
-
mimeType === "application/msword") {
|
|
92
|
-
// Word documents (.docx or .doc) - download and extract text using mammoth
|
|
93
|
-
const downloadUrl = `https://www.googleapis.com/drive/v3/files/${encodeURIComponent(fileId)}?alt=media`;
|
|
94
|
-
const downloadRes = yield axiosClient.get(downloadUrl, {
|
|
95
|
-
headers: {
|
|
96
|
-
Authorization: `Bearer ${authParams.authToken}`,
|
|
97
|
-
},
|
|
98
|
-
responseType: 'arraybuffer',
|
|
99
|
-
});
|
|
100
|
-
try {
|
|
101
|
-
// mammoth works with .docx files. It will ignore formatting and return raw text
|
|
102
|
-
const result = yield mammoth.extractRawText({ buffer: Buffer.from(downloadRes.data) });
|
|
103
|
-
content = result.value; // raw text
|
|
104
|
-
}
|
|
105
|
-
catch (wordError) {
|
|
106
|
-
return {
|
|
107
|
-
success: false,
|
|
108
|
-
error: `Failed to parse Word document: ${wordError instanceof Error ? wordError.message : 'Unknown Word error'}`,
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
else if (mimeType === "text/plain" ||
|
|
113
|
-
mimeType === "text/html" ||
|
|
114
|
-
mimeType === "application/rtf" ||
|
|
115
|
-
(mimeType === null || mimeType === void 0 ? void 0 : mimeType.startsWith("text/"))) {
|
|
116
|
-
// Text-based files
|
|
117
|
-
const downloadUrl = `https://www.googleapis.com/drive/v3/files/${encodeURIComponent(fileId)}?alt=media`;
|
|
118
|
-
const downloadRes = yield axiosClient.get(downloadUrl, {
|
|
119
|
-
headers: {
|
|
120
|
-
Authorization: `Bearer ${authParams.authToken}`,
|
|
121
|
-
},
|
|
122
|
-
responseType: 'text',
|
|
123
|
-
});
|
|
124
|
-
content = downloadRes.data;
|
|
125
|
-
}
|
|
126
|
-
else if (mimeType === null || mimeType === void 0 ? void 0 : mimeType.startsWith("image/")) {
|
|
127
|
-
// Skip images
|
|
128
|
-
return {
|
|
129
|
-
success: false,
|
|
130
|
-
error: "Image files are not supported for text extraction",
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
else {
|
|
134
|
-
// Unsupported file type
|
|
135
|
-
return {
|
|
136
|
-
success: false,
|
|
137
|
-
error: `Unsupported file type: ${mimeType}`,
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
content = content.trim();
|
|
141
|
-
const originalLength = content.length;
|
|
142
|
-
// Naive way to truncate content
|
|
143
|
-
if (limit && content.length > limit) {
|
|
144
|
-
content = content.substring(0, limit);
|
|
145
|
-
}
|
|
146
|
-
return {
|
|
147
|
-
success: true,
|
|
148
|
-
content,
|
|
149
|
-
fileName,
|
|
150
|
-
fileLength: originalLength,
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
catch (error) {
|
|
154
|
-
console.error("Error getting Google Drive file content", error);
|
|
155
|
-
return {
|
|
156
|
-
success: false,
|
|
157
|
-
error: error instanceof Error ? error.message : "Unknown error",
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
export default getDriveFileContentByID;
|
|
@@ -1,47 +0,0 @@
|
|
|
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 { axiosClient } from "../../util/axiosClient.js";
|
|
11
|
-
import { MISSING_AUTH_TOKEN } from "../../util/missingAuthConstants.js";
|
|
12
|
-
const searchDriveByKeywords = (_a) => __awaiter(void 0, [_a], void 0, function* ({ params, authParams, }) {
|
|
13
|
-
var _b;
|
|
14
|
-
if (!authParams.authToken) {
|
|
15
|
-
return { success: false, error: MISSING_AUTH_TOKEN, files: [] };
|
|
16
|
-
}
|
|
17
|
-
const { keywords, limit } = params;
|
|
18
|
-
// Build the query: fullText contains 'keyword1' or fullText contains 'keyword2' ...
|
|
19
|
-
const query = keywords.map(kw => `fullText contains '${kw.replace(/'/g, "\\'")}'`).join(" or ");
|
|
20
|
-
const url = `https://www.googleapis.com/drive/v3/files?q=${encodeURIComponent(query)}&fields=files(id,name,mimeType,webViewLink)&supportsAllDrives=true&includeItemsFromAllDrives=true`;
|
|
21
|
-
// 1. Get the file metadata from google drive search
|
|
22
|
-
let files = [];
|
|
23
|
-
try {
|
|
24
|
-
const res = yield axiosClient.get(url, {
|
|
25
|
-
headers: {
|
|
26
|
-
Authorization: `Bearer ${authParams.authToken}`,
|
|
27
|
-
},
|
|
28
|
-
});
|
|
29
|
-
files =
|
|
30
|
-
((_b = res.data.files) === null || _b === void 0 ? void 0 : _b.map((file) => ({
|
|
31
|
-
id: file.id || "",
|
|
32
|
-
name: file.name || "",
|
|
33
|
-
mimeType: file.mimeType || "",
|
|
34
|
-
url: file.webViewLink || "",
|
|
35
|
-
}))) || [];
|
|
36
|
-
}
|
|
37
|
-
catch (error) {
|
|
38
|
-
console.error("Error searching Google Drive", error);
|
|
39
|
-
return {
|
|
40
|
-
success: false,
|
|
41
|
-
error: error instanceof Error ? error.message : "Unknown error",
|
|
42
|
-
files: [],
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
files = limit ? files.splice(0, limit) : files;
|
|
46
|
-
});
|
|
47
|
-
export default searchDriveByKeywords;
|