diff --git a/core/indexing/LanceDbIndex.ts b/core/indexing/LanceDbIndex.ts index cf41beaca..0bf6c94ca 100644 --- a/core/indexing/LanceDbIndex.ts +++ b/core/indexing/LanceDbIndex.ts @@ -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) { diff --git a/core/llm/llms/Bedrock.ts b/core/llm/llms/Bedrock.ts index b39113aaf..ae595f3f3 100644 --- a/core/llm/llms/Bedrock.ts +++ b/core/llm/llms/Bedrock.ts @@ -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) { diff --git a/core/llm/llms/HuggingFaceTEI.ts b/core/llm/llms/HuggingFaceTEI.ts index a96840a42..175aedde0 100644 --- a/core/llm/llms/HuggingFaceTEI.ts +++ b/core/llm/llms/HuggingFaceTEI.ts @@ -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[][]; } diff --git a/core/llm/llms/SageMaker.ts b/core/llm/llms/SageMaker.ts index 056fed2cc..bbb87c131 100644 --- a/core/llm/llms/SageMaker.ts +++ b/core/llm/llms/SageMaker.ts @@ -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}`, + ); } } }