@agent-os-sdk/client 0.9.26 → 0.9.27

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 (209) hide show
  1. package/dist/client/AgentOsClient.d.ts +141 -0
  2. package/dist/client/AgentOsClient.d.ts.map +1 -0
  3. package/{src/client/AgentOsClient.ts → dist/client/AgentOsClient.js} +61 -105
  4. package/dist/client/HttpRequestBuilder.d.ts +50 -0
  5. package/dist/client/HttpRequestBuilder.d.ts.map +1 -0
  6. package/{src/client/HttpRequestBuilder.ts → dist/client/HttpRequestBuilder.js} +9 -41
  7. package/{src/client/OperationContext.ts → dist/client/OperationContext.d.ts} +6 -9
  8. package/dist/client/OperationContext.d.ts.map +1 -0
  9. package/dist/client/OperationContext.js +13 -0
  10. package/dist/client/OperationContextProvider.d.ts +54 -0
  11. package/dist/client/OperationContextProvider.d.ts.map +1 -0
  12. package/{src/client/OperationContextProvider.ts → dist/client/OperationContextProvider.js} +17 -35
  13. package/{src/client/auth.ts → dist/client/auth.d.ts} +29 -65
  14. package/dist/client/auth.d.ts.map +1 -0
  15. package/dist/client/auth.js +38 -0
  16. package/dist/client/config.d.ts +49 -0
  17. package/dist/client/config.d.ts.map +1 -0
  18. package/{src/client/config.ts → dist/client/config.js} +5 -43
  19. package/dist/client/helpers.d.ts +63 -0
  20. package/dist/client/helpers.d.ts.map +1 -0
  21. package/{src/client/helpers.ts → dist/client/helpers.js} +9 -52
  22. package/dist/client/pagination.d.ts +105 -0
  23. package/dist/client/pagination.d.ts.map +1 -0
  24. package/{src/client/pagination.ts → dist/client/pagination.js} +15 -116
  25. package/dist/client/raw.d.ts +277 -0
  26. package/dist/client/raw.d.ts.map +1 -0
  27. package/dist/client/raw.js +337 -0
  28. package/dist/client/retry.d.ts +37 -0
  29. package/dist/client/retry.d.ts.map +1 -0
  30. package/{src/client/retry.ts → dist/client/retry.js} +13 -55
  31. package/dist/client/sanitize.d.ts +19 -0
  32. package/dist/client/sanitize.d.ts.map +1 -0
  33. package/{src/client/sanitize.ts → dist/client/sanitize.js} +4 -7
  34. package/dist/client/timeout.d.ts +26 -0
  35. package/dist/client/timeout.d.ts.map +1 -0
  36. package/{src/client/timeout.ts → dist/client/timeout.js} +10 -18
  37. package/dist/errors/factory.d.ts +22 -0
  38. package/dist/errors/factory.d.ts.map +1 -0
  39. package/{src/errors/factory.ts → dist/errors/factory.js} +18 -57
  40. package/dist/errors/index.d.ts +210 -0
  41. package/dist/errors/index.d.ts.map +1 -0
  42. package/{src/errors/index.ts → dist/errors/index.js} +74 -156
  43. package/{src/generated/client.ts → dist/generated/client.d.ts} +5 -17
  44. package/dist/generated/client.d.ts.map +1 -0
  45. package/dist/generated/client.js +15 -0
  46. package/{src/generated/index.ts → dist/generated/index.d.ts} +1 -1
  47. package/dist/generated/index.d.ts.map +1 -0
  48. package/dist/generated/index.js +1 -0
  49. package/{src/generated/openapi.ts → dist/generated/openapi.d.ts} +1 -1
  50. package/dist/generated/openapi.d.ts.map +1 -0
  51. package/dist/generated/openapi.js +5 -0
  52. package/dist/index.d.ts +69 -0
  53. package/dist/index.d.ts.map +1 -0
  54. package/{src/index.ts → dist/index.js} +13 -63
  55. package/dist/modules/a2a.d.ts +48 -0
  56. package/dist/modules/a2a.d.ts.map +1 -0
  57. package/dist/modules/a2a.js +37 -0
  58. package/dist/modules/agents.d.ts +346 -0
  59. package/dist/modules/agents.d.ts.map +1 -0
  60. package/dist/modules/agents.js +296 -0
  61. package/dist/modules/apiTokens.d.ts +62 -0
  62. package/dist/modules/apiTokens.d.ts.map +1 -0
  63. package/dist/modules/apiTokens.js +56 -0
  64. package/dist/modules/approvals.d.ts +75 -0
  65. package/dist/modules/approvals.d.ts.map +1 -0
  66. package/dist/modules/approvals.js +91 -0
  67. package/dist/modules/audit.d.ts +78 -0
  68. package/dist/modules/audit.d.ts.map +1 -0
  69. package/{src/modules/audit.ts → dist/modules/audit.js} +23 -74
  70. package/{src/modules/auth.ts → dist/modules/auth.d.ts} +6 -11
  71. package/dist/modules/auth.d.ts.map +1 -0
  72. package/dist/modules/auth.js +22 -0
  73. package/dist/modules/builder.d.ts +242 -0
  74. package/dist/modules/builder.d.ts.map +1 -0
  75. package/dist/modules/builder.js +248 -0
  76. package/dist/modules/catalog.d.ts +142 -0
  77. package/dist/modules/catalog.d.ts.map +1 -0
  78. package/dist/modules/catalog.js +136 -0
  79. package/dist/modules/chatwoot.d.ts +59 -0
  80. package/dist/modules/chatwoot.d.ts.map +1 -0
  81. package/dist/modules/chatwoot.js +200 -0
  82. package/dist/modules/checkpoints.d.ts +62 -0
  83. package/dist/modules/checkpoints.d.ts.map +1 -0
  84. package/dist/modules/checkpoints.js +48 -0
  85. package/dist/modules/contracts.d.ts +48 -0
  86. package/dist/modules/contracts.d.ts.map +1 -0
  87. package/dist/modules/contracts.js +25 -0
  88. package/dist/modules/credentials.d.ts +105 -0
  89. package/dist/modules/credentials.d.ts.map +1 -0
  90. package/dist/modules/credentials.js +135 -0
  91. package/dist/modules/crons.d.ts +73 -0
  92. package/dist/modules/crons.d.ts.map +1 -0
  93. package/dist/modules/crons.js +67 -0
  94. package/dist/modules/datasets.d.ts +80 -0
  95. package/dist/modules/datasets.d.ts.map +1 -0
  96. package/dist/modules/datasets.js +91 -0
  97. package/{src/modules/evaluation.ts → dist/modules/evaluation.d.ts} +23 -109
  98. package/dist/modules/evaluation.d.ts.map +1 -0
  99. package/dist/modules/evaluation.js +108 -0
  100. package/dist/modules/files.d.ts +89 -0
  101. package/dist/modules/files.d.ts.map +1 -0
  102. package/dist/modules/files.js +133 -0
  103. package/dist/modules/graphs.d.ts +120 -0
  104. package/dist/modules/graphs.d.ts.map +1 -0
  105. package/dist/modules/graphs.js +69 -0
  106. package/{src/modules/improvements.ts → dist/modules/improvements.d.ts} +8 -27
  107. package/dist/modules/improvements.d.ts.map +1 -0
  108. package/dist/modules/improvements.js +27 -0
  109. package/dist/modules/info.d.ts +76 -0
  110. package/dist/modules/info.d.ts.map +1 -0
  111. package/{src/modules/info.ts → dist/modules/info.js} +16 -66
  112. package/dist/modules/knowledge.d.ts +91 -0
  113. package/dist/modules/knowledge.d.ts.map +1 -0
  114. package/dist/modules/knowledge.js +80 -0
  115. package/{src/modules/me.ts → dist/modules/me.d.ts} +7 -23
  116. package/dist/modules/me.d.ts.map +1 -0
  117. package/dist/modules/me.js +28 -0
  118. package/dist/modules/members.d.ts +124 -0
  119. package/dist/modules/members.d.ts.map +1 -0
  120. package/dist/modules/members.js +142 -0
  121. package/dist/modules/memberships.d.ts +25 -0
  122. package/dist/modules/memberships.d.ts.map +1 -0
  123. package/dist/modules/memberships.js +20 -0
  124. package/{src/modules/metaAgent.ts → dist/modules/metaAgent.d.ts} +8 -44
  125. package/dist/modules/metaAgent.d.ts.map +1 -0
  126. package/dist/modules/metaAgent.js +32 -0
  127. package/dist/modules/metrics.d.ts +20 -0
  128. package/dist/modules/metrics.d.ts.map +1 -0
  129. package/{src/modules/metrics.ts → dist/modules/metrics.js} +7 -14
  130. package/dist/modules/observability.d.ts +19 -0
  131. package/dist/modules/observability.d.ts.map +1 -0
  132. package/dist/modules/observability.js +14 -0
  133. package/dist/modules/playground.d.ts +42 -0
  134. package/dist/modules/playground.d.ts.map +1 -0
  135. package/dist/modules/playground.js +48 -0
  136. package/dist/modules/presets.d.ts +32 -0
  137. package/dist/modules/presets.d.ts.map +1 -0
  138. package/dist/modules/presets.js +199 -0
  139. package/dist/modules/prompts.d.ts +87 -0
  140. package/dist/modules/prompts.d.ts.map +1 -0
  141. package/dist/modules/prompts.js +87 -0
  142. package/dist/modules/roles.d.ts +64 -0
  143. package/dist/modules/roles.d.ts.map +1 -0
  144. package/dist/modules/roles.js +79 -0
  145. package/dist/modules/runs.d.ts +451 -0
  146. package/dist/modules/runs.d.ts.map +1 -0
  147. package/{src/modules/runs.ts → dist/modules/runs.js} +96 -438
  148. package/dist/modules/store.d.ts +37 -0
  149. package/dist/modules/store.d.ts.map +1 -0
  150. package/dist/modules/store.js +48 -0
  151. package/{src/modules/templates.ts → dist/modules/templates.d.ts} +8 -32
  152. package/dist/modules/templates.d.ts.map +1 -0
  153. package/dist/modules/templates.js +26 -0
  154. package/dist/modules/tenants.d.ts +49 -0
  155. package/dist/modules/tenants.d.ts.map +1 -0
  156. package/dist/modules/tenants.js +56 -0
  157. package/dist/modules/threads.d.ts +205 -0
  158. package/dist/modules/threads.d.ts.map +1 -0
  159. package/dist/modules/threads.js +225 -0
  160. package/dist/modules/tools.d.ts +62 -0
  161. package/dist/modules/tools.d.ts.map +1 -0
  162. package/dist/modules/tools.js +45 -0
  163. package/dist/modules/traces.d.ts +89 -0
  164. package/dist/modules/traces.d.ts.map +1 -0
  165. package/dist/modules/traces.js +65 -0
  166. package/{src/modules/triggers.ts → dist/modules/triggers.d.ts} +24 -153
  167. package/dist/modules/triggers.d.ts.map +1 -0
  168. package/dist/modules/triggers.js +176 -0
  169. package/{src/modules/usage.ts → dist/modules/usage.d.ts} +14 -46
  170. package/dist/modules/usage.d.ts.map +1 -0
  171. package/dist/modules/usage.js +43 -0
  172. package/dist/modules/vectorStores.d.ts +100 -0
  173. package/dist/modules/vectorStores.d.ts.map +1 -0
  174. package/dist/modules/vectorStores.js +159 -0
  175. package/dist/modules/workspaces.d.ts +138 -0
  176. package/dist/modules/workspaces.d.ts.map +1 -0
  177. package/dist/modules/workspaces.js +121 -0
  178. package/dist/sse/client.d.ts +140 -0
  179. package/dist/sse/client.d.ts.map +1 -0
  180. package/{src/sse/client.ts → dist/sse/client.js} +24 -100
  181. package/package.json +8 -15
  182. package/src/client/raw.ts +0 -609
  183. package/src/generated/swagger.json +0 -17014
  184. package/src/modules/a2a.ts +0 -64
  185. package/src/modules/agents.ts +0 -604
  186. package/src/modules/apiTokens.ts +0 -101
  187. package/src/modules/approvals.ts +0 -151
  188. package/src/modules/catalog.ts +0 -241
  189. package/src/modules/chatwoot.ts +0 -242
  190. package/src/modules/checkpoints.ts +0 -87
  191. package/src/modules/contracts.ts +0 -80
  192. package/src/modules/credentials.ts +0 -216
  193. package/src/modules/crons.ts +0 -115
  194. package/src/modules/datasets.ts +0 -142
  195. package/src/modules/files.ts +0 -208
  196. package/src/modules/members.ts +0 -199
  197. package/src/modules/memberships.ts +0 -42
  198. package/src/modules/observability.ts +0 -28
  199. package/src/modules/playground.ts +0 -68
  200. package/src/modules/presets.ts +0 -246
  201. package/src/modules/prompts.ts +0 -147
  202. package/src/modules/roles.ts +0 -112
  203. package/src/modules/store.ts +0 -65
  204. package/src/modules/tenants.ts +0 -79
  205. package/src/modules/threads.ts +0 -343
  206. package/src/modules/tools.ts +0 -91
  207. package/src/modules/traces.ts +0 -133
  208. package/src/modules/vectorStores.ts +0 -257
  209. package/src/modules/workspaces.ts +0 -216
