@mastra/core
Version:
Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.
181 lines (178 loc) • 5.33 kB
JavaScript
;
var chunkCYEQK4PM_cjs = require('../chunk-CYEQK4PM.cjs');
var ai = require('ai');
var test = require('ai/test');
var test$1 = require('ai-v5/test');
function createMockModel({
objectGenerationMode,
mockText,
spyGenerate,
spyStream,
version = "v2"
}) {
const text = typeof mockText === "string" ? mockText : JSON.stringify(mockText);
const finalText = objectGenerationMode === "json" ? JSON.stringify(mockText) : text;
if (version === "v1") {
const mockModel2 = new test.MockLanguageModelV1({
defaultObjectGenerationMode: objectGenerationMode,
doGenerate: async (props) => {
if (spyGenerate) {
spyGenerate(props);
}
return {
rawCall: { rawPrompt: null, rawSettings: {} },
finishReason: "stop",
usage: { promptTokens: 10, completionTokens: 20 },
text: finalText
};
},
doStream: async (props) => {
if (spyStream) {
spyStream(props);
}
const chunks = finalText.split(" ").map((word) => ({
type: "text-delta",
textDelta: word + " "
}));
return {
stream: ai.simulateReadableStream({
chunks: [
...chunks,
{
type: "finish",
finishReason: "stop",
logprobs: void 0,
usage: { completionTokens: 10, promptTokens: 3 }
}
]
}),
rawCall: { rawPrompt: null, rawSettings: {} }
};
}
});
return mockModel2;
}
const mockModel = new test$1.MockLanguageModelV2({
doGenerate: async (props) => {
if (spyGenerate) {
spyGenerate(props);
}
return {
rawCall: { rawPrompt: null, rawSettings: {} },
finishReason: "stop",
usage: { inputTokens: 10, outputTokens: 20, totalTokens: 30 },
content: [
{
type: "text",
text: finalText
}
],
warnings: []
};
},
doStream: async (props) => {
if (spyStream) {
spyStream(props);
}
return {
rawCall: { rawPrompt: null, rawSettings: {} },
warnings: [],
stream: test$1.convertArrayToReadableStream([
{ type: "stream-start", warnings: [] },
{ type: "response-metadata", id: "id-0", modelId: "mock-model-id", timestamp: /* @__PURE__ */ new Date(0) },
{ type: "text-start", id: "1" },
{ type: "text-delta", id: "1", delta: finalText },
{ type: "text-end", id: "1" },
{
type: "finish",
finishReason: "stop",
usage: { inputTokens: 10, outputTokens: 20, totalTokens: 30 }
}
])
};
}
});
return mockModel;
}
var MockProvider = class extends chunkCYEQK4PM_cjs.MastraLLMV1 {
constructor({
spyGenerate,
spyStream,
objectGenerationMode,
mockText = "Hello, world!"
}) {
const mockModel = new test.MockLanguageModelV1({
defaultObjectGenerationMode: objectGenerationMode,
doGenerate: async (props) => {
if (spyGenerate) {
spyGenerate(props);
}
if (objectGenerationMode === "json") {
return {
rawCall: { rawPrompt: null, rawSettings: {} },
finishReason: "stop",
usage: { promptTokens: 10, completionTokens: 20 },
text: JSON.stringify(mockText)
};
}
return {
rawCall: { rawPrompt: null, rawSettings: {} },
finishReason: "stop",
usage: { promptTokens: 10, completionTokens: 20 },
text: typeof mockText === "string" ? mockText : JSON.stringify(mockText)
};
},
doStream: async (props) => {
if (spyStream) {
spyStream(props);
}
const text = typeof mockText === "string" ? mockText : JSON.stringify(mockText);
const chunks = text.split(" ").map((word) => ({
type: "text-delta",
textDelta: word + " "
}));
return {
stream: ai.simulateReadableStream({
chunks: [
...chunks,
{
type: "finish",
finishReason: "stop",
logprobs: void 0,
usage: { completionTokens: 10, promptTokens: 3 }
}
]
}),
rawCall: { rawPrompt: null, rawSettings: {} }
};
}
});
super({ model: mockModel });
}
// @ts-ignore
stream(...args) {
const result = super.stream(...args);
return {
...result,
// @ts-ignore on await read the stream
then: (onfulfilled, onrejected) => {
return result.baseStream.pipeTo(new WritableStream()).then(onfulfilled, onrejected);
}
};
}
// @ts-ignore
__streamObject(...args) {
const result = super.__streamObject(...args);
return {
...result,
// @ts-ignore on await read the stream
then: (onfulfilled, onrejected) => {
return result.baseStream.pipeTo(new WritableStream()).then(onfulfilled, onrejected);
}
};
}
};
exports.MockProvider = MockProvider;
exports.createMockModel = createMockModel;
//# sourceMappingURL=llm-mock.cjs.map
//# sourceMappingURL=llm-mock.cjs.map