@smarter.sh/api 0.0.0-stage → 1.0.2-alpha.1

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 (251) hide show
  1. package/LICENSE.txt +671 -0
  2. package/README.md +193 -2
  3. package/dist/client.d.ts +146 -0
  4. package/dist/client.d.ts.map +1 -0
  5. package/dist/client.js +178 -0
  6. package/dist/client.js.map +1 -0
  7. package/dist/core/config.d.ts +148 -0
  8. package/dist/core/config.d.ts.map +1 -0
  9. package/dist/core/config.js +212 -0
  10. package/dist/core/config.js.map +1 -0
  11. package/dist/core/errors.d.ts +189 -0
  12. package/dist/core/errors.d.ts.map +1 -0
  13. package/dist/core/errors.js +187 -0
  14. package/dist/core/errors.js.map +1 -0
  15. package/dist/core/http.d.ts +207 -0
  16. package/dist/core/http.d.ts.map +1 -0
  17. package/dist/core/http.js +456 -0
  18. package/dist/core/http.js.map +1 -0
  19. package/dist/core/log.d.ts +62 -0
  20. package/dist/core/log.d.ts.map +1 -0
  21. package/dist/core/log.js +68 -0
  22. package/dist/core/log.js.map +1 -0
  23. package/dist/core/sse.d.ts +38 -0
  24. package/dist/core/sse.d.ts.map +1 -0
  25. package/dist/core/sse.js +79 -0
  26. package/dist/core/sse.js.map +1 -0
  27. package/dist/core/typing.d.ts +21 -0
  28. package/dist/core/typing.d.ts.map +1 -0
  29. package/dist/core/typing.js +13 -0
  30. package/dist/core/typing.js.map +1 -0
  31. package/dist/index.d.ts +51 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +51 -0
  34. package/dist/index.js.map +1 -0
  35. package/dist/models/account.d.ts +112 -0
  36. package/dist/models/account.d.ts.map +1 -0
  37. package/dist/models/account.js +55 -0
  38. package/dist/models/account.js.map +1 -0
  39. package/dist/models/base.d.ts +99 -0
  40. package/dist/models/base.d.ts.map +1 -0
  41. package/dist/models/base.js +111 -0
  42. package/dist/models/base.js.map +1 -0
  43. package/dist/models/chat.d.ts +2243 -0
  44. package/dist/models/chat.d.ts.map +1 -0
  45. package/dist/models/chat.js +451 -0
  46. package/dist/models/chat.js.map +1 -0
  47. package/dist/models/guardrail.d.ts +109 -0
  48. package/dist/models/guardrail.d.ts.map +1 -0
  49. package/dist/models/guardrail.js +68 -0
  50. package/dist/models/guardrail.js.map +1 -0
  51. package/dist/models/journal.d.ts +159 -0
  52. package/dist/models/journal.d.ts.map +1 -0
  53. package/dist/models/journal.js +115 -0
  54. package/dist/models/journal.js.map +1 -0
  55. package/dist/models/llmclient.d.ts +183 -0
  56. package/dist/models/llmclient.d.ts.map +1 -0
  57. package/dist/models/llmclient.js +105 -0
  58. package/dist/models/llmclient.js.map +1 -0
  59. package/dist/models/llmhost.d.ts +121 -0
  60. package/dist/models/llmhost.d.ts.map +1 -0
  61. package/dist/models/llmhost.js +79 -0
  62. package/dist/models/llmhost.js.map +1 -0
  63. package/dist/models/manifest.d.ts +211 -0
  64. package/dist/models/manifest.d.ts.map +1 -0
  65. package/dist/models/manifest.js +290 -0
  66. package/dist/models/manifest.js.map +1 -0
  67. package/dist/models/mcpclient.d.ts +99 -0
  68. package/dist/models/mcpclient.d.ts.map +1 -0
  69. package/dist/models/mcpclient.js +65 -0
  70. package/dist/models/mcpclient.js.map +1 -0
  71. package/dist/models/misc.d.ts +232 -0
  72. package/dist/models/misc.d.ts.map +1 -0
  73. package/dist/models/misc.js +110 -0
  74. package/dist/models/misc.js.map +1 -0
  75. package/dist/models/platform.d.ts +88 -0
  76. package/dist/models/platform.d.ts.map +1 -0
  77. package/dist/models/platform.js +35 -0
  78. package/dist/models/platform.js.map +1 -0
  79. package/dist/models/plugin.d.ts +36 -0
  80. package/dist/models/plugin.d.ts.map +1 -0
  81. package/dist/models/plugin.js +16 -0
  82. package/dist/models/plugin.js.map +1 -0
  83. package/dist/models/react.d.ts +104 -0
  84. package/dist/models/react.d.ts.map +1 -0
  85. package/dist/models/react.js +136 -0
  86. package/dist/models/react.js.map +1 -0
  87. package/dist/models/sam/account.d.ts +155 -0
  88. package/dist/models/sam/account.d.ts.map +1 -0
  89. package/dist/models/sam/account.js +7 -0
  90. package/dist/models/sam/account.js.map +1 -0
  91. package/dist/models/sam/apiconnection.d.ts +147 -0
  92. package/dist/models/sam/apiconnection.d.ts.map +1 -0
  93. package/dist/models/sam/apiconnection.js +7 -0
  94. package/dist/models/sam/apiconnection.js.map +1 -0
  95. package/dist/models/sam/apiplugin.d.ts +278 -0
  96. package/dist/models/sam/apiplugin.d.ts.map +1 -0
  97. package/dist/models/sam/apiplugin.js +7 -0
  98. package/dist/models/sam/apiplugin.js.map +1 -0
  99. package/dist/models/sam/budget.d.ts +219 -0
  100. package/dist/models/sam/budget.d.ts.map +1 -0
  101. package/dist/models/sam/budget.js +7 -0
  102. package/dist/models/sam/budget.js.map +1 -0
  103. package/dist/models/sam/customdomain.d.ts +139 -0
  104. package/dist/models/sam/customdomain.d.ts.map +1 -0
  105. package/dist/models/sam/customdomain.js +7 -0
  106. package/dist/models/sam/customdomain.js.map +1 -0
  107. package/dist/models/sam/guardrail.d.ts +222 -0
  108. package/dist/models/sam/guardrail.d.ts.map +1 -0
  109. package/dist/models/sam/guardrail.js +7 -0
  110. package/dist/models/sam/guardrail.js.map +1 -0
  111. package/dist/models/sam/imagesearchplugin.d.ts +235 -0
  112. package/dist/models/sam/imagesearchplugin.d.ts.map +1 -0
  113. package/dist/models/sam/imagesearchplugin.js +7 -0
  114. package/dist/models/sam/imagesearchplugin.js.map +1 -0
  115. package/dist/models/sam/index.d.ts +104 -0
  116. package/dist/models/sam/index.d.ts.map +1 -0
  117. package/dist/models/sam/index.js +50 -0
  118. package/dist/models/sam/index.js.map +1 -0
  119. package/dist/models/sam/llmclient.d.ts +490 -0
  120. package/dist/models/sam/llmclient.d.ts.map +1 -0
  121. package/dist/models/sam/llmclient.js +7 -0
  122. package/dist/models/sam/llmclient.js.map +1 -0
  123. package/dist/models/sam/llmhost.d.ts +429 -0
  124. package/dist/models/sam/llmhost.d.ts.map +1 -0
  125. package/dist/models/sam/llmhost.js +7 -0
  126. package/dist/models/sam/llmhost.js.map +1 -0
  127. package/dist/models/sam/llmhostcompute.d.ts +221 -0
  128. package/dist/models/sam/llmhostcompute.d.ts.map +1 -0
  129. package/dist/models/sam/llmhostcompute.js +7 -0
  130. package/dist/models/sam/llmhostcompute.js.map +1 -0
  131. package/dist/models/sam/mcpclient.d.ts +195 -0
  132. package/dist/models/sam/mcpclient.d.ts.map +1 -0
  133. package/dist/models/sam/mcpclient.js +7 -0
  134. package/dist/models/sam/mcpclient.js.map +1 -0
  135. package/dist/models/sam/orchestrator.d.ts +163 -0
  136. package/dist/models/sam/orchestrator.d.ts.map +1 -0
  137. package/dist/models/sam/orchestrator.js +7 -0
  138. package/dist/models/sam/orchestrator.js.map +1 -0
  139. package/dist/models/sam/plugin.d.ts +163 -0
  140. package/dist/models/sam/plugin.d.ts.map +1 -0
  141. package/dist/models/sam/plugin.js +7 -0
  142. package/dist/models/sam/plugin.js.map +1 -0
  143. package/dist/models/sam/prompt.d.ts +116 -0
  144. package/dist/models/sam/prompt.d.ts.map +1 -0
  145. package/dist/models/sam/prompt.js +7 -0
  146. package/dist/models/sam/prompt.js.map +1 -0
  147. package/dist/models/sam/provider.d.ts +195 -0
  148. package/dist/models/sam/provider.d.ts.map +1 -0
  149. package/dist/models/sam/provider.js +7 -0
  150. package/dist/models/sam/provider.js.map +1 -0
  151. package/dist/models/sam/proxy.d.ts +163 -0
  152. package/dist/models/sam/proxy.d.ts.map +1 -0
  153. package/dist/models/sam/proxy.js +7 -0
  154. package/dist/models/sam/proxy.js.map +1 -0
  155. package/dist/models/sam/schemas.d.ts +8 -0
  156. package/dist/models/sam/schemas.d.ts.map +1 -0
  157. package/dist/models/sam/schemas.js +6059 -0
  158. package/dist/models/sam/schemas.js.map +1 -0
  159. package/dist/models/sam/secret.d.ts +123 -0
  160. package/dist/models/sam/secret.d.ts.map +1 -0
  161. package/dist/models/sam/secret.js +7 -0
  162. package/dist/models/sam/secret.js.map +1 -0
  163. package/dist/models/sam/skillplugin.d.ts +181 -0
  164. package/dist/models/sam/skillplugin.d.ts.map +1 -0
  165. package/dist/models/sam/skillplugin.js +7 -0
  166. package/dist/models/sam/skillplugin.js.map +1 -0
  167. package/dist/models/sam/smarterauthtoken.d.ts +115 -0
  168. package/dist/models/sam/smarterauthtoken.d.ts.map +1 -0
  169. package/dist/models/sam/smarterauthtoken.js +7 -0
  170. package/dist/models/sam/smarterauthtoken.js.map +1 -0
  171. package/dist/models/sam/sqlconnection.d.ts +187 -0
  172. package/dist/models/sam/sqlconnection.d.ts.map +1 -0
  173. package/dist/models/sam/sqlconnection.js +7 -0
  174. package/dist/models/sam/sqlconnection.js.map +1 -0
  175. package/dist/models/sam/sqlplugin.d.ts +232 -0
  176. package/dist/models/sam/sqlplugin.d.ts.map +1 -0
  177. package/dist/models/sam/sqlplugin.js +7 -0
  178. package/dist/models/sam/sqlplugin.js.map +1 -0
  179. package/dist/models/sam/user.d.ts +135 -0
  180. package/dist/models/sam/user.d.ts.map +1 -0
  181. package/dist/models/sam/user.js +7 -0
  182. package/dist/models/sam/user.js.map +1 -0
  183. package/dist/models/sam/vectorsearch.d.ts +150 -0
  184. package/dist/models/sam/vectorsearch.d.ts.map +1 -0
  185. package/dist/models/sam/vectorsearch.js +7 -0
  186. package/dist/models/sam/vectorsearch.js.map +1 -0
  187. package/dist/models/sam/vectorstore.d.ts +291 -0
  188. package/dist/models/sam/vectorstore.d.ts.map +1 -0
  189. package/dist/models/sam/vectorstore.js +7 -0
  190. package/dist/models/sam/vectorstore.js.map +1 -0
  191. package/dist/models/sam/websearchplugin.d.ts +229 -0
  192. package/dist/models/sam/websearchplugin.d.ts.map +1 -0
  193. package/dist/models/sam/websearchplugin.js +7 -0
  194. package/dist/models/sam/websearchplugin.js.map +1 -0
  195. package/dist/models/vectorstore.d.ts +165 -0
  196. package/dist/models/vectorstore.d.ts.map +1 -0
  197. package/dist/models/vectorstore.js +90 -0
  198. package/dist/models/vectorstore.js.map +1 -0
  199. package/dist/resources/accounts.d.ts +107 -0
  200. package/dist/resources/accounts.d.ts.map +1 -0
  201. package/dist/resources/accounts.js +84 -0
  202. package/dist/resources/accounts.js.map +1 -0
  203. package/dist/resources/base.d.ts +127 -0
  204. package/dist/resources/base.d.ts.map +1 -0
  205. package/dist/resources/base.js +139 -0
  206. package/dist/resources/base.js.map +1 -0
  207. package/dist/resources/catalog.d.ts +209 -0
  208. package/dist/resources/catalog.d.ts.map +1 -0
  209. package/dist/resources/catalog.js +124 -0
  210. package/dist/resources/catalog.js.map +1 -0
  211. package/dist/resources/guardrails.d.ts +82 -0
  212. package/dist/resources/guardrails.d.ts.map +1 -0
  213. package/dist/resources/guardrails.js +46 -0
  214. package/dist/resources/guardrails.js.map +1 -0
  215. package/dist/resources/history.d.ts +97 -0
  216. package/dist/resources/history.d.ts.map +1 -0
  217. package/dist/resources/history.js +109 -0
  218. package/dist/resources/history.js.map +1 -0
  219. package/dist/resources/llmclients.d.ts +348 -0
  220. package/dist/resources/llmclients.d.ts.map +1 -0
  221. package/dist/resources/llmclients.js +367 -0
  222. package/dist/resources/llmclients.js.map +1 -0
  223. package/dist/resources/llmhosts.d.ts +146 -0
  224. package/dist/resources/llmhosts.d.ts.map +1 -0
  225. package/dist/resources/llmhosts.js +128 -0
  226. package/dist/resources/llmhosts.js.map +1 -0
  227. package/dist/resources/manifests.d.ts +153 -0
  228. package/dist/resources/manifests.d.ts.map +1 -0
  229. package/dist/resources/manifests.js +203 -0
  230. package/dist/resources/manifests.js.map +1 -0
  231. package/dist/resources/mcpclients.d.ts +90 -0
  232. package/dist/resources/mcpclients.d.ts.map +1 -0
  233. package/dist/resources/mcpclients.js +57 -0
  234. package/dist/resources/mcpclients.js.map +1 -0
  235. package/dist/resources/providers.d.ts +33 -0
  236. package/dist/resources/providers.d.ts.map +1 -0
  237. package/dist/resources/providers.js +39 -0
  238. package/dist/resources/providers.js.map +1 -0
  239. package/dist/resources/proxies.d.ts +61 -0
  240. package/dist/resources/proxies.d.ts.map +1 -0
  241. package/dist/resources/proxies.js +57 -0
  242. package/dist/resources/proxies.js.map +1 -0
  243. package/dist/resources/vectorstores.d.ts +194 -0
  244. package/dist/resources/vectorstores.d.ts.map +1 -0
  245. package/dist/resources/vectorstores.js +200 -0
  246. package/dist/resources/vectorstores.js.map +1 -0
  247. package/dist/version.d.ts +3 -0
  248. package/dist/version.d.ts.map +1 -0
  249. package/dist/version.js +5 -0
  250. package/dist/version.js.map +1 -0
  251. package/package.json +81 -4
