Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions react-native/src/api/bedrock-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ function parseChunk(rawChunk: string) {
if (rawChunk.length > 0) {
try {
const bedrockChunk: BedrockChunk = JSON.parse(rawChunk);
return extractChunkContent(bedrockChunk);
return extractChunkContent(bedrockChunk, rawChunk);
} catch (error) {
if (rawChunk.indexOf('}{') > 0) {
const jsonParts = rawChunk.split('}{');
Expand All @@ -410,7 +410,7 @@ function parseChunk(rawChunk: string) {
(i >= 1 ? '{' : '') + part + (i < jsonParts.length - 1 ? '}' : '');
try {
const chunk: BedrockChunk = JSON.parse(part);
const content = extractChunkContent(chunk);
const content = extractChunkContent(chunk, rawChunk);
if (content.reasoning) {
combinedReasoning += content.reasoning;
}
Expand Down Expand Up @@ -446,11 +446,14 @@ function parseChunk(rawChunk: string) {
/**
* Helper function to extract content from a BedrockChunk
*/
function extractChunkContent(bedrockChunk: BedrockChunk) {
function extractChunkContent(bedrockChunk: BedrockChunk, rawChunk: string) {
const reasoning =
bedrockChunk?.contentBlockDelta?.delta?.reasoningContent?.text;
const text = bedrockChunk?.contentBlockDelta?.delta?.text;
let text = bedrockChunk?.contentBlockDelta?.delta?.text;
const usage = bedrockChunk?.metadata?.usage;
if (bedrockChunk?.detail) {
text = rawChunk;
}
return { reasoning, text, usage };
}

Expand Down
16 changes: 15 additions & 1 deletion react-native/src/chat/component/markdown/useMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ const useMarkdown = (
// Use useRef to store cache to prevent loss during re-renders
const cacheRef = useRef<{
lastValue: string;
lastNewContent: string;
cachedTokens: ReturnType<typeof marked.lexer>;
cachedElements: ReactNode[];
}>({
lastValue: '',
lastNewContent: '',
cachedTokens: marked.lexer(''),
cachedElements: [],
});
Expand Down Expand Up @@ -81,6 +83,7 @@ const useMarkdown = (
if (options?.chatStatus === ChatStatus.Running && value !== '...') {
cacheRef.current = {
lastValue: value,
lastNewContent: value,
cachedTokens: tokens,
cachedElements: elements,
};
Expand All @@ -99,7 +102,17 @@ const useMarkdown = (
const lastToken = cacheRef.current.cachedTokens[lastTokenIndex];

// Combine the text of the last token with new text to get the latest content to parse
let combinedText = lastToken.raw + newContent;
let combinedText = lastToken.raw;
if (lastToken.type === 'list') {
const lastNewContent = cacheRef.current.lastNewContent;
if (
combinedText[combinedText.length - 1] === '\n' &&
lastNewContent[lastNewContent.length - 1] === ' '
) {
combinedText = combinedText.slice(0, -1) + ' ';
}
}
combinedText += newContent;
if (lastToken.type === 'space') {
combinedText =
cacheRef.current.cachedTokens[lastTokenIndex - 1].raw + combinedText;
Expand Down Expand Up @@ -132,6 +145,7 @@ const useMarkdown = (
// Update cache with new references
cacheRef.current = {
lastValue: value,
lastNewContent: newContent,
cachedTokens: mergedTokens,
cachedElements: mergedElements,
};
Expand Down
1 change: 1 addition & 0 deletions react-native/src/types/Chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export interface BedrockChunk {
metadata: {
usage: Usage;
};
detail: string;
}

export interface Delta {
Expand Down
Loading