-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathLanguageController.ts
More file actions
365 lines (314 loc) · 13.7 KB
/
LanguageController.ts
File metadata and controls
365 lines (314 loc) · 13.7 KB
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
import type Expression from '../ad4m/Expression';
import ExpressionRef from '../ad4m/ExpressionRef';
import type Language from '../ad4m/Language'
import type { LinksAdapter } from '../ad4m/Language'
import type { InteractionCall } from '../ad4m/Language'
import type LanguageContext from '../ad4m/LanguageContext';
import type LanguageRef from '../ad4m/LanguageRef'
import fs from 'fs'
import path from 'path'
import multihashing from 'multihashing'
import * as Config from './Config'
import type HolochainService from './storage-services/Holochain/HolochainService';
import type AgentService from './agent/AgentService'
import baseX from 'base-x'
import type Address from '../ad4m/Address';
const BASE58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
const bs58 = baseX(BASE58)
const builtInLanguages = [
'note-ipfs',
'url-iframe',
'gun-links',
'ipfs-links',
'junto-hc-shortform',
'agent-profiles',
'languages',
'shared-perspectives'
].map(l => `./src/languages/${l}/build/bundle.js`)
const aliases = {
'http': 'url-iframe',
'https': 'url-iframe',
'did': 'agent-profiles',
'lang': 'languages',
'perspective': 'shared-perspectives'
}
type LinkObservers = (added: Expression[], removed: Expression[], lang: LanguageRef)=>void;
export default class LanguageController {
#languages: Map<string, Language>
#languageConstructors: Map<string, (LanguageContext)=>Language>
#context: object;
#linkObservers: LinkObservers[];
#holochainService: HolochainService
#agentLanguage: Language
#languageLanguage: Language
#perspectiveLanguage: Language
constructor(context: object, holochainService: HolochainService) {
this.#context = context
this.#holochainService = holochainService
this.#languages = new Map()
this.#languageConstructors = new Map()
this.#linkObservers = []
}
async loadLanguages() {
await this.loadBuiltInLanguages()
await this.loadInstalledLanguages()
}
async loadBuiltInLanguages() {
await Promise.all(builtInLanguages.map( async bundle => {
const { hash, language } = await this.loadLanguage(bundle)
// Do special stuff for AD4M languages:
Object.keys(aliases).forEach(alias => {
if(language.name === aliases[alias]) {
aliases[alias] = hash
if(alias === 'did') {
this.#agentLanguage = language;
((this.#context as LanguageContext).agent as AgentService).setAgentLanguage(language)
}
if(alias === 'lang') {
this.#languageLanguage = language
}
if(alias === 'perspective') {
this.#perspectiveLanguage = language
}
}
})
}))
}
async loadInstalledLanguages() {
const files = fs.readdirSync(Config.languagesPath)
await Promise.all(files.map(async file => {
const bundlePath = path.join(Config.languagesPath, file, 'bundle.js')
if(fs.existsSync(bundlePath)) {
try {
await this.loadLanguage(bundlePath)
} catch(e) {
console.error("LanguageController.loadInstalledLanguages()=========================")
console.error("LanguageController.loadInstalledLanguages(): COULDN'T LOAD LANGUAGE:", bundlePath)
console.error(e)
console.error("LanguageController.loadInstalledLanguages()=========================")
}
}
}))
}
async loadLanguage(sourceFilePath: string): Promise<any> {
if(!path.isAbsolute(sourceFilePath))
sourceFilePath = path.join(process.env.PWD, sourceFilePath)
const bundleBytes = fs.readFileSync(sourceFilePath)
// @ts-ignore
const hash = await this.ipfsHash(bundleBytes)
const { default: create, name } = require(sourceFilePath)
const customSettings = this.getSettings({name, address: hash} as LanguageRef)
const storageDirectory = Config.getLanguageStoragePath(name)
const Holochain = this.#holochainService.getDelegateForLanguage(hash)
const language = create({...this.#context, customSettings, storageDirectory, Holochain})
if(language.linksAdapter) {
language.linksAdapter.addCallback((added, removed) => {
this.#linkObservers.forEach(o => {
o(added, removed, {name, address: hash} as LanguageRef)
})
})
}
this.#languages.set(hash, language)
this.#languageConstructors.set(hash, create)
return { hash, language }
}
async ipfsHash(data: Buffer|string): Promise<string> {
// @ts-ignore
const ipfsAddress = await this.#context.IPFS.add({content: data.toString()}, {onlyHash: true})
return ipfsAddress.cid.toString()
}
async installLanguage(address: Address, languageMeta: void|Expression) {
if(!languageMeta) {
languageMeta = await this.#languageLanguage.expressionAdapter.get(address)
}
// @ts-ignore
console.log("LanguageController: INSTALLING NEW LANGUAGE:", languageMeta.data)
const source = await this.#languageLanguage.languageAdapter.getLanguageSource(address)
const 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: =================================")
console.error("LanguageController.installLanguage: CONTENT:")
//console.error(source)
//console.error("LanguageController.installLanguage: =================================")
//console.error("LanguageController.installLanguage: LANGUAGE WILL BE IGNORED")
//console.error("LanguageController.installLanguage: =================================")
return
}
const sourcePath = path.join(Config.languagesPath, address, 'bundle.js')
const metaPath = path.join(Config.languagesPath, address, 'meta.json')
fs.mkdirSync(path.join(Config.languagesPath, address))
fs.writeFileSync(sourcePath, source)
fs.writeFileSync(metaPath, JSON.stringify(languageMeta))
try {
this.loadLanguage(sourcePath)
} catch(e) {
console.error("LanguageController.installLanguage: ERROR LOADING NEWLY INSTALLED LANGUAGE")
console.error("LanguageController.installLanguage: ======================================")
console.error(e)
}
}
languageForExpression(e: ExpressionRef): Language {
const address = aliases[e.language.address] ? aliases[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))
}
}
languageByRef(ref: LanguageRef): Language {
const address = aliases[ref.address] ? aliases[ref.address] : ref.address
const language = this.#languages.get(address)
if(language) {
return language
} else {
this.#languageLanguage.expressionAdapter.get(address).then(languageMeta => {
if(languageMeta) {
this.installLanguage(address, languageMeta)
}
})
throw new Error("Language not found by reference: " + JSON.stringify(ref))
}
}
filteredLanguageRefs(propertyFilter: void | 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
}
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.#agentLanguage) {
throw new Error("No Language Language installed!")
}
return this.#languageLanguage
}
getPerspectiveLanguage(): Language {
if(!this.#agentLanguage) {
throw new Error("No Perspective Language installed!")
}
return this.#perspectiveLanguage
}
getConstructorIcon(lang: LanguageRef): void | string {
return this.languageByRef(lang).expressionUI?.constructorIcon()
}
getSettingsIcon(lang: LanguageRef): void | string {
return this.languageByRef(lang).settingsUI?.settingsIcon()
}
getIcon(lang: LanguageRef): void | string {
return this.languageByRef(lang).expressionUI?.icon()
}
getSettings(lang: LanguageRef): object {
const FILEPATH = path.join(Config.languagesPath, lang.name, 'settings.json')
if(fs.existsSync(FILEPATH)) {
return JSON.parse(fs.readFileSync(FILEPATH).toString())
} else {
return {}
}
}
putSettings(lang: LanguageRef, settings: object) {
const directory = path.join(Config.languagesPath, lang.name)
if(!fs.existsSync(directory))
fs.mkdirSync(directory)
const FILEPATH = path.join(directory, 'settings.json')
fs.writeFileSync(FILEPATH, JSON.stringify(settings))
this.#languages.set(lang.address, null)
const create = this.#languageConstructors.get(lang.address)
const context = this.#context
const storageDirectory = Config.getLanguageStoragePath(lang.name)
const newInstance = create({...context, storageDirectory, customSettings: settings})
this.#languages.set(lang.address, newInstance)
}
async createPublicExpression(lang: LanguageRef, content: object): Promise<ExpressionRef> {
const putAdapter = this.languageByRef(lang).expressionAdapter.putAdapter
let address = null
try {
// Ok, first we assume its a PublicSharing put adapter...
// @ts-ignore
address = await putAdapter.createPublic(content)
} catch(e1) {
try {
// ...and if it's not, let's try to treat it like a
// ReadOnlyLangauge..
// @ts-ignore
address = await putAdapter.addressOf(content)
} catch(e2) {
// If both don't work, we don't know what to do with this put adapter :/
throw new Error(`Incompatible putAdapter in Languge ${JSON.stringify(lang)}\nPutAdapter: ${Object.keys(putAdapter)}\nError was: ${e1.toString()}\nand: ${e2.toString()}`)
}
}
// This makes sure that Expression references used in Links (i.e. in Perspectives) use the aliased Language schemas.
// Especially important for DIDs
for(const alias of Object.keys(aliases)) {
const target = aliases[alias]
if(lang.address === target) {
lang.address = alias
}
}
return new ExpressionRef(lang, address)
}
async getExpression(ref: ExpressionRef): Promise<void | Expression> {
const expr = await this.languageForExpression(ref).expressionAdapter.get(ref.expression)
if(expr) {
try{
// @ts-ignore
if(! await this.#context.signatures.verify(expr)) {
console.error("BROKEN SIGNATURE FOR EXPRESSION:", expr)
expr.proof.invalid = true
} else {
// console.debug("Valid expr:", ref)
expr.proof.valid = true
}
} catch(e) {
console.error("Error trying to verify expression signature:", e)
console.error("For expression:", expr)
}
}
return expr
}
interact(expression: ExpressionRef, interaction: InteractionCall) {
console.log("TODO")
}
getLinksAdapter(lang: LanguageRef): void | LinksAdapter {
try {
return this.languageByRef(lang).linksAdapter
} catch(e) {
return null
}
}
addLinkObserver(observer) {
this.#linkObservers.push(observer)
}
}
export function init(context: object, holochainService: HolochainService): LanguageController {
const languageController = new LanguageController(context, holochainService)
return languageController
}