@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,111 @@
1
+ import type { AssistantModelMessage, ModelMessage, ToolModelMessage, UserModelMessage } from "ai";
2
+ import type { ContextPrepareInput, ContextRetentionOptions, ConversationRetentionEvent } from "./retention/retention.types";
3
+ export type { AssistantModelMessage, ModelMessage, ToolModelMessage, UserModelMessage, } from "ai";
4
+ export type { CompactionOptions, ContextPrepareInput, ContextRecordTransform, ContextRetentionOptions, ContextTransformInput, ConversationRetentionEvent, RollingWindowOptions, SummarizeRecords, } from "./retention/retention.types";
5
+ export type ResponseMessage = AssistantModelMessage | ToolModelMessage;
6
+ /**
7
+ * Whether a record is part of the agent's effective (LLM-visible) context.
8
+ * `superseded` records are retained for export/audit but excluded from the
9
+ * message projection.
10
+ */
11
+ export type ConversationRecordStatus = "active" | "superseded";
12
+ export interface ConversationUsage {
13
+ /** Tokens used by the prompt/input side of the agent call. */
14
+ readonly promptTokens: number;
15
+ /** Tokens generated by the model output side of the agent call. */
16
+ readonly completionTokens: number;
17
+ }
18
+ export interface ContextUsage {
19
+ /** Tokens occupying the final model step's context window. */
20
+ readonly totalTokens: number;
21
+ /** Input tokens in the final model step, when reported. */
22
+ readonly inputTokens?: number;
23
+ /** Output tokens in the final model step, when reported. */
24
+ readonly outputTokens?: number;
25
+ /** Detailed input-token categories reported by the model provider. */
26
+ readonly inputTokenDetails?: {
27
+ readonly noCacheTokens?: number;
28
+ readonly cacheReadTokens?: number;
29
+ readonly cacheWriteTokens?: number;
30
+ };
31
+ /** Detailed output-token categories reported by the model provider. */
32
+ readonly outputTokenDetails?: {
33
+ readonly textTokens?: number;
34
+ readonly reasoningTokens?: number;
35
+ };
36
+ }
37
+ export interface ConversationRecord {
38
+ /** Stable identifier for this single agent call record. */
39
+ readonly id: string;
40
+ /** Agent that owns this context record. */
41
+ readonly agentName: string;
42
+ /** ISO timestamp for when the record was created. */
43
+ readonly createdAt: string;
44
+ /** User message sent to the agent for this call. */
45
+ readonly userMessage: UserModelMessage;
46
+ /** Assistant and tool messages returned by the AI SDK for this call. */
47
+ readonly responseMessages: readonly ResponseMessage[];
48
+ /** Final text returned by the agent. */
49
+ readonly text: string;
50
+ /** Exact token usage reported by the provider for this call. */
51
+ readonly usage: ConversationUsage;
52
+ /** Final model-step context usage. */
53
+ readonly contextUsage?: ContextUsage;
54
+ /** Provider finish reason for this call. */
55
+ readonly finishReason: string;
56
+ /**
57
+ * Whether this record feeds the agent's effective context.
58
+ * @default "active"
59
+ */
60
+ readonly status?: ConversationRecordStatus;
61
+ /** Id of the summary record that replaced this one, when superseded. */
62
+ readonly supersededBy?: string;
63
+ }
64
+ export interface CreateConversationRecordInput {
65
+ /** Stable identifier for this record. Defaults to a random UUID. */
66
+ readonly id?: string;
67
+ /** Agent that owns this context record. */
68
+ readonly agentName: string;
69
+ /** Creation timestamp. Defaults to the current time. */
70
+ readonly createdAt?: string;
71
+ /** User input sent to the agent. */
72
+ readonly userMessage: string | UserModelMessage;
73
+ /** Assistant and tool messages returned by the AI SDK for this call. */
74
+ readonly responseMessages: readonly ResponseMessage[];
75
+ /** Final text returned by the agent. */
76
+ readonly text: string;
77
+ /** Exact token usage reported by the provider for this call. */
78
+ readonly usage: ConversationUsage;
79
+ /** Final model-step context usage. */
80
+ readonly contextUsage?: ContextUsage;
81
+ /** Provider finish reason for this call. */
82
+ readonly finishReason: string;
83
+ }
84
+ export interface ConversationHistory {
85
+ /** Ordered conversation records across the run. */
86
+ readonly records: readonly ConversationRecord[];
87
+ /** Retention events that changed the effective model context. */
88
+ readonly retentionEvents: readonly ConversationRetentionEvent[];
89
+ }
90
+ /** Options for {@link createConversationContext}. */
91
+ export interface ConversationContextOptions extends ContextRetentionOptions {
92
+ }
93
+ export interface ConversationContext {
94
+ readonly length: number;
95
+ readonly isEmpty: boolean;
96
+ appendRecord(record: ConversationRecord): void;
97
+ records(agentName?: string): readonly ConversationRecord[];
98
+ messages(agentName?: string): readonly ModelMessage[];
99
+ /** Prepare records before a model call by applying configured retention. */
100
+ prepareForCall(input: ContextPrepareInput): Promise<readonly ConversationRetentionEvent[]>;
101
+ importRecords(records: readonly ConversationRecord[]): void;
102
+ exportRecords(): readonly ConversationRecord[];
103
+ importJsonl(jsonl: string): void;
104
+ exportJsonl(): string;
105
+ importJson(json: string): void;
106
+ exportJson(): string;
107
+ importYaml(yaml: string): void;
108
+ exportYaml(): string;
109
+ clear(): void;
110
+ [Symbol.iterator](): Iterator<ConversationRecord>;
111
+ }
@@ -0,0 +1,6 @@
1
+ import type { UserModelMessage } from "ai";
2
+ import type { ConversationRecord } from "./conversation-context.types";
3
+ /** Wrap plain text in the AI SDK user-message shape. */
4
+ export declare function toUserMessage(text: string | UserModelMessage): UserModelMessage;
5
+ /** Check the minimal public record fields expected by the context contract. */
6
+ export declare function isConversationRecord(value: unknown): value is ConversationRecord;
@@ -0,0 +1,3 @@
1
+ export { contextUsageFromSteps, createConversationContext, createConversationRecord, parseConversationJson, parseConversationJsonl, parseConversationYaml, recordsToMessages, recordToJsonlLine, serializeConversationRecords, serializeConversationRecordsJson, serializeConversationRecordsYaml, } from "./conversation-context";
2
+ export type { AssistantModelMessage, CompactionOptions, ContextPrepareInput, ContextRecordTransform, ContextRetentionOptions, ContextTransformInput, ContextUsage, ConversationContext, ConversationContextOptions, ConversationHistory, ConversationRecord, ConversationRecordStatus, ConversationRetentionEvent, ConversationUsage, CreateConversationRecordInput, ModelMessage, ResponseMessage, RollingWindowOptions, SummarizeRecords, ToolModelMessage, UserModelMessage, } from "./conversation-context.types";
3
+ export { applyCompaction, applyRollingWindow, prepareContextRecords, } from "./retention";
@@ -0,0 +1,3 @@
1
+ export declare const DEFAULT_COMPACTION_KEEP_RECENT = 8;
2
+ export declare const DEFAULT_COMPACTION_THRESHOLD_RATIO = 0.85;
3
+ export declare const COMPACTION_SUMMARY_REQUEST = "[Earlier conversation compacted - summary follows.]";
@@ -0,0 +1,21 @@
1
+ import type { ConversationRecord } from "../../conversation-context.types";
2
+ import type { ConversationRetentionEvent } from "../retention.types";
3
+ import type { ApplyCompactionInput } from "./compaction.types";
4
+ /**
5
+ * Compact older active records into one summary record.
6
+ *
7
+ * @param input - Full retained record history, options, agent name, and trigger metadata.
8
+ * @example
9
+ * ```ts
10
+ * const result = await applyCompaction({
11
+ * records,
12
+ * options: { keepRecent: 8, summarize },
13
+ * agentName: "assistant",
14
+ * trigger: { contextUsage: { totalTokens: 90_000 }, tokenLimit: 100_000 },
15
+ * });
16
+ * ```
17
+ */
18
+ export declare function applyCompaction(input: ApplyCompactionInput): Promise<{
19
+ readonly records: readonly ConversationRecord[];
20
+ readonly event?: ConversationRetentionEvent;
21
+ }>;
@@ -0,0 +1,26 @@
1
+ import type { ConversationRecord } from "../../conversation-context.types";
2
+ import type { ConversationRetentionTrigger } from "../retention.types";
3
+ /** Summarize a span of records into a single plain-text summary. */
4
+ export type SummarizeRecords = (records: readonly ConversationRecord[]) => Promise<string>;
5
+ /** Configuration for compacting older records into one summary record. */
6
+ export interface CompactionOptions {
7
+ /** Number of most-recent active records kept verbatim. @default 8 */
8
+ readonly keepRecent?: number;
9
+ /** Active-record count that triggers compaction. @default keepRecent * 2 */
10
+ readonly threshold?: number;
11
+ /** Context usage ratio that triggers compaction. @default 0.85 */
12
+ readonly thresholdRatio?: number;
13
+ /** Summarizer used to fold older records into one summary. */
14
+ readonly summarize?: SummarizeRecords;
15
+ }
16
+ /** Input for applying compaction to full retained record history. */
17
+ export interface ApplyCompactionInput {
18
+ /** Full retained record history. */
19
+ readonly records: readonly ConversationRecord[];
20
+ /** Compaction configuration and summarizer. */
21
+ readonly options: CompactionOptions;
22
+ /** Agent that owns the synthetic summary record. */
23
+ readonly agentName: string;
24
+ /** Metadata used to decide whether compaction should run. */
25
+ readonly trigger: ConversationRetentionTrigger;
26
+ }
@@ -0,0 +1,2 @@
1
+ export { applyCompaction } from "./compaction";
2
+ export type { CompactionOptions, SummarizeRecords } from "./compaction.types";
@@ -0,0 +1,4 @@
1
+ export { applyCompaction } from "./compaction";
2
+ export { prepareContextRecords } from "./retention";
3
+ export type { CompactionOptions, ContextPrepareInput, ContextRecordTransform, ContextRetentionOptions, ContextRetentionResult, ContextTransformInput, ConversationRetentionEvent, ConversationRetentionTrigger, RollingWindowOptions, SummarizeRecords, } from "./retention.types";
4
+ export { applyRollingWindow } from "./rolling-window";
@@ -0,0 +1,9 @@
1
+ import type { ContextRetentionOptions, ContextRetentionResult, ContextTransformInput } from "./retention.types";
2
+ /**
3
+ * Prepare conversation records before an agent call by applying built-in
4
+ * retention options and any custom transforms.
5
+ *
6
+ * @param options - Context retention and transform options.
7
+ * @param input - Current records plus the owning agent name.
8
+ */
9
+ export declare function prepareContextRecords(options: ContextRetentionOptions, input: ContextTransformInput): Promise<ContextRetentionResult>;
@@ -0,0 +1,90 @@
1
+ import type { ContextUsage, ConversationRecord } from "../conversation-context.types";
2
+ import type { CompactionOptions } from "./compaction";
3
+ import type { RollingWindowOptions } from "./rolling-window";
4
+ export type { CompactionOptions, SummarizeRecords } from "./compaction";
5
+ export type { RollingWindowOptions } from "./rolling-window";
6
+ /** Input used when preparing records for an agent call. */
7
+ export interface ContextPrepareInput {
8
+ readonly agentName: string;
9
+ readonly model?: string;
10
+ readonly contextUsage?: ContextUsage;
11
+ readonly contextWindow?: number;
12
+ readonly maxInputTokens?: number;
13
+ }
14
+ /** Input handed to a custom record transform. */
15
+ export interface ContextTransformInput extends ContextPrepareInput {
16
+ readonly records: readonly ConversationRecord[];
17
+ }
18
+ /** Custom record transform used for behavior beyond built-in retention. */
19
+ export type ContextRecordTransform = (input: ContextTransformInput) => readonly ConversationRecord[] | Promise<readonly ConversationRecord[]>;
20
+ /** Metadata explaining why retention ran for an agent context. */
21
+ export interface ConversationRetentionTrigger {
22
+ /** Provider/model identifier involved in the retention decision. */
23
+ readonly model?: string;
24
+ /** Final-step context usage from the previous model call. */
25
+ readonly contextUsage?: ContextUsage;
26
+ /** Token limit used for the threshold comparison. */
27
+ readonly tokenLimit?: number;
28
+ /** Provider context window, when known. */
29
+ readonly contextWindow?: number;
30
+ /** Provider max input tokens, when known. */
31
+ readonly maxInputTokens?: number;
32
+ /** Observed `contextUsage.totalTokens / tokenLimit`, when token metadata is known. */
33
+ readonly ratio?: number;
34
+ /** Ratio threshold that triggers default compaction. */
35
+ readonly thresholdRatio?: number;
36
+ /** Active record count that triggered explicit count-based compaction. */
37
+ readonly activeRecordCount?: number;
38
+ /** Configured active-record threshold for explicit count-based compaction. */
39
+ readonly recordThreshold?: number;
40
+ }
41
+ /** Event emitted when retention changes an agent's effective context. */
42
+ export interface ConversationRetentionEvent {
43
+ /** Stable identifier for this retention event. */
44
+ readonly id: string;
45
+ /** Agent whose context was changed. */
46
+ readonly agentName: string;
47
+ /** ISO timestamp for when retention ran. */
48
+ readonly createdAt: string;
49
+ /** Retention operation that ran. */
50
+ readonly kind: "compaction";
51
+ /** Why the operation was triggered. */
52
+ readonly reason: "context-window" | "record-count";
53
+ /** Trigger metadata used to decide whether retention should run. */
54
+ readonly trigger: ConversationRetentionTrigger;
55
+ /** Number of active records compacted into the summary. */
56
+ readonly recordsCompacted: number;
57
+ /** Number of recent active records retained verbatim. */
58
+ readonly recordsRetained: number;
59
+ /** Synthetic summary record inserted into the active context. */
60
+ readonly summaryRecord: ConversationRecord;
61
+ /** Ids of records tombstoned by this retention event. */
62
+ readonly supersededRecordIds: readonly string[];
63
+ /** Existing active record before which the summary was inserted. */
64
+ readonly insertBeforeRecordId?: string;
65
+ }
66
+ /** Result of applying configured context retention. */
67
+ export interface ContextRetentionResult {
68
+ /** Updated full record list, including tombstoned records. */
69
+ readonly records: readonly ConversationRecord[];
70
+ /** Retention events emitted while preparing the context. */
71
+ readonly events: readonly ConversationRetentionEvent[];
72
+ }
73
+ /** Retention and compaction options for a conversation context. */
74
+ export interface ContextRetentionOptions {
75
+ /**
76
+ * Keep only the most recent active records visible to the model. A number is
77
+ * shorthand for `{ maxRecords: number }`.
78
+ */
79
+ readonly rollingWindow?: number | RollingWindowOptions;
80
+ /**
81
+ * Compact older active records into a summary. `true` enables default
82
+ * thresholds; an object customizes them.
83
+ */
84
+ readonly compaction?: boolean | CompactionOptions;
85
+ /**
86
+ * Optional programmatic extension point that runs after built-in rolling and
87
+ * compaction behavior.
88
+ */
89
+ readonly transformRecords?: ContextRecordTransform | readonly ContextRecordTransform[];
90
+ }
@@ -0,0 +1,3 @@
1
+ import type { ConversationRecord } from "../conversation-context.types";
2
+ /** Records that are still part of the agent's effective (LLM-visible) context. */
3
+ export declare function activeRecords(records: readonly ConversationRecord[]): readonly ConversationRecord[];
@@ -0,0 +1,2 @@
1
+ export { applyRollingWindow } from "./rolling-window";
2
+ export type { RollingWindowOptions } from "./rolling-window.types";
@@ -0,0 +1,13 @@
1
+ import type { ConversationRecord } from "../../conversation-context.types";
2
+ import type { RollingWindowOptions } from "./rolling-window.types";
3
+ /**
4
+ * Apply a rolling active-record window without deleting exported records.
5
+ *
6
+ * @param records - Full retained record history.
7
+ * @param options - Rolling-window configuration.
8
+ * @example
9
+ * ```ts
10
+ * const prepared = applyRollingWindow(records, { maxRecords: 40 });
11
+ * ```
12
+ */
13
+ export declare function applyRollingWindow(records: readonly ConversationRecord[], options: RollingWindowOptions): readonly ConversationRecord[];
@@ -0,0 +1,5 @@
1
+ /** Configuration for rolling active records out of the LLM-visible context. */
2
+ export interface RollingWindowOptions {
3
+ /** Maximum active records kept visible to the model. */
4
+ readonly maxRecords: number;
5
+ }
@@ -0,0 +1,21 @@
1
+ import type { CredentialBackend } from "../credentials.types";
2
+ export interface JsonFileBackendOptions {
3
+ /** Absolute path to the credentials JSON file. */
4
+ readonly filePath: string;
5
+ }
6
+ /**
7
+ * Creates a JSON file-backed credential storage backend.
8
+ *
9
+ * - Creates parent directories on first write.
10
+ * - Sets file permissions to 0o600 (owner read/write only).
11
+ * - Returns empty data when the file doesn't exist.
12
+ * - Returns empty data when the file contains invalid JSON or fails
13
+ * schema validation (logs a warning but doesn't throw).
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * const backend = createJsonFileBackend({ filePath: "/home/user/.config/credentials.json" });
18
+ * const data = await backend.readAll();
19
+ * ```
20
+ */
21
+ export declare function createJsonFileBackend(options: JsonFileBackendOptions): CredentialBackend;
@@ -0,0 +1,9 @@
1
+ import type { EnvVarMap } from "./credentials.types";
2
+ /**
3
+ * Well-known environment variable names for common AI providers.
4
+ *
5
+ * This is intentionally a subset — strategies can specify custom
6
+ * env var names via the strategy schema, and users can extend
7
+ * this map via configuration.
8
+ */
9
+ export declare const WELL_KNOWN_ENV_VARS: EnvVarMap;
@@ -0,0 +1,18 @@
1
+ import type { CreateCredentialStoreOptions, CredentialStore } from "./credentials.types";
2
+ /**
3
+ * Create a credential store backed by the given storage backend.
4
+ *
5
+ * The store provides layered credential resolution:
6
+ * 1. Strategy-scoped credential (if `scope` is a strategy name, not `"$global"`)
7
+ * 2. Environment variable (well-known + custom mappings)
8
+ * 3. Global-scoped credential (`"$global"`)
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * const store = createCredentialStore({
13
+ * backend: createJsonFileBackend({ filePath: "/path/to/credentials.json" }),
14
+ * });
15
+ * const credential = await store.resolve("openai", "my-strategy");
16
+ * ```
17
+ */
18
+ export declare function createCredentialStore(options: CreateCredentialStoreOptions): CredentialStore;
@@ -0,0 +1,102 @@
1
+ import { z } from "zod";
2
+ /** API key credential — simple secret string. */
3
+ export declare const ApiCredentialSchema: z.ZodObject<{
4
+ type: z.ZodLiteral<"api">;
5
+ key: z.ZodString;
6
+ }, "strip", z.ZodTypeAny, {
7
+ type: "api";
8
+ key: string;
9
+ }, {
10
+ type: "api";
11
+ key: string;
12
+ }>;
13
+ /** OAuth 2.0 credential — access + refresh tokens with expiry. */
14
+ export declare const OAuthCredentialSchema: z.ZodObject<{
15
+ type: z.ZodLiteral<"oauth">;
16
+ accessToken: z.ZodString;
17
+ refreshToken: z.ZodOptional<z.ZodString>;
18
+ /** ISO-8601 datetime when the access token expires. */
19
+ expiresAt: z.ZodOptional<z.ZodString>;
20
+ /** Provider account identifier (e.g. GitHub username). */
21
+ accountId: z.ZodOptional<z.ZodString>;
22
+ /** Arbitrary provider-specific metadata. */
23
+ metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
24
+ }, "strip", z.ZodTypeAny, {
25
+ type: "oauth";
26
+ accessToken: string;
27
+ refreshToken?: string | undefined;
28
+ expiresAt?: string | undefined;
29
+ accountId?: string | undefined;
30
+ metadata?: Record<string, unknown> | undefined;
31
+ }, {
32
+ type: "oauth";
33
+ accessToken: string;
34
+ refreshToken?: string | undefined;
35
+ expiresAt?: string | undefined;
36
+ accountId?: string | undefined;
37
+ metadata?: Record<string, unknown> | undefined;
38
+ }>;
39
+ /** Custom/opaque credential — arbitrary key-value data. */
40
+ export declare const CustomCredentialSchema: z.ZodObject<{
41
+ type: z.ZodLiteral<"custom">;
42
+ data: z.ZodRecord<z.ZodString, z.ZodUnknown>;
43
+ }, "strip", z.ZodTypeAny, {
44
+ type: "custom";
45
+ data: Record<string, unknown>;
46
+ }, {
47
+ type: "custom";
48
+ data: Record<string, unknown>;
49
+ }>;
50
+ /**
51
+ * Discriminated union of all credential types.
52
+ * - `api` — simple API key
53
+ * - `oauth` — OAuth 2.0 access/refresh tokens
54
+ * - `custom` — arbitrary opaque data
55
+ */
56
+ export declare const CredentialSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
57
+ type: z.ZodLiteral<"api">;
58
+ key: z.ZodString;
59
+ }, "strip", z.ZodTypeAny, {
60
+ type: "api";
61
+ key: string;
62
+ }, {
63
+ type: "api";
64
+ key: string;
65
+ }>, z.ZodObject<{
66
+ type: z.ZodLiteral<"oauth">;
67
+ accessToken: z.ZodString;
68
+ refreshToken: z.ZodOptional<z.ZodString>;
69
+ /** ISO-8601 datetime when the access token expires. */
70
+ expiresAt: z.ZodOptional<z.ZodString>;
71
+ /** Provider account identifier (e.g. GitHub username). */
72
+ accountId: z.ZodOptional<z.ZodString>;
73
+ /** Arbitrary provider-specific metadata. */
74
+ metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
75
+ }, "strip", z.ZodTypeAny, {
76
+ type: "oauth";
77
+ accessToken: string;
78
+ refreshToken?: string | undefined;
79
+ expiresAt?: string | undefined;
80
+ accountId?: string | undefined;
81
+ metadata?: Record<string, unknown> | undefined;
82
+ }, {
83
+ type: "oauth";
84
+ accessToken: string;
85
+ refreshToken?: string | undefined;
86
+ expiresAt?: string | undefined;
87
+ accountId?: string | undefined;
88
+ metadata?: Record<string, unknown> | undefined;
89
+ }>, z.ZodObject<{
90
+ type: z.ZodLiteral<"custom">;
91
+ data: z.ZodRecord<z.ZodString, z.ZodUnknown>;
92
+ }, "strip", z.ZodTypeAny, {
93
+ type: "custom";
94
+ data: Record<string, unknown>;
95
+ }, {
96
+ type: "custom";
97
+ data: Record<string, unknown>;
98
+ }>]>;
99
+ export type ApiCredential = z.infer<typeof ApiCredentialSchema>;
100
+ export type OAuthCredential = z.infer<typeof OAuthCredentialSchema>;
101
+ export type CustomCredential = z.infer<typeof CustomCredentialSchema>;
102
+ export type Credential = z.infer<typeof CredentialSchema>;
@@ -0,0 +1,118 @@
1
+ import type { Credential } from "./credentials.schema";
2
+ export type { ApiCredential, Credential, CustomCredential, OAuthCredential, } from "./credentials.schema";
3
+ /**
4
+ * Persisted credential data.
5
+ *
6
+ * Top-level keys are scopes:
7
+ * - `"$global"` — credentials available to all strategies.
8
+ * - Any other key — a strategy name for strategy-scoped overrides.
9
+ *
10
+ * Within each scope, keys are provider IDs → credential objects.
11
+ *
12
+ * @example
13
+ * ```json
14
+ * {
15
+ * "$global": { "anthropic": { "type": "api", "key": "sk-..." } },
16
+ * "my-strategy": { "openai": { "type": "api", "key": "sk-..." } }
17
+ * }
18
+ * ```
19
+ */
20
+ export type CredentialStoreData = Readonly<Record<string, Record<string, Credential>>>;
21
+ /**
22
+ * Low-level storage backend for credential data.
23
+ *
24
+ * Implementations handle serialization, file I/O, and encryption.
25
+ * The store itself handles scoping, resolution priority, and env vars.
26
+ */
27
+ export interface CredentialBackend {
28
+ /** Read all credential data from storage. Returns empty object if none exists. */
29
+ readAll(): Promise<CredentialStoreData>;
30
+ /** Write the full credential data to storage (atomic replace). */
31
+ writeAll(data: CredentialStoreData): Promise<void>;
32
+ }
33
+ /**
34
+ * Credential store — resolves, gets, sets, and removes credentials.
35
+ *
36
+ * Resolution priority (most specific wins):
37
+ * 1. Strategy-scoped credential (if `scope` is provided and not `"$global"`)
38
+ * 2. Environment variable (using well-known or custom env var mapping)
39
+ * 3. Global-scoped credential (`"$global"`)
40
+ */
41
+ export interface CredentialStore {
42
+ /**
43
+ * Resolve the best credential for a provider.
44
+ *
45
+ * Checks strategy scope → env vars → global scope, returning the
46
+ * first match found. Returns `undefined` if no credential is available.
47
+ *
48
+ * @param providerId - The provider to resolve credentials for.
49
+ * @param scope - Optional strategy name. If omitted, only env vars and global scope are checked.
50
+ */
51
+ resolve(providerId: string, scope?: string): Promise<Credential | undefined>;
52
+ /**
53
+ * Get a credential from a specific scope (no fallback/resolution chain).
54
+ */
55
+ get(providerId: string, scope: string): Promise<Credential | undefined>;
56
+ /**
57
+ * Set a credential in a specific scope.
58
+ */
59
+ set(providerId: string, scope: string, credential: Credential): Promise<void>;
60
+ /**
61
+ * Remove a credential from a specific scope.
62
+ * Returns `true` if the credential existed and was removed.
63
+ */
64
+ remove(providerId: string, scope: string): Promise<boolean>;
65
+ /**
66
+ * List all provider IDs that have credentials in a given scope.
67
+ */
68
+ list(scope: string): Promise<string[]>;
69
+ /**
70
+ * List all scopes that have at least one credential.
71
+ */
72
+ listScopes(): Promise<string[]>;
73
+ /**
74
+ * Check whether credentials exist for a provider, without performing
75
+ * any network validation.
76
+ *
77
+ * Returns `"configured"` if a credential is resolvable via any of:
78
+ * strategy scope → environment variable → global scope. Returns
79
+ * `"none"` if no credential is available.
80
+ *
81
+ * @param providerId - The provider to check.
82
+ * @param scope - Optional strategy name. If omitted, only env vars and global scope are checked.
83
+ */
84
+ getAuthStatus(providerId: string, scope?: string): Promise<AuthStatus>;
85
+ }
86
+ /**
87
+ * Configuration-level authentication status for a provider.
88
+ *
89
+ * - `"none"` — no credential found in any source.
90
+ * - `"configured"` — a credential was found (env var, strategy scope, or global scope).
91
+ *
92
+ * This is a configuration check only; it never performs a network call
93
+ * to validate the credential works.
94
+ */
95
+ export type AuthStatus = "none" | "configured";
96
+ /**
97
+ * Maps a provider ID to the environment variable name(s) that
98
+ * typically contain its API key.
99
+ *
100
+ * The first env var in the array is the "primary" — the one we check
101
+ * first and suggest to users. Additional entries are aliases.
102
+ */
103
+ export type EnvVarMap = Readonly<Record<string, readonly string[]>>;
104
+ /** Options for creating a credential store. */
105
+ export interface CreateCredentialStoreOptions {
106
+ /** The storage backend to use. */
107
+ readonly backend: CredentialBackend;
108
+ /**
109
+ * Additional env var mappings to merge with WELL_KNOWN_ENV_VARS.
110
+ * Provider IDs here override well-known entries for the same key.
111
+ */
112
+ readonly envVarOverrides?: EnvVarMap;
113
+ /**
114
+ * Override `process.env` for testing.
115
+ * @internal
116
+ */
117
+ readonly env?: Readonly<Record<string, string | undefined>>;
118
+ }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Resolve the platform-aware data directory for comma-agents.
3
+ *
4
+ * Respects platform conventions:
5
+ * - macOS: ~/Library/Application Support/comma-agents/
6
+ * - Windows: %LOCALAPPDATA%/comma-agents/ (fallback ~/AppData/Local)
7
+ * - Linux: $XDG_DATA_HOME/comma-agents/ (fallback ~/.local/share)
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * const dataDir = resolveDataDir();
12
+ * // macOS: "/Users/alice/Library/Application Support/comma-agents"
13
+ * // Linux: "/home/alice/.local/share/comma-agents"
14
+ * // Windows: "C:\\Users\\alice\\AppData\\Local\\comma-agents"
15
+ * ```
16
+ */
17
+ export declare function resolveDataDir(): string;
18
+ /** Default name for the credentials JSON file. */
19
+ export declare const CREDENTIALS_FILENAME = "credentials.json";
20
+ /**
21
+ * Resolve the default path to the credentials JSON file.
22
+ *
23
+ * Convenience wrapper: `join(resolveDataDir(), "credentials.json")`.
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * const backend = createJsonFileBackend({ filePath: resolveCredentialsPath() });
28
+ * ```
29
+ */
30
+ export declare function resolveCredentialsPath(): string;
@@ -0,0 +1,6 @@
1
+ export { createJsonFileBackend } from "./backends/json-file";
2
+ export { createCredentialStore } from "./credentials";
3
+ export type { ApiCredential, Credential, CustomCredential, OAuthCredential, } from "./credentials.schema";
4
+ export { ApiCredentialSchema, CredentialSchema, CustomCredentialSchema, OAuthCredentialSchema, } from "./credentials.schema";
5
+ export type { AuthStatus, CreateCredentialStoreOptions, CredentialBackend, CredentialStore, CredentialStoreData, EnvVarMap, } from "./credentials.types";
6
+ export { resolveCredentialsPath, resolveDataDir } from "./credentials.utils";