Skip to content

Commit

Permalink
ran prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
robgruen committed Feb 4, 2025
1 parent 4f6b22d commit 60d6d60
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 41 deletions.
34 changes: 21 additions & 13 deletions ts/packages/shell/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ process.argv.forEach((arg) => {
});

export function runningTests(): boolean {
return process.env["INSTANCE_NAME"] !== undefined && process.env["INSTANCE_NAME"].startsWith("test_") === true;
return (
process.env["INSTANCE_NAME"] !== undefined &&
process.env["INSTANCE_NAME"].startsWith("test_") === true
);
}

let mainWindow: BrowserWindow | null = null;
Expand Down Expand Up @@ -619,11 +622,20 @@ async function initialize() {
ShellSettings.getinstance().onSettingsChanged!();

// Load chat history if enabled
const chatHistory: string = path.join(getInstanceDir(), "chat_history.html")
if (ShellSettings.getinstance().chatHistory && existsSync(chatHistory)) {
const chatHistory: string = path.join(
getInstanceDir(),
"chat_history.html",
);
if (
ShellSettings.getinstance().chatHistory &&
existsSync(chatHistory)
) {
chatView?.webContents.send(
"chat-history",
readFileSync(path.join(getInstanceDir(), "chat_history.html"), "utf-8"),
readFileSync(
path.join(getInstanceDir(), "chat_history.html"),
"utf-8",
),
);
}

Expand All @@ -640,27 +652,24 @@ async function initialize() {
// send the agent greeting if it's turned on
if (ShellSettings.getinstance().agentGreeting) {
dispatcher.processCommand("@greeting", "agent-0", []);
}
}
});

