@ours.network/tg-connector 0.2.0 → 0.3.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/README.md CHANGED
@@ -205,8 +205,18 @@ added for provenance:
205
205
  delivered as **text only** (no `send_file`, no `attachment` block — the
206
206
  `transcription` block already signals it came from voice). Set
207
207
  `forwardVoiceAudio:true` to also `send_file` the `.ogg` alongside the transcript.
208
+ - **Receiver interoperability.** Whenever a semantic Telegram `voice` attachment
209
+ is forwarded (STT is off/unavailable/fails/is over the cap, or
210
+ `forwardVoiceAudio:true`), the original OGG/Opus bytes travel unchanged under
211
+ a path-safe `voice_<message-id>.ogg` filename. Both `send_file.mime` and the
212
+ correlated envelope's `attachment.mime` are exactly
213
+ `audio/ogg; x-ours-kind=voice-message`, which is the ours-mcp recognition
214
+ contract. The semantic Telegram attachment kind is authoritative: an ordinary
215
+ `audio` attachment whose MIME is plain `audio/ogg` is not marked as voice.
216
+ See [the connector/receiver contract](docs/voice-message-contract.md) for the
217
+ independent verification matrix.
208
218
  - **Graceful degradation.** If STT is disabled, fails, times out, or the audio is
209
- over `sttMaxBytes`, the bridge falls back to **today's exact behaviour** — the
219
+ over `sttMaxBytes`, the bridge falls back to file forwarding — the
210
220
  `.ogg` is still forwarded via `send_file` and nothing the user sent is lost. When
211
221
  STT was attempted and failed, the envelope adds `transcription.status:"error"`
212
222
  (e.g. `error:"too_large"` for the size guard); the daemon logs a single line
package/dist/connector.js CHANGED
@@ -25910,6 +25910,7 @@ var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
25910
25910
 
25911
25911
  // src/envelope.ts
25912
25912
  var ENVELOPE_VERSION = 2;
25913
+ var VOICE_MESSAGE_MIME = "audio/ogg; x-ours-kind=voice-message";
25913
25914
  var REPLY_EXCERPT_MAX = 200;
