(state: SessionState, messages: WithParts[])
| 191 | } |
| 192 | |
| 193 | export function cacheSystemPromptTokens(state: SessionState, messages: WithParts[]): void { |
| 194 | let firstInputTokens = 0 |
| 195 | for (const msg of messages) { |
| 196 | if (msg.info.role !== "assistant") { |
| 197 | continue |
| 198 | } |
| 199 | const info = msg.info as any |
| 200 | const input = info?.tokens?.input || 0 |
| 201 | const cacheRead = info?.tokens?.cache?.read || 0 |
| 202 | const cacheWrite = info?.tokens?.cache?.write || 0 |
| 203 | if (input > 0 || cacheRead > 0 || cacheWrite > 0) { |
| 204 | firstInputTokens = input + cacheRead + cacheWrite |
| 205 | break |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | if (firstInputTokens <= 0) { |
| 210 | state.systemPromptTokens = undefined |
| 211 | return |
| 212 | } |
| 213 | |
| 214 | let firstUserText = "" |
| 215 | for (const msg of messages) { |
| 216 | if (msg.info.role !== "user" || isIgnoredUserMessage(msg)) { |
| 217 | continue |
| 218 | } |
| 219 | const parts = Array.isArray(msg.parts) ? msg.parts : [] |
| 220 | for (const part of parts) { |
| 221 | if (part.type === "text" && !(part as any).ignored) { |
| 222 | firstUserText += part.text |
| 223 | } |
| 224 | } |
| 225 | break |
| 226 | } |
| 227 | |
| 228 | const estimatedSystemTokens = Math.max(0, firstInputTokens - countTokens(firstUserText)) |
| 229 | state.systemPromptTokens = estimatedSystemTokens > 0 ? estimatedSystemTokens : undefined |
| 230 | } |
| 231 | |
| 232 | export function shortenPath(input: string, workingDirectory?: string): string { |
| 233 | const inPathMatch = input.match(/^(.+) in (.+)$/) |
no test coverage detected