@surph_ai/cli 0.0.8 → 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 +1 -1
- package/scaffold/index.js +3 -0
- package/scaffold/tool/examples/dealfinder/index.ts +45 -0
- package/scaffold/tool/examples/dealfinder/manifest.json +21 -0
- package/toolserver/app.js +14 -9
- package/toolserver/middleware/config.js +40 -0
- package/toolserver/middleware/index.js +5 -0
- package/toolserver/routes/api.js +66 -0
- package/toolserver/routes/index.js +21 -2
- package/toolserver/templates/index.html +7 -6
package/package.json
CHANGED
package/scaffold/index.js
CHANGED
|
@@ -79,6 +79,9 @@ module.exports = (name, type = 'app') => {
|
|
|
79
79
|
shell.cp('-R', path.join(__dirname, `tool/examples/weather/index.ts`), 'examples/weather/index.ts')
|
|
80
80
|
shell.cp('-R', path.join(__dirname, `tool/examples/weather/manifest.json`), 'examples/weather/manifest.json')
|
|
81
81
|
|
|
82
|
+
shell.mkdir('-p', 'examples/dealfinder')
|
|
83
|
+
shell.cp('-R', path.join(__dirname, `tool/examples/dealfinder/index.ts`), 'examples/dealfinder/index.ts')
|
|
84
|
+
shell.cp('-R', path.join(__dirname, `tool/examples/dealfinder/manifest.json`), 'examples/dealfinder/manifest.json')
|
|
82
85
|
|
|
83
86
|
|
|
84
87
|
utils.write('manifest-example.json', JSON.stringify({
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { ToolHandler } from '@surph_ai/sdk';
|
|
2
|
+
import { surph } from '@surph_ai/sdk';
|
|
3
|
+
|
|
4
|
+
// your custom tool args goes here
|
|
5
|
+
type ToolArgs = {
|
|
6
|
+
preferences: string[]
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const handler: ToolHandler<ToolArgs> = async ({ args, user, context }: Record<string, any>) => {
|
|
10
|
+
try {
|
|
11
|
+
if (!args) {
|
|
12
|
+
throw new Error('Args required.')
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (!context) {
|
|
16
|
+
throw new Error('Context required.')
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const { preferences } = args; // string[]
|
|
20
|
+
const searchQuery = preferences.join(', ')
|
|
21
|
+
|
|
22
|
+
const searchResults = await surph.search(searchQuery, context)
|
|
23
|
+
|
|
24
|
+
const promptResponse = await surph.prompt({
|
|
25
|
+
instructions: `searchResults is a JSON array data feed of events and announcements. Your job is to sort each item into one of three categories: offering free food, free event, or free giveaway (such as raffles, prizes, etc.).`,
|
|
26
|
+
prompt: `Given this list of items, separate them into free food, free event, or free giveaway:\n\n${JSON.stringify(searchResults)}`,
|
|
27
|
+
output: {
|
|
28
|
+
name: 'deal_search_results',
|
|
29
|
+
description: 'The deal search results',
|
|
30
|
+
fields: [
|
|
31
|
+
{ name: 'food', type: 'array', description: 'a list of events and announcements with free food.' },
|
|
32
|
+
{ name: 'event', type: 'array', description: 'a list of events that are free to attend.' },
|
|
33
|
+
{ name: 'items', type: 'array', description: 'a list of events and announcements with free items.' }
|
|
34
|
+
]
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
return promptResponse
|
|
39
|
+
} catch (error) {
|
|
40
|
+
throw error
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export default handler;
|
|
45
|
+
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"deal-finder-q9je7jou": {
|
|
3
|
+
"name": "deal-finder",
|
|
4
|
+
"slug": "deal-finder-q9je7jou",
|
|
5
|
+
"type": "custom",
|
|
6
|
+
"description": "Search the user's active Surph context for local deals, discounts, promotions, specials, sales, and related opportunities across food, drinks, restaurants, shops, events, entertainment, and other businesses or venues covered by the context. Use whenever the user asks to find, compare, filter, or recommend current or upcoming local deals—including requests for happy hours, meal specials, event discounts, retail sales, coupons, or promotions. Prefer this instead of webSearch when the requested local businesses, venues, events, or listings are represented in the active Surph context.",
|
|
7
|
+
"parameters": {
|
|
8
|
+
"type": "object",
|
|
9
|
+
"properties": {
|
|
10
|
+
"preferences": {
|
|
11
|
+
"type": "array",
|
|
12
|
+
"description": "Optional free-form preferences, categories, constraints, or deal criteria to prioritize, such as food, drinks, events, retail sales, happy hour, nearby neighborhoods, budget, dates, or specific businesses.",
|
|
13
|
+
"items": {
|
|
14
|
+
"type": "string"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"required": []
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
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>
|