@typia/utils 12.0.0 → 12.1.0-dev.20260325

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.
@@ -1,114 +1,114 @@
1
- /**
2
- * Error thrown when HTTP request fails with non-2xx status.
3
- *
4
- * `HttpError` is thrown by {@link HttpLlm.execute} and
5
- * {@link HttpMigration.execute} when the server returns a non-2xx status code.
6
- * Contains the full HTTP context: method, path, status, headers, and response
7
- * body.
8
- *
9
- * The response body is available via {@link message} (raw string) or
10
- * {@link toJSON} (parsed JSON). For non-throwing behavior, use
11
- * {@link HttpLlm.propagate} or {@link HttpMigration.propagate} instead.
12
- *
13
- * @author Jeongho Nam - https://github.com/samchon
14
- */
15
- export class HttpError extends Error {
16
- /** HTTP method used for the request. */
17
- public readonly method:
18
- | "GET"
19
- | "QUERY"
20
- | "DELETE"
21
- | "POST"
22
- | "PUT"
23
- | "PATCH"
24
- | "HEAD";
25
-
26
- /** Request path or URL. */
27
- public readonly path: string;
28
-
29
- /** HTTP status code from server. */
30
- public readonly status: number;
31
-
32
- /** Response headers from server. */
33
- public readonly headers: Record<string, string | string[]>;
34
-
35
- /** @internal */
36
- private body_: any = NOT_YET;
37
-
38
- /**
39
- * @param method HTTP method
40
- * @param path Request path or URL
41
- * @param status HTTP status code
42
- * @param headers Response headers
43
- * @param message Error message (response body)
44
- */
45
- public constructor(
46
- method: "GET" | "QUERY" | "DELETE" | "POST" | "PUT" | "PATCH" | "HEAD",
47
- path: string,
48
- status: number,
49
- headers: Record<string, string | string[]>,
50
- message: string,
51
- ) {
52
- super(message);
53
- this.method = method;
54
- this.path = path;
55
- this.status = status;
56
- this.headers = headers;
57
-
58
- // INHERITANCE POLYFILL
59
- const proto: HttpError = new.target.prototype;
60
- if (Object.setPrototypeOf) Object.setPrototypeOf(this, proto);
61
- else (this as any).__proto__ = proto;
62
- }
63
-
64
- /**
65
- * Serialize to JSON-compatible object.
66
- *
67
- * Lazily parses JSON message body on first call. If parsing fails, returns
68
- * the original string.
69
- *
70
- * @template T Expected response body type
71
- * @returns Structured HTTP error information
72
- */
73
- public toJSON<T>(): HttpError.IProps<T> {
74
- if (this.body_ === NOT_YET)
75
- try {
76
- this.body_ = JSON.parse(this.message);
77
- } catch {
78
- this.body_ = this.message;
79
- }
80
- return {
81
- method: this.method,
82
- path: this.path,
83
- status: this.status,
84
- headers: this.headers,
85
- message: this.body_,
86
- };
87
- }
88
- }
89
- export namespace HttpError {
90
- /**
91
- * JSON representation of HttpError.
92
- *
93
- * @template T Response body type
94
- */
95
- export interface IProps<T> {
96
- /** HTTP method. */
97
- method: "GET" | "QUERY" | "DELETE" | "POST" | "PUT" | "PATCH" | "HEAD";
98
-
99
- /** Request path or URL. */
100
- path: string;
101
-
102
- /** HTTP status code. */
103
- status: number;
104
-
105
- /** Response headers. */
106
- headers: Record<string, string | string[]>;
107
-
108
- /** Response body (parsed JSON or original string). */
109
- message: T;
110
- }
111
- }
112
-
113
- /** @internal */
114
- const NOT_YET = {} as any;
1
+ /**
2
+ * Error thrown when HTTP request fails with non-2xx status.
3
+ *
4
+ * `HttpError` is thrown by {@link HttpLlm.execute} and
5
+ * {@link HttpMigration.execute} when the server returns a non-2xx status code.
6
+ * Contains the full HTTP context: method, path, status, headers, and response
7
+ * body.
8
+ *
9
+ * The response body is available via {@link message} (raw string) or
10
+ * {@link toJSON} (parsed JSON). For non-throwing behavior, use
11
+ * {@link HttpLlm.propagate} or {@link HttpMigration.propagate} instead.
12
+ *
13
+ * @author Jeongho Nam - https://github.com/samchon
14
+ */
15
+ export class HttpError extends Error {
16
+ /** HTTP method used for the request. */
17
+ public readonly method:
18
+ | "GET"
19
+ | "QUERY"
20
+ | "DELETE"
21
+ | "POST"
22
+ | "PUT"
23
+ | "PATCH"
24
+ | "HEAD";
25
+
26
+ /** Request path or URL. */
27
+ public readonly path: string;
28
+
29
+ /** HTTP status code from server. */
30
+ public readonly status: number;
31
+
32
+ /** Response headers from server. */
33
+ public readonly headers: Record<string, string | string[]>;
34
+
35
+ /** @internal */
36
+ private body_: any = NOT_YET;
37
+
38
+ /**
39
+ * @param method HTTP method
40
+ * @param path Request path or URL
41
+ * @param status HTTP status code
42
+ * @param headers Response headers
43
+ * @param message Error message (response body)
44
+ */
45
+ public constructor(
46
+ method: "GET" | "QUERY" | "DELETE" | "POST" | "PUT" | "PATCH" | "HEAD",
47
+ path: string,
48
+ status: number,
49
+ headers: Record<string, string | string[]>,
50
+ message: string,
51
+ ) {
52
+ super(message);
53
+ this.method = method;
54
+ this.path = path;
55
+ this.status = status;
56
+ this.headers = headers;
57
+
58
+ // INHERITANCE POLYFILL
59
+ const proto: HttpError = new.target.prototype;
60
+ if (Object.setPrototypeOf) Object.setPrototypeOf(this, proto);
61
+ else (this as any).__proto__ = proto;
62
+ }
63
+
64
+ /**
65
+ * Serialize to JSON-compatible object.
66
+ *
67
+ * Lazily parses JSON message body on first call. If parsing fails, returns
68
+ * the original string.
69
+ *
70
+ * @template T Expected response body type
71
+ * @returns Structured HTTP error information
72
+ */
73
+ public toJSON<T>(): HttpError.IProps<T> {
74
+ if (this.body_ === NOT_YET)
75
+ try {
76
+ this.body_ = JSON.parse(this.message);
77
+ } catch {
78
+ this.body_ = this.message;
79
+ }
80
+ return {
81
+ method: this.method,
82
+ path: this.path,
83
+ status: this.status,
84
+ headers: this.headers,
85
+ message: this.body_,
86
+ };
87
+ }
88
+ }
89
+ export namespace HttpError {
90
+ /**
91
+ * JSON representation of HttpError.
92
+ *
93
+ * @template T Response body type
94
+ */
95
+ export interface IProps<T> {
96
+ /** HTTP method. */
97
+ method: "GET" | "QUERY" | "DELETE" | "POST" | "PUT" | "PATCH" | "HEAD";
98
+
99
+ /** Request path or URL. */
100
+ path: string;
101
+
102
+ /** HTTP status code. */
103
+ status: number;
104
+
105
+ /** Response headers. */
106
+ headers: Record<string, string | string[]>;
107
+
108
+ /** Response body (parsed JSON or original string). */
109
+ message: T;
110
+ }
111
+ }
112
+
113
+ /** @internal */
114
+ const NOT_YET = {} as any;
@@ -1,169 +1,169 @@
1
- import {
2
- IHttpConnection,
3
- IHttpLlmApplication,
4
- IHttpLlmController,
5
- IHttpLlmFunction,
6
- IHttpMigrateApplication,
7
- IHttpResponse,
8
- OpenApi,
9
- OpenApiV3,
10
- OpenApiV3_1,
11
- OpenApiV3_2,
12
- SwaggerV2,
13
- } from "@typia/interface";
14
-
15
- import { HttpMigration } from "./HttpMigration";
16
- import { HttpLlmApplicationComposer } from "./internal/HttpLlmApplicationComposer";
17
- import { HttpLlmFunctionFetcher } from "./internal/HttpLlmFunctionFetcher";
18
-
19
- /**
20
- * LLM function calling utilities for OpenAPI documents.
21
- *
22
- * `HttpLlm` converts OpenAPI documents into LLM function calling applications
23
- * and executes them. Supports all OpenAPI versions (Swagger 2.0, OpenAPI 3.0,
24
- * 3.1) through automatic conversion to {@link OpenApi} format.
25
- *
26
- * Main functions:
27
- *
28
- * - {@link controller}: Create {@link IHttpLlmController} from OpenAPI document
29
- * - {@link application}: Convert OpenAPI document to {@link IHttpLlmApplication}
30
- * - {@link execute}: Call an LLM function and return the response body
31
- * - {@link propagate}: Call an LLM function and return full HTTP response
32
- * - {@link mergeParameters}: Merge LLM-filled and human-filled parameters
33
- *
34
- * Typical workflow:
35
- *
36
- * 1. Load OpenAPI document (JSON/YAML)
37
- * 2. Call `HttpLlm.application()` to get function schemas
38
- * 3. Send function schemas to LLM for function selection
39
- * 4. Call `HttpLlm.execute()` with LLM's chosen function and arguments
40
- *
41
- * @author Jeongho Nam - https://github.com/samchon
42
- */
43
- export namespace HttpLlm {
44
- /* -----------------------------------------------------------
45
- COMPOSERS
46
- ----------------------------------------------------------- */
47
- /**
48
- * Create HTTP LLM controller from OpenAPI document.
49
- *
50
- * Composes {@link IHttpLlmController} from OpenAPI document with connection
51
- * info. The controller can be used with {@link registerMcpControllers} to
52
- * register all API operations as MCP tools at once.
53
- *
54
- * @param props Controller properties
55
- * @returns HTTP LLM controller
56
- */
57
- export const controller = (props: {
58
- /** Identifier name of the controller. */
59
- name: string;
60
-
61
- /** OpenAPI document to convert. */
62
- document:
63
- | OpenApi.IDocument
64
- | SwaggerV2.IDocument
65
- | OpenApiV3.IDocument
66
- | OpenApiV3_1.IDocument
67
- | OpenApiV3_2.IDocument;
68
-
69
- /** Connection to the API server. */
70
- connection: IHttpConnection;
71
-
72
- /** LLM schema conversion configuration. */
73
- config?: Partial<IHttpLlmApplication.IConfig>;
74
-
75
- /**
76
- * Custom executor of the API function.
77
- *
78
- * Default executor is {@link HttpLlm.execute} function.
79
- */
80
- execute?: IHttpLlmController["execute"];
81
- }): IHttpLlmController => ({
82
- protocol: "http",
83
- name: props.name,
84
- application: application({
85
- document: props.document,
86
- config: props.config,
87
- }),
88
- connection: props.connection,
89
- execute: props.execute,
90
- });
91
-
92
- /**
93
- * Convert OpenAPI document to LLM function calling application.
94
- *
95
- * Converts API operations to LLM-callable functions.
96
- *
97
- * @param props Composition properties
98
- * @returns LLM function calling application
99
- */
100
- export const application = (props: {
101
- /** OpenAPI document to convert. */
102
- document:
103
- | OpenApi.IDocument
104
- | SwaggerV2.IDocument
105
- | OpenApiV3.IDocument
106
- | OpenApiV3_1.IDocument
107
- | OpenApiV3_2.IDocument;
108
-
109
- /** LLM schema conversion configuration. */
110
- config?: Partial<IHttpLlmApplication.IConfig>;
111
- }): IHttpLlmApplication => {
112
- // MIGRATE
113
- const migrate: IHttpMigrateApplication = HttpMigration.application(
114
- props.document,
115
- );
116
- return HttpLlmApplicationComposer.application({
117
- migrate,
118
- config: {
119
- strict: props.config?.strict ?? false,
120
- maxLength: props.config?.maxLength ?? 64,
121
- equals: props.config?.equals ?? false,
122
- },
123
- });
124
- };
125
-
126
- /* -----------------------------------------------------------
127
- FETCHERS
128
- ----------------------------------------------------------- */
129
- /** Properties for LLM function call. */
130
- export interface IFetchProps {
131
- /** LLM function calling application. */
132
- application: IHttpLlmApplication;
133
-
134
- /** Function to call. */
135
- function: IHttpLlmFunction;
136
-
137
- /** HTTP connection info. */
138
- connection: IHttpConnection;
139
-
140
- /** Function arguments. */
141
- input: object;
142
- }
143
-
144
- /**
145
- * Execute LLM function call.
146
- *
147
- * Calls API endpoint and returns response body. Throws {@link HttpError} on
148
- * non-2xx status.
149
- *
150
- * @param props Function call properties
151
- * @returns Response body
152
- * @throws HttpError on non-2xx status
153
- */
154
- export const execute = (props: IFetchProps): Promise<unknown> =>
155
- HttpLlmFunctionFetcher.execute(props);
156
-
157
- /**
158
- * Propagate LLM function call.
159
- *
160
- * Calls API endpoint and returns full response including non-2xx. Use when
161
- * you need to handle error responses yourself.
162
- *
163
- * @param props Function call properties
164
- * @returns Full HTTP response
165
- * @throws Error only on connection failure
166
- */
167
- export const propagate = (props: IFetchProps): Promise<IHttpResponse> =>
168
- HttpLlmFunctionFetcher.propagate(props);
169
- }
1
+ import {
2
+ IHttpConnection,
3
+ IHttpLlmApplication,
4
+ IHttpLlmController,
5
+ IHttpLlmFunction,
6
+ IHttpMigrateApplication,
7
+ IHttpResponse,
8
+ OpenApi,
9
+ OpenApiV3,
10
+ OpenApiV3_1,
11
+ OpenApiV3_2,
12
+ SwaggerV2,
13
+ } from "@typia/interface";
14
+
15
+ import { HttpMigration } from "./HttpMigration";
16
+ import { HttpLlmApplicationComposer } from "./internal/HttpLlmApplicationComposer";
17
+ import { HttpLlmFunctionFetcher } from "./internal/HttpLlmFunctionFetcher";
18
+
19
+ /**
20
+ * LLM function calling utilities for OpenAPI documents.
21
+ *
22
+ * `HttpLlm` converts OpenAPI documents into LLM function calling applications
23
+ * and executes them. Supports all OpenAPI versions (Swagger 2.0, OpenAPI 3.0,
24
+ * 3.1) through automatic conversion to {@link OpenApi} format.
25
+ *
26
+ * Main functions:
27
+ *
28
+ * - {@link controller}: Create {@link IHttpLlmController} from OpenAPI document
29
+ * - {@link application}: Convert OpenAPI document to {@link IHttpLlmApplication}
30
+ * - {@link execute}: Call an LLM function and return the response body
31
+ * - {@link propagate}: Call an LLM function and return full HTTP response
32
+ * - {@link mergeParameters}: Merge LLM-filled and human-filled parameters
33
+ *
34
+ * Typical workflow:
35
+ *
36
+ * 1. Load OpenAPI document (JSON/YAML)
37
+ * 2. Call `HttpLlm.application()` to get function schemas
38
+ * 3. Send function schemas to LLM for function selection
39
+ * 4. Call `HttpLlm.execute()` with LLM's chosen function and arguments
40
+ *
41
+ * @author Jeongho Nam - https://github.com/samchon
42
+ */
43
+ export namespace HttpLlm {
44
+ /* -----------------------------------------------------------
45
+ COMPOSERS
46
+ ----------------------------------------------------------- */
47
+ /**
48
+ * Create HTTP LLM controller from OpenAPI document.
49
+ *
50
+ * Composes {@link IHttpLlmController} from OpenAPI document with connection
51
+ * info. The controller can be used with {@link registerMcpControllers} to
52
+ * register all API operations as MCP tools at once.
53
+ *
54
+ * @param props Controller properties
55
+ * @returns HTTP LLM controller
56
+ */
57
+ export const controller = (props: {
58
+ /** Identifier name of the controller. */
59
+ name: string;
60
+
61
+ /** OpenAPI document to convert. */
62
+ document:
63
+ | OpenApi.IDocument
64
+ | SwaggerV2.IDocument
65
+ | OpenApiV3.IDocument
66
+ | OpenApiV3_1.IDocument
67
+ | OpenApiV3_2.IDocument;
68
+
69
+ /** Connection to the API server. */
70
+ connection: IHttpConnection;
71
+
72
+ /** LLM schema conversion configuration. */
73
+ config?: Partial<IHttpLlmApplication.IConfig>;
74
+
75
+ /**
76
+ * Custom executor of the API function.
77
+ *
78
+ * Default executor is {@link HttpLlm.execute} function.
79
+ */
80
+ execute?: IHttpLlmController["execute"];
81
+ }): IHttpLlmController => ({
82
+ protocol: "http",
83
+ name: props.name,
84
+ application: application({
85
+ document: props.document,
86
+ config: props.config,
87
+ }),
88
+ connection: props.connection,
89
+ execute: props.execute,
90
+ });
91
+
92
+ /**
93
+ * Convert OpenAPI document to LLM function calling application.
94
+ *
95
+ * Converts API operations to LLM-callable functions.
96
+ *
97
+ * @param props Composition properties
98
+ * @returns LLM function calling application
99
+ */
100
+ export const application = (props: {
101
+ /** OpenAPI document to convert. */
102
+ document:
103
+ | OpenApi.IDocument
104
+ | SwaggerV2.IDocument
105
+ | OpenApiV3.IDocument
106
+ | OpenApiV3_1.IDocument
107
+ | OpenApiV3_2.IDocument;
108
+
109
+ /** LLM schema conversion configuration. */
110
+ config?: Partial<IHttpLlmApplication.IConfig>;
111
+ }): IHttpLlmApplication => {
112
+ // MIGRATE
113
+ const migrate: IHttpMigrateApplication = HttpMigration.application(
114
+ props.document,
115
+ );
116
+ return HttpLlmApplicationComposer.application({
117
+ migrate,
118
+ config: {
119
+ strict: props.config?.strict ?? false,
120
+ maxLength: props.config?.maxLength ?? 64,
121
+ equals: props.config?.equals ?? false,
122
+ },
123
+ });
124
+ };
125
+
126
+ /* -----------------------------------------------------------
127
+ FETCHERS
128
+ ----------------------------------------------------------- */
129
+ /** Properties for LLM function call. */
130
+ export interface IFetchProps {
131
+ /** LLM function calling application. */
132
+ application: IHttpLlmApplication;
133
+
134
+ /** Function to call. */
135
+ function: IHttpLlmFunction;
136
+
137
+ /** HTTP connection info. */
138
+ connection: IHttpConnection;
139
+
140
+ /** Function arguments. */
141
+ input: object;
142
+ }
143
+
144
+ /**
145
+ * Execute LLM function call.
146
+ *
147
+ * Calls API endpoint and returns response body. Throws {@link HttpError} on
148
+ * non-2xx status.
149
+ *
150
+ * @param props Function call properties
151
+ * @returns Response body
152
+ * @throws HttpError on non-2xx status
153
+ */
154
+ export const execute = (props: IFetchProps): Promise<unknown> =>
155
+ HttpLlmFunctionFetcher.execute(props);
156
+
157
+ /**
158
+ * Propagate LLM function call.
159
+ *
160
+ * Calls API endpoint and returns full response including non-2xx. Use when
161
+ * you need to handle error responses yourself.
162
+ *
163
+ * @param props Function call properties
164
+ * @returns Full HTTP response
165
+ * @throws Error only on connection failure
166
+ */
167
+ export const propagate = (props: IFetchProps): Promise<IHttpResponse> =>
168
+ HttpLlmFunctionFetcher.propagate(props);
169
+ }