@coasys/ad4m 0.13.0-test-7 → 0.13.0-test-8-profile-cache-fix

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 (63) hide show
  1. package/lib/index.cjs +1121 -835
  2. package/lib/index.js +1108 -835
  3. package/lib/src/Ad4mClient.d.ts +4 -1
  4. package/lib/src/Ad4mClient.js +2 -2
  5. package/lib/src/agent/AgentClient.d.ts +3 -0
  6. package/lib/src/agent/AgentClient.js +27 -5
  7. package/lib/src/ai/AIClient.js +1 -1
  8. package/lib/src/apiClient.d.ts +5 -1
  9. package/lib/src/apiClient.js +14 -6
  10. package/lib/src/index.d.ts +4 -0
  11. package/lib/src/index.js +4 -0
  12. package/lib/src/model/Ad4mModel.d.ts +30 -36
  13. package/lib/src/model/Ad4mModel.js +93 -382
  14. package/lib/src/model/ModelQueryBuilder.d.ts +5 -10
  15. package/lib/src/model/ModelQueryBuilder.js +142 -62
  16. package/lib/src/model/decorators.d.ts +7 -4
  17. package/lib/src/model/decorators.js +0 -7
  18. package/lib/src/model/query-sparql.d.ts +0 -10
  19. package/lib/src/model/query-sparql.js +12 -17
  20. package/lib/src/model/shacl-gen.js +120 -47
  21. package/lib/src/model/types.d.ts +140 -4
  22. package/lib/src/perspectives/PerspectiveClient.d.ts +13 -4
  23. package/lib/src/perspectives/PerspectiveClient.js +22 -6
  24. package/lib/src/perspectives/PerspectiveProxy.d.ts +27 -7
  25. package/lib/src/perspectives/PerspectiveProxy.js +60 -12
  26. package/lib/src/perspectives/SparqlBindings.d.ts +47 -0
  27. package/lib/src/perspectives/SparqlBindings.js +65 -0
  28. package/lib/src/perspectives/WakerSubscriptionManager.js +5 -4
  29. package/lib/src/shacl/NodeExpression.d.ts +44 -0
  30. package/lib/src/shacl/NodeExpression.js +39 -0
  31. package/lib/src/shacl/SHACLShape.d.ts +23 -0
  32. package/lib/src/shacl/SHACLShape.js +115 -1
  33. package/lib/src/shacl/builders.d.ts +58 -0
  34. package/lib/src/shacl/builders.js +76 -0
  35. package/lib/src/shacl/index.d.ts +36 -0
  36. package/lib/src/shacl/index.js +33 -0
  37. package/package.json +1 -1
  38. package/src/Ad4mClient.ts +7 -2
  39. package/src/agent/AgentClient.ts +35 -7
  40. package/src/ai/AIClient.ts +1 -1
  41. package/src/apiClient.ts +17 -6
  42. package/src/index.ts +4 -0
  43. package/src/model/Ad4mModel.test.ts +274 -72
  44. package/src/model/Ad4mModel.ts +140 -420
  45. package/src/model/ModelQueryBuilder.ts +136 -71
  46. package/src/model/decorators.ts +7 -13
  47. package/src/model/query-sparql.test.ts +36 -20
  48. package/src/model/query-sparql.ts +12 -18
  49. package/src/model/relation-filtering.test.ts +46 -39
  50. package/src/model/shacl-gen.ts +128 -54
  51. package/src/model/shacl-roundtrip.test.ts +197 -0
  52. package/src/model/types-typed-query.test.ts +360 -0
  53. package/src/model/types.ts +196 -4
  54. package/src/perspectives/PerspectiveClient.ts +24 -8
  55. package/src/perspectives/PerspectiveProxy.test.ts +85 -1
  56. package/src/perspectives/PerspectiveProxy.ts +65 -20
  57. package/src/perspectives/SparqlBindings.ts +79 -0
  58. package/src/perspectives/WakerSubscriptionManager.ts +5 -4
  59. package/src/shacl/NodeExpression.ts +54 -0
  60. package/src/shacl/SHACLShape.test.ts +172 -0
  61. package/src/shacl/SHACLShape.ts +173 -1
  62. package/src/shacl/builders.ts +86 -0
  63. package/src/shacl/index.ts +72 -0
