maka-agent 0.2.0-dev.14.20260902 → 0.2.0-dev.16.20260903

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 (160) hide show
  1. package/dist/cli-core.js +0 -1
  2. package/dist/pi-transcript.js +48 -0
  3. package/dist/pi-tui-form-interaction.js +497 -0
  4. package/dist/pi-tui-pickers.js +256 -55
  5. package/dist/pi-tui-runner.js +215 -12
  6. package/dist/runtime-host-cli.js +17 -42
  7. package/dist/runtime-host-lifecycle-transaction.js +31 -13
  8. package/dist/runtime-host-managed-lifecycle-manager.js +1 -1
  9. package/dist/runtime-host-onboarding.js +3 -1
  10. package/dist/runtime-host-openrc-service.js +469 -0
  11. package/dist/runtime-host-run-command.js +6 -2
  12. package/dist/runtime-host-service-command.js +3 -3
  13. package/dist/runtime-host-service-management-command.js +43 -15
  14. package/dist/runtime-host-session-channel.js +3 -0
  15. package/dist/runtime-host-session-driver.js +21 -6
  16. package/dist/runtime-host-setup-command.js +30 -31
  17. package/dist/tui-context-refresh.js +103 -0
  18. package/dist/tui-copy-catalog.js +73 -0
  19. package/native/runtime-host-peer/prebuilds/darwin-arm64/maka_runtime_host_peer.node +0 -0
  20. package/native/runtime-host-peer/prebuilds/linux-arm64/maka_runtime_host_peer.node +0 -0
  21. package/native/runtime-host-peer/prebuilds/linux-x64/maka_runtime_host_peer.node +0 -0
  22. package/native/runtime-host-peer/prebuilds/win32-x64/maka_runtime_host_peer.node +0 -0
  23. package/node_modules/@maka/core/dist/chat-model-choice.js +4 -0
  24. package/node_modules/@maka/core/dist/computer-use.js +27 -92
  25. package/node_modules/@maka/core/dist/interaction.js +577 -3
  26. package/node_modules/@maka/core/dist/llm-connections.js +4 -0
  27. package/node_modules/@maka/core/dist/local-memory.js +2 -2
  28. package/node_modules/@maka/core/dist/model-call-usage-projection.js +3 -0
  29. package/node_modules/@maka/core/dist/model-metadata.js +26 -7
  30. package/node_modules/@maka/core/dist/model-thinking.js +17 -0
  31. package/node_modules/@maka/core/dist/provider-registry.js +1 -0
  32. package/node_modules/@maka/core/dist/runtime-event.js +23 -1
  33. package/node_modules/@maka/core/dist/scheduled-task.js +4 -0
  34. package/node_modules/@maka/core/dist/session.js +95 -2
  35. package/node_modules/@maka/core/dist/text-sanitize.js +19 -0
  36. package/node_modules/@maka/core/dist/usage-ledger-merge.js +3 -0
  37. package/node_modules/@maka/core/dist/usage-record-schema.js +11 -5
  38. package/node_modules/@maka/core/dist/workhub-creation-intent.js +75 -9
  39. package/node_modules/@maka/core/dist/workhub-session-resolver.js +50 -0
  40. package/node_modules/@maka/core/package.json +2 -0
  41. package/node_modules/@maka/eval/dist/harbor-external-subject.js +2 -1
  42. package/node_modules/@maka/eval/dist/harness-executor.js +3 -2
  43. package/node_modules/@maka/eval/dist/install-preflight.js +39 -10
  44. package/node_modules/@maka/eval/dist/process-termination.js +83 -0
  45. package/node_modules/@maka/mcp/dist/sep-2243.js +3 -3
  46. package/node_modules/@maka/runtime/dist/agent-run.js +6 -3
  47. package/node_modules/@maka/runtime/dist/ai-sdk-backend.js +332 -118
  48. package/node_modules/@maka/runtime/dist/ai-sdk-compaction-contract.js +6 -1
  49. package/node_modules/@maka/runtime/dist/ai-sdk-compaction.js +171 -341
  50. package/node_modules/@maka/runtime/dist/bots/telegram-bridge.js +3 -26
  51. package/node_modules/@maka/runtime/dist/bundled-skill-catalog.generated.js +1 -1
  52. package/node_modules/@maka/runtime/dist/computer-use-codec.js +1 -141
  53. package/node_modules/@maka/runtime/dist/computer-use-tools.js +23 -105
  54. package/node_modules/@maka/runtime/dist/context-budget-policy.js +20 -26
  55. package/node_modules/@maka/runtime/dist/context-budget.js +18 -20
  56. package/node_modules/@maka/runtime/dist/conversation-copy.js +23 -19
  57. package/node_modules/@maka/runtime/dist/cua-frame-state.js +1 -69
  58. package/node_modules/@maka/runtime/dist/github-copilot-oauth-enrollment.js +267 -0
  59. package/node_modules/@maka/runtime/dist/history-compact-checkpoint.js +6 -9
  60. package/node_modules/@maka/runtime/dist/history-compact-summarizer.js +67 -23
  61. package/node_modules/@maka/runtime/dist/history-compact-summary-validation.js +11 -18
  62. package/node_modules/@maka/runtime/dist/history-compaction.js +66 -73
  63. package/node_modules/@maka/runtime/dist/interaction-authority.js +69 -0
  64. package/node_modules/@maka/runtime/dist/mcp-tools.js +13 -1
  65. package/node_modules/@maka/runtime/dist/memory-extraction.js +11 -11
  66. package/node_modules/@maka/runtime/dist/model-adapter.js +24 -6
  67. package/node_modules/@maka/runtime/dist/model-factory.js +14 -5
  68. package/node_modules/@maka/runtime/dist/oauth-provider-contracts.js +28 -1
  69. package/node_modules/@maka/runtime/dist/openai-codex-history-compactor.js +7 -6
  70. package/node_modules/@maka/runtime/dist/opencode-session-header.js +29 -0
  71. package/node_modules/@maka/runtime/dist/runtime-event-read-model.js +11 -1
  72. package/node_modules/@maka/runtime/dist/runtime-kernel.js +12 -11
  73. package/node_modules/@maka/runtime/dist/runtime-read-model.js +51 -13
  74. package/node_modules/@maka/runtime/dist/scheduled-task-tools.js +3 -0
  75. package/node_modules/@maka/runtime/dist/session-event-runtime-mapper.js +26 -0
  76. package/node_modules/@maka/runtime/dist/session-manager.js +1 -0
  77. package/node_modules/@maka/runtime/dist/session-projection-helpers.js +16 -1
  78. package/node_modules/@maka/runtime/dist/stream-graph-projection.js +6 -0
  79. package/node_modules/@maka/runtime/dist/stream-graph-read-model.js +5 -0
  80. package/node_modules/@maka/runtime/dist/stream-usage-fallback-fetch.js +74 -0
  81. package/node_modules/@maka/runtime/dist/subscription-credentials.js +51 -4
  82. package/node_modules/@maka/runtime/dist/subscription-model-fetch.js +47 -3
  83. package/node_modules/@maka/runtime/dist/system-prompt/main-session-prompt.js +20 -1
  84. package/node_modules/@maka/runtime/dist/test-connection.js +18 -11
  85. package/node_modules/@maka/runtime/dist/tool-runtime.js +183 -0
  86. package/node_modules/@maka/runtime/package.json +1 -0
  87. package/node_modules/@maka/runtime-host/dist/adapter/session-projector.js +14 -0
  88. package/node_modules/@maka/runtime-host/dist/client/client-capability-channel.js +84 -1
  89. package/node_modules/@maka/runtime-host/dist/client/host-profile.js +68 -51
  90. package/node_modules/@maka/runtime-host/dist/client/index.js +1 -1
  91. package/node_modules/@maka/runtime-host/dist/client/owner-connection-code.js +8 -5
  92. package/node_modules/@maka/runtime-host/dist/client/peer-client.js +342 -36
  93. package/node_modules/@maka/runtime-host/dist/client/reconnect-lifecycle.js +60 -4
  94. package/node_modules/@maka/runtime-host/dist/peer-mesh/index.js +3 -3
  95. package/node_modules/@maka/runtime-host/dist/peer-mesh/limits.js +1 -1
  96. package/node_modules/@maka/runtime-host/dist/peer-mesh/model.js +48 -60
  97. package/node_modules/@maka/runtime-host/dist/peer-mesh/node.js +847 -431
  98. package/node_modules/@maka/runtime-host/dist/peer-mesh/owner.js +33 -74
  99. package/node_modules/@maka/runtime-host/dist/peer-mesh/store.js +82 -69
  100. package/node_modules/@maka/runtime-host/dist/peer-reachability/index.js +21 -0
  101. package/node_modules/@maka/runtime-host/dist/peer-reachability/model.js +202 -0
  102. package/node_modules/@maka/runtime-host/dist/peer-reachability/owner.js +119 -0
  103. package/node_modules/@maka/runtime-host/dist/peer-reachability/publisher.js +245 -0
  104. package/node_modules/@maka/runtime-host/dist/protocol/agent-graph.js +4 -1
  105. package/node_modules/@maka/runtime-host/dist/protocol/client-capability.js +68 -0
  106. package/node_modules/@maka/runtime-host/dist/protocol/connection-effects.js +15 -8
  107. package/node_modules/@maka/runtime-host/dist/protocol/host-status.js +7 -23
  108. package/node_modules/@maka/runtime-host/dist/protocol/index.js +47 -1
  109. package/node_modules/@maka/runtime-host/dist/protocol/oauth.js +25 -1
  110. package/node_modules/@maka/runtime-host/dist/protocol/operations.js +1 -0
  111. package/node_modules/@maka/runtime-host/dist/protocol/peer-mesh.js +14 -31
  112. package/node_modules/@maka/runtime-host/dist/protocol/scheduled-task.js +19 -6
  113. package/node_modules/@maka/runtime-host/dist/protocol/session-collaboration.js +36 -1
  114. package/node_modules/@maka/runtime-host/dist/protocol/session-revision.js +11 -2
  115. package/node_modules/@maka/runtime-host/dist/protocol/usage-pricing.js +16 -2
  116. package/node_modules/@maka/runtime-host/dist/protocol/workhub-coordination.js +51 -2
  117. package/node_modules/@maka/runtime-host/dist/server/access-authority.js +35 -2
  118. package/node_modules/@maka/runtime-host/dist/server/access-credential-store.js +1 -0
  119. package/node_modules/@maka/runtime-host/dist/server/client-capability-coordinator.js +12 -10
  120. package/node_modules/@maka/runtime-host/dist/server/client-capability-invocation-broker.js +163 -30
  121. package/node_modules/@maka/runtime-host/dist/server/connection-effect-coordinator.js +38 -14
  122. package/node_modules/@maka/runtime-host/dist/server/daily-review-coordinator.js +45 -22
  123. package/node_modules/@maka/runtime-host/dist/server/execution-composition.js +78 -20
  124. package/node_modules/@maka/runtime-host/dist/server/execution-model-authority.js +4 -2
  125. package/node_modules/@maka/runtime-host/dist/server/execution-model-composition.js +2 -1
  126. package/node_modules/@maka/runtime-host/dist/server/execution-service.js +49 -28
  127. package/node_modules/@maka/runtime-host/dist/server/host-kernel.js +3 -6
  128. package/node_modules/@maka/runtime-host/dist/server/interaction-coordinator.js +130 -27
  129. package/node_modules/@maka/runtime-host/dist/server/interaction-projection.js +30 -1
  130. package/node_modules/@maka/runtime-host/dist/server/listener-set.js +2 -4
  131. package/node_modules/@maka/runtime-host/dist/server/message-coordinator.js +16 -3
  132. package/node_modules/@maka/runtime-host/dist/server/oauth-coordinator.js +66 -3
  133. package/node_modules/@maka/runtime-host/dist/server/oauth-execution-authority.js +20 -3
  134. package/node_modules/@maka/runtime-host/dist/server/peer-listener.js +8 -21
  135. package/node_modules/@maka/runtime-host/dist/server/peer-mesh-authority.js +1 -1
  136. package/node_modules/@maka/runtime-host/dist/server/root-turn-coordinator.js +6 -3
  137. package/node_modules/@maka/runtime-host/dist/server/scheduled-task-coordinator.js +72 -12
  138. package/node_modules/@maka/runtime-host/dist/server/session-revision-coordinator.js +48 -18
  139. package/node_modules/@maka/runtime-host/dist/server/session-turn-access-request-coordinator.js +43 -26
  140. package/node_modules/@maka/runtime-host/dist/server/usage-pricing-coordinator.js +15 -1
  141. package/node_modules/@maka/runtime-host/dist/server/workhub-coordination-action-gate.js +229 -4
  142. package/node_modules/@maka/runtime-host/dist/server/workhub-coordination-coordinator.js +146 -8
  143. package/node_modules/@maka/runtime-host/dist/transport/peer-native.js +32 -6
  144. package/node_modules/@maka/runtime-host/package.json +1 -0
  145. package/node_modules/@maka/storage/dist/agent-run-store.js +18 -5
  146. package/node_modules/@maka/storage/dist/codex-session-adapter.js +14 -0
  147. package/node_modules/@maka/storage/dist/execution-stores.js +5 -0
  148. package/node_modules/@maka/storage/dist/interaction-store.js +13 -1
  149. package/node_modules/@maka/storage/dist/runtime-policy/connection-catalog-document.js +9 -3
  150. package/node_modules/@maka/storage/dist/runtime-policy/coordinator.js +49 -19
  151. package/node_modules/@maka/storage/dist/runtime-policy/oauth-login-receipt-document.js +3 -1
  152. package/node_modules/@maka/storage/dist/runtime-policy/onboarding-transaction.js +13 -5
  153. package/node_modules/@maka/storage/dist/session-copy-cleanup.js +4 -2
  154. package/node_modules/@maka/storage/dist/session-store.js +39 -6
  155. package/node_modules/@maka/storage/dist/sqlite-runtime-store.js +16 -9
  156. package/node_modules/@maka/storage/dist/sqlite-session-metadata-schema.js +30 -3
  157. package/node_modules/@maka/storage/dist/sqlite-session-metadata-store.js +117 -0
  158. package/node_modules/@maka/storage/dist/sqlite-usage-store.js +29 -6
  159. package/node_modules/@maka/storage/dist/usage-stores.js +3 -1
  160. package/package.json +1 -1
