(prompt: string)
| 82 | } |
| 83 | |
| 84 | async ask(prompt: string) { |
| 85 | let result = await this.chat.sendMessage(prompt); |
| 86 | const functionCalls = result.response.functionCalls(); |
| 87 | |
| 88 | if (functionCalls && functionCalls.length > 0) { |
| 89 | for (const functionCall of functionCalls) { |
| 90 | switch (functionCall.name) { |
| 91 | case "getNumberOfProducts": { |
| 92 | const functionResult = this.getNumberOfProducts(); |
| 93 | result = await this.chat.sendMessage([ |
| 94 | { |
| 95 | functionResponse: { |
| 96 | name: functionCall.name, |
| 97 | response: { numberOfItems: functionResult }, |
| 98 | }, |
| 99 | }, |
| 100 | ]); |
| 101 | break; |
| 102 | } |
| 103 | case "getProducts": { |
| 104 | const functionResult = this.getProducts(); |
| 105 | result = await this.chat.sendMessage([ |
| 106 | { |
| 107 | functionResponse: { |
| 108 | name: functionCall.name, |
| 109 | response: { products: functionResult }, |
| 110 | }, |
| 111 | }, |
| 112 | ]); |
| 113 | break; |
| 114 | } |
| 115 | case "addToCart": { |
| 116 | console.log(functionCall.args); |
| 117 | |
| 118 | const args = functionCall.args as { productsToAdd: Product[]} |
| 119 | |
| 120 | const functionResult = this.addToCart(args.productsToAdd); |
| 121 | |
| 122 | result = await this.chat.sendMessage([ |
| 123 | { |
| 124 | functionResponse: { |
| 125 | name: functionCall.name, |
| 126 | response: { numberOfProductsAdded: functionResult }, |
| 127 | }, |
| 128 | } |
| 129 | ]); |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return result.response.text(); |
| 137 | } |
| 138 | |
| 139 | getProducts() { |
| 140 | return this.products.getProducts(); |
no test coverage detected