@soffits/dsh-matrix-bot 0.2.1 → 0.2.2

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/CHANGELOG.md CHANGED
@@ -1,6 +1,9 @@
1
1
  # Changelog
2
2
 
3
- ## Unreleased
3
+ ## 0.2.2
4
+
5
+ - The answer, silent marker, model, voice and sender logic now comes from `@soffits/dsh-channel-core`, the package shared with the Telegram bot, instead of a copy. While an answer streams, a trailing `[SILENT]` that is still being written after real text no longer flashes in the room.
6
+ - `@deepseek-ai/schemastery` is a peer dependency, so the bot uses the harness's own copy and an update never brings a private one.
4
7
 
5
8
  ## 0.2.1
6
9
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soffits/dsh-matrix-bot",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Matrix bot for DeepSeek Harness: an end-to-end encrypted Matrix account, a private Space per person with one room per DSH session, streamed answers, commands, files, voice messages, tool approvals and agent questions.",
5
5
  "type": "module",
6
6
  "main": "./index.js",
@@ -41,8 +41,8 @@
41
41
  "e2e": "node scripts/e2e.js"
42
42
  },
43
43
  "dependencies": {
44
- "@deepseek-ai/schemastery": "^3.19.0",
45
44
  "@matrix-org/matrix-sdk-crypto-nodejs": "0.7.0",
45
+ "@soffits/dsh-channel-core": "^0.1.0",
46
46
  "@vector-im/matrix-bot-sdk": "0.11.0-element.0",
47
47
  "markdown-it": "15.0.3"
48
48
  },
@@ -66,5 +66,11 @@
66
66
  },
67
67
  "publishConfig": {
68
68
  "access": "public"
69
+ },
70
+ "peerDependencies": {
71
+ "@deepseek-ai/schemastery": "^3.18.5"
72
+ },
73
+ "devDependencies": {
74
+ "@deepseek-ai/schemastery": "^3.19.0"
69
75
  }
70
76
  }
package/src/app.js CHANGED
@@ -1,17 +1,16 @@
1
1
  /** Host runtime: the Matrix connection, incoming events and the wiring of the parts. */
2
2
  import { mkdir, writeFile } from 'node:fs/promises'
3
3
  import { join } from 'node:path'
4
+ import { createModelCatalog, createPrincipalTagger } from '@soffits/dsh-channel-core'
4
5
  import { COMMANDS, SIGIL, helpText } from './commands.js'
5
6
  import { dataDir, name, SERVICE_NAME } from './constants.js'
6
7
  import { createEvents } from './events.js'
7
8
  import { createHelpers } from './helpers.js'
8
9
  import { connect as realConnect } from './matrix.js'
9
- import { createModelCatalog } from './models.js'
10
10
  import { createOperations } from './operations.js'
11
11
  import { createOutbound } from './outbound.js'
12
12
  import { createPrompts } from './prompts.js'
13
13
  import { createReactions } from './reactions.js'
14
- import { createPrincipalTagger } from './sender.js'
15
14
  import { createSession, resolveAgent } from './sessions.js'
16
15
  import { createSpaces } from './spaces.js'
17
16
  import { createSpool } from './spool.js'
package/src/constants.js CHANGED
@@ -23,9 +23,6 @@ export function spoolDir() {
23
23
  return join(dataDir(), 'spool')
24
24
  }
25
25
 
26
- /** A final answer ending in exactly this text posts nothing more in the room. */
27
- export const SILENT_REPLY = '[SILENT]'
28
-
29
26
  /**
30
27
  * Longest text sent as one Matrix message. Events are limited to 64 KiB
31
28
  * after encryption, and the HTML copy of the text travels in the same event.
package/src/controls.js CHANGED
@@ -2,7 +2,7 @@
2
2
  * Session controls from a room: model, reasoning effort, access mode and
3
3
  * context use, picked from numbered lists.
4
4
  */
5
- import { defaultModelOf, effortChoices, effortName, findModel, modelName, pickedSelection, selectSessionModel, sessionModelOf } from './models.js'
5
+ import { defaultModelOf, effortChoices, effortName, findModel, modelName, pickedSelection, selectSessionModel, sessionModelOf } from '@soffits/dsh-channel-core'
6
6
  import { currentPermission, permissionOptions, setPermission } from './sessions.js'
7
7
 
8
8
  /** @returns `n` as a 1-based list index into `items`, or undefined. */
package/src/events.js CHANGED
@@ -4,11 +4,12 @@
4
4
  */
5
5
  import { randomUUID } from 'node:crypto'
6
6
  import { join } from 'node:path'
7
+ import { withMetadata } from '@soffits/dsh-channel-core'
7
8
  import { parseCommand, runCommand } from './commands.js'
