@surph_ai/sdk 0.0.26 → 0.0.28
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/cli.js +3 -2
- package/index.js +5 -0
- package/package.json +1 -1
- package/platform/constants.js +4 -0
- package/platform/context.js +44 -0
- package/platform/index.js +27 -0
- package/platform/tools.js +29 -0
- package/platform/user.js +24 -0
- package/scaffold/tool/tools/index.ts +3 -1
- package/toolserver/routes/tool.js +10 -6
- package/types.d.ts +9 -0
package/cli.js
CHANGED
|
@@ -212,9 +212,10 @@ program.command('manifest')
|
|
|
212
212
|
})
|
|
213
213
|
|
|
214
214
|
const { response } = await resp.json()
|
|
215
|
-
const updated = { ...manifest, ...response }
|
|
215
|
+
// const updated = { ...manifest, ...response }
|
|
216
|
+
manifest[slug] = response
|
|
216
217
|
|
|
217
|
-
utils.write('manifest.json', JSON.stringify(
|
|
218
|
+
utils.write('manifest.json', JSON.stringify(manifest, null, 2) + '\n')
|
|
218
219
|
console.log(colors.white('\n\nManifest updated. Check manifest.json for changes.\n'))
|
|
219
220
|
} catch (error) {
|
|
220
221
|
utils.printError(error)
|
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
const { SURPH_URL } = require('./constants')
|
|
2
|
+
|
|
3
|
+
const fetchContext = async (contextSlug) => {
|
|
4
|
+
try {
|
|
5
|
+
const resp = await fetch(`${SURPH_URL}/api/contexts?slug=${contextSlug}`)
|
|
6
|
+
const { success, payload, error = null } = await resp.json()
|
|
7
|
+
if (!success) {
|
|
8
|
+
throw new Error(error)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
if (payload.length === 0) {
|
|
12
|
+
throw new Error(`Context ${contextSlug} not found.`)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const context = payload[0]
|
|
16
|
+
return context
|
|
17
|
+
} catch (error) {
|
|
18
|
+
throw error
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const searchContext = async (contextSlug, query) => {
|
|
23
|
+
try {
|
|
24
|
+
// const resp = await fetch(`${SURPH_URL}/api/contexts?slug=${contextSlug}`)
|
|
25
|
+
// const { success, payload, error = null } = await resp.json()
|
|
26
|
+
// if (!success) {
|
|
27
|
+
// throw new Error(error)
|
|
28
|
+
// }
|
|
29
|
+
|
|
30
|
+
// if (payload.length === 0) {
|
|
31
|
+
// throw new Error(`Context ${contextSlug} not found.`)
|
|
32
|
+
// }
|
|
33
|
+
|
|
34
|
+
// const context = payload[0]
|
|
35
|
+
// return context
|
|
36
|
+
} catch (error) {
|
|
37
|
+
throw error
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
module.exports = {
|
|
42
|
+
fetchContext,
|
|
43
|
+
searchContext
|
|
44
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const { fetchContext, searchContext } = require('./context')
|
|
2
|
+
const { fetchUser } = require('./user')
|
|
3
|
+
const { search } = require('./tools')
|
|
4
|
+
|
|
5
|
+
class Surph {
|
|
6
|
+
constructor() {
|
|
7
|
+
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
async fetchUser(username) {
|
|
11
|
+
return await fetchUser(username)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async fetchContext(slug) {
|
|
15
|
+
return await fetchContext(slug)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async searchContext(ctxSlug, query) {
|
|
19
|
+
return await searchContext(ctxSlug, query)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async search(query, ctxSlug = null) {
|
|
23
|
+
return await search(query, ctxSlug)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
module.exports = { Surph }
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const { SURPH_URL, RPC_URL } = require('./constants')
|
|
2
|
+
|
|
3
|
+
const search = async (query, contextSlug = null) => {
|
|
4
|
+
try {
|
|
5
|
+
const resp = await fetch(`${RPC_URL}/tools/search`, {
|
|
6
|
+
method: 'POST',
|
|
7
|
+
body: JSON.stringify({ query, context: contextSlug }),
|
|
8
|
+
headers: {
|
|
9
|
+
'Accept': 'application/json',
|
|
10
|
+
'Content-type': 'application/json',
|
|
11
|
+
'x-surph-client': 'surph-sdk'
|
|
12
|
+
}
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
const { response } = await resp.json()
|
|
16
|
+
if (response.error) {
|
|
17
|
+
throw new Error(response.error)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return response
|
|
21
|
+
} catch (error) {
|
|
22
|
+
throw error
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
module.exports = {
|
|
28
|
+
search,
|
|
29
|
+
}
|
package/platform/user.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const { SURPH_URL } = require('./constants')
|
|
2
|
+
|
|
3
|
+
const fetchUser = async (username) => {
|
|
4
|
+
try {
|
|
5
|
+
const resp = await fetch(`${SURPH_URL}/api/users?username=${username}`)
|
|
6
|
+
const { success, payload, error = null } = await resp.json()
|
|
7
|
+
if (!success) {
|
|
8
|
+
throw new Error(error)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
if (payload.length === 0) {
|
|
12
|
+
throw new Error(`User ${username} not found.`)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const user = payload[0]
|
|
16
|
+
return user
|
|
17
|
+
} catch (error) {
|
|
18
|
+
throw error
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = {
|
|
23
|
+
fetchUser,
|
|
24
|
+
}
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import type { ToolHandler, ToolContext, User } from '@surph_ai/sdk';
|
|
2
|
+
import { surph } from '@surph_ai/sdk';
|
|
2
3
|
|
|
3
4
|
type ToolArgs = {
|
|
4
5
|
// your custom tool args goes here
|
|
5
6
|
};
|
|
6
7
|
|
|
7
|
-
const handler: ToolHandler<ToolArgs> = async ({ args, user, context }) => {
|
|
8
|
+
const handler: ToolHandler<ToolArgs> = async ({ args, user, context }: Record<string, any>) => {
|
|
8
9
|
// your custom tool code goes here
|
|
9
10
|
};
|
|
10
11
|
|
|
11
12
|
export default handler;
|
|
13
|
+
|
|
@@ -74,8 +74,12 @@ router.get('/test', async (req, res, next) => {
|
|
|
74
74
|
throw new Error('No arguments provided.')
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
-
const
|
|
78
|
-
const
|
|
77
|
+
const context = req.query._context || null
|
|
78
|
+
const user = req.query._user || null
|
|
79
|
+
const params = { args: req.query, context, user }
|
|
80
|
+
|
|
81
|
+
const toolRunner = await loadTool()
|
|
82
|
+
const response = await handler(params, toolRunner)
|
|
79
83
|
res.json({ response })
|
|
80
84
|
} catch (error) {
|
|
81
85
|
res.json({
|
|
@@ -94,10 +98,10 @@ router.post('/custom', async (req, res, next) => {
|
|
|
94
98
|
throw new Error(`Invalid tool ${tool.slug}. Check .env file.`)
|
|
95
99
|
}
|
|
96
100
|
|
|
97
|
-
const
|
|
98
|
-
const response = await handler({ args, context, user },
|
|
99
|
-
if (
|
|
100
|
-
throw new Error(
|
|
101
|
+
const toolRunner = await loadTool()
|
|
102
|
+
const response = await handler({ args, context, user }, toolRunner)
|
|
103
|
+
if (response.error) {
|
|
104
|
+
throw new Error(response.error);
|
|
101
105
|
}
|
|
102
106
|
|
|
103
107
|
res.json({ response })
|
package/types.d.ts
CHANGED
|
@@ -61,3 +61,12 @@ export type ToolHandler<
|
|
|
61
61
|
TArgs = Record<string, unknown>,
|
|
62
62
|
TResult = unknown,
|
|
63
63
|
> = (input: ToolHandlerInput<TArgs>) => Promise<TResult> | TResult;
|
|
64
|
+
|
|
65
|
+
export interface Surph {
|
|
66
|
+
fetchUser(username: string): Promise<User>;
|
|
67
|
+
fetchContext(slug: string | ToolContext): Promise<unknown>;
|
|
68
|
+
searchContext(ctxSlug: string, query: string): Promise<unknown>;
|
|
69
|
+
search(query: string, ctxSlug?: string | null): Promise<unknown>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export declare const surph: Surph;
|