Repository navigation
Expand file tree
/
Copy pathcheck-grammar.ts
More file actions
39 lines (36 loc) · 1.51 KB
/
Copy pathcheck-grammar.ts
File metadata and controls
39 lines (36 loc) · 1.51 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
import { toolsToGbnfWithThink } from "./src/tools/registry.ts"
import { toolDefs as storytellerToolDefs } from "./src/agents/storyteller/tools/index.ts"
import { toolDefs as envoyToolDefs } from "./src/agents/envoy/tools/index.ts"
// Print the full grammar for each
console.log("=== storyteller grammar ===")
console.log(toolsToGbnfWithThink(storytellerToolDefs))
console.log("\n=== envoy grammar ===")
console.log(toolsToGbnfWithThink(envoyToolDefs))
// Now try to compile with schoolmarm
async function test() {
const schoolmarm = await import("schoolmarm")
const Grammar = schoolmarm.Grammar
const GrammarState = schoolmarm.GrammarState
for (const [name, defs] of [["storyteller", storytellerToolDefs], ["envoy", envoyToolDefs]] as const) {
const gbnf = toolsToGbnfWithThink(defs)
try {
const g = Grammar.new(gbnf)
console.log(`\n${name}: Grammar compiled OK`)
const gs = GrammarState.new(g.clone())
console.log(`${name}: GrammarState created OK`)
// Test allowed tokens
const testTokens = [
"hello", " world", "<think>", "</think>", "<tool_call>", "</tool_call>",
'{"name":"write","arguments":{"path":"test","content":"hello"}}',
"\n\nUser:",
]
const allowed = gs.allowed_tokens(testTokens)
for (let i = 0; i < testTokens.length; i++) {
console.log(` ${allowed[i] ? "ALLOW" : "DENY"}: ${JSON.stringify(testTokens[i])}`)
}
} catch (e) {
console.error(`\n${name}: ERROR: ${e}`)
}
}
}
test().catch(console.error)