@matatbread/matbot-core 0.4.17 → 0.4.19

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.
package/src/session.ts CHANGED
@@ -1,13 +1,14 @@
1
- import type { Session, Message } from './types.js';
1
+ import type { Session, SessionStatus, Message } from './types.js';
2
2
 
3
3
  // `createMessage` lives in plugin-api, where the hook registry can also reach it (core is downstream of
4
4
  // that package). Re-exported here so it stays where every consumer already imports it from.
5
5
  export { createMessage } from '@matatbread/matbot-plugin-api';
6
6
 
7
7
  export interface CreateSessionOpts {
8
- title?: string;
9
- parentSessionId?: string;
10
- branchPointMessageId?: string;
8
+ title?: string;
9
+ /** Defaults to `'active'`. `'archived'` creates it hidden: off the default session list from the
10
+ * start, rather than created visible and hidden by a second write. */
11
+ status?: SessionStatus;
11
12
  }
12
13
 
13
14
  export function createSession(opts: CreateSessionOpts = {}): Session {
@@ -15,10 +16,8 @@ export function createSession(opts: CreateSessionOpts = {}): Session {
15
16
  return {
16
17
  id: crypto.randomUUID(),
17
18
  version: crypto.randomUUID(),
18
- ...(opts.title !== undefined ? { title: opts.title } : {}),
19
- ...(opts.parentSessionId !== undefined ? { parentSessionId: opts.parentSessionId } : {}),
20
- ...(opts.branchPointMessageId !== undefined ? { branchPointMessageId: opts.branchPointMessageId } : {}),
21
- status: 'active',
19
+ ...(opts.title !== undefined ? { title: opts.title } : {}),
20
+ status: opts.status ?? 'active',
22
21
  messages: [],
23
22
  createdAt: now,
24
23
  updatedAt: now,
@@ -1,3 +1,4 @@
1
1
  export * from './query/index.js';
2
2
  export { CachingStorageBackend, type CachingOptions, type CacheNamespaceStats } from './caching-backend.js';
3
3
  export { mediumGuard } from './medium-guard.js';
4
+ export { MemoryStore } from './memory-store.js';
@@ -0,0 +1,34 @@
1
+ import type { Store, CASResult, StoreQuery, QueryResult } from '@matatbread/matbot-plugin-api';
2
+ import { executeQuery } from './query/index.js';
3
+
4
+ /** A `Store` held in a `Map`: nothing persists, nothing is announced, nothing outlives the instance. */
5
+ export class MemoryStore<T extends { id: string; version: string }> implements Store<T> {
6
+ private readonly items = new Map<string, T>();
7
+
8
+ async get(id: string): Promise<T | null> {
9
+ return this.items.get(id) ?? null;
10
+ }
11
+
12
+ async set(id: string, value: T): Promise<void> {
13
+ this.items.set(id, value);
14
+ }
15
+
16
+ async cas(id: string, expected: string, next: T): Promise<CASResult<T>> {
17
+ const current = this.items.get(id) ?? null;
18
+ if (current === null || current.version !== expected) return { ok: false, current };
19
+ this.items.set(id, next);
20
+ return { ok: true, doc: next };
21
+ }
22
+
23
+ async delete(id: string, expectedVersion?: string): Promise<boolean> {
24
+ if (expectedVersion !== undefined) {
25
+ const current = this.items.get(id);
26
+ if (current === undefined || current.version !== expectedVersion) return false;
27
+ }
28
+ return this.items.delete(id);
29
+ }
30
+
31
+ async query(q: StoreQuery): Promise<QueryResult<T>> {
32
+ return executeQuery([...this.items.values()], q);
33
+ }
34
+ }