Skip to content

Commit ef1192f

Browse files
added agent instructions drawer (#599)
* added agent instructions drawer * refactor: agent instructions handling in SaveAgentButton and AgentConfigEditors --------- Co-authored-by: Govinda Vashishtha <57435703+govindavashishtha@users.noreply.github.com>
1 parent bc11131 commit ef1192f

12 files changed

Lines changed: 451 additions & 54 deletions

‎.changeset/agent-config-chrome.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"@truefoundry/trueforge-ui": minor
33
---
44

5-
Keep agent configuration visible on the left in full-width builder layouts, make mobile config overlays closable, save the current instruction draft, align builder chrome, and preserve open widgets across chat runtime changes.
5+
Keep agent configuration visible on the left in full-width builder layouts, add a responsive instructions and initial-messages drawer with explicit save, make mobile config overlays closable, save the current instruction draft, align builder chrome, and preserve open widgets across chat runtime changes.

‎packages/trueforge-ui/src/atoms/draft/AgentConfigEditors.tsx‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import { useEffect, useMemo, useState } from 'react';
44

55
import type { AgentSkill, AgentSpec, ConnectorState, McpToolSelection, ModelSelection } from '../../server/types.js';
66
import { useSlot } from '../../theme/SlotsProvider.js';
7+
import type { AgentInstructionsDraft } from './AgentInstructionsDrawer.js';
8+
import { initialUserMessagesFromSpec, withInitialUserMessages } from './agentConfigMessages.js';
79
import { editableMountsFromSpec } from './agentConfigMounts.js';
810
import { connectorsWithSelectedStubs } from './mcpConnectorStubs.js';
911

10-
export type AgentConfigEditor = 'model' | 'model-settings' | 'runtime' | 'mcp' | 'skills';
12+
export type AgentConfigEditor = 'instructions' | 'model' | 'model-settings' | 'runtime' | 'mcp' | 'skills';
1113

1214
export type AgentConfigEditorsProps = {
1315
editor: AgentConfigEditor | null;
@@ -19,6 +21,8 @@ export type AgentConfigEditorsProps = {
1921
error: string | null;
2022
skillsDisabled?: boolean;
2123
sandboxAvailable?: boolean;
24+
instructions?: string;
25+
onInstructionsSave?: (draft: AgentInstructionsDraft) => void;
2226
loadMcpTools?: (connectorId: string) => Promise<McpToolSelection[]>;
2327
onRefreshConnectors?: () => Promise<void>;
2428
onChange: (spec: AgentSpec) => void;
@@ -35,6 +39,8 @@ export function AgentConfigEditors({
3539
error,
3640
skillsDisabled = false,
3741
sandboxAvailable = false,
42+
instructions,
43+
onInstructionsSave,
3844
loadMcpTools,
3945
onRefreshConnectors,
4046
onChange,
@@ -43,6 +49,7 @@ export function AgentConfigEditors({
4349
const AgentModelConfigModal = useSlot('AgentModelConfigModal');
4450
const AgentResourceConfigModal = useSlot('AgentResourceConfigModal');
4551
const AgentRuntimeConfigModal = useSlot('AgentRuntimeConfigModal');
52+
const AgentInstructionsDrawer = useSlot('AgentInstructionsDrawer');
4653
const [query, setQuery] = useState('');
4754
const [activeConnectorId, setActiveConnectorId] = useState<string | null>(null);
4855
const [tools, setTools] = useState<McpToolSelection[]>([]);
@@ -96,9 +103,30 @@ export function AgentConfigEditors({
96103

97104
const modelEditor = editor === 'model' || editor === 'model-settings' ? editor : null;
98105
const resourceEditor = editor === 'mcp' || editor === 'skills' ? editor : null;
106+
const saveInstructions = (draft: AgentInstructionsDraft) => {
107+
if (onInstructionsSave !== undefined) {
108+
onInstructionsSave(draft);
109+
return;
110+
}
111+
onChange(
112+
withInitialUserMessages({
113+
spec: { ...spec, instructions: draft.instructions || undefined },
114+
messages: draft.messages,
115+
}),
116+
);
117+
};
99118

100119
return (
101120
<>
121+
{editor === 'instructions' ? (
122+
<AgentInstructionsDrawer
123+
open
124+
instructions={instructions ?? spec.instructions ?? ''}
125+
messages={initialUserMessagesFromSpec(spec)}
126+
onSave={saveInstructions}
127+
onClose={close}
128+
/>
129+
) : null}
102130
{modelEditor ? (
103131
<AgentModelConfigModal
104132
editor={modelEditor}

‎packages/trueforge-ui/src/atoms/draft/AgentConfigPanel.tsx‎

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
'use client';
22

3-
import { useId, type ReactNode } from 'react';
3+
import type { ReactNode } from 'react';
44

55
import { Icon } from '../../icons/Icon.js';
66
import type { AgentSpec, ModelSelection } from '../../server/types.js';
77
import { useSlot } from '../../theme/SlotsProvider.js';
88
import { auiButtonClass } from '../lib/buttonClasses.js';
99
import { cn } from '../lib/cn.js';
10-
import { auiInputClass } from '../lib/inputClasses.js';
1110
import { Tooltip } from '../primitives/Tooltip.js';
1211
import type { AgentConfigEditor } from './AgentConfigEditors.js';
12+
import { initialUserMessagesFromSpec } from './agentConfigMessages.js';
1313
import {
1414
editableMountsFromSpec,
1515
enabledToolsFromMount,
@@ -26,8 +26,6 @@ export type AgentConfigPanelProps = {
2626
model?: ModelSelection;
2727
skillsAvailable: boolean;
2828
instructions: string;
29-
onInstructionsChange: (value: string) => void;
30-
onInstructionsBlur: () => void;
3129
onOpenEditor: (editor: AgentConfigEditor) => void;
3230
onChange?: (spec: AgentSpec) => void;
3331
onClose?: () => void;
@@ -150,18 +148,17 @@ export function AgentConfigPanel({
150148
model,
151149
skillsAvailable,
152150
instructions,
153-
onInstructionsChange,
154-
onInstructionsBlur,
155151
onOpenEditor,
156152
onChange,
157153
onClose,
158154
}: AgentConfigPanelProps) {
159-
const instructionsId = useId();
160155
const Section = useSlot('AgentConfigSection');
161156
const mcp = editableMountsFromSpec(spec.mcpServers);
162157
const skills = editableMountsFromSpec(spec.skills);
163158
const modelParams = modelParamSummary(spec.model.params);
164159
const runtimeConfig = runtimeConfigSummary(spec.config);
160+
const instructionPreview = instructions.trim();
161+
const userMessageCount = initialUserMessagesFromSpec(spec).length;
165162
const modelInfo = [
166163
model?.properties.contextLength === undefined ? null : formatTokens(model.properties.contextLength),
167164
].filter((value): value is string => value !== null);
@@ -247,18 +244,27 @@ export function AgentConfigPanel({
247244
</Section>
248245

249246
<Section title="Instructions" description="Define the agent's role, goals, and behavior.">
250-
<label htmlFor={instructionsId} className="sr-only">
251-
Agent instructions
252-
</label>
253-
<textarea
254-
id={instructionsId}
255-
value={instructions}
256-
rows={5}
257-
placeholder="Enter detailed instructions for your agent…"
258-
className={auiInputClass('resize-y py-2')}
259-
onChange={event => onInstructionsChange(event.target.value)}
260-
onBlur={onInstructionsBlur}
261-
/>
247+
<button
248+
type="button"
249+
aria-label="Edit Instructions"
250+
className="border-border hover:bg-ghost-button-hover flex w-full items-center gap-3 rounded-lg border p-3 text-left transition-colors"
251+
onClick={() => onOpenEditor('instructions')}
252+
>
253+
<div className="min-w-0 flex-1">
254+
<p
255+
className={cn(
256+
'line-clamp-3 whitespace-pre-wrap text-sm',
257+
instructionPreview ? 'text-text-primary' : 'text-text-secondary',
258+
)}
259+
>
260+
{instructionPreview || 'No instructions added.'}
261+
</p>
262+
<p className="text-text-secondary mt-2 text-xs">
263+
{userMessageCount} user {userMessageCount === 1 ? 'message' : 'messages'}
264+
</p>
265+
</div>
266+
<Icon name="chevron-right" className="text-text-secondary size-4 shrink-0" />
267+
</button>
262268
</Section>
263269

264270
<Section
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
'use client';
2+
3+
import type { TrueForgeApi } from '@truefoundry/trueforge-sdk';
4+
import { useId, useRef, useState } from 'react';
5+
6+
import { Icon } from '../../icons/Icon.js';
7+
import { auiButtonClass } from '../lib/buttonClasses.js';
8+
import { auiInputClass } from '../lib/inputClasses.js';
9+
import { SideDrawer } from '../primitives/SideDrawer.js';
10+
11+
type UserMessageDraft = {
12+
id: number;
13+
content: string;
14+
};
15+
16+
export type AgentInstructionsDraft = {
17+
instructions: string;
18+
messages: TrueForgeApi.InitialUserMessage[];
19+
};
20+
21+
export type AgentInstructionsDrawerProps = {
22+
open: boolean;
23+
instructions: string;
24+
messages?: TrueForgeApi.InitialUserMessage[];
25+
onSave: (draft: AgentInstructionsDraft) => void;
26+
onClose: () => void;
27+
};
28+
29+
function persistedMessages(rows: UserMessageDraft[]): TrueForgeApi.InitialUserMessage[] {
30+
return rows.filter(row => row.content.trim().length > 0).map(row => ({ type: 'user.message', content: row.content }));
31+
}
32+
33+
export function AgentInstructionsDrawer({
34+
open,
35+
instructions,
36+
messages = [],
37+
onSave,
38+
onClose,
39+
}: AgentInstructionsDrawerProps) {
40+
const fieldId = useId();
41+
const nextMessageId = useRef(messages.length);
42+
const [instructionDraft, setInstructionDraft] = useState(instructions);
43+
const [messageDrafts, setMessageDrafts] = useState<UserMessageDraft[]>(() =>
44+
messages.map((message, id) => ({ id, content: message.content })),
45+
);
46+
47+
const updateMessageDrafts = (next: UserMessageDraft[]) => {
48+
setMessageDrafts(next);
49+
};
50+
51+
const addUserMessage = () => {
52+
const next = [...messageDrafts, { id: nextMessageId.current, content: '' }];
53+
nextMessageId.current += 1;
54+
setMessageDrafts(next);
55+
};
56+
57+
const updateUserMessage = ({ id, content }: UserMessageDraft) => {
58+
updateMessageDrafts(messageDrafts.map(message => (message.id === id ? { id, content } : message)));
59+
};
60+
61+
const removeUserMessage = (id: number) => {
62+
updateMessageDrafts(messageDrafts.filter(message => message.id !== id));
63+
};
64+
65+
const save = () => {
66+
onSave({ instructions: instructionDraft, messages: persistedMessages(messageDrafts) });
67+
onClose();
68+
};
69+
70+
return (
71+
<SideDrawer
72+
open={open}
73+
onOpenChange={nextOpen => !nextOpen && onClose()}
74+
title="Instructions"
75+
description="Define the agent behavior and messages added at the start of every session."
76+
anchor="right"
77+
size="lg"
78+
aria-label="Edit Instructions"
79+
footer={
80+
<div className="flex justify-end">
81+
<button type="button" className={auiButtonClass({ variant: 'default' })} onClick={save}>
82+
Save
83+
</button>
84+
</div>
85+
}
86+
>
87+
<div className="flex min-h-0 flex-1 flex-col gap-6 p-5">
88+
<section>
89+
<label htmlFor={`${fieldId}-instructions`} className="text-text-primary text-sm font-semibold">
90+
Agent instructions
91+
</label>
92+
<p className="text-text-secondary mt-1 text-xs">Describe the agent&apos;s role, goals, and constraints.</p>
93+
<textarea
94+
id={`${fieldId}-instructions`}
95+
value={instructionDraft}
96+
rows={14}
97+
placeholder="Enter detailed instructions for your agent…"
98+
className={auiInputClass('mt-3 min-h-64 resize-y py-3')}
99+
onChange={event => setInstructionDraft(event.target.value)}
100+
/>
101+
</section>
102+
103+
<section className="border-border border-t pt-5">
104+
<div className="flex items-center justify-between gap-3">
105+
<div>
106+
<h3 className="text-text-primary text-sm font-semibold">User Messages</h3>
107+
<p className="text-text-secondary mt-1 text-xs">Add initial messages to every new agent session.</p>
108+
</div>
109+
<button
110+
type="button"
111+
className={auiButtonClass({ variant: 'outline', size: 'sm' })}
112+
onClick={addUserMessage}
113+
>
114+
<Icon name="plus" className="size-3.5" />
115+
Add User Message
116+
</button>
117+
</div>
118+
119+
{messageDrafts.length > 0 ? (
120+
<div className="mt-4 flex flex-col gap-4">
121+
{messageDrafts.map((message, index) => {
122+
const messageId = `${fieldId}-user-message-${message.id}`;
123+
return (
124+
<div key={message.id} className="border-border rounded-lg border p-3">
125+
<div className="mb-2 flex items-center gap-2">
126+
<Icon name="user" className="text-text-secondary size-4" />
127+
<label htmlFor={messageId} className="text-text-primary flex-1 text-sm font-medium">
128+
User Message {index + 1}
129+
</label>
130+
<button
131+
type="button"
132+
aria-label={`Remove User Message ${index + 1}`}
133+
className={auiButtonClass({ variant: 'ghost', size: 'icon', className: 'size-7' })}
134+
onClick={() => removeUserMessage(message.id)}
135+
>
136+
<Icon name="trash" className="size-3.5" />
137+
</button>
138+
</div>
139+
<textarea
140+
id={messageId}
141+
value={message.content}
142+
rows={4}
143+
placeholder="Type message content…"
144+
className={auiInputClass('min-h-24 resize-y py-2')}
145+
onChange={event => updateUserMessage({ id: message.id, content: event.target.value })}
146+
/>
147+
</div>
148+
);
149+
})}
150+
</div>
151+
) : (
152+
<p className="text-text-secondary mt-4 text-sm">No initial user messages.</p>
153+
)}
154+
</section>
155+
</div>
156+
</SideDrawer>
157+
);
158+
}
159+
160+
declare module '../../theme/SlotsProvider.js' {
161+
interface AtomSlots {
162+
AgentInstructionsDrawer: typeof AgentInstructionsDrawer;
163+
}
164+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { TrueForgeApi } from '@truefoundry/trueforge-sdk';
2+
3+
import type { AgentSpec } from '../../server/types.js';
4+
5+
function isInitialUserMessage(value: unknown): value is TrueForgeApi.InitialUserMessage {
6+
if (typeof value !== 'object' || value === null) return false;
7+
return Reflect.get(value, 'type') === 'user.message' && typeof Reflect.get(value, 'content') === 'string';
8+
}
9+
10+
// Adapts the SDK-owned messages field until the UI runtime contract exposes it
11+
export function initialUserMessagesFromSpec(spec: AgentSpec): TrueForgeApi.InitialUserMessage[] {
12+
const value: unknown = Reflect.get(spec, 'messages');
13+
return Array.isArray(value) ? value.filter(isInitialUserMessage) : [];
14+
}
15+
16+
export function withInitialUserMessages({
17+
spec,
18+
messages,
19+
}: {
20+
spec: AgentSpec;
21+
messages: TrueForgeApi.InitialUserMessage[];
22+
}): AgentSpec {
23+
const next = { ...spec, messages: messages.length > 0 ? messages : undefined };
24+
return next;
25+
}

0 commit comments

Comments
 (0)