-
Notifications
You must be signed in to change notification settings - Fork 24
feat(kilo-app): add KiloClaw chat with Stream Chat #1674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iscekic
wants to merge
29
commits into
main
Choose a base branch
from
feature/kiloclaw-mobile-chat
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 15 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
f51cab9
feat(kilo-app): add stream-chat-react-native dependency
iscekic 6fc58ca
feat(kilo-app): add useStreamChatCredentials hook
iscekic 3fa5040
feat(kilo-app): add KiloClawChat component with Stream Chat integration
iscekic c7eb84d
feat(kilo-app): wire up chat screen with KiloClawChat component
iscekic a43076a
style(kilo-app): fix lint and formatting in chat files
iscekic 666c14b
fix(kilo-app): add null guard for bot membership lookup
iscekic 0f6f01b
revert: remove unnecessary null guard on bot membership lookup
iscekic 21cc160
Merge branch 'main' into feature/kiloclaw-mobile-chat
iscekic fccb79b
chore(kilo-app): update AGENTS.md and app.config.js
iscekic 9ef4187
fix(kilo-app): ignore netinfo peer dep in knip config
iscekic c841048
Merge remote-tracking branch 'origin/main' into feature/kiloclaw-mobi…
iscekic 6dfb83b
style(kilo-app): format app.config.js
iscekic 6c31de3
fix(kilo-app): add ascAppId for iOS App Store submission
iscekic 13b8576
fix(kilo-app): enable noUncheckedIndexedAccess and fix type errors
iscekic 83f8163
fix(kilo-app): fix connect/disconnect race and clear connectError on …
iscekic 86867e3
fix(kilo-app): restore disconnectUser in cleanup to tear down WS on u…
iscekic 0dec16c
chore(kilo-app): install stream-chat-react-native optional peer deps
iscekic d88d9ca
fix(kilo-app): pass empty config to react-native-share plugin
iscekic f90719d
chore(kilo-app): upgrade Expo SDK packages and exclude from minimumRe…
iscekic c87c135
fix(kilo-app): remove @stream-io/flat-list-mvcp (jcenter incompatible)
iscekic 77ff30a
fix(kilo-app): pin lightningcss to 1.31.1 to fix CSS bundling error
iscekic 113a244
fix: override lightningcss to 1.31.1 to fix react-native-css bundling
iscekic 59a9963
fix(kilo-app): upgrade react-native-css to 3.0.6 to fix lightningcss …
iscekic 237b674
fix: pin lightningcss to 1.30.1 to fix NativeWind CSS deserialization…
iscekic 22d4cc3
chore(kilo-app): upgrade non-Expo-managed deps to latest
iscekic 22d9d89
chore(kilo-app): upgrade @sentry/react-native from v7 to v8
iscekic ebc1c47
chore(kilo-app): suppress stream-chat console noise with LogBox
iscekic e752384
fix(kilo-app): upgrade @gorhom/bottom-sheet to fix getBoundingClientR…
iscekic cde24c7
revert(kilo-app): downgrade @sentry/react-native back to v7 for Expo …
iscekic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,9 @@ | |
| "production": { | ||
| "android": { | ||
| "track": "internal" | ||
| }, | ||
| "ios": { | ||
| "ascAppId": "6761193135" | ||
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 5 additions & 11 deletions
16
kilo-app/src/app/(app)/(tabs)/(1_kiloclaw)/[instance-id]/index.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,17 @@ | ||
| import { useLocalSearchParams } from 'expo-router'; | ||
| import { MessageSquare } from 'lucide-react-native'; | ||
| import { View } from 'react-native'; | ||
|
|
||
| import { EmptyState } from '@/components/empty-state'; | ||
| import { ScreenHeader } from '@/components/screen-header'; | ||
| import { KiloClawChat } from '@/components/kiloclaw/chat'; | ||
| import { useKiloClawStatus } from '@/lib/hooks/use-kiloclaw'; | ||
|
|
||
| export default function ChatScreen() { | ||
| const { 'instance-id': instanceId } = useLocalSearchParams<{ 'instance-id': string }>(); | ||
| const { data: status } = useKiloClawStatus(); | ||
| const isRunning = status?.status === 'running'; | ||
|
|
||
| return ( | ||
| <View className="flex-1 bg-background"> | ||
| <ScreenHeader title="Chat" /> | ||
| <View className="flex-1 items-center justify-center px-6"> | ||
| <EmptyState | ||
| icon={MessageSquare} | ||
| title="Chat coming soon" | ||
| description={`Instance: ${instanceId}`} | ||
| /> | ||
| </View> | ||
| <KiloClawChat instanceId={instanceId} enabled={isRunning} /> | ||
| </View> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,267 @@ | ||
| import { useCallback, useEffect, useState } from 'react'; | ||
| import { ActivityIndicator, Pressable, View } from 'react-native'; | ||
| import { useQueryClient } from '@tanstack/react-query'; | ||
| import { type Href, useRouter } from 'expo-router'; | ||
| import { Settings } from 'lucide-react-native'; | ||
| import { type Event, type Channel as StreamChannel, StreamChat } from 'stream-chat'; | ||
| import { | ||
| Channel, | ||
| Chat, | ||
| MessageInput, | ||
| MessageList, | ||
| OverlayProvider, | ||
| } from 'stream-chat-react-native'; | ||
|
|
||
| import { ScreenHeader } from '@/components/screen-header'; | ||
| import { Text } from '@/components/ui/text'; | ||
| import { useStreamChatCredentials } from '@/lib/hooks/use-kiloclaw'; | ||
| import { useThemeColors } from '@/lib/hooks/use-theme-colors'; | ||
| import { useTRPC } from '@/lib/trpc'; | ||
|
|
||
| type KiloClawChatProps = { | ||
| instanceId: string; | ||
| enabled: boolean; | ||
| }; | ||
|
|
||
| export function KiloClawChat({ instanceId, enabled }: Readonly<KiloClawChatProps>) { | ||
| const { data: creds, isLoading, error } = useStreamChatCredentials(enabled); | ||
|
|
||
| if (!enabled) { | ||
| return ( | ||
| <ChatShell instanceId={instanceId}> | ||
| <ChatPlaceholder message="Chat is available when the machine is running." /> | ||
| </ChatShell> | ||
| ); | ||
| } | ||
|
|
||
| if (isLoading) { | ||
| return ( | ||
| <ChatShell instanceId={instanceId}> | ||
| <View className="flex-1 items-center justify-center"> | ||
| <ActivityIndicator /> | ||
| </View> | ||
| </ChatShell> | ||
| ); | ||
| } | ||
|
|
||
| if (error) { | ||
| return ( | ||
| <ChatShell instanceId={instanceId}> | ||
| <ChatPlaceholder message="Failed to load chat. Please try again." /> | ||
| </ChatShell> | ||
| ); | ||
| } | ||
|
|
||
| if (!creds) { | ||
| return ( | ||
| <ChatShell instanceId={instanceId}> | ||
| <ChatPlaceholder message="Chat requires an upgrade. Use 'Upgrade to Latest' on the dashboard." /> | ||
| </ChatShell> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <StreamChatUI | ||
| instanceId={instanceId} | ||
| apiKey={creds.apiKey} | ||
| userId={creds.userId} | ||
| channelId={creds.channelId} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| // ─── Internal components ──────────────────────────────────────────────────── | ||
|
|
||
| function ChatShell({ instanceId, children }: { instanceId: string; children: React.ReactNode }) { | ||
| return ( | ||
| <View className="flex-1 bg-background"> | ||
| <ChatHeader instanceId={instanceId} /> | ||
| {children} | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| function ChatHeader({ instanceId, botOnline }: { instanceId: string; botOnline?: boolean }) { | ||
| const router = useRouter(); | ||
| const colors = useThemeColors(); | ||
|
|
||
| const settingsButton = ( | ||
| <Pressable | ||
| onPress={() => { | ||
| router.push(`/(app)/(tabs)/(1_kiloclaw)/${instanceId}/dashboard` as Href); | ||
| }} | ||
| hitSlop={12} | ||
| accessibilityLabel="Settings" | ||
| className="active:opacity-70" | ||
| > | ||
| <Settings size={20} color={colors.foreground} /> | ||
| </Pressable> | ||
| ); | ||
|
|
||
| return ( | ||
| <ScreenHeader | ||
| title="Chat" | ||
| headerRight={ | ||
| <View className="flex-row items-center gap-3"> | ||
| {botOnline !== undefined && <BotStatusIndicator online={botOnline} />} | ||
| {settingsButton} | ||
| </View> | ||
| } | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| function BotStatusIndicator({ online }: { online: boolean }) { | ||
| return ( | ||
| <View className="flex-row items-center gap-1.5"> | ||
| <View className={`h-2 w-2 rounded-full ${online ? 'bg-emerald-400' : 'bg-neutral-500'}`} /> | ||
| <Text className="text-xs text-muted-foreground">{online ? 'Online' : 'Offline'}</Text> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| function StreamChatUI({ | ||
| instanceId, | ||
| apiKey, | ||
| userId, | ||
| channelId, | ||
| }: { | ||
| instanceId: string; | ||
| apiKey: string; | ||
| userId: string; | ||
| channelId: string; | ||
| }) { | ||
| const trpc = useTRPC(); | ||
| const queryClient = useQueryClient(); | ||
|
|
||
| // Stable token provider — stream-chat calls this when the current token expires. | ||
| const tokenProvider = useCallback(async () => { | ||
| const creds = await queryClient.fetchQuery( | ||
| trpc.kiloclaw.getStreamChatCredentials.queryOptions(undefined, { | ||
| staleTime: 0, | ||
| }) | ||
| ); | ||
| if (!creds?.userToken) { | ||
| throw new Error('Failed to fetch Stream Chat credentials'); | ||
| } | ||
| return creds.userToken; | ||
| }, [queryClient, trpc]); | ||
|
|
||
| const [client, setClient] = useState<StreamChat | null>(null); | ||
| const [channel, setChannel] = useState<StreamChannel | null>(null); | ||
| const [connectError, setConnectError] = useState<string | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| const chatClient = StreamChat.getInstance(apiKey); | ||
|
|
||
| let cancelled = false; | ||
| setConnectError(null); | ||
|
|
||
| const connect = async () => { | ||
| try { | ||
| // Await disconnect to prevent tokenManager.reset() from racing with the new connection | ||
| if (chatClient.userID) { | ||
| await chatClient.disconnectUser(); | ||
| } | ||
| if (cancelled) { | ||
| return; | ||
| } | ||
| await chatClient.connectUser({ id: userId }, tokenProvider); | ||
| const ch = chatClient.channel('messaging', channelId); | ||
| await ch.watch({ presence: true }); | ||
| // cancelled may change across awaits above | ||
| if (!cancelled) { | ||
|
Check failure on line 173 in kilo-app/src/components/kiloclaw/chat.tsx
|
||
| // eslint-disable-line typescript-eslint/no-unnecessary-condition | ||
| setClient(chatClient); | ||
| setChannel(ch); | ||
| } | ||
| } catch (error) { | ||
| if (!cancelled) { | ||
| setConnectError(error instanceof Error ? error.message : 'Failed to connect to chat.'); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| void connect(); | ||
|
|
||
| return () => { | ||
| cancelled = true; | ||
| setClient(null); | ||
| setChannel(null); | ||
| }; | ||
| }, [apiKey, userId, channelId, tokenProvider]); | ||
|
|
||
| // Bot presence tracking | ||
| const sandboxId = channelId.replace(/^default-/, ''); | ||
| const botUserId = `bot-${sandboxId}`; | ||
| const botOnline = useBotOnlineStatus(client, channel, botUserId); | ||
|
|
||
| if (connectError) { | ||
| return ( | ||
| <ChatShell instanceId={instanceId}> | ||
| <ChatPlaceholder message={connectError} /> | ||
| </ChatShell> | ||
| ); | ||
| } | ||
|
|
||
| if (!client || !channel) { | ||
| return ( | ||
| <ChatShell instanceId={instanceId}> | ||
| <View className="flex-1 items-center justify-center"> | ||
| <ActivityIndicator /> | ||
| </View> | ||
| </ChatShell> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <View className="flex-1 bg-background"> | ||
| <ChatHeader instanceId={instanceId} botOnline={botOnline} /> | ||
| <OverlayProvider> | ||
| <Chat client={client}> | ||
| <Channel channel={channel}> | ||
| <MessageList /> | ||
| <MessageInput /> | ||
| </Channel> | ||
| </Chat> | ||
| </OverlayProvider> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| function useBotOnlineStatus( | ||
| client: StreamChat | null, | ||
| channel: StreamChannel | null, | ||
| botUserId: string | ||
| ): boolean { | ||
| const [online, setOnline] = useState(false); | ||
|
|
||
| useEffect(() => { | ||
| const handlePresenceChange = (event: Event) => { | ||
| if (event.user?.id === botUserId) { | ||
| setOnline(Boolean(event.user.online)); | ||
| } | ||
| }; | ||
|
|
||
| if (client && channel) { | ||
| // Check initial state | ||
| const member = channel.state.members[botUserId]; | ||
| setOnline(Boolean(member?.user?.online)); | ||
| client.on('user.presence.changed', handlePresenceChange); | ||
| } | ||
|
|
||
| return () => { | ||
| client?.off('user.presence.changed', handlePresenceChange); | ||
| }; | ||
| }, [client, channel, botUserId]); | ||
|
|
||
| return online; | ||
| } | ||
|
|
||
| function ChatPlaceholder({ message }: { message: string }) { | ||
| return ( | ||
| <View className="flex-1 items-center justify-center px-6"> | ||
| <Text className="text-sm text-muted-foreground text-center">{message}</Text> | ||
| </View> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.