@rantingjs/studio 0.0.0-stage → 0.1.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.
Files changed (54) hide show
  1. package/README.md +145 -2
  2. package/dist/badge.d.ts +2 -0
  3. package/dist/badge.js +17 -0
  4. package/dist/badge.js.map +1 -0
  5. package/dist/commands.d.ts +8 -0
  6. package/dist/commands.js +49 -0
  7. package/dist/commands.js.map +1 -0
  8. package/dist/config.d.ts +24 -0
  9. package/dist/config.js +26 -0
  10. package/dist/config.js.map +1 -0
  11. package/dist/forge.d.ts +67 -0
  12. package/dist/forge.js +138 -0
  13. package/dist/forge.js.map +1 -0
  14. package/dist/http.d.ts +25 -0
  15. package/dist/http.js +248 -0
  16. package/dist/http.js.map +1 -0
  17. package/dist/index.d.ts +4 -0
  18. package/dist/index.js +4 -0
  19. package/dist/index.js.map +1 -0
  20. package/dist/opencode.d.ts +30 -0
  21. package/dist/opencode.js +113 -0
  22. package/dist/opencode.js.map +1 -0
  23. package/dist/page.d.ts +11 -0
  24. package/dist/page.js +31 -0
  25. package/dist/page.js.map +1 -0
  26. package/dist/studio.module.d.ts +21 -0
  27. package/dist/studio.module.js +129 -0
  28. package/dist/studio.module.js.map +1 -0
  29. package/package.json +99 -4
  30. package/src/config.ts +26 -0
  31. package/ui/app.tsx +862 -0
  32. package/ui/changes.tsx +420 -0
  33. package/ui/code.tsx +194 -0
  34. package/ui/composer.tsx +362 -0
  35. package/ui/conversations.tsx +103 -0
  36. package/ui/editor.ts +144 -0
  37. package/ui/forge.tsx +236 -0
  38. package/ui/icons.tsx +59 -0
  39. package/ui/logic.ts +304 -0
  40. package/ui/logs.tsx +87 -0
  41. package/ui/model-picker.tsx +121 -0
  42. package/ui/notify.ts +45 -0
  43. package/ui/page.ts +27 -0
  44. package/ui/popover.tsx +66 -0
  45. package/ui/prose.tsx +50 -0
  46. package/ui/providers.tsx +384 -0
  47. package/ui/queue.tsx +89 -0
  48. package/ui/shared.ts +74 -0
  49. package/ui/splitter.tsx +70 -0
  50. package/ui/styles.css +2632 -0
  51. package/ui/terminal.tsx +108 -0
  52. package/ui/tsconfig.json +13 -0
  53. package/ui/urls.ts +9 -0
  54. package/ui/usage.tsx +86 -0
