mirror of
https://github.com/geoffsee/open-gsio.git
synced 2025-09-08 22:56:46 +00:00

Moved all SDK files from the `sdk` directory to the `lib` directory to better align with project structure. Updated all associated import paths across the codebase to reflect this change. Removed unused or commented-out code in `SiteCoordinator.js` for better clarity and maintainability.
59 lines
1.5 KiB
JavaScript
59 lines
1.5 KiB
JavaScript
import { DurableObject } from "cloudflare:workers";
|
|
|
|
export default class SiteCoordinator extends DurableObject {
|
|
constructor(state, env) {
|
|
super(state, env);
|
|
this.state = state;
|
|
this.env = env;
|
|
}
|
|
|
|
async dynamicMaxTokens(input, maxOuputTokens) {
|
|
return 2000;
|
|
}
|
|
|
|
async getConversationHistory(conversationId) {
|
|
const history = await this.env.KV_STORAGE.get(
|
|
`conversations:${conversationId}`,
|
|
);
|
|
|
|
return JSON.parse(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 }),
|
|
);
|
|
}
|
|
|
|
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.deleteStreamData(`streams:${streamId}`);
|
|
return null;
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
async deleteStreamData(streamId) {
|
|
await this.env.KV_STORAGE.delete(`streams:${streamId}`);
|
|
}
|
|
}
|