@superpanel-sdk/runtime 0.5.0 → 0.7.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.
Files changed (189) hide show
  1. package/README.md +3 -3
  2. package/dist/builder/codegen/emit.d.ts +5 -10
  3. package/dist/builder/codegen/emit.js +100 -106
  4. package/dist/builder/codegen/generate.d.ts +3 -5
  5. package/dist/builder/codegen/generate.js +13 -32
  6. package/dist/builder/company-imports.d.ts +5 -0
  7. package/dist/builder/company-imports.js +9 -1
  8. package/dist/builder/compile/compile-sequence.d.ts +13 -11
  9. package/dist/builder/compile/compile-sequence.js +135 -83
  10. package/dist/builder/compile/function-code.d.ts +15 -9
  11. package/dist/builder/compile/function-code.js +59 -29
  12. package/dist/builder/compile/registry.d.ts +4 -4
  13. package/dist/builder/compile/registry.js +14 -12
  14. package/dist/builder/errors.d.ts +9 -0
  15. package/dist/builder/errors.js +17 -0
  16. package/dist/builder/function-params.d.ts +10 -0
  17. package/dist/builder/function-params.js +203 -0
  18. package/dist/builder/load.d.ts +17 -17
  19. package/dist/builder/load.js +27 -25
  20. package/dist/builder/paths.d.ts +6 -1
  21. package/dist/builder/paths.js +16 -2
  22. package/dist/builder/schedules.d.ts +9 -7
  23. package/dist/builder/schedules.js +20 -21
  24. package/dist/builder/typecheck.d.ts +1 -1
  25. package/dist/builder/typecheck.js +3 -6
  26. package/dist/builder/types.d.ts +41 -5
  27. package/dist/builder/types.js +19 -0
  28. package/dist/cli/calls/index.d.ts +177 -0
  29. package/dist/cli/calls/index.js +96 -0
  30. package/dist/cli/debug/index.d.ts +31 -0
  31. package/dist/cli/debug/index.js +119 -0
  32. package/dist/cli/exec/index.d.ts +39 -0
  33. package/dist/cli/exec/index.js +138 -0
  34. package/dist/cli/exec/session.d.ts +48 -0
  35. package/dist/cli/exec/session.js +82 -0
  36. package/dist/cli/exec/trace.d.ts +18 -0
  37. package/dist/cli/exec/trace.js +52 -0
  38. package/dist/cli/index.js +84 -1
  39. package/dist/cli/logs/index.d.ts +47 -0
  40. package/dist/cli/logs/index.js +113 -0
  41. package/dist/cli/mobile/index.d.ts +12 -0
  42. package/dist/cli/mobile/index.js +405 -0
  43. package/dist/cli/serve/index.d.ts +40 -4
  44. package/dist/cli/serve/index.js +167 -28
  45. package/dist/cli/turns/index.d.ts +105 -0
  46. package/dist/cli/turns/index.js +128 -0
  47. package/dist/cli/turns/run.d.ts +13 -0
  48. package/dist/cli/turns/run.js +57 -0
  49. package/dist/cli/turns/shadow.d.ts +112 -0
  50. package/dist/cli/turns/shadow.js +54 -0
  51. package/dist/cli/type-gen/index.js +0 -1
  52. package/dist/compute-host/compute-host.d.ts +5 -6
  53. package/dist/compute-host/compute-host.js +4 -13
  54. package/dist/compute-host/source-compute-host.d.ts +5 -7
  55. package/dist/compute-host/source-compute-host.js +12 -33
  56. package/dist/core/case-orchestrator/contact-hours.d.ts +26 -6
  57. package/dist/core/case-orchestrator/contact-hours.js +27 -8
  58. package/dist/core/case-orchestrator/index.d.ts +1 -4
  59. package/dist/core/case-orchestrator/index.js +1 -4
  60. package/dist/core/case-orchestrator/render.d.ts +1 -1
  61. package/dist/core/case-orchestrator/render.js +15 -10
  62. package/dist/core/case-orchestrator/types.d.ts +83 -128
  63. package/dist/core/case-orchestrator/types.js +0 -4
  64. package/dist/core/context/email-message.d.ts +40 -1
  65. package/dist/core/context/email-message.js +27 -0
  66. package/dist/core/context/email.d.ts +13 -1
  67. package/dist/core/context/email.js +2 -3
  68. package/dist/core/context/exchanges.d.ts +25 -0
  69. package/dist/core/context/exchanges.js +25 -7
  70. package/dist/core/context/instances.d.ts +31 -2
  71. package/dist/core/context/instances.js +18 -4
  72. package/dist/core/context/platform-registry.d.ts +35 -0
  73. package/dist/core/context/sms.d.ts +30 -0
  74. package/dist/core/context/sms.js +20 -0
  75. package/dist/core/context/types.d.ts +29 -1
  76. package/dist/core/conversation/index.d.ts +58 -4
  77. package/dist/core/conversation/index.js +123 -4
  78. package/dist/core/functions/errors.d.ts +74 -0
  79. package/dist/core/functions/errors.js +26 -0
  80. package/dist/core/functions/events.d.ts +23 -78
  81. package/dist/core/functions/events.js +8 -17
  82. package/dist/core/functions/schedules.d.ts +9 -10
  83. package/dist/core/functions/schedules.js +2 -2
  84. package/dist/core/functions/types.d.ts +17 -72
  85. package/dist/core/functions/types.js +1 -9
  86. package/dist/core/page/define.d.ts +230 -160
  87. package/dist/core/page/define.js +145 -104
  88. package/dist/core/page/envelope.d.ts +55 -0
  89. package/dist/core/page/envelope.js +4 -0
  90. package/dist/core/page/fns.d.ts +448 -0
  91. package/dist/core/page/fns.js +539 -0
  92. package/dist/core/page/memory-reader.d.ts +27 -0
  93. package/dist/core/page/memory-reader.js +299 -0
  94. package/dist/core/page/page-host.d.ts +20 -0
  95. package/dist/core/page/page-host.js +57 -0
  96. package/dist/core/page/records-query.d.ts +234 -0
  97. package/dist/core/page/records-query.js +780 -0
  98. package/dist/core/page/render.d.ts +67 -25
  99. package/dist/core/page/render.js +492 -272
  100. package/dist/core/page/sandbox.d.ts +85 -0
  101. package/dist/core/page/sandbox.js +357 -0
  102. package/dist/core/page/time-format.d.ts +27 -0
  103. package/dist/core/page/time-format.js +84 -0
  104. package/dist/core/page/typed.d.ts +165 -159
  105. package/dist/core/page/workspace.d.ts +25 -1
  106. package/dist/core/page/workspace.js +48 -13
  107. package/dist/core/party/schema.d.ts +74 -3
  108. package/dist/core/party/schema.js +39 -6
  109. package/dist/core/party/types.d.ts +8 -13
  110. package/dist/core/sequence/define.d.ts +1 -2
  111. package/dist/core/slot/company-metadata.d.ts +11 -22
  112. package/dist/core/slot/company-metadata.js +12 -39
  113. package/dist/core/slot/compile-schema.js +6 -6
  114. package/dist/core/slot/field.d.ts +1 -2
  115. package/dist/core/slot/field.js +0 -2
  116. package/dist/core/slot/index.d.ts +3 -3
  117. package/dist/core/slot/index.js +3 -3
  118. package/dist/core/slot/naming.d.ts +1 -1
  119. package/dist/core/slot/naming.js +1 -1
  120. package/dist/core/slot/platform-fields.d.ts +4 -4
  121. package/dist/core/slot/platform-fields.js +5 -5
  122. package/dist/core/slot/schema-compat.d.ts +8 -7
  123. package/dist/core/slot/schema-compat.js +22 -31
  124. package/dist/core/slot/schema.d.ts +3 -59
  125. package/dist/core/slot/schema.js +0 -15
  126. package/dist/core/slot/types.d.ts +4 -10
  127. package/dist/core/slot/value.d.ts +2 -0
  128. package/dist/core/slot/value.js +6 -2
  129. package/dist/dev-overlay/protocol.d.ts +38 -5
  130. package/dist/fn-context/build-context.js +10 -4
  131. package/dist/fn-context/fake/index.d.ts +2 -0
  132. package/dist/fn-context/fake/index.js +5 -3
  133. package/dist/fn-context/fn-params.d.ts +2 -5
  134. package/dist/fn-context/fn-params.js +6 -9
  135. package/dist/fn-context/index.d.ts +1 -1
  136. package/dist/fn-context/make-ctx.js +5 -4
  137. package/dist/fn-context/rpc/case-orchestrator-tools.d.ts +11 -3
  138. package/dist/fn-context/rpc/case-orchestrator-tools.js +0 -1
  139. package/dist/fn-context/rpc/errors.d.ts +13 -0
  140. package/dist/fn-context/rpc/errors.js +22 -0
  141. package/dist/fn-context/rpc/index.d.ts +1 -1
  142. package/dist/fn-context/rpc/index.js +1 -1
  143. package/dist/fn-context/rpc/integration/index.d.ts +11 -1
  144. package/dist/fn-context/rpc/log.d.ts +11 -1
  145. package/dist/fn-context/rpc/platform.d.ts +11 -1
  146. package/dist/fn-context/rpc/records.d.ts +13 -6
  147. package/dist/fn-context/rpc/records.js +2 -4
  148. package/dist/fn-context/rpc/trpc-client.d.ts +42 -0
  149. package/dist/fn-context/rpc/trpc-client.js +151 -22
  150. package/dist/fn-context/rpc/trpc-init.d.ts +19 -1
  151. package/dist/fn-context/rpc/trpc-init.js +10 -4
  152. package/dist/fn-context/rpc/trpc-router-factory.d.ts +66 -8
  153. package/dist/fn-context/run-scope.js +1 -1
  154. package/dist/fn-context/types.d.ts +20 -6
  155. package/dist/index.d.ts +11 -12
  156. package/dist/index.js +5 -8
  157. package/dist/logger.d.ts +4 -0
  158. package/dist/logger.js +8 -0
  159. package/dist/platform.d.ts +32 -33
  160. package/dist/platform.js +23 -22
  161. package/dist/testing-compile-child.d.ts +2 -0
  162. package/dist/testing-compile-child.js +20 -0
  163. package/dist/testing.d.ts +368 -0
  164. package/dist/testing.js +439 -0
  165. package/dist/voice-service/actions.d.ts +12 -35
  166. package/dist/voice-service/index.d.ts +2 -0
  167. package/dist/voice-service/index.js +2 -0
  168. package/dist/voice-service/protocol.d.ts +604 -0
  169. package/dist/voice-service/protocol.js +67 -0
  170. package/dist/voice-service/types.d.ts +16 -6
  171. package/dist/worker.d.ts +2 -1
  172. package/dist/worker.js +2 -1
  173. package/package.json +20 -16
  174. package/dist/core/case-orchestrator/follow-ups.d.ts +0 -31
  175. package/dist/core/case-orchestrator/follow-ups.js +0 -103
  176. package/dist/core/case-orchestrator/tools.d.ts +0 -29
  177. package/dist/core/case-orchestrator/tools.js +0 -31
  178. package/dist/core/case-orchestrator/validate.d.ts +0 -11
  179. package/dist/core/case-orchestrator/validate.js +0 -63
  180. package/dist/core/functions/define/bind.d.ts +0 -25
  181. package/dist/core/functions/define/bind.js +0 -14
  182. package/dist/core/functions/define/case.d.ts +0 -9
  183. package/dist/core/functions/define/case.js +0 -13
  184. package/dist/core/functions/define/events.d.ts +0 -20
  185. package/dist/core/functions/define/events.js +0 -21
  186. package/dist/core/functions/define/no-case.d.ts +0 -4
  187. package/dist/core/functions/define/no-case.js +0 -9
  188. package/dist/core/functions/func-ref.d.ts +0 -25
  189. package/dist/core/functions/func-ref.js +0 -20
