wizards 0.2.56 → 0.2.58
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/apps/runtime/dist/db/control-schema.js +13 -0
- package/apps/runtime/dist/db/migrations/control/0008_runner.sql +8 -0
- package/apps/runtime/dist/db/migrations/control/meta/0008_snapshot.json +505 -0
- package/apps/runtime/dist/db/migrations/control/meta/_journal.json +7 -0
- package/apps/runtime/dist/db/migrations/tenant/0014_run_answer.sql +2 -0
- package/apps/runtime/dist/db/migrations/tenant/0015_device_calls.sql +13 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0014_snapshot.json +3154 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/0015_snapshot.json +3243 -0
- package/apps/runtime/dist/db/migrations/tenant/meta/_journal.json +14 -0
- package/apps/runtime/dist/db/schema.js +23 -0
- package/apps/runtime/dist/engine/asks.js +53 -33
- package/apps/runtime/dist/engine/device.js +61 -49
- package/apps/runtime/dist/engine/events.js +71 -4
- package/apps/runtime/dist/engine/owner.js +32 -0
- package/apps/runtime/dist/engine/runner.js +94 -79
- package/apps/runtime/dist/env.js +19 -0
- package/apps/runtime/dist/index.js +13 -2
- package/apps/runtime/dist/models.js +1 -1
- package/apps/runtime/dist/plugins/runs.js +5 -8
- package/apps/runtime/dist/routes/runs.js +31 -15
- package/apps/runtime/dist/services/runs.js +7 -8
- package/apps/runtime/dist/tenants/control.js +85 -7
- package/apps/web/dist/assets/{AboutDialog-Bv7p9iGr.js → AboutDialog-eIamCG0d.js} +1 -1
- package/apps/web/dist/assets/{ChangelogDialog-D4sBrhi-.js → ChangelogDialog-Cbv1DEkz.js} +1 -1
- package/apps/web/dist/assets/{ChatView-Bntm0u8V.js → ChatView-B1Thux1N.js} +1 -1
- package/apps/web/dist/assets/{EditorPage-DaVeNzHU.js → EditorPage-Ck1Hq5Ke.js} +1 -1
- package/apps/web/dist/assets/{FlowPreview-C0tTz51A.js → FlowPreview-D5W8dDxx.js} +1 -1
- package/apps/web/dist/assets/{Harness-De-PsLFA.js → Harness-Bhq2tE15.js} +1 -1
- package/apps/web/dist/assets/{Marketplace-7Bdv7Gd5.js → Marketplace-C-Nr-SaJ.js} +2 -2
- package/apps/web/dist/assets/{NewWizardPage-CV7jrSkz.js → NewWizardPage-DgSPRlcs.js} +1 -1
- package/apps/web/dist/assets/{ProjectPage-CBCd24oM.js → ProjectPage-xsMCXYyc.js} +1 -1
- package/apps/web/dist/assets/{PublicRunner-CVmkHSnC.js → PublicRunner-wW-52s2_.js} +1 -1
- package/apps/web/dist/assets/{RunDrawer-CvzuYc0d.js → RunDrawer-i9BHc1lO.js} +1 -1
- package/apps/web/dist/assets/{RunnerView-BtZp1FD6.js → RunnerView-DpVGsCA8.js} +1 -1
- package/apps/web/dist/assets/{SettingsPage--Zj2pTgf.js → SettingsPage-Ro_z_14X.js} +1 -1
- package/apps/web/dist/assets/{SetupPage-F_e8f1to.js → SetupPage-CXbXu-xu.js} +1 -1
- package/apps/web/dist/assets/{SharePage-DLivbYu6.js → SharePage-DwHMZwlW.js} +1 -1
- package/apps/web/dist/assets/changelog-BrLNXOV8.js +1 -0
- package/apps/web/dist/assets/{esm-BMtXxAe0.js → esm-CuG3O0f6.js} +1 -1
- package/apps/web/dist/assets/{index-MlQy_Qoy.js → index-BUUosGDd.js} +4 -4
- package/apps/web/dist/assets/{settings-menu-JtMfy9en.js → settings-menu-Bf9lWtfx.js} +1 -1
- package/apps/web/dist/index.html +2 -2
- package/node_modules/@engenty-wizards/shared/dist/definition.js +1 -0
- package/node_modules/@engenty-wizards/shared/dist/engenty/data-tables/columns.js +1 -0
- package/node_modules/@engenty-wizards/shared/dist/fit.js +1 -0
- package/node_modules/@engenty-wizards/shared/dist/store.js +1 -0
- package/node_modules/@engenty-wizards/shared/dist/surface.js +1 -0
- package/package.json +1 -1
- package/apps/web/dist/assets/changelog-CWualFcc.js +0 -1
|
@@ -99,6 +99,20 @@
|
|
|
99
99
|
"when": 1791728617995,
|
|
100
100
|
"tag": "0013_run_origin",
|
|
101
101
|
"breakpoints": true
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
"idx": 14,
|
|
105
|
+
"version": "6",
|
|
106
|
+
"when": 1791752213176,
|
|
107
|
+
"tag": "0014_run_answer",
|
|
108
|
+
"breakpoints": true
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
"idx": 15,
|
|
112
|
+
"version": "6",
|
|
113
|
+
"when": 1791752504092,
|
|
114
|
+
"tag": "0015_device_calls",
|
|
115
|
+
"breakpoints": true
|
|
102
116
|
}
|
|
103
117
|
]
|
|
104
118
|
}
|
|
@@ -115,6 +115,15 @@ export const run = sqliteTable("run", {
|
|
|
115
115
|
error: text("error"),
|
|
116
116
|
/** What a running step is asking the person right now (a login, a code); null = nothing. */
|
|
117
117
|
ask: text("ask", { mode: "json" }).$type(),
|
|
118
|
+
/** The person's answer to `ask`, written by the web for the runner that waits on it. */
|
|
119
|
+
answer: text("answer", { mode: "json" }).$type(),
|
|
120
|
+
/** An autopilot drives this test run (an MCP client fills its pages): it asks nobody. */
|
|
121
|
+
unattended: integer("unattended", { mode: "boolean" }).notNull().default(false),
|
|
122
|
+
/**
|
|
123
|
+
* What the device the run is watched from offers as a model (the app's Apple Intelligence),
|
|
124
|
+
* with when it last said so; written by the web, read by the runner (engine/device.ts).
|
|
125
|
+
*/
|
|
126
|
+
deviceOffer: text("device_offer", { mode: "json" }).$type(),
|
|
118
127
|
costMicros: integer("cost_micros").notNull().default(0),
|
|
119
128
|
/** Set once the result is shared: `/s/<token>` shows it read-only. */
|
|
120
129
|
shareToken: text("share_token"),
|
|
@@ -144,6 +153,20 @@ export const runEvent = sqliteTable("run_event", {
|
|
|
144
153
|
assetId: text("asset_id"),
|
|
145
154
|
createdAt: createdAt(),
|
|
146
155
|
}, (t) => [index("run_event_run").on(t.runId, t.id)]);
|
|
156
|
+
/**
|
|
157
|
+
* A call the runner put to the run's device, open until the device answers or the runner stops
|
|
158
|
+
* waiting: the stream (any web process) carries it to the phone, and the answer comes back here.
|
|
159
|
+
*/
|
|
160
|
+
export const deviceCall = sqliteTable("device_call", {
|
|
161
|
+
id: integer("id").primaryKey({ autoIncrement: true }),
|
|
162
|
+
runId: text("run_id")
|
|
163
|
+
.notNull()
|
|
164
|
+
.references(() => run.id, { onDelete: "cascade" }),
|
|
165
|
+
reqId: text("req_id").notNull(),
|
|
166
|
+
request: text("request", { mode: "json" }).$type().notNull(),
|
|
167
|
+
answer: text("answer", { mode: "json" }).$type(),
|
|
168
|
+
createdAt: createdAt(),
|
|
169
|
+
}, (t) => [uniqueIndex("device_call_req").on(t.reqId), index("device_call_run").on(t.runId)]);
|
|
147
170
|
/** What each step of a run cost, in credit micros: the measurements estimates are made from. */
|
|
148
171
|
export const runCost = sqliteTable("run_cost", {
|
|
149
172
|
runId: text("run_id")
|
|
@@ -1,62 +1,82 @@
|
|
|
1
|
-
import { eq } from "drizzle-orm";
|
|
1
|
+
import { eq, sql } from "drizzle-orm";
|
|
2
2
|
import { nanoid } from "nanoid";
|
|
3
3
|
import { db, schema } from "../db/client.js";
|
|
4
4
|
import { signalChanged } from "./events.js";
|
|
5
5
|
import { pushRun } from "./push.js";
|
|
6
6
|
/** How long a step waits for the person before it goes on without them. */
|
|
7
7
|
const ASK_TIMEOUT_MS = 15 * 60_000;
|
|
8
|
-
/**
|
|
9
|
-
|
|
10
|
-
const waiting = new Map();
|
|
8
|
+
/** How often the waiting step looks for the answer in the row. */
|
|
9
|
+
const ASK_POLL_MS = 1000;
|
|
11
10
|
async function setAsk(runId, ask) {
|
|
12
|
-
await db
|
|
11
|
+
await db
|
|
12
|
+
.update(schema.run)
|
|
13
|
+
.set({ ask, answer: null, updatedAt: new Date() })
|
|
14
|
+
.where(eq(schema.run.id, runId));
|
|
13
15
|
signalChanged(runId);
|
|
14
16
|
}
|
|
15
17
|
/**
|
|
16
18
|
* Puts a question to the person and waits. The run stays "running": the step is in the middle
|
|
17
|
-
* of its work and goes on with the answer. One question per run at a time.
|
|
19
|
+
* of its work and goes on with the answer. One question per run at a time. The question and the
|
|
20
|
+
* answer go through the run's row: the web that takes the answer need not be the process that
|
|
21
|
+
* asked.
|
|
18
22
|
*/
|
|
19
23
|
export async function askPerson(runId, ask, signal) {
|
|
20
|
-
|
|
24
|
+
const open = await db.query.run.findFirst({
|
|
25
|
+
where: eq(schema.run.id, runId),
|
|
26
|
+
columns: { ask: true },
|
|
27
|
+
});
|
|
28
|
+
if (open?.ask) {
|
|
21
29
|
throw new Error("The wizard is already waiting for the person.");
|
|
22
30
|
}
|
|
23
31
|
const full = { ...ask, id: nanoid(10), at: new Date().toISOString() };
|
|
24
|
-
const result = new Promise((resolve) => {
|
|
25
|
-
const finish = (r) => {
|
|
26
|
-
clearTimeout(timer);
|
|
27
|
-
signal.removeEventListener("abort", onAbort);
|
|
28
|
-
resolve(r);
|
|
29
|
-
};
|
|
30
|
-
const onAbort = () => finish({ type: "skip" });
|
|
31
|
-
const timer = setTimeout(() => finish({ type: "timeout" }), ASK_TIMEOUT_MS);
|
|
32
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
33
|
-
waiting.set(runId, { ask: full, settle: finish });
|
|
34
|
-
});
|
|
35
32
|
await setAsk(runId, full);
|
|
36
33
|
void pushRun(runId, "asks");
|
|
34
|
+
const until = Date.now() + ASK_TIMEOUT_MS;
|
|
37
35
|
try {
|
|
38
|
-
|
|
36
|
+
while (!signal.aborted) {
|
|
37
|
+
if (Date.now() >= until) {
|
|
38
|
+
return { type: "timeout" };
|
|
39
|
+
}
|
|
40
|
+
const row = await db.query.run.findFirst({
|
|
41
|
+
where: eq(schema.run.id, runId),
|
|
42
|
+
columns: { answer: true },
|
|
43
|
+
});
|
|
44
|
+
if (row?.answer?.askId === full.id) {
|
|
45
|
+
return row.answer.answer;
|
|
46
|
+
}
|
|
47
|
+
await new Promise((resolve) => {
|
|
48
|
+
const timer = setTimeout(done, ASK_POLL_MS);
|
|
49
|
+
function done() {
|
|
50
|
+
clearTimeout(timer);
|
|
51
|
+
signal.removeEventListener("abort", done);
|
|
52
|
+
resolve();
|
|
53
|
+
}
|
|
54
|
+
signal.addEventListener("abort", done, { once: true });
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return { type: "skip" };
|
|
39
58
|
}
|
|
40
59
|
finally {
|
|
41
|
-
waiting.delete(runId);
|
|
42
60
|
await setAsk(runId, null).catch(() => undefined);
|
|
43
61
|
}
|
|
44
62
|
}
|
|
45
63
|
/** The question a run is waiting on, if any. */
|
|
46
|
-
export function pendingAsk(runId) {
|
|
47
|
-
|
|
64
|
+
export async function pendingAsk(runId) {
|
|
65
|
+
const row = await db.query.run.findFirst({
|
|
66
|
+
where: eq(schema.run.id, runId),
|
|
67
|
+
columns: { ask: true },
|
|
68
|
+
});
|
|
69
|
+
return row?.ask ?? null;
|
|
48
70
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
return
|
|
71
|
+
/** Leaves the answer for the waiting step; false when that question is no longer open. */
|
|
72
|
+
export async function answerAsk(runId, askId, answer) {
|
|
73
|
+
const result = await db
|
|
74
|
+
.update(schema.run)
|
|
75
|
+
.set({ answer: { askId, answer } })
|
|
76
|
+
.where(sql `${schema.run.id} = ${runId} AND json_extract(${schema.run.ask}, '$.id') = ${askId}`);
|
|
77
|
+
return result.rowsAffected > 0;
|
|
56
78
|
}
|
|
57
|
-
/** A
|
|
79
|
+
/** A run a runner takes starts its step afresh: a question left in the row has no one waiting. */
|
|
58
80
|
export async function clearStaleAsk(runId) {
|
|
59
|
-
|
|
60
|
-
await db.update(schema.run).set({ ask: null }).where(eq(schema.run.id, runId));
|
|
61
|
-
}
|
|
81
|
+
await db.update(schema.run).set({ ask: null, answer: null }).where(eq(schema.run.id, runId));
|
|
62
82
|
}
|
|
@@ -1,71 +1,83 @@
|
|
|
1
|
+
import { and, asc, eq, isNull } from "drizzle-orm";
|
|
1
2
|
import { nanoid } from "nanoid";
|
|
3
|
+
import { db, schema } from "../db/client.js";
|
|
2
4
|
import { parseJsonAnswer, renderPrompt } from "../harness/prompt.js";
|
|
3
|
-
import { signalDevice } from "./events.js";
|
|
4
5
|
/** An offer not renewed for this long is forgotten: the person left the run. */
|
|
5
6
|
const OFFER_TTL_MS = 30 * 60_000;
|
|
6
7
|
/** How long the phone has to answer before the runtime thinks on its own. */
|
|
7
8
|
export const DEVICE_TIMEOUT_MS = 60_000;
|
|
8
|
-
|
|
9
|
-
const
|
|
9
|
+
/** How often a waiting call looks for the answer in the database. */
|
|
10
|
+
const ANSWER_POLL_MS = 250;
|
|
10
11
|
/** The runner says what the device around it can do; renewed on every (re)connect. */
|
|
11
|
-
export function offerDevice(runId, offer) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
offers.delete(runId);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
export function forgetDevice(runId) {
|
|
20
|
-
offers.delete(runId);
|
|
12
|
+
export async function offerDevice(runId, offer) {
|
|
13
|
+
await db
|
|
14
|
+
.update(schema.run)
|
|
15
|
+
.set({ deviceOffer: offer.think?.classes.length ? { offer, at: Date.now() } : null })
|
|
16
|
+
.where(eq(schema.run.id, runId));
|
|
21
17
|
}
|
|
22
18
|
/** Whether the device of this run thinks the class, and how much it takes. */
|
|
23
|
-
export function deviceThinks(runId, cls) {
|
|
24
|
-
const
|
|
19
|
+
export async function deviceThinks(runId, cls) {
|
|
20
|
+
const row = await db.query.run.findFirst({
|
|
21
|
+
where: eq(schema.run.id, runId),
|
|
22
|
+
columns: { deviceOffer: true },
|
|
23
|
+
});
|
|
24
|
+
const hit = row?.deviceOffer;
|
|
25
25
|
if (!hit || Date.now() - hit.at > OFFER_TTL_MS) {
|
|
26
|
-
offers.delete(runId);
|
|
27
26
|
return null;
|
|
28
27
|
}
|
|
29
28
|
const think = hit.offer.think;
|
|
30
29
|
return think?.classes.includes(cls) ? { maxChars: think.maxChars } : null;
|
|
31
30
|
}
|
|
31
|
+
/** The calls of a run still waiting for the device, oldest first: what its stream carries. */
|
|
32
|
+
export async function openDeviceCalls(runId) {
|
|
33
|
+
const rows = await db
|
|
34
|
+
.select({ request: schema.deviceCall.request })
|
|
35
|
+
.from(schema.deviceCall)
|
|
36
|
+
.where(and(eq(schema.deviceCall.runId, runId), isNull(schema.deviceCall.answer)))
|
|
37
|
+
.orderBy(asc(schema.deviceCall.id));
|
|
38
|
+
return rows.map((r) => r.request);
|
|
39
|
+
}
|
|
32
40
|
/** Sends one call to the device and waits for its answer. */
|
|
33
|
-
export function askDevice(runId, request, timeoutMs = DEVICE_TIMEOUT_MS) {
|
|
41
|
+
export async function askDevice(runId, request, timeoutMs = DEVICE_TIMEOUT_MS) {
|
|
34
42
|
const id = nanoid(10);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
/** The device's answer to one call; false when the call is unknown or not this run's. */
|
|
55
|
-
export function answerDevice(runId, reqId, answer) {
|
|
56
|
-
const hit = pending.get(reqId);
|
|
57
|
-
if (!hit || hit.runId !== runId) {
|
|
58
|
-
return false;
|
|
59
|
-
}
|
|
60
|
-
pending.delete(reqId);
|
|
61
|
-
if (typeof answer.text === "string") {
|
|
62
|
-
console.log(`[run ${runId}] the device answered a ${answer.text.length}-character call`);
|
|
63
|
-
hit.resolve(answer.text);
|
|
43
|
+
await db.insert(schema.deviceCall).values({ runId, reqId: id, request: { id, ...request } });
|
|
44
|
+
const until = Date.now() + timeoutMs;
|
|
45
|
+
try {
|
|
46
|
+
while (Date.now() < until) {
|
|
47
|
+
const row = await db.query.deviceCall.findFirst({
|
|
48
|
+
where: eq(schema.deviceCall.reqId, id),
|
|
49
|
+
columns: { answer: true },
|
|
50
|
+
});
|
|
51
|
+
const answer = row?.answer;
|
|
52
|
+
if (answer) {
|
|
53
|
+
if (typeof answer.text === "string") {
|
|
54
|
+
console.log(`[run ${runId}] the device answered a ${answer.text.length}-character call`);
|
|
55
|
+
return answer.text;
|
|
56
|
+
}
|
|
57
|
+
throw new Error(answer.error || "the device could not answer");
|
|
58
|
+
}
|
|
59
|
+
await new Promise((r) => setTimeout(r, Math.min(ANSWER_POLL_MS, until - Date.now())));
|
|
60
|
+
}
|
|
61
|
+
throw new Error("the device did not answer in time");
|
|
64
62
|
}
|
|
65
|
-
|
|
66
|
-
|
|
63
|
+
finally {
|
|
64
|
+
await db
|
|
65
|
+
.delete(schema.deviceCall)
|
|
66
|
+
.where(eq(schema.deviceCall.reqId, id))
|
|
67
|
+
.catch(() => undefined);
|
|
67
68
|
}
|
|
68
|
-
|
|
69
|
+
}
|
|
70
|
+
/** The device's answer to one call; false when the call is unknown, answered or not this run's. */
|
|
71
|
+
export async function answerDevice(runId, reqId, answer) {
|
|
72
|
+
const result = await db
|
|
73
|
+
.update(schema.deviceCall)
|
|
74
|
+
.set({ answer })
|
|
75
|
+
.where(and(eq(schema.deviceCall.reqId, reqId), eq(schema.deviceCall.runId, runId), isNull(schema.deviceCall.answer)));
|
|
76
|
+
return result.rowsAffected > 0;
|
|
77
|
+
}
|
|
78
|
+
/** A run a runner takes starts its step afresh: calls left in the database have no one waiting. */
|
|
79
|
+
export async function clearDeviceCalls(runId) {
|
|
80
|
+
await db.delete(schema.deviceCall).where(eq(schema.deviceCall.runId, runId));
|
|
69
81
|
}
|
|
70
82
|
/**
|
|
71
83
|
* The run's device first, the runtime's own model where the device cannot: a call with tools
|
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
import { AsyncResource } from "node:async_hooks";
|
|
2
2
|
import { EventEmitter } from "node:events";
|
|
3
3
|
import { noteText } from "@engenty-wizards/shared/run";
|
|
4
|
-
import { and, desc, eq, gt } from "drizzle-orm";
|
|
4
|
+
import { and, desc, eq, gt, sql } from "drizzle-orm";
|
|
5
5
|
import { db, schema } from "../db/client.js";
|
|
6
6
|
const bus = new EventEmitter();
|
|
7
7
|
bus.setMaxListeners(0);
|
|
8
|
-
export function signalDevice(runId, request) {
|
|
9
|
-
bus.emit(runId, { runId, event: null, device: request });
|
|
10
|
-
}
|
|
11
8
|
export async function emitEvent(runId, stepId, type, said, asset) {
|
|
12
9
|
const note = typeof said === "string" ? null : said;
|
|
13
10
|
const message = note ? noteText(note, "de") : said;
|
|
@@ -42,6 +39,76 @@ export function subscribe(runId, listener) {
|
|
|
42
39
|
bus.on(runId, bound);
|
|
43
40
|
return () => bus.off(runId, bound);
|
|
44
41
|
}
|
|
42
|
+
/** How often a watcher looks at the row for what another process changed. */
|
|
43
|
+
const WATCH_POLL_MS = 1000;
|
|
44
|
+
/**
|
|
45
|
+
* Tells of every change on a run: at once when this process made it, within a second when the
|
|
46
|
+
* runner (another process) wrote to the row, added an event or put a call to the run's device.
|
|
47
|
+
* Each event row reaches the listener once, whichever process wrote it. Call it in the run's
|
|
48
|
+
* tenant.
|
|
49
|
+
*/
|
|
50
|
+
export function watchRun(runId, listener) {
|
|
51
|
+
/** The newest event the listener has had; null until the first look. */
|
|
52
|
+
let told = null;
|
|
53
|
+
const unsubscribe = subscribe(runId, (signal) => {
|
|
54
|
+
if (signal.event) {
|
|
55
|
+
if (told !== null && signal.event.id <= told) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
told = Math.max(told ?? 0, signal.event.id);
|
|
59
|
+
}
|
|
60
|
+
listener(signal);
|
|
61
|
+
});
|
|
62
|
+
let last = null;
|
|
63
|
+
let busy = false;
|
|
64
|
+
const look = async () => {
|
|
65
|
+
if (busy) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
busy = true;
|
|
69
|
+
try {
|
|
70
|
+
const [row] = await db
|
|
71
|
+
.select({
|
|
72
|
+
updatedAt: schema.run.updatedAt,
|
|
73
|
+
event: sql `(select max(${schema.runEvent.id}) from ${schema.runEvent} where ${schema.runEvent.runId} = ${runId})`,
|
|
74
|
+
call: sql `(select max(${schema.deviceCall.id}) from ${schema.deviceCall} where ${schema.deviceCall.runId} = ${runId})`,
|
|
75
|
+
})
|
|
76
|
+
.from(schema.run)
|
|
77
|
+
.where(eq(schema.run.id, runId));
|
|
78
|
+
const newest = row?.event ?? 0;
|
|
79
|
+
if (told === null) {
|
|
80
|
+
told = newest;
|
|
81
|
+
}
|
|
82
|
+
else if (newest > told) {
|
|
83
|
+
// Events another process wrote, in order; at most a page per look.
|
|
84
|
+
for (const event of await recentEvents(runId, told, 100)) {
|
|
85
|
+
if (event.id > (told ?? 0)) {
|
|
86
|
+
told = event.id;
|
|
87
|
+
listener({ runId, event });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const mark = row ? `${row.updatedAt.getTime()}:${row.call ?? 0}` : null;
|
|
92
|
+
if (last !== null && mark !== last) {
|
|
93
|
+
listener({ runId, event: null });
|
|
94
|
+
}
|
|
95
|
+
last = mark;
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
// The next look tells.
|
|
99
|
+
}
|
|
100
|
+
finally {
|
|
101
|
+
busy = false;
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
const timer = setInterval(() => void look(), WATCH_POLL_MS);
|
|
105
|
+
timer.unref();
|
|
106
|
+
void look();
|
|
107
|
+
return () => {
|
|
108
|
+
clearInterval(timer);
|
|
109
|
+
unsubscribe();
|
|
110
|
+
};
|
|
111
|
+
}
|
|
45
112
|
export async function recentEvents(runId, afterId = 0, limit = 40) {
|
|
46
113
|
const rows = await db
|
|
47
114
|
.select()
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { ownerUrl } from "../tenants/control.js";
|
|
2
|
+
/**
|
|
3
|
+
* Hands a request on to the runner holding the run — what only the process with the run's
|
|
4
|
+
* browser can answer. The runner serves the same routes and checks the same cookie; the caller's
|
|
5
|
+
* headers go along. Null: nobody holds the run, or the runner named no address (RUNNER_URL).
|
|
6
|
+
*/
|
|
7
|
+
export async function fromOwner(c, runId) {
|
|
8
|
+
const base = await ownerUrl(runId);
|
|
9
|
+
if (!base) {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
const here = new URL(c.req.url);
|
|
13
|
+
const headers = new Headers(c.req.raw.headers);
|
|
14
|
+
headers.delete("host");
|
|
15
|
+
headers.delete("content-length");
|
|
16
|
+
const method = c.req.method;
|
|
17
|
+
const body = method === "GET" || method === "HEAD" ? undefined : await c.req.raw.arrayBuffer();
|
|
18
|
+
try {
|
|
19
|
+
const res = await fetch(`${base}${here.pathname}${here.search}`, {
|
|
20
|
+
method,
|
|
21
|
+
headers,
|
|
22
|
+
body,
|
|
23
|
+
redirect: "manual",
|
|
24
|
+
signal: AbortSignal.timeout(15_000),
|
|
25
|
+
});
|
|
26
|
+
return new Response(res.body, { status: res.status, headers: res.headers });
|
|
27
|
+
}
|
|
28
|
+
catch (err) {
|
|
29
|
+
console.error(`[run ${runId}] the runner did not answer:`, err);
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|