@callimacus/thamyr-react-native

中文文档
4.23.5发布于 2026/11/09SEE LICENSE IN LICENSE.mdTypeScriptES Module近 30 天下载 5569

React Native package for Thamyr

原始使用文档
文档目录7 节
  1. Getting started
  2. What differs from the web SDK
  3. Voice recording returns bytes
  4. Hooks that are not re-exported
  5. Intent tracking
  6. Transports, and why voice does not work yet
  7. Things worth knowing

@callimacus/thamyr-react-native

The Thamyr SDK for React Native (Expo and bare). It wraps @callimacus/thamyr-react, so the hooks, store, and response types are the same ones the web SDK exposes — this package narrows them to what works on native and adds a native voice recorder.

npm install @callimacus/thamyr-react-native

Peer dependencies: react >=18, react-native >=0.72.

Getting started

import {useInitThamyr, useThamyr, useConnection} from '@callimacus/thamyr-react-native';

function App() {
  useInitThamyr({clientId: 'cal-pk-…'});
  return <Chat />;
}

function Chat() {
  const {history, sendEvent} = useThamyr();
  const {status} = useConnection();
  // …
}

What differs from the web SDK

Everything not listed here behaves identically, because it is literally the same code.

Voice recording returns bytes

The web recorder hands you a Blob; this one hands you a Uint8Array. Audio travels as inputEvent.data.file over socket.io, and only typed arrays survive that trip on native: a React Native Blob is a handle into the native BlobModule whose bytes never exist in JS, so socket.io cannot read it, and new File([...]) — what the web path uses — has no useful native equivalent. The server drops the filename and mime type either way (socket.io's binary framing strips them), so nothing is lost.

The recorder needs an adapter, which you build from expo-audio:

import {useMemo} from 'react';
import * as ExpoAudio from 'expo-audio';
import {
  createExpoAudioAdapter,
  useVoiceRecorder,
  useThamyr,
  SLInputEventType,
  UserInteractionType,
} from '@callimacus/thamyr-react-native';

function VoiceButton() {
  const {sendEvent} = useThamyr();
  const adapter = useMemo(() => createExpoAudioAdapter(ExpoAudio), []);

  const {state, audioLevels, startRecording, stopRecording, error} = useVoiceRecorder({
    adapter,
    onRecordingComplete(audio) {
      sendEvent(UserInteractionType.CREATE_ROUND, {
        inputEvent: {type: SLInputEventType.question, data: {value: '', file: audio}},
      });
    },
  });
  // …
}

The adapter is passed in rather than defaulted on purpose. If this package imported expo-audio itself, Metro would resolve it statically for every consumer, so an app that never records audio would fail to bundle merely for not having installed it. Handing the module in also keeps expo-audio out of this package's dependency graph entirely, so it typechecks and tests without a native toolchain. For a recorder other than expo-audio, implement VoiceRecorderAdapter yourself — start/stop/cancel over any native module.

Recording auto-stops after silenceDuration (default 1800 ms) below silenceThreshold (default −45 dBFS), and hard-stops at maxDuration (default 30 s).

Hooks that are not re-exported

Not availableWhyInstead
useTtsAudioConstructs an HTMLAudioElement (new Audio(url))Play the URL from extractTtsAudio(block.data) with an expo-audio player
uiObserver configSnapshots mean querySelectorAll + getBoundingClientRectNot yet available on native (SOL-1084)
setConsent, setGeo, initIntent, …@callimacus/intent's browser API@callimacus/intent/native — see below

extractTtsAudio and the TtsAudio type are exported, so you can get the audio URL out of a response and play it however you like; only the web playback hook is missing.

Intent tracking

The intent web build cannot be imported on native at all: it reads localStorage and document at module scope, which throws under Hermes before your app renders. On React Native the SDK therefore resolves @callimacus/thamyr-core's react-native entry, where intent is absent — its functions are present as warn-once no-ops so shared code does not crash, but they do nothing.

Intent has its own native entry with a different shape. Wire it up directly:

import * as RN from 'react-native';
import {MMKV} from 'react-native-mmkv';
import {createNativeAdapters, initIntentNative, mmkvStorage} from '@callimacus/intent/native';

initIntentNative({
  clientId: 'cal-pk-…',
  consent: true,
  adapters: createNativeAdapters(RN, {storage: mmkvStorage(new MMKV())}),
});

Transports, and why voice does not work yet

Text chat works. Sending audio does not, and the cause is neither this package nor your app — it is a TLS mismatch that forces the connection onto a transport which cannot carry binary on React Native. The detail matters because the symptom is misleading.

React Native cannot open a WebSocket to api.callimacus.ai. The host is TLS 1.3-only — it rejects a TLS 1.2 handshake outright with a protocol_version alert (verified with openssl s_client -no_tls1_3). React Native's WebSocket fails against it with close code 1006 and OSStatus -9836, which is Apple's errSSLPeerProtocolVersion ("bad protocol version"). React Native's HTTP stack is fine with the same host, since that goes through NSURLSession, which does negotiate TLS 1.3 — consistent with the WebSocket path using the older SecureTransport route that does not.

The SDK therefore falls back to HTTP long-polling, and text works over it.

But binary cannot traverse polling on React Native. engine.io's polling payload is text, so encodePayload encodes every packet with supportsBinary: false ("force base64 encoding for binary packets"), and in engine.io-parser's browser build that path does encodeBlobAsBase64(new Blob([data])) — while React Native refuses to construct a Blob from an ArrayBuffer at all ("Creating blobs from 'ArrayBuffer' and 'ArrayBufferView' are not supported"). So the audio bytes are dropped with a console error. forceBase64 cannot help: polling already forces it, and that is precisely the branch that builds the Blob.

The fix is to let React Native use the WebSocket transport, by enabling TLS 1.2 on the API host (nginx: keep TLSv1.3 and add TLSv1.2 with a matching cipher suite). Over WebSocket, encodePacket runs with supportsBinary: true and hands the typed array straight to WebSocket.send() — no Blob, no base64 — so audio works with no further SDK change, and text gets lower latency as a bonus.

Until then, recording works and onRecordingComplete gives you correct bytes; only the send fails.

Things worth knowing

  • UUIDs. Hermes ships no global crypto. Core falls back to crypto.getRandomValues and then Math.random(), so nothing throws, but installing react-native-get-random-values at the top of your entry file gets you cryptographically random interaction ids.
  • gatherClientInfo() returns empty strings for userAgent, language, and screenResolution on native — there is no navigator.userAgent or window.screen. Read screen size from Dimensions if you need it.
  • Microphone permissions. Add NSMicrophoneUsageDescription (iOS) and RECORD_AUDIO (Android). Under Expo, expo-audio's config plugin does this.
  • Socket auth goes through socket.io's auth payload, not an Authorization header — React Native's WebSocket transport ignores extraHeaders, and the server reads handshake.auth.token. No action needed; it is noted because the header being dropped looks alarming in logs.

版本与发布记录

@callimacus/thamyr-react-native 共发布 39 个版本,首次发布于 2026/09/29;过去 12 个月发布了 39 个版本。

版本发布日期距今
4.23.5latest2026/11/09今天
4.23.42026/11/072 天前
4.23.32026/11/062 天前
4.23.22026/11/063 天前
4.23.12026/11/063 天前
4.23.02026/11/053 天前

关于 @callimacus/thamyr-react-native 的常见问题

@callimacus/thamyr-react-native 是什么?

React Native package for Thamyr

如何安装 @callimacus/thamyr-react-native?

在项目目录运行 npm install @callimacus/thamyr-react-native,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=18)、react-native(>=0.72)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@callimacus/thamyr-react-native 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@callimacus/thamyr-react-native 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@callimacus/thamyr-react-native 可以商用吗?

@callimacus/thamyr-react-native 使用 SEE LICENSE IN LICENSE.md 许可。许可条款写在包内的独立文件中,请阅读该文件确认可否商用。以上为概括,不构成法律意见,请以许可原文为准。

@callimacus/thamyr-react-native 的下载量有多少?

根据 npm 官方下载统计,@callimacus/thamyr-react-native 在 2026/10/09 – 2026/11/07 这 30 天内被下载 5,569 次,日均约 186 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@callimacus/thamyr-react-native 还在维护吗?

最新版本 4.23.5 发布于 2026/11/09(今天);至今共发布 39 个版本,其中过去 12 个月发布了 39 个;首次发布于 2026/09/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。