@npcify/web 0.2.1 → 0.3.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 +65 -3
- package/dist/index.d.ts +208 -4
- package/dist/index.js +1960 -106
- package/examples/index.html +1 -0
- package/examples/main.js +40 -12
- package/examples/three-game.js +13 -3
- package/package.json +1 -1
package/examples/index.html
CHANGED
|
@@ -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,68 @@
|
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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 {
|
|
16
|
-
|
|
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
|
-
|
|
24
|
-
|
|
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.
|
|
39
|
+
talk.hidden = !character.inputModes.includes('voice')
|
|
40
|
+
character.bindPushToTalk({
|
|
41
|
+
element: talk,
|
|
42
|
+
enabled: () => !talk.disabled,
|
|
43
|
+
onResult: ({ sent }) => {
|
|
44
|
+
if (sent) {
|
|
45
|
+
replies.replaceChildren()
|
|
46
|
+
talk.disabled = true
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
onError: report,
|
|
50
|
+
})
|
|
51
|
+
character.onListeningState((state) => {
|
|
52
|
+
talk.textContent =
|
|
53
|
+
{ listening: 'Listening…', recognizing: 'Recognizing…' }[state] ?? 'Hold to talk'
|
|
28
54
|
})
|
|
55
|
+
character.onTranscript(({ text }) => { status.textContent = text })
|
|
29
56
|
start.disabled = false
|
|
30
57
|
start.onclick = async () => {
|
|
31
58
|
start.disabled = true
|
|
32
59
|
try {
|
|
33
|
-
await
|
|
34
|
-
await
|
|
60
|
+
await character.unlockAudio()
|
|
61
|
+
await character.createConversation()
|
|
35
62
|
} catch (error) {
|
|
36
|
-
|
|
63
|
+
report(error)
|
|
37
64
|
start.disabled = false
|
|
38
65
|
}
|
|
39
66
|
}
|
|
67
|
+
// When removing this view (for example, during navigation):
|
|
40
68
|
window.addEventListener('pagehide', () => { void view.dispose() }, { once: true })
|
package/examples/three-game.js
CHANGED
|
@@ -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: '
|
|
34
|
-
apiKey: '
|
|
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
|
-
//
|
|
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)
|