@polka-codes/core 0.9.84 → 0.9.86

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.
@@ -31,7 +31,7 @@ declare type AgentToolRegistry = {
31
31
  invokeTool: {
32
32
  input: {
33
33
  toolName: string;
34
- input: any;
34
+ input: unknown;
35
35
  };
36
36
  output: ToolResponse;
37
37
  };
@@ -60,7 +60,9 @@ export { AgentWorkflowInput }
60
60
  export { AgentWorkflowInput as AgentWorkflowInput_alias_1 }
61
61
  export { AgentWorkflowInput as AgentWorkflowInput_alias_2 }
62
62
 
63
- declare type BreakStep = z.infer<typeof BreakStepSchema>;
63
+ declare interface BreakStep {
64
+ break: true;
65
+ }
64
66
  export { BreakStep }
65
67
  export { BreakStep as BreakStep_alias_1 }
66
68
  export { BreakStep as BreakStep_alias_2 }
@@ -213,7 +215,9 @@ declare const configSchema: z.ZodOptional<z.ZodNullable<z.ZodObject<{
213
215
  export { configSchema }
214
216
  export { configSchema as configSchema_alias_1 }
215
217
 
216
- declare type ContinueStep = z.infer<typeof ContinueStepSchema>;
218
+ declare interface ContinueStep {
219
+ continue: true;
220
+ }
217
221
  export { ContinueStep }
218
222
  export { ContinueStep as ContinueStep_alias_1 }
219
223
  export { ContinueStep as ContinueStep_alias_2 }
@@ -228,12 +232,24 @@ export { ContinueStepSchema }
228
232
  export { ContinueStepSchema as ContinueStepSchema_alias_1 }
229
233
  export { ContinueStepSchema as ContinueStepSchema_alias_2 }
230
234
 
235
+ /**
236
+ * Convert a JSON Schema to a Zod schema
237
+ * Supports a subset of JSON SchemaDraft 7
238
+ *
239
+ * This is exported to allow reuse in other parts of the codebase that need to
240
+ * convert JSON schemas to Zod schemas (e.g., MCP server tool schema conversion).
241
+ */
242
+ declare function convertJsonSchemaToZod(schema: JsonSchema): z.ZodTypeAny;
243
+ export { convertJsonSchemaToZod }
244
+ export { convertJsonSchemaToZod as convertJsonSchemaToZod_alias_1 }
245
+ export { convertJsonSchemaToZod as convertJsonSchemaToZod_alias_2 }
246
+
231
247
  declare function createContext<TTools extends ToolRegistry>(tools: WorkflowTools<TTools>, stepFn?: StepFn, logger?: Logger): WorkflowContext<TTools>;
232
248
  export { createContext }
233
249
  export { createContext as createContext_alias_1 }
234
250
  export { createContext as createContext_alias_2 }
235
251
 
236
- declare function createDynamicWorkflow<TTools extends ToolRegistry = DynamicWorkflowRegistry>(definition: WorkflowFile | string, options?: DynamicWorkflowRunnerOptions): (workflowId: string, input: Record<string, any>, context: WorkflowContext<TTools>) => Promise<any>;
252
+ declare function createDynamicWorkflow<TTools extends ToolRegistry = DynamicWorkflowRegistry>(definition: WorkflowFile | string, options?: DynamicWorkflowRunnerOptions): (workflowId: string, input: Record<string, unknown>, context: WorkflowContext<TTools>) => Promise<unknown>;
237
253
  export { createDynamicWorkflow }
238
254
  export { createDynamicWorkflow as createDynamicWorkflow_alias_1 }
239
255
  export { createDynamicWorkflow as createDynamicWorkflow_alias_2 }
@@ -511,14 +527,14 @@ export declare function dirname(path: string): string;
511
527
  declare type DynamicStepRuntimeContext<TTools extends ToolRegistry> = {
512
528
  workflowId: string;
513
529
  stepId: string;
514
- input: Record<string, any>;
515
- state: Record<string, any>;
530
+ input: Record<string, unknown>;
531
+ state: Record<string, unknown>;
516
532
  tools: WorkflowTools<TTools>;
517
533
  logger: Logger;
518
534
  step: StepFn;
519
- runWorkflow: (workflowId: string, input?: Record<string, any>) => Promise<any>;
535
+ runWorkflow: (workflowId: string, input?: Record<string, unknown>) => Promise<unknown>;
520
536
  toolInfo: Readonly<FullToolInfo[]> | undefined;
521
- agentTools: Record<string, (input: any) => Promise<any>>;
537
+ agentTools: Record<string, (input: unknown) => Promise<unknown>>;
522
538
  };
523
539
  export { DynamicStepRuntimeContext }
524
540
  export { DynamicStepRuntimeContext as DynamicStepRuntimeContext_alias_1 }
@@ -562,8 +578,8 @@ declare type DynamicWorkflowRunnerOptions = {
562
578
  stepSystemPrompt?: (args: {
563
579
  workflowId: string;
564
580
  step: WorkflowStepDefinition;
565
- input: any;
566
- state: any;
581
+ input: Record<string, unknown>;
582
+ state: Record<string, unknown>;
567
583
  }) => string;
568
584
  /**
569
585
  * Whether to wrap plain text agent responses in an object { result: ... }.
@@ -748,7 +764,15 @@ export declare const handler_alias_8: ToolHandler<typeof toolInfo_alias_8, Files
748
764
 
749
765
  export declare const handler_alias_9: ToolHandler<typeof toolInfo_alias_9, WebProvider>;
750
766
 
751
- declare type IfElseStep = z.infer<typeof IfElseStepSchema>;
767
+ declare interface IfElseStep {
768
+ id: string;
769
+ if: {
770
+ condition: string;
771
+ thenBranch: WorkflowControlFlowStep[];
772
+ elseBranch?: WorkflowControlFlowStep[];
773
+ };
774
+ output?: string | null;
775
+ }
752
776
  export { IfElseStep }
753
777
  export { IfElseStep as IfElseStep_alias_1 }
754
778
  export { IfElseStep as IfElseStep_alias_2 }
@@ -756,7 +780,7 @@ export { IfElseStep as IfElseStep_alias_2 }
756
780
  /**
757
781
  * If/else branch - conditionally executes steps
758
782
  */
759
- declare const IfElseStepSchema: any;
783
+ declare const IfElseStepSchema: z.ZodType<IfElseStep>;
760
784
  export { IfElseStepSchema }
761
785
  export { IfElseStepSchema as IfElseStepSchema_alias_1 }
762
786
  export { IfElseStepSchema as IfElseStepSchema_alias_2 }
@@ -872,6 +896,27 @@ export { JsonResponseMessage }
872
896
  export { JsonResponseMessage as JsonResponseMessage_alias_1 }
873
897
  export { JsonResponseMessage as JsonResponseMessage_alias_2 }
874
898
 
899
+ declare interface JsonSchema {
900
+ type?: JsonSchemaType | JsonSchemaType[];
901
+ enum?: JsonSchemaEnum;
902
+ properties?: Record<string, JsonSchema>;
903
+ required?: string[];
904
+ items?: JsonSchema;
905
+ additionalProperties?: boolean | JsonSchema;
906
+ description?: string;
907
+ [key: string]: unknown;
908
+ }
909
+
910
+ /**
911
+ * JSON Schema enum values - can be string, number, or boolean
912
+ */
913
+ declare type JsonSchemaEnum = (string | number | boolean)[];
914
+
915
+ /**
916
+ * JSON Schema type to Zod type mapping
917
+ */
918
+ declare type JsonSchemaType = 'string' | 'number' | 'integer' | 'boolean' | 'object' | 'array' | 'null';
919
+
875
920
  declare interface JsonToolCallPart {
876
921
  type: 'tool-call';
877
922
  /**
@@ -1347,12 +1392,15 @@ declare const ruleSchema: z.ZodUnion<readonly [z.ZodString, z.ZodObject<{
1347
1392
  export { ruleSchema }
1348
1393
  export { ruleSchema as ruleSchema_alias_1 }
1349
1394
 
1395
+ /**
1396
+ * Type for the runWorkflow tool that allows workflows to call other workflows
1397
+ */
1350
1398
  declare type RunWorkflowTool = {
1351
1399
  input: {
1352
1400
  workflowId: string;
1353
- input?: any;
1401
+ input?: Record<string, unknown>;
1354
1402
  };
1355
- output: any;
1403
+ output: unknown;
1356
1404
  };
1357
1405
  export { RunWorkflowTool }
1358
1406
  export { RunWorkflowTool as RunWorkflowTool_alias_1 }
@@ -2008,7 +2056,14 @@ declare type Totals = {
2008
2056
  messageCount: number;
2009
2057
  };
2010
2058
 
2011
- declare type TryCatchStep = z.infer<typeof TryCatchStepSchema>;
2059
+ declare interface TryCatchStep {
2060
+ id: string;
2061
+ try: {
2062
+ trySteps: WorkflowControlFlowStep[];
2063
+ catchSteps: WorkflowControlFlowStep[];
2064
+ };
2065
+ output?: string | null;
2066
+ }
2012
2067
  export { TryCatchStep }
2013
2068
  export { TryCatchStep as TryCatchStep_alias_1 }
2014
2069
  export { TryCatchStep as TryCatchStep_alias_2 }
@@ -2019,8 +2074,8 @@ export { TryCatchStep as TryCatchStep_alias_2 }
2019
2074
  declare const TryCatchStepSchema: z.ZodObject<{
2020
2075
  id: z.ZodString;
2021
2076
  try: z.ZodObject<{
2022
- trySteps: z.ZodArray<z.ZodLazy<any>>;
2023
- catchSteps: z.ZodArray<z.ZodLazy<any>>;
2077
+ trySteps: z.ZodArray<z.ZodLazy<z.ZodType<WorkflowControlFlowStep, unknown, z.core.$ZodTypeInternals<WorkflowControlFlowStep, unknown>>>>;
2078
+ catchSteps: z.ZodArray<z.ZodLazy<z.ZodType<WorkflowControlFlowStep, unknown, z.core.$ZodTypeInternals<WorkflowControlFlowStep, unknown>>>>;
2024
2079
  }, z.core.$strip>;
2025
2080
  output: z.ZodOptional<z.ZodNullable<z.ZodString>>;
2026
2081
  }, z.core.$strip>;
@@ -2174,7 +2229,14 @@ export { WebProvider }
2174
2229
  export { WebProvider as WebProvider_alias_1 }
2175
2230
  export { WebProvider as WebProvider_alias_2 }
2176
2231
 
2177
- declare type WhileLoopStep = z.infer<typeof WhileLoopStepSchema>;
2232
+ declare interface WhileLoopStep {
2233
+ id: string;
2234
+ while: {
2235
+ condition: string;
2236
+ steps: WorkflowControlFlowStep[];
2237
+ };
2238
+ output?: string | null;
2239
+ }
2178
2240
  export { WhileLoopStep }
2179
2241
  export { WhileLoopStep as WhileLoopStep_alias_1 }
2180
2242
  export { WhileLoopStep as WhileLoopStep_alias_2 }
@@ -2182,7 +2244,7 @@ export { WhileLoopStep as WhileLoopStep_alias_2 }
2182
2244
  /**
2183
2245
  * While loop - repeats steps while condition is true
2184
2246
  */
2185
- declare const WhileLoopStepSchema: any;
2247
+ declare const WhileLoopStepSchema: z.ZodType<WhileLoopStep>;
2186
2248
  export { WhileLoopStepSchema }
2187
2249
  export { WhileLoopStepSchema as WhileLoopStepSchema_alias_1 }
2188
2250
  export { WhileLoopStepSchema as WhileLoopStepSchema_alias_2 }
@@ -2196,7 +2258,7 @@ export { WorkflowContext }
2196
2258
  export { WorkflowContext as WorkflowContext_alias_1 }
2197
2259
  export { WorkflowContext as WorkflowContext_alias_2 }
2198
2260
 
2199
- declare type WorkflowControlFlowStep = z.infer<typeof WorkflowControlFlowStepSchema>;
2261
+ declare type WorkflowControlFlowStep = z.infer<typeof WorkflowStepDefinitionSchema> | WhileLoopStep | IfElseStep | BreakStep | ContinueStep | TryCatchStep;
2200
2262
  export { WorkflowControlFlowStep }
2201
2263
  export { WorkflowControlFlowStep as WorkflowControlFlowStep_alias_1 }
2202
2264
  export { WorkflowControlFlowStep as WorkflowControlFlowStep_alias_2 }
@@ -2205,7 +2267,7 @@ export { WorkflowControlFlowStep as WorkflowControlFlowStep_alias_2 }
2205
2267
  * Any step that can appear in a workflow's steps array
2206
2268
  * Can be a basic step, control flow, or jump statement
2207
2269
  */
2208
- declare const WorkflowControlFlowStepSchema: any;
2270
+ declare const WorkflowControlFlowStepSchema: z.ZodType<WorkflowControlFlowStep>;
2209
2271
  export { WorkflowControlFlowStepSchema }
2210
2272
  export { WorkflowControlFlowStepSchema as WorkflowControlFlowStepSchema_alias_1 }
2211
2273
  export { WorkflowControlFlowStepSchema as WorkflowControlFlowStepSchema_alias_2 }
@@ -2225,7 +2287,7 @@ declare const WorkflowDefinitionSchema: z.ZodObject<{
2225
2287
  description: z.ZodOptional<z.ZodNullable<z.ZodString>>;
2226
2288
  default: z.ZodOptional<z.ZodNullable<z.ZodAny>>;
2227
2289
  }, z.core.$strip>>>>;
2228
- steps: z.ZodArray<any>;
2290
+ steps: z.ZodArray<z.ZodType<WorkflowControlFlowStep, unknown, z.core.$ZodTypeInternals<WorkflowControlFlowStep, unknown>>>;
2229
2291
  output: z.ZodOptional<z.ZodNullable<z.ZodString>>;
2230
2292
  }, z.core.$strip>;
2231
2293
  export { WorkflowDefinitionSchema }
@@ -2245,7 +2307,7 @@ declare const WorkflowFileSchema: z.ZodObject<{
2245
2307
  description: z.ZodOptional<z.ZodNullable<z.ZodString>>;
2246
2308
  default: z.ZodOptional<z.ZodNullable<z.ZodAny>>;
2247
2309
  }, z.core.$strip>>>>;
2248
- steps: z.ZodArray<any>;
2310
+ steps: z.ZodArray<z.ZodType<WorkflowControlFlowStep, unknown, z.core.$ZodTypeInternals<WorkflowControlFlowStep, unknown>>>;
2249
2311
  output: z.ZodOptional<z.ZodNullable<z.ZodString>>;
2250
2312
  }, z.core.$strip>>;
2251
2313
  }, z.core.$strip>;
package/dist/index.d.ts CHANGED
@@ -97,6 +97,7 @@ export { UsageMeter_alias_1 as UsageMeter } from './_tsup-dts-rollup.js';
97
97
  export { AgentWorkflowInput } from './_tsup-dts-rollup.js';
98
98
  export { AgentToolRegistry } from './_tsup-dts-rollup.js';
99
99
  export { agentWorkflow } from './_tsup-dts-rollup.js';
100
+ export { convertJsonSchemaToZod } from './_tsup-dts-rollup.js';
100
101
  export { validateWorkflowFile } from './_tsup-dts-rollup.js';
101
102
  export { parseDynamicWorkflowDefinition } from './_tsup-dts-rollup.js';
102
103
  export { createDynamicWorkflow } from './_tsup-dts-rollup.js';
@@ -107,6 +108,12 @@ export { DynamicWorkflowParseResult } from './_tsup-dts-rollup.js';
107
108
  export { DynamicStepRuntimeContext } from './_tsup-dts-rollup.js';
108
109
  export { DynamicWorkflowRunnerOptions } from './_tsup-dts-rollup.js';
109
110
  export { ValidationResult } from './_tsup-dts-rollup.js';
111
+ export { WhileLoopStep } from './_tsup-dts-rollup.js';
112
+ export { IfElseStep } from './_tsup-dts-rollup.js';
113
+ export { BreakStep } from './_tsup-dts-rollup.js';
114
+ export { ContinueStep } from './_tsup-dts-rollup.js';
115
+ export { TryCatchStep } from './_tsup-dts-rollup.js';
116
+ export { WorkflowControlFlowStep } from './_tsup-dts-rollup.js';
110
117
  export { WorkflowInputDefinitionSchema } from './_tsup-dts-rollup.js';
111
118
  export { WorkflowStepDefinitionSchema } from './_tsup-dts-rollup.js';
112
119
  export { WhileLoopStepSchema } from './_tsup-dts-rollup.js';
@@ -119,12 +126,6 @@ export { WorkflowDefinitionSchema } from './_tsup-dts-rollup.js';
119
126
  export { WorkflowFileSchema } from './_tsup-dts-rollup.js';
120
127
  export { WorkflowInputDefinition } from './_tsup-dts-rollup.js';
121
128
  export { WorkflowStepDefinition } from './_tsup-dts-rollup.js';
122
- export { WhileLoopStep } from './_tsup-dts-rollup.js';
123
- export { IfElseStep } from './_tsup-dts-rollup.js';
124
- export { BreakStep } from './_tsup-dts-rollup.js';
125
- export { ContinueStep } from './_tsup-dts-rollup.js';
126
- export { TryCatchStep } from './_tsup-dts-rollup.js';
127
- export { WorkflowControlFlowStep } from './_tsup-dts-rollup.js';
128
129
  export { WorkflowDefinition } from './_tsup-dts-rollup.js';
129
130
  export { WorkflowFile } from './_tsup-dts-rollup.js';
130
131
  export { JsonImagePart } from './_tsup-dts-rollup.js';
package/dist/index.js CHANGED
@@ -2403,7 +2403,18 @@ var WorkflowFileSchema = z20.object({
2403
2403
  var MAX_WHILE_LOOP_ITERATIONS = 1e3;
2404
2404
  function convertJsonSchemaToZod(schema) {
2405
2405
  if (schema.enum) {
2406
- return z21.enum(schema.enum.map((v) => String(v)));
2406
+ const enumValues = schema.enum;
2407
+ if (enumValues.length === 0) {
2408
+ return z21.never();
2409
+ }
2410
+ if (enumValues.every((v) => typeof v === "string")) {
2411
+ return z21.enum(enumValues);
2412
+ }
2413
+ const literals = enumValues.map((v) => z21.literal(v));
2414
+ if (literals.length === 1) {
2415
+ return literals[0];
2416
+ }
2417
+ return z21.union([literals[0], literals[1], ...literals.slice(2)]);
2407
2418
  }
2408
2419
  if (Array.isArray(schema.type)) {
2409
2420
  const types = schema.type;
@@ -2450,14 +2461,14 @@ function convertJsonSchemaToZod(schema) {
2450
2461
  shape[propName] = isRequired ? propZod : propZod.optional();
2451
2462
  }
2452
2463
  }
2453
- let objectSchema = z21.object(shape);
2454
2464
  if (schema.additionalProperties === true) {
2455
- objectSchema = objectSchema.and(z21.any());
2456
- } else if (typeof schema.additionalProperties === "object") {
2465
+ return z21.object(shape).passthrough();
2466
+ }
2467
+ if (typeof schema.additionalProperties === "object") {
2457
2468
  const additionalSchema = convertJsonSchemaToZod(schema.additionalProperties);
2458
- objectSchema = objectSchema.and(z21.record(z21.string(), additionalSchema));
2469
+ return z21.intersection(z21.object(shape), z21.record(z21.string(), additionalSchema));
2459
2470
  }
2460
- return objectSchema;
2471
+ return z21.object(shape);
2461
2472
  }
2462
2473
  case "array": {
2463
2474
  if (!schema.items) {
@@ -2572,8 +2583,13 @@ ${errors.map((e) => ` - ${e}`).join("\n")}`);
2572
2583
  }
2573
2584
  return validatedInput;
2574
2585
  }
2575
- function evaluateCondition(condition, input, state, allowUnsafeCodeExecution = false) {
2586
+ function evaluateCondition(condition, input, state, allowUnsafeCodeExecution = false, logger) {
2576
2587
  if (allowUnsafeCodeExecution) {
2588
+ if (logger) {
2589
+ logger.warn(
2590
+ `[SECURITY] Executing unsafe code evaluation for condition: ${condition}. This allows arbitrary JavaScript execution and should only be used for trusted workflows.`
2591
+ );
2592
+ }
2577
2593
  const functionBody = `
2578
2594
  try {
2579
2595
  return ${condition};
@@ -2751,6 +2767,7 @@ function getNestedProperty(obj, path) {
2751
2767
  let current = obj;
2752
2768
  for (const part of parts) {
2753
2769
  if (current == null) return void 0;
2770
+ if (typeof current !== "object") return void 0;
2754
2771
  current = current[part];
2755
2772
  }
2756
2773
  return current;
@@ -2865,8 +2882,9 @@ async function executeStepWithAgent(stepDef, workflowId, input, state, context,
2865
2882
  };
2866
2883
  }
2867
2884
  if (toolName === "runWorkflow") {
2868
- const subWorkflowId = toolInput?.workflowId;
2869
- const subInput = toolInput?.input;
2885
+ const runWorkflowInput = toolInput;
2886
+ const subWorkflowId = runWorkflowInput?.workflowId;
2887
+ const subInput = runWorkflowInput?.input;
2870
2888
  if (typeof subWorkflowId !== "string") {
2871
2889
  return {
2872
2890
  success: false,
@@ -3463,6 +3481,7 @@ export {
3463
3481
  askFollowupQuestion_default as askFollowupQuestion,
3464
3482
  computeRateLimitBackoffSeconds,
3465
3483
  configSchema,
3484
+ convertJsonSchemaToZod,
3466
3485
  createContext,
3467
3486
  createDynamicWorkflow,
3468
3487
  executeCommand_default as executeCommand,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polka-codes/core",
3
- "version": "0.9.84",
3
+ "version": "0.9.86",
4
4
  "license": "AGPL-3.0",
5
5
  "author": "github@polka.codes",
6
6
  "type": "module",