Skip to content

Commit b7f931f

Browse files
authored
Merge pull request #50 from pattern-tech/feat/stream-pattern-events
feat: stream pattern events to console behind a feature flag
2 parents 34d6be0 + 83de4c0 commit b7f931f

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

components/message.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { DocumentToolCall, DocumentToolResult } from './document';
1515
import { DocumentPreview } from './document-preview';
1616
import { Markdown } from './markdown';
1717
import { MessageEditor } from './message-editor';
18-
import { MessageReasoning } from './message-reasoning';
1918
import { PreviewAttachment } from './preview-attachment';
2019
import { Weather } from './weather';
2120

@@ -42,6 +41,13 @@ const PurePreviewMessage = ({
4241
}) => {
4342
const [mode, setMode] = useState<'view' | 'edit'>('view');
4443

44+
/**
45+
* For now, only streaming reasoning to console is supported
46+
*/
47+
if (message.reasoning) {
48+
console.log('Pattern event: ', message.reasoning);
49+
}
50+
4551
return (
4652
<AnimatePresence>
4753
<motion.div
@@ -86,13 +92,6 @@ const PurePreviewMessage = ({
8692
</div>
8793
)}
8894

89-
{message.reasoning && (
90-
<MessageReasoning
91-
isLoading={isLoading}
92-
reasoning={message.reasoning}
93-
/>
94-
)}
95-
9695
{(message.content || message.reasoning) && mode === 'view' && (
9796
<div className="flex flex-row gap-2 items-start">
9897
<div

lib/ai/pattern-model.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111
ToolStartEvent,
1212
} from '@/lib/ai/types';
1313

14+
import { showToolCalls as showToolCallsFlag } from '../flags';
1415
import { extractErrorMessageOrDefault } from '../utils';
1516

1617
const textDecoder = new TextDecoder();
@@ -85,7 +86,10 @@ export class PatternModel implements LanguageModelV1 {
8586
let incompleteJsonFragment = '';
8687

8788
return new TransformStream<string, LanguageModelV1StreamPart>({
88-
transform: (chunk, controller) => {
89+
transform: async (chunk, controller) => {
90+
const showToolCalls = await showToolCallsFlag();
91+
const shouldStreamToolCalls = showToolCalls === 'console';
92+
8993
if (ArrayBuffer.isView(chunk)) {
9094
try {
9195
const chunkBuffer = new Uint8Array(
@@ -160,10 +164,12 @@ export class PatternModel implements LanguageModelV1 {
160164
textDelta: event.data,
161165
});
162166
} else if (SUPPORTED_EVENT_TYPES.includes(event.type)) {
163-
/**
164-
* TODO: Re-enable reasoning when backend supports it
165-
* https://github.com/pattern-tech/pattern-app/issues/27
166-
*/
167+
if (shouldStreamToolCalls) {
168+
controller.enqueue({
169+
type: 'reasoning',
170+
textDelta: JSON.stringify(event),
171+
});
172+
}
167173
} else {
168174
controller.enqueue({
169175
type: 'error',

lib/flags.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,14 @@ import { flag } from 'flags/next';
88
* key: 'some-flag',
99
* });
1010
*/
11+
12+
/**
13+
* Feature flag to control whether to show tool calls in the chat UI
14+
*/
15+
type ShowToolCalls = 'console' | undefined;
16+
export const showToolCalls = flag<ShowToolCalls>({
17+
adapter: edgeConfigAdapter(),
18+
key: 'show-tool-calls',
19+
options: ['console'],
20+
defaultValue: undefined,
21+
});

0 commit comments

Comments
 (0)