- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/move.ts?format=json
88 linesFirst import 92% · codex-capacity-chair 8%Last change codex-capacity-chair in version 163, 3 versions changed it
1// Move a file in the agent's draft. Writes go through MoveHost; today the
2// host is Project.
4import type { EditInput, Hunk, MoveInput, ReadView } from "../../src/engine/index.ts";
5import { checkPath } from "./path-rules.ts";
6import { pathKey } from "./pathkey.ts";
7import { ProjectError } from "./project-error.ts";
8import type { AgentState, Entry, Meta, Mode } from "./project-types.ts";
10/** What a file move needs from a project. */
11export interface MoveHost {
12 needInit(): void;
13 readonly agents: ReadonlyMap<string, AgentState>;
14 agentView(agent: AgentState): ReadView;
15 sharedView(): ReadView;
16 agent(name: string, entries: Entry[]): AgentState;
17 addDraft(
18 agent: AgentState,
19 view: ReadView,
20 input: { edits: readonly EditInput[]; move?: MoveInput },
21 time: number,
22 meta: Meta,
23 entries: Entry[],
24 ): void;
25 recordFile(
26 agent: AgentState,
27 view: ReadView,
28 path: string,
29 text: string | null | undefined,
30 time: number,
31 meta: Meta,
32 entries: Entry[],
33 mode?: Mode,
34 blob?: string,
35 hunks?: readonly Hunk[],
36 ): boolean;
37 newsAboutOwnDraft(agent: AgentState, entries: Entry[]): void;
38 takeNews(agent: AgentState, entries: Entry[]): string[];
39 flush(entries: Entry[]): void;
40}
42/**
43 * Move a file in the agent's draft (#49): its lines keep their ids, as with
44 * git mv, so blame and why follow it. The only way to change a file's case.
45 * Refused when `to` is another existing file, ignoring case.
46 */
47export function move(
48 host: MoveHost,
49 agentName: string,
50 from: string,
51 to: string,
52 time: number,
53): { ok: true; from: string; to: string; news: string[] } {
54 host.needInit();
55 if (typeof from !== "string" || typeof to !== "string")
56 throw new ProjectError("bad-request", "move needs {from, to}");
57 checkPath(from);
58 checkPath(to);
59 const known = host.agents.get(agentName);
60 const before = known ? host.agentView(known) : host.sharedView();
61 if (!before.hasFile(from)) throw new ProjectError("not-found", `there is no ${from} to move`);
62 if (from === to) throw new ProjectError("bad-request", `${from} is already called that`);
63 const taken = before.paths().find((p) => p !== from && pathKey(p) === pathKey(to));
64 if (taken)
65 throw new ProjectError(
66 "conflict",
67 `${to} is ${taken}, another file (spellings that differ only in case or Unicode form are one file)`,
68 );
69 const entries: Entry[] = [];
70 const agent = host.agent(agentName, entries);
71 const view = host.agentView(agent);
72 const attrs = view.attrs(from);
73 const lines = view.lines(from).length;
74 const mode: Mode = { exec: attrs.exec, link: attrs.link };
75 if (lines > 0 && attrs.blob === null) {
76 const text = view.text(from);
77 host.addDraft(agent, view, { edits: [], move: { file: from, start: 0, end: lines, to, at: 0 } }, time, {}, entries);
78 host.recordFile(agent, host.agentView(agent), to, text, time, {}, entries, mode);
79 } else {
80 // A binary or empty file has no lines to carry: the new name takes its blob and modes.
81 host.recordFile(agent, host.agentView(agent), to, "", time, {}, entries, mode, attrs.blob ?? undefined);
82 }
83 host.recordFile(agent, host.agentView(agent), from, null, time, {}, entries);
84 host.newsAboutOwnDraft(agent, entries);
85 const news = host.takeNews(agent, entries);
86 host.flush(entries);
87 return { ok: true, from, to, news };
88}