chore: import ovis-composer into repo with baseline tests
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
1 parent
edf09cf532
commit
c05e252d79
21 files changed
+4312
No files matched your search
@@ -0,0 +1,464 @@
|
||||
// view.js - the parts of the composer that need no GPUI and no app.
|
||||
//
|
||||
// Pure: no gpui-kit, no herdr, no IO, no clock. The view (main.js) asks its
|
||||
// questions here, so that Node can check the answers (tests/main.test.mjs)
|
||||
// without a running Herdr.
|
||||
//
|
||||
// The composer talks to one agent at a time - the focused one. Everything that
|
||||
// belongs to an agent (the draft, the queue, the attachments) is keyed by the
|
||||
// agent's address, so two Claude Code agents never share a draft.
|
||||
|
||||
import { drain, gateAfter } from "./delivery.js";
|
||||
import { LIMITS } from "./attachments.js";
|
||||
|
||||
/**
|
||||
* The agent the composer talks to.
|
||||
*
|
||||
* The focused terminal's agent first - that is the pane the person is
|
||||
* looking at. An agent block that is shown (a layout that draws one agent)
|
||||
* counts when no terminal has the focus. Without either there is nobody to
|
||||
* write to, and the composer stays inactive.
|
||||
*
|
||||
* @param {object[]} agents - the live agents of the app
|
||||
* @param {object|null} focused - the live focus of the window
|
||||
* @returns {object|null} the agent, or null
|
||||
*/
|
||||
export function targetAgent(agents, focused) {
|
||||
const list = Array.isArray(agents) ? agents : [];
|
||||
if (!focused || typeof focused !== "object") return null;
|
||||
|
||||
const byTerminal = list.find((agent) => agent && agent.address === focused.terminal);
|
||||
if (byTerminal) return byTerminal;
|
||||
|
||||
const byAgent = list.find((agent) => agent && agent.address === focused.agent);
|
||||
return byAgent ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The key a draft, a queue and a set of attachments belong to.
|
||||
*
|
||||
* The address of the agent, not the name of its program: two Claude Code
|
||||
* agents are two agents, and their drafts and queues must not mix.
|
||||
*
|
||||
* @param {object} agent
|
||||
* @returns {string}
|
||||
*/
|
||||
export function agentKey(agent) {
|
||||
if (!agent || typeof agent !== "object") return "";
|
||||
const address = typeof agent.address === "string" ? agent.address.trim() : "";
|
||||
if (address) return address;
|
||||
return agentName(agent);
|
||||
}
|
||||
|
||||
/**
|
||||
* The name of the agent's program ("claude-code") - what the catalog of the
|
||||
* built-in commands knows.
|
||||
*
|
||||
* @param {object} agent
|
||||
* @returns {string}
|
||||
*/
|
||||
export function agentName(agent) {
|
||||
if (!agent || typeof agent !== "object") return "default";
|
||||
const app = typeof agent.app === "string" ? agent.app.trim() : "";
|
||||
if (app) return app;
|
||||
const key = typeof agent.program?.key === "string" ? agent.program.key.trim() : "";
|
||||
return key || "default";
|
||||
}
|
||||
|
||||
/**
|
||||
* The status of an agent: "idle" when there is none to be had.
|
||||
*
|
||||
* @param {object} agent
|
||||
* @returns {string}
|
||||
*/
|
||||
export function agentStatus(agent) {
|
||||
const status = agent && typeof agent.status === "string" ? agent.status.trim() : "";
|
||||
return status || "idle";
|
||||
}
|
||||
|
||||
/**
|
||||
* The turn gate of one agent.
|
||||
*
|
||||
* @returns {{sawWorking:boolean, open:boolean}}
|
||||
*/
|
||||
export function createGate() {
|
||||
return { sawWorking: false, open: false };
|
||||
}
|
||||
|
||||
/**
|
||||
* May the next message go out?
|
||||
*
|
||||
* The gate opens after a whole turn was seen - the agent was working and came
|
||||
* back to idle. A send outside a turn (the "Wyślij teraz" button) opens it
|
||||
* outright, because it does not wait for a turn at all.
|
||||
*
|
||||
* @param {{sawWorking?:boolean, open?:boolean}} gate
|
||||
* @param {string} status
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function gatePasses(gate, status) {
|
||||
if (!gate || typeof gate !== "object") return false;
|
||||
if (gate.open === true) return true;
|
||||
return gateAfter(status, gate.sawWorking === true);
|
||||
}
|
||||
|
||||
/**
|
||||
* The gate of an agent with the turn step aside.
|
||||
*
|
||||
* @param {{sawWorking?:boolean, open?:boolean}} gate
|
||||
* @returns {{sawWorking:boolean, open:boolean}}
|
||||
*/
|
||||
export function openGate(gate) {
|
||||
return { ...(gate ?? createGate()), open: true };
|
||||
}
|
||||
|
||||
/**
|
||||
* The gate closes again: the next message waits for the next turn.
|
||||
*
|
||||
* @param {{sawWorking?:boolean, open?:boolean}} gate
|
||||
* @returns {{sawWorking:boolean, open:boolean}}
|
||||
*/
|
||||
export function closeGate(gate) {
|
||||
return { ...(gate ?? createGate()), sawWorking: false, open: false };
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this the Enter that sends the message?
|
||||
*
|
||||
* Shift+Enter is a new line. The secondary Enter - the one an IME sends when
|
||||
* it confirms a composition - does not send either (Ovis: "IME Enter ignored").
|
||||
*
|
||||
* @param {{shift?:boolean, secondary?:boolean}} event
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function isPlainEnter(event) {
|
||||
if (!event) return false;
|
||||
if (event.shift === true) return false;
|
||||
if (event.secondary === true) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* The caret one line up or down.
|
||||
*
|
||||
* The field of this app version (gpui-base in herdr-app 0.1.16) does not move
|
||||
* the cursor up and down by itself, so the composer does it: the caret goes to
|
||||
* the same column of the line above (or below) and never leaves the text.
|
||||
*
|
||||
* @param {string} text
|
||||
* @param {number} caret
|
||||
* @param {number} step - negative for up, positive for down
|
||||
* @returns {number} the new caret
|
||||
*/
|
||||
export function moveCaret(text, caret, step) {
|
||||
const source = typeof text === "string" ? text : "";
|
||||
const at = Number.isInteger(caret) ? caret : 0;
|
||||
const here = Math.max(0, Math.min(at, source.length));
|
||||
|
||||
const lineStart = source.lastIndexOf("\n", here - 1) + 1;
|
||||
const column = here - lineStart;
|
||||
|
||||
if (step < 0) {
|
||||
if (lineStart === 0) return here; // there is no line above
|
||||
const aboveEnd = lineStart - 1; // the newline of the line above
|
||||
const aboveStart = source.lastIndexOf("\n", aboveEnd - 1) + 1;
|
||||
return aboveStart + Math.min(column, aboveEnd - aboveStart);
|
||||
}
|
||||
|
||||
const lineEnd = source.indexOf("\n", here);
|
||||
if (lineEnd < 0) return here; // there is no line below
|
||||
const belowStart = lineEnd + 1;
|
||||
const nextBreak = source.indexOf("\n", belowStart);
|
||||
const belowEnd = nextBreak < 0 ? source.length : nextBreak;
|
||||
return belowStart + Math.min(column, belowEnd - belowStart);
|
||||
}
|
||||
|
||||
/**
|
||||
* What a key of the menu does.
|
||||
*
|
||||
* "none" — the composer does not touch the key: another field of the app has
|
||||
* the keyboard, or there is no menu to move.
|
||||
* "menu" — the arrows move the menu.
|
||||
* "accept" — Tab takes the picked row.
|
||||
*
|
||||
* @param {boolean} hasKeyboard - does the composer's own field have the keyboard?
|
||||
* @param {object|null} menu
|
||||
* @param {string} key - "up", "down" or "tab"
|
||||
* @returns {"none"|"menu"|"accept"}
|
||||
*/
|
||||
export function menuAction(hasKeyboard, menu, key) {
|
||||
if (!hasKeyboard) return "none";
|
||||
if (!menu || !(menu.items ?? []).length) return "none";
|
||||
if (key === "up" || key === "down") return "menu";
|
||||
if (key === "tab") return "accept";
|
||||
return "none";
|
||||
}
|
||||
|
||||
/**
|
||||
* The JSON of a `--json` payload, or null when it is not JSON at all.
|
||||
*
|
||||
* @param {string} text
|
||||
* @returns {object|null}
|
||||
*/
|
||||
export function parseJson(text) {
|
||||
if (typeof text !== "string" || text.trim() === "") return null;
|
||||
try {
|
||||
const value = JSON.parse(text);
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* May the composer count on this result of `hc`?
|
||||
*
|
||||
* `hc` exits 0 even when it fails, so the payload decides - and a payload
|
||||
* that is not JSON at all is an unknown result, not a success (the same rule
|
||||
* the delivery layer follows).
|
||||
*
|
||||
* @param {{code?:number, stdout?:string}|null} result
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function hcOk(result) {
|
||||
if (!result || result.code !== 0) return false;
|
||||
const payload = parseJson(result.stdout);
|
||||
return payload !== null && !payload.error;
|
||||
}
|
||||
|
||||
/**
|
||||
* What a send whose hook failed means.
|
||||
*
|
||||
* The hook of the delivery reports the states of the queue; a failure in it
|
||||
* after the message went on the wire must not send the message again. The
|
||||
* entry left the queue, so it is sent; an entry that is still there is
|
||||
* uncertain.
|
||||
*
|
||||
* @param {object[]} queue - the queue of the agent
|
||||
* @param {string} id
|
||||
* @returns {"sent"|"uncertain"}
|
||||
*/
|
||||
export function failedHookOutcome(queue, id) {
|
||||
const still = (Array.isArray(queue) ? queue : []).some((item) => item.id === id);
|
||||
return still ? "uncertain" : "sent";
|
||||
}
|
||||
|
||||
/**
|
||||
* The queue goes out - one message after another, while the gate is open.
|
||||
*
|
||||
* `drain` sends one entry and leaves the gate as it was; the loop asks for the
|
||||
* next one until the queue is empty or the gate closes. The caller keeps the
|
||||
* "one drain in flight" flag for the whole loop: two drains on the same state
|
||||
* would send the same message twice.
|
||||
*
|
||||
* @param {object} ctx - the ctx of `delivery.drain`
|
||||
* @param {object} state
|
||||
* @returns {Promise<{state:object, sent:number}>}
|
||||
*/
|
||||
export async function drainAll(ctx, state) {
|
||||
let current = state;
|
||||
let sent = 0;
|
||||
|
||||
for (;;) {
|
||||
const result = await drain(ctx, current);
|
||||
current = result.state;
|
||||
if (!result.sent) break;
|
||||
sent += 1;
|
||||
}
|
||||
|
||||
return { state: current, sent };
|
||||
}
|
||||
|
||||
/**
|
||||
* What the composer says about an agent's status.
|
||||
*
|
||||
* @param {string} status
|
||||
* @returns {string}
|
||||
*/
|
||||
export function statusText(status) {
|
||||
switch (status) {
|
||||
case "working":
|
||||
return "pracuje";
|
||||
case "blocked":
|
||||
return "czeka na Ciebie";
|
||||
case "done":
|
||||
return "skończył";
|
||||
default:
|
||||
return "gotowy";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The label of a queue row.
|
||||
*
|
||||
* @param {{state?:string}} item
|
||||
* @returns {string}
|
||||
*/
|
||||
export function queueLabel(item) {
|
||||
switch (item?.state) {
|
||||
case "queued":
|
||||
return "Następna tura";
|
||||
case "held":
|
||||
return "Do wysłania teraz";
|
||||
case "sending":
|
||||
return "Wysyłanie…";
|
||||
case "uncertain":
|
||||
return "Niepotwierdzone";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The text and the caret after a suggestion is accepted.
|
||||
*
|
||||
* The token at the caret is replaced by what the item inserts; the caret lands
|
||||
* behind it, so the next word continues where the person was typing.
|
||||
*
|
||||
* @param {string} text
|
||||
* @param {number} caret
|
||||
* @param {{start?:number}} token - the trigger of the suggestion
|
||||
* @param {string} insert
|
||||
* @returns {{value:string, caret:number}}
|
||||
*/
|
||||
export function applySuggestion(text, caret, token, insert) {
|
||||
const source = typeof text === "string" ? text : "";
|
||||
const at = Number.isInteger(caret) ? caret : 0;
|
||||
const start = Number.isInteger(token?.start) ? token.start : at;
|
||||
const value = source.slice(0, start) + String(insert ?? "") + source.slice(at);
|
||||
return { value, caret: start + String(insert ?? "").length };
|
||||
}
|
||||
|
||||
/**
|
||||
* The newline that Enter put into the field.
|
||||
*
|
||||
* The composer sends on Enter, and the field puts a newline in before the
|
||||
* submit arrives. The message is the text as it was before that newline - and
|
||||
* the caret is where the newline went in.
|
||||
*
|
||||
* @param {string} previous - the text before the change
|
||||
* @param {string} next - the text after the change
|
||||
* @returns {{index:number}|null} where the newline went in, or null
|
||||
*/
|
||||
export function insertedNewline(previous, next) {
|
||||
if (typeof previous !== "string" || typeof next !== "string") return null;
|
||||
if (next.length !== previous.length + 1) return null;
|
||||
|
||||
for (let index = 0; index < next.length; index += 1) {
|
||||
if (next[index] !== "\n") continue;
|
||||
if (next.slice(0, index) + next.slice(index + 1) === previous) return { index };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The mentions of the files, at the start of the message.
|
||||
*
|
||||
* A leading command keeps the first place - the mentions follow it.
|
||||
*
|
||||
* @param {string} text
|
||||
* @param {string[]} tokens - the mentions ("@src/a.rs")
|
||||
* @returns {{value:string, caret:number}}
|
||||
*/
|
||||
export function insertMentions(text, tokens) {
|
||||
const source = typeof text === "string" ? text : "";
|
||||
const list = (Array.isArray(tokens) ? tokens : []).filter(Boolean);
|
||||
if (list.length === 0) return { value: source, caret: source.length };
|
||||
|
||||
const command = /^\/[^\s]*\s*/.exec(source);
|
||||
const at = command ? command[0].length : 0;
|
||||
const mentions = list.map((token) => `${token} `).join("");
|
||||
|
||||
return {
|
||||
value: source.slice(0, at) + mentions + source.slice(at),
|
||||
caret: at + mentions.length,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The items of the catalog, grouped by their group.
|
||||
*
|
||||
* @param {object[]} items
|
||||
* @returns {Record<string, object[]>}
|
||||
*/
|
||||
export function groupItems(items) {
|
||||
const groups = {};
|
||||
for (const item of Array.isArray(items) ? items : []) {
|
||||
if (!item) continue;
|
||||
const name = typeof item.group === "string" && item.group !== "" ? item.group : "";
|
||||
if (!groups[name]) groups[name] = [];
|
||||
groups[name].push(item);
|
||||
}
|
||||
return groups;
|
||||
}
|
||||
|
||||
/**
|
||||
* The folder and the branch of the pane the composer talks to.
|
||||
*
|
||||
* @param {object[]} panes
|
||||
* @param {string} terminal
|
||||
* @returns {{cwd:string|null, branch:string|null}}
|
||||
*/
|
||||
export function paneContext(panes, terminal) {
|
||||
const list = Array.isArray(panes) ? panes : [];
|
||||
const pane = list.find((entry) => entry && entry.terminal === terminal);
|
||||
return { cwd: pane?.cwd ?? null, branch: pane?.branch ?? null };
|
||||
}
|
||||
|
||||
/**
|
||||
* The last parts of a path - the folder the person knows by heart.
|
||||
*
|
||||
* @param {string} path
|
||||
* @returns {string}
|
||||
*/
|
||||
export function shortPath(path) {
|
||||
const parts = String(path ?? "")
|
||||
.split(/[\\/]/)
|
||||
.filter(Boolean);
|
||||
if (parts.length <= 2) return parts.join("/");
|
||||
return parts.slice(-2).join("/");
|
||||
}
|
||||
|
||||
/**
|
||||
* A store that never writes back a list it failed to read.
|
||||
*
|
||||
* The reader of storage.js swallows a failing read and answers with an empty
|
||||
* list - writing that list back would wipe what is really there. The store
|
||||
* remembers which keys failed and refuses to write them.
|
||||
*
|
||||
* @param {{get:(key:string)=>unknown, set:(key:string, value:unknown)=>unknown}} raw
|
||||
* @returns {{get:(key:string)=>unknown, set:(key:string, value:unknown)=>void, failed:(key:string)=>boolean}}
|
||||
*/
|
||||
export function guardedStore(raw) {
|
||||
const failed = new Set();
|
||||
|
||||
return {
|
||||
get(key) {
|
||||
try {
|
||||
const value = raw.get(key);
|
||||
return value ?? null;
|
||||
} catch {
|
||||
failed.add(key);
|
||||
return null;
|
||||
}
|
||||
},
|
||||
set(key, value) {
|
||||
if (failed.has(key)) return;
|
||||
raw.set(key, value);
|
||||
},
|
||||
failed(key) {
|
||||
return failed.has(key);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* How many characters the text has over the limit (0 when it fits).
|
||||
*
|
||||
* @param {string} text
|
||||
* @returns {number}
|
||||
*/
|
||||
export function overLimit(text) {
|
||||
const length = typeof text === "string" ? text.length : 0;
|
||||
return Math.max(0, length - LIMITS.messageChars);
|
||||
}
|
||||
Reference in new issue
Block a user