From 5d72581683cc33c10b953c7cefa44542ea011014 Mon Sep 17 00:00:00 2001 From: sawka Date: Wed, 26 Jul 2023 10:10:27 -0700 Subject: [PATCH] checkpoint --- db/schema.sql | 6 ++---- pkg/cmdrunner/cmdrunner.go | 4 ++-- pkg/sstore/dbops.go | 42 +++++++++++++++++--------------------- pkg/sstore/sstore.go | 13 +++++++++++- 4 files changed, 35 insertions(+), 30 deletions(-) diff --git a/db/schema.sql b/db/schema.sql index 018e8442..dd445014 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -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, diff --git a/pkg/cmdrunner/cmdrunner.go b/pkg/cmdrunner/cmdrunner.go index ed4726d1..47dfe885 100644 --- a/pkg/cmdrunner/cmdrunner.go +++ b/pkg/cmdrunner/cmdrunner.go @@ -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 } diff --git a/pkg/sstore/dbops.go b/pkg/sstore/dbops.go index 5e28d437..4e03bb19 100644 --- a/pkg/sstore/dbops.go +++ b/pkg/sstore/dbops.go @@ -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, diff --git a/pkg/sstore/sstore.go b/pkg/sstore/sstore.go index cd07ddb3..1dc630c6 100644 --- a/pkg/sstore/sstore.go +++ b/pkg/sstore/sstore.go @@ -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 }