Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions packages/model/src/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,14 +448,24 @@ export function longReplyPrompt(ctx: PromptContext, kind: LongFormKind, userText
].join("\n");
}

/** Spanish or English from the user's own words (results and tool text are English and must not decide). */
const URL_RE = /https?:\/\/\S+|www\.\S+/gi;
const ES_ACCENTS_RE = /[¿¡ñáéíóú]/i;
const ES_WORDS_RE =
/(?<![\p{L}])(qué|que|cuándo|cuando|dónde|donde|cuál|cual|quién|quien|cuánto|cuanto|hola|gracias|mañana|hoy|ayer|ahora|luego|después|despues|tarde|noche|buenos|buenas|tardes|noches|recuérdame|recuerdame|recordame|recordá|recuerda|avísame|avisame|decile|mandale|mándale|poneme|contestale|fijate|léeme|leeme|porfa|dile|dime|tengo|mi|mis|por favor|vence|un|una|el|la|al|los|las|del|y|o|e|ni|pero|porque|para|con|por|pon|ponme|agenda|mensaje|mensajes|algo|olvida|borra|lunes|martes|miércoles|jueves|viernes|sábado|domingo|llama|cómo|como|es|está|soy|eres|hay|voy|vas|va|van|vamos|puedes|puedo|puede|quiero|necesito|cuéntame|busca|búscame|buscame|buscar|en|de|este|esta|esto|estos|estas|ese|esa|eso|esos|esas|su|sus|tu|tus|te|se|le|les|lo|nos|ayuda|ayúdame|ayudame|lee|explica|explícame|explicame|traduce|tradúceme|traduceme|resumen|resúmeme|resumeme|página|pagina|enlace|correo|correos|nota|notas|evento|eventos|recordatorio|recordatorios|alarma|alarmas|revisa|revísame|abre|cierra|muestra|muéstrame|cambia|edita|elimina|quita|guarda|escribe|manda|envía|envia|crea|haz|hazme|compra|cómprame|comprame|paga|todo|nada|más|mas)(?![\p{L}])/giu;
const EN_WORDS_RE =
/(?<![\p{L}])(the|an|to|is|are|was|were|be|been|have|had|do|does|did|can|could|would|should|will|what|when|where|why|who|which|how|hello|hi|please|thanks|thank|my|your|you|our|we|they|them|their|she|her|his|it|its|this|that|these|those|i|and|or|but|for|from|with|about|into|on|off|up|out|of|by|at|in|there|here|some|any|all|every|more|now|later|soon|again|today|tomorrow|tonight|morning|week|month|new|next|last|good|remind|tell|ask|answer|reply|read|write|check|open|close|show|list|find|search|look|get|give|make|create|build|turn|start|stop|cancel|update|change|edit|remove|clear|set|send|call|buy|book|order|pay|add|schedule|forget|delete|erase|save|note|summarize|summary|explain|translate|help|need|want|like|know|think|remember|keep|track|count|calculate|convert|message|messages|meeting|timer|page|site|article|recipe|recipes|events|calendar|reminders|everything|anything|something)(?![\p{L}])/giu;

/** Spanish or English from the user's own words (URLs are stripped so links never decide). */
export function userLanguage(text: string): string | undefined {
if (!text.trim()) return undefined;
if (/[¿¡ñáéíóú]/i.test(text) || /(?<![\p{L}])(qué|que|cuándo|cuando|dónde|donde|cuál|cual|hola|gracias|mañana|hoy|recuérdame|recuerdame|recordame|recordá|recuerda|avísame|avisame|decile|mandale|poneme|contestale|fijate|leeme|porfa|dile|tengo|mi|mis|por favor|vence|un|una|el|los|las|del|para|con|por|pon|ponme|agenda|mensaje|mensajes|mándale|mandale|algo|olvida|borra|buenos|lunes|martes|miércoles|jueves|viernes|sábado|domingo|llama|cómo|como|es|está|soy|eres|cuéntame|busca|búscame|buscame|buscar|en|de)(?![\p{L}])/iu.test(text)) {
return "Spanish (Latin America)";
const prose = text.replace(URL_RE, " ").trim();
if (!prose) return undefined;
const esHits = (prose.match(ES_WORDS_RE)?.length ?? 0) + (ES_ACCENTS_RE.test(prose) ? 1 : 0);
const enHits = prose.match(EN_WORDS_RE)?.length ?? 0;
if (esHits === 0 && enHits === 0) {
// "a" is also a Spanish preposition, so only count it for English when no other word matched.
return /(?<![\p{L}])a(?![\p{L}])/iu.test(prose) ? "English" : undefined;
}
if (/(?<![\p{L}])(the|what|when|where|hello|hi|please|my|remind|tell|thanks|a|an|to|is|are|set|send|message|meeting|tomorrow|today|good|morning|call|buy|book|order|pay|did|do|any|add|schedule|forget|delete|erase|save|note|your|you|i)(?![\p{L}])/iu.test(text)) return "English";
return undefined;
return esHits >= enHits ? "Spanish (Latin America)" : "English";
}

/**
Expand Down
26 changes: 26 additions & 0 deletions packages/model/test/prompt-language.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,29 @@ test("short everyday phrases are detected in both languages", () => {
}
expect(userLanguage("ok")).toBeUndefined();
});

test("URLs do not decide language and common English/Spanish requests are detected outside links", () => {
for (const t of [
"read https://recipes.example/soup and summarize it",
"summarize https://recipes.example/soup for me",
"check www.recipes.example/soup please",
"open https://site.example/hola/café",
"resume my music",
]) {
expect(userLanguage(t), t).toBe("English");
}
for (const t of [
"léeme https://noticias.example/hoy",
"resume esta página: https://recetas.example/sopa",
"resume esta pagina: https://recetas.example/sopa",
"lee https://site.example/read-and-summarize",
]) {
expect(userLanguage(t), t).toBe("Spanish (Latin America)");
}
for (const t of [
"https://recipes.example/soup",
"www.recipes.example/noticias",
]) {
expect(userLanguage(t), t).toBeUndefined();
}
});
Loading