@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.
package/README.md CHANGED
@@ -1064,6 +1064,10 @@ fake.setProfile({ ...summary, reply: { server: "sms-app-chat", tool: "reply" } }
1064
1064
  const call = (phase, text) => ({ server: "sms-app-chat", tool: "reply", arguments: { to: owedReplyId, phase, text } });
1065
1065
  fake.scriptRun({ toolCalls: [call("ack", "On it."), call("reply", "Done.")] }); // answers, then ends
1066
1066
  fake.scriptRun({ toolCalls: [call("ack", "On it.")] }); // acknowledges, then ends with the reply still owed
1067
+ // An ack, then an app tool that holds the reply, then the run completes with the reply held; a later run is the wake that answers it:
1068
+ fake.scriptRun({ toolCalls: [call("ack", "On it."), { server: "sms-app-chat", tool: "start_query", arguments: {} }] });
1069
+ // A reply whose fields your accept refuses is a tool error in the run's events, and the reply stays owed:
1070
+ fake.scriptRun({ toolCalls: [{ ...call("reply", "Here."), arguments: { to: owedReplyId, phase: "reply", text: "Here.", proposal: badProposal } }] });
1067
1071
  fake.scriptRun({}); // calls nothing
1068
1072
  await fake.scriptsSettled(); // every scripted call sequence has ended its run
1069
1073
  ```
@@ -1114,15 +1118,17 @@ without one, the owed reply fails with a fixed line the person sees. It replaces
1114
1118
  progress/final flow; an app that adopts it removes its `createReplyTool` use in the same change
1115
1119
  (`createReplyTool`, `parseReplyArguments` and `ChatReplyPayload` stay until every app has moved).
1116
1120
 
1117
- **1. Install the tables.** Run `npx owed-reply-sync init`. It installs the fragment's two models
1118
- (`OwedReply`, `OwedReplyDispatch`, tables `app_kit_owed_reply` and `app_kit_owed_reply_dispatch`) into your
1121
+ **1. Install the tables.** Run `npx owed-reply-sync init`. It installs the fragment's three models
1122
+ (`OwedReply`, `OwedReplyDispatch`, `OwedReplyHold`, tables `app_kit_owed_reply`, `app_kit_owed_reply_dispatch` and
1123
+ `app_kit_owed_reply_hold`) into your
1119
1124
  `schema.prisma`. It follows `auth-sync`'s Prisma-migrate mode, because every app that adopts the flow
1120
1125
  deploys with Prisma migrate: it writes no SQL and runs none. Create the tables with
1121
1126
  `npx prisma migrate dev --create-only`, append the CHECK statements `init` prints (Prisma's schema language
1122
1127
  has none), keep the migration one atomic `DO $$ ... $$` block with `SET LOCAL lock_timeout` if it has more
1123
1128
  than one statement, then apply it. An app that does not use Prisma migrate gets the raw SQL of
1124
- `0006_app_kit_owed_reply_init` run for it. `npx owed-reply-sync apply` brings an installed fragment up to
1125
- date.
1129
+ `0006_app_kit_owed_reply_init` and `0007_app_kit_owed_reply_holds` run for it. `npx owed-reply-sync apply`
1130
+ brings an installed fragment up to date: an app locked at fragment 1.0.0 is shown the SQL of 0007 (the
1131
+ `reply_payload` column and the hold table), to put in its own `prisma migrate dev` migration as one atomic block.
1126
1132
 
1127
1133
  **2. Make the store.** The store reads and writes through your own generated Prisma client (a structural
1128
1134
  type: tagged-template `$queryRaw`/`$executeRaw` and an interactive `$transaction`; no new dependency):
@@ -1138,8 +1144,9 @@ const owedReplies = createOwedReplyStore({
1138
1144
  ```
1139
1145
 
1140
1146
  An owed reply's `state` is derived on every read, never stored: `sent` (no run has taken it), `taken`,
1141
- `acked`, `answered` or `failed`. `shownText(reply)` is what the person's chat shows: the reply, else the
1142
- failure line, else the acknowledgement.
1147
+ `acked`, `answered` or `failed`. `held` is a flag on an unanswered one (see "Held replies"), and `replyPayload`
1148
+ is what the reply's own fields carried (see "Reply fields"). `shownText(reply)` is what the person's chat
1149
+ shows: the reply, else the failure line, else the acknowledgement.
1143
1150
 
1144
1151
  **3. Use its three operations around a run.**
1145
1152
 
@@ -1150,12 +1157,53 @@ failure line, else the acknowledgement.
1150
1157
  dispatch and returns them (give their ids to the agent in its task, as the `to` of its replies).
1151
1158
  Idempotent for the same dispatch id, so a retried request or a redelivered webhook never makes a second
1152
1159
  binding. A message that arrives while the run works stays `sent` for the next run.
1153
- - `settle(dispatchId, run)` from your terminal webhook route (`verifyTerminalWebhook` gives the run).
1154
- Every owed reply the run left unanswered fails with one fixed line from `FAILURE_LINES`:
1155
- `Stopped.` when the run was cancelled, `Flytebot finished without answering.` when it completed with
1156
- no reply, and one line each for `error`, `timeout` and `crash`. It is idempotent: an answered or failed
1157
- owed reply is untouched. It returns the conversation's remaining `sent` owed replies; when there are any,
1158
- dispatch the next run for them.
1160
+ - `settle(dispatchId, end)` from your terminal webhook route (`verifyTerminalWebhook` gives the run;
1161
+ pass its `status`), and when Flytebot refused to create the run or the create never answered (`end` is
1162
+ then `not_started`). Every owed reply the dispatch left unanswered fails with one fixed line from
1163
+ `FAILURE_LINES`: `Stopped.` when the run was cancelled, `Flytebot finished without answering.` when it
1164
+ completed with no reply, one line each for `error`, `timeout`, `crash` and `not_started`. A run that
1165
+ `completed` leaves an owed reply a background job holds (see "Held replies"); any other end fails
1166
+ held ones too and ends their holds. It is idempotent: an answered or failed owed reply is untouched, and
1167
+ an owed reply a later dispatch (a wake) took is that run's, so a terminal redelivered for the earlier run
1168
+ changes nothing. It returns `{ waiting, released, cancelHolds }`: the conversation's remaining `sent` owed
1169
+ replies, its released held owed replies (see "Held replies"), and the hold refs of the failed owed replies
1170
+ whose job was still open, for you to cancel.
1171
+
1172
+ **An app never reads or writes the fragment tables directly.** Its own queries copy the store's derivation (and
1173
+ drift from it), and a delete takes no row lock. The store has the operations an app needs around its own chat
1174
+ messages:
1175
+
1176
+ - `ofMessages(messageRefs)` reads the owed replies of those messages (a ref with none is left out), and
1177
+ `ofConversation(conversationRef)` every owed reply of a conversation. Each carries its derived `state` (`held`
1178
+ included, the one derivation every read uses) and its `bindings` (`{ dispatchId, boundAt }`, oldest first, one per
1179
+ dispatch that took it), with `latestDispatchId` and `latestBoundAt` the last of them (`null` while no run has
1180
+ taken it). The reply your `project` callback receives carries them too, so it never reads the tables. A reply
1181
+ and its bindings come from one statement, so they agree.
1182
+ - `hasWaiting(conversationRef)` answers whether the conversation has a `sent` owed reply, one that waits for a run.
1183
+ - `discard(messageRef, tx?)` deletes the owed reply of a message no dispatch currently holds (never claimed, or its
1184
+ dispatch was withdrawn), under its row lock.
1185
+ It answers `{ ok: true }` (also when the message has no owed reply) or `{ ok: false, message }` with
1186
+ `DISCARD_REFUSALS.taken` while a dispatch holds it: that run answers or fails it, so `discard` never races
1187
+ `claimForDispatch` into a run whose task names a deleted owed reply. Pass your own transaction as `tx` to
1188
+ delete your chat message with it, and the lock is held until you commit. Delete your message only on `ok`.
1189
+ - `deleteConversation(tx, conversationRefs)` deletes the owed replies of those conversations with their dispatch
1190
+ bindings and holds, inside your transaction, so you delete threads and their owed replies together. It locks all
1191
+ their rows once, in id order, so two multi-thread deletes cannot deadlock. It returns `{ cancelHolds }`, the refs
1192
+ of the holds whose job was still open: cancel each job after the transaction commits.
1193
+
1194
+ The transaction you pass to `discard`, `hold` or `deleteConversation` must be READ COMMITTED (Prisma's default): each
1195
+ takes a row lock, then reads in a new statement a claim that committed while it waited, which a REPEATABLE READ or
1196
+ SERIALIZABLE snapshot would not see.
1197
+
1198
+ **What to do after `settle`**, in this order, once the run is over (so no run is live for the conversation):
1199
+
1200
+ 1. `waiting` is not empty: dispatch the next run for them (`claimForDispatch`, then `runs.create`).
1201
+ 2. Otherwise `released` is not empty: `claimWake(conversationRef, newDispatchId)` and start the wake run.
1202
+ 3. Otherwise nothing is due.
1203
+
1204
+ A message that arrived while the run worked is answered before a wake, and a wake's run never overlaps another
1205
+ run of the conversation. Do the same after `endHold` when no run is live (`endHold` answers the conversation).
1206
+ Cancel every ref in `cancelHolds`.
1159
1207
 
1160
1208
  `settle` and a late `reply` take the same row lock (`SELECT ... FOR UPDATE`) on the owed reply: a reply to
1161
1209
  a failed owed reply is refused with a tool error, and a `settle` after an answer changes nothing.
@@ -1181,15 +1229,82 @@ await app.register(
1181
1229
  `reply` refuses a `to` that is not an owed reply of the run's own dispatch, a reply to a failed owed
1182
1230
  reply, and a second, different text for an answered one. Resending the same phase with the same text
1183
1231
  answers ok and changes nothing; an ack is allowed until the owed reply is answered, and a second ack
1184
- replaces the first. Each result carries `_meta["flytebot/reply"] = { owed, unheld, reminder }` (an app
1185
- holds no background jobs, so `unheld` equals `owed`). Name the tool in your Flytebot profile
1186
- (`reply: { server: "sms-app-chat", tool: "reply" }`): its runner then ends the run once `owed` is 0.
1187
- `createRemoteMcpPlugin`'s new `resultNotice(context, call: { tool, ok })` option is what adds the line; a plugin without it
1188
- returns results exactly as before. The fixed lines (the failure lines, the reminder, the owed-replies line,
1232
+ replaces the first. Every successful result of the endpoint, your own tools' included, carries
1233
+ `_meta["flytebot/reply"] = { owed, unheld, reminder }`: `owed` counts the run's unanswered owed replies,
1234
+ `unheld` those no background job holds, and the reminder names the held ones. Name the tool in your Flytebot
1235
+ profile (`reply: { server: "sms-app-chat", tool: "reply" }`): its runner reads the state from any tool of the
1236
+ server, ends the run once `owed` is 0, and blocks the model's first stop while `unheld` is above 0.
1237
+ `createRemoteMcpPlugin`'s `annotateResult(context, call: { tool, ok })` option is what adds both: one call
1238
+ answers `{ notice, meta }`, so the line and the `_meta` describe one moment, and a throw fails the request
1239
+ instead of sending a result without them. A plugin without it returns results exactly as before. The fixed lines (the failure lines, the reminder, the owed-replies line,
1189
1240
  the refusals) are one module, `constants.ts`, exported from the subpath.
1190
1241
 
1191
- **5. Test it.** `createFakeFlytebot` scripts a run that acknowledges and replies through your real
1192
- endpoint; see "Testing against the fake".
1242
+ **5. Reply fields.** An app whose replies carry more than text (an audience proposal, a plan packet) gives
1243
+ `createOwedReplyPlugin` (or `createOwedReplyTool`) `replyFields: { properties, accept }`. The `properties` (JSON
1244
+ Schema properties, never `to`, `phase` or `text`) join the tool's input, allowed with phase `reply` only: fields
1245
+ on an acknowledgement are refused. `accept(tx, { owedReply, text, fields })` validates the packet, in the write's
1246
+ transaction under the owed reply's row lock, before the write (what it writes through `tx` commits or rolls back
1247
+ with the reply), and answers `{ ok: true, payload }` or `{ ok: false, message, retryable }`. A refusal writes
1248
+ nothing and comes back as a tool error. The `payload` (any JSON value; `null` stores none) is written with the
1249
+ reply to `reply_payload` (a CHECK keeps it from existing without `reply_text`) and reaches `project` as
1250
+ `reply.replyPayload`. Resending the same reply (same text) answers ok, changes nothing and does not run `accept`
1251
+ again. Without `replyFields` the tool takes `{ to, phase, text }` only.
1252
+
1253
+ ```ts
1254
+ createOwedReplyPlugin({
1255
+ // ...
1256
+ replyFields: {
1257
+ properties: { proposal: { type: "object", description: "The audience proposal." } },
1258
+ accept: async (tx, { fields }) => {
1259
+ const proposal = parseProposal(fields.proposal);
1260
+ return proposal === null
1261
+ ? { ok: false, message: "The proposal has no audience.", retryable: false }
1262
+ : { ok: true, payload: await saveProposal(tx, proposal) };
1263
+ },
1264
+ },
1265
+ });
1266
+ ```
1267
+
1268
+ **6. Held replies.** An agent that acknowledges, starts a background job (your own tool) and stops leaves its
1269
+ owed reply *held* instead of failing it, and a later wake run answers it. This is Flytebot's own rule.
1270
+
1271
+ - `hold(tx, { dispatchId, to?, holdRef })` from your tool, in the same transaction that inserts the job.
1272
+ It binds the job to an acknowledged, unanswered owed reply of the run (`to`, else its only unanswered one).
1273
+ It refuses with a fixed line (`HOLD_REFUSALS`, `REFUSALS`) when the run has none, several and no `to`, an
1274
+ owed reply that is not the run's, not yet acknowledged, answered or failed, or a `holdRef` already on
1275
+ another owed reply. The same `holdRef` for the same owed reply answers ok and changes nothing.
1276
+ - `endHold(holdRef)` when the job ends, whatever its outcome. Idempotent. It answers the owed reply's
1277
+ conversation (null for an unknown ref), so you can `claimWake` once its run is over.
1278
+ - An owed reply is held while it has an open hold or an ended hold not yet reported to a wake. A held one is
1279
+ acked, so `claimForDispatch` never takes it; a run that completes leaves it (`settle(..., "completed")`).
1280
+ - `claimWake(conversationRef, dispatchId)`, called when the conversation has no live run (after `settle`, and
1281
+ after `endHold`): binds the conversation's *released* held owed replies (acked, unanswered, no open hold, an
1282
+ ended hold not yet reported) to the new dispatch and marks those holds reported in the same transaction. It
1283
+ returns `{ replies, holds }`: the owed replies and the jobs whose outcomes the wake's task tells the agent. It is
1284
+ idempotent for one dispatch id. Start the wake run with that dispatch id as its `idempotencyKey`, as for any
1285
+ run; the agent answers through `reply`.
1286
+ - `abandonWake(conversationRef)` when no wake can run (the asker signed out): fails the released held owed
1287
+ replies with `SIGNED_OUT_LINE` and returns them.
1288
+
1289
+ `endHold` and `claimWake` take the same row lock, so a wake never misses a job that has just ended: whichever
1290
+ comes second sees the other. A claim that raced ahead of the end finds the job open; call `claimWake` again
1291
+ after `endHold`.
1292
+
1293
+ **7. Withdraw and stop.**
1294
+
1295
+ - `withdraw(dispatchId)` takes back a dispatch whose run Flytebot would not start (a `409 conversation_busy`) or
1296
+ that ended before any model call (`session_missing`): its owed replies are `sent` again (the running run's
1297
+ terminal dispatches them), and a wake's holds are untold again. It answers false, and changes nothing, for an
1298
+ unknown dispatch and for one whose owed replies were written since it was bound.
1299
+ - `stop(conversationRef)` for the person's Stop: fails every taken, acked or held unanswered owed reply of the
1300
+ conversation with `Stopped.` and returns `{ stopped, cancelHolds }`. Cancel the run and each hold ref's job; the
1301
+ run's later `settle` changes nothing.
1302
+ - `settle(dispatchId, "not_started")` ends a dispatch whose run Flytebot refused to create (a 4xx) or whose
1303
+ create never answered: every unanswered owed reply fails with `FAILURE_LINES.not_started`.
1304
+
1305
+ **8. Test it.** `createFakeFlytebot` scripts a run that acknowledges and replies through your real
1306
+ endpoint, one that holds a reply with your own tool and then stops, a wake run that answers it, and a reply
1307
+ whose fields your `accept` refuses; see "Testing against the fake".
1193
1308
 
1194
1309
  ## Tests
1195
1310
 
@@ -5,21 +5,47 @@ import type { TerminalStatus } from "@flytedesk/flytebot-contracts";
5
5
  */
6
6
  /** The reply tool's name unless the app names it otherwise, and the key a profile's `reply: {server, tool}` points at. */
7
7
  export declare const DEFAULT_REPLY_TOOL_NAME = "reply";
8
+ /**
9
+ * How a dispatch ended: a run's terminal status, or `not_started` for a run Flytebot refused to
10
+ * create or whose create never answered (AK-85).
11
+ */
12
+ export type DispatchEnd = TerminalStatus | "not_started";
8
13
  /**
9
14
  * What a person sees in place of an answer when the run that took their message ended without
10
15
  * answering it, by how the run ended. A run that completed without calling the reply tool is
11
16
  * `completed`.
12
17
  */
13
- export declare const FAILURE_LINES: Readonly<Record<TerminalStatus, string>>;
14
- /** The reminder a reply result carries under `_meta["flytebot/reply"]` while an answer is still owed. */
15
- export declare function owedReminder(toolName: string, owed: number): string;
18
+ export declare const FAILURE_LINES: Readonly<Record<DispatchEnd, string>>;
19
+ /** What an owed reply says when its asker's session ended before the wake that answers it could run. */
20
+ export declare const SIGNED_OUT_LINE = "You signed out before the answer was ready; ask again.";
21
+ /**
22
+ * The reminder a reply result carries under `_meta["flytebot/reply"]` while an answer is still owed.
23
+ * `unheld` of the `owed` replies are the ones no background work holds: the run must answer those; a
24
+ * held one is answered by the wake run when its work finishes.
25
+ */
26
+ export declare function owedReminder(toolName: string, owed: number, unheld: number): string;
16
27
  /** The line that ends every result of an endpoint that names its owed replies; null when none is owed. */
17
- export declare function owedRepliesLine(toolName: string, ids: readonly string[]): string | null;
28
+ export declare function owedRepliesLine(toolName: string, replies: readonly {
29
+ id: string;
30
+ held: boolean;
31
+ }[]): string | null;
18
32
  /** What the model reads after a successful call of the reply tool. */
19
- export declare function deliveredText(toolName: string, phase: "ack" | "reply", owed: number): string;
33
+ export declare function deliveredText(toolName: string, phase: "ack" | "reply", owed: number, unheld: number): string;
20
34
  /** The refusals of the reply tool, as the model reads them. */
21
35
  export declare const REFUSALS: {
22
36
  readonly notOwed: "`to` is not an owed reply of this run.";
23
37
  readonly failed: "That owed reply already ended without an answer, so it takes no more replies.";
24
38
  readonly answered: "That owed reply is already answered.";
39
+ readonly fieldsOnAck: "Reply fields are sent with phase \"reply\" only, not with an acknowledgement.";
40
+ };
41
+ /** The refusal of `discard`, as the app reads it. */
42
+ export declare const DISCARD_REFUSALS: {
43
+ readonly taken: "A run has already taken that message, so it is not discarded.";
44
+ };
45
+ /** The refusals of `hold`, as the app's tool reads them. */
46
+ export declare const HOLD_REFUSALS: {
47
+ readonly nothingToHold: "This run has no unanswered message to hold.";
48
+ readonly ambiguous: "This run has several unanswered messages; say which one the background work is for with `to`.";
49
+ readonly notAcknowledged: "Acknowledge that message first (phase \"ack\"), then start the background work.";
50
+ readonly refInUse: "That hold reference already belongs to another message.";
25
51
  };
@@ -15,31 +15,61 @@ export const FAILURE_LINES = {
15
15
  error: "Flytebot hit an error and stopped.",
16
16
  timeout: "Flytebot ran out of time and stopped.",
17
17
  crash: "Flytebot stopped unexpectedly.",
18
+ not_started: "Flytebot could not start the run.",
18
19
  };
19
- /** The reminder a reply result carries under `_meta["flytebot/reply"]` while an answer is still owed. */
20
- export function owedReminder(toolName, owed) {
21
- return owed === 0
22
- ? "Every reply is answered."
23
- : `You still owe an answer to ${owed} message${owed === 1 ? "" : "s"}. Send each through ${toolName} (phase "reply") before you stop.`;
20
+ /** What an owed reply says when its asker's session ended before the wake that answers it could run. */
21
+ export const SIGNED_OUT_LINE = "You signed out before the answer was ready; ask again.";
22
+ const messages = (count) => `${count} message${count === 1 ? "" : "s"}`;
23
+ const waits = (count) => (count === 1 ? "waits" : "wait");
24
+ /**
25
+ * The reminder a reply result carries under `_meta["flytebot/reply"]` while an answer is still owed.
26
+ * `unheld` of the `owed` replies are the ones no background work holds: the run must answer those; a
27
+ * held one is answered by the wake run when its work finishes.
28
+ */
29
+ export function owedReminder(toolName, owed, unheld) {
30
+ if (owed === 0)
31
+ return "Every reply is answered.";
32
+ const held = owed - unheld;
33
+ if (unheld === 0) {
34
+ return `${messages(held)} ${waits(held)} for the background work you started and will be answered when it finishes. You may stop now.`;
35
+ }
36
+ const answer = `You still owe an answer to ${messages(unheld)}. Send each through ${toolName} (phase "reply") before you stop.`;
37
+ return held === 0 ? answer : `${answer} ${held} more ${held === 1 ? "message waits" : "messages wait"} for the background work you started.`;
24
38
  }
25
39
  /** The line that ends every result of an endpoint that names its owed replies; null when none is owed. */
26
- export function owedRepliesLine(toolName, ids) {
27
- return ids.length === 0
28
- ? null
29
- : `Owed replies not yet answered: ${ids.join(", ")}. Answer each with ${toolName} (phase "reply").`;
40
+ export function owedRepliesLine(toolName, replies) {
41
+ const open = replies.filter((reply) => !reply.held).map((reply) => reply.id);
42
+ const held = replies.filter((reply) => reply.held).map((reply) => reply.id);
43
+ const parts = [
44
+ ...(open.length === 0 ? [] : [`Owed replies not yet answered: ${open.join(", ")}. Answer each with ${toolName} (phase "reply").`]),
45
+ ...(held.length === 0 ? [] : [`Held for the background work you started, answered when it finishes: ${held.join(", ")}.`]),
46
+ ];
47
+ return parts.length === 0 ? null : parts.join(" ");
30
48
  }
31
49
  /** What the model reads after a successful call of the reply tool. */
32
- export function deliveredText(toolName, phase, owed) {
50
+ export function deliveredText(toolName, phase, owed, unheld) {
33
51
  if (phase === "ack")
34
52
  return "Acknowledged. Continue with the work, then send your reply.";
35
53
  return owed === 0
36
54
  ? "Delivered. Every reply is answered: do not send anything further."
37
- : `Delivered. ${owedReminder(toolName, owed)}`;
55
+ : `Delivered. ${owedReminder(toolName, owed, unheld)}`;
38
56
  }
39
57
  /** The refusals of the reply tool, as the model reads them. */
40
58
  export const REFUSALS = {
41
59
  notOwed: "`to` is not an owed reply of this run.",
42
60
  failed: "That owed reply already ended without an answer, so it takes no more replies.",
43
61
  answered: "That owed reply is already answered.",
62
+ fieldsOnAck: "Reply fields are sent with phase \"reply\" only, not with an acknowledgement.",
63
+ };
64
+ /** The refusal of `discard`, as the app reads it. */
65
+ export const DISCARD_REFUSALS = {
66
+ taken: "A run has already taken that message, so it is not discarded.",
67
+ };
68
+ /** The refusals of `hold`, as the app's tool reads them. */
69
+ export const HOLD_REFUSALS = {
70
+ nothingToHold: "This run has no unanswered message to hold.",
71
+ ambiguous: "This run has several unanswered messages; say which one the background work is for with `to`.",
72
+ notAcknowledged: "Acknowledge that message first (phase \"ack\"), then start the background work.",
73
+ refInUse: "That hold reference already belongs to another message.",
44
74
  };
45
75
  //# sourceMappingURL=constants.js.map
@@ -1 +1 @@
1
- {"version":3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AC;IAC1F,OAAO,IAAI,KAAK,CAAC;QACf,CAAC,CAAC,mEAAmE;QACrE,CAAC,CAAC,cAAc,YAAY,CAAC,QAAQ,EAAE,IAAI,EAAE,MAAM,CAAC,EAAE,CAAC;AAC3D,CAAC;AAED,+DAA+D;AAC/D,MAAM,CAAC,MAAM,QAAQ,GAAG;IACtB,OAAO,EAAE,wCAAwC;IACjD,MAAM,EAAE,+EAA+E;IACvF,QAAQ,EAAE,sCAAsC;IAChD,WAAW,EAAE,+EAA+E;CACpF,CAAC;AAEX,qDAAqD;AACrD,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC9B,KAAK,EAAE,+DAA+D;CAC9D,CAAC;AAEX,4DAA4D;AAC5D,MAAM,CAAC,MAAM,aAAa,GAAG;IAC3B,aAAa,EAAE,6CAA6C;IAC5D,SAAS,EAAE,+FAA+F;IAC1G,eAAe,EAAE,iFAAiF;IAClG,QAAQ,EAAE,yDAAyD;CAC3D,CAAC"}
@@ -4,12 +4,13 @@
4
4
  * through the `reply` MCP tool, first an acknowledgement, then the reply that replaces it, so
5
5
  * a message is never left without an answer. See the README's "Owed replies" section.
6
6
  */
7
- export { DEFAULT_REPLY_TOOL_NAME, FAILURE_LINES, REFUSALS, deliveredText, owedRepliesLine, owedReminder } from "./constants.js";
7
+ export { DEFAULT_REPLY_TOOL_NAME, DISCARD_REFUSALS, FAILURE_LINES, HOLD_REFUSALS, REFUSALS, SIGNED_OUT_LINE, deliveredText, owedRepliesLine, owedReminder, } from "./constants.js";
8
+ export type { DispatchEnd } from "./constants.js";
8
9
  export { shownText } from "./state.js";
9
- export type { OwedReply, OwedReplyState } from "./state.js";
10
+ export type { OwedReply, OwedReplyState, ReplyBinding } from "./state.js";
10
11
  export { createOwedReplyStore } from "./store.js";
11
- export type { OwedReplyClient, OwedReplyStore, OwedReplyStoreOptions, OwedReplyTransaction, ReceiveMessageInput, WriteReplyInput, WriteReplyResult, } from "./store.js";
12
+ export type { AcceptReply, AcceptReplyResult, ClaimedWake, DeleteConversationResult, DiscardResult, HoldInput, HoldResult, OwedReplyClient, OwedReplyStore, OwedReplyStoreOptions, OwedReplyTransaction, ReceiveMessageInput, SettleResult, StopResult, WriteReplyInput, WriteReplyResult, } from "./store.js";
12
13
  export { createOwedReplyTool } from "./tool.js";
13
- export type { OwedReplyToolOptions } from "./tool.js";
14
+ export type { OwedReplyFields, OwedReplyToolOptions } from "./tool.js";
14
15
  export { createOwedReplyPlugin } from "./plugin.js";
15
16
  export type { OwedReplyPluginOptions } from "./plugin.js";
@@ -4,7 +4,7 @@
4
4
  * through the `reply` MCP tool, first an acknowledgement, then the reply that replaces it, so
5
5
  * a message is never left without an answer. See the README's "Owed replies" section.
6
6
  */
7
- export { DEFAULT_REPLY_TOOL_NAME, FAILURE_LINES, REFUSALS, deliveredText, owedRepliesLine, owedReminder } from "./constants.js";
7
+ export { DEFAULT_REPLY_TOOL_NAME, DISCARD_REFUSALS, FAILURE_LINES, HOLD_REFUSALS, REFUSALS, SIGNED_OUT_LINE, deliveredText, owedRepliesLine, owedReminder, } from "./constants.js";
8
8
  export { shownText } from "./state.js";
9
9
  export { createOwedReplyStore } from "./store.js";
10
10
  export { createOwedReplyTool } from "./tool.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/chat/owed-replies/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,uBAAuB,EAAE,aAAa,EAAE,QAAQ,EAAE,aAAa,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAChI,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAUlD,OAAO,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAEhD,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/chat/owed-replies/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EACL,uBAAuB,EACvB,gBAAgB,EAChB,aAAa,EACb,aAAa,EACb,QAAQ,EACR,eAAe,EACf,aAAa,EACb,eAAe,EACf,YAAY,GACb,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAmBlD,OAAO,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAEhD,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC"}
@@ -1,7 +1,9 @@
1
1
  /**
2
2
  * The chat MCP endpoint of an app that answers through the owed-reply flow (AK-80):
3
3
  * `createRemoteMcpPlugin` serving the `reply` tool for the run its bearer belongs to, with
4
- * every result except a successful reply ending in one line that names the owed replies not yet answered.
4
+ * every result except a successful reply ending in one line that names the owed replies not yet
5
+ * answered, and every successful result of the app's own tools carrying the run's owed-reply
6
+ * state under `_meta["flytebot/reply"]` (AK-85), so Flytebot's runner reads it from any tool.
5
7
  *
6
8
  * await app.register(
7
9
  * createOwedReplyPlugin({
@@ -17,9 +19,12 @@
17
19
  import type { FastifyPluginAsync } from "fastify";
18
20
  import { type RemoteMcpPluginOptions } from "../tools/index.js";
19
21
  import type { AppRegisteredTool } from "../types.js";
20
- import type { OwedReplyStore } from "./store.js";
21
- export interface OwedReplyPluginOptions<Context> extends Pick<RemoteMcpPluginOptions<Context>, "serverName" | "serverVersion" | "authenticate"> {
22
- store: OwedReplyStore;
22
+ import type { OwedReplyStore, OwedReplyTransaction } from "./store.js";
23
+ import { type OwedReplyFields } from "./tool.js";
24
+ export interface OwedReplyPluginOptions<Context, Tx extends OwedReplyTransaction = OwedReplyTransaction> extends Pick<RemoteMcpPluginOptions<Context>, "serverName" | "serverVersion" | "authenticate"> {
25
+ store: OwedReplyStore<Tx>;
26
+ /** The app's reply fields (see `OwedReplyFields`); left out, the reply tool takes `{to, phase, text}` only. */
27
+ replyFields?: OwedReplyFields<Tx>;
23
28
  /** The dispatch id of the run a resolved context is: the Idempotency-Key the app sent in `runs.create`. */
24
29
  dispatchId: (context: Context) => string;
25
30
  /** The other tools the run may call, besides the reply tool. */
@@ -28,4 +33,4 @@ export interface OwedReplyPluginOptions<Context> extends Pick<RemoteMcpPluginOpt
28
33
  replyToolName?: string;
29
34
  replyToolDescription?: string;
30
35
  }
31
- export declare function createOwedReplyPlugin<Context>(options: OwedReplyPluginOptions<Context>): FastifyPluginAsync;
36
+ export declare function createOwedReplyPlugin<Context, Tx extends OwedReplyTransaction = OwedReplyTransaction>(options: OwedReplyPluginOptions<Context, Tx>): FastifyPluginAsync;
@@ -1,5 +1,6 @@
1
+ import { REPLY_META_KEY } from "../mcp-protocol.js";
1
2
  import { createRemoteMcpPlugin } from "../tools/index.js";
2
- import { DEFAULT_REPLY_TOOL_NAME, owedRepliesLine } from "./constants.js";
3
+ import { DEFAULT_REPLY_TOOL_NAME, owedReminder, owedRepliesLine } from "./constants.js";
3
4
  import { createOwedReplyTool } from "./tool.js";
4
5
  export function createOwedReplyPlugin(options) {
5
6
  const toolName = options.replyToolName ?? DEFAULT_REPLY_TOOL_NAME;
@@ -10,16 +11,23 @@ export function createOwedReplyPlugin(options) {
10
11
  tools: (context) => [
11
12
  createOwedReplyTool({
12
13
  store: options.store,
14
+ replyFields: options.replyFields,
13
15
  dispatchId: options.dispatchId(context),
14
16
  name: toolName,
15
17
  description: options.replyToolDescription,
16
18
  }),
17
19
  ...(options.tools?.(context) ?? []),
18
20
  ],
19
- // A successful reply result already says how many replies are owed, so it takes no second reminder.
20
- resultNotice: async (context, call) => call.tool === toolName && call.ok
21
- ? null
22
- : owedRepliesLine(toolName, (await options.store.unansweredOf(options.dispatchId(context))).map((reply) => reply.id)),
21
+ // The reply tool's own success reports its state from the write, so it takes neither the line nor a
22
+ // second read. Every other result is annotated from one read, so the line and `_meta` agree.
23
+ annotateResult: async (context, call) => {
24
+ if (call.tool === toolName && call.ok)
25
+ return { notice: null, meta: null };
26
+ const unanswered = await options.store.unansweredOf(options.dispatchId(context));
27
+ const unheld = unanswered.filter((reply) => !reply.held).length;
28
+ const meta = { owed: unanswered.length, unheld, reminder: owedReminder(toolName, unanswered.length, unheld) };
29
+ return { notice: owedRepliesLine(toolName, unanswered), meta: call.ok ? { [REPLY_META_KEY]: meta } : null };
30
+ },
23
31
  });
24
32
  }
25
33
  //# sourceMappingURL=plugin.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../../src/chat/owed-replies/plugin.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,qBAAqB,EAA+B,MAAM,mBAAmB,CAAC;AAEvF,OAAO,EAAE,uBAAuB,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAE1E,OAAO,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAchD,MAAM,UAAU,qBAAqB,CAAU,OAAwC;IACrF,MAAM,QAAQ,GAAG,OAAO,CAAC,aAAa,IAAI,uBAAuB,CAAC;IAClE,OAAO,qBAAqB,CAAU;QACpC,UAAU,EAAE,OAAO,CAAC,UAAU;QAC9B,aAAa,EAAE,OAAO,CAAC,aAAa;QACpC,YAAY,EAAE,OAAO,CAAC,YAAY;QAClC,KAAK,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC;YAClB,mBAAmB,CAAC;gBAClB,KAAK,EAAE,OAAO,CAAC,KAAK;gBACpB,UAAU,EAAE,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC;gBACvC,IAAI,EAAE,QAAQ;gBACd,WAAW,EAAE,OAAO,CAAC,oBAAoB;aAC1C,CAAC;YACF,GAAG,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC;SACpC;QACD,oGAAoG;QACpG,YAAY,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,CACpC,IAAI,CAAC,IAAI,KAAK,QAA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pG,6FAA6F;QAC7F,cAAc,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE;YACtC,IAAI,IAAI,CAAC,IAAI,KAAK,QAAQ,IAAI,IAAI,CAAC,EAAE;gBAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;YAC3E,MAAM,UAAU,GAAG,MAAM,OAAO,CAAC,KAAK,CAAC,YAAY,CAAC,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC;YACjF,MAAM,MAAM,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC;YAChE,MAAM,IAAI,GAAc,EAAE,IAAI,EAAE,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,YAAY,CAAC,QAAQ,EAAE,UAAU,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAAC;YACzH,OAAO,EAAE,MAAM,EAAE,eAAe,CAAC,QAAQ,EAAE,UAAU,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,cAAc,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;QAC9G,CAAC;KACF,CAAC,CAAC;AACL,CAAC"}
@@ -5,8 +5,17 @@
5
5
  * acked the run wrote an acknowledgement
6
6
  * answered the run wrote the reply (final)
7
7
  * failed the run ended without answering; `failure` is the line the person sees
8
+ *
9
+ * Held (AK-85) is not a state but a flag on an unanswered one: a background job the app started
10
+ * for it is still working (an open hold), or ended and not yet told to a wake run (an ended hold
11
+ * not yet reported). A held owed reply is acked, and a run that completes leaves it unanswered.
8
12
  */
9
13
  export type OwedReplyState = "sent" | "taken" | "acked" | "answered" | "failed";
14
+ /** A run (dispatch) an owed reply was bound to, and when. */
15
+ export interface ReplyBinding {
16
+ dispatchId: string;
17
+ boundAt: Date;
18
+ }
10
19
  /** One person's message that is owed an answer, as the store reads it. */
11
20
  export interface OwedReply {
12
21
  id: string;
@@ -17,8 +26,17 @@ export interface OwedReply {
17
26
  ackText: string | null;
18
27
  replyText: string | null;
19
28
  failure: string | null;
29
+ /** What the app's reply fields carried with the reply (`accept`'s payload); null without one. */
30
+ replyPayload: unknown;
31
+ /** A background job holds this owed reply open; see the module comment. */
32
+ held: boolean;
20
33
  /** Raised on every write, so an app's projected chat message can tell a stale write. */
21
34
  revision: number;
35
+ /** Every dispatch that took it, oldest first; empty while no run has (a withdrawn dispatch is gone from it). */
36
+ bindings: ReplyBinding[];
37
+ /** The last of `bindings`: the run now answering it; null while no run has taken it. */
38
+ latestDispatchId: string | null;
39
+ latestBoundAt: Date | null;
22
40
  state: OwedReplyState;
23
41
  createdAt: Date;
24
42
  ackedAt: Date | null;
@@ -36,8 +54,14 @@ export interface OwedReplyRow {
36
54
  ackText: string | null;
37
55
  replyText: string | null;
38
56
  failure: string | null;
57
+ replyPayload: unknown;
58
+ held: boolean;
39
59
  revision: number;
40
- taken: boolean;
60
+ /** Each dispatch bound to it and when (epoch milliseconds), oldest first. */
61
+ bindings: {
62
+ dispatchId: string;
63
+ boundAtMs: number;
64
+ }[];
41
65
  createdAt: Date;
42
66
  ackedAt: Date | null;
43
67
  answeredAt: Date | null;
@@ -9,9 +9,11 @@ function deriveState(row) {
9
9
  return "answered";
10
10
  if (row.ackText !== null)
11
11
  return "acked";
12
- return row.taken ? "taken" : "sent";
12
+ return row.bindings.length > 0 ? "taken" : "sent";
13
13
  }
14
14
  export function owedReplyFromRow(row) {
15
+ const bindings = row.bindings.map(({ dispatchId, boundAtMs }) => ({ dispatchId, boundAt: new Date(boundAtMs) }));
16
+ const latest = bindings.at(-1);
15
17
  return {
16
18
  id: row.id,
17
19
  messageRef: row.messageRef,
@@ -20,7 +22,12 @@ export function owedReplyFromRow(row) {
20
22
  ackText: row.ackText,
21
23
  replyText: row.replyText,
22
24
  failure: row.failure,
25
+ replyPayload: row.replyPayload,
26
+ held: row.held,
23
27
  revision: row.revision,
28
+ bindings,
29
+ latestDispatchId: latest?.dispatchId ?? null,
30
+ latestBoundAt: latest?.boundAt ?? null,
24
31
  state: deriveState(row),
25
32
  createdAt: row.createdAt,
26
33
  ackedAt: row.ackedAt,
@@ -1 +1 @@
1
- {"version":3,"file":"state.js","sourceRoot":"","sources":["../../../src/chat/owed-replies/state.ts"],"names":[],"mappings":"AA6BA,iIAAiI;AACjI,MAAM,UAAU,SAAS,CAAC,KAA2D;IACnF,OAAO,KAAK,CAAC,SAAS,IAAI,KAAK,CAAC,OAAO,IAAI,KAAK,CAAC,OAAO,CAAC;AAC3D,CAAC;AAmBD,SAAS,WAAW,CAAC,GAAiB;IACpC,IAAI,GAAG,CAAC,OAAO,KAAK,IAAI;QAAE,OAAO,QAAQ,CAAC;IAC1C,IAAI,GAAG,CAAC,SAAS,KAAK,IAAI;QAAE,OAAO,UAAU,CAAC;IAC9C,IAAI,GAAG,CAAC,OAAO,KAAK,IAAI;QAAE,OAAO,OAAO,CAAC;IACzC,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC;AACtC,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,GAAiB;IAChD,OAAO;QACL,EAAE,EAAE,GAAG,CAAC,EAAE;QACV,UAAU,EAAE,GAAG,CAAC,UAAU;QAC1B,eAAe,EAAE,GAAG,CAAC,eAAe;QACpC,SAAS,EAAE,GAAG,CAAC,SAAS;QACxB,OAAO,EAAE,GAAG,CAAC,OAAO;QACpB,SAAS,EAAE,GAAG,CAAC,SAAS;QACxB,OAAO,EAAE,GAAG,CAAC,OAAO;QACpB,QAAQ,EAAE,GAAG,CAAC,QAAQ;QACtB,KAAK,EAAE,WAAW,CAAC,GAAG,CAAC;QACvB,SAAS,EAAE,GAAG,CAAC,SAAS;QACxB,OAAO,EAAE,GAAG,CAAC,OAAO;QACpB,U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