@tstdl/base 0.92.145 → 0.92.148

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 (260) hide show
  1. package/ai/ai-file.service.d.ts +29 -1
  2. package/ai/ai-file.service.js +66 -23
  3. package/ai/ai-session.d.ts +28 -1
  4. package/ai/ai-session.js +27 -0
  5. package/ai/ai.service.d.ts +89 -5
  6. package/ai/ai.service.js +130 -27
  7. package/ai/functions.d.ts +7 -1
  8. package/ai/functions.js +7 -1
  9. package/ai/module.d.ts +8 -0
  10. package/ai/module.js +4 -0
  11. package/ai/types.d.ts +115 -2
  12. package/ai/types.js +16 -0
  13. package/api/client/client.d.ts +3 -1
  14. package/api/client/client.js +5 -2
  15. package/api/default-error-handlers.d.ts +1 -1
  16. package/api/index.d.ts +1 -9
  17. package/api/index.js +1 -9
  18. package/api/response.d.ts +1 -1
  19. package/api/server/api-controller.d.ts +1 -1
  20. package/api/server/error-handler.d.ts +1 -1
  21. package/api/server/gateway.d.ts +1 -5
  22. package/api/server/gateway.js +0 -4
  23. package/api/server/middlewares/catch-error.middleware.d.ts +1 -1
  24. package/api/server/module.d.ts +1 -1
  25. package/api/types.d.ts +1 -1
  26. package/api/utils.d.ts +1 -0
  27. package/api/utils.js +1 -0
  28. package/application/application.d.ts +1 -1
  29. package/authentication/authentication.api.d.ts +36 -1
  30. package/authentication/authentication.api.js +28 -0
  31. package/authentication/client/api.client.d.ts +14 -1
  32. package/authentication/client/api.client.js +13 -0
  33. package/authentication/client/authentication.service.d.ts +104 -1
  34. package/authentication/client/authentication.service.js +103 -0
  35. package/authentication/client/http-client.middleware.d.ts +5 -0
  36. package/authentication/client/http-client.middleware.js +6 -2
  37. package/authentication/client/module.d.ts +20 -1
  38. package/authentication/client/module.js +6 -1
  39. package/authentication/client/tokens.d.ts +6 -0
  40. package/authentication/client/tokens.js +6 -0
  41. package/authentication/models/authentication-credentials.model.d.ts +6 -0
  42. package/authentication/models/authentication-credentials.model.js +6 -0
  43. package/authentication/models/authentication-session.model.d.ts +6 -0
  44. package/authentication/models/authentication-session.model.js +6 -0
  45. package/authentication/models/init-secret-reset-data.model.d.ts +10 -1
  46. package/authentication/models/init-secret-reset-data.model.js +10 -1
  47. package/authentication/models/token-payload-base.model.d.ts +24 -4
  48. package/authentication/models/token-payload-base.model.js +24 -4
  49. package/authentication/models/token.model.d.ts +33 -2
  50. package/authentication/server/authentication-ancillary.service.d.ts +27 -4
  51. package/authentication/server/authentication-ancillary.service.js +7 -0
  52. package/authentication/server/authentication-api-request-token.provider.d.ts +3 -0
  53. package/authentication/server/authentication-api-request-token.provider.js +3 -0
  54. package/authentication/server/authentication-secret-requirements.validator.d.ts +37 -0
  55. package/authentication/server/authentication-secret-requirements.validator.js +22 -0
  56. package/authentication/server/authentication.api-controller.d.ts +66 -1
  57. package/authentication/server/authentication.api-controller.js +65 -0
  58. package/authentication/server/authentication.service.d.ts +191 -11
  59. package/authentication/server/authentication.service.js +157 -8
  60. package/authentication/server/helper.d.ts +44 -5
  61. package/authentication/server/helper.js +43 -4
  62. package/authentication/server/module.d.ts +23 -1
  63. package/authentication/server/module.js +23 -1
  64. package/browser/browser-context-controller.d.ts +1 -1
  65. package/browser/browser-controller.d.ts +1 -1
  66. package/browser/browser-controller.js +1 -1
  67. package/browser/element-controller.d.ts +1 -1
  68. package/browser/locator-controller.d.ts +1 -1
  69. package/context/context.d.ts +1 -1
  70. package/data-structures/context-data-map.d.ts +1 -1
  71. package/database/mongo/mongo-base.repository.d.ts +1 -1
  72. package/database/mongo/types.d.ts +1 -1
  73. package/database/query.d.ts +1 -1
  74. package/document-management/api/document-management.api.d.ts +13 -0
  75. package/document-management/api/document-management.api.js +8 -0
  76. package/document-management/models/document-management-table.d.ts +1 -1
  77. package/document-management/models/document-validation-definition.model.d.ts +1 -1
  78. package/document-management/server/schemas.d.ts +1 -1
  79. package/document-management/server/services/document-collection.service.d.ts +1 -1
  80. package/document-management/server/services/document-management-observation.service.d.ts +1 -1
  81. package/document-management/server/services/document-management.service.d.ts +1 -1
  82. package/document-management/server/services/document-property.service.d.ts +3 -3
  83. package/document-management/server/services/document-request.service.d.ts +1 -1
  84. package/document-management/server/services/document-validation.service.d.ts +1 -1
  85. package/document-management/server/services/document-workflow.service.d.ts +1 -1
  86. package/document-management/server/services/document.service.d.ts +1 -1
  87. package/document-management/server/services/singleton.d.ts +1 -1
  88. package/document-management/service-models/document-management.view-model.d.ts +1 -1
  89. package/document-management/service-models/enriched/enriched-document-assignment.view.d.ts +1 -1
  90. package/document-management/service-models/enriched/enriched-document-category.view.d.ts +1 -1
  91. package/document-management/service-models/enriched/enriched-document-collection.view.d.ts +1 -1
  92. package/document-management/service-models/enriched/enriched-document-request.view.d.ts +1 -1
  93. package/document-management/service-models/enriched/enriched-document-type.view.d.ts +1 -1
  94. package/document-management/service-models/enriched/enriched-document.view.d.ts +1 -1
  95. package/document-management/service-models/enriched/enriched-requests-template-data.model.d.ts +1 -1
  96. package/dom/file-select-dialog.d.ts +1 -1
  97. package/enumeration/enumeration.d.ts +1 -1
  98. package/errors/custom.error.d.ts +3 -0
  99. package/errors/custom.error.js +0 -1
  100. package/errors/errors.localization.d.ts +1 -1
  101. package/errors/not-supported.error.d.ts +1 -1
  102. package/{formats.js → formats/formats.js} +3 -3
  103. package/formats/index.d.ts +1 -0
  104. package/formats/index.js +1 -0
  105. package/http/client/http-client-request.d.ts +1 -1
  106. package/http/client/http-client-request.js +1 -1
  107. package/http/client/http-client-response.d.ts +1 -1
  108. package/http/client/http-client.d.ts +1 -1
  109. package/http/client/module.d.ts +1 -1
  110. package/http/http-body.d.ts +1 -1
  111. package/http/http-value-map.d.ts +1 -1
  112. package/http/http.error.d.ts +1 -1
  113. package/http/index.d.ts +1 -0
  114. package/http/index.js +1 -0
  115. package/http/server/http-server-request.d.ts +1 -1
  116. package/http/server/http-server-response.d.ts +1 -1
  117. package/http/tokens.d.ts +5 -0
  118. package/http/tokens.js +5 -0
  119. package/http/types.d.ts +1 -1
  120. package/http/utils.d.ts +1 -1
  121. package/injector/decorators.d.ts +1 -1
  122. package/injector/index.d.ts +1 -1
  123. package/injector/index.js +1 -1
  124. package/injector/injector.d.ts +10 -1
  125. package/injector/injector.js +6 -0
  126. package/injector/interfaces.d.ts +1 -1
  127. package/injector/provider.d.ts +1 -1
  128. package/injector/resolution.d.ts +10 -5
  129. package/injector/resolve-chain.d.ts +2 -2
  130. package/injector/resolve-chain.js +1 -1
  131. package/injector/resolve.error.js +1 -1
  132. package/injector/token.d.ts +8 -1
  133. package/injector/token.js +7 -0
  134. package/injector/types.d.ts +1 -1
  135. package/key-value-store/key-value-store.provider.d.ts +2 -2
  136. package/key-value-store/key-value.store.d.ts +2 -2
  137. package/key-value-store/mongo/mongo-key-value-store.provider.d.ts +1 -1
  138. package/key-value-store/mongo/mongo-key-value.store.d.ts +1 -1
  139. package/key-value-store/postgres/key-value-store.service.d.ts +1 -1
  140. package/mail/mail.service.d.ts +1 -1
  141. package/mail/models/mail-data.model.d.ts +1 -1
  142. package/mail/models/mail-template.model.d.ts +1 -1
  143. package/mail/module.d.ts +1 -1
  144. package/module/index.d.ts +0 -1
  145. package/module/index.js +0 -1
  146. package/module/module-base.d.ts +1 -1
  147. package/module/module-metric-reporter.js +1 -1
  148. package/module/module.d.ts +1 -1
  149. package/module/modules/function.module.js +1 -1
  150. package/object-storage/object.d.ts +1 -1
  151. package/openid-connect/mongo-oidc-state.repository.d.ts +1 -1
  152. package/openid-connect/oidc.service.d.ts +1 -1
  153. package/orm/decorators.d.ts +2 -2
  154. package/orm/entity.d.ts +1 -1
  155. package/orm/index.d.ts +3 -3
  156. package/orm/index.js +3 -3
  157. package/orm/query.d.ts +1 -1
  158. package/orm/repository.types.d.ts +1 -1
  159. package/orm/schemas/json.d.ts +1 -1
  160. package/orm/server/database-schema.d.ts +1 -1
  161. package/orm/server/drizzle/schema-converter.d.ts +1 -1
  162. package/orm/server/repository.d.ts +1 -1
  163. package/orm/server/transaction.d.ts +1 -1
  164. package/orm/server/transactional.d.ts +3 -3
  165. package/orm/server/types.d.ts +1 -1
  166. package/orm/types.d.ts +1 -1
  167. package/package.json +23 -20
  168. package/queue/mongo/job.d.ts +1 -1
  169. package/queue/mongo/queue.js +31 -31
  170. package/queue/postgres/job.model.d.ts +1 -1
  171. package/queue/postgres/queue.d.ts +1 -1
  172. package/queue/postgres/queue.provider.d.ts +1 -1
  173. package/queue/provider.d.ts +1 -1
  174. package/reflection/decorators.d.ts +1 -1
  175. package/reflection/registry.d.ts +1 -1
  176. package/reflection/types.d.ts +1 -1
  177. package/reflection/utils.d.ts +1 -1
  178. package/rpc/model.d.ts +1 -1
  179. package/rxjs-utils/retry-backoff.js +2 -2
  180. package/schema/converters/openapi-converter.d.ts +1 -1
  181. package/schema/decorators/schema.d.ts +1 -1
  182. package/schema/decorators/utils.d.ts +1 -1
  183. package/schema/schema.d.ts +1 -1
  184. package/schema/schema.error.d.ts +1 -1
  185. package/schema/schemas/array.d.ts +1 -1
  186. package/schema/schemas/enumeration.d.ts +1 -1
  187. package/schema/schemas/function.d.ts +1 -1
  188. package/schema/schemas/instance.d.ts +1 -1
  189. package/schema/schemas/nullable.d.ts +1 -1
  190. package/schema/schemas/number.d.ts +1 -1
  191. package/schema/schemas/object.d.ts +1 -1
  192. package/schema/schemas/one-or-many.d.ts +1 -1
  193. package/schema/schemas/optional.d.ts +1 -1
  194. package/schema/schemas/simple.d.ts +1 -1
  195. package/search-index/elastic/model/index-mapping.d.ts +1 -1
  196. package/search-index/elastic/search-index.js +3 -4
  197. package/search-index/memory/memory-search-index.d.ts +1 -1
  198. package/search-index/memory/memory-search-index.js +1 -1
  199. package/serializer/handlers/binary.d.ts +1 -1
  200. package/serializer/serializable.d.ts +1 -1
  201. package/serializer/types.d.ts +1 -1
  202. package/templates/module.d.ts +1 -1
  203. package/templates/renderers/handlebars.template-renderer.d.ts +1 -1
  204. package/templates/renderers/jsx.template-renderer.d.ts +1 -1
  205. package/templates/renderers/mjml.template-renderer.d.ts +1 -1
  206. package/templates/renderers/string.template-renderer.d.ts +1 -1
  207. package/templates/resolvers/file.template-resolver.d.ts +1 -1
  208. package/templates/resolvers/jsx.template-resolver.d.ts +1 -1
  209. package/templates/resolvers/string.template-resolver.d.ts +1 -1
  210. package/templates/template.model.d.ts +1 -1
  211. package/templates/template.renderer.d.ts +1 -1
  212. package/templates/template.service.d.ts +1 -1
  213. package/text/dynamic-text.model.d.ts +1 -1
  214. package/text/localization.service.d.ts +1 -1
  215. package/types/geo-json.d.ts +1 -1
  216. package/types/index.d.ts +2 -0
  217. package/types/tagged.d.ts +1 -1
  218. package/{types.d.ts → types/types.d.ts} +2 -1
  219. package/utils/async-hook/async-hook.d.ts +109 -0
  220. package/utils/async-hook/async-hook.js +77 -3
  221. package/utils/backoff.d.ts +125 -43
  222. package/utils/backoff.js +140 -65
  223. package/utils/base64.d.ts +1 -1
  224. package/utils/base64.js +1 -2
  225. package/utils/binary.d.ts +1 -1
  226. package/utils/comparison.d.ts +5 -5
  227. package/utils/comparison.js +5 -3
  228. package/utils/cryptography.d.ts +1 -1
  229. package/utils/encoding.d.ts +1 -1
  230. package/utils/enum.d.ts +1 -1
  231. package/utils/equals.d.ts +1 -1
  232. package/utils/format-error.d.ts +1 -1
  233. package/utils/function/class.d.ts +1 -1
  234. package/utils/function/memoize.d.ts +1 -1
  235. package/utils/helpers.d.ts +1 -1
  236. package/utils/helpers.js +2 -2
  237. package/utils/jwt.d.ts +3 -3
  238. package/utils/merge.d.ts +1 -1
  239. package/utils/middleware.js +3 -3
  240. package/utils/object/decycle.d.ts +1 -1
  241. package/utils/object/forward-ref.d.ts +1 -1
  242. package/utils/object/lazy-property.d.ts +1 -1
  243. package/utils/object/object.d.ts +1 -1
  244. package/utils/object/property-name.d.ts +1 -1
  245. package/utils/patch-worker.d.ts +1 -1
  246. package/utils/reactive-value-to-signal.d.ts +1 -1
  247. package/utils/reflection.d.ts +1 -1
  248. package/utils/repl.d.ts +1 -1
  249. package/utils/singleton.d.ts +1 -1
  250. package/utils/stream/size-limited-stream.d.ts +1 -1
  251. package/utils/type/extends.d.ts +1 -1
  252. package/utils/type-guards.d.ts +1 -1
  253. package/utils/url-builder.d.ts +1 -1
  254. package/utils/z-base32.d.ts +1 -1
  255. package/module/utils.d.ts +0 -4
  256. package/module/utils.js +0 -21
  257. /package/{formats.d.ts → formats/formats.d.ts} +0 -0
  258. /package/{types.js → types/types.js} +0 -0
  259. /package/{web-types.d.ts → types/web-types.d.ts} +0 -0
  260. /package/{web-types.js → types/web-types.js} +0 -0
