mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 11:08:37 +00:00
refactor(media-understanding): share gemini output extract
This commit is contained in:
26
src/media-understanding/output-extract.ts
Normal file
26
src/media-understanding/output-extract.ts
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
export function extractLastJsonObject(raw: string): unknown {
|
||||||
|
const trimmed = raw.trim();
|
||||||
|
const start = trimmed.lastIndexOf("{");
|
||||||
|
if (start === -1) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const slice = trimmed.slice(start);
|
||||||
|
try {
|
||||||
|
return JSON.parse(slice);
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function extractGeminiResponse(raw: string): string | null {
|
||||||
|
const payload = extractLastJsonObject(raw);
|
||||||
|
if (!payload || typeof payload !== "object") {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const response = (payload as { response?: unknown }).response;
|
||||||
|
if (typeof response !== "string") {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const trimmed = response.trim();
|
||||||
|
return trimmed || null;
|
||||||
|
}
|
||||||
@@ -25,6 +25,7 @@ import {
|
|||||||
DEFAULT_TIMEOUT_SECONDS,
|
DEFAULT_TIMEOUT_SECONDS,
|
||||||
} from "./defaults.js";
|
} from "./defaults.js";
|
||||||
import { MediaUnderstandingSkipError } from "./errors.js";
|
import { MediaUnderstandingSkipError } from "./errors.js";
|
||||||
|
import { extractGeminiResponse } from "./output-extract.js";
|
||||||
import { describeImageWithModel } from "./providers/image.js";
|
import { describeImageWithModel } from "./providers/image.js";
|
||||||
import { getMediaUnderstandingProvider, normalizeMediaProviderId } from "./providers/index.js";
|
import { getMediaUnderstandingProvider, normalizeMediaProviderId } from "./providers/index.js";
|
||||||
import { resolveMaxBytes, resolveMaxChars, resolvePrompt, resolveTimeoutMs } from "./resolve.js";
|
import { resolveMaxBytes, resolveMaxChars, resolvePrompt, resolveTimeoutMs } from "./resolve.js";
|
||||||
@@ -40,33 +41,6 @@ function trimOutput(text: string, maxChars?: number): string {
|
|||||||
return trimmed.slice(0, maxChars).trim();
|
return trimmed.slice(0, maxChars).trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractLastJsonObject(raw: string): unknown {
|
|
||||||
const trimmed = raw.trim();
|
|
||||||
const start = trimmed.lastIndexOf("{");
|
|
||||||
if (start === -1) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
const slice = trimmed.slice(start);
|
|
||||||
try {
|
|
||||||
return JSON.parse(slice);
|
|
||||||
} catch {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function extractGeminiResponse(raw: string): string | null {
|
|
||||||
const payload = extractLastJsonObject(raw);
|
|
||||||
if (!payload || typeof payload !== "object") {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
const response = (payload as { response?: unknown }).response;
|
|
||||||
if (typeof response !== "string") {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
const trimmed = response.trim();
|
|
||||||
return trimmed || null;
|
|
||||||
}
|
|
||||||
|
|
||||||
function extractSherpaOnnxText(raw: string): string | null {
|
function extractSherpaOnnxText(raw: string): string | null {
|
||||||
const tryParse = (value: string): string | null => {
|
const tryParse = (value: string): string | null => {
|
||||||
const trimmed = value.trim();
|
const trimmed = value.trim();
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ import {
|
|||||||
DEFAULT_IMAGE_MODELS,
|
DEFAULT_IMAGE_MODELS,
|
||||||
} from "./defaults.js";
|
} from "./defaults.js";
|
||||||
import { isMediaUnderstandingSkipError } from "./errors.js";
|
import { isMediaUnderstandingSkipError } from "./errors.js";
|
||||||
|
import { extractGeminiResponse } from "./output-extract.js";
|
||||||
import {
|
import {
|
||||||
buildMediaUnderstandingRegistry,
|
buildMediaUnderstandingRegistry,
|
||||||
getMediaUnderstandingProvider,
|
getMediaUnderstandingProvider,
|
||||||
@@ -186,33 +187,6 @@ async function fileExists(filePath?: string | null): Promise<boolean> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractLastJsonObject(raw: string): unknown {
|
|
||||||
const trimmed = raw.trim();
|
|
||||||
const start = trimmed.lastIndexOf("{");
|
|
||||||
if (start === -1) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
const slice = trimmed.slice(start);
|
|
||||||
try {
|
|
||||||
return JSON.parse(slice);
|
|
||||||
} catch {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function extractGeminiResponse(raw: string): string | null {
|
|
||||||
const payload = extractLastJsonObject(raw);
|
|
||||||
if (!payload || typeof payload !== "object") {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
const response = (payload as { response?: unknown }).response;
|
|
||||||
if (typeof response !== "string") {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
const trimmed = response.trim();
|
|
||||||
return trimmed || null;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function probeGeminiCli(): Promise<boolean> {
|
async function probeGeminiCli(): Promise<boolean> {
|
||||||
const cached = geminiProbeCache.get("gemini");
|
const cached = geminiProbeCache.get("gemini");
|
||||||
if (cached) {
|
if (cached) {
|
||||||
|
|||||||
Reference in New Issue
Block a user