@@ -8,13 +8,15 @@ import type { ChatMessage, ChatMessageStorage } from "@hypr/store";
88import { CustomChatTransport } from "../../chat/transport" ;
99import type { HyprUIMessage } from "../../chat/types" ;
1010import { useToolRegistry } from "../../contexts/tool" ;
11+ import { useSession } from "../../hooks/tinybase" ;
1112import { useLanguageModel } from "../../hooks/useLLMConnection" ;
1213import * as main from "../../store/tinybase/main" ;
1314import { id } from "../../utils" ;
1415
1516interface ChatSessionProps {
1617 sessionId : string ;
1718 chatGroupId ?: string ;
19+ attachedSessionId ?: string ;
1820 children : ( props : {
1921 messages : HyprUIMessage [ ] ;
2022 sendMessage : ( message : HyprUIMessage ) => void ;
@@ -28,9 +30,10 @@ interface ChatSessionProps {
2830export function ChatSession ( {
2931 sessionId,
3032 chatGroupId,
33+ attachedSessionId,
3134 children,
3235} : ChatSessionProps ) {
33- const transport = useTransport ( ) ;
36+ const transport = useTransport ( attachedSessionId ) ;
3437 const store = main . UI . useStore ( main . STORE_ID ) ;
3538
3639 const { user_id } = main . UI . useValues ( main . STORE_ID ) ;
@@ -161,22 +164,89 @@ export function ChatSession({
161164 ) ;
162165}
163166
164- function useTransport ( ) {
167+ function useTransport ( attachedSessionId ?: string ) {
165168 const registry = useToolRegistry ( ) ;
166169 const model = useLanguageModel ( ) ;
170+ const store = main . UI . useStore ( main . STORE_ID ) ;
167171 const language = main . UI . useValue ( "ai_language" , main . STORE_ID ) ?? "en" ;
168172 const [ systemPrompt , setSystemPrompt ] = useState < string | undefined > ( ) ;
169173
174+ const { title, rawMd, enhancedMd, createdAt } = useSession (
175+ attachedSessionId ?? "" ,
176+ ) ;
177+
178+ const transcriptIds = main . UI . useSliceRowIds (
179+ main . INDEXES . transcriptBySession ,
180+ attachedSessionId ?? "" ,
181+ main . STORE_ID ,
182+ ) ;
183+ const firstTranscriptId = transcriptIds ?. [ 0 ] ;
184+
185+ const wordIds = main . UI . useSliceRowIds (
186+ main . INDEXES . wordsByTranscript ,
187+ firstTranscriptId ?? "" ,
188+ main . STORE_ID ,
189+ ) ;
190+
191+ const words = useMemo ( ( ) => {
192+ if ( ! store || ! wordIds || wordIds . length === 0 ) {
193+ return [ ] ;
194+ }
195+
196+ const result : {
197+ text : string ;
198+ start_ms : number ;
199+ end_ms : number ;
200+ channel : number ;
201+ speaker ?: string ;
202+ } [ ] = [ ] ;
203+
204+ for ( const wordId of wordIds ) {
205+ const row = store . getRow ( "words" , wordId ) ;
206+ if ( row ) {
207+ result . push ( {
208+ text : row . text as string ,
209+ start_ms : row . start_ms as number ,
210+ end_ms : row . end_ms as number ,
211+ channel : row . channel as number ,
212+ speaker : row . speaker as string | undefined ,
213+ } ) ;
214+ }
215+ }
216+
217+ return result . sort ( ( a , b ) => a . start_ms - b . start_ms ) ;
218+ } , [ store , wordIds ] ) ;
219+
220+ const sessionContext = useMemo ( ( ) => {
221+ if ( ! attachedSessionId ) {
222+ return null ;
223+ }
224+
225+ return {
226+ session : true ,
227+ title : title as string | undefined ,
228+ rawContent : rawMd as string | undefined ,
229+ enhancedContent : enhancedMd as string | undefined ,
230+ date : createdAt as string | undefined ,
231+ words : words . length > 0 ? words : undefined ,
232+ } ;
233+ } , [ attachedSessionId , title , rawMd , enhancedMd , createdAt , words ] ) ;
234+
170235 useEffect ( ( ) => {
236+ const templateParams = {
237+ language,
238+ ...( sessionContext ?? { } ) ,
239+ } ;
240+
171241 templateCommands
172- . render ( "chat.system" , { language } )
242+ . render ( "chat.system" , templateParams )
173243 . then ( ( result ) => {
174244 if ( result . status === "ok" ) {
175245 setSystemPrompt ( result . data ) ;
176246 }
177247 } )
178248 . catch ( console . error ) ;
179- } , [ language ] ) ;
249+ } , [ language , sessionContext ] ) ;
180250
181251 const transport = useMemo ( ( ) => {
182252 if ( ! model ) {
0 commit comments