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