@@ -20,10 +20,31 @@ async function availableExperiences(ctx){
20
20
mbr_id,
21
21
}
22
22
}
23
+ async function entry ( ctx ) {
24
+ await mAPIKeyValidation ( ctx )
25
+ const { assistantType, mbr_id } = ctx . state
26
+ if ( ! ctx . request . body ?. summary ?. length )
27
+ throw new Error ( 'No entry summary provided. Use `summary` field.' )
28
+ console . log ( chalk . yellowBright ( 'entry()::entry attempted:' ) , ctx . request . body )
29
+ const summary = {
30
+ ...ctx . request . body ,
31
+ assistantType,
32
+ mbr_id,
33
+ }
34
+ const entry = await ctx . MyLife . entry ( summary )
35
+ console . log ( chalk . yellowBright ( 'entry()::entry submitted:' ) , entry , summary )
36
+ ctx . status = 200
37
+ ctx . body = {
38
+ id : entry . id ,
39
+ message : 'entry submitted successfully.' ,
40
+ success : true ,
41
+ }
42
+
43
+ }
23
44
// @todo implement builder functionality, allowing for interface creation of experiences by members
24
45
// @todo implement access to exposed member experiences using `mbr_key` as parameter to `factory.getItem()`
25
46
async function experienceBuilder ( ctx ) {
26
- mAPIKeyValidation ( ctx )
47
+ await mAPIKeyValidation ( ctx )
27
48
const { assistantType, mbr_id } = ctx . state
28
49
const { eid, sid } = ctx . params
29
50
const { experience } = ctx . request . body ?. experience
@@ -54,7 +75,7 @@ async function experienceCast(ctx){
54
75
* @property {object } scene - Scene data, regardless if "current" or new.
55
76
*/
56
77
async function experience ( ctx ) {
57
- mAPIKeyValidation ( ctx )
78
+ await mAPIKeyValidation ( ctx )
58
79
const { MemberSession, } = ctx . state
59
80
const { eid, } = ctx . params
60
81
const { memberInput, } = ctx . request . body
@@ -66,8 +87,8 @@ async function experience(ctx){
66
87
* @returns {Object } - Represents `ctx.body` object with following `experience` properties.
67
88
* @property {boolean } success - Success status, true/false.
68
89
*/
69
- function experienceEnd ( ctx ) {
70
- mAPIKeyValidation ( ctx )
90
+ async function experienceEnd ( ctx ) {
91
+ await mAPIKeyValidation ( ctx )
71
92
const { MemberSession, } = ctx . state
72
93
const { eid, } = ctx . params
73
94
ctx . body = MemberSession . experienceEnd ( eid )
@@ -80,17 +101,17 @@ function experienceEnd(ctx){
80
101
* @property {Array } cast - Experience cast array.
81
102
* @property {Object } navigation - Navigation object (optional - for interactive display purposes only).
82
103
*/
83
- function experienceManifest ( ctx ) {
84
- mAPIKeyValidation ( ctx )
104
+ async function experienceManifest ( ctx ) {
105
+ await mAPIKeyValidation ( ctx )
85
106
const { avatar, } = ctx . state
86
107
ctx . body = avatar . manifest
87
108
return
88
109
}
89
110
/**
90
111
* Navigation array of scenes for experience.
91
112
*/
92
- function experienceNavigation ( ctx ) {
93
- mAPIKeyValidation ( ctx )
113
+ async function experienceNavigation ( ctx ) {
114
+ await mAPIKeyValidation ( ctx )
94
115
const { avatar, } = ctx . state
95
116
ctx . body = avatar . navigation
96
117
return
@@ -103,18 +124,26 @@ function experienceNavigation(ctx){
103
124
* @property {array } experiences - Array of Experience shorthand objects.
104
125
*/
105
126
async function experiences ( ctx ) {
106
- mAPIKeyValidation ( ctx )
127
+ await mAPIKeyValidation ( ctx )
107
128
const { MemberSession, } = ctx . state
108
129
// limit one mandatory experience (others could be highlighted in alerts) per session
109
130
const experiencesObject = await MemberSession . experiences ( )
110
131
ctx . body = experiencesObject
111
132
}
112
133
async function experiencesLived ( ctx ) {
113
- mAPIKeyValidation ( ctx )
134
+ await mAPIKeyValidation ( ctx )
114
135
const { MemberSession, } = ctx . state
115
136
ctx . body = MemberSession . experiencesLived
116
137
}
117
- async function keyValidation ( ctx ) { // from openAI
138
+ /**
139
+ * Validates member key and returns member data. Leverages the key validation structure to ensure payload is liegimate. Currently in use by OpenAI GPT and local Postman instance.
140
+ * @param {Koa } ctx - Koa Context object
141
+ * @returns {object } - Object with following properties.
142
+ * @property {boolean } success - Success status.
143
+ * @property {string } message - Message to querying intelligence.
144
+ * @property {object } data - Consented Member data.
145
+ */
146
+ async function keyValidation ( ctx ) {
118
147
await mAPIKeyValidation ( ctx )
119
148
ctx . status = 200 // OK
120
149
if ( ctx . method === 'HEAD' ) return
@@ -137,10 +166,10 @@ async function keyValidation(ctx){ // from openAI
137
166
?? names ?. [ 0 ] . split ( ' ' ) [ 0 ]
138
167
?? '' ,
139
168
}
140
- console . log ( chalk . yellowBright ( `keyValidation():${ memberCoreData . mbr_id } ` ) , memberCoreData . fullName )
169
+ console . log ( chalk . yellowBright ( `keyValidation()::` ) , chalk . redBright ( `success::` ) , chalk . redBright ( memberCoreData . mbr_id ) )
141
170
ctx . body = {
142
171
success : true ,
143
- message : 'Valid member. ' ,
172
+ message : 'Valid Member ' ,
144
173
data : memberCoreData ,
145
174
}
146
175
}
@@ -195,21 +224,25 @@ async function register(ctx){
195
224
/**
196
225
* Functionality around story contributions.
197
226
* @param {Koa } ctx - Koa Context object
198
- * @returns {Koa } Koa Context object
199
227
*/
200
- async function story ( ctx ) {
228
+ async function memory ( ctx ) {
201
229
await mAPIKeyValidation ( ctx ) // sets ctx.state.mbr_id and more
202
230
const { assistantType, mbr_id } = ctx . state
203
- const { storySummary } = ctx . request ?. body ?? { }
204
- if ( ! storySummary ?. length )
205
- ctx . throw ( 400 , 'No story summary provided. Use `storySummary` field.' )
206
- // write to cosmos db
207
- const _story = await ctx . MyLife . story ( mbr_id , assistantType , storySummary ) // @todo : remove await
208
- console . log ( chalk . yellowBright ( 'story submitted:' ) , _story )
209
- ctx . status = 200 // OK
231
+ if ( ! ctx . request . body ?. summary ?. length )
232
+ throw new Error ( 'No memory summary provided. Use `summary` field.' )
233
+ console . log ( chalk . yellowBright ( 'memory()::memory attempted:' ) , ctx . request . body )
234
+ const summary = {
235
+ ...ctx . request . body ,
236
+ assistantType,
237
+ mbr_id,
238
+ }
239
+ const memory = await ctx . MyLife . memory ( summary )
240
+ console . log ( chalk . yellowBright ( 'memory()::memory submitted:' ) , memory , summary )
241
+ ctx . status = 200
210
242
ctx . body = {
243
+ id : memory . id ,
244
+ message : 'memory submitted successfully.' ,
211
245
success : true ,
212
- message : 'Story submitted successfully.' ,
213
246
}
214
247
}
215
248
/**
@@ -220,21 +253,21 @@ async function story(ctx){
220
253
* @param {function } next Koa next function
221
254
* @returns {function } Koa next function
222
255
*/
223
- async function tokenValidation ( ctx , next ) {
256
+ async function tokenValidation ( ctx , next ) {
224
257
try {
225
258
const authHeader = ctx . request . headers [ 'authorization' ]
226
259
if ( ! authHeader ) {
227
260
ctx . status = 401
228
261
ctx . body = { error : 'Authorization header is missing' }
229
262
return
230
263
}
231
- const _token = authHeader . split ( ' ' ) [ 1 ] // Bearer TOKEN_VALUE
232
- if ( ! mTokenValidation ( _token ) ) {
264
+ const token = authHeader . split ( ' ' ) [ 1 ] // Bearer TOKEN_VALUE
265
+ if ( ! mTokenValidation ( token ) ) {
233
266
ctx . status = 401
234
267
ctx . body = { error : 'Authorization token failure' }
235
268
return
236
269
}
237
- ctx . state . token = _token // **note:** keep first, as it is used in mTokenType()
270
+ ctx . state . token = token // **note:** keep first, as it is used in mTokenType()
238
271
ctx . state . assistantType = mTokenType ( ctx )
239
272
await next ( )
240
273
} catch ( error ) {
@@ -283,19 +316,11 @@ async function mAPIKeyValidation(ctx){ // transforms ctx.state
283
316
ctx . throw ( 400 , 'Missing member key.' )
284
317
else // unlocked, providing passphrase
285
318
return
286
- let isValidated
287
- if ( ! ctx . state . locked || ctx . session ?. isAPIValidated ) {
288
- isValidated = true
289
- } else {
290
- const serverHostedMembers = JSON . parse ( process . env . MYLIFE_HOSTED_MBR_ID ?? '[]' )
291
- const localHostedMembers = [
292
- 'system-one|4e6e2f26-174b-43e4-851f-7cf9cdf056df' ,
293
- ] . filter ( member => serverHostedMembers . includes ( member ) ) // none currently
294
- serverHostedMembers . push ( ...localHostedMembers )
295
- if ( serverHostedMembers . includes ( memberId ) )
296
- isValidated = await ctx . MyLife . testPartitionKey ( memberId )
297
- }
298
- if ( isValidated ) {
319
+ if ( // validated
320
+ ! ctx . state . locked
321
+ || ( ctx . session . isAPIValidated ?? false )
322
+ || await ctx . MyLife . isMemberHosted ( memberId )
323
+ ) {
299
324
ctx . state . isValidated = true
300
325
ctx . state . mbr_id = memberId
301
326
ctx . state . assistantType = mTokenType ( ctx )
@@ -308,12 +333,13 @@ function mTokenType(ctx){
308
333
const assistantType = mBotSecrets ?. [ token ] ?? 'personal-avatar'
309
334
return assistantType
310
335
}
311
- function mTokenValidation ( _token ) {
312
- return mBotSecrets ?. [ _token ] ?. length ?? false
336
+ function mTokenValidation ( token ) {
337
+ return mBotSecrets ?. [ token ] ?. length ?? false
313
338
}
314
339
/* exports */
315
340
export {
316
341
availableExperiences ,
342
+ entry ,
317
343
experience ,
318
344
experienceCast ,
319
345
experienceEnd ,
@@ -323,8 +349,8 @@ export {
323
349
experiencesLived ,
324
350
keyValidation ,
325
351
logout ,
352
+ memory ,
326
353
register ,
327
- story ,
328
354
tokenValidation ,
329
355
upload ,
330
356
}
0 commit comments