media-asset-meta.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /**
  2. * Shared media-asset metadata inferers — single source of truth for the
  3. * "agent never emits mime/size, and `type` is often absent, so kind/mime
  4. * must be INFERRED from type/extension" rule.
  5. *
  6. * These were originally defined in the UI's `result-card-utils.ts`. They are
  7. * PORTED here so the IM outbound side (`outbound-media.ts`) has a canonical,
  8. * UI-free implementation it can depend on. The UI's `result-card-utils.ts`
  9. * still carries a parallel copy for now; deduplicating it to re-export from
  10. * this module is a tracked follow-up (out of scope for the foundation MR that
  11. * introduced this file). Until then, do NOT add a THIRD copy — extend here.
  12. *
  13. * IMPORTANT: this module lives in `@mavis/shared` and MUST NOT import from
  14. * `@mavis/ui` (that would create a package cycle). The dependency direction
  15. * is strictly ui → shared.
  16. */
  17. /**
  18. * Minimal structural input for the metadata inferers. The UI's
  19. * `DeliverAssetItem` (which carries extra UI-only fields like `downloadUrl`)
  20. * is structurally assignable to this, so callers can keep passing their
  21. * richer objects unchanged.
  22. */
  23. export interface MediaAssetMetaInput {
  24. path: string;
  25. name?: string;
  26. type?: string;
  27. mimeType?: string;
  28. }
  29. const IMAGE_EXTENSIONS = new Set([
  30. 'png',
  31. 'jpg',
  32. 'jpeg',
  33. 'gif',
  34. 'webp',
  35. 'bmp',
  36. 'svg',
  37. 'ico',
  38. 'avif',
  39. ]);
  40. /**
  41. * Image formats that the workspace file preview and Canvas surfaces can render.
  42. * Keep this narrower than `IMAGE_EXTENSIONS`: formats added here must be
  43. * supported by both the local Runtime media boundary and Chromium decoding.
  44. */
  45. export const WORKSPACE_IMAGE_PREVIEW_MIME_TYPES = {
  46. bmp: 'image/bmp',
  47. gif: 'image/gif',
  48. jpg: 'image/jpeg',
  49. jpeg: 'image/jpeg',
  50. png: 'image/png',
  51. svg: 'image/svg+xml',
  52. webp: 'image/webp',
  53. } as const;
  54. export type WorkspaceImagePreviewMimeType =
  55. (typeof WORKSPACE_IMAGE_PREVIEW_MIME_TYPES)[keyof typeof WORKSPACE_IMAGE_PREVIEW_MIME_TYPES];
  56. const WEB_EXTENSIONS = new Set(['html', 'htm']);
  57. export const MIME_BY_EXTENSION: Record<string, string> = {
  58. png: 'image/png',
  59. jpg: 'image/jpeg',
  60. jpeg: 'image/jpeg',
  61. gif: 'image/gif',
  62. webp: 'image/webp',
  63. bmp: 'image/bmp',
  64. svg: 'image/svg+xml',
  65. ico: 'image/x-icon',
  66. avif: 'image/avif',
  67. pdf: 'application/pdf',
  68. doc: 'application/msword',
  69. docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  70. xls: 'application/vnd.ms-excel',
  71. xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  72. ppt: 'application/vnd.ms-powerpoint',
  73. pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  74. key: 'application/vnd.apple.keynote',
  75. html: 'text/html',
  76. htm: 'text/html',
  77. txt: 'text/plain',
  78. md: 'text/markdown',
  79. markdown: 'text/markdown',
  80. json: 'application/json',
  81. csv: 'text/csv',
  82. tsv: 'text/tab-separated-values',
  83. xml: 'application/xml',
  84. yaml: 'application/yaml',
  85. yml: 'application/yaml',
  86. js: 'text/javascript',
  87. jsx: 'text/javascript',
  88. mjs: 'text/javascript',
  89. cjs: 'text/javascript',
  90. ts: 'text/typescript',
  91. tsx: 'text/typescript',
  92. css: 'text/css',
  93. scss: 'text/x-scss',
  94. sql: 'application/sql',
  95. mp4: 'video/mp4',
  96. webm: 'video/webm',
  97. mov: 'video/quicktime',
  98. mp3: 'audio/mpeg',
  99. m4a: 'audio/mp4',
  100. wav: 'audio/wav',
  101. ogg: 'audio/ogg',
  102. flac: 'audio/flac',
  103. aac: 'audio/aac',
  104. mkv: 'video/x-matroska',
  105. };
  106. function getPathWithoutQuery(path: string): string {
  107. return path.split('#')[0]?.split('?')[0] ?? path;
  108. }
  109. function getPathExtension(path: string): string {
  110. const cleanPath = getPathWithoutQuery(path.trim());
  111. const lastSegment = cleanPath.split('/').pop() ?? cleanPath;
  112. const dotIndex = lastSegment.lastIndexOf('.');
  113. if (dotIndex < 0 || dotIndex === lastSegment.length - 1) {
  114. return '';
  115. }
  116. return lastSegment.slice(dotIndex + 1).toLowerCase();
  117. }
  118. export function getWorkspaceImagePreviewMimeType(
  119. path: string,
  120. ): WorkspaceImagePreviewMimeType | undefined {
  121. const extension = getPathExtension(path);
  122. return WORKSPACE_IMAGE_PREVIEW_MIME_TYPES[
  123. extension as keyof typeof WORKSPACE_IMAGE_PREVIEW_MIME_TYPES
  124. ];
  125. }
  126. export function supportsWorkspaceImagePreview(path: string): boolean {
  127. return getWorkspaceImagePreviewMimeType(path) !== undefined;
  128. }
  129. /**
  130. * Canvas region annotations are currently validated end to end only for JPEG
  131. * and PNG. Other previewable formats stay on the direct add-to-chat path until
  132. * their product semantics are defined, especially GIF frame/timeline anchors.
  133. */
  134. export function supportsCanvasImageAnnotation(path: string): boolean {
  135. const mimeType = getWorkspaceImagePreviewMimeType(path);
  136. return mimeType === 'image/jpeg' || mimeType === 'image/png';
  137. }
  138. function getAssetType(item: MediaAssetMetaInput): string {
  139. return item.type?.trim().toLowerCase() ?? '';
  140. }
  141. function getBaseMimeType(item: MediaAssetMetaInput): string {
  142. return item.mimeType?.split(';', 1)[0]?.trim().toLowerCase() ?? '';
  143. }
  144. function isHttpUrl(path: string): boolean {
  145. return /^https?:\/\//u.test(path.trim());
  146. }
  147. export function isImageAsset(item: MediaAssetMetaInput): boolean {
  148. const type = getAssetType(item);
  149. if (type === 'image') {
  150. return true;
  151. }
  152. if (IMAGE_EXTENSIONS.has(type)) {
  153. return true;
  154. }
  155. if (type) {
  156. return false;
  157. }
  158. return IMAGE_EXTENSIONS.has(getPathExtension(item.path));
  159. }
  160. export function isWebAsset(item: MediaAssetMetaInput): boolean {
  161. const type = getAssetType(item);
  162. const mimeType = getBaseMimeType(item);
  163. if (type === 'image') {
  164. return false;
  165. }
  166. if (mimeType === 'text/html' || mimeType === 'application/xhtml+xml') {
  167. return true;
  168. }
  169. if (type === 'file') {
  170. return false;
  171. }
  172. if (
  173. type === 'html' ||
  174. type === 'web' ||
  175. type === 'website' ||
  176. type === 'url' ||
  177. type === 'link'
  178. ) {
  179. return true;
  180. }
  181. if (isHttpUrl(item.path)) {
  182. return true;
  183. }
  184. return WEB_EXTENSIONS.has(getPathExtension(item.path));
  185. }
  186. export function isWebsiteAsset(item: MediaAssetMetaInput): boolean {
  187. return getAssetType(item) === 'website';
  188. }
  189. export function inferAssetMimeType(item: MediaAssetMetaInput): string | undefined {
  190. if (item.mimeType) {
  191. return item.mimeType;
  192. }
  193. const type = getAssetType(item);
  194. if (type === 'html' || type === 'web' || type === 'website') {
  195. return 'text/html';
  196. }
  197. if (type === 'url' || type === 'link') {
  198. return 'text/html';
  199. }
  200. if (type === 'audio') {
  201. return 'audio/mpeg';
  202. }
  203. if (type === 'video') {
  204. return 'video/mp4';
  205. }
  206. if (type === 'ppt' || type === 'pptx' || type === 'powerpoint' || type === 'presentation') {
  207. return 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
  208. }
  209. if (type === 'doc' || type === 'docx' || type === 'word' || type === 'document') {
  210. return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
  211. }
  212. if (type === 'xls' || type === 'xlsx' || type === 'excel' || type === 'spreadsheet') {
  213. return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
  214. }
  215. if (MIME_BY_EXTENSION[type]) {
  216. return MIME_BY_EXTENSION[type];
  217. }
  218. const nameExtension = item.name ? getPathExtension(item.name) : '';
  219. const pathExtension = getPathExtension(item.path);
  220. return MIME_BY_EXTENSION[nameExtension] ?? MIME_BY_EXTENSION[pathExtension];
  221. }
  222. // ---------------------------------------------------------------------------
  223. // Outbound media kind classification
  224. // ---------------------------------------------------------------------------
  225. /**
  226. * Coarse media kind used by IM outbound senders to pick a transport
  227. * (image / file / audio / video). Distinct from the finer-grained
  228. * image/web/website classifiers above — those drive UI card rendering,
  229. * whereas this maps onto the four send primitives IM platforms expose.
  230. */
  231. export type OutboundMediaKind = 'image' | 'file' | 'audio' | 'video';
  232. const KIND_IMAGE_EXTENSIONS = new Set([
  233. 'png',
  234. 'jpg',
  235. 'jpeg',
  236. 'gif',
  237. 'webp',
  238. 'bmp',
  239. 'svg',
  240. 'ico',
  241. 'avif',
  242. 'heic',
  243. ]);
  244. const KIND_AUDIO_EXTENSIONS = new Set(['mp3', 'm4a', 'wav', 'ogg', 'flac', 'aac', 'opus', 'silk']);
  245. const KIND_VIDEO_EXTENSIONS = new Set(['mp4', 'webm', 'mov', 'mkv', 'avi']);
  246. /** Semantic `type` tokens the agent emits that map to a non-file kind. */
  247. const IMAGE_TYPE_TOKENS = new Set(['image', 'img', 'photo']);
  248. const AUDIO_TYPE_TOKENS = new Set(['audio', 'voice', 'mp3']);
  249. const VIDEO_TYPE_TOKENS = new Set(['video', 'mp4', 'mov']);
  250. /**
  251. * Derive the coarse outbound media kind, in precedence order:
  252. *
  253. * 1. explicit `mimeType` prefix (`image/*` / `audio/*` / `video/*`)
  254. * 2. semantic `type` token (image/img/photo → image; audio/voice/mp3 →
  255. * audio; video/mp4/mov → video)
  256. * 3. file extension of `name` (preferred) or `path`
  257. * 4. default `'file'`
  258. */
  259. export function deriveMediaKind(input: {
  260. type?: string;
  261. mimeType?: string;
  262. name?: string;
  263. path?: string;
  264. }): OutboundMediaKind {
  265. const mimeType = input.mimeType?.trim().toLowerCase() ?? '';
  266. if (mimeType.startsWith('image/')) {
  267. return 'image';
  268. }
  269. if (mimeType.startsWith('audio/')) {
  270. return 'audio';
  271. }
  272. if (mimeType.startsWith('video/')) {
  273. return 'video';
  274. }
  275. const type = input.type?.trim().toLowerCase() ?? '';
  276. if (type) {
  277. if (IMAGE_TYPE_TOKENS.has(type)) {
  278. return 'image';
  279. }
  280. if (AUDIO_TYPE_TOKENS.has(type)) {
  281. return 'audio';
  282. }
  283. if (VIDEO_TYPE_TOKENS.has(type)) {
  284. return 'video';
  285. }
  286. }
  287. const extension =
  288. (input.name ? getPathExtension(input.name) : '') ||
  289. (input.path ? getPathExtension(input.path) : '');
  290. if (extension) {
  291. if (KIND_IMAGE_EXTENSIONS.has(extension)) {
  292. return 'image';
  293. }
  294. if (KIND_AUDIO_EXTENSIONS.has(extension)) {
  295. return 'audio';
  296. }
  297. if (KIND_VIDEO_EXTENSIONS.has(extension)) {
  298. return 'video';
  299. }
  300. }
  301. return 'file';
  302. }