view.js 259 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214
  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. if (Array.isArray(node.inputs)) {
  1937. for (const argument of node.inputs) {
  1938. const type = argument.type;
  1939. if (type === 'graph' || type === 'object' || type === 'object[]' || type === 'function' || type === 'function[]') {
  1940. objects.push(argument);
  1941. } else if (options.weights && argument.visible !== false && argument.type !== 'attribute' && Array.isArray(argument.value) && argument.value.length === 1 && argument.value[0].initializer) {
  1942. const item = this.context.createArgument(argument);
  1943. list().add(item);
  1944. } 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)) {
  1945. hiddenTensors = true;
  1946. } else if (options.attributes && argument.visible !== false && argument.type && !argument.type.endsWith('*')) {
  1947. const item = attribute(argument);
  1948. list().add(item);
  1949. }
  1950. }
  1951. }
  1952. if (Array.isArray(node.attributes)) {
  1953. const attributes = node.attributes.slice();
  1954. attributes.sort((a, b) => a.name.toUpperCase().localeCompare(b.name.toUpperCase()));
  1955. for (const argument of attributes) {
  1956. const type = argument.type;
  1957. if (type === 'graph' || type === 'object' || type === 'object[]' || type === 'function' || type === 'function[]') {
  1958. objects.push(argument);
  1959. } else if (options.attributes && argument.visible !== false) {
  1960. const item = attribute(argument);
  1961. list().add(item);
  1962. }
  1963. }
  1964. }
  1965. if (hiddenTensors) {
  1966. const item = list().argument('\u3008\u2026\u3009', '');
  1967. list().add(item);
  1968. }
  1969. for (const argument of objects) {
  1970. const type = argument.type;
  1971. let content = null;
  1972. if (type === 'graph') {
  1973. content = this.context.createNode(null, argument.value);
  1974. } else if (type === 'function' || argument.type === 'object') {
  1975. content = this.context.createNode(argument.value);
  1976. } else if (type === 'function[]' || argument.type === 'object[]') {
  1977. content = argument.value.map((value) => this.context.createNode(value));
  1978. }
  1979. const item = list().argument(argument.name, content);
  1980. list().add(item);
  1981. }
  1982. if (Array.isArray(node.nodes) && node.nodes.length > 0) {
  1983. // this.canvas = this.canvas();
  1984. }
  1985. if (Array.isArray(node.chain) && node.chain.length > 0) {
  1986. for (const innerNode of node.chain) {
  1987. this.context.createNode(innerNode);
  1988. this._add(innerNode);
  1989. }
  1990. }
  1991. if (node.inner) {
  1992. this.context.createNode(node.inner);
  1993. this._add(node.inner);
  1994. }
  1995. }
  1996. toggle() {
  1997. this._expand.content = '-';
  1998. this._graph = new view.Graph(this.context.view, this.context.view.host, this.context.model, this.context.options, false, {});
  1999. this._graph.add(this.value);
  2000. // const document = this.element.ownerDocument;
  2001. // const parent = this.element.parentElement;
  2002. // this._graph.build(document, parent);
  2003. // this._graph.update();
  2004. this.canvas.width = 300;
  2005. this.canvas.height = 300;
  2006. this.layout();
  2007. this.context.update();
  2008. }
  2009. activate() {
  2010. this.context.view.showNodeProperties(this.value);
  2011. }
  2012. edge(to) {
  2013. this._edges = this._edges || new Map();
  2014. if (!this._edges.has(to)) {
  2015. this._edges.set(to, new view.Edge(this, to));
  2016. }
  2017. return this._edges.get(to);
  2018. }
  2019. };
  2020. view.Input = class extends grapher.Node {
  2021. constructor(context, value) {
  2022. super();
  2023. this.context = context;
  2024. this.value = value;
  2025. view.Input.counter = view.Input.counter || 0;
  2026. const types = value.value.map((argument) => argument.type || '').join('\n');
  2027. let name = value.name || '';
  2028. if (name.length > 16) {
  2029. name = name.split('/').pop();
  2030. }
  2031. const header = this.header();
  2032. const title = header.add(null, ['graph-item-input'], name, types);
  2033. title.on('click', () => this.context.view.showModelProperties());
  2034. this.id = `input-${name ? `name-${name}` : `id-${(view.Input.counter++)}`}`;
  2035. }
  2036. get class() {
  2037. return 'graph-input';
  2038. }
  2039. get inputs() {
  2040. return [];
  2041. }
  2042. get outputs() {
  2043. return [this.value];
  2044. }
  2045. activate() {
  2046. this.context.view.showModelProperties();
  2047. }
  2048. edge(to) {
  2049. this._edges = this._edges || new Map();
  2050. if (!this._edges.has(to)) {
  2051. this._edges.set(to, new view.Edge(this, to));
  2052. }
  2053. return this._edges.get(to);
  2054. }
  2055. };
  2056. view.Output = class extends grapher.Node {
  2057. constructor(context, value) {
  2058. super();
  2059. this.context = context;
  2060. this.value = value;
  2061. const types = value.value.map((argument) => argument.type || '').join('\n');
  2062. let name = value.name || '';
  2063. if (name.length > 16) {
  2064. name = name.split('/').pop();
  2065. }
  2066. const header = this.header();
  2067. const title = header.add(null, ['graph-item-output'], name, types);
  2068. title.on('click', () => this.context.view.showModelProperties());
  2069. }
  2070. get inputs() {
  2071. return [this.value];
  2072. }
  2073. get outputs() {
  2074. return [];
  2075. }
  2076. activate() {
  2077. this.context.view.showModelProperties();
  2078. }
  2079. };
  2080. view.Value = class {
  2081. constructor(context, value) {
  2082. this.context = context;
  2083. this.value = value;
  2084. this.from = null;
  2085. this.to = [];
  2086. }
  2087. controlDependency(node) {
  2088. this._controlDependencies = this._controlDependencies || new Set();
  2089. this._controlDependencies.add(this.to.length);
  2090. this.to.push(node);
  2091. }
  2092. build() {
  2093. this._edges = this._edges || [];
  2094. if (this.from && Array.isArray(this.to)) {
  2095. for (let i = 0; i < this.to.length; i++) {
  2096. const to = this.to[i];
  2097. let content = '';
  2098. const type = this.value.type;
  2099. if (type &&
  2100. type.shape &&
  2101. type.shape.dimensions &&
  2102. type.shape.dimensions.length > 0 &&
  2103. type.shape.dimensions.every((dim) => !dim || Number.isInteger(dim) || typeof dim === 'bigint' || (typeof dim === 'string'))) {
  2104. content = type.shape.dimensions.map((dim) => (dim !== null && dim !== undefined && dim !== -1) ? dim : '?').join('\u00D7');
  2105. content = content.length > 16 ? '' : content;
  2106. }
  2107. if (this.context.options.names) {
  2108. content = this.value.name.split('\n').shift(); // custom argument id
  2109. }
  2110. const edge = this.from.edge(to);
  2111. if (!edge.value) {
  2112. edge.value = this;
  2113. if (content) {
  2114. edge.label = content;
  2115. }
  2116. edge.id = `edge-${this.value.name}`;
  2117. if (this._controlDependencies && this._controlDependencies.has(i)) {
  2118. edge.class = 'edge-path-control-dependency';
  2119. }
  2120. }
  2121. this.context.setEdge(edge);
  2122. this._edges.push(edge);
  2123. }
  2124. }
  2125. }
  2126. select() {
  2127. let array = [];
  2128. if (Array.isArray(this._edges)) {
  2129. for (const edge of this._edges) {
  2130. array = array.concat(edge.select());
  2131. }
  2132. }
  2133. return array;
  2134. }
  2135. deselect() {
  2136. if (Array.isArray(this._edges)) {
  2137. for (const edge of this._edges) {
  2138. edge.deselect();
  2139. }
  2140. }
  2141. }
  2142. activate() {
  2143. if (this.value && this.from && Array.isArray(this.to) && !this.value.initializer) {
  2144. const from = this.from.value;
  2145. const to = this.to.map((node) => node.value);
  2146. this.context.view.showConnectionProperties(this.value, from, to);
  2147. } else if (this.value && this.value.initializer) {
  2148. this.context.view.showTensorProperties({ value: [this.value] });
  2149. }
  2150. }
  2151. };
  2152. view.Argument = class extends grapher.Argument {
  2153. constructor(context, value) {
  2154. const name = value.name;
  2155. let content = '';
  2156. let separator = '';
  2157. let tooltip = '';
  2158. if (Array.isArray(value.value) && value.value.length === 1 && value.value[0].initializer) {
  2159. const tensor = value.value[0].initializer;
  2160. const type = value.value[0].type;
  2161. if (type && type.shape && type.shape.dimensions && Array.isArray(type.shape.dimensions)) {
  2162. tooltip = type.toString();
  2163. content = `\u3008${type.shape.dimensions.map((d) => (d !== null && d !== undefined) ? d : '?').join('\u00D7')}\u3009`;
  2164. if (type.shape.dimensions.length === 0) {
  2165. const formatter = new view.Formatter(tensor, 'tensor');
  2166. content = formatter.toString();
  2167. separator = ' = ';
  2168. }
  2169. }
  2170. }
  2171. super(name, content);
  2172. this.context = context;
  2173. this.value = value;
  2174. this.separator = separator;
  2175. this.tooltip = tooltip;
  2176. }
  2177. focus() {
  2178. this.context.focus([this.value]);
  2179. }
  2180. blur() {
  2181. this.context.blur([this.value]);
  2182. }
  2183. activate() {
  2184. this.context.view.showTensorProperties(this.value);
  2185. }
  2186. };
  2187. view.Edge = class extends grapher.Edge {
  2188. constructor(from, to) {
  2189. super(from, to);
  2190. this.v = from.name;
  2191. this.w = to.name;
  2192. }
  2193. get minlen() {
  2194. if (this.from.inputs.every((argument) => (!argument.type || argument.type.endsWith('*')) && argument.value.every((value) => value.initializer))) {
  2195. return 2;
  2196. }
  2197. return 1;
  2198. }
  2199. focus() {
  2200. this.value.context.focus([this.value.value]);
  2201. }
  2202. blur() {
  2203. this.value.context.blur([this.value.value]);
  2204. }
  2205. activate() {
  2206. this.value.context.activate(this.value.value);
  2207. }
  2208. };
  2209. view.Sidebar = class {
  2210. constructor(host) {
  2211. this._host = host;
  2212. this._stack = [];
  2213. const pop = () => this._update(this._stack.slice(0, -1));
  2214. this._closeSidebarHandler = () => pop();
  2215. this._closeSidebarKeyDownHandler = (e) => {
  2216. if (e.keyCode === 27) {
  2217. e.stopPropagation();
  2218. e.preventDefault();
  2219. pop();
  2220. }
  2221. };
  2222. const sidebar = this._element('sidebar');
  2223. sidebar.addEventListener('transitionend', (event) => {
  2224. if (event.propertyName === 'opacity' && sidebar.style.opacity === '0') {
  2225. const content = this._element('sidebar-content');
  2226. content.innerHTML = '';
  2227. }
  2228. });
  2229. }
  2230. _element(id) {
  2231. return this._host.document.getElementById(id);
  2232. }
  2233. open(content, title) {
  2234. const element = this._render(content);
  2235. const entry = { title, element, content };
  2236. this._update([entry]);
  2237. }
  2238. close() {
  2239. this._update([]);
  2240. }
  2241. push(content, title) {
  2242. const element = this._render(content);
  2243. const entry = { title, content, element };
  2244. this._update(this._stack.concat(entry));
  2245. }
  2246. _render(content) {
  2247. try {
  2248. content.render();
  2249. } catch (error) {
  2250. content.error(error, false);
  2251. }
  2252. const element = content.element;
  2253. return Array.isArray(element) ? element : [element];
  2254. }
  2255. _update(stack) {
  2256. const sidebar = this._element('sidebar');
  2257. const content = this._element('sidebar-content');
  2258. const container = this._element('graph');
  2259. const closeButton = this._element('sidebar-closebutton');
  2260. closeButton.removeEventListener('click', this._closeSidebarHandler);
  2261. this._host.document.removeEventListener('keydown', this._closeSidebarKeyDownHandler);
  2262. if (this._stack.length > 0) {
  2263. const entry = this._stack.pop();
  2264. if (entry.content && entry.content.deactivate) {
  2265. entry.content.deactivate();
  2266. }
  2267. }
  2268. if (stack) {
  2269. this._stack = stack;
  2270. }
  2271. if (this._stack.length > 0) {
  2272. const entry = this._stack[this._stack.length - 1];
  2273. this._element('sidebar-title').innerHTML = entry.title || '';
  2274. closeButton.addEventListener('click', this._closeSidebarHandler);
  2275. if (typeof entry.content === 'string') {
  2276. content.innerHTML = entry.element;
  2277. } else if (entry.element instanceof Array) {
  2278. content.innerHTML = '';
  2279. for (const element of entry.element) {
  2280. content.appendChild(element);
  2281. }
  2282. } else {
  2283. content.innerHTML = '';
  2284. content.appendChild(entry.element);
  2285. }
  2286. if (entry.content && entry.content.activate) {
  2287. entry.content.activate();
  2288. }
  2289. sidebar.style.width = 'min(calc(100% * 0.6), 42em)';
  2290. sidebar.style.right = 0;
  2291. sidebar.style.opacity = 1;
  2292. this._host.document.addEventListener('keydown', this._closeSidebarKeyDownHandler);
  2293. container.style.width = 'max(40vw, calc(100vw - 42em))';
  2294. } else {
  2295. sidebar.style.right = 'calc(0px - min(calc(100% * 0.6), 42em))';
  2296. sidebar.style.opacity = 0;
  2297. const clone = content.cloneNode(true);
  2298. content.parentNode.replaceChild(clone, content);
  2299. container.style.width = '100%';
  2300. container.focus();
  2301. }
  2302. }
  2303. };
  2304. view.Control = class {
  2305. constructor(context) {
  2306. this._view = context;
  2307. this._host = context.host;
  2308. }
  2309. createElement(tagName, className) {
  2310. const element = this._host.document.createElement(tagName);
  2311. if (className) {
  2312. element.setAttribute('class', className);
  2313. }
  2314. return element;
  2315. }
  2316. on(event, callback) {
  2317. this._events = this._events || {};
  2318. this._events[event] = this._events[event] || [];
  2319. this._events[event].push(callback);
  2320. }
  2321. emit(event, data) {
  2322. if (this._events && this._events[event]) {
  2323. for (const callback of this._events[event]) {
  2324. callback(this, data);
  2325. }
  2326. }
  2327. }
  2328. error(error, fatal) {
  2329. this._view.exception(error, fatal || false);
  2330. }
  2331. };
  2332. view.Expander = class extends view.Control {
  2333. constructor(context) {
  2334. super(context);
  2335. this.element = this.createElement('div', 'sidebar-item-value');
  2336. this._count = -1;
  2337. }
  2338. render() {
  2339. return [this.element];
  2340. }
  2341. enable() {
  2342. this._expander = this.createElement('div', 'sidebar-item-value-expander');
  2343. this._expander.innerText = '+';
  2344. this._expander.addEventListener('click', () => this.toggle());
  2345. this.add(this._expander);
  2346. }
  2347. add(element) {
  2348. this.element.appendChild(element);
  2349. }
  2350. control(element) {
  2351. this.add(element);
  2352. }
  2353. toggle() {
  2354. this._count = this._count === -1 ? this.element.childElementCount : this._count;
  2355. if (this._expander) {
  2356. while (this.element.childElementCount > this._count) {
  2357. this.element.removeChild(this.element.lastChild);
  2358. }
  2359. if (this._expander.innerText === '+') {
  2360. this._expander.innerText = '-';
  2361. this.expand();
  2362. } else {
  2363. this._expander.innerText = '+';
  2364. this.collapse();
  2365. }
  2366. }
  2367. }
  2368. expand() {
  2369. }
  2370. collapse() {
  2371. }
  2372. };
  2373. view.ObjectSidebar = class extends view.Control {
  2374. constructor(context) {
  2375. super(context);
  2376. this.element = this.createElement('div', 'sidebar-object');
  2377. }
  2378. addEntry(name, item) {
  2379. const entry = new view.NameValueView(this._view, name, item);
  2380. const element = entry.render();
  2381. this.element.appendChild(element);
  2382. }
  2383. addProperty(name, value, style) {
  2384. const item = new view.TextView(this._view, value, style);
  2385. this.addEntry(name, item);
  2386. return item;
  2387. }
  2388. addHeader(title) {
  2389. const element = this.createElement('div', 'sidebar-header');
  2390. element.innerText = title;
  2391. this.element.appendChild(element);
  2392. }
  2393. error(error, fatal) {
  2394. super.error(error, fatal);
  2395. const element = this.createElement('span');
  2396. element.innerHTML = `<b>ERROR:</b> ${error.message}`;
  2397. this.element.appendChild(element);
  2398. }
  2399. };
  2400. view.NodeSidebar = class extends view.ObjectSidebar {
  2401. constructor(context, node) {
  2402. super(context);
  2403. this._node = node;
  2404. }
  2405. render() {
  2406. const node = this._node;
  2407. if (node.type) {
  2408. const type = node.type;
  2409. const item = this.addProperty('type', node.type.identifier || node.type.name);
  2410. if (type && (type.description || type.inputs || type.outputs || type.attributes)) {
  2411. item.action(type.nodes ? '\u0192' : '?', 'Show Definition', () => {
  2412. this.emit('show-documentation', null);
  2413. });
  2414. }
  2415. const module = node.type.module;
  2416. const version = node.type.version;
  2417. const status = node.type.status;
  2418. if (module || version || status) {
  2419. const list = [module, version ? `v${version}` : '', status];
  2420. const value = list.filter((value) => value).join(' ');
  2421. this.addProperty('module', value, 'nowrap');
  2422. }
  2423. }
  2424. if (node.name) {
  2425. this.addProperty('name', node.name, 'nowrap');
  2426. }
  2427. if (node.identifier) {
  2428. this.addProperty('identifier', node.identifier, 'nowrap');
  2429. }
  2430. if (node.description) {
  2431. this.addProperty('description', node.description);
  2432. }
  2433. if (node.device) {
  2434. this.addProperty('device', node.device);
  2435. }
  2436. const attributes = node.attributes;
  2437. if (Array.isArray(attributes) && attributes.length > 0) {
  2438. this.addHeader('Attributes');
  2439. attributes.sort((a, b) => a.name.localeCompare(b.name, undefined, { sensitivity: 'base' }));
  2440. for (const attribute of attributes) {
  2441. this.addArgument(attribute.name, attribute, 'attribute');
  2442. }
  2443. }
  2444. const inputs = node.inputs;
  2445. if (Array.isArray(inputs) && inputs.length > 0) {
  2446. this.addHeader('Inputs');
  2447. for (const input of inputs) {
  2448. const name = input.name;
  2449. this.addArgument(name, input);
  2450. }
  2451. }
  2452. const outputs = node.outputs;
  2453. if (Array.isArray(outputs) && outputs.length > 0) {
  2454. this.addHeader('Outputs');
  2455. for (const output of outputs) {
  2456. const name = output.name;
  2457. this.addArgument(name, output);
  2458. }
  2459. }
  2460. const metadata = node.metadata;
  2461. if (Array.isArray(metadata) && metadata.length > 0) {
  2462. this.addHeader('Metadata');
  2463. for (const entry of metadata) {
  2464. this.addArgument(entry.name, entry, 'attribute');
  2465. }
  2466. }
  2467. }
  2468. addArgument(name, argument, source) {
  2469. const value = new view.ArgumentView(this._view, argument, source);
  2470. value.on('focus', (sender, value) => {
  2471. this.emit('focus', value);
  2472. this._focused = this._focused || new Set();
  2473. this._focused.add(value);
  2474. });
  2475. value.on('blur', (sender, value) => {
  2476. this.emit('blur', value);
  2477. this._focused = this._focused || new Set();
  2478. this._focused.delete(value);
  2479. });
  2480. value.on('select', (sender, value) => this.emit('select', value));
  2481. value.on('activate', (sender, value) => this.emit('activate', value));
  2482. this.addEntry(name, value);
  2483. }
  2484. activate() {
  2485. this.emit('select', this._node);
  2486. }
  2487. deactivate() {
  2488. this.emit('select', null);
  2489. if (this._focused) {
  2490. for (const value of this._focused) {
  2491. this.emit('blur', value);
  2492. }
  2493. this._focused.clear();
  2494. }
  2495. }
  2496. };
  2497. view.NameValueView = class extends view.Control {
  2498. constructor(context, name, value) {
  2499. super(context);
  2500. this._name = name;
  2501. this._value = value;
  2502. const nameElement = this.createElement('div', 'sidebar-item-name');
  2503. const input = this.createElement('input');
  2504. input.setAttribute('type', 'text');
  2505. input.setAttribute('value', name);
  2506. input.setAttribute('title', name);
  2507. input.setAttribute('readonly', 'true');
  2508. nameElement.appendChild(input);
  2509. const valueElement = this.createElement('div', 'sidebar-item-value-list');
  2510. for (const element of value.render()) {
  2511. valueElement.appendChild(element);
  2512. }
  2513. this.element = this.createElement('div', 'sidebar-item');
  2514. this.element.appendChild(nameElement);
  2515. this.element.appendChild(valueElement);
  2516. }
  2517. get name() {
  2518. return this._name;
  2519. }
  2520. render() {
  2521. return this.element;
  2522. }
  2523. toggle() {
  2524. this._value.toggle();
  2525. }
  2526. };
  2527. view.SelectView = class extends view.Control {
  2528. constructor(context, entries, selected) {
  2529. super(context);
  2530. this._elements = [];
  2531. this._entries = Array.from(entries);
  2532. const selectElement = this.createElement('select', 'sidebar-item-selector');
  2533. selectElement.addEventListener('change', (e) => {
  2534. this.emit('change', this._entries[e.target.selectedIndex][1]);
  2535. });
  2536. this._elements.push(selectElement);
  2537. for (const [name, value] of this._entries) {
  2538. const element = this.createElement('option');
  2539. element.innerText = name;
  2540. if (value === selected) {
  2541. element.setAttribute('selected', 'selected');
  2542. }
  2543. selectElement.appendChild(element);
  2544. }
  2545. }
  2546. render() {
  2547. return this._elements;
  2548. }
  2549. };
  2550. view.TextView = class extends view.Control {
  2551. constructor(context, value, style) {
  2552. super(context);
  2553. this.element = this.createElement('div', 'sidebar-item-value');
  2554. let className = 'sidebar-item-value-line';
  2555. if (value) {
  2556. const list = Array.isArray(value) ? value : [value];
  2557. for (const item of list) {
  2558. const line = this.createElement('div', className);
  2559. switch (style) {
  2560. case 'code':
  2561. line.innerHTML = `<code>${item}<code>`;
  2562. break;
  2563. case 'bold':
  2564. line.innerHTML = `<b>${item}<b>`;
  2565. break;
  2566. case 'nowrap':
  2567. line.innerText = item;
  2568. line.style.whiteSpace = style;
  2569. break;
  2570. default:
  2571. line.innerText = item;
  2572. break;
  2573. }
  2574. this.element.appendChild(line);
  2575. className = 'sidebar-item-value-line-border';
  2576. }
  2577. } else {
  2578. const line = this.createElement('div', className);
  2579. line.classList.add('sidebar-item-disable-select');
  2580. line.innerHTML = '&nbsp';
  2581. this.element.appendChild(line);
  2582. }
  2583. }
  2584. action(text, description, callback) {
  2585. const action = this.createElement('div', 'sidebar-item-value-expander');
  2586. action.setAttribute('title', description);
  2587. action.addEventListener('click', () => callback());
  2588. action.innerHTML = text;
  2589. this.element.insertBefore(action, this.element.childNodes[0]);
  2590. }
  2591. render() {
  2592. return [this.element];
  2593. }
  2594. toggle() {
  2595. }
  2596. };
  2597. view.ArgumentView = class extends view.Control {
  2598. constructor(context, argument, source) {
  2599. super(context);
  2600. this._argument = argument;
  2601. this._source = source;
  2602. this._elements = [];
  2603. this._items = [];
  2604. const type = argument.type === 'attribute' ? null : argument.type;
  2605. let value = argument.value;
  2606. if (argument.type === 'attribute') {
  2607. this._source = 'attribute';
  2608. }
  2609. if (argument.type === 'tensor') {
  2610. value = [{ type: value.type, initializer: value }];
  2611. } else if (argument.type === 'tensor[]') {
  2612. value = value.map((value) => ({ type: value.type, initializer: value }));
  2613. }
  2614. this._source = typeof type === 'string' && !type.endsWith('*') ? 'attribute' : this._source;
  2615. if (this._source === 'attribute' && type !== 'tensor' && type !== 'tensor[]') {
  2616. this._source = 'attribute';
  2617. const item = new view.PrimitiveView(context, argument);
  2618. this._items.push(item);
  2619. } else if (Array.isArray(value) && value.length === 0) {
  2620. const item = new view.TextView(this._view, null);
  2621. this._items.push(item);
  2622. } else {
  2623. const values = value;
  2624. for (const value of values) {
  2625. const emit = values.length === 1 && value.initializer;
  2626. const target = emit ? argument : value;
  2627. const item = new view.ValueView(context, value, this._source);
  2628. item.on('focus', () => this.emit('focus', target));
  2629. item.on('blur', () => this.emit('blur', target));
  2630. item.on('activate', () => this.emit('activate', target));
  2631. item.on('select', () => this.emit('select', target));
  2632. this._items.push(item);
  2633. }
  2634. }
  2635. for (const item of this._items) {
  2636. this._elements.push(...item.render());
  2637. }
  2638. }
  2639. render() {
  2640. return this._elements;
  2641. }
  2642. toggle() {
  2643. if (this._source === 'attribute') {
  2644. if (this._expander.innerText === '+') {
  2645. this._expander.innerText = '-';
  2646. } else {
  2647. this._expander.innerText = '+';
  2648. while (this._element.childElementCount > 2) {
  2649. this._element.removeChild(this._element.lastChild);
  2650. }
  2651. }
  2652. } else {
  2653. for (const item of this._items) {
  2654. item.toggle();
  2655. }
  2656. }
  2657. }
  2658. };
  2659. view.PrimitiveView = class extends view.Expander {
  2660. constructor(context, argument) {
  2661. super(context);
  2662. try {
  2663. this._argument = argument;
  2664. const type = argument.type === 'attribute' ? null : argument.type;
  2665. const value = argument.value;
  2666. if (type) {
  2667. this.enable();
  2668. }
  2669. switch (type) {
  2670. case 'graph': {
  2671. const line = this.createElement('div', 'sidebar-item-value-line-link');
  2672. line.innerHTML = value.name || '&nbsp;';
  2673. line.addEventListener('click', () => this.emit('activate', value));
  2674. this.add(line);
  2675. break;
  2676. }
  2677. case 'function': {
  2678. const line = this.createElement('div', 'sidebar-item-value-line-link');
  2679. line.innerHTML = value.type.name;
  2680. line.addEventListener('click', () => this.emit('activate', value));
  2681. this.add(line);
  2682. break;
  2683. }
  2684. case 'object[]': {
  2685. for (const obj of argument.value) {
  2686. const line = this.createElement('div', 'sidebar-item-value-line');
  2687. line.innerHTML = obj.type.name;
  2688. this.add(line);
  2689. }
  2690. break;
  2691. }
  2692. default: {
  2693. let content = new view.Formatter(value, type).toString();
  2694. if (content && content.length > 1000) {
  2695. content = `${content.substring(0, 1000)}\u2026`;
  2696. }
  2697. if (content && typeof content === 'string') {
  2698. content = content.split('<').join('&lt;').split('>').join('&gt;');
  2699. }
  2700. const line = this.createElement('div', 'sidebar-item-value-line');
  2701. line.innerHTML = content ? content : '&nbsp;';
  2702. this.add(line);
  2703. }
  2704. }
  2705. } catch (error) {
  2706. super.error(error, false);
  2707. this._info('ERROR', error.message);
  2708. }
  2709. }
  2710. expand() {
  2711. try {
  2712. const type = this._argument.type;
  2713. const value = this._argument.value;
  2714. const content = type === 'tensor' && value && value.type ? value.type.toString() : this._argument.type;
  2715. const line = this.createElement('div', 'sidebar-item-value-line-border');
  2716. line.innerHTML = `type: <code><b>${content}</b></code>`;
  2717. this.add(line);
  2718. const description = this._argument.description;
  2719. if (description) {
  2720. const line = this.createElement('div', 'sidebar-item-value-line-border');
  2721. line.innerHTML = description;
  2722. this.add(line);
  2723. }
  2724. } catch (error) {
  2725. super.error(error, false);
  2726. this._info('ERROR', error.message);
  2727. }
  2728. }
  2729. _info(name, value) {
  2730. const line = this.createElement('div');
  2731. line.innerHTML = `<b>${name}:</b> ${value}`;
  2732. this._add(line);
  2733. }
  2734. _add(child) {
  2735. child.className = this._first === false ? 'sidebar-item-value-line-border' : 'sidebar-item-value-line';
  2736. this.add(child);
  2737. this._first = false;
  2738. }
  2739. };
  2740. view.ValueView = class extends view.Expander {
  2741. constructor(context, value, source) {
  2742. super(context);
  2743. this._value = value;
  2744. try {
  2745. const type = this._value.type;
  2746. const initializer = this._value.initializer;
  2747. const quantization = this._value.quantization;
  2748. const location = this._value.location !== undefined;
  2749. if (initializer) {
  2750. this.element.classList.add('sidebar-item-value-content');
  2751. }
  2752. if (type || initializer || quantization || location || source === 'attribute') {
  2753. this.enable();
  2754. }
  2755. if (initializer && source !== 'attribute') {
  2756. const element = this.createElement('div', 'sidebar-item-value-button');
  2757. element.classList.add('sidebar-item-value-button-tool');
  2758. element.setAttribute('title', 'Show Tensor');
  2759. element.innerHTML = `<svg class='sidebar-find-content-icon'><use href="#sidebar-icon-weight"></use></svg>`;
  2760. element.addEventListener('pointerenter', () => this.emit('focus', this._value));
  2761. element.addEventListener('pointerleave', () => this.emit('blur', this._value));
  2762. element.style.cursor = 'pointer';
  2763. element.addEventListener('click', () => this.emit('activate', this._value));
  2764. this.control(element);
  2765. }
  2766. const name = this._value.name ? this._value.name.split('\n').shift() : ''; // custom argument id
  2767. this._hasId = name && source !== 'attribute' ? true : false;
  2768. this._hasCategory = initializer && initializer.category && source !== 'attribute' ? true : false;
  2769. if (this._hasId || (!this._hasCategory && !type && source !== 'attribute')) {
  2770. this._hasId = true;
  2771. const element = this.createElement('div', 'sidebar-item-value-line');
  2772. if (typeof name !== 'string') {
  2773. throw new Error(`Invalid value identifier '${JSON.stringify(name)}'.`);
  2774. }
  2775. element.innerHTML = `<span class='sidebar-item-value-line-content'>name: <b>${name || ' '}</b></span>`;
  2776. element.addEventListener('pointerenter', () => this.emit('focus', this._value));
  2777. element.addEventListener('pointerleave', () => this.emit('blur', this._value));
  2778. element.style.cursor = 'pointer';
  2779. element.addEventListener('click', () => this.emit('activate', this._value));
  2780. this._add(element);
  2781. } else if (this._hasCategory) {
  2782. this._bold('category', initializer.category);
  2783. } else if (type) {
  2784. const value = type.toString().split('<').join('&lt;').split('>').join('&gt;');
  2785. this._code('tensor', value);
  2786. }
  2787. } catch (error) {
  2788. super.error(error, false);
  2789. this._info('ERROR', error.message);
  2790. }
  2791. }
  2792. render() {
  2793. return [this.element];
  2794. }
  2795. expand() {
  2796. try {
  2797. const initializer = this._value.initializer;
  2798. if (this._hasId && this._hasCategory) {
  2799. this._bold('category', initializer.category);
  2800. }
  2801. let type = null;
  2802. let denotation = null;
  2803. if (this._value.type) {
  2804. type = this._value.type.toString();
  2805. denotation = this._value.type.denotation || null;
  2806. }
  2807. if (type && (this._hasId || this._hasCategory)) {
  2808. this._code('tensor', type.split('<').join('&lt;').split('>').join('&gt;'));
  2809. }
  2810. if (denotation) {
  2811. this._code('denotation', denotation);
  2812. }
  2813. const description = this._value.description;
  2814. if (description) {
  2815. const line = this.createElement('div', 'sidebar-item-value-line-border');
  2816. line.innerHTML = description;
  2817. this.add(line);
  2818. }
  2819. const identifier = this._value.identifier;
  2820. if (identifier !== undefined) {
  2821. this._bold('identifier', identifier);
  2822. }
  2823. const layout = this._value.type ? this._value.type.layout : null;
  2824. if (layout) {
  2825. this._bold('layout', layout.replace('.', ' '));
  2826. }
  2827. const quantization = this._value.quantization;
  2828. if (quantization) {
  2829. if (typeof quantization.type !== 'string') {
  2830. throw new view.Error('Unsupported quantization value.');
  2831. }
  2832. const value = new view.Quantization(quantization).toString();
  2833. if (quantization.type && (quantization.type !== 'linear' || (value && value !== 'q'))) {
  2834. const line = this.createElement('div', 'sidebar-item-value-line-border');
  2835. const content = [
  2836. `<span class='sidebar-item-value-line-content'>quantization: <b>${quantization.type}</b></span>`
  2837. ];
  2838. if (value) {
  2839. content.push(`<pre style='margin: 4px 0 2px 0'>${value}</pre>`);
  2840. }
  2841. line.innerHTML = content.join('');
  2842. this._add(line);
  2843. }
  2844. }
  2845. if (initializer) {
  2846. if (initializer.location) {
  2847. this._bold('location', initializer.location);
  2848. }
  2849. const stride = initializer.stride;
  2850. if (Array.isArray(stride) && stride.length > 0) {
  2851. this._code('stride', stride.join(','));
  2852. }
  2853. const tensor = new view.TensorView(this._view, initializer);
  2854. const content = tensor.content;
  2855. const line = this.createElement('div', 'sidebar-item-value-line-border');
  2856. line.appendChild(content);
  2857. this._add(line);
  2858. }
  2859. } catch (error) {
  2860. super.error(error, false);
  2861. this._info('ERROR', error.message);
  2862. }
  2863. }
  2864. _bold(name, value) {
  2865. const line = this.createElement('div');
  2866. line.innerHTML = `${name}: <b>${value}</b>`;
  2867. this._add(line);
  2868. }
  2869. _code(name, value) {
  2870. const line = this.createElement('div');
  2871. line.innerHTML = `${name}: <code><b>${value}</b></code>`;
  2872. this._add(line);
  2873. }
  2874. _info(name, value) {
  2875. const line = this.createElement('div');
  2876. line.innerHTML = `<b>${name}:</b> ${value}`;
  2877. this._add(line);
  2878. }
  2879. _add(child) {
  2880. child.className = this._first === false ? 'sidebar-item-value-line-border' : 'sidebar-item-value-line';
  2881. this.add(child);
  2882. this._first = false;
  2883. }
  2884. };
  2885. view.TensorView = class extends view.Expander {
  2886. constructor(context, value, tensor) {
  2887. super(context);
  2888. this._value = value;
  2889. this._tensor = tensor || new base.Tensor(value);
  2890. }
  2891. render() {
  2892. if (!this._button) {
  2893. this.enable();
  2894. this._button = this.createElement('div', 'sidebar-item-value-button');
  2895. this._button.setAttribute('style', 'float: left;');
  2896. this._button.innerHTML = `<svg class='sidebar-find-content-icon'><use href="#sidebar-icon-weight"></use></svg>`;
  2897. this._button.addEventListener('click', () => this.toggle());
  2898. this.control(this._button);
  2899. const line = this.createElement('div', 'sidebar-item-value-line');
  2900. line.classList.add('sidebar-item-disable-select');
  2901. line.innerHTML = '&nbsp';
  2902. this.element.appendChild(line);
  2903. }
  2904. return super.render();
  2905. }
  2906. expand() {
  2907. try {
  2908. const content = this.content;
  2909. const container = this.createElement('div', 'sidebar-item-value-line-border');
  2910. container.appendChild(content);
  2911. this.element.appendChild(container);
  2912. } catch (error) {
  2913. this.error(error, false);
  2914. }
  2915. }
  2916. get content() {
  2917. const content = this.createElement('pre');
  2918. const value = this._value;
  2919. const tensor = this._tensor;
  2920. if (tensor.encoding !== '<' && tensor.encoding !== '>' && tensor.encoding !== '|') {
  2921. content.innerHTML = `Tensor encoding '${tensor.layout}' is not implemented.`;
  2922. } else if (tensor.layout && (tensor.layout !== 'sparse' && tensor.layout !== 'sparse.coo')) {
  2923. content.innerHTML = `Tensor layout '${tensor.layout}' is not implemented.`;
  2924. } else if (tensor.empty) {
  2925. content.innerHTML = 'Tensor data is empty.';
  2926. } else if (tensor.type && tensor.type.dataType === '?') {
  2927. content.innerHTML = 'Tensor data type is not defined.';
  2928. } else if (tensor.type && !tensor.type.shape) {
  2929. content.innerHTML = 'Tensor shape is not defined.';
  2930. } else {
  2931. content.innerHTML = tensor.toString();
  2932. if (this._host.save && value.type.shape && value.type.shape.dimensions && value.type.shape.dimensions.length > 0) {
  2933. this._saveButton = this.createElement('div', 'sidebar-item-value-button');
  2934. this._saveButton.classList.add('sidebar-item-value-button-context');
  2935. this._saveButton.setAttribute('style', 'float: right;');
  2936. this._saveButton.innerHTML = '&#x1F4BE;';
  2937. this._saveButton.addEventListener('click', async () => {
  2938. await this.export();
  2939. });
  2940. content.insertBefore(this._saveButton, content.firstChild);
  2941. }
  2942. }
  2943. return content;
  2944. }
  2945. error(error, fatal) {
  2946. super.error(error, fatal);
  2947. const element = this.createElement('div', 'sidebar-item-value-line');
  2948. element.innerHTML = `<b>ERROR:</b> ${error.message}`;
  2949. this.element.appendChild(element);
  2950. }
  2951. async export() {
  2952. const tensor = this._tensor;
  2953. const defaultPath = tensor.name ? tensor.name.split('/').join('_').split(':').join('_').split('.').join('_') : 'tensor';
  2954. const file = await this._host.save('NumPy Array', 'npy', defaultPath);
  2955. if (file) {
  2956. try {
  2957. let data_type = '?';
  2958. switch (tensor.type.dataType) {
  2959. case 'boolean': data_type = 'bool'; break;
  2960. case 'bfloat16': data_type = 'float32'; break;
  2961. case 'float8e5m2': data_type = 'float16'; break;
  2962. case 'float8e5m2fnuz': data_type = 'float16'; break;
  2963. case 'float8e4m3fn': data_type = 'float16'; break;
  2964. case 'float8e4m3fnuz': data_type = 'float16'; break;
  2965. case 'int4': data_type = 'int8'; break;
  2966. default: data_type = tensor.type.dataType; break;
  2967. }
  2968. const execution = new python.Execution();
  2969. const bytes = execution.invoke('io.BytesIO', []);
  2970. const dtype = execution.invoke('numpy.dtype', [data_type]);
  2971. const array = execution.invoke('numpy.asarray', [tensor.value, dtype]);
  2972. execution.invoke('numpy.save', [bytes, array]);
  2973. bytes.seek(0);
  2974. const blob = new Blob([bytes.read()], { type: 'application/octet-stream' });
  2975. await this._host.export(file, blob);
  2976. } catch (error) {
  2977. this.error(error, 'Error saving NumPy tensor.', null);
  2978. }
  2979. }
  2980. }
  2981. };
  2982. view.NodeView = class extends view.Expander {
  2983. constructor(context, node) {
  2984. super(context);
  2985. this._node = node;
  2986. const name = node.name;
  2987. const type = node.type ? node.type.name : '';
  2988. if (name && type) {
  2989. this.enable();
  2990. }
  2991. if (type) {
  2992. const type = node.type.name;
  2993. const element = this.createElement('div', 'sidebar-item-value-line');
  2994. element.innerHTML = `<span class='sidebar-item-value-line-content'>node: <b>${type || ' '}</b></span>`;
  2995. element.addEventListener('pointerenter', () => this.emit('focus', this._node));
  2996. element.addEventListener('pointerleave', () => this.emit('blur', this._node));
  2997. element.addEventListener('click', () => this.emit('activate', this._node));
  2998. element.style.cursor = 'pointer';
  2999. this.element.appendChild(element);
  3000. } else {
  3001. const element = this.createElement('div', 'sidebar-item-value-line');
  3002. element.innerHTML = `<span class='sidebar-item-value-line-content'>name: <b>${name || ' '}</b></span>`;
  3003. element.addEventListener('pointerenter', () => this.emit('focus', this._node));
  3004. element.addEventListener('pointerleave', () => this.emit('blur', this._node));
  3005. element.addEventListener('click', () => this.emit('activate', this._node));
  3006. element.style.cursor = 'pointer';
  3007. this.element.appendChild(element);
  3008. }
  3009. }
  3010. expand() {
  3011. const name = this._node.name;
  3012. const element = this.createElement('div', 'sidebar-item-value-line-border');
  3013. element.innerHTML = `<span class='sidebar-item-value-line-content'>name: <b>${name}</b></span>`;
  3014. element.addEventListener('pointerenter', () => this.emit('focus', this._node));
  3015. element.addEventListener('pointerleave', () => this.emit('blur', this._node));
  3016. element.addEventListener('click', () => this.emit('activate', this._node));
  3017. element.style.cursor = 'pointer';
  3018. this.element.appendChild(element);
  3019. }
  3020. };
  3021. view.NodeListView = class extends view.Control {
  3022. constructor(context, list) {
  3023. super(context);
  3024. this._elements = [];
  3025. for (const node of list) {
  3026. const item = new view.NodeView(this._view, node);
  3027. item.on('focus', (sender, value) => this.emit('focus', value));
  3028. item.on('blur', (sender, value) => this.emit('blur', value));
  3029. item.on('activate', (sender, value) => this.emit('activate', value));
  3030. item.on('deactivate', (sender, value) => this.emit('deactivate', value));
  3031. item.on('select', (sender, value) => this.emit('select', value));
  3032. item.toggle();
  3033. for (const element of item.render()) {
  3034. this._elements.push(element);
  3035. }
  3036. }
  3037. }
  3038. render() {
  3039. return this._elements;
  3040. }
  3041. };
  3042. view.ConnectionSidebar = class extends view.ObjectSidebar {
  3043. constructor(context, value, from, to) {
  3044. super(context);
  3045. this._value = value;
  3046. this._from = from;
  3047. this._to = to;
  3048. }
  3049. render() {
  3050. const value = this._value;
  3051. const from = this._from;
  3052. const to = this._to;
  3053. const [name] = value.name.split('\n');
  3054. this.addProperty('name', name);
  3055. if (value.type) {
  3056. const item = new view.ValueView(this._view, value);
  3057. this.addEntry('type', item);
  3058. item.toggle();
  3059. }
  3060. if (from) {
  3061. this.addHeader('Inputs');
  3062. this.addNodeList('from', [from]);
  3063. }
  3064. if (Array.isArray(to) && to.length > 0) {
  3065. this.addHeader('Outputs');
  3066. this.addNodeList('to', to);
  3067. }
  3068. }
  3069. addNodeList(name, list) {
  3070. const entry = new view.NodeListView(this._view, list);
  3071. entry.on('focus', (sender, value) => {
  3072. this.emit('focus', value);
  3073. this._focused = this._focused || new Set();
  3074. this._focused.add(value);
  3075. });
  3076. entry.on('blur', (sender, value) => {
  3077. this.emit('blur', value);
  3078. this._focused = this._focused || new Set();
  3079. this._focused.delete(value);
  3080. });
  3081. entry.on('select', (sender, value) => this.emit('select', value));
  3082. entry.on('activate', (sender, value) => this.emit('activate', value));
  3083. this.addEntry(name, entry);
  3084. }
  3085. activate() {
  3086. this.emit('select', this._value);
  3087. }
  3088. deactivate() {
  3089. this.emit('select', null);
  3090. if (this._focused) {
  3091. for (const value of this._focused) {
  3092. this.emit('blur', value);
  3093. }
  3094. this._focused.clear();
  3095. }
  3096. }
  3097. };
  3098. view.TensorSidebar = class extends view.ObjectSidebar {
  3099. constructor(context, value) {
  3100. super(context);
  3101. this._value = value;
  3102. this._tensor = new base.Tensor(value.value[0].initializer);
  3103. }
  3104. render() {
  3105. const [value] = this._value.value;
  3106. const tensor = value.initializer;
  3107. const name = tensor && tensor.name ? tensor.name : value.name.split('\n')[0];
  3108. if (name) {
  3109. this.addProperty('name', name);
  3110. }
  3111. if (tensor) {
  3112. const category = tensor.category;
  3113. if (category) {
  3114. this.addProperty('category', category);
  3115. }
  3116. const description = tensor.description;
  3117. if (description) {
  3118. this.addProperty('description', description);
  3119. }
  3120. const type = tensor.type;
  3121. if (type) {
  3122. const dataType = type.dataType;
  3123. this.addProperty('type', `${dataType}`, 'code');
  3124. const shape = type.shape && Array.isArray(type.shape.dimensions) ? type.shape.dimensions.toString(', ') : '?';
  3125. this.addProperty('shape', `${shape}`, 'code');
  3126. const denotation = type.denotation;
  3127. if (denotation) {
  3128. this.addProperty('denotation', denotation, 'code');
  3129. }
  3130. const layout = type.layout;
  3131. if (layout) {
  3132. this.addProperty('layout', layout.replace('.', ' '));
  3133. }
  3134. }
  3135. const location = tensor.location;
  3136. if (location) {
  3137. this.addProperty('location', tensor.location);
  3138. }
  3139. const stride = tensor.stride;
  3140. if (Array.isArray(stride) && stride.length > 0) {
  3141. this.addProperty('stride', stride.join(','), 'code');
  3142. }
  3143. const value = new view.TensorView(this._view, tensor, this._tensor);
  3144. this.addEntry('value', value);
  3145. const metadata = tensor.metadata;
  3146. if (Array.isArray(metadata) && metadata.length > 0) {
  3147. this.addHeader('Metadata');
  3148. for (const argument of tensor.metadata) {
  3149. this.addProperty(argument.name, argument.value);
  3150. }
  3151. }
  3152. }
  3153. // Metrics
  3154. if (value.initializer) {
  3155. if (!this._tensor.empty) {
  3156. if (!this._metrics) {
  3157. this._metrics = new metrics.Tensor(this._tensor);
  3158. }
  3159. if (this._metrics.metrics.length > 0) {
  3160. this.addHeader('Metrics');
  3161. for (const metric of this._metrics.metrics) {
  3162. const value = metric.type === 'percentage' ? `${(metric.value * 100).toFixed(1)}%` : metric.value;
  3163. this.addProperty(metric.name, [value]);
  3164. }
  3165. }
  3166. }
  3167. }
  3168. }
  3169. activate() {
  3170. this.emit('select', this._value);
  3171. }
  3172. deactivate() {
  3173. this.emit('select', null);
  3174. }
  3175. };
  3176. view.ModelSidebar = class extends view.ObjectSidebar {
  3177. constructor(context, model, graph, signature) {
  3178. super(context);
  3179. this._model = model;
  3180. this._graph = graph;
  3181. this._signature = signature;
  3182. }
  3183. render() {
  3184. const model = this._model;
  3185. const graph = this._graph;
  3186. const signature = this._signature;
  3187. if (model.format) {
  3188. this.addProperty('format', model.format);
  3189. }
  3190. if (model.producer) {
  3191. this.addProperty('producer', model.producer);
  3192. }
  3193. if (model.name) {
  3194. this.addProperty('name', model.name);
  3195. }
  3196. if (model.version) {
  3197. this.addProperty('version', model.version);
  3198. }
  3199. if (model.description) {
  3200. this.addProperty('description', model.description);
  3201. }
  3202. if (model.domain) {
  3203. this.addProperty('domain', model.domain);
  3204. }
  3205. if (model.imports) {
  3206. this.addProperty('imports', model.imports);
  3207. }
  3208. if (model.runtime) {
  3209. this.addProperty('runtime', model.runtime);
  3210. }
  3211. if (model.source) {
  3212. this.addProperty('source', model.source);
  3213. }
  3214. const graphs = Array.isArray(model.graphs) ? model.graphs : [];
  3215. if (graphs.length === 1 && graphs[0].name) {
  3216. this.addProperty('graph', graphs[0].name);
  3217. } else if (graphs.length > 1) {
  3218. const entries = new Map();
  3219. for (const graph of model.graphs) {
  3220. entries.set(graph.name, graph);
  3221. }
  3222. const selector = new view.SelectView(this._view, entries, graph);
  3223. selector.on('change', (sender, data) => this.emit('update-active-graph', data));
  3224. this.addEntry('graph', selector);
  3225. }
  3226. if (graph && Array.isArray(graph.signatures) && graph.signatures.length > 0) {
  3227. const entries = new Map();
  3228. entries.set('', graph);
  3229. for (const signature of graph.signatures) {
  3230. entries.set(signature.name, signature);
  3231. }
  3232. const selector = new view.SelectView(this._view, entries, signature || graph);
  3233. selector.on('change', (sender, data) => this.emit('update-active-graph-signature', data));
  3234. this.addEntry('signature', selector);
  3235. }
  3236. const metadata = model.metadata;
  3237. if (Array.isArray(metadata) && metadata.length > 0) {
  3238. this.addHeader('Metadata');
  3239. for (const argument of metadata) {
  3240. this.addProperty(argument.name, argument.value);
  3241. }
  3242. }
  3243. if (graph) {
  3244. if (graph.version) {
  3245. this.addProperty('version', graph.version);
  3246. }
  3247. if (graph.type) {
  3248. this.addProperty('type', graph.type);
  3249. }
  3250. if (graph.tags) {
  3251. this.addProperty('tags', graph.tags);
  3252. }
  3253. if (graph.description) {
  3254. this.addProperty('description', graph.description);
  3255. }
  3256. const inputs = signature ? signature.inputs : graph.inputs;
  3257. const outputs = signature ? signature.outputs : graph.outputs;
  3258. if (Array.isArray(inputs) && inputs.length > 0) {
  3259. this.addHeader('Inputs');
  3260. for (const input of inputs) {
  3261. this.addArgument(input.name, input);
  3262. }
  3263. }
  3264. if (Array.isArray(outputs) && outputs.length > 0) {
  3265. this.addHeader('Outputs');
  3266. for (const output of outputs) {
  3267. this.addArgument(output.name, output);
  3268. }
  3269. }
  3270. const metadata = graph.metadata;
  3271. if (Array.isArray(metadata) && metadata.length > 0) {
  3272. this.addHeader('Metadata');
  3273. for (const argument of metadata) {
  3274. this.addProperty(argument.name, argument.value);
  3275. }
  3276. }
  3277. }
  3278. }
  3279. addArgument(name, argument) {
  3280. const value = new view.ArgumentView(this._view, argument);
  3281. value.on('focus', (sender, value) => this.emit('focus', value));
  3282. value.on('blur', (sender, value) => this.emit('blur', value));
  3283. value.on('activate', (sender, value) => this.emit('activate', value));
  3284. value.on('deactivate', (sender, value) => this.emit('deactivate', value));
  3285. value.on('select', (sender, value) => this.emit('select', value));
  3286. value.toggle();
  3287. this.addEntry(name, value);
  3288. }
  3289. };
  3290. view.DocumentationSidebar = class extends view.Control {
  3291. constructor(context, type) {
  3292. super(context);
  3293. this._type = type;
  3294. }
  3295. render() {
  3296. if (!this.element) {
  3297. this.element = this.createElement('div', 'sidebar-documentation');
  3298. const type = view.Documentation.open(this._type);
  3299. this._append(this.element, 'h1', type.name);
  3300. if (type.summary) {
  3301. this._append(this.element, 'p', type.summary);
  3302. }
  3303. if (type.description) {
  3304. this._append(this.element, 'p', type.description);
  3305. }
  3306. if (Array.isArray(type.attributes) && type.attributes.length > 0) {
  3307. this._append(this.element, 'h2', 'Attributes');
  3308. const attributes = this._append(this.element, 'dl');
  3309. for (const attribute of type.attributes) {
  3310. this._append(attributes, 'dt', attribute.name + (attribute.type ? `: <tt>${attribute.type}</tt>` : ''));
  3311. this._append(attributes, 'dd', attribute.description);
  3312. }
  3313. this.element.appendChild(attributes);
  3314. }
  3315. if (Array.isArray(type.inputs) && type.inputs.length > 0) {
  3316. this._append(this.element, 'h2', `Inputs${type.inputs_range ? ` (${type.inputs_range})` : ''}`);
  3317. const inputs = this._append(this.element, 'dl');
  3318. for (const input of type.inputs) {
  3319. this._append(inputs, 'dt', input.name + (input.type ? `: <tt>${input.type}</tt>` : '') + (input.option ? ` (${input.option})` : ''));
  3320. this._append(inputs, 'dd', input.description);
  3321. }
  3322. }
  3323. if (Array.isArray(type.outputs) && type.outputs.length > 0) {
  3324. this._append(this.element, 'h2', `Outputs${type.outputs_range ? ` (${type.outputs_range})` : ''}`);
  3325. const outputs = this._append(this.element, 'dl');
  3326. for (const output of type.outputs) {
  3327. this._append(outputs, 'dt', output.name + (output.type ? `: <tt>${output.type}</tt>` : '') + (output.option ? ` (${output.option})` : ''));
  3328. this._append(outputs, 'dd', output.description);
  3329. }
  3330. }
  3331. if (Array.isArray(type.type_constraints) && type.type_constraints.length > 0) {
  3332. this._append(this.element, 'h2', 'Type Constraints');
  3333. const type_constraints = this._append(this.element, 'dl');
  3334. for (const type_constraint of type.type_constraints) {
  3335. this._append(type_constraints, 'dt', `${type_constraint.type_param_str}: ${type_constraint.allowed_type_strs.map((item) => `<tt>${item}</tt>`).join(', ')}`);
  3336. this._append(type_constraints, 'dd', type_constraint.description);
  3337. }
  3338. }
  3339. if (Array.isArray(type.examples) && type.examples.length > 0) {
  3340. this._append(this.element, 'h2', 'Examples');
  3341. for (const example of type.examples) {
  3342. this._append(this.element, 'h3', example.summary);
  3343. this._append(this.element, 'pre', example.code);
  3344. }
  3345. }
  3346. if (Array.isArray(type.references) && type.references.length > 0) {
  3347. this._append(this.element, 'h2', 'References');
  3348. const references = this._append(this.element, 'ul');
  3349. for (const reference of type.references) {
  3350. this._append(references, 'li', reference.description);
  3351. }
  3352. }
  3353. if (this._host.type === 'Electron') {
  3354. this.element.addEventListener('click', (e) => {
  3355. if (e.target && e.target.href) {
  3356. const url = e.target.href;
  3357. if (url.startsWith('http://') || url.startsWith('https://')) {
  3358. e.preventDefault();
  3359. this.emit('navigate', { link: url });
  3360. }
  3361. }
  3362. });
  3363. }
  3364. }
  3365. }
  3366. _append(parent, type, content) {
  3367. const element = this.createElement(type);
  3368. if (content) {
  3369. element.innerHTML = content;
  3370. }
  3371. parent.appendChild(element);
  3372. return element;
  3373. }
  3374. error(error, fatal) {
  3375. super.error(error, fatal);
  3376. const element = this.createElement('span');
  3377. element.innerHTML = `<b>ERROR:</b> ${error.message}`;
  3378. this.element.appendChild(element);
  3379. }
  3380. };
  3381. view.FindSidebar = class extends view.Control {
  3382. constructor(context, graph, signature) {
  3383. super(context);
  3384. this._graph = graph;
  3385. this._signature = signature;
  3386. this._state = {
  3387. query: '',
  3388. node: true,
  3389. connection: true,
  3390. weight: true
  3391. };
  3392. this._toggles = {
  3393. node: { hide: 'Hide Nodes', show: 'Show Nodes' },
  3394. connection: { hide: 'Hide Connections', show: 'Show Connections' },
  3395. weight: { hide: 'Hide Weights', show: 'Show Weights' }
  3396. };
  3397. }
  3398. on(event, callback) {
  3399. this._events = this._events || {};
  3400. this._events[event] = this._events[event] || [];
  3401. this._events[event].push(callback);
  3402. }
  3403. emit(event, data) {
  3404. if (this._events && this._events[event]) {
  3405. for (const callback of this._events[event]) {
  3406. callback(this, data);
  3407. }
  3408. }
  3409. }
  3410. focus(state) {
  3411. this._state = state || this._state;
  3412. this._query.focus();
  3413. this._query.value = '';
  3414. this._query.value = this._state.query;
  3415. for (const [name, toggle] of Object.entries(this._toggles)) {
  3416. toggle.checkbox.checked = this._state[name];
  3417. toggle.element.setAttribute('title', this._state[name] ? toggle.hide : toggle.show);
  3418. }
  3419. this._update();
  3420. }
  3421. _clear() {
  3422. for (const identifier of this._focused) {
  3423. this._blur(identifier);
  3424. }
  3425. this._focused.clear();
  3426. this._table.clear();
  3427. this._edges.clear();
  3428. this._index = 0;
  3429. const unquote = this._state.query.match(new RegExp(/^'(.*)'|"(.*)"$/));
  3430. if (unquote) {
  3431. this._exact = true;
  3432. const term = unquote[1] || unquote[2];
  3433. this._terms = [term];
  3434. } else {
  3435. this._exact = false;
  3436. this._terms = this._state.query.trim().toLowerCase().split(' ').map((term) => term.trim()).filter((term) => term.length > 0);
  3437. }
  3438. }
  3439. _term(value) {
  3440. if (this._exact) {
  3441. return value === this._terms[0];
  3442. }
  3443. value = value.toLowerCase();
  3444. return this._terms.every((term) => value.indexOf(term) !== -1);
  3445. }
  3446. _value(value) {
  3447. if (this._terms.length === 0) {
  3448. return true;
  3449. }
  3450. if (value.name && this._term(value.name.split('\n').shift())) {
  3451. return true;
  3452. }
  3453. if (value.identifier && this._term(value.identifier)) {
  3454. return true;
  3455. }
  3456. if (value.type && !this._exact) {
  3457. for (const term of this._terms) {
  3458. if (value.type.dataType && term === value.type.dataType.toLowerCase()) {
  3459. return true;
  3460. }
  3461. if (value.type.shape) {
  3462. if (term === value.type.shape.toString().toLowerCase()) {
  3463. return true;
  3464. }
  3465. if (value.type.shape && Array.isArray(value.type.shape.dimensions)) {
  3466. const dimensions = value.type.shape.dimensions.map((dimension) => dimension ? dimension.toString().toLowerCase() : '');
  3467. if (term === dimensions.join(',')) {
  3468. return true;
  3469. }
  3470. if (dimensions.some((dimension) => term === dimension)) {
  3471. return true;
  3472. }
  3473. }
  3474. }
  3475. }
  3476. }
  3477. return false;
  3478. }
  3479. _edge(value) {
  3480. if (value.name && !this._edges.has(value.name) && this._value(value)) {
  3481. const content = `${value.name.split('\n').shift()}`;
  3482. this._add(value, content, 'connection'); // split custom argument id
  3483. this._edges.add(value.name);
  3484. }
  3485. }
  3486. _node(node) {
  3487. if (this._state.connection) {
  3488. for (const input of node.inputs) {
  3489. if (!input.type || input.type.endsWith('*')) {
  3490. for (const value of input.value) {
  3491. if (!value.initializer) {
  3492. this._edge(value);
  3493. }
  3494. }
  3495. }
  3496. }
  3497. }
  3498. if (this._state.node) {
  3499. const name = node.name;
  3500. const type = node.type.name;
  3501. const identifier = node.identifier;
  3502. if ((name && this._term(name)) || (type && this._term(type)) || (identifier && this._term(identifier))) {
  3503. const content = `${name || `[${type}]`}`;
  3504. this._add(node, content, 'node');
  3505. }
  3506. }
  3507. if (this._state.weight) {
  3508. for (const argument of node.inputs) {
  3509. if (!argument.type || argument.type.endsWith('*')) {
  3510. for (const value of argument.value) {
  3511. if (value.initializer && this._value(value)) {
  3512. let content = null;
  3513. if (value.name) {
  3514. content = `${value.name.split('\n').shift()}`; // split custom argument id
  3515. } else if (value.type && value.type.shape && Array.isArray(value.type.shape.dimensions) && value.type.shape.dimensions.length > 0) {
  3516. content = `${value.type.shape.dimensions.map((d) => (d !== null && d !== undefined) ? d : '?').join('\u00D7')}`;
  3517. }
  3518. if (content) {
  3519. const target = argument.value.length === 1 ? argument : node;
  3520. this._add(target, content, 'weight');
  3521. }
  3522. }
  3523. }
  3524. } else if (argument.type === 'object') {
  3525. this._node(argument.value);
  3526. } else if (argument.type === 'object[]') {
  3527. for (const value of argument.value) {
  3528. this._node(value);
  3529. }
  3530. }
  3531. }
  3532. }
  3533. }
  3534. _add(value, content, icon) {
  3535. const key = this._index.toString();
  3536. this._index++;
  3537. this._table.set(key, value);
  3538. const element = this.createElement('li');
  3539. element.innerHTML = `<svg class='sidebar-find-content-icon'><use href="#sidebar-icon-${icon}"></use></svg>`;
  3540. const text = this.createElement('span');
  3541. text.innerText = content;
  3542. element.appendChild(text);
  3543. element.setAttribute('data', key);
  3544. element.addEventListener('pointerover', (e) => {
  3545. const identifier = e.target.getAttribute('data');
  3546. this._focus(identifier);
  3547. });
  3548. element.addEventListener('pointerleave', (e) => {
  3549. const identifier = e.target.getAttribute('data');
  3550. this._blur(identifier);
  3551. });
  3552. this._content.appendChild(element);
  3553. }
  3554. _focus(identifier) {
  3555. if (this._table.has(identifier)) {
  3556. this.emit('focus', this._table.get(identifier));
  3557. this._focused.add(identifier);
  3558. }
  3559. }
  3560. _blur(identifier) {
  3561. if (this._table.has(identifier)) {
  3562. this.emit('blur', this._table.get(identifier));
  3563. this._focused.delete(identifier);
  3564. }
  3565. }
  3566. _update() {
  3567. this._content.innerHTML = '';
  3568. try {
  3569. this._clear();
  3570. const inputs = this._signature ? this._signature.inputs : this._graph.inputs;
  3571. if (this._state.connection) {
  3572. for (const input of inputs) {
  3573. for (const value of input.value) {
  3574. this._edge(value);
  3575. }
  3576. }
  3577. }
  3578. for (const node of this._graph.nodes) {
  3579. this._node(node);
  3580. }
  3581. if (this._state.connection) {
  3582. const outputs = this._signature ? this._signature.outputs : this._graph.inputs;
  3583. for (const output of outputs) {
  3584. if (!output.type || output.type.endsWith('*')) {
  3585. for (const value of output.value) {
  3586. this._edge(value);
  3587. }
  3588. }
  3589. }
  3590. }
  3591. } catch (error) {
  3592. this.error(error, false);
  3593. }
  3594. }
  3595. render() {
  3596. this._table = new Map();
  3597. this._focused = new Set();
  3598. this._edges = new Set();
  3599. this._search = this.createElement('div', 'sidebar-find-search');
  3600. this._query = this.createElement('input', 'sidebar-find-query');
  3601. this._search.appendChild(this._query);
  3602. this._content = this.createElement('ol', 'sidebar-find-content');
  3603. this._elements = [this._query, this._content];
  3604. this._query.setAttribute('id', 'search');
  3605. this._query.setAttribute('type', 'text');
  3606. this._query.setAttribute('spellcheck', 'false');
  3607. this._query.setAttribute('placeholder', 'Search');
  3608. this._query.addEventListener('input', (e) => {
  3609. this._state.query = e.target.value;
  3610. this.emit('state-changed', this._state);
  3611. this._update();
  3612. });
  3613. this._query.addEventListener('keydown', (e) => {
  3614. if (e.keyCode === 0x08 && !e.altKey && !e.ctrlKey && !e.shiftKey && !e.metaKey) {
  3615. e.stopPropagation();
  3616. }
  3617. });
  3618. for (const [name, toggle] of Object.entries(this._toggles)) {
  3619. toggle.element = this.createElement('label', 'sidebar-find-toggle');
  3620. toggle.element.innerHTML = `<svg class='sidebar-find-toggle-icon'><use href="#sidebar-icon-${name}"></use></svg>`;
  3621. toggle.element.setAttribute('title', this._state[name] ? toggle.hide : toggle.show);
  3622. toggle.checkbox = this.createElement('input');
  3623. toggle.checkbox.setAttribute('type', 'checkbox');
  3624. toggle.checkbox.setAttribute('data', name);
  3625. toggle.checkbox.addEventListener('change', (e) => {
  3626. const name = e.target.getAttribute('data');
  3627. this._state[name] = e.target.checked;
  3628. const toggle = this._toggles[name];
  3629. toggle.element.setAttribute('title', e.target.checked ? toggle.hide : toggle.show);
  3630. this.emit('state-changed', this._state);
  3631. this._update();
  3632. });
  3633. toggle.element.insertBefore(toggle.checkbox, toggle.element.firstChild);
  3634. this._search.appendChild(toggle.element);
  3635. }
  3636. this._content.addEventListener('click', (e) => {
  3637. const name = e.target.getAttribute('data');
  3638. if (this._table.has(name)) {
  3639. this.emit('select', this._table.get(name));
  3640. }
  3641. });
  3642. this._content.addEventListener('dblclick', (e) => {
  3643. const name = e.target.getAttribute('data');
  3644. if (this._table.has(name)) {
  3645. this.emit('activate', this._table.get(name));
  3646. }
  3647. });
  3648. }
  3649. get element() {
  3650. return [this._search, this._content];
  3651. }
  3652. deactivate() {
  3653. for (const identifier of this._focused) {
  3654. this._blur(identifier);
  3655. }
  3656. this._focused.clear();
  3657. }
  3658. error(error, fatal) {
  3659. super.error(error, fatal);
  3660. const element = this.createElement('li');
  3661. element.innerHTML = `<b>ERROR:</b> ${error.message}`;
  3662. this._content.appendChild(element);
  3663. }
  3664. };
  3665. view.Quantization = class {
  3666. constructor(quantization) {
  3667. Object.assign(this, quantization);
  3668. }
  3669. toString() {
  3670. if (this.type === 'linear' || /^quant\d\d?_.*$/.test(this.type)) {
  3671. const content = [];
  3672. const scale = this.scale || [];
  3673. const offset = this.offset || [];
  3674. const bias = this.bias || [];
  3675. const max = this.max || [];
  3676. const min = this.min || [];
  3677. const length = Math.max(scale.length, offset.length, bias.length, min.length, max.length);
  3678. const size = length.toString().length;
  3679. for (let i = 0; i < length; i++) {
  3680. let s = 'q';
  3681. let bracket = false;
  3682. if (i < offset.length && offset[i] !== undefined && offset[i] !== 0 && offset[i] !== 0n) {
  3683. const value = offset[i];
  3684. s = value > 0 ? `${s} - ${value}` : `${s} + ${-value}`;
  3685. bracket = true;
  3686. }
  3687. if (i < scale.length && scale[i] !== undefined && scale[i] !== 1 && scale[i] !== 1n) {
  3688. const value = scale[i];
  3689. s = bracket ? `(${s})` : s;
  3690. s = `${value} * ${s}`;
  3691. bracket = true;
  3692. }
  3693. if (i < bias.length && bias[i] !== undefined && bias[i] !== 0 && bias[i] !== 0n) {
  3694. const value = bias[i];
  3695. s = bracket ? `(${s})` : s;
  3696. s = value < 0 ? `${s} - ${-value}` : `${s} + ${value}`;
  3697. }
  3698. if (i < min.length && min[i] !== undefined && min[i] !== 0 && min[i] !== 0n) {
  3699. s = `${min[i]} \u2264 ${s}`;
  3700. }
  3701. if (i < max.length && max[i] !== undefined && max[i] !== 0 && max[i] !== 0n) {
  3702. s = `${s} \u2264 ${max[i]}`;
  3703. }
  3704. content.push(length > 1 ? `${i.toString().padStart(size, ' ')}: ${s}` : `${s}`);
  3705. }
  3706. return content.join('\n');
  3707. } else if (this.type === 'lookup') {
  3708. const size = this.value.length.toString().length;
  3709. return this.value.map((value, index) => `${index.toString().padStart(size, ' ')}: ${value}`).join('\n');
  3710. } else if (this.type === 'annotation') {
  3711. return Array.from(this.value).map(([name, value]) => `${name} = ${value}`).join('\n');
  3712. } else if (/^q\d_[01k]$/.test(this.type) || /^iq\d_[xsnlm]+$/.test(this.type)) {
  3713. return '';
  3714. }
  3715. throw new view.Error(`Unknown quantization type '${this.type}'.`);
  3716. }
  3717. };
  3718. view.Documentation = class {
  3719. static open(source) {
  3720. if (source) {
  3721. const generator = markdown.Generator.open();
  3722. const target = {};
  3723. if (source.name) {
  3724. target.name = source.name;
  3725. }
  3726. if (source.module) {
  3727. target.module = source.module;
  3728. }
  3729. if (source.category) {
  3730. target.category = source.category;
  3731. }
  3732. if (source.summary) {
  3733. target.summary = generator.html(source.summary);
  3734. }
  3735. if (source.description) {
  3736. target.description = generator.html(source.description);
  3737. }
  3738. if (Array.isArray(source.attributes)) {
  3739. target.attributes = source.attributes.map((source) => {
  3740. const target = {};
  3741. target.name = source.name;
  3742. if (source.type !== undefined) {
  3743. target.type = source.type;
  3744. }
  3745. if (source.option !== undefined) {
  3746. target.option = source.option;
  3747. }
  3748. if (source.optional !== undefined) {
  3749. target.optional = source.optional;
  3750. }
  3751. if (source.required !== undefined) {
  3752. target.required = source.required;
  3753. }
  3754. if (source.minimum !== undefined) {
  3755. target.minimum = source.minimum;
  3756. }
  3757. if (source.src !== undefined) {
  3758. target.src = source.src;
  3759. }
  3760. if (source.src_type !== undefined) {
  3761. target.src_type = source.src_type;
  3762. }
  3763. if (source.description !== undefined) {
  3764. target.description = generator.html(source.description);
  3765. }
  3766. if (source.default !== undefined) {
  3767. target.default = source.default;
  3768. }
  3769. if (source.visible !== undefined) {
  3770. target.visible = source.visible;
  3771. }
  3772. return target;
  3773. });
  3774. }
  3775. if (Array.isArray(source.inputs)) {
  3776. target.inputs = source.inputs.map((source) => {
  3777. const target = {};
  3778. target.name = source.name;
  3779. if (source.type !== undefined) {
  3780. target.type = source.type;
  3781. }
  3782. if (source.description) {
  3783. target.description = generator.html(source.description);
  3784. }
  3785. if (source.default !== undefined) {
  3786. target.default = source.default;
  3787. }
  3788. if (source.src !== undefined) {
  3789. target.src = source.src;
  3790. }
  3791. if (source.list !== undefined) {
  3792. target.list = source.list;
  3793. }
  3794. if (source.isRef !== undefined) {
  3795. target.isRef = source.isRef;
  3796. }
  3797. if (source.typeAttr !== undefined) {
  3798. target.typeAttr = source.typeAttr;
  3799. }
  3800. if (source.numberAttr !== undefined) {
  3801. target.numberAttr = source.numberAttr;
  3802. }
  3803. if (source.typeListAttr !== undefined) {
  3804. target.typeListAttr = source.typeListAttr;
  3805. }
  3806. if (source.option !== undefined) {
  3807. target.option = source.option;
  3808. }
  3809. if (source.optional !== undefined) {
  3810. target.optional = source.optional;
  3811. }
  3812. if (source.visible !== undefined) {
  3813. target.visible = source.visible;
  3814. }
  3815. return target;
  3816. });
  3817. }
  3818. if (Array.isArray(source.outputs)) {
  3819. target.outputs = source.outputs.map((source) => {
  3820. const target = {};
  3821. target.name = source.name;
  3822. if (source.type) {
  3823. target.type = source.type;
  3824. }
  3825. if (source.description) {
  3826. target.description = generator.html(source.description);
  3827. }
  3828. if (source.list !== undefined) {
  3829. target.list = source.list;
  3830. }
  3831. if (source.typeAttr !== undefined) {
  3832. target.typeAttr = source.typeAttr;
  3833. }
  3834. if (source.typeListAttr !== undefined) {
  3835. target.typeListAttr = source.typeAttr;
  3836. }
  3837. if (source.numberAttr !== undefined) {
  3838. target.numberAttr = source.numberAttr;
  3839. }
  3840. if (source.isRef !== undefined) {
  3841. target.isRef = source.isRef;
  3842. }
  3843. if (source.option !== undefined) {
  3844. target.option = source.option;
  3845. }
  3846. return target;
  3847. });
  3848. }
  3849. if (Array.isArray(source.references)) {
  3850. target.references = source.references.map((source) => {
  3851. if (source) {
  3852. target.description = generator.html(source.description);
  3853. }
  3854. return target;
  3855. });
  3856. }
  3857. if (source.version !== undefined) {
  3858. target.version = source.version;
  3859. }
  3860. if (source.operator !== undefined) {
  3861. target.operator = source.operator;
  3862. }
  3863. if (source.identifier !== undefined) {
  3864. target.identifier = source.identifier;
  3865. }
  3866. if (source.package !== undefined) {
  3867. target.package = source.package;
  3868. }
  3869. if (source.status !== undefined) {
  3870. target.status = source.status;
  3871. }
  3872. if (source.min_input !== undefined) {
  3873. target.min_input = source.min_input;
  3874. }
  3875. if (source.max_input !== undefined) {
  3876. target.max_input = source.max_input;
  3877. }
  3878. if (source.min_output !== undefined) {
  3879. target.min_output = source.min_output;
  3880. }
  3881. if (source.max_input !== undefined) {
  3882. target.max_output = source.max_output;
  3883. }
  3884. if (source.inputs_range !== undefined) {
  3885. target.inputs_range = source.inputs_range;
  3886. }
  3887. if (source.outputs_range !== undefined) {
  3888. target.outputs_range = source.outputs_range;
  3889. }
  3890. if (source.examples !== undefined) {
  3891. target.examples = source.examples;
  3892. }
  3893. if (source.constants !== undefined) {
  3894. target.constants = source.constants;
  3895. }
  3896. if (source.type_constraints !== undefined) {
  3897. target.type_constraints = source.type_constraints;
  3898. }
  3899. return target;
  3900. }
  3901. return null;
  3902. }
  3903. };
  3904. view.Formatter = class {
  3905. constructor(value, type, quote) {
  3906. this._value = value;
  3907. this._type = type;
  3908. this._quote = quote;
  3909. this._values = new Set();
  3910. }
  3911. toString() {
  3912. return this._format(this._value, this._type, this._quote);
  3913. }
  3914. _format(value, type, quote) {
  3915. if (value && value.__class__ && value.__class__.__module__ === 'builtins' && value.__class__.__name__ === 'type') {
  3916. return `${value.__module__}.${value.__name__}`;
  3917. }
  3918. if (value && value.__class__ && value.__class__.__module__ === 'builtins' && value.__class__.__name__ === 'function') {
  3919. return `${value.__module__}.${value.__name__}`;
  3920. }
  3921. if (typeof value === 'function') {
  3922. return value();
  3923. }
  3924. if (value && typeof value === 'bigint') {
  3925. return value.toString();
  3926. }
  3927. if (Number.isNaN(value)) {
  3928. return 'NaN';
  3929. }
  3930. switch (type) {
  3931. case 'shape':
  3932. return value ? value.toString() : '(null)';
  3933. case 'shape[]':
  3934. if (value && !Array.isArray(value)) {
  3935. throw new Error(`Invalid shape '${JSON.stringify(value)}'.`);
  3936. }
  3937. return value ? value.map((item) => item.toString()).join(', ') : '(null)';
  3938. case 'graph':
  3939. return value ? value.name : '(null)';
  3940. case 'graph[]':
  3941. return value ? value.map((graph) => graph.name).join(', ') : '(null)';
  3942. case 'tensor': {
  3943. const type = value.type;
  3944. if (type && type.shape && type.shape.dimensions && Array.isArray(type.shape.dimensions) && type.shape.dimensions.length === 0) {
  3945. const tensor = new base.Tensor(value);
  3946. const encoding = tensor.encoding;
  3947. if ((encoding === '<' || encoding === '>' || encoding === '|') && !tensor.empty && tensor.type.dataType !== '?') {
  3948. let content = tensor.toString();
  3949. if (content && content.length > 10) {
  3950. content = `${content.substring(0, 10)}\u2026`;
  3951. }
  3952. return content;
  3953. }
  3954. }
  3955. return '[\u2026]';
  3956. }
  3957. case 'object':
  3958. case 'function':
  3959. return value.type.name;
  3960. case 'object[]':
  3961. case 'function[]':
  3962. return value ? value.map((item) => item.type.name).join(', ') : '(null)';
  3963. case 'type':
  3964. return value ? value.toString() : '(null)';
  3965. case 'type[]':
  3966. return value ? value.map((item) => item.toString()).join(', ') : '(null)';
  3967. default:
  3968. break;
  3969. }
  3970. if (typeof value === 'string' && (!type || type !== 'string')) {
  3971. return quote ? `"${value}"` : value;
  3972. }
  3973. if (Array.isArray(value)) {
  3974. if (value.length === 0) {
  3975. return quote ? '[]' : '';
  3976. }
  3977. let ellipsis = false;
  3978. if (value.length > 1000) {
  3979. value = value.slice(0, 1000);
  3980. ellipsis = true;
  3981. }
  3982. const itemType = (type && type.endsWith('[]')) ? type.substring(0, type.length - 2) : null;
  3983. const array = value.map((value) => {
  3984. if (value && typeof value === 'bigint') {
  3985. return value.toString();
  3986. }
  3987. if (Number.isNaN(value)) {
  3988. return 'NaN';
  3989. }
  3990. const quote = !itemType || itemType === 'string';
  3991. return this._format(value, itemType, quote);
  3992. });
  3993. if (ellipsis) {
  3994. array.push('\u2026');
  3995. }
  3996. return quote ? ['[', array.join(', '), ']'].join(' ') : array.join(', ');
  3997. }
  3998. if (value === null) {
  3999. return quote ? 'null' : '';
  4000. }
  4001. if (value === undefined) {
  4002. return 'undefined';
  4003. }
  4004. if (value !== Object(value)) {
  4005. return value.toString();
  4006. }
  4007. if (this._values.has(value)) {
  4008. return '\u2026';
  4009. }
  4010. this._values.add(value);
  4011. let list = null;
  4012. const entries = Object.entries(value).filter(([name]) => !name.startsWith('__') && !name.endsWith('__'));
  4013. if (entries.length === 1) {
  4014. list = [this._format(entries[0][1], null, true)];
  4015. } else {
  4016. list = entries.map(([name, value]) => `${name}: ${this._format(value, null, true)}`);
  4017. }
  4018. let objectType = value.__type__;
  4019. if (!objectType && value.constructor.name && value.constructor.name !== 'Object') {
  4020. objectType = value.constructor.name;
  4021. }
  4022. if (objectType) {
  4023. return objectType + (list.length === 0 ? '()' : ['(', list.join(', '), ')'].join(''));
  4024. }
  4025. switch (list.length) {
  4026. case 0:
  4027. return quote ? '()' : '';
  4028. case 1:
  4029. return list[0];
  4030. default:
  4031. return quote ? ['(', list.join(', '), ')'].join(' ') : list.join(', ');
  4032. }
  4033. }
  4034. };
  4035. markdown.Generator = class {
  4036. static open() {
  4037. if (!markdown.Generator.generator) {
  4038. markdown.Generator.generator = new markdown.Generator();
  4039. }
  4040. return markdown.Generator.generator;
  4041. }
  4042. constructor() {
  4043. this._newlineRegExp = /^\n+/;
  4044. this._codeRegExp = /^( {4}[^\n]+\n*)+/;
  4045. this._fencesRegExp = /^ {0,3}(`{3,}(?=[^`\n]*\n)|~{3,})([^\n]*)\n(?:|([\s\S]*?)\n)(?: {0,3}\1[~`]* *(?:\n+|$)|$)/;
  4046. this._hrRegExp = /^ {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)/;
  4047. this._headingRegExp = /^ {0,3}(#{1,6}) +([^\n]*?)(?: +#+)? *(?:\n+|$)/;
  4048. 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|$))+/;
  4049. 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*$)/;
  4050. 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;
  4051. this._defRegExp = /^ {0,3}\[((?!\s*\])(?:\\[[\]]|[^[\]])+)\]: *\n? *<?([^\s>]+)>?(?:(?: +\n? *| *\n *)((?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))))? *(?:\n+|$)/;
  4052. 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*|$)/;
  4053. 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*|$)/;
  4054. this._lheadingRegExp = /^([^\n]+)\n {0,3}(=+|-+) *(?:\n+|$)/;
  4055. this._textRegExp = /^[^\n]+/;
  4056. this._bulletRegExp = /(?:[*+-]|\d{1,9}[.)])/;
  4057. this._itemRegExp = /^( *)((?:[*+-]|\d{1,9}[.)])) ?[^\n]*(?:\n(?!\1(?:[*+-]|\d{1,9}[.)]) ?)[^\n]*)*/gm;
  4058. 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]+)*)/;
  4059. this._backpedalRegExp = /(?:[^?!.,:;*_~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_~)]+(?!$))+/;
  4060. this._escapeRegExp = /^\\([!"#$%&'()*+,\-./:;<=>?@[\]\\^_`{|}~~|])/;
  4061. this._escapesRegExp = /\\([!"#$%&'()*+,\-./:;<=>?@[\]\\^_`{|}~])/g;
  4062. /* eslint-disable no-control-regex */
  4063. 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])?)+(?![-_]))>/;
  4064. this._linkRegExp = /^!?\[((?:\[(?:\\.|[^[\]\\])*\]|\\.|`[^`]*`|[^[\]\\`])*?)\]\(\s*(<(?:\\[<>]?|[^\s<>\\])*>|[^\s\x00-\x1f]*)(?:\s+("(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)))?\s*\)/;
  4065. /* eslint-enable no-control-regex */
  4066. 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;
  4067. 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]*?\]\]>/;
  4068. this._reflinkRegExp = /^!?\[((?:\[(?:\\.|[^[\]\\])*\]|\\.|`[^`]*`|[^[\]\\`])*?)\]\[(?!\s*\])((?:\\[[\]]?|[^[\]\\])+)\]/;
  4069. this._nolinkRegExp = /^!?\[(?!\s*\])((?:\[[^[\]]*\]|\\[[\]]|[^[\]])*)\](?:\[\])?/;
  4070. this._reflinkSearchRegExp = /!?\[((?:\[(?:\\.|[^[\]\\])*\]|\\.|`[^`]*`|[^[\]\\`])*?)\]\[(?!\s*\])((?:\\[[\]]?|[^[\]\\])+)\]|!?\[(?!\s*\])((?:\[[^[\]]*\]|\\[[\]]|[^[\]])*)\](?:\[\])?(?!\()/g;
  4071. this._strongStartRegExp = /^(?:(\*\*(?=[*!"#$%&'()+\-.,/:;<=>?@[\]`{|}~]))|\*\*)(?![\s])|__/;
  4072. this._strongMiddleRegExp = /^\*\*(?:(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^*]|\\\*)|__[^_]*?__|\*\*\[^\*\]*?\*\*)|\*(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^*]|\\\*)|__[^_]*?__|\*\*\[^\*\]*?\*\*)*?\*)+?\*\*$|^__(?![\s])((?:(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^_]|\\_)|__[^_]*?__|\*\*\[^\*\]*?\*\*)|_(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^_]|\\_)|__[^_]*?__|\*\*\[^\*\]*?\*\*)*?_)+?)__$/;
  4073. this._strongEndAstRegExp = /[^!"#$%&'()+\-.,/:;<=>?@[\]`{|}~\s]\*\*(?!\*)|[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~]\*\*(?!\*)(?:(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~_\s]|$))/g;
  4074. this._strongEndUndRegExp = /[^\s]__(?!_)(?:(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~*\s])|$)/g;
  4075. this._emStartRegExp = /^(?:(\*(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~]))|\*)(?![*\s])|_/;
  4076. this._emMiddleRegExp = /^\*(?:(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^*]|\\\*)|__[^_]*?__|\*\*\[^\*\]*?\*\*)|\*(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^*]|\\\*)|__[^_]*?__|\*\*\[^\*\]*?\*\*)*?\*)+?\*$|^_(?![_\s])(?:(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^_]|\\_)|__[^_]*?__|\*\*\[^\*\]*?\*\*)|_(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^_]|\\_)|__[^_]*?__|\*\*\[^\*\]*?\*\*)*?_)+?_$/;
  4077. this._emEndAstRegExp = /[^!"#$%&'()+\-.,/:;<=>?@[\]`{|}~\s]\*(?!\*)|[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~]\*(?!\*)(?:(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~_\s]|$))/g;
  4078. this._emEndUndRegExp = /[^\s]_(?!_)(?:(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~*\s])|$)/g;
  4079. this._codespanRegExp = /^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/;
  4080. this._brRegExp = /^( {2,}|\\)\n(?!\s*$)/;
  4081. this._delRegExp = /^~+(?=\S)([\s\S]*?\S)~+/;
  4082. this._textspanRegExp = /^(`+|[^`])(?:(?= {2,}\n)|[\s\S]*?(?:(?=[\\<![`*~]|\b_|https?:\/\/|ftp:\/\/|www\.|$)|[^ ](?= {2,}\n)|[^a-zA-Z0-9.!#$%&'*+/=?_`{|}~-](?=[a-zA-Z0-9.!#$%&'*+/=?_`{|}~-]+@))|(?=[a-zA-Z0-9.!#$%&'*+/=?_`{|}~-]+@))/;
  4083. this._punctuationRegExp = /^([\s*!"#$%&'()+\-.,/:;<=>?@[\]`{|}~])/;
  4084. this._blockSkipRegExp = /\[[^\]]*?\]\([^)]*?\)|`[^`]*?`|<[^>]*?>/g;
  4085. this._escapeTestRegExp = /[&<>"']/;
  4086. this._escapeReplaceRegExp = /[&<>"']/g;
  4087. this._escapeTestNoEncodeRegExp = /[<>"']|&(?!#?\w+;)/;
  4088. this._escapeReplaceNoEncodeRegExp = /[<>"']|&(?!#?\w+;)/g;
  4089. this._escapeReplacementsMap = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' };
  4090. this._cache = new Map();
  4091. }
  4092. html(source) {
  4093. if (this._cache.has(source)) {
  4094. return this._cache.get(source);
  4095. }
  4096. const tokens = [];
  4097. const links = new Map();
  4098. source = source.replace(/\r\n|\r/g, '\n').replace(/\t/g, ' ');
  4099. this._tokenize(source, tokens, links, true);
  4100. this._tokenizeBlock(tokens, links);
  4101. const target = this._render(tokens, true);
  4102. if (this._cache.size > 256) {
  4103. this._cache.delete(this._cache.keys().next().value);
  4104. }
  4105. this._cache.set(source, target);
  4106. return target;
  4107. }
  4108. _tokenize(source, tokens, links, top) {
  4109. source = source.replace(/^ +$/gm, '');
  4110. while (source) {
  4111. let match = this._newlineRegExp.exec(source);
  4112. if (match) {
  4113. source = source.substring(match[0].length);
  4114. if (match[0].length > 1) {
  4115. tokens.push({ type: 'space' });
  4116. }
  4117. continue;
  4118. }
  4119. match = this._codeRegExp.exec(source);
  4120. if (match) {
  4121. source = source.substring(match[0].length);
  4122. const lastToken = tokens[tokens.length - 1];
  4123. if (lastToken && lastToken.type === 'paragraph') {
  4124. lastToken.text += `\n${match[0].trimRight()}`;
  4125. } else {
  4126. const text = match[0].replace(/^ {4}/gm, '').replace(/\n*$/, '');
  4127. tokens.push({ type: 'code', text });
  4128. }
  4129. continue;
  4130. }
  4131. match = this._fencesRegExp.exec(source);
  4132. if (match) {
  4133. source = source.substring(match[0].length);
  4134. const language = match[2] ? match[2].trim() : match[2];
  4135. let content = match[3] || '';
  4136. const matchIndent = match[0].match(/^(\s+)(?:```)/);
  4137. if (matchIndent !== null) {
  4138. const [, indent] = matchIndent;
  4139. content = content.split('\n').map((node) => {
  4140. const match = node.match(/^\s+/);
  4141. return (match !== null && match[0].length >= indent.length) ? node.slice(indent.length) : node;
  4142. }).join('\n');
  4143. }
  4144. tokens.push({ type: 'code', language, text: content });
  4145. continue;
  4146. }
  4147. match = this._headingRegExp.exec(source);
  4148. if (match) {
  4149. source = source.substring(match[0].length);
  4150. tokens.push({ type: 'heading', depth: match[1].length, text: match[2] });
  4151. continue;
  4152. }
  4153. match = this._nptableRegExp.exec(source);
  4154. if (match) {
  4155. const header = this._splitCells(match[1].replace(/^ *| *\| *$/g, ''));
  4156. const align = match[2].replace(/^ *|\| *$/g, '').split(/ *\| */);
  4157. if (header.length === align.length) {
  4158. const cells = match[3] ? match[3].replace(/\n$/, '').split('\n') : [];
  4159. const token = { type: 'table', header, align, cells, raw: match[0] };
  4160. for (let i = 0; i < token.align.length; i++) {
  4161. if (/^ *-+: *$/.test(token.align[i])) {
  4162. token.align[i] = 'right';
  4163. } else if (/^ *:-+: *$/.test(token.align[i])) {
  4164. token.align[i] = 'center';
  4165. } else if (/^ *:-+ *$/.test(token.align[i])) {
  4166. token.align[i] = 'left';
  4167. } else {
  4168. token.align[i] = null;
  4169. }
  4170. }
  4171. token.cells = token.cells.map((cell) => this._splitCells(cell, token.header.length));
  4172. source = source.substring(token.raw.length);
  4173. tokens.push(token);
  4174. continue;
  4175. }
  4176. }
  4177. match = this._hrRegExp.exec(source);
  4178. if (match) {
  4179. source = source.substring(match[0].length);
  4180. tokens.push({ type: 'hr' });
  4181. continue;
  4182. }
  4183. match = this._blockquoteRegExp.exec(source);
  4184. if (match) {
  4185. source = source.substring(match[0].length);
  4186. const text = match[0].replace(/^ *> ?/gm, '');
  4187. tokens.push({ type: 'blockquote', text, tokens: this._tokenize(text, [], links, top) });
  4188. continue;
  4189. }
  4190. match = this._listRegExp.exec(source);
  4191. if (match) {
  4192. const [value, , bull] = match;
  4193. const ordered = bull.length > 1;
  4194. const parent = bull[bull.length - 1] === ')';
  4195. let raw = value;
  4196. const list = { type: 'list', raw, ordered, start: ordered ? Number(bull.slice(0, -1)) : '', loose: false, items: [] };
  4197. const itemMatch = value.match(this._itemRegExp);
  4198. let next = false;
  4199. const length = itemMatch.length;
  4200. for (let i = 0; i < length; i++) {
  4201. let item = itemMatch[i];
  4202. raw = item;
  4203. let space = item.length;
  4204. item = item.replace(/^ *([*+-]|\d+[.)]) ?/, '');
  4205. if (item.indexOf('\n ') !== -1) {
  4206. space -= item.length;
  4207. item = item.replace(new RegExp(`^ {1,${space}}`, 'gm'), '');
  4208. }
  4209. if (i !== length - 1) {
  4210. const [bullet] = this._bulletRegExp.exec(itemMatch[i + 1]);
  4211. if (ordered ? bullet.length === 1 || (!parent && bullet[bullet.length - 1] === ')') : (bullet.length > 1)) {
  4212. const addBack = itemMatch.slice(i + 1).join('\n');
  4213. list.raw = list.raw.substring(0, list.raw.length - addBack.length);
  4214. i = length - 1;
  4215. }
  4216. }
  4217. let loose = next || /\n\n(?!\s*$)/.test(item);
  4218. if (i !== length - 1) {
  4219. next = item.charAt(item.length - 1) === '\n';
  4220. if (!loose) {
  4221. loose = next;
  4222. }
  4223. }
  4224. if (loose) {
  4225. list.loose = true;
  4226. }
  4227. const task = /^\[[ xX]\] /.test(item);
  4228. let checked = false;
  4229. if (task) {
  4230. checked = item[1] !== ' ';
  4231. item = item.replace(/^\[[ xX]\] +/, '');
  4232. }
  4233. list.items.push({ type: 'list_item', raw, task, checked, loose, text: item });
  4234. }
  4235. source = source.substring(list.raw.length);
  4236. for (const item of list.items) {
  4237. item.tokens = this._tokenize(item.text, [], links, false);
  4238. }
  4239. tokens.push(list);
  4240. continue;
  4241. }
  4242. match = this._htmlRegExp.exec(source);
  4243. if (match) {
  4244. source = source.substring(match[0].length);
  4245. tokens.push({ type: 'html', pre: (match[1] === 'pre' || match[1] === 'script' || match[1] === 'style'), text: match[0] });
  4246. continue;
  4247. }
  4248. if (top) {
  4249. match = this._defRegExp.exec(source);
  4250. if (match) {
  4251. source = source.substring(match[0].length);
  4252. match[3] = match[3] ? match[3].substring(1, match[3].length - 1) : match[3];
  4253. const tag = match[1].toLowerCase().replace(/\s+/g, ' ');
  4254. if (!links.has(tag)) {
  4255. links.set(tag, { href: match[2], title: match[3] });
  4256. }
  4257. continue;
  4258. }
  4259. }
  4260. match = this._tableRegExp.exec(source);
  4261. if (match) {
  4262. const header = this._splitCells(match[1].replace(/^ *| *\| *$/g, ''));
  4263. const align = match[2].replace(/^ *|\| *$/g, '').split(/ *\| */);
  4264. if (header.length === align.length) {
  4265. const cells = match[3] ? match[3].replace(/\n$/, '').split('\n') : [];
  4266. const token = { type: 'table', header, align, cells, raw: match[0] };
  4267. for (let i = 0; i < token.align.length; i++) {
  4268. if (/^ *-+: *$/.test(token.align[i])) {
  4269. token.align[i] = 'right';
  4270. } else if (/^ *:-+: *$/.test(token.align[i])) {
  4271. token.align[i] = 'center';
  4272. } else if (/^ *:-+ *$/.test(token.align[i])) {
  4273. token.align[i] = 'left';
  4274. } else {
  4275. token.align[i] = null;
  4276. }
  4277. }
  4278. token.cells = token.cells.map((cell) => this._splitCells(cell.replace(/^ *\| *| *\| *$/g, ''), token.header.length));
  4279. source = source.substring(token.raw.length);
  4280. tokens.push(token);
  4281. continue;
  4282. }
  4283. }
  4284. match = this._lheadingRegExp.exec(source);
  4285. if (match) {
  4286. source = source.substring(match[0].length);
  4287. tokens.push({ type: 'heading', depth: match[2].charAt(0) === '=' ? 1 : 2, text: match[1] });
  4288. continue;
  4289. }
  4290. if (top) {
  4291. match = this._paragraphRegExp.exec(source);
  4292. if (match) {
  4293. source = source.substring(match[0].length);
  4294. tokens.push({ type: 'paragraph', text: match[1].charAt(match[1].length - 1) === '\n' ? match[1].slice(0, -1) : match[1] });
  4295. continue;
  4296. }
  4297. }
  4298. match = this._textRegExp.exec(source);
  4299. if (match) {
  4300. source = source.substring(match[0].length);
  4301. const lastToken = tokens[tokens.length - 1];
  4302. if (lastToken && lastToken.type === 'text') {
  4303. lastToken.text += `\n${match[0]}`;
  4304. } else {
  4305. tokens.push({ type: 'text', text: match[0] });
  4306. }
  4307. continue;
  4308. }
  4309. throw new Error(`Unexpected '${source.charCodeAt(0)}'.`);
  4310. }
  4311. return tokens;
  4312. }
  4313. _tokenizeInline(source, links, inLink, inRawBlock, prevChar) {
  4314. const tokens = [];
  4315. let maskedSource = source;
  4316. if (links.size > 0) {
  4317. while (maskedSource) {
  4318. const match = this._reflinkSearchRegExp.exec(maskedSource);
  4319. if (match) {
  4320. if (links.has(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) {
  4321. maskedSource = `${maskedSource.slice(0, match.index)}[${'a'.repeat(match[0].length - 2)}]${maskedSource.slice(this._reflinkSearchRegExp.lastIndex)}`;
  4322. }
  4323. continue;
  4324. }
  4325. break;
  4326. }
  4327. }
  4328. while (maskedSource) {
  4329. const match = this._blockSkipRegExp.exec(maskedSource);
  4330. if (match) {
  4331. maskedSource = `${maskedSource.slice(0, match.index)}[${'a'.repeat(match[0].length - 2)}]${maskedSource.slice(this._blockSkipRegExp.lastIndex)}`;
  4332. continue;
  4333. }
  4334. break;
  4335. }
  4336. while (source) {
  4337. let match = this._escapeRegExp.exec(source);
  4338. if (match) {
  4339. source = source.substring(match[0].length);
  4340. tokens.push({ type: 'escape', text: this._escape(match[1]) });
  4341. continue;
  4342. }
  4343. match = this._tagRegExp.exec(source);
  4344. if (match) {
  4345. source = source.substring(match[0].length);
  4346. if (!inLink && /^<a /i.test(match[0])) {
  4347. inLink = true;
  4348. } else if (inLink && /^<\/a>/i.test(match[0])) {
  4349. inLink = false;
  4350. }
  4351. if (!inRawBlock && /^<(pre|code|kbd|script)(\s|>)/i.test(match[0])) {
  4352. inRawBlock = true;
  4353. } else if (inRawBlock && /^<\/(pre|code|kbd|script)(\s|>)/i.test(match[0])) {
  4354. inRawBlock = false;
  4355. }
  4356. tokens.push({ type: 'html', raw: match[0], text: match[0] });
  4357. continue;
  4358. }
  4359. match = this._linkRegExp.exec(source);
  4360. if (match) {
  4361. let index = -1;
  4362. const [, , ref] = match;
  4363. if (ref.indexOf(')') !== -1) {
  4364. let level = 0;
  4365. for (let i = 0; i < ref.length; i++) {
  4366. switch (ref[i]) {
  4367. case '\\':
  4368. i++;
  4369. break;
  4370. case '(':
  4371. level++;
  4372. break;
  4373. case ')':
  4374. level--;
  4375. if (level < 0) {
  4376. index = i;
  4377. i = ref.length;
  4378. }
  4379. break;
  4380. default:
  4381. break;
  4382. }
  4383. }
  4384. }
  4385. if (index > -1) {
  4386. const length = (match[0].indexOf('!') === 0 ? 5 : 4) + match[1].length + index;
  4387. match[2] = match[2].substring(0, index);
  4388. match[0] = match[0].substring(0, length).trim();
  4389. match[3] = '';
  4390. }
  4391. const title = (match[3] ? match[3].slice(1, -1) : '').replace(this._escapesRegExp, '$1');
  4392. const href = match[2].trim().replace(/^<([\s\S]*)>$/, '$1').replace(this._escapesRegExp, '$1');
  4393. const token = this._outputLink(match, href, title);
  4394. source = source.substring(match[0].length);
  4395. if (token.type === 'link') {
  4396. token.tokens = this._tokenizeInline(token.text, links, true, inRawBlock, '');
  4397. }
  4398. tokens.push(token);
  4399. continue;
  4400. }
  4401. match = this._reflinkRegExp.exec(source) || this._nolinkRegExp.exec(source);
  4402. if (match) {
  4403. let link = (match[2] || match[1]).replace(/\s+/g, ' ');
  4404. link = links.get(link.toLowerCase());
  4405. if (!link || !link.href) {
  4406. const text = match[0].charAt(0);
  4407. source = source.substring(text.length);
  4408. tokens.push({ type: 'text', text });
  4409. } else {
  4410. source = source.substring(match[0].length);
  4411. const token = this._outputLink(match, link);
  4412. if (token.type === 'link') {
  4413. token.tokens = this._tokenizeInline(token.text, links, true, inRawBlock, '');
  4414. }
  4415. tokens.push(token);
  4416. }
  4417. continue;
  4418. }
  4419. match = this._strongStartRegExp.exec(source);
  4420. if (match && (!match[1] || (match[1] && (prevChar === '' || this._punctuationRegExp.exec(prevChar))))) {
  4421. const masked = maskedSource.slice(-1 * source.length);
  4422. const endReg = match[0] === '**' ? this._strongEndAstRegExp : this._strongEndUndRegExp;
  4423. endReg.lastIndex = 0;
  4424. let cap = '';
  4425. while ((match = endReg.exec(masked)) !== null) {
  4426. cap = this._strongMiddleRegExp.exec(masked.slice(0, match.index + 3));
  4427. if (cap) {
  4428. break;
  4429. }
  4430. }
  4431. if (cap) {
  4432. const text = source.substring(2, cap[0].length - 2);
  4433. source = source.substring(cap[0].length);
  4434. tokens.push({ type: 'strong', text, tokens: this._tokenizeInline(text, links, inLink, inRawBlock, '') });
  4435. continue;
  4436. }
  4437. }
  4438. match = this._emStartRegExp.exec(source);
  4439. if (match && (!match[1] || (match[1] && (prevChar === '' || this._punctuationRegExp.exec(prevChar))))) {
  4440. const masked = maskedSource.slice(-1 * source.length);
  4441. const endReg = match[0] === '*' ? this._emEndAstRegExp : this._emEndUndRegExp;
  4442. endReg.lastIndex = 0;
  4443. let cap = '';
  4444. while ((match = endReg.exec(masked)) !== null) {
  4445. cap = this._emMiddleRegExp.exec(masked.slice(0, match.index + 2));
  4446. if (cap) {
  4447. break;
  4448. }
  4449. }
  4450. if (cap) {
  4451. const text = source.slice(1, cap[0].length - 1);
  4452. source = source.substring(cap[0].length);
  4453. tokens.push({ type: 'em', text, tokens: this._tokenizeInline(text, links, inLink, inRawBlock, '') });
  4454. continue;
  4455. }
  4456. }
  4457. match = this._codespanRegExp.exec(source);
  4458. if (match) {
  4459. source = source.substring(match[0].length);
  4460. let content = match[2].replace(/\n/g, ' ');
  4461. if (/[^ ]/.test(content) && content.startsWith(' ') && content.endsWith(' ')) {
  4462. content = content.substring(1, content.length - 1);
  4463. }
  4464. tokens.push({ type: 'codespan', text: this._encode(content) });
  4465. continue;
  4466. }
  4467. match = this._brRegExp.exec(source);
  4468. if (match) {
  4469. source = source.substring(match[0].length);
  4470. tokens.push({ type: 'br' });
  4471. continue;
  4472. }
  4473. match = this._delRegExp.exec(source);
  4474. if (match) {
  4475. const [value, text] = match;
  4476. source = source.substring(value.length);
  4477. tokens.push({ type: 'del', text, tokens: this._tokenizeInline(text, links, inLink, inRawBlock, '') });
  4478. continue;
  4479. }
  4480. match = this._autolinkRegExp.exec(source);
  4481. if (match) {
  4482. source = source.substring(match[0].length);
  4483. const text = this._escape(match[1]);
  4484. const href = match[2] === '@' ? `mailto:${text}` : text;
  4485. tokens.push({ type: 'link', text, href, tokens: [{ type: 'text', raw: text, text }] });
  4486. continue;
  4487. }
  4488. if (!inLink) {
  4489. match = this._urlRegExp.exec(source);
  4490. if (match) {
  4491. const email = match[2] === '@';
  4492. let [value] = match;
  4493. if (!email) {
  4494. let prevCapZero = '';
  4495. do {
  4496. prevCapZero = value;
  4497. [value] = this._backpedalRegExp.exec(value);
  4498. } while (prevCapZero !== value);
  4499. }
  4500. const text = this._escape(value);
  4501. let href = text;
  4502. if (email) {
  4503. href = `mailto:${text}`;
  4504. } else if (text.startsWith('www.')) {
  4505. href = `http://${text}`;
  4506. }
  4507. source = source.substring(value.length);
  4508. tokens.push({ type: 'link', text, href, tokens: [{ type: 'text', text }] });
  4509. continue;
  4510. }
  4511. }
  4512. match = this._textspanRegExp.exec(source);
  4513. if (match) {
  4514. source = source.substring(match[0].length);
  4515. prevChar = match[0].slice(-1);
  4516. tokens.push({ type: 'text' , text: inRawBlock ? match[0] : this._escape(match[0]) });
  4517. continue;
  4518. }
  4519. throw new Error(`Unexpected '${source.charCodeAt(0)}'.`);
  4520. }
  4521. return tokens;
  4522. }
  4523. _tokenizeBlock(tokens, links) {
  4524. for (const token of tokens) {
  4525. switch (token.type) {
  4526. case 'paragraph':
  4527. case 'text':
  4528. case 'heading': {
  4529. token.tokens = this._tokenizeInline(token.text, links, false, false, '');
  4530. break;
  4531. }
  4532. case 'table': {
  4533. token.tokens = {};
  4534. token.tokens.header = token.header.map((header) => this._tokenizeInline(header, links, false, false, ''));
  4535. token.tokens.cells = token.cells.map((cell) => cell.map((row) => this._tokenizeInline(row, links, false, false, '')));
  4536. break;
  4537. }
  4538. case 'blockquote': {
  4539. this._tokenizeBlock(token.tokens, links);
  4540. break;
  4541. }
  4542. case 'list': {
  4543. for (const item of token.items) {
  4544. this._tokenizeBlock(item.tokens, links);
  4545. }
  4546. break;
  4547. }
  4548. default: {
  4549. break;
  4550. }
  4551. }
  4552. }
  4553. }
  4554. _render(tokens, top) {
  4555. let html = '';
  4556. while (tokens.length > 0) {
  4557. const token = tokens.shift();
  4558. switch (token.type) {
  4559. case 'space': {
  4560. continue;
  4561. }
  4562. case 'hr': {
  4563. html += '<hr>\n';
  4564. continue;
  4565. }
  4566. case 'heading': {
  4567. const level = token.depth;
  4568. html += `<h${level}">${this._renderInline(token.tokens)}</h${level}>\n`;
  4569. continue;
  4570. }
  4571. case 'code': {
  4572. const code = token.text;
  4573. const [language] = (token.language || '').match(/\S*/);
  4574. html += `<pre><code${language ? ` class="language-${this._encode(language)}"` : ''}>${token.escaped ? code : this._encode(code)}</code></pre>\n`;
  4575. continue;
  4576. }
  4577. case 'table': {
  4578. let header = '';
  4579. let cell = '';
  4580. for (let j = 0; j < token.header.length; j++) {
  4581. const content = this._renderInline(token.tokens.header[j]);
  4582. const align = token.align[j];
  4583. cell += `<th${align ? ` align="${align}"` : ''}>${content}</th>\n`;
  4584. }
  4585. header += `<tr>\n${cell}</tr>\n`;
  4586. let body = '';
  4587. for (let j = 0; j < token.cells.length; j++) {
  4588. const row = token.tokens.cells[j];
  4589. cell = '';
  4590. for (let k = 0; k < row.length; k++) {
  4591. const content = this._renderInline(row[k]);
  4592. const align = token.align[k];
  4593. cell += `<td${align ? ` align="${align}"` : ''}>${content}</td>\n`;
  4594. }
  4595. body += `<tr>\n${cell}</tr>\n`;
  4596. }
  4597. html += `<table>\n<thead>\n${header}</thead>\n${body ? `<tbody>${body}</tbody>` : body}</table>\n`;
  4598. continue;
  4599. }
  4600. case 'blockquote': {
  4601. html += `<blockquote>\n${this._render(token.tokens, true)}</blockquote>\n`;
  4602. continue;
  4603. }
  4604. case 'list': {
  4605. const ordered = token.ordered;
  4606. const start = token.start;
  4607. const loose = token.loose;
  4608. let body = '';
  4609. for (const item of token.items) {
  4610. let itemBody = '';
  4611. if (item.task) {
  4612. const checkbox = `<input ${item.checked ? 'checked="" ' : ''}disabled="" type="checkbox"> `;
  4613. if (loose) {
  4614. if (item.tokens.length > 0 && item.tokens[0].type === 'text') {
  4615. item.tokens[0].text = `${checkbox} ${item.tokens[0].text}`;
  4616. if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') {
  4617. item.tokens[0].tokens[0].text = `${checkbox} ${item.tokens[0].tokens[0].text}`;
  4618. }
  4619. } else {
  4620. item.tokens.unshift({ type: 'text', text: checkbox });
  4621. }
  4622. } else {
  4623. itemBody += checkbox;
  4624. }
  4625. }
  4626. itemBody += this._render(item.tokens, loose);
  4627. body += `<li>${itemBody}</li>\n`;
  4628. }
  4629. const type = (ordered ? 'ol' : 'ul');
  4630. html += `<${type}${ordered && start !== 1 ? (` start="${start}"`) : ''}>\n${body}</${type}>\n`;
  4631. continue;
  4632. }
  4633. case 'html': {
  4634. html += token.text;
  4635. continue;
  4636. }
  4637. case 'paragraph': {
  4638. html += `<p>${this._renderInline(token.tokens)}</p>\n`;
  4639. continue;
  4640. }
  4641. case 'text': {
  4642. html += top ? '<p>' : '';
  4643. html += token.tokens ? this._renderInline(token.tokens) : token.text;
  4644. while (tokens.length > 0 && tokens[0].type === 'text') {
  4645. const token = tokens.shift();
  4646. html += `\n${token.tokens ? this._renderInline(token.tokens) : token.text}`;
  4647. }
  4648. html += top ? '</p>\n' : '';
  4649. continue;
  4650. }
  4651. default: {
  4652. throw new Error(`Unexpected token type '${token.type}'.`);
  4653. }
  4654. }
  4655. }
  4656. return html;
  4657. }
  4658. _renderInline(tokens) {
  4659. let html = '';
  4660. for (const token of tokens) {
  4661. switch (token.type) {
  4662. case 'escape':
  4663. case 'html':
  4664. case 'text': {
  4665. html += token.text;
  4666. break;
  4667. }
  4668. case 'link': {
  4669. const text = this._renderInline(token.tokens);
  4670. html += `<a href="${token.href}"${token.title ? ` title="${token.title}"` : ''} target="_blank">${text}</a>`;
  4671. break;
  4672. }
  4673. case 'image': {
  4674. html += `<img src="${token.href}" alt="${token.text}"${token.title ? ` title="${token.title}"` : ''}>`;
  4675. break;
  4676. }
  4677. case 'strong': {
  4678. const text = this._renderInline(token.tokens);
  4679. html += `<strong>${text}</strong>`;
  4680. break;
  4681. }
  4682. case 'em': {
  4683. const text = this._renderInline(token.tokens);
  4684. html += `<em>${text}</em>`;
  4685. break;
  4686. }
  4687. case 'codespan': {
  4688. html += `<code>${token.text}</code>`;
  4689. break;
  4690. }
  4691. case 'br': {
  4692. html += '<br>';
  4693. break;
  4694. }
  4695. case 'del': {
  4696. const text = this._renderInline(token.tokens);
  4697. html += `<del>${text}</del>`;
  4698. break;
  4699. }
  4700. default: {
  4701. throw new Error(`Unexpected token type '${token.type}'.`);
  4702. }
  4703. }
  4704. }
  4705. return html;
  4706. }
  4707. _outputLink(match, href, title) {
  4708. title = title ? this._escape(title) : null;
  4709. const text = match[1].replace(/\\([[\]])/g, '$1');
  4710. return match[0].charAt(0) === '!' ?
  4711. { type: 'image', href, title, text: this._escape(text) } :
  4712. { type: 'link', href, title, text };
  4713. }
  4714. _splitCells(tableRow, count) {
  4715. const row = tableRow.replace(/\|/g, (match, offset, str) => {
  4716. let escaped = false;
  4717. let position = offset;
  4718. while (--position >= 0 && str[position] === '\\') {
  4719. escaped = !escaped;
  4720. }
  4721. return escaped ? '|' : ' |';
  4722. });
  4723. const cells = row.split(/ \|/);
  4724. if (cells.length > count) {
  4725. cells.splice(count);
  4726. } else {
  4727. while (cells.length < count) {
  4728. cells.push('');
  4729. }
  4730. }
  4731. return cells.map((cell) => cell.trim().replace(/\\\|/g, '|'));
  4732. }
  4733. _encode(content) {
  4734. if (this._escapeTestRegExp.test(content)) {
  4735. return content.replace(this._escapeReplaceRegExp, (ch) => this._escapeReplacementsMap[ch]);
  4736. }
  4737. return content;
  4738. }
  4739. _escape(content) {
  4740. if (this._escapeTestNoEncodeRegExp.test(content)) {
  4741. return content.replace(this._escapeReplaceNoEncodeRegExp, (ch) => this._escapeReplacementsMap[ch]);
  4742. }
  4743. return content;
  4744. }
  4745. };
  4746. metrics.Argument = class {
  4747. constructor(name, value, type) {
  4748. this.name = name;
  4749. this.value = value;
  4750. this.type = type;
  4751. }
  4752. };
  4753. metrics.Tensor = class {
  4754. constructor(tensor) {
  4755. this._tensor = tensor;
  4756. this._metrics = null;
  4757. }
  4758. get metrics() {
  4759. if (this._metrics === null) {
  4760. this._metrics = [];
  4761. this._metrics = Array.from(this._tensor.metrics || []);
  4762. const keys = new Set(this._metrics.map((metrics) => metrics.name));
  4763. const type = this._tensor.type;
  4764. const shape = type.shape.dimensions;
  4765. const size = shape.reduce((a, b) => a * b, 1);
  4766. 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'))) {
  4767. const data = this._tensor.value;
  4768. let zeros = 0;
  4769. let min = null;
  4770. let max = null;
  4771. let sum = 0;
  4772. let count = 0;
  4773. const stack = [data];
  4774. while (stack.length > 0) {
  4775. const data = stack.pop();
  4776. if (Array.isArray(data)) {
  4777. for (const element of data) {
  4778. stack.push(element);
  4779. }
  4780. } else {
  4781. zeros += data === 0 || data === 0n || data === '';
  4782. min = Math.min(data, min === null ? data : min);
  4783. max = Math.max(data, max === null ? data : max);
  4784. sum += data;
  4785. count += 1;
  4786. }
  4787. }
  4788. const mean = sum / count;
  4789. if (!keys.has('sparsity')) {
  4790. this._metrics.push(new metrics.Argument('min', min, type.dataType));
  4791. }
  4792. if (!keys.has('max')) {
  4793. this._metrics.push(new metrics.Argument('max', max, type.dataType));
  4794. }
  4795. if (!keys.has('mean')) {
  4796. this._metrics.push(new metrics.Argument('mean', mean, type.dataType));
  4797. }
  4798. if (!keys.has('std')) {
  4799. let variance = 0;
  4800. const stack = [data];
  4801. while (stack.length > 0) {
  4802. const data = stack.pop();
  4803. if (Array.isArray(data)) {
  4804. for (const element of data) {
  4805. stack.push(element);
  4806. }
  4807. } else {
  4808. variance += Math.pow(data - mean, 2);
  4809. }
  4810. }
  4811. this._metrics.push(new metrics.Argument('std', Math.sqrt(variance / count)));
  4812. }
  4813. if (!keys.has('sparsity')) {
  4814. this._metrics.push(new metrics.Argument('sparsity', count > 0 ? zeros / count : 0, 'percentage'));
  4815. }
  4816. }
  4817. }
  4818. return this._metrics;
  4819. }
  4820. };
  4821. view.Context = class {
  4822. constructor(context, identifier, stream) {
  4823. this._context = context;
  4824. this._tags = new Map();
  4825. this._content = new Map();
  4826. this._stream = stream || context.stream;
  4827. identifier = typeof identifier === 'string' ? identifier : context.identifier;
  4828. const index = Math.max(identifier.lastIndexOf('/'), identifier.lastIndexOf('\\'));
  4829. this._base = index === -1 ? undefined : identifier.substring(0, index);
  4830. this._identifier = index === -1 ? identifier : identifier.substring(index + 1);
  4831. }
  4832. get identifier() {
  4833. return this._identifier;
  4834. }
  4835. get stream() {
  4836. return this._stream;
  4837. }
  4838. async request(file) {
  4839. return this._context.request(file, 'utf-8', null);
  4840. }
  4841. async fetch(file) {
  4842. const stream = await this._context.request(file, null, this._base);
  4843. return new view.Context(this, file, stream, new Map());
  4844. }
  4845. async require(id) {
  4846. return this._context.require(id);
  4847. }
  4848. error(error, fatal) {
  4849. if (error && this.identifier) {
  4850. error.context = this.identifier;
  4851. }
  4852. this._context.error(error, fatal);
  4853. }
  4854. peek(type) {
  4855. if (!this._content.has(type)) {
  4856. this._content.set(type, undefined);
  4857. const stream = this.stream;
  4858. if (stream) {
  4859. const position = stream.position;
  4860. const match = (buffer, signature) => {
  4861. return signature.length <= buffer.length && buffer.every((value, index) => signature[index] === undefined || signature[index] === value);
  4862. };
  4863. const buffer = stream.peek(Math.min(stream.length, 16));
  4864. const skip =
  4865. match(buffer, [0x80, undefined, 0x8a, 0x0a, 0x6c, 0xfc, 0x9c, 0x46, 0xf9, 0x20, 0x6a, 0xa8, 0x50, 0x19]) || // PyTorch
  4866. (type !== 'npz' && type !== 'zip' && match(buffer, [0x50, 0x4B, 0x03, 0x04])) || // Zip
  4867. (type !== 'hdf5' && match(buffer, [0x89, 0x48, 0x44, 0x46, 0x0D, 0x0A, 0x1A, 0x0A])) || // \x89HDF\r\n\x1A\n
  4868. Array.from(this._tags).some(([key, value]) => key !== 'flatbuffers' && value.size > 0) ||
  4869. Array.from(this._content.values()).some((obj) => obj !== undefined);
  4870. if (!skip) {
  4871. switch (type) {
  4872. case 'json': {
  4873. try {
  4874. const buffer = stream.peek(Math.min(this.stream.length, 0x1000));
  4875. if (stream.length < 0x7ffff000 &&
  4876. (buffer.length < 8 || String.fromCharCode.apply(null, buffer.slice(0, 8)) !== '\x89HDF\r\n\x1A\n') &&
  4877. (buffer.some((v) => v === 0x22 || v === 0x5b || v === 0x5d || v === 0x7b || v === 0x7d))) {
  4878. const reader = json.TextReader.open(stream);
  4879. if (reader) {
  4880. const obj = reader.read();
  4881. this._content.set(type, obj);
  4882. }
  4883. }
  4884. } catch {
  4885. // continue regardless of error
  4886. }
  4887. break;
  4888. }
  4889. case 'json.gz': {
  4890. try {
  4891. const entries = this.peek('gzip');
  4892. if (entries && entries.size === 1) {
  4893. const stream = entries.values().next().value;
  4894. const reader = json.TextReader.open(stream);
  4895. if (reader) {
  4896. const obj = reader.read();
  4897. this._content.set(type, obj);
  4898. }
  4899. }
  4900. } catch {
  4901. // continue regardless of error
  4902. }
  4903. break;
  4904. }
  4905. case 'pkl': {
  4906. let unpickler = null;
  4907. const types = new Set();
  4908. try {
  4909. const archive = zip.Archive.open(stream, 'zlib');
  4910. const data = archive ? archive.entries.get('') : stream;
  4911. let condition = false;
  4912. if (data.length > 2) {
  4913. const head = data.peek(2);
  4914. condition = head[0] === 0x80 && head[1] < 7;
  4915. if (!condition) {
  4916. data.seek(-1);
  4917. const tail = data.peek(1);
  4918. data.seek(0);
  4919. condition = tail[0] === 0x2e;
  4920. }
  4921. }
  4922. if (condition) {
  4923. const execution = new python.Execution();
  4924. execution.on('resolve', (_, name) => types.add(name));
  4925. const pickle = execution.__import__('pickle');
  4926. unpickler = new pickle.Unpickler(data);
  4927. }
  4928. } catch {
  4929. // continue regardless of error
  4930. }
  4931. if (unpickler) {
  4932. const storages = new Map();
  4933. unpickler.persistent_load = (saved_id) => {
  4934. if (Array.isArray(saved_id) && saved_id.length > 3) {
  4935. switch (saved_id[0]) {
  4936. case 'storage': {
  4937. const [, storage_type, key, , size] = saved_id;
  4938. if (!storages.has(key)) {
  4939. const storage = new storage_type(size);
  4940. storages.set(key, storage);
  4941. }
  4942. return storages.get(key);
  4943. }
  4944. default: {
  4945. throw new python.Error(`Unsupported persistent load type '${saved_id[0]}'.`);
  4946. }
  4947. }
  4948. }
  4949. throw new view.Error("Unsupported 'persistent_load'.");
  4950. };
  4951. try {
  4952. const obj = unpickler.load();
  4953. this._content.set(type, obj);
  4954. } catch (error) {
  4955. this._content.set(type, error);
  4956. }
  4957. if (Array.from(types).every((name) => !name.startsWith('__torch__.'))) {
  4958. for (const name of types) {
  4959. this.error(new view.Error(`Unknown type name '${name}'.`));
  4960. }
  4961. } else {
  4962. this._content.set(type, new view.Error("PyTorch standalone 'data.pkl' format not supported."));
  4963. }
  4964. }
  4965. break;
  4966. }
  4967. case 'hdf5': {
  4968. const file = hdf5.File.open(stream);
  4969. if (file) {
  4970. try {
  4971. this._content.set(type, file.read());
  4972. } catch (error) {
  4973. this._content.set(type, error);
  4974. }
  4975. }
  4976. break;
  4977. }
  4978. case 'zip':
  4979. case 'tar':
  4980. case 'gzip': {
  4981. this._content.set('zip', undefined);
  4982. this._content.set('tar', undefined);
  4983. this._content.set('gzip', undefined);
  4984. let stream = this._stream;
  4985. try {
  4986. const archive = zip.Archive.open(this._stream, 'gzip');
  4987. if (archive) {
  4988. let entries = archive.entries;
  4989. if (entries.size === 1) {
  4990. const key = entries.keys().next().value;
  4991. stream = entries.values().next().value;
  4992. const name = key === '' ? this.identifier.replace(/\.gz$/, '') : key;
  4993. entries = new Map([[name, stream]]);
  4994. }
  4995. this._content.set('gzip', entries);
  4996. }
  4997. } catch (error) {
  4998. this._content.set('gzip', error);
  4999. }
  5000. let skipTar = false;
  5001. try {
  5002. const archive = zip.Archive.open(stream, 'zip');
  5003. if (archive) {
  5004. this._content.set('zip', archive.entries);
  5005. skipTar = true;
  5006. }
  5007. } catch (error) {
  5008. this._content.set('zip', error);
  5009. }
  5010. if (!skipTar) {
  5011. try {
  5012. const archive = tar.Archive.open(stream);
  5013. if (archive) {
  5014. this._content.set('tar', archive.entries);
  5015. }
  5016. } catch (error) {
  5017. this._content.set('tar', error);
  5018. }
  5019. }
  5020. break;
  5021. }
  5022. case 'flatbuffers.binary': {
  5023. try {
  5024. const reader = flatbuffers.BinaryReader.open(this._stream);
  5025. if (reader) {
  5026. this._content.set('flatbuffers.binary', reader);
  5027. }
  5028. } catch (error) {
  5029. this._content.set('flatbuffers.binary', error);
  5030. }
  5031. break;
  5032. }
  5033. case 'npz': {
  5034. try {
  5035. const content = new Map();
  5036. const entries = this.peek('zip');
  5037. if (entries instanceof Map && entries.size > 0 &&
  5038. Array.from(entries.keys()).every((name) => name.endsWith('.npy'))) {
  5039. const execution = new python.Execution();
  5040. for (const [name, stream] of entries) {
  5041. const buffer = stream.peek();
  5042. const bytes = execution.invoke('io.BytesIO', [buffer]);
  5043. const array = execution.invoke('numpy.load', [bytes]);
  5044. content.set(name, array);
  5045. }
  5046. this._content.set(type, content);
  5047. }
  5048. } catch {
  5049. // continue regardless of error
  5050. }
  5051. break;
  5052. }
  5053. default: {
  5054. throw new view.Error(`Unsupported open format type '${type}'.`);
  5055. }
  5056. }
  5057. }
  5058. if (stream.position !== position) {
  5059. stream.seek(0);
  5060. }
  5061. }
  5062. }
  5063. return this._content.get(type);
  5064. }
  5065. read(type, ...args) {
  5066. if (!this._content.has(type)) {
  5067. switch (type) {
  5068. case 'json': {
  5069. const reader = json.TextReader.open(this._stream);
  5070. if (reader) {
  5071. const obj = reader.read();
  5072. this._content.set('json', obj);
  5073. return obj;
  5074. }
  5075. throw new view.Error('Invalid JSON content.');
  5076. }
  5077. case 'bson': {
  5078. const reader = json.BinaryReader.open(this._stream);
  5079. if (reader) {
  5080. return reader.read();
  5081. }
  5082. throw new view.Error('Invalid BSON content.');
  5083. }
  5084. case 'flatbuffers.binary': {
  5085. const reader = flatbuffers.BinaryReader.open(this._stream);
  5086. if (reader) {
  5087. this._content.set('flatbuffers.reader', reader);
  5088. return reader;
  5089. }
  5090. throw new view.Error('Invalid FlatBuffers content.');
  5091. }
  5092. case 'flatbuffers.text': {
  5093. const obj = this.peek('json');
  5094. return flatbuffers.TextReader.open(obj);
  5095. }
  5096. case 'protobuf.binary': {
  5097. return protobuf.BinaryReader.open(this._stream);
  5098. }
  5099. case 'protobuf.text': {
  5100. return protobuf.TextReader.open(this._stream);
  5101. }
  5102. case 'binary.big-endian': {
  5103. return base.BinaryReader.open(this._stream, false);
  5104. }
  5105. case 'binary': {
  5106. return base.BinaryReader.open(this._stream);
  5107. }
  5108. case 'text': {
  5109. if (typeof args[0] === 'number') {
  5110. const length = Math.min(this._stream.length, args[0]);
  5111. const buffer = this._stream.peek(length);
  5112. text.Reader.open(buffer);
  5113. }
  5114. return text.Reader.open(this._stream);
  5115. }
  5116. case 'text.decoder': {
  5117. return text.Decoder.open(this._stream);
  5118. }
  5119. default: {
  5120. break;
  5121. }
  5122. }
  5123. }
  5124. return this.peek(type);
  5125. }
  5126. tags(type) {
  5127. if (!this._tags.has(type)) {
  5128. let tags = new Map();
  5129. const stream = this.stream;
  5130. if (stream) {
  5131. const position = stream.position;
  5132. const signatures = [
  5133. [0x89, 0x48, 0x44, 0x46, 0x0D, 0x0A, 0x1A, 0x0A], // HDF5
  5134. [0x80, undefined, 0x8a, 0x0a, 0x6c, 0xfc, 0x9c, 0x46, 0xf9, 0x20, 0x6a, 0xa8, 0x50, 0x19], // PyTorch
  5135. [0x50, 0x4b], // Zip
  5136. [0x1f, 0x8b] // Gzip
  5137. ];
  5138. const skip =
  5139. signatures.some((signature) => signature.length <= stream.length && stream.peek(signature.length).every((value, index) => signature[index] === undefined || signature[index] === value)) ||
  5140. (Array.from(this._tags).some(([key, value]) => key !== 'flatbuffers' && value.size > 0) && type !== 'pb+') ||
  5141. Array.from(this._content.values()).some((obj) => obj !== undefined) ||
  5142. (stream.length < 0x7ffff000 && json.TextReader.open(stream));
  5143. if (!skip && stream.length < 0x7ffff000) {
  5144. try {
  5145. switch (type) {
  5146. case 'pbtxt': {
  5147. const reader = protobuf.TextReader.open(stream);
  5148. tags = reader ? reader.signature() : tags;
  5149. break;
  5150. }
  5151. case 'pb': {
  5152. const reader = protobuf.BinaryReader.open(stream);
  5153. tags = reader.signature();
  5154. break;
  5155. }
  5156. case 'pb+': {
  5157. const reader = protobuf.BinaryReader.open(stream);
  5158. tags = reader.decode();
  5159. break;
  5160. }
  5161. case 'xml': {
  5162. const reader = xml.TextReader.open(stream);
  5163. if (reader) {
  5164. const document = reader.peek();
  5165. const element = document.documentElement;
  5166. const namespaceURI = element.namespaceURI;
  5167. const localName = element.localName;
  5168. const name = namespaceURI ? `${namespaceURI}:${localName}` : localName;
  5169. tags.set(name, element);
  5170. }
  5171. break;
  5172. }
  5173. default: {
  5174. throw new view.Error(`Unsupported tags format type '${type}'.`);
  5175. }
  5176. }
  5177. } catch {
  5178. tags.clear();
  5179. }
  5180. }
  5181. if (stream.position !== position) {
  5182. stream.seek(position);
  5183. }
  5184. }
  5185. this._tags.set(type, tags);
  5186. }
  5187. return this._tags.get(type);
  5188. }
  5189. metadata(name) {
  5190. return view.Metadata.open(this, name);
  5191. }
  5192. };
  5193. view.EntryContext = class {
  5194. constructor(host, entries) {
  5195. this._host = host;
  5196. this._entries = entries;
  5197. }
  5198. async request(file, encoding, base) {
  5199. if (base === null) {
  5200. return this._host.request(file, encoding, base);
  5201. }
  5202. let stream = null;
  5203. if (typeof base === 'string') {
  5204. stream = this._entries.get(`${base}/${file}`) || this._entries.get(`${base}\\${file}`);
  5205. } else {
  5206. stream = this._entries.get(file);
  5207. }
  5208. if (!stream) {
  5209. throw new view.Error('File not found.');
  5210. }
  5211. if (encoding) {
  5212. const decoder = new TextDecoder(encoding);
  5213. const buffer = stream.peek();
  5214. const value = decoder.decode(buffer);
  5215. return value;
  5216. }
  5217. return stream;
  5218. }
  5219. async require(id) {
  5220. return this._host.require(id);
  5221. }
  5222. error(error, fatal) {
  5223. this._host.exception(error, fatal);
  5224. }
  5225. };
  5226. view.ArchiveError = class extends Error {
  5227. constructor(message) {
  5228. super(message);
  5229. this.name = 'Error loading archive.';
  5230. }
  5231. };
  5232. view.ModelFactoryService = class {
  5233. constructor(host) {
  5234. this._host = host;
  5235. this._patterns = new Set(['.zip', '.tar', '.tar.gz', '.tgz', '.gz']);
  5236. this._factories = [];
  5237. this.register('./server', ['.netron']);
  5238. 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']);
  5239. this.register('./onnx', ['.onnx', '.onnx.data', '.onn', '.pb', '.onnxtxt', '.pbtxt', '.prototxt', '.txt', '.model', '.pt', '.pth', '.pkl', '.ort', '.ort.onnx', '.ngf', '.json', '.bin', 'onnxmodel']);
  5240. this.register('./tflite', ['.tflite', '.lite', '.tfl', '.bin', '.pb', '.tmfile', '.h5', '.model', '.json', '.txt', '.dat', '.nb', '.ckpt']);
  5241. this.register('./mxnet', ['.json', '.params'], ['.mar']);
  5242. this.register('./coreml', ['.mlmodel', '.bin', 'manifest.json', 'metadata.json', 'featuredescriptions.json', '.pb', '.pbtxt', '.mil'], ['.mlpackage', '.mlmodelc']);
  5243. this.register('./caffe', ['.caffemodel', '.pbtxt', '.prototxt', '.pt', '.txt']);
  5244. this.register('./caffe2', ['.pb', '.pbtxt', '.prototxt']);
  5245. this.register('./torch', ['.t7', '.net']);
  5246. 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']);
  5247. this.register('./tensorrt', ['.trt', '.trtmodel', '.engine', '.model', '.txt', '.uff', '.pb', '.tmfile', '.onnx', '.pth', '.dnn', '.plan', '.pt', '.dat']);
  5248. this.register('./keras', ['.h5', '.hd5', '.hdf5', '.keras', '.json', '.cfg', '.model', '.pb', '.pth', '.weights', '.pkl', '.lite', '.tflite', '.ckpt', '.pb', 'model.weights.npz'], ['.zip']);
  5249. this.register('./numpy', ['.npz', '.npy', '.pkl', '.pickle', '.model', '.model2', '.mge', '.joblib']);
  5250. this.register('./lasagne', ['.pkl', '.pickle', '.joblib', '.model', '.pkl.z', '.joblib.z']);
  5251. this.register('./lightgbm', ['.txt', '.pkl', '.model']);
  5252. this.register('./sklearn', ['.pkl', '.pickle', '.joblib', '.model', '.meta', '.pb', '.pt', '.h5', '.pkl.z', '.joblib.z', '.pickle.dat', '.bin']);
  5253. this.register('./megengine', ['.tm', '.mge', '.pkl']);
  5254. this.register('./pickle', ['.pkl', '.pickle', '.joblib', '.model', '.meta', '.pb', '.pt', '.h5', '.pkl.z', '.joblib.z', '.pdstates', '.mge', '.bin']);
  5255. this.register('./cntk', ['.model', '.cntk', '.cmf', '.dnn']);
  5256. this.register('./uff', ['.uff', '.pb', '.pbtxt', '.uff.txt', '.trt', '.engine']);
  5257. this.register('./paddle', ['.pdmodel', '.pdiparams', '.pdparams', '.pdopt', '.paddle', '__model__', '.__model__', '.pbtxt', '.txt', '.tar', '.tar.gz', '.nb']);
  5258. this.register('./bigdl', ['.model', '.bigdl']);
  5259. this.register('./darknet', ['.cfg', '.model', '.txt', '.weights']);
  5260. this.register('./mediapipe', ['.pbtxt']);
  5261. this.register('./rknn', ['.rknn', '.nb', '.onnx', '.json', '.bin']);
  5262. this.register('./dlc', ['.dlc', 'model', '.params']);
  5263. this.register('./armnn', ['.armnn', '.json']);
  5264. this.register('./mnn', ['.mnn']);
  5265. this.register('./ncnn', ['.param', '.bin', '.cfg.ncnn', '.weights.ncnn', '.ncnnmodel']);
  5266. this.register('./tnn', ['.tnnproto', '.tnnmodel']);
  5267. this.register('./tengine', ['.tmfile']);
  5268. this.register('./mslite', ['.ms', '.bin']);
  5269. this.register('./barracuda', ['.nn']);
  5270. this.register('./circle', ['.circle']);
  5271. this.register('./dnn', ['.dnn']);
  5272. this.register('./xmodel', ['.xmodel']);
  5273. this.register('./kmodel', ['.kmodel']);
  5274. this.register('./flux', ['.bson']);
  5275. this.register('./dl4j', ['.json', '.bin']);
  5276. this.register('./openvino', ['.xml', '.bin']);
  5277. this.register('./mlnet', ['.zip', '.mlnet']);
  5278. this.register('./acuity', ['.json']);
  5279. this.register('./imgdnn', ['.dnn', 'params', '.json']);
  5280. this.register('./flax', ['.msgpack']);
  5281. this.register('./om', ['.om', '.onnx', '.pb', '.engine', '.bin']);
  5282. this.register('./gguf', ['.gguf', /^[^.]+$/]);
  5283. this.register('./nnabla', ['.nntxt'], ['.nnp']);
  5284. this.register('./hickle', ['.h5', '.hkl']);
  5285. this.register('./nnef', ['.nnef', '.dat']);
  5286. this.register('./onednn', ['.json']);
  5287. this.register('./espresso', ['.espresso.net', '.espresso.shape', '.espresso.weights'], ['.mlmodelc']);
  5288. this.register('./mlir', ['.mlir', '.mlir.txt']);
  5289. this.register('./sentencepiece', ['.model']);
  5290. this.register('./hailo', ['.hn', '.har', '.metadata.json']);
  5291. this.register('./nnc', ['.nnc']);
  5292. this.register('./safetensors', ['.safetensors', '.safetensors.index.json']);
  5293. this.register('./modular', ['.maxviz']);
  5294. this.register('./catboost', ['.cbm']);
  5295. this.register('./cambricon', ['.cambricon']);
  5296. this.register('./weka', ['.model']);
  5297. this.register('./qnn', ['.json', '.bin']);
  5298. }
  5299. register(module, factories, containers) {
  5300. for (const pattern of factories) {
  5301. this._factories.push({ pattern, module });
  5302. this._patterns.add(pattern);
  5303. }
  5304. for (const pattern of containers || []) {
  5305. this._patterns.add(pattern);
  5306. }
  5307. }
  5308. async open(context) {
  5309. try {
  5310. await this._openSignature(context);
  5311. const content = new view.Context(context);
  5312. const model = await this._openContext(content);
  5313. if (!model) {
  5314. const check = (obj) => {
  5315. if (obj instanceof Error) {
  5316. throw obj;
  5317. }
  5318. return obj instanceof Map && obj.size > 0;
  5319. };
  5320. let entries = context.entries;
  5321. if (!check(entries)) {
  5322. entries = content.peek('zip');
  5323. if (!check(entries)) {
  5324. entries = content.peek('tar');
  5325. if (!check(entries)) {
  5326. entries = content.peek('gzip');
  5327. }
  5328. }
  5329. }
  5330. if (!check(entries)) {
  5331. this._unsupported(content);
  5332. }
  5333. const entryContext = await this._openEntries(entries);
  5334. if (!entryContext) {
  5335. this._unsupported(content);
  5336. }
  5337. return this._openContext(entryContext);
  5338. }
  5339. return model;
  5340. } catch (error) {
  5341. error.context = !error.context && context && context.identifier ? context.identifier : error.context || '';
  5342. throw error;
  5343. }
  5344. }
  5345. _unsupported(context) {
  5346. const identifier = context.identifier;
  5347. const extension = identifier.split('.').pop().toLowerCase();
  5348. const stream = context.stream;
  5349. const callbacks = [
  5350. (stream) => zip.Archive.open(stream, 'zip'),
  5351. (stream) => tar.Archive.open(stream),
  5352. (stream) => zip.Archive.open(stream, 'gzip')
  5353. ];
  5354. for (const callback of callbacks) {
  5355. let archive = null;
  5356. try {
  5357. archive = callback(stream);
  5358. } catch {
  5359. // continue regardless of error
  5360. }
  5361. if (archive) {
  5362. throw new view.Error("Archive contains no model files.");
  5363. }
  5364. }
  5365. const json = () => {
  5366. const obj = context.peek('json');
  5367. if (obj) {
  5368. const formats = [
  5369. { name: 'Netron metadata', tags: ['[].name', '[].schema'] },
  5370. { name: 'Netron metadata', tags: ['[].name', '[].attributes'] },
  5371. { name: 'Netron metadata', tags: ['[].name', '[].category'] },
  5372. { name: 'Netron test data', tags: ['[].type', '[].target', '[].source', '[].format', '[].link'] },
  5373. { name: 'Netron configuration', tags: ['recents', 'consent'] },
  5374. { name: 'Darkflow metadata', tags: ['net', 'type', 'model'] },
  5375. { name: 'keras-yolo2 configuration', tags: ['model', 'train', 'valid'] },
  5376. { name: 'Vulkan SwiftShader ICD manifest', tags: ['file_format_version', 'ICD'] },
  5377. { name: 'DeepLearningExamples configuration', tags: ['attention_probs_dropout_prob', 'hidden_act', 'hidden_dropout_prob', 'hidden_size',] },
  5378. { name: 'GitHub page data', tags: ['payload', 'title'] },
  5379. { name: 'NuGet assets', tags: ['version', 'targets', 'packageFolders'] },
  5380. { name: 'NuGet data', tags: ['format', 'restore', 'projects'] },
  5381. { name: 'NPM package', tags: ['name', 'version', 'dependencies'] },
  5382. { name: 'NetworkX adjacency_data', tags: ['directed', 'graph', 'nodes'] },
  5383. { name: 'Waifu2x data', tags: ['name', 'arch_name', 'channels'] },
  5384. { name: 'Waifu2x data', tags: ['[].nInputPlane', '[].nOutputPlane', '[].weight', '[].bias'] },
  5385. { name: 'Brain.js data', tags: ['type', 'sizes', 'layers'] },
  5386. { name: 'Custom Vision metadata', tags: ['CustomVision.Metadata.Version'] },
  5387. { name: 'W&B metadata', tags: ['program', 'host', 'executable'] },
  5388. { name: 'TypeScript configuration data', tags: ['compilerOptions'] },
  5389. { name: 'CatBoost model', tags: ['features_info', 'model_info'] },
  5390. { 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
  5391. { name: 'HTTP Archive data', tags: ['log.version', 'log.creator', 'log.entries'] }, // https://w3c.github.io/web-performance/specs/HAR/Overview.html
  5392. { name: 'Trace Event data', tags: ['traceEvents'] },
  5393. { name: 'Trace Event data', tags: ['[].pid', '[].ph'] },
  5394. { name: 'Diffusers configuration', tags: ['_class_name', '_diffusers_version'] },
  5395. { name: 'Transformers configuration', tags: ['architectures', 'model_type'] }, // https://huggingface.co/docs/transformers/en/create_a_model
  5396. { name: 'Transformers generation configuration', tags: ['transformers_version'] },
  5397. { name: 'Transformers tokenizer configuration', tags: ['tokenizer_class'] },
  5398. { name: 'Transformers tokenizer configuration', tags: ['<|im_start|>'] },
  5399. { name: 'Transformers preprocessor configuration', tags: ['crop_size', 'do_center_crop', 'image_mean', 'image_std', 'do_resize'] },
  5400. { name: 'Tokenizers data', tags: ['version', 'added_tokens', 'model'] }, // https://github.com/huggingface/tokenizers/blob/main/tokenizers/src/tokenizer/serialization.rs
  5401. { name: 'Jupyter Notebook data', tags: ['cells', 'nbformat'] },
  5402. { name: 'Kaggle credentials', tags: ['username','key'] },
  5403. { name: '.NET runtime configuration', tags: ['runtimeOptions.configProperties'] },
  5404. { name: '.NET dependency manifest', tags: ['runtimeTarget', 'targets', 'libraries'] }
  5405. ];
  5406. const match = (obj, tag) => {
  5407. if (tag.startsWith('[].')) {
  5408. tag = tag.substring(3);
  5409. return (Array.isArray(obj) && obj.some((item) => Object.prototype.hasOwnProperty.call(item, tag)));
  5410. }
  5411. tag = tag.split('.');
  5412. while (tag.length > 1) {
  5413. const key = tag.shift();
  5414. obj = obj[key];
  5415. if (!obj) {
  5416. return false;
  5417. }
  5418. }
  5419. return Object.prototype.hasOwnProperty.call(obj, tag[0]);
  5420. };
  5421. for (const format of formats) {
  5422. if (format.tags.every((tag) => match(obj, tag))) {
  5423. throw new view.Error(`Invalid file content. File contains ${format.name}.`);
  5424. }
  5425. }
  5426. const content = `${JSON.stringify(obj).substring(0, 100).replace(/\s/, '').substring(0, 48)}...`;
  5427. throw new view.Error(`Unsupported JSON content '${content.length > 64 ? `${content.substring(0, 100)}...` : content}' for extension '.${extension}'.`);
  5428. }
  5429. };
  5430. const pbtxt = () => {
  5431. const formats = [
  5432. { name: 'ImageNet LabelMap data', tags: ['entry', 'entry.target_class'] },
  5433. { name: 'StringIntLabelMapProto data', tags: ['item', 'item.id', 'item.name'] },
  5434. { name: 'caffe.LabelMap data', tags: ['item', 'item.name', 'item.label'] },
  5435. { name: 'Triton Inference Server configuration', tags: ['name', 'platform', 'input', 'output'] },
  5436. { name: 'TensorFlow OpList data', tags: ['op', 'op.name', 'op.input_arg'] },
  5437. { name: 'vitis.ai.proto.DpuModelParamList data', tags: ['model', 'model.name', 'model.kernel'] },
  5438. { name: 'object_detection.protos.DetectionModel data', tags: ['model', 'model.ssd'] },
  5439. { name: 'object_detection.protos.DetectionModel data', tags: ['model', 'model.faster_rcnn'] },
  5440. { name: 'tensorflow.CheckpointState data', tags: ['model_checkpoint_path', 'all_model_checkpoint_paths'] },
  5441. { name: 'apollo.perception.camera.traffic_light.detection.DetectionParam data', tags: ['min_crop_size', 'crop_method'] },
  5442. { name: 'tidl_meta_arch.TIDLMetaArch data', tags: ['caffe_ssd'] }, // https://github.com/TexasInstruments/edgeai-mmdetection/blob/master/mmdet/utils/proto/mmdet_meta_arch.proto
  5443. { name: 'tidl_meta_arch.TIDLMetaArch data', tags: ['tf_od_api_ssd'] },
  5444. { name: 'tidl_meta_arch.TIDLMetaArch data', tags: ['tidl_ssd'] },
  5445. { name: 'tidl_meta_arch.TIDLMetaArch data', tags: ['tidl_faster_rcnn'] },
  5446. { name: 'tidl_meta_arch.TIDLMetaArch data', tags: ['tidl_yolo'] },
  5447. { name: 'tidl_meta_arch.TIDLMetaArch data', tags: ['tidl_retinanet'] },
  5448. { name: 'domi.InsertNewOps data', tags: ['aipp_op'] } // https://github.com/Ascend/parser/blob/development/parser/proto/insert_op.proto
  5449. ];
  5450. const tags = context.tags('pbtxt');
  5451. if (tags.size > 0) {
  5452. for (const format of formats) {
  5453. if (format.tags.every((tag) => tags.has(tag))) {
  5454. const error = new view.Error(`Invalid file content. File contains ${format.name}.`);
  5455. error.context = context.identifier;
  5456. throw error;
  5457. }
  5458. }
  5459. const entries = [];
  5460. entries.push(...Array.from(tags).filter(([key]) => key.toString().indexOf('.') === -1));
  5461. entries.push(...Array.from(tags).filter(([key]) => key.toString().indexOf('.') !== -1));
  5462. const content = entries.map(([key, value]) => value === true ? key : `${key}:${JSON.stringify(value)}`).join(',');
  5463. throw new view.Error(`Unsupported Protocol Buffers text content '${content.length > 64 ? `${content.substring(0, 100)}...` : content}' for extension '.${extension}'.`);
  5464. }
  5465. };
  5466. const pb = () => {
  5467. const tags = context.tags('pb+');
  5468. if (Object.keys(tags).length > 0) {
  5469. const formats = [
  5470. { 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,[]]] },
  5471. { 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]] },
  5472. { name: 'third_party.tensorflow.python.keras.protobuf.SavedMetadata data', tags: [[1,[[1,[[1,0],[2,0]]],[2,0],[3,2],[4,2],[5,2]]]] },
  5473. { 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
  5474. { 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
  5475. { 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
  5476. { 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
  5477. { 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
  5478. { 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
  5479. { name: 'Hobot Dnn data', tags: [[1,0],[2,0],[4,[[1,2],[2,2]]]] }, // https://github.com/HorizonRDK/hobot_dnn
  5480. { name: 'Hobot Dnn data', tags: [[1,0],[2,0],[6,[1,[[1,2],[2,2]]]]] }, // https://github.com/HorizonRDK/hobot_dnn
  5481. ];
  5482. const match = (tags, schema) => {
  5483. for (const [key, inner] of schema) {
  5484. const value = tags[key];
  5485. if (value === undefined) {
  5486. continue;
  5487. }
  5488. if (inner === false) {
  5489. return false;
  5490. }
  5491. if (Array.isArray(inner)) {
  5492. if (typeof value !== 'object' || !match(value, inner)) {
  5493. return false;
  5494. }
  5495. } else if (inner !== value) {
  5496. if (inner === 2 && !Array.isArray(value) && Object(value) === (value) && Object.keys(value).length === 0) {
  5497. return true;
  5498. }
  5499. return false;
  5500. }
  5501. }
  5502. return true;
  5503. };
  5504. for (const format of formats) {
  5505. if (match(tags, format.tags) && (!format.identifier || identifier === context.identifier)) {
  5506. const error = new view.Error(`Invalid file content. File contains ${format.name}.`);
  5507. error.context = context.identifier;
  5508. throw error;
  5509. }
  5510. }
  5511. const format = (tags) => {
  5512. const content = Object.entries(tags).map(([key, value]) => {
  5513. return `${key}:${Object(value) === value ? `{${format(value)}}` : value}`;
  5514. });
  5515. return content.join(',');
  5516. };
  5517. const content = format(tags);
  5518. throw new view.Error(`Unsupported Protocol Buffers content '${content.length > 64 ? `${content.substring(0, 100)}...` : content}' for extension '.${extension}'.`);
  5519. }
  5520. };
  5521. const flatbuffers = () => {
  5522. const stream = context.stream;
  5523. if (stream && stream.length >= 8) {
  5524. let identifier = null;
  5525. const reader = context.peek('flatbuffers.binary');
  5526. if (reader) {
  5527. identifier = reader.identifier;
  5528. } else {
  5529. const data = stream.peek(8);
  5530. if ((data[0] === 0x08 || data[0] === 0x18 || data[0] === 0x1C) && data[1] === 0x00 && data[2] === 0x00 && data[2] === 0x00) {
  5531. identifier = String.fromCharCode.apply(null, data.slice(4, 8));
  5532. }
  5533. }
  5534. if (identifier) {
  5535. const formats = [
  5536. { name: 'ONNX Runtime model data', identifier: 'ORTM' },
  5537. { name: 'TensorFlow Lite model data', identifier: 'TFL3' }
  5538. ];
  5539. for (const format of formats) {
  5540. if (identifier === format.identifier) {
  5541. throw new view.Error(`Invalid file content. File contains ${format.name}.`);
  5542. }
  5543. }
  5544. }
  5545. }
  5546. };
  5547. const xml = () => {
  5548. const tags = context.tags('xml');
  5549. if (tags.size > 0) {
  5550. const formats = [
  5551. { name: 'OpenCV storage data', tags: ['opencv_storage'] },
  5552. { name: 'XHTML markup', tags: ['http://www.w3.org/1999/xhtml:html'] }
  5553. ];
  5554. for (const format of formats) {
  5555. if (format.tags.some((tag) => tags.has(tag))) {
  5556. const error = new view.Error(`Invalid file content. File contains ${format.name}.`);
  5557. error.content = context.identifier;
  5558. throw error;
  5559. }
  5560. }
  5561. throw new view.Error(`Unsupported XML content '${tags.keys().next().value}'.`);
  5562. }
  5563. };
  5564. const hdf5 = () => {
  5565. const obj = context.peek('hdf5');
  5566. if (obj instanceof Error) {
  5567. throw obj;
  5568. }
  5569. if (obj) {
  5570. throw new view.Error(`Invalid file content. File contains HDF5 content.`);
  5571. }
  5572. };
  5573. const unknown = () => {
  5574. if (stream) {
  5575. stream.seek(0);
  5576. const buffer = stream.peek(Math.min(16, stream.length));
  5577. const bytes = Array.from(buffer).map((c) => (c < 16 ? '0' : '') + c.toString(16)).join('');
  5578. const content = stream.length > 268435456 ? `(${bytes}) [${stream.length}]` : `(${bytes})`;
  5579. throw new view.Error(`Unsupported file content ${content} for extension '.${extension}'.`);
  5580. }
  5581. throw new view.Error("Unsupported file directory.");
  5582. };
  5583. json();
  5584. pbtxt();
  5585. pb();
  5586. flatbuffers();
  5587. xml();
  5588. hdf5();
  5589. unknown();
  5590. }
  5591. async _require(id) {
  5592. const module = await this._host.require(id);
  5593. if (!module || !module.ModelFactory) {
  5594. throw new view.Error(`Failed to load module '${id}'.`);
  5595. }
  5596. return new module.ModelFactory();
  5597. }
  5598. async _openContext(context) {
  5599. const modules = this._filter(context).filter((module) => module && module.length > 0);
  5600. const errors = [];
  5601. for (const module of modules) {
  5602. /* eslint-disable no-await-in-loop */
  5603. const factory = await this._require(module);
  5604. /* eslint-enable no-await-in-loop */
  5605. factory.match(context);
  5606. if (context.stream && context.stream.position !== 0) {
  5607. throw new view.Error('Invalid stream position.');
  5608. }
  5609. if (context.type) {
  5610. try {
  5611. /* eslint-disable no-await-in-loop */
  5612. const model = await factory.open(context);
  5613. /* eslint-enable no-await-in-loop */
  5614. if (!model.identifier) {
  5615. model.identifier = context.identifier;
  5616. }
  5617. return model;
  5618. } catch (error) {
  5619. delete context.type;
  5620. delete context.target;
  5621. if (context.stream && context.stream.position !== 0) {
  5622. context.stream.seek(0);
  5623. }
  5624. errors.push(error);
  5625. }
  5626. }
  5627. if (context.stream && context.stream.position !== 0) {
  5628. throw new view.Error('Invalid stream position.');
  5629. }
  5630. }
  5631. if (errors.length > 0) {
  5632. if (errors.length === 1) {
  5633. throw errors[0];
  5634. }
  5635. throw new view.Error(errors.map((err) => err.message).join('\n'));
  5636. }
  5637. return null;
  5638. }
  5639. async _openEntries(entries) {
  5640. try {
  5641. const rootFolder = (files) => {
  5642. const map = files.map((file) => file.split('/').slice(0, -1));
  5643. const at = (index) => (list) => list[index];
  5644. const rotate = (list) => list.length === 0 ? [] : list[0].map((item, index) => list.map(at(index)));
  5645. const equals = (list) => list.every((item) => item === list[0]);
  5646. const folder = rotate(map).filter(equals).map(at(0)).join('/');
  5647. return folder.length === 0 ? folder : `${folder}/`;
  5648. };
  5649. const files = Array.from(entries).filter(([name]) => !(name.endsWith('/') || name.split('/').pop().startsWith('.') || (!name.startsWith('./') && name.startsWith('.'))));
  5650. const folder = rootFolder(files.map(([name]) => name));
  5651. const filter = async (queue, entries) => {
  5652. entries = new Map(Array.from(entries)
  5653. .filter(([path]) => path.startsWith(folder))
  5654. .map(([path, stream]) => [path.substring(folder.length), stream]));
  5655. const entryContext = new view.EntryContext(this._host, entries);
  5656. let matches = [];
  5657. for (const [name, stream] of queue) {
  5658. const identifier = name.substring(folder.length);
  5659. const context = new view.Context(entryContext, identifier, stream);
  5660. const modules = this._filter(context);
  5661. for (const module of modules) {
  5662. /* eslint-disable no-await-in-loop */
  5663. const factory = await this._require(module);
  5664. /* eslint-enable no-await-in-loop */
  5665. factory.match(context);
  5666. if (context.stream && context.stream.position !== 0) {
  5667. throw new view.Error('Invalid stream position.');
  5668. }
  5669. delete context.target;
  5670. if (context.type) {
  5671. matches = matches.filter((match) => !factory.filter || factory.filter(context, match.type));
  5672. if (matches.every((match) => !match.factory.filter || match.factory.filter(match, context.type))) {
  5673. context.factory = factory;
  5674. matches.push(context);
  5675. }
  5676. break;
  5677. }
  5678. }
  5679. }
  5680. if (matches.length > 1) {
  5681. const content = matches.map((context) => context.type).join(',');
  5682. throw new view.ArchiveError(`Archive contains multiple model files '${content}'.`);
  5683. }
  5684. if (matches.length > 0) {
  5685. const match = matches.shift();
  5686. delete match.type;
  5687. delete match.factory;
  5688. return match;
  5689. }
  5690. return null;
  5691. };
  5692. const queue = files.filter(([name]) => name.substring(folder.length).indexOf('/') < 0);
  5693. let context = await filter(queue, entries);
  5694. if (!context) {
  5695. const queue = files.filter(([name]) => name.substring(folder.length).indexOf('/') >= 0);
  5696. context = await filter(queue, entries);
  5697. }
  5698. return context;
  5699. } catch (error) {
  5700. throw new view.ArchiveError(error.message);
  5701. }
  5702. }
  5703. accept(identifier, size) {
  5704. const extension = identifier.indexOf('.') === -1 ? '' : identifier.split('.').pop().toLowerCase();
  5705. identifier = identifier.toLowerCase().split('/').pop();
  5706. let accept = false;
  5707. for (const extension of this._patterns) {
  5708. if ((typeof extension === 'string' && identifier.endsWith(extension)) ||
  5709. (extension instanceof RegExp && extension.exec(identifier))) {
  5710. accept = true;
  5711. break;
  5712. }
  5713. }
  5714. this._host.event('model_file', {
  5715. file_extension: extension,
  5716. file_size: size || 0,
  5717. file_accept: accept ? 1 : 0
  5718. });
  5719. return accept;
  5720. }
  5721. _filter(context) {
  5722. const identifier = context.identifier.toLowerCase().split('/').pop();
  5723. const list = this._factories.filter((entry) =>
  5724. (typeof entry.pattern === 'string' && identifier.endsWith(entry.pattern)) ||
  5725. (entry.pattern instanceof RegExp && entry.pattern.test(identifier)));
  5726. return Array.from(new Set(list.map((entry) => entry.module)));
  5727. }
  5728. async _openSignature(context) {
  5729. const stream = context.stream;
  5730. if (stream) {
  5731. let empty = true;
  5732. let position = 0;
  5733. while (position < stream.length) {
  5734. const buffer = stream.read(Math.min(4096, stream.length - position));
  5735. position += buffer.length;
  5736. if (!buffer.every((value) => value === 0x00)) {
  5737. empty = false;
  5738. break;
  5739. }
  5740. }
  5741. stream.seek(0);
  5742. if (empty) {
  5743. throw new view.Error('File has no content.');
  5744. }
  5745. /* eslint-disable no-control-regex */
  5746. const entries = [
  5747. { name: 'ELF executable', value: /^\x7FELF/ },
  5748. { name: 'PNG image', value: /^\x89PNG/ },
  5749. { name: 'Git LFS header', value: /^version https:\/\/git-lfs.github.com/ },
  5750. { name: 'Git LFS header', value: /^\s*oid sha256:/ },
  5751. { name: 'GGML data', value: /^lmgg|fmgg|tjgg|algg|fugg/ },
  5752. { name: 'HTML markup', value: /^\s*<html(\s+[^>]+)?>/ },
  5753. { name: 'HTML markup', value: /^\s*<!doctype\s*html>/ },
  5754. { name: 'HTML markup', value: /^\s*<!DOCTYPE\s*html>/ },
  5755. { name: 'HTML markup', value: /^\s*<!DOCTYPE\s*HTML>/ },
  5756. { name: 'HTML markup', value: /^\s*<!DOCTYPE\s*HTML\s+(PUBLIC|SYSTEM)?/ },
  5757. { name: 'Unity metadata', value: /^fileFormatVersion:/ },
  5758. { 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))/ },
  5759. { 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*)/ },
  5760. { name: 'Python virtual environment configuration', value: /^home[ ]*=[ ]*/, identifier: 'pyvenv.cfg' },
  5761. { name: 'Bash script', value: /^#!\/usr\/bin\/env\s/ },
  5762. { name: 'Bash script', value: /^#!\/bin\/bash\s/ },
  5763. { name: 'TSD header', value: /^%TSD-Header-###%/ },
  5764. { name: 'AppleDouble data', value: /^\x00\x05\x16\x07/ },
  5765. { name: 'TensorFlow Hub module', value: /^\x08\x03$/, identifier: 'tfhub_module.pb' },
  5766. { name: 'V8 snapshot', value: /^.\x00\x00\x00.\x00\x00\x00/, identifier: 'snapshot_blob.bin' },
  5767. { name: 'V8 context snapshot', value: /^.\x00\x00\x00.\x00\x00\x00/, identifier: 'v8_context_snapshot.bin' },
  5768. { name: 'V8 natives blob', value: /^./, identifier: 'natives_blob.bin' },
  5769. { name: 'ViSQOL model', value: /^svm_type\s/ },
  5770. { name: 'SenseTime model', value: /^STEF/ },
  5771. { name: 'AES Crypt data', value: /^AES[\x01|\x02]\x00/ },
  5772. { name: 'BModel data', value: /^\xEE\xAA\x55\xFF/ }, // https://github.com/sophgo/tpu-mlir/blob/master/include/tpu_mlir/Builder/BM168x/bmodel.fbs
  5773. { name: 'CviModel data', value: /^CviModel/ }, // https://github.com/sophgo/tpu-mlir/blob/master/include/tpu_mlir/Builder/CV18xx/proto/cvimodel.fbs
  5774. { name: 'Tokenizer data', value: /^IQ== 0\n/ }
  5775. ];
  5776. /* eslint-enable no-control-regex */
  5777. const buffer = stream.peek(Math.min(4096, stream.length));
  5778. const content = String.fromCharCode.apply(null, buffer);
  5779. for (const entry of entries) {
  5780. if (content.match(entry.value) && (!entry.identifier || entry.identifier === context.identifier)) {
  5781. throw new view.Error(`Invalid file content. File contains ${entry.name}.`);
  5782. }
  5783. }
  5784. }
  5785. }
  5786. async import() {
  5787. if (this._host.type === 'Browser' || this._host.type === 'Python') {
  5788. const files = [
  5789. './server', './onnx', './pytorch', './tflite', './mlnet',
  5790. './onnx-proto', './onnx-schema', './tflite-schema',
  5791. 'onnx-metadata.json', 'pytorch-metadata.json', 'tflite-metadata.json'
  5792. ];
  5793. for (const file of files) {
  5794. /* eslint-disable no-await-in-loop */
  5795. try {
  5796. if (file.startsWith('./')) {
  5797. await this._host.require(file);
  5798. } else if (file.endsWith('.json')) {
  5799. await this._host.request(file, 'utf-8', null);
  5800. }
  5801. } catch {
  5802. // continue regardless of error
  5803. }
  5804. /* eslint-enable no-await-in-loop */
  5805. }
  5806. }
  5807. }
  5808. };
  5809. view.Metadata = class {
  5810. static async open(context, name) {
  5811. view.Metadata._metadata = view.Metadata._metadata || new Map();
  5812. const metadata = view.Metadata._metadata;
  5813. if (!metadata.has(name)) {
  5814. try {
  5815. const content = await context.request(name);
  5816. const types = JSON.parse(content);
  5817. metadata.set(name, new view.Metadata(types));
  5818. } catch {
  5819. metadata.set(name, new view.Metadata(null));
  5820. }
  5821. }
  5822. return metadata.get(name);
  5823. }
  5824. constructor(types) {
  5825. this._types = new Map();
  5826. this._attributes = new Map();
  5827. this._inputs = new Map();
  5828. if (Array.isArray(types)) {
  5829. for (const type of types) {
  5830. this._types.set(type.name, type);
  5831. if (type.identifier !== undefined) {
  5832. this._types.set(type.identifier, type);
  5833. }
  5834. }
  5835. }
  5836. }
  5837. type(name) {
  5838. if (!this._types.has(name)) {
  5839. this._types.set(name, { name: name.toString() });
  5840. }
  5841. return this._types.get(name);
  5842. }
  5843. attribute(type, name) {
  5844. const key = `${type}:${name}`;
  5845. if (!this._attributes.has(key)) {
  5846. this._attributes.set(key, null);
  5847. const metadata = this.type(type);
  5848. if (metadata && Array.isArray(metadata.attributes)) {
  5849. for (const attribute of metadata.attributes) {
  5850. this._attributes.set(`${type}:${attribute.name}`, attribute);
  5851. }
  5852. }
  5853. }
  5854. return this._attributes.get(key);
  5855. }
  5856. input(type, name) {
  5857. const key = `${type}:${name}`;
  5858. if (!this._inputs.has(key)) {
  5859. this._inputs.set(key, null);
  5860. const metadata = this.type(type);
  5861. if (metadata && Array.isArray(metadata.inputs)) {
  5862. for (const input of metadata.inputs) {
  5863. this._inputs.set(`${type}:${input.name}`, input);
  5864. }
  5865. }
  5866. }
  5867. return this._inputs.get(key);
  5868. }
  5869. };
  5870. view.Error = class extends Error {
  5871. constructor(message) {
  5872. super(message);
  5873. this.name = 'Error loading model.';
  5874. }
  5875. };
  5876. if (typeof window !== 'undefined' && window.exports) {
  5877. window.exports.view = view;
  5878. }
  5879. export const View = view.View;
  5880. export const ModelFactoryService = view.ModelFactoryService;
  5881. export const ModelSidebar = view.ModelSidebar;
  5882. export const NodeSidebar = view.NodeSidebar;
  5883. export const TensorSidebar = view.TensorSidebar;
  5884. export const Documentation = view.Documentation;
  5885. export const Formatter = view.Formatter;
  5886. export const Tensor = view.Tensor;
  5887. export const Quantization = view.Quantization;