@perstack/core 0.0.51 → 0.0.53

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/src/index.js CHANGED
@@ -143,7 +143,6 @@ const expertNameRegex = /^(@[a-z0-9][a-z0-9_-]*\/)?[a-z0-9][a-z0-9_-]*$/;
143
143
  const expertVersionRegex = /^(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[\w.-]+)?(?:\+[\w.-]+)?$/;
144
144
  const tagNameRegex = /^[a-z0-9][a-z0-9_-]*$/;
145
145
  const maxExpertNameLength = 255;
146
- const defaultMaxSteps = 100;
147
146
  const defaultMaxRetries = 5;
148
147
  const defaultTimeout = 5 * 1e3 * 60;
149
148
  const maxSkillNameLength = 255;
@@ -391,7 +390,7 @@ const toolCallPartSchema = basePartSchema.extend({
391
390
  type: z.literal("toolCallPart"),
392
391
  toolCallId: z.string(),
393
392
  toolName: z.string(),
394
- args: z.unknown()
393
+ args: z.record(z.string(), z.unknown())
395
394
  });
396
395
  const thinkingPartSchema = basePartSchema.extend({
397
396
  type: z.literal("thinkingPart"),
@@ -688,7 +687,6 @@ const checkpointStatusSchema = z.enum([
688
687
  "completed",
689
688
  "stoppedByInteractiveTool",
690
689
  "stoppedByDelegate",
691
- "stoppedByExceededMaxSteps",
692
690
  "stoppedByError",
693
691
  "stoppedByCancellation"
694
692
  ]);
@@ -741,6 +739,73 @@ const checkpointSchema = z.object({
741
739
  retryCount: z.number().optional()
742
740
  });
743
741
 
742
+ //#endregion
743
+ //#region src/utils/expert-type.ts
744
+ function getExpertType(expertName) {
745
+ return expertName.startsWith("@") ? "delegate" : "coordinator";
746
+ }
747
+ function isCoordinatorExpert(expertName) {
748
+ return getExpertType(expertName) === "coordinator";
749
+ }
750
+ function isDelegateExpert(expertName) {
751
+ return getExpertType(expertName) === "delegate";
752
+ }
753
+ /**
754
+ * Returns the scope of an expert.
755
+ * - Coordinator "game-producer" -> "game-producer"
756
+ * - Delegate "@game-producer/designer" -> "game-producer"
757
+ */
758
+ function getExpertScope(expertName) {
759
+ if (isDelegateExpert(expertName)) {
760
+ const withoutAt = expertName.slice(1);
761
+ const slashIndex = withoutAt.indexOf("/");
762
+ return slashIndex === -1 ? withoutAt : withoutAt.slice(0, slashIndex);
763
+ }
764
+ return expertName;
765
+ }
766
+ /**
767
+ * Returns the short name of an expert.
768
+ * - Coordinator "game-producer" -> "game-producer"
769
+ * - Delegate "@game-producer/designer" -> "designer"
770
+ */
771
+ function getExpertShortName(expertName) {
772
+ if (isDelegateExpert(expertName)) {
773
+ const slashIndex = expertName.indexOf("/");
774
+ return slashIndex === -1 ? expertName : expertName.slice(slashIndex + 1);
775
+ }
776
+ return expertName;
777
+ }
778
+ /**
779
+ * Validates whether a delegation from source to target is allowed.
780
+ * Returns null if valid, an error message string if invalid.
781
+ *
782
+ * Rules:
783
+ * - No self-delegation
784
+ * - If target is a delegate (@scope/name), source must be in the same scope
785
+ * - A delegate cannot delegate to its own coordinator
786
+ */
787
+ function validateDelegation(source, target) {
788
+ if (source === target) return `Expert "${source}" cannot delegate to itself`;
789
+ const sourceScope = getExpertScope(source);
790
+ if (isDelegateExpert(target)) {
791
+ if (sourceScope !== getExpertScope(target)) return `Expert "${source}" cannot delegate to out-of-scope delegate "${target}"`;
792
+ }
793
+ if (isDelegateExpert(source) && isCoordinatorExpert(target) && target === sourceScope) return `Delegate "${source}" cannot delegate to its own coordinator "${target}"`;
794
+ return null;
795
+ }
796
+ /**
797
+ * Validates all delegations for an expert.
798
+ * Returns an array of error messages (empty if all valid).
799
+ */
800
+ function validateAllDelegations(expertName, delegates) {
801
+ const errors = [];
802
+ for (const delegate of delegates) {
803
+ const error = validateDelegation(expertName, delegate);
804
+ if (error) errors.push(error);
805
+ }
806
+ return errors;
807
+ }
808
+
744
809
  //#endregion
745
810
  //#region src/schemas/provider-tools.ts
746
811
  const anthropicProviderToolNameSchema = z.enum([
@@ -886,7 +951,11 @@ const skillSchema = z.discriminatedUnion("type", [
886
951
 
887
952
  //#endregion
888
953
  //#region src/schemas/expert.ts
889
- const expertSchema = z.object({
954
+ /**
955
+ * Base object schema for Expert. Use this for `.omit()` / `.pick()` operations.
956
+ * For parsing with delegation validation, use `expertSchema` instead.
957
+ */
958
+ const expertBaseSchema = z.object({
890
959
  key: z.string().regex(expertKeyRegex).min(1),
891
960
  name: z.string().regex(expertNameRegex).min(1).max(maxExpertNameLength),
892
961
  version: z.string().regex(expertVersionRegex),
@@ -921,13 +990,24 @@ const expertSchema = z.object({
921
990
  providerSkills: z.array(anthropicProviderSkillSchema).optional(),
922
991
  providerToolOptions: providerToolOptionsSchema
923
992
  });
993
+ /**
994
+ * Expert schema with delegation rule validation.
995
+ * Rejects self-delegation, out-of-scope delegates, and delegate-to-own-coordinator.
996
+ */
997
+ const expertSchema = expertBaseSchema.superRefine((data, ctx) => {
998
+ const errors = validateAllDelegations(data.key, data.delegates);
999
+ for (const error of errors) ctx.addIssue({
1000
+ code: z.ZodIssueCode.custom,
1001
+ message: error,
1002
+ path: ["delegates"]
1003
+ });
1004
+ });
924
1005
 
925
1006
  //#endregion
926
1007
  //#region src/schemas/job.ts
927
1008
  const jobStatusSchema = z.enum([
928
1009
  "running",
929
1010
  "completed",
930
- "stoppedByMaxSteps",
931
1011
  "stoppedByInteractiveTool",
932
1012
  "stoppedByError",
933
1013
  "stoppedByCancellation"
@@ -938,7 +1018,6 @@ const jobSchema = z.object({
938
1018
  coordinatorExpertKey: z.string(),
939
1019
  runtimeVersion: runtimeVersionSchema,
940
1020
  totalSteps: z.number(),
941
- maxSteps: z.number().optional(),
942
1021
  usage: usageSchema,
943
1022
  startedAt: z.number(),
944
1023
  finishedAt: z.number().optional()
@@ -1149,7 +1228,6 @@ const perstackConfigSchema = z.object({
1149
1228
  provider: providerTableSchema.optional(),
1150
1229
  model: z.string().optional(),
1151
1230
  reasoningBudget: reasoningBudgetSchema.optional(),
1152
- maxSteps: z.number().optional(),
1153
1231
  maxRetries: z.number().optional(),
1154
1232
  timeout: z.number().optional(),
1155
1233
  experts: z.record(z.string(), z.object({
@@ -1219,12 +1297,6 @@ const commandOptionsSchema = z.object({
1219
1297
  if (Number.isNaN(parsedValue)) return void 0;
1220
1298
  return parsedValue;
1221
1299
  }).pipe(reasoningBudgetSchema.optional()),
1222
- maxSteps: z.string().optional().transform((value) => {
1223
- if (value === void 0) return void 0;
1224
- const parsedValue = Number.parseInt(value, 10);
1225
- if (Number.isNaN(parsedValue)) return void 0;
1226
- return parsedValue;
1227
- }),
1228
1300
  maxRetries: z.string().optional().transform((value) => {
1229
1301
  if (value === void 0) return void 0;
1230
1302
  const parsedValue = Number.parseInt(value, 10);
@@ -1293,7 +1365,6 @@ const runSettingSchema = z.object({
1293
1365
  }),
1294
1366
  experts: z.record(z.string(), expertSchema),
1295
1367
  reasoningBudget: reasoningBudgetSchema.default(defaultReasoningBudget),
1296
- maxSteps: z.number().min(1).optional().default(defaultMaxSteps),
1297
1368
  maxRetries: z.number().min(0),
1298
1369
  timeout: z.number().min(0),
1299
1370
  startedAt: z.number(),
@@ -1321,12 +1392,11 @@ const runParamsSchema = z.object({
1321
1392
  text: z.string()
1322
1393
  }).optional()
1323
1394
  }),
1324
- experts: z.record(z.string().min(1).regex(expertKeyRegex), expertSchema.omit({ key: true })).optional().default({}).transform((experts) => Object.fromEntries(Object.entries(experts).map(([key, expertWithoutKey]) => [key, expertSchema.parse({
1395
+ experts: z.record(z.string().min(1).regex(expertKeyRegex), expertBaseSchema.omit({ key: true })).optional().default({}).transform((experts) => Object.fromEntries(Object.entries(experts).map(([key, expertWithoutKey]) => [key, expertSchema.parse({
1325
1396
  ...expertWithoutKey,
1326
1397
  key
1327
1398
  })]))),
1328
1399
  reasoningBudget: reasoningBudgetSchema.optional().default(defaultReasoningBudget),
1329
- maxSteps: z.number().min(1).optional().default(defaultMaxSteps),
1330
1400
  maxRetries: z.number().min(0).optional().default(defaultMaxRetries),
1331
1401
  timeout: z.number().min(0).optional().default(defaultTimeout),
1332
1402
  startedAt: z.number().optional().default(Date.now()),
@@ -1382,7 +1452,6 @@ const resumeToolCalls = createEvent("resumeToolCalls");
1382
1452
  const completeRun = createEvent("completeRun");
1383
1453
  const stopRunByInteractiveTool = createEvent("stopRunByInteractiveTool");
1384
1454
  const stopRunByDelegate = createEvent("stopRunByDelegate");
1385
- const stopRunByExceededMaxSteps = createEvent("stopRunByExceededMaxSteps");
1386
1455
  const stopRunByError = createEvent("stopRunByError");
1387
1456
  const continueToNextStep = createEvent("continueToNextStep");
1388
1457
  /** Factory function to create runtime events */
@@ -1414,7 +1483,6 @@ const EXPERT_STATE_EVENT_TYPES = new Set([
1414
1483
  "continueToNextStep",
1415
1484
  "stopRunByInteractiveTool",
1416
1485
  "stopRunByDelegate",
1417
- "stopRunByExceededMaxSteps",
1418
1486
  "stopRunByError",
1419
1487
  "completeRun"
1420
1488
  ]);
@@ -1901,5 +1969,5 @@ function parseWithFriendlyError(schema, data, context) {
1901
1969
  }
1902
1970
 
1903
1971
  //#endregion
1904
- export { BASE_SKILL_PREFIX, PerstackError, SAFE_ENV_VARS, activityOrGroupSchema, activitySchema, addDelegateActivitySchema, addSkillActivitySchema, amazonBedrockProviderConfigSchema, anthropicProviderConfigSchema, anthropicProviderSkillSchema, anthropicProviderToolNameSchema, attemptCompletionActivitySchema, azureOpenAIProviderToolNameSchema, azureOpenAiProviderConfigSchema, basePartSchema, builtinAnthropicSkillSchema, callTools, checkpointSchema, checkpointStatusSchema, clearTodoActivitySchema, completeActivitySchema, completeRun, continueToNextStep, createBaseToolActivity, createCallToolsEvent, createCompleteRunEvent, createEmptyUsage, createEvent, createExpertActivitySchema, createFilteredEventListener, createGeneralToolActivity, createNormalizedCheckpoint, createResolveToolResultsEvent, createRuntimeEvent, createRuntimeInitEvent, createStartRunEvent, createStreamingEvent, createToolMessage, customAnthropicSkillSchema, deepseekProviderConfigSchema, defaultMaxRetries, defaultMaxSteps, defaultPerstackApiBaseUrl, defaultReasoningBudget, defaultTimeout, delegateActivitySchema, delegationCompleteActivitySchema, delegationTargetSchema, domainPatternSchema, editTextFileActivitySchema, errorActivitySchema, execActivitySchema, expertKeyRegex, expertMessageSchema, expertNameRegex, expertSchema, expertVersionRegex, fileBinaryPartSchema, fileInlinePartSchema, fileSearchOptionsSchema, fileUrlPartSchema, finishMcpTools, finishToolCall, formatZodError, generalToolActivitySchema, getActivities, getFilteredEnv, googleGenerativeAiProviderConfigSchema, googleProviderToolNameSchema, googleVertexProviderConfigSchema, headersSchema, imageBinaryPartSchema, imageInlinePartSchema, imageUrlPartSchema, instructionMessageSchema, interactiveSkillSchema, interactiveToolActivitySchema, interactiveToolSchema, isPrivateOrLocalIP, isValidEventType, isValidRuntimeEventType, jobSchema, jobStatusSchema, knownModels, lockfileExpertSchema, lockfileSchema, lockfileToolDefinitionSchema, maxExpertNameLength, maxSkillNameLength, maxSkillToolNameLength, mcpSseSkillSchema, mcpStdioSkillSchema, messagePartSchema, messageSchema, ollamaProviderConfigSchema, openAiProviderConfigSchema, openaiProviderToolNameSchema, parallelActivitiesGroupSchema, parseExpertKey, parseWithFriendlyError, perstackConfigSchema, proceedToInteractiveTools, providerConfigSchema, providerNameSchema, providerTableSchema, providerToolOptionsSchema, queryActivitySchema, readImageFileActivitySchema, readPdfFileActivitySchema, readTextFileActivitySchema, reasoningBudgetSchema, removeDelegateActivitySchema, removeSkillActivitySchema, resolveToolResults, resumeFromStop, resumeToolCalls, retry, retryActivitySchema, runCommandInputSchema, runParamsSchema, runSettingSchema, runtimeVersionSchema, skillSchema, skipDelegates, startCommandInputSchema, startGeneration, startRun, stepSchema, stopRunByDelegate, stopRunByError, stopRunByExceededMaxSteps, stopRunByInteractiveTool, tagNameRegex, textPartSchema, thinkingPartSchema, todoActivitySchema, toolCallPartSchema, toolCallSchema, toolMessageSchema, toolResultPartSchema, toolResultSchema, usageSchema, userMessageSchema, validateEventFilter, vertexProviderToolNameSchema, webFetchOptionsSchema, webSearchOptionsSchema, writeTextFileActivitySchema };
1972
+ export { BASE_SKILL_PREFIX, PerstackError, SAFE_ENV_VARS, activityOrGroupSchema, activitySchema, addDelegateActivitySchema, addSkillActivitySchema, amazonBedrockProviderConfigSchema, anthropicProviderConfigSchema, anthropicProviderSkillSchema, anthropicProviderToolNameSchema, attemptCompletionActivitySchema, azureOpenAIProviderToolNameSchema, azureOpenAiProviderConfigSchema, basePartSchema, builtinAnthropicSkillSchema, callTools, checkpointSchema, checkpointStatusSchema, clearTodoActivitySchema, completeActivitySchema, completeRun, continueToNextStep, createBaseToolActivity, createCallToolsEvent, createCompleteRunEvent, createEmptyUsage, createEvent, createExpertActivitySchema, createFilteredEventListener, createGeneralToolActivity, createNormalizedCheckpoint, createResolveToolResultsEvent, createRuntimeEvent, createRuntimeInitEvent, createStartRunEvent, createStreamingEvent, createToolMessage, customAnthropicSkillSchema, deepseekProviderConfigSchema, defaultMaxRetries, defaultPerstackApiBaseUrl, defaultReasoningBudget, defaultTimeout, delegateActivitySchema, delegationCompleteActivitySchema, delegationTargetSchema, domainPatternSchema, editTextFileActivitySchema, errorActivitySchema, execActivitySchema, expertBaseSchema, expertKeyRegex, expertMessageSchema, expertNameRegex, expertSchema, expertVersionRegex, fileBinaryPartSchema, fileInlinePartSchema, fileSearchOptionsSchema, fileUrlPartSchema, finishMcpTools, finishToolCall, formatZodError, generalToolActivitySchema, getActivities, getExpertScope, getExpertShortName, getExpertType, getFilteredEnv, googleGenerativeAiProviderConfigSchema, googleProviderToolNameSchema, googleVertexProviderConfigSchema, headersSchema, imageBinaryPartSchema, imageInlinePartSchema, imageUrlPartSchema, instructionMessageSchema, interactiveSkillSchema, interactiveToolActivitySchema, interactiveToolSchema, isCoordinatorExpert, isDelegateExpert, isPrivateOrLocalIP, isValidEventType, isValidRuntimeEventType, jobSchema, jobStatusSchema, knownModels, lockfileExpertSchema, lockfileSchema, lockfileToolDefinitionSchema, maxExpertNameLength, maxSkillNameLength, maxSkillToolNameLength, mcpSseSkillSchema, mcpStdioSkillSchema, messagePartSchema, messageSchema, ollamaProviderConfigSchema, openAiProviderConfigSchema, openaiProviderToolNameSchema, parallelActivitiesGroupSchema, parseExpertKey, parseWithFriendlyError, perstackConfigSchema, proceedToInteractiveTools, providerConfigSchema, providerNameSchema, providerTableSchema, providerToolOptionsSchema, queryActivitySchema, readImageFileActivitySchema, readPdfFileActivitySchema, readTextFileActivitySchema, reasoningBudgetSchema, removeDelegateActivitySchema, removeSkillActivitySchema, resolveToolResults, resumeFromStop, resumeToolCalls, retry, retryActivitySchema, runCommandInputSchema, runParamsSchema, runSettingSchema, runtimeVersionSchema, skillSchema, skipDelegates, startCommandInputSchema, startGeneration, startRun, stepSchema, stopRunByDelegate, stopRunByError, stopRunByInteractiveTool, tagNameRegex, textPartSchema, thinkingPartSchema, todoActivitySchema, toolCallPartSchema, toolCallSchema, toolMessageSchema, toolResultPartSchema, toolResultSchema, usageSchema, userMessageSchema, validateAllDelegations, validateDelegation, validateEventFilter, vertexProviderToolNameSchema, webFetchOptionsSchema, webSearchOptionsSchema, writeTextFileActivitySchema };
1905
1973
  //# sourceMappingURL=index.js.map