This commit is contained in:
Adam
2025-12-18 15:47:16 -06:00
parent 0ebcaff927
commit d57b963141
6 changed files with 189 additions and 85 deletions

View File

@@ -154,6 +154,7 @@
"solid-list": "catalog:",
"tailwindcss": "catalog:",
"virtua": "catalog:",
"zod": "catalog:",
},
"devDependencies": {
"@happy-dom/global-registrator": "20.0.11",

View File

@@ -56,6 +56,7 @@
"solid-js": "catalog:",
"solid-list": "catalog:",
"tailwindcss": "catalog:",
"virtua": "catalog:"
"virtua": "catalog:",
"zod": "catalog:"
}
}

View File

@@ -21,7 +21,7 @@ import { DialogSelectModelUnpaid } from "@/components/dialog-select-model-unpaid
import { useProviders } from "@/hooks/use-providers"
import { useCommand, formatKeybind } from "@/context/command"
import { persisted } from "@/utils/persist"
import { Identifier } from "@opencode-ai/util/identifier"
import { Identifier } from "@/utils/id"
const ACCEPTED_IMAGE_TYPES = ["image/png", "image/jpeg", "image/gif", "image/webp"]
const ACCEPTED_FILE_TYPES = [...ACCEPTED_IMAGE_TYPES, "application/pdf"]

View File

@@ -0,0 +1,99 @@
import z from "zod"
const prefixes = {
session: "ses",
message: "msg",
permission: "per",
user: "usr",
part: "prt",
pty: "pty",
} as const
const LENGTH = 26
let lastTimestamp = 0
let counter = 0
type Prefix = keyof typeof prefixes
export namespace Identifier {
export function schema(prefix: Prefix) {
return z.string().startsWith(prefixes[prefix])
}
export function ascending(prefix: Prefix, given?: string) {
return generateID(prefix, false, given)
}
export function descending(prefix: Prefix, given?: string) {
return generateID(prefix, true, given)
}
}
function generateID(prefix: Prefix, descending: boolean, given?: string): string {
if (!given) {
return create(prefix, descending)
}
if (!given.startsWith(prefixes[prefix])) {
throw new Error(`ID ${given} does not start with ${prefixes[prefix]}`)
}
return given
}
function create(prefix: Prefix, descending: boolean, timestamp?: number): string {
const currentTimestamp = timestamp ?? Date.now()
if (currentTimestamp !== lastTimestamp) {
lastTimestamp = currentTimestamp
counter = 0
}
counter += 1
let now = BigInt(currentTimestamp) * BigInt(0x1000) + BigInt(counter)
if (descending) {
now = ~now
}
const timeBytes = new Uint8Array(6)
for (let i = 0; i < 6; i += 1) {
timeBytes[i] = Number((now >> BigInt(40 - 8 * i)) & BigInt(0xff))
}
return prefixes[prefix] + "_" + bytesToHex(timeBytes) + randomBase62(LENGTH - 12)
}
function bytesToHex(bytes: Uint8Array): string {
let hex = ""
for (let i = 0; i < bytes.length; i += 1) {
hex += bytes[i].toString(16).padStart(2, "0")
}
return hex
}
function randomBase62(length: number): string {
const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
const bytes = getRandomBytes(length)
let result = ""
for (let i = 0; i < length; i += 1) {
result += chars[bytes[i] % 62]
}
return result
}
function getRandomBytes(length: number): Uint8Array {
const bytes = new Uint8Array(length)
const cryptoObj = typeof globalThis !== "undefined" ? globalThis.crypto : undefined
if (cryptoObj && typeof cryptoObj.getRandomValues === "function") {
cryptoObj.getRandomValues(bytes)
return bytes
}
for (let i = 0; i < length; i += 1) {
bytes[i] = Math.floor(Math.random() * 256)
}
return bytes
}

View File

