@plucky-ai/react 0.2.1 → 0.3.1
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/README.md +22 -7
- package/dist/index.cjs +24 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +11 -7
- package/dist/index.d.ts +11 -7
- package/dist/index.js +26 -15
- package/dist/index.js.map +1 -1
- package/dist/package.json +1 -1
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @plucky-ai/react
|
|
2
2
|
|
|
3
|
-
A React hook for using the Plucky Chat SDK.
|
|
3
|
+
A React hook for using the Plucky Chat SDK. Access the full docs [here](https://docs.plucky.ai).
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -14,12 +14,27 @@ pnpm add @plucky-ai/react
|
|
|
14
14
|
|
|
15
15
|
## Usage
|
|
16
16
|
|
|
17
|
-
```
|
|
18
|
-
import {
|
|
17
|
+
```tsx
|
|
18
|
+
import { useTools } from '@plucky-ai/react'
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
function MyComponent(props: { temperature: number }) {
|
|
21
|
+
useTools({
|
|
22
|
+
context: {
|
|
23
|
+
temperature,
|
|
24
|
+
},
|
|
25
|
+
tools: [
|
|
26
|
+
{
|
|
27
|
+
name: 'get_current_weather',
|
|
28
|
+
description: 'Get the current weather',
|
|
29
|
+
defaultLoadingText: 'Checking the weather...',
|
|
30
|
+
defaultSuccessText: 'Weather retrieved.',
|
|
31
|
+
cb: async (_, { temperature }) => {
|
|
32
|
+
return `It's currently ${temperature} degrees.`
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
})
|
|
23
37
|
|
|
24
|
-
|
|
38
|
+
return <div>My component content here</div>
|
|
39
|
+
}
|
|
25
40
|
```
|
package/dist/index.cjs
CHANGED
|
@@ -26,25 +26,36 @@ __plucky_ai_chat_sdk = __toESM(__plucky_ai_chat_sdk);
|
|
|
26
26
|
let react = require("react");
|
|
27
27
|
react = __toESM(react);
|
|
28
28
|
|
|
29
|
-
//#region src/hooks/
|
|
30
|
-
function
|
|
31
|
-
const
|
|
29
|
+
//#region src/hooks/useTools.ts
|
|
30
|
+
function useLatest(value) {
|
|
31
|
+
const ref = (0, react.useRef)(value);
|
|
32
32
|
(0, react.useEffect)(() => {
|
|
33
|
-
|
|
34
|
-
}, [
|
|
33
|
+
ref.current = value;
|
|
34
|
+
}, [value]);
|
|
35
|
+
return ref;
|
|
36
|
+
}
|
|
37
|
+
function useTools(params) {
|
|
38
|
+
const contextRef = useLatest(params.context);
|
|
39
|
+
const { tools } = params;
|
|
40
|
+
const wrappedTools = (0, react.useMemo)(() => tools.map((tool) => {
|
|
41
|
+
const originalCb = tool.cb;
|
|
42
|
+
return {
|
|
43
|
+
...tool,
|
|
44
|
+
cb: async (input) => {
|
|
45
|
+
if (!contextRef.current) return "Tool not yet loaded.";
|
|
46
|
+
return originalCb(input, contextRef.current);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}), [tools, contextRef]);
|
|
35
50
|
(0, react.useEffect)(() => {
|
|
36
51
|
if (!tools || tools.length === 0) return;
|
|
37
|
-
|
|
38
|
-
api.registerManyTools(tools);
|
|
52
|
+
(0, __plucky_ai_chat_sdk.addTools)(wrappedTools);
|
|
39
53
|
return () => {
|
|
40
|
-
|
|
54
|
+
(0, __plucky_ai_chat_sdk.removeTools)(wrappedTools.map((tool) => tool.name));
|
|
41
55
|
};
|
|
42
|
-
}, [
|
|
43
|
-
return { sendMessage: (message) => {
|
|
44
|
-
(0, __plucky_ai_chat_sdk.getAPI)().sendMessage({ content: message });
|
|
45
|
-
} };
|
|
56
|
+
}, [wrappedTools]);
|
|
46
57
|
}
|
|
47
58
|
|
|
48
59
|
//#endregion
|
|
49
|
-
exports.
|
|
60
|
+
exports.useTools = useTools;
|
|
50
61
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":[],"sources":["../src/hooks/
|
|
1
|
+
{"version":3,"file":"index.cjs","names":[],"sources":["../src/hooks/useTools.ts"],"sourcesContent":["import { UseToolsParams } from '@/types'\nimport { addTools, removeTools } from '@plucky-ai/chat-sdk'\nimport { useEffect, useMemo, useRef } from 'react'\nfunction useLatest<T>(value: T) {\n const ref = useRef(value)\n useEffect(() => {\n ref.current = value\n }, [value])\n return ref\n}\n\nexport function useTools<TContext = Record<string, unknown>>(\n params: UseToolsParams<TContext>,\n) {\n const contextRef = useLatest(params.context)\n\n const { tools } = params\n const wrappedTools = useMemo(\n () =>\n tools.map((tool) => {\n const originalCb = tool.cb\n return {\n ...tool,\n cb: async (input: Record<string, unknown>) => {\n if (!contextRef.current) return 'Tool not yet loaded.'\n return originalCb(input, contextRef.current)\n },\n }\n }),\n [tools, contextRef],\n )\n\n useEffect(() => {\n if (!tools || tools.length === 0) return\n addTools(wrappedTools)\n return () => {\n removeTools(wrappedTools.map((tool) => tool.name))\n }\n }, [wrappedTools])\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,SAAS,UAAa,OAAU;CAC9B,MAAM,wBAAa,MAAM;AACzB,4BAAgB;AACd,MAAI,UAAU;IACb,CAAC,MAAM,CAAC;AACX,QAAO;;AAGT,SAAgB,SACd,QACA;CACA,MAAM,aAAa,UAAU,OAAO,QAAQ;CAE5C,MAAM,EAAE,UAAU;CAClB,MAAM,wCAEF,MAAM,KAAK,SAAS;EAClB,MAAM,aAAa,KAAK;AACxB,SAAO;GACL,GAAG;GACH,IAAI,OAAO,UAAmC;AAC5C,QAAI,CAAC,WAAW,QAAS,QAAO;AAChC,WAAO,WAAW,OAAO,WAAW,QAAQ;;GAE/C;GACD,EACJ,CAAC,OAAO,WAAW,CACpB;AAED,4BAAgB;AACd,MAAI,CAAC,SAAS,MAAM,WAAW,EAAG;AAClC,qCAAS,aAAa;AACtB,eAAa;AACX,yCAAY,aAAa,KAAK,SAAS,KAAK,KAAK,CAAC;;IAEnD,CAAC,aAAa,CAAC"}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
import { ToolConfig
|
|
1
|
+
import { ToolConfig } from "@plucky-ai/chat-sdk";
|
|
2
2
|
|
|
3
|
-
//#region src/hooks/useChat.d.ts
|
|
4
|
-
declare function useChat(params: Parameters<typeof init>[0]): {
|
|
5
|
-
sendMessage: (message: string) => void;
|
|
6
|
-
};
|
|
7
|
-
//#endregion
|
|
8
3
|
//#region src/types.d.ts
|
|
9
4
|
type UseChatParams = {
|
|
10
5
|
appId: string;
|
|
@@ -15,6 +10,15 @@ type UseChatParams = {
|
|
|
15
10
|
tools?: ToolConfig[];
|
|
16
11
|
tags?: string[];
|
|
17
12
|
};
|
|
13
|
+
type UseToolsParams<TContext = Record<string, unknown>> = {
|
|
14
|
+
context?: TContext;
|
|
15
|
+
tools: Array<Omit<ToolConfig, 'cb'> & {
|
|
16
|
+
cb: (input: Record<string, unknown>, context: TContext) => Promise<string> | string;
|
|
17
|
+
}>;
|
|
18
|
+
};
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/hooks/useTools.d.ts
|
|
21
|
+
declare function useTools<TContext = Record<string, unknown>>(params: UseToolsParams<TContext>): void;
|
|
18
22
|
//#endregion
|
|
19
|
-
export { UseChatParams,
|
|
23
|
+
export { UseChatParams, UseToolsParams, useTools };
|
|
20
24
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
import { ToolConfig
|
|
1
|
+
import { ToolConfig } from "@plucky-ai/chat-sdk";
|
|
2
2
|
|
|
3
|
-
//#region src/hooks/useChat.d.ts
|
|
4
|
-
declare function useChat(params: Parameters<typeof init>[0]): {
|
|
5
|
-
sendMessage: (message: string) => void;
|
|
6
|
-
};
|
|
7
|
-
//#endregion
|
|
8
3
|
//#region src/types.d.ts
|
|
9
4
|
type UseChatParams = {
|
|
10
5
|
appId: string;
|
|
@@ -15,6 +10,15 @@ type UseChatParams = {
|
|
|
15
10
|
tools?: ToolConfig[];
|
|
16
11
|
tags?: string[];
|
|
17
12
|
};
|
|
13
|
+
type UseToolsParams<TContext = Record<string, unknown>> = {
|
|
14
|
+
context?: TContext;
|
|
15
|
+
tools: Array<Omit<ToolConfig, 'cb'> & {
|
|
16
|
+
cb: (input: Record<string, unknown>, context: TContext) => Promise<string> | string;
|
|
17
|
+
}>;
|
|
18
|
+
};
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/hooks/useTools.d.ts
|
|
21
|
+
declare function useTools<TContext = Record<string, unknown>>(params: UseToolsParams<TContext>): void;
|
|
18
22
|
//#endregion
|
|
19
|
-
export { UseChatParams,
|
|
23
|
+
export { UseChatParams, UseToolsParams, useTools };
|
|
20
24
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,25 +1,36 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { useEffect } from "react";
|
|
1
|
+
import { addTools, removeTools } from "@plucky-ai/chat-sdk";
|
|
2
|
+
import { useEffect, useMemo, useRef } from "react";
|
|
3
3
|
|
|
4
|
-
//#region src/hooks/
|
|
5
|
-
function
|
|
6
|
-
const
|
|
4
|
+
//#region src/hooks/useTools.ts
|
|
5
|
+
function useLatest(value) {
|
|
6
|
+
const ref = useRef(value);
|
|
7
7
|
useEffect(() => {
|
|
8
|
-
|
|
9
|
-
}, [
|
|
8
|
+
ref.current = value;
|
|
9
|
+
}, [value]);
|
|
10
|
+
return ref;
|
|
11
|
+
}
|
|
12
|
+
function useTools(params) {
|
|
13
|
+
const contextRef = useLatest(params.context);
|
|
14
|
+
const { tools } = params;
|
|
15
|
+
const wrappedTools = useMemo(() => tools.map((tool) => {
|
|
16
|
+
const originalCb = tool.cb;
|
|
17
|
+
return {
|
|
18
|
+
...tool,
|
|
19
|
+
cb: async (input) => {
|
|
20
|
+
if (!contextRef.current) return "Tool not yet loaded.";
|
|
21
|
+
return originalCb(input, contextRef.current);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
}), [tools, contextRef]);
|
|
10
25
|
useEffect(() => {
|
|
11
26
|
if (!tools || tools.length === 0) return;
|
|
12
|
-
|
|
13
|
-
api.registerManyTools(tools);
|
|
27
|
+
addTools(wrappedTools);
|
|
14
28
|
return () => {
|
|
15
|
-
|
|
29
|
+
removeTools(wrappedTools.map((tool) => tool.name));
|
|
16
30
|
};
|
|
17
|
-
}, [
|
|
18
|
-
return { sendMessage: (message) => {
|
|
19
|
-
getAPI().sendMessage({ content: message });
|
|
20
|
-
} };
|
|
31
|
+
}, [wrappedTools]);
|
|
21
32
|
}
|
|
22
33
|
|
|
23
34
|
//#endregion
|
|
24
|
-
export {
|
|
35
|
+
export { useTools };
|
|
25
36
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/hooks/
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/hooks/useTools.ts"],"sourcesContent":["import { UseToolsParams } from '@/types'\nimport { addTools, removeTools } from '@plucky-ai/chat-sdk'\nimport { useEffect, useMemo, useRef } from 'react'\nfunction useLatest<T>(value: T) {\n const ref = useRef(value)\n useEffect(() => {\n ref.current = value\n }, [value])\n return ref\n}\n\nexport function useTools<TContext = Record<string, unknown>>(\n params: UseToolsParams<TContext>,\n) {\n const contextRef = useLatest(params.context)\n\n const { tools } = params\n const wrappedTools = useMemo(\n () =>\n tools.map((tool) => {\n const originalCb = tool.cb\n return {\n ...tool,\n cb: async (input: Record<string, unknown>) => {\n if (!contextRef.current) return 'Tool not yet loaded.'\n return originalCb(input, contextRef.current)\n },\n }\n }),\n [tools, contextRef],\n )\n\n useEffect(() => {\n if (!tools || tools.length === 0) return\n addTools(wrappedTools)\n return () => {\n removeTools(wrappedTools.map((tool) => tool.name))\n }\n }, [wrappedTools])\n}\n"],"mappings":";;;;AAGA,SAAS,UAAa,OAAU;CAC9B,MAAM,MAAM,OAAO,MAAM;AACzB,iBAAgB;AACd,MAAI,UAAU;IACb,CAAC,MAAM,CAAC;AACX,QAAO;;AAGT,SAAgB,SACd,QACA;CACA,MAAM,aAAa,UAAU,OAAO,QAAQ;CAE5C,MAAM,EAAE,UAAU;CAClB,MAAM,eAAe,cAEjB,MAAM,KAAK,SAAS;EAClB,MAAM,aAAa,KAAK;AACxB,SAAO;GACL,GAAG;GACH,IAAI,OAAO,UAAmC;AAC5C,QAAI,CAAC,WAAW,QAAS,QAAO;AAChC,WAAO,WAAW,OAAO,WAAW,QAAQ;;GAE/C;GACD,EACJ,CAAC,OAAO,WAAW,CACpB;AAED,iBAAgB;AACd,MAAI,CAAC,SAAS,MAAM,WAAW,EAAG;AAClC,WAAS,aAAa;AACtB,eAAa;AACX,eAAY,aAAa,KAAK,SAAS,KAAK,KAAK,CAAC;;IAEnD,CAAC,aAAa,CAAC"}
|
package/dist/package.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plucky-ai/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -13,15 +13,15 @@
|
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"react": "19.
|
|
16
|
+
"react": "19.2.1",
|
|
17
17
|
"zod": "^4.1.11",
|
|
18
|
-
"@plucky-ai/chat-sdk": "^0.
|
|
18
|
+
"@plucky-ai/chat-sdk": "^0.3.1"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"@types/react": "19.1.13",
|
|
22
|
-
"tsdown": "^0.15.
|
|
23
|
-
"tsx": "^4.20.
|
|
24
|
-
"typescript": "^5.9.
|
|
22
|
+
"tsdown": "^0.15.12",
|
|
23
|
+
"tsx": "^4.20.6",
|
|
24
|
+
"typescript": "^5.9.3"
|
|
25
25
|
},
|
|
26
26
|
"publishConfig": {
|
|
27
27
|
"access": "public",
|