@copilotkit/shared 1.51.5-next.0 → 1.51.5-next.2
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/CHANGELOG.md +4 -0
- package/dist/_virtual/_rolldown/runtime.cjs +29 -0
- package/dist/constants/index.cjs +13 -0
- package/dist/constants/index.cjs.map +1 -0
- package/dist/constants/{index.d.ts → index.d.cts} +3 -1
- package/dist/constants/index.d.cts.map +1 -0
- package/dist/constants/index.d.mts +8 -0
- package/dist/constants/index.d.mts.map +1 -0
- package/dist/constants/index.mjs +8 -12
- package/dist/constants/index.mjs.map +1 -1
- package/dist/index.cjs +64 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +22 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +23 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +16 -123
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +1017 -2
- package/dist/index.umd.js.map +1 -1
- package/dist/package.cjs +12 -0
- package/dist/package.cjs.map +1 -0
- package/dist/package.mjs +6 -0
- package/dist/package.mjs.map +1 -0
- package/dist/telemetry/events.d.cts +47 -0
- package/dist/telemetry/events.d.cts.map +1 -0
- package/dist/telemetry/events.d.mts +47 -0
- package/dist/telemetry/events.d.mts.map +1 -0
- package/dist/telemetry/index.d.mts +1 -0
- package/dist/telemetry/scarf-client.cjs +29 -0
- package/dist/telemetry/scarf-client.cjs.map +1 -0
- package/dist/telemetry/scarf-client.mjs +27 -5
- package/dist/telemetry/scarf-client.mjs.map +1 -1
- package/dist/telemetry/telemetry-client.cjs +87 -0
- package/dist/telemetry/telemetry-client.cjs.map +1 -0
- package/dist/telemetry/telemetry-client.d.cts +48 -0
- package/dist/telemetry/telemetry-client.d.cts.map +1 -0
- package/dist/telemetry/telemetry-client.d.mts +48 -0
- package/dist/telemetry/telemetry-client.d.mts.map +1 -0
- package/dist/telemetry/telemetry-client.mjs +83 -9
- package/dist/telemetry/telemetry-client.mjs.map +1 -1
- package/dist/telemetry/utils.cjs +16 -0
- package/dist/telemetry/utils.cjs.map +1 -0
- package/dist/telemetry/utils.mjs +14 -8
- package/dist/telemetry/utils.mjs.map +1 -1
- package/dist/types/{action.d.ts → action.d.cts} +32 -32
- package/dist/types/action.d.cts.map +1 -0
- package/dist/types/action.d.mts +59 -0
- package/dist/types/action.d.mts.map +1 -0
- package/dist/types/copilot-cloud-config.d.cts +15 -0
- package/dist/types/copilot-cloud-config.d.cts.map +1 -0
- package/dist/types/copilot-cloud-config.d.mts +15 -0
- package/dist/types/copilot-cloud-config.d.mts.map +1 -0
- package/dist/types/error.d.cts +57 -0
- package/dist/types/error.d.cts.map +1 -0
- package/dist/types/error.d.mts +57 -0
- package/dist/types/error.d.mts.map +1 -0
- package/dist/types/{message.d.ts → message.d.cts} +14 -12
- package/dist/types/message.d.cts.map +1 -0
- package/dist/types/message.d.mts +31 -0
- package/dist/types/message.d.mts.map +1 -0
- package/dist/types/openai-assistant.d.cts +55 -0
- package/dist/types/openai-assistant.d.cts.map +1 -0
- package/dist/types/openai-assistant.d.mts +55 -0
- package/dist/types/openai-assistant.d.mts.map +1 -0
- package/dist/types/utility.d.cts +6 -0
- package/dist/types/utility.d.cts.map +1 -0
- package/dist/types/utility.d.mts +6 -0
- package/dist/types/utility.d.mts.map +1 -0
- package/dist/utils/conditions.cjs +32 -0
- package/dist/utils/conditions.cjs.map +1 -0
- package/dist/utils/{conditions.d.ts → conditions.d.cts} +16 -11
- package/dist/utils/conditions.d.cts.map +1 -0
- package/dist/utils/conditions.d.mts +31 -0
- package/dist/utils/conditions.d.mts.map +1 -0
- package/dist/utils/conditions.mjs +30 -6
- package/dist/utils/conditions.mjs.map +1 -1
- package/dist/utils/console-styling.cjs +91 -0
- package/dist/utils/console-styling.cjs.map +1 -0
- package/dist/utils/console-styling.d.cts +65 -0
- package/dist/utils/console-styling.d.cts.map +1 -0
- package/dist/utils/console-styling.d.mts +65 -0
- package/dist/utils/console-styling.d.mts.map +1 -0
- package/dist/utils/console-styling.mjs +83 -15
- package/dist/utils/console-styling.mjs.map +1 -1
- package/dist/utils/errors.cjs +439 -0
- package/dist/utils/errors.cjs.map +1 -0
- package/dist/utils/{errors.d.ts → errors.d.cts} +188 -156
- package/dist/utils/errors.d.cts.map +1 -0
- package/dist/utils/errors.d.mts +312 -0
- package/dist/utils/errors.d.mts.map +1 -0
- package/dist/utils/errors.mjs +416 -63
- package/dist/utils/errors.mjs.map +1 -1
- package/dist/utils/index.cjs +50 -0
- package/dist/utils/index.cjs.map +1 -0
- package/dist/utils/{index.d.ts → index.d.cts} +10 -11
- package/dist/utils/index.d.cts.map +1 -0
- package/dist/utils/index.d.mts +31 -0
- package/dist/utils/index.d.mts.map +1 -0
- package/dist/utils/index.mjs +47 -107
- package/dist/utils/index.mjs.map +1 -1
- package/dist/utils/json-schema.cjs +157 -0
- package/dist/utils/json-schema.cjs.map +1 -0
- package/dist/utils/{json-schema.d.ts → json-schema.d.cts} +19 -17
- package/dist/utils/json-schema.d.cts.map +1 -0
- package/dist/utils/json-schema.d.mts +36 -0
- package/dist/utils/json-schema.d.mts.map +1 -0
- package/dist/utils/json-schema.mjs +152 -12
- package/dist/utils/json-schema.mjs.map +1 -1
- package/dist/utils/random-id.cjs +38 -0
- package/dist/utils/random-id.cjs.map +1 -0
- package/dist/utils/{random-id.d.ts → random-id.d.cts} +3 -1
- package/dist/utils/random-id.d.cts.map +1 -0
- package/dist/utils/random-id.d.mts +8 -0
- package/dist/utils/random-id.d.mts.map +1 -0
- package/dist/utils/random-id.mjs +33 -12
- package/dist/utils/random-id.mjs.map +1 -1
- package/dist/utils/requests.cjs +58 -0
- package/dist/utils/requests.cjs.map +1 -0
- package/dist/utils/{requests.d.ts → requests.d.cts} +3 -1
- package/dist/utils/requests.d.cts.map +1 -0
- package/dist/utils/requests.d.mts +12 -0
- package/dist/utils/requests.d.mts.map +1 -0
- package/dist/utils/requests.mjs +56 -6
- package/dist/utils/requests.mjs.map +1 -1
- package/dist/utils/{types.d.ts → types.d.cts} +9 -7
- package/dist/utils/types.d.cts.map +1 -0
- package/dist/utils/types.d.mts +22 -0
- package/dist/utils/types.d.mts.map +1 -0
- package/package.json +16 -14
- package/tsdown.config.ts +36 -0
- package/vitest.config.mjs +11 -0
- package/dist/chunk-2OJ5OJ2D.mjs +0 -1
- package/dist/chunk-2OJ5OJ2D.mjs.map +0 -1
- package/dist/chunk-3DK3UMRW.mjs +0 -217
- package/dist/chunk-3DK3UMRW.mjs.map +0 -1
- package/dist/chunk-7KOT4KP4.mjs +0 -435
- package/dist/chunk-7KOT4KP4.mjs.map +0 -1
- package/dist/chunk-DE5XAMMI.mjs +0 -110
- package/dist/chunk-DE5XAMMI.mjs.map +0 -1
- package/dist/chunk-FCCOSO5L.mjs +0 -1
- package/dist/chunk-FCCOSO5L.mjs.map +0 -1
- package/dist/chunk-GS5BLXSZ.mjs +0 -41
- package/dist/chunk-GS5BLXSZ.mjs.map +0 -1
- package/dist/chunk-IAFBVORQ.mjs +0 -1
- package/dist/chunk-IAFBVORQ.mjs.map +0 -1
- package/dist/chunk-M2RJI5BZ.mjs +0 -94
- package/dist/chunk-M2RJI5BZ.mjs.map +0 -1
- package/dist/chunk-MSUB6DGR.mjs +0 -1
- package/dist/chunk-MSUB6DGR.mjs.map +0 -1
- package/dist/chunk-N5EP5OD5.mjs +0 -1
- package/dist/chunk-N5EP5OD5.mjs.map +0 -1
- package/dist/chunk-NAFEBKSO.mjs +0 -1
- package/dist/chunk-NAFEBKSO.mjs.map +0 -1
- package/dist/chunk-P7STFMPO.mjs +0 -1
- package/dist/chunk-P7STFMPO.mjs.map +0 -1
- package/dist/chunk-UYARLQNA.mjs +0 -1
- package/dist/chunk-UYARLQNA.mjs.map +0 -1
- package/dist/chunk-V7BSERGI.mjs +0 -62
- package/dist/chunk-V7BSERGI.mjs.map +0 -1
- package/dist/chunk-WAZHII6M.mjs +0 -67
- package/dist/chunk-WAZHII6M.mjs.map +0 -1
- package/dist/chunk-XTHC46M2.mjs +0 -1
- package/dist/chunk-XTHC46M2.mjs.map +0 -1
- package/dist/chunk-ZI7ZVRL3.mjs +0 -38
- package/dist/chunk-ZI7ZVRL3.mjs.map +0 -1
- package/dist/chunk-ZIC6V6S5.mjs +0 -13
- package/dist/chunk-ZIC6V6S5.mjs.map +0 -1
- package/dist/chunk-ZUE2VR7D.mjs +0 -38
- package/dist/chunk-ZUE2VR7D.mjs.map +0 -1
- package/dist/constants/index.js +0 -40
- package/dist/constants/index.js.map +0 -1
- package/dist/index.d.ts +0 -25
- package/dist/index.js +0 -1138
- package/dist/index.js.map +0 -1
- package/dist/telemetry/events.d.ts +0 -45
- package/dist/telemetry/events.js +0 -19
- package/dist/telemetry/events.js.map +0 -1
- package/dist/telemetry/events.mjs +0 -1
- package/dist/telemetry/events.mjs.map +0 -1
- package/dist/telemetry/index.d.ts +0 -3
- package/dist/telemetry/index.js +0 -191
- package/dist/telemetry/index.js.map +0 -1
- package/dist/telemetry/index.mjs +0 -12
- package/dist/telemetry/index.mjs.map +0 -1
- package/dist/telemetry/scarf-client.d.ts +0 -7
- package/dist/telemetry/scarf-client.js +0 -59
- package/dist/telemetry/scarf-client.js.map +0 -1
- package/dist/telemetry/security-check.d.ts +0 -2
- package/dist/telemetry/security-check.js +0 -2
- package/dist/telemetry/security-check.js.map +0 -1
- package/dist/telemetry/security-check.mjs +0 -1
- package/dist/telemetry/security-check.mjs.map +0 -1
- package/dist/telemetry/telemetry-client.d.ts +0 -40
- package/dist/telemetry/telemetry-client.js +0 -189
- package/dist/telemetry/telemetry-client.js.map +0 -1
- package/dist/telemetry/utils.d.ts +0 -8
- package/dist/telemetry/utils.js +0 -73
- package/dist/telemetry/utils.js.map +0 -1
- package/dist/types/action.js +0 -19
- package/dist/types/action.js.map +0 -1
- package/dist/types/action.mjs +0 -2
- package/dist/types/action.mjs.map +0 -1
- package/dist/types/copilot-cloud-config.d.ts +0 -13
- package/dist/types/copilot-cloud-config.js +0 -19
- package/dist/types/copilot-cloud-config.js.map +0 -1
- package/dist/types/copilot-cloud-config.mjs +0 -2
- package/dist/types/copilot-cloud-config.mjs.map +0 -1
- package/dist/types/error.d.ts +0 -55
- package/dist/types/error.js +0 -19
- package/dist/types/error.js.map +0 -1
- package/dist/types/error.mjs +0 -2
- package/dist/types/error.mjs.map +0 -1
- package/dist/types/index.d.ts +0 -7
- package/dist/types/index.js +0 -19
- package/dist/types/index.js.map +0 -1
- package/dist/types/index.mjs +0 -8
- package/dist/types/index.mjs.map +0 -1
- package/dist/types/message.js +0 -19
- package/dist/types/message.js.map +0 -1
- package/dist/types/message.mjs +0 -2
- package/dist/types/message.mjs.map +0 -1
- package/dist/types/openai-assistant.d.ts +0 -53
- package/dist/types/openai-assistant.js +0 -19
- package/dist/types/openai-assistant.js.map +0 -1
- package/dist/types/openai-assistant.mjs +0 -2
- package/dist/types/openai-assistant.mjs.map +0 -1
- package/dist/types/utility.d.ts +0 -6
- package/dist/types/utility.js +0 -19
- package/dist/types/utility.js.map +0 -1
- package/dist/types/utility.mjs +0 -2
- package/dist/types/utility.mjs.map +0 -1
- package/dist/utils/conditions.js +0 -86
- package/dist/utils/conditions.js.map +0 -1
- package/dist/utils/console-styling.d.ts +0 -84
- package/dist/utils/console-styling.js +0 -123
- package/dist/utils/console-styling.js.map +0 -1
- package/dist/utils/errors.js +0 -452
- package/dist/utils/errors.js.map +0 -1
- package/dist/utils/index.js +0 -964
- package/dist/utils/index.js.map +0 -1
- package/dist/utils/json-schema.js +0 -244
- package/dist/utils/json-schema.js.map +0 -1
- package/dist/utils/random-id.js +0 -68
- package/dist/utils/random-id.js.map +0 -1
- package/dist/utils/requests.js +0 -91
- package/dist/utils/requests.js.map +0 -1
- package/dist/utils/types.js +0 -19
- package/dist/utils/types.js.map +0 -1
- package/dist/utils/types.mjs +0 -2
- package/dist/utils/types.mjs.map +0 -1
- package/jest.config.js +0 -5
- package/rollup.config.mjs +0 -54
- package/tsup.config.ts +0 -15
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { BaseCondition, ComparisonCondition, ComparisonRule, Condition, ExistenceCondition, ExistenceRule, LogicalCondition, LogicalRule, Rule, executeConditions } from "./conditions.mjs";
|
|
2
|
+
import { ConsoleColors, ConsoleStyles, logCopilotKitPlatformMessage, logStyled, publicApiKeyRequired, styledConsole } from "./console-styling.mjs";
|
|
3
|
+
import { BANNER_ERROR_NAMES, COPILOT_CLOUD_ERROR_NAMES, ConfigurationError, CopilotKitAgentDiscoveryError, CopilotKitApiDiscoveryError, CopilotKitError, CopilotKitErrorCode, CopilotKitLowLevelError, CopilotKitMisuseError, CopilotKitRemoteEndpointDiscoveryError, CopilotKitVersionMismatchError, ERROR_CONFIG, ERROR_NAMES, ErrorVisibility, MissingPublicApiKeyError, ResolvedCopilotKitError, Severity, UpgradeRequiredError, ensureStructuredError, getPossibleVersionMismatch, isStructuredCopilotKitError } from "./errors.mjs";
|
|
4
|
+
import { JSONSchema, JSONSchemaArray, JSONSchemaBoolean, JSONSchemaNumber, JSONSchemaObject, JSONSchemaString, actionParametersToJsonSchema, convertJsonSchemaToZodSchema, getZodParameters, jsonSchemaToActionParameters } from "./json-schema.mjs";
|
|
5
|
+
import { AgentDescription, MaybePromise, NonEmptyRecord, RuntimeInfo } from "./types.mjs";
|
|
6
|
+
import { dataToUUID, isValidUUID, randomId, randomUUID } from "./random-id.mjs";
|
|
7
|
+
import { readBody } from "./requests.mjs";
|
|
8
|
+
|
|
9
|
+
//#region src/utils/index.d.ts
|
|
10
|
+
/**
|
|
11
|
+
* Safely parses a JSON string into an object
|
|
12
|
+
* @param json The JSON string to parse
|
|
13
|
+
* @param fallback Optional fallback value to return if parsing fails. If not provided or set to "unset", returns null
|
|
14
|
+
* @returns The parsed JSON object, or the fallback value (or null) if parsing fails
|
|
15
|
+
*/
|
|
16
|
+
declare function parseJson(json: string, fallback?: any): any;
|
|
17
|
+
/**
|
|
18
|
+
* Maps an array of items to a new array, skipping items that throw errors during mapping
|
|
19
|
+
* @param items The array to map
|
|
20
|
+
* @param callback The mapping function to apply to each item
|
|
21
|
+
* @returns A new array containing only the successfully mapped items
|
|
22
|
+
*/
|
|
23
|
+
declare function tryMap<TItem, TMapped>(items: TItem[], callback: (item: TItem, index: number, array: TItem[]) => TMapped): TMapped[];
|
|
24
|
+
/**
|
|
25
|
+
* Checks if the current environment is macOS
|
|
26
|
+
* @returns {boolean} True if running on macOS, false otherwise
|
|
27
|
+
*/
|
|
28
|
+
declare function isMacOS(): boolean;
|
|
29
|
+
//#endregion
|
|
30
|
+
export { isMacOS, parseJson, tryMap };
|
|
31
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/utils/index.ts"],"mappings":";;;;;;;;;;;AAcA;;;;iBAAgB,SAAA,CAAU,IAAA,UAAc,QAAA;AAcxC;;;;;;AAAA,iBAAgB,MAAA,gBAAA,CACd,KAAA,EAAO,KAAA,IACP,QAAA,GAAW,IAAA,EAAM,KAAA,EAAO,KAAA,UAAe,KAAA,EAAO,KAAA,OAAY,OAAA,GACzD,OAAA;;;;;iBAea,OAAA,CAAA"}
|
package/dist/utils/index.mjs
CHANGED
|
@@ -1,108 +1,48 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
getZodParameters,
|
|
49
|
-
jsonSchemaToActionParameters
|
|
50
|
-
} from "../chunk-3DK3UMRW.mjs";
|
|
51
|
-
import {
|
|
52
|
-
dataToUUID,
|
|
53
|
-
isValidUUID,
|
|
54
|
-
randomId,
|
|
55
|
-
randomUUID
|
|
56
|
-
} from "../chunk-GS5BLXSZ.mjs";
|
|
57
|
-
import {
|
|
58
|
-
readBody
|
|
59
|
-
} from "../chunk-WAZHII6M.mjs";
|
|
60
|
-
import "../chunk-XTHC46M2.mjs";
|
|
61
|
-
import "../chunk-ZIC6V6S5.mjs";
|
|
62
|
-
import "../chunk-P7STFMPO.mjs";
|
|
63
|
-
import "../chunk-DE5XAMMI.mjs";
|
|
64
|
-
import "../chunk-ZI7ZVRL3.mjs";
|
|
65
|
-
import "../chunk-ZUE2VR7D.mjs";
|
|
66
|
-
export {
|
|
67
|
-
BANNER_ERROR_NAMES,
|
|
68
|
-
COPILOT_CLOUD_ERROR_NAMES,
|
|
69
|
-
ConfigurationError,
|
|
70
|
-
ConsoleColors,
|
|
71
|
-
ConsoleStyles,
|
|
72
|
-
CopilotKitAgentDiscoveryError,
|
|
73
|
-
CopilotKitApiDiscoveryError,
|
|
74
|
-
CopilotKitError,
|
|
75
|
-
CopilotKitErrorCode,
|
|
76
|
-
CopilotKitLowLevelError,
|
|
77
|
-
CopilotKitMisuseError,
|
|
78
|
-
CopilotKitRemoteEndpointDiscoveryError,
|
|
79
|
-
CopilotKitVersionMismatchError,
|
|
80
|
-
ERROR_CONFIG,
|
|
81
|
-
ERROR_NAMES,
|
|
82
|
-
ErrorVisibility,
|
|
83
|
-
MissingPublicApiKeyError,
|
|
84
|
-
ResolvedCopilotKitError,
|
|
85
|
-
Severity,
|
|
86
|
-
UpgradeRequiredError,
|
|
87
|
-
actionParametersToJsonSchema,
|
|
88
|
-
convertJsonSchemaToZodSchema,
|
|
89
|
-
dataToUUID,
|
|
90
|
-
ensureStructuredError,
|
|
91
|
-
executeConditions,
|
|
92
|
-
getPossibleVersionMismatch,
|
|
93
|
-
getZodParameters,
|
|
94
|
-
isMacOS,
|
|
95
|
-
isStructuredCopilotKitError,
|
|
96
|
-
isValidUUID,
|
|
97
|
-
jsonSchemaToActionParameters,
|
|
98
|
-
logCopilotKitPlatformMessage,
|
|
99
|
-
logStyled,
|
|
100
|
-
parseJson,
|
|
101
|
-
publicApiKeyRequired,
|
|
102
|
-
randomId,
|
|
103
|
-
randomUUID,
|
|
104
|
-
readBody,
|
|
105
|
-
styledConsole,
|
|
106
|
-
tryMap
|
|
107
|
-
};
|
|
1
|
+
import { executeConditions } from "./conditions.mjs";
|
|
2
|
+
import { ConsoleColors, ConsoleStyles, logCopilotKitPlatformMessage, logStyled, publicApiKeyRequired, styledConsole } from "./console-styling.mjs";
|
|
3
|
+
import { BANNER_ERROR_NAMES, COPILOT_CLOUD_ERROR_NAMES, ConfigurationError, CopilotKitAgentDiscoveryError, CopilotKitApiDiscoveryError, CopilotKitError, CopilotKitErrorCode, CopilotKitLowLevelError, CopilotKitMisuseError, CopilotKitRemoteEndpointDiscoveryError, CopilotKitVersionMismatchError, ERROR_CONFIG, ERROR_NAMES, ErrorVisibility, MissingPublicApiKeyError, ResolvedCopilotKitError, Severity, UpgradeRequiredError, ensureStructuredError, getPossibleVersionMismatch, isStructuredCopilotKitError } from "./errors.mjs";
|
|
4
|
+
import { actionParametersToJsonSchema, convertJsonSchemaToZodSchema, getZodParameters, jsonSchemaToActionParameters } from "./json-schema.mjs";
|
|
5
|
+
import { dataToUUID, isValidUUID, randomId, randomUUID } from "./random-id.mjs";
|
|
6
|
+
import { readBody } from "./requests.mjs";
|
|
7
|
+
|
|
8
|
+
//#region src/utils/index.ts
|
|
9
|
+
/**
|
|
10
|
+
* Safely parses a JSON string into an object
|
|
11
|
+
* @param json The JSON string to parse
|
|
12
|
+
* @param fallback Optional fallback value to return if parsing fails. If not provided or set to "unset", returns null
|
|
13
|
+
* @returns The parsed JSON object, or the fallback value (or null) if parsing fails
|
|
14
|
+
*/
|
|
15
|
+
function parseJson(json, fallback = "unset") {
|
|
16
|
+
try {
|
|
17
|
+
return JSON.parse(json);
|
|
18
|
+
} catch (e) {
|
|
19
|
+
return fallback === "unset" ? null : fallback;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Maps an array of items to a new array, skipping items that throw errors during mapping
|
|
24
|
+
* @param items The array to map
|
|
25
|
+
* @param callback The mapping function to apply to each item
|
|
26
|
+
* @returns A new array containing only the successfully mapped items
|
|
27
|
+
*/
|
|
28
|
+
function tryMap(items, callback) {
|
|
29
|
+
return items.reduce((acc, item, index, array) => {
|
|
30
|
+
try {
|
|
31
|
+
acc.push(callback(item, index, array));
|
|
32
|
+
} catch (error) {
|
|
33
|
+
console.error(error);
|
|
34
|
+
}
|
|
35
|
+
return acc;
|
|
36
|
+
}, []);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Checks if the current environment is macOS
|
|
40
|
+
* @returns {boolean} True if running on macOS, false otherwise
|
|
41
|
+
*/
|
|
42
|
+
function isMacOS() {
|
|
43
|
+
return /Mac|iMac|Macintosh/i.test(navigator.userAgent);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
//#endregion
|
|
47
|
+
export { isMacOS, parseJson, tryMap };
|
|
108
48
|
//# sourceMappingURL=index.mjs.map
|
package/dist/utils/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../src/utils/index.ts"],"sourcesContent":["export * from \"./conditions\";\nexport * from \"./console-styling\";\nexport * from \"./errors\";\nexport * from \"./json-schema\";\nexport * from \"./types\";\nexport * from \"./random-id\";\nexport * from \"./requests\";\n\n/**\n * Safely parses a JSON string into an object\n * @param json The JSON string to parse\n * @param fallback Optional fallback value to return if parsing fails. If not provided or set to \"unset\", returns null\n * @returns The parsed JSON object, or the fallback value (or null) if parsing fails\n */\nexport function parseJson(json: string, fallback: any = \"unset\") {\n try {\n return JSON.parse(json);\n } catch (e) {\n return fallback === \"unset\" ? null : fallback;\n }\n}\n\n/**\n * Maps an array of items to a new array, skipping items that throw errors during mapping\n * @param items The array to map\n * @param callback The mapping function to apply to each item\n * @returns A new array containing only the successfully mapped items\n */\nexport function tryMap<TItem, TMapped>(\n items: TItem[],\n callback: (item: TItem, index: number, array: TItem[]) => TMapped,\n): TMapped[] {\n return items.reduce<TMapped[]>((acc, item, index, array) => {\n try {\n acc.push(callback(item, index, array));\n } catch (error) {\n console.error(error);\n }\n return acc;\n }, []);\n}\n\n/**\n * Checks if the current environment is macOS\n * @returns {boolean} True if running on macOS, false otherwise\n */\nexport function isMacOS(): boolean {\n return /Mac|iMac|Macintosh/i.test(navigator.userAgent);\n}\n"],"mappings":";;;;;;;;;;;;;;AAcA,SAAgB,UAAU,MAAc,WAAgB,SAAS;AAC/D,KAAI;AACF,SAAO,KAAK,MAAM,KAAK;UAChB,GAAG;AACV,SAAO,aAAa,UAAU,OAAO;;;;;;;;;AAUzC,SAAgB,OACd,OACA,UACW;AACX,QAAO,MAAM,QAAmB,KAAK,MAAM,OAAO,UAAU;AAC1D,MAAI;AACF,OAAI,KAAK,SAAS,MAAM,OAAO,MAAM,CAAC;WAC/B,OAAO;AACd,WAAQ,MAAM,MAAM;;AAEtB,SAAO;IACN,EAAE,CAAC;;;;;;AAOR,SAAgB,UAAmB;AACjC,QAAO,sBAAsB,KAAK,UAAU,UAAU"}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
|
|
2
|
+
let zod = require("zod");
|
|
3
|
+
|
|
4
|
+
//#region src/utils/json-schema.ts
|
|
5
|
+
function actionParametersToJsonSchema(actionParameters) {
|
|
6
|
+
let parameters = {};
|
|
7
|
+
for (let parameter of actionParameters || []) parameters[parameter.name] = convertAttribute(parameter);
|
|
8
|
+
let requiredParameterNames = [];
|
|
9
|
+
for (let arg of actionParameters || []) if (arg.required !== false) requiredParameterNames.push(arg.name);
|
|
10
|
+
return {
|
|
11
|
+
type: "object",
|
|
12
|
+
properties: parameters,
|
|
13
|
+
required: requiredParameterNames
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function jsonSchemaToActionParameters(jsonSchema) {
|
|
17
|
+
if (jsonSchema.type !== "object" || !jsonSchema.properties) return [];
|
|
18
|
+
const parameters = [];
|
|
19
|
+
const requiredFields = jsonSchema.required || [];
|
|
20
|
+
for (const [name, schema] of Object.entries(jsonSchema.properties)) {
|
|
21
|
+
const parameter = convertJsonSchemaToParameter(name, schema, requiredFields.includes(name));
|
|
22
|
+
parameters.push(parameter);
|
|
23
|
+
}
|
|
24
|
+
return parameters;
|
|
25
|
+
}
|
|
26
|
+
function convertJsonSchemaToParameter(name, schema, isRequired) {
|
|
27
|
+
const baseParameter = {
|
|
28
|
+
name,
|
|
29
|
+
description: schema.description
|
|
30
|
+
};
|
|
31
|
+
if (!isRequired) baseParameter.required = false;
|
|
32
|
+
switch (schema.type) {
|
|
33
|
+
case "string": return {
|
|
34
|
+
...baseParameter,
|
|
35
|
+
type: "string",
|
|
36
|
+
...schema.enum && { enum: schema.enum }
|
|
37
|
+
};
|
|
38
|
+
case "number":
|
|
39
|
+
case "boolean": return {
|
|
40
|
+
...baseParameter,
|
|
41
|
+
type: schema.type
|
|
42
|
+
};
|
|
43
|
+
case "object":
|
|
44
|
+
if (schema.properties) {
|
|
45
|
+
const attributes = [];
|
|
46
|
+
const requiredFields = schema.required || [];
|
|
47
|
+
for (const [propName, propSchema] of Object.entries(schema.properties)) attributes.push(convertJsonSchemaToParameter(propName, propSchema, requiredFields.includes(propName)));
|
|
48
|
+
return {
|
|
49
|
+
...baseParameter,
|
|
50
|
+
type: "object",
|
|
51
|
+
attributes
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
...baseParameter,
|
|
56
|
+
type: "object"
|
|
57
|
+
};
|
|
58
|
+
case "array": if (schema.items.type === "object" && "properties" in schema.items) {
|
|
59
|
+
const attributes = [];
|
|
60
|
+
const requiredFields = schema.items.required || [];
|
|
61
|
+
for (const [propName, propSchema] of Object.entries(schema.items.properties || {})) attributes.push(convertJsonSchemaToParameter(propName, propSchema, requiredFields.includes(propName)));
|
|
62
|
+
return {
|
|
63
|
+
...baseParameter,
|
|
64
|
+
type: "object[]",
|
|
65
|
+
attributes
|
|
66
|
+
};
|
|
67
|
+
} else if (schema.items.type === "array") throw new Error("Nested arrays are not supported");
|
|
68
|
+
else return {
|
|
69
|
+
...baseParameter,
|
|
70
|
+
type: `${schema.items.type}[]`
|
|
71
|
+
};
|
|
72
|
+
default: return {
|
|
73
|
+
...baseParameter,
|
|
74
|
+
type: "string"
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function convertAttribute(attribute) {
|
|
79
|
+
switch (attribute.type) {
|
|
80
|
+
case "string": return {
|
|
81
|
+
type: "string",
|
|
82
|
+
description: attribute.description,
|
|
83
|
+
...attribute.enum && { enum: attribute.enum }
|
|
84
|
+
};
|
|
85
|
+
case "number":
|
|
86
|
+
case "boolean": return {
|
|
87
|
+
type: attribute.type,
|
|
88
|
+
description: attribute.description
|
|
89
|
+
};
|
|
90
|
+
case "object":
|
|
91
|
+
case "object[]":
|
|
92
|
+
const properties = attribute.attributes?.reduce((acc, attr) => {
|
|
93
|
+
acc[attr.name] = convertAttribute(attr);
|
|
94
|
+
return acc;
|
|
95
|
+
}, {});
|
|
96
|
+
const required = attribute.attributes?.filter((attr) => attr.required !== false).map((attr) => attr.name);
|
|
97
|
+
if (attribute.type === "object[]") return {
|
|
98
|
+
type: "array",
|
|
99
|
+
items: {
|
|
100
|
+
type: "object",
|
|
101
|
+
...properties && { properties },
|
|
102
|
+
...required && required.length > 0 && { required }
|
|
103
|
+
},
|
|
104
|
+
description: attribute.description
|
|
105
|
+
};
|
|
106
|
+
return {
|
|
107
|
+
type: "object",
|
|
108
|
+
description: attribute.description,
|
|
109
|
+
...properties && { properties },
|
|
110
|
+
...required && required.length > 0 && { required }
|
|
111
|
+
};
|
|
112
|
+
default:
|
|
113
|
+
if (attribute.type?.endsWith("[]")) return {
|
|
114
|
+
type: "array",
|
|
115
|
+
items: { type: attribute.type.slice(0, -2) },
|
|
116
|
+
description: attribute.description
|
|
117
|
+
};
|
|
118
|
+
return {
|
|
119
|
+
type: "string",
|
|
120
|
+
description: attribute.description
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
function convertJsonSchemaToZodSchema(jsonSchema, required) {
|
|
125
|
+
if (jsonSchema.type === "object") {
|
|
126
|
+
const spec = {};
|
|
127
|
+
if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) return !required ? zod.z.object(spec).optional() : zod.z.object(spec);
|
|
128
|
+
for (const [key, value] of Object.entries(jsonSchema.properties)) spec[key] = convertJsonSchemaToZodSchema(value, jsonSchema.required ? jsonSchema.required.includes(key) : false);
|
|
129
|
+
let schema = zod.z.object(spec).describe(jsonSchema.description);
|
|
130
|
+
return required ? schema : schema.optional();
|
|
131
|
+
} else if (jsonSchema.type === "string") {
|
|
132
|
+
let schema = zod.z.string().describe(jsonSchema.description);
|
|
133
|
+
return required ? schema : schema.optional();
|
|
134
|
+
} else if (jsonSchema.type === "number") {
|
|
135
|
+
let schema = zod.z.number().describe(jsonSchema.description);
|
|
136
|
+
return required ? schema : schema.optional();
|
|
137
|
+
} else if (jsonSchema.type === "boolean") {
|
|
138
|
+
let schema = zod.z.boolean().describe(jsonSchema.description);
|
|
139
|
+
return required ? schema : schema.optional();
|
|
140
|
+
} else if (jsonSchema.type === "array") {
|
|
141
|
+
let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, true);
|
|
142
|
+
let schema = zod.z.array(itemSchema).describe(jsonSchema.description);
|
|
143
|
+
return required ? schema : schema.optional();
|
|
144
|
+
}
|
|
145
|
+
throw new Error("Invalid JSON schema");
|
|
146
|
+
}
|
|
147
|
+
function getZodParameters(parameters) {
|
|
148
|
+
if (!parameters) return zod.z.object({});
|
|
149
|
+
return convertJsonSchemaToZodSchema(actionParametersToJsonSchema(parameters), true);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
//#endregion
|
|
153
|
+
exports.actionParametersToJsonSchema = actionParametersToJsonSchema;
|
|
154
|
+
exports.convertJsonSchemaToZodSchema = convertJsonSchemaToZodSchema;
|
|
155
|
+
exports.getZodParameters = getZodParameters;
|
|
156
|
+
exports.jsonSchemaToActionParameters = jsonSchemaToActionParameters;
|
|
157
|
+
//# sourceMappingURL=json-schema.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json-schema.cjs","names":["z"],"sources":["../../src/utils/json-schema.ts"],"sourcesContent":["import { z } from \"zod\";\nimport { Parameter } from \"../types\";\n\nexport type JSONSchemaString = {\n type: \"string\";\n description?: string;\n enum?: string[];\n};\n\nexport type JSONSchemaNumber = {\n type: \"number\";\n description?: string;\n};\n\nexport type JSONSchemaBoolean = {\n type: \"boolean\";\n description?: string;\n};\n\nexport type JSONSchemaObject = {\n type: \"object\";\n properties?: Record<string, JSONSchema>;\n required?: string[];\n description?: string;\n};\n\nexport type JSONSchemaArray = {\n type: \"array\";\n items: JSONSchema;\n description?: string;\n};\n\nexport type JSONSchema =\n | JSONSchemaString\n | JSONSchemaNumber\n | JSONSchemaBoolean\n | JSONSchemaObject\n | JSONSchemaArray;\n\nexport function actionParametersToJsonSchema(\n actionParameters: Parameter[],\n): JSONSchema {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let parameter of actionParameters || []) {\n parameters[parameter.name] = convertAttribute(parameter);\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of actionParameters || []) {\n if (arg.required !== false) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n return {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n };\n}\n\n// Convert JSONSchema to Parameter[]\nexport function jsonSchemaToActionParameters(\n jsonSchema: JSONSchema,\n): Parameter[] {\n if (jsonSchema.type !== \"object\" || !jsonSchema.properties) {\n return [];\n }\n\n const parameters: Parameter[] = [];\n const requiredFields = jsonSchema.required || [];\n\n for (const [name, schema] of Object.entries(jsonSchema.properties)) {\n const parameter = convertJsonSchemaToParameter(\n name,\n schema,\n requiredFields.includes(name),\n );\n parameters.push(parameter);\n }\n\n return parameters;\n}\n\n// Convert JSONSchema property to Parameter\nfunction convertJsonSchemaToParameter(\n name: string,\n schema: JSONSchema,\n isRequired: boolean,\n): Parameter {\n const baseParameter: Parameter = {\n name,\n description: schema.description,\n };\n\n if (!isRequired) {\n baseParameter.required = false;\n }\n\n switch (schema.type) {\n case \"string\":\n return {\n ...baseParameter,\n type: \"string\",\n ...(schema.enum && { enum: schema.enum }),\n };\n case \"number\":\n case \"boolean\":\n return {\n ...baseParameter,\n type: schema.type,\n };\n case \"object\":\n if (schema.properties) {\n const attributes: Parameter[] = [];\n const requiredFields = schema.required || [];\n\n for (const [propName, propSchema] of Object.entries(\n schema.properties,\n )) {\n attributes.push(\n convertJsonSchemaToParameter(\n propName,\n propSchema,\n requiredFields.includes(propName),\n ),\n );\n }\n\n return {\n ...baseParameter,\n type: \"object\",\n attributes,\n };\n }\n return {\n ...baseParameter,\n type: \"object\",\n };\n case \"array\":\n if (schema.items.type === \"object\" && \"properties\" in schema.items) {\n const attributes: Parameter[] = [];\n const requiredFields = schema.items.required || [];\n\n for (const [propName, propSchema] of Object.entries(\n schema.items.properties || {},\n )) {\n attributes.push(\n convertJsonSchemaToParameter(\n propName,\n propSchema,\n requiredFields.includes(propName),\n ),\n );\n }\n\n return {\n ...baseParameter,\n type: \"object[]\",\n attributes,\n };\n } else if (schema.items.type === \"array\") {\n throw new Error(\"Nested arrays are not supported\");\n } else {\n return {\n ...baseParameter,\n type: `${schema.items.type}[]`,\n };\n }\n default:\n return {\n ...baseParameter,\n type: \"string\",\n };\n }\n}\n\nfunction convertAttribute(attribute: Parameter): JSONSchema {\n switch (attribute.type) {\n case \"string\":\n return {\n type: \"string\",\n description: attribute.description,\n ...(attribute.enum && { enum: attribute.enum }),\n };\n case \"number\":\n case \"boolean\":\n return {\n type: attribute.type,\n description: attribute.description,\n };\n case \"object\":\n case \"object[]\":\n const properties = attribute.attributes?.reduce(\n (acc, attr) => {\n acc[attr.name] = convertAttribute(attr);\n return acc;\n },\n {} as Record<string, any>,\n );\n const required = attribute.attributes\n ?.filter((attr) => attr.required !== false)\n .map((attr) => attr.name);\n if (attribute.type === \"object[]\") {\n return {\n type: \"array\",\n items: {\n type: \"object\",\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n },\n description: attribute.description,\n };\n }\n return {\n type: \"object\",\n description: attribute.description,\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n };\n default:\n // Handle arrays of primitive types and undefined attribute.type\n if (attribute.type?.endsWith(\"[]\")) {\n const itemType = attribute.type.slice(0, -2);\n return {\n type: \"array\",\n items: { type: itemType as any },\n description: attribute.description,\n };\n }\n // Fallback for undefined type or any other unexpected type\n return {\n type: \"string\",\n description: attribute.description,\n };\n }\n}\n\nexport function convertJsonSchemaToZodSchema(\n jsonSchema: any,\n required: boolean,\n): z.ZodSchema {\n if (jsonSchema.type === \"object\") {\n const spec: { [key: string]: z.ZodSchema } = {};\n\n if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) {\n return !required ? z.object(spec).optional() : z.object(spec);\n }\n\n for (const [key, value] of Object.entries(jsonSchema.properties)) {\n spec[key] = convertJsonSchemaToZodSchema(\n value,\n jsonSchema.required ? jsonSchema.required.includes(key) : false,\n );\n }\n let schema = z.object(spec).describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"string\") {\n let schema = z.string().describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"number\") {\n let schema = z.number().describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"boolean\") {\n let schema = z.boolean().describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"array\") {\n let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, true);\n let schema = z.array(itemSchema).describe(jsonSchema.description);\n return required ? schema : schema.optional();\n }\n throw new Error(\"Invalid JSON schema\");\n}\n\nexport function getZodParameters<T extends [] | Parameter[] | undefined>(\n parameters: T,\n): any {\n if (!parameters) return z.object({});\n const jsonParams = actionParametersToJsonSchema(parameters);\n return convertJsonSchemaToZodSchema(jsonParams, true);\n}\n"],"mappings":";;;;AAuCA,SAAgB,6BACd,kBACY;CAEZ,IAAI,aAAqC,EAAE;AAC3C,MAAK,IAAI,aAAa,oBAAoB,EAAE,CAC1C,YAAW,UAAU,QAAQ,iBAAiB,UAAU;CAG1D,IAAI,yBAAmC,EAAE;AACzC,MAAK,IAAI,OAAO,oBAAoB,EAAE,CACpC,KAAI,IAAI,aAAa,MACnB,wBAAuB,KAAK,IAAI,KAAK;AAKzC,QAAO;EACL,MAAM;EACN,YAAY;EACZ,UAAU;EACX;;AAIH,SAAgB,6BACd,YACa;AACb,KAAI,WAAW,SAAS,YAAY,CAAC,WAAW,WAC9C,QAAO,EAAE;CAGX,MAAM,aAA0B,EAAE;CAClC,MAAM,iBAAiB,WAAW,YAAY,EAAE;AAEhD,MAAK,MAAM,CAAC,MAAM,WAAW,OAAO,QAAQ,WAAW,WAAW,EAAE;EAClE,MAAM,YAAY,6BAChB,MACA,QACA,eAAe,SAAS,KAAK,CAC9B;AACD,aAAW,KAAK,UAAU;;AAG5B,QAAO;;AAIT,SAAS,6BACP,MACA,QACA,YACW;CACX,MAAM,gBAA2B;EAC/B;EACA,aAAa,OAAO;EACrB;AAED,KAAI,CAAC,WACH,eAAc,WAAW;AAG3B,SAAQ,OAAO,MAAf;EACE,KAAK,SACH,QAAO;GACL,GAAG;GACH,MAAM;GACN,GAAI,OAAO,QAAQ,EAAE,MAAM,OAAO,MAAM;GACzC;EACH,KAAK;EACL,KAAK,UACH,QAAO;GACL,GAAG;GACH,MAAM,OAAO;GACd;EACH,KAAK;AACH,OAAI,OAAO,YAAY;IACrB,MAAM,aAA0B,EAAE;IAClC,MAAM,iBAAiB,OAAO,YAAY,EAAE;AAE5C,SAAK,MAAM,CAAC,UAAU,eAAe,OAAO,QAC1C,OAAO,WACR,CACC,YAAW,KACT,6BACE,UACA,YACA,eAAe,SAAS,SAAS,CAClC,CACF;AAGH,WAAO;KACL,GAAG;KACH,MAAM;KACN;KACD;;AAEH,UAAO;IACL,GAAG;IACH,MAAM;IACP;EACH,KAAK,QACH,KAAI,OAAO,MAAM,SAAS,YAAY,gBAAgB,OAAO,OAAO;GAClE,MAAM,aAA0B,EAAE;GAClC,MAAM,iBAAiB,OAAO,MAAM,YAAY,EAAE;AAElD,QAAK,MAAM,CAAC,UAAU,eAAe,OAAO,QAC1C,OAAO,MAAM,cAAc,EAAE,CAC9B,CACC,YAAW,KACT,6BACE,UACA,YACA,eAAe,SAAS,SAAS,CAClC,CACF;AAGH,UAAO;IACL,GAAG;IACH,MAAM;IACN;IACD;aACQ,OAAO,MAAM,SAAS,QAC/B,OAAM,IAAI,MAAM,kCAAkC;MAElD,QAAO;GACL,GAAG;GACH,MAAM,GAAG,OAAO,MAAM,KAAK;GAC5B;EAEL,QACE,QAAO;GACL,GAAG;GACH,MAAM;GACP;;;AAIP,SAAS,iBAAiB,WAAkC;AAC1D,SAAQ,UAAU,MAAlB;EACE,KAAK,SACH,QAAO;GACL,MAAM;GACN,aAAa,UAAU;GACvB,GAAI,UAAU,QAAQ,EAAE,MAAM,UAAU,MAAM;GAC/C;EACH,KAAK;EACL,KAAK,UACH,QAAO;GACL,MAAM,UAAU;GAChB,aAAa,UAAU;GACxB;EACH,KAAK;EACL,KAAK;GACH,MAAM,aAAa,UAAU,YAAY,QACtC,KAAK,SAAS;AACb,QAAI,KAAK,QAAQ,iBAAiB,KAAK;AACvC,WAAO;MAET,EAAE,CACH;GACD,MAAM,WAAW,UAAU,YACvB,QAAQ,SAAS,KAAK,aAAa,MAAM,CAC1C,KAAK,SAAS,KAAK,KAAK;AAC3B,OAAI,UAAU,SAAS,WACrB,QAAO;IACL,MAAM;IACN,OAAO;KACL,MAAM;KACN,GAAI,cAAc,EAAE,YAAY;KAChC,GAAI,YAAY,SAAS,SAAS,KAAK,EAAE,UAAU;KACpD;IACD,aAAa,UAAU;IACxB;AAEH,UAAO;IACL,MAAM;IACN,aAAa,UAAU;IACvB,GAAI,cAAc,EAAE,YAAY;IAChC,GAAI,YAAY,SAAS,SAAS,KAAK,EAAE,UAAU;IACpD;EACH;AAEE,OAAI,UAAU,MAAM,SAAS,KAAK,CAEhC,QAAO;IACL,MAAM;IACN,OAAO,EAAE,MAHM,UAAU,KAAK,MAAM,GAAG,GAAG,EAGV;IAChC,aAAa,UAAU;IACxB;AAGH,UAAO;IACL,MAAM;IACN,aAAa,UAAU;IACxB;;;AAIP,SAAgB,6BACd,YACA,UACa;AACb,KAAI,WAAW,SAAS,UAAU;EAChC,MAAM,OAAuC,EAAE;AAE/C,MAAI,CAAC,WAAW,cAAc,CAAC,OAAO,KAAK,WAAW,WAAW,CAAC,OAChE,QAAO,CAAC,WAAWA,MAAE,OAAO,KAAK,CAAC,UAAU,GAAGA,MAAE,OAAO,KAAK;AAG/D,OAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,WAAW,WAAW,CAC9D,MAAK,OAAO,6BACV,OACA,WAAW,WAAW,WAAW,SAAS,SAAS,IAAI,GAAG,MAC3D;EAEH,IAAI,SAASA,MAAE,OAAO,KAAK,CAAC,SAAS,WAAW,YAAY;AAC5D,SAAO,WAAW,SAAS,OAAO,UAAU;YACnC,WAAW,SAAS,UAAU;EACvC,IAAI,SAASA,MAAE,QAAQ,CAAC,SAAS,WAAW,YAAY;AACxD,SAAO,WAAW,SAAS,OAAO,UAAU;YACnC,WAAW,SAAS,UAAU;EACvC,IAAI,SAASA,MAAE,QAAQ,CAAC,SAAS,WAAW,YAAY;AACxD,SAAO,WAAW,SAAS,OAAO,UAAU;YACnC,WAAW,SAAS,WAAW;EACxC,IAAI,SAASA,MAAE,SAAS,CAAC,SAAS,WAAW,YAAY;AACzD,SAAO,WAAW,SAAS,OAAO,UAAU;YACnC,WAAW,SAAS,SAAS;EACtC,IAAI,aAAa,6BAA6B,WAAW,OAAO,KAAK;EACrE,IAAI,SAASA,MAAE,MAAM,WAAW,CAAC,SAAS,WAAW,YAAY;AACjE,SAAO,WAAW,SAAS,OAAO,UAAU;;AAE9C,OAAM,IAAI,MAAM,sBAAsB;;AAGxC,SAAgB,iBACd,YACK;AACL,KAAI,CAAC,WAAY,QAAOA,MAAE,OAAO,EAAE,CAAC;AAEpC,QAAO,6BADY,6BAA6B,WAAW,EACX,KAAK"}
|
|
@@ -1,34 +1,36 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { Parameter } from "../types/action.cjs";
|
|
2
|
+
import { z } from "zod";
|
|
3
3
|
|
|
4
|
+
//#region src/utils/json-schema.d.ts
|
|
4
5
|
type JSONSchemaString = {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
type: "string";
|
|
7
|
+
description?: string;
|
|
8
|
+
enum?: string[];
|
|
8
9
|
};
|
|
9
10
|
type JSONSchemaNumber = {
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
type: "number";
|
|
12
|
+
description?: string;
|
|
12
13
|
};
|
|
13
14
|
type JSONSchemaBoolean = {
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
type: "boolean";
|
|
16
|
+
description?: string;
|
|
16
17
|
};
|
|
17
18
|
type JSONSchemaObject = {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
type: "object";
|
|
20
|
+
properties?: Record<string, JSONSchema>;
|
|
21
|
+
required?: string[];
|
|
22
|
+
description?: string;
|
|
22
23
|
};
|
|
23
24
|
type JSONSchemaArray = {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
type: "array";
|
|
26
|
+
items: JSONSchema;
|
|
27
|
+
description?: string;
|
|
27
28
|
};
|
|
28
29
|
type JSONSchema = JSONSchemaString | JSONSchemaNumber | JSONSchemaBoolean | JSONSchemaObject | JSONSchemaArray;
|
|
29
30
|
declare function actionParametersToJsonSchema(actionParameters: Parameter[]): JSONSchema;
|
|
30
31
|
declare function jsonSchemaToActionParameters(jsonSchema: JSONSchema): Parameter[];
|
|
31
32
|
declare function convertJsonSchemaToZodSchema(jsonSchema: any, required: boolean): z.ZodSchema;
|
|
32
33
|
declare function getZodParameters<T extends [] | Parameter[] | undefined>(parameters: T): any;
|
|
33
|
-
|
|
34
|
+
//#endregion
|
|
34
35
|
export { JSONSchema, JSONSchemaArray, JSONSchemaBoolean, JSONSchemaNumber, JSONSchemaObject, JSONSchemaString, actionParametersToJsonSchema, convertJsonSchemaToZodSchema, getZodParameters, jsonSchemaToActionParameters };
|
|
36
|
+
//# sourceMappingURL=json-schema.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json-schema.d.cts","names":[],"sources":["../../src/utils/json-schema.ts"],"mappings":";;;;KAGY,gBAAA;EACV,IAAA;EACA,WAAA;EACA,IAAA;AAAA;AAAA,KAGU,gBAAA;EACV,IAAA;EACA,WAAA;AAAA;AAAA,KAGU,iBAAA;EACV,IAAA;EACA,WAAA;AAAA;AAAA,KAGU,gBAAA;EACV,IAAA;EACA,UAAA,GAAa,MAAA,SAAe,UAAA;EAC5B,QAAA;EACA,WAAA;AAAA;AAAA,KAGU,eAAA;EACV,IAAA;EACA,KAAA,EAAO,UAAA;EACP,WAAA;AAAA;AAAA,KAGU,UAAA,GACR,gBAAA,GACA,gBAAA,GACA,iBAAA,GACA,gBAAA,GACA,eAAA;AAAA,iBAEY,4BAAA,CACd,gBAAA,EAAkB,SAAA,KACjB,UAAA;AAAA,iBAuBa,4BAAA,CACd,UAAA,EAAY,UAAA,GACX,SAAA;AAAA,iBA8Ka,4BAAA,CACd,UAAA,OACA,QAAA,YACC,CAAA,CAAE,SAAA;AAAA,iBAiCW,gBAAA,gBAAgC,SAAA,eAAA,CAC9C,UAAA,EAAY,CAAA"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Parameter } from "../types/action.mjs";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
//#region src/utils/json-schema.d.ts
|
|
5
|
+
type JSONSchemaString = {
|
|
6
|
+
type: "string";
|
|
7
|
+
description?: string;
|
|
8
|
+
enum?: string[];
|
|
9
|
+
};
|
|
10
|
+
type JSONSchemaNumber = {
|
|
11
|
+
type: "number";
|
|
12
|
+
description?: string;
|
|
13
|
+
};
|
|
14
|
+
type JSONSchemaBoolean = {
|
|
15
|
+
type: "boolean";
|
|
16
|
+
description?: string;
|
|
17
|
+
};
|
|
18
|
+
type JSONSchemaObject = {
|
|
19
|
+
type: "object";
|
|
20
|
+
properties?: Record<string, JSONSchema>;
|
|
21
|
+
required?: string[];
|
|
22
|
+
description?: string;
|
|
23
|
+
};
|
|
24
|
+
type JSONSchemaArray = {
|
|
25
|
+
type: "array";
|
|
26
|
+
items: JSONSchema;
|
|
27
|
+
description?: string;
|
|
28
|
+
};
|
|
29
|
+
type JSONSchema = JSONSchemaString | JSONSchemaNumber | JSONSchemaBoolean | JSONSchemaObject | JSONSchemaArray;
|
|
30
|
+
declare function actionParametersToJsonSchema(actionParameters: Parameter[]): JSONSchema;
|
|
31
|
+
declare function jsonSchemaToActionParameters(jsonSchema: JSONSchema): Parameter[];
|
|
32
|
+
declare function convertJsonSchemaToZodSchema(jsonSchema: any, required: boolean): z.ZodSchema;
|
|
33
|
+
declare function getZodParameters<T extends [] | Parameter[] | undefined>(parameters: T): any;
|
|
34
|
+
//#endregion
|
|
35
|
+
export { JSONSchema, JSONSchemaArray, JSONSchemaBoolean, JSONSchemaNumber, JSONSchemaObject, JSONSchemaString, actionParametersToJsonSchema, convertJsonSchemaToZodSchema, getZodParameters, jsonSchemaToActionParameters };
|
|
36
|
+
//# sourceMappingURL=json-schema.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json-schema.d.mts","names":[],"sources":["../../src/utils/json-schema.ts"],"mappings":";;;;KAGY,gBAAA;EACV,IAAA;EACA,WAAA;EACA,IAAA;AAAA;AAAA,KAGU,gBAAA;EACV,IAAA;EACA,WAAA;AAAA;AAAA,KAGU,iBAAA;EACV,IAAA;EACA,WAAA;AAAA;AAAA,KAGU,gBAAA;EACV,IAAA;EACA,UAAA,GAAa,MAAA,SAAe,UAAA;EAC5B,QAAA;EACA,WAAA;AAAA;AAAA,KAGU,eAAA;EACV,IAAA;EACA,KAAA,EAAO,UAAA;EACP,WAAA;AAAA;AAAA,KAGU,UAAA,GACR,gBAAA,GACA,gBAAA,GACA,iBAAA,GACA,gBAAA,GACA,eAAA;AAAA,iBAEY,4BAAA,CACd,gBAAA,EAAkB,SAAA,KACjB,UAAA;AAAA,iBAuBa,4BAAA,CACd,UAAA,EAAY,UAAA,GACX,SAAA;AAAA,iBA8Ka,4BAAA,CACd,UAAA,OACA,QAAA,YACC,CAAA,CAAE,SAAA;AAAA,iBAiCW,gBAAA,gBAAgC,SAAA,eAAA,CAC9C,UAAA,EAAY,CAAA"}
|
|
@@ -1,13 +1,153 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/json-schema.ts
|
|
4
|
+
function actionParametersToJsonSchema(actionParameters) {
|
|
5
|
+
let parameters = {};
|
|
6
|
+
for (let parameter of actionParameters || []) parameters[parameter.name] = convertAttribute(parameter);
|
|
7
|
+
let requiredParameterNames = [];
|
|
8
|
+
for (let arg of actionParameters || []) if (arg.required !== false) requiredParameterNames.push(arg.name);
|
|
9
|
+
return {
|
|
10
|
+
type: "object",
|
|
11
|
+
properties: parameters,
|
|
12
|
+
required: requiredParameterNames
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function jsonSchemaToActionParameters(jsonSchema) {
|
|
16
|
+
if (jsonSchema.type !== "object" || !jsonSchema.properties) return [];
|
|
17
|
+
const parameters = [];
|
|
18
|
+
const requiredFields = jsonSchema.required || [];
|
|
19
|
+
for (const [name, schema] of Object.entries(jsonSchema.properties)) {
|
|
20
|
+
const parameter = convertJsonSchemaToParameter(name, schema, requiredFields.includes(name));
|
|
21
|
+
parameters.push(parameter);
|
|
22
|
+
}
|
|
23
|
+
return parameters;
|
|
24
|
+
}
|
|
25
|
+
function convertJsonSchemaToParameter(name, schema, isRequired) {
|
|
26
|
+
const baseParameter = {
|
|
27
|
+
name,
|
|
28
|
+
description: schema.description
|
|
29
|
+
};
|
|
30
|
+
if (!isRequired) baseParameter.required = false;
|
|
31
|
+
switch (schema.type) {
|
|
32
|
+
case "string": return {
|
|
33
|
+
...baseParameter,
|
|
34
|
+
type: "string",
|
|
35
|
+
...schema.enum && { enum: schema.enum }
|
|
36
|
+
};
|
|
37
|
+
case "number":
|
|
38
|
+
case "boolean": return {
|
|
39
|
+
...baseParameter,
|
|
40
|
+
type: schema.type
|
|
41
|
+
};
|
|
42
|
+
case "object":
|
|
43
|
+
if (schema.properties) {
|
|
44
|
+
const attributes = [];
|
|
45
|
+
const requiredFields = schema.required || [];
|
|
46
|
+
for (const [propName, propSchema] of Object.entries(schema.properties)) attributes.push(convertJsonSchemaToParameter(propName, propSchema, requiredFields.includes(propName)));
|
|
47
|
+
return {
|
|
48
|
+
...baseParameter,
|
|
49
|
+
type: "object",
|
|
50
|
+
attributes
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
...baseParameter,
|
|
55
|
+
type: "object"
|
|
56
|
+
};
|
|
57
|
+
case "array": if (schema.items.type === "object" && "properties" in schema.items) {
|
|
58
|
+
const attributes = [];
|
|
59
|
+
const requiredFields = schema.items.required || [];
|
|
60
|
+
for (const [propName, propSchema] of Object.entries(schema.items.properties || {})) attributes.push(convertJsonSchemaToParameter(propName, propSchema, requiredFields.includes(propName)));
|
|
61
|
+
return {
|
|
62
|
+
...baseParameter,
|
|
63
|
+
type: "object[]",
|
|
64
|
+
attributes
|
|
65
|
+
};
|
|
66
|
+
} else if (schema.items.type === "array") throw new Error("Nested arrays are not supported");
|
|
67
|
+
else return {
|
|
68
|
+
...baseParameter,
|
|
69
|
+
type: `${schema.items.type}[]`
|
|
70
|
+
};
|
|
71
|
+
default: return {
|
|
72
|
+
...baseParameter,
|
|
73
|
+
type: "string"
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function convertAttribute(attribute) {
|
|
78
|
+
switch (attribute.type) {
|
|
79
|
+
case "string": return {
|
|
80
|
+
type: "string",
|
|
81
|
+
description: attribute.description,
|
|
82
|
+
...attribute.enum && { enum: attribute.enum }
|
|
83
|
+
};
|
|
84
|
+
case "number":
|
|
85
|
+
case "boolean": return {
|
|
86
|
+
type: attribute.type,
|
|
87
|
+
description: attribute.description
|
|
88
|
+
};
|
|
89
|
+
case "object":
|
|
90
|
+
case "object[]":
|
|
91
|
+
const properties = attribute.attributes?.reduce((acc, attr) => {
|
|
92
|
+
acc[attr.name] = convertAttribute(attr);
|
|
93
|
+
return acc;
|
|
94
|
+
}, {});
|
|
95
|
+
const required = attribute.attributes?.filter((attr) => attr.required !== false).map((attr) => attr.name);
|
|
96
|
+
if (attribute.type === "object[]") return {
|
|
97
|
+
type: "array",
|
|
98
|
+
items: {
|
|
99
|
+
type: "object",
|
|
100
|
+
...properties && { properties },
|
|
101
|
+
...required && required.length > 0 && { required }
|
|
102
|
+
},
|
|
103
|
+
description: attribute.description
|
|
104
|
+
};
|
|
105
|
+
return {
|
|
106
|
+
type: "object",
|
|
107
|
+
description: attribute.description,
|
|
108
|
+
...properties && { properties },
|
|
109
|
+
...required && required.length > 0 && { required }
|
|
110
|
+
};
|
|
111
|
+
default:
|
|
112
|
+
if (attribute.type?.endsWith("[]")) return {
|
|
113
|
+
type: "array",
|
|
114
|
+
items: { type: attribute.type.slice(0, -2) },
|
|
115
|
+
description: attribute.description
|
|
116
|
+
};
|
|
117
|
+
return {
|
|
118
|
+
type: "string",
|
|
119
|
+
description: attribute.description
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
function convertJsonSchemaToZodSchema(jsonSchema, required) {
|
|
124
|
+
if (jsonSchema.type === "object") {
|
|
125
|
+
const spec = {};
|
|
126
|
+
if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) return !required ? z.object(spec).optional() : z.object(spec);
|
|
127
|
+
for (const [key, value] of Object.entries(jsonSchema.properties)) spec[key] = convertJsonSchemaToZodSchema(value, jsonSchema.required ? jsonSchema.required.includes(key) : false);
|
|
128
|
+
let schema = z.object(spec).describe(jsonSchema.description);
|
|
129
|
+
return required ? schema : schema.optional();
|
|
130
|
+
} else if (jsonSchema.type === "string") {
|
|
131
|
+
let schema = z.string().describe(jsonSchema.description);
|
|
132
|
+
return required ? schema : schema.optional();
|
|
133
|
+
} else if (jsonSchema.type === "number") {
|
|
134
|
+
let schema = z.number().describe(jsonSchema.description);
|
|
135
|
+
return required ? schema : schema.optional();
|
|
136
|
+
} else if (jsonSchema.type === "boolean") {
|
|
137
|
+
let schema = z.boolean().describe(jsonSchema.description);
|
|
138
|
+
return required ? schema : schema.optional();
|
|
139
|
+
} else if (jsonSchema.type === "array") {
|
|
140
|
+
let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, true);
|
|
141
|
+
let schema = z.array(itemSchema).describe(jsonSchema.description);
|
|
142
|
+
return required ? schema : schema.optional();
|
|
143
|
+
}
|
|
144
|
+
throw new Error("Invalid JSON schema");
|
|
145
|
+
}
|
|
146
|
+
function getZodParameters(parameters) {
|
|
147
|
+
if (!parameters) return z.object({});
|
|
148
|
+
return convertJsonSchemaToZodSchema(actionParametersToJsonSchema(parameters), true);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
//#endregion
|
|
152
|
+
export { actionParametersToJsonSchema, convertJsonSchemaToZodSchema, getZodParameters, jsonSchemaToActionParameters };
|
|
13
153
|
//# sourceMappingURL=json-schema.mjs.map
|