@npcify/react 0.2.1 → 0.4.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 CHANGED
@@ -68,8 +68,14 @@ export default function App() {
68
68
  ## Component API
69
69
 
70
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.
71
+ `assetProfile` (`original` or `web-optimized-v1`) and `inputModes` (`'options'`,
72
+ `'text'`, `'voice'`; `'voice'` needs the character's voice input, which the
73
+ owner turns on in the STT tab). See Export → Web SDK for the character's
74
+ configuration.
75
+ These are JavaScript props, never HTML attributes.
76
+ - Voice input: call `bindPushToTalk({ element })` on the ready SDK to talk while
77
+ the player holds a button or Space; the release sends the speech. See the
78
+ `@npcify/web` README (Voice input and input modes).
73
79
  - `onReady(npcify)` and `onError(error)` callbacks expose the SDK and errors.
74
80
  - `class`/`style` (React: `className`/`style`) customize the outer div. It defaults
75
81
  to 100% width and 480px height. Give it a nonzero height.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { HTMLAttributes, ReactElement } from 'react';
2
2
  import { Npcify, MountNpcifyOptions } from '@npcify/web';
3
- export { Character, CharacterAnimation, CharacterAnimationPlayOptions, CharacterAudio, CharacterDisposeOptions, CharacterExpressions, CharacterOptions, CharacterShadowOptions, CharacterSpeechState, CharacterTransformOptions, LoadCharacterOptions, Npcify, NpcifyCharacter, NpcifyView, createCharacter, loadCharacter } from '@npcify/web';
3
+ export { Character, CharacterAnimation, CharacterAnimationPlayOptions, CharacterAudio, CharacterDisposeOptions, CharacterExpressions, CharacterOptions, CharacterShadowOptions, CharacterSpeechState, CharacterTransformOptions, InputMode, InputModeError, ListeningOptions, ListeningState, LoadCharacterOptions, Npcify, NpcifyCharacter, NpcifyView, PushToTalkOptions, SpeechInputError, SpeechInputResult, TranscriptEvent, bindPushToTalk, createCharacter, loadCharacter } from '@npcify/web';
4
4
 
5
5
  type NpcifyOptions = Omit<MountNpcifyOptions, 'canvas' | 'onError'>;
6
6
  interface NpcifyCallbacks {
package/dist/index.js CHANGED
@@ -48,7 +48,10 @@ import {
48
48
  Npcify,
49
49
  NpcifyCharacter,
50
50
  createCharacter,
51
- loadCharacter
51
+ loadCharacter,
52
+ InputModeError,
53
+ SpeechInputError,
54
+ bindPushToTalk
52
55
  } from "@npcify/web";
53
56
  var NpcifyCanvas = function({
54
57
  options,
@@ -63,13 +66,21 @@ var NpcifyCanvas = function({
63
66
  callbacks.current = { onReady, onError };
64
67
  }, [onReady, onError]);
65
68
  const { baseUrl, characterId, apiKey, serverRendering, assetProfile } = options;
69
+ const inputModes = options.inputModes?.join(",");
66
70
  useEffect(() => {
67
71
  if (!host.current) {
68
72
  return;
69
73
  }
70
74
  return attachCharacter(
71
75
  host.current,
72
- { baseUrl, characterId, apiKey, serverRendering, assetProfile },
76
+ {
77
+ baseUrl,
78
+ characterId,
79
+ apiKey,
80
+ serverRendering,
81
+ assetProfile,
82
+ inputModes: inputModes ? inputModes.split(",") : void 0
83
+ },
73
84
  {
74
85
  onReady: (npcify) => callbacks.current.onReady?.(npcify),
75
86
  onError: (error) => {
@@ -81,7 +92,7 @@ var NpcifyCanvas = function({
81
92
  }
82
93
  }
83
94
  );
84
- }, [baseUrl, characterId, apiKey, serverRendering, assetProfile]);
95
+ }, [baseUrl, characterId, apiKey, serverRendering, assetProfile, inputModes]);
85
96
  return createElement("div", {
86
97
  ...attributes,
87
98
  ref: host,
@@ -90,9 +101,12 @@ var NpcifyCanvas = function({
90
101
  };
91
102
  export {
92
103
  Character,
104
+ InputModeError,
93
105
  Npcify,
94
106
  NpcifyCanvas,
95
107
  NpcifyCharacter,
108
+ SpeechInputError,
109
+ bindPushToTalk,
96
110
  createCharacter,
97
111
  loadCharacter
98
112
  };
package/examples/App.jsx CHANGED
@@ -1,6 +1,6 @@
1
1
  'use client'
2
2
 
3
- import { useRef, useState } from 'react'
3
+ import { useEffect, useRef, useState } from 'react'
4
4
  import { NpcifyCanvas } from '@npcify/react'
5
5
 
6
6
  const config = {
@@ -12,14 +12,35 @@ const config = {
12
12
 
13
13
  export default function App() {
14
14
  const npcify = useRef(null)
15
+ const talk = useRef(null)
16
+ const unbindTalk = useRef(() => {})
17
+ const replyCount = useRef(0)
15
18
  const [ready, setReady] = useState(false)
16
19
  const [started, setStarted] = useState(false)
17
20
  const [replies, setReplies] = useState([])
21
+ const [voice, setVoice] = useState(false)
22
+ const [listening, setListening] = useState('idle')
18
23
  const [status, setStatus] = useState('')
19
24
  const reportError = (error) => setStatus(error.message)
25
+ useEffect(() => { replyCount.current = replies.length }, [replies])
26
+ useEffect(() => () => unbindTalk.current(), [])
20
27
  const handleReady = (client) => {
21
28
  npcify.current = client
22
29
  client.onOptionsReady(({ options }) => setReplies(options))
30
+ // Hold the button, or Space, to talk. The release sends the speech. Voice
31
+ // needs the character's voice input (its owner turns it on in the STT tab).
32
+ const hasVoice = client.inputModes.includes('voice')
33
+ setVoice(hasVoice)
34
+ client.onListeningState(setListening)
35
+ client.onTranscript(({ text }) => setStatus(text))
36
+ if (hasVoice) {
37
+ unbindTalk.current = client.bindPushToTalk({
38
+ element: talk.current,
39
+ enabled: () => replyCount.current > 0,
40
+ onResult: ({ sent }) => { if (sent) setReplies([]) },
41
+ onError: reportError,
42
+ })
43
+ }
23
44
  setReady(true)
24
45
  }
25
46
  const start = async () => {
@@ -46,6 +67,9 @@ export default function App() {
46
67
  <div>{replies.map((option, index) => (
47
68
  <button key={index} onClick={() => reply(option.label)}>{option.label}</button>
48
69
  ))}</div>
70
+ <button ref={talk} type="button" hidden={!voice} disabled={replies.length === 0}>
71
+ {listening === 'idle' ? 'Hold to talk' : listening === 'recognizing' ? 'Recognizing…' : 'Listening…'}
72
+ </button>
49
73
  <p role="status">{status}</p>
50
74
  </>
51
75
  )
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@npcify/react",
3
- "version": "0.2.1",
3
+ "version": "0.4.0",
4
4
  "description": "React components for Npcify characters and conversations",
5
5
  "type": "module",
6
6
  "license": "UNLICENSED",
@@ -23,7 +23,7 @@
23
23
  "registry": "https://registry.npmjs.org/"
24
24
  },
25
25
  "dependencies": {
26
- "@npcify/web": "^0.2.1"
26
+ "@npcify/web": "^0.4.0"
27
27
  },
28
28
  "peerDependencies": {
29
29
  "react": "^18.3.0 || ^19.0.0"