Skip to content

Commit d57b963

Browse files
committed
fix: id
1 parent 0ebcaff commit d57b963

File tree

6 files changed

+188
-84
lines changed

6 files changed

+188
-84
lines changed

bun.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/desktop/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"solid-js": "catalog:",
5757
"solid-list": "catalog:",
5858
"tailwindcss": "catalog:",
59-
"virtua": "catalog:"
59+
"virtua": "catalog:",
60+
"zod": "catalog:"
6061
}
6162
}

packages/desktop/src/components/prompt-input.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { DialogSelectModelUnpaid } from "@/components/dialog-select-model-unpaid
2121
import { useProviders } from "@/hooks/use-providers"
2222
import { useCommand, formatKeybind } from "@/context/command"
2323
import { persisted } from "@/utils/persist"
24-
import { Identifier } from "@opencode-ai/util/identifier"
24+
import { Identifier } from "@/utils/id"
2525

2626
const ACCEPTED_IMAGE_TYPES = ["image/png", "image/jpeg", "image/gif", "image/webp"]
2727
const ACCEPTED_FILE_TYPES = [...ACCEPTED_IMAGE_TYPES, "application/pdf"]

packages/desktop/src/utils/id.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import z from "zod"
2+
3+
const prefixes = {
4+
session: "ses",
5+
message: "msg",
6+
permission: "per",
7+
user: "usr",
8+
part: "prt",
9+
pty: "pty",
10+
} as const
11+
12+
const LENGTH = 26
13+
let lastTimestamp = 0
14+
let counter = 0
15+
16+
type Prefix = keyof typeof prefixes
17+
export namespace Identifier {
18+
export function schema(prefix: Prefix) {
19+
return z.string().startsWith(prefixes[prefix])
20+
}
21+
22+
export function ascending(prefix: Prefix, given?: string) {
23+
return generateID(prefix, false, given)
24+
}
25+
26+
export function descending(prefix: Prefix, given?: string) {
27+
return generateID(prefix, true, given)
28+
}
29+
}
30+
31+
function generateID(prefix: Prefix, 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+
40+
return given
41+
}
42+
43+
function create(prefix: Prefix, descending: boolean, timestamp?: number): string {
44+
const currentTimestamp = timestamp ?? Date.now()
45+
46+
if (currentTimestamp !== lastTimestamp) {
47+
lastTimestamp = currentTimestamp
48+
counter = 0
49+
}
50+
51+
counter += 1
52+
53+
let now = BigInt(currentTimestamp) * BigInt(0x1000) + BigInt(counter)
54+
55+
if (descending) {
56+
now = ~now
57+
}
58+
59+
const timeBytes = new Uint8Array(6)
60+
for (let i = 0; i < 6; i += 1) {
61+
timeBytes[i] = Number((now >> BigInt(40 - 8 * i)) & BigInt(0xff))
62+
}
63+
64+
return prefixes[prefix] + "_" + bytesToHex(timeBytes) + randomBase62(LENGTH - 12)
65+
}
66+
67+
function bytesToHex(bytes: Uint8Array): string {
68+
let hex = ""
69+
for (let i = 0; i < bytes.length; i += 1) {
70+
hex += bytes[i].toString(16).padStart(2, "0")
71+
}
72+
return hex
73+
}
74+
75+
function randomBase62(length: number): string {
76+
const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
77+
const bytes = getRandomBytes(length)
78+
let result = ""
79+
for (let i = 0; i < length; i += 1) {
80+
result += chars[bytes[i] % 62]
81+
}
82+
return result
83+
}
84+
85+
function getRandomBytes(length: number): Uint8Array {
86+
const bytes = new Uint8Array(length)
87+
const cryptoObj = typeof globalThis !== "undefined" ? globalThis.crypto : undefined
88+
89+
if (cryptoObj && typeof cryptoObj.getRandomValues === "function") {
90+
cryptoObj.getRandomValues(bytes)
91+
return bytes
92+
}
93+
94+
for (let i = 0; i < length; i += 1) {
95+
bytes[i] = Math.floor(Math.random() * 256)
96+
}
97+
98+
return bytes
99+
}

