|
| 1 | +# @qwant/answer |
| 2 | + |
| 3 | +TypeScript SDK for the Qwant Answers API (`POST /v2/answer`). |
| 4 | + |
| 5 | +Handles fetch, SSE parsing, partial-chunk buffering, event routing, and stream cancellation. |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +- Node.js ≥ 18 |
| 10 | +- No runtime dependencies |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +```bash |
| 15 | +npm install @qwant/answer |
| 16 | +``` |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +```ts |
| 21 | +import { AnswerClient } from '@qwant/answer'; |
| 22 | + |
| 23 | +const client = new AnswerClient({ apiKey: process.env.API_KEY }); |
| 24 | +``` |
| 25 | + |
| 26 | +### Non-streaming |
| 27 | + |
| 28 | +```ts |
| 29 | +const result = await client.create({ |
| 30 | + query: 'Macbook néo', |
| 31 | + filter: 'frandroid.com', |
| 32 | + markdown: true, |
| 33 | + related_queries: true, |
| 34 | +}); |
| 35 | + |
| 36 | +console.log(result.answer); |
| 37 | +console.log(result.sources); |
| 38 | +``` |
| 39 | + |
| 40 | +### Streaming |
| 41 | + |
| 42 | +```ts |
| 43 | +const stream = client.stream({ |
| 44 | + query: 'MacBook Neo', |
| 45 | + filter: 'frandroid.com', |
| 46 | + markdown: true, |
| 47 | + style: 'editorial', |
| 48 | + language: 'fr', |
| 49 | +}); |
| 50 | + |
| 51 | +// Synchronous callback (useful for partial rendering) |
| 52 | +stream.onEvent((event) => { |
| 53 | + if (event.type === 'assistant') process.stdout.write(event.delta); |
| 54 | +}); |
| 55 | + |
| 56 | +// Async iterator (main path) |
| 57 | +for await (const event of stream) { |
| 58 | + if (event.type === 'sources') console.log('Sources:', event.sources); |
| 59 | + if (event.type === 'done') console.log('Done:', event.finish_reason); |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +`onEvent` and `for await` are two independent views of the same stream. |
| 64 | + |
| 65 | +### Cancellation |
| 66 | + |
| 67 | +```ts |
| 68 | +const stream = client.stream({ query: '...' }); |
| 69 | + |
| 70 | +// Via the method |
| 71 | +setTimeout(() => stream.cancel(), 2000); |
| 72 | + |
| 73 | +// Via AbortSignal |
| 74 | +const ac = new AbortController(); |
| 75 | +const stream = client.stream({ query: '...' }, { signal: ac.signal }); |
| 76 | + |
| 77 | +// Exiting the for-await loop automatically cancels the HTTP request |
| 78 | +``` |
| 79 | + |
| 80 | +## API |
| 81 | + |
| 82 | +### `new AnswerClient(opts)` |
| 83 | + |
| 84 | +| Option | Type | Default | |
| 85 | +|--------|------|---------| |
| 86 | +| `apiKey` | `string` | — | |
| 87 | +| `baseURL` | `string` | `'https://api.staan.ai/v2'` | |
| 88 | + |
| 89 | +### `client.create(input, signal?): Promise<AnswerV2Result>` |
| 90 | + |
| 91 | +Returns the full response (non-streaming). |
| 92 | + |
| 93 | +### `client.stream(input, opts?): StreamHandle` |
| 94 | + |
| 95 | +Returns a `StreamHandle`: |
| 96 | + |
| 97 | +| Member | Description | |
| 98 | +|--------|-------------| |
| 99 | +| `for await (const event of stream)` | Async iterator | |
| 100 | +| `stream.onEvent(handler)` | Synchronous callback, returns an unsubscribe function | |
| 101 | +| `stream.cancel()` | Cancels the HTTP request | |
| 102 | + |
| 103 | +## Stream events |
| 104 | + |
| 105 | +| Type | Payload | |
| 106 | +|------|---------| |
| 107 | +| `sources` | `{ sources: AnswerV2Source[] }` | |
| 108 | +| `assistant` | `{ delta: string }` | |
| 109 | +| `citation` | `{ reference_ids: number[] }` | |
| 110 | +| `usages` | `{ usages: AnswerV2UsageEntry[] }` | |
| 111 | +| `related` | `{ related_queries: string[] }` | |
| 112 | +| `done` | `{ finish_reason: string }` | |
| 113 | + |
| 114 | +## Error handling |
| 115 | + |
| 116 | +```ts |
| 117 | +import { AnswerApiError, AnswerNetworkError } from '@qwant/answer'; |
| 118 | + |
| 119 | +try { |
| 120 | + await client.create({ query: '...' }); |
| 121 | +} catch (err) { |
| 122 | + if (err instanceof AnswerApiError) console.error(err.status, err.body); |
| 123 | + if (err instanceof AnswerNetworkError) console.error(err.message, err.cause); |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +## Development |
| 128 | + |
| 129 | +```bash |
| 130 | +npm install |
| 131 | +npm run build |
| 132 | +npm run typecheck |
| 133 | +``` |
| 134 | + |
| 135 | +```bash |
| 136 | +API_KEY=xxx npm run example:create |
| 137 | +API_KEY=xxx npm run example:stream |
| 138 | +API_KEY=xxx npm run example:cancel |
| 139 | +``` |
| 140 | + |
| 141 | +## License |
| 142 | + |
| 143 | +MIT |
0 commit comments