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