@utcp/http 1.0.4 → 1.0.7

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/dist/index.d.ts CHANGED
@@ -1,13 +1,435 @@
1
+ import { z } from 'zod';
2
+ import { CallTemplate, Auth, Serializer, CommunicationProtocol, IUtcpClient, RegisterManualResult, UtcpManual } from '@utcp/sdk';
3
+
4
+ /**
5
+ * REQUIRED
6
+ * Provider configuration for HTTP-based tools.
7
+ *
8
+ * Supports RESTful HTTP/HTTPS APIs with various HTTP methods, authentication,
9
+ * custom headers, and flexible request/response handling. Supports URL path
10
+ * parameters using {parameter_name} syntax.
11
+ */
12
+ interface HttpCallTemplate extends CallTemplate {
13
+ call_template_type: 'http';
14
+ http_method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
15
+ url: string;
16
+ content_type: string;
17
+ headers?: Record<string, string>;
18
+ body_field?: string;
19
+ header_fields?: string[];
20
+ auth_tools?: Auth | null;
21
+ }
22
+ /**
23
+ * HTTP Call Template schema for RESTful HTTP/HTTPS API tools.
24
+ * Extends the base CallTemplate and defines HTTP-specific configuration.
25
+ */
26
+ declare const HttpCallTemplateSchema: z.ZodType<HttpCallTemplate>;
27
+ /**
28
+ * REQUIRED
29
+ * Serializer for HttpCallTemplate.
30
+ */
31
+ declare class HttpCallTemplateSerializer extends Serializer<HttpCallTemplate> {
32
+ toDict(obj: HttpCallTemplate): Record<string, unknown>;
33
+ validateDict(obj: Record<string, unknown>): HttpCallTemplate;
34
+ }
35
+
36
+ /**
37
+ * HTTP communication protocol implementation for UTCP client.
38
+ *
39
+ * Handles communication with HTTP-based tool providers, supporting various
40
+ * authentication methods, URL path parameters, and automatic tool discovery.
41
+ * Enforces security by requiring HTTPS or localhost connections.
42
+ */
43
+ declare class HttpCommunicationProtocol implements CommunicationProtocol {
44
+ private _oauthTokens;
45
+ private _axiosInstance;
46
+ constructor();
47
+ private _logInfo;
48
+ private _logError;
49
+ /**
50
+ * Registers a manual and its tools from an HTTP provider.
51
+ * Supports UTCP Manuals directly or OpenAPI specifications which are converted.
52
+ *
53
+ * @param caller The UTCP client instance.
54
+ * @param manualCallTemplate The HTTP call template for discovery.
55
+ * @returns A RegisterManualResult object.
56
+ */
57
+ registerManual(caller: IUtcpClient, manualCallTemplate: CallTemplate): Promise<RegisterManualResult>;
58
+ /**
59
+ * Deregisters an HTTP manual. This is a no-op for stateless HTTP communication.
60
+ * @param caller The UTCP client instance.
61
+ * @param manualCallTemplate The HTTP call template to deregister.
62
+ */
63
+ deregisterManual(caller: IUtcpClient, manualCallTemplate: CallTemplate): Promise<void>;
64
+ /**
65
+ * Executes a tool call through the HTTP protocol.
66
+ *
67
+ * @param caller The UTCP client instance.
68
+ * @param toolName Name of the tool to call.
69
+ * @param toolArgs Dictionary of arguments to pass to the tool.
70
+ * @param toolCallTemplate The HTTP call template for the tool.
71
+ * @returns The tool's response.
72
+ */
73
+ callTool(caller: IUtcpClient, toolName: string, toolArgs: Record<string, any>, toolCallTemplate: CallTemplate): Promise<any>;
74
+ /**
75
+ * Executes a tool call through this transport streamingly.
76
+ * For standard HTTP, this typically means fetching the full response and yielding it as a single chunk.
77
+ * Real streaming for protocols like SSE or HTTP chunked transfer would be in their specific implementations.
78
+ *
79
+ * @param caller The UTCP client instance.
80
+ * @param toolName Name of the tool to call.
81
+ * @param toolArgs Dictionary of arguments to pass to the tool.
82
+ * @param toolCallTemplate The HTTP call template for the tool.
83
+ * @returns An async generator that yields chunks of the tool's response.
84
+ */
85
+ callToolStreaming(caller: IUtcpClient, toolName: string, toolArgs: Record<string, any>, toolCallTemplate: CallTemplate): AsyncGenerator<any, void, unknown>;
86
+ /**
87
+ * Closes any persistent connections or resources held by the communication protocol.
88
+ * For stateless HTTP, this clears OAuth tokens.
89
+ */
90
+ close(): Promise<void>;
91
+ /**
92
+ * Applies authentication details from the HttpCallTemplate to the Axios request configuration.
93
+ * This modifies `requestConfig.headers`, `requestConfig.params`, `requestConfig.auth`, and returns cookies.
94
+ *
95
+ * @param httpCallTemplate The CallTemplate containing authentication details.
96
+ * @param requestConfig The Axios request configuration to modify.
97
+ * @returns A Promise that resolves to an object containing any cookies to be set.
98
+ */
99
+ private _applyAuthToRequestConfig;
100
+ /**
101
+ * Handles OAuth2 client credentials flow, trying both body and auth header methods.
102
+ * Caches tokens and automatically refreshes if expired.
103
+ *
104
+ * @param authDetails The OAuth2 authentication details.
105
+ * @returns The access token.
106
+ * @throws Error if token cannot be fetched.
107
+ */
108
+ private _handleOAuth2;
109
+ /**
110
+ * Builds a URL by substituting path parameters from the provided arguments.
111
+ * Used arguments are removed from the `args` object.
112
+ *
113
+ * @param urlTemplate The URL template with path parameters in `{param_name}` format.
114
+ * @param args The dictionary of arguments; modified to remove path parameters.
115
+ * @returns The URL with path parameters substituted.
116
+ * @throws Error if a required path parameter is missing.
117
+ */
118
+ private _buildUrlWithPathParams;
119
+ }
120
+
121
+ /**
122
+ * REQUIRED
123
+ * Provider configuration for HTTP streaming tools.
124
+ *
125
+ * Uses HTTP Chunked Transfer Encoding to enable streaming of large responses
126
+ * or real-time data. Useful for tools that return large datasets or provide
127
+ * progressive results. All tool arguments not mapped to URL body, headers
128
+ * or query pattern parameters are passed as query parameters using '?arg_name={arg_value}'.
129
+ *
130
+ * Attributes:
131
+ * call_template_type: Always "streamable_http" for HTTP streaming providers.
132
+ * url: The streaming HTTP endpoint URL. Supports path parameters.
133
+ * http_method: The HTTP method to use (GET or POST).
134
+ * content_type: The Content-Type header for requests.
135
+ * chunk_size: Size of each chunk in bytes for reading the stream.
136
+ * timeout: Request timeout in milliseconds.
137
+ * headers: Optional static headers to include in requests.
138
+ * auth: Optional authentication configuration.
139
+ * body_field: Optional tool argument name to map to HTTP request body.
140
+ * header_fields: List of tool argument names to map to HTTP request headers.
141
+ */
142
+ interface StreamableHttpCallTemplate extends CallTemplate {
143
+ call_template_type: 'streamable_http';
144
+ url: string;
145
+ http_method: 'GET' | 'POST';
146
+ content_type: string;
147
+ chunk_size: number;
148
+ timeout: number;
149
+ headers?: Record<string, string>;
150
+ body_field?: string | null;
151
+ header_fields?: string[] | null;
152
+ }
153
+ /**
154
+ * Streamable HTTP Call Template schema.
155
+ */
156
+ declare const StreamableHttpCallTemplateSchema: z.ZodType<StreamableHttpCallTemplate>;
157
+ /**
158
+ * REQUIRED
159
+ * Serializer for StreamableHttpCallTemplate.
160
+ */
161
+ declare class StreamableHttpCallTemplateSerializer extends Serializer<StreamableHttpCallTemplate> {
162
+ /**
163
+ * REQUIRED
164
+ * Convert StreamableHttpCallTemplate to dictionary.
165
+ */
166
+ toDict(obj: StreamableHttpCallTemplate): Record<string, unknown>;
167
+ /**
168
+ * REQUIRED
169
+ * Validate dictionary and convert to StreamableHttpCallTemplate.
170
+ */
171
+ validateDict(obj: Record<string, unknown>): StreamableHttpCallTemplate;
172
+ }
173
+
174
+ /**
175
+ * Streamable HTTP Communication Protocol for UTCP.
176
+ *
177
+ * Handles HTTP streaming with chunked transfer encoding for real-time data.
178
+ */
179
+
180
+ /**
181
+ * REQUIRED
182
+ * Streamable HTTP communication protocol implementation for UTCP client.
183
+ *
184
+ * Handles HTTP streaming with chunked transfer encoding for real-time data.
185
+ */
186
+ declare class StreamableHttpCommunicationProtocol implements CommunicationProtocol {
187
+ private oauthTokens;
188
+ private _logInfo;
189
+ private _logError;
190
+ private _applyAuth;
191
+ /**
192
+ * REQUIRED
193
+ * Register a manual and its tools from a StreamableHttp provider.
194
+ */
195
+ registerManual(caller: IUtcpClient, manualCallTemplate: CallTemplate): Promise<RegisterManualResult>;
196
+ /**
197
+ * REQUIRED
198
+ * Deregister a manual (no-op for HTTP streaming).
199
+ */
200
+ deregisterManual(caller: IUtcpClient, manualCallTemplate: CallTemplate): Promise<void>;
201
+ /**
202
+ * REQUIRED
203
+ * Call a tool using HTTP (non-streaming).
204
+ */
205
+ callTool(caller: IUtcpClient, toolName: string, toolArgs: Record<string, any>, toolCallTemplate: CallTemplate): Promise<any>;
206
+ /**
207
+ * REQUIRED
208
+ * Call a tool using HTTP streaming.
209
+ * Returns an async generator that yields chunks of data.
210
+ */
211
+ callToolStreaming(caller: IUtcpClient, toolName: string, toolArgs: Record<string, any>, toolCallTemplate: CallTemplate): AsyncGenerator<any, void, unknown>;
212
+ /**
213
+ * REQUIRED
214
+ * Close all active connections and clear internal state.
215
+ */
216
+ close(): Promise<void>;
217
+ }
218
+
219
+ /**
220
+ * REQUIRED
221
+ * Provider configuration for Server-Sent Events (SSE) tools.
222
+ *
223
+ * Enables real-time streaming of events from server to client using the
224
+ * Server-Sent Events protocol. Supports automatic reconnection and
225
+ * event type filtering. All tool arguments not mapped to URL body, headers
226
+ * or query pattern parameters are passed as query parameters using '?arg_name={arg_value}'.
227
+ *
228
+ * Attributes:
229
+ * call_template_type: Always "sse" for SSE providers.
230
+ * url: The SSE endpoint URL to connect to.
231
+ * event_type: Optional filter for specific event types. If None, all events are received.
232
+ * reconnect: Whether to automatically reconnect on connection loss.
233
+ * retry_timeout: Timeout in milliseconds before attempting reconnection.
234
+ * auth: Optional authentication configuration.
235
+ * headers: Optional static headers for the initial connection.
236
+ * body_field: Optional tool argument name to map to request body during connection.
237
+ * header_fields: List of tool argument names to map to HTTP headers during connection.
238
+ */
239
+ interface SseCallTemplate extends CallTemplate {
240
+ call_template_type: 'sse';
241
+ url: string;
242
+ event_type?: string | null;
243
+ reconnect: boolean;
244
+ retry_timeout: number;
245
+ headers?: Record<string, string>;
246
+ body_field?: string | null;
247
+ header_fields?: string[] | null;
248
+ }
249
+ /**
250
+ * SSE Call Template schema.
251
+ */
252
+ declare const SseCallTemplateSchema: z.ZodType<SseCallTemplate>;
253
+ /**
254
+ * REQUIRED
255
+ * Serializer for SseCallTemplate.
256
+ */
257
+ declare class SseCallTemplateSerializer extends Serializer<SseCallTemplate> {
258
+ /**
259
+ * REQUIRED
260
+ * Convert SseCallTemplate to dictionary.
261
+ */
262
+ toDict(obj: SseCallTemplate): Record<string, unknown>;
263
+ /**
264
+ * REQUIRED
265
+ * Validate dictionary and convert to SseCallTemplate.
266
+ */
267
+ validateDict(obj: Record<string, unknown>): SseCallTemplate;
268
+ }
269
+
270
+ /**
271
+ * Server-Sent Events (SSE) Communication Protocol for UTCP.
272
+ *
273
+ * Handles Server-Sent Events based tool providers with streaming capabilities.
274
+ */
275
+
276
+ /**
277
+ * REQUIRED
278
+ * SSE communication protocol implementation for UTCP client.
279
+ *
280
+ * Handles Server-Sent Events based tool providers with streaming capabilities.
281
+ */
282
+ declare class SseCommunicationProtocol implements CommunicationProtocol {
283
+ private oauthTokens;
284
+ private _logInfo;
285
+ private _logError;
286
+ private _applyAuth;
287
+ /**
288
+ * REQUIRED
289
+ * Register a manual and its tools from an SSE provider.
290
+ */
291
+ registerManual(caller: IUtcpClient, manualCallTemplate: CallTemplate): Promise<RegisterManualResult>;
292
+ /**
293
+ * REQUIRED
294
+ * Deregister a manual (no-op for SSE).
295
+ */
296
+ deregisterManual(caller: IUtcpClient, manualCallTemplate: CallTemplate): Promise<void>;
297
+ /**
298
+ * REQUIRED
299
+ * Call a tool using SSE (non-streaming).
300
+ */
301
+ callTool(caller: IUtcpClient, toolName: string, toolArgs: Record<string, any>, toolCallTemplate: CallTemplate): Promise<any>;
302
+ /**
303
+ * REQUIRED
304
+ * Call a tool using SSE streaming.
305
+ * Returns an async generator that yields SSE events.
306
+ */
307
+ callToolStreaming(caller: IUtcpClient, toolName: string, toolArgs: Record<string, any>, toolCallTemplate: CallTemplate): AsyncGenerator<any, void, unknown>;
308
+ /**
309
+ * REQUIRED
310
+ * Close all active connections and clear internal state.
311
+ */
312
+ close(): Promise<void>;
313
+ }
314
+
315
+ /**
316
+ * Options for the OpenAPI converter.
317
+ */
318
+ interface OpenApiConverterOptions {
319
+ specUrl?: string;
320
+ callTemplateName?: string;
321
+ authTools?: Auth;
322
+ }
323
+ /**
324
+ * REQUIRED
325
+ * Converts OpenAPI specifications into UTCP tool definitions.
326
+ *
327
+ * Processes OpenAPI 2.0 and 3.0 specifications to generate equivalent UTCP
328
+ * tools, handling schema resolution, authentication mapping, and proper
329
+ * HTTP call_template configuration. Each operation in the OpenAPI spec becomes
330
+ * a UTCP tool with appropriate input/output schemas.
331
+ */
332
+ declare class OpenApiConverter {
333
+ private spec;
334
+ private spec_url;
335
+ private auth_tools;
336
+ private placeholder_counter;
337
+ private call_template_name;
338
+ /**
339
+ * Initializes the OpenAPI converter.
340
+ *
341
+ * @param openapi_spec Parsed OpenAPI specification as a dictionary.
342
+ * @param options Optional settings including spec_url, call_template_name, and auth_tools.
343
+ * - specUrl: URL where the specification was retrieved from.
344
+ * - callTemplateName: Custom name for the call_template if spec title not provided.
345
+ * - authTools: Optional auth configuration for generated tools.
346
+ */
347
+ constructor(openapi_spec: Record<string, any>, options?: OpenApiConverterOptions);
348
+ private _generateUuid;
349
+ private _incrementPlaceholderCounter;
350
+ private _getPlaceholder;
351
+ /**
352
+ * Parses the OpenAPI specification and returns a UtcpManual.
353
+ * @returns A UTCP manual containing tools derived from the OpenAPI specification.
354
+ */
355
+ /**
356
+ * REQUIRED
357
+ * Converts the loaded OpenAPI specification into a UtcpManual.
358
+ */
359
+ convert(): UtcpManual;
360
+ /**
361
+ * Resolves a local JSON reference within the OpenAPI spec.
362
+ * @param ref The reference string (e.g., '#/components/schemas/Pet').
363
+ * @returns The resolved schema object.
364
+ */
365
+ private _resolveRef;
366
+ /**
367
+ * Recursively resolves all $refs in a schema object, preventing infinite loops.
368
+ * @param schema The schema object that may contain references.
369
+ * @param visitedRefs A set of references already visited to detect cycles.
370
+ * @returns The resolved schema with all references replaced by their actual values.
371
+ */
372
+ private _resolveSchema;
373
+ /**
374
+ * Creates a Tool object from an OpenAPI operation.
375
+ * @param path The API path.
376
+ * @param method The HTTP method (GET, POST, etc.).
377
+ * @param operation The operation definition from OpenAPI.
378
+ * @param baseUrl The base URL for the API.
379
+ * @returns A Tool object or null if operationId is not defined.
380
+ */
381
+ private _createTool;
382
+ /**
383
+ * Extracts the input schema, header fields, and body field from an OpenAPI operation.
384
+ * - Merges path-level and operation-level parameters.
385
+ * - Resolves $ref for parameters.
386
+ * - Supports OpenAPI 2.0 body parameters and 3.0 requestBody.
387
+ * @param path The API path.
388
+ * @param operation The OpenAPI operation object.
389
+ * @returns An object containing the inputs schema, a list of header field names, and the body field name (if any).
390
+ */
391
+ private _extractInputs;
392
+ /**
393
+ * Extracts the output schema from an OpenAPI operation, resolving refs.
394
+ * @param operation The OpenAPI operation object.
395
+ * @returns The output schema.
396
+ */
397
+ private _extractOutputs;
398
+ /**
399
+ * Extracts authentication information from OpenAPI operation and global security schemes.
400
+ * Uses auth_tools configuration when compatible with OpenAPI auth requirements.
401
+ * Supports both OpenAPI 2.0 and 3.0 security schemes.
402
+ * @param operation The OpenAPI operation object.
403
+ * @returns An Auth object or undefined if no authentication is specified.
404
+ */
405
+ private _extractAuth;
406
+ /**
407
+ * Checks if auth_tools configuration is compatible with OpenAPI auth requirements.
408
+ *
409
+ * @param openapiAuth Auth generated from OpenAPI security scheme
410
+ * @param authTools Auth configuration from manual call template
411
+ * @returns True if compatible and auth_tools should be used, False otherwise
412
+ */
413
+ private _isAuthCompatible;
414
+ /**
415
+ * Gets security schemes supporting both OpenAPI 2.0 and 3.0.
416
+ * @returns A record of security schemes.
417
+ */
418
+ private _getSecuritySchemes;
419
+ /**
420
+ * Creates an Auth object from an OpenAPI security scheme.
421
+ * @param scheme The security scheme object.
422
+ * @param schemeName The name of the security scheme.
423
+ * @returns An Auth object or undefined if the scheme is not supported.
424
+ */
425
+ private _createAuthFromScheme;
426
+ }
427
+
1
428
  /**
2
429
  * Registers all HTTP-based protocol CallTemplate serializers
3
430
  * and their CommunicationProtocol implementations.
4
431
  * This function is called automatically when the package is imported.
5
432
  */
6
- export declare function register(override?: boolean): void;
7
- export * from './http_call_template';
8
- export * from './http_communication_protocol';
9
- export * from './streamable_http_call_template';
10
- export * from './streamable_http_communication_protocol';
11
- export * from './sse_call_template';
12
- export * from './sse_communication_protocol';
13
- export * from './openapi_converter';
433
+ declare function register(override?: boolean): void;
434
+
435
+ export { type HttpCallTemplate, HttpCallTemplateSchema, HttpCallTemplateSerializer, HttpCommunicationProtocol, OpenApiConverter, type OpenApiConverterOptions, type SseCallTemplate, SseCallTemplateSchema, SseCallTemplateSerializer, SseCommunicationProtocol, type StreamableHttpCallTemplate, StreamableHttpCallTemplateSchema, StreamableHttpCallTemplateSerializer, StreamableHttpCommunicationProtocol, register };