packages/opencode/src/id/id.ts

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,73 @@
1-
import { Identifier as SharedIdentifier } from "@opencode-ai/util/identifier"
1+
import z from "zod"
2+
import { randomBytes } from "crypto"
23

34
export namespace Identifier {
4-
export type Prefix = SharedIdentifier.Prefix
5+
const prefixes = {
6+
session: "ses",
7+
message: "msg",
8+
permission: "per",
9+
user: "usr",
10+
part: "prt",
11+
pty: "pty",
12+
} as const
513

6-
export const schema = (prefix: Prefix) => SharedIdentifier.schema(prefix)
14+
export function schema(prefix: keyof typeof prefixes) {
15+
return z.string().startsWith(prefixes[prefix])
16+
}
17+
18+
const LENGTH = 26
719

8-
export function ascending(prefix: Prefix, given?: string) {
9-
return SharedIdentifier.ascending(prefix, given)
20+
// State for monotonic ID generation
21+
let lastTimestamp = 0
22+
let counter = 0
23+
24+
export function ascending(prefix: keyof typeof prefixes, given?: string) {
25+
return generateID(prefix, false, given)
1026
}
1127

12-
export function descending(prefix: Prefix, given?: string) {
13-
return SharedIdentifier.descending(prefix, given)
28+
export function descending(prefix: keyof typeof prefixes, given?: string) {
29+
return generateID(prefix, true, given)
1430
}
1531

16-
export function create(prefix: Prefix, descending: boolean, timestamp?: number) {
17-
return SharedIdentifier.createPrefixed(prefix, descending, timestamp)
32+
function generateID(prefix: keyof typeof prefixes, descending: boolean, given?: string): string {
33+
if (!given) {
34+
return create(prefix, descending)
35+
}
36+
37+
if (!given.startsWith(prefixes[prefix])) {
38+
throw new Error(`ID ${given} does not start with ${prefixes[prefix]}`)
39+
}
40+
return given
41+
}
42+
43+
function randomBase62(length: number): string {
44+
const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
45+
let result = ""
46+
const bytes = randomBytes(length)
47+
for (let i = 0; i < length; i++) {
48+
result += chars[bytes[i] % 62]
49+
}
50+
return result
51+
}
52+
53+
export function create(prefix: keyof typeof prefixes, descending: boolean, timestamp?: number): string {
54+
const currentTimestamp = timestamp ?? Date.now()
55+
56+
if (currentTimestamp !== lastTimestamp) {
57+
lastTimestamp = currentTimestamp
58+
counter = 0
59+
}
60+
counter++
61+
62+
let now = BigInt(currentTimestamp) * BigInt(0x1000) + BigInt(counter)
63+
64+
now = descending ? ~now : now
65+
66+
const timeBytes = Buffer.alloc(6)
67+
for (let i = 0; i < 6; i++) {
68+
timeBytes[i] = Number((now >> BigInt(40 - 8 * i)) & BigInt(0xff))
69+
}
70+
71+
return prefixes[prefix] + "_" + timeBytes.toString("hex") + randomBase62(LENGTH - 12)
1872
}
1973
}

packages/util/src/identifier.ts

Lines changed: 22 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,48 @@
1-
import z from "zod"
1+
import { randomBytes } from "crypto"
22

33
export namespace Identifier {
4-
const prefixes = {
5-
session: "ses",
6-
message: "msg",
7-
permission: "per",
8-
user: "usr",
9-
part: "prt",
10-
pty: "pty",
11-
} as const
12-
13-
export type Prefix = keyof typeof prefixes
14-
type CryptoLike = {
15-
getRandomValues<T extends ArrayBufferView>(array: T): T
16-
}
17-
18-
const TOTAL_LENGTH = 26
19-
const RANDOM_LENGTH = TOTAL_LENGTH - 12
20-
const BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
4+
const LENGTH = 26
215

6+
// State for monotonic ID generation
227
let lastTimestamp = 0
238
let counter = 0
249

25-
const fillRandomBytes = (buffer: Uint8Array) => {
26-
const cryptoLike = (globalThis as { crypto?: CryptoLike }).crypto
27-
if (cryptoLike?.getRandomValues) {
28-
cryptoLike.getRandomValues(buffer)
29-
return buffer
30-
}
31-
for (let i = 0; i < buffer.length; i++) {
32-
buffer[i] = Math.floor(Math.random() * 256)
33-
}
34-
return buffer
10+
export function ascending() {
11+
return create(false)
3512
}
3613

37-
const randomBase62 = (length: number) => {
38-
const bytes = fillRandomBytes(new Uint8Array(length))
14+
export function descending() {
15+
return create(true)
16+
}
17+
18+
function randomBase62(length: number): string {
19+
const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
3920
let result = ""
21+
const bytes = randomBytes(length)
4022
for (let i = 0; i < length; i++) {
41-
result += BASE62[bytes[i] % BASE62.length]
23+
result += chars[bytes[i] % 62]
4224
}
4325
return result
4426
}
4527

46-
const createSuffix = (descending: boolean, timestamp?: number) => {
28+
export function create(descending: boolean, timestamp?: number): string {
4729
const currentTimestamp = timestamp ?? Date.now()
30+
4831
if (currentTimestamp !== lastTimestamp) {
4932
lastTimestamp = currentTimestamp
5033
counter = 0
5134
}
52-
counter += 1
35+
counter++
5336

54-
let value = BigInt(currentTimestamp) * 0x1000n + BigInt(counter)
55-
if (descending) value = ~value
37+
let now = BigInt(currentTimestamp) * BigInt(0x1000) + BigInt(counter)
5638

57-
const timeBytes = new Uint8Array(6)
58-
for (let i = 0; i < 6; i++) {
59-
timeBytes[i] = Number((value >> BigInt(40 - 8 * i)) & 0xffn)
60-
}
61-
const hex = Array.from(timeBytes)
62-
.map((byte) => byte.toString(16).padStart(2, "0"))
63-
.join("")
64-
return hex + randomBase62(RANDOM_LENGTH)
65-
}
39+
now = descending ? ~now : now
6640

67-
const generateID = (prefix: Prefix, descending: boolean, given?: string, timestamp?: number) => {
68-
if (given) {
69-
const expected = `${prefixes[prefix]}_`
70-
if (!given.startsWith(expected)) throw new Error(`ID ${given} does not start with ${expected}`)
71-
return given
41+
const timeBytes = Buffer.alloc(6)
42+
for (let i = 0; i < 6; i++) {
43+
timeBytes[i] = Number((now >> BigInt(40 - 8 * i)) & BigInt(0xff))
7244
}
73-
return `${prefixes[prefix]}_${createSuffix(descending, timestamp)}`
74-
}
75-
76-
export const schema = (prefix: Prefix) => z.string().startsWith(`${prefixes[prefix]}_`)
77-
78-
export function ascending(): string
79-
export function ascending(prefix: Prefix, given?: string): string
80-
export function ascending(prefix?: Prefix, given?: string) {
81-
if (prefix) return generateID(prefix, false, given)
82-
return create(false)
83-
}
84-
85-
export function descending(): string
86-
export function descending(prefix: Prefix, given?: string): string
87-
export function descending(prefix?: Prefix, given?: string) {
88-
if (prefix) return generateID(prefix, true, given)
89-
return create(true)
90-
}
91-
92-
export function create(descending: boolean, timestamp?: number) {
93-
return createSuffix(descending, timestamp)
94-
}
9545

96-
export function createPrefixed(prefix: Prefix, descending: boolean, timestamp?: number) {
97-
return generateID(prefix, descending, undefined, timestamp)
46+
return timeBytes.toString("hex") + randomBase62(LENGTH - 12)
9847
}
9948
}

0 commit comments

Comments
 (0)