File size: 8,925 Bytes
1905805
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
from quart import Quart, request, websocket
import asyncio
import json
import base64
import logging
from utils.args import args
from utils.helpers.singleton import Singleton
from utils.jaison import JAIson, JobType, NonexistantJobException
from utils.config import Config
from utils.helpers.observer import BaseObserverClient
from .common import create_response, create_preflight

app = Quart(__name__)
cors_header = {'Access-Control-Allow-Origin': '*'}

## Websocket Event Broadcasting Server ##

class SocketServerObserver(BaseObserverClient, metaclass=Singleton):
    def __init__(self):
        super().__init__(server=JAIson().event_server)
        self.connections = set()
        self.shutdown_signal = asyncio.Future()

    async def handle_event(self, event_id: str, payload) -> None:
        '''Broadcast events from broadcast server'''
        for key in payload:
            if isinstance(payload[key], bytes):
                  payload[key] = base64.b64encode(payload[key]).decode('utf-8')
        message = json.dumps(create_response(200, event_id, payload))
        logging.debug(f"Broadcasting event to {len(self.connections)} clients")
        for ws in set(self.connections):
            await ws.send(message)
            
    def shutdown(self, *args): # TODO set for use somewhere
        self.shutdown_signal.set_result(None)
        
@app.websocket("/")
async def ws():
    sso = SocketServerObserver()
    logging.info("Opened new websocket connection")
    ws = websocket._get_current_object()
    await ws.accept()
    sso.connections.add(ws)
    try:
        while not sso.shutdown_signal.done():
            await asyncio.sleep(10)
    except asyncio.CancelledError:
        sso.connections.discard(ws)
        logging.info("Closed websocket connection")
        raise

## Generic endpoints ###################

@app.route('/api/operations', methods=['GET'])
async def get_loaded_operations():
    return create_response(200, f"Loaded operations gotten", JAIson().get_loaded_operations(), cors_header)
  
@app.route('/api/config', methods=['GET'])
async def get_current_config():
    return create_response(200, f"Current config gotten", JAIson().get_current_config(), cors_header)

## Job management endpoints ###########
@app.route('/api/job', methods=['DELETE'])
async def cancel_job():
    try:
        request_data = await request.get_json()
        assert 'job_id' in request_data
        return create_response(200, f"Job flagged for cancellation", await JAIson().cancel_job(request_data['job_id'], request_data.get('reason')), cors_header)
    except NonexistantJobException as err:
        return create_response(400, f"Job ID does not exist or already finished", {}, cors_header)
    except AssertionError as err:
        return create_response(400, f"Request missing job_id", {}, cors_header)
    except Exception as err:
        return create_response(500, str(err), {}, cors_header)

## Specific job creation endpoints ####

async def _request_job(job_type: JobType):
    try:
        request_data = (await request.get_json()) or dict()

        job_id = await JAIson().create_job(job_type, **request_data)
        return create_response(200, f"{job_type} job created", {"job_id": job_id}, cors_header)
    except Exception as err:
        logging.error(f"Error occured for {job_type} API request", stack_info=True, exc_info=True)
        return create_response(500, str(err), {}, cors_header)

# Main response pipeline
@app.route('/api/response', methods=['POST'])
async def response():
    return await _request_job(JobType.RESPONSE)

# Context - General
@app.route('/api/context', methods=['DELETE'])    
async def context_clear():
    return await _request_job(JobType.CONTEXT_CLEAR)

# Context - Configure
@app.route('/api/context/config', methods=['PUT'])    
async def context_configure():
    return await _request_job(JobType.CONTEXT_CONFIGURE)

# Context - Requests
@app.route('/api/context/request', methods=['POST'])    
async def context_request_add():
    return await _request_job(JobType.CONTEXT_REQUEST_ADD)

# Context - Conversation
@app.route('/api/context/conversation/text', methods=['POST'])    
async def context_conversation_add_text():
    return await _request_job(JobType.CONTEXT_CONVERSATION_ADD_TEXT)

@app.route('/api/context/conversation/audio', methods=['POST'])    
async def context_conversation_add_audio():
    return await _request_job(JobType.CONTEXT_CONVERSATION_ADD_AUDIO)

# Context - Custom
@app.route('/api/context/custom', methods=['PUT'])    
async def context_custom_register():
    return await _request_job(JobType.CONTEXT_CUSTOM_REGISTER)

