@positronic/cloudflare 0.0.15 → 0.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/src/api.js CHANGED
@@ -186,7 +186,7 @@ import { RESOURCE_TYPES } from '@positronic/core';
186
186
  var app = new Hono();
187
187
  app.post('/brains/runs', function(context) {
188
188
  return _async_to_generator(function() {
189
- var _ref, brainName, options, manifest, brain, brainRunId, namespace, doId, stub, initialData, response;
189
+ var requestBody, options, identifier, manifest, resolution, brain, brainRunId, namespace, doId, stub, initialData, brainTitle, response;
190
190
  return _ts_generator(this, function(_state) {
191
191
  switch(_state.label){
192
192
  case 0:
@@ -195,12 +195,15 @@ app.post('/brains/runs', function(context) {
195
195
  context.req.json()
196
196
  ];
197
197
  case 1:
198
- _ref = _state.sent(), brainName = _ref.brainName, options = _ref.options;
199
- if (!brainName) {
198
+ requestBody = _state.sent();
199
+ options = requestBody.options;
200
+ // Support both identifier and brainTitle for backward compatibility
201
+ identifier = requestBody.identifier || requestBody.brainTitle;
202
+ if (!identifier) {
200
203
  return [
201
204
  2,
202
205
  context.json({
203
- error: 'Missing brainName in request body'
206
+ error: 'Missing identifier or brainTitle in request body'
204
207
  }, 400)
205
208
  ];
206
209
  }
@@ -214,20 +217,27 @@ app.post('/brains/runs', function(context) {
214
217
  }, 500)
215
218
  ];
216
219
  }
217
- return [
218
- 4,
219
- manifest.import(brainName)
220
- ];
221
- case 2:
222
- brain = _state.sent();
223
- if (!brain) {
220
+ // Resolve the identifier to find the brain
221
+ resolution = manifest.resolve(identifier);
222
+ if (resolution.matchType === 'none') {
224
223
  return [
225
224
  2,
226
225
  context.json({
227
- error: "Brain '".concat(brainName, "' not found")
226
+ error: "Brain '".concat(identifier, "' not found")
228
227
  }, 404)
229
228
  ];
230
229
  }
230
+ if (resolution.matchType === 'multiple') {
231
+ return [
232
+ 2,
233
+ context.json({
234
+ error: 'Multiple brains match the identifier',
235
+ matchType: 'multiple',
236
+ candidates: resolution.candidates
237
+ }, 409)
238
+ ];
239
+ }
240
+ brain = resolution.brain;
231
241
  brainRunId = uuidv4();
232
242
  namespace = context.env.BRAIN_RUNNER_DO;
233
243
  doId = namespace.idFromName(brainRunId);
@@ -236,11 +246,13 @@ app.post('/brains/runs', function(context) {
236
246
  initialData = options ? {
237
247
  options: options
238
248
  } : undefined;
249
+ // Get the actual brain title from the resolved brain
250
+ brainTitle = brain.title || identifier;
239
251
  return [
240
252
  4,
241
- stub.start(brainName, brainRunId, initialData)
253
+ stub.start(brainTitle, brainRunId, initialData)
242
254
  ];
243
- case 3:
255
+ case 2:
244
256
  _state.sent();
245
257
  response = {
246
258
  brainRunId: brainRunId
@@ -255,7 +267,7 @@ app.post('/brains/runs', function(context) {
255
267
  });
256
268
  app.post('/brains/runs/rerun', function(context) {
257
269
  return _async_to_generator(function() {
258
- var _ref, brainName, runId, startsAt, stopsAfter, manifest, brain, monitorId, monitorStub, existingRun, newBrainRunId, namespace, doId, stub, rerunOptions, response;
270
+ var requestBody, runId, startsAt, stopsAfter, identifier, manifest, resolution, brain, monitorId, monitorStub, existingRun, newBrainRunId, namespace, doId, stub, rerunOptions, brainTitle, response;
259
271
  return _ts_generator(this, function(_state) {
260
272
  switch(_state.label){
261
273
  case 0:
@@ -264,12 +276,15 @@ app.post('/brains/runs/rerun', function(context) {
264
276
  context.req.json()
265
277
  ];
266
278
  case 1:
267
- _ref = _state.sent(), brainName = _ref.brainName, runId = _ref.runId, startsAt = _ref.startsAt, stopsAfter = _ref.stopsAfter;
268
- if (!brainName) {
279
+ requestBody = _state.sent();
280
+ runId = requestBody.runId, startsAt = requestBody.startsAt, stopsAfter = requestBody.stopsAfter;
281
+ // Support both identifier and brainTitle for backward compatibility
282
+ identifier = requestBody.identifier || requestBody.brainTitle;
283
+ if (!identifier) {
269
284
  return [
270
285
  2,
271
286
  context.json({
272
- error: 'Missing brainName in request body'
287
+ error: 'Missing identifier or brainTitle in request body'
273
288
  }, 400)
274
289
  ];
275
290
  }
@@ -283,23 +298,30 @@ app.post('/brains/runs/rerun', function(context) {
283
298
  }, 500)
284
299
  ];
285
300
  }
286
- return [
287
- 4,
288
- manifest.import(brainName)
289
- ];
290
- case 2:
291
- brain = _state.sent();
292
- if (!brain) {
301
+ // Resolve the identifier to find the brain
302
+ resolution = manifest.resolve(identifier);
303
+ if (resolution.matchType === 'none') {
293
304
  return [
294
305
  2,
295
306
  context.json({
296
- error: "Brain '".concat(brainName, "' not found")
307
+ error: "Brain '".concat(identifier, "' not found")
297
308
  }, 404)
298
309
  ];
299
310
  }
311
+ if (resolution.matchType === 'multiple') {
312
+ return [
313
+ 2,
314
+ context.json({
315
+ error: 'Multiple brains match the identifier',
316
+ matchType: 'multiple',
317
+ candidates: resolution.candidates
318
+ }, 409)
319
+ ];
320
+ }
321
+ brain = resolution.brain;
300
322
  if (!runId) return [
301
323
  3,
302
- 4
324
+ 3
303
325
  ];
304
326
  monitorId = context.env.MONITOR_DO.idFromName('singleton');
305
327
  monitorStub = context.env.MONITOR_DO.get(monitorId);
@@ -307,7 +329,7 @@ app.post('/brains/runs/rerun', function(context) {
307
329
  4,
308
330
  monitorStub.getLastEvent(runId)
309
331
  ];
310
- case 3:
332
+ case 2:
311
333
  existingRun = _state.sent();
312
334
  if (!existingRun) {
313
335
  return [
@@ -317,8 +339,8 @@ app.post('/brains/runs/rerun', function(context) {
317
339
  }, 404)
318
340
  ];
319
341
  }
320
- _state.label = 4;
321
- case 4:
342
+ _state.label = 3;
343
+ case 3:
322
344
  // Create a new brain run with rerun parameters
323
345
  newBrainRunId = uuidv4();
324
346
  namespace = context.env.BRAIN_RUNNER_DO;
@@ -332,11 +354,13 @@ app.post('/brains/runs/rerun', function(context) {
332
354
  }, stopsAfter !== undefined && {
333
355
  stopsAfter: stopsAfter
334
356
  });
357
+ // Get the actual brain title from the resolved brain
358
+ brainTitle = brain.title || identifier;
335
359
  return [
336
360
  4,
337
- stub.start(brainName, newBrainRunId, rerunOptions)
361
+ stub.start(brainTitle, newBrainRunId, rerunOptions)
338
362
  ];
339
- case 5:
363
+ case 4:
340
364
  _state.sent();
341
365
  response = {
342
366
  brainRunId: newBrainRunId
@@ -373,20 +397,52 @@ app.get('/brains/runs/:runId/watch', function(context) {
373
397
  });
374
398
  })();
375
399
  });
376
- app.get('/brains/:brainName/history', function(context) {
400
+ app.get('/brains/:identifier/history', function(context) {
377
401
  return _async_to_generator(function() {
378
- var brainName, limit, monitorId, monitorStub, runs;
402
+ var identifier, limit, manifest, resolution, brain, brainTitle, monitorId, monitorStub, runs;
379
403
  return _ts_generator(this, function(_state) {
380
404
  switch(_state.label){
381
405
  case 0:
382
- brainName = context.req.param('brainName');
406
+ identifier = context.req.param('identifier');
383
407
  limit = Number(context.req.query('limit') || '10');
408
+ // Resolve the identifier to get the actual brain title
409
+ manifest = getManifest();
410
+ if (!manifest) {
411
+ return [
412
+ 2,
413
+ context.json({
414
+ error: 'Manifest not initialized'
415
+ }, 500)
416
+ ];
417
+ }
418
+ resolution = manifest.resolve(identifier);
419
+ if (resolution.matchType === 'none') {
420
+ return [
421
+ 2,
422
+ context.json({
423
+ error: "Brain '".concat(identifier, "' not found")
424
+ }, 404)
425
+ ];
426
+ }
427
+ if (resolution.matchType === 'multiple') {
428
+ return [
429
+ 2,
430
+ context.json({
431
+ error: 'Multiple brains match the identifier',
432
+ matchType: 'multiple',
433
+ candidates: resolution.candidates
434
+ }, 300)
435
+ ];
436
+ }
437
+ // Get the actual brain title
438
+ brain = resolution.brain;
439
+ brainTitle = brain.title || identifier;
384
440
  // Get the monitor singleton instance
385
441
  monitorId = context.env.MONITOR_DO.idFromName('singleton');
386
442
  monitorStub = context.env.MONITOR_DO.get(monitorId);
387
443
  return [
388
444
  4,
389
- monitorStub.history(brainName, limit)
445
+ monitorStub.history(brainTitle, limit)
390
446
  ];
391
447
  case 1:
392
448
  runs = _state.sent();
@@ -400,19 +456,51 @@ app.get('/brains/:brainName/history', function(context) {
400
456
  });
401
457
  })();
402
458
  });
403
- app.get('/brains/:brainName/active-runs', function(context) {
459
+ app.get('/brains/:identifier/active-runs', function(context) {
404
460
  return _async_to_generator(function() {
405
- var brainName, monitorId, monitorStub, runs;
461
+ var identifier, manifest, resolution, brain, brainTitle, monitorId, monitorStub, runs;
406
462
  return _ts_generator(this, function(_state) {
407
463
  switch(_state.label){
408
464
  case 0:
409
- brainName = context.req.param('brainName');
465
+ identifier = context.req.param('identifier');
466
+ // Resolve the identifier to get the actual brain title
467
+ manifest = getManifest();
468
+ if (!manifest) {
469
+ return [
470
+ 2,
471
+ context.json({
472
+ error: 'Manifest not initialized'
473
+ }, 500)
474
+ ];
475
+ }
476
+ resolution = manifest.resolve(identifier);
477
+ if (resolution.matchType === 'none') {
478
+ return [
479
+ 2,
480
+ context.json({
481
+ error: "Brain '".concat(identifier, "' not found")
482
+ }, 404)
483
+ ];
484
+ }
485
+ if (resolution.matchType === 'multiple') {
486
+ return [
487
+ 2,
488
+ context.json({
489
+ error: 'Multiple brains match the identifier',
490
+ matchType: 'multiple',
491
+ candidates: resolution.candidates
492
+ }, 300)
493
+ ];
494
+ }
495
+ // Get the actual brain title
496
+ brain = resolution.brain;
497
+ brainTitle = brain.title || identifier;
410
498
  // Get the monitor singleton instance
411
499
  monitorId = context.env.MONITOR_DO.idFromName('singleton');
412
500
  monitorStub = context.env.MONITOR_DO.get(monitorId);
413
501
  return [
414
502
  4,
415
- monitorStub.activeRuns(brainName)
503
+ monitorStub.activeRuns(brainTitle)
416
504
  ];
417
505
  case 1:
418
506
  runs = _state.sent();
@@ -450,7 +538,7 @@ app.get('/brains/watch', function(context) {
450
538
  });
451
539
  app.get('/brains', function(context) {
452
540
  return _async_to_generator(function() {
453
- var manifest, brainNames, brains, validBrains;
541
+ var manifest, brainFilenames, brains, validBrains;
454
542
  return _ts_generator(this, function(_state) {
455
543
  switch(_state.label){
456
544
  case 0:
@@ -463,18 +551,18 @@ app.get('/brains', function(context) {
463
551
  }, 500)
464
552
  ];
465
553
  }
466
- brainNames = manifest.list();
554
+ brainFilenames = manifest.list();
467
555
  return [
468
556
  4,
469
- Promise.all(brainNames.map(function(name) {
557
+ Promise.all(brainFilenames.map(function(filename) {
470
558
  return _async_to_generator(function() {
471
- var brain;
559
+ var brain, structure;
472
560
  return _ts_generator(this, function(_state) {
473
561
  switch(_state.label){
474
562
  case 0:
475
563
  return [
476
564
  4,
477
- manifest.import(name)
565
+ manifest.import(filename)
478
566
  ];
479
567
  case 1:
480
568
  brain = _state.sent();
@@ -484,12 +572,13 @@ app.get('/brains', function(context) {
484
572
  null
485
573
  ];
486
574
  }
575
+ structure = brain.structure;
487
576
  return [
488
577
  2,
489
578
  {
490
- name: name,
491
- title: brain.title,
492
- description: "".concat(brain.title, " brain")
579
+ filename: filename,
580
+ title: structure.title,
581
+ description: structure.description || "".concat(structure.title, " brain")
493
582
  }
494
583
  ];
495
584
  }
@@ -518,7 +607,7 @@ app.get('/brains', function(context) {
518
607
  // Create a new schedule
519
608
  app.post('/brains/schedules', function(context) {
520
609
  return _async_to_generator(function() {
521
- var body, brainName, cronExpression, scheduleId, scheduleStub, schedule, error, errorMessage;
610
+ var body, cronExpression, identifier, manifest, resolution, brain, brainTitle, scheduleId, scheduleStub, schedule, error, errorMessage;
522
611
  return _ts_generator(this, function(_state) {
523
612
  switch(_state.label){
524
613
  case 0:
@@ -534,12 +623,14 @@ app.post('/brains/schedules', function(context) {
534
623
  ];
535
624
  case 1:
536
625
  body = _state.sent();
537
- brainName = body.brainName, cronExpression = body.cronExpression;
538
- if (!brainName) {
626
+ cronExpression = body.cronExpression;
627
+ // Support both identifier and brainTitle for backward compatibility
628
+ identifier = body.identifier || body.brainTitle;
629
+ if (!identifier) {
539
630
  return [
540
631
  2,
541
632
  context.json({
542
- error: 'Missing required field "brainName"'
633
+ error: 'Missing required field "identifier" or "brainTitle"'
543
634
  }, 400)
544
635
  ];
545
636
  }
@@ -562,12 +653,44 @@ app.post('/brains/schedules', function(context) {
562
653
  }, 400)
563
654
  ];
564
655
  }
656
+ // Resolve the identifier to get the actual brain title
657
+ manifest = getManifest();
658
+ if (!manifest) {
659
+ return [
660
+ 2,
661
+ context.json({
662
+ error: 'Manifest not initialized'
663
+ }, 500)
664
+ ];
665
+ }
666
+ resolution = manifest.resolve(identifier);
667
+ if (resolution.matchType === 'none') {
668
+ return [
669
+ 2,
670
+ context.json({
671
+ error: "Brain '".concat(identifier, "' not found")
672
+ }, 404)
673
+ ];
674
+ }
675
+ if (resolution.matchType === 'multiple') {
676
+ return [
677
+ 2,
678
+ context.json({
679
+ error: 'Multiple brains match the identifier',
680
+ matchType: 'multiple',
681
+ candidates: resolution.candidates
682
+ }, 409)
683
+ ];
684
+ }
685
+ // Get the actual brain title
686
+ brain = resolution.brain;
687
+ brainTitle = brain.title || identifier;
565
688
  // Get the schedule singleton instance
566
689
  scheduleId = context.env.SCHEDULE_DO.idFromName('singleton');
567
690
  scheduleStub = context.env.SCHEDULE_DO.get(scheduleId);
568
691
  return [
569
692
  4,
570
- scheduleStub.createSchedule(brainName, cronExpression)
693
+ scheduleStub.createSchedule(brainTitle, cronExpression)
571
694
  ];
572
695
  case 2:
573
696
  schedule = _state.sent();
@@ -674,48 +797,51 @@ app.delete('/brains/schedules/:scheduleId', function(context) {
674
797
  });
675
798
  })();
676
799
  });
677
- app.get('/brains/:brainName', function(context) {
800
+ app.get('/brains/:identifier', function(context) {
678
801
  return _async_to_generator(function() {
679
- var brainName, manifest, brain, structure;
802
+ var identifier, manifest, resolution, brain, structure;
680
803
  return _ts_generator(this, function(_state) {
681
- switch(_state.label){
682
- case 0:
683
- brainName = context.req.param('brainName');
684
- manifest = getManifest();
685
- if (!manifest) {
686
- return [
687
- 2,
688
- context.json({
689
- error: 'Manifest not initialized'
690
- }, 500)
691
- ];
692
- }
693
- return [
694
- 4,
695
- manifest.import(brainName)
696
- ];
697
- case 1:
698
- brain = _state.sent();
699
- if (!brain) {
700
- return [
701
- 2,
702
- context.json({
703
- error: "Brain '".concat(brainName, "' not found")
704
- }, 404)
705
- ];
706
- }
707
- // Get the brain structure
708
- structure = brain.structure;
709
- return [
710
- 2,
711
- context.json({
712
- name: brainName,
713
- title: structure.title,
714
- description: structure.description || "".concat(structure.title, " brain"),
715
- steps: structure.steps
716
- })
717
- ];
804
+ identifier = context.req.param('identifier');
805
+ manifest = getManifest();
806
+ if (!manifest) {
807
+ return [
808
+ 2,
809
+ context.json({
810
+ error: 'Manifest not initialized'
811
+ }, 500)
812
+ ];
718
813
  }
814
+ // Resolve the identifier to find the brain
815
+ resolution = manifest.resolve(identifier);
816
+ if (resolution.matchType === 'none') {
817
+ return [
818
+ 2,
819
+ context.json({
820
+ error: "Brain '".concat(identifier, "' not found")
821
+ }, 404)
822
+ ];
823
+ }
824
+ if (resolution.matchType === 'multiple') {
825
+ return [
826
+ 2,
827
+ context.json({
828
+ error: 'Multiple brains match the identifier',
829
+ matchType: 'multiple',
830
+ candidates: resolution.candidates
831
+ }, 300)
832
+ ];
833
+ }
834
+ brain = resolution.brain;
835
+ // Get the brain structure
836
+ structure = brain.structure;
837
+ return [
838
+ 2,
839
+ context.json({
840
+ title: structure.title,
841
+ description: structure.description || "".concat(structure.title, " brain"),
842
+ steps: structure.steps
843
+ })
844
+ ];
719
845
  });
720
846
  })();
721
847
  });