bpmax-cli 0.1.30 → 0.1.31

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/bin.js CHANGED
@@ -665,7 +665,7 @@ async function parseJsonInput(input, label = "JSON input") {
665
665
  // package.json
666
666
  var package_default = {
667
667
  name: "bpmax-cli",
668
- version: "0.1.30",
668
+ version: "0.1.31",
669
669
  description: "CLI for BPMAX environments with multi-profile config and local login callback flow",
670
670
  type: "module",
671
671
  bin: {
@@ -779,11 +779,12 @@ function buildBpmaxUrl(profile, kind, params = {}) {
779
779
  const flowType = params.flowType || "default";
780
780
  const flowKey = params.flowKey || "";
781
781
  const initValue = params.initValue ? JSON.stringify(params.initValue) : void 0;
782
+ const prefillToken = params.prefillToken;
782
783
  const query = {
783
784
  flow_type: flowType,
784
785
  flow_key: flowKey,
785
786
  ...flowKey ? { key: flowKey } : {},
786
- init_value: initValue
787
+ ...prefillToken ? { prefill_token: prefillToken } : { init_value: initValue }
787
788
  };
788
789
  path6 = withQuery(`${basePath}/project/create`, query);
789
790
  result.flow_type = flowType;
@@ -2607,17 +2608,42 @@ async function validateCreateForm(client, options) {
2607
2608
  - ${result.errors.join("\n- ")}`);
2608
2609
  }
2609
2610
  }
2610
- function printCreateFormHandoff(format, profile, flowIdentifiers, options) {
2611
+ async function storeCreateFormPrefill(client, flowIdentifiers, initValue, prefillContext) {
2612
+ const result = await apiRequest(
2613
+ client,
2614
+ "POST",
2615
+ "/project_staging/prefill",
2616
+ {
2617
+ data: {
2618
+ flow_type: flowIdentifiers.flow_type,
2619
+ flow_key: flowIdentifiers.flow_key,
2620
+ init_value: initValue,
2621
+ ...prefillContext ? { prefill_context: prefillContext } : {}
2622
+ }
2623
+ }
2624
+ );
2625
+ if (typeof result.token !== "string" || !result.token) {
2626
+ throw new Error("Project create prefill endpoint did not return a token.");
2627
+ }
2628
+ return {
2629
+ token: result.token,
2630
+ expires_in: result.expires_in,
2631
+ expires_at: result.expires_at
2632
+ };
2633
+ }
2634
+ async function printCreateFormHandoff(client, format, profile, flowIdentifiers, options) {
2611
2635
  const initValue = {
2612
2636
  project: { name: options.name },
2613
2637
  key_to_value: options.form
2614
2638
  };
2639
+ const prefillContext = buildPrefillContext(options.fields ?? [], options.form);
2640
+ const prefill = await storeCreateFormPrefill(client, flowIdentifiers, initValue, prefillContext);
2615
2641
  const url = buildBpmaxUrl(profile, "project-create", {
2616
2642
  flowType: flowIdentifiers.flow_type,
2617
2643
  flowKey: flowIdentifiers.flow_key,
2618
- initValue
2644
+ prefillToken: prefill.token
2619
2645
  });
2620
- assertCreateFormHandoffUrl(url, initValue, Object.keys(options.form));
2646
+ assertCreateFormHandoffUrl(url, initValue, Object.keys(options.form), prefill.token);
2621
2647
  const replyMarkdown = `[\u586B\u5199\u8868\u5355](${url.url})`;
2622
2648
  const agentWarning = [
2623
2649
  "Agent must reply with the full create form URL exactly as provided.",
@@ -2632,35 +2658,28 @@ function printCreateFormHandoff(format, profile, flowIdentifiers, options) {
2632
2658
  mobile_path: url.mobile_path,
2633
2659
  reply_markdown: replyMarkdown,
2634
2660
  agent_warning: agentWarning,
2661
+ prefill_token: prefill.token,
2662
+ prefill_expires_in: prefill.expires_in,
2663
+ prefill_expires_at: prefill.expires_at,
2635
2664
  init_value: initValue,
2636
2665
  init_value_keys: Object.keys(options.form),
2666
+ prefill_context: prefillContext,
2637
2667
  message: options.message
2638
2668
  });
2639
2669
  }
2640
- function parseInitValueFromUrl(rawUrl) {
2641
- const base = rawUrl.startsWith("http") ? void 0 : "https://bpmax.local";
2642
- const initValue = new URL(rawUrl, base).searchParams.get("init_value");
2643
- if (!initValue) {
2644
- return void 0;
2645
- }
2646
- return JSON.parse(initValue);
2647
- }
2648
- function assertCreateFormHandoffUrl(url, expectedInitValue, expectedFormKeys) {
2670
+ function assertCreateFormHandoffUrl(url, expectedInitValue, expectedFormKeys, expectedPrefillToken) {
2649
2671
  const targets = [url.url, url.pc_url, url.mobile_path].filter((item) => Boolean(item));
2650
2672
  for (const target of targets) {
2651
- const initValue = parseInitValueFromUrl(target);
2652
- const keyToValue = initValue?.key_to_value;
2653
- if (!initValue || typeof keyToValue !== "object" || keyToValue === null || Array.isArray(keyToValue)) {
2654
- throw new Error("Generated project create handoff URL is missing init_value.key_to_value; refusing to output an un-prefilled link.");
2655
- }
2656
- const missingKeys = expectedFormKeys.filter((key) => !Object.prototype.hasOwnProperty.call(keyToValue, key));
2657
- if (missingKeys.length > 0) {
2658
- throw new Error(`Generated project create handoff URL is missing prefilled form keys: ${missingKeys.join(", ")}`);
2673
+ const base = target.startsWith("http") ? void 0 : "https://bpmax.local";
2674
+ const prefillToken = new URL(target, base).searchParams.get("prefill_token");
2675
+ if (prefillToken !== expectedPrefillToken) {
2676
+ throw new Error("Generated project create handoff URL is missing prefill_token; refusing to output an un-prefilled link.");
2659
2677
  }
2660
2678
  }
2661
- const encoded = JSON.stringify(expectedInitValue);
2662
- if (!url.url.includes("init_value=") || !url.pc_url?.includes("init_value=") || !url.mobile_path?.includes("init_value=")) {
2663
- throw new Error(`Generated project create handoff URL did not include init_value (${encoded.length} bytes).`);
2679
+ const keyToValue = expectedInitValue.key_to_value;
2680
+ const missingKeys = expectedFormKeys.filter((key) => !Object.prototype.hasOwnProperty.call(keyToValue, key));
2681
+ if (missingKeys.length > 0) {
2682
+ throw new Error(`Generated project create handoff output is missing prefilled form keys: ${missingKeys.join(", ")}`);
2664
2683
  }
2665
2684
  }
2666
2685
  function fieldsForValidation(flowDetail, projectFormTemplate) {
@@ -2683,18 +2702,87 @@ function metaUsesTemplateProjectName(flowDetail) {
2683
2702
  function formValidationErrors(fields, form) {
2684
2703
  return validateFormAgainstFields(form, fields).errors;
2685
2704
  }
2686
- function assertHandoffFormReady(fields, form) {
2687
- const errors = formValidationErrors(fields, form);
2705
+ function isRequiredMissingError(error) {
2706
+ return / is required$/.test(error);
2707
+ }
2708
+ function assertHandoffFormCompatible(fields, form) {
2709
+ const errors = formValidationErrors(fields, form).filter((error) => !isRequiredMissingError(error));
2688
2710
  if (errors.length > 0) {
2689
2711
  throw new Error(
2690
2712
  [
2691
- "Form handoff cannot be generated because collected values are incomplete or not page-compatible.",
2692
- "Continue guiding the user to fill missing fields, or ask them to choose complex selector fields in the BPMAX page.",
2713
+ "Form handoff cannot be generated because collected values are not page-compatible.",
2714
+ "Ask the user to correct invalid values, or ask them to choose complex selector fields in the BPMAX page.",
2693
2715
  `Validation errors: ${errors.join("; ")}`
2694
2716
  ].join("\n")
2695
2717
  );
2696
2718
  }
2697
2719
  }
2720
+ function isEmptyValue(value) {
2721
+ return value === void 0 || value === null || value === "" || Array.isArray(value) && value.length === 0;
2722
+ }
2723
+ function isSuggestedManualField(field) {
2724
+ const type = field.type?.toLowerCase() ?? "";
2725
+ return /(plugin_|selector|relation_|sub[_-]?form|associated[_-]?form|table|upload|image|file|attachment)/.test(type);
2726
+ }
2727
+ function contextField(field, reason, options = {}) {
2728
+ return {
2729
+ key: options.path ?? field.key,
2730
+ ...field.label ? { label: field.label } : {},
2731
+ ...field.type ? { type: field.type } : {},
2732
+ required: field.required,
2733
+ reason,
2734
+ ...options.path ? { path: options.path } : {},
2735
+ ...options.rootKey ? { root_key: options.rootKey } : {},
2736
+ ...options.parentKey ? { parent_key: options.parentKey } : {}
2737
+ };
2738
+ }
2739
+ function collectPrefillContextForFields(fields, form, result, options = {}) {
2740
+ for (const field of fields) {
2741
+ const value = form[field.key];
2742
+ const path6 = options.prefix ? `${options.prefix}.${field.key}` : field.key;
2743
+ const fieldOptions = {
2744
+ path: path6,
2745
+ rootKey: options.rootKey ?? field.key,
2746
+ parentKey: options.parentKey
2747
+ };
2748
+ if (isEmptyValue(value)) {
2749
+ if (field.required) {
2750
+ result.missing_fields.push(contextField(field, "required_empty", fieldOptions));
2751
+ } else if (isSuggestedManualField(field)) {
2752
+ result.suggested_fields.push(contextField(field, "manual_confirm_recommended", fieldOptions));
2753
+ }
2754
+ continue;
2755
+ }
2756
+ result.filled_fields.push(contextField(field, "prefilled", fieldOptions));
2757
+ if (isSubFormField(field) && Array.isArray(value) && field.children?.length) {
2758
+ value.forEach((row, index) => {
2759
+ if (isRecord5(row)) {
2760
+ collectPrefillContextForFields(field.children ?? [], row, result, {
2761
+ prefix: `${field.key}[${index}]`,
2762
+ rootKey: field.key,
2763
+ parentKey: field.key
2764
+ });
2765
+ }
2766
+ });
2767
+ }
2768
+ }
2769
+ }
2770
+ function buildPrefillContext(fields, form) {
2771
+ const result = {
2772
+ missing_fields: [],
2773
+ suggested_fields: [],
2774
+ filled_fields: []
2775
+ };
2776
+ collectPrefillContextForFields(fields, form, result);
2777
+ return {
2778
+ ...result,
2779
+ summary: {
2780
+ missing_count: result.missing_fields.length,
2781
+ suggested_count: result.suggested_fields.length,
2782
+ filled_count: result.filled_fields.length
2783
+ }
2784
+ };
2785
+ }
2698
2786
  function isRecord5(value) {
2699
2787
  return typeof value === "object" && value !== null && !Array.isArray(value);
2700
2788
  }
@@ -2884,10 +2972,12 @@ function registerProjectCommands(program2) {
2884
2972
  return;
2885
2973
  }
2886
2974
  if (projectFormTemplate && hasAutoInitLambda(projectFormTemplate.template)) {
2887
- assertHandoffFormReady(fieldsForValidation(flowDetail, projectFormTemplate), form);
2888
- printCreateFormHandoff(options.format, profile, flowIdentifiers, {
2975
+ const handoffFields = fieldsForValidation(flowDetail, projectFormTemplate);
2976
+ assertHandoffFormCompatible(handoffFields, form);
2977
+ await printCreateFormHandoff(client, options.format, profile, flowIdentifiers, {
2889
2978
  name,
2890
2979
  form,
2980
+ fields: handoffFields,
2891
2981
  reason: "form_template_auto_init_lambda",
2892
2982
  message: "\u68C0\u6D4B\u5230\u8868\u5355\u6A21\u677F\u5305\u542B\u81EA\u52A8\u521D\u59CB\u5316\u811A\u672C\uFF0C\u5DF2\u751F\u6210 BPMAX \u521B\u5EFA\u9875\u94FE\u63A5\uFF0C\u8BF7\u70B9\u51FB\u7EE7\u7EED\u586B\u5199\u5E76\u63D0\u4EA4\u3002"
2893
2983
  });
@@ -2897,10 +2987,12 @@ function registerProjectCommands(program2) {
2897
2987
  if (!options.allowFormHandoff) {
2898
2988
  throw new Error("Form handoff requires --allow-form-handoff. Continue interactive filling by default.");
2899
2989
  }
2900
- assertHandoffFormReady(fieldsForValidation(flowDetail, projectFormTemplate), form);
2901
- printCreateFormHandoff(options.format, profile, flowIdentifiers, {
2990
+ const handoffFields = fieldsForValidation(flowDetail, projectFormTemplate);
2991
+ assertHandoffFormCompatible(handoffFields, form);
2992
+ await printCreateFormHandoff(client, options.format, profile, flowIdentifiers, {
2902
2993
  name,
2903
2994
  form,
2995
+ fields: handoffFields,
2904
2996
  message: "\u5DF2\u751F\u6210 BPMAX \u521B\u5EFA\u9875\u94FE\u63A5\uFF0C\u8BF7\u70B9\u51FB\u7EE7\u7EED\u586B\u5199\u5E76\u63D0\u4EA4\u3002"
2905
2997
  });
2906
2998
  return;
@@ -2912,20 +3004,19 @@ function registerProjectCommands(program2) {
2912
3004
  projectFormTemplate = await loadProjectFormTemplate();
2913
3005
  if (projectFormTemplate && hasAutoInitLambda(projectFormTemplate.template)) {
2914
3006
  const handoffFields = fieldsForValidation(flowDetail, projectFormTemplate);
2915
- const missingFields = formValidationErrors(handoffFields, form);
2916
- if (!useInteractive && missingFields.length > 0) {
3007
+ if (Object.keys(form).length === 0) {
2917
3008
  throw new Error(
2918
3009
  [
2919
- "Form template has auto initialization scripts and cannot be handed off before required fields are collected.",
2920
- "Rerun without --no-interactive to guide the user through the form, or provide --json.form with the missing values.",
2921
- `Missing required fields: ${missingFields.join(", ")}`
3010
+ "Form template has auto initialization scripts and cannot be handed off before any form values are collected.",
3011
+ "Rerun without --no-interactive to guide the user through the form, or provide --json.form with the missing values."
2922
3012
  ].join("\n")
2923
3013
  );
2924
3014
  }
2925
- assertHandoffFormReady(handoffFields, form);
2926
- printCreateFormHandoff(options.format, profile, flowIdentifiers, {
3015
+ assertHandoffFormCompatible(handoffFields, form);
3016
+ await printCreateFormHandoff(client, options.format, profile, flowIdentifiers, {
2927
3017
  name: name ?? "",
2928
3018
  form,
3019
+ fields: handoffFields,
2929
3020
  reason: "form_template_auto_init_lambda",
2930
3021
  message: "\u68C0\u6D4B\u5230\u8868\u5355\u6A21\u677F\u5305\u542B\u81EA\u52A8\u521D\u59CB\u5316\u811A\u672C\uFF0C\u5DF2\u751F\u6210 BPMAX \u521B\u5EFA\u9875\u94FE\u63A5\uFF0C\u8BF7\u70B9\u51FB\u7EE7\u7EED\u586B\u5199\u5E76\u63D0\u4EA4\u3002"
2931
3022
  });