| 69 | const _that = this; |
| 70 | async function respond(botMessage: string) { |
| 71 | async function completePrompt(input: string) { |
| 72 | // Route users message to wave |
| 73 | const result = await wave.completePrompt<string>(input); |
| 74 | switch (result.status) { |
| 75 | case 'success': |
| 76 | const message = result.message as Message<string>; |
| 77 | if (message.function_call) { |
| 78 | // Call function and add result to history |
| 79 | const entry = _that._functions.get(message.function_call.name!)!; |
| 80 | if (entry) { |
| 81 | const args = message.function_call.arguments ? JSON.parse(message.function_call.arguments) : {}; |
| 82 | const result = await entry.fn(args); |
| 83 | wave.addFunctionResultToHistory(message.function_call.name!, result); |
| 84 | |
| 85 | // Call back in with the function result |
| 86 | await completePrompt(''); |
| 87 | } else { |
| 88 | respond(Colorize.error(`Function '${message.function_call.name}' was not found.`)); |
| 89 | } |
| 90 | } else { |
| 91 | // Call respond to display response and wait for user input |
| 92 | await respond(Colorize.output(message.content!)); |
| 93 | } |
| 94 | break; |
| 95 | default: |
| 96 | if (result.message) { |
| 97 | await respond(Colorize.error(`${result.status}: ${result.message}`)); |
| 98 | } else { |
| 99 | await respond(Colorize.error(`A result status of '${result.status}' was returned.`)); |
| 100 | } |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Show the bots message |
| 106 | console.log(botMessage); |