Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion browser/src/components/ChatSender/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { QuillContext } from '../QuillEditor/QuillContext';
import SuggestionList, { type ISuggestionListRef } from '../SuggestionList';
import SenderFooter from './SenderFooter';
import SenderHeader from './SenderHeader';
import type { FilePart, ImagePart } from '@/types/chat';

const useStyle = createStyles(({ token, css }) => {
const maxWidth = 800;
Expand Down Expand Up @@ -54,6 +55,7 @@ const ChatSender: React.FC = () => {
const { t } = useTranslation();
const { status } = useSnapshot(state);
const { prompt } = useSnapshot(sender.state);
const { attachments } = useSnapshot(context.state);

const [openPopup, setOpenPopup] = useState(false);
const [inputText, setInputText] = useState<string>('');
Expand All @@ -73,7 +75,10 @@ const ChatSender: React.FC = () => {
} = useSuggestion();

const handleSubmit = () => {
actions.send(prompt, sender.state.delta);
actions.send(prompt, {
delta: sender.state.delta,
attachments: attachments as (ImagePart | FilePart)[],
});
setInputText('');
sender.actions.updatePrompt('');
sender.actions.updateDelta(new Delta());
Expand Down
20 changes: 12 additions & 8 deletions browser/src/state/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,17 @@ interface ChatActions {
sessionId: string;
messages: Message[];
}): Promise<() => void>;
send(message: string, delta?: Delta): void;
send(
message: string,
extra: { delta?: Delta; attachments?: (FilePart | ImagePart)[] },
): void;
addMessage(message: UIMessage | UIMessage[]): void;
destroy(): void;
sendMessage(opts: {
message: string | null;
planMode?: boolean;
model?: string;
attachments?: (FilePart | ImagePart)[];
}): Promise<LoopResult | { success: false; error: Error }>;
getSlashCommands(): Promise<CommandEntry[]>;
getFiles(opts: { query?: string }): Promise<FileItem[]>;
Expand Down Expand Up @@ -255,7 +259,7 @@ export const actions: ChatActions = {
};
},

async send(message, delta: Delta) {
async send(message, { delta, attachments }) {
const { cwd, sessionId } = state;

const isDelta = BLOT_NAME_CONTENT_REGEX.test(message);
Expand All @@ -267,13 +271,13 @@ export const actions: ChatActions = {
});

if (!isDelta) {
const result = await this.sendMessage({ message });
const result = await this.sendMessage({ message, attachments });
await this.setSummary({ userPrompt: message, result });
return;
}

const isCommand = SLASH_COMMAND_REGEX.test(message);
const prompt = getPrompt(delta);
const prompt = delta ? getPrompt(delta) : '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

什么情况会是空

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handleCapabilityClick 也就是欢迎页点击触发的send是空


if (!isCommand) {
await clientActions.request('session.addMessages', {
Expand All @@ -287,7 +291,7 @@ export const actions: ChatActions = {
},
],
});
const result = await this.sendMessage({ message: null });
const result = await this.sendMessage({ message: null, attachments });
await this.setSummary({ userPrompt: message, result });
return;
}
Expand Down Expand Up @@ -349,7 +353,7 @@ export const actions: ChatActions = {
sessionId,
messages: messages,
});
await this.sendMessage({ message: null });
await this.sendMessage({ message: null, attachments });
} else if (isLocal) {
const parsedMessages = messages.map((message) => {
if (message.role === 'user') {
Expand Down Expand Up @@ -385,21 +389,21 @@ export const actions: ChatActions = {
message: string | null;
planMode?: boolean;
model?: string;
attachments?: (FilePart | ImagePart)[];
}) {
try {
state.status = 'processing';
state.processingTokens = 0;
state.loading = true;
const { cwd, sessionId } = state;
let attachments: Array<FilePart | ImagePart> = [];

const response = (await clientActions.request('session.send', {
message: opts.message,
planMode: opts.planMode,
model: opts.model,
cwd,
sessionId,
attachments,
attachments: opts.attachments,
})) as LoopResult;

if (response.success) {
Expand Down
22 changes: 21 additions & 1 deletion browser/src/state/context.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { proxy } from 'valtio';
import { ContextType } from '@/constants/context';
import * as sender from '@/state/sender';
import type { FileItem, SlashCommand } from '@/types/chat';
import type { FileItem, FilePart, ImagePart, SlashCommand } from '@/types/chat';
import type { ContextItem } from '@/types/context';
import type { ImageItem } from '@/api/model';

interface ContextState {
contexts: {
files: Omit<FileItem, 'name'>[];
slashCommands: Pick<SlashCommand, 'name' | 'description'>[];
};

attachments: (ImagePart | FilePart)[];

attachedContexts: ContextItem[];

loading: boolean;
Expand Down Expand Up @@ -53,6 +56,23 @@ export const state = proxy<ContextState>({
slashCommands,
};
},

get attachments() {
// images
return this.attachedContexts
.filter(
(contextItem: ContextItem) => contextItem.type === ContextType.IMAGE,
)
.map((contextItem: ContextItem) => {
const context = contextItem.context as ImageItem;

return {
type: 'image',
data: context.src,
mimeType: context.mime,
} as ImagePart;
});
},
});

export const actions = {
Expand Down
6 changes: 3 additions & 3 deletions browser/src/types/context.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { FileItem, ImageItem } from '@/api/model';
import type { ImageItem } from '@/api/model';
import type { ContextType } from '@/constants/context';
import type { SlashCommand } from '@/types/chat';
import type { FileItem, SlashCommand } from '@/types/chat';

export type ContextStoreValue = FileItem | ImageItem | SlashCommand;
export type ContextStoreValue = ImageItem | SlashCommand | FileItem;

export interface ContextItem {
type: ContextType;
Expand Down
Loading