@tpmjs/official-cash-flow-project 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,89 @@
1
+ import * as ai from 'ai';
2
+
3
+ /**
4
+ * Cash Flow Project Tool for TPMJS
5
+ * Projects cash flow based on receivables, payables, and recurring items
6
+ */
7
+ /**
8
+ * Receivable item with expected payment date
9
+ */
10
+ interface Receivable {
11
+ description: string;
12
+ amount: number;
13
+ dueDate: string;
14
+ probability?: number;
15
+ }
16
+ /**
17
+ * Payable item with expected payment date
18
+ */
19
+ interface Payable {
20
+ description: string;
21
+ amount: number;
22
+ dueDate: string;
23
+ recurring?: boolean;
24
+ frequency?: 'weekly' | 'monthly' | 'quarterly' | 'annually';
25
+ }
26
+ /**
27
+ * Recurring revenue or expense item
28
+ */
29
+ interface RecurringItem {
30
+ description: string;
31
+ amount: number;
32
+ frequency: 'weekly' | 'monthly' | 'quarterly' | 'annually';
33
+ type: 'income' | 'expense';
34
+ startDate?: string;
35
+ endDate?: string;
36
+ }
37
+ /**
38
+ * Cash flow projection for a specific period
39
+ */
40
+ interface CashFlowPeriod {
41
+ period: string;
42
+ startingBalance: number;
43
+ inflows: number;
44
+ outflows: number;
45
+ netChange: number;
46
+ endingBalance: number;
47
+ inflowDetails: Array<{
48
+ description: string;
49
+ amount: number;
50
+ }>;
51
+ outflowDetails: Array<{
52
+ description: string;
53
+ amount: number;
54
+ }>;
55
+ }
56
+ /**
57
+ * Input interface for cash flow projection
58
+ */
59
+ interface CashFlowProjectInput {
60
+ currentCash: number;
61
+ receivables: Receivable[];
62
+ payables: Payable[];
63
+ recurringItems?: RecurringItem[];
64
+ projectionMonths?: number;
65
+ }
66
+ /**
67
+ * Output interface for cash flow projection
68
+ */
69
+ interface CashFlowProjectResult {
70
+ projections: CashFlowPeriod[];
71
+ summary: {
72
+ currentCash: number;
73
+ projectedCashAtEnd: number;
74
+ totalInflows: number;
75
+ totalOutflows: number;
76
+ netChange: number;
77
+ averageMonthlyBurn: number;
78
+ runwayMonths: number | null;
79
+ lowestBalance: number;
80
+ lowestBalancePeriod: string;
81
+ };
82
+ }
83
+ /**
84
+ * Cash Flow Project Tool
85
+ * Projects future cash flow based on receivables, payables, and recurring items
86
+ */
87
+ declare const cashFlowProjectTool: ai.Tool<CashFlowProjectInput, CashFlowProjectResult>;
88
+
89
+ export { type CashFlowProjectResult, cashFlowProjectTool, cashFlowProjectTool as default };
package/dist/index.js ADDED
@@ -0,0 +1,285 @@
1
+ import { tool, jsonSchema } from 'ai';
2
+
3
+ // src/index.ts
4
+ var cashFlowProjectTool = tool({
5
+ description: "Projects cash flow based on receivables, payables, and recurring items. Calculates runway at current burn rate and identifies cash flow risks.",
6
+ inputSchema: jsonSchema({
7
+ type: "object",
8
+ properties: {
9
+ currentCash: {
10
+ type: "number",
11
+ description: "Current cash balance"
12
+ },
13
+ receivables: {
14
+ type: "array",
15
+ description: "Expected receivables with due dates",
16
+ items: {
17
+ type: "object",
18
+ properties: {
19
+ description: {
20
+ type: "string",
21
+ description: "Description of receivable"
22
+ },
23
+ amount: {
24
+ type: "number",
25
+ description: "Amount expected to receive"
26
+ },
27
+ dueDate: {
28
+ type: "string",
29
+ description: "Expected payment date (ISO format)"
30
+ },
31
+ probability: {
32
+ type: "number",
33
+ description: "Probability of payment (0-1)"
34
+ }
35
+ },
36
+ required: ["description", "amount", "dueDate"]
37
+ }
38
+ },
39
+ payables: {
40
+ type: "array",
41
+ description: "Expected payables with due dates",
42
+ items: {
43
+ type: "object",
44
+ properties: {
45
+ description: {
46
+ type: "string",
47
+ description: "Description of payable"
48
+ },
49
+ amount: {
50
+ type: "number",
51
+ description: "Amount to pay"
52
+ },
53
+ dueDate: {
54
+ type: "string",
55
+ description: "Payment due date (ISO format)"
56
+ },
57
+ recurring: {
58
+ type: "boolean",
59
+ description: "Whether this is a recurring payment"
60
+ },
61
+ frequency: {
62
+ type: "string",
63
+ enum: ["weekly", "monthly", "quarterly", "annually"],
64
+ description: "Frequency if recurring"
65
+ }
66
+ },
67
+ required: ["description", "amount", "dueDate"]
68
+ }
69
+ },
70
+ recurringItems: {
71
+ type: "array",
72
+ description: "Recurring revenue or expenses",
73
+ items: {
74
+ type: "object",
75
+ properties: {
76
+ description: {
77
+ type: "string",
78
+ description: "Description of recurring item"
79
+ },
80
+ amount: {
81
+ type: "number",
82
+ description: "Amount per period"
83
+ },
84
+ frequency: {
85
+ type: "string",
86
+ enum: ["weekly", "monthly", "quarterly", "annually"],
87
+ description: "Frequency of recurrence"
88
+ },
89
+ type: {
90
+ type: "string",
91
+ enum: ["income", "expense"],
92
+ description: "Whether this is income or expense"
93
+ },
94
+ startDate: {
95
+ type: "string",
96
+ description: "Start date (ISO format)"
97
+ },
98
+ endDate: {
99
+ type: "string",
100
+ description: "End date (ISO format)"
101
+ }
102
+ },
103
+ required: ["description", "amount", "frequency", "type"]
104
+ }
105
+ },
106
+ projectionMonths: {
107
+ type: "number",
108
+ description: "Number of months to project (default: 12)"
109
+ }
110
+ },
111
+ required: ["currentCash", "receivables", "payables"],
112
+ additionalProperties: false
113
+ }),
114
+ execute: async ({
115
+ currentCash,
116
+ receivables,
117
+ payables,
118
+ recurringItems = [],
119
+ projectionMonths = 12
120
+ }) => {
121
+ if (typeof currentCash !== "number" || currentCash < 0) {
122
+ throw new Error("Current cash must be a non-negative number");
123
+ }
124
+ if (!Array.isArray(receivables)) {
125
+ throw new Error("Receivables must be an array");
126
+ }
127
+ if (!Array.isArray(payables)) {
128
+ throw new Error("Payables must be an array");
129
+ }
130
+ if (projectionMonths < 1 || projectionMonths > 60) {
131
+ throw new Error("Projection months must be between 1 and 60");
132
+ }
133
+ const projections = [];
134
+ const now = /* @__PURE__ */ new Date();
135
+ let runningBalance = currentCash;
136
+ let totalInflows = 0;
137
+ let totalOutflows = 0;
138
+ let lowestBalance = currentCash;
139
+ let lowestBalancePeriod = formatMonth(now);
140
+ for (let i = 0; i < projectionMonths; i++) {
141
+ const periodStart = new Date(now.getFullYear(), now.getMonth() + i, 1);
142
+ const periodEnd = new Date(now.getFullYear(), now.getMonth() + i + 1, 0);
143
+ const periodLabel = formatMonth(periodStart);
144
+ const inflowDetails = [];
145
+ const outflowDetails = [];
146
+ for (const receivable of receivables) {
147
+ const dueDate = new Date(receivable.dueDate);
148
+ if (dueDate >= periodStart && dueDate <= periodEnd) {
149
+ const probability = receivable.probability ?? 1;
150
+ const expectedAmount = receivable.amount * probability;
151
+ inflowDetails.push({
152
+ description: receivable.description,
153
+ amount: expectedAmount
154
+ });
155
+ }
156
+ }
157
+ for (const payable of payables) {
158
+ const dueDate = new Date(payable.dueDate);
159
+ if (dueDate >= periodStart && dueDate <= periodEnd) {
160
+ outflowDetails.push({
161
+ description: payable.description,
162
+ amount: payable.amount
163
+ });
164
+ }
165
+ if (payable.recurring && payable.frequency) {
166
+ const shouldInclude = shouldRecur(
167
+ new Date(payable.dueDate),
168
+ periodStart,
169
+ periodEnd,
170
+ payable.frequency
171
+ );
172
+ if (shouldInclude && dueDate < periodStart) {
173
+ outflowDetails.push({
174
+ description: `${payable.description} (recurring)`,
175
+ amount: payable.amount
176
+ });
177
+ }
178
+ }
179
+ }
180
+ for (const item of recurringItems) {
181
+ const startDate = item.startDate ? new Date(item.startDate) : /* @__PURE__ */ new Date(0);
182
+ const endDate = item.endDate ? new Date(item.endDate) : new Date(9999, 11, 31);
183
+ if (periodStart >= startDate && periodEnd <= endDate) {
184
+ const occurrences = getOccurrencesInPeriod(periodStart, periodEnd, item.frequency);
185
+ for (let j = 0; j < occurrences; j++) {
186
+ if (item.type === "income") {
187
+ inflowDetails.push({
188
+ description: item.description,
189
+ amount: item.amount
190
+ });
191
+ } else {
192
+ outflowDetails.push({
193
+ description: item.description,
194
+ amount: item.amount
195
+ });
196
+ }
197
+ }
198
+ }
199
+ }
200
+ const periodInflows = inflowDetails.reduce((sum, item) => sum + item.amount, 0);
201
+ const periodOutflows = outflowDetails.reduce((sum, item) => sum + item.amount, 0);
202
+ const netChange = periodInflows - periodOutflows;
203
+ const endingBalance = runningBalance + netChange;
204
+ if (endingBalance < lowestBalance) {
205
+ lowestBalance = endingBalance;
206
+ lowestBalancePeriod = periodLabel;
207
+ }
208
+ projections.push({
209
+ period: periodLabel,
210
+ startingBalance: runningBalance,
211
+ inflows: periodInflows,
212
+ outflows: periodOutflows,
213
+ netChange,
214
+ endingBalance,
215
+ inflowDetails,
216
+ outflowDetails
217
+ });
218
+ runningBalance = endingBalance;
219
+ totalInflows += periodInflows;
220
+ totalOutflows += periodOutflows;
221
+ }
222
+ const averageMonthlyBurn = totalOutflows > totalInflows ? (totalOutflows - totalInflows) / projectionMonths : 0;
223
+ let runwayMonths = null;
224
+ if (averageMonthlyBurn > 0) {
225
+ runwayMonths = currentCash / averageMonthlyBurn;
226
+ }
227
+ return {
228
+ projections,
229
+ summary: {
230
+ currentCash,
231
+ projectedCashAtEnd: runningBalance,
232
+ totalInflows,
233
+ totalOutflows,
234
+ netChange: totalInflows - totalOutflows,
235
+ averageMonthlyBurn: Math.round(averageMonthlyBurn * 100) / 100,
236
+ runwayMonths: runwayMonths !== null ? Math.round(runwayMonths * 10) / 10 : null,
237
+ lowestBalance: Math.round(lowestBalance * 100) / 100,
238
+ lowestBalancePeriod
239
+ }
240
+ };
241
+ }
242
+ });
243
+ function formatMonth(date) {
244
+ const year = date.getFullYear();
245
+ const month = String(date.getMonth() + 1).padStart(2, "0");
246
+ return `${year}-${month}`;
247
+ }
248
+ function shouldRecur(originalDate, periodStart, periodEnd, frequency) {
249
+ if (originalDate >= periodStart && originalDate <= periodEnd) {
250
+ return false;
251
+ }
252
+ const monthsDiff = (periodStart.getFullYear() - originalDate.getFullYear()) * 12 + (periodStart.getMonth() - originalDate.getMonth());
253
+ switch (frequency) {
254
+ case "monthly":
255
+ return monthsDiff > 0 && monthsDiff % 1 === 0;
256
+ case "quarterly":
257
+ return monthsDiff > 0 && monthsDiff % 3 === 0;
258
+ case "annually":
259
+ return monthsDiff > 0 && monthsDiff % 12 === 0;
260
+ case "weekly":
261
+ return monthsDiff > 0;
262
+ default:
263
+ return false;
264
+ }
265
+ }
266
+ function getOccurrencesInPeriod(_periodStart, _periodEnd, frequency) {
267
+ switch (frequency) {
268
+ case "weekly":
269
+ return 4;
270
+ // Approximate 4 weeks per month
271
+ case "monthly":
272
+ return 1;
273
+ case "quarterly":
274
+ return 0.33;
275
+ // Approximately 1/3 per month
276
+ case "annually":
277
+ return 0.08;
278
+ // Approximately 1/12 per month
279
+ default:
280
+ return 1;
281
+ }
282
+ }
283
+ var index_default = cashFlowProjectTool;
284
+
285
+ export { cashFlowProjectTool, index_default as default };
package/package.json ADDED
@@ -0,0 +1,94 @@
1
+ {
2
+ "name": "@tpmjs/official-cash-flow-project",
3
+ "version": "0.1.0",
4
+ "description": "Projects cash flow based on receivables, payables, and recurring items",
5
+ "type": "module",
6
+ "keywords": [
7
+ "tpmjs",
8
+ "finance",
9
+ "cash-flow",
10
+ "projection",
11
+ "runway"
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/cash-flow-project"
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": "cashFlowProjectTool",
45
+ "description": "Projects cash flow based on receivables, payables, and recurring items",
46
+ "parameters": [
47
+ {
48
+ "name": "currentCash",
49
+ "type": "number",
50
+ "description": "Current cash balance",
51
+ "required": true
52
+ },
53
+ {
54
+ "name": "receivables",
55
+ "type": "array",
56
+ "description": "Expected receivables with due dates",
57
+ "required": true
58
+ },
59
+ {
60
+ "name": "payables",
61
+ "type": "array",
62
+ "description": "Expected payables with due dates",
63
+ "required": true
64
+ },
65
+ {
66
+ "name": "recurringItems",
67
+ "type": "array",
68
+ "description": "Recurring revenue or expenses",
69
+ "required": false
70
+ },
71
+ {
72
+ "name": "projectionMonths",
73
+ "type": "number",
74
+ "description": "Number of months to project",
75
+ "required": false
76
+ }
77
+ ],
78
+ "returns": {
79
+ "type": "CashFlowProjectResult",
80
+ "description": "Cash flow projections with runway calculation"
81
+ }
82
+ }
83
+ ]
84
+ },
85
+ "dependencies": {
86
+ "ai": "6.0.0-beta.124"
87
+ },
88
+ "scripts": {
89
+ "build": "tsup",
90
+ "dev": "tsup --watch",
91
+ "type-check": "tsc --noEmit",
92
+ "clean": "rm -rf dist .turbo"
93
+ }
94
+ }