Skip to content

Commit

Permalink
Add document versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
flvndvd authored and Henry Fontanier committed Nov 28, 2024
1 parent 7fffbe7 commit df2a1d5
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 56 deletions.
6 changes: 4 additions & 2 deletions front/components/assistant/conversation/AgentMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,8 @@ export function AgentMessage({
]);
const { configuration: agentConfiguration } = agentMessageToRender;

const messageVersion = message.rank * 1000 + message.version;

const additionalMarkdownComponents: Components = useMemo(
() => ({
visualization: getVisualizationPlugin(
Expand All @@ -461,12 +463,12 @@ export function AgentMessage({
),
doc: getInteractiveDocumentPlugin(
agentConfiguration.sId,
`${message.rank}.${message.version}`
messageVersion.toString()
),
sup: CiteBlock,
mention: MentionBlock,
}),
[owner, conversationId, message.sId, agentConfiguration.sId]
[owner, conversationId, message.sId, agentConfiguration.sId, messageVersion]
);

const additionalMarkdownPlugins: PluggableList = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import type { LightWorkspaceType } from "@dust-tt/types";
import { useCallback, useMemo } from "react";

import { VisualizationActionIframe } from "@app/components/assistant/conversation/actions/VisualizationActionIframe";
import type { CoEditionState } from "@app/components/assistant/conversation/co-edition/CoEditionContext";
import type {
CoEditionState,
CoEditionVisualizationContent,
} from "@app/components/assistant/conversation/co-edition/CoEditionContext";
import { useCoEditionContext } from "@app/components/assistant/conversation/co-edition/CoEditionContext";

interface CoEditionContainerProps {
Expand All @@ -17,51 +20,74 @@ export function CoEditionContainer({
}: CoEditionContainerProps) {
const { state, actions } = useCoEditionContext();

const renderContent = useCallback(
(coEditionContent: CoEditionState["content"]) => {
if (!coEditionContent) {
return null;
}
// Only render one visualization at a time
const renderVisualization = useCallback(
(identifier: string, content: CoEditionVisualizationContent) => {
return (
<div key={`${identifier}-${content.version}`} className="p-4">
<h1>ID: {identifier}</h1>
<VisualizationActionIframe
owner={owner}
visualization={{
code: content.code,
complete: content.complete,
identifier,
}}
conversationId={conversationId}
agentConfigurationId={content.agentConfigurationId}
/>
</div>
);
},
[conversationId, owner]
);

const nodes = [];
// const renderContent = useCallback(
// (coEditionContent: CoEditionState["content"]) => {
// console.log("> state content has changed", coEditionContent);
// if (!coEditionContent) {
// return null;
// }

for (const [identifier, content] of coEditionContent) {
switch (content.type) {
case "visualization":
const { agentConfigurationId, code, complete } = content;
// const nodes = [];

nodes.push(
<>
<h1>ID: {identifier}</h1>
<VisualizationActionIframe
owner={owner}
visualization={{
code,
complete,
identifier,
}}
key={identifier}
conversationId={conversationId}
agentConfigurationId={agentConfigurationId}
/>
</>
);
break;
// for (const [identifier, content] of Object.entries(coEditionContent)) {
// switch (content.type) {
// case "visualization":
// const { agentConfigurationId, code, complete } = content;

default:
return null;
}
}
// nodes.push(
// <>
// <h1>ID: {identifier}</h1>
// <VisualizationActionIframe
// owner={owner}
// visualization={{
// code,
// complete,
// identifier,
// }}
// key={identifier}
// conversationId={conversationId}
// agentConfigurationId={agentConfigurationId}
// />
// </>
// );
// break;

return nodes;
},
[conversationId, owner]
);
// default:
// return null;
// }
// }

const nodes = useMemo(
() => renderContent(state.content),
[state.content, renderContent]
);
// return nodes;
// },
// [conversationId, owner]
// );

// const nodes = useMemo(
// () => renderContent(state.content),
// [state.content, renderContent]
// );

return (
<>
Expand All @@ -75,7 +101,14 @@ export function CoEditionContainer({
onClick={actions.hide}
/>
</div>
{nodes}
{Array.from(Object.entries(state.content)).map(
([identifier, content]) => {
if (content.type === "visualization") {
return renderVisualization(identifier, content);
}
return null;
}
)}
</div>
)}
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
import { createContext, useContext, useState } from "react";

type VisualizationContent = {
interface BaseCodeEditionContent {
version: string;
}

export type CoEditionVisualizationContent = BaseCodeEditionContent & {
type: "visualization";
agentConfigurationId: string;
code: string;
complete: boolean;
};

type TextContent = {
type TextContent = BaseCodeEditionContent & {
type: "text";
content: string;
title?: string;
};

type CodeContent = {
type CodeContent = BaseCodeEditionContent & {
type: "code";
content: string;
language: string;
title?: string;
};

type CoEditionContent = VisualizationContent | TextContent | CodeContent;
type CoEditionContent =
| CoEditionVisualizationContent
| TextContent
| CodeContent;

export interface CoEditionState {
isVisible: boolean;
content: Map<string, CoEditionContent>;
content: Record<string, CoEditionContent>;
}

export interface CoEditionContextType {
Expand Down Expand Up @@ -56,7 +63,7 @@ const CoEditionContext = createContext<CoEditionContextType | null>(null);
export function CoEditionProvider({ children }: { children: React.ReactNode }) {
const [state, setState] = useState<CoEditionState>({
isVisible: false,
content: new Map(),
content: {},
});

const actions = {
Expand All @@ -69,32 +76,48 @@ export function CoEditionProvider({ children }: { children: React.ReactNode }) {
agentConfigurationId,
code,
complete,
version,
}: {
agentConfigurationId: string;
code: string;
complete: boolean;
identifier: string;
version: string;
}
) => {
console.log(">>> updating state with visualization", identifier);
setState((prevState) => {
if (!prevState.content) {
prevState.content = new Map();
const existingContent = prevState.content[identifier];
if (existingContent) {
// Same version, no need to update
if (existingContent.version === version) {
return prevState;
}

// Otherwise keep the latest version.
const sortedVersions = [existingContent.version, version].sort();
// If the new version is NOT the latest, ignore.
if (sortedVersions[1] !== version) {
return prevState;
}
}

prevState.content.set(identifier, {
prevState.content[identifier] = {
type: "visualization",
agentConfigurationId,
code,
complete,
});
version,
};

return prevState;
return {
...prevState,
};
});
},
clear: () => {
setState({
isVisible: false,
content: new Map(),
content: {},
});
},
};
Expand Down

0 comments on commit df2a1d5

Please sign in to comment.