Repository navigation
Expand file tree
/
Copy pathAttachmentClaims.ts
More file actions
172 lines (165 loc) · 6.57 KB
/
Copy pathAttachmentClaims.ts
File metadata and controls
172 lines (165 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import * as FileSystem from "effect/FileSystem";
import {
ChatAttachmentId,
getProviderAttachmentLimitError,
type ChatAttachment,
} from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
import { isSvgAttachment } from "@t3tools/shared/image";
import {
parseThreadSegmentFromAttachmentId,
PENDING_ATTACHMENT_THREAD_SEGMENT,
planAttachmentClaim,
resolveAttachmentPath,
} from "../attachmentStore.ts";
import * as ServerConfig from "../config.ts";
import * as DocumentText from "../provider/DocumentText.ts";
export class AttachmentClaimError extends Schema.TaggedError<AttachmentClaimError>()(
"AttachmentClaimError",
{
message: Schema.String,
cause: Schema.optional(Schema.Defect()),
},
) {}
export const validateAttachmentLimits = Effect.fn("AttachmentClaims.validateAttachmentLimits")(
function* (attachments: ReadonlyArray<ChatAttachment>) {
const error = getProviderAttachmentLimitError(attachments);
if (error) return yield* new AttachmentClaimError({ message: error });
},
);
export interface ClaimedAttachments {
readonly attachments: ReadonlyArray<ChatAttachment>;
readonly claimedPaths: ReadonlyArray<string>;
}
export function attachmentIsPendingUpload(attachment: ChatAttachment): boolean {
return parseThreadSegmentFromAttachmentId(attachment.id) === PENDING_ATTACHMENT_THREAD_SEGMENT;
}
/** Remove partial claims only before dispatch, or after proving they were not accepted. */
export const releaseClaimedAttachments = Effect.fn("AttachmentClaims.releaseClaimedAttachments")(
function* (claimedPaths: ReadonlyArray<string>) {
if (claimedPaths.length === 0) return;
const fileSystem = yield* FileSystem.FileSystem;
yield* Effect.forEach(
claimedPaths,
(path) =>
fileSystem
.remove(path)
.pipe(
Effect.ignore,
Effect.andThen(fileSystem.remove(`${path}.txt`, { force: true }).pipe(Effect.ignore)),
),
{
concurrency: 1,
discard: true,
},
).pipe(Effect.uninterruptible);
},
);
/** Copies preserve the pending retry source after failed bootstrap; the pending sweep reclaims it. */
export const claimPendingAttachments = Effect.fn("AttachmentClaims.claimPendingAttachments")(
function* (input: {
readonly threadId: string;
readonly attachments: ReadonlyArray<ChatAttachment>;
}) {
yield* validateAttachmentLimits(input.attachments);
if (
new Set(input.attachments.map((attachment) => attachment.id)).size !==
input.attachments.length
) {
return yield* new AttachmentClaimError({
message: "Duplicate attachment ids are not allowed.",
});
}
if (!input.attachments.some(attachmentIsPendingUpload)) {
return { attachments: input.attachments, claimedPaths: [] } satisfies ClaimedAttachments;
}
const serverConfig = yield* ServerConfig.ServerConfig;
const fileSystem = yield* FileSystem.FileSystem;
const claimedPaths: string[] = [];
const documents = yield* DocumentText.DocumentText;
const attachments = yield* Effect.forEach(
input.attachments,
(attachment) =>
Effect.gen(function* () {
if (!attachmentIsPendingUpload(attachment)) {
return attachment;
}
const claim = planAttachmentClaim({
attachmentsDir: serverConfig.attachmentsDir,
threadId: input.threadId,
attachmentId: attachment.id,
});
if (!claim.ok) {
return yield* new AttachmentClaimError({
message: `Attachment '${attachment.name}' cannot be sent: ${claim.reason}.`,
});
}
const info = yield* fileSystem.stat(claim.currentPath).pipe(
Effect.mapError(
(cause) =>
new AttachmentClaimError({
message: `Attachment '${attachment.name}' cannot be sent: attachment not found.`,
cause,
}),
),
);
if (Number(info.size) !== attachment.sizeBytes) {
return yield* new AttachmentClaimError({
message: `Attachment '${attachment.name}' cannot be sent: stored size does not match.`,
});
}
const normalized: ChatAttachment = {
...attachment,
id: ChatAttachmentId.make(claim.finalId),
name:
attachment.type === "file" &&
isSvgAttachment(attachment) &&
!/\.svg$/i.test(attachment.name)
? `${attachment.name}.svg`
: attachment.name,
mimeType: attachment.mimeType.toLowerCase(),
};
const expectedPath = resolveAttachmentPath({
attachmentsDir: serverConfig.attachmentsDir,
attachment: normalized,
});
if (expectedPath !== claim.finalPath) {
return yield* new AttachmentClaimError({
message: `Attachment '${attachment.name}' cannot be sent: attachment type does not match the upload.`,
});
}
// Copies isolate edits from the retry source. Keep the uncancellable copy and rollback registration
// uninterruptible so cleanup always sees completed writes.
yield* fileSystem.copyFile(claim.currentPath, claim.finalPath).pipe(
Effect.mapError(
(cause) =>
new AttachmentClaimError({
message: `Failed to claim attachment '${attachment.name}' for this thread.`,
cause,
}),
),
Effect.andThen(Effect.sync(() => claimedPaths.push(claim.finalPath))),
Effect.uninterruptible,
);
const sidecar = `${claim.currentPath}.txt`;
if (yield* fileSystem.exists(sidecar).pipe(Effect.orElseSucceed(() => false))) {
yield* fileSystem.copyFile(sidecar, `${claim.finalPath}.txt`).pipe(
Effect.andThen(Effect.sync(() => claimedPaths.push(`${claim.finalPath}.txt`))),
Effect.onError(() =>
fileSystem.remove(`${claim.finalPath}.txt`, { force: true }).pipe(Effect.ignore),
),
Effect.uninterruptible,
Effect.ignore,
);
} else {
const extracted = yield* documents.ensure(claim.finalPath);
if (extracted) claimedPaths.push(extracted);
}
return normalized;
}),
{ concurrency: 1 },
).pipe(Effect.onError(() => releaseClaimedAttachments(claimedPaths)));
return { attachments, claimedPaths } satisfies ClaimedAttachments;
},
);