@surph_ai/cli 0.0.9 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
package/toolserver/app.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const express = require('express')
|
|
2
2
|
const vinely = require('@vinely-ai/sdk')
|
|
3
|
+
const { config } = require('./middleware/index.js')
|
|
3
4
|
|
|
4
5
|
const app = express()
|
|
5
6
|
|
|
@@ -8,18 +9,22 @@ vinely.configureApp(app, {
|
|
|
8
9
|
static: 'public',
|
|
9
10
|
})
|
|
10
11
|
|
|
11
|
-
app.use(
|
|
12
|
-
res.setHeader('Access-Control-Allow-Origin', '*')
|
|
13
|
-
res.header('Access-Control-Allow-Headers', 'x-surph-user, Content-Type');
|
|
14
|
-
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
|
15
|
-
if (req.method === 'OPTIONS') return res.sendStatus(204);
|
|
16
|
-
next()
|
|
17
|
-
})
|
|
12
|
+
app.use(config)
|
|
18
13
|
|
|
19
|
-
|
|
14
|
+
// app.use((req, res, next) => {
|
|
15
|
+
// res.setHeader('Access-Control-Allow-Origin', '*')
|
|
16
|
+
// res.header('Access-Control-Allow-Headers', 'x-surph-user, Content-Type');
|
|
17
|
+
// res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
|
18
|
+
// if (req.method === 'OPTIONS') return res.sendStatus(204);
|
|
19
|
+
// next()
|
|
20
|
+
// })
|
|
21
|
+
|
|
22
|
+
const api = require('./routes/api')
|
|
20
23
|
const tool = require('./routes/tool')
|
|
24
|
+
const index = require('./routes/index')
|
|
21
25
|
|
|
22
|
-
app.use('/',
|
|
26
|
+
app.use('/api', api)
|
|
23
27
|
app.use('/tools', tool)
|
|
28
|
+
app.use('/', index)
|
|
24
29
|
|
|
25
30
|
module.exports.entry = vinely.server(app)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// this is meant to mimic production environment
|
|
2
|
+
// so no need to distinguish between local and production
|
|
3
|
+
|
|
4
|
+
let config = null;
|
|
5
|
+
|
|
6
|
+
const CDN = 'https://surph.s3.us-east-1.amazonaws.com/webapp/static';
|
|
7
|
+
|
|
8
|
+
const fetchDesktopConfig = async () => {
|
|
9
|
+
const response = await fetch('https://surph.s3.us-east-1.amazonaws.com/admin/desktop/config.json');
|
|
10
|
+
const data = await response.json();
|
|
11
|
+
return data;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const fetchWebConfig = async () => {
|
|
15
|
+
const response = await fetch('https://surph.s3.us-east-1.amazonaws.com/webapp/static/dist/prod/imports.json');
|
|
16
|
+
const data = await response.json();
|
|
17
|
+
return { imports: data }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = async (req, res, next) => {
|
|
21
|
+
const webConfig = await fetchWebConfig(); // always fetch the latest imports.json
|
|
22
|
+
|
|
23
|
+
if (config !== null) {
|
|
24
|
+
req.config = { ...config, ...webConfig };
|
|
25
|
+
// console.log('CACHED CONFIG: ', req.config);
|
|
26
|
+
return next();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const desktopConfig = await fetchDesktopConfig();
|
|
30
|
+
config = {
|
|
31
|
+
...desktopConfig,
|
|
32
|
+
...webConfig,
|
|
33
|
+
isLocal: false,
|
|
34
|
+
isDev: true, // this is undefined in production.
|
|
35
|
+
CDN,
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
req.config = config;
|
|
39
|
+
next();
|
|
40
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
const { Router } = require('express')
|
|
2
|
+
// import { REST_API_URL } from '../utils/constants.js'
|
|
3
|
+
|
|
4
|
+
const router = Router()
|
|
5
|
+
|
|
6
|
+
const BASE_URL = 'https://surph.ai'
|
|
7
|
+
// const BASE_URL = 'http://localhost:3005' // for local testing
|
|
8
|
+
const PATH_RE = /^\/[a-zA-Z0-9_\-/]+$/
|
|
9
|
+
|
|
10
|
+
router.post('*', async (req, res, next) => {
|
|
11
|
+
const path = req.path
|
|
12
|
+
if (!PATH_RE.test(path) || path.includes('//') || path.includes('..')) {
|
|
13
|
+
res.status(400).json({ error: 'invalid path' })
|
|
14
|
+
return
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
const url = `${BASE_URL}/api${path}`
|
|
19
|
+
const upstream = await fetch(`${url}`, {
|
|
20
|
+
method: 'POST',
|
|
21
|
+
body: JSON.stringify(req.body || {}),
|
|
22
|
+
headers: {
|
|
23
|
+
'Content-Type': 'application/json',
|
|
24
|
+
'Accept': 'application/json',
|
|
25
|
+
'x-surph-client': 'surph-webapp'
|
|
26
|
+
},
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
const body = await upstream.json()
|
|
30
|
+
const contentType = upstream.headers.get('content-type')
|
|
31
|
+
if (contentType) res.set('Content-Type', contentType)
|
|
32
|
+
res.status(upstream.status).json(body)
|
|
33
|
+
} catch (e) {
|
|
34
|
+
res.status(502).json({ error: e?.message || 'upstream api failed' })
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
router.get('*', async (req, res, next) => {
|
|
39
|
+
const path = req.path
|
|
40
|
+
if (!PATH_RE.test(path) || path.includes('//') || path.includes('..')) {
|
|
41
|
+
res.status(400).json({ error: 'invalid path' })
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
const url = `${BASE_URL}/api${path}`
|
|
47
|
+
const upstream = await fetch(`${url}`, {
|
|
48
|
+
method: 'GET',
|
|
49
|
+
headers: {
|
|
50
|
+
'Content-Type': 'application/json',
|
|
51
|
+
'Accept': 'application/json',
|
|
52
|
+
'x-surph-client': 'surph-webapp'
|
|
53
|
+
},
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
const body = await upstream.json()
|
|
57
|
+
const contentType = upstream.headers.get('content-type')
|
|
58
|
+
if (contentType) res.set('Content-Type', contentType)
|
|
59
|
+
res.status(upstream.status).json(body)
|
|
60
|
+
} catch (e) {
|
|
61
|
+
console.log('ERR: ' + e.message)
|
|
62
|
+
res.status(502).json({ error: e?.message || 'upstream api failed' })
|
|
63
|
+
}
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
module.exports = router
|
|
@@ -1,8 +1,27 @@
|
|
|
1
1
|
const { Router } = require('express')
|
|
2
2
|
const router = Router()
|
|
3
|
+
const manifest = require('../../../../../manifest.json')
|
|
3
4
|
|
|
4
|
-
router.get('/', (req, res, next) => {
|
|
5
|
-
|
|
5
|
+
router.get('/', async (req, res, next) => {
|
|
6
|
+
try {
|
|
7
|
+
const toolSlugs = Object.keys(manifest)
|
|
8
|
+
|
|
9
|
+
const context = {
|
|
10
|
+
env: 'sandbox',
|
|
11
|
+
config: { ...req.config, entry: 'web' },
|
|
12
|
+
tools: toolSlugs.map(slug => (manifest[slug])),
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
res.render('index', {
|
|
16
|
+
...context,
|
|
17
|
+
CDN: req.config.CDN,
|
|
18
|
+
preloaded: JSON.stringify(context)
|
|
19
|
+
})
|
|
20
|
+
} catch (error) {
|
|
21
|
+
res.json({
|
|
22
|
+
error: 'something went wrong'
|
|
23
|
+
})
|
|
24
|
+
}
|
|
6
25
|
})
|
|
7
26
|
|
|
8
27
|
module.exports = router
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
|
|
1
2
|
<!DOCTYPE html>
|
|
2
3
|
<html dir="ltr" lang="en-US">
|
|
3
4
|
<head>
|
|
@@ -7,14 +8,14 @@
|
|
|
7
8
|
<meta http-equiv="Content-Language" content="en" />
|
|
8
9
|
|
|
9
10
|
<title>Surph AI</title>
|
|
10
|
-
<link rel="shortcut icon" href="https://
|
|
11
|
-
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
|
|
12
|
-
<link rel="stylesheet" href="https://cdn.turbo360-dev.com/admin/assets/scaffold/css/style.min.css" />
|
|
11
|
+
<link rel="shortcut icon" href="https://surph.s3.us-east-1.amazonaws.com/admin/assets/icon-green.svg">
|
|
13
12
|
</head>
|
|
14
13
|
|
|
15
14
|
<body>
|
|
16
|
-
<div
|
|
17
|
-
|
|
18
|
-
</
|
|
15
|
+
<div id="root"></div>
|
|
16
|
+
|
|
17
|
+
<script type="text/javascript">window.__PRELOADED__ = {{{ preloaded }}}</script>
|
|
18
|
+
<script defer="defer" src="{{{ CDN }}}/dist/prod/{{{ config.imports.commons }}}"></script>
|
|
19
|
+
<script defer="defer" src="{{{ CDN }}}/dist/prod/{{{ config.imports.app }}}"></script>
|
|
19
20
|
</body>
|
|
20
21
|
</html>
|