Skip to content

Commit df766c9

Browse files
authored
feat: added support for reasoning content (stackblitz-labs#1168)
1 parent 6603533 commit df766c9

File tree

7 files changed

+223
-109
lines changed

7 files changed

+223
-109
lines changed

app/components/chat/Markdown.tsx

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Artifact } from './Artifact';
77
import { CodeBlock } from './CodeBlock';
88

99
import styles from './Markdown.module.scss';
10+
import ThoughtBox from './ThoughtBox';
1011

1112
const logger = createScopedLogger('MarkdownComponent');
1213

@@ -22,6 +23,8 @@ export const Markdown = memo(({ children, html = false, limitedMarkdown = false
2223
const components = useMemo(() => {
2324
return {
2425
div: ({ className, children, node, ...props }) => {
26+
console.log(className, node);
27+
2528
if (className?.includes('__boltArtifact__')) {
2629
const messageId = node?.properties.dataMessageId as string;
2730

@@ -32,6 +35,10 @@ export const Markdown = memo(({ children, html = false, limitedMarkdown = false
3235
return <Artifact messageId={messageId} />;
3336
}
3437

38+
if (className?.includes('__boltThought__')) {
39+
return <ThoughtBox title="Thought process">{children}</ThoughtBox>;
40+
}
41+
3542
return (
3643
<div className={className} {...props}>
3744
{children}

app/components/chat/ThoughtBox.tsx

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { useState, type PropsWithChildren } from 'react';
2+
3+
const ThoughtBox = ({ title, children }: PropsWithChildren<{ title: string }>) => {
4+
const [isExpanded, setIsExpanded] = useState(false);
5+
6+
return (
7+
<div
8+
onClick={() => setIsExpanded(!isExpanded)}
9+
className={`
10+
bg-bolt-elements-background-depth-2
11+
shadow-md
12+
rounded-lg
13+
cursor-pointer
14+
transition-all
15+
duration-300
16+
${isExpanded ? 'max-h-96' : 'max-h-13'}
17+
overflow-auto
18+
border border-bolt-elements-borderColor
19+
`}
20+
>
21+
<div className="p-4 flex items-center gap-4 rounded-lg text-bolt-elements-textSecondary font-medium leading-5 text-sm border border-bolt-elements-borderColor">
22+
<div className="i-ph:brain-thin text-2xl" />
23+
<div className="div">
24+
<span> {title}</span>{' '}
25+
{!isExpanded && <span className="text-bolt-elements-textTertiary"> - Click to expand</span>}
26+
</div>
27+
</div>
28+
<div
29+
className={`
30+
transition-opacity
31+
duration-300
32+
p-4
33+
rounded-lg
34+
${isExpanded ? 'opacity-100' : 'opacity-0'}
35+
`}
36+
>
37+
{children}
38+
</div>
39+
</div>
40+
);
41+
};
42+
43+
export default ThoughtBox;

app/lib/modules/llm/providers/deepseek.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { BaseProvider } from '~/lib/modules/llm/base-provider';
22
import type { ModelInfo } from '~/lib/modules/llm/types';
33
import type { IProviderSetting } from '~/types/model';
44
import type { LanguageModelV1 } from 'ai';
5-
import { createOpenAI } from '@ai-sdk/openai';
5+
import { createDeepSeek } from '@ai-sdk/deepseek';
66

77
export default class DeepseekProvider extends BaseProvider {
88
name = 'Deepseek';
@@ -38,11 +38,12 @@ export default class DeepseekProvider extends BaseProvider {
3838
throw new Error(`Missing API key for ${this.name} provider`);
3939
}
4040

41-
const openai = createOpenAI({
42-
baseURL: 'https://api.deepseek.com/beta',
41+
const deepseek = createDeepSeek({
4342
apiKey,
4443
});
4544

46-
return openai(model);
45+
return deepseek(model, {
46+
// simulateStreaming: true,
47+
});
4748
}
4849
}

app/routes/api.chat.ts

+31-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ async function chatAction({ context, request }: ActionFunctionArgs) {
6363
const totalMessageContent = messages.reduce((acc, message) => acc + message.content, '');
6464
logger.debug(`Total message length: ${totalMessageContent.split(' ').length}, words`);
6565

66+
let lastChunk: string | undefined = undefined;
67+
6668
const dataStream = createDataStream({
6769
async execute(dataStream) {
6870
const filePaths = getFilePaths(files || {});
@@ -247,15 +249,42 @@ async function chatAction({ context, request }: ActionFunctionArgs) {
247249
}
248250
}
249251
})();
250-
251252
result.mergeIntoDataStream(dataStream);
252253
},
253254
onError: (error: any) => `Custom error: ${error.message}`,
254255
}).pipeThrough(
255256
new TransformStream({
256257
transform: (chunk, controller) => {
258+
if (!lastChunk) {
259+
lastChunk = ' ';
260+
}
261+
262+
if (typeof chunk === 'string') {
263+
if (chunk.startsWith('g') && !lastChunk.startsWith('g')) {
264+
controller.enqueue(encoder.encode(`0: "<div class=\\"__boltThought__\\">"\n`));
265+
}
266+
267+
if (lastChunk.startsWith('g') && !chunk.startsWith('g')) {
268+
controller.enqueue(encoder.encode(`0: "</div>\\n"\n`));
269+
}
270+
}
271+
272+
lastChunk = chunk;
273+
274+
let transformedChunk = chunk;
275+
276+
if (typeof chunk === 'string' && chunk.startsWith('g')) {
277+
let content = chunk.split(':').slice(1).join(':');
278+
279+
if (content.endsWith('\n')) {
280+
content = content.slice(0, content.length - 1);
281+
}
282+
283+
transformedChunk = `0:${content}\n`;
284+
}
285+
257286
// Convert the string stream to a byte stream
258-
const str = typeof chunk === 'string' ? chunk : JSON.stringify(chunk);
287+
const str = typeof transformedChunk === 'string' ? transformedChunk : JSON.stringify(transformedChunk);
259288
controller.enqueue(encoder.encode(str));
260289
},
261290
}),

app/utils/markdown.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,13 @@ const rehypeSanitizeOptions: RehypeSanitizeOptions = {
6161
tagNames: allowedHTMLElements,
6262
attributes: {
6363
...defaultSchema.attributes,
64-
div: [...(defaultSchema.attributes?.div ?? []), 'data*', ['className', '__boltArtifact__']],
64+
div: [
65+
...(defaultSchema.attributes?.div ?? []),
66+
'data*',
67+
['className', '__boltArtifact__', '__boltThought__'],
68+
69+
// ['className', '__boltThought__']
70+
],
6571
},
6672
strip: [],
6773
};

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@
3333
"@ai-sdk/amazon-bedrock": "1.0.6",
3434
"@ai-sdk/anthropic": "^0.0.39",
3535
"@ai-sdk/cohere": "^1.0.3",
36+
"@ai-sdk/deepseek": "^0.1.3",
3637
"@ai-sdk/google": "^0.0.52",
3738
"@ai-sdk/mistral": "^0.0.43",
38-
"@ai-sdk/openai": "^0.0.66",
39+
"@ai-sdk/openai": "^1.1.2",
3940
"@codemirror/autocomplete": "^6.18.3",
4041
"@codemirror/commands": "^6.7.1",
4142
"@codemirror/lang-cpp": "^6.0.2",
@@ -75,7 +76,7 @@
7576
"@xterm/addon-fit": "^0.10.0",
7677
"@xterm/addon-web-links": "^0.11.0",
7778
"@xterm/xterm": "^5.5.0",
78-
"ai": "^4.0.13",
79+
"ai": "^4.1.2",
7980
"chalk": "^5.4.1",
8081
"date-fns": "^3.6.0",
8182
"diff": "^5.2.0",
@@ -131,7 +132,7 @@
131132
"vite-tsconfig-paths": "^4.3.2",
132133
"vitest": "^2.1.7",
133134
"wrangler": "^3.91.0",
134-
"zod": "^3.23.8"
135+
"zod": "^3.24.1"
135136
},
136137
"resolutions": {
137138
"@typescript-eslint/utils": "^8.0.0-alpha.30"

0 commit comments

Comments
 (0)