@surph_ai/sdk 0.0.26 → 0.0.27

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/index.js CHANGED
@@ -0,0 +1,5 @@
1
+ const { Surph } = require('./platform')
2
+
3
+ module.exports = {
4
+ surph: new Surph()
5
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@surph_ai/sdk",
3
- "version": "0.0.26",
3
+ "version": "0.0.27",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "types": "./types.d.ts",
@@ -0,0 +1,4 @@
1
+ module.exports = {
2
+ SURPH_URL: 'https://surph.ai',
3
+ RPC_URL: 'https://rpc.surph.ai'
4
+ }
@@ -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
+ }
@@ -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
+
@@ -96,8 +96,8 @@ router.post('/custom', async (req, res, next) => {
96
96
 
97
97
  const tools = await loadTool()
98
98
  const response = await handler({ args, context, user }, tools)
99
- if (resp.error) {
100
- throw new Error(resp.error);
99
+ if (response.error) {
100
+ throw new Error(response.error);
101
101
  }
102
102
 
103
103
  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;