weifuwu 0.22.1 → 0.22.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 +241 -1
- package/dist/auth.d.ts +14 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +662 -69
- package/dist/kb.d.ts +70 -0
- package/dist/oauth-client.d.ts +41 -0
- package/dist/s3.d.ts +68 -0
- package/dist/serve.d.ts +1 -1
- package/dist/session.d.ts +12 -0
- package/dist/test-utils.d.ts +10 -10
- package/package.json +3 -1
package/dist/kb.d.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { Sql } from './vendor.ts';
|
|
2
|
+
import type { Middleware } from './types.ts';
|
|
3
|
+
export interface KBOptions {
|
|
4
|
+
/** Postgres SQL client (with pgvector extension enabled). */
|
|
5
|
+
sql: Sql<{}>;
|
|
6
|
+
/**
|
|
7
|
+
* Embedding function.
|
|
8
|
+
* Takes a text string, returns a vector of numbers.
|
|
9
|
+
* Example: (text) => embed({ model: openai.embedding('text-embedding-3-small'), value: text }).then(r => r.embedding)
|
|
10
|
+
*/
|
|
11
|
+
embedding: (text: string) => Promise<number[]>;
|
|
12
|
+
/** Vector dimensions (default: 1536 for text-embedding-3-small). */
|
|
13
|
+
dimensions?: number;
|
|
14
|
+
/** Table name (default: '_kb_docs'). */
|
|
15
|
+
table?: string;
|
|
16
|
+
/** Default chunk size in characters (default: 512). */
|
|
17
|
+
chunkSize?: number;
|
|
18
|
+
/** Default chunk overlap in characters (default: 64). */
|
|
19
|
+
chunkOverlap?: number;
|
|
20
|
+
/** Default search limit (default: 5). */
|
|
21
|
+
searchLimit?: number;
|
|
22
|
+
/** Minimum similarity score threshold (0–1, default: 0). Set higher for stricter matches. */
|
|
23
|
+
searchThreshold?: number;
|
|
24
|
+
}
|
|
25
|
+
export interface KBIngestOptions {
|
|
26
|
+
title?: string;
|
|
27
|
+
metadata?: Record<string, unknown>;
|
|
28
|
+
chunkSize?: number;
|
|
29
|
+
chunkOverlap?: number;
|
|
30
|
+
}
|
|
31
|
+
export interface KBSearchResult {
|
|
32
|
+
id: number;
|
|
33
|
+
key: string;
|
|
34
|
+
title: string;
|
|
35
|
+
content: string;
|
|
36
|
+
score: number;
|
|
37
|
+
metadata: Record<string, unknown>;
|
|
38
|
+
}
|
|
39
|
+
export interface KBSearchOptions {
|
|
40
|
+
limit?: number;
|
|
41
|
+
/** Minimum cosine similarity score (0–1). Results below this are excluded. */
|
|
42
|
+
threshold?: number;
|
|
43
|
+
}
|
|
44
|
+
export interface KBListEntry {
|
|
45
|
+
key: string;
|
|
46
|
+
title: string;
|
|
47
|
+
chunks: number;
|
|
48
|
+
}
|
|
49
|
+
export interface KBModule {
|
|
50
|
+
/**
|
|
51
|
+
* Ingest a document: chunk → embed → store.
|
|
52
|
+
* If a document with the same key exists, it is replaced (delete + re-insert).
|
|
53
|
+
* Returns the number of chunks created.
|
|
54
|
+
*/
|
|
55
|
+
ingest(key: string, content: string, options?: KBIngestOptions): Promise<number>;
|
|
56
|
+
/**
|
|
57
|
+
* Search the knowledge base by semantic similarity.
|
|
58
|
+
* Query is embedded, then vector similarity search returns top results.
|
|
59
|
+
*/
|
|
60
|
+
search(query: string, searchOptions?: KBSearchOptions): Promise<KBSearchResult[]>;
|
|
61
|
+
/** Delete all chunks for a document key. */
|
|
62
|
+
delete(key: string): Promise<void>;
|
|
63
|
+
/** List all document keys with title and chunk count. */
|
|
64
|
+
list(): Promise<KBListEntry[]>;
|
|
65
|
+
/** Create the table and HNSW index. Safe to call multiple times. */
|
|
66
|
+
migrate(): Promise<void>;
|
|
67
|
+
/** Middleware that injects `ctx.kb` with `.search()` method. */
|
|
68
|
+
middleware(): Middleware;
|
|
69
|
+
}
|
|
70
|
+
export declare function knowledgeBase(options: KBOptions): KBModule;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { PostgresClient } from './postgres/types.ts';
|
|
2
|
+
import { Router } from './router.ts';
|
|
3
|
+
export interface OAuthProviderConfig {
|
|
4
|
+
clientId: string;
|
|
5
|
+
clientSecret: string;
|
|
6
|
+
scope?: string;
|
|
7
|
+
/** Custom auth URL (overrides built-in provider default). */
|
|
8
|
+
authUrl?: string;
|
|
9
|
+
/** Custom token URL (overrides built-in provider default). */
|
|
10
|
+
tokenUrl?: string;
|
|
11
|
+
/** Custom user info URL (overrides built-in provider default). */
|
|
12
|
+
userUrl?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Custom user parser.
|
|
15
|
+
* Required when any of authUrl/tokenUrl/userUrl is custom.
|
|
16
|
+
* Receives the raw response from userUrl + the access token.
|
|
17
|
+
*/
|
|
18
|
+
parseUser?: (data: any, accessToken: string) => ProviderUser;
|
|
19
|
+
}
|
|
20
|
+
export interface OAuthClientOptions {
|
|
21
|
+
/** Postgres client (required). */
|
|
22
|
+
pg: PostgresClient;
|
|
23
|
+
/** JWT secret — must match user() module's jwtSecret. */
|
|
24
|
+
jwtSecret: string;
|
|
25
|
+
/** JWT expiry (default: '24h'). */
|
|
26
|
+
expiresIn?: string | number;
|
|
27
|
+
/** Where to redirect after successful login (default: '/'). */
|
|
28
|
+
redirectUrl?: string;
|
|
29
|
+
/** Provider configurations. */
|
|
30
|
+
providers: Record<string, OAuthProviderConfig>;
|
|
31
|
+
/** Table name for provider-user links (default: '_auth_providers'). */
|
|
32
|
+
table?: string;
|
|
33
|
+
}
|
|
34
|
+
interface ProviderUser {
|
|
35
|
+
id: string;
|
|
36
|
+
email: string;
|
|
37
|
+
name: string;
|
|
38
|
+
avatarUrl?: string;
|
|
39
|
+
}
|
|
40
|
+
export declare function oauthClient(options: OAuthClientOptions): Router;
|
|
41
|
+
export {};
|
package/dist/s3.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { S3Client } from '@aws-sdk/client-s3';
|
|
2
|
+
import type { Middleware } from './types.ts';
|
|
3
|
+
import type { Readable } from 'node:stream';
|
|
4
|
+
declare module './types.ts' {
|
|
5
|
+
interface Context {
|
|
6
|
+
s3: S3Module;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export interface S3Options {
|
|
10
|
+
/** S3 bucket name. Required. */
|
|
11
|
+
bucket: string;
|
|
12
|
+
/** AWS region. Default: 'us-east-1'. */
|
|
13
|
+
region?: string;
|
|
14
|
+
/** Custom endpoint (MinIO, Cloudflare R2, Backblaze B2, etc.). */
|
|
15
|
+
endpoint?: string;
|
|
16
|
+
/** Force path-style addressing (required for MinIO, some private clouds). */
|
|
17
|
+
forcePathStyle?: boolean;
|
|
18
|
+
/** AWS credentials. Falls back to AWS env vars / IAM role if omitted. */
|
|
19
|
+
credentials?: {
|
|
20
|
+
accessKeyId: string;
|
|
21
|
+
secretAccessKey: string;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Base public URL for unsigned URLs.
|
|
25
|
+
* When set, `s3.url(key, { expiresIn: 0 })` returns a public URL.
|
|
26
|
+
* Example: 'https://cdn.example.com' or 'https://pub-xxx.r2.dev'
|
|
27
|
+
*/
|
|
28
|
+
publicUrl?: string;
|
|
29
|
+
}
|
|
30
|
+
export interface S3PutOptions {
|
|
31
|
+
/** Content-Type of the object. */
|
|
32
|
+
contentType?: string;
|
|
33
|
+
/** Cache-Control header value. */
|
|
34
|
+
cacheControl?: string;
|
|
35
|
+
/** User-defined metadata (prefix x-amz-meta-). */
|
|
36
|
+
metadata?: Record<string, string>;
|
|
37
|
+
}
|
|
38
|
+
export interface S3UrlOptions {
|
|
39
|
+
/**
|
|
40
|
+
* Signed URL expiry in seconds.
|
|
41
|
+
* Default: 3600 (1 hour).
|
|
42
|
+
* Set to 0 to return an unsigned public URL (requires `publicUrl` option).
|
|
43
|
+
*/
|
|
44
|
+
expiresIn?: number;
|
|
45
|
+
}
|
|
46
|
+
export interface S3Module {
|
|
47
|
+
/** Upload a file. Returns the key. */
|
|
48
|
+
put(key: string, body: S3Body, options?: S3PutOptions): Promise<string>;
|
|
49
|
+
/** Download a file. Returns the body as Buffer, or null if not found. */
|
|
50
|
+
get(key: string): Promise<Buffer | null>;
|
|
51
|
+
/** Delete a file. */
|
|
52
|
+
delete(key: string): Promise<void>;
|
|
53
|
+
/** Check if a file exists. */
|
|
54
|
+
exists(key: string): Promise<boolean>;
|
|
55
|
+
/**
|
|
56
|
+
* Generate a URL for an object.
|
|
57
|
+
* - If `expiresIn` > 0 (default): returns a signed URL with that expiry.
|
|
58
|
+
* - If `expiresIn` === 0 and `publicUrl` is configured: returns an unsigned
|
|
59
|
+
* public URL. Throws if `publicUrl` is not set.
|
|
60
|
+
*/
|
|
61
|
+
url(key: string, options?: S3UrlOptions): Promise<string>;
|
|
62
|
+
/** List object keys under a prefix. */
|
|
63
|
+
list(prefix?: string): Promise<string[]>;
|
|
64
|
+
/** The underlying S3Client (for advanced usage). */
|
|
65
|
+
client: S3Client;
|
|
66
|
+
}
|
|
67
|
+
export type S3Body = Buffer | Uint8Array | string | ReadableStream | Readable;
|
|
68
|
+
export declare function s3(options: S3Options): S3Module & Middleware;
|
package/dist/serve.d.ts
CHANGED
package/dist/session.d.ts
CHANGED
|
@@ -50,6 +50,18 @@ export interface SessionOptions {
|
|
|
50
50
|
secure?: boolean;
|
|
51
51
|
sameSite?: 'strict' | 'lax' | 'none';
|
|
52
52
|
};
|
|
53
|
+
/**
|
|
54
|
+
* Secret for signing the session cookie with HMAC-SHA256.
|
|
55
|
+
* When set, the cookie value becomes `sid.signature` — tampering is detected
|
|
56
|
+
* and rejected. Strongly recommended in production.
|
|
57
|
+
*/
|
|
58
|
+
secret?: string;
|
|
59
|
+
/**
|
|
60
|
+
* Interval (ms) for automatic session ID rotation.
|
|
61
|
+
* Rotating the ID mitigates session fixation attacks.
|
|
62
|
+
* Default: 900_000 (15 min). Set to 0 to disable.
|
|
63
|
+
*/
|
|
64
|
+
rotateInterval?: number;
|
|
53
65
|
}
|
|
54
66
|
export interface SessionInjected {
|
|
55
67
|
session: Session;
|
package/dist/test-utils.d.ts
CHANGED
|
@@ -38,16 +38,16 @@ export declare class TestApp {
|
|
|
38
38
|
constructor();
|
|
39
39
|
/** Add global middleware */
|
|
40
40
|
use(mw: any): this;
|
|
41
|
-
/** Register a GET route */
|
|
42
|
-
get(path: string,
|
|
43
|
-
/** Register a POST route */
|
|
44
|
-
post(path: string,
|
|
45
|
-
/** Register a PUT route */
|
|
46
|
-
put(path: string,
|
|
47
|
-
/** Register a PATCH route */
|
|
48
|
-
patch(path: string,
|
|
49
|
-
/** Register a DELETE route */
|
|
50
|
-
delete(path: string,
|
|
41
|
+
/** Register a GET route — supports route-level middleware via spread args. */
|
|
42
|
+
get(path: string, ...args: any[]): this;
|
|
43
|
+
/** Register a POST route. */
|
|
44
|
+
post(path: string, ...args: any[]): this;
|
|
45
|
+
/** Register a PUT route. */
|
|
46
|
+
put(path: string, ...args: any[]): this;
|
|
47
|
+
/** Register a PATCH route. */
|
|
48
|
+
patch(path: string, ...args: any[]): this;
|
|
49
|
+
/** Register a DELETE route. */
|
|
50
|
+
delete(path: string, ...args: any[]): this;
|
|
51
51
|
/** Start building a GET request */
|
|
52
52
|
getReq(path: string): TestRequest;
|
|
53
53
|
/** Start building a POST request */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "weifuwu",
|
|
3
|
-
"version": "0.22.
|
|
3
|
+
"version": "0.22.3",
|
|
4
4
|
"description": "Web-standard HTTP framework for Node.js — (req, ctx) => Response",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -28,6 +28,8 @@
|
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"@ai-sdk/openai": "^3.0.66",
|
|
31
|
+
"@aws-sdk/client-s3": "^3.1068.0",
|
|
32
|
+
"@aws-sdk/s3-request-presigner": "^3.1068.0",
|
|
31
33
|
"@graphql-tools/schema": "^10",
|
|
32
34
|
"@tailwindcss/postcss": "^4",
|
|
33
35
|
"ai": "^6",
|