@ -21,6 +21,26 @@ import { db } from "../../database/postgres";
import type { ConversationsId } from "../../database/generated/public/Conversations" ;
import type { ConversationsId } from "../../database/generated/public/Conversations" ;
import type { UsersId } from "../../database/generated/public/Users" ;
import type { UsersId } from "../../database/generated/public/Users" ;
const mainSystemPrompt = ( {
systemPrompt ,
previousRunningSummary ,
} : { systemPrompt : string ; previousRunningSummary : string } ) = > ` ${ systemPrompt }
This is a summary of the conversation so far , from your point - of - view ( so "I" and "me" refer to you ) :
< running_summary >
$ { previousRunningSummary }
< / running_summary >
` ;
const runningSummarySystemPrompt = ( {
previousRunningSummary ,
} : {
previousRunningSummary : string ;
} ) = > ` Given the following summary of a conversation, coupled with the messages exchanged since that summary was produced, produce a new summary of the conversation.
< running_summary >
$ { previousRunningSummary }
< / running_summary >
` ;
const openrouter = createOpenRouter ( {
const openrouter = createOpenRouter ( {
apiKey : env.OPENROUTER_API_KEY ,
apiKey : env.OPENROUTER_API_KEY ,
} ) ;
} ) ;
@ -145,13 +165,10 @@ export const chat = router({
? { role : "system" as const , content : systemPrompt }
? { role : "system" as const , content : systemPrompt }
: {
: {
role : "system" as const ,
role : "system" as const ,
content : ` ${ systemPrompt }
content : mainSystemPrompt ( {
systemPrompt ,
This is a summary of the conversation so far , from your point - of - view ( so "I" and "me" refer to you ) :
previousRunningSummary ,
< running_summary >
} ) ,
$ { previousRunningSummary }
< / running_summary >
` ,
} ,
} ,
. . . messages . slice ( previousRunningSummaryIndex + 1 ) ,
. . . messages . slice ( previousRunningSummaryIndex + 1 ) ,
] ,
] ,
@ -159,21 +176,6 @@ ${previousRunningSummary}
tools : undefined ,
tools : undefined ,
. . . parameters ,
. . . parameters ,
} ) ;
} ) ;
console . log ( "sent" , [
previousRunningSummary === ""
? { role : "system" as const , content : systemPrompt }
: {
role : "system" as const ,
content : ` ${ systemPrompt }
This is a summary of the conversation so far , from your point - of - view ( so "I" and "me" refer to you ) :
< running_summary >
$ { previousRunningSummary }
< / running_summary >
` ,
} ,
. . . messages . slice ( previousRunningSummaryIndex + 1 ) ,
] ) ;
/ * * E x t r a c t F a c t s f r o m t h e u s e r ' s m e s s a g e , a n d a d d t h e m t o t h e d a t a b a s e ,
/ * * E x t r a c t F a c t s f r o m t h e u s e r ' s m e s s a g e , a n d a d d t h e m t o t h e d a t a b a s e ,
* linking the Facts with the messages they came from . ( Yes , this should
* linking the Facts with the messages they came from . ( Yes , this should
* be done * after * the model response , not before ; because when we run a
* be done * after * the model response , not before ; because when we run a
@ -199,11 +201,9 @@ ${previousRunningSummary}
messages : [
messages : [
{
{
role : "system" as const ,
role : "system" as const ,
content : ` Given the following summary of a conversation, coupled with the messages exchanged since that summary was produced, produce a new summary of the conversation.
content : runningSummarySystemPrompt ( {
< running_summary >
previousRunningSummary ,
$ { previousRunningSummary }
} ) ,
< / running_summary >
` ,
} ,
} ,
. . . messages . slice ( previousRunningSummaryIndex + 1 ) ,
. . . messages . slice ( previousRunningSummaryIndex + 1 ) ,
{
{