25914
25915
  var KIND_DEFAULTS = {
25915
25916
  photo: { ext: "jpg", mime: "image/jpeg" },
@@ -25933,6 +25934,10 @@ function buildReply(m) {
25933
25934
  return { message_id, text: text === void 0 ? void 0 : excerpt(text) };
25934
25935
  }
25935
25936
  function attachmentMeta(d, messageId) {
25937
+ if (d.kind === "voice") {
25938
+ const id = Number.isSafeInteger(messageId) && messageId >= 0 ? String(messageId) : "unknown";
25939
+ return { filename: `voice_${id}.ogg`, mime: VOICE_MESSAGE_MIME };
25940
+ }
25936
25941
  const defaults = KIND_DEFAULTS[d.kind] ?? KIND_DEFAULTS.document;
25937
25942
  return {
25938
25943
  filename: d.file_name ?? `${d.kind}_${messageId}.${defaults.ext}`,
@@ -25945,6 +25950,9 @@ function buildAttachment(m, resolved, fileWireId) {
25945
25950
  const { filename, mime } = attachmentMeta(d, m.message_id);
25946
25951
  const base = { kind: d.kind, filename, mime, size: d.file_size };
25947
25952
  if (resolved?.ok) {
25953
+ if (!fileWireId) {
25954
+ return { ...base, size: d.file_size ?? resolved.bytes.length, error: "file transfer failed" };
25955
+ }
25948
25956
  return { ...base, size: d.file_size ?? resolved.bytes.length, transport: "send_file", wire_id: fileWireId };
25949
25957
  }
25950
25958
  if (resolved && !resolved.ok) {
@@ -25979,13 +25987,19 @@ function buildEnvelope(m, resolved, fileWireId, transcription) {
25979
25987
  }
25980
25988
 
25981
25989
  // src/stt.ts
25990
+ function baseMime(mime) {
25991
+ return mime.split(";", 1)[0].trim() || "application/octet-stream";
25992
+ }
25993
+ function redactSecret(text, secret) {
25994
+ return secret ? text.split(secret).join("[redacted]") : text;
25995
+ }
25982
25996
  async function transcribe(bytes, filename, mime, opts) {
25983
25997
  if (!opts.apiKey) return { ok: false, error: "no STT api key configured" };
25984
25998
  const aborter = new AbortController();
25985
25999
  const timer = setTimeout(() => aborter.abort(), opts.timeoutMs);
25986
26000
  try {
25987
26001
  const form = new FormData();
25988
- form.set("file", new Blob([new Uint8Array(bytes)], { type: mime }), filename);
26002
+ form.set("file", new Blob([new Uint8Array(bytes)], { type: baseMime(mime) }), filename);
25989
26003
  form.set("model", opts.model);
25990
26004
  form.set("response_format", "json");
25991
26005
  if (opts.language) form.set("language", opts.language);
@@ -25998,7 +26012,8 @@ async function transcribe(bytes, filename, mime, opts) {
25998
26012
  });
25999
26013
  if (!resp.ok) {
26000
26014
  const detail = await resp.text().catch(() => "");
26001
- return { ok: false, error: `STT HTTP ${resp.status}${detail ? `: ${detail.slice(0, 200)}` : ""}` };
26015
+ const safeDetail = redactSecret(detail, opts.apiKey).slice(0, 200);
26016
+ return { ok: false, error: `STT HTTP ${resp.status}${safeDetail ? `: ${safeDetail}` : ""}` };
26002
26017
  }
26003
26018
  const body = await resp.json();
26004
26019
  if (typeof body.text !== "string") return { ok: false, error: "STT response missing text" };
@@ -26006,7 +26021,8 @@ async function transcribe(bytes, filename, mime, opts) {
26006
26021
  } catch (err) {
26007
26022
  const name = err?.name;
26008
26023
  if (name === "AbortError") return { ok: false, error: `STT timeout after ${opts.timeoutMs}ms` };
26009
- return { ok: false, error: err instanceof Error ? err.message : String(err) };
26024
+ const detail = err instanceof Error ? err.message : String(err);
26025
+ return { ok: false, error: redactSecret(detail, opts.apiKey) };
26010
26026
  } finally {
26011
26027
  clearTimeout(timer);
26012
26028
  }
@@ -0,0 +1,63 @@
1
+ # Telegram voice-message interoperability contract
2
+
3
+ This connector bridges Telegram's semantic `message.voice` attachment to the
4
+ ours file channel. The official
5
+ [Telegram Bot API](https://core.telegram.org/bots/api#voice) defines these voice
6
+ recordings as OGG containers encoded with Opus; the connector forwards the
7
+ downloaded bytes without transcoding.
8
+
9
+ ## Wire contract
10
+
11
+ When an attachment descriptor has `kind: "voice"` and its bytes are forwarded:
12
+
13
+ - `send_file.filename` is `voice_<telegram-message-id>.ogg`;
14
+ - `send_file.mime` is exactly
15
+ `audio/ogg; x-ours-kind=voice-message`;
16
+ - the v2 message envelope's `attachment.filename` and `attachment.mime` are
17
+ byte-for-byte identical to the `send_file` values;
18
+ - `attachment.wire_id` is the `wire_id` returned by that successful
19
+ `send_file`;
20
+ - the downloaded bytes are passed unchanged.
21
+
22
+ The semantic Telegram attachment kind is the only voice classification source.
23
+ Filename or MIME guessing must not promote a normal `kind: "audio"` attachment:
24
+ `recording.ogg` with plain `audio/ogg` stays ordinary audio.
25
+
26
+ Malformed filename or MIME fields on a semantic voice descriptor are ignored.
27
+ The connector generates the safe `.ogg` filename and fixed marked MIME above.
28
+ If `send_file` fails, the envelope reports `error: "file transfer failed"` and
29
+ does not claim a `transport` or `wire_id`.
30
+
31
+ ## STT decision matrix
32
+
33
+ | Connector-side state | File forwarded? | Voice MIME marker? | Transcription block |
34
+ | --- | --- | --- | --- |
35
+ | STT disabled | yes | yes, file + envelope | absent |
36
+ | STT enabled without a key | yes | yes, file + envelope | `status: "error"` |
37
+ | STT provider failure/timeout | yes | yes, file + envelope | `status: "error"` |
38
+ | Over `sttMaxBytes` | yes | yes, file + envelope | `status: "error", error: "too_large"` |
39
+ | STT succeeds, `forwardVoiceAudio: false` | no | not applicable | `status: "ok"`; transcript folds into text |
40
+ | STT succeeds, `forwardVoiceAudio: true` | yes | yes, file + envelope | `status: "ok"` |
41
+
42
+ The ours MIME parameter is wire metadata, not a provider media type. Before an
43
+ STT upload, the connector strips parameters and sends the provider the base
44
+ `audio/ogg` MIME.
45
+
46
+ ## Receiver evidence
47
+
48
+ ours-mcp commit `c8dd602` (`packages/core/src/transcribe.ts`) recognizes the
49
+ case-insensitive MIME parameter `x-ours-kind=voice-message` and deliberately
50
+ rejects an unmarked `audio/ogg` file. Its
51
+ `packages/core/test/transcribe-detect.test.mjs` test locks both behaviors.
52
+
53
+ Independent checks in this repository:
54
+
55
+ ```sh
56
+ node --import tsx tests/envelope.test.mjs
57
+ node --import tsx tests/stt.test.mjs
58
+ node --import tsx tests/voice.test.mjs
59
+ ```
60
+
61
+ The first two are pure local tests. The voice integration test uses placeholder
62
+ OGG/Opus-signature bytes, an in-process ADAPT host, and a stubbed STT provider;
63
+ it requires no Telegram token, STT token, or real media.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ours.network/tg-connector",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Bridge Telegram bots to ours proxy nodes — one ADAPT wrapper hosting N messenger packets, each connecting one Telegram bot to one ours node. Run `ours-tg-connector add_new_connection`.",
5
5
  "type": "module",
6
6
  "license": "FSL-1.1-Apache-2.0",
@@ -19,7 +19,8 @@
19
19
  },
20
20
  "files": [
21
21
  "dist",
22
- "README.md"
22
+ "README.md",
23
+ "docs"
23
24
  ],
24
25
  "engines": {
25
26
  "node": ">=20"