@npcify/react 0.1.0
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 +94 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +88 -0
- package/examples/App.jsx +52 -0
- package/package.json +46 -0
package/README.md
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# @npcify/react
|
|
2
|
+
|
|
3
|
+
React 18.3 or 19 integration for Npcify characters. The `NpcifyCanvas` component mounts
|
|
4
|
+
`@npcify/web`, resizes its canvas, and cleans up rendering and conversations when
|
|
5
|
+
removed. Both local WebGL and protected server rendering are supported.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @npcify/react
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Use this example in `App.jsx` in your existing React project:
|
|
12
|
+
|
|
13
|
+
```jsx
|
|
14
|
+
'use client'
|
|
15
|
+
|
|
16
|
+
import { useRef, useState } from 'react'
|
|
17
|
+
import { NpcifyCanvas } from '@npcify/react'
|
|
18
|
+
|
|
19
|
+
const config = {
|
|
20
|
+
baseUrl: 'https://api.npcify.ai',
|
|
21
|
+
characterId: '<character-id>',
|
|
22
|
+
apiKey: '<paste-your-scoped-api-key>',
|
|
23
|
+
serverRendering: false,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export default function App() {
|
|
27
|
+
const npcify = useRef(null)
|
|
28
|
+
const [ready, setReady] = useState(false)
|
|
29
|
+
const [started, setStarted] = useState(false)
|
|
30
|
+
const [replies, setReplies] = useState([])
|
|
31
|
+
const [status, setStatus] = useState('')
|
|
32
|
+
const reportError = (error) => setStatus(error.message)
|
|
33
|
+
const handleReady = (client) => {
|
|
34
|
+
npcify.current = client
|
|
35
|
+
client.onOptionsReady(({ options }) => setReplies(options))
|
|
36
|
+
setReady(true)
|
|
37
|
+
}
|
|
38
|
+
const start = async () => {
|
|
39
|
+
setStarted(true)
|
|
40
|
+
setStatus('')
|
|
41
|
+
try {
|
|
42
|
+
await npcify.current.unlockAudio()
|
|
43
|
+
await npcify.current.createConversation()
|
|
44
|
+
} catch (error) {
|
|
45
|
+
reportError(error)
|
|
46
|
+
setStarted(false)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const reply = async (label) => {
|
|
50
|
+
setReplies([])
|
|
51
|
+
try { await npcify.current.sendMessage(label) }
|
|
52
|
+
catch (error) { reportError(error) }
|
|
53
|
+
}
|
|
54
|
+
return (
|
|
55
|
+
<>
|
|
56
|
+
<NpcifyCanvas options={config} onReady={handleReady} onError={reportError}
|
|
57
|
+
style={{ height: 480 }} />
|
|
58
|
+
<button disabled={!ready || started} onClick={start}>Start conversation</button>
|
|
59
|
+
<div>{replies.map((option, index) => (
|
|
60
|
+
<button key={index} onClick={() => reply(option.label)}>{option.label}</button>
|
|
61
|
+
))}</div>
|
|
62
|
+
<p role="status">{status}</p>
|
|
63
|
+
</>
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Component API
|
|
69
|
+
|
|
70
|
+
- `options`: `baseUrl`, `characterId`, `apiKey`, `serverRendering`, and optional
|
|
71
|
+
`assetProfile` (`original` or `web-optimized-v1`). See Export → Web SDK for the
|
|
72
|
+
character's configuration. These are JavaScript props, never HTML attributes.
|
|
73
|
+
- `onReady(npcify)` and `onError(error)` callbacks expose the SDK and errors.
|
|
74
|
+
- `class`/`style` (React: `className`/`style`) customize the outer div. It defaults
|
|
75
|
+
to 100% width and 480px height. Give it a nonzero height.
|
|
76
|
+
|
|
77
|
+
Changing a configuration field replaces the SDK instance with a fresh canvas.
|
|
78
|
+
Register conversation callbacks on each ready event, and clear any application
|
|
79
|
+
state that belongs to the previous character when changing configuration.
|
|
80
|
+
Equivalent configuration values do not restart the renderer. Pending loads are
|
|
81
|
+
disposed when they finish after unmount; obsolete ready/error callbacks are
|
|
82
|
+
suppressed. Mounting does not start a conversation or request audio permission.
|
|
83
|
+
Start conversations from a user gesture, as shown above.
|
|
84
|
+
|
|
85
|
+
The component is safe to render on the server; browser loading starts after
|
|
86
|
+
mount. The package includes a `use client` directive for React server component environments and supports Strict Mode.
|
|
87
|
+
|
|
88
|
+
Create a scoped key in Export → API keys. Browser keys are visible to visitors;
|
|
89
|
+
keep character scopes narrow and use your own backend when a key must stay
|
|
90
|
+
private. API use is billed to the key owner. Rotation and scope changes revoke
|
|
91
|
+
live conversation connections; downloaded assets retain their existing lifetime.
|
|
92
|
+
|
|
93
|
+
See [@npcify/web](https://www.npmjs.com/package/@npcify/web) for the underlying SDK,
|
|
94
|
+
conversation methods, rendering modes, and deployment requirements.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { HTMLAttributes, ReactElement } from 'react';
|
|
2
|
+
import { Npcify, MountNpcifyOptions } from '@npcify/web';
|
|
3
|
+
export { Npcify, NpcifyView } from '@npcify/web';
|
|
4
|
+
|
|
5
|
+
type NpcifyOptions = Omit<MountNpcifyOptions, 'canvas' | 'onError'>;
|
|
6
|
+
interface NpcifyCallbacks {
|
|
7
|
+
onReady?: (npcify: Npcify) => void;
|
|
8
|
+
onError?: (error: Error) => void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface NpcifyCanvasProps extends Omit<HTMLAttributes<HTMLDivElement>, 'onError' | 'onLoad' | 'children' | 'dangerouslySetInnerHTML'>, NpcifyCallbacks {
|
|
12
|
+
options: NpcifyOptions;
|
|
13
|
+
}
|
|
14
|
+
/** Sized container with an automatically managed canvas and SDK lifecycle. */
|
|
15
|
+
declare const NpcifyCanvas: ({ options, onReady, onError, style, ...attributes }: NpcifyCanvasProps) => ReactElement;
|
|
16
|
+
|
|
17
|
+
export { NpcifyCanvas, type NpcifyCanvasProps, type NpcifyOptions };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
"use client";
|
|
3
|
+
|
|
4
|
+
// src/index.ts
|
|
5
|
+
import {
|
|
6
|
+
createElement,
|
|
7
|
+
useEffect,
|
|
8
|
+
useRef
|
|
9
|
+
} from "react";
|
|
10
|
+
|
|
11
|
+
// ../framework-bindings/lifecycle.ts
|
|
12
|
+
import { mountNpcify } from "@npcify/web";
|
|
13
|
+
var attachCharacter = function(host, options, callbacks) {
|
|
14
|
+
const canvas = document.createElement("canvas");
|
|
15
|
+
canvas.style.cssText = "display:block;width:100%;height:100%;touch-action:none";
|
|
16
|
+
host.append(canvas);
|
|
17
|
+
let active = true;
|
|
18
|
+
let dispose;
|
|
19
|
+
const report = (value) => {
|
|
20
|
+
const error = value instanceof Error ? value : new Error(String(value));
|
|
21
|
+
if (active) {
|
|
22
|
+
if (callbacks.onError) {
|
|
23
|
+
callbacks.onError(error);
|
|
24
|
+
} else {
|
|
25
|
+
console.error(error);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
void mountNpcify({ ...options, canvas, onError: report }).then(async (view) => {
|
|
30
|
+
if (!active) {
|
|
31
|
+
await view.dispose();
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
dispose = () => view.dispose();
|
|
35
|
+
callbacks.onReady?.(view.npcify);
|
|
36
|
+
}).catch(report);
|
|
37
|
+
return () => {
|
|
38
|
+
active = false;
|
|
39
|
+
canvas.remove();
|
|
40
|
+
void dispose?.().catch((error) => console.error(error));
|
|
41
|
+
dispose = void 0;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// src/index.ts
|
|
46
|
+
import { Npcify } from "@npcify/web";
|
|
47
|
+
var NpcifyCanvas = function({
|
|
48
|
+
options,
|
|
49
|
+
onReady,
|
|
50
|
+
onError,
|
|
51
|
+
style,
|
|
52
|
+
...attributes
|
|
53
|
+
}) {
|
|
54
|
+
const host = useRef(null);
|
|
55
|
+
const callbacks = useRef({ onReady, onError });
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
callbacks.current = { onReady, onError };
|
|
58
|
+
}, [onReady, onError]);
|
|
59
|
+
const { baseUrl, characterId, apiKey, serverRendering, assetProfile } = options;
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
if (!host.current) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
return attachCharacter(
|
|
65
|
+
host.current,
|
|
66
|
+
{ baseUrl, characterId, apiKey, serverRendering, assetProfile },
|
|
67
|
+
{
|
|
68
|
+
onReady: (npcify) => callbacks.current.onReady?.(npcify),
|
|
69
|
+
onError: (error) => {
|
|
70
|
+
if (callbacks.current.onError) {
|
|
71
|
+
callbacks.current.onError(error);
|
|
72
|
+
} else {
|
|
73
|
+
console.error(error);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
);
|
|
78
|
+
}, [baseUrl, characterId, apiKey, serverRendering, assetProfile]);
|
|
79
|
+
return createElement("div", {
|
|
80
|
+
...attributes,
|
|
81
|
+
ref: host,
|
|
82
|
+
style: { width: "100%", height: 480, ...style }
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
export {
|
|
86
|
+
Npcify,
|
|
87
|
+
NpcifyCanvas
|
|
88
|
+
};
|
package/examples/App.jsx
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useRef, useState } from 'react'
|
|
4
|
+
import { NpcifyCanvas } from '@npcify/react'
|
|
5
|
+
|
|
6
|
+
const config = {
|
|
7
|
+
baseUrl: 'https://api.npcify.ai',
|
|
8
|
+
characterId: '<character-id>',
|
|
9
|
+
apiKey: '<paste-your-scoped-api-key>',
|
|
10
|
+
serverRendering: false,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export default function App() {
|
|
14
|
+
const npcify = useRef(null)
|
|
15
|
+
const [ready, setReady] = useState(false)
|
|
16
|
+
const [started, setStarted] = useState(false)
|
|
17
|
+
const [replies, setReplies] = useState([])
|
|
18
|
+
const [status, setStatus] = useState('')
|
|
19
|
+
const reportError = (error) => setStatus(error.message)
|
|
20
|
+
const handleReady = (client) => {
|
|
21
|
+
npcify.current = client
|
|
22
|
+
client.onOptionsReady(({ options }) => setReplies(options))
|
|
23
|
+
setReady(true)
|
|
24
|
+
}
|
|
25
|
+
const start = async () => {
|
|
26
|
+
setStarted(true)
|
|
27
|
+
setStatus('')
|
|
28
|
+
try {
|
|
29
|
+
await npcify.current.unlockAudio()
|
|
30
|
+
await npcify.current.createConversation()
|
|
31
|
+
} catch (error) {
|
|
32
|
+
reportError(error)
|
|
33
|
+
setStarted(false)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const reply = async (label) => {
|
|
37
|
+
setReplies([])
|
|
38
|
+
try { await npcify.current.sendMessage(label) }
|
|
39
|
+
catch (error) { reportError(error) }
|
|
40
|
+
}
|
|
41
|
+
return (
|
|
42
|
+
<>
|
|
43
|
+
<NpcifyCanvas options={config} onReady={handleReady} onError={reportError}
|
|
44
|
+
style={{ height: 480 }} />
|
|
45
|
+
<button disabled={!ready || started} onClick={start}>Start conversation</button>
|
|
46
|
+
<div>{replies.map((option, index) => (
|
|
47
|
+
<button key={index} onClick={() => reply(option.label)}>{option.label}</button>
|
|
48
|
+
))}</div>
|
|
49
|
+
<p role="status">{status}</p>
|
|
50
|
+
</>
|
|
51
|
+
)
|
|
52
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@npcify/react",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "React components for Npcify characters and conversations",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "UNLICENSED",
|
|
7
|
+
"homepage": "https://npcify.ai",
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist",
|
|
19
|
+
"examples"
|
|
20
|
+
],
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public",
|
|
23
|
+
"registry": "https://registry.npmjs.org/"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@npcify/web": "^0.1.0"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"react": "^18.3.0 || ^19.0.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"tsup": "^8.5.0",
|
|
33
|
+
"typescript": "^5.9.3",
|
|
34
|
+
"vitest": "^4.0.0",
|
|
35
|
+
"jsdom": "^27.0.0",
|
|
36
|
+
"react": "^19.3.0",
|
|
37
|
+
"react-dom": "^19.3.0",
|
|
38
|
+
"@types/react": "^19.0.0",
|
|
39
|
+
"@types/react-dom": "^19.0.0"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsup",
|
|
43
|
+
"typecheck": "tsc --noEmit",
|
|
44
|
+
"test": "vitest run"
|
|
45
|
+
}
|
|
46
|
+
}
|