fortrip 4.3.0 → 5.0.0-rc.1

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.
package/dist/cli.js CHANGED
@@ -3,13 +3,14 @@ import {
3
3
  API,
4
4
  COMMANDS,
5
5
  COMMAND_GROUPS,
6
+ CURRENCIES,
6
7
  CliError,
7
- DEFAULT_API_URL,
8
- DEFAULT_MCP_URL,
9
8
  EXIT,
10
9
  GLOBAL_FLAGS,
11
10
  HTTP_ROUTES,
12
11
  LIMITS,
12
+ MCP_SERVER_URL,
13
+ PUBLIC_ORIGINS,
13
14
  TOOLS,
14
15
  VERSION,
15
16
  authorizedRequest,
@@ -20,7 +21,6 @@ import {
20
21
  logout,
21
22
  notices,
22
23
  order,
23
- renderGuide,
24
24
  renderToolResult,
25
25
  resolveApiUrl,
26
26
  resolveMcpUrl,
@@ -28,12 +28,35 @@ import {
28
28
  unwrap,
29
29
  usageError,
30
30
  whoami
31
- } from "./chunk-B6TJTLVZ.js";
31
+ } from "./chunk-GZSRXWVN.js";
32
32
 
33
33
  // src/commands.ts
34
34
  import { readFileSync } from "fs";
35
35
  import { parseArgs } from "util";
36
36
 
37
+ // src/environment.ts
38
+ var CURRENCY_COMMANDS = COMMANDS.filter(
39
+ (spec) => spec.flags.some((flag) => flag.field === "currency")
40
+ ).map((spec) => commandInvocation(spec).slice("fortrip ".length)).join(" and ");
41
+ var ENVIRONMENT = [
42
+ {
43
+ name: "FORTRIP_TOKEN",
44
+ help: "Use this token instead of the saved login (a remote agent environment or a CI run)",
45
+ internal: false
46
+ },
47
+ { name: "FORTRIP_API_URL", help: `API base URL (default ${PUBLIC_ORIGINS.api})`, internal: true },
48
+ {
49
+ name: "FORTRIP_MCP_URL",
50
+ help: `MCP server URL for fortrip mcp (default ${MCP_SERVER_URL})`,
51
+ internal: true
52
+ },
53
+ {
54
+ name: "FORTRIP_CURRENCY",
55
+ help: `Default --currency for ${CURRENCY_COMMANDS}`,
56
+ internal: false
57
+ }
58
+ ];
59
+
37
60
  // src/help.ts
38
61
  var WIDTH = 78;
39
62
  function wrap(text, indent) {
@@ -52,9 +75,7 @@ function wrap(text, indent) {
52
75
  return lines;
53
76
  }
54
77
  function positionals(spec) {
55
- return (spec.positionals ?? []).map(
56
- ({ name, optional }) => optional ? `[${name}]` : `<${name}>`
57
- );
78
+ return (spec.positionals ?? []).map(({ name }) => `<${name}>`);
58
79
  }
59
80
  function label(spec) {
60
81
  return [spec.name, ...positionals(spec)].join(" ");
@@ -107,9 +128,9 @@ Options:
107
128
  ${optionList(HELP_FLAG)}
108
129
  `;
109
130
  }
110
- var CURRENCY_COMMANDS = COMMANDS.filter(
111
- (spec) => spec.flags.some((flag) => flag.field === "displayCurrency")
112
- ).map((spec) => commandInvocation(spec).slice("fortrip ".length)).join(" and ");
131
+ var ENVIRONMENT_LINES = ENVIRONMENT.map(
132
+ (variable) => ` ${variable.name.padEnd(Math.max(...ENVIRONMENT.map((v) => v.name.length)) + 2)}${variable.help}`
133
+ ).join("\n");
113
134
  function overallHelp() {
114
135
  const rows = [
115
136
  ...COMMAND_GROUPS.map((group) => [`${group.name} <command>`, group.summary]),
@@ -131,20 +152,18 @@ ${optionList(GLOBAL_FLAGS)}
131
152
  Output is compact text on stdout; --json prints it as one line of JSON.
132
153
  Errors go to stderr, as text or, with --json, as JSON.
133
154
  Exit codes: 0 ok, 1 error (retry once when the error says it is retryable),
134
- 2 invalid arguments, or orders cancel without --max-penalty (nothing is
135
- cancelled), 3 not signed in or sign-in not accepted, 4 rate limited.
155
+ 2 invalid arguments, or orders cancel without --max-penalty or --close
156
+ (nothing is cancelled), 3 not signed in or sign-in not accepted, 4 rate
157
+ limited.
136
158
 
137
159
  Environment:
138
- FORTRIP_TOKEN Use this token instead of the saved login (CI, sandboxes)
139
- FORTRIP_API_URL API base URL (default ${DEFAULT_API_URL})
140
- FORTRIP_MCP_URL MCP server URL for fortrip mcp (default ${DEFAULT_MCP_URL})
141
- FORTRIP_DISPLAY_CURRENCY Default --display-currency for ${CURRENCY_COMMANDS}
160
+ ${ENVIRONMENT_LINES}
142
161
  `;
143
162
  }
144
163
 
145
164
  // src/commands.ts
146
- var UPPER_CASE = /* @__PURE__ */ new Set(["displayCurrency", "destination.countryCode"]);
147
- var FROM_ENV = { displayCurrency: "FORTRIP_DISPLAY_CURRENCY" };
165
+ var UPPER_CASE = /* @__PURE__ */ new Set(["currency", "destination.countryCode"]);
166
+ var FROM_ENV = { currency: "FORTRIP_CURRENCY" };
148
167
  var ONE_ITEM_LISTS = /* @__PURE__ */ new Set(["items"]);
149
168
  var GUEST_FIELD = "items.hotel.rooms.guests";
150
169
  var ROOMS_FIELD = "items.hotel.rooms";
@@ -185,16 +204,16 @@ function parseOptions(spec, argv) {
185
204
  return { values: parsed.values, positionals: parsed.positionals };
186
205
  }
187
206
  function parseCommand(spec, argv, env) {
188
- const route = spec.tool && spec.tool !== "get_planning_guide" ? HTTP_ROUTES[spec.tool] : void 0;
207
+ const route = spec.tool ? HTTP_ROUTES[spec.tool] : void 0;
189
208
  if (!route) throw new Error(`${commandInvocation(spec)} sends no request`);
190
209
  const { values, positionals: positionals2 } = parseOptions(spec, argv);
191
210
  const input = baseBody(values.body);
192
211
  const wanted = spec.positionals ?? [];
193
212
  if (positionals2.length !== wanted.length) throw usageError(`Usage: ${usageLine(spec)}`);
194
213
  wanted.forEach((positional, index) => {
195
- if (positional.field) input[positional.field] = positionals2[index];
214
+ input[positional.field] = positionals2[index];
196
215
  });
197
- setFlags(spec, values, input);
216
+ setFlags(spec, values, input, requestCurrency(spec, values, input, env));
198
217
  const fromEnv = /* @__PURE__ */ new Map();
199
218
  for (const [field, name] of Object.entries(FROM_ENV)) {
200
219
  const fallback = env[name]?.trim().toUpperCase();
@@ -209,16 +228,35 @@ function parseCommand(spec, argv, env) {
209
228
  const body = Object.fromEntries(Object.entries(input).filter(([field]) => !route.pathFields.includes(field)));
210
229
  return { method: "POST", path, body };
211
230
  }
212
- function cancelPreview(spec, argv) {
231
+ function requestCurrency(spec, values, input, env) {
232
+ const flag = spec.flags.find((candidate) => candidate.field === "currency");
233
+ if (!flag) return "USD";
234
+ const given = values[flag.flag];
235
+ if (typeof given === "string") return given.trim().toUpperCase();
236
+ if (typeof input.currency === "string") {
237
+ const currency = input.currency.trim().toUpperCase();
238
+ input.currency = currency;
239
+ return currency;
240
+ }
241
+ return env[FROM_ENV.currency]?.trim().toUpperCase() || "USD";
242
+ }
243
+ function cancelPreview(spec, argv, env) {
213
244
  const flag = spec.tool === "cancel_order" ? spec.flags.find((f) => f.field === "maximumPenalty") : void 0;
214
245
  if (!flag) return null;
215
246
  const { values, positionals: positionals2 } = parseOptions(spec, argv);
216
- if (values[flag.flag] !== void 0) return null;
247
+ const close = values.close === true;
248
+ const penalty = values[flag.flag] !== void 0;
249
+ if (close && penalty) {
250
+ throw usageError(
251
+ `--close and --${flag.flag} can't be given together: --close closes an unpaid order, --${flag.flag} cancels a paid one`
252
+ );
253
+ }
217
254
  if (positionals2.length !== 1) throw usageError(`Usage: ${usageLine(spec)}`);
218
255
  const input = { orderId: positionals2[0] };
219
- check({ ...spec, tool: "get_order" }, input, HTTP_ROUTES.get_order, /* @__PURE__ */ new Map());
256
+ const request = close || penalty ? parseCommand(spec, argv, env) : null;
257
+ if (!request) check({ ...spec, tool: "get_order" }, input, HTTP_ROUTES.get_order, /* @__PURE__ */ new Map());
220
258
  const orderId = input.orderId;
221
- return { method: "GET", path: API.order(orderId), flag: `--${flag.flag}`, orderId };
259
+ return { method: "GET", path: API.order(orderId), flag: `--${flag.flag}`, orderId, close, request };
222
260
  }
223
261
  function check(spec, input, route, fromEnv) {
224
262
  const schema = TOOLS.find((tool) => tool.name === spec.tool)?.input;
@@ -244,7 +282,7 @@ function check(spec, input, route, fromEnv) {
244
282
  { details: { issues } }
245
283
  );
246
284
  }
247
- function setFlags(spec, values, input) {
285
+ function setFlags(spec, values, input, currency) {
248
286
  const given = spec.flags.filter((flag) => values[flag.flag] !== void 0);
249
287
  const picked = /* @__PURE__ */ new Map();
250
288
  for (const flag of given) {
@@ -272,7 +310,7 @@ function setFlags(spec, values, input) {
272
310
  if (flag.field === GUEST_FIELD) {
273
311
  setField(input, ROOMS_FIELD, withGuests(getField(input, ROOMS_FIELD), parseGuests(raw)));
274
312
  } else {
275
- setField(input, flag.field, readFlag(flag, raw));
313
+ setField(input, flag.field, readFlag(flag, raw, currency));
276
314
  }
277
315
  }
278
316
  }
@@ -332,7 +370,7 @@ function setField(body, field, value) {
332
370
  for (const part of path) node = step(node, part, true);
333
371
  node[last] = value;
334
372
  }
335
- function readFlag(flag, raw) {
373
+ function readFlag(flag, raw, currency) {
336
374
  if (flag.type === "boolean") return flag.value ?? true;
337
375
  const given = Array.isArray(raw) ? raw : [raw];
338
376
  switch (flag.field) {
@@ -342,7 +380,8 @@ function readFlag(flag, raw) {
342
380
  return given.map(parseRoom);
343
381
  case "filters.price.maxPerRoomNight":
344
382
  case "total":
345
- return { amount: given[0].trim(), currency: "USD" };
383
+ case "maximumPenalty":
384
+ return { amount: given[0].trim(), currency };
346
385
  }
347
386
  const parsed = given.map((value) => {
348
387
  if (flag.type === "number") return num(flag.flag, value);
@@ -409,11 +448,6 @@ function withGuests(fromBody, guests) {
409
448
  return rooms;
410
449
  }
411
450
 
412
- // src/guide.ts
413
- function readGuide(topic) {
414
- return renderGuide("cli", topic).replaceAll("fortrip@latest", `fortrip@${VERSION}`);
415
- }
416
-
417
451
  // ../../node_modules/.pnpm/uqr@0.1.3/node_modules/uqr/dist/index.mjs
418
452
  var QrCodeDataType = /* @__PURE__ */ ((QrCodeDataType2) => {
419
453
  QrCodeDataType2[QrCodeDataType2["Border"] = -1] = "Border";
@@ -1150,15 +1184,6 @@ async function run(argv) {
1150
1184
  process.stdout.write(commandHelp(spec));
1151
1185
  return void 0;
1152
1186
  }
1153
- if (spec.group === null && spec.name === "guide") {
1154
- const { positionals: positionals2 } = parseOptions(spec, rest);
1155
- const topic = positionals2.join(" ");
1156
- if (topic !== "" && topic !== "planning") {
1157
- throw usageError("Usage: fortrip guide [planning]");
1158
- }
1159
- process.stdout.write(readGuide(topic || "main"));
1160
- return void 0;
1161
- }
1162
1187
  const apiUrl = resolveApiUrl();
1163
1188
  const log = (line) => process.stderr.write(`${line}
1164
1189
  `);
@@ -1182,29 +1207,78 @@ async function run(argv) {
1182
1207
  case "mcp": {
1183
1208
  parseOptions(spec, rest);
1184
1209
  const mcpUrl = resolveMcpUrl(apiUrl);
1185
- const { runMcpBridge } = await import("./mcp-bridge-NHYLPWBJ.js");
1210
+ const { runMcpBridge } = await import("./mcp-bridge-IK2S4XXQ.js");
1186
1211
  await runMcpBridge(mcpUrl, apiUrl);
1187
1212
  return void 0;
1188
1213
  }
1189
1214
  }
1190
1215
  }
1191
- const preview = cancelPreview(spec, rest);
1192
- if (preview) {
1193
- const order2 = unwrap(await authorizedRequest(apiUrl, preview.path, { method: preview.method }));
1194
- throw usageError(notCancelled(spec, preview.flag, preview.orderId, order2));
1195
- }
1216
+ const preview = cancelPreview(spec, rest, process.env);
1217
+ if (preview) return cancelOrder(apiUrl, spec, preview);
1196
1218
  const { method, path, body } = parseCommand(spec, rest, process.env);
1219
+ if (spec.tool === "pay_order" && body) await inOrderCurrency(apiUrl, spec, rest, body);
1197
1220
  const data = unwrap(await authorizedRequest(apiUrl, path, { method, body }));
1198
1221
  return { data, text: renderToolResult(spec.tool, data, "cli") };
1199
1222
  }
1223
+ async function inOrderCurrency(apiUrl, spec, rest, body) {
1224
+ const [orderId] = parseOptions(spec, rest).positionals;
1225
+ const order2 = unwrap(await authorizedRequest(apiUrl, API.order(orderId), { method: "GET" }));
1226
+ const currency = order2?.price?.total?.currency;
1227
+ if (typeof currency !== "string" || !CURRENCIES.includes(currency)) {
1228
+ throw new CliError(
1229
+ `Not sent: the order ${orderId} has no price.total.currency in USD or CNY to pay in.`,
1230
+ EXIT.error
1231
+ );
1232
+ }
1233
+ body.total = { ...body.total, currency };
1234
+ check(spec, { ...body, orderId }, HTTP_ROUTES.pay_order, /* @__PURE__ */ new Map());
1235
+ }
1236
+ async function cancelOrder(apiUrl, spec, preview) {
1237
+ const { flag, orderId, close, request } = preview;
1238
+ const order2 = unwrap(await authorizedRequest(apiUrl, preview.path, { method: preview.method }));
1239
+ const read = order2;
1240
+ const closable = read?.cancel?.available === true && read.cancel.reason === "unpaid";
1241
+ if (!request || !close && closable) throw usageError(notCancelled(spec, flag, orderId, order2));
1242
+ if (close) {
1243
+ if (read?.status === "expired") return { data: order2, text: renderToolResult("cancel_order", order2, "cli") };
1244
+ if (!closable) {
1245
+ if (read?.cancel?.available === true && read.cancel.reason === "cancellable") {
1246
+ throw usageError(
1247
+ `Not closed: the order is paid. To cancel it under its terms, show the user cancel.penalty and, once they agree, run \`${commandInvocation(spec)} ${orderId} ${flag} <amount>\`.`
1248
+ );
1249
+ }
1250
+ throw new CliError(["Not closed: nothing was sent.", ...orderLines(order2)].join("\n"), EXIT.error);
1251
+ }
1252
+ const data2 = unwrap(await authorizedRequest(apiUrl, request.path, { method: "POST", body: request.body }));
1253
+ const text = renderToolResult("cancel_order", data2, "cli");
1254
+ if (data2?.status !== "expired") {
1255
+ throw new CliError(["Not closed: it was paid meanwhile.", text].join("\n"), EXIT.error);
1256
+ }
1257
+ return { data: data2, text };
1258
+ }
1259
+ const penalty = read?.cancel?.penalty?.currency;
1260
+ const currency = typeof penalty === "string" ? penalty : read?.price?.total?.currency;
1261
+ if (typeof currency !== "string") {
1262
+ throw new CliError(`Not sent: the order ${orderId} has no currency to send the penalty in.`, EXIT.error);
1263
+ }
1264
+ const body = request.body;
1265
+ body.maximumPenalty = { ...body.maximumPenalty, currency };
1266
+ check(spec, { ...body, orderId }, HTTP_ROUTES.cancel_order, /* @__PURE__ */ new Map());
1267
+ const data = unwrap(await authorizedRequest(apiUrl, request.path, { method: "POST", body }));
1268
+ return { data, text: renderToolResult("cancel_order", data, "cli") };
1269
+ }
1270
+ function orderLines(order2) {
1271
+ return renderToolResult("get_order", order2, "cli").split("\n").filter((text) => text.startsWith("Hotel: ") || text.startsWith("Cancel: "));
1272
+ }
1200
1273
  function notCancelled(spec, flag, orderId, order2) {
1201
- const lines = renderToolResult("get_order", order2, "cli").split("\n").filter((text) => text.startsWith("Hotel: ") || text.startsWith("Cancel: "));
1202
1274
  const parsed = order.safeParse(order2);
1203
1275
  const cancel = parsed.success ? parsed.data.cancel : void 0;
1204
- const next = cancel?.available && cancel.penalty ? [
1276
+ const next = cancel?.available && cancel.reason === "unpaid" ? [
1277
+ `Only once the user says they no longer want this order, run \`${commandInvocation(spec)} ${orderId} --close\`.`
1278
+ ] : cancel?.available && cancel.penalty ? [
1205
1279
  `Show the user the stay, this penalty and this refund, and ask them to confirm. Only after a clear yes, run \`${commandInvocation(spec)} ${orderId} ${flag} ${cancel.penalty.amount}\`.`
1206
1280
  ] : cancel ? [] : [`${commandInvocation(spec)} needs ${flag}, the penalty the user agreed to.`];
1207
- return ["Not cancelled: nothing was sent.", ...lines, ...next].join("\n");
1281
+ return ["Not cancelled: nothing was sent.", ...orderLines(order2), ...next].join("\n");
1208
1282
  }
1209
1283
  function account(data) {
1210
1284
  const { apiUrl, userId, email, name } = data;
@@ -1233,8 +1307,9 @@ function takeJson(argv) {
1233
1307
  }
1234
1308
  function walletCode(argv, data) {
1235
1309
  if (argv[0] !== "orders" || argv[1] !== "pay" || !process.stdout.isTTY) return null;
1236
- const wallets = data?.payment?.wallets;
1237
- const qrData = wallets?.wechatPay?.qrData;
1310
+ const methods = data?.payment?.methods;
1311
+ const wallet = Array.isArray(methods) ? methods.find((m) => m?.id === "stripe_alipay_wechat_pay") : void 0;
1312
+ const qrData = wallet?.wechatPay?.qrData;
1238
1313
  return typeof qrData === "string" ? terminalQr(qrData, process.stdout.columns) : null;
1239
1314
  }
1240
1315
  async function main() {
@@ -11,7 +11,7 @@ import {
11
11
  renderError,
12
12
  renderGuide,
13
13
  requireToken
14
- } from "./chunk-B6TJTLVZ.js";
14
+ } from "./chunk-GZSRXWVN.js";
15
15
 
16
16
  // src/mcp-bridge.ts
17
17
  import {
@@ -208,17 +208,13 @@ function localServer() {
208
208
  title: spec.title,
209
209
  description: spec.description,
210
210
  annotations: spec.annotations,
211
- ...spec.output ? { outputSchema: passThrough(spec.output) } : {}
211
+ outputSchema: passThrough(spec.output)
212
212
  };
213
- if (spec.input === null) {
214
- server.registerTool(spec.name, config, handler);
215
- } else {
216
- server.registerTool(
217
- spec.name,
218
- { ...config, inputSchema: passThrough(spec.input) },
219
- handler
220
- );
221
- }
213
+ server.registerTool(
214
+ spec.name,
215
+ { ...config, inputSchema: passThrough(spec.input) },
216
+ handler
217
+ );
222
218
  }
223
219
  return server;
224
220
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fortrip",
3
- "version": "4.3.0",
3
+ "version": "5.0.0-rc.1",
4
4
  "license": "Apache-2.0",
5
5
  "description": "Search, price and book Fortrip hotels from the terminal or an AI agent.",
6
6
  "mcpName": "io.github.FortripEngineering/fortrip",
@@ -33,7 +33,7 @@
33
33
  "typescript": "^5.9.0",
34
34
  "uqr": "0.1.3",
35
35
  "zod": "4.4.3",
36
- "@fortrip/agent-contract": "4.3.0"
36
+ "@fortrip/agent-contract": "5.0.0-rc.1"
37
37
  },
38
38
  "scripts": {
39
39
  "build": "tsup",