/
Alex_Ural
/
opencode
Обзор
Документация
Войти
/
Alex_Ural
/
opencode
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
dev
packages/core/src/id/id.ts
47 строк
1 KB
Kit Langton
refactor(schema): extract shared public schemas (#33571)
24 июн 2026, 05:31
Не верифицирован
24 июн 2026, 05:31
516cfe4
Код
Авторство
О чём код?
import { create as createIdentifier } from "@opencode-ai/schema/identifier" const prefixes = { job: "job", event: "evt", session: "ses", message: "msg", permission: "per", question: "que", part: "prt", pty: "pty", tool: "tool", workspace: "wrk", } as const export function ascending(prefix: keyof typeof prefixes, given?: string) { return generateID(prefix, "ascending", given) } export function descending(prefix: keyof typeof prefixes, given?: string) { return generateID(prefix, "descending", given) } function generateID(prefix: keyof typeof prefixes, direction: "descending" | "ascending", given?: string): string { if (!given) { return create(prefixes[prefix], direction) } if (!given.startsWith(prefixes[prefix])) { throw new Error(`ID ${given} does not start with ${prefixes[prefix]}`) } return given } export function create(prefix: string, direction: "descending" | "ascending", timestamp?: number): string { return prefix + "_" + createIdentifier(direction === "descending", timestamp) } /** Extract timestamp from an ascending ID. Does not work with descending IDs. */ export function timestamp(id: string): number { const prefix = id.split("_")[0] const hex = id.slice(prefix.length + 1, prefix.length + 13) const encoded = BigInt("0x" + hex) return Number(encoded / BigInt(0x1000)) } export * as Identifier from "./id"