File size: 14,387 Bytes
b39afbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Copyright (c) 2023 MERCENARIES.AI PTE. LTD.
 * All rights reserved.
 */

// Import required modules
import { rmSync } from 'fs'
import { ensureDir } from 'fs-extra'
import fs from 'fs/promises'
import yaml from 'js-yaml'
import path from 'path'
import { simpleGit } from 'simple-git'
import { exec } from 'child_process'

const TEMPLATE_EXTENSION_REPO = 'https://github.com/omnitool-ai/omni-extension-template.git'

// Function to validate directory existence
async function validateDirectoryExists (path) {
  try {
    const stats = await fs.stat(path)
    return stats.isDirectory() // Returns true if directory exists
  } catch {
    return false // Returns false if directory doesn't exist
  }
}

// Function to validate file existence
async function validateFileExists (path) {
  try {
    const stats = await fs.stat(path)
    return stats.isFile() // Returns true if file exists
  } catch {
    return false // Returns false if file doesn't exist
  }
}

// Function to check if there's a build entry in package.json
async function hasBuildScript(packageJsonPath) {
  try {
    const content = await fs.readFile(packageJsonPath, 'utf8');
    const packageJson = JSON.parse(content);
    return packageJson.scripts && packageJson.scripts.build;
  } catch (e) {
    return false;
  }
}

async function try_exec_command(command, cwd) {
  let cmd_error = null;

  try
  {
    exec(command, { cwd: cwd }, (error, stdout, stderr) => {
    if (error)
    {
      cmd_error = `Warning: problem while issuing command ${command} at ${cwd} with stdout: ${stdout}\nstderr: ${stderr}\nerror: ${error}`;
    }
    });
  }
  catch (e)
  {
    cmd_error = `Warning: problem while issuing command ${command} at ${cwd} with error message: ${e.message}`;
  }
  if (cmd_error)
  {
    console.warn(cmd_error);
    return false;
  }

  return true;
}

async function getKnownExtensions() {
  const knownFilePath = path.join(process.cwd(), 'config.default', 'extensions', 'known_extensions.yaml')
  const knownFileContents = await fs.readFile(knownFilePath, 'utf8')
  return yaml.load(knownFileContents)
}

// Function to update extensions
const updateExtension = async function (ctx, extensionId) {
  const sessionId = ctx.session.sessionId
  const target = path.join(process.cwd(), '/extensions/', extensionId)

  // Log the target of the update
  ctx.integration.info('Update', extensionId, target)

  // Check if the extension and its directory exist
  if (ctx.app.extensions.has(extensionId) && await validateDirectoryExists(target)) {
    // Check if the required file is present
    if (!await validateFileExists(path.join(target, 'extension.yaml'))) {
      const error = `Failed to update extension:\n${extensionId} appears to not be a valid extension (extension.yaml not found).`
      ctx.app.sendErrorToSession(sessionId, error)
      return { error }
    }
    // Check if .git directory exists
    if (await validateDirectoryExists(path.join(target, '.git'))) {
      await ctx.app.sendMessageToSession(sessionId, `Attempting to update extension: ${extensionId}`, 'text/plain')
      try {
        // Update the git repository
        const git = simpleGit({ baseDir: target })
        const result = await git.pull()

        // Send successful update message
        await ctx.app.sendMessageToSession(sessionId, 'Extension updated (May require a server restart).\n',
          'text/plain',
          {
            objects: [result],
            commands: [
              {
                title: 'Restart Server',
                id: 'rs',
                args: []
              }
            ]

          }
        )
        return true
      } catch (e) {
        // Handle update failure
        const error = `Failed to update extension:\n${e.message}`
        ctx.integration.error(error)
        ctx.app.sendErrorToSession(sessionId, error)
        return { error }
      }
    } else {
      // Handle missing git repository case
      const error = `Failed to update extension: ${extensionId} cannot be updated. (no git repository found at ${target})`
      ctx.integration.error(error)
      ctx.app.sendErrorToSession(sessionId, error)
      return { error }
    }
  } else {
    // Handle invalid/active extension case
    const error = `Failed to update extension:\n${extensionId} appears to not be a valid/active extension.`
    ctx.app.sendErrorToSession(sessionId, error)
    return { error }
  }
}