8
9
  import { dataDir, POST_SOURCE_KIND, REQUEST_PREFIX } from './constants.js'
9
10
  import { composePrompt, hasMedia, ownText, replyInfo, replyTargetOf } from './inbound.js'
10
11
  import { allowedEntry } from './operations.js'
11
- import { matrixUserPrincipal, withPrincipal } from './sender.js'
12
+ import { matrixUserPrincipal } from './sender.js'
12
13
  import { admitPrompt, hasRequest, recordMessage, unarchive } from './sessions.js'
13
14
  import { clip } from './text.js'
14
15
 
@@ -243,7 +244,7 @@ export function createEvents({ ctx, getStore, storeReady, getMatrix, setting, sp
243
244
  const owner = target.sender === getMatrix().userId ? 'your message' : 'the message'
244
245
  const text = `[Matrix: ${sender.name} reacted ${relation.key} to ${owner} ${relation.event_id}:]\n${clip(ownText(target.content), 500)}`
245
246
  const message = { id: randomUUID(), role: 'user', content: [{ type: 'text', text }], source: { kind: POST_SOURCE_KIND, form: 'notice', summary: clip(`${sender.name} reacted ${relation.key}`, 120) } }
246
- recordMessage(agent, withPrincipal(message, matrixUserPrincipal(sender.userId, { name: sender.name, roomId })))
247
+ recordMessage(agent, withMetadata(message, { principal: matrixUserPrincipal(sender.userId, { name: sender.name, roomId }) }))
247
248
  }
248
249
 
249
250
 
package/src/helpers.js CHANGED
@@ -3,7 +3,7 @@
3
3
  * status message, edited in place as they start and finish and left as it is
4
4
  * once all of them are done. `/agents` lists a session's helpers on demand.
5
5
  */
6
- import { sessionModelOf } from './models.js'
6
+ import { sessionModelOf } from '@soffits/dsh-channel-core'
7
7
  import { recentAssistantTexts } from './sessions.js'
8
8
  import { clip } from './text.js'
9
9
 
package/src/inbound.js CHANGED
@@ -5,9 +5,9 @@
5
5
  */
6
6
  import { mkdir, writeFile } from 'node:fs/promises'
7
7
  import { extname, join } from 'node:path'
8
+ import { transcribe, transcriptNote } from '@soffits/dsh-channel-core'
8
9
  import { MAX_FILE_BYTES } from './constants.js'
9
10
  import { clip } from './text.js'
10
- import { transcribe, transcriptNote } from './voice.js'
11
11
 
12
12
  /** Image types the DSH attachment store takes as pictures. */
13
13
  const IMAGE_TYPES = new Set(['image/jpeg', 'image/png', 'image/webp', 'image/gif'])
@@ -114,9 +114,9 @@ export async function composePrompt(event, options) {
114
114
  const media = { kind: voiceMessage ? 'voice message' : 'audio file', name, seconds: content.info?.duration > 0 ? Math.round(content.info.duration / 1000) : undefined }
115
115
  try {
116
116
  const result = await transcribe({ ...voice, path, signal })
117
- lines.push(transcriptNote(media, { ...result, path }))
117
+ lines.push(transcriptNote(media, { ...result, path }, 'Matrix'))
118
118
  } catch (error) {
119
- lines.push(transcriptNote(media, { path, error: error?.message ?? String(error) }))
119
+ lines.push(transcriptNote(media, { path, error: error?.message ?? String(error) }, 'Matrix'))
120
120
  }
121
121
  } else {
122
122
  lines.push(`[${content.msgtype === 'm.image' ? 'Picture' : 'File'} "${name}", saved at ${path}]`)
package/src/operations.js CHANGED
@@ -3,9 +3,9 @@
3
3
  * allowing a person, and its status.
4
4
  */
5
5
  import { randomUUID } from 'node:crypto'
6
+ import { sendPrincipal, withMetadata } from '@soffits/dsh-channel-core'
6
7
  import { POST_SOURCE_KIND } from './constants.js'
7
8
  import { finalError } from './outbound.js'
8
- import { sendPrincipal, withPrincipal } from './sender.js'
9
9
  import { recordMessage, resolveAgent } from './sessions.js'
10
10
  import { clip } from './text.js'
11
11
 
@@ -86,7 +86,7 @@ export function createOperations({ ctx, getStore, storeReady, requireMatrix, get
86
86
  const header = `Sent to Matrix room "${record.roomName ?? roomId}"${by}`
87
87
  const content = [{ type: 'text', text: `[${header}]${text ? `\n${text}` : ''}${files.length > 0 ? `\n[Files: ${files.map((file) => file.name).join(', ')}]` : ''}` }]
88
88
  const message = { id: randomUUID(), role: 'user', content, source: { kind: POST_SOURCE_KIND, form: 'notice', summary: clip(header, 120), roomId, self } }
89
- recordMessage(agent, withPrincipal(message, sendPrincipal(from)))
89
+ recordMessage(agent, withMetadata(message, { principal: sendPrincipal(from) }))
90
90
  recorded = true
91
91
  } catch (error) {
92
92
  recordError = messageOf(error)
package/src/sender.js CHANGED
@@ -1,28 +1,9 @@
1
1
  /**
2
- * Who sent a message, as data on the message itself.
3
- *
4
- * DSH's field for the caller behind a user message is `source.principal`:
5
- * the session controller writes it for callers other than the owner, so an
6
- * absent principal means the owner. The bot admits every Matrix prompt as
7
- * the owner, so it fills the field itself, and also on the copies of sends it
8
- * records in bound sessions. Readers get the sender from it, not from text.
9
- *
10
- * Every principal has `kind`, `channel` and `id`, and `name` when one is
11
- * known; the other fields depend on the kind. None carries a credential, so
12
- * all are safe in a session log.
13
- *
14
- * When the message was sent and what it replies to go on `source.envelope`,
15
- * the metadata dsh-message-context renders for the model. The bot never adds
16
- * them to the text: DSH titles a session from the first words of its first
17
- * human message, and those must be the person's own.
2
+ * The `matrix-user` principal: who wrote a message in a room. The shared
3
+ * fields and the tagger that records it on prompts are in
4
+ * @soffits/dsh-channel-core.
18
5
  */
19
-
20
- /** Remembered prompts waiting for their step; the oldest go first past this. */
21
- export const MAX_PENDING_PRINCIPALS = 1024
22
-
23
- function nonEmpty(value) {
24
- return typeof value === 'string' && value.trim().length > 0 ? value.trim() : undefined
25
- }
6
+ import { nonEmpty } from '@soffits/dsh-channel-core'
26
7
 
27
8
  /**
28
9
  * The Matrix user who wrote a message, and where.
@@ -41,125 +22,3 @@ export function matrixUserPrincipal(userId, { name, roomId, room } = {}) {
41
22
  ...(nonEmpty(roomId) === undefined ? {} : { chat: { id: roomId, ...(nonEmpty(room) === undefined ? {} : { name: nonEmpty(room) }) } }),
42
23
  }
43
24
  }
44
-
45
- /**
46
- * The sender of a send into a room, from the request's `from`: a scheduled
47
- * job, else a DSH session, else the free-form label of an outside caller.
48
- * @param from - `{ job?, jobId?, sessionId?, label? }`.
49
- * @returns the principal, or undefined when the request names no sender.
50
- */
51
- export function sendPrincipal(from) {
52
- const job = nonEmpty(from?.job)
53
- const jobId = nonEmpty(from?.jobId)
54
- const sessionId = nonEmpty(from?.sessionId)
55
- const label = nonEmpty(from?.label)
56
- if (job !== undefined || jobId !== undefined) {
57
- return {
58
- kind: 'cron-job',
59
- channel: 'cron',
60
- id: jobId ?? job,
61
- ...(job === undefined ? {} : { name: job }),
62
- ...(sessionId === undefined ? {} : { sessionId }),
63
- }
64
- }
65
- if (sessionId !== undefined) {
66
- return { kind: 'dsh-session', channel: 'dsh', id: sessionId, ...(label === undefined ? {} : { name: label }) }
67
- }
68
- if (label !== undefined) return { kind: 'external', channel: 'external', id: label, name: label }
69
- return undefined
70
- }
71
-
72
- /**
73
- * A frozen copy of a message whose source records its sender.
74
- * @param message - the user message.
75
- * @param principal - its sender.
76
- * @returns the copy; the message itself when there is no principal.
77
- */
78
- export function withPrincipal(message, principal) {
79
- if (principal === undefined) return message
80
- return Object.freeze({ ...message, source: Object.freeze({ ...message.source, principal: deepFreeze(principal) }) })
81
- }
82
-
83
- /**
84
- * A frozen copy of a message whose source records its sender and envelope.
85
- * Fields the message already has are kept.
86
- * @param message - the user message.
87
- * @param metadata - `{ principal, envelope }`, each optional.
88
- * @returns the copy; the message itself when there is nothing to add.
89
- */
90
- export function withMetadata(message, { principal, envelope } = {}) {
91
- const addPrincipal = principal !== undefined && message.source.principal === undefined
92
- const addEnvelope = envelope !== undefined && message.source.envelope === undefined
93
- if (!addPrincipal && !addEnvelope) return message
94
- return Object.freeze({
95
- ...message,
96
- source: Object.freeze({
97
- ...message.source,
98
- ...(addPrincipal ? { principal: deepFreeze(principal) } : {}),
99
- ...(addEnvelope ? { envelope: deepFreeze(envelope) } : {}),
100
- }),
101
- })
102
- }
103
-
104
- function deepFreeze(value) {
105
- if (typeof value === 'object' && value !== null && !Object.isFrozen(value)) {
106
- for (const child of Object.values(value)) deepFreeze(child)
107
- Object.freeze(value)
108
- }
109
- return value
110
- }
111
-
112
- /**
113
- * Principals for prompts the bot hands to DSH's session controller, which
114
- * builds the message itself. The bot remembers the sender by request id
115
- * before the hand-over; `preStep`, an `agent/pre-step` listener, records it
116
- * and the envelope on the message as it enters a step, so they are in the
117
- * session log with the message (DSH lets that hook replace the messages of a
118
- * step). It is registered without `prepend`, so a prepended renderer sees them.
119
- * @param limit - most principals remembered at once.
120
- */
121
- export function createPrincipalTagger(limit = MAX_PENDING_PRINCIPALS) {
122
- const pending = new Map()
123
- return {
124
- /**
125
- * Remember the sender and envelope of the prompt with this request id
126
- * (or, for a message the bot builds itself, this message id).
127
- * @param requestId - the request or message id.
128
- * @param metadata - `{ principal, envelope }`, each optional.
129
- */
130
- remember(requestId, { principal, envelope } = {}) {
131
- if (typeof requestId !== 'string' || (principal === undefined && envelope === undefined)) return
132
- pending.delete(requestId)
133
- pending.set(requestId, { principal, envelope })
134
- if (pending.size > limit) pending.delete(pending.keys().next().value)
135
- },
136
- /** Pending prompts, for tests. */
137
- get size() {
138
- return pending.size
139
- },
140
- /**
141
- * `agent/pre-step` listener: record remembered senders and envelopes on
142
- * the step's messages.
143
- */
144
- async preStep(_payload, next) {
145
- const decision = await next()
146
- if (decision?.kind !== 'enter' || !Array.isArray(decision.messages)) return decision
147
- let changed = false
148
- const messages = []
149
- for (const original of decision.messages) {
150
- let message = original
151
- const user = message?.source?.kind === 'user'
152
- const key = !user ? undefined : (typeof message.source.rpcId === 'string' && pending.has(message.source.rpcId) ? message.source.rpcId : message.id)
153
- const entry = typeof key === 'string' ? pending.get(key) : undefined
154
- if (entry !== undefined) pending.delete(key)
155
- if (entry !== undefined) {
156
- const tagged = withMetadata(message, entry)
157
- if (tagged !== message) changed = true
158
- message = tagged
159
- }
160
- messages.push(message)
161
- }
162
- return changed ? { ...decision, messages } : decision
163
- },
164
- }
165
- }
package/src/text.js CHANGED
@@ -1,41 +1,14 @@
1
1
  /**
2
- * Text going between DSH and Matrix: the answer a turn leaves, Markdown
3
- * rendered to the HTML Matrix clients show, splitting, and the silent marker.
2
+ * Text going between DSH and Matrix: Markdown rendered to the HTML Matrix
3
+ * clients show, local images, splitting and previews. The answer a turn
4
+ * leaves and the silent marker are @soffits/dsh-channel-core's.
4
5
  */
5
6
  import MarkdownIt from 'markdown-it'
6
- import { MAX_MESSAGE_TEXT, SILENT_REPLY } from './constants.js'
7
+ import { MAX_MESSAGE_TEXT } from './constants.js'
7
8
 
8
9
  /** Raw HTML in answers is escaped: clients get only what Markdown makes. */
9
10
  const markdown = new MarkdownIt({ html: false, linkify: true, breaks: true })
10
11
 
11
- /**
12
- * The answer one assistant message holds: the last text block of a message
13
- * that called no tool. Text written between tool calls is working narration,
14
- * not part of the answer; a message that called a tool is mid-chain.
15
- * @param message - an assistant message (`{ content: [...] }`).
16
- * @returns the answer, or '' when the message holds none.
17
- */
18
- export function messageAnswer(message) {
19
- const content = Array.isArray(message?.content) ? message.content : []
20
- if (content.some((block) => block?.type === 'tool-call')) return ''
21
- const texts = content
22
- .filter((block) => block?.type === 'text' && typeof block.text === 'string')
23
- .map((block) => block.text.trim())
24
- .filter((text) => text.length > 0)
25
- return texts.at(-1) ?? ''
26
- }
27
-
28
- /**
29
- * Remove the silent marker from the end of an answer.
30
- * @param text - final answer.
31
- * @returns `{ text, silent }`: the rest of the text and whether the marker was there.
32
- */
33
- export function stripSilent(text) {
34
- const trimmed = String(text ?? '').trim()
35
- if (!trimmed.endsWith(SILENT_REPLY)) return { text: trimmed, silent: false }
36
- return { text: trimmed.slice(0, -SILENT_REPLY.length).trim(), silent: true }
37
- }
38
-
39
12
  /**
40
13
  * Local image paths a Markdown answer shows inline (`![alt](/abs/path)` or a
41
14
  * `file://` URL), outside code. Matrix clients cannot show them inline, so they
@@ -77,16 +50,6 @@ export function toHtml(text) {
77
50
  return markdown.render(String(text ?? '')).trim()
78
51
  }
79
52
 
80
- /**
81
- * Whether streamed text so far could still turn out to be only the silent
82
- * marker, so it is not shown yet.
83
- * @param text - streamed text.
84
- */
85
- export function mayBeSilent(text) {
86
- const trimmed = String(text ?? '').trim()
87
- return trimmed.length === 0 || SILENT_REPLY.startsWith(trimmed)
88
- }
89
-
90
53
  /**
91
54
  * Split text into messages no longer than the limit, preferring paragraph,
92
55
  * then line, then word boundaries.
package/src/turns.js CHANGED
@@ -5,8 +5,9 @@
5
5
  * working reactions of its messages.
6
6
  */
7
7
  import { basename } from 'node:path'
8
+ import { cutSilent, messageAnswer } from '@soffits/dsh-channel-core'
8
9
  import { REQUEST_PREFIX, STREAM_EDIT_MS, TYPING_MS } from './constants.js'
9
- import { clip, localImagePaths, mayBeSilent, messageAnswer, splitText, stripSilent } from './text.js'
10
+ import { clip, localImagePaths, splitText } from './text.js'
10
11
 
11
12
  /**
12
13
  * The event id a request id of a Matrix prompt names.
@@ -115,8 +116,8 @@ export function createTurns({ ctx, getStore, isRunning, setting, outbound, react
115
116
  async function flush(roomId, turn) {
116
117
  const stream = turn.stream
117
118
  if (stream.closed) return
118
- const text = stream.text.trim()
119
- if (mayBeSilent(text) || text === stream.shown) return
119
+ const { text } = cutSilent(stream.text, { streaming: true })
120
+ if (text.length === 0 || text === stream.shown) return
120
121
  const [visible] = splitText(text)
121
122
  stream.lastFlush = Date.now()
122
123
  if (stream.eventId === undefined) {
@@ -273,7 +274,7 @@ export function createTurns({ ctx, getStore, isRunning, setting, outbound, react
273
274
  }
274
275
  // Text the session already sent here with its files is not repeated.
275
276
  const sent = new Set(turn.sentTexts.map(sameText))
276
- const { text, silent } = stripSilent(turn.texts.filter((part) => !sent.has(sameText(part))).join('\n\n'))
277
+ const { text, silent } = cutSilent(turn.texts.filter((part) => !sent.has(sameText(part))).join('\n\n'))
277
278
  if (reason?.kind === 'error' && text.length === 0) {
278
279
  await dropLive()
279
280
  await sendText(roomId, `❌ ${reason.error?.message ?? 'The turn failed.'}`, { plain: true, notice: true })
package/src/models.js DELETED
@@ -1,209 +0,0 @@
1
- /**
2
- * Model selection for rooms. The session's live agent owns its model
3
- * (`agent.model.current()`); the bot keeps no copy of it. Lists come from the
4
- * same catalog as the Web model picker (`sessionController.modelCatalog()`),
5
- * so every configured provider and model shows with its Web display name.
6
- */
7
-
8
- /** How long one catalog read is reused for labels and menus. */
9
- const CATALOG_TTL_MS = 60_000
10
-
11
- /**
12
- * Copy a selection, dropping an absent effort.
13
- * @param selection - provider, model and optional effort.
14
- * @returns a detached selection.
15
- */
16
- function toSelection({ provider, model, reasoningEffort }) {
17
- return { provider, model, ...(reasoningEffort === undefined ? {} : { reasoningEffort }) }
18
- }
19
-
20
- /**
21
- * Read a session's current model: the live agent's own choice, or, for a
22
- * session that is not running, its recorded selection.
23
- * @param ctx - Host context.
24
- * @param sessionId - DSH session id.
25
- * @returns the selection, or undefined when the session has none recorded.
26
- */
27
- export async function sessionModelOf(ctx, sessionId) {
28
- if (typeof sessionId !== 'string') return undefined
29
- const agent = ctx.get('agents')?.get(sessionId)
30
- if (typeof agent?.model?.current === 'function') {
31
- const current = agent.model.current()
32
- return current === undefined ? undefined : toSelection(current)
33
- }
34
- const query = ctx.get('sessionQuery')
35
- if (typeof query?.observeSession !== 'function') return undefined
36
- let observation
37
- try {
38
- observation = await query.observeSession(sessionId)
39
- // The modelSelection projection's view: `next` is the pending choice or
40
- // the route of the last request.
41
- const view = observation?.projections?.values?.modelSelection
42
- const recorded = view?.next ?? view?.pending ?? view?.lastUsed
43
- return typeof recorded?.provider === 'string' && typeof recorded?.model === 'string' ? toSelection(recorded) : undefined
44
- } catch {
45
- return undefined
46
- } finally {
47
- observation?.[Symbol.dispose]?.()
48
- }
49
- }
50
-
51
- /**
52
- * Switch one session to a model without touching the default for new
53
- * sessions. The Web picker's `sessionController.selectModel` saves every pick
54
- * as that default, so a switch in one room would move every room's next
55
- * session; this takes its other steps instead: the host checks it can route
56
- * the model (`llm.resolveCallConfig`, which throws with the reason when it
57
- * cannot), then the live agent records it.
58
- * @param ctx - Host context.
59
- * @param agent - the session's live agent.
60
- * @param selection - provider, model and optional effort.
61
- */
62
- export async function selectSessionModel(ctx, agent, selection) {
63
- const picked = toSelection(selection)
64
- const llm = ctx.get('llm')
65
- if (typeof llm?.resolveCallConfig === 'function') await llm.resolveCallConfig({ ...picked })
66
- agent.model.select(picked)
67
- }
68
-
69
- /**
70
- * Read the deployment default model (the one the Web UI saves).
71
- * @param ctx - Host context.
72
- * @returns the selection, or undefined without a default-model service.
73
- */
74
- export function defaultModelOf(ctx) {
75
- try {
76
- const selection = ctx.get('agentDefaultModel')?.currentSelection()
77
- if (typeof selection?.provider === 'string' && typeof selection?.model === 'string') return toSelection(selection)
78
- } catch {
79
- // No default configured.
80
- }
81
- return undefined
82
- }
83
-
84
- /**
85
- * Whether the catalog no longer offers a selection's provider or model, for
86
- * example after a provider was removed from the profile. A provider the
87
- * catalog reports as failing is not counted: its models are unknown now, not
88
- * gone. Without a catalog nothing is called gone.
89
- * @param catalog - loaded catalog, or undefined.
90
- * @param selection - provider and model, or undefined.
91
- * @returns true when the selection names something the catalog dropped.
92
- */
93
- export function isGoneModel(catalog, selection) {
94
- if (catalog === undefined || typeof selection?.provider !== 'string' || typeof selection?.model !== 'string') return false
95
- if (catalog.failures.some((failure) => failure?.id === selection.provider)) return false
96
- return findModel(catalog, selection) === undefined
97
- }
98
-
99
- /**
100
- * Whether two selections name the same provider, model and effort.
101
- * @param left - first selection.
102
- * @param right - second selection.
103
- * @returns true when they agree.
104
- */
105
- export function sameSelection(left, right) {
106
- return left?.provider === right?.provider && left?.model === right?.model && left?.reasoningEffort === right?.reasoningEffort
107
- }
108
-
109
- /**
110
- * Cached reader for the Web model catalog.
111
- * @param ctx - Host context.
112
- * @returns `load()` resolving the catalog, or undefined when it is unavailable.
113
- */
114
- export function createModelCatalog(ctx) {
115
- let cached
116
- let loadedAt = 0
117
- let pending
118
- return {
119
- /**
120
- * @param fresh - skip the short-lived cache (used before applying a pick).
121
- * @returns `{ groups, failures }` as the Web picker gets it, or undefined.
122
- */
123
- async load(fresh = false) {
124
- if (!fresh && cached !== undefined && Date.now() - loadedAt < CATALOG_TTL_MS) return cached
125
- pending ??= (async () => {
126
- const controller = ctx.get('sessionController')
127
- if (typeof controller?.modelCatalog !== 'function') return undefined
128
- const catalog = await controller.modelCatalog()
129
- cached = { groups: Array.isArray(catalog?.groups) ? catalog.groups : [], failures: Array.isArray(catalog?.failures) ? catalog.failures : [] }
130
- loadedAt = Date.now()
131
- return cached
132
- })().finally(() => { pending = undefined })
133
- try {
134
- return await pending
135
- } catch {
136
- return cached
137
- }
138
- },
139
- }
140
- }
141
-
142
- /**
143
- * Find a catalog entry for a selection.
144
- * @param catalog - loaded catalog.
145
- * @param selection - provider and model.
146
- * @returns `{ group, model }`, or undefined.
147
- */
148
- export function findModel(catalog, selection) {
149
- const group = catalog?.groups.find((entry) => entry.id === selection?.provider)
150
- const model = group?.models.find((entry) => entry.id === selection?.model)
151
- return model === undefined ? undefined : { group, model }
152
- }
153
-
154
- /**
155
- * Display name of a selection's model, as the Web picker shows it.
156
- * @param catalog - loaded catalog, or undefined.
157
- * @param selection - provider and model.
158
- * @returns the name, or the raw model id.
159
- */
160
- export function modelName(catalog, selection) {
161
- if (selection === undefined) return '?'
162
- return findModel(catalog, selection)?.model.name ?? selection.model
163
- }
164
-
165
- /**
166
- * Effort choices for one catalog model, the same list the Web picker builds:
167
- * a provider-default entry when the model has no default effort, then its
168
- * efforts. A model without reasoning has none.
169
- * @param model - catalog model entry.
170
- * @param defaultLabel - label for the provider default.
171
- * @returns `{ id, name }` entries; `id` undefined means the provider default.
172
- */
173
- export function effortChoices(model, defaultLabel) {
174
- const reasoning = model?.reasoning
175
- if (reasoning === undefined) return []
176
- return [
177
- ...(reasoning.defaultEffort === undefined ? [{ id: undefined, name: defaultLabel }] : []),
178
- ...reasoning.efforts.map((effort) => ({ id: effort.id, name: effort.name })),
179
- ]
180
- }
181
-
182
- /**
183
- * Display name of a selection's effective effort.
184
- * @param catalog - loaded catalog, or undefined.
185
- * @param selection - current selection.
186
- * @param defaultLabel - label for the provider default.
187
- * @returns the name.
188
- */
189
- export function effortName(catalog, selection, defaultLabel) {
190
- const reasoning = findModel(catalog, selection)?.model.reasoning
191
- const effective = selection?.reasoningEffort ?? reasoning?.defaultEffort
192
- if (effective === undefined) return defaultLabel
193
- return reasoning?.efforts.find((effort) => effort.id === effective)?.name ?? effective
194
- }
195
-
196
- /**
197
- * The selection a model pick makes, by the Web picker's rule: the same model
198
- * keeps its effort, another model starts at its default effort.
199
- * @param current - the session's current selection.
200
- * @param provider - picked provider id.
201
- * @param model - picked catalog model entry.
202
- * @returns the selection to apply.
203
- */
204
- export function pickedSelection(current, provider, model) {
205
- const same = current?.provider === provider && current?.model === model.id
206
- const reasoningEffort = same ? current.reasoningEffort ?? model.reasoning?.defaultEffort : model.reasoning?.defaultEffort
207
- return toSelection({ provider, model: model.id, reasoningEffort })
208
- }
209
-
package/src/voice.js DELETED
@@ -1,126 +0,0 @@
1
- /** Voice messages and audio files: decoding and transcription. */
2
- import { spawn } from 'node:child_process'
3
-
4
- /**
5
- * Wrap raw 16 kHz mono 16-bit PCM in the canonical 44-byte WAV header that
6
- * DSH speech providers accept.
7
- * @param pcm - little-endian samples.
8
- * @returns WAV bytes.
9
- */
10
- export function pcmToWave(pcm) {
11
- const data = pcm.length % 2 === 0 ? pcm : pcm.subarray(0, pcm.length - 1)
12
- const header = Buffer.alloc(44)
13
- header.write('RIFF', 0, 'ascii')
14
- header.writeUInt32LE(36 + data.length, 4)
15
- header.write('WAVE', 8, 'ascii')
16
- header.write('fmt ', 12, 'ascii')
17
- header.writeUInt32LE(16, 16)
18
- header.writeUInt16LE(1, 20)
19
- header.writeUInt16LE(1, 22)
20
- header.writeUInt32LE(16000, 24)
21
- header.writeUInt32LE(32000, 28)
22
- header.writeUInt16LE(2, 32)
23
- header.writeUInt16LE(16, 34)
24
- header.write('data', 36, 'ascii')
25
- header.writeUInt32LE(data.length, 40)
26
- return Buffer.concat([header, data])
27
- }
28
-
29
- /**
30
- * Run one program and collect its output.
31
- * @param argv - program and arguments; no shell is involved.
32
- * @param signal - cancellation.
33
- * @param timeoutMs - upper bound for the run.
34
- * @returns stdout bytes; rejects with stderr on a non-zero exit.
35
- */
36
- export function runProgram(argv, signal, timeoutMs) {
37
- return new Promise((resolve, reject) => {
38
- if (signal?.aborted) {
39
- reject(signal.reason ?? new Error('aborted'))
40
- return
41
- }
42
- const child = spawn(argv[0], argv.slice(1), { stdio: ['ignore', 'pipe', 'pipe'] })
43
- const out = []
44
- const err = []
45
- let settled = false
46
- const finish = (error, value) => {
47
- if (settled) return
48
- settled = true
49
- clearTimeout(timer)
50
- signal?.removeEventListener('abort', abort)
51
- if (error) reject(error)
52
- else resolve(value)
53
- }
54
- const abort = () => {
55
- child.kill('SIGKILL')
56
- finish(new Error(`${argv[0]} was cancelled`))
57
- }
58
- const timer = setTimeout(() => {
59
- child.kill('SIGKILL')
60
- finish(new Error(`${argv[0]} took longer than ${Math.round(timeoutMs / 1000)} s`))
61
- }, timeoutMs)
62
- signal?.addEventListener('abort', abort, { once: true })
63
- child.stdout.on('data', (chunk) => out.push(chunk))
64
- child.stderr.on('data', (chunk) => err.push(chunk))
65
- child.on('error', (error) => finish(new Error(`${argv[0]} could not start: ${error.message}`)))
66
- child.on('close', (code) => {
67
- if (code === 0) finish(undefined, Buffer.concat(out))
68
- else finish(new Error(`${argv[0]} exited with ${code}: ${Buffer.concat(err).toString('utf8').trim().slice(-400)}`))
69
- })
70
- })
71
- }
72
-
73
- /**
74
- * Transcribe one recording.
75
- * @param options - `{ mode, path, language, command, ffmpeg, speech, signal, timeoutMs }`:
76
- * `mode` is `dsh` (the DSH speech-to-text service, `speech`), `command`
77
- * (`command` argv, `{file}` replaced by `path`, transcript on stdout) or `off`.
78
- * @returns `{ text, by }`; rejects when transcription fails.
79
- */
80
- export async function transcribe(options) {
81
- const { mode, path, signal } = options
82
- const timeoutMs = options.timeoutMs ?? 180_000
83
- if (mode === 'command') {
84
- const command = Array.isArray(options.command) ? options.command.filter((part) => typeof part === 'string') : []
85
- if (command.length === 0) throw new Error('no transcription command is configured')
86
- const argv = command.some((part) => part.includes('{file}'))
87
- ? command.map((part) => part.replaceAll('{file}', path))
88
- : [...command, path]
89
- const text = (await runProgram(argv, signal, timeoutMs)).toString('utf8').trim()
90
- return { text, by: 'the configured transcription command' }
91
- }
92
- if (mode !== 'dsh') throw new Error('transcription is turned off')
93
- const speech = options.speech
94
- if (speech === undefined || typeof speech.resolve !== 'function') throw new Error('the DSH speech-to-text service is not enabled')
95
- const pcm = await runProgram(
96
- [options.ffmpeg || 'ffmpeg', '-nostdin', '-hide_banner', '-loglevel', 'error', '-i', path, '-vn', '-ac', '1', '-ar', '16000', '-f', 's16le', 'pipe:1'],
97
- signal,
98
- timeoutMs,
99
- )
100
- if (pcm.length === 0) throw new Error('the recording has no audio')
101
- const spec = speech.resolve({
102
- audio: pcmToWave(pcm),
103
- ...(typeof options.language === 'string' && options.language.length > 0 ? { language: options.language } : {}),
104
- })
105
- const result = await speech.transcribe(spec, signal ?? new AbortController().signal)
106
- const provider = spec.provider?.info?.name ?? spec.provider?.info?.id
107
- return { text: String(result?.text ?? '').trim(), by: `DSH speech-to-text${provider ? ` (${provider})` : ''}` }
108
- }
109
-
110
- /**
111
- * The text the agent reads next to a spoken recording.
112
- * @param media - `{ kind, name, seconds }`.
113
- * @param outcome - `{ path, text, by, error }`.
114
- * @returns prompt text.
115
- */
116
- export function transcriptNote(media, outcome) {
117
- const length = media.seconds === undefined ? '' : `, ${Math.floor(media.seconds / 60)}:${String(media.seconds % 60).padStart(2, '0')}`
118
- const where = typeof outcome.path === 'string' ? `the file is attached and saved at ${outcome.path}` : 'the file is attached'
119
- if (outcome.error !== undefined) {
120
- return `[Matrix ${media.kind} "${media.name}"${length}: not transcribed (${outcome.error}); ${where}]`
121
- }
122
- if (outcome.text.length === 0) {
123
- return `[Matrix ${media.kind} "${media.name}"${length}: ${outcome.by} heard no speech; ${where}]`
124
- }
125
- return `[Matrix ${media.kind} "${media.name}"${length}, transcribed by ${outcome.by}; the transcript may contain mistakes, and ${where} to check it]\n${outcome.text}`
126
- }