Files
open-gsio/packages/durable-objects/src/ServerCoordinator.ts
geoffsee c6e09644e2 **Refactor:** Restructure server package to streamline imports and improve file organization
- Moved `providers`, `services`, `models`, `lib`, and related files to `src` directory within `server` package.
- Adjusted imports across the codebase to reflect the new paths.
- Renamed several `.ts` files for consistency.
- Introduced an `index.ts` in the `ai/providers` package to export all providers.

This improves maintainability and aligns with the project's updated directory structure.
2025-06-24 20:46:15 -04:00

80 lines
2.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { ProviderRepository } from '@open-gsio/ai/providers/_ProviderRepository.ts';
// @ts-expect-error - don't care
// eslint-disable-next-line import/no-unresolved
import { DurableObject } from 'cloudflare:workers';
export default class ServerCoordinator extends DurableObject {
env;
state;
constructor(state, env) {
super(state, env);
this.state = state;
this.env = env;
}
// Public method to calculate dynamic max tokens
async dynamicMaxTokens(model, input, maxOuputTokens) {
const modelMeta = ProviderRepository.getModelMeta(model, this.env);
// The tokenlimit information is stored in three different keys:
// max_completion_tokens
// context_window
// context_length
if ('max_completion_tokens' in modelMeta) {
return modelMeta.max_completion_tokens;
} else if ('context_window' in modelMeta) {
return modelMeta.context_window;
} else if ('context_length' in modelMeta) {
return modelMeta.context_length;
} else {
return 2000;
}
}
// Public method to retrieve conversation history
async getConversationHistory(conversationId) {
const history = await this.env.KV_STORAGE.get(`conversations:${conversationId}`);
return JSON.parse(history) || [];
}
// Public method to save a message to the conversation history
async saveConversationHistory(conversationId, message) {
const history = await this.getConversationHistory(conversationId);
history.push(message);
await this.env.KV_STORAGE.put(`conversations:${conversationId}`, JSON.stringify(history));
}
async saveStreamData(streamId, data, ttl = 10) {
const expirationTimestamp = Date.now() + ttl * 1000;
// await this.state.storage.put(streamId, { data, expirationTimestamp });
await this.env.KV_STORAGE.put(
`streams:${streamId}`,
JSON.stringify({ data, expirationTimestamp }),
);
}
// New method to get stream data
async getStreamData(streamId) {
const streamEntry = await this.env.KV_STORAGE.get(`streams:${streamId}`);
if (!streamEntry) {
return null;
}
const { data, expirationTimestamp } = JSON.parse(streamEntry);
if (Date.now() > expirationTimestamp) {
// await this.state.storage.delete(streamId); // Clean up expired entry
await this.deleteStreamData(`streams:${streamId}`);
return null;
}
return data;
}
// New method to delete stream data (cleanup)
async deleteStreamData(streamId) {
await this.env.KV_STORAGE.delete(`streams:${streamId}`);
}
}