|
| 1 | +// Webview-side script. Runs in a restricted context with no Node access; |
| 2 | +// all agent communication goes through the extension host via postMessage. |
| 3 | +(function () { |
| 4 | + const vscode = acquireVsCodeApi(); |
| 5 | + const messagesEl = document.getElementById('messages'); |
| 6 | + const inputEl = document.getElementById('input-box'); |
| 7 | + const sendBtn = document.getElementById('send-btn'); |
| 8 | + const stopBtn = document.getElementById('stop-btn'); |
| 9 | + |
| 10 | + let currentAgentBubble = null; |
| 11 | + const toolCallEls = new Map(); |
| 12 | + |
| 13 | + function appendRow(text, cls) { |
| 14 | + const row = document.createElement('div'); |
| 15 | + row.className = 'row ' + cls; |
| 16 | + const bubble = document.createElement('div'); |
| 17 | + bubble.className = 'bubble ' + cls; |
| 18 | + bubble.textContent = text; |
| 19 | + row.appendChild(bubble); |
| 20 | + messagesEl.appendChild(row); |
| 21 | + messagesEl.scrollTop = messagesEl.scrollHeight; |
| 22 | + return bubble; |
| 23 | + } |
| 24 | + |
| 25 | + function statusIcon(status) { |
| 26 | + if (status === 'completed') return '✓'; |
| 27 | + if (status === 'failed') return '✗'; |
| 28 | + if (status === 'in_progress' || status === 'pending') return '◌'; |
| 29 | + return '•'; |
| 30 | + } |
| 31 | + |
| 32 | + // The initial tool_call event carries a title; the tool_call_update sent |
| 33 | + // on completion never does (the agent only sends status + content there). |
| 34 | + // Remember the title on the element itself so completion doesn't blank it. |
| 35 | + function upsertToolCall(id, title, status) { |
| 36 | + let el = id ? toolCallEls.get(id) : null; |
| 37 | + if (!el) { |
| 38 | + el = document.createElement('div'); |
| 39 | + el.className = 'tool-call'; |
| 40 | + messagesEl.appendChild(el); |
| 41 | + if (id) { |
| 42 | + toolCallEls.set(id, el); |
| 43 | + } |
| 44 | + } |
| 45 | + if (title) { |
| 46 | + el.dataset.title = title; |
| 47 | + } |
| 48 | + el.className = 'tool-call ' + (status || ''); |
| 49 | + el.textContent = `${statusIcon(status)} ${el.dataset.title || '(tool call)'}`; |
| 50 | + messagesEl.scrollTop = messagesEl.scrollHeight; |
| 51 | + } |
| 52 | + |
| 53 | + function setBusy(busy) { |
| 54 | + sendBtn.disabled = busy; |
| 55 | + stopBtn.classList.toggle('hidden', !busy); |
| 56 | + } |
| 57 | + |
| 58 | + function autoResize() { |
| 59 | + inputEl.style.height = 'auto'; |
| 60 | + inputEl.style.height = Math.min(inputEl.scrollHeight, 200) + 'px'; |
| 61 | + } |
| 62 | + inputEl.addEventListener('input', autoResize); |
| 63 | + |
| 64 | + function send() { |
| 65 | + const text = inputEl.value.trim(); |
| 66 | + if (!text) { |
| 67 | + return; |
| 68 | + } |
| 69 | + appendRow(text, 'user'); |
| 70 | + inputEl.value = ''; |
| 71 | + autoResize(); |
| 72 | + currentAgentBubble = null; |
| 73 | + setBusy(true); |
| 74 | + vscode.postMessage({ type: 'prompt', text }); |
| 75 | + } |
| 76 | + |
| 77 | + sendBtn.addEventListener('click', send); |
| 78 | + stopBtn.addEventListener('click', () => vscode.postMessage({ type: 'stop' })); |
| 79 | + inputEl.addEventListener('keydown', (e) => { |
| 80 | + if (e.key === 'Enter' && !e.shiftKey) { |
| 81 | + e.preventDefault(); |
| 82 | + send(); |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + setBusy(false); |
| 87 | + |
| 88 | + window.addEventListener('message', (event) => { |
| 89 | + const msg = event.data; |
| 90 | + switch (msg.type) { |
| 91 | + case 'textChunk': { |
| 92 | + const cls = msg.isThought ? 'thought' : 'agent'; |
| 93 | + if (!currentAgentBubble || currentAgentBubble.dataset.cls !== cls) { |
| 94 | + currentAgentBubble = appendRow('', cls); |
| 95 | + currentAgentBubble.dataset.cls = cls; |
| 96 | + } |
| 97 | + currentAgentBubble.textContent += msg.text; |
| 98 | + messagesEl.scrollTop = messagesEl.scrollHeight; |
| 99 | + break; |
| 100 | + } |
| 101 | + case 'toolCall': |
| 102 | + case 'toolCallUpdate': { |
| 103 | + currentAgentBubble = null; |
| 104 | + upsertToolCall(msg.toolCallId, msg.title, msg.status); |
| 105 | + break; |
| 106 | + } |
| 107 | + case 'status': { |
| 108 | + currentAgentBubble = null; |
| 109 | + appendRow(msg.text, 'system'); |
| 110 | + break; |
| 111 | + } |
| 112 | + case 'turnEnded': { |
| 113 | + currentAgentBubble = null; |
| 114 | + setBusy(false); |
| 115 | + break; |
| 116 | + } |
| 117 | + default: |
| 118 | + break; |
| 119 | + } |
| 120 | + }); |
| 121 | +})(); |
0 commit comments