@@ -36,9 +36,19 @@ export const INTERACTION_SANDBOX_BOUNDARY_JUSTIFICATION_MAX_CHARS = 2_000;
36
36
  export const INTERACTION_ANSWER_SERIALIZED_MAX_BYTES = 8 * 1024;
37
37
  export const INTERACTION_OUTCOME_SERIALIZED_MAX_BYTES = 8 * 1024;
38
38
  export const INTERACTION_AUTO_REVIEW_RATIONALE_MAX_CHARS = 1_000;
39
+ export const INTERACTION_FORM_MAX_FIELDS = 32;
40
+ export const INTERACTION_FORM_MAX_OPTIONS = 64;
41
+ export const INTERACTION_FORM_MESSAGE_MAX_BYTES = 2_048;
42
+ export const INTERACTION_FORM_REQUESTER_NAME_MAX_BYTES = 256;
43
+ export const INTERACTION_FORM_REQUESTER_SOURCE_MAX_BYTES = 512;
44
+ export const INTERACTION_FORM_FIELD_NAME_MAX_BYTES = 256;
45
+ export const INTERACTION_FORM_FIELD_LABEL_MAX_BYTES = 256;
46
+ export const INTERACTION_FORM_FIELD_DESCRIPTION_MAX_BYTES = 512;
47
+ export const INTERACTION_FORM_VALUE_MAX_BYTES = 2_048;
39
48
  export const INTERACTION_CLOSURE_REASONS = [
40
49
  'turn_stopped',
41
50
  'turn_terminal',
51
+ 'producer_cancelled',
42
52
  'timed_out',
43
53
  'host_restarted',
44
54
  'provider_disconnected',
@@ -46,19 +56,33 @@ export const INTERACTION_CLOSURE_REASONS = [
46
56
  const UTF8 = new TextEncoder();
47
57
  const PERMISSION_REQUEST_SHAPE = defineObjectShape()(['kind', 'toolUseId', 'prompt'], []);
48
58
  const QUESTION_REQUEST_SHAPE = defineObjectShape()(['kind', 'toolUseId', 'questions'], []);
59
+ const FORM_REQUEST_SHAPE = defineObjectShape()(['kind', 'toolUseId', 'message', 'requester', 'fields'], []);
49
60
  const SANDBOX_BOUNDARY_REQUEST_SHAPE = defineObjectShape()(['kind', 'expansion', 'justification'], []);
50
61
  const CLIENT_CAPABILITY_REQUEST_SHAPE = defineObjectShape()(['kind', 'toolUseId', 'target'], []);
51
62
  const PERMISSION_ANSWER_SHAPE = defineObjectShape()(['kind', 'decision', 'rememberForTurn'], []);
52
63
  const QUESTION_ANSWER_SHAPE = defineObjectShape()(['kind', 'answers'], []);
64
+ const FORM_ACCEPT_ANSWER_SHAPE = defineObjectShape()(['kind', 'action', 'values'], []);
65
+ const FORM_EMPTY_ANSWER_SHAPE = defineObjectShape()(['kind', 'action'], []);
66
+ const FORM_ACCEPT_RESPONSE_SHAPE = defineObjectShape()(['requestId', 'action', 'values'], []);
67
+ const FORM_EMPTY_RESPONSE_SHAPE = defineObjectShape()(['requestId', 'action'], []);
53
68
  const SANDBOX_BOUNDARY_ANSWER_SHAPE = defineObjectShape()(['kind', 'decision'], []);
54
69
  const CLIENT_CAPABILITY_ANSWER_SHAPE = defineObjectShape()(['kind', 'decision'], []);
55
70
  const PERMISSION_OUTCOME_SHAPE = defineObjectShape()(['kind', 'reviewer', 'committedAt', 'decision', 'rememberForTurn'], ['rationale', 'riskLevel']);
56
71
  const QUESTION_OUTCOME_SHAPE = defineObjectShape()(['kind', 'answers', 'committedAt'], []);
72
+ const FORM_ACCEPT_OUTCOME_SHAPE = defineObjectShape()(['kind', 'action', 'values', 'committedAt'], []);
73
+ const FORM_EMPTY_OUTCOME_SHAPE = defineObjectShape()(['kind', 'action', 'committedAt'], []);
57
74
  const SANDBOX_BOUNDARY_OUTCOME_SHAPE = defineObjectShape()(['kind', 'decision', 'status', 'committedAt'], []);
58
75
  const CLIENT_CAPABILITY_OUTCOME_SHAPE = defineObjectShape()(['kind', 'decision', 'committedAt'], []);
59
76
  const CLOSURE_OUTCOME_SHAPE = defineObjectShape()(['kind', 'reason', 'committedAt'], []);
60
77
  const QUESTION_SHAPE = defineObjectShape()(['question', 'options'], []);
61
78
  const OPTION_SHAPE = defineObjectShape()(['label'], ['description']);
79
+ const FORM_REQUESTER_SHAPE = defineObjectShape()(['name'], ['source']);
80
+ const FORM_OPTION_SHAPE = defineObjectShape()(['value', 'label'], []);
81
+ const FORM_STRING_FIELD_SHAPE = defineObjectShape()(['kind', 'name', 'label', 'required'], ['description', 'default', 'minLength', 'maxLength', 'format']);
82
+ const FORM_NUMBER_FIELD_SHAPE = defineObjectShape()(['kind', 'name', 'label', 'required'], ['description', 'default', 'minimum', 'maximum']);
83
+ const FORM_BOOLEAN_FIELD_SHAPE = defineObjectShape()(['kind', 'name', 'label', 'required'], ['description', 'default']);
84
+ const FORM_SINGLE_SELECT_FIELD_SHAPE = defineObjectShape()(['kind', 'name', 'label', 'required', 'options'], ['description', 'default']);
85
+ const FORM_MULTI_SELECT_FIELD_SHAPE = defineObjectShape()(['kind', 'name', 'label', 'required', 'options'], ['description', 'default', 'minItems', 'maxItems']);
62
86
  export function decodeInteractionRequest(value) {
63
87
  const record = plainRecord(value, 'Interaction request');
64
88
  let request;
@@ -78,6 +102,20 @@ export function decodeInteractionRequest(value) {
78
102
  questions: plainArray(record.questions, 'questions', INTERACTION_MIN_QUESTIONS, INTERACTION_MAX_QUESTIONS).map(decodeQuestion),
79
103
  };
80
104
  }
105
+ else if (record.kind === 'form') {
106
+ exact(record, FORM_REQUEST_SHAPE, 'form request');
107
+ const fields = plainArray(record.fields, 'form fields', 0, INTERACTION_FORM_MAX_FIELDS).map(decodeFormField);
108
+ if (new Set(fields.map((field) => field.name)).size !== fields.length) {
109
+ throw new Error('Duplicate form field name');
110
+ }
111
+ request = {
112
+ kind: 'form',
113
+ toolUseId: boundedString(record.toolUseId, 'toolUseId', INTERACTION_ID_MAX_BYTES),
114
+ message: boundedString(record.message, 'form message', INTERACTION_FORM_MESSAGE_MAX_BYTES),
115
+ requester: decodeFormRequester(record.requester),
116
+ fields,
117
+ };
118
+ }
81
119
  else if (record.kind === 'sandbox_boundary') {
82
120
  exact(record, SANDBOX_BOUNDARY_REQUEST_SHAPE, 'sandbox boundary request');
83
121
  const expansion = validateSandboxBoundaryExpansion(record.expansion);
@@ -100,6 +138,8 @@ export function decodeInteractionRequest(value) {
100
138
  else {
101
139
  throw new Error('Invalid Interaction request kind');
102
140
  }
141
+ if (request.kind === 'form')
142
+ assertFormHasAcceptedAnswer(request);
103
143
  if (request.kind !== 'sandbox_boundary') {
104
144
  serializedLimit(request, INTERACTION_REQUEST_MAX_BYTES, 'Interaction request');
105
145
  }
@@ -123,6 +163,17 @@ export function decodeInteractionAnswer(value) {
123
163
  exact(record, QUESTION_ANSWER_SHAPE, 'question answer');
124
164
  answer = { kind: 'question', answers: decodeAnswers(record.answers) };
125
165
  }
166
+ else if (record.kind === 'form') {
167
+ const action = oneOf(record.action, ['accept', 'decline', 'cancel'], 'form action');
168
+ if (action === 'accept') {
169
+ exact(record, FORM_ACCEPT_ANSWER_SHAPE, 'accepted form answer');
170
+ answer = { kind: 'form', action, values: decodeFormValues(record.values) };
171
+ }
172
+ else {
173
+ exact(record, FORM_EMPTY_ANSWER_SHAPE, 'empty form answer');
174
+ answer = { kind: 'form', action };
175
+ }
176
+ }
126
177
  else if (record.kind === 'sandbox_boundary') {
127
178
  exact(record, SANDBOX_BOUNDARY_ANSWER_SHAPE, 'sandbox boundary answer');
128
179
  answer = {
@@ -141,8 +192,30 @@ export function decodeInteractionAnswer(value) {
141
192
  throw new Error('Invalid Interaction answer kind');
142
193
  }
143
194
  serializedLimit(answer, INTERACTION_ANSWER_SERIALIZED_MAX_BYTES, 'Interaction answer');
195
+ if (answer.kind === 'form' && answer.action === 'accept') {
196
+ assertAcceptedFormAnswerFitsCanonicalOutcome(answer);
197
+ }
144
198
  return deepFreeze(answer);
145
199
  }
200
+ export function decodeInteractionFormResponse(value) {
201
+ const record = plainRecord(value, 'Interaction form response');
202
+ const requestId = boundedString(record.requestId, 'requestId', INTERACTION_ID_MAX_BYTES);
203
+ const action = oneOf(record.action, ['accept', 'decline', 'cancel'], 'form action');
204
+ if (action === 'accept') {
205
+ exact(record, FORM_ACCEPT_RESPONSE_SHAPE, 'accepted form response');
206
+ const answer = decodeInteractionAnswer({
207
+ kind: 'form',
208
+ action,
209
+ values: record.values,
210
+ });
211
+ if (answer.kind !== 'form' || answer.action !== 'accept') {
212
+ throw new Error('Invalid accepted form response');
213
+ }
214
+ return deepFreeze({ requestId, action, values: answer.values });
215
+ }
216
+ exact(record, FORM_EMPTY_RESPONSE_SHAPE, 'empty form response');
217
+ return deepFreeze({ requestId, action });
218
+ }
146
219
  export function decodeInteractionCanonicalOutcome(value) {
147
220
  const record = plainRecord(value, 'Interaction canonical outcome');
148
221
  let outcome;
@@ -184,6 +257,23 @@ export function decodeInteractionCanonicalOutcome(value) {
184
257
  committedAt: safeInteger(record.committedAt, 'committedAt', false),
185
258
  };
186
259
  }
260
+ else if (record.kind === 'form_answer') {
261
+ const action = oneOf(record.action, ['accept', 'decline', 'cancel'], 'form action');
262
+ const committedAt = safeInteger(record.committedAt, 'committedAt', false);
263
+ if (action === 'accept') {
264
+ exact(record, FORM_ACCEPT_OUTCOME_SHAPE, 'accepted form outcome');
265
+ outcome = {
266
+ kind: 'form_answer',
267
+ action,
268
+ values: decodeFormValues(record.values),
269
+ committedAt,
270
+ };
271
+ }
272
+ else {
273
+ exact(record, FORM_EMPTY_OUTCOME_SHAPE, 'empty form outcome');
274
+ outcome = { kind: 'form_answer', action, committedAt };
275
+ }
276
+ }
187
277
  else if (record.kind === 'sandbox_boundary_decision') {
188
278
  exact(record, SANDBOX_BOUNDARY_OUTCOME_SHAPE, 'sandbox boundary outcome');
189
279
  const status = oneOf(record.status, SANDBOX_BOUNDARY_REQUEST_STATUSES, 'sandbox boundary status');
@@ -271,6 +361,56 @@ export function projectInteractionQuestionRequest(request) {
271
361
  };
272
362
  return decodeInteractionRequest(projected);
273
363
  }
364
+ export function projectInteractionFormRequest(input) {
365
+ const decoded = decodeInteractionRequest({ kind: 'form', ...input });
366
+ return decodeInteractionRequest({
367
+ ...decoded,
368
+ message: projectInteractionReviewText(decoded.message, INTERACTION_FORM_MESSAGE_MAX_BYTES),
369
+ requester: {
370
+ name: projectInteractionReviewText(decoded.requester.name, INTERACTION_FORM_REQUESTER_NAME_MAX_BYTES),
371
+ ...(decoded.requester.source === undefined
372
+ ? {}
373
+ : {
374
+ source: projectInteractionReviewText(decoded.requester.source, INTERACTION_FORM_REQUESTER_SOURCE_MAX_BYTES, true),
375
+ }),
376
+ },
377
+ fields: decoded.fields.map(projectInteractionFormField),
378
+ });
379
+ }
380
+ function projectInteractionFormField(field) {
381
+ const display = {
382
+ label: projectInteractionReviewText(field.label, INTERACTION_FORM_FIELD_LABEL_MAX_BYTES),
383
+ ...(field.description === undefined
384
+ ? {}
385
+ : {
386
+ description: projectInteractionReviewText(field.description, INTERACTION_FORM_FIELD_DESCRIPTION_MAX_BYTES, true),
387
+ }),
388
+ };
389
+ if (field.kind !== 'single_select' && field.kind !== 'multi_select') {
390
+ // `name` is a protocol identity returned to the tool. A string default is
391
+ // both display text and a canonical answer value, so a safety rewrite must
392
+ // not silently change its semantics under the original constraints.
393
+ if (field.kind === 'string' && field.default !== undefined) {
394
+ const { default: canonicalDefault, ...fieldWithoutDefault } = field;
395
+ const projectedDefault = projectInteractionReviewText(canonicalDefault, INTERACTION_FORM_VALUE_MAX_BYTES, true);
396
+ return {
397
+ ...fieldWithoutDefault,
398
+ ...display,
399
+ ...(projectedDefault === canonicalDefault ? { default: canonicalDefault } : {}),
400
+ };
401
+ }
402
+ return { ...field, ...display };
403
+ }
404
+ // Select values are protocol identities; labels are their display text.
405
+ const options = field.options.map((option) => ({
406
+ ...option,
407
+ label: projectInteractionReviewText(option.label, INTERACTION_FORM_FIELD_LABEL_MAX_BYTES),
408
+ }));
409
+ if (new Set(options.map((option) => option.label)).size !== options.length) {
410
+ throw new Error('Form option labels collide after safe projection');
411
+ }
412
+ return { ...field, ...display, options };
413
+ }
274
414
  export function projectInteractionSandboxBoundaryRequest(input) {
275
415
  return decodeInteractionRequest({
276
416
  kind: 'sandbox_boundary',
@@ -294,9 +434,11 @@ export function interactionOutcomeMatchesRequestKind(request, outcome) {
294
434
  ? outcome.kind === 'permission_answer'
295
435
  : request.kind === 'question'
296
436
  ? outcome.kind === 'question_answer'
297
- : request.kind === 'sandbox_boundary'
298
- ? outcome.kind === 'sandbox_boundary_decision'
299
- : outcome.kind === 'client_capability_decision'));
437
+ : request.kind === 'form'
438
+ ? outcome.kind === 'form_answer'
439
+ : request.kind === 'sandbox_boundary'
440
+ ? outcome.kind === 'sandbox_boundary_decision'
441
+ : outcome.kind === 'client_capability_decision'));
300
442
  }
301
443
  export function interactionQuestionAnswerCountMatchesRequest(request, answers) {
302
444
  return request.questions.length === answers.length;
@@ -317,6 +459,9 @@ export function isInteractionAnswerValidForRequest(request, answer) {
317
459
  return (request.kind === 'question' &&
318
460
  interactionQuestionAnswerCountMatchesRequest(request, answer.answers));
319
461
  }
462
+ if (answer.kind === 'form') {
463
+ return request.kind === 'form' && interactionFormAnswerMatchesRequest(request, answer);
464
+ }
320
465
  if (answer.kind === 'sandbox_boundary')
321
466
  return request.kind === 'sandbox_boundary';
322
467
  if (answer.kind === 'client_capability')
@@ -337,6 +482,12 @@ export function isInteractionCanonicalOutcomeValidForRequest(request, outcome) {
337
482
  return (request.kind === 'question' &&
338
483
  interactionQuestionAnswerCountMatchesRequest(request, outcome.answers));
339
484
  }
485
+ if (outcome.kind === 'form_answer') {
486
+ return (request.kind === 'form' &&
487
+ interactionFormAnswerMatchesRequest(request, outcome.action === 'accept'
488
+ ? { kind: 'form', action: 'accept', values: outcome.values }
489
+ : { kind: 'form', action: outcome.action }));
490
+ }
340
491
  if (outcome.kind === 'client_capability_decision') {
341
492
  return request.kind === 'client_capability';
342
493
  }
@@ -349,6 +500,11 @@ export function interactionCanonicalOutcomesEquivalent(left, right) {
349
500
  return left.decision === right.decision && left.rememberForTurn === right.rememberForTurn;
350
501
  if (left.kind === 'question_answer' && right.kind === 'question_answer')
351
502
  return equalAnswers(left.answers, right.answers);
503
+ if (left.kind === 'form_answer' && right.kind === 'form_answer') {
504
+ return (left.action === right.action &&
505
+ (left.action !== 'accept' ||
506
+ (right.action === 'accept' && equalFormValues(left.values, right.values))));
507
+ }
352
508
  if (left.kind === 'sandbox_boundary_decision' && right.kind === 'sandbox_boundary_decision') {
353
509
  return left.decision === right.decision && left.status === right.status;
354
510
  }
@@ -380,6 +536,387 @@ function decodeOption(value) {
380
536
  }),
381
537
  });
382
538
  }
539
+ function decodeFormRequester(value) {
540
+ const record = plainRecord(value, 'Interaction form requester');
541
+ exact(record, FORM_REQUESTER_SHAPE, 'form requester');
542
+ return Object.freeze({
543
+ name: boundedString(record.name, 'form requester name', INTERACTION_FORM_REQUESTER_NAME_MAX_BYTES),
544
+ ...(record.source === undefined
545
+ ? {}
546
+ : {
547
+ source: boundedText(record.source, 'form requester source', INTERACTION_FORM_REQUESTER_SOURCE_MAX_BYTES),
548
+ }),
549
+ });
550
+ }
551
+ function decodeFormField(value) {
552
+ const record = plainRecord(value, 'Interaction form field');
553
+ const common = decodeFormFieldBase(record);
554
+ if (record.kind === 'string') {
555
+ exact(record, FORM_STRING_FIELD_SHAPE, 'string form field');
556
+ const minLength = optionalBoundedCount(record.minLength, 'minLength');
557
+ const maxLength = optionalBoundedCount(record.maxLength, 'maxLength');
558
+ if (minLength !== undefined && maxLength !== undefined && minLength > maxLength) {
559
+ throw new Error('Invalid form string length range');
560
+ }
561
+ const field = {
562
+ ...common,
563
+ kind: 'string',
564
+ ...(record.default === undefined
565
+ ? {}
566
+ : {
567
+ default: boundedText(record.default, 'form string default', INTERACTION_FORM_VALUE_MAX_BYTES),
568
+ }),
569
+ ...(minLength === undefined ? {} : { minLength }),
570
+ ...(maxLength === undefined ? {} : { maxLength }),
571
+ ...(record.format === undefined
572
+ ? {}
573
+ : {
574
+ format: oneOf(record.format, ['email', 'uri', 'date', 'date-time'], 'form string format'),
575
+ }),
576
+ };
577
+ if (field.default !== undefined && !isInteractionFormFieldValueValid(field, field.default)) {
578
+ throw new Error('Invalid form string default');
579
+ }
580
+ return deepFreeze(field);
581
+ }
582
+ if (record.kind === 'number' || record.kind === 'integer') {
583
+ exact(record, FORM_NUMBER_FIELD_SHAPE, 'number form field');
584
+ const minimum = optionalFiniteNumber(record.minimum, 'minimum');
585
+ const maximum = optionalFiniteNumber(record.maximum, 'maximum');
586
+ if (minimum !== undefined && maximum !== undefined && minimum > maximum) {
587
+ throw new Error('Invalid form number range');
588
+ }
589
+ const field = {
590
+ ...common,
591
+ kind: record.kind,
592
+ ...(record.default === undefined
593
+ ? {}
594
+ : { default: finiteNumber(record.default, 'form number default') }),
595
+ ...(minimum === undefined ? {} : { minimum }),
596
+ ...(maximum === undefined ? {} : { maximum }),
597
+ };
598
+ if (field.default !== undefined && !isInteractionFormFieldValueValid(field, field.default)) {
599
+ throw new Error('Invalid form number default');
600
+ }
601
+ return deepFreeze(field);
602
+ }
603
+ if (record.kind === 'boolean') {
604
+ exact(record, FORM_BOOLEAN_FIELD_SHAPE, 'boolean form field');
605
+ return Object.freeze({
606
+ ...common,
607
+ kind: 'boolean',
608
+ ...(record.default === undefined
609
+ ? {}
610
+ : { default: boolean(record.default, 'form boolean default') }),
611
+ });
612
+ }
613
+ if (record.kind === 'single_select') {
614
+ exact(record, FORM_SINGLE_SELECT_FIELD_SHAPE, 'single-select form field');
615
+ const options = decodeFormOptions(record.options);
616
+ const field = {
617
+ ...common,
618
+ kind: 'single_select',
619
+ options,
620
+ ...(record.default === undefined
621
+ ? {}
622
+ : {
623
+ default: boundedText(record.default, 'single-select default', INTERACTION_FORM_VALUE_MAX_BYTES),
624
+ }),
625
+ };
626
+ if (field.default !== undefined && !isInteractionFormFieldValueValid(field, field.default)) {
627
+ throw new Error('Invalid single-select default');
628
+ }
629
+ return deepFreeze(field);
630
+ }
631
+ if (record.kind === 'multi_select') {
632
+ exact(record, FORM_MULTI_SELECT_FIELD_SHAPE, 'multi-select form field');
633
+ const options = decodeFormOptions(record.options);
634
+ const minItems = optionalBoundedCount(record.minItems, 'minItems', options.length);
635
+ const maxItems = optionalBoundedCount(record.maxItems, 'maxItems', options.length);
636
+ if (minItems !== undefined && maxItems !== undefined && minItems > maxItems) {
637
+ throw new Error('Invalid multi-select item range');
638
+ }
639
+ const field = {
640
+ ...common,
641
+ kind: 'multi_select',
642
+ options,
643
+ ...(record.default === undefined
644
+ ? {}
645
+ : {
646
+ default: decodeFormStringArray(record.default, 'multi-select default'),
647
+ }),
648
+ ...(minItems === undefined ? {} : { minItems }),
649
+ ...(maxItems === undefined ? {} : { maxItems }),
650
+ };
651
+ if (field.default !== undefined && !isInteractionFormFieldValueValid(field, field.default)) {
652
+ throw new Error('Invalid multi-select default');
653
+ }
654
+ return deepFreeze(field);
655
+ }
656
+ throw new Error('Invalid form field kind');
657
+ }
658
+ function decodeFormFieldBase(record) {
659
+ return {
660
+ name: boundedString(record.name, 'form field name', INTERACTION_FORM_FIELD_NAME_MAX_BYTES),
661
+ label: boundedString(record.label, 'form field label', INTERACTION_FORM_FIELD_LABEL_MAX_BYTES),
662
+ required: boolean(record.required, 'form field required'),
663
+ ...(record.description === undefined
664
+ ? {}
665
+ : {
666
+ description: boundedText(record.description, 'form field description', INTERACTION_FORM_FIELD_DESCRIPTION_MAX_BYTES),
667
+ }),
668
+ };
669
+ }
670
+ function decodeFormOptions(value) {
671
+ const options = plainArray(value, 'form options', 1, INTERACTION_FORM_MAX_OPTIONS).map((candidate) => {
672
+ const record = plainRecord(candidate, 'Interaction form option');
673
+ exact(record, FORM_OPTION_SHAPE, 'form option');
674
+ return Object.freeze({
675
+ value: boundedText(record.value, 'form option value', INTERACTION_FORM_VALUE_MAX_BYTES),
676
+ label: boundedString(record.label, 'form option label', INTERACTION_FORM_FIELD_LABEL_MAX_BYTES),
677
+ });
678
+ });
679
+ if (new Set(options.map((option) => option.value)).size !== options.length) {
680
+ throw new Error('Duplicate form option value');
681
+ }
682
+ if (new Set(options.map((option) => option.label)).size !== options.length) {
683
+ throw new Error('Duplicate form option label');
684
+ }
685
+ return Object.freeze(options);
686
+ }
687
+ function decodeFormValues(value) {
688
+ const record = plainRecord(value, 'Interaction form values');
689
+ const entries = Object.entries(record);
690
+ if (entries.length > INTERACTION_FORM_MAX_FIELDS)
691
+ throw new Error('Too many form values');
692
+ return deepFreeze(Object.fromEntries(entries.map(([name, candidate]) => [
693
+ boundedString(name, 'form value name', INTERACTION_FORM_FIELD_NAME_MAX_BYTES),
694
+ decodeFormValue(candidate),
695
+ ])));
696
+ }
697
+ function decodeFormValue(value) {
698
+ if (typeof value === 'string') {
699
+ return boundedText(value, 'form string value', INTERACTION_FORM_VALUE_MAX_BYTES);
700
+ }
701
+ if (typeof value === 'number')
702
+ return finiteNumber(value, 'form number value');
703
+ if (typeof value === 'boolean')
704
+ return value;
705
+ return decodeFormStringArray(value, 'form multi-select value');
706
+ }
707
+ function decodeFormStringArray(value, label) {
708
+ const values = plainArray(value, label, 0, INTERACTION_FORM_MAX_OPTIONS).map((candidate) => boundedText(candidate, label, INTERACTION_FORM_VALUE_MAX_BYTES));
709
+ if (new Set(values).size !== values.length)
710
+ throw new Error(`Duplicate ${label}`);
711
+ return Object.freeze(values);
712
+ }
713
+ export function interactionFormAnswerMatchesRequest(request, answer) {
714
+ if (answer.action !== 'accept')
715
+ return true;
716
+ const fields = new Map(request.fields.map((field) => [field.name, field]));
717
+ const names = Object.keys(answer.values);
718
+ if (names.some((name) => !fields.has(name)))
719
+ return false;
720
+ for (const field of request.fields) {
721
+ if (!Object.hasOwn(answer.values, field.name)) {
722
+ if (field.required)
723
+ return false;
724
+ continue;
725
+ }
726
+ if (!isInteractionFormFieldValueValid(field, answer.values[field.name]))
727
+ return false;
728
+ }
729
+ return true;
730
+ }
731
+ export function isInteractionFormFieldValueValid(field, value) {
732
+ if (value === undefined)
733
+ return false;
734
+ if (field.kind === 'string') {
735
+ if (typeof value !== 'string')
736
+ return false;
737
+ const length = [...value].length;
738
+ return ((field.minLength === undefined || length >= field.minLength) &&
739
+ (field.maxLength === undefined || length <= field.maxLength) &&
740
+ matchesStringFormat(value, field.format));
741
+ }
742
+ if (field.kind === 'number' || field.kind === 'integer') {
743
+ return (typeof value === 'number' &&
744
+ Number.isFinite(value) &&
745
+ (field.kind !== 'integer' || Number.isSafeInteger(value)) &&
746
+ (field.minimum === undefined || value >= field.minimum) &&
747
+ (field.maximum === undefined || value <= field.maximum));
748
+ }
749
+ if (field.kind === 'boolean')
750
+ return typeof value === 'boolean';
751
+ if (field.kind === 'single_select') {
752
+ return typeof value === 'string' && field.options.some((option) => option.value === value);
753
+ }
754
+ if (!Array.isArray(value) || value.some((candidate) => typeof candidate !== 'string')) {
755
+ return false;
756
+ }
757
+ return (new Set(value).size === value.length &&
758
+ (field.minItems === undefined || value.length >= field.minItems) &&
759
+ (field.maxItems === undefined || value.length <= field.maxItems) &&
760
+ value.every((candidate) => field.options.some((option) => option.value === candidate)));
761
+ }
762
+ function matchesStringFormat(value, format) {
763
+ if (format === undefined)
764
+ return true;
765
+ if (format === 'email')
766
+ return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
767
+ if (format === 'uri') {
768
+ try {
769
+ return new URL(value).protocol.length > 1;
770
+ }
771
+ catch {
772
+ return false;
773
+ }
774
+ }
775
+ if (format === 'date') {
776
+ const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value);
777
+ if (!match)
778
+ return false;
779
+ return isValidCalendarDate(Number(match[1]), Number(match[2]), Number(match[3]));
780
+ }
781
+ const match = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:Z|[+-](\d{2}):(\d{2}))$/.exec(value);
782
+ if (!match)
783
+ return false;
784
+ return (isValidCalendarDate(Number(match[1]), Number(match[2]), Number(match[3])) &&
785
+ Number(match[4]) <= 23 &&
786
+ Number(match[5]) <= 59 &&
787
+ Number(match[6]) <= 59 &&
788
+ (match[7] === undefined || Number(match[7]) <= 23) &&
789
+ (match[8] === undefined || Number(match[8]) <= 59) &&
790
+ Number.isFinite(Date.parse(value)));
791
+ }
792
+ function isValidCalendarDate(year, month, day) {
793
+ if (month < 1 || month > 12 || day < 1)
794
+ return false;
795
+ const leap = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
796
+ const days = [31, leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
797
+ return day <= days[month - 1];
798
+ }
799
+ /** Reject a form that can be admitted but can never produce a bounded accepted answer. */
800
+ function assertFormHasAcceptedAnswer(request) {
801
+ const values = {};
802
+ for (const field of request.fields) {
803
+ if (!field.required)
804
+ continue;
805
+ values[field.name] = formFieldWitness(field);
806
+ }
807
+ const answer = { kind: 'form', action: 'accept', values };
808
+ if (!interactionFormAnswerMatchesRequest(request, answer)) {
809
+ throw new Error('Interaction form has no valid accepted answer');
810
+ }
811
+ serializedLimit(answer, INTERACTION_ANSWER_SERIALIZED_MAX_BYTES, 'Interaction form answer');
812
+ assertAcceptedFormAnswerFitsCanonicalOutcome(answer);
813
+ assertEveryFormAnswerFitsCanonicalOutcome(request);
814
+ }
815
+ /**
816
+ * Admission must reserve the whole legal answer envelope, not only a smallest
817
+ * witness. This intentionally over-approximates format-constrained strings:
818
+ * rejecting an over-large form is safe, whereas accepting one that can later
819
+ * reject a valid user answer strands the interaction.
820
+ */
821
+ function assertEveryFormAnswerFitsCanonicalOutcome(request) {
822
+ const values = Object.fromEntries(request.fields.map((field) => [field.name, formFieldMaximumEnvelope(field)]));
823
+ const answer = { kind: 'form', action: 'accept', values };
824
+ serializedLimit(answer, INTERACTION_ANSWER_SERIALIZED_MAX_BYTES, 'Interaction form answer');
825
+ assertAcceptedFormAnswerFitsCanonicalOutcome(answer);
826
+ }
827
+ function formFieldMaximumEnvelope(field) {
828
+ if (field.kind === 'string') {
829
+ // Admission must prove every legal answer serializes, so reserve the worst
830
+ // value inside the format's legal language, measured post-serialization.
831
+ // - date is a fixed-length language over [0-9-]; nothing JSON-escapes.
832
+ // - date-time adds only digits and `.:TZ+-`; nothing JSON-escapes, and the
833
+ // fractional seconds leave the length unbounded up to the field caps.
834
+ // - every other string may legally hold control characters, which
835
+ // JSON-escape to six bytes per code point (one raw byte each).
836
+ if (field.format === 'date')
837
+ return '0000-01-01';
838
+ const maximumCodePoints = Math.min(field.maxLength ?? INTERACTION_FORM_VALUE_MAX_BYTES, INTERACTION_FORM_VALUE_MAX_BYTES);
839
+ if (field.format === 'date-time')
840
+ return '0'.repeat(maximumCodePoints);
841
+ return '\u0001'.repeat(maximumCodePoints);
842
+ }
843
+ if (field.kind === 'number' || field.kind === 'integer')
844
+ return -1.7976931348623157e308;
845
+ if (field.kind === 'boolean')
846
+ return false;
847
+ if (field.kind === 'single_select') {
848
+ return field.options.reduce((longest, option) => serializedByteLength(option.value) > serializedByteLength(longest) ? option.value : longest, field.options[0].value);
849
+ }
850
+ return [...field.options]
851
+ .sort((left, right) => serializedByteLength(right.value) - serializedByteLength(left.value))
852
+ .slice(0, field.maxItems ?? field.options.length)
853
+ .map((option) => option.value);
854
+ }
855
+ function serializedByteLength(value) {
856
+ return UTF8.encode(JSON.stringify(value)).byteLength;
857
+ }
858
+ function assertAcceptedFormAnswerFitsCanonicalOutcome(answer) {
859
+ serializedLimit({
860
+ kind: 'form_answer',
861
+ action: 'accept',
862
+ values: answer.values,
863
+ committedAt: Number.MAX_SAFE_INTEGER,
864
+ }, INTERACTION_OUTCOME_SERIALIZED_MAX_BYTES, 'Interaction form outcome');
865
+ }
866
+ function formFieldWitness(field) {
867
+ if (field.default !== undefined)
868
+ return field.default;
869
+ if (field.kind === 'string') {
870
+ const minLength = field.minLength ?? 0;
871
+ let value;
872
+ if (field.format === 'email') {
873
+ value = `${'a'.repeat(Math.max(1, minLength - 5))}@b.co`;
874
+ }
875
+ else if (field.format === 'uri') {
876
+ const base = 'https://a.co/';
877
+ value = `${base}${'a'.repeat(Math.max(0, minLength - base.length))}`;
878
+ }
879
+ else if (field.format === 'date') {
880
+ value = '2000-01-01';
881
+ }
882
+ else if (field.format === 'date-time') {
883
+ const base = '2000-01-01T00:00:00';
884
+ const fractionalLength = minLength <= 20 ? 0 : Math.max(1, minLength - 21);
885
+ value = fractionalLength === 0 ? `${base}Z` : `${base}.${'0'.repeat(fractionalLength)}Z`;
886
+ }
887
+ else {
888
+ value = 'a'.repeat(minLength);
889
+ }
890
+ if (!isInteractionFormFieldValueValid(field, value)) {
891
+ throw new Error(`Interaction form field ${field.name} has no valid bounded value`);
892
+ }
893
+ return value;
894
+ }
895
+ if (field.kind === 'number' || field.kind === 'integer') {
896
+ const lower = field.minimum ?? Number.NEGATIVE_INFINITY;
897
+ const upper = field.maximum ?? Number.POSITIVE_INFINITY;
898
+ const value = field.kind === 'integer'
899
+ ? lower > 0
900
+ ? Math.ceil(lower)
901
+ : upper < 0
902
+ ? Math.floor(upper)
903
+ : 0
904
+ : lower > 0
905
+ ? lower
906
+ : upper < 0
907
+ ? upper
908
+ : 0;
909
+ if (!isInteractionFormFieldValueValid(field, value)) {
910
+ throw new Error(`Interaction form field ${field.name} has no valid bounded value`);
911
+ }
912
+ return value;
913
+ }
914
+ if (field.kind === 'boolean')
915
+ return false;
916
+ if (field.kind === 'single_select')
917
+ return field.options[0].value;
918
+ return field.options.slice(0, field.minItems ?? 0).map((option) => option.value);
919
+ }
383
920
  function decodeAnswers(value) {
384
921
  return Object.freeze(plainArray(value, 'answers', 1, INTERACTION_MAX_QUESTIONS).map((answer) => answer === null ? null : boundedString(answer, 'answer', INTERACTION_ANSWER_MAX_BYTES)));
385
922
  }
@@ -422,6 +959,12 @@ function boundedString(value, label, maxBytes) {
422
959
  throw new Error(`Invalid ${label}`);
423
960
  return value;
424
961
  }
962
+ function boundedText(value, label, maxBytes) {
963
+ if (typeof value !== 'string' || UTF8.encode(value).byteLength > maxBytes) {
964
+ throw new Error(`Invalid ${label}`);
965
+ }
966
+ return value;
967
+ }
425
968
  function boundedCharacterString(value, label, maxChars) {
426
969
  if (typeof value !== 'string' || value.length === 0 || value.length > maxChars)
427
970
  throw new Error(`Invalid ${label}`);
@@ -437,6 +980,22 @@ function safeInteger(value, label, positive) {
437
980
  throw new Error(`Invalid ${label}`);
438
981
  return value;
439
982
  }
983
+ function optionalBoundedCount(value, label, max = INTERACTION_FORM_VALUE_MAX_BYTES) {
984
+ if (value === undefined)
985
+ return undefined;
986
+ const count = safeInteger(value, label, false);
987
+ if (count > max)
988
+ throw new Error(`Invalid ${label}`);
989
+ return count;
990
+ }
991
+ function finiteNumber(value, label) {
992
+ if (typeof value !== 'number' || !Number.isFinite(value))
993
+ throw new Error(`Invalid ${label}`);
994
+ return Object.is(value, -0) ? 0 : value;
995
+ }
996
+ function optionalFiniteNumber(value, label) {
997
+ return value === undefined ? undefined : finiteNumber(value, label);
998
+ }
440
999
  function oneOf(value, values, label) {
441
1000
  if (!values.includes(value))
442
1001
  throw new Error(`Invalid ${label}`);
@@ -456,6 +1015,21 @@ function serializedLimit(value, maxBytes, label) {
456
1015
  function equalAnswers(left, right) {
457
1016
  return left.length === right.length && left.every((answer, index) => answer === right[index]);
458
1017
  }
1018
+ function equalFormValues(left, right) {
1019
+ const leftKeys = Object.keys(left).sort();
1020
+ const rightKeys = Object.keys(right).sort();
1021
+ return (leftKeys.length === rightKeys.length &&
1022
+ leftKeys.every((key, index) => {
1023
+ if (key !== rightKeys[index])
1024
+ return false;
1025
+ const leftValue = left[key];
1026
+ const rightValue = right[key];
1027
+ return Array.isArray(leftValue) && Array.isArray(rightValue)
1028
+ ? leftValue.length === rightValue.length &&
1029
+ leftValue.every((candidate, valueIndex) => candidate === rightValue[valueIndex])
1030
+ : leftValue === rightValue;
1031
+ }));
1032
+ }
459
1033
  function deepFreeze(value) {
460
1034
  if (value !== null && typeof value === 'object' && !Object.isFrozen(value)) {
461
1035
  for (const nested of Object.values(value))