weifuwu 0.4.0 → 0.5.0

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.
@@ -0,0 +1,2 @@
1
+ import type { PostgresOptions, PostgresClient } from './types.ts';
2
+ export declare function postgres(opts?: string | PostgresOptions): PostgresClient;
@@ -0,0 +1,2 @@
1
+ export { postgres } from './client.ts';
2
+ export type { PostgresOptions, PostgresClient, TableProxy, ListOptions, TableBuilder } from './types.ts';
@@ -0,0 +1,3 @@
1
+ import type { Sql } from 'postgres';
2
+ import type { TableDef } from './types.ts';
3
+ export declare function runMigrations(sql: Sql<{}>, tables: TableDef[]): Promise<void>;
@@ -0,0 +1,4 @@
1
+ import type { Sql } from 'postgres';
2
+ import { z } from 'zod';
3
+ import type { TableDef, TableProxy } from './types.ts';
4
+ export declare function buildTable(sql: Sql<{}>, tables: TableDef[]): <T extends Record<string, z.ZodTypeAny>>(name: string, schema: T) => TableProxy<z.output<z.ZodObject<T>>, z.input<z.ZodObject<T>>>;
@@ -0,0 +1,51 @@
1
+ import type { Sql } from 'postgres';
2
+ import type { z } from 'zod';
3
+ import type { Context, Handler } from '../types.ts';
4
+ declare module '../types.ts' {
5
+ interface Context {
6
+ sql?: Sql<{}>;
7
+ }
8
+ }
9
+ export interface PostgresOptions {
10
+ connection?: string | Record<string, unknown>;
11
+ signal?: AbortSignal;
12
+ }
13
+ export interface PostgresClient {
14
+ (req: Request, ctx: Context, next: Handler): Response | Promise<Response>;
15
+ sql: Sql<{}>;
16
+ table: TableBuilder;
17
+ migrate: () => Promise<void>;
18
+ close: () => Promise<void>;
19
+ }
20
+ export type TableBuilder = <T extends Record<string, z.ZodTypeAny>>(name: string, schema: T, opts?: {
21
+ primaryKey?: string;
22
+ }) => TableProxy<z.output<z.ZodObject<T>>, z.input<z.ZodObject<T>>>;
23
+ export interface TableProxy<TRow = unknown, TInsert = unknown> {
24
+ $type: TRow;
25
+ $insert: TInsert;
26
+ get: (id: number | string) => Promise<TRow | undefined>;
27
+ list: (filter?: Record<string, unknown>, opts?: ListOptions) => Promise<{
28
+ rows: TRow[];
29
+ count: number;
30
+ }>;
31
+ create: (data: TInsert) => Promise<TRow>;
32
+ patch: (id: number | string, data: Partial<TInsert>) => Promise<TRow | undefined>;
33
+ remove: (id: number | string) => Promise<boolean>;
34
+ }
35
+ export interface ListOptions {
36
+ limit?: number;
37
+ offset?: number;
38
+ sort?: Record<string, 'asc' | 'desc'>;
39
+ }
40
+ export interface ColumnDef {
41
+ name: string;
42
+ sqlType: string;
43
+ nullable: boolean;
44
+ isPrimaryKey: boolean;
45
+ defaultExpr: string | null;
46
+ autoGenerate: boolean;
47
+ }
48
+ export interface TableDef {
49
+ name: string;
50
+ columns: ColumnDef[];
51
+ }
package/dist/router.d.ts CHANGED
@@ -1,28 +1,13 @@
1
1
  import { type WebSocket } from 'ws';
2
2
  import type { IncomingMessage } from 'node:http';
3
3
  import type { Duplex } from 'node:stream';
4
- import { type GraphQLSchema } from 'graphql';
5
- import { streamText, generateText } from 'ai';
6
4
  import type { Context, Handler, Middleware, ErrorHandler } from './types.ts';
7
- import type { Tool as WfTool } from './workflow/types.ts';
8
- type StreamTextParams = Parameters<typeof streamText>[0];
9
5
  export type WebSocketHandler = {
10
6
  open?: (ws: WebSocket, ctx: Context) => void | Promise<void>;
11
7
  message?: (ws: WebSocket, ctx: Context, data: string | Buffer) => void | Promise<void>;
12
8
  close?: (ws: WebSocket, ctx: Context) => void | Promise<void>;
13
9
  error?: (ws: WebSocket, ctx: Context, error: Error) => void | Promise<void>;
14
10
  };
