tencil 0.9.0 → 0.10.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/react.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { t as TencilCloudOptions } from "./cloud-dXtv3DCr.mjs";
1
+ import { t as TencilCloudOptions } from "./cloud-DKtU6lcZ.mjs";
2
2
  //#region src/integrations/react.d.ts
3
3
  /**
4
4
  * Loads the Tencil overlay in development from the daemon on the machine
@@ -112,6 +112,42 @@ const BuildInfo = z.object({
112
112
  branch: z.string().min(1).max(255),
113
113
  sha: z.string().min(7).max(64)
114
114
  });
115
+ /** A key combination, matched by key position (`code`) so it works on any layout. */
116
+ const Shortcut = z.object({
117
+ code: z.string().min(1).max(32),
118
+ /** What the layout prints for it, for labels. */
119
+ key: z.string().min(1).max(32),
120
+ alt: z.boolean(),
121
+ ctrl: z.boolean(),
122
+ meta: z.boolean(),
123
+ shift: z.boolean()
124
+ });
125
+ const Shortcuts = z.object({
126
+ comment: Shortcut.nullable(),
127
+ ruler: Shortcut.nullable(),
128
+ hidePins: Shortcut.nullable(),
129
+ /** Never cleared: it's the only way back to a hidden dock. */
130
+ hideDock: Shortcut,
131
+ /** Held, not pressed. */
132
+ measure: z.enum([
133
+ "Alt",
134
+ "Control",
135
+ "Meta",
136
+ "Shift"
137
+ ]).nullable()
138
+ });
139
+ /**
140
+ * The person's own overlay settings, the same in every project on the machine.
141
+ * Saved by the local daemon; unset ones fall back to the overlay's defaults.
142
+ */
143
+ const Preferences = z.object({
144
+ theme: z.enum([
145
+ "dark",
146
+ "light",
147
+ "system"
148
+ ]).optional(),
149
+ shortcuts: Shortcuts.optional()
150
+ });
115
151
  const ClientMessage = z.discriminatedUnion("type", [
116
152
  z.object({
117
153
  type: z.literal("hello"),
@@ -158,6 +194,7 @@ const ClientMessage = z.discriminatedUnion("type", [
158
194
  }),
159
195
  z.object({ type: z.literal("provider.threads") }),
160
196
  z.object({ type: z.literal("provider.models") }),
197
+ z.object({ type: z.literal("project.choices") }),
161
198
  z.object({
162
199
  type: z.literal("project.select"),
163
200
  root: z.string().min(1)
@@ -197,7 +234,11 @@ const ClientMessage = z.discriminatedUnion("type", [
197
234
  type: z.literal("model.select"),
198
235
  model: z.string().min(1).nullable()
199
236
  }),
200
- z.object({ type: z.literal("update.start") })
237
+ z.object({ type: z.literal("update.start") }),
238
+ z.object({
239
+ type: z.literal("preferences.update"),
240
+ preferences: Preferences
241
+ })
201
242
  ]);
202
243
  z.object({
203
244
  id: z.string(),
@@ -230,6 +271,27 @@ z.object({
230
271
  /** The toolbar offers the ruler: spacing while picking, distances with Alt. */
231
272
  ruler: z.boolean()
232
273
  });
274
+ /**
275
+ * How the daemon matched the page to its project: an Ensemble thread's dev
276
+ * server, the git root the Vite or Next plugin passed, the dev server listening
277
+ * on the page's port, the Docker container publishing it, or a pick in the overlay.
278
+ */
279
+ const DetectedVia = z.enum([
280
+ "ensemble",
281
+ "plugin",
282
+ "port",
283
+ "docker"
284
+ ]);
285
+ z.object({
286
+ via: z.enum([...DetectedVia.options, "picked"]),
287
+ port: z.number().int().optional(),
288
+ /** Picked over a project Tencil did detect: that one, to go back to. */
289
+ detected: z.object({
290
+ root: z.string(),
291
+ via: DetectedVia,
292
+ port: z.number().int().optional()
293
+ }).optional()
294
+ });
233
295
  //#endregion
234
296
  //#region ../protocol/src/thread.ts
235
297
  /**
@@ -657,6 +719,8 @@ var Bridge = class {
657
719
  return;
658
720
  }
659
721
  case "update.start": throw new Error("Tencil can't update itself here");
722
+ case "preferences.update": throw new Error("Preferences aren't saved here");
723
+ case "project.choices": throw new Error("Projects can't be switched here");
660
724
  }
661
725
  } catch (error) {
662
726
  const threadId = "threadId" in message ? message.threadId : void 0;
@@ -2028,38 +2092,90 @@ const defaultExec = async (command, args) => (await run$1(command, args, {
2028
2092
  timeout: 3e3,
2029
2093
  windowsHide: true
2030
2094
  })).stdout;
2031
- /**
2032
- * Works out which project folder a page belongs to:
2033
- * 1. the Ensemble thread whose dev server listens on the port,
2034
- * 2. the repository root the Vite or Next loader sent, if it's a git checkout,
2035
- * 3. the dev server's working directory, found from the port it listens on,
2036
- * 4. what the user picked for this address before (a folder or an Ensemble thread).
2037
- * Picks come last: ports get reused (5173 is every Vite app's default), so a pick
2038
- * only stands in for what can't be detected. Returns undefined when nothing works;
2039
- * the overlay then asks.
2040
- */
2095
+ function pickKey(origin, detected) {
2096
+ return `${origin} ${detected}`;
2097
+ }
2041
2098
  var ProjectResolver = class {
2042
2099
  #memory;
2100
+ /** Picks made over a detected project, by address and that project. */
2101
+ #picks;
2043
2102
  #exec;
2044
2103
  #platform;
2045
2104
  #ensembleForPort;
2046
2105
  constructor(options) {
2047
2106
  this.#memory = new OriginMemory(join(options.dataDir, "origins.json"));
2107
+ this.#picks = new OriginMemory(join(options.dataDir, "project-picks.json"));
2048
2108
  this.#exec = options.exec ?? defaultExec;
2049
2109
  this.#platform = options.platform ?? process.platform;
2050
2110
  this.#ensembleForPort = options.ensembleForPort;
2051
2111
  }
2052
2112
  async resolve(pageUrl, claimedRoot) {
2113
+ return (await this.detect(pageUrl, claimedRoot))?.root;
2114
+ }
2115
+ /**
2116
+ * The page's project and how it was found. A pick made over a detected
2117
+ * project only holds while that same project is detected on the address:
2118
+ * when another app takes the port, detection wins again.
2119
+ */
2120
+ async detect(pageUrl, claimedRoot) {
2053
2121
  const origin = safeOrigin(pageUrl);
2054
2122
  if (!origin) return void 0;
2123
+ const found = await this.#automatic(origin, claimedRoot);
2124
+ if (found) {
2125
+ const picked = await this.#picks.get(pickKey(origin, found.root));
2126
+ if (!(picked && (ensembleThreadId(picked) || existsSync(picked))) || picked === found.root) return found;
2127
+ return {
2128
+ root: picked,
2129
+ source: {
2130
+ via: "picked",
2131
+ detected: {
2132
+ root: found.root,
2133
+ ...found.source
2134
+ }
2135
+ }
2136
+ };
2137
+ }
2138
+ const remembered = await this.#memory.get(origin);
2139
+ return remembered && (ensembleThreadId(remembered) || existsSync(remembered)) ? {
2140
+ root: remembered,
2141
+ source: { via: "picked" }
2142
+ } : void 0;
2143
+ }
2144
+ /** Detection alone: Ensemble thread, the plugin's git root, then the port. */
2145
+ async #automatic(origin, claimedRoot) {
2055
2146
  const port = portOf(origin);
2056
2147
  const thread = port ? await this.#ensembleForPort?.(port).catch(() => void 0) : void 0;
2057
- if (thread) return thread;
2058
- if (claimedRoot && isGitRoot(claimedRoot)) return resolve(claimedRoot);
2059
- const folder = port ? await this.folderForPort(port).catch(() => void 0) : void 0;
2060
- if (folder) return projectRoot(folder);
2061
- const remembered = await this.#memory.get(origin);
2062
- return remembered && (ensembleThreadId(remembered) || existsSync(remembered)) ? remembered : void 0;
2148
+ if (thread) return {
2149
+ root: thread,
2150
+ source: {
2151
+ via: "ensemble",
2152
+ port
2153
+ }
2154
+ };
2155
+ if (claimedRoot && isGitRoot(claimedRoot)) return {
2156
+ root: resolve(claimedRoot),
2157
+ source: { via: "plugin" }
2158
+ };
2159
+ const found = port ? await this.#portFolder(port).catch(() => void 0) : void 0;
2160
+ if (!found || !port) return void 0;
2161
+ return {
2162
+ root: projectRoot(found.folder),
2163
+ source: {
2164
+ via: found.docker ? "docker" : "port",
2165
+ port
2166
+ }
2167
+ };
2168
+ }
2169
+ /**
2170
+ * Use `root` for this address while `detected` is what Tencil finds there
2171
+ * (the overlay's Change). Picking the detected project itself undoes it.
2172
+ */
2173
+ async pick(pageUrl, detected, root) {
2174
+ const origin = safeOrigin(pageUrl);
2175
+ if (!origin) return;
2176
+ const key = pickKey(origin, detected);
2177
+ if (projectKey(root) === detected) await this.#picks.delete(key);
2178
+ else await this.#picks.set(key, projectKey(root));
2063
2179
  }
2064
2180
  /** Remember the user's choice for an address (from the overlay's project picker). */
2065
2181
  remember(pageUrl, root) {
@@ -2071,15 +2187,31 @@ var ProjectResolver = class {
2071
2187
  * package. When Docker publishes the port, the compose project of the container.
2072
2188
  */
2073
2189
  async folderForPort(port) {
2190
+ return (await this.#portFolder(port))?.folder;
2191
+ }
2192
+ async #portFolder(port) {
2193
+ const docker = async () => {
2194
+ const folder = await this.#composeFolder(port);
2195
+ return folder ? {
2196
+ folder,
2197
+ docker: true
2198
+ } : void 0;
2199
+ };
2074
2200
  const pids = await this.#listeners(port);
2075
- if (pids.length === 0 && this.#platform === "linux") return this.#composeFolder(port);
2201
+ if (pids.length === 0 && this.#platform === "linux") return docker();
2076
2202
  for (const pid of pids) {
2077
2203
  const cwd = await this.#cwd(pid).catch(() => void 0);
2078
- if (cwd && cwd !== sep && existsSync(join(cwd, "package.json"))) return cwd;
2204
+ if (cwd && cwd !== sep && existsSync(join(cwd, "package.json"))) return {
2205
+ folder: cwd,
2206
+ docker: false
2207
+ };
2079
2208
  const commandLine = await this.#commandLine(pid).catch(() => "");
2080
- if (isDockerForwarder(commandLine)) return this.#composeFolder(port);
2209
+ if (isDockerForwarder(commandLine)) return docker();
2081
2210
  const fromCommand = projectFromCommandLine(commandLine);
2082
- if (fromCommand) return fromCommand;
2211
+ if (fromCommand) return {
2212
+ folder: fromCommand,
2213
+ docker: false
2214
+ };
2083
2215
  }
2084
2216
  }
2085
2217
  /** Working directory of the compose project whose container publishes `port`. */
@@ -2214,6 +2346,15 @@ var OriginMemory = class {
2214
2346
  async set(origin, root) {
2215
2347
  const all = await this.#read();
2216
2348
  all[origin] = root;
2349
+ await this.#write(all);
2350
+ }
2351
+ async delete(origin) {
2352
+ const all = await this.#read();
2353
+ if (!(origin in all)) return;
2354
+ delete all[origin];
2355
+ await this.#write(all);
2356
+ }
2357
+ async #write(all) {
2217
2358
  await mkdir(dirname(this.path), { recursive: true });
2218
2359
  await writeFile(this.path, `${JSON.stringify(all, null, 2)}\n`);
2219
2360
  }
@@ -4118,7 +4259,7 @@ function errorMessage(error) {
4118
4259
  }
4119
4260
  //#endregion
4120
4261
  //#region src/version.ts
4121
- const VERSION = "0.9.0";
4262
+ const VERSION = "0.10.0";
4122
4263
  //#endregion
4123
4264
  //#region src/daemon/thread-store.ts
4124
4265
  const StoreFile = z.object({
@@ -4307,6 +4448,56 @@ async function hasClaudeCode() {
4307
4448
  return (await Promise.all(candidates.map((path) => access(path).then(() => true, () => false)))).includes(true);
4308
4449
  }
4309
4450
  //#endregion
4451
+ //#region src/daemon/preferences.ts
4452
+ const PreferencesFile = Preferences.extend({ version: z.literal(1) });
4453
+ function preferencesPath(dataDir = defaultDataDir()) {
4454
+ return join(dataDir, "preferences.json");
4455
+ }
4456
+ /**
4457
+ * The person's overlay preferences (theme, shortcuts), shared by every project
4458
+ * on this machine. One file, next to `settings.json`.
4459
+ */
4460
+ var FilePreferences = class {
4461
+ path;
4462
+ #cached;
4463
+ /** Saves run one after another, so a quick second change can't land first. */
4464
+ #writing = Promise.resolve();
4465
+ constructor(path = preferencesPath()) {
4466
+ this.path = path;
4467
+ }
4468
+ async load() {
4469
+ if (this.#cached) return this.#cached;
4470
+ const raw = await readFile(this.path, "utf8").catch(() => void 0);
4471
+ let parsed;
4472
+ try {
4473
+ parsed = raw === void 0 ? void 0 : PreferencesFile.safeParse(JSON.parse(raw));
4474
+ } catch {}
4475
+ const { version: _, ...preferences } = parsed?.success ? parsed.data : { version: 1 };
4476
+ this.#cached = preferences;
4477
+ return preferences;
4478
+ }
4479
+ /** Merges `changes` into what's saved and returns the result. */
4480
+ async update(changes) {
4481
+ const next = {
4482
+ ...await this.load(),
4483
+ ...changes
4484
+ };
4485
+ this.#cached = next;
4486
+ this.#writing = this.#writing.then(() => this.#write(next)).catch(() => {});
4487
+ await this.#writing;
4488
+ return next;
4489
+ }
4490
+ async #write(preferences) {
4491
+ await mkdir(dirname(this.path), { recursive: true });
4492
+ const temp = `${this.path}.tmp`;
4493
+ await writeFile(temp, `${JSON.stringify({
4494
+ version: 1,
4495
+ ...preferences
4496
+ }, null, 2)}\n`);
4497
+ await rename(temp, this.path);
4498
+ }
4499
+ };
4500
+ //#endregion
4310
4501
  //#region src/daemon/project-settings.ts
4311
4502
  const SettingsFile = z.object({
4312
4503
  version: z.literal(1),
@@ -5199,12 +5390,16 @@ var Updater = class {
5199
5390
  const DEFAULT_PORT = 4747;
5200
5391
  /** Screenshots ride along as data URLs. */
5201
5392
  const MAX_PAYLOAD_BYTES = 33554432;
5202
- /**
5203
- * One Tencil daemon per machine. Pages load `/tencil.js` from it; each
5204
- * page's WebSocket is attached to the project it belongs to (worked out from
5205
- * the dev server's port, or picked by the user), and every project gets its
5206
- * own bridge, provider connection and pin store, created on first use.
5207
- */
5393
+ /** The detected project behind a result, also when a pick overrides it. */
5394
+ function automaticPart(found) {
5395
+ if (!found) return void 0;
5396
+ if (found.source.via === "picked") return found.source.detected;
5397
+ return {
5398
+ root: found.root,
5399
+ via: found.source.via,
5400
+ port: found.source.port
5401
+ };
5402
+ }
5208
5403
  async function startDaemon(options = {}) {
5209
5404
  const port = options.port ?? 4747;
5210
5405
  const host = options.host ?? "127.0.0.1";
@@ -5222,10 +5417,18 @@ async function startDaemon(options = {}) {
5222
5417
  exec: options.exec,
5223
5418
  ensembleForPort: (port) => ensembleProjectForPort(port, ensembleOptions)
5224
5419
  });
5225
- const resolveProject = options.resolveProject ?? ((pageUrl, root) => resolver.resolve(pageUrl, root));
5420
+ const detectProject = async (pageUrl, root) => {
5421
+ if (!options.resolveProject) return resolver.detect(pageUrl, root);
5422
+ const found = await options.resolveProject(pageUrl, root);
5423
+ return found ? {
5424
+ root: found,
5425
+ source: { via: "port" }
5426
+ } : void 0;
5427
+ };
5226
5428
  const sessions = /* @__PURE__ */ new Map();
5227
5429
  const overlayPath = resolveOverlayPath();
5228
5430
  let updater;
5431
+ const preferences = new FilePreferences(preferencesPath(dataDir));
5229
5432
  const claudeCodeDir = join(dataDir, "claude-code");
5230
5433
  const t3Url = options.t3?.url ?? process.env.TENCIL_T3_URL;
5231
5434
  const createProvider = options.createProvider ?? ((root, id) => {
@@ -5381,7 +5584,13 @@ async function startDaemon(options = {}) {
5381
5584
  let current;
5382
5585
  let detach;
5383
5586
  let pageUrl = "";
5587
+ /** What detection found for this page, to switch from and back to. */
5588
+ let detected;
5384
5589
  let queue = Promise.resolve();
5590
+ preferences.load().then((saved) => client.send({
5591
+ type: "preferences",
5592
+ preferences: saved
5593
+ }));
5385
5594
  const attach = async (root, hello) => {
5386
5595
  detach?.();
5387
5596
  if (current) current.clients--;
@@ -5395,8 +5604,15 @@ async function startDaemon(options = {}) {
5395
5604
  if (message.type === "hello") {
5396
5605
  pageUrl = message.pageUrl ?? "";
5397
5606
  const claimed = typeof message.root === "string" ? message.root : void 0;
5398
- const root = await resolveProject(pageUrl, claimed).catch(() => void 0);
5399
- if (root) return attach(root, raw);
5607
+ const found = await detectProject(pageUrl, claimed).catch(() => void 0);
5608
+ detected = automaticPart(found);
5609
+ if (found) {
5610
+ await attach(found.root, raw);
5611
+ return client.send({
5612
+ type: "project.source",
5613
+ source: found.source
5614
+ });
5615
+ }
5400
5616
  return client.send({
5401
5617
  type: "project.unknown",
5402
5618
  pageUrl,
@@ -5409,14 +5625,42 @@ async function startDaemon(options = {}) {
5409
5625
  updater.update();
5410
5626
  return;
5411
5627
  }
5628
+ if (message.type === "preferences.update") {
5629
+ const changes = Preferences.parse(raw.preferences);
5630
+ const saved = await preferences.update(changes);
5631
+ const update = JSON.stringify({
5632
+ type: "preferences",
5633
+ preferences: saved
5634
+ });
5635
+ for (const other of wss.clients) if (other.readyState === WebSocket$1.OPEN) other.send(update);
5636
+ return;
5637
+ }
5412
5638
  if (message.type === "project.select" && message.root) {
5413
- await resolver.remember(pageUrl, message.root);
5414
- return attach(message.root, {
5639
+ const root = message.root;
5640
+ const auto = detected;
5641
+ if (auto) await resolver.pick(pageUrl, auto.root, root);
5642
+ else await resolver.remember(pageUrl, root);
5643
+ await attach(root, {
5415
5644
  type: "hello",
5416
5645
  protocol: 1,
5417
5646
  pageUrl
5418
5647
  });
5648
+ const source = !auto ? { via: "picked" } : projectKey(root) === auto.root ? {
5649
+ via: auto.via,
5650
+ port: auto.port
5651
+ } : {
5652
+ via: "picked",
5653
+ detected: auto
5654
+ };
5655
+ return client.send({
5656
+ type: "project.source",
5657
+ source
5658
+ });
5419
5659
  }
5660
+ if (message.type === "project.choices") return client.send({
5661
+ type: "project.choices",
5662
+ choices: await choices()
5663
+ });
5420
5664
  if (!current) return client.send({
5421
5665
  type: "error",
5422
5666
  message: "No project selected yet"
package/dist/vite.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { t as TencilCloudOptions } from "./cloud-dXtv3DCr.mjs";
1
+ import { t as TencilCloudOptions } from "./cloud-DKtU6lcZ.mjs";
2
2
  //#region src/integrations/vite.d.ts
3
3
  /** Structural subset of Vite's Plugin type, so `vite` stays an optional peer. */
4
4
  interface VitePlugin {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tencil",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "Point at any element of your running app and tell your coding agent what to fix.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Pixel Perfect Agency",
@@ -93,14 +93,14 @@
93
93
  "@types/ws": "^8.18.2",
94
94
  "react": "^19.3.0",
95
95
  "tsdown": "^0.23.0",
96
+ "@tencildev/bridge": "0.5.1",
96
97
  "@tencildev/overlay": "0.0.0",
97
- "@tencildev/protocol": "0.5.0",
98
- "@tencildev/bridge": "0.5.0",
99
98
  "@tencildev/provider-ensemble": "0.0.0",
99
+ "@tencildev/protocol": "0.6.0",
100
100
  "@tencildev/provider-claude-code": "0.0.0",
101
- "@tencildev/testing": "0.3.0",
102
- "@tencildev/tsconfig": "0.0.0",
103
- "@tencildev/provider-t3code": "0.0.0"
101
+ "@tencildev/provider-t3code": "0.0.0",
102
+ "@tencildev/testing": "0.3.1",
103
+ "@tencildev/tsconfig": "0.0.0"
104
104
  },
105
105
  "scripts": {
106
106
  "build": "tsdown && node scripts/copy-overlay.mjs",