-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathChatPlugin.tsx
More file actions
98 lines (92 loc) · 3.14 KB
/
ChatPlugin.tsx
File metadata and controls
98 lines (92 loc) · 3.14 KB
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
/* Copyright 2026 Marimo. All rights reserved. */
import type { UIMessage } from "ai";
import React, { Suspense } from "react";
import { z } from "zod";
import { createPlugin } from "@/plugins/core/builder";
import { rpc } from "@/plugins/core/rpc";
import { Arrays } from "@/utils/arrays";
import type { CancelPromptRequest, SendMessageRequest } from "./types";
const LazyChatbot = React.lazy(() =>
import("./chat-ui").then((m) => ({ default: m.Chatbot })),
);
// oxlint-disable-next-line typescript/consistent-type-definitions
export type PluginFunctions = {
get_chat_history: (req: {}) => Promise<{ messages: UIMessage[] }>;
delete_chat_history: (req: {}) => Promise<null>;
delete_chat_message: (req: { index: number }) => Promise<null>;
send_prompt: (req: SendMessageRequest) => Promise<unknown>;
cancel_prompt: (req: CancelPromptRequest) => Promise<null>;
};
const messageSchema = z.array(
z.object({
id: z.string(),
role: z.enum(["system", "user", "assistant"]),
content: z.string().nullable(),
parts: z.array(z.any()),
metadata: z.any().nullable(),
}),
);
const configSchema = z.object({
max_tokens: z.number().nullable(),
temperature: z.number().nullable(),
top_p: z.number().nullable(),
top_k: z.number().nullable(),
frequency_penalty: z.number().nullable(),
presence_penalty: z.number().nullable(),
});
export const ChatPlugin = createPlugin<{ messages: UIMessage[] }>(
"marimo-chatbot",
)
.withData(
z.object({
prompts: z.array(z.string()).default(Arrays.EMPTY),
showConfigurationControls: z.boolean(),
maxHeight: z.number().optional(),
config: configSchema,
allowAttachments: z.union([z.boolean(), z.string().array()]),
disabled: z.boolean().default(false),
}),
)
.withFunctions<PluginFunctions>({
get_chat_history: rpc.input(z.object({})).output(
z.object({
messages: messageSchema,
}),
),
delete_chat_history: rpc.input(z.object({})).output(z.null()),
delete_chat_message: rpc
.input(z.object({ index: z.number() }))
.output(z.null()),
send_prompt: rpc
.input(
z.object({
request_id: z.string(),
messages: messageSchema,
config: configSchema,
}),
)
.output(z.unknown()),
cancel_prompt: rpc
.input(z.object({ request_id: z.string() }))
.output(z.null()),
})
.renderer((props) => (
<Suspense>
<LazyChatbot
prompts={props.data.prompts}
showConfigurationControls={props.data.showConfigurationControls}
maxHeight={props.data.maxHeight}
allowAttachments={props.data.allowAttachments}
disabled={props.data.disabled}
config={props.data.config}
get_chat_history={props.functions.get_chat_history}
delete_chat_history={props.functions.delete_chat_history}
delete_chat_message={props.functions.delete_chat_message}
send_prompt={props.functions.send_prompt}
cancel_prompt={props.functions.cancel_prompt}
value={props.value?.messages || Arrays.EMPTY}
setValue={(messages) => props.setValue({ messages })}
host={props.host}
/>
</Suspense>
));