Skip to content

Commit bd9af67

Browse files
committed
feat: parse object to string for tool response input field
1 parent 48436bb commit bd9af67

3 files changed

Lines changed: 32 additions & 8 deletions

File tree

libs/mobile/chat/features/chat/src/lib/utils/parse-response-message-content.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { decode } from 'html-entities';
2-
import { isEmpty } from 'lodash-es';
2+
import { parseObjectToString } from '@open-webui-react-native/shared/utils/strings';
33

44
type PayloadContentType = 'json' | 'text';
55

@@ -50,7 +50,11 @@ const readQuotedAttr = (tag: string, attr: string): string | null => {
5050

5151
continue;
5252
} else {
53-
out += esc;
53+
// e.g. `\"` in attributes: keep `\`, let `"` pass through for html decode
54+
out += c;
55+
i += 1;
56+
57+
continue;
5458
}
5559

5660
i += 2;
@@ -114,8 +118,8 @@ const indexAfterOpenDetailsTag = (s: string): number => {
114118
return -1;
115119
};
116120

117-
const parseJsonRecursive = (str: string): unknown => {
118-
let cur: unknown = str.trim();
121+
const parseJsonRecursive = (str: string): string => {
122+
let cur = str.trim();
119123

120124
for (let depth = 0; depth < 32; depth++) {
121125
if (typeof cur !== 'string') {
@@ -198,11 +202,8 @@ const tryParseLeadingToolCallsDetails = (content: string): { tool: ToolData; res
198202
const argsRaw = readQuotedAttr(openTag, 'arguments') ?? '';
199203
const resultRaw = readQuotedAttr(openTag, 'result') ?? '';
200204

201-
const inputPayload = classifyAndNormalizePayload(argsRaw);
202205
const outputPayload = classifyAndNormalizePayload(resultRaw);
203-
const parsedArgs = parseJsonRecursive(decode(argsRaw).trim());
204-
const input =
205-
typeof parsedArgs === 'object' && parsedArgs !== null && isEmpty(parsedArgs) ? undefined : inputPayload.normalized;
206+
const input = parseObjectToString(parseJsonRecursive(decode(argsRaw).trim()));
206207

207208
const blockEnd = leadingWs.length + openEnd + closeMatch.index + closeMatch[0].length;
208209
const rest = content.slice(blockEnd).trimStart();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './get-initials';
22
export * from './get-line-count';
3+
export * from './parse-object-to-string';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { isEmpty } from 'lodash-es';
2+
3+
export const parseObjectToString = (parsed: string): string | undefined => {
4+
if (typeof parsed === 'object' && parsed !== null && isEmpty(parsed)) {
5+
return undefined;
6+
}
7+
8+
if (typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed)) {
9+
return Object.entries(parsed)
10+
.map(
11+
([key, value]) =>
12+
`${key}\n${typeof value === 'object' && value !== null ? JSON.stringify(value) : String(value)}`,
13+
)
14+
.join('\n\n');
15+
}
16+
17+
if (typeof parsed === 'object' && parsed !== null) {
18+
return JSON.stringify(parsed, null, 2);
19+
}
20+
21+
return String(parsed);
22+
};

0 commit comments

Comments
 (0)