@breadstone/archipel-mcp 0.0.52 → 0.0.54

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 (59) hide show
  1. package/data/guides/ai-realtime-voice.md +506 -0
  2. package/data/guides/ai-text-generation.md +9 -0
  3. package/data/guides/health-indicators.md +12 -3
  4. package/data/guides/index.md +6 -4
  5. package/data/guides/intelligence-prompts-warm-up.md +358 -0
  6. package/data/packages/platform-health/index.md +1 -1
  7. package/data/packages/platform-intelligence/api/Class.DefaultIntelligencePromptPolicyAdapter.md +54 -0
  8. package/data/packages/platform-intelligence/api/Class.IntelligenceHealthIndicator.md +6 -5
  9. package/data/packages/platform-intelligence/api/Class.IntelligenceModule.md +2 -2
  10. package/data/packages/platform-intelligence/api/Class.IntelligencePromptExecutor.md +88 -0
  11. package/data/packages/platform-intelligence/api/Class.IntelligencePromptPolicyPort.md +48 -0
  12. package/data/packages/platform-intelligence/api/Class.IntelligenceRealtimeError.md +110 -0
  13. package/data/packages/platform-intelligence/api/Class.IntelligenceRealtimeMeteringPort.md +47 -0
  14. package/data/packages/platform-intelligence/api/Class.IntelligenceRealtimeSessionFactory.md +129 -0
  15. package/data/packages/platform-intelligence/api/Class.IntelligenceRealtimeSessionPort.md +308 -0
  16. package/data/packages/platform-intelligence/api/Class.IntelligenceTextGenerator.md +6 -6
  17. package/data/packages/platform-intelligence/api/Function.intelligencePrompt.md +31 -0
  18. package/data/packages/platform-intelligence/api/Function.loadGoogleRealtimeModel.md +25 -0
  19. package/data/packages/platform-intelligence/api/Function.loadGrokRealtimeModel.md +25 -0
  20. package/data/packages/platform-intelligence/api/Function.loadOpenAIRealtimeModel.md +25 -0
  21. package/data/packages/platform-intelligence/api/Interface.IIntelligenceModuleOptions.md +30 -6
  22. package/data/packages/platform-intelligence/api/Interface.IIntelligencePromptDefinition.md +145 -0
  23. package/data/packages/platform-intelligence/api/Interface.IIntelligencePromptEffectivePolicy.md +30 -0
  24. package/data/packages/platform-intelligence/api/Interface.IIntelligencePromptExecutionContext.md +20 -0
  25. package/data/packages/platform-intelligence/api/Interface.IIntelligencePromptExecutionPolicy.md +70 -0
  26. package/data/packages/platform-intelligence/api/Interface.IIntelligencePromptExecutionResult.md +139 -0
  27. package/data/packages/platform-intelligence/api/Interface.IIntelligencePromptInvocation.md +49 -0
  28. package/data/packages/platform-intelligence/api/Interface.IIntelligencePromptPolicyRequest.md +60 -0
  29. package/data/packages/platform-intelligence/api/Interface.IIntelligencePromptRenderContext.md +26 -0
  30. package/data/packages/platform-intelligence/api/Interface.IIntelligenceRealtimeAudioFormat.md +34 -0
  31. package/data/packages/platform-intelligence/api/Interface.IIntelligenceRealtimeCloseEvent.md +40 -0
  32. package/data/packages/platform-intelligence/api/Interface.IIntelligenceRealtimeModuleOptions.md +120 -0
  33. package/data/packages/platform-intelligence/api/Interface.IIntelligenceRealtimeSessionConfiguration.md +135 -0
  34. package/data/packages/platform-intelligence/api/Interface.IIntelligenceRealtimeSessionOptions.md +148 -0
  35. package/data/packages/platform-intelligence/api/Interface.IIntelligenceRealtimeToolDefinition.md +58 -0
  36. package/data/packages/platform-intelligence/api/Interface.IIntelligenceRealtimeTranscriptionConfiguration.md +46 -0
  37. package/data/packages/platform-intelligence/api/Interface.IIntelligenceRealtimeTurnDetectionConfiguration.md +58 -0
  38. package/data/packages/platform-intelligence/api/Interface.IIntelligenceRealtimeUsage.md +103 -0
  39. package/data/packages/platform-intelligence/api/Interface.IIntelligenceTextCompletion.md +24 -12
  40. package/data/packages/platform-intelligence/api/TypeAlias.IIntelligencePromptModelRequirement.md +25 -0
  41. package/data/packages/platform-intelligence/api/TypeAlias.IntelligencePromptCapability.md +20 -0
  42. package/data/packages/platform-intelligence/api/TypeAlias.IntelligencePromptDefinitionConfiguration.md +29 -0
  43. package/data/packages/platform-intelligence/api/TypeAlias.IntelligencePromptGenerationOptions.md +16 -0
  44. package/data/packages/platform-intelligence/api/TypeAlias.IntelligencePromptLatency.md +14 -0
  45. package/data/packages/platform-intelligence/api/TypeAlias.IntelligencePromptMode.md +14 -0
  46. package/data/packages/platform-intelligence/api/TypeAlias.IntelligencePromptQuality.md +14 -0
  47. package/data/packages/platform-intelligence/api/TypeAlias.IntelligencePromptReasoning.md +14 -0
  48. package/data/packages/platform-intelligence/api/TypeAlias.IntelligenceRealtimeClientEvent.md +64 -0
  49. package/data/packages/platform-intelligence/api/TypeAlias.IntelligenceRealtimeMetadataValue.md +14 -0
  50. package/data/packages/platform-intelligence/api/TypeAlias.IntelligenceRealtimeMeteringEvent.md +44 -0
  51. package/data/packages/platform-intelligence/api/TypeAlias.IntelligenceRealtimeModelLoader.md +25 -0
  52. package/data/packages/platform-intelligence/api/TypeAlias.IntelligenceRealtimeModelLoaders.md +14 -0
  53. package/data/packages/platform-intelligence/api/TypeAlias.IntelligenceRealtimeServerEvent.md +159 -0
  54. package/data/packages/platform-intelligence/api/TypeAlias.IntelligenceRealtimeStatus.md +14 -0
  55. package/data/packages/platform-intelligence/api/index.md +43 -0
  56. package/data/packages/platform-intelligence/index.md +211 -7
  57. package/data/patterns/index.md +1 -0
  58. package/data/patterns/realtime-session-pattern.md +309 -0
  59. package/package.json +1 -1
