@npcify/web 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.
@@ -1,5 +1,6 @@
1
1
  <canvas id="npc" style="width: 100%; height: 480px; touch-action: none"></canvas>
2
2
  <button id="start" disabled>Start conversation</button>
3
3
  <div id="replies"></div>
4
+ <button id="talk" type="button" hidden disabled>Hold to talk</button>
4
5
  <p id="status" role="status"></p>
5
6
  <script type="module" src="/main.js"></script>
package/examples/main.js CHANGED
@@ -1,40 +1,71 @@
1
1
  // main.js
2
2
  import { mountNpcify } from '@npcify/web'
3
3
 
4
+ const config = {
5
+ baseUrl: 'https://api.npcify.ai',
6
+ characterId: '<character-id>',
7
+ apiKey: '<paste-your-scoped-api-key>',
8
+ serverRendering: false,
9
+ }
4
10
  const start = document.querySelector('#start')
5
11
  const replies = document.querySelector('#replies')
12
+ const talk = document.querySelector('#talk')
6
13
  const status = document.querySelector('#status')
14
+ const report = (error) => { status.textContent = error.message }
7
15
  const view = await mountNpcify({
8
16
  canvas: document.querySelector('#npc'),
9
- baseUrl: 'https://api.npcify.ai',
10
- characterId: 'YOUR_CHARACTER_ID',
11
- apiKey: 'YOUR_SCOPED_API_KEY',
12
- serverRendering: false, // Use the setting from Export → Web SDK.
13
- onError: (error) => { status.textContent = error.message },
17
+ ...config,
18
+ // Optional: 'options', 'text' and 'voice'. One mode forces it.
19
+ // inputModes: ['voice', 'options'],
20
+ onError: report,
14
21
  })
15
- const { npcify } = view
16
- npcify.onOptionsReady(({ options }) => {
22
+ const { character } = view
23
+ character.onOptionsReady(({ options }) => {
17
24
  replies.replaceChildren()
18
25
  for (const option of options) {
19
26
  const button = document.createElement('button')
20
27
  button.textContent = option.label
21
28
  button.onclick = async () => {
22
29
  replies.replaceChildren()
23
- try { await npcify.sendMessage(option.label) }
24
- catch (error) { status.textContent = error.message }
30
+ talk.disabled = true
31
+ try { await character.sendMessage(option.label) }
32
+ catch (error) { report(error) }
25
33
  }
26
34
  replies.append(button)
27
35
  }
36
+ talk.disabled = false
37
+ })
38
+ // Hold the button, or Space, to talk. The release sends the speech. Voice
39
+ // needs the character's voice input (its owner turns it on in the STT tab).
40
+ talk.hidden = !character.inputModes.includes('voice')
41
+ if (!talk.hidden) {
42
+ character.bindPushToTalk({
43
+ element: talk,
44
+ enabled: () => !talk.disabled,
45
+ onResult: ({ sent }) => {
46
+ if (sent) {
47
+ replies.replaceChildren()
48
+ talk.disabled = true
49
+ }
50
+ },
51
+ onError: report,
52
+ })
53
+ }
54
+ character.onListeningState((state) => {
55
+ talk.textContent =
56
+ { listening: 'Listening…', recognizing: 'Recognizing…' }[state] ?? 'Hold to talk'
28
57
  })
58
+ character.onTranscript(({ text }) => { status.textContent = text })
29
59
  start.disabled = false
30
60
  start.onclick = async () => {
31
61
  start.disabled = true
32
62
  try {
33
- await npcify.unlockAudio()
34
- await npcify.createConversation()
63
+ await character.unlockAudio()
64
+ await character.createConversation()
35
65
  } catch (error) {
36
- status.textContent = error.message
66
+ report(error)
37
67
  start.disabled = false
38
68
  }
39
69
  }
70
+ // When removing this view (for example, during navigation):
40
71
  window.addEventListener('pagehide', () => { void view.dispose() }, { once: true })
@@ -30,8 +30,8 @@ scene.add(sun)
30
30
  // 2. Load the AI character and attach it to your Three.js scene
31
31
  const character = await loadCharacter({
32
32
  baseUrl: 'https://api.npcify.ai',
33
- characterId: 'YOUR_CHARACTER_ID',
34
- apiKey: 'YOUR_SCOPED_API_KEY',
33
+ characterId: '<character-id>',
34
+ apiKey: '<paste-your-scoped-api-key>',
35
35
  parent: scene,
36
36
  gazeTarget: camera,
37
37
  shadows: true,
@@ -67,6 +67,16 @@ character.onOptionsReady(({ options }) => {
67
67
  }
68
68
  })
69
69
 
70
+ // 5. Voice: hold V to talk; the release sends the speech. A game can also
71
+ // listen without a key, for example when the character stops speaking:
72
+ // character.startListening({ endOnSilenceMs: 800 })
73
+ character.bindPushToTalk({
74
+ key: 'KeyV',
75
+ onError: (error) => {
76
+ status.textContent = error.message
77
+ },
78
+ })
79
+
70
80
  start.disabled = false
71
81
  start.onclick = async () => {
72
82
  start.disabled = true
@@ -79,7 +89,7 @@ start.onclick = async () => {
79
89
  }
80
90
  }
81
91
 
82
- // 5. Advance the character inside your game loop and read real-time state
92
+ // 6. Advance the character inside your game loop and read real-time state
83
93
  const clock = new THREE.Clock()
84
94
  const animate = function () {
85
95
  requestAnimationFrame(animate)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@npcify/web",
3
- "version": "0.2.1",
3
+ "version": "0.4.0",
4
4
  "description": "Framework-independent Npcify characters, live conversations and baked performances for the web",
5
5
  "type": "module",
6
6
  "license": "UNLICENSED",
@@ -35,8 +35,8 @@
35
35
  "tsup": "^8.5.0",
36
36
  "typescript": "^5.9.3",
37
37
  "@npcify/shared": "^0.1.0",
38
- "@npcify/vrm-renderer": "^0.1.0",
39
- "@npcify/web-client": "^0.0.0"
38
+ "@npcify/web-client": "^0.0.0",
39
+ "@npcify/vrm-renderer": "^0.1.0"
40
40
  },
41
41
  "repository": {
42
42
  "type": "git",