@sentry/junior 0.96.0 → 0.97.0
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/{agent-hooks-5URSMY3O.js → agent-hooks-XSUIVRQS.js} +8 -6
- package/dist/{reporting → api}/conversations/activity.d.ts +1 -1
- package/dist/api/conversations/detail-projection.d.ts +10 -0
- package/dist/api/conversations/detail.d.ts +3 -0
- package/dist/api/conversations/detail.query.d.ts +3 -0
- package/dist/api/conversations/list.d.ts +3 -0
- package/dist/api/conversations/list.query.d.ts +94 -0
- package/dist/api/conversations/projection.d.ts +14 -0
- package/dist/api/conversations/schema.d.ts +683 -0
- package/dist/{reporting → api}/conversations/shared.d.ts +1 -5
- package/dist/api/conversations/stats.d.ts +3 -0
- package/dist/api/conversations/stats.query.d.ts +3 -0
- package/dist/api/conversations/subagent.d.ts +3 -0
- package/dist/{reporting → api}/conversations/transcript.d.ts +4 -4
- package/dist/api/people/list.d.ts +1 -2
- package/dist/api/people/list.query.d.ts +3 -4
- package/dist/api/people/profile.d.ts +1 -2
- package/dist/api/people/profile.query.d.ts +3 -4
- package/dist/api/people/schema.d.ts +193 -0
- package/dist/api/people/shared.d.ts +31 -9
- package/dist/api/schema.d.ts +20 -0
- package/dist/api/schema.js +50 -0
- package/dist/api-reference.d.ts +4 -3
- package/dist/api.d.ts +3 -0
- package/dist/api.js +156 -0
- package/dist/app.d.ts +1 -4
- package/dist/app.js +100 -366
- package/dist/build/virtual-config.d.ts +2 -2
- package/dist/{catalog-runtime-MSMAYYIK.js → catalog-runtime-PFHVMHDR.js} +3 -2
- package/dist/chat/agent/resume.d.ts +3 -1
- package/dist/chat/agent/tools.d.ts +2 -1
- package/dist/chat/config.d.ts +1 -8
- package/dist/chat/conversations/history.d.ts +241 -27
- package/dist/chat/conversations/legacy-advisor-session.d.ts +7 -0
- package/dist/chat/conversations/legacy-import.d.ts +2 -2
- package/dist/chat/conversations/projection.d.ts +19 -3
- package/dist/chat/conversations/sql/legacy-history-import.d.ts +1 -1
- package/dist/chat/conversations/sql/store.d.ts +15 -9
- package/dist/chat/conversations/store.d.ts +9 -4
- package/dist/chat/model-profile.d.ts +14 -0
- package/dist/chat/pi/transcript.d.ts +5 -3
- package/dist/chat/resource-events/store.d.ts +2 -2
- package/dist/chat/services/context-compaction.d.ts +15 -0
- package/dist/chat/services/turn-result.d.ts +1 -0
- package/dist/chat/services/turn-session-record.d.ts +6 -6
- package/dist/chat/state/turn-session.d.ts +16 -5
- package/dist/chat/task-execution/store.d.ts +2 -2
- package/dist/chat/tools/handoff/tool.d.ts +45 -0
- package/dist/chat/tools/types.d.ts +7 -2
- package/dist/chat/usage.d.ts +25 -25
- package/dist/{chunk-RITD7E5L.js → chunk-35BNLDOJ.js} +338 -577
- package/dist/{chunk-PMNN6ST4.js → chunk-3ECW2K2O.js} +7 -2
- package/dist/{chunk-5C6XTTFI.js → chunk-3F4OPKGF.js} +89 -55
- package/dist/chunk-3V7OFJZC.js +264 -0
- package/dist/chunk-4ZNGQH7C.js +25 -0
- package/dist/{chunk-S5KZ5LPG.js → chunk-5HLDPVYS.js} +14 -447
- package/dist/{chunk-DZENG5ML.js → chunk-6U26PYI5.js} +24 -14
- package/dist/chunk-AUUCAFDE.js +546 -0
- package/dist/{chunk-6APU57E6.js → chunk-B3IT5END.js} +41 -260
- package/dist/chunk-CEA3A3UA.js +86 -0
- package/dist/{chunk-YDT7SNCN.js → chunk-D5KZYS4T.js} +1 -1
- package/dist/{chunk-YDVEH3SB.js → chunk-EEZCY5WX.js} +78 -28
- package/dist/{chunk-2NAZZPPS.js → chunk-EOWXRY5E.js} +1 -1
- package/dist/{chunk-GQ3GK22G.js → chunk-FBHZJFHI.js} +2 -2
- package/dist/{chunk-CZEX6KIQ.js → chunk-G3E7SCME.js} +0 -23
- package/dist/chunk-GUAP3H7O.js +319 -0
- package/dist/{chunk-BYZWPRJA.js → chunk-H66UCBPW.js} +6 -4
- package/dist/{chunk-L7CDEIVJ.js → chunk-LMJPRYHP.js} +4 -2
- package/dist/chunk-LUAE4MGW.js +199 -0
- package/dist/{chunk-GY34VGP5.js → chunk-LVA4I4H7.js} +3 -1
- package/dist/chunk-LVUKF5CE.js +63 -0
- package/dist/{chunk-AAZXTVJT.js → chunk-NG2AV7IB.js} +2 -72
- package/dist/chunk-ON6YKIQ7.js +260 -0
- package/dist/chunk-PDO5BLNM.js +22 -0
- package/dist/chunk-QNVHH5PU.js +20 -0
- package/dist/{chunk-P3YFQLEN.js → chunk-T533JLM3.js} +8 -10
- package/dist/chunk-UAMKJPDF.js +11 -0
- package/dist/chunk-VFH4ZKZT.js +66 -0
- package/dist/{chunk-UKHVMPX5.js → chunk-WGXMTFNU.js} +15 -17
- package/dist/{chunk-MNTINMWR.js → chunk-WJOSZOON.js} +184 -300
- package/dist/chunk-WTYEKPXU.js +451 -0
- package/dist/{chunk-IYM2SIVG.js → chunk-XIRPQQVE.js} +605 -363
- package/dist/cli/chat.js +24 -21
- package/dist/cli/check.js +4 -3
- package/dist/cli/init.js +12 -0
- package/dist/cli/plugins.js +12 -9
- package/dist/cli/snapshot-warmup.js +8 -6
- package/dist/cli/upgrade.js +265 -137
- package/dist/db/db.d.ts +4 -0
- package/dist/db/schema/conversations.d.ts +128 -0
- package/dist/db/schema.d.ts +129 -60
- package/dist/{db-2D6RMG67.js → db-BWXYIKFQ.js} +3 -3
- package/dist/detail-GA6NXWQJ.js +43 -0
- package/dist/discovery-RMHYSRL7.js +43 -0
- package/dist/handlers/health.d.ts +3 -0
- package/dist/instrumentation.js +1 -1
- package/dist/{legacy-import-VZ2MEGP7.js → legacy-import-3NQJVGQL.js} +4 -4
- package/dist/{api/people/list.js → list-A3WFRIYX.js} +26 -16
- package/dist/list-UXJI5Q5X.js +27 -0
- package/dist/nitro.d.ts +1 -1
- package/dist/nitro.js +2 -1
- package/dist/plugin-conversations-DDNGC35A.js +36 -0
- package/dist/{api/people/profile.js → profile-4RYDXWTF.js} +33 -18
- package/dist/reporting/plugin-conversations.d.ts +3 -0
- package/dist/reporting-schema.d.ts +140 -0
- package/dist/reporting.d.ts +12 -81
- package/dist/{runner-JZBRQP4F.js → runner-JALSYH2P.js} +17 -12
- package/dist/skills-MAZXZ2KE.js +26 -0
- package/dist/stats-XKXH3PTY.js +217 -0
- package/dist/subagent-TZ55XY37.js +48 -0
- package/dist/usage-schema.d.ts +25 -0
- package/dist/{validation-6XOOQGME.js → validation-FMEAEKCY.js} +4 -3
- package/migrations/0000_initial.sql +115 -0
- package/migrations/0001_conversation_metrics.sql +4 -0
- package/migrations/README.md +12 -30
- package/migrations/meta/0000_snapshot.json +1 -33
- package/migrations/meta/0001_snapshot.json +972 -0
- package/migrations/meta/_journal.json +9 -2
- package/package.json +14 -18
- package/dist/api/people/types.d.ts +0 -85
- package/dist/chat/advisor-request.d.ts +0 -4
- package/dist/chat/conversations/sql/migrations.d.ts +0 -22
- package/dist/chat/tools/advisor/session-store.d.ts +0 -9
- package/dist/chat/tools/advisor/tool.d.ts +0 -72
- package/dist/chunk-RAIB6WSG.js +0 -142
- package/dist/chunk-WM6KHPBQ.js +0 -48
- package/dist/db/schema/migrations.d.ts +0 -58
- package/dist/reporting/conversations/context.d.ts +0 -8
- package/dist/reporting/conversations/details.d.ts +0 -6
- package/dist/reporting/conversations/index.d.ts +0 -4
- package/dist/reporting/conversations/statistics.d.ts +0 -4
- package/dist/reporting/conversations/summaries.d.ts +0 -20
- package/dist/reporting/conversations/types.d.ts +0 -191
- package/dist/reporting/conversations.d.ts +0 -1
- package/dist/reporting.js +0 -1155
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
import {
|
|
2
|
+
sleep
|
|
3
|
+
} from "./chunk-4ZNGQH7C.js";
|
|
4
|
+
import {
|
|
5
|
+
getSlackBotToken
|
|
6
|
+
} from "./chunk-EEZCY5WX.js";
|
|
7
|
+
import {
|
|
8
|
+
logWarn,
|
|
9
|
+
setSpanAttributes,
|
|
10
|
+
setSpanStatus,
|
|
11
|
+
withSpan
|
|
12
|
+
} from "./chunk-I35CTGPO.js";
|
|
13
|
+
import {
|
|
14
|
+
parseSlackChannelReferenceId
|
|
15
|
+
} from "./chunk-7FBGKXPE.js";
|
|
16
|
+
|
|
17
|
+
// src/chat/slack/client.ts
|
|
18
|
+
import { AsyncLocalStorage as AsyncLocalStorage2 } from "async_hooks";
|
|
19
|
+
import { WebClient } from "@slack/web-api";
|
|
20
|
+
|
|
21
|
+
// src/chat/slack/workspace-context.ts
|
|
22
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
23
|
+
var workspaceTeamIdStorage = new AsyncLocalStorage();
|
|
24
|
+
function runWithWorkspaceTeamId(teamId, fn) {
|
|
25
|
+
if (!teamId) return fn();
|
|
26
|
+
return workspaceTeamIdStorage.run(teamId, fn);
|
|
27
|
+
}
|
|
28
|
+
function getWorkspaceTeamId() {
|
|
29
|
+
return workspaceTeamIdStorage.getStore();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// src/chat/slack/client.ts
|
|
33
|
+
var SlackActionError = class extends Error {
|
|
34
|
+
code;
|
|
35
|
+
apiError;
|
|
36
|
+
needed;
|
|
37
|
+
provided;
|
|
38
|
+
statusCode;
|
|
39
|
+
requestId;
|
|
40
|
+
errorData;
|
|
41
|
+
retryAfterSeconds;
|
|
42
|
+
detail;
|
|
43
|
+
detailLine;
|
|
44
|
+
detailRule;
|
|
45
|
+
constructor(message, code, options = {}) {
|
|
46
|
+
super(message);
|
|
47
|
+
this.name = "SlackActionError";
|
|
48
|
+
this.code = code;
|
|
49
|
+
this.apiError = options.apiError;
|
|
50
|
+
this.needed = options.needed;
|
|
51
|
+
this.provided = options.provided;
|
|
52
|
+
this.statusCode = options.statusCode;
|
|
53
|
+
this.requestId = options.requestId;
|
|
54
|
+
this.errorData = options.errorData;
|
|
55
|
+
this.retryAfterSeconds = options.retryAfterSeconds;
|
|
56
|
+
this.detail = options.detail;
|
|
57
|
+
this.detailLine = options.detailLine;
|
|
58
|
+
this.detailRule = options.detailRule;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
function serializeSlackErrorData(data) {
|
|
62
|
+
if (!data || typeof data !== "object") {
|
|
63
|
+
return void 0;
|
|
64
|
+
}
|
|
65
|
+
const filtered = Object.fromEntries(
|
|
66
|
+
Object.entries(data).filter(
|
|
67
|
+
([key]) => key !== "error"
|
|
68
|
+
)
|
|
69
|
+
);
|
|
70
|
+
if (Object.keys(filtered).length === 0) {
|
|
71
|
+
return void 0;
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
const serialized = JSON.stringify(filtered);
|
|
75
|
+
return serialized.length <= 600 ? serialized : `${serialized.slice(0, 597)}...`;
|
|
76
|
+
} catch {
|
|
77
|
+
return void 0;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function getHeaderString(headers, name) {
|
|
81
|
+
if (!headers || typeof headers !== "object") {
|
|
82
|
+
return void 0;
|
|
83
|
+
}
|
|
84
|
+
const key = name.toLowerCase();
|
|
85
|
+
const entries = headers;
|
|
86
|
+
for (const [entryKey, value] of Object.entries(entries)) {
|
|
87
|
+
if (entryKey.toLowerCase() !== key) continue;
|
|
88
|
+
if (typeof value === "string") return value;
|
|
89
|
+
if (Array.isArray(value)) {
|
|
90
|
+
const first = value.find((entry) => typeof entry === "string");
|
|
91
|
+
return typeof first === "string" ? first : void 0;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return void 0;
|
|
95
|
+
}
|
|
96
|
+
function parseSlackCanvasDetail(detail) {
|
|
97
|
+
if (typeof detail !== "string") {
|
|
98
|
+
return {};
|
|
99
|
+
}
|
|
100
|
+
const trimmed = detail.trim();
|
|
101
|
+
if (!trimmed) {
|
|
102
|
+
return {};
|
|
103
|
+
}
|
|
104
|
+
const parsed = {
|
|
105
|
+
detail: trimmed
|
|
106
|
+
};
|
|
107
|
+
const lineMatch = trimmed.match(/line\s+(\d+):/i);
|
|
108
|
+
if (lineMatch) {
|
|
109
|
+
const line = Number.parseInt(lineMatch[1] ?? "", 10);
|
|
110
|
+
if (Number.isFinite(line)) {
|
|
111
|
+
parsed.detailLine = line;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (/unsupported heading depth/i.test(trimmed)) {
|
|
115
|
+
parsed.detailRule = "unsupported_heading_depth";
|
|
116
|
+
}
|
|
117
|
+
return parsed;
|
|
118
|
+
}
|
|
119
|
+
var installationTokenStorage = new AsyncLocalStorage2();
|
|
120
|
+
var clientsByToken = /* @__PURE__ */ new Map();
|
|
121
|
+
function runWithSlackInstallationToken(token, fn) {
|
|
122
|
+
const trimmed = token.trim();
|
|
123
|
+
if (!trimmed) {
|
|
124
|
+
throw new SlackActionError(
|
|
125
|
+
"Slack installation token binding requires a non-empty token",
|
|
126
|
+
"missing_token"
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
return installationTokenStorage.run({ token: trimmed }, fn);
|
|
130
|
+
}
|
|
131
|
+
function resolveSlackToken() {
|
|
132
|
+
const ambientToken = installationTokenStorage.getStore()?.token;
|
|
133
|
+
if (ambientToken) {
|
|
134
|
+
return ambientToken;
|
|
135
|
+
}
|
|
136
|
+
const envToken = getSlackBotToken();
|
|
137
|
+
if (envToken) {
|
|
138
|
+
return envToken;
|
|
139
|
+
}
|
|
140
|
+
const teamId = getWorkspaceTeamId();
|
|
141
|
+
if (teamId) {
|
|
142
|
+
throw new SlackActionError(
|
|
143
|
+
`Slack call is scoped to workspace ${teamId} but no installation token is bound and no default bot token is configured`,
|
|
144
|
+
"missing_token"
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
throw new SlackActionError(
|
|
148
|
+
"SLACK_BOT_TOKEN (or SLACK_BOT_USER_TOKEN) is required for Slack Web API actions in this service",
|
|
149
|
+
"missing_token"
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
function normalizeSlackConversationId(channelId) {
|
|
153
|
+
return parseSlackChannelReferenceId(channelId);
|
|
154
|
+
}
|
|
155
|
+
function getClient() {
|
|
156
|
+
const token = resolveSlackToken();
|
|
157
|
+
let cached = clientsByToken.get(token);
|
|
158
|
+
if (!cached) {
|
|
159
|
+
cached = new WebClient(token, {
|
|
160
|
+
retryConfig: { retries: 0 },
|
|
161
|
+
rejectRateLimitedCalls: true
|
|
162
|
+
});
|
|
163
|
+
clientsByToken.set(token, cached);
|
|
164
|
+
}
|
|
165
|
+
return cached;
|
|
166
|
+
}
|
|
167
|
+
function mapSlackError(error) {
|
|
168
|
+
if (error instanceof SlackActionError) {
|
|
169
|
+
return error;
|
|
170
|
+
}
|
|
171
|
+
const candidate = error;
|
|
172
|
+
const apiError = candidate.data?.error;
|
|
173
|
+
const message = candidate.message ?? "Slack action failed";
|
|
174
|
+
const baseOptions = {
|
|
175
|
+
apiError,
|
|
176
|
+
statusCode: candidate.statusCode,
|
|
177
|
+
requestId: getHeaderString(candidate.headers, "x-slack-req-id"),
|
|
178
|
+
errorData: serializeSlackErrorData(candidate.data),
|
|
179
|
+
...parseSlackCanvasDetail(candidate.data?.detail)
|
|
180
|
+
};
|
|
181
|
+
if (apiError === "missing_scope") {
|
|
182
|
+
return new SlackActionError(message, "missing_scope", {
|
|
183
|
+
...baseOptions,
|
|
184
|
+
needed: candidate.data?.needed,
|
|
185
|
+
provided: candidate.data?.provided
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
if (apiError === "not_in_channel") {
|
|
189
|
+
return new SlackActionError(message, "not_in_channel", baseOptions);
|
|
190
|
+
}
|
|
191
|
+
if (apiError === "restricted_action_read_only_channel") {
|
|
192
|
+
return new SlackActionError(message, "read_only_channel", baseOptions);
|
|
193
|
+
}
|
|
194
|
+
if (apiError === "already_reacted") {
|
|
195
|
+
return new SlackActionError(message, "already_reacted", baseOptions);
|
|
196
|
+
}
|
|
197
|
+
if (apiError === "no_reaction") {
|
|
198
|
+
return new SlackActionError(message, "no_reaction", baseOptions);
|
|
199
|
+
}
|
|
200
|
+
if (apiError === "invalid_arguments") {
|
|
201
|
+
return new SlackActionError(message, "invalid_arguments", baseOptions);
|
|
202
|
+
}
|
|
203
|
+
if (apiError === "invalid_cursor") {
|
|
204
|
+
return new SlackActionError(message, "invalid_arguments", baseOptions);
|
|
205
|
+
}
|
|
206
|
+
if (apiError === "invalid_name") {
|
|
207
|
+
return new SlackActionError(message, "invalid_arguments", baseOptions);
|
|
208
|
+
}
|
|
209
|
+
if (apiError === "not_found" || apiError === "channel_not_found" || apiError === "message_not_found") {
|
|
210
|
+
return new SlackActionError(message, "not_found", baseOptions);
|
|
211
|
+
}
|
|
212
|
+
if (apiError === "feature_not_enabled" || apiError === "not_allowed_token_type") {
|
|
213
|
+
return new SlackActionError(message, "feature_unavailable", baseOptions);
|
|
214
|
+
}
|
|
215
|
+
if (apiError === "canvas_creation_failed") {
|
|
216
|
+
return new SlackActionError(message, "canvas_creation_failed", baseOptions);
|
|
217
|
+
}
|
|
218
|
+
if (apiError === "canvas_editing_failed") {
|
|
219
|
+
return new SlackActionError(message, "canvas_editing_failed", baseOptions);
|
|
220
|
+
}
|
|
221
|
+
if (candidate.code === "slack_webapi_rate_limited_error" || candidate.statusCode === 429) {
|
|
222
|
+
return new SlackActionError(message, "rate_limited", {
|
|
223
|
+
...baseOptions,
|
|
224
|
+
retryAfterSeconds: candidate.retryAfter
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
return new SlackActionError(message, "internal_error", baseOptions);
|
|
228
|
+
}
|
|
229
|
+
var MAX_RETRY_DELAY_MS = 1e4;
|
|
230
|
+
var MAX_TOTAL_RETRY_DELAY_MS = 2e4;
|
|
231
|
+
var CONNECTION_ERROR_CODES = /* @__PURE__ */ new Set([
|
|
232
|
+
"ECONNREFUSED",
|
|
233
|
+
"ECONNRESET",
|
|
234
|
+
"ENOTFOUND",
|
|
235
|
+
"EAI_AGAIN"
|
|
236
|
+
]);
|
|
237
|
+
var TIMEOUT_ERROR_CODES = /* @__PURE__ */ new Set([
|
|
238
|
+
"ETIMEDOUT",
|
|
239
|
+
"ECONNABORTED",
|
|
240
|
+
"ESOCKETTIMEDOUT"
|
|
241
|
+
]);
|
|
242
|
+
function findNetworkErrorCode(error, depth = 0) {
|
|
243
|
+
if (!error || typeof error !== "object" || depth > 4) {
|
|
244
|
+
return void 0;
|
|
245
|
+
}
|
|
246
|
+
const candidate = error;
|
|
247
|
+
if (typeof candidate.code === "string" && (CONNECTION_ERROR_CODES.has(candidate.code) || TIMEOUT_ERROR_CODES.has(candidate.code))) {
|
|
248
|
+
return candidate.code;
|
|
249
|
+
}
|
|
250
|
+
return findNetworkErrorCode(candidate.original, depth + 1) ?? findNetworkErrorCode(candidate.cause, depth + 1);
|
|
251
|
+
}
|
|
252
|
+
function hasSocketHangUpMessage(error) {
|
|
253
|
+
return error instanceof Error && error.message.toLowerCase().includes("socket hang up");
|
|
254
|
+
}
|
|
255
|
+
function classifySlackRetry(raw, mapped) {
|
|
256
|
+
if (mapped.code === "rate_limited") {
|
|
257
|
+
return "rate_limited";
|
|
258
|
+
}
|
|
259
|
+
if (mapped.statusCode !== void 0 && mapped.statusCode >= 500) {
|
|
260
|
+
return "server_error";
|
|
261
|
+
}
|
|
262
|
+
const networkCode = findNetworkErrorCode(raw);
|
|
263
|
+
if (networkCode && CONNECTION_ERROR_CODES.has(networkCode)) {
|
|
264
|
+
return "connection";
|
|
265
|
+
}
|
|
266
|
+
if (networkCode && TIMEOUT_ERROR_CODES.has(networkCode)) {
|
|
267
|
+
return "timeout";
|
|
268
|
+
}
|
|
269
|
+
if (hasSocketHangUpMessage(raw)) {
|
|
270
|
+
return "connection";
|
|
271
|
+
}
|
|
272
|
+
return "none";
|
|
273
|
+
}
|
|
274
|
+
async function withSlackRetries(task, maxAttempts = 3, context = {}) {
|
|
275
|
+
let attempt = 0;
|
|
276
|
+
let totalDelayMs = 0;
|
|
277
|
+
const action = context.action ?? "unknown";
|
|
278
|
+
while (attempt < maxAttempts) {
|
|
279
|
+
attempt += 1;
|
|
280
|
+
const attemptNumber = attempt;
|
|
281
|
+
try {
|
|
282
|
+
return await withSpan(
|
|
283
|
+
`POST slack.com/api/${action}`,
|
|
284
|
+
"http.client",
|
|
285
|
+
{},
|
|
286
|
+
async () => {
|
|
287
|
+
try {
|
|
288
|
+
return await task();
|
|
289
|
+
} catch (error) {
|
|
290
|
+
const mapped = mapSlackError(error);
|
|
291
|
+
const errorAttrs = {
|
|
292
|
+
"error.type": mapped.code
|
|
293
|
+
};
|
|
294
|
+
if (mapped.apiError) {
|
|
295
|
+
errorAttrs["app.slack.api_error_code"] = mapped.apiError;
|
|
296
|
+
}
|
|
297
|
+
if (mapped.code === "rate_limited") {
|
|
298
|
+
errorAttrs["http.response.status_code"] = 429;
|
|
299
|
+
if (mapped.retryAfterSeconds) {
|
|
300
|
+
errorAttrs["app.slack.retry_after_ms"] = mapped.retryAfterSeconds * 1e3;
|
|
301
|
+
}
|
|
302
|
+
} else if (mapped.statusCode != null) {
|
|
303
|
+
errorAttrs["http.response.status_code"] = mapped.statusCode;
|
|
304
|
+
}
|
|
305
|
+
setSpanAttributes(errorAttrs);
|
|
306
|
+
setSpanStatus("error");
|
|
307
|
+
throw error;
|
|
308
|
+
}
|
|
309
|
+
},
|
|
310
|
+
{
|
|
311
|
+
"http.request.method": "POST",
|
|
312
|
+
"server.address": "slack.com",
|
|
313
|
+
"url.scheme": "https",
|
|
314
|
+
"url.path": `/api/${action}`,
|
|
315
|
+
"app.slack.method": action,
|
|
316
|
+
"app.retry.max_attempts": maxAttempts,
|
|
317
|
+
...attemptNumber > 1 ? { "http.resend_count": attemptNumber - 1 } : {},
|
|
318
|
+
...context.attributes ?? {},
|
|
319
|
+
...context.spanAttributes ?? {}
|
|
320
|
+
}
|
|
321
|
+
);
|
|
322
|
+
} catch (error) {
|
|
323
|
+
const mapped = mapSlackError(error);
|
|
324
|
+
const retryClass = classifySlackRetry(error, mapped);
|
|
325
|
+
const isRetryable = retryClass === "rate_limited" || retryClass === "connection" || retryClass === "server_error" || retryClass === "timeout" && context.idempotent === true;
|
|
326
|
+
const remainingDelayBudgetMs = MAX_TOTAL_RETRY_DELAY_MS - totalDelayMs;
|
|
327
|
+
const baseLogAttributes = {
|
|
328
|
+
"app.slack.action": action,
|
|
329
|
+
"app.slack.error_code": mapped.code,
|
|
330
|
+
...mapped.apiError ? { "app.slack.api_error": mapped.apiError } : {},
|
|
331
|
+
...mapped.detail ? { "app.slack.detail": mapped.detail } : {},
|
|
332
|
+
...mapped.detailLine !== void 0 ? { "app.slack.detail_line": mapped.detailLine } : {},
|
|
333
|
+
...mapped.detailRule ? { "app.slack.detail_rule": mapped.detailRule } : {},
|
|
334
|
+
...mapped.requestId ? { "app.slack.request_id": mapped.requestId } : {},
|
|
335
|
+
...mapped.statusCode !== void 0 ? { "http.response.status_code": mapped.statusCode } : {},
|
|
336
|
+
...context.attributes ?? {}
|
|
337
|
+
};
|
|
338
|
+
if (!isRetryable || attempt >= maxAttempts || remainingDelayBudgetMs <= 0) {
|
|
339
|
+
logWarn(
|
|
340
|
+
"slack_action_failed",
|
|
341
|
+
{},
|
|
342
|
+
{
|
|
343
|
+
...baseLogAttributes,
|
|
344
|
+
...mapped.errorData ? { "app.slack.error_data": mapped.errorData } : {}
|
|
345
|
+
},
|
|
346
|
+
"Slack action failed"
|
|
347
|
+
);
|
|
348
|
+
throw mapped;
|
|
349
|
+
}
|
|
350
|
+
logWarn(
|
|
351
|
+
"slack_action_retrying",
|
|
352
|
+
{},
|
|
353
|
+
{
|
|
354
|
+
...baseLogAttributes,
|
|
355
|
+
"app.slack.retry_attempt": attempt,
|
|
356
|
+
"app.slack.retry_class": retryClass
|
|
357
|
+
},
|
|
358
|
+
"Retrying Slack action after transient failure"
|
|
359
|
+
);
|
|
360
|
+
const retryAfterMs = retryClass === "rate_limited" && mapped.retryAfterSeconds && mapped.retryAfterSeconds > 0 ? mapped.retryAfterSeconds * 1e3 : void 0;
|
|
361
|
+
const backoffMs = Math.min(2e3, 250 * 2 ** (attempt - 1));
|
|
362
|
+
const delayMs = Math.min(
|
|
363
|
+
retryAfterMs ?? backoffMs,
|
|
364
|
+
MAX_RETRY_DELAY_MS,
|
|
365
|
+
remainingDelayBudgetMs
|
|
366
|
+
);
|
|
367
|
+
totalDelayMs += delayMs;
|
|
368
|
+
await sleep(delayMs);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
throw new SlackActionError(
|
|
372
|
+
"Slack action exhausted retries",
|
|
373
|
+
"internal_error"
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
function getSlackClient() {
|
|
377
|
+
return getClient();
|
|
378
|
+
}
|
|
379
|
+
function isDmChannel(channelId) {
|
|
380
|
+
const normalized = normalizeSlackConversationId(channelId);
|
|
381
|
+
return Boolean(normalized && normalized.startsWith("D"));
|
|
382
|
+
}
|
|
383
|
+
function isConversationScopedChannel(channelId) {
|
|
384
|
+
const normalized = normalizeSlackConversationId(channelId);
|
|
385
|
+
if (!normalized) return false;
|
|
386
|
+
return normalized.startsWith("C") || normalized.startsWith("G") || normalized.startsWith("D");
|
|
387
|
+
}
|
|
388
|
+
function isConversationChannel(channelId) {
|
|
389
|
+
const normalized = normalizeSlackConversationId(channelId);
|
|
390
|
+
if (!normalized) return false;
|
|
391
|
+
return normalized.startsWith("C") || normalized.startsWith("G");
|
|
392
|
+
}
|
|
393
|
+
async function getFilePermalink(fileId) {
|
|
394
|
+
const client = getClient();
|
|
395
|
+
const response = await withSlackRetries(
|
|
396
|
+
() => client.files.info({
|
|
397
|
+
file: fileId
|
|
398
|
+
}),
|
|
399
|
+
3,
|
|
400
|
+
{
|
|
401
|
+
action: "files.info",
|
|
402
|
+
idempotent: true,
|
|
403
|
+
spanAttributes: { "app.slack.file_id": fileId }
|
|
404
|
+
}
|
|
405
|
+
);
|
|
406
|
+
return response.file?.permalink;
|
|
407
|
+
}
|
|
408
|
+
async function downloadPrivateSlackFile(url) {
|
|
409
|
+
const token = resolveSlackToken();
|
|
410
|
+
return withSpan(
|
|
411
|
+
"GET files.slack.com",
|
|
412
|
+
"http.client",
|
|
413
|
+
{},
|
|
414
|
+
async () => {
|
|
415
|
+
const response = await fetch(url, {
|
|
416
|
+
headers: {
|
|
417
|
+
Authorization: `Bearer ${token}`
|
|
418
|
+
}
|
|
419
|
+
});
|
|
420
|
+
setSpanAttributes({ "http.response.status_code": response.status });
|
|
421
|
+
if (!response.ok) {
|
|
422
|
+
setSpanAttributes({ "error.type": String(response.status) });
|
|
423
|
+
setSpanStatus("error");
|
|
424
|
+
throw new Error(`Slack file download failed: ${response.status}`);
|
|
425
|
+
}
|
|
426
|
+
return Buffer.from(await response.arrayBuffer());
|
|
427
|
+
},
|
|
428
|
+
{
|
|
429
|
+
"http.request.method": "GET",
|
|
430
|
+
"server.address": "files.slack.com",
|
|
431
|
+
"url.scheme": "https",
|
|
432
|
+
"app.slack.method": "files.download"
|
|
433
|
+
}
|
|
434
|
+
);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
export {
|
|
438
|
+
runWithWorkspaceTeamId,
|
|
439
|
+
getWorkspaceTeamId,
|
|
440
|
+
SlackActionError,
|
|
441
|
+
getHeaderString,
|
|
442
|
+
runWithSlackInstallationToken,
|
|
443
|
+
normalizeSlackConversationId,
|
|
444
|
+
withSlackRetries,
|
|
445
|
+
getSlackClient,
|
|
446
|
+
isDmChannel,
|
|
447
|
+
isConversationScopedChannel,
|
|
448
|
+
isConversationChannel,
|
|
449
|
+
getFilePermalink,
|
|
450
|
+
downloadPrivateSlackFile
|
|
451
|
+
};
|