@sst-community/sst 0.0.0-placeholder.0 → 4.17.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +84 -0
- package/bin/sst.mjs +33 -0
- package/dist/auth/adapter/adapter.d.ts +23 -0
- package/dist/auth/adapter/adapter.js +4 -0
- package/dist/auth/adapter/apple.d.ts +5 -0
- package/dist/auth/adapter/apple.js +22 -0
- package/dist/auth/adapter/code.d.ts +7 -0
- package/dist/auth/adapter/code.js +47 -0
- package/dist/auth/adapter/facebook.d.ts +5 -0
- package/dist/auth/adapter/facebook.js +27 -0
- package/dist/auth/adapter/github.d.ts +7 -0
- package/dist/auth/adapter/github.js +15 -0
- package/dist/auth/adapter/google.d.ts +17 -0
- package/dist/auth/adapter/google.js +22 -0
- package/dist/auth/adapter/index.d.ts +11 -0
- package/dist/auth/adapter/index.js +10 -0
- package/dist/auth/adapter/link.d.ts +5 -0
- package/dist/auth/adapter/link.js +27 -0
- package/dist/auth/adapter/microsoft.d.ts +11 -0
- package/dist/auth/adapter/microsoft.js +16 -0
- package/dist/auth/adapter/oauth.d.ts +33 -0
- package/dist/auth/adapter/oauth.js +82 -0
- package/dist/auth/adapter/oidc.d.ts +19 -0
- package/dist/auth/adapter/oidc.js +48 -0
- package/dist/auth/adapter/spotify.d.ts +12 -0
- package/dist/auth/adapter/spotify.js +22 -0
- package/dist/auth/example/bun.d.ts +2 -0
- package/dist/auth/example/bun.js +46 -0
- package/dist/auth/handler.d.ts +57 -0
- package/dist/auth/handler.js +206 -0
- package/dist/auth/index.d.ts +10 -0
- package/dist/auth/index.js +10 -0
- package/dist/auth/session.d.ts +25 -0
- package/dist/auth/session.js +28 -0
- package/dist/aws/auth.d.ts +9 -0
- package/dist/aws/auth.js +12 -0
- package/dist/aws/bus.d.ts +28 -0
- package/dist/aws/bus.js +66 -0
- package/dist/aws/client.d.ts +8 -0
- package/dist/aws/client.js +54 -0
- package/dist/aws/realtime.d.ts +118 -0
- package/dist/aws/realtime.js +83 -0
- package/dist/aws/task.d.ts +228 -0
- package/dist/aws/task.js +232 -0
- package/dist/aws/workflow.d.ts +324 -0
- package/dist/aws/workflow.js +504 -0
- package/dist/event/index.d.ts +72 -0
- package/dist/event/index.js +35 -0
- package/dist/event/validator.d.ts +4 -0
- package/dist/event/validator.js +11 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +17 -0
- package/dist/realtime/index.d.ts +25 -0
- package/dist/realtime/index.js +24 -0
- package/dist/resource/cloudflare.d.ts +5 -0
- package/dist/resource/cloudflare.js +30 -0
- package/dist/resource/index.d.ts +1 -0
- package/dist/resource/index.js +1 -0
- package/dist/resource/node.d.ts +4 -0
- package/dist/resource/node.js +38 -0
- package/dist/resource/shared.d.ts +9 -0
- package/dist/resource/shared.js +62 -0
- package/dist/resource.d.ts +1 -0
- package/dist/resource.js +1 -0
- package/dist/util/prettify.d.ts +3 -0
- package/dist/util/prettify.js +1 -0
- package/dist/vector/index.d.ts +247 -0
- package/dist/vector/index.js +107 -0
- package/package.json +73 -1
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { realtime } from "../aws/realtime.js";
|
|
2
|
+
/**
|
|
3
|
+
* @deprecated import from `sst/aws/realtime` instead.
|
|
4
|
+
*
|
|
5
|
+
* Creates an authorization handler for the `Realtime` component, that validates
|
|
6
|
+
* the token and grants permissions for the topics the client can subscribe and publish to.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```js
|
|
10
|
+
* import { RealtimeAuthHandler, Resource } from "sst";
|
|
11
|
+
*
|
|
12
|
+
* export const handler = RealtimeAuthHandler(async (token) => {
|
|
13
|
+
* // Validate the token
|
|
14
|
+
* console.log(token);
|
|
15
|
+
*
|
|
16
|
+
* // Return the topics to subscribe and publish
|
|
17
|
+
* return {
|
|
18
|
+
* subscribe: [`${Resource.App.name}/${Resource.App.stage}/chat/room1`],
|
|
19
|
+
* publish: [`${Resource.App.name}/${Resource.App.stage}/chat/room1`],
|
|
20
|
+
* };
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export const RealtimeAuthHandler = realtime.authorizer;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { env } from "cloudflare:workers";
|
|
2
|
+
import { createResource, loadResourceEnvironment } from "./shared.js";
|
|
3
|
+
function loadCloudflareResources() {
|
|
4
|
+
loadResourceEnvironment(env);
|
|
5
|
+
}
|
|
6
|
+
export function wrapCloudflareHandler(handler) {
|
|
7
|
+
if (handler == null) {
|
|
8
|
+
return undefined;
|
|
9
|
+
}
|
|
10
|
+
if (typeof handler === "function" && handler.hasOwnProperty("prototype")) {
|
|
11
|
+
return class extends handler {
|
|
12
|
+
constructor(ctx, env) {
|
|
13
|
+
loadResourceEnvironment(env);
|
|
14
|
+
super(ctx, env);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function wrap(fn) {
|
|
19
|
+
return function (req, env, ...rest) {
|
|
20
|
+
loadResourceEnvironment(env);
|
|
21
|
+
return fn(req, env, ...rest);
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
const result = {};
|
|
25
|
+
for (const [key, value] of Object.entries(handler)) {
|
|
26
|
+
result[key] = wrap(value);
|
|
27
|
+
}
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
export const Resource = createResource(loadCloudflareResources);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./node.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./node.js";
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import crypto from "crypto";
|
|
2
|
+
import { readFileSync } from "fs";
|
|
3
|
+
import { env } from "process";
|
|
4
|
+
import { createResource, loadResourceData, loadResourceEnvironment, } from "./shared.js";
|
|
5
|
+
const state = globalThis;
|
|
6
|
+
function loadNodeResources() {
|
|
7
|
+
const environment = {
|
|
8
|
+
...env,
|
|
9
|
+
...globalThis.process?.env,
|
|
10
|
+
};
|
|
11
|
+
loadResourceEnvironment(environment);
|
|
12
|
+
if (environment.SST_RESOURCES_JSON) {
|
|
13
|
+
try {
|
|
14
|
+
loadResourceData(JSON.parse(environment.SST_RESOURCES_JSON));
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
console.error("Failed to parse SST_RESOURCES_JSON:", error);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
if (environment.SST_KEY_FILE &&
|
|
21
|
+
environment.SST_KEY &&
|
|
22
|
+
!state.SST_KEY_FILE_DATA) {
|
|
23
|
+
const key = Buffer.from(environment.SST_KEY, "base64");
|
|
24
|
+
const encryptedData = readFileSync(environment.SST_KEY_FILE);
|
|
25
|
+
const nonce = Buffer.alloc(12, 0);
|
|
26
|
+
const decipher = crypto.createDecipheriv("aes-256-gcm", key, nonce);
|
|
27
|
+
const authTag = encryptedData.subarray(-16);
|
|
28
|
+
const actualCiphertext = encryptedData.subarray(0, -16);
|
|
29
|
+
decipher.setAuthTag(authTag);
|
|
30
|
+
let decrypted = decipher.update(actualCiphertext);
|
|
31
|
+
decrypted = Buffer.concat([decrypted, decipher.final()]);
|
|
32
|
+
loadResourceData(JSON.parse(decrypted.toString()));
|
|
33
|
+
}
|
|
34
|
+
if (state.SST_KEY_FILE_DATA) {
|
|
35
|
+
loadResourceData(state.SST_KEY_FILE_DATA);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export const Resource = createResource(loadNodeResources);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface Resource {
|
|
2
|
+
App: {
|
|
3
|
+
name: string;
|
|
4
|
+
stage: string;
|
|
5
|
+
};
|
|
6
|
+
}
|
|
7
|
+
export declare function loadResourceEnvironment(input?: Record<string, any>): void;
|
|
8
|
+
export declare function loadResourceData(input?: Record<string, any>): void;
|
|
9
|
+
export declare function createResource<T extends Resource>(load: () => void): T;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const state = globalThis;
|
|
2
|
+
const raw = (state.__SST_RESOURCE_RAW__ ??= {
|
|
3
|
+
// @ts-expect-error
|
|
4
|
+
...globalThis.$SST_LINKS,
|
|
5
|
+
});
|
|
6
|
+
const environment = (state.__SST_RESOURCE_ENVIRONMENT__ ??= {});
|
|
7
|
+
export function loadResourceEnvironment(input) {
|
|
8
|
+
for (let [key, value] of Object.entries(input ?? {})) {
|
|
9
|
+
if (typeof value === "string") {
|
|
10
|
+
environment[key] = value;
|
|
11
|
+
if (!key.startsWith("SST_RESOURCE_") || !value) {
|
|
12
|
+
continue;
|
|
13
|
+
}
|
|
14
|
+
raw[key.slice("SST_RESOURCE_".length)] = JSON.parse(value);
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
raw[key] = value;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export function loadResourceData(input) {
|
|
21
|
+
Object.assign(raw, input ?? {});
|
|
22
|
+
}
|
|
23
|
+
export function createResource(load) {
|
|
24
|
+
let loaded = false;
|
|
25
|
+
const loadData = () => {
|
|
26
|
+
if (loaded)
|
|
27
|
+
return;
|
|
28
|
+
load();
|
|
29
|
+
loaded = true;
|
|
30
|
+
};
|
|
31
|
+
return new Proxy(raw, {
|
|
32
|
+
get(_target, prop) {
|
|
33
|
+
loadData();
|
|
34
|
+
if (prop in raw) {
|
|
35
|
+
return raw[prop];
|
|
36
|
+
}
|
|
37
|
+
if (typeof prop !== "string") {
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
if (!environment.SST_RESOURCE_App && !raw.App) {
|
|
41
|
+
throw new Error("It does not look like SST links are active. If this is in local development and you are not starting this process through the multiplexer, wrap your command with `sst dev -- <command>`");
|
|
42
|
+
}
|
|
43
|
+
let msg = `"${prop}" is not linked in your sst.config.ts`;
|
|
44
|
+
if (environment.AWS_LAMBDA_FUNCTION_NAME) {
|
|
45
|
+
msg += ` to ${environment.AWS_LAMBDA_FUNCTION_NAME}`;
|
|
46
|
+
}
|
|
47
|
+
throw new Error(msg);
|
|
48
|
+
},
|
|
49
|
+
has(_target, prop) {
|
|
50
|
+
loadData();
|
|
51
|
+
return prop in raw;
|
|
52
|
+
},
|
|
53
|
+
ownKeys() {
|
|
54
|
+
loadData();
|
|
55
|
+
return Reflect.ownKeys(raw);
|
|
56
|
+
},
|
|
57
|
+
getOwnPropertyDescriptor(_target, prop) {
|
|
58
|
+
loadData();
|
|
59
|
+
return Object.getOwnPropertyDescriptor(raw, prop);
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./resource/index.js";
|
package/dist/resource.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./resource/index.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { Resource } from "../resource/index.js";
|
|
2
|
+
export interface PutEvent {
|
|
3
|
+
/**
|
|
4
|
+
* The vector to store in the database.
|
|
5
|
+
* @example
|
|
6
|
+
* ```js
|
|
7
|
+
* {
|
|
8
|
+
* vector: [32.4, 6.55, 11.2, 10.3, 87.9]
|
|
9
|
+
* }
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
vector: number[];
|
|
13
|
+
/**
|
|
14
|
+
* Metadata for the event as JSON.
|
|
15
|
+
* This will be used to filter when querying and removing vectors.
|
|
16
|
+
* @example
|
|
17
|
+
* ```js
|
|
18
|
+
* {
|
|
19
|
+
* metadata: {
|
|
20
|
+
* type: "movie",
|
|
21
|
+
* id: "movie-123",
|
|
22
|
+
* name: "Spiderman"
|
|
23
|
+
* }
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
metadata: Record<string, any>;
|
|
28
|
+
}
|
|
29
|
+
export interface QueryEvent {
|
|
30
|
+
/**
|
|
31
|
+
* The vector used to query the database. When omitted, performs a metadata-only
|
|
32
|
+
* query and returns `score: 0` for each result.
|
|
33
|
+
* @example
|
|
34
|
+
* ```js
|
|
35
|
+
* {
|
|
36
|
+
* vector: [32.4, 6.55, 11.2, 10.3, 87.9]
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
vector?: number[];
|
|
41
|
+
/**
|
|
42
|
+
* The metadata used to filter the vectors.
|
|
43
|
+
* Only vectors that match the provided fields will be returned.
|
|
44
|
+
* @default `{}`
|
|
45
|
+
* @example
|
|
46
|
+
* Given this filter.
|
|
47
|
+
* ```js
|
|
48
|
+
* {
|
|
49
|
+
* include: {
|
|
50
|
+
* type: "movie",
|
|
51
|
+
* release: "2001"
|
|
52
|
+
* }
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
* It will match a vector with the metadata:
|
|
56
|
+
* ```js
|
|
57
|
+
* {
|
|
58
|
+
* type: "movie",
|
|
59
|
+
* name: "Spiderman",
|
|
60
|
+
* release: "2001"
|
|
61
|
+
* }
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* But not a vector with this metadata:
|
|
65
|
+
* ```js
|
|
66
|
+
* {
|
|
67
|
+
* type: "book",
|
|
68
|
+
* name: "Spiderman",
|
|
69
|
+
* release: "2001"
|
|
70
|
+
* }
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
include?: Record<string, any>;
|
|
74
|
+
/**
|
|
75
|
+
* Exclude vectors with metadata that match the provided fields.
|
|
76
|
+
* @example
|
|
77
|
+
* Given this filter.
|
|
78
|
+
* ```js
|
|
79
|
+
* {
|
|
80
|
+
* include: {
|
|
81
|
+
* type: "movie",
|
|
82
|
+
* release: "2001"
|
|
83
|
+
* },
|
|
84
|
+
* exclude: {
|
|
85
|
+
* name: "Spiderman"
|
|
86
|
+
* }
|
|
87
|
+
* }
|
|
88
|
+
* ```
|
|
89
|
+
* This will match a vector with metadata:
|
|
90
|
+
* ```js
|
|
91
|
+
* {
|
|
92
|
+
* type: "movie",
|
|
93
|
+
* name: "A Beautiful Mind",
|
|
94
|
+
* release: "2001"
|
|
95
|
+
* }
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* But not a vector with the metadata:
|
|
99
|
+
* ```js
|
|
100
|
+
* {
|
|
101
|
+
* type: "book",
|
|
102
|
+
* name: "Spiderman",
|
|
103
|
+
* release: "2001"
|
|
104
|
+
* }
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
exclude?: Record<string, any>;
|
|
108
|
+
/**
|
|
109
|
+
* The threshold of similarity between the prompt and the queried vectors.
|
|
110
|
+
* Only vectors with a similarity score higher than the threshold will be returned.
|
|
111
|
+
*
|
|
112
|
+
* This will return values is between 0 and 1.
|
|
113
|
+
* - `0` means the prompt and the queried vectors are completely different.
|
|
114
|
+
* - `1` means the prompt and the queried vectors are identical.
|
|
115
|
+
*
|
|
116
|
+
* @default `0`
|
|
117
|
+
* @example
|
|
118
|
+
* ```js
|
|
119
|
+
* {
|
|
120
|
+
* threshold: 0.5
|
|
121
|
+
* }
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
threshold?: number;
|
|
125
|
+
/**
|
|
126
|
+
* The number of results to return.
|
|
127
|
+
* @default `10`
|
|
128
|
+
* @example
|
|
129
|
+
* ```js
|
|
130
|
+
* {
|
|
131
|
+
* count: 10
|
|
132
|
+
* }
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
count?: number;
|
|
136
|
+
}
|
|
137
|
+
export interface RemoveEvent {
|
|
138
|
+
/**
|
|
139
|
+
* The metadata used to filter the removal of vectors.
|
|
140
|
+
* Only vectors with metadata that match the provided fields will be removed.
|
|
141
|
+
* @example
|
|
142
|
+
* To remove vectors for movie with id `movie-123`:
|
|
143
|
+
* ```js
|
|
144
|
+
* {
|
|
145
|
+
* include: {
|
|
146
|
+
* id: "movie-123",
|
|
147
|
+
* }
|
|
148
|
+
* }
|
|
149
|
+
* ```
|
|
150
|
+
* To remove vectors for all _movies_:
|
|
151
|
+
* ```js
|
|
152
|
+
* {
|
|
153
|
+
* include: {
|
|
154
|
+
* type: "movie",
|
|
155
|
+
* }
|
|
156
|
+
* }
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
include: Record<string, any>;
|
|
160
|
+
}
|
|
161
|
+
export interface QueryResponse {
|
|
162
|
+
/**
|
|
163
|
+
* List of results matching the query.
|
|
164
|
+
*/
|
|
165
|
+
results: {
|
|
166
|
+
/**
|
|
167
|
+
* Metadata for the event that was provided when storing the vector.
|
|
168
|
+
*/
|
|
169
|
+
metadata: Record<string, any>;
|
|
170
|
+
/**
|
|
171
|
+
* The similarity score between the prompt and the queried vector.
|
|
172
|
+
*/
|
|
173
|
+
score: number;
|
|
174
|
+
}[];
|
|
175
|
+
}
|
|
176
|
+
export interface VectorClientResponse {
|
|
177
|
+
/**
|
|
178
|
+
* Store a vector into the database.
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts title="src/lambda.ts"
|
|
181
|
+
* await client.put({
|
|
182
|
+
* vector: [32.4, 6.55, 11.2, 10.3, 87.9],
|
|
183
|
+
* metadata: { type: "movie", genre: "comedy" },
|
|
184
|
+
* });
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
put: (event: PutEvent) => Promise<void>;
|
|
188
|
+
/**
|
|
189
|
+
* Query vectors that are similar to the given vector.
|
|
190
|
+
* @example
|
|
191
|
+
* Query by vector similarity.
|
|
192
|
+
* ```ts title="src/lambda.ts"
|
|
193
|
+
* const result = await client.query({
|
|
194
|
+
* vector: [32.4, 6.55, 11.2, 10.3, 87.9],
|
|
195
|
+
* include: { type: "movie" },
|
|
196
|
+
* exclude: { genre: "thriller" },
|
|
197
|
+
* });
|
|
198
|
+
* ```
|
|
199
|
+
* Query by metadata only (no vector). Returns `score: 0` for each result.
|
|
200
|
+
* ```ts title="src/lambda.ts"
|
|
201
|
+
* const result = await client.query({
|
|
202
|
+
* include: { type: "movie" },
|
|
203
|
+
* });
|
|
204
|
+
* ```
|
|
205
|
+
*/
|
|
206
|
+
query: (event: QueryEvent) => Promise<QueryResponse>;
|
|
207
|
+
/**
|
|
208
|
+
* Remove vectors from the database.
|
|
209
|
+
* @example
|
|
210
|
+
* ```ts title="src/lambda.ts"
|
|
211
|
+
* await client.remove({
|
|
212
|
+
* include: { type: "movie" },
|
|
213
|
+
* });
|
|
214
|
+
* ```
|
|
215
|
+
*/
|
|
216
|
+
remove: (event: RemoveEvent) => Promise<void>;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Create a client to interact with the Vector database.
|
|
220
|
+
* @example
|
|
221
|
+
* ```ts title="src/lambda.ts"
|
|
222
|
+
* import { VectorClient } from "sst";
|
|
223
|
+
* const client = VectorClient("MyVectorDB");
|
|
224
|
+
* ```
|
|
225
|
+
*
|
|
226
|
+
* Store a vector into the db
|
|
227
|
+
*
|
|
228
|
+
* ```ts title="src/lambda.ts"
|
|
229
|
+
* await client.put({
|
|
230
|
+
* vector: [32.4, 6.55, 11.2, 10.3, 87.9],
|
|
231
|
+
* metadata: { type: "movie", genre: "comedy" },
|
|
232
|
+
* });
|
|
233
|
+
* ```
|
|
234
|
+
*
|
|
235
|
+
* Query vectors that are similar to the given vector
|
|
236
|
+
*
|
|
237
|
+
* ```ts title="src/lambda.ts"
|
|
238
|
+
* const result = await client.query({
|
|
239
|
+
* vector: [32.4, 6.55, 11.2, 10.3, 87.9],
|
|
240
|
+
* include: { type: "movie" },
|
|
241
|
+
* exclude: { genre: "thriller" },
|
|
242
|
+
* });
|
|
243
|
+
* ```
|
|
244
|
+
*/
|
|
245
|
+
export declare function VectorClient<T extends keyof {
|
|
246
|
+
[key in keyof Resource as "sst.aws.Vector" extends Resource[key]["type"] ? string extends key ? never : key : never]: Resource[key];
|
|
247
|
+
}>(name: T): VectorClientResponse;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { Resource } from "../resource/index.js";
|
|
2
|
+
import { client } from "../aws/client.js";
|
|
3
|
+
/**
|
|
4
|
+
* Create a client to interact with the Vector database.
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts title="src/lambda.ts"
|
|
7
|
+
* import { VectorClient } from "sst";
|
|
8
|
+
* const client = VectorClient("MyVectorDB");
|
|
9
|
+
* ```
|
|
10
|
+
*
|
|
11
|
+
* Store a vector into the db
|
|
12
|
+
*
|
|
13
|
+
* ```ts title="src/lambda.ts"
|
|
14
|
+
* await client.put({
|
|
15
|
+
* vector: [32.4, 6.55, 11.2, 10.3, 87.9],
|
|
16
|
+
* metadata: { type: "movie", genre: "comedy" },
|
|
17
|
+
* });
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* Query vectors that are similar to the given vector
|
|
21
|
+
*
|
|
22
|
+
* ```ts title="src/lambda.ts"
|
|
23
|
+
* const result = await client.query({
|
|
24
|
+
* vector: [32.4, 6.55, 11.2, 10.3, 87.9],
|
|
25
|
+
* include: { type: "movie" },
|
|
26
|
+
* exclude: { genre: "thriller" },
|
|
27
|
+
* });
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export function VectorClient(name) {
|
|
31
|
+
return {
|
|
32
|
+
put: async (event) => {
|
|
33
|
+
await invokeFunction(
|
|
34
|
+
// @ts-expect-error
|
|
35
|
+
Resource[name].putFunction, JSON.stringify(event), "Failed to store into the vector db");
|
|
36
|
+
},
|
|
37
|
+
query: async (event) => {
|
|
38
|
+
return await invokeFunction(
|
|
39
|
+
// @ts-expect-error
|
|
40
|
+
Resource[name].queryFunction, JSON.stringify(event), "Failed to query the vector db");
|
|
41
|
+
},
|
|
42
|
+
remove: async (event) => {
|
|
43
|
+
await invokeFunction(
|
|
44
|
+
// @ts-expect-error
|
|
45
|
+
Resource[name].removeFunction, JSON.stringify(event), "Failed to remove from the vector db");
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
async function invokeFunction(functionName, body, errorMessage, attempts = 0) {
|
|
50
|
+
try {
|
|
51
|
+
const c = await client();
|
|
52
|
+
const endpoint = `https://lambda.${process.env.AWS_REGION}.amazonaws.com/2015-03-31`;
|
|
53
|
+
const response = await c.fetch(`${endpoint}/functions/${functionName}/invocations`, {
|
|
54
|
+
method: "POST",
|
|
55
|
+
headers: { Accept: "application/json" },
|
|
56
|
+
body,
|
|
57
|
+
});
|
|
58
|
+
// success
|
|
59
|
+
if (response.status === 200 || response.status === 201) {
|
|
60
|
+
if (response.headers.get("content-length") === "0")
|
|
61
|
+
return undefined;
|
|
62
|
+
const text = await response.text();
|
|
63
|
+
try {
|
|
64
|
+
return JSON.parse(text);
|
|
65
|
+
}
|
|
66
|
+
catch (e) {
|
|
67
|
+
throw new Error(`Failed to parse JSON response: ${text}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
// error
|
|
71
|
+
const error = new Error();
|
|
72
|
+
const text = await response.text();
|
|
73
|
+
try {
|
|
74
|
+
const json = JSON.parse(text);
|
|
75
|
+
error.name = json.Error?.Code;
|
|
76
|
+
error.message = json.Error?.Message ?? json.message ?? text;
|
|
77
|
+
}
|
|
78
|
+
catch (e) {
|
|
79
|
+
error.message = text;
|
|
80
|
+
}
|
|
81
|
+
error.name = error.name ?? response.headers.get("x-amzn-ErrorType");
|
|
82
|
+
// @ts-expect-error
|
|
83
|
+
error.requestID = response.headers.get("x-amzn-RequestId");
|
|
84
|
+
// @ts-expect-error
|
|
85
|
+
error.statusCode = response.status;
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
88
|
+
catch (e) {
|
|
89
|
+
let isRetryable = false;
|
|
90
|
+
// AWS throttling errors => retry
|
|
91
|
+
if ([
|
|
92
|
+
"ThrottlingException",
|
|
93
|
+
"Throttling",
|
|
94
|
+
"TooManyRequestsException",
|
|
95
|
+
"OperationAbortedException",
|
|
96
|
+
"TimeoutError",
|
|
97
|
+
"NetworkingError",
|
|
98
|
+
].includes(e.name)) {
|
|
99
|
+
isRetryable = true;
|
|
100
|
+
}
|
|
101
|
+
if (!isRetryable)
|
|
102
|
+
throw e;
|
|
103
|
+
// retry
|
|
104
|
+
await new Promise((resolve) => setTimeout(resolve, 1.5 ** attempts * 100 * Math.random()));
|
|
105
|
+
return await invokeFunction(functionName, body, errorMessage, attempts + 1);
|
|
106
|
+
}
|
|
107
|
+
}
|
package/package.json
CHANGED
|
@@ -1 +1,73 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/package.json",
|
|
3
|
+
"name": "@sst-community/sst",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"version": "4.17.3",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/sst-community/sst.git"
|
|
11
|
+
},
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": "./dist/index.js",
|
|
15
|
+
"./resource": {
|
|
16
|
+
"types": "./dist/resource/index.d.ts",
|
|
17
|
+
"workerd": "./dist/resource/cloudflare.js",
|
|
18
|
+
"worker": "./dist/resource/cloudflare.js",
|
|
19
|
+
"default": "./dist/resource/node.js"
|
|
20
|
+
},
|
|
21
|
+
"./resource/cloudflare": {
|
|
22
|
+
"types": "./dist/resource/cloudflare.d.ts",
|
|
23
|
+
"default": "./dist/resource/cloudflare.js"
|
|
24
|
+
},
|
|
25
|
+
"./auth": "./dist/auth/index.js",
|
|
26
|
+
"./auth/adapter": "./dist/auth/adapter/index.js",
|
|
27
|
+
"./event": "./dist/event/index.js",
|
|
28
|
+
"./realtime": "./dist/realtime/index.js",
|
|
29
|
+
"./*": {
|
|
30
|
+
"import": "./dist/*.js",
|
|
31
|
+
"types": "./dist/*.d.ts"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsc",
|
|
36
|
+
"dev": "tsc -w",
|
|
37
|
+
"release": "./scripts/release.ts"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@aws/durable-execution-sdk-js-testing": "^1.1.1",
|
|
41
|
+
"@tsconfig/node20": "20.1.4",
|
|
42
|
+
"@types/aws-lambda": "^8.10.155",
|
|
43
|
+
"@types/node": "22.10.0",
|
|
44
|
+
"bun-types": "1.3.0",
|
|
45
|
+
"hono": "4.3.9",
|
|
46
|
+
"typescript": "5.7.2",
|
|
47
|
+
"valibot": "^1.0.0-rc.3",
|
|
48
|
+
"zod": "^3.24.2"
|
|
49
|
+
},
|
|
50
|
+
"files": [
|
|
51
|
+
"dist",
|
|
52
|
+
"bin"
|
|
53
|
+
],
|
|
54
|
+
"bin": {
|
|
55
|
+
"sst": "./bin/sst.mjs"
|
|
56
|
+
},
|
|
57
|
+
"optionalDependencies": {
|
|
58
|
+
"@sst-community/sst-linux-x64": "4.17.3",
|
|
59
|
+
"@sst-community/sst-linux-x86": "4.17.3",
|
|
60
|
+
"@sst-community/sst-linux-arm64": "4.17.3",
|
|
61
|
+
"@sst-community/sst-darwin-x64": "4.17.3",
|
|
62
|
+
"@sst-community/sst-darwin-arm64": "4.17.3",
|
|
63
|
+
"@sst-community/sst-win32-x64": "4.17.3",
|
|
64
|
+
"@sst-community/sst-win32-x86": "4.17.3",
|
|
65
|
+
"@sst-community/sst-win32-arm64": "4.17.3"
|
|
66
|
+
},
|
|
67
|
+
"dependencies": {
|
|
68
|
+
"@aws/durable-execution-sdk-js": "1.0.2",
|
|
69
|
+
"aws4fetch": "1.0.18",
|
|
70
|
+
"jose": "5.2.3",
|
|
71
|
+
"openid-client": "5.6.4"
|
|
72
|
+
}
|
|
73
|
+
}
|