@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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.18.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 58aadd0: Add `withMediaUnderstanding`, a handler wrapper that folds a voice note's transcript into the turn text, naming any attachment it could not understand.
8
+
3
9
  ## 0.17.0
4
10
 
5
11
  ### Minor Changes
package/README.md CHANGED
@@ -167,6 +167,30 @@ const runner = new GatewayRunner({
167
167
  });
168
168
  ```
169
169
 
170
+ ### Folding a transcript into the turn text
171
+
172
+ A voice note's `text` is empty, so the agent would see nothing. `withMediaUnderstanding` wraps a
173
+ handler and appends one line per item to `text`: `[Audio transcript: ...]`, or
174
+ `[Attachment of type audio/ogg could not be understood]` when the function throws or the payload
175
+ exceeds `maxBytes` (default 20 MiB), or `[Attachment of type audio/ogg not understood]` when no
176
+ function is configured. The media type is sanitized to `[\w/.+-]`, the event keeps `audio`, and
177
+ the handler always runs once. `write` receives one line per failure with the type and error name
178
+ only. The gateway makes no network call; pass plugin-voice's `transcribe`:
179
+
180
+ ```typescript
181
+ import { GatewayRunner, withMediaUnderstanding } from "@theokit/gateway";
182
+ import { transcribe } from "@theokit/plugin-voice";
183
+
184
+ const runner = new GatewayRunner({
185
+ adapters: [adapter],
186
+ handler: withMediaUnderstanding(answer, {
187
+ transcribe: (audio) => transcribe(audio, voiceConfig),
188
+ }),
189
+ });
190
+ ```
191
+
192
+ `describeImage` is accepted for when events carry image bytes; no adapter fills them today.
193
+
170
194
  ## Editing a sent message
171
195
 
172
196
  Six adapters can replace the text of a message they sent, keeping its id: Telegram, Discord,
package/dist/index.cjs CHANGED
@@ -1250,6 +1250,54 @@ var GatewayRunner = class {
1250
1250
  }
1251
1251
  };
1252
1252
 
1253
+ // src/runner/media-understanding.ts
1254
+ var DEFAULT_MAX_BYTES = 20 * 1024 * 1024;
1255
+ function sanitizeType(mediaType) {
1256
+ return mediaType.replace(/[^\w/.+-]/g, "");
1257
+ }
1258
+ function errorName(error) {
1259
+ return error instanceof Error ? error.name : "UnknownError";
1260
+ }
1261
+ function withMediaUnderstanding(handler, options) {
1262
+ const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
1263
+ const write = options.write ?? ((line) => void process.stderr.write(`${line}
1264
+ `));
1265
+ const understand = async (kind, media, fn) => {
1266
+ const type = sanitizeType(media.mediaType);
1267
+ if (fn === void 0) return `[Attachment of type ${type} not understood]`;
1268
+ const failed = `[Attachment of type ${type} could not be understood]`;
1269
+ if (media.data.byteLength > maxBytes) {
1270
+ report(type, "TooLarge");
1271
+ return failed;
1272
+ }
1273
+ try {
1274
+ const text = await fn({ data: media.data, mediaType: media.mediaType });
1275
+ return `[${kind}: ${text}]`;
1276
+ } catch (error) {
1277
+ report(type, errorName(error));
1278
+ return failed;
1279
+ }
1280
+ };
1281
+ const report = (type, name) => {
1282
+ try {
1283
+ write(`[gateway] media understanding failed for ${type}: ${name}`);
1284
+ } catch {
1285
+ }
1286
+ };
1287
+ return async (event, ctx) => {
1288
+ const lines = [];
1289
+ if (event.audio !== void 0) {
1290
+ lines.push(
1291
+ await understand("Audio transcript", event.audio, options.transcribe?.bind(options))
1292
+ );
1293
+ }
1294
+ if (lines.length === 0) return handler(event, ctx);
1295
+ const text = [event.text, ...lines].filter((part) => part !== "").join("\n");
1296
+ const wrapped = { ...event, text };
1297
+ return handler(wrapped, ctx);
1298
+ };
1299
+ }
1300
+
1253
1301
  // src/session/router.ts
1254
1302
  function defaultStrategy(event) {
1255
1303
  const { platform, channel, sender } = event;
@@ -1649,5 +1697,6 @@ exports.redactSecrets = redactSecrets;
1649
1697
  exports.scanFences = scanFences;
1650
1698
  exports.splitKeepingRows = splitKeepingRows;
1651
1699
  exports.toDialect = toDialect;
1700
+ exports.withMediaUnderstanding = withMediaUnderstanding;
1652
1701
  //# sourceMappingURL=index.cjs.map
1653
1702
  //# sourceMappingURL=index.cjs.map