@paymanai/payman-typescript-ask-sdk
v1.2.9
Published
Core TypeScript SDK for Payman workflows - logic only, no UI
Maintainers
Readme
Payman TypeScript Ask SDK
Core TypeScript SDK for Payman workflows - Logic only, no UI components
This package contains only the business logic for interacting with Payman workflows. Use this if you want to build your own custom UI or integrate Payman into existing applications without including the default UI components.
Installation
npm install @paymanai/payman-typescript-ask-sdk
# or
yarn add @paymanai/payman-typescript-ask-sdk
# or
bun add @paymanai/payman-typescript-ask-sdkFeatures
- ✅
useChatHook - React hook for managing chat state and streaming - ✅
useVoiceHook - React hook for voice recognition (web and mobile) - ✅ Streaming Client - Low-level streaming utilities
- ✅ TypeScript Types - Full type definitions
- ✅ Cross-Platform - Works in web and React Native
- ✅ Zero UI Dependencies - No UI libraries included
Usage
Basic Example with Custom UI
import { useChat } from '@paymanai/payman-typescript-ask-sdk';
function MyCustomChat() {
const {
messages,
sendMessage,
isWaitingForResponse,
resetSession,
} = useChat({
config: {
api: {
baseUrl: 'https://api.payman.ai',
authToken: 'your-api-key',
},
workflowName: 'my-workflow',
stage: 'DEV',
sessionParams: {
id: 'user-123',
name: 'John Doe',
},
},
});
return (
<div>
{/* Your custom UI */}
{messages.map((msg) => (
<div key={msg.id}>{msg.content}</div>
))}
<button
onClick={() => sendMessage('Hello')}
disabled={isWaitingForResponse}
>
Send
</button>
</div>
);
}Direct Streaming API
import { streamWorkflowEvents } from '@paymanai/payman-typescript-ask-sdk';
await streamWorkflowEvents(
'https://api.payman.ai/api/workflows/ask/stream',
{
workflowName: 'my-workflow',
userInput: 'Hello',
sessionOwnerId: 'user-123',
sessionOwnerLabel: 'John Doe',
},
{
'x-yaak-api-key': 'your-api-key',
},
{
onEvent: (event) => {
console.log('Received event:', event);
},
onComplete: () => {
console.log('Stream completed');
},
onError: (error) => {
console.error('Stream error:', error);
},
}
);Voice Support
The SDK includes built-in voice recognition for both web and mobile platforms.
import { useVoice } from '@paymanai/payman-typescript-ask-sdk';
function MyChat() {
const {
voiceState,
transcribedText,
isAvailable,
isRecording,
startRecording,
stopRecording,
} = useVoice(
{ lang: 'en-US' },
{
onResult: (transcript) => console.log('Transcript:', transcript),
}
);
return (
<div>
<button onClick={startRecording} disabled={!isAvailable || isRecording}>
Start Voice
</button>
<button onClick={stopRecording} disabled={!isRecording}>
Stop Voice
</button>
<p>{transcribedText}</p>
</div>
);
}Platform Support:
- Web: Uses browser's Web Speech API (Chrome, Edge, Safari)
- React Native: Uses
expo-speech-recognition(iOS & Android). You must install it in your app:npm install expo-speech-recognition(oryarn add expo-speech-recognition). If the package is not installed, the voice button will show butisAvailablewill be false and no permissions are requested.
Voice UI layout (Ask UI / custom UIs): When voice is enabled, show the voice control beside the send button (e.g. voice on the left, send on the right), not replacing it. Both should be visible so users can send text or use voice.
API Reference
useChat(options)
React hook for managing chat state.
Parameters:
config: ChatConfig- Configuration objectapi.baseUrl: string- API base URLapi.authToken?: string- Authentication tokenapi.headers?: Record<string, string>- Custom headersworkflowName: string- Workflow namestage?: WorkflowStage- Environment stage (DEV, SANDBOX, PROD)sessionParams?: SessionParams- Session owner information
callbacks?: ChatCallbacks- Event callbacks
Returns:
messages: MessageDisplay[]- Array of messagessendMessage: (message: string) => Promise<void>- Send a messageresetSession: () => void- Reset the sessioncancelStream: () => void- Cancel current streamisWaitingForResponse: boolean- Loading state
useVoice(config?, callbacks?)
React hook for voice recognition.
Parameters:
config?: VoiceConfig- Voice configurationlang?: string- Language (default: "en-US")interimResults?: boolean- Enable interim results (default: true)continuous?: boolean- Continuous mode (default: true)maxAlternatives?: number- Max alternatives (default: 1)autoStopAfterSilence?: number- Auto-stop after silence in ms (web only)
callbacks?: VoiceCallbacks- Event callbacksonStart?: () => void- Recording startedonEnd?: () => void- Recording endedonResult?: (transcript: string) => void- New transcriptonError?: (error: string) => void- Error occurredonStateChange?: (state: VoiceState) => void- State changed
Returns:
voiceState: VoiceState- Current state ("idle" | "listening" | "processing" | "error")transcribedText: string- Current transcribed textisAvailable: boolean- Is voice available on this device/browserisRecording: boolean- Is currently recordingstartRecording: () => Promise<void>- Start voice recordingstopRecording: () => void- Stop voice recordingrequestPermissions: () => Promise<VoicePermissions>- Request mic permissionsgetPermissions: () => Promise<VoicePermissions>- Check mic permissionsclearTranscript: () => void- Clear transcribed textreset: () => void- Reset voice state
streamWorkflowEvents(url, body, headers, options)
Low-level streaming function.
Parameters:
url: string- API endpoint URLbody: Record<string, unknown>- Request bodyheaders: Record<string, string>- Request headersoptions: StreamOptions- Streaming optionsonEvent?: (event: StreamEvent) => void- Event callbackonComplete?: () => void- Completion callbackonError?: (error: Error) => void- Error callbacksignal?: AbortSignal- Abort signal
TypeScript Types
All types are exported:
import type {
// Chat types
ChatConfig,
ChatCallbacks,
MessageDisplay,
StreamingStep,
WorkflowStage,
// Voice types
VoiceConfig,
VoiceCallbacks,
VoiceState,
VoicePermissions,
UseVoiceReturn,
// ... and more
} from '@paymanai/payman-typescript-ask-sdk';Related Packages
- @paymanai/payman-ask-sdk - Full SDK with UI components (uses this package internally)
License
MIT
