@sigrix-io/gatehouse 0.3.0 → 0.5.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 +33 -4
- package/package.json +3 -3
- package/src/gatehouse.css +41 -2
- package/src/host_blocks.js +14 -0
- package/src/run_renderer.js +191 -15
- package/src/run_view_model.js +36 -0
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
|
[](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. `options.titleLevel`: the heading level of the bundle's name, 1 to 6, default 1 (see [The title's level](#the-titles-level)). |
|
|
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
|
|
|
@@ -63,6 +70,16 @@ Each is held by a test rather than by memory (`tests/contract.test.js` and the s
|
|
|
63
70
|
- **Documents are handed in.** The composition and the choice arrive as data from whoever mounted the page, and so does everything the host blocks and the playbook block draw. Fetching them here would be reaching for a fifth verb, and Postern has four.
|
|
64
71
|
- **A link goes only to a web address.** An address a runner's author wrote becomes a link only when it is an absolute `http` or `https` one, so a `javascript:` address in `describe` is never something a click runs.
|
|
65
72
|
|
|
73
|
+
## The title's level
|
|
74
|
+
|
|
75
|
+
The run page draws the bundle's name as `<h1 class="run-title">`, which is right for a page that is nothing but the run page. A host page that already has an `<h1>` of its own passes `titleLevel: 2` (or any whole number from 1 to 6), and the name is drawn as that level instead, with the same class and the same text:
|
|
76
|
+
|
|
77
|
+
```js
|
|
78
|
+
new Gatehouse.RunRenderer(container, client, { titleLevel: 2 });
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
A number or numeric text (`'2'`) is read as a level; anything that does not read as a whole number from 1 to 6 (absent, `0`, `7`, `2.5`, other text) is the default, 1. Pass 2 for a page that has an `<h1>` of its own; from 3 on, the heading above the examples and a result's top heading are both `<h2>` and outrank the name. Only the name's level moves: the heading above the examples stays an `<h2>`, and a result's own headings stay between `<h2>` and `<h4>`, so at `titleLevel: 2` they sit beside the name and skip no level beneath it. The renderer draws no other `<h1>`.
|
|
82
|
+
|
|
66
83
|
## The composition
|
|
67
84
|
|
|
68
85
|
`options.composition` is a seller's `ui.json`: how a runner's inputs and result are *presented*. A seller writes it, so the client re-checks everything in it rather than trusting the file it was handed.
|
|
@@ -93,6 +110,18 @@ The form shows only the chosen option's inputs; a chosen option's `required` add
|
|
|
93
110
|
|
|
94
111
|
`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
112
|
|
|
113
|
+
## A conversation
|
|
114
|
+
|
|
115
|
+
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:
|
|
116
|
+
|
|
117
|
+
- **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.
|
|
118
|
+
- **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.
|
|
119
|
+
- **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.
|
|
120
|
+
- **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.
|
|
121
|
+
- **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**.
|
|
122
|
+
|
|
123
|
+
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.
|
|
124
|
+
|
|
96
125
|
## Several apps on one page
|
|
97
126
|
|
|
98
127
|
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.
|
|
@@ -145,7 +174,7 @@ Each carries `"v": 1`. A block refuses a document in a version it does not read,
|
|
|
145
174
|
}
|
|
146
175
|
```
|
|
147
176
|
|
|
148
|
-
**Run row.** `app_id`, `app_name`, `started_at`, `input_tokens`, `output_tokens` and `outcome` are required. `outcome` is `done`, `timed_out`, `failed` or `cancelled`; `duration_seconds`, `model`, `cost_estimate` and `detail` may be null; `currency`, an ISO 4217 code, goes beside a cost. `detail` is the host's own sentence about how the run ended, drawn as given, so it is written for the person reading. A runner's error message is not one: on a hosted run it can carry what a provider said about the operator's key (see [Hosted runs](#hosted-runs)).
|
|
177
|
+
**Run row.** `app_id`, `app_name`, `started_at`, `input_tokens`, `output_tokens` and `outcome` are required. `outcome` is `done`, `timed_out`, `failed` or `cancelled`; `duration_seconds`, `model`, `cost_estimate` and `detail` may be null; `currency`, an ISO 4217 code, goes beside a cost. `credits`, a count, is what the host's credits paid for the run, left out or null when they paid nothing for it: `RunHistory` draws a Credits column beside the tokens once any row carries one, and a dash for a row that does not. `detail` is the host's own sentence about how the run ended, drawn as given, so it is written for the person reading. A runner's error message is not one: on a hosted run it can carry what a provider said about the operator's key (see [Hosted runs](#hosted-runs)).
|
|
149
178
|
|
|
150
179
|
```json
|
|
151
180
|
{
|
|
@@ -236,8 +265,8 @@ Gatehouse is one of the open-source projects [Sigrix](https://sigrix.io) publish
|
|
|
236
265
|
- **[Postern](https://github.com/sigrix-io/postern)**, the protocol whose four verbs it calls. Its [§2.3](https://github.com/sigrix-io/postern/blob/main/SPEC.md#23-browser-clients) says what a runner owes a browser client, and why the default is to refuse.
|
|
237
266
|
- **[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
267
|
- **[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
|
-
- **[Bailey](https://github.com/sigrix-io/bailey)
|
|
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).
|
|
268
|
+
- **[Bailey](https://github.com/sigrix-io/bailey)** (`uvx bailey`), the open-source assistant, which draws a solution's apps with Gatehouse.
|
|
269
|
+
- **[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
270
|
|
|
242
271
|
Every project Sigrix publishes, and a map of how they connect: [sigrix.io/open-source](https://sigrix.io/open-source).
|
|
243
272
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sigrix-io/gatehouse",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.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",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"test": "vitest run"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"jsdom": "
|
|
45
|
-
"vitest": "5.0
|
|
44
|
+
"jsdom": "31.0.0",
|
|
45
|
+
"vitest": "5.1.0"
|
|
46
46
|
}
|
|
47
47
|
}
|
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; }
|
package/src/host_blocks.js
CHANGED
|
@@ -90,6 +90,12 @@
|
|
|
90
90
|
return value;
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
+
/* A count a document may leave out: `null` when it does. */
|
|
94
|
+
function countOrNone(doc, key) {
|
|
95
|
+
if (doc[key] === undefined || doc[key] === null) return null;
|
|
96
|
+
return countMember(doc, key);
|
|
97
|
+
}
|
|
98
|
+
|
|
93
99
|
function amountMember(doc, key, nullable) {
|
|
94
100
|
const value = doc[key];
|
|
95
101
|
if ((value === undefined || value === null) && nullable) return null;
|
|
@@ -167,6 +173,8 @@
|
|
|
167
173
|
model: textMember(row, 'model', false),
|
|
168
174
|
inputTokens: countMember(row, 'input_tokens'),
|
|
169
175
|
outputTokens: countMember(row, 'output_tokens'),
|
|
176
|
+
/* What the host's credits paid for the run, when they paid for it. */
|
|
177
|
+
credits: countOrNone(row, 'credits'),
|
|
170
178
|
costEstimate,
|
|
171
179
|
currency: costEstimate === null ? '' : currencyMember(row, 'currency'),
|
|
172
180
|
outcome: textMember(row, 'outcome', true),
|
|
@@ -511,6 +519,10 @@
|
|
|
511
519
|
if (read.refusal) refusals.push(read.refusal);
|
|
512
520
|
else runs.push(read.value);
|
|
513
521
|
});
|
|
522
|
+
/* A host whose runs no credits paid for has no such column. */
|
|
523
|
+
const showCredits = runs.some(function (run) {
|
|
524
|
+
return run.credits !== null;
|
|
525
|
+
});
|
|
514
526
|
|
|
515
527
|
if (!runs.length) {
|
|
516
528
|
if (!refusals.length) block.appendChild(el('p', 'gh-empty', options.emptyText || 'No runs yet.'));
|
|
@@ -531,6 +543,7 @@
|
|
|
531
543
|
headRow.appendChild(headCell('Duration', true));
|
|
532
544
|
headRow.appendChild(headCell('Tokens in', true));
|
|
533
545
|
headRow.appendChild(headCell('Tokens out', true));
|
|
546
|
+
if (showCredits) headRow.appendChild(headCell('Credits', true));
|
|
534
547
|
/* The column says what the figure is. A run's cost is the host's
|
|
535
548
|
* estimate from token counts and a price list, never a bill. */
|
|
536
549
|
headRow.appendChild(headCell('Cost (estimate)', true));
|
|
@@ -551,6 +564,7 @@
|
|
|
551
564
|
row.appendChild(cell(run.durationSeconds === null ? '—' : formatted.duration(run.durationSeconds), true));
|
|
552
565
|
row.appendChild(cell(formatted.count(run.inputTokens), true));
|
|
553
566
|
row.appendChild(cell(formatted.count(run.outputTokens), true));
|
|
567
|
+
if (showCredits) row.appendChild(cell(run.credits === null ? '—' : formatted.count(run.credits), true));
|
|
554
568
|
row.appendChild(cell(run.costEstimate === null ? '—' : formatted.money(run.costEstimate, run.currency), true));
|
|
555
569
|
row.appendChild(cell(run.model || '—', false));
|
|
556
570
|
body.appendChild(row);
|
package/src/run_renderer.js
CHANGED
|
@@ -25,6 +25,28 @@
|
|
|
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
|
+
|
|
39
|
+
/* `options.titleLevel`: which heading the bundle's name is drawn as, 1 to 6.
|
|
40
|
+
* A number, or text that reads as one ('2'), as `headingLevel` does in the
|
|
41
|
+
* host blocks. Anything that does not read as a whole number from 1 to 6
|
|
42
|
+
* (absent, 0, 7, 2.5, other text, other types) is the default, 1, so a page
|
|
43
|
+
* that asks for nothing keeps the `<h1>` it has always had. A host that
|
|
44
|
+
* already has an `<h1>` of its own passes 2. */
|
|
45
|
+
function titleLevel(option) {
|
|
46
|
+
const level = typeof option === 'number' || typeof option === 'string' ? Number(option) : NaN;
|
|
47
|
+
return Number.isInteger(level) && level >= 1 && level <= 6 ? level : 1;
|
|
48
|
+
}
|
|
49
|
+
|
|
28
50
|
function randomKey() {
|
|
29
51
|
/* An idempotency key only has to be unique to this client (SPEC 4.2). */
|
|
30
52
|
if (root.crypto && typeof root.crypto.randomUUID === 'function') return root.crypto.randomUUID();
|
|
@@ -49,7 +71,10 @@
|
|
|
49
71
|
* `reportFailure`). `options.choice` says one select input chooses which
|
|
50
72
|
* of the others apply (`inputChoice` in the view model has its shape): a
|
|
51
73
|
* fact the host read about this runner, handed in for the composition's
|
|
52
|
-
* reason.
|
|
74
|
+
* reason. `options.conversation` keeps each brief and answer on the page
|
|
75
|
+
* and sends them with the next brief (`renderConversation`).
|
|
76
|
+
* `options.titleLevel` (1 to 6, default 1) is the level of the heading the
|
|
77
|
+
* bundle's name is drawn as (`titleLevel`). Nothing else is read. */
|
|
53
78
|
constructor(container, client, options) {
|
|
54
79
|
const settings = options || {};
|
|
55
80
|
this.container = container;
|
|
@@ -69,7 +94,14 @@
|
|
|
69
94
|
this.lastRunId = '';
|
|
70
95
|
this.lastInputsKey = '';
|
|
71
96
|
this.idempotencyKey = '';
|
|
97
|
+
this.titleLevel = titleLevel(settings.titleLevel);
|
|
72
98
|
this.onRunStateChange = settings.onRunStateChange || function () {};
|
|
99
|
+
this.conversation = conversationSettings(settings.conversation);
|
|
100
|
+
/* Held here and nowhere else: the page forgets the conversation when it
|
|
101
|
+
* is left, and a runner is only ever sent it inside a brief. */
|
|
102
|
+
this.turns = [];
|
|
103
|
+
this.lastDropped = 0;
|
|
104
|
+
this.lastAnswer = null;
|
|
73
105
|
this.nodes = {};
|
|
74
106
|
renderersMade += 1;
|
|
75
107
|
this.idScope = 'run-' + renderersMade;
|
|
@@ -110,6 +142,8 @@
|
|
|
110
142
|
shell.appendChild(this.renderReadiness(vm));
|
|
111
143
|
const templates = this.renderTemplates(vm);
|
|
112
144
|
if (templates) shell.appendChild(templates);
|
|
145
|
+
const conversation = this.renderConversation(vm);
|
|
146
|
+
if (conversation) shell.appendChild(conversation);
|
|
113
147
|
shell.appendChild(this.renderForm(vm));
|
|
114
148
|
shell.appendChild(this.renderControls(vm));
|
|
115
149
|
shell.appendChild(this.renderOutput());
|
|
@@ -117,7 +151,7 @@
|
|
|
117
151
|
|
|
118
152
|
renderHeader(vm) {
|
|
119
153
|
const section = el('section', 'run-header');
|
|
120
|
-
section.appendChild(el('
|
|
154
|
+
section.appendChild(el('h' + this.titleLevel, 'run-title', vm.header.name));
|
|
121
155
|
if (vm.header.summary) section.appendChild(el('p', 'run-summary', vm.header.summary));
|
|
122
156
|
const meta = el('p', 'run-meta');
|
|
123
157
|
/* Verbatim, both of them: the identifier is what the buyer types into
|
|
@@ -205,6 +239,122 @@
|
|
|
205
239
|
return section;
|
|
206
240
|
}
|
|
207
241
|
|
|
242
|
+
/* The field a conversation carries: the reserved brief, where the runner
|
|
243
|
+
* declares it as text. A runner with none has nothing to reply into, so
|
|
244
|
+
* its page stays the one-run page whatever the host asked for. */
|
|
245
|
+
conversationField() {
|
|
246
|
+
if (!this.conversation || !this.viewModel) return null;
|
|
247
|
+
const key = NS.viewModel.PROMPT_KEY;
|
|
248
|
+
return this.viewModel.fields.find(field => field.key === key && field.type === 'text') || null;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/* The characters a reply may carry: the brief's own `max_length`, which the
|
|
252
|
+
* runner enforces, and the host's bound or the view model's under it. */
|
|
253
|
+
conversationLimit(field) {
|
|
254
|
+
const bounds = [this.conversation.maxChars || NS.viewModel.CONVERSATION_MAX_CHARS];
|
|
255
|
+
if (field.maxLength !== null) bounds.push(field.maxLength);
|
|
256
|
+
return Math.min.apply(Math, bounds);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/* A conversation, above the form: each brief sent and the answer it got,
|
|
260
|
+
* oldest first. No verb takes a `run_id` (SPEC 4.5), so a run cannot be
|
|
261
|
+
* pointed at the one before it: the agent sees what came before because
|
|
262
|
+
* the next brief carries it, which `conversationBrief` composes, and the
|
|
263
|
+
* note says so. */
|
|
264
|
+
renderConversation(vm) {
|
|
265
|
+
if (!this.conversationField()) return null;
|
|
266
|
+
const self = this;
|
|
267
|
+
const section = el('section', 'run-conversation');
|
|
268
|
+
section.setAttribute('aria-label', 'Conversation');
|
|
269
|
+
this.nodes.turns = el('ol', 'run-turns');
|
|
270
|
+
section.appendChild(this.nodes.turns);
|
|
271
|
+
const foot = el('div', 'run-conversation-foot');
|
|
272
|
+
this.nodes.conversationNote = el('p', 'run-conversation-note');
|
|
273
|
+
foot.appendChild(this.nodes.conversationNote);
|
|
274
|
+
const reset = el('button', 'run-conversation-reset', 'New conversation');
|
|
275
|
+
reset.type = 'button';
|
|
276
|
+
reset.addEventListener('click', function () { self.resetConversation(); });
|
|
277
|
+
foot.appendChild(reset);
|
|
278
|
+
section.appendChild(foot);
|
|
279
|
+
this.nodes.conversation = section;
|
|
280
|
+
this.nodes.conversationReset = reset;
|
|
281
|
+
this.turns.forEach(function (turn) { self.appendTurn(turn, vm); });
|
|
282
|
+
this.syncConversation();
|
|
283
|
+
return section;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
appendTurn(turn, vm) {
|
|
287
|
+
const item = el('li', 'run-turn');
|
|
288
|
+
item.appendChild(el('p', 'run-turn-who', 'You'));
|
|
289
|
+
item.appendChild(el('p', 'run-turn-brief', turn.brief));
|
|
290
|
+
item.appendChild(el('p', 'run-turn-who', vm.header.name || 'The agent'));
|
|
291
|
+
const answer = el('div', 'run-turn-answer');
|
|
292
|
+
/* Drawn as the result panel draws it, so a turn reads the same before
|
|
293
|
+
* and after it moves up: structure for `markdown`, text for the rest. */
|
|
294
|
+
if (vm.output.mode === 'markdown') this.renderMarkdown(turn.answer, answer);
|
|
295
|
+
else answer.appendChild(el('p', 'run-turn-text', turn.answer));
|
|
296
|
+
if (vm.output.mode === 'file') answer.appendChild(this.textDownloadLink(turn.answer));
|
|
297
|
+
item.appendChild(answer);
|
|
298
|
+
this.nodes.turns.appendChild(item);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
syncConversation() {
|
|
302
|
+
if (!this.nodes.conversation) return;
|
|
303
|
+
this.nodes.conversation.hidden = this.turns.length === 0;
|
|
304
|
+
this.nodes.conversationReset.disabled = Boolean(this.abortController);
|
|
305
|
+
const dropped = this.lastDropped;
|
|
306
|
+
this.nodes.conversationNote.textContent = dropped
|
|
307
|
+
? 'A reply sends the conversation above with it, except the ' +
|
|
308
|
+
(dropped === 1 ? 'first message, which no longer fits' : dropped + ' oldest messages, which no longer fit') +
|
|
309
|
+
'. The page forgets it when you leave.'
|
|
310
|
+
: 'A reply sends the conversation above with it, so the agent can build on it. The page forgets it when you leave.';
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
recordTurn(message, answer) {
|
|
314
|
+
const field = this.conversationField();
|
|
315
|
+
const turn = { brief: message, answer: answer };
|
|
316
|
+
this.turns.push(turn);
|
|
317
|
+
this.appendTurn(turn, this.viewModel);
|
|
318
|
+
/* The answer lives in the conversation now, so the result panel is
|
|
319
|
+
* emptied rather than left showing it twice. The usage line and the
|
|
320
|
+
* timeline stay: they are about the run that has just ended. */
|
|
321
|
+
this.nodes.result.textContent = '';
|
|
322
|
+
this.clearRendered();
|
|
323
|
+
this.values[field.key] = '';
|
|
324
|
+
const control = this.nodes.inputs[field.key];
|
|
325
|
+
if (control) control.value = '';
|
|
326
|
+
this.lastDropped = NS.viewModel.conversationBrief(this.turns, '', this.conversationLimit(field)).dropped;
|
|
327
|
+
this.syncConversation();
|
|
328
|
+
if (control) control.focus();
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/* Reached only from its button, which `syncConversation` disables while a
|
|
332
|
+
* run is in flight, so a run never records into a conversation it did not
|
|
333
|
+
* start from. */
|
|
334
|
+
resetConversation() {
|
|
335
|
+
this.turns = [];
|
|
336
|
+
this.lastDropped = 0;
|
|
337
|
+
if (this.nodes.turns) {
|
|
338
|
+
this.revokeDownloads(this.nodes.turns);
|
|
339
|
+
clear(this.nodes.turns);
|
|
340
|
+
}
|
|
341
|
+
this.syncConversation();
|
|
342
|
+
const field = this.conversationField();
|
|
343
|
+
if (field && this.nodes.inputs[field.key]) this.nodes.inputs[field.key].focus();
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
revokeDownloads(scope) {
|
|
347
|
+
Array.prototype.slice.call(scope.querySelectorAll('.run-download')).forEach(function (node) {
|
|
348
|
+
URL.revokeObjectURL(node.href);
|
|
349
|
+
node.parentNode.removeChild(node);
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
clearRendered() {
|
|
354
|
+
this.revokeDownloads(this.nodes.rendered);
|
|
355
|
+
clear(this.nodes.rendered);
|
|
356
|
+
}
|
|
357
|
+
|
|
208
358
|
renderForm(vm) {
|
|
209
359
|
const section = el('section', 'run-form');
|
|
210
360
|
const self = this;
|
|
@@ -397,6 +547,7 @@
|
|
|
397
547
|
* content only, so headings and lists cannot be built inside it. */
|
|
398
548
|
this.nodes.rendered = el('div', 'run-result-rendered');
|
|
399
549
|
this.nodes.usage = el('p', 'run-usage');
|
|
550
|
+
this.nodes.output = section;
|
|
400
551
|
section.appendChild(this.nodes.notice);
|
|
401
552
|
section.appendChild(this.nodes.timeline);
|
|
402
553
|
section.appendChild(this.nodes.result);
|
|
@@ -441,6 +592,16 @@
|
|
|
441
592
|
if (!vm.level.canRun || this.abortController) return;
|
|
442
593
|
|
|
443
594
|
const inputs = NS.viewModel.runInputs(vm.fields, this.values, vm.choice);
|
|
595
|
+
/* In a conversation the brief carries the turns before it. What was
|
|
596
|
+
* typed is kept apart, because that, not the composed whole, is the
|
|
597
|
+
* turn the page records. */
|
|
598
|
+
const brief = this.conversationField();
|
|
599
|
+
const message = brief && this.values[brief.key] !== undefined ? String(this.values[brief.key]) : '';
|
|
600
|
+
if (brief && this.turns.length && Object.prototype.hasOwnProperty.call(inputs, brief.key)) {
|
|
601
|
+
const reply = NS.viewModel.conversationBrief(this.turns, message, this.conversationLimit(brief));
|
|
602
|
+
inputs[brief.key] = reply.value;
|
|
603
|
+
this.lastDropped = reply.dropped;
|
|
604
|
+
}
|
|
444
605
|
const inputsKey = JSON.stringify(inputs);
|
|
445
606
|
if (vm.capabilities.idempotentRetry) {
|
|
446
607
|
/* A replay is free only for the same inputs under the same key. Edited
|
|
@@ -459,13 +620,10 @@
|
|
|
459
620
|
/* A download link is appended beside the result rather than inside it,
|
|
460
621
|
* so clearing the result does not remove it — two `bytes` runs would
|
|
461
622
|
* otherwise leave two links, the older one pointing at the earlier
|
|
462
|
-
* result and no way to tell them apart.
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
URL.revokeObjectURL(node.href);
|
|
467
|
-
node.parentNode.removeChild(node);
|
|
468
|
-
});
|
|
623
|
+
* result and no way to tell them apart. Only the result panel's: a turn
|
|
624
|
+
* of the conversation keeps the link to its own answer. */
|
|
625
|
+
this.revokeDownloads(this.nodes.output);
|
|
626
|
+
this.lastAnswer = null;
|
|
469
627
|
/* And a field marked invalid by the last refusal stops being marked, or
|
|
470
628
|
* a corrected input keeps wearing the error that has just been fixed. */
|
|
471
629
|
Object.keys(this.nodes.inputs || {}).forEach(key => {
|
|
@@ -476,18 +634,30 @@
|
|
|
476
634
|
this.nodes.stop.disabled = false;
|
|
477
635
|
this.nodes.retry.disabled = true;
|
|
478
636
|
this.syncRunEnabled();
|
|
637
|
+
this.syncConversation();
|
|
479
638
|
this.onRunStateChange('running');
|
|
480
639
|
|
|
640
|
+
/* A turn is recorded only for an answer the page could show as text: a
|
|
641
|
+
* refusal, a stop, a file or a type this client cannot draw leaves the
|
|
642
|
+
* brief in its field, to send again or change. */
|
|
643
|
+
let recorded = false;
|
|
481
644
|
try {
|
|
482
645
|
if (vm.level.streams) await this.runStreamed(inputs);
|
|
483
646
|
else await this.runWhole(inputs);
|
|
647
|
+
if (brief && this.lastAnswer !== null) {
|
|
648
|
+
this.recordTurn(message, this.lastAnswer);
|
|
649
|
+
recorded = true;
|
|
650
|
+
}
|
|
484
651
|
} catch (err) {
|
|
485
652
|
this.reportFailure(err);
|
|
486
653
|
} finally {
|
|
487
654
|
this.abortController = null;
|
|
488
655
|
this.nodes.stop.disabled = true;
|
|
489
|
-
|
|
656
|
+
/* Nothing to retry once the answer is a turn: its brief has left the
|
|
657
|
+
* field, and a reply is the next thing to send. */
|
|
658
|
+
this.nodes.retry.disabled = recorded;
|
|
490
659
|
this.syncRunEnabled();
|
|
660
|
+
this.syncConversation();
|
|
491
661
|
this.onRunStateChange('idle');
|
|
492
662
|
}
|
|
493
663
|
}
|
|
@@ -601,6 +771,7 @@
|
|
|
601
771
|
}
|
|
602
772
|
|
|
603
773
|
const value = output.value === undefined || output.value === null ? '' : String(output.value);
|
|
774
|
+
this.lastAnswer = value;
|
|
604
775
|
if (plan.mode === 'markdown') {
|
|
605
776
|
/* Structure only, and built node by node — see `outputBlocks`. The
|
|
606
777
|
* `<pre>` is emptied rather than left carrying the stream's plain copy
|
|
@@ -618,12 +789,13 @@
|
|
|
618
789
|
}
|
|
619
790
|
}
|
|
620
791
|
|
|
621
|
-
renderMarkdown(value) {
|
|
622
|
-
const target = this.nodes.rendered;
|
|
792
|
+
renderMarkdown(value, into) {
|
|
793
|
+
const target = into || this.nodes.rendered;
|
|
623
794
|
NS.viewModel.outputBlocks(value).forEach(function (block) {
|
|
624
795
|
if (block.kind === 'heading') {
|
|
625
|
-
/* Clamped to h2..h4: the page
|
|
626
|
-
*
|
|
796
|
+
/* Clamped to h2..h4: the page has an `<h1>` (the agent's name by
|
|
797
|
+
* default, or the host's own when `titleLevel` moves it), and an
|
|
798
|
+
* agent that emits `#` is describing its answer, not the page. */
|
|
627
799
|
const level = Math.min(4, Math.max(2, block.level + 1));
|
|
628
800
|
target.appendChild(el('h' + level, 'run-md-heading', block.text));
|
|
629
801
|
return;
|
|
@@ -643,10 +815,14 @@
|
|
|
643
815
|
}
|
|
644
816
|
|
|
645
817
|
offerTextDownload(value) {
|
|
818
|
+
this.nodes.rendered.appendChild(this.textDownloadLink(value));
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
textDownloadLink(value) {
|
|
646
822
|
const link = el('a', 'run-download', 'Download the result');
|
|
647
823
|
link.href = URL.createObjectURL(new Blob([value], { type: 'text/plain' }));
|
|
648
824
|
link.download = 'result.txt';
|
|
649
|
-
|
|
825
|
+
return link;
|
|
650
826
|
}
|
|
651
827
|
|
|
652
828
|
offerDownload(output) {
|
package/src/run_view_model.js
CHANGED
|
@@ -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,
|