Skip to content
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

Update project context with related files from templateNodes #1480

Open
wants to merge 2 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
79 changes: 77 additions & 2 deletions apps/studio/src/lib/editor/engine/chat/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
type HighlightMessageContext,
type ImageMessageContext,
type ProjectMessageContext,
type RelatedFileMessageContext,
} from '@onlook/models/chat';
import type { DomElement } from '@onlook/models/element';
import type { ParsedError } from '@onlook/utility';
Expand Down Expand Up @@ -142,20 +143,94 @@ export class ChatContext {
}
}

getProjectContext(): ProjectMessageContext[] {
async getProjectContext(): Promise<(ProjectMessageContext | RelatedFileMessageContext)[]> {
const folderPath = this.projectsManager.project?.folderPath;
if (!folderPath) {
return [];
}

return [
// Get selected elements
const selected = this.editorEngine.elements.selected;
if (selected.length === 0) {
return [
{
type: MessageContextType.PROJECT,
content: '',
displayName: 'Project',
path: folderPath,
},
];
}

// Get related files from templateNodes using oid or instanceId of children
const relatedFiles = new Set<string>();

// Process each selected element
for (const element of selected) {
// Get the layer node for the selected element
const layerNode = this.editorEngine.ast.mappings.getLayerNode(
element.webviewId,
element.domId,
);
if (!layerNode || !layerNode.children) {
continue;
}

// Process each child
for (const childId of layerNode.children) {
const childLayerNode = this.editorEngine.ast.mappings.getLayerNode(
element.webviewId,
childId,
);
if (!childLayerNode) {
continue;
}

// Try to get templateNode using oid
if (childLayerNode.oid) {
const templateNode = await this.editorEngine.ast.getTemplateNodeById(
childLayerNode.oid,
);
if (templateNode && templateNode.path) {
relatedFiles.add(templateNode.path);
}
}

// Try to get templateNode using instanceId
if (childLayerNode.instanceId) {
// For now, we'll just log that we found an instanceId
console.log(`Found child with instanceId: ${childLayerNode.instanceId}`);
// Additional logic could be added here to retrieve related files using instanceId
}
}
}

// Create project context with related files
const projectContext: (ProjectMessageContext | RelatedFileMessageContext)[] = [
{
type: MessageContextType.PROJECT,
content: '',
displayName: 'Project',
path: folderPath,
},
];

// Add related files to project context
for (const filePath of relatedFiles) {
const fileContent = await this.editorEngine.code.getFileContent(filePath, true);
if (fileContent === null) {
continue;
}

projectContext.push({
type: MessageContextType.RELATED_FILE,
content: fileContent,
displayName: `Related: ${filePath}`,
path: filePath,
});
}

return projectContext;
}

getMessageContext(errors: ParsedError[]): ErrorMessageContext[] {
Expand Down
11 changes: 11 additions & 0 deletions apps/studio/src/lib/editor/engine/element/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ export class ElementManager {
this.selectedElements = [];
}

getElementByDomId(domId: string): DomElement | null {
// Check all webviews for the element
for (const webviewId of this.editorEngine.webviews.getWebviewIds()) {
const layerNode = this.editorEngine.ast.mappings.getLayerNode(webviewId, domId);
if (layerNode) {
return layerNode;
}
}
return null;
}

async delete() {
const selected = this.selected;
if (selected.length === 0) {
Expand Down
9 changes: 8 additions & 1 deletion packages/models/src/chat/message/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export enum MessageContextType {
IMAGE = 'image',
ERROR = 'error',
PROJECT = 'project',
RELATED_FILE = 'relatedFile',
}

type BaseMessageContext = {
Expand Down Expand Up @@ -38,9 +39,15 @@ export type ProjectMessageContext = BaseMessageContext & {
path: string;
};

export type RelatedFileMessageContext = BaseMessageContext & {
type: MessageContextType.RELATED_FILE;
path: string;
};

export type ChatMessageContext =
| FileMessageContext
| HighlightMessageContext
| ImageMessageContext
| ErrorMessageContext
| ProjectMessageContext;
| ProjectMessageContext
| RelatedFileMessageContext;