From 83de4c041aa85aeefe3314e9d2ff66cc813dd34c Mon Sep 17 00:00:00 2001 From: Mohammad Kermani Date: Tue, 22 Apr 2025 09:23:58 +0000 Subject: [PATCH] feat: stream pattern events to console behind a feature flag --- components/message.tsx | 15 +++++++-------- lib/ai/pattern-model.ts | 16 +++++++++++----- lib/flags.ts | 11 +++++++++++ 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/components/message.tsx b/components/message.tsx index 052183d..0820111 100644 --- a/components/message.tsx +++ b/components/message.tsx @@ -15,7 +15,6 @@ import { DocumentToolCall, DocumentToolResult } from './document'; import { DocumentPreview } from './document-preview'; import { Markdown } from './markdown'; import { MessageEditor } from './message-editor'; -import { MessageReasoning } from './message-reasoning'; import { PreviewAttachment } from './preview-attachment'; import { Weather } from './weather'; @@ -42,6 +41,13 @@ const PurePreviewMessage = ({ }) => { const [mode, setMode] = useState<'view' | 'edit'>('view'); + /** + * For now, only streaming reasoning to console is supported + */ + if (message.reasoning) { + console.log('Pattern event: ', message.reasoning); + } + return ( )} - {message.reasoning && ( - - )} - {(message.content || message.reasoning) && mode === 'view' && (
({ - transform: (chunk, controller) => { + transform: async (chunk, controller) => { + const showToolCalls = await showToolCallsFlag(); + const shouldStreamToolCalls = showToolCalls === 'console'; + if (ArrayBuffer.isView(chunk)) { try { const chunkBuffer = new Uint8Array( @@ -160,10 +164,12 @@ export class PatternModel implements LanguageModelV1 { textDelta: event.data, }); } else if (SUPPORTED_EVENT_TYPES.includes(event.type)) { - /** - * TODO: Re-enable reasoning when backend supports it - * https://github.com/pattern-tech/pattern-app/issues/27 - */ + if (shouldStreamToolCalls) { + controller.enqueue({ + type: 'reasoning', + textDelta: JSON.stringify(event), + }); + } } else { controller.enqueue({ type: 'error', diff --git a/lib/flags.ts b/lib/flags.ts index 6938f92..fa6b326 100644 --- a/lib/flags.ts +++ b/lib/flags.ts @@ -8,3 +8,14 @@ import { flag } from 'flags/next'; * key: 'some-flag', * }); */ + +/** + * Feature flag to control whether to show tool calls in the chat UI + */ +type ShowToolCalls = 'console' | undefined; +export const showToolCalls = flag({ + adapter: edgeConfigAdapter(), + key: 'show-tool-calls', + options: ['console'], + defaultValue: undefined, +});