@open-wa/api 5.0.0 → 5.1.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/dashboard-neo/dist/assets/{AgentScalarDrawer.vue-BGBDvSQB.js → AgentScalarDrawer.vue-Bq0juj-E.js} +3 -3
- package/dashboard-neo/dist/assets/{index-rjufqWkX.js → index-DO9OELB4.js} +101 -101
- package/dashboard-neo/dist/assets/{modal-CKSoZwLD.js → modal-74dUeivw.js} +1 -1
- package/dashboard-neo/dist/index.html +1 -1
- package/dist/index.cjs +9 -4
- package/package.json +5 -4
- package/src/auth/api-key.ts +28 -0
- package/src/compat/args.ts +1 -0
- package/src/compat/deprecation.ts +35 -0
- package/src/compat/session-path.ts +19 -0
- package/src/createApiMiddleware.ts +184 -0
- package/src/createApiServer.ts +466 -0
- package/src/dashboard/middleware.ts +94 -0
- package/src/docs/openapi.ts +77 -0
- package/src/docs/postman.ts +42 -0
- package/src/events/EventBridge.ts +4 -0
- package/src/events/EventBroadcaster.ts +126 -0
- package/src/execution/kernel.ts +1 -0
- package/src/health/HealthStore.ts +252 -0
- package/src/index.ts +9 -0
- package/src/invoke-client-method.ts +1 -0
- package/src/middleware/rate-limit.ts +30 -0
- package/src/monitoring/elastic.ts +104 -0
- package/src/routes/agent-discovery.ts +150 -0
- package/src/routes/debug.ts +99 -0
- package/src/routes/meta.ts +512 -0
- package/src/types.ts +31 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-wa/api",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.1.0",
|
|
4
4
|
"description": "Reusable Easy API transport and runtime explorer layer for open-wa",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"types": "./dist/index.d.cts",
|
|
@@ -12,15 +12,16 @@
|
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
14
|
"files": [
|
|
15
|
+
"src",
|
|
15
16
|
"dist",
|
|
16
17
|
"dashboard-neo/dist"
|
|
17
18
|
],
|
|
18
19
|
"dependencies": {
|
|
19
20
|
"@elastic/elasticsearch": "^9.5.1",
|
|
20
21
|
"@hono/node-server": "^2.1.1",
|
|
21
|
-
"@open-wa/mcp": "5.
|
|
22
|
-
"@open-wa/schema": "5.
|
|
23
|
-
"@open-wa/screencaster": "5.
|
|
22
|
+
"@open-wa/mcp": "5.1.0",
|
|
23
|
+
"@open-wa/schema": "5.1.0",
|
|
24
|
+
"@open-wa/screencaster": "5.1.0",
|
|
24
25
|
"hono": "^4.13.9",
|
|
25
26
|
"tree-kill": "^1.2.2",
|
|
26
27
|
"ws": "^8.22.0"
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Context, Next } from 'hono';
|
|
2
|
+
import { applyDeprecationHeaders } from '../compat/deprecation';
|
|
3
|
+
|
|
4
|
+
export function apiKeyMiddleware(apiKey: string) {
|
|
5
|
+
return async (c: Context, next: Next) => {
|
|
6
|
+
const queryKey = c.req.query('api_key') || c.req.query('key');
|
|
7
|
+
const headerKey =
|
|
8
|
+
c.req.header('X-API-Key') ||
|
|
9
|
+
c.req.header('api_key') ||
|
|
10
|
+
c.req.header('key');
|
|
11
|
+
|
|
12
|
+
const resolvedKey = queryKey || headerKey;
|
|
13
|
+
|
|
14
|
+
if (c.req.query('api_key') || c.req.query('key') || c.req.header('api_key') || c.req.header('key')) {
|
|
15
|
+
applyDeprecationHeaders(c, {
|
|
16
|
+
route: 'legacy-api-key-alias',
|
|
17
|
+
message: 'Legacy API key aliases are deprecated.',
|
|
18
|
+
replacement: 'X-API-Key header',
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (!resolvedKey || resolvedKey !== apiKey) {
|
|
23
|
+
return c.json({ error: 'Unauthorized', details: 'Invalid or missing API key' }, 401);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return await next();
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { normalizeMethodPayload } from '@open-wa/mcp';
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export interface DeprecationNotice {
|
|
2
|
+
route: string;
|
|
3
|
+
message: string;
|
|
4
|
+
replacement?: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function applyDeprecationHeaders(c: any, notice: DeprecationNotice) {
|
|
8
|
+
const parts = [notice.message];
|
|
9
|
+
|
|
10
|
+
if (notice.replacement) {
|
|
11
|
+
parts.push(`Use ${notice.replacement} instead.`);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
c.header('X-OpenWA-Deprecated', notice.route);
|
|
15
|
+
c.header('Warning', `299 open-wa "${parts.join(' ')}"`);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function deprecatedJson(
|
|
19
|
+
c: any,
|
|
20
|
+
notice: DeprecationNotice,
|
|
21
|
+
status = 200,
|
|
22
|
+
extra: Record<string, unknown> = {}
|
|
23
|
+
) {
|
|
24
|
+
applyDeprecationHeaders(c, notice);
|
|
25
|
+
return c.json(
|
|
26
|
+
{
|
|
27
|
+
deprecated: true,
|
|
28
|
+
route: notice.route,
|
|
29
|
+
message: notice.message,
|
|
30
|
+
replacement: notice.replacement,
|
|
31
|
+
...extra,
|
|
32
|
+
},
|
|
33
|
+
status as any
|
|
34
|
+
);
|
|
35
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export function assertSessionPath(
|
|
2
|
+
expectedSessionId: string,
|
|
3
|
+
requestedSessionId: string | undefined,
|
|
4
|
+
useSessionIdInPath: boolean
|
|
5
|
+
): { ok: true } | { ok: false; status: number; error: string } {
|
|
6
|
+
if (!useSessionIdInPath) {
|
|
7
|
+
return { ok: true };
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
if (!requestedSessionId) {
|
|
11
|
+
return { ok: false, status: 404, error: 'Session id missing from request path' };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (requestedSessionId !== expectedSessionId) {
|
|
15
|
+
return { ok: false, status: 404, error: `Unknown session path: ${requestedSessionId}` };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return { ok: true };
|
|
19
|
+
}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { Hono } from 'hono';
|
|
2
|
+
import { getHttpMethodDefinitions, type HttpMethodDefinition } from '@open-wa/schema';
|
|
3
|
+
import '@open-wa/schema/methods';
|
|
4
|
+
import { apiKeyMiddleware } from './auth/api-key';
|
|
5
|
+
import { rateLimitMiddleware } from './middleware/rate-limit';
|
|
6
|
+
import { normalizeMethodPayload } from './compat/args';
|
|
7
|
+
import { assertSessionPath } from './compat/session-path';
|
|
8
|
+
import { invokeClientMethod } from './invoke-client-method';
|
|
9
|
+
import { executeCapability } from './execution/kernel';
|
|
10
|
+
import type { ApiMiddlewareOptions, ClientMethodMap, ClientSource } from './types';
|
|
11
|
+
|
|
12
|
+
function resolveClient(clientSource: ClientSource): ClientMethodMap | undefined {
|
|
13
|
+
return typeof clientSource === 'function' ? clientSource() : clientSource;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async function readRequestBody(c: any): Promise<Record<string, unknown>> {
|
|
17
|
+
try {
|
|
18
|
+
return await c.req.json();
|
|
19
|
+
} catch {
|
|
20
|
+
return {};
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function readQueryParams(c: any): Record<string, unknown> {
|
|
25
|
+
const url = new URL(c.req.url);
|
|
26
|
+
return Object.fromEntries(url.searchParams.entries());
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function toLocalPath(path: string, basePath: string): string {
|
|
30
|
+
const normalizedBase = basePath === '/' ? '' : basePath.replace(/\/+$/, '');
|
|
31
|
+
if (!normalizedBase) {
|
|
32
|
+
return path || '/';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (!path.startsWith(normalizedBase)) {
|
|
36
|
+
return path || '/';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const stripped = path.slice(normalizedBase.length);
|
|
40
|
+
return stripped.startsWith('/') ? stripped : `/${stripped}`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function createMethodMap(methodDefinitions: HttpMethodDefinition[]) {
|
|
44
|
+
const entries: Array<[string, HttpMethodDefinition]> = [];
|
|
45
|
+
for (const definition of methodDefinitions) {
|
|
46
|
+
for (const name of definition.invocationNames) {
|
|
47
|
+
entries.push([name, definition]);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return new Map(entries);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function shouldBlockForLifecycle(options: ApiMiddlewareOptions): boolean {
|
|
55
|
+
if (!options.isSessionConnected) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (options.config.apiLifecycle === 'immediate') {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return !options.isSessionConnected();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function createApiMiddleware(clientSource: ClientSource, options: ApiMiddlewareOptions) {
|
|
67
|
+
const app = new Hono();
|
|
68
|
+
const basePath = options.basePath || '/api';
|
|
69
|
+
const methodDefinitions = options.methodDefinitions || getHttpMethodDefinitions(basePath);
|
|
70
|
+
const methods = createMethodMap(methodDefinitions);
|
|
71
|
+
const useSessionIdInPath = options.useSessionIdInPath || false;
|
|
72
|
+
|
|
73
|
+
app.use('/*', rateLimitMiddleware(100, 60000));
|
|
74
|
+
|
|
75
|
+
if (options.config.apiKey) {
|
|
76
|
+
app.use('/*', apiKeyMiddleware(options.config.apiKey));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const handleInvocation = async (
|
|
80
|
+
c: any,
|
|
81
|
+
routeMethod: string | undefined,
|
|
82
|
+
routeSessionId?: string,
|
|
83
|
+
routeDefinition?: HttpMethodDefinition,
|
|
84
|
+
) => {
|
|
85
|
+
if (shouldBlockForLifecycle(options)) {
|
|
86
|
+
return c.json({ error: 'API not available until the session is truly ready', status: 503 }, 503);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const sessionCheck = assertSessionPath(options.config.sessionId, routeSessionId, useSessionIdInPath);
|
|
90
|
+
|
|
91
|
+
if (!sessionCheck.ok) {
|
|
92
|
+
return c.json({ error: sessionCheck.error }, sessionCheck.status as any);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const startTime = Date.now();
|
|
96
|
+
const query = readQueryParams(c);
|
|
97
|
+
const body = c.req.method === 'GET' || c.req.method === 'DELETE' ? {} : await readRequestBody(c);
|
|
98
|
+
const payload = { ...body, ...query };
|
|
99
|
+
const methodName = routeMethod || routeDefinition?.functionName || (typeof payload.method === 'string' ? payload.method : undefined);
|
|
100
|
+
|
|
101
|
+
if (!methodName) {
|
|
102
|
+
return c.json({ error: 'Missing method name' }, 400);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const definition = routeDefinition || methods.get(methodName);
|
|
106
|
+
|
|
107
|
+
if (!definition) {
|
|
108
|
+
return c.json({ error: `Unknown method: ${methodName}` }, 404);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const result = await executeCapability({
|
|
112
|
+
client: resolveClient(clientSource),
|
|
113
|
+
definition,
|
|
114
|
+
payload,
|
|
115
|
+
elasticEmitter: options.elasticEmitter,
|
|
116
|
+
sessionId: options.config.sessionId,
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
if (result.success) {
|
|
120
|
+
return c.json({ success: true, data: result.data });
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return c.json(
|
|
124
|
+
{
|
|
125
|
+
error: result.error,
|
|
126
|
+
details: result.details,
|
|
127
|
+
},
|
|
128
|
+
result.status as any,
|
|
129
|
+
);
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const redirectToCanonical = (definition: HttpMethodDefinition, c: any) => {
|
|
133
|
+
c.header('X-OpenWA-Aliases', definition.routeSignatures.join(', '));
|
|
134
|
+
const query = new URL(c.req.url).search;
|
|
135
|
+
return c.redirect(`${definition.path}${query}`, 308);
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
for (const definition of methodDefinitions) {
|
|
139
|
+
const localCanonicalPath = toLocalPath(definition.path, basePath);
|
|
140
|
+
app.on(definition.httpMethod, localCanonicalPath, (c) =>
|
|
141
|
+
handleInvocation(c, definition.functionName, undefined, definition),
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
for (const aliasRoute of definition.aliasRoutes) {
|
|
145
|
+
const localAliasPath = toLocalPath(aliasRoute.path, basePath);
|
|
146
|
+
app.on(definition.httpMethod, localAliasPath, (c) => redirectToCanonical(definition, c));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
app.post('/', (c) => handleInvocation(c, undefined));
|
|
151
|
+
app.post('/:method', (c) => handleInvocation(c, c.req.param('method')));
|
|
152
|
+
app.post('/:namespace/:method', (c) => handleInvocation(c, `${c.req.param('namespace')}.${c.req.param('method')}`));
|
|
153
|
+
|
|
154
|
+
if (useSessionIdInPath) {
|
|
155
|
+
app.post('/:sessionId', (c) => handleInvocation(c, undefined, c.req.param('sessionId')));
|
|
156
|
+
app.post('/:sessionId/:method', (c) =>
|
|
157
|
+
handleInvocation(c, c.req.param('method'), c.req.param('sessionId'))
|
|
158
|
+
);
|
|
159
|
+
app.post('/:sessionId/:namespace/:method', (c) =>
|
|
160
|
+
handleInvocation(
|
|
161
|
+
c,
|
|
162
|
+
`${c.req.param('namespace')}.${c.req.param('method')}`,
|
|
163
|
+
c.req.param('sessionId'),
|
|
164
|
+
)
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
app.get('/', (c) =>
|
|
169
|
+
c.json({
|
|
170
|
+
endpoints: methodDefinitions.map((def) => ({
|
|
171
|
+
method: def.httpMethod,
|
|
172
|
+
path: def.path,
|
|
173
|
+
name: def.functionName,
|
|
174
|
+
namespacedName: def.namespacedName,
|
|
175
|
+
description: def.description,
|
|
176
|
+
category: def.namespace,
|
|
177
|
+
aliases: def.aliases,
|
|
178
|
+
routeSignatures: def.routeSignatures,
|
|
179
|
+
})),
|
|
180
|
+
})
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
return app;
|
|
184
|
+
}
|