src/dispatcher.ts routes a directive to capability.on(name | "*"), device.onDirective() or device.onReportState(), with the payload checked by the descriptor of the interface. The DirectiveContext answers with respond/report/defer/error; respond() and report() start from device.state(). A handler or 1.x listener that throws or rejects is answered with INTERNAL_ERROR (an AlexaError with itself) and reported through "error", never as an unhandled rejection. Undeclared interface, unknown directive, AdjustMode on an unordered mode and a missing handler get INVALID_DIRECTIVE, a bad payload INVALID_VALUE; a device with an "Event" or "ReportState" listener keeps the answer to itself. Every answer passes the dispatcher: the first one per correlationToken is published, a second is refused. No answer within answerWithinMs (6500, 0 = off, unref'd timer) sends INTERNAL_ERROR and emits "unanswered". A messageId that arrived in the last 60 s is dropped. New: "unknownEndpoint", options answerUnknownEndpoints, publisher, timers, and bridge.receive() to run a bridge without a broker. 168 tests pass (18 new). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
99 lines
4.7 KiB
TypeScript
99 lines
4.7 KiB
TypeScript
import type { ChangeCause, ChangeReportMessage, DeferredResponseMessage, ErrorResponseMessage, ProactiveEventMessage, Property, ResponseMessage, SceneEventMessage } from "./types.js";
|
|
/** A message that would be dropped on its way to Alexa, refused where it is built. */
|
|
export declare class MessageError extends Error {
|
|
readonly endpointId: string;
|
|
readonly problem: string;
|
|
constructor(endpointId: string, problem: string);
|
|
}
|
|
interface Envelope {
|
|
endpointId: string;
|
|
/** Default: a new version 4 UUID, which is what Alexa recommends (message-guide.html, "Header object"). */
|
|
messageId?: string;
|
|
}
|
|
interface Answer extends Envelope {
|
|
/** The correlationToken of the directive that is answered. */
|
|
correlationToken: string;
|
|
}
|
|
export interface ResponseFields extends Answer {
|
|
/** Default: "Response". */
|
|
name?: string;
|
|
/** Default: "Alexa". An interface with a response of its own names it: Alexa.SecurityPanelController, Arm.Response. */
|
|
namespace?: string;
|
|
payload?: Record<string, unknown>;
|
|
/** The state of the endpoint after the directive. */
|
|
context?: readonly Property[];
|
|
}
|
|
/**
|
|
* The answer to a directive (alexa-response.html, "Synchronous response"). An endpoint with nothing to report
|
|
* answers with an empty list of properties, not without a context (state-reporting-for-smart-home-addons.html,
|
|
* "Directive response example for a property that isn't retrievable").
|
|
*/
|
|
export declare function response(fields: ResponseFields): ResponseMessage;
|
|
/** The answer to ReportState: every retrievable property of the endpoint. */
|
|
export declare function stateReport(fields: Answer & {
|
|
context?: readonly Property[];
|
|
}): ResponseMessage;
|
|
/**
|
|
* "The directive arrived, the answer follows": no context, the state is in the Response that follows
|
|
* (alexa-response.html, "Deferred response example").
|
|
*/
|
|
export declare function deferredResponse(fields: Answer & {
|
|
estimatedDeferralInSeconds?: number;
|
|
}): DeferredResponseMessage;
|
|
export interface ErrorResponseFields extends Answer {
|
|
/** "ENDPOINT_UNREACHABLE" */
|
|
type: string;
|
|
/** For the log of the skill. */
|
|
message: string;
|
|
/** The fields a type adds to the payload: validRange, currentDeviceMode. */
|
|
extra?: Record<string, unknown>;
|
|
/** Default: the namespace the type is documented under, see errorNamespace(). */
|
|
namespace?: string;
|
|
}
|
|
/** The answer to a directive the endpoint could not follow (alexa-errorresponse.html). */
|
|
export declare function errorResponse(fields: ErrorResponseFields): ErrorResponseMessage;
|
|
export interface ChangeReportFields extends Envelope {
|
|
/** Default: "PHYSICAL_INTERACTION". */
|
|
cause?: ChangeCause;
|
|
/** The properties that changed, at least one. */
|
|
changed: readonly Property[];
|
|
/** The other properties of the endpoint, as they are now. */
|
|
context?: readonly Property[];
|
|
}
|
|
/** The same property of the same instance, whatever its value. */
|
|
export declare const sameProperty: (a: Property, b: Property) => boolean;
|
|
/**
|
|
* A change of state nobody asked for. The header has no correlationToken (message-guide.html, "Header object").
|
|
* A property that changed is left out of the context: it is reported in one of the two (same page, "Context
|
|
* object"). Throws MessageError when nothing changed: Alex2MQTT drops such a report without a word.
|
|
*/
|
|
export declare function changeReport(fields: ChangeReportFields): ChangeReportMessage;
|
|
export interface SceneEventFields extends Answer {
|
|
/** true: ActivationStarted. false: DeactivationStarted. */
|
|
activated: boolean;
|
|
/** Default: "VOICE_INTERACTION", a scene is started by a directive. */
|
|
cause?: ChangeCause;
|
|
/** When the scene started. Default: now. */
|
|
timestamp?: string | Date;
|
|
}
|
|
/** The answer to Activate and Deactivate of a scene (alexa-scenecontroller.html). */
|
|
export declare function sceneEvent(fields: SceneEventFields): SceneEventMessage;
|
|
export interface DoorbellPressFields extends Envelope {
|
|
/** Default: "PHYSICAL_INTERACTION", somebody pressed the button. */
|
|
cause?: ChangeCause;
|
|
/** When the button was pressed. Default: now. */
|
|
timestamp?: string | Date;
|
|
}
|
|
/** Somebody rang (alexa-doorbelleventsource.html). */
|
|
export declare function doorbellPress(fields: DoorbellPressFields): ProactiveEventMessage;
|
|
export interface SimpleEventFields extends Envelope {
|
|
/** The instance of Alexa.SimpleEventSource that raises the event. */
|
|
instance: string;
|
|
/** The id of the event as discovery listed it: "Button.SinglePush.1". */
|
|
id: string;
|
|
/** When the event happened. Default: now. */
|
|
timestamp?: string | Date;
|
|
}
|
|
/** An event of a button or a sensor that routines start on (alexa-simpleeventsource.html). */
|
|
export declare function simpleEvent(fields: SimpleEventFields): ProactiveEventMessage;
|
|
export {};
|