Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 76dc75a03b | |||
| a551f52682 | |||
| 6de855e226 | |||
| c6357e52d9 | |||
| 5d40f2113f | |||
| 9a96fdb3c0 | |||
| 460959f0d4 | |||
| 0dbbb98cf5 | |||
| 4b7ca1b17e | |||
| 65a3689aaa | |||
| 42c11dedae | |||
| 0fbb1fbd92 | |||
| bb650ba563 | |||
| c663532fd4 | |||
| 090b7d83dd | |||
| 4e9eead3ab | |||
| fc6ebf81eb | |||
| b88d5ee6b3 | |||
| 020bd6614b | |||
| 4403026797 | |||
| 552943d6c0 | |||
| 2576be9e49 | |||
| e17fc088b2 | |||
| c5b0344240 |
@@ -1 +1 @@
|
||||
2298268
|
||||
3158517
|
||||
|
||||
Generated
+1
-1
@@ -184,7 +184,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "fable-mcp"
|
||||
version = "0.2.6"
|
||||
version = "0.3.0"
|
||||
source = { editable = "." }
|
||||
dependencies = [
|
||||
{ name = "httpx" },
|
||||
|
||||
@@ -317,6 +317,7 @@ export interface JournalConfig {
|
||||
day_rollover_hour: number;
|
||||
morning_end_hour?: number;
|
||||
midday_end_hour?: number;
|
||||
closeout_enabled?: boolean;
|
||||
// Ambient-context fields (carried forward from the briefing config schema)
|
||||
locations?: { home?: JournalLocation; work?: JournalLocation };
|
||||
temp_unit?: 'C' | 'F';
|
||||
@@ -696,3 +697,11 @@ export const updateProfile = (data: Partial<UserProfile>) =>
|
||||
export const consolidateProfile = () =>
|
||||
apiPost<{ status: string; learned_summary: string }>('/api/profile/consolidate', {})
|
||||
export const clearProfileObservations = () => apiDelete('/api/profile/observations')
|
||||
|
||||
export interface ProfileObservationEntry {
|
||||
date: string
|
||||
bullets: string
|
||||
}
|
||||
|
||||
export const listProfileObservations = () =>
|
||||
apiGet<{ observations: ProfileObservationEntry[] }>('/api/profile/observations')
|
||||
|
||||
@@ -4,7 +4,7 @@ import { ref, computed, watch, onMounted } from "vue";
|
||||
import { useSettingsStore } from "@/stores/settings";
|
||||
import { useAuthStore } from "@/stores/auth";
|
||||
import { useToastStore } from "@/stores/toast";
|
||||
import { apiGet, apiPost, apiPut, apiDelete, listGroups, createGroup, deleteGroup, listGroupMembers, addGroupMember, removeGroupMember, searchUsers, getFableMcpInfo, listApiKeys, createApiKey as apiCreateApiKey, revokeApiKey as apiRevokeApiKey, getVoiceStatus, getVoiceList, synthesiseSpeech, getProfile, updateProfile, consolidateProfile, clearProfileObservations, getJournalConfig, saveJournalConfig, geocodeAddress, type ApiKeyEntry, type GroupEntry, type GroupMember, type UserSearchResult, type VoiceStatusResult, type VoiceEntry, type VoiceBlendEntry, type UserProfile, type JournalConfig } from "@/api/client";
|
||||
import { apiGet, apiPost, apiPut, apiDelete, listGroups, createGroup, deleteGroup, listGroupMembers, addGroupMember, removeGroupMember, searchUsers, getFableMcpInfo, listApiKeys, createApiKey as apiCreateApiKey, revokeApiKey as apiRevokeApiKey, getVoiceStatus, getVoiceList, synthesiseSpeech, getProfile, updateProfile, consolidateProfile, clearProfileObservations, listProfileObservations, getJournalConfig, saveJournalConfig, geocodeAddress, type ApiKeyEntry, type GroupEntry, type GroupMember, type UserSearchResult, type VoiceStatusResult, type VoiceEntry, type VoiceBlendEntry, type UserProfile, type JournalConfig, type ProfileObservationEntry } from "@/api/client";
|
||||
import { usePushStore } from "@/stores/push";
|
||||
import type { User } from "@/types/auth";
|
||||
import PaginationBar from "@/components/PaginationBar.vue";
|
||||
@@ -549,6 +549,26 @@ const profileSaving = ref(false)
|
||||
const profileSaved = ref(false)
|
||||
const consolidating = ref(false)
|
||||
const clearingObs = ref(false)
|
||||
const observations = ref<ProfileObservationEntry[]>([])
|
||||
const observationsExpanded = ref(false)
|
||||
const observationsLoading = ref(false)
|
||||
const observationsLoaded = ref(false)
|
||||
|
||||
async function toggleObservations() {
|
||||
observationsExpanded.value = !observationsExpanded.value
|
||||
if (observationsExpanded.value && !observationsLoaded.value) {
|
||||
observationsLoading.value = true
|
||||
try {
|
||||
const res = await listProfileObservations()
|
||||
observations.value = res.observations
|
||||
observationsLoaded.value = true
|
||||
} catch {
|
||||
toastStore.show('Failed to load observations', 'error')
|
||||
} finally {
|
||||
observationsLoading.value = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function loadProfile() {
|
||||
try { profile.value = await getProfile() } catch { /* non-critical */ }
|
||||
@@ -582,6 +602,17 @@ function toggleProfileWorkDay(day: string) {
|
||||
profile.value.work_schedule = { ...profile.value.work_schedule, days }
|
||||
}
|
||||
|
||||
async function onToggleCloseout(enabled: boolean) {
|
||||
journalConfig.value.closeout_enabled = enabled
|
||||
try {
|
||||
await saveJournalConfig(journalConfig.value)
|
||||
toastStore.show(enabled ? 'Nightly closeout enabled' : 'Nightly closeout disabled')
|
||||
} catch {
|
||||
journalConfig.value.closeout_enabled = !enabled // revert UI on failure
|
||||
toastStore.show('Failed to update closeout setting', 'error')
|
||||
}
|
||||
}
|
||||
|
||||
async function runConsolidate() {
|
||||
consolidating.value = true
|
||||
try {
|
||||
@@ -600,6 +631,7 @@ const journalConfig = ref<JournalConfig>({
|
||||
prep_hour: 5,
|
||||
prep_minute: 0,
|
||||
day_rollover_hour: 4,
|
||||
closeout_enabled: true,
|
||||
locations: { home: { label: 'Home', address: '' }, work: { label: 'Work', address: '' } },
|
||||
temp_unit: 'C',
|
||||
})
|
||||
@@ -620,6 +652,7 @@ async function loadJournalConfig() {
|
||||
prep_hour: cfg.prep_hour ?? 5,
|
||||
prep_minute: cfg.prep_minute ?? 0,
|
||||
day_rollover_hour: cfg.day_rollover_hour ?? 4,
|
||||
closeout_enabled: cfg.closeout_enabled ?? true,
|
||||
morning_end_hour: cfg.morning_end_hour,
|
||||
midday_end_hour: cfg.midday_end_hour,
|
||||
locations: {
|
||||
@@ -689,6 +722,8 @@ async function clearObservations() {
|
||||
profile.value.learned_summary = ''
|
||||
profile.value.observations_count = 0
|
||||
profile.value.observations_updated_at = null
|
||||
observations.value = []
|
||||
observationsLoaded.value = false
|
||||
toastStore.show('Learned data cleared')
|
||||
} catch { toastStore.show('Failed to clear observations', 'error') }
|
||||
finally { clearingObs.value = false }
|
||||
@@ -1901,8 +1936,38 @@ function formatUserDate(iso: string): string {
|
||||
The assistant observes patterns from your journal and chat conversations and builds a summary over time. The summary is included in the journal's system prompt so the daily prep can reference what it knows about you.
|
||||
<span v-if="profile.observations_count > 0"> {{ profile.observations_count }} raw observation{{ profile.observations_count !== 1 ? 's' : '' }} stored.</span>
|
||||
</p>
|
||||
|
||||
<label class="toggle-row" style="display:flex;align-items:center;gap:0.6rem;margin:0.5rem 0 0.75rem">
|
||||
<input
|
||||
type="checkbox"
|
||||
:checked="journalConfig.closeout_enabled !== false"
|
||||
@change="onToggleCloseout(($event.target as HTMLInputElement).checked)"
|
||||
/>
|
||||
<span>
|
||||
<strong>Nightly closeout</strong>
|
||||
<small style="display:block;color:var(--color-text-muted)">Extracts patterns from yesterday's journal at your day-rollover hour.</small>
|
||||
</span>
|
||||
</label>
|
||||
|
||||
<div v-if="profile.learned_summary" class="learned-summary">{{ profile.learned_summary }}</div>
|
||||
<div v-else class="learned-empty">No learned summary yet. Observations accumulate from journal and chat conversations.</div>
|
||||
|
||||
<div class="observations-panel" style="margin-top:0.75rem">
|
||||
<button class="btn-secondary" @click="toggleObservations" :disabled="profile.observations_count === 0">
|
||||
{{ observationsExpanded ? '▾' : '▸' }} Recent observations ({{ profile.observations_count }})
|
||||
</button>
|
||||
<div v-if="observationsExpanded" class="observations-list" style="margin-top:0.5rem;padding:0.5rem 0.75rem;border:1px solid var(--color-border);border-radius:var(--radius-md);background:var(--color-bg-elev-1)">
|
||||
<div v-if="observationsLoading">Loading…</div>
|
||||
<div v-else-if="observations.length === 0">No observations yet.</div>
|
||||
<div v-else>
|
||||
<div v-for="entry in observations" :key="entry.date" style="margin-bottom:0.75rem">
|
||||
<div style="font-weight:600;font-size:0.875rem;color:var(--color-text-muted)">{{ entry.date }}</div>
|
||||
<div style="white-space:pre-wrap;font-size:0.9rem">{{ entry.bullets }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="actions" style="gap:0.5rem;flex-wrap:wrap">
|
||||
<button class="btn-secondary" @click="runConsolidate" :disabled="consolidating || profile.observations_count === 0">
|
||||
{{ consolidating ? 'Consolidating…' : 'Consolidate Now' }}
|
||||
|
||||
@@ -59,3 +59,13 @@ async def clear_observations():
|
||||
uid = get_current_user_id()
|
||||
await clear_learned_data(uid)
|
||||
return jsonify({"status": "ok"})
|
||||
|
||||
|
||||
@profile_bp.route("/observations", methods=["GET"])
|
||||
@login_required
|
||||
async def list_observations():
|
||||
uid = get_current_user_id()
|
||||
profile = await get_profile(uid)
|
||||
raw = list(profile.observations_raw or [])
|
||||
# Newest first, last 14 entries
|
||||
return jsonify({"observations": list(reversed(raw[-14:]))})
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
"""Journal closeout — nightly extraction of profile observations.
|
||||
|
||||
Runs once per user per day at day_rollover_hour. Reads yesterday's /journal
|
||||
conversation, filters out assistant-authored auto-content (daily prep),
|
||||
asks the background LLM to extract user-side patterns/habits, and appends
|
||||
the bullets to user_profiles.observations_raw via append_observations.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime
|
||||
import logging
|
||||
|
||||
from sqlalchemy import or_, select
|
||||
|
||||
from fabledassistant.config import Config
|
||||
from fabledassistant.models import async_session
|
||||
from fabledassistant.models.conversation import Conversation, Message
|
||||
from fabledassistant.services.llm import generate_completion
|
||||
from fabledassistant.services.settings import get_setting
|
||||
from fabledassistant.services.user_profile import append_observations
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Message kinds whose content must NEVER be sent to the closeout LLM.
|
||||
# These are assistant-authored auto-blocks that would otherwise dominate
|
||||
# attention and leak back into "what the assistant has learned."
|
||||
EXCLUDED_KINDS: set[str] = {"daily_prep"}
|
||||
|
||||
|
||||
def _filter_messages(messages):
|
||||
"""Drop messages whose msg_metadata.kind is in EXCLUDED_KINDS.
|
||||
|
||||
Accepts any iterable of message-like objects with `role`, `content`,
|
||||
and `msg_metadata` attributes (real Message rows or SimpleNamespace).
|
||||
"""
|
||||
kept = []
|
||||
for m in messages:
|
||||
meta = getattr(m, "msg_metadata", None) or {}
|
||||
if meta.get("kind") in EXCLUDED_KINDS:
|
||||
continue
|
||||
kept.append(m)
|
||||
return kept
|
||||
|
||||
|
||||
_TRANSCRIPT_WINDOW = 20
|
||||
_CONTENT_CAP = 500
|
||||
|
||||
|
||||
def _build_transcript(messages) -> str:
|
||||
"""Format the last 20 messages as `ROLE: content[:500]` lines."""
|
||||
tail = list(messages)[-_TRANSCRIPT_WINDOW:]
|
||||
return "\n".join(
|
||||
f"{m.role.upper()}: {m.content[:_CONTENT_CAP]}" for m in tail
|
||||
)
|
||||
|
||||
|
||||
SYSTEM_PROMPT = (
|
||||
"You are reviewing a day's journal conversation to extract preference "
|
||||
"observations the USER revealed about themselves.\n\n"
|
||||
"Rules:\n"
|
||||
"- Only extract patterns, habits, recurring frustrations, or contextual "
|
||||
"facts the user said or demonstrated.\n"
|
||||
"- DO NOT restate facts that belong in structured fields: name, job title, "
|
||||
"industry, expertise level, response style, tone, interests. Those are "
|
||||
"handled separately.\n"
|
||||
"- DO NOT extract anything from the ASSISTANT turns about the user — only "
|
||||
"what the user themselves stated or demonstrated by their choices.\n"
|
||||
"- Write 2-5 short bullet points. Be specific and factual.\n"
|
||||
"- If nothing notable, output only: (nothing to note)"
|
||||
)
|
||||
|
||||
|
||||
async def run_for_user(user_id: int, yesterday: datetime.date) -> None:
|
||||
"""Extract preference observations from yesterday's journal conversation.
|
||||
|
||||
Skips silently when there is nothing meaningful to extract.
|
||||
"""
|
||||
async with async_session() as session:
|
||||
conv_result = await session.execute(
|
||||
select(Conversation).where(
|
||||
Conversation.user_id == user_id,
|
||||
Conversation.conversation_type == "journal",
|
||||
Conversation.day_date == yesterday,
|
||||
)
|
||||
)
|
||||
conv = conv_result.scalar_one_or_none()
|
||||
if conv is None:
|
||||
logger.debug("closeout: no journal conv for user %d on %s", user_id, yesterday)
|
||||
return
|
||||
|
||||
msg_result = await session.execute(
|
||||
select(Message)
|
||||
.where(
|
||||
Message.conversation_id == conv.id,
|
||||
Message.role.in_(("user", "assistant")),
|
||||
or_(
|
||||
Message.msg_metadata.is_(None),
|
||||
~Message.msg_metadata["kind"].astext.in_(EXCLUDED_KINDS),
|
||||
),
|
||||
)
|
||||
.order_by(Message.created_at)
|
||||
)
|
||||
messages = list(msg_result.scalars().all())
|
||||
|
||||
# Defensive second-pass filter (covers any message with metadata the
|
||||
# SQL JSON path can't reach, e.g. older rows where kind nesting differs).
|
||||
messages = _filter_messages(messages)
|
||||
|
||||
if len(messages) < 2:
|
||||
logger.debug("closeout: not enough messages for user %d (%d)", user_id, len(messages))
|
||||
return
|
||||
|
||||
transcript = _build_transcript(messages)
|
||||
model = await get_setting(user_id, "background_model", Config.OLLAMA_BACKGROUND_MODEL)
|
||||
|
||||
try:
|
||||
output = (await generate_completion(
|
||||
[
|
||||
{"role": "system", "content": SYSTEM_PROMPT},
|
||||
{"role": "user", "content": transcript},
|
||||
],
|
||||
model,
|
||||
)).strip()
|
||||
except Exception:
|
||||
logger.warning("closeout LLM failed for user %d", user_id, exc_info=True)
|
||||
return
|
||||
|
||||
if not output or "(nothing to note)" in output.lower():
|
||||
logger.debug("closeout: nothing to note for user %d", user_id)
|
||||
return
|
||||
|
||||
await append_observations(user_id, output)
|
||||
logger.info("closeout: appended observations for user %d (%s)", user_id, yesterday)
|
||||
@@ -79,6 +79,15 @@ MOMENT ENTITY LINKING — be conservative.
|
||||
are NOT places — drop them and let the user name the real one if it
|
||||
matters.
|
||||
|
||||
EXISTING WORK — search before recording.
|
||||
- If the user describes ongoing or completed work that references a specific
|
||||
project or task by name or partial name (e.g. "the sebring task",
|
||||
"continuing on the AT&T circuit", "finished the auth refactor"), CALL
|
||||
search_notes FIRST to locate the existing task. Update its status or log
|
||||
work on it instead of recording a new moment when an obvious match exists.
|
||||
- Only call record_moment for that beat if no matching task surfaces and the
|
||||
user confirms they want a moment recorded.
|
||||
|
||||
WHEN LINKING ENTITIES: use the *_names parameters (person_names,
|
||||
place_names, task_titles, note_titles). Server resolves them to IDs by
|
||||
lookup. Do NOT pass *_ids unless you have an exact ID returned from
|
||||
|
||||
@@ -35,6 +35,7 @@ DEFAULT_CONFIG = {
|
||||
"day_rollover_hour": 4,
|
||||
"morning_end_hour": 12,
|
||||
"midday_end_hour": 18,
|
||||
"closeout_enabled": True,
|
||||
}
|
||||
|
||||
|
||||
@@ -88,30 +89,96 @@ def _run_daily_prep_threadsafe(user_id: int) -> None:
|
||||
asyncio.run_coroutine_threadsafe(_do_daily_prep(user_id), _loop)
|
||||
|
||||
|
||||
async def _do_closeout(user_id: int) -> None:
|
||||
try:
|
||||
tz_str = await get_user_timezone(user_id)
|
||||
tz = _resolve_tz(tz_str)
|
||||
now = datetime.datetime.now(tz)
|
||||
# We just rolled into a new day in user-local time. The day that
|
||||
# just ended is yesterday's calendar date regardless of whether
|
||||
# rollover_hour is 0 or 4 — APScheduler fires precisely at the
|
||||
# configured hour so no clock-skew correction is needed.
|
||||
yesterday = now.date() - datetime.timedelta(days=1)
|
||||
from fabledassistant.services.journal_closeout import run_for_user
|
||||
await run_for_user(user_id=user_id, yesterday=yesterday)
|
||||
except Exception:
|
||||
logger.exception("Closeout failed for user %d", user_id)
|
||||
|
||||
|
||||
def _run_closeout_threadsafe(user_id: int) -> None:
|
||||
if _loop is None:
|
||||
return
|
||||
asyncio.run_coroutine_threadsafe(_do_closeout(user_id), _loop)
|
||||
|
||||
|
||||
async def _closeout_catchup(user_id: int) -> None:
|
||||
"""On startup, run yesterday's closeout once if the slot already passed
|
||||
and no entry for yesterday exists in observations_raw.
|
||||
"""
|
||||
try:
|
||||
tz_str = await get_user_timezone(user_id)
|
||||
tz = _resolve_tz(tz_str)
|
||||
config = await get_journal_config(user_id)
|
||||
if not config.get("closeout_enabled", True):
|
||||
return
|
||||
rollover_hour = int(config.get("day_rollover_hour", 4))
|
||||
now = datetime.datetime.now(tz)
|
||||
# Slot hasn't passed yet today → wait for the cron.
|
||||
if now.hour < rollover_hour:
|
||||
return
|
||||
yesterday = (now - datetime.timedelta(days=1)).date()
|
||||
|
||||
from fabledassistant.services.user_profile import get_profile
|
||||
profile = await get_profile(user_id)
|
||||
existing_dates = {
|
||||
(e or {}).get("date") for e in (profile.observations_raw or [])
|
||||
}
|
||||
if yesterday.isoformat() in existing_dates:
|
||||
return
|
||||
|
||||
from fabledassistant.services.journal_closeout import run_for_user
|
||||
await run_for_user(user_id=user_id, yesterday=yesterday)
|
||||
except Exception:
|
||||
logger.exception("Closeout catch-up failed for user %d", user_id)
|
||||
|
||||
|
||||
async def update_user_schedule(user_id: int) -> None:
|
||||
"""Add or replace this user's daily-prep job using their current config."""
|
||||
"""Add or replace this user's daily-prep + closeout jobs from current config."""
|
||||
if _scheduler is None:
|
||||
return
|
||||
job_id = f"journal_prep_{user_id}"
|
||||
if _scheduler.get_job(job_id):
|
||||
_scheduler.remove_job(job_id)
|
||||
|
||||
config = await get_journal_config(user_id)
|
||||
if not config.get("prep_enabled", True):
|
||||
return
|
||||
|
||||
tz_str = await get_user_timezone(user_id)
|
||||
tz = _resolve_tz(tz_str)
|
||||
prep_hour = int(config.get("prep_hour", 5))
|
||||
prep_minute = int(config.get("prep_minute", 0))
|
||||
|
||||
_scheduler.add_job(
|
||||
_run_daily_prep_threadsafe,
|
||||
trigger=CronTrigger(hour=prep_hour, minute=prep_minute, timezone=tz),
|
||||
args=[user_id],
|
||||
id=job_id,
|
||||
replace_existing=True,
|
||||
)
|
||||
# ── Prep job ──────────────────────────────────────────────────────────
|
||||
prep_job_id = f"journal_prep_{user_id}"
|
||||
if _scheduler.get_job(prep_job_id):
|
||||
_scheduler.remove_job(prep_job_id)
|
||||
if config.get("prep_enabled", True):
|
||||
prep_hour = int(config.get("prep_hour", 5))
|
||||
prep_minute = int(config.get("prep_minute", 0))
|
||||
_scheduler.add_job(
|
||||
_run_daily_prep_threadsafe,
|
||||
trigger=CronTrigger(hour=prep_hour, minute=prep_minute, timezone=tz),
|
||||
args=[user_id],
|
||||
id=prep_job_id,
|
||||
replace_existing=True,
|
||||
)
|
||||
|
||||
# ── Closeout job ──────────────────────────────────────────────────────
|
||||
closeout_job_id = f"journal_closeout_{user_id}"
|
||||
if _scheduler.get_job(closeout_job_id):
|
||||
_scheduler.remove_job(closeout_job_id)
|
||||
if config.get("closeout_enabled", True):
|
||||
rollover_hour = int(config.get("day_rollover_hour", 4))
|
||||
_scheduler.add_job(
|
||||
_run_closeout_threadsafe,
|
||||
trigger=CronTrigger(hour=rollover_hour, minute=0, timezone=tz),
|
||||
args=[user_id],
|
||||
id=closeout_job_id,
|
||||
replace_existing=True,
|
||||
)
|
||||
|
||||
|
||||
async def _register_all_user_jobs() -> None:
|
||||
@@ -119,6 +186,9 @@ async def _register_all_user_jobs() -> None:
|
||||
users = (await session.execute(select(User))).scalars().all()
|
||||
for user in users:
|
||||
await update_user_schedule(user.id)
|
||||
# Fire catch-up asynchronously so a slow LLM call doesn't block startup
|
||||
if _loop is not None:
|
||||
asyncio.run_coroutine_threadsafe(_closeout_catchup(user.id), _loop)
|
||||
|
||||
|
||||
def start_journal_scheduler(loop: asyncio.AbstractEventLoop) -> None:
|
||||
|
||||
@@ -620,6 +620,7 @@ async def build_context(
|
||||
"CRITICAL: Call the tool functions directly. NEVER write out function calls as text or code. NEVER describe what you would do — just do it.",
|
||||
"GROUNDING: When the user asks about their own data — tasks, notes, events, projects, news, anything stored in this system — call the relevant tool to see what actually exists before answering. Never assert facts about the user's data from memory, prior context, or assumption. If you are unsure whether something exists, check with a tool.",
|
||||
"HONESTY WHEN EMPTY: If a tool returns empty results (no matching tasks, no events in the date range, no search hits, no notes found), tell the user plainly that nothing matched. Do not fabricate example items, do not invent plausible-sounding meetings or deadlines to fill the response, and do not hedge with generic suggestions dressed up as real data. A direct 'you don't have anything on your calendar today' is always better than an invented event.",
|
||||
"EXISTING WORK: When the user describes ongoing or completed work that references a specific project or task by name or partial name, call search_notes first to locate the existing item. Only call record_moment, create_task, or create_note if no matching task surfaces and the user confirms.",
|
||||
]
|
||||
actions = [
|
||||
"create_note (also creates tasks — set status='todo')", "update_note", "delete_note",
|
||||
|
||||
@@ -22,6 +22,17 @@ def _normalize_tags(tags: list[str]) -> list[str]:
|
||||
return out
|
||||
|
||||
|
||||
# Type-nouns the LLM tends to include in search queries. Treating them as
|
||||
# required ILIKE terms drops literal-title matches; we strip them server-side
|
||||
# and let the `type` / `project` parameters scope results instead.
|
||||
_SEARCH_TYPE_NOUNS = {"task", "tasks", "note", "notes", "project", "projects"}
|
||||
|
||||
|
||||
def _strip_type_nouns(q: str) -> list[str]:
|
||||
"""Return q's tokens with type-nouns removed (case-insensitive)."""
|
||||
return [t for t in q.split() if t.lower() not in _SEARCH_TYPE_NOUNS]
|
||||
|
||||
|
||||
async def _maybe_reactivate_project(project_id: int) -> None:
|
||||
"""If a project is paused, reactivate it — activity indicates resumed work."""
|
||||
from fabledassistant.models.project import Project
|
||||
@@ -155,7 +166,7 @@ async def list_notes(
|
||||
count_query = count_query.where(Note.status.is_(None))
|
||||
|
||||
if q:
|
||||
terms = q.split()
|
||||
terms = _strip_type_nouns(q)
|
||||
for term in terms:
|
||||
escaped_term = term.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
|
||||
pattern = f"%{escaped_term}%"
|
||||
|
||||
@@ -22,31 +22,71 @@ def schedule_embedding(note_id: int, user_id: int, title: str, body: str) -> Non
|
||||
asyncio.create_task(upsert_note_embedding(note_id, user_id, text))
|
||||
|
||||
|
||||
_PROJECT_QUERY_NOISE = {"project", "projects"}
|
||||
|
||||
|
||||
def _normalize(s: str) -> str:
|
||||
"""Lowercase and collapse non-alphanumerics to single spaces."""
|
||||
return re.sub(r"[^a-z0-9]+", " ", s.lower()).strip()
|
||||
|
||||
|
||||
def _normalize_query(query: str) -> str:
|
||||
"""Normalize plus drop trailing type-nouns ('project' / 'projects')
|
||||
that users add as filler when referring to a project by name."""
|
||||
tokens = [t for t in _normalize(query).split() if t not in _PROJECT_QUERY_NOISE]
|
||||
return " ".join(tokens)
|
||||
|
||||
|
||||
def score_project_match(query: str, project) -> float:
|
||||
"""Score how well `query` matches `project`. Range [0.0, 1.0].
|
||||
|
||||
Tiered: exact title → 1.0, substring either-way → 0.85, query found in
|
||||
description/summary → 0.70, otherwise SequenceMatcher ratio against the
|
||||
title. Substring tiers exist because LLM-generated colloquial queries
|
||||
(e.g. "famous supply project" for "Famous-Supply Work topics") would
|
||||
otherwise score too low under pure SequenceMatcher and be treated as
|
||||
no match. Filler words like "project" are stripped from the query so
|
||||
the substring check still fires.
|
||||
"""
|
||||
q = _normalize_query(query)
|
||||
if not q:
|
||||
return 0.0
|
||||
title = _normalize(project.title)
|
||||
description = _normalize(project.description or "")
|
||||
summary = _normalize(project.auto_summary or "")
|
||||
combined = f"{title} {description} {summary}".strip()
|
||||
|
||||
if q == title:
|
||||
return 1.0
|
||||
if q in title or title in q:
|
||||
return 0.85
|
||||
if q in combined:
|
||||
return 0.70
|
||||
# SequenceMatcher against the title — comparing against `combined`
|
||||
# dilutes the ratio with long description/summary text and produces
|
||||
# uniformly low scores even for plausible matches.
|
||||
return SequenceMatcher(None, q, title).ratio()
|
||||
|
||||
|
||||
async def resolve_project(user_id: int, project_name: str):
|
||||
"""Exact-then-fuzzy project lookup. Returns the Project or None.
|
||||
"""Exact-then-scored project lookup. Returns the Project or None.
|
||||
|
||||
Resolution order:
|
||||
1. Exact title match (case-insensitive via DB)
|
||||
2. project_name is a substring of an existing title
|
||||
3. Existing title is a substring of project_name
|
||||
4. SequenceMatcher ratio >= 0.55
|
||||
1. Exact title match (case-insensitive via DB query).
|
||||
2. Highest `score_project_match` across all projects, threshold 0.55.
|
||||
"""
|
||||
from fabledassistant.services.projects import get_project_by_title, list_projects
|
||||
|
||||
proj = await get_project_by_title(user_id, project_name)
|
||||
if proj is not None:
|
||||
return proj
|
||||
needle = project_name.lower().strip()
|
||||
|
||||
all_p = await list_projects(user_id)
|
||||
best, best_score = None, 0.0
|
||||
for p in all_p:
|
||||
haystack = p.title.lower().strip()
|
||||
if needle in haystack or haystack in needle:
|
||||
return p
|
||||
best, best_r = None, 0.0
|
||||
for p in all_p:
|
||||
r = SequenceMatcher(None, needle, p.title.lower().strip()).ratio()
|
||||
if r >= 0.55 and r > best_r:
|
||||
best, best_r = p, r
|
||||
score = score_project_match(project_name, p)
|
||||
if score >= 0.55 and score > best_score:
|
||||
best, best_score = p, score
|
||||
return best
|
||||
|
||||
|
||||
|
||||
@@ -165,7 +165,11 @@ async def _resolve_entity_ids_by_name(
|
||||
"STRONGLY PREFER the *_names parameters when linking entities — the server "
|
||||
"resolves names to IDs by lookup, so you cannot accidentally invent or "
|
||||
"re-use the wrong ID. Use *_ids only when you have an exact ID returned "
|
||||
"from another tool call in this same turn."
|
||||
"from another tool call in this same turn. "
|
||||
"`task_titles` and `note_titles` must be exact titles returned by a prior "
|
||||
"search_notes call in this same turn. Do NOT pass user-typed phrases, "
|
||||
"project names, or invented titles. If you have not searched yet, call "
|
||||
"search_notes first."
|
||||
),
|
||||
parameters={
|
||||
"content": {
|
||||
|
||||
@@ -235,7 +235,13 @@ async def update_note_tool(*, user_id, arguments, **_ctx):
|
||||
|
||||
@tool(
|
||||
name="search_notes",
|
||||
description="Find notes or tasks by meaning. Returns a ranked list of matches with short previews. Use this when looking for items on a topic but you don't know the exact title. For the full body of a known item, use read_note instead.",
|
||||
description=(
|
||||
"Find notes or tasks by meaning. Returns a ranked list of matches with "
|
||||
"short previews. Use this when looking for items on a topic but you "
|
||||
"don't know the exact title. For the full body of a known item, use "
|
||||
"read_note instead. Do not include 'task', 'note', or 'project' in the "
|
||||
"`query` — use the `type` and `project` parameters instead."
|
||||
),
|
||||
parameters={
|
||||
"query": {"type": "string", "description": "A natural-language description of what you're looking for — concepts, themes, topics, or keywords"},
|
||||
"type": {"type": "string", "enum": ["note", "task"], "description": "Restrict results to only notes or only tasks. Omit to search both."},
|
||||
|
||||
@@ -135,20 +135,12 @@ async def update_project_tool(*, user_id, arguments, **_ctx):
|
||||
briefing=True,
|
||||
)
|
||||
async def search_projects_tool(*, user_id, arguments, **_ctx):
|
||||
from difflib import SequenceMatcher
|
||||
|
||||
from fabledassistant.services.projects import list_projects
|
||||
from fabledassistant.services.tools._helpers import score_project_match
|
||||
|
||||
query = str(arguments.get("query", "")).lower()
|
||||
query = str(arguments.get("query", ""))
|
||||
projects = await list_projects(user_id)
|
||||
scored: list[tuple[float, object]] = []
|
||||
for p in projects:
|
||||
combined = f"{p.title} {p.description or ''} {p.auto_summary or ''}".lower()
|
||||
base_score = SequenceMatcher(None, query, combined).ratio()
|
||||
query_words = set(query.split())
|
||||
overlap = sum(1 for w in query_words if w in combined)
|
||||
score = base_score + overlap * 0.05
|
||||
scored.append((score, p))
|
||||
scored: list[tuple[float, object]] = [(score_project_match(query, p), p) for p in projects]
|
||||
scored.sort(key=lambda x: x[0], reverse=True)
|
||||
results = []
|
||||
for score, p in scored[:5]:
|
||||
@@ -158,7 +150,7 @@ async def search_projects_tool(*, user_id, arguments, **_ctx):
|
||||
"summary_snippet": (p.auto_summary or p.description or "")[:200],
|
||||
"score": round(score, 3),
|
||||
})
|
||||
return {"type": "projects_list", "data": {"projects": results}}
|
||||
return {"success": True, "type": "projects_list", "data": {"projects": results}}
|
||||
|
||||
|
||||
@tool(
|
||||
|
||||
@@ -0,0 +1,249 @@
|
||||
"""Tests for journal closeout extraction helpers."""
|
||||
|
||||
import datetime
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def _msg(role: str, content: str, kind: str | None = None):
|
||||
"""Build a Message-like stand-in for the filter helpers."""
|
||||
metadata = {"kind": kind} if kind else None
|
||||
return SimpleNamespace(role=role, content=content, msg_metadata=metadata)
|
||||
|
||||
|
||||
def _fake_conv(conv_id: int = 1):
|
||||
return SimpleNamespace(id=conv_id)
|
||||
|
||||
|
||||
def _patch_db(messages, conv=None):
|
||||
"""Build a context manager that fakes async_session() and the two
|
||||
select() calls (conversation lookup + messages query)."""
|
||||
session = AsyncMock()
|
||||
session.execute = AsyncMock()
|
||||
conv_result = MagicMock()
|
||||
conv_result.scalar_one_or_none = MagicMock(return_value=conv)
|
||||
msg_result = MagicMock()
|
||||
scalars = MagicMock()
|
||||
scalars.all = MagicMock(return_value=list(messages))
|
||||
msg_result.scalars = MagicMock(return_value=scalars)
|
||||
session.execute.side_effect = [conv_result, msg_result]
|
||||
|
||||
session_ctx = MagicMock()
|
||||
session_ctx.__aenter__ = AsyncMock(return_value=session)
|
||||
session_ctx.__aexit__ = AsyncMock(return_value=None)
|
||||
return session_ctx
|
||||
|
||||
|
||||
def test_filter_excludes_daily_prep_messages():
|
||||
from fabledassistant.services.journal_closeout import _filter_messages
|
||||
|
||||
msgs = [
|
||||
_msg("assistant", "Good morning — here is today's plan…", kind="daily_prep"),
|
||||
_msg("user", "I want to focus on the auth refactor today."),
|
||||
_msg("assistant", "Got it. I'll keep tool calls quiet."),
|
||||
]
|
||||
kept = _filter_messages(msgs)
|
||||
contents = [m.content for m in kept]
|
||||
assert "Good morning — here is today's plan…" not in contents
|
||||
assert "I want to focus on the auth refactor today." in contents
|
||||
assert "Got it. I'll keep tool calls quiet." in contents
|
||||
|
||||
|
||||
def test_build_transcript_labels_roles_and_caps_content():
|
||||
from fabledassistant.services.journal_closeout import _build_transcript
|
||||
|
||||
msgs = [
|
||||
_msg("user", "hello"),
|
||||
_msg("assistant", "hi"),
|
||||
_msg("user", "x" * 600),
|
||||
]
|
||||
out = _build_transcript(msgs)
|
||||
lines = out.splitlines()
|
||||
assert lines[0] == "USER: hello"
|
||||
assert lines[1] == "ASSISTANT: hi"
|
||||
# Third line content truncated to 500 chars
|
||||
assert lines[2].startswith("USER: ")
|
||||
assert len(lines[2]) == len("USER: ") + 500
|
||||
|
||||
|
||||
def test_build_transcript_keeps_only_last_20_messages():
|
||||
from fabledassistant.services.journal_closeout import _build_transcript
|
||||
|
||||
msgs = [_msg("user", f"msg-{i}") for i in range(30)]
|
||||
out = _build_transcript(msgs)
|
||||
lines = out.splitlines()
|
||||
assert len(lines) == 20
|
||||
# Newest 20 means msg-10 through msg-29
|
||||
assert lines[0] == "USER: msg-10"
|
||||
assert lines[-1] == "USER: msg-29"
|
||||
|
||||
|
||||
def test_system_prompt_lists_structured_fields_to_exclude():
|
||||
"""The prompt must explicitly tell the LLM not to restate structured
|
||||
fields, so the freeform learned_summary stays a narrow lane."""
|
||||
from fabledassistant.services.journal_closeout import SYSTEM_PROMPT
|
||||
|
||||
text = SYSTEM_PROMPT.lower()
|
||||
for field in ("name", "job title", "industry", "expertise", "response style", "tone", "interests"):
|
||||
assert field in text, f"system prompt should mention '{field}'"
|
||||
assert "(nothing to note)" in SYSTEM_PROMPT
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_for_user_happy_path_appends_bullets():
|
||||
from fabledassistant.services import journal_closeout
|
||||
|
||||
yesterday = datetime.date(2026, 5, 11)
|
||||
msgs = [
|
||||
_msg("assistant", "Good morning — here's today.", kind="daily_prep"),
|
||||
_msg("user", "Skip the news section — I never read it."),
|
||||
_msg("assistant", "Noted. Will drop it."),
|
||||
]
|
||||
|
||||
with (
|
||||
patch.object(journal_closeout, "async_session", return_value=_patch_db(msgs, conv=_fake_conv())),
|
||||
patch.object(journal_closeout, "get_setting", new=AsyncMock(return_value="bg-model")),
|
||||
patch.object(journal_closeout, "generate_completion", new=AsyncMock(return_value="- User skips news section")),
|
||||
patch.object(journal_closeout, "append_observations", new=AsyncMock()) as mock_append,
|
||||
):
|
||||
await journal_closeout.run_for_user(user_id=42, yesterday=yesterday)
|
||||
|
||||
mock_append.assert_awaited_once_with(42, "- User skips news section")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_for_user_skips_when_no_conversation():
|
||||
from fabledassistant.services import journal_closeout
|
||||
|
||||
with (
|
||||
patch.object(journal_closeout, "async_session", return_value=_patch_db([], conv=None)),
|
||||
patch.object(journal_closeout, "append_observations", new=AsyncMock()) as mock_append,
|
||||
patch.object(journal_closeout, "generate_completion", new=AsyncMock()) as mock_llm,
|
||||
):
|
||||
await journal_closeout.run_for_user(user_id=42, yesterday=datetime.date(2026, 5, 11))
|
||||
|
||||
mock_append.assert_not_awaited()
|
||||
mock_llm.assert_not_awaited()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_for_user_skips_when_only_prep_message_after_filter():
|
||||
"""If only the daily_prep message exists, the in-Python filter pass
|
||||
leaves zero messages and we short-circuit before the LLM."""
|
||||
from fabledassistant.services import journal_closeout
|
||||
|
||||
msgs_post_sql = [
|
||||
_msg("assistant", "Daily prep block.", kind="daily_prep"),
|
||||
]
|
||||
with (
|
||||
patch.object(journal_closeout, "async_session", return_value=_patch_db(msgs_post_sql, conv=_fake_conv())),
|
||||
patch.object(journal_closeout, "append_observations", new=AsyncMock()) as mock_append,
|
||||
patch.object(journal_closeout, "generate_completion", new=AsyncMock()) as mock_llm,
|
||||
):
|
||||
await journal_closeout.run_for_user(user_id=42, yesterday=datetime.date(2026, 5, 11))
|
||||
|
||||
mock_append.assert_not_awaited()
|
||||
mock_llm.assert_not_awaited()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_for_user_respects_nothing_to_note_sentinel():
|
||||
from fabledassistant.services import journal_closeout
|
||||
|
||||
msgs = [_msg("user", "hi"), _msg("assistant", "hi back")]
|
||||
with (
|
||||
patch.object(journal_closeout, "async_session", return_value=_patch_db(msgs, conv=_fake_conv())),
|
||||
patch.object(journal_closeout, "get_setting", new=AsyncMock(return_value="bg-model")),
|
||||
patch.object(journal_closeout, "generate_completion", new=AsyncMock(return_value="(nothing to note)")),
|
||||
patch.object(journal_closeout, "append_observations", new=AsyncMock()) as mock_append,
|
||||
):
|
||||
await journal_closeout.run_for_user(user_id=42, yesterday=datetime.date(2026, 5, 11))
|
||||
|
||||
mock_append.assert_not_awaited()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_user_schedule_registers_closeout_when_enabled(monkeypatch):
|
||||
from fabledassistant.services import journal_scheduler as sched
|
||||
|
||||
fake_scheduler = MagicMock()
|
||||
fake_scheduler.get_job = MagicMock(return_value=None)
|
||||
monkeypatch.setattr(sched, "_scheduler", fake_scheduler)
|
||||
monkeypatch.setattr(sched, "get_journal_config", AsyncMock(return_value={
|
||||
"prep_enabled": False, # isolate the closeout job
|
||||
"closeout_enabled": True,
|
||||
"day_rollover_hour": 4,
|
||||
}))
|
||||
monkeypatch.setattr(sched, "get_user_timezone", AsyncMock(return_value="UTC"))
|
||||
|
||||
await sched.update_user_schedule(user_id=7)
|
||||
|
||||
added = [c.kwargs.get("id") for c in fake_scheduler.add_job.call_args_list]
|
||||
assert "journal_closeout_7" in added
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_user_schedule_does_not_register_closeout_when_disabled(monkeypatch):
|
||||
from fabledassistant.services import journal_scheduler as sched
|
||||
|
||||
fake_scheduler = MagicMock()
|
||||
fake_scheduler.get_job = MagicMock(return_value=None)
|
||||
monkeypatch.setattr(sched, "_scheduler", fake_scheduler)
|
||||
monkeypatch.setattr(sched, "get_journal_config", AsyncMock(return_value={
|
||||
"prep_enabled": False,
|
||||
"closeout_enabled": False,
|
||||
"day_rollover_hour": 4,
|
||||
}))
|
||||
monkeypatch.setattr(sched, "get_user_timezone", AsyncMock(return_value="UTC"))
|
||||
|
||||
await sched.update_user_schedule(user_id=7)
|
||||
|
||||
added = [c.kwargs.get("id") for c in fake_scheduler.add_job.call_args_list]
|
||||
assert "journal_closeout_7" not in added
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_closeout_catchup_skips_when_already_have_entry(monkeypatch):
|
||||
from fabledassistant.services import journal_scheduler as sched
|
||||
|
||||
monkeypatch.setattr(sched, "get_user_timezone", AsyncMock(return_value="UTC"))
|
||||
monkeypatch.setattr(sched, "get_journal_config", AsyncMock(return_value={
|
||||
"closeout_enabled": True,
|
||||
"day_rollover_hour": 0, # slot has always passed
|
||||
}))
|
||||
yesterday = (datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(days=1)).date()
|
||||
fake_profile = SimpleNamespace(observations_raw=[{"date": yesterday.isoformat(), "bullets": "..."}])
|
||||
|
||||
from fabledassistant.services import user_profile as up
|
||||
monkeypatch.setattr(up, "get_profile", AsyncMock(return_value=fake_profile))
|
||||
|
||||
run_mock = AsyncMock()
|
||||
from fabledassistant.services import journal_closeout as jc
|
||||
monkeypatch.setattr(jc, "run_for_user", run_mock)
|
||||
|
||||
await sched._closeout_catchup(user_id=7)
|
||||
run_mock.assert_not_awaited()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_closeout_catchup_runs_when_no_entry_yet(monkeypatch):
|
||||
from fabledassistant.services import journal_scheduler as sched
|
||||
|
||||
monkeypatch.setattr(sched, "get_user_timezone", AsyncMock(return_value="UTC"))
|
||||
monkeypatch.setattr(sched, "get_journal_config", AsyncMock(return_value={
|
||||
"closeout_enabled": True,
|
||||
"day_rollover_hour": 0,
|
||||
}))
|
||||
fake_profile = SimpleNamespace(observations_raw=[])
|
||||
|
||||
from fabledassistant.services import user_profile as up
|
||||
monkeypatch.setattr(up, "get_profile", AsyncMock(return_value=fake_profile))
|
||||
|
||||
run_mock = AsyncMock()
|
||||
from fabledassistant.services import journal_closeout as jc
|
||||
monkeypatch.setattr(jc, "run_for_user", run_mock)
|
||||
|
||||
await sched._closeout_catchup(user_id=7)
|
||||
run_mock.assert_awaited_once()
|
||||
@@ -0,0 +1,145 @@
|
||||
"""Tests for the tool-use fixes from the 2026-05-08 journal session."""
|
||||
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def _project(pid: int, title: str, description: str = "", auto_summary: str = ""):
|
||||
return SimpleNamespace(
|
||||
id=pid, title=title, description=description, auto_summary=auto_summary,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_search_projects_tool_returns_success_true():
|
||||
"""The dispatcher sets status from result['success']; without this key
|
||||
the call gets labeled 'error' even when data is returned."""
|
||||
from fabledassistant.services.tools import projects as projects_tool
|
||||
|
||||
fake_projects = [_project(5, "Famous-Supply Work topics", "AT&T fiber circuit")]
|
||||
|
||||
# `list_projects` is imported locally inside search_projects_tool, so we
|
||||
# patch the source module rather than the consumer.
|
||||
with patch("fabledassistant.services.projects.list_projects", new=AsyncMock(return_value=fake_projects)):
|
||||
result = await projects_tool.search_projects_tool(
|
||||
user_id=1, arguments={"query": "famous supply"},
|
||||
)
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["type"] == "projects_list"
|
||||
assert len(result["data"]["projects"]) == 1
|
||||
|
||||
|
||||
def test_strip_type_nouns_removes_task_word():
|
||||
from fabledassistant.services.notes import _strip_type_nouns
|
||||
assert _strip_type_nouns("sebring secondary task") == ["sebring", "secondary"]
|
||||
|
||||
|
||||
def test_strip_type_nouns_removes_all_variants():
|
||||
from fabledassistant.services.notes import _strip_type_nouns
|
||||
assert _strip_type_nouns("project notes task") == []
|
||||
|
||||
|
||||
def test_strip_type_nouns_case_insensitive():
|
||||
from fabledassistant.services.notes import _strip_type_nouns
|
||||
assert _strip_type_nouns("Sebring Task NOTES") == ["Sebring"]
|
||||
|
||||
|
||||
def test_strip_type_nouns_preserves_real_content_words():
|
||||
from fabledassistant.services.notes import _strip_type_nouns
|
||||
assert _strip_type_nouns("circuit configuration") == ["circuit", "configuration"]
|
||||
|
||||
|
||||
def test_strip_type_nouns_handles_empty_string():
|
||||
from fabledassistant.services.notes import _strip_type_nouns
|
||||
assert _strip_type_nouns("") == []
|
||||
assert _strip_type_nouns(" ") == []
|
||||
|
||||
|
||||
def test_score_project_match_exact_title_returns_1():
|
||||
from fabledassistant.services.tools._helpers import score_project_match
|
||||
p = _project(5, "Famous-Supply Work topics")
|
||||
assert score_project_match("Famous-Supply Work topics", p) == 1.0
|
||||
|
||||
|
||||
def test_score_project_match_colloquial_substring_at_least_85():
|
||||
"""'famous supply' is a substring of normalized 'famous supply work topics'
|
||||
after stripping the hyphen. Substring match returns 0.85."""
|
||||
from fabledassistant.services.tools._helpers import score_project_match
|
||||
p = _project(5, "Famous-Supply Work topics", auto_summary="AT&T fiber circuit")
|
||||
score = score_project_match("famous supply project", p)
|
||||
assert score >= 0.85, f"expected substring tier (>=0.85), got {score}"
|
||||
|
||||
|
||||
def test_score_project_match_query_in_summary_returns_70():
|
||||
from fabledassistant.services.tools._helpers import score_project_match
|
||||
p = _project(12, "Minstrel", auto_summary="self-hosted music server")
|
||||
assert score_project_match("music server", p) == 0.70
|
||||
|
||||
|
||||
def test_score_project_match_unrelated_returns_low():
|
||||
from fabledassistant.services.tools._helpers import score_project_match
|
||||
p = _project(12, "Minstrel", auto_summary="self-hosted music server")
|
||||
assert score_project_match("garden renovation", p) < 0.5
|
||||
|
||||
|
||||
def test_score_project_match_empty_query_returns_zero():
|
||||
from fabledassistant.services.tools._helpers import score_project_match
|
||||
p = _project(5, "Famous-Supply Work topics")
|
||||
assert score_project_match("", p) == 0.0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_search_projects_tool_ranks_substring_match_above_others():
|
||||
"""Substring hits (score 0.85) must rank above SequenceMatcher misses
|
||||
for unrelated projects."""
|
||||
from fabledassistant.services.tools import projects as projects_tool
|
||||
|
||||
fake_projects = [
|
||||
_project(12, "Minstrel", auto_summary="self-hosted music server"),
|
||||
_project(5, "Famous-Supply Work topics", auto_summary="AT&T fiber circuit"),
|
||||
_project(13, "ImageRepo", auto_summary="self-hosted Flask app"),
|
||||
]
|
||||
|
||||
with patch("fabledassistant.services.projects.list_projects", new=AsyncMock(return_value=fake_projects)):
|
||||
result = await projects_tool.search_projects_tool(
|
||||
user_id=1, arguments={"query": "famous supply project"},
|
||||
)
|
||||
|
||||
top = result["data"]["projects"][0]
|
||||
assert top["id"] == 5, f"expected Famous-Supply to rank first, got {top}"
|
||||
assert top["score"] >= 0.85
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_resolve_project_finds_colloquial_match(monkeypatch):
|
||||
"""resolve_project must surface 'Famous-Supply Work topics' when the
|
||||
user passes 'famous supply project' — substring match via the shared
|
||||
score helper, score 0.85 ≥ 0.55 threshold."""
|
||||
from fabledassistant.services.tools import _helpers
|
||||
|
||||
fake_projects = [
|
||||
_project(12, "Minstrel", auto_summary="self-hosted music server"),
|
||||
_project(5, "Famous-Supply Work topics", auto_summary="AT&T fiber circuit"),
|
||||
]
|
||||
|
||||
async def fake_get_project_by_title(uid, name):
|
||||
return None # force the scored path
|
||||
|
||||
async def fake_list_projects(uid):
|
||||
return fake_projects
|
||||
|
||||
monkeypatch.setattr(
|
||||
"fabledassistant.services.projects.get_project_by_title",
|
||||
fake_get_project_by_title,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"fabledassistant.services.projects.list_projects",
|
||||
fake_list_projects,
|
||||
)
|
||||
|
||||
result = await _helpers.resolve_project(user_id=1, project_name="famous supply project")
|
||||
assert result is not None
|
||||
assert result.id == 5
|
||||
Reference in New Issue
Block a user