package/ai/ai.service.js CHANGED
@@ -7,11 +7,11 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
7
7
  var _a;
8
8
  var AiService_1;
9
9
  import { FinishReason, FunctionCallingConfigMode as GoogleFunctionCallingMode, GoogleGenAI } from '@google/genai';
10
+ import { CancellationSignal } from '../cancellation/index.js';
10
11
  import { NotSupportedError } from '../errors/not-supported.error.js';
11
12
  import { Singleton } from '../injector/decorators.js';
12
13
  import { inject, injectArgument } from '../injector/inject.js';
13
14
  import { Logger } from '../logger/logger.js';
14
- import { getShutdownSignal } from '../process-shutdown.js';
15
15
  import { DeferredPromise } from '../promise/deferred-promise.js';
16
16
  import { LazyPromise } from '../promise/lazy-promise.js';
17
17
  import { convertToOpenApiSchema } from '../schema/converters/openapi-converter.js';
@@ -19,19 +19,28 @@ import { Schema } from '../schema/index.js';
19
19
  import { toArray } from '../utils/array/array.js';
20
20
  import { mapAsync } from '../utils/async-iterable-helpers/map.js';
21
21
  import { toArrayAsync } from '../utils/async-iterable-helpers/to-array.js';
22
+ import { backoffGenerator } from '../utils/backoff.js';
22
23
  import { lazyObject } from '../utils/object/lazy-property.js';
