@simulacra-ai/session 0.0.2 → 0.0.4

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.
@@ -1,60 +0,0 @@
1
- import type { Message } from "@simulacra-ai/core";
2
- import type { SessionMetadata, SessionStore } from "./types.ts";
3
- /**
4
- * A file-based implementation of SessionStore.
5
- *
6
- * This store persists sessions as JSON files in a specified directory. Each session
7
- * is stored in a separate file named with its session ID. The store also maintains
8
- * hard links for fork relationships to enable efficient querying of session trees.
9
- */
10
- export declare class FileSessionStore implements SessionStore {
11
- #private;
12
- /**
13
- * Creates a new FileSessionStore instance.
14
- *
15
- * @param root - The absolute path to the directory where session files will be stored.
16
- */
17
- constructor(root: string);
18
- /**
19
- * Lists all sessions stored in the file system.
20
- *
21
- * Reads all JSON files from the root directory and parses their metadata.
22
- *
23
- * @returns A promise that resolves to an array of session metadata, sorted by most recently updated first.
24
- */
25
- list(): Promise<SessionMetadata[]>;
26
- /**
27
- * Loads a session from the file system.
28
- *
29
- * @param id - The unique identifier of the session to load.
30
- * @returns A promise that resolves to the session metadata and messages, or undefined if not found.
31
- */
32
- load(id: string): Promise<{
33
- metadata: SessionMetadata;
34
- messages: Message[];
35
- } | undefined>;
36
- /**
37
- * Saves a session to the file system.
38
- *
39
- * Creates a new session file if the ID does not exist, or updates an existing file.
40
- * Automatically updates the updated_at timestamp and message_count. If the session
41
- * has a parent, creates a hard link in the parent's fork directory for efficient querying.
42
- *
43
- * @param id - The unique identifier of the session.
44
- * @param messages - The messages to store for this session.
45
- * @param metadata - Optional partial metadata to merge with existing metadata.
46
- * @returns A promise that resolves when the save operation is complete.
47
- */
48
- save(id: string, messages: Message[], metadata?: Partial<SessionMetadata>): Promise<void>;
49
- /**
50
- * Deletes a session from the file system.
51
- *
52
- * Removes the session file, any hard links in parent fork directories, and the session's
53
- * own fork directory if it exists.
54
- *
55
- * @param id - The unique identifier of the session to delete.
56
- * @returns A promise that resolves to true if the session was deleted, false if it was not found.
57
- */
58
- delete(id: string): Promise<boolean>;
59
- }
60
- //# sourceMappingURL=file-session-store.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"file-session-store.d.ts","sourceRoot":"","sources":["../src/file-session-store.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAOhE;;;;;;GAMG;AACH,qBAAa,gBAAiB,YAAW,YAAY;;IAGnD;;;;OAIG;gBACS,IAAI,EAAE,MAAM;IAIxB;;;;;;OAMG;IACG,IAAI,IAAI,OAAO,CAAC,eAAe,EAAE,CAAC;IAqBxC;;;;;OAKG;IACG,IAAI,CAAC,EAAE,EAAE,MAAM;kBAyGuB,eAAe;;;IArG3D;;;;;;;;;;;OAWG;IACG,IAAI,CAAC,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,EAAE,QAAQ,CAAC,EAAE,OAAO,CAAC,eAAe,CAAC;IA2B/E;;;;;;;;OAQG;IACG,MAAM,CAAC,EAAE,EAAE,MAAM;CA0ExB"}
@@ -1,166 +0,0 @@
1
- import fs from "node:fs/promises";
2
- import path from "node:path";
3
- /**
4
- * A file-based implementation of SessionStore.
5
- *
6
- * This store persists sessions as JSON files in a specified directory. Each session
7
- * is stored in a separate file named with its session ID. The store also maintains
8
- * hard links for fork relationships to enable efficient querying of session trees.
9
- */
10
- export class FileSessionStore {
11
- #root;
12
- /**
13
- * Creates a new FileSessionStore instance.
14
- *
15
- * @param root - The absolute path to the directory where session files will be stored.
16
- */
17
- constructor(root) {
18
- this.#root = root;
19
- }
20
- /**
21
- * Lists all sessions stored in the file system.
22
- *
23
- * Reads all JSON files from the root directory and parses their metadata.
24
- *
25
- * @returns A promise that resolves to an array of session metadata, sorted by most recently updated first.
26
- */
27
- async list() {
28
- await this.#ensure_dir();
29
- const sessions = [];
30
- const entries = await fs.readdir(this.#root, { withFileTypes: true });
31
- for (const entry of entries) {
32
- if (!entry.isFile() || !entry.name.endsWith(".json")) {
33
- continue;
34
- }
35
- const session = await this.#read_session(path.join(this.#root, entry.name), entry.name.replace(/\.json$/, ""));
36
- if (session) {
37
- sessions.push(session);
38
- }
39
- }
40
- return sessions.sort((a, b) => b.updated_at.localeCompare(a.updated_at));
41
- }
42
- /**
43
- * Loads a session from the file system.
44
- *
45
- * @param id - The unique identifier of the session to load.
46
- * @returns A promise that resolves to the session metadata and messages, or undefined if not found.
47
- */
48
- async load(id) {
49
- return this.#read_file(path.join(this.#root, `${id}.json`), id);
50
- }
51
- /**
52
- * Saves a session to the file system.
53
- *
54
- * Creates a new session file if the ID does not exist, or updates an existing file.
55
- * Automatically updates the updated_at timestamp and message_count. If the session
56
- * has a parent, creates a hard link in the parent's fork directory for efficient querying.
57
- *
58
- * @param id - The unique identifier of the session.
59
- * @param messages - The messages to store for this session.
60
- * @param metadata - Optional partial metadata to merge with existing metadata.
61
- * @returns A promise that resolves when the save operation is complete.
62
- */
63
- async save(id, messages, metadata) {
64
- const now = new Date().toISOString();
65
- await this.#ensure_dir();
66
- const file_path = path.join(this.#root, `${id}.json`);
67
- const existing = await this.#read_file(file_path, id);
68
- const existing_metadata = existing?.metadata ?? {};
69
- const parent_id = metadata?.parent_id ?? existing_metadata.parent_id;
70
- const file = {
71
- metadata: {
72
- ...existing_metadata,
73
- created_at: existing_metadata.created_at ?? now,
74
- updated_at: now,
75
- message_count: messages.length,
76
- ...metadata,
77
- },
78
- messages,
79
- };
80
- await fs.writeFile(file_path, JSON.stringify(file, null, 2), "utf8");
81
- if (parent_id) {
82
- await this.#ensure_fork_link(parent_id, id);
83
- }
84
- }
85
- /**
86
- * Deletes a session from the file system.
87
- *
88
- * Removes the session file, any hard links in parent fork directories, and the session's
89
- * own fork directory if it exists.
90
- *
91
- * @param id - The unique identifier of the session to delete.
92
- * @returns A promise that resolves to true if the session was deleted, false if it was not found.
93
- */
94
- async delete(id) {
95
- const file_path = path.join(this.#root, `${id}.json`);
96
- try {
97
- const result = await this.#read_file(file_path, id);
98
- await fs.unlink(file_path);
99
- if (result?.metadata.parent_id) {
100
- const link = path.join(this.#root, `${result.metadata.parent_id}-forks`, `${id}.json`);
101
- try {
102
- await fs.unlink(link);
103
- }
104
- catch {
105
- /* link may not exist */
106
- }
107
- }
108
- const fork_dir = path.join(this.#root, `${id}-forks`);
109
- try {
110
- await fs.rm(fork_dir, { recursive: true });
111
- }
112
- catch {
113
- /* no forks */
114
- }
115
- return true;
116
- }
117
- catch {
118
- return false;
119
- }
120
- }
121
- async #ensure_fork_link(parent_id, fork_id) {
122
- const fork_dir = path.join(this.#root, `${parent_id}-forks`);
123
- await fs.mkdir(fork_dir, { recursive: true });
124
- const canonical = path.join(this.#root, `${fork_id}.json`);
125
- const link = path.join(fork_dir, `${fork_id}.json`);
126
- try {
127
- await fs.unlink(link);
128
- }
129
- catch {
130
- /* doesn't exist yet */
131
- }
132
- try {
133
- await fs.link(canonical, link);
134
- }
135
- catch {
136
- // hard links can fail across filesystems — fall back to no index
137
- }
138
- }
139
- async #read_file(file_path, id) {
140
- try {
141
- const content = await fs.readFile(file_path, "utf8");
142
- const data = JSON.parse(content);
143
- return {
144
- metadata: { id, ...data.metadata },
145
- messages: data.messages,
146
- };
147
- }
148
- catch {
149
- return undefined;
150
- }
151
- }
152
- async #read_session(file_path, id) {
153
- try {
154
- const content = await fs.readFile(file_path, "utf8");
155
- const data = JSON.parse(content);
156
- return { id, ...data.metadata };
157
- }
158
- catch {
159
- return undefined;
160
- }
161
- }
162
- async #ensure_dir() {
163
- await fs.mkdir(this.#root, { recursive: true });
164
- }
165
- }
166
- //# sourceMappingURL=file-session-store.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"file-session-store.js","sourceRoot":"","sources":["../src/file-session-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAClC,OAAO,IAAI,MAAM,WAAW,CAAC;A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,CAAC;CACF"}
@@ -1,62 +0,0 @@
1
- import type { Message } from "@simulacra-ai/core";
2
- import type { SessionMetadata, SessionStore } from "./types.ts";
3
- /**
4
- * An in-memory implementation of SessionStore.
5
- *
6
- * This store keeps all session data in memory using a Map. Data is not persisted
7
- * across process restarts. Useful for testing or scenarios where persistence is not required.
8
- */
9
- export declare class InMemorySessionStore implements SessionStore {
10
- #private;
11
- /**
12
- * Lists all sessions stored in memory.
13
- *
14
- * @returns A promise that resolves to an array of session metadata, sorted by most recently updated first.
15
- */
16
- list(): Promise<SessionMetadata[]>;
17
- /**
18
- * Loads a session from memory.
19
- *
20
- * Returns a deep clone of the stored data to prevent external mutations.
21
- *
22
- * @param id - The unique identifier of the session to load.
23
- * @returns A promise that resolves to the session metadata and messages, or undefined if not found.
24
- */
25
- load(id: string): Promise<{
26
- metadata: {
27
- id: string;
28
- created_at: string;
29
- updated_at: string;
30
- provider?: string;
31
- model?: string;
32
- label?: string;
33
- message_count: number;
34
- parent_id?: string;
35
- fork_message_id?: string;
36
- detached?: boolean;
37
- is_checkpoint?: boolean;
38
- checkpoint_state?: import("@simulacra-ai/core").CheckpointState;
39
- };
40
- messages: Message[];
41
- } | undefined>;
42
- /**
43
- * Saves a session to memory.
44
- *
45
- * Creates a new session if the ID does not exist, or updates an existing session.
46
- * Automatically updates the updated_at timestamp and message_count.
47
- *
48
- * @param id - The unique identifier of the session.
49
- * @param messages - The messages to store for this session.
50
- * @param metadata - Optional partial metadata to merge with existing metadata.
51
- * @returns A promise that resolves when the save operation is complete.
52
- */
53
- save(id: string, messages: Message[], metadata?: Partial<SessionMetadata>): Promise<void>;
54
- /**
55
- * Deletes a session from memory.
56
- *
57
- * @param id - The unique identifier of the session to delete.
58
- * @returns A promise that resolves to true if the session was deleted, false if it was not found.
59
- */
60
- delete(id: string): Promise<boolean>;
61
- }
62
- //# sourceMappingURL=in-memory-session-store.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"in-memory-session-store.d.ts","sourceRoot":"","sources":["../src/in-memory-session-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAEhE;;;;;GAKG;AACH,qBAAa,oBAAqB,YAAW,YAAY;;IAGvD;;;;OAIG;IACG,IAAI,IAAI,OAAO,CAAC,eAAe,EAAE,CAAC;IAMxC;;;;;;;OAOG;IACG,IAAI,CAAC,EAAE,EAAE,MAAM;;;;;;;;;;;;;;;;;IAWrB;;;;;;;;;;OAUG;IACG,IAAI,CAAC,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,EAAE,QAAQ,CAAC,EAAE,OAAO,CAAC,eAAe,CAAC;IAiB/E;;;;;OAKG;IACG,MAAM,CAAC,EAAE,EAAE,MAAM;CAGxB"}
@@ -1,73 +0,0 @@
1
- /**
2
- * An in-memory implementation of SessionStore.
3
- *
4
- * This store keeps all session data in memory using a Map. Data is not persisted
5
- * across process restarts. Useful for testing or scenarios where persistence is not required.
6
- */
7
- export class InMemorySessionStore {
8
- #sessions = new Map();
9
- /**
10
- * Lists all sessions stored in memory.
11
- *
12
- * @returns A promise that resolves to an array of session metadata, sorted by most recently updated first.
13
- */
14
- async list() {
15
- return [...this.#sessions.values()]
16
- .map((s) => s.metadata)
17
- .sort((a, b) => b.updated_at.localeCompare(a.updated_at));
18
- }
19
- /**
20
- * Loads a session from memory.
21
- *
22
- * Returns a deep clone of the stored data to prevent external mutations.
23
- *
24
- * @param id - The unique identifier of the session to load.
25
- * @returns A promise that resolves to the session metadata and messages, or undefined if not found.
26
- */
27
- async load(id) {
28
- const entry = this.#sessions.get(id);
29
- if (!entry) {
30
- return undefined;
31
- }
32
- return {
33
- metadata: { ...entry.metadata },
34
- messages: structuredClone(entry.messages),
35
- };
36
- }
37
- /**
38
- * Saves a session to memory.
39
- *
40
- * Creates a new session if the ID does not exist, or updates an existing session.
41
- * Automatically updates the updated_at timestamp and message_count.
42
- *
43
- * @param id - The unique identifier of the session.
44
- * @param messages - The messages to store for this session.
45
- * @param metadata - Optional partial metadata to merge with existing metadata.
46
- * @returns A promise that resolves when the save operation is complete.
47
- */
48
- async save(id, messages, metadata) {
49
- const now = new Date().toISOString();
50
- const existing = this.#sessions.get(id);
51
- this.#sessions.set(id, {
52
- metadata: {
53
- id,
54
- ...existing?.metadata,
55
- created_at: existing?.metadata.created_at ?? now,
56
- updated_at: now,
57
- message_count: messages.length,
58
- ...metadata,
59
- },
60
- messages: structuredClone(messages),
61
- });
62
- }
63
- /**
64
- * Deletes a session from memory.
65
- *
66
- * @param id - The unique identifier of the session to delete.
67
- * @returns A promise that resolves to true if the session was deleted, false if it was not found.
68
- */
69
- async delete(id) {
70
- return this.#sessions.delete(id);
71
- }
72
- }
73
- //# sourceMappingURL=in-memory-session-store.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"in-memory-session-store.js","sourceRoot":"","sources":["../src/in-memory-session-store.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,MAAM,OAAO,oBAAoB;IACtB,SAAS,GAAG,IAAI,GAAG,EAA8D,CAAC;IAE3F;;;;OAIG;IACH,KAAK,CAAC,IAAI;QACR,OAAO,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC;aAChC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC;aACtB,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC;IAC9D,CAAC;IAED;;;;;;;OAOG;IACH,KAAK,CAAC,IAAI,CAAC,EAAU;QACnB,MAAM,KAAK,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QACrC,IAAI,CAAC,KAAK,EAAE,CAAC;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+0,0 @@
1
- import type { Conversation } from "@simulacra-ai/core";
2
- import type { SessionMetadata, SessionStore, SessionManagerOptions, SessionManagerEvents } from "./types.ts";
3
- /**
4
- * Manages conversation sessions including creation, loading, saving, and forking.
5
- *
6
- * The SessionManager acts as a bridge between a Conversation instance and a SessionStore,
7
- * handling session lifecycle, automatic saving, and session tree management through forking.
8
- * It supports disposable resource management and event emission for session operations.
9
- */
10
- export declare class SessionManager {
11
- #private;
12
- /**
13
- * Creates a new SessionManager instance.
14
- *
15
- * @param store - The storage backend to use for persisting sessions.
16
- * @param conversation - The conversation instance to manage.
17
- * @param options - Optional configuration for session management behavior.
18
- */
19
- constructor(store: SessionStore, conversation: Conversation, options?: SessionManagerOptions);
20
- /**
21
- * The ID of the currently active session.
22
- *
23
- * @returns The session ID if a session is active, otherwise undefined.
24
- */
25
- get current_session_id(): string | undefined;
26
- /**
27
- * Whether a session is currently loaded.
28
- *
29
- * @returns True if a session is active, false otherwise.
30
- */
31
- get is_loaded(): boolean;
32
- /**
33
- * Disposes of the SessionManager and cleans up resources.
34
- *
35
- * This method removes event listeners, disposes child sessions, and emits the dispose event.
36
- * It is called automatically when the associated conversation is disposed or when using
37
- * explicit resource management.
38
- */
39
- [Symbol.dispose](): void;
40
- /**
41
- * Starts a new session with a freshly generated ID.
42
- *
43
- * This method clears the conversation history and creates a new session. If auto_save
44
- * is enabled or a label is provided, the session is immediately saved to the store.
45
- *
46
- * @param label - Optional label to assign to the new session.
47
- * @returns The generated session ID.
48
- */
49
- start_new(label?: string): string;
50
- /**
51
- * Creates a new session as a fork of an existing parent session.
52
- *
53
- * A fork creates a new session that inherits messages from the parent session up to
54
- * the fork point. If detached, the fork does not inherit any messages from the parent
55
- * but still maintains a parent reference for organizational purposes.
56
- *
57
- * @param parent_session_id - The ID of the session to fork from.
58
- * @param options - Optional configuration for the fork operation.
59
- * @param options.detached - Whether to create a detached fork that does not inherit parent messages.
60
- * @returns A promise that resolves to the generated session ID for the fork.
61
- */
62
- fork(parent_session_id: string, options?: {
63
- detached?: boolean;
64
- }): Promise<string>;
65
- /**
66
- * Loads a session by ID or loads the most recent session if no ID is provided.
67
- *
68
- * If no session ID is provided and no sessions exist in the store, this method
69
- * starts a new session automatically. Loading a session resolves its full message
70
- * history by recursively following parent references.
71
- *
72
- * @param id - The ID of the session to load, or undefined to load the most recent session.
73
- * @returns A promise that resolves when the session is loaded.
74
- * @throws {Error} If the specified session ID is not found in the store.
75
- */
76
- load(id?: string): Promise<void>;
77
- /**
78
- * Saves the current session to the store.
79
- *
80
- * This method persists only the messages owned by this session, excluding any messages
81
- * inherited from parent sessions. If auto_slug is enabled and no label has been set,
82
- * a label is automatically generated from the first user message.
83
- *
84
- * @param metadata - Optional partial metadata to update on the session.
85
- * @returns A promise that resolves when the save operation is complete.
86
- * @throws {Error} If no session is currently active.
87
- */
88
- save(metadata?: Partial<Pick<SessionMetadata, "label" | "provider" | "model" | "parent_id" | "fork_message_id" | "detached">>): Promise<void>;
89
- /**
90
- * Lists all sessions stored in the store.
91
- *
92
- * @returns A promise that resolves to an array of session metadata, typically sorted by last update time.
93
- */
94
- list(): Promise<SessionMetadata[]>;
95
- /**
96
- * Deletes a session from the store.
97
- *
98
- * If the deleted session is currently active, the conversation is cleared and the
99
- * current session is unset.
100
- *
101
- * @param id - The ID of the session to delete.
102
- * @returns A promise that resolves to true if the session was deleted, false if it was not found.
103
- */
104
- delete(id: string): Promise<boolean>;
105
- /**
106
- * Renames a session by updating its label.
107
- *
108
- * @param id - The ID of the session to rename.
109
- * @param label - The new label to assign to the session.
110
- * @returns A promise that resolves when the rename operation is complete.
111
- * @throws {Error} If the specified session ID is not found in the store.
112
- */
113
- rename(id: string, label: string): Promise<void>;
114
- /**
115
- * Registers an event listener for the specified event.
116
- *
117
- * @param event - The name of the event to listen for.
118
- * @param listener - The callback function to invoke when the event is emitted.
119
- * @returns This SessionManager instance for method chaining.
120
- */
121
- on<E extends keyof SessionManagerEvents>(event: E, listener: (...args: SessionManagerEvents[E]) => void): this;
122
- /**
123
- * Removes an event listener for the specified event.
124
- *
125
- * @param event - The name of the event to stop listening for.
126
- * @param listener - The callback function to remove.
127
- * @returns This SessionManager instance for method chaining.
128
- */
129
- off<E extends keyof SessionManagerEvents>(event: E, listener: (...args: SessionManagerEvents[E]) => void): this;
130
- }
131
- //# sourceMappingURL=session-manager.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"session-manager.d.ts","sourceRoot":"","sources":["../src/session-manager.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAW,MAAM,oBAAoB,CAAC;AAChE,OAAO,KAAK,EACV,eAAe,EACf,YAAY,EACZ,qBAAqB,EACrB,oBAAoB,EACrB,MAAM,YAAY,CAAC;AAEpB;;;;;;GAMG;AACH,qBAAa,cAAc;;IAazB;;;;;;OAMG;gBACS,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,YAAY,EAAE,OAAO,CAAC,EAAE,qBAAqB;IAa5F;;;;OAIG;IACH,IAAI,kBAAkB,uBAErB;IAED;;;;OAIG;IACH,IAAI,SAAS,YAEZ;IAED;;;;;;OAMG;IACH,CAAC,MAAM,CAAC,OAAO,CAAC;IAkBhB;;;;;;;;OAQG;IACH,SAAS,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM;IAmBjC;;;;;;;;;;;OAWG;IACG,IAAI,CAAC,iBAAiB,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;QAAE,QAAQ,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,OAAO,CAAC,MAAM,CAAC;IAqCxF;;;;;;;;;;OAUG;IACG,IAAI,CAAC,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IA2BtC;;;;;;;;;;OAUG;IACG,IAAI,CACR,QAAQ,CAAC,EAAE,OAAO,CAChB,IAAI,CACF,eAAe,EACf,OAAO,GAAG,UAAU,GAAG,OAAO,GAAG,WAAW,GAAG,iBAAiB,GAAG,UAAU,CAC9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