-
Notifications
You must be signed in to change notification settings - Fork 10
/
LanguageController.ts
1162 lines (1032 loc) · 50.8 KB
/
LanguageController.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
Address, Expression, Language, LanguageContext, LinkSyncAdapter, InteractionCall, InteractionMeta,
PublicSharing, ReadOnlyLanguage, LanguageMetaInternal, LanguageMetaInput, PerspectiveExpression,
parseExprUrl, Literal, TelepresenceAdapter, PerspectiveState
} from '@coasys/ad4m';
import { ExpressionRef, LanguageRef, LanguageExpression, LanguageLanguageInput, ExceptionType, PerspectiveDiff } from '@coasys/ad4m';
import { ExceptionInfo } from '@coasys/ad4m/lib/src/runtime/RuntimeResolver';
import fs from 'node:fs'
import path from 'node:path'
import * as Config from './Config'
import type HolochainService from './storage-services/Holochain/HolochainService';
import type AgentService from './agent/AgentService'
import * as PubSubDefinitions from './graphQL-interface/SubscriptionDefinitions'
import yaml from "js-yaml";
import { v4 as uuidv4 } from 'uuid';
import RuntimeService from './RuntimeService';
import { Ad4mDb } from './db';
import stringify from 'json-stable-stringify'
import { getPubSub, tagExpressionSignatureStatus } from './utils';
function cloneWithoutCircularReferences(obj: any, seen: WeakSet<any> = new WeakSet()): any {
if (typeof obj === 'object' && obj !== null) {
if (seen.has(obj)) {
return;
}
seen.add(obj);
const clonedObj: any = Array.isArray(obj) ? [] : {};
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
clonedObj[key] = cloneWithoutCircularReferences(obj[key], seen);
}
}
return clonedObj;
}
return obj;
}
type LinkObservers = (diff: PerspectiveDiff, lang: LanguageRef)=>void;
type TelepresenceSignalObserver = (signal: PerspectiveExpression, lang: LanguageRef)=>void;
type SyncStateChangeObserver = (state: PerspectiveState, lang: LanguageRef)=>void;
interface Services {
holochainService: HolochainService,
runtimeService: RuntimeService,
db: Ad4mDb
}
const importModule = async (modulePath: string) => {
// To deal with ESM on windows requires absolute path and file protocol
if (process.platform === "win32") {
const path = `file:\\\\${modulePath}`
return await import(path)
}
return await import(`file://${modulePath}`)
}
const loadModule = async (modulePath: string) => {
// Check if the file exists
try {
//@ts-ignore
await Deno.stat(modulePath);
} catch (err) {
//@ts-ignore
if (err instanceof Deno.errors.NotFound) {
throw new Error(`File not found: ${modulePath}`);
}
throw err;
}
const res = await UTILS.loadModule(`file://${modulePath}`);
return await importModule(modulePath)
}
export default class LanguageController {
#languages: Map<string, Language>
#languageConstructors: Map<string, (context: LanguageContext)=>Language>
#context: object;
#linkObservers: LinkObservers[];
#telepresenceSignalObservers: TelepresenceSignalObserver[];
#syncStateChangeObservers: SyncStateChangeObserver[];
#holochainService: HolochainService
#runtimeService: RuntimeService;
#db: Ad4mDb;
#config: Config.MainConfig;
#pubSub: PubSub;
#agentLanguage?: Language
#languageLanguage?: Language
#neighbourhoodLanguage?: Language
#perspectiveLanguage?: Language
constructor(context: object, services: Services) {
this.#context = context
this.#holochainService = services.holochainService
this.#runtimeService = services.runtimeService
this.#db = services.db
this.#languages = new Map()
this.#languages.set("literal", {
name: "literal",
interactions() { return [] },
} as Language)
this.#languageConstructors = new Map()
this.#linkObservers = []
this.#telepresenceSignalObservers = []
this.#syncStateChangeObservers = []
this.#config = (context as any).config;
this.#pubSub = getPubSub();
}
async loadLanguages() {
await this.loadSystemLanguages()
if (!this.#config.languageLanguageOnly) await this.loadInstalledLanguages()
}
async loadSystemLanguages() {
//Install language language from the bundle file and then update languageAliases to point to language hash
const { sourcePath, hash: calculatedHash } = await this.saveLanguageBundle(this.#config.languageLanguageBundle);
if (this.#config.languageLanguageSettings) {
console.log("LanguageController.loadSystemLanguages: Found settings for languageLanguage, writting settings");
this.writeSettings(calculatedHash, this.#config.languageLanguageSettings);
}
const { hash, language: languageLanguage } = await this.loadLanguage(sourcePath);
this.#config.languageAliases[Config.languageLanguageAlias] = hash;
this.#languageLanguage = languageLanguage!;
if (!this.#config.languageLanguageOnly) {
//Install the agent language and set
if (this.#config.agentLanguageSettings) {
console.log("LanguageController.loadSystemLanguages: Found settings for agentLanguage, writting settings");
this.writeSettings(this.#config.languageAliases[Config.agentLanguageAlias], this.#config.agentLanguageSettings);
}
const agentLanguage = await this.installLanguage(this.#config.languageAliases[Config.agentLanguageAlias], null);
this.#agentLanguage = agentLanguage!;
((this.#context as LanguageContext).agent as AgentService).setAgentLanguage(agentLanguage!)
//Install the neighbourhood language and set
if (this.#config.neighbourhoodLanguageSettings) {
console.log("LanguageController.loadSystemLanguages: Found settings for neighbourhoodLanguage, writting settings");
this.writeSettings(this.#config.languageAliases[Config.neighbourhoodLanguageAlias], this.#config.neighbourhoodLanguageSettings);
}
const neighbourhoodLanguage = await this.installLanguage(this.#config.languageAliases[Config.neighbourhoodLanguageAlias], null);
this.#neighbourhoodLanguage = neighbourhoodLanguage!;
//Install the perspective language and set
if (this.#config.perspectiveLanguageSettings) {
console.log("LanguageController.loadSystemLanguages: Found settings for a perspectiveLanguage, writting settings")
this.writeSettings(this.#config.languageAliases[Config.perspectiveLanguageAlias], this.#config.perspectiveLanguageSettings);
}
const perspectiveLanguage = await this.installLanguage(this.#config.languageAliases[Config.perspectiveLanguageAlias], null);
this.#perspectiveLanguage = perspectiveLanguage!;
//Install preload languages
await Promise.all(this.#config.preloadLanguages.map(async hash => {
await this.installLanguage(hash, null);
}))
//Load bootstrap languages
if (this.#config.bootstrapFixtures) {
if (this.#config.bootstrapFixtures!.languages) {
await Promise.all(this.#config.bootstrapFixtures!.languages!.map(async file => {
const { sourcePath } = await this.saveLanguageBundle(this.#config.languageLanguageBundle);
await this.loadLanguage(sourcePath);
}))
}
}
}
}
async loadInstalledLanguages() {
const files = fs.readdirSync(this.#config.languagesPath)
return Promise.all(files.map(async file => {
//Ensure we do not loaded previously loaded system languages again
if (!this.#config.systemLanguages.find((lang) => lang === file) && !this.#config.preloadLanguages.find((lang) => lang === file)) {
const bundlePath = path.join(this.#config.languagesPath, file, 'bundle.js')
if(fs.existsSync(bundlePath)) {
try {
await this.loadLanguage(bundlePath)
} catch(e) {
let errMsg = `LanguageController.loadInstalledLanguages(): COULDN'T LOAD LANGUAGE: ${bundlePath}`
console.error(errMsg)
console.error(e)
await this.#pubSub.publish(
PubSubDefinitions.EXCEPTION_OCCURRED_TOPIC,
{
title: "Failed to load installed language",
message: errMsg,
type: ExceptionType.LanguageIsNotLoaded
} as ExceptionInfo
);
}
}
}
}))
}
callLinkObservers(diff: PerspectiveDiff, ref: LanguageRef) {
LANGUAGE_CONTROLLER.perspectiveDiffReceived(diff, ref.address)
this.#linkObservers.forEach(o => {
o(diff, ref)
})
}
callSyncStateChangeObservers(syncState: PerspectiveState, ref: LanguageRef) {
LANGUAGE_CONTROLLER.syncStateChanged(syncState, ref.address)
this.#syncStateChangeObservers.forEach(o => {
o(syncState, ref)
})
}
callTelepresenceSignalObservers(signal: PerspectiveExpression, ref: LanguageRef) {
LANGUAGE_CONTROLLER.telepresenceSignalReceived(signal, ref.address)
this.#telepresenceSignalObservers.forEach(o => {
o(signal, ref)
})
}
async loadLanguage(sourceFilePath: string): Promise<{
language: Language,
hash: string,
}> {
if(!path.isAbsolute(sourceFilePath))
// @ts-ignore
sourceFilePath = path.join(Deno.cwd()!, sourceFilePath)
const bundleBytes = fs.readFileSync(sourceFilePath)
if (bundleBytes.length === 0) {
throw new Error("Language to be loaded does not contain any data")
}
// @ts-ignore
const hash = await this.ipfsHash(bundleBytes)
console.debug("LanguageController.loadLanguage: loading language at path", sourceFilePath, "with hash", hash);
let languageSource;
try {
languageSource = await loadModule(sourceFilePath);
} catch (e) {
const errMsg = `Could not load language ${e}`;
console.error(errMsg);
await this.#pubSub.publish(
PubSubDefinitions.EXCEPTION_OCCURRED_TOPIC,
{
title: "Failed to load installed language",
message: errMsg,
type: ExceptionType.LanguageIsNotLoaded
} as ExceptionInfo
);
throw new Error(errMsg);
}
console.warn("LanguageController.loadLanguage: language loaded!");
let create;
if (!languageSource.default) {
create = languageSource;
} else {
if (languageSource.default.default) {
create = languageSource.default.default;
} else {
create = languageSource.default;
}
}
const customSettings = this.getSettings(hash)
const storageDirectory = this.getLanguageStoragePath(hash)
const Holochain = this.#holochainService?.getDelegateForLanguage(hash)
//@ts-ignore
const ad4mSignal = this.#context.ad4mSignal.bind({language: hash, pubsub: this.#pubSub});
const language = await create({...this.#context, customSettings, storageDirectory, Holochain, ad4mSignal})
if(language.linksAdapter) {
language.linksAdapter.addCallback((diff: PerspectiveDiff) => {
console.log("LINKS CALLBACK", diff)
this.callLinkObservers(diff, {address: hash, name: language.name} as LanguageRef);
})
if (language.linksAdapter.addSyncStateChangeCallback) {
language.linksAdapter.addSyncStateChangeCallback((state: PerspectiveState) => {
console.log("LanguageController.loadLanguage: sync state change", state);
this.callSyncStateChangeObservers(state, {address: hash, name: language.name} as LanguageRef);
})
}
}
if(language.telepresenceAdapter) {
language.telepresenceAdapter.registerSignalCallback(async (payload: PerspectiveExpression) => {
await this.tagPerspectiveExpressionSignatureStatus(payload)
this.callTelepresenceSignalObservers(payload, {address: hash, name: language.name} as LanguageRef);
})
}
//@ts-ignore
if(language.directMessageAdapter && language.directMessageAdapter.recipient() == this.#context.agent.did) {
language.directMessageAdapter.addMessageCallback(async (message: PerspectiveExpression) => {
await this.#pubSub.publish(PubSubDefinitions.RUNTIME_MESSAGED_RECEIVED_TOPIC, message)
})
}
this.#languages.set(hash, language)
this.#languageConstructors.set(hash, create)
return { hash, language }
}
async reloadLanguage(hash: string): Promise<{
language: Language,
hash: string
}> {
this.#languages.delete(hash);
const create = this.#languageConstructors.get(hash)
if (!create) {
throw new Error(`Could not find create constructor when trying to reload language: ${hash}`);
}
const customSettings = this.getSettings(hash)
const storageDirectory = this.getLanguageStoragePath(hash)
const Holochain = this.#holochainService?.getDelegateForLanguage(hash)
//@ts-ignore
const ad4mSignal = this.#context.ad4mSignal.bind({language: address, pubsub: this.#pubSub});
//@ts-ignore
const language = await create!({...this.#context, storageDirectory, Holochain, ad4mSignal, customSettings})
if(language.linksAdapter) {
language.linksAdapter.addCallback((diff: PerspectiveDiff) => {
this.callLinkObservers(diff, {address: hash, name: language.name});
})
if (language.linksAdapter.addSyncStateChangeCallback) {
language.linksAdapter.addSyncStateChangeCallback((state: PerspectiveState) => {
this.callSyncStateChangeObservers(state, {address: hash, name: language.name} as LanguageRef);
})
}
}
if(language.telepresenceAdapter) {
language.telepresenceAdapter.registerSignalCallback(async (payload: PerspectiveExpression) => {
await this.tagPerspectiveExpressionSignatureStatus(payload)
this.callTelepresenceSignalObservers(payload, {address: hash, name: language.name} as LanguageRef);
})
}
//@ts-ignore
if(language.directMessageAdapter && language.directMessageAdapter.recipient() == this.#context.agent.did) {
language.directMessageAdapter.addMessageCallback(async (message: PerspectiveExpression) => {
await this.#pubSub.publish(PubSubDefinitions.RUNTIME_MESSAGED_RECEIVED_TOPIC, message)
})
}
this.#languages.set(hash, language)
return {language: language, hash}
}
async saveLanguageBundle(bundle: string, languageMeta?: object, hash?: string): Promise<{
languagePath: string,
sourcePath: string,
metaPath: string,
hash: string
}> {
if (!hash) {
hash = await this.ipfsHash(bundle);
}
const languagePath = path.join(this.#config.languagesPath, hash);
const sourcePath = path.join(languagePath, 'bundle.js')
const metaPath = path.join(languagePath, 'meta.json')
console.log("Saving at path", sourcePath);
if (!fs.existsSync(languagePath)) {
fs.mkdirSync(languagePath)
}
fs.writeFileSync(sourcePath, bundle)
if (languageMeta) { fs.writeFileSync(metaPath, JSON.stringify(languageMeta)) }
return {languagePath, sourcePath, metaPath, hash}
}
async ipfsHash(data: Buffer|string): Promise<string> {
if (typeof data != "string") {
data = data.toString();
}
const hash = UTILS.hash(data);
return hash;
}
async installLanguage(address: Address, languageMeta: null|Expression): Promise<Language | undefined> {
const language = this.#languages.get(address)
if (language) return language
if(!languageMeta) {
//Check that the metafile already exists with language with this address to avoid refetch
const metaFile = path.join(path.join(this.#config.languagesPath, address), "meta.json");
if(fs.existsSync(metaFile)) {
languageMeta = JSON.parse(fs.readFileSync(metaFile).toString());
} else {
// We need to get the meta from the language language
// Retry 10 times with increasing delay to account for Holochain sync
let retries = 0;
while (!languageMeta && retries < 10) {
try {
languageMeta = await this.getLanguageExpression(address)
} catch (e) {
console.error(`Error getting language meta from language language: ${e}\nRetrying...`)
}
retries++;
await new Promise(r => setTimeout(r, 5000 * retries));
}
}
if (languageMeta == null) {
//@ts-ignore
languageMeta = {data: {}};
}
}
console.log("LanguageController.installLanguage: INSTALLING LANGUAGE:", languageMeta.data)
let bundlePath = path.join(path.join(this.#config.languagesPath, address), "bundle.js");
let source;
let hash;
//Check if the bundle file already exists to avoid refetching
if (!fs.existsSync(bundlePath)) {
try {
source = await this.getLanguageSource(address);
} catch (e) {
throw Error(`Error getting language source from language language, language adapter: ${e}`)
}
if(!source){
console.error("LanguageController.installLanguage: COULDN'T GET SOURCE OF LANGUAGE TO INSTALL!")
console.error("LanguageController.installLanguage: Address:", address)
console.error("LanguageController.installLanguage:", languageMeta)
throw Error(`Could not find language source for language with address: ${address}`)
}
hash = await this.ipfsHash(source)
if(hash === 'asdf') {
console.error("LanguageController.installLanguage: COULDN'T VERIFY HASH OF LANGUAGE!")
console.error("LanguageController.installLanguage: Address:", address)
console.error("LanguageController.installLanguage: Computed hash:", hash)
console.error("LanguageController.installLanguage: =================================")
console.error("LanguageController.installLanguage: LANGUAGE WILL BE IGNORED")
console.error("LanguageController.installLanguage: =================================")
console.error("LanguageController.installLanguage:", languageMeta)
console.error("LanguageController.installLanguage: =================================")
console.error("LanguageController.installLanguage: =================================")
return
}
} else {
source = fs.readFileSync(bundlePath).toString();
hash = await this.ipfsHash(source);
}
//TODO: potential for unnecassary write to happen here if meta / source is already present at languages paths
const {languagePath, sourcePath} = await this.saveLanguageBundle(source, languageMeta, hash);
console.log(new Date(), "LanguageController.installLanguage: installed language");
try {
const {language} = await this.loadLanguage(sourcePath);
let newLang = {
...language,
linksAdapter: cloneWithoutCircularReferences(language).linksAdapter,
telepresenceAdapter: cloneWithoutCircularReferences(language).telepresenceAdapter
};
return newLang
} catch(e) {
console.error("LanguageController.installLanguage: ERROR LOADING NEWLY INSTALLED LANGUAGE")
console.error("LanguageController.installLanguage: ======================================")
console.error(e)
//fs.rmdirSync(languagePath, {recursive: true})
//@ts-ignore
// throw Error(`Error loading language [${sourcePath}]: ${e.toString()}`)
}
}
async languageRemove(hash: String): Promise<void> {
//Teardown any intervals the language has running
const language = this.#languages.get(hash as string);
if (language?.teardown) {
language.teardown();
}
//Remove language from memory
this.#languages.delete(hash as string);
this.#languageConstructors.delete(hash as string);
try {
await this.#holochainService?.removeDnaForLang(hash as string);
} catch(e) {
console.log("No DNA found for language installed");
}
//Remove language files
const languagePath = path.join(this.#config.languagesPath, hash as string);
fs.rmdirSync(languagePath, {recursive: true});
}
languageForExpression(e: ExpressionRef): Language {
const address = this.#config.languageAliases[e.language.address] ? this.#config.languageAliases[e.language.address] : e.language.address
const language = this.#languages.get(address)
if(language) {
return language
} else {
throw new Error("Language for expression not found: " + JSON.stringify(e))
}
}
async languageByRef(ref: LanguageRef): Promise<Language> {
const address = this.#config.languageAliases[ref.address] ? this.#config.languageAliases[ref.address] : ref.address
const language = this.#languages.get(address)
//If the language is already installed then just return it
if(language) {
return language
} else {
let languageMeta = await this.getExpression(new ExpressionRef(new LanguageRef('lang'), address));
if(!languageMeta) {
throw new Error("Language not found by reference: " + JSON.stringify(ref))
}
if (languageMeta.proof.valid != true) {
throw new Error(`Language to be installed does not have valid proof`);
}
const languageMetaData = languageMeta.data as LanguageExpression;
const languageAuthor = languageMeta.author;
const trustedAgents: string[] = this.#runtimeService.getTrustedAgents();
const agentService = (this.#context as LanguageContext).agent as AgentService;
//Check if the author of the language is in the trusted agent list the current agent holds, if so then go ahead and install
if (trustedAgents.find((agent) => agent === languageAuthor) || agentService.agent! === languageAuthor) {
//Get the language source so we can generate a hash and check against the hash given in the language meta information
const languageSource = await this.getLanguageSource(address);
if (!languageSource) {
throw new Error("Could not get languageSource for language")
}
const languageHash = await this.ipfsHash(languageSource);
if (!languageMetaData.address) {
throw new Error(`Could not find 'address' value inside languageMetaData object: ${languageMetaData["address"]} - ${languageMetaData.address} - ${JSON.stringify(languageMetaData)}`)
}
//Check the hash matches and then install!
if (languageHash == languageMetaData.address) {
//TODO: in here we are getting the source again even though we have already done that before, implement installLocalLanguage()?
const lang = await this.installLanguage(address, languageMeta)
// @ts-ignore
return lang
} else {
throw new Error("Calculated languageHash did not match address found in meta information")
}
} else {
//Person who created this language is not trusted so lets try and get a source language template
//Check that there are some template params to even apply
//Check that there is a sourceLanguage that we can follow to
if (!languageMetaData.templateAppliedParams ||
Object.keys(languageMetaData.templateAppliedParams).length == 0 ||
!languageMetaData.templateSourceLanguageAddress
) {
let errMsg = `Language not created by trusted agent: ${languageAuthor} and is not templated... aborting language install. Language metadata: ${stringify(languageMetaData)}`
console.error(errMsg)
await this.#pubSub.publish(
PubSubDefinitions.EXCEPTION_OCCURRED_TOPIC,
{
title: "Failed to install language",
message: errMsg,
type: ExceptionType.AgentIsUntrusted,
addon: languageAuthor,
} as ExceptionInfo
);
throw new Error(errMsg);
}
//Get the meta information of the source language
const sourceLanguageHash = languageMetaData.templateSourceLanguageAddress;
const sourceLanguageMeta = await this.getLanguageExpression(sourceLanguageHash);
if (!sourceLanguageMeta) {
throw new Error("Could not get the meta for the source language");
}
//Check that the agent who authored the original template language is in the current agents trust list
if (trustedAgents.find((agent) => agent === sourceLanguageMeta.author)) {
//Apply the template information supplied in the language to be installed to the source language and make sure that the resulting
//language hash is equal to the one trying to be installed. This ensures that the only thing changed between language versions is the template data
const sourceLanguageTemplated = await this.languageApplyTemplateOnSource(sourceLanguageHash, JSON.parse(languageMetaData.templateAppliedParams));
const languageSource = await this.getLanguageSource(address);
if (!languageSource) {
throw new Error("Could not get languageSource for language")
}
//Hash the language source and ensure its the same as the templated language
const languageHash = await this.ipfsHash(languageSource);
if (sourceLanguageTemplated.meta.address === languageHash) {
//TODO: in here we are getting the source again even though we have already done that before, implement installLocalLanguage()?
const lang = await this.installLanguage(address, languageMeta)
// @ts-ignore
return lang!
} else {
throw new Error(`Templating of original source language did not result in the same language hash of un-trusted language trying to be installed... aborting language install. Expected hash: ${languageHash}. But got: ${sourceLanguageTemplated.meta.address}`)
}
} else {
let errMsg = "Agent which created source language for language trying to be installed is not a trustedAgent... aborting language install";
await this.#pubSub.publish(
PubSubDefinitions.EXCEPTION_OCCURRED_TOPIC,
{
title: "Failed to install language",
message: errMsg,
type: ExceptionType.AgentIsUntrusted,
addon: sourceLanguageMeta.author,
} as ExceptionInfo
);
throw new Error(errMsg)
}
}
}
}
async readAndTemplateHolochainDNA(sourceLanguageLines: string[], templateData: object, sourceLanguageHash?: string): Promise<{dnaCode: string | null}> {
//Look for var dna in languageSource which would tell us its a holochain language and then apply templating to the holochain language also
const dnaIndex = sourceLanguageLines.findIndex(element => element.includes(`var dna = `));
let dnaCodeRes: null | string = null;
if (dnaIndex != -1) {
if (!sourceLanguageHash) {
sourceLanguageHash = uuidv4();
}
//Create a directory for all of our DNA templating operations
const tempTemplatingPath = path.join(this.#config.tempLangPath, sourceLanguageHash);
fs.mkdirSync(tempTemplatingPath);
//The place where we will put the .dna from the b64
const tempDnaPath = path.join(tempTemplatingPath, `${sourceLanguageHash}.dna`);
const wasmPath = path.join(tempTemplatingPath, "target/wasm32-unknown-unknown/release/")
//Write the DNA code to a file
let dnaCode = sourceLanguageLines[dnaIndex].split("var dna = ")[1]
//TODO: here was are assuming that the first character is " and the last two "; we should check for this and not assume
dnaCode = dnaCode.substring(1, dnaCode.length-2);
fs.writeFileSync(tempDnaPath, Buffer.from(dnaCode, "base64"));
//Unpack the DNA
//TODO: we need to be able to check for errors in this fn call, currently we just crudly split the result
console.log("LanguageController.readAndTemplateHolochainDNA: unpacking DNA");
let unpackPath = (await this.#holochainService?.unpackDna(tempDnaPath)).replace(/(\r\n|\n|\r)/gm, "");
fs.unlinkSync(tempDnaPath);
//TODO: are all dna's using the same dna.yaml?
const dnaYamlPath = path.join(unpackPath, "dna.yaml");
if (!fs.existsSync(dnaYamlPath)) {
throw new Error("Expected to find DNA of source language at path: " + dnaYamlPath + " after unpacking but could not find at given path");
}
//Read for files inside wasm path after unpack since we should now have .wasm file there but we do not know which name it may have
const wasmName = fs.readdirSync(wasmPath);
if (wasmName.length == 0) {
throw new Error("Got incorrect number of files inside wasm path when unpacking DNA");
}
//Read the yaml file
let dnaYaml = yaml.load(fs.readFileSync(dnaYamlPath, 'utf8'));
//@ts-ignore
if (templateData.uid) {
//@ts-ignore
dnaYaml.integrity.network_seed = templateData.uid;
}
//@ts-ignore
for (const [templateKey, templateValue] of Object.entries(templateData)) {
//@ts-ignore
dnaYaml.integrity.properties[templateKey] = templateValue;
}
let dnaYamlDump = yaml.dump(dnaYaml, {
'styles': {
'!!null': 'canonical' // dump null as ~
}
});
fs.writeFileSync(dnaYamlPath, dnaYamlDump);
//TODO: we need to be able to check for errors in this fn call, currently we just crudly split the result
console.log("LanguageController.readAndTemplateHolochainDNA: packing DNA");
let packPath = (await this.#holochainService?.packDna(unpackPath)).replace(/(\r\n|\n|\r)/gm, "");
const base64 = fs.readFileSync(packPath, "base64").replace(/[\r\n]+/gm, '');
//Cleanup temp directory
fs.rmdirSync(tempTemplatingPath, {recursive: true});
dnaCodeRes = base64;
}
return {
dnaCode: dnaCodeRes
}
}
orderObject(templateData: object): object {
return Object.keys(templateData).sort().reduce(
(obj, key) => {
//@ts-ignore
obj[key] = templateData[key];
return obj;
},
{}
);
}
applyTemplateData(sourceLanguageLines: string[], templateData: object) {
//Get lines in sourceLanguageLines which have ad4m-template-variable declared
const ad4mTemplatePattern = "//!@ad4m-template-variable";
var indexes = [];
for(let i = 0; i < sourceLanguageLines.length; i++) {
if (sourceLanguageLines[i].includes(ad4mTemplatePattern)) {
indexes.push(i);
}
}
//Possible variable patterns for template variables
const patterns = [
new RegExp(/var ([a-zA-Z0-9_-]{1,})/g),
new RegExp(/const ([a-zA-Z0-9_-]{1,})/g),
new RegExp(/let ([a-zA-Z0-9_-]{1,})/g)
];
//Look over the template variable indexes
for (let i = 0; i < indexes.length; i++) {
//Get the index of the variable which should be next line after comment denote
let variableIndex = indexes[i] + 1;
let variable = sourceLanguageLines[variableIndex];
for (const pattern of patterns) {
let matches = variable.match(pattern);
if (matches) {
//Get variable type and name
let variableType = matches[0].split(" ")[0];
let variableName = matches[0].split(" ")[1];
for (const [key, value] of Object.entries(templateData)) {
//if matching variable name from template data
if (key === variableName) {
//Ensure we use the correct type of variable
if (variableType === "const") {
//Ensure we use quotes where required if variable is a string
if (typeof value === "string") {
sourceLanguageLines[variableIndex] = `const ${key} = "${value}"`;
} else {
sourceLanguageLines[variableIndex] = `const ${key} = ${value}`
}
} else if (variableType === "let") {
if (typeof value === "string") {
sourceLanguageLines[variableIndex] = `let ${key} = "${value}"`;
} else {
sourceLanguageLines[variableIndex] = `let ${key} = ${value}`
}
} else if (variableType === "var") {
if (typeof value === "string") {
sourceLanguageLines[variableIndex] = `var ${key} = "${value}"`;
} else {
sourceLanguageLines[variableIndex] = `var ${key} = ${value}`
}
}
}
}
}
}
}
//Add custom case for inserting templated dna data
const dnaPattern = new RegExp(/var (dna{1,})/g);
//@ts-ignore
if (templateData["dna"]) {
let dnaIndex = 0;
for(let i = 0; i < sourceLanguageLines.length; i++) {
if (sourceLanguageLines[i].match(dnaPattern)) {
dnaIndex = i;
}
}
//@ts-ignore
sourceLanguageLines[dnaIndex] = `var dna = "${templateData["dna"]}"`;
}
}
async constructLanguageLanguageInput(
sourceLanguageLines: string[],
metaInput: LanguageMetaInput
): Promise<LanguageLanguageInput> {
const languageData = sourceLanguageLines.join('\n');
const languageHash = await this.ipfsHash(languageData);
const internal: LanguageMetaInternal = metaInput as LanguageMetaInternal
internal.address = languageHash
let input = new LanguageLanguageInput()
input.bundle = languageData
input.meta = internal
return input
}
async languageApplyTemplateOnSource(sourceLanguageHash: string, templateData: object): Promise<LanguageLanguageInput> {
const sourceLanguageExpr = await this.getLanguageExpression(sourceLanguageHash);
if(!sourceLanguageExpr) throw new Error(`Language not found: ${sourceLanguageHash}`)
const sourceLanguage = await this.getLanguageSource(sourceLanguageHash);
if (!sourceLanguage) {
throw new Error("LanguageController.languageApplyTemplate: Could not get sourceLanguage with hash: " + sourceLanguageHash)
}
if (!sourceLanguageExpr) {
throw new Error("LanguageController.languageApplyTemplate: Could not get sourceLanguageMeta with hash: " + sourceLanguageHash)
}
const sourceLanguageLines = sourceLanguage!.split("\n");
templateData = this.orderObject(templateData);
const { dnaCode } = await this.readAndTemplateHolochainDNA(sourceLanguageLines, templateData, sourceLanguageHash);
//If there was some dna code in the source language then lets also add that to the language bundle (but not to the templateData as we dont want that on the meta)
if (dnaCode) {
//@ts-ignore
templateData["dna"] = dnaCode;
}
templateData = this.orderObject(templateData);
//console.debug("LangugeFactory.languageApplyTemplate: Templating language with template data", templateData);
this.applyTemplateData(sourceLanguageLines, templateData);
//@ts-ignore
delete templateData["dna"];
const meta = sourceLanguageExpr.data
//@ts-ignore
if(templateData["name"])
//@ts-ignore
meta.name = templateData["name"]
//@ts-ignore
if(templateData["description"])
//@ts-ignore
meta.description = templateData["description"]
meta.templateAppliedParams = JSON.stringify(templateData)
meta.templateSourceLanguageAddress = sourceLanguageHash
return await this.constructLanguageLanguageInput(sourceLanguageLines, meta)
}
filteredLanguageRefs(propertyFilter?: string): LanguageRef[] {
const refs: LanguageRef[] = []
this.#languages.forEach((language, hash) => {
if(!propertyFilter || Object.keys(language).includes(propertyFilter)) {
refs.push({
address: hash,
name: language.name,
})
}
})
return refs
}
async getLanguageExpression(address: string): Promise<LanguageExpression | null> {
if(this.#config.bootstrapFixtures) {
const fixtures = this.#config.bootstrapFixtures.languages;
if (fixtures) {
const fixtureLanguage = fixtures.find(f=>f.address===address)
if(fixtureLanguage && fixtureLanguage.meta) {
return fixtureLanguage.meta
}
}
}
try {
return await this.#languageLanguage!.expressionAdapter!.get(address)
} catch (e) {
throw Error(`Error inside language language expression adapter: ${e}`)
}
}
async getLanguageSource(address: string): Promise<string | null> {
if(this.#config.bootstrapFixtures) {
const fixtures = this.#config.bootstrapFixtures.languages;
if (fixtures) {
const fixtureLanguage = fixtures.find(f=>f.address===address)
if(fixtureLanguage && fixtureLanguage.bundle) {
return fixtureLanguage.bundle
}
}
}
try {
return await this.#languageLanguage!.languageAdapter!.getLanguageSource(address)
} catch (e) {
throw Error(`Error inside language language getLanguageSource adapter: ${e}`)
}
}
async getPerspective(address: string): Promise<Expression | null> {
if(this.#config.bootstrapFixtures) {
const perspectives = this.#config.bootstrapFixtures.perspectives;
if (perspectives) {
const perspective = perspectives.find(f=>f.address===address)
if(perspective && perspective.expression) {
return perspective.expression
}
}
}
try {
return await this.#neighbourhoodLanguage!.expressionAdapter!.get(address)
} catch (e) {
throw Error(`Error inside perspective language expression get adapter: ${e}`)
}
}
getInstalledLanguages(): LanguageRef[] {
return this.filteredLanguageRefs()
}
getLanguagesWithExpressionUI(): LanguageRef[] {
return this.filteredLanguageRefs("expressionUI")
}
getLanguagesWithLinksAdapter(): LanguageRef[] {
return this.filteredLanguageRefs("linksAdapter")
}
getAgentLanguage(): Language {
if(!this.#agentLanguage) {
throw new Error("No Agent Language installed!")
}
return this.#agentLanguage
}
getLanguageLanguage(): Language {
if(!this.#languageLanguage) {
throw new Error("No Language Language installed!")
}
return this.#languageLanguage
}
getNeighbourhoodLanguage(): Language {
if(!this.#neighbourhoodLanguage) {
throw new Error("No Neighbourhood Language installed!")
}
return this.#neighbourhoodLanguage
}
getPerspectiveLanguage(): Language {
if(!this.#perspectiveLanguage) {
throw new Error("No Perspective Language installed!")
}
return this.#perspectiveLanguage
}
async getConstructorIcon(lang: LanguageRef): Promise<string | undefined> {
let grabbedLang = await this.languageByRef(lang);
return grabbedLang.expressionUI?.constructorIcon()
}
async getSettingsIcon(lang: LanguageRef): Promise<string | undefined> {
let grabbedLang = await this.languageByRef(lang);
return grabbedLang.settingsUI?.settingsIcon()
}
async getIcon(lang: LanguageRef): Promise<string | undefined> {
let grabbedLang = await this.languageByRef(lang);
return grabbedLang.expressionUI?.icon()
}
getSettings(hash: string): object {
const FILEPATH = path.join(this.#config.languagesPath, hash, 'settings.json')
if(fs.existsSync(FILEPATH)) {
return JSON.parse(fs.readFileSync(FILEPATH).toString())
} else {
return {}
}
}
writeSettings(hash: string, settings: object) {
const directory = path.join(this.#config.languagesPath, hash)
if(!fs.existsSync(directory))
fs.mkdirSync(directory)
const FILEPATH = path.join(directory, 'settings.json')
fs.writeFileSync(FILEPATH, JSON.stringify(settings))
}
async putSettings(hash: string, settings: object) {
this.writeSettings(hash, settings);
this.reloadLanguage(hash);
}
getLanguageStoragePath(hash: string) {
const languageConfigPath = path.join(this.#config.languagesPath, hash)
if(!fs.existsSync(languageConfigPath))
fs.mkdirSync(languageConfigPath)
const storageDirectory = path.join(languageConfigPath, "storage")
if(!fs.existsSync(storageDirectory))
fs.mkdirSync(storageDirectory)
return storageDirectory
}
async expressionCreate(lang: LanguageRef, content: object): Promise<ExpressionRef> {
if(lang.address == "literal") {
const expr = (this.#context as LanguageContext).agent.createSignedExpression(content)
return parseExprUrl(Literal.from(expr).toUrl())
}
const language = await this.languageByRef(lang)
if (!language.expressionAdapter) {
throw new Error("Language does not have an expressionAdapter")
}
if (!language.expressionAdapter.putAdapter) {
throw new Error("Language does not have an expressionAdapter.putAdapter")
}
const putAdapter = language.expressionAdapter.putAdapter;
let address = null
let isPublic = function isPublic(adapter: PublicSharing | ReadOnlyLanguage): adapter is PublicSharing {
return (adapter as PublicSharing).createPublic !== undefined;
}