-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexample.html
More file actions
92 lines (84 loc) · 2.73 KB
/
example.html
File metadata and controls
92 lines (84 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Example</title>
</head>
<body>
<script src="./dist/browser/index.umd.js"></script>
<script>
const { NeuroClient } = NeuroGameSdk
const NEURO_SERVER_URL = 'ws://localhost:8000'
const GAME_NAME = 'Guess the Number'
const neuroClient = new NeuroClient(NEURO_SERVER_URL, GAME_NAME, () => {
neuroClient.registerActions([
{
name: 'guess_number',
description: 'Guess the number between 1 and 10.',
schema: {
type: 'object',
properties: {
number: { type: 'integer', minimum: 1, maximum: 10 },
},
required: ['number'],
},
},
])
let targetNumber = Math.floor(Math.random() * 10) + 1
neuroClient.onAction(actionData => {
if (actionData.name === 'guess_number') {
const guessedNumber = actionData.params.number
if (
typeof guessedNumber !== 'number' ||
guessedNumber < 1 ||
guessedNumber > 10
) {
neuroClient.sendActionResult(
actionData.id,
false,
'Invalid number. Please guess a number between 1 and 10.'
)
return
}
if (guessedNumber === targetNumber) {
neuroClient.sendActionResult(
actionData.id,
true,
`Correct! The number was ${targetNumber}. Generating a new number.`
)
targetNumber = Math.floor(Math.random() * 10) + 1
promptNeuroAction()
} else {
neuroClient.sendActionResult(
actionData.id,
true,
`Incorrect. The number is ${
guessedNumber < targetNumber ? 'higher' : 'lower'
}. Try again.`
)
promptNeuroAction()
}
} else {
neuroClient.sendActionResult(
actionData.id,
false,
'Unknown action.'
)
}
})
neuroClient.sendContext(
'Game started. I have picked a number between 1 and 10.',
false
)
function promptNeuroAction() {
const availableActions = ['guess_number']
const query = 'Please guess a number between 1 and 10.'
const state = 'Waiting for your guess.'
neuroClient.forceActions(query, availableActions, state)
}
promptNeuroAction()
})
</script>
</body>
</html>