feat: extract parseJSON()

This commit is contained in:
Li Xin
2025-04-22 11:02:18 +08:00
parent b36f36f530
commit 5ee1632489
5 changed files with 30 additions and 9 deletions

View File

@@ -1,7 +1,6 @@
// Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
// SPDX-License-Identifier: MIT
import { parse } from "best-effort-json-parser";
import { nanoid } from "nanoid";
import { create } from "zustand";
@@ -9,6 +8,7 @@ import { chatStream } from "../api";
import { generatePodcast } from "../api/podcast";
import type { Message } from "../messages";
import { mergeMessage } from "../messages";
import { parseJSON } from "../utils";
const THREAD_ID = nanoid();
@@ -230,7 +230,7 @@ export async function listenToPodcast(researchId: string) {
const reportMessageId = useStore.getState().researchReportIds.get(researchId);
if (planMessageId && reportMessageId) {
const planMessage = getMessage(planMessageId)!;
const title = (JSON.parse(planMessage.content) as { title: string }).title;
const title = parseJSON(planMessage.content, { title: "Untitled" }).title;
const reportMessage = getMessage(reportMessageId);
if (reportMessage?.content) {
appendMessage({
@@ -269,7 +269,9 @@ export function useResearchTitle(researchId: string) {
const planMessage = useMessage(
useStore.getState().researchPlanIds.get(researchId),
);
return planMessage ? parse(planMessage.content).title : undefined;
return planMessage
? parseJSON(planMessage.content, { title: "" }).title
: undefined;
}
export function useMessage(messageId: string | null | undefined) {

View File

@@ -2,3 +2,5 @@
// SPDX-License-Identifier: MIT
export * from "./time";
export * from "./json";
export * from "./deep-clone";

View File

@@ -0,0 +1,17 @@
import { parse } from "best-effort-json-parser";
export function parseJSON<T>(json: string | null | undefined, fallback: T) {
if (!json) {
return fallback;
}
try {
const raw = json
.trim()
.replace(/^```json\s*/, "")
.replace(/^```\s*/, "")
.replace(/\s*```$/, "");
return parse(raw) as T;
} catch {
return fallback;
}
}