|
| 1 | +<script lang="ts"> |
| 2 | + import ReduxResult from '$components/ReduxResult.svelte'; |
| 3 | + import { Ai2Service } from '$lib/ai2/service'; |
| 4 | + import { getContext } from '@gitbutler/shared/context'; |
| 5 | + import AsyncButton from '@gitbutler/ui/AsyncButton.svelte'; |
| 6 | + import Textarea from '@gitbutler/ui/Textarea.svelte'; |
| 7 | +
|
| 8 | + type Props = { |
| 9 | + projectId: string; |
| 10 | + }; |
| 11 | +
|
| 12 | + const { projectId }: Props = $props(); |
| 13 | +
|
| 14 | + const ai2Service = getContext(Ai2Service); |
| 15 | +
|
| 16 | + const isOpenRouterTokenSet = ai2Service.isOpenRouterTokenSet; |
| 17 | + const conversations = $derived(ai2Service.conversations({ projectId })); |
| 18 | + const [agentCreateConversation] = ai2Service.createConversation; |
| 19 | + const [agentSendMessage] = ai2Service.sendMessage; |
| 20 | +
|
| 21 | + let currentConversation = $state<string>(); |
| 22 | + let message = $state(''); |
| 23 | +
|
| 24 | + async function createConversation({ projectId }: { projectId: string }) { |
| 25 | + const id = await agentCreateConversation({ projectId }); |
| 26 | + currentConversation = id; |
| 27 | + } |
| 28 | +
|
| 29 | + async function sendMessage({ |
| 30 | + projectId, |
| 31 | + conversationId |
| 32 | + }: { |
| 33 | + projectId: string; |
| 34 | + conversationId: string; |
| 35 | + }) { |
| 36 | + await agentSendMessage({ projectId, conversationId, message }); |
| 37 | + message = ''; |
| 38 | + } |
| 39 | +</script> |
| 40 | + |
| 41 | +<div class="page"> |
| 42 | + <div class="sidebar"> |
| 43 | + {#if !isOpenRouterTokenSet.current.data} |
| 44 | + <p> |
| 45 | + The agent token is currently not set. Please configure it in the global experimental |
| 46 | + settings. |
| 47 | + </p> |
| 48 | + {/if} |
| 49 | + <div class="conversations"> |
| 50 | + <p class="text-13">Conversations</p> |
| 51 | + <ReduxResult {projectId} result={conversations.current}> |
| 52 | + {#snippet children(conversations, { projectId })} |
| 53 | + <ul> |
| 54 | + {#each Object.entries(conversations) as [id, _conversation] (id)} |
| 55 | + <!-- svelte-ignore a11y_click_events_have_key_events --> |
| 56 | + <!-- svelte-ignore a11y_no_noninteractive_element_interactions --> |
| 57 | + <li onclick={() => (currentConversation = id)}> |
| 58 | + {#if currentConversation === id} |
| 59 | + <span>🔥</span> |
| 60 | + {/if} |
| 61 | + {id} |
| 62 | + </li> |
| 63 | + {/each} |
| 64 | + </ul> |
| 65 | + <AsyncButton |
| 66 | + action={async () => { |
| 67 | + await createConversation({ projectId }); |
| 68 | + }}>Create new conversation</AsyncButton |
| 69 | + > |
| 70 | + {/snippet} |
| 71 | + </ReduxResult> |
| 72 | + </div> |
| 73 | + </div> |
| 74 | + |
| 75 | + <div class="main"> |
| 76 | + <div class="conversation"> |
| 77 | + <ReduxResult {projectId} result={conversations.current}> |
| 78 | + {#snippet children(conversations, { projectId })} |
| 79 | + {@const conversation = currentConversation |
| 80 | + ? conversations[currentConversation] |
| 81 | + : undefined} |
| 82 | + {#if conversation} |
| 83 | + <ul> |
| 84 | + {#each conversation as message} |
| 85 | + <li> |
| 86 | + <div class="message"> |
| 87 | + <p>Role: {message.role}</p> |
| 88 | + <pre>{message.content}</pre> |
| 89 | + </div> |
| 90 | + </li> |
| 91 | + {/each} |
| 92 | + </ul> |
| 93 | + <Textarea bind:value={message} placeholder="Could you help me with..."></Textarea> |
| 94 | + <AsyncButton |
| 95 | + action={async () => { |
| 96 | + await sendMessage({ projectId, conversationId: currentConversation! }); |
| 97 | + }}>Send</AsyncButton |
| 98 | + > |
| 99 | + {/if} |
| 100 | + {/snippet} |
| 101 | + </ReduxResult> |
| 102 | + </div> |
| 103 | + </div> |
| 104 | +</div> |
| 105 | + |
| 106 | +<style lang="postcss"> |
| 107 | + .page { |
| 108 | + display: flex; |
| 109 | +
|
| 110 | + width: 100%; |
| 111 | + height: 100%; |
| 112 | + gap: 1rem; |
| 113 | + } |
| 114 | +
|
| 115 | + .sidebar { |
| 116 | + width: 350px; |
| 117 | + height: 100%; |
| 118 | + padding: 1rem; |
| 119 | +
|
| 120 | + border: 1px solid var(--clr-border-2); |
| 121 | + border-radius: var(--radius-l); |
| 122 | + background-color: var(--clr-bg-1); |
| 123 | + } |
| 124 | +
|
| 125 | + .main { |
| 126 | + width: 100%; |
| 127 | + height: 100%; |
| 128 | + padding: 1rem; |
| 129 | +
|
| 130 | + border: 1px solid var(--clr-border-2); |
| 131 | + border-radius: var(--radius-l); |
| 132 | + background-color: var(--clr-bg-1); |
| 133 | + } |
| 134 | +
|
| 135 | + .conversation { |
| 136 | + height: 100%; |
| 137 | + overflow: auto; |
| 138 | + } |
| 139 | +
|
| 140 | + .message { |
| 141 | + padding: 0.5rem; |
| 142 | + border: 1px solid var(--clr-border-1); |
| 143 | + border-radius: 0.5rem; |
| 144 | + } |
| 145 | +</style> |
0 commit comments