@flytedesk/app-kit 16.2.0 → 16.3.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 +108 -19
- package/dist/chat/owed-replies/constants.d.ts +27 -5
- package/dist/chat/owed-replies/constants.js +37 -11
- package/dist/chat/owed-replies/constants.js.map +1 -1
- package/dist/chat/owed-replies/index.d.ts +4 -3
- package/dist/chat/owed-replies/index.js +1 -1
- package/dist/chat/owed-replies/index.js.map +1 -1
- package/dist/chat/owed-replies/plugin.d.ts +10 -5
- package/dist/chat/owed-replies/plugin.js +13 -5
- package/dist/chat/owed-replies/plugin.js.map +1 -1
- package/dist/chat/owed-replies/state.d.ts +10 -0
- package/dist/chat/owed-replies/state.js +2 -0
- package/dist/chat/owed-replies/state.js.map +1 -1
- package/dist/chat/owed-replies/store.d.ts +107 -9
- package/dist/chat/owed-replies/store.js +302 -60
- package/dist/chat/owed-replies/store.js.map +1 -1
- package/dist/chat/owed-replies/tool.d.ts +16 -4
- package/dist/chat/owed-replies/tool.js +22 -6
- package/dist/chat/owed-replies/tool.js.map +1 -1
- package/dist/chat/tools/index.d.ts +15 -9
- package/dist/chat/tools/index.js +11 -13
- package/dist/chat/tools/index.js.map +1 -1
- package/package.json +1 -1
- package/prisma/fragments/owed-reply.meta.json +1 -1
- package/prisma/fragments/owed-reply.prisma +24 -4
- package/prisma/migrations/0007_app_kit_owed_reply_holds/migration.sql +39 -0
- package/prisma/migrations/manifest.json +7 -0
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
|
|
1118
|
-
(`OwedReply`, `OwedReplyDispatch`, tables `app_kit_owed_reply
|
|
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`
|
|
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`. `
|
|
1142
|
-
|
|
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,27 @@ 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,
|
|
1154
|
-
|
|
1155
|
-
`
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
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
|
+
**What to do after `settle`**, in this order, once the run is over (so no run is live for the conversation):
|
|
1173
|
+
|
|
1174
|
+
1. `waiting` is not empty: dispatch the next run for them (`claimForDispatch`, then `runs.create`).
|
|
1175
|
+
2. Otherwise `released` is not empty: `claimWake(conversationRef, newDispatchId)` and start the wake run.
|
|
1176
|
+
3. Otherwise nothing is due.
|
|
1177
|
+
|
|
1178
|
+
A message that arrived while the run worked is answered before a wake, and a wake's run never overlaps another
|
|
1179
|
+
run of the conversation. Do the same after `endHold` when no run is live (`endHold` answers the conversation).
|
|
1180
|
+
Cancel every ref in `cancelHolds`.
|
|
1159
1181
|
|
|
1160
1182
|
`settle` and a late `reply` take the same row lock (`SELECT ... FOR UPDATE`) on the owed reply: a reply to
|
|
1161
1183
|
a failed owed reply is refused with a tool error, and a `settle` after an answer changes nothing.
|
|
@@ -1181,15 +1203,82 @@ await app.register(
|
|
|
1181
1203
|
`reply` refuses a `to` that is not an owed reply of the run's own dispatch, a reply to a failed owed
|
|
1182
1204
|
reply, and a second, different text for an answered one. Resending the same phase with the same text
|
|
1183
1205
|
answers ok and changes nothing; an ack is allowed until the owed reply is answered, and a second ack
|
|
1184
|
-
replaces the first.
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1206
|
+
replaces the first. Every successful result of the endpoint, your own tools' included, carries
|
|
1207
|
+
`_meta["flytebot/reply"] = { owed, unheld, reminder }`: `owed` counts the run's unanswered owed replies,
|
|
1208
|
+
`unheld` those no background job holds, and the reminder names the held ones. Name the tool in your Flytebot
|
|
1209
|
+
profile (`reply: { server: "sms-app-chat", tool: "reply" }`): its runner reads the state from any tool of the
|
|
1210
|
+
server, ends the run once `owed` is 0, and blocks the model's first stop while `unheld` is above 0.
|
|
1211
|
+
`createRemoteMcpPlugin`'s `annotateResult(context, call: { tool, ok })` option is what adds both: one call
|
|
1212
|
+
answers `{ notice, meta }`, so the line and the `_meta` describe one moment, and a throw fails the request
|
|
1213
|
+
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
1214
|
the refusals) are one module, `constants.ts`, exported from the subpath.
|
|
1190
1215
|
|
|
1191
|
-
**5.
|
|
1192
|
-
|
|
1216
|
+
**5. Reply fields.** An app whose replies carry more than text (an audience proposal, a plan packet) gives
|
|
1217
|
+
`createOwedReplyPlugin` (or `createOwedReplyTool`) `replyFields: { properties, accept }`. The `properties` (JSON
|
|
1218
|
+
Schema properties, never `to`, `phase` or `text`) join the tool's input, allowed with phase `reply` only: fields
|
|
1219
|
+
on an acknowledgement are refused. `accept(tx, { owedReply, text, fields })` validates the packet, in the write's
|
|
1220
|
+
transaction under the owed reply's row lock, before the write (what it writes through `tx` commits or rolls back
|
|
1221
|
+
with the reply), and answers `{ ok: true, payload }` or `{ ok: false, message, retryable }`. A refusal writes
|
|
1222
|
+
nothing and comes back as a tool error. The `payload` (any JSON value; `null` stores none) is written with the
|
|
1223
|
+
reply to `reply_payload` (a CHECK keeps it from existing without `reply_text`) and reaches `project` as
|
|
1224
|
+
`reply.replyPayload`. Resending the same reply (same text) answers ok, changes nothing and does not run `accept`
|
|
1225
|
+
again. Without `replyFields` the tool takes `{ to, phase, text }` only.
|
|
1226
|
+
|
|
1227
|
+
```ts
|
|
1228
|
+
createOwedReplyPlugin({
|
|
1229
|
+
// ...
|
|
1230
|
+
replyFields: {
|
|
1231
|
+
properties: { proposal: { type: "object", description: "The audience proposal." } },
|
|
1232
|
+
accept: async (tx, { fields }) => {
|
|
1233
|
+
const proposal = parseProposal(fields.proposal);
|
|
1234
|
+
return proposal === null
|
|
1235
|
+
? { ok: false, message: "The proposal has no audience.", retryable: false }
|
|
1236
|
+
: { ok: true, payload: await saveProposal(tx, proposal) };
|
|
1237
|
+
},
|
|
1238
|
+
},
|
|
1239
|
+
});
|
|
1240
|
+
```
|
|
1241
|
+
|
|
1242
|
+
**6. Held replies.** An agent that acknowledges, starts a background job (your own tool) and stops leaves its
|
|
1243
|
+
owed reply *held* instead of failing it, and a later wake run answers it. This is Flytebot's own rule.
|
|
1244
|
+
|
|
1245
|
+
- `hold(tx, { dispatchId, to?, holdRef })` from your tool, in the same transaction that inserts the job.
|
|
1246
|
+
It binds the job to an acknowledged, unanswered owed reply of the run (`to`, else its only unanswered one).
|
|
1247
|
+
It refuses with a fixed line (`HOLD_REFUSALS`, `REFUSALS`) when the run has none, several and no `to`, an
|
|
1248
|
+
owed reply that is not the run's, not yet acknowledged, answered or failed, or a `holdRef` already on
|
|
1249
|
+
another owed reply. The same `holdRef` for the same owed reply answers ok and changes nothing.
|
|
1250
|
+
- `endHold(holdRef)` when the job ends, whatever its outcome. Idempotent. It answers the owed reply's
|
|
1251
|
+
conversation (null for an unknown ref), so you can `claimWake` once its run is over.
|
|
1252
|
+
- 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
|
|
1253
|
+
acked, so `claimForDispatch` never takes it; a run that completes leaves it (`settle(..., "completed")`).
|
|
1254
|
+
- `claimWake(conversationRef, dispatchId)`, called when the conversation has no live run (after `settle`, and
|
|
1255
|
+
after `endHold`): binds the conversation's *released* held owed replies (acked, unanswered, no open hold, an
|
|
1256
|
+
ended hold not yet reported) to the new dispatch and marks those holds reported in the same transaction. It
|
|
1257
|
+
returns `{ replies, holds }`: the owed replies and the jobs whose outcomes the wake's task tells the agent. It is
|
|
1258
|
+
idempotent for one dispatch id. Start the wake run with that dispatch id as its `idempotencyKey`, as for any
|
|
1259
|
+
run; the agent answers through `reply`.
|
|
1260
|
+
- `abandonWake(conversationRef)` when no wake can run (the asker signed out): fails the released held owed
|
|
1261
|
+
replies with `SIGNED_OUT_LINE` and returns them.
|
|
1262
|
+
|
|
1263
|
+
`endHold` and `claimWake` take the same row lock, so a wake never misses a job that has just ended: whichever
|
|
1264
|
+
comes second sees the other. A claim that raced ahead of the end finds the job open; call `claimWake` again
|
|
1265
|
+
after `endHold`.
|
|
1266
|
+
|
|
1267
|
+
**7. Withdraw and stop.**
|
|
1268
|
+
|
|
1269
|
+
- `withdraw(dispatchId)` takes back a dispatch whose run Flytebot would not start (a `409 conversation_busy`) or
|
|
1270
|
+
that ended before any model call (`session_missing`): its owed replies are `sent` again (the running run's
|
|
1271
|
+
terminal dispatches them), and a wake's holds are untold again. It answers false, and changes nothing, for an
|
|
1272
|
+
unknown dispatch and for one whose owed replies were written since it was bound.
|
|
1273
|
+
- `stop(conversationRef)` for the person's Stop: fails every taken, acked or held unanswered owed reply of the
|
|
1274
|
+
conversation with `Stopped.` and returns `{ stopped, cancelHolds }`. Cancel the run and each hold ref's job; the
|
|
1275
|
+
run's later `settle` changes nothing.
|
|
1276
|
+
- `settle(dispatchId, "not_started")` ends a dispatch whose run Flytebot refused to create (a 4xx) or whose
|
|
1277
|
+
create never answered: every unanswered owed reply fails with `FAILURE_LINES.not_started`.
|
|
1278
|
+
|
|
1279
|
+
**8. Test it.** `createFakeFlytebot` scripts a run that acknowledges and replies through your real
|
|
1280
|
+
endpoint, one that holds a reply with your own tool and then stops, a wake run that answers it, and a reply
|
|
1281
|
+
whose fields your `accept` refuses; see "Testing against the fake".
|
|
1193
1282
|
|
|
1194
1283
|
## Tests
|
|
1195
1284
|
|
|
@@ -5,21 +5,43 @@ 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<
|
|
14
|
-
/**
|
|
15
|
-
export declare
|
|
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,
|
|
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 refusals of `hold`, as the app's tool reads them. */
|
|
42
|
+
export declare const HOLD_REFUSALS: {
|
|
43
|
+
readonly nothingToHold: "This run has no unanswered message to hold.";
|
|
44
|
+
readonly ambiguous: "This run has several unanswered messages; say which one the background work is for with `to`.";
|
|
45
|
+
readonly notAcknowledged: "Acknowledge that message first (phase \"ack\"), then start the background work.";
|
|
46
|
+
readonly refInUse: "That hold reference already belongs to another message.";
|
|
25
47
|
};
|
|
@@ -15,31 +15,57 @@ 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
|
-
/**
|
|
20
|
-
export
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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,
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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 refusals of `hold`, as the app's tool reads them. */
|
|
65
|
+
export const HOLD_REFUSALS = {
|
|
66
|
+
nothingToHold: "This run has no unanswered message to hold.",
|
|
67
|
+
ambiguous: "This run has several unanswered messages; say which one the background work is for with `to`.",
|
|
68
|
+
notAcknowledged: "Acknowledge that message first (phase \"ack\"), then start the background work.",
|
|
69
|
+
refInUse: "That hold reference already belongs to another message.",
|
|
44
70
|
};
|
|
45
71
|
//# sourceMappingURL=constants.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/chat/owed-replies/constants.ts"],"names":[],"mappings":"AAEA;;;GAGG;AAEH,0HAA0H;AAC1H,MAAM,CAAC,MAAM,uBAAuB,GAAG,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/chat/owed-replies/constants.ts"],"names":[],"mappings":"AAEA;;;GAGG;AAEH,0HAA0H;AAC1H,MAAM,CAAC,MAAM,uBAAuB,GAAG,OAAO,CAAC;AAQ/C;;;;GAIG;AACH,MAAM,CAAC,MAAM,aAAa,GAA0C;IAClE,SAAS,EAAE,UAAU;IACrB,SAAS,EAAE,sCAAsC;IACjD,KAAK,EAAE,oCAAoC;IAC3C,OAAO,EAAE,uCAAuC;IAChD,KAAK,EAAE,gCAAgC;IACvC,WAAW,EAAE,mCAAmC;CACjD,CAAC;AAEF,wGAAwG;AACxG,MAAM,CAAC,MAAM,eAAe,GAAG,wDAAwD,CAAC;AAExF,MAAM,QAAQ,GAAG,CAAC,KAAa,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,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, 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
10
|
export type { OwedReply, OwedReplyState } 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, 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, 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,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/chat/owed-replies/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EACL,uBAAuB,EACvB,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;AAiBlD,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
|
|
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
|
-
|
|
22
|
-
|
|
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
|
-
//
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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":"
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../../src/chat/owed-replies/plugin.ts"],"names":[],"mappings":"AAmBA,OAAO,EAAE,cAAc,EAAkB,MAAM,oBAAoB,CAAC;AACpE,OAAO,EAAE,qBAAqB,EAA+B,MAAM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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,6 +5,10 @@
|
|
|
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";
|
|
10
14
|
/** One person's message that is owed an answer, as the store reads it. */
|
|
@@ -17,6 +21,10 @@ export interface OwedReply {
|
|
|
17
21
|
ackText: string | null;
|
|
18
22
|
replyText: string | null;
|
|
19
23
|
failure: string | null;
|
|
24
|
+
/** What the app's reply fields carried with the reply (`accept`'s payload); null without one. */
|
|
25
|
+
replyPayload: unknown;
|
|
26
|
+
/** A background job holds this owed reply open; see the module comment. */
|
|
27
|
+
held: boolean;
|
|
20
28
|
/** Raised on every write, so an app's projected chat message can tell a stale write. */
|
|
21
29
|
revision: number;
|
|
22
30
|
state: OwedReplyState;
|
|
@@ -36,6 +44,8 @@ export interface OwedReplyRow {
|
|
|
36
44
|
ackText: string | null;
|
|
37
45
|
replyText: string | null;
|
|
38
46
|
failure: string | null;
|
|
47
|
+
replyPayload: unknown;
|
|
48
|
+
held: boolean;
|
|
39
49
|
revision: number;
|
|
40
50
|
taken: boolean;
|
|
41
51
|
createdAt: Date;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"state.js","sourceRoot":"","sources":["../../../src/chat/owed-replies/state.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"state.js","sourceRoot":"","sources":["../../../src/chat/owed-replies/state.ts"],"names":[],"mappings":"AAqCA,iIAAiI;AACjI,MAAM,UAAU,SAAS,CAAC,KAA2D;IACnF,OAAO,KAAK,CAAC,SAAS,IAAI,KAAK,CAAC,OAAO,IAAI,KAAK,CAAC,OAAO,CAAC;AAC3D,CAAC;AAqBD,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,YAAY,EAAE,GAAG,CAAC,YAAY;QAC9B,IAAI,EAAE,GAAG,CAAC,IAAI;QACd,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,UAAU,EAAE,GAAG,CAAC,UAAU;QAC1B,QAAQ,EAAE,GAAG,CAAC,QAAQ;KACvB,CAAC;AACJ,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
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,129 @@ export interface ReceiveMessageInput {
|
|
|
27
27
|
conversationRef: string;
|
|
28
28
|
personRef: string;
|
|
29
29
|
}
|
|
30
|
-
|
|
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;
|
|
43
88
|
} | {
|
|
44
89
|
ok: false;
|
|
45
90
|
message: string;
|
|
46
91
|
};
|
|
47
|
-
|
|
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
|
+
export interface OwedReplyStore<Tx extends OwedReplyTransaction = OwedReplyTransaction> {
|
|
48
119
|
receiveMessage(input: ReceiveMessageInput): Promise<OwedReply>;
|
|
49
120
|
claimForDispatch(conversationRef: string, dispatchId: string): Promise<OwedReply[]>;
|
|
50
|
-
writeReply(input: WriteReplyInput): Promise<WriteReplyResult>;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
121
|
+
writeReply(input: WriteReplyInput<Tx>): Promise<WriteReplyResult>;
|
|
122
|
+
/**
|
|
123
|
+
* Binds a background job to an acknowledged, unanswered owed reply of the run, in the app's
|
|
124
|
+
* own transaction `tx` (so the job row and the hold commit together). Refused with a fixed
|
|
125
|
+
* line otherwise. The same `holdRef` for the same owed reply answers ok and changes nothing.
|
|
126
|
+
*/
|
|
127
|
+
hold(tx: OwedReplyTransaction, input: HoldInput): Promise<HoldResult>;
|
|
128
|
+
/**
|
|
129
|
+
* The job ended. Idempotent; an ended hold changes nothing and an unknown one answers null.
|
|
130
|
+
* Answers the conversation of the held owed reply, so the app can `claimWake` it once its run is over.
|
|
131
|
+
*/
|
|
132
|
+
endHold(holdRef: string): Promise<{
|
|
133
|
+
conversationRef: string;
|
|
134
|
+
} | null>;
|
|
135
|
+
/** The run ended: `completed` leaves held owed replies held; any other end fails every unanswered one. */
|
|
136
|
+
settle(dispatchId: string, end: DispatchEnd): Promise<SettleResult>;
|
|
137
|
+
/**
|
|
138
|
+
* Called when the conversation has no live run: binds its released held owed replies to
|
|
139
|
+
* `dispatchId` and reports their holds. Idempotent for one dispatch id.
|
|
140
|
+
*/
|
|
141
|
+
claimWake(conversationRef: string, dispatchId: string): Promise<ClaimedWake>;
|
|
142
|
+
/** No wake can run (the asker signed out): fails the conversation's released held owed replies. */
|
|
143
|
+
abandonWake(conversationRef: string): Promise<OwedReply[]>;
|
|
144
|
+
/**
|
|
145
|
+
* Takes back a dispatch whose replies have no write yet (Flytebot answered 409 conversation_busy, or
|
|
146
|
+
* the run ended before any model call): its owed replies are `sent` again, a wake's holds unreported
|
|
147
|
+
* again. False when the dispatch is unknown or any of its replies was written since.
|
|
148
|
+
*/
|
|
149
|
+
withdraw(dispatchId: string): Promise<boolean>;
|
|
150
|
+
/** The person's Stop: fails every taken, acked or held unanswered owed reply of the conversation. */
|
|
151
|
+
stop(conversationRef: string): Promise<StopResult>;
|
|
54
152
|
/** The owed replies of a run that are neither answered nor failed, oldest first. */
|
|
55
153
|
unansweredOf(dispatchId: string): Promise<OwedReply[]>;
|
|
56
154
|
}
|
|
57
|
-
export declare function createOwedReplyStore<Tx extends OwedReplyTransaction>(options: OwedReplyStoreOptions<Tx>): OwedReplyStore
|
|
155
|
+
export declare function createOwedReplyStore<Tx extends OwedReplyTransaction>(options: OwedReplyStoreOptions<Tx>): OwedReplyStore<Tx>;
|