-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat: added AI image tools for view and upload to project operations #2939
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
iNerdStack
wants to merge
5
commits into
onlook-dev:main
Choose a base branch
from
iNerdStack:image-upload
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+225
−12
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -39,4 +39,4 @@ export async function handleToolCall(agentType: AgentType, toolCall: ToolCall<st | |
}); | ||
} | ||
|
||
} | ||
} |
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,90 @@ | ||
import { Icons } from '@onlook/ui/icons'; | ||
import type { EditorEngine } from '@onlook/web-client/src/components/store/editor/engine'; | ||
import { MessageContextType, type MessageContext } from '@onlook/models'; | ||
import mime from 'mime-lite'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { z } from 'zod'; | ||
import { ClientTool } from '../models/client'; | ||
import { BRANCH_ID_SCHEMA } from '../shared/type'; | ||
|
||
export class UploadImageTool extends ClientTool { | ||
static readonly toolName = 'upload_image'; | ||
static readonly description = "Uploads an image from the chat context to the project's file system. Use this tool when the user asks you to save, add, or upload an image to their project. The image will be stored in public/images/ directory by default and can be referenced in code. After uploading, you can use the file path in your code changes."; | ||
static readonly parameters = z.object({ | ||
image_id: z | ||
.string() | ||
.describe( | ||
'The unique ID of the image from the available images list', | ||
), | ||
destination_path: z | ||
.string() | ||
.optional() | ||
.describe('Destination path within the project. Defaults to "public/images" if not specified.'), | ||
filename: z | ||
.string() | ||
.optional() | ||
.describe('Custom filename (without extension). If not provided, a UUID will be generated'), | ||
branchId: BRANCH_ID_SCHEMA, | ||
}); | ||
static readonly icon = Icons.Image; | ||
|
||
async handle( | ||
args: z.infer<typeof UploadImageTool.parameters>, | ||
editorEngine: EditorEngine | ||
): Promise<string> { | ||
try { | ||
const sandbox = editorEngine.branches.getSandboxById(args.branchId); | ||
if (!sandbox) { | ||
throw new Error(`Sandbox not found for branch ID: ${args.branchId}`); | ||
} | ||
|
||
const context = editorEngine.chat.context.context; | ||
const imageContext = context.find((ctx) => | ||
ctx.type === MessageContextType.IMAGE && ctx.id === args.image_id | ||
); | ||
|
||
if (!imageContext || imageContext.type !== MessageContextType.IMAGE) { | ||
throw new Error(`No image found with ID: ${args.image_id}`); | ||
} | ||
|
||
const fullPath = await this.uploadImageToSandbox(imageContext, args, sandbox); | ||
await editorEngine.image.scanImages(); | ||
|
||
return `Image "${imageContext.displayName}" uploaded successfully to ${fullPath}`; | ||
} catch (error) { | ||
throw new Error(`Cannot upload image: ${error}`); | ||
} | ||
} | ||
|
||
getLabel(input?: z.infer<typeof UploadImageTool.parameters>): string { | ||
if (input?.image_id) { | ||
return 'Uploading image ' + input.image_id.substring(0, 20); | ||
} | ||
return 'Uploading image'; | ||
} | ||
|
||
private async uploadImageToSandbox( | ||
imageContext: Extract<MessageContext, { type: MessageContextType.IMAGE }>, | ||
args: z.infer<typeof UploadImageTool.parameters>, | ||
sandbox: any | ||
): Promise<string> { | ||
const mimeType = imageContext.mimeType; | ||
const extension = mime.getExtension(mimeType) || 'png'; | ||
const filename = args.filename ? `${args.filename}.${extension}` : `${uuidv4()}.${extension}`; | ||
const destinationPath = args.destination_path?.trim() || 'public/images'; | ||
const fullPath = `${destinationPath}/${filename}`; | ||
const base64Data = imageContext.content.replace(/^data:image\/[a-zA-Z0-9+.-]+;base64,/, ''); | ||
const binaryData = this.base64ToUint8Array(base64Data); | ||
await sandbox.writeBinaryFile(fullPath, binaryData); | ||
return fullPath; | ||
} | ||
|
||
private base64ToUint8Array(base64: string): Uint8Array { | ||
const binaryString = atob(base64); | ||
const bytes = new Uint8Array(binaryString.length); | ||
for (let i = 0; i < binaryString.length; i++) { | ||
bytes[i] = binaryString.charCodeAt(i); | ||
} | ||
return bytes; | ||
} | ||
} |
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,51 @@ | ||
import { Icons } from '@onlook/ui/icons'; | ||
import type { EditorEngine } from '@onlook/web-client/src/components/store/editor/engine'; | ||
import { MessageContextType } from '@onlook/models'; | ||
import { z } from 'zod'; | ||
import { ClientTool } from '../models/client'; | ||
|
||
export class ViewImageTool extends ClientTool { | ||
static readonly toolName = 'view_image'; | ||
static readonly description = "Retrieves and views an image from the chat context for analysis. Use this tool when the user asks you to analyze, describe, or work with an image they've attached. The image data will be returned so you can see and analyze its contents. This does NOT save the image to the project."; | ||
static readonly parameters = z.object({ | ||
image_id: z | ||
.string() | ||
.describe( | ||
'The unique ID of the image from the available images list', | ||
), | ||
}); | ||
static readonly icon = Icons.Image; | ||
|
||
async handle( | ||
args: z.infer<typeof ViewImageTool.parameters>, | ||
editorEngine: EditorEngine | ||
): Promise<{ image: { mimeType: string; data: string }; message: string }> { | ||
try { | ||
const context = editorEngine.chat.context.context; | ||
const imageContext = context.find((ctx) => | ||
ctx.type === MessageContextType.IMAGE && ctx.id === args.image_id | ||
); | ||
|
||
if (!imageContext || imageContext.type !== MessageContextType.IMAGE) { | ||
throw new Error(`No image found with ID: ${args.image_id}`); | ||
} | ||
|
||
return { | ||
image: { | ||
mimeType: imageContext.mimeType, | ||
data: imageContext.content, | ||
}, | ||
message: `Retrieved image "${imageContext.displayName}" for analysis.`, | ||
}; | ||
} catch (error) { | ||
throw new Error(`Cannot view image: ${error}`); | ||
} | ||
} | ||
|
||
getLabel(input?: z.infer<typeof ViewImageTool.parameters>): string { | ||
if (input?.image_id) { | ||
return 'Viewing image ' + input.image_id.substring(0, 20); | ||
} | ||
return 'Viewing image'; | ||
} | ||
} |
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
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.