Skip to content

Commit 57d2722

Browse files
fix: template
1 parent b362173 commit 57d2722

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { DurableObject } from 'cloudflare:workers';
2+
import type { SessionInfo } from './types';
3+
import type { Env } from './core-utils';
4+
5+
// 🤖 AI Extension Point: Add session management features
6+
export class AppController extends DurableObject<Env> {
7+
private sessions = new Map<string, SessionInfo>();
8+
private loaded = false;
9+
10+
constructor(ctx: DurableObjectState, env: Env) {
11+
super(ctx, env);
12+
}
13+
14+
private async ensureLoaded(): Promise<void> {
15+
if (!this.loaded) {
16+
const stored = await this.ctx.storage.get<Record<string, SessionInfo>>('sessions') || {};
17+
this.sessions = new Map(Object.entries(stored));
18+
this.loaded = true;
19+
}
20+
}
21+
22+
private async persist(): Promise<void> {
23+
await this.ctx.storage.put('sessions', Object.fromEntries(this.sessions));
24+
}
25+
26+
async addSession(sessionId: string, title?: string): Promise<void> {
27+
await this.ensureLoaded();
28+
const now = Date.now();
29+
this.sessions.set(sessionId, {
30+
id: sessionId,
31+
title: title || `Chat ${new Date(now).toLocaleDateString()}`,
32+
createdAt: now,
33+
lastActive: now
34+
});
35+
await this.persist();
36+
}
37+
38+
async removeSession(sessionId: string): Promise<boolean> {
39+
await this.ensureLoaded();
40+
const deleted = this.sessions.delete(sessionId);
41+
if (deleted) await this.persist();
42+
return deleted;
43+
}
44+
45+
async updateSessionActivity(sessionId: string): Promise<void> {
46+
await this.ensureLoaded();
47+
const session = this.sessions.get(sessionId);
48+
if (session) {
49+
session.lastActive = Date.now();
50+
await this.persist();
51+
}
52+
}
53+
54+
async updateSessionTitle(sessionId: string, title: string): Promise<boolean> {
55+
await this.ensureLoaded();
56+
const session = this.sessions.get(sessionId);
57+
if (session) {
58+
session.title = title;
59+
await this.persist();
60+
return true;
61+
}
62+
return false;
63+
}
64+
65+
async listSessions(): Promise<SessionInfo[]> {
66+
await this.ensureLoaded();
67+
return Array.from(this.sessions.values()).sort((a, b) => b.lastActive - a.lastActive);
68+
}
69+
70+
async getSessionCount(): Promise<number> {
71+
await this.ensureLoaded();
72+
return this.sessions.size;
73+
}
74+
75+
async getSession(sessionId: string): Promise<SessionInfo | null> {
76+
await this.ensureLoaded();
77+
return this.sessions.get(sessionId) || null;
78+
}
79+
80+
async clearAllSessions(): Promise<number> {
81+
await this.ensureLoaded();
82+
const count = this.sessions.size;
83+
this.sessions.clear();
84+
await this.persist();
85+
return count;
86+
}
87+
}

definitions/vite-supadata-runner/worker/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { cors } from "hono/cors";
55
import { logger } from "hono/logger";
66
import { API_RESPONSES } from "./config";
77
import { userRoutes } from "./userRoutes";
8+
import { AppController } from "./app-controller";
9+
export { AppController };
810
export interface ClientErrorReport {
911
message: string;
1012
url: string;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export interface ApiResponse<T = unknown> { success: boolean; data?: T; error?: string; }
2+
3+
export interface MCPResult {
4+
content: string;
5+
}
6+
7+
export interface ErrorResult {
8+
error: string;
9+
}
10+
11+
export interface SessionInfo {
12+
id: string;
13+
title: string;
14+
createdAt: number;
15+
lastActive: number;
16+
}

0 commit comments

Comments
 (0)