bincode-cli 1.0.1
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/AGENTS.md +27 -0
- package/README.md +15 -0
- package/bin/bincode +98 -0
- package/bunfig.toml +4 -0
- package/package.json +124 -0
- package/parsers-config.ts +239 -0
- package/script/build.ts +167 -0
- package/script/postinstall.mjs +206 -0
- package/script/publish.ts +99 -0
- package/script/schema.ts +47 -0
- package/src/acp/README.md +164 -0
- package/src/acp/agent.ts +1051 -0
- package/src/acp/session.ts +101 -0
- package/src/acp/types.ts +22 -0
- package/src/agent/agent.ts +398 -0
- package/src/agent/generate.txt +75 -0
- package/src/agent/prompt/compaction.txt +12 -0
- package/src/agent/prompt/explore.txt +18 -0
- package/src/agent/prompt/summary.txt +10 -0
- package/src/agent/prompt/title.txt +36 -0
- package/src/auth/bineric-login.ts +506 -0
- package/src/auth/index.ts +70 -0
- package/src/bun/index.ts +114 -0
- package/src/bus/bus-event.ts +43 -0
- package/src/bus/global.ts +10 -0
- package/src/bus/index.ts +105 -0
- package/src/cli/auth-check.ts +61 -0
- package/src/cli/bootstrap.ts +21 -0
- package/src/cli/cmd/acp.ts +88 -0
- package/src/cli/cmd/agent.ts +256 -0
- package/src/cli/cmd/auth.ts +436 -0
- package/src/cli/cmd/cmd.ts +7 -0
- package/src/cli/cmd/debug/config.ts +15 -0
- package/src/cli/cmd/debug/file.ts +91 -0
- package/src/cli/cmd/debug/index.ts +43 -0
- package/src/cli/cmd/debug/lsp.ts +48 -0
- package/src/cli/cmd/debug/ripgrep.ts +83 -0
- package/src/cli/cmd/debug/scrap.ts +15 -0
- package/src/cli/cmd/debug/skill.ts +15 -0
- package/src/cli/cmd/debug/snapshot.ts +48 -0
- package/src/cli/cmd/export.ts +88 -0
- package/src/cli/cmd/generate.ts +38 -0
- package/src/cli/cmd/github.ts +1399 -0
- package/src/cli/cmd/import.ts +98 -0
- package/src/cli/cmd/login.ts +112 -0
- package/src/cli/cmd/logout.ts +38 -0
- package/src/cli/cmd/mcp.ts +654 -0
- package/src/cli/cmd/models.ts +77 -0
- package/src/cli/cmd/pr.ts +112 -0
- package/src/cli/cmd/run.ts +368 -0
- package/src/cli/cmd/serve.ts +31 -0
- package/src/cli/cmd/session.ts +106 -0
- package/src/cli/cmd/stats.ts +298 -0
- package/src/cli/cmd/tui/app.tsx +669 -0
- package/src/cli/cmd/tui/attach.ts +30 -0
- package/src/cli/cmd/tui/component/border.tsx +21 -0
- package/src/cli/cmd/tui/component/dialog-agent.tsx +31 -0
- package/src/cli/cmd/tui/component/dialog-command.tsx +123 -0
- package/src/cli/cmd/tui/component/dialog-mcp.tsx +86 -0
- package/src/cli/cmd/tui/component/dialog-model.tsx +223 -0
- package/src/cli/cmd/tui/component/dialog-provider.tsx +224 -0
- package/src/cli/cmd/tui/component/dialog-session-list.tsx +102 -0
- package/src/cli/cmd/tui/component/dialog-session-rename.tsx +31 -0
- package/src/cli/cmd/tui/component/dialog-status.tsx +162 -0
- package/src/cli/cmd/tui/component/dialog-tag.tsx +44 -0
- package/src/cli/cmd/tui/component/dialog-theme-list.tsx +50 -0
- package/src/cli/cmd/tui/component/logo.tsx +32 -0
- package/src/cli/cmd/tui/component/prompt/autocomplete.tsx +560 -0
- package/src/cli/cmd/tui/component/prompt/history.tsx +108 -0
- package/src/cli/cmd/tui/component/prompt/index.tsx +1052 -0
- package/src/cli/cmd/tui/context/args.tsx +14 -0
- package/src/cli/cmd/tui/context/directory.ts +13 -0
- package/src/cli/cmd/tui/context/exit.tsx +23 -0
- package/src/cli/cmd/tui/context/helper.tsx +25 -0
- package/src/cli/cmd/tui/context/keybind.tsx +101 -0
- package/src/cli/cmd/tui/context/kv.tsx +49 -0
- package/src/cli/cmd/tui/context/local.tsx +339 -0
- package/src/cli/cmd/tui/context/prompt.tsx +18 -0
- package/src/cli/cmd/tui/context/route.tsx +46 -0
- package/src/cli/cmd/tui/context/sdk.tsx +74 -0
- package/src/cli/cmd/tui/context/sync.tsx +372 -0
- package/src/cli/cmd/tui/context/theme/aura.json +69 -0
- package/src/cli/cmd/tui/context/theme/ayu.json +80 -0
- package/src/cli/cmd/tui/context/theme/bincode.json +245 -0
- package/src/cli/cmd/tui/context/theme/catppuccin-frappe.json +233 -0
- package/src/cli/cmd/tui/context/theme/catppuccin-macchiato.json +233 -0
- package/src/cli/cmd/tui/context/theme/catppuccin.json +112 -0
- package/src/cli/cmd/tui/context/theme/cobalt2.json +228 -0
- package/src/cli/cmd/tui/context/theme/cursor.json +249 -0
- package/src/cli/cmd/tui/context/theme/dracula.json +219 -0
- package/src/cli/cmd/tui/context/theme/everforest.json +241 -0
- package/src/cli/cmd/tui/context/theme/flexoki.json +237 -0
- package/src/cli/cmd/tui/context/theme/github.json +233 -0
- package/src/cli/cmd/tui/context/theme/gruvbox.json +95 -0
- package/src/cli/cmd/tui/context/theme/kanagawa.json +77 -0
- package/src/cli/cmd/tui/context/theme/lucent-orng.json +227 -0
- package/src/cli/cmd/tui/context/theme/material.json +235 -0
- package/src/cli/cmd/tui/context/theme/matrix.json +77 -0
- package/src/cli/cmd/tui/context/theme/mercury.json +252 -0
- package/src/cli/cmd/tui/context/theme/monokai.json +221 -0
- package/src/cli/cmd/tui/context/theme/nightowl.json +221 -0
- package/src/cli/cmd/tui/context/theme/nord.json +223 -0
- package/src/cli/cmd/tui/context/theme/one-dark.json +84 -0
- package/src/cli/cmd/tui/context/theme/orng.json +245 -0
- package/src/cli/cmd/tui/context/theme/palenight.json +222 -0
- package/src/cli/cmd/tui/context/theme/rosepine.json +234 -0
- package/src/cli/cmd/tui/context/theme/solarized.json +223 -0
- package/src/cli/cmd/tui/context/theme/synthwave84.json +226 -0
- package/src/cli/cmd/tui/context/theme/tokyonight.json +243 -0
- package/src/cli/cmd/tui/context/theme/vercel.json +245 -0
- package/src/cli/cmd/tui/context/theme/vesper.json +218 -0
- package/src/cli/cmd/tui/context/theme/zenburn.json +223 -0
- package/src/cli/cmd/tui/context/theme.tsx +1109 -0
- package/src/cli/cmd/tui/event.ts +40 -0
- package/src/cli/cmd/tui/routes/home.tsx +105 -0
- package/src/cli/cmd/tui/routes/session/dialog-fork-from-timeline.tsx +64 -0
- package/src/cli/cmd/tui/routes/session/dialog-message.tsx +109 -0
- package/src/cli/cmd/tui/routes/session/dialog-subagent.tsx +26 -0
- package/src/cli/cmd/tui/routes/session/dialog-timeline.tsx +47 -0
- package/src/cli/cmd/tui/routes/session/footer.tsx +88 -0
- package/src/cli/cmd/tui/routes/session/header.tsx +141 -0
- package/src/cli/cmd/tui/routes/session/index.tsx +1888 -0
- package/src/cli/cmd/tui/routes/session/sidebar.tsx +321 -0
- package/src/cli/cmd/tui/spawn.ts +60 -0
- package/src/cli/cmd/tui/thread.ts +120 -0
- package/src/cli/cmd/tui/ui/dialog-alert.tsx +57 -0
- package/src/cli/cmd/tui/ui/dialog-confirm.tsx +83 -0
- package/src/cli/cmd/tui/ui/dialog-help.tsx +38 -0
- package/src/cli/cmd/tui/ui/dialog-prompt.tsx +77 -0
- package/src/cli/cmd/tui/ui/dialog-select.tsx +330 -0
- package/src/cli/cmd/tui/ui/dialog.tsx +170 -0
- package/src/cli/cmd/tui/ui/spinner.ts +368 -0
- package/src/cli/cmd/tui/ui/toast.tsx +100 -0
- package/src/cli/cmd/tui/util/clipboard.ts +127 -0
- package/src/cli/cmd/tui/util/editor.ts +32 -0
- package/src/cli/cmd/tui/util/terminal.ts +114 -0
- package/src/cli/cmd/tui/worker.ts +63 -0
- package/src/cli/cmd/uninstall.ts +344 -0
- package/src/cli/cmd/upgrade.ts +67 -0
- package/src/cli/cmd/web.ts +84 -0
- package/src/cli/error.ts +55 -0
- package/src/cli/ui.ts +84 -0
- package/src/cli/upgrade.ts +25 -0
- package/src/command/index.ts +80 -0
- package/src/command/template/initialize.txt +10 -0
- package/src/command/template/review.txt +97 -0
- package/src/config/config.ts +995 -0
- package/src/config/markdown.ts +41 -0
- package/src/env/index.ts +26 -0
- package/src/file/ignore.ts +83 -0
- package/src/file/index.ts +328 -0
- package/src/file/ripgrep.ts +393 -0
- package/src/file/time.ts +64 -0
- package/src/file/watcher.ts +103 -0
- package/src/flag/flag.ts +46 -0
- package/src/format/formatter.ts +315 -0
- package/src/format/index.ts +137 -0
- package/src/global/index.ts +52 -0
- package/src/id/id.ts +73 -0
- package/src/ide/index.ts +76 -0
- package/src/index.ts +217 -0
- package/src/installation/index.ts +196 -0
- package/src/lsp/client.ts +229 -0
- package/src/lsp/index.ts +485 -0
- package/src/lsp/language.ts +116 -0
- package/src/lsp/server.ts +1895 -0
- package/src/mcp/auth.ts +135 -0
- package/src/mcp/index.ts +654 -0
- package/src/mcp/oauth-callback.ts +200 -0
- package/src/mcp/oauth-provider.ts +154 -0
- package/src/patch/index.ts +622 -0
- package/src/permission/index.ts +199 -0
- package/src/plugin/index.ts +101 -0
- package/src/project/bootstrap.ts +31 -0
- package/src/project/instance.ts +78 -0
- package/src/project/project.ts +221 -0
- package/src/project/state.ts +65 -0
- package/src/project/vcs.ts +76 -0
- package/src/provider/auth.ts +143 -0
- package/src/provider/models-macro.ts +11 -0
- package/src/provider/models.ts +106 -0
- package/src/provider/provider.ts +1071 -0
- package/src/provider/sdk/openai-compatible/src/README.md +5 -0
- package/src/provider/sdk/openai-compatible/src/index.ts +2 -0
- package/src/provider/sdk/openai-compatible/src/openai-compatible-provider.ts +101 -0
- package/src/provider/sdk/openai-compatible/src/responses/convert-to-openai-responses-input.ts +303 -0
- package/src/provider/sdk/openai-compatible/src/responses/map-openai-responses-finish-reason.ts +22 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-config.ts +18 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-error.ts +22 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-api-types.ts +207 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-language-model.ts +1713 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-prepare-tools.ts +177 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-settings.ts +1 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/code-interpreter.ts +88 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/file-search.ts +128 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/image-generation.ts +115 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/local-shell.ts +65 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/web-search-preview.ts +104 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/web-search.ts +103 -0
- package/src/provider/transform.ts +455 -0
- package/src/pty/index.ts +231 -0
- package/src/server/error.ts +36 -0
- package/src/server/project.ts +79 -0
- package/src/server/server.ts +2642 -0
- package/src/server/tui.ts +71 -0
- package/src/session/compaction.ts +223 -0
- package/src/session/index.ts +458 -0
- package/src/session/llm.ts +201 -0
- package/src/session/message-v2.ts +659 -0
- package/src/session/message.ts +189 -0
- package/src/session/processor.ts +409 -0
- package/src/session/prompt/anthropic-20250930.txt +166 -0
- package/src/session/prompt/anthropic.txt +104 -0
- package/src/session/prompt/anthropic_spoof.txt +1 -0
- package/src/session/prompt/beast.txt +147 -0
- package/src/session/prompt/build-switch.txt +5 -0
- package/src/session/prompt/codex.txt +318 -0
- package/src/session/prompt/copilot-gpt-5.txt +143 -0
- package/src/session/prompt/gemini.txt +155 -0
- package/src/session/prompt/max-steps.txt +16 -0
- package/src/session/prompt/plan-reminder-anthropic.txt +67 -0
- package/src/session/prompt/plan.txt +26 -0
- package/src/session/prompt/polaris.txt +106 -0
- package/src/session/prompt/qwen.txt +109 -0
- package/src/session/prompt.ts +1446 -0
- package/src/session/retry.ts +86 -0
- package/src/session/revert.ts +108 -0
- package/src/session/status.ts +76 -0
- package/src/session/summary.ts +194 -0
- package/src/session/system.ts +120 -0
- package/src/session/todo.ts +37 -0
- package/src/share/share-next.ts +194 -0
- package/src/share/share.ts +87 -0
- package/src/shell/shell.ts +67 -0
- package/src/skill/index.ts +1 -0
- package/src/skill/skill.ts +83 -0
- package/src/snapshot/index.ts +197 -0
- package/src/storage/storage.ts +226 -0
- package/src/tool/bash.ts +306 -0
- package/src/tool/bash.txt +158 -0
- package/src/tool/batch.ts +175 -0
- package/src/tool/batch.txt +24 -0
- package/src/tool/codesearch.ts +138 -0
- package/src/tool/codesearch.txt +12 -0
- package/src/tool/edit.ts +675 -0
- package/src/tool/edit.txt +10 -0
- package/src/tool/glob.ts +65 -0
- package/src/tool/glob.txt +6 -0
- package/src/tool/grep.ts +121 -0
- package/src/tool/grep.txt +8 -0
- package/src/tool/invalid.ts +17 -0
- package/src/tool/ls.ts +110 -0
- package/src/tool/ls.txt +1 -0
- package/src/tool/lsp-diagnostics.ts +26 -0
- package/src/tool/lsp-diagnostics.txt +1 -0
- package/src/tool/lsp-hover.ts +31 -0
- package/src/tool/lsp-hover.txt +1 -0
- package/src/tool/lsp.ts +87 -0
- package/src/tool/lsp.txt +19 -0
- package/src/tool/multiedit.ts +46 -0
- package/src/tool/multiedit.txt +41 -0
- package/src/tool/patch.ts +233 -0
- package/src/tool/patch.txt +1 -0
- package/src/tool/read.ts +219 -0
- package/src/tool/read.txt +12 -0
- package/src/tool/registry.ts +162 -0
- package/src/tool/skill.ts +100 -0
- package/src/tool/task.ts +136 -0
- package/src/tool/task.txt +60 -0
- package/src/tool/todo.ts +39 -0
- package/src/tool/todoread.txt +14 -0
- package/src/tool/todowrite.txt +167 -0
- package/src/tool/tool.ts +71 -0
- package/src/tool/webfetch.ts +187 -0
- package/src/tool/webfetch.txt +13 -0
- package/src/tool/websearch.ts +150 -0
- package/src/tool/websearch.txt +11 -0
- package/src/tool/write.ts +110 -0
- package/src/tool/write.txt +8 -0
- package/src/util/archive.ts +16 -0
- package/src/util/color.ts +19 -0
- package/src/util/context.ts +25 -0
- package/src/util/defer.ts +12 -0
- package/src/util/eventloop.ts +20 -0
- package/src/util/filesystem.ts +83 -0
- package/src/util/fn.ts +11 -0
- package/src/util/iife.ts +3 -0
- package/src/util/keybind.ts +102 -0
- package/src/util/lazy.ts +11 -0
- package/src/util/locale.ts +81 -0
- package/src/util/lock.ts +98 -0
- package/src/util/log.ts +180 -0
- package/src/util/queue.ts +32 -0
- package/src/util/rpc.ts +42 -0
- package/src/util/scrap.ts +10 -0
- package/src/util/signal.ts +12 -0
- package/src/util/timeout.ts +14 -0
- package/src/util/token.ts +7 -0
- package/src/util/wildcard.ts +54 -0
- package/tsconfig.json +16 -0
package/src/pty/index.ts
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { BusEvent } from "@/bus/bus-event"
|
|
2
|
+
import { Bus } from "@/bus"
|
|
3
|
+
import { type IPty } from "bun-pty"
|
|
4
|
+
import z from "zod"
|
|
5
|
+
import { Identifier } from "../id/id"
|
|
6
|
+
import { Log } from "../util/log"
|
|
7
|
+
import type { WSContext } from "hono/ws"
|
|
8
|
+
import { Instance } from "../project/instance"
|
|
9
|
+
import { lazy } from "@bincode-ai/util/lazy"
|
|
10
|
+
import {} from "process"
|
|
11
|
+
import { Installation } from "@/installation"
|
|
12
|
+
import { Shell } from "@/shell/shell"
|
|
13
|
+
|
|
14
|
+
export namespace Pty {
|
|
15
|
+
const log = Log.create({ service: "pty" })
|
|
16
|
+
|
|
17
|
+
const pty = lazy(async () => {
|
|
18
|
+
if (!Installation.isLocal()) {
|
|
19
|
+
const path = require(
|
|
20
|
+
`bun-pty/rust-pty/target/release/${
|
|
21
|
+
process.platform === "win32"
|
|
22
|
+
? "rust_pty.dll"
|
|
23
|
+
: process.platform === "linux" && process.arch === "x64"
|
|
24
|
+
? "librust_pty.so"
|
|
25
|
+
: process.platform === "darwin" && process.arch === "x64"
|
|
26
|
+
? "librust_pty.dylib"
|
|
27
|
+
: process.platform === "darwin" && process.arch === "arm64"
|
|
28
|
+
? "librust_pty_arm64.dylib"
|
|
29
|
+
: process.platform === "linux" && process.arch === "arm64"
|
|
30
|
+
? "librust_pty_arm64.so"
|
|
31
|
+
: ""
|
|
32
|
+
}`,
|
|
33
|
+
)
|
|
34
|
+
process.env.BUN_PTY_LIB = path
|
|
35
|
+
}
|
|
36
|
+
const { spawn } = await import("bun-pty")
|
|
37
|
+
return spawn
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
export const Info = z
|
|
41
|
+
.object({
|
|
42
|
+
id: Identifier.schema("pty"),
|
|
43
|
+
title: z.string(),
|
|
44
|
+
command: z.string(),
|
|
45
|
+
args: z.array(z.string()),
|
|
46
|
+
cwd: z.string(),
|
|
47
|
+
status: z.enum(["running", "exited"]),
|
|
48
|
+
pid: z.number(),
|
|
49
|
+
})
|
|
50
|
+
.meta({ ref: "Pty" })
|
|
51
|
+
|
|
52
|
+
export type Info = z.infer<typeof Info>
|
|
53
|
+
|
|
54
|
+
export const CreateInput = z.object({
|
|
55
|
+
command: z.string().optional(),
|
|
56
|
+
args: z.array(z.string()).optional(),
|
|
57
|
+
cwd: z.string().optional(),
|
|
58
|
+
title: z.string().optional(),
|
|
59
|
+
env: z.record(z.string(), z.string()).optional(),
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
export type CreateInput = z.infer<typeof CreateInput>
|
|
63
|
+
|
|
64
|
+
export const UpdateInput = z.object({
|
|
65
|
+
title: z.string().optional(),
|
|
66
|
+
size: z
|
|
67
|
+
.object({
|
|
68
|
+
rows: z.number(),
|
|
69
|
+
cols: z.number(),
|
|
70
|
+
})
|
|
71
|
+
.optional(),
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
export type UpdateInput = z.infer<typeof UpdateInput>
|
|
75
|
+
|
|
76
|
+
export const Event = {
|
|
77
|
+
Created: BusEvent.define("pty.created", z.object({ info: Info })),
|
|
78
|
+
Updated: BusEvent.define("pty.updated", z.object({ info: Info })),
|
|
79
|
+
Exited: BusEvent.define("pty.exited", z.object({ id: Identifier.schema("pty"), exitCode: z.number() })),
|
|
80
|
+
Deleted: BusEvent.define("pty.deleted", z.object({ id: Identifier.schema("pty") })),
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
interface ActiveSession {
|
|
84
|
+
info: Info
|
|
85
|
+
process: IPty
|
|
86
|
+
buffer: string
|
|
87
|
+
subscribers: Set<WSContext>
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const state = Instance.state(
|
|
91
|
+
() => new Map<string, ActiveSession>(),
|
|
92
|
+
async (sessions) => {
|
|
93
|
+
for (const session of sessions.values()) {
|
|
94
|
+
try {
|
|
95
|
+
session.process.kill()
|
|
96
|
+
} catch {}
|
|
97
|
+
for (const ws of session.subscribers) {
|
|
98
|
+
ws.close()
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
sessions.clear()
|
|
102
|
+
},
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
export function list() {
|
|
106
|
+
return Array.from(state().values()).map((s) => s.info)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function get(id: string) {
|
|
110
|
+
return state().get(id)?.info
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export async function create(input: CreateInput) {
|
|
114
|
+
const id = Identifier.create("pty", false)
|
|
115
|
+
const command = input.command || Shell.preferred()
|
|
116
|
+
const args = input.args || []
|
|
117
|
+
if (command.endsWith("sh")) {
|
|
118
|
+
args.push("-l")
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const cwd = input.cwd || Instance.directory
|
|
122
|
+
const env = { ...process.env, ...input.env, TERM: "xterm-256color" } as Record<string, string>
|
|
123
|
+
log.info("creating session", { id, cmd: command, args, cwd })
|
|
124
|
+
|
|
125
|
+
const spawn = await pty()
|
|
126
|
+
const ptyProcess = spawn(command, args, {
|
|
127
|
+
name: "xterm-256color",
|
|
128
|
+
cwd,
|
|
129
|
+
env,
|
|
130
|
+
})
|
|
131
|
+
const info = {
|
|
132
|
+
id,
|
|
133
|
+
title: input.title || `Terminal ${id.slice(-4)}`,
|
|
134
|
+
command,
|
|
135
|
+
args,
|
|
136
|
+
cwd,
|
|
137
|
+
status: "running",
|
|
138
|
+
pid: ptyProcess.pid,
|
|
139
|
+
} as const
|
|
140
|
+
const session: ActiveSession = {
|
|
141
|
+
info,
|
|
142
|
+
process: ptyProcess,
|
|
143
|
+
buffer: "",
|
|
144
|
+
subscribers: new Set(),
|
|
145
|
+
}
|
|
146
|
+
state().set(id, session)
|
|
147
|
+
ptyProcess.onData((data) => {
|
|
148
|
+
if (session.subscribers.size === 0) {
|
|
149
|
+
session.buffer += data
|
|
150
|
+
return
|
|
151
|
+
}
|
|
152
|
+
for (const ws of session.subscribers) {
|
|
153
|
+
if (ws.readyState === 1) {
|
|
154
|
+
ws.send(data)
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
})
|
|
158
|
+
ptyProcess.onExit(({ exitCode }) => {
|
|
159
|
+
log.info("session exited", { id, exitCode })
|
|
160
|
+
session.info.status = "exited"
|
|
161
|
+
Bus.publish(Event.Exited, { id, exitCode })
|
|
162
|
+
state().delete(id)
|
|
163
|
+
})
|
|
164
|
+
Bus.publish(Event.Created, { info })
|
|
165
|
+
return info
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export async function update(id: string, input: UpdateInput) {
|
|
169
|
+
const session = state().get(id)
|
|
170
|
+
if (!session) return
|
|
171
|
+
if (input.title) {
|
|
172
|
+
session.info.title = input.title
|
|
173
|
+
}
|
|
174
|
+
if (input.size) {
|
|
175
|
+
session.process.resize(input.size.cols, input.size.rows)
|
|
176
|
+
}
|
|
177
|
+
Bus.publish(Event.Updated, { info: session.info })
|
|
178
|
+
return session.info
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export async function remove(id: string) {
|
|
182
|
+
const session = state().get(id)
|
|
183
|
+
if (!session) return
|
|
184
|
+
log.info("removing session", { id })
|
|
185
|
+
try {
|
|
186
|
+
session.process.kill()
|
|
187
|
+
} catch {}
|
|
188
|
+
for (const ws of session.subscribers) {
|
|
189
|
+
ws.close()
|
|
190
|
+
}
|
|
191
|
+
state().delete(id)
|
|
192
|
+
Bus.publish(Event.Deleted, { id })
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export function resize(id: string, cols: number, rows: number) {
|
|
196
|
+
const session = state().get(id)
|
|
197
|
+
if (session && session.info.status === "running") {
|
|
198
|
+
session.process.resize(cols, rows)
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export function write(id: string, data: string) {
|
|
203
|
+
const session = state().get(id)
|
|
204
|
+
if (session && session.info.status === "running") {
|
|
205
|
+
session.process.write(data)
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export function connect(id: string, ws: WSContext) {
|
|
210
|
+
const session = state().get(id)
|
|
211
|
+
if (!session) {
|
|
212
|
+
ws.close()
|
|
213
|
+
return
|
|
214
|
+
}
|
|
215
|
+
log.info("client connected to session", { id })
|
|
216
|
+
session.subscribers.add(ws)
|
|
217
|
+
if (session.buffer) {
|
|
218
|
+
ws.send(session.buffer)
|
|
219
|
+
session.buffer = ""
|
|
220
|
+
}
|
|
221
|
+
return {
|
|
222
|
+
onMessage: (message: string | ArrayBuffer) => {
|
|
223
|
+
session.process.write(String(message))
|
|
224
|
+
},
|
|
225
|
+
onClose: () => {
|
|
226
|
+
log.info("client disconnected from session", { id })
|
|
227
|
+
session.subscribers.delete(ws)
|
|
228
|
+
},
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { resolver } from "hono-openapi"
|
|
2
|
+
import z from "zod"
|
|
3
|
+
import { Storage } from "../storage/storage"
|
|
4
|
+
|
|
5
|
+
export const ERRORS = {
|
|
6
|
+
400: {
|
|
7
|
+
description: "Bad request",
|
|
8
|
+
content: {
|
|
9
|
+
"application/json": {
|
|
10
|
+
schema: resolver(
|
|
11
|
+
z
|
|
12
|
+
.object({
|
|
13
|
+
data: z.any(),
|
|
14
|
+
errors: z.array(z.record(z.string(), z.any())),
|
|
15
|
+
success: z.literal(false),
|
|
16
|
+
})
|
|
17
|
+
.meta({
|
|
18
|
+
ref: "BadRequestError",
|
|
19
|
+
}),
|
|
20
|
+
),
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
404: {
|
|
25
|
+
description: "Not found",
|
|
26
|
+
content: {
|
|
27
|
+
"application/json": {
|
|
28
|
+
schema: resolver(Storage.NotFoundError.Schema),
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
} as const
|
|
33
|
+
|
|
34
|
+
export function errors(...codes: number[]) {
|
|
35
|
+
return Object.fromEntries(codes.map((code) => [code, ERRORS[code as keyof typeof ERRORS]]))
|
|
36
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Hono } from "hono"
|
|
2
|
+
import { describeRoute, validator } from "hono-openapi"
|
|
3
|
+
import { resolver } from "hono-openapi"
|
|
4
|
+
import { Instance } from "../project/instance"
|
|
5
|
+
import { Project } from "../project/project"
|
|
6
|
+
import z from "zod"
|
|
7
|
+
import { errors } from "./error"
|
|
8
|
+
|
|
9
|
+
export const ProjectRoute = new Hono()
|
|
10
|
+
.get(
|
|
11
|
+
"/",
|
|
12
|
+
describeRoute({
|
|
13
|
+
summary: "List all projects",
|
|
14
|
+
description: "Get a list of projects that have been opened with Bincode.",
|
|
15
|
+
operationId: "project.list",
|
|
16
|
+
responses: {
|
|
17
|
+
200: {
|
|
18
|
+
description: "List of projects",
|
|
19
|
+
content: {
|
|
20
|
+
"application/json": {
|
|
21
|
+
schema: resolver(Project.Info.array()),
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
}),
|
|
27
|
+
async (c) => {
|
|
28
|
+
const projects = await Project.list()
|
|
29
|
+
return c.json(projects)
|
|
30
|
+
},
|
|
31
|
+
)
|
|
32
|
+
.get(
|
|
33
|
+
"/current",
|
|
34
|
+
describeRoute({
|
|
35
|
+
summary: "Get current project",
|
|
36
|
+
description: "Retrieve the currently active project that Bincode is working with.",
|
|
37
|
+
operationId: "project.current",
|
|
38
|
+
responses: {
|
|
39
|
+
200: {
|
|
40
|
+
description: "Current project information",
|
|
41
|
+
content: {
|
|
42
|
+
"application/json": {
|
|
43
|
+
schema: resolver(Project.Info),
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
}),
|
|
49
|
+
async (c) => {
|
|
50
|
+
return c.json(Instance.project)
|
|
51
|
+
},
|
|
52
|
+
)
|
|
53
|
+
.patch(
|
|
54
|
+
"/:projectID",
|
|
55
|
+
describeRoute({
|
|
56
|
+
summary: "Update project",
|
|
57
|
+
description: "Update project properties such as name, icon and color.",
|
|
58
|
+
operationId: "project.update",
|
|
59
|
+
responses: {
|
|
60
|
+
200: {
|
|
61
|
+
description: "Updated project information",
|
|
62
|
+
content: {
|
|
63
|
+
"application/json": {
|
|
64
|
+
schema: resolver(Project.Info),
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
...errors(400, 404),
|
|
69
|
+
},
|
|
70
|
+
}),
|
|
71
|
+
validator("param", z.object({ projectID: z.string() })),
|
|
72
|
+
validator("json", Project.update.schema.omit({ projectID: true })),
|
|
73
|
+
async (c) => {
|
|
74
|
+
const projectID = c.req.valid("param").projectID
|
|
75
|
+
const body = c.req.valid("json")
|
|
76
|
+
const project = await Project.update({ ...body, projectID })
|
|
77
|
+
return c.json(project)
|
|
78
|
+
},
|
|
79
|
+
)
|