1
+ import { ConvoStackBackendExpress } from "convostack/backend-express" ;
2
+ import express from "express" ;
3
+ import { StorageEnginePrismaSQLite } from "convostack/storage-engine-prisma-sqlite" ;
4
+ import cors , { CorsOptions } from "cors" ;
5
+ import { AuthJWT } from "convostack/auth-jwt" ;
6
+ import { createServer } from "http" ;
7
+ import * as dotenv from "dotenv" ;
8
+ import { DefaultAgentManager } from "convostack/agent" ;
9
+ import { IStorageEngine , IConversationEventServiceOptions } from "convostack/models" ;
10
+ import { StorageEnginePrismaPostgres } from "convostack/storage-engine-prisma-postgres" ;
11
+ import { StorageEnginePrismaMySQL } from "convostack/storage-engine-prisma-mysql" ;
12
+ import { RedisPubSub } from "graphql-redis-subscriptions" ;
13
+ import Redis , { RedisOptions } from "ioredis" ;
14
+ import { AgentHTTPClient } from "convostack/agent-http" ;
15
+
16
+ dotenv . config ( ) ;
17
+
18
+ const port = process . env . PORT || "3000" ;
19
+ const host = process . env . HOST || "localhost" ;
20
+ const proxyUrl = 'http://localhost:8088/client'
21
+ console . log ( "Configuring server..." ) ;
22
+
23
+ const corsOptions : CorsOptions = {
24
+ origin : [ "http://localhost:5173" , "https://studio.apollographql.com" ] ,
25
+ methods : "GET,HEAD,PUT,PATCH,POST,DELETE" ,
26
+ preflightContinue : false ,
27
+ optionsSuccessStatus : 204
28
+ } ;
29
+
30
+ const main = async ( ) => {
31
+ const app = express ( ) ;
32
+ app . use ( cors ( corsOptions ) ) ;
33
+ const httpServer = createServer ( app ) ;
34
+
35
+ let storage : IStorageEngine ;
36
+ switch ( process . env . STORAGE_ENGINE ) {
37
+ case 'sqlite' :
38
+ storage = new StorageEnginePrismaSQLite ( process . env . DATABASE_URL ) ;
39
+ await ( storage as StorageEnginePrismaSQLite ) . init ( ) ;
40
+ break ;
41
+ case 'postgres' :
42
+ storage = new StorageEnginePrismaPostgres ( process . env . DATABASE_URL ) ;
43
+ await ( storage as StorageEnginePrismaPostgres ) . init ( ) ;
44
+ break ;
45
+ case 'mysql' :
46
+ storage = new StorageEnginePrismaMySQL ( process . env . DATABASE_URL ) ;
47
+ await ( storage as StorageEnginePrismaMySQL ) . init ( ) ;
48
+ break ;
49
+ default :
50
+ throw new Error ( `Invalid storage engine: ${ process . env . STORAGE_ENGINE } ` )
51
+ }
52
+
53
+ const convEventsOpts = { } as IConversationEventServiceOptions ;
54
+ if ( process . env . REDIS_URL ) {
55
+ convEventsOpts . pubSubEngine = new RedisPubSub ( {
56
+ connection : process . env . REDIS_URL
57
+ } ) ;
58
+ convEventsOpts . cache = new Redis ( process . env . REDIS_URL ) ;
59
+ }
60
+
61
+ const backend = new ConvoStackBackendExpress ( {
62
+ basePath : "/" ,
63
+ storage,
64
+ auth : new AuthJWT ( storage , {
65
+ jwtSecret : process . env . JWT_SECRET ,
66
+ userDataVerificationSecret : process . env . USER_VERIFICATION_HASH_SECRET ,
67
+ allowAnonUsers : process . env . ALLOW_ANONYMOUS_USERS == "true" ,
68
+ requireUserVerificationHash : ! (
69
+ process . env . REQUIRE_USER_VERIFICATION_HASH == "false"
70
+ )
71
+ } ) ,
72
+ agents : new DefaultAgentManager ( {
73
+ "default" : {
74
+ agent : new AgentHTTPClient ( `${ proxyUrl } ?agentId=${ encodeURIComponent ( 'rjKv1v2JHNrwZdWzPP+YOJvsTR11N+7HfItM' ) } ` ) ,
75
+ metadata : {
76
+ displayName : "Echo Agent" ,
77
+ primer : "This is demo echo agent. Write me a message, and I will send it back to you!"
78
+ }
79
+ }
80
+ } , "default" ) ,
81
+ } ) ;
82
+
83
+ await backend . init ( app , httpServer ) ;
84
+
85
+ console . log ( `Starting server on port ${ port } ...` ) ;
86
+ httpServer . listen ( parseInt ( port ) , host , ( ) => {
87
+ console . log ( `Server is running on http://${ host } :${ port } /graphql` ) ;
88
+ } ) ;
89
+ } ;
90
+
91
+ try {
92
+ main ( ) ;
93
+ } catch ( err ) {
94
+ console . error ( err ) ;
95
+ }
0 commit comments