// Store the chat history whenever the DOM changes
// this let's us rehydrate the chat when reopening the shell
ipcMain.on("dom changed", async(_event, html) => {
ipcMain.on("dom changed", async (_event, html) => {
// store the modified DOM contents
const file: string = path.join(getInstanceDir(), "chat_history.html");

debugShell(
`Saving chat history to '${file}'.`,
performance.now(),
);
debugShell(`Saving chat history to '${file}'.`, performance.now());

try {
writeFileSync(file, html);
} catch (e) {
debugShell(
`Unable to save history to '${file}'. Error: ${e}`,
performance.now(),
);
);
}
});

Expand Down Expand Up @@ -710,8 +719,7 @@ async function initialize() {
// On windows, we will spin up a local end point that listens
// for pen events which will trigger speech reco
// Don't spin this up during testing
if (
process.platform == "win32" && !runningTests()) {
if (process.platform == "win32" && !runningTests()) {
const pipePath = path.join("\\\\.\\pipe\\TypeAgent", "speech");
const server = net.createServer((stream) => {
stream.on("data", (c) => {
Expand Down
2 changes: 1 addition & 1 deletion ts/packages/shell/src/main/shellSettingsType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ export const defaultSettings: ShellSettingsType = {
partialCompletion: true,
disallowedDisplayType: [],
darkMode: false,
chatHistory: true
chatHistory: true,
};
4 changes: 3 additions & 1 deletion ts/packages/shell/src/preload/electronTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ export interface ClientAPI {
settings: ShellSettings,
) => void,
): void;
onChatHistory(callback: (e: Electron.IpcRendererEvent, chatHistory: string) => void): void;
onChatHistory(
callback: (e: Electron.IpcRendererEvent, chatHistory: string) => void,
): void;
registerClientIO(clientIO: ClientIO);
}

Expand Down
2 changes: 1 addition & 1 deletion ts/packages/shell/src/renderer/assets/styles.less
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ body {
}

.chat-separator-text {
margin: 0px 10px;
margin: 0px 10px;
color: darkgray;
height: 20px;
}
Expand Down
58 changes: 36 additions & 22 deletions ts/packages/shell/src/renderer/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,38 +276,51 @@ function addEvents(

// add the separator
if (history.length > 0) {

// don't add a separator if there's already one there
if (!chatView.getScollContainer().children[0].classList.contains("chat-separator")) {
let separator: HTMLDivElement = document.createElement("div");
if (
!chatView
.getScollContainer()
.children[0].classList.contains("chat-separator")
) {
let separator: HTMLDivElement =
document.createElement("div");
separator.classList.add("chat-separator");
separator.innerHTML = "<div class=\"chat-separator-line\"></div><div class=\"chat-separator-text\">previously</div><div class=\"chat-separator-line\"></div>"

separator.innerHTML =
'<div class="chat-separator-line"></div><div class="chat-separator-text">previously</div><div class="chat-separator-line"></div>';

chatView.getScollContainer().prepend(separator);
}

// make all old messages "inactive" and set the context for each separator
let lastSeparatorText: HTMLDivElement | null;
for(let i = 0; i < chatView.getScollContainer().children.length; i++) {

for (
let i = 0;
i < chatView.getScollContainer().children.length;
i++
) {
// gray out this item
const div = chatView.getScollContainer().children[i];
div.classList.add("history");

// is this a separator?
const separator = div.querySelector(".chat-separator-text");
if (separator != null) {
lastSeparatorText = div.querySelector(".chat-separator-text");
lastSeparatorText = div.querySelector(
".chat-separator-text",
);
}

// get the timestamp for this chat bubble (if applicable)
const span: HTMLSpanElement | null = div.querySelector(".timestring");
const span: HTMLSpanElement | null =
div.querySelector(".timestring");

if (span !== null) {
const timeStamp: Date = new Date(span.attributes["data"].value);
lastSeparatorText!.innerText = getDateDifferenceDescription(new Date(), timeStamp);
const timeStamp: Date = new Date(
span.attributes["data"].value,
);
lastSeparatorText!.innerText =
getDateDifferenceDescription(new Date(), timeStamp);
}

}
}
}
Expand Down Expand Up @@ -476,25 +489,26 @@ document.addEventListener("DOMContentLoaded", async function () {
});

function watchForDOMChanges(element: HTMLDivElement) {

// ignore attribute changes but wach for
// ignore attribute changes but wach for
const config = { attributes: false, childList: true, subtree: true };

// timeout
let idleCounter: number = 0;

// observer callback
const observer = new MutationObserver(() => {

// increment the idle counter
idleCounter++;

// decrement the idle counter
setTimeout(() => {
if (--idleCounter == 0) {
// last one notifies main process
// last one notifies main process
if ((window as any).electron) {
(window as any).electron.ipcRenderer.send("dom changed", element.innerHTML);
(window as any).electron.ipcRenderer.send(
"dom changed",
element.innerHTML,
);
}
}
}, 3000);
Expand All @@ -503,7 +517,7 @@ function watchForDOMChanges(element: HTMLDivElement) {
// start observing
observer.observe(element!, config);

// observer.disconnect();
// observer.disconnect();
}

function getDateDifferenceDescription(date1: Date, date2: Date): string {
Expand All @@ -516,26 +530,26 @@ function getDateDifferenceDescription(date1: Date, date2: Date): string {
const diffYears = Math.floor(diff / (1000 * 60 * 60 * 24 * 365));

if (diffMinutes < 1) {
return "just now"
return "just now";
} else if (diffMinutes < 15) {
return "a few minutes ago";
} else if (diffMinutes < 60) {
return "under an hour ago"
return "under an hour ago";
} else if (diffHours < 2) {
return "an hour ago";
} else if (diffDays < 1) {
return "earlier today";
} else if (diffDays < 2) {
return "yesterday";
} else if (diffDays < 7) {
return date1.toLocaleDateString('en-US', { weekday: 'long' });
return date1.toLocaleDateString("en-US", { weekday: "long" });
} else if (diffWeeks < 2) {
return "last week";
} else if (diffMonths < 2) {
return "last month";
} else if (diffYears < 2) {
return "last year";
} else {
return date1.toLocaleDateString('en-US', { weekday: 'long' });
return date1.toLocaleDateString("en-US", { weekday: "long" });
}
}
10 changes: 7 additions & 3 deletions ts/packages/shell/src/renderer/src/settingsView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,13 @@ export class SettingsView {
this._shellSettings.darkMode ? "Light mode" : "Dark mode",
);

this.saveChatHistoryCheckBox = this.addCheckbox("Save Chat History", () => {
this._shellSettings.chatHistory = this.saveChatHistoryCheckBox.checked;
});
this.saveChatHistoryCheckBox = this.addCheckbox(
"Save Chat History",
() => {
this._shellSettings.chatHistory =
this.saveChatHistoryCheckBox.checked;
},
);
}

getContainer() {
Expand Down

0 comments on commit 60d6d60

Please sign in to comment.