@@ -33,6 +33,9 @@ import {
3333import { randomUUID } from 'crypto' ;
3434import { isString } from '../utils' ;
3535import { mimeTypes } from './constants' ;
36+ import { MimeTypeHandler } from './mimeTypeHandler' ;
37+ import { ExecutionSummary } from './executionSummary' ;
38+
3639
3740export function base64ToUint8Array ( base64 : string ) : Uint8Array {
3841 if ( typeof Buffer !== 'undefined' && typeof Buffer . from === 'function' ) {
@@ -56,15 +59,6 @@ export function uint8ArrayToBase64(data: Uint8Array): string {
5659 return btoa ( binary ) ;
5760}
5861
59- export function createOutputItem ( data : string | Uint8Array , mimeType : string ) : vscode . NotebookCellOutputItem {
60- if ( mimeType . startsWith ( 'image/' ) ) {
61- const bytes = typeof data === 'string' ? base64ToUint8Array ( data ) : data ;
62- return new vscode . NotebookCellOutputItem ( bytes , mimeType ) ;
63- }
64- const text = typeof data === 'string' ? data : new TextDecoder ( ) . decode ( data ) ;
65- return vscode . NotebookCellOutputItem . text ( text , mimeType ) ;
66- }
67-
6862export const createErrorOutput = ( err : string | Error ) => {
6963 return new vscode . NotebookCellOutput ( [ createErrorOutputItem ( err ) ] ) ;
7064}
@@ -86,14 +80,13 @@ export function parseCell(cell: ICell): vscode.NotebookCellData {
8680 const cellData = new vscode . NotebookCellData ( kind , value , language ) ;
8781 cellData . metadata = { id : cell . id , ...cell . metadata } ;
8882 if ( cell . cell_type === 'code' ) {
89-
90- const metaExec = ( cell . metadata as IMetadata ) . executionSummary ;
91- const executionOrder = metaExec ?. executionOrder ?? cell . execution_count ?? undefined ;
92- const success = metaExec ?. success ?? undefined ;
93-
83+ const execSummary = ExecutionSummary . fromMetadata (
84+ ( cell . metadata as IMetadata ) . executionSummary ,
85+ cell . execution_count ?? null
86+ ) ;
9487 cellData . executionSummary = {
95- executionOrder,
96- success,
88+ executionOrder : execSummary . executionOrder ?? undefined ,
89+ success : execSummary . success ,
9790 } ;
9891
9992 if ( Array . isArray ( cell . outputs ) ) {
@@ -119,7 +112,7 @@ export function parseOutput(raw: IOutput): vscode.NotebookCellOutput[] {
119112 case 'stream' :
120113 outputs . push (
121114 new vscode . NotebookCellOutput ( [
122- vscode . NotebookCellOutputItem . text ( Array . isArray ( raw . text ) ? raw . text . join ( '' ) : raw . text ) ,
115+ new MimeTypeHandler ( mimeTypes . TEXT ) . makeOutputItem ( Array . isArray ( raw . text ) ? raw . text . join ( '' ) : raw . text ) ,
123116 ] )
124117 ) ;
125118 break ;
@@ -138,19 +131,8 @@ export function parseOutput(raw: IOutput): vscode.NotebookCellOutput[] {
138131
139132 case 'display_data' :
140133 case 'execute_result' :
141- const items : vscode . NotebookCellOutputItem [ ] = [ ] ;
142- const bundle = raw . data || { } ;
143- for ( const mime in bundle ) {
144- const data = bundle [ mime ] ;
145- if ( mime === mimeTypes . TEXT ) {
146- const text = Array . isArray ( data ) ? data . join ( '' ) : String ( data ) ;
147- items . push ( vscode . NotebookCellOutputItem . text ( text ) ) ;
148- } else if ( ( mime as string ) . startsWith ( 'image/' ) ) {
149- const b64 = Array . isArray ( data ) ? data . join ( '' ) : String ( data ) ;
150- const bytes = base64ToUint8Array ( b64 ) ;
151- items . push ( new vscode . NotebookCellOutputItem ( bytes , mime ) ) ;
152- }
153- }
134+ const bundle = raw . data ?? { } ;
135+ const items = MimeTypeHandler . itemsFromBundle ( bundle ) ;
154136 if ( items . length ) {
155137 outputs . push ( new vscode . NotebookCellOutput ( items , raw . metadata ) ) ;
156138 }
@@ -168,7 +150,8 @@ export function serializeCell(cell: vscode.NotebookCellData): ICell {
168150 const executionCount = exec . executionOrder ?? null ;
169151 const success = exec . success ?? false ;
170152
171- const metadata = { ...baseMeta , executionSummary : { executionOrder : executionCount , success } } ;
153+ const execSummary = new ExecutionSummary ( executionCount , success ) ;
154+ const metadata = { ...baseMeta , executionSummary : execSummary . toJSON ( ) } ;
172155
173156 const outputs : IOutput [ ] = ( cell . outputs || [ ] ) . map ( ( output ) : IOutput => {
174157 const data : IMimeBundle = { } ;
0 commit comments