|
| 1 | +import Component from '@glimmer/component'; |
| 2 | +import Logstream from 'codecrafters-frontend/utils/logstream'; |
| 3 | +import fade from 'ember-animated/transitions/fade'; |
| 4 | +import move from 'ember-animated/motions/move'; |
| 5 | +import type ActionCableConsumerService from 'codecrafters-frontend/services/action-cable-consumer'; |
| 6 | +import type AutofixRequestModel from 'codecrafters-frontend/models/autofix-request'; |
| 7 | +import type Store from '@ember-data/store'; |
| 8 | +import { action } from '@ember/object'; |
| 9 | +import { fadeIn, fadeOut } from 'ember-animated/motions/opacity'; |
| 10 | +import { next } from '@ember/runloop'; |
| 11 | +import { service } from '@ember/service'; |
| 12 | +import { tracked } from '@glimmer/tracking'; |
| 13 | + |
| 14 | +interface Signature { |
| 15 | + Element: HTMLDivElement; |
| 16 | + |
| 17 | + Args: { |
| 18 | + autofixRequest: AutofixRequestModel; |
| 19 | + }; |
| 20 | +} |
| 21 | + |
| 22 | +interface ToolCallStartEvent { |
| 23 | + event: 'tool_call_start'; |
| 24 | + params: { |
| 25 | + tool_call_id: string; |
| 26 | + tool_name: string; |
| 27 | + tool_arguments: Record<string, unknown>; |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +interface ToolCallEndEvent { |
| 32 | + event: 'tool_call_end'; |
| 33 | + params: { tool_call_id: string }; |
| 34 | +} |
| 35 | + |
| 36 | +type Event = ToolCallStartEvent | ToolCallEndEvent; |
| 37 | + |
| 38 | +class ToolCall { |
| 39 | + tool_call_id: string; |
| 40 | + tool_name: string; |
| 41 | + tool_arguments: Record<string, unknown>; |
| 42 | + status: 'in_progress' | 'completed'; |
| 43 | + |
| 44 | + constructor(tool_call_id: string, tool_name: string, tool_arguments: Record<string, unknown>) { |
| 45 | + this.tool_call_id = tool_call_id; |
| 46 | + this.tool_name = tool_name; |
| 47 | + this.tool_arguments = tool_arguments; |
| 48 | + this.status = 'in_progress'; |
| 49 | + } |
| 50 | + |
| 51 | + get text(): string { |
| 52 | + switch (this.tool_name) { |
| 53 | + case 'read': |
| 54 | + if (this.status === 'in_progress') { |
| 55 | + return `Reading ${this.tool_arguments['path']!}`; |
| 56 | + } else { |
| 57 | + return `Read ${this.tool_arguments['path']!}`; |
| 58 | + } |
| 59 | + |
| 60 | + case 'edit': |
| 61 | + if (this.status === 'in_progress') { |
| 62 | + return `Editing ${this.tool_arguments['path']!}`; |
| 63 | + } else { |
| 64 | + return `Edited ${this.tool_arguments['path']!}`; |
| 65 | + } |
| 66 | + |
| 67 | + case 'write': |
| 68 | + if (this.status === 'in_progress') { |
| 69 | + return `Creating ${this.tool_arguments['path']!}`; |
| 70 | + } else { |
| 71 | + return `Created ${this.tool_arguments['path']!}`; |
| 72 | + } |
| 73 | + |
| 74 | + case 'delete': |
| 75 | + if (this.status === 'in_progress') { |
| 76 | + return `Deleting ${this.tool_arguments['path']!}`; |
| 77 | + } else { |
| 78 | + return `Deleted ${this.tool_arguments['path']!}`; |
| 79 | + } |
| 80 | + |
| 81 | + case 'list': |
| 82 | + if (this.status === 'in_progress') { |
| 83 | + return 'Listing files'; |
| 84 | + } else { |
| 85 | + return 'Listed files'; |
| 86 | + } |
| 87 | + |
| 88 | + case 'bash': |
| 89 | + if (this.status === 'in_progress') { |
| 90 | + return 'Running tests'; |
| 91 | + } else { |
| 92 | + return 'Ran tests'; |
| 93 | + } |
| 94 | + |
| 95 | + // This is a fake tool call that we insert for UI display purposes |
| 96 | + case 'analyze': |
| 97 | + if (this.status === 'in_progress') { |
| 98 | + return 'Analyzing codebase'; |
| 99 | + } else { |
| 100 | + return 'Analyzed codebase'; |
| 101 | + } |
| 102 | + |
| 103 | + default: |
| 104 | + return this.tool_name; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + isAnalysisAction(): boolean { |
| 109 | + return this.tool_name === 'analyze' || this.tool_name === 'read' || this.tool_name === 'list'; |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +export default class LogstreamSection extends Component<Signature> { |
| 114 | + transition = fade; |
| 115 | + |
| 116 | + @service declare actionCableConsumer: ActionCableConsumerService; |
| 117 | + @service declare store: Store; |
| 118 | + |
| 119 | + @tracked declare logstream: Logstream; |
| 120 | + @tracked logstreamContent = ''; |
| 121 | + @tracked eventsContainer: HTMLDivElement | null = null; |
| 122 | + |
| 123 | + constructor(owner: unknown, args: Signature['Args']) { |
| 124 | + super(owner, args); |
| 125 | + |
| 126 | + this.logstream = new Logstream(args.autofixRequest.logstreamId, this.actionCableConsumer, this.store, () => { |
| 127 | + this.logstreamContent = this.logstream.content || ''; |
| 128 | + |
| 129 | + next(() => { |
| 130 | + if (this.eventsContainer) { |
| 131 | + this.eventsContainer.scrollTop = this.eventsContainer.scrollHeight; |
| 132 | + } |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + next(() => { |
| 137 | + this.logstream.subscribe(); |
| 138 | + }); |
| 139 | + } |
| 140 | + |
| 141 | + get events(): Event[] { |
| 142 | + return this.logstream.content |
| 143 | + .split('\n') |
| 144 | + .map((line) => { |
| 145 | + try { |
| 146 | + return JSON.parse(line) as Event; |
| 147 | + } catch { |
| 148 | + return null; |
| 149 | + } |
| 150 | + }) |
| 151 | + .filter(Boolean) as Event[]; |
| 152 | + } |
| 153 | + |
| 154 | + get toolCalls(): ToolCall[] { |
| 155 | + const result: ToolCall[] = []; |
| 156 | + |
| 157 | + for (const event of this.events) { |
| 158 | + if (event.event === 'tool_call_start') { |
| 159 | + result.push(new ToolCall(event.params.tool_call_id, event.params.tool_name, event.params.tool_arguments || {})); |
| 160 | + } else if (event.event === 'tool_call_end') { |
| 161 | + result.find((toolCall) => toolCall.tool_call_id === event.params.tool_call_id)!.status = 'completed'; |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + if (result.every((toolCall) => toolCall.isAnalysisAction())) { |
| 166 | + // Show an in-progress analysis action if all tool calls are analysis actions. |
| 167 | + result.push(new ToolCall('fake-tool-call-id', 'analyze', {})); |
| 168 | + } else { |
| 169 | + const analysisToolCall = new ToolCall('fake-tool-call-id', 'analyze', {}); |
| 170 | + analysisToolCall.status = 'completed'; |
| 171 | + |
| 172 | + let lastAnalysisActionIndex = -1; |
| 173 | + |
| 174 | + for (let i = result.length - 1; i >= 0; i--) { |
| 175 | + if (result[i]!.isAnalysisAction()) { |
| 176 | + lastAnalysisActionIndex = i; |
| 177 | + break; |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + result.splice(lastAnalysisActionIndex + 1, 0, analysisToolCall); |
| 182 | + } |
| 183 | + |
| 184 | + return result; |
| 185 | + } |
| 186 | + |
| 187 | + @action |
| 188 | + handleDidInsertEventsContainer(eventsContainer: HTMLDivElement) { |
| 189 | + this.eventsContainer = eventsContainer; |
| 190 | + } |
| 191 | + |
| 192 | + // @ts-expect-error ember-animated not typed |
| 193 | + // eslint-disable-next-line require-yield |
| 194 | + *listTransition({ insertedSprites, keptSprites, removedSprites }) { |
| 195 | + for (const sprite of keptSprites) { |
| 196 | + move(sprite); |
| 197 | + } |
| 198 | + |
| 199 | + for (const sprite of insertedSprites) { |
| 200 | + fadeIn(sprite); |
| 201 | + } |
| 202 | + |
| 203 | + for (const sprite of removedSprites) { |
| 204 | + fadeOut(sprite); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + willDestroy() { |
| 209 | + super.willDestroy(); |
| 210 | + |
| 211 | + if (this.logstream) { |
| 212 | + this.logstream.unsubscribe(); |
| 213 | + } |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +declare module '@glint/environment-ember-loose/registry' { |
| 218 | + export default interface Registry { |
| 219 | + 'CoursePage::TestResultsBar::AutofixRequestCard::LogstreamSection': typeof LogstreamSection; |
| 220 | + } |
| 221 | +} |
0 commit comments