@inworld/cli 2.0.0 → 2.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.
@@ -62525,7 +62525,7 @@ var init_package = __esm({
62525
62525
  "package.json"() {
62526
62526
  package_default = {
62527
62527
  name: "@inworld/cli",
62528
- version: "2.0.0",
62528
+ version: "2.1.0",
62529
62529
  type: "module",
62530
62530
  main: "dist/index.js",
62531
62531
  bin: {
@@ -62717,7 +62717,7 @@ function gotrueErrorMessage(data, fallback) {
62717
62717
  }
62718
62718
  return fallback;
62719
62719
  }
62720
- async function gotrueToken(authUrl, grantType, body) {
62720
+ async function gotrueToken(authUrl, grantType, body, signal) {
62721
62721
  const url = `${authUrl}/token?grant_type=${grantType}`;
62722
62722
  const res = await fetch(url, {
62723
62723
  method: "POST",
@@ -62725,7 +62725,8 @@ async function gotrueToken(authUrl, grantType, body) {
62725
62725
  "Content-Type": "application/json",
62726
62726
  [CLIENT_INFO_HEADER]: CLIENT_INFO
62727
62727
  },
62728
- body: JSON.stringify(body)
62728
+ body: JSON.stringify(body),
62729
+ signal
62729
62730
  });
62730
62731
  const text = await res.text();
62731
62732
  let data = {};
@@ -62819,23 +62820,55 @@ async function startLoopbackReceiver(options) {
62819
62820
  settle = { resolve: resolve2, reject };
62820
62821
  });
62821
62822
  let claimed = false;
62822
- const finish = (res, status, redeem) => {
62823
+ let ended = false;
62824
+ let failure;
62825
+ const redemption = new AbortController();
62826
+ const end = (outcome) => {
62827
+ if (ended) return false;
62828
+ ended = true;
62823
62829
  claimed = true;
62824
- redeem().then(
62830
+ if (outcome instanceof Error) {
62831
+ failure = outcome;
62832
+ redemption.abort();
62833
+ settle.reject(outcome);
62834
+ } else {
62835
+ settle.resolve(outcome);
62836
+ }
62837
+ return true;
62838
+ };
62839
+ const finish = (redeem, answer = () => {
62840
+ }) => {
62841
+ claimed = true;
62842
+ redeem(redemption.signal).then(
62843
+ // A result after cancel or the timeout is dropped. Ending closes the
62844
+ // listener first, so a browser still waiting gets no result page: its
62845
+ // connection is reset, and `answer` finds it gone.
62825
62846
  (tokens) => {
62826
- res.writeHead(status, { Location: signInResultUrl(options.pageUrl) });
62827
- res.end();
62828
- settle.resolve(tokens);
62847
+ end(tokens);
62848
+ answer(signInResultUrl(options.pageUrl, failure?.message));
62829
62849
  },
62830
62850
  (error2) => {
62831
- res.writeHead(status, {
62832
- Location: signInResultUrl(options.pageUrl, error2.message)
62833
- });
62834
- res.end();
62835
- settle.reject(error2);
62851
+ end(error2);
62852
+ answer(signInResultUrl(options.pageUrl, failure?.message));
62836
62853
  }
62837
62854
  );
62838
62855
  };
62856
+ const answerWith = (res, status) => {
62857
+ return (location) => {
62858
+ if (res.headersSent || res.destroyed) return;
62859
+ res.writeHead(status, { Location: location });
62860
+ res.end();
62861
+ };
62862
+ };
62863
+ const redeemRedirect = (params) => {
62864
+ const error2 = params.get("error_description") ?? params.get("error");
62865
+ const code = params.get("code");
62866
+ if (error2 || !code) {
62867
+ const msg = error2 ?? "No authorization code in the redirect.";
62868
+ return () => Promise.reject(new Error(msg));
62869
+ }
62870
+ return (signal) => options.redeemCode(code, signal);
62871
+ };
62839
62872
  const handler = (req, res) => {
62840
62873
  const url = new URL(req.url ?? "/", "http://127.0.0.1");
62841
62874
  if (url.pathname !== "/") {
@@ -62851,14 +62884,7 @@ async function startLoopbackReceiver(options) {
62851
62884
  res.writeHead(400).end("Unknown sign-in.");
62852
62885
  return;
62853
62886
  }
62854
- const error2 = url.searchParams.get("error_description") ?? url.searchParams.get("error");
62855
- const code = url.searchParams.get("code");
62856
- if (error2 || !code) {
62857
- const msg = error2 ?? "No authorization code in the redirect.";
62858
- finish(res, 302, () => Promise.reject(new Error(msg)));
62859
- return;
62860
- }
62861
- finish(res, 302, () => options.redeemCode(code));
62887
+ finish(redeemRedirect(url.searchParams), answerWith(res, 302));
62862
62888
  return;
62863
62889
  }
62864
62890
  if (req.method === "POST") {
@@ -62881,7 +62907,10 @@ async function startLoopbackReceiver(options) {
62881
62907
  res.writeHead(400).end("Missing refresh_token.");
62882
62908
  return;
62883
62909
  }
62884
- finish(res, 303, () => options.redeemRefreshToken(refreshToken));
62910
+ finish(
62911
+ (signal) => options.redeemRefreshToken(refreshToken, signal),
62912
+ answerWith(res, 303)
62913
+ );
62885
62914
  },
62886
62915
  () => {
62887
62916
  if (!res.headersSent) res.writeHead(413).end();
@@ -62897,23 +62926,42 @@ async function startLoopbackReceiver(options) {
62897
62926
  server.listen(options.port, "127.0.0.1", () => resolve2());
62898
62927
  });
62899
62928
  const { port } = server.address();
62900
- const timer = setTimeout(
62901
- () => settle.reject(
62929
+ const timer = setTimeout(() => {
62930
+ end(
62902
62931
  new Error(
62903
62932
  `Sign-in timed out after ${OAUTH_SIGN_IN_TIMEOUT_MS / 6e4} minutes. Run the login again.`
62904
62933
  )
62905
- ),
62906
- OAUTH_SIGN_IN_TIMEOUT_MS
62907
- );
62934
+ );
62935
+ }, OAUTH_SIGN_IN_TIMEOUT_MS);
62908
62936
  timer.unref();
62909
- const redirectTo = new URL(`http://127.0.0.1:${port}/`);
62910
- redirectTo.searchParams.set("state", state);
62937
+ const loopback = new URL(`http://127.0.0.1:${port}/`);
62938
+ loopback.searchParams.set("state", state);
62939
+ const pasteRedirect = (address) => {
62940
+ if (claimed) return false;
62941
+ let url;
62942
+ try {
62943
+ url = new URL(address.trim());
62944
+ } catch {
62945
+ return false;
62946
+ }
62947
+ if (url.origin !== loopback.origin || url.pathname !== loopback.pathname || !sameSecret(url.searchParams.get("state"), state) || !(url.searchParams.get("code") || url.searchParams.get("error") || url.searchParams.get("error_description"))) {
62948
+ return false;
62949
+ }
62950
+ finish(redeemRedirect(url.searchParams));
62951
+ return true;
62952
+ };
62953
+ const cancel = () => {
62954
+ end(new Error("Sign-in cancelled."));
62955
+ };
62911
62956
  return {
62912
62957
  state,
62913
62958
  port,
62914
- redirectTo: redirectTo.toString(),
62959
+ redirectTo: loopback.toString(),
62915
62960
  done,
62961
+ pasteRedirect,
62962
+ cancel,
62916
62963
  close: () => {
62964
+ claimed = true;
62917
62965
  clearTimeout(timer);
62918
62966
  server.close();
62919
62967
  server.closeAllConnections?.();
@@ -62925,13 +62973,18 @@ async function startBrowserSignIn(options) {
62925
62973
  const receiver = await startLoopbackReceiver({
62926
62974
  port: options.port ?? gotrueCallbackPort(),
62927
62975
  pageUrl: options.pageUrl,
62928
- redeemCode: (code) => gotrueToken(options.authUrl, "pkce", {
62929
- auth_code: code,
62930
- code_verifier: verifier
62931
- }),
62932
- redeemRefreshToken: (refreshToken) => gotrueToken(options.authUrl, "refresh_token", {
62933
- refresh_token: refreshToken
62934
- })
62976
+ redeemCode: (code, signal) => gotrueToken(
62977
+ options.authUrl,
62978
+ "pkce",
62979
+ { auth_code: code, code_verifier: verifier },
62980
+ signal
62981
+ ),
62982
+ redeemRefreshToken: (refreshToken, signal) => gotrueToken(
62983
+ options.authUrl,
62984
+ "refresh_token",
62985
+ { refresh_token: refreshToken },
62986
+ signal
62987
+ )
62935
62988
  });
62936
62989
  let url;
62937
62990
  if (options.oauthProvider) {
@@ -62949,7 +63002,12 @@ async function startBrowserSignIn(options) {
62949
63002
  page.searchParams.set("state", receiver.state);
62950
63003
  url = page.toString();
62951
63004
  }
62952
- return { url, done: receiver.done.finally(receiver.close) };
63005
+ return {
63006
+ url,
63007
+ done: receiver.done.finally(receiver.close),
63008
+ pasteRedirect: receiver.pasteRedirect,
63009
+ cancel: receiver.cancel
63010
+ };
62953
63011
  }
62954
63012
  var BEARER_TYPE_HEADER, IDENTITY_BEARER_TYPE, IDENTITY_AUTH_TYPE, GOTRUE_OAUTH_PROVIDERS, base64url2, OAUTH_SIGN_IN_TIMEOUT_MS, MAX_FORM_BYTES;
62955
63013
  var init_identity = __esm({
@@ -65243,40 +65301,79 @@ var init_browser = __esm({
65243
65301
  }
65244
65302
  });
65245
65303
 
65246
- // src/mcp/cli/login.ts
65304
+ // src/mcp/auth/sign_in_prompt.ts
65247
65305
  import * as readline from "node:readline";
65248
- async function offerBrowser(url, done) {
65249
- console.error("Please visit the following URL to authenticate:");
65250
- console.error(url);
65251
- console.error();
65252
- const readlineInterface = readline.createInterface({
65306
+ async function promptForSignIn(signIn, out) {
65307
+ const { url, done, pasteRedirect, cancel } = signIn;
65308
+ const printLink = out.printLink ?? out.print;
65309
+ const printError = out.printError ?? out.print;
65310
+ out.print("Please visit the following URL to authenticate:");
65311
+ printLink(url);
65312
+ out.print("");
65313
+ out.print(
65314
+ "Browser on another machine? Sign in there with Google, GitHub, Microsoft or SSO. It then fails to load an http://127.0.0.1 address: copy that address and paste it here. Email sign-in needs a browser on this machine."
65315
+ );
65316
+ out.print("");
65317
+ const terminal = readline.createInterface({
65253
65318
  input: process.stdin,
65254
- output: process.stdout
65319
+ output: out.output
65255
65320
  });
65256
- const enter = new Promise((resolve2) => {
65257
- readlineInterface.question(
65258
- "Press Enter to open your browser...",
65259
- () => resolve2("enter")
65260
- );
65321
+ let closed = false;
65322
+ let opened = false;
65323
+ terminal.on("close", () => {
65324
+ closed = true;
65261
65325
  });
65262
- const settled = done.then(
65263
- () => "done",
65264
- () => "done"
65265
- );
65266
- const first = await Promise.race([enter, settled]);
65267
- readlineInterface.close();
65268
- if (first === "done") return;
65269
- try {
65270
- await openInBrowser(url);
65271
- console.error("Browser opened.");
65272
- } catch {
65273
- console.error(
65274
- "Could not open browser automatically. Open the URL manually."
65326
+ terminal.on("SIGINT", () => {
65327
+ cancel();
65328
+ terminal.close();
65329
+ });
65330
+ const ask = (prompt) => {
65331
+ if (closed) return;
65332
+ terminal.setPrompt(prompt);
65333
+ terminal.prompt();
65334
+ };
65335
+ terminal.on("line", async (line) => {
65336
+ const address = line.trim();
65337
+ if (!address) {
65338
+ if (!opened) {
65339
+ opened = true;
65340
+ try {
65341
+ await openInBrowser(url);
65342
+ out.print("\u2713 Browser opened");
65343
+ } catch {
65344
+ out.print(
65345
+ "\u26A0 Could not open browser automatically. Please open the URL manually."
65346
+ );
65347
+ }
65348
+ out.print("");
65349
+ out.print("Waiting for authentication...");
65350
+ }
65351
+ ask("Paste the address here if the browser could not load it: ");
65352
+ return;
65353
+ }
65354
+ if (pasteRedirect(address)) {
65355
+ out.print("Finishing sign-in...");
65356
+ terminal.close();
65357
+ return;
65358
+ }
65359
+ printError(
65360
+ "That is not this sign-in's address. Paste the whole address from the browser's address bar; it starts with http://127.0.0.1:"
65275
65361
  );
65276
- }
65277
- console.error();
65278
- console.error("Waiting for authentication...");
65362
+ ask("Paste the address: ");
65363
+ });
65364
+ ask("Press Enter to open your browser, or paste the address: ");
65365
+ await done.catch(() => {
65366
+ });
65367
+ terminal.close();
65279
65368
  }
65369
+ var init_sign_in_prompt = __esm({
65370
+ "src/mcp/auth/sign_in_prompt.ts"() {
65371
+ "use strict";
65372
+ init_browser();
65373
+ }
65374
+ });
65375
+
65376
+ // src/mcp/cli/login.ts
65280
65377
  async function runLogin() {
65281
65378
  const source = await resolveIdentitySource(PLATFORM_URL);
65282
65379
  const oauthProvider = process.env.INWORLD_AUTH_OAUTH_PROVIDER || void 0;
@@ -65285,14 +65382,17 @@ async function runLogin() {
65285
65382
  `INWORLD_AUTH_OAUTH_PROVIDER=${oauthProvider} is not supported by inworld-mcp login (use ${GOTRUE_OAUTH_PROVIDERS.join(", ")}, or unset it to choose on the sign-in page).`
65286
65383
  );
65287
65384
  }
65288
- const { url, done } = await startBrowserSignIn({
65385
+ const signIn = await startBrowserSignIn({
65289
65386
  authUrl: source.authUrl,
65290
65387
  pageUrl: INWORLD_CLI_AUTH_URL,
65291
65388
  version: CLI_VERSION,
65292
65389
  oauthProvider
65293
65390
  });
65294
- await offerBrowser(url, done);
65295
- const tokens = await done;
65391
+ await promptForSignIn(signIn, {
65392
+ print: (line) => console.error(line),
65393
+ output: process.stdout
65394
+ });
65395
+ const tokens = await signIn.done;
65296
65396
  console.error();
65297
65397
  console.error("Exchanging for Inworld session...");
65298
65398
  const exchanged = await exchangeIdentityToken(tokens.idToken);
@@ -65329,12 +65429,12 @@ var init_login = __esm({
65329
65429
  "use strict";
65330
65430
  init_dist4();
65331
65431
  init_constants6();
65332
- init_browser();
65333
65432
  init_constants5();
65334
65433
  init_exchange();
65335
65434
  init_identity();
65336
65435
  init_paths();
65337
65436
  init_session();
65437
+ init_sign_in_prompt();
65338
65438
  }
65339
65439
  });
65340
65440