@pixerate/editor 0.140.3 → 0.141.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/dist/agent/canvasPlayback.d.cts +15 -0
- package/dist/agent/canvasPlayback.d.ts +15 -0
- package/dist/agent/index.cjs +144 -23
- package/dist/agent/index.d.cts +1 -0
- package/dist/agent/index.d.ts +1 -0
- package/dist/agent/index.js +125 -4
- package/dist/agent/providers.d.cts +8 -0
- package/dist/agent/providers.d.ts +8 -0
- package/dist/agent/types.d.cts +3 -0
- package/dist/agent/types.d.ts +3 -0
- package/dist/animation/index.cjs +2 -2
- package/dist/animation/index.js +1 -1
- package/dist/animation/presence.d.cts +1 -75
- package/dist/animation/presence.d.ts +1 -75
- package/dist/animation-editor/index.cjs +55 -55
- package/dist/animation-editor/index.js +2 -2
- package/dist/bridge/cli.cjs +3 -3
- package/dist/bridge/cli.js +2 -2
- package/dist/bridge/index.cjs +3 -3
- package/dist/bridge/index.js +2 -2
- package/dist/canvas/index.cjs +336 -646
- package/dist/canvas/index.d.cts +1 -0
- package/dist/canvas/index.d.ts +1 -0
- package/dist/canvas/index.js +44 -354
- package/dist/canvas/playback.d.cts +14 -0
- package/dist/canvas/playback.d.ts +14 -0
- package/dist/chart/index.cjs +7 -7
- package/dist/chart/index.js +1 -1
- package/dist/chunk-6SFREX6H.cjs +406 -0
- package/dist/{chunk-XGOU44RJ.js → chunk-77SG47WT.js} +80 -9
- package/dist/{chunk-2SHFIVC3.js → chunk-DAS7M4RY.js} +1 -1
- package/dist/chunk-DW4CV5H4.js +591 -0
- package/dist/{chunk-UMS6YFA6.js → chunk-EAQ4BGNS.js} +1 -1
- package/dist/{chunk-AGCD3EHB.cjs → chunk-ENIZYNEF.cjs} +7 -7
- package/dist/{chunk-VSR5A22L.cjs → chunk-H6FX45CP.cjs} +35 -35
- package/dist/{chunk-RCUX6CT2.cjs → chunk-HRI3ZMDM.cjs} +110 -39
- package/dist/chunk-MFFE55CS.js +406 -0
- package/dist/{chunk-BSGPXPTW.js → chunk-PEEP2YR7.js} +1 -1
- package/dist/{chunk-RB62U6MS.js → chunk-RFBFTLVN.js} +27 -5
- package/dist/chunk-UBRYHNRU.cjs +591 -0
- package/dist/{chunk-GEY73RUW.cjs → chunk-UJGAA6AK.cjs} +1 -1
- package/dist/{chunk-6UZCFD7Y.js → chunk-VM6JYWXQ.js} +1 -1
- package/dist/{chunk-O3ZU6QIZ.cjs → chunk-XNZWATSB.cjs} +81 -81
- package/dist/{chunk-HDK3T2QS.cjs → chunk-Y4BWZB6J.cjs} +29 -7
- package/dist/commands/index.cjs +37 -1
- package/dist/commands/index.js +36 -0
- package/dist/commands/registry.d.cts +11 -0
- package/dist/commands/registry.d.ts +11 -0
- package/dist/diagram/index.cjs +7 -7
- package/dist/diagram/index.js +1 -1
- package/dist/image-editor/index.cjs +720 -628
- package/dist/image-editor/index.d.cts +1 -0
- package/dist/image-editor/index.d.ts +1 -0
- package/dist/image-editor/index.js +109 -17
- package/dist/image-editor/playback.d.cts +22 -0
- package/dist/image-editor/playback.d.ts +22 -0
- package/dist/paint-editor/index.cjs +13 -13
- package/dist/paint-editor/index.js +3 -3
- package/dist/tools/index.cjs +33 -3
- package/dist/tools/index.d.cts +2 -0
- package/dist/tools/index.d.ts +2 -0
- package/dist/tools/index.js +32 -2
- package/dist/tools/playback.d.cts +205 -0
- package/dist/tools/playback.d.ts +205 -0
- package/dist/tools/presence.d.cts +82 -0
- package/dist/tools/presence.d.ts +82 -0
- package/dist/tools/registry.d.cts +45 -6
- package/dist/tools/registry.d.ts +45 -6
- package/dist/tools/view.d.cts +2 -4
- package/dist/tools/view.d.ts +2 -4
- package/dist/vector/index.cjs +1159 -1042
- package/dist/vector/index.d.cts +1 -0
- package/dist/vector/index.d.ts +1 -0
- package/dist/vector/index.js +142 -25
- package/dist/vector/playback.d.cts +13 -0
- package/dist/vector/playback.d.ts +13 -0
- package/package.json +1 -1
- package/skills/pixerate-agent-tools/SKILL.md +13 -0
- package/skills/pixerate-canvas/SKILL.md +3 -0
- package/skills/pixerate-image-editor/SKILL.md +2 -2
- package/skills/pixerate-vector/SKILL.md +5 -0
- package/dist/chunk-BTZS3KZY.js +0 -221
- package/dist/chunk-MRD3NXKR.cjs +0 -221
- /package/dist/{chunk-K73HMKQP.js → chunk-PLKIYNJZ.js} +0 -0
- /package/dist/{chunk-LACYURUW.cjs → chunk-TNCWKC5M.cjs} +0 -0
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PRESENCE_COLORS,
|
|
3
|
+
realPresenceClock
|
|
4
|
+
} from "./chunk-PEEP2YR7.js";
|
|
5
|
+
|
|
6
|
+
// src/tools/playback.ts
|
|
7
|
+
var ARG_KINDS = {
|
|
8
|
+
nodeId: "node",
|
|
9
|
+
edgeId: "edge",
|
|
10
|
+
layerId: "layer",
|
|
11
|
+
clipId: "clip",
|
|
12
|
+
trackId: "track",
|
|
13
|
+
seriesId: "series",
|
|
14
|
+
parameterId: "parameter",
|
|
15
|
+
keyframeId: "keyframe",
|
|
16
|
+
shapeId: "shape"
|
|
17
|
+
};
|
|
18
|
+
function pushRef(out, kind, id) {
|
|
19
|
+
if (typeof id !== "string" || !id) return;
|
|
20
|
+
if (!out.some((r) => r.kind === kind && r.id === id)) out.push({ kind, id });
|
|
21
|
+
}
|
|
22
|
+
function playbackRefsFromArgs(args, defaultKind = "item") {
|
|
23
|
+
const out = [];
|
|
24
|
+
const visit = (value, depth) => {
|
|
25
|
+
if (!value || typeof value !== "object" || Array.isArray(value) || depth > 1) return;
|
|
26
|
+
const record = value;
|
|
27
|
+
pushRef(out, defaultKind, record.id);
|
|
28
|
+
if (Array.isArray(record.ids)) for (const id of record.ids) pushRef(out, defaultKind, id);
|
|
29
|
+
for (const [key, kind] of Object.entries(ARG_KINDS)) {
|
|
30
|
+
pushRef(out, kind, record[key]);
|
|
31
|
+
const many = record[`${key}s`];
|
|
32
|
+
if (Array.isArray(many)) for (const id of many) pushRef(out, kind, id);
|
|
33
|
+
}
|
|
34
|
+
pushRef(out, "cell", record.range);
|
|
35
|
+
pushRef(out, "cell", record.cell);
|
|
36
|
+
visit(record.params, depth + 1);
|
|
37
|
+
};
|
|
38
|
+
visit(args, 0);
|
|
39
|
+
return out;
|
|
40
|
+
}
|
|
41
|
+
function playbackRefsFromResult(value, defaultKind = "item") {
|
|
42
|
+
const out = [];
|
|
43
|
+
const visit = (v, depth) => {
|
|
44
|
+
if (!v || typeof v !== "object" || depth > 2) return;
|
|
45
|
+
if (Array.isArray(v)) {
|
|
46
|
+
for (const item of v) if (item && typeof item === "object") pushRef(out, defaultKind, item.id);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const record = v;
|
|
50
|
+
if (Array.isArray(record.content) && record.structuredContent) return visit(record.structuredContent, depth);
|
|
51
|
+
pushRef(out, defaultKind, record.id);
|
|
52
|
+
if (Array.isArray(record.ids)) for (const id of record.ids) pushRef(out, defaultKind, id);
|
|
53
|
+
if (record.node && typeof record.node === "object") pushRef(out, "node", record.node.id);
|
|
54
|
+
pushRef(out, "layer", record.layerId);
|
|
55
|
+
if (record.layer && typeof record.layer === "object") pushRef(out, "layer", record.layer.id);
|
|
56
|
+
const created = record.created;
|
|
57
|
+
if (Array.isArray(created)) visit(created, depth + 1);
|
|
58
|
+
else if (created && typeof created === "object") {
|
|
59
|
+
const c = created;
|
|
60
|
+
for (const n of c.nodes ?? []) pushRef(out, "node", n?.id);
|
|
61
|
+
for (const e of c.edges ?? []) pushRef(out, "edge", e?.id);
|
|
62
|
+
}
|
|
63
|
+
if (record.result && typeof record.result === "object") visit(record.result, depth + 1);
|
|
64
|
+
};
|
|
65
|
+
visit(value, 0);
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
function derivePlaybackSteps(args, defaultKind) {
|
|
69
|
+
const refs = playbackRefsFromArgs(args, defaultKind).slice(0, 8);
|
|
70
|
+
if (!refs.length) return [{ type: "commit" }];
|
|
71
|
+
return [...refs.map((ref) => ({ type: "target", ref })), { type: "press" }, { type: "release" }, { type: "commit" }];
|
|
72
|
+
}
|
|
73
|
+
function readingTime(text) {
|
|
74
|
+
return Math.min(2200, Math.max(600, text.length * 65));
|
|
75
|
+
}
|
|
76
|
+
function unionRects(rects) {
|
|
77
|
+
const list = rects.filter((r) => !!r);
|
|
78
|
+
if (!list.length) return null;
|
|
79
|
+
const x = Math.min(...list.map((r) => r.x));
|
|
80
|
+
const y = Math.min(...list.map((r) => r.y));
|
|
81
|
+
return { x, y, width: Math.max(...list.map((r) => r.x + r.width)) - x, height: Math.max(...list.map((r) => r.y + r.height)) - y };
|
|
82
|
+
}
|
|
83
|
+
function normalizePlan(plan) {
|
|
84
|
+
if (!plan) return null;
|
|
85
|
+
if (Array.isArray(plan)) return plan.length ? { steps: plan } : null;
|
|
86
|
+
return plan.steps?.length || plan.surface || plan.readOnly !== void 0 ? plan : null;
|
|
87
|
+
}
|
|
88
|
+
var isErrorResult = (value) => !!value && typeof value === "object" && value.isError === true;
|
|
89
|
+
var center = (r) => ({ x: r.x + r.width / 2, y: r.y + r.height / 2 });
|
|
90
|
+
var GLANCE_STEPS = /* @__PURE__ */ new Set(["move", "target", "focus", "say", "wait", "commit"]);
|
|
91
|
+
var AgentPlayback = class {
|
|
92
|
+
/** 1 = natural pace; `Infinity` plays nothing (calls run instantly). Takes effect on the next step. */
|
|
93
|
+
speed;
|
|
94
|
+
reads;
|
|
95
|
+
clock;
|
|
96
|
+
linger;
|
|
97
|
+
defaultActor;
|
|
98
|
+
surfaces = /* @__PURE__ */ new Map();
|
|
99
|
+
actors = /* @__PURE__ */ new Map();
|
|
100
|
+
recordings = /* @__PURE__ */ new Set();
|
|
101
|
+
installed = /* @__PURE__ */ new WeakSet();
|
|
102
|
+
listeners = /* @__PURE__ */ new Set();
|
|
103
|
+
generation = 0;
|
|
104
|
+
running = 0;
|
|
105
|
+
calls = 0;
|
|
106
|
+
constructor(options = {}) {
|
|
107
|
+
this.clock = options.clock ?? realPresenceClock;
|
|
108
|
+
this.speed = options.speed ?? 1;
|
|
109
|
+
this.reads = options.reads ?? "glance";
|
|
110
|
+
this.linger = options.linger ?? 1500;
|
|
111
|
+
this.defaultActor = options.actor ?? { id: "agent", name: "Agent" };
|
|
112
|
+
}
|
|
113
|
+
/** The registry middleware that plays each call. */
|
|
114
|
+
middleware = (call, next) => this.perform(call, next);
|
|
115
|
+
/** Adds the middleware to `registry` once (later calls with the same registry do nothing). */
|
|
116
|
+
install(registry) {
|
|
117
|
+
if (this.installed.has(registry)) return;
|
|
118
|
+
this.installed.add(registry);
|
|
119
|
+
registry.use(this.middleware);
|
|
120
|
+
}
|
|
121
|
+
/** Whether a performance is playing now (e.g. to make the editor read-only meanwhile). */
|
|
122
|
+
get active() {
|
|
123
|
+
return this.running > 0;
|
|
124
|
+
}
|
|
125
|
+
/** Called when `active` changes. Returns an unsubscribe function. */
|
|
126
|
+
subscribe(listener) {
|
|
127
|
+
this.listeners.add(listener);
|
|
128
|
+
return () => {
|
|
129
|
+
this.listeners.delete(listener);
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/** Attaches a surface under a provider id (or the id providers name as `playbackSurface`). */
|
|
133
|
+
attach(id, surface) {
|
|
134
|
+
this.surfaces.set(id, surface);
|
|
135
|
+
return () => {
|
|
136
|
+
if (this.surfaces.get(id) !== surface) return;
|
|
137
|
+
this.surfaces.delete(id);
|
|
138
|
+
for (const actor of this.actors.values()) if (actor.surface === id) actor.surface = void 0;
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
/** The surface attached under `id`. */
|
|
142
|
+
surface(id) {
|
|
143
|
+
return this.surfaces.get(id);
|
|
144
|
+
}
|
|
145
|
+
/** Shows `text` in the actor's bubble on the surface it last worked on (until its next step). */
|
|
146
|
+
say(text, options = {}) {
|
|
147
|
+
const actor = this.actorState(options.actor);
|
|
148
|
+
const surface = actor.surface ? this.surfaces.get(actor.surface) : void 0;
|
|
149
|
+
if (surface?.presence.get(actor.info.id)) surface.presence.set({ id: actor.info.id, message: text });
|
|
150
|
+
}
|
|
151
|
+
/** Finishes every running performance at once (the calls still run and keep their results). */
|
|
152
|
+
stop() {
|
|
153
|
+
this.generation++;
|
|
154
|
+
}
|
|
155
|
+
/** Collects every step played from now on (for tests). */
|
|
156
|
+
record() {
|
|
157
|
+
const list = [];
|
|
158
|
+
this.recordings.add(list);
|
|
159
|
+
return list;
|
|
160
|
+
}
|
|
161
|
+
actorState(info) {
|
|
162
|
+
const base = info ?? this.defaultActor;
|
|
163
|
+
let actor = this.actors.get(base.id);
|
|
164
|
+
if (!actor) {
|
|
165
|
+
actor = { info: { ...base, color: base.color ?? PRESENCE_COLORS[this.actors.size % PRESENCE_COLORS.length] }, queue: Promise.resolve(), version: 0 };
|
|
166
|
+
this.actors.set(base.id, actor);
|
|
167
|
+
} else if (info?.name && info.name !== actor.info.name) actor.info = { ...actor.info, name: info.name };
|
|
168
|
+
return actor;
|
|
169
|
+
}
|
|
170
|
+
enqueue(actor, run) {
|
|
171
|
+
const result = actor.queue.then(run);
|
|
172
|
+
actor.queue = result.catch(() => void 0);
|
|
173
|
+
return result;
|
|
174
|
+
}
|
|
175
|
+
surfaceFor(provider, hint) {
|
|
176
|
+
for (const id of [hint, provider.playbackSurface, provider.id, provider.namespace]) {
|
|
177
|
+
const surface = id ? this.surfaces.get(id) : void 0;
|
|
178
|
+
if (id && surface) return [id, surface];
|
|
179
|
+
}
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
async perform(call, next) {
|
|
183
|
+
const tool = call.tool;
|
|
184
|
+
if (tool.playback === false || !Number.isFinite(this.speed)) return next();
|
|
185
|
+
const actor = this.actorState(call.context.actor);
|
|
186
|
+
if (tool.playback === "self") return this.enqueue(actor, next);
|
|
187
|
+
const fallback = this.surfaceFor(call.provider);
|
|
188
|
+
let planned = null;
|
|
189
|
+
if (tool.plan) {
|
|
190
|
+
try {
|
|
191
|
+
const state = {
|
|
192
|
+
surfaceId: fallback?.[0] ?? null,
|
|
193
|
+
locate: (ref) => fallback?.[1].locate?.(ref) ?? null
|
|
194
|
+
};
|
|
195
|
+
planned = normalizePlan(tool.plan(call.args, state));
|
|
196
|
+
} catch {
|
|
197
|
+
planned = null;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
const readOnly = planned?.readOnly ?? tool.annotations?.readOnlyHint === true;
|
|
201
|
+
if (readOnly && this.reads === "skip") return next();
|
|
202
|
+
const found = this.surfaceFor(call.provider, planned?.surface);
|
|
203
|
+
if (!found) return next();
|
|
204
|
+
const [surfaceId, surface] = found;
|
|
205
|
+
const steps = planned?.steps?.length ? planned.steps : derivePlaybackSteps(call.args, surface.defaultKind);
|
|
206
|
+
const glance = readOnly && this.reads === "glance";
|
|
207
|
+
const played = glance ? steps.filter((s) => GLANCE_STEPS.has(s.type)) : steps;
|
|
208
|
+
if (!played.some((s) => s.type !== "commit")) return next();
|
|
209
|
+
return this.enqueue(actor, async () => {
|
|
210
|
+
if (surface.visible && !surface.visible()) {
|
|
211
|
+
await surface.reveal?.();
|
|
212
|
+
if (surface.visible && !surface.visible()) return next();
|
|
213
|
+
}
|
|
214
|
+
return this.play({ call, next, actor, surfaceId, surface, steps: played, readOnly, glance });
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
async play(run) {
|
|
218
|
+
const { call, next, actor, surfaceId, surface, readOnly } = run;
|
|
219
|
+
const id = actor.info.id;
|
|
220
|
+
const gen = this.generation;
|
|
221
|
+
const instant = () => gen !== this.generation || !Number.isFinite(this.speed);
|
|
222
|
+
const pace = () => Math.max(0.1, this.speed) * (run.glance ? 1.6 : 1);
|
|
223
|
+
const wait = (ms) => instant() || ms <= 0 ? Promise.resolve() : this.clock.wait(ms / pace());
|
|
224
|
+
const presence = surface.presence;
|
|
225
|
+
const locate = (ref) => surface.locate?.(ref.kind === "item" ? { kind: surface.defaultKind ?? "item", id: ref.id } : ref) ?? null;
|
|
226
|
+
const record = (step) => {
|
|
227
|
+
for (const list of this.recordings) list.push({ actor: id, surface: surfaceId, tool: call.name, step });
|
|
228
|
+
};
|
|
229
|
+
this.running++;
|
|
230
|
+
if (this.running === 1) this.listeners.forEach((l) => l());
|
|
231
|
+
try {
|
|
232
|
+
if (actor.surface && actor.surface !== surfaceId) this.surfaces.get(actor.surface)?.presence.remove(id);
|
|
233
|
+
if (!presence.get(id)) {
|
|
234
|
+
const bounds = surface.bounds?.() ?? null;
|
|
235
|
+
let at = actor.viewport && surface.fromViewport ? surface.fromViewport(actor.viewport) : null;
|
|
236
|
+
if (at && bounds) at = { x: Math.min(bounds.x + bounds.width, Math.max(bounds.x, at.x)), y: Math.min(bounds.y + bounds.height, Math.max(bounds.y, at.y)) };
|
|
237
|
+
at ??= bounds ? { x: bounds.x + bounds.width - 48, y: bounds.y + 32 } : null;
|
|
238
|
+
if (!at) {
|
|
239
|
+
const first = run.steps.find((s) => s.type === "move" || s.type === "target" || s.type === "drag");
|
|
240
|
+
const rect = first?.type === "target" ? locate(first.ref) : null;
|
|
241
|
+
at = first && first.type !== "target" ? first.to : rect ? center(rect) : { x: 0, y: 0 };
|
|
242
|
+
}
|
|
243
|
+
presence.set({ id, name: actor.info.name ?? id, color: actor.info.color, x: at.x, y: at.y, pressed: false });
|
|
244
|
+
} else presence.set({ id, name: actor.info.name ?? id, color: actor.info.color });
|
|
245
|
+
actor.surface = surfaceId;
|
|
246
|
+
actor.version++;
|
|
247
|
+
const argRefs = playbackRefsFromArgs(call.args, surface.defaultKind);
|
|
248
|
+
if (!readOnly) surface.beginGesture?.(`agent:${id}:${++this.calls}`);
|
|
249
|
+
let result;
|
|
250
|
+
let committed = false;
|
|
251
|
+
try {
|
|
252
|
+
for (const step of run.steps) {
|
|
253
|
+
record(step);
|
|
254
|
+
if (step.type === "commit") {
|
|
255
|
+
if (committed) continue;
|
|
256
|
+
committed = true;
|
|
257
|
+
result = await next();
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
if (step.type === "open") {
|
|
261
|
+
if (!surface.open) continue;
|
|
262
|
+
const rect = locate(step.ref);
|
|
263
|
+
if (rect) {
|
|
264
|
+
await this.glide(presence, id, { x: rect.x + rect.width / 2, y: rect.y + rect.height / 2 }, step.ms, instant);
|
|
265
|
+
presence.set({ id, pressed: true });
|
|
266
|
+
await wait(90);
|
|
267
|
+
}
|
|
268
|
+
await surface.open(step.ref);
|
|
269
|
+
presence.set({ id, pressed: false });
|
|
270
|
+
await wait(220);
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
await this.step(presence, locate, id, step, wait, instant);
|
|
274
|
+
}
|
|
275
|
+
if (!committed) {
|
|
276
|
+
record({ type: "commit" });
|
|
277
|
+
result = await next();
|
|
278
|
+
}
|
|
279
|
+
} catch (error) {
|
|
280
|
+
presence.set({ id, pressed: false, message: `\u2717 ${error instanceof Error ? error.message : String(error)}`.slice(0, 140) });
|
|
281
|
+
await wait(900);
|
|
282
|
+
throw error;
|
|
283
|
+
} finally {
|
|
284
|
+
if (!readOnly) surface.endGesture?.();
|
|
285
|
+
presence.set({ id, pressed: false });
|
|
286
|
+
}
|
|
287
|
+
if (isErrorResult(result)) {
|
|
288
|
+
const text = result.content?.find((c) => c.type === "text")?.text ?? "The tool failed.";
|
|
289
|
+
presence.set({ id, message: `\u2717 ${text}`.slice(0, 140) });
|
|
290
|
+
await wait(900);
|
|
291
|
+
} else if (!readOnly) {
|
|
292
|
+
const known = new Set(argRefs.map((r) => `${r.kind}:${r.id}`));
|
|
293
|
+
const fresh = playbackRefsFromResult(result, surface.defaultKind).filter((r) => !known.has(`${r.kind}:${r.id}`));
|
|
294
|
+
const rect = unionRects(fresh.slice(0, 16).map(locate));
|
|
295
|
+
if (rect) {
|
|
296
|
+
const step = { type: "focus", rect };
|
|
297
|
+
record(step);
|
|
298
|
+
presence.set({ id, focus: rect });
|
|
299
|
+
await wait(450);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
presence.set({ id, message: void 0, focus: void 0 });
|
|
303
|
+
const here = presence.get(id);
|
|
304
|
+
if (here && surface.toViewport) actor.viewport = surface.toViewport({ x: here.x, y: here.y });
|
|
305
|
+
this.scheduleLinger(actor, surface);
|
|
306
|
+
return result;
|
|
307
|
+
} finally {
|
|
308
|
+
this.running--;
|
|
309
|
+
if (this.running === 0) this.listeners.forEach((l) => l());
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
scheduleLinger(actor, surface) {
|
|
313
|
+
if (this.linger <= 0) return;
|
|
314
|
+
const version = actor.version;
|
|
315
|
+
void this.clock.wait(this.linger).then(() => {
|
|
316
|
+
if (actor.version !== version) return;
|
|
317
|
+
surface.presence.remove(actor.info.id);
|
|
318
|
+
for (const [sid, s] of this.surfaces) if (s === surface && actor.surface === sid) actor.surface = void 0;
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
async step(presence, locate, id, step, wait, instant) {
|
|
322
|
+
switch (step.type) {
|
|
323
|
+
case "move":
|
|
324
|
+
return this.glide(presence, id, step.to, step.ms, instant);
|
|
325
|
+
case "drag":
|
|
326
|
+
presence.set({ id, pressed: true });
|
|
327
|
+
return this.glide(presence, id, step.to, step.ms, instant);
|
|
328
|
+
case "target": {
|
|
329
|
+
const rect = locate(step.ref);
|
|
330
|
+
if (!rect) return;
|
|
331
|
+
await this.glide(presence, id, center(rect), step.ms, instant);
|
|
332
|
+
presence.set({ id, focus: rect });
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
case "focus":
|
|
336
|
+
presence.set({ id, focus: step.rect ?? void 0 });
|
|
337
|
+
return;
|
|
338
|
+
case "say":
|
|
339
|
+
presence.set({ id, message: step.text });
|
|
340
|
+
return wait(step.ms ?? readingTime(step.text));
|
|
341
|
+
case "press":
|
|
342
|
+
presence.set({ id, pressed: true });
|
|
343
|
+
return wait(90);
|
|
344
|
+
case "release":
|
|
345
|
+
presence.set({ id, pressed: false });
|
|
346
|
+
return wait(60);
|
|
347
|
+
case "type": {
|
|
348
|
+
const total = step.ms ?? Math.min(900, 40 * step.text.length + 150);
|
|
349
|
+
if (instant() || total <= 0) {
|
|
350
|
+
presence.set({ id, message: step.text });
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
const start = this.clock.now();
|
|
354
|
+
for (; ; ) {
|
|
355
|
+
const t = Math.min(1, (this.clock.now() - start) / (total / Math.max(0.1, this.speed)));
|
|
356
|
+
presence.set({ id, message: step.text.slice(0, Math.max(1, Math.round(step.text.length * t))) });
|
|
357
|
+
if (t >= 1 || instant()) break;
|
|
358
|
+
await this.clock.wait(16);
|
|
359
|
+
}
|
|
360
|
+
presence.set({ id, message: step.text });
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
case "wait":
|
|
364
|
+
return wait(step.ms);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
/** Glides along a slight arc with ease-out, by elapsed time (fewer frames in a throttled tab, never longer). */
|
|
368
|
+
async glide(presence, id, to, ms, instant) {
|
|
369
|
+
const a = presence.get(id);
|
|
370
|
+
const from = a ? { x: a.x, y: a.y } : to;
|
|
371
|
+
const dist = Math.hypot(to.x - from.x, to.y - from.y);
|
|
372
|
+
const duration = (ms ?? Math.min(900, 260 + dist * 0.5)) / Math.max(0.1, this.speed);
|
|
373
|
+
if (instant() || dist < 1 || duration <= 0) {
|
|
374
|
+
presence.set({ id, x: to.x, y: to.y });
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
const bend = Math.min(60, dist * 0.12);
|
|
378
|
+
const ctrl = { x: (from.x + to.x) / 2 - (to.y - from.y) / dist * bend, y: (from.y + to.y) / 2 + (to.x - from.x) / dist * bend };
|
|
379
|
+
const start = this.clock.now();
|
|
380
|
+
for (; ; ) {
|
|
381
|
+
const t = Math.min(1, (this.clock.now() - start) / duration);
|
|
382
|
+
const e = 1 - Math.pow(1 - t, 3);
|
|
383
|
+
const u = 1 - e;
|
|
384
|
+
presence.set({ id, x: u * u * from.x + 2 * u * e * ctrl.x + e * e * to.x, y: u * u * from.y + 2 * u * e * ctrl.y + e * e * to.y });
|
|
385
|
+
if (t >= 1) break;
|
|
386
|
+
if (instant()) {
|
|
387
|
+
presence.set({ id, x: to.x, y: to.y });
|
|
388
|
+
break;
|
|
389
|
+
}
|
|
390
|
+
await this.clock.wait(16);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
function createAgentPlayback(options = {}) {
|
|
395
|
+
return new AgentPlayback(options);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
export {
|
|
399
|
+
playbackRefsFromArgs,
|
|
400
|
+
playbackRefsFromResult,
|
|
401
|
+
derivePlaybackSteps,
|
|
402
|
+
readingTime,
|
|
403
|
+
unionRects,
|
|
404
|
+
AgentPlayback,
|
|
405
|
+
createAgentPlayback
|
|
406
|
+
};
|
|
@@ -413,7 +413,7 @@ function readTools(provider) {
|
|
|
413
413
|
return typeof provider.tools === "function" ? provider.tools() : provider.tools;
|
|
414
414
|
}
|
|
415
415
|
function toDefinition2(name, tool) {
|
|
416
|
-
const { handler: _handler, name: _name, ...rest } = tool;
|
|
416
|
+
const { handler: _handler, name: _name, plan: _plan, playback: _playback, ...rest } = tool;
|
|
417
417
|
return { name, ...rest };
|
|
418
418
|
}
|
|
419
419
|
function errorResult(error) {
|
|
@@ -442,6 +442,7 @@ var ToolRegistry = class {
|
|
|
442
442
|
capabilities = { tools: { listChanged: true } };
|
|
443
443
|
entries = [];
|
|
444
444
|
listeners = /* @__PURE__ */ new Set();
|
|
445
|
+
middleware = [];
|
|
445
446
|
options;
|
|
446
447
|
timer = null;
|
|
447
448
|
lastSignature;
|
|
@@ -502,6 +503,17 @@ var ToolRegistry = class {
|
|
|
502
503
|
this.notifyListChanged();
|
|
503
504
|
return true;
|
|
504
505
|
}
|
|
506
|
+
/**
|
|
507
|
+
* Adds a middleware that wraps every tool call (first added runs outermost), e.g. agent
|
|
508
|
+
* playback's `playback.middleware`. Returns a function that removes it.
|
|
509
|
+
*/
|
|
510
|
+
use(middleware) {
|
|
511
|
+
this.middleware.push(middleware);
|
|
512
|
+
return () => {
|
|
513
|
+
const index = this.middleware.indexOf(middleware);
|
|
514
|
+
if (index !== -1) this.middleware.splice(index, 1);
|
|
515
|
+
};
|
|
516
|
+
}
|
|
505
517
|
/** Registered provider ids in registration order. */
|
|
506
518
|
providerIds() {
|
|
507
519
|
return this.entries.map((e) => e.provider.id);
|
|
@@ -609,11 +621,16 @@ var ToolRegistry = class {
|
|
|
609
621
|
}
|
|
610
622
|
}
|
|
611
623
|
try {
|
|
612
|
-
const
|
|
624
|
+
const context = {
|
|
613
625
|
name,
|
|
614
626
|
providerId: found.provider.id,
|
|
615
|
-
signal: options.signal
|
|
616
|
-
|
|
627
|
+
signal: options.signal,
|
|
628
|
+
...options.actor ? { actor: options.actor } : {}
|
|
629
|
+
};
|
|
630
|
+
const call = { name, tool: found.tool, provider: found.provider, args: input, context };
|
|
631
|
+
const chain = [...this.middleware];
|
|
632
|
+
const run = async (i) => i < chain.length ? chain[i](call, () => run(i + 1)) : found.tool.handler(call.args, context);
|
|
633
|
+
const value = await run(0);
|
|
617
634
|
return toToolResult(value);
|
|
618
635
|
} catch (error) {
|
|
619
636
|
return errorResult({
|
|
@@ -896,6 +913,7 @@ function createMcpServer(registry, options) {
|
|
|
896
913
|
};
|
|
897
914
|
const readSkillPath = (path) => skillFiles?.[path] ?? skillFiles?.[`./${path}`];
|
|
898
915
|
let initialized = false;
|
|
916
|
+
let actor;
|
|
899
917
|
let unsubscribe = null;
|
|
900
918
|
const running = /* @__PURE__ */ new Map();
|
|
901
919
|
const startNotifications = () => {
|
|
@@ -909,6 +927,10 @@ function createMcpServer(registry, options) {
|
|
|
909
927
|
const params = request.params ?? {};
|
|
910
928
|
switch (request.method) {
|
|
911
929
|
case "initialize": {
|
|
930
|
+
const client = params.clientInfo;
|
|
931
|
+
if (client && typeof client.name === "string" && client.name) {
|
|
932
|
+
actor = { id: `mcp:${client.name}`, name: typeof client.title === "string" && client.title ? client.title : client.name };
|
|
933
|
+
}
|
|
912
934
|
const instructions = typeof options.instructions === "function" ? options.instructions(registry) : options.instructions;
|
|
913
935
|
const requested = String(params.protocolVersion ?? "");
|
|
914
936
|
const protocolVersion = MCP_PROTOCOL_VERSIONS.includes(requested) ? requested : MCP_PROTOCOL_VERSIONS[0];
|
|
@@ -951,7 +973,7 @@ function createMcpServer(registry, options) {
|
|
|
951
973
|
const controller = new AbortController();
|
|
952
974
|
if (!isNotification) running.set(id, controller);
|
|
953
975
|
try {
|
|
954
|
-
const result = await registry.callTool(name, args ?? {}, { signal: controller.signal });
|
|
976
|
+
const result = await registry.callTool(name, args ?? {}, { signal: controller.signal, ...actor ? { actor } : {} });
|
|
955
977
|
if (controller.signal.aborted) return null;
|
|
956
978
|
return { jsonrpc: "2.0", id, result };
|
|
957
979
|
} finally {
|