23
24
  import { hasOwnProperty, objectEntries } from '../utils/object/object.js';
24
- import { cancelableTimeout } from '../utils/timing.js';
25
25
  import { assertDefinedPass, isDefined, isError, isNotNull, isNull, isUndefined } from '../utils/type-guards.js';
26
- import { millisecondsPerSecond } from '../utils/units.js';
27
26
  import { resolveValueOrAsyncProvider } from '../utils/value-or-provider.js';
28
27
  import { AiFileService } from './ai-file.service.js';
29
28
  import { AiSession } from './ai-session.js';
30
29
  import { isSchemaFunctionDeclarationWithHandler } from './types.js';
30
+ /**
31
+ * Options for configuring the {@link AiService}.
32
+ */
31
33
  export class AiServiceOptions {
34
+ /** Google AI API key. */
32
35
  apiKey;
36
+ /** Path to the Google Cloud credentials file. */
33
37
  keyFile;
38
+ /** Vertex AI specific options. If provided, the service will use Vertex AI endpoints. */
34
39
  vertex;
40
+ /**
41
+ * The default model to use for generation requests.
42
+ * @default 'gemini-2.5-flash-lite'
43
+ */
35
44
  defaultModel;
36
45
  }
37
46
  ;
@@ -41,10 +50,17 @@ const functionCallingModeMap = {
41
50
  none: GoogleFunctionCallingMode.NONE,
42
51
  };