@@ -1,30 +1,22 @@
1
1
  import type { SystemContext } from '../context/types.js';
2
- import type { Db, ReadDb } from '../page/define.js';
3
- import type { Id, PageTablesDesc, TypedDb, TypedReadDb } from '../page/typed.js';
2
+ import type { Db } from '../page/define.js';
3
+ import type { Id, PageTablesDesc, TypedDb } from '../page/typed.js';
4
4
  import type { ContactSchedule } from '../slot/schema.js';
5
5
  /** Reserved export of `sequences/<id>/orchestrator.ts` and its name in the build's `functions`. */
6
6
  export declare const ORCHESTRATOR_FUNCTION = "orchestrator";
7
- /** `orchestrator.ts`'s required `contacts` export: its name in the build's `functions`. */
8
- export declare const ORCHESTRATOR_CONTACTS_FUNCTION = "orchestratorContacts";
9
7
  /** `ctx.meta.entry` / RunScope entry of a Case Orchestrator call. */
10
8
  export declare const ORCHESTRATOR_ENTRY = "orchestrator";
11
- /** `SequenceBuild.orchestrator`: the entry point, and the function that says whom a Run may contact. */
9
+ /** `SequenceBuild.orchestrator`: the entry point. */
12
10
  export type CaseOrchestratorManifest = {
13
11
  fn: string;
14
- contactsFn: string;
15
12
  };