@@ -0,0 +1,187 @@
1
+ /**
2
+ * The errors of the Smarter Node.js SDK.
3
+ *
4
+ * Every error that the SDK throws is a {@link SmarterError}, so a caller can catch them all with one
5
+ * `instanceof` test. The classes, their names and their hierarchy are the Python SDK's:
6
+ *
7
+ * ```text
8
+ * SmarterError
9
+ * ├── SmarterConfigurationError the client has no api key or base url
10
+ * ├── SmarterValueError an argument is invalid, before any request is sent
11
+ * ├── ResourceNotFoundError a resource was not found, by the api (NotFoundError) or by find()
12
+ * ├── SmarterPromptError a streamed prompt failed
13
+ * └── APIError
14
+ * ├── APIConnectionError the request never got a response
15
+ * │ └── APITimeoutError
16
+ * ├── APIResponseValidationError the response does not match the SDK's schema of it
17
+ * └── APIStatusError the api returned a 4xx or 5xx status
18
+ * ├── BadRequestError 400
19
+ * ├── AuthenticationError 401
20
+ * ├── PermissionDeniedError 403
21
+ * ├── NotFoundError 404, also an instanceof ResourceNotFoundError
22
+ * ├── ConflictError 409
23
+ * ├── UnprocessableEntityError 422
24
+ * ├── RateLimitError 429
25
+ * └── InternalServerError 5xx
26
+ * ```
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * try {
31
+ * await client.manifests.describe("LLMClient", "no_such_client");
32
+ * } catch (err) {
33
+ * if (err instanceof NotFoundError) console.log(err.statusCode, err.message, err.errorClass);
34
+ * else throw err;
35
+ * }
36
+ * ```
37
+ */
38
+ /** The base class of every error that the Smarter SDK throws. */
39
+ export class SmarterError extends Error {
40
+ name = "SmarterError";
41
+ }
42
+ /** The client is missing configuration that it needs, e.g. an api key or a base url. */
43
+ export class SmarterConfigurationError extends SmarterError {
44
+ name = "SmarterConfigurationError";
45
+ }
46
+ /** An argument is invalid. Thrown before a request is sent. */
47
+ export class SmarterValueError extends SmarterError {
48
+ name = "SmarterValueError";
49
+ }
50
+ /**
51
+ * A resource was not found: the api answered 404 ({@link NotFoundError}), or `find()` found no resource of that name.
52
+ *
53
+ * `err instanceof ResourceNotFoundError` is also true for a {@link NotFoundError}, as in the Python SDK,
54
+ * where `NotFoundError` subclasses both.
55
+ */
56
+ export class ResourceNotFoundError extends SmarterError {
57
+ name = "ResourceNotFoundError";
58
+ static [Symbol.hasInstance](value) {
59
+ if (Function.prototype[Symbol.hasInstance].call(this, value))
60
+ return true;
61
+ return this === ResourceNotFoundError && value instanceof NotFoundError;
62
+ }
63
+ }
64
+ /** A request to the Smarter api failed. */
65
+ export class APIError extends SmarterError {
66
+ name = "APIError";
67
+ /** The request that failed. */
68
+ request;
69
+ /** The response's parsed JSON body, or its text, or `undefined` when there is no response. */
70
+ body;
71
+ /**
72
+ * @param message - A human-readable description of the error.
73
+ * @param options - The request, the body, and the cause.
74
+ */
75
+ constructor(message, options) {
76
+ super(message, options.cause === undefined ? undefined : { cause: options.cause });
77
+ this.request = options.request;
78
+ this.body = options.body;
79
+ }
80
+ }
81
+ /** The request did not get a response, e.g. the server is unreachable. */
82
+ export class APIConnectionError extends APIError {
83
+ name = "APIConnectionError";
84
+ }
85
+ /** The request timed out. */
86
+ export class APITimeoutError extends APIConnectionError {
87
+ name = "APITimeoutError";
88
+ /**
89
+ * @param request - The request that timed out.
90
+ * @param cause - The abort that ended it.
91
+ */
92
+ constructor(request, cause) {
93
+ super("Request timed out.", { request, cause });
94
+ }
95
+ }
96
+ /**
97
+ * The api's response does not match the SDK's schema of it.
98
+ *
99
+ * This usually means that the SDK and the Smarter platform are of incompatible versions.
100
+ */
101
+ export class APIResponseValidationError extends APIError {
102
+ name = "APIResponseValidationError";
103
+ /** The response's http status. */
104
+ statusCode;
105
+ /**
106
+ * @param message - A description of the mismatch.
107
+ * @param options - The request, the response's status and body, and the validation error.
108
+ */
109
+ constructor(message, options) {
110
+ super(message, options);
111
+ this.statusCode = options.statusCode;
112
+ }
113
+ }
114
+ /** The api returned a 4xx or 5xx status. */
115
+ export class APIStatusError extends APIError {
116
+ name = "APIStatusError";
117
+ /** The response's http status. */
118
+ statusCode;
119
+ /** The response's headers. */
120
+ headers;
121
+ /** The Smarter Journal error body, when the response is a journaled error. */
122
+ error;
123
+ /**
124
+ * @param message - The error's description, from the response when it has one.
125
+ * @param options - The request, the response's status, headers and body, and its journal error.
126
+ */
127
+ constructor(message, options) {
128
+ super(message, options);
129
+ this.statusCode = options.statusCode;
130
+ this.headers = options.headers;
131
+ this.error = options.error;
132
+ }
133
+ /** The name of the server-side exception class, e.g. `SAMBrokerErrorNotFound`, when the platform sent it. */
134
+ get errorClass() {
135
+ return this.error?.errorClass;
136
+ }
137
+ toString() {
138
+ return `${this.name}: Error code: ${String(this.statusCode)} - ${this.message}`;
139
+ }
140
+ }
141
+ /** 400: the request is invalid, e.g. a malformed manifest. */
142
+ export class BadRequestError extends APIStatusError {
143
+ name = "BadRequestError";
144
+ }
145
+ /** 401: the api key is missing, invalid, or inactive. */
146
+ export class AuthenticationError extends APIStatusError {
147
+ name = "AuthenticationError";
148
+ }
149
+ /** 403: the api key's user may not perform the operation. */
150
+ export class PermissionDeniedError extends APIStatusError {
151
+ name = "PermissionDeniedError";
152
+ }
153
+ /** 404: the resource does not exist, or the api key's user may not read it. Also an `instanceof` {@link ResourceNotFoundError}. */
154
+ export class NotFoundError extends APIStatusError {
155
+ name = "NotFoundError";
156
+ }
157
+ /** 409: the request conflicts with the resource's current state. */
158
+ export class ConflictError extends APIStatusError {
159
+ name = "ConflictError";
160
+ }
161
+ /** 422: the request is well-formed but cannot be processed. */
162
+ export class UnprocessableEntityError extends APIStatusError {
163
+ name = "UnprocessableEntityError";
164
+ }
165
+ /** 429: too many requests. */
166
+ export class RateLimitError extends APIStatusError {
167
+ name = "RateLimitError";
168
+ }
169
+ /** 5xx: the platform failed to process the request. */
170
+ export class InternalServerError extends APIStatusError {
171
+ name = "InternalServerError";
172
+ }
173
+ /** A streamed prompt failed: its `result` event carried an error response. */
174
+ export class SmarterPromptError extends SmarterError {
175
+ name = "SmarterPromptError";
176
+ /** The error response. */
177
+ response;
178
+ /**
179
+ * @param message - The error's description.
180
+ * @param response - The error response.
181
+ */
182
+ constructor(message, response) {
183
+ super(message);
184
+ this.response = response;
185
+ }
186
+ }
187
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/core/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AAaH,iEAAiE;AACjE,MAAM,OAAO,YAAa,SAAQ,KAAK;IACnB,IAAI,GAAW,cAAc,CAAC;CACjD;AAED,wFAAwF;AACxF,MAAM,OAAO,yBAA0B,SAAQ,YAAY;IACvC,IAAI,GAAW,2BAA2B,CAAC;CAC9D;AAED,+DAA+D;AAC/D,MAAM,OAAO,iBAAkB,SAAQ,YAAY;IAC/B,IAAI,GAAW,mBAAmB,CAAC;CACtD;AAED;;;;;GAKG;AACH,MAAM,OAAO,qBAAsB,SAAQ,YAAY;IACnC,IAAI,GAAW,uBAAuB,CAAC;IAEzD,MAAM,CAAU,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,KAAc;QACjD,IAAI,QAAQ,CAAC,SAAS,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC;YAAE,OAAO,IAAI,CAAC;QAC1E,OAAO,IAAI,KAAK,qBAAqB,IAAI,KAAK,YAAY,aAAa,CAAC;IAC1E,CAAC;CACF;AAUD,2CAA2C;AAC3C,MAAM,OAAO,QAAS,SAAQ,YAAY;IACtB,IAAI,GAAW,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MAAM,OAAO,kBAAmB,SAAQ,YAAY;IAChC,IAAI,GAAW,oBAAoB,CAAC;IACtD,0BAA0B;IACjB,QAAQ,CAA2D;IAE5E;;;OAGG;IACH,YAAY,OAAe,EAAE,QAAkE;QAC7F,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;CACF"}
@@ -0,0 +1,207 @@
1
+ /**
2
+ * The SDK's http transport: authentication, retries, error mapping, and response validation.
3
+ *
4
+ * {@link SmarterHttpClient} sends every request of a `Smarter` client with `fetch`. It adds the api
5
+ * key and the SDK's headers, retries requests that failed for transient reasons, throws an
6
+ * {@link APIStatusError} for a 4xx or 5xx response, and validates the response's JSON with a Zod
7
+ * schema. Its retry rules are the Python SDK's.
8
+ */
9
+ import { z } from "zod";
10
+ import { SAMJournalApiErrorBody } from "../models/journal.ts";
11
+ import type { ClientConfig } from "./config.ts";
12
+ import { APIStatusError, type FailedRequest } from "./errors.ts";
13
+ import { type Logger } from "./log.ts";
14
+ import { type ServerSentEvent } from "./sse.ts";
15
+ /** A query parameter's value. An array repeats the parameter once per item; `null` and `undefined` leave it out. */
16
+ export type QueryParamValue = string | number | boolean | readonly (string | number | boolean)[] | null | undefined;
17
+ /** The SDK's `User-Agent`. */
18
+ export declare const USER_AGENT = "smarter-nodejs/1.0.2-alpha.1";
19
+ /** The SDK's `X-Smarter-Client` header, which identifies it to the platform's logs and journal. */
20
+ export declare const CLIENT_NAME = "smarter-nodejs";
21
+ /** Statuses that mean the request may succeed if it is sent again. 503 is retried only when the platform did not send it: see {@link isRetryable}. */
22
+ export declare const RETRYABLE_STATUSES: ReadonlySet<number>;
23
+ /** Seconds. A longer `Retry-After` is not waited for. */
24
+ export declare const MAX_RETRY_AFTER = 60;
25
+ /** The `fetch` function that the client sends requests with. */
26
+ export type Fetch = (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
27
+ /** Per-request options, which every method of the SDK takes as its last argument. */
28
+ export interface RequestOptions {
29
+ /** A signal to cancel the request. The request then rejects with the signal's reason. */
30
+ signal?: AbortSignal | undefined;
31
+ /** The timeout in seconds, instead of the client's. For a stream, it applies to each read. */
32
+ timeout?: number | undefined;
33
+ /** Headers for this request, which override the client's. */
34
+ headers?: Record<string, string> | undefined;
35
+ }
36
+ /** What a request sends. */
37
+ export interface RequestInput extends RequestOptions {
38
+ /** Query parameters. */
39
+ params?: Record<string, QueryParamValue> | undefined;
40
+ /** A JSON body. */
41
+ json?: unknown;
42
+ /** A raw or multipart body, e.g. a `FormData` of files. */
43
+ body?: string | FormData | undefined;
44
+ }
45
+ /**
46
+ * A response's body: its JSON, or its text when it is not JSON.
47
+ *
48
+ * @param text - The body's text.
49
+ * @returns The parsed JSON, the text, or undefined for an empty body.
50
+ */
51
+ export declare function parseBody(text: string): unknown;
52
+ /**
53
+ * The Smarter Journal error of a response body, if it is a journaled error response.
54
+ *
55
+ * @param body - A parsed response body.
56
+ * @returns The error body, or undefined.
57
+ */
58
+ export declare function journalError(body: unknown): SAMJournalApiErrorBody | undefined;
59
+ /**
60
+ * A human-readable message for an error response.
61
+ *
62
+ * The platform's views return errors in several shapes: the journal's `error` object, an `error`
63
+ * string, Django REST framework's `detail`, plain text, or a Django error page.
64
+ *
65
+ * @param status - The response's status.
66
+ * @param statusText - The response's status text.
67
+ * @param body - Its parsed body.
68
+ * @param error - Its journal error, if any.
69
+ * @returns The message.
70
+ */
71
+ export declare function errorMessage(status: number, statusText: string, body: unknown, error: SAMJournalApiErrorBody | undefined): string;
72
+ /**
73
+ * The error for a 4xx or 5xx response, by its status.
74
+ *
75
+ * @param request - The request.
76
+ * @param response - The response.
77
+ * @param text - The response's body.
78
+ * @returns e.g. a {@link NotFoundError} for a 404.
79
+ */
80
+ export declare function makeStatusError(request: FailedRequest, response: Response, text: string): APIStatusError;
81
+ /**
82
+ * True if a failed response may succeed when the request is sent again.
83
+ *
84
+ * A 503 is retried only when it is not a journaled response. The Smarter platform itself answers
85
+ * 503 for a resource that is not ready (e.g. `SAMBrokerErrorNotReady`), which a retry won't change,
86
+ * whereas a 503 from a load balancer or proxy means the platform is briefly unavailable.
87
+ *
88
+ * @param status - The response's status.
89
+ * @param text - The response's body.
90
+ * @returns True to retry.
91
+ */
92
+ export declare function isRetryable(status: number, text: string): boolean;
93
+ /**
94
+ * Seconds to wait before a retry: the response's `Retry-After`, or exponential backoff with jitter.
95
+ *
96
+ * @param attempt - The number of the attempt that failed, from 0.
97
+ * @param headers - The failed response's headers, if there was one.
98
+ * @returns The delay.
99
+ */
100
+ export declare function retryDelay(attempt: number, headers?: Headers): number;
101
+ /**
102
+ * Query parameters as strings: booleans as `true` or `false`, an array as the parameter repeated once
103
+ * per item, and null values left out.
104
+ *
105
+ * @param params - The parameters.
106
+ * @returns The parameters' query string, without its `?`, or an empty string.
107
+ */
108
+ export declare function queryString(params: Record<string, QueryParamValue> | undefined): string;
109
+ /** Options of a {@link SmarterHttpClient}. */
110
+ export interface SmarterHttpClientOptions {
111
+ /** Headers to add to every request. */
112
+ defaultHeaders?: Record<string, string> | undefined;
113
+ /** The `fetch` to send requests with, e.g. one with a proxy. The global `fetch` by default. */
114
+ fetch?: Fetch | undefined;
115
+ /** The client's logger. */
116
+ logger: Logger;
117
+ }
118
+ /** Sends the requests of a Smarter client. */
119
+ export declare class SmarterHttpClient {
120
+ #private;
121
+ /** The client's resolved configuration. */
122
+ readonly config: ClientConfig;
123
+ /**
124
+ * @param config - The client's resolved configuration.
125
+ * @param options - Default headers, a `fetch`, and the logger.
126
+ */
127
+ constructor(config: ClientConfig, options: SmarterHttpClientOptions);
128
+ /** The api's base url, e.g. `https://platform.smarter.sh/api/v1/`. */
129
+ get baseUrl(): string;
130
+ /** How many times a failed request is retried. */
131
+ get maxRetries(): number;
132
+ /**
133
+ * The absolute url of an api path.
134
+ *
135
+ * @param path - A path relative to the base url, e.g. `cli/whoami/`. An absolute url is returned as is.
136
+ * @param params - Query parameters.
137
+ * @returns The url.
138
+ */
139
+ url(path: string, params?: Record<string, QueryParamValue>): string;
140
+ /**
141
+ * The headers of a request.
142
+ *
143
+ * @param extra - Headers for this request, which override the defaults.
144
+ * @returns The headers.
145
+ */
146
+ headers(extra?: Record<string, string>): Record<string, string>;
147
+ /**
148
+ * Send a request, with retries, and throw for an error status.
149
+ *
150
+ * @param method - The http method.
151
+ * @param path - The path, relative to the api's base url.
152
+ * @param input - What to send.
153
+ * @param stream - True to return as soon as the headers arrive, with the timeout applied to each read instead.
154
+ * @returns The successful response, and the controller whose abort ends a stream.
155
+ * @throws {@link APIConnectionError} If no response was received after the retries.
156
+ * @throws {@link APITimeoutError} If the request timed out after the retries.
157
+ * @throws {@link APIStatusError} For a 4xx or 5xx response, as the status's subclass.
158
+ */
159
+ send(method: string, path: string, input?: RequestInput, stream?: boolean): Promise<{
160
+ response: Response;
161
+ request: FailedRequest;
162
+ idle: IdleTimeout;
163
+ }>;
164
+ /**
165
+ * Send a request, and validate its JSON response.
166
+ *
167
+ * @param method - The http method, e.g. `GET`.
168
+ * @param path - The path, relative to the api's base url, e.g. `guardrails/`.
169
+ * @param schema - The schema of the response's JSON, e.g. `Guardrail` or `z.array(Guardrail)`.
170
+ * @param input - What to send.
171
+ * @returns The validated response.
172
+ * @throws {@link APIResponseValidationError} If the response does not match the schema.
173
+ */
174
+ request<S extends z.ZodType>(method: string, path: string, schema: S, input?: RequestInput): Promise<z.output<S>>;
175
+ /**
176
+ * Send a request that asks for Server-Sent Events, and iterate its events.
177
+ *
178
+ * The request is sent when the iteration starts, and the response is closed when it ends, also
179
+ * when the caller stops early.
180
+ *
181
+ * @param method - The http method.
182
+ * @param path - The path, relative to the api's base url.
183
+ * @param input - What to send. Its timeout applies to each read.
184
+ * @returns The events.
185
+ */
186
+ events(method: string, path: string, input?: RequestInput): AsyncGenerator<ServerSentEvent>;
187
+ }
188
+ /**
189
+ * A timeout that can be restarted, e.g. on each read of a stream, combined with the caller's signal.
190
+ */
191
+ export declare class IdleTimeout {
192
+ #private;
193
+ /** The signal to pass to `fetch`: aborted by the timeout or by the caller's signal. */
194
+ readonly signal: AbortSignal;
195
+ /**
196
+ * @param seconds - The timeout.
197
+ * @param signal - The caller's signal, if any.
198
+ */
199
+ constructor(seconds: number, signal: AbortSignal | undefined);
200
+ /** True once the timeout, not the caller, aborted the request. */
201
+ get timedOut(): boolean;
202
+ /** Start the timeout again. */
203
+ restart(): void;
204
+ /** Stop the timeout. */
205
+ clear(): void;
206
+ }
207
+ //# sourceMappingURL=http.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/core/http.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,OAAO,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAC;AAE9D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EAGL,cAAc,EAWd,KAAK,aAAa,EACnB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAc,KAAK,MAAM,EAAE,MAAM,UAAU,CAAC;AACnD,OAAO,EAAiD,KAAK,eAAe,EAAE,MAAM,UAAU,CAAC;AAE/F,oHAAoH;AACpH,MAAM,MAAM,eAAe,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,CAAC,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,EAAE,GAAG,IAAI,GAAG,SAAS,CAAC;AAEpH,8BAA8B;AAC9B,eAAO,MAAM,UAAU,iCAA8B,CAAC;AAEtD,mGAAmG;AACnG,eAAO,MAAM,WAAW,mBAAmB,CAAC;AAE5C,sJAAsJ;AACtJ,eAAO,MAAM,kBAAkB,EAAE,WAAW,CAAC,MAAM,CAAiC,CAAC;AAErF,yDAAyD;AACzD,eAAO,MAAM,eAAe,KAAK,CAAC;AAElC,gEAAgE;AAChE,MAAM,MAAM,KAAK,GAAG,CAAC,KAAK,EAAE,MAAM,GA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