43
52
  let generationCounter = 0;
53
+ /**
54
+ * A service for interacting with Google's Generative AI models (Gemini).
55
+ *
56
+ * This service provides methods for content generation, function calling, and file processing,
57
+ * supporting both standard Google AI and Vertex AI endpoints.
58
+ */
44
59
  let AiService = AiService_1 = class AiService {
45
60
  #options = injectArgument(this, { optional: true }) ?? inject(AiServiceOptions);
46
61
  #fileService = inject(AiFileService, this.#options);
47
62
  #logger = inject(Logger, AiService_1.name);
63
+ #cancellationSignal = inject(CancellationSignal);
48
64
  #genAI = new GoogleGenAI({
49
65
  vertexai: isDefined(this.#options.vertex?.project),
50
66
  project: this.#options.vertex?.project,
@@ -53,19 +69,56 @@ let AiService = AiService_1 = class AiService {
53
69
  apiKey: isUndefined(this.#options.vertex?.project) ? assertDefinedPass(this.#options.apiKey, 'Api key not defined') : undefined,
54
70
  });
55
71
  #maxOutputTokensCache = new Map();
56
- defaultModel = this.#options.defaultModel ?? 'gemini-2.5-flash-lite-preview-06-17';
72
+ #backoffOptions = {
73
+ cancellationSignal: this.#cancellationSignal,
74
+ strategy: 'exponential',
75
+ initialDelay: 2500,
76
+ increase: 1.5,
77
+ jitter: 0.2,
78
+ maximumDelay: 30000,
79
+ };
80
+ /**
81
+ * The default AI model to use for requests if not specified otherwise.
82
+ */
83
+ defaultModel = this.#options.defaultModel ?? 'gemini-2.5-flash-lite';
84
+ /**
85
+ * Creates a new {@link AiSession} for managing conversational history.
86
+ */
57
87
  createSession() {
58
88
  return new AiSession(this);
59
89
  }
90
+ /**
91
+ * Processes a single file for use in AI requests by uploading it and making it available to the model.
92
+ * @param fileInput The file to process.
93
+ * @returns A promise that resolves to a {@link FileContentPart} which can be included in a generation request.
94
+ */
60
95
  async processFile(fileInput) {
61
96
  return await this.#fileService.processFile(fileInput);
62
97
  }
98
+ /**
99
+ * Processes multiple files in parallel for use in AI requests.
100
+ * @param fileInputs The files to process.
101
+ * @returns A promise that resolves to an array of {@link FileContentPart}s which can be included in generation requests.
102
+ */
63
103
  async processFiles(fileInputs) {
64
104
  return await this.#fileService.processFiles(fileInputs);
65
105
  }
106
+ /**
107
+ * Creates a file content part from a previously processed file ID.
108
+ * This does not re-upload the file.
109
+ * @param id The ID of the file, obtained from {@link AiFileService.processFile} or {@link AiFileService.processFiles}.
110
+ * @returns A {@link FileContentPart} for use in a generation request.
111
+ */
66
112
  getFileById(id) {
67
113
  return { file: id };
68
114
  }
115
+ /**
116
+ * A high-level method to prompt the model to call one or more functions.
117
+ * This method sends the request and parses the model's response to identify function calls.
118
+ * If the function declaration includes a handler, it will be executed automatically.
119
+ * @param options The options for the function call request.
120
+ * @returns A promise that resolves to a {@link SpecializedGenerationResult} containing the function call results.
121
+ */
69
122
  async callFunctions(options) {
70
123
  const generation = await this.generate({
71
124
  model: options.model,
@@ -94,9 +147,15 @@ let AiService = AiService_1 = class AiService {
94
147
  return {
95
148
  result,
96
149
  raw: generation,
97
- getFunctionResultContentParts: () => result.map((result) => result.getFunctionResultContentPart()),
98
150
  };
99
151
  }
152
+ /**
153
+ * A streaming version of `callFunctions`.
154
+ * Yields function call results as they are received from the model.
155
+ * If function declarations include handlers, they are executed as soon as a complete function call is parsed.
156
+ * @param options The options for the function call request.
157
+ * @returns A {@link SpecializedGenerationResultGenerator} that yields function call results.
158
+ */
100
159
  callFunctionsStream(options) {
101
160
  const itemsPromise = new DeferredPromise();
102
161
  const generator = this._callFunctionsStream(options, itemsPromise);
@@ -106,10 +165,22 @@ let AiService = AiService_1 = class AiService {
106
165
  });
107
166
  return generator;
108
167
  }
168
+ /**
169
+ * Generates content from the model based on a request.
170
+ * This method waits for the full response from the model. For streaming, use {@link generateStream}.
171
+ * @param request The generation request.
172
+ * @returns A promise that resolves to the complete {@link GenerationResult}.
173
+ */
109
174
  async generate(request) {
110
175
  const items = await toArrayAsync(this.generateStream(request));
111
176
  return mergeGenerationStreamItems(items, request.generationSchema);
112
177
  }
178
+ /**
179
+ * Generates content as a stream.
180
+ * Yields partial generation results as they are received from the model.
181
+ * @param request The generation request.
182
+ * @returns An `AsyncGenerator` that yields {@link GenerationResult} chunks.
183
+ */
113
184
  async *generateStream(request) {
114
185
  const generationNumber = ++generationCounter;
115
186
  const googleFunctionDeclarations = isDefined(request.functions) ? await this.convertFunctions(request.functions) : undefined;
@@ -141,7 +212,8 @@ let AiService = AiService_1 = class AiService {
141
212
  let totalTokens = 0;
142
213
  while (totalOutputTokens < maxTotalOutputTokens) {
143
214
  let generation;
144
- for (let i = 0;; i++) {
215
+ let triesLeft = 10;
216
+ for await (const backoff of backoffGenerator(this.#backoffOptions)) {
145
217
  try {
146
218
  this.#logger.verbose(`[C:${generationNumber}] [I:${iterations + 1}] Generating...`);
147
219
  generation = await this.#genAI.models.generateContentStream({
@@ -155,14 +227,13 @@ let AiService = AiService_1 = class AiService {
155
227
  break;
156
228
  }
157
229
  catch (error) {
158
- if ((i < 20) && isError(error) && (error.message.includes('429 Too Many Requests') || (error.message.includes('503 Service Unavailable')))) {
159
- this.#logger.verbose('429 Too Many Requests - trying again in 15 seconds');
160
- const timeoutResult = await cancelableTimeout(15 * millisecondsPerSecond, getShutdownSignal());
161
- if (timeoutResult == 'timeout') {
162
- continue;
163
- }
230
+ triesLeft -= 1;
231
+ if ((triesLeft > 0) && isError(error) && (error.message.includes('429 Too Many Requests') || error.message.includes('503 Service Unavailable'))) {
232
+ this.#logger.verbose(`Retrying after transient error: ${error.message}`);
233
+ backoff();
234
+ continue;
164
235
  }
165
- throw error;
236
+ throw error; // Non-retryable error
166
237
  }
167
238
  }
168
239
  iterations++;
@@ -266,7 +337,23 @@ let AiService = AiService_1 = class AiService {
266
337
  return await promise;
267
338
  }
268
339
  async _getModelOutputTokenLimit(model) {
269
- const modelInfo = await this.#genAI.models.get({ model });
340
+ let modelInfo;
341
+ let triesLeft = 10;
342
+ for await (const backoff of backoffGenerator(this.#backoffOptions)) {
343
+ try {
344
+ modelInfo = await this.#genAI.models.get({ model });
345
+ break;
346
+ }
347
+ catch (error) {
348
+ triesLeft -= 1;
349
+ if ((triesLeft > 0) && isError(error) && (error.message.includes('429 Too Many Requests') || error.message.includes('503 Service Unavailable'))) {
350
+ this.#logger.verbose(`Could not get model info for ${model} due to a transient error (${error.message}). Retrying...`);
351
+ backoff();
352
+ continue;
353
+ }
354
+ throw error;
355
+ }
356
+ }
270
357
  if (isUndefined(modelInfo.outputTokenLimit)) {
271
358
  throw new Error(`Model ${model} does not support maxOutputTokens`);
272
359
  }
@@ -345,33 +432,49 @@ AiService = AiService_1 = __decorate([
345
432
  Singleton()
346
433
  ], AiService);
347
434
  export { AiService };
435
+ /**
436
+ * Merges an array of streaming generation results into a single, consolidated result.
437
+ * This is useful for combining the chunks from a streaming response into a final object.
438
+ * @param items The array of {@link GenerationResult} items from a stream.
439
+ * @param schema An optional schema to parse the merged JSON output.
440
+ * @returns A single, merged {@link GenerationResult}.
441
+ */
348
442
  export function mergeGenerationStreamItems(items, schema) {
443
+ if (items.length == 0) {
444
+ return {
445
+ content: { role: 'model', parts: [] },
446
+ text: null,
447
+ json: undefined,
448
+ functionCalls: [],
449
+ finishReason: 'unknown',
450
+ usage: { iterations: 0, prompt: 0, output: 0, total: 0 },
451
+ };
452
+ }
349
453
  const parts = items.flatMap((item) => item.content.parts);
350
- let text;
351
- let functionCallParts;
352
454
  return lazyObject({
353
455
  content: { value: { role: 'model', parts } },
354
456
  text() {
355
- if (isUndefined(text)) {
356
- const textParts = parts.filter((part) => hasOwnProperty(part, 'text')).map((part) => part.text);
357
- text = (textParts.length > 0) ? textParts.join('') : null;
358
- }
359
- return text;
457
+ const textParts = parts.filter((part) => hasOwnProperty(part, 'text')).map((part) => part.text);
458
+ return (textParts.length > 0) ? textParts.join('') : null;
360
459
  },
361
460
  json() {
362
461
  if (isUndefined(schema)) {
363
- return undefined; // eslint-disable-line @typescript-eslint/no-unsafe-return
462
+ return undefined;
364
463
  }
365
464
  if (isNull(this.text)) {
366
465
  throw new Error('No text to parse available.');
367
466
  }
368
- return Schema.parse(schema, JSON.parse(this.text));
467
+ let parsed;
468
+ try {
469
+ parsed = JSON.parse(this.text);
470
+ }
471
+ catch (error) {
472
+ throw new Error(`Failed to parse model output as JSON. Raw text: "${this.text}"`, { cause: error });
473
+ }
474
+ return Schema.parse(schema, parsed);
369
475
  },
370
476
  functionCalls() {
371
- if (isUndefined(functionCallParts)) {
372
- functionCallParts = parts.filter((part) => hasOwnProperty(part, 'functionCall')).map((part) => part.functionCall);
373
- }
374
- return functionCallParts;
477
+ return parts.filter((part) => hasOwnProperty(part, 'functionCall')).map((part) => part.functionCall);
375
478
  },
376
479
  finishReason: { value: items.at(-1).finishReason },
377
480
  usage: { value: items.at(-1).usage },
package/ai/functions.d.ts CHANGED
@@ -1,3 +1,9 @@
1
1
  import type { FunctionDeclaration } from '@google/genai';
2
- import type { AbstractConstructor } from '../types.js';
2
+ import type { AbstractConstructor } from '../types/index.js';
3
+ /**
4
+ * Extracts Google AI function declarations from a class decorated with schema information.
5
+ * It iterates over the properties of the class schema and converts `FunctionSchema` instances
6
+ * into the format required by the Google Generative AI API.
7
+ * @param type The constructor of the class to extract function declarations from.
8
+ */
3
9
  export declare function getFunctionDeclarations(type: AbstractConstructor): FunctionDeclaration[];
package/ai/functions.js CHANGED
@@ -2,6 +2,12 @@ import { convertToOpenApiSchema } from '../schema/converters/openapi-converter.j
2
2
  import { FunctionSchema, getObjectSchema, object } from '../schema/index.js';
3
3
  import { fromEntries, objectEntries } from '../utils/object/object.js';
4
4
  import { isNotNull, isNull, isString } from '../utils/type-guards.js';
5
+ /**
6
+ * Extracts Google AI function declarations from a class decorated with schema information.
7
+ * It iterates over the properties of the class schema and converts `FunctionSchema` instances
8
+ * into the format required by the Google Generative AI API.
9
+ * @param type The constructor of the class to extract function declarations from.
10
+ */
5
11
  export function getFunctionDeclarations(type) {
6
12
  const objectSchema = getObjectSchema(type);
7
13
  return objectEntries(objectSchema.properties)
@@ -14,7 +20,7 @@ export function getFunctionDeclarations(type) {
14
20
  description: schema.description ?? undefined,
15
21
  parameters: isNull(schema.parameterSchemas)
16
22
  ? undefined
17
- : getFunctionDeclarationParameters(schema)
23
+ : getFunctionDeclarationParameters(schema),
18
24
  };
19
25
  })
20
26
  .filter(isNotNull);
package/ai/module.d.ts CHANGED
@@ -1,3 +1,11 @@
1
1
  import { AiServiceOptions } from './ai.service.js';
2
+ /**
3
+ * Options for configuring the AI module.
4
+ * @see {@link AiServiceOptions}
5
+ */
2
6
  export type ApiModuleOptions = AiServiceOptions;
7
+ /**
8
+ * Configures the {@link AiService}.
9
+ * @param options The configuration options for the AI services.
10
+ */
3
11
  export declare function configureAiService(options: ApiModuleOptions): void;
package/ai/module.js CHANGED
@@ -1,5 +1,9 @@
1
1
  import { Injector } from '../injector/injector.js';
2
2
  import { AiServiceOptions } from './ai.service.js';
3
+ /**
4
+ * Configures the {@link AiService}.
5
+ * @param options The configuration options for the AI services.
6
+ */
3
7
  export function configureAiService(options) {
4
8
  Injector.register(AiServiceOptions, { useValue: options });
5
9
  }
package/ai/types.d.ts CHANGED
@@ -1,91 +1,204 @@
1
1
  import type { LiteralUnion } from 'type-fest';
2
2
  import type { ObjectSchema, SchemaOutput, SchemaTestable } from '../schema/index.js';
3
- import type { Record, UndefinableJsonObject } from '../types.js';
3
+ import type { Record, UndefinableJsonObject } from '../types/index.js';
4
4
  import type { ResolvedValueOrProvider, ValueOrAsyncProvider } from '../utils/value-or-provider.js';
5
+ /**
6
+ * Represents a file to be uploaded, either from a local path or as a `Blob`.
7
+ */
5
8
  export type FileInput = {
6
9
  path: string;
7
10
  mimeType: string;
8
11
  } | Blob;
12
+ /**
13
+ * A record of named function declarations, where each key is the function name.
14
+ */
9
15
  export type SchemaFunctionDeclarations = Record<string, SchemaFunctionDeclaration<any>>;
16
+ /**
17
+ * A function declaration that only defines the function's signature (name, description, parameters).
18
+ * @template T The record type for the function's parameters.
19
+ */
10
20
  export type SchemaFunctionDeclarationWithoutHandler<T extends Record = Record> = {
11
21
  description: string;
12
22
  parameters?: ValueOrAsyncProvider<ObjectSchema<T>>;
13
23
  enabled?: ValueOrAsyncProvider<boolean>;
14
24
  };
25
+ /**
26
+ * A function declaration that includes a handler to be executed when the function is called by the model.
27
+ * @template T The record type for the function's parameters.
28
+ * @template R The return type of the handler.
29
+ */
15
30
  export type SchemaFunctionDeclarationWithHandler<T extends Record = Record, R = unknown> = SchemaFunctionDeclarationWithoutHandler<T> & {
16
31
  handler: (parameters: T) => R | Promise<R>;
17
32
  };
33
+ /**
34
+ * A union of a function declaration with or without a handler.
35
+ * @template T The record type for the function's parameters.
36
+ * @template R The return type of the handler if it exists.
37
+ */
18
38
  export type SchemaFunctionDeclaration<T extends Record = Record, R = unknown> = SchemaFunctionDeclarationWithoutHandler<T> | SchemaFunctionDeclarationWithHandler<T, R>;
39
+ /**
40
+ * Extracts the return type of a function declaration's handler.
41
+ * If the declaration has no handler, it resolves to `never`.
42
+ * @template T The function declaration type.
43
+ */
19
44
  export type SchemaFunctionDeclarationHandlerResult<T extends SchemaFunctionDeclaration> = T extends SchemaFunctionDeclarationWithHandler<any, infer R> ? R : never;
45
+ /**
46
+ * Represents the result of a function call, typed based on the provided declarations.
47
+ * This is a distributive conditional type that maps over the function declarations.
48
+ * @template T The schema declarations for the available functions.
49
+ */
20
50
  export type SchemaFunctionDeclarationsResult<T extends SchemaFunctionDeclarations = SchemaFunctionDeclarations> = {
21
51
  [P in keyof T]: {
52
+ /** The name of the function that was called. */
22
53
  functionName: P;
54
+ /** The parameters passed to the function call, parsed against the schema. */
23
55
  parameters: SchemaOutput<NonNullable<ResolvedValueOrProvider<T[P]['parameters']>>>;
56
+ /** The result of executing the handler, if one was provided. */
24
57
  handlerResult: SchemaFunctionDeclarationHandlerResult<T[P]>;
58
+ /** A function to get the content part representing the function result, for use in subsequent AI requests. */
25
59
  getFunctionResultContentPart: () => FunctionResultContentPart;
26
60
  };
27
61
  }[keyof T];
62
+ /**
63
+ * The role of the author of a piece of content.
64
+ */
28
65
  export type ContentRole = 'user' | 'model';
66
+ /** A part of a `Content` object containing text. */
29
67
  export type TextContentPart = {
30
68
  text: string;
31
69
  };
70
+ /** A part of a `Content` object referencing a processed file via its ID. */
32
71
  export type FileContentPart = {
33
72
  file: string;
34
73
  };
74
+ /** Represents a function call requested by the model. */
35
75
  export type FunctionCall = {
36
76
  name: string;
37
77
  parameters: UndefinableJsonObject;
38
78
  };
79
+ /** A part of a `Content` object representing a function call request from the model. */
39
80
  export type FunctionCallContentPart = {
40
81
  functionCall: FunctionCall;
41
82
  };
83
+ /** The result of a function execution. */
42
84
  export type FunctionResult = {
43
85
  name: string;
44
86
  value: UndefinableJsonObject;
45
87
  };
88
+ /** A part of a `Content` object representing the result of a function call. */
46
89
  export type FunctionResultContentPart = {
47
90
  functionResult: FunctionResult;
48
91
  };
92
+ /** A union of all possible content part types. */
49
93
  export type ContentPart = TextContentPart | FileContentPart | FunctionCallContentPart | FunctionResultContentPart;
94
+ /** A single message in a conversation, containing a role and one or more parts. */
50
95
  export type Content = {
51
96
  role: ContentRole;
52
97
  parts: readonly ContentPart[];
53
98
  };
99
+ /**
100
+ * Specifies the mode for function calling.
101
+ * - `auto`: The model decides whether to call a function.
102
+ * - `force`: The model is forced to call a function.
103
+ * - `none`: The model will not call any functions.
104
+ */
54
105
  export type FunctionCallingMode = 'auto' | 'force' | 'none';
106
+ /**
107
+ * The reason why the model stopped generating content.
108
+ */
55
109
  export type FinishReason = 'stop' | 'maxTokens' | 'unknown';
56
- export type AiModel = LiteralUnion<'gemini-2.5-pro' | 'gemini-2.5-flash' | 'gemini-2.5-flash-lite-preview-06-17', string>;
110
+ /**
111
+ * The specific AI model to use for a request.
112
+ */
113
+ export type AiModel = LiteralUnion<'gemini-2.5-pro' | 'gemini-2.5-flash' | 'gemini-2.5-flash-lite', string>;
114
+ /**
115
+ * Options to control the generation process.
116
+ */
57
117
  export type GenerationOptions = {
118
+ /** The maximum number of tokens to generate in the response. */
58
119
  maxOutputTokens?: number;
120
+ /** Controls the randomness of the output. Higher values (closer to 1.0) make the output more random. */
59
121
  temperature?: number;
122
+ /** The cumulative probability of tokens to consider for sampling. */
60
123
  topP?: number;
124
+ /** The number of highest-probability tokens to consider for sampling. */
61
125
  topK?: number;
126
+ /** A penalty for tokens that have already appeared in the text. */
62
127
  presencePenalty?: number;
128
+ /** A penalty for tokens based on their frequency in the text. */
63
129
  frequencyPenalty?: number;
130
+ /**
131
+ * The maximum number of tokens the model is allowed to generate for its thinking phase.
132
+ */
64
133
  thinkingBudget?: number;
65
134
  };
135
+ /**
136
+ * A request to generate content from the AI model.
137
+ * @template S The expected type of the `json` property in the result if `generationSchema` is provided.
138
+ */
66
139
  export type GenerationRequest<S = unknown> = {
140
+ /** The model to use for the request. If not provided, a default will be used. */
67
141
  model?: AiModel;
142
+ /** A system instruction to guide the model's behavior. */
68
143
  systemInstruction?: string;
144
+ /** The content or conversation history to send to the model. */
69
145
  contents: Content | readonly Content[];
146
+ /** A set of functions the model can call. */
70
147
  functions?: SchemaFunctionDeclarations;
148
+ /** The mode for function calling. */
71
149
  functionCallingMode?: FunctionCallingMode;
150
+ /** A schema to which the model's output should conform. Implies `responseMimeType: 'application/json'`. */
72
151
  generationSchema?: SchemaTestable<S>;
152
+ /** Options to control the generation process. */
73
153
  generationOptions?: GenerationOptions;
74
154
  };
155
+ /**
156
+ * Token usage statistics for a generation request.
157
+ */
75
158
  export type GenerationUsage = {
159
+ /** The number of generation iterations (usually 1, more if `maxOutputTokens` is hit and generation continues). */
76
160
  iterations: number;
161
+ /** The number of tokens in the prompt. */
77
162
  prompt: number;
163
+ /** The number of tokens in the generated output. */
78
164
  output: number;
165
+ /** The total number of tokens used. */
79
166
  total: number;
80
167
  };
168
+ /**
169
+ * The result of a generation request.
170
+ * @template S The expected type of the `json` property if a schema was provided in the request.
171
+ */
81
172
  export type GenerationResult<S = unknown> = {
173
+ /** The content generated by the model. */
82
174
  content: Content;
175
+ /** The complete text generated by the model, or `null` if no text was generated. */
83
176
  text: string | null;
177
+ /** The text parsed as JSON, conforming to the provided schema. `undefined` if no schema was provided. */
84
178
  json: S;
179
+ /** An array of function calls requested by the model. */
85
180
  functionCalls: FunctionCall[];
181
+ /** The reason why the model stopped generating. */
86
182
  finishReason: FinishReason;
183
+ /** Token usage statistics for the request. */
87
184
  usage: GenerationUsage;
88
185
  };
186
+ /**
187
+ * A helper function to define a set of function declarations with strong type inference.
188
+ * @param declarations A record of function declarations.
189
+ * @returns The same record of declarations, with types preserved.
190
+ */
89
191
  export declare function defineFunctions<T extends SchemaFunctionDeclarations>(declarations: T): T;
192
+ /**
193
+ * A helper function to define a single function declaration with strong type inference,
194
+ * particularly for its parameters.
195
+ * @param declaration The function declaration.
196
+ * @returns The same declaration, with types preserved.
197
+ */
90
198
  export declare function defineFunction<P extends Record, T extends SchemaFunctionDeclaration<P>>(declaration: T & Pick<SchemaFunctionDeclaration<P>, 'parameters'>): T;
199
+ /**
200
+ * A type guard to check if a function declaration includes a handler.
201
+ * @param declaration The function declaration to check.
202
+ * @returns `true` if the declaration has a `handler` property, `false` otherwise.
203
+ */
91
204
  export declare function isSchemaFunctionDeclarationWithHandler(declaration: SchemaFunctionDeclaration): declaration is SchemaFunctionDeclarationWithHandler;
package/ai/types.js CHANGED
@@ -1,10 +1,26 @@
1
1
  import { hasOwnProperty } from '../utils/object/object.js';
2
+ /**
3
+ * A helper function to define a set of function declarations with strong type inference.
4
+ * @param declarations A record of function declarations.
5
+ * @returns The same record of declarations, with types preserved.
6
+ */
2
7
  export function defineFunctions(declarations) {
3
8
  return declarations;
4
9
  }
10
+ /**
11
+ * A helper function to define a single function declaration with strong type inference,
12
+ * particularly for its parameters.
13
+ * @param declaration The function declaration.
14
+ * @returns The same declaration, with types preserved.
15
+ */
5
16
  export function defineFunction(declaration) {
6
17
  return declaration;
7
18
  }
19
+ /**
20
+ * A type guard to check if a function declaration includes a handler.
21
+ * @param declaration The function declaration to check.
22
+ * @returns `true` if the declaration has a `handler` property, `false` otherwise.
23
+ */
8
24
  export function isSchemaFunctionDeclarationWithHandler(declaration) {
9
25
  return hasOwnProperty(declaration, 'handler');
10
26
  }
@@ -1,6 +1,7 @@
1
1
  import { HttpClient, type HttpClientOptions } from '../../http/client/index.js';
2
+ import { bustCacheToken } from '../../http/index.js';
2
3
  import { type Resolvable } from '../../injector/interfaces.js';
3
- import type { Type } from '../../types.js';
4
+ import type { Type } from '../../types/index.js';
4
5
  import { type ApiClientImplementation, type ApiDefinition, type ApiEndpointDefinition } from '../types.js';
5
6
  export type ApiClient<T extends ApiDefinition> = Type<ApiClientImplementation<T> & Resolvable<HttpClient | HttpClientOptions>, [httpClientOrOptions?: HttpClient | HttpClientOptions]> & Pick<ApiClientImplementation<T>, 'getEndpointResource' | 'getEndpointUrl'>;
6
7
  export type ClientOptions = {
@@ -13,6 +14,7 @@ export type ClientOptions = {
13
14
  };
14
15
  export type ApiClientHttpRequestContext = {
15
16
  endpoint: ApiEndpointDefinition;
17
+ [bustCacheToken]?: boolean;
16
18
  };
17
19
  export declare const httpClientSymbol: unique symbol;
18
20
  export declare const apiDefinitionSymbol: unique symbol;
@@ -1,5 +1,5 @@
1
1
  import { HttpClient, HttpClientRequest } from '../../http/client/index.js';
2
- import { normalizeSingleHttpValue } from '../../http/types.js';
2
+ import { bustCacheToken, normalizeSingleHttpValue } from '../../http/index.js';
3
3
  import { inject } from '../../injector/inject.js';
4
4
  import { Injector } from '../../injector/injector.js';
5
5
  import { resolveArgumentType } from '../../injector/interfaces.js';
@@ -13,7 +13,7 @@ import { buildUrl } from '../../utils/url-builder.js';
13
13
  import { resolveValueOrProvider } from '../../utils/value-or-provider.js';
14
14
  import { normalizedApiDefinitionEndpointsEntries } from '../types.js';
15
15
  import { getFullApiEndpointResource } from '../utils.js';
16
- export const httpClientSymbol = Symbol('ApiTransport');
16
+ export const httpClientSymbol = Symbol('HttpClient for ApiClient');
17
17
  export const apiDefinitionSymbol = Symbol('ApiDefinition');
18
18
  export const defaultOptions = {};
19
19
  export function setDefaultApiClientOptions(options) {
@@ -74,6 +74,9 @@ export function compileClient(definition, options = defaultOptions) {
74
74
  }
75
75
  return getServerSentEvents(this[httpClientSymbol].options.baseUrl, resource, endpoint, parameters);
76
76
  }
77
+ if (context.endpoint.data?.[bustCacheToken] == true) {
78
+ context[bustCacheToken] = true;
79
+ }
77
80
  const request = new HttpClientRequest({
78
81
  method,
79
82
  url: resource,
@@ -1,5 +1,5 @@
1
1
  import { SchemaError } from '../schema/index.js';
2
- import type { OneOrMany } from '../types.js';
2
+ import type { OneOrMany } from '../types/index.js';
3
3
  export type SerializedSchemaError = {
4
4
  path: string;
5
5
  details?: any;
package/api/index.d.ts CHANGED
@@ -1,13 +1,5 @@
1
1
  /**
2
- * Create and consume HTTP APIs
3
- *
4
- * It has built-in support for validation and error handling.
5
- *
6
- * This module contains types and functions to define and register apis and error handlers.
7
- *
8
- * Consumers of these definitions are [client](api_client.html) and [server](api_server.html).
9
- *
10
- * Examples can be found in `source/examples/api/`.
2
+ * {@include ./README.md}
11
3
  *
12
4
  * @module API
13
5
  */
package/api/index.js CHANGED
@@ -1,13 +1,5 @@
1
1
  /**
2
- * Create and consume HTTP APIs
3
- *
4
- * It has built-in support for validation and error handling.
5
- *
6
- * This module contains types and functions to define and register apis and error handlers.
7
- *
8
- * Consumers of these definitions are [client](api_client.html) and [server](api_server.html).
9
- *
10
- * Examples can be found in `source/examples/api/`.
2
+ * {@include ./README.md}
11
3
  *
12
4
  * @module API
13
5
  */
package/api/response.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { type CustomError, type CustomErrorStatic } from '../errors/index.js';
2
- import type { UndefinableJson } from '../types.js';
2
+ import type { UndefinableJson } from '../types/index.js';
3
3
  export type ErrorHandlerData = undefined | UndefinableJson;
4
4
  export type ErrorSerializer<T extends CustomError, TData extends ErrorHandlerData> = (error: T) => TData;
5
5
  export type ErrorDeserializer<T extends CustomError, TData extends ErrorHandlerData> = (data: TData, responseError: ResponseError) => T;