UNPKG

@naktibalda/jorel

Version:

The easiest way to use LLMs, including streams, images, documents, tools and various agent scenarios.

110 lines (109 loc) 4.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TestProvider = void 0; const providers_1 = require("../providers"); class TestProvider { name; defaultResponse; defaultStreamResponse; simulateDelay; failOnModels; constructor(config = {}) { this.name = config.name || "test-provider"; this.defaultResponse = config.defaultResponse || "This is a test response"; this.defaultStreamResponse = config.defaultStreamResponse || ["This ", "is ", "a ", "test ", "response"]; this.simulateDelay = config.simulateDelay || 0; this.failOnModels = config.failOnModels || []; } async delay() { if (this.simulateDelay > 0) { await new Promise((resolve) => setTimeout(resolve, this.simulateDelay)); } } async generateResponse(model, messages, config = {}) { await this.delay(); if (this.failOnModels.includes(model)) { throw new Error(`Model ${model} is configured to fail`); } // If tools are provided and toolChoice is not "none", return a tool call if (config.tools && config.toolChoice !== "none") { const tool = config.tools.tools[0]; // Use first available tool if (tool) { return { ...(0, providers_1.generateAssistantMessage)(null, [ { id: "test-tool-call", request: { id: "test-function-call", function: { name: tool.name, arguments: { test: "value" }, }, }, approvalState: tool.requiresConfirmation ? "requiresApproval" : "noApprovalRequired", executionState: "pending", result: null, error: null, }, ]), meta: { model, provider: this.name, temperature: config.temperature ?? undefined, durationMs: this.simulateDelay, inputTokens: 10, outputTokens: 10, }, }; } } return { ...(0, providers_1.generateAssistantMessage)(this.defaultResponse), meta: { model, provider: this.name, temperature: config.temperature ?? undefined, durationMs: this.simulateDelay, inputTokens: 10, outputTokens: 10, }, }; } async *generateResponseStream(model, messages, config = {}) { if (this.failOnModels.includes(model)) { throw new Error(`Model ${model} is configured to fail`); } for (const chunk of this.defaultStreamResponse) { await this.delay(); yield { type: "chunk", content: chunk }; } yield { type: "response", role: "assistant", content: this.defaultStreamResponse.join(""), meta: { model, provider: this.name, temperature: config.temperature ?? undefined, durationMs: this.simulateDelay * this.defaultStreamResponse.length, inputTokens: 10, outputTokens: 10, }, }; } async getAvailableModels() { await this.delay(); return ["test-model-1", "test-model-2", "test-model-3"]; } async createEmbedding(model, text) { await this.delay(); if (this.failOnModels.includes(model)) { throw new Error(`Model ${model} is configured to fail`); } // Return a deterministic embedding based on input text length return Array(10) .fill(0) .map((_, i) => (text.length + i) / 100); } } exports.TestProvider = TestProvider;