codebuff 1.0.136 → 1.0.138
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/__tests__/tool-handlers.test.js +3 -2
- package/dist/__tests__/tool-handlers.test.js.map +1 -1
- package/dist/chat-storage.js +17 -7
- package/dist/chat-storage.js.map +1 -1
- package/dist/cli.js +17 -7
- package/dist/cli.js.map +1 -1
- package/dist/client.js +18 -8
- package/dist/client.js.map +1 -1
- package/dist/code-map/languages.js +17 -7
- package/dist/code-map/languages.js.map +1 -1
- package/dist/code-map/parse.js +17 -7
- package/dist/code-map/parse.js.map +1 -1
- package/dist/code-map/tsconfig.tsbuildinfo +1 -1
- package/dist/common/constants.js +2 -0
- package/dist/common/constants.js.map +1 -1
- package/dist/common/types/usage.d.ts +2 -2
- package/dist/common/util/credentials.d.ts +2 -2
- package/dist/common/util/string.d.ts +1 -0
- package/dist/common/util/string.js +7 -1
- package/dist/common/util/string.js.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/menu.js +17 -7
- package/dist/menu.js.map +1 -1
- package/dist/project-files.js +17 -7
- package/dist/project-files.js.map +1 -1
- package/dist/tool-handlers.d.ts +0 -1
- package/dist/tool-handlers.js +35 -282
- package/dist/tool-handlers.js.map +1 -1
- package/dist/utils/terminal.d.ts +5 -0
- package/dist/utils/terminal.js +322 -0
- package/dist/utils/terminal.js.map +1 -0
- package/package.json +2 -2
- package/dist/common/billing/payment-guards.d.ts +0 -11
- package/dist/common/billing/payment-guards.js +0 -136
- package/dist/common/billing/payment-guards.js.map +0 -1
- package/dist/common/billing/subscription-state.d.ts +0 -13
- package/dist/common/billing/subscription-state.js +0 -117
- package/dist/common/billing/subscription-state.js.map +0 -1
- package/dist/common/billing/webhook-processor.d.ts +0 -21
- package/dist/common/billing/webhook-processor.js +0 -315
- package/dist/common/billing/webhook-processor.js.map +0 -1
- package/dist/common/scripts/update-subscriptions.d.ts +0 -1
- package/dist/common/scripts/update-subscriptions.js +0 -92
- package/dist/common/scripts/update-subscriptions.js.map +0 -1
- package/dist/common/websockets/logger-interface.d.ts +0 -6
- package/dist/common/websockets/logger-interface.js +0 -9
- package/dist/common/websockets/logger-interface.js.map +0 -1
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.runTerminalCommand = exports.resetPtyShell = void 0;
|
|
40
|
+
const child_process_1 = require("child_process");
|
|
41
|
+
const path_1 = __importDefault(require("path"));
|
|
42
|
+
const picocolors_1 = require("picocolors");
|
|
43
|
+
const os = __importStar(require("os"));
|
|
44
|
+
const detect_shell_1 = require("./detect-shell");
|
|
45
|
+
const project_files_1 = require("src/project-files");
|
|
46
|
+
const string_1 = require("../common/util/string");
|
|
47
|
+
let pty;
|
|
48
|
+
const tempConsoleError = console.error;
|
|
49
|
+
console.error = () => { };
|
|
50
|
+
try {
|
|
51
|
+
pty = require('@homebridge/node-pty-prebuilt-multiarch');
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
}
|
|
55
|
+
finally {
|
|
56
|
+
console.error = tempConsoleError;
|
|
57
|
+
}
|
|
58
|
+
const createPty = (dir) => {
|
|
59
|
+
if (pty) {
|
|
60
|
+
const isWindows = os.platform() === 'win32';
|
|
61
|
+
const currShell = (0, detect_shell_1.detectShell)();
|
|
62
|
+
const shell = isWindows
|
|
63
|
+
? currShell === 'powershell'
|
|
64
|
+
? 'powershell.exe'
|
|
65
|
+
: 'cmd.exe'
|
|
66
|
+
: 'bash';
|
|
67
|
+
const shellWithoutExe = shell.split('.')[0];
|
|
68
|
+
// Prepare shell init commands
|
|
69
|
+
let shellInitCommands = '';
|
|
70
|
+
if (!isWindows) {
|
|
71
|
+
const rcFile = currShell === 'zsh'
|
|
72
|
+
? '~/.zshrc'
|
|
73
|
+
: currShell === 'fish'
|
|
74
|
+
? '~/.config/fish/config.fish'
|
|
75
|
+
: '~/.bashrc';
|
|
76
|
+
shellInitCommands = `source ${rcFile} 2>/dev/null || true\n`;
|
|
77
|
+
}
|
|
78
|
+
else if (currShell === 'powershell') {
|
|
79
|
+
// Try to source PowerShell profile if it exists
|
|
80
|
+
shellInitCommands =
|
|
81
|
+
'$PSProfile = $PROFILE.CurrentUserAllHosts; if (Test-Path $PSProfile) { . $PSProfile }\n';
|
|
82
|
+
}
|
|
83
|
+
const persistentPty = pty.spawn(shell, [], {
|
|
84
|
+
name: 'xterm-256color',
|
|
85
|
+
cols: process.stdout.columns || 80,
|
|
86
|
+
rows: process.stdout.rows || 24,
|
|
87
|
+
cwd: dir,
|
|
88
|
+
env: {
|
|
89
|
+
...process.env,
|
|
90
|
+
TERM: 'xterm-256color',
|
|
91
|
+
PAGER: 'cat',
|
|
92
|
+
GIT_PAGER: 'cat',
|
|
93
|
+
GIT_TERMINAL_PROMPT: '0',
|
|
94
|
+
...(isWindows
|
|
95
|
+
? {
|
|
96
|
+
TERM: 'cygwin',
|
|
97
|
+
ANSICON: '1', // Better ANSI support in cmd.exe
|
|
98
|
+
PROMPT: '$P$G', // Simple prompt to avoid parsing issues
|
|
99
|
+
PSModulePath: process.env.PSModulePath || '', // Preserve PowerShell modules
|
|
100
|
+
}
|
|
101
|
+
: {}),
|
|
102
|
+
LESS: '-FRX',
|
|
103
|
+
TERM_PROGRAM: 'mintty',
|
|
104
|
+
NO_COLOR: process.env.NO_COLOR, // Respect NO_COLOR if set
|
|
105
|
+
FORCE_COLOR: '1', // Enable colors in CI/CD
|
|
106
|
+
CI: process.env.CI, // Preserve CI environment
|
|
107
|
+
// Locale settings for consistent output
|
|
108
|
+
LANG: 'en_US.UTF-8',
|
|
109
|
+
LC_ALL: 'en_US.UTF-8',
|
|
110
|
+
// Shell-specific settings
|
|
111
|
+
SHELL: shellWithoutExe,
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
// Source the shell config file if available
|
|
115
|
+
if (shellInitCommands) {
|
|
116
|
+
persistentPty.write(shellInitCommands);
|
|
117
|
+
}
|
|
118
|
+
return { type: 'pty', pty: persistentPty };
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
// Fallback to child_process
|
|
122
|
+
const isWindows = os.platform() === 'win32';
|
|
123
|
+
const currShell = (0, detect_shell_1.detectShell)();
|
|
124
|
+
const shell = isWindows
|
|
125
|
+
? currShell === 'powershell'
|
|
126
|
+
? 'powershell.exe'
|
|
127
|
+
: 'cmd.exe'
|
|
128
|
+
: 'bash';
|
|
129
|
+
return { type: 'process', shell };
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
let persistentProcess = null;
|
|
133
|
+
process.stdout.on('resize', () => {
|
|
134
|
+
if (!persistentProcess)
|
|
135
|
+
return;
|
|
136
|
+
const { pty } = persistentProcess;
|
|
137
|
+
if (pty) {
|
|
138
|
+
pty.resize(process.stdout.columns, process.stdout.rows);
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
const resetPtyShell = (dir) => {
|
|
142
|
+
if (persistentProcess) {
|
|
143
|
+
if (persistentProcess.type === 'pty') {
|
|
144
|
+
persistentProcess.pty.kill();
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
persistentProcess = createPty(dir);
|
|
148
|
+
};
|
|
149
|
+
exports.resetPtyShell = resetPtyShell;
|
|
150
|
+
function formatResult(stdout, status) {
|
|
151
|
+
let result = '<terminal_command_result>\n';
|
|
152
|
+
result += `<output>${(0, string_1.truncateStringWithMessage)(stdout, 10000)}</output>\n`;
|
|
153
|
+
result += `<status>${status}</status>\n`;
|
|
154
|
+
result += '</terminal_command_result>';
|
|
155
|
+
return result;
|
|
156
|
+
}
|
|
157
|
+
const isNotACommand = (output) => {
|
|
158
|
+
return (output.includes('command not found') ||
|
|
159
|
+
// Linux
|
|
160
|
+
output.includes(': not found') ||
|
|
161
|
+
// Common
|
|
162
|
+
output.includes('syntax error:') ||
|
|
163
|
+
output.includes('syntax error near unexpected token') ||
|
|
164
|
+
// Linux
|
|
165
|
+
output.includes('Syntax error:') ||
|
|
166
|
+
// Windows
|
|
167
|
+
output.includes('is not recognized as an internal or external command') ||
|
|
168
|
+
output.includes('/bin/sh: -c: line') ||
|
|
169
|
+
output.includes('/bin/sh: line') ||
|
|
170
|
+
output.startsWith('fatal:') ||
|
|
171
|
+
output.startsWith('error:') ||
|
|
172
|
+
output.startsWith('Der Befehl') ||
|
|
173
|
+
output.includes('konnte nicht gefunden werden') ||
|
|
174
|
+
output.includes('wurde nicht als Name eines Cmdlet, einer Funktion, einer Skriptdatei oder eines ausführbaren'));
|
|
175
|
+
};
|
|
176
|
+
const runTerminalCommand = async (command, mode) => {
|
|
177
|
+
const MAX_EXECUTION_TIME = 30_000;
|
|
178
|
+
const projectRoot = (0, project_files_1.getProjectRoot)();
|
|
179
|
+
return new Promise((resolve) => {
|
|
180
|
+
if (!persistentProcess) {
|
|
181
|
+
throw new Error('Shell not initialized');
|
|
182
|
+
}
|
|
183
|
+
if (persistentProcess.type === 'pty') {
|
|
184
|
+
// Use PTY implementation
|
|
185
|
+
const ptyProcess = persistentProcess.pty;
|
|
186
|
+
let commandOutput = '';
|
|
187
|
+
let foundFirstNewLine = false;
|
|
188
|
+
if (mode === 'assistant') {
|
|
189
|
+
console.log();
|
|
190
|
+
console.log((0, picocolors_1.green)(`> ${command}`));
|
|
191
|
+
}
|
|
192
|
+
const timer = setTimeout(() => {
|
|
193
|
+
if (mode === 'assistant') {
|
|
194
|
+
// Kill and recreate PTY
|
|
195
|
+
(0, exports.resetPtyShell)(projectRoot);
|
|
196
|
+
resolve({
|
|
197
|
+
result: formatResult(commandOutput, `Command timed out after ${MAX_EXECUTION_TIME / 1000} seconds and was terminated. Shell has been restarted.`),
|
|
198
|
+
stdout: commandOutput,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
}, MAX_EXECUTION_TIME);
|
|
202
|
+
const dataDisposable = ptyProcess.onData((data) => {
|
|
203
|
+
const prefix = commandOutput + data;
|
|
204
|
+
// Skip the first line of the output, because it's the command being printed.
|
|
205
|
+
if (!foundFirstNewLine) {
|
|
206
|
+
if (!prefix.includes('\n')) {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
foundFirstNewLine = true;
|
|
210
|
+
const newLineIndex = prefix.indexOf('\n');
|
|
211
|
+
data = prefix.slice(newLineIndex + 1);
|
|
212
|
+
}
|
|
213
|
+
// Try to detect error messages in the output
|
|
214
|
+
if (mode === 'user' && isNotACommand(data)) {
|
|
215
|
+
clearTimeout(timer);
|
|
216
|
+
dataDisposable.dispose();
|
|
217
|
+
resolve({
|
|
218
|
+
result: 'command not found',
|
|
219
|
+
stdout: commandOutput,
|
|
220
|
+
});
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
// Detect the end of the command output if the prompt is printed.
|
|
224
|
+
// Windows PowerShell prompt pattern: "MM/DD HH:mm Path ►"
|
|
225
|
+
const simpleWindowsPromptRegex = /\d{2}:\d{2}.*►/;
|
|
226
|
+
// Another cmd prompt: "C:\Users\Name\My Projects>"
|
|
227
|
+
const simpleWindowsPromptRegex2 = /[A-Z]:\\.*\\.*>/;
|
|
228
|
+
const hasSimplePromptOnWindows = simpleWindowsPromptRegex.test(prefix);
|
|
229
|
+
const hasSimplePromptOnWindows2 = simpleWindowsPromptRegex2.test(prefix);
|
|
230
|
+
const promptDetected = prefix.includes('bash-3.2$ ') ||
|
|
231
|
+
hasSimplePromptOnWindows ||
|
|
232
|
+
hasSimplePromptOnWindows2;
|
|
233
|
+
if (promptDetected) {
|
|
234
|
+
clearTimeout(timer);
|
|
235
|
+
dataDisposable.dispose();
|
|
236
|
+
if (command.startsWith('cd ') && mode === 'user') {
|
|
237
|
+
const newWorkingDirectory = command.split(' ')[1];
|
|
238
|
+
(0, project_files_1.setProjectRoot)(path_1.default.join(projectRoot, newWorkingDirectory));
|
|
239
|
+
}
|
|
240
|
+
if (mode === 'assistant') {
|
|
241
|
+
console.log((0, picocolors_1.green)(`Command completed`));
|
|
242
|
+
}
|
|
243
|
+
// Reset the PTY to the project root
|
|
244
|
+
ptyProcess.write(`cd ${projectRoot}\r`);
|
|
245
|
+
resolve({
|
|
246
|
+
result: formatResult(commandOutput, 'Command completed'),
|
|
247
|
+
stdout: commandOutput,
|
|
248
|
+
});
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
process.stdout.write(data);
|
|
252
|
+
commandOutput += data;
|
|
253
|
+
});
|
|
254
|
+
// Write the command
|
|
255
|
+
ptyProcess.write(command + '\r');
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
// Fallback to child_process implementation
|
|
259
|
+
const isWindows = os.platform() === 'win32';
|
|
260
|
+
let commandOutput = '';
|
|
261
|
+
if (mode === 'assistant') {
|
|
262
|
+
console.log();
|
|
263
|
+
console.log((0, picocolors_1.green)(`> ${command}`));
|
|
264
|
+
}
|
|
265
|
+
const childProcess = (0, child_process_1.spawn)(persistentProcess.shell, [isWindows ? '/c' : '-c', command], {
|
|
266
|
+
cwd: projectRoot,
|
|
267
|
+
env: {
|
|
268
|
+
...process.env,
|
|
269
|
+
PAGER: 'cat',
|
|
270
|
+
GIT_PAGER: 'cat',
|
|
271
|
+
GIT_TERMINAL_PROMPT: '0',
|
|
272
|
+
LESS: '-FRX',
|
|
273
|
+
},
|
|
274
|
+
});
|
|
275
|
+
const timer = setTimeout(() => {
|
|
276
|
+
childProcess.kill();
|
|
277
|
+
if (mode === 'assistant') {
|
|
278
|
+
resolve({
|
|
279
|
+
result: formatResult(commandOutput, `Command timed out after ${MAX_EXECUTION_TIME / 1000} seconds and was terminated.`),
|
|
280
|
+
stdout: commandOutput,
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
}, MAX_EXECUTION_TIME);
|
|
284
|
+
childProcess.stdout.on('data', (data) => {
|
|
285
|
+
const output = data.toString();
|
|
286
|
+
process.stdout.write(output);
|
|
287
|
+
commandOutput += output;
|
|
288
|
+
});
|
|
289
|
+
childProcess.stderr.on('data', (data) => {
|
|
290
|
+
const output = data.toString();
|
|
291
|
+
// Try to detect error messages in the output
|
|
292
|
+
if (mode === 'user' && isNotACommand(output)) {
|
|
293
|
+
clearTimeout(timer);
|
|
294
|
+
childProcess.kill();
|
|
295
|
+
resolve({
|
|
296
|
+
result: 'command not found',
|
|
297
|
+
stdout: commandOutput,
|
|
298
|
+
});
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
process.stdout.write(output);
|
|
302
|
+
commandOutput += output;
|
|
303
|
+
});
|
|
304
|
+
childProcess.on('close', (code) => {
|
|
305
|
+
clearTimeout(timer);
|
|
306
|
+
if (command.startsWith('cd ') && mode === 'user') {
|
|
307
|
+
const newWorkingDirectory = command.split(' ')[1];
|
|
308
|
+
(0, project_files_1.setProjectRoot)(path_1.default.join(projectRoot, newWorkingDirectory));
|
|
309
|
+
}
|
|
310
|
+
if (mode === 'assistant') {
|
|
311
|
+
console.log((0, picocolors_1.green)(`Command completed`));
|
|
312
|
+
}
|
|
313
|
+
resolve({
|
|
314
|
+
result: formatResult(commandOutput, `Command completed`),
|
|
315
|
+
stdout: commandOutput,
|
|
316
|
+
});
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
};
|
|
321
|
+
exports.runTerminalCommand = runTerminalCommand;
|
|
322
|
+
//# sourceMappingURL=terminal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"terminal.js","sourceRoot":"","sources":["../../src/utils/terminal.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAAqC;AACrC,gDAAuB;AACvB,2CAAkC;AAClC,uCAAwB;AACxB,iDAA4C;AAC5C,qDAAkE;AAClE,+CAA8D;AAE9D,IAAI,GAAyE,CAAA;AAC7E,MAAM,gBAAgB,GAAG,OAAO,CAAC,KAAK,CAAA;AACtC,OAAO,CAAC,KAAK,GAAG,GAAG,EAAE,GAAE,CAAC,CAAA;AACxB,IAAI,CAAC;IACH,GAAG,GAAG,OAAO,CAAC,yCAAyC,CAAC,CAAA;AAC1D,CAAC;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-1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "codebuff",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.138",
|
|
4
4
|
"description": "AI dev assistant",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
"zod": "3.23.8"
|
|
66
66
|
},
|
|
67
67
|
"optionalDependencies": {
|
|
68
|
-
"@homebridge/node-pty-prebuilt-multiarch": "0.
|
|
68
|
+
"@homebridge/node-pty-prebuilt-multiarch": "0.12.0-beta.5"
|
|
69
69
|
},
|
|
70
70
|
"trustedDependencies": [
|
|
71
71
|
"@homebridge/node-pty-prebuilt-multiarch",
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export declare class PaymentGuards {
|
|
2
|
-
private subscriptionId;
|
|
3
|
-
constructor(subscriptionId: string);
|
|
4
|
-
validatePaymentMethod(): Promise<{
|
|
5
|
-
valid: boolean;
|
|
6
|
-
error?: string;
|
|
7
|
-
}>;
|
|
8
|
-
handleFailedPayment(): Promise<void>;
|
|
9
|
-
processPartialPayment(amountPaid: number): Promise<void>;
|
|
10
|
-
handleRefund(amount: number): Promise<void>;
|
|
11
|
-
}
|
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.PaymentGuards = void 0;
|
|
30
|
-
const stripe_1 = require("../util/stripe");
|
|
31
|
-
const db_1 = __importDefault(require("../db"));
|
|
32
|
-
const schema = __importStar(require("../db/schema"));
|
|
33
|
-
const drizzle_orm_1 = require("drizzle-orm");
|
|
34
|
-
class PaymentGuards {
|
|
35
|
-
subscriptionId;
|
|
36
|
-
constructor(subscriptionId) {
|
|
37
|
-
this.subscriptionId = subscriptionId;
|
|
38
|
-
}
|
|
39
|
-
async validatePaymentMethod() {
|
|
40
|
-
try {
|
|
41
|
-
const subscription = await stripe_1.stripeServer.subscriptions.retrieve(this.subscriptionId, {
|
|
42
|
-
expand: ['latest_invoice.payment_intent'],
|
|
43
|
-
});
|
|
44
|
-
const invoice = subscription.latest_invoice;
|
|
45
|
-
if (!invoice?.payment_intent) {
|
|
46
|
-
return { valid: true }; // No payment needed
|
|
47
|
-
}
|
|
48
|
-
const paymentIntent = invoice.payment_intent;
|
|
49
|
-
if (paymentIntent.status === 'succeeded') {
|
|
50
|
-
return { valid: true };
|
|
51
|
-
}
|
|
52
|
-
return {
|
|
53
|
-
valid: false,
|
|
54
|
-
error: `Payment failed: ${paymentIntent.last_payment_error?.message || 'Unknown error'}`,
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
catch (error) {
|
|
58
|
-
console.error('Error validating payment method:', error);
|
|
59
|
-
return {
|
|
60
|
-
valid: false,
|
|
61
|
-
error: 'Failed to validate payment method',
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
async handleFailedPayment() {
|
|
66
|
-
// Record the failed attempt
|
|
67
|
-
await db_1.default.insert(schema.payment_attempts).values({
|
|
68
|
-
id: crypto.randomUUID(),
|
|
69
|
-
subscription_id: this.subscriptionId,
|
|
70
|
-
status: 'failed',
|
|
71
|
-
created_at: new Date(),
|
|
72
|
-
});
|
|
73
|
-
// Get the subscription to check retry count
|
|
74
|
-
const attempts = await db_1.default
|
|
75
|
-
.select({
|
|
76
|
-
count: (0, drizzle_orm_1.sql) `count(*)::int`,
|
|
77
|
-
})
|
|
78
|
-
.from(schema.payment_attempts)
|
|
79
|
-
.where((0, drizzle_orm_1.and)((0, drizzle_orm_1.eq)(schema.payment_attempts.subscription_id, this.subscriptionId), (0, drizzle_orm_1.eq)(schema.payment_attempts.status, 'failed'), (0, drizzle_orm_1.sql) `created_at > now() - interval '24 hours'`));
|
|
80
|
-
const retryCount = attempts[0].count;
|
|
81
|
-
if (retryCount >= 3) {
|
|
82
|
-
// Too many failures, pause the subscription
|
|
83
|
-
await stripe_1.stripeServer.subscriptions.update(this.subscriptionId, {
|
|
84
|
-
pause_collection: {
|
|
85
|
-
behavior: 'void',
|
|
86
|
-
},
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
async processPartialPayment(amountPaid) {
|
|
91
|
-
// Record the partial payment
|
|
92
|
-
await db_1.default.insert(schema.payment_attempts).values({
|
|
93
|
-
id: crypto.randomUUID(),
|
|
94
|
-
subscription_id: this.subscriptionId,
|
|
95
|
-
amount: amountPaid.toString(), // Convert to string for decimal type
|
|
96
|
-
status: 'partial',
|
|
97
|
-
created_at: new Date(),
|
|
98
|
-
});
|
|
99
|
-
// Update the subscription with remaining balance
|
|
100
|
-
const subscription = await stripe_1.stripeServer.subscriptions.retrieve(this.subscriptionId, {
|
|
101
|
-
expand: ['latest_invoice'],
|
|
102
|
-
});
|
|
103
|
-
const invoice = subscription.latest_invoice;
|
|
104
|
-
if (invoice) {
|
|
105
|
-
const remainingAmount = invoice.amount_due - amountPaid;
|
|
106
|
-
if (remainingAmount > 0) {
|
|
107
|
-
await stripe_1.stripeServer.invoices.update(invoice.id, {
|
|
108
|
-
description: `Partial payment received: ${amountPaid}. Remaining: ${remainingAmount}`,
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
async handleRefund(amount) {
|
|
114
|
-
const subscription = await stripe_1.stripeServer.subscriptions.retrieve(this.subscriptionId, {
|
|
115
|
-
expand: ['latest_invoice.payment_intent'],
|
|
116
|
-
});
|
|
117
|
-
const invoice = subscription.latest_invoice;
|
|
118
|
-
if (!invoice?.payment_intent) {
|
|
119
|
-
throw new Error('No payment found to refund');
|
|
120
|
-
}
|
|
121
|
-
await stripe_1.stripeServer.refunds.create({
|
|
122
|
-
payment_intent: invoice.payment_intent.id,
|
|
123
|
-
amount,
|
|
124
|
-
});
|
|
125
|
-
// Record the refund
|
|
126
|
-
await db_1.default.insert(schema.payment_attempts).values({
|
|
127
|
-
id: crypto.randomUUID(),
|
|
128
|
-
subscription_id: this.subscriptionId,
|
|
129
|
-
amount: (-amount).toString(), // Convert to string for decimal type
|
|
130
|
-
status: 'refunded',
|
|
131
|
-
created_at: new Date(),
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
exports.PaymentGuards = PaymentGuards;
|
|
136
|
-
//# sourceMappingURL=payment-guards.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"payment-guards.js","sourceRoot":"","sources":["../../src/billing/payment-guards.ts"],"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AI,IAAI,EAAE;SACvB,CAAC,CAAA;IACJ,CAAC;CACF;AAhID,sCAgIC"}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
export type SubscriptionState = 'active' | 'past_due' | 'incomplete' | 'canceled' | 'trialing' | 'paused' | 'transitioning';
|
|
3
|
-
export declare const subscriptionStateSchema: z.ZodEnum<["active", "past_due", "incomplete", "canceled", "trialing", "paused", "transitioning"]>;
|
|
4
|
-
export declare class SubscriptionStateManager {
|
|
5
|
-
private subscriptionId;
|
|
6
|
-
private transitionLock;
|
|
7
|
-
constructor(subscriptionId: string);
|
|
8
|
-
getCurrentState(): Promise<SubscriptionState>;
|
|
9
|
-
beginStateTransition(): Promise<boolean>;
|
|
10
|
-
endStateTransition(): Promise<void>;
|
|
11
|
-
addStateHistoryEntry(fromState: SubscriptionState, toState: SubscriptionState, reason?: string): Promise<void>;
|
|
12
|
-
private mapStripeState;
|
|
13
|
-
}
|
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.SubscriptionStateManager = exports.subscriptionStateSchema = void 0;
|
|
30
|
-
const zod_1 = require("zod");
|
|
31
|
-
const drizzle_orm_1 = require("drizzle-orm");
|
|
32
|
-
const db_1 = __importDefault(require("../db"));
|
|
33
|
-
const schema = __importStar(require("../db/schema"));
|
|
34
|
-
const stripe_1 = require("../util/stripe");
|
|
35
|
-
exports.subscriptionStateSchema = zod_1.z.enum([
|
|
36
|
-
'active',
|
|
37
|
-
'past_due',
|
|
38
|
-
'incomplete',
|
|
39
|
-
'canceled',
|
|
40
|
-
'trialing',
|
|
41
|
-
'paused',
|
|
42
|
-
'transitioning',
|
|
43
|
-
]);
|
|
44
|
-
class SubscriptionStateManager {
|
|
45
|
-
subscriptionId;
|
|
46
|
-
transitionLock = null;
|
|
47
|
-
constructor(subscriptionId) {
|
|
48
|
-
this.subscriptionId = subscriptionId;
|
|
49
|
-
}
|
|
50
|
-
async getCurrentState() {
|
|
51
|
-
const subscription = await stripe_1.stripeServer.subscriptions.retrieve(this.subscriptionId);
|
|
52
|
-
return this.mapStripeState(subscription.status);
|
|
53
|
-
}
|
|
54
|
-
async beginStateTransition() {
|
|
55
|
-
// Generate a new lock UUID
|
|
56
|
-
const newLock = crypto.randomUUID();
|
|
57
|
-
// Try to acquire lock
|
|
58
|
-
const result = await db_1.default
|
|
59
|
-
.update(schema.user)
|
|
60
|
-
.set({
|
|
61
|
-
transition_lock: newLock,
|
|
62
|
-
})
|
|
63
|
-
.where((0, drizzle_orm_1.and)((0, drizzle_orm_1.eq)(schema.user.stripe_price_id, this.subscriptionId), (0, drizzle_orm_1.sql) `transition_lock IS NULL`))
|
|
64
|
-
.returning({ transitionLock: schema.user.transition_lock });
|
|
65
|
-
if (result.length === 0) {
|
|
66
|
-
return false; // Lock acquisition failed
|
|
67
|
-
}
|
|
68
|
-
this.transitionLock = newLock;
|
|
69
|
-
return true;
|
|
70
|
-
}
|
|
71
|
-
async endStateTransition() {
|
|
72
|
-
if (!this.transitionLock) {
|
|
73
|
-
throw new Error('No active transition');
|
|
74
|
-
}
|
|
75
|
-
await db_1.default
|
|
76
|
-
.update(schema.user)
|
|
77
|
-
.set({
|
|
78
|
-
transition_lock: null,
|
|
79
|
-
})
|
|
80
|
-
.where((0, drizzle_orm_1.and)((0, drizzle_orm_1.eq)(schema.user.stripe_price_id, this.subscriptionId), (0, drizzle_orm_1.eq)(schema.user.transition_lock, this.transitionLock)));
|
|
81
|
-
this.transitionLock = null;
|
|
82
|
-
}
|
|
83
|
-
async addStateHistoryEntry(fromState, toState, reason) {
|
|
84
|
-
const entry = {
|
|
85
|
-
fromState,
|
|
86
|
-
toState,
|
|
87
|
-
reason,
|
|
88
|
-
timestamp: new Date().toISOString(),
|
|
89
|
-
};
|
|
90
|
-
await db_1.default
|
|
91
|
-
.update(schema.user)
|
|
92
|
-
.set({
|
|
93
|
-
state_history: (0, drizzle_orm_1.sql) `COALESCE(state_history, '[]'::jsonb) || ${JSON.stringify(entry)}::jsonb`,
|
|
94
|
-
})
|
|
95
|
-
.where((0, drizzle_orm_1.eq)(schema.user.stripe_price_id, this.subscriptionId));
|
|
96
|
-
}
|
|
97
|
-
mapStripeState(stripeStatus) {
|
|
98
|
-
switch (stripeStatus) {
|
|
99
|
-
case 'active':
|
|
100
|
-
return 'active';
|
|
101
|
-
case 'past_due':
|
|
102
|
-
return 'past_due';
|
|
103
|
-
case 'incomplete':
|
|
104
|
-
return 'incomplete';
|
|
105
|
-
case 'canceled':
|
|
106
|
-
return 'canceled';
|
|
107
|
-
case 'trialing':
|
|
108
|
-
return 'trialing';
|
|
109
|
-
case 'paused':
|
|
110
|
-
return 'paused';
|
|
111
|
-
default:
|
|
112
|
-
throw new Error(`Unknown Stripe status: ${stripeStatus}`);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
exports.SubscriptionStateManager = SubscriptionStateManager;
|
|
117
|
-
//# sourceMappingURL=subscription-state.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"subscription-state.js","sourceRoot":"","sources":["../../src/billing/subscription-state.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6BAAuB;AACvB,6CAA0C;AAC1C,+CAAsB;AACtB,qDAAsC;AACtC,2CAA6C;AAWhC,QAAA,uBAAuB,GAAG,OAAC,CAAC,IAAI,CAAC;IAC5C,QAAQ;IACR,UAAU;IACV,YAAY;IACZ,UAAU;IACV,UAAU;IACV,QAAQ;IACR,eAAe;CAChB,CAAC,CAAA;AAEF,MAAa,wBAAwB;IAGf;IAFZ,cAAc,GAAkB,IAAI,CAAA;IAE5C,YAAoB,cAAsB;QAAtB,mBAAc,GAAd,cAAc,CAAQ;IAAG,CAAC;IAE9C,KAAK,CAAC,eAAe;QACnB,MAAM,YAAY,GAAG,MAAM,qBAAY,CAAC,aAAa,CAAC,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,CAAA;QACnF,OAAO,IAAI,CAAC,cAAc,CAAC,YAAY,CAAC,MAAM,CAAC,CAAA;IACjD,CAAC;IAED,KAAK,CAAC,oBAAoB;QACxB,2BAA2B;QAC3B,MAAM,OAAO,GAAG,MAAM,CAAC,UAAU,EAAE,CAAA;QAEnC,sBAAsB;QACtB,MAAM,MAAM,GAAG,MAAM,YAAE;aACpB,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC;aACnB,GAAG,CAA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