evolcore 0.0.8 → 0.0.10

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 (215) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/README.md +37 -3
  3. package/dist/agents/baseagent.js +111 -19
  4. package/dist/agents/claude-runner.js +30 -2
  5. package/dist/agents/codex-app-server-client.js +31 -0
  6. package/dist/agents/codex-runner.js +3 -0
  7. package/dist/aun/msg/group.js +9 -9
  8. package/dist/aun/msg/p2p.js +11 -6
  9. package/dist/aun/msg/upload.js +43 -29
  10. package/dist/aun/outbox.js +48 -5
  11. package/dist/channels/aun.js +199 -50
  12. package/dist/cli/aun-commands.js +10 -7
  13. package/dist/cli/daemon-commands.js +19 -4
  14. package/dist/cli/index.js +11 -1
  15. package/dist/cli/init.js +103 -4
  16. package/dist/cli/model.js +5 -5
  17. package/dist/config/builtin-role-templates.js +4 -3
  18. package/dist/config/config-field-policy.js +6 -2
  19. package/dist/config/config-manager.js +48 -11
  20. package/dist/config/contact-book-store.js +1 -1
  21. package/dist/config/role-schema.js +18 -3
  22. package/dist/config/role-service.js +3 -3
  23. package/dist/config/role-store.js +4 -3
  24. package/dist/config-store.js +4 -0
  25. package/dist/core/auth/operation-authorizer.js +3 -1
  26. package/dist/core/bootstrap-service.js +0 -12
  27. package/dist/core/channel-loader.js +5 -1
  28. package/dist/core/command/command-handler.js +1 -1
  29. package/dist/core/command/evol-menu-version-gate.js +3 -2
  30. package/dist/core/command/menu-handler.js +33 -22
  31. package/dist/core/command/menu-protocol.js +7 -4
  32. package/dist/core/command/slash-handler.js +4 -4
  33. package/dist/core/data-migration.js +79 -27
  34. package/dist/core/inference/text-inference.js +18 -5
  35. package/dist/core/message/message-bridge.js +17 -6
  36. package/dist/core/message/response-engine.js +22 -3
  37. package/dist/core/model/config-scope.js +3 -3
  38. package/dist/core/permission/tool-policy.js +18 -0
  39. package/dist/index.js +43 -26
  40. package/dist/ipc.js +1 -1
  41. package/dist/product.js +1 -0
  42. package/dist/utils/autostart.js +27 -0
  43. package/dist/utils/ecweb-utils.js +15 -2
  44. package/dist/utils/macos-autostart.js +179 -0
  45. package/dist/utils/windows-autostart.js +130 -0
  46. package/ecagent/dist/agent-loop.d.ts +1 -1
  47. package/ecagent/dist/agent-loop.js +1 -1
  48. package/ecagent/dist/agent.d.ts +1 -1
  49. package/ecagent/dist/agent.js +1 -1
  50. package/ecagent/dist/harness/agent-harness.d.ts +1 -1
  51. package/ecagent/dist/harness/compaction/branch-summarization.d.ts +1 -1
  52. package/ecagent/dist/harness/compaction/compaction.d.ts +1 -1
  53. package/ecagent/dist/harness/compaction/utils.d.ts +1 -1
  54. package/ecagent/dist/harness/messages.d.ts +1 -1
  55. package/ecagent/dist/harness/session/session.d.ts +1 -1
  56. package/ecagent/dist/harness/types.d.ts +1 -1
  57. package/ecagent/dist/proxy.d.ts +1 -1
  58. package/ecagent/dist/proxy.js +1 -1
  59. package/ecagent/dist/types.d.ts +1 -1
  60. package/kits/migrations/migrate-role-config-v5.mjs +27 -2
  61. package/kits/schemas/agent-config.schema.10.json +19 -1
  62. package/kits/schemas/daemon.schema.4.json +4 -0
  63. package/kits/schemas/defaults.schema.4.json +15 -1
  64. package/kits/schemas/relation-config.schema.8.json +22 -1
  65. package/kits/schemas/role-config.schema.1.json +2 -2
  66. package/package.json +2 -4
  67. package/ecagent/dist/agent-loop.d.ts.map +0 -1
  68. package/ecagent/dist/agent-loop.js.map +0 -1
  69. package/ecagent/dist/agent.d.ts.map +0 -1
  70. package/ecagent/dist/agent.js.map +0 -1
  71. package/ecagent/dist/harness/agent-harness.d.ts.map +0 -1
  72. package/ecagent/dist/harness/agent-harness.js.map +0 -1
  73. package/ecagent/dist/harness/compaction/branch-summarization.d.ts.map +0 -1
  74. package/ecagent/dist/harness/compaction/branch-summarization.js.map +0 -1
  75. package/ecagent/dist/harness/compaction/compaction.d.ts.map +0 -1
  76. package/ecagent/dist/harness/compaction/compaction.js.map +0 -1
  77. package/ecagent/dist/harness/compaction/utils.d.ts.map +0 -1
  78. package/ecagent/dist/harness/compaction/utils.js.map +0 -1
  79. package/ecagent/dist/harness/env/nodejs.d.ts.map +0 -1
  80. package/ecagent/dist/harness/env/nodejs.js.map +0 -1
  81. package/ecagent/dist/harness/messages.d.ts.map +0 -1
  82. package/ecagent/dist/harness/messages.js.map +0 -1
  83. package/ecagent/dist/harness/prompt-templates.d.ts.map +0 -1
  84. package/ecagent/dist/harness/prompt-templates.js.map +0 -1
  85. package/ecagent/dist/harness/session/jsonl-repo.d.ts.map +0 -1
  86. package/ecagent/dist/harness/session/jsonl-repo.js.map +0 -1
  87. package/ecagent/dist/harness/session/jsonl-storage.d.ts.map +0 -1
  88. package/ecagent/dist/harness/session/jsonl-storage.js.map +0 -1
  89. package/ecagent/dist/harness/session/memory-repo.d.ts.map +0 -1
  90. package/ecagent/dist/harness/session/memory-repo.js.map +0 -1
  91. package/ecagent/dist/harness/session/memory-storage.d.ts.map +0 -1
  92. package/ecagent/dist/harness/session/memory-storage.js.map +0 -1
  93. package/ecagent/dist/harness/session/repo-utils.d.ts.map +0 -1
  94. package/ecagent/dist/harness/session/repo-utils.js.map +0 -1
  95. package/ecagent/dist/harness/session/session.d.ts.map +0 -1
  96. package/ecagent/dist/harness/session/session.js.map +0 -1
  97. package/ecagent/dist/harness/session/uuid.d.ts.map +0 -1
  98. package/ecagent/dist/harness/session/uuid.js.map +0 -1
  99. package/ecagent/dist/harness/skills.d.ts.map +0 -1
  100. package/ecagent/dist/harness/skills.js.map +0 -1
  101. package/ecagent/dist/harness/system-prompt.d.ts.map +0 -1
  102. package/ecagent/dist/harness/system-prompt.js.map +0 -1
  103. package/ecagent/dist/harness/types.d.ts.map +0 -1
  104. package/ecagent/dist/harness/types.js.map +0 -1
  105. package/ecagent/dist/harness/utils/shell-output.d.ts.map +0 -1
  106. package/ecagent/dist/harness/utils/shell-output.js.map +0 -1
  107. package/ecagent/dist/harness/utils/truncate.d.ts.map +0 -1
  108. package/ecagent/dist/harness/utils/truncate.js.map +0 -1
  109. package/ecagent/dist/index.d.ts.map +0 -1
  110. package/ecagent/dist/index.js.map +0 -1
  111. package/ecagent/dist/node.d.ts.map +0 -1
  112. package/ecagent/dist/node.js.map +0 -1
  113. package/ecagent/dist/proxy.d.ts.map +0 -1
  114. package/ecagent/dist/proxy.js.map +0 -1
  115. package/ecagent/dist/runtime/api/lazy.d.ts.map +0 -1
  116. package/ecagent/dist/runtime/api/lazy.js.map +0 -1
  117. package/ecagent/dist/runtime/api/pi-messages.d.ts.map +0 -1
  118. package/ecagent/dist/runtime/api/pi-messages.js.map +0 -1
  119. package/ecagent/dist/runtime/api/pi-messages.lazy.d.ts.map +0 -1
  120. package/ecagent/dist/runtime/api/pi-messages.lazy.js.map +0 -1
  121. package/ecagent/dist/runtime/auth/context.d.ts.map +0 -1
  122. package/ecagent/dist/runtime/auth/context.js.map +0 -1
  123. package/ecagent/dist/runtime/auth/credential-store.d.ts.map +0 -1
  124. package/ecagent/dist/runtime/auth/credential-store.js.map +0 -1
  125. package/ecagent/dist/runtime/auth/helpers.d.ts.map +0 -1
  126. package/ecagent/dist/runtime/auth/helpers.js.map +0 -1
  127. package/ecagent/dist/runtime/auth/resolve.d.ts.map +0 -1
  128. package/ecagent/dist/runtime/auth/resolve.js.map +0 -1
  129. package/ecagent/dist/runtime/auth/types.d.ts.map +0 -1
  130. package/ecagent/dist/runtime/auth/types.js.map +0 -1
  131. package/ecagent/dist/runtime/compat.d.ts.map +0 -1
  132. package/ecagent/dist/runtime/compat.js.map +0 -1
  133. package/ecagent/dist/runtime/index.d.ts.map +0 -1
  134. package/ecagent/dist/runtime/index.js.map +0 -1
  135. package/ecagent/dist/runtime/models.d.ts.map +0 -1
  136. package/ecagent/dist/runtime/models.js.map +0 -1
  137. package/ecagent/dist/runtime/oauth.d.ts.map +0 -1
  138. package/ecagent/dist/runtime/oauth.js.map +0 -1
  139. package/ecagent/dist/runtime/providers/faux.d.ts.map +0 -1
  140. package/ecagent/dist/runtime/providers/faux.js.map +0 -1
  141. package/ecagent/dist/runtime/types.d.ts.map +0 -1
  142. package/ecagent/dist/runtime/types.js.map +0 -1
  143. package/ecagent/dist/runtime/utils/diagnostics.d.ts.map +0 -1
  144. package/ecagent/dist/runtime/utils/diagnostics.js.map +0 -1
  145. package/ecagent/dist/runtime/utils/event-stream.d.ts.map +0 -1
  146. package/ecagent/dist/runtime/utils/event-stream.js.map +0 -1
  147. package/ecagent/dist/runtime/utils/headers.d.ts.map +0 -1
  148. package/ecagent/dist/runtime/utils/headers.js.map +0 -1
  149. package/ecagent/dist/runtime/utils/json-parse.d.ts.map +0 -1
  150. package/ecagent/dist/runtime/utils/json-parse.js.map +0 -1
  151. package/ecagent/dist/runtime/utils/oauth/types.d.ts.map +0 -1
  152. package/ecagent/dist/runtime/utils/oauth/types.js.map +0 -1
  153. package/ecagent/dist/runtime/utils/overflow.d.ts.map +0 -1
  154. package/ecagent/dist/runtime/utils/overflow.js.map +0 -1
  155. package/ecagent/dist/runtime/utils/provider-env.d.ts.map +0 -1
  156. package/ecagent/dist/runtime/utils/provider-env.js.map +0 -1
  157. package/ecagent/dist/runtime/utils/retry.d.ts.map +0 -1
  158. package/ecagent/dist/runtime/utils/retry.js.map +0 -1
  159. package/ecagent/dist/runtime/utils/typebox-helpers.d.ts.map +0 -1
  160. package/ecagent/dist/runtime/utils/typebox-helpers.js.map +0 -1
  161. package/ecagent/dist/runtime/utils/validation.d.ts.map +0 -1
  162. package/ecagent/dist/runtime/utils/validation.js.map +0 -1
  163. package/ecagent/dist/types.d.ts.map +0 -1
  164. package/ecagent/dist/types.js.map +0 -1
  165. package/ecagent/package.json +0 -93
  166. package/ecagent/src/agent-loop.ts +0 -792
  167. package/ecagent/src/agent.ts +0 -575
  168. package/ecagent/src/harness/agent-harness.ts +0 -1029
  169. package/ecagent/src/harness/compaction/branch-summarization.ts +0 -261
  170. package/ecagent/src/harness/compaction/compaction.ts +0 -753
  171. package/ecagent/src/harness/compaction/utils.ts +0 -144
  172. package/ecagent/src/harness/env/nodejs.ts +0 -569
  173. package/ecagent/src/harness/messages.ts +0 -164
  174. package/ecagent/src/harness/prompt-templates.ts +0 -267
  175. package/ecagent/src/harness/session/jsonl-repo.ts +0 -179
  176. package/ecagent/src/harness/session/jsonl-storage.ts +0 -314
  177. package/ecagent/src/harness/session/memory-repo.ts +0 -50
  178. package/ecagent/src/harness/session/memory-storage.ts +0 -133
  179. package/ecagent/src/harness/session/repo-utils.ts +0 -51
  180. package/ecagent/src/harness/session/session.ts +0 -338
  181. package/ecagent/src/harness/session/uuid.ts +0 -54
  182. package/ecagent/src/harness/skills.ts +0 -375
  183. package/ecagent/src/harness/system-prompt.ts +0 -34
  184. package/ecagent/src/harness/types.ts +0 -838
  185. package/ecagent/src/harness/utils/shell-output.ts +0 -135
  186. package/ecagent/src/harness/utils/truncate.ts +0 -344
  187. package/ecagent/src/index.ts +0 -46
  188. package/ecagent/src/node.ts +0 -2
  189. package/ecagent/src/proxy.ts +0 -367
  190. package/ecagent/src/runtime/api/lazy.ts +0 -70
  191. package/ecagent/src/runtime/api/pi-messages.lazy.ts +0 -4
  192. package/ecagent/src/runtime/api/pi-messages.ts +0 -436
  193. package/ecagent/src/runtime/auth/context.ts +0 -45
  194. package/ecagent/src/runtime/auth/credential-store.ts +0 -47
  195. package/ecagent/src/runtime/auth/helpers.ts +0 -46
  196. package/ecagent/src/runtime/auth/resolve.ts +0 -141
  197. package/ecagent/src/runtime/auth/types.ts +0 -182
  198. package/ecagent/src/runtime/compat.ts +0 -158
  199. package/ecagent/src/runtime/index.ts +0 -31
  200. package/ecagent/src/runtime/models.ts +0 -452
  201. package/ecagent/src/runtime/oauth.ts +0 -1
  202. package/ecagent/src/runtime/providers/faux.ts +0 -538
  203. package/ecagent/src/runtime/types.ts +0 -718
  204. package/ecagent/src/runtime/utils/diagnostics.ts +0 -45
  205. package/ecagent/src/runtime/utils/event-stream.ts +0 -88
  206. package/ecagent/src/runtime/utils/headers.ts +0 -18
  207. package/ecagent/src/runtime/utils/json-parse.ts +0 -124
  208. package/ecagent/src/runtime/utils/oauth/types.ts +0 -79
  209. package/ecagent/src/runtime/utils/overflow.ts +0 -165
  210. package/ecagent/src/runtime/utils/provider-env.ts +0 -52
  211. package/ecagent/src/runtime/utils/retry.ts +0 -101
  212. package/ecagent/src/runtime/utils/typebox-helpers.ts +0 -24
  213. package/ecagent/src/runtime/utils/validation.ts +0 -310
  214. package/ecagent/src/types.ts +0 -430
  215. package/ecagent/tsconfig.build.json +0 -32
