* Starts the chat session and listens for user input.
()
| 44 | * Starts the chat session and listens for user input. |
| 45 | */ |
| 46 | public async chat(): Promise<void> { |
| 47 | // Create a readline interface object with the standard input and output streams |
| 48 | const rl = readline.createInterface({ |
| 49 | input: process.stdin, |
| 50 | output: process.stdout |
| 51 | }); |
| 52 | |
| 53 | // Create model and wave |
| 54 | const model = this.createModel(); |
| 55 | const wave = new AlphaWave({ |
| 56 | model, |
| 57 | prompt: new Prompt([ |
| 58 | new SystemMessage([ |
| 59 | `You are an expert software developer.`, |
| 60 | `You are chatting with another developer who is asking for help with the project they're working on.`, |
| 61 | ].join('\n')), |
| 62 | new SourceCodeSection(this._index, 0.6), |
| 63 | new ConversationHistory('history', 0.4), |
| 64 | new UserMessage('{{$input}}', 500) |
| 65 | ]) |
| 66 | }); |
| 67 | |
| 68 | // Define main chat loop |
| 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 | } |
no test coverage detected