From 7cba3c46d46c6e4a9668dd2339e20f0449009c8d Mon Sep 17 00:00:00 2001 From: Sylvie Crowe <107814465+oneirocosm@users.noreply.github.com> Date: Thu, 1 Aug 2024 00:57:06 -0700 Subject: [PATCH] Additional Demo Fixes (#191) --- frontend/app/view/cpuplot.tsx | 13 ++++++++++++- frontend/app/view/waveai.tsx | 2 +- frontend/types/gotypes.d.ts | 2 ++ pkg/waveai/waveai.go | 2 +- pkg/wconfig/settingsconfig.go | 2 +- pkg/wshrpc/wshrpctypes.go | 3 ++- pkg/wshrpc/wshserver/wshserver.go | 11 +++++++++++ pkg/wstore/wstore_meta.go | 2 ++ 8 files changed, 32 insertions(+), 5 deletions(-) diff --git a/frontend/app/view/cpuplot.tsx b/frontend/app/view/cpuplot.tsx index 826f32bb0..1b407a309 100644 --- a/frontend/app/view/cpuplot.tsx +++ b/frontend/app/view/cpuplot.tsx @@ -3,6 +3,7 @@ import { useHeight } from "@/app/hook/useHeight"; import { useWidth } from "@/app/hook/useWidth"; +import { WOS } from "@/store/global"; import { WshServer } from "@/store/wshserver"; import * as Plot from "@observablehq/plot"; import dayjs from "dayjs"; @@ -28,9 +29,11 @@ class CpuPlotViewModel { dataAtom: jotai.PrimitiveAtom>; addDataAtom: jotai.WritableAtom; width: number; + incrementCount: jotai.WritableAtom>; constructor(blockId: string) { this.blockId = blockId; + this.blockAtom = WOS.getWaveObjectAtom(`block:${blockId}`); this.width = 100; this.dataAtom = jotai.atom(this.getDefaultData()); this.addDataAtom = jotai.atom(null, (get, set, point) => { @@ -45,6 +48,11 @@ class CpuPlotViewModel { this.viewName = jotai.atom((get) => { return "CPU %"; // should not be hardcoded }); + this.incrementCount = jotai.atom(null, async (get, set) => { + const meta = get(this.blockAtom).meta; + const count = meta.count ?? 0; + await WshServer.SetMetaCommand({ oref: WOS.makeORef("block", this.blockId), meta: { count: count + 1 } }); + }); } getDefaultData(): Array { @@ -69,6 +77,8 @@ function CpuPlotView({ model }: { model: CpuPlotViewModel }) { const addPlotData = jotai.useSetAtom(model.addDataAtom); const parentHeight = useHeight(containerRef); const parentWidth = useWidth(containerRef); + const block = jotai.useAtomValue(model.blockAtom); + const incrementCount = jotai.useSetAtom(model.incrementCount); // temporary React.useEffect(() => { console.log("plotData:", plotData); @@ -76,8 +86,9 @@ function CpuPlotView({ model }: { model: CpuPlotViewModel }) { React.useEffect(() => { const temp = async () => { + await incrementCount(); const dataGen = WshServer.StreamCpuDataCommand( - { id: model.blockId }, + { id: model.blockId, count: (block.meta?.count ?? 0) + 1 }, { timeout: 999999999, noresponse: false } ); try { diff --git a/frontend/app/view/waveai.tsx b/frontend/app/view/waveai.tsx index 3d52e3c2a..3742e2e74 100644 --- a/frontend/app/view/waveai.tsx +++ b/frontend/app/view/waveai.tsx @@ -107,7 +107,7 @@ export class WaveAiModel implements ViewModel { const viewTextChildren: HeaderElem[] = [ { elemtype: "text", - text: get(atoms.settingsConfigAtom).ai?.model ?? "gpt-4o-mini", + text: get(atoms.settingsConfigAtom).ai?.model ?? "gpt-3.5-turbo", }, ]; return viewTextChildren; diff --git a/frontend/types/gotypes.d.ts b/frontend/types/gotypes.d.ts index de049d688..17485bde5 100644 --- a/frontend/types/gotypes.d.ts +++ b/frontend/types/gotypes.d.ts @@ -134,6 +134,7 @@ declare global { // wshrpc.CpuDataRequest type CpuDataRequest = { id: string; + count: number; }; // wshrpc.CpuDataType @@ -217,6 +218,7 @@ declare global { "term:fontfamily"?: string; "term:mode"?: string; "term:theme"?: string; + count?: number; }; // tsgenmeta.MethodMeta diff --git a/pkg/waveai/waveai.go b/pkg/waveai/waveai.go index 3800b9c8c..47a51c60b 100644 --- a/pkg/waveai/waveai.go +++ b/pkg/waveai/waveai.go @@ -81,7 +81,7 @@ func GetWSEndpoint() string { } const DefaultMaxTokens = 1000 -const DefaultModel = "gpt-4o-mini" +const DefaultModel = "gpt-3.5-turbo" const DefaultStreamChanSize = 10 const PCloudWSEndpoint = "wss://wsapi.waveterm.dev/" const PCloudWSEndpointVarName = "PCLOUD_WS_ENDPOINT" diff --git a/pkg/wconfig/settingsconfig.go b/pkg/wconfig/settingsconfig.go index 137d90d0f..41a9e48e4 100644 --- a/pkg/wconfig/settingsconfig.go +++ b/pkg/wconfig/settingsconfig.go @@ -259,7 +259,7 @@ func applyDefaultSettings(settings *SettingsConfigType) { if settings.Ai == nil { settings.Ai = &AiConfigType{ Name: userName, - Model: "gpt-4o-mini", + Model: "gpt-3.5-turbo", MaxTokens: 1000, TimeoutMs: 10 * 1000, } diff --git a/pkg/wshrpc/wshrpctypes.go b/pkg/wshrpc/wshrpctypes.go index dab222964..04be12c75 100644 --- a/pkg/wshrpc/wshrpctypes.go +++ b/pkg/wshrpc/wshrpctypes.go @@ -232,7 +232,8 @@ type OpenAIUsageType struct { } type CpuDataRequest struct { - Id string `json:"id"` + Id string `json:"id"` + Count int `json:"count"` } type CpuDataType struct { diff --git a/pkg/wshrpc/wshserver/wshserver.go b/pkg/wshrpc/wshserver/wshserver.go index b2ab795e8..07e101f4b 100644 --- a/pkg/wshrpc/wshserver/wshserver.go +++ b/pkg/wshrpc/wshserver/wshserver.go @@ -97,6 +97,17 @@ func (ws *WshServer) StreamCpuDataCommand(ctx context.Context, request wshrpc.Cp rtn <- wshrpc.RespOrErrorUnion[wshrpc.CpuDataType]{Error: err} return } + blockData, getBlockDataErr := wstore.DBMustGet[*wstore.Block](ctx, request.Id) + if getBlockDataErr != nil { + rtn <- wshrpc.RespOrErrorUnion[wshrpc.CpuDataType]{Error: getBlockDataErr} + return + } + count := blockData.Meta.GetInt(wstore.MetaKey_Count, 0) + if count != request.Count { + rtn <- wshrpc.RespOrErrorUnion[wshrpc.CpuDataType]{Error: fmt.Errorf("new instance created. canceling old goroutine")} + return + } + } }() diff --git a/pkg/wstore/wstore_meta.go b/pkg/wstore/wstore_meta.go index e8c8b116c..a4cf0a1c8 100644 --- a/pkg/wstore/wstore_meta.go +++ b/pkg/wstore/wstore_meta.go @@ -54,6 +54,7 @@ const ( MetaKey_TermFontFamily = "term:fontfamily" MetaKey_TermMode = "term:mode" MetaKey_TermTheme = "term:theme" + MetaKey_Count = "count" // temp for cpu plot. will remove later ) // for typescript typing @@ -96,6 +97,7 @@ type MetaTSType struct { TermFontFamily string `json:"term:fontfamily,omitempty"` TermMode string `json:"term:mode,omitempty"` TermTheme string `json:"term:theme,omitempty"` + Count int `json:"count,omitempty"` // temp for cpu plot. will remove later } type MetaDataDecl struct {