Agents
Three decorations show what an agent did or is doing in a file. The editor only draws them. The app decides who the agent is, which lines it wrote and when to accept a suggestion.
import { AGENT_COLORS, type Editor, type EditorAttributionHover } from '@adecore/editor';
import { useCallback, useState } from 'react';
import { EditorFrame } from '../shared/editor-frame.tsx';
import { SAMPLE, useEditor } from '../shared/editor.ts';
const OPTIONS = { text: SAMPLE, language: 'typescript', theme: 'demo', line: 10, column: 2 } as const;
const SUGGESTION = '\n\nexport function orderCount(lines: OrderLine[]): number {\n return lines.length;\n}';
export default function AgentsDemo() {
const [hover, setHover] = useState<EditorAttributionHover | null>(null);
const setup = useCallback((editor: Editor) => {
editor.setAttributionMarks([
{ id: 'pricing', startLine: 4, endLine: 10, color: AGENT_COLORS[0] },
{ id: 'summary', startLine: 12, endLine: 15, color: AGENT_COLORS[1] }
]);
editor.setRemoteCursors([{ id: 'writer', position: { line: 13, character: 11 }, name: 'Writer', color: AGENT_COLORS[1] }]);
editor.setGhostText({
position: { line: 9, character: 1 },
text: SUGGESTION,
accessory: (container) => {
container.textContent = 'Tab';
}
});
const stops = [
editor.onAttributionHover(setHover),
// The editor never takes a suggestion itself: the host binds the key and inserts the text.
editor.onKeyDown((event) => {
const caret = editor.getCaret();
if (event.key !== 'Tab' || caret.line !== 9 || caret.character !== 1) {
return false;
}
return editor.applyEdits([{ range: { start: caret, end: caret }, text: SUGGESTION }]);
})
];
return () => stops.forEach((stop) => stop());
}, []);
const { host } = useEditor(OPTIONS, setup);
return (
<EditorFrame
host={host}
toolbar={
<span className="px-1 text-xs text-text-muted">
{hover === null
? 'Rest the pointer on a colored bar in the gutter, or press Tab to take the suggestion.'
: `The pointer is on the run "${hover.id}".`}
</span>
}
/>
);
}Colors
AGENT_COLORS lists six custom properties, --agent-1 through --agent-6, which editor.css defines for both themes, with --agent-ink for text on them. Pass the property name as the color so it follows the theme. Any CSS color works too. EditorMarkColor is the type of such a color, a string.
Attribution marks
setAttributionMarks(marks) draws a three-pixel gutter bar beside each run of lines an agent wrote. An EditorAttributionMark has an id, one-based inclusive startLine and endLine, and a color. On a line with a change mark, the bar replaces it; the scroll track still shows the change.
onAttributionHover(listener) reports an EditorAttributionHover, the id and the bar's rect under the pointer, and null when it leaves. AttributionCard is a card to show there.
The runs follow their text through edits until the app sets them again.
Remote cursors
setRemoteCursors(cursors) draws an agent's caret with a name label. Each EditorRemoteCursor has an id, position, name and color. The label sits above the caret, or below when there is no room. A remote cursor follows its text, ignores the pointer, hides on a folded line and never moves the editor's own caret.
Ghost text
setGhostText(ghost) draws an EditorGhostText suggestion after a position, such as a model's continuation of a line. The first line of text appears inline, faint and italic, after the caret. Further lines push the lines below down. accessory(container) fills an element after the first line, such as a key hint, and may return a function that runs once when that element goes. null removes the suggestion, and so does any text change.
The editor never accepts a suggestion. The app binds a key, checks the suggestion still fits and inserts it:
editor.setGhostText({ position, text: suggestion });
editor.onKeyDown((event) => {
if (event.key !== 'Tab') {
return false;
}
return editor.applyEdits([{ range: { start: position, end: position }, text: suggestion }]);
});acceptGhostWord in the key table names a key for taking one word. It has no default chord, and the editor does nothing with it.