@sigrix-io/gatehouse 0.1.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +164 -5
- package/package.json +6 -3
- package/src/gatehouse.css +202 -18
- package/src/host_blocks.js +770 -0
- package/src/playbook_contents.js +329 -0
- package/src/run_renderer.js +19 -3
- package/src/run_view_model.js +20 -1
|
@@ -0,0 +1,770 @@
|
|
|
1
|
+
/* Blocks a host draws from its own data: an app card, a list of them, a run
|
|
2
|
+
* history and a usage summary.
|
|
3
|
+
*
|
|
4
|
+
* The run page speaks Postern; these do not. They are handed documents the
|
|
5
|
+
* host built from what it already holds, so a page listing four apps wakes no
|
|
6
|
+
* runner to draw itself, and they fetch nothing at all. Every document carries
|
|
7
|
+
* `"v": 1`, and a block refuses a version it does not read, whole, rather than
|
|
8
|
+
* drawing whichever members it happens to recognise.
|
|
9
|
+
*
|
|
10
|
+
* **Nothing here uses `innerHTML`**, for the run page's reason: an app's name
|
|
11
|
+
* and summary are its author's prose, a model's name is whatever a runner
|
|
12
|
+
* reported, and all of it reaches the page through `textContent`. The only
|
|
13
|
+
* attributes written from the data are a link's address, which must be a web
|
|
14
|
+
* address, and the host's own ids, kept as data for the host's scripts.
|
|
15
|
+
*/
|
|
16
|
+
(function (global) {
|
|
17
|
+
const root = global || (typeof window !== 'undefined' ? window : {});
|
|
18
|
+
|
|
19
|
+
/* The one version of the three shapes this file reads. */
|
|
20
|
+
const VERSION = 1;
|
|
21
|
+
|
|
22
|
+
/* What an app's state is called here, and the tone it is drawn in. A state
|
|
23
|
+
* this client has not heard of is shown as the host spelled it, untoned,
|
|
24
|
+
* rather than guessed into one of these. */
|
|
25
|
+
const APP_STATES = {
|
|
26
|
+
ready: { label: 'Ready', tone: 'ok' },
|
|
27
|
+
needs_key: { label: 'Needs a key', tone: 'caution' },
|
|
28
|
+
starting: { label: 'Starting', tone: 'quiet' },
|
|
29
|
+
unavailable: { label: 'Unavailable', tone: 'blocked' },
|
|
30
|
+
ended: { label: 'Access ended', tone: 'quiet' },
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/* How a run ended. Same rule for an outcome this client does not know. */
|
|
34
|
+
const OUTCOMES = {
|
|
35
|
+
done: { label: 'Done', tone: 'ok' },
|
|
36
|
+
timed_out: { label: 'Timed out', tone: 'caution' },
|
|
37
|
+
failed: { label: 'Failed', tone: 'blocked' },
|
|
38
|
+
cancelled: { label: 'Cancelled', tone: 'quiet' },
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/* The share of a cap at or past which its meter turns to a warning. */
|
|
42
|
+
const NEAR_CAP = 0.8;
|
|
43
|
+
|
|
44
|
+
function el(tag, className, textContent) {
|
|
45
|
+
const node = document.createElement(tag);
|
|
46
|
+
if (className) node.className = className;
|
|
47
|
+
if (textContent !== undefined && textContent !== null) node.textContent = String(textContent);
|
|
48
|
+
return node;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function clear(node) {
|
|
52
|
+
while (node.firstChild) node.removeChild(node.firstChild);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function isObject(value) {
|
|
56
|
+
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/* ---------- reading the documents ----------
|
|
60
|
+
*
|
|
61
|
+
* Each reader answers `{ value }` or `{ refusal }`, never both. A refusal is
|
|
62
|
+
* one sentence naming what was wrong, because the person who reads it is
|
|
63
|
+
* whoever built the host's data, and "could not be shown" alone would send
|
|
64
|
+
* them to the source. A member the shape does not name is ignored. */
|
|
65
|
+
|
|
66
|
+
class Fault extends Error {}
|
|
67
|
+
|
|
68
|
+
function versionOf(doc, what) {
|
|
69
|
+
if (!isObject(doc)) throw new Fault(what + ' is not an object.');
|
|
70
|
+
if (doc.v !== VERSION) {
|
|
71
|
+
const given = doc.v === undefined ? 'no version' : 'version ' + String(doc.v);
|
|
72
|
+
throw new Fault(what + ' carries ' + given + ', and this client reads version ' + VERSION + '.');
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function textMember(doc, key, required) {
|
|
77
|
+
const value = doc[key];
|
|
78
|
+
if (value === undefined || value === null) {
|
|
79
|
+
if (required) throw new Fault('"' + key + '" is missing.');
|
|
80
|
+
return '';
|
|
81
|
+
}
|
|
82
|
+
if (typeof value !== 'string') throw new Fault('"' + key + '" is not text.');
|
|
83
|
+
if (required && !value.trim()) throw new Fault('"' + key + '" is empty.');
|
|
84
|
+
return value;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function countMember(doc, key) {
|
|
88
|
+
const value = doc[key];
|
|
89
|
+
if (!Number.isInteger(value) || value < 0) throw new Fault('"' + key + '" is not a count.');
|
|
90
|
+
return value;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function amountMember(doc, key, nullable) {
|
|
94
|
+
const value = doc[key];
|
|
95
|
+
if ((value === undefined || value === null) && nullable) return null;
|
|
96
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value < 0) {
|
|
97
|
+
throw new Fault('"' + key + '" is not an amount.');
|
|
98
|
+
}
|
|
99
|
+
return value;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function timeMember(doc, key, nullable) {
|
|
103
|
+
const value = doc[key];
|
|
104
|
+
if ((value === undefined || value === null) && nullable) return null;
|
|
105
|
+
if (typeof value !== 'string' || Number.isNaN(Date.parse(value))) {
|
|
106
|
+
throw new Fault('"' + key + '" is not a date.');
|
|
107
|
+
}
|
|
108
|
+
return value;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function listMember(doc, key) {
|
|
112
|
+
const value = doc[key];
|
|
113
|
+
if (value === undefined || value === null) return [];
|
|
114
|
+
if (!Array.isArray(value)) throw new Fault('"' + key + '" is not a list.');
|
|
115
|
+
return value;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/* An ISO 4217 code, which is what `Intl` formats a sum in. */
|
|
119
|
+
function currencyMember(doc, key) {
|
|
120
|
+
const value = textMember(doc, key, true);
|
|
121
|
+
if (!/^[A-Z]{3}$/.test(value)) throw new Fault('"' + key + '" is not a currency code.');
|
|
122
|
+
return value;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function read(what, doc, build) {
|
|
126
|
+
try {
|
|
127
|
+
versionOf(doc, what);
|
|
128
|
+
return { value: build(doc) };
|
|
129
|
+
} catch (err) {
|
|
130
|
+
if (err instanceof Fault) return { refusal: err.message };
|
|
131
|
+
throw err;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function readAppSummary(doc) {
|
|
136
|
+
return read('An app summary', doc, function (summary) {
|
|
137
|
+
return {
|
|
138
|
+
appId: textMember(summary, 'app_id', true),
|
|
139
|
+
name: textMember(summary, 'name', true),
|
|
140
|
+
summary: textMember(summary, 'summary', false),
|
|
141
|
+
by: textMember(summary, 'by', false),
|
|
142
|
+
state: textMember(summary, 'state', true),
|
|
143
|
+
missingKeys: listMember(summary, 'missing_keys').map(function (entry) {
|
|
144
|
+
if (!isObject(entry)) throw new Fault('"missing_keys" holds something other than a key.');
|
|
145
|
+
return {
|
|
146
|
+
env: textMember(entry, 'env', true),
|
|
147
|
+
purpose: textMember(entry, 'purpose', false),
|
|
148
|
+
signupUrl: textMember(entry, 'signup_url', false),
|
|
149
|
+
};
|
|
150
|
+
}),
|
|
151
|
+
accessEndsAt: timeMember(summary, 'access_ends_at', true),
|
|
152
|
+
lastRunAt: timeMember(summary, 'last_run_at', true),
|
|
153
|
+
runUrl: textMember(summary, 'run_url', false),
|
|
154
|
+
};
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function readRunRow(doc) {
|
|
159
|
+
return read('A run row', doc, function (row) {
|
|
160
|
+
const costEstimate = amountMember(row, 'cost_estimate', true);
|
|
161
|
+
return {
|
|
162
|
+
runId: textMember(row, 'run_id', false),
|
|
163
|
+
appId: textMember(row, 'app_id', true),
|
|
164
|
+
appName: textMember(row, 'app_name', true),
|
|
165
|
+
startedAt: timeMember(row, 'started_at', false),
|
|
166
|
+
durationSeconds: amountMember(row, 'duration_seconds', true),
|
|
167
|
+
model: textMember(row, 'model', false),
|
|
168
|
+
inputTokens: countMember(row, 'input_tokens'),
|
|
169
|
+
outputTokens: countMember(row, 'output_tokens'),
|
|
170
|
+
costEstimate,
|
|
171
|
+
currency: costEstimate === null ? '' : currencyMember(row, 'currency'),
|
|
172
|
+
outcome: textMember(row, 'outcome', true),
|
|
173
|
+
detail: textMember(row, 'detail', false),
|
|
174
|
+
};
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function readUsageSummary(doc) {
|
|
179
|
+
return read('A usage summary', doc, function (usage) {
|
|
180
|
+
if (!isObject(usage.period)) throw new Fault('"period" is missing.');
|
|
181
|
+
const caps = isObject(usage.caps) ? usage.caps : {};
|
|
182
|
+
return {
|
|
183
|
+
period: {
|
|
184
|
+
label: textMember(usage.period, 'label', true),
|
|
185
|
+
start: timeMember(usage.period, 'start', false),
|
|
186
|
+
end: timeMember(usage.period, 'end', false),
|
|
187
|
+
},
|
|
188
|
+
runs: countMember(usage, 'runs'),
|
|
189
|
+
runsDone: countMember(usage, 'runs_done'),
|
|
190
|
+
inputTokens: countMember(usage, 'input_tokens'),
|
|
191
|
+
outputTokens: countMember(usage, 'output_tokens'),
|
|
192
|
+
costEstimate: amountMember(usage, 'cost_estimate', false),
|
|
193
|
+
currency: currencyMember(usage, 'currency'),
|
|
194
|
+
caps: {
|
|
195
|
+
runsPerDay: caps.runs_per_day === undefined || caps.runs_per_day === null ? null : countMember(caps, 'runs_per_day'),
|
|
196
|
+
runsUsed: caps.runs_used === undefined || caps.runs_used === null ? null : countMember(caps, 'runs_used'),
|
|
197
|
+
spendPerPeriod: amountMember(caps, 'spend_per_period', true),
|
|
198
|
+
},
|
|
199
|
+
byApp: listMember(usage, 'by_app').map(function (entry) {
|
|
200
|
+
if (!isObject(entry)) throw new Fault('"by_app" holds something other than an app.');
|
|
201
|
+
return {
|
|
202
|
+
appId: textMember(entry, 'app_id', true),
|
|
203
|
+
appName: textMember(entry, 'app_name', true),
|
|
204
|
+
runs: countMember(entry, 'runs'),
|
|
205
|
+
inputTokens: countMember(entry, 'input_tokens'),
|
|
206
|
+
outputTokens: countMember(entry, 'output_tokens'),
|
|
207
|
+
costEstimate: amountMember(entry, 'cost_estimate', false),
|
|
208
|
+
};
|
|
209
|
+
}),
|
|
210
|
+
byDay: listMember(usage, 'by_day').map(function (entry) {
|
|
211
|
+
if (!isObject(entry)) throw new Fault('"by_day" holds something other than a day.');
|
|
212
|
+
return { date: timeMember(entry, 'date', false), runs: countMember(entry, 'runs') };
|
|
213
|
+
}),
|
|
214
|
+
};
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/* ---------- writing them down ---------- */
|
|
219
|
+
|
|
220
|
+
/* A link this file may draw: a web address and nothing else. `signup_url`
|
|
221
|
+
* comes from a runner's author, and an anchor is the one element through
|
|
222
|
+
* which a string becomes something a click runs, so a `javascript:` address
|
|
223
|
+
* is dropped rather than drawn. `relative` admits a path on the host's own
|
|
224
|
+
* page, which is what a host's `run_url` usually is. */
|
|
225
|
+
function webLink(value, relative) {
|
|
226
|
+
const candidate = String(value || '').trim();
|
|
227
|
+
if (!candidate) return '';
|
|
228
|
+
let parsed;
|
|
229
|
+
try {
|
|
230
|
+
parsed = relative ? new URL(candidate, root.document && root.document.baseURI) : new URL(candidate);
|
|
231
|
+
} catch (err) {
|
|
232
|
+
return '';
|
|
233
|
+
}
|
|
234
|
+
return parsed.protocol === 'http:' || parsed.protocol === 'https:' ? parsed.href : '';
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/* Numbers, sums and dates in the reader's own conventions, or in the ones a
|
|
238
|
+
* host names (`options.locale`, `options.timeZone`). A locale or a time zone
|
|
239
|
+
* `Intl` refuses falls back to the reader's own rather than failing the
|
|
240
|
+
* block. */
|
|
241
|
+
function formats(options) {
|
|
242
|
+
const settings = options || {};
|
|
243
|
+
let locale = settings.locale;
|
|
244
|
+
try {
|
|
245
|
+
new Intl.NumberFormat(locale);
|
|
246
|
+
} catch (err) {
|
|
247
|
+
locale = undefined;
|
|
248
|
+
}
|
|
249
|
+
let timeZone = settings.timeZone;
|
|
250
|
+
try {
|
|
251
|
+
new Intl.DateTimeFormat(locale, { timeZone });
|
|
252
|
+
} catch (err) {
|
|
253
|
+
timeZone = undefined;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function count(value) {
|
|
257
|
+
return new Intl.NumberFormat(locale).format(value);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/* A tile's figure, compact from 100,000 up. The exact figure stays in the
|
|
261
|
+
* per-app table's totals row. */
|
|
262
|
+
function figure(value) {
|
|
263
|
+
if (value < 100000) return count(value);
|
|
264
|
+
return new Intl.NumberFormat(locale, { notation: 'compact', maximumFractionDigits: 1 }).format(value);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/* A cost, to the cent. A run that cost less than one is said to, rather
|
|
268
|
+
* than rounded to nothing: "$0.00" reads as free. */
|
|
269
|
+
function money(value, currency) {
|
|
270
|
+
let format;
|
|
271
|
+
try {
|
|
272
|
+
format = new Intl.NumberFormat(locale, {
|
|
273
|
+
style: 'currency', currency, minimumFractionDigits: 2, maximumFractionDigits: 2,
|
|
274
|
+
});
|
|
275
|
+
} catch (err) {
|
|
276
|
+
format = { format: amount => amount.toFixed(2) + ' ' + currency };
|
|
277
|
+
}
|
|
278
|
+
if (value > 0 && value < 0.01) return '< ' + format.format(0.01);
|
|
279
|
+
return format.format(value);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/* A moment, in the reader's time zone. */
|
|
283
|
+
function moment(value) {
|
|
284
|
+
return new Intl.DateTimeFormat(locale, { dateStyle: 'medium', timeStyle: 'short', timeZone }).format(new Date(value));
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/* A calendar day. A bare date names a day rather than an instant, so it is
|
|
288
|
+
* read and written in UTC: in a time zone west of Greenwich, midnight UTC
|
|
289
|
+
* on the 1st is still the 31st. */
|
|
290
|
+
function day(value) {
|
|
291
|
+
const instant = /^\d{4}-\d{2}-\d{2}$/.test(value) ? new Date(value + 'T00:00:00Z') : new Date(value);
|
|
292
|
+
return new Intl.DateTimeFormat(locale, { dateStyle: 'medium', timeZone: 'UTC' }).format(instant);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function duration(seconds) {
|
|
296
|
+
if (seconds < 1) return 'under 1 s';
|
|
297
|
+
const whole = Math.round(seconds);
|
|
298
|
+
if (whole < 60) return whole + ' s';
|
|
299
|
+
const hours = Math.floor(whole / 3600);
|
|
300
|
+
const minutes = Math.floor((whole % 3600) / 60);
|
|
301
|
+
const rest = whole % 60;
|
|
302
|
+
if (hours) return hours + ' h' + (minutes ? ' ' + minutes + ' min' : '');
|
|
303
|
+
return minutes + ' min' + (rest ? ' ' + rest + ' s' : '');
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
return { count, figure, money, moment, day, duration };
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function plural(count, one, many) {
|
|
310
|
+
return count + ' ' + (count === 1 ? one : many);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/* A share as a CSS length, to a tenth of a percent. */
|
|
314
|
+
function percent(share) {
|
|
315
|
+
return Math.round(share * 1000) / 10 + '%';
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function headingLevel(options) {
|
|
319
|
+
const level = Number(options && options.headingLevel);
|
|
320
|
+
return Number.isInteger(level) && level >= 2 && level <= 6 ? level : 3;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/* A state or an outcome, said in words beside a dot of its tone: the words
|
|
324
|
+
* carry it, so a reader who cannot tell the colours apart loses nothing. */
|
|
325
|
+
function statusLabel(table, value) {
|
|
326
|
+
const known = Object.prototype.hasOwnProperty.call(table, value) ? table[value] : null;
|
|
327
|
+
const node = el('span', 'gh-status', known ? known.label : value);
|
|
328
|
+
node.classList.add('gh-status--' + (known ? known.tone : 'quiet'));
|
|
329
|
+
return node;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function refusalNote(sentence) {
|
|
333
|
+
return el('p', 'gh-refuse', sentence);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/* What a list could not show, said once below what it could, with the
|
|
337
|
+
* first reason: the rest are usually the same one. */
|
|
338
|
+
function skippedNote(refusals, one, many) {
|
|
339
|
+
if (!refusals.length) return null;
|
|
340
|
+
const count = plural(refusals.length, one, many);
|
|
341
|
+
return el('p', 'gh-note', refusals.length === 1
|
|
342
|
+
? count + ' could not be shown. ' + refusals[0]
|
|
343
|
+
: count + ' could not be shown. The first: ' + refusals[0]);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/* ---------- an app ---------- */
|
|
347
|
+
|
|
348
|
+
function drawApp(app, options) {
|
|
349
|
+
const formatted = formats(options);
|
|
350
|
+
const card = el('article', 'gh-block gh-app');
|
|
351
|
+
card.dataset.appId = app.appId;
|
|
352
|
+
|
|
353
|
+
const head = el('header', 'gh-app-head');
|
|
354
|
+
head.appendChild(el('h' + headingLevel(options), 'gh-app-name', app.name));
|
|
355
|
+
head.appendChild(statusLabel(APP_STATES, app.state));
|
|
356
|
+
card.appendChild(head);
|
|
357
|
+
|
|
358
|
+
if (app.summary) card.appendChild(el('p', 'gh-app-summary', app.summary));
|
|
359
|
+
if (app.by) card.appendChild(el('p', 'gh-app-by', 'by ' + app.by));
|
|
360
|
+
|
|
361
|
+
if (app.missingKeys.length) {
|
|
362
|
+
const keys = el('ul', 'gh-app-keys');
|
|
363
|
+
app.missingKeys.forEach(function (key) {
|
|
364
|
+
const item = el('li', 'gh-app-key');
|
|
365
|
+
item.appendChild(el('code', 'gh-app-key-env', key.env));
|
|
366
|
+
item.appendChild(el('span', 'gh-app-key-tick', ' not set'));
|
|
367
|
+
if (key.purpose) item.appendChild(el('span', 'gh-app-key-purpose', ' — ' + key.purpose));
|
|
368
|
+
const href = webLink(key.signupUrl, false);
|
|
369
|
+
if (href) {
|
|
370
|
+
const link = el('a', 'gh-app-key-link', 'Get a key');
|
|
371
|
+
link.href = href;
|
|
372
|
+
link.rel = 'noopener noreferrer';
|
|
373
|
+
link.target = '_blank';
|
|
374
|
+
item.appendChild(link);
|
|
375
|
+
}
|
|
376
|
+
keys.appendChild(item);
|
|
377
|
+
});
|
|
378
|
+
card.appendChild(keys);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
const facts = [];
|
|
382
|
+
if (app.lastRunAt) facts.push('Last run ' + formatted.moment(app.lastRunAt));
|
|
383
|
+
else if (app.state !== 'ended') facts.push('No runs yet');
|
|
384
|
+
if (app.accessEndsAt) {
|
|
385
|
+
facts.push((app.state === 'ended' ? 'Access ended ' : 'Access ends ') + formatted.day(app.accessEndsAt));
|
|
386
|
+
}
|
|
387
|
+
if (facts.length) card.appendChild(el('p', 'gh-app-meta', facts.join(' · ')));
|
|
388
|
+
|
|
389
|
+
const href = webLink(app.runUrl, true);
|
|
390
|
+
if (href) {
|
|
391
|
+
const open = el('a', 'gh-app-open', (options && options.openLabel) || 'Open');
|
|
392
|
+
open.href = href;
|
|
393
|
+
card.appendChild(open);
|
|
394
|
+
}
|
|
395
|
+
return card;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
class AppCard {
|
|
399
|
+
/* `summary` is one app summary; `options.headingLevel` (2 to 6, default
|
|
400
|
+
* 3) is the level of the app's name, `options.openLabel` the words of the
|
|
401
|
+
* link to its `run_url`. */
|
|
402
|
+
constructor(container, summary, options) {
|
|
403
|
+
this.container = container;
|
|
404
|
+
this.summary = summary;
|
|
405
|
+
this.options = options || {};
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
render() {
|
|
409
|
+
clear(this.container);
|
|
410
|
+
const read = readAppSummary(this.summary);
|
|
411
|
+
if (read.refusal) {
|
|
412
|
+
const block = el('div', 'gh-block');
|
|
413
|
+
block.appendChild(refusalNote('This app could not be shown. ' + read.refusal));
|
|
414
|
+
this.container.appendChild(block);
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
this.container.appendChild(drawApp(read.value, this.options));
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
update(summary) {
|
|
421
|
+
this.summary = summary;
|
|
422
|
+
this.render();
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
class AppList {
|
|
427
|
+
/* `summaries` is a list of app summaries, drawn as cards in the order
|
|
428
|
+
* given. `options.emptyText` is said when there are none; the rest are
|
|
429
|
+
* `AppCard`'s. */
|
|
430
|
+
constructor(container, summaries, options) {
|
|
431
|
+
this.container = container;
|
|
432
|
+
this.summaries = summaries;
|
|
433
|
+
this.options = options || {};
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
render() {
|
|
437
|
+
clear(this.container);
|
|
438
|
+
const block = el('section', 'gh-block gh-apps');
|
|
439
|
+
this.container.appendChild(block);
|
|
440
|
+
if (!Array.isArray(this.summaries)) {
|
|
441
|
+
block.appendChild(refusalNote('These apps could not be shown: the host handed in something other than a list.'));
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
const list = el('ul', 'gh-app-list');
|
|
445
|
+
const refusals = [];
|
|
446
|
+
const options = this.options;
|
|
447
|
+
this.summaries.forEach(function (doc) {
|
|
448
|
+
const read = readAppSummary(doc);
|
|
449
|
+
if (read.refusal) {
|
|
450
|
+
refusals.push(read.refusal);
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
const item = el('li', 'gh-app-item');
|
|
454
|
+
item.appendChild(drawApp(read.value, options));
|
|
455
|
+
list.appendChild(item);
|
|
456
|
+
});
|
|
457
|
+
if (list.childNodes.length) block.appendChild(list);
|
|
458
|
+
else if (!refusals.length) block.appendChild(el('p', 'gh-empty', options.emptyText || 'No apps yet.'));
|
|
459
|
+
const note = skippedNote(refusals, 'app', 'apps');
|
|
460
|
+
if (note) block.appendChild(note);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
update(summaries) {
|
|
464
|
+
this.summaries = summaries;
|
|
465
|
+
this.render();
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/* ---------- runs ---------- */
|
|
470
|
+
|
|
471
|
+
/* A figure's column is right-aligned on even-width digits, so a column of
|
|
472
|
+
* them reads by place value. */
|
|
473
|
+
function headCell(label, numeric) {
|
|
474
|
+
const node = el('th', 'gh-table-head', label);
|
|
475
|
+
if (numeric) node.classList.add('gh-num');
|
|
476
|
+
node.scope = 'col';
|
|
477
|
+
return node;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
function cell(value, numeric) {
|
|
481
|
+
const node = el('td', 'gh-table-cell', value);
|
|
482
|
+
if (numeric) node.classList.add('gh-num');
|
|
483
|
+
return node;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
class RunHistory {
|
|
487
|
+
/* `rows` is a list of run rows, drawn in the order given (a host sorts
|
|
488
|
+
* them). `options.showApp: false` drops the app column, for a page about
|
|
489
|
+
* one app; `options.emptyText` is said when there are none. */
|
|
490
|
+
constructor(container, rows, options) {
|
|
491
|
+
this.container = container;
|
|
492
|
+
this.rows = rows;
|
|
493
|
+
this.options = options || {};
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
render() {
|
|
497
|
+
clear(this.container);
|
|
498
|
+
const block = el('section', 'gh-block gh-runs');
|
|
499
|
+
this.container.appendChild(block);
|
|
500
|
+
if (!Array.isArray(this.rows)) {
|
|
501
|
+
block.appendChild(refusalNote('These runs could not be shown: the host handed in something other than a list.'));
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
const options = this.options;
|
|
505
|
+
const showApp = options.showApp !== false;
|
|
506
|
+
const formatted = formats(options);
|
|
507
|
+
const refusals = [];
|
|
508
|
+
const runs = [];
|
|
509
|
+
this.rows.forEach(function (doc) {
|
|
510
|
+
const read = readRunRow(doc);
|
|
511
|
+
if (read.refusal) refusals.push(read.refusal);
|
|
512
|
+
else runs.push(read.value);
|
|
513
|
+
});
|
|
514
|
+
|
|
515
|
+
if (!runs.length) {
|
|
516
|
+
if (!refusals.length) block.appendChild(el('p', 'gh-empty', options.emptyText || 'No runs yet.'));
|
|
517
|
+
} else {
|
|
518
|
+
/* Wide by nature, so it scrolls inside its own box rather than pushing
|
|
519
|
+
* the page sideways on a phone. The box takes focus so a keyboard can
|
|
520
|
+
* scroll it too. */
|
|
521
|
+
const scroller = el('div', 'gh-table-scroll');
|
|
522
|
+
scroller.tabIndex = 0;
|
|
523
|
+
scroller.setAttribute('role', 'region');
|
|
524
|
+
scroller.setAttribute('aria-label', 'Runs');
|
|
525
|
+
const table = el('table', 'gh-table gh-table--wide');
|
|
526
|
+
const head = el('thead');
|
|
527
|
+
const headRow = el('tr');
|
|
528
|
+
headRow.appendChild(headCell('Started', false));
|
|
529
|
+
if (showApp) headRow.appendChild(headCell('App', false));
|
|
530
|
+
headRow.appendChild(headCell('Outcome', false));
|
|
531
|
+
headRow.appendChild(headCell('Duration', true));
|
|
532
|
+
headRow.appendChild(headCell('Tokens in', true));
|
|
533
|
+
headRow.appendChild(headCell('Tokens out', true));
|
|
534
|
+
/* The column says what the figure is. A run's cost is the host's
|
|
535
|
+
* estimate from token counts and a price list, never a bill. */
|
|
536
|
+
headRow.appendChild(headCell('Cost (estimate)', true));
|
|
537
|
+
headRow.appendChild(headCell('Model', false));
|
|
538
|
+
head.appendChild(headRow);
|
|
539
|
+
table.appendChild(head);
|
|
540
|
+
|
|
541
|
+
const body = el('tbody');
|
|
542
|
+
runs.forEach(function (run) {
|
|
543
|
+
const row = el('tr', 'gh-run');
|
|
544
|
+
if (run.runId) row.dataset.runId = run.runId;
|
|
545
|
+
row.appendChild(cell(formatted.moment(run.startedAt), false));
|
|
546
|
+
if (showApp) row.appendChild(cell(run.appName, false));
|
|
547
|
+
const outcome = el('td', 'gh-table-cell');
|
|
548
|
+
outcome.appendChild(statusLabel(OUTCOMES, run.outcome));
|
|
549
|
+
if (run.detail) outcome.appendChild(el('span', 'gh-run-detail', run.detail));
|
|
550
|
+
row.appendChild(outcome);
|
|
551
|
+
row.appendChild(cell(run.durationSeconds === null ? '—' : formatted.duration(run.durationSeconds), true));
|
|
552
|
+
row.appendChild(cell(formatted.count(run.inputTokens), true));
|
|
553
|
+
row.appendChild(cell(formatted.count(run.outputTokens), true));
|
|
554
|
+
row.appendChild(cell(run.costEstimate === null ? '—' : formatted.money(run.costEstimate, run.currency), true));
|
|
555
|
+
row.appendChild(cell(run.model || '—', false));
|
|
556
|
+
body.appendChild(row);
|
|
557
|
+
});
|
|
558
|
+
table.appendChild(body);
|
|
559
|
+
scroller.appendChild(table);
|
|
560
|
+
block.appendChild(scroller);
|
|
561
|
+
}
|
|
562
|
+
const note = skippedNote(refusals, 'run', 'runs');
|
|
563
|
+
if (note) block.appendChild(note);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
update(rows) {
|
|
567
|
+
this.rows = rows;
|
|
568
|
+
this.render();
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/* ---------- usage ---------- */
|
|
573
|
+
|
|
574
|
+
function stat(label, value, note) {
|
|
575
|
+
const tile = el('div', 'gh-usage-stat');
|
|
576
|
+
tile.appendChild(el('dt', 'gh-usage-label', label));
|
|
577
|
+
tile.appendChild(el('dd', 'gh-usage-value', value));
|
|
578
|
+
if (note) tile.appendChild(el('dd', 'gh-usage-note', note));
|
|
579
|
+
return tile;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/* One cap: the sentence first, since it carries the numbers, then a bar
|
|
583
|
+
* that shows the same share at a glance and is hidden from a screen reader,
|
|
584
|
+
* which has the sentence. */
|
|
585
|
+
function capRow(sentence, used, limit) {
|
|
586
|
+
const row = el('div', 'gh-cap');
|
|
587
|
+
row.appendChild(el('p', 'gh-cap-label', sentence));
|
|
588
|
+
const share = limit > 0 ? Math.min(1, used / limit) : 1;
|
|
589
|
+
const meter = el('div', 'gh-meter');
|
|
590
|
+
meter.classList.add(share >= 1 ? 'gh-meter--full' : share >= NEAR_CAP ? 'gh-meter--near' : 'gh-meter--clear');
|
|
591
|
+
meter.setAttribute('aria-hidden', 'true');
|
|
592
|
+
const fill = el('span', 'gh-meter-fill');
|
|
593
|
+
/* Through the object model rather than a `style` attribute, which a
|
|
594
|
+
* strict content security policy refuses and this property is not. */
|
|
595
|
+
fill.style.width = percent(share);
|
|
596
|
+
meter.appendChild(fill);
|
|
597
|
+
row.appendChild(meter);
|
|
598
|
+
return row;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
/* Runs per day as columns. The tallest is labelled; the rest show on hover,
|
|
602
|
+
* and every day's figure is in the table beneath, so no value is reachable
|
|
603
|
+
* only by pointing at it. */
|
|
604
|
+
function dayChart(days, formatted) {
|
|
605
|
+
const figure = el('figure', 'gh-days');
|
|
606
|
+
figure.appendChild(el('figcaption', 'gh-days-title', 'Runs per day'));
|
|
607
|
+
const most = days.reduce((top, entry) => Math.max(top, entry.runs), 0);
|
|
608
|
+
const plot = el('div', 'gh-days-plot');
|
|
609
|
+
plot.setAttribute('aria-hidden', 'true');
|
|
610
|
+
plot.appendChild(el('span', 'gh-days-max', plural(most, 'run', 'runs')));
|
|
611
|
+
const bars = el('ol', 'gh-days-bars');
|
|
612
|
+
days.forEach(function (entry) {
|
|
613
|
+
const item = el('li', 'gh-day');
|
|
614
|
+
const bar = el('span', 'gh-day-bar');
|
|
615
|
+
bar.style.height = percent(most ? entry.runs / most : 0);
|
|
616
|
+
item.appendChild(bar);
|
|
617
|
+
item.appendChild(el('span', 'gh-day-tip', formatted.day(entry.date) + ' · ' + plural(entry.runs, 'run', 'runs')));
|
|
618
|
+
bars.appendChild(item);
|
|
619
|
+
});
|
|
620
|
+
plot.appendChild(bars);
|
|
621
|
+
figure.appendChild(plot);
|
|
622
|
+
const axis = el('div', 'gh-days-axis');
|
|
623
|
+
axis.setAttribute('aria-hidden', 'true');
|
|
624
|
+
axis.appendChild(el('span', 'gh-days-axis-label', formatted.day(days[0].date)));
|
|
625
|
+
if (days.length > 1) axis.appendChild(el('span', 'gh-days-axis-label', formatted.day(days[days.length - 1].date)));
|
|
626
|
+
figure.appendChild(axis);
|
|
627
|
+
|
|
628
|
+
const table = el('details', 'gh-days-table');
|
|
629
|
+
table.appendChild(el('summary', 'gh-days-table-toggle', 'Each day as a table'));
|
|
630
|
+
const grid = el('table', 'gh-table');
|
|
631
|
+
const head = el('thead');
|
|
632
|
+
const headRow = el('tr');
|
|
633
|
+
headRow.appendChild(headCell('Day', false));
|
|
634
|
+
headRow.appendChild(headCell('Runs', true));
|
|
635
|
+
head.appendChild(headRow);
|
|
636
|
+
grid.appendChild(head);
|
|
637
|
+
const body = el('tbody');
|
|
638
|
+
days.forEach(function (entry) {
|
|
639
|
+
const row = el('tr');
|
|
640
|
+
row.appendChild(cell(formatted.day(entry.date), false));
|
|
641
|
+
row.appendChild(cell(formatted.count(entry.runs), true));
|
|
642
|
+
body.appendChild(row);
|
|
643
|
+
});
|
|
644
|
+
grid.appendChild(body);
|
|
645
|
+
table.appendChild(grid);
|
|
646
|
+
figure.appendChild(table);
|
|
647
|
+
return figure;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
function appTable(usage, formatted) {
|
|
651
|
+
const scroller = el('div', 'gh-table-scroll');
|
|
652
|
+
scroller.tabIndex = 0;
|
|
653
|
+
scroller.setAttribute('role', 'region');
|
|
654
|
+
scroller.setAttribute('aria-label', 'Usage by app');
|
|
655
|
+
const table = el('table', 'gh-table');
|
|
656
|
+
const head = el('thead');
|
|
657
|
+
const headRow = el('tr');
|
|
658
|
+
headRow.appendChild(headCell('App', false));
|
|
659
|
+
headRow.appendChild(headCell('Runs', true));
|
|
660
|
+
headRow.appendChild(headCell('Tokens in', true));
|
|
661
|
+
headRow.appendChild(headCell('Tokens out', true));
|
|
662
|
+
headRow.appendChild(headCell('Cost (estimate)', true));
|
|
663
|
+
head.appendChild(headRow);
|
|
664
|
+
table.appendChild(head);
|
|
665
|
+
const body = el('tbody');
|
|
666
|
+
usage.byApp.forEach(function (app) {
|
|
667
|
+
const row = el('tr');
|
|
668
|
+
row.dataset.appId = app.appId;
|
|
669
|
+
row.appendChild(cell(app.appName, false));
|
|
670
|
+
row.appendChild(cell(formatted.count(app.runs), true));
|
|
671
|
+
row.appendChild(cell(formatted.count(app.inputTokens), true));
|
|
672
|
+
row.appendChild(cell(formatted.count(app.outputTokens), true));
|
|
673
|
+
row.appendChild(cell(formatted.money(app.costEstimate, usage.currency), true));
|
|
674
|
+
body.appendChild(row);
|
|
675
|
+
});
|
|
676
|
+
table.appendChild(body);
|
|
677
|
+
/* The period's own totals, exactly, which the tiles above round. */
|
|
678
|
+
const foot = el('tfoot');
|
|
679
|
+
const total = el('tr', 'gh-table-total');
|
|
680
|
+
total.appendChild(cell('All apps', false));
|
|
681
|
+
total.appendChild(cell(formatted.count(usage.runs), true));
|
|
682
|
+
total.appendChild(cell(formatted.count(usage.inputTokens), true));
|
|
683
|
+
total.appendChild(cell(formatted.count(usage.outputTokens), true));
|
|
684
|
+
total.appendChild(cell(formatted.money(usage.costEstimate, usage.currency), true));
|
|
685
|
+
foot.appendChild(total);
|
|
686
|
+
table.appendChild(foot);
|
|
687
|
+
scroller.appendChild(table);
|
|
688
|
+
return scroller;
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
class UsageSummary {
|
|
692
|
+
/* `usage` is one usage summary. `options.headingLevel` (2 to 6, default
|
|
693
|
+
* 3) is the level of the period's name. */
|
|
694
|
+
constructor(container, usage, options) {
|
|
695
|
+
this.container = container;
|
|
696
|
+
this.usage = usage;
|
|
697
|
+
this.options = options || {};
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
render() {
|
|
701
|
+
clear(this.container);
|
|
702
|
+
const block = el('section', 'gh-block gh-usage');
|
|
703
|
+
this.container.appendChild(block);
|
|
704
|
+
const read = readUsageSummary(this.usage);
|
|
705
|
+
if (read.refusal) {
|
|
706
|
+
block.appendChild(refusalNote('This usage could not be shown. ' + read.refusal));
|
|
707
|
+
return;
|
|
708
|
+
}
|
|
709
|
+
const usage = read.value;
|
|
710
|
+
const formatted = formats(this.options);
|
|
711
|
+
|
|
712
|
+
const head = el('header', 'gh-usage-head');
|
|
713
|
+
head.appendChild(el('h' + headingLevel(this.options), 'gh-usage-title', usage.period.label));
|
|
714
|
+
head.appendChild(el('p', 'gh-usage-dates', formatted.day(usage.period.start) + ' – ' + formatted.day(usage.period.end)));
|
|
715
|
+
block.appendChild(head);
|
|
716
|
+
|
|
717
|
+
const stats = el('dl', 'gh-usage-stats');
|
|
718
|
+
stats.appendChild(stat('Runs', formatted.figure(usage.runs), plural(usage.runsDone, 'finished', 'finished')));
|
|
719
|
+
stats.appendChild(stat('Tokens in', formatted.figure(usage.inputTokens)));
|
|
720
|
+
stats.appendChild(stat('Tokens out', formatted.figure(usage.outputTokens)));
|
|
721
|
+
/* Named as an estimate where it is drawn, the way the run page labels a
|
|
722
|
+
* runner's `cost_usd`: a figure on a dashboard is read as a bill unless
|
|
723
|
+
* it says otherwise. */
|
|
724
|
+
stats.appendChild(stat('Estimated cost', formatted.money(usage.costEstimate, usage.currency), 'An estimate, not a bill'));
|
|
725
|
+
block.appendChild(stats);
|
|
726
|
+
|
|
727
|
+
const caps = el('div', 'gh-caps');
|
|
728
|
+
if (usage.caps.runsPerDay !== null && usage.caps.runsUsed !== null) {
|
|
729
|
+
caps.appendChild(capRow(
|
|
730
|
+
usage.caps.runsUsed + ' of ' + plural(usage.caps.runsPerDay, 'run', 'runs') + ' in the last 24 hours' +
|
|
731
|
+
(usage.caps.runsUsed >= usage.caps.runsPerDay ? ' — the limit is reached' : ''),
|
|
732
|
+
usage.caps.runsUsed,
|
|
733
|
+
usage.caps.runsPerDay
|
|
734
|
+
));
|
|
735
|
+
}
|
|
736
|
+
if (usage.caps.spendPerPeriod !== null) {
|
|
737
|
+
caps.appendChild(capRow(
|
|
738
|
+
formatted.money(usage.costEstimate, usage.currency) + ' of ' +
|
|
739
|
+
formatted.money(usage.caps.spendPerPeriod, usage.currency) + ' estimated spend this period' +
|
|
740
|
+
(usage.costEstimate >= usage.caps.spendPerPeriod ? ' — the cap is reached' : ''),
|
|
741
|
+
usage.costEstimate,
|
|
742
|
+
usage.caps.spendPerPeriod
|
|
743
|
+
));
|
|
744
|
+
}
|
|
745
|
+
if (caps.childNodes.length) block.appendChild(caps);
|
|
746
|
+
|
|
747
|
+
if (usage.byDay.length) block.appendChild(dayChart(usage.byDay, formatted));
|
|
748
|
+
if (usage.byApp.length) block.appendChild(appTable(usage, formatted));
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
update(usage) {
|
|
752
|
+
this.usage = usage;
|
|
753
|
+
this.render();
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
root.Gatehouse = Object.assign({}, root.Gatehouse, {
|
|
758
|
+
AppCard,
|
|
759
|
+
AppList,
|
|
760
|
+
RunHistory,
|
|
761
|
+
UsageSummary,
|
|
762
|
+
hostData: { VERSION, readAppSummary, readRunRow, readUsageSummary },
|
|
763
|
+
});
|
|
764
|
+
|
|
765
|
+
/* `SigrixRun` is the name this package was born under, inside one host. It
|
|
766
|
+
* is kept pointing at the same object so that host keeps working across the
|
|
767
|
+
* extraction, and because a global is the one part of a browser package a
|
|
768
|
+
* consumer cannot alias for itself. Deprecated; removed at 1.0. */
|
|
769
|
+
root.SigrixRun = root.Gatehouse;
|
|
770
|
+
})(typeof window !== 'undefined' ? window : globalThis);
|