ktemia 0.0.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,371 @@
1
+ /**
2
+ * Request and response shapes for the Ktemia API.
3
+ *
4
+ * Responses are typed loosely on purpose: providers add fields often, and a
5
+ * strict type would turn a harmless new field into a compile error for every
6
+ * user. The fields typed here are the ones the API documents as stable.
7
+ */
8
+ type Vertical = "flights" | "hotels" | "cars";
9
+ type LookupKind = "airport" | "airline" | "hotelLocation" | "carLocation";
10
+ type SortOrder = "asc" | "desc";
11
+ type Cabin = "economy" | "premiumEconomy" | "business" | "first";
12
+ type Alliance = "oneworld" | "skyteam" | "starAlliance";
13
+ interface Passengers {
14
+ /** Age 12 and over. Defaults to 1. */
15
+ adults?: number;
16
+ /** Age 2 to 11. */
17
+ children?: number;
18
+ /** Under 2, on an adult's lap. */
19
+ infantsOnLap?: number;
20
+ }
21
+ interface FlightLeg {
22
+ origin: string;
23
+ destination: string;
24
+ /** YYYY-MM-DD */
25
+ date: string;
26
+ }
27
+ interface FlightSearchParams {
28
+ /** IATA airport or city code, for example "LAX" or "LON". */
29
+ origin?: string;
30
+ destination?: string;
31
+ /** YYYY-MM-DD */
32
+ departureDate?: string;
33
+ /** YYYY-MM-DD. Omit for one-way. */
34
+ returnDate?: string;
35
+ /** Multi-city, 2 to 6 legs. Mutually exclusive with origin/destination/dates. */
36
+ legs?: FlightLeg[];
37
+ cabin?: Cabin;
38
+ passengers?: Passengers;
39
+ maxStops?: number;
40
+ airlines?: string[];
41
+ alliances?: Alliance[];
42
+ maxPrice?: number;
43
+ sortBy?: "bestValue" | "price" | "duration" | "departure" | "arrival";
44
+ sortOrder?: SortOrder;
45
+ /** Two-letter market code. Prices come back in that market's currency. */
46
+ country?: string;
47
+ }
48
+ interface HotelSearchParams {
49
+ /** locationId from `lookup({ kind: "hotelLocation" })`. Not a city name. */
50
+ location: string;
51
+ /** YYYY-MM-DD */
52
+ checkin: string;
53
+ /** YYYY-MM-DD, 1 to 30 nights after checkin. */
54
+ checkout: string;
55
+ adults?: number;
56
+ rooms?: number;
57
+ childAges?: number[];
58
+ minStars?: number;
59
+ minRating?: number;
60
+ maxPrice?: number;
61
+ freeCancellation?: boolean;
62
+ amenities?: string[];
63
+ providers?: string[];
64
+ sortBy?: "bestValue" | "price" | "rating" | "stars" | "distance";
65
+ sortOrder?: SortOrder;
66
+ country?: string;
67
+ }
68
+ interface CarSearchParams {
69
+ /** locationId from `lookup({ kind: "carLocation" })`. */
70
+ pickupLocation: string;
71
+ /** Omit to return the car where it was collected. */
72
+ dropoffLocation?: string;
73
+ /** YYYY-MM-DD */
74
+ pickupDate: string;
75
+ /** YYYY-MM-DD, on or after pickupDate. */
76
+ dropoffDate: string;
77
+ /** HH:MM on the hour or half hour. Defaults to 12:00. */
78
+ pickupTime?: string;
79
+ dropoffTime?: string;
80
+ driverAge?: number;
81
+ carClass?: "small" | "medium" | "large" | "suv" | "van" | "pickup" | "luxury" | "convertible";
82
+ transmission?: "automatic" | "manual";
83
+ maxPrice?: number;
84
+ providers?: string[];
85
+ agencies?: string[];
86
+ sortBy?: "bestValue" | "price" | "rating" | "distance";
87
+ sortOrder?: SortOrder;
88
+ priceMode?: "total" | "perDay";
89
+ country?: string;
90
+ }
91
+ interface LookupParams {
92
+ kind: LookupKind;
93
+ /** Place, airport, or airline name, for example "London" or "Delta". */
94
+ query: string;
95
+ /** For kind "carLocation" only. */
96
+ type?: "city" | "airport" | "all";
97
+ country?: string;
98
+ locale?: string;
99
+ }
100
+ interface HotelReviewsParams {
101
+ hotelId: string;
102
+ sortBy?: "recent" | "oldest" | "highestScore" | "lowestScore";
103
+ travelerTypes?: Array<"families" | "couples" | "solo" | "friends" | "business">;
104
+ /** Calendar months of the stay, 1 to 12. */
105
+ months?: number[];
106
+ sources?: Array<"booking" | "agoda" | "priceline">;
107
+ search?: string;
108
+ page?: number;
109
+ }
110
+ /**
111
+ * One page of search results.
112
+ *
113
+ * `token` is the token this page was read with: pass it to `details()` for a
114
+ * result on this page. `nextToken` reads the following page. Tokens expire
115
+ * one hour after the search started.
116
+ */
117
+ interface SearchPage {
118
+ vertical: Vertical;
119
+ token: string;
120
+ hasMore: boolean;
121
+ nextToken?: string;
122
+ page?: number;
123
+ totalResults?: number;
124
+ /** Set when the search matched nothing; the page has no results then. */
125
+ noResults?: {
126
+ code: string;
127
+ message: string;
128
+ };
129
+ [field: string]: unknown;
130
+ }
131
+ interface KeyStatus {
132
+ key: {
133
+ fingerprint: string;
134
+ status: string;
135
+ createdAt?: string | null;
136
+ };
137
+ plan: {
138
+ name: string;
139
+ monthlyQuota: number;
140
+ requestsPerSecond: number;
141
+ burst: number;
142
+ };
143
+ usage: {
144
+ month: string;
145
+ used: number;
146
+ remaining: number;
147
+ resetsAt: string;
148
+ scope: string;
149
+ };
150
+ metered: boolean;
151
+ [field: string]: unknown;
152
+ }
153
+ type ApiObject = Record<string, unknown>;
154
+
155
+ /**
156
+ * The Ktemia API client.
157
+ *
158
+ * Zero dependencies: it needs only the platform `fetch`, so it runs on Node
159
+ * 20+, Bun, Deno, and edge runtimes. Keep it server-side: the API key must
160
+ * not ship to a browser.
161
+ */
162
+
163
+ declare const VERSION = "0.1.0";
164
+ declare const DEFAULT_BASE_URL = "https://api.ktemia.com";
165
+ declare const API_KEYS_URL = "https://app.ktemia.com/api-keys";
166
+ interface KtemiaOptions {
167
+ /** Defaults to the `KTEMIA_API_KEY` environment variable. */
168
+ apiKey?: string;
169
+ /** Defaults to `KTEMIA_API_URL`, then https://api.ktemia.com. Must be https. */
170
+ baseUrl?: string;
171
+ /** Per-request timeout. Searches are the slow path. Default 60 seconds. */
172
+ timeoutMs?: number;
173
+ /**
174
+ * How many times to retry a rate-limited request (RATE_LIMIT_EXCEEDED),
175
+ * waiting for Retry-After each time. Default 2. A spent monthly quota
176
+ * (QUOTA_EXCEEDED) is never retried.
177
+ */
178
+ maxRetries?: number;
179
+ /** A custom fetch, for tests or proxies. */
180
+ fetch?: typeof fetch;
181
+ /** Identifies the integration in Ktemia's request analytics, for example "ai-sdk/0.1.0". */
182
+ userAgent?: string;
183
+ /** @internal Replaces waiting, so tests do not sleep. */
184
+ sleep?: (ms: number) => Promise<void>;
185
+ }
186
+ interface RequestOptions {
187
+ query?: Record<string, unknown>;
188
+ body?: unknown;
189
+ signal?: AbortSignal;
190
+ }
191
+ /** Retry-After is seconds or an HTTP date. Clamped so a bad header cannot stall a caller. */
192
+ declare function parseRetryAfter(header: string | null): number | undefined;
193
+ /** Search, paging and details for one vertical. */
194
+ declare class VerticalResource<P> {
195
+ private readonly client;
196
+ readonly vertical: Vertical;
197
+ private readonly check;
198
+ constructor(client: Ktemia, vertical: Vertical, check: (params: P) => P);
199
+ /**
200
+ * Starts a search and reads its first page. Costs two requests.
201
+ * A search that matches nothing returns a page with `noResults` set.
202
+ */
203
+ search(params: P, options?: {
204
+ signal?: AbortSignal;
205
+ }): Promise<SearchPage>;
206
+ /** Reads the page at `token`: a searchToken, or the nextToken of the previous page. Costs one request. */
207
+ page(token: string, options?: {
208
+ signal?: AbortSignal;
209
+ }): Promise<SearchPage>;
210
+ /** Every price and booking link for one result. `token` is the token of the page it appeared on. */
211
+ details(token: string, resultId: string, options?: {
212
+ signal?: AbortSignal;
213
+ }): Promise<ApiObject>;
214
+ private empty;
215
+ }
216
+ declare class HotelsResource extends VerticalResource<HotelSearchParams> {
217
+ private readonly kt;
218
+ constructor(kt: Ktemia);
219
+ /** Guest reviews for one hotel. Pages are numbered, starting at 1. Costs one request. */
220
+ reviews(params: HotelReviewsParams, options?: {
221
+ signal?: AbortSignal;
222
+ }): Promise<ApiObject>;
223
+ }
224
+ /**
225
+ * Ktemia travel search: flights, hotels, and rental cars across 100+
226
+ * providers. Search only; booking links hand off to the provider.
227
+ *
228
+ * ```ts
229
+ * const kt = new Ktemia(); // reads KTEMIA_API_KEY
230
+ * const page = await kt.flights.search({ origin: "LAX", destination: "LON", departureDate: "2027-03-14" });
231
+ * ```
232
+ */
233
+ declare class Ktemia {
234
+ readonly flights: VerticalResource<FlightSearchParams>;
235
+ readonly hotels: HotelsResource;
236
+ readonly cars: VerticalResource<CarSearchParams>;
237
+ private readonly apiKey?;
238
+ private readonly baseUrl;
239
+ private readonly timeoutMs;
240
+ private readonly maxRetries;
241
+ private readonly fetchImpl;
242
+ private readonly clientId;
243
+ private readonly sleep;
244
+ constructor(options?: KtemiaOptions);
245
+ /** Plan, rate limits, and remaining monthly quota. Free: never counts against the quota. */
246
+ keyStatus(options?: {
247
+ signal?: AbortSignal;
248
+ }): Promise<KeyStatus>;
249
+ /**
250
+ * Resolves a place or airline name to the codes searches need: IATA codes
251
+ * for flights, a locationId for hotels and cars. Costs one request.
252
+ */
253
+ lookup(params: LookupParams, options?: {
254
+ signal?: AbortSignal;
255
+ }): Promise<ApiObject>;
256
+ /**
257
+ * Low-level call to any Ktemia endpoint, with the same authentication,
258
+ * error mapping, and retries as the typed methods.
259
+ */
260
+ request<T = ApiObject>(method: "GET" | "POST", path: string, options?: RequestOptions): Promise<T>;
261
+ private once;
262
+ }
263
+
264
+ /**
265
+ * Errors raised by the Ktemia client.
266
+ *
267
+ * Every failure is a `KtemiaError` with a stable `code`, so callers (and
268
+ * agents reading a tool result) can branch on the code instead of parsing
269
+ * the message.
270
+ */
271
+ interface KtemiaErrorOptions {
272
+ code: string;
273
+ message: string;
274
+ /** HTTP status, or 0 when the request never reached the API. */
275
+ status: number;
276
+ /** The request field the error is about, when the API names one. */
277
+ param?: string;
278
+ /** True when the same call can succeed later without changing anything. */
279
+ retryable?: boolean;
280
+ /** Seconds the API asked us to wait, from `Retry-After`. */
281
+ retryAfter?: number;
282
+ /** The parsed response body, when there was one. */
283
+ body?: unknown;
284
+ }
285
+ declare class KtemiaError extends Error {
286
+ readonly code: string;
287
+ readonly status: number;
288
+ readonly param?: string;
289
+ readonly retryable: boolean;
290
+ readonly retryAfter?: number;
291
+ readonly body?: unknown;
292
+ constructor(options: KtemiaErrorOptions);
293
+ static [Symbol.hasInstance](value: unknown): boolean;
294
+ /** A plain object safe to hand to a model or a log line. Never holds the key. */
295
+ toJSON(): {
296
+ code: string;
297
+ message: string;
298
+ retryable: boolean;
299
+ param?: string;
300
+ retryAfter?: number;
301
+ };
302
+ }
303
+ /**
304
+ * Raised before any request is sent, when the input cannot be valid.
305
+ * Nothing reached the API, so no quota was spent.
306
+ */
307
+ declare class KtemiaValidationError extends KtemiaError {
308
+ constructor(param: string, message: string);
309
+ }
310
+ /** Codes that mean "the search found nothing", not "the request was wrong". */
311
+ declare const EMPTY_RESULT_CODES: Set<string>;
312
+ declare function isKtemiaError(error: unknown): error is KtemiaError;
313
+
314
+ /**
315
+ * Input checks that run before any request is sent.
316
+ *
317
+ * Every search spends quota, so a request that can only fail is rejected
318
+ * here instead. These mirror the API's own rules; the API stays the final
319
+ * judge, so anything not checked here is still caught upstream.
320
+ */
321
+
322
+ declare const MAX_PASSENGERS = 9;
323
+ declare const MAX_NIGHTS = 30;
324
+ declare const MIN_LEGS = 2;
325
+ declare const MAX_LEGS = 6;
326
+ /** Checks a YYYY-MM-DD date is real and, unless allowed, not in the past. */
327
+ declare function checkDate(value: unknown, param: string, now?: Date): string;
328
+ /** IATA airport or city code, upper-cased: "lhr" becomes "LHR". */
329
+ declare function checkPlace(value: unknown, param: string): string;
330
+ /** Validates and normalises a flight search body. Returns a new object. */
331
+ declare function checkFlightSearch(params: FlightSearchParams, now?: Date): FlightSearchParams;
332
+ declare function checkHotelSearch(params: HotelSearchParams, now?: Date): HotelSearchParams;
333
+ declare function checkCarSearch(params: CarSearchParams, now?: Date): CarSearchParams;
334
+
335
+ /**
336
+ * The shared agent tool contract: names, descriptions, enums, and defaults.
337
+ *
338
+ * `tools.json` is copied from ktemia-mcp's `spec/tools.json` (run
339
+ * `npm run sync-spec`), so every adapter, and the MCP server, describe the
340
+ * tools to a model in the same words. Framework adapters build their own
341
+ * schemas and read only the prose and enum values from here.
342
+ */
343
+ interface ToolParamSpec {
344
+ description: string;
345
+ enum?: string;
346
+ default?: unknown;
347
+ required?: boolean;
348
+ }
349
+ interface ToolSpec {
350
+ name: string;
351
+ title: string;
352
+ description: string;
353
+ annotations: {
354
+ readOnlyHint: boolean;
355
+ destructiveHint: boolean;
356
+ idempotentHint: boolean;
357
+ openWorldHint: boolean;
358
+ };
359
+ params: Record<string, ToolParamSpec>;
360
+ }
361
+ type ToolName = "ktemia_key_status" | "ktemia_lookup" | "ktemia_search_flights" | "ktemia_search_hotels" | "ktemia_search_cars" | "ktemia_get_details" | "ktemia_get_hotel_reviews";
362
+ declare const TOOL_NAMES: ToolName[];
363
+ declare function toolSpec(name: ToolName): ToolSpec;
364
+ /** A tool's description plus the shared notes that apply to it. */
365
+ declare function toolDescription(name: ToolName, notes?: string[]): string;
366
+ declare function paramDescription(name: ToolName, param: string): string;
367
+ /** An enum's values as a non-empty tuple, the shape schema libraries expect. */
368
+ declare function enumValues(name: string): [string, ...string[]];
369
+ declare function sharedNote(name: string): string;
370
+
371
+ export { API_KEYS_URL, type Alliance, type ApiObject, type Cabin, type CarSearchParams, DEFAULT_BASE_URL, EMPTY_RESULT_CODES, type FlightLeg, type FlightSearchParams, type HotelReviewsParams, type HotelSearchParams, HotelsResource, type KeyStatus, Ktemia, KtemiaError, type KtemiaErrorOptions, type KtemiaOptions, KtemiaValidationError, type LookupKind, type LookupParams, MAX_LEGS, MAX_NIGHTS, MAX_PASSENGERS, MIN_LEGS, type Passengers, type RequestOptions, type SearchPage, type SortOrder, TOOL_NAMES, type ToolName, type ToolParamSpec, type ToolSpec, VERSION, type Vertical, VerticalResource, checkCarSearch, checkDate, checkFlightSearch, checkHotelSearch, checkPlace, enumValues, isKtemiaError, paramDescription, parseRetryAfter, sharedNote, toolDescription, toolSpec };