@tpmjs/official-budget-variance 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 +21 -0
- package/dist/index.d.ts +63 -0
- package/dist/index.js +142 -0
- package/package.json +76 -0
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.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import * as ai from 'ai';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Budget Variance Tool for TPMJS
|
|
5
|
+
* Calculates budget vs actual variance with percentage and trend analysis
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Budget line item
|
|
9
|
+
*/
|
|
10
|
+
interface BudgetItem {
|
|
11
|
+
category: string;
|
|
12
|
+
amount: number;
|
|
13
|
+
period?: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Actual spending line item
|
|
17
|
+
*/
|
|
18
|
+
interface ActualItem {
|
|
19
|
+
category: string;
|
|
20
|
+
amount: number;
|
|
21
|
+
period?: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Variance analysis for a single category
|
|
25
|
+
*/
|
|
26
|
+
interface VarianceItem {
|
|
27
|
+
category: string;
|
|
28
|
+
budget: number;
|
|
29
|
+
actual: number;
|
|
30
|
+
variance: number;
|
|
31
|
+
percentageVariance: number;
|
|
32
|
+
trend: 'favorable' | 'unfavorable' | 'neutral';
|
|
33
|
+
status: 'over' | 'under' | 'on-track';
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Input interface for budget variance calculation
|
|
37
|
+
*/
|
|
38
|
+
interface BudgetVarianceInput {
|
|
39
|
+
budget: BudgetItem[];
|
|
40
|
+
actual: ActualItem[];
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Output interface for budget variance analysis
|
|
44
|
+
*/
|
|
45
|
+
interface BudgetVarianceResult {
|
|
46
|
+
variances: VarianceItem[];
|
|
47
|
+
summary: {
|
|
48
|
+
totalBudget: number;
|
|
49
|
+
totalActual: number;
|
|
50
|
+
totalVariance: number;
|
|
51
|
+
overallPercentageVariance: number;
|
|
52
|
+
categoriesOverBudget: number;
|
|
53
|
+
categoriesUnderBudget: number;
|
|
54
|
+
categoriesOnTrack: number;
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Budget Variance Tool
|
|
59
|
+
* Calculates variance between budgeted and actual amounts with trend analysis
|
|
60
|
+
*/
|
|
61
|
+
declare const budgetVarianceTool: ai.Tool<BudgetVarianceInput, BudgetVarianceResult>;
|
|
62
|
+
|
|
63
|
+
export { type BudgetVarianceResult, budgetVarianceTool, budgetVarianceTool as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { tool, jsonSchema } from 'ai';
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
var budgetVarianceTool = tool({
|
|
5
|
+
description: "Calculates budget vs actual variance with percentage and trend analysis. Identifies favorable and unfavorable trends, and provides summary statistics.",
|
|
6
|
+
inputSchema: jsonSchema({
|
|
7
|
+
type: "object",
|
|
8
|
+
properties: {
|
|
9
|
+
budget: {
|
|
10
|
+
type: "array",
|
|
11
|
+
description: "Budget line items with category and amount",
|
|
12
|
+
items: {
|
|
13
|
+
type: "object",
|
|
14
|
+
properties: {
|
|
15
|
+
category: {
|
|
16
|
+
type: "string",
|
|
17
|
+
description: "Budget category name"
|
|
18
|
+
},
|
|
19
|
+
amount: {
|
|
20
|
+
type: "number",
|
|
21
|
+
description: "Budgeted amount"
|
|
22
|
+
},
|
|
23
|
+
period: {
|
|
24
|
+
type: "string",
|
|
25
|
+
description: 'Optional budget period (e.g., "2024-Q1")'
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
required: ["category", "amount"]
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
actual: {
|
|
32
|
+
type: "array",
|
|
33
|
+
description: "Actual spending line items with category and amount",
|
|
34
|
+
items: {
|
|
35
|
+
type: "object",
|
|
36
|
+
properties: {
|
|
37
|
+
category: {
|
|
38
|
+
type: "string",
|
|
39
|
+
description: "Spending category name"
|
|
40
|
+
},
|
|
41
|
+
amount: {
|
|
42
|
+
type: "number",
|
|
43
|
+
description: "Actual amount spent"
|
|
44
|
+
},
|
|
45
|
+
period: {
|
|
46
|
+
type: "string",
|
|
47
|
+
description: 'Optional spending period (e.g., "2024-Q1")'
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
required: ["category", "amount"]
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
required: ["budget", "actual"],
|
|
55
|
+
additionalProperties: false
|
|
56
|
+
}),
|
|
57
|
+
execute: async ({ budget, actual }) => {
|
|
58
|
+
if (!Array.isArray(budget) || budget.length === 0) {
|
|
59
|
+
throw new Error("Budget must be a non-empty array");
|
|
60
|
+
}
|
|
61
|
+
if (!Array.isArray(actual) || actual.length === 0) {
|
|
62
|
+
throw new Error("Actual must be a non-empty array");
|
|
63
|
+
}
|
|
64
|
+
const budgetMap = /* @__PURE__ */ new Map();
|
|
65
|
+
const actualMap = /* @__PURE__ */ new Map();
|
|
66
|
+
for (const item of budget) {
|
|
67
|
+
if (!item.category || typeof item.amount !== "number") {
|
|
68
|
+
throw new Error("Each budget item must have a category and amount");
|
|
69
|
+
}
|
|
70
|
+
const current = budgetMap.get(item.category) || 0;
|
|
71
|
+
budgetMap.set(item.category, current + item.amount);
|
|
72
|
+
}
|
|
73
|
+
for (const item of actual) {
|
|
74
|
+
if (!item.category || typeof item.amount !== "number") {
|
|
75
|
+
throw new Error("Each actual item must have a category and amount");
|
|
76
|
+
}
|
|
77
|
+
const current = actualMap.get(item.category) || 0;
|
|
78
|
+
actualMap.set(item.category, current + item.amount);
|
|
79
|
+
}
|
|
80
|
+
const allCategories = /* @__PURE__ */ new Set([...budgetMap.keys(), ...actualMap.keys()]);
|
|
81
|
+
const variances = [];
|
|
82
|
+
let totalBudget = 0;
|
|
83
|
+
let totalActual = 0;
|
|
84
|
+
let categoriesOverBudget = 0;
|
|
85
|
+
let categoriesUnderBudget = 0;
|
|
86
|
+
let categoriesOnTrack = 0;
|
|
87
|
+
for (const category of allCategories) {
|
|
88
|
+
const budgetAmount = budgetMap.get(category) || 0;
|
|
89
|
+
const actualAmount = actualMap.get(category) || 0;
|
|
90
|
+
const variance = actualAmount - budgetAmount;
|
|
91
|
+
const percentageVariance = budgetAmount !== 0 ? variance / budgetAmount * 100 : actualAmount !== 0 ? 100 : 0;
|
|
92
|
+
let trend;
|
|
93
|
+
if (Math.abs(percentageVariance) < 5) {
|
|
94
|
+
trend = "neutral";
|
|
95
|
+
} else if (variance < 0) {
|
|
96
|
+
trend = "favorable";
|
|
97
|
+
} else {
|
|
98
|
+
trend = "unfavorable";
|
|
99
|
+
}
|
|
100
|
+
let status;
|
|
101
|
+
if (Math.abs(percentageVariance) < 5) {
|
|
102
|
+
status = "on-track";
|
|
103
|
+
categoriesOnTrack++;
|
|
104
|
+
} else if (variance > 0) {
|
|
105
|
+
status = "over";
|
|
106
|
+
categoriesOverBudget++;
|
|
107
|
+
} else {
|
|
108
|
+
status = "under";
|
|
109
|
+
categoriesUnderBudget++;
|
|
110
|
+
}
|
|
111
|
+
variances.push({
|
|
112
|
+
category,
|
|
113
|
+
budget: budgetAmount,
|
|
114
|
+
actual: actualAmount,
|
|
115
|
+
variance,
|
|
116
|
+
percentageVariance: Math.round(percentageVariance * 100) / 100,
|
|
117
|
+
trend,
|
|
118
|
+
status
|
|
119
|
+
});
|
|
120
|
+
totalBudget += budgetAmount;
|
|
121
|
+
totalActual += actualAmount;
|
|
122
|
+
}
|
|
123
|
+
variances.sort((a, b) => Math.abs(b.variance) - Math.abs(a.variance));
|
|
124
|
+
const totalVariance = totalActual - totalBudget;
|
|
125
|
+
const overallPercentageVariance = totalBudget !== 0 ? totalVariance / totalBudget * 100 : totalActual !== 0 ? 100 : 0;
|
|
126
|
+
return {
|
|
127
|
+
variances,
|
|
128
|
+
summary: {
|
|
129
|
+
totalBudget,
|
|
130
|
+
totalActual,
|
|
131
|
+
totalVariance,
|
|
132
|
+
overallPercentageVariance: Math.round(overallPercentageVariance * 100) / 100,
|
|
133
|
+
categoriesOverBudget,
|
|
134
|
+
categoriesUnderBudget,
|
|
135
|
+
categoriesOnTrack
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
var index_default = budgetVarianceTool;
|
|
141
|
+
|
|
142
|
+
export { budgetVarianceTool, index_default as default };
|
package/package.json
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tpmjs/official-budget-variance",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Calculates budget vs actual variance with percentage and trend analysis",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"tpmjs",
|
|
8
|
+
"finance",
|
|
9
|
+
"budget",
|
|
10
|
+
"variance",
|
|
11
|
+
"analysis"
|
|
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/budget-variance"
|
|
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": "budgetVarianceTool",
|
|
45
|
+
"description": "Calculates budget vs actual variance with percentage and trend analysis",
|
|
46
|
+
"parameters": [
|
|
47
|
+
{
|
|
48
|
+
"name": "budget",
|
|
49
|
+
"type": "array",
|
|
50
|
+
"description": "Budget line items with category and amount",
|
|
51
|
+
"required": true
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
"name": "actual",
|
|
55
|
+
"type": "array",
|
|
56
|
+
"description": "Actual spending line items with category and amount",
|
|
57
|
+
"required": true
|
|
58
|
+
}
|
|
59
|
+
],
|
|
60
|
+
"returns": {
|
|
61
|
+
"type": "BudgetVarianceResult",
|
|
62
|
+
"description": "Variance analysis with trends and summary statistics"
|
|
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
|
+
}
|