~cytrogen/gstack

ref: 78bc1d19687445fd09dd78c59d07781d2893a067 gstack/design/src/session.ts -rw-r--r-- 1.8 KiB
78bc1d19 — Garry Tan feat: design binary — real UI mockup generation for gstack skills (v0.13.0.0) (#551) 12 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
 * Session state management for multi-turn design iteration.
 * Session files are JSON in /tmp, keyed by PID + timestamp.
 */

import fs from "fs";
import path from "path";

export interface DesignSession {
  id: string;
  lastResponseId: string;
  originalBrief: string;
  feedbackHistory: string[];
  outputPaths: string[];
  createdAt: string;
  updatedAt: string;
}

/**
 * Generate a unique session ID from PID + timestamp.
 */
export function createSessionId(): string {
  return `${process.pid}-${Date.now()}`;
}

/**
 * Get the file path for a session.
 */
export function sessionPath(sessionId: string): string {
  return path.join("/tmp", `design-session-${sessionId}.json`);
}

/**
 * Create a new session after initial generation.
 */
export function createSession(
  responseId: string,
  brief: string,
  outputPath: string,
): DesignSession {
  const id = createSessionId();
  const session: DesignSession = {
    id,
    lastResponseId: responseId,
    originalBrief: brief,
    feedbackHistory: [],
    outputPaths: [outputPath],
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString(),
  };

  fs.writeFileSync(sessionPath(id), JSON.stringify(session, null, 2));
  return session;
}

/**
 * Read an existing session from disk.
 */
export function readSession(sessionFilePath: string): DesignSession {
  const content = fs.readFileSync(sessionFilePath, "utf-8");
  return JSON.parse(content);
}

/**
 * Update a session with new iteration data.
 */
export function updateSession(
  session: DesignSession,
  responseId: string,
  feedback: string,
  outputPath: string,
): void {
  session.lastResponseId = responseId;
  session.feedbackHistory.push(feedback);
  session.outputPaths.push(outputPath);
  session.updatedAt = new Date().toISOString();

  fs.writeFileSync(sessionPath(session.id), JSON.stringify(session, null, 2));
}