@lincyaw/studio 0.2.0-rc.34 → 0.2.0-rc.36

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
@@ -17,6 +17,10 @@ Studio is included with Presmium. In a project with `pnpm add -D @lincyaw/presmi
17
17
 
18
18
  Sessions use the workspace root as their working directory and can read and write files throughout it. Opening another lecture changes document context, not the session or its directory.
19
19
 
20
+ External MCP clients connect over Streamable HTTP at `http://127.0.0.1:4321/mcp`. Set `Authorization: Bearer <token>` using the token file named at startup. Studio keeps this credential for the same launch directory across restarts and upgrades, separately from its internal service credential. With `--host 0.0.0.0`, use the printed network MCP address from other machines. The address and credential need configuring once; clients reconnect after Studio restarts.
21
+
22
+ The workspace service and MCP share fixed port 4321. If it is occupied, Studio reports an error instead of changing the address. Use `--preview-port <n>` to choose another fixed port for a second workspace, or `--preview-port 0` to explicitly request a temporary port. Behind a reverse proxy, MCP is at `mcp` under `--preview-url`.
23
+
20
24
  The model is chosen in the workbench's model settings, or given by the environment: `DEEPSEEK_API_KEY`, or `PRESMIUM_MODEL_URL`, `PRESMIUM_MODEL_KEY` and `PRESMIUM_MODEL_PROVIDER` for a Messages-compatible service. DeepSeek Harness's home for the workbench (its sessions, model settings and credentials, and the log) is `$XDG_STATE_HOME/presmium/studio`, never your own `~/.dsh`. Each installation (a version, or a checkout) has its own profile there, `profiles/presmium-studio-<hash>`, and its own log, `presmium-studio-<hash>.log`, so Studios of different installations can run at the same time without loading each other's code. Sessions and credentials are shared; a new installation's profile starts with the settings of the one used last.
21
25
 
22
26
  Startup prints the log path to stderr and streams workspace-service logs there while running. The file keeps timestamped output from both processes. Preview logs identify the document, renderer, request, cache/fallback choice and elapsed time; export logs identify the job and phase. A backend preview marked ready means its address is prepared; browser loading and the preview handshake may still be pending. The host's output, including its login URL, stays in the log file.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lincyaw/studio",
3
- "version": "0.2.0-rc.34",
3
+ "version": "0.2.0-rc.36",
4
4
  "description": "presmium studio: Presmium's workbench on DeepSeek Harness in one command, with the workspace service of a directory (experimental)",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -39,9 +39,9 @@
39
39
  "@deepseek-ai/dsh": "0.2.1-alpha.4",
40
40
  "http-proxy-3": "2.0.1",
41
41
  "jiti": "^2.7.0",
42
- "@lincyaw/dsh": "0.2.0-rc.34"
42
+ "@lincyaw/dsh": "0.2.0-rc.36"
43
43
  },
44
44
  "devDependencies": {
45
- "@lincyaw/cli": "1.0.0-rc.35"
45
+ "@lincyaw/cli": "1.0.0-rc.37"
46
46
  }
47
47
  }
package/src/cli.ts CHANGED
@@ -62,6 +62,8 @@ export async function main(argv: string[], options: MainOptions = {}): Promise<n
62
62
  }
63
63
  process.stdout.write(`studio ${studio.url}\n`)
64
64
  for (const url of studio.networkUrls) process.stdout.write(`network ${url}\n`)
65
+ process.stdout.write(`mcp ${studio.mcp.url}\n`)
66
+ process.stderr.write(`${t.mcpToken(studio.mcp.tokenFile)}\n`)
65
67
  process.stderr.write(`${t.log(studio.log)}\n`)
