@nhtio/adk 0.1.0-master-f0aa531d
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +9 -0
- package/README.md +3 -0
- package/batteries/index.d.ts +28 -0
- package/batteries/llm/index.d.ts +11 -0
- package/batteries/llm/openai_chat_completions/adapter.cjs +916 -0
- package/batteries/llm/openai_chat_completions/adapter.cjs.map +1 -0
- package/batteries/llm/openai_chat_completions/adapter.d.ts +101 -0
- package/batteries/llm/openai_chat_completions/adapter.mjs +914 -0
- package/batteries/llm/openai_chat_completions/adapter.mjs.map +1 -0
- package/batteries/llm/openai_chat_completions/exceptions.cjs +89 -0
- package/batteries/llm/openai_chat_completions/exceptions.cjs.map +1 -0
- package/batteries/llm/openai_chat_completions/exceptions.d.ts +97 -0
- package/batteries/llm/openai_chat_completions/exceptions.mjs +81 -0
- package/batteries/llm/openai_chat_completions/exceptions.mjs.map +1 -0
- package/batteries/llm/openai_chat_completions/helpers.cjs +819 -0
- package/batteries/llm/openai_chat_completions/helpers.cjs.map +1 -0
- package/batteries/llm/openai_chat_completions/helpers.d.ts +233 -0
- package/batteries/llm/openai_chat_completions/helpers.mjs +783 -0
- package/batteries/llm/openai_chat_completions/helpers.mjs.map +1 -0
- package/batteries/llm/openai_chat_completions/index.d.ts +27 -0
- package/batteries/llm/openai_chat_completions/types.cjs +1 -0
- package/batteries/llm/openai_chat_completions/types.d.ts +524 -0
- package/batteries/llm/openai_chat_completions/types.mjs +0 -0
- package/batteries/llm/openai_chat_completions/validation.cjs +190 -0
- package/batteries/llm/openai_chat_completions/validation.cjs.map +1 -0
- package/batteries/llm/openai_chat_completions/validation.d.ts +31 -0
- package/batteries/llm/openai_chat_completions/validation.mjs +187 -0
- package/batteries/llm/openai_chat_completions/validation.mjs.map +1 -0
- package/batteries/llm/openai_chat_completions.cjs +51 -0
- package/batteries/llm/openai_chat_completions.mjs +5 -0
- package/batteries/llm/webllm_chat_completions/adapter.cjs +658 -0
- package/batteries/llm/webllm_chat_completions/adapter.cjs.map +1 -0
- package/batteries/llm/webllm_chat_completions/adapter.d.ts +103 -0
- package/batteries/llm/webllm_chat_completions/adapter.mjs +656 -0
- package/batteries/llm/webllm_chat_completions/adapter.mjs.map +1 -0
- package/batteries/llm/webllm_chat_completions/exceptions.cjs +70 -0
- package/batteries/llm/webllm_chat_completions/exceptions.cjs.map +1 -0
- package/batteries/llm/webllm_chat_completions/exceptions.d.ts +74 -0
- package/batteries/llm/webllm_chat_completions/exceptions.mjs +65 -0
- package/batteries/llm/webllm_chat_completions/exceptions.mjs.map +1 -0
- package/batteries/llm/webllm_chat_completions/helpers.cjs +38 -0
- package/batteries/llm/webllm_chat_completions/helpers.d.ts +6 -0
- package/batteries/llm/webllm_chat_completions/helpers.mjs +2 -0
- package/batteries/llm/webllm_chat_completions/index.d.ts +25 -0
- package/batteries/llm/webllm_chat_completions/types.d.ts +31 -0
- package/batteries/llm/webllm_chat_completions/validation.cjs +115 -0
- package/batteries/llm/webllm_chat_completions/validation.cjs.map +1 -0
- package/batteries/llm/webllm_chat_completions/validation.d.ts +8 -0
- package/batteries/llm/webllm_chat_completions/validation.mjs +112 -0
- package/batteries/llm/webllm_chat_completions/validation.mjs.map +1 -0
- package/batteries/llm/webllm_chat_completions.cjs +50 -0
- package/batteries/llm/webllm_chat_completions.mjs +6 -0
- package/batteries/llm.cjs +63 -0
- package/batteries/llm.mjs +10 -0
- package/batteries/storage/flydrive/index.d.ts +167 -0
- package/batteries/storage/flydrive.cjs +249 -0
- package/batteries/storage/flydrive.cjs.map +1 -0
- package/batteries/storage/flydrive.mjs +249 -0
- package/batteries/storage/flydrive.mjs.map +1 -0
- package/batteries/storage/in_memory/index.d.ts +106 -0
- package/batteries/storage/in_memory.cjs +121 -0
- package/batteries/storage/in_memory.cjs.map +1 -0
- package/batteries/storage/in_memory.mjs +119 -0
- package/batteries/storage/in_memory.mjs.map +1 -0
- package/batteries/storage/index.d.ts +18 -0
- package/batteries/storage/opfs/index.d.ts +299 -0
- package/batteries/storage/opfs.cjs +368 -0
- package/batteries/storage/opfs.cjs.map +1 -0
- package/batteries/storage/opfs.mjs +366 -0
- package/batteries/storage/opfs.mjs.map +1 -0
- package/batteries/storage.cjs +4 -0
- package/batteries/storage.mjs +2 -0
- package/batteries/tools/color/index.d.ts +37 -0
- package/batteries/tools/color.cjs +659 -0
- package/batteries/tools/color.cjs.map +1 -0
- package/batteries/tools/color.mjs +655 -0
- package/batteries/tools/color.mjs.map +1 -0
- package/batteries/tools/comparison/index.d.ts +29 -0
- package/batteries/tools/comparison.cjs +171 -0
- package/batteries/tools/comparison.cjs.map +1 -0
- package/batteries/tools/comparison.mjs +168 -0
- package/batteries/tools/comparison.mjs.map +1 -0
- package/batteries/tools/data_structure/index.d.ts +30 -0
- package/batteries/tools/data_structure.cjs +270 -0
- package/batteries/tools/data_structure.cjs.map +1 -0
- package/batteries/tools/data_structure.mjs +267 -0
- package/batteries/tools/data_structure.mjs.map +1 -0
- package/batteries/tools/datetime_extended/index.d.ts +51 -0
- package/batteries/tools/datetime_extended.cjs +309 -0
- package/batteries/tools/datetime_extended.cjs.map +1 -0
- package/batteries/tools/datetime_extended.mjs +302 -0
- package/batteries/tools/datetime_extended.mjs.map +1 -0
- package/batteries/tools/datetime_math/index.d.ts +36 -0
- package/batteries/tools/datetime_math.cjs +175 -0
- package/batteries/tools/datetime_math.cjs.map +1 -0
- package/batteries/tools/datetime_math.mjs +171 -0
- package/batteries/tools/datetime_math.mjs.map +1 -0
- package/batteries/tools/encoding/index.d.ts +36 -0
- package/batteries/tools/encoding.cjs +156 -0
- package/batteries/tools/encoding.cjs.map +1 -0
- package/batteries/tools/encoding.mjs +152 -0
- package/batteries/tools/encoding.mjs.map +1 -0
- package/batteries/tools/formatting/index.d.ts +28 -0
- package/batteries/tools/formatting.cjs +120 -0
- package/batteries/tools/formatting.cjs.map +1 -0
- package/batteries/tools/formatting.mjs +117 -0
- package/batteries/tools/formatting.mjs.map +1 -0
- package/batteries/tools/geo_basics/index.d.ts +33 -0
- package/batteries/tools/geo_basics.cjs +136 -0
- package/batteries/tools/geo_basics.cjs.map +1 -0
- package/batteries/tools/geo_basics.mjs +132 -0
- package/batteries/tools/geo_basics.mjs.map +1 -0
- package/batteries/tools/index.d.ts +32 -0
- package/batteries/tools/math/index.d.ts +37 -0
- package/batteries/tools/math.cjs +136 -0
- package/batteries/tools/math.cjs.map +1 -0
- package/batteries/tools/math.mjs +133 -0
- package/batteries/tools/math.mjs.map +1 -0
- package/batteries/tools/memory/index.d.ts +73 -0
- package/batteries/tools/memory.cjs +193 -0
- package/batteries/tools/memory.cjs.map +1 -0
- package/batteries/tools/memory.mjs +187 -0
- package/batteries/tools/memory.mjs.map +1 -0
- package/batteries/tools/parsing/index.d.ts +47 -0
- package/batteries/tools/parsing.cjs +191 -0
- package/batteries/tools/parsing.cjs.map +1 -0
- package/batteries/tools/parsing.mjs +185 -0
- package/batteries/tools/parsing.mjs.map +1 -0
- package/batteries/tools/retrievables/index.d.ts +81 -0
- package/batteries/tools/retrievables.cjs +215 -0
- package/batteries/tools/retrievables.cjs.map +1 -0
- package/batteries/tools/retrievables.mjs +209 -0
- package/batteries/tools/retrievables.mjs.map +1 -0
- package/batteries/tools/standing_instructions/index.d.ts +64 -0
- package/batteries/tools/standing_instructions.cjs +126 -0
- package/batteries/tools/standing_instructions.cjs.map +1 -0
- package/batteries/tools/standing_instructions.mjs +121 -0
- package/batteries/tools/standing_instructions.mjs.map +1 -0
- package/batteries/tools/statistics/index.d.ts +46 -0
- package/batteries/tools/statistics.cjs +253 -0
- package/batteries/tools/statistics.cjs.map +1 -0
- package/batteries/tools/statistics.mjs +248 -0
- package/batteries/tools/statistics.mjs.map +1 -0
- package/batteries/tools/string_processing/index.d.ts +29 -0
- package/batteries/tools/string_processing.cjs +154 -0
- package/batteries/tools/string_processing.cjs.map +1 -0
- package/batteries/tools/string_processing.mjs +151 -0
- package/batteries/tools/string_processing.mjs.map +1 -0
- package/batteries/tools/structured_data/index.d.ts +34 -0
- package/batteries/tools/structured_data.cjs +189 -0
- package/batteries/tools/structured_data.cjs.map +1 -0
- package/batteries/tools/structured_data.mjs +185 -0
- package/batteries/tools/structured_data.mjs.map +1 -0
- package/batteries/tools/text_analysis/index.d.ts +31 -0
- package/batteries/tools/text_analysis.cjs +120 -0
- package/batteries/tools/text_analysis.cjs.map +1 -0
- package/batteries/tools/text_analysis.mjs +117 -0
- package/batteries/tools/text_analysis.mjs.map +1 -0
- package/batteries/tools/text_comparison/index.d.ts +28 -0
- package/batteries/tools/text_comparison.cjs +96 -0
- package/batteries/tools/text_comparison.cjs.map +1 -0
- package/batteries/tools/text_comparison.mjs +93 -0
- package/batteries/tools/text_comparison.mjs.map +1 -0
- package/batteries/tools/time/index.d.ts +27 -0
- package/batteries/tools/time.cjs +63 -0
- package/batteries/tools/time.cjs.map +1 -0
- package/batteries/tools/time.mjs +60 -0
- package/batteries/tools/time.mjs.map +1 -0
- package/batteries/tools/unit_conversion/index.d.ts +19 -0
- package/batteries/tools/unit_conversion.cjs +452 -0
- package/batteries/tools/unit_conversion.cjs.map +1 -0
- package/batteries/tools/unit_conversion.mjs +450 -0
- package/batteries/tools/unit_conversion.mjs.map +1 -0
- package/batteries/tools.cjs +80 -0
- package/batteries/tools.mjs +21 -0
- package/batteries.cjs +142 -0
- package/batteries.mjs +30 -0
- package/chunk-KmRHZBOW.js +35 -0
- package/common-DeZaonK1.mjs +208 -0
- package/common-DeZaonK1.mjs.map +1 -0
- package/common-Od8edUXU.js +232 -0
- package/common-Od8edUXU.js.map +1 -0
- package/common.cjs +31 -0
- package/common.d.ts +108 -0
- package/common.mjs +8 -0
- package/dispatch_runner-9j6bXHL3.mjs +1609 -0
- package/dispatch_runner-9j6bXHL3.mjs.map +1 -0
- package/dispatch_runner-CsoH0nld.js +1627 -0
- package/dispatch_runner-CsoH0nld.js.map +1 -0
- package/dispatch_runner.cjs +3 -0
- package/dispatch_runner.d.ts +17 -0
- package/dispatch_runner.mjs +2 -0
- package/exceptions-D5YrO9Vm.js +280 -0
- package/exceptions-D5YrO9Vm.js.map +1 -0
- package/exceptions-NrzIHw_R.mjs +244 -0
- package/exceptions-NrzIHw_R.mjs.map +1 -0
- package/exceptions.cjs +33 -0
- package/exceptions.d.ts +52 -0
- package/exceptions.mjs +3 -0
- package/factories.cjs +4 -0
- package/factories.d.ts +39 -0
- package/factories.mjs +2 -0
- package/forge.cjs +9 -0
- package/forge.d.ts +49 -0
- package/forge.mjs +5 -0
- package/guards.cjs +96 -0
- package/guards.cjs.map +1 -0
- package/guards.d.ts +83 -0
- package/guards.mjs +72 -0
- package/guards.mjs.map +1 -0
- package/index.cjs +107 -0
- package/index.cjs.map +1 -0
- package/index.d.ts +18 -0
- package/index.mjs +31 -0
- package/index.mjs.map +1 -0
- package/lib/classes/artifact_tool.d.ts +129 -0
- package/lib/classes/base_exception.d.ts +83 -0
- package/lib/classes/identity.d.ts +71 -0
- package/lib/classes/media.d.ts +326 -0
- package/lib/classes/memory.d.ts +72 -0
- package/lib/classes/message.d.ts +137 -0
- package/lib/classes/registry.d.ts +79 -0
- package/lib/classes/retrievable.d.ts +100 -0
- package/lib/classes/spooled_artifact.d.ts +296 -0
- package/lib/classes/spooled_json_artifact.d.ts +158 -0
- package/lib/classes/spooled_markdown_artifact.d.ts +202 -0
- package/lib/classes/thought.d.ts +142 -0
- package/lib/classes/tokenizable.d.ts +124 -0
- package/lib/classes/tool.d.ts +228 -0
- package/lib/classes/tool_call.d.ts +190 -0
- package/lib/classes/tool_registry.d.ts +159 -0
- package/lib/classes/turn_gate.d.ts +109 -0
- package/lib/contracts/dispatch_context.d.ts +345 -0
- package/lib/contracts/media_reader.d.ts +60 -0
- package/lib/contracts/spool_reader.d.ts +80 -0
- package/lib/contracts/spooled_artifact_constructor.d.ts +38 -0
- package/lib/contracts/turn_runner_config.d.ts +101 -0
- package/lib/contracts/turn_runner_context.d.ts +267 -0
- package/lib/dispatch_runner.d.ts +98 -0
- package/lib/exceptions/runtime.d.ts +370 -0
- package/lib/helpers/media_readers.d.ts +39 -0
- package/lib/turn_runner.d.ts +144 -0
- package/lib/types/dispatch_context.d.ts +233 -0
- package/lib/types/dispatch_runner.d.ts +387 -0
- package/lib/types/turn_runner.d.ts +322 -0
- package/lib/utils/canonical_json.d.ts +18 -0
- package/lib/utils/exceptions.d.ts +78 -0
- package/lib/utils/guards.d.ts +32 -0
- package/lib/utils/validation.d.ts +77 -0
- package/package.json +334 -0
- package/runtime-BJVkrGQe.js +519 -0
- package/runtime-BJVkrGQe.js.map +1 -0
- package/runtime-CrEPIFgr.mjs +346 -0
- package/runtime-CrEPIFgr.mjs.map +1 -0
- package/skills/adk-assembly/SKILL.md +109 -0
- package/skills/adk-assembly/references/assembly-contract.md +66 -0
- package/skills/adk-assembly/references/executors-tools-pipelines-events.md +113 -0
- package/skills/adk-assembly/references/first-integration.md +93 -0
- package/skills/adk-assembly/references/storage-and-context.md +102 -0
- package/spooled_artifact-C5ZtGxuJ.mjs +544 -0
- package/spooled_artifact-C5ZtGxuJ.mjs.map +1 -0
- package/spooled_artifact-Cm9Te22K.js +568 -0
- package/spooled_artifact-Cm9Te22K.js.map +1 -0
- package/spooled_artifact.cjs +7 -0
- package/spooled_artifact.d.ts +40 -0
- package/spooled_artifact.mjs +3 -0
- package/spooled_markdown_artifact-BpUJol0W.mjs +771 -0
- package/spooled_markdown_artifact-BpUJol0W.mjs.map +1 -0
- package/spooled_markdown_artifact-RRB113sy.js +786 -0
- package/spooled_markdown_artifact-RRB113sy.js.map +1 -0
- package/thought-CDb457b4.mjs +470 -0
- package/thought-CDb457b4.mjs.map +1 -0
- package/thought-DuN2PgdO.js +494 -0
- package/thought-DuN2PgdO.js.map +1 -0
- package/tool-COSeH8I6.js +302 -0
- package/tool-COSeH8I6.js.map +1 -0
- package/tool-D2WB1EA1.mjs +296 -0
- package/tool-D2WB1EA1.mjs.map +1 -0
- package/tool_call-BKyyxGaZ.mjs +578 -0
- package/tool_call-BKyyxGaZ.mjs.map +1 -0
- package/tool_call-DFgzcVcU.js +608 -0
- package/tool_call-DFgzcVcU.js.map +1 -0
- package/tool_registry-Dkfprsck.js +641 -0
- package/tool_registry-Dkfprsck.js.map +1 -0
- package/tool_registry-DqLOyGyG.mjs +592 -0
- package/tool_registry-DqLOyGyG.mjs.map +1 -0
- package/turn_runner-CMm2BHdX.js +615 -0
- package/turn_runner-CMm2BHdX.js.map +1 -0
- package/turn_runner-y7eyEcJH.mjs +603 -0
- package/turn_runner-y7eyEcJH.mjs.map +1 -0
- package/turn_runner.cjs +3 -0
- package/turn_runner.d.ts +21 -0
- package/turn_runner.mjs +2 -0
- package/types.cjs +1 -0
- package/types.d.ts +56 -0
- package/types.mjs +0 -0
- package/vite-env.d.ts +23 -0
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
require("./chunk-KmRHZBOW.js");
|
|
2
|
+
let _nhtio_validation = require("@nhtio/validation");
|
|
3
|
+
let fast_printf = require("fast-printf");
|
|
4
|
+
//#region src/lib/classes/base_exception.ts
|
|
5
|
+
/**
|
|
6
|
+
* Base class for all structured exceptions in the ADK.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* Subclasses should declare static `code`, `status`, `fatal`, and optionally `help` to avoid
|
|
10
|
+
* repeating those values on every instance. Instance-level options always take precedence over
|
|
11
|
+
* static defaults, so a single exception class can still be thrown with per-site overrides when
|
|
12
|
+
* needed.
|
|
13
|
+
*
|
|
14
|
+
* The runtime cross-realm guard is inlined here rather than imported from `../utils/guards`
|
|
15
|
+
* to break a circular-import chain: `guards` depends on `validation`, which extends
|
|
16
|
+
* `BaseException`. Importing the shared `isInstanceOf` helper into this file would create a
|
|
17
|
+
* load-order cycle that leaves `BaseException` undefined when `ValidationException extends
|
|
18
|
+
* BaseException` evaluates.
|
|
19
|
+
*/
|
|
20
|
+
var BaseException = class BaseException extends Error {
|
|
21
|
+
/**
|
|
22
|
+
* Returns `true` if `value` is a {@link BaseException} instance.
|
|
23
|
+
*
|
|
24
|
+
* @remarks
|
|
25
|
+
* Performs cross-realm-safe detection: tries `instanceof`, then `Symbol.hasInstance`, then
|
|
26
|
+
* constructor-name comparison. The ADK does not export the `BaseException` class itself
|
|
27
|
+
* as a constructable value — use this guard plus the {@link BaseException} type for runtime
|
|
28
|
+
* detection and TypeScript narrowing.
|
|
29
|
+
*
|
|
30
|
+
* @param value - The value to test.
|
|
31
|
+
* @returns `true` when `value` is a {@link BaseException} instance.
|
|
32
|
+
*/
|
|
33
|
+
static isBaseException(value) {
|
|
34
|
+
if (value instanceof BaseException) return true;
|
|
35
|
+
if (typeof BaseException[Symbol.hasInstance] === "function" && BaseException[Symbol.hasInstance](value)) return true;
|
|
36
|
+
if (typeof value === "object" && value !== null) {
|
|
37
|
+
if (value.constructor?.name === "BaseException") return true;
|
|
38
|
+
}
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Name of the class that raised the exception.
|
|
43
|
+
*/
|
|
44
|
+
name;
|
|
45
|
+
/**
|
|
46
|
+
* @param message - Human-readable error message. Falls back to the static `message` on the
|
|
47
|
+
* subclass if omitted.
|
|
48
|
+
* @param options - Standard `ErrorOptions` extended with `code`, `status`, and `fatal`
|
|
49
|
+
* overrides. Static defaults on the subclass are used when these are absent.
|
|
50
|
+
*/
|
|
51
|
+
constructor(message, options) {
|
|
52
|
+
super(message, options);
|
|
53
|
+
const ErrorConstructor = this.constructor;
|
|
54
|
+
this.name = ErrorConstructor.name;
|
|
55
|
+
this.message = message || ErrorConstructor.message || "";
|
|
56
|
+
const code = options?.code || ErrorConstructor.code;
|
|
57
|
+
if (code !== void 0) this.code = code;
|
|
58
|
+
const status = options?.status || ErrorConstructor.status;
|
|
59
|
+
if (status !== void 0) this.status = status;
|
|
60
|
+
const fatal = options?.fatal ?? ErrorConstructor.fatal;
|
|
61
|
+
if (fatal !== void 0) this.fatal = fatal;
|
|
62
|
+
const help = ErrorConstructor.help;
|
|
63
|
+
if (help !== void 0) this.help = help;
|
|
64
|
+
Error.captureStackTrace(this, ErrorConstructor);
|
|
65
|
+
}
|
|
66
|
+
get [Symbol.toStringTag]() {
|
|
67
|
+
return this.constructor.name;
|
|
68
|
+
}
|
|
69
|
+
toString() {
|
|
70
|
+
if (this.code) return `${this.name} [${this.code}]: ${this.message}`;
|
|
71
|
+
return `${this.name}: ${this.message}`;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
//#endregion
|
|
75
|
+
//#region src/lib/utils/validation.ts
|
|
76
|
+
/**
|
|
77
|
+
* Returns `true` if `value` satisfies `schema` without throwing.
|
|
78
|
+
*
|
|
79
|
+
* @remarks
|
|
80
|
+
* Aborts on the first validation error. Use {@link validateOrThrow} or
|
|
81
|
+
* {@link asyncValidateOrThrow} when you need the full set of field errors.
|
|
82
|
+
*
|
|
83
|
+
* @param schema - The schema to validate against.
|
|
84
|
+
* @param value - The value to test.
|
|
85
|
+
* @returns `true` when `value` passes the schema; `false` otherwise.
|
|
86
|
+
*/
|
|
87
|
+
var passesSchema = (schema, value) => {
|
|
88
|
+
const { error } = schema.validate(value, { abortEarly: true });
|
|
89
|
+
return !error;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Returns `true` if `value` is a `ValidationError` or satisfies its minimum duck-type shape.
|
|
93
|
+
*
|
|
94
|
+
* @remarks
|
|
95
|
+
* The duck-typing path handles `ValidationError` objects that cross module or realm boundaries
|
|
96
|
+
* where `instanceof` would return `false`.
|
|
97
|
+
*
|
|
98
|
+
* @param value - The value to test.
|
|
99
|
+
* @returns `true` when `value` conforms to the `ValidationError` shape.
|
|
100
|
+
*/
|
|
101
|
+
var isValidationError = (value) => {
|
|
102
|
+
return passesSchema(_nhtio_validation.validator.alternatives(_nhtio_validation.validator.object().instance(_nhtio_validation.ValidationError), _nhtio_validation.validator.function().instance(_nhtio_validation.ValidationError), _nhtio_validation.validator.object({
|
|
103
|
+
message: _nhtio_validation.validator.string().required(),
|
|
104
|
+
details: _nhtio_validation.validator.array().items(_nhtio_validation.validator.object({
|
|
105
|
+
message: _nhtio_validation.validator.string().required(),
|
|
106
|
+
path: _nhtio_validation.validator.array().items(_nhtio_validation.validator.alternatives(_nhtio_validation.validator.string(), _nhtio_validation.validator.number())).required(),
|
|
107
|
+
type: _nhtio_validation.validator.string().required(),
|
|
108
|
+
context: _nhtio_validation.validator.object().unknown(true).required()
|
|
109
|
+
})).required()
|
|
110
|
+
}).unknown(true)), value);
|
|
111
|
+
};
|
|
112
|
+
var messageFromValidationError = (reason, fallback) => {
|
|
113
|
+
return reason ? reason.details.map((d) => d.message).join(" and ") : fallback;
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* Thrown when input fails schema validation.
|
|
117
|
+
*
|
|
118
|
+
* @remarks
|
|
119
|
+
* Carries the full `details` array from the underlying `ValidationError` so callers can surface
|
|
120
|
+
* field-level messages without unwrapping the `cause` manually.
|
|
121
|
+
*/
|
|
122
|
+
var ValidationException = class ValidationException extends BaseException {
|
|
123
|
+
static status = 422;
|
|
124
|
+
static code = "VALIDATION_EXCEPTION";
|
|
125
|
+
static fatal = false;
|
|
126
|
+
/**
|
|
127
|
+
* @param reason - The `ValidationError` thrown by the schema; its `details` are surfaced
|
|
128
|
+
* directly on this exception and its messages are joined to form the human-readable message.
|
|
129
|
+
*/
|
|
130
|
+
constructor(reason) {
|
|
131
|
+
const message = messageFromValidationError(reason, "Validation failed");
|
|
132
|
+
super(message, {
|
|
133
|
+
code: ValidationException.code,
|
|
134
|
+
status: ValidationException.status,
|
|
135
|
+
fatal: ValidationException.fatal,
|
|
136
|
+
cause: reason
|
|
137
|
+
});
|
|
138
|
+
Object.defineProperty(this, "details", {
|
|
139
|
+
value: reason.details,
|
|
140
|
+
enumerable: true,
|
|
141
|
+
configurable: false,
|
|
142
|
+
writable: false
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
/**
|
|
147
|
+
* Validates `value` against `schema` synchronously and returns the coerced result typed as `T`.
|
|
148
|
+
*
|
|
149
|
+
* @remarks
|
|
150
|
+
* Collects all field errors before throwing. Use {@link asyncValidateOrThrow} for schemas that
|
|
151
|
+
* include async custom validators.
|
|
152
|
+
*
|
|
153
|
+
* @typeParam T - The expected type of `value` after successful validation.
|
|
154
|
+
* @param schema - The schema to validate against.
|
|
155
|
+
* @param value - The value to validate.
|
|
156
|
+
* @param convert - When `true`, the validator coerces values to their target types (e.g. string
|
|
157
|
+
* `"1"` → number `1`). Defaults to `false` to prevent silent type coercion.
|
|
158
|
+
* @returns The validated (and optionally coerced) value typed as `T`.
|
|
159
|
+
* @throws {@link ValidationException} when `value` does not satisfy `schema`.
|
|
160
|
+
*/
|
|
161
|
+
var validateOrThrow = (schema, value, convert = false) => {
|
|
162
|
+
const { value: returnable, error } = schema.validate(value, {
|
|
163
|
+
abortEarly: false,
|
|
164
|
+
convert
|
|
165
|
+
});
|
|
166
|
+
if (error) throw new ValidationException(error);
|
|
167
|
+
return returnable;
|
|
168
|
+
};
|
|
169
|
+
/**
|
|
170
|
+
* Validates `value` against `schema` asynchronously and returns the coerced result typed as `T`.
|
|
171
|
+
*
|
|
172
|
+
* @remarks
|
|
173
|
+
* Collects all field errors before throwing. Prefer this over {@link validateOrThrow} when the
|
|
174
|
+
* schema includes async custom validators.
|
|
175
|
+
*
|
|
176
|
+
* @typeParam T - The expected type of the validated and coerced return value.
|
|
177
|
+
* @param schema - The schema to validate against.
|
|
178
|
+
* @param value - The value to validate.
|
|
179
|
+
* @param convert - When `true`, the validator coerces values to their target types (e.g. string
|
|
180
|
+
* `"1"` → number `1`). Defaults to `false` to prevent silent type coercion.
|
|
181
|
+
* @returns The validated (and optionally coerced) value typed as `T`.
|
|
182
|
+
* @throws {@link ValidationException} when `value` does not satisfy `schema`.
|
|
183
|
+
*/
|
|
184
|
+
var asyncValidateOrThrow = async (schema, value, convert = false) => {
|
|
185
|
+
try {
|
|
186
|
+
return await schema.validateAsync(value, {
|
|
187
|
+
abortEarly: false,
|
|
188
|
+
convert
|
|
189
|
+
});
|
|
190
|
+
} catch (error) {
|
|
191
|
+
if (isValidationError(error)) throw new ValidationException(error);
|
|
192
|
+
throw error;
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
//#endregion
|
|
196
|
+
//#region src/lib/utils/exceptions.ts
|
|
197
|
+
/**
|
|
198
|
+
* Factory that produces a named {@link @nhtio/adk!BaseException} subclass with a fixed printf-style message
|
|
199
|
+
* template, error code, HTTP status, and fatality flag.
|
|
200
|
+
*
|
|
201
|
+
* @remarks
|
|
202
|
+
* Prefer this over hand-writing subclasses for simple, static exception definitions.
|
|
203
|
+
*
|
|
204
|
+
* @typeParam T - Tuple of printf format argument types. Pass a non-empty tuple to require
|
|
205
|
+
* callers to supply interpolation values at the throw site.
|
|
206
|
+
*
|
|
207
|
+
* @param name - The `name` property set on thrown instances (used by {@link isNamedException}).
|
|
208
|
+
* @param message - Printf-style template string for the error message.
|
|
209
|
+
* @param code - Machine-readable error code stored on the static and instance `code` property.
|
|
210
|
+
* @param status - HTTP status code associated with this exception class.
|
|
211
|
+
* @param fatal - When `true`, signals that the error is unrecoverable.
|
|
212
|
+
* @returns A constructor for a {@link @nhtio/adk!BaseException} subclass with the given metadata baked in.
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* ```ts
|
|
216
|
+
* export const E_NOT_FOUND = createException<[string]>(
|
|
217
|
+
* 'E_NOT_FOUND', 'Resource %s not found', 'E_NOT_FOUND', 404, false
|
|
218
|
+
* )
|
|
219
|
+
* throw new E_NOT_FOUND(['my-id'])
|
|
220
|
+
* ```
|
|
221
|
+
*/
|
|
222
|
+
var createException = (name, message, code, status, fatal) => {
|
|
223
|
+
const Ctor = class extends BaseException {
|
|
224
|
+
static message = message;
|
|
225
|
+
static code = code;
|
|
226
|
+
static status = status;
|
|
227
|
+
static fatal = fatal;
|
|
228
|
+
constructor(args, options) {
|
|
229
|
+
const hasMessageArgs = Array.isArray(args);
|
|
230
|
+
const messageArgs = hasMessageArgs ? args : [];
|
|
231
|
+
const errorOptions = hasMessageArgs ? options : args;
|
|
232
|
+
super((0, fast_printf.printf)(message, ...messageArgs), errorOptions);
|
|
233
|
+
this.name = name;
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
Object.defineProperty(Ctor, "name", {
|
|
237
|
+
value: name,
|
|
238
|
+
configurable: true
|
|
239
|
+
});
|
|
240
|
+
return Ctor;
|
|
241
|
+
};
|
|
242
|
+
//#endregion
|
|
243
|
+
Object.defineProperty(exports, "BaseException", {
|
|
244
|
+
enumerable: true,
|
|
245
|
+
get: function() {
|
|
246
|
+
return BaseException;
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
Object.defineProperty(exports, "ValidationException", {
|
|
250
|
+
enumerable: true,
|
|
251
|
+
get: function() {
|
|
252
|
+
return ValidationException;
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
Object.defineProperty(exports, "asyncValidateOrThrow", {
|
|
256
|
+
enumerable: true,
|
|
257
|
+
get: function() {
|
|
258
|
+
return asyncValidateOrThrow;
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
Object.defineProperty(exports, "createException", {
|
|
262
|
+
enumerable: true,
|
|
263
|
+
get: function() {
|
|
264
|
+
return createException;
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
Object.defineProperty(exports, "passesSchema", {
|
|
268
|
+
enumerable: true,
|
|
269
|
+
get: function() {
|
|
270
|
+
return passesSchema;
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
Object.defineProperty(exports, "validateOrThrow", {
|
|
274
|
+
enumerable: true,
|
|
275
|
+
get: function() {
|
|
276
|
+
return validateOrThrow;
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
//# sourceMappingURL=exceptions-D5YrO9Vm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exceptions-D5YrO9Vm.js","names":[],"sources":["../src/lib/classes/base_exception.ts","../src/lib/utils/validation.ts","../src/lib/utils/exceptions.ts"],"sourcesContent":["/**\n * Base class for all structured exceptions in the ADK.\n *\n * @remarks\n * Subclasses should declare static `code`, `status`, `fatal`, and optionally `help` to avoid\n * repeating those values on every instance. Instance-level options always take precedence over\n * static defaults, so a single exception class can still be thrown with per-site overrides when\n * needed.\n *\n * The runtime cross-realm guard is inlined here rather than imported from `../utils/guards`\n * to break a circular-import chain: `guards` depends on `validation`, which extends\n * `BaseException`. Importing the shared `isInstanceOf` helper into this file would create a\n * load-order cycle that leaves `BaseException` undefined when `ValidationException extends\n * BaseException` evaluates.\n */\nexport class BaseException extends Error {\n /**\n * Returns `true` if `value` is a {@link BaseException} instance.\n *\n * @remarks\n * Performs cross-realm-safe detection: tries `instanceof`, then `Symbol.hasInstance`, then\n * constructor-name comparison. The ADK does not export the `BaseException` class itself\n * as a constructable value — use this guard plus the {@link BaseException} type for runtime\n * detection and TypeScript narrowing.\n *\n * @param value - The value to test.\n * @returns `true` when `value` is a {@link BaseException} instance.\n */\n public static isBaseException(value: unknown): value is BaseException {\n // eslint-disable-next-line adk/use-is-instance-of -- module cycle (guards ↔ validation ↔ BaseException); the cross-realm fallback is inlined below\n if (value instanceof BaseException) return true\n if (\n typeof BaseException[Symbol.hasInstance] === 'function' &&\n BaseException[Symbol.hasInstance](value)\n )\n return true\n // eslint-disable-next-line adk/prefer-is-object -- module cycle (guards ↔ validation ↔ BaseException); isObject would create a load-order cycle\n if (typeof value === 'object' && value !== null) {\n const ctorName = (value as { constructor?: { name?: string } }).constructor?.name\n if (ctorName === 'BaseException') return true\n }\n return false\n }\n /**\n * Default help text inherited by all instances unless overridden at the throw site.\n */\n declare static help?: string\n /**\n * Default machine-readable error code inherited by all instances.\n */\n declare static code?: string\n /**\n * Default HTTP status code inherited by all instances.\n */\n declare static status?: number\n /**\n * Whether exceptions of this class are fatal by default.\n */\n declare static fatal?: boolean\n /**\n * Default message used when no message is supplied to the constructor.\n */\n declare static message?: string\n\n /**\n * Name of the class that raised the exception.\n */\n name: string\n\n /**\n * Human-readable guidance for resolving or reporting this error.\n */\n declare help?: string\n\n /**\n * Machine-readable error code for narrowing exception-handling logic.\n */\n declare code?: string\n\n /**\n * HTTP status code associated with this error.\n */\n declare status?: number\n\n /**\n * When `true`, the ADK treats this error as unrecoverable and should halt the agent loop.\n */\n declare fatal?: boolean\n\n /**\n * @param message - Human-readable error message. Falls back to the static `message` on the\n * subclass if omitted.\n * @param options - Standard `ErrorOptions` extended with `code`, `status`, and `fatal`\n * overrides. Static defaults on the subclass are used when these are absent.\n */\n constructor(\n message?: string,\n options?: ErrorOptions & { code?: string; status?: number; fatal?: boolean }\n ) {\n super(message, options)\n\n const ErrorConstructor = this.constructor as typeof BaseException\n\n this.name = ErrorConstructor.name\n this.message = message || ErrorConstructor.message || ''\n\n const code = options?.code || ErrorConstructor.code\n if (code !== undefined) {\n this.code = code\n }\n\n const status = options?.status || ErrorConstructor.status\n if (status !== undefined) {\n this.status = status\n }\n\n const fatal = options?.fatal ?? ErrorConstructor.fatal\n if (fatal !== undefined) {\n this.fatal = fatal\n }\n\n const help = ErrorConstructor.help\n if (help !== undefined) {\n this.help = help\n }\n\n Error.captureStackTrace(this, ErrorConstructor)\n }\n\n get [Symbol.toStringTag]() {\n return this.constructor.name\n }\n\n toString() {\n if (this.code) {\n return `${this.name} [${this.code}]: ${this.message}`\n }\n return `${this.name}: ${this.message}`\n }\n}\n","import { BaseException } from '../classes/base_exception'\nimport { validator, ValidationError } from '@nhtio/validation'\nimport type { Schema } from '@nhtio/validation'\n\n/**\n * Returns `true` if `value` satisfies `schema` without throwing.\n *\n * @remarks\n * Aborts on the first validation error. Use {@link validateOrThrow} or\n * {@link asyncValidateOrThrow} when you need the full set of field errors.\n *\n * @param schema - The schema to validate against.\n * @param value - The value to test.\n * @returns `true` when `value` passes the schema; `false` otherwise.\n */\nexport const passesSchema = (schema: Schema, value: unknown): boolean => {\n const { error } = schema.validate(value, { abortEarly: true })\n return !error\n}\n\n/**\n * Returns `true` if `value` is a `ValidationError` or satisfies its minimum duck-type shape.\n *\n * @remarks\n * The duck-typing path handles `ValidationError` objects that cross module or realm boundaries\n * where `instanceof` would return `false`.\n *\n * @param value - The value to test.\n * @returns `true` when `value` conforms to the `ValidationError` shape.\n */\nexport const isValidationError = (value: unknown): value is ValidationError => {\n const schema = validator.alternatives(\n validator.object().instance(ValidationError as any),\n validator.function().instance(ValidationError as any),\n validator\n .object({\n message: validator.string().required(),\n details: validator\n .array()\n .items(\n validator.object({\n message: validator.string().required(),\n path: validator\n .array()\n .items(validator.alternatives(validator.string(), validator.number()))\n .required(),\n type: validator.string().required(),\n context: validator.object().unknown(true).required(),\n })\n )\n .required(),\n })\n .unknown(true)\n )\n return passesSchema(schema, value)\n}\n\nconst messageFromValidationError = (reason: ValidationError | undefined, fallback: string) => {\n return reason ? reason.details.map((d) => d.message).join(' and ') : fallback\n}\n\n/**\n * Thrown when input fails schema validation.\n *\n * @remarks\n * Carries the full `details` array from the underlying `ValidationError` so callers can surface\n * field-level messages without unwrapping the `cause` manually.\n */\nexport class ValidationException extends BaseException {\n static status = 422\n static code = 'VALIDATION_EXCEPTION'\n static fatal = false\n\n /** The raw field-level error details from the underlying `ValidationError`. */\n declare readonly details?: ValidationError['details']\n\n /**\n * @param reason - The `ValidationError` thrown by the schema; its `details` are surfaced\n * directly on this exception and its messages are joined to form the human-readable message.\n */\n constructor(reason: ValidationError) {\n const message = messageFromValidationError(reason, 'Validation failed')\n super(message, {\n code: ValidationException.code,\n status: ValidationException.status,\n fatal: ValidationException.fatal,\n cause: reason,\n })\n Object.defineProperty(this, 'details', {\n value: reason.details,\n enumerable: true,\n configurable: false,\n writable: false,\n })\n }\n}\n\n/**\n * Validates `value` against `schema` synchronously and returns the coerced result typed as `T`.\n *\n * @remarks\n * Collects all field errors before throwing. Use {@link asyncValidateOrThrow} for schemas that\n * include async custom validators.\n *\n * @typeParam T - The expected type of `value` after successful validation.\n * @param schema - The schema to validate against.\n * @param value - The value to validate.\n * @param convert - When `true`, the validator coerces values to their target types (e.g. string\n * `\"1\"` → number `1`). Defaults to `false` to prevent silent type coercion.\n * @returns The validated (and optionally coerced) value typed as `T`.\n * @throws {@link ValidationException} when `value` does not satisfy `schema`.\n */\nexport const validateOrThrow = <T>(schema: Schema, value: unknown, convert: boolean = false): T => {\n const { value: returnable, error } = schema.validate(value, { abortEarly: false, convert })\n if (error) {\n throw new ValidationException(error)\n }\n return returnable as T\n}\n\n/**\n * Validates `value` against `schema` asynchronously and returns the coerced result typed as `T`.\n *\n * @remarks\n * Collects all field errors before throwing. Prefer this over {@link validateOrThrow} when the\n * schema includes async custom validators.\n *\n * @typeParam T - The expected type of the validated and coerced return value.\n * @param schema - The schema to validate against.\n * @param value - The value to validate.\n * @param convert - When `true`, the validator coerces values to their target types (e.g. string\n * `\"1\"` → number `1`). Defaults to `false` to prevent silent type coercion.\n * @returns The validated (and optionally coerced) value typed as `T`.\n * @throws {@link ValidationException} when `value` does not satisfy `schema`.\n */\nexport const asyncValidateOrThrow = async <T>(\n schema: Schema,\n value: unknown,\n convert: boolean = false\n): Promise<T> => {\n try {\n return await schema.validateAsync(value, { abortEarly: false, convert })\n } catch (error) {\n if (isValidationError(error)) {\n throw new ValidationException(error)\n }\n throw error\n }\n}\n","import { passesSchema } from './validation'\nimport { validator } from '@nhtio/validation'\nimport { printf as format } from 'fast-printf'\nimport { BaseException } from '../classes/base_exception'\n\n/**\n * Options accepted by {@link @nhtio/adk!BaseException} (and factory-created exceptions) beyond the\n * standard `ErrorOptions`.\n *\n * @remarks\n * These mirror the static defaults on {@link @nhtio/adk!BaseException} but allow per-throw overrides so a\n * single exception class can carry different metadata at different throw sites.\n */\nexport type ExceptionOptions = ErrorOptions & {\n code?: string\n status?: number\n fatal?: boolean\n}\n\n/**\n * Constructor signature of an exception class produced by {@link createException}.\n *\n * @typeParam T - Tuple of printf-style format argument types. When `T` is an empty tuple the\n * constructor takes no positional message arguments; when non-empty the first argument must be\n * an array of values matching `T`.\n */\nexport type CreatedException<T extends any[] = []> = typeof BaseException &\n (T extends []\n ? {\n new (options?: ExceptionOptions): BaseException\n }\n : { new (args: T, options?: ExceptionOptions): BaseException })\n\n/**\n * Factory that produces a named {@link @nhtio/adk!BaseException} subclass with a fixed printf-style message\n * template, error code, HTTP status, and fatality flag.\n *\n * @remarks\n * Prefer this over hand-writing subclasses for simple, static exception definitions.\n *\n * @typeParam T - Tuple of printf format argument types. Pass a non-empty tuple to require\n * callers to supply interpolation values at the throw site.\n *\n * @param name - The `name` property set on thrown instances (used by {@link isNamedException}).\n * @param message - Printf-style template string for the error message.\n * @param code - Machine-readable error code stored on the static and instance `code` property.\n * @param status - HTTP status code associated with this exception class.\n * @param fatal - When `true`, signals that the error is unrecoverable.\n * @returns A constructor for a {@link @nhtio/adk!BaseException} subclass with the given metadata baked in.\n *\n * @example\n * ```ts\n * export const E_NOT_FOUND = createException<[string]>(\n * 'E_NOT_FOUND', 'Resource %s not found', 'E_NOT_FOUND', 404, false\n * )\n * throw new E_NOT_FOUND(['my-id'])\n * ```\n */\nexport const createException = <T extends any[] = []>(\n name: string,\n message: string,\n code: string,\n status?: number,\n fatal?: boolean\n): CreatedException<T> => {\n const Ctor = class extends BaseException {\n static message = message\n static code = code\n static status = status\n static fatal = fatal\n constructor(args?: T | ExceptionOptions, options?: ExceptionOptions) {\n const hasMessageArgs = Array.isArray(args)\n const messageArgs = hasMessageArgs ? args : []\n const errorOptions = hasMessageArgs ? options : args\n\n super(format(message, ...messageArgs), errorOptions)\n this.name = name\n }\n }\n // Without this, the factory returns an anonymous class — constructor.name is \"\" and\n // cross-realm `isInstanceOf(err, 'E_FOO')` (which falls back to constructor-name comparison)\n // never matches. Setting the name on the class itself makes the identity carry through.\n Object.defineProperty(Ctor, 'name', { value: name, configurable: true })\n return Ctor as unknown as CreatedException<T>\n}\n\n/**\n * Returns `true` if `value` is a {@link @nhtio/adk!BaseException} or satisfies its minimum duck-type shape.\n *\n * @remarks\n * The duck-typing path handles exceptions that cross module or realm boundaries where\n * `instanceof` would return `false` for structurally identical objects.\n *\n * @param value - The value to test.\n * @returns `true` when `value` conforms to the {@link @nhtio/adk!BaseException} shape.\n */\nexport const isException = (value: unknown): value is BaseException => {\n const schema = validator.alternatives(\n validator.object().instance(BaseException as any),\n validator.function().instance(BaseException as any),\n validator\n .object({\n name: validator.string().required(),\n message: validator.string().required(),\n help: validator.string().optional(),\n code: validator.string().optional(),\n status: validator.number().optional(),\n fatal: validator.boolean().optional(),\n })\n .unknown(true)\n )\n return passesSchema(schema, value)\n}\n\n/**\n * Narrows `value` to a {@link @nhtio/adk!BaseException} whose `name` property matches `name` exactly.\n *\n * @remarks\n * Useful for catching a specific factory-created exception by its string identifier when\n * `instanceof` checks are not available (e.g. across module boundaries).\n *\n * @param value - The value to test.\n * @param name - The exact string to compare against `value.name`.\n * @returns `true` when `value` is a {@link @nhtio/adk!BaseException} with the given `name`.\n */\nexport const isNamedException = (value: unknown, name: string): value is BaseException => {\n return isException(value) && value.name === name\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAeA,IAAa,gBAAb,MAAa,sBAAsB,MAAM;;;;;;;;;;;;;CAavC,OAAc,gBAAgB,OAAwC;EAEpE,IAAI,iBAAiB,eAAe,OAAO;EAC3C,IACE,OAAO,cAAc,OAAO,iBAAiB,cAC7C,cAAc,OAAO,aAAa,KAAK,GAEvC,OAAO;EAET,IAAI,OAAO,UAAU,YAAY,UAAU;OACvB,MAA8C,aAAa,SAC5D,iBAAiB,OAAO;EAAA;EAE3C,OAAO;CACT;;;;CAyBA;;;;;;;CA4BA,YACE,SACA,SACA;EACA,MAAM,SAAS,OAAO;EAEtB,MAAM,mBAAmB,KAAK;EAE9B,KAAK,OAAO,iBAAiB;EAC7B,KAAK,UAAU,WAAW,iBAAiB,WAAW;EAEtD,MAAM,OAAO,SAAS,QAAQ,iBAAiB;EAC/C,IAAI,SAAS,KAAA,GACX,KAAK,OAAO;EAGd,M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I;GACzC,MAAM,cAAc,iBAAiB,OAAO,CAAC;GAC7C,MAAM,eAAe,iBAAiB,UAAU;GAEhD,OAAA,GAAA,YAAA,QAAa,SAAS,GAAG,WAAW,GAAG,YAAY;GACnD,KAAK,OAAO;EACd;CACF;CAIA,OAAO,eAAe,MAAM,QAAQ;EAAE,OAAO;EAAM,cAAc;CAAK,CAAC;CACvE,OAAO;AACT"}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { ValidationError, validator } from "@nhtio/validation";
|
|
2
|
+
import { printf } from "fast-printf";
|
|
3
|
+
//#region src/lib/classes/base_exception.ts
|
|
4
|
+
/**
|
|
5
|
+
* Base class for all structured exceptions in the ADK.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* Subclasses should declare static `code`, `status`, `fatal`, and optionally `help` to avoid
|
|
9
|
+
* repeating those values on every instance. Instance-level options always take precedence over
|
|
10
|
+
* static defaults, so a single exception class can still be thrown with per-site overrides when
|
|
11
|
+
* needed.
|
|
12
|
+
*
|
|
13
|
+
* The runtime cross-realm guard is inlined here rather than imported from `../utils/guards`
|
|
14
|
+
* to break a circular-import chain: `guards` depends on `validation`, which extends
|
|
15
|
+
* `BaseException`. Importing the shared `isInstanceOf` helper into this file would create a
|
|
16
|
+
* load-order cycle that leaves `BaseException` undefined when `ValidationException extends
|
|
17
|
+
* BaseException` evaluates.
|
|
18
|
+
*/
|
|
19
|
+
var BaseException = class BaseException extends Error {
|
|
20
|
+
/**
|
|
21
|
+
* Returns `true` if `value` is a {@link BaseException} instance.
|
|
22
|
+
*
|
|
23
|
+
* @remarks
|
|
24
|
+
* Performs cross-realm-safe detection: tries `instanceof`, then `Symbol.hasInstance`, then
|
|
25
|
+
* constructor-name comparison. The ADK does not export the `BaseException` class itself
|
|
26
|
+
* as a constructable value — use this guard plus the {@link BaseException} type for runtime
|
|
27
|
+
* detection and TypeScript narrowing.
|
|
28
|
+
*
|
|
29
|
+
* @param value - The value to test.
|
|
30
|
+
* @returns `true` when `value` is a {@link BaseException} instance.
|
|
31
|
+
*/
|
|
32
|
+
static isBaseException(value) {
|
|
33
|
+
if (value instanceof BaseException) return true;
|
|
34
|
+
if (typeof BaseException[Symbol.hasInstance] === "function" && BaseException[Symbol.hasInstance](value)) return true;
|
|
35
|
+
if (typeof value === "object" && value !== null) {
|
|
36
|
+
if (value.constructor?.name === "BaseException") return true;
|
|
37
|
+
}
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Name of the class that raised the exception.
|
|
42
|
+
*/
|
|
43
|
+
name;
|
|
44
|
+
/**
|
|
45
|
+
* @param message - Human-readable error message. Falls back to the static `message` on the
|
|
46
|
+
* subclass if omitted.
|
|
47
|
+
* @param options - Standard `ErrorOptions` extended with `code`, `status`, and `fatal`
|
|
48
|
+
* overrides. Static defaults on the subclass are used when these are absent.
|
|
49
|
+
*/
|
|
50
|
+
constructor(message, options) {
|
|
51
|
+
super(message, options);
|
|
52
|
+
const ErrorConstructor = this.constructor;
|
|
53
|
+
this.name = ErrorConstructor.name;
|
|
54
|
+
this.message = message || ErrorConstructor.message || "";
|
|
55
|
+
const code = options?.code || ErrorConstructor.code;
|
|
56
|
+
if (code !== void 0) this.code = code;
|
|
57
|
+
const status = options?.status || ErrorConstructor.status;
|
|
58
|
+
if (status !== void 0) this.status = status;
|
|
59
|
+
const fatal = options?.fatal ?? ErrorConstructor.fatal;
|
|
60
|
+
if (fatal !== void 0) this.fatal = fatal;
|
|
61
|
+
const help = ErrorConstructor.help;
|
|
62
|
+
if (help !== void 0) this.help = help;
|
|
63
|
+
Error.captureStackTrace(this, ErrorConstructor);
|
|
64
|
+
}
|
|
65
|
+
get [Symbol.toStringTag]() {
|
|
66
|
+
return this.constructor.name;
|
|
67
|
+
}
|
|
68
|
+
toString() {
|
|
69
|
+
if (this.code) return `${this.name} [${this.code}]: ${this.message}`;
|
|
70
|
+
return `${this.name}: ${this.message}`;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
//#endregion
|
|
74
|
+
//#region src/lib/utils/validation.ts
|
|
75
|
+
/**
|
|
76
|
+
* Returns `true` if `value` satisfies `schema` without throwing.
|
|
77
|
+
*
|
|
78
|
+
* @remarks
|
|
79
|
+
* Aborts on the first validation error. Use {@link validateOrThrow} or
|
|
80
|
+
* {@link asyncValidateOrThrow} when you need the full set of field errors.
|
|
81
|
+
*
|
|
82
|
+
* @param schema - The schema to validate against.
|
|
83
|
+
* @param value - The value to test.
|
|
84
|
+
* @returns `true` when `value` passes the schema; `false` otherwise.
|
|
85
|
+
*/
|
|
86
|
+
var passesSchema = (schema, value) => {
|
|
87
|
+
const { error } = schema.validate(value, { abortEarly: true });
|
|
88
|
+
return !error;
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* Returns `true` if `value` is a `ValidationError` or satisfies its minimum duck-type shape.
|
|
92
|
+
*
|
|
93
|
+
* @remarks
|
|
94
|
+
* The duck-typing path handles `ValidationError` objects that cross module or realm boundaries
|
|
95
|
+
* where `instanceof` would return `false`.
|
|
96
|
+
*
|
|
97
|
+
* @param value - The value to test.
|
|
98
|
+
* @returns `true` when `value` conforms to the `ValidationError` shape.
|
|
99
|
+
*/
|
|
100
|
+
var isValidationError = (value) => {
|
|
101
|
+
return passesSchema(validator.alternatives(validator.object().instance(ValidationError), validator.function().instance(ValidationError), validator.object({
|
|
102
|
+
message: validator.string().required(),
|
|
103
|
+
details: validator.array().items(validator.object({
|
|
104
|
+
message: validator.string().required(),
|
|
105
|
+
path: validator.array().items(validator.alternatives(validator.string(), validator.number())).required(),
|
|
106
|
+
type: validator.string().required(),
|
|
107
|
+
context: validator.object().unknown(true).required()
|
|
108
|
+
})).required()
|
|
109
|
+
}).unknown(true)), value);
|
|
110
|
+
};
|
|
111
|
+
var messageFromValidationError = (reason, fallback) => {
|
|
112
|
+
return reason ? reason.details.map((d) => d.message).join(" and ") : fallback;
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* Thrown when input fails schema validation.
|
|
116
|
+
*
|
|
117
|
+
* @remarks
|
|
118
|
+
* Carries the full `details` array from the underlying `ValidationError` so callers can surface
|
|
119
|
+
* field-level messages without unwrapping the `cause` manually.
|
|
120
|
+
*/
|
|
121
|
+
var ValidationException = class ValidationException extends BaseException {
|
|
122
|
+
static status = 422;
|
|
123
|
+
static code = "VALIDATION_EXCEPTION";
|
|
124
|
+
static fatal = false;
|
|
125
|
+
/**
|
|
126
|
+
* @param reason - The `ValidationError` thrown by the schema; its `details` are surfaced
|
|
127
|
+
* directly on this exception and its messages are joined to form the human-readable message.
|
|
128
|
+
*/
|
|
129
|
+
constructor(reason) {
|
|
130
|
+
const message = messageFromValidationError(reason, "Validation failed");
|
|
131
|
+
super(message, {
|
|
132
|
+
code: ValidationException.code,
|
|
133
|
+
status: ValidationException.status,
|
|
134
|
+
fatal: ValidationException.fatal,
|
|
135
|
+
cause: reason
|
|
136
|
+
});
|
|
137
|
+
Object.defineProperty(this, "details", {
|
|
138
|
+
value: reason.details,
|
|
139
|
+
enumerable: true,
|
|
140
|
+
configurable: false,
|
|
141
|
+
writable: false
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
/**
|
|
146
|
+
* Validates `value` against `schema` synchronously and returns the coerced result typed as `T`.
|
|
147
|
+
*
|
|
148
|
+
* @remarks
|
|
149
|
+
* Collects all field errors before throwing. Use {@link asyncValidateOrThrow} for schemas that
|
|
150
|
+
* include async custom validators.
|
|
151
|
+
*
|
|
152
|
+
* @typeParam T - The expected type of `value` after successful validation.
|
|
153
|
+
* @param schema - The schema to validate against.
|
|
154
|
+
* @param value - The value to validate.
|
|
155
|
+
* @param convert - When `true`, the validator coerces values to their target types (e.g. string
|
|
156
|
+
* `"1"` → number `1`). Defaults to `false` to prevent silent type coercion.
|
|
157
|
+
* @returns The validated (and optionally coerced) value typed as `T`.
|
|
158
|
+
* @throws {@link ValidationException} when `value` does not satisfy `schema`.
|
|
159
|
+
*/
|
|
160
|
+
var validateOrThrow = (schema, value, convert = false) => {
|
|
161
|
+
const { value: returnable, error } = schema.validate(value, {
|
|
162
|
+
abortEarly: false,
|
|
163
|
+
convert
|
|
164
|
+
});
|
|
165
|
+
if (error) throw new ValidationException(error);
|
|
166
|
+
return returnable;
|
|
167
|
+
};
|
|
168
|
+
/**
|
|
169
|
+
* Validates `value` against `schema` asynchronously and returns the coerced result typed as `T`.
|
|
170
|
+
*
|
|
171
|
+
* @remarks
|
|
172
|
+
* Collects all field errors before throwing. Prefer this over {@link validateOrThrow} when the
|
|
173
|
+
* schema includes async custom validators.
|
|
174
|
+
*
|
|
175
|
+
* @typeParam T - The expected type of the validated and coerced return value.
|
|
176
|
+
* @param schema - The schema to validate against.
|
|
177
|
+
* @param value - The value to validate.
|
|
178
|
+
* @param convert - When `true`, the validator coerces values to their target types (e.g. string
|
|
179
|
+
* `"1"` → number `1`). Defaults to `false` to prevent silent type coercion.
|
|
180
|
+
* @returns The validated (and optionally coerced) value typed as `T`.
|
|
181
|
+
* @throws {@link ValidationException} when `value` does not satisfy `schema`.
|
|
182
|
+
*/
|
|
183
|
+
var asyncValidateOrThrow = async (schema, value, convert = false) => {
|
|
184
|
+
try {
|
|
185
|
+
return await schema.validateAsync(value, {
|
|
186
|
+
abortEarly: false,
|
|
187
|
+
convert
|
|
188
|
+
});
|
|
189
|
+
} catch (error) {
|
|
190
|
+
if (isValidationError(error)) throw new ValidationException(error);
|
|
191
|
+
throw error;
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
//#endregion
|
|
195
|
+
//#region src/lib/utils/exceptions.ts
|
|
196
|
+
/**
|
|
197
|
+
* Factory that produces a named {@link @nhtio/adk!BaseException} subclass with a fixed printf-style message
|
|
198
|
+
* template, error code, HTTP status, and fatality flag.
|
|
199
|
+
*
|
|
200
|
+
* @remarks
|
|
201
|
+
* Prefer this over hand-writing subclasses for simple, static exception definitions.
|
|
202
|
+
*
|
|
203
|
+
* @typeParam T - Tuple of printf format argument types. Pass a non-empty tuple to require
|
|
204
|
+
* callers to supply interpolation values at the throw site.
|
|
205
|
+
*
|
|
206
|
+
* @param name - The `name` property set on thrown instances (used by {@link isNamedException}).
|
|
207
|
+
* @param message - Printf-style template string for the error message.
|
|
208
|
+
* @param code - Machine-readable error code stored on the static and instance `code` property.
|
|
209
|
+
* @param status - HTTP status code associated with this exception class.
|
|
210
|
+
* @param fatal - When `true`, signals that the error is unrecoverable.
|
|
211
|
+
* @returns A constructor for a {@link @nhtio/adk!BaseException} subclass with the given metadata baked in.
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* ```ts
|
|
215
|
+
* export const E_NOT_FOUND = createException<[string]>(
|
|
216
|
+
* 'E_NOT_FOUND', 'Resource %s not found', 'E_NOT_FOUND', 404, false
|
|
217
|
+
* )
|
|
218
|
+
* throw new E_NOT_FOUND(['my-id'])
|
|
219
|
+
* ```
|
|
220
|
+
*/
|
|
221
|
+
var createException = (name, message, code, status, fatal) => {
|
|
222
|
+
const Ctor = class extends BaseException {
|
|
223
|
+
static message = message;
|
|
224
|
+
static code = code;
|
|
225
|
+
static status = status;
|
|
226
|
+
static fatal = fatal;
|
|
227
|
+
constructor(args, options) {
|
|
228
|
+
const hasMessageArgs = Array.isArray(args);
|
|
229
|
+
const messageArgs = hasMessageArgs ? args : [];
|
|
230
|
+
const errorOptions = hasMessageArgs ? options : args;
|
|
231
|
+
super(printf(message, ...messageArgs), errorOptions);
|
|
232
|
+
this.name = name;
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
Object.defineProperty(Ctor, "name", {
|
|
236
|
+
value: name,
|
|
237
|
+
configurable: true
|
|
238
|
+
});
|
|
239
|
+
return Ctor;
|
|
240
|
+
};
|
|
241
|
+
//#endregion
|
|
242
|
+
export { validateOrThrow as a, passesSchema as i, ValidationException as n, BaseException as o, asyncValidateOrThrow as r, createException as t };
|
|
243
|
+
|
|
244
|
+
//# sourceMappingURL=exceptions-NrzIHw_R.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exceptions-NrzIHw_R.mjs","names":[],"sources":["../src/lib/classes/base_exception.ts","../src/lib/utils/validation.ts","../src/lib/utils/exceptions.ts"],"sourcesContent":["/**\n * Base class for all structured exceptions in the ADK.\n *\n * @remarks\n * Subclasses should declare static `code`, `status`, `fatal`, and optionally `help` to avoid\n * repeating those values on every instance. Instance-level options always take precedence over\n * static defaults, so a single exception class can still be thrown with per-site overrides when\n * needed.\n *\n * The runtime cross-realm guard is inlined here rather than imported from `../utils/guards`\n * to break a circular-import chain: `guards` depends on `validation`, which extends\n * `BaseException`. Importing the shared `isInstanceOf` helper into this file would create a\n * load-order cycle that leaves `BaseException` undefined when `ValidationException extends\n * BaseException` evaluates.\n */\nexport class BaseException extends Error {\n /**\n * Returns `true` if `value` is a {@link BaseException} instance.\n *\n * @remarks\n * Performs cross-realm-safe detection: tries `instanceof`, then `Symbol.hasInstance`, then\n * constructor-name comparison. The ADK does not export the `BaseException` class itself\n * as a constructable value — use this guard plus the {@link BaseException} type for runtime\n * detection and TypeScript narrowing.\n *\n * @param value - The value to test.\n * @returns `true` when `value` is a {@link BaseException} instance.\n */\n public static isBaseException(value: unknown): value is BaseException {\n // eslint-disable-next-line adk/use-is-instance-of -- module cycle (guards ↔ validation ↔ BaseException); the cross-realm fallback is inlined below\n if (value instanceof BaseException) return true\n if (\n typeof BaseException[Symbol.hasInstance] === 'function' &&\n BaseException[Symbol.hasInstance](value)\n )\n return true\n // eslint-disable-next-line adk/prefer-is-object -- module cycle (guards ↔ validation ↔ BaseException); isObject would create a load-order cycle\n if (typeof value === 'object' && value !== null) {\n const ctorName = (value as { constructor?: { name?: string } }).constructor?.name\n if (ctorName === 'BaseException') return true\n }\n return false\n }\n /**\n * Default help text inherited by all instances unless overridden at the throw site.\n */\n declare static help?: string\n /**\n * Default machine-readable error code inherited by all instances.\n */\n declare static code?: string\n /**\n * Default HTTP status code inherited by all instances.\n */\n declare static status?: number\n /**\n * Whether exceptions of this class are fatal by default.\n */\n declare static fatal?: boolean\n /**\n * Default message used when no message is supplied to the constructor.\n */\n declare static message?: string\n\n /**\n * Name of the class that raised the exception.\n */\n name: string\n\n /**\n * Human-readable guidance for resolving or reporting this error.\n */\n declare help?: string\n\n /**\n * Machine-readable error code for narrowing exception-handling logic.\n */\n declare code?: string\n\n /**\n * HTTP status code associated with this error.\n */\n declare status?: number\n\n /**\n * When `true`, the ADK treats this error as unrecoverable and should halt the agent loop.\n */\n declare fatal?: boolean\n\n /**\n * @param message - Human-readable error message. Falls back to the static `message` on the\n * subclass if omitted.\n * @param options - Standard `ErrorOptions` extended with `code`, `status`, and `fatal`\n * overrides. Static defaults on the subclass are used when these are absent.\n */\n constructor(\n message?: string,\n options?: ErrorOptions & { code?: string; status?: number; fatal?: boolean }\n ) {\n super(message, options)\n\n const ErrorConstructor = this.constructor as typeof BaseException\n\n this.name = ErrorConstructor.name\n this.message = message || ErrorConstructor.message || ''\n\n const code = options?.code || ErrorConstructor.code\n if (code !== undefined) {\n this.code = code\n }\n\n const status = options?.status || ErrorConstructor.status\n if (status !== undefined) {\n this.status = status\n }\n\n const fatal = options?.fatal ?? ErrorConstructor.fatal\n if (fatal !== undefined) {\n this.fatal = fatal\n }\n\n const help = ErrorConstructor.help\n if (help !== undefined) {\n this.help = help\n }\n\n Error.captureStackTrace(this, ErrorConstructor)\n }\n\n get [Symbol.toStringTag]() {\n return this.constructor.name\n }\n\n toString() {\n if (this.code) {\n return `${this.name} [${this.code}]: ${this.message}`\n }\n return `${this.name}: ${this.message}`\n }\n}\n","import { BaseException } from '../classes/base_exception'\nimport { validator, ValidationError } from '@nhtio/validation'\nimport type { Schema } from '@nhtio/validation'\n\n/**\n * Returns `true` if `value` satisfies `schema` without throwing.\n *\n * @remarks\n * Aborts on the first validation error. Use {@link validateOrThrow} or\n * {@link asyncValidateOrThrow} when you need the full set of field errors.\n *\n * @param schema - The schema to validate against.\n * @param value - The value to test.\n * @returns `true` when `value` passes the schema; `false` otherwise.\n */\nexport const passesSchema = (schema: Schema, value: unknown): boolean => {\n const { error } = schema.validate(value, { abortEarly: true })\n return !error\n}\n\n/**\n * Returns `true` if `value` is a `ValidationError` or satisfies its minimum duck-type shape.\n *\n * @remarks\n * The duck-typing path handles `ValidationError` objects that cross module or realm boundaries\n * where `instanceof` would return `false`.\n *\n * @param value - The value to test.\n * @returns `true` when `value` conforms to the `ValidationError` shape.\n */\nexport const isValidationError = (value: unknown): value is ValidationError => {\n const schema = validator.alternatives(\n validator.object().instance(ValidationError as any),\n validator.function().instance(ValidationError as any),\n validator\n .object({\n message: validator.string().required(),\n details: validator\n .array()\n .items(\n validator.object({\n message: validator.string().required(),\n path: validator\n .array()\n .items(validator.alternatives(validator.string(), validator.number()))\n .required(),\n type: validator.string().required(),\n context: validator.object().unknown(true).required(),\n })\n )\n .required(),\n })\n .unknown(true)\n )\n return passesSchema(schema, value)\n}\n\nconst messageFromValidationError = (reason: ValidationError | undefined, fallback: string) => {\n return reason ? reason.details.map((d) => d.message).join(' and ') : fallback\n}\n\n/**\n * Thrown when input fails schema validation.\n *\n * @remarks\n * Carries the full `details` array from the underlying `ValidationError` so callers can surface\n * field-level messages without unwrapping the `cause` manually.\n */\nexport class ValidationException extends BaseException {\n static status = 422\n static code = 'VALIDATION_EXCEPTION'\n static fatal = false\n\n /** The raw field-level error details from the underlying `ValidationError`. */\n declare readonly details?: ValidationError['details']\n\n /**\n * @param reason - The `ValidationError` thrown by the schema; its `details` are surfaced\n * directly on this exception and its messages are joined to form the human-readable message.\n */\n constructor(reason: ValidationError) {\n const message = messageFromValidationError(reason, 'Validation failed')\n super(message, {\n code: ValidationException.code,\n status: ValidationException.status,\n fatal: ValidationException.fatal,\n cause: reason,\n })\n Object.defineProperty(this, 'details', {\n value: reason.details,\n enumerable: true,\n configurable: false,\n writable: false,\n })\n }\n}\n\n/**\n * Validates `value` against `schema` synchronously and returns the coerced result typed as `T`.\n *\n * @remarks\n * Collects all field errors before throwing. Use {@link asyncValidateOrThrow} for schemas that\n * include async custom validators.\n *\n * @typeParam T - The expected type of `value` after successful validation.\n * @param schema - The schema to validate against.\n * @param value - The value to validate.\n * @param convert - When `true`, the validator coerces values to their target types (e.g. string\n * `\"1\"` → number `1`). Defaults to `false` to prevent silent type coercion.\n * @returns The validated (and optionally coerced) value typed as `T`.\n * @throws {@link ValidationException} when `value` does not satisfy `schema`.\n */\nexport const validateOrThrow = <T>(schema: Schema, value: unknown, convert: boolean = false): T => {\n const { value: returnable, error } = schema.validate(value, { abortEarly: false, convert })\n if (error) {\n throw new ValidationException(error)\n }\n return returnable as T\n}\n\n/**\n * Validates `value` against `schema` asynchronously and returns the coerced result typed as `T`.\n *\n * @remarks\n * Collects all field errors before throwing. Prefer this over {@link validateOrThrow} when the\n * schema includes async custom validators.\n *\n * @typeParam T - The expected type of the validated and coerced return value.\n * @param schema - The schema to validate against.\n * @param value - The value to validate.\n * @param convert - When `true`, the validator coerces values to their target types (e.g. string\n * `\"1\"` → number `1`). Defaults to `false` to prevent silent type coercion.\n * @returns The validated (and optionally coerced) value typed as `T`.\n * @throws {@link ValidationException} when `value` does not satisfy `schema`.\n */\nexport const asyncValidateOrThrow = async <T>(\n schema: Schema,\n value: unknown,\n convert: boolean = false\n): Promise<T> => {\n try {\n return await schema.validateAsync(value, { abortEarly: false, convert })\n } catch (error) {\n if (isValidationError(error)) {\n throw new ValidationException(error)\n }\n throw error\n }\n}\n","import { passesSchema } from './validation'\nimport { validator } from '@nhtio/validation'\nimport { printf as format } from 'fast-printf'\nimport { BaseException } from '../classes/base_exception'\n\n/**\n * Options accepted by {@link @nhtio/adk!BaseException} (and factory-created exceptions) beyond the\n * standard `ErrorOptions`.\n *\n * @remarks\n * These mirror the static defaults on {@link @nhtio/adk!BaseException} but allow per-throw overrides so a\n * single exception class can carry different metadata at different throw sites.\n */\nexport type ExceptionOptions = ErrorOptions & {\n code?: string\n status?: number\n fatal?: boolean\n}\n\n/**\n * Constructor signature of an exception class produced by {@link createException}.\n *\n * @typeParam T - Tuple of printf-style format argument types. When `T` is an empty tuple the\n * constructor takes no positional message arguments; when non-empty the first argument must be\n * an array of values matching `T`.\n */\nexport type CreatedException<T extends any[] = []> = typeof BaseException &\n (T extends []\n ? {\n new (options?: ExceptionOptions): BaseException\n }\n : { new (args: T, options?: ExceptionOptions): BaseException })\n\n/**\n * Factory that produces a named {@link @nhtio/adk!BaseException} subclass with a fixed printf-style message\n * template, error code, HTTP status, and fatality flag.\n *\n * @remarks\n * Prefer this over hand-writing subclasses for simple, static exception definitions.\n *\n * @typeParam T - Tuple of printf format argument types. Pass a non-empty tuple to require\n * callers to supply interpolation values at the throw site.\n *\n * @param name - The `name` property set on thrown instances (used by {@link isNamedException}).\n * @param message - Printf-style template string for the error message.\n * @param code - Machine-readable error code stored on the static and instance `code` property.\n * @param status - HTTP status code associated with this exception class.\n * @param fatal - When `true`, signals that the error is unrecoverable.\n * @returns A constructor for a {@link @nhtio/adk!BaseException} subclass with the given metadata baked in.\n *\n * @example\n * ```ts\n * export const E_NOT_FOUND = createException<[string]>(\n * 'E_NOT_FOUND', 'Resource %s not found', 'E_NOT_FOUND', 404, false\n * )\n * throw new E_NOT_FOUND(['my-id'])\n * ```\n */\nexport const createException = <T extends any[] = []>(\n name: string,\n message: string,\n code: string,\n status?: number,\n fatal?: boolean\n): CreatedException<T> => {\n const Ctor = class extends BaseException {\n static message = message\n static code = code\n static status = status\n static fatal = fatal\n constructor(args?: T | ExceptionOptions, options?: ExceptionOptions) {\n const hasMessageArgs = Array.isArray(args)\n const messageArgs = hasMessageArgs ? args : []\n const errorOptions = hasMessageArgs ? options : args\n\n super(format(message, ...messageArgs), errorOptions)\n this.name = name\n }\n }\n // Without this, the factory returns an anonymous class — constructor.name is \"\" and\n // cross-realm `isInstanceOf(err, 'E_FOO')` (which falls back to constructor-name comparison)\n // never matches. Setting the name on the class itself makes the identity carry through.\n Object.defineProperty(Ctor, 'name', { value: name, configurable: true })\n return Ctor as unknown as CreatedException<T>\n}\n\n/**\n * Returns `true` if `value` is a {@link @nhtio/adk!BaseException} or satisfies its minimum duck-type shape.\n *\n * @remarks\n * The duck-typing path handles exceptions that cross module or realm boundaries where\n * `instanceof` would return `false` for structurally identical objects.\n *\n * @param value - The value to test.\n * @returns `true` when `value` conforms to the {@link @nhtio/adk!BaseException} shape.\n */\nexport const isException = (value: unknown): value is BaseException => {\n const schema = validator.alternatives(\n validator.object().instance(BaseException as any),\n validator.function().instance(BaseException as any),\n validator\n .object({\n name: validator.string().required(),\n message: validator.string().required(),\n help: validator.string().optional(),\n code: validator.string().optional(),\n status: validator.number().optional(),\n fatal: validator.boolean().optional(),\n })\n .unknown(true)\n )\n return passesSchema(schema, value)\n}\n\n/**\n * Narrows `value` to a {@link @nhtio/adk!BaseException} whose `name` property matches `name` exactly.\n *\n * @remarks\n * Useful for catching a specific factory-created exception by its string identifier when\n * `instanceof` checks are not available (e.g. across module boundaries).\n *\n * @param value - The value to test.\n * @param name - The exact string to compare against `value.name`.\n * @returns `true` when `value` is a {@link @nhtio/adk!BaseException} with the given `name`.\n */\nexport const isNamedException = (value: unknown, name: string): value is BaseException => {\n return isException(value) && value.name === name\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAeA,IAAa,gBAAb,MAAa,sBAAsB,MAAM;;;;;;;;;;;;;CAavC,OAAc,gBAAgB,OAAwC;EAEpE,IAAI,iBAAiB,eAAe,OAAO;EAC3C,IACE,OAAO,cAAc,OAAO,iBAAiB,cAC7C,cAAc,OAAO,aAAa,KAAK,GAEvC,OAAO;EAET,IAAI,OAAO,UAAU,YAAY,UAAU;OACvB,MAA8C,aAAa,SAC5D,iBAAiB,OAAO;EAAA;EAE3C,OAAO;CACT;;;;CAyBA;;;;;;;CA4BA,YACE,SACA,SACA;EACA,MAAM,SAAS,OAAO;EAEtB,MAAM,mBAAmB,KAAK;EAE9B,KAAK,OAAO,iBAAiB;EAC7B,KAAK,UAAU,WAAW,iBAAiB,WAAW;EAEtD,MAAM,O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;GAC7C,MAAM,eAAe,iBAAiB,UAAU;GAEhD,MAAM,OAAO,SAAS,GAAG,WAAW,GAAG,YAAY;GACnD,KAAK,OAAO;EACd;CACF;CAIA,OAAO,eAAe,MAAM,QAAQ;EAAE,OAAO;EAAM,cAAc;CAAK,CAAC;CACvE,OAAO;AACT"}
|
package/exceptions.cjs
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_exceptions = require("./exceptions-D5YrO9Vm.js");
|
|
3
|
+
const require_runtime = require("./runtime-BJVkrGQe.js");
|
|
4
|
+
exports.E_DISPATCH_PIPELINE_ERROR = require_runtime.E_DISPATCH_PIPELINE_ERROR;
|
|
5
|
+
exports.E_INPUT_PIPELINE_ERROR = require_runtime.E_INPUT_PIPELINE_ERROR;
|
|
6
|
+
exports.E_INVALID_INITIAL_IDENTITY_VALUE = require_runtime.E_INVALID_INITIAL_IDENTITY_VALUE;
|
|
7
|
+
exports.E_INVALID_INITIAL_MEDIA_VALUE = require_runtime.E_INVALID_INITIAL_MEDIA_VALUE;
|
|
8
|
+
exports.E_INVALID_INITIAL_MEMORY_VALUE = require_runtime.E_INVALID_INITIAL_MEMORY_VALUE;
|
|
9
|
+
exports.E_INVALID_INITIAL_MESSAGE_VALUE = require_runtime.E_INVALID_INITIAL_MESSAGE_VALUE;
|
|
10
|
+
exports.E_INVALID_INITIAL_REGISTRY_VALUE = require_runtime.E_INVALID_INITIAL_REGISTRY_VALUE;
|
|
11
|
+
exports.E_INVALID_INITIAL_RETRIEVABLE_VALUE = require_runtime.E_INVALID_INITIAL_RETRIEVABLE_VALUE;
|
|
12
|
+
exports.E_INVALID_INITIAL_THOUGHT_VALUE = require_runtime.E_INVALID_INITIAL_THOUGHT_VALUE;
|
|
13
|
+
exports.E_INVALID_INITIAL_TOOL_CALL_VALUE = require_runtime.E_INVALID_INITIAL_TOOL_CALL_VALUE;
|
|
14
|
+
exports.E_INVALID_INITIAL_TOOL_VALUE = require_runtime.E_INVALID_INITIAL_TOOL_VALUE;
|
|
15
|
+
exports.E_INVALID_INITIAL_TURN_GATE_VALUE = require_runtime.E_INVALID_INITIAL_TURN_GATE_VALUE;
|
|
16
|
+
exports.E_INVALID_LLM_DISPATCH_INPUT = require_runtime.E_INVALID_LLM_DISPATCH_INPUT;
|
|
17
|
+
exports.E_INVALID_LLM_EXECUTION_CONTEXT = require_runtime.E_INVALID_LLM_EXECUTION_CONTEXT;
|
|
18
|
+
exports.E_INVALID_TOOL_ARGS = require_runtime.E_INVALID_TOOL_ARGS;
|
|
19
|
+
exports.E_INVALID_TURN_CONTEXT = require_runtime.E_INVALID_TURN_CONTEXT;
|
|
20
|
+
exports.E_INVALID_TURN_GATE_RESOLUTION = require_runtime.E_INVALID_TURN_GATE_RESOLUTION;
|
|
21
|
+
exports.E_INVALID_TURN_RUNNER_CONFIG = require_runtime.E_INVALID_TURN_RUNNER_CONFIG;
|
|
22
|
+
exports.E_LLM_EXECUTION_ALREADY_SIGNALLED = require_runtime.E_LLM_EXECUTION_ALREADY_SIGNALLED;
|
|
23
|
+
exports.E_LLM_EXECUTION_EXECUTOR_ERROR = require_runtime.E_LLM_EXECUTION_EXECUTOR_ERROR;
|
|
24
|
+
exports.E_LLM_EXECUTION_GATE_NOT_SUPPORTED = require_runtime.E_LLM_EXECUTION_GATE_NOT_SUPPORTED;
|
|
25
|
+
exports.E_NOT_A_MEDIA_READER = require_runtime.E_NOT_A_MEDIA_READER;
|
|
26
|
+
exports.E_NOT_A_SPOOL_READER = require_runtime.E_NOT_A_SPOOL_READER;
|
|
27
|
+
exports.E_OUTPUT_PIPELINE_ERROR = require_runtime.E_OUTPUT_PIPELINE_ERROR;
|
|
28
|
+
exports.E_PIPELINE_SHORT_CIRCUITED = require_runtime.E_PIPELINE_SHORT_CIRCUITED;
|
|
29
|
+
exports.E_TOOL_ALREADY_REGISTERED = require_runtime.E_TOOL_ALREADY_REGISTERED;
|
|
30
|
+
exports.E_TOOL_DOWNSTREAM_ERROR = require_runtime.E_TOOL_DOWNSTREAM_ERROR;
|
|
31
|
+
exports.E_TURN_GATE_ABORTED = require_runtime.E_TURN_GATE_ABORTED;
|
|
32
|
+
exports.E_TURN_GATE_TIMEOUT = require_runtime.E_TURN_GATE_TIMEOUT;
|
|
33
|
+
exports.ValidationException = require_exceptions.ValidationException;
|