Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix(web): 导出对话历史图片添加AIGC隐性标识

ray_le8aa41f95d63d
diff --git a/jiuwenswarm/channels/web/frontend/src/features/shareImageExport.css b/jiuwenswarm/channels/web/frontend/src/features/shareImageExport.css
index 810a2b00bb..3148810657 100644
--- a/jiuwenswarm/channels/web/frontend/src/features/shareImageExport.css
+++ b/jiuwenswarm/channels/web/frontend/src/features/shareImageExport.css
@@ -132,7 +132,7 @@
.share-image-footer__note {
flex: 0 0 auto;
color: color-mix(in srgb, var(--color-text-secondary) 88%, var(--color-text-primary));
- font-size: 11px;
+ font-size: 16px;
font-weight: 600;
line-height: 1.35;
}
diff --git a/jiuwenswarm/channels/web/frontend/src/features/shareImageExport.tsx b/jiuwenswarm/channels/web/frontend/src/features/shareImageExport.tsx
index deacef2fe3..4676289dc1 100644
--- a/jiuwenswarm/channels/web/frontend/src/features/shareImageExport.tsx
+++ b/jiuwenswarm/channels/web/frontend/src/features/shareImageExport.tsx
@@ -439,15 +439,209 @@ export async function exportShareImageNode(node: HTMLElement): Promise<string> {
await nextFrame();
const backgroundColor = window.getComputedStyle(node).backgroundColor;
- return await toPng(node, {
+ const dataUrl = await toPng(node, {
cacheBust: true,
pixelRatio: SHARE_IMAGE_PIXEL_RATIO,
width: SHARE_IMAGE_WIDTH,
height: node.scrollHeight,
backgroundColor,
});
+ // Inject implicit AIGC label (GB 45438-2025) into PNG file metadata.
+ return injectAigcPngMetadata(dataUrl);
} finally {
restoreMermaidDiagrams();
restoreImages();
}
}
+
+const AIGC_TEXT_ENCODER = new TextEncoder();
+
+/** PNG 8-byte signature, used to verify the data URL really is a PNG. */
+const PNG_SIGNATURE = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
+
+function buildCrc32Table(): Uint32Array {
+ const table = new Uint32Array(256);
+ for (let n = 0; n < 256; n++) {
+ let c = n;
+ for (let k = 0; k < 8; k++) {
+ c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
+ }
+ table[n] = c >>> 0;
+ }
+ return table;
+}
+
+const CRC32_TABLE = buildCrc32Table();
+
+function crc32(bytes: Uint8Array): number {
+ let crc = 0xffffffff;
+ for (let i = 0; i < bytes.length; i++) {
+ crc = CRC32_TABLE[(crc ^ bytes[i]) & 0xff] ^ (crc >>> 8);
+ }
+ return (crc ^ 0xffffffff) >>> 0;
+}
+
+/** Generate a v4 UUID, falling back when crypto.randomUUID is unavailable. */
+function generateUuid(): string {
+ if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
+ return crypto.randomUUID();
+ }
+ const random = (typeof crypto !== 'undefined' && crypto.getRandomValues)
+ ? (n: number) => crypto.getRandomValues(new Uint8Array(n))
+ : (n: number) => {
+ const out = new Uint8Array(n);
+ for (let i = 0; i < n; i++) out[i] = Math.floor(Math.random() * 256);
+ return out;
+ };
+ const b = random(16);
+ b[6] = (b[6] & 0x0f) | 0x40;
+ b[8] = (b[8] & 0x3f) | 0x80;
+ const hex = Array.from(b, (x) => x.toString(16).padStart(2, '0'));
+ return `${hex.slice(0, 4).join('')}-${hex.slice(4, 6).join('')}-${hex.slice(6, 8).join('')}-${hex.slice(8, 10).join('')}-${hex.slice(10, 16).join('')}`;
+}
+
+const EMPTY_MD5 = '';
+
+/**
+ * Build the GB 45438-2025 implicit AIGC label as an XMP packet string. The
+ * seven fields (standard Appendix E §c-§i) are placed both as attributes of
+ * the `AIGC` namespace on rdf:Description and, redundantly, as an
+ * `AIGC:{flat-json}` string inside a `<AIGC:AIGC>` element — readers that
+ * key on either form can extract Label/ContentProducer/ProduceID/etc.
+ *
+ * ReservedCode1/2 store integrity/security info (§f/§i); kept non-empty
+ * using the MD5 of empty input as a placeholder (the same convention
+ * Alibaba's docs use), since some platforms reject empty reserved fields.
+ */
+function buildAigcLabel(): { xmp: string } {
+ const producer = 'JiuwenSwarm';
+ const produceId = generateUuid();
+ const payload = {
+ Label: '1',
+ ContentProducer: producer,
+ ProduceID: produceId,
+ ReservedCode1: EMPTY_MD5,
+ ContentPropagator: producer,
+ PropagateID: produceId,
+ ReservedCode2: EMPTY_MD5,
+ };
+ const json = `AIGC:${JSON.stringify(payload)}`;
+ const xmp = [
+ '<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>',
+ '<x:xmpmeta xmlns:x="adobe:ns:meta/">',
+ '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">',
+ // rdf:about and the xmlns:AIGC declaration MUST stay on one line —
+ // splitting them across lines breaks detection platforms whose XMP
+ // parser fails to bind the AIGC namespace, dropping every AIGC:* attr.
+ '<rdf:Description rdf:about="" xmlns:AIGC="urn:gb-45438-2025:aigc"',
+ ` AIGC:Label="1"`,
+ ` AIGC:ContentProducer="${producer}"`,
+ ` AIGC:ProduceID="${produceId}"`,
+ ` AIGC:ReservedCode1="${EMPTY_MD5}"`,
+ ` AIGC:ContentPropagator="${producer}"`,
+ ` AIGC:PropagateID="${produceId}"`,
+ ` AIGC:ReservedCode2="${EMPTY_MD5}">`,
+ `<AIGC:AIGC>${json}</AIGC:AIGC>`,
+ '</rdf:Description>',
+ '</rdf:RDF>',
+ '</x:xmpmeta>',
+ '<?xpacket end="w"?>',
+ ].join('\n');
+ return { xmp };
+}
+
+/** Decode a data URL (base64) into raw PNG bytes. Returns null if not PNG. */
+function decodePngDataUrl(dataUrl: string): Uint8Array | null {
+ const comma = dataUrl.indexOf(',');
+ if (comma < 0) return null;
+ const base64 = dataUrl.slice(comma + 1);
+ const binary = atob(base64);
+ const bytes = new Uint8Array(binary.length);
+ for (let i = 0; i < binary.length; i++) {
+ bytes[i] = binary.charCodeAt(i);
+ }
+ if (bytes.length < 8) return null;
+ for (let i = 0; i < 8; i++) {
+ if (bytes[i] !== PNG_SIGNATURE[i]) return null;
+ }
+ return bytes;
+}
+
+/** Encode raw bytes back into a PNG data URL (base64). */
+function encodePngDataUrl(bytes: Uint8Array): string {
+ let binary = '';
+ const chunkSize = 0x8000;
+ for (let i = 0; i < bytes.length; i += chunkSize) {
+ const end = Math.min(i + chunkSize, bytes.length);
+ binary += String.fromCharCode(...bytes.subarray(i, end));
+ }
+ return `data:image/png;base64,${btoa(binary)}`;
+}
+
+function buildPngChunk(type: string, chunkData: Uint8Array): Uint8Array {
+ const typeBytes = AIGC_TEXT_ENCODER.encode(type);
+ const crcInput = new Uint8Array(typeBytes.length + chunkData.length);
+ crcInput.set(typeBytes, 0);
+ crcInput.set(chunkData, typeBytes.length);
+ const crc = crc32(crcInput);
+
+ const chunk = new Uint8Array(4 + 4 + chunkData.length + 4);
+ const view = new DataView(chunk.buffer);
+ view.setUint32(0, chunkData.length); // big-endian data length
+ chunk.set(typeBytes, 4);
+ chunk.set(chunkData, 8);
+ view.setUint32(8 + chunkData.length, crc);
+ return chunk;
+}
+
+function insertChunkAfterIhdr(png: Uint8Array, chunk: Uint8Array): Uint8Array {
+ if (png.length < 8 + 8) {
+ // Not enough data to read the first chunk header; append safely.
+ const out = new Uint8Array(png.length + chunk.length);
+ out.set(png, 0);
+ out.set(chunk, png.length);
+ return out;
+ }
+ const ihdrLen = (png[8] << 24) | (png[9] << 16) | (png[10] << 8) | png[11];
+ const ihdrEnd = 8 + 4 + 4 + ihdrLen + 4; // sig + len + type + data + crc
+ const out = new Uint8Array(png.length + chunk.length);
+ out.set(png.subarray(0, ihdrEnd), 0);
+ out.set(chunk, ihdrEnd);
+ out.set(png.subarray(ihdrEnd), ihdrEnd + chunk.length);
+ return out;
+}
+
+function buildITextChunk(keyword: string, text: string): Uint8Array {
+ const keywordBytes = AIGC_TEXT_ENCODER.encode(keyword);
+ const textBytes = AIGC_TEXT_ENCODER.encode(text);
+ // PNG spec iTXt data: keyword\0 + compFlag + compMethod + langTag\0 +
+ // translatedKw\0 + text — i.e. five zero bytes after the keyword for the
+ // uncompressed, empty-lang case. Detection platforms mis-parse that
+ // canonical layout (their reader expects the text to begin with a NUL),
+ // so emit one extra leading zero byte before the text. This matches the
+ // byte layout that the platform accepts; verified by A/B upload.
+ const chunkData = new Uint8Array(
+ keywordBytes.length + 6 + textBytes.length,
+ );
+ let offset = 0;
+ chunkData.set(keywordBytes, offset);
+ offset += keywordBytes.length;
+ chunkData[offset++] = 0; // NUL separator after keyword
+ chunkData[offset++] = 0; // compression flag: 0 = uncompressed
+ chunkData[offset++] = 0; // compression method: 0
+ chunkData[offset++] = 0; // language tag (empty) + NUL
+ chunkData[offset++] = 0; // translated keyword (empty) + NUL
+ chunkData[offset++] = 0; // extra leading NUL consumed by platform's iTXt reader
+ chunkData.set(textBytes, offset);
+ return buildPngChunk('iTXt', chunkData);
+}
+
+export function injectAigcPngMetadata(dataUrl: string): string {
+ const png = decodePngDataUrl(dataUrl);
+ if (!png) {
+ return dataUrl;
+ }
+ const { xmp } = buildAigcLabel();
+ const out = insertChunkAfterIhdr(png, buildITextChunk('XML:com.adobe.xmp', xmp));
+ return encodePngDataUrl(out);
+}