dsh-realtime 0.2.1 → 0.2.3
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/LICENSE +21 -0
- package/README.md +31 -1
- package/lib/error.d.ts +4 -0
- package/lib/error.d.ts.map +1 -1
- package/lib/error.js +4 -0
- package/lib/error.js.map +1 -1
- package/lib/index.d.ts +23 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +23 -0
- package/lib/index.js.map +1 -1
- package/lib/journal.d.ts +147 -0
- package/lib/journal.d.ts.map +1 -0
- package/lib/journal.js +117 -0
- package/lib/journal.js.map +1 -0
- package/lib/settings.d.ts +240 -0
- package/lib/settings.d.ts.map +1 -0
- package/lib/settings.js +338 -0
- package/lib/settings.js.map +1 -0
- package/package.json +6 -6
- package/src/error.ts +4 -0
- package/src/index.ts +25 -0
- package/src/journal.ts +193 -0
- package/src/settings.ts +520 -0
package/src/index.ts
CHANGED
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
|
|
12
12
|
import { Service, type Context } from '@deepseek-ai/cordis'
|
|
13
13
|
import { REALTIME_ERROR_CODES, RealtimeError } from './error.ts'
|
|
14
|
+
import { Journal } from './journal.ts'
|
|
15
|
+
import { RealtimeSettings } from './settings.ts'
|
|
14
16
|
import type {
|
|
15
17
|
RealtimeDelegation,
|
|
16
18
|
RealtimeModelInfo,
|
|
@@ -23,6 +25,8 @@ import type {
|
|
|
23
25
|
export * from './types.ts'
|
|
24
26
|
export * from './error.ts'
|
|
25
27
|
export * from './redact.ts'
|
|
28
|
+
export * from './journal.ts'
|
|
29
|
+
export * from './settings.ts'
|
|
26
30
|
export { RealtimeError, REALTIME_ERROR_CODES } from './error.ts'
|
|
27
31
|
|
|
28
32
|
declare module '@deepseek-ai/cordis' {
|
|
@@ -109,6 +113,27 @@ interface AdapterRegistration {
|
|
|
109
113
|
export class RealtimeRuntime extends Service {
|
|
110
114
|
private readonly adapters = new Map<string, AdapterRegistration>()
|
|
111
115
|
|
|
116
|
+
/**
|
|
117
|
+
* The journal every plugin in this bundle writes to, and the diagnostics route serves.
|
|
118
|
+
*
|
|
119
|
+
* Owned here because this is the only object all of them already hold: the responder knows the
|
|
120
|
+
* delegation, the audio route knows the socket, and the seam is where those two meet. One journal
|
|
121
|
+
* per process — a reader should not be left correlating three partial ones.
|
|
122
|
+
*/
|
|
123
|
+
readonly journal = new Journal()
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The settings every plugin in this bundle declares, and the surface a control plane changes them
|
|
127
|
+
* through.
|
|
128
|
+
*
|
|
129
|
+
* Owned here beside the journal, for the same reason: it is the one object all of them already hold,
|
|
130
|
+
* and a surface split across three of them would leave a reader correlating three partial answers to
|
|
131
|
+
* one question. A setting's class comes from `docs/control-plane-fields.md`, and the registry is what
|
|
132
|
+
* makes that classification bite — a change to a field the protocol cannot honour is refused with the
|
|
133
|
+
* reason, on the same channel it arrived on.
|
|
134
|
+
*/
|
|
135
|
+
readonly settings = new RealtimeSettings(this.journal)
|
|
136
|
+
|
|
112
137
|
/**
|
|
113
138
|
* @param ctx - the Cordis context this service is mounted on.
|
|
114
139
|
*/
|
package/src/journal.ts
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The journal: a bounded, redaction-safe record of what the plugin did.
|
|
3
|
+
*
|
|
4
|
+
* S1 story 2, and the substrate for the rest of the sprint — the diagnostics route serves it, the
|
|
5
|
+
* spoken failures narrate from it, and the fault-injection matrix asserts against it. It exists
|
|
6
|
+
* because every failure this project has spent hours on was *unobservable* rather than wrong: the
|
|
7
|
+
* delegation that produced nothing, the append nobody heard, the socket the host refused. A journal
|
|
8
|
+
* turns each of those into a line.
|
|
9
|
+
*
|
|
10
|
+
* Three properties are load-bearing, and each is a test rather than a promise:
|
|
11
|
+
*
|
|
12
|
+
* - **Redaction happens on write, not on read.** A journal that stores the raw text and redacts when
|
|
13
|
+
* serving it has already leaked: the secret is in memory, in a crash dump, and in whatever later
|
|
14
|
+
* edit forgets the second step. Every string is passed through {@link redact} *before* it is
|
|
15
|
+
* retained, so there is no unredacted copy to forget about. (That is design.md invariant 3 —
|
|
16
|
+
* credentials proven safe by a test that fails on a planted string.)
|
|
17
|
+
* - **Bounded, with eviction visible.** The buffer keeps the last `capacity` entries. Every entry
|
|
18
|
+
* carries a monotonic {@link JournalEntry.seq}, so a reader can tell that earlier entries were
|
|
19
|
+
* dropped rather than never recorded — a ring buffer that silently truncates is how "the log is
|
|
20
|
+
* empty" becomes indistinguishable from "nothing happened".
|
|
21
|
+
* - **Ack is not delivery.** Invariant 6: an acknowledgement proves injection, not that anyone heard
|
|
22
|
+
* it. `append.acknowledged` and `speech.played` are separate kinds with separate entries, and
|
|
23
|
+
* nothing here merges them into one "sent" line.
|
|
24
|
+
*
|
|
25
|
+
* @module dsh-realtime/journal
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import { redact } from './redact.ts'
|
|
29
|
+
|
|
30
|
+
/** Entries retained when a caller does not say otherwise. */
|
|
31
|
+
export const DEFAULT_JOURNAL_CAPACITY = 200
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* What a journal entry can describe.
|
|
35
|
+
*
|
|
36
|
+
* The list is deliberately small and closed: an entry kind is a contract with whatever reads the
|
|
37
|
+
* journal later, so adding one is a considered act rather than a free-form log line.
|
|
38
|
+
*/
|
|
39
|
+
export type JournalKind =
|
|
40
|
+
/** A voice session opened, or was asked to and could not. */
|
|
41
|
+
| 'session.opened' | 'session.closed' | 'session.failed'
|
|
42
|
+
/**
|
|
43
|
+
* The audio route accepted a socket, refused one and said why, or watched one leave.
|
|
44
|
+
*
|
|
45
|
+
* A close is recorded rather than left to be inferred from the absence of later entries: a reader
|
|
46
|
+
* looking at a journal that simply stops cannot tell a client that hung up from a process that died,
|
|
47
|
+
* and that difference is the whole reason anybody opens the journal.
|
|
48
|
+
*/
|
|
49
|
+
| 'socket.accepted' | 'socket.rejected' | 'socket.closed'
|
|
50
|
+
/** The model raised a delegation, and the id it will be correlated by. */
|
|
51
|
+
| 'delegation.seen'
|
|
52
|
+
/** A delegated turn was admitted to the session, refused, declined, or ran out of window. */
|
|
53
|
+
| 'prompt.admitted' | 'prompt.refused' | 'prompt.declined' | 'window.elapsed'
|
|
54
|
+
/** A turn produced an answer. */
|
|
55
|
+
| 'answer.received'
|
|
56
|
+
/** A context append was acknowledged on the wire. Not delivery — see invariant 6. */
|
|
57
|
+
| 'append.acknowledged'
|
|
58
|
+
/** Audio was handed to the transport. **Not** a claim that anyone heard it. */
|
|
59
|
+
| 'speech.sent'
|
|
60
|
+
/** The configuration as resolved, so a journal can be read without the profile beside it. */
|
|
61
|
+
| 'config.resolved'
|
|
62
|
+
/**
|
|
63
|
+
* A live setting changed while the plugin was running.
|
|
64
|
+
*
|
|
65
|
+
* Recorded with the setting's **key and not its value**: the value of a secret-bearing setting is
|
|
66
|
+
* exactly the text that must not be retained, and this is the one record built to be read, quoted
|
|
67
|
+
* and pasted. What a reader needs is that a change happened and which setting it was — the value is
|
|
68
|
+
* whatever the plugin now reports through the settings surface.
|
|
69
|
+
*/
|
|
70
|
+
| 'config.changed'
|
|
71
|
+
|
|
72
|
+
/** One retained entry. */
|
|
73
|
+
export interface JournalEntry {
|
|
74
|
+
/**
|
|
75
|
+
* Monotonic sequence number, never reused and never reset within one journal instance.
|
|
76
|
+
*
|
|
77
|
+
* A gap between the oldest retained `seq` and the newest means entries were evicted, which is the
|
|
78
|
+
* difference between "nothing happened" and "the buffer rolled over".
|
|
79
|
+
*/
|
|
80
|
+
readonly seq: number
|
|
81
|
+
/** Milliseconds since the epoch, as the injecting caller saw it. */
|
|
82
|
+
readonly at: number
|
|
83
|
+
/** What happened. */
|
|
84
|
+
readonly kind: JournalKind
|
|
85
|
+
/** Redacted detail fields; keys are the caller's, values are already safe to serve. */
|
|
86
|
+
readonly detail: Readonly<Record<string, string>>
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** How a journal is constructed. */
|
|
90
|
+
export interface JournalOptions {
|
|
91
|
+
/** Entries retained before the oldest is evicted. */
|
|
92
|
+
capacity?: number
|
|
93
|
+
/**
|
|
94
|
+
* Values that must never appear in an entry, however they are spelled.
|
|
95
|
+
*
|
|
96
|
+
* The route's capability token is 32 random bytes of base64url: it has no prefix and no structure,
|
|
97
|
+
* so no pattern can catch it and only a caller holding it can name it. The provider key is
|
|
98
|
+
* normally caught by shape, but pass it here too — a seam that relies on shape alone fails the
|
|
99
|
+
* moment a vendor changes its prefix.
|
|
100
|
+
*/
|
|
101
|
+
secrets?: readonly string[]
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* A bounded, redaction-safe journal of plugin activity.
|
|
106
|
+
*
|
|
107
|
+
* Not a service: it is a plain object so it can be constructed in a test, handed to a route, and
|
|
108
|
+
* read without a Cordis context. The seam owns one instance and lends it to the plugins that write
|
|
109
|
+
* to it.
|
|
110
|
+
*/
|
|
111
|
+
export class Journal {
|
|
112
|
+
private readonly capacity: number
|
|
113
|
+
private readonly secrets: string[]
|
|
114
|
+
private readonly entries: JournalEntry[] = []
|
|
115
|
+
private nextSeq = 1
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* @param options - capacity and the values that must never be retained.
|
|
119
|
+
* @throws RangeError when `capacity` is not a positive integer — a journal that retains nothing
|
|
120
|
+
* is a failure that looks like silence, which is the defect this module exists to remove.
|
|
121
|
+
*/
|
|
122
|
+
constructor(options: JournalOptions = {}) {
|
|
123
|
+
const capacity = options.capacity ?? DEFAULT_JOURNAL_CAPACITY
|
|
124
|
+
if (!Number.isInteger(capacity) || capacity < 1) {
|
|
125
|
+
throw new RangeError(`journal capacity must be a positive integer, received ${String(capacity)}`)
|
|
126
|
+
}
|
|
127
|
+
this.capacity = capacity
|
|
128
|
+
this.secrets = [...options.secrets ?? []]
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Add values that must never be retained, from a plugin that has only just learned them.
|
|
133
|
+
*
|
|
134
|
+
* Additive rather than a constructor argument because secrets arrive with the plugins that hold
|
|
135
|
+
* them, and plugins load in no guaranteed order: the route's capability token does not exist until
|
|
136
|
+
* the plugin that mints it applies, and a provider key arrives with whichever plugin reads the
|
|
137
|
+
* environment. A journal that could only be seeded at construction would have to be replaced to
|
|
138
|
+
* learn either — which is how a sink ends up with two instances and a reader with half a story.
|
|
139
|
+
* @param values - secrets to add; empty and duplicate values are ignored.
|
|
140
|
+
*/
|
|
141
|
+
addSecrets(values: readonly string[]): void {
|
|
142
|
+
for (const value of values) {
|
|
143
|
+
if (typeof value !== 'string' || value.length === 0) continue
|
|
144
|
+
if (!this.secrets.includes(value)) this.secrets.push(value)
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Redact and retain one entry, evicting the oldest when the buffer is full.
|
|
150
|
+
* @param kind - what happened.
|
|
151
|
+
* @param detail - string fields to retain; every value is redacted before it is stored.
|
|
152
|
+
* @param at - epoch milliseconds; defaults to now, and is injectable so a test can pin a time.
|
|
153
|
+
* @returns the entry exactly as it was retained, so a caller can correlate on `seq`.
|
|
154
|
+
*/
|
|
155
|
+
record(kind: JournalKind, detail: Readonly<Record<string, string>> = {}, at: number = Date.now()): JournalEntry {
|
|
156
|
+
const safe: Record<string, string> = {}
|
|
157
|
+
for (const [key, value] of Object.entries(detail)) {
|
|
158
|
+
safe[key] = redact(value, this.secrets)
|
|
159
|
+
}
|
|
160
|
+
const entry: JournalEntry = Object.freeze({
|
|
161
|
+
seq: this.nextSeq++,
|
|
162
|
+
at,
|
|
163
|
+
kind,
|
|
164
|
+
detail: Object.freeze(safe),
|
|
165
|
+
})
|
|
166
|
+
this.entries.push(entry)
|
|
167
|
+
// `shift()` on a bounded array: the buffer is capped, so this stays O(capacity) at worst and
|
|
168
|
+
// the memory ceiling is a property of the type rather than of the caller's discipline.
|
|
169
|
+
while (this.entries.length > this.capacity) this.entries.shift()
|
|
170
|
+
return entry
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Every retained entry, oldest first.
|
|
175
|
+
*
|
|
176
|
+
* Detached: mutating the result cannot reach the journal, so a route serving this cannot be used
|
|
177
|
+
* to tamper with the record it is reporting.
|
|
178
|
+
* @returns a detached, chronologically ordered copy.
|
|
179
|
+
*/
|
|
180
|
+
snapshot(): JournalEntry[] {
|
|
181
|
+
return this.entries.map(entry => ({ ...entry, detail: { ...entry.detail } }))
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** Entries currently retained. */
|
|
185
|
+
get size(): number {
|
|
186
|
+
return this.entries.length
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Sequence number of the oldest retained entry, or `undefined` when empty. */
|
|
190
|
+
get oldestSeq(): number | undefined {
|
|
191
|
+
return this.entries[0]?.seq
|
|
192
|
+
}
|
|
193
|
+
}
|