@theokit/gateway 0.17.0 → 0.18.0
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/CHANGELOG.md +6 -0
- package/README.md +24 -0
- package/dist/index.cjs +49 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +21 -1
- package/dist/index.d.ts +21 -1
- package/dist/index.js +49 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -1608,6 +1608,26 @@ declare class GatewayRunner {
|
|
|
1608
1608
|
private buildContext;
|
|
1609
1609
|
}
|
|
1610
1610
|
|
|
1611
|
+
/** Options for `withMediaUnderstanding`: the functions that understand media, a size cap and a failure sink. */
|
|
1612
|
+
interface MediaUnderstandingOptions {
|
|
1613
|
+
/** e.g. plugin-voice's transcribe, bound to its config. */
|
|
1614
|
+
transcribe?(audio: {
|
|
1615
|
+
data: Uint8Array;
|
|
1616
|
+
mediaType: string;
|
|
1617
|
+
}): Promise<string>;
|
|
1618
|
+
/** Called for each image an event carries, once inbound images exist. */
|
|
1619
|
+
describeImage?(image: {
|
|
1620
|
+
data: Uint8Array;
|
|
1621
|
+
mediaType: string;
|
|
1622
|
+
}): Promise<string>;
|
|
1623
|
+
/** Largest payload sent to a function. Default 20 MiB; larger is reported as not understood. */
|
|
1624
|
+
readonly maxBytes?: number;
|
|
1625
|
+
/** Where a failure is reported, with the media type and the error name only. */
|
|
1626
|
+
readonly write?: (line: string) => void;
|
|
1627
|
+
}
|
|
1628
|
+
/** Wraps a handler: the event it receives has the transcript or description appended to text. */
|
|
1629
|
+
declare function withMediaUnderstanding(handler: GatewayHandler, options: MediaUnderstandingOptions): GatewayHandler;
|
|
1630
|
+
|
|
1611
1631
|
/** A credential shape this module masks, and the cost of matching it. */
|
|
1612
1632
|
interface CoveredShape {
|
|
1613
1633
|
/** The platform and field, for the reader of the table. */
|
|
@@ -1997,4 +2017,4 @@ declare function listenerLifecycle(opts: {
|
|
|
1997
2017
|
stop(): Promise<void>;
|
|
1998
2018
|
};
|
|
1999
2019
|
|
|
2000
|
-
export { type AgentIdStrategy, type BaseMessageEvent, BasePlatformAdapter, type BuiltChannel, type BuzzMessageEvent, CREDENTIAL_SHAPES, type ChannelConfigField, type ChannelDescriptor, type ChannelEntry, type ChannelRejection, type ChannelRendering, type ChannelWebhook, type ChunkByGraphemeOptions, type ChunkTextOptions, type ClickClackMessageEvent, type ConnectFailure, type CoveredShape, type CreateChannelsResult, DEFAULT_WEBHOOK_MAX_BYTES, type DeliveryRequest, DeliveryRouter, type DeliveryTarget, type DiscordMessageEvent, type EmailMessageEvent, type FeishuLarkMessageEvent, type FenceRegion, GatewayConfigurationError, type GatewayConfigurationErrorOptions, type GatewayContext, type GatewayHandler, type GatewayHook, GatewayLifecycleError, type GatewayLifecycleErrorOptions, GatewayRunner, type GatewayRunnerOptions, type GoogleChatMessageEvent, type HookDecision, HookExecutor, type HookName, type IMessageMessageEvent, type InboundAudio, type IngestResult, type IrcMessageEvent, type LineMessageEvent, type ListenableApp, type ListenerLike, type MatrixMessageEvent, type MattermostMessageEvent, type MessageEvent, type NextcloudTalkMessageEvent, type NostrMessageEvent, type OnErrorContext, type OutboundAttachment, type OutboundMessage, type PlatformEventRegistry, type PlatformName, type PostOutboundContext, type PreInboundContext, type QQMessageEvent, type RaftMessageEvent, type RawBodyCaptureOptions, type RawBodyRequest, type ReconnectPolicy, type SMSMessageEvent, SeenIds, type SendResult, SessionRouter, type SignalMessageEvent, type SlackMessageEvent, type StartResult, type SynologyChatMessageEvent, type TeamsMessageEvent, type TelegramMessageEvent, type TlonUrbitMessageEvent, type TwitchMessageEvent, type UncoveredShape, type WeChatMessageEvent, type WeComMessageEvent, type WebhookRequest, WeixinAdapterBase, type WeixinAdapterConfig, type WeixinFetch, type WeixinSendRequest, type WeixinWebhookAnswer, type WhatsAppMessageEvent, type XMessageEvent, type XThreadTweet, type YuanbaoMessageEvent, type ZaloMessageEvent, type ZaloPersonalMessageEvent, channelRendering, chunkByGrapheme, chunkText, createChannels, defaultStrategy, listenerLifecycle, loadPeer, maskShapes, parseFlatXml, rawBodyCapture, redactSecrets, scanFences, splitKeepingRows, toDialect };
|
|
2020
|
+
export { type AgentIdStrategy, type BaseMessageEvent, BasePlatformAdapter, type BuiltChannel, type BuzzMessageEvent, CREDENTIAL_SHAPES, type ChannelConfigField, type ChannelDescriptor, type ChannelEntry, type ChannelRejection, type ChannelRendering, type ChannelWebhook, type ChunkByGraphemeOptions, type ChunkTextOptions, type ClickClackMessageEvent, type ConnectFailure, type CoveredShape, type CreateChannelsResult, DEFAULT_WEBHOOK_MAX_BYTES, type DeliveryRequest, DeliveryRouter, type DeliveryTarget, type DiscordMessageEvent, type EmailMessageEvent, type FeishuLarkMessageEvent, type FenceRegion, GatewayConfigurationError, type GatewayConfigurationErrorOptions, type GatewayContext, type GatewayHandler, type GatewayHook, GatewayLifecycleError, type GatewayLifecycleErrorOptions, GatewayRunner, type GatewayRunnerOptions, type GoogleChatMessageEvent, type HookDecision, HookExecutor, type HookName, type IMessageMessageEvent, type InboundAudio, type IngestResult, type IrcMessageEvent, type LineMessageEvent, type ListenableApp, type ListenerLike, type MatrixMessageEvent, type MattermostMessageEvent, type MediaUnderstandingOptions, type MessageEvent, type NextcloudTalkMessageEvent, type NostrMessageEvent, type OnErrorContext, type OutboundAttachment, type OutboundMessage, type PlatformEventRegistry, type PlatformName, type PostOutboundContext, type PreInboundContext, type QQMessageEvent, type RaftMessageEvent, type RawBodyCaptureOptions, type RawBodyRequest, type ReconnectPolicy, type SMSMessageEvent, SeenIds, type SendResult, SessionRouter, type SignalMessageEvent, type SlackMessageEvent, type StartResult, type SynologyChatMessageEvent, type TeamsMessageEvent, type TelegramMessageEvent, type TlonUrbitMessageEvent, type TwitchMessageEvent, type UncoveredShape, type WeChatMessageEvent, type WeComMessageEvent, type WebhookRequest, WeixinAdapterBase, type WeixinAdapterConfig, type WeixinFetch, type WeixinSendRequest, type WeixinWebhookAnswer, type WhatsAppMessageEvent, type XMessageEvent, type XThreadTweet, type YuanbaoMessageEvent, type ZaloMessageEvent, type ZaloPersonalMessageEvent, channelRendering, chunkByGrapheme, chunkText, createChannels, defaultStrategy, listenerLifecycle, loadPeer, maskShapes, parseFlatXml, rawBodyCapture, redactSecrets, scanFences, splitKeepingRows, toDialect, withMediaUnderstanding };
|
package/dist/index.d.ts
CHANGED
|
@@ -1608,6 +1608,26 @@ declare class GatewayRunner {
|
|
|
1608
1608
|
private buildContext;
|
|
1609
1609
|
}
|
|
1610
1610
|
|
|
1611
|
+
/** Options for `withMediaUnderstanding`: the functions that understand media, a size cap and a failure sink. */
|
|
1612
|
+
interface MediaUnderstandingOptions {
|
|
1613
|
+
/** e.g. plugin-voice's transcribe, bound to its config. */
|
|
1614
|
+
transcribe?(audio: {
|
|
1615
|
+
data: Uint8Array;
|
|
1616
|
+
mediaType: string;
|
|
1617
|
+
}): Promise<string>;
|
|
1618
|
+
/** Called for each image an event carries, once inbound images exist. */
|
|
1619
|
+
describeImage?(image: {
|
|
1620
|
+
data: Uint8Array;
|
|
1621
|
+
mediaType: string;
|
|
1622
|
+
}): Promise<string>;
|
|
1623
|
+
/** Largest payload sent to a function. Default 20 MiB; larger is reported as not understood. */
|
|
1624
|
+
readonly maxBytes?: number;
|
|
1625
|
+
/** Where a failure is reported, with the media type and the error name only. */
|
|
1626
|
+
readonly write?: (line: string) => void;
|
|
1627
|
+
}
|
|
1628
|
+
/** Wraps a handler: the event it receives has the transcript or description appended to text. */
|
|
1629
|
+
declare function withMediaUnderstanding(handler: GatewayHandler, options: MediaUnderstandingOptions): GatewayHandler;
|
|
1630
|
+
|
|
1611
1631
|
/** A credential shape this module masks, and the cost of matching it. */
|
|
1612
1632
|
interface CoveredShape {
|
|
1613
1633
|
/** The platform and field, for the reader of the table. */
|
|
@@ -1997,4 +2017,4 @@ declare function listenerLifecycle(opts: {
|
|
|
1997
2017
|
stop(): Promise<void>;
|
|
1998
2018
|
};
|
|
1999
2019
|
|
|
2000
|
-
export { type AgentIdStrategy, type BaseMessageEvent, BasePlatformAdapter, type BuiltChannel, type BuzzMessageEvent, CREDENTIAL_SHAPES, type ChannelConfigField, type ChannelDescriptor, type ChannelEntry, type ChannelRejection, type ChannelRendering, type ChannelWebhook, type ChunkByGraphemeOptions, type ChunkTextOptions, type ClickClackMessageEvent, type ConnectFailure, type CoveredShape, type CreateChannelsResult, DEFAULT_WEBHOOK_MAX_BYTES, type DeliveryRequest, DeliveryRouter, type DeliveryTarget, type DiscordMessageEvent, type EmailMessageEvent, type FeishuLarkMessageEvent, type FenceRegion, GatewayConfigurationError, type GatewayConfigurationErrorOptions, type GatewayContext, type GatewayHandler, type GatewayHook, GatewayLifecycleError, type GatewayLifecycleErrorOptions, GatewayRunner, type GatewayRunnerOptions, type GoogleChatMessageEvent, type HookDecision, HookExecutor, type HookName, type IMessageMessageEvent, type InboundAudio, type IngestResult, type IrcMessageEvent, type LineMessageEvent, type ListenableApp, type ListenerLike, type MatrixMessageEvent, type MattermostMessageEvent, type MessageEvent, type NextcloudTalkMessageEvent, type NostrMessageEvent, type OnErrorContext, type OutboundAttachment, type OutboundMessage, type PlatformEventRegistry, type PlatformName, type PostOutboundContext, type PreInboundContext, type QQMessageEvent, type RaftMessageEvent, type RawBodyCaptureOptions, type RawBodyRequest, type ReconnectPolicy, type SMSMessageEvent, SeenIds, type SendResult, SessionRouter, type SignalMessageEvent, type SlackMessageEvent, type StartResult, type SynologyChatMessageEvent, type TeamsMessageEvent, type TelegramMessageEvent, type TlonUrbitMessageEvent, type TwitchMessageEvent, type UncoveredShape, type WeChatMessageEvent, type WeComMessageEvent, type WebhookRequest, WeixinAdapterBase, type WeixinAdapterConfig, type WeixinFetch, type WeixinSendRequest, type WeixinWebhookAnswer, type WhatsAppMessageEvent, type XMessageEvent, type XThreadTweet, type YuanbaoMessageEvent, type ZaloMessageEvent, type ZaloPersonalMessageEvent, channelRendering, chunkByGrapheme, chunkText, createChannels, defaultStrategy, listenerLifecycle, loadPeer, maskShapes, parseFlatXml, rawBodyCapture, redactSecrets, scanFences, splitKeepingRows, toDialect };
|
|
2020
|
+
export { type AgentIdStrategy, type BaseMessageEvent, BasePlatformAdapter, type BuiltChannel, type BuzzMessageEvent, CREDENTIAL_SHAPES, type ChannelConfigField, type ChannelDescriptor, type ChannelEntry, type ChannelRejection, type ChannelRendering, type ChannelWebhook, type ChunkByGraphemeOptions, type ChunkTextOptions, type ClickClackMessageEvent, type ConnectFailure, type CoveredShape, type CreateChannelsResult, DEFAULT_WEBHOOK_MAX_BYTES, type DeliveryRequest, DeliveryRouter, type DeliveryTarget, type DiscordMessageEvent, type EmailMessageEvent, type FeishuLarkMessageEvent, type FenceRegion, GatewayConfigurationError, type GatewayConfigurationErrorOptions, type GatewayContext, type GatewayHandler, type GatewayHook, GatewayLifecycleError, type GatewayLifecycleErrorOptions, GatewayRunner, type GatewayRunnerOptions, type GoogleChatMessageEvent, type HookDecision, HookExecutor, type HookName, type IMessageMessageEvent, type InboundAudio, type IngestResult, type IrcMessageEvent, type LineMessageEvent, type ListenableApp, type ListenerLike, type MatrixMessageEvent, type MattermostMessageEvent, type MediaUnderstandingOptions, type MessageEvent, type NextcloudTalkMessageEvent, type NostrMessageEvent, type OnErrorContext, type OutboundAttachment, type OutboundMessage, type PlatformEventRegistry, type PlatformName, type PostOutboundContext, type PreInboundContext, type QQMessageEvent, type RaftMessageEvent, type RawBodyCaptureOptions, type RawBodyRequest, type ReconnectPolicy, type SMSMessageEvent, SeenIds, type SendResult, SessionRouter, type SignalMessageEvent, type SlackMessageEvent, type StartResult, type SynologyChatMessageEvent, type TeamsMessageEvent, type TelegramMessageEvent, type TlonUrbitMessageEvent, type TwitchMessageEvent, type UncoveredShape, type WeChatMessageEvent, type WeComMessageEvent, type WebhookRequest, WeixinAdapterBase, type WeixinAdapterConfig, type WeixinFetch, type WeixinSendRequest, type WeixinWebhookAnswer, type WhatsAppMessageEvent, type XMessageEvent, type XThreadTweet, type YuanbaoMessageEvent, type ZaloMessageEvent, type ZaloPersonalMessageEvent, channelRendering, chunkByGrapheme, chunkText, createChannels, defaultStrategy, listenerLifecycle, loadPeer, maskShapes, parseFlatXml, rawBodyCapture, redactSecrets, scanFences, splitKeepingRows, toDialect, withMediaUnderstanding };
|
package/dist/index.js
CHANGED
|
@@ -1248,6 +1248,54 @@ var GatewayRunner = class {
|
|
|
1248
1248
|
}
|
|
1249
1249
|
};
|
|
1250
1250
|
|
|
1251
|
+
// src/runner/media-understanding.ts
|
|
1252
|
+
var DEFAULT_MAX_BYTES = 20 * 1024 * 1024;
|
|
1253
|
+
function sanitizeType(mediaType) {
|
|
1254
|
+
return mediaType.replace(/[^\w/.+-]/g, "");
|
|
1255
|
+
}
|
|
1256
|
+
function errorName(error) {
|
|
1257
|
+
return error instanceof Error ? error.name : "UnknownError";
|
|
1258
|
+
}
|
|
1259
|
+
function withMediaUnderstanding(handler, options) {
|
|
1260
|
+
const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
1261
|
+
const write = options.write ?? ((line) => void process.stderr.write(`${line}
|
|
1262
|
+
`));
|
|
1263
|
+
const understand = async (kind, media, fn) => {
|
|
1264
|
+
const type = sanitizeType(media.mediaType);
|
|
1265
|
+
if (fn === void 0) return `[Attachment of type ${type} not understood]`;
|
|
1266
|
+
const failed = `[Attachment of type ${type} could not be understood]`;
|
|
1267
|
+
if (media.data.byteLength > maxBytes) {
|
|
1268
|
+
report(type, "TooLarge");
|
|
1269
|
+
return failed;
|
|
1270
|
+
}
|
|
1271
|
+
try {
|
|
1272
|
+
const text = await fn({ data: media.data, mediaType: media.mediaType });
|
|
1273
|
+
return `[${kind}: ${text}]`;
|
|
1274
|
+
} catch (error) {
|
|
1275
|
+
report(type, errorName(error));
|
|
1276
|
+
return failed;
|
|
1277
|
+
}
|
|
1278
|
+
};
|
|
1279
|
+
const report = (type, name) => {
|
|
1280
|
+
try {
|
|
1281
|
+
write(`[gateway] media understanding failed for ${type}: ${name}`);
|
|
1282
|
+
} catch {
|
|
1283
|
+
}
|
|
1284
|
+
};
|
|
1285
|
+
return async (event, ctx) => {
|
|
1286
|
+
const lines = [];
|
|
1287
|
+
if (event.audio !== void 0) {
|
|
1288
|
+
lines.push(
|
|
1289
|
+
await understand("Audio transcript", event.audio, options.transcribe?.bind(options))
|
|
1290
|
+
);
|
|
1291
|
+
}
|
|
1292
|
+
if (lines.length === 0) return handler(event, ctx);
|
|
1293
|
+
const text = [event.text, ...lines].filter((part) => part !== "").join("\n");
|
|
1294
|
+
const wrapped = { ...event, text };
|
|
1295
|
+
return handler(wrapped, ctx);
|
|
1296
|
+
};
|
|
1297
|
+
}
|
|
1298
|
+
|
|
1251
1299
|
// src/session/router.ts
|
|
1252
1300
|
function defaultStrategy(event) {
|
|
1253
1301
|
const { platform, channel, sender } = event;
|
|
@@ -1622,6 +1670,6 @@ function listenerLifecycle(opts) {
|
|
|
1622
1670
|
};
|
|
1623
1671
|
}
|
|
1624
1672
|
|
|
1625
|
-
export { BasePlatformAdapter, CREDENTIAL_SHAPES, DEFAULT_WEBHOOK_MAX_BYTES, DeliveryRouter, GatewayConfigurationError, GatewayLifecycleError, GatewayRunner, HookExecutor, SeenIds, SessionRouter, WeixinAdapterBase, channelRendering, chunkByGrapheme, chunkText, createChannels, defaultStrategy, listenerLifecycle, loadPeer, maskShapes, parseFlatXml, rawBodyCapture, redactSecrets, scanFences, splitKeepingRows, toDialect };
|
|
1673
|
+
export { BasePlatformAdapter, CREDENTIAL_SHAPES, DEFAULT_WEBHOOK_MAX_BYTES, DeliveryRouter, GatewayConfigurationError, GatewayLifecycleError, GatewayRunner, HookExecutor, SeenIds, SessionRouter, WeixinAdapterBase, channelRendering, chunkByGrapheme, chunkText, createChannels, defaultStrategy, listenerLifecycle, loadPeer, maskShapes, parseFlatXml, rawBodyCapture, redactSecrets, scanFences, splitKeepingRows, toDialect, withMediaUnderstanding };
|
|
1626
1674
|
//# sourceMappingURL=index.js.map
|
|
1627
1675
|
//# sourceMappingURL=index.js.map
|