@sentry/junior 0.311.0 → 0.312.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/{adapter-context-G2M0y6rP.js → adapter-context-BRjEWRy1.js} +2 -2
- package/dist/{adapter-B1wujchl.js → adapter-iIq7ihf7.js} +1 -1
- package/dist/{admin-DCiBhUT1.js → admin-DhInw5K9.js} +1 -1
- package/dist/{agent-runner-BVHPUIc8.js → agent-runner-C9iDurwX.js} +3423 -3477
- package/dist/api/schema.d.ts +2 -2
- package/dist/api/schema.js +6 -6
- package/dist/api.d.ts +2 -2
- package/dist/api.js +289 -306
- package/dist/{app-Cw5Vy6H6.d.ts → app-DRFrC8Mw.d.ts} +2 -9
- package/dist/app.d.ts +1 -1
- package/dist/app.js +338 -604
- package/dist/{background-compaction-queue-CJDnWVOJ.js → background-compaction-queue-DrKi7w3d.js} +10 -85
- package/dist/callback-mFlTUWWx.js +75 -0
- package/dist/{catalog-runtime-DMY3VV-D.js → catalog-runtime-D7xapBAX.js} +1 -1
- package/dist/channel-access-CJGfNTdn.js +221 -0
- package/dist/child-message-DcsritGf.js +179 -0
- package/dist/cli/chat.js +18 -19
- package/dist/cli/check.js +3 -3
- package/dist/cli/init.js +0 -1
- package/dist/cli/main.js +3 -7
- package/dist/cli/plugins.js +4 -4
- package/dist/cli/run.d.ts +1 -2
- package/dist/cli/run.js +1 -2
- package/dist/cli/snapshot-create.js +5 -5
- package/dist/cli/upgrade.d.ts +1 -1
- package/dist/cli/upgrade.js +3 -3
- package/dist/{config-BrXDQefC.js → config-kXB2Y5tl.js} +59 -27
- package/dist/{continue-in-slack-DNXSC9So.js → continue-in-slack-O6pV5004.js} +7 -8
- package/dist/{conversation-feed-DkinDu5o.js → conversation-feed-uNOv_x68.js} +74 -20
- package/dist/{conversation-privacy-DxttrKb9.js → conversation-privacy-L4ja1_W6.js} +1 -1
- package/dist/{conversation-turn-DWw7Xhdr.js → conversation-turn-CeLbHqdw.js} +31 -27
- package/dist/{credential-sync-Bk8wGb8Y.js → credential-sync-I8VBGk05.js} +16 -15
- package/dist/{projection-Cns7XwWa.js → credentialed-Cb6E2phL.js} +16308 -20732
- package/dist/{db-7ETESH44.d.ts → db-BbC5stfa.d.ts} +11 -6
- package/dist/{db-DTxzH7u5.js → db-LAyyX8tk.js} +174 -91
- package/dist/{executor-DqTiYj9S.js → executor-SCub-fAq.js} +117 -97
- package/dist/{html-B9Sl4Axb.js → html-BCZ3Xtb_.js} +6 -6
- package/dist/ingest-BDf3D50j.js +3836 -0
- package/dist/{install-Sg4ePfLl.js → install-DCFJFkC0.js} +2 -2
- package/dist/instrumentation.js +3 -3
- package/dist/{lineage-BHAMpNKr.js → lineage-B_m-4TJw.js} +2 -2
- package/dist/{stop-CYsfc0Ed.js → list-CN2QA2R9.js} +663 -442
- package/dist/{logging-FjclQsXu.js → logging-dQ9ZRGQ8.js} +1 -1
- package/dist/{schedule-BLJ476XQ.js → media-CYm1QD8T.js} +1055 -11
- package/dist/{message-CJ7_RL50.js → message-DSXGj1hM.js} +19 -26
- package/dist/{model-input-CMLh4Cwq.js → model-input--TrP9hYV.js} +3 -3
- package/dist/nitro.d.ts +1 -1
- package/dist/nitro.js +4 -3
- package/dist/{oauth-callback-server-RVcbiDjL.js → oauth-callback-server-DHCZC6B4.js} +2 -2
- package/dist/{oauth-relay-Bu9kkaQu.js → oauth-relay-pPMGnDZt.js} +1 -1
- package/dist/{registry-BPwuOP2O.js → registry-CZOzddwu.js} +1 -1
- package/dist/{resolve-BK29bBry.js → resolve-cKOQ14Dh.js} +3 -3
- package/dist/result-DcvEOe1r.js +574 -0
- package/dist/result-event-type-BdIJuZxg.js +48 -0
- package/dist/{runner-BcAE-cJm.js → runner-DBfUW44l.js} +14 -16
- package/dist/{sandbox-egress-signals-C-q0go5b.js → sandbox-egress-signals-BVF_xAm1.js} +1 -1
- package/dist/{schema-DYcJeV7d.d.ts → schema-BEHKxr4I.d.ts} +166 -242
- package/dist/{schema-BLgSZXcL.js → schema-Bk9Nm2E8.js} +106 -121
- package/dist/{service-account-iJoCtkHR.js → service-account-CVYsFjty.js} +3 -3
- package/dist/{service-account-gN0zqf0S.js → service-account-Ts4ulcrV.js} +5 -5
- package/dist/{skills-BoZ80epJ.js → skills-VTnhAJWm.js} +2 -2
- package/dist/{slack-conversation-unfurl-CogYrITA.js → slack-conversation-unfurl-fuLnF1jE.js} +9 -9
- package/dist/{spawn-Dk92r7mX.js → spawn-vxAarlX5.js} +6 -5
- package/dist/start-conversation-CEn2KH1A.js +941 -0
- package/dist/{state-DH3Cmgp3.js → state-C1P3_Ywk.js} +4 -4
- package/dist/statuses-_cRJHXK7.js +150 -0
- package/dist/store-DFZaN-Ix.js +492 -0
- package/dist/task-queue-Cx4Lmjyk.js +90 -0
- package/dist/task-runner-CbTwFA6k.js +1432 -0
- package/dist/{thread-context-BW2BsQbH.js → thread-context-BkAZDTX2.js} +7 -7
- package/dist/thread-state-DhPndWi1.js +5625 -0
- package/dist/{validation-DrJd0nnq.js → validation-nIzMtAVJ.js} +1 -1
- package/dist/{task-queue-C_zldF5J.js → vercel-queue-hW90XzbG.js} +93 -172
- package/dist/{version-CNKZ8owc.js → version-Brbtz4Qf.js} +1 -1
- package/dist/version.js +1 -1
- package/dist/{warmup-BWdHew2H.js → warmup-BDA5myRK.js} +6463 -12290
- package/dist/web-message-id-C6P83ans.js +10 -0
- package/dist/{worker-t9qpu_Zx.js → worker-D63RcaVD.js} +20 -7
- package/migrations/0143_todos.sql +14 -0
- package/migrations/0144_inbox_items.sql +32 -0
- package/migrations/0145_todo_changed_by_agent.sql +1 -0
- package/migrations/0146_inbox_item_subject_title_parts.sql +1 -0
- package/migrations/0147_inbox_snooze_wake_reason.sql +1 -0
- package/migrations/0148_authorization_request_index.sql +1 -0
- package/migrations/0149_conversation_metadata.sql +10 -0
- package/migrations/0150_conversation_metadata_fill.sql +39 -0
- package/migrations/0151_conversation_search_index.sql +1 -0
- package/migrations/0152_inbox_item_legacy_automation_keys.sql +21 -0
- package/migrations/0153_artifact_expiry.sql +1 -0
- package/migrations/0154_artifact_permanent_sha_index.sql +1 -0
- package/migrations/0155_artifact_drop_public_sha_index.sql +3 -0
- package/migrations/0156_conversation_kind_lead.sql +6 -0
- package/migrations/0157_conversation_lead_actor_index.sql +1 -0
- package/migrations/meta/0143_snapshot.json +6434 -0
- package/migrations/meta/0144_snapshot.json +6434 -0
- package/migrations/meta/0145_snapshot.json +6440 -0
- package/migrations/meta/0146_snapshot.json +6446 -0
- package/migrations/meta/0147_snapshot.json +6452 -0
- package/migrations/meta/0148_snapshot.json +6474 -0
- package/migrations/meta/0149_snapshot.json +6490 -0
- package/migrations/meta/0150_snapshot.json +6490 -0
- package/migrations/meta/0151_snapshot.json +6505 -0
- package/migrations/meta/0152_snapshot.json +6505 -0
- package/migrations/meta/0153_snapshot.json +6511 -0
- package/migrations/meta/0154_snapshot.json +6533 -0
- package/migrations/meta/0155_snapshot.json +6511 -0
- package/migrations/meta/0156_snapshot.json +6511 -0
- package/migrations/meta/0157_snapshot.json +6527 -0
- package/migrations/meta/_journal.json +106 -1
- package/package.json +6 -6
- package/skills/design-automation-skill/SKILL.md +108 -0
- package/dist/briefs-BDjnug6Y.js +0 -395
- package/dist/conversation-ca__NDQW.js +0 -1063
- package/dist/prompt-CDqcsDYC.js +0 -910
- package/dist/store-DmmMIrQi.js +0 -335
- package/dist/task-runner-C7oCJQIx.js +0 -294
- package/dist/turn-session-routing-Xh_2nSQe.js +0 -261
- package/dist/web-message-id-Ch5dPq-a.js +0 -306
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { h as logWarn } from "./logging-
|
|
3
|
-
import { o as getDb } from "./db-
|
|
4
|
-
import { K as juniorUsers } from "./executor-
|
|
1
|
+
import { r as getChatConfig } from "./config-kXB2Y5tl.js";
|
|
2
|
+
import { h as logWarn } from "./logging-dQ9ZRGQ8.js";
|
|
3
|
+
import { o as getDb } from "./db-LAyyX8tk.js";
|
|
4
|
+
import { K as juniorUsers } from "./executor-SCub-fAq.js";
|
|
5
5
|
import { createCipheriv, createDecipheriv, createHash, randomBytes } from "node:crypto";
|
|
6
6
|
import { eq } from "drizzle-orm";
|
|
7
7
|
// src/chat/conversations/legacy-web-actor-id.ts
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
// src/api/reporting-window.ts
|
|
2
|
+
/** Shared UTC day/hour/six-hour windows for dashboard activity series. */
|
|
3
|
+
const HOUR_MS = 36e5;
|
|
4
|
+
const SIX_HOUR_MS = 6 * HOUR_MS;
|
|
5
|
+
const DAY_MS = 24 * HOUR_MS;
|
|
6
|
+
/** Start of the UTC calendar day containing `valueMs`. */
|
|
7
|
+
function startOfUtcDay(valueMs) {
|
|
8
|
+
const date = new Date(valueMs);
|
|
9
|
+
date.setUTCHours(0, 0, 0, 0);
|
|
10
|
+
return date;
|
|
11
|
+
}
|
|
12
|
+
/** Start of the UTC hour containing `valueMs`. */
|
|
13
|
+
function startOfUtcHour(valueMs) {
|
|
14
|
+
const date = new Date(valueMs);
|
|
15
|
+
date.setUTCMinutes(0, 0, 0);
|
|
16
|
+
return date;
|
|
17
|
+
}
|
|
18
|
+
/** Start of the UTC 6-hour bucket containing `valueMs` (00/06/12/18). */
|
|
19
|
+
function startOfUtcSixHour(valueMs) {
|
|
20
|
+
const date = startOfUtcHour(valueMs);
|
|
21
|
+
date.setUTCHours(Math.floor(date.getUTCHours() / 6) * 6, 0, 0, 0);
|
|
22
|
+
return date;
|
|
23
|
+
}
|
|
24
|
+
/** UTC day key `YYYY-MM-DD`. */
|
|
25
|
+
function utcDayKey(value) {
|
|
26
|
+
return (typeof value === "number" ? new Date(value) : value).toISOString().slice(0, 10);
|
|
27
|
+
}
|
|
28
|
+
/** UTC hour key `YYYY-MM-DDTHH`. */
|
|
29
|
+
function utcHourKey(value) {
|
|
30
|
+
return (typeof value === "number" ? new Date(value) : value).toISOString().slice(0, 13);
|
|
31
|
+
}
|
|
32
|
+
/** UTC 6-hour bucket key `YYYY-MM-DDTHH` at 00/06/12/18. */
|
|
33
|
+
function utcSixHourKey(value) {
|
|
34
|
+
return utcHourKey(startOfUtcSixHour(typeof value === "number" ? value : value.getTime()));
|
|
35
|
+
}
|
|
36
|
+
/** Inclusive trailing UTC day window ending on today's UTC day. */
|
|
37
|
+
function trailingUtcDayWindow(nowMs, dayCount) {
|
|
38
|
+
const end = startOfUtcDay(nowMs);
|
|
39
|
+
const start = new Date(end);
|
|
40
|
+
start.setUTCDate(start.getUTCDate() - (dayCount - 1));
|
|
41
|
+
return {
|
|
42
|
+
end,
|
|
43
|
+
start
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/** Inclusive trailing UTC hour window ending on the current UTC hour. */
|
|
47
|
+
function trailingUtcHourWindow(nowMs, hourCount = 24) {
|
|
48
|
+
const end = startOfUtcHour(nowMs);
|
|
49
|
+
return {
|
|
50
|
+
end,
|
|
51
|
+
start: /* @__PURE__ */ new Date(end.getTime() - (hourCount - 1) * HOUR_MS)
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/** Inclusive trailing UTC 6-hour window ending on the current 6h bucket. */
|
|
55
|
+
function trailingUtcSixHourWindow(nowMs, bucketCount = 28) {
|
|
56
|
+
const end = startOfUtcSixHour(nowMs);
|
|
57
|
+
return {
|
|
58
|
+
end,
|
|
59
|
+
start: /* @__PURE__ */ new Date(end.getTime() - (bucketCount - 1) * SIX_HOUR_MS)
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/** Fill a fixed UTC day window from a sparse map. */
|
|
63
|
+
function fillUtcDays(args) {
|
|
64
|
+
const { end, start } = trailingUtcDayWindow(args.nowMs, args.count);
|
|
65
|
+
const items = [];
|
|
66
|
+
for (const cursor = new Date(start); cursor.getTime() <= end.getTime(); cursor.setUTCDate(cursor.getUTCDate() + 1)) {
|
|
67
|
+
const date = utcDayKey(cursor);
|
|
68
|
+
items.push(args.rows.get(date) ?? args.empty(date));
|
|
69
|
+
}
|
|
70
|
+
return items;
|
|
71
|
+
}
|
|
72
|
+
/** Fill a fixed UTC hour window from a sparse map. */
|
|
73
|
+
function fillUtcHours(args) {
|
|
74
|
+
const hourCount = args.count ?? 24;
|
|
75
|
+
const { end, start } = trailingUtcHourWindow(args.nowMs, hourCount);
|
|
76
|
+
const items = [];
|
|
77
|
+
for (const cursor = new Date(start); cursor.getTime() <= end.getTime(); cursor.setTime(cursor.getTime() + HOUR_MS)) {
|
|
78
|
+
const date = utcHourKey(cursor);
|
|
79
|
+
items.push(args.rows.get(date) ?? args.empty(date));
|
|
80
|
+
}
|
|
81
|
+
return items;
|
|
82
|
+
}
|
|
83
|
+
/** Fill a fixed UTC 6-hour window from a map of known rows. */
|
|
84
|
+
function fillUtcSixHours(args) {
|
|
85
|
+
const bucketCount = args.count ?? 28;
|
|
86
|
+
const { end, start } = trailingUtcSixHourWindow(args.nowMs, bucketCount);
|
|
87
|
+
const items = [];
|
|
88
|
+
for (const cursor = new Date(start); cursor.getTime() <= end.getTime(); cursor.setTime(cursor.getTime() + SIX_HOUR_MS)) {
|
|
89
|
+
const date = utcSixHourKey(cursor);
|
|
90
|
+
items.push(args.rows.get(date) ?? args.empty(date));
|
|
91
|
+
}
|
|
92
|
+
return items;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Sum hour rows into trailing 6-hour buckets.
|
|
96
|
+
* Adds number fields. Keeps other fields from the first row in the bucket.
|
|
97
|
+
* Sets `date` to the 6-hour bucket key.
|
|
98
|
+
*/
|
|
99
|
+
function sumUtcHoursIntoSixHours(args) {
|
|
100
|
+
const bucketCount = args.count ?? 28;
|
|
101
|
+
const bySix = /* @__PURE__ */ new Map();
|
|
102
|
+
for (const hour of args.hours) {
|
|
103
|
+
const key = utcSixHourKey(Date.parse(`${hour.date}:00:00.000Z`));
|
|
104
|
+
const next = {
|
|
105
|
+
...bySix.get(key) ?? args.empty(key),
|
|
106
|
+
date: key
|
|
107
|
+
};
|
|
108
|
+
for (const [field, value] of Object.entries(hour)) {
|
|
109
|
+
if (field === "date") continue;
|
|
110
|
+
if (typeof value === "number") {
|
|
111
|
+
const prior = next[field];
|
|
112
|
+
next[field] = (typeof prior === "number" ? prior : 0) + value;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
bySix.set(key, next);
|
|
116
|
+
}
|
|
117
|
+
return fillUtcSixHours({
|
|
118
|
+
count: bucketCount,
|
|
119
|
+
empty: args.empty,
|
|
120
|
+
nowMs: args.nowMs,
|
|
121
|
+
rows: bySix
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
// src/chat/agent-invocations/statuses.ts
|
|
125
|
+
const AGENT_INVOCATION_STATUSES = [
|
|
126
|
+
"pending",
|
|
127
|
+
"running",
|
|
128
|
+
"paused",
|
|
129
|
+
"blocked",
|
|
130
|
+
"completed",
|
|
131
|
+
"failed",
|
|
132
|
+
"stopped"
|
|
133
|
+
];
|
|
134
|
+
const AGENT_INVOCATION_MAILBOX_STATUSES = ["pending", "appended"];
|
|
135
|
+
/**
|
|
136
|
+
* Parent mailbox append state. Only terminal invocations have a value.
|
|
137
|
+
* `replaced` means the child still had open work, so a later agent invocation
|
|
138
|
+
* on the same child sends the result instead. `skipped` means a stop ended
|
|
139
|
+
* the work, so the result is never used. `steered` means the agent invocation
|
|
140
|
+
* that was running read the input as a steering message, so its result covers
|
|
141
|
+
* this one.
|
|
142
|
+
*/
|
|
143
|
+
const AGENT_INVOCATION_PARENT_MAILBOX_STATUSES = [
|
|
144
|
+
"pending",
|
|
145
|
+
"appended",
|
|
146
|
+
"replaced",
|
|
147
|
+
"skipped",
|
|
148
|
+
"steered"
|
|
149
|
+
];
|
|
150
|
+
export { HOUR_MS as a, fillUtcSixHours as c, trailingUtcDayWindow as d, trailingUtcHourWindow as f, DAY_MS as i, startOfUtcHour as l, utcHourKey as m, AGENT_INVOCATION_PARENT_MAILBOX_STATUSES as n, fillUtcDays as o, trailingUtcSixHourWindow as p, AGENT_INVOCATION_STATUSES as r, fillUtcHours as s, AGENT_INVOCATION_MAILBOX_STATUSES as t, sumUtcHoursIntoSixHours as u };
|
|
@@ -0,0 +1,492 @@
|
|
|
1
|
+
import { B as juniorAttachments, H as juniorConversations, V as juniorMessageAttachments } from "./executor-SCub-fAq.js";
|
|
2
|
+
import { Fn as conversationDetailTouch, Ln as touchConversationDetail } from "./db-LAyyX8tk.js";
|
|
3
|
+
import { t as mapBounded } from "./fan-out-Cod-yEHG.js";
|
|
4
|
+
import { ht as bufferBody } from "./config-kXB2Y5tl.js";
|
|
5
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
6
|
+
import { and, asc, eq, inArray, isNotNull, isNull, lte, notExists, or } from "drizzle-orm";
|
|
7
|
+
// src/chat/attachments/source.ts
|
|
8
|
+
/** The source deleted the file, or Junior can no longer read it. */
|
|
9
|
+
var AttachmentSourceUnavailableError = class extends Error {
|
|
10
|
+
constructor(message, options) {
|
|
11
|
+
super(message, options);
|
|
12
|
+
this.name = "AttachmentSourceUnavailableError";
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
// src/chat/attachments/store.ts
|
|
16
|
+
const DAY_MS = 864e5;
|
|
17
|
+
const ATTACHMENT_GC_BATCH_LIMIT = 200;
|
|
18
|
+
/** Time that Junior keeps attachment bytes after the file is stored. */
|
|
19
|
+
const ATTACHMENT_RETENTION_MS = 30 * DAY_MS;
|
|
20
|
+
/** Only `file` rows: a `url` row has no storage or file fields. */
|
|
21
|
+
const isFileAttachment = eq(juniorAttachments.kind, "file");
|
|
22
|
+
/**
|
|
23
|
+
* The storage and file fields of a `file` row. Every `file` row has them, so
|
|
24
|
+
* a missing one is a broken row.
|
|
25
|
+
*/
|
|
26
|
+
function fileFields(row) {
|
|
27
|
+
const { bytes, contentType, storageProvider, title } = row;
|
|
28
|
+
if (bytes === null || contentType === null || storageProvider === null || title === null) throw new Error(`Attachment ${row.id} has no file fields`);
|
|
29
|
+
return {
|
|
30
|
+
bytes,
|
|
31
|
+
contentType,
|
|
32
|
+
filename: title,
|
|
33
|
+
storageProvider
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function attachmentIdentity(file) {
|
|
37
|
+
if ("data" in file) return file.source ?? { sha256: createHash("sha256").update(file.data).digest("hex") };
|
|
38
|
+
return file.source;
|
|
39
|
+
}
|
|
40
|
+
/** Stable conversation-owned id for one file and its presentation metadata. */
|
|
41
|
+
function attachmentId(args) {
|
|
42
|
+
const identity = "sha256" in args.identity ? [
|
|
43
|
+
args.conversationId,
|
|
44
|
+
args.storageProvider,
|
|
45
|
+
args.identity.sha256,
|
|
46
|
+
args.filename,
|
|
47
|
+
args.contentType
|
|
48
|
+
] : [
|
|
49
|
+
args.conversationId,
|
|
50
|
+
args.storageProvider,
|
|
51
|
+
args.filename,
|
|
52
|
+
args.contentType,
|
|
53
|
+
args.identity.provider,
|
|
54
|
+
args.identity.id
|
|
55
|
+
];
|
|
56
|
+
return createHash("sha256").update(identity.join("\0")).digest("hex");
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Object key for one write attempt.
|
|
60
|
+
*
|
|
61
|
+
* Attachment ids are content-stable. Object keys are not: each put gets a fresh
|
|
62
|
+
* key so a failed-write cleanup or GC delete cannot remove another writer's
|
|
63
|
+
* durable blob.
|
|
64
|
+
*/
|
|
65
|
+
function attachmentKey(args) {
|
|
66
|
+
return `conversations/${args.conversationId}/attachments/${args.attachmentId}/${args.writeId}/${args.filename}`;
|
|
67
|
+
}
|
|
68
|
+
/** A source whose bytes do not match its declared size. A retry cannot fix it. */
|
|
69
|
+
var AttachmentSizeError = class extends Error {
|
|
70
|
+
constructor(bytes, expected, options) {
|
|
71
|
+
super(`Attachment body does not match its declared ${expected} bytes (read ${bytes})`, options);
|
|
72
|
+
this.name = "AttachmentSizeError";
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
/** Tries for one object write, each with a freshly opened body. */
|
|
76
|
+
const WRITE_ATTEMPTS = 3;
|
|
77
|
+
async function bestEffortDelete(storage, key) {
|
|
78
|
+
if (key === null) return;
|
|
79
|
+
try {
|
|
80
|
+
await storage.delete([key]);
|
|
81
|
+
} catch {}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Stream one file to a fresh key, and return its size and digest.
|
|
85
|
+
*
|
|
86
|
+
* The digest is taken while the bytes stream, so the body is never held
|
|
87
|
+
* whole. A body that does not match its declared size is not kept.
|
|
88
|
+
*/
|
|
89
|
+
async function writeAttachmentObject(args) {
|
|
90
|
+
for (let attempt = 1;; attempt += 1) try {
|
|
91
|
+
return await writeAttachmentObjectOnce(args);
|
|
92
|
+
} catch (error) {
|
|
93
|
+
if (error instanceof AttachmentSizeError || error instanceof AttachmentSourceUnavailableError || attempt >= WRITE_ATTEMPTS) {
|
|
94
|
+
await bestEffortDelete(args.storage, args.key);
|
|
95
|
+
throw error;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
async function writeAttachmentObjectOnce(args) {
|
|
100
|
+
const { file } = args;
|
|
101
|
+
const expected = "data" in file ? file.data.byteLength : file.bytes;
|
|
102
|
+
const source = "data" in file ? bufferBody(file.data) : await file.open();
|
|
103
|
+
const hash = createHash("sha256");
|
|
104
|
+
let bytes = 0;
|
|
105
|
+
const body = source.pipeThrough(new TransformStream({ transform(chunk, controller) {
|
|
106
|
+
bytes += chunk.byteLength;
|
|
107
|
+
if (bytes > expected) throw new AttachmentSizeError(bytes, expected);
|
|
108
|
+
hash.update(chunk);
|
|
109
|
+
controller.enqueue(chunk);
|
|
110
|
+
} }));
|
|
111
|
+
try {
|
|
112
|
+
await args.storage.put({
|
|
113
|
+
body,
|
|
114
|
+
bytes: expected,
|
|
115
|
+
contentType: file.mimeType,
|
|
116
|
+
key: args.key
|
|
117
|
+
});
|
|
118
|
+
} catch (error) {
|
|
119
|
+
if (bytes > expected) throw new AttachmentSizeError(bytes, expected, { cause: error });
|
|
120
|
+
throw error;
|
|
121
|
+
}
|
|
122
|
+
if (bytes !== expected) throw new AttachmentSizeError(bytes, expected);
|
|
123
|
+
return {
|
|
124
|
+
bytes,
|
|
125
|
+
sha256: hash.digest("hex")
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
async function isConversationPurged(db, conversationId) {
|
|
129
|
+
const [row] = await db.db().select({ transcriptPurgedAt: juniorConversations.transcriptPurgedAt }).from(juniorConversations).where(eq(juniorConversations.conversationId, conversationId));
|
|
130
|
+
return row?.transcriptPurgedAt != null;
|
|
131
|
+
}
|
|
132
|
+
function purgedConversationError(conversationId) {
|
|
133
|
+
return /* @__PURE__ */ new Error(`Cannot store attachment for purged conversation ${conversationId}`);
|
|
134
|
+
}
|
|
135
|
+
async function assertConversationNotPurged(db, conversationId) {
|
|
136
|
+
if (await isConversationPurged(db, conversationId)) throw purgedConversationError(conversationId);
|
|
137
|
+
}
|
|
138
|
+
/** Mark a just-written row for deletion and drop its unused object. */
|
|
139
|
+
async function rejectPurgedAttachmentWrite(args) {
|
|
140
|
+
const db = args.db.db();
|
|
141
|
+
await db.with(conversationDetailTouch(db, [args.conversationId])).update(juniorAttachments).set({ deleteRequestedAt: args.now }).where(eq(juniorAttachments.id, args.id));
|
|
142
|
+
await bestEffortDelete(args.storage, args.storageKey);
|
|
143
|
+
throw purgedConversationError(args.conversationId);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Return another writer's live row only when the conversation is still active.
|
|
147
|
+
*
|
|
148
|
+
* The caller already wrote a unique object for this attempt. Delete that unused
|
|
149
|
+
* object either way. If the conversation is purged, do not report success for a
|
|
150
|
+
* row that purge/GC is about to remove.
|
|
151
|
+
*/
|
|
152
|
+
async function reuseLiveAttachment(args) {
|
|
153
|
+
await bestEffortDelete(args.storage, args.storageKey);
|
|
154
|
+
await assertConversationNotPurged(args.db, args.conversationId);
|
|
155
|
+
return { id: args.id };
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Persist one conversation-owned file.
|
|
159
|
+
*
|
|
160
|
+
* Identity is stable under the conversation: it comes from the file's source
|
|
161
|
+
* when it has one, and from its digest otherwise. Object storage is written
|
|
162
|
+
* first under a unique key; the SQL row is created only after that write
|
|
163
|
+
* succeeds. A row means the attachment is durable. Retries reuse an existing
|
|
164
|
+
* live row. A purge-marked row is revived by writing a new object and pointing
|
|
165
|
+
* the row at it, unless the conversation is still purged. If the SQL write
|
|
166
|
+
* fails after object storage accepts the bytes, this path deletes that unique
|
|
167
|
+
* object. A process crash between those steps can leave an unreferenced unique
|
|
168
|
+
* object with no SQL row.
|
|
169
|
+
*/
|
|
170
|
+
async function storeAttachment(args) {
|
|
171
|
+
const now = new Date(args.nowMs ?? Date.now());
|
|
172
|
+
const identity = attachmentIdentity(args.file);
|
|
173
|
+
const source = args.file.source;
|
|
174
|
+
const id = attachmentId({
|
|
175
|
+
conversationId: args.conversationId,
|
|
176
|
+
contentType: args.file.mimeType,
|
|
177
|
+
filename: args.file.filename,
|
|
178
|
+
identity,
|
|
179
|
+
storageProvider: args.storage.provider
|
|
180
|
+
});
|
|
181
|
+
const [existing] = await args.db.db().select({
|
|
182
|
+
contentType: juniorAttachments.contentType,
|
|
183
|
+
conversationId: juniorAttachments.conversationId,
|
|
184
|
+
deleteRequestedAt: juniorAttachments.deleteRequestedAt,
|
|
185
|
+
filename: juniorAttachments.title,
|
|
186
|
+
id: juniorAttachments.id,
|
|
187
|
+
provider: juniorAttachments.provider,
|
|
188
|
+
providerId: juniorAttachments.providerId,
|
|
189
|
+
storageProvider: juniorAttachments.storageProvider,
|
|
190
|
+
sha256: juniorAttachments.sha256,
|
|
191
|
+
storageKey: juniorAttachments.storageKey
|
|
192
|
+
}).from(juniorAttachments).where(eq(juniorAttachments.id, id));
|
|
193
|
+
if (existing) {
|
|
194
|
+
if (existing.contentType !== args.file.mimeType || existing.conversationId !== args.conversationId || existing.filename !== args.file.filename || existing.provider !== (source?.provider ?? null) || existing.providerId !== (source?.id ?? null) || existing.storageProvider !== args.storage.provider || "sha256" in identity && existing.sha256 !== identity.sha256) throw new Error(`Attachment write conflicts with ${existing.id}`);
|
|
195
|
+
if (existing.deleteRequestedAt === null) {
|
|
196
|
+
await assertConversationNotPurged(args.db, args.conversationId);
|
|
197
|
+
return { id: existing.id };
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
await assertConversationNotPurged(args.db, args.conversationId);
|
|
201
|
+
const { file } = args;
|
|
202
|
+
let storageKey = null;
|
|
203
|
+
let written;
|
|
204
|
+
if ("deferred" in file) written = {
|
|
205
|
+
bytes: file.bytes,
|
|
206
|
+
sha256: null
|
|
207
|
+
};
|
|
208
|
+
else {
|
|
209
|
+
storageKey = attachmentKey({
|
|
210
|
+
attachmentId: id,
|
|
211
|
+
conversationId: args.conversationId,
|
|
212
|
+
filename: file.filename,
|
|
213
|
+
writeId: randomUUID()
|
|
214
|
+
});
|
|
215
|
+
written = await writeAttachmentObject({
|
|
216
|
+
file,
|
|
217
|
+
key: storageKey,
|
|
218
|
+
storage: args.storage
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
if (existing?.deleteRequestedAt != null) {
|
|
222
|
+
if (await isConversationPurged(args.db, args.conversationId)) {
|
|
223
|
+
await bestEffortDelete(args.storage, storageKey);
|
|
224
|
+
throw purgedConversationError(args.conversationId);
|
|
225
|
+
}
|
|
226
|
+
const oldStorageKey = existing.storageKey;
|
|
227
|
+
if ((await args.db.db().with(conversationDetailTouch(args.db.db(), [args.conversationId])).update(juniorAttachments).set({
|
|
228
|
+
...written,
|
|
229
|
+
deleteRequestedAt: null,
|
|
230
|
+
storageKey
|
|
231
|
+
}).where(and(eq(juniorAttachments.id, id), isNotNull(juniorAttachments.deleteRequestedAt))).returning({ id: juniorAttachments.id })).length > 0) {
|
|
232
|
+
if (await isConversationPurged(args.db, args.conversationId)) await rejectPurgedAttachmentWrite({
|
|
233
|
+
conversationId: args.conversationId,
|
|
234
|
+
db: args.db,
|
|
235
|
+
id,
|
|
236
|
+
now,
|
|
237
|
+
storage: args.storage,
|
|
238
|
+
storageKey
|
|
239
|
+
});
|
|
240
|
+
if (oldStorageKey !== storageKey) await bestEffortDelete(args.storage, oldStorageKey);
|
|
241
|
+
return { id };
|
|
242
|
+
}
|
|
243
|
+
const [afterRace] = await args.db.db().select({
|
|
244
|
+
deleteRequestedAt: juniorAttachments.deleteRequestedAt,
|
|
245
|
+
id: juniorAttachments.id,
|
|
246
|
+
storageKey: juniorAttachments.storageKey
|
|
247
|
+
}).from(juniorAttachments).where(eq(juniorAttachments.id, id));
|
|
248
|
+
if (afterRace?.deleteRequestedAt === null) return reuseLiveAttachment({
|
|
249
|
+
conversationId: args.conversationId,
|
|
250
|
+
db: args.db,
|
|
251
|
+
id: afterRace.id,
|
|
252
|
+
storage: args.storage,
|
|
253
|
+
storageKey
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
let inserted = [];
|
|
257
|
+
try {
|
|
258
|
+
inserted = await args.db.db().with(conversationDetailTouch(args.db.db(), [args.conversationId])).insert(juniorAttachments).values({
|
|
259
|
+
id,
|
|
260
|
+
conversationId: args.conversationId,
|
|
261
|
+
storageProvider: args.storage.provider,
|
|
262
|
+
storageKey,
|
|
263
|
+
provider: source?.provider,
|
|
264
|
+
providerId: source?.id,
|
|
265
|
+
title: args.file.filename,
|
|
266
|
+
contentType: args.file.mimeType,
|
|
267
|
+
...written,
|
|
268
|
+
createdAt: now
|
|
269
|
+
}).onConflictDoNothing({ target: juniorAttachments.id }).returning({ id: juniorAttachments.id });
|
|
270
|
+
} catch (error) {
|
|
271
|
+
await bestEffortDelete(args.storage, storageKey);
|
|
272
|
+
throw error;
|
|
273
|
+
}
|
|
274
|
+
if (inserted.length > 0) {
|
|
275
|
+
if (await isConversationPurged(args.db, args.conversationId)) await rejectPurgedAttachmentWrite({
|
|
276
|
+
conversationId: args.conversationId,
|
|
277
|
+
db: args.db,
|
|
278
|
+
id,
|
|
279
|
+
now,
|
|
280
|
+
storage: args.storage,
|
|
281
|
+
storageKey
|
|
282
|
+
});
|
|
283
|
+
return { id };
|
|
284
|
+
}
|
|
285
|
+
const [row] = await args.db.db().select({
|
|
286
|
+
deleteRequestedAt: juniorAttachments.deleteRequestedAt,
|
|
287
|
+
id: juniorAttachments.id,
|
|
288
|
+
storageKey: juniorAttachments.storageKey
|
|
289
|
+
}).from(juniorAttachments).where(eq(juniorAttachments.id, id));
|
|
290
|
+
if (!row) {
|
|
291
|
+
await bestEffortDelete(args.storage, storageKey);
|
|
292
|
+
throw new Error(`Attachment row missing after write for ${id}`);
|
|
293
|
+
}
|
|
294
|
+
if (row.deleteRequestedAt === null) return reuseLiveAttachment({
|
|
295
|
+
conversationId: args.conversationId,
|
|
296
|
+
db: args.db,
|
|
297
|
+
id: row.id,
|
|
298
|
+
storage: args.storage,
|
|
299
|
+
storageKey
|
|
300
|
+
});
|
|
301
|
+
if (await isConversationPurged(args.db, args.conversationId)) {
|
|
302
|
+
await bestEffortDelete(args.storage, storageKey);
|
|
303
|
+
throw purgedConversationError(args.conversationId);
|
|
304
|
+
}
|
|
305
|
+
if ((await args.db.db().with(conversationDetailTouch(args.db.db(), [args.conversationId])).update(juniorAttachments).set({
|
|
306
|
+
...written,
|
|
307
|
+
deleteRequestedAt: null,
|
|
308
|
+
storageKey
|
|
309
|
+
}).where(and(eq(juniorAttachments.id, id), isNotNull(juniorAttachments.deleteRequestedAt))).returning({ id: juniorAttachments.id })).length > 0) {
|
|
310
|
+
if (await isConversationPurged(args.db, args.conversationId)) await rejectPurgedAttachmentWrite({
|
|
311
|
+
conversationId: args.conversationId,
|
|
312
|
+
db: args.db,
|
|
313
|
+
id,
|
|
314
|
+
now,
|
|
315
|
+
storage: args.storage,
|
|
316
|
+
storageKey
|
|
317
|
+
});
|
|
318
|
+
if (row.storageKey !== storageKey) await bestEffortDelete(args.storage, row.storageKey);
|
|
319
|
+
return { id };
|
|
320
|
+
}
|
|
321
|
+
const [afterRace] = await args.db.db().select({
|
|
322
|
+
deleteRequestedAt: juniorAttachments.deleteRequestedAt,
|
|
323
|
+
id: juniorAttachments.id
|
|
324
|
+
}).from(juniorAttachments).where(eq(juniorAttachments.id, id));
|
|
325
|
+
if (afterRace?.deleteRequestedAt === null) return reuseLiveAttachment({
|
|
326
|
+
conversationId: args.conversationId,
|
|
327
|
+
db: args.db,
|
|
328
|
+
id: afterRace.id,
|
|
329
|
+
storage: args.storage,
|
|
330
|
+
storageKey
|
|
331
|
+
});
|
|
332
|
+
await bestEffortDelete(args.storage, storageKey);
|
|
333
|
+
throw new Error(`Attachment ${id} disappeared while reviving`);
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Record an object that a browser already wrote, under the upload's id.
|
|
337
|
+
*
|
|
338
|
+
* The row takes over the object key. A retry with the same id keeps the
|
|
339
|
+
* first row. The caller checked the object's size, type, and digest.
|
|
340
|
+
*/
|
|
341
|
+
async function recordUploadedAttachment(args) {
|
|
342
|
+
const { attachment } = args;
|
|
343
|
+
const now = new Date(args.nowMs);
|
|
344
|
+
await assertConversationNotPurged(args.db, attachment.conversationId);
|
|
345
|
+
if ((await args.db.db().with(conversationDetailTouch(args.db.db(), [attachment.conversationId])).insert(juniorAttachments).values({
|
|
346
|
+
bytes: attachment.bytes,
|
|
347
|
+
contentType: attachment.contentType,
|
|
348
|
+
conversationId: attachment.conversationId,
|
|
349
|
+
createdAt: now,
|
|
350
|
+
id: attachment.id,
|
|
351
|
+
sha256: attachment.sha256,
|
|
352
|
+
storageKey: attachment.storageKey,
|
|
353
|
+
storageProvider: args.storage.provider,
|
|
354
|
+
title: attachment.filename
|
|
355
|
+
}).onConflictDoNothing({ target: juniorAttachments.id }).returning({ id: juniorAttachments.id })).length > 0 && await isConversationPurged(args.db, attachment.conversationId)) await rejectPurgedAttachmentWrite({
|
|
356
|
+
conversationId: attachment.conversationId,
|
|
357
|
+
db: args.db,
|
|
358
|
+
id: attachment.id,
|
|
359
|
+
now,
|
|
360
|
+
storage: args.storage,
|
|
361
|
+
storageKey: attachment.storageKey
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Store a file that Junior recorded without its bytes, on its first read.
|
|
366
|
+
*
|
|
367
|
+
* The bytes stream from the source to a fresh key. The row then takes that
|
|
368
|
+
* key only while it is still unstored, live, and in a conversation that is
|
|
369
|
+
* not purged. Purge marks rows in the transaction that purges the
|
|
370
|
+
* conversation, so this update cannot revive a purged row. When the update
|
|
371
|
+
* misses, the new object is deleted: return the row that another writer
|
|
372
|
+
* stored, or null when the row is no longer live.
|
|
373
|
+
*/
|
|
374
|
+
async function storeDeferredAttachment(args) {
|
|
375
|
+
const { attachment, storage } = args;
|
|
376
|
+
const storageKey = attachmentKey({
|
|
377
|
+
attachmentId: attachment.id,
|
|
378
|
+
conversationId: attachment.conversationId,
|
|
379
|
+
filename: attachment.filename,
|
|
380
|
+
writeId: randomUUID()
|
|
381
|
+
});
|
|
382
|
+
const written = await writeAttachmentObject({
|
|
383
|
+
file: {
|
|
384
|
+
bytes: args.file.bytes,
|
|
385
|
+
filename: attachment.filename,
|
|
386
|
+
mimeType: attachment.contentType,
|
|
387
|
+
open: args.file.open,
|
|
388
|
+
source: attachment.source
|
|
389
|
+
},
|
|
390
|
+
key: storageKey,
|
|
391
|
+
storage
|
|
392
|
+
});
|
|
393
|
+
const db = args.db.db();
|
|
394
|
+
if ((await db.with(conversationDetailTouch(db, [attachment.conversationId])).update(juniorAttachments).set({
|
|
395
|
+
...written,
|
|
396
|
+
storageKey
|
|
397
|
+
}).where(and(eq(juniorAttachments.id, attachment.id), eq(juniorAttachments.conversationId, attachment.conversationId), eq(juniorAttachments.storageProvider, storage.provider), isNull(juniorAttachments.storageKey), isNull(juniorAttachments.deleteRequestedAt), notExists(db.select({ id: juniorConversations.conversationId }).from(juniorConversations).where(and(eq(juniorConversations.conversationId, attachment.conversationId), isNotNull(juniorConversations.transcriptPurgedAt)))))).returning({ id: juniorAttachments.id })).length > 0) return {
|
|
398
|
+
...attachment,
|
|
399
|
+
bytes: written.bytes,
|
|
400
|
+
storageKey
|
|
401
|
+
};
|
|
402
|
+
await bestEffortDelete(storage, storageKey);
|
|
403
|
+
const current = await readLiveAttachment({
|
|
404
|
+
attachmentId: attachment.id,
|
|
405
|
+
conversationId: attachment.conversationId,
|
|
406
|
+
db: args.db
|
|
407
|
+
});
|
|
408
|
+
return current?.storageKey ? {
|
|
409
|
+
...current,
|
|
410
|
+
storageKey: current.storageKey
|
|
411
|
+
} : null;
|
|
412
|
+
}
|
|
413
|
+
/** Load one live attachment owned by a conversation. */
|
|
414
|
+
async function readLiveAttachment(args) {
|
|
415
|
+
const [row] = await args.db.db().select({
|
|
416
|
+
bytes: juniorAttachments.bytes,
|
|
417
|
+
contentType: juniorAttachments.contentType,
|
|
418
|
+
conversationId: juniorAttachments.conversationId,
|
|
419
|
+
deleteRequestedAt: juniorAttachments.deleteRequestedAt,
|
|
420
|
+
id: juniorAttachments.id,
|
|
421
|
+
provider: juniorAttachments.provider,
|
|
422
|
+
providerId: juniorAttachments.providerId,
|
|
423
|
+
storageProvider: juniorAttachments.storageProvider,
|
|
424
|
+
storageKey: juniorAttachments.storageKey,
|
|
425
|
+
title: juniorAttachments.title
|
|
426
|
+
}).from(juniorAttachments).where(and(eq(juniorAttachments.id, args.attachmentId), isFileAttachment, eq(juniorAttachments.conversationId, args.conversationId), isNull(juniorAttachments.deleteRequestedAt)));
|
|
427
|
+
if (!row) return null;
|
|
428
|
+
if (await isConversationPurged(args.db, args.conversationId)) return null;
|
|
429
|
+
return {
|
|
430
|
+
...fileFields(row),
|
|
431
|
+
conversationId: row.conversationId,
|
|
432
|
+
id: row.id,
|
|
433
|
+
source: row.provider && row.providerId ? {
|
|
434
|
+
id: row.providerId,
|
|
435
|
+
provider: row.provider
|
|
436
|
+
} : null,
|
|
437
|
+
storageKey: row.storageKey
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
/** Persist many conversation-owned files. */
|
|
441
|
+
async function storeAttachments(args) {
|
|
442
|
+
return await mapBounded(args.files, (file) => storeAttachment({
|
|
443
|
+
conversationId: args.conversationId,
|
|
444
|
+
db: args.db,
|
|
445
|
+
file: {
|
|
446
|
+
data: file.data,
|
|
447
|
+
filename: file.filename,
|
|
448
|
+
mimeType: file.mimeType
|
|
449
|
+
},
|
|
450
|
+
...args.nowMs !== void 0 ? { nowMs: args.nowMs } : void 0,
|
|
451
|
+
storage: args.storage
|
|
452
|
+
}));
|
|
453
|
+
}
|
|
454
|
+
/** Mark attachments unavailable when their conversation content is purged. */
|
|
455
|
+
async function requestAttachmentDeletion(db, conversationIds, nowMs) {
|
|
456
|
+
if (conversationIds.length === 0) return;
|
|
457
|
+
await db.db().with(conversationDetailTouch(db.db(), conversationIds)).update(juniorAttachments).set({ deleteRequestedAt: new Date(nowMs) }).where(and(inArray(juniorAttachments.conversationId, conversationIds), isNull(juniorAttachments.deleteRequestedAt)));
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Delete purged attachments in one bounded batch.
|
|
461
|
+
*
|
|
462
|
+
* Eligible files are 30 days old, explicitly purge-marked, or owned by a
|
|
463
|
+
* conversation whose transcript is already purged. A `url` row has no bytes,
|
|
464
|
+
* so it lives as long as its message: it is eligible only when purge-marked
|
|
465
|
+
* or purged. Delete object keys first, then remove rows that are still
|
|
466
|
+
* eligible so a failed blob delete remains retryable.
|
|
467
|
+
*/
|
|
468
|
+
async function collectAttachmentGarbage(args) {
|
|
469
|
+
const expiresBefore = /* @__PURE__ */ new Date(args.nowMs - ATTACHMENT_RETENTION_MS);
|
|
470
|
+
const purged = or(isNotNull(juniorAttachments.deleteRequestedAt), isNotNull(juniorConversations.transcriptPurgedAt));
|
|
471
|
+
const eligible = or(lte(juniorAttachments.createdAt, expiresBefore), purged);
|
|
472
|
+
const isUrl = eq(juniorAttachments.kind, "url");
|
|
473
|
+
const rows = await args.db.db().select({
|
|
474
|
+
id: juniorAttachments.id,
|
|
475
|
+
storageKey: juniorAttachments.storageKey
|
|
476
|
+
}).from(juniorAttachments).innerJoin(juniorConversations, eq(juniorAttachments.conversationId, juniorConversations.conversationId)).where(or(and(eq(juniorAttachments.storageProvider, args.storage.provider), eligible), and(isUrl, purged))).orderBy(asc(juniorAttachments.createdAt), asc(juniorAttachments.id)).limit(args.limit ?? ATTACHMENT_GC_BATCH_LIMIT);
|
|
477
|
+
if (rows.length === 0) return { deleted: 0 };
|
|
478
|
+
const keys = rows.flatMap((row) => row.storageKey ? [row.storageKey] : []);
|
|
479
|
+
if (keys.length > 0) await args.storage.delete(keys);
|
|
480
|
+
const stillEligible = await args.db.db().select({ id: juniorAttachments.id }).from(juniorAttachments).innerJoin(juniorConversations, eq(juniorAttachments.conversationId, juniorConversations.conversationId)).where(and(inArray(juniorAttachments.id, rows.map((row) => row.id)), or(and(isFileAttachment, eligible), and(isUrl, purged))));
|
|
481
|
+
if (stillEligible.length === 0) return { deleted: 0 };
|
|
482
|
+
const removed = await args.db.db().delete(juniorAttachments).where(inArray(juniorAttachments.id, stillEligible.map((row) => row.id))).returning({
|
|
483
|
+
conversationId: juniorAttachments.conversationId,
|
|
484
|
+
id: juniorAttachments.id,
|
|
485
|
+
kind: juniorAttachments.kind
|
|
486
|
+
});
|
|
487
|
+
const urls = removed.filter((row) => row.kind === "url");
|
|
488
|
+
if (urls.length > 0) await args.db.db().delete(juniorMessageAttachments).where(and(inArray(juniorMessageAttachments.conversationId, urls.map((row) => row.conversationId)), inArray(juniorMessageAttachments.attachmentId, urls.map((row) => row.id))));
|
|
489
|
+
await touchConversationDetail(args.db.db(), removed.map((row) => row.conversationId));
|
|
490
|
+
return { deleted: removed.length };
|
|
491
|
+
}
|
|
492
|
+
export { recordUploadedAttachment as a, storeAttachments as c, readLiveAttachment as i, storeDeferredAttachment as l, fileFields as n, requestAttachmentDeletion as o, isFileAttachment as r, storeAttachment as s, collectAttachmentGarbage as t, AttachmentSourceUnavailableError as u };
|