@openfloor/protocol
Version:
Open Floor Protocol implementation for JavaScript/TypeScript - enables interoperable multi-agent conversations
135 lines • 4.39 kB
TypeScript
/**
* @fileoverview Utility functions for the Open Floor Protocol
* @author Open Voice Interoperability Initiative
* @version 0.0.1
* @license Apache-2.0
*/
/**
* Parses an ISO 8601 duration string and returns the number of milliseconds
* Supports the format: P[n]D[T[n]H[n]M[n]S]
*
* @param duration - ISO 8601 duration string (e.g., "PT3H30M15S")
* @returns Duration in milliseconds
* @throws Error if the duration string is invalid
*
* @example
* ```typescript
* parseIsoDuration("PT1H30M"); // Returns 5400000 (1.5 hours in ms)
* parseIsoDuration("P1DT2H"); // Returns 93600000 (26 hours in ms)
* ```
*/
export declare function parseIsoDuration(duration: string): number;
/**
* Converts milliseconds to ISO 8601 duration format
*
* @param milliseconds - Duration in milliseconds
* @returns ISO 8601 duration string
*
* @example
* ```typescript
* millisecondsToIsoDuration(5400000); // Returns "PT1H30M"
* millisecondsToIsoDuration(90000); // Returns "PT1M30S"
* ```
*/
export declare function millisecondsToIsoDuration(milliseconds: number): string;
/**
* Generates a UUID v4 string using the available crypto API
* Falls back to a simple implementation if crypto.randomUUID is not available
*
* @returns A UUID v4 string
*
* @example
* ```typescript
* const id = generateUUID(); // "f47ac10b-58cc-4372-a567-0e02b2c3d479"
* ```
*/
export declare function generateUUID(): string;
/**
* Resolves JSON Path expressions with support for the substring() extension
*
* @param path - JSON Path expression (e.g., "$.features.text.tokens[0].value")
* @param data - Object to query
* @returns Array of matched values
*
* @example
* ```typescript
* const data = { features: { text: { tokens: [{ value: "hello world" }] } } };
* resolveJsonPath("$.features.text.tokens[0].value", data); // ["hello world"]
* resolveJsonPath("$.features.text.tokens[0].value.substring(0,5)", data); // ["hello"]
* ```
*/
export declare function resolveJsonPath(path: string, data: unknown): unknown[];
/**
* Validates that a string is a properly formatted URI
*
* @param uri - String to validate
* @returns True if the string is a valid URI format
*
* @example
* ```typescript
* isValidUri("tag:example.com,2025:agent1"); // true
* isValidUri("https://example.com/agent"); // true
* isValidUri("not-a-uri"); // false
* ```
*/
export declare function isValidUri(uri: string): boolean;
/**
* Validates that a string is a properly formatted URL
*
* @param url - String to validate
* @returns True if the string is a valid URL
*
* @example
* ```typescript
* isValidUrl("https://example.com/api"); // true
* isValidUrl("http://localhost:3000"); // true
* isValidUrl("not-a-url"); // false
* ```
*/
export declare function isValidUrl(url: string): boolean;
/**
* Validates that a confidence value is within the valid range (0.0 to 1.0)
*
* @param confidence - Confidence value to validate
* @returns True if the confidence is valid
*
* @example
* ```typescript
* isValidConfidence(0.95); // true
* isValidConfidence(1.0); // true
* isValidConfidence(1.5); // false
* ```
*/
export declare function isValidConfidence(confidence: number): boolean;
/**
* Validates encoding string against allowed values
*
* @param encoding - Encoding string to validate
* @returns True if encoding is valid
*/
export declare function isValidEncoding(encoding: string): boolean;
/**
* Deep clones an object using structuredClone if available, otherwise JSON fallback
*
* @param obj - Object to clone
* @returns Deep clone of the object
*/
export declare function deepClone<T>(obj: T): T;
/**
* Checks if an object has all required properties
*
* @param obj - Object to check
* @param requiredProps - Array of required property names
* @returns True if all required properties are present and not undefined
*/
export declare function hasRequiredProperties(obj: unknown, requiredProps: string[]): obj is Record<string, unknown>;
/**
* Creates a standardized error message for validation failures
*
* @param fieldName - Name of the field that failed validation
* @param value - The invalid value
* @param requirement - Description of what was required
* @returns Formatted error message
*/
export declare function createValidationError(fieldName: string, value: unknown, requirement: string): string;
//# sourceMappingURL=utils.d.ts.map