|
| 1 | +# Showing Agent Reasoning in Custom UIs |
| 2 | + |
| 3 | +> **IMPORTANT!** This is the sample repository for [https://microsoft.github.io/mcscatblog/posts/show-reasoning-agents-sdk/](This article). Follow the article for a more detailed explanation. |
| 4 | +
|
| 5 | +Render Anthropic reasoning traces from Microsoft 365 Copilot Studio agents inside your own UI. This README distills the companion article posted on MCS CAT blog. |
| 6 | + |
| 7 | +## Why Bubble Up Reasoning? |
| 8 | +- Strengthen trust in automated or assisted decisions. |
| 9 | +- Give operators visibility into multi-step, decision-heavy workflows. |
| 10 | +- Help end users judge the suitability of an answer before acting on it. |
| 11 | + |
| 12 | +## Demo Scenario |
| 13 | +The reference sample (static HTML + JS) simulates an organization triaging monday.com tickets with an Anthropic-enabled Copilot Studio agent. Submitting a new ticket shows incremental reasoning as typing activities, and near-duplicate tickets are merged automatically instead of duplicated. |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | +- Copilot Studio agent configured with an Anthropic model (Settings -> Agent model). |
| 17 | +- Custom UI wired to the Microsoft 365 Agents SDK. |
| 18 | +- Optional backend summarization endpoint to shorten verbose reasoning (recommended for UX). GPT-family models do not yet emit reasoning traces. |
| 19 | + |
| 20 | +## Core Flow |
| 21 | +1. **Initialize the client** |
| 22 | + ```js |
| 23 | + import { CopilotStudioClient } from '@microsoft/agents-copilotstudio-client'; |
| 24 | + import { acquireToken } from './acquireToken.js'; |
| 25 | + import { settings } from './settings.js'; |
| 26 | + |
| 27 | + export const createCopilotClient = async () => { |
| 28 | + const token = await acquireToken(settings); |
| 29 | + return new CopilotStudioClient(settings, token); |
| 30 | + }; |
| 31 | + ``` |
| 32 | +2. **Start a conversation** |
| 33 | + ```js |
| 34 | + const copilotClient = await createCopilotClient(); |
| 35 | + let conversationId; |
| 36 | + |
| 37 | + for await (const act of copilotClient.startConversationAsync(true)) { |
| 38 | + conversationId = act.conversation?.id ?? conversationId; |
| 39 | + if (conversationId) break; |
| 40 | + } |
| 41 | + ``` |
| 42 | +3. **Send a prompt** |
| 43 | + ```js |
| 44 | + const prompt = `Create the following ticket:\n\nTitle: ${shortTitle}\nDescription: ${longDescription}`; |
| 45 | + const activityStream = copilotClient.askQuestionAsync(prompt, conversationId); |
| 46 | + ``` |
| 47 | +4. **Capture reasoning and answers** |
| 48 | + ```js |
| 49 | + for await (const activity of activityStream) { |
| 50 | + if (!activity) continue; |
| 51 | + |
| 52 | + const activityType = activity.type?.toLowerCase(); |
| 53 | + |
| 54 | + if (activityType === 'typing' && activity.channelData?.streamType === 'informative') { |
| 55 | + const streamKey = resolveStreamKey(activity); |
| 56 | + const previousActivity = streamLastActivity.get(streamKey); |
| 57 | + |
| 58 | + if (previousActivity && isContinuationOfPrevious(previousActivity, activity)) { |
| 59 | + streamLastActivity.set(streamKey, activity); |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + await flushActivity(previousActivity, false); |
| 64 | + streamLastActivity.set(streamKey, activity); |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + if (activityType === 'message') { |
| 69 | + agentMessages.push(activity.text); |
| 70 | + continue; |
| 71 | + } |
| 72 | + } |
| 73 | + ``` |
| 74 | +
|
| 75 | +### Detect Informative Typing |
| 76 | +```js |
| 77 | +const isReasoningTyping = (activity) => |
| 78 | + (activity?.type || '').toLowerCase() === 'typing' && |
| 79 | + activity?.channelData?.streamType === 'informative'; |
| 80 | +``` |
| 81 | +
|
| 82 | +## Summarize Long Thoughts |
| 83 | +Reasoning chunks can be lengthy. Capture completed thoughts and optionally POST them to a backend summarizer: |
| 84 | +```js |
| 85 | +async function summarizeSafely(text) { |
| 86 | + try { |
| 87 | + const res = await fetch('/api/summarize', { |
| 88 | + method: 'POST', |
| 89 | + headers: { 'Content-Type': 'application/json' }, |
| 90 | + body: JSON.stringify({ text }) |
| 91 | + }); |
| 92 | + const { summary } = await res.json(); |
| 93 | + return summary.trim(); |
| 94 | + } catch { |
| 95 | + return null; |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | +Keep API keys server-side and apply rate limiting. The sample UI exposes an input control so end users can supply their summarizer key during demos. |
| 100 | +
|
| 101 | +## UI Pattern Tips |
| 102 | +- Show a calm, rotating status label once users submit a ticket (for example, "Reviewing possible options..."). |
| 103 | +- Keep the spinner visible while informative typing events are active. |
| 104 | +- Prepend the newest summarized reasoning to the top, keeping the latest five items. |
| 105 | +- Add a subtle entrance animation and collapse the panel once the final answer arrives, with an optional "Show details" toggle. |
| 106 | +
|
| 107 | +## Summary Checklist |
| 108 | +1. Switch your agent to an Anthropic model. |
| 109 | +2. Iterate the Microsoft 365 Agents SDK activity stream. |
| 110 | +3. Detect reasoning via `type === 'typing'` and `channelData.streamType === 'informative'`. |
| 111 | +4. Group reasoning chunks by `channelData.streamId`. |
| 112 | +5. Flush pending reasoning when you receive the final message. |
| 113 | +6. Optionally summarize server-side to protect secrets. |
| 114 | +7. Render a compact Thinking panel with recent updates. |
| 115 | +
|
| 116 | +## Try It Yourself |
| 117 | +- Clone the reference implementation. |
| 118 | +- Configure Copilot Studio with an Anthropic model. |
| 119 | +- Run the sample locally, observe informative typing streams, and integrate your summarizer. |
| 120 | +
|
| 121 | +Questions or feedback on the pattern are welcome. |
0 commit comments