@@ -1,19 +1,73 @@
import { Identifier as SharedIdentifier } from "@opencode-ai/util/identifier"
import z from "zod"
import { randomBytes } from "crypto"
export namespace Identifier {
export type Prefix = SharedIdentifier.Prefix
const prefixes = {
session: "ses",
message: "msg",
permission: "per",
user: "usr",
part: "prt",
pty: "pty",
} as const
export const schema = (prefix: Prefix) => SharedIdentifier.schema(prefix)
export function ascending(prefix: Prefix, given?: string) {
return SharedIdentifier.ascending(prefix, given)
export function schema(prefix: keyof typeof prefixes) {
return z.string().startsWith(prefixes[prefix])
}
export function descending(prefix: Prefix, given?: string) {
return SharedIdentifier.descending(prefix, given)
const LENGTH = 26
// State for monotonic ID generation
let lastTimestamp = 0
let counter = 0
export function ascending(prefix: keyof typeof prefixes, given?: string) {
return generateID(prefix, false, given)
}
export function create(prefix: Prefix, descending: boolean, timestamp?: number) {
return SharedIdentifier.createPrefixed(prefix, descending, timestamp)
export function descending(prefix: keyof typeof prefixes, given?: string) {
return generateID(prefix, true, given)
}
function generateID(prefix: keyof typeof prefixes, descending: boolean, given?: string): string {
if (!given) {
return create(prefix, descending)
}
if (!given.startsWith(prefixes[prefix])) {
throw new Error(`ID ${given} does not start with ${prefixes[prefix]}`)
}
return given
}
function randomBase62(length: number): string {
const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
let result = ""
const bytes = randomBytes(length)
for (let i = 0; i < length; i++) {
result += chars[bytes[i] % 62]
}
return result
}
export function create(prefix: keyof typeof prefixes, descending: boolean, timestamp?: number): string {
const currentTimestamp = timestamp ?? Date.now()
if (currentTimestamp !== lastTimestamp) {
lastTimestamp = currentTimestamp
counter = 0
}
counter++
let now = BigInt(currentTimestamp) * BigInt(0x1000) + BigInt(counter)
now = descending ? ~now : now
const timeBytes = Buffer.alloc(6)
for (let i = 0; i < 6; i++) {
timeBytes[i] = Number((now >> BigInt(40 - 8 * i)) & BigInt(0xff))
}
return prefixes[prefix] + "_" + timeBytes.toString("hex") + randomBase62(LENGTH - 12)
}
}

View File

@@ -1,99 +1,48 @@
import z from "zod"
import { randomBytes } from "crypto"
export namespace Identifier {
const prefixes = {
session: "ses",
message: "msg",
permission: "per",
user: "usr",
part: "prt",
pty: "pty",
} as const
export type Prefix = keyof typeof prefixes
type CryptoLike = {
getRandomValues<T extends ArrayBufferView>(array: T): T
}
const TOTAL_LENGTH = 26
const RANDOM_LENGTH = TOTAL_LENGTH - 12
const BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
const LENGTH = 26
// State for monotonic ID generation
let lastTimestamp = 0
let counter = 0
const fillRandomBytes = (buffer: Uint8Array) => {
const cryptoLike = (globalThis as { crypto?: CryptoLike }).crypto
if (cryptoLike?.getRandomValues) {
cryptoLike.getRandomValues(buffer)
return buffer
}
for (let i = 0; i < buffer.length; i++) {
buffer[i] = Math.floor(Math.random() * 256)
}
return buffer
export function ascending() {
return create(false)
}
const randomBase62 = (length: number) => {
const bytes = fillRandomBytes(new Uint8Array(length))
export function descending() {
return create(true)
}
function randomBase62(length: number): string {
const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
let result = ""
const bytes = randomBytes(length)
for (let i = 0; i < length; i++) {
result += BASE62[bytes[i] % BASE62.length]
result += chars[bytes[i] % 62]
}
return result
}
const createSuffix = (descending: boolean, timestamp?: number) => {
export function create(descending: boolean, timestamp?: number): string {
const currentTimestamp = timestamp ?? Date.now()
if (currentTimestamp !== lastTimestamp) {
lastTimestamp = currentTimestamp
counter = 0
}
counter += 1
counter++
let value = BigInt(currentTimestamp) * 0x1000n + BigInt(counter)
if (descending) value = ~value
let now = BigInt(currentTimestamp) * BigInt(0x1000) + BigInt(counter)
const timeBytes = new Uint8Array(6)
now = descending ? ~now : now
const timeBytes = Buffer.alloc(6)
for (let i = 0; i < 6; i++) {
timeBytes[i] = Number((value >> BigInt(40 - 8 * i)) & 0xffn)
timeBytes[i] = Number((now >> BigInt(40 - 8 * i)) & BigInt(0xff))
}
const hex = Array.from(timeBytes)
.map((byte) => byte.toString(16).padStart(2, "0"))
.join("")
return hex + randomBase62(RANDOM_LENGTH)
}
const generateID = (prefix: Prefix, descending: boolean, given?: string, timestamp?: number) => {
if (given) {
const expected = `${prefixes[prefix]}_`
if (!given.startsWith(expected)) throw new Error(`ID ${given} does not start with ${expected}`)
return given
}
return `${prefixes[prefix]}_${createSuffix(descending, timestamp)}`
}
export const schema = (prefix: Prefix) => z.string().startsWith(`${prefixes[prefix]}_`)
export function ascending(): string
export function ascending(prefix: Prefix, given?: string): string
export function ascending(prefix?: Prefix, given?: string) {
if (prefix) return generateID(prefix, false, given)
return create(false)
}
export function descending(): string
export function descending(prefix: Prefix, given?: string): string
export function descending(prefix?: Prefix, given?: string) {
if (prefix) return generateID(prefix, true, given)
return create(true)
}
export function create(descending: boolean, timestamp?: number) {
return createSuffix(descending, timestamp)
}
export function createPrefixed(prefix: Prefix, descending: boolean, timestamp?: number) {
return generateID(prefix, descending, undefined, timestamp)
return timeBytes.toString("hex") + randomBase62(LENGTH - 12)
}
}