From cac32bf5eb20d9577be2ff7d1f98454ee1d0db5b Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Sat, 28 Oct 2023 22:11:26 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20implement=20first=20version=20of?= =?UTF-8?q?=20SessionState=20reducer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/vscode/schema/ContextItemId.d.ts | 19 +++ extensions/vscode/schema/SessionUpdate.d.ts | 61 ++++++++ .../AbstractContinueGUIClientProtocol.ts | 3 +- gui/src/hooks/ContinueGUIClientProtocol.ts | 10 +- gui/src/redux/selectors/chatSelectors.ts | 11 -- gui/src/redux/slices/chatSlice.ts | 93 ------------ gui/src/redux/slices/serverStateReducer.ts | 1 - gui/src/redux/slices/sessionStateReducer.ts | 74 ++++++++++ gui/src/redux/store.ts | 14 +- gui/src/schema/ContextItemId.d.ts | 19 +++ gui/src/schema/SessionUpdate.d.ts | 61 ++++++++ schema/json/ContextItemId.json | 25 ++++ schema/json/SessionUpdate.json | 133 ++++++++++++++++++ server/continuedev/core/main.py | 2 - .../models/generate_json_schema.py | 8 +- 15 files changed, 409 insertions(+), 125 deletions(-) create mode 100644 extensions/vscode/schema/ContextItemId.d.ts create mode 100644 extensions/vscode/schema/SessionUpdate.d.ts delete mode 100644 gui/src/redux/selectors/chatSelectors.ts delete mode 100644 gui/src/redux/slices/chatSlice.ts create mode 100644 gui/src/redux/slices/sessionStateReducer.ts create mode 100644 gui/src/schema/ContextItemId.d.ts create mode 100644 gui/src/schema/SessionUpdate.d.ts create mode 100644 schema/json/ContextItemId.json create mode 100644 schema/json/SessionUpdate.json diff --git a/extensions/vscode/schema/ContextItemId.d.ts b/extensions/vscode/schema/ContextItemId.d.ts new file mode 100644 index 000000000..4d4580eb7 --- /dev/null +++ b/extensions/vscode/schema/ContextItemId.d.ts @@ -0,0 +1,19 @@ +/* eslint-disable */ +/** + * This file was automatically generated by json-schema-to-typescript. + * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file, + * and run json-schema-to-typescript to regenerate this file. + */ + +export type ContextItemId = ContextItemId1; +export type ProviderTitle = string; +export type ItemId = string; + +/** + * A ContextItemId is a unique identifier for a ContextItem. + */ +export interface ContextItemId1 { + provider_title: ProviderTitle; + item_id: ItemId; + [k: string]: unknown; +} diff --git a/extensions/vscode/schema/SessionUpdate.d.ts b/extensions/vscode/schema/SessionUpdate.d.ts new file mode 100644 index 000000000..245810b62 --- /dev/null +++ b/extensions/vscode/schema/SessionUpdate.d.ts @@ -0,0 +1,61 @@ +/* eslint-disable */ +/** + * This file was automatically generated by json-schema-to-typescript. + * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file, + * and run json-schema-to-typescript to regenerate this file. + */ + +export type SessionUpdate = SessionUpdate1; +export type Index = number; +export type Update = SetStep | DeltaStep; +export type StepType = string; +export type Name = string; +export type Description = string; +export type Hide = boolean; +export type Depth = number; +export type Title = string; +export type Message = string; +export type Observations = Observation[]; +export type Logs = string[]; +export type Name1 = string; +export type Description1 = string; +export type Observations1 = Observation[]; +export type Logs1 = string[]; +export type Stop = boolean; + +export interface SessionUpdate1 { + index: Index; + update: Update; + stop?: Stop; + [k: string]: unknown; +} +export interface SetStep { + step_type?: StepType; + name?: Name; + description?: Description; + params?: Params; + hide?: Hide; + depth?: Depth; + error?: ContinueError; + observations?: Observations; + logs?: Logs; + [k: string]: unknown; +} +export interface Params { + [k: string]: unknown; +} +export interface ContinueError { + title: Title; + message: Message; + [k: string]: unknown; +} +export interface Observation { + [k: string]: unknown; +} +export interface DeltaStep { + name?: Name1; + description?: Description1; + observations?: Observations1; + logs?: Logs1; + [k: string]: unknown; +} diff --git a/gui/src/hooks/AbstractContinueGUIClientProtocol.ts b/gui/src/hooks/AbstractContinueGUIClientProtocol.ts index c25ab4060..dfab5d480 100644 --- a/gui/src/hooks/AbstractContinueGUIClientProtocol.ts +++ b/gui/src/hooks/AbstractContinueGUIClientProtocol.ts @@ -1,4 +1,5 @@ -import { ContextItem, ContextItemId } from "../schema/FullState"; +import { ContextItem } from "../schema/ContextItem"; +import { ContextItemId } from "../schema/ContextItemId"; abstract class AbstractContinueGUIClientProtocol { abstract sendMainInput(input: string): void; diff --git a/gui/src/hooks/ContinueGUIClientProtocol.ts b/gui/src/hooks/ContinueGUIClientProtocol.ts index d366f0210..dc73d0f9c 100644 --- a/gui/src/hooks/ContinueGUIClientProtocol.ts +++ b/gui/src/hooks/ContinueGUIClientProtocol.ts @@ -1,6 +1,8 @@ -import { ContextItem, ContextItemId } from "../schema/FullState"; +import { ContextItem } from "../schema/ContextItem"; +import { ContextItemId } from "../schema/ContextItemId"; +import { SessionUpdate } from "../schema/SessionUpdate"; import AbstractContinueGUIClientProtocol from "./AbstractContinueGUIClientProtocol"; -import { Messenger, SocketIOMessenger, WebsocketMessenger } from "./messenger"; +import { Messenger, SocketIOMessenger } from "./messenger"; import { VscodeMessenger } from "./vscodeMessenger"; class ContinueGUIClientProtocol extends AbstractContinueGUIClientProtocol { @@ -85,9 +87,7 @@ class ContinueGUIClientProtocol extends AbstractContinueGUIClientProtocol { onStepUpdate(callback: (update: SessionUpdate) => void) { this.messenger?.onMessageType("step_update", (data: SessionUpdate) => { - if (data.update) { - callback(data.update); - } + callback(data); }); } diff --git a/gui/src/redux/selectors/chatSelectors.ts b/gui/src/redux/selectors/chatSelectors.ts deleted file mode 100644 index 51e8a636b..000000000 --- a/gui/src/redux/selectors/chatSelectors.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { RootStore } from "../store"; - -const selectChatMessages = (state: RootStore) => { - return state.chat.messages; -}; - -const selectIsStreaming = (state: RootStore) => { - return state.chat.isStreaming; -}; - -export { selectChatMessages, selectIsStreaming }; diff --git a/gui/src/redux/slices/chatSlice.ts b/gui/src/redux/slices/chatSlice.ts deleted file mode 100644 index 848cccd45..000000000 --- a/gui/src/redux/slices/chatSlice.ts +++ /dev/null @@ -1,93 +0,0 @@ -import { createSlice } from "@reduxjs/toolkit"; -import { ChatMessage, RootStore } from "../store"; - -export const chatSlice = createSlice({ - name: "chat", - initialState: { - messages: [], - isStreaming: false, - } as RootStore["chat"], - reducers: { - addMessage: ( - state, - action: { - type: string; - payload: ChatMessage; - } - ) => { - return { - ...state, - messages: [...state.messages, action.payload], - }; - }, - setIsStreaming: (state, action) => { - return { - ...state, - isStreaming: action.payload, - }; - }, - streamUpdate: (state, action) => { - if (!state.isStreaming) { - return { - ...state, - messages: [ - ...state.messages, - { - role: "assistant", - content: action.payload, - }, - ], - isStreaming: true, - }; - } else { - let lastMessage = state.messages[state.messages.length - 1]; - if (lastMessage.role !== "assistant") { - return { - ...state, - messages: [ - ...state.messages, - { - role: "assistant", - content: action.payload, - }, - ], - isStreaming: true, - }; - } - return { - ...state, - messages: [ - ...state.messages.slice(0, state.messages.length - 1), - { - ...lastMessage, - content: lastMessage.content + action.payload, - }, - ], - isStreaming: true, - }; - } - }, - closeStream: (state) => { - return { - ...state, - isStreaming: false, - }; - }, - clearChat: (state) => { - return { - ...state, - messages: [], - isStreaming: false, - }; - }, - }, -}); - -export const { - addMessage, - streamUpdate, - closeStream, - clearChat, - setIsStreaming, -} = chatSlice.actions; -export default chatSlice.reducer; diff --git a/gui/src/redux/slices/serverStateReducer.ts b/gui/src/redux/slices/serverStateReducer.ts index 176a780f3..0ea4cb371 100644 --- a/gui/src/redux/slices/serverStateReducer.ts +++ b/gui/src/redux/slices/serverStateReducer.ts @@ -1,5 +1,4 @@ import { createSlice } from "@reduxjs/toolkit"; -import { FullState } from "../../schema/FullState"; import { ContextItem } from "../../schema/ContextItem"; const TEST_TIMELINE = [ diff --git a/gui/src/redux/slices/sessionStateReducer.ts b/gui/src/redux/slices/sessionStateReducer.ts new file mode 100644 index 000000000..2019bd44a --- /dev/null +++ b/gui/src/redux/slices/sessionStateReducer.ts @@ -0,0 +1,74 @@ +import { createSlice } from "@reduxjs/toolkit"; +import { SessionState, StepDescription } from "../../schema/SessionState"; +import { SessionUpdate } from "../../schema/SessionUpdate"; +import { ContextItem } from "../../schema/ContextItem"; + +export interface SessionFullState { + history: StepDescription[]; + context_items: ContextItem[]; + active: boolean; +} + +export const sessionStateSlice = createSlice({ + name: "sessionState", + initialState: { + history: [], + context_items: [], + active: false, + }, + reducers: { + processSessionUpdate: ( + state: SessionFullState, + { payload }: { payload: SessionUpdate } + ) => { + let active = state.active; + if (typeof payload.stop === "boolean") { + active = !payload.stop; + } + + let step: StepDescription | undefined = undefined; + + if (payload.index > state.history.length) { + // Partial above to allow here + step = { + ...(payload.update as StepDescription), + }; + } else if (payload.delta === true) { + step = { + ...state.history[payload.index], + }; + if (payload.update.name) { + step.name += payload.update.name; + } + if (payload.update.description) { + step.description += payload.update.description; + } + if (payload.update.observations) { + step.observations.push(...payload.update.observations); + } + if (payload.update.logs) { + step.logs.push(...payload.update.logs); + } + } else if (payload.delta === false) { + step = { + ...state.history[payload.index], + ...payload.update, + }; + } + + let history = [...state.history]; + if (step) { + history[payload.index] = step; + } + + return { + ...state, + history, + active, + }; + }, + }, +}); + +export const { processSessionUpdate } = sessionStateSlice.actions; +export default sessionStateSlice.reducer; diff --git a/gui/src/redux/store.ts b/gui/src/redux/store.ts index 3960ca156..df8221fa8 100644 --- a/gui/src/redux/store.ts +++ b/gui/src/redux/store.ts @@ -1,11 +1,13 @@ import { configureStore } from "@reduxjs/toolkit"; -import chatReducer from "./slices/chatSlice"; import configReducer from "./slices/configSlice"; import miscReducer from "./slices/miscSlice"; import uiStateReducer from "./slices/uiStateSlice"; -import { FullState } from "../schema/FullState"; import { RangeInFile } from "../schema/RangeInFile"; import serverStateReducer from "./slices/serverStateReducer"; +import { SessionState } from "../schema/SessionState"; +import sessionStateReducer, { + SessionFullState, +} from "./slices/sessionStateReducer"; export interface ChatMessage { role: "system" | "user" | "assistant"; @@ -22,10 +24,6 @@ export interface RootStore { vscMediaUrl: string | undefined; dataSwitchOn: boolean | undefined; }; - chat: { - messages: ChatMessage[]; - isStreaming: boolean; - }; misc: { highlightedCode: RangeInFile | undefined; takenAction: boolean; @@ -39,16 +37,16 @@ export interface RootStore { dialogMessage: string | JSX.Element; dialogEntryOn: boolean; }; - serverState: FullState; + sessionState: SessionFullState; } const store = configureStore({ reducer: { - chat: chatReducer, config: configReducer, misc: miscReducer, uiState: uiStateReducer, serverState: serverStateReducer, + sessionState: sessionStateReducer, }, }); diff --git a/gui/src/schema/ContextItemId.d.ts b/gui/src/schema/ContextItemId.d.ts new file mode 100644 index 000000000..4d4580eb7 --- /dev/null +++ b/gui/src/schema/ContextItemId.d.ts @@ -0,0 +1,19 @@ +/* eslint-disable */ +/** + * This file was automatically generated by json-schema-to-typescript. + * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file, + * and run json-schema-to-typescript to regenerate this file. + */ + +export type ContextItemId = ContextItemId1; +export type ProviderTitle = string; +export type ItemId = string; + +/** + * A ContextItemId is a unique identifier for a ContextItem. + */ +export interface ContextItemId1 { + provider_title: ProviderTitle; + item_id: ItemId; + [k: string]: unknown; +} diff --git a/gui/src/schema/SessionUpdate.d.ts b/gui/src/schema/SessionUpdate.d.ts new file mode 100644 index 000000000..245810b62 --- /dev/null +++ b/gui/src/schema/SessionUpdate.d.ts @@ -0,0 +1,61 @@ +/* eslint-disable */ +/** + * This file was automatically generated by json-schema-to-typescript. + * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file, + * and run json-schema-to-typescript to regenerate this file. + */ + +export type SessionUpdate = SessionUpdate1; +export type Index = number; +export type Update = SetStep | DeltaStep; +export type StepType = string; +export type Name = string; +export type Description = string; +export type Hide = boolean; +export type Depth = number; +export type Title = string; +export type Message = string; +export type Observations = Observation[]; +export type Logs = string[]; +export type Name1 = string; +export type Description1 = string; +export type Observations1 = Observation[]; +export type Logs1 = string[]; +export type Stop = boolean; + +export interface SessionUpdate1 { + index: Index; + update: Update; + stop?: Stop; + [k: string]: unknown; +} +export interface SetStep { + step_type?: StepType; + name?: Name; + description?: Description; + params?: Params; + hide?: Hide; + depth?: Depth; + error?: ContinueError; + observations?: Observations; + logs?: Logs; + [k: string]: unknown; +} +export interface Params { + [k: string]: unknown; +} +export interface ContinueError { + title: Title; + message: Message; + [k: string]: unknown; +} +export interface Observation { + [k: string]: unknown; +} +export interface DeltaStep { + name?: Name1; + description?: Description1; + observations?: Observations1; + logs?: Logs1; + [k: string]: unknown; +} diff --git a/schema/json/ContextItemId.json b/schema/json/ContextItemId.json new file mode 100644 index 000000000..569eec865 --- /dev/null +++ b/schema/json/ContextItemId.json @@ -0,0 +1,25 @@ +{ + "title": "ContextItemId", + "$ref": "#/definitions/continuedev__core__main__ContextItemId", + "definitions": { + "continuedev__core__main__ContextItemId": { + "title": "ContextItemId", + "description": "A ContextItemId is a unique identifier for a ContextItem.", + "type": "object", + "properties": { + "provider_title": { + "title": "Provider Title", + "type": "string" + }, + "item_id": { + "title": "Item Id", + "type": "string" + } + }, + "required": [ + "provider_title", + "item_id" + ] + } + } +} \ No newline at end of file diff --git a/schema/json/SessionUpdate.json b/schema/json/SessionUpdate.json new file mode 100644 index 000000000..882e533f3 --- /dev/null +++ b/schema/json/SessionUpdate.json @@ -0,0 +1,133 @@ +{ + "title": "SessionUpdate", + "$ref": "#/definitions/continuedev__core__main__SessionUpdate", + "definitions": { + "ContinueError": { + "title": "ContinueError", + "type": "object", + "properties": { + "title": { + "title": "Title", + "type": "string" + }, + "message": { + "title": "Message", + "type": "string" + } + }, + "required": [ + "title", + "message" + ] + }, + "Observation": { + "title": "Observation", + "type": "object", + "properties": {} + }, + "SetStep": { + "title": "SetStep", + "type": "object", + "properties": { + "step_type": { + "title": "Step Type", + "type": "string" + }, + "name": { + "title": "Name", + "type": "string" + }, + "description": { + "title": "Description", + "type": "string" + }, + "params": { + "title": "Params", + "type": "object" + }, + "hide": { + "title": "Hide", + "type": "boolean" + }, + "depth": { + "title": "Depth", + "type": "integer" + }, + "error": { + "$ref": "#/definitions/ContinueError" + }, + "observations": { + "title": "Observations", + "type": "array", + "items": { + "$ref": "#/definitions/Observation" + } + }, + "logs": { + "title": "Logs", + "type": "array", + "items": { + "type": "string" + } + } + } + }, + "DeltaStep": { + "title": "DeltaStep", + "type": "object", + "properties": { + "name": { + "title": "Name", + "type": "string" + }, + "description": { + "title": "Description", + "type": "string" + }, + "observations": { + "title": "Observations", + "type": "array", + "items": { + "$ref": "#/definitions/Observation" + } + }, + "logs": { + "title": "Logs", + "type": "array", + "items": { + "type": "string" + } + } + } + }, + "continuedev__core__main__SessionUpdate": { + "title": "SessionUpdate", + "type": "object", + "properties": { + "index": { + "title": "Index", + "type": "integer" + }, + "update": { + "title": "Update", + "anyOf": [ + { + "$ref": "#/definitions/SetStep" + }, + { + "$ref": "#/definitions/DeltaStep" + } + ] + }, + "stop": { + "title": "Stop", + "type": "boolean" + } + }, + "required": [ + "index", + "update" + ] + } + } +} \ No newline at end of file diff --git a/server/continuedev/core/main.py b/server/continuedev/core/main.py index 89f49210c..c44a240f0 100644 --- a/server/continuedev/core/main.py +++ b/server/continuedev/core/main.py @@ -131,8 +131,6 @@ UpdateStep = Union[SetStep, DeltaStep] class StepDescription(BaseModel): - # Can this just be a Step? If the step is already included... - # Totally! It should be right?? step_type: str name: str description: str diff --git a/server/continuedev/models/generate_json_schema.py b/server/continuedev/models/generate_json_schema.py index c85423dc2..614d5468c 100644 --- a/server/continuedev/models/generate_json_schema.py +++ b/server/continuedev/models/generate_json_schema.py @@ -3,8 +3,8 @@ import os from pydantic import schema_json_of from ..core.config import ContinueConfig -from ..core.context import ContextItem -from ..core.main import SessionInfo, SessionState +from ..core.context import ContextItem, ContextItemId +from ..core.main import SessionInfo, SessionState, SessionUpdate from ..core.models import Models from ..libs.llm.base import LLM from .filesystem import FileEdit, RangeInFile @@ -15,9 +15,9 @@ MODELS_TO_GENERATE = ( [Position, Range, Traceback, TracebackFrame] + [RangeInFile, FileEdit] + [FileEditWithFullContents] - + [SessionInfo, SessionState] + + [SessionInfo, SessionState, SessionUpdate] + [ContinueConfig] - + [ContextItem] + + [ContextItem, ContextItemId] + [Models] + [LLM] )