@jack-kernel/sdk 1.0.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.
Files changed (68) hide show
  1. package/README.md +757 -0
  2. package/dist/cjs/agent.js +321 -0
  3. package/dist/cjs/agent.js.map +1 -0
  4. package/dist/cjs/cache.js +58 -0
  5. package/dist/cjs/cache.js.map +1 -0
  6. package/dist/cjs/client.js +196 -0
  7. package/dist/cjs/client.js.map +1 -0
  8. package/dist/cjs/costs.js +104 -0
  9. package/dist/cjs/costs.js.map +1 -0
  10. package/dist/cjs/errors.js +287 -0
  11. package/dist/cjs/errors.js.map +1 -0
  12. package/dist/cjs/execution.js +385 -0
  13. package/dist/cjs/execution.js.map +1 -0
  14. package/dist/cjs/index.js +256 -0
  15. package/dist/cjs/index.js.map +1 -0
  16. package/dist/cjs/intents.js +185 -0
  17. package/dist/cjs/intents.js.map +1 -0
  18. package/dist/cjs/serialization.js +164 -0
  19. package/dist/cjs/serialization.js.map +1 -0
  20. package/dist/cjs/types.js +31 -0
  21. package/dist/cjs/types.js.map +1 -0
  22. package/dist/cjs/validation.js +114 -0
  23. package/dist/cjs/validation.js.map +1 -0
  24. package/dist/esm/agent.js +317 -0
  25. package/dist/esm/agent.js.map +1 -0
  26. package/dist/esm/cache.js +54 -0
  27. package/dist/esm/cache.js.map +1 -0
  28. package/dist/esm/client.js +192 -0
  29. package/dist/esm/client.js.map +1 -0
  30. package/dist/esm/costs.js +100 -0
  31. package/dist/esm/costs.js.map +1 -0
  32. package/dist/esm/errors.js +278 -0
  33. package/dist/esm/errors.js.map +1 -0
  34. package/dist/esm/execution.js +381 -0
  35. package/dist/esm/execution.js.map +1 -0
  36. package/dist/esm/index.js +236 -0
  37. package/dist/esm/index.js.map +1 -0
  38. package/dist/esm/intents.js +181 -0
  39. package/dist/esm/intents.js.map +1 -0
  40. package/dist/esm/serialization.js +159 -0
  41. package/dist/esm/serialization.js.map +1 -0
  42. package/dist/esm/types.js +28 -0
  43. package/dist/esm/types.js.map +1 -0
  44. package/dist/esm/validation.js +111 -0
  45. package/dist/esm/validation.js.map +1 -0
  46. package/dist/types/agent.d.ts +171 -0
  47. package/dist/types/agent.d.ts.map +1 -0
  48. package/dist/types/cache.d.ts +25 -0
  49. package/dist/types/cache.d.ts.map +1 -0
  50. package/dist/types/client.d.ts +46 -0
  51. package/dist/types/client.d.ts.map +1 -0
  52. package/dist/types/costs.d.ts +91 -0
  53. package/dist/types/costs.d.ts.map +1 -0
  54. package/dist/types/errors.d.ts +242 -0
  55. package/dist/types/errors.d.ts.map +1 -0
  56. package/dist/types/execution.d.ts +145 -0
  57. package/dist/types/execution.d.ts.map +1 -0
  58. package/dist/types/index.d.ts +205 -0
  59. package/dist/types/index.d.ts.map +1 -0
  60. package/dist/types/intents.d.ts +158 -0
  61. package/dist/types/intents.d.ts.map +1 -0
  62. package/dist/types/serialization.d.ts +82 -0
  63. package/dist/types/serialization.d.ts.map +1 -0
  64. package/dist/types/types.d.ts +302 -0
  65. package/dist/types/types.d.ts.map +1 -0
  66. package/dist/types/validation.d.ts +40 -0
  67. package/dist/types/validation.d.ts.map +1 -0
  68. package/package.json +56 -0
