-
-
Notifications
You must be signed in to change notification settings - Fork 526
/
Copy pathApp.tsx
178 lines (164 loc) · 4.92 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import {
AIToolbarButton,
BlockNoteAIContextProvider,
BlockNoteAIUI,
locales as aiLocales,
createBlockNoteAIClient,
getAISlashMenuItems,
useBlockNoteAIContext,
} from "@blocknote/xl-ai";
import { createGroq } from "@ai-sdk/groq";
import { createOpenAI } from "@ai-sdk/openai";
import {
BlockNoteEditor,
BlockNoteSchema,
defaultBlockSpecs,
filterSuggestionItems,
locales,
} from "@blocknote/core";
import "@blocknote/core/fonts/inter.css";
import { BlockNoteView } from "@blocknote/mantine";
import "@blocknote/mantine/style.css";
import {
FormattingToolbar,
FormattingToolbarController,
SuggestionMenuController,
blockTypeSelectItems,
getDefaultReactSlashMenuItems,
getFormattingToolbarItems,
useCreateBlockNote,
} from "@blocknote/react";
import "@blocknote/xl-ai/style.css";
import { Fieldset, Switch } from "@mantine/core";
import { useMemo, useState } from "react";
import { BasicAutocomplete } from "./AutoComplete.js";
import RadioGroupComponent from "./components/RadioGroupComponent.js";
const schema = BlockNoteSchema.create({
blockSpecs: {
...defaultBlockSpecs,
// ai: AIBlock,
},
});
const client = createBlockNoteAIClient({
apiKey: import.meta.env.VITE_BLOCKNOTE_AI_SERVER_API_KEY || "PLACEHOLDER",
baseURL:
import.meta.env.VITE_BLOCKNOTE_AI_SERVER_BASE_URL ||
"https://localhost:3000/ai",
});
export default function App() {
const [aiModelString, setAiModelString] = useState(
"openai/gpt-4o-2024-08-06"
);
// Creates a new editor instance.
const editor = useCreateBlockNote({
schema,
dictionary: {
...locales.en,
ai: aiLocales.en,
} as any,
});
const model = useMemo(() => {
const [provider, ...modelNameParts] = aiModelString.split("/");
const modelName = modelNameParts.join("/");
if (provider === "openai") {
return createOpenAI({
...client.getProviderSettings("openai"),
})(modelName, {});
} else if (provider === "groq") {
return createGroq({
...client.getProviderSettings("groq"),
})(modelName);
} else if (provider === "albert-etalab") {
return createOpenAI({
// albert-etalab/neuralmagic/Meta-Llama-3.1-70B-Instruct-FP8
baseURL: "https://albert.api.staging.etalab.gouv.fr/v1",
...client.getProviderSettings("albert-etalab"),
compatibility: "compatible",
})(modelName);
}
throw new Error(`Unknown model: ${aiModelString}`);
}, [aiModelString]);
const [dataFormat, setDataFormat] = useState<"json" | "markdown">("json");
const [streamStateValue, setStream] = useState(true);
const stream = dataFormat === "markdown" ? false : streamStateValue;
// Renders the editor instance using a React component.
return (
<div>
<Fieldset legend="Model settings" style={{ maxWidth: "500px" }}>
<BasicAutocomplete value={aiModelString} onChange={setAiModelString} />
<RadioGroupComponent
label="Data format"
items={[
{ name: "JSON", description: "JSON format", value: "json" },
{
name: "Markdown",
description: "Markdown format",
value: "markdown",
},
]}
value={dataFormat}
onChange={(value) => setDataFormat(value as "json" | "markdown")}
/>
<Switch
checked={stream}
disabled={dataFormat === "markdown"}
onChange={(e) => setStream(e.target.checked)}
label="Streaming"
/>
</Fieldset>
<BlockNoteView
editor={editor}
formattingToolbar={false}
slashMenu={false}>
<BlockNoteAIContextProvider
model={model}
dataFormat={dataFormat}
// TODO: doesn't work well with typings
stream={streamStateValue as any}>
<BlockNoteAIUI />
<FormattingToolbarController
formattingToolbar={() => (
<FormattingToolbar>
{...getFormattingToolbarItems([
...blockTypeSelectItems(editor.dictionary),
// ...aiBlockTypeSelectItems(aiLocales.en),
])}
<AIToolbarButton />
</FormattingToolbar>
)}
/>
<SuggestionMenu editor={editor} />
</BlockNoteAIContextProvider>
{/* TODO: Side Menu customization */}
</BlockNoteView>
</div>
);
}
function SuggestionMenu(props: { editor: BlockNoteEditor<any, any, any> }) {
const ctx = useBlockNoteAIContext();
return (
<SuggestionMenuController
triggerCharacter="/"
getItems={async (query) =>
filterSuggestionItems(
[
...getDefaultReactSlashMenuItems(props.editor),
...getAISlashMenuItems(props.editor, ctx),
],
query
)
}
/>
);
}
/**
* - correct commands
* - FIX UI
*
* API:
* - context (which part of document to pass along)
* - based on selection
*
* - box with proposal
*
*/