@@ -1,150 +1,108 @@
1
1
  /**
2
2
  * Agent OS SDK - Retry Logic
3
- *
3
+ *
4
4
  * Enterprise-grade retry with:
5
5
  * - Exponential backoff with jitter
6
6
  * - Respect for Retry-After headers
7
7
  * - Idempotency-aware mutation retries
8
8
  * - Proper abort signal handling (no listener leaks)
9
9
  */
10
-
11
10
  import { AgentOsError, NetworkError, TimeoutError, RateLimitError } from "../errors/index.js";
12
- import type { RetryConfig } from "./config.js";
13
-
14
- /**
15
- * Context for retry decision-making.
16
- */
17
- export interface RetryContext {
18
- /** HTTP method (GET, POST, etc.) */
19
- method: string;
20
-
21
- /** Whether the request has an idempotency key */
22
- hasIdempotencyKey: boolean;
23
- }
24
-
25
11
  /**
26
12
  * Wraps an async function with retry logic.
27
- *
13
+ *
28
14
  * IMPORTANT: Mutations (POST/PUT/PATCH/DELETE) are only retried if hasIdempotencyKey is true.
29
15
  * This prevents duplicate side effects.
30
- *
16
+ *
31
17
  * @param fn - Function to execute (receives an AbortSignal)
32
18
  * @param config - Retry configuration
33
19
  * @param context - Request context for retry decisions
34
20
  * @param parentSignal - Optional parent AbortSignal for cancellation
35
21
  */
