@rork-ai/toolkit-sdk 0.2.3 → 0.2.6
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/lib/module/agent.js +119 -0
- package/lib/module/agent.js.map +1 -0
- package/lib/module/api.js +9 -0
- package/lib/module/api.js.map +1 -0
- package/lib/module/index.js +2 -3
- package/lib/module/index.js.map +1 -1
- package/lib/module/integrations.js +45 -0
- package/lib/module/integrations.js.map +1 -0
- package/lib/module/polyfills.js +14 -0
- package/lib/module/polyfills.js.map +1 -0
- package/lib/module/polyfills.web.js +4 -0
- package/lib/module/polyfills.web.js.map +1 -0
- package/lib/typescript/src/agent.d.ts +141 -0
- package/lib/typescript/src/agent.d.ts.map +1 -0
- package/lib/typescript/src/api.d.ts +3 -0
- package/lib/typescript/src/api.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +2 -1
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/integrations.d.ts +18 -0
- package/lib/typescript/src/integrations.d.ts.map +1 -0
- package/lib/typescript/src/polyfills.d.ts +2 -0
- package/lib/typescript/src/polyfills.d.ts.map +1 -0
- package/lib/typescript/src/polyfills.web.d.ts +2 -0
- package/lib/typescript/src/polyfills.web.d.ts.map +1 -0
- package/package.json +17 -19
- package/src/agent.ts +165 -0
- package/src/api.ts +8 -0
- package/src/index.tsx +2 -3
- package/src/integrations.ts +74 -0
- package/src/polyfills.ts +16 -0
- package/src/polyfills.web.ts +1 -0
- package/lib/module/index.web.js +0 -6
- package/lib/module/index.web.js.map +0 -1
- package/lib/typescript/src/index.web.d.ts +0 -2
- package/lib/typescript/src/index.web.d.ts.map +0 -1
- package/src/index.web.tsx +0 -3
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { useChat } from "@ai-sdk/react";
|
|
4
|
+
import { DefaultChatTransport, lastAssistantMessageIsCompleteWithToolCalls } from "ai";
|
|
5
|
+
import { fetch as expoFetch } from "expo/fetch";
|
|
6
|
+
import { useCallback, useMemo } from "react";
|
|
7
|
+
import * as z from "zod/v4";
|
|
8
|
+
import { BASE_URL, withBaseUrl } from "./api.js";
|
|
9
|
+
import "./polyfills";
|
|
10
|
+
const AGENT_URL = withBaseUrl("/agent/chat");
|
|
11
|
+
export function createRorkTool(tool) {
|
|
12
|
+
return tool;
|
|
13
|
+
}
|
|
14
|
+
// use generateObject and generate text only if you need a single generation.
|
|
15
|
+
// When the chat history and agentic flows are not needed
|
|
16
|
+
// For example, parsing image to text in mutation.
|
|
17
|
+
// or generating a caption for image, or a summary
|
|
18
|
+
|
|
19
|
+
export async function generateObject(params) {
|
|
20
|
+
const GENERATE_OBJECT_URL = new URL("/llm/object", BASE_URL).toString();
|
|
21
|
+
const result = await fetch(GENERATE_OBJECT_URL, {
|
|
22
|
+
method: "POST",
|
|
23
|
+
body: JSON.stringify({
|
|
24
|
+
messages: params.messages,
|
|
25
|
+
schema: z.toJSONSchema(params.schema)
|
|
26
|
+
})
|
|
27
|
+
});
|
|
28
|
+
const data = await result.json();
|
|
29
|
+
return params.schema.parse(data.object);
|
|
30
|
+
}
|
|
31
|
+
export async function generateText(params) {
|
|
32
|
+
const GENERATE_TEXT_URL = new URL("/llm/text", BASE_URL).toString();
|
|
33
|
+
const messages = typeof params === "string" ? [{
|
|
34
|
+
role: "user",
|
|
35
|
+
content: params
|
|
36
|
+
}] : params.messages;
|
|
37
|
+
const result = await fetch(GENERATE_TEXT_URL, {
|
|
38
|
+
method: "POST",
|
|
39
|
+
body: JSON.stringify({
|
|
40
|
+
messages
|
|
41
|
+
})
|
|
42
|
+
});
|
|
43
|
+
const data = await result.json();
|
|
44
|
+
return data.completion;
|
|
45
|
+
}
|
|
46
|
+
export function useRorkAgent(options) {
|
|
47
|
+
const {
|
|
48
|
+
addToolResult,
|
|
49
|
+
...rest
|
|
50
|
+
} = useChat({
|
|
51
|
+
...options,
|
|
52
|
+
sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls,
|
|
53
|
+
transport: new DefaultChatTransport({
|
|
54
|
+
fetch: expoFetch,
|
|
55
|
+
api: AGENT_URL,
|
|
56
|
+
body: () => {
|
|
57
|
+
const toolsEntries = Object.entries(options.tools).map(([name, tool]) => [name, {
|
|
58
|
+
description: tool.description,
|
|
59
|
+
jsonSchema: z.toJSONSchema(tool.zodSchema)
|
|
60
|
+
}]);
|
|
61
|
+
return {
|
|
62
|
+
tools: Object.fromEntries(toolsEntries)
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
}),
|
|
66
|
+
onToolCall: async ({
|
|
67
|
+
toolCall
|
|
68
|
+
}) => {
|
|
69
|
+
const tool = options.tools[toolCall.toolName];
|
|
70
|
+
if (!tool) {
|
|
71
|
+
throw new Error(`Tool ${toolCall.dynamic} not found`);
|
|
72
|
+
}
|
|
73
|
+
if (!tool.execute) return;
|
|
74
|
+
const result = await tool.execute(toolCall.input);
|
|
75
|
+
addToolResult({
|
|
76
|
+
toolCallId: toolCall.toolCallId,
|
|
77
|
+
tool: toolCall.toolName,
|
|
78
|
+
output: result
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
const messages = useMemo(() => {
|
|
83
|
+
return rest.messages.map(m => {
|
|
84
|
+
const parts = m.parts.map(p => {
|
|
85
|
+
if (p.type === "text") return p;
|
|
86
|
+
if (p.type.startsWith("tool")) {
|
|
87
|
+
const toolPart = p;
|
|
88
|
+
const toolName = toolPart.type.replace(/^tool-/, "");
|
|
89
|
+
return {
|
|
90
|
+
...p,
|
|
91
|
+
type: "tool",
|
|
92
|
+
toolName
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
return undefined;
|
|
96
|
+
}).filter(p => p !== undefined);
|
|
97
|
+
return {
|
|
98
|
+
...m,
|
|
99
|
+
parts
|
|
100
|
+
};
|
|
101
|
+
});
|
|
102
|
+
}, [rest.messages]);
|
|
103
|
+
const sendMessage = useCallback(message => {
|
|
104
|
+
if (typeof message === "string") {
|
|
105
|
+
rest.sendMessage({
|
|
106
|
+
text: message
|
|
107
|
+
});
|
|
108
|
+
} else {
|
|
109
|
+
rest.sendMessage(message);
|
|
110
|
+
}
|
|
111
|
+
}, [rest]);
|
|
112
|
+
return {
|
|
113
|
+
...rest,
|
|
114
|
+
sendMessage,
|
|
115
|
+
messages,
|
|
116
|
+
addToolResult
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
//# sourceMappingURL=agent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useChat","DefaultChatTransport","lastAssistantMessageIsCompleteWithToolCalls","fetch","expoFetch","useCallback","useMemo","z","BASE_URL","withBaseUrl","AGENT_URL","createRorkTool","tool","generateObject","params","GENERATE_OBJECT_URL","URL","toString","result","method","body","JSON","stringify","messages","schema","toJSONSchema","data","json","parse","object","generateText","GENERATE_TEXT_URL","role","content","completion","useRorkAgent","options","addToolResult","rest","sendAutomaticallyWhen","transport","api","toolsEntries","Object","entries","tools","map","name","description","jsonSchema","zodSchema","fromEntries","onToolCall","toolCall","toolName","Error","dynamic","execute","input","toolCallId","output","m","parts","p","type","startsWith","toolPart","replace","undefined","filter","sendMessage","message","text"],"sourceRoot":"../../src","sources":["agent.ts"],"mappings":";;AAAA,SAASA,OAAO,QAA6B,eAAe;AAC5D,SACEC,oBAAoB,EACpBC,2CAA2C,QAGtC,IAAI;AACX,SAASC,KAAK,IAAIC,SAAS,QAAQ,YAAY;AAC/C,SAASC,WAAW,EAAEC,OAAO,QAAQ,OAAO;AAC5C,OAAO,KAAKC,CAAC,MAAM,QAAQ;AAE3B,SAASC,QAAQ,EAAEC,WAAW,QAAQ,UAAO;AAE7C,OAAO,aAAa;AAEpB,MAAMC,SAAS,GAAGD,WAAW,CAAC,aAAa,CAAC;AAQ5C,OAAO,SAASE,cAAcA,CAAsBC,IAAa,EAAW;EAC1E,OAAOA,IAAI;AACb;AAOA;AACA;AACA;AACA;;AAEA,OAAO,e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AMC,KAAK,GAAGD,CAAC,CAACC,KAAK,CAClBhB,GAAG,CAAEiB,CAAC,IAAK;QACV,IAAIA,CAAC,CAACC,IAAI,KAAK,MAAM,EAAE,OAAOD,CAAC;QAC/B,IAAIA,CAAC,CAACC,IAAI,CAACC,UAAU,CAAC,MAAM,CAAC,EAAE;UAC7B,MAAMC,QAAQ,GAAGH,CAAe;UAChC,MAAMT,QAAQ,GAAGY,QAAQ,CAACF,IAAI,CAACG,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC;UAEpD,OAAO;YACL,GAAIJ,CAAgB;YACpBC,IAAI,EAAE,MAAM;YACZV;UACF,CAAC;QAIH;QAEA,OAAOc,SAAS;MAClB,CAAC,CAAC,CACDC,MAAM,CAAEN,CAAC,IAAKA,CAAC,KAAKK,SAAS,CAAC;MAEjC,OAAO;QAAE,GAAGP,CAAC;QAAEC;MAAM,CAAC;IACxB,CAAC,CAAC;EACJ,CAAC,EAAE,CAACxB,IAAI,CAACf,QAAQ,CAAC,CAAC;EAEnB,MAAM+C,WAAW,GAAGjE,WAAW,CAC5BkE,OAAwD,IAAK;IAC5D,IAAI,OAAOA,OAAO,KAAK,QAAQ,EAAE;MAC/BjC,IAAI,CAACgC,WAAW,CAAC;QACfE,IAAI,EAAED;MACR,CAAC,CAAC;IACJ,CAAC,MAAM;MACLjC,IAAI,CAACgC,WAAW,CAACC,OAAO,CAAC;IAC3B;EACF,CAAC,EACD,CAACjC,IAAI,CACP,CAAC;EAED,OAAO;IACL,GAAGA,IAAI;IACPgC,WAAW;IACX/C,QAAQ;IACRc;EACF,CAAC;AACH","ignoreList":[]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
export const BASE_URL = process.env["EXPO_PUBLIC_TOOLKIT_URL"] ?? "http://localhost:3005";
|
|
4
|
+
export function withBaseUrl(path) {
|
|
5
|
+
const cleanBaseUrl = BASE_URL.replace(/\/$/, "");
|
|
6
|
+
const cleanPath = path.replace(/^\//, "");
|
|
7
|
+
return `${cleanBaseUrl}/${cleanPath}`;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=api.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["BASE_URL","process","env","withBaseUrl","path","cleanBaseUrl","replace","cleanPath"],"sourceRoot":"../../src","sources":["api.ts"],"mappings":";;AAAA,OAAO,MAAMA,QAAQ,GACnBC,OAAO,CAACC,GAAG,CAAC,yBAAyB,CAAC,IAAI,uBAAuB;AAEnE,OAAO,SAASC,WAAWA,CAACC,IAAY,EAAE;EACxC,MAAMC,YAAY,GAAGL,QAAQ,CAACM,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC;EAChD,MAAMC,SAAS,GAAGH,IAAI,CAACE,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC;EACzC,OAAO,GAAGD,YAAY,IAAIE,SAAS,EAAE;AACvC","ignoreList":[]}
|
package/lib/module/index.js
CHANGED
package/lib/module/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":[
|
|
1
|
+
{"version":3,"names":[],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":";;AAAA,cAAc,YAAS;AACvB,cAAc,mBAAgB","ignoreList":[]}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { useMutation, useQuery } from "@tanstack/react-query";
|
|
4
|
+
import * as WebBrowser from "expo-web-browser";
|
|
5
|
+
import { withBaseUrl } from "./api.js";
|
|
6
|
+
const EMPTY_ARRAY = [];
|
|
7
|
+
async function waitForConnection(id) {
|
|
8
|
+
const response = await fetch(withBaseUrl(`/connection/wait/${id}`));
|
|
9
|
+
return response.json();
|
|
10
|
+
}
|
|
11
|
+
export function useConnections() {
|
|
12
|
+
const list = useQuery({
|
|
13
|
+
queryKey: ["rork__connections"],
|
|
14
|
+
queryFn: async () => {
|
|
15
|
+
const response = await fetch(withBaseUrl("/connections/list"));
|
|
16
|
+
return response.json();
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
const initiate = useMutation({
|
|
20
|
+
mutationFn: async toolkit => {
|
|
21
|
+
const authUrl = withBaseUrl(`/connection/init/${toolkit}`);
|
|
22
|
+
const response = await fetch(authUrl).then(res => res.json());
|
|
23
|
+
const data = response;
|
|
24
|
+
const resultPromise = WebBrowser.openAuthSessionAsync(data.redirectUrl, "demotoolkit://connection/callback");
|
|
25
|
+
const result = await resultPromise;
|
|
26
|
+
if (result.type === "success") {
|
|
27
|
+
list.refetch();
|
|
28
|
+
}
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
const disconnect = useMutation({
|
|
33
|
+
mutationFn: async toolkit => {
|
|
34
|
+
const response = await fetch(withBaseUrl(`/connection/disconnect/${toolkit}`));
|
|
35
|
+
return response.json();
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
return {
|
|
39
|
+
currentConnections: list.data?.connections ?? EMPTY_ARRAY,
|
|
40
|
+
refetchConnections: list.refetch,
|
|
41
|
+
initiate,
|
|
42
|
+
disconnect
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=integrations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useMutation","useQuery","WebBrowser","withBaseUrl","EMPTY_ARRAY","waitForConnection","id","response","fetch","json","useConnections","list","queryKey","queryFn","initiate","mutationFn","toolkit","authUrl","then","res","data","resultPromise","openAuthSessionAsync","redirectUrl","result","type","refetch","disconnect","currentConnections","connections","refetchConnections"],"sourceRoot":"../../src","sources":["integrations.ts"],"mappings":";;AAAA,SAASA,WAAW,EAAEC,QAAQ,QAAQ,uBAAuB;AAC7D,OAAO,KAAKC,UAAU,MAAM,kBAAkB;AAE9C,SAASC,WAAW,QAAQ,UAAO;AAcnC,MAAMC,WAAW,GAAG,EAAkB;AAEtC,eAAeC,iBAAiBA,CAACC,EAAU,EAAE;EAC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AC;IACxB;EACF,CAAC,CAAC;EAEF,OAAO;IACLmB,kBAAkB,EAAEjB,IAAI,CAACS,IAAI,EAAES,WAAW,IAAIzB,WAAW;IACzD0B,kBAAkB,EAAEnB,IAAI,CAACe,OAAO;IAChCZ,QAAQ;IACRa;EACF,CAAC;AACH","ignoreList":[]}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { TextDecoderStream, TextEncoderStream } from "@stardazed/streams-text-encoding";
|
|
4
|
+
import structuredClone from "@ungap/structured-clone";
|
|
5
|
+
// @ts-ignore
|
|
6
|
+
import { polyfillGlobal } from "react-native/Libraries/Utilities/PolyfillFunctions";
|
|
7
|
+
if (!("structuredClone" in globalThis)) {
|
|
8
|
+
console.log("polyfilling structuredClone");
|
|
9
|
+
polyfillGlobal("structuredClone", () => structuredClone);
|
|
10
|
+
}
|
|
11
|
+
polyfillGlobal("TextEncoderStream", () => TextEncoderStream);
|
|
12
|
+
polyfillGlobal("TextDecoderStream", () => TextDecoderStream);
|
|
13
|
+
export {};
|
|
14
|
+
//# sourceMappingURL=polyfills.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["TextDecoderStream","TextEncoderStream","structuredClone","polyfillGlobal","globalThis","console","log"],"sourceRoot":"../../src","sources":["polyfills.ts"],"mappings":";;AAAA,SACEA,iBAAiB,EACjBC,iBAAiB,QACZ,kCAAkC;AACzC,OAAOC,eAAe,MAAM,yBAAyB;AACrD;AACA,SAASC,cAAc,QAAQ,oDAAoD;AAEnF,IAAI,EAAE,iBAAiB,IAAIC,UAAU,CAAC,EAAE;EACtCC,OAAO,CAACC,GAAG,CAAC,6BAA6B,CAAC;EAC1CH,cAAc,CAAC,iBAAiB,EAAE,MAAMD,eAAe,CAAC;AAC1D;AACAC,cAAc,CAAC,mBAAmB,EAAE,MAAMF,iBAAiB,CAAC;AAC5DE,cAAc,CAAC,mBAAmB,EAAE,MAAMH,iBAAiB,CAAC;AAE5D","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":[],"sourceRoot":"../../src","sources":["polyfills.web.ts"],"mappings":";;AAAA","ignoreList":[]}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { type UseChatOptions } from "@ai-sdk/react";
|
|
2
|
+
import { type UIMessage } from "ai";
|
|
3
|
+
import * as z from "zod/v4";
|
|
4
|
+
import "./polyfills";
|
|
5
|
+
type Tool<T extends z.ZodType = z.ZodType> = {
|
|
6
|
+
description: string;
|
|
7
|
+
zodSchema: T;
|
|
8
|
+
execute?: (input: z.infer<T>) => Promise<string> | string;
|
|
9
|
+
};
|
|
10
|
+
export declare function createRorkTool<T extends z.ZodType>(tool: Tool<T>): Tool<T>;
|
|
11
|
+
type TextPart = {
|
|
12
|
+
type: "text";
|
|
13
|
+
text: string;
|
|
14
|
+
};
|
|
15
|
+
type ImagePart = {
|
|
16
|
+
type: "image";
|
|
17
|
+
image: string;
|
|
18
|
+
};
|
|
19
|
+
type UserMessage = {
|
|
20
|
+
role: "user";
|
|
21
|
+
content: string | (TextPart | ImagePart)[];
|
|
22
|
+
};
|
|
23
|
+
type AssistantMessage = {
|
|
24
|
+
role: "assistant";
|
|
25
|
+
content: string | TextPart[];
|
|
26
|
+
};
|
|
27
|
+
export declare function generateObject<T extends z.ZodType>(params: {
|
|
28
|
+
messages: (UserMessage | AssistantMessage)[];
|
|
29
|
+
schema: T;
|
|
30
|
+
}): Promise<z.infer<T>>;
|
|
31
|
+
export declare function generateText(params: string | {
|
|
32
|
+
messages: (UserMessage | AssistantMessage)[];
|
|
33
|
+
}): Promise<string>;
|
|
34
|
+
export declare function useRorkAgent<T extends Record<string, Tool<z.ZodType>>>(options: Omit<UseChatOptions<UIMessage>, "transport"> & {
|
|
35
|
+
tools: T;
|
|
36
|
+
}): {
|
|
37
|
+
sendMessage: (message: string | Parameters<(message?: (Omit<UIMessage<unknown, import("ai").UIDataTypes, import("ai").UITools>, "id" | "role"> & {
|
|
38
|
+
id?: string | undefined;
|
|
39
|
+
role?: "user" | "assistant" | "system" | undefined;
|
|
40
|
+
} & {
|
|
41
|
+
text?: never;
|
|
42
|
+
files?: never;
|
|
43
|
+
messageId?: string;
|
|
44
|
+
}) | {
|
|
45
|
+
text: string;
|
|
46
|
+
files?: FileList | import("ai").FileUIPart[];
|
|
47
|
+
metadata?: unknown;
|
|
48
|
+
parts?: never;
|
|
49
|
+
messageId?: string;
|
|
50
|
+
} | {
|
|
51
|
+
files: FileList | import("ai").FileUIPart[];
|
|
52
|
+
metadata?: unknown;
|
|
53
|
+
parts?: never;
|
|
54
|
+
messageId?: string;
|
|
55
|
+
} | undefined, options?: import("ai").ChatRequestOptions) => Promise<void>>[0]) => void;
|
|
56
|
+
messages: {
|
|
57
|
+
parts: (import("ai").TextUIPart | {
|
|
58
|
+
type: "tool";
|
|
59
|
+
toolName: string;
|
|
60
|
+
toolCallId: string;
|
|
61
|
+
providerExecuted?: boolean;
|
|
62
|
+
state: "input-streaming";
|
|
63
|
+
input: unknown;
|
|
64
|
+
output?: never;
|
|
65
|
+
errorText?: never;
|
|
66
|
+
} | {
|
|
67
|
+
type: "tool";
|
|
68
|
+
toolName: string;
|
|
69
|
+
toolCallId: string;
|
|
70
|
+
providerExecuted?: boolean;
|
|
71
|
+
state: "input-available";
|
|
72
|
+
input: unknown;
|
|
73
|
+
output?: never;
|
|
74
|
+
errorText?: never;
|
|
75
|
+
callProviderMetadata?: import("ai").ProviderMetadata;
|
|
76
|
+
} | {
|
|
77
|
+
type: "tool";
|
|
78
|
+
toolName: string;
|
|
79
|
+
toolCallId: string;
|
|
80
|
+
providerExecuted?: boolean;
|
|
81
|
+
state: "output-available";
|
|
82
|
+
input: unknown;
|
|
83
|
+
output: unknown;
|
|
84
|
+
errorText?: never;
|
|
85
|
+
callProviderMetadata?: import("ai").ProviderMetadata;
|
|
86
|
+
preliminary?: boolean;
|
|
87
|
+
} | {
|
|
88
|
+
type: "tool";
|
|
89
|
+
toolName: string;
|
|
90
|
+
toolCallId: string;
|
|
91
|
+
providerExecuted?: boolean;
|
|
92
|
+
state: "output-error";
|
|
93
|
+
input: unknown;
|
|
94
|
+
rawInput?: unknown;
|
|
95
|
+
output?: never;
|
|
96
|
+
errorText: string;
|
|
97
|
+
callProviderMetadata?: import("ai").ProviderMetadata;
|
|
98
|
+
})[];
|
|
99
|
+
id: string;
|
|
100
|
+
role: "system" | "user" | "assistant";
|
|
101
|
+
metadata?: unknown;
|
|
102
|
+
}[];
|
|
103
|
+
addToolResult: <TOOL extends string>({ state, tool, toolCallId, output, errorText, }: {
|
|
104
|
+
state?: "output-available";
|
|
105
|
+
tool: TOOL;
|
|
106
|
+
toolCallId: string;
|
|
107
|
+
output: unknown;
|
|
108
|
+
errorText?: never;
|
|
109
|
+
} | {
|
|
110
|
+
state: "output-error";
|
|
111
|
+
tool: TOOL;
|
|
112
|
+
toolCallId: string;
|
|
113
|
+
output?: never;
|
|
114
|
+
errorText: string;
|
|
115
|
+
}) => Promise<void>;
|
|
116
|
+
id: string;
|
|
117
|
+
setMessages: (messages: UIMessage<unknown, import("ai").UIDataTypes, import("ai").UITools>[] | ((messages: UIMessage<unknown, import("ai").UIDataTypes, import("ai").UITools>[]) => UIMessage<unknown, import("ai").UIDataTypes, import("ai").UITools>[])) => void;
|
|
118
|
+
error: Error | undefined;
|
|
119
|
+
status: import("ai").ChatStatus;
|
|
120
|
+
regenerate: ({ messageId, ...options }?: {
|
|
121
|
+
messageId?: string;
|
|
122
|
+
} & import("ai").ChatRequestOptions) => Promise<void>;
|
|
123
|
+
stop: () => Promise<void>;
|
|
124
|
+
resumeStream: (options?: import("ai").ChatRequestOptions) => Promise<void>;
|
|
125
|
+
addToolOutput: <TOOL extends string>({ state, tool, toolCallId, output, errorText, }: {
|
|
126
|
+
state?: "output-available";
|
|
127
|
+
tool: TOOL;
|
|
128
|
+
toolCallId: string;
|
|
129
|
+
output: unknown;
|
|
130
|
+
errorText?: never;
|
|
131
|
+
} | {
|
|
132
|
+
state: "output-error";
|
|
133
|
+
tool: TOOL;
|
|
134
|
+
toolCallId: string;
|
|
135
|
+
output?: never;
|
|
136
|
+
errorText: string;
|
|
137
|
+
}) => Promise<void>;
|
|
138
|
+
clearError: () => void;
|
|
139
|
+
};
|
|
140
|
+
export {};
|
|
141
|
+
//# sourceMappingURL=agent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../../../src/agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,KAAK,cAAc,EAAE,MAAM,eAAe,CAAC;AAC7D,OAAO,EAIL,KAAK,SAAS,EACf,MAAM,IAAI,CAAC;AAGZ,OAAO,KAAK,CAAC,MAAM,QAAQ,CAAC;AAI5B,OAAO,aAAa,CAAC;AAIrB,KAAK,IAAI,CAAC,CAAC,SAAS,CAAC,CAAC,OAAO,GAAG,CAAC,CAAC,OAAO,IAAI;IAC3C,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,CAAC,CAAC;IACb,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC;CAC3D,CAAC;AAEF,wBAAgB,cAAc,CAAC,CAAC,SAAS,CAAC,CAAC,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAE1E;AAED,KAAK,QAAQ,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAC/C,KAAK,SAAS,GAAG;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAClD,KAAK,WAAW,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,GAAG,CAAC,QAAQ,GAA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
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as WebBrowser from "expo-web-browser";
|
|
2
|
+
type Connection = {
|
|
3
|
+
id: string;
|
|
4
|
+
status: "INITIALIZING" | "INITIATED" | "ACTIVE" | "FAILED" | "EXPIRED" | "INACTIVE";
|
|
5
|
+
toolkit: string;
|
|
6
|
+
};
|
|
7
|
+
export declare function useConnections(): {
|
|
8
|
+
currentConnections: Connection[];
|
|
9
|
+
refetchConnections: (options?: import("@tanstack/react-query").RefetchOptions) => Promise<import("@tanstack/react-query").QueryObserverResult<{
|
|
10
|
+
connections: Connection[];
|
|
11
|
+
}, Error>>;
|
|
12
|
+
initiate: import("@tanstack/react-query").UseMutationResult<WebBrowser.WebBrowserAuthSessionResult, Error, string, unknown>;
|
|
13
|
+
disconnect: import("@tanstack/react-query").UseMutationResult<{
|
|
14
|
+
status: string;
|
|
15
|
+
}, Error, string, unknown>;
|
|
16
|
+
};
|
|
17
|
+
export {};
|
|
18
|
+
//# sourceMappingURL=integrations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"integrations.d.ts","sourceRoot":"","sources":["../../../src/integrations.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,UAAU,MAAM,kBAAkB,CAAC;AAI/C,KAAK,UAAU,GAAG;IAChB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EACF,cAAc,GACd,WAAW,GACX,QAAQ,GACR,QAAQ,GACR,SAAS,GACT,UAAU,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AASF,wBAAgB,cAAc;;;qBAKyB,UAAU,EAAE;;;;gBAkCjB,MAAM;;EAUvD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"polyfills.d.ts","sourceRoot":"","sources":["../../../src/polyfills.ts"],"names":[],"mappings":"AAeA,OAAO,EAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"polyfills.web.d.ts","sourceRoot":"","sources":["../../../src/polyfills.web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rork-ai/toolkit-sdk",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
4
4
|
"description": "toolkit-sdk",
|
|
5
5
|
"main": "./lib/module/index.js",
|
|
6
6
|
"types": "./lib/typescript/src/index.d.ts",
|
|
@@ -32,13 +32,13 @@
|
|
|
32
32
|
"!**/.*"
|
|
33
33
|
],
|
|
34
34
|
"scripts": {
|
|
35
|
-
"example": "yarn workspace @rork-ai/toolkit-sdk-example",
|
|
36
35
|
"test": "jest",
|
|
37
36
|
"typecheck": "tsc",
|
|
38
37
|
"lint": "eslint \"**/*.{js,ts,tsx}\"",
|
|
39
38
|
"clean": "del-cli lib",
|
|
40
39
|
"prepare": "bob build",
|
|
41
|
-
"release": "release-it --only-version --no-git"
|
|
40
|
+
"release": "release-it --only-version --no-git",
|
|
41
|
+
"start": "cd example && bun start"
|
|
42
42
|
},
|
|
43
43
|
"keywords": [
|
|
44
44
|
"react-native",
|
|
@@ -56,20 +56,18 @@
|
|
|
56
56
|
},
|
|
57
57
|
"homepage": "https://github.com/LevanKvirkvelia/rork-ai-toolkit-sdk#readme",
|
|
58
58
|
"publishConfig": {
|
|
59
|
-
"registry": "https://registry.npmjs.org/"
|
|
59
|
+
"registry": "https://registry.npmjs.org/",
|
|
60
|
+
"access": "public"
|
|
60
61
|
},
|
|
61
62
|
"devDependencies": {
|
|
62
|
-
"@commitlint/config-conventional": "^19.8.1",
|
|
63
63
|
"@eslint/compat": "^1.3.2",
|
|
64
64
|
"@eslint/eslintrc": "^3.3.1",
|
|
65
65
|
"@eslint/js": "^9.35.0",
|
|
66
|
-
"@evilmartians/lefthook": "^1.12.3",
|
|
67
66
|
"@react-native/babel-preset": "0.81.1",
|
|
68
67
|
"@react-native/eslint-config": "^0.81.1",
|
|
69
68
|
"@types/jest": "^29.5.14",
|
|
70
69
|
"@types/react": "^19.1.12",
|
|
71
|
-
"
|
|
72
|
-
"del-cli": "^6.0.0",
|
|
70
|
+
"@types/ungap__structured-clone": "^1.2.0",
|
|
73
71
|
"eslint": "^9.35.0",
|
|
74
72
|
"eslint-config-prettier": "^10.1.8",
|
|
75
73
|
"eslint-plugin-prettier": "^5.5.4",
|
|
@@ -82,8 +80,11 @@
|
|
|
82
80
|
"typescript": "^5.9.2"
|
|
83
81
|
},
|
|
84
82
|
"peerDependencies": {
|
|
85
|
-
"react": "*",
|
|
86
|
-
"
|
|
83
|
+
"react-native": "*",
|
|
84
|
+
"expo-web-browser": "*",
|
|
85
|
+
"expo": "*",
|
|
86
|
+
"@tanstack/react-query": "*",
|
|
87
|
+
"zod": "^4"
|
|
87
88
|
},
|
|
88
89
|
"workspaces": [
|
|
89
90
|
"example"
|
|
@@ -96,16 +97,7 @@
|
|
|
96
97
|
"<rootDir>/lib/"
|
|
97
98
|
]
|
|
98
99
|
},
|
|
99
|
-
"commitlint": {
|
|
100
|
-
"extends": [
|
|
101
|
-
"@commitlint/config-conventional"
|
|
102
|
-
]
|
|
103
|
-
},
|
|
104
100
|
"release-it": {
|
|
105
|
-
"git": {
|
|
106
|
-
"commitMessage": "chore: release ${version}",
|
|
107
|
-
"tagName": "v${version}"
|
|
108
|
-
},
|
|
109
101
|
"npm": {
|
|
110
102
|
"publish": true
|
|
111
103
|
},
|
|
@@ -142,5 +134,11 @@
|
|
|
142
134
|
"languages": "js",
|
|
143
135
|
"type": "library",
|
|
144
136
|
"version": "0.54.8"
|
|
137
|
+
},
|
|
138
|
+
"dependencies": {
|
|
139
|
+
"@ai-sdk/react": "^2.0.89",
|
|
140
|
+
"@stardazed/streams-text-encoding": "^1.0.2",
|
|
141
|
+
"@ungap/structured-clone": "^1.3.0",
|
|
142
|
+
"ai": "^5.0.89"
|
|
145
143
|
}
|
|
146
144
|
}
|
package/src/agent.ts
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { useChat, type UseChatOptions } from "@ai-sdk/react";
|
|
2
|
+
import {
|
|
3
|
+
DefaultChatTransport,
|
|
4
|
+
lastAssistantMessageIsCompleteWithToolCalls,
|
|
5
|
+
type ToolUIPart,
|
|
6
|
+
type UIMessage,
|
|
7
|
+
} from "ai";
|
|
8
|
+
import { fetch as expoFetch } from "expo/fetch";
|
|
9
|
+
import { useCallback, useMemo } from "react";
|
|
10
|
+
import * as z from "zod/v4";
|
|
11
|
+
|
|
12
|
+
import { BASE_URL, withBaseUrl } from "./api";
|
|
13
|
+
|
|
14
|
+
import "./polyfills";
|
|
15
|
+
|
|
16
|
+
const AGENT_URL = withBaseUrl("/agent/chat");
|
|
17
|
+
|
|
18
|
+
type Tool<T extends z.ZodType = z.ZodType> = {
|
|
19
|
+
description: string;
|
|
20
|
+
zodSchema: T;
|
|
21
|
+
execute?: (input: z.infer<T>) => Promise<string> | string;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export function createRorkTool<T extends z.ZodType>(tool: Tool<T>): Tool<T> {
|
|
25
|
+
return tool;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
type TextPart = { type: "text"; text: string };
|
|
29
|
+
type ImagePart = { type: "image"; image: string };
|
|
30
|
+
type UserMessage = { role: "user"; content: string | (TextPart | ImagePart)[] };
|
|
31
|
+
type AssistantMessage = { role: "assistant"; content: string | TextPart[] };
|
|
32
|
+
|
|
33
|
+
// use generateObject and generate text only if you need a single generation.
|
|
34
|
+
// When the chat history and agentic flows are not needed
|
|
35
|
+
// For example, parsing image to text in mutation.
|
|
36
|
+
// or generating a caption for image, or a summary
|
|
37
|
+
|
|
38
|
+
export async function generateObject<T extends z.ZodType>(params: {
|
|
39
|
+
messages: (UserMessage | AssistantMessage)[];
|
|
40
|
+
schema: T;
|
|
41
|
+
}): Promise<z.infer<T>> {
|
|
42
|
+
const GENERATE_OBJECT_URL = new URL("/llm/object", BASE_URL).toString();
|
|
43
|
+
const result = await fetch(GENERATE_OBJECT_URL, {
|
|
44
|
+
method: "POST",
|
|
45
|
+
body: JSON.stringify({
|
|
46
|
+
messages: params.messages,
|
|
47
|
+
schema: z.toJSONSchema(params.schema),
|
|
48
|
+
}),
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const data = await result.json();
|
|
52
|
+
|
|
53
|
+
return params.schema.parse(data.object);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function generateText(
|
|
57
|
+
params: string | { messages: (UserMessage | AssistantMessage)[] },
|
|
58
|
+
): Promise<string> {
|
|
59
|
+
const GENERATE_TEXT_URL = new URL("/llm/text", BASE_URL).toString();
|
|
60
|
+
const messages =
|
|
61
|
+
typeof params === "string"
|
|
62
|
+
? [{ role: "user", content: params }]
|
|
63
|
+
: params.messages;
|
|
64
|
+
const result = await fetch(GENERATE_TEXT_URL, {
|
|
65
|
+
method: "POST",
|
|
66
|
+
body: JSON.stringify({ messages }),
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const data = await result.json();
|
|
70
|
+
|
|
71
|
+
return data.completion;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function useRorkAgent<T extends Record<string, Tool<z.ZodType>>>(
|
|
75
|
+
options: Omit<UseChatOptions<UIMessage>, "transport"> & {
|
|
76
|
+
tools: T;
|
|
77
|
+
},
|
|
78
|
+
) {
|
|
79
|
+
const { addToolResult, ...rest } = useChat({
|
|
80
|
+
...options,
|
|
81
|
+
sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls,
|
|
82
|
+
transport: new DefaultChatTransport({
|
|
83
|
+
fetch: expoFetch as unknown as typeof globalThis.fetch,
|
|
84
|
+
api: AGENT_URL,
|
|
85
|
+
body: () => {
|
|
86
|
+
const toolsEntries = Object.entries(options.tools).map(
|
|
87
|
+
([name, tool]) => [
|
|
88
|
+
name,
|
|
89
|
+
{
|
|
90
|
+
description: tool.description,
|
|
91
|
+
jsonSchema: z.toJSONSchema(tool.zodSchema),
|
|
92
|
+
},
|
|
93
|
+
],
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
tools: Object.fromEntries(toolsEntries),
|
|
98
|
+
};
|
|
99
|
+
},
|
|
100
|
+
}),
|
|
101
|
+
onToolCall: async ({ toolCall }) => {
|
|
102
|
+
const tool = options.tools[toolCall.toolName];
|
|
103
|
+
|
|
104
|
+
if (!tool) {
|
|
105
|
+
throw new Error(`Tool ${toolCall.dynamic} not found`);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (!tool.execute) return;
|
|
109
|
+
|
|
110
|
+
const result = await tool.execute(toolCall.input);
|
|
111
|
+
addToolResult({
|
|
112
|
+
toolCallId: toolCall.toolCallId,
|
|
113
|
+
tool: toolCall.toolName,
|
|
114
|
+
output: result,
|
|
115
|
+
});
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
const messages = useMemo(() => {
|
|
120
|
+
return rest.messages.map((m) => {
|
|
121
|
+
const parts = m.parts
|
|
122
|
+
.map((p) => {
|
|
123
|
+
if (p.type === "text") return p;
|
|
124
|
+
if (p.type.startsWith("tool")) {
|
|
125
|
+
const toolPart = p as ToolUIPart;
|
|
126
|
+
const toolName = toolPart.type.replace(/^tool-/, "");
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
...(p as ToolUIPart),
|
|
130
|
+
type: "tool",
|
|
131
|
+
toolName,
|
|
132
|
+
} satisfies Omit<ToolUIPart, "type"> & {
|
|
133
|
+
type: "tool";
|
|
134
|
+
toolName: string;
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return undefined;
|
|
139
|
+
})
|
|
140
|
+
.filter((p) => p !== undefined);
|
|
141
|
+
|
|
142
|
+
return { ...m, parts };
|
|
143
|
+
});
|
|
144
|
+
}, [rest.messages]);
|
|
145
|
+
|
|
146
|
+
const sendMessage = useCallback(
|
|
147
|
+
(message: string | Parameters<typeof rest.sendMessage>[0]) => {
|
|
148
|
+
if (typeof message === "string") {
|
|
149
|
+
rest.sendMessage({
|
|
150
|
+
text: message,
|
|
151
|
+
});
|
|
152
|
+
} else {
|
|
153
|
+
rest.sendMessage(message);
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
[rest],
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
return {
|
|
160
|
+
...rest,
|
|
161
|
+
sendMessage,
|
|
162
|
+
messages,
|
|
163
|
+
addToolResult,
|
|
164
|
+
};
|
|
165
|
+
}
|
package/src/api.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export const BASE_URL =
|
|
2
|
+
process.env["EXPO_PUBLIC_TOOLKIT_URL"] ?? "http://localhost:3005";
|
|
3
|
+
|
|
4
|
+
export function withBaseUrl(path: string) {
|
|
5
|
+
const cleanBaseUrl = BASE_URL.replace(/\/$/, "");
|
|
6
|
+
const cleanPath = path.replace(/^\//, "");
|
|
7
|
+
return `${cleanBaseUrl}/${cleanPath}`;
|
|
8
|
+
}
|
package/src/index.tsx
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
}
|
|
1
|
+
export * from "./agent";
|
|
2
|
+
export * from "./integrations";
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { useMutation, useQuery } from "@tanstack/react-query";
|
|
2
|
+
import * as WebBrowser from "expo-web-browser";
|
|
3
|
+
|
|
4
|
+
import { withBaseUrl } from "./api";
|
|
5
|
+
|
|
6
|
+
type Connection = {
|
|
7
|
+
id: string;
|
|
8
|
+
status:
|
|
9
|
+
| "INITIALIZING"
|
|
10
|
+
| "INITIATED"
|
|
11
|
+
| "ACTIVE"
|
|
12
|
+
| "FAILED"
|
|
13
|
+
| "EXPIRED"
|
|
14
|
+
| "INACTIVE";
|
|
15
|
+
toolkit: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const EMPTY_ARRAY = [] as Connection[];
|
|
19
|
+
|
|
20
|
+
async function waitForConnection(id: string) {
|
|
21
|
+
const response = await fetch(withBaseUrl(`/connection/wait/${id}`));
|
|
22
|
+
return response.json() as Promise<{ status: string }>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function useConnections() {
|
|
26
|
+
const list = useQuery({
|
|
27
|
+
queryKey: ["rork__connections"],
|
|
28
|
+
queryFn: async () => {
|
|
29
|
+
const response = await fetch(withBaseUrl("/connections/list"));
|
|
30
|
+
return response.json() as Promise<{ connections: Connection[] }>;
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const initiate = useMutation({
|
|
35
|
+
mutationFn: async (toolkit: string) => {
|
|
36
|
+
const authUrl = withBaseUrl(`/connection/init/${toolkit}`);
|
|
37
|
+
const response = await fetch(authUrl).then((res) => res.json());
|
|
38
|
+
const data = response as {
|
|
39
|
+
id: string;
|
|
40
|
+
status: string;
|
|
41
|
+
redirectUrl: string;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const resultPromise = WebBrowser.openAuthSessionAsync(
|
|
45
|
+
data.redirectUrl,
|
|
46
|
+
"demotoolkit://connection/callback",
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
const result = await resultPromise;
|
|
50
|
+
|
|
51
|
+
if (result.type === "success") {
|
|
52
|
+
list.refetch();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return result;
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const disconnect = useMutation({
|
|
60
|
+
mutationFn: async (toolkit: string) => {
|
|
61
|
+
const response = await fetch(
|
|
62
|
+
withBaseUrl(`/connection/disconnect/${toolkit}`),
|
|
63
|
+
);
|
|
64
|
+
return response.json() as Promise<{ status: string }>;
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
currentConnections: list.data?.connections ?? EMPTY_ARRAY,
|
|
70
|
+
refetchConnections: list.refetch,
|
|
71
|
+
initiate,
|
|
72
|
+
disconnect,
|
|
73
|
+
};
|
|
74
|
+
}
|
package/src/polyfills.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {
|
|
2
|
+
TextDecoderStream,
|
|
3
|
+
TextEncoderStream,
|
|
4
|
+
} from "@stardazed/streams-text-encoding";
|
|
5
|
+
import structuredClone from "@ungap/structured-clone";
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
import { polyfillGlobal } from "react-native/Libraries/Utilities/PolyfillFunctions";
|
|
8
|
+
|
|
9
|
+
if (!("structuredClone" in globalThis)) {
|
|
10
|
+
console.log("polyfilling structuredClone");
|
|
11
|
+
polyfillGlobal("structuredClone", () => structuredClone);
|
|
12
|
+
}
|
|
13
|
+
polyfillGlobal("TextEncoderStream", () => TextEncoderStream);
|
|
14
|
+
polyfillGlobal("TextDecoderStream", () => TextDecoderStream);
|
|
15
|
+
|
|
16
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/module/index.web.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":["multiply","a","b"],"sourceRoot":"../../src","sources":["index.web.tsx"],"mappings":";;AAAA,OAAO,SAASA,QAAQA,CAACC,CAAS,EAAEC,CAAS,EAAU;EACrD,OAAOD,CAAC,GAAGC,CAAC;AACd","ignoreList":[]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.web.d.ts","sourceRoot":"","sources":["../../../src/index.web.tsx"],"names":[],"mappings":"AAAA,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAErD"}
|
package/src/index.web.tsx
DELETED