|
| 1 | +# OTAIP Integration Guide |
| 2 | + |
| 3 | +How to run a contracted OTAIP agent against a **live distribution supplier** through |
| 4 | +the six pipeline gates, and how to read the run's **execution + gate trace** back by id. |
| 5 | + |
| 6 | +This guide covers exactly one proven path, end to end: |
| 7 | + |
| 8 | +> **Agent 1.1 — Availability Search → Duffel NDC sandbox → real offers → durable trace** |
| 9 | +
|
| 10 | +It is implemented in a single caller-facing package, [`@otaip/integration`](packages/integration), |
| 11 | +which wires the **real** components together with **no parallel execution or validation path**: |
| 12 | + |
| 13 | +``` |
| 14 | +runAvailabilitySearch() ← packages/integration |
| 15 | + └─ AvailabilitySearch (Agent 1.1) ← @otaip/agents-search (unchanged) |
| 16 | + └─ DuffelAdapter (DistributionAdapter) ← @otaip/adapter-duffel (live Duffel REST) |
| 17 | + └─ PipelineOrchestrator.runAgent() ← @otaip/core (the six gates) |
| 18 | + └─ availabilitySearchContract ← @otaip/agents-search (Zod + semantic gate) |
| 19 | + └─ ReferenceAgentDataProvider ← @otaip/agents-reference (OurAirports data) |
| 20 | + └─ agent.executed + adapter.health events → EventStore (in-memory or FileEventStore) |
| 21 | +``` |
| 22 | + |
| 23 | +The gates, event types, and unified output model are the existing ones in `@otaip/core`. |
| 24 | +The integration package only adds the glue function, a durable file-backed `EventStore`, |
| 25 | +and a by-id trace reader. |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +## 1. Adapter execution — run Agent 1.1 via Duffel |
| 30 | + |
| 31 | +**Mechanism:** in-process (package import). There is no HTTP server in this path. |
| 32 | + |
| 33 | +```ts |
| 34 | +import { runAvailabilitySearch, getRunTrace } from '@otaip/integration'; |
| 35 | + |
| 36 | +const result = await runAvailabilitySearch( |
| 37 | + { |
| 38 | + origin: 'JFK', // IATA, 3 letters |
| 39 | + destination: 'LHR', // IATA, 3 letters |
| 40 | + departure_date: '2026-07-23', // ISO YYYY-MM-DD, must be in the future |
| 41 | + passengers: [{ type: 'ADT', count: 1 }], |
| 42 | + cabin_class: 'economy', // economy | premium_economy | business | first |
| 43 | + currency: 'GBP', // optional ISO 4217 (see sandbox note below) |
| 44 | + max_results: 3, |
| 45 | + sort_by: 'price', |
| 46 | + }, |
| 47 | + { |
| 48 | + duffelApiKey: process.env.DUFFEL_API_KEY, // see "Credentials" below |
| 49 | + // eventStore, reference, adapter, now are all optional — sensible defaults |
| 50 | + }, |
| 51 | +); |
| 52 | + |
| 53 | +if (result.ok) { |
| 54 | + console.log(result.output.offers.length, 'offers'); // unified SearchOffer[] |
| 55 | +} else { |
| 56 | + console.error(result.failure.reason, result.failure.issues); // gate rejection |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +### Input shape — `AvailabilitySearchInput` |
| 61 | + |
| 62 | +The input is Agent 1.1's own contract input (`@otaip/agents-search`), validated by the |
| 63 | +`schema_in` Zod gate and the `semantic_in` gate before the adapter is ever called: |
| 64 | + |
| 65 | +| Field | Type | Required | Notes | |
| 66 | +|------------------|----------------------------------------|----------|-------| |
| 67 | +| `origin` | `string` (len 3) | yes | resolved against reference data (semantic gate) | |
| 68 | +| `destination` | `string` (len 3) | yes | must differ from `origin` | |
| 69 | +| `departure_date` | `string` `YYYY-MM-DD` | yes | must not be in the past (semantic gate) | |
| 70 | +| `return_date` | `string` `YYYY-MM-DD` | no | if set, must be ≥ `departure_date` | |
| 71 | +| `passengers` | `{ type: 'ADT'\|'CHD'\|'INF'\|…; count: number }[]` | yes | ≥ 1 | |
| 72 | +| `cabin_class` | `'economy'\|'premium_economy'\|'business'\|'first'` | no | | |
| 73 | +| `currency` | `string` (len 3) | no | ISO 4217 | |
| 74 | +| `max_connections`, `direct_only`, `max_results`, `sort_by`, `sort_order`, `sources` | various | no | see [types.ts](packages/agents/search/src/availability-search/types.ts) | |
| 75 | + |
| 76 | +### Credentials-injection contract |
| 77 | + |
| 78 | +The Duffel API key is read **once, at adapter construction**, from either: |
| 79 | + |
| 80 | +1. **`options.duffelApiKey`** passed to `runAvailabilitySearch`, **or** |
| 81 | +2. **`process.env.DUFFEL_API_KEY`** if (1) is omitted (the `DuffelAdapter` default). |
| 82 | + |
| 83 | +A `duffel_test_…` key targets the **sandbox**; a `duffel_live_…` key targets **production**. |
| 84 | +The environment is determined by the key prefix, not by code. |
| 85 | + |
| 86 | +**The key is server-side only.** It is never written to an event, a trace, a log line, or |
| 87 | +the returned payload. The trace contains only `OtaipEvent` objects (gate booleans, timing, |
| 88 | +adapter id, status) — there is no field that can carry a credential or PII. The offline test |
| 89 | +[`run-search.test.ts`](packages/integration/src/__tests__/run-search.test.ts) asserts the |
| 90 | +on-disk trace file never matches `/duffel_test_|duffel_live_|Bearer/`. |
| 91 | + |
| 92 | +To target a different supplier or a recorded-fixture server, inject a pre-built adapter: |
| 93 | +`runAvailabilitySearch(input, { adapter: myAdapter })` (then `duffelApiKey` is ignored). |
| 94 | + |
| 95 | +### Output shape — `RunSearchResult` |
| 96 | + |
| 97 | +```ts |
| 98 | +interface RunSearchResult { |
| 99 | + sessionId: string; // use this to read the trace back |
| 100 | + ok: boolean; // true ⇔ every gate passed |
| 101 | + output?: AvailabilitySearchOutput; // present iff ok (the unified model) |
| 102 | + failure?: { reason: string; failureClass: 'infra'|'execution'|'validation'; issues: SemanticIssue[] }; |
| 103 | + trace: RunTrace; // also durably in eventStore |
| 104 | + eventStore: EventStore; // the store this run wrote to |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +`output` is the existing unified output model — `AvailabilitySearchOutput` with |
| 109 | +`offers: SearchOffer[]` (`offer_id`, `source`, `itinerary`, `price`, …), `total_raw_offers`, |
| 110 | +`source_status[]`, `truncated`. The Duffel adapter normalizes the raw NDC response into this |
| 111 | +model; nothing here forks the shape. |
| 112 | + |
| 113 | +`failureClass` distinguishes an infrastructure/setup problem (`infra`) from a real agent |
| 114 | +execution error (`execution`) from a genuine contract-gate rejection (`validation`) — so a |
| 115 | +caller never mistakes a wiring bug for a data/model rejection. |
| 116 | + |
| 117 | +> **Runtime note:** the default `ReferenceAgentDataProvider` loads airport data from |
| 118 | +> `${process.cwd()}/data/reference/airports.json`. Run `pnpm run data:download` once, and |
| 119 | +> invoke from the **repo root** (or pass your own `reference` provider). If the data is |
| 120 | +> missing, the run fails at the semantic gate, not silently. |
| 121 | +
|
| 122 | +--- |
| 123 | + |
| 124 | +## 2. Trace read — fetch a run's trace by id |
| 125 | + |
| 126 | +**Mechanism:** in-process API over the same `EventStore` the run wrote to. |
| 127 | + |
| 128 | +```ts |
| 129 | +import { getRunTrace, FileEventStore } from '@otaip/integration'; |
| 130 | + |
| 131 | +// Durable: a DIFFERENT process can re-open the same file and read the trace by id. |
| 132 | +const store = await FileEventStore.open('./traces/run.jsonl'); |
| 133 | +const result = await runAvailabilitySearch(input, { eventStore: store }); |
| 134 | + |
| 135 | +// …later, anywhere with access to the file: |
| 136 | +const reopened = await FileEventStore.open('./traces/run.jsonl'); |
| 137 | +const trace = await getRunTrace(reopened, result.sessionId); |
| 138 | +``` |
| 139 | + |
| 140 | +`getRunTrace(store, sessionId)` is a pure projection over the event store (no re-execution). |
| 141 | +Auth for the trace is **the same as filesystem access to the JSONL file** (or to whichever |
| 142 | +`EventStore` backend you supply) — there is no separate auth surface in this in-process path. |
| 143 | + |
| 144 | +### Trace response shape — `RunTrace` |
| 145 | + |
| 146 | +```ts |
| 147 | +interface RunTrace { |
| 148 | + sessionId: string; |
| 149 | + outcome: 'ok' | 'rejected' | 'empty'; |
| 150 | + agentExecutions: { |
| 151 | + agentId: string; success: boolean; confidence: number; durationMs: number; |
| 152 | + timestamp: string; gateResults: { gate: string; passed: boolean }[]; |
| 153 | + }[]; |
| 154 | + adapterHealth: { adapterId: string; status: 'healthy'|'degraded'|'unhealthy'; latencyMs?: number; timestamp: string }[]; |
| 155 | + events: OtaipEvent[]; // raw, chronological — the durable source of truth |
| 156 | +} |
| 157 | +``` |
| 158 | + |
| 159 | +The underlying events are the existing core types: `agent.executed` (gate results + timing, |
| 160 | +emitted exactly as the `agentToTool` bridge does) and `adapter.health` (derived from the real |
| 161 | +search's per-source status). `EventStore.query({ sessionId })` / `.aggregate()` are the |
| 162 | +existing core APIs; `FileEventStore` reuses core's `InMemoryEventStore` for them and only adds |
| 163 | +durable JSONL persistence. |
| 164 | + |
| 165 | +--- |
| 166 | + |
| 167 | +## 3. Package versions & environment |
| 168 | + |
| 169 | +All packages are workspace version **`0.7.2`** (Node **≥ 24.14.1**, pnpm 10): |
| 170 | + |
| 171 | +| Package | Role | |
| 172 | +|--------------------------|------| |
| 173 | +| `@otaip/integration@0.7.2` | the entry point in this guide (new) | |
| 174 | +| `@otaip/core@0.7.2` | gates, event types, unified model | |
| 175 | +| `@otaip/adapter-duffel@0.7.2` | live Duffel REST adapter | |
| 176 | +| `@otaip/agents-search@0.7.2` | Agent 1.1 + contract | |
| 177 | +| `@otaip/agents-reference@0.7.2` | airport/airline reference provider | |
| 178 | + |
| 179 | +**Environment variables a caller must set:** |
| 180 | + |
| 181 | +| Var | Required | Purpose | |
| 182 | +|-----|----------|---------| |
| 183 | +| `DUFFEL_API_KEY` | yes (unless `options.duffelApiKey` is passed) | Duffel token; `duffel_test_…` = sandbox, `duffel_live_…` = production | |
| 184 | + |
| 185 | +One-time data setup: `pnpm run data:download` (populates `data/reference/` from OurAirports; |
| 186 | +gitignored). No other new env vars are introduced. |
| 187 | + |
| 188 | +### Reproduce the proof yourself |
| 189 | + |
| 190 | +```bash |
| 191 | +pnpm install |
| 192 | +pnpm run data:download # one-time reference data |
| 193 | +export DUFFEL_API_KEY=duffel_test_xxx # your sandbox key |
| 194 | +pnpm exec tsx packages/integration/scripts/duffel-search.ts # run from repo root |
| 195 | +``` |
| 196 | + |
| 197 | +Source: [`scripts/duffel-search.ts`](packages/integration/scripts/duffel-search.ts). |
| 198 | + |
| 199 | +--- |
| 200 | + |
| 201 | +## 4. Sandbox proof (real Duffel sandbox, keys redacted) |
| 202 | + |
| 203 | +Captured **2026-06-23** against `https://api.duffel.com` with a `duffel_test_…` key |
| 204 | +(`live_mode: false`). Route **JFK → LHR**, `2026-07-23`, 1 ADT, economy. |
| 205 | + |
| 206 | +### 4a. Request the adapter POSTs to `POST /air/offer_requests?return_offers=true` |
| 207 | + |
| 208 | +```json |
| 209 | +{ |
| 210 | + "data": { |
| 211 | + "slices": [{ "origin": "JFK", "destination": "LHR", "departure_date": "2026-07-23" }], |
| 212 | + "passengers": [{ "type": "adult" }], |
| 213 | + "cabin_class": "economy", |
| 214 | + "return_offers": true |
| 215 | + } |
| 216 | +} |
| 217 | +``` |
| 218 | + |
| 219 | +Headers (key redacted): `Authorization: Bearer duffel_test_***REDACTED***`, |
| 220 | +`Duffel-Version: v2`, `Content-Type: application/json`. |
| 221 | + |
| 222 | +### 4b. Real Duffel response (first offer, trimmed — `live_mode: false` confirms sandbox) |
| 223 | + |
| 224 | +```json |
| 225 | +{ |
| 226 | + "offer_request_id": "orq_0000B7dFlto81bmCx4mQXw", |
| 227 | + "offer": { |
| 228 | + "id": "off_0000B7dFlu2f9ZP1g9a2CY", |
| 229 | + "total_amount": "215.74", |
| 230 | + "total_currency": "USD", |
| 231 | + "base_amount": "182.83", |
| 232 | + "tax_amount": "32.91", |
| 233 | + "live_mode": false, |
| 234 | + "slices": [{ |
| 235 | + "origin": "JFK", "destination": "LHR", "duration": "PT7H58M", |
| 236 | + "segments": [{ |
| 237 | + "marketing_carrier": "ZZ", "flight_number": "7611", |
| 238 | + "origin": "JFK", "destination": "LHR", |
| 239 | + "departing_at": "2026-07-23T17:01:00", "arriving_at": "2026-07-24T05:59:00", |
| 240 | + "cabin": "economy" |
| 241 | + }] |
| 242 | + }] |
| 243 | + }, |
| 244 | + "total_offers": 168 |
| 245 | +} |
| 246 | +``` |
| 247 | + |
| 248 | +### 4c. Normalized output via `runAvailabilitySearch` (gates PASS, 168 raw offers → top 3) |
| 249 | + |
| 250 | +``` |
| 251 | +Gate outcome: PASS (all gates) |
| 252 | +Session id: sess_mqqxj2pe_bsunoc |
| 253 | +Raw offers: 168 | returned: 3 |
| 254 | +
|
| 255 | + 1. off_0000B7dF1KBJAIvVkG0qB8 BA0107 JFK→LHR dep 2026-07-23T17:01:00 215.15 USD (0 stop(s), 478m) |
| 256 | + 2. off_0000B7dF1KAxBcdvj9qYcs ZZ7611 JFK→LHR dep 2026-07-23T17:01:00 228.95 USD (0 stop(s), 478m) |
| 257 | + 3. off_0000B7dF1KBJAIvVkG0qBB IB3177 JFK→LHR dep 2026-07-23T17:01:00 229.96 USD (0 stop(s), 478m) |
| 258 | +
|
| 259 | +source_status: [{ "source": "duffel", "success": true, "offer_count": 168, "response_time_ms": 2054 }] |
| 260 | +``` |
| 261 | + |
| 262 | +> **Sandbox note (honest):** `currency: 'GBP'` was requested but the Duffel sandbox returned |
| 263 | +> offers in **USD**. The unified model faithfully reports whatever the supplier returns; OTAIP |
| 264 | +> does not coerce it. `ZZ` is Duffel's sandbox test airline ("Duffel Airways"); real carriers |
| 265 | +> (BA, IB) also appear because the sandbox blends fixture and live-schedule data. |
| 266 | +
|
| 267 | +### 4d. Real emitted trace for that run (durable JSONL, read back by session id) |
| 268 | + |
| 269 | +The exact two lines written to `traces/duffel-jfk-lhr-2026-07-23.jsonl` — no secrets, no PII: |
| 270 | + |
| 271 | +```json |
| 272 | +{"eventId":"evt_mqqxj4aj_1","type":"agent.executed","timestamp":"2026-06-23T17:39:50.779Z","sessionId":"sess_mqqxj2pe_bsunoc","agentId":"1.1","inputHash":"6fcd2f57","confidence":1,"durationMs":2057,"success":true,"gateResults":[{"gate":"intent_lock","passed":true},{"gate":"schema_in","passed":true},{"gate":"semantic_in","passed":true},{"gate":"cross_agent","passed":true},{"gate":"execute","passed":true},{"gate":"schema_out","passed":true},{"gate":"confidence","passed":true},{"gate":"action_class","passed":true}]} |
| 273 | +{"eventId":"evt_mqqxj4ak_2","type":"adapter.health","timestamp":"2026-06-23T17:39:50.780Z","sessionId":"sess_mqqxj2pe_bsunoc","adapterId":"duffel","status":"healthy","latencyMs":2054} |
| 274 | +``` |
| 275 | + |
| 276 | +`getRunTrace(store, 'sess_mqqxj2pe_bsunoc')` projects this into: |
| 277 | + |
| 278 | +```json |
| 279 | +{ |
| 280 | + "sessionId": "sess_mqqxj2pe_bsunoc", |
| 281 | + "outcome": "ok", |
| 282 | + "agentExecutions": [{ |
| 283 | + "agentId": "1.1", "success": true, "confidence": 1, "durationMs": 2057, |
| 284 | + "timestamp": "2026-06-23T17:39:50.779Z", |
| 285 | + "gateResults": [ |
| 286 | + { "gate": "intent_lock", "passed": true }, { "gate": "schema_in", "passed": true }, |
| 287 | + { "gate": "semantic_in", "passed": true }, { "gate": "cross_agent", "passed": true }, |
| 288 | + { "gate": "execute", "passed": true }, { "gate": "schema_out", "passed": true }, |
| 289 | + { "gate": "confidence", "passed": true }, { "gate": "action_class", "passed": true } |
| 290 | + ] |
| 291 | + }], |
| 292 | + "adapterHealth": [{ "adapterId": "duffel", "status": "healthy", "latencyMs": 2054, "timestamp": "2026-06-23T17:39:50.780Z" }] |
| 293 | +} |
| 294 | +``` |
| 295 | + |
| 296 | +--- |
| 297 | + |
| 298 | +## What runs end to end vs. what does not |
| 299 | + |
| 300 | +- ✅ **Live**: Duffel sandbox **availability search** (Agent 1.1) → real, normalized offers, |
| 301 | + through all six gates, with a durable, re-readable trace. Verified above and by the live e2e |
| 302 | + test [`duffel-e2e.test.ts`](packages/adapters/duffel/src/__tests__/duffel-e2e.test.ts) |
| 303 | + (runs when `DUFFEL_API_KEY` is set; skipped otherwise). |
| 304 | +- ⛔ **Not claimed**: booking/ticketing against a live supplier. The Duffel adapter has |
| 305 | + `book()`, but creating real orders needs more than a sandbox search key (payment/balance |
| 306 | + setup, passenger PII handling, order-management lifecycle) and is **not** proven here. It is |
| 307 | + left unstubbed rather than presented as a working "live" result. |
| 308 | + |
| 309 | +### Note for maintainers |
| 310 | + |
| 311 | +The previous `duffel-e2e.test.ts` was **stale**: it constructed `new DuffelAdapter({ apiKey })` |
| 312 | +(the real constructor is positional `(apiKey?, baseUrl?)` with `DUFFEL_API_KEY` fallback) and |
| 313 | +asserted on `offer.total_price` / `offer.itineraries`, which the unified model does not have |
| 314 | +(`offer.price` / `offer.itinerary`). It could not compile or pass against the shipped adapter. |
| 315 | +It has been corrected to the real API and unified output model, and now passes live (3/3). |
0 commit comments