mirror of
https://gitee.com/wanwujie/deer-flow
synced 2026-04-25 23:14:46 +08:00
feat: extract parseJSON()
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -2,3 +2,5 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
export * from "./time";
|
||||
export * from "./json";
|
||||
export * from "./deep-clone";
|
||||
|
||||
17
web/src/core/utils/json.ts
Normal file
17
web/src/core/utils/json.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user