farseer-cli 1.1.3 → 1.2.1

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.
@@ -37,6 +37,7 @@ var path = __toESM(require("path"));
37
37
  var os = __toESM(require("os"));
38
38
  var import_farseerFactory = require("../../services/farseerFactory");
39
39
  var import_helpers = require("../utils/helpers");
40
+ var farseer = __toESM(require("farseer-client"));
40
41
  var import_farseer_client = require("farseer-client");
41
42
  const ROLLUP_TYPE_MAP = {
42
43
  sum: "SUM",
@@ -88,7 +89,9 @@ const manageDimensionMembersSchema = {
88
89
  action: import_zod.z.enum(["create", "delete"]).describe("Action to perform"),
89
90
  tableName: import_zod.z.string().describe("Dimension table name"),
90
91
  memberName: import_zod.z.string().describe("Member name to create or delete"),
91
- parentMemberName: import_zod.z.string().optional().describe("Parent member name (for create with hierarchy)")
92
+ parentMemberName: import_zod.z.string().optional().describe("Parent member name (for create with hierarchy)"),
93
+ compound: import_zod.z.boolean().optional().describe("Set to true to create a compound (calculated) member. Use with formula param."),
94
+ formula: import_zod.z.string().optional().describe('Formula for compound members (e.g., "$M1+$M2"). Only used with compound=true.')
92
95
  };
93
96
  const createVariableSchema = {
94
97
  organisation: import_zod.z.string().optional().describe("Farseer organisation (subdomain). If omitted, uses checked-out org or saved mapping."),
@@ -99,17 +102,37 @@ const createVariableSchema = {
99
102
  rollupType: import_zod.z.string().optional().describe("Rollup type (Sum, Average, Min, Max, None, Formula, Closing State, Beginning State)"),
100
103
  dataType: import_zod.z.string().optional().describe("Data type (Decimal, Integer, Text)"),
101
104
  description: import_zod.z.string().optional().describe("Variable description"),
102
- readonly: import_zod.z.boolean().optional().describe("Whether the variable is read-only")
105
+ readonly: import_zod.z.boolean().optional().describe("Whether the variable is read-only"),
106
+ measure: import_zod.z.boolean().optional().describe("Set to true for dimensionless formula variables (measure mode)"),
107
+ numberFormat: import_zod.z.record(import_zod.z.any()).optional().describe('Number format config (e.g., { type: "PERCENTAGE", decimals: 1 }). Saves a separate update call.'),
108
+ folderPath: import_zod.z.array(import_zod.z.string()).optional().describe('Folder path to place the variable in (e.g., ["Variables", "Revenue"]). Uses create + move.')
103
109
  };
104
110
  const updateVariableSchema = {
105
111
  organisation: import_zod.z.string().optional().describe("Farseer organisation (subdomain). If omitted, uses checked-out org or saved mapping."),
106
112
  tenant: import_zod.z.string().optional().describe("Tenant ID. Uses checked-out tenant if omitted."),
107
- name: import_zod.z.string().describe("Variable name to update"),
113
+ name: import_zod.z.string().describe("Variable name to update (used for lookup)"),
114
+ newName: import_zod.z.string().optional().describe("Rename the variable to this name"),
108
115
  dimensions: import_zod.z.array(import_zod.z.string()).optional().describe("New dimension table names (replaces all existing dimensions)"),
109
- formula: import_zod.z.string().optional().describe("New formula expression"),
116
+ formula: import_zod.z.string().optional().describe(
117
+ 'New formula expression. Supports $GID syntax for direct references (e.g., "$155-$297"). Pass the formula string as-is.'
118
+ ),
110
119
  rollupType: import_zod.z.string().optional().describe("New rollup type"),
111
120
  description: import_zod.z.string().optional().describe("New description"),
112
- readonly: import_zod.z.boolean().optional().describe("Set read-only status")
121
+ readonly: import_zod.z.boolean().optional().describe("Set read-only status"),
122
+ polarity: import_zod.z.enum(["POSITIVE", "NEGATIVE"]).optional().describe("Number format polarity (POSITIVE or NEGATIVE)"),
123
+ numberFormat: import_zod.z.record(import_zod.z.any()).optional().describe(
124
+ "Full number format configuration object. Fields: type, custom, formatString, decimals, polarity, negatives, unit, currency. Use this for fine-grained control over number formatting."
125
+ )
126
+ };
127
+ const deleteVariableSchema = {
128
+ organisation: import_zod.z.string().optional().describe("Farseer organisation (subdomain). If omitted, uses checked-out org or saved mapping."),
129
+ tenant: import_zod.z.string().optional().describe("Tenant ID. Uses checked-out tenant if omitted."),
130
+ variableName: import_zod.z.string().describe("Variable name to delete")
131
+ };
132
+ const deleteVariableDataSchema = {
133
+ organisation: import_zod.z.string().optional().describe("Farseer organisation (subdomain). If omitted, uses checked-out org or saved mapping."),
134
+ tenant: import_zod.z.string().optional().describe("Tenant ID. Uses checked-out tenant if omitted."),
135
+ variableName: import_zod.z.string().describe("Variable name whose data to delete")
113
136
  };
114
137
  const exportModelSchema = {
115
138
  tenant: import_zod.z.string().optional().describe('Tenant ID (e.g. "salvus-prodaja"). Uses checked-out tenant if omitted.'),
@@ -288,8 +311,21 @@ If no tenant is specified, uses the currently checked-out tenant.`,
288
311
  }
289
312
  try {
290
313
  const model = await clientResult.client.exportModel();
314
+ const gidMap = /* @__PURE__ */ new Map();
315
+ const nativeClient = await (0, import_farseerFactory.getNativeFarseerClient)(organisation, tenant);
316
+ if (nativeClient) {
317
+ try {
318
+ await nativeClient.initTagMap();
319
+ for (const v of model.variables) {
320
+ const tag = await nativeClient.getVariable(v.name);
321
+ gidMap.set(v.name, tag.id);
322
+ }
323
+ } catch {
324
+ }
325
+ }
291
326
  const variables = model.variables.map((v) => ({
292
327
  name: v.name,
328
+ entityGid: gidMap.get(v.name) || null,
293
329
  dimensions: v.tables || [],
294
330
  hasFormula: !!v.formula,
295
331
  rollupType: v.rollupType,
@@ -400,10 +436,21 @@ If no tenant is specified, uses the currently checked-out tenant.`,
400
436
  if (!variable) {
401
437
  return (0, import_helpers.errorResponse)(`Variable "${params.variableName}" not found`, "NOT_FOUND");
402
438
  }
439
+ let entityGid = null;
440
+ const nativeClient = await (0, import_farseerFactory.getNativeFarseerClient)(organisation, tenant);
441
+ if (nativeClient) {
442
+ try {
443
+ await nativeClient.initTagMap();
444
+ const tag = await nativeClient.getVariable(variable.name);
445
+ entityGid = tag.id;
446
+ } catch {
447
+ }
448
+ }
403
449
  return (0, import_helpers.successResponse)({
404
450
  tenant,
405
451
  variable: {
406
452
  name: variable.name,
453
+ entityGid,
407
454
  description: variable.description,
408
455
  formula: variable.formula,
409
456
  dimensions: variable.tables || [],
@@ -548,6 +595,8 @@ If no tenant is specified, uses the currently checked-out tenant.`,
548
595
  Actions:
549
596
  - "create": Creates a new member in the specified dimension table.
550
597
  Optionally set a parent member for hierarchical tables.
598
+ Set compound=true with a formula to create a compound (calculated) member
599
+ (e.g., for AC Period / FC Period filters on variance rows).
551
600
  - "delete": Deletes an existing member by name from the specified table.
552
601
 
553
602
  WARNING: Delete is destructive and may affect data. Use with caution.
@@ -585,6 +634,12 @@ If no tenant is specified, uses the currently checked-out tenant.`,
585
634
  order: 0
586
635
  };
587
636
  }
637
+ if (params.compound) {
638
+ createEntity.calculation = {
639
+ type: "COMPOUND",
640
+ formula: params.formula || ""
641
+ };
642
+ }
588
643
  const result = await nativeClient.entities.mutate({ created: [createEntity] });
589
644
  return (0, import_helpers.successResponse)({
590
645
  tenant,
@@ -615,6 +670,76 @@ If no tenant is specified, uses the currently checked-out tenant.`,
615
670
  }
616
671
  }
617
672
  );
673
+ server.tool(
674
+ "farseer_create_table",
675
+ `Create a new dimension table in a Farseer tenant.
676
+
677
+ Dimension tables define the structure of data (e.g., Product, Region, Compound Members).
678
+ Creates an empty table that you can then add members to with farseer_manage_dimension_members.
679
+
680
+ If no tenant is specified, uses the currently checked-out tenant.`,
681
+ {
682
+ organisation: import_zod.z.string().optional().describe("Farseer organisation (subdomain). If omitted, uses checked-out org or saved mapping."),
683
+ tenant: import_zod.z.string().optional().describe("Tenant ID. Uses checked-out tenant if omitted."),
684
+ name: import_zod.z.string().describe("Dimension table name"),
685
+ folderPath: import_zod.z.array(import_zod.z.string()).optional().describe('Folder path (e.g., ["Dimensions"]). Uses create + move.')
686
+ },
687
+ // @ts-expect-error TS2589 - MCP SDK deep type inference with zod schemas
688
+ async (params) => {
689
+ const resolved = (0, import_helpers.resolveOrgAndTenant)(params);
690
+ if (!resolved) return (0, import_helpers.tenantRequiredResponse)();
691
+ const { organisation, tenant } = resolved;
692
+ const nativeClient = await (0, import_farseerFactory.getNativeFarseerClient)(organisation, tenant);
693
+ if (!nativeClient) return (0, import_helpers.authRequiredResponse)(tenant);
694
+ try {
695
+ await nativeClient.initTagMap();
696
+ const createEntity = {
697
+ type: import_farseer_client.EntityType.DimensionTable,
698
+ meta: { name: params.name, description: null }
699
+ };
700
+ if (params.folderPath && params.folderPath.length > 0) {
701
+ try {
702
+ let currentFolderId = null;
703
+ for (const segment of params.folderPath) {
704
+ const parentItems = await nativeClient.folders.listItemsBatch([{ id: currentFolderId }]);
705
+ const existing = parentItems[0]?.items?.find(
706
+ (i) => i.type === farseer.FolderItemType.Folder && i.name.toLowerCase() === segment.toLowerCase()
707
+ );
708
+ if (existing) {
709
+ currentFolderId = existing.id;
710
+ } else {
711
+ const newFolder = await nativeClient.folders.create({
712
+ parentId: currentFolderId,
713
+ name: segment,
714
+ allowedTypes: null
715
+ });
716
+ currentFolderId = newFolder.id;
717
+ }
718
+ }
719
+ if (currentFolderId) {
720
+ createEntity.relations = {
721
+ folder: { key: { id: currentFolderId, type: import_farseer_client.EntityType.Folder }, order: 0 }
722
+ };
723
+ }
724
+ } catch {
725
+ }
726
+ }
727
+ const result = await nativeClient.entities.mutate({ created: [createEntity] });
728
+ const created = result.created?.[0];
729
+ return (0, import_helpers.successResponse)({
730
+ tenant,
731
+ action: "created",
732
+ table: {
733
+ id: created?.id,
734
+ name: params.name
735
+ },
736
+ message: `Dimension table "${params.name}" created`
737
+ });
738
+ } catch (error) {
739
+ return (0, import_helpers.errorResponse)(`Create table failed: ${await extractErrorMessage(error)}`, "NETWORK_ERROR");
740
+ }
741
+ }
742
+ );
618
743
  server.tool(
619
744
  "farseer_create_variable",
620
745
  `Create a new variable in a Farseer tenant.
@@ -661,12 +786,43 @@ If no tenant is specified, uses the currently checked-out tenant.`,
661
786
  tableGids,
662
787
  rollupType,
663
788
  dataType,
664
- readonly: params.readonly || false
789
+ readonly: params.readonly || false,
790
+ measure: params.measure || false
665
791
  }
666
792
  };
667
793
  if (params.formula) {
668
794
  createEntity.calculation = { formula: params.formula };
669
795
  }
796
+ if (params.numberFormat) {
797
+ createEntity.numberFormat = params.numberFormat;
798
+ }
799
+ if (params.folderPath && params.folderPath.length > 0) {
800
+ try {
801
+ let currentFolderId = null;
802
+ for (const segment of params.folderPath) {
803
+ const parentItems = await nativeClient.folders.listItemsBatch([{ id: currentFolderId }]);
804
+ const existing = parentItems[0]?.items?.find(
805
+ (i) => i.type === farseer.FolderItemType.Folder && i.name.toLowerCase() === segment.toLowerCase()
806
+ );
807
+ if (existing) {
808
+ currentFolderId = existing.id;
809
+ } else {
810
+ const newFolder = await nativeClient.folders.create({
811
+ parentId: currentFolderId,
812
+ name: segment,
813
+ allowedTypes: null
814
+ });
815
+ currentFolderId = newFolder.id;
816
+ }
817
+ }
818
+ if (currentFolderId) {
819
+ createEntity.relations = {
820
+ folder: { key: { id: currentFolderId, type: import_farseer_client.EntityType.Folder }, order: 0 }
821
+ };
822
+ }
823
+ } catch {
824
+ }
825
+ }
670
826
  const result = await nativeClient.entities.mutate({ created: [createEntity] });
671
827
  return (0, import_helpers.successResponse)({
672
828
  tenant,
@@ -688,14 +844,18 @@ Resolves the variable by name and updates the specified fields.
688
844
  Only provided fields are updated; omitted fields remain unchanged.
689
845
 
690
846
  Updatable fields:
847
+ - newName: Rename the variable
691
848
  - dimensions: Change which dimension tables the variable spans (replaces all existing dimensions)
692
- - formula: Change the calculation formula
849
+ - formula: Change the calculation formula (supports $GID syntax like "$155-$297")
693
850
  - rollupType: Change aggregation method (Sum, Average, Min, Max, etc.)
694
851
  - description: Change the variable description
695
852
  - readonly: Set or clear read-only status
853
+ - polarity: Set number polarity (POSITIVE or NEGATIVE)
854
+ - numberFormat: Full number format config (type, decimals, polarity, unit, currency, etc.)
696
855
 
697
856
  If no tenant is specified, uses the currently checked-out tenant.`,
698
857
  updateVariableSchema,
858
+ // @ts-expect-error TS2589 - MCP SDK deep type inference with zod schemas
699
859
  async (params) => {
700
860
  const resolved = (0, import_helpers.resolveOrgAndTenant)(params);
701
861
  if (!resolved) return (0, import_helpers.tenantRequiredResponse)();
@@ -712,7 +872,7 @@ If no tenant is specified, uses the currently checked-out tenant.`,
712
872
  id: variable.id,
713
873
  type: import_farseer_client.EntityType.Variable,
714
874
  meta: {
715
- name: variable.name,
875
+ name: params.newName || variable.name,
716
876
  description: params.description !== void 0 ? params.description : current.meta?.description ?? null
717
877
  },
718
878
  variable: {
@@ -744,8 +904,24 @@ If no tenant is specified, uses the currently checked-out tenant.`,
744
904
  if (params.readonly !== void 0) {
745
905
  updateEntity.variable.readonly = params.readonly;
746
906
  }
907
+ const currentCalc = current.calculation;
747
908
  if (params.formula !== void 0) {
748
- updateEntity.calculation = { formula: params.formula };
909
+ updateEntity.calculation = {
910
+ formula: params.formula,
911
+ type: currentCalc?.type || "VARIABLE"
912
+ };
913
+ } else if (currentCalc) {
914
+ updateEntity.calculation = currentCalc;
915
+ }
916
+ if (params.numberFormat || params.polarity) {
917
+ const currentFormat = current.numberFormat || {};
918
+ updateEntity.numberFormat = {
919
+ ...currentFormat,
920
+ ...params.numberFormat || {}
921
+ };
922
+ if (params.polarity) {
923
+ updateEntity.numberFormat.polarity = params.polarity;
924
+ }
749
925
  }
750
926
  const result = await nativeClient.entities.mutate({ updated: [updateEntity] });
751
927
  return (0, import_helpers.successResponse)({
@@ -760,6 +936,79 @@ If no tenant is specified, uses the currently checked-out tenant.`,
760
936
  }
761
937
  }
762
938
  );
939
+ server.tool(
940
+ "farseer_delete_variable_data",
941
+ `Delete ALL data for a variable in a Farseer tenant.
942
+
943
+ WARNING: This is destructive and cannot be undone. It removes all cell values
944
+ for the specified variable across all dimensions and time periods.
945
+
946
+ Use this to clear old template data or reset a variable before re-importing.
947
+
948
+ If no tenant is specified, uses the currently checked-out tenant.`,
949
+ deleteVariableDataSchema,
950
+ async (params) => {
951
+ const resolved = (0, import_helpers.resolveOrgAndTenant)(params);
952
+ if (!resolved) return (0, import_helpers.tenantRequiredResponse)();
953
+ const { organisation, tenant } = resolved;
954
+ const nativeClient = await (0, import_farseerFactory.getNativeFarseerClient)(organisation, tenant);
955
+ if (!nativeClient) return (0, import_helpers.authRequiredResponse)(tenant);
956
+ try {
957
+ await nativeClient.initTagMap();
958
+ const variable = await nativeClient.getVariable(params.variableName);
959
+ await nativeClient.entities.deleteData({
960
+ id: variable.id,
961
+ type: import_farseer_client.EntityType.Variable
962
+ });
963
+ return (0, import_helpers.successResponse)({
964
+ tenant,
965
+ action: "deleted_data",
966
+ variable: params.variableName,
967
+ variableId: variable.id,
968
+ message: `All data deleted for variable "${params.variableName}"`
969
+ });
970
+ } catch (error) {
971
+ return (0, import_helpers.errorResponse)(
972
+ `Delete variable data failed: ${await extractErrorMessage(error)}`,
973
+ "NETWORK_ERROR"
974
+ );
975
+ }
976
+ }
977
+ );
978
+ server.tool(
979
+ "farseer_delete_variable",
980
+ `Delete a variable from a Farseer tenant.
981
+
982
+ Resolves the variable by name and permanently deletes it along with all its data.
983
+
984
+ WARNING: This is destructive and cannot be undone.
985
+
986
+ If no tenant is specified, uses the currently checked-out tenant.`,
987
+ deleteVariableSchema,
988
+ async (params) => {
989
+ const resolved = (0, import_helpers.resolveOrgAndTenant)(params);
990
+ if (!resolved) return (0, import_helpers.tenantRequiredResponse)();
991
+ const { organisation, tenant } = resolved;
992
+ const nativeClient = await (0, import_farseerFactory.getNativeFarseerClient)(organisation, tenant);
993
+ if (!nativeClient) return (0, import_helpers.authRequiredResponse)(tenant);
994
+ try {
995
+ await nativeClient.initTagMap();
996
+ const variable = await nativeClient.getVariable(params.variableName);
997
+ await nativeClient.entities.mutate({
998
+ deleted: [{ id: variable.id, type: import_farseer_client.EntityType.Variable }]
999
+ });
1000
+ return (0, import_helpers.successResponse)({
1001
+ tenant,
1002
+ action: "deleted",
1003
+ variable: params.variableName,
1004
+ variableId: variable.id,
1005
+ message: `Variable "${params.variableName}" deleted`
1006
+ });
1007
+ } catch (error) {
1008
+ return (0, import_helpers.errorResponse)(`Delete variable failed: ${await extractErrorMessage(error)}`, "NETWORK_ERROR");
1009
+ }
1010
+ }
1011
+ );
763
1012
  }
764
1013
  // Annotate the CommonJS export names for ESM import in node:
765
1014
  0 && (module.exports = {