UNPKG

@openfloor/protocol

Version:

Open Floor Protocol implementation for JavaScript/TypeScript - enables interoperable multi-agent conversations

228 lines 7.13 kB
/** * @fileoverview Dialog Event implementation for the Open Floor Protocol * Implements the Dialog Event Object Specification Version 1.0.2 * @author Open Voice Interoperability Initiative * @version 0.0.1 * @license Apache-2.0 */ import { SpanOptions, TokenOptions, FeatureOptions, TextFeatureOptions, DialogEventOptions, JsonSerializable } from './types'; /** * Represents a time span for a dialog event or token according to the Open Floor specification. * Time spans can be absolute (using Date objects) or relative (using duration offsets). * * @example * ```typescript * // Absolute time span * const span1 = new Span({ startTime: new Date(), endTime: new Date(Date.now() + 5000) }); * * // Relative time span * const span2 = new Span({ startOffset: 1000, endOffset: 5000 }); * ``` */ export declare class Span implements JsonSerializable { readonly startTime?: Date; readonly startOffset?: number; readonly endTime?: Date; readonly endOffset?: number; /** * Creates a new Span instance * @param options - Span configuration options * @throws Error if invalid combination of time parameters is provided */ constructor(options?: SpanOptions); /** * Convert to plain object for JSON serialization */ toObject(): Record<string, unknown>; /** * Convert to JSON string */ toJSON(): string; /** * Creates a Span instance from a dictionary with automatic type conversion */ static fromObject(data: Record<string, unknown>): Span; } /** * Represents a single token in a dialog event feature with optional metadata. * Tokens are the fundamental units of information within features. * * @example * ```typescript * // Simple text token * const token1 = new Token({ value: "Hello world" }); * * // Token with confidence and span * const token2 = new Token({ * value: "Hello", * confidence: 0.95, * span: { startOffset: 0, endOffset: 1000 } * }); * * // Token with links to other features * const token3 = new Token({ * value: { intent: "greeting" }, * links: ["$.textFeature.tokens[0].value"] * }); * ``` */ export declare class Token implements JsonSerializable { readonly value?: unknown; readonly valueUrl?: string; readonly span?: Span; readonly confidence?: number; readonly links: readonly string[]; /** * Creates a new Token instance * @param options - Token configuration options * @throws Error if invalid configuration is provided */ constructor(options?: TokenOptions); /** * Convert to plain object for JSON serialization */ toObject(): Record<string, unknown>; /** * Convert to JSON string */ toJSON(): string; /** * Creates a Token instance from a dictionary with nested object conversion */ static fromObject(data: Record<string, unknown>): Token; /** * Resolves JSON Path links within a dialog event to find linked values * @param dialogEvent - The parent dialog event to search within * @returns Array of [path, value] pairs for matched links */ getLinkedValues(dialogEvent: DialogEvent): Array<[string, unknown]>; } /** * Represents a feature in a dialog event according to the Open Floor specification. * Features contain tokens and metadata about different aspects of the dialog event. * * @example * ```typescript * // Text feature with multiple tokens * const feature = new Feature({ * mimeType: 'text/plain', * tokens: [ * { value: 'Hello' }, * { value: 'world' } * ] * }); * * // Feature with alternatives and language * const feature2 = new Feature({ * mimeType: 'text/plain', * lang: 'en-US', * tokens: [{ value: 'Hello', confidence: 0.95 }], * alternates: [[{ value: 'Hi', confidence: 0.85 }]] * }); * ``` */ export declare class Feature implements JsonSerializable { readonly mimeType: string; readonly tokens: readonly Token[]; readonly alternates: readonly (readonly Token[])[]; readonly lang?: string; readonly encoding?: string; readonly tokenSchema?: string; /** * Creates a new Feature instance * @param options - Feature configuration options * @throws Error if invalid configuration is provided */ constructor(options: FeatureOptions); /** * Convert to plain object for JSON serialization */ toObject(): Record<string, unknown>; /** * Convert to JSON string */ toJSON(): string; /** * Creates a Feature instance from a dictionary with nested object conversion */ static fromObject(data: Record<string, unknown>): Feature; } /** * Specialized text feature with convenient string value handling. * Automatically sets mimeType to 'text/plain' and provides a values convenience property. * * @example * ```typescript * // Create from string values * const textFeature = new TextFeature({ values: ['Hello', 'world'] }); * * // Create with full token options * const textFeature2 = new TextFeature({ * tokens: [{ value: 'Hello', confidence: 0.95 }], * lang: 'en-US' * }); * ``` */ export declare class TextFeature extends Feature { /** * Creates a new TextFeature instance * @param options - TextFeature configuration options */ constructor(options?: TextFeatureOptions); } /** * Represents a dialog event according to the Open Floor specification. * Dialog events capture linguistic events with features, timing, and speaker information. * * @example * ```typescript * // Simple text dialog event * const event = new DialogEvent({ * speakerUri: 'tag:example.com,2025:user1', * features: { * text: { mimeType: 'text/plain', tokens: [{ value: 'Hello world' }] } * } * }); * * // Dialog event with timing and context * const event2 = new DialogEvent({ * speakerUri: 'tag:example.com,2025:agent1', * span: { startTime: new Date() }, * features: { * text: { mimeType: 'text/plain', tokens: [{ value: 'How can I help?' }] } * }, * context: 'Greeting response' * }); * ``` */ export declare class DialogEvent implements JsonSerializable { readonly id: string; readonly speakerUri: string; readonly span: Span; readonly features: ReadonlyMap<string, Feature>; readonly previousId?: string; readonly context?: string; /** * Creates a new DialogEvent instance * @param options - DialogEvent configuration options * @throws Error if required parameters are missing */ constructor(options: DialogEventOptions); /** * Convert to plain object for JSON serialization */ toObject(): Record<string, unknown>; /** * Convert to JSON string */ toJSON(): string; /** * Creates a DialogEvent instance from a dictionary with nested object conversion */ static fromObject(data: Record<string, unknown>): DialogEvent; } /** * Type alias for an array of DialogEvent instances representing conversation history */ export type DialogHistory = DialogEvent[]; //# sourceMappingURL=dialog-event.d.ts.map