@soffits/dsh-plugin-sdk 0.0.0-stage → 0.1.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.
@@ -0,0 +1,132 @@
1
+ /**
2
+ * Fakes of DSH behaviour for bridge tests, imported as
3
+ * `@soffits/dsh-plugin-sdk/channel/testing`: how a queued message passes the
4
+ * `agent/pre-step` gate, the `sessionTitle` service, and `agent.model` folded
5
+ * from the session log. A bridge builds its own fake Host and agents around
6
+ * them, so every bridge's tests model DSH the same way.
7
+ */
8
+
9
+ /** @returns a fake session's events, from `snapshotEvents()` or an `events` array. */
10
+ function eventsOf(session) {
11
+ if (typeof session.snapshotEvents === 'function') return session.snapshotEvents()
12
+ return Array.isArray(session.events) ? session.events : []
13
+ }
14
+
15
+ /**
16
+ * Model how DSH starts a turn for a queued message: every follow-up opens a
17
+ * turn (so the session is no longer blank), and `agent/pre-step` handlers may
18
+ * reject it before any model request, in which case the message is never
19
+ * recorded. Messages a handler adds with another source kind (notices) go
20
+ * to `agent.notices`; `received` holds the prompts as recorded.
21
+ * @param agent - fake agent with `received`; gets `turns` and `rejected`.
22
+ * @param message - the queued user message.
23
+ * @param handlers - the `agent/pre-step` handlers registered now, the
24
+ * agent's own first, then host-wide ones.
25
+ */
26
+ export function followupThroughGate(agent, message, handlers) {
27
+ agent.turns = (agent.turns ?? 0) + 1
28
+ agent.received.push(message)
29
+ const run = (index) => index < handlers.length
30
+ ? Promise.resolve(handlers[index]({ agent, messages: [message] }, () => run(index + 1)))
31
+ : Promise.resolve({ kind: 'enter', messages: [message] })
32
+ void run(0).then((decision) => {
33
+ const at = agent.received.indexOf(message)
34
+ if (at === -1) return
35
+ if (decision?.kind === 'reject') {
36
+ agent.received.splice(at, 1)
37
+ agent.rejected ??= []
38
+ agent.rejected.push(message)
39
+ } else if (Array.isArray(decision?.messages)) {
40
+ // What DSH records: the step's messages as the handlers left them;
41
+ // producer notices (source kinds other than user) are kept apart.
42
+ agent.received.splice(at, 1, ...decision.messages.filter((item) => item.source?.kind === 'user'))
43
+ agent.notices ??= []
44
+ agent.notices.push(...decision.messages.filter((item) => item.source?.kind !== 'user'))
45
+ }
46
+ })
47
+ }
48
+
49
+ /**
50
+ * Fake `sessionTitle` service. A title is the latest `session/title` event
51
+ * of the session, else the fields a test put on the session object (`title`,
52
+ * `titleSource`, `titleSeqs`, a title kept from an earlier run). Titles are
53
+ * written through `session.append('session/title', ...)` when the fake
54
+ * session has one, so listeners see the event as in DSH. `refresh` stands in
55
+ * for the title model: it writes `session.nextGeneratedTitle`, or a fixed one.
56
+ * @returns the service.
57
+ */
58
+ export function makeTitleService() {
59
+ const write = (session, data) => {
60
+ if (typeof session.append === 'function') session.append('session/title', data)
61
+ else Object.assign(session, { title: data.title, titleSource: data.source, titleSeqs: data.messageSeqs })
62
+ }
63
+ const service = {
64
+ refreshed: [],
65
+ // DSH's private provider slot; set to undefined to run without a provider.
66
+ registration: { provider: { id: 'fake' }, closing: false },
67
+ get: (session) => {
68
+ const event = eventsOf(session).filter((item) => item.type === 'session/title').at(-1)
69
+ if (event !== undefined) return { title: event.data.title, source: event.data.source ?? { kind: 'user' }, messageSeqs: event.data.messageSeqs ?? [] }
70
+ return typeof session.title === 'string'
71
+ ? { title: session.title, source: session.titleSource ?? { kind: 'user' }, messageSeqs: session.titleSeqs ?? [] }
72
+ : undefined
73
+ },
74
+ rename: (session, title) => {
75
+ write(session, { title, messageSeqs: [], source: { kind: 'user' } })
76
+ return service.get(session)
77
+ },
78
+ async refresh(session) {
79
+ service.refreshed.push(session.id)
80
+ if (service.registration === undefined) {
81
+ // DSH's built-in fallback: the first words of the first human message, over any title.
82
+ const first = eventsOf(session).find((event) => event.type === 'user/message' && event.data?.source?.kind === 'user')
83
+ const text = (first?.data.content ?? []).filter((block) => block.type === 'text').map((block) => block.text).join('\n')
84
+ write(session, { title: text.split(/\s+/u).filter(Boolean).slice(0, 5).join(' '), messageSeqs: [first?.seq ?? 1], source: { kind: 'fallback' } })
85
+ return service.get(session)
86
+ }
87
+ write(session, { title: session.nextGeneratedTitle ?? 'Generated task', messageSeqs: [1], source: { kind: 'provider', provider: 'fake' } })
88
+ return service.get(session)
89
+ },
90
+ }
91
+ return service
92
+ }
93
+
94
+ /**
95
+ * Fake `agent.model`, folding the session log the way DSH's modelSelection
96
+ * projection does: a `model/selection` event is the pending choice, a
97
+ * `request/header` is the last route used and retires a matching pending
98
+ * choice; with neither, the agent's options apply.
99
+ * @param session - fake session with `append` and `snapshotEvents()` or `events`.
100
+ * @param options - agent options (`provider`, `model`, `reasoningEffort`), may be getters.
101
+ * @returns the model handle.
102
+ */
103
+ export function makeAgentModel(session, options = {}) {
104
+ const same = (left, right) => left !== null && right !== null
105
+ && left.provider === right.provider && left.model === right.model && left.reasoningEffort === right.reasoningEffort
106
+ const recorded = () => {
107
+ let lastUsed = null
108
+ let pending = null
109
+ for (const event of eventsOf(session)) {
110
+ if (event.type === 'model/selection') pending = { ...event.data }
111
+ if (event.type === 'request/header') {
112
+ const { provider, model, reasoningEffort } = event.data.header.config
113
+ lastUsed = { provider, model, ...(reasoningEffort === undefined ? {} : { reasoningEffort }) }
114
+ if (same(pending, lastUsed)) pending = null
115
+ }
116
+ }
117
+ return pending ?? lastUsed
118
+ }
119
+ return {
120
+ current() {
121
+ const found = recorded()
122
+ if (found !== null) return { ...found }
123
+ const { provider, model, reasoningEffort } = options
124
+ return provider && model ? { provider, model, ...(reasoningEffort === undefined ? {} : { reasoningEffort }) } : undefined
125
+ },
126
+ select(selection) {
127
+ const next = { provider: selection.provider, model: selection.model, ...(selection.reasoningEffort === undefined ? {} : { reasoningEffort: selection.reasoningEffort }) }
128
+ if (same(recorded(), next)) return
129
+ session.append('model/selection', next)
130
+ },
131
+ }
132
+ }