@adminide-stack/clock-tik-mobile 0.0.0-stage → 12.0.37-alpha.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.
- package/.claude-flow/data/pending-insights.jsonl +43 -0
- package/.claude-flow/sessions/session-1794323376548.json +14 -0
- package/CHANGELOG.md +31 -0
- package/jest.config.cjs +22 -0
- package/lib/assets/home/loop-day.jpg +0 -0
- package/lib/assets/home/loop-day.mp4 +0 -0
- package/lib/assets/home/loop-time-back.jpg +0 -0
- package/lib/assets/home/loop-time-back.mp4 +0 -0
- package/lib/assets/home/loop-where.jpg +0 -0
- package/lib/assets/home/loop-where.mp4 +0 -0
- package/lib/assets/home/reel-gym.jpg +0 -0
- package/lib/assets/home/reel-gym.mp4 +0 -0
- package/lib/assets/home/reel-internet.jpg +0 -0
- package/lib/assets/home/reel-internet.mp4 +0 -0
- package/lib/assets/icon.png +0 -0
- package/lib/assets/lottie/.claude-flow/data/pending-insights.jsonl +1 -0
- package/lib/assets/lottie/lucy-mark-calendar.json +1827 -0
- package/lib/assets/lottie/lucy-mark-call.json +1470 -0
- package/lib/assets/lottie/lucy-mark-connect.json +2552 -0
- package/lib/assets/lottie/lucy-mark-details.json +1735 -0
- package/lib/assets/lottie/lucy-mark-email.json +1363 -0
- package/lib/assets/lottie/lucy-mark-message.json +1154 -0
- package/lib/assets/lottie/lucy-mark-search.json +1067 -0
- package/lib/assets/lottie/lucy-mark-think.json +1253 -0
- package/lib/assets/lottie/lucy-mark-write.json +1532 -0
- package/lib/assets/lottie/time-back.json +16458 -0
- package/lib/assets/lottie/where-lucy-works-chat.json +6170 -0
- package/lib/assets/lottie/where-lucy-works-cloud.json +10066 -0
- package/lib/assets/lottie/where-lucy-works-mine-none.json +2478 -0
- package/lib/assets/lottie/where-lucy-works-mine-offline.json +2373 -0
- package/lib/assets/lottie/where-lucy-works-mine-online.json +7881 -0
- package/lib/assets/sounds/hours-back.m4a +0 -0
- package/lib/assets/sounds/meet-lucy.m4a +0 -0
- package/lib/assets/sounds/power-on.m4a +0 -0
- package/lib/assets/sounds/wind-back.m4a +0 -0
- package/lib/assets/sounds/youre-in.m4a +0 -0
- package/lib/chat/agent.js +358 -0
- package/lib/chat/agent.js.map +1 -0
- package/lib/chat/appFront.js +32 -0
- package/lib/chat/appFront.js.map +1 -0
- package/lib/chat/auth.js +370 -0
- package/lib/chat/auth.js.map +1 -0
- package/lib/chat/chores.js +360 -0
- package/lib/chat/chores.js.map +1 -0
- package/lib/chat/connectors.js +42 -0
- package/lib/chat/connectors.js.map +1 -0
- package/lib/chat/jobs.js +223 -0
- package/lib/chat/jobs.js.map +1 -0
- package/lib/chat/lucyMotion.js +6 -0
- package/lib/chat/lucyMotion.js.map +1 -0
- package/lib/chat/protocol.js +111 -0
- package/lib/chat/protocol.js.map +1 -0
- package/lib/chat/results.js +133 -0
- package/lib/chat/results.js.map +1 -0
- package/lib/chat/store.js +81 -0
- package/lib/chat/store.js.map +1 -0
- package/lib/chat/useDictation.js +168 -0
- package/lib/chat/useDictation.js.map +1 -0
- package/lib/chat/useLucyChat.js +451 -0
- package/lib/chat/useLucyChat.js.map +1 -0
- package/lib/chat/useMyComputer.js +32 -0
- package/lib/chat/useMyComputer.js.map +1 -0
- package/lib/chat/whereLucyWorks.js +52 -0
- package/lib/chat/whereLucyWorks.js.map +1 -0
- package/lib/chat/workspace.js +124 -0
- package/lib/chat/workspace.js.map +1 -0
- package/lib/components/AreaShot.js +289 -0
- package/lib/components/AreaShot.js.map +1 -0
- package/lib/components/Backdrops.js +262 -0
- package/lib/components/Backdrops.js.map +1 -0
- package/lib/components/BottomSheet.js +61 -0
- package/lib/components/BottomSheet.js.map +1 -0
- package/lib/components/ChatSheets.js +429 -0
- package/lib/components/ChatSheets.js.map +1 -0
- package/lib/components/ConnectorLogo.js +91 -0
- package/lib/components/ConnectorLogo.js.map +1 -0
- package/lib/components/CustomDrawer.js +534 -0
- package/lib/components/CustomDrawer.js.map +1 -0
- package/lib/components/IdeasPane.js +394 -0
- package/lib/components/IdeasPane.js.map +1 -0
- package/lib/components/ImessageSheet.js +261 -0
- package/lib/components/ImessageSheet.js.map +1 -0
- package/lib/components/KeyboardAvoider.js +29 -0
- package/lib/components/KeyboardAvoider.js.map +1 -0
- package/lib/components/Logo.js +81 -0
- package/lib/components/Logo.js.map +1 -0
- package/lib/components/LucyWorking.js +67 -0
- package/lib/components/LucyWorking.js.map +1 -0
- package/lib/components/Markdown.js +260 -0
- package/lib/components/Markdown.js.map +1 -0
- package/lib/components/Media.js +93 -0
- package/lib/components/Media.js.map +1 -0
- package/lib/components/MessagesAppIcon.js +23 -0
- package/lib/components/MessagesAppIcon.js.map +1 -0
- package/lib/components/MicButton.js +71 -0
- package/lib/components/MicButton.js.map +1 -0
- package/lib/components/ResultCards.js +974 -0
- package/lib/components/ResultCards.js.map +1 -0
- package/lib/components/SignInPrompt.js +148 -0
- package/lib/components/SignInPrompt.js.map +1 -0
- package/lib/components/SignedOut.js +38 -0
- package/lib/components/SignedOut.js.map +1 -0
- package/lib/components/TalkToLucyHero.js +487 -0
- package/lib/components/TalkToLucyHero.js.map +1 -0
- package/lib/components/TalkToLucySheet.js +182 -0
- package/lib/components/TalkToLucySheet.js.map +1 -0
- package/lib/components/WhereLucyWorks.js +277 -0
- package/lib/components/WhereLucyWorks.js.map +1 -0
- package/lib/components/WorkspaceSection.js +271 -0
- package/lib/components/WorkspaceSection.js.map +1 -0
- package/lib/components/YantraMark.js +13 -0
- package/lib/components/YantraMark.js.map +1 -0
- package/lib/components/drawerHeader.js +38 -0
- package/lib/components/drawerHeader.js.map +1 -0
- package/lib/components/lottieInstalled.js +6 -0
- package/lib/components/lottieInstalled.js.map +1 -0
- package/lib/components/ui.js +550 -0
- package/lib/components/ui.js.map +1 -0
- package/lib/components/useSignOut.js +7 -0
- package/lib/components/useSignOut.js.map +1 -0
- package/lib/components/viewport.js +121 -0
- package/lib/components/viewport.js.map +1 -0
- package/lib/components/voiceDial.js +10 -0
- package/lib/components/voiceDial.js.map +1 -0
- package/lib/compute.js +157 -0
- package/lib/compute.js.map +1 -0
- package/lib/config/env-config.js +21 -0
- package/lib/config/env-config.js.map +1 -0
- package/lib/config/routes.js +15 -0
- package/lib/config/routes.js.map +1 -0
- package/lib/content.js +25 -0
- package/lib/content.js.map +1 -0
- package/lib/hooks/keyboardCover.js +39 -0
- package/lib/hooks/keyboardCover.js.map +1 -0
- package/lib/hooks/useKeyboard.js +121 -0
- package/lib/hooks/useKeyboard.js.map +1 -0
- package/lib/hooks/useReducedMotion.js +13 -0
- package/lib/hooks/useReducedMotion.js.map +1 -0
- package/lib/imessage/openMessages.js +25 -0
- package/lib/imessage/openMessages.js.map +1 -0
- package/lib/imessage/platform.js +12 -0
- package/lib/imessage/platform.js.map +1 -0
- package/lib/imessage/textLink.js +262 -0
- package/lib/imessage/textLink.js.map +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -0
- package/lib/live/card.js +28 -0
- package/lib/live/card.js.map +1 -0
- package/lib/live/liveActivity.js +144 -0
- package/lib/live/liveActivity.js.map +1 -0
- package/lib/module.js +3 -0
- package/lib/module.js.map +1 -0
- package/lib/offer/BlackFridayOffer.js +378 -0
- package/lib/offer/BlackFridayOffer.js.map +1 -0
- package/lib/offer/blackFriday.js +78 -0
- package/lib/offer/blackFriday.js.map +1 -0
- package/lib/offer/parts.js +624 -0
- package/lib/offer/parts.js.map +1 -0
- package/lib/offer/state.js +63 -0
- package/lib/offer/state.js.map +1 -0
- package/lib/push/push.js +142 -0
- package/lib/push/push.js.map +1 -0
- package/lib/push/usePushNotifications.js +39 -0
- package/lib/push/usePushNotifications.js.map +1 -0
- package/lib/routes.json +175 -0
- package/lib/screens/Account/index.js +209 -0
- package/lib/screens/Account/index.js.map +1 -0
- package/lib/screens/Chat/index.js +710 -0
- package/lib/screens/Chat/index.js.map +1 -0
- package/lib/screens/Chores/index.js +18 -0
- package/lib/screens/Chores/index.js.map +1 -0
- package/lib/screens/Connectors/index.js +17 -0
- package/lib/screens/Connectors/index.js.map +1 -0
- package/lib/screens/History/index.js +380 -0
- package/lib/screens/History/index.js.map +1 -0
- package/lib/screens/Home/index.js +2711 -0
- package/lib/screens/Home/index.js.map +1 -0
- package/lib/screens/Home/media.js +25 -0
- package/lib/screens/Home/media.js.map +1 -0
- package/lib/screens/Welcome/index.js +1066 -0
- package/lib/screens/Welcome/index.js.map +1 -0
- package/lib/talk/talkToLucy.js +139 -0
- package/lib/talk/talkToLucy.js.map +1 -0
- package/lib/theme/colors.js +29 -0
- package/lib/theme/colors.js.map +1 -0
- package/lib/theme/fonts.js +17 -0
- package/lib/theme/fonts.js.map +1 -0
- package/lib/theme/muse.js +134 -0
- package/lib/theme/muse.js.map +1 -0
- package/lib/utils/links.js +54 -0
- package/lib/utils/links.js.map +1 -0
- package/lib/utils/yantraLinks.js +68 -0
- package/lib/utils/yantraLinks.js.map +1 -0
- package/lib/welcome/flow.js +375 -0
- package/lib/welcome/flow.js.map +1 -0
- package/lib/welcome/haptics.js +125 -0
- package/lib/welcome/haptics.js.map +1 -0
- package/lib/welcome/parts.js +1996 -0
- package/lib/welcome/parts.js.map +1 -0
- package/lib/welcome/powerOn.js +29 -0
- package/lib/welcome/powerOn.js.map +1 -0
- package/lib/welcome/seen.js +41 -0
- package/lib/welcome/seen.js.map +1 -0
- package/lib/welcome/sound.js +110 -0
- package/lib/welcome/sound.js.map +1 -0
- package/package.json +66 -5
- package/rollup.config.mjs +42 -0
- package/src/.claude-flow/data/pending-insights.jsonl +19 -0
- package/src/.claude-flow/sessions/session-1795013028422.json +14 -0
- package/src/__tests__/auth.test.ts +94 -0
- package/src/__tests__/blackFriday.test.ts +75 -0
- package/src/__tests__/cardTokens.test.ts +77 -0
- package/src/__tests__/chores.test.ts +179 -0
- package/src/__tests__/choresList.test.ts +100 -0
- package/src/__tests__/content.test.ts +54 -0
- package/src/__tests__/dictation.test.ts +186 -0
- package/src/__tests__/haptics.test.ts +202 -0
- package/src/__tests__/jobs.test.ts +130 -0
- package/src/__tests__/keyboardCover.test.ts +84 -0
- package/src/__tests__/liveCard.test.ts +39 -0
- package/src/__tests__/lockedPhone.test.ts +355 -0
- package/src/__tests__/lucyMotion.test.ts +34 -0
- package/src/__tests__/platform.test.ts +32 -0
- package/src/__tests__/powerOn.test.ts +62 -0
- package/src/__tests__/protocol.test.ts +125 -0
- package/src/__tests__/push.test.ts +111 -0
- package/src/__tests__/results.test.ts +256 -0
- package/src/__tests__/sound.test.ts +248 -0
- package/src/__tests__/talkToLucy.test.ts +205 -0
- package/src/__tests__/textLink.test.ts +91 -0
- package/src/__tests__/voiceDial.test.ts +53 -0
- package/src/__tests__/welcomeFlow.test.ts +157 -0
- package/src/__tests__/whereLucyWorks.test.ts +81 -0
- package/src/__tests__/yantraLinks.test.ts +96 -0
- package/src/assets/home/loop-day.jpg +0 -0
- package/src/assets/home/loop-day.mp4 +0 -0
- package/src/assets/home/loop-time-back.jpg +0 -0
- package/src/assets/home/loop-time-back.mp4 +0 -0
- package/src/assets/home/loop-where.jpg +0 -0
- package/src/assets/home/loop-where.mp4 +0 -0
- package/src/assets/home/reel-gym.jpg +0 -0
- package/src/assets/home/reel-gym.mp4 +0 -0
- package/src/assets/home/reel-internet.jpg +0 -0
- package/src/assets/home/reel-internet.mp4 +0 -0
- package/src/assets/icon.png +0 -0
- package/src/assets/lottie/.claude-flow/data/pending-insights.jsonl +1 -0
- package/src/assets/lottie/lucy-mark-calendar.json +1827 -0
- package/src/assets/lottie/lucy-mark-call.json +1470 -0
- package/src/assets/lottie/lucy-mark-connect.json +2552 -0
- package/src/assets/lottie/lucy-mark-details.json +1735 -0
- package/src/assets/lottie/lucy-mark-email.json +1363 -0
- package/src/assets/lottie/lucy-mark-message.json +1154 -0
- package/src/assets/lottie/lucy-mark-search.json +1067 -0
- package/src/assets/lottie/lucy-mark-think.json +1253 -0
- package/src/assets/lottie/lucy-mark-write.json +1532 -0
- package/src/assets/lottie/time-back.json +16458 -0
- package/src/assets/lottie/where-lucy-works-chat.json +6170 -0
- package/src/assets/lottie/where-lucy-works-cloud.json +10066 -0
- package/src/assets/lottie/where-lucy-works-mine-none.json +2478 -0
- package/src/assets/lottie/where-lucy-works-mine-offline.json +2373 -0
- package/src/assets/lottie/where-lucy-works-mine-online.json +7881 -0
- package/src/assets/sounds/hours-back.m4a +0 -0
- package/src/assets/sounds/meet-lucy.m4a +0 -0
- package/src/assets/sounds/power-on.m4a +0 -0
- package/src/assets/sounds/wind-back.m4a +0 -0
- package/src/assets/sounds/youre-in.m4a +0 -0
- package/src/chat/.claude-flow/data/pending-insights.jsonl +1 -0
- package/src/chat/agent.ts +376 -0
- package/src/chat/appFront.ts +39 -0
- package/src/chat/auth.ts +433 -0
- package/src/chat/chores.ts +394 -0
- package/src/chat/connectors.ts +65 -0
- package/src/chat/jobs.ts +228 -0
- package/src/chat/lucyMotion.ts +33 -0
- package/src/chat/protocol.ts +192 -0
- package/src/chat/results.ts +224 -0
- package/src/chat/store.ts +87 -0
- package/src/chat/useDictation.ts +201 -0
- package/src/chat/useLucyChat.ts +458 -0
- package/src/chat/useMyComputer.ts +41 -0
- package/src/chat/whereLucyWorks.ts +97 -0
- package/src/chat/workspace.ts +127 -0
- package/src/components/.claude-flow/data/pending-insights.jsonl +3 -0
- package/src/components/AreaShot.tsx +239 -0
- package/src/components/Backdrops.tsx +323 -0
- package/src/components/BottomSheet.tsx +96 -0
- package/src/components/ChatSheets.tsx +565 -0
- package/src/components/ConnectorLogo.tsx +116 -0
- package/src/components/CustomDrawer.tsx +526 -0
- package/src/components/IdeasPane.tsx +318 -0
- package/src/components/ImessageSheet.tsx +280 -0
- package/src/components/KeyboardAvoider.tsx +45 -0
- package/src/components/Logo.tsx +97 -0
- package/src/components/LucyWorking.tsx +122 -0
- package/src/components/Markdown.tsx +219 -0
- package/src/components/Media.tsx +173 -0
- package/src/components/MessagesAppIcon.tsx +51 -0
- package/src/components/MicButton.tsx +92 -0
- package/src/components/ResultCards.tsx +1067 -0
- package/src/components/SignInPrompt.tsx +182 -0
- package/src/components/SignedOut.tsx +47 -0
- package/src/components/TalkToLucyHero.tsx +540 -0
- package/src/components/TalkToLucySheet.tsx +208 -0
- package/src/components/VoiceOrb.tsx +364 -0
- package/src/components/WhereLucyWorks.tsx +261 -0
- package/src/components/WorkspaceSection.tsx +307 -0
- package/src/components/YantraMark.tsx +30 -0
- package/src/components/drawerHeader.tsx +46 -0
- package/src/components/kit.tsx +309 -0
- package/src/components/lottieInstalled.ts +7 -0
- package/src/components/ui.tsx +700 -0
- package/src/components/useSignOut.ts +21 -0
- package/src/components/viewport.tsx +166 -0
- package/src/components/voiceDial.ts +45 -0
- package/src/compute.ts +168 -0
- package/src/config/env-config.ts +37 -0
- package/src/config/routes.ts +28 -0
- package/src/content.ts +217 -0
- package/src/hooks/keyboardCover.ts +70 -0
- package/src/hooks/useKeyboard.ts +135 -0
- package/src/hooks/useReducedMotion.ts +19 -0
- package/src/imessage/openMessages.ts +53 -0
- package/src/imessage/platform.ts +32 -0
- package/src/imessage/textLink.ts +307 -0
- package/src/index.ts +24 -0
- package/src/live/card.ts +62 -0
- package/src/live/liveActivity.ts +192 -0
- package/src/module.ts +8 -0
- package/src/offer/BlackFridayOffer.tsx +394 -0
- package/src/offer/blackFriday.ts +109 -0
- package/src/offer/parts.tsx +650 -0
- package/src/offer/state.ts +78 -0
- package/src/push/push.ts +171 -0
- package/src/push/usePushNotifications.ts +47 -0
- package/src/screens/Account/index.tsx +310 -0
- package/src/screens/Chat/.claude-flow/data/pending-insights.jsonl +4 -0
- package/src/screens/Chat/index.tsx +891 -0
- package/src/screens/Chores/index.tsx +35 -0
- package/src/screens/Connectors/index.tsx +35 -0
- package/src/screens/History/index.tsx +346 -0
- package/src/screens/Home/index.tsx +2773 -0
- package/src/screens/Home/media.ts +31 -0
- package/src/screens/Welcome/index.tsx +1182 -0
- package/src/talk/talkToLucy.ts +187 -0
- package/src/theme/colors.ts +38 -0
- package/src/theme/fonts.ts +30 -0
- package/src/theme/muse.ts +86 -0
- package/src/utils/links.ts +84 -0
- package/src/utils/yantraLinks.ts +94 -0
- package/src/welcome/flow.ts +418 -0
- package/src/welcome/haptics.ts +179 -0
- package/src/welcome/parts.tsx +2160 -0
- package/src/welcome/powerOn.ts +84 -0
- package/src/welcome/seen.ts +68 -0
- package/src/welcome/sound.ts +173 -0
- package/tools/lottie/chat.motion +0 -0
- package/tools/lottie/cloud.motion +0 -0
- package/tools/lottie/generate.mjs +1209 -0
- package/tools/lottie/inter-glyphs.json +4416 -0
- package/tools/lottie/lucy-mark-calendar.motion +0 -0
- package/tools/lottie/lucy-mark-call.motion +0 -0
- package/tools/lottie/lucy-mark-connect.motion +0 -0
- package/tools/lottie/lucy-mark-details.motion +0 -0
- package/tools/lottie/lucy-mark-email.motion +0 -0
- package/tools/lottie/lucy-mark-message.motion +0 -0
- package/tools/lottie/lucy-mark-search.motion +0 -0
- package/tools/lottie/lucy-mark-think.motion +0 -0
- package/tools/lottie/lucy-mark-write.motion +0 -0
- package/tools/lottie/mark.json +1 -0
- package/tools/lottie/mine-none.motion +0 -0
- package/tools/lottie/mine-offline.motion +0 -0
- package/tools/lottie/mine-online.motion +0 -0
- package/tools/lottie/time-back.mjs +604 -0
- package/tools/sounds/render.mjs +730 -0
- package/tsconfig.json +17 -0
- package/README.md +0 -3
package/src/chat/auth.ts
ADDED
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The token Lucy's agent takes. The agent only accepts a CDE-minted "cdecli" JWT (it carries the org, project and
|
|
3
|
+
* environment the agent works in), never the Auth0 token itself. The app mints one against the backend the agent
|
|
4
|
+
* trusts: the signed-in person's Auth0 id_token as a Bearer, sent the way the platform's mobile Apollo link sends it
|
|
5
|
+
* (`ismobile` + `authstrategy` headers - without them the backend's JWT check answers 401), then default organization
|
|
6
|
+
* -> first project -> first environment -> obtainUserSystemToken.
|
|
7
|
+
*
|
|
8
|
+
* The minted token is kept in SecureStore per Auth0 user and reused until a day before it expires.
|
|
9
|
+
*
|
|
10
|
+
* A locked phone shuts the keychain (SecureStore's items open only while it is unlocked): a read then fails, which is
|
|
11
|
+
* not a sign-out. While it is shut the tokens last read here stand in (the person cannot sign out while the phone is
|
|
12
|
+
* locked); with none, the error says `locked`, and the chat waits for the phone to be unlocked (see untilUnlocked).
|
|
13
|
+
*/
|
|
14
|
+
import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
15
|
+
import * as SecureStore from 'expo-secure-store';
|
|
16
|
+
import { Platform } from 'react-native';
|
|
17
|
+
import { config } from '../config/env-config';
|
|
18
|
+
import { inFront, whenInFront } from './appFront';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Where user-auth0-mobile keeps the Auth0 tokens: SecureStore on a phone, AsyncStorage otherwise. The id_token is
|
|
22
|
+
* the one the backend accepts from a mobile client; the access_token is the fallback.
|
|
23
|
+
*/
|
|
24
|
+
const AUTH0_TOKEN_KEYS = ['id_token', 'access_token'] as const;
|
|
25
|
+
/** common HttpHeaders.IS_MOBILE / AUTH_STRATEGY, and AuthStrategy.Auth0. */
|
|
26
|
+
const MOBILE_AUTH_HEADERS = { ismobile: 'true', authstrategy: 'auth0' } as const;
|
|
27
|
+
const TOKEN_KEY = 'clockbook.lucy.token';
|
|
28
|
+
const RENEW_BEFORE_MS = 24 * 60 * 60 * 1000;
|
|
29
|
+
const YEAR_SECONDS = 31536000;
|
|
30
|
+
|
|
31
|
+
export class ChatAuthError extends Error {
|
|
32
|
+
/**
|
|
33
|
+
* signed-out: no Auth0 session. locked: the phone is locked, its keychain shut (the session is there, it opens
|
|
34
|
+
* again once the phone is unlocked). rejected: Yantra refused it. no-workspace: nothing to mint in.
|
|
35
|
+
*/
|
|
36
|
+
readonly kind: 'signed-out' | 'locked' | 'rejected' | 'no-workspace' | 'failed';
|
|
37
|
+
|
|
38
|
+
constructor(kind: ChatAuthError['kind'], message: string) {
|
|
39
|
+
super(message);
|
|
40
|
+
this.name = 'ChatAuthError';
|
|
41
|
+
this.kind = kind;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function agentUrl(): string {
|
|
46
|
+
return String(config.CDECLI_AGENT_URL).replace(/\/+$/, '');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** What the agent says about where it runs: GET /v1/auth/context. */
|
|
50
|
+
interface AgentContext {
|
|
51
|
+
backendGraphqlUrl: string;
|
|
52
|
+
/** The Yantra web app, when the agent names it. */
|
|
53
|
+
appUrl?: string;
|
|
54
|
+
frontendUrl?: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
let context: Promise<AgentContext> | null = null;
|
|
58
|
+
function agentContext(): Promise<AgentContext> {
|
|
59
|
+
if (!context) {
|
|
60
|
+
context = fetch(`${agentUrl()}/v1/auth/context`)
|
|
61
|
+
.then((r) => (r.ok ? r.json() : Promise.reject(new Error(`HTTP ${r.status}`))))
|
|
62
|
+
.then((j: AgentContext) => {
|
|
63
|
+
if (!j?.backendGraphqlUrl) throw new Error('the agent named no backend');
|
|
64
|
+
return j;
|
|
65
|
+
})
|
|
66
|
+
.catch((err) => {
|
|
67
|
+
context = null;
|
|
68
|
+
throw new ChatAuthError('failed', `Could not reach Lucy (${(err as Error).message})`);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
return context;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The Yantra GraphQL backend the agent validates tokens against: the one to mint on. */
|
|
75
|
+
export function agentBackendUrl(): Promise<string> {
|
|
76
|
+
return agentContext().then((c) => c.backendGraphqlUrl);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Where people open Yantra for each backend Lucy's agent can name. */
|
|
80
|
+
const APP_FOR_BACKEND: Record<string, string> = {
|
|
81
|
+
// Production.
|
|
82
|
+
'ideback.yantra.app': 'https://yantra.app',
|
|
83
|
+
// Staging.
|
|
84
|
+
'ideback.yantra-app-v1.cdebase.dev': 'https://idefront.yantra-app-v1.cdebase.dev',
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/** The Yantra web apps this build knows: production's and staging's. */
|
|
88
|
+
export const KNOWN_YANTRA_APPS: readonly string[] = Object.values(APP_FOR_BACKEND);
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The Yantra web app that goes with a backend: production's is yantra.app, staging's idefront; any other
|
|
92
|
+
* `ideback.<domain>` is served at `idefront.<domain>`. '' when the backend is not named that way.
|
|
93
|
+
*/
|
|
94
|
+
export function appUrlForBackend(backendGraphqlUrl: string): string {
|
|
95
|
+
try {
|
|
96
|
+
const u = new URL(backendGraphqlUrl);
|
|
97
|
+
const host = u.hostname.toLowerCase();
|
|
98
|
+
if (APP_FOR_BACKEND[host]) return APP_FOR_BACKEND[host];
|
|
99
|
+
if (!host.startsWith('ideback.')) return '';
|
|
100
|
+
return `${u.protocol}//${host.replace(/^ideback\./, 'idefront.')}${u.port ? `:${u.port}` : ''}`;
|
|
101
|
+
} catch {
|
|
102
|
+
return '';
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The Yantra web app on the same environment as Lucy's agent (the one her token is minted on), so what is connected
|
|
108
|
+
* there is what Lucy sees: the agent's own word for it, else the front end of its backend. '' when neither is known.
|
|
109
|
+
*/
|
|
110
|
+
export async function agentAppUrl(): Promise<string> {
|
|
111
|
+
const c = await agentContext();
|
|
112
|
+
const named = (c.appUrl || c.frontendUrl || '').trim().replace(/\/+$/, '');
|
|
113
|
+
return named || appUrlForBackend(c.backendGraphqlUrl);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function jwtClaims(token: string): { exp?: number; sub?: string; email?: string } {
|
|
117
|
+
try {
|
|
118
|
+
const part = token.split('.')[1] ?? '';
|
|
119
|
+
const json = globalThis.atob(
|
|
120
|
+
part
|
|
121
|
+
.replace(/-/g, '+')
|
|
122
|
+
.replace(/_/g, '/')
|
|
123
|
+
.padEnd(Math.ceil(part.length / 4) * 4, '='),
|
|
124
|
+
);
|
|
125
|
+
return JSON.parse(json);
|
|
126
|
+
} catch {
|
|
127
|
+
return {};
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The account a token belongs to: keys the chat's local history. */
|
|
132
|
+
export function accountKey(token: string): string {
|
|
133
|
+
const c = jwtClaims(token);
|
|
134
|
+
return String(c.sub || c.email || 'me');
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export async function gql<T>(url: string, bearer: string, query: string, variables?: object): Promise<T> {
|
|
138
|
+
let res: Response;
|
|
139
|
+
try {
|
|
140
|
+
res = await fetch(url, {
|
|
141
|
+
method: 'POST',
|
|
142
|
+
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${bearer}`, ...MOBILE_AUTH_HEADERS },
|
|
143
|
+
body: JSON.stringify({ query, variables }),
|
|
144
|
+
});
|
|
145
|
+
} catch (err) {
|
|
146
|
+
throw new ChatAuthError('failed', `Could not reach Yantra (${(err as Error).message})`);
|
|
147
|
+
}
|
|
148
|
+
if (res.status === 401 || res.status === 403)
|
|
149
|
+
throw new ChatAuthError('rejected', 'Yantra did not accept this sign-in');
|
|
150
|
+
const json = (await res.json().catch(() => ({}))) as { data?: T; errors?: { message: string }[] };
|
|
151
|
+
if (json.errors?.length) {
|
|
152
|
+
const msg = json.errors.map((e) => e.message).join('; ');
|
|
153
|
+
if (/unauth|not authenticated|jwt|token/i.test(msg)) throw new ChatAuthError('rejected', msg);
|
|
154
|
+
throw new Error(msg);
|
|
155
|
+
}
|
|
156
|
+
return json.data as T;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** The account settings resource (as Yantra's settingsUriForAccount): where the person's defaults live. */
|
|
160
|
+
export function accountSettingsUri(userId: string) {
|
|
161
|
+
return { scheme: 'cdecode', authority: 'default', path: `//accounts/${userId}`, fragment: 'settings' };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** The backend's own id for the signed-in person (the settings resource is keyed by it). */
|
|
165
|
+
export async function currentUserId(url: string, bearer: string): Promise<string | undefined> {
|
|
166
|
+
const d = await gql<{ currentUser?: { id?: string } }>(url, bearer, 'query { currentUser { id } }').catch((err) => {
|
|
167
|
+
if (err instanceof ChatAuthError) throw err;
|
|
168
|
+
return null;
|
|
169
|
+
});
|
|
170
|
+
return d?.currentUser?.id ?? undefined;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** The workspace saved as the person's default (Yantra's Account > Workspace): `account.default.*`. */
|
|
174
|
+
export interface WorkspaceDefaults {
|
|
175
|
+
organization?: string;
|
|
176
|
+
/** The project's public UUID (its `projectId`). */
|
|
177
|
+
project?: string;
|
|
178
|
+
/** An environment tag id; empty means Production. */
|
|
179
|
+
environment?: string;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export async function readWorkspaceDefaults(url: string, bearer: string): Promise<WorkspaceDefaults> {
|
|
183
|
+
const userId = await currentUserId(url, bearer);
|
|
184
|
+
const resourceUri = userId
|
|
185
|
+
? accountSettingsUri(userId)
|
|
186
|
+
: { scheme: 'cdecode', authority: 'default', path: '/', fragment: 'settings' };
|
|
187
|
+
try {
|
|
188
|
+
const d = await gql<{ pageSettings?: { settings?: unknown } | null }>(
|
|
189
|
+
url,
|
|
190
|
+
bearer,
|
|
191
|
+
'query PageSettings($resourceUri: URIInput!, $options: PageSettingsOptionsInput) { pageSettings(resourceUri: $resourceUri, options: $options) { settings } }',
|
|
192
|
+
{ resourceUri, options: { configKey: 'account.default' } },
|
|
193
|
+
);
|
|
194
|
+
// With a configKey the server answers with that subtree itself (platform-client useSettings reads it so).
|
|
195
|
+
let value = d?.pageSettings?.settings;
|
|
196
|
+
if (typeof value === 'string') value = JSON.parse(value);
|
|
197
|
+
const v = (value ?? {}) as Record<string, unknown>;
|
|
198
|
+
const pick = (k: string) => {
|
|
199
|
+
const x = v[k] ?? v[`account.default.${k}`];
|
|
200
|
+
return typeof x === 'string' && x.trim() ? x.trim() : undefined;
|
|
201
|
+
};
|
|
202
|
+
return { organization: pick('organization'), project: pick('project'), environment: pick('environment') };
|
|
203
|
+
} catch (err) {
|
|
204
|
+
if (err instanceof ChatAuthError) throw err;
|
|
205
|
+
return {};
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** Production's root tag, as the settings name it ('' means it too). */
|
|
210
|
+
const PRODUCTION_TAG_ID = 'aa0e8400e29b41d4a7164464';
|
|
211
|
+
|
|
212
|
+
async function mint(auth0Token: string): Promise<string> {
|
|
213
|
+
const url = await agentBackendUrl();
|
|
214
|
+
const saved = await readWorkspaceDefaults(url, auth0Token);
|
|
215
|
+
let orgName = saved.organization;
|
|
216
|
+
if (!orgName) {
|
|
217
|
+
// No saved default: the person's first organization, as the web sign-in picks.
|
|
218
|
+
const o = await gql<{ getUserOrganizations?: { name: string }[] }>(
|
|
219
|
+
url,
|
|
220
|
+
auth0Token,
|
|
221
|
+
'query { getUserOrganizations { name } }',
|
|
222
|
+
).catch(() => null);
|
|
223
|
+
orgName = o?.getUserOrganizations?.[0]?.name;
|
|
224
|
+
}
|
|
225
|
+
if (!orgName) throw new ChatAuthError('no-workspace', 'Your account has no workspace for Lucy yet');
|
|
226
|
+
const p = await gql<{ getProjects?: { data?: { id: string; projectId?: string }[] } }>(
|
|
227
|
+
url,
|
|
228
|
+
auth0Token,
|
|
229
|
+
'query GetProjects($orgName: String!) { getProjects(orgName: $orgName) { data { id projectId } } }',
|
|
230
|
+
{ orgName },
|
|
231
|
+
);
|
|
232
|
+
const projects = p.getProjects?.data ?? [];
|
|
233
|
+
// The saved project when it is in this organization, else its first.
|
|
234
|
+
const project =
|
|
235
|
+
(orgName === saved.organization && projects.find((x) => x.projectId && x.projectId === saved.project)) ||
|
|
236
|
+
projects[0];
|
|
237
|
+
const projectId = project?.id;
|
|
238
|
+
if (!projectId) throw new ChatAuthError('no-workspace', 'Your workspace has no project for Lucy yet');
|
|
239
|
+
const e = await gql<{ getEnvironmentTags?: { id: string }[] }>(
|
|
240
|
+
url,
|
|
241
|
+
auth0Token,
|
|
242
|
+
'query GetEnvironmentTags($projectId: ID) { getEnvironmentTags(projectId: $projectId) { id } }',
|
|
243
|
+
{ projectId },
|
|
244
|
+
);
|
|
245
|
+
const tags = e.getEnvironmentTags ?? [];
|
|
246
|
+
const wanted = saved.environment && saved.environment !== PRODUCTION_TAG_ID ? saved.environment : undefined;
|
|
247
|
+
const environmentTagId = (wanted && tags.find((t) => t.id === wanted)?.id) || tags[0]?.id;
|
|
248
|
+
if (!environmentTagId) throw new ChatAuthError('no-workspace', 'Your project has no environment for Lucy yet');
|
|
249
|
+
const m = await gql<{ obtainUserSystemToken?: { token?: string } }>(
|
|
250
|
+
url,
|
|
251
|
+
auth0Token,
|
|
252
|
+
'mutation ObtainUserSystemToken($input: ObtainUserSystemTokenInput!) { obtainUserSystemToken(input: $input) { token } }',
|
|
253
|
+
{ input: { orgName, projectId, environmentTagId, expiryInSeconds: YEAR_SECONDS } },
|
|
254
|
+
);
|
|
255
|
+
const token = m.obtainUserSystemToken?.token;
|
|
256
|
+
if (!token) throw new ChatAuthError('failed', 'Yantra returned no token for Lucy');
|
|
257
|
+
return token;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** What a stored item read gave: the value, and whether the keychain was shut (a locked phone) so it could not say. */
|
|
261
|
+
interface Stored {
|
|
262
|
+
value: string | null;
|
|
263
|
+
shut: boolean;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
async function readStored(key: string): Promise<Stored> {
|
|
267
|
+
let raw: string | null = null;
|
|
268
|
+
let shut = false;
|
|
269
|
+
if (Platform.OS !== 'web') {
|
|
270
|
+
try {
|
|
271
|
+
raw = await SecureStore.getItemAsync(key);
|
|
272
|
+
} catch {
|
|
273
|
+
// iOS answers "User interaction is not allowed" while the phone is locked.
|
|
274
|
+
shut = true;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
if (!raw) raw = await AsyncStorage.getItem(key).catch(() => null);
|
|
278
|
+
return { value: raw && raw !== 'undefined' && raw !== 'null' ? raw : null, shut };
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** A stored token may be saved JSON-encoded (with its quotes): the bare JWT either way. */
|
|
282
|
+
export function unwrapToken(raw: string): string | null {
|
|
283
|
+
try {
|
|
284
|
+
const parsed: unknown = JSON.parse(raw);
|
|
285
|
+
if (typeof parsed === 'string') return parsed.trim() || null;
|
|
286
|
+
} catch {
|
|
287
|
+
// not JSON: the token itself
|
|
288
|
+
}
|
|
289
|
+
return raw.trim() || null;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const lockedError = () => new ChatAuthError('locked', 'Lucy carries on once the phone is unlocked');
|
|
293
|
+
|
|
294
|
+
/** The Auth0 token last read from the keychain: stands in while a locked phone keeps it shut. */
|
|
295
|
+
let lastAuth0: string | null = null;
|
|
296
|
+
|
|
297
|
+
/** The Auth0 token, and whether it is the one last read (the keychain shut) rather than the stored one. */
|
|
298
|
+
async function readAuth0(): Promise<{ token: string; remembered: boolean }> {
|
|
299
|
+
let shut = false;
|
|
300
|
+
for (const key of AUTH0_TOKEN_KEYS) {
|
|
301
|
+
const stored = await readStored(key);
|
|
302
|
+
shut = shut || stored.shut;
|
|
303
|
+
const token = stored.value ? unwrapToken(stored.value) : null;
|
|
304
|
+
if (token) {
|
|
305
|
+
lastAuth0 = token;
|
|
306
|
+
return { token, remembered: false };
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
if (shut) {
|
|
310
|
+
if (lastAuth0) return { token: lastAuth0, remembered: true };
|
|
311
|
+
throw lockedError();
|
|
312
|
+
}
|
|
313
|
+
lastAuth0 = null;
|
|
314
|
+
throw new ChatAuthError('signed-out', 'Sign in to chat with Lucy');
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
export async function auth0Token(): Promise<string> {
|
|
318
|
+
return (await readAuth0()).token;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
interface SavedChatToken {
|
|
322
|
+
token: string;
|
|
323
|
+
owner: string;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** The minted token last read or saved here: stands in while a locked phone keeps the keychain shut. */
|
|
327
|
+
let lastChat: SavedChatToken | null = null;
|
|
328
|
+
|
|
329
|
+
async function readChatToken(): Promise<SavedChatToken | null> {
|
|
330
|
+
let raw: string | null;
|
|
331
|
+
try {
|
|
332
|
+
raw = await SecureStore.getItemAsync(TOKEN_KEY);
|
|
333
|
+
} catch {
|
|
334
|
+
return lastChat;
|
|
335
|
+
}
|
|
336
|
+
if (!raw) return null;
|
|
337
|
+
try {
|
|
338
|
+
const saved = JSON.parse(raw) as SavedChatToken;
|
|
339
|
+
lastChat = saved;
|
|
340
|
+
return saved;
|
|
341
|
+
} catch {
|
|
342
|
+
return null;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
let inflight: Promise<string> | null = null;
|
|
347
|
+
|
|
348
|
+
/** A usable token for the agent: the stored one while it has a day left, otherwise a fresh mint. */
|
|
349
|
+
export function getChatToken({ fresh = false }: { fresh?: boolean } = {}): Promise<string> {
|
|
350
|
+
if (inflight) return inflight;
|
|
351
|
+
inflight = (async () => {
|
|
352
|
+
const a0 = await readAuth0();
|
|
353
|
+
const owner = jwtClaims(a0.token).sub ?? '';
|
|
354
|
+
if (!fresh) {
|
|
355
|
+
const saved = await readChatToken();
|
|
356
|
+
const exp = (jwtClaims(saved?.token ?? '').exp ?? 0) * 1000;
|
|
357
|
+
if (saved && saved.owner === owner && exp - Date.now() > RENEW_BEFORE_MS) return saved.token;
|
|
358
|
+
}
|
|
359
|
+
// The Auth0 token last read may have been renewed (and this one lapsed) since: mint with the stored one, once
|
|
360
|
+
// the phone is unlocked.
|
|
361
|
+
if (a0.remembered) throw lockedError();
|
|
362
|
+
const token = await mint(a0.token);
|
|
363
|
+
lastChat = { token, owner };
|
|
364
|
+
await SecureStore.setItemAsync(TOKEN_KEY, JSON.stringify(lastChat)).catch(() => undefined);
|
|
365
|
+
return token;
|
|
366
|
+
})().finally(() => {
|
|
367
|
+
inflight = null;
|
|
368
|
+
});
|
|
369
|
+
return inflight;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** Forget the minted token (sign-out, or the agent refused it). */
|
|
373
|
+
export async function clearChatToken(): Promise<void> {
|
|
374
|
+
lastChat = null;
|
|
375
|
+
await SecureStore.deleteItemAsync(TOKEN_KEY).catch(() => undefined);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/** The phone is locked: what failed needs the keychain, and runs once the phone is unlocked. */
|
|
379
|
+
export function isLockedOut(err: unknown): boolean {
|
|
380
|
+
return err instanceof ChatAuthError && err.kind === 'locked';
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** How often a keychain still shut with the app in front (it opens a moment after the unlock) is read again. */
|
|
384
|
+
const SHUT_IN_FRONT_TRIES = 3;
|
|
385
|
+
const SHUT_IN_FRONT_PAUSE_MS = 1_000;
|
|
386
|
+
|
|
387
|
+
function pause(ms: number, signal: AbortSignal | undefined, stopped: () => Error): Promise<void> {
|
|
388
|
+
return new Promise<void>((resolve, reject) => {
|
|
389
|
+
const onAbort = () => reject(stopped());
|
|
390
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
391
|
+
setTimeout(() => {
|
|
392
|
+
signal?.removeEventListener('abort', onAbort);
|
|
393
|
+
resolve();
|
|
394
|
+
}, ms);
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Runs `step`; while the phone is locked (it needs the keychain), waits for the app to be in front again and runs it
|
|
400
|
+
* once more. Anything else it throws goes through. Rejects with `stopped()` when `signal` ends the wait.
|
|
401
|
+
*/
|
|
402
|
+
export async function untilUnlocked<T>(
|
|
403
|
+
step: () => Promise<T>,
|
|
404
|
+
signal?: AbortSignal,
|
|
405
|
+
stopped: () => Error = () => new Error('Stopped'),
|
|
406
|
+
): Promise<T> {
|
|
407
|
+
for (let shutInFront = 0; ; ) {
|
|
408
|
+
try {
|
|
409
|
+
return await step();
|
|
410
|
+
} catch (err) {
|
|
411
|
+
if (!isLockedOut(err)) throw err;
|
|
412
|
+
if (signal?.aborted) throw stopped();
|
|
413
|
+
if (!inFront()) await whenInFront(signal, stopped);
|
|
414
|
+
else {
|
|
415
|
+
shutInFront += 1;
|
|
416
|
+
if (shutInFront > SHUT_IN_FRONT_TRIES) throw err;
|
|
417
|
+
await pause(SHUT_IN_FRONT_PAUSE_MS, signal, stopped);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
type Listener = () => void;
|
|
424
|
+
const workspaceListeners = new Set<Listener>();
|
|
425
|
+
/** Told when the person switches workspace, so an open chat signs in again for the new one. */
|
|
426
|
+
export function onWorkspaceChange(listener: Listener): () => void {
|
|
427
|
+
workspaceListeners.add(listener);
|
|
428
|
+
return () => workspaceListeners.delete(listener);
|
|
429
|
+
}
|
|
430
|
+
export async function workspaceChanged(): Promise<void> {
|
|
431
|
+
await clearChatToken();
|
|
432
|
+
workspaceListeners.forEach((l) => l());
|
|
433
|
+
}
|