@@ -0,0 +1,100 @@
1
+ /**
2
+ * Cost tracking module for JACK SDK
3
+ *
4
+ * Provides methods to query execution costs and budgets.
5
+ * Validates Requirements 4.1, 4.2
6
+ */
7
+ /**
8
+ * CostTracker class for querying execution costs and budgets
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * const tracker = new CostTracker(client);
13
+ * const costs = await tracker.getCosts();
14
+ * const issueCost = await tracker.getIssueCost('ISSUE-123');
15
+ * const overBudget = await tracker.getOverBudgetIssues();
16
+ * ```
17
+ */
18
+ export class CostTracker {
19
+ client;
20
+ constructor(client) {
21
+ this.client = client;
22
+ }
23
+ /**
24
+ * Get all issue costs from the API
25
+ *
26
+ * Makes a GET request to /api/costs and returns the complete response
27
+ * with cost data for all issues.
28
+ *
29
+ * @returns Promise resolving to CostsResponse with all issue costs
30
+ * @throws {APIError} If the API request fails
31
+ * @throws {NetworkError} If network connection fails
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * const costs = await tracker.getCosts();
36
+ * console.log(`Found ${costs.issueCosts.length} issues`);
37
+ * ```
38
+ *
39
+ * Validates Requirement 4.1
40
+ */
41
+ async getCosts() {
42
+ return this.client.get('/api/costs');
43
+ }
44
+ /**
45
+ * Get costs for a specific issue
46
+ *
47
+ * Fetches all costs and filters by the specified issue ID.
48
+ * Returns null if the issue is not found.
49
+ *
50
+ * @param issueId - The issue identifier to filter by
51
+ * @returns Promise resolving to IssueCost or null if not found
52
+ * @throws {APIError} If the API request fails
53
+ * @throws {NetworkError} If network connection fails
54
+ *
55
+ * @example
56
+ * ```typescript
57
+ * const issueCost = await tracker.getIssueCost('ISSUE-123');
58
+ * if (issueCost) {
59
+ * console.log(`Total cost: ${issueCost.totalCost}`);
60
+ * console.log(`Budget: ${issueCost.budget}`);
61
+ * console.log(`Over budget: ${issueCost.overBudget}`);
62
+ * }
63
+ * ```
64
+ *
65
+ * Validates Requirement 4.2
66
+ */
67
+ async getIssueCost(issueId) {
68
+ const response = await this.getCosts();
69
+ const issueCost = response.issueCosts.find((cost) => cost.issueId === issueId);
70
+ return issueCost ?? null;
71
+ }
72
+ /**
73
+ * Get all issues that are over budget
74
+ *
75
+ * Fetches all costs and filters to only those with overBudget flag set to true.
76
+ * Returns an empty array if no issues are over budget.
77
+ *
78
+ * @returns Promise resolving to array of IssueCost objects that are over budget
79
+ * @throws {APIError} If the API request fails
80
+ * @throws {NetworkError} If network connection fails
81
+ *
82
+ * @example
83
+ * ```typescript
84
+ * const overBudget = await tracker.getOverBudgetIssues();
85
+ * if (overBudget.length > 0) {
86
+ * console.log(`Warning: ${overBudget.length} issues are over budget`);
87
+ * overBudget.forEach(issue => {
88
+ * console.log(`${issue.issueId}: ${issue.totalCost} / ${issue.budget}`);
89
+ * });
90
+ * }
91
+ * ```
92
+ *
93
+ * Validates Requirement 4.2
94
+ */
95
+ async getOverBudgetIssues() {
96
+ const response = await this.getCosts();
97
+ return response.issueCosts.filter((cost) => cost.overBudget);
98
+ }
99
+ }
100
+ //# sourceMappingURL=costs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"costs.js","sourceRoot":"","sources":["../../src/costs.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH;;;;;;;;;;GAUG;AACH,MAAM,OAAO,WAAW;IACO;IAA7B,YAA6B,MAAkB;QAAlB,WAAM,GAAN,MAAM,CAAY;IAAG,CAAC;IAEnD;;;;;;;;;;;;;;;;;OAiBG;IACH,KAAK,CAAC,QAAQ;QACZ,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAgB,YAAY,CAAC,CAAC;IACtD,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,KAAK,CAAC,YAAY,CAAC,OAAe;QAChC,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,QAAQ,EAAE,CAAC;QACvC,MAAM,SAAS,GAAG,QAAQ,CAAC,UAAU,CAAC,IAAI,CACxC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,OAAO,KAAK,OAAO,CACnC,CAAC;QACF,OAAO,SAAS,IAAI,IAAI,CAAC;IAC3B,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,KAAK,CAAC,mBAAmB;QACvB,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,QAAQ,EAAE,CAAC;QACvC,OAAO,QAAQ,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IAC/D,CAAC;CACF"}
@@ -0,0 +1,278 @@
1
+ /**
2
+ * Error hierarchy for the JACK SDK
3
+ *
4
+ * This module defines custom error classes for different failure scenarios.
5
+ * All errors extend the base JackError class and include context for debugging.
6
+ * Requirements: 5.2, 5.3
7
+ */
8
+ /**
9
+ * Base error class for all JACK SDK errors
10
+ *
11
+ * Extends the native Error class and adds optional context for debugging.
12
+ * All SDK-specific errors inherit from this class.
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * throw new JackError('Something went wrong', { requestId: '123', timestamp: Date.now() });
17
+ * ```
18
+ *
19
+ * Requirement 5.2
20
+ */
21
+ export class JackError extends Error {
22
+ /**
23
+ * Additional context information for debugging
24
+ * Can include request details, timestamps, or any relevant metadata
25
+ */
26
+ context;
27
+ /**
28
+ * Creates a new JackError
29
+ *
30
+ * @param message - Human-readable error message
31
+ * @param context - Optional context object with debugging information
32
+ */
33
+ constructor(message, context) {
34
+ super(message);
35
+ this.name = 'JackError';
36
+ this.context = context;
37
+ // Maintains proper stack trace for where our error was thrown (only available on V8)
38
+ if (Error.captureStackTrace) {
39
+ Error.captureStackTrace(this, this.constructor);
40
+ }
41
+ }
42
+ }
43
+ /**
44
+ * Error thrown when network-level failures occur
45
+ *
46
+ * This includes connection failures, DNS resolution errors, timeouts at the
47
+ * network layer, and other transport-level issues. The original error is
48
+ * preserved for detailed debugging.
49
+ *
50
+ * @example
51
+ * ```typescript
52
+ * try {
53
+ * await fetch('https://api.jack.example');
54
+ * } catch (err) {
55
+ * throw new NetworkError('Failed to connect to API', err as Error);
56
+ * }
57
+ * ```
58
+ *
59
+ * Requirement 5.3
60
+ */
61
+ export class NetworkError extends JackError {
62
+ /**
63
+ * The original error that caused the network failure
64
+ * Preserved for detailed debugging and error analysis
65
+ */
66
+ originalError;
67
+ /**
68
+ * Creates a new NetworkError
69
+ *
70
+ * @param message - Human-readable error message
71
+ * @param originalError - The underlying error that caused the network failure
72
+ * @param context - Optional additional context
73
+ */
74
+ constructor(message, originalError, context) {
75
+ super(message, context);
76
+ this.name = 'NetworkError';
77
+ this.originalError = originalError;
78
+ }
79
+ }
80
+ /**
81
+ * Error thrown when the API returns an error response
82
+ *
83
+ * This includes 4xx client errors (bad request, not found, unauthorized) and
84
+ * 5xx server errors (internal server error, service unavailable). The HTTP
85
+ * status code and response body are included for debugging.
86
+ *
87
+ * @example
88
+ * ```typescript
89
+ * if (response.status >= 400) {
90
+ * throw new APIError(
91
+ * 'Intent not found',
92
+ * 404,
93
+ * { error: 'Intent with ID JK-ABC123456 does not exist' }
94
+ * );
95
+ * }
96
+ * ```
97
+ *
98
+ * Requirement 5.3
99
+ */
100
+ export class APIError extends JackError {
101
+ /**
102
+ * HTTP status code from the API response
103
+ * Used to distinguish between client errors (4xx) and server errors (5xx)
104
+ */
105
+ statusCode;
106
+ /**
107
+ * The parsed response body from the API
108
+ * May contain error details, validation messages, or other diagnostic information
109
+ */
110
+ response;
111
+ /**
112
+ * Creates a new APIError
113
+ *
114
+ * @param message - Human-readable error message
115
+ * @param statusCode - HTTP status code from the response
116
+ * @param response - Optional parsed response body
117
+ * @param context - Optional additional context
118
+ */
119
+ constructor(message, statusCode, response, context) {
120
+ super(message, context);
121
+ this.name = 'APIError';
122
+ this.statusCode = statusCode;
123
+ this.response = response;
124
+ }
125
+ /**
126
+ * Check if this is a client error (4xx status code)
127
+ * @returns true if status code is in the 400-499 range
128
+ */
129
+ isClientError() {
130
+ return this.statusCode >= 400 && this.statusCode < 500;
131
+ }
132
+ /**
133
+ * Check if this is a server error (5xx status code)
134
+ * @returns true if status code is in the 500-599 range
135
+ */
136
+ isServerError() {
137
+ return this.statusCode >= 500 && this.statusCode < 600;
138
+ }
139
+ /**
140
+ * Check if this error is retryable
141
+ * Server errors (5xx) are typically retryable, client errors (4xx) are not
142
+ * @returns true if the error should be retried
143
+ */
144
+ isRetryable() {
145
+ return this.isServerError();
146
+ }
147
+ }
148
+ /**
149
+ * Error thrown when client-side validation fails
150
+ *
151
+ * This error is thrown before making any network requests when input parameters
152
+ * fail validation checks. It includes an array of specific validation errors
153
+ * to help developers identify and fix issues.
154
+ *
155
+ * @example
156
+ * ```typescript
157
+ * const errors = [];
158
+ * if (params.amountIn <= 0) errors.push('amountIn must be positive');
159
+ * if (params.deadline < Date.now()) errors.push('deadline must be in the future');
160
+ * if (errors.length > 0) {
161
+ * throw new ValidationError('Invalid intent parameters', errors);
162
+ * }
163
+ * ```
164
+ *
165
+ * Requirement 5.3
166
+ */
167
+ export class ValidationError extends JackError {
168
+ /**
169
+ * Array of specific validation error messages
170
+ * Each message describes a single validation failure
171
+ */
172
+ errors;
173
+ /**
174
+ * Creates a new ValidationError
175
+ *
176
+ * @param message - Human-readable error message
177
+ * @param errors - Array of specific validation error messages
178
+ * @param context - Optional additional context
179
+ */
180
+ constructor(message, errors, context) {
181
+ super(message, context);
182
+ this.name = 'ValidationError';
183
+ this.errors = errors;
184
+ }
185
+ }
186
+ /**
187
+ * Error thrown when an operation exceeds its timeout
188
+ *
189
+ * This includes request timeouts, polling timeouts, and any other time-bound
190
+ * operations. The timeout duration is included for debugging.
191
+ *
192
+ * @example
193
+ * ```typescript
194
+ * const timeout = 30000; // 30 seconds
195
+ * const timeoutPromise = new Promise((_, reject) => {
196
+ * setTimeout(() => {
197
+ * reject(new TimeoutError('Request timed out', timeout));
198
+ * }, timeout);
199
+ * });
200
+ * await Promise.race([fetchPromise, timeoutPromise]);
201
+ * ```
202
+ *
203
+ * Requirement 5.3
204
+ */
205
+ export class TimeoutError extends JackError {
206
+ /**
207
+ * The timeout duration in milliseconds
208
+ * Indicates how long the operation was allowed to run before timing out
209
+ */
210
+ timeoutMs;
211
+ /**
212
+ * Creates a new TimeoutError
213
+ *
214
+ * @param message - Human-readable error message
215
+ * @param timeoutMs - The timeout duration in milliseconds
216
+ * @param context - Optional additional context
217
+ */
218
+ constructor(message, timeoutMs, context) {
219
+ super(message, context);
220
+ this.name = 'TimeoutError';
221
+ this.timeoutMs = timeoutMs;
222
+ }
223
+ }
224
+ /**
225
+ * Error thrown when retry attempts are exhausted
226
+ *
227
+ * This error is thrown after all retry attempts have failed. It includes the
228
+ * number of attempts made and the final error that caused the failure.
229
+ *
230
+ * @example
231
+ * ```typescript
232
+ * let lastError: Error;
233
+ * for (let attempt = 1; attempt <= maxRetries; attempt++) {
234
+ * try {
235
+ * return await makeRequest();
236
+ * } catch (err) {
237
+ * lastError = err as Error;
238
+ * if (attempt === maxRetries) {
239
+ * throw new RetryError(
240
+ * 'All retry attempts exhausted',
241
+ * attempt,
242
+ * lastError
243
+ * );
244
+ * }
245
+ * await delay(retryDelay * Math.pow(retryBackoff, attempt - 1));
246
+ * }
247
+ * }
248
+ * ```
249
+ *
250
+ * Requirement 5.3
251
+ */
252
+ export class RetryError extends JackError {
253
+ /**
254
+ * The number of retry attempts that were made
255
+ * Includes the initial attempt plus all retries
256
+ */
257
+ attempts;
258
+ /**
259
+ * The final error that caused the retry loop to fail
260
+ * This is the error from the last retry attempt
261
+ */
262
+ lastError;
263
+ /**
264
+ * Creates a new RetryError
265
+ *
266
+ * @param message - Human-readable error message
267
+ * @param attempts - The number of attempts that were made
268
+ * @param lastError - The final error from the last attempt
269
+ * @param context - Optional additional context
270
+ */
271
+ constructor(message, attempts, lastError, context) {
272
+ super(message, context);
273
+ this.name = 'RetryError';
274
+ this.attempts = attempts;
275
+ this.lastError = lastError;
276
+ }
277
+ }
278
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,SAAU,SAAQ,KAAK;IAClC;;;OAGG;IACa,OAAO,CAA2B;IAElD;;;;;OAKG;IACH,YAAY,OAAe,EAAE,OAAiC;QAC5D,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,WAAW,CAAC;QACxB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QAEvB,qFAAqF;QACrF,IAAI,KAAK,CAAC,iBAAiB,EAAE,CAAC;YAC5B,KAAK,CAAC,iBAAiB,CAAC,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC;QAClD,CAAC;IACH,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;GAi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