Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
vector: do not boot client on init
Browse files Browse the repository at this point in the history
  • Loading branch information
thdxr committed Sep 18, 2024
1 parent 1c949c8 commit 95ab0f6
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions sdk/js/src/vector/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import { AwsClient } from "aws4fetch";
import { Resource } from "../resource.js";

const client = new AwsClient({
accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
sessionToken: process.env.AWS_SESSION_TOKEN!,
region: process.env.AWS_REGION!,
});
const endpoint = `https://lambda.${process.env.AWS_REGION}.amazonaws.com/2015-03-31`;
import { client } from "../aws/client.js";

export interface PutEvent {
/**
Expand Down Expand Up @@ -264,7 +256,7 @@ export function VectorClient<
// @ts-expect-error
Resource[name].putFunction,
JSON.stringify(event),
"Failed to store into the vector db"
"Failed to store into the vector db",
);
},

Expand All @@ -273,7 +265,7 @@ export function VectorClient<
// @ts-expect-error
Resource[name].queryFunction,
JSON.stringify(event),
"Failed to query the vector db"
"Failed to query the vector db",
);
},

Expand All @@ -282,7 +274,7 @@ export function VectorClient<
// @ts-expect-error
Resource[name].removeFunction,
JSON.stringify(event),
"Failed to remove from the vector db"
"Failed to remove from the vector db",
);
},
};
Expand All @@ -292,16 +284,18 @@ async function invokeFunction<T>(
functionName: string,
body: string,
errorMessage: string,
attempts = 0
attempts = 0,
): Promise<T> {
try {
const response = await client.fetch(
const c = await client();
const endpoint = `https://lambda.${process.env.AWS_REGION}.amazonaws.com/2015-03-31`;
const response = await c.fetch(
`${endpoint}/functions/${functionName}/invocations`,
{
method: "POST",
headers: { Accept: "application/json" },
body,
}
},
);

// success
Expand Down Expand Up @@ -352,13 +346,13 @@ async function invokeFunction<T>(

// retry
await new Promise((resolve) =>
setTimeout(resolve, 1.5 ** attempts * 100 * Math.random())
setTimeout(resolve, 1.5 ** attempts * 100 * Math.random()),
);
return await invokeFunction<T>(
functionName,
body,
errorMessage,
attempts + 1
attempts + 1,
);
}
}

0 comments on commit 95ab0f6

Please sign in to comment.