const script = {
  name: 'extensions',

  exec: async function (ctx, payload) {
    const sessionId = ctx.session.sessionId
    let [command, arg1] = [...payload]

    ctx.integration.debug('extensions', command, arg1)
    if (arg1?.length > 1) {
      if (command === 'manifest') {
        const response = await fetch(arg1)
        if (!response.ok) {
          throw new Error(`HTTP error! status: ${response.status}`)
        }
        const text = await response.text()
        const manifest = yaml.safeLoad(text)

        await ctx.app.sendMessageToSession(sessionId, 'Extension Manifest', 'text/plain', {
          objects: [manifest],
          commands: [
            {
              title: 'Install',
              id: 'extensions',
              args: ['add', manifest.origin]
            }
          ]
        })
      } else if (command === 'add') {
        try {
          // if it's not a git repository we need to resolve it.
          if (!arg1.endsWith('.git')) {
            await ctx.app.sendMessageToSession(sessionId, 'Loading extension manifest from: ' + arg1, 'text/plain')
            const response = await fetch(arg1)
            if (!response.ok) {
              ctx.app.sendErrorToSession(sessionId, 'Failed to retrieve manifest' + response.status)
              throw new Error(`HTTP error! status: ${response.status}`)
            }
            const text = await response.text()
            const manifest = yaml.load(text)
            if (manifest.origin !== undefined) {
              arg1 = manifest.origin
            } else {
              ctx.app.sendErrorToSession(sessionId, 'Failed to install extension: No origin found in extension.yaml')
              throw new Error('No origin found in extension.yaml')
            }
            await ctx.app.sendMessageToSession(sessionId, 'Downloaded extension manifest from: ' + arg1, 'text/plain', { objects: [manifest] })
          }
          await ctx.app.sendMessageToSession(sessionId, 'Attempting to install extension from: ' + arg1 + '\nPlease wait...', 'text/plain')

          const git = simpleGit({ baseDir: path.join(process.cwd(),'extensions') })
          await git.clone(arg1)

          await ctx.app.sendMessageToSession(sessionId, 'Extension added, please restart the server.\n', 'text/plain',{
          commands: [
            {
              title: 'Restart Server',
              id: 'rs',
              args: []
            }
          ]}
          );

          await ctx.app.kvStorage?.set('extensions.dirty', true)

          return true;
        } catch (e) {
          const error = 'Failed to install extension:\n' + e.message
          console.log(e.message)
          return { error }
        }
      } else if (command === 'show') {
        const extensionId = arg1.replace(/[^a-zA-Z0-9-_]/g, '')
        if (ctx.app.extensions.has(extensionId)) {
          const extension = ctx.app.extensions.get(extensionId)
          await ctx.app.sendMessageToSession(sessionId, JSON.stringify({ id: extension.id, title: extension.config.title, description: extension.config.description || 'No description.' }, null, 2),
            'text/plain',
            {
              commands:
                [
                  {
                    title: 'Update',
                    id: 'extensions',
                    args: ['update', extensionId]
                  }
                ]
            }
          )
        }
        return true
      } else if (command === 'update') {
        const extensionId = arg1.replace(/[^a-zA-Z0-9-_]/g, '')
        return await updateExtension(ctx, extensionId)
      } else if (command === 'create') {
        const extensionId = arg1.replace(/[^a-zA-Z0-9-_]/g, '')
        const targetPath = path.join(process.cwd(), 'extensions', extensionId)
        if (!(await validateDirectoryExists(targetPath))) {
          await ensureDir(targetPath)

          const git = simpleGit({ baseDir: targetPath })
          await git.clone(TEMPLATE_EXTENSION_REPO, targetPath)
          // Remove git directory to re-init repository to a new one
          rmSync(path.join(targetPath, '.git'), { recursive: true, force: true })
          const result = await git.init()
          await ctx.app.sendMessageToSession(sessionId, `Extension ${extensionId} created at ${targetPath}, please restart server.\n`, 'text/plain', { objects: [result],
            commands: [
              {
                title: 'Restart Server',
                id: 'rs',
                args: []
              }
            ]
          })
          await ctx.app.kvStorage?.set('extensions.dirty', true)
          return true
        } else {
          ctx.app.sendErrorToSession(sessionId, `An extension with the id ${extensionId} already exists.`)
        }
      } else if (command === 'remove') {
        const extensionId = arg1.replace(/[^a-zA-Z0-9-_]/g, '')
        await ctx.app.sendMessageToSession(sessionId, `To remove this extension, please delete the folder ${extensionId} in the server's extension folder and restart the server.`, 'text/plain',
        {
          commands: [
          {
            title: 'Restart Server',
            id: 'rs',
            args: []
          }
        ]
      }

        )
      }
    } else {
      if (command === 'installed') {
        const allExtensions = ctx.app.extensions.all()
        const commands = allExtensions.map(e => ({ id: 'extensions', title: 'Show ' + e.id, args: ['show', e.id] }))
        await ctx.app.sendMessageToSession(sessionId, 'Installed Extensions\n\n' + allExtensions.map(e => `- ${e.id} ${e.config.title}: ${e.config.description || 'No description available'}`).join('\n'),
          'text/plain',
          {
            commands
          })

        return true
      } else if (command === 'list') {
        const knownFile = await getKnownExtensions()
        const commands = knownFile.core_extensions.concat(knownFile.known_extensions).map(function (e) {
          if (ctx.app.extensions.has(e.id)) {
            return {
              id: 'extensions', title: '☑️ ' + e.title, args: ['show', e.id], classes: ['w-32']
            }
          } else {
            return {
              id: 'extensions', title: '📥 ' + e.title, args: ['add', e.url],  classes: ['w-32']
            }
          }
        }
        ).sort((a, b) => a.title.localeCompare(b.title))
        const extensionList = {
          core: knownFile.core_extensions?.map(e => {
            return {type: 'core', installed: `${ctx.app.extensions.has(e.id)}`, id: `${e.id}`, title: `${e.title}`, description: `${e.description}`, url: `${e.url}`};
          }).sort((a, b) => a.title.localeCompare(b.title)),
          premium: knownFile.premium_extensions?.map(e => {
            return {type: 'premium', installed: `${ctx.app.extensions.has(e.id)}`, id: `${e.id}`, title: `${e.title}`, description: `${e.description}`, url: `${e.url}`};
          }).sort((a, b) => a.title.localeCompare(b.title)),
          known: knownFile.known_extensions?.map(e => {
            return {type: 'known', installed: `${ctx.app.extensions.has(e.id)}`, id: `${e.id}`, title: `${e.title}`, description: `${e.description}`, url: `${e.url}`};
          }).sort((a, b) => a.title.localeCompare(b.title)),
          available: knownFile.known_extensions?.filter(e => !e.deprecated).filter(e=>!ctx.app.extensions.has(e.id)).map(e => {
            return {type: 'available', installed: `${ctx.app.extensions.has(e.id)}`, id: `${e.id}`, title: `${e.title}`, description: `${e.description}`, url: `${e.url}`};
          }).sort((a, b) => a.title.localeCompare(b.title)),
        };

        await ctx.app.sendMessageToSession(sessionId, extensionList, 'omni/extension-list', { commands })
        return true
      } else if (command === 'updateAll') {
        const allExtensions = ctx.app.extensions.all()
        for (const extension of allExtensions) {
          try {
            await updateExtension(ctx, extension.id)
          } catch (ex) {
            ctx.integration.error('Failed to update extension: ' + extension.id, ex)
            ctx.app.sendErrorToSession(sessionId, 'Failed to update extension: ' + extension.id + ':' + ex.message)
          }
        }
        return true
      }

      await ctx.app.sendMessageToSession(sessionId, `--- Extension Management ---
        Use to install, manage and remove omnitool extensions.\n\
        Usage:\n/extensions add <git url> - add an extension
        /extensions show <id> - show details on an extension
        /extensions update <id> - update an extension
        /extensions remove <name> - remove an extension
        /extensions create <id> - create a new extension.
        /extensions installed - show installed extensions
        /extensions updateAll - update all installed extensions
        /extensions list - list all known extensions

        ⚠️  WARNING ⚠️
        Extensions are executable code and have the same level access as omnitool itself.
        Only install extensions from trusted sources!
        To safeguard credentials from potentially malicious extensions, especially in a multi-user environment, consider performing an advanced install that sandboxes the key management and REST execution engine a separate machine.`
      ,
      'text/plain',
      {
        commands:
            [
              {
                id: 'extensions',
                title: 'List Available',
                args: ['list'],
                classes: ['w-32']
              },
              {
                id: 'extensions',
                title: 'List Installed',
                args: ['installed'],
                classes: ['w-32']
              },
              {
                id: 'extensions',
                title: 'Update All',
                args: ['updateAll'],
                classes: ['w-32']
              }
            ]
      })

      return true
    }

    return true
  }

}

export default script