@@ -0,0 +1,309 @@
1
+ ---
2
+ title: Realtime Session Pattern
3
+ description: Ownership, durability, metering, backpressure, and recovery rules for long-lived bidirectional provider sessions.
4
+ order: 11
5
+ ---
6
+
7
+ # Archipel Realtime Session Pattern
8
+
9
+ ## Overview
10
+
11
+ A realtime session is a long-lived, bidirectional connection to an external
12
+ provider. Unlike a normal request, it can outlive access-token refreshes,
13
+ client network changes, deployments, and individual HTTP transactions. Its
14
+ correctness therefore depends on explicit ownership, durable checkpoints,
15
+ bounded queues, idempotent accounting, and visible reconnects.
16
+
17
+ `platform-intelligence` applies this pattern to live AI audio and text through
18
+ `IntelligenceRealtimeSessionFactory` and
19
+ `IntelligenceRealtimeSessionPort`. The same rules apply to other long-lived
20
+ provider streams.
21
+
22
+ ---
23
+
24
+ ## Canonical Topology
25
+
26
+ ```mermaid
27
+ flowchart LR
28
+ Client[Client capture and playback]
29
+ ClientQueue[Local durable chunk queue]
30
+ Gateway[Authenticated application gateway]
31
+ DomainStore[Durable domain state]
32
+ Metering[Idempotent ledger and audit adapter]
33
+ SessionPort[Provider-neutral session port]
34
+ Upstream[External realtime provider]
35
+
36
+ Client --> ClientQueue
37
+ ClientQueue <--> Gateway
38
+ Gateway <--> DomainStore
39
+ Gateway <--> SessionPort
40
+ SessionPort --> Metering
41
+ SessionPort <--> Upstream
42
+ ```
43
+
44
+ The platform library owns the upstream provider connection. The application
45
+ owns the client-facing transport and all business state. The client owns media
46
+ capture and the last local copy of data not yet acknowledged by the
47
+ application.
48
+
49
+ ---
50
+
51
+ ## Invariant 1: Separate Logical and Physical Identity
52
+
53
+ Never use one identifier for both the durable conversation and a provider
54
+ socket.
55
+
56
+ | Identity | Changes on reconnect | Used for |
57
+ | --- | --- | --- |
58
+ | Logical session ID | No | Authorization, domain persistence, customer-visible history |
59
+ | Connection ID | Yes | Provider attempt, metering idempotency, diagnostics, close history |
60
+
61
+ One logical session may have many sequential provider connections. Every
62
+ connection start and end must remain visible in the audit trail.
63
+
64
+ In `platform-intelligence`, these identities are `sessionId` and
65
+ `connectionId`.
66
+
67
+ ---
68
+
69
+ ## Invariant 2: One Owner Per Active Connection
70
+
71
+ An upstream WebSocket and its event queues live in one process. All commands
72
+ for that connection must reach the owning process and execute in order.
73
+
74
+ Acceptable cluster strategies include:
75
+
76
+ - sticky client routing for the active gateway connection;
77
+ - a distributed ownership record mapping connection ID to pod;
78
+ - a dedicated realtime worker selected before the client begins streaming.
79
+
80
+ The in-memory factory registry is a process-local lifecycle mechanism, not a
81
+ distributed session store. Durable business state must never depend on it.
82
+
83
+ ---
84
+
85
+ ## Invariant 3: Persist Small Units While Streaming
86
+
87
+ Durability must not turn realtime media into a deferred upload. Split the
88
+ stream into small ordered fragments and persist each fragment before it is
89
+ acknowledged.
90
+
91
+ ```mermaid
92
+ sequenceDiagram
93
+ participant Capture as Client capture
94
+ participant Local as Local durable queue
95
+ participant Gateway as Application gateway
96
+ participant Store as Durable application store
97
+ participant Port as Realtime session port
98
+ participant Provider as Provider
99
+
100
+ Capture->>Local: Write chunk (chunkId, sequence, audio)
101
+ Local->>Gateway: Send chunk
102
+ Gateway->>Store: Insert idempotently
103
+ Store-->>Gateway: Durable
104
+ Gateway->>Port: appendAudio(audio)
105
+ Port->>Provider: Forward immediately
106
+ Port-->>Gateway: Accepted
107
+ Gateway-->>Local: Acknowledge chunkId
108
+ Local->>Local: Remove acknowledged chunk
109
+ ```
110
+
111
+ The local write protects against a browser or network failure. The application
112
+ write protects against gateway restarts and provides the source for recovery.
113
+ The provider call remains live because this happens per fragment rather than
114
+ after the complete recording.
115
+
116
+ ### Chunk-envelope rules
117
+
118
+ - The application envelope contains a stable `chunkId` and monotonic
119
+ `sequence`.
120
+ - Persistence uses `(logicalSessionId, chunkId)` as an idempotency key.
121
+ - The durable record distinguishes `pending` from `forwarded`. A duplicate of
122
+ a forwarded chunk is acknowledged without another upstream write; a pending
123
+ chunk still needs forwarding.
124
+ - Gaps stop acknowledgement until the application's ordering policy resolves
125
+ them.
126
+ - The provider-neutral port receives only the media payload required by the
127
+ upstream contract.
128
+ - Chunk size, capture cadence, and audio format are negotiated by the
129
+ application and selected provider.
130
+
131
+ There is an unavoidable ambiguity if a process dies after the provider accepts
132
+ a fragment but before the application marks it forwarded. Most realtime
133
+ provider audio-append operations have no application idempotency key. Do not
134
+ claim exactly-once provider delivery; retain the durable recording and make
135
+ connection recovery explicit instead.
136
+
137
+ ---
138
+
139
+ ## Invariant 4: Preserve Wire Order, Bound Every Queue
140
+
141
+ Inbound provider events and outbound client commands must be serialized per
142
+ connection. Concurrent callbacks may otherwise reorder transcript deltas,
143
+ tool calls, response cancellation, or audio commits.
144
+
145
+ Every queue also needs a finite capacity:
146
+
147
+ | Queue or buffer | Overflow policy |
148
+ | --- | --- |
149
+ | Client local queue | Stop capture or warn before storage exhaustion |
150
+ | Client-to-application frames | Apply socket backpressure and stop acknowledgement |
151
+ | Application persistence queue | Reject or pause the client; never grow without limit |
152
+ | Provider outbound queue | Fail the command with a retryability signal |
153
+ | Provider inbound queue | Close the connection when ordering can no longer be guaranteed |
154
+ | Metering queue | Close or degrade according to the application's billing safety policy |
155
+ | WebSocket buffered bytes | Reject additional writes until the socket drains |
156
+
157
+ `platform-intelligence` serializes provider events and enforces per-session
158
+ event and byte limits. These limits do not replace tenant quotas or client
159
+ gateway limits.
160
+
161
+ ---
162
+
163
+ ## Invariant 5: Meter Through an Idempotent Application Port
164
+
165
+ Long-lived streams cannot rely on request interceptors for complete usage
166
+ accounting. Emit durable lifecycle and interval events through an application
167
+ port instead.
168
+
169
+ Each event must contain:
170
+
171
+ - a stable event ID;
172
+ - logical session and physical connection IDs;
173
+ - provider and model;
174
+ - occurrence time;
175
+ - bounded correlation metadata;
176
+ - event-specific interval, usage, or close data.
177
+
178
+ The adapter uses the event ID as a unique key and commits the usage event,
179
+ commercial ledger change, and AI audit record in one transaction.
180
+
181
+ ```mermaid
182
+ flowchart LR
183
+ Event[Metering event with eventId]
184
+ Unique[Insert with unique eventId]
185
+ Debit[Debit credit ledger]
186
+ Audit[Append AI audit record]
187
+ Commit[Commit transaction]
188
+
189
+ Event --> Unique
190
+ Unique --> Debit
191
+ Debit --> Audit
192
+ Audit --> Commit
193
+ ```
194
+
195
+ Use provider-independent intervals for predictable commercial charging and
196
+ provider-reported token dimensions for reconciliation. Do not charge both
197
+ sources for the same work unless that is an explicit pricing rule.
198
+
199
+ The adapter should normally write to a local database transaction or outbox.
200
+ Calling a slow remote billing API directly increases queue pressure and makes
201
+ shutdown flushes unreliable.
202
+
203
+ ---
204
+
205
+ ## Invariant 6: Reconnect Is a New Connection
206
+
207
+ Do not silently reconnect a stateful provider session. A replacement socket
208
+ does not necessarily contain the same model conversation, input buffer, tool
209
+ state, or response state.
210
+
211
+ ```mermaid
212
+ stateDiagram-v2
213
+ [*] --> Idle
214
+ Idle --> Connecting: connect
215
+ Connecting --> Connected: socket open
216
+ Connecting --> Error: setup failure
217
+ Connected --> Closing: close
218
+ Connected --> Error: terminal transport failure
219
+ Closing --> Closed: close event or timeout
220
+ Error --> Closed: finalize
221
+ Closed --> [*]
222
+ ```
223
+
224
+ Recovery creates a new connection:
225
+
226
+ 1. finalize and audit the failed connection;
227
+ 2. keep the same logical session ID;
228
+ 3. derive safe context from durable transcript and domain state;
229
+ 4. create a replacement port with a new connection ID;
230
+ 5. record the recovery relationship explicitly;
231
+ 6. resume only unacknowledged application fragments according to provider
232
+ semantics.
233
+
234
+ Never claim lossless replay when the provider-neutral contract cannot restore
235
+ provider-native conversation state.
236
+
237
+ ---
238
+
239
+ ## Invariant 7: Keep Provider Secrets and Contracts Upstream
240
+
241
+ The client must not receive permanent provider API keys. The application
242
+ gateway speaks an application-owned protocol; the platform adapter speaks the
243
+ provider protocol.
244
+
245
+ For realtime intelligence:
246
+
247
+ - provider-specific model creation and wire mapping go through Vercel AI SDK
248
+ provider packages;
249
+ - Archipel exposes normalized client and server event unions;
250
+ - a generic server WebSocket carries the provider configuration returned by
251
+ the AI SDK adapter;
252
+ - provider-native events remain available only as an audited `raw` escape
253
+ hatch.
254
+
255
+ This boundary allows the application to switch providers without rewriting
256
+ authentication, persistence, metering, or the client transport.
257
+
258
+ ---
259
+
260
+ ## Invariant 8: Shutdown Is Part of Correctness
261
+
262
+ The owner of a long-lived connection must implement deterministic teardown:
263
+
264
+ 1. reject or stop accepting new client input;
265
+ 2. clear periodic timers;
266
+ 3. close the upstream socket with a bounded timeout;
267
+ 4. emit the final partial billing interval;
268
+ 5. emit the connection-end event;
269
+ 6. flush queued metering within the shutdown budget;
270
+ 7. release the process-local ownership record.
271
+
272
+ The Nest application must enable shutdown hooks for `OnModuleDestroy` to run
273
+ on process signals.
274
+
275
+ ---
276
+
277
+ ## Failure Policy
278
+
279
+ | Failure | Retry in place? | Required action |
280
+ | --- | --- | --- |
281
+ | Transient provider token creation | Yes, bounded | Retry with backoff before socket creation |
282
+ | Socket open timeout | No hidden reconnect | Fail connection attempt and report retryability |
283
+ | Outbound command failure | Caller decides | Keep client data unacknowledged when safe to retry |
284
+ | Malformed provider event | No | Report receive error; close if ordering is unsafe |
285
+ | Queue or byte limit exceeded | No | Apply backpressure or close with explicit reason |
286
+ | Metering adapter failure | Bounded retry | Persist through a durable local adapter; surface failure |
287
+ | Provider disconnect | No hidden reconnect | Finalize connection and start explicit recovery |
288
+ | Process shutdown | No | Drain and close within the configured timeout |
289
+
290
+ ---
291
+
292
+ ## Checklist
293
+
294
+ - Logical session and physical connection IDs are distinct.
295
+ - Exactly one process owns an active upstream connection.
296
+ - The client keeps unacknowledged fragments durably.
297
+ - The application persists fragments idempotently before acknowledgement.
298
+ - Streaming continues per fragment; there is no whole-session upload gate.
299
+ - Provider commands and events preserve per-connection order.
300
+ - Every queue, collection, timer, and byte buffer is bounded.
301
+ - Metering events have stable idempotency keys.
302
+ - Ledger debit and AI audit persistence share one transaction.
303
+ - Provider token data never reaches the client-facing protocol.
304
+ - Reconnect creates a visible new connection.
305
+ - Recovery claims only the context the provider contract can reconstruct.
306
+ - Shutdown closes sockets and flushes final metering.
307
+
308
+ See [AI Realtime Voice](../guides/ai-realtime-voice) for the concrete
309
+ `platform-intelligence` integration.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@breadstone/archipel-mcp",
3
- "version": "0.0.52",
3
+ "version": "0.0.54",
4
4
  "description": "MCP server providing Archipel platform knowledge - documentation, query patterns, and coding conventions - to AI development tools.",
5
5
  "type": "commonjs",
6
6
  "main": "./src/main.js",