@positronic/cloudflare 0.0.38 → 0.0.40
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
|
@@ -227,6 +227,7 @@ import { Hono } from 'hono';
|
|
|
227
227
|
import { v4 as uuidv4 } from 'uuid';
|
|
228
228
|
import { AwsClient } from 'aws4fetch';
|
|
229
229
|
import { parseCronExpression } from 'cron-schedule';
|
|
230
|
+
import Fuse from 'fuse.js';
|
|
230
231
|
import { getManifest, getWebhookManifest } from './brain-runner-do.js';
|
|
231
232
|
import { RESOURCE_TYPES } from '@positronic/core';
|
|
232
233
|
/**
|
|
@@ -700,7 +701,7 @@ app.get('/brains/watch', function(context) {
|
|
|
700
701
|
});
|
|
701
702
|
app.get('/brains', function(context) {
|
|
702
703
|
return _async_to_generator(function() {
|
|
703
|
-
var manifest, brainFilenames, brains, validBrains;
|
|
704
|
+
var _context_req_query, manifest, query, brainFilenames, brains, validBrains, queryLower, exactMatch, fuse, results;
|
|
704
705
|
return _ts_generator(this, function(_state) {
|
|
705
706
|
switch(_state.label){
|
|
706
707
|
case 0:
|
|
@@ -713,6 +714,7 @@ app.get('/brains', function(context) {
|
|
|
713
714
|
}, 500)
|
|
714
715
|
];
|
|
715
716
|
}
|
|
717
|
+
query = (_context_req_query = context.req.query('q')) === null || _context_req_query === void 0 ? void 0 : _context_req_query.trim();
|
|
716
718
|
brainFilenames = manifest.list();
|
|
717
719
|
return [
|
|
718
720
|
4,
|
|
@@ -754,11 +756,84 @@ app.get('/brains', function(context) {
|
|
|
754
756
|
validBrains = brains.filter(function(brain) {
|
|
755
757
|
return brain !== null;
|
|
756
758
|
});
|
|
759
|
+
// If no query, return all brains
|
|
760
|
+
if (!query) {
|
|
761
|
+
return [
|
|
762
|
+
2,
|
|
763
|
+
context.json({
|
|
764
|
+
brains: validBrains,
|
|
765
|
+
count: validBrains.length
|
|
766
|
+
})
|
|
767
|
+
];
|
|
768
|
+
}
|
|
769
|
+
// Check for exact match on title or filename first
|
|
770
|
+
queryLower = query.toLowerCase();
|
|
771
|
+
exactMatch = validBrains.find(function(brain) {
|
|
772
|
+
return brain.title.toLowerCase() === queryLower || brain.filename.toLowerCase() === queryLower;
|
|
773
|
+
});
|
|
774
|
+
if (exactMatch) {
|
|
775
|
+
return [
|
|
776
|
+
2,
|
|
777
|
+
context.json({
|
|
778
|
+
brains: [
|
|
779
|
+
exactMatch
|
|
780
|
+
],
|
|
781
|
+
count: 1
|
|
782
|
+
})
|
|
783
|
+
];
|
|
784
|
+
}
|
|
785
|
+
// Use fuse.js for fuzzy matching with weighted keys
|
|
786
|
+
fuse = new Fuse(validBrains, {
|
|
787
|
+
keys: [
|
|
788
|
+
{
|
|
789
|
+
name: 'title',
|
|
790
|
+
weight: 2
|
|
791
|
+
},
|
|
792
|
+
{
|
|
793
|
+
name: 'filename',
|
|
794
|
+
weight: 2
|
|
795
|
+
},
|
|
796
|
+
{
|
|
797
|
+
name: 'description',
|
|
798
|
+
weight: 0.5
|
|
799
|
+
}
|
|
800
|
+
],
|
|
801
|
+
includeScore: true,
|
|
802
|
+
threshold: 0.4,
|
|
803
|
+
ignoreLocation: true
|
|
804
|
+
});
|
|
805
|
+
results = fuse.search(query);
|
|
806
|
+
// If no results, return empty
|
|
807
|
+
if (results.length === 0) {
|
|
808
|
+
return [
|
|
809
|
+
2,
|
|
810
|
+
context.json({
|
|
811
|
+
brains: [],
|
|
812
|
+
count: 0
|
|
813
|
+
})
|
|
814
|
+
];
|
|
815
|
+
}
|
|
816
|
+
// If top result is significantly better than others (score difference > 0.2),
|
|
817
|
+
// or there's only one result, return just that one
|
|
818
|
+
if (results.length === 1 || results.length > 1 && results[1].score - results[0].score > 0.2) {
|
|
819
|
+
return [
|
|
820
|
+
2,
|
|
821
|
+
context.json({
|
|
822
|
+
brains: [
|
|
823
|
+
results[0].item
|
|
824
|
+
],
|
|
825
|
+
count: 1
|
|
826
|
+
})
|
|
827
|
+
];
|
|
828
|
+
}
|
|
829
|
+
// Return all matching results, sorted by score (best first)
|
|
757
830
|
return [
|
|
758
831
|
2,
|
|
759
832
|
context.json({
|
|
760
|
-
brains:
|
|
761
|
-
|
|
833
|
+
brains: results.map(function(r) {
|
|
834
|
+
return r.item;
|
|
835
|
+
}),
|
|
836
|
+
count: results.length
|
|
762
837
|
})
|
|
763
838
|
];
|
|
764
839
|
}
|
|
@@ -625,28 +625,87 @@ export var BrainRunnerDO = /*#__PURE__*/ function(DurableObject) {
|
|
|
625
625
|
key: "kill",
|
|
626
626
|
value: function kill() {
|
|
627
627
|
return _async_to_generator(function() {
|
|
628
|
+
var sql, startEventResult, startEvent, actualBrainRunId, monitorStub, existingRun, cancelledEvent;
|
|
628
629
|
return _ts_generator(this, function(_state) {
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
630
|
+
switch(_state.label){
|
|
631
|
+
case 0:
|
|
632
|
+
// If brain is actively running, abort it
|
|
633
|
+
if (this.abortController && !this.abortController.signal.aborted) {
|
|
634
|
+
this.abortController.abort();
|
|
635
|
+
return [
|
|
636
|
+
2,
|
|
637
|
+
{
|
|
638
|
+
success: true,
|
|
639
|
+
message: 'Brain run kill signal sent'
|
|
640
|
+
}
|
|
641
|
+
];
|
|
636
642
|
}
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
643
|
+
// Brain is not actively running - it might be suspended for a webhook
|
|
644
|
+
// First, get the actual brainRunId from the stored START event
|
|
645
|
+
// (this.brainRunId is the DO's internal ID, not the UUID brainRunId)
|
|
646
|
+
sql = this.sql;
|
|
647
|
+
startEventResult = sql.exec("SELECT serialized_event FROM brain_events WHERE event_type IN (?, ?) ORDER BY event_id DESC LIMIT 1", BRAIN_EVENTS.START, BRAIN_EVENTS.RESTART).toArray();
|
|
648
|
+
if (startEventResult.length === 0) {
|
|
649
|
+
return [
|
|
650
|
+
2,
|
|
651
|
+
{
|
|
652
|
+
success: false,
|
|
653
|
+
message: 'Brain run not found or never started'
|
|
654
|
+
}
|
|
655
|
+
];
|
|
644
656
|
}
|
|
645
|
-
|
|
657
|
+
startEvent = JSON.parse(startEventResult[0].serialized_event);
|
|
658
|
+
actualBrainRunId = startEvent.brainRunId;
|
|
659
|
+
// Check if it's still in RUNNING status (which means it's suspended)
|
|
660
|
+
monitorStub = this.env.MONITOR_DO.get(this.env.MONITOR_DO.idFromName('singleton'));
|
|
661
|
+
return [
|
|
662
|
+
4,
|
|
663
|
+
monitorStub.getLastEvent(actualBrainRunId)
|
|
664
|
+
];
|
|
665
|
+
case 1:
|
|
666
|
+
existingRun = _state.sent();
|
|
667
|
+
if (!existingRun) {
|
|
668
|
+
return [
|
|
669
|
+
2,
|
|
670
|
+
{
|
|
671
|
+
success: false,
|
|
672
|
+
message: 'Brain run not found in monitor'
|
|
673
|
+
}
|
|
674
|
+
];
|
|
675
|
+
}
|
|
676
|
+
// If already completed/cancelled/errored, nothing to do
|
|
677
|
+
if (existingRun.status !== STATUS.RUNNING) {
|
|
678
|
+
return [
|
|
679
|
+
2,
|
|
680
|
+
{
|
|
681
|
+
success: false,
|
|
682
|
+
message: 'Brain run is not active or already completed'
|
|
683
|
+
}
|
|
684
|
+
];
|
|
685
|
+
}
|
|
686
|
+
// Brain is suspended for a webhook - emit CANCELLED event to MonitorDO
|
|
687
|
+
cancelledEvent = {
|
|
688
|
+
type: BRAIN_EVENTS.CANCELLED,
|
|
689
|
+
status: STATUS.CANCELLED,
|
|
690
|
+
brainTitle: startEvent.brainTitle,
|
|
691
|
+
brainDescription: startEvent.brainDescription || '',
|
|
692
|
+
brainRunId: actualBrainRunId,
|
|
693
|
+
options: startEvent.options || {}
|
|
694
|
+
};
|
|
695
|
+
return [
|
|
696
|
+
4,
|
|
697
|
+
monitorStub.handleBrainEvent(cancelledEvent)
|
|
698
|
+
];
|
|
699
|
+
case 2:
|
|
700
|
+
_state.sent();
|
|
701
|
+
return [
|
|
702
|
+
2,
|
|
703
|
+
{
|
|
704
|
+
success: true,
|
|
705
|
+
message: 'Suspended brain run cancelled'
|
|
706
|
+
}
|
|
707
|
+
];
|
|
646
708
|
}
|
|
647
|
-
return [
|
|
648
|
-
2
|
|
649
|
-
];
|
|
650
709
|
});
|
|
651
710
|
}).call(this);
|
|
652
711
|
}
|
package/dist/types/api.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../src/api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAgB,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../src/api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAgB,MAAM,MAAM,CAAC;AAK1C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAE1D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,KAAK,EAAE,QAAQ,EAAY,MAAM,2BAA2B,CAAC;AAGpE,KAAK,QAAQ,GAAG;IACd,eAAe,EAAE,sBAAsB,CAAC,aAAa,CAAC,CAAC;IACvD,UAAU,EAAE,sBAAsB,CAAC,SAAS,CAAC,CAAC;IAC9C,WAAW,EAAE,sBAAsB,CAAC,UAAU,CAAC,CAAC;IAChD,gBAAgB,EAAE,QAAQ,CAAC;IAC3B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,qBAAq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B,CAAC,UAAU,CAAC,CAAC;IAChD,gBAAgB,EAAE,QAAQ,CAAC;IAC3B,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAwDD,qBAAa,aAAc,SAAQ,aAAa,CAAC,GAAG,CAAC;IACnD,OAAO,CAAC,GAAG,CAAa;IACxB,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,kBAAkB,CAA4B;IACtD,OAAO,CAAC,eAAe,CAAgC;gBAE3C,KAAK,EAAE,kBAAkB,EAAE,GAAG,EAAE,GAAG;YAOjC,mBAAmB;IAqEjC;;;OAGG;IACH,OAAO,CAAC,eAAe;IAiBjB,IAAI,IAAI,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;IAwDtD,KAAK,CACT,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,MAAM,EAClB,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAuG7B,MAAM,CACV,UAAU,EAAE,MAAM,EAClB,eAAe,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAmJhC,KAAK,CAAC,OAAO,EAAE,OAAO;CA4E7B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@positronic/cloudflare",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.40",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -31,13 +31,14 @@
|
|
|
31
31
|
"clean": "rm -rf tsconfig.tsbuildinfo dist"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@positronic/core": "^0.0.
|
|
35
|
-
"@positronic/spec": "^0.0.
|
|
36
|
-
"@positronic/template-new-project": "^0.0.
|
|
34
|
+
"@positronic/core": "^0.0.40",
|
|
35
|
+
"@positronic/spec": "^0.0.40",
|
|
36
|
+
"@positronic/template-new-project": "^0.0.40",
|
|
37
37
|
"aws4fetch": "^1.0.18",
|
|
38
38
|
"caz": "^2.0.0",
|
|
39
39
|
"cron-schedule": "^5.0.4",
|
|
40
40
|
"dotenv": "^16.0.3",
|
|
41
|
+
"fuse.js": "^7.1.0",
|
|
41
42
|
"hono": "^4.2.3",
|
|
42
43
|
"uuid": "^9.0.1"
|
|
43
44
|
},
|