( executeCodeResponse: ExecuteCode2Item[], )
| 106 | } |
| 107 | |
| 108 | export function convertToGraphData( |
| 109 | executeCodeResponse: ExecuteCode2Item[], |
| 110 | ): (GraphMessage | FunctionMessage | ErrorMessage | DebugMessage)[] { |
| 111 | if (executeCodeResponse.length === 0) { |
| 112 | throw new Error("No logs, errors or API calls from code execution"); |
| 113 | } |
| 114 | let functionMessage: FunctionMessage = { |
| 115 | role: "function", |
| 116 | name: "logs", |
| 117 | content: |
| 118 | `Logs and API calls from code execution:\n` + |
| 119 | executeCodeResponse |
| 120 | .filter((m) => ["call", "log"].includes(m.type)) |
| 121 | .map((m) => |
| 122 | m.type === "log" |
| 123 | ? m.args.message |
| 124 | : // @ts-ignore |
| 125 | `${m.args.name}(${Object.entries(m.args.params) |
| 126 | .map(([key, value]) => `${key}=${value}`) |
| 127 | .join(", ")})`, |
| 128 | ) |
| 129 | .join("\n"), |
| 130 | }; |
| 131 | |
| 132 | const errorMessages: ErrorMessage[] = executeCodeResponse |
| 133 | .filter((m) => m.type === "error") |
| 134 | .map((m) => ({ |
| 135 | role: "error", |
| 136 | content: m.args.message, |
| 137 | })); |
| 138 | |
| 139 | // Sometimes the AI will write a for loop and put the plot() call in the loop, leading to multiple plots which were |
| 140 | // meant to be 1, each with 1 data point. We combine these out here |
| 141 | let plotItems = executeCodeResponse |
| 142 | .filter((g) => g.type === "plot") |
| 143 | // Convert other graph types to bar |
| 144 | .map((g) => { |
| 145 | if (!["line", "bar", "table"].includes(g.args.type)) |
| 146 | return { |
| 147 | ...g, |
| 148 | type: "bar", |
| 149 | }; |
| 150 | return g; |
| 151 | }) as { |
| 152 | type: "plot"; |
| 153 | args: BertieGraphData; |
| 154 | }[]; |
| 155 | |
| 156 | const originalDataLengths = plotItems.map((g) => g.args.data.length); |
| 157 | plotItems = plotItems |
| 158 | .map((g1, idx, items) => { |
| 159 | const matchedPlotIdx = plotItems.findIndex((g2, i) => { |
| 160 | if (i >= idx) return false; |
| 161 | return ( |
| 162 | g1.args.labels?.x === g2.args.labels?.x && |
| 163 | g1.args.labels?.y === g2.args.labels?.y && |
| 164 | g1.args.data.length === 1 && |
| 165 | originalDataLengths[i] === 1 |
no outgoing calls
no test coverage detected