| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1 | import { css, html, LitElement } from "lit"; |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 2 | import { customElement, property, state } from "lit/decorators.js"; |
| Sean McCullough | 2deac84 | 2025-04-21 18:17:57 -0700 | [diff] [blame] | 3 | import { repeat } from "lit/directives/repeat.js"; |
| Sean McCullough | d9f1337 | 2025-04-21 15:08:49 -0700 | [diff] [blame] | 4 | import { ToolCall } from "../types"; |
| Pokey Rule | 7ac5ed0 | 2025-05-07 15:26:10 +0100 | [diff] [blame] | 5 | import "./sketch-tool-card"; |
| Philip Zeyliger | 80b488d | 2025-05-10 18:21:54 -0700 | [diff] [blame] | 6 | import "./sketch-tool-card-take-screenshot"; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 7 | |
| 8 | @customElement("sketch-tool-calls") |
| 9 | export class SketchToolCalls extends LitElement { |
| 10 | @property() |
| 11 | toolCalls: ToolCall[] = []; |
| 12 | |
| Sean McCullough | 2deac84 | 2025-04-21 18:17:57 -0700 | [diff] [blame] | 13 | @property() |
| 14 | open: boolean = false; |
| 15 | |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 16 | @state() |
| 17 | expanded: boolean = false; |
| 18 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 19 | static styles = css` |
| 20 | /* Tool calls container styles */ |
| 21 | .tool-calls-container { |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 22 | margin-top: 8px; |
| 23 | padding-top: 4px; |
| Philip Zeyliger | e31d2a9 | 2025-05-11 15:22:35 -0700 | [diff] [blame] | 24 | max-width: 100%; |
| 25 | width: 100%; |
| 26 | box-sizing: border-box; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| Sean McCullough | ec3ad1a | 2025-04-18 13:55:16 -0700 | [diff] [blame] | 29 | /* Card container */ |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 30 | .tool-call-card { |
| 31 | display: flex; |
| 32 | flex-direction: column; |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 33 | background-color: rgba(255, 255, 255, 0.6); |
| 34 | border-radius: 6px; |
| 35 | margin-bottom: 6px; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 36 | overflow: hidden; |
| 37 | cursor: pointer; |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 38 | border-left: 2px solid rgba(0, 0, 0, 0.1); |
| 39 | box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05); |
| Philip Zeyliger | e31d2a9 | 2025-05-11 15:22:35 -0700 | [diff] [blame] | 40 | max-width: 100%; |
| 41 | overflow-wrap: break-word; |
| 42 | word-break: break-word; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| Sean McCullough | ec3ad1a | 2025-04-18 13:55:16 -0700 | [diff] [blame] | 45 | /* Status indicators for tool calls */ |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 46 | .tool-call-status { |
| 47 | margin-right: 4px; |
| 48 | text-align: center; |
| 49 | } |
| 50 | |
| 51 | .tool-call-status.spinner { |
| 52 | animation: spin 1s infinite linear; |
| 53 | display: inline-block; |
| 54 | width: 1em; |
| 55 | } |
| 56 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 57 | @keyframes spin { |
| 58 | 0% { |
| 59 | transform: rotate(0deg); |
| 60 | } |
| 61 | 100% { |
| 62 | transform: rotate(360deg); |
| 63 | } |
| 64 | } |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 65 | |
| 66 | .tool-call-cards-container { |
| 67 | display: block; |
| 68 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 69 | `; |
| 70 | |
| 71 | constructor() { |
| 72 | super(); |
| 73 | } |
| 74 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 75 | connectedCallback() { |
| 76 | super.connectedCallback(); |
| 77 | } |
| 78 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 79 | disconnectedCallback() { |
| 80 | super.disconnectedCallback(); |
| 81 | } |
| 82 | |
| Sean McCullough | ec3ad1a | 2025-04-18 13:55:16 -0700 | [diff] [blame] | 83 | cardForToolCall(toolCall: ToolCall, open: boolean) { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 84 | switch (toolCall.name) { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 85 | case "bash": |
| Sean McCullough | ec3ad1a | 2025-04-18 13:55:16 -0700 | [diff] [blame] | 86 | return html`<sketch-tool-card-bash |
| 87 | .open=${open} |
| 88 | .toolCall=${toolCall} |
| 89 | ></sketch-tool-card-bash>`; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 90 | case "codereview": |
| Sean McCullough | ec3ad1a | 2025-04-18 13:55:16 -0700 | [diff] [blame] | 91 | return html`<sketch-tool-card-codereview |
| 92 | .open=${open} |
| 93 | .toolCall=${toolCall} |
| 94 | ></sketch-tool-card-codereview>`; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 95 | case "done": |
| Sean McCullough | ec3ad1a | 2025-04-18 13:55:16 -0700 | [diff] [blame] | 96 | return html`<sketch-tool-card-done |
| 97 | .open=${open} |
| 98 | .toolCall=${toolCall} |
| 99 | ></sketch-tool-card-done>`; |
| Sean McCullough | 485afc6 | 2025-04-28 14:28:39 -0700 | [diff] [blame] | 100 | case "multiplechoice": |
| 101 | return html`<sketch-tool-card-multiple-choice |
| 102 | .open=${open} |
| 103 | .toolCall=${toolCall} |
| 104 | ></sketch-tool-card-multiple-choice>`; |
| Sean McCullough | ec3ad1a | 2025-04-18 13:55:16 -0700 | [diff] [blame] | 105 | case "patch": |
| 106 | return html`<sketch-tool-card-patch |
| 107 | .open=${open} |
| 108 | .toolCall=${toolCall} |
| 109 | ></sketch-tool-card-patch>`; |
| 110 | case "think": |
| 111 | return html`<sketch-tool-card-think |
| 112 | .open=${open} |
| 113 | .toolCall=${toolCall} |
| 114 | ></sketch-tool-card-think>`; |
| 115 | case "title": |
| 116 | return html`<sketch-tool-card-title |
| 117 | .open=${open} |
| 118 | .toolCall=${toolCall} |
| 119 | ></sketch-tool-card-title>`; |
| Josh Bleecher Snyder | a2a3150 | 2025-05-07 12:37:18 +0000 | [diff] [blame] | 120 | case "precommit": |
| 121 | return html`<sketch-tool-card-precommit |
| 122 | .open=${open} |
| 123 | .toolCall=${toolCall} |
| 124 | ></sketch-tool-card-precommit>`; |
| Philip Zeyliger | 80b488d | 2025-05-10 18:21:54 -0700 | [diff] [blame] | 125 | case "browser_take_screenshot": |
| 126 | return html`<sketch-tool-card-take-screenshot |
| Philip Zeyliger | 33d282f | 2025-05-03 04:01:54 +0000 | [diff] [blame] | 127 | .open=${open} |
| 128 | .toolCall=${toolCall} |
| Philip Zeyliger | 80b488d | 2025-05-10 18:21:54 -0700 | [diff] [blame] | 129 | ></sketch-tool-card-take-screenshot>`; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 130 | } |
| Sean McCullough | ec3ad1a | 2025-04-18 13:55:16 -0700 | [diff] [blame] | 131 | return html`<sketch-tool-card-generic |
| 132 | .open=${open} |
| 133 | .toolCall=${toolCall} |
| 134 | ></sketch-tool-card-generic>`; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 135 | } |
| Sean McCullough | ec3ad1a | 2025-04-18 13:55:16 -0700 | [diff] [blame] | 136 | |
| Sean McCullough | 2deac84 | 2025-04-21 18:17:57 -0700 | [diff] [blame] | 137 | // toolUseKey return value should change, if the toolCall gets a response. |
| 138 | toolUseKey(toolCall: ToolCall): string { |
| Sean McCullough | 2deac84 | 2025-04-21 18:17:57 -0700 | [diff] [blame] | 139 | if (!toolCall.result_message) { |
| 140 | return toolCall.tool_call_id; |
| 141 | } |
| 142 | return `${toolCall.tool_call_id}-${toolCall.result_message.idx}`; |
| 143 | } |
| 144 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 145 | render() { |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 146 | if (!this.toolCalls || this.toolCalls.length === 0) { |
| 147 | return html``; |
| 148 | } |
| 149 | |
| Sean McCullough | ec3ad1a | 2025-04-18 13:55:16 -0700 | [diff] [blame] | 150 | return html`<div class="tool-calls-container"> |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 151 | <div class="tool-call-cards-container"> |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 152 | ${repeat(this.toolCalls, this.toolUseKey, (toolCall, idx) => { |
| Philip Zeyliger | 33d282f | 2025-05-03 04:01:54 +0000 | [diff] [blame] | 153 | let shouldOpen = false; |
| 154 | // Always expand screenshot tool calls, expand last tool call if this.open is true |
| 155 | if ( |
| Philip Zeyliger | 80b488d | 2025-05-10 18:21:54 -0700 | [diff] [blame] | 156 | toolCall.name === "browser_take_screenshot" || |
| Philip Zeyliger | 33d282f | 2025-05-03 04:01:54 +0000 | [diff] [blame] | 157 | (idx == this.toolCalls?.length - 1 && this.open) |
| 158 | ) { |
| 159 | shouldOpen = true; |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 160 | } |
| 161 | return html`<div |
| 162 | id="${toolCall.tool_call_id}" |
| 163 | class="tool-call-card ${toolCall.name}" |
| 164 | > |
| Philip Zeyliger | 33d282f | 2025-05-03 04:01:54 +0000 | [diff] [blame] | 165 | ${this.cardForToolCall(toolCall, shouldOpen)} |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 166 | </div>`; |
| 167 | })} |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 168 | </div> |
| 169 | </div>`; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | declare global { |
| 174 | interface HTMLElementTagNameMap { |
| 175 | "sketch-tool-calls": SketchToolCalls; |
| 176 | } |
| 177 | } |