refactor(id): localize prefixes
This commit is contained in:
parent
fb4bab8a66
commit
075cbb9d94
12 changed files with 65 additions and 78 deletions
|
|
@ -3,7 +3,7 @@ import { type AgentPartInput, type FilePartInput, type Part, type TextPartInput
|
|||
import type { FileSelection } from "@/context/file"
|
||||
import { encodeFilePath } from "@/context/file/path"
|
||||
import type { AgentPart, FileAttachmentPart, ImageAttachmentPart, Prompt } from "@/context/prompt"
|
||||
import { Identifier } from "@/utils/id"
|
||||
import { PartID } from "@/utils/id"
|
||||
import { createCommentMetadata, formatCommentNote } from "@/utils/comment-note"
|
||||
|
||||
type PromptRequestPart = (TextPartInput | FilePartInput | AgentPartInput) & { id: string }
|
||||
|
|
@ -91,7 +91,7 @@ const toOptimisticPart = (part: PromptRequestPart, sessionID: string, messageID:
|
|||
export function buildRequestParts(input: BuildRequestPartsInput) {
|
||||
const requestParts: PromptRequestPart[] = [
|
||||
{
|
||||
id: Identifier.ascending("part"),
|
||||
id: PartID.ascending(),
|
||||
type: "text",
|
||||
text: input.text,
|
||||
},
|
||||
|
|
@ -100,7 +100,7 @@ export function buildRequestParts(input: BuildRequestPartsInput) {
|
|||
const files = input.prompt.filter(isFileAttachment).map((attachment) => {
|
||||
const path = absolute(input.sessionDirectory, attachment.path)
|
||||
return {
|
||||
id: Identifier.ascending("part"),
|
||||
id: PartID.ascending(),
|
||||
type: "file",
|
||||
mime: "text/plain",
|
||||
url: `file://${encodeFilePath(path)}${fileQuery(attachment.selection)}`,
|
||||
|
|
@ -119,7 +119,7 @@ export function buildRequestParts(input: BuildRequestPartsInput) {
|
|||
|
||||
const agents = input.prompt.filter(isAgentAttachment).map((attachment) => {
|
||||
return {
|
||||
id: Identifier.ascending("part"),
|
||||
id: PartID.ascending(),
|
||||
type: "agent",
|
||||
name: attachment.name,
|
||||
source: {
|
||||
|
|
@ -139,7 +139,7 @@ export function buildRequestParts(input: BuildRequestPartsInput) {
|
|||
used.add(url)
|
||||
|
||||
const filePart = {
|
||||
id: Identifier.ascending("part"),
|
||||
id: PartID.ascending(),
|
||||
type: "file",
|
||||
mime: "text/plain",
|
||||
url,
|
||||
|
|
@ -154,7 +154,7 @@ export function buildRequestParts(input: BuildRequestPartsInput) {
|
|||
used.add(url)
|
||||
return [
|
||||
{
|
||||
id: Identifier.ascending("part"),
|
||||
id: PartID.ascending(),
|
||||
type: "file",
|
||||
mime: "text/plain",
|
||||
url,
|
||||
|
|
@ -165,7 +165,7 @@ export function buildRequestParts(input: BuildRequestPartsInput) {
|
|||
|
||||
return [
|
||||
{
|
||||
id: Identifier.ascending("part"),
|
||||
id: PartID.ascending(),
|
||||
type: "text",
|
||||
text: formatCommentNote({ path: item.path, selection: item.selection, comment }),
|
||||
synthetic: true,
|
||||
|
|
@ -184,7 +184,7 @@ export function buildRequestParts(input: BuildRequestPartsInput) {
|
|||
|
||||
const images = input.images.map((attachment) => {
|
||||
return {
|
||||
id: Identifier.ascending("part"),
|
||||
id: PartID.ascending(),
|
||||
type: "file",
|
||||
mime: attachment.mime,
|
||||
url: attachment.dataUrl,
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ import { usePermission } from "@/context/permission"
|
|||
import { type ContextItem, type ImageAttachmentPart, type Prompt, usePrompt } from "@/context/prompt"
|
||||
import { useSDK } from "@/context/sdk"
|
||||
import { useSync } from "@/context/sync"
|
||||
import { Identifier } from "@/utils/id"
|
||||
import { MessageID, PartID } from "@/utils/id"
|
||||
import { Worktree as WorktreeState } from "@/utils/worktree"
|
||||
import { buildRequestParts } from "./build-request-parts"
|
||||
import { setCursorPosition } from "./editor-dom"
|
||||
|
|
@ -89,7 +89,7 @@ export async function sendFollowupDraft(input: FollowupSendInput) {
|
|||
model: `${input.draft.model.providerID}/${input.draft.model.modelID}`,
|
||||
variant: input.draft.variant,
|
||||
parts: images.map((attachment) => ({
|
||||
id: Identifier.ascending("part"),
|
||||
id: PartID.ascending(),
|
||||
type: "file" as const,
|
||||
mime: attachment.mime,
|
||||
url: attachment.dataUrl,
|
||||
|
|
@ -103,7 +103,7 @@ export async function sendFollowupDraft(input: FollowupSendInput) {
|
|||
}
|
||||
}
|
||||
|
||||
const messageID = input.messageID ?? Identifier.ascending("message")
|
||||
const messageID = input.messageID ?? MessageID.ascending()
|
||||
const { requestParts, optimisticParts } = buildRequestParts({
|
||||
prompt: input.draft.prompt,
|
||||
context: input.draft.context,
|
||||
|
|
@ -467,7 +467,7 @@ export function createPromptSubmit(input: PromptSubmitInput) {
|
|||
model: `${model.providerID}/${model.modelID}`,
|
||||
variant,
|
||||
parts: images.map((attachment) => ({
|
||||
id: Identifier.ascending("part"),
|
||||
id: PartID.ascending(),
|
||||
type: "file" as const,
|
||||
mime: attachment.mime,
|
||||
url: attachment.dataUrl,
|
||||
|
|
@ -486,7 +486,7 @@ export function createPromptSubmit(input: PromptSubmitInput) {
|
|||
}
|
||||
|
||||
const commentItems = context.filter((item) => item.type === "file" && !!item.comment?.trim())
|
||||
const messageID = Identifier.ascending("message")
|
||||
const messageID = MessageID.ascending()
|
||||
|
||||
const removeOptimisticMessage = () => {
|
||||
sync.session.optimistic.remove({
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ import { SessionSidePanel } from "@/pages/session/session-side-panel"
|
|||
import { TerminalPanel } from "@/pages/session/terminal-panel"
|
||||
import { useSessionCommands } from "@/pages/session/use-session-commands"
|
||||
import { useSessionHashScroll } from "@/pages/session/use-session-hash-scroll"
|
||||
import { Identifier } from "@/utils/id"
|
||||
import { MessageID } from "@/utils/id"
|
||||
import { diffs as list } from "@/utils/diffs"
|
||||
import { Persist, persisted } from "@/utils/persist"
|
||||
import { extractPromptFromParts } from "@/utils/prompt"
|
||||
|
|
@ -1575,10 +1575,7 @@ export default function Page() {
|
|||
}
|
||||
|
||||
const queueFollowup = (draft: FollowupDraft) => {
|
||||
setFollowup("items", draft.sessionID, (items) => [
|
||||
...(items ?? []),
|
||||
{ id: Identifier.ascending("message"), ...draft },
|
||||
])
|
||||
setFollowup("items", draft.sessionID, (items) => [...(items ?? []), { id: MessageID.ascending(), ...draft }])
|
||||
setFollowup("failed", draft.sessionID, undefined)
|
||||
setFollowup("paused", draft.sessionID, undefined)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,31 +1,15 @@
|
|||
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])
|
||||
}
|
||||
type Prefix = "msg" | "prt"
|
||||
|
||||
export function ascending(prefix: Prefix, given?: string) {
|
||||
return generateID(prefix, false, given)
|
||||
}
|
||||
export const MessageID = {
|
||||
ascending: (given?: string) => generateID("msg", false, given),
|
||||
}
|
||||
|
||||
export function descending(prefix: Prefix, given?: string) {
|
||||
return generateID(prefix, true, given)
|
||||
}
|
||||
export const PartID = {
|
||||
ascending: (given?: string) => generateID("prt", false, given),
|
||||
}
|
||||
|
||||
function generateID(prefix: Prefix, descending: boolean, given?: string): string {
|
||||
|
|
@ -33,8 +17,8 @@ function generateID(prefix: Prefix, descending: boolean, given?: string): string
|
|||
return create(prefix, descending)
|
||||
}
|
||||
|
||||
if (!given.startsWith(prefixes[prefix])) {
|
||||
throw new Error(`ID ${given} does not start with ${prefixes[prefix]}`)
|
||||
if (!given.startsWith(prefix)) {
|
||||
throw new Error(`ID ${given} does not start with ${prefix}`)
|
||||
}
|
||||
|
||||
return given
|
||||
|
|
@ -61,7 +45,7 @@ function create(prefix: Prefix, descending: boolean, timestamp?: number): string
|
|||
timeBytes[i] = Number((now >> BigInt(40 - 8 * i)) & BigInt(0xff))
|
||||
}
|
||||
|
||||
return prefixes[prefix] + "_" + bytesToHex(timeBytes) + randomBase62(LENGTH - 12)
|
||||
return prefix + "_" + bytesToHex(timeBytes) + randomBase62(LENGTH - 12)
|
||||
}
|
||||
|
||||
function bytesToHex(bytes: Uint8Array): string {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue