File size: 3,254 Bytes
4e7644b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { request } from "../request";

/**
 * Provenance of a chat's effective project directory / directory list.
 *
 * - "session": this chat has its own override.
 * - "agent": inherited from the agent default.
 * - "workspace_fallback": nothing configured; the agent workspace is used.
 * - "fork": inherited from a fork source and locked.
 * - "active_mode": inherited from the active mode and locked.
 * - "request": supplied per-request and locked.
 * - "inherited": inherited from a parent and locked.
 */
export type ChatProjectDirSource =
  | "session"
  | "agent"
  | "workspace_fallback"
  | "fork"
  | "active_mode"
  | "request"
  | "inherited";

export interface EffectiveProjectDirectory {
  project_dir: string;
  source: ChatProjectDirSource;
  agent_project_dir: string | null;
  exists: boolean;
}

/** One effective project-directory entry as returned by the server. */
export interface ProjectDirEntry {
  path: string;
  label: string | null;
  exists: boolean;
  nested_with: string | null;
  /**
   * Whether this entry is the agent's own workspace directory.
   *
   * Decided by the server from filesystem identity, because the client
   * cannot: comparing the two paths as text splits one directory into two
   * roots on a case-sensitive volume β€” two switcher entries, two sets of
   * editor tabs β€” and merges two distinct ones on a folding volume.
   */
  is_workspace: boolean;
}

/** Effective project-directory list for a chat, plus provenance. */
export interface ChatProjectDirs {
  project_dirs: ProjectDirEntry[];
  source: ChatProjectDirSource;
  agent_project_dir: string | null;
}

/** One entry as sent to the server when setting the list. */
export interface ProjectDirPayloadEntry {
  path: string;
  label?: string | null;
}

export const chatProjectDirectoryApi = {
  // ── Plural (session-scoped ordered list) ──────────────────────────────
  /** Get the chat's effective project-directory list, primary first. */
  getProjectDirs: (chatId: string) =>
    request<ChatProjectDirs>(
      `/chats/${encodeURIComponent(chatId)}/project-dirs`,
    ),

  /**
   * Replace the chat's whole project-directory list. The payload is the
   * full ordered list (index 0 becomes primary); add/remove/make-primary
   * are all expressed as list transforms followed by one PUT.
   */
  setProjectDirs: (chatId: string, entries: ProjectDirPayloadEntry[]) =>
    request<ChatProjectDirs>(
      `/chats/${encodeURIComponent(chatId)}/project-dirs`,
      {
        method: "PUT",
        body: JSON.stringify({ project_dirs: entries }),
      },
    ),

  /** Clear the chat's override so it inherits the agent default. */
  clearProjectDirs: (chatId: string) =>
    request<ChatProjectDirs>(
      `/chats/${encodeURIComponent(chatId)}/project-dirs`,
      { method: "DELETE" },
    ),

  /**
   * Primary directory only. Still used where a single path is enough (the
   * Files workspace and navigator), and as the fallback display value when
   * the chat has no override and the plural list comes back empty.
   */
  get: (chatId: string) =>
    request<EffectiveProjectDirectory>(
      `/chats/${encodeURIComponent(chatId)}/project-dir`,
    ),
};