view.js 260 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227
  1. import * as base from './base.js';
  2. import * as flatbuffers from './flatbuffers.js';
  3. import * as grapher from './grapher.js';
  4. import * as hdf5 from './hdf5.js';
  5. import * as json from './json.js';
  6. import * as protobuf from './protobuf.js';
  7. import * as python from './python.js';
  8. import * as tar from './tar.js';
  9. import * as text from './text.js';
  10. import * as xml from './xml.js';
  11. import * as zip from './zip.js';
  12. const view = {};
  13. const markdown = {};
  14. const metrics = {};
  15. view.View = class {
  16. constructor(host) {
  17. this._host = host;
  18. this._defaultOptions = {
  19. weights: true,
  20. attributes: false,
  21. names: false,
  22. direction: 'vertical',
  23. mousewheel: 'scroll'
  24. };
  25. this._options = { ...this._defaultOptions };
  26. this._model = null;
  27. this._stack = [];
  28. this._selection = [];
  29. this._sidebar = new view.Sidebar(this._host);
  30. this._find = null;
  31. this._modelFactoryService = new view.ModelFactoryService(this._host);
  32. this._modelFactoryService.import();
  33. this._worker = this._host.environment('measure') ? null : new view.Worker(this._host);
  34. }
  35. async start() {
  36. try {
  37. await zip.Archive.import();
  38. await this._host.view(this);
  39. const options = this._host.get('options') || {};
  40. for (const [name, value] of Object.entries(options)) {
  41. this._options[name] = value;
  42. }
  43. this._element('sidebar-button').addEventListener('click', () => {
  44. this.showModelProperties();
  45. });
  46. this._element('zoom-in-button').addEventListener('click', () => {
  47. this.zoomIn();
  48. });
  49. this._element('zoom-out-button').addEventListener('click', () => {
  50. this.zoomOut();
  51. });
  52. this._element('toolbar-path-back-button').addEventListener('click', async () => {
  53. await this.popGraph();
  54. });
  55. this._element('sidebar').addEventListener('mousewheel', (e) => {
  56. if (e.shiftKey || e.ctrlKey) {
  57. e.preventDefault();
  58. }
  59. }, { passive: true });
  60. this._host.document.addEventListener('keydown', () => {
  61. if (this._graph) {
  62. this._graph.select(null);
  63. }
  64. });
  65. if (this._host.type === 'Electron') {
  66. this._host.update({ 'can-copy': false });
  67. this._host.document.addEventListener('selectionchange', () => {
  68. const selection = this._host.document.getSelection();
  69. const selected = selection.rangeCount === 0 || selection.toString().trim() !== '';
  70. this._host.update({ 'can-copy': selected });
  71. });
  72. }
  73. const platform = this._host.environment('platform');
  74. this._menu = new view.Menu(this._host);
  75. this._menu.add({
  76. accelerator: platform === 'darwin' ? 'Ctrl+Cmd+F' : 'F11',
  77. execute: async () => await this._host.execute('fullscreen')
  78. });
  79. this._menu.add({
  80. accelerator: 'Backspace',
  81. execute: async () => await this.popGraph()
  82. });
  83. if (this._host.environment('menu')) {
  84. this._menu.attach(this._element('menu'), this._element('menu-button'));
  85. const file = this._menu.group('&File');
  86. file.add({
  87. label: '&Open...',
  88. accelerator: 'CmdOrCtrl+O',
  89. execute: async () => await this._host.execute('open')
  90. });
  91. if (this._host.type === 'Electron') {
  92. this._recents = file.group('Open &Recent');
  93. file.add({
  94. label: '&Export...',
  95. accelerator: 'CmdOrCtrl+Shift+E',
  96. execute: async () => await this._host.execute('export'),
  97. enabled: () => this.activeGraph
  98. });
  99. file.add({
  100. label: platform === 'darwin' ? '&Close Window' : '&Close',
  101. accelerator: 'CmdOrCtrl+W',
  102. execute: async () => await this._host.execute('close'),
  103. });
  104. file.add({
  105. label: platform === 'win32' ? 'E&xit' : '&Quit',
  106. accelerator: platform === 'win32' ? '' : 'CmdOrCtrl+Q',
  107. execute: async () => await this._host.execute('quit'),
  108. });
  109. } else {
  110. file.add({
  111. label: 'Export as &PNG',
  112. accelerator: 'CmdOrCtrl+Shift+E',
  113. execute: async () => await this.export(`${this._host.document.title}.png`),
  114. enabled: () => this.activeGraph
  115. });
  116. file.add({
  117. label: 'Export as &SVG',
  118. accelerator: 'CmdOrCtrl+Alt+E',
  119. execute: async () => await this.export(`${this._host.document.title}.svg`),
  120. enabled: () => this.activeGraph
  121. });
  122. }
  123. const edit = this._menu.group('&Edit');
  124. edit.add({
  125. label: '&Find...',
  126. accelerator: 'CmdOrCtrl+F',
  127. execute: () => this.find(),
  128. enabled: () => this.activeGraph
  129. });
  130. const view = this._menu.group('&View');
  131. view.add({
  132. label: () => this.options.attributes ? 'Hide &Attributes' : 'Show &Attributes',
  133. accelerator: 'CmdOrCtrl+D',
  134. execute: () => this.toggle('attributes'),
  135. enabled: () => this.activeGraph
  136. });
  137. view.add({
  138. label: () => this.options.weights ? 'Hide &Weights' : 'Show &Weights',
  139. accelerator: 'CmdOrCtrl+I',
  140. execute: () => this.toggle('weights'),
  141. enabled: () => this.activeGraph
  142. });
  143. view.add({
  144. label: () => this.options.names ? 'Hide &Names' : 'Show &Names',
  145. accelerator: 'CmdOrCtrl+U',
  146. execute: () => this.toggle('names'),
  147. enabled: () => this.activeGraph
  148. });
  149. view.add({
  150. label: () => this.options.direction === 'vertical' ? 'Show &Horizontal' : 'Show &Vertical',
  151. accelerator: 'CmdOrCtrl+K',
  152. execute: () => this.toggle('direction'),
  153. enabled: () => this.activeGraph
  154. });
  155. view.add({
  156. label: () => this.options.mousewheel === 'scroll' ? '&Mouse Wheel: Zoom' : '&Mouse Wheel: Scroll',
  157. accelerator: 'CmdOrCtrl+M',
  158. execute: () => this.toggle('mousewheel'),
  159. enabled: () => this.activeGraph
  160. });
  161. view.add({});
  162. if (this._host.type === 'Electron') {
  163. view.add({
  164. label: '&Reload',
  165. accelerator: platform === 'darwin' ? 'CmdOrCtrl+R' : 'F5',
  166. execute: async () => await this._host.execute('reload'),
  167. enabled: () => this.activeGraph
  168. });
  169. view.add({});
  170. }
  171. view.add({
  172. label: 'Zoom &In',
  173. accelerator: 'Shift+Up',
  174. execute: () => this.zoomIn(),
  175. enabled: () => this.activeGraph
  176. });
  177. view.add({
  178. label: 'Zoom &Out',
  179. accelerator: 'Shift+Down',
  180. execute: () => this.zoomOut(),
  181. enabled: () => this.activeGraph
  182. });
  183. view.add({
  184. label: 'Actual &Size',
  185. accelerator: 'Shift+Backspace',
  186. execute: () => this.resetZoom(),
  187. enabled: () => this.activeGraph
  188. });
  189. view.add({});
  190. view.add({
  191. label: '&Properties...',
  192. accelerator: 'CmdOrCtrl+Enter',
  193. execute: () => this.showModelProperties(),
  194. enabled: () => this.activeGraph
  195. });
  196. if (this._host.type === 'Electron' && !this._host.environment('packaged')) {
  197. view.add({});
  198. view.add({
  199. label: '&Developer Tools...',
  200. accelerator: 'CmdOrCtrl+Alt+I',
  201. execute: async () => await this._host.execute('toggle-developer-tools')
  202. });
  203. }
  204. const help = this._menu.group('&Help');
  205. help.add({
  206. label: 'Report &Issue',
  207. execute: async () => await this._host.execute('report-issue')
  208. });
  209. help.add({
  210. label: `&About ${this._host.environment('name')}`,
  211. execute: async () => await this._host.execute('about')
  212. });
  213. }
  214. await this._host.start();
  215. } catch (error) {
  216. this.error(error, null, null);
  217. }
  218. }
  219. get host() {
  220. return this._host;
  221. }
  222. show(page) {
  223. if (!page) {
  224. page = (!this._model && !this.activeGraph) ? 'welcome' : 'default';
  225. }
  226. this._host.event('screen_view', {
  227. screen_name: page,
  228. });
  229. if (this._sidebar) {
  230. this._sidebar.close();
  231. }
  232. if (this._menu) {
  233. this._menu.close();
  234. }
  235. this._host.document.body.classList.remove(...Array.from(this._host.document.body.classList).filter((_) => _ !== 'active'));
  236. this._host.document.body.classList.add(...page.split(' '));
  237. if (page === 'default') {
  238. this._activate();
  239. } else {
  240. this._deactivate();
  241. }
  242. if (page === 'welcome') {
  243. const element = this._element('open-file-button');
  244. if (element) {
  245. element.focus();
  246. }
  247. }
  248. this._page = page;
  249. }
  250. progress(percent) {
  251. const bar = this._element('progress-bar');
  252. if (bar) {
  253. bar.style.width = `${percent}%`;
  254. }
  255. }
  256. cut() {
  257. this._host.document.execCommand('cut');
  258. }
  259. copy() {
  260. this._host.document.execCommand('copy');
  261. }
  262. paste() {
  263. this._host.document.execCommand('paste');
  264. }
  265. selectAll() {
  266. this._host.document.execCommand('selectall');
  267. }
  268. find() {
  269. if (this._graph) {
  270. this._graph.select(null);
  271. const sidebar = new view.FindSidebar(this, this.activeGraph, this.activeSignature);
  272. sidebar.on('state-changed', (sender, state) => {
  273. this._find = state;
  274. });
  275. sidebar.on('select', (sender, value) => {
  276. this.scrollTo(this._graph.select([value]));
  277. });
  278. sidebar.on('focus', (sender, value) => {
  279. this._graph.focus([value]);
  280. });
  281. sidebar.on('blur', (sender, value) => {
  282. this._graph.blur([value]);
  283. });
  284. sidebar.on('activate', (sender, value) => {
  285. this.scrollTo(this._graph.activate(value));
  286. });
  287. this._sidebar.open(sidebar, 'Find');
  288. sidebar.focus(this._find);
  289. }
  290. }
  291. get model() {
  292. return this._model;
  293. }
  294. get options() {
  295. return this._options;
  296. }
  297. toggle(name) {
  298. switch (name) {
  299. case 'names':
  300. case 'attributes':
  301. case 'weights':
  302. this._options[name] = !this._options[name];
  303. this._reload();
  304. break;
  305. case 'direction':
  306. this._options.direction = this._options.direction === 'vertical' ? 'horizontal' : 'vertical';
  307. this._reload();
  308. break;
  309. case 'mousewheel':
  310. this._options.mousewheel = this._options.mousewheel === 'scroll' ? 'zoom' : 'scroll';
  311. break;
  312. default:
  313. throw new view.Error(`Unsupported toggle '${name}'.`);
  314. }
  315. const options = {};
  316. for (const [name, value] of Object.entries(this._options)) {
  317. if (this._defaultOptions[name] !== value) {
  318. options[name] = value;
  319. }
  320. }
  321. if (Object.entries(options).length === 0) {
  322. this._host.delete('options');
  323. } else {
  324. this._host.set('options', options);
  325. }
  326. }
  327. recents(recents) {
  328. if (this._recents) {
  329. this._recents.clear();
  330. for (let i = 0; i < recents.length; i++) {
  331. const recent = recents[i];
  332. this._recents.add({
  333. label: recent.label,
  334. accelerator: `CmdOrCtrl+${(i + 1)}`,
  335. execute: () => this._host.execute('open', recent.path)
  336. });
  337. }
  338. }
  339. }
  340. _reload() {
  341. this.show('welcome spinner');
  342. if (this._model && this._stack.length > 0) {
  343. this._updateGraph(this._model, this._stack).catch((error) => {
  344. if (error) {
  345. this.error(error, 'Graph update failed.', 'welcome');
  346. }
  347. });
  348. }
  349. }
  350. _timeout(delay) {
  351. return new Promise((resolve) => {
  352. setTimeout(resolve, delay);
  353. });
  354. }
  355. _element(id) {
  356. return this._host.document.getElementById(id);
  357. }
  358. zoomIn() {
  359. this._updateZoom(this._zoom * 1.1);
  360. }
  361. zoomOut() {
  362. this._updateZoom(this._zoom * 0.9);
  363. }
  364. resetZoom() {
  365. this._updateZoom(1);
  366. }
  367. _activate() {
  368. if (!this._events) {
  369. this._events = {};
  370. this._events.scroll = (e) => this._scrollHandler(e);
  371. this._events.wheel = (e) => this._wheelHandler(e);
  372. this._events.gesturestart = (e) => this._gestureStartHandler(e);
  373. this._events.pointerdown = (e) => this._pointerDownHandler(e);
  374. this._events.touchstart = (e) => this._touchStartHandler(e);
  375. }
  376. const graph = this._element('graph');
  377. graph.focus();
  378. graph.addEventListener('scroll', this._events.scroll);
  379. graph.addEventListener('wheel', this._events.wheel, { passive: false });
  380. graph.addEventListener('pointerdown', this._events.pointerdown);
  381. if (this._host.environment('agent') === 'safari') {
  382. graph.addEventListener('gesturestart', this._events.gesturestart, false);
  383. } else {
  384. graph.addEventListener('touchstart', this._events.touchstart, { passive: true });
  385. }
  386. }
  387. _deactivate() {
  388. if (this._events) {
  389. const graph = this._element('graph');
  390. graph.removeEventListener('scroll', this._events.scroll);
  391. graph.removeEventListener('wheel', this._events.wheel);
  392. graph.removeEventListener('pointerdown', this._events.pointerdown);
  393. graph.removeEventListener('gesturestart', this._events.gesturestart);
  394. graph.removeEventListener('touchstart', this._events.touchstart);
  395. }
  396. }
  397. _updateZoom(zoom, e) {
  398. const container = this._element('graph');
  399. const canvas = this._element('canvas');
  400. const limit = this._options.direction === 'vertical' ?
  401. container.clientHeight / this._height :
  402. container.clientWidth / this._width;
  403. const min = Math.min(Math.max(limit, 0.15), 1);
  404. zoom = Math.max(min, Math.min(zoom, 1.4));
  405. const scrollLeft = this._scrollLeft || container.scrollLeft;
  406. const scrollTop = this._scrollTop || container.scrollTop;
  407. const x = (e ? e.pageX : (container.clientWidth / 2)) + scrollLeft;
  408. const y = (e ? e.pageY : (container.clientHeight / 2)) + scrollTop;
  409. const width = zoom * this._width;
  410. const height = zoom * this._height;
  411. canvas.style.width = `${width}px`;
  412. canvas.style.height = `${height}px`;
  413. this._scrollLeft = Math.max(0, ((x * zoom) / this._zoom) - (x - scrollLeft));
  414. this._scrollTop = Math.max(0, ((y * zoom) / this._zoom) - (y - scrollTop));
  415. container.scrollLeft = this._scrollLeft;
  416. container.scrollTop = this._scrollTop;
  417. this._zoom = zoom;
  418. }
  419. _pointerDownHandler(e) {
  420. if (e.pointerType === 'touch' || e.buttons !== 1) {
  421. return;
  422. }
  423. const container = this._element('graph');
  424. if (e.target === container) {
  425. return;
  426. }
  427. e.target.setPointerCapture(e.pointerId);
  428. this._mousePosition = {
  429. left: container.scrollLeft,
  430. top: container.scrollTop,
  431. x: e.clientX,
  432. y: e.clientY
  433. };
  434. container.style.cursor = 'grabbing';
  435. e.preventDefault();
  436. e.stopImmediatePropagation();
  437. const pointerMoveHandler = (e) => {
  438. e.preventDefault();
  439. e.stopImmediatePropagation();
  440. if (this._mousePosition) {
  441. const dx = e.clientX - this._mousePosition.x;
  442. const dy = e.clientY - this._mousePosition.y;
  443. this._mousePosition.moved = dx * dx + dy * dy > 0;
  444. if (this._mousePosition.moved) {
  445. const container = this._element('graph');
  446. container.scrollTop = this._mousePosition.top - dy;
  447. container.scrollLeft = this._mousePosition.left - dx;
  448. }
  449. }
  450. };
  451. const clickHandler = (e) => {
  452. e.stopPropagation();
  453. document.removeEventListener('click', clickHandler, true);
  454. };
  455. const pointerUpHandler = (e) => {
  456. e.target.releasePointerCapture(e.pointerId);
  457. container.style.removeProperty('cursor');
  458. container.removeEventListener('pointerup', pointerUpHandler);
  459. container.removeEventListener('pointermove', pointerMoveHandler);
  460. if (this._mousePosition && this._mousePosition.moved) {
  461. e.preventDefault();
  462. e.stopImmediatePropagation();
  463. delete this._mousePosition;
  464. document.addEventListener('click', clickHandler, true);
  465. }
  466. };
  467. container.addEventListener('pointermove', pointerMoveHandler);
  468. container.addEventListener('pointerup', pointerUpHandler);
  469. }
  470. _touchStartHandler(e) {
  471. if (e.touches.length === 2) {
  472. this._touchPoints = Array.from(e.touches);
  473. this._touchZoom = this._zoom;
  474. }
  475. const touchMoveHandler = (e) => {
  476. if (Array.isArray(this._touchPoints) && this._touchPoints.length === 2 && e.touches.length === 2) {
  477. const distance = (points) => {
  478. const dx = (points[1].clientX - points[0].clientX);
  479. const dy = (points[1].clientY - points[0].clientY);
  480. return Math.sqrt(dx * dx + dy * dy);
  481. };
  482. const d1 = distance(Array.from(e.touches));
  483. const d2 = distance(this._touchPoints);
  484. if (d2 !== 0) {
  485. const points = this._touchPoints;
  486. const e = {
  487. pageX: (points[1].pageX + points[0].pageX) / 2,
  488. pageY: (points[1].pageY + points[0].pageY) / 2
  489. };
  490. const zoom = d2 === 0 ? d1 : d1 / d2;
  491. this._updateZoom(this._touchZoom * zoom, e);
  492. }
  493. }
  494. };
  495. const container = this._element('graph');
  496. const touchEndHandler = () => {
  497. container.removeEventListener('touchmove', touchMoveHandler, { passive: true });
  498. container.removeEventListener('touchcancel', touchEndHandler, { passive: true });
  499. container.removeEventListener('touchend', touchEndHandler, { passive: true });
  500. delete this._touchPoints;
  501. delete this._touchZoom;
  502. };
  503. container.addEventListener('touchmove', touchMoveHandler, { passive: true });
  504. container.addEventListener('touchcancel', touchEndHandler, { passive: true });
  505. container.addEventListener('touchend', touchEndHandler, { passive: true });
  506. }
  507. _gestureStartHandler(e) {
  508. e.preventDefault();
  509. this._gestureZoom = this._zoom;
  510. const container = this._element('graph');
  511. const gestureChangeHandler = (e) => {
  512. e.preventDefault();
  513. this._updateZoom(this._gestureZoom * e.scale, e);
  514. };
  515. const gestureEndHandler = (e) => {
  516. container.removeEventListener('gesturechange', gestureChangeHandler, false);
  517. container.removeEventListener('gestureend', gestureEndHandler, false);
  518. e.preventDefault();
  519. if (this._gestureZoom) {
  520. this._updateZoom(this._gestureZoom * e.scale, e);
  521. delete this._gestureZoom;
  522. }
  523. };
  524. container.addEventListener('gesturechange', gestureChangeHandler, false);
  525. container.addEventListener('gestureend', gestureEndHandler, false);
  526. }
  527. _scrollHandler(e) {
  528. if (this._scrollLeft && e.target.scrollLeft !== Math.floor(this._scrollLeft)) {
  529. delete this._scrollLeft;
  530. }
  531. if (this._scrollTop && e.target.scrollTop !== Math.floor(this._scrollTop)) {
  532. delete this._scrollTop;
  533. }
  534. }
  535. _wheelHandler(e) {
  536. if (e.shiftKey || e.ctrlKey || this._options.mousewheel === 'zoom') {
  537. let factor = 1;
  538. if (e.deltaMode === 1) {
  539. factor = 0.05;
  540. } else if (e.deltaMode) {
  541. factor = 1;
  542. } else {
  543. factor = 0.002;
  544. }
  545. const delta = -e.deltaY * factor * (e.ctrlKey ? 10 : 1);
  546. this._updateZoom(this._zoom * Math.pow(2, delta), e);
  547. e.preventDefault();
  548. }
  549. }
  550. scrollTo(selection) {
  551. if (selection && selection.length > 0) {
  552. const container = this._element('graph');
  553. let x = 0;
  554. let y = 0;
  555. for (const element of selection) {
  556. const rect = element.getBoundingClientRect();
  557. x += rect.left + (rect.width / 2);
  558. y += rect.top + (rect.height / 2);
  559. }
  560. x /= selection.length;
  561. y /= selection.length;
  562. const rect = container.getBoundingClientRect();
  563. const left = (container.scrollLeft + x - rect.left) - (rect.width / 2);
  564. const top = (container.scrollTop + y - rect.top) - (rect.height / 2);
  565. container.scrollTo({ left, top, behavior: 'smooth' });
  566. }
  567. }
  568. async error(error, name, screen) {
  569. if (this._sidebar) {
  570. this._sidebar.close();
  571. }
  572. this.exception(error, false);
  573. const knowns = [
  574. { name: '', message: /^Invalid value identifier/, url: 'https://github.com/lutzroeder/netron/issues/540' },
  575. { name: '', message: /^Cannot read property/, url: 'https://github.com/lutzroeder/netron/issues/647' },
  576. { name: 'Error', message: /^EPERM: operation not permitted/, url: 'https://github.com/lutzroeder/netron/issues/551' },
  577. { name: 'Error', message: /^EACCES: permission denied/, url: 'https://github.com/lutzroeder/netron/issues/504' },
  578. { name: 'RangeError', message: /^Offset is outside the bounds of the DataView/, url: 'https://github.com/lutzroeder/netron/issues/563' },
  579. { name: 'RangeError', message: /^Invalid string length/, url: 'https://github.com/lutzroeder/netron/issues/648' },
  580. { name: 'Python Error', message: /^Unknown function/, url: 'https://github.com/lutzroeder/netron/issues/546' },
  581. { name: 'Error loading model.', message: /^Unsupported file content \(/, url: 'https://github.com/lutzroeder/netron/issues/550' },
  582. { name: 'Error loading model.', message: /^Unsupported Protocol Buffers content/, url: 'https://github.com/lutzroeder/netron/issues/593' },
  583. { name: 'Error loading model.', message: /^Unsupported Protocol Buffers text content/, url: 'https://github.com/lutzroeder/netron/issues/594' },
  584. { name: 'Error loading model.', message: /^Unsupported JSON content/, url: 'https://github.com/lutzroeder/netron/issues/595' },
  585. { name: 'Error loading PyTorch model.', message: /^File does not contain root module or state dictionary/, url: 'https://github.com/lutzroeder/netron/issues/543' },
  586. { name: 'Error loading PyTorch model.', message: /^Module does not contain modules/, url: 'https://github.com/lutzroeder/netron/issues/544' },
  587. { name: 'Error loading PyTorch model.', message: /^Unknown type name/, url: 'https://github.com/lutzroeder/netron/issues/969' },
  588. { name: 'Error loading ONNX model.', message: /^File format is not onnx\.ModelProto \(Unexpected end of file\)\./, url: 'https://github.com/lutzroeder/netron/issues/1155' },
  589. { name: 'Error loading ONNX model.', message: /^File format is not onnx\.ModelProto \(Cannot read properties of undefined \(reading 'ModelProto'\)\)\./, url: 'https://github.com/lutzroeder/netron/issues/1156' },
  590. { name: 'Error loading ONNX model.', message: /^File format is not onnx\.ModelProto/, url: 'https://github.com/lutzroeder/netron/issues/549' }
  591. ];
  592. const known = knowns.find((known) => (known.name.length === 0 || known.name === error.name) && error.message.match(known.message));
  593. const url = known && known.url ? known.url : null;
  594. const message = error.message;
  595. name = name || error.name;
  596. const report = url ? true : false;
  597. await this._host.message(message, true, report ? 'Report' : 'OK');
  598. if (report) {
  599. this._host.openURL(url || `${this._host.environment('repository')}/issues`);
  600. }
  601. this.show(screen);
  602. }
  603. accept(file, size) {
  604. return this._modelFactoryService.accept(file, size);
  605. }
  606. async open(context) {
  607. this._sidebar.close();
  608. await this._timeout(2);
  609. try {
  610. const model = await this._modelFactoryService.open(context);
  611. const format = [];
  612. if (model.format) {
  613. format.push(model.format);
  614. }
  615. if (model.producer) {
  616. format.push(`(${model.producer})`);
  617. }
  618. if (format.length > 0) {
  619. this._host.event('model_open', {
  620. model_format: model.format || '',
  621. model_producer: model.producer || ''
  622. });
  623. }
  624. await this._timeout(20);
  625. const stack = [];
  626. if (Array.isArray(model.graphs) && model.graphs.length > 0) {
  627. const [graph] = model.graphs;
  628. const entry = {
  629. graph,
  630. signature: Array.isArray(graph.signatures) && graph.signatures.length > 0 ? graph.signatures[0] : null
  631. };
  632. stack.push(entry);
  633. }
  634. return await this._updateGraph(model, stack);
  635. } catch (error) {
  636. error.context = !error.context && context && context.identifier ? context.identifier : error.context || '';
  637. throw error;
  638. }
  639. }
  640. async _updateActive(stack) {
  641. this._sidebar.close();
  642. if (this._model) {
  643. this.show('welcome spinner');
  644. await this._timeout(200);
  645. try {
  646. await this._updateGraph(this._model, stack);
  647. } catch (error) {
  648. if (error) {
  649. this.error(error, 'Graph update failed.', 'welcome');
  650. }
  651. }
  652. }
  653. }
  654. get activeGraph() {
  655. if (Array.isArray(this._stack) && this._stack.length > 0) {
  656. return this._stack[0].graph;
  657. }
  658. return null;
  659. }
  660. get activeSignature() {
  661. if (Array.isArray(this._stack) && this._stack.length > 0) {
  662. return this._stack[0].signature;
  663. }
  664. return null;
  665. }
  666. async _updateGraph(model, stack) {
  667. const update = async (model, stack) => {
  668. this._model = model;
  669. this._stack = stack;
  670. const status = await this.renderGraph(this._model, this.activeGraph, this.activeSignature, this._options);
  671. if (status !== '') {
  672. this._model = null;
  673. this._stack = [];
  674. this._activeGraph = null;
  675. }
  676. this.show(null);
  677. const path = this._element('toolbar-path');
  678. const back = this._element('toolbar-path-back-button');
  679. while (path.children.length > 1) {
  680. path.removeChild(path.lastElementChild);
  681. }
  682. if (status === '') {
  683. if (this._stack.length <= 1) {
  684. back.style.opacity = 0;
  685. } else {
  686. back.style.opacity = 1;
  687. const last = this._stack.length - 2;
  688. const count = Math.min(2, last);
  689. if (count < last) {
  690. const element = this._host.document.createElement('button');
  691. element.setAttribute('class', 'toolbar-path-name-button');
  692. element.innerHTML = '&hellip;';
  693. path.appendChild(element);
  694. }
  695. for (let i = count; i >= 0; i--) {
  696. const graph = this._stack[i].graph;
  697. const element = this._host.document.createElement('button');
  698. element.setAttribute('class', 'toolbar-path-name-button');
  699. element.addEventListener('click', async () => {
  700. if (i > 0) {
  701. this._stack = this._stack.slice(i);
  702. await this._updateGraph(this._model, this._stack);
  703. } else {
  704. await this.showDefinition(graph);
  705. }
  706. });
  707. let name = '';
  708. if (graph && graph.identifier) {
  709. name = graph.identifier;
  710. } else if (graph && graph.name) {
  711. name = graph.name;
  712. }
  713. if (name.length > 24) {
  714. element.setAttribute('title', name);
  715. element.innerHTML = `&hellip;${name.substring(name.length - 24, name.length)}`;
  716. } else {
  717. element.removeAttribute('title');
  718. element.innerHTML = name;
  719. }
  720. path.appendChild(element);
  721. }
  722. }
  723. }
  724. };
  725. const lastModel = this._model;
  726. const lastStack = this._stack;
  727. try {
  728. await update(model, stack);
  729. return this._model;
  730. } catch (error) {
  731. await update(lastModel, lastStack);
  732. throw error;
  733. }
  734. }
  735. async pushGraph(graph) {
  736. if (graph && graph !== this.activeGraph && Array.isArray(graph.nodes)) {
  737. this._sidebar.close();
  738. const signature = Array.isArray(graph.signatures) && graph.signatures.length > 0 ? graph.signatures[0] : null;
  739. const entry = { graph, signature };
  740. const stack = [entry].concat(this._stack);
  741. await this._updateGraph(this._model, stack);
  742. }
  743. }
  744. async popGraph() {
  745. if (this._stack.length > 1) {
  746. this._sidebar.close();
  747. return await this._updateGraph(this._model, this._stack.slice(1));
  748. }
  749. return null;
  750. }
  751. async renderGraph(model, graph, signature, options) {
  752. this._graph = null;
  753. const canvas = this._element('canvas');
  754. while (canvas.lastChild) {
  755. canvas.removeChild(canvas.lastChild);
  756. }
  757. if (!graph) {
  758. return '';
  759. }
  760. this._zoom = 1;
  761. const groups = graph.groups || false;
  762. const nodes = graph.nodes;
  763. this._host.event('graph_view', {
  764. graph_node_count: nodes.length,
  765. graph_skip: 0
  766. });
  767. const layout = {};
  768. layout.nodesep = 20;
  769. layout.ranksep = 20;
  770. const rotate = graph.nodes.every((node) => {
  771. if (node.inputs.filter((input) => !input.type || input.type.endsWith('*')).some((input) => input.value.every((value) => !value.initializer))) {
  772. return false;
  773. }
  774. if (node.outputs.length > 0) {
  775. return false;
  776. }
  777. return true;
  778. });
  779. const horizontal = rotate ? options.direction === 'vertical' : options.direction !== 'vertical';
  780. if (horizontal) {
  781. layout.rankdir = 'LR';
  782. }
  783. if (nodes.length > 3000) {
  784. layout.ranker = 'longest-path';
  785. }
  786. const viewGraph = new view.Graph(this, this._host, model, options, groups, layout);
  787. viewGraph.add(graph, signature);
  788. // Workaround for Safari background drag/zoom issue:
  789. // https://stackoverflow.com/questions/40887193/d3-js-zoom-is-not-working-with-mousewheel-in-safari
  790. const background = this._host.document.createElementNS('http://www.w3.org/2000/svg', 'rect');
  791. background.setAttribute('id', 'background');
  792. background.setAttribute('fill', 'none');
  793. background.setAttribute('pointer-events', 'all');
  794. canvas.appendChild(background);
  795. const origin = this._host.document.createElementNS('http://www.w3.org/2000/svg', 'g');
  796. origin.setAttribute('id', 'origin');
  797. canvas.appendChild(origin);
  798. viewGraph.build(this._host.document, origin);
  799. await this._timeout(20);
  800. viewGraph.measure();
  801. const status = await viewGraph.layout(this._worker);
  802. if (status === '') {
  803. viewGraph.update();
  804. const elements = Array.from(canvas.getElementsByClassName('graph-input') || []);
  805. if (elements.length === 0) {
  806. const nodeElements = Array.from(canvas.getElementsByClassName('graph-node') || []);
  807. if (nodeElements.length > 0) {
  808. elements.push(nodeElements[0]);
  809. }
  810. }
  811. const size = canvas.getBBox();
  812. const margin = 100;
  813. const width = Math.ceil(margin + size.width + margin);
  814. const height = Math.ceil(margin + size.height + margin);
  815. origin.setAttribute('transform', `translate(${margin}, ${margin}) scale(1)`);
  816. background.setAttribute('width', width);
  817. background.setAttribute('height', height);
  818. this._width = width;
  819. this._height = height;
  820. delete this._scrollLeft;
  821. delete this._scrollRight;
  822. canvas.setAttribute('viewBox', `0 0 ${width} ${height}`);
  823. canvas.setAttribute('width', width);
  824. canvas.setAttribute('height', height);
  825. this._zoom = 1;
  826. this._updateZoom(this._zoom);
  827. const container = this._element('graph');
  828. if (elements && elements.length > 0) {
  829. // Center view based on input elements
  830. const xs = [];
  831. const ys = [];
  832. for (let i = 0; i < elements.length; i++) {
  833. const element = elements[i];
  834. const rect = element.getBoundingClientRect();
  835. xs.push(rect.left + (rect.width / 2));
  836. ys.push(rect.top + (rect.height / 2));
  837. }
  838. let [x] = xs;
  839. const [y] = ys;
  840. if (ys.every((y) => y === ys[0])) {
  841. x = xs.reduce((a, b) => a + b, 0) / xs.length;
  842. }
  843. const graphRect = container.getBoundingClientRect();
  844. const left = (container.scrollLeft + x - graphRect.left) - (graphRect.width / 2);
  845. const top = (container.scrollTop + y - graphRect.top) - (graphRect.height / 2);
  846. container.scrollTo({ left, top, behavior: 'auto' });
  847. } else {
  848. const canvasRect = canvas.getBoundingClientRect();
  849. const graphRect = container.getBoundingClientRect();
  850. const left = (container.scrollLeft + (canvasRect.width / 2) - graphRect.left) - (graphRect.width / 2);
  851. const top = (container.scrollTop + (canvasRect.height / 2) - graphRect.top) - (graphRect.height / 2);
  852. container.scrollTo({ left, top, behavior: 'auto' });
  853. }
  854. this._graph = viewGraph;
  855. }
  856. return status;
  857. }
  858. applyStyleSheet(element, name) {
  859. let rules = [];
  860. for (const styleSheet of this._host.document.styleSheets) {
  861. if (styleSheet && styleSheet.href && styleSheet.href.endsWith(`/${name}`)) {
  862. rules = styleSheet.cssRules;
  863. break;
  864. }
  865. }
  866. const nodes = element.getElementsByTagName('*');
  867. for (const node of nodes) {
  868. for (const rule of rules) {
  869. if (node.matches(rule.selectorText)) {
  870. for (const item of rule.style) {
  871. node.style[item] = rule.style[item];
  872. }
  873. }
  874. }
  875. }
  876. }
  877. async export(file) {
  878. const lastIndex = file.lastIndexOf('.');
  879. const extension = lastIndex === -1 ? 'png' : file.substring(lastIndex + 1).toLowerCase();
  880. if (this.activeGraph && (extension === 'png' || extension === 'svg')) {
  881. const canvas = this._element('canvas');
  882. const clone = canvas.cloneNode(true);
  883. this.applyStyleSheet(clone, 'grapher.css');
  884. clone.setAttribute('id', 'export');
  885. clone.removeAttribute('viewBox');
  886. clone.removeAttribute('width');
  887. clone.removeAttribute('height');
  888. clone.style.removeProperty('opacity');
  889. clone.style.removeProperty('display');
  890. clone.style.removeProperty('width');
  891. clone.style.removeProperty('height');
  892. const background = clone.querySelector('#background');
  893. const origin = clone.querySelector('#origin');
  894. origin.setAttribute('transform', 'translate(0,0) scale(1)');
  895. background.removeAttribute('width');
  896. background.removeAttribute('height');
  897. const parent = canvas.parentElement;
  898. parent.insertBefore(clone, canvas);
  899. const size = clone.getBBox();
  900. parent.removeChild(clone);
  901. parent.removeChild(canvas);
  902. parent.appendChild(canvas);
  903. const delta = (Math.min(size.width, size.height) / 2.0) * 0.1;
  904. const width = Math.ceil(delta + size.width + delta);
  905. const height = Math.ceil(delta + size.height + delta);
  906. origin.setAttribute('transform', `translate(${(delta - size.x)}, ${(delta - size.y)}) scale(1)`);
  907. clone.setAttribute('width', width);
  908. clone.setAttribute('height', height);
  909. background.setAttribute('width', width);
  910. background.setAttribute('height', height);
  911. background.setAttribute('fill', '#fff');
  912. const data = new XMLSerializer().serializeToString(clone);
  913. if (extension === 'svg') {
  914. const blob = new Blob([data], { type: 'image/svg' });
  915. await this._host.export(file, blob);
  916. }
  917. if (extension === 'png') {
  918. try {
  919. const blob = await new Promise((resolve, reject) => {
  920. const image = new Image();
  921. image.onload = async () => {
  922. const max = Math.max(width, height);
  923. const scale = Math.min(24000.0 / max, 2.0);
  924. const canvas = this._host.document.createElement('canvas');
  925. canvas.width = Math.ceil(width * scale);
  926. canvas.height = Math.ceil(height * scale);
  927. const context = canvas.getContext('2d');
  928. context.scale(scale, scale);
  929. context.drawImage(image, 0, 0);
  930. canvas.toBlob((blob) => {
  931. if (blob) {
  932. resolve(blob);
  933. } else {
  934. const error = new Error('Image may be too large to render as PNG.');
  935. error.name = 'Error exporting image.';
  936. reject(error);
  937. }
  938. }, 'image/png');
  939. };
  940. image.onerror = (error) => {
  941. reject(error);
  942. };
  943. image.src = `data:image/svg+xml;base64,${this._host.window.btoa(unescape(encodeURIComponent(data)))}`;
  944. });
  945. await this._host.export(file, blob);
  946. } catch (error) {
  947. await this.error(error);
  948. }
  949. }
  950. }
  951. }
  952. showModelProperties() {
  953. if (this._model) {
  954. try {
  955. const sidebar = new view.ModelSidebar(this, this.model, this.activeGraph, this.activeSignature);
  956. sidebar.on('update-active-graph', (sender, graph) => {
  957. const entry = {
  958. graph,
  959. signature: Array.isArray(graph.signatures) && graph.signatures.length > 0 ? graph.signatures[0] : null
  960. };
  961. this._updateActive([entry]);
  962. });
  963. sidebar.on('update-active-graph-signature', (sender, signature) => {
  964. const stack = this._stack.map((entry) => {
  965. return { graph: entry.graph, signature: entry.signature };
  966. });
  967. stack[0].signature = signature;
  968. this._updateActive(stack);
  969. });
  970. sidebar.on('focus', (sender, value) => {
  971. this._graph.focus([value]);
  972. });
  973. sidebar.on('blur', (sender, value) => {
  974. this._graph.blur([value]);
  975. });
  976. sidebar.on('select', (sender, value) => {
  977. this.scrollTo(this._graph.activate(value));
  978. });
  979. sidebar.on('activate', (sender, value) => {
  980. this.scrollTo(this._graph.select([value]));
  981. });
  982. sidebar.on('deactivate', () => {
  983. this._graph.select(null);
  984. });
  985. this._sidebar.open(sidebar, 'Model Properties');
  986. } catch (error) {
  987. this.error(error, 'Error showing model properties.', null);
  988. }
  989. }
  990. }
  991. showNodeProperties(node) {
  992. if (node) {
  993. try {
  994. if (this._menu) {
  995. this._menu.close();
  996. }
  997. const sidebar = new view.NodeSidebar(this, node);
  998. sidebar.on('show-documentation', async (/* sender, e */) => {
  999. await this.showDefinition(node.type);
  1000. });
  1001. sidebar.on('focus', (sender, value) => {
  1002. this._graph.focus([value]);
  1003. });
  1004. sidebar.on('blur', (sender, value) => {
  1005. this._graph.blur([value]);
  1006. });
  1007. sidebar.on('select', (sender, value) => {
  1008. this.scrollTo(this._graph.select([value]));
  1009. });
  1010. sidebar.on('activate', (sender, value) => {
  1011. this.scrollTo(this._graph.activate(value));
  1012. });
  1013. this._sidebar.open(sidebar, 'Node Properties');
  1014. } catch (error) {
  1015. this.error(error, 'Error showing node properties.', null);
  1016. }
  1017. }
  1018. }
  1019. showConnectionProperties(value, from, to) {
  1020. try {
  1021. if (this._menu) {
  1022. this._menu.close();
  1023. }
  1024. const sidebar = new view.ConnectionSidebar(this, value, from, to);
  1025. sidebar.on('focus', (sender, value) => {
  1026. this._graph.focus([value]);
  1027. });
  1028. sidebar.on('blur', (sender, value) => {
  1029. this._graph.blur([value]);
  1030. });
  1031. sidebar.on('select', (sender, value) => {
  1032. this.scrollTo(this._graph.select([value]));
  1033. });
  1034. sidebar.on('activate', (sender, value) => {
  1035. this.scrollTo(this._graph.activate(value));
  1036. });
  1037. this._sidebar.push(sidebar, 'Connection Properties');
  1038. } catch (error) {
  1039. this.error(error, 'Error showing connection properties.', null);
  1040. }
  1041. }
  1042. showTensorProperties(value) {
  1043. try {
  1044. if (this._menu) {
  1045. this._menu.close();
  1046. }
  1047. const sidebar = new view.TensorSidebar(this, value);
  1048. sidebar.on('focus', (sender, value) => {
  1049. this._graph.focus([value]);
  1050. });
  1051. sidebar.on('blur', () => {
  1052. this._graph.blur(null);
  1053. });
  1054. sidebar.on('select', (sender, value) => {
  1055. this.scrollTo(this._graph.select([value]));
  1056. });
  1057. sidebar.on('activate', (sender, value) => {
  1058. this.scrollTo(this._graph.activate(value));
  1059. });
  1060. this._sidebar.push(sidebar, 'Tensor Properties');
  1061. } catch (error) {
  1062. this.error(error, 'Error showing tensor properties.', null);
  1063. }
  1064. }
  1065. exception(error, fatal) {
  1066. if (error && !error.context && this._model && this._model.identifier) {
  1067. error.context = this._model.identifier;
  1068. }
  1069. this._host.exception(error, fatal);
  1070. }
  1071. async showDefinition(type) {
  1072. if (type && (type.description || type.inputs || type.outputs || type.attributes)) {
  1073. if (type.nodes && type.nodes.length > 0) {
  1074. await this.pushGraph(type);
  1075. }
  1076. const sidebar = new view.DocumentationSidebar(this, type);
  1077. sidebar.on('navigate', (sender, e) => {
  1078. this._host.openURL(e.link);
  1079. });
  1080. const title = type.type === 'function' ? 'Function' : 'Documentation';
  1081. this._sidebar.push(sidebar, title);
  1082. }
  1083. }
  1084. about() {
  1085. this._host.document.getElementById('version').innerText = this._host.version;
  1086. const handler = () => {
  1087. this._host.window.removeEventListener('keydown', handler);
  1088. this._host.document.body.removeEventListener('click', handler);
  1089. this._host.document.body.classList.remove('about');
  1090. };
  1091. this._host.window.addEventListener('keydown', handler);
  1092. this._host.document.body.addEventListener('click', handler);
  1093. this._host.document.body.classList.add('about');
  1094. }
  1095. };
  1096. view.Menu = class {
  1097. constructor(host) {
  1098. this.items = [];
  1099. this._darwin = host.environment('platform') === 'darwin';
  1100. this._document = host.document;
  1101. this._stack = [];
  1102. this._root = [];
  1103. this._buttons = [];
  1104. this._accelerators = new Map();
  1105. this._keyCodes = new Map([
  1106. ['Backspace', 0x08], ['Enter', 0x0D], ['Escape', 0x1B],
  1107. ['Left', 0x25], ['Up', 0x26], ['Right', 0x27], ['Down', 0x28],
  1108. ['F5', 0x74], ['F11', 0x7a]
  1109. ]);
  1110. this._symbols = new Map([
  1111. ['Backspace', '&#x232B;'], ['Enter', '&#x23ce;'],
  1112. ['Up', '&#x2191;'], ['Down', '&#x2193;'],
  1113. ]);
  1114. this._keydown = (e) => {
  1115. this._alt = false;
  1116. const code = e.keyCode | (e.altKey ? 0x0200 : 0) | (e.shiftKey ? 0x0100 : 0);
  1117. const modifier = (e.ctrlKey ? 0x0400 : 0) | (e.metaKey ? 0x0800 : 0);
  1118. if ((code | modifier) === 0x0212) { // Alt
  1119. this._alt = true;
  1120. } else {
  1121. const action =
  1122. this._accelerators.get(code | modifier) ||
  1123. this._accelerators.get(code | ((e.ctrlKey && !this._darwin) || (e.metaKey && this._darwin) ? 0x1000 : 0));
  1124. if (action && this._execute(action)) {
  1125. e.preventDefault();
  1126. } else {
  1127. const item = this._mnemonic(code | modifier);
  1128. if (item && this._activate(item)) {
  1129. e.preventDefault();
  1130. }
  1131. }
  1132. }
  1133. };
  1134. this._keyup = (e) => {
  1135. const code = e.keyCode;
  1136. if (code === 0x0012 && this._alt) { // Alt
  1137. switch (this._stack.length) {
  1138. case 0: {
  1139. if (this.open()) {
  1140. e.preventDefault();
  1141. }
  1142. break;
  1143. }
  1144. case 1: {
  1145. if (this.close()) {
  1146. e.preventDefault();
  1147. }
  1148. break;
  1149. }
  1150. default: {
  1151. this._stack = [this];
  1152. if (this._root.length > 1) {
  1153. this._root = [this];
  1154. this._rebuild();
  1155. }
  1156. this._update();
  1157. e.preventDefault();
  1158. break;
  1159. }
  1160. }
  1161. }
  1162. this._alt = false;
  1163. };
  1164. this._next = () => {
  1165. const button = this._element.ownerDocument.activeElement;
  1166. const index = this._buttons.indexOf(button);
  1167. if (index !== -1 && index < this._buttons.length - 1) {
  1168. const next = this._buttons[index + 1];
  1169. next.focus();
  1170. }
  1171. };
  1172. this._previous = () => {
  1173. const button = this._element.ownerDocument.activeElement;
  1174. const index = this._buttons.indexOf(button);
  1175. if (index > 0) {
  1176. const next = this._buttons[index - 1];
  1177. next.focus();
  1178. }
  1179. };
  1180. this._push = () => {
  1181. const button = this._element.ownerDocument.activeElement;
  1182. if (button && button.getAttribute('data-type') === 'group') {
  1183. button.click();
  1184. }
  1185. };
  1186. this._pop = () => {
  1187. if (this._stack.length > 1) {
  1188. this._deactivate();
  1189. }
  1190. };
  1191. this._exit = () => {
  1192. this._deactivate();
  1193. if (this._stack.length === 0) {
  1194. this.close();
  1195. }
  1196. };
  1197. host.window.addEventListener('keydown', this._keydown);
  1198. host.window.addEventListener('keyup', this._keyup);
  1199. }
  1200. attach(element, button) {
  1201. this._element = element;
  1202. button.addEventListener('click', (e) => {
  1203. this.toggle();
  1204. e.preventDefault();
  1205. });
  1206. }
  1207. add(value) {
  1208. const item = new view.Menu.Command(value);
  1209. this.register(item, item.accelerator);
  1210. }
  1211. group(label) {
  1212. const item = new view.Menu.Group(this, label);
  1213. item.identifier = `menu-item-${this.items.length}`;
  1214. this.items.push(item);
  1215. item.shortcut = this.register(item.accelerator);
  1216. return item;
  1217. }
  1218. toggle() {
  1219. if (this._element.style.opacity >= 1) {
  1220. this.close();
  1221. } else {
  1222. this._root = [this];
  1223. this._stack = [this];
  1224. this.open();
  1225. }
  1226. }
  1227. open() {
  1228. if (this._element) {
  1229. if (this._stack.length === 0) {
  1230. this.toggle();
  1231. this._stack = [this];
  1232. }
  1233. this._rebuild();
  1234. this._update();
  1235. this.register(this._exit, 'Escape');
  1236. this.register(this._previous, 'Up');
  1237. this.register(this._next, 'Down');
  1238. this.register(this._pop, 'Left');
  1239. this.register(this._push, 'Right');
  1240. }
  1241. }
  1242. close() {
  1243. if (this._element) {
  1244. this.unregister(this._exit);
  1245. this.unregister(this._previous);
  1246. this.unregister(this._next);
  1247. this.unregister(this._pop);
  1248. this.unregister(this._push);
  1249. this._element.style.opacity = 0;
  1250. this._element.style.left = '-17em';
  1251. const button = this._element.ownerDocument.activeElement;
  1252. if (this._buttons.indexOf(button) > 0) {
  1253. button.blur();
  1254. }
  1255. while (this._root.length > 1) {
  1256. this._deactivate();
  1257. }
  1258. this._stack = [];
  1259. }
  1260. }
  1261. register(action, accelerator) {
  1262. let shortcut = '';
  1263. if (accelerator) {
  1264. let shift = false;
  1265. let alt = false;
  1266. let ctrl = false;
  1267. let cmd = false;
  1268. let cmdOrCtrl = false;
  1269. let key = '';
  1270. for (const part of accelerator.split('+')) {
  1271. switch (part) {
  1272. case 'CmdOrCtrl': cmdOrCtrl = true; break;
  1273. case 'Cmd': cmd = true; break;
  1274. case 'Ctrl': ctrl = true; break;
  1275. case 'Alt': alt = true; break;
  1276. case 'Shift': shift = true; break;
  1277. default: key = part; break;
  1278. }
  1279. }
  1280. if (key !== '') {
  1281. if (this._darwin) {
  1282. shortcut += ctrl ? '&#x2303' : '';
  1283. shortcut += alt ? '&#x2325;' : '';
  1284. shortcut += shift ? '&#x21e7;' : '';
  1285. shortcut += cmdOrCtrl || cmd ? '&#x2318;' : '';
  1286. shortcut += this._symbols.has(key) ? this._symbols.get(key) : key;
  1287. } else {
  1288. shortcut += cmdOrCtrl || ctrl ? 'Ctrl+' : '';
  1289. shortcut += alt ? 'Alt+' : '';
  1290. shortcut += shift ? 'Shift+' : '';
  1291. shortcut += key;
  1292. }
  1293. let code = (cmdOrCtrl ? 0x1000 : 0) | (cmd ? 0x0800 : 0) | (ctrl ? 0x0400 : 0) | (alt ? 0x0200 : 0) | (shift ? 0x0100 : 0);
  1294. code |= this._keyCodes.has(key) ? this._keyCodes.get(key) : key.charCodeAt(0);
  1295. this._accelerators.set(code, action);
  1296. }
  1297. }
  1298. return shortcut;
  1299. }
  1300. unregister(action) {
  1301. this._accelerators = new Map(Array.from(this._accelerators.entries()).filter(([, value]) => value !== action));
  1302. }
  1303. _execute(action) {
  1304. if (typeof action === 'function') {
  1305. action();
  1306. return true;
  1307. }
  1308. switch (action ? action.type : null) {
  1309. case 'group': {
  1310. while (this._stack.length > this._root.length) {
  1311. this._stack.pop();
  1312. }
  1313. this._root.push({ items: [action] });
  1314. this._stack.push(action);
  1315. this._rebuild();
  1316. this._update();
  1317. return true;
  1318. }
  1319. case 'command': {
  1320. this.close();
  1321. setTimeout(() => action.execute(), 10);
  1322. return true;
  1323. }
  1324. default: {
  1325. return false;
  1326. }
  1327. }
  1328. }
  1329. _mnemonic(code) {
  1330. const key = /[a-zA-Z0-9]/.test(String.fromCharCode(code & 0x00FF));
  1331. const modifier = (code & 0xFF00) !== 0;
  1332. const alt = (code & 0xFF00) === 0x0200;
  1333. if (alt && key) {
  1334. this.open();
  1335. }
  1336. if (this._stack.length > 0 && key && (alt || !modifier)) {
  1337. const key = String.fromCharCode(code & 0x00FF);
  1338. const group = this._stack.length > 0 ? this._stack[this._stack.length - 1] : this;
  1339. const item = group.items.find((item) => key === item.mnemonic && (item.type === 'group' || item.type === 'command') && item.enabled);
  1340. if (item) {
  1341. return item;
  1342. }
  1343. }
  1344. return null;
  1345. }
  1346. _activate(item) {
  1347. switch (item ? item.type : null) {
  1348. case 'group': {
  1349. this._stack.push(item);
  1350. this._rebuild();
  1351. this._update();
  1352. return true;
  1353. }
  1354. case 'command': {
  1355. return this._execute(item);
  1356. }
  1357. default: {
  1358. return false;
  1359. }
  1360. }
  1361. }
  1362. _deactivate() {
  1363. if (this._root.length > 1) {
  1364. this._root.pop();
  1365. const group = this._stack.pop();
  1366. this._rebuild();
  1367. this._update();
  1368. if (group) {
  1369. const button = this._buttons.find((button) => button.getAttribute('id') === group.identifier);
  1370. if (button) {
  1371. button.focus();
  1372. }
  1373. }
  1374. } else if (this._stack.length > 0) {
  1375. this._stack.pop();
  1376. this._update();
  1377. }
  1378. }
  1379. _label(item, mnemonic) {
  1380. delete item.mnemonic;
  1381. const value = item.label;
  1382. if (value) {
  1383. const index = value.indexOf('&');
  1384. if (index !== -1) {
  1385. if (mnemonic) {
  1386. item.mnemonic = value[index + 1].toUpperCase();
  1387. return `${value.substring(0, index)}<u>${value[index + 1]}</u>${value.substring(index + 2)}`;
  1388. }
  1389. return value.substring(0, index) + value.substring(index + 1);
  1390. }
  1391. }
  1392. return value || '';
  1393. }
  1394. _rebuild() {
  1395. this._element.innerHTML = '';
  1396. const root = this._root[this._root.length - 1];
  1397. for (const group of root.items) {
  1398. const container = this._document.createElement('div');
  1399. container.setAttribute('id', group.identifier);
  1400. container.setAttribute('class', 'menu-group');
  1401. container.innerHTML = "<div class='menu-group-header'></div>";
  1402. for (const item of group.items) {
  1403. switch (item.type) {
  1404. case 'group':
  1405. case 'command': {
  1406. const button = this._document.createElement('button');
  1407. button.setAttribute('class', 'menu-command');
  1408. button.setAttribute('id', item.identifier);
  1409. button.setAttribute('data-type', item.type);
  1410. button.addEventListener('mouseenter', () => button.focus());
  1411. button.addEventListener('click', () => this._execute(item));
  1412. const accelerator = this._document.createElement('span');
  1413. accelerator.setAttribute('class', 'menu-shortcut');
  1414. if (item.type === 'group') {
  1415. accelerator.innerHTML = '&#10095;';
  1416. } else if (item.shortcut) {
  1417. accelerator.innerHTML = item.shortcut;
  1418. }
  1419. button.appendChild(accelerator);
  1420. const content = this._document.createElement('span');
  1421. content.setAttribute('class', 'menu-label');
  1422. button.appendChild(content);
  1423. container.appendChild(button);
  1424. break;
  1425. }
  1426. case 'separator': {
  1427. const element = this._document.createElement('div');
  1428. element.setAttribute('class', 'menu-separator');
  1429. element.setAttribute('id', item.identifier);
  1430. container.appendChild(element);
  1431. break;
  1432. }
  1433. default: {
  1434. break;
  1435. }
  1436. }
  1437. }
  1438. this._element.appendChild(container);
  1439. }
  1440. this._element.style.opacity = 1.0;
  1441. this._element.style.left = '0px';
  1442. if (this._root.length > 1) {
  1443. this._element.style.width = 'auto';
  1444. this._element.style.maxWidth = '60%';
  1445. } else {
  1446. this._element.style.removeProperty('width');
  1447. this._element.style.maxWidth = 'auto';
  1448. }
  1449. }
  1450. _update() {
  1451. this._buttons = [];
  1452. const selected = this._stack.length > 0 ? this._stack[this._stack.length - 1] : null;
  1453. const root = this._root[this._root.length - 1];
  1454. for (const group of root.items) {
  1455. let visible = false;
  1456. let block = false;
  1457. const active = this._stack.length <= 1 || this._stack[1] === group;
  1458. const container = this._document.getElementById(group.identifier);
  1459. container.childNodes[0].innerHTML = this._label(group, this === selected);
  1460. for (const item of group.items) {
  1461. switch (item.type) {
  1462. case 'group':
  1463. case 'command': {
  1464. const label = this._label(item, group === selected);
  1465. const button = this._document.getElementById(item.identifier);
  1466. button.childNodes[1].innerHTML = label;
  1467. if (item.enabled) {
  1468. button.removeAttribute('disabled');
  1469. button.style.display = 'block';
  1470. visible = true;
  1471. block = true;
  1472. if (active) {
  1473. this._buttons.push(button);
  1474. }
  1475. } else {
  1476. button.setAttribute('disabled', '');
  1477. button.style.display = 'none';
  1478. }
  1479. break;
  1480. }
  1481. case 'separator': {
  1482. const element = this._document.getElementById(item.identifier);
  1483. element.style.display = block ? 'block' : 'none';
  1484. block = false;
  1485. break;
  1486. }
  1487. default: {
  1488. break;
  1489. }
  1490. }
  1491. }
  1492. for (let i = group.items.length - 1; i >= 0; i--) {
  1493. const item = group.items[i];
  1494. if ((item.type === 'group' || item.type === 'command') && item.enabled) {
  1495. break;
  1496. } else if (item.type === 'separator') {
  1497. const element = this._document.getElementById(item.identifier);
  1498. element.style.display = 'none';
  1499. }
  1500. }
  1501. if (!visible) {
  1502. container.style.display = 'none';
  1503. }
  1504. container.style.opacity = active ? 1 : 0;
  1505. }
  1506. const button = this._element.ownerDocument.activeElement;
  1507. const index = this._buttons.indexOf(button);
  1508. if (index === -1 && this._buttons.length > 0) {
  1509. this._buttons[0].focus();
  1510. }
  1511. }
  1512. };
  1513. view.Menu.Group = class {
  1514. constructor(parent, label) {
  1515. this.type = 'group';
  1516. this.parent = parent;
  1517. this.label = label;
  1518. this.items = [];
  1519. }
  1520. get enabled() {
  1521. return this.items.some((item) => item.enabled);
  1522. }
  1523. add(value) {
  1524. const item = Object.keys(value).length > 0 ? new view.Menu.Command(value) : new view.Menu.Separator();
  1525. item.identifier = `${this.identifier}-${this.items.length}`;
  1526. this.items.push(item);
  1527. item.shortcut = this.parent.register(item, item.accelerator);
  1528. }
  1529. group(label) {
  1530. const item = new view.Menu.Group(this, label);
  1531. item.identifier = `${this.identifier}-${this.items.length}`;
  1532. this.items.push(item);
  1533. item.shortcut = this.parent.register(item, item.accelerator);
  1534. return item;
  1535. }
  1536. clear() {
  1537. for (const item of this.items) {
  1538. if (item.clear) {
  1539. item.clear();
  1540. }
  1541. this.parent.unregister(item);
  1542. }
  1543. this.items = [];
  1544. }
  1545. register(item, accelerator) {
  1546. return this.parent.register(item, accelerator);
  1547. }
  1548. unregister(item) {
  1549. this.parent.unregister(item);
  1550. }
  1551. };
  1552. view.Menu.Command = class {
  1553. constructor(item) {
  1554. this.type = 'command';
  1555. this.accelerator = item.accelerator;
  1556. this._label = item.label;
  1557. this._enabled = item.enabled;
  1558. this._execute = item.execute;
  1559. }
  1560. get label() {
  1561. return typeof this._label === 'function' ? this._label() : this._label;
  1562. }
  1563. get enabled() {
  1564. return this._enabled ? this._enabled() : true;
  1565. }
  1566. execute() {
  1567. if (this._execute && this.enabled) {
  1568. this._execute();
  1569. }
  1570. }
  1571. };
  1572. view.Menu.Separator = class {
  1573. constructor() {
  1574. this.type = 'separator';
  1575. this.enabled = false;
  1576. }
  1577. };
  1578. view.Worker = class {
  1579. constructor(host) {
  1580. this._host = host;
  1581. const type = this._host.type;
  1582. this._browser = type === 'Browser' || type === 'Python';
  1583. if (this._browser) {
  1584. this._create();
  1585. }
  1586. }
  1587. async request(message, delay, notification) {
  1588. this._timeout = -1;
  1589. return new Promise((resolve, reject) => {
  1590. this._resolve = resolve;
  1591. this._reject = reject;
  1592. if (!this._worker) {
  1593. this._create();
  1594. }
  1595. this._worker.postMessage(message);
  1596. this._timeout = setTimeout(async () => {
  1597. await this._host.message(notification, null, 'Cancel');
  1598. this._cancel(true);
  1599. delete this._resolve;
  1600. delete this._reject;
  1601. resolve({ type: 'cancel' });
  1602. }, delay);
  1603. });
  1604. }
  1605. _create() {
  1606. this._worker = this._host.worker('./worker');
  1607. this._worker.addEventListener('message', (e) => {
  1608. this._cancel(false);
  1609. const message = e.data;
  1610. if (message.type === 'error') {
  1611. this._reject(new Error(message.message));
  1612. } else {
  1613. this._resolve(message);
  1614. }
  1615. delete this._resolve;
  1616. delete this._reject;
  1617. });
  1618. this._worker.addEventListener('error', (e) => {
  1619. this._cancel(true);
  1620. if (this._reject) {
  1621. this._reject(new Error(`Unknown worker error type '${e.type}'.`));
  1622. delete this._resolve;
  1623. delete this._reject;
  1624. }
  1625. });
  1626. }
  1627. _cancel(terminate) {
  1628. terminate = terminate || !this._browser;
  1629. if (this._worker && terminate) {
  1630. this._worker.terminate();
  1631. this._worker = null;
  1632. }
  1633. if (this._timeout >= 0) {
  1634. clearTimeout(this._timeout);
  1635. this._timeout = -1;
  1636. this._host.message();
  1637. }
  1638. }
  1639. };
  1640. view.Graph = class extends grapher.Graph {
  1641. constructor(view, host, model, options, compound, layout) {
  1642. super(compound, layout);
  1643. this.view = view;
  1644. this.host = host;
  1645. this.model = model;
  1646. this.options = options;
  1647. this._nodeKey = 0;
  1648. this._values = new Map();
  1649. this._tensors = new Map();
  1650. this._table = new Map();
  1651. this._selection = new Set();
  1652. }
  1653. createNode(node, type) {
  1654. if (type) {
  1655. const obj = new view.Node(this, { type });
  1656. obj.name = (this._nodeKey++).toString();
  1657. this._table.set(type, obj);
  1658. return obj;
  1659. }
  1660. const obj = new view.Node(this, node);
  1661. obj.name = (this._nodeKey++).toString();
  1662. this._table.set(node, obj);
  1663. for (const argument of node.inputs) {
  1664. if (!argument.type || argument.type.endsWith('*')) {
  1665. if (Array.isArray(argument.value) && argument.value.length === 1 && argument.value[0].initializer) {
  1666. this.createArgument(argument);
  1667. } else {
  1668. for (const value of argument.value) {
  1669. if (value.name !== '' && !value.initializer) {
  1670. this.createValue(value).to.push(obj);
  1671. } else if (value.initializer) {
  1672. this.createValue(value);
  1673. }
  1674. }
  1675. }
  1676. }
  1677. }
  1678. return obj;
  1679. }
  1680. createInput(input) {
  1681. const obj = new view.Input(this, input);
  1682. obj.name = (this._nodeKey++).toString();
  1683. this._table.set(input, obj);
  1684. return obj;
  1685. }
  1686. createOutput(output) {
  1687. const obj = new view.Output(this, output);
  1688. obj.name = (this._nodeKey++).toString();
  1689. this._table.set(output, obj);
  1690. return obj;
  1691. }
  1692. createValue(value) {
  1693. const key = value && value.name && !value.initializer ? value.name : value;
  1694. if (this._values.has(key)) {
  1695. // duplicate argument name
  1696. const obj = this._values.get(key);
  1697. this._table.set(value, obj);
  1698. } else {
  1699. const obj = new view.Value(this, value);
  1700. this._values.set(key, obj);
  1701. this._table.set(value, obj);
  1702. }
  1703. return this._values.get(key);
  1704. }
  1705. createArgument(value) {
  1706. if (Array.isArray(value.value) && value.value.length === 1 && value.value[0].initializer) {
  1707. if (!this._tensors.has(value)) {
  1708. const obj = new view.Argument(this, value);
  1709. this._tensors.set(value, obj);
  1710. this._table.set(value, obj);
  1711. }
  1712. return this._tensors.get(value);
  1713. }
  1714. return null;
  1715. }
  1716. add(graph, signature) {
  1717. this.identifier = this.model.identifier;
  1718. this.identifier += graph && graph.name ? `.${graph.name.replace(/\/|\\/, '.')}` : '';
  1719. const clusters = new Set();
  1720. const clusterParentMap = new Map();
  1721. const groups = graph.groups;
  1722. if (groups) {
  1723. for (const node of graph.nodes) {
  1724. if (node.group) {
  1725. const path = node.group.split('/');
  1726. while (path.length > 0) {
  1727. const name = path.join('/');
  1728. path.pop();
  1729. clusterParentMap.set(name, path.join('/'));
  1730. }
  1731. }
  1732. }
  1733. }
  1734. const inputs = signature ? signature.inputs : graph.inputs;
  1735. const outputs = signature ? signature.outputs : graph.outputs;
  1736. if (Array.isArray(inputs)) {
  1737. for (const argument of inputs) {
  1738. if (argument.visible !== false) {
  1739. const viewInput = this.createInput(argument);
  1740. this.setNode(viewInput);
  1741. for (const value of argument.value) {
  1742. this.createValue(value).from = viewInput;
  1743. }
  1744. }
  1745. }
  1746. }
  1747. for (const node of graph.nodes) {
  1748. const viewNode = this.createNode(node);
  1749. this.setNode(viewNode);
  1750. let outputs = node.outputs;
  1751. if (node.chain && node.chain.length > 0) {
  1752. const chainOutputs = node.chain[node.chain.length - 1].outputs;
  1753. if (chainOutputs.length > 0) {
  1754. outputs = chainOutputs;
  1755. }
  1756. }
  1757. for (const argument of outputs) {
  1758. for (const value of argument.value) {
  1759. if (!value) {
  1760. throw new view.Error('Invalid null argument.');
  1761. }
  1762. if (value.name !== '') {
  1763. this.createValue(value).from = viewNode;
  1764. }
  1765. }
  1766. }
  1767. if (node.controlDependencies && node.controlDependencies.length > 0) {
  1768. for (const value of node.controlDependencies) {
  1769. this.createValue(value).controlDependency(viewNode);
  1770. }
  1771. }
  1772. const createCluster = (name) => {
  1773. if (!clusters.has(name)) {
  1774. this.setNode({ name, rx: 5, ry: 5 });
  1775. clusters.add(name);
  1776. const parent = clusterParentMap.get(name);
  1777. if (parent) {
  1778. createCluster(parent);
  1779. this.setParent(name, parent);
  1780. }
  1781. }
  1782. };
  1783. if (groups) {
  1784. let groupName = node.group;
  1785. if (groupName && groupName.length > 0) {
  1786. if (!clusterParentMap.has(groupName)) {
  1787. const lastIndex = groupName.lastIndexOf('/');
  1788. if (lastIndex === -1) {
  1789. groupName = null;
  1790. } else {
  1791. groupName = groupName.substring(0, lastIndex);
  1792. if (!clusterParentMap.has(groupName)) {
  1793. groupName = null;
  1794. }
  1795. }
  1796. }
  1797. if (groupName) {
  1798. createCluster(`${groupName}\ngroup`);
  1799. this.setParent(viewNode.name, `${groupName}\ngroup`);
  1800. }
  1801. }
  1802. }
  1803. }
  1804. if (Array.isArray(outputs)) {
  1805. for (const argument of outputs) {
  1806. if (argument.visible !== false) {
  1807. const viewOutput = this.createOutput(argument);
  1808. this.setNode(viewOutput);
  1809. for (const value of argument.value) {
  1810. this.createValue(value).to.push(viewOutput);
  1811. }
  1812. }
  1813. }
  1814. }
  1815. }
  1816. build(document, origin) {
  1817. for (const value of this._values.values()) {
  1818. value.build();
  1819. }
  1820. super.build(document, origin);
  1821. }
  1822. select(selection) {
  1823. if (this._selection.size > 0) {
  1824. for (const element of this._selection) {
  1825. element.deselect();
  1826. }
  1827. this._selection.clear();
  1828. }
  1829. if (selection) {
  1830. let array = [];
  1831. for (const value of selection) {
  1832. if (this._table.has(value)) {
  1833. const element = this._table.get(value);
  1834. array = array.concat(element.select());
  1835. this._selection.add(element);
  1836. }
  1837. }
  1838. return array;
  1839. }
  1840. return null;
  1841. }
  1842. activate(value) {
  1843. if (this._table.has(value)) {
  1844. this.select(null);
  1845. const element = this._table.get(value);
  1846. element.activate();
  1847. return this.select([value]);
  1848. }
  1849. return [];
  1850. }
  1851. focus(selection) {
  1852. for (const value of selection) {
  1853. const element = this._table.get(value);
  1854. if (element && !this._selection.has(element)) {
  1855. element.select();
  1856. }
  1857. }
  1858. }
  1859. blur(selection) {
  1860. for (const value of selection) {
  1861. const element = this._table.get(value);
  1862. if (element && !this._selection.has(element)) {
  1863. element.deselect();
  1864. }
  1865. }
  1866. }
  1867. };
  1868. view.Node = class extends grapher.Node {
  1869. constructor(context, value) {
  1870. super();
  1871. this.context = context;
  1872. this.value = value;
  1873. view.Node.counter = view.Node.counter || 0;
  1874. this.id = `node-${value.name ? `name-${value.name}` : `id-${(view.Node.counter++)}`}`;
  1875. this._add(this.value);
  1876. }
  1877. get class() {
  1878. return 'graph-node';
  1879. }
  1880. get inputs() {
  1881. return this.value.inputs;
  1882. }
  1883. get outputs() {
  1884. return this.value.outputs;
  1885. }
  1886. _add(node) {
  1887. const options = this.context.options;
  1888. const header = this.header();
  1889. const type = node.type;
  1890. const category = type && type.category ? type.category : '';
  1891. if (typeof type.name !== 'string' || !type.name.split) { // #416
  1892. const error = new view.Error(`Unsupported node type '${JSON.stringify(type.name)}'.`);
  1893. if (this.context.model && this.context.model.identifier) {
  1894. error.context = this.context.model.identifier;
  1895. }
  1896. throw error;
  1897. }
  1898. let content = options.names && (node.name || node.identifier) ? (node.name || node.identifier) : type.name.split('.').pop();
  1899. const tooltip = options.names && (node.name || node.identifier) ? type.name : (node.name || node.identifier);
  1900. if (content.length > 24) {
  1901. content = `${content.substring(0, 12)}\u2026${content.substring(content.length - 12, content.length)}`;
  1902. }
  1903. const styles = category ? ['node-item-type', `node-item-type-${category.toLowerCase()}`] : ['node-item-type'];
  1904. const title = header.add(null, styles, content, tooltip);
  1905. title.on('click', () => {
  1906. this.context.activate(node);
  1907. });
  1908. if (Array.isArray(node.type.nodes) && node.type.nodes.length > 0) {
  1909. const definition = header.add(null, styles, '\u0192', 'Show Function Definition');
  1910. definition.on('click', async () => await this.context.view.pushGraph(node.type));
  1911. }
  1912. if (Array.isArray(node.nodes)) {
  1913. // this._expand = header.add(null, styles, '+', null);
  1914. // this._expand.on('click', () => this.toggle());
  1915. }
  1916. let current = null;
  1917. const list = () => {
  1918. if (!current) {
  1919. current = this.list();
  1920. current.on('click', () => this.context.activate(node));
  1921. }
  1922. return current;
  1923. };
  1924. let hiddenTensors = false;
  1925. const objects = [];
  1926. const attribute = (argument) => {
  1927. let content = new view.Formatter(argument.value, argument.type).toString();
  1928. if (content && content.length > 12) {
  1929. content = `${content.substring(0, 12)}\u2026`;
  1930. }
  1931. const item = list().argument(argument.name, content);
  1932. item.tooltip = argument.type;
  1933. item.separator = ' = ';
  1934. return item;
  1935. };
  1936. const isObject = (node) => {
  1937. if (node.name || node.identifier || node.description ||
  1938. (Array.isArray(node.inputs) && node.inputs.length > 0) ||
  1939. (Array.isArray(node.outputs) && node.outputs.length > 0) ||
  1940. (Array.isArray(node.attributes) && node.attributes.length > 0) ||
  1941. (Array.isArray(node.chain) && node.chain.length > 0)) {
  1942. return true;
  1943. }
  1944. return false;
  1945. };
  1946. if (Array.isArray(node.inputs)) {
  1947. for (const argument of node.inputs) {
  1948. const type = argument.type;
  1949. if (type === 'graph' ||
  1950. (type === 'object' && isObject(argument.value)) ||
  1951. type === 'object[]' || type === 'function' || type === 'function[]') {
  1952. objects.push(argument);
  1953. } else if (options.weights && argument.visible !== false && argument.type !== 'attribute' && Array.isArray(argument.value) && argument.value.length === 1 && argument.value[0].initializer) {
  1954. const item = this.context.createArgument(argument);
  1955. list().add(item);
  1956. } else if (options.weights && (argument.visible === false || Array.isArray(argument.value) && argument.value.length > 1) && (!argument.type || argument.type.endsWith('*')) && argument.value.some((value) => value.initializer)) {
  1957. hiddenTensors = true;
  1958. } else if (options.attributes && argument.visible !== false && argument.type && !argument.type.endsWith('*')) {
  1959. const item = attribute(argument);
  1960. list().add(item);
  1961. }
  1962. }
  1963. }
  1964. if (Array.isArray(node.attributes)) {
  1965. const attributes = node.attributes.slice();
  1966. attributes.sort((a, b) => a.name.toUpperCase().localeCompare(b.name.toUpperCase()));
  1967. for (const argument of attributes) {
  1968. const type = argument.type;
  1969. if (type === 'graph' || type === 'object' || type === 'object[]' || type === 'function' || type === 'function[]') {
  1970. objects.push(argument);
  1971. } else if (options.attributes && argument.visible !== false) {
  1972. const item = attribute(argument);
  1973. list().add(item);
  1974. }
  1975. }
  1976. }
  1977. if (hiddenTensors) {
  1978. const item = list().argument('\u3008\u2026\u3009', '');
  1979. list().add(item);
  1980. }
  1981. for (const argument of objects) {
  1982. const type = argument.type;
  1983. let content = null;
  1984. if (type === 'graph') {
  1985. content = this.context.createNode(null, argument.value);
  1986. } else if (type === 'function' || argument.type === 'object') {
  1987. content = this.context.createNode(argument.value);
  1988. } else if (type === 'function[]' || argument.type === 'object[]') {
  1989. content = argument.value.map((value) => this.context.createNode(value));
  1990. }
  1991. const item = list().argument(argument.name, content);
  1992. list().add(item);
  1993. }
  1994. if (Array.isArray(node.nodes) && node.nodes.length > 0) {
  1995. // this.canvas = this.canvas();
  1996. }
  1997. if (Array.isArray(node.chain) && node.chain.length > 0) {
  1998. for (const innerNode of node.chain) {
  1999. this.context.createNode(innerNode);
  2000. this._add(innerNode);
  2001. }
  2002. }
  2003. if (node.inner) {
  2004. this.context.createNode(node.inner);
  2005. this._add(node.inner);
  2006. }
  2007. }
  2008. toggle() {
  2009. this._expand.content = '-';
  2010. this._graph = new view.Graph(this.context.view, this.context.view.host, this.context.model, this.context.options, false, {});
  2011. this._graph.add(this.value);
  2012. // const document = this.element.ownerDocument;
  2013. // const parent = this.element.parentElement;
  2014. // this._graph.build(document, parent);
  2015. // this._graph.update();
  2016. this.canvas.width = 300;
  2017. this.canvas.height = 300;
  2018. this.layout();
  2019. this.context.update();
  2020. }
  2021. activate() {
  2022. this.context.view.showNodeProperties(this.value);
  2023. }
  2024. edge(to) {
  2025. this._edges = this._edges || new Map();
  2026. if (!this._edges.has(to)) {
  2027. this._edges.set(to, new view.Edge(this, to));
  2028. }
  2029. return this._edges.get(to);
  2030. }
  2031. };
  2032. view.Input = class extends grapher.Node {
  2033. constructor(context, value) {
  2034. super();
  2035. this.context = context;
  2036. this.value = value;
  2037. view.Input.counter = view.Input.counter || 0;
  2038. const types = value.value.map((argument) => argument.type || '').join('\n');
  2039. let name = value.name || '';
  2040. if (name.length > 16) {
  2041. name = name.split('/').pop();
  2042. }
  2043. const header = this.header();
  2044. const title = header.add(null, ['graph-item-input'], name, types);
  2045. title.on('click', () => this.context.view.showModelProperties());
  2046. this.id = `input-${name ? `name-${name}` : `id-${(view.Input.counter++)}`}`;
  2047. }
  2048. get class() {
  2049. return 'graph-input';
  2050. }
  2051. get inputs() {
  2052. return [];
  2053. }
  2054. get outputs() {
  2055. return [this.value];
  2056. }
  2057. activate() {
  2058. this.context.view.showModelProperties();
  2059. }
  2060. edge(to) {
  2061. this._edges = this._edges || new Map();
  2062. if (!this._edges.has(to)) {
  2063. this._edges.set(to, new view.Edge(this, to));
  2064. }
  2065. return this._edges.get(to);
  2066. }
  2067. };
  2068. view.Output = class extends grapher.Node {
  2069. constructor(context, value) {
  2070. super();
  2071. this.context = context;
  2072. this.value = value;
  2073. const types = value.value.map((argument) => argument.type || '').join('\n');
  2074. let name = value.name || '';
  2075. if (name.length > 16) {
  2076. name = name.split('/').pop();
  2077. }
  2078. const header = this.header();
  2079. const title = header.add(null, ['graph-item-output'], name, types);
  2080. title.on('click', () => this.context.view.showModelProperties());
  2081. }
  2082. get inputs() {
  2083. return [this.value];
  2084. }
  2085. get outputs() {
  2086. return [];
  2087. }
  2088. activate() {
  2089. this.context.view.showModelProperties();
  2090. }
  2091. };
  2092. view.Value = class {
  2093. constructor(context, value) {
  2094. this.context = context;
  2095. this.value = value;
  2096. this.from = null;
  2097. this.to = [];
  2098. }
  2099. controlDependency(node) {
  2100. this._controlDependencies = this._controlDependencies || new Set();
  2101. this._controlDependencies.add(this.to.length);
  2102. this.to.push(node);
  2103. }
  2104. build() {
  2105. this._edges = this._edges || [];
  2106. if (this.from && Array.isArray(this.to)) {
  2107. for (let i = 0; i < this.to.length; i++) {
  2108. const to = this.to[i];
  2109. let content = '';
  2110. const type = this.value.type;
  2111. if (type &&
  2112. type.shape &&
  2113. type.shape.dimensions &&
  2114. type.shape.dimensions.length > 0 &&
  2115. type.shape.dimensions.every((dim) => !dim || Number.isInteger(dim) || typeof dim === 'bigint' || (typeof dim === 'string'))) {
  2116. content = type.shape.dimensions.map((dim) => (dim !== null && dim !== undefined && dim !== -1) ? dim : '?').join('\u00D7');
  2117. content = content.length > 16 ? '' : content;
  2118. }
  2119. if (this.context.options.names) {
  2120. content = this.value.name.split('\n').shift(); // custom argument id
  2121. }
  2122. const edge = this.from.edge(to);
  2123. if (!edge.value) {
  2124. edge.value = this;
  2125. if (content) {
  2126. edge.label = content;
  2127. }
  2128. edge.id = `edge-${this.value.name}`;
  2129. if (this._controlDependencies && this._controlDependencies.has(i)) {
  2130. edge.class = 'edge-path-control-dependency';
  2131. }
  2132. }
  2133. this.context.setEdge(edge);
  2134. this._edges.push(edge);
  2135. }
  2136. }
  2137. }
  2138. select() {
  2139. let array = [];
  2140. if (Array.isArray(this._edges)) {
  2141. for (const edge of this._edges) {
  2142. array = array.concat(edge.select());
  2143. }
  2144. }
  2145. return array;
  2146. }
  2147. deselect() {
  2148. if (Array.isArray(this._edges)) {
  2149. for (const edge of this._edges) {
  2150. edge.deselect();
  2151. }
  2152. }
  2153. }
  2154. activate() {
  2155. if (this.value && this.from && Array.isArray(this.to) && !this.value.initializer) {
  2156. const from = this.from.value;
  2157. const to = this.to.map((node) => node.value);
  2158. this.context.view.showConnectionProperties(this.value, from, to);
  2159. } else if (this.value && this.value.initializer) {
  2160. this.context.view.showTensorProperties({ value: [this.value] });
  2161. }
  2162. }
  2163. };
  2164. view.Argument = class extends grapher.Argument {
  2165. constructor(context, value) {
  2166. const name = value.name;
  2167. let content = '';
  2168. let separator = '';
  2169. let tooltip = '';
  2170. if (Array.isArray(value.value) && value.value.length === 1 && value.value[0].initializer) {
  2171. const tensor = value.value[0].initializer;
  2172. const type = value.value[0].type;
  2173. if (type && type.shape && type.shape.dimensions && Array.isArray(type.shape.dimensions)) {
  2174. tooltip = type.toString();
  2175. content = `\u3008${type.shape.dimensions.map((d) => (d !== null && d !== undefined) ? d : '?').join('\u00D7')}\u3009`;
  2176. if (type.shape.dimensions.length === 0) {
  2177. const formatter = new view.Formatter(tensor, 'tensor');
  2178. content = formatter.toString();
  2179. separator = ' = ';
  2180. }
  2181. }
  2182. }
  2183. super(name, content);
  2184. this.context = context;
  2185. this.value = value;
  2186. this.separator = separator;
  2187. this.tooltip = tooltip;
  2188. }
  2189. focus() {
  2190. this.context.focus([this.value]);
  2191. }
  2192. blur() {
  2193. this.context.blur([this.value]);
  2194. }
  2195. activate() {
  2196. this.context.view.showTensorProperties(this.value);
  2197. }
  2198. };
  2199. view.Edge = class extends grapher.Edge {
  2200. constructor(from, to) {
  2201. super(from, to);
  2202. this.v = from.name;
  2203. this.w = to.name;
  2204. }
  2205. get minlen() {
  2206. if (this.from.inputs.every((argument) => (!argument.type || argument.type.endsWith('*')) && argument.value.every((value) => value.initializer))) {
  2207. return 2;
  2208. }
  2209. return 1;
  2210. }
  2211. focus() {
  2212. this.value.context.focus([this.value.value]);
  2213. }
  2214. blur() {
  2215. this.value.context.blur([this.value.value]);
  2216. }
  2217. activate() {
  2218. this.value.context.activate(this.value.value);
  2219. }
  2220. };
  2221. view.Sidebar = class {
  2222. constructor(host) {
  2223. this._host = host;
  2224. this._stack = [];
  2225. const pop = () => this._update(this._stack.slice(0, -1));
  2226. this._closeSidebarHandler = () => pop();
  2227. this._closeSidebarKeyDownHandler = (e) => {
  2228. if (e.keyCode === 27) {
  2229. e.stopPropagation();
  2230. e.preventDefault();
  2231. pop();
  2232. }
  2233. };
  2234. const sidebar = this._element('sidebar');
  2235. sidebar.addEventListener('transitionend', (event) => {
  2236. if (event.propertyName === 'opacity' && sidebar.style.opacity === '0') {
  2237. const content = this._element('sidebar-content');
  2238. content.innerHTML = '';
  2239. }
  2240. });
  2241. }
  2242. _element(id) {
  2243. return this._host.document.getElementById(id);
  2244. }
  2245. open(content, title) {
  2246. const element = this._render(content);
  2247. const entry = { title, element, content };
  2248. this._update([entry]);
  2249. }
  2250. close() {
  2251. this._update([]);
  2252. }
  2253. push(content, title) {
  2254. const element = this._render(content);
  2255. const entry = { title, content, element };
  2256. this._update(this._stack.concat(entry));
  2257. }
  2258. _render(content) {
  2259. try {
  2260. content.render();
  2261. } catch (error) {
  2262. content.error(error, false);
  2263. }
  2264. const element = content.element;
  2265. return Array.isArray(element) ? element : [element];
  2266. }
  2267. _update(stack) {
  2268. const sidebar = this._element('sidebar');
  2269. const content = this._element('sidebar-content');
  2270. const container = this._element('graph');
  2271. const closeButton = this._element('sidebar-closebutton');
  2272. closeButton.removeEventListener('click', this._closeSidebarHandler);
  2273. this._host.document.removeEventListener('keydown', this._closeSidebarKeyDownHandler);
  2274. if (this._stack.length > 0) {
  2275. const entry = this._stack.pop();
  2276. if (entry.content && entry.content.deactivate) {
  2277. entry.content.deactivate();
  2278. }
  2279. }
  2280. if (stack) {
  2281. this._stack = stack;
  2282. }
  2283. if (this._stack.length > 0) {
  2284. const entry = this._stack[this._stack.length - 1];
  2285. this._element('sidebar-title').innerHTML = entry.title || '';
  2286. closeButton.addEventListener('click', this._closeSidebarHandler);
  2287. if (typeof entry.content === 'string') {
  2288. content.innerHTML = entry.element;
  2289. } else if (entry.element instanceof Array) {
  2290. content.innerHTML = '';
  2291. for (const element of entry.element) {
  2292. content.appendChild(element);
  2293. }
  2294. } else {
  2295. content.innerHTML = '';
  2296. content.appendChild(entry.element);
  2297. }
  2298. if (entry.content && entry.content.activate) {
  2299. entry.content.activate();
  2300. }
  2301. sidebar.style.width = 'min(calc(100% * 0.6), 42em)';
  2302. sidebar.style.right = 0;
  2303. sidebar.style.opacity = 1;
  2304. this._host.document.addEventListener('keydown', this._closeSidebarKeyDownHandler);
  2305. container.style.width = 'max(40vw, calc(100vw - 42em))';
  2306. } else {
  2307. sidebar.style.right = 'calc(0px - min(calc(100% * 0.6), 42em))';
  2308. sidebar.style.opacity = 0;
  2309. const clone = content.cloneNode(true);
  2310. content.parentNode.replaceChild(clone, content);
  2311. container.style.width = '100%';
  2312. container.focus();
  2313. }
  2314. }
  2315. };
  2316. view.Control = class {
  2317. constructor(context) {
  2318. this._view = context;
  2319. this._host = context.host;
  2320. }
  2321. createElement(tagName, className) {
  2322. const element = this._host.document.createElement(tagName);
  2323. if (className) {
  2324. element.setAttribute('class', className);
  2325. }
  2326. return element;
  2327. }
  2328. on(event, callback) {
  2329. this._events = this._events || {};
  2330. this._events[event] = this._events[event] || [];
  2331. this._events[event].push(callback);
  2332. }
  2333. emit(event, data) {
  2334. if (this._events && this._events[event]) {
  2335. for (const callback of this._events[event]) {
  2336. callback(this, data);
  2337. }
  2338. }
  2339. }
  2340. error(error, fatal) {
  2341. this._view.exception(error, fatal || false);
  2342. }
  2343. };
  2344. view.Expander = class extends view.Control {
  2345. constructor(context) {
  2346. super(context);
  2347. this.element = this.createElement('div', 'sidebar-item-value');
  2348. this._count = -1;
  2349. }
  2350. render() {
  2351. return [this.element];
  2352. }
  2353. enable() {
  2354. this._expander = this.createElement('div', 'sidebar-item-value-expander');
  2355. this._expander.innerText = '+';
  2356. this._expander.addEventListener('click', () => this.toggle());
  2357. this.add(this._expander);
  2358. }
  2359. add(element) {
  2360. this.element.appendChild(element);
  2361. }
  2362. control(element) {
  2363. this.add(element);
  2364. }
  2365. toggle() {
  2366. this._count = this._count === -1 ? this.element.childElementCount : this._count;
  2367. if (this._expander) {
  2368. while (this.element.childElementCount > this._count) {
  2369. this.element.removeChild(this.element.lastChild);
  2370. }
  2371. if (this._expander.innerText === '+') {
  2372. this._expander.innerText = '-';
  2373. this.expand();
  2374. } else {
  2375. this._expander.innerText = '+';
  2376. this.collapse();
  2377. }
  2378. }
  2379. }
  2380. expand() {
  2381. }
  2382. collapse() {
  2383. }
  2384. };
  2385. view.ObjectSidebar = class extends view.Control {
  2386. constructor(context) {
  2387. super(context);
  2388. this.element = this.createElement('div', 'sidebar-object');
  2389. }
  2390. addEntry(name, item) {
  2391. const entry = new view.NameValueView(this._view, name, item);
  2392. const element = entry.render();
  2393. this.element.appendChild(element);
  2394. }
  2395. addProperty(name, value, style) {
  2396. const item = new view.TextView(this._view, value, style);
  2397. this.addEntry(name, item);
  2398. return item;
  2399. }
  2400. addHeader(title) {
  2401. const element = this.createElement('div', 'sidebar-header');
  2402. element.innerText = title;
  2403. this.element.appendChild(element);
  2404. }
  2405. error(error, fatal) {
  2406. super.error(error, fatal);
  2407. const element = this.createElement('span');
  2408. element.innerHTML = `<b>ERROR:</b> ${error.message}`;
  2409. this.element.appendChild(element);
  2410. }
  2411. };
  2412. view.NodeSidebar = class extends view.ObjectSidebar {
  2413. constructor(context, node) {
  2414. super(context);
  2415. this._node = node;
  2416. }
  2417. render() {
  2418. const node = this._node;
  2419. if (node.type) {
  2420. const type = node.type;
  2421. const item = this.addProperty('type', node.type.identifier || node.type.name);
  2422. if (type && (type.description || type.inputs || type.outputs || type.attributes)) {
  2423. item.action(type.nodes ? '\u0192' : '?', 'Show Definition', () => {
  2424. this.emit('show-documentation', null);
  2425. });
  2426. }
  2427. const module = node.type.module;
  2428. const version = node.type.version;
  2429. const status = node.type.status;
  2430. if (module || version || status) {
  2431. const list = [module, version ? `v${version}` : '', status];
  2432. const value = list.filter((value) => value).join(' ');
  2433. this.addProperty('module', value, 'nowrap');
  2434. }
  2435. }
  2436. if (node.name) {
  2437. this.addProperty('name', node.name, 'nowrap');
  2438. }
  2439. if (node.identifier) {
  2440. this.addProperty('identifier', node.identifier, 'nowrap');
  2441. }
  2442. if (node.description) {
  2443. this.addProperty('description', node.description);
  2444. }
  2445. if (node.device) {
  2446. this.addProperty('device', node.device);
  2447. }
  2448. const attributes = node.attributes;
  2449. if (Array.isArray(attributes) && attributes.length > 0) {
  2450. this.addHeader('Attributes');
  2451. attributes.sort((a, b) => a.name.localeCompare(b.name, undefined, { sensitivity: 'base' }));
  2452. for (const attribute of attributes) {
  2453. this.addArgument(attribute.name, attribute, 'attribute');
  2454. }
  2455. }
  2456. const inputs = node.inputs;
  2457. if (Array.isArray(inputs) && inputs.length > 0) {
  2458. this.addHeader('Inputs');
  2459. for (const input of inputs) {
  2460. const name = input.name;
  2461. this.addArgument(name, input);
  2462. }
  2463. }
  2464. const outputs = node.outputs;
  2465. if (Array.isArray(outputs) && outputs.length > 0) {
  2466. this.addHeader('Outputs');
  2467. for (const output of outputs) {
  2468. const name = output.name;
  2469. this.addArgument(name, output);
  2470. }
  2471. }
  2472. const metadata = node.metadata;
  2473. if (Array.isArray(metadata) && metadata.length > 0) {
  2474. this.addHeader('Metadata');
  2475. for (const entry of metadata) {
  2476. this.addArgument(entry.name, entry, 'attribute');
  2477. }
  2478. }
  2479. }
  2480. addArgument(name, argument, source) {
  2481. const value = new view.ArgumentView(this._view, argument, source);
  2482. value.on('focus', (sender, value) => {
  2483. this.emit('focus', value);
  2484. this._focused = this._focused || new Set();
  2485. this._focused.add(value);
  2486. });
  2487. value.on('blur', (sender, value) => {
  2488. this.emit('blur', value);
  2489. this._focused = this._focused || new Set();
  2490. this._focused.delete(value);
  2491. });
  2492. value.on('select', (sender, value) => this.emit('select', value));
  2493. value.on('activate', (sender, value) => this.emit('activate', value));
  2494. this.addEntry(name, value);
  2495. }
  2496. activate() {
  2497. this.emit('select', this._node);
  2498. }
  2499. deactivate() {
  2500. this.emit('select', null);
  2501. if (this._focused) {
  2502. for (const value of this._focused) {
  2503. this.emit('blur', value);
  2504. }
  2505. this._focused.clear();
  2506. }
  2507. }
  2508. };
  2509. view.NameValueView = class extends view.Control {
  2510. constructor(context, name, value) {
  2511. super(context);
  2512. this._name = name;
  2513. this._value = value;
  2514. const nameElement = this.createElement('div', 'sidebar-item-name');
  2515. const input = this.createElement('input');
  2516. input.setAttribute('type', 'text');
  2517. input.setAttribute('value', name);
  2518. input.setAttribute('title', name);
  2519. input.setAttribute('readonly', 'true');
  2520. nameElement.appendChild(input);
  2521. const valueElement = this.createElement('div', 'sidebar-item-value-list');
  2522. for (const element of value.render()) {
  2523. valueElement.appendChild(element);
  2524. }
  2525. this.element = this.createElement('div', 'sidebar-item');
  2526. this.element.appendChild(nameElement);
  2527. this.element.appendChild(valueElement);
  2528. }
  2529. get name() {
  2530. return this._name;
  2531. }
  2532. render() {
  2533. return this.element;
  2534. }
  2535. toggle() {
  2536. this._value.toggle();
  2537. }
  2538. };
  2539. view.SelectView = class extends view.Control {
  2540. constructor(context, entries, selected) {
  2541. super(context);
  2542. this._elements = [];
  2543. this._entries = Array.from(entries);
  2544. const selectElement = this.createElement('select', 'sidebar-item-selector');
  2545. selectElement.addEventListener('change', (e) => {
  2546. this.emit('change', this._entries[e.target.selectedIndex][1]);
  2547. });
  2548. this._elements.push(selectElement);
  2549. for (const [name, value] of this._entries) {
  2550. const element = this.createElement('option');
  2551. element.innerText = name;
  2552. if (value === selected) {
  2553. element.setAttribute('selected', 'selected');
  2554. }
  2555. selectElement.appendChild(element);
  2556. }
  2557. }
  2558. render() {
  2559. return this._elements;
  2560. }
  2561. };
  2562. view.TextView = class extends view.Control {
  2563. constructor(context, value, style) {
  2564. super(context);
  2565. this.element = this.createElement('div', 'sidebar-item-value');
  2566. let className = 'sidebar-item-value-line';
  2567. if (value) {
  2568. const list = Array.isArray(value) ? value : [value];
  2569. for (const item of list) {
  2570. const line = this.createElement('div', className);
  2571. switch (style) {
  2572. case 'code':
  2573. line.innerHTML = `<code>${item}<code>`;
  2574. break;
  2575. case 'bold':
  2576. line.innerHTML = `<b>${item}<b>`;
  2577. break;
  2578. case 'nowrap':
  2579. line.innerText = item;
  2580. line.style.whiteSpace = style;
  2581. break;
  2582. default:
  2583. line.innerText = item;
  2584. break;
  2585. }
  2586. this.element.appendChild(line);
  2587. className = 'sidebar-item-value-line-border';
  2588. }
  2589. } else {
  2590. const line = this.createElement('div', className);
  2591. line.classList.add('sidebar-item-disable-select');
  2592. line.innerHTML = '&nbsp';
  2593. this.element.appendChild(line);
  2594. }
  2595. }
  2596. action(text, description, callback) {
  2597. const action = this.createElement('div', 'sidebar-item-value-expander');
  2598. action.setAttribute('title', description);
  2599. action.addEventListener('click', () => callback());
  2600. action.innerHTML = text;
  2601. this.element.insertBefore(action, this.element.childNodes[0]);
  2602. }
  2603. render() {
  2604. return [this.element];
  2605. }
  2606. toggle() {
  2607. }
  2608. };
  2609. view.ArgumentView = class extends view.Control {
  2610. constructor(context, argument, source) {
  2611. super(context);
  2612. this._argument = argument;
  2613. this._source = source;
  2614. this._elements = [];
  2615. this._items = [];
  2616. const type = argument.type === 'attribute' ? null : argument.type;
  2617. let value = argument.value;
  2618. if (argument.type === 'attribute') {
  2619. this._source = 'attribute';
  2620. }
  2621. if (argument.type === 'tensor') {
  2622. value = [{ type: value.type, initializer: value }];
  2623. } else if (argument.type === 'tensor[]') {
  2624. value = value.map((value) => ({ type: value.type, initializer: value }));
  2625. }
  2626. this._source = typeof type === 'string' && !type.endsWith('*') ? 'attribute' : this._source;
  2627. if (this._source === 'attribute' && type !== 'tensor' && type !== 'tensor[]') {
  2628. this._source = 'attribute';
  2629. const item = new view.PrimitiveView(context, argument);
  2630. this._items.push(item);
  2631. } else if (Array.isArray(value) && value.length === 0) {
  2632. const item = new view.TextView(this._view, null);
  2633. this._items.push(item);
  2634. } else {
  2635. const values = value;
  2636. for (const value of values) {
  2637. const emit = values.length === 1 && value.initializer;
  2638. const target = emit ? argument : value;
  2639. const item = new view.ValueView(context, value, this._source);
  2640. item.on('focus', () => this.emit('focus', target));
  2641. item.on('blur', () => this.emit('blur', target));
  2642. item.on('activate', () => this.emit('activate', target));
  2643. item.on('select', () => this.emit('select', target));
  2644. this._items.push(item);
  2645. }
  2646. }
  2647. for (const item of this._items) {
  2648. this._elements.push(...item.render());
  2649. }
  2650. }
  2651. render() {
  2652. return this._elements;
  2653. }
  2654. toggle() {
  2655. if (this._source === 'attribute') {
  2656. if (this._expander.innerText === '+') {
  2657. this._expander.innerText = '-';
  2658. } else {
  2659. this._expander.innerText = '+';
  2660. while (this._element.childElementCount > 2) {
  2661. this._element.removeChild(this._element.lastChild);
  2662. }
  2663. }
  2664. } else {
  2665. for (const item of this._items) {
  2666. item.toggle();
  2667. }
  2668. }
  2669. }
  2670. };
  2671. view.PrimitiveView = class extends view.Expander {
  2672. constructor(context, argument) {
  2673. super(context);
  2674. try {
  2675. this._argument = argument;
  2676. const type = argument.type === 'attribute' ? null : argument.type;
  2677. const value = argument.value;
  2678. if (type) {
  2679. this.enable();
  2680. }
  2681. switch (type) {
  2682. case 'graph': {
  2683. const line = this.createElement('div', 'sidebar-item-value-line-link');
  2684. line.innerHTML = value.name || '&nbsp;';
  2685. line.addEventListener('click', () => this.emit('activate', value));
  2686. this.add(line);
  2687. break;
  2688. }
  2689. case 'function': {
  2690. const line = this.createElement('div', 'sidebar-item-value-line-link');
  2691. line.innerHTML = value.type.name;
  2692. line.addEventListener('click', () => this.emit('activate', value));
  2693. this.add(line);
  2694. break;
  2695. }
  2696. case 'object[]': {
  2697. for (const obj of argument.value) {
  2698. const line = this.createElement('div', 'sidebar-item-value-line');
  2699. line.innerHTML = obj.type.name;
  2700. this.add(line);
  2701. }
  2702. break;
  2703. }
  2704. default: {
  2705. let content = new view.Formatter(value, type).toString();
  2706. if (content && content.length > 1000) {
  2707. content = `${content.substring(0, 1000)}\u2026`;
  2708. }
  2709. if (content && typeof content === 'string') {
  2710. content = content.split('<').join('&lt;').split('>').join('&gt;');
  2711. }
  2712. const line = this.createElement('div', 'sidebar-item-value-line');
  2713. line.innerHTML = content ? content : '&nbsp;';
  2714. this.add(line);
  2715. }
  2716. }
  2717. } catch (error) {
  2718. super.error(error, false);
  2719. this._info('ERROR', error.message);
  2720. }
  2721. }
  2722. expand() {
  2723. try {
  2724. const type = this._argument.type;
  2725. const value = this._argument.value;
  2726. const content = type === 'tensor' && value && value.type ? value.type.toString() : this._argument.type;
  2727. const line = this.createElement('div', 'sidebar-item-value-line-border');
  2728. line.innerHTML = `type: <code><b>${content}</b></code>`;
  2729. this.add(line);
  2730. const description = this._argument.description;
  2731. if (description) {
  2732. const line = this.createElement('div', 'sidebar-item-value-line-border');
  2733. line.innerHTML = description;
  2734. this.add(line);
  2735. }
  2736. } catch (error) {
  2737. super.error(error, false);
  2738. this._info('ERROR', error.message);
  2739. }
  2740. }
  2741. _info(name, value) {
  2742. const line = this.createElement('div');
  2743. line.innerHTML = `<b>${name}:</b> ${value}`;
  2744. this._add(line);
  2745. }
  2746. _add(child) {
  2747. child.className = this._first === false ? 'sidebar-item-value-line-border' : 'sidebar-item-value-line';
  2748. this.add(child);
  2749. this._first = false;
  2750. }
  2751. };
  2752. view.ValueView = class extends view.Expander {
  2753. constructor(context, value, source) {
  2754. super(context);
  2755. this._value = value;
  2756. try {
  2757. const type = this._value.type;
  2758. const initializer = this._value.initializer;
  2759. const quantization = this._value.quantization;
  2760. const location = this._value.location !== undefined;
  2761. if (initializer) {
  2762. this.element.classList.add('sidebar-item-value-content');
  2763. }
  2764. if (type || initializer || quantization || location || source === 'attribute') {
  2765. this.enable();
  2766. }
  2767. if (initializer && source !== 'attribute') {
  2768. const element = this.createElement('div', 'sidebar-item-value-button');
  2769. element.classList.add('sidebar-item-value-button-tool');
  2770. element.setAttribute('title', 'Show Tensor');
  2771. element.innerHTML = `<svg class='sidebar-find-content-icon'><use href="#sidebar-icon-weight"></use></svg>`;
  2772. element.addEventListener('pointerenter', () => this.emit('focus', this._value));
  2773. element.addEventListener('pointerleave', () => this.emit('blur', this._value));
  2774. element.style.cursor = 'pointer';
  2775. element.addEventListener('click', () => this.emit('activate', this._value));
  2776. this.control(element);
  2777. }
  2778. const name = this._value.name ? this._value.name.split('\n').shift() : ''; // custom argument id
  2779. this._hasId = name && source !== 'attribute' ? true : false;
  2780. this._hasCategory = initializer && initializer.category && source !== 'attribute' ? true : false;
  2781. if (this._hasId || (!this._hasCategory && !type && source !== 'attribute')) {
  2782. this._hasId = true;
  2783. const element = this.createElement('div', 'sidebar-item-value-line');
  2784. if (typeof name !== 'string') {
  2785. throw new Error(`Invalid value identifier '${JSON.stringify(name)}'.`);
  2786. }
  2787. element.innerHTML = `<span class='sidebar-item-value-line-content'>name: <b>${name || ' '}</b></span>`;
  2788. element.addEventListener('pointerenter', () => this.emit('focus', this._value));
  2789. element.addEventListener('pointerleave', () => this.emit('blur', this._value));
  2790. element.style.cursor = 'pointer';
  2791. element.addEventListener('click', () => this.emit('activate', this._value));
  2792. this._add(element);
  2793. } else if (this._hasCategory) {
  2794. this._bold('category', initializer.category);
  2795. } else if (type) {
  2796. const value = type.toString().split('<').join('&lt;').split('>').join('&gt;');
  2797. this._code('tensor', value);
  2798. }
  2799. } catch (error) {
  2800. super.error(error, false);
  2801. this._info('ERROR', error.message);
  2802. }
  2803. }
  2804. render() {
  2805. return [this.element];
  2806. }
  2807. expand() {
  2808. try {
  2809. const initializer = this._value.initializer;
  2810. if (this._hasId && this._hasCategory) {
  2811. this._bold('category', initializer.category);
  2812. }
  2813. let type = null;
  2814. let denotation = null;
  2815. if (this._value.type) {
  2816. type = this._value.type.toString();
  2817. denotation = this._value.type.denotation || null;
  2818. }
  2819. if (type && (this._hasId || this._hasCategory)) {
  2820. this._code('tensor', type.split('<').join('&lt;').split('>').join('&gt;'));
  2821. }
  2822. if (denotation) {
  2823. this._code('denotation', denotation);
  2824. }
  2825. const description = this._value.description;
  2826. if (description) {
  2827. const line = this.createElement('div', 'sidebar-item-value-line-border');
  2828. line.innerHTML = description;
  2829. this.add(line);
  2830. }
  2831. const identifier = this._value.identifier;
  2832. if (identifier !== undefined) {
  2833. this._bold('identifier', identifier);
  2834. }
  2835. const layout = this._value.type ? this._value.type.layout : null;
  2836. if (layout) {
  2837. this._bold('layout', layout.replace('.', ' '));
  2838. }
  2839. const quantization = this._value.quantization;
  2840. if (quantization) {
  2841. if (typeof quantization.type !== 'string') {
  2842. throw new view.Error('Unsupported quantization value.');
  2843. }
  2844. const value = new view.Quantization(quantization).toString();
  2845. if (quantization.type && (quantization.type !== 'linear' || (value && value !== 'q'))) {
  2846. const line = this.createElement('div', 'sidebar-item-value-line-border');
  2847. const content = [
  2848. `<span class='sidebar-item-value-line-content'>quantization: <b>${quantization.type}</b></span>`
  2849. ];
  2850. if (value) {
  2851. content.push(`<pre style='margin: 4px 0 2px 0'>${value}</pre>`);
  2852. }
  2853. line.innerHTML = content.join('');
  2854. this._add(line);
  2855. }
  2856. }
  2857. if (initializer) {
  2858. if (initializer.location) {
  2859. this._bold('location', initializer.location);
  2860. }
  2861. const stride = initializer.stride;
  2862. if (Array.isArray(stride) && stride.length > 0) {
  2863. this._code('stride', stride.join(','));
  2864. }
  2865. const tensor = new view.TensorView(this._view, initializer);
  2866. const content = tensor.content;
  2867. const line = this.createElement('div', 'sidebar-item-value-line-border');
  2868. line.appendChild(content);
  2869. this._add(line);
  2870. }
  2871. } catch (error) {
  2872. super.error(error, false);
  2873. this._info('ERROR', error.message);
  2874. }
  2875. }
  2876. _bold(name, value) {
  2877. const line = this.createElement('div');
  2878. line.innerHTML = `${name}: <b>${value}</b>`;
  2879. this._add(line);
  2880. }
  2881. _code(name, value) {
  2882. const line = this.createElement('div');
  2883. line.innerHTML = `${name}: <code><b>${value}</b></code>`;
  2884. this._add(line);
  2885. }
  2886. _info(name, value) {
  2887. const line = this.createElement('div');
  2888. line.innerHTML = `<b>${name}:</b> ${value}`;
  2889. this._add(line);
  2890. }
  2891. _add(child) {
  2892. child.className = this._first === false ? 'sidebar-item-value-line-border' : 'sidebar-item-value-line';
  2893. this.add(child);
  2894. this._first = false;
  2895. }
  2896. };
  2897. view.TensorView = class extends view.Expander {
  2898. constructor(context, value, tensor) {
  2899. super(context);
  2900. this._value = value;
  2901. this._tensor = tensor || new base.Tensor(value);
  2902. }
  2903. render() {
  2904. if (!this._button) {
  2905. this.enable();
  2906. this._button = this.createElement('div', 'sidebar-item-value-button');
  2907. this._button.setAttribute('style', 'float: left;');
  2908. this._button.innerHTML = `<svg class='sidebar-find-content-icon'><use href="#sidebar-icon-weight"></use></svg>`;
  2909. this._button.addEventListener('click', () => this.toggle());
  2910. this.control(this._button);
  2911. const line = this.createElement('div', 'sidebar-item-value-line');
  2912. line.classList.add('sidebar-item-disable-select');
  2913. line.innerHTML = '&nbsp';
  2914. this.element.appendChild(line);
  2915. }
  2916. return super.render();
  2917. }
  2918. expand() {
  2919. try {
  2920. const content = this.content;
  2921. const container = this.createElement('div', 'sidebar-item-value-line-border');
  2922. container.appendChild(content);
  2923. this.element.appendChild(container);
  2924. } catch (error) {
  2925. this.error(error, false);
  2926. }
  2927. }
  2928. get content() {
  2929. const content = this.createElement('pre');
  2930. const value = this._value;
  2931. const tensor = this._tensor;
  2932. if (tensor.encoding !== '<' && tensor.encoding !== '>' && tensor.encoding !== '|') {
  2933. content.innerHTML = `Tensor encoding '${tensor.layout}' is not implemented.`;
  2934. } else if (tensor.layout && (tensor.layout !== 'sparse' && tensor.layout !== 'sparse.coo')) {
  2935. content.innerHTML = `Tensor layout '${tensor.layout}' is not implemented.`;
  2936. } else if (tensor.empty) {
  2937. content.innerHTML = 'Tensor data is empty.';
  2938. } else if (tensor.type && tensor.type.dataType === '?') {
  2939. content.innerHTML = 'Tensor data type is not defined.';
  2940. } else if (tensor.type && !tensor.type.shape) {
  2941. content.innerHTML = 'Tensor shape is not defined.';
  2942. } else {
  2943. content.innerHTML = tensor.toString();
  2944. if (this._host.save && value.type.shape && value.type.shape.dimensions && value.type.shape.dimensions.length > 0) {
  2945. this._saveButton = this.createElement('div', 'sidebar-item-value-button');
  2946. this._saveButton.classList.add('sidebar-item-value-button-context');
  2947. this._saveButton.setAttribute('style', 'float: right;');
  2948. this._saveButton.innerHTML = '&#x1F4BE;';
  2949. this._saveButton.addEventListener('click', async () => {
  2950. await this.export();
  2951. });
  2952. content.insertBefore(this._saveButton, content.firstChild);
  2953. }
  2954. }
  2955. return content;
  2956. }
  2957. error(error, fatal) {
  2958. super.error(error, fatal);
  2959. const element = this.createElement('div', 'sidebar-item-value-line');
  2960. element.innerHTML = `<b>ERROR:</b> ${error.message}`;
  2961. this.element.appendChild(element);
  2962. }
  2963. async export() {
  2964. const tensor = this._tensor;
  2965. const defaultPath = tensor.name ? tensor.name.split('/').join('_').split(':').join('_').split('.').join('_') : 'tensor';
  2966. const file = await this._host.save('NumPy Array', 'npy', defaultPath);
  2967. if (file) {
  2968. try {
  2969. let data_type = '?';
  2970. switch (tensor.type.dataType) {
  2971. case 'boolean': data_type = 'bool'; break;
  2972. case 'bfloat16': data_type = 'float32'; break;
  2973. case 'float8e5m2': data_type = 'float16'; break;
  2974. case 'float8e5m2fnuz': data_type = 'float16'; break;
  2975. case 'float8e4m3fn': data_type = 'float16'; break;
  2976. case 'float8e4m3fnuz': data_type = 'float16'; break;
  2977. case 'int4': data_type = 'int8'; break;
  2978. default: data_type = tensor.type.dataType; break;
  2979. }
  2980. const execution = new python.Execution();
  2981. const bytes = execution.invoke('io.BytesIO', []);
  2982. const dtype = execution.invoke('numpy.dtype', [data_type]);
  2983. const array = execution.invoke('numpy.asarray', [tensor.value, dtype]);
  2984. execution.invoke('numpy.save', [bytes, array]);
  2985. bytes.seek(0);
  2986. const blob = new Blob([bytes.read()], { type: 'application/octet-stream' });
  2987. await this._host.export(file, blob);
  2988. } catch (error) {
  2989. this.error(error, 'Error saving NumPy tensor.', null);
  2990. }
  2991. }
  2992. }
  2993. };
  2994. view.NodeView = class extends view.Expander {
  2995. constructor(context, node) {
  2996. super(context);
  2997. this._node = node;
  2998. const name = node.name;
  2999. const type = node.type ? node.type.name : '';
  3000. if (name && type) {
  3001. this.enable();
  3002. }
  3003. if (type) {
  3004. const type = node.type.name;
  3005. const element = this.createElement('div', 'sidebar-item-value-line');
  3006. element.innerHTML = `<span class='sidebar-item-value-line-content'>node: <b>${type || ' '}</b></span>`;
  3007. element.addEventListener('pointerenter', () => this.emit('focus', this._node));
  3008. element.addEventListener('pointerleave', () => this.emit('blur', this._node));
  3009. element.addEventListener('click', () => this.emit('activate', this._node));
  3010. element.style.cursor = 'pointer';
  3011. this.element.appendChild(element);
  3012. } else {
  3013. const element = this.createElement('div', 'sidebar-item-value-line');
  3014. element.innerHTML = `<span class='sidebar-item-value-line-content'>name: <b>${name || ' '}</b></span>`;
  3015. element.addEventListener('pointerenter', () => this.emit('focus', this._node));
  3016. element.addEventListener('pointerleave', () => this.emit('blur', this._node));
  3017. element.addEventListener('click', () => this.emit('activate', this._node));
  3018. element.style.cursor = 'pointer';
  3019. this.element.appendChild(element);
  3020. }
  3021. }
  3022. expand() {
  3023. const name = this._node.name;
  3024. const element = this.createElement('div', 'sidebar-item-value-line-border');
  3025. element.innerHTML = `<span class='sidebar-item-value-line-content'>name: <b>${name}</b></span>`;
  3026. element.addEventListener('pointerenter', () => this.emit('focus', this._node));
  3027. element.addEventListener('pointerleave', () => this.emit('blur', this._node));
  3028. element.addEventListener('click', () => this.emit('activate', this._node));
  3029. element.style.cursor = 'pointer';
  3030. this.element.appendChild(element);
  3031. }
  3032. };
  3033. view.NodeListView = class extends view.Control {
  3034. constructor(context, list) {
  3035. super(context);
  3036. this._elements = [];
  3037. for (const node of list) {
  3038. const item = new view.NodeView(this._view, node);
  3039. item.on('focus', (sender, value) => this.emit('focus', value));
  3040. item.on('blur', (sender, value) => this.emit('blur', value));
  3041. item.on('activate', (sender, value) => this.emit('activate', value));
  3042. item.on('deactivate', (sender, value) => this.emit('deactivate', value));
  3043. item.on('select', (sender, value) => this.emit('select', value));
  3044. item.toggle();
  3045. for (const element of item.render()) {
  3046. this._elements.push(element);
  3047. }
  3048. }
  3049. }
  3050. render() {
  3051. return this._elements;
  3052. }
  3053. };
  3054. view.ConnectionSidebar = class extends view.ObjectSidebar {
  3055. constructor(context, value, from, to) {
  3056. super(context);
  3057. this._value = value;
  3058. this._from = from;
  3059. this._to = to;
  3060. }
  3061. render() {
  3062. const value = this._value;
  3063. const from = this._from;
  3064. const to = this._to;
  3065. const [name] = value.name.split('\n');
  3066. this.addProperty('name', name);
  3067. if (value.type) {
  3068. const item = new view.ValueView(this._view, value);
  3069. this.addEntry('type', item);
  3070. item.toggle();
  3071. }
  3072. if (from) {
  3073. this.addHeader('Inputs');
  3074. this.addNodeList('from', [from]);
  3075. }
  3076. if (Array.isArray(to) && to.length > 0) {
  3077. this.addHeader('Outputs');
  3078. this.addNodeList('to', to);
  3079. }
  3080. }
  3081. addNodeList(name, list) {
  3082. const entry = new view.NodeListView(this._view, list);
  3083. entry.on('focus', (sender, value) => {
  3084. this.emit('focus', value);
  3085. this._focused = this._focused || new Set();
  3086. this._focused.add(value);
  3087. });
  3088. entry.on('blur', (sender, value) => {
  3089. this.emit('blur', value);
  3090. this._focused = this._focused || new Set();
  3091. this._focused.delete(value);
  3092. });
  3093. entry.on('select', (sender, value) => this.emit('select', value));
  3094. entry.on('activate', (sender, value) => this.emit('activate', value));
  3095. this.addEntry(name, entry);
  3096. }
  3097. activate() {
  3098. this.emit('select', this._value);
  3099. }
  3100. deactivate() {
  3101. this.emit('select', null);
  3102. if (this._focused) {
  3103. for (const value of this._focused) {
  3104. this.emit('blur', value);
  3105. }
  3106. this._focused.clear();
  3107. }
  3108. }
  3109. };
  3110. view.TensorSidebar = class extends view.ObjectSidebar {
  3111. constructor(context, value) {
  3112. super(context);
  3113. this._value = value;
  3114. this._tensor = new base.Tensor(value.value[0].initializer);
  3115. }
  3116. render() {
  3117. const [value] = this._value.value;
  3118. const tensor = value.initializer;
  3119. const name = tensor && tensor.name ? tensor.name : value.name.split('\n')[0];
  3120. if (name) {
  3121. this.addProperty('name', name);
  3122. }
  3123. if (tensor) {
  3124. const category = tensor.category;
  3125. if (category) {
  3126. this.addProperty('category', category);
  3127. }
  3128. const description = tensor.description;
  3129. if (description) {
  3130. this.addProperty('description', description);
  3131. }
  3132. const type = tensor.type;
  3133. if (type) {
  3134. const dataType = type.dataType;
  3135. this.addProperty('type', `${dataType}`, 'code');
  3136. const shape = type.shape && Array.isArray(type.shape.dimensions) ? type.shape.dimensions.toString(', ') : '?';
  3137. this.addProperty('shape', `${shape}`, 'code');
  3138. const denotation = type.denotation;
  3139. if (denotation) {
  3140. this.addProperty('denotation', denotation, 'code');
  3141. }
  3142. const layout = type.layout;
  3143. if (layout) {
  3144. this.addProperty('layout', layout.replace('.', ' '));
  3145. }
  3146. }
  3147. const location = tensor.location;
  3148. if (location) {
  3149. this.addProperty('location', tensor.location);
  3150. }
  3151. const stride = tensor.stride;
  3152. if (Array.isArray(stride) && stride.length > 0) {
  3153. this.addProperty('stride', stride.join(','), 'code');
  3154. }
  3155. const value = new view.TensorView(this._view, tensor, this._tensor);
  3156. this.addEntry('value', value);
  3157. const metadata = tensor.metadata;
  3158. if (Array.isArray(metadata) && metadata.length > 0) {
  3159. this.addHeader('Metadata');
  3160. for (const argument of tensor.metadata) {
  3161. this.addProperty(argument.name, argument.value);
  3162. }
  3163. }
  3164. }
  3165. // Metrics
  3166. if (value.initializer) {
  3167. if (!this._tensor.empty) {
  3168. if (!this._metrics) {
  3169. this._metrics = new metrics.Tensor(this._tensor);
  3170. }
  3171. if (this._metrics.metrics.length > 0) {
  3172. this.addHeader('Metrics');
  3173. for (const metric of this._metrics.metrics) {
  3174. const value = metric.type === 'percentage' ? `${(metric.value * 100).toFixed(1)}%` : metric.value;
  3175. this.addProperty(metric.name, [value]);
  3176. }
  3177. }
  3178. }
  3179. }
  3180. }
  3181. activate() {
  3182. this.emit('select', this._value);
  3183. }
  3184. deactivate() {
  3185. this.emit('select', null);
  3186. }
  3187. };
  3188. view.ModelSidebar = class extends view.ObjectSidebar {
  3189. constructor(context, model, graph, signature) {
  3190. super(context);
  3191. this._model = model;
  3192. this._graph = graph;
  3193. this._signature = signature;
  3194. }
  3195. render() {
  3196. const model = this._model;
  3197. const graph = this._graph;
  3198. const signature = this._signature;
  3199. if (model.format) {
  3200. this.addProperty('format', model.format);
  3201. }
  3202. if (model.producer) {
  3203. this.addProperty('producer', model.producer);
  3204. }
  3205. if (model.name) {
  3206. this.addProperty('name', model.name);
  3207. }
  3208. if (model.version) {
  3209. this.addProperty('version', model.version);
  3210. }
  3211. if (model.description) {
  3212. this.addProperty('description', model.description);
  3213. }
  3214. if (model.domain) {
  3215. this.addProperty('domain', model.domain);
  3216. }
  3217. if (model.imports) {
  3218. this.addProperty('imports', model.imports);
  3219. }
  3220. if (model.runtime) {
  3221. this.addProperty('runtime', model.runtime);
  3222. }
  3223. if (model.source) {
  3224. this.addProperty('source', model.source);
  3225. }
  3226. const graphs = Array.isArray(model.graphs) ? model.graphs : [];
  3227. if (graphs.length === 1 && graphs[0].name) {
  3228. this.addProperty('graph', graphs[0].name);
  3229. } else if (graphs.length > 1) {
  3230. const entries = new Map();
  3231. for (const graph of model.graphs) {
  3232. entries.set(graph.name, graph);
  3233. }
  3234. const selector = new view.SelectView(this._view, entries, graph);
  3235. selector.on('change', (sender, data) => this.emit('update-active-graph', data));
  3236. this.addEntry('graph', selector);
  3237. }
  3238. if (graph && Array.isArray(graph.signatures) && graph.signatures.length > 0) {
  3239. const entries = new Map();
  3240. entries.set('', graph);
  3241. for (const signature of graph.signatures) {
  3242. entries.set(signature.name, signature);
  3243. }
  3244. const selector = new view.SelectView(this._view, entries, signature || graph);
  3245. selector.on('change', (sender, data) => this.emit('update-active-graph-signature', data));
  3246. this.addEntry('signature', selector);
  3247. }
  3248. const metadata = model.metadata;
  3249. if (Array.isArray(metadata) && metadata.length > 0) {
  3250. this.addHeader('Metadata');
  3251. for (const argument of metadata) {
  3252. this.addProperty(argument.name, argument.value);
  3253. }
  3254. }
  3255. if (graph) {
  3256. if (graph.version) {
  3257. this.addProperty('version', graph.version);
  3258. }
  3259. if (graph.type) {
  3260. this.addProperty('type', graph.type);
  3261. }
  3262. if (graph.tags) {
  3263. this.addProperty('tags', graph.tags);
  3264. }
  3265. if (graph.description) {
  3266. this.addProperty('description', graph.description);
  3267. }
  3268. const inputs = signature ? signature.inputs : graph.inputs;
  3269. const outputs = signature ? signature.outputs : graph.outputs;
  3270. if (Array.isArray(inputs) && inputs.length > 0) {
  3271. this.addHeader('Inputs');
  3272. for (const input of inputs) {
  3273. this.addArgument(input.name, input);
  3274. }
  3275. }
  3276. if (Array.isArray(outputs) && outputs.length > 0) {
  3277. this.addHeader('Outputs');
  3278. for (const output of outputs) {
  3279. this.addArgument(output.name, output);
  3280. }
  3281. }
  3282. const metadata = graph.metadata;
  3283. if (Array.isArray(metadata) && metadata.length > 0) {
  3284. this.addHeader('Metadata');
  3285. for (const argument of metadata) {
  3286. this.addProperty(argument.name, argument.value);
  3287. }
  3288. }
  3289. }
  3290. }
  3291. addArgument(name, argument) {
  3292. const value = new view.ArgumentView(this._view, argument);
  3293. value.on('focus', (sender, value) => this.emit('focus', value));
  3294. value.on('blur', (sender, value) => this.emit('blur', value));
  3295. value.on('activate', (sender, value) => this.emit('activate', value));
  3296. value.on('deactivate', (sender, value) => this.emit('deactivate', value));
  3297. value.on('select', (sender, value) => this.emit('select', value));
  3298. value.toggle();
  3299. this.addEntry(name, value);
  3300. }
  3301. };
  3302. view.DocumentationSidebar = class extends view.Control {
  3303. constructor(context, type) {
  3304. super(context);
  3305. this._type = type;
  3306. }
  3307. render() {
  3308. if (!this.element) {
  3309. this.element = this.createElement('div', 'sidebar-documentation');
  3310. const type = view.Documentation.open(this._type);
  3311. this._append(this.element, 'h1', type.name);
  3312. if (type.summary) {
  3313. this._append(this.element, 'p', type.summary);
  3314. }
  3315. if (type.description) {
  3316. this._append(this.element, 'p', type.description);
  3317. }
  3318. if (Array.isArray(type.attributes) && type.attributes.length > 0) {
  3319. this._append(this.element, 'h2', 'Attributes');
  3320. const attributes = this._append(this.element, 'dl');
  3321. for (const attribute of type.attributes) {
  3322. this._append(attributes, 'dt', attribute.name + (attribute.type ? `: <tt>${attribute.type}</tt>` : ''));
  3323. this._append(attributes, 'dd', attribute.description);
  3324. }
  3325. this.element.appendChild(attributes);
  3326. }
  3327. if (Array.isArray(type.inputs) && type.inputs.length > 0) {
  3328. this._append(this.element, 'h2', `Inputs${type.inputs_range ? ` (${type.inputs_range})` : ''}`);
  3329. const inputs = this._append(this.element, 'dl');
  3330. for (const input of type.inputs) {
  3331. this._append(inputs, 'dt', input.name + (input.type ? `: <tt>${input.type}</tt>` : '') + (input.option ? ` (${input.option})` : ''));
  3332. this._append(inputs, 'dd', input.description);
  3333. }
  3334. }
  3335. if (Array.isArray(type.outputs) && type.outputs.length > 0) {
  3336. this._append(this.element, 'h2', `Outputs${type.outputs_range ? ` (${type.outputs_range})` : ''}`);
  3337. const outputs = this._append(this.element, 'dl');
  3338. for (const output of type.outputs) {
  3339. this._append(outputs, 'dt', output.name + (output.type ? `: <tt>${output.type}</tt>` : '') + (output.option ? ` (${output.option})` : ''));
  3340. this._append(outputs, 'dd', output.description);
  3341. }
  3342. }
  3343. if (Array.isArray(type.type_constraints) && type.type_constraints.length > 0) {
  3344. this._append(this.element, 'h2', 'Type Constraints');
  3345. const type_constraints = this._append(this.element, 'dl');
  3346. for (const type_constraint of type.type_constraints) {
  3347. this._append(type_constraints, 'dt', `${type_constraint.type_param_str}: ${type_constraint.allowed_type_strs.map((item) => `<tt>${item}</tt>`).join(', ')}`);
  3348. this._append(type_constraints, 'dd', type_constraint.description);
  3349. }
  3350. }
  3351. if (Array.isArray(type.examples) && type.examples.length > 0) {
  3352. this._append(this.element, 'h2', 'Examples');
  3353. for (const example of type.examples) {
  3354. this._append(this.element, 'h3', example.summary);
  3355. this._append(this.element, 'pre', example.code);
  3356. }
  3357. }
  3358. if (Array.isArray(type.references) && type.references.length > 0) {
  3359. this._append(this.element, 'h2', 'References');
  3360. const references = this._append(this.element, 'ul');
  3361. for (const reference of type.references) {
  3362. this._append(references, 'li', reference.description);
  3363. }
  3364. }
  3365. if (this._host.type === 'Electron') {
  3366. this.element.addEventListener('click', (e) => {
  3367. if (e.target && e.target.href) {
  3368. const url = e.target.href;
  3369. if (url.startsWith('http://') || url.startsWith('https://')) {
  3370. e.preventDefault();
  3371. this.emit('navigate', { link: url });
  3372. }
  3373. }
  3374. });
  3375. }
  3376. }
  3377. }
  3378. _append(parent, type, content) {
  3379. const element = this.createElement(type);
  3380. if (content) {
  3381. element.innerHTML = content;
  3382. }
  3383. parent.appendChild(element);
  3384. return element;
  3385. }
  3386. error(error, fatal) {
  3387. super.error(error, fatal);
  3388. const element = this.createElement('span');
  3389. element.innerHTML = `<b>ERROR:</b> ${error.message}`;
  3390. this.element.appendChild(element);
  3391. }
  3392. };
  3393. view.FindSidebar = class extends view.Control {
  3394. constructor(context, graph, signature) {
  3395. super(context);
  3396. this._graph = graph;
  3397. this._signature = signature;
  3398. this._state = {
  3399. query: '',
  3400. node: true,
  3401. connection: true,
  3402. weight: true
  3403. };
  3404. this._toggles = {
  3405. node: { hide: 'Hide Nodes', show: 'Show Nodes' },
  3406. connection: { hide: 'Hide Connections', show: 'Show Connections' },
  3407. weight: { hide: 'Hide Weights', show: 'Show Weights' }
  3408. };
  3409. }
  3410. on(event, callback) {
  3411. this._events = this._events || {};
  3412. this._events[event] = this._events[event] || [];
  3413. this._events[event].push(callback);
  3414. }
  3415. emit(event, data) {
  3416. if (this._events && this._events[event]) {
  3417. for (const callback of this._events[event]) {
  3418. callback(this, data);
  3419. }
  3420. }
  3421. }
  3422. focus(state) {
  3423. this._state = state || this._state;
  3424. this._query.focus();
  3425. this._query.value = '';
  3426. this._query.value = this._state.query;
  3427. for (const [name, toggle] of Object.entries(this._toggles)) {
  3428. toggle.checkbox.checked = this._state[name];
  3429. toggle.element.setAttribute('title', this._state[name] ? toggle.hide : toggle.show);
  3430. }
  3431. this._update();
  3432. }
  3433. _clear() {
  3434. for (const identifier of this._focused) {
  3435. this._blur(identifier);
  3436. }
  3437. this._focused.clear();
  3438. this._table.clear();
  3439. this._edges.clear();
  3440. this._index = 0;
  3441. const unquote = this._state.query.match(new RegExp(/^'(.*)'|"(.*)"$/));
  3442. if (unquote) {
  3443. this._exact = true;
  3444. const term = unquote[1] || unquote[2];
  3445. this._terms = [term];
  3446. } else {
  3447. this._exact = false;
  3448. this._terms = this._state.query.trim().toLowerCase().split(' ').map((term) => term.trim()).filter((term) => term.length > 0);
  3449. }
  3450. }
  3451. _term(value) {
  3452. if (this._exact) {
  3453. return value === this._terms[0];
  3454. }
  3455. value = value.toLowerCase();
  3456. return this._terms.every((term) => value.indexOf(term) !== -1);
  3457. }
  3458. _value(value) {
  3459. if (this._terms.length === 0) {
  3460. return true;
  3461. }
  3462. if (value.name && this._term(value.name.split('\n').shift())) {
  3463. return true;
  3464. }
  3465. if (value.identifier && this._term(value.identifier)) {
  3466. return true;
  3467. }
  3468. if (value.type && !this._exact) {
  3469. for (const term of this._terms) {
  3470. if (value.type.dataType && term === value.type.dataType.toLowerCase()) {
  3471. return true;
  3472. }
  3473. if (value.type.shape) {
  3474. if (term === value.type.shape.toString().toLowerCase()) {
  3475. return true;
  3476. }
  3477. if (value.type.shape && Array.isArray(value.type.shape.dimensions)) {
  3478. const dimensions = value.type.shape.dimensions.map((dimension) => dimension ? dimension.toString().toLowerCase() : '');
  3479. if (term === dimensions.join(',')) {
  3480. return true;
  3481. }
  3482. if (dimensions.some((dimension) => term === dimension)) {
  3483. return true;
  3484. }
  3485. }
  3486. }
  3487. }
  3488. }
  3489. return false;
  3490. }
  3491. _edge(value) {
  3492. if (value.name && !this._edges.has(value.name) && this._value(value)) {
  3493. const content = `${value.name.split('\n').shift()}`;
  3494. this._add(value, content, 'connection'); // split custom argument id
  3495. this._edges.add(value.name);
  3496. }
  3497. }
  3498. _node(node) {
  3499. if (this._state.connection) {
  3500. for (const input of node.inputs) {
  3501. if (!input.type || input.type.endsWith('*')) {
  3502. for (const value of input.value) {
  3503. if (!value.initializer) {
  3504. this._edge(value);
  3505. }
  3506. }
  3507. }
  3508. }
  3509. }
  3510. if (this._state.node) {
  3511. const name = node.name;
  3512. const type = node.type.name;
  3513. const identifier = node.identifier;
  3514. if ((name && this._term(name)) || (type && this._term(type)) || (identifier && this._term(identifier))) {
  3515. const content = `${name || `[${type}]`}`;
  3516. this._add(node, content, 'node');
  3517. }
  3518. }
  3519. if (this._state.weight) {
  3520. for (const argument of node.inputs) {
  3521. if (!argument.type || argument.type.endsWith('*')) {
  3522. for (const value of argument.value) {
  3523. if (value.initializer && this._value(value)) {
  3524. let content = null;
  3525. if (value.name) {
  3526. content = `${value.name.split('\n').shift()}`; // split custom argument id
  3527. } else if (value.type && value.type.shape && Array.isArray(value.type.shape.dimensions) && value.type.shape.dimensions.length > 0) {
  3528. content = `${value.type.shape.dimensions.map((d) => (d !== null && d !== undefined) ? d : '?').join('\u00D7')}`;
  3529. }
  3530. if (content) {
  3531. const target = argument.value.length === 1 ? argument : node;
  3532. this._add(target, content, 'weight');
  3533. }
  3534. }
  3535. }
  3536. } else if (argument.type === 'object') {
  3537. this._node(argument.value);
  3538. } else if (argument.type === 'object[]') {
  3539. for (const value of argument.value) {
  3540. this._node(value);
  3541. }
  3542. }
  3543. }
  3544. }
  3545. }
  3546. _add(value, content, icon) {
  3547. const key = this._index.toString();
  3548. this._index++;
  3549. this._table.set(key, value);
  3550. const element = this.createElement('li');
  3551. element.innerHTML = `<svg class='sidebar-find-content-icon'><use href="#sidebar-icon-${icon}"></use></svg>`;
  3552. const text = this.createElement('span');
  3553. text.innerText = content;
  3554. element.appendChild(text);
  3555. element.setAttribute('data', key);
  3556. element.addEventListener('pointerover', (e) => {
  3557. const identifier = e.target.getAttribute('data');
  3558. this._focus(identifier);
  3559. });
  3560. element.addEventListener('pointerleave', (e) => {
  3561. const identifier = e.target.getAttribute('data');
  3562. this._blur(identifier);
  3563. });
  3564. this._content.appendChild(element);
  3565. }
  3566. _focus(identifier) {
  3567. if (this._table.has(identifier)) {
  3568. this.emit('focus', this._table.get(identifier));
  3569. this._focused.add(identifier);
  3570. }
  3571. }
  3572. _blur(identifier) {
  3573. if (this._table.has(identifier)) {
  3574. this.emit('blur', this._table.get(identifier));
  3575. this._focused.delete(identifier);
  3576. }
  3577. }
  3578. _update() {
  3579. this._content.innerHTML = '';
  3580. try {
  3581. this._clear();
  3582. const inputs = this._signature ? this._signature.inputs : this._graph.inputs;
  3583. if (this._state.connection) {
  3584. for (const input of inputs) {
  3585. for (const value of input.value) {
  3586. this._edge(value);
  3587. }
  3588. }
  3589. }
  3590. for (const node of this._graph.nodes) {
  3591. this._node(node);
  3592. }
  3593. if (this._state.connection) {
  3594. const outputs = this._signature ? this._signature.outputs : this._graph.inputs;
  3595. for (const output of outputs) {
  3596. if (!output.type || output.type.endsWith('*')) {
  3597. for (const value of output.value) {
  3598. this._edge(value);
  3599. }
  3600. }
  3601. }
  3602. }
  3603. } catch (error) {
  3604. this.error(error, false);
  3605. }
  3606. }
  3607. render() {
  3608. this._table = new Map();
  3609. this._focused = new Set();
  3610. this._edges = new Set();
  3611. this._search = this.createElement('div', 'sidebar-find-search');
  3612. this._query = this.createElement('input', 'sidebar-find-query');
  3613. this._search.appendChild(this._query);
  3614. this._content = this.createElement('ol', 'sidebar-find-content');
  3615. this._elements = [this._query, this._content];
  3616. this._query.setAttribute('id', 'search');
  3617. this._query.setAttribute('type', 'text');
  3618. this._query.setAttribute('spellcheck', 'false');
  3619. this._query.setAttribute('placeholder', 'Search');
  3620. this._query.addEventListener('input', (e) => {
  3621. this._state.query = e.target.value;
  3622. this.emit('state-changed', this._state);
  3623. this._update();
  3624. });
  3625. this._query.addEventListener('keydown', (e) => {
  3626. if (e.keyCode === 0x08 && !e.altKey && !e.ctrlKey && !e.shiftKey && !e.metaKey) {
  3627. e.stopPropagation();
  3628. }
  3629. });
  3630. for (const [name, toggle] of Object.entries(this._toggles)) {
  3631. toggle.element = this.createElement('label', 'sidebar-find-toggle');
  3632. toggle.element.innerHTML = `<svg class='sidebar-find-toggle-icon'><use href="#sidebar-icon-${name}"></use></svg>`;
  3633. toggle.element.setAttribute('title', this._state[name] ? toggle.hide : toggle.show);
  3634. toggle.checkbox = this.createElement('input');
  3635. toggle.checkbox.setAttribute('type', 'checkbox');
  3636. toggle.checkbox.setAttribute('data', name);
  3637. toggle.checkbox.addEventListener('change', (e) => {
  3638. const name = e.target.getAttribute('data');
  3639. this._state[name] = e.target.checked;
  3640. const toggle = this._toggles[name];
  3641. toggle.element.setAttribute('title', e.target.checked ? toggle.hide : toggle.show);
  3642. this.emit('state-changed', this._state);
  3643. this._update();
  3644. });
  3645. toggle.element.insertBefore(toggle.checkbox, toggle.element.firstChild);
  3646. this._search.appendChild(toggle.element);
  3647. }
  3648. this._content.addEventListener('click', (e) => {
  3649. const name = e.target.getAttribute('data');
  3650. if (this._table.has(name)) {
  3651. this.emit('select', this._table.get(name));
  3652. }
  3653. });
  3654. this._content.addEventListener('dblclick', (e) => {
  3655. const name = e.target.getAttribute('data');
  3656. if (this._table.has(name)) {
  3657. this.emit('activate', this._table.get(name));
  3658. }
  3659. });
  3660. }
  3661. get element() {
  3662. return [this._search, this._content];
  3663. }
  3664. deactivate() {
  3665. for (const identifier of this._focused) {
  3666. this._blur(identifier);
  3667. }
  3668. this._focused.clear();
  3669. }
  3670. error(error, fatal) {
  3671. super.error(error, fatal);
  3672. const element = this.createElement('li');
  3673. element.innerHTML = `<b>ERROR:</b> ${error.message}`;
  3674. this._content.appendChild(element);
  3675. }
  3676. };
  3677. view.Quantization = class {
  3678. constructor(quantization) {
  3679. Object.assign(this, quantization);
  3680. }
  3681. toString() {
  3682. if (this.type === 'linear' || /^quant\d\d?_.*$/.test(this.type)) {
  3683. const content = [];
  3684. const scale = this.scale || [];
  3685. const offset = this.offset || [];
  3686. const bias = this.bias || [];
  3687. const max = this.max || [];
  3688. const min = this.min || [];
  3689. const length = Math.max(scale.length, offset.length, bias.length, min.length, max.length);
  3690. const size = length.toString().length;
  3691. for (let i = 0; i < length; i++) {
  3692. let s = 'q';
  3693. let bracket = false;
  3694. if (i < offset.length && offset[i] !== undefined && offset[i] !== 0 && offset[i] !== 0n) {
  3695. const value = offset[i];
  3696. s = value > 0 ? `${s} - ${value}` : `${s} + ${-value}`;
  3697. bracket = true;
  3698. }
  3699. if (i < scale.length && scale[i] !== undefined && scale[i] !== 1 && scale[i] !== 1n) {
  3700. const value = scale[i];
  3701. s = bracket ? `(${s})` : s;
  3702. s = `${value} * ${s}`;
  3703. bracket = true;
  3704. }
  3705. if (i < bias.length && bias[i] !== undefined && bias[i] !== 0 && bias[i] !== 0n) {
  3706. const value = bias[i];
  3707. s = bracket ? `(${s})` : s;
  3708. s = value < 0 ? `${s} - ${-value}` : `${s} + ${value}`;
  3709. }
  3710. if (i < min.length && min[i] !== undefined && min[i] !== 0 && min[i] !== 0n) {
  3711. s = `${min[i]} \u2264 ${s}`;
  3712. }
  3713. if (i < max.length && max[i] !== undefined && max[i] !== 0 && max[i] !== 0n) {
  3714. s = `${s} \u2264 ${max[i]}`;
  3715. }
  3716. content.push(length > 1 ? `${i.toString().padStart(size, ' ')}: ${s}` : `${s}`);
  3717. }
  3718. return content.join('\n');
  3719. } else if (this.type === 'lookup') {
  3720. const size = this.value.length.toString().length;
  3721. return this.value.map((value, index) => `${index.toString().padStart(size, ' ')}: ${value}`).join('\n');
  3722. } else if (this.type === 'annotation') {
  3723. return Array.from(this.value).map(([name, value]) => `${name} = ${value}`).join('\n');
  3724. } else if (/^q\d_[01k]$/.test(this.type) || /^iq\d_[xsnlm]+$/.test(this.type)) {
  3725. return '';
  3726. }
  3727. throw new view.Error(`Unknown quantization type '${this.type}'.`);
  3728. }
  3729. };
  3730. view.Documentation = class {
  3731. static open(source) {
  3732. if (source) {
  3733. const generator = markdown.Generator.open();
  3734. const target = {};
  3735. if (source.name) {
  3736. target.name = source.name;
  3737. }
  3738. if (source.module) {
  3739. target.module = source.module;
  3740. }
  3741. if (source.category) {
  3742. target.category = source.category;
  3743. }
  3744. if (source.summary) {
  3745. target.summary = generator.html(source.summary);
  3746. }
  3747. if (source.description) {
  3748. target.description = generator.html(source.description);
  3749. }
  3750. if (Array.isArray(source.attributes)) {
  3751. target.attributes = source.attributes.map((source) => {
  3752. const target = {};
  3753. target.name = source.name;
  3754. if (source.type !== undefined) {
  3755. target.type = source.type;
  3756. }
  3757. if (source.option !== undefined) {
  3758. target.option = source.option;
  3759. }
  3760. if (source.optional !== undefined) {
  3761. target.optional = source.optional;
  3762. }
  3763. if (source.required !== undefined) {
  3764. target.required = source.required;
  3765. }
  3766. if (source.minimum !== undefined) {
  3767. target.minimum = source.minimum;
  3768. }
  3769. if (source.src !== undefined) {
  3770. target.src = source.src;
  3771. }
  3772. if (source.src_type !== undefined) {
  3773. target.src_type = source.src_type;
  3774. }
  3775. if (source.description !== undefined) {
  3776. target.description = generator.html(source.description);
  3777. }
  3778. if (source.default !== undefined) {
  3779. target.default = source.default;
  3780. }
  3781. if (source.visible !== undefined) {
  3782. target.visible = source.visible;
  3783. }
  3784. return target;
  3785. });
  3786. }
  3787. if (Array.isArray(source.inputs)) {
  3788. target.inputs = source.inputs.map((source) => {
  3789. const target = {};
  3790. target.name = source.name;
  3791. if (source.type !== undefined) {
  3792. target.type = source.type;
  3793. }
  3794. if (source.description) {
  3795. target.description = generator.html(source.description);
  3796. }
  3797. if (source.default !== undefined) {
  3798. target.default = source.default;
  3799. }
  3800. if (source.src !== undefined) {
  3801. target.src = source.src;
  3802. }
  3803. if (source.list !== undefined) {
  3804. target.list = source.list;
  3805. }
  3806. if (source.isRef !== undefined) {
  3807. target.isRef = source.isRef;
  3808. }
  3809. if (source.typeAttr !== undefined) {
  3810. target.typeAttr = source.typeAttr;
  3811. }
  3812. if (source.numberAttr !== undefined) {
  3813. target.numberAttr = source.numberAttr;
  3814. }
  3815. if (source.typeListAttr !== undefined) {
  3816. target.typeListAttr = source.typeListAttr;
  3817. }
  3818. if (source.option !== undefined) {
  3819. target.option = source.option;
  3820. }
  3821. if (source.optional !== undefined) {
  3822. target.optional = source.optional;
  3823. }
  3824. if (source.visible !== undefined) {
  3825. target.visible = source.visible;
  3826. }
  3827. return target;
  3828. });
  3829. }
  3830. if (Array.isArray(source.outputs)) {
  3831. target.outputs = source.outputs.map((source) => {
  3832. const target = {};
  3833. target.name = source.name;
  3834. if (source.type) {
  3835. target.type = source.type;
  3836. }
  3837. if (source.description) {
  3838. target.description = generator.html(source.description);
  3839. }
  3840. if (source.list !== undefined) {
  3841. target.list = source.list;
  3842. }
  3843. if (source.typeAttr !== undefined) {
  3844. target.typeAttr = source.typeAttr;
  3845. }
  3846. if (source.typeListAttr !== undefined) {
  3847. target.typeListAttr = source.typeAttr;
  3848. }
  3849. if (source.numberAttr !== undefined) {
  3850. target.numberAttr = source.numberAttr;
  3851. }
  3852. if (source.isRef !== undefined) {
  3853. target.isRef = source.isRef;
  3854. }
  3855. if (source.option !== undefined) {
  3856. target.option = source.option;
  3857. }
  3858. return target;
  3859. });
  3860. }
  3861. if (Array.isArray(source.references)) {
  3862. target.references = source.references.map((source) => {
  3863. if (source) {
  3864. target.description = generator.html(source.description);
  3865. }
  3866. return target;
  3867. });
  3868. }
  3869. if (source.version !== undefined) {
  3870. target.version = source.version;
  3871. }
  3872. if (source.operator !== undefined) {
  3873. target.operator = source.operator;
  3874. }
  3875. if (source.identifier !== undefined) {
  3876. target.identifier = source.identifier;
  3877. }
  3878. if (source.package !== undefined) {
  3879. target.package = source.package;
  3880. }
  3881. if (source.status !== undefined) {
  3882. target.status = source.status;
  3883. }
  3884. if (source.min_input !== undefined) {
  3885. target.min_input = source.min_input;
  3886. }
  3887. if (source.max_input !== undefined) {
  3888. target.max_input = source.max_input;
  3889. }
  3890. if (source.min_output !== undefined) {
  3891. target.min_output = source.min_output;
  3892. }
  3893. if (source.max_input !== undefined) {
  3894. target.max_output = source.max_output;
  3895. }
  3896. if (source.inputs_range !== undefined) {
  3897. target.inputs_range = source.inputs_range;
  3898. }
  3899. if (source.outputs_range !== undefined) {
  3900. target.outputs_range = source.outputs_range;
  3901. }
  3902. if (source.examples !== undefined) {
  3903. target.examples = source.examples;
  3904. }
  3905. if (source.constants !== undefined) {
  3906. target.constants = source.constants;
  3907. }
  3908. if (source.type_constraints !== undefined) {
  3909. target.type_constraints = source.type_constraints;
  3910. }
  3911. return target;
  3912. }
  3913. return null;
  3914. }
  3915. };
  3916. view.Formatter = class {
  3917. constructor(value, type, quote) {
  3918. this._value = value;
  3919. this._type = type;
  3920. this._quote = quote;
  3921. this._values = new Set();
  3922. }
  3923. toString() {
  3924. return this._format(this._value, this._type, this._quote);
  3925. }
  3926. _format(value, type, quote) {
  3927. if (value && value.__class__ && value.__class__.__module__ === 'builtins' && value.__class__.__name__ === 'type') {
  3928. return `${value.__module__}.${value.__name__}`;
  3929. }
  3930. if (value && value.__class__ && value.__class__.__module__ === 'builtins' && value.__class__.__name__ === 'function') {
  3931. return `${value.__module__}.${value.__name__}`;
  3932. }
  3933. if (typeof value === 'function') {
  3934. return value();
  3935. }
  3936. if (value && typeof value === 'bigint') {
  3937. return value.toString();
  3938. }
  3939. if (Number.isNaN(value)) {
  3940. return 'NaN';
  3941. }
  3942. switch (type) {
  3943. case 'shape':
  3944. return value ? value.toString() : '(null)';
  3945. case 'shape[]':
  3946. if (value && !Array.isArray(value)) {
  3947. throw new Error(`Invalid shape '${JSON.stringify(value)}'.`);
  3948. }
  3949. return value ? value.map((item) => item.toString()).join(', ') : '(null)';
  3950. case 'graph':
  3951. return value ? value.name : '(null)';
  3952. case 'graph[]':
  3953. return value ? value.map((graph) => graph.name).join(', ') : '(null)';
  3954. case 'tensor': {
  3955. const type = value.type;
  3956. if (type && type.shape && type.shape.dimensions && Array.isArray(type.shape.dimensions) && type.shape.dimensions.length === 0) {
  3957. const tensor = new base.Tensor(value);
  3958. const encoding = tensor.encoding;
  3959. if ((encoding === '<' || encoding === '>' || encoding === '|') && !tensor.empty && tensor.type.dataType !== '?') {
  3960. let content = tensor.toString();
  3961. if (content && content.length > 10) {
  3962. content = `${content.substring(0, 10)}\u2026`;
  3963. }
  3964. return content;
  3965. }
  3966. }
  3967. return '[\u2026]';
  3968. }
  3969. case 'object':
  3970. case 'function':
  3971. return value.type.name;
  3972. case 'object[]':
  3973. case 'function[]':
  3974. return value ? value.map((item) => item.type.name).join(', ') : '(null)';
  3975. case 'type':
  3976. return value ? value.toString() : '(null)';
  3977. case 'type[]':
  3978. return value ? value.map((item) => item.toString()).join(', ') : '(null)';
  3979. default:
  3980. break;
  3981. }
  3982. if (typeof value === 'string' && (!type || type !== 'string')) {
  3983. return quote ? `"${value}"` : value;
  3984. }
  3985. if (Array.isArray(value)) {
  3986. if (value.length === 0) {
  3987. return quote ? '[]' : '';
  3988. }
  3989. let ellipsis = false;
  3990. if (value.length > 1000) {
  3991. value = value.slice(0, 1000);
  3992. ellipsis = true;
  3993. }
  3994. const itemType = (type && type.endsWith('[]')) ? type.substring(0, type.length - 2) : null;
  3995. const array = value.map((value) => {
  3996. if (value && typeof value === 'bigint') {
  3997. return value.toString();
  3998. }
  3999. if (Number.isNaN(value)) {
  4000. return 'NaN';
  4001. }
  4002. const quote = !itemType || itemType === 'string';
  4003. return this._format(value, itemType, quote);
  4004. });
  4005. if (ellipsis) {
  4006. array.push('\u2026');
  4007. }
  4008. return quote ? ['[', array.join(', '), ']'].join(' ') : array.join(', ');
  4009. }
  4010. if (value === null) {
  4011. return quote ? 'null' : '';
  4012. }
  4013. if (value === undefined) {
  4014. return 'undefined';
  4015. }
  4016. if (value !== Object(value)) {
  4017. return value.toString();
  4018. }
  4019. if (this._values.has(value)) {
  4020. return '\u2026';
  4021. }
  4022. this._values.add(value);
  4023. let list = null;
  4024. const entries = Object.entries(value).filter(([name]) => !name.startsWith('__') && !name.endsWith('__'));
  4025. if (entries.length === 1) {
  4026. list = [this._format(entries[0][1], null, true)];
  4027. } else {
  4028. list = entries.map(([name, value]) => `${name}: ${this._format(value, null, true)}`);
  4029. }
  4030. let objectType = value.__type__;
  4031. if (!objectType && value.constructor.name && value.constructor.name !== 'Object') {
  4032. objectType = value.constructor.name;
  4033. }
  4034. if (objectType) {
  4035. return objectType + (list.length === 0 ? '()' : ['(', list.join(', '), ')'].join(''));
  4036. }
  4037. switch (list.length) {
  4038. case 0:
  4039. return quote ? '()' : '';
  4040. case 1:
  4041. return list[0];
  4042. default:
  4043. return quote ? ['(', list.join(', '), ')'].join(' ') : list.join(', ');
  4044. }
  4045. }
  4046. };
  4047. markdown.Generator = class {
  4048. static open() {
  4049. if (!markdown.Generator.generator) {
  4050. markdown.Generator.generator = new markdown.Generator();
  4051. }
  4052. return markdown.Generator.generator;
  4053. }
  4054. constructor() {
  4055. this._newlineRegExp = /^\n+/;
  4056. this._codeRegExp = /^( {4}[^\n]+\n*)+/;
  4057. this._fencesRegExp = /^ {0,3}(`{3,}(?=[^`\n]*\n)|~{3,})([^\n]*)\n(?:|([\s\S]*?)\n)(?: {0,3}\1[~`]* *(?:\n+|$)|$)/;
  4058. this._hrRegExp = /^ {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)/;
  4059. this._headingRegExp = /^ {0,3}(#{1,6}) +([^\n]*?)(?: +#+)? *(?:\n+|$)/;
  4060. this._blockquoteRegExp = /^( {0,3}> ?(([^\n]+(?:\n(?! {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)| {0,3}#{1,6} | {0,3}>| {0,3}(?:`{3,}(?=[^`\n]*\n)|~{3,})[^\n]*\n| {0,3}(?:[*+-]|1[.)]) |<\/?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?: +|\n|\/?>)|<(?:script|pre|style|!--))[^\n]+)*)|[^\n]*)(?:\n|$))+/;
  4061. this._listRegExp = /^( {0,3})((?:[*+-]|\d{1,9}[.)])) [\s\S]+?(?:\n+(?=\1?(?:(?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$))|\n+(?= {0,3}\[((?!\s*\])(?:\\[[\]]|[^[\]])+)\]: *\n? *<?([^\s>]+)>?(?:(?: +\n? *| *\n *)((?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))))? *(?:\n+|$))|\n{2,}(?! )(?!\1(?:[*+-]|\d{1,9}[.)]) )\n*|\s*$)/;
  4062. this._htmlRegExp = /^ {0,3}(?:<(script|pre|style)[\s>][\s\S]*?(?:<\/\1>[^\n]*\n+|$)|<!--(?!-?>)[\s\S]*?(?:-->|$)[^\n]*(\n+|$)|<\?[\s\S]*?(?:\?>\n*|$)|<![A-Z][\s\S]*?(?:>\n*|$)|<!\[CDATA\[[\s\S]*?(?:\]\]>\n*|$)|<\/?(address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?: +|\n|\/?>)[\s\S]*?(?:\n{2,}|$)|<(?!script|pre|style)([a-z][\w-]*)(?: +[a-zA-Z:_][\w.:-]*(?: *= *"[^"\n]*"| *= *'[^'\n]*'| *= *[^\s"'=<>`]+)?)*? *\/?>(?=[ \t]*(?:\n|$))[\s\S]*?(?:\n{2,}|$)|<\/(?!script|pre|style)[a-z][\w-]*\s*>(?=[ \t]*(?:\n|$))[\s\S]*?(?:\n{2,}|$))/i;
  4063. this._defRegExp = /^ {0,3}\[((?!\s*\])(?:\\[[\]]|[^[\]])+)\]: *\n? *<?([^\s>]+)>?(?:(?: +\n? *| *\n *)((?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))))? *(?:\n+|$)/;
  4064. this._nptableRegExp = /^ *([^|\n ].*\|.*)\n {0,3}([-:]+ *\|[-| :]*)(?:\n((?:(?!\n| {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)| {0,3}#{1,6} | {0,3}>| {4}[^\n]| {0,3}(?:`{3,}(?=[^`\n]*\n)|~{3,})[^\n]*\n| {0,3}(?:[*+-]|1[.)]) |<\/?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?: +|\n|\/?>)|<(?:script|pre|style|!--)).*(?:\n|$))*)\n*|$)/;
  4065. this._tableRegExp = /^ *\|(.+)\n {0,3}\|?( *[-:]+[-| :]*)(?:\n *((?:(?!\n| {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)| {0,3}#{1,6} | {0,3}>| {4}[^\n]| {0,3}(?:`{3,}(?=[^`\n]*\n)|~{3,})[^\n]*\n| {0,3}(?:[*+-]|1[.)]) |<\/?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?: +|\n|\/?>)|<(?:script|pre|style|!--)).*(?:\n|$))*)\n*|$)/;
  4066. this._lheadingRegExp = /^([^\n]+)\n {0,3}(=+|-+) *(?:\n+|$)/;
  4067. this._textRegExp = /^[^\n]+/;
  4068. this._bulletRegExp = /(?:[*+-]|\d{1,9}[.)])/;
  4069. this._itemRegExp = /^( *)((?:[*+-]|\d{1,9}[.)])) ?[^\n]*(?:\n(?!\1(?:[*+-]|\d{1,9}[.)]) ?)[^\n]*)*/gm;
  4070. this._paragraphRegExp = /^([^\n]+(?:\n(?! {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)| {0,3}#{1,6} | {0,3}>| {0,3}(?:`{3,}(?=[^`\n]*\n)|~{3,})[^\n]*\n| {0,3}(?:[*+-]|1[.)]) |<\/?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?: +|\n|\/?>)|<(?:script|pre|style|!--))[^\n]+)*)/;
  4071. this._backpedalRegExp = /(?:[^?!.,:;*_~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_~)]+(?!$))+/;
  4072. this._escapeRegExp = /^\\([!"#$%&'()*+,\-./:;<=>?@[\]\\^_`{|}~~|])/;
  4073. this._escapesRegExp = /\\([!"#$%&'()*+,\-./:;<=>?@[\]\\^_`{|}~])/g;
  4074. /* eslint-disable no-control-regex */
  4075. this._autolinkRegExp = /^<([a-zA-Z][a-zA-Z0-9+.-]{1,31}:[^\s\x00-\x1f<>]*|[a-zA-Z0-9.!#$%&'*+/=?_`{|}~-]+(@)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(?![-_]))>/;
  4076. this._linkRegExp = /^!?\[((?:\[(?:\\.|[^[\]\\])*\]|\\.|`[^`]*`|[^[\]\\`])*?)\]\(\s*(<(?:\\[<>]?|[^\s<>\\])*>|[^\s\x00-\x1f]*)(?:\s+("(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)))?\s*\)/;
  4077. /* eslint-enable no-control-regex */
  4078. this._urlRegExp = /^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9-]+\.?)+[^\s<]*|^[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/i;
  4079. this._tagRegExp = /^<!--(?!-?>)[\s\S]*?-->|^<\/[a-zA-Z][\w:-]*\s*>|^<[a-zA-Z][\w-]*(?:\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?)*?\s*\/?>|^<\?[\s\S]*?\?>|^<![a-zA-Z]+\s[\s\S]*?>|^<!\[CDATA\[[\s\S]*?\]\]>/;
  4080. this._reflinkRegExp = /^!?\[((?:\[(?:\\.|[^[\]\\])*\]|\\.|`[^`]*`|[^[\]\\`])*?)\]\[(?!\s*\])((?:\\[[\]]?|[^[\]\\])+)\]/;
  4081. this._nolinkRegExp = /^!?\[(?!\s*\])((?:\[[^[\]]*\]|\\[[\]]|[^[\]])*)\](?:\[\])?/;
  4082. this._reflinkSearchRegExp = /!?\[((?:\[(?:\\.|[^[\]\\])*\]|\\.|`[^`]*`|[^[\]\\`])*?)\]\[(?!\s*\])((?:\\[[\]]?|[^[\]\\])+)\]|!?\[(?!\s*\])((?:\[[^[\]]*\]|\\[[\]]|[^[\]])*)\](?:\[\])?(?!\()/g;
  4083. this._strongStartRegExp = /^(?:(\*\*(?=[*!"#$%&'()+\-.,/:;<=>?@[\]`{|}~]))|\*\*)(?![\s])|__/;
  4084. this._strongMiddleRegExp = /^\*\*(?:(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^*]|\\\*)|__[^_]*?__|\*\*\[^\*\]*?\*\*)|\*(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^*]|\\\*)|__[^_]*?__|\*\*\[^\*\]*?\*\*)*?\*)+?\*\*$|^__(?![\s])((?:(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^_]|\\_)|__[^_]*?__|\*\*\[^\*\]*?\*\*)|_(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^_]|\\_)|__[^_]*?__|\*\*\[^\*\]*?\*\*)*?_)+?)__$/;
  4085. this._strongEndAstRegExp = /[^!"#$%&'()+\-.,/:;<=>?@[\]`{|}~\s]\*\*(?!\*)|[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~]\*\*(?!\*)(?:(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~_\s]|$))/g;
  4086. this._strongEndUndRegExp = /[^\s]__(?!_)(?:(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~*\s])|$)/g;
  4087. this._emStartRegExp = /^(?:(\*(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~]))|\*)(?![*\s])|_/;
  4088. this._emMiddleRegExp = /^\*(?:(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^*]|\\\*)|__[^_]*?__|\*\*\[^\*\]*?\*\*)|\*(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^*]|\\\*)|__[^_]*?__|\*\*\[^\*\]*?\*\*)*?\*)+?\*$|^_(?![_\s])(?:(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^_]|\\_)|__[^_]*?__|\*\*\[^\*\]*?\*\*)|_(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^_]|\\_)|__[^_]*?__|\*\*\[^\*\]*?\*\*)*?_)+?_$/;
  4089. this._emEndAstRegExp = /[^!"#$%&'()+\-.,/:;<=>?@[\]`{|}~\s]\*(?!\*)|[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~]\*(?!\*)(?:(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~_\s]|$))/g;
  4090. this._emEndUndRegExp = /[^\s]_(?!_)(?:(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~*\s])|$)/g;
  4091. this._codespanRegExp = /^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/;
  4092. this._brRegExp = /^( {2,}|\\)\n(?!\s*$)/;
  4093. this._delRegExp = /^~+(?=\S)([\s\S]*?\S)~+/;
  4094. this._textspanRegExp = /^(`+|[^`])(?:(?= {2,}\n)|[\s\S]*?(?:(?=[\\<![`*~]|\b_|https?:\/\/|ftp:\/\/|www\.|$)|[^ ](?= {2,}\n)|[^a-zA-Z0-9.!#$%&'*+/=?_`{|}~-](?=[a-zA-Z0-9.!#$%&'*+/=?_`{|}~-]+@))|(?=[a-zA-Z0-9.!#$%&'*+/=?_`{|}~-]+@))/;
  4095. this._punctuationRegExp = /^([\s*!"#$%&'()+\-.,/:;<=>?@[\]`{|}~])/;
  4096. this._blockSkipRegExp = /\[[^\]]*?\]\([^)]*?\)|`[^`]*?`|<[^>]*?>/g;
  4097. this._escapeTestRegExp = /[&<>"']/;
  4098. this._escapeReplaceRegExp = /[&<>"']/g;
  4099. this._escapeTestNoEncodeRegExp = /[<>"']|&(?!#?\w+;)/;
  4100. this._escapeReplaceNoEncodeRegExp = /[<>"']|&(?!#?\w+;)/g;
  4101. this._escapeReplacementsMap = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' };
  4102. this._cache = new Map();
  4103. }
  4104. html(source) {
  4105. if (this._cache.has(source)) {
  4106. return this._cache.get(source);
  4107. }
  4108. const tokens = [];
  4109. const links = new Map();
  4110. source = source.replace(/\r\n|\r/g, '\n').replace(/\t/g, ' ');
  4111. this._tokenize(source, tokens, links, true);
  4112. this._tokenizeBlock(tokens, links);
  4113. const target = this._render(tokens, true);
  4114. if (this._cache.size > 256) {
  4115. this._cache.delete(this._cache.keys().next().value);
  4116. }
  4117. this._cache.set(source, target);
  4118. return target;
  4119. }
  4120. _tokenize(source, tokens, links, top) {
  4121. source = source.replace(/^ +$/gm, '');
  4122. while (source) {
  4123. let match = this._newlineRegExp.exec(source);
  4124. if (match) {
  4125. source = source.substring(match[0].length);
  4126. if (match[0].length > 1) {
  4127. tokens.push({ type: 'space' });
  4128. }
  4129. continue;
  4130. }
  4131. match = this._codeRegExp.exec(source);
  4132. if (match) {
  4133. source = source.substring(match[0].length);
  4134. const lastToken = tokens[tokens.length - 1];
  4135. if (lastToken && lastToken.type === 'paragraph') {
  4136. lastToken.text += `\n${match[0].trimRight()}`;
  4137. } else {
  4138. const text = match[0].replace(/^ {4}/gm, '').replace(/\n*$/, '');
  4139. tokens.push({ type: 'code', text });
  4140. }
  4141. continue;
  4142. }
  4143. match = this._fencesRegExp.exec(source);
  4144. if (match) {
  4145. source = source.substring(match[0].length);
  4146. const language = match[2] ? match[2].trim() : match[2];
  4147. let content = match[3] || '';
  4148. const matchIndent = match[0].match(/^(\s+)(?:```)/);
  4149. if (matchIndent !== null) {
  4150. const [, indent] = matchIndent;
  4151. content = content.split('\n').map((node) => {
  4152. const match = node.match(/^\s+/);
  4153. return (match !== null && match[0].length >= indent.length) ? node.slice(indent.length) : node;
  4154. }).join('\n');
  4155. }
  4156. tokens.push({ type: 'code', language, text: content });
  4157. continue;
  4158. }
  4159. match = this._headingRegExp.exec(source);
  4160. if (match) {
  4161. source = source.substring(match[0].length);
  4162. tokens.push({ type: 'heading', depth: match[1].length, text: match[2] });
  4163. continue;
  4164. }
  4165. match = this._nptableRegExp.exec(source);
  4166. if (match) {
  4167. const header = this._splitCells(match[1].replace(/^ *| *\| *$/g, ''));
  4168. const align = match[2].replace(/^ *|\| *$/g, '').split(/ *\| */);
  4169. if (header.length === align.length) {
  4170. const cells = match[3] ? match[3].replace(/\n$/, '').split('\n') : [];
  4171. const token = { type: 'table', header, align, cells, raw: match[0] };
  4172. for (let i = 0; i < token.align.length; i++) {
  4173. if (/^ *-+: *$/.test(token.align[i])) {
  4174. token.align[i] = 'right';
  4175. } else if (/^ *:-+: *$/.test(token.align[i])) {
  4176. token.align[i] = 'center';
  4177. } else if (/^ *:-+ *$/.test(token.align[i])) {
  4178. token.align[i] = 'left';
  4179. } else {
  4180. token.align[i] = null;
  4181. }
  4182. }
  4183. token.cells = token.cells.map((cell) => this._splitCells(cell, token.header.length));
  4184. source = source.substring(token.raw.length);
  4185. tokens.push(token);
  4186. continue;
  4187. }
  4188. }
  4189. match = this._hrRegExp.exec(source);
  4190. if (match) {
  4191. source = source.substring(match[0].length);
  4192. tokens.push({ type: 'hr' });
  4193. continue;
  4194. }
  4195. match = this._blockquoteRegExp.exec(source);
  4196. if (match) {
  4197. source = source.substring(match[0].length);
  4198. const text = match[0].replace(/^ *> ?/gm, '');
  4199. tokens.push({ type: 'blockquote', text, tokens: this._tokenize(text, [], links, top) });
  4200. continue;
  4201. }
  4202. match = this._listRegExp.exec(source);
  4203. if (match) {
  4204. const [value, , bull] = match;
  4205. const ordered = bull.length > 1;
  4206. const parent = bull[bull.length - 1] === ')';
  4207. let raw = value;
  4208. const list = { type: 'list', raw, ordered, start: ordered ? Number(bull.slice(0, -1)) : '', loose: false, items: [] };
  4209. const itemMatch = value.match(this._itemRegExp);
  4210. let next = false;
  4211. const length = itemMatch.length;
  4212. for (let i = 0; i < length; i++) {
  4213. let item = itemMatch[i];
  4214. raw = item;
  4215. let space = item.length;
  4216. item = item.replace(/^ *([*+-]|\d+[.)]) ?/, '');
  4217. if (item.indexOf('\n ') !== -1) {
  4218. space -= item.length;
  4219. item = item.replace(new RegExp(`^ {1,${space}}`, 'gm'), '');
  4220. }
  4221. if (i !== length - 1) {
  4222. const [bullet] = this._bulletRegExp.exec(itemMatch[i + 1]);
  4223. if (ordered ? bullet.length === 1 || (!parent && bullet[bullet.length - 1] === ')') : (bullet.length > 1)) {
  4224. const addBack = itemMatch.slice(i + 1).join('\n');
  4225. list.raw = list.raw.substring(0, list.raw.length - addBack.length);
  4226. i = length - 1;
  4227. }
  4228. }
  4229. let loose = next || /\n\n(?!\s*$)/.test(item);
  4230. if (i !== length - 1) {
  4231. next = item.charAt(item.length - 1) === '\n';
  4232. if (!loose) {
  4233. loose = next;
  4234. }
  4235. }
  4236. if (loose) {
  4237. list.loose = true;
  4238. }
  4239. const task = /^\[[ xX]\] /.test(item);
  4240. let checked = false;
  4241. if (task) {
  4242. checked = item[1] !== ' ';
  4243. item = item.replace(/^\[[ xX]\] +/, '');
  4244. }
  4245. list.items.push({ type: 'list_item', raw, task, checked, loose, text: item });
  4246. }
  4247. source = source.substring(list.raw.length);
  4248. for (const item of list.items) {
  4249. item.tokens = this._tokenize(item.text, [], links, false);
  4250. }
  4251. tokens.push(list);
  4252. continue;
  4253. }
  4254. match = this._htmlRegExp.exec(source);
  4255. if (match) {
  4256. source = source.substring(match[0].length);
  4257. tokens.push({ type: 'html', pre: (match[1] === 'pre' || match[1] === 'script' || match[1] === 'style'), text: match[0] });
  4258. continue;
  4259. }
  4260. if (top) {
  4261. match = this._defRegExp.exec(source);
  4262. if (match) {
  4263. source = source.substring(match[0].length);
  4264. match[3] = match[3] ? match[3].substring(1, match[3].length - 1) : match[3];
  4265. const tag = match[1].toLowerCase().replace(/\s+/g, ' ');
  4266. if (!links.has(tag)) {
  4267. links.set(tag, { href: match[2], title: match[3] });
  4268. }
  4269. continue;
  4270. }
  4271. }
  4272. match = this._tableRegExp.exec(source);
  4273. if (match) {
  4274. const header = this._splitCells(match[1].replace(/^ *| *\| *$/g, ''));
  4275. const align = match[2].replace(/^ *|\| *$/g, '').split(/ *\| */);
  4276. if (header.length === align.length) {
  4277. const cells = match[3] ? match[3].replace(/\n$/, '').split('\n') : [];
  4278. const token = { type: 'table', header, align, cells, raw: match[0] };
  4279. for (let i = 0; i < token.align.length; i++) {
  4280. if (/^ *-+: *$/.test(token.align[i])) {
  4281. token.align[i] = 'right';
  4282. } else if (/^ *:-+: *$/.test(token.align[i])) {
  4283. token.align[i] = 'center';
  4284. } else if (/^ *:-+ *$/.test(token.align[i])) {
  4285. token.align[i] = 'left';
  4286. } else {
  4287. token.align[i] = null;
  4288. }
  4289. }
  4290. token.cells = token.cells.map((cell) => this._splitCells(cell.replace(/^ *\| *| *\| *$/g, ''), token.header.length));
  4291. source = source.substring(token.raw.length);
  4292. tokens.push(token);
  4293. continue;
  4294. }
  4295. }
  4296. match = this._lheadingRegExp.exec(source);
  4297. if (match) {
  4298. source = source.substring(match[0].length);
  4299. tokens.push({ type: 'heading', depth: match[2].charAt(0) === '=' ? 1 : 2, text: match[1] });
  4300. continue;
  4301. }
  4302. if (top) {
  4303. match = this._paragraphRegExp.exec(source);
  4304. if (match) {
  4305. source = source.substring(match[0].length);
  4306. tokens.push({ type: 'paragraph', text: match[1].charAt(match[1].length - 1) === '\n' ? match[1].slice(0, -1) : match[1] });
  4307. continue;
  4308. }
  4309. }
  4310. match = this._textRegExp.exec(source);
  4311. if (match) {
  4312. source = source.substring(match[0].length);
  4313. const lastToken = tokens[tokens.length - 1];
  4314. if (lastToken && lastToken.type === 'text') {
  4315. lastToken.text += `\n${match[0]}`;
  4316. } else {
  4317. tokens.push({ type: 'text', text: match[0] });
  4318. }
  4319. continue;
  4320. }
  4321. throw new Error(`Unexpected '${source.charCodeAt(0)}'.`);
  4322. }
  4323. return tokens;
  4324. }
  4325. _tokenizeInline(source, links, inLink, inRawBlock, prevChar) {
  4326. const tokens = [];
  4327. let maskedSource = source;
  4328. if (links.size > 0) {
  4329. while (maskedSource) {
  4330. const match = this._reflinkSearchRegExp.exec(maskedSource);
  4331. if (match) {
  4332. if (links.has(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) {
  4333. maskedSource = `${maskedSource.slice(0, match.index)}[${'a'.repeat(match[0].length - 2)}]${maskedSource.slice(this._reflinkSearchRegExp.lastIndex)}`;
  4334. }
  4335. continue;
  4336. }
  4337. break;
  4338. }
  4339. }
  4340. while (maskedSource) {
  4341. const match = this._blockSkipRegExp.exec(maskedSource);
  4342. if (match) {
  4343. maskedSource = `${maskedSource.slice(0, match.index)}[${'a'.repeat(match[0].length - 2)}]${maskedSource.slice(this._blockSkipRegExp.lastIndex)}`;
  4344. continue;
  4345. }
  4346. break;
  4347. }
  4348. while (source) {
  4349. let match = this._escapeRegExp.exec(source);
  4350. if (match) {
  4351. source = source.substring(match[0].length);
  4352. tokens.push({ type: 'escape', text: this._escape(match[1]) });
  4353. continue;
  4354. }
  4355. match = this._tagRegExp.exec(source);
  4356. if (match) {
  4357. source = source.substring(match[0].length);
  4358. if (!inLink && /^<a /i.test(match[0])) {
  4359. inLink = true;
  4360. } else if (inLink && /^<\/a>/i.test(match[0])) {
  4361. inLink = false;
  4362. }
  4363. if (!inRawBlock && /^<(pre|code|kbd|script)(\s|>)/i.test(match[0])) {
  4364. inRawBlock = true;
  4365. } else if (inRawBlock && /^<\/(pre|code|kbd|script)(\s|>)/i.test(match[0])) {
  4366. inRawBlock = false;
  4367. }
  4368. tokens.push({ type: 'html', raw: match[0], text: match[0] });
  4369. continue;
  4370. }
  4371. match = this._linkRegExp.exec(source);
  4372. if (match) {
  4373. let index = -1;
  4374. const [, , ref] = match;
  4375. if (ref.indexOf(')') !== -1) {
  4376. let level = 0;
  4377. for (let i = 0; i < ref.length; i++) {
  4378. switch (ref[i]) {
  4379. case '\\':
  4380. i++;
  4381. break;
  4382. case '(':
  4383. level++;
  4384. break;
  4385. case ')':
  4386. level--;
  4387. if (level < 0) {
  4388. index = i;
  4389. i = ref.length;
  4390. }
  4391. break;
  4392. default:
  4393. break;
  4394. }
  4395. }
  4396. }
  4397. if (index > -1) {
  4398. const length = (match[0].indexOf('!') === 0 ? 5 : 4) + match[1].length + index;
  4399. match[2] = match[2].substring(0, index);
  4400. match[0] = match[0].substring(0, length).trim();
  4401. match[3] = '';
  4402. }
  4403. const title = (match[3] ? match[3].slice(1, -1) : '').replace(this._escapesRegExp, '$1');
  4404. const href = match[2].trim().replace(/^<([\s\S]*)>$/, '$1').replace(this._escapesRegExp, '$1');
  4405. const token = this._outputLink(match, href, title);
  4406. source = source.substring(match[0].length);
  4407. if (token.type === 'link') {
  4408. token.tokens = this._tokenizeInline(token.text, links, true, inRawBlock, '');
  4409. }
  4410. tokens.push(token);
  4411. continue;
  4412. }
  4413. match = this._reflinkRegExp.exec(source) || this._nolinkRegExp.exec(source);
  4414. if (match) {
  4415. let link = (match[2] || match[1]).replace(/\s+/g, ' ');
  4416. link = links.get(link.toLowerCase());
  4417. if (!link || !link.href) {
  4418. const text = match[0].charAt(0);
  4419. source = source.substring(text.length);
  4420. tokens.push({ type: 'text', text });
  4421. } else {
  4422. source = source.substring(match[0].length);
  4423. const token = this._outputLink(match, link);
  4424. if (token.type === 'link') {
  4425. token.tokens = this._tokenizeInline(token.text, links, true, inRawBlock, '');
  4426. }
  4427. tokens.push(token);
  4428. }
  4429. continue;
  4430. }
  4431. match = this._strongStartRegExp.exec(source);
  4432. if (match && (!match[1] || (match[1] && (prevChar === '' || this._punctuationRegExp.exec(prevChar))))) {
  4433. const masked = maskedSource.slice(-1 * source.length);
  4434. const endReg = match[0] === '**' ? this._strongEndAstRegExp : this._strongEndUndRegExp;
  4435. endReg.lastIndex = 0;
  4436. let cap = '';
  4437. while ((match = endReg.exec(masked)) !== null) {
  4438. cap = this._strongMiddleRegExp.exec(masked.slice(0, match.index + 3));
  4439. if (cap) {
  4440. break;
  4441. }
  4442. }
  4443. if (cap) {
  4444. const text = source.substring(2, cap[0].length - 2);
  4445. source = source.substring(cap[0].length);
  4446. tokens.push({ type: 'strong', text, tokens: this._tokenizeInline(text, links, inLink, inRawBlock, '') });
  4447. continue;
  4448. }
  4449. }
  4450. match = this._emStartRegExp.exec(source);
  4451. if (match && (!match[1] || (match[1] && (prevChar === '' || this._punctuationRegExp.exec(prevChar))))) {
  4452. const masked = maskedSource.slice(-1 * source.length);
  4453. const endReg = match[0] === '*' ? this._emEndAstRegExp : this._emEndUndRegExp;
  4454. endReg.lastIndex = 0;
  4455. let cap = '';
  4456. while ((match = endReg.exec(masked)) !== null) {
  4457. cap = this._emMiddleRegExp.exec(masked.slice(0, match.index + 2));
  4458. if (cap) {
  4459. break;
  4460. }
  4461. }
  4462. if (cap) {
  4463. const text = source.slice(1, cap[0].length - 1);
  4464. source = source.substring(cap[0].length);
  4465. tokens.push({ type: 'em', text, tokens: this._tokenizeInline(text, links, inLink, inRawBlock, '') });
  4466. continue;
  4467. }
  4468. }
  4469. match = this._codespanRegExp.exec(source);
  4470. if (match) {
  4471. source = source.substring(match[0].length);
  4472. let content = match[2].replace(/\n/g, ' ');
  4473. if (/[^ ]/.test(content) && content.startsWith(' ') && content.endsWith(' ')) {
  4474. content = content.substring(1, content.length - 1);
  4475. }
  4476. tokens.push({ type: 'codespan', text: this._encode(content) });
  4477. continue;
  4478. }
  4479. match = this._brRegExp.exec(source);
  4480. if (match) {
  4481. source = source.substring(match[0].length);
  4482. tokens.push({ type: 'br' });
  4483. continue;
  4484. }
  4485. match = this._delRegExp.exec(source);
  4486. if (match) {
  4487. const [value, text] = match;
  4488. source = source.substring(value.length);
  4489. tokens.push({ type: 'del', text, tokens: this._tokenizeInline(text, links, inLink, inRawBlock, '') });
  4490. continue;
  4491. }
  4492. match = this._autolinkRegExp.exec(source);
  4493. if (match) {
  4494. source = source.substring(match[0].length);
  4495. const text = this._escape(match[1]);
  4496. const href = match[2] === '@' ? `mailto:${text}` : text;
  4497. tokens.push({ type: 'link', text, href, tokens: [{ type: 'text', raw: text, text }] });
  4498. continue;
  4499. }
  4500. if (!inLink) {
  4501. match = this._urlRegExp.exec(source);
  4502. if (match) {
  4503. const email = match[2] === '@';
  4504. let [value] = match;
  4505. if (!email) {
  4506. let prevCapZero = '';
  4507. do {
  4508. prevCapZero = value;
  4509. [value] = this._backpedalRegExp.exec(value);
  4510. } while (prevCapZero !== value);
  4511. }
  4512. const text = this._escape(value);
  4513. let href = text;
  4514. if (email) {
  4515. href = `mailto:${text}`;
  4516. } else if (text.startsWith('www.')) {
  4517. href = `http://${text}`;
  4518. }
  4519. source = source.substring(value.length);
  4520. tokens.push({ type: 'link', text, href, tokens: [{ type: 'text', text }] });
  4521. continue;
  4522. }
  4523. }
  4524. match = this._textspanRegExp.exec(source);
  4525. if (match) {
  4526. source = source.substring(match[0].length);
  4527. prevChar = match[0].slice(-1);
  4528. tokens.push({ type: 'text' , text: inRawBlock ? match[0] : this._escape(match[0]) });
  4529. continue;
  4530. }
  4531. throw new Error(`Unexpected '${source.charCodeAt(0)}'.`);
  4532. }
  4533. return tokens;
  4534. }
  4535. _tokenizeBlock(tokens, links) {
  4536. for (const token of tokens) {
  4537. switch (token.type) {
  4538. case 'paragraph':
  4539. case 'text':
  4540. case 'heading': {
  4541. token.tokens = this._tokenizeInline(token.text, links, false, false, '');
  4542. break;
  4543. }
  4544. case 'table': {
  4545. token.tokens = {};
  4546. token.tokens.header = token.header.map((header) => this._tokenizeInline(header, links, false, false, ''));
  4547. token.tokens.cells = token.cells.map((cell) => cell.map((row) => this._tokenizeInline(row, links, false, false, '')));
  4548. break;
  4549. }
  4550. case 'blockquote': {
  4551. this._tokenizeBlock(token.tokens, links);
  4552. break;
  4553. }
  4554. case 'list': {
  4555. for (const item of token.items) {
  4556. this._tokenizeBlock(item.tokens, links);
  4557. }
  4558. break;
  4559. }
  4560. default: {
  4561. break;
  4562. }
  4563. }
  4564. }
  4565. }
  4566. _render(tokens, top) {
  4567. let html = '';
  4568. while (tokens.length > 0) {
  4569. const token = tokens.shift();
  4570. switch (token.type) {
  4571. case 'space': {
  4572. continue;
  4573. }
  4574. case 'hr': {
  4575. html += '<hr>\n';
  4576. continue;
  4577. }
  4578. case 'heading': {
  4579. const level = token.depth;
  4580. html += `<h${level}">${this._renderInline(token.tokens)}</h${level}>\n`;
  4581. continue;
  4582. }
  4583. case 'code': {
  4584. const code = token.text;
  4585. const [language] = (token.language || '').match(/\S*/);
  4586. html += `<pre><code${language ? ` class="language-${this._encode(language)}"` : ''}>${token.escaped ? code : this._encode(code)}</code></pre>\n`;
  4587. continue;
  4588. }
  4589. case 'table': {
  4590. let header = '';
  4591. let cell = '';
  4592. for (let j = 0; j < token.header.length; j++) {
  4593. const content = this._renderInline(token.tokens.header[j]);
  4594. const align = token.align[j];
  4595. cell += `<th${align ? ` align="${align}"` : ''}>${content}</th>\n`;
  4596. }
  4597. header += `<tr>\n${cell}</tr>\n`;
  4598. let body = '';
  4599. for (let j = 0; j < token.cells.length; j++) {
  4600. const row = token.tokens.cells[j];
  4601. cell = '';
  4602. for (let k = 0; k < row.length; k++) {
  4603. const content = this._renderInline(row[k]);
  4604. const align = token.align[k];
  4605. cell += `<td${align ? ` align="${align}"` : ''}>${content}</td>\n`;
  4606. }
  4607. body += `<tr>\n${cell}</tr>\n`;
  4608. }
  4609. html += `<table>\n<thead>\n${header}</thead>\n${body ? `<tbody>${body}</tbody>` : body}</table>\n`;
  4610. continue;
  4611. }
  4612. case 'blockquote': {
  4613. html += `<blockquote>\n${this._render(token.tokens, true)}</blockquote>\n`;
  4614. continue;
  4615. }
  4616. case 'list': {
  4617. const ordered = token.ordered;
  4618. const start = token.start;
  4619. const loose = token.loose;
  4620. let body = '';
  4621. for (const item of token.items) {
  4622. let itemBody = '';
  4623. if (item.task) {
  4624. const checkbox = `<input ${item.checked ? 'checked="" ' : ''}disabled="" type="checkbox"> `;
  4625. if (loose) {
  4626. if (item.tokens.length > 0 && item.tokens[0].type === 'text') {
  4627. item.tokens[0].text = `${checkbox} ${item.tokens[0].text}`;
  4628. if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') {
  4629. item.tokens[0].tokens[0].text = `${checkbox} ${item.tokens[0].tokens[0].text}`;
  4630. }
  4631. } else {
  4632. item.tokens.unshift({ type: 'text', text: checkbox });
  4633. }
  4634. } else {
  4635. itemBody += checkbox;
  4636. }
  4637. }
  4638. itemBody += this._render(item.tokens, loose);
  4639. body += `<li>${itemBody}</li>\n`;
  4640. }
  4641. const type = (ordered ? 'ol' : 'ul');
  4642. html += `<${type}${ordered && start !== 1 ? (` start="${start}"`) : ''}>\n${body}</${type}>\n`;
  4643. continue;
  4644. }
  4645. case 'html': {
  4646. html += token.text;
  4647. continue;
  4648. }
  4649. case 'paragraph': {
  4650. html += `<p>${this._renderInline(token.tokens)}</p>\n`;
  4651. continue;
  4652. }
  4653. case 'text': {
  4654. html += top ? '<p>' : '';
  4655. html += token.tokens ? this._renderInline(token.tokens) : token.text;
  4656. while (tokens.length > 0 && tokens[0].type === 'text') {
  4657. const token = tokens.shift();
  4658. html += `\n${token.tokens ? this._renderInline(token.tokens) : token.text}`;
  4659. }
  4660. html += top ? '</p>\n' : '';
  4661. continue;
  4662. }
  4663. default: {
  4664. throw new Error(`Unexpected token type '${token.type}'.`);
  4665. }
  4666. }
  4667. }
  4668. return html;
  4669. }
  4670. _renderInline(tokens) {
  4671. let html = '';
  4672. for (const token of tokens) {
  4673. switch (token.type) {
  4674. case 'escape':
  4675. case 'html':
  4676. case 'text': {
  4677. html += token.text;
  4678. break;
  4679. }
  4680. case 'link': {
  4681. const text = this._renderInline(token.tokens);
  4682. html += `<a href="${token.href}"${token.title ? ` title="${token.title}"` : ''} target="_blank">${text}</a>`;
  4683. break;
  4684. }
  4685. case 'image': {
  4686. html += `<img src="${token.href}" alt="${token.text}"${token.title ? ` title="${token.title}"` : ''}>`;
  4687. break;
  4688. }
  4689. case 'strong': {
  4690. const text = this._renderInline(token.tokens);
  4691. html += `<strong>${text}</strong>`;
  4692. break;
  4693. }
  4694. case 'em': {
  4695. const text = this._renderInline(token.tokens);
  4696. html += `<em>${text}</em>`;
  4697. break;
  4698. }
  4699. case 'codespan': {
  4700. html += `<code>${token.text}</code>`;
  4701. break;
  4702. }
  4703. case 'br': {
  4704. html += '<br>';
  4705. break;
  4706. }
  4707. case 'del': {
  4708. const text = this._renderInline(token.tokens);
  4709. html += `<del>${text}</del>`;
  4710. break;
  4711. }
  4712. default: {
  4713. throw new Error(`Unexpected token type '${token.type}'.`);
  4714. }
  4715. }
  4716. }
  4717. return html;
  4718. }
  4719. _outputLink(match, href, title) {
  4720. title = title ? this._escape(title) : null;
  4721. const text = match[1].replace(/\\([[\]])/g, '$1');
  4722. return match[0].charAt(0) === '!' ?
  4723. { type: 'image', href, title, text: this._escape(text) } :
  4724. { type: 'link', href, title, text };
  4725. }
  4726. _splitCells(tableRow, count) {
  4727. const row = tableRow.replace(/\|/g, (match, offset, str) => {
  4728. let escaped = false;
  4729. let position = offset;
  4730. while (--position >= 0 && str[position] === '\\') {
  4731. escaped = !escaped;
  4732. }
  4733. return escaped ? '|' : ' |';
  4734. });
  4735. const cells = row.split(/ \|/);
  4736. if (cells.length > count) {
  4737. cells.splice(count);
  4738. } else {
  4739. while (cells.length < count) {
  4740. cells.push('');
  4741. }
  4742. }
  4743. return cells.map((cell) => cell.trim().replace(/\\\|/g, '|'));
  4744. }
  4745. _encode(content) {
  4746. if (this._escapeTestRegExp.test(content)) {
  4747. return content.replace(this._escapeReplaceRegExp, (ch) => this._escapeReplacementsMap[ch]);
  4748. }
  4749. return content;
  4750. }
  4751. _escape(content) {
  4752. if (this._escapeTestNoEncodeRegExp.test(content)) {
  4753. return content.replace(this._escapeReplaceNoEncodeRegExp, (ch) => this._escapeReplacementsMap[ch]);
  4754. }
  4755. return content;
  4756. }
  4757. };
  4758. metrics.Argument = class {
  4759. constructor(name, value, type) {
  4760. this.name = name;
  4761. this.value = value;
  4762. this.type = type;
  4763. }
  4764. };
  4765. metrics.Tensor = class {
  4766. constructor(tensor) {
  4767. this._tensor = tensor;
  4768. this._metrics = null;
  4769. }
  4770. get metrics() {
  4771. if (this._metrics === null) {
  4772. this._metrics = [];
  4773. this._metrics = Array.from(this._tensor.metrics || []);
  4774. const keys = new Set(this._metrics.map((metrics) => metrics.name));
  4775. const type = this._tensor.type;
  4776. const shape = type.shape.dimensions;
  4777. const size = shape.reduce((a, b) => a * b, 1);
  4778. if (type.dataType.startsWith('float') && size < 0x800000 && (!keys.has('sparsity') || !keys.has('min') || !keys.has('max') && !keys.has('mean') || !keys.has('max') || !keys.has('std'))) {
  4779. const data = this._tensor.value;
  4780. let zeros = 0;
  4781. let min = null;
  4782. let max = null;
  4783. let sum = 0;
  4784. let count = 0;
  4785. const stack = [data];
  4786. while (stack.length > 0) {
  4787. const data = stack.pop();
  4788. if (Array.isArray(data)) {
  4789. for (const element of data) {
  4790. stack.push(element);
  4791. }
  4792. } else {
  4793. zeros += data === 0 || data === 0n || data === '';
  4794. min = Math.min(data, min === null ? data : min);
  4795. max = Math.max(data, max === null ? data : max);
  4796. sum += data;
  4797. count += 1;
  4798. }
  4799. }
  4800. const mean = sum / count;
  4801. if (!keys.has('sparsity')) {
  4802. this._metrics.push(new metrics.Argument('min', min, type.dataType));
  4803. }
  4804. if (!keys.has('max')) {
  4805. this._metrics.push(new metrics.Argument('max', max, type.dataType));
  4806. }
  4807. if (!keys.has('mean')) {
  4808. this._metrics.push(new metrics.Argument('mean', mean, type.dataType));
  4809. }
  4810. if (!keys.has('std')) {
  4811. let variance = 0;
  4812. const stack = [data];
  4813. while (stack.length > 0) {
  4814. const data = stack.pop();
  4815. if (Array.isArray(data)) {
  4816. for (const element of data) {
  4817. stack.push(element);
  4818. }
  4819. } else {
  4820. variance += Math.pow(data - mean, 2);
  4821. }
  4822. }
  4823. this._metrics.push(new metrics.Argument('std', Math.sqrt(variance / count)));
  4824. }
  4825. if (!keys.has('sparsity')) {
  4826. this._metrics.push(new metrics.Argument('sparsity', count > 0 ? zeros / count : 0, 'percentage'));
  4827. }
  4828. }
  4829. }
  4830. return this._metrics;
  4831. }
  4832. };
  4833. view.Context = class {
  4834. constructor(context, identifier, stream) {
  4835. this._context = context;
  4836. this._tags = new Map();
  4837. this._content = new Map();
  4838. this._stream = stream || context.stream;
  4839. identifier = typeof identifier === 'string' ? identifier : context.identifier;
  4840. const index = Math.max(identifier.lastIndexOf('/'), identifier.lastIndexOf('\\'));
  4841. this._base = index === -1 ? undefined : identifier.substring(0, index);
  4842. this._identifier = index === -1 ? identifier : identifier.substring(index + 1);
  4843. }
  4844. get identifier() {
  4845. return this._identifier;
  4846. }
  4847. get stream() {
  4848. return this._stream;
  4849. }
  4850. async request(file) {
  4851. return this._context.request(file, 'utf-8', null);
  4852. }
  4853. async fetch(file) {
  4854. const stream = await this._context.request(file, null, this._base);
  4855. return new view.Context(this, file, stream, new Map());
  4856. }
  4857. async require(id) {
  4858. return this._context.require(id);
  4859. }
  4860. error(error, fatal) {
  4861. if (error && this.identifier) {
  4862. error.context = this.identifier;
  4863. }
  4864. this._context.error(error, fatal);
  4865. }
  4866. peek(type) {
  4867. if (!this._content.has(type)) {
  4868. this._content.set(type, undefined);
  4869. const stream = this.stream;
  4870. if (stream) {
  4871. const position = stream.position;
  4872. const match = (buffer, signature) => {
  4873. return signature.length <= buffer.length && buffer.every((value, index) => signature[index] === undefined || signature[index] === value);
  4874. };
  4875. const buffer = stream.peek(Math.min(stream.length, 16));
  4876. const skip =
  4877. match(buffer, [0x80, undefined, 0x8a, 0x0a, 0x6c, 0xfc, 0x9c, 0x46, 0xf9, 0x20, 0x6a, 0xa8, 0x50, 0x19]) || // PyTorch
  4878. (type !== 'npz' && type !== 'zip' && match(buffer, [0x50, 0x4B, 0x03, 0x04])) || // Zip
  4879. (type !== 'hdf5' && match(buffer, [0x89, 0x48, 0x44, 0x46, 0x0D, 0x0A, 0x1A, 0x0A])) || // \x89HDF\r\n\x1A\n
  4880. Array.from(this._tags).some(([key, value]) => key !== 'flatbuffers' && value.size > 0) ||
  4881. Array.from(this._content.values()).some((obj) => obj !== undefined);
  4882. if (!skip) {
  4883. switch (type) {
  4884. case 'json': {
  4885. try {
  4886. const buffer = stream.peek(Math.min(this.stream.length, 0x1000));
  4887. if (stream.length < 0x7ffff000 &&
  4888. (buffer.length < 8 || String.fromCharCode.apply(null, buffer.slice(0, 8)) !== '\x89HDF\r\n\x1A\n') &&
  4889. (buffer.some((v) => v === 0x22 || v === 0x5b || v === 0x5d || v === 0x7b || v === 0x7d))) {
  4890. const reader = json.TextReader.open(stream);
  4891. if (reader) {
  4892. const obj = reader.read();
  4893. this._content.set(type, obj);
  4894. }
  4895. }
  4896. } catch {
  4897. // continue regardless of error
  4898. }
  4899. break;
  4900. }
  4901. case 'json.gz': {
  4902. try {
  4903. const entries = this.peek('gzip');
  4904. if (entries && entries.size === 1) {
  4905. const stream = entries.values().next().value;
  4906. const reader = json.TextReader.open(stream);
  4907. if (reader) {
  4908. const obj = reader.read();
  4909. this._content.set(type, obj);
  4910. }
  4911. }
  4912. } catch {
  4913. // continue regardless of error
  4914. }
  4915. break;
  4916. }
  4917. case 'pkl': {
  4918. let unpickler = null;
  4919. const types = new Set();
  4920. try {
  4921. const archive = zip.Archive.open(stream, 'zlib');
  4922. const data = archive ? archive.entries.get('') : stream;
  4923. let condition = false;
  4924. if (data.length > 2) {
  4925. const head = data.peek(2);
  4926. condition = head[0] === 0x80 && head[1] < 7;
  4927. if (!condition) {
  4928. data.seek(-1);
  4929. const tail = data.peek(1);
  4930. data.seek(0);
  4931. condition = tail[0] === 0x2e;
  4932. }
  4933. }
  4934. if (condition) {
  4935. const execution = new python.Execution();
  4936. execution.on('resolve', (_, name) => types.add(name));
  4937. const pickle = execution.__import__('pickle');
  4938. unpickler = new pickle.Unpickler(data);
  4939. }
  4940. } catch {
  4941. // continue regardless of error
  4942. }
  4943. if (unpickler) {
  4944. const storages = new Map();
  4945. unpickler.persistent_load = (saved_id) => {
  4946. if (Array.isArray(saved_id) && saved_id.length > 3) {
  4947. switch (saved_id[0]) {
  4948. case 'storage': {
  4949. const [, storage_type, key, , size] = saved_id;
  4950. if (!storages.has(key)) {
  4951. const storage = new storage_type(size);
  4952. storages.set(key, storage);
  4953. }
  4954. return storages.get(key);
  4955. }
  4956. default: {
  4957. throw new python.Error(`Unsupported persistent load type '${saved_id[0]}'.`);
  4958. }
  4959. }
  4960. }
  4961. throw new view.Error("Unsupported 'persistent_load'.");
  4962. };
  4963. try {
  4964. const obj = unpickler.load();
  4965. this._content.set(type, obj);
  4966. } catch (error) {
  4967. this._content.set(type, error);
  4968. }
  4969. if (Array.from(types).every((name) => !name.startsWith('__torch__.'))) {
  4970. for (const name of types) {
  4971. this.error(new view.Error(`Unknown type name '${name}'.`));
  4972. }
  4973. } else {
  4974. this._content.set(type, new view.Error("PyTorch standalone 'data.pkl' format not supported."));
  4975. }
  4976. }
  4977. break;
  4978. }
  4979. case 'hdf5': {
  4980. const file = hdf5.File.open(stream);
  4981. if (file) {
  4982. try {
  4983. this._content.set(type, file.read());
  4984. } catch (error) {
  4985. this._content.set(type, error);
  4986. }
  4987. }
  4988. break;
  4989. }
  4990. case 'zip':
  4991. case 'tar':
  4992. case 'gzip': {
  4993. this._content.set('zip', undefined);
  4994. this._content.set('tar', undefined);
  4995. this._content.set('gzip', undefined);
  4996. let stream = this._stream;
  4997. try {
  4998. const archive = zip.Archive.open(this._stream, 'gzip');
  4999. if (archive) {
  5000. let entries = archive.entries;
  5001. if (entries.size === 1) {
  5002. const key = entries.keys().next().value;
  5003. stream = entries.values().next().value;
  5004. const name = key === '' ? this.identifier.replace(/\.gz$/, '') : key;
  5005. entries = new Map([[name, stream]]);
  5006. }
  5007. this._content.set('gzip', entries);
  5008. }
  5009. } catch (error) {
  5010. this._content.set('gzip', error);
  5011. }
  5012. let skipTar = false;
  5013. try {
  5014. const archive = zip.Archive.open(stream, 'zip');
  5015. if (archive) {
  5016. this._content.set('zip', archive.entries);
  5017. skipTar = true;
  5018. }
  5019. } catch (error) {
  5020. this._content.set('zip', error);
  5021. }
  5022. if (!skipTar) {
  5023. try {
  5024. const archive = tar.Archive.open(stream);
  5025. if (archive) {
  5026. this._content.set('tar', archive.entries);
  5027. }
  5028. } catch (error) {
  5029. this._content.set('tar', error);
  5030. }
  5031. }
  5032. break;
  5033. }
  5034. case 'flatbuffers.binary': {
  5035. try {
  5036. const reader = flatbuffers.BinaryReader.open(this._stream);
  5037. if (reader) {
  5038. this._content.set('flatbuffers.binary', reader);
  5039. }
  5040. } catch (error) {
  5041. this._content.set('flatbuffers.binary', error);
  5042. }
  5043. break;
  5044. }
  5045. case 'npz': {
  5046. try {
  5047. const content = new Map();
  5048. const entries = this.peek('zip');
  5049. if (entries instanceof Map && entries.size > 0 &&
  5050. Array.from(entries.keys()).every((name) => name.endsWith('.npy'))) {
  5051. const execution = new python.Execution();
  5052. for (const [name, stream] of entries) {
  5053. const buffer = stream.peek();
  5054. const bytes = execution.invoke('io.BytesIO', [buffer]);
  5055. const array = execution.invoke('numpy.load', [bytes]);
  5056. content.set(name, array);
  5057. }
  5058. this._content.set(type, content);
  5059. }
  5060. } catch {
  5061. // continue regardless of error
  5062. }
  5063. break;
  5064. }
  5065. default: {
  5066. throw new view.Error(`Unsupported open format type '${type}'.`);
  5067. }
  5068. }
  5069. }
  5070. if (stream.position !== position) {
  5071. stream.seek(0);
  5072. }
  5073. }
  5074. }
  5075. return this._content.get(type);
  5076. }
  5077. read(type, ...args) {
  5078. if (!this._content.has(type)) {
  5079. switch (type) {
  5080. case 'json': {
  5081. const reader = json.TextReader.open(this._stream);
  5082. if (reader) {
  5083. const obj = reader.read();
  5084. this._content.set('json', obj);
  5085. return obj;
  5086. }
  5087. throw new view.Error('Invalid JSON content.');
  5088. }
  5089. case 'bson': {
  5090. const reader = json.BinaryReader.open(this._stream);
  5091. if (reader) {
  5092. return reader.read();
  5093. }
  5094. throw new view.Error('Invalid BSON content.');
  5095. }
  5096. case 'flatbuffers.binary': {
  5097. const reader = flatbuffers.BinaryReader.open(this._stream);
  5098. if (reader) {
  5099. this._content.set('flatbuffers.reader', reader);
  5100. return reader;
  5101. }
  5102. throw new view.Error('Invalid FlatBuffers content.');
  5103. }
  5104. case 'flatbuffers.text': {
  5105. const obj = this.peek('json');
  5106. return flatbuffers.TextReader.open(obj);
  5107. }
  5108. case 'protobuf.binary': {
  5109. return protobuf.BinaryReader.open(this._stream);
  5110. }
  5111. case 'protobuf.text': {
  5112. return protobuf.TextReader.open(this._stream);
  5113. }
  5114. case 'binary.big-endian': {
  5115. return base.BinaryReader.open(this._stream, false);
  5116. }
  5117. case 'binary': {
  5118. return base.BinaryReader.open(this._stream);
  5119. }
  5120. case 'text': {
  5121. if (typeof args[0] === 'number') {
  5122. const length = Math.min(this._stream.length, args[0]);
  5123. const buffer = this._stream.peek(length);
  5124. text.Reader.open(buffer);
  5125. }
  5126. return text.Reader.open(this._stream);
  5127. }
  5128. case 'text.decoder': {
  5129. return text.Decoder.open(this._stream);
  5130. }
  5131. default: {
  5132. break;
  5133. }
  5134. }
  5135. }
  5136. return this.peek(type);
  5137. }
  5138. tags(type) {
  5139. if (!this._tags.has(type)) {
  5140. let tags = new Map();
  5141. const stream = this.stream;
  5142. if (stream) {
  5143. const position = stream.position;
  5144. const signatures = [
  5145. [0x89, 0x48, 0x44, 0x46, 0x0D, 0x0A, 0x1A, 0x0A], // HDF5
  5146. [0x80, undefined, 0x8a, 0x0a, 0x6c, 0xfc, 0x9c, 0x46, 0xf9, 0x20, 0x6a, 0xa8, 0x50, 0x19], // PyTorch
  5147. [0x50, 0x4b], // Zip
  5148. [0x1f, 0x8b] // Gzip
  5149. ];
  5150. const skip =
  5151. signatures.some((signature) => signature.length <= stream.length && stream.peek(signature.length).every((value, index) => signature[index] === undefined || signature[index] === value)) ||
  5152. (Array.from(this._tags).some(([key, value]) => key !== 'flatbuffers' && value.size > 0) && type !== 'pb+') ||
  5153. Array.from(this._content.values()).some((obj) => obj !== undefined) ||
  5154. (stream.length < 0x7ffff000 && json.TextReader.open(stream));
  5155. if (!skip && stream.length < 0x7ffff000) {
  5156. try {
  5157. switch (type) {
  5158. case 'pbtxt': {
  5159. const reader = protobuf.TextReader.open(stream);
  5160. tags = reader ? reader.signature() : tags;
  5161. break;
  5162. }
  5163. case 'pb': {
  5164. const reader = protobuf.BinaryReader.open(stream);
  5165. tags = reader.signature();
  5166. break;
  5167. }
  5168. case 'pb+': {
  5169. const reader = protobuf.BinaryReader.open(stream);
  5170. tags = reader.decode();
  5171. break;
  5172. }
  5173. case 'xml': {
  5174. const reader = xml.TextReader.open(stream);
  5175. if (reader) {
  5176. const document = reader.peek();
  5177. const element = document.documentElement;
  5178. const namespaceURI = element.namespaceURI;
  5179. const localName = element.localName;
  5180. const name = namespaceURI ? `${namespaceURI}:${localName}` : localName;
  5181. tags.set(name, element);
  5182. }
  5183. break;
  5184. }
  5185. default: {
  5186. throw new view.Error(`Unsupported tags format type '${type}'.`);
  5187. }
  5188. }
  5189. } catch {
  5190. tags.clear();
  5191. }
  5192. }
  5193. if (stream.position !== position) {
  5194. stream.seek(position);
  5195. }
  5196. }
  5197. this._tags.set(type, tags);
  5198. }
  5199. return this._tags.get(type);
  5200. }
  5201. metadata(name) {
  5202. return view.Metadata.open(this, name);
  5203. }
  5204. };
  5205. view.EntryContext = class {
  5206. constructor(host, entries) {
  5207. this._host = host;
  5208. this._entries = entries;
  5209. }
  5210. async request(file, encoding, base) {
  5211. if (base === null) {
  5212. return this._host.request(file, encoding, base);
  5213. }
  5214. let stream = null;
  5215. if (typeof base === 'string') {
  5216. stream = this._entries.get(`${base}/${file}`) || this._entries.get(`${base}\\${file}`);
  5217. } else {
  5218. stream = this._entries.get(file);
  5219. }
  5220. if (!stream) {
  5221. throw new view.Error('File not found.');
  5222. }
  5223. if (encoding) {
  5224. const decoder = new TextDecoder(encoding);
  5225. const buffer = stream.peek();
  5226. const value = decoder.decode(buffer);
  5227. return value;
  5228. }
  5229. return stream;
  5230. }
  5231. async require(id) {
  5232. return this._host.require(id);
  5233. }
  5234. error(error, fatal) {
  5235. this._host.exception(error, fatal);
  5236. }
  5237. };
  5238. view.ArchiveError = class extends Error {
  5239. constructor(message) {
  5240. super(message);
  5241. this.name = 'Error loading archive.';
  5242. }
  5243. };
  5244. view.ModelFactoryService = class {
  5245. constructor(host) {
  5246. this._host = host;
  5247. this._patterns = new Set(['.zip', '.tar', '.tar.gz', '.tgz', '.gz']);
  5248. this._factories = [];
  5249. this.register('./server', ['.netron']);
  5250. this.register('./pytorch', ['.pt', '.pth', '.ptl', '.pt1', '.pyt', '.pyth', '.pkl', '.pickle', '.h5', '.t7', '.model', '.dms', '.tar', '.ckpt', '.chkpt', '.tckpt', '.bin', '.pb', '.zip', '.nn', '.torchmodel', '.torchscript', '.pytorch', '.ot', '.params', '.trt', '.ff', '.ptmf', '.jit', '.pte', '.bin.index.json', 'serialized_exported_program.json', 'model.json'], ['.model', '.pt2']);
  5251. this.register('./onnx', ['.onnx', '.onnx.data', '.onn', '.pb', '.onnxtxt', '.pbtxt', '.prototxt', '.txt', '.model', '.pt', '.pth', '.pkl', '.ort', '.ort.onnx', '.ngf', '.json', '.bin', 'onnxmodel']);
  5252. this.register('./tflite', ['.tflite', '.lite', '.tfl', '.bin', '.pb', '.tmfile', '.h5', '.model', '.json', '.txt', '.dat', '.nb', '.ckpt']);
  5253. this.register('./mxnet', ['.json', '.params'], ['.mar']);
  5254. this.register('./coreml', ['.mlmodel', '.bin', 'manifest.json', 'metadata.json', 'featuredescriptions.json', '.pb', '.pbtxt', '.mil'], ['.mlpackage', '.mlmodelc']);
  5255. this.register('./caffe', ['.caffemodel', '.pbtxt', '.prototxt', '.pt', '.txt']);
  5256. this.register('./caffe2', ['.pb', '.pbtxt', '.prototxt']);
  5257. this.register('./torch', ['.t7', '.net']);
  5258. this.register('./tf', ['.pb', '.meta', '.pbtxt', '.prototxt', '.txt', '.pt', '.json', '.index', '.ckpt', '.graphdef', '.pbmm', /.data-[0-9][0-9][0-9][0-9][0-9]-of-[0-9][0-9][0-9][0-9][0-9]$/, /^events.out.tfevents./], ['.zip']);
  5259. this.register('./tensorrt', ['.trt', '.trtmodel', '.engine', '.model', '.txt', '.uff', '.pb', '.tmfile', '.onnx', '.pth', '.dnn', '.plan', '.pt', '.dat']);
  5260. this.register('./keras', ['.h5', '.hd5', '.hdf5', '.keras', '.json', '.cfg', '.model', '.pb', '.pth', '.weights', '.pkl', '.lite', '.tflite', '.ckpt', '.pb', 'model.weights.npz'], ['.zip']);
  5261. this.register('./numpy', ['.npz', '.npy', '.pkl', '.pickle', '.model', '.model2', '.mge', '.joblib']);
  5262. this.register('./lasagne', ['.pkl', '.pickle', '.joblib', '.model', '.pkl.z', '.joblib.z']);
  5263. this.register('./lightgbm', ['.txt', '.pkl', '.model']);
  5264. this.register('./sklearn', ['.pkl', '.pickle', '.joblib', '.model', '.meta', '.pb', '.pt', '.h5', '.pkl.z', '.joblib.z', '.pickle.dat', '.bin']);
  5265. this.register('./megengine', ['.tm', '.mge', '.pkl']);
  5266. this.register('./pickle', ['.pkl', '.pickle', '.joblib', '.model', '.meta', '.pb', '.pt', '.h5', '.pkl.z', '.joblib.z', '.pdstates', '.mge', '.bin']);
  5267. this.register('./cntk', ['.model', '.cntk', '.cmf', '.dnn']);
  5268. this.register('./uff', ['.uff', '.pb', '.pbtxt', '.uff.txt', '.trt', '.engine']);
  5269. this.register('./paddle', ['.pdmodel', '.pdiparams', '.pdparams', '.pdopt', '.paddle', '__model__', '.__model__', '.pbtxt', '.txt', '.tar', '.tar.gz', '.nb']);
  5270. this.register('./bigdl', ['.model', '.bigdl']);
  5271. this.register('./darknet', ['.cfg', '.model', '.txt', '.weights']);
  5272. this.register('./mediapipe', ['.pbtxt']);
  5273. this.register('./rknn', ['.rknn', '.nb', '.onnx', '.json', '.bin']);
  5274. this.register('./dlc', ['.dlc', 'model', '.params']);
  5275. this.register('./armnn', ['.armnn', '.json']);
  5276. this.register('./mnn', ['.mnn']);
  5277. this.register('./ncnn', ['.param', '.bin', '.cfg.ncnn', '.weights.ncnn', '.ncnnmodel']);
  5278. this.register('./tnn', ['.tnnproto', '.tnnmodel']);
  5279. this.register('./tengine', ['.tmfile']);
  5280. this.register('./mslite', ['.ms', '.bin']);
  5281. this.register('./barracuda', ['.nn']);
  5282. this.register('./circle', ['.circle']);
  5283. this.register('./dnn', ['.dnn']);
  5284. this.register('./xmodel', ['.xmodel']);
  5285. this.register('./kmodel', ['.kmodel']);
  5286. this.register('./flux', ['.bson']);
  5287. this.register('./dl4j', ['.json', '.bin']);
  5288. this.register('./openvino', ['.xml', '.bin']);
  5289. this.register('./mlnet', ['.zip', '.mlnet']);
  5290. this.register('./acuity', ['.json']);
  5291. this.register('./imgdnn', ['.dnn', 'params', '.json']);
  5292. this.register('./flax', ['.msgpack']);
  5293. this.register('./om', ['.om', '.onnx', '.pb', '.engine', '.bin']);
  5294. this.register('./gguf', ['.gguf', /^[^.]+$/]);
  5295. this.register('./nnabla', ['.nntxt'], ['.nnp']);
  5296. this.register('./hickle', ['.h5', '.hkl']);
  5297. this.register('./nnef', ['.nnef', '.dat']);
  5298. this.register('./onednn', ['.json']);
  5299. this.register('./espresso', ['.espresso.net', '.espresso.shape', '.espresso.weights'], ['.mlmodelc']);
  5300. this.register('./mlir', ['.mlir', '.mlir.txt']);
  5301. this.register('./sentencepiece', ['.model']);
  5302. this.register('./hailo', ['.hn', '.har', '.metadata.json']);
  5303. this.register('./nnc', ['.nnc']);
  5304. this.register('./safetensors', ['.safetensors', '.safetensors.index.json']);
  5305. this.register('./modular', ['.maxviz']);
  5306. this.register('./catboost', ['.cbm']);
  5307. this.register('./cambricon', ['.cambricon']);
  5308. this.register('./weka', ['.model']);
  5309. this.register('./qnn', ['.json', '.bin']);
  5310. }
  5311. register(module, factories, containers) {
  5312. for (const pattern of factories) {
  5313. this._factories.push({ pattern, module });
  5314. this._patterns.add(pattern);
  5315. }
  5316. for (const pattern of containers || []) {
  5317. this._patterns.add(pattern);
  5318. }
  5319. }
  5320. async open(context) {
  5321. try {
  5322. await this._openSignature(context);
  5323. const content = new view.Context(context);
  5324. const model = await this._openContext(content);
  5325. if (!model) {
  5326. const check = (obj) => {
  5327. if (obj instanceof Error) {
  5328. throw obj;
  5329. }
  5330. return obj instanceof Map && obj.size > 0;
  5331. };
  5332. let entries = context.entries;
  5333. if (!check(entries)) {
  5334. entries = content.peek('zip');
  5335. if (!check(entries)) {
  5336. entries = content.peek('tar');
  5337. if (!check(entries)) {
  5338. entries = content.peek('gzip');
  5339. }
  5340. }
  5341. }
  5342. if (!check(entries)) {
  5343. this._unsupported(content);
  5344. }
  5345. const entryContext = await this._openEntries(entries);
  5346. if (!entryContext) {
  5347. this._unsupported(content);
  5348. }
  5349. return this._openContext(entryContext);
  5350. }
  5351. return model;
  5352. } catch (error) {
  5353. error.context = !error.context && context && context.identifier ? context.identifier : error.context || '';
  5354. throw error;
  5355. }
  5356. }
  5357. _unsupported(context) {
  5358. const identifier = context.identifier;
  5359. const extension = identifier.split('.').pop().toLowerCase();
  5360. const stream = context.stream;
  5361. const callbacks = [
  5362. (stream) => zip.Archive.open(stream, 'zip'),
  5363. (stream) => tar.Archive.open(stream),
  5364. (stream) => zip.Archive.open(stream, 'gzip')
  5365. ];
  5366. for (const callback of callbacks) {
  5367. let archive = null;
  5368. try {
  5369. archive = callback(stream);
  5370. } catch {
  5371. // continue regardless of error
  5372. }
  5373. if (archive) {
  5374. throw new view.Error("Archive contains no model files.");
  5375. }
  5376. }
  5377. const json = () => {
  5378. const obj = context.peek('json');
  5379. if (obj) {
  5380. const formats = [
  5381. { name: 'Netron metadata', tags: ['[].name', '[].schema'] },
  5382. { name: 'Netron metadata', tags: ['[].name', '[].attributes'] },
  5383. { name: 'Netron metadata', tags: ['[].name', '[].category'] },
  5384. { name: 'Netron test data', tags: ['[].type', '[].target', '[].source', '[].format', '[].link'] },
  5385. { name: 'Netron configuration', tags: ['recents', 'consent'] },
  5386. { name: 'Darkflow metadata', tags: ['net', 'type', 'model'] },
  5387. { name: 'keras-yolo2 configuration', tags: ['model', 'train', 'valid'] },
  5388. { name: 'Vulkan SwiftShader ICD manifest', tags: ['file_format_version', 'ICD'] },
  5389. { name: 'DeepLearningExamples configuration', tags: ['attention_probs_dropout_prob', 'hidden_act', 'hidden_dropout_prob', 'hidden_size',] },
  5390. { name: 'GitHub page data', tags: ['payload', 'title'] },
  5391. { name: 'NuGet assets', tags: ['version', 'targets', 'packageFolders'] },
  5392. { name: 'NuGet data', tags: ['format', 'restore', 'projects'] },
  5393. { name: 'NPM package', tags: ['name', 'version', 'dependencies'] },
  5394. { name: 'NetworkX adjacency_data', tags: ['directed', 'graph', 'nodes'] },
  5395. { name: 'Waifu2x data', tags: ['name', 'arch_name', 'channels'] },
  5396. { name: 'Waifu2x data', tags: ['[].nInputPlane', '[].nOutputPlane', '[].weight', '[].bias'] },
  5397. { name: 'Brain.js data', tags: ['type', 'sizes', 'layers'] },
  5398. { name: 'Custom Vision metadata', tags: ['CustomVision.Metadata.Version'] },
  5399. { name: 'W&B metadata', tags: ['program', 'host', 'executable'] },
  5400. { name: 'TypeScript configuration data', tags: ['compilerOptions'] },
  5401. { name: 'CatBoost model', tags: ['features_info', 'model_info'] },
  5402. { name: 'TPU-MLIR tensor location data', tags: ['file-line', 'subnet_id', 'core_id'] }, // https://github.com/sophgo/tpu-mlir/blob/master/lib/Dialect/Tpu/Transforms/Codegen/TensorLocation.cpp
  5403. { name: 'HTTP Archive data', tags: ['log.version', 'log.creator', 'log.entries'] }, // https://w3c.github.io/web-performance/specs/HAR/Overview.html
  5404. { name: 'Trace Event data', tags: ['traceEvents'] },
  5405. { name: 'Trace Event data', tags: ['[].pid', '[].ph'] },
  5406. { name: 'Diffusers configuration', tags: ['_class_name', '_diffusers_version'] },
  5407. { name: 'Transformers configuration', tags: ['architectures', 'model_type'] }, // https://huggingface.co/docs/transformers/en/create_a_model
  5408. { name: 'Transformers generation configuration', tags: ['transformers_version'] },
  5409. { name: 'Transformers tokenizer configuration', tags: ['tokenizer_class'] },
  5410. { name: 'Transformers tokenizer configuration', tags: ['<|im_start|>'] },
  5411. { name: 'Transformers preprocessor configuration', tags: ['crop_size', 'do_center_crop', 'image_mean', 'image_std', 'do_resize'] },
  5412. { name: 'Tokenizers data', tags: ['version', 'added_tokens', 'model'] }, // https://github.com/huggingface/tokenizers/blob/main/tokenizers/src/tokenizer/serialization.rs
  5413. { name: 'Jupyter Notebook data', tags: ['cells', 'nbformat'] },
  5414. { name: 'Kaggle credentials', tags: ['username','key'] },
  5415. { name: '.NET runtime configuration', tags: ['runtimeOptions.configProperties'] },
  5416. { name: '.NET dependency manifest', tags: ['runtimeTarget', 'targets', 'libraries'] }
  5417. ];
  5418. const match = (obj, tag) => {
  5419. if (tag.startsWith('[].')) {
  5420. tag = tag.substring(3);
  5421. return (Array.isArray(obj) && obj.some((item) => Object.prototype.hasOwnProperty.call(item, tag)));
  5422. }
  5423. tag = tag.split('.');
  5424. while (tag.length > 1) {
  5425. const key = tag.shift();
  5426. obj = obj[key];
  5427. if (!obj) {
  5428. return false;
  5429. }
  5430. }
  5431. return Object.prototype.hasOwnProperty.call(obj, tag[0]);
  5432. };
  5433. for (const format of formats) {
  5434. if (format.tags.every((tag) => match(obj, tag))) {
  5435. throw new view.Error(`Invalid file content. File contains ${format.name}.`);
  5436. }
  5437. }
  5438. const content = `${JSON.stringify(obj).substring(0, 100).replace(/\s/, '').substring(0, 48)}...`;
  5439. throw new view.Error(`Unsupported JSON content '${content.length > 64 ? `${content.substring(0, 100)}...` : content}' for extension '.${extension}'.`);
  5440. }
  5441. };
  5442. const pbtxt = () => {
  5443. const formats = [
  5444. { name: 'ImageNet LabelMap data', tags: ['entry', 'entry.target_class'] },
  5445. { name: 'StringIntLabelMapProto data', tags: ['item', 'item.id', 'item.name'] },
  5446. { name: 'caffe.LabelMap data', tags: ['item', 'item.name', 'item.label'] },
  5447. { name: 'Triton Inference Server configuration', tags: ['name', 'platform', 'input', 'output'] },
  5448. { name: 'TensorFlow OpList data', tags: ['op', 'op.name', 'op.input_arg'] },
  5449. { name: 'vitis.ai.proto.DpuModelParamList data', tags: ['model', 'model.name', 'model.kernel'] },
  5450. { name: 'object_detection.protos.DetectionModel data', tags: ['model', 'model.ssd'] },
  5451. { name: 'object_detection.protos.DetectionModel data', tags: ['model', 'model.faster_rcnn'] },
  5452. { name: 'tensorflow.CheckpointState data', tags: ['model_checkpoint_path', 'all_model_checkpoint_paths'] },
  5453. { name: 'apollo.perception.camera.traffic_light.detection.DetectionParam data', tags: ['min_crop_size', 'crop_method'] },
  5454. { name: 'tidl_meta_arch.TIDLMetaArch data', tags: ['caffe_ssd'] }, // https://github.com/TexasInstruments/edgeai-mmdetection/blob/master/mmdet/utils/proto/mmdet_meta_arch.proto
  5455. { name: 'tidl_meta_arch.TIDLMetaArch data', tags: ['tf_od_api_ssd'] },
  5456. { name: 'tidl_meta_arch.TIDLMetaArch data', tags: ['tidl_ssd'] },
  5457. { name: 'tidl_meta_arch.TIDLMetaArch data', tags: ['tidl_faster_rcnn'] },
  5458. { name: 'tidl_meta_arch.TIDLMetaArch data', tags: ['tidl_yolo'] },
  5459. { name: 'tidl_meta_arch.TIDLMetaArch data', tags: ['tidl_retinanet'] },
  5460. { name: 'domi.InsertNewOps data', tags: ['aipp_op'] } // https://github.com/Ascend/parser/blob/development/parser/proto/insert_op.proto
  5461. ];
  5462. const tags = context.tags('pbtxt');
  5463. if (tags.size > 0) {
  5464. for (const format of formats) {
  5465. if (format.tags.every((tag) => tags.has(tag))) {
  5466. const error = new view.Error(`Invalid file content. File contains ${format.name}.`);
  5467. error.context = context.identifier;
  5468. throw error;
  5469. }
  5470. }
  5471. const entries = [];
  5472. entries.push(...Array.from(tags).filter(([key]) => key.toString().indexOf('.') === -1));
  5473. entries.push(...Array.from(tags).filter(([key]) => key.toString().indexOf('.') !== -1));
  5474. const content = entries.map(([key, value]) => value === true ? key : `${key}:${JSON.stringify(value)}`).join(',');
  5475. throw new view.Error(`Unsupported Protocol Buffers text content '${content.length > 64 ? `${content.substring(0, 100)}...` : content}' for extension '.${extension}'.`);
  5476. }
  5477. };
  5478. const pb = () => {
  5479. const tags = context.tags('pb+');
  5480. if (Object.keys(tags).length > 0) {
  5481. const formats = [
  5482. { name: 'sentencepiece.ModelProto data', tags: [[1,[[1,2],[2,5],[3,0]]],[2,[[1,2],[2,2],[3,0],[4,0],[5,2],[6,0],[7,2],[10,5],[16,0],[40,0],[41,0],[42,0],[43,0]]],[3,[]],[4,[]],[5,[]]] },
  5483. { name: 'mediapipe.BoxDetectorIndex data', tags: [[1,[[1,[[1,[[1,5],[2,5],[3,5],[4,5],[6,0],[7,5],[8,5],[10,5],[11,0],[12,0]]],[2,5],[3,[]]]],[2,false],[3,false],[4,false],[5,false]]],[2,false],[3,false]] },
  5484. { name: 'third_party.tensorflow.python.keras.protobuf.SavedMetadata data', tags: [[1,[[1,[[1,0],[2,0]]],[2,0],[3,2],[4,2],[5,2]]]] },
  5485. { name: 'pblczero.Net data', tags: [[1,5],[2,2],[3,[[1,0],[2,0],[3,0]],[10,[[1,[]],[2,[]],[3,[]],[4,[]],[5,[]],[6,[]]]],[11,[]]]] }, // https://github.com/LeelaChessZero/lczero-common/blob/master/proto/net.proto
  5486. { name: 'chrome_browser_media.PreloadedData', tags: [[1,2]], identifier: 'preloaded_data.pb' }, // https://github.com/kiwibrowser/src/blob/86afd150b847c9dd6f9ad3faddee1a28b8c9b23b/chrome/browser/media/media_engagement_preload.proto#L9
  5487. { name: 'mind_ir.ModelProto', tags: [[1,2],[2,2],[5,2],[7,[]],[10,0],[12,[]],[13,0]] }, // https://github.com/mindspore-ai/mindspore/blob/master/mindspore/core/proto/mind_ir.proto
  5488. { name: 'mindspore.irpb.Checkpoint', tags: [[1,[[1,2],[2,[[1,0],[2,2],[3,2]]]]]] }, // https://github.com/mindspore-ai/mindspore/blob/master/mindspore/ccsrc/utils/checkpoint.proto
  5489. { name: 'optimization_guide.proto.PageTopicsOverrideList data', tags: [[1,[[1,2],[2,[]]]]] }, // https://github.com/chromium/chromium/blob/main/components/optimization_guide/proto/page_topics_override_list.proto
  5490. { name: 'optimization_guide.proto.ModelInfo data', tags: [[1,0],[2,0],[4,0],[6,false],[7,[]],[9,0]] }, // https://github.com/chromium/chromium/blob/22b0d711657b451b61d50dd2e242b3c6e38e6ef5/components/optimization_guide/proto/models.proto#L80
  5491. { name: 'Hobot Dnn data', tags: [[1,0],[2,0],[4,[[1,2],[2,2]]]] }, // https://github.com/HorizonRDK/hobot_dnn
  5492. { name: 'Hobot Dnn data', tags: [[1,0],[2,0],[6,[1,[[1,2],[2,2]]]]] }, // https://github.com/HorizonRDK/hobot_dnn
  5493. ];
  5494. const match = (tags, schema) => {
  5495. for (const [key, inner] of schema) {
  5496. const value = tags[key];
  5497. if (value === undefined) {
  5498. continue;
  5499. }
  5500. if (inner === false) {
  5501. return false;
  5502. }
  5503. if (Array.isArray(inner)) {
  5504. if (typeof value !== 'object' || !match(value, inner)) {
  5505. return false;
  5506. }
  5507. } else if (inner !== value) {
  5508. if (inner === 2 && !Array.isArray(value) && Object(value) === (value) && Object.keys(value).length === 0) {
  5509. return true;
  5510. }
  5511. return false;
  5512. }
  5513. }
  5514. return true;
  5515. };
  5516. for (const format of formats) {
  5517. if (match(tags, format.tags) && (!format.identifier || identifier === context.identifier)) {
  5518. const error = new view.Error(`Invalid file content. File contains ${format.name}.`);
  5519. error.context = context.identifier;
  5520. throw error;
  5521. }
  5522. }
  5523. const format = (tags) => {
  5524. const content = Object.entries(tags).map(([key, value]) => {
  5525. return `${key}:${Object(value) === value ? `{${format(value)}}` : value}`;
  5526. });
  5527. return content.join(',');
  5528. };
  5529. const content = format(tags);
  5530. throw new view.Error(`Unsupported Protocol Buffers content '${content.length > 64 ? `${content.substring(0, 100)}...` : content}' for extension '.${extension}'.`);
  5531. }
  5532. };
  5533. const flatbuffers = () => {
  5534. const stream = context.stream;
  5535. if (stream && stream.length >= 8) {
  5536. let identifier = null;
  5537. const reader = context.peek('flatbuffers.binary');
  5538. if (reader) {
  5539. identifier = reader.identifier;
  5540. } else {
  5541. const data = stream.peek(8);
  5542. if ((data[0] === 0x08 || data[0] === 0x18 || data[0] === 0x1C) && data[1] === 0x00 && data[2] === 0x00 && data[2] === 0x00) {
  5543. identifier = String.fromCharCode.apply(null, data.slice(4, 8));
  5544. }
  5545. }
  5546. if (identifier) {
  5547. const formats = [
  5548. { name: 'ONNX Runtime model data', identifier: 'ORTM' },
  5549. { name: 'TensorFlow Lite model data', identifier: 'TFL3' }
  5550. ];
  5551. for (const format of formats) {
  5552. if (identifier === format.identifier) {
  5553. throw new view.Error(`Invalid file content. File contains ${format.name}.`);
  5554. }
  5555. }
  5556. }
  5557. }
  5558. };
  5559. const xml = () => {
  5560. const tags = context.tags('xml');
  5561. if (tags.size > 0) {
  5562. const formats = [
  5563. { name: 'OpenCV storage data', tags: ['opencv_storage'] },
  5564. { name: 'XHTML markup', tags: ['http://www.w3.org/1999/xhtml:html'] }
  5565. ];
  5566. for (const format of formats) {
  5567. if (format.tags.some((tag) => tags.has(tag))) {
  5568. const error = new view.Error(`Invalid file content. File contains ${format.name}.`);
  5569. error.content = context.identifier;
  5570. throw error;
  5571. }
  5572. }
  5573. throw new view.Error(`Unsupported XML content '${tags.keys().next().value}'.`);
  5574. }
  5575. };
  5576. const hdf5 = () => {
  5577. const obj = context.peek('hdf5');
  5578. if (obj instanceof Error) {
  5579. throw obj;
  5580. }
  5581. if (obj) {
  5582. throw new view.Error(`Invalid file content. File contains HDF5 content.`);
  5583. }
  5584. };
  5585. const unknown = () => {
  5586. if (stream) {
  5587. stream.seek(0);
  5588. const buffer = stream.peek(Math.min(16, stream.length));
  5589. const bytes = Array.from(buffer).map((c) => (c < 16 ? '0' : '') + c.toString(16)).join('');
  5590. const content = stream.length > 268435456 ? `(${bytes}) [${stream.length}]` : `(${bytes})`;
  5591. throw new view.Error(`Unsupported file content ${content} for extension '.${extension}'.`);
  5592. }
  5593. throw new view.Error("Unsupported file directory.");
  5594. };
  5595. json();
  5596. pbtxt();
  5597. pb();
  5598. flatbuffers();
  5599. xml();
  5600. hdf5();
  5601. unknown();
  5602. }
  5603. async _require(id) {
  5604. const module = await this._host.require(id);
  5605. if (!module || !module.ModelFactory) {
  5606. throw new view.Error(`Failed to load module '${id}'.`);
  5607. }
  5608. return new module.ModelFactory();
  5609. }
  5610. async _openContext(context) {
  5611. const modules = this._filter(context).filter((module) => module && module.length > 0);
  5612. const errors = [];
  5613. for (const module of modules) {
  5614. /* eslint-disable no-await-in-loop */
  5615. const factory = await this._require(module);
  5616. /* eslint-enable no-await-in-loop */
  5617. factory.match(context);
  5618. if (context.stream && context.stream.position !== 0) {
  5619. throw new view.Error('Invalid stream position.');
  5620. }
  5621. if (context.type) {
  5622. try {
  5623. /* eslint-disable no-await-in-loop */
  5624. const model = await factory.open(context);
  5625. /* eslint-enable no-await-in-loop */
  5626. if (!model.identifier) {
  5627. model.identifier = context.identifier;
  5628. }
  5629. return model;
  5630. } catch (error) {
  5631. delete context.type;
  5632. delete context.target;
  5633. if (context.stream && context.stream.position !== 0) {
  5634. context.stream.seek(0);
  5635. }
  5636. errors.push(error);
  5637. }
  5638. }
  5639. if (context.stream && context.stream.position !== 0) {
  5640. throw new view.Error('Invalid stream position.');
  5641. }
  5642. }
  5643. if (errors.length > 0) {
  5644. if (errors.length === 1) {
  5645. throw errors[0];
  5646. }
  5647. throw new view.Error(errors.map((err) => err.message).join('\n'));
  5648. }
  5649. return null;
  5650. }
  5651. async _openEntries(entries) {
  5652. try {
  5653. const rootFolder = (files) => {
  5654. const map = files.map((file) => file.split('/').slice(0, -1));
  5655. const at = (index) => (list) => list[index];
  5656. const rotate = (list) => list.length === 0 ? [] : list[0].map((item, index) => list.map(at(index)));
  5657. const equals = (list) => list.every((item) => item === list[0]);
  5658. const folder = rotate(map).filter(equals).map(at(0)).join('/');
  5659. return folder.length === 0 ? folder : `${folder}/`;
  5660. };
  5661. const files = Array.from(entries).filter(([name]) => !(name.endsWith('/') || name.split('/').pop().startsWith('.') || (!name.startsWith('./') && name.startsWith('.'))));
  5662. const folder = rootFolder(files.map(([name]) => name));
  5663. const filter = async (queue, entries) => {
  5664. entries = new Map(Array.from(entries)
  5665. .filter(([path]) => path.startsWith(folder))
  5666. .map(([path, stream]) => [path.substring(folder.length), stream]));
  5667. const entryContext = new view.EntryContext(this._host, entries);
  5668. let matches = [];
  5669. for (const [name, stream] of queue) {
  5670. const identifier = name.substring(folder.length);
  5671. const context = new view.Context(entryContext, identifier, stream);
  5672. const modules = this._filter(context);
  5673. for (const module of modules) {
  5674. /* eslint-disable no-await-in-loop */
  5675. const factory = await this._require(module);
  5676. /* eslint-enable no-await-in-loop */
  5677. factory.match(context);
  5678. if (context.stream && context.stream.position !== 0) {
  5679. throw new view.Error('Invalid stream position.');
  5680. }
  5681. delete context.target;
  5682. if (context.type) {
  5683. matches = matches.filter((match) => !factory.filter || factory.filter(context, match.type));
  5684. if (matches.every((match) => !match.factory.filter || match.factory.filter(match, context.type))) {
  5685. context.factory = factory;
  5686. matches.push(context);
  5687. }
  5688. break;
  5689. }
  5690. }
  5691. }
  5692. if (matches.length > 1) {
  5693. const content = matches.map((context) => context.type).join(',');
  5694. throw new view.ArchiveError(`Archive contains multiple model files '${content}'.`);
  5695. }
  5696. if (matches.length > 0) {
  5697. const match = matches.shift();
  5698. delete match.type;
  5699. delete match.factory;
  5700. return match;
  5701. }
  5702. return null;
  5703. };
  5704. const queue = files.filter(([name]) => name.substring(folder.length).indexOf('/') < 0);
  5705. let context = await filter(queue, entries);
  5706. if (!context) {
  5707. const queue = files.filter(([name]) => name.substring(folder.length).indexOf('/') >= 0);
  5708. context = await filter(queue, entries);
  5709. }
  5710. return context;
  5711. } catch (error) {
  5712. throw new view.ArchiveError(error.message);
  5713. }
  5714. }
  5715. accept(identifier, size) {
  5716. const extension = identifier.indexOf('.') === -1 ? '' : identifier.split('.').pop().toLowerCase();
  5717. identifier = identifier.toLowerCase().split('/').pop();
  5718. let accept = false;
  5719. for (const extension of this._patterns) {
  5720. if ((typeof extension === 'string' && identifier.endsWith(extension)) ||
  5721. (extension instanceof RegExp && extension.exec(identifier))) {
  5722. accept = true;
  5723. break;
  5724. }
  5725. }
  5726. this._host.event('model_file', {
  5727. file_extension: extension,
  5728. file_size: size || 0,
  5729. file_accept: accept ? 1 : 0
  5730. });
  5731. return accept;
  5732. }
  5733. _filter(context) {
  5734. const identifier = context.identifier.toLowerCase().split('/').pop();
  5735. const list = this._factories.filter((entry) =>
  5736. (typeof entry.pattern === 'string' && identifier.endsWith(entry.pattern)) ||
  5737. (entry.pattern instanceof RegExp && entry.pattern.test(identifier)));
  5738. return Array.from(new Set(list.map((entry) => entry.module)));
  5739. }
  5740. async _openSignature(context) {
  5741. const stream = context.stream;
  5742. if (stream) {
  5743. let empty = true;
  5744. let position = 0;
  5745. while (position < stream.length) {
  5746. const buffer = stream.read(Math.min(4096, stream.length - position));
  5747. position += buffer.length;
  5748. if (!buffer.every((value) => value === 0x00)) {
  5749. empty = false;
  5750. break;
  5751. }
  5752. }
  5753. stream.seek(0);
  5754. if (empty) {
  5755. throw new view.Error('File has no content.');
  5756. }
  5757. /* eslint-disable no-control-regex */
  5758. const entries = [
  5759. { name: 'ELF executable', value: /^\x7FELF/ },
  5760. { name: 'PNG image', value: /^\x89PNG/ },
  5761. { name: 'Git LFS header', value: /^version https:\/\/git-lfs.github.com/ },
  5762. { name: 'Git LFS header', value: /^\s*oid sha256:/ },
  5763. { name: 'GGML data', value: /^lmgg|fmgg|tjgg|algg|fugg/ },
  5764. { name: 'HTML markup', value: /^\s*<html(\s+[^>]+)?>/ },
  5765. { name: 'HTML markup', value: /^\s*<!doctype\s*html>/ },
  5766. { name: 'HTML markup', value: /^\s*<!DOCTYPE\s*html>/ },
  5767. { name: 'HTML markup', value: /^\s*<!DOCTYPE\s*HTML>/ },
  5768. { name: 'HTML markup', value: /^\s*<!DOCTYPE\s*HTML\s+(PUBLIC|SYSTEM)?/ },
  5769. { name: 'Unity metadata', value: /^fileFormatVersion:/ },
  5770. { name: 'Python source code', value: /^((#.*(\n|\r\n))|('''.*'''(\n|\r\n))|("""[\s\S]*""")|(\n|\r\n))*(import[ ]+[a-zA-Z_]\w*(\.[a-zA-Z_]\w*)*([ ]+as[ ]+[a-zA-Z]\w*)?[ ]*(,|;|\n|\r\n))/ },
  5771. { name: 'Python source code', value: /^((#.*(\n|\r\n))|('''.*'''(\n|\r\n))|("""[\s\S]*""")|(\n|\r\n))*(from[ ]+([a-zA-Z_]\w*(\.[a-zA-Z_]\w*)*)[ ]+import[ ]+[a-zA-Z]\w*)/ },
  5772. { name: 'Python virtual environment configuration', value: /^home[ ]*=[ ]*/, identifier: 'pyvenv.cfg' },
  5773. { name: 'Bash script', value: /^#!\/usr\/bin\/env\s/ },
  5774. { name: 'Bash script', value: /^#!\/bin\/bash\s/ },
  5775. { name: 'TSD header', value: /^%TSD-Header-###%/ },
  5776. { name: 'AppleDouble data', value: /^\x00\x05\x16\x07/ },
  5777. { name: 'TensorFlow Hub module', value: /^\x08\x03$/, identifier: 'tfhub_module.pb' },
  5778. { name: 'V8 snapshot', value: /^.\x00\x00\x00.\x00\x00\x00/, identifier: 'snapshot_blob.bin' },
  5779. { name: 'V8 context snapshot', value: /^.\x00\x00\x00.\x00\x00\x00/, identifier: 'v8_context_snapshot.bin' },
  5780. { name: 'V8 natives blob', value: /^./, identifier: 'natives_blob.bin' },
  5781. { name: 'ViSQOL model', value: /^svm_type\s/ },
  5782. { name: 'SenseTime model', value: /^STEF/ },
  5783. { name: 'AES Crypt data', value: /^AES[\x01|\x02]\x00/ },
  5784. { name: 'BModel data', value: /^\xEE\xAA\x55\xFF/ }, // https://github.com/sophgo/tpu-mlir/blob/master/include/tpu_mlir/Builder/BM168x/bmodel.fbs
  5785. { name: 'CviModel data', value: /^CviModel/ }, // https://github.com/sophgo/tpu-mlir/blob/master/include/tpu_mlir/Builder/CV18xx/proto/cvimodel.fbs
  5786. { name: 'Tokenizer data', value: /^IQ== 0\n/ }
  5787. ];
  5788. /* eslint-enable no-control-regex */
  5789. const buffer = stream.peek(Math.min(4096, stream.length));
  5790. const content = String.fromCharCode.apply(null, buffer);
  5791. for (const entry of entries) {
  5792. if (content.match(entry.value) && (!entry.identifier || entry.identifier === context.identifier)) {
  5793. throw new view.Error(`Invalid file content. File contains ${entry.name}.`);
  5794. }
  5795. }
  5796. }
  5797. }
  5798. async import() {
  5799. if (this._host.type === 'Browser' || this._host.type === 'Python') {
  5800. const files = [
  5801. './server', './onnx', './pytorch', './tflite', './mlnet',
  5802. './onnx-proto', './onnx-schema', './tflite-schema',
  5803. 'onnx-metadata.json', 'pytorch-metadata.json', 'tflite-metadata.json'
  5804. ];
  5805. for (const file of files) {
  5806. /* eslint-disable no-await-in-loop */
  5807. try {
  5808. if (file.startsWith('./')) {
  5809. await this._host.require(file);
  5810. } else if (file.endsWith('.json')) {
  5811. await this._host.request(file, 'utf-8', null);
  5812. }
  5813. } catch {
  5814. // continue regardless of error
  5815. }
  5816. /* eslint-enable no-await-in-loop */
  5817. }
  5818. }
  5819. }
  5820. };
  5821. view.Metadata = class {
  5822. static async open(context, name) {
  5823. view.Metadata._metadata = view.Metadata._metadata || new Map();
  5824. const metadata = view.Metadata._metadata;
  5825. if (!metadata.has(name)) {
  5826. try {
  5827. const content = await context.request(name);
  5828. const types = JSON.parse(content);
  5829. metadata.set(name, new view.Metadata(types));
  5830. } catch {
  5831. metadata.set(name, new view.Metadata(null));
  5832. }
  5833. }
  5834. return metadata.get(name);
  5835. }
  5836. constructor(types) {
  5837. this._types = new Map();
  5838. this._attributes = new Map();
  5839. this._inputs = new Map();
  5840. if (Array.isArray(types)) {
  5841. for (const type of types) {
  5842. this._types.set(type.name, type);
  5843. if (type.identifier !== undefined) {
  5844. this._types.set(type.identifier, type);
  5845. }
  5846. }
  5847. }
  5848. }
  5849. type(name) {
  5850. if (!this._types.has(name)) {
  5851. this._types.set(name, { name: name.toString() });
  5852. }
  5853. return this._types.get(name);
  5854. }
  5855. attribute(type, name) {
  5856. const key = `${type}:${name}`;
  5857. if (!this._attributes.has(key)) {
  5858. this._attributes.set(key, null);
  5859. const metadata = this.type(type);
  5860. if (metadata && Array.isArray(metadata.attributes)) {
  5861. for (const attribute of metadata.attributes) {
  5862. this._attributes.set(`${type}:${attribute.name}`, attribute);
  5863. }
  5864. }
  5865. }
  5866. return this._attributes.get(key);
  5867. }
  5868. input(type, name) {
  5869. const key = `${type}:${name}`;
  5870. if (!this._inputs.has(key)) {
  5871. this._inputs.set(key, null);
  5872. const metadata = this.type(type);
  5873. if (metadata && Array.isArray(metadata.inputs)) {
  5874. for (const input of metadata.inputs) {
  5875. this._inputs.set(`${type}:${input.name}`, input);
  5876. }
  5877. }
  5878. }
  5879. return this._inputs.get(key);
  5880. }
  5881. };
  5882. view.Error = class extends Error {
  5883. constructor(message) {
  5884. super(message);
  5885. this.name = 'Error loading model.';
  5886. }
  5887. };
  5888. if (typeof window !== 'undefined' && window.exports) {
  5889. window.exports.view = view;
  5890. }
  5891. export const View = view.View;
  5892. export const ModelFactoryService = view.ModelFactoryService;
  5893. export const ModelSidebar = view.ModelSidebar;
  5894. export const NodeSidebar = view.NodeSidebar;
  5895. export const TensorSidebar = view.TensorSidebar;
  5896. export const Documentation = view.Documentation;
  5897. export const Formatter = view.Formatter;
  5898. export const Tensor = view.Tensor;
  5899. export const Quantization = view.Quantization;