惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
B
Blog
月光博客
月光博客
博客园 - 【当耐特】
WordPress大学
WordPress大学
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Jina AI
Jina AI
博客园 - Franky
MyScale Blog
MyScale Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Last Week in AI
Last Week in AI
B
Blog RSS Feed
H
Help Net Security

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(media-understanding): parse nested Gemini output JSON...
lin-hongkuan · 2026-06-25 · via Recent Commits to openclaw:main

@@ -3,16 +3,119 @@

33

/** Parse the last JSON object in a noisy provider output string. */

44

function extractLastJsonObject(raw: string): unknown {

55

const trimmed = raw.trim();

6-

const start = trimmed.lastIndexOf("{");

7-

if (start === -1) {

8-

return null;

6+

const ranges: Array<{ end: number; start: number }> = [];

7+

const starts: number[] = [];

8+

let inString = false;

9+

let escaped = false;

10+

let preambleQuote: string | undefined;

11+

let preambleEscaped = false;

12+

let previousSignificant: string | undefined;

13+

let lineHasNonWhitespace = false;

14+

let arrayDepth = 0;

15+

let candidateHasContent = false;

16+17+

for (let index = 0; index < trimmed.length; index += 1) {

18+

const character = trimmed[index];

19+

if (inString) {

20+

if (character === "\n" || character === "\r") {

21+

starts.length = 0;

22+

inString = false;

23+

escaped = false;

24+

} else if (escaped) {

25+

escaped = false;

26+

} else if (character === "\\") {

27+

escaped = true;

28+

} else if (character === '"') {

29+

inString = false;

30+

}

31+

continue;

32+

}

33+34+

if (starts.length === 0) {

35+

if (preambleQuote !== undefined) {

36+

if (character === "\n" || character === "\r") {

37+

preambleQuote = undefined;

38+

preambleEscaped = false;

39+

} else if (preambleEscaped) {

40+

preambleEscaped = false;

41+

} else if (character === "\\") {

42+

preambleEscaped = true;

43+

} else if (character === preambleQuote) {

44+

preambleQuote = undefined;

45+

}

46+

continue;

47+

}

48+

if (character === '"' || character === "'" || character === "`") {

49+

const previous = trimmed[index - 1];

50+

if (previous === undefined || /[\s:([{]/.test(previous)) {

51+

preambleQuote = character;

52+

preambleEscaped = false;

53+

continue;

54+

}

55+

}

56+

if (character === "{") {

57+

arrayDepth = 0;

58+

candidateHasContent = false;

59+

starts.push(index);

60+

}

61+

if (!/\s/.test(character)) {

62+

previousSignificant = character;

63+

lineHasNonWhitespace = true;

64+

} else if (character === "\n" || character === "\r") {

65+

lineHasNonWhitespace = false;

66+

}

67+

continue;

68+

}

69+70+

const hadCandidateContent = candidateHasContent;

71+

if (character === '"') {

72+

inString = true;

73+

} else if (character === "{") {

74+

if (

75+

previousSignificant === ":" ||

76+

previousSignificant === "[" ||

77+

previousSignificant === '"' ||

78+

(previousSignificant === "," && (lineHasNonWhitespace || arrayDepth > 0))

79+

) {

80+

starts.push(index);

81+

} else if (!lineHasNonWhitespace && !hadCandidateContent) {

82+

// Only resync at a clean record boundary; otherwise keep malformed

83+

// outer objects from promoting diagnostic payloads as valid results.

84+

starts.length = 1;

85+

starts[0] = index;

86+

arrayDepth = 0;

87+

candidateHasContent = false;

88+

}

89+

} else if (character === "}" && starts.length > 0) {

90+

const start = starts.pop();

91+

if (start !== undefined && starts.length === 0) {

92+

ranges.push({ start, end: index });

93+

}

94+

} else if (character === "[") {

95+

arrayDepth += 1;

96+

} else if (character === "]" && arrayDepth > 0) {

97+

arrayDepth -= 1;

98+

}

99+100+

if (!/\s/.test(character)) {

101+

candidateHasContent = true;

102+

previousSignificant = character;

103+

lineHasNonWhitespace = true;

104+

} else if (character === "\n" || character === "\r") {

105+

lineHasNonWhitespace = false;

106+

}

9107

}

10-

const slice = trimmed.slice(start);

11-

try {

12-

return JSON.parse(slice);

13-

} catch {

14-

return null;

108+109+

for (let index = ranges.length - 1; index >= 0; index -= 1) {

110+

const range = ranges[index];

111+

try {

112+

return JSON.parse(trimmed.slice(range.start, range.end + 1));

113+

} catch {

114+

// Ignore malformed objects and try the previous completed range.

115+

}

15116

}

117+118+

return null;

16119

}

1712018121

/** Extract Gemini CLI-style response text from the last JSON object in output. */