@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 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
- ```typescript
18
- import { useChat } from '@plucky-ai/react'
17
+ ```tsx
18
+ import { useTools } from '@plucky-ai/react'
19
19
 
20
- const { sendMessage } = useChat({
21
- appId: '123',
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
- sendMessage('Hello, world!')
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/useChat.ts
30
- function useChat(params) {
31
- const { tools,...rest } = params;
29
+ //#region src/hooks/useTools.ts
30
+ function useLatest(value) {
31
+ const ref = (0, react.useRef)(value);
32
32
  (0, react.useEffect)(() => {
33
- (0, __plucky_ai_chat_sdk.init)({ ...rest });
34
- }, [rest]);
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
- const api = (0, __plucky_ai_chat_sdk.getAPI)();
38
- api.registerManyTools(tools);
52
+ (0, __plucky_ai_chat_sdk.addTools)(wrappedTools);
39
53
  return () => {
40
- tools.forEach((tool) => api.removeTool(tool.name));
54
+ (0, __plucky_ai_chat_sdk.removeTools)(wrappedTools.map((tool) => tool.name));
41
55
  };
42
- }, [tools]);
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.useChat = useChat;
60
+ exports.useTools = useTools;
50
61
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","names":[],"sources":["../src/hooks/useChat.ts"],"sourcesContent":["import { getAPI, init } from '@plucky-ai/chat-sdk'\nimport { useEffect } from 'react'\n\nexport function useChat(params: Parameters<typeof init>[0]) {\n const { tools, ...rest } = params\n\n useEffect(() => {\n init({\n ...rest,\n })\n }, [rest])\n useEffect(() => {\n if (!tools || tools.length === 0) return\n const api = getAPI()\n api.registerManyTools(tools)\n return () => {\n tools.forEach((tool) => api.removeTool(tool.name))\n }\n }, [tools])\n return {\n sendMessage: (message: string) => {\n getAPI().sendMessage({\n content: message,\n })\n },\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,SAAgB,QAAQ,QAAoC;CAC1D,MAAM,EAAE,MAAO,GAAG,SAAS;AAE3B,4BAAgB;AACd,iCAAK,EACH,GAAG,MACJ,CAAC;IACD,CAAC,KAAK,CAAC;AACV,4BAAgB;AACd,MAAI,CAAC,SAAS,MAAM,WAAW,EAAG;EAClC,MAAM,wCAAc;AACpB,MAAI,kBAAkB,MAAM;AAC5B,eAAa;AACX,SAAM,SAAS,SAAS,IAAI,WAAW,KAAK,KAAK,CAAC;;IAEnD,CAAC,MAAM,CAAC;AACX,QAAO,EACL,cAAc,YAAoB;AAChC,oCAAQ,CAAC,YAAY,EACnB,SAAS,SACV,CAAC;IAEL"}
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, init } from "@plucky-ai/chat-sdk";
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, useChat };
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, init } from "@plucky-ai/chat-sdk";
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, useChat };
23
+ export { UseChatParams, UseToolsParams, useTools };
20
24
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,25 +1,36 @@
1
- import { getAPI, init } from "@plucky-ai/chat-sdk";
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/useChat.ts
5
- function useChat(params) {
6
- const { tools,...rest } = params;
4
+ //#region src/hooks/useTools.ts
5
+ function useLatest(value) {
6
+ const ref = useRef(value);
7
7
  useEffect(() => {
8
- init({ ...rest });
9
- }, [rest]);
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
- const api = getAPI();
13
- api.registerManyTools(tools);
27
+ addTools(wrappedTools);
14
28
  return () => {
15
- tools.forEach((tool) => api.removeTool(tool.name));
29
+ removeTools(wrappedTools.map((tool) => tool.name));
16
30
  };
17
- }, [tools]);
18
- return { sendMessage: (message) => {
19
- getAPI().sendMessage({ content: message });
20
- } };
31
+ }, [wrappedTools]);
21
32
  }
22
33
 
23
34
  //#endregion
24
- export { useChat };
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/useChat.ts"],"sourcesContent":["import { getAPI, init } from '@plucky-ai/chat-sdk'\nimport { useEffect } from 'react'\n\nexport function useChat(params: Parameters<typeof init>[0]) {\n const { tools, ...rest } = params\n\n useEffect(() => {\n init({\n ...rest,\n })\n }, [rest])\n useEffect(() => {\n if (!tools || tools.length === 0) return\n const api = getAPI()\n api.registerManyTools(tools)\n return () => {\n tools.forEach((tool) => api.removeTool(tool.name))\n }\n }, [tools])\n return {\n sendMessage: (message: string) => {\n getAPI().sendMessage({\n content: message,\n })\n },\n }\n}\n"],"mappings":";;;;AAGA,SAAgB,QAAQ,QAAoC;CAC1D,MAAM,EAAE,MAAO,GAAG,SAAS;AAE3B,iBAAgB;AACd,OAAK,EACH,GAAG,MACJ,CAAC;IACD,CAAC,KAAK,CAAC;AACV,iBAAgB;AACd,MAAI,CAAC,SAAS,MAAM,WAAW,EAAG;EAClC,MAAM,MAAM,QAAQ;AACpB,MAAI,kBAAkB,MAAM;AAC5B,eAAa;AACX,SAAM,SAAS,SAAS,IAAI,WAAW,KAAK,KAAK,CAAC;;IAEnD,CAAC,MAAM,CAAC;AACX,QAAO,EACL,cAAc,YAAoB;AAChC,UAAQ,CAAC,YAAY,EACnB,SAAS,SACV,CAAC;IAEL"}
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plucky-ai/react",
3
- "version": "0.2.1",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "main": "./index.cjs",
6
6
  "module": "./index.js",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plucky-ai/react",
3
- "version": "0.2.1",
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.1.1",
16
+ "react": "19.2.1",
17
17
  "zod": "^4.1.11",
18
- "@plucky-ai/chat-sdk": "^0.2.1"
18
+ "@plucky-ai/chat-sdk": "^0.3.1"
19
19
  },
20
20
  "devDependencies": {
21
21
  "@types/react": "19.1.13",
22
- "tsdown": "^0.15.9",
23
- "tsx": "^4.20.5",
24
- "typescript": "^5.9.2"
22
+ "tsdown": "^0.15.12",
23
+ "tsx": "^4.20.6",
24
+ "typescript": "^5.9.3"
25
25
  },
26
26
  "publishConfig": {
27
27
  "access": "public",