magpie-image-slim 0.0.0-stage → 0.1.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.
@@ -0,0 +1,120 @@
1
+ const IMAGE_SLIM_RE = /\[\d+(?:\.\d+)? MB image left out here: [^\]\r\n]*\]/g;
2
+ const TEXT_TYPES = new Set(['input_text', 'output_text', 'text']);
3
+
4
+ export function onRequest(body, ctx) {
5
+ const options = readOptions(ctx?.options);
6
+ if (!matchesScope(ctx, options)) return;
7
+ if (ctx?.protocol !== 'responses' || !Array.isArray(body?.input)) return;
8
+
9
+ let changed = false;
10
+ let replaced = 0;
11
+ let split = 0;
12
+ const protectCurrentTurn = options.keepCurrentTurn;
13
+ const turnStart = protectCurrentTurn ? currentTurnStart(body.input) : -1;
14
+
15
+ for (let index = 0; index < body.input.length; index++) {
16
+ const item = body.input[index];
17
+ if (item?.role !== 'user' || !Array.isArray(item.content)) continue;
18
+ const aggregate = isAggregateMessage(item.content);
19
+ const content = [];
20
+ for (const block of item.content) {
21
+ if (aggregate && (!protectCurrentTurn || index < turnStart) && options.replaceImages && isInlineImage(block)) {
22
+ content.push({ type: 'input_text', text: imageSlimPlaceholder(imageBytes(block)) });
23
+ replaced++;
24
+ changed = true;
25
+ continue;
26
+ }
27
+ if (options.splitPlaceholders && TEXT_TYPES.has(block?.type) && typeof block.text === 'string') {
28
+ const pieces = splitImageSlimText(block);
29
+ if (pieces.length > 1) {
30
+ content.push(...pieces);
31
+ split++;
32
+ changed = true;
33
+ continue;
34
+ }
35
+ }
36
+ content.push(block);
37
+ }
38
+ item.content = content;
39
+ }
40
+
41
+ if (changed && options.log) {
42
+ console.log(`image-slim: [${ctx.agent || 'unknown'}] replaced ${replaced} image(s), split ${split} text block(s)`);
43
+ }
44
+ return changed ? body : undefined;
45
+ }
46
+
47
+ function matchesScope(ctx, options) {
48
+ if (options.agents.length && !options.agents.includes(ctx?.agent)) return false;
49
+ if (options.models.length && !options.models.includes(ctx?.model)) return false;
50
+ return true;
51
+ }
52
+
53
+ function readOptions(raw) {
54
+ const value = raw && typeof raw === 'object' ? raw : {};
55
+ return {
56
+ agents: Array.isArray(value.agents) ? value.agents.filter((x) => typeof x === 'string' && x) : ['dsh'],
57
+ models: Array.isArray(value.models) ? value.models.filter((x) => typeof x === 'string' && x) : [],
58
+ keepCurrentTurn: value.keepCurrentTurn !== false,
59
+ replaceImages: value.replaceImages !== false,
60
+ splitPlaceholders: value.splitPlaceholders !== false,
61
+ log: value.log !== false,
62
+ };
63
+ }
64
+
65
+ function currentTurnStart(input) {
66
+ for (let index = input.length - 1; index >= 0; index--) {
67
+ const item = input[index];
68
+ if (item?.role === 'assistant' || item?.type === 'reasoning' ||
69
+ item?.type === 'function_call' || item?.type === 'custom_tool_call') return index + 1;
70
+ }
71
+ return 0;
72
+ }
73
+
74
+ function textOf(content) {
75
+ return content.map((block) => TEXT_TYPES.has(block?.type) ? String(block.text || '') : '').join('\n');
76
+ }
77
+
78
+ function isAggregateMessage(content) {
79
+ const text = textOf(content);
80
+ return text.includes('[图片聚合]') || text.includes('Image "aggregate.jpg"') ||
81
+ (/(↑ [^\n]* 发的图)/u.test(text) && content.some(isInlineImage));
82
+ }
83
+
84
+ function isInlineImage(block) {
85
+ return block?.type === 'input_image' && typeof block.image_url === 'string' && block.image_url.startsWith('data:');
86
+ }
87
+
88
+ function imageBytes(block) {
89
+ const comma = block.image_url.indexOf(',');
90
+ return comma < 0 ? 0 : Math.max(0, block.image_url.length - comma - 1);
91
+ }
92
+
93
+ function imageSlimPlaceholder(bytes) {
94
+ const mb = (bytes / 1048576).toFixed(2);
95
+ return `[${mb} MB image left out here: an image the user attached, already seen earlier in this conversation. Rely on what you already said about it.]`;
96
+ }
97
+
98
+ function splitImageSlimText(block) {
99
+ const text = block.text;
100
+ IMAGE_SLIM_RE.lastIndex = 0;
101
+ const matches = [...text.matchAll(IMAGE_SLIM_RE)];
102
+ if (!matches.length) return [block];
103
+ const pieces = [];
104
+ let cursor = 0;
105
+ for (const match of matches) {
106
+ if (match.index > cursor) pieces.push({ ...block, text: text.slice(cursor, match.index) });
107
+ pieces.push({ ...block, text: match[0] });
108
+ cursor = match.index + match[0].length;
109
+ }
110
+ if (cursor < text.length) pieces.push({ ...block, text: text.slice(cursor) });
111
+ return pieces;
112
+ }
113
+
114
+ export const _internal = {
115
+ readOptions,
116
+ currentTurnStart,
117
+ isAggregateMessage,
118
+ imageSlimPlaceholder,
119
+ splitImageSlimText,
120
+ };
package/package.json CHANGED
@@ -1,6 +1,36 @@
1
1
  {
2
2
  "name": "magpie-image-slim",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Magpie gateway middleware for DSH aggregate images: replace historical inline images with standard image-slim placeholders and split glued placeholder text blocks.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "files": [
8
+ "image-slim.middleware.js"
9
+ ],
10
+ "keywords": [
11
+ "magpie",
12
+ "magpie-middleware",
13
+ "magpie-plugin",
14
+ "image-slim",
15
+ "dsh",
16
+ "cache"
17
+ ],
18
+ "magpie": {
19
+ "middleware": "./image-slim.middleware.js",
20
+ "options": {
21
+ "agents": [
22
+ "dsh"
23
+ ],
24
+ "models": [],
25
+ "keepCurrentTurn": true,
26
+ "replaceImages": true,
27
+ "splitPlaceholders": true,
28
+ "log": true
29
+ }
30
+ },
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+https://github.com/Bemly/dsh-plug.git",
34
+ "directory": "magpie/middlewares/magpie-image-slim"
35
+ }
36
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.