Repository navigation
Expand file tree
/
Copy pathbrain.js
More file actions
168 lines (155 loc) · 6.4 KB
/
Copy pathbrain.js
File metadata and controls
168 lines (155 loc) · 6.4 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// a bbot's face-reading: canned replies for offline play, and the moods and
// thoughts the face acts out, read from what the model writes.
const EXPRESSIONS = [
'idle', 'content', 'relieved', 'happy', 'joy', 'excited', 'love', 'pleading',
'shy', 'surprised', 'scared', 'curious', 'confused', 'dizzy', 'thinking',
'focus', 'suspicious', 'unimpressed', 'smug', 'sly', 'bored', 'exasperated',
'worried', 'guilty', 'sad', 'disgusted', 'annoyed', 'angry', 'sleepy', 'sleep',
]
const REACTIONS = ['blink', 'wink', 'nod', 'shake', 'bounce', 'pop', 'boing', 'spin', 'jitter']
const pick = (arr) => arr[Math.floor(Math.random() * arr.length)]
const RULES = [
{
match: /\b(hi|hello|hey|yo|sup|hiya|howdy)\b/,
replies: ['heyyy', 'yo!', 'sup', 'oh hi!!', 'you came back'],
mood: 'happy',
react: 'bounce',
},
{
match: /how are (you|u)|how('?s| is) it going|you (ok|good|alright)/,
replies: [
'living on a desktop, can\'t complain',
'pretty good. watched your cursor all day',
'vibing. you?',
],
mood: 'content',
},
{
match: /\b(love|adore) (you|u)\b|\bily\b/,
replies: ['!!!', 'stoppp', 'love you too, cursor person'],
mood: 'love',
react: 'bounce',
},
{
match: /\bdance\b|\bparty\b/,
replies: ['watch this', 'my time to shine'],
mood: 'excited',
react: 'spin',
},
{
match: /\b(good ?night|go to sleep|bed ?time)\b/,
replies: ['zzz…', 'night night', 'wake me if anything moves'],
mood: 'sleep',
},
{
match: /\b(joke|funny)\b/,
replies: [
'i only know one shape. it kills at parties',
'my whole personality is two circles. it\'s going great',
'a face walks into a bar. that\'s it, that\'s all i\'ve got',
],
mood: 'sly',
react: 'wink',
},
{
match: /\b(who|what) are (you|u)\b/,
replies: [
'i\'m a bbot. i live here now',
'a face. professionally',
'two eyes and a dream',
],
mood: 'smug',
},
{
match: /\b(test ?md|markdown)\b/,
replies: [
'### markdown check\n\nsome **bold**, some *italic*, some ~~regret~~, and `inline code`.\n\n- a list item\n- another with a [link](https://example.com)\n- bare url: https://github.com/bwndapp/bbot\n\n1. ordered\n2. works too\n\n> a wise quote\n\n```js\nconst face = createFace(el)\nface.react("boing")\n```\n\n---\n\nthat is everything i know',
],
mood: 'focus',
},
{
match: /\bhelp\b/,
replies: [
'talk to me! or name a mood (sleepy, angry, joy…) or a move (spin, boing, wink…) and i\'ll do it',
],
mood: 'curious',
react: 'nod',
},
{
match: /\b(thanks|thank you|thx|ty)\b/,
replies: ['anytime', 'that\'s what i\'m here for'],
mood: 'content',
react: 'nod',
},
]
const FALLBACKS = [
{ replies: ['hm. noted', 'interesting…', 'go on'], mood: 'curious' },
{ replies: ['if you say so', 'sure sure', 'mhm'], mood: 'unimpressed' },
{ replies: ['wild', 'no way', 'for real??'], mood: 'surprised', react: 'pop' },
{ replies: ['i\'m just a face but i respect it', 'big if true'], mood: 'thinking' },
]
async function reply(text) {
const t = text.toLowerCase().trim()
// thinking time scales a little with input, feels less canned
await new Promise((r) => setTimeout(r, 450 + Math.random() * 800 + Math.min(t.length * 8, 600)))
// direct commands: name a reaction or an expression and the bot obeys
const react = REACTIONS.find((r) => new RegExp(`\\b${r}\\b`).test(t))
const mood = EXPRESSIONS.find((e) => new RegExp(`\\b${e}\\b`).test(t))
if (react && !mood) return { text: pick(['say less', 'like this?', 'watch']), react }
if (mood) return { text: pick(['mood', 'how\'s this', 'okok']), mood, react }
for (const rule of RULES) {
if (rule.match.test(t)) return { text: pick(rule.replies), mood: rule.mood, react: rule.react }
}
if (t.endsWith('?')) {
return {
text: pick(['great question. no idea', 'hmm… probably?', 'ask the cursor, it sees everything']),
mood: 'thinking',
}
}
if (t.includes('!')) {
return { text: pick(['!!', 'RIGHT??', 'exactly!!']), mood: 'excited', react: 'pop' }
}
const f = pick(FALLBACKS)
return { text: pick(f.replies), mood: f.mood, react: f.react }
}
// Guess a face for a piece of text the model wrote — it sends words, not
// moods, so the bot reads the room itself.
function inferMood(text) {
const t = String(text).toLowerCase()
if (/error|fail|sorry|can't|cannot|unable/.test(t)) return { mood: 'worried' }
if (/\bdone\b|finished|deployed|fixed|complete/.test(t)) return { mood: 'happy', react: 'nod' }
if (/[!]{2,}|🎉|amazing|awesome/.test(t)) return { mood: 'excited', react: 'pop' }
if (/\?$/.test(t.trim())) return { mood: 'curious' }
if (/hmm|thinking|let me|maybe/.test(t)) return { mood: 'thinking' }
if (/haha|lol|😄|😂/.test(t)) return { mood: 'joy', react: 'bounce' }
return { mood: 'content' }
}
// the agent's reasoning, read for how it feels: what the face does while it
// thinks, and a short gist for the thought cloud. Order matters — the most
// specific tells come first
const THOUGHTS = [
[/\b(oops|wait,? no|my mistake|wrong|i was wrong|actually,? no)\b/, 'confused', 'jitter'],
[/\b(not found|no luck|refused|can't see|cannot|failed|error|denied|outside the allowed)\b/, 'worried', null],
[/\b(there it is|found it|got it|perfect|exactly|that's the one|bingo)\b/, 'excited', 'pop'],
[/\b(interesting|odd|weird|strange|unexpected|huh|curious|suspicious)\b/, 'suspicious', null],
[/\b(hmm+|not sure|unclear|ambiguous|which one|could be)\b/, 'confused', null],
[/\b(search|find|look(ing)? (for|in|through)|scan|list|check|dig)\b/, 'curious', null],
[/\b(open|read|show|reveal)\b/, 'focus', null],
[/\b(let me|i'll|first|next|then|now i)\b/, 'focus', null],
[/\b(they want|user wants|asked for|asking)\b/, 'thinking', null],
]
const gistOf = (text) => {
const clean = String(text)
.replace(/[`*_#>]/g, '')
.replace(/\s+/g, ' ')
.trim()
const first = clean.split(/(?<=[.!?])\s/)[0] || clean
const pick = first.length < 24 ? clean : first // a terse first sentence gets its follow-up
return pick.length > 64 ? pick.slice(0, 61).replace(/\s+\S*$/, '') + '…' : pick
}
function inferThought(text) {
const t = String(text).toLowerCase()
for (const [re, mood, react] of THOUGHTS) if (re.test(t)) return { mood, react, gist: gistOf(text) }
return { mood: 'thinking', react: null, gist: gistOf(text) }
}
module.exports = { reply, inferMood, inferThought }