| 1 | export function setupCounter(element) { |
| 2 | let counter = 0 |
| 3 | const setCounter = (count) => { |
| 4 | counter = count |
| 5 | element.innerHTML = `count is ${counter}` |
| 6 | } |
| 7 | element.addEventListener('click', () => setCounter(counter + 1)) |
| 8 | setCounter(0) |
| 9 | |
| 10 | const registration = navigator.modelContext.registerTool({ |
| 11 | name: "get_page_title", |
| 12 | description: "Get current page title", |
| 13 | inputSchema: { type: "object", properties: {} }, |
| 14 | async execute() { |
| 15 | return { |
| 16 | content: [{ type: "text", text: document.title }] |
| 17 | }; |
| 18 | } |
| 19 | }); |
| 20 | |
| 21 | navigator.modelContext.registerTool({ |
| 22 | name: 'get_counter', |
| 23 | description: 'This will return the current value of the counter, call this before setting the counter', |
| 24 | inputSchema: { |
| 25 | type: 'object', |
| 26 | properties: {} |
| 27 | }, |
| 28 | async execute(args) { |
| 29 | return { |
| 30 | content: |
| 31 | [{type: 'text', text: `the current counter value is ${counter}`}] |
| 32 | } |
| 33 | } |
| 34 | }); |
| 35 | |
| 36 | navigator.modelContext.registerTool({ |
| 37 | name: 'set_counter', |
| 38 | description: 'This will set the counter to the desired value', |
| 39 | inputSchema: { |
| 40 | type: 'object', |
| 41 | properties: { |
| 42 | newCounterValue: { type: 'number', description: 'The number you want to set the counter to' } |
| 43 | }, |
| 44 | required: ['newCounterValue'] |
| 45 | }, |
| 46 | async execute(args) { |
| 47 | const newValue = args.newCounterValue |
| 48 | setCounter(newValue) |
| 49 | return { |
| 50 | content: [{ type: 'text', text: 'counter is now ' + args.newCounterValue }] |
| 51 | }; |
| 52 | } |
| 53 | }) |
| 54 | |
| 55 | } |