36
- export async function withRetry<T>(
37
- fn: (signal: AbortSignal) => Promise<T>,
38
- config: RetryConfig,
39
- context: RetryContext,
40
- parentSignal?: AbortSignal
41
- ): Promise<T> {
42
- let lastError: Error | undefined;
43
-
22
+ export async function withRetry(fn, config, context, parentSignal) {
23
+ let lastError;
44
24
  for (let attempt = 0; attempt <= config.maxRetries; attempt++) {
45
25
  // Check parent abort before each attempt
46
26
  if (parentSignal?.aborted) {
47
27
  throw new Error("Request aborted");
48
28
  }
49
-
50
29
  const controller = new AbortController();
51
-
52
30
  // Proper abort propagation without listener leak
53
31
  const onAbort = () => controller.abort();
54
32
  parentSignal?.addEventListener("abort", onAbort, { once: true });
55
-
56
33
  try {
57
34
  return await fn(controller.signal);
58
- } catch (err) {
59
- lastError = err as Error;
60
-
35
+ }
36
+ catch (err) {
37
+ lastError = err;
61
38
  // If parent aborted, don't retry
62
39
  if (controller.signal.aborted && parentSignal?.aborted) {
63
40
  throw err;
64
41
  }
65
-
66
42
  // Check if this error is retryable
67
43
  if (!shouldRetry(err, config, context)) {
68
44
  throw err;
69
45
  }
70
-
71
46
  // Last attempt reached
72
47
  if (attempt === config.maxRetries) {
73
48
  throw err;
74
49
  }
75
-
76
50
  // Rate limit: use Retry-After if available
77
51
  if (err instanceof RateLimitError && err.retryAfterMs) {
78
52
  await sleep(err.retryAfterMs);
79
53
  continue;
80
54
  }
81
-
82
55
  // Exponential backoff with jitter
83
56
  const delay = calculateBackoff(attempt, config);
84
57
  await sleep(delay);
85
- } finally {
58
+ }
59
+ finally {
86
60
  // CRITICAL: Always cleanup listener to prevent memory leak
87
61
  parentSignal?.removeEventListener("abort", onAbort);
88
62
  }
89
63
  }
90
-
91
64
  throw lastError ?? new Error("Retry failed");
92
65
  }
93
-
94
66
  /**
95
67
  * Determines if an error should trigger a retry.
96
68
  */
97
- function shouldRetry(
98
- err: unknown,
99
- config: RetryConfig,
100
- context: RetryContext
101
- ): boolean {
69
+ function shouldRetry(err, config, context) {
102
70
  // Network and timeout errors are always retryable
103
71
  if (err instanceof NetworkError || err instanceof TimeoutError) {
104
72
  return true;
105
73
  }
106
-
107
74
  // Unknown errors are not retryable
108
75
  if (!(err instanceof AgentOsError)) {
109
76
  return false;
110
77
  }
111
-
112
78
  // Check if status code is in retryable list
113
79
  if (!config.retryableStatuses.includes(err.status)) {
114
80
  return false;
115
81
  }
116
-
117
82
  // CRITICAL: Only retry mutations if they have an idempotency key
118
83
  // This prevents duplicate side effects from retrying POST/PUT/PATCH/DELETE
119
84
  const isMutation = !["GET", "HEAD", "OPTIONS"].includes(context.method.toUpperCase());
120
85
  if (isMutation && !context.hasIdempotencyKey) {
121
86
  return false;
122
87
  }
123
-
124
88
  return true;
125
89
  }
126
-
127
90
  /**
128
91
  * Calculates backoff delay with exponential increase and jitter.
129
92
  */
130
- function calculateBackoff(attempt: number, config: RetryConfig): number {
93
+ function calculateBackoff(attempt, config) {
131
94
  // Exponential backoff: baseDelay * 2^attempt
132
95
  const exponential = config.baseDelayMs * Math.pow(2, attempt);
133
-
134
96
  // Cap at max delay
135
97
  const capped = Math.min(exponential, config.maxDelayMs);
136
-
137
98
  // Add jitter to prevent thundering herd
138
99
  const jitter = capped * config.jitterFactor * Math.random();
139
-
140
100
  return Math.floor(capped + jitter);
141
101
  }
142
-
143
102
  /**
144
103
  * Sleep utility.
145
104
  */
146
- function sleep(ms: number): Promise<void> {
105
+ function sleep(ms) {
147
106
  return new Promise(resolve => setTimeout(resolve, ms));
148
107
  }
149
-
150
108
  export { sleep };
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Header Sanitization — Security layer for HTTP headers
3
+ *
4
+ * Removes control characters and enforces size limits.
5
+ *
6
+ * @see sdk-upgrade.md Section 1.4
7
+ */
8
+ /**
9
+ * Sanitize a single header value.
10
+ * - Removes control characters (0x00-0x1F, 0x7F)
11
+ * - Truncates to MAX_LEN
12
+ */
13
+ export declare function sanitizeHeader(v: string): string;
14
+ /**
15
+ * Sanitize all header values in a record.
16
+ * Mutates in place for performance.
17
+ */
18
+ export declare function sanitizeHeaders(h: Record<string, string>): Record<string, string>;
19
+ //# sourceMappingURL=sanitize.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sanitize.d.ts","sourceRoot":"","sources":["../../src/client/sanitize.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAEhD;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAOjF"}
@@ -1,27 +1,24 @@
1
1
  /**
2
2
  * Header Sanitization — Security layer for HTTP headers
3
- *
3
+ *
4
4
  * Removes control characters and enforces size limits.
5
- *
5
+ *
6
6
  * @see sdk-upgrade.md Section 1.4
7
7
  */
8
-
9
8
  const MAX_LEN = 1024;
10
-
11
9
  /**
12
10
  * Sanitize a single header value.
13
11
  * - Removes control characters (0x00-0x1F, 0x7F)
14
12
  * - Truncates to MAX_LEN
15
13
  */
16
- export function sanitizeHeader(v: string): string {
14
+ export function sanitizeHeader(v) {
17
15
  return v.replace(/[\u0000-\u001F\u007F]/g, "").slice(0, MAX_LEN);
18
16
  }
19
-
20
17
  /**
21
18
  * Sanitize all header values in a record.
22
19
  * Mutates in place for performance.
23
20
  */
24
- export function sanitizeHeaders(h: Record<string, string>): Record<string, string> {
21
+ export function sanitizeHeaders(h) {
25
22
  for (const k in h) {
26
23
  if (Object.prototype.hasOwnProperty.call(h, k) && h[k] !== undefined) {
27
24
  h[k] = sanitizeHeader(h[k]);
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Agent OS SDK - Timeout Logic
3
+ *
4
+ * Wraps async operations with per-attempt timeout.
5
+ * Proper abort signal propagation without listener leaks.
6
+ */
7
+ /**
8
+ * Wraps an async function with a timeout.
9
+ *
10
+ * The timeout is per-attempt, not global. This ensures that slow attempts
11
+ * don't consume the entire retry budget.
12
+ *
13
+ * @param fn - Function to execute (receives an AbortSignal)
14
+ * @param timeoutMs - Timeout in milliseconds
15
+ * @param parentSignal - Optional parent AbortSignal for cancellation
16
+ */
17
+ export declare function withTimeout<T>(fn: (signal: AbortSignal) => Promise<T>, timeoutMs: number, parentSignal?: AbortSignal): Promise<T>;
18
+ /**
19
+ * Creates an AbortController that times out after the specified duration.
20
+ * Useful for creating deadline-aware operations.
21
+ *
22
+ * @param timeoutMs - Timeout in milliseconds
23
+ * @returns AbortController that will abort after timeout
24
+ */
25
+ export declare function createTimeoutController(timeoutMs: number): AbortController;
26
+ //# sourceMappingURL=timeout.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"timeout.d.ts","sourceRoot":"","sources":["../../src/client/timeout.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH;;;;;;;;;GASG;AACH,wBAAsB,WAAW,CAAC,CAAC,EAC/B,EAAE,EAAE,CAAC,MAAM,EAAE,WAAW,KAAK,OAAO,CAAC,CAAC,CAAC,EACvC,SAAS,EAAE,MAAM,EACjB,YAAY,CAAC,EAAE,WAAW,GAC3B,OAAO,CAAC,CAAC,CAAC,CAsBZ;AAED;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CAAC,SAAS,EAAE,MAAM,GAAG,eAAe,CAI1E"}
@@ -1,58 +1,50 @@
1
1
  /**
2
2
  * Agent OS SDK - Timeout Logic
3
- *
3
+ *
4
4
  * Wraps async operations with per-attempt timeout.
5
5
  * Proper abort signal propagation without listener leaks.
6
6
  */
7
-
8
7
  import { TimeoutError } from "../errors/index.js";
9
-
10
8
  /**
11
9
  * Wraps an async function with a timeout.
12
- *
10
+ *
13
11
  * The timeout is per-attempt, not global. This ensures that slow attempts
14
12
  * don't consume the entire retry budget.
15
- *
13
+ *
16
14
  * @param fn - Function to execute (receives an AbortSignal)
17
15
  * @param timeoutMs - Timeout in milliseconds
18
16
  * @param parentSignal - Optional parent AbortSignal for cancellation
19
17
  */
20
- export async function withTimeout<T>(
21
- fn: (signal: AbortSignal) => Promise<T>,
22
- timeoutMs: number,
23
- parentSignal?: AbortSignal
24
- ): Promise<T> {
18
+ export async function withTimeout(fn, timeoutMs, parentSignal) {
25
19
  const controller = new AbortController();
26
-
27
20
  // Propagate parent abort
28
21
  const onAbort = () => controller.abort();
29
22
  parentSignal?.addEventListener("abort", onAbort, { once: true });
30
-
31
23
  // Set timeout
32
24
  const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
33
-
34
25
  try {
35
26
  return await fn(controller.signal);
36
- } catch (err) {
27
+ }
28
+ catch (err) {
37
29
  // If our timeout triggered the abort (not parent), throw TimeoutError
38
30
  if (controller.signal.aborted && !parentSignal?.aborted) {
39
31
  throw new TimeoutError(timeoutMs);
40
32
  }
41
33
  throw err;
42
- } finally {
34
+ }
35
+ finally {
43
36
  clearTimeout(timeoutId);
44
37
  parentSignal?.removeEventListener("abort", onAbort);
45
38
  }
46
39
  }
47
-
48
40
  /**
49
41
  * Creates an AbortController that times out after the specified duration.
50
42
  * Useful for creating deadline-aware operations.
51
- *
43
+ *
52
44
  * @param timeoutMs - Timeout in milliseconds
53
45
  * @returns AbortController that will abort after timeout
54
46
  */
55
- export function createTimeoutController(timeoutMs: number): AbortController {
47
+ export function createTimeoutController(timeoutMs) {
56
48
  const controller = new AbortController();
57
49
  setTimeout(() => controller.abort(), timeoutMs);
58
50
  return controller;
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Agent OS SDK - Error Factory
3
+ *
4
+ * Creates typed errors from HTTP responses.
5
+ * Preserves backend error codes and details for debugging.
6
+ */
7
+ import { AgentOsError } from "./index.js";
8
+ /**
9
+ * Creates a typed error from an HTTP response.
10
+ *
11
+ * @param response - The fetch Response object
12
+ * @param body - Parsed JSON body (if available)
13
+ * @param requestPath - The original request path (for 404 errors)
14
+ */
15
+ export declare function createErrorFromResponse(response: Response, body?: {
16
+ code?: string;
17
+ message?: string;
18
+ detail?: string;
19
+ title?: string;
20
+ details?: unknown;
21
+ }, requestPath?: string): AgentOsError;
22
+ //# sourceMappingURL=factory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"factory.d.ts","sourceRoot":"","sources":["../../src/errors/factory.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EACH,YAAY,EAUf,MAAM,YAAY,CAAC;AAEpB;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CACnC,QAAQ,EAAE,QAAQ,EAClB,IAAI,CAAC,EAAE;IAAE,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,OAAO,CAAA;CAAE,EAC9F,WAAW,CAAC,EAAE,MAAM,GACrB,YAAY,CAgEd"}
@@ -1,81 +1,52 @@
1
1
  /**
2
2
  * Agent OS SDK - Error Factory
3
- *
3
+ *
4
4
  * Creates typed errors from HTTP responses.
5
5
  * Preserves backend error codes and details for debugging.
6
6
  */
7
-
8
- import {
9
- AgentOsError,
10
- UnauthorizedError,
11
- ForbiddenError,
12
- NotFoundError,
13
- ConflictError,
14
- ValidationError,
15
- RateLimitError,
16
- ServerError,
17
- type FieldError,
18
- type ErrorOptions,
19
- } from "./index.js";
20
-
7
+ import { AgentOsError, UnauthorizedError, ForbiddenError, NotFoundError, ConflictError, ValidationError, RateLimitError, ServerError, } from "./index.js";
21
8
  /**
22
9
  * Creates a typed error from an HTTP response.
23
- *
10
+ *
24
11
  * @param response - The fetch Response object
25
12
  * @param body - Parsed JSON body (if available)
26
13
  * @param requestPath - The original request path (for 404 errors)
27
14
  */
28
- export function createErrorFromResponse(
29
- response: Response,
30
- body?: { code?: string; message?: string; detail?: string; title?: string; details?: unknown },
31
- requestPath?: string
32
- ): AgentOsError {
15
+ export function createErrorFromResponse(response, body, requestPath) {
33
16
  const status = response.status;
34
- const message =
35
- body?.message ||
17
+ const message = body?.message ||
36
18
  body?.detail ||
37
19
  body?.title ||
38
20
  response.statusText ||
39
21
  `HTTP ${status}`;
40
22
  const requestId = response.headers.get("x-request-id") ?? undefined;
41
- const opts: ErrorOptions = {
23
+ const opts = {
42
24
  requestId,
43
25
  backendCode: body?.code,
44
26
  details: body?.details,
45
27
  };
46
-
47
28
  switch (status) {
48
29
  case 401:
49
30
  return new UnauthorizedError(message, opts);
50
-
51
31
  case 403:
52
32
  return new ForbiddenError(message, opts);
53
-
54
33
  case 404:
55
34
  return new NotFoundError(message, requestPath, opts);
56
-
57
35
  case 409:
58
36
  return new ConflictError(message, opts);
59
-
60
37
  case 400:
61
38
  case 422:
62
- return new ValidationError(
63
- message,
64
- status as 400 | 422,
65
- parseFieldErrors(body?.details),
66
- opts
67
- );
68
-
39
+ return new ValidationError(message, status, parseFieldErrors(body?.details), opts);
69
40
  case 429:
70
41
  const retryAfterHeader = response.headers.get("Retry-After");
71
- let retryAfterMs: number | undefined;
72
-
42
+ let retryAfterMs;
73
43
  if (retryAfterHeader) {
74
44
  // Retry-After can be seconds or a date
75
45
  const seconds = parseInt(retryAfterHeader, 10);
76
46
  if (!isNaN(seconds)) {
77
47
  retryAfterMs = seconds * 1000;
78
- } else {
48
+ }
49
+ else {
79
50
  // Try parsing as date
80
51
  const date = new Date(retryAfterHeader);
81
52
  if (!isNaN(date.getTime())) {
@@ -83,9 +54,7 @@ export function createErrorFromResponse(
83
54
  }
84
55
  }
85
56
  }
86
-
87
57
  return new RateLimitError(message, retryAfterMs, opts);
88
-
89
58
  default:
90
59
  if (status >= 500) {
91
60
  return new ServerError(message, status, opts);
@@ -94,36 +63,29 @@ export function createErrorFromResponse(
94
63
  return new ServerError(message, status, opts);
95
64
  }
96
65
  }
97
-
98
66
  /**
99
67
  * Parses field errors from various backend formats.
100
68
  */
101
- function parseFieldErrors(details: unknown): FieldError[] | undefined {
69
+ function parseFieldErrors(details) {
102
70
  if (!details || typeof details !== "object") {
103
71
  return undefined;
104
72
  }
105
-
106
73
  // Format 1: Array of { field, message } objects
107
74
  if (Array.isArray(details)) {
108
- const errors = details.filter(
109
- (d): d is FieldError =>
110
- typeof d === "object" &&
111
- d !== null &&
112
- typeof d.field === "string" &&
113
- typeof d.message === "string"
114
- );
75
+ const errors = details.filter((d) => typeof d === "object" &&
76
+ d !== null &&
77
+ typeof d.field === "string" &&
78
+ typeof d.message === "string");
115
79
  return errors.length > 0 ? errors : undefined;
116
80
  }
117
-
118
81
  // Format 2: { errors: [...] }
119
- if ("errors" in details && Array.isArray((details as { errors: unknown }).errors)) {
120
- return parseFieldErrors((details as { errors: unknown[] }).errors);
82
+ if ("errors" in details && Array.isArray(details.errors)) {
83
+ return parseFieldErrors(details.errors);
121
84
  }
122
-
123
85
  // Format 3: { field: [messages] } (Rails/Django style)
124
86
  const entries = Object.entries(details);
125
87
  if (entries.every(([, v]) => Array.isArray(v))) {
126
- const errors: FieldError[] = [];
88
+ const errors = [];
127
89
  for (const [field, messages] of entries) {
128
90
  if (Array.isArray(messages)) {
129
91
  for (const message of messages) {
@@ -135,6 +97,5 @@ function parseFieldErrors(details: unknown): FieldError[] | undefined {
135
97
  }
136
98
  return errors.length > 0 ? errors : undefined;
137
99
  }
138
-
139
100
  return undefined;
140
101
  }