Repository navigation
feat(native-chat): render Muse transcripts #22998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
clichedmoog
wants to merge
1
commit into
stablyai:main
Choose a base branch
from
clichedmoog:feat/native-chat-muse
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,232 @@ | ||
| // Muse Code session.jsonl line → NativeChatMessage decoder. | ||
| // | ||
| // Layout: <root>/YYYY/MM/DD/<uuid>/session.jsonl. Conversation turns are a few | ||
| // record shapes amid heavy bookkeeping (task lifecycle, reasoning, usage, | ||
| // reminders): `runtime.user_intent.accepted` (user), `assistant_message_committed` | ||
| // (assistant text), `assistant_tool_calls_committed` / `tool_result_batch_committed` | ||
| // (tool turns), `user_input_prompt_requested/settled` (question cards), and a | ||
| // cancelled `terminal` (interrupt row). Everything else skips, including the | ||
| // `retained_frame` / `retained_marker` retention envelopes, which carry no | ||
| // conversation payload. | ||
|
|
||
| import { | ||
| NATIVE_CHAT_INTERRUPTED_STATUS_TEXT, | ||
| type NativeChatBlock, | ||
| type NativeChatMessage | ||
| } from '../../shared/native-chat-types' | ||
| import { museTimestampMs } from '../ai-vault/session-scanner-muse-parser' | ||
| import { | ||
| arrayValue, | ||
| asRecord, | ||
| extractString, | ||
| parseJsonObject | ||
| } from '../ai-vault/session-scanner-values' | ||
| import { toolResultOutput } from './transcript-record-blocks' | ||
|
|
||
| /** | ||
| * Muse session rows: `runtime.user_intent.accepted` user turns plus the run | ||
| * events the assistant emits (message, tool calls, tool results, question | ||
| * prompts and their answers, terminal interrupts). Records of any other shape | ||
| * are engine bookkeeping and return null. | ||
| */ | ||
| export function decodeMuseTranscriptLine( | ||
| line: string, | ||
| fallbackId: string | ||
| ): NativeChatMessage | null { | ||
| const record = parseJsonObject(line) | ||
| if (!record) { | ||
| return null | ||
| } | ||
| const id = extractString(record.id) ?? fallbackId | ||
| const timestamp = museTimestampMs(record.recorded_at) | ||
| const payload = asRecord(record.payload) | ||
| if (!payload) { | ||
| return null | ||
| } | ||
|
|
||
| if (extractString(record.payload_type) === 'runtime.user_intent.accepted') { | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| const text = museUserText(payload) | ||
| return text | ||
| ? { id, role: 'user', blocks: [{ type: 'text', text }], timestamp, source: 'transcript' } | ||
| : null | ||
| } | ||
|
|
||
| const event = asRecord(payload.event) | ||
| if (!event) { | ||
| return null | ||
| } | ||
| const kind = extractString(event.kind) | ||
| if (!kind) { | ||
| return null | ||
| } | ||
| switch (kind) { | ||
| case 'assistant_message_committed': { | ||
| const text = extractString(event.text) | ||
| return text | ||
| ? { | ||
| id, | ||
| role: 'assistant', | ||
| blocks: [{ type: 'text', text }], | ||
| timestamp, | ||
| source: 'transcript' | ||
| } | ||
| : null | ||
| } | ||
| case 'assistant_tool_calls_committed': { | ||
| const blocks = museToolCallBlocks(event.tool_calls) | ||
| return blocks.length === 0 | ||
| ? null | ||
| : { id, role: 'assistant', blocks, timestamp, source: 'transcript' } | ||
| } | ||
| case 'tool_result_batch_committed': { | ||
| const blocks = museToolResultBlocks(event.results) | ||
| return blocks.length === 0 | ||
| ? null | ||
| : { id, role: 'tool', blocks, timestamp, source: 'transcript' } | ||
| } | ||
| case 'user_input_prompt_requested': { | ||
| const text = museQuestionText(event.questions) | ||
| return text | ||
| ? { | ||
| id, | ||
| role: 'assistant', | ||
| blocks: [{ type: 'text', text }], | ||
| timestamp, | ||
| source: 'transcript' | ||
| } | ||
| : null | ||
| } | ||
| case 'user_input_prompt_settled': { | ||
| const text = museAnswerText(event.answers) | ||
| return text | ||
| ? { id, role: 'user', blocks: [{ type: 'text', text }], timestamp, source: 'transcript' } | ||
| : null | ||
| } | ||
| case 'terminal': | ||
| // Why: a cancelled run stamps `terminal: 'cancelled'` on its own record | ||
| // (a separate task `failed`/`cancelled` row carries only the reason, with | ||
| // no tool link a stateless decoder could join). Surface the same | ||
| // interrupted row the other decoders emit for their aborts. | ||
| return event.terminal === 'cancelled' | ||
| ? { | ||
| id, | ||
| role: 'system', | ||
| blocks: [{ type: 'text', text: NATIVE_CHAT_INTERRUPTED_STATUS_TEXT }], | ||
| timestamp, | ||
| source: 'transcript' | ||
| } | ||
| : null | ||
| default: | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| /** All text blocks of a user intent: `refill_blocks`, then `model_messages` content. */ | ||
| function museUserText(payload: Record<string, unknown>): string | null { | ||
| const texts = textBlocks(payload.refill_blocks) | ||
| if (texts.length === 0) { | ||
| for (const message of arrayValue(payload.model_messages)) { | ||
| texts.push(...textBlocks(asRecord(message)?.content)) | ||
| } | ||
| } | ||
| return texts.length === 0 ? null : texts.join('\n') | ||
| } | ||
|
|
||
| function textBlocks(value: unknown): string[] { | ||
| const texts: string[] = [] | ||
| for (const block of arrayValue(value)) { | ||
| const text = extractString(asRecord(block)?.text) | ||
| if (text) { | ||
| texts.push(text) | ||
| } | ||
| } | ||
| return texts | ||
| } | ||
|
|
||
| function museToolCallBlocks(value: unknown): NativeChatBlock[] { | ||
| const blocks: NativeChatBlock[] = [] | ||
| for (const item of arrayValue(value)) { | ||
| const call = asRecord(item) | ||
| if (!call) { | ||
| continue | ||
| } | ||
| const callId = extractString(call.call_id) | ||
| blocks.push({ | ||
| type: 'tool-call', | ||
| name: extractString(call.name) ?? 'tool', | ||
| input: parseToolArgs(call.args), | ||
| ...(callId ? { callId } : {}) | ||
| }) | ||
| } | ||
| return blocks | ||
| } | ||
|
|
||
| /** Muse writes tool args as a JSON string; keep the raw string when it won't parse. */ | ||
| function parseToolArgs(value: unknown): unknown { | ||
| if (typeof value !== 'string') { | ||
| return value ?? '' | ||
| } | ||
| try { | ||
| const parsed: unknown = JSON.parse(value) | ||
| return parsed | ||
| } catch { | ||
| return value | ||
| } | ||
| } | ||
|
|
||
| function museToolResultBlocks(value: unknown): NativeChatBlock[] { | ||
| const blocks: NativeChatBlock[] = [] | ||
| for (const item of arrayValue(value)) { | ||
| const result = asRecord(item) | ||
| if (!result) { | ||
| continue | ||
| } | ||
| // Why: read the output raw rather than via `extractString` — it trims, and | ||
| // leading/trailing whitespace is meaningful in captured command output. | ||
| // Muse results carry no error flag; the detached task `failed` row is skipped. | ||
| blocks.push({ type: 'tool-result', output: toolResultOutput(result.text) }) | ||
| } | ||
| return blocks | ||
| } | ||
|
|
||
| /** One assistant turn per question card: header, question, and option labels. */ | ||
| function museQuestionText(value: unknown): string | null { | ||
| const cards: string[] = [] | ||
| for (const item of arrayValue(value)) { | ||
| const question = asRecord(item) | ||
| if (!question) { | ||
| continue | ||
| } | ||
| const header = extractString(question.header) | ||
| const prompt = extractString(question.question) | ||
| const options: string[] = [] | ||
| for (const option of arrayValue(question.options)) { | ||
| const label = extractString(asRecord(option)?.label) | ||
| if (label) { | ||
| options.push(`- ${label}`) | ||
| } | ||
| } | ||
| const head = [header, prompt].filter((part) => part).join(': ') | ||
| const card = [head, ...options].filter((part) => part).join('\n') | ||
| if (card) { | ||
| cards.push(card) | ||
| } | ||
| } | ||
| return cards.length === 0 ? null : cards.join('\n\n') | ||
| } | ||
|
|
||
| /** The user's answers render as their reply turn. */ | ||
| function museAnswerText(value: unknown): string | null { | ||
| const answers: string[] = [] | ||
| for (const item of arrayValue(value)) { | ||
| const answer = asRecord(item) | ||
| const label = | ||
| extractString(answer?.selected_label) ?? | ||
| extractString(answer?.label) ?? | ||
| extractString(answer?.text) | ||
| if (label) { | ||
| answers.push(label) | ||
| } | ||
| } | ||
| return answers.length === 0 ? null : answers.join('\n') | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.