-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmedia-utils.ts
More file actions
231 lines (209 loc) · 6.95 KB
/
media-utils.ts
File metadata and controls
231 lines (209 loc) · 6.95 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* AI-specific media utilities for converting rich output to LLM-friendly formats
*
* This module provides utilities for converting Jupyter-style rich output
* into formats that work well with Large Language Models, while preserving
* the flexibility to extend with custom AI-specific transformations.
*/
import {
IMAGE_MIME_TYPES,
isImageMimeType,
type MediaContainer,
} from "@runt/schema";
/**
* Media bundle interface for AI processing
* Maps MIME types to their content representations
*/
export interface AIMediaBundle {
[mimeType: string]: unknown;
}
/**
* Rich output data structure used in notebook outputs
*/
export interface RichOutputData {
[mimeType: string]: MediaContainer;
}
/**
* Convert rich notebook output to AI-friendly formats
*
* AI models work better with certain formats:
* - Markdown is more compact and structured than HTML
* - JSON preserves data structure for reasoning
* - Images work with vision-capable models
* - Plain text provides universal fallback
*
* @example
* ```typescript
* const richOutput = {
* "text/html": { type: "inline", data: "<h1>Sales Report</h1>" },
* "text/markdown": { type: "inline", data: "# Sales Report" },
* "application/json": { type: "inline", data: { revenue: 10000 } }
* };
*
* const aiBundle = toAIMediaBundle(richOutput);
* // Prefers markdown over HTML, keeps JSON structure
* ```
*/
export function toAIMediaBundle(richOutput: RichOutputData): AIMediaBundle {
const result: AIMediaBundle = {};
// Always include text/plain if available
if (richOutput["text/plain"]) {
const container = richOutput["text/plain"];
if (container.type === "inline") {
result["text/plain"] = container.data;
}
}
// Prefer markdown over HTML for AI
if (richOutput["text/markdown"]) {
const container = richOutput["text/markdown"];
if (container.type === "inline") {
result["text/markdown"] = container.data;
}
} else if (richOutput["text/html"]) {
const container = richOutput["text/html"];
if (container.type === "inline" && typeof container.data === "string") {
// Convert HTML to plain text for AI if no markdown available
const plainFromHtml = container.data.replace(/<[^>]*>/g, "");
if (!result["text/plain"]) {
result["text/plain"] = plainFromHtml;
}
}
}
// Include JSON for structured data
if (richOutput["application/json"]) {
const container = richOutput["application/json"];
if (container.type === "inline") {
result["application/json"] = container.data;
}
}
// Include images that some AI providers support
for (const imageType of IMAGE_MIME_TYPES) {
if (richOutput[imageType]) {
const container = richOutput[imageType];
if (container.type === "inline") {
result[imageType] = container.data;
} else if (container.type === "artifact") {
// For artifacts, include a reference that AI can understand
// The AI can request the artifact URL if needed
result[imageType] = `[Image artifact: ${container.artifactId}]`;
// Also add metadata if available for context
if (container.metadata?.originalSizeBytes) {
result[`${imageType}:metadata`] = {
artifactId: container.artifactId,
sizeBytes: container.metadata.originalSizeBytes,
type: "artifact",
};
}
}
}
}
return result;
}
/**
* Ensure every media bundle has text/plain for maximum AI compatibility
*
* Some AI providers only support text, and text/plain ensures your output
* is never completely invisible to an AI system.
*
* @example
* ```typescript
* const bundle = { "text/html": "<b>Important data</b>" };
* const withFallback = ensureTextPlainFallback(bundle);
* // { "text/html": "<b>Important data</b>", "text/plain": "Important data" }
* ```
*/
export function ensureTextPlainFallback(bundle: AIMediaBundle): AIMediaBundle {
if (bundle["text/plain"]) {
return bundle;
}
const result = { ...bundle };
// Try to generate text/plain from other formats
if (typeof result["text/html"] === "string") {
// Strip HTML tags for plain text
result["text/plain"] = result["text/html"].replace(/<[^>]*>/g, "");
} else if (typeof result["text/markdown"] === "string") {
// Markdown is readable as plain text
result["text/plain"] = result["text/markdown"];
} else {
// Use first available string content, but skip image mime types
const firstStringValue = Object.entries(result).find(
([mimeType, value]) => {
// Skip image mime types to avoid using base64 data as text
if (isImageMimeType(mimeType)) {
return false;
}
return typeof value === "string";
},
)?.[1] as string | undefined;
if (firstStringValue) {
result["text/plain"] = firstStringValue;
} else {
// Last resort: JSON stringify first available non-image content
const firstNonImageEntry = Object.entries(result).find(
([mimeType]) => !isImageMimeType(mimeType),
);
if (firstNonImageEntry && firstNonImageEntry[1] != null) {
try {
result["text/plain"] = JSON.stringify(firstNonImageEntry[1], null, 2);
} catch {
result["text/plain"] = String(firstNonImageEntry[1]);
}
} else {
result["text/plain"] = "";
}
}
}
return result;
}
/**
* Convert rich output data to a simplified format for AI context
*
* This function prioritizes content types that work well with LLMs:
* 1. Markdown over HTML (better structure, less noise)
* 2. Plain text as universal fallback
* 3. JSON for structured data
* 4. Images for vision-capable models
*
* Skips verbose formats like HTML with embedded CSS/JS.
*/
export function toAIContext(richOutput: RichOutputData): string {
const aiBundle = toAIMediaBundle(richOutput);
const withFallback = ensureTextPlainFallback(aiBundle);
// Prioritize markdown for AI readability
if (withFallback["text/markdown"]) {
return String(withFallback["text/markdown"]);
}
// Fall back to plain text
if (withFallback["text/plain"]) {
return String(withFallback["text/plain"]);
}
// Last resort: JSON representation
if (withFallback["application/json"]) {
try {
return JSON.stringify(withFallback["application/json"], null, 2);
} catch {
return String(withFallback["application/json"]);
}
}
return "";
}
/**
* Check if rich output contains visual content (images, plots, etc.)
*/
export function hasVisualContent(richOutput: RichOutputData): boolean {
return IMAGE_MIME_TYPES.some((mimeType) =>
richOutput[mimeType] && richOutput[mimeType].type === "inline"
);
}
/**
* Extract structured data from rich output for AI analysis
*/
export function extractStructuredData(richOutput: RichOutputData): unknown {
if (richOutput["application/json"]) {
const container = richOutput["application/json"];
if (container.type === "inline") {
return container.data;
}
}
return null;
}