@flytedesk/app-kit 16.2.0 → 16.4.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.
@@ -1,4 +1,4 @@
1
- import type { TerminalStatus } from "@flytedesk/flytebot-contracts";
1
+ import { type DispatchEnd } from "./constants.js";
2
2
  import { type OwedReply } from "./state.js";
3
3
  /** The raw-SQL half of a Prisma client, or of one of its interactive transactions. */
4
4
  export interface OwedReplyTransaction {
@@ -27,31 +27,168 @@ export interface ReceiveMessageInput {
27
27
  conversationRef: string;
28
28
  personRef: string;
29
29
  }
30
- export interface WriteReplyInput {
30
+ /**
31
+ * What an app's `accept` answers for a reply that carries its own fields: the payload to store
32
+ * with the reply (any JSON value; `null` or `undefined` stores none), or a refusal the model reads as a tool
33
+ * error (`retryable` as for `ChatToolCallResult`).
34
+ */
35
+ export type AcceptReplyResult = {
36
+ ok: true;
37
+ payload: unknown;
38
+ } | {
39
+ ok: false;
40
+ message: string;
41
+ retryable: boolean;
42
+ };
43
+ /**
44
+ * An app's validation of its reply fields. It runs in the write's transaction under the owed
45
+ * reply's row lock, before the write, so what it writes through `tx` commits or fails with the
46
+ * reply. It does not run for a resend of a reply already written.
47
+ */
48
+ export type AcceptReply<Tx extends OwedReplyTransaction> = (tx: Tx, input: {
49
+ owedReply: OwedReply;
50
+ text: string;
51
+ fields: Record<string, unknown>;
52
+ }) => Promise<AcceptReplyResult>;
53
+ export interface WriteReplyInput<Tx extends OwedReplyTransaction = OwedReplyTransaction> {
31
54
  /** The dispatch id of the run writing: the Idempotency-Key the app sent in `runs.create`. */
32
55
  dispatchId: string;
33
56
  /** The owed reply written to. */
34
57
  to: string;
35
58
  phase: "ack" | "reply";
36
59
  text: string;
60
+ /** The app's reply fields and its validation of them; only with phase `reply`. */
61
+ fields?: {
62
+ values: Record<string, unknown>;
63
+ accept: AcceptReply<Tx>;
64
+ };
37
65
  }
38
66
  export type WriteReplyResult =
39
- /** `owed` counts the run's owed replies still unanswered after this write. */
67
+ /** `owed` counts the run's owed replies still unanswered after this write, `unheld` those no hold holds. */
40
68
  {
41
69
  ok: true;
42
70
  owed: number;
71
+ unheld: number;
72
+ } | {
73
+ ok: false;
74
+ message: string;
75
+ retryable: boolean;
76
+ };
77
+ export interface HoldInput {
78
+ /** The dispatch id of the run the background job is started in. */
79
+ dispatchId: string;
80
+ /** The owed reply the job is for; left out, the run's only unanswered one. */
81
+ to?: string;
82
+ /** The app's own id for the job; `endHold` takes it. */
83
+ holdRef: string;
84
+ }
85
+ export type HoldResult = {
86
+ ok: true;
87
+ owedReplyId: string;
88
+ } | {
89
+ ok: false;
90
+ message: string;
91
+ };
92
+ /** The result of ending a dispatch: what is waiting for a run, and the jobs the app must stop. */
93
+ export interface SettleResult {
94
+ /** The conversation's remaining `sent` owed replies; when there are any, dispatch the next run for them. */
95
+ waiting: OwedReply[];
96
+ /**
97
+ * The conversation's released held owed replies (acked, no open hold, an ended hold not yet reported):
98
+ * when nothing is `waiting` and no run is live, `claimWake` them.
99
+ */
100
+ released: OwedReply[];
101
+ /** Holds of failed owed replies that were still open: cancel each job. */
102
+ cancelHolds: string[];
103
+ }
104
+ export interface StopResult {
105
+ stopped: OwedReply[];
106
+ /** Holds of stopped owed replies that were still open: cancel each job. */
107
+ cancelHolds: string[];
108
+ }
109
+ export interface ClaimedWake {
110
+ /** The owed replies bound to the wake dispatch, oldest first. */
111
+ replies: OwedReply[];
112
+ /** The holds this wake reports to its run (the jobs whose outcomes its task tells), by owed reply. */
113
+ holds: {
114
+ holdRef: string;
115
+ owedReplyId: string;
116
+ }[];
117
+ }
118
+ /** `ok` also when the message has no owed reply (nothing to discard); a refusal is a fixed line. */
119
+ export type DiscardResult = {
120
+ ok: true;
43
121
  } | {
44
122
  ok: false;
45
123
  message: string;
46
124
  };
47
- export interface OwedReplyStore {
125
+ export interface DeleteConversationResult {
126
+ /** Holds of the deleted owed replies whose job was still open: cancel each job. */
127
+ cancelHolds: string[];
128
+ }
129
+ export interface OwedReplyStore<Tx extends OwedReplyTransaction = OwedReplyTransaction> {
48
130
  receiveMessage(input: ReceiveMessageInput): Promise<OwedReply>;
49
131
  claimForDispatch(conversationRef: string, dispatchId: string): Promise<OwedReply[]>;
50
- writeReply(input: WriteReplyInput): Promise<WriteReplyResult>;
51
- settle(dispatchId: string, run: {
52
- status: TerminalStatus;
53
- }): Promise<OwedReply[]>;
132
+ writeReply(input: WriteReplyInput<Tx>): Promise<WriteReplyResult>;
133
+ /**
134
+ * Binds a background job to an acknowledged, unanswered owed reply of the run, in the app's
135
+ * own transaction `tx` (so the job row and the hold commit together; `tx` must be READ COMMITTED, as for
136
+ * `discard`). Refused with a fixed line otherwise. The same `holdRef` for the same owed reply answers ok
137
+ * and changes nothing.
138
+ */
139
+ hold(tx: OwedReplyTransaction, input: HoldInput): Promise<HoldResult>;
140
+ /**
141
+ * The job ended. Idempotent; an ended hold changes nothing and an unknown one answers null.
142
+ * Answers the conversation of the held owed reply, so the app can `claimWake` it once its run is over.
143
+ */
144
+ endHold(holdRef: string): Promise<{
145
+ conversationRef: string;
146
+ } | null>;
147
+ /** The run ended: `completed` leaves held owed replies held; any other end fails every unanswered one. */
148
+ settle(dispatchId: string, end: DispatchEnd): Promise<SettleResult>;
149
+ /**
150
+ * Called when the conversation has no live run: binds its released held owed replies to
151
+ * `dispatchId` and reports their holds. Idempotent for one dispatch id.
152
+ */
153
+ claimWake(conversationRef: string, dispatchId: string): Promise<ClaimedWake>;
154
+ /** No wake can run (the asker signed out): fails the conversation's released held owed replies. */
155
+ abandonWake(conversationRef: string): Promise<OwedReply[]>;
156
+ /**
157
+ * Takes back a dispatch whose replies have no write yet (Flytebot answered 409 conversation_busy, or
158
+ * the run ended before any model call): its owed replies are `sent` again, a wake's holds unreported
159
+ * again. False when the dispatch is unknown or any of its replies was written since.
160
+ */
161
+ withdraw(dispatchId: string): Promise<boolean>;
162
+ /** The person's Stop: fails every taken, acked or held unanswered owed reply of the conversation. */
163
+ stop(conversationRef: string): Promise<StopResult>;
54
164
  /** The owed replies of a run that are neither answered nor failed, oldest first. */
55
165
  unansweredOf(dispatchId: string): Promise<OwedReply[]>;
166
+ /**
167
+ * The owed replies of these message refs, oldest first. A ref with no owed reply is left out. Each carries
168
+ * its derived state (held included) and its bindings, read in one statement, so they agree.
169
+ */
170
+ ofMessages(messageRefs: readonly string[]): Promise<OwedReply[]>;
171
+ /** Every owed reply of the conversation, oldest first, as `ofMessages` reads them. */
172
+ ofConversation(conversationRef: string): Promise<OwedReply[]>;
173
+ /** Whether the conversation has an owed reply no run has taken yet (`sent`), which waits for a run. */
174
+ hasWaiting(conversationRef: string): Promise<boolean>;
175
+ /**
176
+ * Deletes the owed reply of a message no dispatch currently holds (one that was never claimed, or whose
177
+ * dispatch was withdrawn), under its row lock, so a `claimForDispatch` racing it can never bind a run to a
178
+ * deleted owed reply. Refused with a fixed line while a dispatch holds it (that run answers or fails it).
179
+ * With `tx`, in the app's own transaction (so the app deletes its chat message with it, and the lock is
180
+ * held until the app commits); otherwise in its own. A `tx` must be READ COMMITTED (Prisma's default):
181
+ * after taking the lock the check reads, in a new statement, a claim that committed meanwhile, which a
182
+ * REPEATABLE READ or SERIALIZABLE snapshot would not see.
183
+ */
184
+ discard(messageRef: string, tx?: OwedReplyTransaction): Promise<DiscardResult>;
185
+ /**
186
+ * Deletes the owed replies of these conversations with their dispatch bindings and holds, in the app's
187
+ * own transaction `tx` (so the app deletes its threads and the replies together). It locks every row once,
188
+ * in id order, so two deletes of several conversations in any order cannot deadlock. Answers the holds
189
+ * whose job was still open. A write racing it either finishes first or finds the owed reply gone. `tx`
190
+ * must be READ COMMITTED, as for `discard`.
191
+ */
192
+ deleteConversation(tx: OwedReplyTransaction, conversationRefs: readonly string[]): Promise<DeleteConversationResult>;
56
193
  }
57
- export declare function createOwedReplyStore<Tx extends OwedReplyTransaction>(options: OwedReplyStoreOptions<Tx>): OwedReplyStore;
194
+ export declare function createOwedReplyStore<Tx extends OwedReplyTransaction>(options: OwedReplyStoreOptions<Tx>): OwedReplyStore<Tx>;