Skip to content

Commit b5d9f62

Browse files
committed
Trying to add the terminal output to the chat context
1 parent ad24e03 commit b5d9f62

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

apps/studio/src/lib/editor/engine/chat/context.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
type HighlightMessageContext,
88
type ImageMessageContext,
99
type ProjectMessageContext,
10+
type TerminalMessageContext,
1011
} from '@onlook/models/chat';
1112
import type { DomElement } from '@onlook/models/element';
1213
import type { ParsedError } from '@onlook/utility';
@@ -44,7 +45,14 @@ export class ChatContext {
4445
const fileContext = await this.getFileContext(fileNames);
4546
const imageContext = await this.getImageContext();
4647
const projectContext = await this.getProjectContext();
47-
const context = [...fileContext, ...highlightedContext, ...imageContext, ...projectContext];
48+
const terminalContext = await this.getTerminalContext();
49+
const context = [
50+
...fileContext,
51+
...highlightedContext,
52+
...imageContext,
53+
...projectContext,
54+
...terminalContext,
55+
];
4856
return context;
4957
}
5058

@@ -72,6 +80,13 @@ export class ChatContext {
7280
return fileContext;
7381
}
7482

83+
private async getTerminalContext(): Promise<TerminalMessageContext[]> {
84+
const terminalContext = this.context.filter(
85+
(context) => context.type === MessageContextType.TERMINAL,
86+
);
87+
return terminalContext;
88+
}
89+
7590
private async getHighlightedContext(
7691
selected: DomElement[],
7792
fileNames: Set<string>,
@@ -111,6 +126,26 @@ export class ChatContext {
111126
this.context = [];
112127
}
113128

129+
async addTerminalContext() {
130+
const terminalContent = await this.getTerminalContent();
131+
if (terminalContent) {
132+
this.context = this.context.filter((ctx) => ctx.type !== MessageContextType.TERMINAL);
133+
this.context.push(terminalContent);
134+
}
135+
}
136+
137+
async getTerminalContent(): Promise<TerminalMessageContext | null> {
138+
const output = await this.editorEngine.getTerminalOutput();
139+
if (!output) {
140+
return null;
141+
}
142+
return {
143+
type: MessageContextType.TERMINAL,
144+
content: output,
145+
displayName: 'Terminal',
146+
};
147+
}
148+
114149
async addScreenshotContext() {
115150
const screenshot = await this.getScreenshotContext();
116151
if (screenshot) {

apps/studio/src/lib/editor/engine/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,18 @@ export class EditorEngine {
350350
};
351351
}
352352

353+
async getTerminalOutput(): Promise<string | null> {
354+
try {
355+
if (!this.projectsManager.runner) {
356+
return null;
357+
}
358+
const history = await this.projectsManager.runner.getHistory();
359+
return history || null;
360+
} catch (error) {
361+
console.error('Failed to get terminal output:', error);
362+
return null;
363+
}
364+
}
353365
canDeleteWindow() {
354366
return this.canvas.frames.length > 1;
355367
}

apps/studio/src/routes/editor/EditPanel/ChatTab/ChatInput.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,33 @@ export const ChatInput = observer(() => {
420420
</TooltipContent>
421421
</TooltipPortal>
422422
</Tooltip>
423+
<Tooltip>
424+
<TooltipTrigger asChild>
425+
<Button
426+
variant={'ghost'}
427+
size={'icon'}
428+
className="w-9 h-9 text-foreground-tertiary group hover:bg-transparent"
429+
onClick={() => {
430+
editorEngine.chat.context.addTerminalContext();
431+
}}
432+
disabled={disabled}
433+
>
434+
<Icons.Terminal
435+
className={cn(
436+
'w-5 h-5',
437+
disabled
438+
? 'text-foreground-tertiary'
439+
: 'group-hover:text-foreground',
440+
)}
441+
/>
442+
</Button>
443+
</TooltipTrigger>
444+
<TooltipPortal>
445+
<TooltipContent side="top" sideOffset={5}>
446+
{disabled ? 'Select an element to start' : 'Add terminal output'}
447+
</TooltipContent>
448+
</TooltipPortal>
449+
</Tooltip>
423450
<Button
424451
variant={'outline'}
425452
className="w-fit h-fit py-0.5 px-2.5 text-foreground-tertiary hidden"

packages/models/src/chat/message/context.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export enum MessageContextType {
44
IMAGE = 'image',
55
ERROR = 'error',
66
PROJECT = 'project',
7+
TERMINAL = 'terminal',
78
}
89

910
type BaseMessageContext = {
@@ -38,9 +39,16 @@ export type ProjectMessageContext = BaseMessageContext & {
3839
path: string;
3940
};
4041

42+
export type TerminalMessageContext = BaseMessageContext & {
43+
type: MessageContextType.TERMINAL;
44+
content: string;
45+
displayName: string;
46+
};
47+
4148
export type ChatMessageContext =
4249
| FileMessageContext
4350
| HighlightMessageContext
4451
| ImageMessageContext
4552
| ErrorMessageContext
46-
| ProjectMessageContext;
53+
| ProjectMessageContext
54+
| TerminalMessageContext;

0 commit comments

Comments
 (0)