more JSON parsing cases

This commit is contained in:
Dallin Romney 2025-06-04 17:55:31 -07:00
parent dcdc04f0ba
commit 4137954fea
4 changed files with 68 additions and 33 deletions

View file

@ -294,17 +294,27 @@ export class LanceDbIndex implements CodebaseIndex {
);
const cachedItems = await stmt.all();
const lanceRows: LanceDbRow[] = cachedItems.map(
({ uuid, vector, startLine, endLine, contents }) => ({
path,
uuid,
startLine,
endLine,
contents,
cachekey: cacheKey,
vector: JSON.parse(vector),
}),
);
const lanceRows: LanceDbRow[] = [];
for (const item of cachedItems) {
try {
const vector = JSON.parse(item.vector);
const { uuid, startLine, endLine, contents } = item;
cachedItems.push({
path,
uuid,
startLine,
endLine,
contents,
cachekey: cacheKey,
vector,
});
} catch (err) {
console.warn(
`LanceDBIndex, skipping ${item.path} due to invalid vector JSON:\n${item.vector}\n\nError: ${err}`,
);
}
}
if (lanceRows.length > 0) {
if (needToCreateLanceTable) {

View file

@ -565,10 +565,14 @@ class Bedrock extends BaseLLM {
const command = new InvokeModelCommand(input);
const response = await client.send(command);
if (response.body) {
const responseBody = JSON.parse(
new TextDecoder().decode(response.body),
);
return this._extractEmbeddings(responseBody);
const decoder = new TextDecoder();
const decoded = decoder.decode(response.body);
try {
const responseBody = JSON.parse(decoded);
return this._extractEmbeddings(responseBody);
} catch (e) {
console.error(`Error parsing response body from:\n${decoded}`, e);
}
}
return [];
}),
@ -663,12 +667,19 @@ class Bedrock extends BaseLLM {
throw new Error("Empty response received from Bedrock");
}
const responseBody = JSON.parse(new TextDecoder().decode(response.body));
// Sort results by index to maintain original order
return responseBody.results
.sort((a: any, b: any) => a.index - b.index)
.map((result: any) => result.relevance_score);
const decoder = new TextDecoder();
const decoded = decoder.decode(response.body);
try {
const responseBody = JSON.parse(decoded);
// Sort results by index to maintain original order
return responseBody.results
.sort((a: any, b: any) => a.index - b.index)
.map((result: any) => result.relevance_score);
} catch (e) {
throw new Error(
`Error parsing JSON from Bedrock response body:\n${decoded}, ${JSON.stringify(e)}`,
);
}
} catch (error: unknown) {
if (error instanceof Error) {
if ("code" in error) {

View file

@ -52,11 +52,16 @@ class HuggingFaceTEIEmbeddingsProvider extends BaseLLM {
});
if (!resp.ok) {
const text = await resp.text();
const embedError = JSON.parse(text) as TEIEmbedErrorResponse;
if (!embedError.error_type || !embedError.error) {
throw new Error(text);
let teiError: TEIEmbedErrorResponse | null = null;
try {
teiError = JSON.parse(text);
} catch (e) {
console.log(`Failed to parse TEI embed error response:\n${text}`, e);
}
throw new TEIEmbedError(embedError);
if (teiError && (teiError.error_type || teiError.error)) {
throw new TEIEmbedError(teiError);
}
throw new Error(text);
}
return (await resp.json()) as number[][];
}

View file

@ -157,14 +157,23 @@ class SageMaker extends BaseLLM {
const response = await client.send(command);
if (response.Body) {
const responseBody = JSON.parse(new TextDecoder().decode(response.Body));
// If the body contains a key called "embedding" or "embeddings", return the value, otherwise return the whole body
if (responseBody.embedding) {
return responseBody.embedding;
} else if (responseBody.embeddings) {
return responseBody.embeddings;
} else {
return responseBody;
const decoder = new TextDecoder();
const decoded = decoder.decode(response.Body);
try {
const responseBody = JSON.parse(decoded);
// If the body contains a key called "embedding" or "embeddings", return the value, otherwise return the whole body
if (responseBody.embedding) {
return responseBody.embedding;
} else if (responseBody.embeddings) {
return responseBody.embeddings;
} else {
return responseBody;
}
} catch (e) {
let message = e instanceof Error ? e.message : String(e);
throw new Error(
`Failed to parse response from SageMaker:\n${decoded}\nError: ${message}`,
);
}
}
}