@lvce-editor/chat-view 7.11.0 → 7.12.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.
@@ -4365,6 +4365,7 @@ const getBackendAuthUrl = (backendUrl, path) => {
4365
4365
  return `${trimTrailingSlashes(backendUrl)}${path}`;
4366
4366
  };
4367
4367
 
4368
+ const PlatformTypeElectron$1 = 2;
4368
4369
  const getCurrentHref = async () => {
4369
4370
  try {
4370
4371
  return await invoke('Layout.getHref');
@@ -4376,11 +4377,20 @@ const getCurrentHref = async () => {
4376
4377
  }
4377
4378
  return globalThis.location.href;
4378
4379
  };
4379
- const getBackendLoginUrl = async backendUrl => {
4380
- const loginUrl = new URL(getBackendAuthUrl(backendUrl, '/login'));
4381
- const redirectUri = await getCurrentHref();
4380
+ const getEffectiveRedirectUri = async (platform, uid, redirectUri) => {
4382
4381
  if (redirectUri) {
4383
- loginUrl.searchParams.set('redirect_uri', redirectUri);
4382
+ return redirectUri;
4383
+ }
4384
+ if (platform === PlatformTypeElectron$1) {
4385
+ return `http://localhost:${await invoke('OAuthServer.create', String(uid))}`;
4386
+ }
4387
+ return getCurrentHref();
4388
+ };
4389
+ const getBackendLoginUrl = async (backendUrl, platform = 0, uid = 0, redirectUri = '') => {
4390
+ const loginUrl = new URL(getBackendAuthUrl(backendUrl, '/login'));
4391
+ const effectiveRedirectUri = await getEffectiveRedirectUri(platform, uid, redirectUri);
4392
+ if (effectiveRedirectUri) {
4393
+ loginUrl.searchParams.set('redirect_uri', effectiveRedirectUri);
4384
4394
  }
4385
4395
  return loginUrl.toString();
4386
4396
  };
@@ -4561,6 +4571,25 @@ const waitForBackendLogin = async (backendUrl, timeoutMs = 30_000, pollIntervalM
4561
4571
  return getLoggedOutBackendAuthState(lastErrorMessage);
4562
4572
  };
4563
4573
 
4574
+ const hasAuthorizationCode = value => {
4575
+ return typeof value === 'string' && value.length > 0;
4576
+ };
4577
+ const waitForElectronBackendLogin = async (backendUrl, uid, timeoutMs = 30_000, pollIntervalMs = 1000) => {
4578
+ const started = Date.now();
4579
+ const deadline = started + timeoutMs;
4580
+ while (Date.now() < deadline) {
4581
+ const authorizationCode = await invoke('OAuthServer.getCode', String(uid));
4582
+ if (hasAuthorizationCode(authorizationCode)) {
4583
+ const elapsed = Date.now() - started;
4584
+ const remainingTime = Math.max(0, timeoutMs - elapsed);
4585
+ return waitForBackendLogin(backendUrl, remainingTime, pollIntervalMs);
4586
+ }
4587
+ await delay(pollIntervalMs);
4588
+ }
4589
+ return getLoggedOutBackendAuthState('Timed out waiting for backend login.');
4590
+ };
4591
+
4592
+ const PlatformTypeElectron = 2;
4564
4593
  const isLoginResponse = value => {
4565
4594
  if (!value || typeof value !== 'object') {
4566
4595
  return false;
@@ -4568,19 +4597,29 @@ const isLoginResponse = value => {
4568
4597
  return true;
4569
4598
  };
4570
4599
  const getLoggedInState = (state, response) => {
4600
+ const {
4601
+ userName,
4602
+ userSubscriptionPlan,
4603
+ userUsedTokens
4604
+ } = state;
4571
4605
  const accessToken = typeof response.accessToken === 'string' ? response.accessToken : '';
4572
4606
  return {
4573
4607
  ...state,
4574
4608
  authAccessToken: accessToken,
4575
4609
  authErrorMessage: '',
4576
- userName: typeof response.userName === 'string' ? response.userName : state.userName,
4610
+ userName: typeof response.userName === 'string' ? response.userName : userName,
4577
4611
  userState: accessToken ? 'loggedIn' : 'loggedOut',
4578
- userSubscriptionPlan: typeof response.subscriptionPlan === 'string' ? response.subscriptionPlan : state.userSubscriptionPlan,
4579
- userUsedTokens: typeof response.usedTokens === 'number' ? response.usedTokens : state.userUsedTokens
4612
+ userSubscriptionPlan: typeof response.subscriptionPlan === 'string' ? response.subscriptionPlan : userSubscriptionPlan,
4613
+ userUsedTokens: typeof response.usedTokens === 'number' ? response.usedTokens : userUsedTokens
4580
4614
  };
4581
4615
  };
4582
4616
  const handleClickLogin = async state => {
4583
- if (!state.backendUrl) {
4617
+ const {
4618
+ backendUrl,
4619
+ platform,
4620
+ uid
4621
+ } = state;
4622
+ if (!backendUrl) {
4584
4623
  return {
4585
4624
  ...state,
4586
4625
  authErrorMessage: 'Backend URL is missing.',
@@ -4592,8 +4631,8 @@ const handleClickLogin = async state => {
4592
4631
  authErrorMessage: '',
4593
4632
  userState: 'loggingIn'
4594
4633
  };
4595
- if (state.uid) {
4596
- set(state.uid, state, signingInState);
4634
+ if (uid) {
4635
+ set(uid, state, signingInState);
4597
4636
  await invoke('Chat.rerender');
4598
4637
  }
4599
4638
  try {
@@ -4615,9 +4654,9 @@ const handleClickLogin = async state => {
4615
4654
  }
4616
4655
  return getLoggedInState(signingInState, response);
4617
4656
  }
4618
- const url = await getBackendLoginUrl(state.backendUrl);
4619
- await openUrl(url, state.platform);
4620
- const authState = await waitForBackendLogin(state.backendUrl);
4657
+ const url = await getBackendLoginUrl(backendUrl, platform, uid);
4658
+ await openUrl(url, platform);
4659
+ const authState = platform === PlatformTypeElectron ? await waitForElectronBackendLogin(backendUrl, uid) : await waitForBackendLogin(backendUrl);
4621
4660
  return {
4622
4661
  ...signingInState,
4623
4662
  ...authState
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/chat-view",
3
- "version": "7.11.0",
3
+ "version": "7.12.0",
4
4
  "description": "Chat View Worker",
5
5
  "repository": {
6
6
  "type": "git",