Skip to content

Generate system instructions from chat history - #403

Open
gyanu2507 wants to merge 17 commits into
truefoundry:mainfrom
gyanu2507:feat/generate-instructions-from-chat
Open

Generate system instructions from chat history#403
gyanu2507 wants to merge 17 commits into
truefoundry:mainfrom
gyanu2507:feat/generate-instructions-from-chat

Conversation

@gyanu2507

@gyanu2507 gyanu2507 commented Aug 22, 2026

Copy link
Copy Markdown

Fixes #357

System instructions currently have to be written from scratch, even when the chat already made it obvious how the user wants the agent to behave.

This adds a From chat button on the session header. It drafts instructions from the transcript, you can edit them in a dialog, and nothing is saved until you apply. Inline chats can apply the draft to that session. Named/registry agents only get copy, so we don't mutate the saved agent.

Short chats return 422 before we call the model, so a "hey"/"hi" conversation doesn't turn into a generic helpful-assistant prompt.

New route: POST /api/v1/sessions/{session_id}/generate-instructions

It only returns a suggestion (instructions, current_instructions, sources). Saving still goes through the existing session update.

I didn't regenerate the SDK. The UI adapter hits the new path with client.fetch.


Note

Medium Risk
New session API reads full chat history and invokes the configured model; apply still mutates agent spec via existing session update, with guards for named agents.

Overview
Adds From chat in the session header to draft system instructions from the conversation, edit them in a modal, and save only on explicit Apply (mutable inline sessions). Chats bound to a saved registry agent get Copy only so the shared agent is not mutated.

Backend: New POST /api/v1/sessions/{session_id}/generate-instructions returns a suggestion (instructions, current_instructions, sources) without persisting. Transcript is built from session events with short-chat gates (minimum user text) so trivial threads return 422 before an LLM call; durable drafts use the session model via a bounded prompt.

UI: Optional generateInstructionsFromChat on the chat server (Harness adapter uses client.fetch on the new path). Button is hidden when the host does not implement the capability.

Reviewed by Cursor Bugbot for commit c8d15a0. Bugbot is set up for automated code reviews on this repo. Configure here.

@changeset-bot

changeset-bot Bot commented Aug 22, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: c8d15a0

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@truefoundry/trueforge Patch
@truefoundry/trueforge-ui Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@gyanu2507
gyanu2507 requested a review from thesujai as a code owner August 22, 2026 10:28
Comment thread packages/trueforge/src/runtime/generateSessionInstructions.ts Outdated
Comment thread packages/trueforge/src/runtime/chatInstructionTranscript.ts Outdated
@gyanu2507
gyanu2507 force-pushed the feat/generate-instructions-from-chat branch from b3f5f6a to 5e08c13 Compare August 22, 2026 15:40
Comment thread packages/trueforge-ui/src/atoms/GenerateInstructionsButton.tsx
Comment thread packages/trueforge-ui/src/atoms/GenerateInstructionsButton.tsx
Comment thread packages/trueforge/src/runtime/chatInstructionTranscript.ts Outdated
@gyanu2507
gyanu2507 force-pushed the feat/generate-instructions-from-chat branch from c7fe4cd to 9f22d41 Compare August 24, 2026 10:07
Comment thread packages/trueforge/src/runtime/chatInstructionTranscript.ts
gyanu2507 and others added 5 commits August 24, 2026 16:36
Users can promote a working conversation into an editable draft instead of writing the agent prompt from scratch, and nothing is saved until they apply or copy it.

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Walk a turn's user messages newest-first so the transcript reverse keeps them in order.
@gyanu2507
gyanu2507 force-pushed the feat/generate-instructions-from-chat branch from 9f22d41 to 0672b2f Compare August 24, 2026 11:06
Comment thread packages/trueforge/src/apis/sessions.ts
Gyanu and others added 2 commits August 24, 2026 22:08
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Comment thread packages/trueforge/src/apis/sessions.ts
Gyanu and others added 2 commits August 25, 2026 08:21
Co-authored-by: Cursor <cursoragent@cursor.com>
The only conflicts were the import lists in sessions.ts and
sessionRoutes.ts. Keep generate-instructions next to the new
get-or-create-by-external-id route.
Comment thread packages/trueforge/src/runtime/chatInstructionTranscript.ts
Keep the generate-from-chat slots next to the new library/sessions pages,
and keep both harness tests.
Comment thread packages/trueforge/src/apis/sessions.ts
Gyanu and others added 3 commits September 2, 2026 16:33
Keep GenerateInstructionsButton next to SaveAgent, hide Save when
agent config is open, and keep the new SaveAgentForm slots. The
harness server still exposes generateInstructionsFromChat on the
typed AgentChatServer from main.
@gyanu2507

Copy link
Copy Markdown
Author

Merged current main into this branch.

Conflicts were in the chat chrome and harness adapter. GenerateInstructionsButton stays next to Save. Save still hides while agent config is open (SaveAgentForm / SaveAgentFormFields from main). createHarnessChatServer keeps generateInstructionsFromChat on the typed server from main.

gyanu2507 and others added 2 commits September 4, 2026 09:26
Main dropped TENANT_ID and the global modelProviderStore. The handler now
resolves tenant, agent, and provider the same way as the other session
routes. Transcript clipping reserves user turns first so a long latest
assistant reply cannot drop earlier preferences.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want higher recall? High effort reviews run extra passes and find more bugs. A team admin can switch effort levels in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit d887691. Configure here.

}
pageToken = next;
}
return events;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Paging can 422 established chats

Medium Severity

loadInstructionTranscriptEvents stops after four newest pages of 100 events, and the short-chat gate only counts user text from that window. Tool-heavy turns can fill the window with non-user events, so an earlier preference-setting message never loads and a long chat returns 422 as if it were empty.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit d887691. Configure here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generate system instructions by leveraging chat history

2 participants