@@ -0,0 +1,36 @@
1
+ /**
2
+ * SHACL-AF Transform DSL — public API
3
+ *
4
+ * Re-exports builder functions and provides common built-in transforms.
5
+ */
6
+ import { focus, literal, path, exists, ifExpr, concat, coalesce, fn } from './builders';
7
+ export type { NodeExpression } from './NodeExpression';
8
+ export { isNodeExpression } from './NodeExpression';
9
+ export { focus, literal, path, exists, ifExpr, concat, coalesce, fn, };
10
+ /**
11
+ * Built-in transform: FileData → data: URI
12
+ *
13
+ * Converts a file stored via FILE_STORAGE_LANGUAGE into a data: URL.
14
+ * If the object has a `data_base64` field, returns:
15
+ * `data:{file_type};base64,{data_base64}`
16
+ * Otherwise passes through the input unchanged.
17
+ *
18
+ * This is the Rust equivalent of the JS `defaultFileDecode` function.
19
+ *
20
+ * Usage:
21
+ * ```typescript
22
+ * @Property({
23
+ * through: 'we://image',
24
+ * resolveLanguage: FILE_STORAGE_LANGUAGE,
25
+ * transform: fileToDataUri,
26
+ * })
27
+ * avatar?: string;
28
+ * ```
29
+ */
30
+ export declare const fileToDataUri: Readonly<{
31
+ readonly else: import("./NodeExpression").NodeExpression;
32
+ readonly type: "if";
33
+ readonly cond: import("./NodeExpression").NodeExpression;
34
+ readonly then: import("./NodeExpression").NodeExpression;
35
+ }>;
36
+ export { SHACLShape, SHACLPropertyShape, AD4MAction, ConformanceCondition, } from './SHACLShape';
@@ -0,0 +1,33 @@
1
+ /**
2
+ * SHACL-AF Transform DSL — public API
3
+ *
4
+ * Re-exports builder functions and provides common built-in transforms.
5
+ */
6
+ import { focus, literal, path, exists, ifExpr, concat, coalesce, fn, } from './builders';
7
+ export { isNodeExpression } from './NodeExpression';
8
+ export { focus, literal, path, exists, ifExpr, concat, coalesce, fn, };
9
+ /**
10
+ * Built-in transform: FileData → data: URI
11
+ *
12
+ * Converts a file stored via FILE_STORAGE_LANGUAGE into a data: URL.
13
+ * If the object has a `data_base64` field, returns:
14
+ * `data:{file_type};base64,{data_base64}`
15
+ * Otherwise passes through the input unchanged.
16
+ *
17
+ * This is the Rust equivalent of the JS `defaultFileDecode` function.
18
+ *
19
+ * Usage:
20
+ * ```typescript
21
+ * @Property({
22
+ * through: 'we://image',
23
+ * resolveLanguage: FILE_STORAGE_LANGUAGE,
24
+ * transform: fileToDataUri,
25
+ * })
26
+ * avatar?: string;
27
+ * ```
28
+ */
29
+ export const fileToDataUri = ifExpr(exists(path('file_storage://data_base64')))
30
+ .then(concat(literal('data:'), coalesce(path('file_storage://file_type'), literal('image/png')), literal(';base64,'), path('file_storage://data_base64')))
31
+ .else(focus());
32
+ // Re-export SHACL types for convenience
33
+ export { SHACLShape, } from './SHACLShape';
package/package.json CHANGED
@@ -48,7 +48,7 @@
48
48
  "ws": "8.13.0"
49
49
  },
50
50
  "patchedDependencies": {},
