A customer can now show the Copilot what went wrong instead of describing it. The picture is uploaded before the question, kept in the artifact bucket under the tenant, project and session that own it, and read back for that person only. The type is decided by the bytes, never by the request: anything that is not a PNG, JPEG or WebP is refused, as is anything over 4 MiB. Only the question's own attachment ids are accepted, at most three, and the upload body is the picture itself so a 4 MiB image never becomes a 5 MiB JSON string. A question with a picture is answered from what the picture shows. It is not measured against the pages of the project — no page could ever quote a screenshot — and it does not become a gap in what the project's docs cover. Also fixes the Copilot tool stub in the security test, which had stopped satisfying the project-scoped code reader.
209 lines
8.2 KiB
TypeScript
209 lines
8.2 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
import { DomainError, type Actor, type CopilotSession, type Entity } from '@wishforge/contracts';
|
|
import { S3Artifacts, sha256 } from '../../../../packages/artifacts/src/index.js';
|
|
import type { Store, Transaction } from '../db/store.js';
|
|
import { must, projectAccess, record } from './control-plane.js';
|
|
import { requireHuman } from './policy.js';
|
|
|
|
/** A customer may show the Copilot at most three pictures with one question. */
|
|
export const MAX_ATTACHMENTS = 3;
|
|
/** Matches the artifact store's own ceiling; anything larger is refused before it is read. */
|
|
export const MAX_ATTACHMENT_BYTES = 4 * 1024 * 1024;
|
|
export const ATTACHMENT_MEDIA = ['image/png', 'image/jpeg', 'image/webp'] as const;
|
|
export type AttachmentMedia = (typeof ATTACHMENT_MEDIA)[number];
|
|
|
|
export interface CopilotAttachmentRecord extends Entity {
|
|
projectId: string;
|
|
sessionId: string;
|
|
userId: string;
|
|
/** The name the customer's file had, kept only for display. */
|
|
name: string;
|
|
contentType: AttachmentMedia;
|
|
size: number;
|
|
sha256: string;
|
|
key: string;
|
|
}
|
|
|
|
/** What the browser needs to show the picture again, in the transcript or before sending. */
|
|
export interface AttachmentView {
|
|
id: string;
|
|
name: string;
|
|
contentType: AttachmentMedia;
|
|
size: number;
|
|
}
|
|
|
|
/**
|
|
* The bytes decide the type, never the request. A file that says `image/png` but is not a PNG is
|
|
* refused, so nothing but a real picture ever reaches the model.
|
|
*/
|
|
export function sniffImage(bytes: Buffer): AttachmentMedia | undefined {
|
|
if (bytes.length >= 8 && bytes.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])))
|
|
return 'image/png';
|
|
if (bytes.length >= 3 && bytes.subarray(0, 3).equals(Buffer.from([0xff, 0xd8, 0xff]))) return 'image/jpeg';
|
|
if (
|
|
bytes.length >= 12 &&
|
|
bytes.subarray(0, 4).toString('latin1') === 'RIFF' &&
|
|
bytes.subarray(8, 12).toString('latin1') === 'WEBP'
|
|
)
|
|
return 'image/webp';
|
|
return undefined;
|
|
}
|
|
|
|
/** A display name only: no path, no control characters, and short enough for a label. */
|
|
export function safeName(raw: unknown, contentType: AttachmentMedia) {
|
|
const fallback = `截图.${contentType === 'image/png' ? 'png' : contentType === 'image/jpeg' ? 'jpg' : 'webp'}`;
|
|
const value = typeof raw === 'string' ? (raw.replace(/\\/g, '/').split('/').pop() ?? '') : '';
|
|
const cleaned = value
|
|
.replace(/[\u0000-\u001f\u007f]/g, '')
|
|
.replace(/\s+/g, ' ')
|
|
.trim()
|
|
.slice(0, 120);
|
|
return cleaned || fallback;
|
|
}
|
|
|
|
async function sessionOf(tx: Transaction, actor: Actor, projectId: string, sessionId: string) {
|
|
await projectAccess(tx, actor, projectId);
|
|
const session = await must<CopilotSession>(tx, 'copilot_session', sessionId);
|
|
if (session.projectId !== projectId || session.userId !== actor.userId)
|
|
throw new DomainError('NOT_FOUND', 'Copilot session not found', 404);
|
|
return session;
|
|
}
|
|
|
|
/**
|
|
* Pictures a customer attaches to a Copilot question. They are stored in the same bucket as run
|
|
* artifacts, under the tenant, project and session that own them, and are only ever read back for
|
|
* the person who asked: no run, no other user, no other project.
|
|
*/
|
|
export class CopilotAttachments {
|
|
constructor(
|
|
private store: Store,
|
|
objects?: S3Artifacts,
|
|
) {
|
|
this.objects = objects ?? new S3Artifacts(process.env);
|
|
}
|
|
private readonly objects: S3Artifacts;
|
|
|
|
async upload(
|
|
actor: Actor,
|
|
projectId: string,
|
|
sessionId: string,
|
|
body: Buffer,
|
|
requestedName?: unknown,
|
|
): Promise<AttachmentView> {
|
|
requireHuman(actor);
|
|
if (body.length < 1) throw new DomainError('ATTACHMENT_EMPTY', 'The picture is empty', 400);
|
|
if (body.length > MAX_ATTACHMENT_BYTES) throw new DomainError('ATTACHMENT_TOO_LARGE', '每张图片不能超过 4MB', 413);
|
|
const contentType = sniffImage(body);
|
|
if (!contentType) throw new DomainError('ATTACHMENT_TYPE', '只支持 PNG、JPEG 或 WebP 图片', 415);
|
|
const name = safeName(requestedName, contentType);
|
|
const digest = sha256(body);
|
|
const scope = { tenantId: actor.tenantId, projectId, sessionId };
|
|
const key = this.objects.attachmentKey(scope, digest);
|
|
return this.store.transaction(actor.tenantId, async tx => {
|
|
await sessionOf(tx, actor, projectId, sessionId);
|
|
const existing = (await tx.list<CopilotAttachmentRecord>('copilot_attachment')).find(
|
|
attachment => attachment.key === key && attachment.sessionId === sessionId,
|
|
);
|
|
if (existing) return view(existing);
|
|
const attachment: CopilotAttachmentRecord = {
|
|
...record(actor.tenantId),
|
|
projectId,
|
|
sessionId,
|
|
userId: actor.userId,
|
|
name,
|
|
contentType,
|
|
size: body.length,
|
|
sha256: digest,
|
|
key,
|
|
};
|
|
await this.objects.putBytes(key, body, contentType, {
|
|
tenant: actor.tenantId,
|
|
project: projectId,
|
|
session: sessionId,
|
|
sha256: digest,
|
|
});
|
|
await tx.put('copilot_attachment', attachment);
|
|
return view(attachment);
|
|
});
|
|
}
|
|
|
|
async read(actor: Actor, projectId: string, sessionId: string, attachmentId: string) {
|
|
requireHuman(actor);
|
|
const recordFile = await this.store.read(actor.tenantId, async tx => {
|
|
await sessionOf(tx, actor, projectId, sessionId);
|
|
const attachment = await must<CopilotAttachmentRecord>(tx, 'copilot_attachment', attachmentId);
|
|
if (attachment.projectId !== projectId || attachment.sessionId !== sessionId)
|
|
throw new DomainError('NOT_FOUND', 'Attachment not found', 404);
|
|
return attachment;
|
|
});
|
|
const bytes = await this.objects.getBytes(recordFile.key, { size: recordFile.size, sha256: recordFile.sha256 });
|
|
return { attachment: recordFile, bytes };
|
|
}
|
|
|
|
/** The pictures to hand the model, in the order the customer attached them. */
|
|
async forQuestion(
|
|
actor: Actor,
|
|
projectId: string,
|
|
sessionId: string,
|
|
ids: string[],
|
|
): Promise<{ mediaType: AttachmentMedia; data: string }[]> {
|
|
if (ids.length === 0) return [];
|
|
if (ids.length > MAX_ATTACHMENTS)
|
|
throw new DomainError('ATTACHMENT_LIMIT', `一次最多附上 ${MAX_ATTACHMENTS} 张图片`, 400);
|
|
const files = await this.store.read(actor.tenantId, async tx => {
|
|
await sessionOf(tx, actor, projectId, sessionId);
|
|
const all = await tx.list<CopilotAttachmentRecord>('copilot_attachment');
|
|
return ids.map(id => {
|
|
const attachment = all.find(item => item.id === id);
|
|
if (!attachment || attachment.projectId !== projectId || attachment.sessionId !== sessionId)
|
|
throw new DomainError('NOT_FOUND', 'Attachment not found', 404);
|
|
return attachment;
|
|
});
|
|
});
|
|
const images = [];
|
|
for (const attachment of files) {
|
|
const bytes = await this.objects.getBytes(attachment.key, {
|
|
size: attachment.size,
|
|
sha256: attachment.sha256,
|
|
});
|
|
images.push({ mediaType: attachment.contentType, data: bytes.toString('base64') });
|
|
}
|
|
return images;
|
|
}
|
|
|
|
/** Shapes the stored pictures a question was sent with, for the transcript. */
|
|
async ofQuestion(actor: Actor, projectId: string, sessionId: string, ids: string[]): Promise<AttachmentView[]> {
|
|
if (ids.length === 0) return [];
|
|
return this.store.read(actor.tenantId, async tx => {
|
|
await sessionOf(tx, actor, projectId, sessionId);
|
|
const all = await tx.list<CopilotAttachmentRecord>('copilot_attachment');
|
|
return ids.flatMap(id => {
|
|
const attachment = all.find(item => item.id === id);
|
|
return attachment && attachment.projectId === projectId && attachment.sessionId === sessionId
|
|
? [view(attachment)]
|
|
: [];
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
export function view(attachment: CopilotAttachmentRecord): AttachmentView {
|
|
return {
|
|
id: attachment.id,
|
|
name: attachment.name,
|
|
contentType: attachment.contentType,
|
|
size: attachment.size,
|
|
};
|
|
}
|
|
|
|
/** Ids are client-supplied; a repeated or unknown id is dropped rather than trusted. */
|
|
export function attachmentIds(input: { attachments?: string[] } | undefined) {
|
|
return [...new Set((input?.attachments ?? []).filter(id => typeof id === 'string' && id.length > 0))].slice(
|
|
0,
|
|
MAX_ATTACHMENTS,
|
|
);
|
|
}
|
|
|
|
/** A fresh id for a picture that never made it to storage, keeps the transcript honest. */
|
|
export const placeholderAttachmentId = () => randomUUID();
|