Skip to content

Commit

Permalink
feat: allow sending attachment-only messages (#784)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Sep 9, 2024
1 parent adc0e21 commit c845fcf
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-crews-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@assistant-ui/react": patch
---

feat: allow sending attachment-only messages
2 changes: 2 additions & 0 deletions packages/react/src/context/providers/ThreadProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ export const ThreadProvider: FC<PropsWithChildren<ThreadProviderProps>> = ({

const composerState = context.useComposer.getState();
if (
thread.composer.isEmpty !== composerState.isEmpty ||
thread.composer.text !== composerState.text ||
thread.composer.attachments !== composerState.attachments ||
state.capabilities.cancel !== composerState.canCancel
) {
writableStore(context.useComposer).setState({
isEmpty: thread.composer.isEmpty,
text: thread.composer.text,
attachments: thread.composer.attachments,
canCancel: state.capabilities.cancel,
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/context/stores/Composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type ComposerState = Readonly<{

canCancel: boolean;
isEditing: true;
isEmpty: boolean;

send: () => void;
cancel: () => void;
Expand Down Expand Up @@ -60,6 +61,7 @@ export const makeComposerStore = (

canCancel: runtime.capabilities.cancel,
isEditing: true,
isEmpty: runtime.composer.isEmpty,

send: () => {
const runtime = useThreadRuntime.getState();
Expand Down
13 changes: 10 additions & 3 deletions packages/react/src/context/stores/EditComposer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { create } from "zustand";
import { ReadonlyStore } from "../ReadonlyStore";

export type EditComposerState = Readonly<{
// TODO
// TODO
/** @deprecated Use `text` instead. */
value: string;
/** @deprecated Use `setText` instead. */
Expand All @@ -13,6 +13,7 @@ export type EditComposerState = Readonly<{

canCancel: boolean;
isEditing: boolean;
isEmpty: boolean;

edit: () => void;
send: () => void;
Expand All @@ -36,15 +37,21 @@ export const makeEditComposerStore = ({

text: "",
setText: (text) => {
set({ text });
set({ text, isEmpty: text.trim().length === 0 });
},

canCancel: false,
isEditing: false,
isEmpty: true,

edit: () => {
const text = onEdit();
set({ isEditing: true, canCancel: true, text });
set({
isEditing: true,
canCancel: true,
isEmpty: text.trim().length === 0,
text,
});
},
send: () => {
const text = get().text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const useComposerSend = () => {

const disabled = useCombinedStore(
[useThread, useComposer],
(t, c) => t.isRunning || !c.isEditing || c.text.length === 0,
(t, c) => t.isRunning || !c.isEditing || c.isEmpty,
);

const callback = useCallback(() => {
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/runtimes/core/ThreadRuntime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export declare namespace ThreadRuntime {
addAttachment: (file: File) => Promise<void>;
removeAttachment: (attachmentId: string) => Promise<void>;

isEmpty: boolean;

text: string;
setText: (value: string) => void;

Expand Down
4 changes: 4 additions & 0 deletions packages/react/src/runtimes/utils/ThreadRuntimeComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export class ThreadRuntimeComposer implements ThreadRuntime.Composer {

public attachmentAccept: string = "*";

public get isEmpty() {
return !this.text.trim() && !this.attachments.length;
}

constructor(
private runtime: {
messages: ThreadRuntime["messages"];
Expand Down

0 comments on commit c845fcf

Please sign in to comment.