@deepseek-ai/dsh-user-questions 0.2.2-rc.1 → 0.2.2-rc.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.
@@ -0,0 +1,83 @@
1
+ /** Foreground question lifetime and Client claims; durable question state stays in the projection. */
2
+
3
+ /** One live foreground wait, counted by the Host only while no answer UI holds it. */
4
+ export class TimedQuestionWait {
5
+ private readonly controller = new AbortController()
6
+ private readonly completion = Promise.withResolvers<void>()
7
+ private readonly claims = new Set<() => void>()
8
+ private timer: ReturnType<typeof setTimeout> | undefined
9
+
10
+ /**
11
+ * @param deadline - Host-clock deadline used while no Client holds the wait.
12
+ * @param parent - Calling Turn's cancellation signal.
13
+ * @param timeout - Business error used to settle an unattended wait.
14
+ */
15
+ constructor(
16
+ readonly deadline: number,
17
+ private readonly parent: AbortSignal | undefined,
18
+ private readonly timeout: Error,
19
+ ) {
20
+ parent?.addEventListener('abort', this.parentAborted, { once: true })
21
+ if (parent?.aborted === true) this.parentAborted()
22
+ else this.schedule()
23
+ }
24
+
25
+ /** Cancellation shared with the foreground waterfall, not with the calling Turn. */
26
+ get signal(): AbortSignal { return this.controller.signal }
27
+
28
+ /** Settles when the wait is cancelled, expires, or is disposed. */
29
+ get done(): Promise<void> { return this.completion.promise }
30
+
31
+ private readonly parentAborted = (): void => { this.close(this.parent?.reason) }
32
+
33
+ private schedule(): void {
34
+ clearTimeout(this.timer)
35
+ this.timer = undefined
36
+ if (this.signal.aborted || this.claims.size > 0) return
37
+ this.timer = setTimeout(() => { this.close(this.timeout) }, Math.max(0, this.deadline - Date.now()))
38
+ }
39
+
40
+ /**
41
+ * Hold the wait for one answer UI until its stream closes or the question settles.
42
+ * @param signal - This business stream's cancellation lifetime.
43
+ * @returns One remaining-duration frame; completion releases the claim.
44
+ */
45
+ async *attach(signal: AbortSignal): AsyncIterable<{ remainingMs: number }> {
46
+ if (signal.aborted || this.signal.aborted) return
47
+ if (this.claims.size === 0 && Date.now() >= this.deadline) {
48
+ this.close(this.timeout)
49
+ return
50
+ }
51
+ const ended = Promise.withResolvers<void>()
52
+ const release = (): void => {
53
+ if (!this.claims.delete(release)) return
54
+ signal.removeEventListener('abort', release)
55
+ this.signal.removeEventListener('abort', release)
56
+ ended.resolve()
57
+ this.schedule()
58
+ }
59
+ this.claims.add(release)
60
+ signal.addEventListener('abort', release, { once: true })
61
+ this.signal.addEventListener('abort', release, { once: true })
62
+ this.schedule()
63
+ try {
64
+ yield { remainingMs: Math.max(0, this.deadline - Date.now()) }
65
+ await ended.promise
66
+ } finally {
67
+ release()
68
+ }
69
+ }
70
+
71
+ /**
72
+ * Release timers, parent cancellation, and all Client claims.
73
+ * @param reason - Cancellation delivered to the foreground waterfall.
74
+ */
75
+ close(reason: unknown): void {
76
+ if (this.signal.aborted) return
77
+ clearTimeout(this.timer)
78
+ this.timer = undefined
79
+ this.parent?.removeEventListener('abort', this.parentAborted)
80
+ this.controller.abort(reason)
81
+ this.completion.resolve()
82
+ }
83
+ }
package/src/types.ts ADDED
@@ -0,0 +1,180 @@
1
+ /** Client-safe question, answer, and event types. @module @deepseek-ai/dsh-user-questions/types */
2
+
3
+ import type { Scoped } from '@deepseek-ai/dsh-scope'
4
+ import type { Agent } from '@deepseek-ai/dsh-agent/types'
5
+ import type { ToolCallId } from '@deepseek-ai/dsh-llm/brand'
6
+ import type {} from '@deepseek-ai/dsh-session/types'
7
+ import type {} from '@deepseek-ai/dsh-session-projection/types'
8
+
9
+ /** One selectable answer offered to the user. */
10
+ export interface AskUserQuestionOption {
11
+ /** User-facing label. */
12
+ label: string
13
+ /** Optional extra context rendered by capable UIs. */
14
+ description?: string
15
+ }
16
+
17
+ /**
18
+ * A caller-declared presentation intent: the question IS this kind of
19
+ * decision, so a UI that recognises the tag may present it as such instead of as a
20
+ * generic option list. Tagged so further intents can be added; a UI that does
21
+ * not know a tag renders the generic flow, and the answer encoding is identical
22
+ * either way — an intent changes presentation only, never the protocol.
23
+ */
24
+ export type AskUserQuestionIntent = {
25
+ /** A plan submitted for review: `detail` is the plan markdown `ask()` requires, and the decision approves or declines it. */
26
+ kind: 'plan-review'
27
+ /**
28
+ * The option label that approves the plan; every other option declines it.
29
+ * Named rather than positional so no UI infers the verdict from option order.
30
+ * An `approve` naming no option of its own question is rejected at `ask()`.
31
+ */
32
+ approve: string
33
+ /** Logged tool invocation whose arguments contain the reviewed plan. */
34
+ callId?: ToolCallId
35
+ } | {
36
+ /** A blocking permission decision with fixed actions and no free-text answer. */
37
+ kind: 'approval'
38
+ /** Option label granting the scope described by the caller. */
39
+ approve: string
40
+ /** Option label refusing the requested access. */
41
+ reject: string
42
+ /** Optional persistent grant offered only when the caller supports it. */
43
+ always?: string
44
+ /** Metadata for human recognition; the caller admits icon sources, which the UI may load as URLs. Never an authorization identity. */
45
+ subject?: { name: string; icon?: string }
46
+ /** Localized question and limited-grant label; fixed cards own reject/always wording. Option labels remain answer values. */
47
+ display?: {
48
+ question: { en: string; [locale: string]: string }
49
+ detail?: { en: string; [locale: string]: string }
50
+ approve?: { en: string; [locale: string]: string }
51
+ }
52
+ }
53
+
54
+ /** One question in a user-questions request. */
55
+ export interface AskUserQuestionItem {
56
+ /** Stable caller-provided question id, echoed in the answer. */
57
+ id: string
58
+ /** The question to display. */
59
+ question: string
60
+ /** Optional supporting detail rendered with the question but kept out of option labels. */
61
+ detail?: string
62
+ /** Optional short heading/group label. */
63
+ header?: string
64
+ /** Optional choices the UI can render as a menu. */
65
+ options?: AskUserQuestionOption[]
66
+ /** Whether more than one option may be selected. Defaults to single-select. */
67
+ multiSelect?: boolean
68
+ /** Optional presentation intent for capable UIs; absent asks for the generic option list. */
69
+ intent?: AskUserQuestionIntent
70
+ }
71
+
72
+ /** Answer to one question. */
73
+ export interface AskUserQuestionAnswerItem {
74
+ /** The answered question id. */
75
+ id: string
76
+ /** Selected option labels. May accompany custom text for a multi-select question. */
77
+ selected: string[]
78
+ /** Optional free-text "Other" answer. */
79
+ custom?: string
80
+ }
81
+
82
+ /** The human's answer. */
83
+ export interface AskUserQuestionAnswer {
84
+ /** Structured answers keyed by question id. */
85
+ answers: AskUserQuestionAnswerItem[]
86
+ }
87
+
88
+ /** `open` while the tool call may still return the answer; `continued` once the answer can only arrive as a new turn. */
89
+ export type UserQuestionState = 'open' | 'continued'
90
+
91
+ /** One unanswered timed `ask_user_question` call reconstructed from the Session log. */
92
+ export interface PendingUserQuestion {
93
+ readonly callId: ToolCallId
94
+ readonly questions: readonly AskUserQuestionItem[]
95
+ readonly state: UserQuestionState
96
+ }
97
+
98
+ /**
99
+ * One timed `ask_user_question` call and the answers it settled with: the
100
+ * batch its own result carried when the user answered inside the window,
101
+ * otherwise the batch its late reply carried, because that call's own result
102
+ * recorded the timeout. A transcript row reads what the user finally answered
103
+ * from here, and only a call listed here was a timed one.
104
+ */
105
+ export interface SettledUserQuestion {
106
+ /** The settled call. */
107
+ readonly callId: ToolCallId
108
+ /** The recorded batch, one entry per question; empty when the late reply carried none. */
109
+ readonly answers: readonly AskUserQuestionAnswerItem[]
110
+ }
111
+
112
+ /**
113
+ * Both halves of one Session's timed `ask_user_question` state, as every
114
+ * Client reads them. A call made while the blocking legacy tool was in
115
+ * effect appears in neither half.
116
+ */
117
+ export interface UserQuestionProjectionView {
118
+ /** Calls that can still take an answer, in ask order. */
119
+ readonly active: readonly PendingUserQuestion[]
120
+ /** Calls an answer settled, in settlement order. */
121
+ readonly settled: readonly SettledUserQuestion[]
122
+ }
123
+
124
+ declare module '@deepseek-ai/dsh-llm' {
125
+ interface MessageSourceMap {
126
+ /**
127
+ * Late reply to a continued `ask_user_question` call, steered into the
128
+ * agent by `dsh-user-questions` as an answer batch. Closing a question
129
+ * panel persists no reply. Readers preserve this message without the
130
+ * producer; only the `userQuestions` projection reads the kind to close
131
+ * the question and record its answers.
132
+ * @persistenceAttribution
133
+ */
134
+ 'user-question-reply': { kind: 'user-question-reply'; callId: ToolCallId; outcome: 'answered' }
135
+ }
136
+ }
137
+
138
+ declare module '@deepseek-ai/dsh-session-projection/types' {
139
+ interface SessionProjectionMap {
140
+ /** Timed questions that remain answerable in this Session, and the ones a late reply settled. */
141
+ userQuestions: UserQuestionProjectionView
142
+ }
143
+ }
144
+
145
+ /** Client-safe payload declared for the user-question answerer waterfall. */
146
+ export interface AskUserQuestionRequestEvent {
147
+ /** Questions to display. */
148
+ questions: AskUserQuestionItem[]
149
+ /** Agent identity projected to the corresponding Client Context in transit. */
150
+ agent?: Agent
151
+ /** Cancellation lifetime of the pending request. */
152
+ signal?: AbortSignal
153
+ /**
154
+ * Tool call the Client card is keyed by. Timed answerers attach to the
155
+ * business wait stream before starting their local countdown.
156
+ */
157
+ wait?: {
158
+ /** Tool call the Client card is keyed by. */
159
+ callId: ToolCallId
160
+ /** True for a foreground wait that requires a Client claim; absent for indefinite waits. */
161
+ timed?: boolean
162
+ }
163
+ }
164
+
165
+ declare module '@deepseek-ai/cordis' {
166
+ interface Events {
167
+ /**
168
+ * Ask composed answerers for structured user input. Return an answer to
169
+ * claim the request or call `next()` to delegate. Scope-filtered dispatch
170
+ * (`@deepseek-ai/dsh-scope`): agent-scoped listeners receive only that agent.
171
+ * @param request - pending user-question request.
172
+ * @mode waterfall
173
+ */
174
+ 'user-questions/request'(
175
+ this: Scoped<Agent>,
176
+ request: AskUserQuestionRequestEvent,
177
+ next: () => Promise<AskUserQuestionAnswer>,
178
+ ): Promise<AskUserQuestionAnswer>
179
+ }
180
+ }