mirror of
https://github.com/geoffsee/open-gsio.git
synced 2025-09-08 22:56:46 +00:00
wip
This commit is contained in:

committed by
Geoff Seemueller

parent
21d6c8604e
commit
554096abb2
3
packages/services/README.md
Normal file
3
packages/services/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# @open-gsio/services
|
||||
|
||||
A service layer powered by mobx-state-tree.
|
37
packages/services/package.json
Normal file
37
packages/services/package.json
Normal file
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"name": "@open-gsio/services",
|
||||
"type": "module",
|
||||
"module": "src/index.ts",
|
||||
"scripts": {
|
||||
"tests": "vitest run",
|
||||
"tests:coverage": "vitest run --coverage.enabled=true"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@open-gsio/env": "workspace:*",
|
||||
"@open-gsio/client": "workspace:*",
|
||||
"@open-gsio/coordinators": "workspace:*",
|
||||
"@open-gsio/schema": "workspace:*",
|
||||
"@open-gsio/ai": "workspace:*",
|
||||
"@anthropic-ai/sdk": "^0.32.1",
|
||||
"bun-sqlite-key-value": "^1.13.1",
|
||||
"@cloudflare/workers-types": "^4.20241205.0",
|
||||
"@testing-library/jest-dom": "^6.4.2",
|
||||
"@testing-library/user-event": "^14.5.2",
|
||||
"@vitest/coverage-v8": "^3.1.4",
|
||||
"@vitest/ui": "^3.1.4",
|
||||
"chokidar": "^4.0.1",
|
||||
"itty-router": "^5.0.18",
|
||||
"jsdom": "^24.0.0",
|
||||
"mobx": "^6.13.5",
|
||||
"mobx-state-tree": "^6.0.1",
|
||||
"moo": "^0.5.2",
|
||||
"openai": "^5.0.1",
|
||||
"typescript": "^5.7.2",
|
||||
"vike": "0.4.193",
|
||||
"vite": "^6.3.5",
|
||||
"vitest": "^3.1.4",
|
||||
"wrangler": "^4.18.0",
|
||||
"zod": "^3.23.8",
|
||||
"dotenv": "^16.5.0"
|
||||
}
|
||||
}
|
165
packages/services/src/__tests__/AssetService.test.ts
Normal file
165
packages/services/src/__tests__/AssetService.test.ts
Normal file
@@ -0,0 +1,165 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
|
||||
import AssetService from '../asset-service/AssetService.ts';
|
||||
|
||||
// Mock the vike/server module
|
||||
vi.mock('vike/server', () => ({
|
||||
renderPage: vi.fn(),
|
||||
}));
|
||||
|
||||
// Import the mocked renderPage function for assertions
|
||||
// eslint-disable-next-line import/order
|
||||
import { renderPage } from 'vike/server';
|
||||
|
||||
describe('AssetService', () => {
|
||||
let assetService;
|
||||
|
||||
beforeEach(() => {
|
||||
// Create a new instance of the service before each test
|
||||
assetService = AssetService.create();
|
||||
|
||||
// Reset mocks
|
||||
vi.resetAllMocks();
|
||||
});
|
||||
|
||||
describe('Initial state', () => {
|
||||
it('should have empty env and ctx objects initially', () => {
|
||||
expect(assetService.env).toEqual({});
|
||||
expect(assetService.ctx).toEqual({});
|
||||
});
|
||||
});
|
||||
|
||||
describe('setEnv', () => {
|
||||
it('should set the environment', () => {
|
||||
const mockEnv = { ASSETS: { fetch: vi.fn() } };
|
||||
assetService.setEnv(mockEnv);
|
||||
expect(assetService.env).toEqual(mockEnv);
|
||||
});
|
||||
});
|
||||
|
||||
describe('setCtx', () => {
|
||||
it('should set the execution context', () => {
|
||||
const mockCtx = { waitUntil: vi.fn() };
|
||||
assetService.setCtx(mockCtx);
|
||||
expect(assetService.ctx).toEqual(mockCtx);
|
||||
});
|
||||
});
|
||||
|
||||
describe('handleSsr', () => {
|
||||
it('should return null when httpResponse is not available', async () => {
|
||||
// Setup mock to return a pageContext without httpResponse
|
||||
vi.mocked(renderPage).mockResolvedValue({});
|
||||
|
||||
const url = 'https://example.com';
|
||||
const headers = new Headers();
|
||||
const env = {};
|
||||
|
||||
const result = await assetService.handleSsr(url, headers, env);
|
||||
|
||||
// Verify renderPage was called with correct arguments
|
||||
expect(renderPage).toHaveBeenCalledWith({
|
||||
urlOriginal: url,
|
||||
headersOriginal: headers,
|
||||
fetch: expect.any(Function),
|
||||
env,
|
||||
});
|
||||
|
||||
// Verify result is null
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should return a Response when httpResponse is available', async () => {
|
||||
// Create mock stream
|
||||
const mockStream = new ReadableStream();
|
||||
|
||||
// Setup mock to return a pageContext with httpResponse
|
||||
vi.mocked(renderPage).mockResolvedValue({
|
||||
httpResponse: {
|
||||
statusCode: 200,
|
||||
headers: new Headers({ 'Content-Type': 'text/html' }),
|
||||
getReadableWebStream: () => mockStream,
|
||||
},
|
||||
});
|
||||
|
||||
const url = 'https://example.com';
|
||||
const headers = new Headers();
|
||||
const env = {};
|
||||
|
||||
const result = await assetService.handleSsr(url, headers, env);
|
||||
|
||||
// Verify renderPage was called with correct arguments
|
||||
expect(renderPage).toHaveBeenCalledWith({
|
||||
urlOriginal: url,
|
||||
headersOriginal: headers,
|
||||
fetch: expect.any(Function),
|
||||
env,
|
||||
});
|
||||
|
||||
// Verify result is a Response with correct properties
|
||||
expect(result).toBeInstanceOf(Response);
|
||||
expect(result.status).toBe(200);
|
||||
expect(result.headers.get('Content-Type')).toBe('text/html');
|
||||
});
|
||||
});
|
||||
|
||||
describe('handleStaticAssets', () => {
|
||||
it('should fetch assets from the environment', async () => {
|
||||
// Create mock request
|
||||
const request = new Request('https://example.com/static/image.png');
|
||||
|
||||
// Create mock response
|
||||
const mockResponse = new Response('Mock asset content', {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': 'image/png' },
|
||||
});
|
||||
|
||||
// Create mock environment with ASSETS.fetch
|
||||
const mockEnv = {
|
||||
ASSETS: {
|
||||
fetch: vi.fn().mockResolvedValue(mockResponse),
|
||||
},
|
||||
};
|
||||
|
||||
// Set the environment
|
||||
assetService.setEnv(mockEnv);
|
||||
|
||||
// Call the method
|
||||
const result = await assetService.handleStaticAssets(request, mockEnv);
|
||||
|
||||
// Verify ASSETS.fetch was called with the request
|
||||
expect(mockEnv.ASSETS.fetch).toHaveBeenCalledWith(request);
|
||||
|
||||
// Verify result is the expected response
|
||||
expect(result).toBe(mockResponse);
|
||||
});
|
||||
|
||||
it('should return a 404 response when an error occurs', async () => {
|
||||
// Create mock request
|
||||
const request = new Request('https://example.com/static/not-found.png');
|
||||
|
||||
// Create mock environment with ASSETS.fetch that throws an error
|
||||
const mockEnv = {
|
||||
ASSETS: {
|
||||
fetch: vi.fn().mockRejectedValue(new Error('Asset not found')),
|
||||
},
|
||||
};
|
||||
|
||||
// Set the environment
|
||||
assetService.setEnv(mockEnv);
|
||||
|
||||
// Call the method
|
||||
const result = await assetService.handleStaticAssets(request, mockEnv);
|
||||
|
||||
// Verify ASSETS.fetch was called with the request
|
||||
expect(mockEnv.ASSETS.fetch).toHaveBeenCalledWith(request);
|
||||
|
||||
// Verify result is a 404 Response
|
||||
expect(result).toBeInstanceOf(Response);
|
||||
expect(result.status).toBe(404);
|
||||
|
||||
// Verify response body
|
||||
const text = await result.clone().text();
|
||||
expect(text).toBe('Asset not found');
|
||||
});
|
||||
});
|
||||
});
|
358
packages/services/src/__tests__/ChatService.test.ts
Normal file
358
packages/services/src/__tests__/ChatService.test.ts
Normal file
@@ -0,0 +1,358 @@
|
||||
import { getSnapshot } from 'mobx-state-tree';
|
||||
import OpenAI from 'openai';
|
||||
import { ChatSdk } from 'packages/ai/src/chat-sdk';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import ChatService, { ClientError } from '../chat-service/ChatService.ts';
|
||||
// Create mock OpenAI instance
|
||||
const mockOpenAIInstance = {
|
||||
models: {
|
||||
list: vi.fn().mockResolvedValue({
|
||||
data: [{ id: 'mlx-model-1' }, { id: 'mlx-model-2' }, { id: 'other-model' }],
|
||||
}),
|
||||
},
|
||||
chat: {
|
||||
completions: {
|
||||
create: vi.fn(),
|
||||
},
|
||||
},
|
||||
baseURL: 'http://localhost:8000',
|
||||
};
|
||||
|
||||
// Mock dependencies
|
||||
vi.mock('openai', () => {
|
||||
return {
|
||||
default: vi.fn().mockImplementation(() => mockOpenAIInstance),
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock('../../lib/chat-sdk', () => ({
|
||||
default: {
|
||||
handleChatRequest: vi.fn(),
|
||||
buildAssistantPrompt: vi.fn(),
|
||||
buildMessageChain: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock('../../lib/handleStreamData', () => ({
|
||||
default: vi.fn().mockReturnValue(() => {}),
|
||||
}));
|
||||
|
||||
describe('ChatService', () => {
|
||||
let chatService: any;
|
||||
let mockEnv: any;
|
||||
|
||||
beforeEach(() => {
|
||||
// Create a new instance of the service before each test
|
||||
chatService = ChatService.create({
|
||||
maxTokens: 2000,
|
||||
systemPrompt: 'You are a helpful assistant.',
|
||||
openAIApiKey: 'test-api-key',
|
||||
openAIBaseURL: 'https://api.openai.com/v1',
|
||||
});
|
||||
|
||||
// Create mock environment
|
||||
mockEnv = {
|
||||
OPENAI_API_KEY: 'test-api-key',
|
||||
OPENAI_API_ENDPOINT: 'https://api.openai.com/v1',
|
||||
SERVER_COORDINATOR: {
|
||||
idFromName: vi.fn().mockReturnValue('test-id'),
|
||||
get: vi.fn().mockReturnValue({
|
||||
getStreamData: vi.fn().mockResolvedValue(
|
||||
JSON.stringify({
|
||||
messages: [],
|
||||
model: 'gpt-4',
|
||||
systemPrompt: 'You are a helpful assistant.',
|
||||
preprocessedContext: {},
|
||||
}),
|
||||
),
|
||||
}),
|
||||
},
|
||||
};
|
||||
|
||||
// Set the environment using the action
|
||||
chatService.setEnv(mockEnv);
|
||||
|
||||
// Reset mocks
|
||||
vi.resetAllMocks();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('Initial state', () => {
|
||||
it('should have the correct initial state', () => {
|
||||
const freshService = ChatService.create({
|
||||
maxTokens: 2000,
|
||||
systemPrompt: 'You are a helpful assistant.',
|
||||
});
|
||||
|
||||
expect(freshService.maxTokens).toBe(2000);
|
||||
expect(freshService.systemPrompt).toBe('You are a helpful assistant.');
|
||||
expect(freshService.activeStreams.size).toBe(0);
|
||||
expect(freshService.openAIApiKey).toBe('');
|
||||
expect(freshService.openAIBaseURL).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('setEnv', () => {
|
||||
it('should set the environment and initialize OpenAI client with local endpoint', () => {
|
||||
const localEnv = {
|
||||
...mockEnv,
|
||||
OPENAI_API_ENDPOINT: 'http://localhost:8000',
|
||||
};
|
||||
|
||||
// Reset the mock to track new calls
|
||||
vi.mocked(OpenAI).mockClear();
|
||||
|
||||
chatService.setEnv(localEnv);
|
||||
|
||||
expect(chatService.env).toEqual(localEnv);
|
||||
expect(OpenAI).toHaveBeenCalledWith({
|
||||
apiKey: localEnv.OPENAI_API_KEY,
|
||||
baseURL: localEnv.OPENAI_API_ENDPOINT,
|
||||
});
|
||||
});
|
||||
|
||||
it('should set the environment and initialize OpenAI client with API key and base URL', () => {
|
||||
// Create a new instance with the properties already set
|
||||
const service = ChatService.create({
|
||||
maxTokens: 2000,
|
||||
systemPrompt: 'You are a helpful assistant.',
|
||||
openAIApiKey: 'test-api-key',
|
||||
openAIBaseURL: 'https://api.openai.com/v1',
|
||||
});
|
||||
|
||||
// Reset the mock to track new calls
|
||||
vi.mocked(OpenAI).mockClear();
|
||||
|
||||
service.setEnv(mockEnv);
|
||||
|
||||
expect(service.env).toEqual(mockEnv);
|
||||
expect(OpenAI).toHaveBeenCalledWith({
|
||||
apiKey: 'test-api-key',
|
||||
baseURL: 'https://api.openai.com/v1',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('setActiveStream and removeActiveStream', () => {
|
||||
it('should set and remove active streams', () => {
|
||||
const streamId = 'test-stream-id';
|
||||
const streamData = {
|
||||
name: 'Test Stream',
|
||||
maxTokens: 1000,
|
||||
systemPrompt: 'You are a helpful assistant.',
|
||||
model: 'gpt-4',
|
||||
messages: [],
|
||||
};
|
||||
|
||||
// Set active stream
|
||||
chatService.setActiveStream(streamId, streamData);
|
||||
expect(chatService.activeStreams.has(streamId)).toBe(true);
|
||||
expect(getSnapshot(chatService.activeStreams.get(streamId))).toEqual(streamData);
|
||||
|
||||
// Remove active stream
|
||||
chatService.removeActiveStream(streamId);
|
||||
expect(chatService.activeStreams.has(streamId)).toBe(false);
|
||||
});
|
||||
|
||||
it('should handle missing or incomplete stream data', () => {
|
||||
const streamId = 'test-stream-id';
|
||||
|
||||
// Set active stream with undefined data
|
||||
chatService.setActiveStream(streamId, undefined);
|
||||
expect(chatService.activeStreams.has(streamId)).toBe(true);
|
||||
expect(getSnapshot(chatService.activeStreams.get(streamId))).toEqual({
|
||||
name: 'Unnamed Stream',
|
||||
maxTokens: 0,
|
||||
systemPrompt: '',
|
||||
model: '',
|
||||
messages: [],
|
||||
});
|
||||
|
||||
// Set active stream with partial data
|
||||
chatService.setActiveStream(streamId, { name: 'Partial Stream' });
|
||||
expect(chatService.activeStreams.has(streamId)).toBe(true);
|
||||
expect(getSnapshot(chatService.activeStreams.get(streamId))).toEqual({
|
||||
name: 'Partial Stream',
|
||||
maxTokens: 0,
|
||||
systemPrompt: '',
|
||||
model: '',
|
||||
messages: [],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getSupportedModels', () => {
|
||||
it('should return local models when using localhost endpoint', async () => {
|
||||
const originalResponseJson = Response.json;
|
||||
Response.json = vi.fn().mockImplementation(data => {
|
||||
return {
|
||||
json: async () => data,
|
||||
};
|
||||
});
|
||||
|
||||
const localEnv = {
|
||||
...mockEnv,
|
||||
OPENAI_API_ENDPOINT: 'http://localhost:8000',
|
||||
};
|
||||
|
||||
// Create a new service instance for this test
|
||||
const localService = ChatService.create({
|
||||
maxTokens: 2000,
|
||||
systemPrompt: 'You are a helpful assistant.',
|
||||
});
|
||||
|
||||
localService.setEnv(localEnv);
|
||||
|
||||
// Mock the implementation of getSupportedModels for this test
|
||||
const originalGetSupportedModels = localService.getSupportedModels;
|
||||
localService.getSupportedModels = vi.fn().mockResolvedValueOnce({
|
||||
json: async () => ['mlx-model-1', 'mlx-model-2'],
|
||||
});
|
||||
|
||||
const response = await localService.getSupportedModels();
|
||||
const data = await response.json();
|
||||
|
||||
expect(data).toEqual(['mlx-model-1', 'mlx-model-2']);
|
||||
|
||||
// Restore mocks
|
||||
Response.json = originalResponseJson;
|
||||
localService.getSupportedModels = originalGetSupportedModels;
|
||||
});
|
||||
});
|
||||
|
||||
describe('handleChatRequest', () => {
|
||||
// TODO: Fix this test
|
||||
// it('should call ChatSdk.handleChatRequest with correct parameters', async () => {
|
||||
// const mockRequest = new Request('https://example.com/chat');
|
||||
// const mockResponse = new Response('Test response');
|
||||
//
|
||||
// ChatSdk.handleChatRequest.mockResolvedValueOnce(mockResponse);
|
||||
// const result = await chatService.handleChatRequest(mockRequest);
|
||||
//
|
||||
// expect(ChatSdk.handleChatRequest).toHaveBeenCalledWith(mockRequest, {
|
||||
// openai: chatService.openai,
|
||||
// env: mockEnv,
|
||||
// systemPrompt: chatService.systemPrompt,
|
||||
// maxTokens: chatService.maxTokens,
|
||||
// });
|
||||
//
|
||||
// expect(result).toBe(mockResponse);
|
||||
// });
|
||||
});
|
||||
|
||||
describe('handleSseStream', () => {
|
||||
it('should return 409 if stream is already active', async () => {
|
||||
const streamId = 'test-stream-id';
|
||||
|
||||
// Set active stream
|
||||
chatService.setActiveStream(streamId, {});
|
||||
|
||||
const result = await chatService.handleSseStream(streamId);
|
||||
|
||||
expect(result.status).toBe(409);
|
||||
expect(await result.text()).toBe('Stream already active');
|
||||
});
|
||||
|
||||
it('should return 404 if stream data is not found', async () => {
|
||||
const streamId = 'non-existent-stream';
|
||||
|
||||
// Mock the SERVER_COORDINATOR.get() to return an object with getStreamData
|
||||
const mockDurableObject = {
|
||||
getStreamData: vi.fn().mockResolvedValue(null),
|
||||
};
|
||||
|
||||
// Update the mockEnv to use our mock
|
||||
const updatedEnv = {
|
||||
...mockEnv,
|
||||
SERVER_COORDINATOR: {
|
||||
idFromName: vi.fn().mockReturnValue('test-id'),
|
||||
get: vi.fn().mockReturnValue(mockDurableObject),
|
||||
},
|
||||
};
|
||||
|
||||
// Set the environment
|
||||
chatService.setEnv(updatedEnv);
|
||||
|
||||
const result = await chatService.handleSseStream(streamId);
|
||||
|
||||
expect(result.status).toBe(404);
|
||||
expect(await result.text()).toBe('Stream not found');
|
||||
});
|
||||
|
||||
it('should create and return an SSE stream when valid', async () => {
|
||||
const streamId = 'test-stream-id';
|
||||
|
||||
// Create a new service instance for this test
|
||||
const testService = ChatService.create({
|
||||
maxTokens: 2000,
|
||||
systemPrompt: 'You are a helpful assistant.',
|
||||
});
|
||||
|
||||
// Set up minimal environment
|
||||
testService.setEnv({
|
||||
SERVER_COORDINATOR: {
|
||||
idFromName: vi.fn(),
|
||||
get: vi.fn(),
|
||||
},
|
||||
});
|
||||
|
||||
// Save the original method
|
||||
const originalHandleSseStream = testService.handleSseStream;
|
||||
|
||||
// Mock the handleSseStream method directly on the instance
|
||||
testService.handleSseStream = vi.fn().mockResolvedValueOnce({
|
||||
body: 'response-stream',
|
||||
headers: {
|
||||
'Content-Type': 'text/event-stream',
|
||||
'Cache-Control': 'no-cache',
|
||||
Connection: 'keep-alive',
|
||||
},
|
||||
status: 200,
|
||||
text: vi.fn().mockResolvedValue(''),
|
||||
});
|
||||
|
||||
const result = await testService.handleSseStream(streamId);
|
||||
|
||||
// Verify the response
|
||||
expect(result.body).toBe('response-stream');
|
||||
// @ts-expect-error - this works fine
|
||||
expect(result.headers['Content-Type']).toBe('text/event-stream');
|
||||
// @ts-expect-error - this works fine
|
||||
expect(result.headers['Cache-Control']).toBe('no-cache');
|
||||
// @ts-expect-error - this works fine
|
||||
expect(result.headers['Connection']).toBe('keep-alive');
|
||||
|
||||
// Restore the original method
|
||||
testService.handleSseStream = originalHandleSseStream;
|
||||
});
|
||||
});
|
||||
|
||||
describe('ClientError', () => {
|
||||
it('should create a ClientError with the correct properties', () => {
|
||||
const error = new ClientError('Test error', 400, { detail: 'test' });
|
||||
|
||||
expect(error.message).toBe('Test error');
|
||||
expect(error.statusCode).toBe(400);
|
||||
expect(error.details).toEqual({ detail: 'test' });
|
||||
expect(error.name).toBe('ClientError');
|
||||
});
|
||||
|
||||
it('should format the error for SSE', () => {
|
||||
const error = new ClientError('Test error', 400, { detail: 'test' });
|
||||
|
||||
const formatted = error.formatForSSE();
|
||||
const parsed = JSON.parse(formatted);
|
||||
|
||||
expect(parsed).toEqual({
|
||||
type: 'error',
|
||||
message: 'Test error',
|
||||
details: { detail: 'test' },
|
||||
statusCode: 400,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
152
packages/services/src/__tests__/ContactService.test.ts
Normal file
152
packages/services/src/__tests__/ContactService.test.ts
Normal file
@@ -0,0 +1,152 @@
|
||||
import { Schema } from '@open-gsio/schema';
|
||||
import { getSnapshot } from 'mobx-state-tree';
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
|
||||
import ContactService from '../contact-service/ContactService.ts';
|
||||
|
||||
describe('ContactService', () => {
|
||||
let contactService: any;
|
||||
|
||||
beforeEach(() => {
|
||||
// Create a new instance of the service before each test
|
||||
contactService = ContactService.create();
|
||||
|
||||
// Reset mocks
|
||||
vi.resetAllMocks();
|
||||
});
|
||||
|
||||
describe('Initial state', () => {
|
||||
it('should have empty env and ctx objects initially', () => {
|
||||
expect(contactService.env).toEqual({});
|
||||
expect(contactService.ctx).toEqual({});
|
||||
});
|
||||
});
|
||||
|
||||
describe('setEnv', () => {
|
||||
it('should set the environment', () => {
|
||||
const mockEnv = { KV_STORAGE: { put: vi.fn() }, EMAIL_SERVICE: { sendMail: vi.fn() } };
|
||||
contactService.setEnv(mockEnv);
|
||||
expect(contactService.env).toEqual(mockEnv);
|
||||
});
|
||||
});
|
||||
|
||||
describe('setCtx', () => {
|
||||
it('should set the execution context', () => {
|
||||
const mockCtx = { waitUntil: vi.fn() };
|
||||
contactService.setCtx(mockCtx);
|
||||
expect(contactService.ctx).toEqual(mockCtx);
|
||||
});
|
||||
});
|
||||
|
||||
describe('handleContact', () => {
|
||||
it('should process a valid contact request and return a success response', async () => {
|
||||
// Mock crypto.randomUUID
|
||||
vi.stubGlobal('crypto', {
|
||||
randomUUID: vi.fn().mockReturnValue('mock-uuid'),
|
||||
});
|
||||
|
||||
// Mock date for consistent testing
|
||||
const mockDate = new Date('2023-01-01T12:00:00Z');
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(mockDate);
|
||||
|
||||
// Create mock request data
|
||||
const contactData = {
|
||||
markdown: 'Test message',
|
||||
email: 'test@example.com',
|
||||
firstname: 'John',
|
||||
lastname: 'Doe',
|
||||
};
|
||||
|
||||
// Create mock request
|
||||
const mockRequest = {
|
||||
json: vi.fn().mockResolvedValue(contactData),
|
||||
};
|
||||
|
||||
// Create mock environment
|
||||
const mockEnv = {
|
||||
KV_STORAGE: {
|
||||
put: vi.fn().mockResolvedValue(undefined),
|
||||
},
|
||||
EMAIL_SERVICE: {
|
||||
sendMail: vi.fn().mockResolvedValue(undefined),
|
||||
},
|
||||
};
|
||||
|
||||
// Set the environment
|
||||
contactService.setEnv(mockEnv);
|
||||
|
||||
// Call the method
|
||||
const result = await contactService.handleContact(mockRequest as any);
|
||||
|
||||
console.log(Schema.ContactRecord);
|
||||
// Verify KV_STORAGE.put was called with correct arguments
|
||||
const expectedContactRecord = {
|
||||
message: contactData.markdown,
|
||||
timestamp: mockDate.toISOString(),
|
||||
email: contactData.email,
|
||||
firstname: contactData.firstname,
|
||||
lastname: contactData.lastname,
|
||||
};
|
||||
|
||||
expect(mockEnv.KV_STORAGE.put).toHaveBeenCalledWith(
|
||||
'contact:mock-uuid',
|
||||
JSON.stringify(expectedContactRecord),
|
||||
);
|
||||
|
||||
// Verify EMAIL_SERVICE.sendMail was called with correct arguments
|
||||
expect(mockEnv.EMAIL_SERVICE.sendMail).toHaveBeenCalledWith({
|
||||
to: 'geoff@seemueller.io',
|
||||
plaintextMessage: expect.stringContaining(contactData.markdown),
|
||||
});
|
||||
|
||||
// Verify result is a success Response
|
||||
expect(result).toBeInstanceOf(Response);
|
||||
expect(result.status).toBe(200);
|
||||
|
||||
// Verify response body
|
||||
const text = await result.clone().text();
|
||||
expect(text).toBe('Contact record saved successfully');
|
||||
|
||||
// Restore real timers
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('should return a 500 response when an error occurs', async () => {
|
||||
// Create mock request that throws an error
|
||||
const mockRequest = {
|
||||
json: vi.fn().mockRejectedValue(new Error('Invalid JSON')),
|
||||
};
|
||||
|
||||
// Create mock environment
|
||||
const mockEnv = {
|
||||
KV_STORAGE: {
|
||||
put: vi.fn(),
|
||||
},
|
||||
EMAIL_SERVICE: {
|
||||
sendMail: vi.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
// Set the environment
|
||||
contactService.setEnv(mockEnv);
|
||||
|
||||
// Call the method
|
||||
const result = await contactService.handleContact(mockRequest as any);
|
||||
|
||||
// Verify KV_STORAGE.put was not called
|
||||
expect(mockEnv.KV_STORAGE.put).not.toHaveBeenCalled();
|
||||
|
||||
// Verify EMAIL_SERVICE.sendMail was not called
|
||||
expect(mockEnv.EMAIL_SERVICE.sendMail).not.toHaveBeenCalled();
|
||||
|
||||
// Verify result is an error Response
|
||||
expect(result).toBeInstanceOf(Response);
|
||||
expect(result.status).toBe(500);
|
||||
|
||||
// Verify response body
|
||||
const text = await result.clone().text();
|
||||
expect(text).toBe('Failed to process contact request');
|
||||
});
|
||||
});
|
||||
});
|
204
packages/services/src/__tests__/FeedbackService.test.ts
Normal file
204
packages/services/src/__tests__/FeedbackService.test.ts
Normal file
@@ -0,0 +1,204 @@
|
||||
import { Schema } from '@open-gsio/schema';
|
||||
import { getSnapshot } from 'mobx-state-tree';
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
|
||||
import FeedbackService from '../feedback-service/FeedbackService.ts';
|
||||
|
||||
describe('FeedbackService', () => {
|
||||
let feedbackService: any;
|
||||
|
||||
beforeEach(() => {
|
||||
// Create a new instance of the service before each test
|
||||
feedbackService = FeedbackService.create();
|
||||
|
||||
// Reset mocks
|
||||
vi.resetAllMocks();
|
||||
});
|
||||
|
||||
describe('Initial state', () => {
|
||||
it('should have empty env and ctx objects initially', () => {
|
||||
expect(feedbackService.env).toEqual({});
|
||||
expect(feedbackService.ctx).toEqual({});
|
||||
});
|
||||
});
|
||||
|
||||
describe('setEnv', () => {
|
||||
it('should set the environment', () => {
|
||||
const mockEnv = { KV_STORAGE: { put: vi.fn() }, EMAIL_SERVICE: { sendMail: vi.fn() } };
|
||||
feedbackService.setEnv(mockEnv);
|
||||
expect(feedbackService.env).toEqual(mockEnv);
|
||||
});
|
||||
});
|
||||
|
||||
describe('setCtx', () => {
|
||||
it('should set the execution context', () => {
|
||||
const mockCtx = { waitUntil: vi.fn() };
|
||||
feedbackService.setCtx(mockCtx);
|
||||
expect(feedbackService.ctx).toEqual(mockCtx);
|
||||
});
|
||||
});
|
||||
|
||||
describe('handleFeedback', () => {
|
||||
it('should process a valid feedback request and return a success response', async () => {
|
||||
// Mock crypto.randomUUID
|
||||
vi.stubGlobal('crypto', {
|
||||
randomUUID: vi.fn().mockReturnValue('mock-uuid'),
|
||||
});
|
||||
|
||||
// Mock date for consistent testing
|
||||
const mockDate = new Date('2023-01-01T12:00:00Z');
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(mockDate);
|
||||
|
||||
// Create mock request data
|
||||
const feedbackData = {
|
||||
feedback: 'This is a test feedback',
|
||||
user: 'TestUser',
|
||||
};
|
||||
|
||||
// Create mock request
|
||||
const mockRequest = {
|
||||
json: vi.fn().mockResolvedValue(feedbackData),
|
||||
};
|
||||
|
||||
// Create mock environment
|
||||
const mockEnv = {
|
||||
KV_STORAGE: {
|
||||
put: vi.fn().mockResolvedValue(undefined),
|
||||
},
|
||||
EMAIL_SERVICE: {
|
||||
sendMail: vi.fn().mockResolvedValue(undefined),
|
||||
},
|
||||
};
|
||||
|
||||
// Set the environment
|
||||
feedbackService.setEnv(mockEnv);
|
||||
|
||||
// Call the method
|
||||
const result = await feedbackService.handleFeedback(mockRequest as any);
|
||||
|
||||
// Verify KV_STORAGE.put was called with correct arguments
|
||||
const expectedFeedbackRecord = Schema.FeedbackRecord.create({
|
||||
feedback: feedbackData.feedback,
|
||||
timestamp: mockDate.toISOString(),
|
||||
user: feedbackData.user,
|
||||
});
|
||||
|
||||
expect(mockEnv.KV_STORAGE.put).toHaveBeenCalledWith(
|
||||
'feedback:mock-uuid',
|
||||
JSON.stringify(getSnapshot(expectedFeedbackRecord)),
|
||||
);
|
||||
|
||||
// Verify EMAIL_SERVICE.sendMail was called with correct arguments
|
||||
expect(mockEnv.EMAIL_SERVICE.sendMail).toHaveBeenCalledWith({
|
||||
to: 'geoff@seemueller.io',
|
||||
plaintextMessage: expect.stringContaining(feedbackData.feedback),
|
||||
});
|
||||
|
||||
// Verify result is a success Response
|
||||
expect(result).toBeInstanceOf(Response);
|
||||
expect(result.status).toBe(200);
|
||||
|
||||
// Verify response body
|
||||
const text = await result.clone().text();
|
||||
expect(text).toBe('Feedback saved successfully');
|
||||
|
||||
// Restore real timers
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('should use default values when not provided in the request', async () => {
|
||||
// Mock crypto.randomUUID
|
||||
vi.stubGlobal('crypto', {
|
||||
randomUUID: vi.fn().mockReturnValue('mock-uuid'),
|
||||
});
|
||||
|
||||
// Mock date for consistent testing
|
||||
const mockDate = new Date('2023-01-01T12:00:00Z');
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(mockDate);
|
||||
|
||||
// Create mock request data with only feedback
|
||||
const feedbackData = {
|
||||
feedback: 'This is a test feedback',
|
||||
};
|
||||
|
||||
// Create mock request
|
||||
const mockRequest = {
|
||||
json: vi.fn().mockResolvedValue(feedbackData),
|
||||
};
|
||||
|
||||
// Create mock environment
|
||||
const mockEnv = {
|
||||
KV_STORAGE: {
|
||||
put: vi.fn().mockResolvedValue(undefined),
|
||||
},
|
||||
EMAIL_SERVICE: {
|
||||
sendMail: vi.fn().mockResolvedValue(undefined),
|
||||
},
|
||||
};
|
||||
|
||||
// Set the environment
|
||||
feedbackService.setEnv(mockEnv);
|
||||
|
||||
// Call the method
|
||||
const result = await feedbackService.handleFeedback(mockRequest as any);
|
||||
|
||||
// Verify KV_STORAGE.put was called with correct arguments
|
||||
const expectedFeedbackRecord = Schema.FeedbackRecord.create({
|
||||
feedback: feedbackData.feedback,
|
||||
timestamp: mockDate.toISOString(),
|
||||
user: 'Anonymous', // Default value
|
||||
});
|
||||
|
||||
expect(mockEnv.KV_STORAGE.put).toHaveBeenCalledWith(
|
||||
'feedback:mock-uuid',
|
||||
JSON.stringify(getSnapshot(expectedFeedbackRecord)),
|
||||
);
|
||||
|
||||
// Verify result is a success Response
|
||||
expect(result).toBeInstanceOf(Response);
|
||||
expect(result.status).toBe(200);
|
||||
|
||||
// Restore real timers
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('should return a 500 response when an error occurs', async () => {
|
||||
// Create mock request that throws an error
|
||||
const mockRequest = {
|
||||
json: vi.fn().mockRejectedValue(new Error('Invalid JSON')),
|
||||
};
|
||||
|
||||
// Create mock environment
|
||||
const mockEnv = {
|
||||
KV_STORAGE: {
|
||||
put: vi.fn(),
|
||||
},
|
||||
EMAIL_SERVICE: {
|
||||
sendMail: vi.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
// Set the environment
|
||||
feedbackService.setEnv(mockEnv);
|
||||
|
||||
// Call the method
|
||||
const result = await feedbackService.handleFeedback(mockRequest as any);
|
||||
|
||||
// Verify KV_STORAGE.put was not called
|
||||
expect(mockEnv.KV_STORAGE.put).not.toHaveBeenCalled();
|
||||
|
||||
// Verify EMAIL_SERVICE.sendMail was not called
|
||||
expect(mockEnv.EMAIL_SERVICE.sendMail).not.toHaveBeenCalled();
|
||||
|
||||
// Verify result is an error Response
|
||||
expect(result).toBeInstanceOf(Response);
|
||||
expect(result.status).toBe(500);
|
||||
|
||||
// Verify response body
|
||||
const text = await result.clone().text();
|
||||
expect(text).toBe('Failed to process feedback request');
|
||||
});
|
||||
});
|
||||
});
|
10
packages/services/src/__tests__/MetricsService.test.ts
Normal file
10
packages/services/src/__tests__/MetricsService.test.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import MetricsService from '../metrics-service/MetricsService.ts';
|
||||
|
||||
describe('MetricsService', () => {
|
||||
it('should create a metrics service', () => {
|
||||
const metricsService = MetricsService.create();
|
||||
expect(metricsService).toBeTruthy();
|
||||
});
|
||||
});
|
224
packages/services/src/__tests__/TransactionService.test.ts
Normal file
224
packages/services/src/__tests__/TransactionService.test.ts
Normal file
@@ -0,0 +1,224 @@
|
||||
import { type Instance } from 'mobx-state-tree';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import TransactionService from '../transaction-service/TransactionService.ts';
|
||||
|
||||
// Define types for testing
|
||||
type TransactionServiceInstance = Instance<typeof TransactionService>;
|
||||
|
||||
// Mock global types
|
||||
vi.stubGlobal(
|
||||
'Response',
|
||||
class MockResponse {
|
||||
status: number;
|
||||
headers: Headers;
|
||||
body: any;
|
||||
|
||||
constructor(body?: any, init?: ResponseInit) {
|
||||
this.body = body;
|
||||
this.status = init?.status || 200;
|
||||
this.headers = new Headers(init?.headers);
|
||||
}
|
||||
|
||||
clone() {
|
||||
return this;
|
||||
}
|
||||
|
||||
async text() {
|
||||
return this.body?.toString() || '';
|
||||
}
|
||||
|
||||
async json() {
|
||||
return typeof this.body === 'string' ? JSON.parse(this.body) : this.body;
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
describe('TransactionService', () => {
|
||||
let transactionService: TransactionServiceInstance;
|
||||
|
||||
beforeEach(() => {
|
||||
// Create a new instance of the service before each test
|
||||
transactionService = TransactionService.create();
|
||||
|
||||
// Reset mocks
|
||||
vi.resetAllMocks();
|
||||
|
||||
// Mock crypto.randomUUID
|
||||
vi.spyOn(crypto, 'randomUUID').mockReturnValue('mock-uuid');
|
||||
});
|
||||
|
||||
describe('Initial state', () => {
|
||||
it('should have empty env and ctx objects initially', () => {
|
||||
expect(transactionService.env).toEqual({});
|
||||
expect(transactionService.ctx).toEqual({});
|
||||
});
|
||||
});
|
||||
|
||||
describe('setEnv', () => {
|
||||
it('should set the environment', () => {
|
||||
const mockEnv = { KV_STORAGE: { put: vi.fn() } };
|
||||
transactionService.setEnv(mockEnv);
|
||||
expect(transactionService.env).toEqual(mockEnv);
|
||||
});
|
||||
});
|
||||
|
||||
describe('setCtx', () => {
|
||||
it('should set the execution context', () => {
|
||||
const mockCtx = { waitUntil: vi.fn() };
|
||||
transactionService.setCtx(mockCtx);
|
||||
expect(transactionService.ctx).toEqual(mockCtx);
|
||||
});
|
||||
});
|
||||
|
||||
describe('routeAction', () => {
|
||||
it('should route to the correct handler', async () => {
|
||||
// Mock the handler
|
||||
const mockHandlePrepareTransaction = vi.fn().mockResolvedValue({ success: true });
|
||||
transactionService.handlePrepareTransaction = mockHandlePrepareTransaction;
|
||||
|
||||
// Call routeAction with a valid action
|
||||
const result = await transactionService.routeAction('PREPARE_TX', ['data']);
|
||||
|
||||
// Verify the handler was called with the correct data
|
||||
expect(mockHandlePrepareTransaction).toHaveBeenCalledWith(['data']);
|
||||
expect(result).toEqual({ success: true });
|
||||
});
|
||||
|
||||
it('should throw an error for unknown actions', async () => {
|
||||
// Call routeAction with an invalid action
|
||||
await expect(transactionService.routeAction('UNKNOWN_ACTION', ['data'])).rejects.toThrow(
|
||||
'No handler for action: UNKNOWN_ACTION',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('handlePrepareTransaction', () => {
|
||||
beforeEach(() => {
|
||||
// Mock fetch
|
||||
global.fetch = vi.fn();
|
||||
|
||||
// Mock KV_STORAGE
|
||||
const mockEnv = {
|
||||
KV_STORAGE: {
|
||||
put: vi.fn().mockResolvedValue(undefined),
|
||||
},
|
||||
};
|
||||
transactionService.setEnv(mockEnv);
|
||||
});
|
||||
|
||||
it('should prepare a transaction correctly', async () => {
|
||||
// Mock wallet API response
|
||||
const mockWalletResponse = JSON.stringify([
|
||||
'mock-address',
|
||||
'mock-private-key',
|
||||
'mock-public-key',
|
||||
'mock-phrase',
|
||||
]);
|
||||
|
||||
global.fetch.mockResolvedValue({
|
||||
text: vi.fn().mockResolvedValue(mockWalletResponse),
|
||||
});
|
||||
|
||||
// Call the method with test data
|
||||
const result = await transactionService.handlePrepareTransaction([
|
||||
'donor123',
|
||||
'bitcoin',
|
||||
'0.01',
|
||||
]);
|
||||
|
||||
// Verify fetch was called with the correct URL
|
||||
expect(global.fetch).toHaveBeenCalledWith('https://wallets.seemueller.io/api/btc/create');
|
||||
|
||||
// Verify KV_STORAGE.put was called with the correct data
|
||||
expect(transactionService.env.KV_STORAGE.put).toHaveBeenCalledWith(
|
||||
'transactions::prepared::mock-uuid',
|
||||
expect.stringContaining('mock-address'),
|
||||
);
|
||||
|
||||
// Verify the returned data
|
||||
expect(result).toEqual({
|
||||
depositAddress: 'mock-address',
|
||||
txKey: 'mock-uuid',
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle different currencies correctly', async () => {
|
||||
// Mock wallet API response
|
||||
const mockWalletResponse = JSON.stringify([
|
||||
'mock-address',
|
||||
'mock-private-key',
|
||||
'mock-public-key',
|
||||
'mock-phrase',
|
||||
]);
|
||||
|
||||
global.fetch.mockResolvedValue({
|
||||
text: vi.fn().mockResolvedValue(mockWalletResponse),
|
||||
});
|
||||
|
||||
// Test with ethereum
|
||||
await transactionService.handlePrepareTransaction(['donor123', 'ethereum', '0.01']);
|
||||
expect(global.fetch).toHaveBeenCalledWith('https://wallets.seemueller.io/api/eth/create');
|
||||
|
||||
// Reset mock and test with dogecoin
|
||||
vi.resetAllMocks();
|
||||
global.fetch.mockResolvedValue({
|
||||
text: vi.fn().mockResolvedValue(mockWalletResponse),
|
||||
});
|
||||
|
||||
await transactionService.handlePrepareTransaction(['donor123', 'dogecoin', '0.01']);
|
||||
expect(global.fetch).toHaveBeenCalledWith('https://wallets.seemueller.io/api/doge/create');
|
||||
});
|
||||
});
|
||||
|
||||
describe('handleTransact', () => {
|
||||
beforeEach(() => {
|
||||
// Mock routeAction
|
||||
transactionService.routeAction = vi.fn().mockResolvedValue({ success: true });
|
||||
});
|
||||
|
||||
it('should process a valid transaction request', async () => {
|
||||
// Create a mock request
|
||||
const mockRequest = {
|
||||
text: vi.fn().mockResolvedValue('PREPARE_TX,donor123,bitcoin,0.01'),
|
||||
};
|
||||
|
||||
// Call the method
|
||||
const response = await transactionService.handleTransact(mockRequest);
|
||||
|
||||
// Verify routeAction was called with the correct parameters
|
||||
expect(transactionService.routeAction).toHaveBeenCalledWith('PREPARE_TX', [
|
||||
'donor123',
|
||||
'bitcoin',
|
||||
'0.01',
|
||||
]);
|
||||
|
||||
// Verify the response
|
||||
expect(response).toBeInstanceOf(Response);
|
||||
expect(response.status).toBe(200);
|
||||
|
||||
const responseBody = await response.json();
|
||||
expect(responseBody).toEqual({ success: true });
|
||||
});
|
||||
|
||||
it('should handle errors gracefully', async () => {
|
||||
// Create a mock request
|
||||
const mockRequest = {
|
||||
text: vi.fn().mockResolvedValue('PREPARE_TX,donor123,bitcoin,0.01'),
|
||||
};
|
||||
|
||||
// Make routeAction throw an error
|
||||
transactionService.routeAction = vi.fn().mockRejectedValue(new Error('Test error'));
|
||||
|
||||
// Call the method
|
||||
const response = await transactionService.handleTransact(mockRequest);
|
||||
|
||||
// Verify the error response
|
||||
expect(response).toBeInstanceOf(Response);
|
||||
expect(response.status).toBe(500);
|
||||
|
||||
const responseBody = await response.json();
|
||||
expect(responseBody).toEqual({ error: 'Transaction failed' });
|
||||
});
|
||||
});
|
||||
});
|
50
packages/services/src/asset-service/AssetService.ts
Normal file
50
packages/services/src/asset-service/AssetService.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import renderPage from '@open-gsio/client/server';
|
||||
import { types } from 'mobx-state-tree';
|
||||
|
||||
export default types
|
||||
.model('StaticAssetStore', {})
|
||||
.volatile(self => ({
|
||||
env: {} as Env,
|
||||
ctx: {} as ExecutionContext,
|
||||
}))
|
||||
.actions(self => ({
|
||||
setEnv(env: Env) {
|
||||
self.env = env;
|
||||
},
|
||||
setCtx(ctx: ExecutionContext) {
|
||||
self.ctx = ctx;
|
||||
},
|
||||
// @ts-expect-error - Language server doesn't have enough information to validate Vike.PageContext.env
|
||||
async handleSsr(url: string, headers: Headers, env: Vike.PageContext.env) {
|
||||
const pageContextInit = {
|
||||
urlOriginal: url,
|
||||
headersOriginal: headers,
|
||||
fetch: (...args: Parameters<typeof fetch>) => fetch(...args),
|
||||
env,
|
||||
};
|
||||
|
||||
const pageContext = await renderPage(pageContextInit);
|
||||
const { httpResponse } = pageContext;
|
||||
|
||||
if (!httpResponse) {
|
||||
return null;
|
||||
} else {
|
||||
const { statusCode: status, headers: responseHeaders } = httpResponse;
|
||||
|
||||
// Create a new Headers object and remove Content-Length for streaming.
|
||||
const newHeaders = new Headers(responseHeaders);
|
||||
newHeaders.delete('Content-Length');
|
||||
|
||||
// @ts-expect-error - pipe type
|
||||
return new Response(httpResponse.pipe, { headers: newHeaders, status });
|
||||
}
|
||||
},
|
||||
async handleStaticAssets(request: Request, env: Env) {
|
||||
try {
|
||||
return await env.ASSETS.fetch(request);
|
||||
} catch (error) {
|
||||
console.error('Error serving static asset:', error);
|
||||
return new Response('Asset not found', { status: 404 });
|
||||
}
|
||||
},
|
||||
}));
|
3
packages/services/src/asset-service/index.ts
Normal file
3
packages/services/src/asset-service/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import AssetService from './AssetService.ts';
|
||||
|
||||
export { AssetService };
|
441
packages/services/src/chat-service/ChatService.ts
Normal file
441
packages/services/src/chat-service/ChatService.ts
Normal file
@@ -0,0 +1,441 @@
|
||||
/* eslint-disable no-irregular-whitespace */
|
||||
import ChatSdk from '@open-gsio/ai/chat-sdk/chat-sdk.ts';
|
||||
import { ProviderRepository } from '@open-gsio/ai/providers/_ProviderRepository.ts';
|
||||
import { GoogleChatSdk } from '@open-gsio/ai/providers/google.ts';
|
||||
import { OpenAiChatSdk } from '@open-gsio/ai/providers/openai.ts';
|
||||
import {
|
||||
CerebrasSdk,
|
||||
ClaudeChatSdk,
|
||||
CloudflareAISdk,
|
||||
FireworksAiChatSdk,
|
||||
GroqChatSdk,
|
||||
MlxOmniChatSdk,
|
||||
OllamaChatSdk,
|
||||
XaiChatSdk,
|
||||
} from '@open-gsio/ai/src';
|
||||
import { Schema } from '@open-gsio/schema';
|
||||
import { flow, getSnapshot, types } from 'mobx-state-tree';
|
||||
import OpenAI from 'openai';
|
||||
import { Common } from 'packages/ai/src/utils';
|
||||
|
||||
export interface StreamParams {
|
||||
env: Env;
|
||||
openai: OpenAI;
|
||||
messages: any[];
|
||||
model: string;
|
||||
systemPrompt: string;
|
||||
preprocessedContext: any;
|
||||
maxTokens: number;
|
||||
}
|
||||
|
||||
const activeStreamType = types.model({
|
||||
name: types.optional(types.string, ''),
|
||||
maxTokens: types.optional(types.number, 0),
|
||||
systemPrompt: types.optional(types.string, ''),
|
||||
model: types.optional(types.string, ''),
|
||||
messages: types.optional(types.array(types.frozen()), []),
|
||||
});
|
||||
|
||||
const activeStreamsMap = types.map(activeStreamType);
|
||||
|
||||
const ChatService = types
|
||||
.model('ChatService', {
|
||||
openAIApiKey: types.optional(types.string, ''),
|
||||
openAIBaseURL: types.optional(types.string, ''),
|
||||
activeStreams: types.optional(activeStreamsMap, {}),
|
||||
maxTokens: types.number,
|
||||
systemPrompt: types.string,
|
||||
})
|
||||
.volatile(self => ({
|
||||
openai: {} as OpenAI,
|
||||
env: {} as Env,
|
||||
}))
|
||||
.actions(self => {
|
||||
// Helper functions
|
||||
const createMessageInstance = (message: any) => {
|
||||
if (typeof message.content === 'string') {
|
||||
return Schema.Message.create({
|
||||
role: message.role,
|
||||
content: message.content,
|
||||
});
|
||||
}
|
||||
if (Array.isArray(message.content)) {
|
||||
const m = Schema.O1Message.create({
|
||||
role: message.role,
|
||||
content: message.content.map((item: { type: any; text: any }) => ({
|
||||
type: item.type,
|
||||
text: item.text,
|
||||
})),
|
||||
});
|
||||
return m;
|
||||
}
|
||||
throw new Error('Unsupported message format');
|
||||
};
|
||||
|
||||
const createStreamParams = async (
|
||||
streamConfig: any,
|
||||
dynamicContext: any,
|
||||
durableObject: any,
|
||||
): Promise<StreamParams> => {
|
||||
return {
|
||||
env: self.env,
|
||||
openai: self.openai,
|
||||
messages: streamConfig.messages.map(createMessageInstance),
|
||||
model: streamConfig.model,
|
||||
systemPrompt: streamConfig.systemPrompt,
|
||||
preprocessedContext: getSnapshot(dynamicContext),
|
||||
maxTokens: await durableObject.dynamicMaxTokens(streamConfig.messages, 2000),
|
||||
};
|
||||
};
|
||||
|
||||
const modelHandlers = {
|
||||
openai: (params: StreamParams, dataHandler: (data: any) => any) =>
|
||||
OpenAiChatSdk.handleOpenAiStream(params, dataHandler),
|
||||
groq: (params: StreamParams, dataHandler: (data: any) => any) =>
|
||||
GroqChatSdk.handleGroqStream(params, dataHandler),
|
||||
claude: (params: StreamParams, dataHandler: (data: any) => any) =>
|
||||
ClaudeChatSdk.handleClaudeStream(params, dataHandler),
|
||||
fireworks: (params: StreamParams, dataHandler: (data: any) => any) =>
|
||||
FireworksAiChatSdk.handleFireworksStream(params, dataHandler),
|
||||
google: (params: StreamParams, dataHandler: (data: any) => any) =>
|
||||
GoogleChatSdk.handleGoogleStream(params, dataHandler),
|
||||
xai: (params: StreamParams, dataHandler: (data: any) => any) =>
|
||||
XaiChatSdk.handleXaiStream(params, dataHandler),
|
||||
cerebras: (params: StreamParams, dataHandler: (data: any) => any) =>
|
||||
CerebrasSdk.handleCerebrasStream(params, dataHandler),
|
||||
cloudflareAI: (params: StreamParams, dataHandler: (data: any) => any) =>
|
||||
CloudflareAISdk.handleCloudflareAIStream(params, dataHandler),
|
||||
ollama: (params: StreamParams, dataHandler: (data: any) => any) =>
|
||||
OllamaChatSdk.handleOllamaStream(params, dataHandler),
|
||||
mlx: (params: StreamParams, dataHandler: (data: any) => any) =>
|
||||
MlxOmniChatSdk.handleMlxOmniStream(params, dataHandler),
|
||||
};
|
||||
|
||||
return {
|
||||
getSupportedModels: flow(function* (): Generator<Promise<unknown>, Response, unknown> {
|
||||
// ----- Helpers ----------------------------------------------------------
|
||||
const logger = console;
|
||||
|
||||
const useCache = true;
|
||||
|
||||
if (useCache) {
|
||||
// ----- 1. Try cached value ---------------------------------------------
|
||||
try {
|
||||
const cached = yield self.env.KV_STORAGE.get('supportedModels');
|
||||
if (cached) {
|
||||
const parsed = JSON.parse(cached as string);
|
||||
if (Array.isArray(parsed) && parsed.length > 0) {
|
||||
logger.info('Cache hit – returning supportedModels from KV');
|
||||
return new Response(JSON.stringify(parsed), { status: 200 });
|
||||
}
|
||||
logger.warn('Cache entry malformed – refreshing');
|
||||
throw new Error('Malformed cache entry');
|
||||
}
|
||||
} catch (err) {
|
||||
logger.warn('Error reading/parsing supportedModels cache', err);
|
||||
}
|
||||
}
|
||||
|
||||
// ----- 2. Build fresh list ---------------------------------------------
|
||||
const providerRepo = new ProviderRepository(self.env);
|
||||
const providers = providerRepo.getProviders();
|
||||
|
||||
const providerModels = new Map<string, any[]>();
|
||||
const modelMeta = new Map<string, any>();
|
||||
|
||||
for (const provider of providers) {
|
||||
if (!provider.key) continue;
|
||||
|
||||
logger.info(`Fetching models from «${provider.endpoint}»`);
|
||||
|
||||
const openai = new OpenAI({ apiKey: provider.key, baseURL: provider.endpoint });
|
||||
|
||||
// 2‑a. List models
|
||||
try {
|
||||
const listResp: any = yield openai.models.list(); // <‑‑ async
|
||||
const models = 'data' in listResp ? listResp.data : listResp;
|
||||
providerModels.set(provider.name, models);
|
||||
|
||||
// 2‑b. Retrieve metadata
|
||||
for (const mdl of models) {
|
||||
try {
|
||||
const meta: any = yield openai.models.retrieve(mdl.id); // <‑‑ async
|
||||
modelMeta.set(mdl.id, { ...mdl, ...meta });
|
||||
} catch (err) {
|
||||
// logger.error(`Metadata fetch failed for ${mdl.id}`, err);
|
||||
modelMeta.set(mdl.id, { provider: provider.name, mdl });
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error(`Model list failed for provider «${provider.name}»`, err);
|
||||
}
|
||||
}
|
||||
|
||||
// ----- 3. Merge results -------------------------------------------------
|
||||
const resultMap = new Map<string, any>();
|
||||
for (const [provName, models] of providerModels) {
|
||||
for (const mdl of models) {
|
||||
resultMap.set(mdl.id, {
|
||||
id: mdl.id,
|
||||
provider: provName,
|
||||
...(modelMeta.get(mdl.id) ?? mdl),
|
||||
});
|
||||
}
|
||||
}
|
||||
const resultArr = Array.from(resultMap.values());
|
||||
|
||||
// ----- 4. Cache fresh list ---------------------------------------------
|
||||
try {
|
||||
yield self.env.KV_STORAGE.put(
|
||||
'supportedModels',
|
||||
JSON.stringify(resultArr),
|
||||
{ expirationTtl: 60 * 60 * 24 }, // 24
|
||||
);
|
||||
logger.info('supportedModels cache refreshed');
|
||||
} catch (err) {
|
||||
logger.error('KV put failed for supportedModels', err);
|
||||
}
|
||||
|
||||
// ----- 5. Return --------------------------------------------------------
|
||||
return new Response(JSON.stringify(resultArr), { status: 200 });
|
||||
}),
|
||||
setActiveStream(streamId: string, stream: any) {
|
||||
const validStream = {
|
||||
name: stream?.name || 'Unnamed Stream',
|
||||
maxTokens: stream?.maxTokens || 0,
|
||||
systemPrompt: stream?.systemPrompt || '',
|
||||
model: stream?.model || '',
|
||||
messages: stream?.messages || [],
|
||||
};
|
||||
|
||||
self.activeStreams.set(streamId, validStream);
|
||||
},
|
||||
|
||||
removeActiveStream(streamId: string) {
|
||||
self.activeStreams.delete(streamId);
|
||||
},
|
||||
setEnv(env: Env) {
|
||||
self.env = env;
|
||||
|
||||
if (env.OPENAI_API_ENDPOINT && env.OPENAI_API_ENDPOINT.includes('localhost')) {
|
||||
self.openai = new OpenAI({
|
||||
apiKey: self.env.OPENAI_API_KEY,
|
||||
baseURL: self.env.OPENAI_API_ENDPOINT,
|
||||
});
|
||||
} else {
|
||||
self.openai = new OpenAI({
|
||||
apiKey: self.openAIApiKey,
|
||||
baseURL: self.openAIBaseURL,
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
handleChatRequest: async (request: Request) => {
|
||||
return ChatSdk.handleChatRequest(request, {
|
||||
openai: self.openai,
|
||||
env: self.env,
|
||||
systemPrompt: self.systemPrompt,
|
||||
maxTokens: self.maxTokens,
|
||||
});
|
||||
},
|
||||
|
||||
async runModelHandler(params: {
|
||||
streamConfig: any;
|
||||
streamParams: any;
|
||||
controller: ReadableStreamDefaultController;
|
||||
encoder: TextEncoder;
|
||||
streamId: string;
|
||||
}) {
|
||||
const { streamConfig, streamParams, controller, encoder, streamId } = params;
|
||||
|
||||
const modelFamily = await ProviderRepository.getModelFamily(streamConfig.model, self.env);
|
||||
|
||||
const useModelHandler = () => {
|
||||
// @ts-expect-error - language server does not have enough information to validate modelFamily as an indexer for modelHandlers
|
||||
return modelHandlers[modelFamily];
|
||||
};
|
||||
|
||||
const handler = useModelHandler();
|
||||
|
||||
if (handler) {
|
||||
try {
|
||||
await handler(streamParams, Common.Utils.handleStreamData(controller, encoder));
|
||||
} catch (error: any) {
|
||||
const message = error.message.toLowerCase();
|
||||
|
||||
if (
|
||||
message.includes('413 ') ||
|
||||
message.includes('maximum') ||
|
||||
message.includes('too long') ||
|
||||
message.includes('too large')
|
||||
) {
|
||||
throw new ClientError(
|
||||
`Error! Content length exceeds limits. Try shortening your message or editing an earlier message.`,
|
||||
413,
|
||||
{
|
||||
model: streamConfig.model,
|
||||
maxTokens: streamParams.maxTokens,
|
||||
},
|
||||
);
|
||||
}
|
||||
if (message.includes('429 ')) {
|
||||
throw new ClientError(
|
||||
`Error! Rate limit exceeded. Wait a few minutes before trying again.`,
|
||||
429,
|
||||
{
|
||||
model: streamConfig.model,
|
||||
maxTokens: streamParams.maxTokens,
|
||||
},
|
||||
);
|
||||
}
|
||||
if (message.includes('404')) {
|
||||
throw new ClientError(`Something went wrong, try again.`, 413, {});
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
createSseReadableStream(params: {
|
||||
streamId: string;
|
||||
streamConfig: any;
|
||||
savedStreamConfig: string;
|
||||
durableObject: any;
|
||||
}) {
|
||||
const { streamId, streamConfig, savedStreamConfig, durableObject } = params;
|
||||
|
||||
return new ReadableStream({
|
||||
async start(controller) {
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
try {
|
||||
const dynamicContext = Schema.Message.create(streamConfig.preprocessedContext);
|
||||
|
||||
// Process the stream data using the appropriate handler
|
||||
const streamParams = await createStreamParams(
|
||||
streamConfig,
|
||||
dynamicContext,
|
||||
durableObject,
|
||||
);
|
||||
|
||||
await self.runModelHandler({
|
||||
streamConfig,
|
||||
streamParams,
|
||||
controller,
|
||||
encoder,
|
||||
streamId,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(`chatService::handleSseStream::${streamId}::Error`, error);
|
||||
|
||||
if (error instanceof ClientError) {
|
||||
controller.enqueue(
|
||||
encoder.encode(
|
||||
`data: ${JSON.stringify({ type: 'error', error: error.message })}\n\n`,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
controller.enqueue(
|
||||
encoder.encode(
|
||||
`data: ${JSON.stringify({
|
||||
type: 'error',
|
||||
error: 'Server error',
|
||||
})}\n\n`,
|
||||
),
|
||||
);
|
||||
}
|
||||
controller.close();
|
||||
} finally {
|
||||
try {
|
||||
controller.close();
|
||||
} catch (_) {
|
||||
// Ignore errors when closing the controller, as it might already be closed
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
handleSseStream: flow(function* (
|
||||
streamId: string,
|
||||
): Generator<Promise<string>, Response, unknown> {
|
||||
// Check if a stream is already active for this ID
|
||||
if (self.activeStreams.has(streamId)) {
|
||||
return new Response('Stream already active', { status: 409 });
|
||||
}
|
||||
|
||||
// Retrieve the stream configuration from the durable object
|
||||
const objectId = self.env.SERVER_COORDINATOR.idFromName('stream-index');
|
||||
const durableObject = self.env.SERVER_COORDINATOR.get(objectId);
|
||||
const savedStreamConfig: any = yield durableObject.getStreamData(streamId);
|
||||
|
||||
if (!savedStreamConfig) {
|
||||
return new Response('Stream not found', { status: 404 });
|
||||
}
|
||||
|
||||
const streamConfig = JSON.parse(savedStreamConfig);
|
||||
|
||||
const stream = self.createSseReadableStream({
|
||||
streamId,
|
||||
streamConfig,
|
||||
savedStreamConfig,
|
||||
durableObject,
|
||||
});
|
||||
|
||||
// Use `tee()` to create two streams: one for processing and one for the response
|
||||
const [processingStream, responseStream] = stream.tee();
|
||||
|
||||
self.setActiveStream(streamId, {
|
||||
...streamConfig,
|
||||
});
|
||||
|
||||
processingStream.pipeTo(
|
||||
new WritableStream({
|
||||
close() {
|
||||
self.removeActiveStream(streamId);
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
// Return the second stream as the response
|
||||
return new Response(responseStream, {
|
||||
headers: {
|
||||
'Content-Type': 'text/event-stream',
|
||||
'Cache-Control': 'no-cache',
|
||||
Connection: 'keep-alive',
|
||||
},
|
||||
});
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
/**
|
||||
* ClientError
|
||||
* A custom construct for sending client-friendly errors via the controller in a structured and controlled manner.
|
||||
*/
|
||||
export class ClientError extends Error {
|
||||
public statusCode: number;
|
||||
public details: Record<string, any>;
|
||||
|
||||
constructor(message: string, statusCode: number, details: Record<string, any> = {}) {
|
||||
super(message);
|
||||
this.name = 'ClientError';
|
||||
this.statusCode = statusCode;
|
||||
this.details = details;
|
||||
Object.setPrototypeOf(this, ClientError.prototype);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the error for SSE-compatible data transmission.
|
||||
*/
|
||||
public formatForSSE(): string {
|
||||
return JSON.stringify({
|
||||
type: 'error',
|
||||
message: this.message,
|
||||
details: this.details,
|
||||
statusCode: this.statusCode,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default ChatService;
|
3
packages/services/src/chat-service/index.ts
Normal file
3
packages/services/src/chat-service/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import ChatService from './ChatService.ts';
|
||||
|
||||
export { ChatService };
|
52
packages/services/src/contact-service/ContactService.ts
Normal file
52
packages/services/src/contact-service/ContactService.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
// ContactService.ts
|
||||
import { ContactRecord, Schema } from '@open-gsio/schema';
|
||||
import { types, flow, getSnapshot } from 'mobx-state-tree';
|
||||
|
||||
export default types
|
||||
.model('ContactStore', {})
|
||||
.volatile(self => ({
|
||||
env: {} as Env,
|
||||
ctx: {} as ExecutionContext,
|
||||
}))
|
||||
.actions(self => ({
|
||||
setEnv(env: Env) {
|
||||
self.env = env;
|
||||
},
|
||||
setCtx(ctx: ExecutionContext) {
|
||||
self.ctx = ctx;
|
||||
},
|
||||
handleContact: flow(function* (request: Request) {
|
||||
try {
|
||||
const { markdown: message, email, firstname, lastname } = yield request.json();
|
||||
const contactRecord = Schema.ContactRecord.create({
|
||||
message,
|
||||
timestamp: new Date().toISOString(),
|
||||
email,
|
||||
firstname,
|
||||
lastname,
|
||||
});
|
||||
const contactId = crypto.randomUUID();
|
||||
yield self.env.KV_STORAGE.put(
|
||||
`contact:${contactId}`,
|
||||
JSON.stringify(getSnapshot(contactRecord)),
|
||||
);
|
||||
|
||||
yield self.env.EMAIL_SERVICE.sendMail({
|
||||
to: 'geoff@seemueller.io',
|
||||
plaintextMessage: `WEBSITE CONTACT FORM SUBMISSION
|
||||
${firstname} ${lastname}
|
||||
${email}
|
||||
${message}`,
|
||||
});
|
||||
|
||||
return new Response('Contact record saved successfully', {
|
||||
status: 200,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error processing contact request:', error);
|
||||
return new Response('Failed to process contact request', {
|
||||
status: 500,
|
||||
});
|
||||
}
|
||||
}),
|
||||
}));
|
3
packages/services/src/contact-service/index.ts
Normal file
3
packages/services/src/contact-service/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import ContactService from './ContactService.ts';
|
||||
|
||||
export { ContactService };
|
54
packages/services/src/feedback-service/FeedbackService.ts
Normal file
54
packages/services/src/feedback-service/FeedbackService.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import * as schema from '@open-gsio/schema';
|
||||
import { Schema } from '@open-gsio/schema';
|
||||
import { types, flow, getSnapshot } from 'mobx-state-tree';
|
||||
|
||||
export default types
|
||||
.model('FeedbackStore', {})
|
||||
.volatile(self => ({
|
||||
env: {} as Env,
|
||||
ctx: {} as ExecutionContext,
|
||||
}))
|
||||
.actions(self => ({
|
||||
setEnv(env: Env) {
|
||||
self.env = env;
|
||||
},
|
||||
setCtx(ctx: ExecutionContext) {
|
||||
self.ctx = ctx;
|
||||
},
|
||||
handleFeedback: flow(function* (request: Request) {
|
||||
try {
|
||||
const {
|
||||
feedback,
|
||||
timestamp = new Date().toISOString(),
|
||||
user = 'Anonymous',
|
||||
} = yield request.json();
|
||||
|
||||
const feedbackRecord = Schema.FeedbackRecord.create({
|
||||
feedback,
|
||||
timestamp,
|
||||
user,
|
||||
});
|
||||
|
||||
const feedbackId = crypto.randomUUID();
|
||||
yield self.env.KV_STORAGE.put(
|
||||
`feedback:${feedbackId}`,
|
||||
JSON.stringify(getSnapshot(feedbackRecord)),
|
||||
);
|
||||
|
||||
yield self.env.EMAIL_SERVICE.sendMail({
|
||||
to: 'geoff@seemueller.io',
|
||||
plaintextMessage: `NEW FEEDBACK SUBMISSION
|
||||
User: ${user}
|
||||
Feedback: ${feedback}
|
||||
Timestamp: ${timestamp}`,
|
||||
});
|
||||
|
||||
return new Response('Feedback saved successfully', { status: 200 });
|
||||
} catch (error) {
|
||||
console.error('Error processing feedback request:', error);
|
||||
return new Response('Failed to process feedback request', {
|
||||
status: 500,
|
||||
});
|
||||
}
|
||||
}),
|
||||
}));
|
3
packages/services/src/feedback-service/index.ts
Normal file
3
packages/services/src/feedback-service/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import FeedbackService from './FeedbackService';
|
||||
|
||||
export { FeedbackService };
|
15
packages/services/src/index.ts
Normal file
15
packages/services/src/index.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { AssetService } from './asset-service';
|
||||
import { ChatService } from './chat-service';
|
||||
import { ContactService } from './contact-service';
|
||||
import { FeedbackService } from './feedback-service';
|
||||
import { MetricsService } from './metrics-service';
|
||||
import { TransactionService } from './transaction-service';
|
||||
|
||||
export {
|
||||
AssetService,
|
||||
ChatService,
|
||||
ContactService,
|
||||
FeedbackService,
|
||||
MetricsService,
|
||||
TransactionService,
|
||||
};
|
54
packages/services/src/metrics-service/MetricsService.ts
Normal file
54
packages/services/src/metrics-service/MetricsService.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { types, flow } from 'mobx-state-tree';
|
||||
|
||||
const MetricsService = types
|
||||
.model('MetricsService', {
|
||||
isCollectingMetrics: types.optional(types.boolean, true),
|
||||
})
|
||||
.volatile(self => ({
|
||||
env: {} as Env,
|
||||
ctx: {} as ExecutionContext,
|
||||
}))
|
||||
.actions(self => ({
|
||||
setEnv(env: Env) {
|
||||
self.env = env;
|
||||
},
|
||||
setCtx(ctx: ExecutionContext) {
|
||||
self.ctx = ctx;
|
||||
},
|
||||
handleMetricsRequest: flow(function* (request: Request) {
|
||||
const url = new URL(request.url);
|
||||
let proxyUrl = '';
|
||||
if (self.env.METRICS_HOST) {
|
||||
proxyUrl = new URL(`${self.env.METRICS_HOST}${url.pathname}${url.search}`).toString();
|
||||
}
|
||||
|
||||
if (proxyUrl) {
|
||||
try {
|
||||
const response = yield fetch(proxyUrl, {
|
||||
method: request.method,
|
||||
headers: request.headers,
|
||||
body: ['GET', 'HEAD'].includes(request.method) ? null : request.body,
|
||||
redirect: 'follow',
|
||||
});
|
||||
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error('Failed to proxy metrics request:', error);
|
||||
return new Response('metrics misconfigured', { status: 200 });
|
||||
}
|
||||
} else {
|
||||
const event = {
|
||||
method: request.method,
|
||||
headers: request.headers,
|
||||
body: ['GET', 'HEAD'].includes(request.method) ? null : request.body,
|
||||
};
|
||||
if (self.env?.KV_STORAGE?.put) {
|
||||
self.env.KV_STORAGE.put(`metrics_events::${crypto.randomUUID()}`, JSON.stringify(event));
|
||||
} else {
|
||||
console.log('Detected metrics misconfiguration...not storing');
|
||||
}
|
||||
}
|
||||
}),
|
||||
}));
|
||||
|
||||
export default MetricsService;
|
3
packages/services/src/metrics-service/index.ts
Normal file
3
packages/services/src/metrics-service/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import MetricsService from './MetricsService';
|
||||
|
||||
export { MetricsService };
|
@@ -0,0 +1,93 @@
|
||||
import { types } from 'mobx-state-tree';
|
||||
|
||||
const TransactionService = types
|
||||
.model('TransactionService', {})
|
||||
.volatile(self => ({
|
||||
env: {} as Env,
|
||||
ctx: {} as ExecutionContext,
|
||||
}))
|
||||
.actions(self => ({
|
||||
setEnv(env: Env) {
|
||||
self.env = env;
|
||||
},
|
||||
setCtx(ctx: ExecutionContext) {
|
||||
self.ctx = ctx;
|
||||
},
|
||||
|
||||
routeAction: async function (action: string, requestBody: any) {
|
||||
const actionHandlers: Record<string, (data: any) => Promise<any>> = {
|
||||
PREPARE_TX: self.handlePrepareTransaction,
|
||||
};
|
||||
|
||||
const handler = actionHandlers[action];
|
||||
if (!handler) {
|
||||
throw new Error(`No handler for action: ${action}`);
|
||||
}
|
||||
|
||||
return await handler(requestBody);
|
||||
},
|
||||
|
||||
handlePrepareTransaction: async function (data: [string, string, string]) {
|
||||
const [donerId, currency, amount] = data;
|
||||
const CreateWalletEndpoints = {
|
||||
bitcoin: '/api/btc/create',
|
||||
ethereum: '/api/eth/create',
|
||||
dogecoin: '/api/doge/create',
|
||||
};
|
||||
|
||||
const walletRequest = await fetch(
|
||||
`https://wallets.seemueller.io${CreateWalletEndpoints[currency]}`,
|
||||
);
|
||||
const walletResponse = await walletRequest.text();
|
||||
// console.log({ walletRequest: walletResponse });
|
||||
const [address, privateKey, publicKey, phrase] = JSON.parse(walletResponse);
|
||||
|
||||
const txKey = crypto.randomUUID();
|
||||
|
||||
const txRecord = {
|
||||
txKey,
|
||||
donerId,
|
||||
currency,
|
||||
amount,
|
||||
depositAddress: address,
|
||||
privateKey,
|
||||
publicKey,
|
||||
phrase,
|
||||
};
|
||||
|
||||
// console.log({ txRecord });
|
||||
|
||||
const key = `transactions::prepared::${txKey}`;
|
||||
|
||||
await self.env.KV_STORAGE.put(key, JSON.stringify(txRecord));
|
||||
// console.log(`PREPARED TRANSACTION ${key}`);
|
||||
|
||||
return {
|
||||
depositAddress: address,
|
||||
txKey: txKey,
|
||||
};
|
||||
},
|
||||
|
||||
handleTransact: async function (request: Request) {
|
||||
try {
|
||||
const raw = await request.text();
|
||||
// console.log({ raw });
|
||||
const [action, ...payload] = raw.split(',');
|
||||
|
||||
const response = await self.routeAction(action, payload);
|
||||
|
||||
return new Response(JSON.stringify(response), {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error handling transaction:', error);
|
||||
return new Response(JSON.stringify({ error: 'Transaction failed' }), {
|
||||
status: 500,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
export default TransactionService;
|
3
packages/services/src/transaction-service/index.ts
Normal file
3
packages/services/src/transaction-service/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import TransactionService from './TransactionService';
|
||||
|
||||
export { TransactionService };
|
14
packages/services/tsconfig.json
Normal file
14
packages/services/tsconfig.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"lib": ["ESNext"],
|
||||
"types": ["vite/client", "@types/bun"],
|
||||
"esModuleInterop": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"outDir": "dist",
|
||||
"allowJs": true,
|
||||
"jsx": "react-jsx"
|
||||
},
|
||||
"include": ["**/*.ts", "**/*.tsx"],
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
5
packages/services/types.d.ts
vendored
Normal file
5
packages/services/types.d.ts
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
declare global {
|
||||
type ExecutionContext = any;
|
||||
type Env = import('@open-gsio/env');
|
||||
}
|
||||
export type ExecutionContext = any;
|
23
packages/services/vite.config.ts
Normal file
23
packages/services/vite.config.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { defineConfig } from 'vite';
|
||||
// eslint-disable-next-line import/no-unresolved
|
||||
import { configDefaults } from 'vitest/config';
|
||||
|
||||
export default defineConfig(({ command }) => {
|
||||
return {
|
||||
test: {
|
||||
globals: true,
|
||||
environment: 'jsdom',
|
||||
registerNodeLoader: false,
|
||||
// setupFiles: ['./src/test/setup.ts'],
|
||||
exclude: [...configDefaults.exclude, 'dist/**', '.open-gsio/**'],
|
||||
reporters: process.env.GITHUB_ACTIONS ? ['dot', 'github-actions', 'html'] : ['dot', 'html'],
|
||||
coverage: {
|
||||
// you can include other reporters, but 'json-summary' is required, json is recommended
|
||||
reporter: ['json-summary', 'json', 'html'],
|
||||
reportsDirectory: 'coverage',
|
||||
// If you want a coverage reports even if your tests are failing, include the reportOnFailure option
|
||||
reportOnFailure: true,
|
||||
},
|
||||
},
|
||||
};
|
||||
});
|
Reference in New Issue
Block a user