weifuwu 0.3.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.
- package/README.md +399 -226
- package/dist/ai.d.ts +7 -0
- package/dist/graphql.d.ts +12 -0
- package/dist/index.d.ts +9 -1
- package/dist/index.js +900 -139
- package/dist/postgres/client.d.ts +2 -0
- package/dist/postgres/index.d.ts +2 -0
- package/dist/postgres/migrate.d.ts +3 -0
- package/dist/postgres/table.d.ts +4 -0
- package/dist/postgres/types.d.ts +51 -0
- package/dist/router.d.ts +0 -16
- package/dist/workflow/engine.d.ts +7 -0
- package/dist/workflow/index.d.ts +8 -0
- package/dist/workflow/llm.d.ts +10 -0
- package/dist/workflow/nodes.d.ts +10 -0
- package/dist/workflow/reference.d.ts +3 -0
- package/dist/workflow/route.d.ts +11 -0
- package/dist/workflow/sse.d.ts +2 -0
- package/dist/workflow/tool.d.ts +8 -0
- package/dist/workflow/types.d.ts +86 -0
- package/package.json +3 -2
|
@@ -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,27 +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 } from 'ai';
|
|
6
4
|
import type { Context, Handler, Middleware, ErrorHandler } from './types.ts';
|
|
7
|
-
type StreamTextParams = Parameters<typeof streamText>[0];
|
|
8
5
|
export type WebSocketHandler = {
|
|
9
6
|
open?: (ws: WebSocket, ctx: Context) => void | Promise<void>;
|
|
10
7
|
message?: (ws: WebSocket, ctx: Context, data: string | Buffer) => void | Promise<void>;
|
|
11
8
|
close?: (ws: WebSocket, ctx: Context) => void | Promise<void>;
|
|
12
9
|
error?: (ws: WebSocket, ctx: Context, error: Error) => void | Promise<void>;
|
|
13
10
|
};
|
|
14
|
-
export type AIOptions = Omit<StreamTextParams, 'model'> & {
|
|
15
|
-
model: string | (() => any);
|
|
16
|
-
};
|
|
17
|
-
export type AIHandler = (req: Request, ctx: Context) => AIOptions | Promise<AIOptions>;
|
|
18
|
-
export type GraphQLOptions = {
|
|
19
|
-
schema: string | GraphQLSchema;
|
|
20
|
-
rootValue?: any;
|
|
21
|
-
resolvers?: any;
|
|
22
|
-
context?: (req: Request, ctx: Context) => Record<string, any> | Promise<Record<string, any>>;
|
|
23
|
-
graphiql?: boolean;
|
|
24
|
-
};
|
|
25
11
|
type WsUpgradeHandler = (req: IncomingMessage, socket: Duplex, head: Buffer) => void;
|
|
26
12
|
export declare class Router {
|
|
27
13
|
private root;
|
|
@@ -42,8 +28,6 @@ export declare class Router {
|
|
|
42
28
|
onError(handler: ErrorHandler): this;
|
|
43
29
|
route(method: string, path: string, ...args: [...Middleware[], Handler]): this;
|
|
44
30
|
ws(path: string, ...args: [...Middleware[], WebSocketHandler]): this;
|
|
45
|
-
graphql(path: string, ...args: [...Middleware[], GraphQLOptions]): this;
|
|
46
|
-
ai(path: string, ...args: [...Middleware[], AIHandler]): this;
|
|
47
31
|
handler(): Handler;
|
|
48
32
|
websocketHandler(): WsUpgradeHandler;
|
|
49
33
|
private splitPath;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Tool, WorkflowEngine, SSEManager } from './types.ts';
|
|
2
|
+
import { type LanguageModel } from 'ai';
|
|
3
|
+
export declare function createWorkflowEngine(options: {
|
|
4
|
+
tools: Record<string, Tool>;
|
|
5
|
+
sseManager?: SSEManager;
|
|
6
|
+
model?: LanguageModel;
|
|
7
|
+
}): WorkflowEngine;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { tool } from './tool.ts';
|
|
2
|
+
export { createWorkflowEngine } from './engine.ts';
|
|
3
|
+
export { createSSEManager } from './sse.ts';
|
|
4
|
+
export { resolveRef, resolveValue } from './reference.ts';
|
|
5
|
+
export { generateWorkflow } from './llm.ts';
|
|
6
|
+
export { workflow } from './route.ts';
|
|
7
|
+
export type { WorkflowOptions, WorkflowHandler } from './route.ts';
|
|
8
|
+
export type { Tool, ToolContext, Workflow, Node, WorkflowState, SSEEvent, SSEManager, WorkflowEngine, ExecuteOptions, StreamEvent, Condition, SubWorkflow } from './types.ts';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Tool, Workflow } from './types.ts';
|
|
2
|
+
export declare function generateWorkflow(goal: string, tools: Record<string, Tool>, generateFn: (prompt: {
|
|
3
|
+
system: string;
|
|
4
|
+
messages: {
|
|
5
|
+
role: string;
|
|
6
|
+
content: string;
|
|
7
|
+
}[];
|
|
8
|
+
}) => Promise<{
|
|
9
|
+
text: string;
|
|
10
|
+
}>): Promise<Workflow>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Node, WorkflowContext } from './types.ts';
|
|
2
|
+
export type NodeExecutor = (node: Node, ctx: WorkflowContext) => Promise<unknown>;
|
|
3
|
+
export declare function executeEval(node: Node, ctx: WorkflowContext): Promise<unknown>;
|
|
4
|
+
export declare function executeSet(node: Node, ctx: WorkflowContext): Promise<unknown>;
|
|
5
|
+
export declare function executeGet(node: Node, ctx: WorkflowContext): Promise<unknown>;
|
|
6
|
+
export declare function executeIf(node: Node, ctx: WorkflowContext): Promise<unknown>;
|
|
7
|
+
export declare function executeWhile(node: Node, ctx: WorkflowContext): Promise<unknown>;
|
|
8
|
+
export declare function executeCall(node: Node, ctx: WorkflowContext): Promise<unknown>;
|
|
9
|
+
export declare function executeHttp(node: Node, ctx: WorkflowContext): Promise<unknown>;
|
|
10
|
+
export declare function executeNode(node: Node, ctx: WorkflowContext): Promise<unknown>;
|
|
@@ -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;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
import type { Tool, ToolContext } from './types.ts';
|
|
3
|
+
export declare function tool<TInput = unknown, TOutput = unknown>(def: {
|
|
4
|
+
name?: string;
|
|
5
|
+
description: string;
|
|
6
|
+
inputSchema: z.ZodSchema<TInput>;
|
|
7
|
+
execute: (input: TInput, ctx: ToolContext) => Promise<TOutput>;
|
|
8
|
+
}): Tool<TInput, TOutput>;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
export type NodeType = 'eval' | 'set' | 'get' | 'if' | 'while' | 'call' | 'http';
|
|
3
|
+
export interface Tool<TInput = unknown, TOutput = unknown> {
|
|
4
|
+
name: string;
|
|
5
|
+
description: string;
|
|
6
|
+
inputSchema: z.ZodSchema<TInput>;
|
|
7
|
+
execute: (input: TInput, ctx: ToolContext) => Promise<TOutput>;
|
|
8
|
+
}
|
|
9
|
+
export interface ToolContext {
|
|
10
|
+
workflowId?: string;
|
|
11
|
+
nodeId: string;
|
|
12
|
+
onStream?: (event: StreamEvent) => Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
export interface StreamEvent {
|
|
15
|
+
type: string;
|
|
16
|
+
chunk?: string;
|
|
17
|
+
accumulated?: string;
|
|
18
|
+
[key: string]: unknown;
|
|
19
|
+
}
|
|
20
|
+
export interface Node {
|
|
21
|
+
id: string;
|
|
22
|
+
tool: NodeType | string;
|
|
23
|
+
input: Record<string, unknown>;
|
|
24
|
+
conditions?: Condition[];
|
|
25
|
+
body?: Node[];
|
|
26
|
+
}
|
|
27
|
+
export interface Condition {
|
|
28
|
+
test: string | boolean;
|
|
29
|
+
body: Node[];
|
|
30
|
+
}
|
|
31
|
+
export interface Workflow {
|
|
32
|
+
name?: string;
|
|
33
|
+
nodes: Node[];
|
|
34
|
+
functions?: Record<string, SubWorkflow>;
|
|
35
|
+
}
|
|
36
|
+
export interface SubWorkflow {
|
|
37
|
+
inputSchema: Record<string, unknown>;
|
|
38
|
+
workflow: {
|
|
39
|
+
nodes: Node[];
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export interface WorkflowState {
|
|
43
|
+
workflowId: string;
|
|
44
|
+
status: 'running' | 'completed' | 'error';
|
|
45
|
+
goal: string;
|
|
46
|
+
result?: unknown;
|
|
47
|
+
error?: string;
|
|
48
|
+
startTime: number;
|
|
49
|
+
endTime?: number;
|
|
50
|
+
}
|
|
51
|
+
export interface SSEEvent {
|
|
52
|
+
event: string;
|
|
53
|
+
data: unknown;
|
|
54
|
+
}
|
|
55
|
+
export interface ExecuteOptions {
|
|
56
|
+
initialInput?: Record<string, unknown>;
|
|
57
|
+
maxSteps?: number;
|
|
58
|
+
}
|
|
59
|
+
export interface WorkflowContext {
|
|
60
|
+
variables: Map<string, unknown>;
|
|
61
|
+
nodeOutputs: Map<string, unknown>;
|
|
62
|
+
functions: Record<string, SubWorkflow>;
|
|
63
|
+
stepCount: number;
|
|
64
|
+
maxSteps: number;
|
|
65
|
+
input: Record<string, unknown>;
|
|
66
|
+
toolRegistry: Map<string, Tool>;
|
|
67
|
+
sseManager?: SSEManager;
|
|
68
|
+
workflowId?: string;
|
|
69
|
+
onNodeEvent?: (event: SSEEvent) => void;
|
|
70
|
+
}
|
|
71
|
+
export interface SSEManager {
|
|
72
|
+
createStream: (workflowId: string) => ReadableStream<Uint8Array>;
|
|
73
|
+
send: (workflowId: string, event: SSEEvent) => void;
|
|
74
|
+
close: (workflowId: string) => void;
|
|
75
|
+
}
|
|
76
|
+
export interface EngineOptions {
|
|
77
|
+
tools: Record<string, Tool>;
|
|
78
|
+
model?: unknown;
|
|
79
|
+
sseManager?: SSEManager;
|
|
80
|
+
}
|
|
81
|
+
export interface WorkflowEngine {
|
|
82
|
+
execute: (workflow: Workflow, options?: ExecuteOptions) => Promise<unknown>;
|
|
83
|
+
getState: (workflowId: string) => WorkflowState | undefined;
|
|
84
|
+
runAsync: (workflowId: string, workflow: Workflow, options?: ExecuteOptions) => Promise<void>;
|
|
85
|
+
generateWorkflow: (goal: string) => Promise<Workflow>;
|
|
86
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "weifuwu",
|
|
3
|
-
"version": "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",
|