2025-12-13 13:37:37 +09:00
|
|
|
import {
|
2025-12-14 11:38:33 +09:00
|
|
|
KEYWORD_DETECTORS,
|
2025-12-13 13:37:37 +09:00
|
|
|
CODE_BLOCK_PATTERN,
|
|
|
|
|
INLINE_CODE_PATTERN,
|
|
|
|
|
} from "./constants"
|
|
|
|
|
|
2026-01-01 20:58:02 +09:00
|
|
|
export interface DetectedKeyword {
|
|
|
|
|
type: "ultrawork" | "search" | "analyze"
|
|
|
|
|
message: string
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-13 13:37:37 +09:00
|
|
|
export function removeCodeBlocks(text: string): string {
|
|
|
|
|
return text.replace(CODE_BLOCK_PATTERN, "").replace(INLINE_CODE_PATTERN, "")
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-05 16:26:29 +09:00
|
|
|
/**
|
|
|
|
|
* Resolves message to string, handling both static strings and dynamic functions.
|
|
|
|
|
*/
|
|
|
|
|
function resolveMessage(
|
|
|
|
|
message: string | ((agentName?: string) => string),
|
|
|
|
|
agentName?: string
|
|
|
|
|
): string {
|
|
|
|
|
return typeof message === "function" ? message(agentName) : message
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function detectKeywords(text: string, agentName?: string): string[] {
|
2025-12-13 13:37:37 +09:00
|
|
|
const textWithoutCode = removeCodeBlocks(text)
|
2025-12-14 11:38:33 +09:00
|
|
|
return KEYWORD_DETECTORS.filter(({ pattern }) =>
|
|
|
|
|
pattern.test(textWithoutCode)
|
2026-01-05 16:26:29 +09:00
|
|
|
).map(({ message }) => resolveMessage(message, agentName))
|
2025-12-13 13:37:37 +09:00
|
|
|
}
|
|
|
|
|
|
2026-01-05 16:26:29 +09:00
|
|
|
export function detectKeywordsWithType(text: string, agentName?: string): DetectedKeyword[] {
|
2026-01-01 20:58:02 +09:00
|
|
|
const textWithoutCode = removeCodeBlocks(text)
|
|
|
|
|
const types: Array<"ultrawork" | "search" | "analyze"> = ["ultrawork", "search", "analyze"]
|
|
|
|
|
return KEYWORD_DETECTORS.map(({ pattern, message }, index) => ({
|
|
|
|
|
matches: pattern.test(textWithoutCode),
|
|
|
|
|
type: types[index],
|
2026-01-05 16:26:29 +09:00
|
|
|
message: resolveMessage(message, agentName),
|
2026-01-01 20:58:02 +09:00
|
|
|
}))
|
|
|
|
|
.filter((result) => result.matches)
|
|
|
|
|
.map(({ type, message }) => ({ type, message }))
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-13 13:37:37 +09:00
|
|
|
export function extractPromptText(
|
|
|
|
|
parts: Array<{ type: string; text?: string }>
|
|
|
|
|
): string {
|
|
|
|
|
return parts
|
|
|
|
|
.filter((p) => p.type === "text")
|
|
|
|
|
.map((p) => p.text || "")
|
2025-12-14 11:38:33 +09:00
|
|
|
.join(" ")
|
2025-12-13 13:37:37 +09:00
|
|
|
}
|