| 641 | } |
| 642 | |
| 643 | private async dispatchEvent( |
| 644 | workspaceId: string, |
| 645 | event: MockAssistantEvent, |
| 646 | messageId: string, |
| 647 | historySequence: number |
| 648 | ): Promise<void> { |
| 649 | const active = this.activeStreams.get(workspaceId); |
| 650 | if (!active || active.cancelled || active.messageId !== messageId) { |
| 651 | return; |
| 652 | } |
| 653 | |
| 654 | switch (event.kind) { |
| 655 | case "stream-start": { |
| 656 | const payload: StreamStartEvent = { |
| 657 | type: "stream-start", |
| 658 | workspaceId, |
| 659 | messageId, |
| 660 | model: event.model, |
| 661 | historySequence, |
| 662 | startTime: Date.now(), |
| 663 | ...(event.mode && { mode: event.mode }), |
| 664 | ...(event.thinkingLevel && { thinkingLevel: event.thinkingLevel }), |
| 665 | }; |
| 666 | active.model = event.model; |
| 667 | active.startTime = payload.startTime; |
| 668 | this.deps.aiService.emit("stream-start", payload); |
| 669 | break; |
| 670 | } |
| 671 | case "reasoning-delta": { |
| 672 | // Mock streams use the same tokenization logic as real streams for consistency |
| 673 | const tokens = await tokenizeWithMockModel(event.text, "reasoning-delta text"); |
| 674 | if (active.cancelled) return; |
| 675 | const payload: ReasoningDeltaEvent = { |
| 676 | type: "reasoning-delta", |
| 677 | workspaceId, |
| 678 | messageId, |
| 679 | delta: event.text, |
| 680 | tokens, |
| 681 | timestamp: Date.now(), |
| 682 | }; |
| 683 | this.appendReasoningPart(active, event.text, payload.timestamp); |
| 684 | this.schedulePartialWrite(workspaceId, active); |
| 685 | this.deps.aiService.emit("reasoning-delta", payload); |
| 686 | break; |
| 687 | } |
| 688 | case "tool-start": { |
| 689 | // Mock streams use the same tokenization logic as real streams for consistency |
| 690 | const inputText = JSON.stringify(event.args); |
| 691 | const tokens = await tokenizeWithMockModel(inputText, "tool-call args"); |
| 692 | if (active.cancelled) return; |
| 693 | const payload: ToolCallStartEvent = { |
| 694 | type: "tool-call-start", |
| 695 | workspaceId, |
| 696 | messageId, |
| 697 | toolCallId: event.toolCallId, |
| 698 | toolName: event.toolName, |
| 699 | args: event.args, |
| 700 | tokens, |