-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathutils.ts
More file actions
185 lines (162 loc) · 5.61 KB
/
utils.ts
File metadata and controls
185 lines (162 loc) · 5.61 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { createHash } from "node:crypto"
import type { SessionState, WithParts } from "../state"
import { isMessageCompacted } from "../state/utils"
import type { UserMessage } from "@opencode-ai/sdk/v2"
const SUMMARY_ID_HASH_LENGTH = 16
const DCP_BLOCK_ID_TAG_REGEX = /(<dcp-message-id(?=[\s>])[^>]*>)b\d+(<\/dcp-message-id>)/g
const DCP_PAIRED_TAG_REGEX = /<dcp[^>]*>[\s\S]*?<\/dcp[^>]*>/gi
const DCP_UNPAIRED_TAG_REGEX = /<\/?dcp[^>]*>/gi
const generateStableId = (prefix: string, seed: string): string => {
const hash = createHash("sha256").update(seed).digest("hex").slice(0, SUMMARY_ID_HASH_LENGTH)
return `${prefix}_${hash}`
}
export const createSyntheticUserMessage = (
baseMessage: WithParts,
content: string,
stableSeed?: string,
): WithParts => {
const userInfo = baseMessage.info as UserMessage
const now = Date.now()
const deterministicSeed = stableSeed?.trim() || userInfo.id
const messageId = generateStableId("msg_dcp_summary", deterministicSeed)
const partId = generateStableId("prt_dcp_summary", deterministicSeed)
return {
info: {
id: messageId,
sessionID: userInfo.sessionID,
role: "user" as const,
agent: userInfo.agent,
model: userInfo.model,
time: { created: now },
},
parts: [
{
id: partId,
sessionID: userInfo.sessionID,
messageID: messageId,
type: "text" as const,
text: content,
},
],
}
}
export const createSyntheticTextPart = (
baseMessage: WithParts,
content: string,
stableSeed?: string,
) => {
const userInfo = baseMessage.info as UserMessage
const deterministicSeed = stableSeed?.trim() || userInfo.id
const partId = generateStableId("prt_dcp_text", deterministicSeed)
return {
id: partId,
sessionID: userInfo.sessionID,
messageID: userInfo.id,
type: "text" as const,
text: content,
}
}
type MessagePart = WithParts["parts"][number]
type ToolPart = Extract<MessagePart, { type: "tool" }>
type TextPart = Extract<MessagePart, { type: "text" }>
export const appendToLastTextPart = (message: WithParts, injection: string): boolean => {
const textPart = findLastTextPart(message)
if (!textPart) {
return false
}
return appendToTextPart(textPart, injection)
}
const findLastTextPart = (message: WithParts): TextPart | null => {
for (let i = message.parts.length - 1; i >= 0; i--) {
const part = message.parts[i]
if (part.type === "text") {
return part
}
}
return null
}
export const appendToTextPart = (part: TextPart, injection: string): boolean => {
if (typeof part.text !== "string") {
return false
}
const normalizedInjection = injection.replace(/^\n+/, "")
if (!normalizedInjection.trim()) {
return false
}
if (part.text.includes(normalizedInjection)) {
return true
}
const baseText = part.text.replace(/\n*$/, "")
part.text = baseText.length > 0 ? `${baseText}\n\n${normalizedInjection}` : normalizedInjection
return true
}
export const appendToAllToolParts = (message: WithParts, tag: string): boolean => {
let injected = false
for (const part of message.parts) {
if (part.type === "tool") {
injected = appendToToolPart(part, tag) || injected
}
}
return injected
}
export const appendToToolPart = (part: ToolPart, tag: string): boolean => {
if (part.state?.status !== "completed" || typeof part.state.output !== "string") {
return false
}
if (part.state.output.includes(tag)) {
return true
}
part.state.output = `${part.state.output}${tag}`
return true
}
export const hasContent = (message: WithParts): boolean => {
return message.parts.some(
(part) =>
(part.type === "text" &&
typeof part.text === "string" &&
part.text.trim().length > 0) ||
(part.type === "tool" &&
part.state?.status === "completed" &&
typeof part.state.output === "string"),
)
}
export function buildToolIdList(state: SessionState, messages: WithParts[]): string[] {
const toolIds: string[] = []
for (const msg of messages) {
if (isMessageCompacted(state, msg)) {
continue
}
const parts = Array.isArray(msg.parts) ? msg.parts : []
if (parts.length > 0) {
for (const part of parts) {
if (part.type === "tool" && part.callID && part.tool) {
toolIds.push(part.callID)
}
}
}
}
state.toolIdList = toolIds
return toolIds
}
export const replaceBlockIdsWithBlocked = (text: string): string => {
return text.replace(DCP_BLOCK_ID_TAG_REGEX, "$1BLOCKED$2")
}
export const stripHallucinationsFromString = (text: string): string => {
return text.replace(DCP_PAIRED_TAG_REGEX, "").replace(DCP_UNPAIRED_TAG_REGEX, "")
}
export const stripHallucinations = (messages: WithParts[]): void => {
for (const message of messages) {
for (const part of message.parts) {
if (part.type === "text" && typeof part.text === "string") {
part.text = stripHallucinationsFromString(part.text)
}
if (
part.type === "tool" &&
part.state?.status === "completed" &&
typeof part.state.output === "string"
) {
part.state.output = stripHallucinationsFromString(part.state.output)
}
}
}
}