@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,55 @@
1
+ import type { Agent } from "../../agents/agent/agent.types";
2
+ import type { LoadFlowOptions } from "./loader.types";
3
+ /**
4
+ * Load a single flow from a JSON or YAML description file.
5
+ *
6
+ * Auto-detects format by file extension (`.json`, `.yaml`, `.yml`).
7
+ * Validates the structure, resolves agent references from the provided
8
+ * registry, and returns a live `Agent` (flow) ready to call.
9
+ *
10
+ * @param filePath - Absolute or relative path to the flow description file.
11
+ * @param options - Agent registry and optional hooks.
12
+ * @returns A live Agent implementing the flow.
13
+ * @throws {StrategyValidationError} If the file is invalid or missing required fields.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { loadAgent, loadFlow } from "@comma-agents/core";
18
+ *
19
+ * const writer = await loadAgent("./agents/writer.yaml");
20
+ * const reviewer = await loadAgent("./agents/reviewer.yaml");
21
+ *
22
+ * const flow = await loadFlow("./flows/review-pipeline.yaml", {
23
+ * agents: { writer, reviewer },
24
+ * });
25
+ * const result = await flow.call("Write a function that adds two numbers");
26
+ * ```
27
+ */
28
+ export declare function loadFlow(filePath: string, options: LoadFlowOptions): Promise<Agent>;
29
+ /**
30
+ * Load a single flow from a raw JSON or YAML string.
31
+ *
32
+ * Useful when the content is already in memory (e.g., received over
33
+ * a WebSocket, from a database, or from a test fixture).
34
+ *
35
+ * @param content - The raw flow description string.
36
+ * @param format - "json" or "yaml".
37
+ * @param options - Agent registry and optional hooks.
38
+ * @returns A live Agent implementing the flow.
39
+ * @throws {StrategyValidationError} If parsing or validation fails.
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * const yaml = `
44
+ * name: review-pipeline
45
+ * type: sequential
46
+ * steps:
47
+ * - agent: writer
48
+ * - agent: reviewer
49
+ * `;
50
+ * const flow = loadFlowFromString(yaml, "yaml", {
51
+ * agents: { writer, reviewer },
52
+ * });
53
+ * ```
54
+ */
55
+ export declare function loadFlowFromString(content: string, format: "json" | "yaml", options: LoadFlowOptions): Agent;
@@ -0,0 +1,195 @@
1
+ import { z } from "zod";
2
+ /**
3
+ * A step that references a named agent from the caller-provided registry.
4
+ *
5
+ * @example
6
+ * ```yaml
7
+ * steps:
8
+ * - agent: researcher
9
+ * - agent: writer
10
+ * ```
11
+ */
12
+ export declare const AgentStepDescriptionSchema: z.ZodObject<{
13
+ /** Name of the agent in the caller-provided registry. */
14
+ agent: z.ZodString;
15
+ /** Optional human-readable description of this step. */
16
+ description: z.ZodOptional<z.ZodString>;
17
+ }, "strict", z.ZodTypeAny, {
18
+ agent: string;
19
+ description?: string | undefined;
20
+ }, {
21
+ agent: string;
22
+ description?: string | undefined;
23
+ }>;
24
+ /**
25
+ * A step that is a nested flow definition (recursive).
26
+ * Uses `z.lazy()` to support arbitrarily deep nesting.
27
+ */
28
+ export declare const FlowStepDescriptionSchema: z.ZodType;
29
+ /** Sequential flow — pipeline where each step receives the previous step's output. */
30
+ export declare const SequentialFlowDescriptionSchema: z.ZodObject<{
31
+ type: z.ZodLiteral<"sequential">;
32
+ /** Unique name for this flow. */
33
+ name: z.ZodString;
34
+ /** Optional human-readable description. */
35
+ description: z.ZodOptional<z.ZodString>;
36
+ /** Steps to execute — agent references or nested flow definitions. */
37
+ steps: z.ZodArray<z.ZodType<any, z.ZodTypeDef, any>, "many">;
38
+ }, "strict", z.ZodTypeAny, {
39
+ type: "sequential";
40
+ name: string;
41
+ steps: any[];
42
+ description?: string | undefined;
43
+ }, {
44
+ type: "sequential";
45
+ name: string;
46
+ steps: any[];
47
+ description?: string | undefined;
48
+ }>;
49
+ /** Cycle flow — repeating pipeline with optional observer and cycle count. */
50
+ export declare const CycleFlowDescriptionSchema: z.ZodObject<{
51
+ type: z.ZodLiteral<"cycle">;
52
+ /**
53
+ * Number of cycle iterations. Use `"Infinity"` for infinite loops.
54
+ * @default 1
55
+ */
56
+ cycles: z.ZodOptional<z.ZodUnion<[z.ZodNumber, z.ZodLiteral<"Infinity">]>>;
57
+ /** Name of an observer agent that runs after each cycle. */
58
+ observer: z.ZodOptional<z.ZodString>;
59
+ /** Unique name for this flow. */
60
+ name: z.ZodString;
61
+ /** Optional human-readable description. */
62
+ description: z.ZodOptional<z.ZodString>;
63
+ /** Steps to execute — agent references or nested flow definitions. */
64
+ steps: z.ZodArray<z.ZodType<any, z.ZodTypeDef, any>, "many">;
65
+ }, "strict", z.ZodTypeAny, {
66
+ type: "cycle";
67
+ name: string;
68
+ steps: any[];
69
+ description?: string | undefined;
70
+ cycles?: number | "Infinity" | undefined;
71
+ observer?: string | undefined;
72
+ }, {
73
+ type: "cycle";
74
+ name: string;
75
+ steps: any[];
76
+ description?: string | undefined;
77
+ cycles?: number | "Infinity" | undefined;
78
+ observer?: string | undefined;
79
+ }>;
80
+ /** Broadcast flow — fan-out where all steps receive the same message. */
81
+ export declare const BroadcastFlowDescriptionSchema: z.ZodObject<{
82
+ type: z.ZodLiteral<"broadcast">;
83
+ /**
84
+ * Separator used to join step responses.
85
+ * @default "\n\n"
86
+ */
87
+ separator: z.ZodOptional<z.ZodString>;
88
+ /** Unique name for this flow. */
89
+ name: z.ZodString;
90
+ /** Optional human-readable description. */
91
+ description: z.ZodOptional<z.ZodString>;
92
+ /** Steps to execute — agent references or nested flow definitions. */
93
+ steps: z.ZodArray<z.ZodType<any, z.ZodTypeDef, any>, "many">;
94
+ }, "strict", z.ZodTypeAny, {
95
+ type: "broadcast";
96
+ name: string;
97
+ steps: any[];
98
+ description?: string | undefined;
99
+ separator?: string | undefined;
100
+ }, {
101
+ type: "broadcast";
102
+ name: string;
103
+ steps: any[];
104
+ description?: string | undefined;
105
+ separator?: string | undefined;
106
+ }>;
107
+ /**
108
+ * Flow description schema — validates a standalone flow description file.
109
+ *
110
+ * Discriminated union on `type` supporting sequential, cycle, and broadcast flows.
111
+ *
112
+ * @example
113
+ * ```yaml
114
+ * name: code-review
115
+ * type: sequential
116
+ * steps:
117
+ * - agent: writer
118
+ * - agent: reviewer
119
+ * - agent: editor
120
+ * ```
121
+ */
122
+ export declare const FlowDescriptionSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
123
+ type: z.ZodLiteral<"sequential">;
124
+ /** Unique name for this flow. */
125
+ name: z.ZodString;
126
+ /** Optional human-readable description. */
127
+ description: z.ZodOptional<z.ZodString>;
128
+ /** Steps to execute — agent references or nested flow definitions. */
129
+ steps: z.ZodArray<z.ZodType<any, z.ZodTypeDef, any>, "many">;
130
+ }, "strict", z.ZodTypeAny, {
131
+ type: "sequential";
132
+ name: string;
133
+ steps: any[];
134
+ description?: string | undefined;
135
+ }, {
136
+ type: "sequential";
137
+ name: string;
138
+ steps: any[];
139
+ description?: string | undefined;
140
+ }>, z.ZodObject<{
141
+ type: z.ZodLiteral<"cycle">;
142
+ /**
143
+ * Number of cycle iterations. Use `"Infinity"` for infinite loops.
144
+ * @default 1
145
+ */
146
+ cycles: z.ZodOptional<z.ZodUnion<[z.ZodNumber, z.ZodLiteral<"Infinity">]>>;
147
+ /** Name of an observer agent that runs after each cycle. */
148
+ observer: z.ZodOptional<z.ZodString>;
149
+ /** Unique name for this flow. */
150
+ name: z.ZodString;
151
+ /** Optional human-readable description. */
152
+ description: z.ZodOptional<z.ZodString>;
153
+ /** Steps to execute — agent references or nested flow definitions. */
154
+ steps: z.ZodArray<z.ZodType<any, z.ZodTypeDef, any>, "many">;
155
+ }, "strict", z.ZodTypeAny, {
156
+ type: "cycle";
157
+ name: string;
158
+ steps: any[];
159
+ description?: string | undefined;
160
+ cycles?: number | "Infinity" | undefined;
161
+ observer?: string | undefined;
162
+ }, {
163
+ type: "cycle";
164
+ name: string;
165
+ steps: any[];
166
+ description?: string | undefined;
167
+ cycles?: number | "Infinity" | undefined;
168
+ observer?: string | undefined;
169
+ }>, z.ZodObject<{
170
+ type: z.ZodLiteral<"broadcast">;
171
+ /**
172
+ * Separator used to join step responses.
173
+ * @default "\n\n"
174
+ */
175
+ separator: z.ZodOptional<z.ZodString>;
176
+ /** Unique name for this flow. */
177
+ name: z.ZodString;
178
+ /** Optional human-readable description. */
179
+ description: z.ZodOptional<z.ZodString>;
180
+ /** Steps to execute — agent references or nested flow definitions. */
181
+ steps: z.ZodArray<z.ZodType<any, z.ZodTypeDef, any>, "many">;
182
+ }, "strict", z.ZodTypeAny, {
183
+ type: "broadcast";
184
+ name: string;
185
+ steps: any[];
186
+ description?: string | undefined;
187
+ separator?: string | undefined;
188
+ }, {
189
+ type: "broadcast";
190
+ name: string;
191
+ steps: any[];
192
+ description?: string | undefined;
193
+ separator?: string | undefined;
194
+ }>]>;
195
+ export type FlowDescription = z.infer<typeof FlowDescriptionSchema>;
@@ -0,0 +1,27 @@
1
+ import type { Agent } from "../../agents/agent/agent.types";
2
+ import type { FlowHooks } from "../flow/flow.types";
3
+ /**
4
+ * Options for loading a flow from a description file.
5
+ *
6
+ * The `agents` registry is required — it maps agent names referenced in
7
+ * the flow steps to live `Agent` instances. Agents can be created via
8
+ * `createAgent()`, `loadAgent()`, or any other factory.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * import { loadAgent, loadFlow } from "@comma-agents/core";
13
+ *
14
+ * const writer = await loadAgent("./agents/writer.yaml");
15
+ * const reviewer = await loadAgent("./agents/reviewer.yaml");
16
+ *
17
+ * const flow = await loadFlow("./flows/review-pipeline.yaml", {
18
+ * agents: { writer, reviewer },
19
+ * });
20
+ * ```
21
+ */
22
+ export interface LoadFlowOptions {
23
+ /** Registry of named agents that steps can reference. */
24
+ readonly agents: Readonly<Record<string, Agent>>;
25
+ /** Optional hooks to inject into the loaded flow. */
26
+ readonly flowHooks?: FlowHooks;
27
+ }
@@ -0,0 +1,17 @@
1
+ import type { SandboxTrashMetadata } from "../tools/io/trash";
2
+ import type { Guard, GuardCallbacks, Policy } from "./guard.types";
3
+ /**
4
+ * Create a per-tool Guard instance.
5
+ *
6
+ * The guard resolves paths, enforces the jail boundary, evaluates the
7
+ * policy chain, dispatches "ask" via the callbacks, and self-updates
8
+ * session memory when the user grants/denies access.
9
+ *
10
+ * @param toolName - The tool this guard enforces.
11
+ * @param cwd - Workspace root for path resolution.
12
+ * @param allowAbsolutePaths - Whether absolute input paths are allowed.
13
+ * @param jail - Whether to enforce the cwd boundary.
14
+ * @param policies - Initial policy chain.
15
+ * @param callbacks - onAsk / onPolicyChange handlers (shared across guards).
16
+ */
17
+ export declare function createGuard(toolName: string, cwd: string, allowAbsolutePaths: boolean, jail: boolean, policies: readonly Policy[], callbacks?: GuardCallbacks, trashMetadata?: SandboxTrashMetadata): Guard;
@@ -0,0 +1,112 @@
1
+ import type { PermissionDecision, PermissionOperation } from "../sandbox/sandbox.types";
2
+ import type { SandboxTrashMetadata } from "../tools/io/trash";
3
+ /** Closed set of access categories that a tool can request. */
4
+ export type AccessType = "fs.read" | "fs.write" | "command.execute";
5
+ /** Describes what a tool wants to do. Evaluated against the guard's policy chain. */
6
+ export interface AccessRequest {
7
+ /** Category of access: file read, file write, or command execution. */
8
+ readonly type: AccessType;
9
+ /** The path, command, or other resource being accessed. */
10
+ readonly resource: string;
11
+ }
12
+ /** A policy's evaluation result. "pass" means "skip me, let the next policy decide." */
13
+ export type PolicyDecision = "allow" | "deny" | "ask" | "pass";
14
+ /**
15
+ * Pure evaluation function — stateless, composable, testable.
16
+ * Returns "pass" when the policy doesn't handle this request type.
17
+ */
18
+ export interface Policy {
19
+ readonly name: string;
20
+ evaluate(request: AccessRequest): PolicyDecision | Promise<PolicyDecision>;
21
+ }
22
+ /**
23
+ * Context forwarded by a tool when it calls `guard.authorize()`.
24
+ */
25
+ export interface AuthorizationContext {
26
+ readonly agentName: string;
27
+ readonly toolName: string;
28
+ readonly signal: AbortSignal;
29
+ }
30
+ /**
31
+ * Permission request dispatched to the guard's `onAsk` callback when a
32
+ * policy returns "ask".
33
+ */
34
+ export interface GuardPermissionRequest {
35
+ readonly agentName: string;
36
+ readonly toolName: string;
37
+ readonly operation: PermissionOperation | AccessType;
38
+ readonly resource: string;
39
+ readonly reason: "policy-ask";
40
+ readonly signal?: AbortSignal;
41
+ }
42
+ /** Snapshot of the guard's current policy chain. */
43
+ export interface GuardPolicySnapshot {
44
+ readonly toolName: string;
45
+ readonly policies: ReadonlyArray<{
46
+ readonly name: string;
47
+ }>;
48
+ }
49
+ /** Callbacks injected into every guard at creation time. */
50
+ /** Callbacks injected into every guard at creation time. */
51
+ /** Callbacks injected into every guard at creation time. */
52
+ export interface GuardCallbacks {
53
+ /**
54
+ * Called when the policy chain returns "ask". Returns the user's decision.
55
+ * If absent and a policy returns "ask", the guard fails-closed.
56
+ */
57
+ readonly onAsk?: (request: GuardPermissionRequest) => Promise<PermissionDecision>;
58
+ /**
59
+ * Called whenever the guard's policy chain changes (addPolicy/removePolicy
60
+ * or session-memory self-update). The snapshot includes the tool name and
61
+ * the ordered policy chain.
62
+ */
63
+ readonly onPolicyChange?: (snapshot: GuardPolicySnapshot) => void;
64
+ /**
65
+ * Called when a tool requests direct user input/feedback.
66
+ */
67
+ readonly onQuestion?: (request: {
68
+ readonly agentName: string;
69
+ readonly toolName: string;
70
+ readonly question: string;
71
+ }) => Promise<string>;
72
+ }
73
+ /**
74
+ * Per-tool enforcement engine.
75
+ *
76
+ * Created lazily by the Sandbox for each tool. Handles path resolution,
77
+ * jail enforcement, policy chain evaluation, "ask" dispatch, and
78
+ * self-updating session memory.
79
+ */
80
+ export interface Guard {
81
+ /** The tool this guard enforces. */
82
+ readonly toolName: string;
83
+ /** Workspace root for path resolution. */
84
+ readonly cwd: string;
85
+ /** Trash metadata for archive identification (runId, sessionId). */
86
+ readonly trashMetadata?: SandboxTrashMetadata;
87
+ /** Trash metadata for archive identification (runId, sessionId). */
88
+ readonly trashMetadata?: SandboxTrashMetadata;
89
+ /**
90
+ * Full authorization through the policy chain.
91
+ * Resolves the path, checks jail, evaluates policies, dispatches "ask"
92
+ * if needed. Returns the resolved absolute path on success.
93
+ *
94
+ * @throws {SandboxViolationError} on deny, jail escape, or forbidden glob.
95
+ */
96
+ authorize(request: AccessRequest, ctx: AuthorizationContext): Promise<string>;
97
+ /**
98
+ * Non-throwing inspection. Returns false when the policy chain would deny.
99
+ * Never dispatches "ask" — treats "ask" the same as "deny".
100
+ */
101
+ canAccess(request: AccessRequest): boolean;
102
+ /** Insert a policy into the chain, optionally before a named policy. */
103
+ addPolicy(policy: Policy, before?: string): void;
104
+ /** Remove a policy by name. Returns true if it was found and removed. */
105
+ removePolicy(name: string): boolean;
106
+ /** Snapshot of the current policy chain for broadcasting. */
107
+ getPolicies(): GuardPolicySnapshot;
108
+ /** Subscribe to policy chain changes. Returns an unsubscribe function. */
109
+ onPolicyChange(listener: (snapshot: GuardPolicySnapshot) => void): () => void;
110
+ /** Ask the user a question / prompt for interactive feedback. */
111
+ askQuestion(question: string, ctx: AuthorizationContext): Promise<string>;
112
+ }
@@ -0,0 +1,3 @@
1
+ export { createGuard } from "./guard";
2
+ export type { AccessRequest, AccessType, AuthorizationContext, Guard, GuardCallbacks, GuardPermissionRequest, GuardPolicySnapshot, Policy, PolicyDecision, } from "./guard.types";
3
+ export { approveCommandsPolicy, buildDefaultPolicies, denyCommandsPolicy, forbiddenGlobsPolicy, pathPolicy, } from "./policies";
@@ -0,0 +1,28 @@
1
+ import type { PathPolicy } from "../sandbox/sandbox.types";
2
+ import type { Policy } from "./guard.types";
3
+ /**
4
+ * Unconditionally deny paths matching secret/sensitive glob patterns.
5
+ * Runs before read/write policies; cannot be overridden.
6
+ */
7
+ export declare function forbiddenGlobsPolicy(globs: readonly string[], cwd: string): Policy;
8
+ /**
9
+ * deny > allow > default for a class of file-system operations.
10
+ * Respects the evaluation order: deny wins over allow, allow wins over default.
11
+ */
12
+ export declare function pathPolicy(mode: "read" | "write", config: PathPolicy | undefined, cwd: string): Policy;
13
+ /**
14
+ * Always-deny specific commands (regex patterns).
15
+ * Runs during the guard's policy chain for "command.execute" requests.
16
+ */
17
+ export declare function denyCommandsPolicy(patterns: readonly string[]): Policy;
18
+ /**
19
+ * Ask (prompt the user) for commands matching approval patterns.
20
+ * Returns "ask" when a command matches; the guard's onAsk callback handles the prompt.
21
+ */
22
+ export declare function approveCommandsPolicy(patterns: readonly string[]): Policy;
23
+ /**
24
+ * Build the default policy chain for a new guard.
25
+ * Order: forbiddenGlobs → readPath → writePath.
26
+ * Tool-specific policies should be added after these.
27
+ */
28
+ export declare function buildDefaultPolicies(forbiddenGlobs: readonly string[], read: PathPolicy | undefined, write: PathPolicy | undefined, cwd: string): Policy[];
@@ -0,0 +1,2 @@
1
+ export { createTokenTracker, useTokenTracking } from "./token-tracking";
2
+ export type { ModelMetadata, TokenSnapshot, TokenTracker, TokenTrackerConfig, TokenUsageRecord, UseTokenTrackingConfig, } from "./token-tracking.types";
@@ -0,0 +1,14 @@
1
+ import type { ModelMetadata } from "./token-tracking.types";
2
+ /**
3
+ * Built-in model catalog mapping model identifiers to context window metadata.
4
+ *
5
+ * TODO: Determine if this can be queried somehow to have a more generalized
6
+ * way of getting this data.
7
+ *
8
+ * Keys are `"providerID/modelID"` strings matching the strategy model format.
9
+ * Values are frozen ModelMetadata objects.
10
+ *
11
+ * This catalog covers widely-used models. For models not listed, pass
12
+ * `modelMetadata` explicitly to `createTokenTracker()`.
13
+ */
14
+ export declare const MODEL_CATALOG: Readonly<Record<string, ModelMetadata>>;
@@ -0,0 +1,87 @@
1
+ import type { Agent } from "../../../agents/agent/agent.types";
2
+ import type { TokenTracker, TokenTrackerConfig, UseTokenTrackingConfig } from "./token-tracking.types";
3
+ /**
4
+ * Create a new token usage tracker.
5
+ *
6
+ * The tracker accumulates real token counts from the AI SDK (not estimates).
7
+ * When model metadata is available (via the built-in catalog or explicit config),
8
+ * snapshots include context budget information.
9
+ *
10
+ * Typically you should use the higher-level `useTokenTracking()` hook, which
11
+ * creates a tracker, auto-detects the model, and installs the `afterCallResult`
12
+ * hook on the agent automatically. Use `createTokenTracker()` directly when you
13
+ * need a standalone tracker or want full control over hook wiring.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { useTokenTracking } from "@comma-agents/core";
18
+ *
19
+ * // Recommended: auto-detects model from agent config
20
+ * const tracker = useTokenTracking(agent);
21
+ *
22
+ * await agent.call("Hello");
23
+ * const snap = tracker.snapshot();
24
+ * console.log(`${snap.totalTokens} tokens used`);
25
+ * console.log(`Context ${(snap.contextUsagePercent! * 100).toFixed(1)}% full`);
26
+ * console.log(`${snap.contextRemaining} tokens remaining`);
27
+ * ```
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * // With explicit metadata for a model not in the catalog
32
+ * const tracker = createTokenTracker({
33
+ * modelMetadata: { contextWindow: 32_000, maxOutputTokens: 4_096 },
34
+ * });
35
+ * ```
36
+ *
37
+ * @example
38
+ * ```ts
39
+ * // Without model metadata — usage tracking only, no budget info
40
+ * const tracker = createTokenTracker({});
41
+ * ```
42
+ */
43
+ export declare function createTokenTracker(config?: TokenTrackerConfig): TokenTracker;
44
+ /**
45
+ * Hook token tracking into an agent.
46
+ *
47
+ * Creates a `TokenTracker`, installs an `afterCallResult` hook on the agent
48
+ * that records token usage after each call, and returns the tracker.
49
+ *
50
+ * The model's context window metadata is resolved in this order:
51
+ * 1. Explicit `config.modelMetadata` (if provided)
52
+ * 2. Explicit `config.model` string (catalog lookup)
53
+ * 3. Auto-detected from `agent.config.model` (already a "providerID/modelID" string)
54
+ *
55
+ * @param agent - An agent created by `createAgent()`.
56
+ * @param config - Optional configuration to override model detection.
57
+ * @returns A `TokenTracker` that accumulates usage from subsequent agent calls.
58
+ *
59
+ * @example
60
+ * ```ts
61
+ * import { createAgent, useTokenTracking } from "@comma-agents/core";
62
+ *
63
+ * const agent = createAgent({
64
+ * name: "writer",
65
+ * model: "openai/gpt-4o",
66
+ * systemPrompt: "You are helpful.",
67
+ * });
68
+ *
69
+ * // Auto-detects model, looks up catalog for context window info
70
+ * const tracker = useTokenTracking(agent);
71
+ *
72
+ * await agent.call("Hello");
73
+ * const snap = tracker.snapshot();
74
+ * console.log(`Used ${snap.totalTokens} tokens`);
75
+ * console.log(`Context ${(snap.contextUsagePercent! * 100).toFixed(1)}% full`);
76
+ * console.log(`${snap.contextRemaining} tokens remaining`);
77
+ * ```
78
+ *
79
+ * @example
80
+ * ```ts
81
+ * // Explicit metadata for a model not in the catalog
82
+ * const tracker = useTokenTracking(agent, {
83
+ * modelMetadata: { contextWindow: 32_000, maxOutputTokens: 4_096 },
84
+ * });
85
+ * ```
86
+ */
87
+ export declare function useTokenTracking(agent: Agent, config?: UseTokenTrackingConfig): TokenTracker;