Skip to content

Commit

Permalink
New sessions copy the current session settings
Browse files Browse the repository at this point in the history
  • Loading branch information
sedwards2009 committed Jan 14, 2024
1 parent 4aea1b7 commit 506b23e
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 45 deletions.
20 changes: 17 additions & 3 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,23 @@ func handleNewSession(c *gin.Context) {
return
}

session.ModelSettings.ModelID = llmEngine.DefaultID()
session.ModelSettings.PresetID = presetDatabase.DefaultID()
session.ModelSettings.TemplateID = templates.DefaultID()
var data struct {
ModelID string `json:"modelId"`
TemplateID string `json:"templateId"`
PresetID string `json:"presetId"`
}

if err := c.ShouldBindJSON(&data); err != nil {
log.Printf("handleNewSession: Unable to parse POST")
session.ModelSettings.ModelID = llmEngine.DefaultID()
session.ModelSettings.PresetID = presetDatabase.DefaultID()
session.ModelSettings.TemplateID = templates.DefaultID()
} else {
session.ModelSettings.ModelID = data.ModelID
session.ModelSettings.PresetID = data.PresetID
session.ModelSettings.TemplateID = data.TemplateID
}

log.Printf("presetDatabase.DefaultID(): %s", presetDatabase.DefaultID())
sessionStorage.WriteSession(session)
c.JSON(http.StatusOK, session)
Expand Down
12 changes: 10 additions & 2 deletions frontend/src/dataloading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,16 @@ export async function loadSession(sessionId: string): Promise<Session | null> {
return null;
}

export async function newSession(): Promise<Session | null> {
const response = await fetch(`${SERVER_BASE_URL}/session`, {method: "POST"});
export async function newSession(defaults?: ModelSettings | null): Promise<Session | null> {
const fetchOptions: RequestInit = {
method: "POST",
};
if (defaults != null) {
fetchOptions.headers = {"Content-Type": "application/json"};
fetchOptions.body = JSON.stringify(defaults);
}

const response = await fetch(`${SERVER_BASE_URL}/session`, fetchOptions);
try {
if (response.ok) {
return await response.json();
Expand Down
29 changes: 26 additions & 3 deletions frontend/src/emptyhome.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { useEffect } from "react";
import { useEffect, useState } from "react";
import classNames from "classnames";

import { SessionOverview } from "./data";
import { navigate } from "raviger";
import { NewSessionButton } from "./newsessionbutton";
import { newSession } from "./dataloading";

export interface Props {
sessionOverview: SessionOverview;
Expand All @@ -16,7 +18,28 @@ export function EmptyHome({ sessionOverview, onSessionChange }: Props): JSX.Elem
navigate(`/session/${sessionOverview.sessionSummaries[0].id}`);
}, [sessionOverview]);

const [isCreatingSession, setIsCreatingSession] = useState<boolean>(false);

const onNewSessionClick = () => {
setIsCreatingSession(true);
(async () => {
const session = await newSession();
setIsCreatingSession(false);
if (session == null) {
console.log(`Unable to create a new session.`);
} else {
onSessionChange();
navigate(`/session/${session.id}`);
}
})();
};

return <div>
<NewSessionButton onSessionChange={onSessionChange} />
<button
className={classNames({"primary": !isCreatingSession})}
disabled={isCreatingSession}
onClick={onNewSessionClick}>
{isCreatingSession ? "Creating session..." : "New Session" }
</button>
</div>;
}
40 changes: 36 additions & 4 deletions frontend/src/home.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { ModelOverview, PresetOverview, SessionOverview, TemplateOverview } from "./data";
import { ModelOverview, ModelSettings, PresetOverview, SessionOverview, TemplateOverview } from "./data";
import { SessionEditor } from "./sessioneditor";
import { SessionOverviewList } from "./sessionoverviewlist";
import { NewSessionButton } from "./newsessionbutton";
import { navigate } from "raviger";
import { deleteSession } from "./dataloading";
import { deleteSession, loadSession, newSession } from "./dataloading";
import { useState } from "react";
import classNames from "classnames";


export interface Props {
modelOverview: ModelOverview;
Expand All @@ -17,6 +19,8 @@ export interface Props {
export function Home({ modelOverview, presetOverview, sessionOverview, templateOverview, sessionId, onSessionChange
}: Props): JSX.Element {

const [isCreatingSession, setIsCreatingSession] = useState<boolean>(false);

const onSessionDelete = () => {
(async () => {
await deleteSession(sessionId);
Expand All @@ -25,14 +29,42 @@ export function Home({ modelOverview, presetOverview, sessionOverview, templateO
})();
};

const onNewSessionClick = () => {
setIsCreatingSession(true);
(async () => {

let previousSettings: ModelSettings | null = null;
if (sessionId != null) {
const loadedSession = await loadSession(sessionId);
if (loadedSession != null) {
previousSettings = loadedSession.modelSettings;
}
}

const session = await newSession(previousSettings);
setIsCreatingSession(false);
if (session == null) {
console.log(`Unable to create a new session.`);
} else {
onSessionChange();
navigate(`/session/${session.id}`);
}
})();
};

return (
<div className="top-layout">
<div className="session-list">
<SessionOverviewList
sessionOverview={sessionOverview}
selectedSessionId={sessionId}
/>
<NewSessionButton onSessionChange={onSessionChange} />
<button
className={classNames({"primary": !isCreatingSession})}
disabled={isCreatingSession}
onClick={onNewSessionClick}>
{isCreatingSession ? "Creating session..." : "New Session" }
</button>
</div>
<div className="session-tab">
<SessionEditor
Expand Down
33 changes: 0 additions & 33 deletions frontend/src/newsessionbutton.tsx

This file was deleted.

0 comments on commit 506b23e

Please sign in to comment.