@aqian0/dsh-renderer 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.
- package/LICENSE +21 -0
- package/README.md +119 -2
- package/cordis.patch.yml +4 -0
- package/icon.svg +5 -0
- package/lib/client.js +738 -0
- package/lib/config.js +92 -0
- package/lib/host/artifact-tool.js +141 -0
- package/lib/host/media-bridge.js +102 -0
- package/lib/index.js +101 -0
- package/lib/shared/config-defaults.js +88 -0
- package/lib/shared/content-complexity.js +748 -0
- package/lib/shared/media-extractor.js +154 -0
- package/lib/shared/sandbox-core.js +217 -0
- package/lib/shared/sanitize.js +539 -0
- package/lib/types/client/artifact-toolview.d.ts +17 -0
- package/lib/types/client/code-view.d.ts +22 -0
- package/lib/types/client/document-card.d.ts +33 -0
- package/lib/types/client/fence-layer.d.ts +26 -0
- package/lib/types/client/highlight.d.ts +24 -0
- package/lib/types/client/icons.d.ts +10 -0
- package/lib/types/client/locales.d.ts +54 -0
- package/lib/types/client/sandbox.d.ts +23 -0
- package/lib/types/client/styles.d.ts +20 -0
- package/lib/types/client/svg-view.d.ts +19 -0
- package/lib/types/client/theme.d.ts +20 -0
- package/lib/types/client/turn-media.d.ts +10 -0
- package/lib/types/client/types.d.ts +181 -0
- package/lib/types/client/workbench.d.ts +24 -0
- package/lib/types/client.d.ts +10 -0
- package/lib/types/config.d.ts +48 -0
- package/lib/types/host/artifact-tool.d.ts +53 -0
- package/lib/types/host/media-bridge.d.ts +47 -0
- package/lib/types/index.d.ts +29 -0
- package/lib/types/shared/config-defaults.d.ts +54 -0
- package/lib/types/shared/content-complexity.d.ts +88 -0
- package/lib/types/shared/media-extractor.d.ts +42 -0
- package/lib/types/shared/sandbox-core.d.ts +101 -0
- package/lib/types/shared/sanitize.d.ts +97 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +91 -4
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Non-destructive prose media extractor for the `conversation.chat.turnTail` fallback.
|
|
3
|
+
*
|
|
4
|
+
* DSH's built-in `MarkdownText` currently rejects `data:image/*` Markdown image
|
|
5
|
+
* destinations and renders raw HTML (`<img>`, `<svg>`) as literal escaped text.
|
|
6
|
+
* This module scans Assistant prose *outside* code fences and inline code spans
|
|
7
|
+
* to recover those visual items without duplicating fenced `html`/`svg` blocks.
|
|
8
|
+
*
|
|
9
|
+
* @module dsh-renderer/shared/media-extractor
|
|
10
|
+
*/
|
|
11
|
+
import { prepareSvgForImage, utf8ByteLength } from './sanitize.js';
|
|
12
|
+
const SAFE_DATA_IMAGE_URI_RE = /^data:image\/(?:png|jpeg|jpg|gif|webp|bmp|x-icon|vnd\.microsoft\.icon|svg\+xml)(?:;[^,\s)]*)?,[^\s)]+$/i;
|
|
13
|
+
const SAFE_HTTP_IMAGE_URI_RE = /^https?:\/\/[^\s"'<>]+$/i;
|
|
14
|
+
/**
|
|
15
|
+
* Blank out fenced code blocks (`` ``` `` or `~~~`) and inline code spans (`` `...` ``)
|
|
16
|
+
* while preserving string length so downstream prose scanning never matches code examples
|
|
17
|
+
* or fences already handled by `conversation.chat.markdown.fence`.
|
|
18
|
+
*/
|
|
19
|
+
export function stripMarkdownCode(markdown) {
|
|
20
|
+
const lines = markdown.split('\n');
|
|
21
|
+
const outLines = [];
|
|
22
|
+
let fenceChar = null;
|
|
23
|
+
let fenceLen = 0;
|
|
24
|
+
for (const line of lines) {
|
|
25
|
+
if (fenceChar === null) {
|
|
26
|
+
const open = /^\s{0,3}(`{3,}|~{3,})/.exec(line);
|
|
27
|
+
if (open !== null) {
|
|
28
|
+
fenceChar = open[1][0];
|
|
29
|
+
fenceLen = open[1].length;
|
|
30
|
+
outLines.push('');
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
// Blank inline code spans on non-fence lines.
|
|
34
|
+
outLines.push(stripInlineCodeSpans(line));
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
const close = /^\s{0,3}(`{3,}|~{3,})\s*$/.exec(line);
|
|
38
|
+
if (close !== null && close[1][0] === fenceChar && close[1].length >= fenceLen) {
|
|
39
|
+
fenceChar = null;
|
|
40
|
+
fenceLen = 0;
|
|
41
|
+
}
|
|
42
|
+
outLines.push('');
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return outLines.join('\n');
|
|
46
|
+
}
|
|
47
|
+
function stripInlineCodeSpans(line) {
|
|
48
|
+
let out = '';
|
|
49
|
+
let i = 0;
|
|
50
|
+
while (i < line.length) {
|
|
51
|
+
if (line[i] !== '`') {
|
|
52
|
+
out += line[i];
|
|
53
|
+
i += 1;
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
let tickCount = 0;
|
|
57
|
+
while (i + tickCount < line.length && line[i + tickCount] === '`') {
|
|
58
|
+
tickCount += 1;
|
|
59
|
+
}
|
|
60
|
+
const closer = '`'.repeat(tickCount);
|
|
61
|
+
const closeIdx = line.indexOf(closer, i + tickCount);
|
|
62
|
+
if (closeIdx === -1) {
|
|
63
|
+
out += line.slice(i, i + tickCount);
|
|
64
|
+
i += tickCount;
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
out += ' ';
|
|
68
|
+
i = closeIdx + tickCount;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
/** Read an HTML attribute value (quoted or unquoted) from a single tag string. */
|
|
74
|
+
function readTagAttr(tagSource, attrName) {
|
|
75
|
+
const re = new RegExp(`\\b${attrName}\\s*=\\s*(?:"([^"]*)"|'([^']*)'|([^\\s"'=<>\\x60]+))`, 'i');
|
|
76
|
+
const match = re.exec(tagSource);
|
|
77
|
+
if (match === null)
|
|
78
|
+
return undefined;
|
|
79
|
+
return match[1] ?? match[2] ?? match[3];
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Extract fallback media items (`data:image/*` Markdown images, raw HTML `<img>` tags,
|
|
83
|
+
* and raw inline `<svg>...</svg>` blocks) from Assistant prose.
|
|
84
|
+
*
|
|
85
|
+
* Returns items in document appearance order, deduplicated and bounded by `maxContentBytes`
|
|
86
|
+
* and `maxItems`.
|
|
87
|
+
*/
|
|
88
|
+
export function extractFallbackMedia(markdown, options) {
|
|
89
|
+
const maxItems = options.maxItems ?? 12;
|
|
90
|
+
if (markdown.length === 0 || maxItems <= 0)
|
|
91
|
+
return [];
|
|
92
|
+
const prose = stripMarkdownCode(markdown);
|
|
93
|
+
const candidates = [];
|
|
94
|
+
const seen = new Set();
|
|
95
|
+
const pushUnique = (index, item) => {
|
|
96
|
+
if (seen.has(item.key))
|
|
97
|
+
return;
|
|
98
|
+
if (utf8ByteLength(item.source) > options.maxContentBytes)
|
|
99
|
+
return;
|
|
100
|
+
seen.add(item.key);
|
|
101
|
+
candidates.push({ index, item });
|
|
102
|
+
};
|
|
103
|
+
// 1. Markdown data:image/* images: `` or ``.
|
|
104
|
+
const mdDataImgRe = /!\[([^\]]*)\]\(\s*<?(data:image\/[^)\s>]+)>?(?:\s+(?:"[^"]*"|'[^']*'))?\s*\)/gi;
|
|
105
|
+
for (const match of prose.matchAll(mdDataImgRe)) {
|
|
106
|
+
const rawUrl = (match[2] ?? '').trim();
|
|
107
|
+
if (!SAFE_DATA_IMAGE_URI_RE.test(rawUrl))
|
|
108
|
+
continue;
|
|
109
|
+
const alt = (match[1] ?? '').trim() || 'Image';
|
|
110
|
+
pushUnique(match.index ?? 0, {
|
|
111
|
+
key: `img:${rawUrl}`,
|
|
112
|
+
kind: 'image',
|
|
113
|
+
source: rawUrl,
|
|
114
|
+
alt,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
// 2. Raw HTML `<img ...>` tags (which DSH's MarkdownText renders as literal text).
|
|
118
|
+
const htmlImgRe = /<img\b[^>]*>/gi;
|
|
119
|
+
for (const match of prose.matchAll(htmlImgRe)) {
|
|
120
|
+
const tag = match[0];
|
|
121
|
+
const src = readTagAttr(tag, 'src')?.trim();
|
|
122
|
+
if (src === undefined || src.length === 0)
|
|
123
|
+
continue;
|
|
124
|
+
if (!SAFE_DATA_IMAGE_URI_RE.test(src) && !SAFE_HTTP_IMAGE_URI_RE.test(src))
|
|
125
|
+
continue;
|
|
126
|
+
const alt = readTagAttr(tag, 'alt')?.trim() || readTagAttr(tag, 'title')?.trim() || 'Image';
|
|
127
|
+
pushUnique(match.index ?? 0, {
|
|
128
|
+
key: `img:${src}`,
|
|
129
|
+
kind: 'image',
|
|
130
|
+
source: src,
|
|
131
|
+
alt,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
// 3. Raw inline `<svg ...>...</svg>` blocks outside fences.
|
|
135
|
+
const inlineSvgRe = /<svg\b[\s\S]*?<\/svg>/gi;
|
|
136
|
+
for (const match of prose.matchAll(inlineSvgRe)) {
|
|
137
|
+
const rawSvg = match[0];
|
|
138
|
+
if (utf8ByteLength(rawSvg) > options.maxContentBytes)
|
|
139
|
+
continue;
|
|
140
|
+
const prepared = prepareSvgForImage(rawSvg);
|
|
141
|
+
if (prepared === null)
|
|
142
|
+
continue;
|
|
143
|
+
const titleMatch = /<title\b[^>]*>([\s\S]*?)<\/title>/i.exec(prepared);
|
|
144
|
+
const alt = titleMatch?.[1]?.trim() || 'SVG graphic';
|
|
145
|
+
pushUnique(match.index ?? 0, {
|
|
146
|
+
key: `svg:${prepared}`,
|
|
147
|
+
kind: 'svg',
|
|
148
|
+
source: prepared,
|
|
149
|
+
alt,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
candidates.sort((a, b) => a.index - b.index);
|
|
153
|
+
return candidates.slice(0, maxItems).map((entry) => entry.item);
|
|
154
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure runtime state primitives shared by the sandbox iframe host and Artifact workbench:
|
|
3
|
+
* - LRU pool capping concurrently active interactive sandboxes
|
|
4
|
+
* - Strict `postMessage` frame parser with token verification and height clamping
|
|
5
|
+
* - Session-aware in-memory Artifact store with subscription support
|
|
6
|
+
*
|
|
7
|
+
* @module dsh-renderer/shared/sandbox-core
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Validate and parse a `MessageEvent.data` payload from a sandboxed iframe.
|
|
11
|
+
*
|
|
12
|
+
* Rejects anything whose `token` does not match the instance's secret token or
|
|
13
|
+
* whose `type` is outside the explicit two-event allowlist (`resize` / `error`).
|
|
14
|
+
*/
|
|
15
|
+
export function parseSandboxMessage(data, expectedToken, minHeight, maxHeight) {
|
|
16
|
+
if (data === null || typeof data !== 'object')
|
|
17
|
+
return null;
|
|
18
|
+
const record = data;
|
|
19
|
+
if (record['token'] !== expectedToken)
|
|
20
|
+
return null;
|
|
21
|
+
if (record['type'] === 'dsh-renderer:resize') {
|
|
22
|
+
const rawHeight = record['height'];
|
|
23
|
+
if (typeof rawHeight !== 'number' || !Number.isFinite(rawHeight) || rawHeight <= 0) {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
const clamped = Math.min(maxHeight, Math.max(minHeight, Math.ceil(rawHeight)));
|
|
27
|
+
return { type: 'resize', height: clamped };
|
|
28
|
+
}
|
|
29
|
+
if (record['type'] === 'dsh-renderer:error') {
|
|
30
|
+
const rawMessage = record['message'];
|
|
31
|
+
if (typeof rawMessage !== 'string')
|
|
32
|
+
return null;
|
|
33
|
+
const trimmed = rawMessage.trim().slice(0, 500);
|
|
34
|
+
return trimmed.length > 0 ? { type: 'error', message: trimmed } : null;
|
|
35
|
+
}
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* LRU pool that bounds the number of simultaneously live interactive iframe sandboxes.
|
|
40
|
+
*
|
|
41
|
+
* Activating a new sandbox past `capacity` puts the least-recently-used sandbox
|
|
42
|
+
* to sleep via its registered `onSleep` callback so memory and Blob URLs stay bounded
|
|
43
|
+
* across long conversations.
|
|
44
|
+
*/
|
|
45
|
+
export class SandboxLruPool {
|
|
46
|
+
capacity;
|
|
47
|
+
active = new Map();
|
|
48
|
+
constructor(capacity) {
|
|
49
|
+
this.capacity = Math.max(1, Math.floor(capacity));
|
|
50
|
+
}
|
|
51
|
+
/** Mark `id` active (or refresh its recency) and evict the oldest entry when full. */
|
|
52
|
+
activate(id, onSleep) {
|
|
53
|
+
if (this.active.has(id)) {
|
|
54
|
+
this.active.delete(id);
|
|
55
|
+
this.active.set(id, onSleep);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
while (this.active.size >= this.capacity) {
|
|
59
|
+
const oldestKey = this.active.keys().next().value;
|
|
60
|
+
if (oldestKey === undefined)
|
|
61
|
+
break;
|
|
62
|
+
const sleep = this.active.get(oldestKey);
|
|
63
|
+
this.active.delete(oldestKey);
|
|
64
|
+
try {
|
|
65
|
+
sleep?.();
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
// One misbehaving callback must never stall eviction of other sandboxes.
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
this.active.set(id, onSleep);
|
|
72
|
+
}
|
|
73
|
+
/** Refresh recency of an already active sandbox without changing its callback. */
|
|
74
|
+
touch(id) {
|
|
75
|
+
const existing = this.active.get(id);
|
|
76
|
+
if (existing === undefined)
|
|
77
|
+
return;
|
|
78
|
+
this.active.delete(id);
|
|
79
|
+
this.active.set(id, existing);
|
|
80
|
+
}
|
|
81
|
+
/** Remove `id` from the pool without invoking its sleep callback (e.g. normal unmount). */
|
|
82
|
+
release(id) {
|
|
83
|
+
this.active.delete(id);
|
|
84
|
+
}
|
|
85
|
+
/** Put every active sandbox to sleep and clear the pool (e.g. plugin teardown). */
|
|
86
|
+
sleepAll() {
|
|
87
|
+
const callbacks = [...this.active.values()];
|
|
88
|
+
this.active.clear();
|
|
89
|
+
for (const sleep of callbacks) {
|
|
90
|
+
try {
|
|
91
|
+
sleep();
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
// Keep tearing down remaining sandboxes even if one throws.
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/** Whether `id` is currently active in the pool. */
|
|
99
|
+
isActive(id) {
|
|
100
|
+
return this.active.has(id);
|
|
101
|
+
}
|
|
102
|
+
/** Current number of active sandboxes. */
|
|
103
|
+
size() {
|
|
104
|
+
return this.active.size;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/** Resource URI prefix used for right-Sidebar artifact tabs. */
|
|
108
|
+
export const ARTIFACT_RESOURCE_PREFIX = 'dsh-resource://dsh-renderer/artifact/';
|
|
109
|
+
/** Build a canonical `dsh-resource://` address for one Artifact id. */
|
|
110
|
+
export function artifactAddressFor(id) {
|
|
111
|
+
return `${ARTIFACT_RESOURCE_PREFIX}${encodeURIComponent(id)}`;
|
|
112
|
+
}
|
|
113
|
+
/** Extract the Artifact id from a `dsh-resource://dsh-renderer/artifact/<id>` address. */
|
|
114
|
+
export function parseArtifactAddress(address) {
|
|
115
|
+
if (!address.startsWith(ARTIFACT_RESOURCE_PREFIX))
|
|
116
|
+
return null;
|
|
117
|
+
const raw = address.slice(ARTIFACT_RESOURCE_PREFIX.length);
|
|
118
|
+
if (raw.length === 0)
|
|
119
|
+
return null;
|
|
120
|
+
try {
|
|
121
|
+
return decodeURIComponent(raw);
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Bounded in-memory store for Artifacts opened in the right Sidebar workbench
|
|
129
|
+
* or fullscreen modal fallback.
|
|
130
|
+
*/
|
|
131
|
+
export class ArtifactStore {
|
|
132
|
+
maxEntries;
|
|
133
|
+
records = new Map();
|
|
134
|
+
listeners = new Set();
|
|
135
|
+
modalArtifactId = null;
|
|
136
|
+
seq = 0;
|
|
137
|
+
constructor(maxEntries = 64) {
|
|
138
|
+
this.maxEntries = Math.max(1, Math.floor(maxEntries));
|
|
139
|
+
}
|
|
140
|
+
/** Insert or update an artifact, returning the stored record. */
|
|
141
|
+
put(input) {
|
|
142
|
+
const id = input.id ?? `a-${++this.seq}`;
|
|
143
|
+
const record = {
|
|
144
|
+
id,
|
|
145
|
+
address: artifactAddressFor(id),
|
|
146
|
+
kind: input.kind,
|
|
147
|
+
lang: input.lang,
|
|
148
|
+
title: input.title,
|
|
149
|
+
code: input.code,
|
|
150
|
+
sessionId: input.sessionId,
|
|
151
|
+
};
|
|
152
|
+
if (this.records.has(id)) {
|
|
153
|
+
this.records.delete(id);
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
while (this.records.size >= this.maxEntries) {
|
|
157
|
+
const oldest = this.records.keys().next().value;
|
|
158
|
+
if (oldest === undefined)
|
|
159
|
+
break;
|
|
160
|
+
this.records.delete(oldest);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
this.records.set(id, record);
|
|
164
|
+
this.emit();
|
|
165
|
+
return record;
|
|
166
|
+
}
|
|
167
|
+
/** Look up one artifact by id. */
|
|
168
|
+
get(id) {
|
|
169
|
+
return this.records.get(id);
|
|
170
|
+
}
|
|
171
|
+
/** Look up one artifact by its `dsh-resource://` address. */
|
|
172
|
+
getByAddress(address) {
|
|
173
|
+
const id = parseArtifactAddress(address);
|
|
174
|
+
return id === null ? undefined : this.records.get(id);
|
|
175
|
+
}
|
|
176
|
+
/** Open an artifact inside the fullscreen modal fallback. */
|
|
177
|
+
openModal(id) {
|
|
178
|
+
if (!this.records.has(id))
|
|
179
|
+
return;
|
|
180
|
+
this.modalArtifactId = id;
|
|
181
|
+
this.emit();
|
|
182
|
+
}
|
|
183
|
+
/** Close the fullscreen modal fallback. */
|
|
184
|
+
closeModal() {
|
|
185
|
+
if (this.modalArtifactId === null)
|
|
186
|
+
return;
|
|
187
|
+
this.modalArtifactId = null;
|
|
188
|
+
this.emit();
|
|
189
|
+
}
|
|
190
|
+
/** Current artifact displayed in the fullscreen modal fallback, if any. */
|
|
191
|
+
getModalArtifact() {
|
|
192
|
+
return this.modalArtifactId === null ? undefined : this.records.get(this.modalArtifactId);
|
|
193
|
+
}
|
|
194
|
+
/** Subscribe to store updates (`useSyncExternalStore` compatible). */
|
|
195
|
+
subscribe(listener) {
|
|
196
|
+
this.listeners.add(listener);
|
|
197
|
+
return () => {
|
|
198
|
+
this.listeners.delete(listener);
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
/** Drop all artifacts and listeners on plugin teardown. */
|
|
202
|
+
clear() {
|
|
203
|
+
this.records.clear();
|
|
204
|
+
this.modalArtifactId = null;
|
|
205
|
+
this.emit();
|
|
206
|
+
}
|
|
207
|
+
emit() {
|
|
208
|
+
for (const listener of this.listeners) {
|
|
209
|
+
try {
|
|
210
|
+
listener();
|
|
211
|
+
}
|
|
212
|
+
catch {
|
|
213
|
+
// Isolate subscriber errors.
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|