51
- "version": "0.13.0-test-7",
51
+ "version": "0.13.0-test-8-profile-cache-fix",
52
52
  "scripts": {
53
53
  "build": "patch-package && tsc && pnpm run bundle",
54
54
  "bundle": "rollup -c rollup.config.js",
package/src/Ad4mClient.ts CHANGED
@@ -29,10 +29,15 @@ export class Ad4mClient {
29
29
  #runtimeClient: RuntimeClient
30
30
  #aiClient: AIClient
31
31
 
32
- constructor(baseUrl: string, token?: string, subscribe: boolean = true) {
32
+ constructor(
33
+ baseUrl: string,
34
+ token?: string,
35
+ subscribe: boolean = true,
36
+ options?: { webSocketImpl?: new (url: string) => WebSocket; fetchImpl?: typeof fetch }
37
+ ) {
33
38
  this.#baseUrl = baseUrl
34
39
  this.#token = token
35
- this.#apiClient = new ApiClient(baseUrl, token)
40
+ this.#apiClient = new ApiClient(baseUrl, token, options?.webSocketImpl, options?.fetchImpl)
36
41
  this.#agentClient = new AgentClient(baseUrl, token, subscribe, this.#apiClient)
37
42
  this.#expressionClient = new ExpressionClient(baseUrl, token, this.#apiClient)
38
43
  this.#languageClient = new LanguageClient(baseUrl, token, this.#apiClient)
@@ -53,11 +53,15 @@ export class AgentClient {
53
53
  // L1: in-memory promise cache with timestamps for TTL
54
54
  #memCache = new Map<string, { promise: Promise<Agent>; ts: number }>();
55
55
  // L2: persistent cache (IndexedDB in browser, NullCache in Node)
56
- #persistent: PersistentCache<Agent>;
56
+ // Entries are wrapped with a timestamp so the TTL can be enforced across restarts.
57
+ #persistent: PersistentCache<{ agent: Agent; ts: number }>;
57
58
  // Self-DID for event-driven invalidation (no TTL for own profile)
58
59
  #selfDid: string | null = null;
59
60
  /** TTL for remote (non-self) agent profiles in L1 cache (ms). */
60
61
  static REMOTE_AGENT_TTL_MS = 5 * 60_000; // 5 minutes
62
+ /** TTL for remote agent profiles in the L2 (IndexedDB) cache (ms).
63
+ * After this window any restart triggers a fresh network fetch. */
64
+ static REMOTE_AGENT_TTL_L2_MS = 5 * 60_000; // 5 minutes
61
65
 
62
66
  constructor(baseUrl: string, token?: string, subscribe: boolean = true, sharedApiClient?: ApiClient) {
63
67
  this.#baseUrl = baseUrl;
@@ -69,7 +73,7 @@ export class AgentClient {
69
73
  this.#hostingUserInfoChangedCallbacks = [];
70
74
  this.#computeLogUpdatedCallbacks = [];
71
75
  this.#unsubscribers = [];
72
- this.#persistent = createPersistentCache<Agent>('ad4m-agent-cache', 'agents');
76
+ this.#persistent = createPersistentCache<{ agent: Agent; ts: number }>('ad4m-agent-cache', 'agents');
73
77
 
74
78
  if (subscribe) {
75
79
  this.subscribeAgentUpdated();
@@ -82,6 +86,13 @@ export class AgentClient {
82
86
  const agent = await this.#apiClient.call<Agent>('agent.get');
83
87
  let agentObject = new Agent(agent.did, agent.perspective);
84
88
  agentObject.directMessageLanguage = agent.directMessageLanguage;
89
+
90
+ // Auto-set selfDid so event-driven cache invalidation works
91
+ // without requiring apps to call setSelfDid() manually
92
+ if (agentObject.did && !this.#selfDid) {
93
+ this.#selfDid = agentObject.did;
94
+ }
95
+
85
96
  return agentObject;
86
97
  }
87
98
 
@@ -126,14 +137,15 @@ export class AgentClient {
126
137
  // Deduplicate: store the promise immediately so concurrent calls share one RPC
127
138
  const promise = (async () => {
128
139
  // L2: check persistent cache before network
129
- const persisted = await this.#persistent.get(did);
130
- if (persisted) {
131
- return persisted;
140
+ const entry = await this.#persistent.get(did);
141
+ // entry.agent may be undefined for pre-TTL cache entries (migration) — treat as expired
142
+ if (entry?.agent && (now - (entry.ts ?? 0)) < AgentClient.REMOTE_AGENT_TTL_L2_MS) {
143
+ return entry.agent;
132
144
  }
133
145
 
134
146
  // L3: network fetch
135
147
  const result = await this.#apiClient.call<Agent>('agent.byDid', { did });
136
- this.#persistent.put(did, result); // fire-and-forget write to L2
148
+ this.#persistent.put(did, { agent: result, ts: Date.now() }); // fire-and-forget write to L2
137
149
  return result;
138
150
  })();
139
151
 
@@ -186,6 +198,14 @@ export class AgentClient {
186
198
  const a = await this.#apiClient.call<Agent>('agent.updateProfile', { publicPerspective: cleanedPerspective });
187
199
  const agent = new Agent(a.did, a.perspective);
188
200
  agent.directMessageLanguage = a.directMessageLanguage;
201
+
202
+ // Immediately update byDID cache so subsequent byDID() calls
203
+ // return fresh data without waiting for the subscription event
204
+ if (agent.did) {
205
+ this.#memCache.set(agent.did, { promise: Promise.resolve(agent), ts: Date.now() });
206
+ this.#persistent.put(agent.did, { agent, ts: Date.now() }); // fire-and-forget
207
+ }
208
+
189
209
  return agent;
190
210
  }
191
211
 
@@ -216,6 +236,14 @@ export class AgentClient {
216
236
  const a = await this.#apiClient.call<Agent>('agent.updateProfile', { dmLanguage: directMessageLanguage });
217
237
  const agent = new Agent(a.did, a.perspective);
218
238
  agent.directMessageLanguage = a.directMessageLanguage;
239
+
240
+ // Immediately update byDID cache so subsequent byDID() calls
241
+ // return fresh data without waiting for the subscription event
242
+ if (agent.did) {
243
+ this.#memCache.set(agent.did, { promise: Promise.resolve(agent), ts: Date.now() });
244
+ this.#persistent.put(agent.did, { agent, ts: Date.now() }); // fire-and-forget
245
+ }
246
+
219
247
  return agent;
220
248
  }
221
249
 
@@ -254,7 +282,7 @@ export class AgentClient {
254
282
  promise: Promise.resolve(agent),
255
283
  ts: Date.now(),
256
284
  });
257
- this.#persistent.put(agent.did, agent); // fire-and-forget
285
+ this.#persistent.put(agent.did, { agent, ts: Date.now() }); // fire-and-forget
258
286
  }
259
287
 
260
288
  this.#updatedCallbacks.forEach((cb) => cb(agent));
@@ -156,7 +156,7 @@ export class AIClient {
156
156
  typedAudio.byteOffset + typedAudio.byteLength
157
157
  );
158
158
 
159
- const response = await fetch(`${baseUrl}/api/v1/ai/transcription/feed`, {
159
+ const response = await this.#apiClient.doFetch(`${baseUrl}/api/v1/ai/transcription/feed`, {
160
160
  method: 'POST',
161
161
  headers: {
162
162
  'Content-Type': 'application/octet-stream',
package/src/apiClient.ts CHANGED
@@ -43,15 +43,24 @@ interface PendingCall {
43
43
  export class ApiClient {
44
44
  private baseUrl: string
45
45
  private token?: string
46
+ private readonly _webSocketImpl?: new (url: string) => WebSocket
47
+ private readonly _fetchImpl?: typeof fetch
46
48
 
47
- constructor(baseUrl: string, token?: string) {
49
+ constructor(baseUrl: string, token?: string, webSocketImpl?: new (url: string) => WebSocket, fetchImpl?: typeof fetch) {
48
50
  this.baseUrl = baseUrl
49
51
  this.token = token
52
+ this._webSocketImpl = webSocketImpl
53
+ this._fetchImpl = fetchImpl
50
54
  }
51
55
 
52
56
  getBaseUrl(): string { return this.baseUrl }
53
57
  getToken(): string | undefined { return this.token }
54
58
 
59
+ /** Perform an HTTP request, using an injected fetch implementation if provided. */
60
+ doFetch(url: string, init: RequestInit): Promise<Response> {
61
+ return this._fetchImpl ? this._fetchImpl(url, init) : fetch(url, init)
62
+ }
63
+
55
64
  setToken(token: string) {
56
65
  this.token = token
57
66
  }
@@ -78,7 +87,7 @@ export class ApiClient {
78
87
  }
79
88
 
80
89
  private _ensureWs(): void {
81
- if (this._ws && (this._ws.readyState === WebSocket.OPEN || this._ws.readyState === WebSocket.CONNECTING)) {
90
+ if (this._ws && (this._ws.readyState === 1 /* OPEN */ || this._ws.readyState === 0 /* CONNECTING */)) {
82
91
  return
83
92
  }
84
93
  // Prevent duplicate connections from concurrent callers
@@ -91,7 +100,8 @@ export class ApiClient {
91
100
  })
92
101
 
93
102
  const url = this._getWsUrl()
94
- const ws = new WebSocket(url)
103
+ const WsImpl = this._webSocketImpl ?? WebSocket
104
+ const ws = new WsImpl(url)
95
105
  this._ws = ws
96
106
 
97
107
  ws.onopen = () => {
@@ -144,13 +154,14 @@ export class ApiClient {
144
154
  this._wsReady = null
145
155
 
146
156
  // Reject all pending calls
157
+ const hadPendingCalls = this._pendingCalls.size > 0
147
158
  for (const [id, pending] of this._pendingCalls) {
148
159
  clearTimeout(pending.timer)
149
160
  pending.reject(new RpcError(503, 'WebSocket connection closed'))
150
161
  }
151
162
  this._pendingCalls.clear()
152
163
 
153
- if (!this._wsClosed && (this._wsCallbacks.size > 0 || this._pendingCalls.size > 0)) {
164
+ if (!this._wsClosed && (this._wsCallbacks.size > 0 || hadPendingCalls)) {
154
165
  this._scheduleReconnect()
155
166
  }
156
167
  }
@@ -159,7 +170,7 @@ export class ApiClient {
159
170
  private _startPing(): void {
160
171
  this._stopPing()
161
172
  this._wsPingTimer = setInterval(() => {
162
- if (this._ws && this._ws.readyState === WebSocket.OPEN) {
173
+ if (this._ws && this._ws.readyState === 1 /* OPEN */) {
163
174
  this._ws.send(JSON.stringify({ type: 'ping' }))
164
175
  }
165
176
  }, 30_000)
@@ -219,7 +230,7 @@ export class ApiClient {
219
230
  timer,
220
231
  })
221
232
 
222
- if (this._ws && this._ws.readyState === WebSocket.OPEN) {
233
+ if (this._ws && this._ws.readyState === 1 /* OPEN */) {
223
234
  this._ws.send(JSON.stringify(message))
224
235
  } else {
225
236
  this._pendingCalls.delete(id)
package/src/index.ts CHANGED
@@ -16,6 +16,7 @@ export * from "./Literal";
16
16
  export * from "./perspectives/Perspective";
17
17
  export * from "./perspectives/PerspectiveHandle";
18
18
  export * from "./perspectives/PerspectiveProxy";
19
+ export * from "./perspectives/SparqlBindings";
19
20
  export * from "./perspectives/WakerSubscriptionManager";
20
21
  export * from "./perspectives/PerspectiveDiff";
21
22
  export * from "./perspectives/LinkQuery";
@@ -33,6 +34,9 @@ export * from "./runtime/RuntimeTypes"
33
34
  export { Model as AIModel, ModelApi, TokenizerSource, LocalModel, ModelType, ModelApiInput, TokenizerSourceInput, LocalModelInput, ModelInput, VoiceActivityParamsInput } from "./ai/AITypes"
34
35
  export * from './shacl/SHACLShape'
35
36
  export { SHACLFlow, FlowState, FlowTransition, LinkPattern, FlowableCondition } from './shacl/SHACLFlow'
37
+ export * from './shacl/NodeExpression'
38
+ export * from './shacl/builders'
39
+ export { fileToDataUri } from './shacl/index'
36
40
  export * from "./generated/api";
37
41
  export { RpcError } from "./apiClient";
38
42
  /** @deprecated Use `RpcError` instead. */