@link-assistant/agent 0.0.8
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/EXAMPLES.md +383 -0
- package/LICENSE +24 -0
- package/MODELS.md +95 -0
- package/README.md +388 -0
- package/TOOLS.md +134 -0
- package/package.json +89 -0
- package/src/agent/agent.ts +150 -0
- package/src/agent/generate.txt +75 -0
- package/src/auth/index.ts +64 -0
- package/src/bun/index.ts +96 -0
- package/src/bus/global.ts +10 -0
- package/src/bus/index.ts +119 -0
- package/src/cli/bootstrap.js +41 -0
- package/src/cli/bootstrap.ts +17 -0
- package/src/cli/cmd/agent.ts +165 -0
- package/src/cli/cmd/cmd.ts +5 -0
- package/src/cli/cmd/export.ts +88 -0
- package/src/cli/cmd/mcp.ts +80 -0
- package/src/cli/cmd/models.ts +58 -0
- package/src/cli/cmd/run.ts +359 -0
- package/src/cli/cmd/stats.ts +276 -0
- package/src/cli/error.ts +27 -0
- package/src/command/index.ts +73 -0
- package/src/command/template/initialize.txt +10 -0
- package/src/config/config.ts +705 -0
- package/src/config/markdown.ts +41 -0
- package/src/file/ripgrep.ts +391 -0
- package/src/file/time.ts +38 -0
- package/src/file/watcher.ts +75 -0
- package/src/file.ts +6 -0
- package/src/flag/flag.ts +19 -0
- package/src/format/formatter.ts +248 -0
- package/src/format/index.ts +137 -0
- package/src/global/index.ts +52 -0
- package/src/id/id.ts +72 -0
- package/src/index.js +371 -0
- package/src/mcp/index.ts +289 -0
- package/src/patch/index.ts +622 -0
- package/src/project/bootstrap.ts +22 -0
- package/src/project/instance.ts +67 -0
- package/src/project/project.ts +105 -0
- package/src/project/state.ts +65 -0
- package/src/provider/models-macro.ts +11 -0
- package/src/provider/models.ts +98 -0
- package/src/provider/opencode.js +47 -0
- package/src/provider/provider.ts +636 -0
- package/src/provider/transform.ts +241 -0
- package/src/server/project.ts +48 -0
- package/src/server/server.ts +249 -0
- package/src/session/agent.js +204 -0
- package/src/session/compaction.ts +249 -0
- package/src/session/index.ts +380 -0
- package/src/session/message-v2.ts +758 -0
- package/src/session/message.ts +189 -0
- package/src/session/processor.ts +356 -0
- package/src/session/prompt/anthropic-20250930.txt +166 -0
- package/src/session/prompt/anthropic.txt +105 -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/grok-code.txt +1 -0
- package/src/session/prompt/plan.txt +8 -0
- package/src/session/prompt/polaris.txt +107 -0
- package/src/session/prompt/qwen.txt +109 -0
- package/src/session/prompt/summarize-turn.txt +5 -0
- package/src/session/prompt/summarize.txt +10 -0
- package/src/session/prompt/title.txt +25 -0
- package/src/session/prompt.ts +1390 -0
- package/src/session/retry.ts +53 -0
- package/src/session/revert.ts +108 -0
- package/src/session/status.ts +75 -0
- package/src/session/summary.ts +179 -0
- package/src/session/system.ts +138 -0
- package/src/session/todo.ts +36 -0
- package/src/snapshot/index.ts +197 -0
- package/src/storage/storage.ts +226 -0
- package/src/tool/bash.ts +193 -0
- package/src/tool/bash.txt +121 -0
- package/src/tool/batch.ts +173 -0
- package/src/tool/batch.txt +28 -0
- package/src/tool/codesearch.ts +123 -0
- package/src/tool/codesearch.txt +12 -0
- package/src/tool/edit.ts +604 -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 +116 -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/multiedit.ts +46 -0
- package/src/tool/multiedit.txt +41 -0
- package/src/tool/patch.ts +188 -0
- package/src/tool/patch.txt +1 -0
- package/src/tool/read.ts +201 -0
- package/src/tool/read.txt +12 -0
- package/src/tool/registry.ts +87 -0
- package/src/tool/task.ts +126 -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 +66 -0
- package/src/tool/webfetch.ts +171 -0
- package/src/tool/webfetch.txt +14 -0
- package/src/tool/websearch.ts +133 -0
- package/src/tool/websearch.txt +11 -0
- package/src/tool/write.ts +33 -0
- package/src/tool/write.txt +8 -0
- package/src/util/binary.ts +41 -0
- package/src/util/context.ts +25 -0
- package/src/util/defer.ts +12 -0
- package/src/util/error.ts +54 -0
- package/src/util/eventloop.ts +20 -0
- package/src/util/filesystem.ts +69 -0
- package/src/util/fn.ts +11 -0
- package/src/util/iife.ts +3 -0
- package/src/util/keybind.ts +79 -0
- package/src/util/lazy.ts +11 -0
- package/src/util/locale.ts +39 -0
- package/src/util/lock.ts +98 -0
- package/src/util/log.ts +177 -0
- package/src/util/queue.ts +19 -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
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { readableStreamToText } from "bun"
|
|
2
|
+
import { BunProc } from "../bun"
|
|
3
|
+
import { Instance } from "../project/instance"
|
|
4
|
+
import { Filesystem } from "../util/filesystem"
|
|
5
|
+
|
|
6
|
+
export interface Info {
|
|
7
|
+
name: string
|
|
8
|
+
command: string[]
|
|
9
|
+
environment?: Record<string, string>
|
|
10
|
+
extensions: string[]
|
|
11
|
+
enabled(): Promise<boolean>
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const gofmt: Info = {
|
|
15
|
+
name: "gofmt",
|
|
16
|
+
command: ["gofmt", "-w", "$FILE"],
|
|
17
|
+
extensions: [".go"],
|
|
18
|
+
async enabled() {
|
|
19
|
+
return Bun.which("gofmt") !== null
|
|
20
|
+
},
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const mix: Info = {
|
|
24
|
+
name: "mix",
|
|
25
|
+
command: ["mix", "format", "$FILE"],
|
|
26
|
+
extensions: [".ex", ".exs", ".eex", ".heex", ".leex", ".neex", ".sface"],
|
|
27
|
+
async enabled() {
|
|
28
|
+
return Bun.which("mix") !== null
|
|
29
|
+
},
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const prettier: Info = {
|
|
33
|
+
name: "prettier",
|
|
34
|
+
command: [BunProc.which(), "x", "prettier", "--write", "$FILE"],
|
|
35
|
+
environment: {
|
|
36
|
+
BUN_BE_BUN: "1",
|
|
37
|
+
},
|
|
38
|
+
extensions: [
|
|
39
|
+
".js",
|
|
40
|
+
".jsx",
|
|
41
|
+
".mjs",
|
|
42
|
+
".cjs",
|
|
43
|
+
".ts",
|
|
44
|
+
".tsx",
|
|
45
|
+
".mts",
|
|
46
|
+
".cts",
|
|
47
|
+
".html",
|
|
48
|
+
".htm",
|
|
49
|
+
".css",
|
|
50
|
+
".scss",
|
|
51
|
+
".sass",
|
|
52
|
+
".less",
|
|
53
|
+
".vue",
|
|
54
|
+
".svelte",
|
|
55
|
+
".json",
|
|
56
|
+
".jsonc",
|
|
57
|
+
".yaml",
|
|
58
|
+
".yml",
|
|
59
|
+
".toml",
|
|
60
|
+
".xml",
|
|
61
|
+
".md",
|
|
62
|
+
".mdx",
|
|
63
|
+
".graphql",
|
|
64
|
+
".gql",
|
|
65
|
+
],
|
|
66
|
+
async enabled() {
|
|
67
|
+
const items = await Filesystem.findUp("package.json", Instance.directory, Instance.worktree)
|
|
68
|
+
for (const item of items) {
|
|
69
|
+
const json = await Bun.file(item).json()
|
|
70
|
+
if (json.dependencies?.prettier) return true
|
|
71
|
+
if (json.devDependencies?.prettier) return true
|
|
72
|
+
}
|
|
73
|
+
return false
|
|
74
|
+
},
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export const biome: Info = {
|
|
78
|
+
name: "biome",
|
|
79
|
+
command: [BunProc.which(), "x", "@biomejs/biome", "format", "--write", "$FILE"],
|
|
80
|
+
environment: {
|
|
81
|
+
BUN_BE_BUN: "1",
|
|
82
|
+
},
|
|
83
|
+
extensions: [
|
|
84
|
+
".js",
|
|
85
|
+
".jsx",
|
|
86
|
+
".mjs",
|
|
87
|
+
".cjs",
|
|
88
|
+
".ts",
|
|
89
|
+
".tsx",
|
|
90
|
+
".mts",
|
|
91
|
+
".cts",
|
|
92
|
+
".html",
|
|
93
|
+
".htm",
|
|
94
|
+
".css",
|
|
95
|
+
".scss",
|
|
96
|
+
".sass",
|
|
97
|
+
".less",
|
|
98
|
+
".vue",
|
|
99
|
+
".svelte",
|
|
100
|
+
".json",
|
|
101
|
+
".jsonc",
|
|
102
|
+
".yaml",
|
|
103
|
+
".yml",
|
|
104
|
+
".toml",
|
|
105
|
+
".xml",
|
|
106
|
+
".md",
|
|
107
|
+
".mdx",
|
|
108
|
+
".graphql",
|
|
109
|
+
".gql",
|
|
110
|
+
],
|
|
111
|
+
async enabled() {
|
|
112
|
+
const configs = ["biome.json", "biome.jsonc"]
|
|
113
|
+
for (const config of configs) {
|
|
114
|
+
const found = await Filesystem.findUp(config, Instance.directory, Instance.worktree)
|
|
115
|
+
if (found.length > 0) {
|
|
116
|
+
return true
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return false
|
|
120
|
+
},
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export const zig: Info = {
|
|
124
|
+
name: "zig",
|
|
125
|
+
command: ["zig", "fmt", "$FILE"],
|
|
126
|
+
extensions: [".zig", ".zon"],
|
|
127
|
+
async enabled() {
|
|
128
|
+
return Bun.which("zig") !== null
|
|
129
|
+
},
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export const clang: Info = {
|
|
133
|
+
name: "clang-format",
|
|
134
|
+
command: ["clang-format", "-i", "$FILE"],
|
|
135
|
+
extensions: [".c", ".cc", ".cpp", ".cxx", ".c++", ".h", ".hh", ".hpp", ".hxx", ".h++", ".ino", ".C", ".H"],
|
|
136
|
+
async enabled() {
|
|
137
|
+
const items = await Filesystem.findUp(".clang-format", Instance.directory, Instance.worktree)
|
|
138
|
+
return items.length > 0
|
|
139
|
+
},
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export const ktlint: Info = {
|
|
143
|
+
name: "ktlint",
|
|
144
|
+
command: ["ktlint", "-F", "$FILE"],
|
|
145
|
+
extensions: [".kt", ".kts"],
|
|
146
|
+
async enabled() {
|
|
147
|
+
return Bun.which("ktlint") !== null
|
|
148
|
+
},
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export const ruff: Info = {
|
|
152
|
+
name: "ruff",
|
|
153
|
+
command: ["ruff", "format", "$FILE"],
|
|
154
|
+
extensions: [".py", ".pyi"],
|
|
155
|
+
async enabled() {
|
|
156
|
+
if (!Bun.which("ruff")) return false
|
|
157
|
+
const configs = ["pyproject.toml", "ruff.toml", ".ruff.toml"]
|
|
158
|
+
for (const config of configs) {
|
|
159
|
+
const found = await Filesystem.findUp(config, Instance.directory, Instance.worktree)
|
|
160
|
+
if (found.length > 0) {
|
|
161
|
+
if (config === "pyproject.toml") {
|
|
162
|
+
const content = await Bun.file(found[0]).text()
|
|
163
|
+
if (content.includes("[tool.ruff]")) return true
|
|
164
|
+
} else {
|
|
165
|
+
return true
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
const deps = ["requirements.txt", "pyproject.toml", "Pipfile"]
|
|
170
|
+
for (const dep of deps) {
|
|
171
|
+
const found = await Filesystem.findUp(dep, Instance.directory, Instance.worktree)
|
|
172
|
+
if (found.length > 0) {
|
|
173
|
+
const content = await Bun.file(found[0]).text()
|
|
174
|
+
if (content.includes("ruff")) return true
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return false
|
|
178
|
+
},
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export const rlang: Info = {
|
|
182
|
+
name: "air",
|
|
183
|
+
command: ["air", "format", "$FILE"],
|
|
184
|
+
extensions: [".R"],
|
|
185
|
+
async enabled() {
|
|
186
|
+
const airPath = Bun.which("air")
|
|
187
|
+
if (airPath == null) return false
|
|
188
|
+
|
|
189
|
+
try {
|
|
190
|
+
const proc = Bun.spawn(["air", "--help"], {
|
|
191
|
+
stdout: "pipe",
|
|
192
|
+
stderr: "pipe",
|
|
193
|
+
})
|
|
194
|
+
await proc.exited
|
|
195
|
+
const output = await readableStreamToText(proc.stdout)
|
|
196
|
+
|
|
197
|
+
// Check for "Air: An R language server and formatter"
|
|
198
|
+
const firstLine = output.split("\n")[0]
|
|
199
|
+
const hasR = firstLine.includes("R language")
|
|
200
|
+
const hasFormatter = firstLine.includes("formatter")
|
|
201
|
+
return hasR && hasFormatter
|
|
202
|
+
} catch (error) {
|
|
203
|
+
return false
|
|
204
|
+
}
|
|
205
|
+
},
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export const uvformat: Info = {
|
|
209
|
+
name: "uv format",
|
|
210
|
+
command: ["uv", "format", "--", "$FILE"],
|
|
211
|
+
extensions: [".py", ".pyi"],
|
|
212
|
+
async enabled() {
|
|
213
|
+
if (await ruff.enabled()) return false
|
|
214
|
+
if (Bun.which("uv") !== null) {
|
|
215
|
+
const proc = Bun.spawn(["uv", "format", "--help"], { stderr: "pipe", stdout: "pipe" })
|
|
216
|
+
const code = await proc.exited
|
|
217
|
+
return code === 0
|
|
218
|
+
}
|
|
219
|
+
return false
|
|
220
|
+
},
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export const rubocop: Info = {
|
|
224
|
+
name: "rubocop",
|
|
225
|
+
command: ["rubocop", "--autocorrect", "$FILE"],
|
|
226
|
+
extensions: [".rb", ".rake", ".gemspec", ".ru"],
|
|
227
|
+
async enabled() {
|
|
228
|
+
return Bun.which("rubocop") !== null
|
|
229
|
+
},
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export const standardrb: Info = {
|
|
233
|
+
name: "standardrb",
|
|
234
|
+
command: ["standardrb", "--fix", "$FILE"],
|
|
235
|
+
extensions: [".rb", ".rake", ".gemspec", ".ru"],
|
|
236
|
+
async enabled() {
|
|
237
|
+
return Bun.which("standardrb") !== null
|
|
238
|
+
},
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export const htmlbeautifier: Info = {
|
|
242
|
+
name: "htmlbeautifier",
|
|
243
|
+
command: ["htmlbeautifier", "$FILE"],
|
|
244
|
+
extensions: [".erb", ".html.erb"],
|
|
245
|
+
async enabled() {
|
|
246
|
+
return Bun.which("htmlbeautifier") !== null
|
|
247
|
+
},
|
|
248
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { Bus } from "../bus"
|
|
2
|
+
import { File } from "../file"
|
|
3
|
+
import { Log } from "../util/log"
|
|
4
|
+
import path from "path"
|
|
5
|
+
import z from "zod"
|
|
6
|
+
|
|
7
|
+
import * as Formatter from "./formatter"
|
|
8
|
+
import { Config } from "../config/config"
|
|
9
|
+
import { mergeDeep } from "remeda"
|
|
10
|
+
import { Instance } from "../project/instance"
|
|
11
|
+
|
|
12
|
+
export namespace Format {
|
|
13
|
+
const log = Log.create({ service: "format" })
|
|
14
|
+
|
|
15
|
+
export const Status = z
|
|
16
|
+
.object({
|
|
17
|
+
name: z.string(),
|
|
18
|
+
extensions: z.string().array(),
|
|
19
|
+
enabled: z.boolean(),
|
|
20
|
+
})
|
|
21
|
+
.meta({
|
|
22
|
+
ref: "FormatterStatus",
|
|
23
|
+
})
|
|
24
|
+
export type Status = z.infer<typeof Status>
|
|
25
|
+
|
|
26
|
+
const state = Instance.state(async () => {
|
|
27
|
+
const enabled: Record<string, boolean> = {}
|
|
28
|
+
const cfg = await Config.get()
|
|
29
|
+
|
|
30
|
+
const formatters: Record<string, Formatter.Info> = {}
|
|
31
|
+
if (cfg.formatter === false) {
|
|
32
|
+
log.info("all formatters are disabled")
|
|
33
|
+
return {
|
|
34
|
+
enabled,
|
|
35
|
+
formatters,
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
for (const item of Object.values(Formatter)) {
|
|
40
|
+
formatters[item.name] = item
|
|
41
|
+
}
|
|
42
|
+
for (const [name, item] of Object.entries(cfg.formatter ?? {})) {
|
|
43
|
+
if (item.disabled) {
|
|
44
|
+
delete formatters[name]
|
|
45
|
+
continue
|
|
46
|
+
}
|
|
47
|
+
const result: Formatter.Info = mergeDeep(formatters[name] ?? {}, {
|
|
48
|
+
command: [],
|
|
49
|
+
extensions: [],
|
|
50
|
+
...item,
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
if (result.command.length === 0) continue
|
|
54
|
+
|
|
55
|
+
result.enabled = async () => true
|
|
56
|
+
result.name = name
|
|
57
|
+
formatters[name] = result
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
enabled,
|
|
62
|
+
formatters,
|
|
63
|
+
}
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
async function isEnabled(item: Formatter.Info) {
|
|
67
|
+
const s = await state()
|
|
68
|
+
let status = s.enabled[item.name]
|
|
69
|
+
if (status === undefined) {
|
|
70
|
+
status = await item.enabled()
|
|
71
|
+
s.enabled[item.name] = status
|
|
72
|
+
}
|
|
73
|
+
return status
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function getFormatter(ext: string) {
|
|
77
|
+
const formatters = await state().then((x) => x.formatters)
|
|
78
|
+
const result = []
|
|
79
|
+
for (const item of Object.values(formatters)) {
|
|
80
|
+
log.info("checking", { name: item.name, ext })
|
|
81
|
+
if (!item.extensions.includes(ext)) continue
|
|
82
|
+
if (!(await isEnabled(item))) continue
|
|
83
|
+
log.info("enabled", { name: item.name, ext })
|
|
84
|
+
result.push(item)
|
|
85
|
+
}
|
|
86
|
+
return result
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export async function status() {
|
|
90
|
+
const s = await state()
|
|
91
|
+
const result: Status[] = []
|
|
92
|
+
for (const formatter of Object.values(s.formatters)) {
|
|
93
|
+
const enabled = await isEnabled(formatter)
|
|
94
|
+
result.push({
|
|
95
|
+
name: formatter.name,
|
|
96
|
+
extensions: formatter.extensions,
|
|
97
|
+
enabled,
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
return result
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function init() {
|
|
104
|
+
log.info("init")
|
|
105
|
+
Bus.subscribe(File.Event.Edited, async (payload) => {
|
|
106
|
+
const file = payload.properties.file
|
|
107
|
+
log.info("formatting", { file })
|
|
108
|
+
const ext = path.extname(file)
|
|
109
|
+
|
|
110
|
+
for (const item of await getFormatter(ext)) {
|
|
111
|
+
log.info("running", { command: item.command })
|
|
112
|
+
try {
|
|
113
|
+
const proc = Bun.spawn({
|
|
114
|
+
cmd: item.command.map((x) => x.replace("$FILE", file)),
|
|
115
|
+
cwd: Instance.directory,
|
|
116
|
+
env: { ...process.env, ...item.environment },
|
|
117
|
+
stdout: "ignore",
|
|
118
|
+
stderr: "ignore",
|
|
119
|
+
})
|
|
120
|
+
const exit = await proc.exited
|
|
121
|
+
if (exit !== 0)
|
|
122
|
+
log.error("failed", {
|
|
123
|
+
command: item.command,
|
|
124
|
+
...item.environment,
|
|
125
|
+
})
|
|
126
|
+
} catch (error) {
|
|
127
|
+
log.error("failed to format file", {
|
|
128
|
+
error,
|
|
129
|
+
command: item.command,
|
|
130
|
+
...item.environment,
|
|
131
|
+
file,
|
|
132
|
+
})
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import fs from "fs/promises"
|
|
2
|
+
import { xdgData, xdgCache, xdgConfig, xdgState } from "xdg-basedir"
|
|
3
|
+
import path from "path"
|
|
4
|
+
import os from "os"
|
|
5
|
+
|
|
6
|
+
const app = "opencode"
|
|
7
|
+
|
|
8
|
+
const data = path.join(xdgData!, app)
|
|
9
|
+
const cache = path.join(xdgCache!, app)
|
|
10
|
+
const config = path.join(xdgConfig!, app)
|
|
11
|
+
const state = path.join(xdgState!, app)
|
|
12
|
+
|
|
13
|
+
export namespace Global {
|
|
14
|
+
export const Path = {
|
|
15
|
+
home: os.homedir(),
|
|
16
|
+
data,
|
|
17
|
+
bin: path.join(data, "bin"),
|
|
18
|
+
log: path.join(data, "log"),
|
|
19
|
+
cache,
|
|
20
|
+
config,
|
|
21
|
+
state,
|
|
22
|
+
} as const
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
await Promise.all([
|
|
26
|
+
fs.mkdir(Global.Path.data, { recursive: true }),
|
|
27
|
+
fs.mkdir(Global.Path.config, { recursive: true }),
|
|
28
|
+
fs.mkdir(Global.Path.state, { recursive: true }),
|
|
29
|
+
fs.mkdir(Global.Path.log, { recursive: true }),
|
|
30
|
+
fs.mkdir(Global.Path.bin, { recursive: true }),
|
|
31
|
+
])
|
|
32
|
+
|
|
33
|
+
const CACHE_VERSION = "9"
|
|
34
|
+
|
|
35
|
+
const version = await Bun.file(path.join(Global.Path.cache, "version"))
|
|
36
|
+
.text()
|
|
37
|
+
.catch(() => "0")
|
|
38
|
+
|
|
39
|
+
if (version !== CACHE_VERSION) {
|
|
40
|
+
try {
|
|
41
|
+
const contents = await fs.readdir(Global.Path.cache)
|
|
42
|
+
await Promise.all(
|
|
43
|
+
contents.map((item) =>
|
|
44
|
+
fs.rm(path.join(Global.Path.cache, item), {
|
|
45
|
+
recursive: true,
|
|
46
|
+
force: true,
|
|
47
|
+
}),
|
|
48
|
+
),
|
|
49
|
+
)
|
|
50
|
+
} catch (e) {}
|
|
51
|
+
await Bun.file(path.join(Global.Path.cache, "version")).write(CACHE_VERSION)
|
|
52
|
+
}
|
package/src/id/id.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import z from "zod"
|
|
2
|
+
import { randomBytes } from "crypto"
|
|
3
|
+
|
|
4
|
+
export namespace Identifier {
|
|
5
|
+
const prefixes = {
|
|
6
|
+
session: "ses",
|
|
7
|
+
message: "msg",
|
|
8
|
+
permission: "per",
|
|
9
|
+
user: "usr",
|
|
10
|
+
part: "prt",
|
|
11
|
+
} as const
|
|
12
|
+
|
|
13
|
+
export function schema(prefix: keyof typeof prefixes) {
|
|
14
|
+
return z.string().startsWith(prefixes[prefix])
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const LENGTH = 26
|
|
18
|
+
|
|
19
|
+
// State for monotonic ID generation
|
|
20
|
+
let lastTimestamp = 0
|
|
21
|
+
let counter = 0
|
|
22
|
+
|
|
23
|
+
export function ascending(prefix: keyof typeof prefixes, given?: string) {
|
|
24
|
+
return generateID(prefix, false, given)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function descending(prefix: keyof typeof prefixes, given?: string) {
|
|
28
|
+
return generateID(prefix, true, given)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function generateID(prefix: keyof typeof prefixes, descending: boolean, given?: string): string {
|
|
32
|
+
if (!given) {
|
|
33
|
+
return create(prefix, descending)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!given.startsWith(prefixes[prefix])) {
|
|
37
|
+
throw new Error(`ID ${given} does not start with ${prefixes[prefix]}`)
|
|
38
|
+
}
|
|
39
|
+
return given
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function randomBase62(length: number): string {
|
|
43
|
+
const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
44
|
+
let result = ""
|
|
45
|
+
const bytes = randomBytes(length)
|
|
46
|
+
for (let i = 0; i < length; i++) {
|
|
47
|
+
result += chars[bytes[i] % 62]
|
|
48
|
+
}
|
|
49
|
+
return result
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function create(prefix: keyof typeof prefixes, descending: boolean, timestamp?: number): string {
|
|
53
|
+
const currentTimestamp = timestamp ?? Date.now()
|
|
54
|
+
|
|
55
|
+
if (currentTimestamp !== lastTimestamp) {
|
|
56
|
+
lastTimestamp = currentTimestamp
|
|
57
|
+
counter = 0
|
|
58
|
+
}
|
|
59
|
+
counter++
|
|
60
|
+
|
|
61
|
+
let now = BigInt(currentTimestamp) * BigInt(0x1000) + BigInt(counter)
|
|
62
|
+
|
|
63
|
+
now = descending ? ~now : now
|
|
64
|
+
|
|
65
|
+
const timeBytes = Buffer.alloc(6)
|
|
66
|
+
for (let i = 0; i < 6; i++) {
|
|
67
|
+
timeBytes[i] = Number((now >> BigInt(40 - 8 * i)) & BigInt(0xff))
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return prefixes[prefix] + "_" + timeBytes.toString("hex") + randomBase62(LENGTH - 12)
|
|
71
|
+
}
|
|
72
|
+
}
|