checkpoint

This commit is contained in:
sawka 2023-07-26 10:10:27 -07:00
parent 7c09d8c09e
commit 5d72581683
4 changed files with 35 additions and 30 deletions

View File

@ -7,7 +7,7 @@ CREATE TABLE client (
userpublickeybytes blob NOT NULL,
userprivatekeybytes blob NOT NULL,
winsize json NOT NULL
, clientopts json NOT NULL DEFAULT '', feopts json NOT NULL DEFAULT '{}', cmdstoretype varchar(20) DEFAULT 'session');
, clientopts json NOT NULL DEFAULT '', feopts json NOT NULL DEFAULT '{}', cmdstoretype varchar(20) DEFAULT 'session', openaiopts json NOT NULL DEFAULT '{}');
CREATE TABLE session (
sessionid varchar(36) PRIMARY KEY,
name varchar(50) NOT NULL,
@ -43,11 +43,9 @@ CREATE TABLE state_diff (
);
CREATE TABLE remote (
remoteid varchar(36) PRIMARY KEY,
physicalid varchar(36) NOT NULL,
remotetype varchar(10) NOT NULL,
remotealias varchar(50) NOT NULL,
remotecanonicalname varchar(200) NOT NULL,
remotesudo boolean NOT NULL,
remoteuser varchar(50) NOT NULL,
remotehost varchar(200) NOT NULL,
connectmode varchar(20) NOT NULL,
@ -58,7 +56,7 @@ CREATE TABLE remote (
local boolean NOT NULL,
archived boolean NOT NULL,
remoteidx int NOT NULL
, statevars json NOT NULL DEFAULT '{}');
, statevars json NOT NULL DEFAULT '{}', openaiopts json NOT NULL DEFAULT '{}');
CREATE TABLE history (
historyid varchar(36) PRIMARY KEY,
ts bigint NOT NULL,

View File

@ -657,7 +657,7 @@ func ScreenOpenCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (s
return nil, err
}
}
update, err := sstore.InsertScreen(ctx, ids.SessionId, newName, activate)
update, err := sstore.InsertScreen(ctx, ids.SessionId, newName, sstore.ScreenCreateOpts{}, activate)
if err != nil {
return nil, err
}
@ -1937,7 +1937,7 @@ func SessionOpenCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (
return nil, err
}
}
update, err := sstore.InsertSessionWithName(ctx, newName, sstore.ShareModeLocal, activate)
update, err := sstore.InsertSessionWithName(ctx, newName, activate)
if err != nil {
return nil, err
}

View File

@ -525,27 +525,9 @@ func GetSessionByName(ctx context.Context, name string) (*SessionType, error) {
return session, nil
}
func InsertCloudSession(ctx context.Context, sessionName string, shareMode string, activate bool) (*ModelUpdate, error) {
var updateRtn *ModelUpdate
txErr := WithTx(ctx, func(tx *TxWrap) error {
var err error
updateRtn, err = InsertSessionWithName(tx.Context(), sessionName, shareMode, activate)
if err != nil {
return err
}
sessionId := updateRtn.Sessions[0].SessionId
fmt.Printf("sessionid: %v\n", sessionId)
return nil
})
if txErr != nil {
return nil, txErr
}
return updateRtn, nil
}
// returns sessionId
// if sessionName == "", it will be generated
func InsertSessionWithName(ctx context.Context, sessionName string, shareMode string, activate bool) (*ModelUpdate, error) {
func InsertSessionWithName(ctx context.Context, sessionName string, activate bool) (*ModelUpdate, error) {
var newScreen *ScreenType
newSessionId := scbase.GenPromptUUID()
txErr := WithTx(ctx, func(tx *TxWrap) error {
@ -553,9 +535,9 @@ func InsertSessionWithName(ctx context.Context, sessionName string, shareMode st
sessionName = fmtUniqueName(sessionName, "session-%d", len(names)+1, names)
maxSessionIdx := tx.GetInt(`SELECT COALESCE(max(sessionidx), 0) FROM session`)
query := `INSERT INTO session (sessionid, name, activescreenid, sessionidx, notifynum, archived, archivedts, sharemode)
VALUES (?, ?, '', ?, ?, 0, 0, 'local')`
tx.Exec(query, newSessionId, sessionName, maxSessionIdx+1, 0)
screenUpdate, err := InsertScreen(tx.Context(), newSessionId, "", true)
VALUES (?, ?, '', ?, 0, 0, 0, ?)`
tx.Exec(query, newSessionId, sessionName, maxSessionIdx+1, ShareModeLocal)
screenUpdate, err := InsertScreen(tx.Context(), newSessionId, "", ScreenCreateOpts{}, true)
if err != nil {
return err
}
@ -666,7 +648,7 @@ func fmtUniqueName(name string, defaultFmtStr string, startIdx int, strs []strin
}
}
func InsertScreen(ctx context.Context, sessionId string, origScreenName string, activate bool) (*ModelUpdate, error) {
func InsertScreen(ctx context.Context, sessionId string, origScreenName string, opts ScreenCreateOpts, activate bool) (*ModelUpdate, error) {
var newScreenId string
txErr := WithTx(ctx, func(tx *TxWrap) error {
query := `SELECT sessionid FROM session WHERE sessionid = ? AND NOT archived`
@ -685,6 +667,20 @@ func InsertScreen(ctx context.Context, sessionId string, origScreenName string,
} else {
screenName = origScreenName
}
var baseScreen *ScreenType
if opts.HasCopy() {
if opts.BaseScreenId == "" {
return fmt.Errorf("invalid screen create opts, copy option with no base screen specified")
}
var err error
baseScreen, err = GetScreenById(tx.Context(), opts.BaseScreenId)
if err != nil {
return err
}
if baseScreen == nil {
return fmt.Errorf("cannot create screen, base screen not found")
}
}
newScreenId = scbase.GenPromptUUID()
screen := &ScreenType{
SessionId: sessionId,

View File

@ -429,6 +429,17 @@ type ScreenWebShareOpts struct {
ViewKey string `json:"viewkey"`
}
type ScreenCreateOpts struct {
BaseScreenId string
CopyRemote bool
CopyCwd bool
CopyEnv bool
}
func (sco ScreenCreateOpts) HasCopy() bool {
return sco.CopyRemote || sco.CopyCwd || sco.CopyEnv
}
type ScreenType struct {
SessionId string `json:"sessionid"`
ScreenId string `json:"screenid"`
@ -1179,7 +1190,7 @@ func EnsureDefaultSession(ctx context.Context) (*SessionType, error) {
if session != nil {
return session, nil
}
_, err = InsertSessionWithName(ctx, DefaultSessionName, ShareModeLocal, true)
_, err = InsertSessionWithName(ctx, DefaultSessionName, true)
if err != nil {
return nil, err
}