@soffits/dsh-matrix-bot 0.1.0 → 0.2.0
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 +4 -0
- package/package.json +1 -1
- package/src/events.js +10 -5
- package/src/inbound.js +27 -17
- package/src/sender.js +38 -66
- package/src/sessions.js +2 -2
- package/src/text.js +0 -14
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## 0.2.0
|
|
6
|
+
|
|
7
|
+
- Prompts carry metadata only: the sender on `source.principal` (the room is now `chat: { id, name }`, was `room`), and the send time and the replied-to message on `source.envelope` (`sentAt`, `replyTo`), the shape [dsh-message-context](https://github.com/soffits/dsh-message-context) reads. The bot no longer adds its own `matrix-sender` notice or reply text; install dsh-message-context to show the model who wrote each message.
|
|
8
|
+
|
|
5
9
|
## 0.1.0
|
|
6
10
|
|
|
7
11
|
First release: a Matrix bot for DSH with end-to-end encryption (cross-signing, recovery key, key backup), a private Space per allowed person with one room per session, rooms joined from the Space or made with `!new`, streamed answers, typing notices and working reactions, files and voice messages, tool approvals and agent questions answered with reactions, Telegram-style commands published for bot command completion, `Matrix-<person>-<task>` session titles, the `matrixRooms` send service, the `matrix_send` tool and the `dsh-matrix` command.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soffits/dsh-matrix-bot",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
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",
|
package/src/events.js
CHANGED
|
@@ -6,11 +6,11 @@ import { randomUUID } from 'node:crypto'
|
|
|
6
6
|
import { join } from 'node:path'
|
|
7
7
|
import { parseCommand, runCommand } from './commands.js'
|
|
8
8
|
import { dataDir, POST_SOURCE_KIND, REQUEST_PREFIX } from './constants.js'
|
|
9
|
-
import { composePrompt,
|
|
9
|
+
import { composePrompt, hasMedia, ownText, replyInfo, replyTargetOf } from './inbound.js'
|
|
10
10
|
import { allowedEntry } from './operations.js'
|
|
11
11
|
import { matrixUserPrincipal, withPrincipal } from './sender.js'
|
|
12
12
|
import { admitPrompt, hasRequest, recordMessage, unarchive } from './sessions.js'
|
|
13
|
-
import { clip
|
|
13
|
+
import { clip } from './text.js'
|
|
14
14
|
|
|
15
15
|
/** @returns a short message for any thrown value. */
|
|
16
16
|
function messageOf(error) {
|
|
@@ -185,7 +185,7 @@ export function createEvents({ ctx, getStore, storeReady, getMatrix, setting, sp
|
|
|
185
185
|
if (react) await reactions.mark(roomId, event.event_id, reactions.emojiFor(live?.status !== undefined && live.status !== 'idle', steer))
|
|
186
186
|
const replyTo = replyTargetOf(event.content)
|
|
187
187
|
const target = replyTo === undefined ? undefined : await getMatrix().getEvent(roomId, replyTo).catch(() => undefined)
|
|
188
|
-
const
|
|
188
|
+
const reply = replyInfo(target, { botId: getMatrix().userId, senderId: sender.userId, sentRecord: replyTo === undefined ? undefined : getStore().sentRecord(replyTo), names: { [sender.userId]: sender.name } })
|
|
189
189
|
const prompt = await composePrompt(event, {
|
|
190
190
|
download: (content) => getMatrix().download(content),
|
|
191
191
|
mediaDir: join(dataDir(), 'media'),
|
|
@@ -206,8 +206,13 @@ export function createEvents({ ctx, getStore, storeReady, getMatrix, setting, sp
|
|
|
206
206
|
const running = agent.status !== undefined && agent.status !== 'idle'
|
|
207
207
|
const mode = running && steer ? 'steer' : 'queue'
|
|
208
208
|
const principal = matrixUserPrincipal(sender.userId, { name: sender.name, roomId, room: getStore().room(roomId)?.roomName })
|
|
209
|
-
|
|
210
|
-
|
|
209
|
+
const envelope = {
|
|
210
|
+
...(typeof event.origin_server_ts === 'number' ? { sentAt: event.origin_server_ts } : {}),
|
|
211
|
+
...(reply === undefined ? {} : { replyTo: reply }),
|
|
212
|
+
}
|
|
213
|
+
const metadata = { principal, ...(Object.keys(envelope).length === 0 ? {} : { envelope }) }
|
|
214
|
+
principals.remember(requestId, metadata)
|
|
215
|
+
const admitted = await admitPrompt(ctx, agent, { requestId, text: prompt.text, attachments: prompt.attachments, mode, ...metadata }, signal)
|
|
211
216
|
if (!react) return
|
|
212
217
|
if (admitted) reactions.admitted(event.event_id)
|
|
213
218
|
else await reactions.release([event.event_id])
|
package/src/inbound.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Turn one incoming Matrix message into a DSH prompt: its text, its media as
|
|
3
|
-
* an attachment (pictures as images, voice messages transcribed), and
|
|
4
|
-
*
|
|
3
|
+
* an attachment (pictures as images, voice messages transcribed), and what it
|
|
4
|
+
* replies to as metadata for the message's `source.envelope`.
|
|
5
5
|
*/
|
|
6
6
|
import { mkdir, writeFile } from 'node:fs/promises'
|
|
7
7
|
import { extname, join } from 'node:path'
|
|
@@ -9,10 +9,6 @@ import { MAX_FILE_BYTES } from './constants.js'
|
|
|
9
9
|
import { clip } from './text.js'
|
|
10
10
|
import { transcribe, transcriptNote } from './voice.js'
|
|
11
11
|
|
|
12
|
-
/** Longest replied-to text passed to the agent. */
|
|
13
|
-
const QUOTE_LIMIT = 2000
|
|
14
|
-
/** Longest text of a message sent on someone's behalf passed to the agent. */
|
|
15
|
-
const SENT_TEXT_LIMIT = 8000
|
|
16
12
|
/** Image types the DSH attachment store takes as pictures. */
|
|
17
13
|
const IMAGE_TYPES = new Set(['image/jpeg', 'image/png', 'image/webp', 'image/gif'])
|
|
18
14
|
/** Message types that carry media. */
|
|
@@ -46,25 +42,39 @@ export function hasMedia(content) {
|
|
|
46
42
|
}
|
|
47
43
|
|
|
48
44
|
/**
|
|
49
|
-
*
|
|
45
|
+
* What a message replies to, as `envelope.replyTo` (the shape dsh-message-context reads).
|
|
50
46
|
* @param target - the replied-to event, or undefined when it could not be read.
|
|
51
|
-
* @param options - `{ botId, sentRecord, names }`: the bot's user id,
|
|
52
|
-
* record the target belongs to, and names by user id.
|
|
53
|
-
* @returns
|
|
47
|
+
* @param options - `{ botId, senderId, sentRecord, names }`: the bot's user id,
|
|
48
|
+
* the replying user's id, the send record the target belongs to, and names by user id.
|
|
49
|
+
* @returns `{ from, sentAt, text, details }` (fields only when known), or undefined.
|
|
54
50
|
*/
|
|
55
|
-
export function
|
|
51
|
+
export function replyInfo(target, { botId, senderId, sentRecord, names = {} } = {}) {
|
|
56
52
|
if (target === undefined) return undefined
|
|
57
53
|
const content = target.content ?? {}
|
|
58
|
-
const
|
|
54
|
+
const details = []
|
|
55
|
+
let text = hasMedia(content) ? undefined : ownText(content)
|
|
56
|
+
if (hasMedia(content)) details.push(`contains ${content.msgtype.slice(2)} "${content.body ?? ''}"`)
|
|
57
|
+
let from
|
|
59
58
|
if (target.sender === botId) {
|
|
59
|
+
from = { role: 'assistant' }
|
|
60
60
|
if (sentRecord !== undefined) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
details.unshift('posted by the bot on behalf of another sender')
|
|
62
|
+
if (sentRecord.job) from = { kind: 'cron-job', channel: 'cron', id: sentRecord.job, name: sentRecord.job }
|
|
63
|
+
else if (sentRecord.fromSession) from = { kind: 'dsh-session', channel: 'dsh', id: sentRecord.fromSession }
|
|
64
|
+
else if (sentRecord.label) from = { kind: 'external', channel: 'external', id: sentRecord.label, name: sentRecord.label }
|
|
65
|
+
if (typeof sentRecord.text === 'string' && sentRecord.text.length > 0) text = sentRecord.text
|
|
64
66
|
}
|
|
65
|
-
|
|
67
|
+
} else if (target.sender === senderId) {
|
|
68
|
+
from = { role: 'sender' }
|
|
69
|
+
} else {
|
|
70
|
+
from = { kind: 'matrix-user', channel: 'matrix', id: target.sender, ...(names[target.sender] ? { name: names[target.sender] } : {}) }
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
from,
|
|
74
|
+
...(typeof target.origin_server_ts === 'number' ? { sentAt: target.origin_server_ts } : {}),
|
|
75
|
+
...(text ? { text } : {}),
|
|
76
|
+
...(details.length > 0 ? { details } : {}),
|
|
66
77
|
}
|
|
67
|
-
return `[Replying to ${names[target.sender] || target.sender}:]\n${text}`
|
|
68
78
|
}
|
|
69
79
|
|
|
70
80
|
/** @returns a safe file name for a download. */
|
package/src/sender.js
CHANGED
|
@@ -11,15 +11,12 @@
|
|
|
11
11
|
* known; the other fields depend on the kind. None carries a credential, so
|
|
12
12
|
* all are safe in a session log.
|
|
13
13
|
*
|
|
14
|
-
*
|
|
15
|
-
* the message
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* person's own.
|
|
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.
|
|
19
18
|
*/
|
|
20
19
|
|
|
21
|
-
import { randomUUID } from 'node:crypto'
|
|
22
|
-
|
|
23
20
|
/** Remembered prompts waiting for their step; the oldest go first past this. */
|
|
24
21
|
export const MAX_PENDING_PRINCIPALS = 1024
|
|
25
22
|
|
|
@@ -30,7 +27,8 @@ function nonEmpty(value) {
|
|
|
30
27
|
/**
|
|
31
28
|
* The Matrix user who wrote a message, and where.
|
|
32
29
|
* @param userId - their Matrix user id.
|
|
33
|
-
* @param where - `{ name, roomId, room }`: their name from the allowlist, the room and its name
|
|
30
|
+
* @param where - `{ name, roomId, room }`: their name from the allowlist, the room and its name,
|
|
31
|
+
* recorded as `chat` (where the message was sent), the field renderers read.
|
|
34
32
|
* @returns the principal, or undefined without a sender.
|
|
35
33
|
*/
|
|
36
34
|
export function matrixUserPrincipal(userId, { name, roomId, room } = {}) {
|
|
@@ -40,7 +38,7 @@ export function matrixUserPrincipal(userId, { name, roomId, room } = {}) {
|
|
|
40
38
|
channel: 'matrix',
|
|
41
39
|
id: userId,
|
|
42
40
|
...(nonEmpty(name) === undefined ? {} : { name: nonEmpty(name) }),
|
|
43
|
-
...(nonEmpty(roomId) === undefined ? {} : {
|
|
41
|
+
...(nonEmpty(roomId) === undefined ? {} : { chat: { id: roomId, ...(nonEmpty(room) === undefined ? {} : { name: nonEmpty(room) }) } }),
|
|
44
42
|
}
|
|
45
43
|
}
|
|
46
44
|
|
|
@@ -82,47 +80,24 @@ export function withPrincipal(message, principal) {
|
|
|
82
80
|
return Object.freeze({ ...message, source: Object.freeze({ ...message.source, principal: deepFreeze(principal) }) })
|
|
83
81
|
}
|
|
84
82
|
|
|
85
|
-
/** Source kind of the sender notice before each Matrix prompt. */
|
|
86
|
-
export const SENDER_NOTICE_KIND = 'matrix-sender'
|
|
87
|
-
|
|
88
83
|
/**
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
* @param
|
|
93
|
-
* @
|
|
94
|
-
* @returns a one-line label, or undefined with neither a Matrix sender nor a time.
|
|
95
|
-
*/
|
|
96
|
-
export function senderLabel(principal, sentAt) {
|
|
97
|
-
const time = nonEmpty(sentAt) === undefined ? '' : `, sent ${sentAt.trim()}`
|
|
98
|
-
if (principal?.kind !== 'matrix-user' || principal.id === undefined) {
|
|
99
|
-
return time === '' ? undefined : `[Matrix message${time}]`
|
|
100
|
-
}
|
|
101
|
-
const name = nonEmpty(principal.name)
|
|
102
|
-
return name !== undefined ? `[Matrix message from ${name} (${principal.id})${time}]` : `[Matrix message from ${principal.id}${time}]`
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* The notice that comes before a prompt: who sent it, when, and the context
|
|
107
|
-
* the bot adds around it. It is its own message with a producer source, so
|
|
108
|
-
* DSH's title input and readers of human messages see only what the person
|
|
109
|
-
* wrote.
|
|
110
|
-
* @param message - a user message entering a step.
|
|
111
|
-
* @param details - `{ context, sentAt }`: context blocks for this prompt (see
|
|
112
|
-
* `composePrompt`) and when it was sent, as text; both optional.
|
|
113
|
-
* @returns the notice, or undefined with no sender, time or context.
|
|
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.
|
|
114
89
|
*/
|
|
115
|
-
export function
|
|
116
|
-
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
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
|
+
}),
|
|
126
101
|
})
|
|
127
102
|
}
|
|
128
103
|
|
|
@@ -138,23 +113,24 @@ function deepFreeze(value) {
|
|
|
138
113
|
* Principals for prompts the bot hands to DSH's session controller, which
|
|
139
114
|
* builds the message itself. The bot remembers the sender by request id
|
|
140
115
|
* before the hand-over; `preStep`, an `agent/pre-step` listener, records it
|
|
141
|
-
* on the message as it enters a step, so
|
|
142
|
-
* message (DSH lets that hook replace the messages of a
|
|
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.
|
|
143
119
|
* @param limit - most principals remembered at once.
|
|
144
120
|
*/
|
|
145
121
|
export function createPrincipalTagger(limit = MAX_PENDING_PRINCIPALS) {
|
|
146
122
|
const pending = new Map()
|
|
147
123
|
return {
|
|
148
124
|
/**
|
|
149
|
-
* Remember the sender
|
|
150
|
-
*
|
|
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).
|
|
151
127
|
* @param requestId - the request or message id.
|
|
152
|
-
* @param
|
|
128
|
+
* @param metadata - `{ principal, envelope }`, each optional.
|
|
153
129
|
*/
|
|
154
|
-
remember(requestId, { principal,
|
|
155
|
-
if (typeof requestId !== 'string' || (principal === undefined &&
|
|
130
|
+
remember(requestId, { principal, envelope } = {}) {
|
|
131
|
+
if (typeof requestId !== 'string' || (principal === undefined && envelope === undefined)) return
|
|
156
132
|
pending.delete(requestId)
|
|
157
|
-
pending.set(requestId, { principal,
|
|
133
|
+
pending.set(requestId, { principal, envelope })
|
|
158
134
|
if (pending.size > limit) pending.delete(pending.keys().next().value)
|
|
159
135
|
},
|
|
160
136
|
/** Pending prompts, for tests. */
|
|
@@ -162,8 +138,8 @@ export function createPrincipalTagger(limit = MAX_PENDING_PRINCIPALS) {
|
|
|
162
138
|
return pending.size
|
|
163
139
|
},
|
|
164
140
|
/**
|
|
165
|
-
* `agent/pre-step` listener: record remembered senders
|
|
166
|
-
*
|
|
141
|
+
* `agent/pre-step` listener: record remembered senders and envelopes on
|
|
142
|
+
* the step's messages.
|
|
167
143
|
*/
|
|
168
144
|
async preStep(_payload, next) {
|
|
169
145
|
const decision = await next()
|
|
@@ -176,14 +152,10 @@ export function createPrincipalTagger(limit = MAX_PENDING_PRINCIPALS) {
|
|
|
176
152
|
const key = !user ? undefined : (typeof message.source.rpcId === 'string' && pending.has(message.source.rpcId) ? message.source.rpcId : message.id)
|
|
177
153
|
const entry = typeof key === 'string' ? pending.get(key) : undefined
|
|
178
154
|
if (entry !== undefined) pending.delete(key)
|
|
179
|
-
if (entry
|
|
180
|
-
|
|
181
|
-
changed = true
|
|
182
|
-
|
|
183
|
-
const notice = senderNotice(message, entry)
|
|
184
|
-
if (notice !== undefined) {
|
|
185
|
-
messages.push(notice)
|
|
186
|
-
changed = true
|
|
155
|
+
if (entry !== undefined) {
|
|
156
|
+
const tagged = withMetadata(message, entry)
|
|
157
|
+
if (tagged !== message) changed = true
|
|
158
|
+
message = tagged
|
|
187
159
|
}
|
|
188
160
|
messages.push(message)
|
|
189
161
|
}
|
package/src/sessions.js
CHANGED
|
@@ -182,7 +182,7 @@ export function hasRequest(agent, requestId) {
|
|
|
182
182
|
* @param signal - request lifetime.
|
|
183
183
|
* @returns whether the prompt was handed over (false for a duplicate or an empty one).
|
|
184
184
|
*/
|
|
185
|
-
export async function admitPrompt(ctx, agent, { requestId, text, attachments = [], mode, principal }, signal) {
|
|
185
|
+
export async function admitPrompt(ctx, agent, { requestId, text, attachments = [], mode, principal, envelope }, signal) {
|
|
186
186
|
if (hasRequest(agent, requestId)) return false
|
|
187
187
|
const content = [...(text.trim().length === 0 ? [] : [{ type: 'text', text }]), ...attachments]
|
|
188
188
|
if (content.length === 0) return false
|
|
@@ -192,7 +192,7 @@ export async function admitPrompt(ctx, agent, { requestId, text, attachments = [
|
|
|
192
192
|
await controller.prompt({ sessionId, requestId, mode, content }, signal)
|
|
193
193
|
return true
|
|
194
194
|
}
|
|
195
|
-
const message = { id: randomUUID(), role: 'user', content, source: { kind: 'user', rpcId: requestId, ...(principal === undefined ? {} : { principal }) } }
|
|
195
|
+
const message = { id: randomUUID(), role: 'user', content, source: { kind: 'user', rpcId: requestId, ...(principal === undefined ? {} : { principal }), ...(envelope === undefined ? {} : { envelope }) } }
|
|
196
196
|
if (mode === 'steer' && typeof agent.steer === 'function') agent.steer(message)
|
|
197
197
|
else if (typeof agent.followup === 'function') agent.followup(message)
|
|
198
198
|
else agent.send(message, mode === 'steer' ? 'next-step' : 'next-turn', true)
|
package/src/text.js
CHANGED
|
@@ -87,20 +87,6 @@ export function mayBeSilent(text) {
|
|
|
87
87
|
return trimmed.length === 0 || SILENT_REPLY.startsWith(trimmed)
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
/**
|
|
91
|
-
* When a message was sent, for the agent: the date and time in this
|
|
92
|
-
* process's time zone, with the zone's name.
|
|
93
|
-
* @param timestamp - milliseconds since the epoch.
|
|
94
|
-
* @returns the time as text, or undefined without a timestamp.
|
|
95
|
-
*/
|
|
96
|
-
export function formatMoment(timestamp) {
|
|
97
|
-
if (!Number.isFinite(timestamp)) return undefined
|
|
98
|
-
const zone = Intl.DateTimeFormat().resolvedOptions().timeZone
|
|
99
|
-
const parts = new Intl.DateTimeFormat('en-CA', { timeZone: zone, year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', hourCycle: 'h23', weekday: 'short' }).formatToParts(new Date(timestamp))
|
|
100
|
-
const part = (type) => parts.find((entry) => entry.type === type)?.value ?? ''
|
|
101
|
-
return `${part('weekday')} ${part('year')}-${part('month')}-${part('day')} ${part('hour')}:${part('minute')} (${zone})`
|
|
102
|
-
}
|
|
103
|
-
|
|
104
90
|
/**
|
|
105
91
|
* Split text into messages no longer than the limit, preferring paragraph,
|
|
106
92
|
* then line, then word boundaries.
|