16
13
  /** Platform tools a Run may open. Names, descriptions, schemas and executors belong to the platform. */
17
- export declare const CASE_ORCHESTRATOR_TOOL_NAMES: readonly ['readExchange', 'searchExchanges', 'updateSlots', 'scheduleCall', 'scheduleSms', 'scheduleEmail', 'scheduleUpdateSlots', 'rescheduleAction', 'cancelScheduledAction', 'scheduleNextSequence', 'closeInstance', 'scheduleWake', 'cancelWake'];
14
+ export declare const CASE_ORCHESTRATOR_TOOL_NAMES: readonly ['readExchange', 'searchExchanges', 'scheduleCall', 'scheduleSms', 'scheduleEmail', 'scheduleUpdateSlots', 'rescheduleAction', 'cancelScheduledAction', 'scheduleNextSequence', 'scheduleWake', 'cancelWake'];
18
15
  export type CaseOrchestratorToolName = (typeof CASE_ORCHESTRATOR_TOOL_NAMES)[number];
19
- /** A reference to one platform tool, from `tools.*()`. */
20
- export type ToolRef<N extends CaseOrchestratorToolName = CaseOrchestratorToolName> = {
21
- kind: 'tool';
22
- name: N;
23
- };
24
- /** Channels the Agent can contact a contact on. */
16
+ /** Channels a contact is reached on. */
25
17
  export type ContactChannel = 'call' | 'sms' | 'email';
26
18
  /**
27
- * Kinds of actions the Agent can schedule: contacting a contact, slot writes at a later time,
19
+ * Kinds of actions that can be scheduled: contacting a contact, slot writes at a later time,
28
20
  * handing the instance over to the next sequence (14-sequence-instance §4.3), or waking the
29
21
  * Orchestrator up (`wake`, 25 §6.3).
30
22
  */
