@pikku/core 0.12.2 → 0.12.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/CHANGELOG.md +18 -0
- package/dist/crypto-utils.d.ts +10 -0
- package/dist/crypto-utils.js +62 -0
- package/dist/function/function-runner.js +4 -4
- package/dist/function/functions.types.d.ts +26 -1
- package/dist/function/functions.types.js +16 -0
- package/dist/handle-error.d.ts +1 -1
- package/dist/handle-error.js +7 -3
- package/dist/index.d.ts +14 -2
- package/dist/index.js +1 -1
- package/dist/pikku-state.js +3 -1
- package/dist/schema.js +1 -1
- package/dist/services/in-memory-workflow-service.d.ts +8 -2
- package/dist/services/in-memory-workflow-service.js +28 -1
- package/dist/services/workflow-service.d.ts +8 -2
- package/dist/testing/index.d.ts +2 -0
- package/dist/testing/index.js +1 -0
- package/dist/testing/service-tests.d.ts +31 -0
- package/dist/testing/service-tests.js +598 -0
- package/dist/types/core.types.d.ts +9 -1
- package/dist/types/state.types.d.ts +4 -2
- package/dist/utils/hash.d.ts +2 -0
- package/dist/utils/hash.js +23 -0
- package/dist/wirings/ai-agent/agent-dynamic-workflow.d.ts +6 -0
- package/dist/wirings/ai-agent/agent-dynamic-workflow.js +348 -0
- package/dist/wirings/ai-agent/ai-agent-memory.d.ts +1 -1
- package/dist/wirings/ai-agent/ai-agent-memory.js +6 -5
- package/dist/wirings/ai-agent/ai-agent-prepare.d.ts +10 -7
- package/dist/wirings/ai-agent/ai-agent-prepare.js +127 -47
- package/dist/wirings/ai-agent/ai-agent-runner.d.ts +4 -0
- package/dist/wirings/ai-agent/ai-agent-runner.js +381 -27
- package/dist/wirings/ai-agent/ai-agent-stream.d.ts +7 -2
- package/dist/wirings/ai-agent/ai-agent-stream.js +109 -46
- package/dist/wirings/ai-agent/ai-agent.types.d.ts +22 -1
- package/dist/wirings/ai-agent/index.d.ts +1 -2
- package/dist/wirings/ai-agent/index.js +1 -2
- package/dist/wirings/channel/channel-handler.js +5 -1
- package/dist/wirings/channel/channel.types.d.ts +1 -7
- package/dist/wirings/channel/local/local-channel-handler.d.ts +1 -1
- package/dist/wirings/channel/local/local-channel-handler.js +5 -3
- package/dist/wirings/channel/local/local-channel-runner.js +14 -4
- package/dist/wirings/channel/serverless/serverless-channel-runner.js +4 -1
- package/dist/wirings/cli/cli-runner.js +10 -2
- package/dist/wirings/cli/cli.types.d.ts +1 -0
- package/dist/wirings/http/http-routes.js +0 -1
- package/dist/wirings/http/http-runner.d.ts +1 -1
- package/dist/wirings/http/http-runner.js +25 -3
- package/dist/wirings/http/http.types.d.ts +3 -11
- package/dist/wirings/http/index.d.ts +2 -1
- package/dist/wirings/http/index.js +1 -0
- package/dist/wirings/http/pikku-fetch-http-request.d.ts +1 -0
- package/dist/wirings/http/pikku-fetch-http-request.js +3 -0
- package/dist/wirings/http/pikku-fetch-http-response.d.ts +1 -0
- package/dist/wirings/http/pikku-fetch-http-response.js +4 -4
- package/dist/wirings/http/web-request.d.ts +12 -0
- package/dist/wirings/http/web-request.js +85 -0
- package/dist/wirings/mcp/mcp-endpoint-registry.js +1 -1
- package/dist/wirings/mcp/mcp-runner.js +13 -1
- package/dist/wirings/rpc/rpc-runner.d.ts +30 -6
- package/dist/wirings/rpc/rpc-runner.js +36 -14
- package/dist/wirings/rpc/rpc-types.d.ts +6 -1
- package/dist/wirings/rpc/wire-addon.d.ts +1 -0
- package/dist/wirings/rpc/wire-addon.js +1 -1
- package/dist/wirings/scheduler/scheduler-runner.js +1 -0
- package/dist/wirings/workflow/graph/graph-validation.d.ts +3 -0
- package/dist/wirings/workflow/graph/graph-validation.js +175 -0
- package/dist/wirings/workflow/graph/index.d.ts +1 -0
- package/dist/wirings/workflow/graph/index.js +1 -0
- package/dist/wirings/workflow/index.d.ts +1 -1
- package/dist/wirings/workflow/pikku-workflow-service.d.ts +8 -2
- package/dist/wirings/workflow/pikku-workflow-service.js +1 -2
- package/dist/wirings/workflow/workflow.types.d.ts +10 -1
- package/package.json +4 -2
- package/src/crypto-utils.test.ts +99 -1
- package/src/crypto-utils.ts +114 -0
- package/src/function/function-runner.ts +5 -5
- package/src/function/functions.types.ts +38 -1
- package/src/handle-error.test.ts +65 -2
- package/src/handle-error.ts +8 -4
- package/src/index.ts +14 -0
- package/src/pikku-state.ts +3 -1
- package/src/schema.ts +1 -1
- package/src/services/in-memory-workflow-service.ts +45 -2
- package/src/services/workflow-service.ts +12 -1
- package/src/testing/index.ts +2 -0
- package/src/testing/service-tests.ts +873 -0
- package/src/types/core.types.ts +9 -1
- package/src/types/state.types.ts +4 -2
- package/src/utils/hash.test.ts +68 -0
- package/src/utils/hash.ts +26 -0
- package/src/wirings/ai-agent/agent-dynamic-workflow.ts +469 -0
- package/src/wirings/ai-agent/ai-agent-memory.ts +7 -6
- package/src/wirings/ai-agent/ai-agent-prepare.ts +175 -61
- package/src/wirings/ai-agent/ai-agent-runner.test.ts +3 -1
- package/src/wirings/ai-agent/ai-agent-runner.ts +553 -31
- package/src/wirings/ai-agent/ai-agent-stream.test.ts +209 -2
- package/src/wirings/ai-agent/ai-agent-stream.ts +182 -89
- package/src/wirings/ai-agent/ai-agent.types.ts +23 -1
- package/src/wirings/ai-agent/index.ts +1 -5
- package/src/wirings/channel/channel-handler.ts +11 -2
- package/src/wirings/channel/channel.types.ts +1 -7
- package/src/wirings/channel/local/local-channel-handler.ts +5 -3
- package/src/wirings/channel/local/local-channel-runner.ts +17 -7
- package/src/wirings/channel/serverless/serverless-channel-runner.ts +4 -1
- package/src/wirings/cli/cli-runner.ts +12 -2
- package/src/wirings/cli/cli.types.ts +1 -0
- package/src/wirings/http/http-routes.ts +0 -1
- package/src/wirings/http/http-runner.ts +35 -10
- package/src/wirings/http/http.types.ts +3 -12
- package/src/wirings/http/index.ts +3 -0
- package/src/wirings/http/pikku-fetch-http-request.ts +4 -0
- package/src/wirings/http/pikku-fetch-http-response.ts +4 -4
- package/src/wirings/http/web-request.test.ts +236 -0
- package/src/wirings/http/web-request.ts +104 -0
- package/src/wirings/mcp/mcp-endpoint-registry.ts +1 -1
- package/src/wirings/mcp/mcp-runner.ts +30 -12
- package/src/wirings/rpc/rpc-runner.ts +45 -24
- package/src/wirings/rpc/rpc-types.ts +6 -1
- package/src/wirings/rpc/wire-addon.ts +2 -1
- package/src/wirings/scheduler/scheduler-runner.ts +1 -0
- package/src/wirings/workflow/graph/graph-validation.test.ts +170 -0
- package/src/wirings/workflow/graph/graph-validation.ts +237 -0
- package/src/wirings/workflow/graph/index.ts +5 -0
- package/src/wirings/workflow/index.ts +1 -0
- package/src/wirings/workflow/pikku-workflow-service.ts +14 -3
- package/src/wirings/workflow/workflow.types.ts +9 -1
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/wirings/ai-agent/ai-agent-assistant-ui.d.ts +0 -5
- package/dist/wirings/ai-agent/ai-agent-assistant-ui.js +0 -168
- package/src/wirings/ai-agent/ai-agent-assistant-ui.test.ts +0 -363
- package/src/wirings/ai-agent/ai-agent-assistant-ui.ts +0 -238
|
@@ -7,6 +7,7 @@ import { pikkuState } from '../../pikku-state.js';
|
|
|
7
7
|
import { PikkuFetchHTTPResponse } from './pikku-fetch-http-response.js';
|
|
8
8
|
import { PikkuFetchHTTPRequest } from './pikku-fetch-http-request.js';
|
|
9
9
|
import { addFunction, runPikkuFunc } from '../../function/function-runner.js';
|
|
10
|
+
import { applyWebResponse } from './web-request.js';
|
|
10
11
|
import { httpRouter } from './routers/http-router.js';
|
|
11
12
|
import { validateSchema } from '../../schema.js';
|
|
12
13
|
import { runMiddleware } from '../../middleware-runner.js';
|
|
@@ -271,9 +272,13 @@ const executeRoute = async (services, matchedRoute, http, options) => {
|
|
|
271
272
|
tags: route.tags,
|
|
272
273
|
wire,
|
|
273
274
|
sessionService: userSession,
|
|
275
|
+
packageName: meta.packageName,
|
|
274
276
|
});
|
|
275
277
|
if (!matchedRoute.route.sse) {
|
|
276
|
-
if (result
|
|
278
|
+
if (result instanceof Response) {
|
|
279
|
+
await applyWebResponse(http.response, result);
|
|
280
|
+
}
|
|
281
|
+
else if (result === undefined || result === null) {
|
|
277
282
|
http?.response?.status(204);
|
|
278
283
|
}
|
|
279
284
|
else if (route.returnsJSON === false) {
|
|
@@ -341,7 +346,7 @@ export const pikkuFetch = async (request, params = {}) => {
|
|
|
341
346
|
* @param {RunHTTPWiringOptions} options - Options such as singleton services, session handling, and error configuration.
|
|
342
347
|
* @returns {Promise<Out | void>} The output from the route handler or void if an error occurred.
|
|
343
348
|
*/
|
|
344
|
-
export const fetchData = async (request, response, { skipUserSession = false, respondWith404 = true, logWarningsForStatusCodes = [], coerceDataFromSchema = true, bubbleErrors = false, generateRequestId, } = {}) => {
|
|
349
|
+
export const fetchData = async (request, response, { skipUserSession = false, respondWith404 = true, logWarningsForStatusCodes = [], coerceDataFromSchema = true, bubbleErrors = false, exposeErrors = false, generateRequestId, } = {}) => {
|
|
345
350
|
const singletonServices = getSingletonServices();
|
|
346
351
|
const createWireServices = getCreateWireServices();
|
|
347
352
|
let wireServices;
|
|
@@ -389,7 +394,24 @@ export const fetchData = async (request, response, { skipUserSession = false, re
|
|
|
389
394
|
return result;
|
|
390
395
|
}
|
|
391
396
|
catch (e) {
|
|
392
|
-
|
|
397
|
+
if (matchedRoute?.route.sse) {
|
|
398
|
+
// For SSE routes, send error through the stream since the response is already in stream mode
|
|
399
|
+
singletonServices.logger.error(e instanceof Error ? e.message : e);
|
|
400
|
+
try {
|
|
401
|
+
response.arrayBuffer(JSON.stringify({
|
|
402
|
+
type: 'error',
|
|
403
|
+
errorText: e instanceof Error ? e.message : String(e),
|
|
404
|
+
}));
|
|
405
|
+
response.arrayBuffer('[DONE]');
|
|
406
|
+
}
|
|
407
|
+
catch (streamErr) {
|
|
408
|
+
singletonServices.logger.error(`SSE error while sending error payload: ${streamErr instanceof Error ? streamErr.message : String(streamErr)}`);
|
|
409
|
+
}
|
|
410
|
+
response.close?.();
|
|
411
|
+
}
|
|
412
|
+
else {
|
|
413
|
+
handleHTTPError(e, http, requestId, singletonServices.logger, logWarningsForStatusCodes, respondWith404, bubbleErrors, exposeErrors);
|
|
414
|
+
}
|
|
393
415
|
}
|
|
394
416
|
finally {
|
|
395
417
|
// Clean up any session-specific services created during processing
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { SerializeOptions } from 'cookie';
|
|
2
2
|
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
3
|
-
import type { PikkuError } from '../../errors/error-handler.js';
|
|
4
3
|
import type { CorePikkuMiddleware, CommonWireMeta } from '../../types/core.types.js';
|
|
5
4
|
import type { CorePikkuFunction, CorePikkuFunctionSessionless, CorePikkuPermission, CorePermissionGroup, CorePikkuFunctionConfig } from '../../function/functions.types.js';
|
|
6
5
|
type ExtractHTTPWiringParams<S extends string> = S extends `${string}:${infer Param}/${infer Rest}` ? Param | ExtractHTTPWiringParams<`/${Rest}`> : S extends `${string}:${infer Param}` ? Param : never;
|
|
@@ -16,6 +15,7 @@ export type RunHTTPWiringOptions = Partial<{
|
|
|
16
15
|
logWarningsForStatusCodes: number[];
|
|
17
16
|
coerceDataFromSchema: boolean;
|
|
18
17
|
bubbleErrors: boolean;
|
|
18
|
+
exposeErrors: boolean;
|
|
19
19
|
generateRequestId: () => string;
|
|
20
20
|
}>;
|
|
21
21
|
/**
|
|
@@ -27,15 +27,6 @@ export type HTTPMethod = 'post' | 'get' | 'delete' | 'patch' | 'head' | 'put' |
|
|
|
27
27
|
* Uses Standard Schema interface - works with Zod, Valibot, ArkType, Effect Schema, etc.
|
|
28
28
|
*/
|
|
29
29
|
export type HTTPHeadersSchema = StandardSchemaV1<Record<string, string | string[] | undefined>>;
|
|
30
|
-
/**
|
|
31
|
-
* Documentation config for OpenAPI generation
|
|
32
|
-
*/
|
|
33
|
-
export type HTTPRouteDocsConfig = {
|
|
34
|
-
description?: string;
|
|
35
|
-
response?: string;
|
|
36
|
-
errors?: Array<typeof PikkuError>;
|
|
37
|
-
tags?: string[];
|
|
38
|
-
};
|
|
39
30
|
/**
|
|
40
31
|
* Common HTTP route configuration shared between wireHTTP and wireHTTPRoutes
|
|
41
32
|
*/
|
|
@@ -44,7 +35,6 @@ export type HTTPRouteBaseConfig = {
|
|
|
44
35
|
timeout?: number;
|
|
45
36
|
tags?: string[];
|
|
46
37
|
headers?: HTTPHeadersSchema;
|
|
47
|
-
docs?: HTTPRouteDocsConfig;
|
|
48
38
|
};
|
|
49
39
|
/**
|
|
50
40
|
* Represents an API HTTP wiring without a function, including metadata such as content type, route, and timeout settings.
|
|
@@ -167,6 +157,7 @@ export interface PikkuHTTPRequest<In = unknown> {
|
|
|
167
157
|
data(): Promise<In>;
|
|
168
158
|
json(): Promise<unknown>;
|
|
169
159
|
arrayBuffer(): Promise<ArrayBuffer>;
|
|
160
|
+
headers(): Record<string, string>;
|
|
170
161
|
header(headerName: string): string | null;
|
|
171
162
|
cookie(name?: string): string | null;
|
|
172
163
|
params(): Partial<Record<string, string | string[]>>;
|
|
@@ -179,6 +170,7 @@ export interface PikkuHTTPResponse<Out = unknown> {
|
|
|
179
170
|
header(name: string, value: string | string[]): this;
|
|
180
171
|
arrayBuffer(data: ArrayBuffer | ArrayBufferView | Blob | string | FormData | URLSearchParams | ReadableStream): this;
|
|
181
172
|
json(data: Out): this;
|
|
173
|
+
send?(data: string | ArrayBuffer | Buffer): this;
|
|
182
174
|
redirect(location: string, status?: number): this;
|
|
183
175
|
close?: () => void;
|
|
184
176
|
setMode?: (mode: 'stream') => void;
|
|
@@ -3,4 +3,5 @@ export { PikkuFetchHTTPResponse } from './pikku-fetch-http-response.js';
|
|
|
3
3
|
export { logRoutes } from './log-http-routes.js';
|
|
4
4
|
export { fetch, fetchData, wireHTTP, addHTTPMiddleware, addHTTPPermission, } from './http-runner.js';
|
|
5
5
|
export { wireHTTPRoutes, defineHTTPRoutes } from './http-routes.js';
|
|
6
|
-
export
|
|
6
|
+
export { toWebRequest, applyWebResponse } from './web-request.js';
|
|
7
|
+
export type { AssertHTTPWiringParams, CoreHTTPFunctionWiring, HTTPMethod, HTTPRouteBaseConfig, HTTPRouteContract, HTTPRouteMap, HTTPWiringsMeta, PikkuHTTPRequest, PikkuHTTPResponse, PikkuQuery, RunHTTPWiringOptions, } from './http.types.js';
|
|
@@ -3,3 +3,4 @@ export { PikkuFetchHTTPResponse } from './pikku-fetch-http-response.js';
|
|
|
3
3
|
export { logRoutes } from './log-http-routes.js';
|
|
4
4
|
export { fetch, fetchData, wireHTTP, addHTTPMiddleware, addHTTPPermission, } from './http-runner.js';
|
|
5
5
|
export { wireHTTPRoutes, defineHTTPRoutes } from './http-routes.js';
|
|
6
|
+
export { toWebRequest, applyWebResponse } from './web-request.js';
|
|
@@ -20,6 +20,7 @@ export declare class PikkuFetchHTTPRequest<In = unknown> implements PikkuHTTPReq
|
|
|
20
20
|
* @returns A promise that resolves to the raw request body.
|
|
21
21
|
*/
|
|
22
22
|
arrayBuffer(): Promise<ArrayBuffer>;
|
|
23
|
+
headers(): Record<string, string>;
|
|
23
24
|
/**
|
|
24
25
|
* Retrieves the value of a specific header.
|
|
25
26
|
* @param headerName - The name of the header to retrieve.
|
|
@@ -35,6 +35,9 @@ export class PikkuFetchHTTPRequest {
|
|
|
35
35
|
arrayBuffer() {
|
|
36
36
|
return this.request.arrayBuffer();
|
|
37
37
|
}
|
|
38
|
+
headers() {
|
|
39
|
+
return Object.fromEntries(this.request.headers.entries());
|
|
40
|
+
}
|
|
38
41
|
/**
|
|
39
42
|
* Retrieves the value of a specific header.
|
|
40
43
|
* @param headerName - The name of the header to retrieve.
|
|
@@ -10,6 +10,7 @@ export declare class PikkuFetchHTTPResponse implements PikkuHTTPResponse {
|
|
|
10
10
|
json(data: unknown): this;
|
|
11
11
|
text(content: string): this;
|
|
12
12
|
html(content: string): this;
|
|
13
|
+
send(data: any): this;
|
|
13
14
|
redirect(location: string, status?: number): this;
|
|
14
15
|
close(): this;
|
|
15
16
|
toResponse(args?: Record<string, any>): Response;
|
|
@@ -71,10 +71,10 @@ export class PikkuFetchHTTPResponse {
|
|
|
71
71
|
}
|
|
72
72
|
return this;
|
|
73
73
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
74
|
+
send(data) {
|
|
75
|
+
this.#body = data;
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
78
|
redirect(location, status = 302) {
|
|
79
79
|
if (!location.startsWith('/') &&
|
|
80
80
|
!location.startsWith('https://') &&
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { PikkuHTTPRequest } from './http.types.js';
|
|
2
|
+
import type { PikkuHTTPResponse } from './http.types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Converts a PikkuHTTPRequest into a Web API Request.
|
|
5
|
+
* Useful for bridging Pikku routes to libraries that expect standard Web Request objects.
|
|
6
|
+
*/
|
|
7
|
+
export declare function toWebRequest(req: PikkuHTTPRequest, baseUrl?: string): Request;
|
|
8
|
+
/**
|
|
9
|
+
* Applies a Web API Response to a PikkuHTTPResponse.
|
|
10
|
+
* Copies status, headers (including Set-Cookie), redirects, and body.
|
|
11
|
+
*/
|
|
12
|
+
export declare function applyWebResponse(res: PikkuHTTPResponse, webResponse: Response): Promise<void>;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a PikkuHTTPRequest into a Web API Request.
|
|
3
|
+
* Useful for bridging Pikku routes to libraries that expect standard Web Request objects.
|
|
4
|
+
*/
|
|
5
|
+
export function toWebRequest(req, baseUrl) {
|
|
6
|
+
const url = new URL(req.path(), baseUrl ?? `http://${req.header('host') ?? 'localhost'}`);
|
|
7
|
+
const query = req.query();
|
|
8
|
+
for (const [key, value] of Object.entries(query)) {
|
|
9
|
+
if (value != null) {
|
|
10
|
+
url.searchParams.set(key, String(value));
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
const headers = new Headers(req.headers());
|
|
14
|
+
const method = req.method().toUpperCase();
|
|
15
|
+
const hasBody = !['GET', 'HEAD', 'OPTIONS'].includes(method);
|
|
16
|
+
if (!hasBody) {
|
|
17
|
+
return new Request(url, { method, headers });
|
|
18
|
+
}
|
|
19
|
+
return new Request(url, {
|
|
20
|
+
method,
|
|
21
|
+
headers,
|
|
22
|
+
body: new ReadableStream({
|
|
23
|
+
async start(controller) {
|
|
24
|
+
try {
|
|
25
|
+
const buffer = await req.arrayBuffer();
|
|
26
|
+
if (buffer.byteLength > 0) {
|
|
27
|
+
controller.enqueue(new Uint8Array(buffer));
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
// arrayBuffer may be empty if body was pre-parsed (e.g., Express middleware)
|
|
31
|
+
const contentType = (headers.get('content-type') || '').toLowerCase();
|
|
32
|
+
const parsed = await req.json();
|
|
33
|
+
if (parsed &&
|
|
34
|
+
typeof parsed === 'object' &&
|
|
35
|
+
Object.keys(parsed).length > 0) {
|
|
36
|
+
let reconstructed;
|
|
37
|
+
if (contentType.includes('application/x-www-form-urlencoded')) {
|
|
38
|
+
reconstructed = new URLSearchParams(parsed).toString();
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
reconstructed = JSON.stringify(parsed);
|
|
42
|
+
}
|
|
43
|
+
controller.enqueue(new TextEncoder().encode(reconstructed));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
// Empty body
|
|
49
|
+
}
|
|
50
|
+
controller.close();
|
|
51
|
+
},
|
|
52
|
+
}),
|
|
53
|
+
// @ts-ignore - duplex is needed for streaming body in Node.js
|
|
54
|
+
duplex: 'half',
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
const SKIP_RESPONSE_HEADERS = new Set(['content-length', 'transfer-encoding']);
|
|
58
|
+
/**
|
|
59
|
+
* Applies a Web API Response to a PikkuHTTPResponse.
|
|
60
|
+
* Copies status, headers (including Set-Cookie), redirects, and body.
|
|
61
|
+
*/
|
|
62
|
+
export async function applyWebResponse(res, webResponse) {
|
|
63
|
+
res.status(webResponse.status);
|
|
64
|
+
webResponse.headers.forEach((value, name) => {
|
|
65
|
+
const lower = name.toLowerCase();
|
|
66
|
+
if (SKIP_RESPONSE_HEADERS.has(lower)) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (lower === 'location') {
|
|
70
|
+
res.redirect(value, webResponse.status);
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
res.header(name, value);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
const body = await webResponse.text();
|
|
77
|
+
if (body) {
|
|
78
|
+
if (res.send) {
|
|
79
|
+
res.send(body);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
res.arrayBuffer(body);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -12,7 +12,7 @@ export class MCPEndpointRegistry {
|
|
|
12
12
|
name: tool.name,
|
|
13
13
|
title: tool.title,
|
|
14
14
|
description: tool.description,
|
|
15
|
-
inputSchema: tool.parameters,
|
|
15
|
+
inputSchema: tool.parameters ?? { type: 'object' },
|
|
16
16
|
outputSchema: tool.returns,
|
|
17
17
|
enabled: tool.enabled !== undefined ? tool.enabled : true,
|
|
18
18
|
});
|
|
@@ -2,6 +2,7 @@ import { getErrorResponse } from '../../errors/error-handler.js';
|
|
|
2
2
|
import { closeWireServices } from '../../utils.js';
|
|
3
3
|
import { pikkuState, getSingletonServices, getCreateWireServices, } from '../../pikku-state.js';
|
|
4
4
|
import { addFunction, runPikkuFunc } from '../../function/function-runner.js';
|
|
5
|
+
import { resolveNamespace } from '../rpc/rpc-runner.js';
|
|
5
6
|
import { BadRequestError, NotFoundError, PikkuMissingMetaError, } from '../../errors/errors.js';
|
|
6
7
|
import { PikkuSessionService, createMiddlewareSessionWireProps, } from '../../services/user-session-service.js';
|
|
7
8
|
export class MCPError extends Error {
|
|
@@ -115,7 +116,17 @@ async function runMCPPikkuFunc(request, type, name, mcp, pikkuFuncId, { mcp: mcp
|
|
|
115
116
|
else if (type === 'prompt') {
|
|
116
117
|
meta = pikkuState(null, 'mcp', 'promptsMeta')[name];
|
|
117
118
|
}
|
|
118
|
-
|
|
119
|
+
// Resolve namespaced function IDs (e.g., 'swaggerPetstore:findPetsByStatus')
|
|
120
|
+
let resolvedFuncName = pikkuFuncId;
|
|
121
|
+
let resolvedPackageName = meta?.packageName ?? null;
|
|
122
|
+
if (pikkuFuncId.includes(':')) {
|
|
123
|
+
const resolved = resolveNamespace(pikkuFuncId);
|
|
124
|
+
if (resolved) {
|
|
125
|
+
resolvedFuncName = resolved.function;
|
|
126
|
+
resolvedPackageName = resolved.package;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
let result = await runPikkuFunc('mcp', `${type}:${name}`, resolvedFuncName, {
|
|
119
130
|
singletonServices,
|
|
120
131
|
createWireServices,
|
|
121
132
|
data: () => request.params,
|
|
@@ -126,6 +137,7 @@ async function runMCPPikkuFunc(request, type, name, mcp, pikkuFuncId, { mcp: mcp
|
|
|
126
137
|
tags: mcp?.tags,
|
|
127
138
|
wire,
|
|
128
139
|
sessionService: mcpSessionService,
|
|
140
|
+
packageName: resolvedPackageName,
|
|
129
141
|
});
|
|
130
142
|
if (type === 'tool' && meta?.outputSchema !== 'MCPToolResponse') {
|
|
131
143
|
result = [{ type: 'text', text: JSON.stringify(result) }];
|
|
@@ -2,13 +2,18 @@ import type { CoreServices, PikkuWire } from '../../types/core.types.js';
|
|
|
2
2
|
import type { SessionService } from '../../services/user-session-service.js';
|
|
3
3
|
import type { CoreUserSession } from '../../types/core.types.js';
|
|
4
4
|
import { PikkuError } from '../../errors/error-handler.js';
|
|
5
|
-
import type { PikkuRPC } from './rpc-types.js';
|
|
5
|
+
import type { PikkuRPC, ResolvedFunction } from './rpc-types.js';
|
|
6
6
|
export declare class RPCNotFoundError extends PikkuError {
|
|
7
7
|
readonly rpcName: string;
|
|
8
8
|
constructor(rpcName: string);
|
|
9
9
|
}
|
|
10
10
|
import type { AIAgentInput } from '../ai-agent/ai-agent.types.js';
|
|
11
11
|
import type { StreamAIAgentOptions } from '../ai-agent/ai-agent-prepare.js';
|
|
12
|
+
/**
|
|
13
|
+
* Resolve a namespaced function reference to package and function names
|
|
14
|
+
* Uses pikkuState to look up the namespace -> package mapping
|
|
15
|
+
*/
|
|
16
|
+
export declare const resolveNamespace: (namespacedFunction: string) => ResolvedFunction | null;
|
|
12
17
|
export declare class ContextAwareRPCService {
|
|
13
18
|
private services;
|
|
14
19
|
private wire;
|
|
@@ -39,6 +44,14 @@ export declare class ContextAwareRPCService {
|
|
|
39
44
|
}>;
|
|
40
45
|
get agent(): {
|
|
41
46
|
run: (agentName: string, input: AIAgentInput) => Promise<{
|
|
47
|
+
status?: "suspended" | undefined;
|
|
48
|
+
pendingApprovals?: {
|
|
49
|
+
toolCallId: string;
|
|
50
|
+
toolName: string;
|
|
51
|
+
args: unknown;
|
|
52
|
+
reason?: string;
|
|
53
|
+
runId: string;
|
|
54
|
+
}[] | undefined;
|
|
42
55
|
runId: string;
|
|
43
56
|
result: {};
|
|
44
57
|
usage: {
|
|
@@ -50,7 +63,9 @@ export declare class ContextAwareRPCService {
|
|
|
50
63
|
message: string;
|
|
51
64
|
threadId: string;
|
|
52
65
|
resourceId: string;
|
|
53
|
-
|
|
66
|
+
model?: string;
|
|
67
|
+
temperature?: number;
|
|
68
|
+
}, options?: StreamAIAgentOptions) => Promise<void>;
|
|
54
69
|
resume: (runId: string, input: {
|
|
55
70
|
toolCallId: string;
|
|
56
71
|
approved: boolean;
|
|
@@ -59,11 +74,20 @@ export declare class ContextAwareRPCService {
|
|
|
59
74
|
toolCallId: string;
|
|
60
75
|
approved: boolean;
|
|
61
76
|
}[], expectedAgentName?: string) => Promise<{
|
|
62
|
-
status
|
|
77
|
+
status?: "suspended" | undefined;
|
|
78
|
+
pendingApprovals?: {
|
|
79
|
+
toolCallId: string;
|
|
80
|
+
toolName: string;
|
|
81
|
+
args: unknown;
|
|
82
|
+
reason?: string;
|
|
83
|
+
runId: string;
|
|
84
|
+
}[] | undefined;
|
|
63
85
|
runId: string;
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
86
|
+
result: {};
|
|
87
|
+
usage: {
|
|
88
|
+
inputTokens: number;
|
|
89
|
+
outputTokens: number;
|
|
90
|
+
};
|
|
67
91
|
}>;
|
|
68
92
|
};
|
|
69
93
|
remote<In = any, Out = any>(funcName: string, data: In): Promise<Out>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { runPikkuFunc } from '../../function/function-runner.js';
|
|
2
2
|
import { pikkuState } from '../../pikku-state.js';
|
|
3
3
|
import { ForbiddenError } from '../../errors/errors.js';
|
|
4
|
-
import { PikkuError } from '../../errors/error-handler.js';
|
|
4
|
+
import { PikkuError, addError } from '../../errors/error-handler.js';
|
|
5
5
|
import { parseVersionedId } from '../../version.js';
|
|
6
6
|
import { encryptJSON } from '../../crypto-utils.js';
|
|
7
7
|
export class RPCNotFoundError extends PikkuError {
|
|
@@ -11,29 +11,33 @@ export class RPCNotFoundError extends PikkuError {
|
|
|
11
11
|
this.rpcName = rpcName;
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
|
-
|
|
14
|
+
addError(RPCNotFoundError, {
|
|
15
|
+
status: 404,
|
|
16
|
+
mcpCode: -32601,
|
|
17
|
+
message: 'RPC function not found.',
|
|
18
|
+
});
|
|
19
|
+
import { runAIAgent, resumeAIAgentSync } from '../ai-agent/ai-agent-runner.js';
|
|
15
20
|
import { streamAIAgent, resumeAIAgent } from '../ai-agent/ai-agent-stream.js';
|
|
16
|
-
import { approveAIAgent } from '../ai-agent/ai-agent-registry.js';
|
|
17
21
|
/**
|
|
18
22
|
* Resolve a namespaced function reference to package and function names
|
|
19
23
|
* Uses pikkuState to look up the namespace -> package mapping
|
|
20
24
|
*/
|
|
21
|
-
const resolveNamespace = (namespacedFunction) => {
|
|
25
|
+
export const resolveNamespace = (namespacedFunction) => {
|
|
22
26
|
const colonIndex = namespacedFunction.indexOf(':');
|
|
23
27
|
if (colonIndex === -1) {
|
|
24
28
|
return null;
|
|
25
29
|
}
|
|
26
30
|
const namespace = namespacedFunction.substring(0, colonIndex);
|
|
27
31
|
const functionName = namespacedFunction.substring(colonIndex + 1);
|
|
28
|
-
const addons = pikkuState(null, '
|
|
32
|
+
const addons = pikkuState(null, 'addons', 'packages');
|
|
29
33
|
const pkgConfig = addons.get(namespace);
|
|
30
34
|
if (!pkgConfig) {
|
|
31
35
|
return null;
|
|
32
36
|
}
|
|
33
37
|
return {
|
|
34
|
-
namespace,
|
|
35
38
|
package: pkgConfig.package,
|
|
36
39
|
function: functionName,
|
|
40
|
+
addonConfig: pkgConfig,
|
|
37
41
|
};
|
|
38
42
|
};
|
|
39
43
|
const getPikkuFunctionName = (rpcName) => {
|
|
@@ -72,7 +76,7 @@ export class ContextAwareRPCService {
|
|
|
72
76
|
functionMeta = pikkuState(null, 'function', 'meta')[funcName];
|
|
73
77
|
}
|
|
74
78
|
if (!functionMeta) {
|
|
75
|
-
throw new
|
|
79
|
+
throw new RPCNotFoundError(funcName);
|
|
76
80
|
}
|
|
77
81
|
if (!functionMeta.expose) {
|
|
78
82
|
throw new ForbiddenError();
|
|
@@ -125,9 +129,11 @@ export class ContextAwareRPCService {
|
|
|
125
129
|
`Available functions: ${Object.keys(addonFunctionMeta).join(', ') || '(none)'}`);
|
|
126
130
|
}
|
|
127
131
|
const funcName = funcMeta.pikkuFuncId || resolved.function;
|
|
128
|
-
const
|
|
129
|
-
const
|
|
130
|
-
|
|
132
|
+
const auth = resolved.addonConfig?.auth ?? this.options.requiresAuth;
|
|
133
|
+
const tags = [
|
|
134
|
+
...(resolved.addonConfig?.tags ?? []),
|
|
135
|
+
...(funcMeta.tags ?? []),
|
|
136
|
+
];
|
|
131
137
|
// Execute the function using runPikkuFunc with the addon package's state
|
|
132
138
|
// We use the parent services (this.services) since addon packages share services
|
|
133
139
|
// Pass the function's tags so tag-based middleware/permissions are applied
|
|
@@ -153,6 +159,9 @@ export class ContextAwareRPCService {
|
|
|
153
159
|
}
|
|
154
160
|
: undefined,
|
|
155
161
|
};
|
|
162
|
+
if (rpcName.includes(':')) {
|
|
163
|
+
return this.invokeAddonFunction(rpcName, data, mergedWire);
|
|
164
|
+
}
|
|
156
165
|
return runPikkuFunc('rpc', rpcName, getPikkuFunctionName(rpcName), {
|
|
157
166
|
auth: this.options.requiresAuth,
|
|
158
167
|
singletonServices: this.services,
|
|
@@ -182,6 +191,10 @@ export class ContextAwareRPCService {
|
|
|
182
191
|
runId: result.runId,
|
|
183
192
|
result: result.object ?? result.text,
|
|
184
193
|
usage: result.usage,
|
|
194
|
+
...(result.status === 'suspended' && {
|
|
195
|
+
status: 'suspended',
|
|
196
|
+
pendingApprovals: result.pendingApprovals,
|
|
197
|
+
}),
|
|
185
198
|
};
|
|
186
199
|
},
|
|
187
200
|
stream: async (agentName, input, options) => {
|
|
@@ -197,9 +210,18 @@ export class ContextAwareRPCService {
|
|
|
197
210
|
await resumeAIAgent({ runId, ...input }, channel, { sessionService: this.options.sessionService }, options);
|
|
198
211
|
},
|
|
199
212
|
approve: async (runId, approvals, expectedAgentName) => {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
213
|
+
const result = await resumeAIAgentSync(runId, approvals, {
|
|
214
|
+
sessionService: this.options.sessionService,
|
|
215
|
+
}, expectedAgentName);
|
|
216
|
+
return {
|
|
217
|
+
runId: result.runId,
|
|
218
|
+
result: result.object ?? result.text,
|
|
219
|
+
usage: result.usage,
|
|
220
|
+
...(result.status === 'suspended' && {
|
|
221
|
+
status: 'suspended',
|
|
222
|
+
pendingApprovals: result.pendingApprovals,
|
|
223
|
+
}),
|
|
224
|
+
};
|
|
203
225
|
},
|
|
204
226
|
};
|
|
205
227
|
}
|
|
@@ -211,7 +233,7 @@ export class ContextAwareRPCService {
|
|
|
211
233
|
const colonIndex = funcName.indexOf(':');
|
|
212
234
|
if (colonIndex !== -1) {
|
|
213
235
|
const namespace = funcName.substring(0, colonIndex);
|
|
214
|
-
const addons = pikkuState(null, '
|
|
236
|
+
const addons = pikkuState(null, 'addons', 'packages');
|
|
215
237
|
const pkgConfig = addons.get(namespace);
|
|
216
238
|
endpoint = pkgConfig?.rpcEndpoint;
|
|
217
239
|
}
|
|
@@ -34,7 +34,12 @@ export interface RPCHandler<Input, Output> {
|
|
|
34
34
|
* Resolved function reference from namespace
|
|
35
35
|
*/
|
|
36
36
|
export interface ResolvedFunction {
|
|
37
|
-
namespace: string;
|
|
38
37
|
package: string;
|
|
39
38
|
function: string;
|
|
39
|
+
addonConfig: {
|
|
40
|
+
package: string;
|
|
41
|
+
auth?: boolean;
|
|
42
|
+
tags?: string[];
|
|
43
|
+
rpcEndpoint?: string;
|
|
44
|
+
};
|
|
40
45
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { pikkuState } from '../../pikku-state.js';
|
|
2
2
|
export const wireAddon = (config) => {
|
|
3
|
-
pikkuState(null, '
|
|
3
|
+
pikkuState(null, 'addons', 'packages').set(config.name, {
|
|
4
4
|
package: config.package,
|
|
5
5
|
rpcEndpoint: config.rpcEndpoint,
|
|
6
6
|
auth: config.auth,
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export declare function computeEntryNodeIds(nodes: Record<string, any>): string[];
|
|
2
|
+
export declare function validateWorkflowWiring(nodes: Record<string, any>, toolNames: string[]): string[];
|
|
3
|
+
export declare function generateMermaidDiagram(workflowName: string, nodes: Record<string, any>, entryNodeIds: string[]): string;
|