opencube 0.3.0 → 0.3.3

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 CHANGED
@@ -21,7 +21,7 @@ OpenCube is packaged as an opencode plugin plus an Electron desktop process.
21
21
 
22
22
  ## Install
23
23
 
24
- Install globally through opencode:
24
+ Install the latest published version globally through opencode:
25
25
 
26
26
  ```sh
27
27
  opencode plugin opencube --global
@@ -41,6 +41,30 @@ You can also add it manually to `~/.config/opencode/opencode.json`:
41
41
  }
42
42
  ```
43
43
 
44
+ ## Update
45
+
46
+ If you already installed OpenCube and want to upgrade, reinstall the target version with `--force`:
47
+
48
+ ```sh
49
+ opencode plugin opencube@0.3.1 --global --force
50
+ ```
51
+
52
+ Using an explicit version is recommended for upgrades because it avoids stale `latest` cache behavior. You can still install the npm latest tag if desired:
53
+
54
+ ```sh
55
+ opencode plugin opencube@latest --global --force
56
+ ```
57
+
58
+ Then fully restart opencode and run `/pet` again. OpenCube and opencode plugins are loaded at startup, so the running desktop pet is not hot-replaced in place.
59
+
60
+ You can ask OpenCube to check npm for the latest published version:
61
+
62
+ ```text
63
+ /pet_update
64
+ ```
65
+
66
+ `/pet_update` does not hot-replace the running plugin. It reports whether a newer version exists and prints an explicit `opencode plugin opencube@<version> --global --force` command to run.
67
+
44
68
  ## Commands
45
69
 
46
70
  | Command | Description |
@@ -49,6 +73,8 @@ You can also add it manually to `~/.config/opencode/opencode.json`:
49
73
  | `/pet_stop` | Quit OpenCube. |
50
74
  | `/pet_say_hello` | Flash one currently free face three times with a random color. |
51
75
  | `/pet_fancy_say_hello` | Run a denser randomized light show across currently free faces. |
76
+ | `/pet_update` | Check npm for a newer OpenCube release and show the upgrade command. |
77
+ | `/pet_upgrade` | Alias for `/pet_update`. |
52
78
 
53
79
  These commands are handled by the plugin and do not get sent to the model.
54
80
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencube",
3
- "version": "0.3.0",
3
+ "version": "0.3.3",
4
4
  "description": "A tiny Three.js desktop pet for opencode session activity.",
5
5
  "main": "src/main.js",
