Skip to main content

AI Assistant (TSAIAssistantInterface)

The AI Assistant provides streaming text summarization and long-running, device-microphone voice-chat sessions.

Prerequisites​

TSAIContext *context = [TSAIKit sharedInstance].activeContext;
id<TSAIAssistantInterface> assistant = context.assistant;

Query TSAIFeatureAISummary or TSAIFeatureAIChat before using the corresponding API. Voice chat also requires a connected device and a supported audio path.

Data models​

Text summary​

ModelMain propertiesDescription
TSAISummaryPartialResulttaskId, textCumulative streaming text
TSAISummaryResulttaskId, text, durationFinal summary

Voice chat​

ModelMain propertiesDescription
TSAIChatConfigaudioRouteConfiguration, languageHint, initialPrompt, agentId, speakerIdAudio route, language, role, and prompt configuration
TSAIChatConfigenableVoiceOutput, allowUserInterruptVoice output and interruption options
TSAIChatConfigsilenceBeforeReplyInterval, autoEndSessionTimeoutVAD wait and automatic session timeout
TSAIChatContentcontentType, roundIndex, text, isTextFinalQuestion or answer text and finality
TSAIChatContentaudioChunk, audioFormat, isAudioFinal, intentIncremental audio, its format, and business intent
TSAIChatIntenttype, intentId, query, value, valueDictionaryStructured device-control or business intent
TSAIChatEventeventType, timestampVAD, playback, network, and session events
TSAIChatReportstartTime, endTime, roundCount, endReasonFinal session report

Create a configuration with [TSAIChatConfig defaultConfig].

Callback types​

CallbackDescription
TSAISummaryPartialBlockZero or more cumulative summary updates
TSAISummaryCompletionBlockInvoked once on success, failure, or cancellation
TSAIChatContentBlockHigh-frequency text, audio, or intent content
TSAIChatEventBlockLow-frequency session events
TSAIChatCompletionBlockFinal report or error
TSAIChatStateBlockApp-facing chat state

Text summary​

- (NSString *)summarizeText:(NSString *)text
onPartialResult:(nullable TSAISummaryPartialBlock)onPartialResult
completion:(nullable TSAISummaryCompletionBlock)completion;
- (void)cancelSummarizeWithTaskId:(NSString *)taskId;
NSString *taskId = [assistant summarizeText:sourceText
onPartialResult:^(TSAISummaryPartialResult *partial) {
TSLog(@"Summary: %@", partial.text);
}
completion:^(TSAISummaryResult *result, NSError *error) {
if (error) {
TSLog(@"Summary failed: %@", error.localizedDescription);
return;
}
TSLog(@"Final summary: %@", result.text);
}];

// [assistant cancelSummarizeWithTaskId:taskId];

The synchronously returned client task ID is echoed by every partial and final result. Text that exceeds the provider context window fails validation and is not silently truncated.

Voice chat​

- (NSString *)startChatWithConfig:(TSAIChatConfig *)config
onContent:(nullable TSAIChatContentBlock)onContent
onEvent:(nullable TSAIChatEventBlock)onEvent
completion:(nullable TSAIChatCompletionBlock)completion;
- (void)stopChatWithTaskId:(NSString *)taskId;
TSAIChatConfig *config = [TSAIChatConfig defaultConfig];
config.languageHint = @"en-US";
config.enableVoiceOutput = YES;
config.audioRouteConfiguration = [TSAIAudioRouteConfiguration defaultConfiguration];

NSString *chatTaskId = [assistant startChatWithConfig:config
onContent:^(TSAIChatContent *content) {
if (content.text.length > 0) {
TSLog(@"Chat content: %@", content.text);
}
}
onEvent:^(TSAIChatEvent *event) {
TSLog(@"Chat event: %ld", (long)event.eventType);
}
completion:^(TSAIChatReport *report, NSError *error) {
if (error) {
TSLog(@"Chat ended with error: %@", error.localizedDescription);
return;
}
TSLog(@"Chat rounds: %ld", (long)report.roundCount);
}];

// Gracefully flush and stop.
// [assistant stopChatWithTaskId:chatTaskId];

The SDK manages device activation, microphone audio, and the ASR → LLM → TTS pipeline. VAD segments multiple rounds. Stop the session explicitly or configure autoEndSessionTimeout.

Device events and state​

- (void)registerOnAIChatDeviceEvent:(nullable void (^)(TSAIChatDeviceEvent event))deviceEventBlock;
- (void)registerAIChatStateDidChanged:(nullable TSAIChatStateBlock)stateBlock;
[assistant registerOnAIChatDeviceEvent:^(TSAIChatDeviceEvent event) {
if (event == TSAIChatDeviceEventRequestStart) {
TSLog(@"The device requested AI chat");
}
}];

A device event is a request signal; it does not change SDK state automatically. On RequestStart, call startChatWithConfig: to prepare but do not present UI yet. Present it only after onEvent receives TSAIChatEventTypeSessionStarted, confirming device synchronization. Handle RequestEnd with stopChatWithTaskId:.

Key enums​

TSAIChatContentType​

ValueRaw valueMeaning
TSAIChatContentTypeUnknown-1Unknown content
TSAIChatContentTypeQuestion1User question text
TSAIChatContentTypeAnswer2AI answer text
TSAIChatContentTypeAudioChunk3Incremental AI answer audio
TSAIChatContentTypeIntent4Device-control or business intent

TSAIChatEventType​

ValueRaw valueMeaning
TSAIChatEventTypeUnknown-1Unknown event
TSAIChatEventTypeSessionStarted1Session started
TSAIChatEventTypeUserSpeechStarted2User speech started
TSAIChatEventTypeUserSpeechEnded3User speech ended
TSAIChatEventTypeAIPlaybackStarted4AI playback started
TSAIChatEventTypeAIPlaybackEnded5AI playback ended
TSAIChatEventTypeAIPlaybackInterrupted6AI playback was interrupted
TSAIChatEventTypeNetworkError7Network error
TSAIChatEventTypeBleDisconnected8BLE device disconnected
TSAIChatEventTypeAutoEnding9Session is about to end automatically

Device events, session states, and end reasons​

EnumValues and raw values
TSAIChatDeviceEventRequestStart = 0, RequestEnd = 1, Interrupted = 2
TSAIChatStateDeviceClosed = 0, DeviceOpened = 1, Started = 2, Ended = 3, Interrupted = 255
TSAIChatEndReasonUnknown = -1, UserStop = 1, Timeout = 2, Cancelled = 3, Error = 4

Add the corresponding enum prefix to shortened values in this table, for example TSAIChatStateStarted.

Important notes​

  1. Completion is the authoritative terminal signal.
  2. stopChatWithTaskId: is graceful; an unknown or completed task ID is a no-op.
  3. Unregister device-event and state callbacks when the owning screen is destroyed.
  4. Avoid blocking I/O in high-frequency audio callbacks.