2024-05-14 22:34:41 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package blockservice
|
|
|
|
|
|
|
|
import (
|
2024-06-18 07:38:48 +02:00
|
|
|
"context"
|
2024-07-30 07:35:21 +02:00
|
|
|
"encoding/json"
|
2024-05-14 22:34:41 +02:00
|
|
|
"fmt"
|
2024-05-22 06:15:11 +02:00
|
|
|
"time"
|
2024-05-14 22:34:41 +02:00
|
|
|
|
2024-09-05 23:25:45 +02:00
|
|
|
"github.com/wavetermdev/waveterm/pkg/blockcontroller"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/filestore"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/tsgen/tsgenmeta"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/waveobj"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/wshrpc"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/wstore"
|
2024-05-14 22:34:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type BlockService struct{}
|
|
|
|
|
2024-05-24 23:08:24 +02:00
|
|
|
const DefaultTimeout = 2 * time.Second
|
|
|
|
|
2024-06-12 23:18:03 +02:00
|
|
|
var BlockServiceInstance = &BlockService{}
|
|
|
|
|
2024-06-12 22:47:13 +02:00
|
|
|
func (bs *BlockService) SendCommand_Meta() tsgenmeta.MethodMeta {
|
|
|
|
return tsgenmeta.MethodMeta{
|
2024-06-12 02:42:10 +02:00
|
|
|
Desc: "send command to block",
|
2024-06-12 22:47:13 +02:00
|
|
|
ArgNames: []string{"blockid", "cmd"},
|
2024-06-12 02:42:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-24 23:34:31 +02:00
|
|
|
func (bs *BlockService) GetControllerStatus(ctx context.Context, blockId string) (*blockcontroller.BlockControllerRuntimeStatus, error) {
|
|
|
|
bc := blockcontroller.GetBlockController(blockId)
|
|
|
|
if bc == nil {
|
2024-09-05 09:21:08 +02:00
|
|
|
return nil, nil
|
2024-06-24 23:34:31 +02:00
|
|
|
}
|
|
|
|
return bc.GetRuntimeStatus(), nil
|
|
|
|
}
|
|
|
|
|
2024-10-24 20:01:58 +02:00
|
|
|
func (*BlockService) SaveTerminalState_Meta() tsgenmeta.MethodMeta {
|
|
|
|
return tsgenmeta.MethodMeta{
|
|
|
|
Desc: "save the terminal state to a blockfile",
|
|
|
|
ArgNames: []string{"ctx", "blockId", "state", "stateType", "ptyOffset", "termSize"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *BlockService) SaveTerminalState(ctx context.Context, blockId string, state string, stateType string, ptyOffset int64, termSize waveobj.TermSize) error {
|
2024-08-20 23:56:48 +02:00
|
|
|
_, err := wstore.DBMustGet[*waveobj.Block](ctx, blockId)
|
2024-06-18 07:38:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if stateType != "full" && stateType != "preview" {
|
|
|
|
return fmt.Errorf("invalid state type: %q", stateType)
|
|
|
|
}
|
|
|
|
// ignore MakeFile error (already exists is ok)
|
|
|
|
filestore.WFS.MakeFile(ctx, blockId, "cache:term:"+stateType, nil, filestore.FileOptsType{})
|
|
|
|
err = filestore.WFS.WriteFile(ctx, blockId, "cache:term:"+stateType, []byte(state))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot save terminal state: %w", err)
|
|
|
|
}
|
2024-10-24 20:01:58 +02:00
|
|
|
fileMeta := filestore.FileMeta{
|
|
|
|
"ptyoffset": ptyOffset,
|
|
|
|
"termsize": termSize,
|
|
|
|
}
|
|
|
|
err = filestore.WFS.WriteMeta(ctx, blockId, "cache:term:"+stateType, fileMeta, true)
|
2024-06-18 07:38:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot save terminal state meta: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-07-30 07:35:21 +02:00
|
|
|
|
|
|
|
func (bs *BlockService) SaveWaveAiData(ctx context.Context, blockId string, history []wshrpc.OpenAIPromptMessageType) error {
|
2024-08-20 23:56:48 +02:00
|
|
|
block, err := wstore.DBMustGet[*waveobj.Block](ctx, blockId)
|
2024-07-30 07:35:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-08-20 23:56:48 +02:00
|
|
|
viewName := block.Meta.GetString(waveobj.MetaKey_View, "")
|
2024-07-30 21:33:28 +02:00
|
|
|
if viewName != "waveai" {
|
|
|
|
return fmt.Errorf("invalid view type: %s", viewName)
|
2024-07-30 07:35:21 +02:00
|
|
|
}
|
|
|
|
historyBytes, err := json.Marshal(history)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to serialize ai history: %v", err)
|
|
|
|
}
|
|
|
|
// ignore MakeFile error (already exists is ok)
|
|
|
|
filestore.WFS.MakeFile(ctx, blockId, "aidata", nil, filestore.FileOptsType{})
|
|
|
|
err = filestore.WFS.WriteFile(ctx, blockId, "aidata", historyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot save terminal state: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|