15
- export type AIOptions = Omit<StreamTextParams, 'model'> & {
16
- model: string | (() => any);
17
- };
18
- export type AIHandler = (req: Request, ctx: Context) => AIOptions | Promise<AIOptions>;
19
- export type GraphQLOptions = {
20
- schema: string | GraphQLSchema;
21
- rootValue?: any;
22
- resolvers?: any;
23
- context?: (req: Request, ctx: Context) => Record<string, any> | Promise<Record<string, any>>;
24
- graphiql?: boolean;
25
- };
26
11
  type WsUpgradeHandler = (req: IncomingMessage, socket: Duplex, head: Buffer) => void;
27
12
  export declare class Router {
28
13
  private root;
@@ -43,13 +28,6 @@ export declare class Router {
43
28
  onError(handler: ErrorHandler): this;
44
29
  route(method: string, path: string, ...args: [...Middleware[], Handler]): this;
45
30
  ws(path: string, ...args: [...Middleware[], WebSocketHandler]): this;
46
- graphql(path: string, ...args: [...Middleware[], GraphQLOptions]): this;
47
- ai(path: string, ...args: [...Middleware[], AIHandler]): this;
48
- workflow(path: string, options: {
49
- tools: Record<string, WfTool>;
50
- model?: Parameters<typeof generateText>[0]['model'];
51
- stream?: boolean;
52
- }): this;
53
31
  handler(): Handler;
54
32
  websocketHandler(): WsUpgradeHandler;
55
33
  private splitPath;
@@ -3,4 +3,6 @@ export { createWorkflowEngine } from './engine.ts';
3
3
  export { createSSEManager } from './sse.ts';
4
4
  export { resolveRef, resolveValue } from './reference.ts';
5
5
  export { generateWorkflow } from './llm.ts';
6
+ export { workflow } from './route.ts';
7
+ export type { WorkflowOptions, WorkflowHandler } from './route.ts';
6
8
  export type { Tool, ToolContext, Workflow, Node, WorkflowState, SSEEvent, SSEManager, WorkflowEngine, ExecuteOptions, StreamEvent, Condition, SubWorkflow } from './types.ts';
@@ -0,0 +1,11 @@
1
+ import { Router } from '../router.ts';
2
+ import type { Tool as WfTool } from './types.ts';
3
+ import type { generateText } from 'ai';
4
+ export interface WorkflowOptions {
5
+ tools: Record<string, WfTool>;
6
+ model?: Parameters<typeof generateText>[0]['model'];
7
+ stream?: boolean;
8
+ }
9
+ export type WorkflowHandler = (req: Request, ctx: Context) => WorkflowOptions | Promise<WorkflowOptions>;
10
+ import type { Context } from '../types.ts';
11
+ export declare function workflow(handler: WorkflowHandler): Router;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "weifuwu",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
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",
@@ -14,7 +14,7 @@
14
14
  "LICENSE"
15
15
  ],
16
16
  "scripts": {
17
- "build": "esbuild index.ts --bundle --format=esm --platform=node --outfile=dist/index.js --external:react --external:react-dom --external:esbuild --external:graphql --external:ws --external:zod --external:@graphql-tools/schema --external:ai",
17
+ "build": "esbuild index.ts --bundle --format=esm --platform=node --outfile=dist/index.js --external:react --external:react-dom --external:esbuild --external:graphql --external:ws --external:zod --external:@graphql-tools/schema --external:ai --external:postgres",
18
18
  "prepublishOnly": "npm run build && tsc --emitDeclarationOnly --outdir dist",
19
19
  "test": "node --test 'test/**/*.test.ts'"
20
20
  },
@@ -23,6 +23,7 @@
23
23
  "ai": "^6",
24
24
  "esbuild": "^0.28.0",
25
25
  "graphql": "^16",
26
+ "postgres": "^3.4.9",
26
27
  "react": "^19",
27
28
  "react-dom": "^19",
28
29
  "ws": "^8",