@@ -44,14 +36,13 @@ export type RunTrigger = TriggerBase & ({
44
36
  direction: 'inbound' | 'outbound';
45
37
  /** Null when the call never connected. Read it with `ctx.api.db.session.findUnique({ where: { id } })`. */
46
38
  sessionId: Id<'session'> | null;
47
- /** Set when the Agent scheduled this call. */
39
+ /** Set when the call was a scheduled action. */
48
40
  scheduledActionId: string | null;
49
41
  /**
50
42
  * `'connected'` when a session opened (corresponds to `result === 'answered'` on `ExchangeEvent`);
51
- * `'voicemail'` when a message was left on it, `'screened'` when only a call-screening assistant
52
- * answered (25 §13).
43
+ * `'voicemail'` when a message was left on it (65 §3.3).
53
44
  */
54
- result: 'connected' | 'no-answer' | 'busy' | 'failed' | 'voicemail' | 'screened';
45
+ result: 'connected' | 'no-answer' | 'busy' | 'failed' | 'voicemail';
55
46
  }
56
47
  /** An outbound SMS was handed to the provider, or failed to send. */
57
48
  | {
@@ -91,8 +82,8 @@ export type RunTrigger = TriggerBase & ({
91
82
  } | {
92
83
  type: 'contact.communication-preferences.updated';
93
84
  contactId: Id<'contact'>;
94
- /** The contact's `Record.version` after the preference write committed. */
95
- contactVersion: number;
85
+ /** The `SlotWriteLog` row of the preference field this write changed: what changed, by whom (41 §2). */
86
+ slotWriteLogId: string;
96
87
  }
97
88
  /** The instance started (a first conversation, a script, a hand-over). */
98
89
  | {
@@ -113,6 +104,19 @@ export type RunTrigger = TriggerBase & ({
113
104
  kind: ScheduledActionKind;
114
105
  errorCode: string;
115
106
  }
107
+ /**
108
+ * A scheduled action ended with nothing else to say so (74 §4.7): every action that ends wakes the
109
+ * Orchestrator, to book the next step or close the case. `not_sent`: a text or email whose page decided to
110
+ * send nothing (`reason` says why); `written`: slot writes made.
111
+ */
112
+ | {
113
+ type: 'scheduled_action.ended';
114
+ scheduledActionId: string;
115
+ kind: ScheduledActionKind;
116
+ outcome: 'not_sent' | 'written';
117
+ /** Why it was not sent (`noReply: …`, `handoff: …`); null for `written`. */
118
+ reason: string | null;
119
+ }
116
120
  /**
117
121
  * Company code told the instance about something outside the platform (a payment posted, a CRM
118
122
  * change): `ctx.api.instances.notify` from a webhook, a function or an event handler (25 §6.2).
@@ -146,12 +150,11 @@ export type ScheduledActionTiming = {
146
150
  };
147
151
  /**
148
152
  * What a contact (call / SMS / email) was asked for: its stored `input`. `timing` says when: it is
149
- * what two requests are compared on and what a reschedule replaces; the other keys ride along.
153
+ * what two requests are compared on and what a reschedule replaces; the other keys ride along. Why
154
+ * it happens is the action's `reason` (40 §2.6).
150
155
  */
151
156
  export type ContactActionInput = {
152
157
  timing: ScheduledActionTiming;
153
- /** Why it happens; the conversation it opens gets it too. */
154
- note?: string;
155
158
  /** The contact's address to use: its `phone` (a call, a text) or `email`, the default. */
156
159
  to?: string;
157
160
  /** Our number to use; default the contact's assigned number, else the sequence's outbound numbers. */
@@ -167,16 +170,15 @@ export type ContactActionInput = {
167
170
  customerQuote: string;
168
171
  };
169
172
  };
170
- /** What scheduled slot writes (`update_slots`) were asked for: when, what is written, and why. */
173
+ /** What scheduled slot writes (`update_slots`) were asked for: when, and what is written (why: the action's `reason`). */
171
174
  export type UpdateSlotsActionInput = {
172
175
  timing: {
173
176
  mode: 'at';
174
177
  at: string;
175
178
  };
176
179
  writes: SlotWriteInput[];
177
- reason: string;
178
180
  };
179
- /** What a hand-over (`next_sequence`) was asked for: when, to which sequence, on which row, its variables, and why. */
181
+ /** What a hand-over (`next_sequence`) was asked for: when, to which sequence, on which row, its variables (why: the action's `reason`). */
180
182
  export type NextSequenceActionInput = {
181
183
  timing: {
182
184
  mode: 'at';
@@ -187,16 +189,13 @@ export type NextSequenceActionInput = {
187
189
  /** The row the next instance starts on, when one was named; else this instance's own. */
188
190
  subjectRecordId?: string;
189
191
  variables?: Record<string, unknown>;
190
- reason: string;
191
192
  };
192
- /** What a wake-up (`wake`) was booked for: when, its name (one pending per name), and why. */
193
+ /** When a wake-up fires. Its business name and explanation are the action's `name` and `reason` columns (51). */
193
194
  export type WakeActionInput = {
194
195
  timing: {
195
196
  mode: 'at';
196
197
  at: string;
197
198
  };
198
- name: string;
199
- note?: string;
200
199
  };
201
200
  /** Each kind's stored `input`. */
202
201
  export type ScheduledActionInputs = {
@@ -211,6 +210,8 @@ export type ScheduledActionInput = ScheduledActionInputs[ScheduledActionKind];
211
210
  /** A pending contact: who and its `input`. */
212
211
  export type ContactActionView = {
213
212
  id: string;
213
+ name?: string | null;
214
+ status?: 'scheduled' | 'running' | 'completed' | 'failed' | 'cancelled';
214
215
  kind: ContactChannel;
215
216
  /** The contact to reach. */
216
217
  contactId: string;
@@ -219,33 +220,47 @@ export type ContactActionView = {
219
220
  */
220
221
  scheduledFor: string;
221
222
  input: ContactActionInput;
223
+ /** Why it was booked (40 §2.6); null when nobody said. */
224
+ reason: string | null;
222
225
  };
223
226
  /** Pending scheduled slot writes: instance-level, no contact. */
224
227
  export type UpdateSlotsActionView = {
225
228
  id: string;
229
+ name?: string | null;
230
+ status?: 'scheduled' | 'running' | 'completed' | 'failed' | 'cancelled';
226
231
  kind: 'update_slots';
227
232
  contactId: null;
228
233
  /** Execution time (`input.timing.at`, ISO 8601). */
229
234
  scheduledFor: string;
230
235
  input: UpdateSlotsActionInput;
236
+ /** Why it was booked (40 §2.6); null when nobody said. */
237
+ reason: string | null;
231
238
  };
232
239
  /** A pending hand-over to the next sequence: instance-level, no contact. */
233
240
  export type NextSequenceActionView = {
234
241
  id: string;
242
+ name?: string | null;
243
+ status?: 'scheduled' | 'running' | 'completed' | 'failed' | 'cancelled';
235
244
  kind: 'next_sequence';
236
245
  contactId: null;
237
246
  /** Execution time (`input.timing.at`, ISO 8601). */
238
247
  scheduledFor: string;
239
248
  input: NextSequenceActionInput;
249
+ /** Why it was booked (40 §2.6); null when nobody said. */
250
+ reason: string | null;
240
251
  };
241
252
  /** A pending wake-up of the Orchestrator: instance-level, no contact. */
242
253
  export type WakeActionView = {
243
254
  id: string;
255
+ name?: string | null;
256
+ status?: 'scheduled' | 'running' | 'completed' | 'failed' | 'cancelled';
244
257
  kind: 'wake';
245
258
  contactId: null;
246
259
  /** When it wakes the Orchestrator (`input.timing.at`, ISO 8601). */
247
260
  scheduledFor: string;
248
261
  input: WakeActionInput;
262
+ /** Why it was booked (40 §2.6); null when nobody said. */
263
+ reason: string | null;
249
264
  };
250
265
  /** A scheduled action still active when the Run input is built; `kind` tells which `input` it has. */
251
266
  export type ScheduledActionView = ContactActionView | UpdateSlotsActionView | NextSequenceActionView | WakeActionView;
@@ -271,7 +286,7 @@ export type ExchangeEvent = {
271
286
  result: 'answered' | 'no-answer' | 'sent' | 'failed' | 'received';
272
287
  /** A call's length in seconds; null for SMS and email, or a call whose length is not known yet. */
273
288
  durationSec: number | null;
274
- /** Set when the Agent scheduled it. */
289
+ /** Set when it was a scheduled action. */
275
290
  scheduledActionId: string | null;
276
291
  };
277
292
  /** Exchange counts, last events by kind, and recent timeline for the Case (`all`) or one party. */
@@ -360,12 +375,12 @@ export type PartyContactHours = {
360
375
  }[];
361
376
  }[];
362
377
  };
363
- /** Exchange aggregates the platform computes when a Run starts. */
378
+ /** Exchange aggregates of an instance with the contacts its code names (`ctx.api.exchanges.activity`, 38 §2.8). */
364
379
  export type CaseActivity = {
365
- /** Timezone of the instance-level day numbers: the first contact's of the Run's `contacts`. */
380
+ /** Timezone of the instance-level day numbers: the first named contact's. */
366
381
  timezone: string;
367
382
  all: ActivityStats;
368
- /** Every contact the Run may reach (orchestrator.ts's `contacts`), keyed by contactId, in that order. */
383
+ /** Every named contact, keyed by contactId, in the order named. */
369
384
  byContact: Readonly<Record<string, PartyActivity>>;
370
385
  };
371
386
  export declare const EMPTY_CASE_ACTIVITY: CaseActivity;
@@ -380,7 +395,6 @@ export type CaseOrchestratorParams = {
380
395
  scheduledActions: readonly ScheduledActionView[];
381
396
  /** Agreed follow-ups that finished in the last {@link RECENT_AGREED_ACTIONS_DAYS} days, newest first: a promise already kept is not booked again. */
382
397
  recentAgreedActions: readonly FinishedAgreedActionView[];
383
- activity: CaseActivity;
384
398
  };
385
399
  /**
386
400
  * The Orchestrator's API: everything a function's `ctx.api` has (25 §7: the platform surfaces, every
@@ -396,19 +410,20 @@ export type CaseOrchestratorParams = {
396
410
  */
397
411
  export type CaseOrchestratorApi<D extends PageTablesDesc | undefined = undefined> = Omit<SystemContext['api'], 'db'> & {
398
412
  /**
399
- * The platform's tools, the Agent's too: scheduling goes through them, where opt-outs and the
413
+ * The platform's tools: scheduling goes through them, where opt-outs and the
400
414
  * instance's state are checked. A text or email sent with an integration directly is not.
401
415
  */
402
416
  tools: CaseOrchestratorTools;
403
417
  db: D extends PageTablesDesc ? TypedDb<D> : Db;
404
418
  };
405
419
  /**
406
- * `ctx` of `orchestrator`: a function's `ctx`, so a function can be
407
- * called with it, plus `params` and `api.tools`. `D` types `ctx.api.db`.
420
+ * `ctx` of `orchestrator`: a function's `ctx` (38 §2.3), so a function can be called with it, plus
421
+ * `api.tools`. `D` types `ctx.api.db`.
408
422
  */
409
- export type CaseOrchestratorContext<D extends PageTablesDesc | undefined = undefined> = Omit<SystemContext, 'api'> & {
423
+ export type CaseOrchestratorContext<D extends PageTablesDesc | undefined = undefined> = Omit<SystemContext, 'api' | 'meta'> & {
424
+ /** What runs, where (38 §2.3): the instance is `args.instanceId`. */
425
+ meta: Omit<SystemContext['meta'], 'instanceId'>;
410
426
  api: CaseOrchestratorApi<D>;
411
- params: CaseOrchestratorParams;
412
427
  };
413
428
  /**
414
429
  * When a scheduled action runs (25 §4): `at`, an exact instant (ISO 8601 with an offset or Z; its
@@ -422,7 +437,7 @@ export type ScheduleTiming = {
422
437
  /**
423
438
  * What makes a contact an agreed follow-up: the party asked for it or agreed to it in
424
439
  * `agreedInExchangeId`, in their message at `agreedAt`, in the words `customerQuote` (all three
425
- * together; the platform does not check them, and `agreedAt` tells agreements apart). Like any
440
+ * together; the platform does not check them, and they serve only as evidence, not deduplication). Like any
426
441
  * action it runs at its `at`; an opt-out still stops it.
427
442
  * docs/design/09-agreed-follow-up.md
428
443
  */
@@ -435,7 +450,7 @@ export type AgreementFields = {
435
450
  note?: string;
436
451
  };
437
452
  /**
438
- * Whom to contact (one of the Run's `contacts`) and, optionally, the addresses: `to` one of the
453
+ * Whom to contact and, optionally, the addresses: `to` one of the
439
454
  * contact's phone / email fields' values (default: its `phone` / `email`), `from` one of our numbers
440
455
  * (default: the contact's assigned number, else this sequence's outbound numbers, 14 §10).
441
456
  */
@@ -443,8 +458,9 @@ export type ScheduleActionInput = {
443
458
  contactId: string;
444
459
  to?: string;
445
460
  from?: string;
461
+ name?: string;
446
462
  } & ScheduleTiming & AgreementFields;
447
- /** One field on an existing row, as `updateSlots` takes it. */
463
+ /** One field on an existing row, as `scheduleUpdateSlots` takes it. */
448
464
  export type SlotFieldWriteInput = {
449
465
  /** `table.field`. */
450
466
  path: string;
@@ -462,7 +478,7 @@ export type SlotInsertInput = {
462
478
  insert: string;
463
479
  values: Record<string, string | number | boolean | string[] | null | ContactSchedule>;
464
480
  };
465
- /** One slot write, as `updateSlots` and `scheduleUpdateSlots` take it. */
481
+ /** One slot write, as `scheduleUpdateSlots` takes it. */
466
482
  export type SlotWriteInput = SlotFieldWriteInput | SlotInsertInput;
467
483
  /**
468
484
  * Slot writes to make at a set time, and why. Validated now, written as given when due (checked
@@ -558,15 +574,12 @@ export type ToolCallArgs = {
558
574
  direction?: ExchangeEvent['direction'];
559
575
  result?: ExchangeEvent['result'][];
560
576
  scheduledActionId?: string;
561
- /** `case` (default): this case only; `contact`: every case of this case's contacts (27 §5.4). */
562
- scope?: 'case' | 'contact';
577
+ /** That case's exchanges. Company code names at least one of `instanceId`, `contactId`, `scheduledActionId`. */
578
+ instanceId?: string;
563
579
  limit?: number;
564
580
  /** Pagination cursor from a previous result's `nextBefore`. */
565
581
  before?: string;
566
582
  };
567
- updateSlots: {
568
- writes: SlotWriteInput[];
569
- };
570
583
  scheduleCall: ScheduleActionInput;
571
584
  scheduleSms: ScheduleActionInput;
572
585
  scheduleEmail: ScheduleActionInput;
@@ -586,11 +599,6 @@ export type ToolCallArgs = {
586
599
  reason: string;
587
600
  };
588
601
  scheduleNextSequence: ScheduleNextSequenceInput;
589
- /** End this instance (completed / cancelled / a short text): pending actions are cancelled. */
590
- /** `reason`: why, 1–64 characters (`'signed up'`, `'asked us to stop'`). */
591
- closeInstance: {
592
- reason: string;
593
- };
594
602
  scheduleWake: ScheduleWakeInput;
595
603
  /** Cancel the pending wake-up of this name, if there is one. */
596
604
  cancelWake: {
@@ -625,12 +633,6 @@ export type ScheduledResult = {
625
633
  outcome: 'created' | 'existing';
626
634
  scheduledActionId: string;
627
635
  scheduledFor: string;
628
- /**
629
- * The contact's `Record.version` at the time the action was scheduled (`0` without a contact);
630
- * for `existing`, the version recorded when it was first scheduled. Recorded only, not checked
631
- * when the action fires.
632
- */
633
- contactVersion: number;
634
636
  /** An agreed follow-up replaced this pending regular follow-up on the same channel (it is cancelled). */
635
637
  supersededScheduledActionId?: string;
636
638
  };
@@ -641,17 +643,10 @@ export type RescheduledResult = {
641
643
  scheduledFor: string;
642
644
  /** The `scheduledFor` timestamp before rescheduling (equals `scheduledFor` when `unchanged`). */
643
645
  previousScheduledFor: string;
644
- /** The contact's `Record.version` at the time of rescheduling (`0` without a contact). */
645
- contactVersion: number;
646
646
  };
647
647
  export type ToolCallResult = {
648
648
  readExchange: ReadExchangeResult;
649
649
  searchExchanges: SearchExchangesResult;
650
- updateSlots: {
651
- outcome: 'written';
652
- paths: string[];
653
- inserts: string[];
654
- };
655
650
  scheduleCall: ScheduledResult;
656
651
  scheduleSms: ScheduledResult;
657
652
  scheduleEmail: ScheduledResult;
@@ -663,10 +658,6 @@ export type ToolCallResult = {
663
658
  scheduledActionId: string;
664
659
  };
665
660
  scheduleNextSequence: ScheduledResult;
666
- closeInstance: {
667
- outcome: 'closed' | 'already_closed';
668
- cancelledScheduledActionIds: string[];
669
- };
670
661
  scheduleWake: WakeResult;
671
662
  /** `not_pending`: no wake-up of that name was waiting (it never was, already ran, or was cancelled). */
672
663
  cancelWake: {
@@ -677,77 +668,41 @@ export type ToolCallResult = {
677
668
  export type ToolResult<T> = {
678
669
  ok: true;
679
670
  value: T;
680
- toolCallId: string;
671
+ toolCallId?: string;
681
672
  } | {
682
673
  ok: false;
683
674
  error: {
684
675
  code: string;
685
676
  message: string;
686
677
  };
687
- toolCallId: string;
688
- };
689
- export type ToolCallOptions = {
690
- /**
691
- * Stable idempotency key within the Run: calling the same tool again with the same `callKey`
692
- * and arguments replays the recorded result instead of executing twice; reusing it for another
693
- * tool or other arguments fails with `idempotency_conflict`.
694
- */
695
- callKey: string;
678
+ toolCallId?: string;
696
679
  };
680
+ /** The tools that only read: they work on no instance (38 §2.7). */
681
+ export type ReadToolName = 'readExchange' | 'searchExchanges';
682
+ /**
683
+ * `ctx.api.tools` (38 §2.7): a platform API of every function and entry, the same for every caller,
684
+ * held to the caller's company and environment. A command names the instance it works on and acts
685
+ * each time it is called (its record is `toolCallId`); a read names what it reads.
686
+ */
697
687
  export type CaseOrchestratorTools = {
698
- [N in CaseOrchestratorToolName]: (input: ToolCallArgs[N], options: ToolCallOptions) => Promise<ToolResult<ToolCallResult[N]>>;
699
- };
700
- /** An Agent Run request: the LLM decides which of `tools` to call. */
701
- export type AgentRequest = {
702
- kind: 'agent';
703
- /** The whole user message the Agent reads, written by company code (docs/design/08-case-orchestrator.md §5.3). */
704
- prompt: string;
705
- /** The tools the Agent may call, from `tools.*()`. Left out: every platform tool; `[]`: none. */
706
- tools?: readonly ToolRef[];
707
- };
708
- /** An AgentRequest as the platform runs it: `tools` resolved to the Agent's whitelist. */
709
- export type ResolvedAgentRequest = AgentRequest & {
710
- tools: readonly ToolRef[];
688
+ [N in CaseOrchestratorToolName]: (input: N extends ReadToolName ? ToolCallArgs[N] : ToolCallArgs[N] & {
689
+ instanceId: string;
690
+ }) => Promise<ToolResult<ToolCallResult[N]>>;
711
691
  };
712
- export type FinalOrchestrationResult = {
713
- kind: 'final';
714
- finalSummary: string;
715
- };
716
- export type CaseOrchestratorResult = AgentRequest | FinalOrchestrationResult;
717
692
  /**
718
- * What code working on a case receives as `c` — the Orchestrator and event handlers: the instance, and
719
- * its business root's id, the anchor of its reads (`ctx.api.db.<root table>.findUnique({ where: { id:
720
- * c.subject } })`). Fixed for the instance, so the platform fills it; what changes (run variables, the
721
- * instance's status) is read with `ctx.api.instances.get`.
693
+ * What `orchestrator` is called with (38 §2.2): the instance and its business root's id, the anchor of
694
+ * its reads (`ctx.api.db.<root table>.findUnique({ where: { id: args.subject } })`), and the Run's
695
+ * frozen inputs. What changes (run variables, the instance's status) is read with
696
+ * `ctx.api.instances.get`.
722
697
  */
723
- export type CaseInput<S extends string = string> = {
698
+ export type CaseOrchestratorArgs<S extends string = string> = {
724
699
  subject: Id<S>;
725
700
  instanceId: string;
726
- };
727
- /** What `orchestrator` and `contacts` receive as `c` ({@link CaseInput}). */
728
- export type CaseOrchestratorInput<S extends string = string> = CaseInput<S>;
729
- /**
730
- * `ctx` of `contacts`: the Orchestrator's API, before the Run's params are gathered; it only reads
731
- * (`db` has the read methods only). The platform runs it more than once per Run, so a write or a
732
- * tool call there would be made again each time.
733
- */
734
- export type CaseOrchestratorContactsContext<D extends PageTablesDesc | undefined = undefined> = Omit<CaseOrchestratorContext<D>, 'params' | 'api'> & {
735
- api: Pick<CaseOrchestratorApi<D>, 'call' | 'sms' | 'email' | 'log'> & {
736
- db: D extends PageTablesDesc ? TypedReadDb<D> : ReadDb;
737
- };
738
- };
739
- /**
740
- * `orchestrator.ts`'s `contacts`: whom this Run may contact (schedule calls, SMS and email to), read
741
- * with `ctx.api.db`; the ids of contact rows of the company. The platform runs it when the Run
742
- * starts (a pending contact of a person no longer listed is cancelled, and
743
- * `ctx.params.activity.byContact` is keyed by these), and again when the Run is handed to the Agent
744
- * (the orchestrator may have changed the data).
745
- */
746
- export type CaseOrchestratorContacts<C = CaseOrchestratorInput, D extends PageTablesDesc | undefined = undefined> = (caseContext: C, ctx: CaseOrchestratorContactsContext<D>) => Promise<readonly string[]>;
701
+ } & CaseOrchestratorParams;
747
702
  /**
748
- * `orchestrator.ts`: `export const orchestrator: CaseOrchestrator = async (c, ctx) => …`.
749
- * `C` is the sequence's `CaseOrchestratorInput`; `D` its `PageTables` (`__gen` binds both).
703
+ * `orchestrator.ts`: `export const orchestrator: CaseOrchestrator = async (ctx, args) => …`.
704
+ * `D` is the company's `PageTables`, `S` the sequence's subject table (`__gen` binds both).
750
705
  */
751
- export type CaseOrchestrator<C = CaseOrchestratorInput, D extends PageTablesDesc | undefined = undefined> = (caseContext: C, ctx: CaseOrchestratorContext<D>) => Promise<CaseOrchestratorResult>;
706
+ export type CaseOrchestrator<D extends PageTablesDesc | undefined = undefined, S extends string = string> = (ctx: CaseOrchestratorContext<D>, args: CaseOrchestratorArgs<S>) => Promise<void>;
752
707
  export {};
753
708
  //# sourceMappingURL=types.d.ts.map
@@ -1,14 +1,11 @@
1
1
  /** Reserved export of `sequences/<id>/orchestrator.ts` and its name in the build's `functions`. */
2
2
  export const ORCHESTRATOR_FUNCTION = 'orchestrator';
3
- /** `orchestrator.ts`'s required `contacts` export: its name in the build's `functions`. */
4
- export const ORCHESTRATOR_CONTACTS_FUNCTION = 'orchestratorContacts';
5
3
  /** `ctx.meta.entry` / RunScope entry of a Case Orchestrator call. */
6
4
  export const ORCHESTRATOR_ENTRY = 'orchestrator';
7
5
  /** Platform tools a Run may open. Names, descriptions, schemas and executors belong to the platform. */
8
6
  export const CASE_ORCHESTRATOR_TOOL_NAMES = [
9
7
  'readExchange',
10
8
  'searchExchanges',
11
- 'updateSlots',
12
9
  'scheduleCall',
13
10
  'scheduleSms',
14
11
  'scheduleEmail',
@@ -16,7 +13,6 @@ export const CASE_ORCHESTRATOR_TOOL_NAMES = [
16
13
  'rescheduleAction',
17
14
  'cancelScheduledAction',
18
15
  'scheduleNextSequence',
19
- 'closeInstance',
20
16
  'scheduleWake',
21
17
  'cancelWake',
22
18
  ];
@@ -85,10 +85,35 @@ export declare const emailGetOutput: z.ZodNullable<z.ZodObject<{
85
85
  delivery: z.ZodNullable<z.ZodString>;
86
86
  occurredAt: z.ZodString;
87
87
  }, z.core.$strip>>;
88
- /** Platform email-history capability, reached from scripts as `ctx.api.email`. */
88
+ /**
89
+ * An email sent now (64 §15), from any entry, as a text is (`ctx.api.sms.send`): one recipient, no cc or bcc.
90
+ * `to` is a contact's row id, or `{ address }` for someone who is not one (an on-call lawyer, opposing
91
+ * counsel). An address that is a contact's of this environment is sent as to that contact: their opt-outs
92
+ * are checked. `from` names the mailbox (its name in the environment); absent, a contact's own mailbox (one
93
+ * picked at random is remembered on them), else any active one. It goes back in the newest thread with
94
+ * them on that mailbox, else starts one; stored with the run, then sent. The case it is about, and who
95
+ * hears `email.sent`: as for a text. Pause and contact hours are not checked.
96
+ */
97
+ export declare const emailSendInput: z.ZodObject<{
98
+ to: z.ZodUnion<readonly [z.ZodString, z.ZodObject<{
99
+ address: z.ZodString;
100
+ }, z.core.$strict>]>;
101
+ body: z.ZodString;
102
+ subject: z.ZodOptional<z.ZodString>;
103
+ instanceId: z.ZodOptional<z.ZodString>;
104
+ from: z.ZodOptional<z.ZodString>;
105
+ }, z.core.$strict>;
106
+ export type EmailSendParams = z.infer<typeof emailSendInput>;
107
+ /** The thread the email went on (`email.sent` on the case's timeline). */
108
+ export declare const emailSendOutput: z.ZodObject<{
109
+ exchangeId: z.ZodString;
110
+ }, z.core.$strip>;
111
+ export type EmailSendResult = z.infer<typeof emailSendOutput>;
112
+ /** Platform email capability, reached from scripts as `ctx.api.email`: the history, and sending one now. */
89
113
  export type EmailHistoryApi = {
90
114
  list(input: EmailListParams): Promise<EmailListResult>;
91
115
  get(id: string): Promise<EmailRecord | null>;
116
+ send(input: EmailSendParams): Promise<EmailSendResult>;
92
117
  };
93
118
  export declare const emailHistoryMockService: EmailHistoryApi;
94
119
  export declare const emailHistorySurface: {
@@ -153,6 +178,20 @@ export declare const emailHistorySurface: {
153
178
  occurredAt: z.ZodString;
154
179
  }, z.core.$strip>>;
155
180
  };
181
+ send: {
182
+ input: z.ZodObject<{
183
+ to: z.ZodUnion<readonly [z.ZodString, z.ZodObject<{
184
+ address: z.ZodString;
185
+ }, z.core.$strict>]>;
186
+ body: z.ZodString;
187
+ subject: z.ZodOptional<z.ZodString>;
188
+ instanceId: z.ZodOptional<z.ZodString>;
189
+ from: z.ZodOptional<z.ZodString>;
190
+ }, z.core.$strict>;
191
+ output: z.ZodObject<{
192
+ exchangeId: z.ZodString;
193
+ }, z.core.$strip>;
194
+ };
156
195
  };
157
196
  mock: EmailHistoryApi;
158
197
  };
@@ -41,6 +41,29 @@ export const emailListOutput = z.object({
41
41
  });
42
42
  export const emailGetInput = z.string().min(1);
43
43
  export const emailGetOutput = emailRecord.nullable();
44
+ /**
45
+ * An email sent now (64 §15), from any entry, as a text is (`ctx.api.sms.send`): one recipient, no cc or bcc.
46
+ * `to` is a contact's row id, or `{ address }` for someone who is not one (an on-call lawyer, opposing
47
+ * counsel). An address that is a contact's of this environment is sent as to that contact: their opt-outs
48
+ * are checked. `from` names the mailbox (its name in the environment); absent, a contact's own mailbox (one
49
+ * picked at random is remembered on them), else any active one. It goes back in the newest thread with
50
+ * them on that mailbox, else starts one; stored with the run, then sent. The case it is about, and who
51
+ * hears `email.sent`: as for a text. Pause and contact hours are not checked.
52
+ */
53
+ export const emailSendInput = z
54
+ .object({
55
+ to: z.union([z.string().min(1), z.object({ address: z.string().min(1) }).strict()]),
56
+ body: z.string().min(1),
57
+ /** Absent: a reply to the thread's subject, or a plain one for a new thread. */
58
+ subject: z.string().optional(),
59
+ /** The case the email is about, any of this environment's; absent: the run's own case, if any. */
60
+ instanceId: z.string().min(1).optional(),
61
+ /** The mailbox to send from, by its name in the environment. */
62
+ from: z.string().min(1).optional(),
63
+ })
64
+ .strict();
65
+ /** The thread the email went on (`email.sent` on the case's timeline). */
66
+ export const emailSendOutput = z.object({ exchangeId: z.string() });
44
67
  export const emailHistoryMockService = {
45
68
  async list(input) {
46
69
  return { emails: [], offset: input.offset, limit: input.limit };
@@ -48,11 +71,15 @@ export const emailHistoryMockService = {
48
71
  async get() {
49
72
  return null;
50
73
  },
74
+ async send() {
75
+ return { exchangeId: 'mock-email' };
76
+ },
51
77
  };
52
78
  export const emailHistorySurface = {
53
79
  methods: {
54
80
  list: { input: emailListInput, output: emailListOutput },
55
81
  get: { input: emailGetInput, output: emailGetOutput },
82
+ send: { input: emailSendInput, output: emailSendOutput },
56
83
  },
57
84
  mock: emailHistoryMockService,
58
85
  };
@@ -83,12 +83,24 @@ export declare const emailGetAttachmentOutput: z.ZodObject<{
83
83
  data: z.ZodString;
84
84
  }, z.core.$strip>;
85
85
  export type EmailGetAttachmentOutput = z.infer<typeof emailGetAttachmentOutput>;
86
+ /**
87
+ * A mailbox as company code reaches it (`ctx.api.integration.emails[]`): reading its messages and their
88
+ * attachments. Sending is `ctx.api.email.send` (64 §15): opt-outs checked, stored, threaded, on the case.
89
+ */
86
90
  export type EmailService = {
87
- sendEmail(input: EmailSendEmailInput): Promise<EmailSendEmailOutput>;
88
91
  getMessage(input: EmailGetMessageInput): Promise<EmailGetMessageOutput>;
89
92
  listMessages(input: EmailListMessagesInput): Promise<EmailListMessagesOutput>;
90
93
  getAttachment(input: EmailGetAttachmentInput): Promise<EmailGetAttachmentOutput>;
91
94
  };
95
+ /**
96
+ * The mail provider as the platform drives it: the reads, and handing an email to Gmail / Outlook as is
97
+ * (no opt-out check, no record). The platform's own; company code never reaches `sendEmail`.
98
+ */
99
+ export type EmailProviderService = EmailService & {
100
+ sendEmail(input: EmailSendEmailInput): Promise<EmailSendEmailOutput>;
101
+ };
102
+ /** The provider methods only the platform calls: left out of the company's script surface. */
103
+ export declare const EMAIL_PLATFORM_ONLY_METHODS: readonly ["sendEmail"];
92
104
  export declare const emailMockService: EmailService;
93
105
  export type EmailApi = IntegrationAccountFields & EmailService & {
94
106
  readonly address: string;
@@ -61,10 +61,9 @@ export const emailGetAttachmentOutput = z.object({
61
61
  contentType: z.string(),
62
62
  data: z.string(),
63
63
  });
64
+ /** The provider methods only the platform calls: left out of the company's script surface. */
65
+ export const EMAIL_PLATFORM_ONLY_METHODS = ['sendEmail'];
64
66
  export const emailMockService = {
65
- async sendEmail() {
66
- return { id: 'mock-email-id', threadId: 'mock-thread' };
67
- },
68
67
  async getMessage() {
69
68
  return {
70
69
  id: 'mock-email-id',