mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Rip out the streaming chat code
This commit is contained in:
19 files changed
+212
-2551
No files matched your search
@@ -1,280 +0,0 @@
|
||||
import { join } from "@std/path";
|
||||
import { JSRuntimeFS } from "./JSRuntime";
|
||||
|
||||
export interface AIMessage {
|
||||
role: string;
|
||||
content: string | { type: string }[];
|
||||
}
|
||||
|
||||
/** NodeFS interface for file system operations */
|
||||
export interface NodeFS {
|
||||
access(path: string): Promise<void>;
|
||||
readFile(path: string, encoding: "utf8"): Promise<string>;
|
||||
writeFile(path: string, data: string, encoding: "utf8"): Promise<void>;
|
||||
appendFile(path: string, data: string, encoding: "utf8"): Promise<void>;
|
||||
mkdir(path: string, options: { recursive: boolean }): Promise<unknown>;
|
||||
}
|
||||
|
||||
/** AI message history manager and .ai directory utilities */
|
||||
export class DotAI {
|
||||
private readonly fs: JSRuntimeFS;
|
||||
private readonly historyDir: string;
|
||||
private readonly workingDir: string;
|
||||
|
||||
constructor(fs: JSRuntimeFS, workingDir: string = process.cwd()) {
|
||||
this.fs = fs;
|
||||
this.workingDir = workingDir;
|
||||
this.historyDir = join(workingDir, ".ai", "history");
|
||||
}
|
||||
|
||||
/** Generate a unique session name based on the current date and time */
|
||||
static generateSessionName(): string {
|
||||
// Generate filename with date, time in milliseconds, and random value
|
||||
const now = new Date();
|
||||
const dateString = now.toISOString();
|
||||
|
||||
// Generate a random 3-character suffix to avoid collisions
|
||||
const randomSuffix = Math.random().toString(36).substring(2, 5);
|
||||
|
||||
// Format: YYYY-MM-DDTHH-MM-SSZ-suffix.jsonl
|
||||
const filename = `${
|
||||
dateString.replace(/:/g, "-").slice(0, dateString.indexOf("."))
|
||||
}Z-${randomSuffix}`;
|
||||
|
||||
return filename;
|
||||
}
|
||||
|
||||
/** Check if AI history should be enabled based on the new criteria */
|
||||
async isEnabled(): Promise<boolean> {
|
||||
// Check if .ai directory exists
|
||||
try {
|
||||
const stat = await this.fs.stat(join(this.workingDir, ".ai"));
|
||||
return stat.isDirectory();
|
||||
} catch {
|
||||
// Continue to next check
|
||||
}
|
||||
|
||||
// Check if .ai/ or .ai is gitignored
|
||||
try {
|
||||
const gitignorePath = join(this.workingDir, ".gitignore");
|
||||
const gitignoreContent = await this.fs.readFile(gitignorePath, "utf8");
|
||||
const lines = gitignoreContent.split("\n").map((line) => line.trim());
|
||||
|
||||
// Check for .ai/ or .ai patterns (ignore comments and empty lines)
|
||||
for (const line of lines) {
|
||||
if (line.startsWith("#") || line === "") {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check for various .ai patterns
|
||||
if (
|
||||
line === ".ai" || line === ".ai/" || line === "/.ai" ||
|
||||
line === "/.ai/" ||
|
||||
line === ".ai/*" || line === "/.ai/*"
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// .gitignore doesn't exist or can't be read
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Check if the history directory exists */
|
||||
async historyDirExists(): Promise<boolean> {
|
||||
try {
|
||||
const stat = await this.fs.stat(this.historyDir);
|
||||
return stat.isDirectory();
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Save a message to the history file */
|
||||
async addToHistory(sessionName: string, message: AIMessage): Promise<void> {
|
||||
// Only save if history should be enabled based on the new criteria
|
||||
if (!(await this.isEnabled())) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Create the history directory if it doesn't exist
|
||||
if (!(await this.historyDirExists())) {
|
||||
await this.setupAiHistoryDir();
|
||||
}
|
||||
|
||||
const jsonLine = JSON.stringify(message);
|
||||
const sessionFile = join(this.historyDir, sessionName + ".jsonl");
|
||||
|
||||
try {
|
||||
// Try to read existing content
|
||||
const existingContent = await this.fs.readFile(sessionFile, "utf8");
|
||||
await this.fs.writeFile(sessionFile, existingContent + "\n" + jsonLine, "utf8");
|
||||
} catch {
|
||||
// File doesn't exist, create it with the data
|
||||
await this.fs.writeFile(sessionFile, jsonLine, "utf8");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates .ai/history directory and ensures .ai/ is gitignored at the project root.
|
||||
*/
|
||||
async setupAiHistoryDir(): Promise<void> {
|
||||
const aiHistoryDir = join(this.workingDir, ".ai", "history");
|
||||
|
||||
try {
|
||||
// Create .ai/history directory
|
||||
await this.fs.mkdir(aiHistoryDir, { recursive: true });
|
||||
|
||||
// Ensure .ai/ is gitignored at the project root
|
||||
await this.ensureAiGitignored();
|
||||
} catch (error) {
|
||||
// Log error but don't fail the main operation
|
||||
console.warn(`Failed to setup .ai/history directory: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the model from .ai/MODEL file
|
||||
*/
|
||||
async readAiModel(): Promise<string | undefined> {
|
||||
try {
|
||||
const modelPath = join(this.workingDir, ".ai", "MODEL");
|
||||
const content = await this.fs.readFile(modelPath, "utf8");
|
||||
return content.trim();
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the model to .ai/MODEL file
|
||||
*/
|
||||
async writeAiModel(model: string): Promise<void> {
|
||||
const aiDir = join(this.workingDir, ".ai");
|
||||
const modelPath = join(aiDir, "MODEL");
|
||||
|
||||
try {
|
||||
// Ensure .ai directory exists
|
||||
await this.fs.mkdir(aiDir, { recursive: true });
|
||||
|
||||
// Write the model
|
||||
await this.fs.writeFile(modelPath, model.trim() + "\n", "utf8");
|
||||
} catch (error) {
|
||||
console.warn(`Failed to write .ai/MODEL file: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read parameters from .ai/PARAMETERS file
|
||||
*/
|
||||
async readAiParameters(): Promise<Record<string, string>> {
|
||||
try {
|
||||
const parametersPath = join(this.workingDir, ".ai", "PARAMETERS");
|
||||
const content = await this.fs.readFile(parametersPath, "utf8");
|
||||
|
||||
const parameters: Record<string, string> = {};
|
||||
const lines = content.split("\n");
|
||||
|
||||
for (const line of lines) {
|
||||
const trimmedLine = line.trim();
|
||||
if (trimmedLine && !trimmedLine.startsWith("#")) {
|
||||
const equalIndex = trimmedLine.indexOf("=");
|
||||
if (equalIndex > 0) {
|
||||
const key = trimmedLine.substring(0, equalIndex).trim();
|
||||
const value = trimmedLine.substring(equalIndex + 1).trim();
|
||||
parameters[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return parameters;
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write parameters to .ai/PARAMETERS file
|
||||
*/
|
||||
async writeAiParameters(parameters: Record<string, string>): Promise<void> {
|
||||
// Check if we should create the .ai directory based on the same logic as MODEL
|
||||
const shouldCreate = await this.isEnabled();
|
||||
|
||||
if (!shouldCreate) {
|
||||
return;
|
||||
}
|
||||
|
||||
const aiDir = join(this.workingDir, ".ai");
|
||||
const parametersPath = join(aiDir, "PARAMETERS");
|
||||
|
||||
try {
|
||||
// Ensure .ai directory exists
|
||||
await this.fs.mkdir(aiDir, { recursive: true });
|
||||
|
||||
// Format parameters as key=value pairs
|
||||
const lines = Object.entries(parameters).map(([key, value]) =>
|
||||
`${key}=${value}`
|
||||
);
|
||||
const content = lines.join("\n") + (lines.length > 0 ? "\n" : "");
|
||||
|
||||
// Write the parameters
|
||||
await this.fs.writeFile(parametersPath, content, "utf8");
|
||||
} catch (error) {
|
||||
console.warn(`Failed to write .ai/PARAMETERS file: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a single parameter in .ai/PARAMETERS file
|
||||
*/
|
||||
async updateAiParameter(key: string, value: string): Promise<void> {
|
||||
const currentParameters = await this.readAiParameters();
|
||||
currentParameters[key] = value;
|
||||
await this.writeAiParameters(currentParameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that .ai/ is gitignored at the project root level.
|
||||
* Creates a .gitignore file if it doesn't exist, or adds the line if not already present.
|
||||
*/
|
||||
async ensureAiGitignored(): Promise<void> {
|
||||
const gitignorePath = join(this.workingDir, ".gitignore");
|
||||
const aiPattern = ".ai/";
|
||||
|
||||
try {
|
||||
// Try to read existing .gitignore
|
||||
let gitignoreContent = "";
|
||||
try {
|
||||
gitignoreContent = await this.fs.readFile(gitignorePath, "utf8");
|
||||
} catch {
|
||||
// .gitignore doesn't exist, will create it
|
||||
}
|
||||
|
||||
// Check if .ai/ is already gitignored
|
||||
const lines = gitignoreContent.split("\n").map((line) => line.trim());
|
||||
const hasAiPattern = lines.some((line) => {
|
||||
if (line.startsWith("#") || line === "") {
|
||||
return false;
|
||||
}
|
||||
// Check for various .ai patterns
|
||||
return line === ".ai" || line === ".ai/" || line === "/.ai" ||
|
||||
line === "/.ai/" ||
|
||||
line === ".ai/*" || line === "/.ai/*";
|
||||
});
|
||||
|
||||
if (!hasAiPattern) {
|
||||
// Add .ai/ to .gitignore
|
||||
const newContent = gitignoreContent.trim() === ""
|
||||
? aiPattern
|
||||
: gitignoreContent.trim() + "\n" + aiPattern;
|
||||
|
||||
await this.fs.writeFile(gitignorePath, newContent + "\n", "utf8");
|
||||
}
|
||||
} catch (error) {
|
||||
// Log error but don't fail the main operation
|
||||
console.warn(`Failed to ensure .ai/ is gitignored: ${error}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import git from 'isomorphic-git';
|
||||
import http from 'isomorphic-git/http/web';
|
||||
import { Buffer } from 'buffer';
|
||||
import type { JSRuntimeFS } from '@/lib/JSRuntime';
|
||||
import { DotAI } from './DotAI';
|
||||
|
||||
|
||||
// Polyfill Buffer for browser
|
||||
if (typeof window !== 'undefined') {
|
||||
@@ -38,8 +38,6 @@ export class ProjectsManager {
|
||||
|
||||
async createProject(name: string): Promise<Project> {
|
||||
const project = await this.cloneProject(name, GIT_TEMPLATE_URL);
|
||||
const dotAI = new DotAI(this.fs, project.path);
|
||||
await dotAI.setupAiHistoryDir();
|
||||
return project;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
import { ModelMessage, generateId } from 'ai';
|
||||
import { ChatMessage } from '@/hooks/useStreamingChat';
|
||||
|
||||
/**
|
||||
* Convert ModelMessage array to ChatMessage array
|
||||
* This is useful when loading chat history from storage
|
||||
*/
|
||||
export function convertModelMessagesToChatMessages(modelMessages: ModelMessage[]): ChatMessage[] {
|
||||
return modelMessages.map(msg => ({
|
||||
...msg,
|
||||
id: generateId(),
|
||||
timestamp: Date.now()
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert ChatMessage array to ModelMessage array
|
||||
* This is useful when saving chat history to storage or sending to AI
|
||||
*/
|
||||
export function convertChatMessagesToModelMessages(chatMessages: ChatMessage[]): ModelMessage[] {
|
||||
return chatMessages.map(({ id, isStreaming, timestamp, ...modelMessage }) => modelMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save chat history as ModelMessage array (for persistence)
|
||||
*/
|
||||
export function prepareChatHistoryForStorage(chatMessages: ChatMessage[]): ModelMessage[] {
|
||||
return convertChatMessagesToModelMessages(chatMessages);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load chat history from ModelMessage array (from persistence)
|
||||
*/
|
||||
export function loadChatHistoryFromStorage(modelMessages: ModelMessage[]): ChatMessage[] {
|
||||
return convertModelMessagesToChatMessages(modelMessages);
|
||||
}
|
||||
Reference in new issue
Block a user