@@ -0,0 +1,108 @@
1
+ /** @jsxImportSource hono/jsx/dom */
2
+ import { FitAddon } from '@xterm/addon-fit';
3
+ import { Terminal } from '@xterm/xterm';
4
+ import { useEffect, useRef, useState } from 'hono/jsx/dom';
5
+ import { client, directory, errorText, location_ } from './shared.js';
6
+
7
+ /**
8
+ * A shell in the project, OpenCode's terminal. The page asks the app for a one-use ticket (the app's gate decides),
9
+ * then talks to OpenCode's WebSocket with it: output comes as text frames, keys go back as text.
10
+ * ponytail: the WebSocket goes to OpenCode on 127.0.0.1, so a Studio opened from another machine has no terminal;
11
+ * pass the upgrade through the app if that is wanted.
12
+ */
13
+ export function TerminalPanel({ visible }: { visible: boolean }) {
14
+ const host = useRef<HTMLDivElement>(null);
15
+ const [status, setStatus] = useState<'connecting' | 'open' | 'ended'>('connecting');
16
+ const [error, setError] = useState('');
17
+ const [generation, setGeneration] = useState(0);
18
+ const fitter = useRef<() => void>(() => undefined);
19
+
20
+ useEffect(() => {
21
+ if (!host.current) return;
22
+ const term = new Terminal({
23
+ cursorBlink: true,
24
+ fontFamily: "'IBM Plex Mono', ui-monospace, monospace",
25
+ fontSize: 13,
26
+ theme: { background: '#14181f', foreground: '#ecebe6', cursor: '#f76b1c', selectionBackground: '#3a2216' },
27
+ });
28
+ const fit = new FitAddon();
29
+ term.loadAddon(fit);
30
+ term.open(host.current);
31
+ let socket: WebSocket | undefined;
32
+ let ptyID: string | undefined;
33
+ let closed = false;
34
+ const resize = () => {
35
+ try {
36
+ fit.fit();
37
+ } catch {
38
+ return; // Hidden: no size to take.
39
+ }
40
+ if (ptyID) void client.pty.update({ ptyID, location: location_, size: { rows: term.rows, cols: term.cols } }).catch(() => undefined);
41
+ };
42
+ fitter.current = resize;
43
+ const observer = new ResizeObserver(resize);
44
+ observer.observe(host.current);
45
+
46
+ void (async () => {
47
+ try {
48
+ setStatus('connecting');
49
+ setError('');
50
+ const [info, where] = await Promise.all([client.server.info(), directory]);
51
+ // The shell left running (a page reloaded), else a new one; a new terminal after one ended.
52
+ const running = generation === 0 ? (await client.pty.list({ location: location_ })).data.find((pty) => pty.status === 'running') : undefined;
53
+ const pty = running ?? (await client.pty.create({ location: location_, title: 'Studio' })).data;
54
+ ptyID = pty.id;
55
+ const { ticket } = (await client.pty.connect.token({ ptyID, location: location_, 'x-opencode-ticket': '1' })).data;
56
+ if (closed) return;
57
+ const url = new URL(`api/pty/${encodeURIComponent(ptyID)}/connect`, info.urls[0]!.replace(/^http/, 'ws').replace(/\/?$/, '/'));
58
+ url.searchParams.set('location[directory]', where);
59
+ url.searchParams.set('cursor', '0');
60
+ url.searchParams.set('ticket', ticket);
61
+ socket = new WebSocket(url);
62
+ socket.binaryType = 'arraybuffer';
63
+ socket.onopen = () => {
64
+ setStatus('open');
65
+ resize();
66
+ term.focus();
67
+ };
68
+ // Text is the terminal's output; binary frames are OpenCode's own notes (where the stream is).
69
+ socket.onmessage = (event) => typeof event.data === 'string' && term.write(event.data);
70
+ socket.onclose = () => !closed && setStatus('ended');
71
+ term.onData((data) => socket?.readyState === WebSocket.OPEN && socket.send(data));
72
+ } catch (failure) {
73
+ setError(errorText(failure));
74
+ setStatus('ended');
75
+ }
76
+ })();
77
+
78
+ return () => {
79
+ closed = true;
80
+ observer.disconnect();
81
+ socket?.close();
82
+ term.dispose();
83
+ };
84
+ }, [generation]);
85
+
86
+ // Shown again: its size may have changed while hidden.
87
+ useEffect(() => {
88
+ if (visible) requestAnimationFrame(() => fitter.current());
89
+ }, [visible]);
90
+
91
+ return (
92
+ <div class="terminal">
93
+ <div class="terminal-host" ref={host} />
94
+ {status !== 'open' && (
95
+ <div class="terminal-note">
96
+ {status === 'connecting' ? (
97
+ 'Connecting…'
98
+ ) : (
99
+ <>
100
+ <span>{error || 'The terminal ended.'}</span>
101
+ <button class="primary" onClick={() => setGeneration((value) => value + 1)}>New terminal</button>
102
+ </>
103
+ )}
104
+ </div>
105
+ )}
106
+ </div>
107
+ );
108
+ }
@@ -0,0 +1,13 @@
1
+ {
2
+ "extends": "../../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "module": "esnext",
5
+ "moduleResolution": "bundler",
6
+ "jsx": "react-jsx",
7
+ "jsxImportSource": "hono/jsx/dom",
8
+ "lib": ["ES2023", "DOM", "DOM.Iterable"],
9
+ "types": ["bun"],
10
+ "noEmit": true
11
+ },
12
+ "include": ["."]
13
+ }
package/ui/urls.ts ADDED
@@ -0,0 +1,9 @@
1
+ /**
2
+ * A page of this app for the preview, from a path a link or the address bar gave. Anything but a path of this origin
3
+ * (`javascript:...`, `//other.site`, a full URL) is the home page: a crafted link would otherwise run its script in
4
+ * Studio's origin, where it can drive the agent.
5
+ */
6
+ export function pageUrl(path: string, origin: string): string {
7
+ const own = /^\/(?![/\\])/.test(path) ? new URL(path, origin) : undefined;
8
+ return own && own.origin === origin ? own.href : `${origin}/`;
9
+ }
package/ui/usage.tsx ADDED
@@ -0,0 +1,86 @@
1
+ /** @jsxImportSource hono/jsx/dom */
2
+ // How full the conversation's context is: a ring beside the model picker, and what makes it up, with the summary that
3
+ // frees it.
4
+ import type { SessionInfo } from '@opencode/client';
5
+ import { useState } from 'hono/jsx/dom';
6
+ import { Icon } from './icons.js';
7
+ import { contextLabel, contextShare } from './logic.js';
8
+ import { Popover } from './popover.js';
9
+
10
+ /** From this share on, the ring warns and the summary is offered first. */
11
+ const FULL = 80;
12
+ const RING = 2 * Math.PI * 7;
13
+
14
+ export interface Usage {
15
+ tokens: number;
16
+ last: { input: number; output: number; reasoning: number; cache: { read: number; write: number } };
17
+ }
18
+
19
+ export function ContextMeter({ usage, window, modelName, session, onCompact }: { usage: Usage | undefined; window: number | undefined; modelName: string | undefined; session: SessionInfo | undefined; onCompact: () => void }) {
20
+ const [open, setOpen] = useState(false);
21
+ if (!usage || !window) return null;
22
+ const share = contextShare(usage.tokens, window);
23
+ const full = share >= FULL;
24
+ const total = session ? session.tokens.input + session.tokens.output + session.tokens.reasoning + session.tokens.cache.read + session.tokens.cache.write : 0;
25
+ const { last } = usage;
26
+ return (
27
+ <div class="anchor usage-anchor">
28
+ <button
29
+ type="button"
30
+ class={`chip usage-chip${full ? ' full' : ''}${open ? ' open' : ''}`}
31
+ onClick={() => setOpen(!open)}
32
+ title={`${contextLabel(usage.tokens)} of ${contextLabel(window)} tokens of context (${share}%)`}
33
+ aria-label={`Context: ${share}% used`}
34
+ >
35
+ <svg class="usage-ring" width="18" height="18" viewBox="0 0 18 18" aria-hidden="true">
36
+ <circle cx="9" cy="9" r="7" class="usage-track" />
37
+ <circle cx="9" cy="9" r="7" class="usage-fill" stroke-dasharray={`${(RING * share) / 100} ${RING}`} transform="rotate(-90 9 9)" />
38
+ </svg>
39
+ <span class="chip-text">{contextLabel(usage.tokens)}</span>
40
+ </button>
41
+ <Popover open={open} onClose={() => setOpen(false)} placement="above-end" class="usage-popover">
42
+ <div class="usage-body">
43
+ <div class="usage-head">
44
+ <strong>Context</strong>
45
+ <span>
46
+ {contextLabel(usage.tokens)} of {contextLabel(window)} · {share}%
47
+ </span>
48
+ </div>
49
+ <div class={`usage-bar${full ? ' full' : ''}`}>
50
+ <span style={`width: ${share}%`} />
51
+ </div>
52
+ <dl class="usage-rows">
53
+ <dt>Last request</dt>
54
+ <dd>
55
+ {contextLabel(last.cache.read)} from the cache · {contextLabel(last.cache.write)} cached · {contextLabel(last.input)} new · {contextLabel(last.output)} written
56
+ </dd>
57
+ {session && total > 0 && (
58
+ <>
59
+ <dt>This conversation</dt>
60
+ <dd>
61
+ {contextLabel(total)} tokens in all{session.cost > 0 ? ` · $${session.cost.toFixed(2)}` : ''}
62
+ </dd>
63
+ </>
64
+ )}
65
+ </dl>
66
+ <p class="hint">
67
+ {full
68
+ ? `Close to what ${modelName ?? 'the model'} can take: summarize the earlier messages, or start a new conversation.`
69
+ : `What ${modelName ?? 'the model'} reads with each message. Summarizing the earlier messages frees it when it fills.`}
70
+ </p>
71
+ <button
72
+ type="button"
73
+ class={full ? 'primary' : ''}
74
+ onClick={() => {
75
+ setOpen(false);
76
+ onCompact();
77
+ }}
78
+ >
79
+ <Icon name="history" size={14} />
80
+ Summarize earlier messages
81
+ </button>
82
+ </div>
83
+ </Popover>
84
+ </div>
85
+ );
86
+ }