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