@pikku/core 0.12.14 → 0.12.15
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 +30 -0
- package/dist/errors/errors.d.ts +4 -0
- package/dist/errors/errors.js +12 -0
- package/dist/function/function-runner.js +2 -1
- package/dist/function/functions.types.js +1 -0
- package/dist/function/index.d.ts +2 -1
- package/dist/function/index.js +1 -0
- package/dist/handle-error.d.ts +2 -2
- package/dist/handle-error.js +8 -8
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -2
- package/dist/middleware/index.d.ts +2 -0
- package/dist/middleware/index.js +2 -0
- package/dist/middleware/remote-auth.js +5 -2
- package/dist/permissions.d.ts +13 -1
- package/dist/permissions.js +51 -0
- package/dist/pikku-state.d.ts +0 -1
- package/dist/pikku-state.js +1 -4
- package/dist/remote.d.ts +10 -0
- package/dist/remote.js +32 -0
- package/dist/schema.js +2 -0
- package/dist/services/deployment-service.d.ts +12 -1
- package/dist/services/in-memory-queue-service.d.ts +7 -0
- package/dist/services/in-memory-queue-service.js +28 -0
- package/dist/services/index.d.ts +4 -1
- package/dist/services/index.js +2 -1
- package/dist/services/logger-console.d.ts +26 -40
- package/dist/services/logger-console.js +102 -46
- package/dist/services/logger.d.ts +5 -0
- package/dist/services/meta-service.d.ts +175 -0
- package/dist/services/meta-service.js +310 -0
- package/dist/services/workflow-service.d.ts +6 -1
- package/dist/testing/service-tests.js +0 -8
- package/dist/types/core.types.d.ts +8 -0
- package/dist/types/state.types.d.ts +2 -2
- package/dist/wirings/ai-agent/ai-agent-prepare.js +42 -21
- package/dist/wirings/ai-agent/ai-agent-registry.js +2 -2
- package/dist/wirings/ai-agent/ai-agent-runner.js +18 -1
- package/dist/wirings/ai-agent/ai-agent-stream.js +1 -0
- package/dist/wirings/ai-agent/ai-agent.types.d.ts +5 -3
- package/dist/wirings/channel/channel-runner.js +3 -3
- package/dist/wirings/cli/cli-runner.js +3 -2
- package/dist/wirings/cli/command-parser.js +7 -3
- package/dist/wirings/http/http-runner.d.ts +1 -1
- package/dist/wirings/http/http-runner.js +23 -11
- package/dist/wirings/http/http.types.d.ts +2 -0
- package/dist/wirings/mcp/mcp-runner.js +5 -3
- package/dist/wirings/queue/queue-runner.d.ts +3 -1
- package/dist/wirings/queue/queue-runner.js +7 -3
- package/dist/wirings/rpc/rpc-runner.js +56 -90
- package/dist/wirings/scheduler/scheduler-runner.d.ts +3 -1
- package/dist/wirings/scheduler/scheduler-runner.js +9 -5
- package/dist/wirings/trigger/trigger-runner.js +4 -2
- package/dist/wirings/workflow/dsl/workflow-runner.d.ts +1 -1
- package/dist/wirings/workflow/dsl/workflow-runner.js +6 -9
- package/dist/wirings/workflow/graph/graph-runner.d.ts +1 -1
- package/dist/wirings/workflow/graph/graph-runner.js +18 -4
- package/dist/wirings/workflow/index.d.ts +4 -2
- package/dist/wirings/workflow/index.js +4 -2
- package/dist/wirings/workflow/pikku-workflow-service.d.ts +16 -4
- package/dist/wirings/workflow/pikku-workflow-service.js +216 -24
- package/dist/wirings/workflow/workflow-queue-workers.d.ts +40 -0
- package/dist/wirings/workflow/workflow-queue-workers.js +41 -0
- package/dist/wirings/workflow/workflow.types.d.ts +17 -1
- package/package.json +4 -1
- package/src/errors/errors.ts +12 -0
- package/src/function/function-runner.ts +5 -4
- package/src/function/functions.types.ts +1 -0
- package/src/function/index.ts +10 -0
- package/src/handle-error.test.ts +2 -2
- package/src/handle-error.ts +8 -8
- package/src/index.ts +1 -2
- package/src/middleware/index.ts +2 -0
- package/src/middleware/remote-auth.test.ts +4 -4
- package/src/middleware/remote-auth.ts +7 -2
- package/src/permissions.ts +70 -0
- package/src/pikku-state.test.ts +0 -26
- package/src/pikku-state.ts +1 -5
- package/src/remote.ts +46 -0
- package/src/schema.ts +1 -0
- package/src/services/deployment-service.ts +17 -1
- package/src/services/in-memory-queue-service.ts +46 -0
- package/src/services/index.ts +21 -1
- package/src/services/logger-console.ts +127 -47
- package/src/services/logger.ts +6 -0
- package/src/services/meta-service.ts +523 -0
- package/src/services/workflow-service.ts +8 -0
- package/src/testing/service-tests.ts +0 -10
- package/src/types/core.types.ts +8 -0
- package/src/types/state.types.ts +2 -2
- package/src/wirings/ai-agent/ai-agent-prepare.ts +51 -40
- package/src/wirings/ai-agent/ai-agent-registry.ts +3 -3
- package/src/wirings/ai-agent/ai-agent-runner.ts +16 -1
- package/src/wirings/ai-agent/ai-agent-stream.ts +8 -0
- package/src/wirings/ai-agent/ai-agent.types.ts +5 -3
- package/src/wirings/channel/channel-runner.ts +4 -5
- package/src/wirings/cli/cli-runner.test.ts +8 -7
- package/src/wirings/cli/cli-runner.ts +4 -3
- package/src/wirings/cli/command-parser.ts +8 -3
- package/src/wirings/http/http-runner.ts +27 -11
- package/src/wirings/http/http.types.ts +2 -0
- package/src/wirings/mcp/mcp-runner.ts +7 -9
- package/src/wirings/queue/queue-runner.test.ts +5 -11
- package/src/wirings/queue/queue-runner.ts +11 -3
- package/src/wirings/rpc/rpc-runner.ts +82 -115
- package/src/wirings/scheduler/scheduler-runner.test.ts +5 -11
- package/src/wirings/scheduler/scheduler-runner.ts +13 -5
- package/src/wirings/trigger/trigger-runner.test.ts +10 -11
- package/src/wirings/trigger/trigger-runner.ts +6 -4
- package/src/wirings/workflow/dsl/workflow-runner.ts +11 -10
- package/src/wirings/workflow/graph/graph-runner.ts +19 -4
- package/src/wirings/workflow/index.ts +17 -6
- package/src/wirings/workflow/pikku-workflow-service.ts +284 -24
- package/src/wirings/workflow/workflow-queue-workers.ts +85 -0
- package/src/wirings/workflow/workflow.types.ts +16 -1
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/wirings/ai-agent/agent-dynamic-workflow.d.ts +0 -6
- package/dist/wirings/ai-agent/agent-dynamic-workflow.js +0 -468
- package/dist/wirings/workflow/workflow-helpers.d.ts +0 -36
- package/dist/wirings/workflow/workflow-helpers.js +0 -38
- package/src/wirings/ai-agent/agent-dynamic-workflow.ts +0 -610
- package/src/wirings/workflow/workflow-helpers.test.ts +0 -129
- package/src/wirings/workflow/workflow-helpers.ts +0 -79
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { SerializedError } from '../types/core.types.js';
|
|
2
|
-
import type { WorkflowRun, WorkflowRunWire, StepState, WorkflowStatus, WorkflowVersionStatus } from '../wirings/workflow/workflow.types.js';
|
|
2
|
+
import type { WorkflowRun, WorkflowRunWire, WorkflowRunStatus, StepState, WorkflowStatus, WorkflowVersionStatus } from '../wirings/workflow/workflow.types.js';
|
|
3
3
|
/**
|
|
4
4
|
* Interface for workflow orchestration
|
|
5
5
|
* Handles workflow execution, replay, orchestration logic, and run-level state
|
|
@@ -7,6 +7,7 @@ import type { WorkflowRun, WorkflowRunWire, StepState, WorkflowStatus, WorkflowV
|
|
|
7
7
|
export interface WorkflowService {
|
|
8
8
|
createRun(workflowName: string, input: any, inline: boolean, graphHash: string, wire: WorkflowRunWire): Promise<string>;
|
|
9
9
|
getRun(id: string): Promise<WorkflowRun | null>;
|
|
10
|
+
getRunStatus(id: string): Promise<WorkflowRunStatus | null>;
|
|
10
11
|
getRunHistory(runId: string): Promise<Array<StepState & {
|
|
11
12
|
stepName: string;
|
|
12
13
|
}>>;
|
|
@@ -20,6 +21,10 @@ export interface WorkflowService {
|
|
|
20
21
|
}): Promise<{
|
|
21
22
|
runId: string;
|
|
22
23
|
}>;
|
|
24
|
+
runToCompletion<I>(name: string, input: I, rpcService: any, options?: {
|
|
25
|
+
pollIntervalMs?: number;
|
|
26
|
+
wire?: WorkflowRunWire;
|
|
27
|
+
}): Promise<unknown>;
|
|
23
28
|
runWorkflowJob(runId: string, rpcService: any): Promise<void>;
|
|
24
29
|
orchestrateWorkflow(runId: string, rpcService: any): Promise<void>;
|
|
25
30
|
executeWorkflowSleepCompleted(runId: string, stepId: string): Promise<void>;
|
|
@@ -482,14 +482,6 @@ export function defineServiceTests(config) {
|
|
|
482
482
|
endpoint: 'http://localhost:3000',
|
|
483
483
|
functions: ['funcA', 'funcB'],
|
|
484
484
|
});
|
|
485
|
-
const infos = await service.findFunction('funcA');
|
|
486
|
-
assert.ok(infos.length >= 1);
|
|
487
|
-
assert.equal(infos[0].deploymentId, 'deploy-1');
|
|
488
|
-
assert.equal(infos[0].endpoint, 'http://localhost:3000');
|
|
489
|
-
});
|
|
490
|
-
test('findFunction returns empty for unknown function', async () => {
|
|
491
|
-
const infos = await service.findFunction('unknown-func');
|
|
492
|
-
assert.deepEqual(infos, []);
|
|
493
485
|
});
|
|
494
486
|
});
|
|
495
487
|
}
|
|
@@ -24,6 +24,7 @@ import type { AgentRunService } from '../wirings/ai-agent/ai-agent.types.js';
|
|
|
24
24
|
import type { PikkuAIMiddlewareHooks } from '../wirings/ai-agent/ai-agent.types.js';
|
|
25
25
|
import type { WorkflowRunService } from '../wirings/workflow/workflow.types.js';
|
|
26
26
|
import type { CredentialService } from '../services/credential-service.js';
|
|
27
|
+
import type { MetaService } from '../services/meta-service.js';
|
|
27
28
|
export type PikkuWiringTypes = 'http' | 'scheduler' | 'trigger' | 'channel' | 'rpc' | 'queue' | 'mcp' | 'cli' | 'workflow' | 'agent' | 'gateway';
|
|
28
29
|
export interface FunctionServicesMeta {
|
|
29
30
|
optimized: boolean;
|
|
@@ -75,6 +76,7 @@ export type FunctionRuntimeMeta = {
|
|
|
75
76
|
remote?: boolean;
|
|
76
77
|
mcp?: boolean;
|
|
77
78
|
readonly?: boolean;
|
|
79
|
+
deploy?: 'serverless' | 'server' | 'auto';
|
|
78
80
|
sessionless?: boolean;
|
|
79
81
|
version?: number;
|
|
80
82
|
approvalRequired?: boolean;
|
|
@@ -94,6 +96,8 @@ export type FunctionMeta = FunctionRuntimeMeta & Partial<{
|
|
|
94
96
|
middleware: MiddlewareMetadata[];
|
|
95
97
|
permissions: PermissionMetadata[];
|
|
96
98
|
isDirectFunction: boolean;
|
|
99
|
+
sourceFile: string;
|
|
100
|
+
exportedName: string;
|
|
97
101
|
} & CommonWireMeta>;
|
|
98
102
|
export type FunctionsRuntimeMeta = Record<string, FunctionRuntimeMeta>;
|
|
99
103
|
export type FunctionsMeta = Record<string, FunctionMeta>;
|
|
@@ -175,6 +179,8 @@ export interface CoreSingletonServices<Config extends CoreConfig = CoreConfig> {
|
|
|
175
179
|
workflowRunService?: WorkflowRunService;
|
|
176
180
|
/** Credential service for dynamic/managed credentials (OAuth tokens, per-user API keys) */
|
|
177
181
|
credentialService?: CredentialService;
|
|
182
|
+
/** Meta service for reading .pikku metadata files (filesystem on Node, R2/KV on CF) */
|
|
183
|
+
metaService?: MetaService;
|
|
178
184
|
}
|
|
179
185
|
/**
|
|
180
186
|
* Represents different forms of wire within Pikku and the outside world.
|
|
@@ -182,6 +188,8 @@ export interface CoreSingletonServices<Config extends CoreConfig = CoreConfig> {
|
|
|
182
188
|
export type PikkuWire<In = unknown, Out = unknown, HasInitialSession extends boolean = false, UserSession extends CoreUserSession = CoreUserSession, TypedRPC extends PikkuRPC = PikkuRPC, IsChannel extends true | null = null, MCPTools extends string | never = never, TypedWorkflow extends PikkuWorkflowWire | never = PikkuWorkflowWire, TriggerOutput = unknown> = Partial<{
|
|
183
189
|
wireType: PikkuWiringTypes;
|
|
184
190
|
wireId: string;
|
|
191
|
+
/** Trace ID for distributed tracing — propagated across remote RPC calls via x-trace-id header */
|
|
192
|
+
traceId: string;
|
|
185
193
|
http: PikkuHTTP<In>;
|
|
186
194
|
mcp: PikkuMCP<MCPTools>;
|
|
187
195
|
rpc: TypedRPC;
|
|
@@ -130,8 +130,6 @@ export interface PikkuPackageState {
|
|
|
130
130
|
} | null;
|
|
131
131
|
/** Cached singleton services for this package */
|
|
132
132
|
singletonServices: CoreSingletonServices | null;
|
|
133
|
-
/** Absolute path to this package's .pikku directory */
|
|
134
|
-
metaDir: string | null;
|
|
135
133
|
/** Credential metadata for this addon package */
|
|
136
134
|
credentialsMeta: Record<string, {
|
|
137
135
|
name: string;
|
|
@@ -139,5 +137,7 @@ export interface PikkuPackageState {
|
|
|
139
137
|
type: string;
|
|
140
138
|
oauth2?: boolean;
|
|
141
139
|
}> | null;
|
|
140
|
+
/** Services this addon needs from the parent project */
|
|
141
|
+
requiredParentServices: string[] | null;
|
|
142
142
|
};
|
|
143
143
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { PikkuError } from '../../errors/error-handler.js';
|
|
2
|
+
import { checkAuthPermissions } from '../../permissions.js';
|
|
2
3
|
import { AIProviderNotConfiguredError } from '../../errors/errors.js';
|
|
3
4
|
import { pikkuState, getSingletonServices } from '../../pikku-state.js';
|
|
4
5
|
import { createMiddlewareSessionWireProps } from '../../services/user-session-service.js';
|
|
@@ -6,7 +7,6 @@ import { randomUUID } from 'crypto';
|
|
|
6
7
|
import { streamAIAgent } from './ai-agent-stream.js';
|
|
7
8
|
import { runAIAgent } from './ai-agent-runner.js';
|
|
8
9
|
import { resolveNamespace, ContextAwareRPCService, } from '../../wirings/rpc/rpc-runner.js';
|
|
9
|
-
import { buildDynamicWorkflowInstructions, buildWorkflowTools, } from './agent-dynamic-workflow.js';
|
|
10
10
|
import { resolveMemoryServices, loadContextMessages, trimMessages, } from './ai-agent-memory.js';
|
|
11
11
|
import { resolveModelConfig } from './ai-agent-model-config.js';
|
|
12
12
|
export class ToolApprovalRequired extends PikkuError {
|
|
@@ -75,6 +75,10 @@ export function getAddonCredentialRequirements(toolNames) {
|
|
|
75
75
|
return [...requirements.values()];
|
|
76
76
|
}
|
|
77
77
|
export const resolveAgent = (agentName) => {
|
|
78
|
+
if (!agentName) {
|
|
79
|
+
console.error('[resolveAgent] agentName is undefined/null! Stack:', new Error().stack);
|
|
80
|
+
throw new Error('resolveAgent called with undefined agentName');
|
|
81
|
+
}
|
|
78
82
|
const mainAgent = pikkuState(null, 'agent', 'agents').get(agentName);
|
|
79
83
|
if (mainAgent) {
|
|
80
84
|
return { agent: mainAgent, packageName: null, resolvedName: agentName };
|
|
@@ -100,10 +104,23 @@ export const resolveAgent = (agentName) => {
|
|
|
100
104
|
};
|
|
101
105
|
export async function buildInstructions(agentName, packageName) {
|
|
102
106
|
const meta = pikkuState(packageName, 'agent', 'agentsMeta')[agentName];
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
+
const parts = [];
|
|
108
|
+
if (meta?.role)
|
|
109
|
+
parts.push(meta.role);
|
|
110
|
+
if (meta?.personality)
|
|
111
|
+
parts.push(meta.personality);
|
|
112
|
+
if (meta?.goal)
|
|
113
|
+
parts.push(meta.goal);
|
|
114
|
+
let instructions = parts.join('\n\n');
|
|
115
|
+
if (meta?.tools?.length) {
|
|
116
|
+
instructions +=
|
|
117
|
+
'\n\nTool usage rules:\n' +
|
|
118
|
+
'- Act immediately with the information given. Do NOT ask clarifying questions unless a required field is truly missing.\n' +
|
|
119
|
+
'- Only use fields defined in your tool schemas. Never mention or ask for fields that do not exist.\n' +
|
|
120
|
+
'- Never fill optional fields with placeholder or zero values. Omit them entirely unless the user provides a real value.\n' +
|
|
121
|
+
'- Never stuff unrelated information into the wrong field.\n' +
|
|
122
|
+
'- Keep responses concise.';
|
|
123
|
+
}
|
|
107
124
|
if (meta?.agents?.length) {
|
|
108
125
|
instructions +=
|
|
109
126
|
'\n\nWhen calling a sub-agent, provide a short session name that describes the task. ' +
|
|
@@ -111,9 +128,6 @@ export async function buildInstructions(agentName, packageName) {
|
|
|
111
128
|
'Use a new session name for a new independent task. ' +
|
|
112
129
|
'When a request involves multiple actions for the same domain, combine them into a single sub-agent call rather than making separate calls.';
|
|
113
130
|
}
|
|
114
|
-
if (meta?.dynamicWorkflows && meta.tools?.length) {
|
|
115
|
-
instructions += buildDynamicWorkflowInstructions(meta.tools, meta.dynamicWorkflows);
|
|
116
|
-
}
|
|
117
131
|
return instructions;
|
|
118
132
|
}
|
|
119
133
|
export function createScopedChannel(parent, agentName, session) {
|
|
@@ -165,6 +179,10 @@ export async function buildToolDefs(params, agentSessionMap, resourceId, agentNa
|
|
|
165
179
|
const meta = pikkuState(packageName, 'agent', 'agentsMeta')[agentName];
|
|
166
180
|
if (!meta)
|
|
167
181
|
return { tools, missingRpcs };
|
|
182
|
+
// Get session for permission filtering
|
|
183
|
+
const session = params.sessionService
|
|
184
|
+
? await params.sessionService.get()
|
|
185
|
+
: null;
|
|
168
186
|
const metaTools = meta.tools;
|
|
169
187
|
const metaAgents = meta.agents;
|
|
170
188
|
if (metaTools?.length) {
|
|
@@ -196,6 +214,14 @@ export async function buildToolDefs(params, agentSessionMap, resourceId, agentNa
|
|
|
196
214
|
missingRpcs.push(toolName);
|
|
197
215
|
continue;
|
|
198
216
|
}
|
|
217
|
+
// Filter out tools the user doesn't have auth for
|
|
218
|
+
if (fnMeta.permissions?.length) {
|
|
219
|
+
if (!session)
|
|
220
|
+
continue;
|
|
221
|
+
const allowed = await checkAuthPermissions(fnMeta.permissions, session, singletonServices, resolvedPkg);
|
|
222
|
+
if (!allowed)
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
199
225
|
const inputSchemaName = fnMeta?.inputSchemaName;
|
|
200
226
|
let inputSchema = inputSchemaName
|
|
201
227
|
? schemas.get(inputSchemaName)
|
|
@@ -251,6 +277,14 @@ export async function buildToolDefs(params, agentSessionMap, resourceId, agentNa
|
|
|
251
277
|
singletonServices.logger.warn(`Sub-agent '${subAgentName}' not found in agent registry`);
|
|
252
278
|
continue;
|
|
253
279
|
}
|
|
280
|
+
// Filter out sub-agents the user doesn't have auth for
|
|
281
|
+
if (subMeta.permissions?.length) {
|
|
282
|
+
if (!session)
|
|
283
|
+
continue;
|
|
284
|
+
const allowed = await checkAuthPermissions(subMeta.permissions, session, singletonServices);
|
|
285
|
+
if (!allowed)
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
254
288
|
tools.push({
|
|
255
289
|
name: subAgentName,
|
|
256
290
|
description: subMeta.description,
|
|
@@ -339,10 +373,6 @@ export async function buildToolDefs(params, agentSessionMap, resourceId, agentNa
|
|
|
339
373
|
});
|
|
340
374
|
}
|
|
341
375
|
}
|
|
342
|
-
if (meta.dynamicWorkflows) {
|
|
343
|
-
const workflowTools = buildWorkflowTools(agentName, packageName, meta.tools ?? [], meta.dynamicWorkflows, streamContext, params.sessionService);
|
|
344
|
-
tools.push(...workflowTools);
|
|
345
|
-
}
|
|
346
376
|
const hasToolHooks = aiMiddlewares?.some((mw) => mw.beforeToolCall || mw.afterToolCall);
|
|
347
377
|
if (hasToolHooks) {
|
|
348
378
|
for (const tool of tools) {
|
|
@@ -405,15 +435,6 @@ export async function prepareAgentRun(agentName, input, params, agentSessionMap,
|
|
|
405
435
|
if (!agentRunner) {
|
|
406
436
|
throw new AIProviderNotConfiguredError();
|
|
407
437
|
}
|
|
408
|
-
if (agent.dynamicWorkflows && singletonServices.workflowService) {
|
|
409
|
-
const persisted = await singletonServices.workflowService.getAIGeneratedWorkflows(resolvedName);
|
|
410
|
-
const allMeta = pikkuState(null, 'workflows', 'meta');
|
|
411
|
-
for (const wf of persisted) {
|
|
412
|
-
if (!allMeta[wf.workflowName]) {
|
|
413
|
-
allMeta[wf.workflowName] = wf.graph;
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
438
|
const { storage } = resolveMemoryServices(agent, singletonServices);
|
|
418
439
|
const memoryConfig = agent.memory;
|
|
419
440
|
const threadId = input.threadId;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { pikkuState } from '../../pikku-state.js';
|
|
2
|
-
import { PikkuMissingMetaError } from '../../errors/errors.js';
|
|
3
2
|
export const addAIAgent = (agentName, agent, packageName = null) => {
|
|
4
3
|
const agentsMeta = pikkuState(packageName, 'agent', 'agentsMeta');
|
|
5
4
|
const agentMeta = agentsMeta[agentName];
|
|
6
5
|
if (!agentMeta) {
|
|
7
|
-
|
|
6
|
+
console.warn(`[pikku] Skipping AI agent '${agentName}' — metadata not found. Consider moving this wiring to its own file.`);
|
|
7
|
+
return;
|
|
8
8
|
}
|
|
9
9
|
const agents = pikkuState(packageName, 'agent', 'agents');
|
|
10
10
|
if (agents.has(agentName)) {
|
|
@@ -4,6 +4,21 @@ import { checkForApprovals, appendStepMessages } from './ai-agent-stream.js';
|
|
|
4
4
|
import { pikkuState, getSingletonServices } from '../../pikku-state.js';
|
|
5
5
|
import { resolveModelConfig } from './ai-agent-model-config.js';
|
|
6
6
|
import { AIProviderNotConfiguredError } from '../../errors/errors.js';
|
|
7
|
+
function stripNulls(obj) {
|
|
8
|
+
if (obj === null)
|
|
9
|
+
return undefined;
|
|
10
|
+
if (Array.isArray(obj))
|
|
11
|
+
return obj.map(stripNulls);
|
|
12
|
+
if (typeof obj !== 'object')
|
|
13
|
+
return obj;
|
|
14
|
+
const result = {};
|
|
15
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
16
|
+
if (value !== null) {
|
|
17
|
+
result[key] = stripNulls(value);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
7
22
|
import { randomUUID } from 'crypto';
|
|
8
23
|
export async function runAIAgent(agentName, input, params, agentSessionMap) {
|
|
9
24
|
const sessionMap = agentSessionMap ?? new Map();
|
|
@@ -287,9 +302,11 @@ export async function resumeAIAgentSync(runId, approvals, params, expectedAgentN
|
|
|
287
302
|
if (!matchingTool) {
|
|
288
303
|
throw new Error(`Tool "${pending.toolName}" not found in agent definition`);
|
|
289
304
|
}
|
|
290
|
-
const
|
|
305
|
+
const rawArgs = typeof pending.args === 'string'
|
|
291
306
|
? JSON.parse(pending.args)
|
|
292
307
|
: pending.args;
|
|
308
|
+
// Strip null values recursively — LLMs send null for optional fields but Zod expects undefined
|
|
309
|
+
const toolArgs = stripNulls(rawArgs) ?? {};
|
|
293
310
|
try {
|
|
294
311
|
const toolResult = await matchingTool.execute(toolArgs);
|
|
295
312
|
resultStr =
|
|
@@ -733,6 +733,7 @@ async function continueAfterToolResult(run, agent, packageName, resolvedName, st
|
|
|
733
733
|
const streamContext = { channel, options };
|
|
734
734
|
const resumeTools = (await buildToolDefs(params, new Map(), run.resourceId, resolvedName, packageName, streamContext, aiMiddlewares)).tools;
|
|
735
735
|
const resolved = resolveModelConfig(resolvedName, agent);
|
|
736
|
+
console.log('[DEBUG resume] resolvedName:', resolvedName, 'agent.model:', agent.model, 'resolved.model:', resolved.model);
|
|
736
737
|
const maxSteps = resolved.maxSteps ?? 10;
|
|
737
738
|
const runnerParams = {
|
|
738
739
|
model: resolved.model,
|
|
@@ -183,7 +183,9 @@ export type CoreAIAgent<PikkuPermission = CorePikkuPermission<any, any>, PikkuMi
|
|
|
183
183
|
description: string;
|
|
184
184
|
summary?: string;
|
|
185
185
|
errors?: string[];
|
|
186
|
-
|
|
186
|
+
role?: string;
|
|
187
|
+
personality?: string;
|
|
188
|
+
goal: string;
|
|
187
189
|
model: string;
|
|
188
190
|
temperature?: number;
|
|
189
191
|
tools?: unknown[];
|
|
@@ -192,7 +194,6 @@ export type CoreAIAgent<PikkuPermission = CorePikkuPermission<any, any>, PikkuMi
|
|
|
192
194
|
memory?: AIAgentMemoryConfig;
|
|
193
195
|
maxSteps?: number;
|
|
194
196
|
toolChoice?: 'auto' | 'required' | 'none';
|
|
195
|
-
dynamicWorkflows?: 'read' | 'always' | 'ask';
|
|
196
197
|
input?: unknown;
|
|
197
198
|
output?: unknown;
|
|
198
199
|
tags?: string[];
|
|
@@ -382,5 +383,6 @@ export type AIAgentMeta = Record<string, Omit<CoreAIAgent, 'input' | 'output' |
|
|
|
382
383
|
channelMiddleware?: MiddlewareMetadata[];
|
|
383
384
|
aiMiddleware?: MiddlewareMetadata[];
|
|
384
385
|
permissions?: PermissionMetadata[];
|
|
385
|
-
|
|
386
|
+
sourceFile?: string;
|
|
387
|
+
exportedName?: string;
|
|
386
388
|
}>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { NotFoundError
|
|
1
|
+
import { NotFoundError } from '../../errors/errors.js';
|
|
2
2
|
import { addFunction } from '../../function/function-runner.js';
|
|
3
3
|
import { pikkuState, getSingletonServices } from '../../pikku-state.js';
|
|
4
4
|
import { coerceTopLevelDataFromSchema, validateSchema } from '../../schema.js';
|
|
@@ -12,9 +12,9 @@ export const wireChannel = (channel) => {
|
|
|
12
12
|
const channelsMeta = pikkuState(null, 'channel', 'meta');
|
|
13
13
|
const channelMeta = channelsMeta[channel.name];
|
|
14
14
|
if (!channelMeta) {
|
|
15
|
-
|
|
15
|
+
console.warn(`[pikku] Skipping channel '${channel.name}' — metadata not found. Consider moving this wiring to its own file.`);
|
|
16
|
+
return;
|
|
16
17
|
}
|
|
17
|
-
pikkuState(null, 'channel', 'channels').set(channel.name, channel);
|
|
18
18
|
// Register onConnect function if provided
|
|
19
19
|
if (channel.onConnect && channelMeta.connect) {
|
|
20
20
|
addFunction(channelMeta.connect.pikkuFuncId, channel.onConnect);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { NotFoundError
|
|
1
|
+
import { NotFoundError } from '../../errors/errors.js';
|
|
2
2
|
import { addFunction, runPikkuFunc } from '../../function/function-runner.js';
|
|
3
3
|
import { pikkuState } from '../../pikku-state.js';
|
|
4
4
|
import { PikkuSessionService, createMiddlewareSessionWireProps, } from '../../services/user-session-service.js';
|
|
@@ -29,7 +29,8 @@ export const wireCLI = (cli) => {
|
|
|
29
29
|
// Get the existing metadata that was generated during inspection
|
|
30
30
|
const cliMeta = pikkuState(null, 'cli', 'meta') || {};
|
|
31
31
|
if (!cliMeta.programs?.[cli.program]) {
|
|
32
|
-
|
|
32
|
+
console.warn(`[pikku] Skipping CLI program '${cli.program}' — metadata not found. Consider moving this wiring to its own file.`);
|
|
33
|
+
return;
|
|
33
34
|
}
|
|
34
35
|
// Get existing programs state and add this program
|
|
35
36
|
const programs = pikkuState(null, 'cli', 'programs') || {};
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/** Convert kebab-case to camelCase: "from-plan" → "fromPlan" */
|
|
2
|
+
function toCamelCase(str) {
|
|
3
|
+
return str.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
|
|
4
|
+
}
|
|
1
5
|
/**
|
|
2
6
|
* Parses raw CLI arguments into structured data for a specific program
|
|
3
7
|
*/
|
|
@@ -81,11 +85,11 @@ export function parseCLIArguments(args, programName, allMeta) {
|
|
|
81
85
|
while (currentIndex < args.length) {
|
|
82
86
|
const arg = args[currentIndex];
|
|
83
87
|
if (arg.startsWith('--')) {
|
|
84
|
-
// Long option
|
|
88
|
+
// Long option (--from-plan → fromPlan)
|
|
85
89
|
const equalIndex = arg.indexOf('=');
|
|
86
90
|
if (equalIndex > 0) {
|
|
87
91
|
// --option=value format
|
|
88
|
-
const key = arg.slice(2, equalIndex);
|
|
92
|
+
const key = toCamelCase(arg.slice(2, equalIndex));
|
|
89
93
|
const optionDef = availableOptions[key];
|
|
90
94
|
// Unknown options are allowed for forward compatibility
|
|
91
95
|
const value = arg.slice(equalIndex + 1);
|
|
@@ -93,7 +97,7 @@ export function parseCLIArguments(args, programName, allMeta) {
|
|
|
93
97
|
}
|
|
94
98
|
else {
|
|
95
99
|
// --option value format
|
|
96
|
-
const key = arg.slice(2);
|
|
100
|
+
const key = toCamelCase(arg.slice(2));
|
|
97
101
|
const optionDef = availableOptions[key];
|
|
98
102
|
// Unknown options are allowed for forward compatibility
|
|
99
103
|
if (optionDef && optionDef.array) {
|
|
@@ -136,4 +136,4 @@ export declare const pikkuFetch: <In, Out>(request: Request | PikkuHTTPRequest,
|
|
|
136
136
|
* @param {RunHTTPWiringOptions} options - Options such as singleton services, session handling, and error configuration.
|
|
137
137
|
* @returns {Promise<Out | void>} The output from the route handler or void if an error occurred.
|
|
138
138
|
*/
|
|
139
|
-
export declare const fetchData: <In, Out>(request: Request | PikkuHTTPRequest, response: PikkuHTTPResponse, { skipUserSession, respondWith404, logWarningsForStatusCodes, coerceDataFromSchema, bubbleErrors, exposeErrors, generateRequestId, }?: RunHTTPWiringOptions) => Promise<Out | void>;
|
|
139
|
+
export declare const fetchData: <In, Out>(request: Request | PikkuHTTPRequest, response: PikkuHTTPResponse, { skipUserSession, respondWith404, logWarningsForStatusCodes, coerceDataFromSchema, bubbleErrors, exposeErrors, generateRequestId, traceId: externalTraceId, }?: RunHTTPWiringOptions) => Promise<Out | void>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { NotFoundError
|
|
1
|
+
import { NotFoundError } from '../../errors/errors.js';
|
|
2
2
|
import { closeWireServices, createWeakUID, isSerializable, } from '../../utils.js';
|
|
3
3
|
import { getSingletonServices, getCreateWireServices, } from '../../pikku-state.js';
|
|
4
4
|
import { PikkuSessionService } from '../../services/user-session-service.js';
|
|
@@ -112,7 +112,12 @@ export const wireHTTP = (httpWiring) => {
|
|
|
112
112
|
const httpMeta = pikkuState(null, 'http', 'meta');
|
|
113
113
|
const routeMeta = httpMeta[httpWiring.method][httpWiring.route];
|
|
114
114
|
if (!routeMeta) {
|
|
115
|
-
|
|
115
|
+
// In deploy units with filtered metadata, wiring files may include
|
|
116
|
+
// routes for functions not in this unit. This happens when multiple
|
|
117
|
+
// wirings share a file — split them into separate files for better
|
|
118
|
+
// tree-shaking.
|
|
119
|
+
console.warn(`[pikku] Skipping HTTP route '${httpWiring.method.toUpperCase()} ${httpWiring.route}' — metadata not found. Consider moving this wiring to its own file.`);
|
|
120
|
+
return;
|
|
116
121
|
}
|
|
117
122
|
if (httpWiring.func) {
|
|
118
123
|
addFunction(routeMeta.pikkuFuncId, httpWiring.func);
|
|
@@ -251,6 +256,7 @@ const executeRoute = async (services, matchedRoute, http, options) => {
|
|
|
251
256
|
};
|
|
252
257
|
}
|
|
253
258
|
const wire = {
|
|
259
|
+
traceId: requestId,
|
|
254
260
|
http,
|
|
255
261
|
channel,
|
|
256
262
|
session: userSession.get(),
|
|
@@ -345,19 +351,25 @@ export const pikkuFetch = async (request, params = {}) => {
|
|
|
345
351
|
* @param {RunHTTPWiringOptions} options - Options such as singleton services, session handling, and error configuration.
|
|
346
352
|
* @returns {Promise<Out | void>} The output from the route handler or void if an error occurred.
|
|
347
353
|
*/
|
|
348
|
-
export const fetchData = async (request, response, { skipUserSession = false, respondWith404 = true, logWarningsForStatusCodes = [], coerceDataFromSchema = true, bubbleErrors = false, exposeErrors = false, generateRequestId, } = {}) => {
|
|
354
|
+
export const fetchData = async (request, response, { skipUserSession = false, respondWith404 = true, logWarningsForStatusCodes = [], coerceDataFromSchema = true, bubbleErrors = false, exposeErrors = false, generateRequestId, traceId: externalTraceId, } = {}) => {
|
|
349
355
|
const singletonServices = getSingletonServices();
|
|
350
356
|
const createWireServices = getCreateWireServices();
|
|
351
357
|
let wireServices;
|
|
352
358
|
let result;
|
|
353
359
|
// Combine the request and response into one wire object
|
|
354
360
|
const pikkuRequest = request instanceof Request ? new PikkuFetchHTTPRequest(request) : request;
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
361
|
+
// Resolve traceId: external (e.g. CF-Ray) > x-request-id header > generated
|
|
362
|
+
let requestId = externalTraceId ?? null;
|
|
363
|
+
if (!requestId) {
|
|
364
|
+
try {
|
|
365
|
+
requestId = pikkuRequest.header('x-request-id');
|
|
366
|
+
}
|
|
367
|
+
catch { }
|
|
358
368
|
}
|
|
359
|
-
catch { }
|
|
360
369
|
requestId = requestId || generateRequestId?.() || createWeakUID();
|
|
370
|
+
// Scoped logger for HTTP runner internal logging (error handling, route matching)
|
|
371
|
+
// Functions/middleware receive singletonServices.logger directly for compatibility
|
|
372
|
+
const scopedLogger = singletonServices.logger.scope?.(requestId) ?? singletonServices.logger;
|
|
361
373
|
const http = createHTTPWire(pikkuRequest, response);
|
|
362
374
|
const apiType = http.request.method();
|
|
363
375
|
const apiRoute = http.request.path();
|
|
@@ -375,7 +387,7 @@ export const fetchData = async (request, response, { skipUserSession = false, re
|
|
|
375
387
|
response.status(204).json(undefined);
|
|
376
388
|
return;
|
|
377
389
|
}
|
|
378
|
-
|
|
390
|
+
scopedLogger.info({
|
|
379
391
|
message: 'Route not found',
|
|
380
392
|
apiRoute,
|
|
381
393
|
apiType,
|
|
@@ -395,7 +407,7 @@ export const fetchData = async (request, response, { skipUserSession = false, re
|
|
|
395
407
|
catch (e) {
|
|
396
408
|
if (matchedRoute?.route.sse) {
|
|
397
409
|
// For SSE routes, send error through the stream since the response is already in stream mode
|
|
398
|
-
|
|
410
|
+
scopedLogger.error(e instanceof Error ? e.message : e);
|
|
399
411
|
try {
|
|
400
412
|
const errorResponse = getErrorResponse(e);
|
|
401
413
|
response.arrayBuffer(JSON.stringify({
|
|
@@ -405,12 +417,12 @@ export const fetchData = async (request, response, { skipUserSession = false, re
|
|
|
405
417
|
response.arrayBuffer(JSON.stringify({ type: 'done' }));
|
|
406
418
|
}
|
|
407
419
|
catch (streamErr) {
|
|
408
|
-
|
|
420
|
+
scopedLogger.error(`SSE error while sending error payload: ${streamErr instanceof Error ? streamErr.message : String(streamErr)}`);
|
|
409
421
|
}
|
|
410
422
|
response.close?.();
|
|
411
423
|
}
|
|
412
424
|
else {
|
|
413
|
-
handleHTTPError(e, http, requestId,
|
|
425
|
+
handleHTTPError(e, http, requestId, scopedLogger, logWarningsForStatusCodes, respondWith404, bubbleErrors, exposeErrors);
|
|
414
426
|
}
|
|
415
427
|
}
|
|
416
428
|
finally {
|
|
@@ -17,6 +17,8 @@ export type RunHTTPWiringOptions = Partial<{
|
|
|
17
17
|
bubbleErrors: boolean;
|
|
18
18
|
exposeErrors: boolean;
|
|
19
19
|
generateRequestId: () => string;
|
|
20
|
+
/** Pre-resolved trace ID (e.g. CF-Ray). Falls back to x-request-id header or generated ID. */
|
|
21
|
+
traceId: string;
|
|
20
22
|
}>;
|
|
21
23
|
/**
|
|
22
24
|
* Represents the HTTP methods supported for API HTTP wirings.
|
|
@@ -3,7 +3,7 @@ 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
5
|
import { resolveNamespace } from '../rpc/rpc-runner.js';
|
|
6
|
-
import { BadRequestError, NotFoundError
|
|
6
|
+
import { BadRequestError, NotFoundError } from '../../errors/errors.js';
|
|
7
7
|
import { PikkuSessionService, createMiddlewareSessionWireProps, } from '../../services/user-session-service.js';
|
|
8
8
|
export class MCPError extends Error {
|
|
9
9
|
error;
|
|
@@ -18,7 +18,8 @@ export const wireMCPResource = (mcpResource) => {
|
|
|
18
18
|
const resourcesMeta = pikkuState(null, 'mcp', 'resourcesMeta');
|
|
19
19
|
const mcpResourceMeta = resourcesMeta[mcpResource.uri];
|
|
20
20
|
if (!mcpResourceMeta) {
|
|
21
|
-
|
|
21
|
+
console.warn(`[pikku] Skipping MCP resource '${mcpResource.uri}' — metadata not found. Consider moving this wiring to its own file.`);
|
|
22
|
+
return;
|
|
22
23
|
}
|
|
23
24
|
addFunction(mcpResourceMeta.pikkuFuncId, mcpResource.func);
|
|
24
25
|
const resources = pikkuState(null, 'mcp', 'resources');
|
|
@@ -31,7 +32,8 @@ export const wireMCPPrompt = (mcpPrompt) => {
|
|
|
31
32
|
const promptsMeta = pikkuState(null, 'mcp', 'promptsMeta');
|
|
32
33
|
const mcpPromptMeta = promptsMeta[mcpPrompt.name];
|
|
33
34
|
if (!mcpPromptMeta) {
|
|
34
|
-
|
|
35
|
+
console.warn(`[pikku] Skipping MCP prompt '${mcpPrompt.name}' — metadata not found. Consider moving this wiring to its own file.`);
|
|
36
|
+
return;
|
|
35
37
|
}
|
|
36
38
|
addFunction(mcpPromptMeta.pikkuFuncId, mcpPrompt.func);
|
|
37
39
|
const prompts = pikkuState(null, 'mcp', 'prompts');
|
|
@@ -28,7 +28,9 @@ export declare function removeQueueWorker(name: string): Promise<void>;
|
|
|
28
28
|
/**
|
|
29
29
|
* Process a single queue job - this function is called by queue consumers
|
|
30
30
|
*/
|
|
31
|
-
export declare function runQueueJob({ job, updateProgress, }: {
|
|
31
|
+
export declare function runQueueJob({ job, updateProgress, traceId, }: {
|
|
32
32
|
job: QueueJob;
|
|
33
33
|
updateProgress?: (progress: number | string | object) => Promise<void>;
|
|
34
|
+
/** Pre-resolved trace ID (e.g. from queue message metadata) */
|
|
35
|
+
traceId?: string;
|
|
34
36
|
}): Promise<void>;
|
|
@@ -36,7 +36,8 @@ export const wireQueueWorker = (queueWorker) => {
|
|
|
36
36
|
const meta = pikkuState(null, 'queue', 'meta');
|
|
37
37
|
const processorMeta = meta[queueWorker.name];
|
|
38
38
|
if (!processorMeta) {
|
|
39
|
-
|
|
39
|
+
console.warn(`[pikku] Skipping queue worker '${queueWorker.name}' — metadata not found. Consider moving this wiring to its own file.`);
|
|
40
|
+
return;
|
|
40
41
|
}
|
|
41
42
|
// Register the function with pikku
|
|
42
43
|
addFunction(processorMeta.pikkuFuncId, {
|
|
@@ -70,10 +71,12 @@ export async function removeQueueWorker(name) {
|
|
|
70
71
|
/**
|
|
71
72
|
* Process a single queue job - this function is called by queue consumers
|
|
72
73
|
*/
|
|
73
|
-
export async function runQueueJob({ job, updateProgress, }) {
|
|
74
|
+
export async function runQueueJob({ job, updateProgress, traceId, }) {
|
|
74
75
|
const singletonServices = getSingletonServices();
|
|
75
76
|
const createWireServices = getCreateWireServices();
|
|
76
|
-
const
|
|
77
|
+
const resolvedTraceId = traceId ?? `q-${job.id}`;
|
|
78
|
+
const logger = singletonServices.logger.scope?.(resolvedTraceId) ??
|
|
79
|
+
singletonServices.logger;
|
|
77
80
|
const meta = pikkuState(null, 'queue', 'meta');
|
|
78
81
|
const processorMeta = meta[job.queueName];
|
|
79
82
|
if (!processorMeta) {
|
|
@@ -105,6 +108,7 @@ export async function runQueueJob({ job, updateProgress, }) {
|
|
|
105
108
|
try {
|
|
106
109
|
logger.info(`Processing job ${job.id} in queue ${job.queueName}`);
|
|
107
110
|
const wire = {
|
|
111
|
+
traceId: resolvedTraceId,
|
|
108
112
|
queue,
|
|
109
113
|
};
|
|
110
114
|
// Execute the pikku function with the job data
|