@mastra/core 0.24.6 → 0.24.7-alpha.1
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/CHANGELOG.md +54 -0
- package/dist/agent/index.cjs +13 -13
- package/dist/agent/index.js +2 -2
- package/dist/agent/input-processor/index.cjs +6 -6
- package/dist/agent/input-processor/index.js +1 -1
- package/dist/agent/message-list/index.d.ts.map +1 -1
- package/dist/ai-tracing/index.cjs +36 -36
- package/dist/ai-tracing/index.js +1 -1
- package/dist/{chunk-XWGHD4C7.cjs → chunk-22DLIF7L.cjs} +5 -5
- package/dist/{chunk-XWGHD4C7.cjs.map → chunk-22DLIF7L.cjs.map} +1 -1
- package/dist/{chunk-PAOBGBU7.cjs → chunk-3IDFSGG3.cjs} +4 -4
- package/dist/{chunk-PAOBGBU7.cjs.map → chunk-3IDFSGG3.cjs.map} +1 -1
- package/dist/{chunk-42RUESSD.cjs → chunk-4YYPQOP7.cjs} +9 -4
- package/dist/{chunk-42RUESSD.cjs.map → chunk-4YYPQOP7.cjs.map} +1 -1
- package/dist/{chunk-AQGLVU53.cjs → chunk-5PSD3IKG.cjs} +19 -6
- package/dist/chunk-5PSD3IKG.cjs.map +1 -0
- package/dist/{chunk-LAQQETGP.js → chunk-5UWR4YYL.js} +33 -28
- package/dist/chunk-5UWR4YYL.js.map +1 -0
- package/dist/{chunk-RJOVFHWN.cjs → chunk-6DFSLVVY.cjs} +34 -7
- package/dist/chunk-6DFSLVVY.cjs.map +1 -0
- package/dist/{chunk-6D2K2CAA.js → chunk-6VB52TJV.js} +3 -3
- package/dist/{chunk-6D2K2CAA.js.map → chunk-6VB52TJV.js.map} +1 -1
- package/dist/{chunk-VXHOOZSK.js → chunk-72F4RGK7.js} +3 -3
- package/dist/{chunk-VXHOOZSK.js.map → chunk-72F4RGK7.js.map} +1 -1
- package/dist/{chunk-XDMQQZNX.cjs → chunk-AHQOJZT2.cjs} +74 -66
- package/dist/chunk-AHQOJZT2.cjs.map +1 -0
- package/dist/{chunk-JVV5LREI.js → chunk-BKBVQTOH.js} +2 -2
- package/dist/chunk-BKBVQTOH.js.map +1 -0
- package/dist/{chunk-DMBN72QF.cjs → chunk-DISETAP2.cjs} +4 -4
- package/dist/{chunk-DMBN72QF.cjs.map → chunk-DISETAP2.cjs.map} +1 -1
- package/dist/{chunk-BLUDYAPI.js → chunk-DSUKMFZY.js} +19 -6
- package/dist/chunk-DSUKMFZY.js.map +1 -0
- package/dist/{chunk-NRQC7DQW.js → chunk-DYJA57WK.js} +55 -7
- package/dist/chunk-DYJA57WK.js.map +1 -0
- package/dist/{chunk-Q6LWNLAJ.js → chunk-GIK7MM2K.js} +8 -3
- package/dist/{chunk-Q6LWNLAJ.js.map → chunk-GIK7MM2K.js.map} +1 -1
- package/dist/{chunk-DHLW4AP7.js → chunk-GOEAJJED.js} +3 -3
- package/dist/{chunk-DHLW4AP7.js.map → chunk-GOEAJJED.js.map} +1 -1
- package/dist/{chunk-GZDIHQDK.cjs → chunk-GWA56ELO.cjs} +4 -4
- package/dist/{chunk-GZDIHQDK.cjs.map → chunk-GWA56ELO.cjs.map} +1 -1
- package/dist/{chunk-CT2DMHGC.cjs → chunk-ICICKWAG.cjs} +8 -8
- package/dist/{chunk-CT2DMHGC.cjs.map → chunk-ICICKWAG.cjs.map} +1 -1
- package/dist/{chunk-Z6QCWTTO.js → chunk-KQGP7VMN.js} +145 -19
- package/dist/chunk-KQGP7VMN.js.map +1 -0
- package/dist/{chunk-INOOZ2A2.js → chunk-PHSTPDWR.js} +33 -6
- package/dist/chunk-PHSTPDWR.js.map +1 -0
- package/dist/{chunk-WIMFJ2BA.js → chunk-PPAIPZ6C.js} +51 -13
- package/dist/chunk-PPAIPZ6C.js.map +1 -0
- package/dist/{chunk-UWTYVVVZ.js → chunk-RLYWCJQ6.js} +3 -3
- package/dist/{chunk-UWTYVVVZ.js.map → chunk-RLYWCJQ6.js.map} +1 -1
- package/dist/{chunk-HCCXJ5YJ.js → chunk-RP4YCZP7.js} +3 -3
- package/dist/{chunk-HCCXJ5YJ.js.map → chunk-RP4YCZP7.js.map} +1 -1
- package/dist/{chunk-CYEQK4PM.cjs → chunk-RQPGIUUA.cjs} +6 -6
- package/dist/{chunk-CYEQK4PM.cjs.map → chunk-RQPGIUUA.cjs.map} +1 -1
- package/dist/{chunk-46XGIEXM.cjs → chunk-RWPKCFZT.cjs} +4 -4
- package/dist/{chunk-46XGIEXM.cjs.map → chunk-RWPKCFZT.cjs.map} +1 -1
- package/dist/{chunk-KWF3J2Q4.cjs → chunk-SJ3PJ5D6.cjs} +11 -11
- package/dist/{chunk-KWF3J2Q4.cjs.map → chunk-SJ3PJ5D6.cjs.map} +1 -1
- package/dist/{chunk-ICF3MCIN.cjs → chunk-TR5CDVHO.cjs} +65 -17
- package/dist/chunk-TR5CDVHO.cjs.map +1 -0
- package/dist/{chunk-PIH5FBNQ.cjs → chunk-U57JN7JY.cjs} +154 -28
- package/dist/chunk-U57JN7JY.cjs.map +1 -0
- package/dist/{chunk-NPNGPMT2.js → chunk-V2IMZSNO.js} +3 -3
- package/dist/{chunk-NPNGPMT2.js.map → chunk-V2IMZSNO.js.map} +1 -1
- package/dist/{chunk-IAJHRFO4.cjs → chunk-YPYLWTZI.cjs} +60 -21
- package/dist/chunk-YPYLWTZI.cjs.map +1 -0
- package/dist/{chunk-ZIWN73GQ.js → chunk-Z6ZIJYKK.js} +5 -5
- package/dist/{chunk-ZIWN73GQ.js.map → chunk-Z6ZIJYKK.js.map} +1 -1
- package/dist/{chunk-MRSBLBQ5.js → chunk-ZOQ6Q5Q3.js} +4 -4
- package/dist/{chunk-MRSBLBQ5.js.map → chunk-ZOQ6Q5Q3.js.map} +1 -1
- package/dist/{chunk-TX4TTPYJ.cjs → chunk-ZZ2DSE2G.cjs} +2 -2
- package/dist/chunk-ZZ2DSE2G.cjs.map +1 -0
- package/dist/index.cjs +96 -64
- package/dist/index.js +13 -13
- package/dist/llm/index.cjs +8 -8
- package/dist/llm/index.js +2 -2
- package/dist/llm/model/provider-registry.d.ts.map +1 -1
- package/dist/loop/index.cjs +2 -2
- package/dist/loop/index.js +1 -1
- package/dist/loop/workflows/stream.d.ts +1 -1
- package/dist/loop/workflows/stream.d.ts.map +1 -1
- package/dist/mastra/index.cjs +2 -2
- package/dist/mastra/index.d.ts +3 -1
- package/dist/mastra/index.d.ts.map +1 -1
- package/dist/mastra/index.js +1 -1
- package/dist/memory/index.cjs +4 -4
- package/dist/memory/index.js +1 -1
- package/dist/processors/index.cjs +11 -11
- package/dist/processors/index.js +1 -1
- package/dist/provider-registry-254GJGUL.cjs +40 -0
- package/dist/{provider-registry-RUDYHG7J.cjs.map → provider-registry-254GJGUL.cjs.map} +1 -1
- package/dist/provider-registry-7BUYRBGO.js +3 -0
- package/dist/{provider-registry-THITZUJ7.js.map → provider-registry-7BUYRBGO.js.map} +1 -1
- package/dist/relevance/index.cjs +4 -4
- package/dist/relevance/index.js +1 -1
- package/dist/scores/index.cjs +9 -9
- package/dist/scores/index.js +2 -2
- package/dist/scores/scoreTraces/index.cjs +8 -8
- package/dist/scores/scoreTraces/index.js +3 -3
- package/dist/server/index.cjs +25 -36
- package/dist/server/index.cjs.map +1 -1
- package/dist/server/index.js +24 -35
- package/dist/server/index.js.map +1 -1
- package/dist/server/simple-auth.d.ts +13 -18
- package/dist/server/simple-auth.d.ts.map +1 -1
- package/dist/storage/base.d.ts +2 -9
- package/dist/storage/base.d.ts.map +1 -1
- package/dist/storage/domains/workflows/base.d.ts +2 -9
- package/dist/storage/domains/workflows/base.d.ts.map +1 -1
- package/dist/storage/domains/workflows/inmemory.d.ts +2 -9
- package/dist/storage/domains/workflows/inmemory.d.ts.map +1 -1
- package/dist/storage/index.cjs +31 -10
- package/dist/storage/index.cjs.map +1 -1
- package/dist/storage/index.js +25 -4
- package/dist/storage/index.js.map +1 -1
- package/dist/storage/types.d.ts +10 -1
- package/dist/storage/types.d.ts.map +1 -1
- package/dist/stream/aisdk/v5/transform.d.ts.map +1 -1
- package/dist/stream/index.cjs +11 -11
- package/dist/stream/index.js +2 -2
- package/dist/telemetry/index.cjs +7 -7
- package/dist/telemetry/index.js +1 -1
- package/dist/telemetry/telemetry.decorators.d.ts.map +1 -1
- package/dist/test-utils/llm-mock.cjs +2 -2
- package/dist/test-utils/llm-mock.js +1 -1
- package/dist/tts/index.cjs +2 -2
- package/dist/tts/index.js +1 -1
- package/dist/utils.cjs +37 -17
- package/dist/utils.d.ts +32 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +1 -1
- package/dist/voice/index.cjs +6 -6
- package/dist/voice/index.js +1 -1
- package/dist/workflows/default.d.ts +26 -8
- package/dist/workflows/default.d.ts.map +1 -1
- package/dist/workflows/evented/execution-engine.d.ts +5 -1
- package/dist/workflows/evented/execution-engine.d.ts.map +1 -1
- package/dist/workflows/evented/index.cjs +10 -10
- package/dist/workflows/evented/index.js +1 -1
- package/dist/workflows/evented/workflow-event-processor/index.d.ts +5 -4
- package/dist/workflows/evented/workflow-event-processor/index.d.ts.map +1 -1
- package/dist/workflows/evented/workflow-event-processor/loop.d.ts +1 -1
- package/dist/workflows/evented/workflow-event-processor/loop.d.ts.map +1 -1
- package/dist/workflows/evented/workflow-event-processor/parallel.d.ts +2 -2
- package/dist/workflows/evented/workflow-event-processor/parallel.d.ts.map +1 -1
- package/dist/workflows/evented/workflow-event-processor/sleep.d.ts +2 -2
- package/dist/workflows/evented/workflow-event-processor/sleep.d.ts.map +1 -1
- package/dist/workflows/evented/workflow.d.ts +2 -1
- package/dist/workflows/evented/workflow.d.ts.map +1 -1
- package/dist/workflows/execution-engine.d.ts +5 -3
- package/dist/workflows/execution-engine.d.ts.map +1 -1
- package/dist/workflows/index.cjs +26 -14
- package/dist/workflows/index.js +1 -1
- package/dist/workflows/legacy/index.cjs +22 -22
- package/dist/workflows/legacy/index.js +1 -1
- package/dist/workflows/step.d.ts +1 -0
- package/dist/workflows/step.d.ts.map +1 -1
- package/dist/workflows/types.d.ts +51 -1
- package/dist/workflows/types.d.ts.map +1 -1
- package/dist/workflows/utils.d.ts +20 -0
- package/dist/workflows/utils.d.ts.map +1 -1
- package/dist/workflows/workflow.d.ts +98 -12
- package/dist/workflows/workflow.d.ts.map +1 -1
- package/package.json +5 -5
- package/dist/chunk-AQGLVU53.cjs.map +0 -1
- package/dist/chunk-BLUDYAPI.js.map +0 -1
- package/dist/chunk-IAJHRFO4.cjs.map +0 -1
- package/dist/chunk-ICF3MCIN.cjs.map +0 -1
- package/dist/chunk-INOOZ2A2.js.map +0 -1
- package/dist/chunk-JVV5LREI.js.map +0 -1
- package/dist/chunk-LAQQETGP.js.map +0 -1
- package/dist/chunk-NRQC7DQW.js.map +0 -1
- package/dist/chunk-PIH5FBNQ.cjs.map +0 -1
- package/dist/chunk-RJOVFHWN.cjs.map +0 -1
- package/dist/chunk-TX4TTPYJ.cjs.map +0 -1
- package/dist/chunk-WIMFJ2BA.js.map +0 -1
- package/dist/chunk-XDMQQZNX.cjs.map +0 -1
- package/dist/chunk-Z6QCWTTO.js.map +0 -1
- package/dist/provider-registry-RUDYHG7J.cjs +0 -40
- package/dist/provider-registry-THITZUJ7.js +0 -3
|
@@ -1,48 +1,48 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkAHQOJZT2_cjs = require('../chunk-AHQOJZT2.cjs');
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
Object.defineProperty(exports, "BatchPartsProcessor", {
|
|
8
8
|
enumerable: true,
|
|
9
|
-
get: function () { return
|
|
9
|
+
get: function () { return chunkAHQOJZT2_cjs.BatchPartsProcessor; }
|
|
10
10
|
});
|
|
11
11
|
Object.defineProperty(exports, "LanguageDetector", {
|
|
12
12
|
enumerable: true,
|
|
13
|
-
get: function () { return
|
|
13
|
+
get: function () { return chunkAHQOJZT2_cjs.LanguageDetector; }
|
|
14
14
|
});
|
|
15
15
|
Object.defineProperty(exports, "ModerationProcessor", {
|
|
16
16
|
enumerable: true,
|
|
17
|
-
get: function () { return
|
|
17
|
+
get: function () { return chunkAHQOJZT2_cjs.ModerationProcessor; }
|
|
18
18
|
});
|
|
19
19
|
Object.defineProperty(exports, "PIIDetector", {
|
|
20
20
|
enumerable: true,
|
|
21
|
-
get: function () { return
|
|
21
|
+
get: function () { return chunkAHQOJZT2_cjs.PIIDetector; }
|
|
22
22
|
});
|
|
23
23
|
Object.defineProperty(exports, "ProcessorState", {
|
|
24
24
|
enumerable: true,
|
|
25
|
-
get: function () { return
|
|
25
|
+
get: function () { return chunkAHQOJZT2_cjs.ProcessorState; }
|
|
26
26
|
});
|
|
27
27
|
Object.defineProperty(exports, "PromptInjectionDetector", {
|
|
28
28
|
enumerable: true,
|
|
29
|
-
get: function () { return
|
|
29
|
+
get: function () { return chunkAHQOJZT2_cjs.PromptInjectionDetector; }
|
|
30
30
|
});
|
|
31
31
|
Object.defineProperty(exports, "StructuredOutputProcessor", {
|
|
32
32
|
enumerable: true,
|
|
33
|
-
get: function () { return
|
|
33
|
+
get: function () { return chunkAHQOJZT2_cjs.StructuredOutputProcessor; }
|
|
34
34
|
});
|
|
35
35
|
Object.defineProperty(exports, "SystemPromptScrubber", {
|
|
36
36
|
enumerable: true,
|
|
37
|
-
get: function () { return
|
|
37
|
+
get: function () { return chunkAHQOJZT2_cjs.SystemPromptScrubber; }
|
|
38
38
|
});
|
|
39
39
|
Object.defineProperty(exports, "TokenLimiterProcessor", {
|
|
40
40
|
enumerable: true,
|
|
41
|
-
get: function () { return
|
|
41
|
+
get: function () { return chunkAHQOJZT2_cjs.TokenLimiterProcessor; }
|
|
42
42
|
});
|
|
43
43
|
Object.defineProperty(exports, "UnicodeNormalizer", {
|
|
44
44
|
enumerable: true,
|
|
45
|
-
get: function () { return
|
|
45
|
+
get: function () { return chunkAHQOJZT2_cjs.UnicodeNormalizer; }
|
|
46
46
|
});
|
|
47
47
|
//# sourceMappingURL=index.cjs.map
|
|
48
48
|
//# sourceMappingURL=index.cjs.map
|
package/dist/processors/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { BatchPartsProcessor, LanguageDetector, ModerationProcessor, PIIDetector, ProcessorState, PromptInjectionDetector, StructuredOutputProcessor, SystemPromptScrubber, TokenLimiterProcessor, UnicodeNormalizer } from '../chunk-
|
|
1
|
+
export { BatchPartsProcessor, LanguageDetector, ModerationProcessor, PIIDetector, ProcessorState, PromptInjectionDetector, StructuredOutputProcessor, SystemPromptScrubber, TokenLimiterProcessor, UnicodeNormalizer } from '../chunk-5UWR4YYL.js';
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
|
3
3
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunk6DFSLVVY_cjs = require('./chunk-6DFSLVVY.cjs');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
Object.defineProperty(exports, "GatewayRegistry", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function () { return chunk6DFSLVVY_cjs.GatewayRegistry; }
|
|
10
|
+
});
|
|
11
|
+
Object.defineProperty(exports, "PROVIDER_MODELS", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: function () { return chunk6DFSLVVY_cjs.PROVIDER_MODELS; }
|
|
14
|
+
});
|
|
15
|
+
Object.defineProperty(exports, "PROVIDER_REGISTRY", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
get: function () { return chunk6DFSLVVY_cjs.PROVIDER_REGISTRY; }
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports, "getProviderConfig", {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
get: function () { return chunk6DFSLVVY_cjs.getProviderConfig; }
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(exports, "getRegisteredProviders", {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
get: function () { return chunk6DFSLVVY_cjs.getRegisteredProviders; }
|
|
26
|
+
});
|
|
27
|
+
Object.defineProperty(exports, "isProviderRegistered", {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
get: function () { return chunk6DFSLVVY_cjs.isProviderRegistered; }
|
|
30
|
+
});
|
|
31
|
+
Object.defineProperty(exports, "isValidModelId", {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
get: function () { return chunk6DFSLVVY_cjs.isValidModelId; }
|
|
34
|
+
});
|
|
35
|
+
Object.defineProperty(exports, "parseModelString", {
|
|
36
|
+
enumerable: true,
|
|
37
|
+
get: function () { return chunk6DFSLVVY_cjs.parseModelString; }
|
|
38
|
+
});
|
|
39
|
+
//# sourceMappingURL=provider-registry-254GJGUL.cjs.map
|
|
40
|
+
//# sourceMappingURL=provider-registry-254GJGUL.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"names":[],"mappings":"","file":"provider-registry-
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"provider-registry-254GJGUL.cjs"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { GatewayRegistry, PROVIDER_MODELS, PROVIDER_REGISTRY, getProviderConfig, getRegisteredProviders, isProviderRegistered, isValidModelId, parseModelString } from './chunk-PHSTPDWR.js';
|
|
2
|
+
//# sourceMappingURL=provider-registry-7BUYRBGO.js.map
|
|
3
|
+
//# sourceMappingURL=provider-registry-7BUYRBGO.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"names":[],"mappings":"","file":"provider-registry-
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"provider-registry-7BUYRBGO.js"}
|
package/dist/relevance/index.cjs
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkGWA56ELO_cjs = require('../chunk-GWA56ELO.cjs');
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
Object.defineProperty(exports, "CohereRelevanceScorer", {
|
|
8
8
|
enumerable: true,
|
|
9
|
-
get: function () { return
|
|
9
|
+
get: function () { return chunkGWA56ELO_cjs.CohereRelevanceScorer; }
|
|
10
10
|
});
|
|
11
11
|
Object.defineProperty(exports, "MastraAgentRelevanceScorer", {
|
|
12
12
|
enumerable: true,
|
|
13
|
-
get: function () { return
|
|
13
|
+
get: function () { return chunkGWA56ELO_cjs.MastraAgentRelevanceScorer; }
|
|
14
14
|
});
|
|
15
15
|
Object.defineProperty(exports, "createSimilarityPrompt", {
|
|
16
16
|
enumerable: true,
|
|
17
|
-
get: function () { return
|
|
17
|
+
get: function () { return chunkGWA56ELO_cjs.createSimilarityPrompt; }
|
|
18
18
|
});
|
|
19
19
|
//# sourceMappingURL=index.cjs.map
|
|
20
20
|
//# sourceMappingURL=index.cjs.map
|
package/dist/relevance/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { CohereRelevanceScorer, MastraAgentRelevanceScorer, createSimilarityPrompt } from '../chunk-
|
|
1
|
+
export { CohereRelevanceScorer, MastraAgentRelevanceScorer, createSimilarityPrompt } from '../chunk-GOEAJJED.js';
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
|
3
3
|
//# sourceMappingURL=index.js.map
|
package/dist/scores/index.cjs
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var
|
|
3
|
+
var chunkSJ3PJ5D6_cjs = require('../chunk-SJ3PJ5D6.cjs');
|
|
4
|
+
var chunkRWPKCFZT_cjs = require('../chunk-RWPKCFZT.cjs');
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
Object.defineProperty(exports, "MastraScorer", {
|
|
9
9
|
enumerable: true,
|
|
10
|
-
get: function () { return
|
|
10
|
+
get: function () { return chunkSJ3PJ5D6_cjs.MastraScorer; }
|
|
11
11
|
});
|
|
12
12
|
Object.defineProperty(exports, "createScorer", {
|
|
13
13
|
enumerable: true,
|
|
14
|
-
get: function () { return
|
|
14
|
+
get: function () { return chunkSJ3PJ5D6_cjs.createScorer; }
|
|
15
15
|
});
|
|
16
16
|
Object.defineProperty(exports, "runExperiment", {
|
|
17
17
|
enumerable: true,
|
|
18
|
-
get: function () { return
|
|
18
|
+
get: function () { return chunkSJ3PJ5D6_cjs.runExperiment; }
|
|
19
19
|
});
|
|
20
20
|
Object.defineProperty(exports, "saveScorePayloadSchema", {
|
|
21
21
|
enumerable: true,
|
|
22
|
-
get: function () { return
|
|
22
|
+
get: function () { return chunkRWPKCFZT_cjs.saveScorePayloadSchema; }
|
|
23
23
|
});
|
|
24
24
|
Object.defineProperty(exports, "scoreResultSchema", {
|
|
25
25
|
enumerable: true,
|
|
26
|
-
get: function () { return
|
|
26
|
+
get: function () { return chunkRWPKCFZT_cjs.scoreResultSchema; }
|
|
27
27
|
});
|
|
28
28
|
Object.defineProperty(exports, "scoringExtractStepResultSchema", {
|
|
29
29
|
enumerable: true,
|
|
30
|
-
get: function () { return
|
|
30
|
+
get: function () { return chunkRWPKCFZT_cjs.scoringExtractStepResultSchema; }
|
|
31
31
|
});
|
|
32
32
|
Object.defineProperty(exports, "scoringValueSchema", {
|
|
33
33
|
enumerable: true,
|
|
34
|
-
get: function () { return
|
|
34
|
+
get: function () { return chunkRWPKCFZT_cjs.scoringValueSchema; }
|
|
35
35
|
});
|
|
36
36
|
//# sourceMappingURL=index.cjs.map
|
|
37
37
|
//# sourceMappingURL=index.cjs.map
|
package/dist/scores/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { MastraScorer, createScorer, runExperiment } from '../chunk-
|
|
2
|
-
export { saveScorePayloadSchema, scoreResultSchema, scoringExtractStepResultSchema, scoringValueSchema } from '../chunk-
|
|
1
|
+
export { MastraScorer, createScorer, runExperiment } from '../chunk-ZOQ6Q5Q3.js';
|
|
2
|
+
export { saveScorePayloadSchema, scoreResultSchema, scoringExtractStepResultSchema, scoringValueSchema } from '../chunk-RLYWCJQ6.js';
|
|
3
3
|
//# sourceMappingURL=index.js.map
|
|
4
4
|
//# sourceMappingURL=index.js.map
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
3
|
+
var chunkU57JN7JY_cjs = require('../../chunk-U57JN7JY.cjs');
|
|
4
|
+
var chunkRWPKCFZT_cjs = require('../../chunk-RWPKCFZT.cjs');
|
|
5
|
+
var chunk4YYPQOP7_cjs = require('../../chunk-4YYPQOP7.cjs');
|
|
6
6
|
var chunk5NTO7S5I_cjs = require('../../chunk-5NTO7S5I.cjs');
|
|
7
7
|
var pMap = require('p-map');
|
|
8
8
|
var z = require('zod');
|
|
@@ -76,7 +76,7 @@ function normalizeMessageContent(content) {
|
|
|
76
76
|
role: "user",
|
|
77
77
|
parts: content.map((part) => ({ type: part.type, text: part.text }))
|
|
78
78
|
};
|
|
79
|
-
const converted =
|
|
79
|
+
const converted = chunk4YYPQOP7_cjs.convertMessages(tempMessage).to("AIV4.UI");
|
|
80
80
|
return converted[0]?.content || "";
|
|
81
81
|
}
|
|
82
82
|
function convertToUIMessage(message, createdAt) {
|
|
@@ -94,7 +94,7 @@ function convertToUIMessage(message, createdAt) {
|
|
|
94
94
|
parts: message.content.map((part) => ({ type: part.type, text: part.text }))
|
|
95
95
|
};
|
|
96
96
|
}
|
|
97
|
-
const converted =
|
|
97
|
+
const converted = chunk4YYPQOP7_cjs.convertMessages(messageInput).to("AIV4.UI");
|
|
98
98
|
const result = converted[0];
|
|
99
99
|
if (!result) {
|
|
100
100
|
throw new Error("Failed to convert message");
|
|
@@ -234,7 +234,7 @@ function transformTraceToScorerInputAndOutput(trace) {
|
|
|
234
234
|
}
|
|
235
235
|
|
|
236
236
|
// src/scores/scoreTraces/scoreTracesWorkflow.ts
|
|
237
|
-
var getTraceStep =
|
|
237
|
+
var getTraceStep = chunkU57JN7JY_cjs.createStep({
|
|
238
238
|
id: "__process-trace-scoring",
|
|
239
239
|
inputSchema: z__default.default.object({
|
|
240
240
|
targets: z__default.default.array(
|
|
@@ -362,7 +362,7 @@ async function runScorerOnTarget({
|
|
|
362
362
|
await attachScoreToSpan({ storage, span, scoreRecord: savedScoreRecord });
|
|
363
363
|
}
|
|
364
364
|
async function validateAndSaveScore({ storage, scorerResult }) {
|
|
365
|
-
const payloadToSave =
|
|
365
|
+
const payloadToSave = chunkRWPKCFZT_cjs.saveScorePayloadSchema.parse(scorerResult);
|
|
366
366
|
const result = await storage.saveScore(payloadToSave);
|
|
367
367
|
return result.score;
|
|
368
368
|
}
|
|
@@ -404,7 +404,7 @@ async function attachScoreToSpan({
|
|
|
404
404
|
updates: { links: [...existingLinks, link] }
|
|
405
405
|
});
|
|
406
406
|
}
|
|
407
|
-
var scoreTracesWorkflow =
|
|
407
|
+
var scoreTracesWorkflow = chunkU57JN7JY_cjs.createWorkflow({
|
|
408
408
|
id: "__batch-scoring-traces",
|
|
409
409
|
inputSchema: z__default.default.object({
|
|
410
410
|
targets: z__default.default.array(
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { createStep, createWorkflow } from '../../chunk-
|
|
2
|
-
import { saveScorePayloadSchema } from '../../chunk-
|
|
3
|
-
import { convertMessages } from '../../chunk-
|
|
1
|
+
import { createStep, createWorkflow } from '../../chunk-KQGP7VMN.js';
|
|
2
|
+
import { saveScorePayloadSchema } from '../../chunk-RLYWCJQ6.js';
|
|
3
|
+
import { convertMessages } from '../../chunk-GIK7MM2K.js';
|
|
4
4
|
import { MastraError } from '../../chunk-PZUZNPFM.js';
|
|
5
5
|
import pMap from 'p-map';
|
|
6
6
|
import z from 'zod';
|
package/dist/server/index.cjs
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunk5PSD3IKG_cjs = require('../chunk-5PSD3IKG.cjs');
|
|
4
4
|
var chunk5NTO7S5I_cjs = require('../chunk-5NTO7S5I.cjs');
|
|
5
5
|
var chunk6VOPKVYH_cjs = require('../chunk-6VOPKVYH.cjs');
|
|
6
6
|
var chunkEBVYYC2Q_cjs = require('../chunk-EBVYYC2Q.cjs');
|
|
7
7
|
|
|
8
8
|
// src/server/auth.ts
|
|
9
9
|
var _MastraAuthProvider_decorators, _init, _a;
|
|
10
|
-
_MastraAuthProvider_decorators = [
|
|
10
|
+
_MastraAuthProvider_decorators = [chunk5PSD3IKG_cjs.InstrumentClass({
|
|
11
11
|
prefix: "auth",
|
|
12
12
|
excludeMethods: ["__setTools", "__setLogger", "__setTelemetry", "#log"]
|
|
13
13
|
})];
|
|
@@ -75,53 +75,42 @@ var CompositeAuth = class extends exports.MastraAuthProvider {
|
|
|
75
75
|
};
|
|
76
76
|
|
|
77
77
|
// src/server/simple-auth.ts
|
|
78
|
+
var DEFAULT_HEADERS = ["Authorization", "X-Playground-Access"];
|
|
78
79
|
var SimpleAuth = class extends exports.MastraAuthProvider {
|
|
79
80
|
tokens;
|
|
80
|
-
|
|
81
|
-
|
|
81
|
+
headers;
|
|
82
|
+
users;
|
|
82
83
|
constructor(options) {
|
|
83
84
|
super(options);
|
|
84
85
|
this.tokens = options.tokens;
|
|
85
|
-
this.
|
|
86
|
-
this.
|
|
86
|
+
this.users = Object.values(this.tokens);
|
|
87
|
+
this.headers = [...DEFAULT_HEADERS].concat(options.headers || []);
|
|
87
88
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
89
|
+
async authenticateToken(token, request) {
|
|
90
|
+
const requestTokens = this.getTokensFromHeaders(token, request);
|
|
91
|
+
for (const requestToken of requestTokens) {
|
|
92
|
+
const tokenToUser = this.tokens[requestToken];
|
|
93
|
+
if (tokenToUser) {
|
|
94
|
+
return tokenToUser;
|
|
95
|
+
}
|
|
91
96
|
}
|
|
92
|
-
return
|
|
97
|
+
return null;
|
|
93
98
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
return
|
|
99
|
+
async authorizeUser(user, _request) {
|
|
100
|
+
return this.users.includes(user);
|
|
101
|
+
}
|
|
102
|
+
stripBearerPrefix(token) {
|
|
103
|
+
return token.startsWith("Bearer ") ? token.slice(7) : token;
|
|
99
104
|
}
|
|
100
|
-
|
|
101
|
-
|
|
105
|
+
getTokensFromHeaders(token, request) {
|
|
106
|
+
const tokens = [token];
|
|
107
|
+
for (const headerName of this.headers) {
|
|
102
108
|
const headerValue = request.header(headerName);
|
|
103
109
|
if (headerValue) {
|
|
104
|
-
|
|
105
|
-
return this.extractBearerToken(headerValue);
|
|
106
|
-
}
|
|
107
|
-
return headerValue;
|
|
110
|
+
tokens.push(this.stripBearerPrefix(headerValue));
|
|
108
111
|
}
|
|
109
112
|
}
|
|
110
|
-
return
|
|
111
|
-
}
|
|
112
|
-
async authenticateToken(token, request) {
|
|
113
|
-
const directToken = this.extractBearerToken(token);
|
|
114
|
-
if (directToken in this.tokens) {
|
|
115
|
-
return this.tokens[directToken];
|
|
116
|
-
}
|
|
117
|
-
const headerToken = this.findTokenInHeaders(request);
|
|
118
|
-
if (headerToken && headerToken in this.tokens) {
|
|
119
|
-
return this.tokens[headerToken];
|
|
120
|
-
}
|
|
121
|
-
return null;
|
|
122
|
-
}
|
|
123
|
-
async authorizeUser(user, _request) {
|
|
124
|
-
return this.authenticatedUsers.has(user);
|
|
113
|
+
return tokens;
|
|
125
114
|
}
|
|
126
115
|
};
|
|
127
116
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/server/auth.ts","../../src/server/composite-auth.ts","../../src/server/simple-auth.ts","../../src/server/index.ts"],"names":["MastraAuthProvider","__decoratorStart","__decorateElement","__runInitializers","MastraError"],"mappings":";;;;;;;;;;;;;;;;;;AAAA,MAAA,SAAA,EAAA,MAAA;AAkBA,MAAA,IAAA,EAAA,OAAA,EAAA;AAAiB,KACf,CAAA;AAAQ,IACR,IAAA,OAAA,EAAA,aAAiB,EAAA;AACnB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AA;AAAA,GAAA;AACM;AACN,mBACL,CAAA,MAAQ,EAAA;AAAA,EAAA;AACE","file":"index.cjs","sourcesContent":["import type { HonoRequest } from 'hono';\nimport { MastraBase } from '../base';\nimport { InstrumentClass } from '../telemetry';\nimport type { MastraAuthConfig } from './types';\n\nexport interface MastraAuthProviderOptions<TUser = unknown> {\n name?: string;\n authorizeUser?: (user: TUser, request: HonoRequest) => Promise<boolean> | boolean;\n /**\n * Protected paths for the auth provider\n */\n protected?: MastraAuthConfig['protected'];\n /**\n * Public paths for the auth provider\n */\n public?: MastraAuthConfig['public'];\n}\n\n@InstrumentClass({\n prefix: 'auth',\n excludeMethods: ['__setTools', '__setLogger', '__setTelemetry', '#log'],\n})\nexport abstract class MastraAuthProvider<TUser = unknown> extends MastraBase {\n public protected?: MastraAuthConfig['protected'];\n public public?: MastraAuthConfig['public'];\n\n constructor(options?: MastraAuthProviderOptions<TUser>) {\n super({ component: 'AUTH', name: options?.name });\n\n if (options?.authorizeUser) {\n this.authorizeUser = options.authorizeUser.bind(this);\n }\n\n this.protected = options?.protected;\n this.public = options?.public;\n }\n\n /**\n * Authenticate a token and return the payload\n * @param token - The token to authenticate\n * @param request - The request\n * @returns The payload\n */\n abstract authenticateToken(token: string, request: HonoRequest): Promise<TUser | null>;\n\n /**\n * Authorize a user for a path and method\n * @param user - The user to authorize\n * @param request - The request\n * @returns The authorization result\n */\n abstract authorizeUser(user: TUser, request: HonoRequest): Promise<boolean> | boolean;\n\n protected registerOptions(opts?: MastraAuthProviderOptions<TUser>) {\n if (opts?.authorizeUser) {\n this.authorizeUser = opts.authorizeUser.bind(this);\n }\n if (opts?.protected) {\n this.protected = opts.protected;\n }\n if (opts?.public) {\n this.public = opts.public;\n }\n }\n}\n","import type { HonoRequest } from 'hono';\nimport { MastraAuthProvider } from './auth';\n\nexport class CompositeAuth extends MastraAuthProvider {\n private providers: MastraAuthProvider[];\n\n constructor(providers: MastraAuthProvider[]) {\n super();\n this.providers = providers;\n }\n\n async authenticateToken(token: string, request: HonoRequest): Promise<unknown | null> {\n for (const provider of this.providers) {\n try {\n const user = await provider.authenticateToken(token, request);\n if (user) {\n return user;\n }\n } catch {\n // ignore error, try next provider\n }\n }\n return null;\n }\n\n async authorizeUser(user: unknown, request: HonoRequest): Promise<boolean> {\n for (const provider of this.providers) {\n const authorized = await provider.authorizeUser(user, request);\n if (authorized) {\n return true;\n }\n }\n return false;\n }\n}\n","import type { HonoRequest } from 'hono';\nimport { MastraAuthProvider } from './auth';\nimport type { MastraAuthProviderOptions } from './auth';\n\nexport interface SimpleAuthOptions<TUser = unknown> extends MastraAuthProviderOptions<TUser> {\n /**\n * A map of tokens to users.\n * When a token is provided, it will be looked up in this map.\n */\n tokens: Record<string, TUser>;\n /**\n * Headers to check for the token.\n * Defaults to 'Authorization' with Bearer token extraction.\n * Can be a string or array of strings for custom header names.\n */\n headers?: string | string[];\n}\n\n/**\n * SimpleAuth is a basic token-based authentication provider.\n * It validates tokens against a predefined map of tokens to users.\n */\nexport class SimpleAuth<TUser = unknown> extends MastraAuthProvider<TUser> {\n private tokens: Record<string, TUser>;\n private headerNames: string[];\n private authenticatedUsers: Set<TUser>;\n\n constructor(options: SimpleAuthOptions<TUser>) {\n super(options);\n this.tokens = options.tokens;\n this.headerNames = this.normalizeHeaders(options.headers);\n // Store reference to all valid users for authorization\n this.authenticatedUsers = new Set(Object.values(this.tokens));\n }\n\n private normalizeHeaders(headers?: string | string[]): string[] {\n if (!headers) {\n return ['Authorization'];\n }\n return Array.isArray(headers) ? headers : [headers];\n }\n\n private extractBearerToken(value: string): string {\n if (value.startsWith('Bearer ')) {\n return value.slice(7);\n }\n return value;\n }\n\n private findTokenInHeaders(request: HonoRequest): string | null {\n for (const headerName of this.headerNames) {\n const headerValue = request.header(headerName);\n if (headerValue) {\n // For Authorization header, extract Bearer token\n if (headerName.toLowerCase() === 'authorization') {\n return this.extractBearerToken(headerValue);\n }\n return headerValue;\n }\n }\n return null;\n }\n\n async authenticateToken(token: string, request: HonoRequest): Promise<TUser | null> {\n // First, try the direct token\n const directToken = this.extractBearerToken(token);\n if (directToken in this.tokens) {\n return this.tokens[directToken]!;\n }\n\n // Then, try to find token in headers\n const headerToken = this.findTokenInHeaders(request);\n if (headerToken && headerToken in this.tokens) {\n return this.tokens[headerToken]!;\n }\n\n return null;\n }\n\n async authorizeUser(user: TUser, _request: HonoRequest): Promise<boolean> {\n // Check if this user was authenticated through our tokens\n return this.authenticatedUsers.has(user);\n }\n}\n","import type { Context, Handler, MiddlewareHandler } from 'hono';\nimport type { DescribeRouteOptions } from 'hono-openapi';\nimport { MastraError, ErrorDomain, ErrorCategory } from '../error';\nimport type { Mastra } from '../mastra';\nimport type { ApiRoute, MastraAuthConfig, Methods } from './types';\n\nexport type { MastraAuthConfig, ContextWithMastra, ApiRoute } from './types';\nexport { MastraAuthProvider } from './auth';\nexport type { MastraAuthProviderOptions } from './auth';\nexport { CompositeAuth } from './composite-auth';\nexport { SimpleAuth } from './simple-auth';\nexport type { SimpleAuthOptions } from './simple-auth';\n\n// Helper type for inferring parameters from a path\n// Thank you Claude!\ntype ParamsFromPath<P extends string> = {\n [K in P extends `${string}:${infer Param}/${string}` | `${string}:${infer Param}` ? Param : never]: string;\n};\n\ntype RegisterApiRoutePathError = `Param 'path' must not start with '/api', it is reserved for internal API routes.`;\ntype ValidatePath<P extends string, T> = P extends `/api/${string}` ? RegisterApiRoutePathError : T;\n\ntype RegisterApiRouteOptions<P extends string> = {\n method: Methods;\n openapi?: DescribeRouteOptions;\n handler?: Handler<\n {\n Variables: {\n mastra: Mastra;\n };\n },\n P,\n ParamsFromPath<P>\n >;\n createHandler?: (c: Context) => Promise<\n Handler<\n {\n Variables: {\n mastra: Mastra;\n };\n },\n P,\n ParamsFromPath<P>\n >\n >;\n middleware?: MiddlewareHandler | MiddlewareHandler[];\n /**\n * When false, skips Mastra auth for this route (defaults to true)\n */\n requiresAuth?: boolean;\n};\n\nfunction validateOptions<P extends string>(\n path: P,\n options: RegisterApiRoutePathError | RegisterApiRouteOptions<P>,\n): asserts options is RegisterApiRouteOptions<P> {\n const opts = options as RegisterApiRouteOptions<P>;\n\n if (opts.method === undefined) {\n throw new MastraError({\n id: 'MASTRA_SERVER_API_INVALID_ROUTE_OPTIONS',\n text: `Invalid options for route \"${path}\", missing \"method\" property`,\n domain: ErrorDomain.MASTRA_SERVER,\n category: ErrorCategory.USER,\n });\n }\n\n if (opts.handler === undefined && opts.createHandler === undefined) {\n throw new MastraError({\n id: 'MASTRA_SERVER_API_INVALID_ROUTE_OPTIONS',\n text: `Invalid options for route \"${path}\", you must define a \"handler\" or \"createHandler\" property`,\n domain: ErrorDomain.MASTRA_SERVER,\n category: ErrorCategory.USER,\n });\n }\n\n if (opts.handler !== undefined && opts.createHandler !== undefined) {\n throw new MastraError({\n id: 'MASTRA_SERVER_API_INVALID_ROUTE_OPTIONS',\n text: `Invalid options for route \"${path}\", you can only define one of the following properties: \"handler\" or \"createHandler\"`,\n domain: ErrorDomain.MASTRA_SERVER,\n category: ErrorCategory.USER,\n });\n }\n}\n\nexport function registerApiRoute<P extends string>(\n path: P,\n options: ValidatePath<P, RegisterApiRouteOptions<P>>,\n): ValidatePath<P, ApiRoute> {\n if (path.startsWith('/api/')) {\n throw new MastraError({\n id: 'MASTRA_SERVER_API_PATH_RESERVED',\n text: 'Path must not start with \"/api\", it\\'s reserved for internal API routes',\n domain: ErrorDomain.MASTRA_SERVER,\n category: ErrorCategory.USER,\n });\n }\n\n validateOptions(path, options);\n\n return {\n path,\n method: options.method,\n handler: options.handler,\n createHandler: options.createHandler,\n openapi: options.openapi,\n middleware: options.middleware,\n requiresAuth: options.requiresAuth,\n } as unknown as ValidatePath<P, ApiRoute>;\n}\n\nexport function defineAuth<TUser>(config: MastraAuthConfig<TUser>): MastraAuthConfig<TUser> {\n return config;\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/server/auth.ts","../../src/server/composite-auth.ts","../../src/server/simple-auth.ts","../../src/server/index.ts"],"names":["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A,EAAA;AACE","file":"index.cjs","sourcesContent":["import type { HonoRequest } from 'hono';\nimport { MastraBase } from '../base';\nimport { InstrumentClass } from '../telemetry';\nimport type { MastraAuthConfig } from './types';\n\nexport interface MastraAuthProviderOptions<TUser = unknown> {\n name?: string;\n authorizeUser?: (user: TUser, request: HonoRequest) => Promise<boolean> | boolean;\n /**\n * Protected paths for the auth provider\n */\n protected?: MastraAuthConfig['protected'];\n /**\n * Public paths for the auth provider\n */\n public?: MastraAuthConfig['public'];\n}\n\n@InstrumentClass({\n prefix: 'auth',\n excludeMethods: ['__setTools', '__setLogger', '__setTelemetry', '#log'],\n})\nexport abstract class MastraAuthProvider<TUser = unknown> extends MastraBase {\n public protected?: MastraAuthConfig['protected'];\n public public?: MastraAuthConfig['public'];\n\n constructor(options?: MastraAuthProviderOptions<TUser>) {\n super({ component: 'AUTH', name: options?.name });\n\n if (options?.authorizeUser) {\n this.authorizeUser = options.authorizeUser.bind(this);\n }\n\n this.protected = options?.protected;\n this.public = options?.public;\n }\n\n /**\n * Authenticate a token and return the payload\n * @param token - The token to authenticate\n * @param request - The request\n * @returns The payload\n */\n abstract authenticateToken(token: string, request: HonoRequest): Promise<TUser | null>;\n\n /**\n * Authorize a user for a path and method\n * @param user - The user to authorize\n * @param request - The request\n * @returns The authorization result\n */\n abstract authorizeUser(user: TUser, request: HonoRequest): Promise<boolean> | boolean;\n\n protected registerOptions(opts?: MastraAuthProviderOptions<TUser>) {\n if (opts?.authorizeUser) {\n this.authorizeUser = opts.authorizeUser.bind(this);\n }\n if (opts?.protected) {\n this.protected = opts.protected;\n }\n if (opts?.public) {\n this.public = opts.public;\n }\n }\n}\n","import type { HonoRequest } from 'hono';\nimport { MastraAuthProvider } from './auth';\n\nexport class CompositeAuth extends MastraAuthProvider {\n private providers: MastraAuthProvider[];\n\n constructor(providers: MastraAuthProvider[]) {\n super();\n this.providers = providers;\n }\n\n async authenticateToken(token: string, request: HonoRequest): Promise<unknown | null> {\n for (const provider of this.providers) {\n try {\n const user = await provider.authenticateToken(token, request);\n if (user) {\n return user;\n }\n } catch {\n // ignore error, try next provider\n }\n }\n return null;\n }\n\n async authorizeUser(user: unknown, request: HonoRequest): Promise<boolean> {\n for (const provider of this.providers) {\n const authorized = await provider.authorizeUser(user, request);\n if (authorized) {\n return true;\n }\n }\n return false;\n }\n}\n","import type { HonoRequest } from 'hono';\nimport type { MastraAuthProviderOptions } from './auth';\nimport { MastraAuthProvider } from './auth';\n\nconst DEFAULT_HEADERS = ['Authorization', 'X-Playground-Access'];\n\ntype TokenToUser<TUser> = Record<string, TUser>;\n\nexport interface SimpleAuthOptions<TUser> extends MastraAuthProviderOptions<TUser> {\n /**\n * Valid tokens to authenticate against\n */\n tokens: TokenToUser<TUser>;\n /**\n * Headers to check for authentication\n * @default ['Authorization', 'X-Playground-Access']\n */\n headers?: string | string[];\n}\n\nexport class SimpleAuth<TUser> extends MastraAuthProvider<TUser> {\n private tokens: TokenToUser<TUser>;\n private headers: string[];\n private users: TUser[];\n\n constructor(options: SimpleAuthOptions<TUser>) {\n super(options);\n this.tokens = options.tokens;\n this.users = Object.values(this.tokens);\n this.headers = [...DEFAULT_HEADERS].concat(options.headers || []);\n }\n\n async authenticateToken(token: string, request: HonoRequest): Promise<TUser | null> {\n const requestTokens = this.getTokensFromHeaders(token, request);\n\n for (const requestToken of requestTokens) {\n const tokenToUser = this.tokens[requestToken];\n if (tokenToUser) {\n return tokenToUser;\n }\n }\n\n return null;\n }\n\n async authorizeUser(user: TUser, _request: HonoRequest): Promise<boolean> {\n return this.users.includes(user);\n }\n\n private stripBearerPrefix(token: string): string {\n return token.startsWith('Bearer ') ? token.slice(7) : token;\n }\n\n private getTokensFromHeaders(token: string, request: HonoRequest): string[] {\n const tokens = [token];\n for (const headerName of this.headers) {\n const headerValue = request.header(headerName);\n if (headerValue) {\n tokens.push(this.stripBearerPrefix(headerValue));\n }\n }\n return tokens;\n }\n}\n","import type { Context, Handler, MiddlewareHandler } from 'hono';\nimport type { DescribeRouteOptions } from 'hono-openapi';\nimport { MastraError, ErrorDomain, ErrorCategory } from '../error';\nimport type { Mastra } from '../mastra';\nimport type { ApiRoute, MastraAuthConfig, Methods } from './types';\n\nexport type { MastraAuthConfig, ContextWithMastra, ApiRoute } from './types';\nexport { MastraAuthProvider } from './auth';\nexport type { MastraAuthProviderOptions } from './auth';\nexport { CompositeAuth } from './composite-auth';\nexport { SimpleAuth } from './simple-auth';\nexport type { SimpleAuthOptions } from './simple-auth';\n\n// Helper type for inferring parameters from a path\n// Thank you Claude!\ntype ParamsFromPath<P extends string> = {\n [K in P extends `${string}:${infer Param}/${string}` | `${string}:${infer Param}` ? Param : never]: string;\n};\n\ntype RegisterApiRoutePathError = `Param 'path' must not start with '/api', it is reserved for internal API routes.`;\ntype ValidatePath<P extends string, T> = P extends `/api/${string}` ? RegisterApiRoutePathError : T;\n\ntype RegisterApiRouteOptions<P extends string> = {\n method: Methods;\n openapi?: DescribeRouteOptions;\n handler?: Handler<\n {\n Variables: {\n mastra: Mastra;\n };\n },\n P,\n ParamsFromPath<P>\n >;\n createHandler?: (c: Context) => Promise<\n Handler<\n {\n Variables: {\n mastra: Mastra;\n };\n },\n P,\n ParamsFromPath<P>\n >\n >;\n middleware?: MiddlewareHandler | MiddlewareHandler[];\n /**\n * When false, skips Mastra auth for this route (defaults to true)\n */\n requiresAuth?: boolean;\n};\n\nfunction validateOptions<P extends string>(\n path: P,\n options: RegisterApiRoutePathError | RegisterApiRouteOptions<P>,\n): asserts options is RegisterApiRouteOptions<P> {\n const opts = options as RegisterApiRouteOptions<P>;\n\n if (opts.method === undefined) {\n throw new MastraError({\n id: 'MASTRA_SERVER_API_INVALID_ROUTE_OPTIONS',\n text: `Invalid options for route \"${path}\", missing \"method\" property`,\n domain: ErrorDomain.MASTRA_SERVER,\n category: ErrorCategory.USER,\n });\n }\n\n if (opts.handler === undefined && opts.createHandler === undefined) {\n throw new MastraError({\n id: 'MASTRA_SERVER_API_INVALID_ROUTE_OPTIONS',\n text: `Invalid options for route \"${path}\", you must define a \"handler\" or \"createHandler\" property`,\n domain: ErrorDomain.MASTRA_SERVER,\n category: ErrorCategory.USER,\n });\n }\n\n if (opts.handler !== undefined && opts.createHandler !== undefined) {\n throw new MastraError({\n id: 'MASTRA_SERVER_API_INVALID_ROUTE_OPTIONS',\n text: `Invalid options for route \"${path}\", you can only define one of the following properties: \"handler\" or \"createHandler\"`,\n domain: ErrorDomain.MASTRA_SERVER,\n category: ErrorCategory.USER,\n });\n }\n}\n\nexport function registerApiRoute<P extends string>(\n path: P,\n options: ValidatePath<P, RegisterApiRouteOptions<P>>,\n): ValidatePath<P, ApiRoute> {\n if (path.startsWith('/api/')) {\n throw new MastraError({\n id: 'MASTRA_SERVER_API_PATH_RESERVED',\n text: 'Path must not start with \"/api\", it\\'s reserved for internal API routes',\n domain: ErrorDomain.MASTRA_SERVER,\n category: ErrorCategory.USER,\n });\n }\n\n validateOptions(path, options);\n\n return {\n path,\n method: options.method,\n handler: options.handler,\n createHandler: options.createHandler,\n openapi: options.openapi,\n middleware: options.middleware,\n requiresAuth: options.requiresAuth,\n } as unknown as ValidatePath<P, ApiRoute>;\n}\n\nexport function defineAuth<TUser>(config: MastraAuthConfig<TUser>): MastraAuthConfig<TUser> {\n return config;\n}\n"]}
|
package/dist/server/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { InstrumentClass } from '../chunk-
|
|
1
|
+
import { InstrumentClass } from '../chunk-DSUKMFZY.js';
|
|
2
2
|
import { MastraError } from '../chunk-PZUZNPFM.js';
|
|
3
3
|
import { MastraBase } from '../chunk-VQASQG5D.js';
|
|
4
4
|
import { __decoratorStart, __decorateElement, __runInitializers } from '../chunk-3HXBPDKN.js';
|
|
@@ -73,53 +73,42 @@ var CompositeAuth = class extends MastraAuthProvider {
|
|
|
73
73
|
};
|
|
74
74
|
|
|
75
75
|
// src/server/simple-auth.ts
|
|
76
|
+
var DEFAULT_HEADERS = ["Authorization", "X-Playground-Access"];
|
|
76
77
|
var SimpleAuth = class extends MastraAuthProvider {
|
|
77
78
|
tokens;
|
|
78
|
-
|
|
79
|
-
|
|
79
|
+
headers;
|
|
80
|
+
users;
|
|
80
81
|
constructor(options) {
|
|
81
82
|
super(options);
|
|
82
83
|
this.tokens = options.tokens;
|
|
83
|
-
this.
|
|
84
|
-
this.
|
|
84
|
+
this.users = Object.values(this.tokens);
|
|
85
|
+
this.headers = [...DEFAULT_HEADERS].concat(options.headers || []);
|
|
85
86
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
87
|
+
async authenticateToken(token, request) {
|
|
88
|
+
const requestTokens = this.getTokensFromHeaders(token, request);
|
|
89
|
+
for (const requestToken of requestTokens) {
|
|
90
|
+
const tokenToUser = this.tokens[requestToken];
|
|
91
|
+
if (tokenToUser) {
|
|
92
|
+
return tokenToUser;
|
|
93
|
+
}
|
|
89
94
|
}
|
|
90
|
-
return
|
|
95
|
+
return null;
|
|
91
96
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
return
|
|
97
|
+
async authorizeUser(user, _request) {
|
|
98
|
+
return this.users.includes(user);
|
|
99
|
+
}
|
|
100
|
+
stripBearerPrefix(token) {
|
|
101
|
+
return token.startsWith("Bearer ") ? token.slice(7) : token;
|
|
97
102
|
}
|
|
98
|
-
|
|
99
|
-
|
|
103
|
+
getTokensFromHeaders(token, request) {
|
|
104
|
+
const tokens = [token];
|
|
105
|
+
for (const headerName of this.headers) {
|
|
100
106
|
const headerValue = request.header(headerName);
|
|
101
107
|
if (headerValue) {
|
|
102
|
-
|
|
103
|
-
return this.extractBearerToken(headerValue);
|
|
104
|
-
}
|
|
105
|
-
return headerValue;
|
|
108
|
+
tokens.push(this.stripBearerPrefix(headerValue));
|
|
106
109
|
}
|
|
107
110
|
}
|
|
108
|
-
return
|
|
109
|
-
}
|
|
110
|
-
async authenticateToken(token, request) {
|
|
111
|
-
const directToken = this.extractBearerToken(token);
|
|
112
|
-
if (directToken in this.tokens) {
|
|
113
|
-
return this.tokens[directToken];
|
|
114
|
-
}
|
|
115
|
-
const headerToken = this.findTokenInHeaders(request);
|
|
116
|
-
if (headerToken && headerToken in this.tokens) {
|
|
117
|
-
return this.tokens[headerToken];
|
|
118
|
-
}
|
|
119
|
-
return null;
|
|
120
|
-
}
|
|
121
|
-
async authorizeUser(user, _request) {
|
|
122
|
-
return this.authenticatedUsers.has(user);
|
|
111
|
+
return tokens;
|
|
123
112
|
}
|
|
124
113
|
};
|
|
125
114
|
|
package/dist/server/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/server/auth.ts","../../src/server/composite-auth.ts","../../src/server/simple-auth.ts","../../src/server/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,MAAA,SAAA,EAAA,MAAA;AAkBA,MAAA,IAAA,EAAA,OAAA,EAAA;AAAiB,KACf,CAAA;AAAQ,IACR,IAAA,OAAA,EAAA,aAAiB,EAAA;AACnB,MAAC,IAAA,CAAA,aAAA,GAAA,OAAA,CAAA,aAAA,CAAA,IAAA,CAAA,IAAA,CAAA;AACM,IAAe;AAAuD,IACpE,IAAA,CAAA,SAAA,GAAA,OAAA,EAAA,SAAA;AAAA,IACA,IAAA,CAAA,MAAA,GAAA,OAAA,EAAA,MAAA;AAAA,EAEP;AACE,EAAA,eAAQ,CAAA,IAAA,EAAW;AAEnB,IAAA,IAAI,mBAAS,EAAA;AACX,MAAA,IAAA,CAAK,aAAA,GAAgB,IAAA,CAAA,aAAQ,CAAA,IAAc,CAAA,IAAK,CAAA;AAAI,IACtD;AAEA,IAAA,IAAA,eAAiB,EAAA;AACjB,MAAA,cAAc,GAAA,IAAS,CAAA,SAAA;AAAA,IACzB;AAAA,oBAkB0B,EAAA;AACxB,MAAA,WAAU,GAAA,IAAA,CAAA,MAAe;AACvB,IAAA;AAAiD,EAAA;AAEnD;AACE,kCAAsB,CAAA,CAAA,IAAA;AAAA,EA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,SACA,gBAAA,CAAA,IAAA,EAAA,OAAA,EAAA;AAAA,EAAA,IACD,IAAA,CAAA,UAAA,CAAA,OAAA,CAAA,EAAA;AAAA,IACH,MAAA,IAAA,WAAA,CAAA;AACF,MAAA,EAAA,EAAA,iCAAA;AAEO,MAAA,IAAS,EAAA,CAAA,sEAGa,CAAA;AAC3B,MAAI,MAAK,EAAA,eAAkB;AACzB,MAAA,QAAU,EAAA,MAAA;AAAY,KAAA,CACpB;AAAI,EAAA;AACE,EAAA,eACN,CAAA,IAAA,EAAA,OAAA,CAAA;AAAA,EAAA,OACA;AAAA,IACF,IAAC;AAAA,IACH,MAAA,EAAA,OAAA,CAAA,MAAA;AAEA,IAAA,OAAA,EAAA,eAAsB;AAEtB,IAAA,aAAO,EAAA,OAAA,CAAA,aAAA;AAAA,IACL,OAAA,EAAA,OAAA,CAAA,OAAA;AAAA,IACA,UAAQ,EAAA,OAAQ,CAAA,UAAA;AAAA,IAChB,YAAS,EAAA,OAAQ,CAAA;AAAA,GAAA;AACM;AACN,mBACL,CAAA,MAAQ,EAAA;AAAA,EAAA;AACE","file":"index.js","sourcesContent":["import type { HonoRequest } from 'hono';\nimport { MastraBase } from '../base';\nimport { InstrumentClass } from '../telemetry';\nimport type { MastraAuthConfig } from './types';\n\nexport interface MastraAuthProviderOptions<TUser = unknown> {\n name?: string;\n authorizeUser?: (user: TUser, request: HonoRequest) => Promise<boolean> | boolean;\n /**\n * Protected paths for the auth provider\n */\n protected?: MastraAuthConfig['protected'];\n /**\n * Public paths for the auth provider\n */\n public?: MastraAuthConfig['public'];\n}\n\n@InstrumentClass({\n prefix: 'auth',\n excludeMethods: ['__setTools', '__setLogger', '__setTelemetry', '#log'],\n})\nexport abstract class MastraAuthProvider<TUser = unknown> extends MastraBase {\n public protected?: MastraAuthConfig['protected'];\n public public?: MastraAuthConfig['public'];\n\n constructor(options?: MastraAuthProviderOptions<TUser>) {\n super({ component: 'AUTH', name: options?.name });\n\n if (options?.authorizeUser) {\n this.authorizeUser = options.authorizeUser.bind(this);\n }\n\n this.protected = options?.protected;\n this.public = options?.public;\n }\n\n /**\n * Authenticate a token and return the payload\n * @param token - The token to authenticate\n * @param request - The request\n * @returns The payload\n */\n abstract authenticateToken(token: string, request: HonoRequest): Promise<TUser | null>;\n\n /**\n * Authorize a user for a path and method\n * @param user - The user to authorize\n * @param request - The request\n * @returns The authorization result\n */\n abstract authorizeUser(user: TUser, request: HonoRequest): Promise<boolean> | boolean;\n\n protected registerOptions(opts?: MastraAuthProviderOptions<TUser>) {\n if (opts?.authorizeUser) {\n this.authorizeUser = opts.authorizeUser.bind(this);\n }\n if (opts?.protected) {\n this.protected = opts.protected;\n }\n if (opts?.public) {\n this.public = opts.public;\n }\n }\n}\n","import type { HonoRequest } from 'hono';\nimport { MastraAuthProvider } from './auth';\n\nexport class CompositeAuth extends MastraAuthProvider {\n private providers: MastraAuthProvider[];\n\n constructor(providers: MastraAuthProvider[]) {\n super();\n this.providers = providers;\n }\n\n async authenticateToken(token: string, request: HonoRequest): Promise<unknown | null> {\n for (const provider of this.providers) {\n try {\n const user = await provider.authenticateToken(token, request);\n if (user) {\n return user;\n }\n } catch {\n // ignore error, try next provider\n }\n }\n return null;\n }\n\n async authorizeUser(user: unknown, request: HonoRequest): Promise<boolean> {\n for (const provider of this.providers) {\n const authorized = await provider.authorizeUser(user, request);\n if (authorized) {\n return true;\n }\n }\n return false;\n }\n}\n","import type { HonoRequest } from 'hono';\nimport { MastraAuthProvider } from './auth';\nimport type { MastraAuthProviderOptions } from './auth';\n\nexport interface SimpleAuthOptions<TUser = unknown> extends MastraAuthProviderOptions<TUser> {\n /**\n * A map of tokens to users.\n * When a token is provided, it will be looked up in this map.\n */\n tokens: Record<string, TUser>;\n /**\n * Headers to check for the token.\n * Defaults to 'Authorization' with Bearer token extraction.\n * Can be a string or array of strings for custom header names.\n */\n headers?: string | string[];\n}\n\n/**\n * SimpleAuth is a basic token-based authentication provider.\n * It validates tokens against a predefined map of tokens to users.\n */\nexport class SimpleAuth<TUser = unknown> extends MastraAuthProvider<TUser> {\n private tokens: Record<string, TUser>;\n private headerNames: string[];\n private authenticatedUsers: Set<TUser>;\n\n constructor(options: SimpleAuthOptions<TUser>) {\n super(options);\n this.tokens = options.tokens;\n this.headerNames = this.normalizeHeaders(options.headers);\n // Store reference to all valid users for authorization\n this.authenticatedUsers = new Set(Object.values(this.tokens));\n }\n\n private normalizeHeaders(headers?: string | string[]): string[] {\n if (!headers) {\n return ['Authorization'];\n }\n return Array.isArray(headers) ? headers : [headers];\n }\n\n private extractBearerToken(value: string): string {\n if (value.startsWith('Bearer ')) {\n return value.slice(7);\n }\n return value;\n }\n\n private findTokenInHeaders(request: HonoRequest): string | null {\n for (const headerName of this.headerNames) {\n const headerValue = request.header(headerName);\n if (headerValue) {\n // For Authorization header, extract Bearer token\n if (headerName.toLowerCase() === 'authorization') {\n return this.extractBearerToken(headerValue);\n }\n return headerValue;\n }\n }\n return null;\n }\n\n async authenticateToken(token: string, request: HonoRequest): Promise<TUser | null> {\n // First, try the direct token\n const directToken = this.extractBearerToken(token);\n if (directToken in this.tokens) {\n return this.tokens[directToken]!;\n }\n\n // Then, try to find token in headers\n const headerToken = this.findTokenInHeaders(request);\n if (headerToken && headerToken in this.tokens) {\n return this.tokens[headerToken]!;\n }\n\n return null;\n }\n\n async authorizeUser(user: TUser, _request: HonoRequest): Promise<boolean> {\n // Check if this user was authenticated through our tokens\n return this.authenticatedUsers.has(user);\n }\n}\n","import type { Context, Handler, MiddlewareHandler } from 'hono';\nimport type { DescribeRouteOptions } from 'hono-openapi';\nimport { MastraError, ErrorDomain, ErrorCategory } from '../error';\nimport type { Mastra } from '../mastra';\nimport type { ApiRoute, MastraAuthConfig, Methods } from './types';\n\nexport type { MastraAuthConfig, ContextWithMastra, ApiRoute } from './types';\nexport { MastraAuthProvider } from './auth';\nexport type { MastraAuthProviderOptions } from './auth';\nexport { CompositeAuth } from './composite-auth';\nexport { SimpleAuth } from './simple-auth';\nexport type { SimpleAuthOptions } from './simple-auth';\n\n// Helper type for inferring parameters from a path\n// Thank you Claude!\ntype ParamsFromPath<P extends string> = {\n [K in P extends `${string}:${infer Param}/${string}` | `${string}:${infer Param}` ? Param : never]: string;\n};\n\ntype RegisterApiRoutePathError = `Param 'path' must not start with '/api', it is reserved for internal API routes.`;\ntype ValidatePath<P extends string, T> = P extends `/api/${string}` ? RegisterApiRoutePathError : T;\n\ntype RegisterApiRouteOptions<P extends string> = {\n method: Methods;\n openapi?: DescribeRouteOptions;\n handler?: Handler<\n {\n Variables: {\n mastra: Mastra;\n };\n },\n P,\n ParamsFromPath<P>\n >;\n createHandler?: (c: Context) => Promise<\n Handler<\n {\n Variables: {\n mastra: Mastra;\n };\n },\n P,\n ParamsFromPath<P>\n >\n >;\n middleware?: MiddlewareHandler | MiddlewareHandler[];\n /**\n * When false, skips Mastra auth for this route (defaults to true)\n */\n requiresAuth?: boolean;\n};\n\nfunction validateOptions<P extends string>(\n path: P,\n options: RegisterApiRoutePathError | RegisterApiRouteOptions<P>,\n): asserts options is RegisterApiRouteOptions<P> {\n const opts = options as RegisterApiRouteOptions<P>;\n\n if (opts.method === undefined) {\n throw new MastraError({\n id: 'MASTRA_SERVER_API_INVALID_ROUTE_OPTIONS',\n text: `Invalid options for route \"${path}\", missing \"method\" property`,\n domain: ErrorDomain.MASTRA_SERVER,\n category: ErrorCategory.USER,\n });\n }\n\n if (opts.handler === undefined && opts.createHandler === undefined) {\n throw new MastraError({\n id: 'MASTRA_SERVER_API_INVALID_ROUTE_OPTIONS',\n text: `Invalid options for route \"${path}\", you must define a \"handler\" or \"createHandler\" property`,\n domain: ErrorDomain.MASTRA_SERVER,\n category: ErrorCategory.USER,\n });\n }\n\n if (opts.handler !== undefined && opts.createHandler !== undefined) {\n throw new MastraError({\n id: 'MASTRA_SERVER_API_INVALID_ROUTE_OPTIONS',\n text: `Invalid options for route \"${path}\", you can only define one of the following properties: \"handler\" or \"createHandler\"`,\n domain: ErrorDomain.MASTRA_SERVER,\n category: ErrorCategory.USER,\n });\n }\n}\n\nexport function registerApiRoute<P extends string>(\n path: P,\n options: ValidatePath<P, RegisterApiRouteOptions<P>>,\n): ValidatePath<P, ApiRoute> {\n if (path.startsWith('/api/')) {\n throw new MastraError({\n id: 'MASTRA_SERVER_API_PATH_RESERVED',\n text: 'Path must not start with \"/api\", it\\'s reserved for internal API routes',\n domain: ErrorDomain.MASTRA_SERVER,\n category: ErrorCategory.USER,\n });\n }\n\n validateOptions(path, options);\n\n return {\n path,\n method: options.method,\n handler: options.handler,\n createHandler: options.createHandler,\n openapi: options.openapi,\n middleware: options.middleware,\n requiresAuth: options.requiresAuth,\n } as unknown as ValidatePath<P, ApiRoute>;\n}\n\nexport function defineAuth<TUser>(config: MastraAuthConfig<TUser>): MastraAuthConfig<TUser> {\n return config;\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/server/auth.ts","../../src/server/composite-auth.ts","../../src/server/simple-auth.ts","../../src/server/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,MAAA,SAAA,EAAA,MAAA;AAkBA,MAAA,IAAA,EAAA,OAAA,EAAA;AAAiB,KACf,CAAA;AAAQ,IACR,IAAA,OAAA,EAAA,aAAiB,EAAA;AACnB,MAAC,IAAA,CAAA,aAAA,GAAA,OAAA,CAAA,aAAA,CAAA,IAAA,CAAA,IAAA,CAAA;AACM,IAAe;AAAuD,IACpE,IAAA,CAAA,SAAA,GAAA,OAAA,EAAA,SAAA;AAAA,IACA,IAAA,CAAA,MAAA,GAAA,OAAA,EAAA,MAAA;AAAA,EAEP;AACE,EAAA,eAAQ,CAAA,IAAA,EAAW;AAEnB,IAAA,IAAI,mBAAS,EAAA;AACX,MAAA,IAAA,CAAK,aAAA,GAAgB,IAAA,CAAA,aAAQ,CAAA,IAAc,CAAA,IAAK,CAAA;AAAI,IACtD;AAEA,IAAA,IAAA,eAAiB,EAAA;AACjB,MAAA,cAAc,GAAA,IAAS,CAAA,SAAA;AAAA,IAC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AA,WAAA,CAAA;AACF,MAAA,EAAA,EAAA,iCAAA;AAEO,MAAA,IAAS,EAAA,CAAA,sEAGa,CAAA;AAC3B,MAAI,MAAK,EAAA,eAAkB;AACzB,MAAA,QAAU,EAAA,MAAA;AAAY,KAAA,CACpB;AAAI,EAAA;AACE,EAAA,eACN,CAAA,IAAA,EAAA,OAAA,CAAA;AAAA,EAAA,OACA;AAAA,IACF,IAAC;AAAA,IACH,MAAA,EAAA,OAAA,CAAA,MAAA;AAEA,IAAA,OAAA,EAAA,eAAsB;AAEtB,IAAA,aAAO,EAAA,OAAA,CAAA,aAAA;AAAA,IACL,OAAA,EAAA,OAAA,CAAA,OAAA;AAAA,IACA,UAAQ,EAAA,OAAQ,CAAA,UAAA;AAAA,IAChB,YAAS,EAAA,OAAQ,CAAA;AAAA,GAAA;AACM;AACN,mBACL,CAAA,MAAQ,EAAA;AAAA,EAAA;AACE","file":"index.js","sourcesContent":["import type { HonoRequest } from 'hono';\nimport { MastraBase } from '../base';\nimport { InstrumentClass } from '../telemetry';\nimport type { MastraAuthConfig } from './types';\n\nexport interface MastraAuthProviderOptions<TUser = unknown> {\n name?: string;\n authorizeUser?: (user: TUser, request: HonoRequest) => Promise<boolean> | boolean;\n /**\n * Protected paths for the auth provider\n */\n protected?: MastraAuthConfig['protected'];\n /**\n * Public paths for the auth provider\n */\n public?: MastraAuthConfig['public'];\n}\n\n@InstrumentClass({\n prefix: 'auth',\n excludeMethods: ['__setTools', '__setLogger', '__setTelemetry', '#log'],\n})\nexport abstract class MastraAuthProvider<TUser = unknown> extends MastraBase {\n public protected?: MastraAuthConfig['protected'];\n public public?: MastraAuthConfig['public'];\n\n constructor(options?: MastraAuthProviderOptions<TUser>) {\n super({ component: 'AUTH', name: options?.name });\n\n if (options?.authorizeUser) {\n this.authorizeUser = options.authorizeUser.bind(this);\n }\n\n this.protected = options?.protected;\n this.public = options?.public;\n }\n\n /**\n * Authenticate a token and return the payload\n * @param token - The token to authenticate\n * @param request - The request\n * @returns The payload\n */\n abstract authenticateToken(token: string, request: HonoRequest): Promise<TUser | null>;\n\n /**\n * Authorize a user for a path and method\n * @param user - The user to authorize\n * @param request - The request\n * @returns The authorization result\n */\n abstract authorizeUser(user: TUser, request: HonoRequest): Promise<boolean> | boolean;\n\n protected registerOptions(opts?: MastraAuthProviderOptions<TUser>) {\n if (opts?.authorizeUser) {\n this.authorizeUser = opts.authorizeUser.bind(this);\n }\n if (opts?.protected) {\n this.protected = opts.protected;\n }\n if (opts?.public) {\n this.public = opts.public;\n }\n }\n}\n","import type { HonoRequest } from 'hono';\nimport { MastraAuthProvider } from './auth';\n\nexport class CompositeAuth extends MastraAuthProvider {\n private providers: MastraAuthProvider[];\n\n constructor(providers: MastraAuthProvider[]) {\n super();\n this.providers = providers;\n }\n\n async authenticateToken(token: string, request: HonoRequest): Promise<unknown | null> {\n for (const provider of this.providers) {\n try {\n const user = await provider.authenticateToken(token, request);\n if (user) {\n return user;\n }\n } catch {\n // ignore error, try next provider\n }\n }\n return null;\n }\n\n async authorizeUser(user: unknown, request: HonoRequest): Promise<boolean> {\n for (const provider of this.providers) {\n const authorized = await provider.authorizeUser(user, request);\n if (authorized) {\n return true;\n }\n }\n return false;\n }\n}\n","import type { HonoRequest } from 'hono';\nimport type { MastraAuthProviderOptions } from './auth';\nimport { MastraAuthProvider } from './auth';\n\nconst DEFAULT_HEADERS = ['Authorization', 'X-Playground-Access'];\n\ntype TokenToUser<TUser> = Record<string, TUser>;\n\nexport interface SimpleAuthOptions<TUser> extends MastraAuthProviderOptions<TUser> {\n /**\n * Valid tokens to authenticate against\n */\n tokens: TokenToUser<TUser>;\n /**\n * Headers to check for authentication\n * @default ['Authorization', 'X-Playground-Access']\n */\n headers?: string | string[];\n}\n\nexport class SimpleAuth<TUser> extends MastraAuthProvider<TUser> {\n private tokens: TokenToUser<TUser>;\n private headers: string[];\n private users: TUser[];\n\n constructor(options: SimpleAuthOptions<TUser>) {\n super(options);\n this.tokens = options.tokens;\n this.users = Object.values(this.tokens);\n this.headers = [...DEFAULT_HEADERS].concat(options.headers || []);\n }\n\n async authenticateToken(token: string, request: HonoRequest): Promise<TUser | null> {\n const requestTokens = this.getTokensFromHeaders(token, request);\n\n for (const requestToken of requestTokens) {\n const tokenToUser = this.tokens[requestToken];\n if (tokenToUser) {\n return tokenToUser;\n }\n }\n\n return null;\n }\n\n async authorizeUser(user: TUser, _request: HonoRequest): Promise<boolean> {\n return this.users.includes(user);\n }\n\n private stripBearerPrefix(token: string): string {\n return token.startsWith('Bearer ') ? token.slice(7) : token;\n }\n\n private getTokensFromHeaders(token: string, request: HonoRequest): string[] {\n const tokens = [token];\n for (const headerName of this.headers) {\n const headerValue = request.header(headerName);\n if (headerValue) {\n tokens.push(this.stripBearerPrefix(headerValue));\n }\n }\n return tokens;\n }\n}\n","import type { Context, Handler, MiddlewareHandler } from 'hono';\nimport type { DescribeRouteOptions } from 'hono-openapi';\nimport { MastraError, ErrorDomain, ErrorCategory } from '../error';\nimport type { Mastra } from '../mastra';\nimport type { ApiRoute, MastraAuthConfig, Methods } from './types';\n\nexport type { MastraAuthConfig, ContextWithMastra, ApiRoute } from './types';\nexport { MastraAuthProvider } from './auth';\nexport type { MastraAuthProviderOptions } from './auth';\nexport { CompositeAuth } from './composite-auth';\nexport { SimpleAuth } from './simple-auth';\nexport type { SimpleAuthOptions } from './simple-auth';\n\n// Helper type for inferring parameters from a path\n// Thank you Claude!\ntype ParamsFromPath<P extends string> = {\n [K in P extends `${string}:${infer Param}/${string}` | `${string}:${infer Param}` ? Param : never]: string;\n};\n\ntype RegisterApiRoutePathError = `Param 'path' must not start with '/api', it is reserved for internal API routes.`;\ntype ValidatePath<P extends string, T> = P extends `/api/${string}` ? RegisterApiRoutePathError : T;\n\ntype RegisterApiRouteOptions<P extends string> = {\n method: Methods;\n openapi?: DescribeRouteOptions;\n handler?: Handler<\n {\n Variables: {\n mastra: Mastra;\n };\n },\n P,\n ParamsFromPath<P>\n >;\n createHandler?: (c: Context) => Promise<\n Handler<\n {\n Variables: {\n mastra: Mastra;\n };\n },\n P,\n ParamsFromPath<P>\n >\n >;\n middleware?: MiddlewareHandler | MiddlewareHandler[];\n /**\n * When false, skips Mastra auth for this route (defaults to true)\n */\n requiresAuth?: boolean;\n};\n\nfunction validateOptions<P extends string>(\n path: P,\n options: RegisterApiRoutePathError | RegisterApiRouteOptions<P>,\n): asserts options is RegisterApiRouteOptions<P> {\n const opts = options as RegisterApiRouteOptions<P>;\n\n if (opts.method === undefined) {\n throw new MastraError({\n id: 'MASTRA_SERVER_API_INVALID_ROUTE_OPTIONS',\n text: `Invalid options for route \"${path}\", missing \"method\" property`,\n domain: ErrorDomain.MASTRA_SERVER,\n category: ErrorCategory.USER,\n });\n }\n\n if (opts.handler === undefined && opts.createHandler === undefined) {\n throw new MastraError({\n id: 'MASTRA_SERVER_API_INVALID_ROUTE_OPTIONS',\n text: `Invalid options for route \"${path}\", you must define a \"handler\" or \"createHandler\" property`,\n domain: ErrorDomain.MASTRA_SERVER,\n category: ErrorCategory.USER,\n });\n }\n\n if (opts.handler !== undefined && opts.createHandler !== undefined) {\n throw new MastraError({\n id: 'MASTRA_SERVER_API_INVALID_ROUTE_OPTIONS',\n text: `Invalid options for route \"${path}\", you can only define one of the following properties: \"handler\" or \"createHandler\"`,\n domain: ErrorDomain.MASTRA_SERVER,\n category: ErrorCategory.USER,\n });\n }\n}\n\nexport function registerApiRoute<P extends string>(\n path: P,\n options: ValidatePath<P, RegisterApiRouteOptions<P>>,\n): ValidatePath<P, ApiRoute> {\n if (path.startsWith('/api/')) {\n throw new MastraError({\n id: 'MASTRA_SERVER_API_PATH_RESERVED',\n text: 'Path must not start with \"/api\", it\\'s reserved for internal API routes',\n domain: ErrorDomain.MASTRA_SERVER,\n category: ErrorCategory.USER,\n });\n }\n\n validateOptions(path, options);\n\n return {\n path,\n method: options.method,\n handler: options.handler,\n createHandler: options.createHandler,\n openapi: options.openapi,\n middleware: options.middleware,\n requiresAuth: options.requiresAuth,\n } as unknown as ValidatePath<P, ApiRoute>;\n}\n\nexport function defineAuth<TUser>(config: MastraAuthConfig<TUser>): MastraAuthConfig<TUser> {\n return config;\n}\n"]}
|