-
Notifications
You must be signed in to change notification settings - Fork 1.1k
use image uploader in chat #499
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
ec61b96
image uploader in chat extension
justschen 6c4f578
fix comment
justschen e108a99
cleanup
justschen af4f212
use capi client + extract into own service
justschen 1749283
capi version bump + injected service
justschen 361c9d0
cleanup
justschen d345aed
Merge branch 'main' into justin/nincada
justschen 8f170ff
add service in promptrenderer
justschen ddc9cac
Merge branch 'main' into justin/nincada
justschen 574d5d5
add null image service for tets
justschen 49ca5c4
add null service in vscode node too
justschen 7282681
trying to remove unsued service in prompt renderer
justschen 373c7cf
cleanup spacing
justschen 42a09be
use urlREquestMetaData instead
justschen 9775a9c
remove vendor
justschen 8a712f1
remove whitespace
justschen 64ff922
some more cleanup
justschen 97fccf2
more robust mimetype check
justschen da576ba
add exp setting
justschen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { createServiceIdentifier } from '../../../util/common/services'; | ||
import { URI } from '../../../util/vs/base/common/uri'; | ||
|
||
export const IImageService = createServiceIdentifier<IImageService>('IImageService'); | ||
|
||
export interface IImageService { | ||
readonly _serviceBrand: undefined; | ||
|
||
/** | ||
* Upload image data to GitHub Copilot chat attachments endpoint | ||
* @param binaryData The image binary data as Uint8Array | ||
* @param name The name for the uploaded file | ||
* @param mimeType The MIME type of the image | ||
* @param token The authentication token for GitHub API | ||
* @returns Promise<URI> The URI of the uploaded image | ||
*/ | ||
uploadChatImageAttachment(binaryData: Uint8Array, name: string, mimeType: string | undefined, token: string | undefined): Promise<URI>; | ||
} | ||
|
||
export const nullImageService: IImageService = { | ||
_serviceBrand: undefined, | ||
async uploadChatImageAttachment(): Promise<URI> { | ||
throw new Error('Image service not implemented'); | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { RequestType } from '@vscode/copilot-api'; | ||
import { URI } from '../../../util/vs/base/common/uri'; | ||
import { ICAPIClientService } from '../../endpoint/common/capiClient'; | ||
import { IImageService } from '../common/imageService'; | ||
|
||
export class ImageServiceImpl implements IImageService { | ||
declare readonly _serviceBrand: undefined; | ||
|
||
constructor( | ||
@ICAPIClientService private readonly capiClient: ICAPIClientService, | ||
) { } | ||
|
||
async uploadChatImageAttachment(binaryData: Uint8Array, name: string, mimeType: string | undefined, token: string | undefined): Promise<URI> { | ||
if (!mimeType || !token) { | ||
throw new Error('Missing required mimeType or token for image upload'); | ||
} | ||
|
||
const sanitizedName = name.replace(/[^a-zA-Z0-9._-]/g, ''); | ||
let uploadName = sanitizedName; | ||
|
||
// can catch unexpected types like "IMAGE/JPEG", "image/svg+xml", or "image/png; charset=UTF-8" | ||
const subtypeMatch = mimeType.toLowerCase().match(/^[^\/]+\/([^+;]+)/); | ||
const subtype = subtypeMatch?.[1]; | ||
|
||
// add the extension if it is missing. | ||
if (subtype && !uploadName.toLowerCase().endsWith(`.${subtype}`)) { | ||
uploadName = `${uploadName}.${subtype}`; | ||
} | ||
|
||
try { | ||
const response = await this.capiClient.makeRequest<Response>({ | ||
method: 'POST', | ||
body: binaryData, | ||
headers: { | ||
'Content-Type': 'application/octet-stream', | ||
Authorization: `Bearer ${token}`, | ||
} | ||
}, { type: RequestType.ChatAttachmentUpload, uploadName, mimeType }); | ||
if (!response.ok) { | ||
throw new Error(`Image upload failed: ${response.status} ${response.statusText}`); | ||
} | ||
const result = await response.json() as { url: string }; | ||
return URI.parse(result.url); | ||
} catch (error) { | ||
throw new Error(`Error uploading image: ${error}`); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.