@comma-agents/core 2.0.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (216) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +18 -0
  3. package/dist/abortable/abortable.d.ts +5 -0
  4. package/dist/abortable/abortable.types.d.ts +10 -0
  5. package/dist/abortable/index.d.ts +2 -0
  6. package/dist/agents/agent/agent.constants.d.ts +5 -0
  7. package/dist/agents/agent/agent.d.ts +24 -0
  8. package/dist/agents/agent/agent.types.d.ts +408 -0
  9. package/dist/agents/agent/agent.utils.d.ts +107 -0
  10. package/dist/agents/built-in/user/user-agent.d.ts +32 -0
  11. package/dist/agents/built-in/user/user-agent.types.d.ts +53 -0
  12. package/dist/agents/built-in/user/user-agent.utils.d.ts +2 -0
  13. package/dist/agents/hook-into-agent/hook-into-agent.d.ts +29 -0
  14. package/dist/agents/hooks/hooks.types.d.ts +64 -0
  15. package/dist/agents/hooks/hooks.utils.d.ts +9 -0
  16. package/dist/agents/hooks/index.d.ts +2 -0
  17. package/dist/agents/loader/index.d.ts +4 -0
  18. package/dist/agents/loader/loader.d.ts +49 -0
  19. package/dist/agents/loader/loader.schema.d.ts +270 -0
  20. package/dist/agents/loader/loader.types.d.ts +15 -0
  21. package/dist/conversation-context/conversation-context.d.ts +78 -0
  22. package/dist/conversation-context/conversation-context.types.d.ts +111 -0
  23. package/dist/conversation-context/conversation-context.utils.d.ts +6 -0
  24. package/dist/conversation-context/index.d.ts +3 -0
  25. package/dist/conversation-context/retention/compaction/compaction.constants.d.ts +3 -0
  26. package/dist/conversation-context/retention/compaction/compaction.d.ts +21 -0
  27. package/dist/conversation-context/retention/compaction/compaction.types.d.ts +26 -0
  28. package/dist/conversation-context/retention/compaction/index.d.ts +2 -0
  29. package/dist/conversation-context/retention/index.d.ts +4 -0
  30. package/dist/conversation-context/retention/retention.d.ts +9 -0
  31. package/dist/conversation-context/retention/retention.types.d.ts +90 -0
  32. package/dist/conversation-context/retention/retention.utils.d.ts +3 -0
  33. package/dist/conversation-context/retention/rolling-window/index.d.ts +2 -0
  34. package/dist/conversation-context/retention/rolling-window/rolling-window.d.ts +13 -0
  35. package/dist/conversation-context/retention/rolling-window/rolling-window.types.d.ts +5 -0
  36. package/dist/credentials/backends/json-file.d.ts +21 -0
  37. package/dist/credentials/credentials.constants.d.ts +9 -0
  38. package/dist/credentials/credentials.d.ts +18 -0
  39. package/dist/credentials/credentials.schema.d.ts +102 -0
  40. package/dist/credentials/credentials.types.d.ts +118 -0
  41. package/dist/credentials/credentials.utils.d.ts +30 -0
  42. package/dist/credentials/index.d.ts +6 -0
  43. package/dist/defaults/defaults.d.ts +108 -0
  44. package/dist/defaults/defaults.types.d.ts +51 -0
  45. package/dist/defaults/index.d.ts +2 -0
  46. package/dist/errors/index.d.ts +70 -0
  47. package/dist/flows/built-in/broadcast/broadcast-flow.constants.d.ts +2 -0
  48. package/dist/flows/built-in/broadcast/broadcast-flow.d.ts +30 -0
  49. package/dist/flows/built-in/cycle/cycle-flow.d.ts +53 -0
  50. package/dist/flows/built-in/sequential/sequential-flow.d.ts +25 -0
  51. package/dist/flows/flow/flow.d.ts +79 -0
  52. package/dist/flows/flow/flow.types.d.ts +179 -0
  53. package/dist/flows/flow/flow.utils.d.ts +18 -0
  54. package/dist/flows/hook-into-flow/hook-into-flow.d.ts +30 -0
  55. package/dist/flows/index.d.ts +6 -0
  56. package/dist/flows/loader/index.d.ts +4 -0
  57. package/dist/flows/loader/loader.d.ts +55 -0
  58. package/dist/flows/loader/loader.schema.d.ts +195 -0
  59. package/dist/flows/loader/loader.types.d.ts +27 -0
  60. package/dist/guard/guard.d.ts +17 -0
  61. package/dist/guard/guard.types.d.ts +112 -0
  62. package/dist/guard/index.d.ts +3 -0
  63. package/dist/guard/policies.d.ts +28 -0
  64. package/dist/hooks/built-in/token-tracking/index.d.ts +2 -0
  65. package/dist/hooks/built-in/token-tracking/token-tracking.constants.d.ts +14 -0
  66. package/dist/hooks/built-in/token-tracking/token-tracking.d.ts +87 -0
  67. package/dist/hooks/built-in/token-tracking/token-tracking.types.d.ts +136 -0
  68. package/dist/hooks/hooks.d.ts +10 -0
  69. package/dist/hooks/hooks.types.d.ts +11 -0
  70. package/dist/hooks/index.d.ts +2 -0
  71. package/dist/index.d.ts +50 -0
  72. package/dist/index.js +8961 -0
  73. package/dist/language/index.d.ts +1 -0
  74. package/dist/language/language.types.d.ts +60 -0
  75. package/dist/model/index.d.ts +5 -0
  76. package/dist/model/model.d.ts +63 -0
  77. package/dist/model/model.types.d.ts +85 -0
  78. package/dist/model/model.utils.d.ts +151 -0
  79. package/dist/model/providers/catalog/catalog.d.ts +57 -0
  80. package/dist/model/providers/catalog/catalog.types.d.ts +50 -0
  81. package/dist/model/providers/catalog/catalog.utils.d.ts +15 -0
  82. package/dist/model/providers/catalog/index.d.ts +3 -0
  83. package/dist/model/providers/index.d.ts +7 -0
  84. package/dist/model/providers/listers/copilot.d.ts +10 -0
  85. package/dist/model/providers/listers/index.d.ts +2 -0
  86. package/dist/model/providers/listers/ollama.d.ts +9 -0
  87. package/dist/model/providers/providers.d.ts +69 -0
  88. package/dist/model/providers/providers.types.d.ts +157 -0
  89. package/dist/model/providers/providers.utils.d.ts +16 -0
  90. package/dist/prompts/index.d.ts +4 -0
  91. package/dist/prompts/message-builder.d.ts +89 -0
  92. package/dist/prompts/prompts.types.d.ts +86 -0
  93. package/dist/prompts/template/prompt-template.d.ts +22 -0
  94. package/dist/sandbox/in-sandbox.d.ts +31 -0
  95. package/dist/sandbox/index.d.ts +4 -0
  96. package/dist/sandbox/sandbox.constants.d.ts +31 -0
  97. package/dist/sandbox/sandbox.d.ts +11 -0
  98. package/dist/sandbox/sandbox.types.d.ts +105 -0
  99. package/dist/skills/index.d.ts +4 -0
  100. package/dist/skills/skills.constants.d.ts +10 -0
  101. package/dist/skills/skills.loader.d.ts +46 -0
  102. package/dist/skills/skills.registry.d.ts +18 -0
  103. package/dist/skills/skills.types.d.ts +66 -0
  104. package/dist/skills/skills.utils.d.ts +37 -0
  105. package/dist/strategy/discover/discover.d.ts +16 -0
  106. package/dist/strategy/discover/discover.types.d.ts +70 -0
  107. package/dist/strategy/discover/discover.utils.d.ts +95 -0
  108. package/dist/strategy/discover/index.d.ts +3 -0
  109. package/dist/strategy/exporter/exporter.d.ts +27 -0
  110. package/dist/strategy/exporter/exporter.types.d.ts +6 -0
  111. package/dist/strategy/index.d.ts +10 -0
  112. package/dist/strategy/loader/loader.d.ts +39 -0
  113. package/dist/strategy/loader/loader.types.d.ts +106 -0
  114. package/dist/strategy/loader/loader.utils.d.ts +20 -0
  115. package/dist/strategy/loader/project-loader.d.ts +9 -0
  116. package/dist/strategy/schema.d.ts +1032 -0
  117. package/dist/timeline/index.d.ts +4 -0
  118. package/dist/timeline/projections/conversation-context.d.ts +19 -0
  119. package/dist/timeline/projections/file-state.d.ts +8 -0
  120. package/dist/timeline/projections/index.d.ts +3 -0
  121. package/dist/timeline/timeline.d.ts +11 -0
  122. package/dist/timeline/timeline.types.d.ts +97 -0
  123. package/dist/tools/build-tool-system-prompt.d.ts +41 -0
  124. package/dist/tools/built-in/ask-question/ask-question.d.ts +21 -0
  125. package/dist/tools/built-in/ask-question/index.d.ts +2 -0
  126. package/dist/tools/built-in/create-file/create-file.d.ts +18 -0
  127. package/dist/tools/built-in/create-file/create-file.types.d.ts +11 -0
  128. package/dist/tools/built-in/create-file/index.d.ts +2 -0
  129. package/dist/tools/built-in/delete-file/delete-file.constants.d.ts +14 -0
  130. package/dist/tools/built-in/delete-file/delete-file.d.ts +5 -0
  131. package/dist/tools/built-in/delete-file/delete-file.types.d.ts +26 -0
  132. package/dist/tools/built-in/delete-file/index.d.ts +2 -0
  133. package/dist/tools/built-in/describe-tool.d.ts +61 -0
  134. package/dist/tools/built-in/edit-file/edit-file.d.ts +46 -0
  135. package/dist/tools/built-in/edit-file/edit-file.replacers.d.ts +81 -0
  136. package/dist/tools/built-in/edit-file/edit-file.types.d.ts +59 -0
  137. package/dist/tools/built-in/edit-file/edit-file.utils.d.ts +70 -0
  138. package/dist/tools/built-in/edit-file/index.d.ts +2 -0
  139. package/dist/tools/built-in/glob/glob.constants.d.ts +3 -0
  140. package/dist/tools/built-in/glob/glob.d.ts +25 -0
  141. package/dist/tools/built-in/glob/glob.types.d.ts +31 -0
  142. package/dist/tools/built-in/glob/glob.utils.d.ts +4 -0
  143. package/dist/tools/built-in/glob/index.d.ts +2 -0
  144. package/dist/tools/built-in/launch-strategy/index.d.ts +2 -0
  145. package/dist/tools/built-in/launch-strategy/launch-strategy.d.ts +26 -0
  146. package/dist/tools/built-in/launch-strategy/launch-strategy.types.d.ts +14 -0
  147. package/dist/tools/built-in/list-directory/index.d.ts +2 -0
  148. package/dist/tools/built-in/list-directory/list-directory.constants.d.ts +3 -0
  149. package/dist/tools/built-in/list-directory/list-directory.d.ts +20 -0
  150. package/dist/tools/built-in/list-directory/list-directory.types.d.ts +41 -0
  151. package/dist/tools/built-in/list-directory/list-directory.utils.d.ts +4 -0
  152. package/dist/tools/built-in/list-skills/index.d.ts +2 -0
  153. package/dist/tools/built-in/list-skills/list-skills.d.ts +5 -0
  154. package/dist/tools/built-in/list-skills/list-skills.types.d.ts +10 -0
  155. package/dist/tools/built-in/list-strategy/index.d.ts +2 -0
  156. package/dist/tools/built-in/list-strategy/list-strategy.d.ts +14 -0
  157. package/dist/tools/built-in/list-strategy/list-strategy.types.d.ts +33 -0
  158. package/dist/tools/built-in/load-skill/index.d.ts +2 -0
  159. package/dist/tools/built-in/load-skill/load-skill.d.ts +24 -0
  160. package/dist/tools/built-in/load-skill/load-skill.types.d.ts +15 -0
  161. package/dist/tools/built-in/lsp-request/index.d.ts +2 -0
  162. package/dist/tools/built-in/lsp-request/lsp-request.d.ts +4 -0
  163. package/dist/tools/built-in/lsp-request/lsp-request.schema.d.ts +23 -0
  164. package/dist/tools/built-in/lsp-request/lsp-request.types.d.ts +4 -0
  165. package/dist/tools/built-in/move-file/index.d.ts +2 -0
  166. package/dist/tools/built-in/move-file/move-file.d.ts +21 -0
  167. package/dist/tools/built-in/move-file/move-file.types.d.ts +12 -0
  168. package/dist/tools/built-in/read-file/index.d.ts +2 -0
  169. package/dist/tools/built-in/read-file/read-file.constants.d.ts +1 -0
  170. package/dist/tools/built-in/read-file/read-file.d.ts +32 -0
  171. package/dist/tools/built-in/read-file/read-file.types.d.ts +34 -0
  172. package/dist/tools/built-in/read-file/read-file.utils.d.ts +14 -0
  173. package/dist/tools/built-in/restore-file/index.d.ts +2 -0
  174. package/dist/tools/built-in/restore-file/restore-file.d.ts +16 -0
  175. package/dist/tools/built-in/restore-file/restore-file.types.d.ts +11 -0
  176. package/dist/tools/built-in/run-command/index.d.ts +2 -0
  177. package/dist/tools/built-in/run-command/run-command.constants.d.ts +23 -0
  178. package/dist/tools/built-in/run-command/run-command.d.ts +21 -0
  179. package/dist/tools/built-in/run-command/run-command.types.d.ts +81 -0
  180. package/dist/tools/built-in/run-command/run-command.utils.d.ts +37 -0
  181. package/dist/tools/built-in/search-files/index.d.ts +2 -0
  182. package/dist/tools/built-in/search-files/search-files.constants.d.ts +5 -0
  183. package/dist/tools/built-in/search-files/search-files.d.ts +30 -0
  184. package/dist/tools/built-in/search-files/search-files.types.d.ts +19 -0
  185. package/dist/tools/built-in/search-files/search-files.utils.d.ts +11 -0
  186. package/dist/tools/built-in/todo/todo.d.ts +62 -0
  187. package/dist/tools/built-in/todo/todo.types.d.ts +7 -0
  188. package/dist/tools/built-in/webfetch/webfetch.constants.d.ts +3 -0
  189. package/dist/tools/built-in/webfetch/webfetch.d.ts +33 -0
  190. package/dist/tools/built-in/webfetch/webfetch.types.d.ts +8 -0
  191. package/dist/tools/built-in/webfetch/webfetch.utils.d.ts +19 -0
  192. package/dist/tools/built-in/write-file/index.d.ts +2 -0
  193. package/dist/tools/built-in/write-file/write-file.d.ts +18 -0
  194. package/dist/tools/built-in/write-file/write-file.types.d.ts +11 -0
  195. package/dist/tools/define/define-tool.d.ts +15 -0
  196. package/dist/tools/io/atomic-write.d.ts +37 -0
  197. package/dist/tools/io/audit-sink.d.ts +29 -0
  198. package/dist/tools/io/audit.types.d.ts +115 -0
  199. package/dist/tools/io/audit.utils.d.ts +48 -0
  200. package/dist/tools/io/binary.d.ts +16 -0
  201. package/dist/tools/io/bom.d.ts +15 -0
  202. package/dist/tools/io/diff.d.ts +27 -0
  203. package/dist/tools/io/hash.d.ts +25 -0
  204. package/dist/tools/io/index.d.ts +19 -0
  205. package/dist/tools/io/newline.d.ts +34 -0
  206. package/dist/tools/io/sandbox-error.d.ts +13 -0
  207. package/dist/tools/io/session-file-state.d.ts +68 -0
  208. package/dist/tools/io/stale-file.d.ts +9 -0
  209. package/dist/tools/io/trash.d.ts +57 -0
  210. package/dist/tools/launch-strategy.types.d.ts +38 -0
  211. package/dist/tools/result/index.d.ts +1 -0
  212. package/dist/tools/result/result.d.ts +47 -0
  213. package/dist/tools/tool.constants.d.ts +7 -0
  214. package/dist/tools/tool.registry.d.ts +54 -0
  215. package/dist/tools/tool.types.d.ts +191 -0
  216. package/package.json +48 -0
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Newline style as detected in a source buffer.
3
+ *
4
+ * - `"lf"` — Unix (`\n`). Default for new files.
5
+ * - `"crlf"` — Windows (`\r\n`).
6
+ * - `"mixed"` — File contains both `\n` and `\r\n`. We preserve the
7
+ * dominant style on write but flag this on read.
8
+ * - `"none"` — No newlines at all (single line or empty file).
9
+ */
10
+ export type NewlineStyle = "lf" | "crlf" | "mixed" | "none";
11
+ /**
12
+ * Detect the newline style of a string by counting `\r\n` vs lone
13
+ * `\n` occurrences.
14
+ *
15
+ * Lone `\r` (classic Mac OS) is treated as `none`/`lf` per modern
16
+ * tooling — we don't try to preserve it.
17
+ */
18
+ export declare function detectNewline(content: string): NewlineStyle;
19
+ /**
20
+ * Normalize all newlines in `content` to `\n`. Used before applying
21
+ * structural edits (so `oldText` matching is newline-agnostic);
22
+ * `applyNewline` re-emits the original style afterwards.
23
+ */
24
+ export declare function toLF(content: string): string;
25
+ /**
26
+ * Re-apply a newline style to a buffer that has been normalized to LF.
27
+ *
28
+ * - `"crlf"` rewrites `\n` back to `\r\n`.
29
+ * - `"lf"`, `"mixed"`, and `"none"` leave the content as-is. (For
30
+ * `"mixed"` we deliberately don't try to reconstruct the original
31
+ * per-line pattern; preserving LF is the safer default and matches
32
+ * what editors do when "Use LF" is the user's setting.)
33
+ */
34
+ export declare function applyNewline(content: string, style: NewlineStyle): string;
@@ -0,0 +1,13 @@
1
+ import type { SandboxViolationError } from "../../errors";
2
+ import type { ToolError } from "../tool.types";
3
+ /**
4
+ * Map a `SandboxViolationError` to the appropriate `ToolError`.
5
+ *
6
+ * Mapping:
7
+ * - `jail`, `absolute-path` → `outside_workspace` (non-recoverable;
8
+ * the path is structurally invalid, not just policy-blocked).
9
+ * - `read-denied`, `write-denied`,
10
+ * `forbidden-glob`, `ask-no-handler`,
11
+ * `ask-aborted` → `permission_denied`.
12
+ */
13
+ export declare function sandboxErrorToToolError(error: SandboxViolationError): ToolError;
@@ -0,0 +1,68 @@
1
+ import type { AuditEntry } from "./audit.types";
2
+ /**
3
+ * Most-recent known state for a single file in this session.
4
+ */
5
+ export interface SessionFileEntry {
6
+ /**
7
+ * Workspace-relative path of the file.
8
+ */
9
+ readonly path: string;
10
+ /**
11
+ * SHA-256 the agent last produced or observed for this file. For
12
+ * deleted files this is the hash at the moment of deletion (so the
13
+ * LLM can re-create with the correct expected hash if it wants).
14
+ */
15
+ readonly sha256: string;
16
+ /**
17
+ * `true` when the file is known to have been deleted in this
18
+ * session (most recent audit entry was a `"delete"`). Lookups still
19
+ * return the entry so the LLM can see it was deleted.
20
+ */
21
+ readonly deleted: boolean;
22
+ /**
23
+ * `true` when the on-disk hash no longer matches `sha256` (the file
24
+ * was edited outside the agent between sessions). Set by
25
+ * `verifySessionFileState` — defaults to `false` from
26
+ * `buildSessionFileState` alone.
27
+ */
28
+ readonly stale: boolean;
29
+ }
30
+ /**
31
+ * Snapshot of every file path the session has touched, keyed by
32
+ * workspace-relative path.
33
+ *
34
+ * Immutable from the caller's perspective: rebuild by replaying the
35
+ * audit log rather than mutating in place.
36
+ */
37
+ export type SessionFileState = ReadonlyMap<string, SessionFileEntry>;
38
+ /**
39
+ * Replay an ordered audit log into a `SessionFileState`.
40
+ *
41
+ * Replay rules (deterministic, audit-log order):
42
+ * - `"create"` / `"update"` → set `{ sha256: afterSha256, deleted: false }`.
43
+ * `afterSha256` is required; entries missing it are skipped (defensive
44
+ * against malformed log lines).
45
+ * - `"delete"` → set `{ sha256: beforeSha256 ?? "", deleted: true }`.
46
+ * - `"move"` → delete the source entry, then upsert the target with
47
+ * `afterSha256`.
48
+ * - Failed entries (`success: false`) are skipped — they didn't
49
+ * mutate disk so the prior state stands.
50
+ *
51
+ * Replays run in O(n) over the entries. Caller is responsible for
52
+ * passing entries in chronological order; `AuditSink.load` guarantees
53
+ * insertion order, which is chronological.
54
+ */
55
+ export declare function buildSessionFileState(entries: readonly AuditEntry[]): SessionFileState;
56
+ /**
57
+ * For each known (non-deleted) entry in `state`, recompute the
58
+ * on-disk SHA-256 and flag entries whose stored hash no longer
59
+ * matches as `stale`. Missing files (`ENOENT`) are also flagged
60
+ * stale so the LLM can decide whether to re-create.
61
+ *
62
+ * Returns a fresh `SessionFileState` — does not mutate the input.
63
+ *
64
+ * @param state - Result of `buildSessionFileState`.
65
+ * @param resolveAbsolute - Map workspace-relative path → absolute
66
+ * path. Provided by the caller so this module stays sandbox-free.
67
+ */
68
+ export declare function verifySessionFileState(state: SessionFileState, resolveAbsolute: (path: string) => string): Promise<SessionFileState>;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Recovery hint returned on every `stale_file` failure. Appended to
3
+ * the tool's LLM-facing `output` by `agent.utils.ts` (because
4
+ * `error.recoverable` is `true`).
5
+ *
6
+ * Wording is deliberately imperative and short — the LLM is asked
7
+ * to perform a specific corrective action, not to "consider" one.
8
+ */
9
+ export declare const STALE_FILE_RECOVERY_HINT = "Re-read the file to obtain the current sha256 and re-apply your edit.";
@@ -0,0 +1,57 @@
1
+ /** Metadata stored inside each trash archive. */
2
+ export interface TrashMetadata {
3
+ readonly trashedAt: string;
4
+ readonly originalPath: string;
5
+ readonly originalSha256: string;
6
+ readonly sessionId?: string;
7
+ readonly runId?: string;
8
+ readonly agentName?: string;
9
+ }
10
+ /** Subset of TrashMetadata injected by the daemon executor into the sandbox config. */
11
+ export type SandboxTrashMetadata = Pick<TrashMetadata, "runId" | "sessionId">;
12
+ /** A single trash entry returned by `listTrash`. */
13
+ export interface TrashEntry {
14
+ readonly path: string;
15
+ readonly metadata: TrashMetadata;
16
+ readonly sizeBytes: number;
17
+ }
18
+ /**
19
+ * Compute the trash bucket directory for `workspaceRoot`.
20
+ */
21
+ export declare function trashWorkspaceDir(workspaceRoot: string): string;
22
+ /**
23
+ * Move `absSourcePath` into the workspace's trash bucket as a tar.gz
24
+ * archive, returning the absolute path to the archive.
25
+ *
26
+ * The archive contains:
27
+ * - `metadata.json` with audit information
28
+ * - The original file at its workspace-relative path
29
+ */
30
+ export declare function moveToTrash(workspaceRoot: string, absSourcePath: string, metadata?: Omit<TrashMetadata, "trashedAt" | "originalPath" | "originalSha256">): Promise<string>;
31
+ /**
32
+ * List all trash entries for a workspace.
33
+ */
34
+ export declare function listTrash(workspaceRoot: string): Promise<readonly TrashEntry[]>;
35
+ /**
36
+ * Read the metadata.json from a trash archive without extracting files.
37
+ */
38
+ export declare function readTrashMetadata(archivePath: string): Promise<TrashMetadata | undefined>;
39
+ /**
40
+ * Restore a file from a trash archive.
41
+ *
42
+ * @param workspaceRoot - Workspace root directory.
43
+ * @param trashPath - Absolute path to the .tar.gz trash archive.
44
+ * @param targetPath - Optional workspace-relative override for the restore location.
45
+ * Defaults to the original path from metadata.
46
+ * @returns The absolute path of the restored file.
47
+ */
48
+ export declare function restoreFromTrash(workspaceRoot: string, trashPath: string, targetPath?: string): Promise<string>;
49
+ /**
50
+ * Clear all trash entries for a workspace.
51
+ *
52
+ * @returns Count of cleared entries and total bytes freed.
53
+ */
54
+ export declare function clearTrash(workspaceRoot: string): Promise<{
55
+ cleared: number;
56
+ bytesFreed: number;
57
+ }>;
@@ -0,0 +1,38 @@
1
+ /** Input to a {@link LaunchStrategyHandle}. */
2
+ export interface LaunchStrategyRequest {
3
+ /** Absolute path to the strategy file. */
4
+ readonly strategyPath: string;
5
+ /**
6
+ * Optional absolute path to a `comma-project.json` manifest. When
7
+ * present, the runtime is expected to call `loadProject(manifestPath)`
8
+ * before loading the strategy (registering custom tools, etc.).
9
+ */
10
+ readonly manifestPath?: string;
11
+ /** Initial message passed to the strategy's entry flow. */
12
+ readonly input: string;
13
+ /**
14
+ * Provider/model override applied to all LLM agents in the
15
+ * sub-strategy. Format: `"providerID/modelID"`.
16
+ */
17
+ readonly modelOverride?: string;
18
+ }
19
+ /** Output of a successful sub-strategy run. */
20
+ export interface LaunchStrategyResult {
21
+ /** The sub-strategy's `name` field. */
22
+ readonly strategyName: string;
23
+ /** Final text produced by the strategy's entry flow. */
24
+ readonly text: string;
25
+ /**
26
+ * Why the flow stopped (e.g., `"stop"`, `"tool-calls"`). Mirrors
27
+ * `AgentCallResult.finishReason` from the underlying flow call.
28
+ */
29
+ readonly finishReason?: string;
30
+ }
31
+ /**
32
+ * Run a sub-strategy and return its final result.
33
+ *
34
+ * Implementations are responsible for wiring `inputCollector`,
35
+ * `flowHooks`, agent hooks, and the loaded project (if any), as well
36
+ * as broadcasting events to any observers.
37
+ */
38
+ export type LaunchStrategyHandle = (request: LaunchStrategyRequest) => Promise<LaunchStrategyResult>;
@@ -0,0 +1 @@
1
+ export { errorResult, okResult, toolError } from "./result";
@@ -0,0 +1,47 @@
1
+ import type { ToolError, ToolErrorKind, ToolResult } from "../tool.types";
2
+ /**
3
+ * Build a successful `ToolResult`.
4
+ *
5
+ * Use this from every tool's `execute` to guarantee the `ok` invariant
6
+ * (`ok: true` ⇒ `error` undefined) without having to repeat the shape.
7
+ *
8
+ * @param output - Text returned to the LLM.
9
+ * @param options - Optional structured `data` and `metadata` payloads.
10
+ * @example
11
+ * ```ts
12
+ * return okResult("Wrote 12 bytes", { data: { sha256, sizeBytes: 12 } });
13
+ * ```
14
+ */
15
+ export declare function okResult<DataShape = unknown>(output: string, options?: {
16
+ readonly data?: DataShape;
17
+ readonly metadata?: Readonly<Record<string, unknown>>;
18
+ }): ToolResult<DataShape>;
19
+ /**
20
+ * Build a failure `ToolResult` from a structured `ToolError`.
21
+ *
22
+ * `output` defaults to `error.message` so the LLM still receives a
23
+ * human-readable string when the AI SDK only forwards `output`.
24
+ *
25
+ * @param errorInput - Either a fully-built `ToolError` or the fields needed to construct one.
26
+ * @param options - Optional `output` override and `data` payload.
27
+ * @example
28
+ * ```ts
29
+ * return errorResult({
30
+ * kind: "stale_file",
31
+ * message: "File changed since last read",
32
+ * path,
33
+ * recoverable: true,
34
+ * suggestedNextAction: STALE_FILE_RECOVERY_HINT,
35
+ * });
36
+ * ```
37
+ */
38
+ export declare function errorResult<DataShape = unknown>(errorInput: ToolError, options?: {
39
+ readonly output?: string;
40
+ readonly data?: DataShape;
41
+ readonly metadata?: Readonly<Record<string, unknown>>;
42
+ }): ToolResult<DataShape>;
43
+ /**
44
+ * Convenience: build a `ToolError` without manually spelling the literal kind twice.
45
+ * Equivalent to writing the object literal directly; provided for ergonomics.
46
+ */
47
+ export declare function toolError(kind: ToolErrorKind, message: string, fields?: Omit<ToolError, "kind" | "message">): ToolError;
@@ -0,0 +1,7 @@
1
+ import type { ToolDefinition } from "./tool.types";
2
+ /**
3
+ * The set of tool names recognized as built-in.
4
+ */
5
+ export declare const BUILT_IN_TOOL_NAMES: readonly ["read_file", "list_directory", "search_files", "glob", "create_file", "write_file", "edit_file", "delete_file", "restore_file", "move_file", "run_command", "webfetch", "load_skill", "list_skills", "list_strategy", "launch_strategy", "todo_add", "todo_complete", "todo_get", "todo_get_next", "todo_remove", "todo_clear", "ask_question", "lsp_request"];
6
+ /** Factory map for instantiating built-in tools by name. */
7
+ export declare const BUILT_IN_TOOL_FACTORIES: Readonly<Record<string, () => ToolDefinition>>;
@@ -0,0 +1,54 @@
1
+ import type { ToolDefinition } from "./tool.types";
2
+ /**
3
+ * Register a custom tool with the global tool registry.
4
+ *
5
+ * Registered tools can be referenced by name in agent definitions
6
+ * (strategy files, agent descriptions, or programmatic config).
7
+ * Custom registry tools take precedence over built-in tools during
8
+ * resolution — if you register a tool with a built-in name (e.g., "bash"),
9
+ * it shadows the default. A warning is logged in that case.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { registerTool, defineTool } from "@comma-agents/core";
14
+ *
15
+ * const myTool = defineTool({
16
+ * description: "Fetch a URL",
17
+ * parameters: z.object({ url: z.string() }),
18
+ * execute: async ({ url }) => okResult(await fetch(url).then(r => r.text())),
19
+ * });
20
+ *
21
+ * registerTool("fetch", myTool);
22
+ * ```
23
+ */
24
+ export declare function registerTool(name: string, tool: ToolDefinition): void;
25
+ /**
26
+ * Remove a previously registered custom tool.
27
+ * Returns `true` if the tool was registered and removed.
28
+ * Built-in tools cannot be unregistered via this function.
29
+ */
30
+ export declare function unregisterTool(name: string): boolean;
31
+ /**
32
+ * Resolve an array of tool name strings into a `Record<string, ToolDefinition>`.
33
+ *
34
+ * Resolution order for each name:
35
+ * 1. Global tool registry (custom tools via `registerTool()`)
36
+ * 2. Built-in tool factories (see `BUILT_IN_TOOL_NAMES` in `tool.constants.ts`)
37
+ * 3. Explicit `customTools` parameter (passed by the strategy loader / description loader)
38
+ * 4. Error — unknown tool name
39
+ *
40
+ * Custom registry tools shadow built-in tools of the same name.
41
+ *
42
+ * @throws {StrategyValidationError} If a tool name cannot be resolved.
43
+ */
44
+ export declare function resolveTools(toolNames: readonly string[], agentName: string, customTools?: Readonly<Record<string, ToolDefinition>>): Record<string, ToolDefinition>;
45
+ /**
46
+ * Get the list of currently registered custom tool names.
47
+ * Does not include built-in tools.
48
+ */
49
+ export declare function getRegisteredToolNames(): readonly string[];
50
+ /**
51
+ * Reset the global tool registry to empty state.
52
+ * Primarily for tests.
53
+ */
54
+ export declare function resetToolRegistry(): void;
@@ -0,0 +1,191 @@
1
+ import type { z } from "zod";
2
+ import type { InputCollector } from "../agents/built-in/user/user-agent.types";
3
+ import type { Guard, Policy } from "../guard/guard.types";
4
+ import type { LanguageService } from "../language";
5
+ import type { SkillRegistry } from "../skills/skills.types";
6
+ import type { AuditSink } from "./io/audit.types";
7
+ import type { LaunchStrategyHandle } from "./launch-strategy.types";
8
+ /**
9
+ * Context passed to tool execute functions.
10
+ * Provides the calling agent's identity, its per-tool Guard for
11
+ * file-system and command authorization, cancellation support,
12
+ * and optional session/audit/skill state.
13
+ */
14
+ export interface ToolContext {
15
+ /** Name of the agent that invoked this tool. */
16
+ readonly agentName: string;
17
+ /** AbortSignal for cancellation propagation. */
18
+ readonly abort: AbortSignal;
19
+ /**
20
+ * Per-tool guard that handles path resolution, jail enforcement,
21
+ * policy chain evaluation, and interactive "ask" dispatch.
22
+ * Always present — defaults to a permissive guard when none is configured.
23
+ */
24
+ readonly guard: Guard;
25
+ /**
26
+ * Optional session identifier. When present, mutations are written to a
27
+ * session-scoped audit log and `SessionFileState` can be reconstructed
28
+ * across runtime restarts.
29
+ *
30
+ * Sessions are a broader concept than runs — one user/daemon session
31
+ * typically spans many strategy runs and is the unit of audit-log
32
+ * grouping and trash-archive identification. For per-run isolation
33
+ * (notably `todo_*` silos across recursive `launch_strategy` calls),
34
+ * use {@link runId} instead.
35
+ */
36
+ readonly sessionId?: string;
37
+ /**
38
+ * Optional run identifier — one strategy invocation. The daemon
39
+ * supplies the top-level run's id at the entry point and a *fresh*
40
+ * derived id for each `launch_strategy` sub-load, so recursive
41
+ * sub-strategies get isolated state for tools that key on `runId`.
42
+ *
43
+ * Distinct from {@link sessionId}: sessionId groups many runs (audit,
44
+ * trash); runId identifies a single run. Tools that need per-run shared
45
+ * state with per-launch isolation (e.g., `todo_*`) silo on `runId`.
46
+ */
47
+ readonly runId?: string;
48
+ /**
49
+ * Optional audit sink for destructive file operations.
50
+ */
51
+ readonly auditSink?: AuditSink;
52
+ /**
53
+ * Optional skill registry exposed to the `load_skill` tool.
54
+ */
55
+ readonly skillRegistry?: SkillRegistry;
56
+ /**
57
+ * Optional input collector forwarded from the parent strategy.
58
+ *
59
+ * Tools that spawn sub-strategies (e.g., `launch_strategy`) pass this
60
+ * to `loadStrategyFromString` so any `user` agents in the child can
61
+ * prompt through the same UI as the parent run.
62
+ */
63
+ readonly inputCollector?: InputCollector;
64
+ /**
65
+ * Optional handle for spawning a sub-strategy.
66
+ *
67
+ * When provided by the runtime (e.g., the daemon executor), tools
68
+ * such as `launch_strategy` delegate to this handle instead of
69
+ * calling `loadStrategy` directly, so flow / agent hooks and
70
+ * broadcast wiring are reused for the nested run.
71
+ */
72
+ readonly launchStrategy?: LaunchStrategyHandle;
73
+ /**
74
+ * Optional runtime language service.
75
+ *
76
+ * Core defines the stable contract and language tools; runtimes such as the
77
+ * daemon provide concrete implementations (TypeScript today, more language
78
+ * adapters later).
79
+ */
80
+ readonly languageService?: LanguageService;
81
+ }
82
+ /**
83
+ * Closed enumeration of error kinds emitted by built-in tools.
84
+ *
85
+ * The set is fixed so callers (LLMs and orchestration code) can switch on
86
+ * `kind` without parsing free-form messages. New kinds are added here
87
+ * deliberately and never derived from underlying error strings.
88
+ */
89
+ export type ToolErrorKind = "not_found" | "already_exists" | "permission_denied" | "outside_workspace" | "binary_file" | "file_too_large" | "stale_file" | "old_text_not_found" | "multiple_matches" | "overlapping_edits" | "patch_parse_error" | "patch_apply_error" | "command_failed" | "timeout" | "skill_unavailable" | "language_unavailable" | "unknown";
90
+ /**
91
+ * Structured error returned by a tool when an operation cannot complete.
92
+ *
93
+ * Tools must populate `recoverable` and (when recoverable) `suggestedNextAction`
94
+ * so the calling LLM can self-correct without escalating to the user.
95
+ */
96
+ export interface ToolError {
97
+ /** Stable machine-readable error category. */
98
+ readonly kind: ToolErrorKind;
99
+ /** Human-readable description of what went wrong. */
100
+ readonly message: string;
101
+ /** Resolved path the error refers to, when applicable. */
102
+ readonly path?: string;
103
+ /** Free-form structured detail (match ranges, conflict indices, etc.). */
104
+ readonly details?: Readonly<Record<string, unknown>>;
105
+ /** Whether the LLM can plausibly retry after applying `suggestedNextAction`. */
106
+ readonly recoverable: boolean;
107
+ /** When `recoverable`, a concrete instruction for the LLM's next call. */
108
+ readonly suggestedNextAction?: string;
109
+ }
110
+ /**
111
+ * Result returned from a tool execution.
112
+ *
113
+ * The shape is dual-purpose:
114
+ * - `output` and `metadata` remain the LLM-facing surface: `output` is the
115
+ * text returned to the model and `metadata` is opaque side-channel data
116
+ * for serializers and hooks.
117
+ * - `ok`, `error`, and `data` are the structured surface used by tests,
118
+ * the daemon, and any code that needs to inspect the outcome without
119
+ * parsing strings.
120
+ *
121
+ * `ok` is the source of truth for success/failure. When `ok` is `false`,
122
+ * `error` MUST be set. When `ok` is `true`, `error` MUST be undefined.
123
+ */
124
+ export interface ToolResult<DataShape = unknown> {
125
+ /** Whether the tool completed successfully. */
126
+ readonly ok: boolean;
127
+ /** The textual output to return to the LLM. */
128
+ readonly output: string;
129
+ /** Structured payload — typed per-tool via the `DataShape` generic. */
130
+ readonly data?: DataShape;
131
+ /** Structured failure information. Required when `ok` is `false`. */
132
+ readonly error?: ToolError;
133
+ /** Optional opaque metadata for hooks and serializers. */
134
+ readonly metadata?: Readonly<Record<string, unknown>>;
135
+ }
136
+ /**
137
+ * Definition of a tool that an agent can invoke.
138
+ *
139
+ * Tools have a description (for the LLM), Zod-validated parameters,
140
+ * and an async execute function.
141
+ *
142
+ * Optionally, tools can contribute to the agent's system prompt via
143
+ * the `systemPrompt` field. This is useful when a tool needs to inject
144
+ * complex context or instructions that should be visible to the LLM.
145
+ *
146
+ * @typeParam ParameterSchema - Zod schema type for the tool's parameters
147
+ * @typeParam DataShape - Typed payload returned in `ToolResult.data`
148
+ */
149
+ export interface ToolDefinition<ParameterSchema extends z.ZodType = z.ZodType, DataShape = unknown> {
150
+ /** Human-readable description of what this tool does (sent to the LLM). */
151
+ readonly description: string;
152
+ /** Zod schema that validates and types the tool's parameters. */
153
+ readonly parameters: ParameterSchema;
154
+ /**
155
+ * Additional policies for this tool (e.g., run_command deny/approval patterns).
156
+ * Added to the tool's Guard policy chain by `createSandbox()`.
157
+ */
158
+ readonly policies?: readonly Policy[];
159
+ /**
160
+ * Optional system prompt contribution from this tool.
161
+ *
162
+ * When provided, this content is injected ONCE into the agent's system
163
+ * prompt at agent creation time (not per-call). This allows tools to
164
+ * define complex context, instructions, or formatting requirements that
165
+ * the LLM should know about.
166
+ *
167
+ * Can be a static string or a function that receives the ToolContext
168
+ * and returns a string (sync or async). The function form is useful
169
+ * when the prompt depends on runtime state (e.g., current workspace).
170
+ *
171
+ * @example
172
+ * ```ts
173
+ * // Static prompt
174
+ * defineTool({
175
+ * systemPrompt: "When using this tool, always format output as JSON.",
176
+ * // ...
177
+ * })
178
+ *
179
+ * // Dynamic prompt (function)
180
+ * defineTool({
181
+ * systemPrompt: async (toolContext) => {
182
+ * return `Working directory: ${toolContext.guard.cwd}`;
183
+ * },
184
+ * // ...
185
+ * })
186
+ * ```
187
+ */
188
+ readonly systemPrompt?: string | ((toolContext: ToolContext) => Promise<string> | string);
189
+ /** Execute the tool with validated arguments and context. */
190
+ execute(validatedArguments: z.infer<ParameterSchema>, toolContext: ToolContext): Promise<ToolResult<DataShape>>;
191
+ }
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@comma-agents/core",
3
+ "version": "2.0.0-rc.0",
4
+ "description": "Core agent orchestration framework: agents, flows, hooks, and providers",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": ["dist", "README.md", "LICENSE"],
15
+ "scripts": {
16
+ "build": "bun run ../../scripts/build-public-package.ts core",
17
+ "prepack": "bun run build",
18
+ "test": "bun test --coverage",
19
+ "lint": "biome check src",
20
+ "lint:fix": "biome check --write src"
21
+ },
22
+ "dependencies": {
23
+ "@ai-sdk/openai-compatible": "2.0.30",
24
+ "@types/diff": "8.0.0",
25
+ "ai": "6.0.91",
26
+ "diff": "9.0.0",
27
+ "liquidjs": "10.27.0",
28
+ "tar-stream": "3.2.0",
29
+ "turndown": "7.2.4",
30
+ "yaml": "2.8.3",
31
+ "zod": "3.25.76",
32
+ "strip-json-comments": "5.0.3"
33
+ },
34
+ "engines": { "bun": ">=1.3.0" },
35
+ "publishConfig": { "access": "public", "tag": "next" },
36
+ "repository": {
37
+ "type": "git",
38
+ "url": "https://github.com/CloAI/CommaAgents.git",
39
+ "directory": "packages/core"
40
+ },
41
+ "author": "CloAI",
42
+ "license": "MIT",
43
+ "devDependencies": {
44
+ "@comma-agents/utils": "file:../utils",
45
+ "@types/tar-stream": "^3.1.4",
46
+ "@types/turndown": "5.0.6"
47
+ }
48
+ }