Skip to content
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
6 changes: 6 additions & 0 deletions experiment/app/api/writing-support/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export async function POST(req: Request) {

const { beforeCursor, selectedText, afterCursor } = body.editorState;
const context = (body.context as keyof typeof prompts) || 'proposal_advice';
const writingDescription = body.writingDescription;
const promptTemplate = prompts[context];

try {
Expand All @@ -77,6 +78,11 @@ export async function POST(req: Request) {
const afterCursorTrim = afterCursor.slice(0, 100);

let fullPrompt = promptTemplate;

if (writingDescription) {
fullPrompt += `\n\n# Writer's Intent\n\nThe writer has described what they are trying to write: "${writingDescription}"`;
}

fullPrompt += `\n\n# Writer's Document So Far\n\n<document>\n${documentText}</document>\n\n`;

if (selectedText === '') {
Expand Down
21 changes: 19 additions & 2 deletions experiment/components/AIPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export default function AIPanel({
const [isLoading, setIsLoading] = useState(false);
const [savedItems, setSavedItems] = useState<SavedItem[]>([]);
const [errorMsg, setErrorMsg] = useState('');
const [writingDescription, setWritingDescription] = useState('');
const docContextRef = useRef<TextEditorState | null>(null);
const studyParams = useAtomValue(studyParamsAtom);
const studyCondition = useAtomValue(studyConditionAtom);
Expand Down Expand Up @@ -161,6 +162,7 @@ export default function AIPanel({
event: `aiRequest:${modeToUse}`,
extra_data: {
isAutoRefresh,
writingDescription: writingDescription.trim() || undefined,
// Don't log document content right now; we'll log it with the response
},
});
Expand All @@ -174,6 +176,7 @@ export default function AIPanel({
body: JSON.stringify({
editorState,
context: modeToUse,
...(writingDescription.trim() && { writingDescription: writingDescription.trim() }),
}),
signal: AbortSignal.timeout(API_TIMEOUT_MS),
});
Expand Down Expand Up @@ -207,7 +210,7 @@ export default function AIPanel({
await log({
username: studyParams.username,
event: `aiResponse:${modeToUse}`,
extra_data: { isAutoRefresh, generation, editorState },
extra_data: { isAutoRefresh, generation, editorState, writingDescription: writingDescription.trim() || undefined },
});
}
}
Expand All @@ -230,7 +233,7 @@ export default function AIPanel({
setIsLoading(false);
}
},
[writingAreaRef, save, mode, isStudyMode, studyParams]
[writingAreaRef, save, mode, isStudyMode, studyParams, writingDescription]
);

// Auto-refresh logic for study mode
Expand Down Expand Up @@ -289,6 +292,20 @@ export default function AIPanel({
<div className="h-full p-4 text-sm text-gray-700 flex flex-col overflow-hidden">
<h3 className="text-sm font-bold text-gray-900 mb-3">AI Writing Assistant</h3>

<div className="mb-3">
<label htmlFor="writing-description" className="block text-xs font-semibold text-gray-600 mb-1">
What are you trying to write?
</label>
<textarea
id="writing-description"
value={writingDescription}
onChange={(e) => setWritingDescription(e.target.value)}
placeholder='e.g., "telling him we need to move the panel we&#39;d scheduled for tomorrow"'
rows={2}
className="w-full text-xs text-gray-800 border border-gray-300 rounded px-2 py-1.5 resize-none focus:outline-none focus:ring-1 focus:ring-blue-400 focus:border-blue-400 placeholder:text-gray-400"
/>
</div>

{!isStudyMode && (
<div className="flex gap-1.5 mb-3 flex-wrap">
{modes.map((mode) => (
Expand Down
1 change: 1 addition & 0 deletions experiment/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface TextEditorState {
export interface WritingSupportRequest {
editorState: TextEditorState;
context?: string;
writingDescription?: string;
}

export interface WritingSupportResponse {
Expand Down