@app.route('/api/context/custom', methods=['DELETE'])    
async def context_custom_remove():
    return await _request_job(JobType.CONTEXT_CUSTOM_REMOVE)

@app.route('/api/context/custom', methods=['POST'])    
async def context_custom_add():
    return await _request_job(JobType.CONTEXT_CUSTOM_ADD)

# Operation management
@app.route('/api/operations/load', methods=['POST'])    
async def operation_start():
    return await _request_job(JobType.OPERATION_LOAD)

@app.route('/api/operations/reload', methods=['POST'])    
async def operation_reload():
    return await _request_job(JobType.OPERATION_CONFIG_RELOAD)

@app.route('/api/operations/unload', methods=['POST'])    
async def operation_unload():
    return await _request_job(JobType.OPERATION_UNLOAD)

@app.route('/api/operations/config', methods=['POST'])    
async def operation_configure():
    return await _request_job(JobType.OPERATION_CONFIGURE)

@app.route('/api/operations/use', methods=['POST'])    
async def operation_use():
    return await _request_job(JobType.OPERATION_USE)

# Configuration
@app.route('/api/config/load', methods=['PUT'])    
async def config_load():
    return await _request_job(JobType.CONFIG_LOAD)

# Configuration
@app.route('/api/config/update', methods=['PUT'])    
async def config_update():
    return await _request_job(JobType.CONFIG_UPDATE)

@app.route('/api/config/save', methods=['POST'])    
async def config_save():
    return await _request_job(JobType.CONFIG_SAVE)

# Allow CORS
@app.route('/api/job', methods=['OPTIONS']) 
async def preflight_job():
    return create_preflight('DELETE')

@app.route('/api/response', methods=['OPTIONS']) 
async def preflight_response():
    return create_preflight('POST')

@app.route('/api/context', methods=['OPTIONS']) 
async def preflight_context_conversation_clear():
    return create_preflight('DELETE')

@app.route('/api/context/config', methods=['OPTIONS'])    
async def preflight_context_configure():
    return create_preflight('PUT')

@app.route('/api/context/request', methods=['OPTIONS']) 
async def preflight_context_request():
    return create_preflight('POST')

@app.route('/api/context/conversation/text', methods=['OPTIONS']) 
async def preflight_context_conversation_text():
    return create_preflight('POST')

@app.route('/api/context/conversation/audio', methods=['OPTIONS']) 
async def preflight_context_conversation_audio():
    return create_preflight('POST')

@app.route('/api/context/custom', methods=['OPTIONS']) 
async def preflight_context_custom():
    return create_preflight('POST, PUT, DELETE')

@app.route('/api/operations', methods=['OPTIONS']) 
async def preflight_operations_info():
    return create_preflight('GET')

@app.route('/api/operations/load', methods=['OPTIONS']) 
async def preflight_operation_start():
    return create_preflight('POST')

@app.route('/api/operations/reload', methods=['OPTIONS']) 
async def preflight_operation_reload():
    return create_preflight('POST')

@app.route('/api/operations/unload', methods=['OPTIONS']) 
async def preflight_operation_unload():
    return create_preflight('POST')

@app.route('/api/operations/config', methods=['OPTIONS'])    
async def preflight_operation_configure():
    return create_preflight('POST')

@app.route('/api/operations/use', methods=['OPTIONS'])
async def preflight_operation_use():
    return create_preflight('POST')

@app.route('/api/config', methods=['OPTIONS']) 
async def preflight_config():
    return create_preflight('GET')

@app.route('/api/config/load', methods=['OPTIONS']) 
async def preflight_config_load():
    return create_preflight('PUT')

@app.route('/api/config/update', methods=['OPTIONS']) 
async def preflight_config_update():
    return create_preflight('PUT')

@app.route('/api/config/save', methods=['OPTIONS']) 
async def preflight_config_save():
    return create_preflight('POST')

## START ###################################
async def start_web_server(): # TODO launch application plugins here as well
    try:
        global app
        await JAIson().start()
        SocketServerObserver()
        await app.run_task(host=args.host, port=args.port)
    except Exception as err:
        logging.error("Stopping server due to exception", exc_info=True)
    finally:    
        await JAIson().stop()