6
6
  "exports": {
package/src/main.js CHANGED
@@ -16,6 +16,7 @@ const ICON_PATH = process.env.OPENCODE_PET_ICON || path.join(__dirname, "..", "a
16
16
  const MAX_EVENTS = 100
17
17
  const MAX_INTERACTION_EVENTS = 80
18
18
  const IDLE_TTL_MS = 5 * 60 * 1000
19
+ const PET_WINDOW_SIZE = 120
19
20
 
20
21
  let petWindow = null
21
22
  let panelWindow = null
@@ -31,17 +32,68 @@ let pendingQuestionsByRequest = new Map()
31
32
  let cleanupTimer = null
32
33
  let dragState = null
33
34
 
35
+ function normalizeDragPoint(point) {
36
+ const screenX = Number(point?.screenX)
37
+ const screenY = Number(point?.screenY)
38
+ if (!Number.isFinite(screenX) || !Number.isFinite(screenY)) return null
39
+ return { screenX, screenY }
40
+ }
41
+
42
+ function clamp(value, min, max) {
43
+ return Math.max(min, Math.min(max, value))
44
+ }
45
+
46
+ function getVirtualWorkArea() {
47
+ const displays = screen.getAllDisplays()
48
+ if (!displays.length) return screen.getPrimaryDisplay().workArea
49
+
50
+ return displays.reduce((area, display) => {
51
+ const next = display.workArea
52
+ const left = Math.min(area.x, next.x)
53
+ const top = Math.min(area.y, next.y)
54
+ const right = Math.max(area.x + area.width, next.x + next.width)
55
+ const bottom = Math.max(area.y + area.height, next.y + next.height)
56
+ return { x: left, y: top, width: right - left, height: bottom - top }
57
+ }, displays[0].workArea)
58
+ }
59
+
60
+ function normalizeWindowPosition(x, y, win) {
61
+ if (!Number.isFinite(x) || !Number.isFinite(y) || !win || win.isDestroyed()) return null
62
+
63
+ const bounds = win.getBounds()
64
+ const area = getVirtualWorkArea()
65
+ const minX = area.x
66
+ const minY = area.y
67
+ const maxX = area.x + area.width - Math.max(1, bounds.width)
68
+ const maxY = area.y + area.height - Math.max(1, bounds.height)
69
+
70
+ return {
71
+ x: Math.round(clamp(x, Math.min(minX, maxX), Math.max(minX, maxX))),
72
+ y: Math.round(clamp(y, Math.min(minY, maxY), Math.max(minY, maxY))),
73
+ }
74
+ }
75
+
34
76
  ipcMain.on("opencube-drag-start", (event, point) => {
35
77
  if (!petWindow || petWindow.isDestroyed()) return
78
+ const dragPoint = normalizeDragPoint(point)
79
+ if (!dragPoint) return
36
80
  const [x, y] = petWindow.getPosition()
37
- dragState = { windowX: x, windowY: y, screenX: point.screenX, screenY: point.screenY }
81
+ dragState = { windowX: x, windowY: y, screenX: dragPoint.screenX, screenY: dragPoint.screenY }
38
82
  })
39
83
 
40
84
  ipcMain.on("opencube-drag-move", (event, point) => {
41
85
  if (!petWindow || petWindow.isDestroyed() || !dragState) return
42
- const nextX = Math.round(dragState.windowX + point.screenX - dragState.screenX)
43
- const nextY = Math.round(dragState.windowY + point.screenY - dragState.screenY)
44
- petWindow.setPosition(nextX, nextY, false)
86
+ const dragPoint = normalizeDragPoint(point)
87
+ if (!dragPoint) return
88
+ const nextX = Math.round(dragState.windowX + dragPoint.screenX - dragState.screenX)
89
+ const nextY = Math.round(dragState.windowY + dragPoint.screenY - dragState.screenY)
90
+ const nextPosition = normalizeWindowPosition(nextX, nextY, petWindow)
91
+ if (!nextPosition) return
92
+ try {
93
+ petWindow.setPosition(nextPosition.x, nextPosition.y, false)
94
+ } catch {
95
+ dragState = null
96
+ }
45
97
  })
46
98
 
47
99
  ipcMain.on("opencube-drag-end", () => {
@@ -680,6 +732,7 @@ function petHtml3D() {
680
732
  const initialStateJson = JSON.stringify(getPetState()).replaceAll("<", "\\u003c")
681
733
  const iconUrl = createIconDataUrl()
682
734
  const threeCjsPath = JSON.stringify(path.join(path.dirname(require.resolve("three")), "three.cjs"))
735
+ const renderSize = PET_WINDOW_SIZE
683
736
  return `<!doctype html>
684
737
  <html>
685
738
  <head>
@@ -704,8 +757,8 @@ function petHtml3D() {
704
757
  #scene {
705
758
  position: absolute;
706
759
  inset: 0;
707
- width: 100%;
708
- height: 100%;
760
+ width: ${renderSize}px;
761
+ height: ${renderSize}px;
709
762
  pointer-events: none;
710
763
  }
711
764
  #hit-layer {
@@ -734,6 +787,7 @@ function petHtml3D() {
734
787
  const THREE = require(${threeCjsPath})
735
788
  const { ipcRenderer } = require("electron")
736
789
 
790
+ const PET_RENDER_SIZE = ${renderSize}
737
791
  window.__PET_STATE = ${initialStateJson}
738
792
  const stage = document.querySelector(".stage")
739
793
  const hitLayer = document.getElementById("hit-layer")
@@ -765,7 +819,7 @@ function petHtml3D() {
765
819
  camera.position.set(0, 0.04, 3.25)
766
820
  const renderer = new THREE.WebGLRenderer({ canvas, alpha: true, antialias: true })
767
821
  renderer.setClearColor(0x000000, 0)
768
- renderer.setPixelRatio(Math.min((window.devicePixelRatio || 1) * 1.5, 3))
822
+ renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2))
769
823
  renderer.outputColorSpace = THREE.SRGBColorSpace
770
824
 
771
825
  const cubeGroup = new THREE.Group()
@@ -1093,10 +1147,8 @@ function petHtml3D() {
1093
1147
  }
1094
1148
 
1095
1149
  function resize() {
1096
- const width = Math.max(1, window.innerWidth)
1097
- const height = Math.max(1, window.innerHeight)
1098
- renderer.setSize(width, height, false)
1099
- camera.aspect = width / height
1150
+ renderer.setSize(PET_RENDER_SIZE, PET_RENDER_SIZE, false)
1151
+ camera.aspect = 1
1100
1152
  camera.updateProjectionMatrix()
1101
1153
  }
1102
1154
  window.addEventListener("resize", resize)
@@ -1631,6 +1683,16 @@ function petHtml3D() {
1631
1683
  busyFaces,
1632
1684
  helloFlashes,
1633
1685
  permissionGlows,
1686
+ renderSize: {
1687
+ fixed: PET_RENDER_SIZE,
1688
+ innerWidth: window.innerWidth,
1689
+ innerHeight: window.innerHeight,
1690
+ devicePixelRatio: window.devicePixelRatio || 1,
1691
+ canvasWidth: canvas.width,
1692
+ canvasHeight: canvas.height,
1693
+ canvasClientWidth: canvas.clientWidth,
1694
+ canvasClientHeight: canvas.clientHeight,
1695
+ },
1634
1696
  }
1635
1697
  requestAnimationFrame(tick)
1636
1698
  }
@@ -1658,7 +1720,7 @@ function restorePosition(win) {
1658
1720
  try {
1659
1721
  const raw = fs.readFileSync(STATE_FILE, "utf8")
1660
1722
  const state = JSON.parse(raw)
1661
- if (typeof state.x === "number" && typeof state.y === "number") {
1723
+ if (Number.isFinite(state.x) && Number.isFinite(state.y)) {
1662
1724
  win.setPosition(state.x, state.y, false)
1663
1725
  return
1664
1726
  }
@@ -1676,8 +1738,8 @@ function restorePosition(win) {
1676
1738
  function createPetWindow() {
1677
1739
  if (petWindow && !petWindow.isDestroyed()) return petWindow
1678
1740
  petWindow = new BrowserWindow({
1679
- width: 120,
1680
- height: 120,
1741
+ width: PET_WINDOW_SIZE,
1742
+ height: PET_WINDOW_SIZE,
1681
1743
  show: false,
1682
1744
  frame: false,
1683
1745
  resizable: false,
@@ -1,7 +1,9 @@
1
1
  const { quitPet, sendEvent, showPet } = require("./plugin-shared.cjs")
2
+ const pkg = require("../package.json")
2
3
 
3
4
  const COMMAND_HANDLED_SENTINEL = "__OPENCODE_PET_COMMAND_HANDLED__"
4
5
  const CUB_ICON = "◈"
6
+ const UPDATE_CHECK_TIMEOUT_MS = 5000
5
7
 
6
8
  function handled() {
7
9
  throw new Error(COMMAND_HANDLED_SENTINEL)
@@ -27,6 +29,72 @@ function isFancySayHello(input) {
27
29
  return input.command === "pet_fancy_say_hello"
28
30
  }
29
31
 
32
+ function isUpdateCheck(input) {
33
+ return input.command === "pet_update" || input.command === "pet_upgrade"
34
+ }
35
+
36
+ function compareVersions(a, b) {
37
+ const left = String(a || "").split(".").map((part) => Number.parseInt(part, 10) || 0)
38
+ const right = String(b || "").split(".").map((part) => Number.parseInt(part, 10) || 0)
39
+ const length = Math.max(left.length, right.length)
40
+ for (let index = 0; index < length; index += 1) {
41
+ const diff = (left[index] || 0) - (right[index] || 0)
42
+ if (diff !== 0) return diff > 0 ? 1 : -1
43
+ }
44
+ return 0
45
+ }
46
+
47
+ async function fetchLatestPackageVersion(name = pkg.name, timeoutMs = UPDATE_CHECK_TIMEOUT_MS) {
48
+ const controller = new AbortController()
49
+ const timeout = setTimeout(() => controller.abort(), timeoutMs)
50
+
51
+ try {
52
+ const response = await fetch(`https://registry.npmjs.org/${encodeURIComponent(name)}/latest`, {
53
+ headers: { accept: "application/json" },
54
+ signal: controller.signal,
55
+ })
56
+ if (!response.ok) throw new Error(`npm registry returned ${response.status}`)
57
+ const info = await response.json()
58
+ return {
59
+ name: info.name || name,
60
+ version: info.version,
61
+ tarball: info.dist?.tarball,
62
+ }
63
+ } catch (error) {
64
+ if (error?.name === "AbortError") throw new Error(`npm registry check timed out after ${timeoutMs}ms`)
65
+ throw error
66
+ } finally {
67
+ clearTimeout(timeout)
68
+ }
69
+ }
70
+
71
+ function formatUpdateNotice(latest) {
72
+ const current = pkg.version
73
+ if (!latest?.version) return cubNotice(`Could not read the latest ${pkg.name} version from npm.`, "△")
74
+
75
+ const comparison = compareVersions(latest.version, current)
76
+ if (comparison < 0) {
77
+ return cubNotice(
78
+ [
79
+ `Welcome to the tiny time machine ✨`,
80
+ `This OpenCube is a dev build (${current}), ahead of npm (${latest.version}).`,
81
+ `Keep this cube away from paradoxes and production users.`,
82
+ ].join("\n"),
83
+ "✧",
84
+ )
85
+ }
86
+ if (comparison === 0) return cubNotice(`OpenCube is up to date (${current}).`, "✓")
87
+
88
+ return cubNotice(
89
+ [
90
+ `OpenCube ${latest.version} is available. Current version: ${current}.`,
91
+ `Upgrade with: opencode plugin ${pkg.name}@${latest.version} --global --force`,
92
+ `Then fully restart opencode and run /pet again.`,
93
+ ].join("\n"),
94
+ "↻",
95
+ )
96
+ }
97
+
30
98
  async function injectNotice(client, sessionID, text) {
31
99
  if (!sessionID || !client?.session?.prompt) return
32
100
 
@@ -85,10 +153,18 @@ module.exports = {
85
153
  template: "/pet_fancy_say_hello",
86
154
  description: "Trigger a randomized light show on OpenCube's free faces.",
87
155
  }
156
+ cfg.command.pet_update = {
157
+ template: "/pet_update",
158
+ description: "Check npm for a newer OpenCube version and show the upgrade command.",
159
+ }
160
+ cfg.command.pet_upgrade = {
161
+ template: "/pet_upgrade",
162
+ description: "Alias for /pet_update.",
163
+ }
88
164
  },
89
165
 
90
166
  "command.execute.before": async (input, output) => {
91
- if (!["pet", "pet_stop", "pet_say_hello", "pet_fancy_say_hello"].includes(input.command)) return
167
+ if (!["pet", "pet_stop", "pet_say_hello", "pet_fancy_say_hello", "pet_update", "pet_upgrade"].includes(input.command)) return
92
168
 
93
169
  // There is no official cancel primitive in command.execute.before yet.
94
170
  // Throwing this sentinel aborts the command flow before opencode sends
@@ -129,6 +205,39 @@ module.exports = {
129
205
  ? cubNotice("OpenCube is putting on a light show ✨", "✺")
130
206
  : cubNotice("OpenCube is sleeping... zzz Start it with /pet before the light show.", "☾"),
131
207
  )
208
+ } else if (isUpdateCheck(input)) {
209
+ let latest
210
+ try {
211
+ latest = await fetchLatestPackageVersion()
212
+ const versionComparison = compareVersions(latest.version, pkg.version)
213
+ await sendEvent({
214
+ type: "update.check",
215
+ message: "OpenCube checked npm for updates",
216
+ package: pkg.name,
217
+ currentVersion: pkg.version,
218
+ latestVersion: latest.version,
219
+ updateAvailable: versionComparison > 0,
220
+ devVersion: versionComparison < 0,
221
+ tarball: latest.tarball,
222
+ timeoutMs: UPDATE_CHECK_TIMEOUT_MS,
223
+ sessionID: input.sessionID,
224
+ source: "opencube-plugin",
225
+ })
226
+ } catch (error) {
227
+ await sendEvent({
228
+ type: "update.check_failed",
229
+ message: "OpenCube could not check npm for updates",
230
+ package: pkg.name,
231
+ currentVersion: pkg.version,
232
+ error: error?.message || String(error),
233
+ sessionID: input.sessionID,
234
+ source: "opencube-plugin",
235
+ })
236
+ await injectNotice(client, input.sessionID, cubNotice(`Could not check npm for OpenCube updates: ${error?.message || error}`, "△"))
237
+ handled()
238
+ }
239
+
240
+ await injectNotice(client, input.sessionID, formatUpdateNotice(latest))
132
241
  } else {
133
242
  await showPet({
134
243
  onProgress: (message) => injectNotice(client, input.sessionID, message),