@kirha/planner 0.1.2 → 0.1.4

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.
Files changed (2) hide show
  1. package/dist/index.js +174 -169
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -20650,7 +20650,8 @@ function resolveValue(value, outputsByStepId) {
20650
20650
  for (const [i, ref] of value.$values.entries()) {
20651
20651
  const stepOutput = outputsByStepId.get(ref.$fromStep);
20652
20652
  const resolvedValue = getNestedValue(stepOutput, parsePath(ref.$outputKey));
20653
- result = result.replace(`{${i}}`, String(resolvedValue));
20653
+ const stringified = typeof resolvedValue === "object" && resolvedValue !== null ? JSON.stringify(resolvedValue) : String(resolvedValue);
20654
+ result = result.replace(`{${i}}`, stringified);
20654
20655
  }
20655
20656
  return result;
20656
20657
  }
@@ -20666,39 +20667,22 @@ function resolveValue(value, outputsByStepId) {
20666
20667
 
20667
20668
  // src/validator/index.ts
20668
20669
  var import_json52 = __toESM(require_lib(), 1);
20669
- function createErrorContext(step, argumentPath, reference) {
20670
- return {
20671
- stepId: step.stepId,
20672
- toolName: step.toolName,
20673
- argumentPath: formatPath(argumentPath),
20674
- ...reference && {
20675
- fromStepId: reference.$fromStep,
20676
- outputPath: reference.$outputKey
20677
- }
20678
- };
20679
- }
20680
- function createValidationError(code, message, context, extra) {
20681
- return {
20682
- code,
20683
- message,
20684
- ...context,
20685
- ...extra
20686
- };
20687
- }
20688
20670
  function isValidPlan(steps, tools) {
20689
20671
  const errors3 = [];
20690
20672
  const toolsByName = new Map(tools.map((tool) => [tool.name, tool]));
20691
20673
  const schemasByTool = new Map;
20692
20674
  const stepsById = new Map(steps.map((step) => [step.stepId, step]));
20693
20675
  for (const tool of tools) {
20694
- const { schemas: schemas3, errorDetail } = parseToolSchemas(tool);
20695
- if (schemas3) {
20696
- schemasByTool.set(tool.name, schemas3);
20697
- } else {
20698
- const detail = errorDetail ? `: ${errorDetail}` : "";
20676
+ try {
20677
+ schemasByTool.set(tool.name, {
20678
+ input: parseSchema(tool.inputSchema),
20679
+ output: parseSchema(tool.outputSchema)
20680
+ });
20681
+ } catch (error47) {
20682
+ const detail = error47 instanceof Error ? error47.message : String(error47);
20699
20683
  errors3.push({
20700
20684
  code: "schema_parse_error",
20701
- message: `Failed to parse input/output schema for tool "${tool.name}"${detail}`,
20685
+ message: `Failed to parse input/output schema for tool "${tool.name}": ${detail}`,
20702
20686
  toolName: tool.name
20703
20687
  });
20704
20688
  }
@@ -20725,42 +20709,30 @@ function isValidPlan(steps, tools) {
20725
20709
  continue;
20726
20710
  }
20727
20711
  traverseReferences(step.arguments, {
20728
- onDependency: (ref, path2) => {
20729
- validateDependencyReference({
20730
- reference: ref,
20731
- argumentPath: path2,
20732
- step,
20733
- inputSchema: schemas3.input,
20734
- stepsById,
20735
- schemasByTool,
20736
- errors: errors3
20737
- });
20712
+ onDependency: (ref, inputPath) => {
20713
+ const formattedInputPath = formatPath(inputPath);
20714
+ const expectedSchema = getSchemaAtPath(schemas3.input, inputPath);
20715
+ if (!expectedSchema) {
20716
+ errors3.push({
20717
+ code: "input_key_missing",
20718
+ message: `Input path "${formattedInputPath}" not found on tool "${step.toolName}"`,
20719
+ stepId: step.stepId,
20720
+ toolName: step.toolName,
20721
+ argumentPath: formattedInputPath,
20722
+ fromStepId: ref.$fromStep,
20723
+ outputPath: ref.$outputKey
20724
+ });
20725
+ return;
20726
+ }
20727
+ validateOutputReference(ref, formattedInputPath, expectedSchema, step, stepsById, schemasByTool, errors3);
20738
20728
  },
20739
- onTemplate: (ref, path2) => {
20740
- validateStringTemplateReference({
20741
- reference: ref,
20742
- argumentPath: path2,
20743
- step,
20744
- inputSchema: schemas3.input,
20745
- stepsById,
20746
- schemasByTool,
20747
- errors: errors3
20748
- });
20729
+ onTemplate: (ref, inputPath) => {
20730
+ validateStringTemplateReference(ref, inputPath, step, schemas3.input, stepsById, schemasByTool, errors3);
20749
20731
  }
20750
20732
  });
20751
20733
  }
20752
20734
  return { valid: errors3.length === 0, errors: errors3 };
20753
20735
  }
20754
- function parseToolSchemas(tool) {
20755
- try {
20756
- const input = parseSchema(tool.inputSchema);
20757
- const output = parseSchema(tool.outputSchema);
20758
- return { schemas: { input, output } };
20759
- } catch (error47) {
20760
- const errorDetail = error47 instanceof Error ? error47.message : String(error47);
20761
- return { schemas: null, errorDetail };
20762
- }
20763
- }
20764
20736
  function parseSchema(rawSchema) {
20765
20737
  try {
20766
20738
  return exports_external.fromJSONSchema(import_json52.default.parse(rawSchema));
@@ -20769,156 +20741,175 @@ function parseSchema(rawSchema) {
20769
20741
  throw new Error(`Invalid JSON schema: ${message}`);
20770
20742
  }
20771
20743
  }
20772
- function getExpectedSchemaOrError(inputSchema, argumentPath, step, reference, errors3) {
20773
- const expectedSchema = getSchemaAtPath(inputSchema, argumentPath);
20774
- if (!expectedSchema) {
20775
- const context = createErrorContext(step, argumentPath, reference);
20776
- errors3.push(createValidationError("input_key_missing", `Input path "${context.argumentPath}" not found on tool "${step.toolName}"`, context));
20777
- return null;
20778
- }
20779
- return expectedSchema;
20780
- }
20781
- function validateDependencyReference({
20782
- reference,
20783
- argumentPath,
20784
- step,
20785
- inputSchema,
20786
- stepsById,
20787
- schemasByTool,
20788
- errors: errors3
20789
- }) {
20790
- const expectedSchema = getExpectedSchemaOrError(inputSchema, argumentPath, step, reference, errors3);
20791
- if (expectedSchema) {
20792
- validateOutputReference({
20793
- reference,
20794
- argumentPath,
20795
- expectedSchema,
20796
- step,
20797
- stepsById,
20798
- schemasByTool,
20799
- errors: errors3
20800
- });
20801
- }
20802
- }
20803
- function resolveSourceStep(reference, stepsById, context, errors3) {
20744
+ function validateOutputReference(reference, inputPath, expectedSchema, step, stepsById, schemasByTool, errors3) {
20745
+ const baseContext = {
20746
+ stepId: step.stepId,
20747
+ toolName: step.toolName,
20748
+ argumentPath: inputPath,
20749
+ fromStepId: reference.$fromStep,
20750
+ outputPath: reference.$outputKey
20751
+ };
20804
20752
  const sourceStep = stepsById.get(reference.$fromStep);
20805
20753
  if (!sourceStep) {
20806
- errors3.push(createValidationError("dependency_step_missing", `Step "${reference.$fromStep}" not found`, context));
20807
- return null;
20754
+ errors3.push({
20755
+ code: "dependency_step_missing",
20756
+ message: `Step "${reference.$fromStep}" not found`,
20757
+ ...baseContext
20758
+ });
20759
+ return;
20808
20760
  }
20809
- return sourceStep;
20810
- }
20811
- function resolveSourceSchemas(sourceStep, schemasByTool, context, errors3) {
20812
20761
  const sourceSchemas = schemasByTool.get(sourceStep.toolName);
20813
20762
  if (!sourceSchemas) {
20814
- errors3.push(createValidationError("schema_parse_error", `Output schema for tool "${sourceStep.toolName}" could not be parsed`, context));
20815
- return null;
20763
+ errors3.push({
20764
+ code: "schema_parse_error",
20765
+ message: `Output schema for tool "${sourceStep.toolName}" could not be parsed`,
20766
+ ...baseContext
20767
+ });
20768
+ return;
20816
20769
  }
20817
- return sourceSchemas;
20818
- }
20819
- function resolveOutputSchema(reference, sourceSchemas, sourceStep, context, errors3) {
20820
20770
  const outputSchema = getSchemaAtPath(sourceSchemas.output, parsePath(reference.$outputKey));
20821
20771
  if (!outputSchema) {
20822
- errors3.push(createValidationError("output_key_missing", `Output key "${reference.$outputKey}" not found on tool "${sourceStep.toolName}"`, context));
20823
- return null;
20824
- }
20825
- return outputSchema;
20826
- }
20827
- function validateOutputReference({
20828
- reference,
20829
- argumentPath,
20830
- expectedSchema,
20831
- step,
20832
- stepsById,
20833
- schemasByTool,
20834
- errors: errors3
20835
- }) {
20836
- const context = createErrorContext(step, argumentPath, reference);
20837
- const sourceStep = resolveSourceStep(reference, stepsById, context, errors3);
20838
- if (!sourceStep)
20839
- return;
20840
- const sourceSchemas = resolveSourceSchemas(sourceStep, schemasByTool, context, errors3);
20841
- if (!sourceSchemas)
20842
- return;
20843
- const outputSchema = resolveOutputSchema(reference, sourceSchemas, sourceStep, context, errors3);
20844
- if (!outputSchema)
20772
+ errors3.push({
20773
+ code: "output_key_missing",
20774
+ message: `Output key "${reference.$outputKey}" not found on tool "${sourceStep.toolName}"`,
20775
+ ...baseContext
20776
+ });
20845
20777
  return;
20778
+ }
20846
20779
  if (!isSchemaCompatible(expectedSchema, outputSchema)) {
20847
- errors3.push(createValidationError("type_mismatch", `Type mismatch for "${context.argumentPath}"`, context, {
20780
+ errors3.push({
20781
+ code: "type_mismatch",
20782
+ message: `Type mismatch for "${inputPath}"`,
20783
+ ...baseContext,
20848
20784
  expectedType: describeSchemaType(expectedSchema),
20849
20785
  actualType: describeSchemaType(outputSchema)
20850
- }));
20786
+ });
20851
20787
  }
20852
20788
  }
20853
- function validateStringTemplateReference({
20854
- reference,
20855
- argumentPath,
20856
- step,
20857
- inputSchema,
20858
- stepsById,
20859
- schemasByTool,
20860
- errors: errors3
20861
- }) {
20862
- const expectedSchema = getExpectedSchemaOrError(inputSchema, argumentPath, step, undefined, errors3);
20789
+ function validateStringTemplateReference(reference, inputPath, step, inputSchema, stepsById, schemasByTool, errors3) {
20790
+ const formattedInputPath = formatPath(inputPath);
20791
+ const expectedSchema = getSchemaAtPath(inputSchema, inputPath);
20863
20792
  if (!expectedSchema) {
20793
+ errors3.push({
20794
+ code: "input_key_missing",
20795
+ message: `Input path "${formattedInputPath}" not found on tool "${step.toolName}"`,
20796
+ stepId: step.stepId,
20797
+ toolName: step.toolName,
20798
+ argumentPath: formattedInputPath
20799
+ });
20864
20800
  return;
20865
20801
  }
20866
- const context = createErrorContext(step, argumentPath);
20867
20802
  if (!isSchemaCompatible(expectedSchema, exports_external.string())) {
20868
- errors3.push(createValidationError("type_mismatch", `Type mismatch for "${context.argumentPath}"`, context, {
20803
+ errors3.push({
20804
+ code: "type_mismatch",
20805
+ message: `Type mismatch for "${formattedInputPath}"`,
20806
+ stepId: step.stepId,
20807
+ toolName: step.toolName,
20808
+ argumentPath: formattedInputPath,
20869
20809
  expectedType: describeSchemaType(expectedSchema),
20870
20810
  actualType: "string"
20871
- }));
20811
+ });
20872
20812
  }
20873
- const stringCoercible = exports_external.union([exports_external.string(), exports_external.number(), exports_external.boolean()]);
20813
+ const stringCoercible = exports_external.union([
20814
+ exports_external.string(),
20815
+ exports_external.number(),
20816
+ exports_external.boolean(),
20817
+ exports_external.object({}),
20818
+ exports_external.array(exports_external.any())
20819
+ ]);
20874
20820
  for (const ref of reference.$values) {
20875
- validateOutputReference({
20876
- reference: ref,
20877
- argumentPath,
20878
- expectedSchema: stringCoercible,
20879
- step,
20880
- stepsById,
20881
- schemasByTool,
20882
- errors: errors3
20883
- });
20821
+ validateOutputReference(ref, formattedInputPath, stringCoercible, step, stepsById, schemasByTool, errors3);
20884
20822
  }
20885
20823
  }
20886
20824
  function getSchemaAtPath(schema, path2) {
20887
- let current = unwrapSchema(schema);
20888
- for (const segment of path2) {
20889
- if (typeof segment === "number") {
20890
- if (!(current instanceof exports_external.ZodArray)) {
20891
- return;
20892
- }
20893
- current = unwrapSchema(current.element);
20894
- continue;
20895
- }
20896
- if (current instanceof exports_external.ZodArray && isNumericString(segment)) {
20897
- current = unwrapSchema(current.element);
20898
- continue;
20899
- }
20900
- if (!(current instanceof exports_external.ZodObject)) {
20825
+ const unwrapped = unwrapSchema(schema);
20826
+ if (path2.length === 0) {
20827
+ return unwrapped;
20828
+ }
20829
+ if (unwrapped instanceof exports_external.ZodUnion || unwrapped instanceof exports_external.ZodXor) {
20830
+ const resolvedOptions = unwrapped.options.map((option) => getSchemaAtPath(option, path2)).filter((resolved) => resolved !== undefined);
20831
+ if (resolvedOptions.length === 0) {
20901
20832
  return;
20902
20833
  }
20903
- const shape = current.shape;
20904
- const child = shape[segment];
20905
- if (!child) {
20834
+ if (resolvedOptions.length === 1) {
20835
+ return resolvedOptions[0];
20836
+ }
20837
+ return exports_external.union(resolvedOptions);
20838
+ }
20839
+ const segment = path2[0];
20840
+ if (segment === undefined) {
20841
+ return unwrapped;
20842
+ }
20843
+ const remainingPath = path2.slice(1);
20844
+ if (typeof segment === "number") {
20845
+ if (!(unwrapped instanceof exports_external.ZodArray)) {
20906
20846
  return;
20907
20847
  }
20908
- current = unwrapSchema(child);
20848
+ return getSchemaAtPath(unwrapped.element, remainingPath);
20909
20849
  }
20910
- return current;
20850
+ if (unwrapped instanceof exports_external.ZodArray && isNumericString(segment)) {
20851
+ return getSchemaAtPath(unwrapped.element, remainingPath);
20852
+ }
20853
+ if (!(unwrapped instanceof exports_external.ZodObject)) {
20854
+ return;
20855
+ }
20856
+ const shape = unwrapped.shape;
20857
+ const childSchema = shape[segment];
20858
+ if (!childSchema) {
20859
+ return;
20860
+ }
20861
+ return getSchemaAtPath(childSchema, remainingPath);
20911
20862
  }
20912
20863
  function unwrapSchema(schema) {
20913
20864
  if (schema instanceof exports_external.ZodOptional || schema instanceof exports_external.ZodDefault || schema instanceof exports_external.ZodNullable) {
20914
- return unwrapSchema(schema._def.innerType);
20865
+ return unwrapSchema(schema.def.innerType);
20915
20866
  }
20916
20867
  return schema;
20917
20868
  }
20918
20869
  function isSchemaCompatible(expected, actual) {
20870
+ const expectedUnwrapped = unwrapSchema(expected);
20871
+ const actualUnwrapped = unwrapSchema(actual);
20872
+ if (expectedUnwrapped instanceof exports_external.ZodAny) {
20873
+ return true;
20874
+ }
20875
+ if (expectedUnwrapped instanceof exports_external.ZodUnion || expectedUnwrapped instanceof exports_external.ZodXor) {
20876
+ return expectedUnwrapped.options.some((option) => isSchemaCompatible(option, actualUnwrapped));
20877
+ }
20878
+ if (actualUnwrapped instanceof exports_external.ZodUnion || actualUnwrapped instanceof exports_external.ZodXor) {
20879
+ return actualUnwrapped.options.some((option) => isSchemaCompatible(expectedUnwrapped, option));
20880
+ }
20881
+ if (expectedUnwrapped instanceof exports_external.ZodArray && actualUnwrapped instanceof exports_external.ZodArray) {
20882
+ return isSchemaCompatible(expectedUnwrapped.element, actualUnwrapped.element);
20883
+ }
20884
+ if (expectedUnwrapped instanceof exports_external.ZodObject && actualUnwrapped instanceof exports_external.ZodObject) {
20885
+ const expectedShape = expectedUnwrapped.shape;
20886
+ const actualShape = actualUnwrapped.shape;
20887
+ for (const [key, expectedFieldSchema] of Object.entries(expectedShape)) {
20888
+ if (isOptionalField(expectedFieldSchema)) {
20889
+ continue;
20890
+ }
20891
+ const actualFieldSchema = actualShape[key];
20892
+ if (!actualFieldSchema) {
20893
+ return false;
20894
+ }
20895
+ if (!isSchemaCompatible(expectedFieldSchema, actualFieldSchema)) {
20896
+ return false;
20897
+ }
20898
+ }
20899
+ for (const [key, actualFieldSchema] of Object.entries(actualShape)) {
20900
+ const expectedFieldSchema = expectedShape[key];
20901
+ if (!expectedFieldSchema) {
20902
+ continue;
20903
+ }
20904
+ if (!isSchemaCompatible(expectedFieldSchema, actualFieldSchema)) {
20905
+ return false;
20906
+ }
20907
+ }
20908
+ return true;
20909
+ }
20919
20910
  const expectedTypes = getTypeSet(expected);
20920
20911
  const actualTypes = getTypeSet(actual);
20921
- if (expectedTypes.has("any")) {
20912
+ if (expectedTypes.has("any") || actualTypes.has("unknown")) {
20922
20913
  return true;
20923
20914
  }
20924
20915
  for (const actualType of actualTypes) {
@@ -20928,6 +20919,9 @@ function isSchemaCompatible(expected, actual) {
20928
20919
  }
20929
20920
  return false;
20930
20921
  }
20922
+ function isOptionalField(schema) {
20923
+ return schema instanceof exports_external.ZodOptional || schema instanceof exports_external.ZodDefault;
20924
+ }
20931
20925
  var KNOWN_ZOD_TYPES = [
20932
20926
  [exports_external.ZodAny, "any"],
20933
20927
  [exports_external.ZodString, "string"],
@@ -20935,8 +20929,15 @@ var KNOWN_ZOD_TYPES = [
20935
20929
  [exports_external.ZodBoolean, "boolean"],
20936
20930
  [exports_external.ZodNull, "null"],
20937
20931
  [exports_external.ZodArray, "array"],
20938
- [exports_external.ZodObject, "object"]
20932
+ [exports_external.ZodTuple, "array"],
20933
+ [exports_external.ZodObject, "object"],
20934
+ [exports_external.ZodEnum, "string"]
20939
20935
  ];
20936
+ var LITERAL_TYPE_MAP = {
20937
+ string: "string",
20938
+ number: "number",
20939
+ boolean: "boolean"
20940
+ };
20940
20941
  function getTypeSet(schema) {
20941
20942
  const unwrapped = unwrapSchema(schema);
20942
20943
  for (const [ZodClass, typeName] of KNOWN_ZOD_TYPES) {
@@ -20944,7 +20945,11 @@ function getTypeSet(schema) {
20944
20945
  return new Set([typeName]);
20945
20946
  }
20946
20947
  }
20947
- if (unwrapped instanceof exports_external.ZodUnion) {
20948
+ if (unwrapped instanceof exports_external.ZodLiteral) {
20949
+ const type = LITERAL_TYPE_MAP[typeof unwrapped.value];
20950
+ return new Set([type ?? "unknown"]);
20951
+ }
20952
+ if (unwrapped instanceof exports_external.ZodUnion || unwrapped instanceof exports_external.ZodXor) {
20948
20953
  const types = new Set;
20949
20954
  for (const option of unwrapped.options) {
20950
20955
  for (const type of getTypeSet(option)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kirha/planner",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "SDK for tool-planning agents - generate and execute DAG execution plans from natural language",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",