Skip to main content

AI Asking

Send a text question to the selected AI provider, correlate callbacks with the returned question identifier, and render either accumulated or incremental answer text.

Animated workflow

AI asking request lifecycle

A request may receive its identifier synchronously or through a callback, followed by streamed answer updates and one terminal callback.

Host app

Validate Question

Trim the input and reject an empty prompt before creating a request.

Host app

Configure Agent

Optionally select the provider-specific agent identifier.

AI service

Send Question

Call AIBudsAISDK.send and retain any synchronously returned identifier.

Start callback

Start Answering

Capture the question identifier when the provider starts answering.

AI service → app

Stream Answer

Prefer fullText when present; otherwise append the deltaText update.

delta or full text
Authoritative result

Finish or Fail

Re-enable UI and close request state from the terminal callback.

Treat onFinishAnswering or onError as terminal; isFinal only marks the final answer update.

Prerequisites

  • Initialize AIBudsAISDK, select a registered provider, and complete authentication when required.
  • Confirm that the selected provider implements AIAskingServiceAPI.
  • Dispatch callback-driven UI work to the main queue.

Implement with AI Assistance

Build with AI

Implement this workflow with AI

Use the official Implement AIBuds AI Asking skill to adapt this workflow to your app.

Read and follow https://docs-aibuds.github.io/skills/implement-aibuds-ai-asking. Use it to implement Implement AIBuds AI Asking in this iOS project and verify the result.
View official skill

API Reference

Framework

AIBudsAI.xcframework

Import

Swift
import AIBudsAI
import AIBudsAIFoundation

Declaration

Swift
/// Sends a text question to the currently selected AI service and receives
/// the answer as a stream of updates.
///
/// - Parameters:
///   - question: The question or prompt to send.
///   - config: Configuration for the request. Defaults to `.default`.
///   - onStartAnswering: Called when the service starts answering. The
///     question identifier may be `nil` if it has not yet been assigned.
///   - onAnswer: Called for each answer update.
///     - questionId: The identifier of the question being answered.
///     - deltaText: Newly generated text in this update, if available.
///     - fullText: The accumulated answer text, if available.
///     - isFinal: `true` when this is the final answer update.
///   - onFinishAnswering: Called when answering finishes successfully.
///   - onError: Called when validation fails or the provider reports an error.
/// - Returns: The question identifier when the request is created; otherwise `nil`.
public static func send(question: String,
                          config: AIAskingConfig = .default,
                onStartAnswering: ((_ questionId: String?) -> Void)? = nil,
                        onAnswer: ((
                            _ questionId: String,
                            _ deltaText: String?,
                            _ fullText: String?,
                            _ isFinal: Bool
                        ) -> Void)? = nil,
               onFinishAnswering: ((_ questionId: String) -> Void)? = nil,
                         onError: ((_ questionId: String, _ error: Error) -> Void)? = nil) -> String?

See send and AIAskingConfig.

Configuration

AIAskingConfig.specifiedAgent is optional and provider-specific. Leave it nil to use the provider's normal agent selection. Do not assume an agent identifier from the Demo is valid for another account or provider.

Usage Examples

The examples follow AIAskingDemoController: they accept either a full accumulated answer or a delta, preserve the latest non-empty question identifier, and separate the final answer update from request completion.

Swift
let question = input.trimmingCharacters(in: .whitespacesAndNewlines)
guard !question.isEmpty else { return }

let config = AIAskingConfig()
config.specifiedAgent = selectedAgentID

var activeQuestionID: String?
var answer = ""

let returnedID = AIBudsAISDK.send(
    question: question,
    config: config,
    onStartAnswering: { questionID in
        DispatchQueue.main.async {
            activeQuestionID = questionID ?? activeQuestionID
        }
    },
    onAnswer: { questionID, deltaText, fullText, isFinal in
        DispatchQueue.main.async {
            activeQuestionID = questionID
            if let fullText {
                answer = fullText
            } else if let deltaText {
                answer += deltaText
            }
            render(answer: answer, isFinalUpdate: isFinal)
        }
    },
    onFinishAnswering: { questionID in
        DispatchQueue.main.async {
            activeQuestionID = questionID
            setAsking(false)
        }
    },
    onError: { questionID, error in
        DispatchQueue.main.async {
            if !questionID.isEmpty { activeQuestionID = questionID }
            setAsking(false)
            show(error)
        }
    }
)

activeQuestionID = returnedID ?? activeQuestionID

Error Handling

send can return nil and invoke onError synchronously when validation fails before a request is created. In that case the error callback's question identifier is empty. Disable duplicate submission while a request is active and restore UI state from both terminal callbacks.

Notes

  • isFinal marks the last streamed answer update; use onFinishAnswering as successful request completion.
  • A provider can supply fullText, deltaText, or both. Prefer fullText to avoid duplicating accumulated text.
  • The public API does not currently expose a cancellation method for an active asking request.