dwk-sse-client
v2.1.12
Published
A client library for DWK SSE API
Readme
DWK SSE Client
A JavaScript client for handling SSE streaming responses with support for JWT authentication and stream control.
Features
- Handle DWKSSE API streaming responses
- Support JWT authentication
- Support DWK token authentication
- Abort streaming requests
- Error handling with detailed error information
- Works in both browser and Node.js environments
Installation
npm install dwk-sse-clientOr include directly in browser:
<script src="https://unpkg.com/dwk-sse-client/dist/index.js"></script>Usage
Browser Example
<script>
const deepseek = new DWKSSEClient({
baseUrl: 'https://your-api-url.com',
jwt: 'your.jwt.token.here',
dwk_token: 'your.dwk.token.here'
});
// 设置错误处理
deepseek.onError = (error) => {
console.error('Error:', error.message);
if (error.code) {
console.error('Error code:', error.code);
}
if (error.innerExceptions) {
console.error('Inner exceptions:', error.innerExceptions);
}
};
// Start streaming
const stream = deepseek.stream('Your prompt here');
// Handle responses
for await (const chunk of stream) {
if (chunk.code === '200') {
console.log(chunk.data);
} else {
console.error(chunk.message);
}
}
// To abort
deepseek.abort();
</script>Node.js Example
const { DWKSSEClient } = require('deepseek-stream');
const deepseek = new DWKSSEClient({
baseUrl: 'https://your-api-url.com',
jwt: 'your.jwt.token.here',
dwk_token: 'your.dwk.token.here'
});
// 设置错误处理
deepseek.onError = (error) => {
console.error('Error:', error.message);
if (error.code) {
console.error('Error code:', error.code);
}
if (error.innerExceptions) {
console.error('Inner exceptions:', error.innerExceptions);
}
};
async function run() {
const stream = deepseek.stream('Your prompt here');
for await (const chunk of stream) {
if (chunk.code === '200') {
process.stdout.write(chunk.data);
} else {
console.error(chunk.message);
break;
}
}
}
run();API
new DWKSSEClient(options)
Constructor options:
baseUrl: Base API URL (required)jwt: JWT token for authentication (optional)dwk_token: DWK token for authentication (optional)systemPrompt: System prompt for API (optional, default: 'You are a helpful assistant')
Methods
stream(prompt: string): AsyncGenerator<DWKSSEResponse>
Starts a streaming request with the given prompt.
abort(): void
Aborts the current streaming request.
Error Handling
The client provides detailed error information through the onError callback:
client.onError = (error) => {
console.error('Error:', error.message);
if (error.code) {
console.error('Error code:', error.code);
}
if (error.innerExceptions) {
console.error('Inner exceptions:', error.innerExceptions);
}
};The error object contains:
message: The error messagecode: The error code from the serverinnerExceptions: Array of inner business exceptions (if any)
Development
# Install dependencies
npm install
# Build
npm run build
# Watch mode
npm run dev
# Run tests
npm testLicense
MIT
