|
| 1 | +import * as Sentry from '@sentry/node'; |
| 2 | +import { choice, noul, score, TypeSafeClient } from '@typesafe-ai/sdk'; |
| 3 | +import express from 'express'; |
| 4 | + |
| 5 | +function startMockServer() { |
| 6 | + const app = express(); |
| 7 | + app.use(express.json()); |
| 8 | + |
| 9 | + app.post('/v1/systemone', (req, res) => { |
| 10 | + if (req.body.model === 'error-model') { |
| 11 | + res.status(400).json({ error: 'Unknown model' }); |
| 12 | + return; |
| 13 | + } |
| 14 | + |
| 15 | + res.json({ |
| 16 | + model: 'jev-1.13.0', |
| 17 | + answers: { |
| 18 | + authIssue: { type: 'noul', noul: 0.98 }, |
| 19 | + department: { |
| 20 | + type: 'choice', |
| 21 | + choice: 'billing', |
| 22 | + confidence: 0.28, |
| 23 | + probabilities: { billing: 0.64, technical: 0.36 }, |
| 24 | + }, |
| 25 | + urgency: { |
| 26 | + type: 'score', |
| 27 | + score: 1.58, |
| 28 | + confidence: 0.9, |
| 29 | + legend: { 0: 'low', 1: 'medium', 2: 'high' }, |
| 30 | + probabilities: { 0: 0, 1: 0.42, 2: 0.58 }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + usage: { input_tokens: 275, output_tokens: 20 }, |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + return new Promise(resolve => { |
| 38 | + const server = app.listen(0, () => { |
| 39 | + resolve(server); |
| 40 | + }); |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +async function run() { |
| 45 | + const server = await startMockServer(); |
| 46 | + const client = Sentry.instrumentTypeSafeClient( |
| 47 | + new TypeSafeClient({ |
| 48 | + apiKey: 'mock-api-key', |
| 49 | + baseURL: `http://localhost:${server.address().port}`, |
| 50 | + retry: { maxRetries: 0 }, |
| 51 | + }), |
| 52 | + ); |
| 53 | + const state = 'I cannot log in, and I also want a refund for last month.'; |
| 54 | + const questions = { |
| 55 | + authIssue: noul('Is there a login problem?'), |
| 56 | + department: choice('Which team should handle this?', { |
| 57 | + billing: 'Charges and refunds', |
| 58 | + technical: 'Bugs and outages', |
| 59 | + }), |
| 60 | + urgency: score('How urgent is this ticket?', ['low', 'medium', 'high']), |
| 61 | + }; |
| 62 | + |
| 63 | + await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { |
| 64 | + await client.systemOne({ state, questions }); |
| 65 | + |
| 66 | + // The instrumentation must not read the body the caller gets from `asResponse()`. |
| 67 | + const response = await client.systemOne({ model: 'jev-1.13', state, questions }).asResponse(); |
| 68 | + await response.json(); |
| 69 | + |
| 70 | + try { |
| 71 | + await client.systemOne({ model: 'error-model', state, questions }); |
| 72 | + } catch { |
| 73 | + // expected |
| 74 | + } |
| 75 | + |
| 76 | + // The SDK rejects empty questions with a `TypeSafeError` before sending the request. |
| 77 | + try { |
| 78 | + await client.systemOne({ model: 'validation-error', state, questions: {} }); |
| 79 | + } catch { |
| 80 | + // expected |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + server.close(); |
| 85 | +} |
| 86 | + |
| 87 | +run(); |
0 commit comments