-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathturn-identity.ts
More file actions
90 lines (79 loc) · 2.76 KB
/
Copy pathturn-identity.ts
File metadata and controls
90 lines (79 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import type { JsonRecord } from './stream-normalizer'
export interface PersistedChatMessageForTurn {
id: string
role: 'user' | 'assistant' | 'system' | 'tool'
content: string
parts: Array<Record<string, unknown>> | null
}
export interface ResolvedChatTurn {
turnIndex: number
shouldInsertUserMessage: boolean
priorMessages: PersistedChatMessageForTurn[]
userParts: JsonRecord[]
}
export function normalizeClientTurnId(value: unknown): string | undefined {
if (value === undefined || value === null) return undefined
if (typeof value !== 'string') throw new Error('turnId must be a string')
const trimmed = value.trim()
if (!trimmed) throw new Error('turnId must not be blank')
if (trimmed.length > 160) throw new Error('turnId is too long')
if (!/^[A-Za-z0-9:_-]+$/.test(trimmed)) {
throw new Error('turnId contains unsupported characters')
}
return trimmed
}
export function buildUserTextParts(text: string, turnId: string | undefined): JsonRecord[] {
const part: JsonRecord = { type: 'text', text }
if (turnId) part.turnId = turnId
return [part]
}
export function messageHasTurnId(message: PersistedChatMessageForTurn, turnId: string): boolean {
for (const part of message.parts ?? []) {
if (part && typeof part === 'object' && String(part.turnId ?? '') === turnId) {
return true
}
}
return false
}
export function resolveChatTurn(input: {
existingMessages: PersistedChatMessageForTurn[]
userContent: string
turnId?: string
}): ResolvedChatTurn {
const { existingMessages, userContent, turnId } = input
const reusableIndex = findReusableUserMessageIndex(existingMessages, userContent, turnId)
if (reusableIndex >= 0) {
return {
turnIndex: countUserMessages(existingMessages.slice(0, reusableIndex)),
shouldInsertUserMessage: false,
priorMessages: existingMessages.slice(0, reusableIndex),
userParts: buildUserTextParts(userContent, turnId),
}
}
return {
turnIndex: countUserMessages(existingMessages),
shouldInsertUserMessage: true,
priorMessages: existingMessages,
userParts: buildUserTextParts(userContent, turnId),
}
}
function findReusableUserMessageIndex(
messages: PersistedChatMessageForTurn[],
userContent: string,
turnId: string | undefined,
): number {
if (turnId) {
for (let index = messages.length - 1; index >= 0; index -= 1) {
const message = messages[index]
if (message?.role === 'user' && messageHasTurnId(message, turnId)) return index
}
}
const latest = messages.at(-1)
if (latest?.role === 'user' && latest.content === userContent) {
return messages.length - 1
}
return -1
}
function countUserMessages(messages: PersistedChatMessageForTurn[]): number {
return messages.filter((message) => message.role === 'user').length
}