@tpmjs/official-revenue-breakdown 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-2025 TPMJS
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,62 @@
1
+ import * as ai from 'ai';
2
+
3
+ /**
4
+ * Revenue Breakdown Tool for TPMJS
5
+ * Breaks down revenue by segment, product, or period with growth rates
6
+ */
7
+ /**
8
+ * Revenue data item
9
+ */
10
+ interface RevenueItem {
11
+ product?: string;
12
+ segment?: string;
13
+ region?: string;
14
+ period?: string;
15
+ amount: number;
16
+ }
17
+ /**
18
+ * Breakdown dimension type
19
+ */
20
+ type Dimension = 'product' | 'segment' | 'region' | 'period';
21
+ /**
22
+ * Revenue breakdown item with growth metrics
23
+ */
24
+ interface BreakdownItem {
25
+ name: string;
26
+ revenue: number;
27
+ percentage: number;
28
+ growth?: number;
29
+ growthPercentage?: number;
30
+ periods?: Array<{
31
+ period: string;
32
+ revenue: number;
33
+ }>;
34
+ }
35
+ /**
36
+ * Input interface for revenue breakdown
37
+ */
38
+ interface RevenueBreakdownInput {
39
+ revenue: RevenueItem[];
40
+ dimension: Dimension;
41
+ }
42
+ /**
43
+ * Output interface for revenue breakdown
44
+ */
45
+ interface RevenueBreakdownResult {
46
+ breakdown: BreakdownItem[];
47
+ summary: {
48
+ totalRevenue: number;
49
+ numberOfCategories: number;
50
+ topCategory: string;
51
+ topCategoryRevenue: number;
52
+ topCategoryPercentage: number;
53
+ averageGrowth?: number;
54
+ };
55
+ }
56
+ /**
57
+ * Revenue Breakdown Tool
58
+ * Analyzes revenue by specified dimension and calculates growth rates
59
+ */
60
+ declare const revenueBreakdownTool: ai.Tool<RevenueBreakdownInput, RevenueBreakdownResult>;
61
+
62
+ export { type RevenueBreakdownResult, revenueBreakdownTool as default, revenueBreakdownTool };
package/dist/index.js ADDED
@@ -0,0 +1,149 @@
1
+ import { tool, jsonSchema } from 'ai';
2
+
3
+ // src/index.ts
4
+ var revenueBreakdownTool = tool({
5
+ description: "Breaks down revenue by segment, product, region, or period. Calculates period-over-period growth rates and identifies top performers.",
6
+ inputSchema: jsonSchema({
7
+ type: "object",
8
+ properties: {
9
+ revenue: {
10
+ type: "array",
11
+ description: "Revenue data with segments and periods",
12
+ items: {
13
+ type: "object",
14
+ properties: {
15
+ product: {
16
+ type: "string",
17
+ description: "Product name"
18
+ },
19
+ segment: {
20
+ type: "string",
21
+ description: "Business segment"
22
+ },
23
+ region: {
24
+ type: "string",
25
+ description: "Geographic region"
26
+ },
27
+ period: {
28
+ type: "string",
29
+ description: 'Time period (e.g., "2024-Q1", "2024-01")'
30
+ },
31
+ amount: {
32
+ type: "number",
33
+ description: "Revenue amount"
34
+ }
35
+ },
36
+ required: ["amount"]
37
+ }
38
+ },
39
+ dimension: {
40
+ type: "string",
41
+ enum: ["product", "segment", "region", "period"],
42
+ description: "Breakdown dimension"
43
+ }
44
+ },
45
+ required: ["revenue", "dimension"],
46
+ additionalProperties: false
47
+ }),
48
+ execute: async ({ revenue, dimension }) => {
49
+ if (!Array.isArray(revenue) || revenue.length === 0) {
50
+ throw new Error("Revenue must be a non-empty array");
51
+ }
52
+ const validDimensions = ["product", "segment", "region", "period"];
53
+ if (!validDimensions.includes(dimension)) {
54
+ throw new Error(
55
+ `Invalid dimension: ${dimension}. Must be one of: ${validDimensions.join(", ")}`
56
+ );
57
+ }
58
+ const revenueMap = /* @__PURE__ */ new Map();
59
+ const periodMap = /* @__PURE__ */ new Map();
60
+ let totalRevenue = 0;
61
+ for (const item of revenue) {
62
+ if (typeof item.amount !== "number" || item.amount < 0) {
63
+ throw new Error("Each revenue item must have a non-negative amount");
64
+ }
65
+ const key = getKeyForDimension(item, dimension);
66
+ if (!key) {
67
+ throw new Error(
68
+ `Revenue item missing required field for dimension "${dimension}": ${JSON.stringify(item)}`
69
+ );
70
+ }
71
+ const current = revenueMap.get(key) || [];
72
+ current.push(item.amount);
73
+ revenueMap.set(key, current);
74
+ if (item.period) {
75
+ if (!periodMap.has(key)) {
76
+ periodMap.set(key, /* @__PURE__ */ new Map());
77
+ }
78
+ const periods = periodMap.get(key);
79
+ const periodRevenue = periods.get(item.period) || 0;
80
+ periods.set(item.period, periodRevenue + item.amount);
81
+ }
82
+ totalRevenue += item.amount;
83
+ }
84
+ if (totalRevenue === 0) {
85
+ throw new Error("Total revenue cannot be zero");
86
+ }
87
+ const breakdown = [];
88
+ for (const [name, amounts] of revenueMap.entries()) {
89
+ const itemRevenue = amounts.reduce((sum, amount) => sum + amount, 0);
90
+ const percentage = itemRevenue / totalRevenue * 100;
91
+ const breakdownItem = {
92
+ name,
93
+ revenue: Math.round(itemRevenue * 100) / 100,
94
+ percentage: Math.round(percentage * 100) / 100
95
+ };
96
+ if (periodMap.has(name)) {
97
+ const periods = periodMap.get(name);
98
+ const periodEntries = Array.from(periods.entries()).sort(
99
+ (a, b) => a[0].localeCompare(b[0])
100
+ );
101
+ if (periodEntries.length >= 2) {
102
+ const oldestPeriod = periodEntries[0];
103
+ const newestPeriod = periodEntries[periodEntries.length - 1];
104
+ const growth = newestPeriod[1] - oldestPeriod[1];
105
+ const growthPercentage = oldestPeriod[1] !== 0 ? growth / oldestPeriod[1] * 100 : 0;
106
+ breakdownItem.growth = Math.round(growth * 100) / 100;
107
+ breakdownItem.growthPercentage = Math.round(growthPercentage * 100) / 100;
108
+ }
109
+ breakdownItem.periods = periodEntries.map(([period, revenue2]) => ({
110
+ period,
111
+ revenue: Math.round(revenue2 * 100) / 100
112
+ }));
113
+ }
114
+ breakdown.push(breakdownItem);
115
+ }
116
+ breakdown.sort((a, b) => b.revenue - a.revenue);
117
+ const topCategory = breakdown[0];
118
+ const growthRates = breakdown.filter((item) => item.growthPercentage !== void 0).map((item) => item.growthPercentage);
119
+ const averageGrowth = growthRates.length > 0 ? Math.round(growthRates.reduce((sum, g) => sum + g, 0) / growthRates.length * 100) / 100 : void 0;
120
+ return {
121
+ breakdown,
122
+ summary: {
123
+ totalRevenue: Math.round(totalRevenue * 100) / 100,
124
+ numberOfCategories: breakdown.length,
125
+ topCategory: topCategory.name,
126
+ topCategoryRevenue: topCategory.revenue,
127
+ topCategoryPercentage: topCategory.percentage,
128
+ averageGrowth
129
+ }
130
+ };
131
+ }
132
+ });
133
+ function getKeyForDimension(item, dimension) {
134
+ switch (dimension) {
135
+ case "product":
136
+ return item.product || null;
137
+ case "segment":
138
+ return item.segment || null;
139
+ case "region":
140
+ return item.region || null;
141
+ case "period":
142
+ return item.period || null;
143
+ default:
144
+ return null;
145
+ }
146
+ }
147
+ var index_default = revenueBreakdownTool;
148
+
149
+ export { index_default as default, revenueBreakdownTool };
package/package.json ADDED
@@ -0,0 +1,76 @@
1
+ {
2
+ "name": "@tpmjs/official-revenue-breakdown",
3
+ "version": "0.1.0",
4
+ "description": "Breaks down revenue by segment, product, or period with growth rates",
5
+ "type": "module",
6
+ "keywords": [
7
+ "tpmjs",
8
+ "finance",
9
+ "revenue",
10
+ "breakdown",
11
+ "growth"
12
+ ],
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "default": "./dist/index.js"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "devDependencies": {
23
+ "tsup": "^8.3.5",
24
+ "typescript": "^5.9.3",
25
+ "@tpmjs/tsconfig": "0.0.0"
26
+ },
27
+ "publishConfig": {
28
+ "access": "public"
29
+ },
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "https://github.com/anthropics/tpmjs.git",
33
+ "directory": "packages/tools/official/revenue-breakdown"
34
+ },
35
+ "homepage": "https://tpmjs.com",
36
+ "license": "MIT",
37
+ "tpmjs": {
38
+ "category": "finance",
39
+ "frameworks": [
40
+ "vercel-ai"
41
+ ],
42
+ "tools": [
43
+ {
44
+ "name": "revenueBreakdownTool",
45
+ "description": "Breaks down revenue by segment, product, or period with growth rates",
46
+ "parameters": [
47
+ {
48
+ "name": "revenue",
49
+ "type": "array",
50
+ "description": "Revenue data with segments and periods",
51
+ "required": true
52
+ },
53
+ {
54
+ "name": "dimension",
55
+ "type": "string",
56
+ "description": "Breakdown dimension (product, segment, region, period)",
57
+ "required": true
58
+ }
59
+ ],
60
+ "returns": {
61
+ "type": "RevenueBreakdownResult",
62
+ "description": "Revenue breakdown with growth rates and summary"
63
+ }
64
+ }
65
+ ]
66
+ },
67
+ "dependencies": {
68
+ "ai": "6.0.0-beta.124"
69
+ },
70
+ "scripts": {
71
+ "build": "tsup",
72
+ "dev": "tsup --watch",
73
+ "type-check": "tsc --noEmit",
74
+ "clean": "rm -rf dist .turbo"
75
+ }
76
+ }