66
68
  if (values.host && values.host !== '127.0.0.1' && values.host !== '::1' && !values['public-url']) {
67
69
  process.stderr.write(`${studio.networkUrls.length
package/src/messages.ts CHANGED
@@ -13,6 +13,9 @@ export interface StudioMessages {
13
13
  openOnAllNetworks(port: string, previewPort: string): string
14
14
  ended(how: string, log: string): string
15
15
  portInUse(port: number, address: string): string
16
+ previewPortInUse(port: number, address: string): string
17
+ mcpToken(file: string): string
18
+ badMcpToken(file: string): string
16
19
  noFreePort(address: string): string
17
20
  badHost(address: string): string
18
21
  wholeLog(message: string, file: string): string
@@ -40,7 +43,7 @@ presmium-studio-<散列>.log(每个安装一个)保存在 $XDG_STATE_HOME/pr
40
43
  它的主机受信任,打印的网址用它。代理需保留 Host 并转发 WebSocket
41
44
  --preview-url <网址> 同样情况下预览所在的地址,由代理转发到工作区服务;可以带路径,
42
45
  此时它必须与 Studio 不同源,否则预览中文稿的代码能读到 Studio 的会话
43
- --preview-port <端口> 工作区服务的端口(默认为空闲端口),供需要事先知道端口的代理使用
46
+ --preview-port <端口> 工作区服务与 HTTP MCP 的固定端口(默认 4321);0 为随机端口
44
47
  -h, --help 显示本说明`,
45
48
  badPort: value => `--port 需要 0 到 65535 之间的端口号,而不是“${value}”。`,
46
49
  badUrl: (option, value) => `${option} 需要一个完整的网址,例如 https://studio.example.com/,而不是“${value}”。`,
@@ -52,6 +55,9 @@ presmium-studio-<散列>.log(每个安装一个)保存在 $XDG_STATE_HOME/pr
52
55
  openOnAllNetworks: (port, previewPort) => `presmium studio: 正在全部局域网网络接口上监听,且没有登录:任何能用上面的网址访问端口 ${port}(以及预览的端口 ${previewPort})的人都以你的身份使用这个工作区(编辑、运行代码、使用你的模型)。请只在可信的网络中使用;不加 --host 时只在本机监听。`,
53
56
  ended: (how, log) => `presmium studio: ${how}。日志在 ${log}`,
54
57
  portInUse: (port, address) => `${address} 的端口 ${port} 已被占用。请用 --port 指定另一个端口。`,
58
+ previewPortInUse: (port, address) => `${address} 的工作区与 MCP 端口 ${port} 已被占用。请停止占用它的服务,或用 --preview-port 指定另一个固定端口。`,
59
+ mcpToken: file => `presmium studio: MCP 客户端使用 HTTP(Streamable HTTP),从 ${file} 读取访问令牌,设置 Authorization: Bearer <访问令牌>;同一目录重启与升级后保持不变。`,
60
+ badMcpToken: file => `MCP 访问令牌文件 ${file} 无效。请移走该文件后重试,并在客户端更新访问令牌。`,
55
61
  noFreePort: address => `${address} 上没有空闲的端口。请停止占用端口的服务后重试。`,
56
62
  badHost: address => `--host 需要本机的一个 IP 地址,例如 192.168.0.21,也可以使用 0.0.0.0,而不是“${address}”。`,
57
63
  wholeLog: (message, file) => `${message}${message.endsWith('\n') ? '' : '。'}完整的日志在 ${file}`,
@@ -79,7 +85,7 @@ Options:
79
85
  --preview-url <url> in that case, the address of the previews, forwarded to the workspace service; it
80
86
  may have a path, and should be another origin than Studio’s, or the documents’ code in
81
87
  previews can read Studio’s session
82
- --preview-port <n> the workspace service’s port (default: a free one), for a proxy that needs it in advance
88
+ --preview-port <n> fixed workspace-service and HTTP MCP port (default: 4321); 0 requests a random port
83
89
  -h, --help show this help`,
84
90
  badPort: value => `--port takes a port number from 0 to 65535, not “${value}”.`,
85
91
  badUrl: (option, value) => `${option} takes a full address, such as https://studio.example.com/, not “${value}”.`,
@@ -91,6 +97,9 @@ Options:
91
97
  openOnAllNetworks: (port, previewPort) => `presmium studio: listening on all local network interfaces, with no login: anyone who reaches port ${port} (and the previews’ port ${previewPort}) with the addresses above works in this workspace as you (edits, runs code, uses your model). Use it on a network you trust; without --host it listens on this machine only.`,
92
98
  ended: (how, log) => `presmium studio: ${how}; the log is ${log}`,
93
99
  portInUse: (port, address) => `Port ${port} of ${address} is in use. Give another with --port.`,
100
+ previewPortInUse: (port, address) => `Workspace and MCP port ${port} of ${address} is in use. Stop the service using it, or choose another fixed port with --preview-port.`,
101
+ mcpToken: file => `presmium studio: MCP clients use HTTP (Streamable HTTP); read the access token from ${file} and set Authorization: Bearer <value>. It stays the same for this directory across restarts and upgrades.`,
102
+ badMcpToken: file => `MCP access token file ${file} is invalid. Move it aside and retry, then update the access token in your clients.`,
94
103
  noFreePort: address => `There is no free port on ${address}. Stop a service that uses one, then retry.`,
95
104
  badHost: address => `--host takes one local IP address, such as 192.168.0.21, or 0.0.0.0 to listen on all IPv4 network interfaces, not “${address}”.`,
96
105
  wholeLog: (message, file) => `${message.endsWith('\n') ? message : `${message}; `}the whole log is ${file}`,
package/src/studio.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { createHash, randomBytes } from 'node:crypto'
2
- import { closeSync, copyFileSync, existsSync, mkdirSync, openSync, readdirSync, readFileSync, realpathSync, renameSync, rmSync, statSync, writeFileSync, writeSync } from 'node:fs'
2
+ import { chmodSync, closeSync, copyFileSync, existsSync, mkdirSync, openSync, readdirSync, readFileSync, realpathSync, renameSync, rmSync, statSync, writeFileSync, writeSync } from 'node:fs'
3
3
  import { createRequire } from 'node:module'
4
4
  import { createServer, isIP } from 'node:net'
5
5
  import type { AddressInfo } from 'node:net'
@@ -15,6 +15,7 @@ import type { Proxy } from './proxy.ts'
15
15
 
16
16
  const PROFILE = 'presmium-studio'
17
17
  const DEFAULT_PORT = 4320
18
+ const DEFAULT_PREVIEW_PORT = 4321
18
19
  const LOG_BYTES = 16 << 20
19
20
 
20
21
  const PACKAGE_DIR = fileURLToPath(new URL('..', import.meta.url))
@@ -30,7 +31,7 @@ export interface StudioOptions {
30
31
  */
31
32
  publicUrl?: string
32
33
  previewUrl?: string
33
- /** The workspace service's port (default: a free one), for a proxy that needs it in advance. */
34
+ /** The workspace service and HTTP MCP port (default: 4321). Zero explicitly requests a free port. */
34
35
  previewPort?: number
35
36
  env?: NodeJS.ProcessEnv
36
37
  presmium?: string
@@ -42,6 +43,7 @@ export interface Studio {
42
43
  url: string
43
44
  networkUrls: string[]
44
45
  workspaceServerUrl: string
46
+ mcp: { url: string, tokenFile: string }
45
47
  root: string
46
48
  home: string
47
49
  log: string
@@ -55,6 +57,23 @@ function studioHome(env: NodeJS.ProcessEnv = process.env): string {
55
57
  return join(env.XDG_STATE_HOME || join(env.HOME || homedir(), '.local', 'state'), 'presmium', 'studio')
56
58
  }
57
59
 
60
+ /** Independent of the installation/profile so upgrades and restarts preserve external MCP access. */
61
+ function workspaceCredential(home: string, dir: string): { token: string, file: string } {
62
+ const directory = join(home, 'mcp')
63
+ mkdirSync(directory, { recursive: true, mode: 0o700 })
64
+ const id = createHash('sha256').update(realpathSync(dir)).digest('hex').slice(0, 32)
65
+ const file = join(directory, `${id}.token`)
66
+ try {
67
+ writeFileSync(file, `${randomBytes(24).toString('base64url')}\n`, { flag: 'wx', mode: 0o600 })
68
+ } catch (error) {
69
+ if ((error as NodeJS.ErrnoException).code !== 'EEXIST') throw error
70
+ }
71
+ chmodSync(file, 0o600)
72
+ const token = readFileSync(file, 'utf8').trim()
73
+ if (!/^[A-Za-z0-9_-]{32}$/.test(token)) throw new Error(STUDIO_MESSAGES[systemLocale()].badMcpToken(file))
74
+ return { token, file }
75
+ }
76
+
58
77
  /** The bundle `@lincyaw/dsh` this package depends on: the directory the profile links to. */
59
78
  function bundleDir(): string {
60
79
  return realpathSync(dirname(createRequire(join(PACKAGE_DIR, 'package.json')).resolve('@lincyaw/dsh/package.json')))
@@ -202,6 +221,8 @@ export async function startStudio(options: StudioOptions): Promise<Studio> {
202
221
  }
203
222
  const boundAddress = listenAddress ?? '127.0.0.1'
204
223
  const wildcard = WILDCARD.test(boundAddress)
224
+ const previewPort = options.previewPort ?? DEFAULT_PREVIEW_PORT
225
+ if (previewPort !== 0 && await freePort(boundAddress, previewPort) === undefined) throw new Error(t.previewPortInUse(previewPort, boundAddress))
205
226
  const port = await choosePort(boundAddress, options.port)
206
227
  const dshAddress = wildcard ? '127.0.0.1' : boundAddress
207
228
  const dshPort = wildcard ? await choosePort(dshAddress, undefined) : port
@@ -218,6 +239,7 @@ export async function startStudio(options: StudioOptions): Promise<Studio> {
218
239
  const bundle = bundleDir()
219
240
  const profile = installProfile(home, bundle)
220
241
  writeProfile(home, profile, 'web', bundle)
242
+ const { token: mcpToken, file: tokenFile } = workspaceCredential(home, options.dir)
221
243
  const log = new Log(join(home, `${profile}.log`))
222
244
  const serviceToken = randomBytes(24).toString('base64url')
223
245
  const started: LaunchedProcess[] = []
@@ -234,7 +256,8 @@ export async function startStudio(options: StudioOptions): Promise<Studio> {
234
256
  presmium: options.presmium ?? presmiumBin(env, t.presmiumMissing),
235
257
  dir: resolve(options.dir),
236
258
  serviceToken,
237
- ...options.previewPort ? { port: options.previewPort } : {},
259
+ mcpToken,
260
+ port: previewPort,
238
261
  args: [
239
262
  ...listenAddress ? ['--host', listenAddress] : [],
240
263
  ...origins.flatMap(origin => ['--preview-origin', origin]),
@@ -245,6 +268,8 @@ export async function startStudio(options: StudioOptions): Promise<Studio> {
245
268
  })
246
269
  started.push(server.process)
247
270
  const { url, root } = await server.ready
271
+ const mcpUrl = new URL(`${(options.previewUrl ?? url).replace(/\/+$/, '')}/mcp`)
272
+ if (!options.previewUrl && listenAddress) mcpUrl.hostname = bracketIPv6(primaryHost)
248
273
 
249
274
  const trustedHosts = wildcard
250
275
  ? [...hosts, hostname(), `${hostname()}.local`]
@@ -285,6 +310,7 @@ export async function startStudio(options: StudioOptions): Promise<Studio> {
285
310
  url: address,
286
311
  networkUrls,
287
312
  workspaceServerUrl: url,
313
+ mcp: { url: mcpUrl.href, tokenFile },
288
314
  root,
289
315
  home,
290
316
  log: log.file,