Skip to content

image upload url #434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion src/extension/prompts/node/panel/chatVariables.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export async function renderChatVariables(chatVariables: ChatVariablesCollection
</Tag>
);
} else if (variableValue instanceof ChatReferenceBinaryData) {
elements.push(<Image variableName={variableName} variableValue={await variableValue.data()} reference={variableValue.reference} omitReferences={omitReferences}></Image>);
elements.push(<Image variableName={variableName} variableValue={await variableValue.data()} reference={variableValue.reference} omitReferences={omitReferences} url={variableValue.url}></Image>);
} else if (typeof ChatReferenceDiagnostic !== 'undefined' && variableValue instanceof ChatReferenceDiagnostic) { // check undefined to avoid breaking old Insiders versions
elements.push(<DiagnosticVariable diagnostics={variableValue.diagnostics} />);
}
Expand Down
7 changes: 6 additions & 1 deletion src/extension/prompts/node/panel/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
*--------------------------------------------------------------------------------------------*/

import * as l10n from '@vscode/l10n';
import { BasePromptElementProps, ChatResponseReferencePartStatusKind, PromptElement, PromptReference, PromptSizing, UserMessage, Image as BaseImage } from '@vscode/prompt-tsx';
import { Image as BaseImage, BasePromptElementProps, ChatResponseReferencePartStatusKind, PromptElement, PromptReference, PromptSizing, UserMessage } from '@vscode/prompt-tsx';
import { Uri } from '../../../../vscodeTypes';
import { IPromptEndpoint } from '../base/promptRenderer';

export interface ImageProps extends BasePromptElementProps {
variableName: string;
variableValue: Uint8Array | Promise<Uint8Array>;
omitReferences?: boolean;
url?: string;
reference?: Uri;
}

Expand Down Expand Up @@ -48,6 +49,10 @@ export class Image extends PromptElement<ImageProps, unknown> {
decoded = decodedString;
}

if (this.props.url) {
decoded = this.props.url;
}

return (
<UserMessage priority={0}>
<BaseImage src={decoded} detail='high' />
Expand Down
10 changes: 8 additions & 2 deletions src/extension/prompts/node/panel/toolCalling.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { ITokenizer } from '../../../../util/common/tokenizer';
import { CancellationToken } from '../../../../util/vs/base/common/cancellation';
import { toErrorMessage } from '../../../../util/vs/base/common/errorMessage';
import { isCancellationError } from '../../../../util/vs/base/common/errors';
import { LanguageModelDataPart, LanguageModelPromptTsxPart, ToolResultAudience, LanguageModelTextPart, LanguageModelToolResult, LanguageModelTextPart2, LanguageModelDataPart2 } from '../../../../vscodeTypes';
import { LanguageModelDataPart, LanguageModelDataPart2, LanguageModelPromptTsxPart, LanguageModelTextPart, LanguageModelTextPart2, LanguageModelToolResult, ToolResultAudience } from '../../../../vscodeTypes';
import { isImageDataPart } from '../../../conversation/common/languageModelChatMessageHelpers';
import { IResultMetadata } from '../../../prompt/common/conversation';
import { IBuildPromptContext, IToolCall, IToolCallRound } from '../../../prompt/common/intents';
Expand Down Expand Up @@ -295,7 +295,13 @@ enum ToolInvocationOutcome {
export function imageDataPartToTSX(part: LanguageModelDataPart) {
if (isImageDataPart(part)) {
const base64 = Buffer.from(part.data).toString('base64');
return <Image src={`data:${part.mimeType};base64,${base64}`} />;
const decodedString = new TextDecoder().decode(part.data);

// Check if the decoded data is a URL
const isUrl = /^https?:\/\/.+/.test(decodedString);
const src = isUrl ? decodedString : `data:${part.mimeType};base64,${base64}`;

return <Image src={src} />;
}
}

Expand Down
12 changes: 8 additions & 4 deletions src/extension/vscode.proposed.chatBinaryReferenceData.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

declare module 'vscode' {

/**
* A reference to a value that the user added to their chat request.
*/
export interface ChatPromptReference {
/**
* The value of this reference. The `string | Uri | Location` types are used today, but this could expand in the future.
Expand All @@ -23,10 +20,17 @@ declare module 'vscode' {

/**
* Retrieves the binary data of the reference. This is primarily used to receive image attachments from the chat.
* Note: base64 will only be used as a fallback if upload fails.
* @returns A promise that resolves to the binary data as a Uint8Array.
*/
data(): Thenable<Uint8Array>;

/**
* URL of image that was uploaded to GitHub
*/
url: string;


/**
* Retrieves a URI reference to the binary data, if available.
*/
Expand All @@ -36,6 +40,6 @@ declare module 'vscode' {
* @param mimeType The MIME type of the binary data.
* @param data The binary data of the reference.
*/
constructor(mimeType: string, data: () => Thenable<Uint8Array>);
constructor(mimeType: string, data: () => Thenable<Uint8Array>, url: string, reference?: Uri);
}
}
3 changes: 2 additions & 1 deletion src/util/common/test/shims/chatTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ export class ChatReferenceDiagnostic {
export class ChatReferenceBinaryData {
constructor(
readonly mimeType: string,
readonly data: () => Thenable<Uint8Array>
readonly data: () => Thenable<Uint8Array>,
readonly url: string
) { }
}

Expand Down