@sigrix-io/gatehouse 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,10 @@
1
+ <p align="center">
2
+ <picture>
3
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/sigrix-io/gatehouse/main/.github/assets/logo-on-dark.svg">
4
+ <img src="https://raw.githubusercontent.com/sigrix-io/gatehouse/main/.github/assets/logo.svg" alt="Gatehouse" width="120">
5
+ </picture>
6
+ </p>
7
+
1
8
  # Gatehouse
2
9
 
3
10
  [![npm](https://img.shields.io/npm/v/@sigrix-io/gatehouse)](https://www.npmjs.com/package/@sigrix-io/gatehouse)
@@ -46,7 +53,7 @@ Each script is an IIFE that adds to one global, `window.Gatehouse`. `window.Sigr
46
53
  | Constructor | Arguments |
47
54
  | --- | --- |
48
55
  | `PosternClient(baseUrl, options)` | `baseUrl`: where the runner (or the proxy) answers; every request goes to it under `/postern/v0`. `options.headers`: extra headers for every request. `options.fetchImpl`: a `fetch` to use instead of the page's. |
49
- | `RunRenderer(container, client, options)` | `container`: the element to draw into. `client`: anything with the four verbs. `options.composition`, `options.choice`, `options.hosted`: below. |
56
+ | `RunRenderer(container, client, options)` | `container`: the element to draw into. `client`: anything with the four verbs. `options.composition`, `options.choice`, `options.hosted`, `options.conversation`: below. |
50
57
 
51
58
  `renderer.load()` reads `describe` and `status` and draws the page. Running, stopping and retrying are the page's own buttons.
52
59
 
@@ -93,6 +100,18 @@ The form shows only the chosen option's inputs; a chosen option's `required` add
93
100
 
94
101
  `options.hosted: true` says the page's operator runs the runner, not the person reading it. It changes only what a failure may quote back: an `agent_error` is the one refusal whose message carries what a model provider said about the *operator's* credentials, so on a hosted page it is withheld and the reader sees the next move alone.
95
102
 
103
+ ## A conversation
104
+
105
+ No verb takes a run's `run_id` (SPEC §4.5), so a client cannot point a run at the one before it, and a page that sends one brief and shows one answer gives a reader nothing to answer an agent's question with; each new brief replaces the last answer. `options.conversation: true` keeps the exchange on the page instead:
106
+
107
+ - **Each brief and its answer become a turn**, drawn above the form, oldest first, the answer as the result panel would draw it (structure in the `markdown` output mode, text otherwise). The box empties for the reply, and the result panel is emptied rather than showing the answer twice.
108
+ - **A reply carries the turns before it.** The reserved `prompt` input is sent as one brief holding the earlier turns, oldest first and each said by whom, then the new message: `Gatehouse.viewModel.conversationBrief(turns, message, limit)` composes it, so the agent reads the whole exchange as its brief. Every other input is sent as it is.
109
+ - **What a reply carries is bounded**: by the brief's own `max_length`, which the runner enforces, and by `options.conversation.maxChars` (`{ maxChars: 8000 }`), or 16,000 characters when neither is set (`CONVERSATION_MAX_CHARS`). The oldest turns are left out first, every turn stays on the page, and a note says how many no longer fit. The new message is never cut.
110
+ - **Only an answer the page can show as text is a turn.** A refusal, a stop, a file or an output type this client cannot draw leaves the brief in its box to send again.
111
+ - **The page holds it, and nothing else does.** The turns live in the renderer, so leaving the page forgets them; a reader starts over sooner with **New conversation**.
112
+
113
+ A runner whose `describe` declares no text `prompt` has nothing to reply into, and its page stays the one-run page whatever the host asked for.
114
+
96
115
  ## Several apps on one page
97
116
 
98
117
  A `RunRenderer` writes its ids under a scope of its own (`run-1-field-prompt` for the first renderer a page makes, `run-2-field-prompt` for the second), so two on one page never share one and each `<label>` stays bound to its own field. To find a field, look for its `name`, which is its input's key, inside the container you handed in: `container.querySelector('[name="prompt"]')`. The id is the renderer's own business.
@@ -237,7 +256,7 @@ Gatehouse is one of the open-source projects [Sigrix](https://sigrix.io) publish
237
256
  - **[sigrix-runtime](https://github.com/sigrix-io/sigrix-runtime)** (`pip install sigrix-runtime`), a runner to point it at: the one inside every bundle Sigrix delivers, which can also serve an MCP server's tools as one agent. Start it with `--allow-origin` set to the page's origin.
238
257
  - **[postern-conformance](https://pypi.org/project/postern-conformance/)** (`pip install postern-conformance`), which reports the level a runner really meets before you draw a page for it.
239
258
  - **[Bailey](https://github.com/sigrix-io/bailey)**, the open-source assistant: the app planned for its 0.1 draws a solution's apps with Gatehouse.
240
- - **Muster**, the format a playbook's `playbook.json` is written in, which the playbook block reads. Its schema is at [sigrix.io/schemas/muster/0.1/playbook.schema.json](https://sigrix.io/schemas/muster/0.1/playbook.schema.json).
259
+ - **[Muster](https://github.com/sigrix-io/muster)**, the format a playbook's `playbook.json` is written in, which the playbook block reads. Its schema is at [sigrix.io/schemas/muster/0.1/playbook.schema.json](https://sigrix.io/schemas/muster/0.1/playbook.schema.json).
241
260
 
242
261
  Every project Sigrix publishes, and a map of how they connect: [sigrix.io/open-source](https://sigrix.io/open-source).
243
262
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sigrix-io/gatehouse",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "A browser client for Postern: draws a run page from a describe document and connects it to the four verbs. No build step and no dependencies.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Sigrix",
package/src/gatehouse.css CHANGED
@@ -38,12 +38,12 @@
38
38
  .run-shell {
39
39
  display: grid;
40
40
  grid-template-columns: minmax(0, 1fr) 300px;
41
- grid-template-areas: "head rail" "form rail" "controls rail" "output rail";
41
+ grid-template-areas: "head rail" "conversation rail" "form rail" "controls rail" "output rail";
42
42
  gap: 0 24px;
43
43
  align-items: start;
44
44
  }
45
45
  @media (max-width: 960px) {
46
- .run-shell { grid-template-columns: minmax(0, 1fr); grid-template-areas: "head" "rail" "form" "controls" "output"; }
46
+ .run-shell { grid-template-columns: minmax(0, 1fr); grid-template-areas: "head" "rail" "conversation" "form" "controls" "output"; }
47
47
  }
48
48
  /* Stated here rather than left to a host's reset, which a page linking only
49
49
  * this file does not have. A browser draws `select` border-box and `input` and
@@ -153,6 +153,45 @@
153
153
  * write — they should not read as the same voice. */
154
154
  .run-field-help { font-size: 12.5px; color: var(--run-text-2); margin: 6px 0 0; max-width: 64ch; }
155
155
 
156
+ /* ---------- conversation ----------
157
+ *
158
+ * Above the form, oldest first, so the newest answer sits just over the box
159
+ * the reply is typed into. A page that asked for no conversation draws no
160
+ * section, and its grid row is empty and takes no height. */
161
+ .run-conversation { grid-area: conversation; margin-bottom: 18px; min-width: 0; }
162
+ .run-conversation[hidden] { display: none; }
163
+ .run-turns { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; }
164
+ .run-turn {
165
+ background: var(--run-surface); border: 1px solid var(--run-line);
166
+ border-radius: 14px; padding: 14px 16px; min-width: 0;
167
+ }
168
+ .run-turn-who {
169
+ font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em;
170
+ color: var(--run-text-3); margin: 0 0 4px;
171
+ }
172
+ /* What the reader typed, as typed: their line breaks kept, and a long word
173
+ * (a URL, a key) broken rather than pushing the card wider than its column. */
174
+ .run-turn-brief {
175
+ white-space: pre-wrap; overflow-wrap: anywhere; margin: 0 0 14px;
176
+ font-size: 14px; line-height: 1.55; color: var(--run-text-2);
177
+ }
178
+ .run-turn-answer { font-size: 14px; line-height: 1.62; overflow-wrap: anywhere; min-width: 0; }
179
+ .run-turn-text { white-space: pre-wrap; margin: 0; max-width: 72ch; }
180
+ .run-turn-answer .run-download { margin-top: 10px; }
181
+ .run-conversation-foot { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 16px; margin-top: 10px; }
182
+ .run-conversation-note { font-size: 12px; color: var(--run-text-3); margin: 0; max-width: 64ch; flex: 1 1 260px; }
183
+ .run-conversation-reset {
184
+ font: inherit; font-size: 12.5px; font-weight: 600; cursor: pointer;
185
+ border-radius: 10px; padding: 0 12px; height: 32px; min-height: 0;
186
+ background: var(--run-surface); color: var(--run-text); border: 1px solid var(--run-line);
187
+ }
188
+ .run-conversation-reset:disabled { opacity: 0.45; cursor: not-allowed; }
189
+ /* A host may floor every button at 44px for a thumb; given back here, after
190
+ * the opt-out above, for the same reason the write gate's box is. */
191
+ @media (pointer: coarse) {
192
+ .run-conversation-reset { min-height: 44px; }
193
+ }
194
+
156
195
  /* ---------- controls ---------- */
157
196
  .run-controls { grid-area: controls; margin-top: 18px; }
158
197
  .run-actions { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
@@ -25,6 +25,17 @@
25
25
  while (node.firstChild) node.removeChild(node.firstChild);
26
26
  }
27
27
 
28
+ /* `options.conversation`: `true`, or `{ maxChars }` to bound what a reply
29
+ * carries. Anything else is no conversation: the one-run page this file has
30
+ * always drawn. */
31
+ function conversationSettings(option) {
32
+ if (option === true) return { maxChars: null };
33
+ if (option && typeof option === 'object') {
34
+ return { maxChars: typeof option.maxChars === 'number' && option.maxChars > 0 ? option.maxChars : null };
35
+ }
36
+ return null;
37
+ }
38
+
28
39
  function randomKey() {
29
40
  /* An idempotency key only has to be unique to this client (SPEC 4.2). */
30
41
  if (root.crypto && typeof root.crypto.randomUUID === 'function') return root.crypto.randomUUID();
@@ -49,7 +60,9 @@
49
60
  * `reportFailure`). `options.choice` says one select input chooses which
50
61
  * of the others apply (`inputChoice` in the view model has its shape): a
51
62
  * fact the host read about this runner, handed in for the composition's
52
- * reason. Nothing else is read. */
63
+ * reason. `options.conversation` keeps each brief and answer on the page
64
+ * and sends them with the next brief (`renderConversation`). Nothing else
65
+ * is read. */
53
66
  constructor(container, client, options) {
54
67
  const settings = options || {};
55
68
  this.container = container;
@@ -70,6 +83,12 @@
70
83
  this.lastInputsKey = '';
71
84
  this.idempotencyKey = '';
72
85
  this.onRunStateChange = settings.onRunStateChange || function () {};
86
+ this.conversation = conversationSettings(settings.conversation);
87
+ /* Held here and nowhere else: the page forgets the conversation when it
88
+ * is left, and a runner is only ever sent it inside a brief. */
89
+ this.turns = [];
90
+ this.lastDropped = 0;
91
+ this.lastAnswer = null;
73
92
  this.nodes = {};
74
93
  renderersMade += 1;
75
94
  this.idScope = 'run-' + renderersMade;
@@ -110,6 +129,8 @@
110
129
  shell.appendChild(this.renderReadiness(vm));
111
130
  const templates = this.renderTemplates(vm);
112
131
  if (templates) shell.appendChild(templates);
132
+ const conversation = this.renderConversation(vm);
133
+ if (conversation) shell.appendChild(conversation);
113
134
  shell.appendChild(this.renderForm(vm));
114
135
  shell.appendChild(this.renderControls(vm));
115
136
  shell.appendChild(this.renderOutput());
@@ -205,6 +226,122 @@
205
226
  return section;
206
227
  }
207
228
 
229
+ /* The field a conversation carries: the reserved brief, where the runner
230
+ * declares it as text. A runner with none has nothing to reply into, so
231
+ * its page stays the one-run page whatever the host asked for. */
232
+ conversationField() {
233
+ if (!this.conversation || !this.viewModel) return null;
234
+ const key = NS.viewModel.PROMPT_KEY;
235
+ return this.viewModel.fields.find(field => field.key === key && field.type === 'text') || null;
236
+ }
237
+
238
+ /* The characters a reply may carry: the brief's own `max_length`, which the
239
+ * runner enforces, and the host's bound or the view model's under it. */
240
+ conversationLimit(field) {
241
+ const bounds = [this.conversation.maxChars || NS.viewModel.CONVERSATION_MAX_CHARS];
242
+ if (field.maxLength !== null) bounds.push(field.maxLength);
243
+ return Math.min.apply(Math, bounds);
244
+ }
245
+
246
+ /* A conversation, above the form: each brief sent and the answer it got,
247
+ * oldest first. No verb takes a `run_id` (SPEC 4.5), so a run cannot be
248
+ * pointed at the one before it: the agent sees what came before because
249
+ * the next brief carries it, which `conversationBrief` composes, and the
250
+ * note says so. */
251
+ renderConversation(vm) {
252
+ if (!this.conversationField()) return null;
253
+ const self = this;
254
+ const section = el('section', 'run-conversation');
255
+ section.setAttribute('aria-label', 'Conversation');
256
+ this.nodes.turns = el('ol', 'run-turns');
257
+ section.appendChild(this.nodes.turns);
258
+ const foot = el('div', 'run-conversation-foot');
259
+ this.nodes.conversationNote = el('p', 'run-conversation-note');
260
+ foot.appendChild(this.nodes.conversationNote);
261
+ const reset = el('button', 'run-conversation-reset', 'New conversation');
262
+ reset.type = 'button';
263
+ reset.addEventListener('click', function () { self.resetConversation(); });
264
+ foot.appendChild(reset);
265
+ section.appendChild(foot);
266
+ this.nodes.conversation = section;
267
+ this.nodes.conversationReset = reset;
268
+ this.turns.forEach(function (turn) { self.appendTurn(turn, vm); });
269
+ this.syncConversation();
270
+ return section;
271
+ }
272
+
273
+ appendTurn(turn, vm) {
274
+ const item = el('li', 'run-turn');
275
+ item.appendChild(el('p', 'run-turn-who', 'You'));
276
+ item.appendChild(el('p', 'run-turn-brief', turn.brief));
277
+ item.appendChild(el('p', 'run-turn-who', vm.header.name || 'The agent'));
278
+ const answer = el('div', 'run-turn-answer');
279
+ /* Drawn as the result panel draws it, so a turn reads the same before
280
+ * and after it moves up: structure for `markdown`, text for the rest. */
281
+ if (vm.output.mode === 'markdown') this.renderMarkdown(turn.answer, answer);
282
+ else answer.appendChild(el('p', 'run-turn-text', turn.answer));
283
+ if (vm.output.mode === 'file') answer.appendChild(this.textDownloadLink(turn.answer));
284
+ item.appendChild(answer);
285
+ this.nodes.turns.appendChild(item);
286
+ }
287
+
288
+ syncConversation() {
289
+ if (!this.nodes.conversation) return;
290
+ this.nodes.conversation.hidden = this.turns.length === 0;
291
+ this.nodes.conversationReset.disabled = Boolean(this.abortController);
292
+ const dropped = this.lastDropped;
293
+ this.nodes.conversationNote.textContent = dropped
294
+ ? 'A reply sends the conversation above with it, except the ' +
295
+ (dropped === 1 ? 'first message, which no longer fits' : dropped + ' oldest messages, which no longer fit') +
296
+ '. The page forgets it when you leave.'
297
+ : 'A reply sends the conversation above with it, so the agent can build on it. The page forgets it when you leave.';
298
+ }
299
+
300
+ recordTurn(message, answer) {
301
+ const field = this.conversationField();
302
+ const turn = { brief: message, answer: answer };
303
+ this.turns.push(turn);
304
+ this.appendTurn(turn, this.viewModel);
305
+ /* The answer lives in the conversation now, so the result panel is
306
+ * emptied rather than left showing it twice. The usage line and the
307
+ * timeline stay: they are about the run that has just ended. */
308
+ this.nodes.result.textContent = '';
309
+ this.clearRendered();
310
+ this.values[field.key] = '';
311
+ const control = this.nodes.inputs[field.key];
312
+ if (control) control.value = '';
313
+ this.lastDropped = NS.viewModel.conversationBrief(this.turns, '', this.conversationLimit(field)).dropped;
314
+ this.syncConversation();
315
+ if (control) control.focus();
316
+ }
317
+
318
+ /* Reached only from its button, which `syncConversation` disables while a
319
+ * run is in flight, so a run never records into a conversation it did not
320
+ * start from. */
321
+ resetConversation() {
322
+ this.turns = [];
323
+ this.lastDropped = 0;
324
+ if (this.nodes.turns) {
325
+ this.revokeDownloads(this.nodes.turns);
326
+ clear(this.nodes.turns);
327
+ }
328
+ this.syncConversation();
329
+ const field = this.conversationField();
330
+ if (field && this.nodes.inputs[field.key]) this.nodes.inputs[field.key].focus();
331
+ }
332
+
333
+ revokeDownloads(scope) {
334
+ Array.prototype.slice.call(scope.querySelectorAll('.run-download')).forEach(function (node) {
335
+ URL.revokeObjectURL(node.href);
336
+ node.parentNode.removeChild(node);
337
+ });
338
+ }
339
+
340
+ clearRendered() {
341
+ this.revokeDownloads(this.nodes.rendered);
342
+ clear(this.nodes.rendered);
343
+ }
344
+
208
345
  renderForm(vm) {
209
346
  const section = el('section', 'run-form');
210
347
  const self = this;
@@ -397,6 +534,7 @@
397
534
  * content only, so headings and lists cannot be built inside it. */
398
535
  this.nodes.rendered = el('div', 'run-result-rendered');
399
536
  this.nodes.usage = el('p', 'run-usage');
537
+ this.nodes.output = section;
400
538
  section.appendChild(this.nodes.notice);
401
539
  section.appendChild(this.nodes.timeline);
402
540
  section.appendChild(this.nodes.result);
@@ -441,6 +579,16 @@
441
579
  if (!vm.level.canRun || this.abortController) return;
442
580
 
443
581
  const inputs = NS.viewModel.runInputs(vm.fields, this.values, vm.choice);
582
+ /* In a conversation the brief carries the turns before it. What was
583
+ * typed is kept apart, because that, not the composed whole, is the
584
+ * turn the page records. */
585
+ const brief = this.conversationField();
586
+ const message = brief && this.values[brief.key] !== undefined ? String(this.values[brief.key]) : '';
587
+ if (brief && this.turns.length && Object.prototype.hasOwnProperty.call(inputs, brief.key)) {
588
+ const reply = NS.viewModel.conversationBrief(this.turns, message, this.conversationLimit(brief));
589
+ inputs[brief.key] = reply.value;
590
+ this.lastDropped = reply.dropped;
591
+ }
444
592
  const inputsKey = JSON.stringify(inputs);
445
593
  if (vm.capabilities.idempotentRetry) {
446
594
  /* A replay is free only for the same inputs under the same key. Edited
@@ -459,13 +607,10 @@
459
607
  /* A download link is appended beside the result rather than inside it,
460
608
  * so clearing the result does not remove it — two `bytes` runs would
461
609
  * otherwise leave two links, the older one pointing at the earlier
462
- * result and no way to tell them apart. */
463
- Array.prototype.slice
464
- .call(this.container.querySelectorAll('.run-download'))
465
- .forEach(function (node) {
466
- URL.revokeObjectURL(node.href);
467
- node.parentNode.removeChild(node);
468
- });
610
+ * result and no way to tell them apart. Only the result panel's: a turn
611
+ * of the conversation keeps the link to its own answer. */
612
+ this.revokeDownloads(this.nodes.output);
613
+ this.lastAnswer = null;
469
614
  /* And a field marked invalid by the last refusal stops being marked, or
470
615
  * a corrected input keeps wearing the error that has just been fixed. */
471
616
  Object.keys(this.nodes.inputs || {}).forEach(key => {
@@ -476,18 +621,30 @@
476
621
  this.nodes.stop.disabled = false;
477
622
  this.nodes.retry.disabled = true;
478
623
  this.syncRunEnabled();
624
+ this.syncConversation();
479
625
  this.onRunStateChange('running');
480
626
 
627
+ /* A turn is recorded only for an answer the page could show as text: a
628
+ * refusal, a stop, a file or a type this client cannot draw leaves the
629
+ * brief in its field, to send again or change. */
630
+ let recorded = false;
481
631
  try {
482
632
  if (vm.level.streams) await this.runStreamed(inputs);
483
633
  else await this.runWhole(inputs);
634
+ if (brief && this.lastAnswer !== null) {
635
+ this.recordTurn(message, this.lastAnswer);
636
+ recorded = true;
637
+ }
484
638
  } catch (err) {
485
639
  this.reportFailure(err);
486
640
  } finally {
487
641
  this.abortController = null;
488
642
  this.nodes.stop.disabled = true;
489
- this.nodes.retry.disabled = false;
643
+ /* Nothing to retry once the answer is a turn: its brief has left the
644
+ * field, and a reply is the next thing to send. */
645
+ this.nodes.retry.disabled = recorded;
490
646
  this.syncRunEnabled();
647
+ this.syncConversation();
491
648
  this.onRunStateChange('idle');
492
649
  }
493
650
  }
@@ -601,6 +758,7 @@
601
758
  }
602
759
 
603
760
  const value = output.value === undefined || output.value === null ? '' : String(output.value);
761
+ this.lastAnswer = value;
604
762
  if (plan.mode === 'markdown') {
605
763
  /* Structure only, and built node by node — see `outputBlocks`. The
606
764
  * `<pre>` is emptied rather than left carrying the stream's plain copy
@@ -618,8 +776,8 @@
618
776
  }
619
777
  }
620
778
 
621
- renderMarkdown(value) {
622
- const target = this.nodes.rendered;
779
+ renderMarkdown(value, into) {
780
+ const target = into || this.nodes.rendered;
623
781
  NS.viewModel.outputBlocks(value).forEach(function (block) {
624
782
  if (block.kind === 'heading') {
625
783
  /* Clamped to h2..h4: the page's own `<h1>` is the agent's name, and
@@ -643,10 +801,14 @@
643
801
  }
644
802
 
645
803
  offerTextDownload(value) {
804
+ this.nodes.rendered.appendChild(this.textDownloadLink(value));
805
+ }
806
+
807
+ textDownloadLink(value) {
646
808
  const link = el('a', 'run-download', 'Download the result');
647
809
  link.href = URL.createObjectURL(new Blob([value], { type: 'text/plain' }));
648
810
  link.download = 'result.txt';
649
- this.nodes.rendered.appendChild(link);
811
+ return link;
650
812
  }
651
813
 
652
814
  offerDownload(output) {
@@ -426,6 +426,40 @@
426
426
  /* SPEC 4.1.2: a client SHOULD surface `write_tools` and MAY require a
427
427
  * confirmation. This one requires it, once per session, because the tools
428
428
  * named here are the ones that spend money or reach past the workspace. */
429
+ /* How much of a conversation a reply carries when the host names no bound
430
+ * and the brief declares no `max_length`: about four thousand tokens of
431
+ * text, enough for several exchanges and a cap on what a long one costs. */
432
+ const CONVERSATION_MAX_CHARS = 16000;
433
+
434
+ /* A reply in a conversation, as the one brief a run takes. No verb takes a
435
+ * `run_id` (SPEC 4.5), so a client cannot point a run at an earlier one, and
436
+ * the way an agent sees what was said before is for the client to send it.
437
+ * The earlier turns come first, oldest first, then the new message, each
438
+ * under a line saying whose words they are, in plain text the agent reads as
439
+ * its brief.
440
+ *
441
+ * `limit` bounds the whole in characters. The oldest turns are the ones
442
+ * left out when it would pass it, and the answer says how many were, so the
443
+ * page can say so rather than let a reader think the agent saw them. The new
444
+ * message is never cut: the field's own `max_length` bounds it as typed. */
445
+ function conversationBrief(turns, message, limit) {
446
+ const said = text(message);
447
+ const all = asArray(turns).filter(isObject);
448
+ const cap = typeof limit === 'number' && limit > 0 ? limit : CONVERSATION_MAX_CHARS;
449
+ for (let first = 0; first < all.length; first++) {
450
+ const kept = all.slice(first);
451
+ const parts = ['This continues an earlier exchange with you. What was said before, oldest first:'];
452
+ kept.forEach(function (turn) {
453
+ parts.push('The user wrote:\n' + text(turn.brief));
454
+ parts.push('You answered:\n' + text(turn.answer));
455
+ });
456
+ parts.push('Now the user writes, and this is the message to answer:\n' + said);
457
+ const value = parts.join('\n\n');
458
+ if (value.length <= cap) return { value, sent: kept.length, dropped: first };
459
+ }
460
+ return { value: said, sent: 0, dropped: all.length };
461
+ }
462
+
429
463
  function capabilities(describe) {
430
464
  const declared = isObject(describe && describe.capabilities) ? describe.capabilities : {};
431
465
  const writeTools = asArray(declared.write_tools).map(text).filter(Boolean);
@@ -621,6 +655,8 @@
621
655
  formFields,
622
656
  runInputs,
623
657
  initialValues,
658
+ conversationBrief,
659
+ CONVERSATION_MAX_CHARS,
624
660
  examples,
625
661
  outputPlan,
626
662
  capabilities,