@rork-ai/toolkit-sdk 0.2.2 → 0.2.5
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 +125 -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/typescript/src/agent.d.ts +140 -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/package.json +17 -16
- package/src/agent.ts +167 -0
- package/src/api.ts +8 -0
- package/src/index.tsx +2 -3
- package/src/integrations.ts +74 -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,125 @@
|
|
|
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
|
+
const AGENT_URL = withBaseUrl("/agent/chat");
|
|
10
|
+
export function createRorkTool(tool) {
|
|
11
|
+
return tool;
|
|
12
|
+
}
|
|
13
|
+
// use generateObject and generate text only if you need a single generation.
|
|
14
|
+
// When the chat history and agentic flows are not needed
|
|
15
|
+
// For example, parsing image to text in mutation.
|
|
16
|
+
// or generating a caption for image, or a summary
|
|
17
|
+
|
|
18
|
+
export async function generateObject(params) {
|
|
19
|
+
const GENERATE_OBJECT_URL = new URL("/llm/object", BASE_URL).toString();
|
|
20
|
+
const result = await fetch(GENERATE_OBJECT_URL, {
|
|
21
|
+
method: "POST",
|
|
22
|
+
body: JSON.stringify({
|
|
23
|
+
messages: params.messages,
|
|
24
|
+
schema: z.toJSONSchema(params.schema)
|
|
25
|
+
})
|
|
26
|
+
});
|
|
27
|
+
const data = await result.json();
|
|
28
|
+
return params.schema.parse(data.object);
|
|
29
|
+
}
|
|
30
|
+
export async function generateText(params) {
|
|
31
|
+
const GENERATE_TEXT_URL = new URL("/llm/text", BASE_URL).toString();
|
|
32
|
+
const messages = typeof params === "string" ? [{
|
|
33
|
+
role: "user",
|
|
34
|
+
content: params
|
|
35
|
+
}] : params.messages;
|
|
36
|
+
const result = await fetch(GENERATE_TEXT_URL, {
|
|
37
|
+
method: "POST",
|
|
38
|
+
body: JSON.stringify({
|
|
39
|
+
messages
|
|
40
|
+
})
|
|
41
|
+
});
|
|
42
|
+
const data = await result.json();
|
|
43
|
+
return data.completion;
|
|
44
|
+
}
|
|
45
|
+
export function useRorkAgent(options) {
|
|
46
|
+
const {
|
|
47
|
+
addToolResult,
|
|
48
|
+
...rest
|
|
49
|
+
} = useChat({
|
|
50
|
+
...options,
|
|
51
|
+
sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls,
|
|
52
|
+
transport: new DefaultChatTransport({
|
|
53
|
+
fetch: expoFetch,
|
|
54
|
+
api: AGENT_URL,
|
|
55
|
+
body: () => {
|
|
56
|
+
const toolsEntries = Object.entries(options.tools).map(([name, tool]) => [name, {
|
|
57
|
+
description: tool.description,
|
|
58
|
+
jsonSchema: z.toJSONSchema(tool.zodSchema)
|
|
59
|
+
}]);
|
|
60
|
+
return {
|
|
61
|
+
tools: Object.fromEntries(toolsEntries)
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
}),
|
|
65
|
+
onToolCall: async ({
|
|
66
|
+
toolCall
|
|
67
|
+
}) => {
|
|
68
|
+
const tool = options.tools[toolCall.toolName];
|
|
69
|
+
if (!tool) {
|
|
70
|
+
throw new Error(`Tool ${toolCall.dynamic} not found`);
|
|
71
|
+
}
|
|
72
|
+
if (!tool.execute) return;
|
|
73
|
+
const result = await tool.execute(toolCall.input);
|
|
74
|
+
addToolResult({
|
|
75
|
+
toolCallId: toolCall.toolCallId,
|
|
76
|
+
tool: toolCall.toolName,
|
|
77
|
+
output: result
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
const messages = useMemo(() => {
|
|
82
|
+
return rest.messages.map(m => {
|
|
83
|
+
const parts = m.parts.map(p => {
|
|
84
|
+
if (p.type === "text") return p;
|
|
85
|
+
if (p.type.startsWith("tool")) {
|
|
86
|
+
const toolPart = p;
|
|
87
|
+
const toolName = toolPart.type.replace(/^tool-/, "");
|
|
88
|
+
return {
|
|
89
|
+
...p,
|
|
90
|
+
type: "tool",
|
|
91
|
+
toolName
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
return undefined;
|
|
95
|
+
}).filter(p => p !== undefined);
|
|
96
|
+
return {
|
|
97
|
+
...m,
|
|
98
|
+
parts
|
|
99
|
+
};
|
|
100
|
+
});
|
|
101
|
+
}, [rest.messages]);
|
|
102
|
+
rest.sendMessage({
|
|
103
|
+
files: [{
|
|
104
|
+
type: "file",
|
|
105
|
+
mediaType: "image/png",
|
|
106
|
+
url: "https://www.google.com"
|
|
107
|
+
}]
|
|
108
|
+
});
|
|
109
|
+
const sendMessage = useCallback(message => {
|
|
110
|
+
if (typeof message === "string") {
|
|
111
|
+
rest.sendMessage({
|
|
112
|
+
text: message
|
|
113
|
+
});
|
|
114
|
+
} else {
|
|
115
|
+
rest.sendMessage(message);
|
|
116
|
+
}
|
|
117
|
+
}, [rest]);
|
|
118
|
+
return {
|
|
119
|
+
...rest,
|
|
120
|
+
sendMessage,
|
|
121
|
+
messages,
|
|
122
|
+
addToolResult
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
//# 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","files","mediaType","url","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,MAAMC,SAAS,GAAGD,WAAW,CAAC,aAAa,CAAC;AAQ5C,OAAO,SAASE,cAAcA,CAAsBC,IAAa,EAAW;EAC1E,OAAOA,IAAI;AACb;AAOA;AACA;AACA;AACA;;AAEA,OAAO,eAAeC,cAAcA,CAAsBC,MAGzD,EAAuB;EACtB,MAAMC,mBAAmB,GAAG,IAAIC,GAAG,CAAC,aAAa,EAAER,QAAQ,CAAC,CAACS,QAAQ,CAAC,CAAC;EACvE,MAAMC,MAAM,GAAG,MAAMf,KAAK,CAACY,m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AC,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;EACnBe,IAAI,CAACgC,WAAW,CAAC;IACfC,KAAK,EAAE,CACL;MAAEP,IAAI,EAAE,MAAM;MAAEQ,SAAS,EAAE,WAAW;MAAEC,GAAG,EAAE;IAAyB,CAAC;EAE3E,CAAC,CAAC;EACF,MAAMH,WAAW,GAAGjE,WAAW,CAC5BqE,OAAwD,IAAK;IAC5D,IAAI,OAAOA,OAAO,KAAK,QAAQ,EAAE;MAC/BpC,IAAI,CAACgC,WAAW,CAAC;QACfK,IAAI,EAAED;MACR,CAAC,CAAC;IACJ,CAAC,MAAM;MACLpC,IAAI,CAACgC,WAAW,CAACI,OAAO,CAAC;IAC3B;EACF,CAAC,EACD,CAACpC,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;EAC3C,MAAMC,QAAQ,GAAG,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C;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,140 @@
|
|
|
1
|
+
import { type UseChatOptions } from "@ai-sdk/react";
|
|
2
|
+
import { type UIMessage } from "ai";
|
|
3
|
+
import * as z from "zod/v4";
|
|
4
|
+
type Tool<T extends z.ZodType = z.ZodType> = {
|
|
5
|
+
description: string;
|
|
6
|
+
zodSchema: T;
|
|
7
|
+
execute?: (input: z.infer<T>) => Promise<string> | string;
|
|
8
|
+
};
|
|
9
|
+
export declare function createRorkTool<T extends z.ZodType>(tool: Tool<T>): Tool<T>;
|
|
10
|
+
type TextPart = {
|
|
11
|
+
type: "text";
|
|
12
|
+
text: string;
|
|
13
|
+
};
|
|
14
|
+
type ImagePart = {
|
|
15
|
+
type: "image";
|
|
16
|
+
image: string;
|
|
17
|
+
};
|
|
18
|
+
type UserMessage = {
|
|
19
|
+
role: "user";
|
|
20
|
+
content: string | (TextPart | ImagePart)[];
|
|
21
|
+
};
|
|
22
|
+
type AssistantMessage = {
|
|
23
|
+
role: "assistant";
|
|
24
|
+
content: string | TextPart[];
|
|
25
|
+
};
|
|
26
|
+
export declare function generateObject<T extends z.ZodType>(params: {
|
|
27
|
+
messages: (UserMessage | AssistantMessage)[];
|
|
28
|
+
schema: T;
|
|
29
|
+
}): Promise<z.infer<T>>;
|
|
30
|
+
export declare function generateText(params: string | {
|
|
31
|
+
messages: (UserMessage | AssistantMessage)[];
|
|
32
|
+
}): Promise<string>;
|
|
33
|
+
export declare function useRorkAgent<T extends Record<string, Tool<z.ZodType>>>(options: Omit<UseChatOptions<UIMessage>, "transport"> & {
|
|
34
|
+
tools: T;
|
|
35
|
+
}): {
|
|
36
|
+
sendMessage: (message: string | Parameters<(message?: (Omit<UIMessage<unknown, import("ai").UIDataTypes, import("ai").UITools>, "id" | "role"> & {
|
|
37
|
+
id?: string | undefined;
|
|
38
|
+
role?: "user" | "assistant" | "system" | undefined;
|
|
39
|
+
} & {
|
|
40
|
+
text?: never;
|
|
41
|
+
files?: never;
|
|
42
|
+
messageId?: string;
|
|
43
|
+
}) | {
|
|
44
|
+
text: string;
|
|
45
|
+
files?: FileList | import("ai").FileUIPart[];
|
|
46
|
+
metadata?: unknown;
|
|
47
|
+
parts?: never;
|
|
48
|
+
messageId?: string;
|
|
49
|
+
} | {
|
|
50
|
+
files: FileList | import("ai").FileUIPart[];
|
|
51
|
+
metadata?: unknown;
|
|
52
|
+
parts?: never;
|
|
53
|
+
messageId?: string;
|
|
54
|
+
} | undefined, options?: import("ai").ChatRequestOptions) => Promise<void>>[0]) => void;
|
|
55
|
+
messages: {
|
|
56
|
+
parts: (import("ai").TextUIPart | {
|
|
57
|
+
type: "tool";
|
|
58
|
+
toolName: string;
|
|
59
|
+
toolCallId: string;
|
|
60
|
+
providerExecuted?: boolean;
|
|
61
|
+
state: "input-streaming";
|
|
62
|
+
input: unknown;
|
|
63
|
+
output?: never;
|
|
64
|
+
errorText?: never;
|
|
65
|
+
} | {
|
|
66
|
+
type: "tool";
|
|
67
|
+
toolName: string;
|
|
68
|
+
toolCallId: string;
|
|
69
|
+
providerExecuted?: boolean;
|
|
70
|
+
state: "input-available";
|
|
71
|
+
input: unknown;
|
|
72
|
+
output?: never;
|
|
73
|
+
errorText?: never;
|
|
74
|
+
callProviderMetadata?: import("ai").ProviderMetadata;
|
|
75
|
+
} | {
|
|
76
|
+
type: "tool";
|
|
77
|
+
toolName: string;
|
|
78
|
+
toolCallId: string;
|
|
79
|
+
providerExecuted?: boolean;
|
|
80
|
+
state: "output-available";
|
|
81
|
+
input: unknown;
|
|
82
|
+
output: unknown;
|
|
83
|
+
errorText?: never;
|
|
84
|
+
callProviderMetadata?: import("ai").ProviderMetadata;
|
|
85
|
+
preliminary?: boolean;
|
|
86
|
+
} | {
|
|
87
|
+
type: "tool";
|
|
88
|
+
toolName: string;
|
|
89
|
+
toolCallId: string;
|
|
90
|
+
providerExecuted?: boolean;
|
|
91
|
+
state: "output-error";
|
|
92
|
+
input: unknown;
|
|
93
|
+
rawInput?: unknown;
|
|
94
|
+
output?: never;
|
|
95
|
+
errorText: string;
|
|
96
|
+
callProviderMetadata?: import("ai").ProviderMetadata;
|
|
97
|
+
})[];
|
|
98
|
+
id: string;
|
|
99
|
+
role: "system" | "user" | "assistant";
|
|
100
|
+
metadata?: unknown;
|
|
101
|
+
}[];
|
|
102
|
+
addToolResult: <TOOL extends string>({ state, tool, toolCallId, output, errorText, }: {
|
|
103
|
+
state?: "output-available";
|
|
104
|
+
tool: TOOL;
|
|
105
|
+
toolCallId: string;
|
|
106
|
+
output: unknown;
|
|
107
|
+
errorText?: never;
|
|
108
|
+
} | {
|
|
109
|
+
state: "output-error";
|
|
110
|
+
tool: TOOL;
|
|
111
|
+
toolCallId: string;
|
|
112
|
+
output?: never;
|
|
113
|
+
errorText: string;
|
|
114
|
+
}) => Promise<void>;
|
|
115
|
+
id: string;
|
|
116
|
+
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;
|
|
117
|
+
error: Error | undefined;
|
|
118
|
+
status: import("ai").ChatStatus;
|
|
119
|
+
regenerate: ({ messageId, ...options }?: {
|
|
120
|
+
messageId?: string;
|
|
121
|
+
} & import("ai").ChatRequestOptions) => Promise<void>;
|
|
122
|
+
stop: () => Promise<void>;
|
|
123
|
+
resumeStream: (options?: import("ai").ChatRequestOptions) => Promise<void>;
|
|
124
|
+
addToolOutput: <TOOL extends string>({ state, tool, toolCallId, output, errorText, }: {
|
|
125
|
+
state?: "output-available";
|
|
126
|
+
tool: TOOL;
|
|
127
|
+
toolCallId: string;
|
|
128
|
+
output: unknown;
|
|
129
|
+
errorText?: never;
|
|
130
|
+
} | {
|
|
131
|
+
state: "output-error";
|
|
132
|
+
tool: TOOL;
|
|
133
|
+
toolCallId: string;
|
|
134
|
+
output?: never;
|
|
135
|
+
errorText: string;
|
|
136
|
+
}) => Promise<void>;
|
|
137
|
+
clearError: () => void;
|
|
138
|
+
};
|
|
139
|
+
export {};
|
|
140
|
+
//# 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;AAM5B,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,GAAG,SA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|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"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"}
|
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.5",
|
|
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"
|
|
40
|
+
"release": "release-it --only-version --no-git",
|
|
41
|
+
"start": "cd example && bun start"
|
|
42
42
|
},
|
|
43
43
|
"keywords": [
|
|
44
44
|
"react-native",
|
|
@@ -56,21 +56,17 @@
|
|
|
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
|
-
"@release-it/conventional-changelog": "^10.0.1",
|
|
70
68
|
"@types/jest": "^29.5.14",
|
|
71
69
|
"@types/react": "^19.1.12",
|
|
72
|
-
"commitlint": "^19.8.1",
|
|
73
|
-
"del-cli": "^6.0.0",
|
|
74
70
|
"eslint": "^9.35.0",
|
|
75
71
|
"eslint-config-prettier": "^10.1.8",
|
|
76
72
|
"eslint-plugin-prettier": "^5.5.4",
|
|
@@ -83,8 +79,11 @@
|
|
|
83
79
|
"typescript": "^5.9.2"
|
|
84
80
|
},
|
|
85
81
|
"peerDependencies": {
|
|
86
|
-
"react": "*",
|
|
87
|
-
"
|
|
82
|
+
"react-native": "*",
|
|
83
|
+
"expo-web-browser": "*",
|
|
84
|
+
"expo": "*",
|
|
85
|
+
"@tanstack/react-query": "*",
|
|
86
|
+
"zod": "^4"
|
|
88
87
|
},
|
|
89
88
|
"workspaces": [
|
|
90
89
|
"example"
|
|
@@ -97,14 +96,12 @@
|
|
|
97
96
|
"<rootDir>/lib/"
|
|
98
97
|
]
|
|
99
98
|
},
|
|
100
|
-
"commitlint": {
|
|
101
|
-
"extends": [
|
|
102
|
-
"@commitlint/config-conventional"
|
|
103
|
-
]
|
|
104
|
-
},
|
|
105
99
|
"release-it": {
|
|
106
100
|
"npm": {
|
|
107
101
|
"publish": true
|
|
102
|
+
},
|
|
103
|
+
"github": {
|
|
104
|
+
"release": false
|
|
108
105
|
}
|
|
109
106
|
},
|
|
110
107
|
"prettier": {
|
|
@@ -136,5 +133,9 @@
|
|
|
136
133
|
"languages": "js",
|
|
137
134
|
"type": "library",
|
|
138
135
|
"version": "0.54.8"
|
|
136
|
+
},
|
|
137
|
+
"dependencies": {
|
|
138
|
+
"@ai-sdk/react": "^2.0.89",
|
|
139
|
+
"ai": "^5.0.89"
|
|
139
140
|
}
|
|
140
141
|
}
|
package/src/agent.ts
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
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
|
+
const AGENT_URL = withBaseUrl("/agent/chat");
|
|
15
|
+
|
|
16
|
+
type Tool<T extends z.ZodType = z.ZodType> = {
|
|
17
|
+
description: string;
|
|
18
|
+
zodSchema: T;
|
|
19
|
+
execute?: (input: z.infer<T>) => Promise<string> | string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export function createRorkTool<T extends z.ZodType>(tool: Tool<T>): Tool<T> {
|
|
23
|
+
return tool;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type TextPart = { type: "text"; text: string };
|
|
27
|
+
type ImagePart = { type: "image"; image: string };
|
|
28
|
+
type UserMessage = { role: "user"; content: string | (TextPart | ImagePart)[] };
|
|
29
|
+
type AssistantMessage = { role: "assistant"; content: string | TextPart[] };
|
|
30
|
+
|
|
31
|
+
// use generateObject and generate text only if you need a single generation.
|
|
32
|
+
// When the chat history and agentic flows are not needed
|
|
33
|
+
// For example, parsing image to text in mutation.
|
|
34
|
+
// or generating a caption for image, or a summary
|
|
35
|
+
|
|
36
|
+
export async function generateObject<T extends z.ZodType>(params: {
|
|
37
|
+
messages: (UserMessage | AssistantMessage)[];
|
|
38
|
+
schema: T;
|
|
39
|
+
}): Promise<z.infer<T>> {
|
|
40
|
+
const GENERATE_OBJECT_URL = new URL("/llm/object", BASE_URL).toString();
|
|
41
|
+
const result = await fetch(GENERATE_OBJECT_URL, {
|
|
42
|
+
method: "POST",
|
|
43
|
+
body: JSON.stringify({
|
|
44
|
+
messages: params.messages,
|
|
45
|
+
schema: z.toJSONSchema(params.schema),
|
|
46
|
+
}),
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const data = await result.json();
|
|
50
|
+
|
|
51
|
+
return params.schema.parse(data.object);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export async function generateText(
|
|
55
|
+
params: string | { messages: (UserMessage | AssistantMessage)[] },
|
|
56
|
+
): Promise<string> {
|
|
57
|
+
const GENERATE_TEXT_URL = new URL("/llm/text", BASE_URL).toString();
|
|
58
|
+
const messages =
|
|
59
|
+
typeof params === "string"
|
|
60
|
+
? [{ role: "user", content: params }]
|
|
61
|
+
: params.messages;
|
|
62
|
+
const result = await fetch(GENERATE_TEXT_URL, {
|
|
63
|
+
method: "POST",
|
|
64
|
+
body: JSON.stringify({ messages }),
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const data = await result.json();
|
|
68
|
+
|
|
69
|
+
return data.completion;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function useRorkAgent<T extends Record<string, Tool<z.ZodType>>>(
|
|
73
|
+
options: Omit<UseChatOptions<UIMessage>, "transport"> & {
|
|
74
|
+
tools: T;
|
|
75
|
+
},
|
|
76
|
+
) {
|
|
77
|
+
const { addToolResult, ...rest } = useChat({
|
|
78
|
+
...options,
|
|
79
|
+
sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls,
|
|
80
|
+
transport: new DefaultChatTransport({
|
|
81
|
+
fetch: expoFetch as unknown as typeof globalThis.fetch,
|
|
82
|
+
api: AGENT_URL,
|
|
83
|
+
body: () => {
|
|
84
|
+
const toolsEntries = Object.entries(options.tools).map(
|
|
85
|
+
([name, tool]) => [
|
|
86
|
+
name,
|
|
87
|
+
{
|
|
88
|
+
description: tool.description,
|
|
89
|
+
jsonSchema: z.toJSONSchema(tool.zodSchema),
|
|
90
|
+
},
|
|
91
|
+
],
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
tools: Object.fromEntries(toolsEntries),
|
|
96
|
+
};
|
|
97
|
+
},
|
|
98
|
+
}),
|
|
99
|
+
onToolCall: async ({ toolCall }) => {
|
|
100
|
+
const tool = options.tools[toolCall.toolName];
|
|
101
|
+
|
|
102
|
+
if (!tool) {
|
|
103
|
+
throw new Error(`Tool ${toolCall.dynamic} not found`);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (!tool.execute) return;
|
|
107
|
+
|
|
108
|
+
const result = await tool.execute(toolCall.input);
|
|
109
|
+
addToolResult({
|
|
110
|
+
toolCallId: toolCall.toolCallId,
|
|
111
|
+
tool: toolCall.toolName,
|
|
112
|
+
output: result,
|
|
113
|
+
});
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
const messages = useMemo(() => {
|
|
118
|
+
return rest.messages.map((m) => {
|
|
119
|
+
const parts = m.parts
|
|
120
|
+
.map((p) => {
|
|
121
|
+
if (p.type === "text") return p;
|
|
122
|
+
if (p.type.startsWith("tool")) {
|
|
123
|
+
const toolPart = p as ToolUIPart;
|
|
124
|
+
const toolName = toolPart.type.replace(/^tool-/, "");
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
...(p as ToolUIPart),
|
|
128
|
+
type: "tool",
|
|
129
|
+
toolName,
|
|
130
|
+
} satisfies Omit<ToolUIPart, "type"> & {
|
|
131
|
+
type: "tool";
|
|
132
|
+
toolName: string;
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return undefined;
|
|
137
|
+
})
|
|
138
|
+
.filter((p) => p !== undefined);
|
|
139
|
+
|
|
140
|
+
return { ...m, parts };
|
|
141
|
+
});
|
|
142
|
+
}, [rest.messages]);
|
|
143
|
+
rest.sendMessage({
|
|
144
|
+
files: [
|
|
145
|
+
{ type: "file", mediaType: "image/png", url: "https://www.google.com" },
|
|
146
|
+
],
|
|
147
|
+
});
|
|
148
|
+
const sendMessage = useCallback(
|
|
149
|
+
(message: string | Parameters<typeof rest.sendMessage>[0]) => {
|
|
150
|
+
if (typeof message === "string") {
|
|
151
|
+
rest.sendMessage({
|
|
152
|
+
text: message,
|
|
153
|
+
});
|
|
154
|
+
} else {
|
|
155
|
+
rest.sendMessage(message);
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
[rest],
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
...rest,
|
|
163
|
+
sendMessage,
|
|
164
|
+
messages,
|
|
165
|
+
addToolResult,
|
|
166
|
+
};
|
|
167
|
+
}
|
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/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