@@ -1,338 +0,0 @@
1
- import type { ImageContent, TextContent } from "ecagent/runtime";
2
- import type { AgentMessage } from "../../types.ts";
3
- import { createBranchSummaryMessage, createCompactionSummaryMessage, createCustomMessage } from "../messages.ts";
4
- import type {
5
- ActiveToolsChangeEntry,
6
- BranchSummaryEntry,
7
- CompactionEntry,
8
- CustomEntry,
9
- CustomMessageEntry,
10
- LabelEntry,
11
- MessageEntry,
12
- ModelChangeEntry,
13
- SessionContext,
14
- SessionInfoEntry,
15
- SessionMetadata,
16
- SessionStorage,
17
- SessionTreeEntry,
18
- ThinkingLevelChangeEntry,
19
- } from "../types.ts";
20
- import { SessionError } from "../types.ts";
21
-
22
- export type ContextEntryTransform = (entries: readonly SessionTreeEntry[]) => readonly SessionTreeEntry[];
23
-
24
- export type CustomEntryContextMessageProjector = (
25
- entry: CustomEntry,
26
- index: number,
27
- entries: readonly SessionTreeEntry[],
28
- ) => readonly AgentMessage[] | undefined;
29
-
30
- export interface SessionContextBuildOptions {
31
- /** Additional entry transforms applied after the default compaction transform. */
32
- entryTransforms?: readonly ContextEntryTransform[];
33
- /** Optional custom-entry projectors. Custom entries are omitted from model context by default. */
34
- entryProjectors?: Readonly<Record<string, CustomEntryContextMessageProjector>>;
35
- }
36
-
37
- function deriveSessionContextState(pathEntries: readonly SessionTreeEntry[]): Omit<SessionContext, "messages"> {
38
- let thinkingLevel = "off";
39
- let model: { provider: string; modelId: string } | null = null;
40
- let activeToolNames: string[] | null = null;
41
-
42
- for (const entry of pathEntries) {
43
- if (entry.type === "thinking_level_change") {
44
- thinkingLevel = entry.thinkingLevel;
45
- } else if (entry.type === "model_change") {
46
- model = { provider: entry.provider, modelId: entry.modelId };
47
- } else if (entry.type === "message" && entry.message.role === "assistant") {
48
- model = { provider: entry.message.provider, modelId: entry.message.model };
49
- } else if (entry.type === "active_tools_change") {
50
- activeToolNames = [...entry.activeToolNames];
51
- }
52
- }
53
-
54
- return { thinkingLevel, model, activeToolNames };
55
- }
56
-
57
- export function defaultContextEntryTransform(pathEntries: readonly SessionTreeEntry[]): SessionTreeEntry[] {
58
- let compaction: CompactionEntry | null = null;
59
- for (const entry of pathEntries) {
60
- if (entry.type === "compaction") {
61
- compaction = entry;
62
- }
63
- }
64
- if (!compaction) {
65
- return [...pathEntries];
66
- }
67
-
68
- const entries: SessionTreeEntry[] = [compaction];
69
- const compactionIdx = pathEntries.findIndex((entry) => entry.type === "compaction" && entry.id === compaction.id);
70
- let foundFirstKept = false;
71
- for (let i = 0; i < compactionIdx; i++) {
72
- const entry = pathEntries[i]!;
73
- if (entry.id === compaction.firstKeptEntryId) foundFirstKept = true;
74
- if (foundFirstKept) entries.push(entry);
75
- }
76
- for (let i = compactionIdx + 1; i < pathEntries.length; i++) {
77
- entries.push(pathEntries[i]!);
78
- }
79
- return entries;
80
- }
81
-
82
- export function buildContextEntries(
83
- pathEntries: readonly SessionTreeEntry[],
84
- options: SessionContextBuildOptions = {},
85
- ): SessionTreeEntry[] {
86
- let entries = defaultContextEntryTransform(pathEntries);
87
- for (const transform of options.entryTransforms ?? []) {
88
- entries = [...transform(entries)];
89
- }
90
- return entries;
91
- }
92
-
93
- export function sessionEntryToContextMessages(
94
- entry: SessionTreeEntry,
95
- index: number,
96
- entries: readonly SessionTreeEntry[],
97
- options: SessionContextBuildOptions = {},
98
- ): AgentMessage[] {
99
- if (entry.type === "message") {
100
- return [entry.message as AgentMessage];
101
- }
102
- if (entry.type === "custom_message") {
103
- return [
104
- createCustomMessage(
105
- entry.customType,
106
- entry.content as string | (TextContent | ImageContent)[],
107
- entry.display,
108
- entry.details,
109
- entry.timestamp,
110
- ),
111
- ];
112
- }
113
- if (entry.type === "compaction") {
114
- return [createCompactionSummaryMessage(entry.summary, entry.tokensBefore, entry.timestamp)];
115
- }
116
- if (entry.type === "branch_summary" && entry.summary) {
117
- return [createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp)];
118
- }
119
- if (entry.type === "custom") {
120
- return [...(options.entryProjectors?.[entry.customType]?.(entry, index, entries) ?? [])];
121
- }
122
- return [];
123
- }
124
-
125
- export function buildSessionContext(
126
- pathEntries: readonly SessionTreeEntry[],
127
- options: SessionContextBuildOptions = {},
128
- ): SessionContext {
129
- const state = deriveSessionContextState(pathEntries);
130
- const contextEntries = buildContextEntries(pathEntries, options);
131
- const messages = contextEntries.flatMap((entry, index) =>
132
- sessionEntryToContextMessages(entry, index, contextEntries, options),
133
- );
134
- return { ...state, messages };
135
- }
136
-
137
- export class Session<TMetadata extends SessionMetadata = SessionMetadata> {
138
- private storage: SessionStorage<TMetadata>;
139
- private contextBuildOptions: SessionContextBuildOptions;
140
-
141
- constructor(storage: SessionStorage<TMetadata>, contextBuildOptions: SessionContextBuildOptions = {}) {
142
- this.storage = storage;
143
- this.contextBuildOptions = contextBuildOptions;
144
- }
145
-
146
- getMetadata(): Promise<TMetadata> {
147
- return this.storage.getMetadata();
148
- }
149
-
150
- getStorage(): SessionStorage<TMetadata> {
151
- return this.storage;
152
- }
153
-
154
- getLeafId(): Promise<string | null> {
155
- return this.storage.getLeafId();
156
- }
157
-
158
- getEntry(id: string): Promise<SessionTreeEntry | undefined> {
159
- return this.storage.getEntry(id);
160
- }
161
-
162
- getEntries(): Promise<SessionTreeEntry[]> {
163
- return this.storage.getEntries();
164
- }
165
-
166
- async getBranch(fromId?: string): Promise<SessionTreeEntry[]> {
167
- const leafId = fromId ?? (await this.storage.getLeafId());
168
- return this.storage.getPathToRoot(leafId);
169
- }
170
-
171
- async buildContextEntries(options: SessionContextBuildOptions = {}): Promise<SessionTreeEntry[]> {
172
- return buildContextEntries(await this.getBranch(), this.mergeContextBuildOptions(options));
173
- }
174
-
175
- async buildContext(options: SessionContextBuildOptions = {}): Promise<SessionContext> {
176
- return buildSessionContext(await this.getBranch(), this.mergeContextBuildOptions(options));
177
- }
178
-
179
- private mergeContextBuildOptions(options: SessionContextBuildOptions): SessionContextBuildOptions {
180
- return {
181
- entryTransforms: [...(this.contextBuildOptions.entryTransforms ?? []), ...(options.entryTransforms ?? [])],
182
- entryProjectors: {
183
- ...(this.contextBuildOptions.entryProjectors ?? {}),
184
- ...(options.entryProjectors ?? {}),
185
- },
186
- };
187
- }
188
-
189
- getLabel(id: string): Promise<string | undefined> {
190
- return this.storage.getLabel(id);
191
- }
192
-
193
- async getSessionName(): Promise<string | undefined> {
194
- const entries = await this.storage.findEntries("session_info");
195
- return entries[entries.length - 1]?.name?.trim() || undefined;
196
- }
197
-
198
- private async appendTypedEntry<TEntry extends SessionTreeEntry>(entry: TEntry): Promise<string> {
199
- await this.storage.appendEntry(entry);
200
- return entry.id;
201
- }
202
-
203
- async appendMessage(message: AgentMessage): Promise<string> {
204
- return this.appendTypedEntry({
205
- type: "message",
206
- id: await this.storage.createEntryId(),
207
- parentId: await this.storage.getLeafId(),
208
- timestamp: new Date().toISOString(),
209
- message,
210
- } satisfies MessageEntry);
211
- }
212
-
213
- async appendThinkingLevelChange(thinkingLevel: string): Promise<string> {
214
- return this.appendTypedEntry({
215
- type: "thinking_level_change",
216
- id: await this.storage.createEntryId(),
217
- parentId: await this.storage.getLeafId(),
218
- timestamp: new Date().toISOString(),
219
- thinkingLevel,
220
- } satisfies ThinkingLevelChangeEntry);
221
- }
222
-
223
- async appendModelChange(provider: string, modelId: string): Promise<string> {
224
- return this.appendTypedEntry({
225
- type: "model_change",
226
- id: await this.storage.createEntryId(),
227
- parentId: await this.storage.getLeafId(),
228
- timestamp: new Date().toISOString(),
229
- provider,
230
- modelId,
231
- } satisfies ModelChangeEntry);
232
- }
233
-
234
- async appendActiveToolsChange(activeToolNames: string[]): Promise<string> {
235
- return this.appendTypedEntry({
236
- type: "active_tools_change",
237
- id: await this.storage.createEntryId(),
238
- parentId: await this.storage.getLeafId(),
239
- timestamp: new Date().toISOString(),
240
- activeToolNames: [...activeToolNames],
241
- } satisfies ActiveToolsChangeEntry);
242
- }
243
-
244
- async appendCompaction<T = unknown>(
245
- summary: string,
246
- firstKeptEntryId: string,
247
- tokensBefore: number,
248
- details?: T,
249
- fromHook?: boolean,
250
- ): Promise<string> {
251
- return this.appendTypedEntry({
252
- type: "compaction",
253
- id: await this.storage.createEntryId(),
254
- parentId: await this.storage.getLeafId(),
255
- timestamp: new Date().toISOString(),
256
- summary,
257
- firstKeptEntryId,
258
- tokensBefore,
259
- details,
260
- fromHook,
261
- } satisfies CompactionEntry<T>);
262
- }
263
-
264
- async appendCustomEntry(customType: string, data?: unknown): Promise<string> {
265
- return this.appendTypedEntry({
266
- type: "custom",
267
- id: await this.storage.createEntryId(),
268
- parentId: await this.storage.getLeafId(),
269
- timestamp: new Date().toISOString(),
270
- customType,
271
- data,
272
- } satisfies CustomEntry);
273
- }
274
-
275
- async appendCustomMessageEntry<T = unknown>(
276
- customType: string,
277
- content: string | (TextContent | ImageContent)[],
278
- display: boolean,
279
- details?: T,
280
- ): Promise<string> {
281
- return this.appendTypedEntry({
282
- type: "custom_message",
283
- id: await this.storage.createEntryId(),
284
- parentId: await this.storage.getLeafId(),
285
- timestamp: new Date().toISOString(),
286
- customType,
287
- content,
288
- display,
289
- details,
290
- } satisfies CustomMessageEntry<T>);
291
- }
292
-
293
- async appendLabel(targetId: string, label: string | undefined): Promise<string> {
294
- if (!(await this.storage.getEntry(targetId))) {
295
- throw new SessionError("not_found", `Entry ${targetId} not found`);
296
- }
297
- return this.appendTypedEntry({
298
- type: "label",
299
- id: await this.storage.createEntryId(),
300
- parentId: await this.storage.getLeafId(),
301
- timestamp: new Date().toISOString(),
302
- targetId,
303
- label,
304
- } satisfies LabelEntry);
305
- }
306
-
307
- async appendSessionName(name: string): Promise<string> {
308
- const sanitizedName = name.replace(/[\r\n]+/g, " ").trim();
309
- return this.appendTypedEntry({
310
- type: "session_info",
311
- id: await this.storage.createEntryId(),
312
- parentId: await this.storage.getLeafId(),
313
- timestamp: new Date().toISOString(),
314
- name: sanitizedName,
315
- } satisfies SessionInfoEntry);
316
- }
317
-
318
- async moveTo(
319
- entryId: string | null,
320
- summary?: { summary: string; details?: unknown; fromHook?: boolean },
321
- ): Promise<string | undefined> {
322
- if (entryId !== null && !(await this.storage.getEntry(entryId))) {
323
- throw new SessionError("not_found", `Entry ${entryId} not found`);
324
- }
325
- await this.storage.setLeafId(entryId);
326
- if (!summary) return undefined;
327
- return this.appendTypedEntry({
328
- type: "branch_summary",
329
- id: await this.storage.createEntryId(),
330
- parentId: entryId,
331
- timestamp: new Date().toISOString(),
332
- fromId: entryId ?? "root",
333
- summary: summary.summary,
334
- details: summary.details,
335
- fromHook: summary.fromHook,
336
- } satisfies BranchSummaryEntry);
337
- }
338
- }
@@ -1,54 +0,0 @@
1
- let lastTimestamp = -Infinity;
2
- let sequence = 0;
3
-
4
- function fillRandomBytes(bytes: Uint8Array): void {
5
- const crypto = globalThis.crypto;
6
- if (crypto?.getRandomValues) {
7
- crypto.getRandomValues(bytes);
8
- return;
9
- }
10
- for (let i = 0; i < bytes.length; i++) {
11
- bytes[i] = Math.floor(Math.random() * 256);
12
- }
13
- }
14
-
15
- export function uuidv7(): string {
16
- const random = new Uint8Array(16);
17
- fillRandomBytes(random);
18
- const timestamp = Date.now();
19
-
20
- if (timestamp > lastTimestamp) {
21
- sequence = random[6] * 0x1000000 + random[7] * 0x10000 + random[8] * 0x100 + random[9];
22
- lastTimestamp = timestamp;
23
- } else {
24
- sequence = (sequence + 1) >>> 0;
25
- if (sequence === 0) {
26
- lastTimestamp++;
27
- }
28
- }
29
-
30
- const bytes = new Uint8Array(16);
31
- bytes[0] = (lastTimestamp / 0x10000000000) & 0xff;
32
- bytes[1] = (lastTimestamp / 0x100000000) & 0xff;
33
- bytes[2] = (lastTimestamp / 0x1000000) & 0xff;
34
- bytes[3] = (lastTimestamp / 0x10000) & 0xff;
35
- bytes[4] = (lastTimestamp / 0x100) & 0xff;
36
- bytes[5] = lastTimestamp & 0xff;
37
- bytes[6] = 0x70 | ((sequence >>> 28) & 0x0f);
38
- bytes[7] = (sequence >>> 20) & 0xff;
39
- bytes[8] = 0x80 | ((sequence >>> 14) & 0x3f);
40
- bytes[9] = (sequence >>> 6) & 0xff;
41
- bytes[10] = ((sequence & 0x3f) << 2) | (random[10] & 0x03);
42
- bytes[11] = random[11];
43
- bytes[12] = random[12];
44
- bytes[13] = random[13];
45
- bytes[14] = random[14];
46
- bytes[15] = random[15];
47
-
48
- return formatUuid(bytes);
49
- }
50
-
51
- function formatUuid(bytes: Uint8Array): string {
52
- const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0"));
53
- return `${hex.slice(0, 4).join("")}-${hex.slice(4, 6).join("")}-${hex.slice(6, 8).join("")}-${hex.slice(8, 10).join("")}-${hex.slice(10, 16).join("")}`;
54
- }