* feat(memhub): add model usage audit and cost controls Co-authored-by: Cursor <cursoragent@cursor.com> * fix(memhub): typed source authorization state and managed-key registry ownership Closes #29 Issue #29 surfaced as "WorkBuddy plugin cannot be uninstalled / source sync cannot be turned on", but it is two independent failure domains that were both being reported as a single opaque `desktop_action_failed`: 1. Source sync domain: the UI could not tell whether a source failed to sync because the grant was missing, revoked, or because the source identity or root path changed. Every failure collapsed into one error, so the UI could only say "failed". 2. Host plugin install domain: WorkBuddy shares three registry files (`known_marketplaces.json`, `installed_plugins.json`, `settings.json`) with the host. MemHub recorded a whole-file hash at install time and then refused to uninstall whenever the host (or a reformat) changed any byte anywhere in the file, even in entries MemHub does not own. What changes Domain errors (new `packages/host-bridge/src/domain-error.ts`) - Closed allowlist `DOMAIN_CODES`: grant_required, source_identity_changed, host_uninstall_modified_managed_value, host_uninstall_unknown_owned_file, host_uninstall_needs_review, host_install_journal_invalid, host_install_recovery_required, host_permission_storage_unsafe. - `DomainError` + `domainCodeOf()` so callers stop string-matching on `Error.message`. `diagnosticDomainError()` emits only the code and a repo-relative safe stack, never message text, so diagnostics cannot leak paths or payloads. - Re-exported from `runtime-control` (host-bridge owns the type to avoid a dependency cycle); desktop `observeDesktopAction` now uses `domainCodeOf(error) ?? fallbackCode`. Source authorization state - `SourceAuthorizationState` enum: ok / missing_grant / revoked_grant / source_identity_changed / source_root_unavailable / candidate_changed, plus `SourceGrantStore.authorization_state()` which is read-only and fails closed. - `/api/v1/sources` now returns `authorization_state` per source; contracts and the web app render a concrete next action next to the sync switch instead of a generic failure. - `_change_source_sync` distinguishes sync_prepare_failed, sync_config_write_failed, sync_resume_failed, sync_watcher_failed (409 with the code as detail) while keeping the existing rollback behaviour. - Capture refuses silently but now records a fail-open rejection marker (`sync_disabled` / grant_missing / grant_revoked) that counts occurrences in a time window and carries no payload, path, or session content. WorkBuddy registry ownership (journal v1 -> v2) - Ownership is now the value at MemHub's own key, not the whole file. Added `canonicalJson`, `registryManagedDigest`, `registryWithoutOurs`, `registryHoldsOnlyOurs`. - Install: the plan records `keyPath` + managed digest; apply compares the managed value (CAS) instead of the whole file; the journal writes both the v1 `installed_hash` and the v2 `installed_managed`. - Uninstall: `resolveRegistryOwnership()` compares the v2 digest, falls back to the v1 hash and `isManagedRegistryEntry`, and otherwise produces a review finding instead of throwing. `managedRegistryRemoval()` deletes a file MemHub created when it still holds only our entry; removes just our key when the host has added its own content; and restores recorded bytes only when the rest of the document is unchanged, otherwise patches the current document. - v1 journals migrate: a whole-file hash mismatch no longer blocks uninstall. Tests - `packages/host-bridge/src/domain-error.test.ts` (5): code is carried, message never reaches diagnostics, no guessing on unclassified errors, refuses to parse codes out of free text, allowlist is closed. - `workbuddy-plugin-installer.test.ts` (34 total): uninstall survives a host reformat, removes only our key when the host changed something else, asks for review when a managed entry was changed, migrates a v1 journal, keeps a registry the host now uses, installs over a post-plan change. - `apps/core/tests/test_source_authorization_state.py` (12): every state, fail-closed ordering, capture rejection names, marker contains no secret or path, recording never raises. - `apps/web`: authorization note rendered for revoked grant and hidden when ok. Verification - host-bridge WorkBuddy suite 34/34; domain-error 5/5. - apps/web 1906 tests across 105 files pass. - pnpm -r typecheck clean; biome check clean on touched files. - core-fast: 232 failed / 4131 passed, identical failure count to the clean baseline (232 failed / 4119 passed) with +12 from the new tests; the failures are pre-existing and unrelated (confirmed by stashing and re-running). * feat(memhub): add a start/stop switch for model sedimentation Shipped builds gave the user no way to stop model calls: the model content permission switch is gone (removed with its endpoint in #20) and there is no pause or remove entry either, so a configured model keeps turning conversations into memories and keeps costing money. Adds a user switch in settings: stopping keeps the model configuration and the already imported conversations, leaves queued work pending instead of failing it, and stops new work, enqueueing and backfill so nothing further reaches the model. Starting again resumes the same work without re-entering a key. The flag lives in Core's own user config (config.json), so no schema migration is needed, and the supervisor takes it as an optional callback so every existing caller and test keeps its current behaviour. * test(desktop): keep host-bridge mocks whole and assert the review contract The memhub CI job failed four desktop tests after the issue #29 change. Both were test-side contracts that the new ownership model intentionally moves, not regressions in the fix itself. 1. `host-control-ownership.test.ts` mocked `@one-memory/host-bridge` with a whitelist that carried only `RecallBroker`. Main now imports `domainCodeOf`, which runtime-control re-exports from host-bridge, so the mock handed back undefined and every error path that called it threw a TypeError instead of the error the test was asserting on. The mock now spreads the real module and overrides only `RecallBroker`, which is the part it was written for. 2. `host-permissions.test.ts` asserted the pre-#29 contract, where a change anywhere in a shared registry made uninstall throw `host_uninstall_modified_files`. Ownership is now the value at our own key, so a change to that value is reported instead of guessed at: the plan schedules no removal, names the file in a review finding, and applying it refuses with `host_uninstall_needs_review`. The test keeps the guarantee it existed for, that those bytes are never overwritten, and now also asserts that apply refuses, so the safety is checked at both steps. Verification: full desktop suite 38 files / 436 tests pass, `pnpm -r typecheck` clean, biome reports only two pre-existing warnings on lines this change does not touch. * fix(logging): keep info diagnostics actionable Co-authored-by: Cursor <cursoragent@cursor.com> * test(memhub): register the sedimentation switch test in the core test inventory New core test files must appear in TEST_INDEX.md and in the classification inventory's expected count and digest, and the backfill case needs a settled turn because an unanswered user tail is not backfill material. * feat(diagnostics): expose build info and fullscreen logs Co-authored-by: Cursor <cursoragent@cursor.com> * fix(settings): align help controls and tighten spacing Co-authored-by: Cursor <cursoragent@cursor.com> * release: prepare MemHub 1.0.4 build 8 Co-authored-by: Cursor <cursoragent@cursor.com> * fix(memhub): clear pre-existing dev lint errors and flaky SDK dedup tests Three things blocking CI that came in with dev and are unrelated to the sedimentation switch itself: - biome errors in DebugLogPanel.tsx (import order) and ModelUsageView.tsx (refreshToken is an intentional refresh trigger, annotated like the existing modelVersion trigger in R6Settings). - test_extractor_prompt_override.py's fake LLM now accepts keyword args the real pipeline passes since the usage audit landed (audit_stage). - the fake embeddings in the intention dedup tests used the process-random hash(), so two different texts collided roughly one run in 32 and were misread as duplicates; switched to a deterministic md5 index. * fix(cli): count recall characters as code points A recall whose hits contain an emoji outside the BMP was reported as response_invalid even though the data and the search were fine: the CLI cross-checked the response by summing String.prototype.length, which counts UTF-16 code units, against Core's used_characters, which Python's len() counts in code points. One emoji is two units here and one character there, so any hit set with an emoji failed the check. The sibling validator in host-bridge (context-protocol.ts) already counted code points, so the CLI was the odd one out rather than a deliberate choice. - validateContextResponse now sums through characterCount(), which counts code points, matching Core. - Two regression tests: an emoji hit whose used_characters follows code points must stay grounded, and the same response counted as UTF-16 units must still be refused, so the cross-check is not simply loosened. - The contract now states used_characters counts Unicode code points and is not an encoding length, so a client in another language does not repeat the comparison. Verified by reverting the source change: the first test fails on the old implementation and passes with it. Closes #30 * fix(web): keep the model recovery entry reachable while a probe is pending Closes #40 A pending model probe fenced the configuration editor, and the only recovery entry ("核对测试结果") lived inside the area the fence kept shut. Reaching it required opening the form; opening the form required having recovered. People with a stuck probe could not change or repair their model at all. The fence itself was doing its job and still is. What was wrong is that refusal also hid the way out. R6Settings - openConfiguration() now opens the model area whether or not the editor accepts, so the recovery entry inside it is reachable. editProfile() and add() still return false while the fence is up; nothing about the protection changed. - The refusal notice is reworded to match: the configuration cannot be changed yet, and the next step is down in the area that just opened. Saying "cannot be opened" was both untrue now and unhelpful before. ModelSettings - A refused open is remembered instead of dropped. Once the fence clears, the intent is replayed, so the person lands in the form they asked for. Without this they would get a blank new-configuration form the moment the probe reconciled, which invites a duplicate entry. - Replaying only fills fields in. It never sends a request, so no test is re-sent implicitly; "核对测试结果" remains the only thing that does. Tests - R6Settings: with a current model and without one, the recovery entry is reachable while the editor is fenced, and the refusal is still reported. - ModelSettings: after reconciling, the form reopens on the profile that was asked for, and the probe is sent exactly once. - Both new R6Settings cases and the ModelSettings case fail on the previous code. * perf(runtime-control): decide runtime reuse before staging the payload Every App launch copied the whole 373MB bundle into a temp directory and hashed it twice before asking whether the selected runtime could be reused. On a machine that already runs this build the answer is yes and all of that work is thrown away: measured on a release Mac bundle, 6.9s to copy and about 3.8s of hashing (fileDigests, then verifyInstalledDistribution over the same tree). installPackagedRuntime now describes the payload in place first. The staged tree is a byte for byte copy of resources/{core,host-bridge,runtime,web} plus two generated launchers, so the same relative paths and digests can be read without copying anything. reuseSelectedSameBuild takes that description instead of a staged directory, and only a payload that is going to be installed is ever staged. The decision itself is unchanged: it still compares every file digest except bin/node, still repairs the packaged launcher when the App moved, and still raises installed_version_conflict or distribution_hash_mismatch exactly as before. Nothing was replaced by a cheaper fingerprint, because a wrong reuse is what those errors exist to prevent. Hashing also runs through a small worker pool now; the digests are identical and the manifest keeps its sorted key order. New test: describeBundlePayload must equal the manifest the staged copy produces, so moving the decision ahead of the copy cannot decide something different. Verified read-only against the installed release bundle: 1534 files, identical file sets, zero digest mismatches. * perf(desktop): raise a startup window before the runtime and Core come up Opening the App currently shows nothing at all until initialize finishes. On this machine that is 55.6s from desktop.initializing to desktop.ready (about 19s installing and starting the managed runtime, then about 36s for Core to become ready), and window.requested is logged after desktop.ready. A launch that slow with no window reads as a launch that failed, and people click the icon again. startDesktopLifecycle now takes two optional hooks. showStartupWindow runs before initialize and loads a placeholder; revealStartupWindow runs after initialize and swaps that same window onto the real app. Because the window is reused rather than replaced, there is no second window and no flash. The placeholder is served from the app protocol as /__startup, in memory, not as a file in the web bundle. It performs no IPC: it renders before initialize has registered a single handler, so anything it asked for would fail and leave the page in an error state for the whole wait. That route is also reserved, so a file of the same name inside the bundle can never be reached through it. Raising the placeholder is best effort. It only fills a wait that would otherwise be blank; failing to draw it must not stop the services or the real window. Tests: five lifecycle cases cover the ordering (placeholder before initialize, reveal after, reuse of the same window, nothing for a background launch, and a real window when the placeholder was closed or could not be raised). The four suites that mock ./app-protocol now spread the real module instead of whitelisting it, which is what broke when the protocol gained exports. host-control-ownership gains the BrowserWindow methods the placeholder path uses, and its "no window" assertion now names the placeholder, since a placeholder window is expected there by design. * fix(core): stop the sedimentation switch failing its own request Turning the switch off answered "停止未能确认,模型可能仍在整理记忆,请重试" every time. The value was saved and the error was a lie the endpoint told afterwards: `put_sedimentation` logged `memory_sedimentation.toggled` with `details={"enabled": ...}`, `enabled` is not in the diagnostic allow-list, and `_details` raises. That ValueError escaped the endpoint, so the request is recorded in this machine's log three times as `api.request.failed` / `core.request.unhandled` with `diagnostic_detail_key_invalid`, and the UI took the 500 as an unconfirmed stop. Because the raise sits between the save and the resume, starting again was broken the same way: `resume_allowed()` never ran, so work parked by a stop stayed parked. Three changes, in the order they matter: 1. `enabled` is now a legal detail key. It is a bounded boolean, the same shape as `spawned` and `index_complete`. 2. `diagnostic_event` no longer lets a rejected payload reach the caller. It already swallowed `OSError` with the comment "Diagnostics must not become a new product outage"; an unlisted key is the same failure seen from the caller, and it is exactly what turned every external recall into a 500 and then killed the process last time. The payload is still wrong and still caught in CI — now by every module rather than five. 3. `test_diagnostic_payload_keys.py` walks every module instead of a hand-picked list. The two payloads that caused real outages were in files nobody thought to add to that list; the only shape of the check that holds the line is "every module, always". This file was already failing on dev with `api.py:3673: unlisted details key 'enabled'` — the bug was known to the suite and shipped anyway. `DiagnosticLog.write` still raises, so the rejection itself is unchanged and still tested; only the callers are protected. * fix(core): let the drain claim a manual note's job A memory added by hand stayed on "原话已保存,等待整理" indefinitely, with `task_id: null`: the note's durable job was never claimed, so no task was ever created for it. On this machine the job is still `pending` with `attempts=0` more than eight hours after it was saved, while the write gate admits its source without complaint. The drain had two different answers to one question. `source_writes_permitted` is `_source_writes_on`, which admits `one-memory-manual` by its grant. The claim gate was `_config_store(app).sync_enabled`, the per-app toggle, and `one-memory-manual` is not a connected app: it never appears in the configuration document, so that predicate answers False for it. The job survived the `enabled_sources` prefilter and was then dropped row by row, on every tick, forever. Nothing ever failed and nothing ever ran. The claim gate now asks the same question the write gate asks. Admission is one decision; giving it two predicates is what parked the work. Two tests pin it. One builds the drain as the composition root does and shows the job is claimed, and shows the shipped wiring stranding the same job, so the regression is visible if the toggle comes back. The other records the asymmetry itself: `_source_writes_on` is True for the manual source while `sync_enabled` is False, which is why the pair could disagree at all. No migration and no reinstall: existing pending jobs are picked up on the next drain tick. * fix(memhub): make sedimentation pause stop model dispatch Co-authored-by: Cursor <cursoragent@cursor.com> * feat(memhub): schedule memory writes by event time Co-authored-by: Cursor <cursoragent@cursor.com> * fix(core): answer the sedimentation switch before the backlog walk Turning "用模型沉淀记忆" back on wrote the choice to the store and then awaited `resume_allowed()` inside the PUT handler. That call is not a nudge: it runs maintenance over every unfinished task and then a whole-queue scan, and the bounded scan waited -- holding the scan lock -- for the queue to drop under its cohort ceiling. With a real backlog the queue never drops, so the request never returned: one toggle in the field is still open 20+ minutes later with no `api.request.completed`, while the renderer gave up at its 30s timeout and showed "启用未能确认,仍显示上次确认的状态;请重试。" for a switch that had in fact been saved. The choice is the durable fact; the walk is follow-up work: - The handler now stores the choice and returns it, and starts the resume as a watched background task (same idiom as the other Core background walks). Repeated toggles reuse the running walk instead of stacking. - A bounded scan now stops at its ceiling instead of sleeping under the scan lock waiting for capacity, which also un-starves the periodic scan that was queued behind it. It reports `queue_saturated` rather than `no_tasks`, so the status stops claiming there is nothing to do while hundreds wait. Verified on the field log first: the failing toggle is the only request in the window with `api.request.started` and no `api.request.completed`, and its trace shows `memory_sedimentation.toggled` succeeded 6ms in at `pending_count` ~500. New tests fail on the old code (timeout) and pass now. * fix(core): stop a blank author from erasing a whole conversation A user turn whose host did not say who wrote it carries `written_by=unknown`. The write binding's allow-list had every other role/author pair but not that one, so verifying the context raised `hy_memory_write_verification_context_scope` and the task ended as `model_content_outcome_unknown` after two attempts: a memory the model may well have written, that can never be confirmed, and a conversation that is never sedimented again. This is not a rare shape. On this machine 1078 user records across 1002 WorkBuddy conversations are `unknown`, and the failures were 42 per 15 minutes in the field log — every one of them a task reporting `result_count=0` while 402 sat pending. The author is missing, not the turn: the record is still inside the same conversation, workspace and source, which is what the binding actually checks, and the turn assembly already sent it. Accepting it here puts the verification back in agreement with what was sent, the same reasoning that added ("user", AGENT) for sub agent questions. New test fails on the old code at hy_source_context.py:76 — the same frame as the field stack — and passes now. * fix(core): stop an abandoned read reporting a failure nobody owns `in_read_pool` shields the wrapped worker, so a lane that runs out of budget gives up on the answer without yanking a query out from under SQLite. The caller cancels; the read runs on. When such an abandoned read then failed, nothing awaited its future any more: `shield` drops its inner callback as soon as the outer future is cancelled, so the inner future finished with an exception that nobody retrieved. asyncio reports that at GC, and the loop handler files it as `core.loop.task_exception` -- a red error sitting next to a lane that had already degraded on its own budget and already said so. On this machine that is the `raw_context_timeout` pair in the field log: a Future, not a Task, carrying TimeoutError('raw_context_timeout'), reported three times in half an hour. The lane result itself was right; only the diagnostics were arguing with themselves. Retrieving the outcome on completion keeps the diagnostics honest without hiding anything: awaiting the read still raises for a caller who waited, which the second case pins down. The new case fails on the previous code (it reports the TimeoutError) and passes now. * fix(core): one task with no inputs no longer strands every unknown task `MemoryTaskRunner._request` falls back to `chunk[0]` when no record is a person-written user turn. A task whose inputs are gone yields an empty chunk, so that fallback raised IndexError -- an error no caller can read. A reconcile pass catches domain errors per task and moves on, so IndexError did not skip the task: it ended the pass, and every unknown task behind it stayed unknown while the retry backed off 1, 2, 4 ... 128 and gave up. On this machine 68 of 303 unknown tasks had no inputs, which is exactly the `reconcile_unknown / error_type=IndexError` backing off in the field log. Those 68 cannot be reconciled either way -- there is nothing to ask HY about -- but the 235 that can must not be held hostage to them, and this is what stands between a task whose model write may already have landed and its confirmation. An empty chunk is now a domain error, so the pass skips the task and continues. Also folds in one formatting hunk left over in this file from the previous change, so the formatter is clean again. The new case fails on the previous code at memory_processing.py:1036 with the same IndexError the log shows, and passes now. * chore(memhub): bump release build number to 10 The installer refuses a package whose bytes differ while its build number stays the same, so the four Core fixes on dev need their own number to be installable over the 1.0.4 / build 9 package already on the machine. * fix(core): retire a window whose remaining rows FIFO refuses The event-time window asked only whether its ten-minute range still held *some* pending row before deciding the anchor was still active. A pending row that FIFO holds back is not claimable: it may be blocked by an earlier sibling of the same Harness whose own event time sits outside the range, and that sibling can never be claimed from inside this window. The anchor then stayed pinned forever and the whole queue starved. Live shape on one machine: the anchor sat on 2026-10-07T03:24Z while the four pending rows inside it were blocked by 929/930/1243/2347 earlier workbuddy jobs, the earliest of them dated 2026-03-12. Completions stopped at 09:59 and 3.8k queued turns sat at zero output; restarting does not help because the anchor is persisted. The contract (docs/contracts/memory-event-time-scheduling-v1.md) retires a window once every *eligible* pending/running row in the range has settled or parked, and re-anchors on the next oldest eligible job. Reuse the claim's own predicate — free lease, no busy Harness, no earlier unsent sibling — so "the window is busy" and "the window can still be worked" finally agree. Anchor selection and in-window selection already used it; only the liveness check did not. * fix(core): a refused source context is a failure, not an unknown write Issue #44: an OpenCode import carried `role=user, written_by=unknown`. The role/author set now admits it, but the row still never moved: `HyWriteBindings` is built inside `ingest`, and the invocation marker is recorded *before* that call, so the refusal arrived with the marker already set. The classifier checked `hy_invoked` first and parked the task as `unknown` — a state that means "HY may hold a write we cannot confirm" about a request that provably never reached HY. The reason stayed off the row, the task could not be re-driven, and the queue behind it stopped. `hy_memory_write_verification_context_scope` is raised from that construction alone, and `_check_source_context` (which does run after the call) raises `hy_memory_evidence_changed` instead, so the code is unambiguous: nothing was sent. It joins `PRE_SEND_REFUSAL_CODES`, and both classifiers now test that set before the marker: the durable path settles `failed` with the marker cleared so reclaim does not treat a row HY never saw as possibly-sent, and the task path fails with the code instead of marking unknown. Regression covers both directions: a pre-call refusal must end `failed` with `hy_invoked=0`, and a post-call refusal (`authority_invalid`) must still be `unknown` — the fix narrows the unknown set, it does not widen the failure set. * chore(memhub): bump release build number to 11 Carries the window starvation fix (dc0ba49) and the pre-send refusal fix (0df84c0). A new build number keeps the package distinguishable from build 10 and lets it install over the current one. * refactor(memhub): simplify durable memory scheduling Co-authored-by: Cursor <cursoragent@cursor.com> * chore(memhub): bump release build number to 12 Co-authored-by: Cursor <cursoragent@cursor.com> * fix(security): parse diagnostic stack paths linearly Co-authored-by: Cursor <cursoragent@cursor.com> * fix(web): sync sedimentation switch status to home (#51) --------- Co-authored-by: alvinfei <alvinfei@tencent.com> Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: kristinhe <kristinhe@tencent.com>
1527 lines
62 KiB
Python
1527 lines
62 KiB
Python
"""
|
||
Agent Memory - Extractor 提取智能体
|
||
|
||
统一 Prompt 提取: 单次 LLM 调用提取 memory / intentions / basic_info。
|
||
- memory: 一切值得长期记忆的信息(偏好/观点/事件/计划/经历/习惯/决定)
|
||
- intentions: 用户未来要做的前瞻意图(带 valid_until 截止日)
|
||
- basic_info: 结构化基础画像 kv
|
||
|
||
LLM 只需感知 memory 与 intention 两类,无需感知内部分层(layer)。
|
||
|
||
另保留 deep_extract (System 2 深度分析) 供后台精炼使用。
|
||
"""
|
||
|
||
from typing import Dict, Any, Optional, List
|
||
from dataclasses import dataclass, field
|
||
from enum import Enum
|
||
import json
|
||
import logging
|
||
import re
|
||
|
||
from .llm_provider import LLMProvider, _content_fingerprint, _log_llm_content_enabled
|
||
from ..config import LLMConfig as GlobalLLMConfig
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
|
||
def _sentence_parts_for_dedup(text: str) -> List[str]:
|
||
"""Split complete sentences without breaking initials in personal names."""
|
||
return [
|
||
part.strip()
|
||
for part in re.split(r"(?<=[.!?])\s+(?=[A-Z])", str(text or "").strip())
|
||
if part.strip()
|
||
]
|
||
|
||
|
||
def _content_words_for_dedup(text: str) -> set:
|
||
stop = {
|
||
"the", "a", "an", "and", "or", "of", "to", "in", "on", "at", "for",
|
||
"is", "was", "were", "has", "have", "had", "he", "she", "they", "it",
|
||
"his", "her", "their", "user",
|
||
}
|
||
return {
|
||
word for word in re.findall(r"[a-z0-9]+", str(text or "").casefold())
|
||
if len(word) > 2 and word not in stop
|
||
}
|
||
|
||
|
||
def dedup_internal_memory_sentences(text: str) -> str:
|
||
"""Drop only later complete sentences that are near-contained paraphrases."""
|
||
parts = _sentence_parts_for_dedup(text)
|
||
if len(parts) < 2:
|
||
return text
|
||
keep = [True] * len(parts)
|
||
words = [_content_words_for_dedup(part) for part in parts]
|
||
for later in range(1, len(parts)):
|
||
short = words[later]
|
||
if len(short) < 8:
|
||
continue
|
||
for earlier in range(later):
|
||
long = words[earlier]
|
||
if not long:
|
||
continue
|
||
coverage = len(short & long) / len(short)
|
||
jaccard = len(short & long) / max(1, len(short | long))
|
||
if coverage >= 0.75 and jaccard >= 0.45:
|
||
keep[later] = False
|
||
break
|
||
return " ".join(part for part, include in zip(parts, keep) if include)
|
||
|
||
|
||
# ================================================================
|
||
# 数据结构
|
||
# ================================================================
|
||
|
||
|
||
class ExtractMode(str, Enum):
|
||
"""提取模式"""
|
||
|
||
LIGHT = "light" # System 1 轻量提取
|
||
DEEP = "deep" # System 2 深度提取
|
||
|
||
|
||
@dataclass
|
||
class ExtractResult:
|
||
"""提取结果 (V1 兼容)"""
|
||
|
||
success: bool
|
||
info: Dict[str, Any] = field(default_factory=dict)
|
||
suggested_layer: Optional[str] = None
|
||
confidence: float = 0.0
|
||
tokens_used: int = 0
|
||
prompt_tokens: int = 0
|
||
completion_tokens: int = 0
|
||
error: Optional[str] = None
|
||
error_code: Optional[str] = (
|
||
None # "EMPTY_RESPONSE" / "JSON_PARSE_FAILED" / "LLM_ERROR"
|
||
)
|
||
raw_response: Optional[str] = (
|
||
None # LLM 原始返回(失败时保留,用于写 pipeline_log)
|
||
)
|
||
llm_finish_reason: str = ""
|
||
llm_model: str = ""
|
||
llm_response_id: str = ""
|
||
llm_choices_count: int = 0
|
||
llm_content_len: int = 0
|
||
llm_tool_calls_count: int = 0
|
||
# 推理模型诊断:reasoning_tokens 接近/等于 max_tokens 且 content 为空,
|
||
# 说明思维链吃光了输出配额(hy3 需传 reasoning_effort=no_think)。
|
||
llm_reasoning_tokens: int = 0
|
||
llm_reasoning_len: int = 0
|
||
tool_calls_only: bool = False # True 当 LLM 只返回了 tool_calls,content 为空
|
||
attempts: List[Dict[str, Any]] = field(
|
||
default_factory=list
|
||
) # LLM 重试各次失败明细(来自 LLMRetryError.attempts),写入 EXTRACT trace
|
||
_actual_prompt: Optional[str] = (
|
||
None # 实际发送给 LLM 的 user prompt(用于 trace 日志)
|
||
)
|
||
_actual_system_prompt: Optional[str] = None # 实际的 system prompt
|
||
|
||
|
||
@dataclass
|
||
class V2ExtractResult:
|
||
"""V2 提取结果 (丰富结构)"""
|
||
|
||
success: bool
|
||
mode: ExtractMode = ExtractMode.LIGHT
|
||
|
||
# 事实提取
|
||
facts: List[Dict[str, Any]] = field(default_factory=list)
|
||
|
||
# 实体与关系
|
||
entities: List[Dict[str, Any]] = field(default_factory=list)
|
||
relations: List[Dict[str, Any]] = field(default_factory=list)
|
||
|
||
# 情绪标注
|
||
emotional_valence: float = 0.0
|
||
emotional_arousal: float = 0.0
|
||
|
||
# 时间信息
|
||
temporal_expressions: List[Dict[str, Any]] = field(default_factory=list)
|
||
|
||
# 意图检测
|
||
intentions: List[Dict[str, Any]] = field(default_factory=list)
|
||
|
||
# 画像信息
|
||
profile_updates: List[Dict[str, Any]] = field(default_factory=list)
|
||
|
||
# 更新类型判断 (深度模式)
|
||
update_classifications: List[Dict[str, Any]] = field(default_factory=list)
|
||
|
||
tokens_used: int = 0
|
||
error: Optional[str] = None
|
||
|
||
|
||
# ================================================================
|
||
# Prompt 模板
|
||
# ================================================================
|
||
|
||
# 统一提取 Prompt
|
||
#
|
||
# 设计要点(v0.3.45_v0 post8):
|
||
# - 不再输出 basic_info 字段;稳定结构化属性(name/age/location/occupation/employer)
|
||
# 由 LLM 通过 function-calling tool `update_basic_user_profile` 表达。
|
||
# - 不再输出 time_ref 字段。时间相关信息如果是时间敏感的(schedule / past event),
|
||
# 让 LLM 把已解析的时间自然嵌入 content 即可;稳定偏好类 content 保持不含时间。
|
||
|
||
# System prompt(固定,不参与 format)
|
||
EXTRACT_SYSTEM_PROMPT = """# ROLE
|
||
|
||
You are a Memory Extractor.
|
||
|
||
Extract durable, high-value memories from conversations and convert them into structured, self-contained statements.
|
||
|
||
Only extract information that is likely to matter in future conversations.
|
||
|
||
---
|
||
|
||
# INPUTS
|
||
|
||
You will receive:
|
||
|
||
* Last k Messages
|
||
* New Messages
|
||
* Memory Date
|
||
* Current Date
|
||
|
||
Use:
|
||
|
||
* Last k Messages only for coreference resolution
|
||
* Memory Date as the ONLY temporal anchor
|
||
* Current Date only as metadata, never for resolving relative time
|
||
|
||
---
|
||
|
||
# WHAT TO EXTRACT
|
||
|
||
Extract ONLY genuinely new information introduced in the NEW user messages.
|
||
|
||
Good memories include:
|
||
|
||
* preferences
|
||
* long-term projects
|
||
* habits
|
||
* goals
|
||
* recurring activities
|
||
* opinions
|
||
* relationships
|
||
* important experiences
|
||
* stable traits
|
||
* future plans
|
||
* decisions
|
||
* constraints
|
||
* accepted recommendations
|
||
|
||
Do NOT extract:
|
||
|
||
* small talk
|
||
* acknowledgements
|
||
* generic emotions
|
||
* temporary conversational context
|
||
* assistant reminders of old memories
|
||
* information already implied by prior context
|
||
* vague personality judgments
|
||
|
||
---
|
||
|
||
# SUBJECT
|
||
|
||
Every memory must be self-contained, written in third person, with an explicit
|
||
subject. Which subject to use is decided by **who spoke**:
|
||
|
||
| Speaker | Subject to write | Example |
|
||
| --- | --- | --- |
|
||
| `role: "user"` | the user | "The user ..." / "用户..." |
|
||
| any speaker with a `name` other than the user (e.g. `name: "John"`) | that person, by name | "John ..." |
|
||
| `role: "assistant"` with NO `name` | the AI assistant | "The assistant ..." |
|
||
|
||
The two input sections express the speaker differently:
|
||
|
||
* **New Messages** is a JSON array — read `role` and the optional `name` field.
|
||
* **Last k Messages** is rendered text — the speaker is the bracket label,
|
||
`[user]:` / `[John]:` / `[assistant]:`, optionally preceded by one line
|
||
"The user's name is Tim" / "用户名叫Tim".
|
||
|
||
Rules:
|
||
|
||
* The user is the person these memories belong to. Even when the user's own name
|
||
is known (from `name` on their message, or from the leading name line), still
|
||
write "The user ...", NEVER "Tim ...".
|
||
* **`role: "assistant"` WITH a `name` is another real person, not the AI.**
|
||
Write their memories with their name as the subject: "John ...".
|
||
* Only a nameless `role: "assistant"` is the AI assistant.
|
||
* Never invent a name for the AI assistant, it has none.
|
||
|
||
---
|
||
|
||
# LANGUAGE RULES
|
||
|
||
* Output language must match input language.
|
||
* Preserve original scripts and technical terms exactly.
|
||
* Never transliterate names, brands, or equations.
|
||
|
||
---
|
||
|
||
# TWO KINDS OF MEMORY
|
||
|
||
You extract two kinds of memory: **memory** and **intentions**.
|
||
|
||
## memory
|
||
|
||
Everything durable and worth remembering about the user — and about any other
|
||
named person in the conversation — goes here:
|
||
|
||
* preferences, opinions, values, attitudes
|
||
* events, actions, experiences
|
||
* habits, behavioral patterns
|
||
* relationships, traits
|
||
* objective facts, decisions, constraints
|
||
* ongoing activities
|
||
|
||
Implicit preferences MUST be inferred when strongly suggested
|
||
(e.g. resuming an activity → positive interest; avoiding something →
|
||
negative preference; repeated voluntary effort → sustained interest).
|
||
|
||
Do NOT separate "what kind of person the user is" from "what happened".
|
||
Both go into `memory`.
|
||
|
||
## intentions
|
||
|
||
A forward-looking thing the user **plans or intends to do in the future**,
|
||
which can be acted on and which becomes irrelevant once done or past.
|
||
|
||
Examples:
|
||
|
||
* "The user has a job interview next Tuesday."
|
||
* "The user plans to move apartments at the end of the month."
|
||
* "The user intends to finish the report before Friday."
|
||
|
||
NOT intentions (these belong in `memory`):
|
||
|
||
* stable preferences / traits ("The user likes basketball")
|
||
* completed past events ("The user moved to Beijing last year")
|
||
* general goals with no actionable future step
|
||
|
||
### valid_until
|
||
|
||
For each intention, provide `valid_until`: the date after which the
|
||
intention no longer matters (the deadline / target date), resolved
|
||
against **Memory Date** (e.g. "next Tuesday" → that absolute date).
|
||
If no deadline can be determined, set `valid_until` to `null`.
|
||
|
||
---
|
||
|
||
# OWNER
|
||
|
||
Every memory and intention MUST carry an `owner` saying which memory store it
|
||
belongs to. There are exactly two values:
|
||
|
||
* `"user"` — anything belonging to the user's own memory store. This includes
|
||
facts/preferences/traits about the user themselves, AND facts about other
|
||
named real people in the conversation, because those are still things the
|
||
user's memory should hold.
|
||
* `"agent"` — information the AI assistant (a nameless `role: "assistant"`)
|
||
provides (recommendations, confirmations, things it researched), or a future
|
||
action the user wants the **assistant** to perform ("remind me in 8 hours").
|
||
|
||
A named person is NEVER `owner: "agent"` — they are a real person, not the AI,
|
||
even though their messages carry `role: "assistant"`. Their memories are
|
||
`owner: "user"` with their name as the content subject.
|
||
|
||
Decide `owner` per item. When unsure, default to `"user"`.
|
||
|
||
---
|
||
|
||
# TEMPORAL RULES
|
||
|
||
Resolve relative time using Memory Date.
|
||
|
||
Examples:
|
||
|
||
* "next week"
|
||
* "last month"
|
||
* "tomorrow"
|
||
|
||
If time cannot be resolved safely:
|
||
|
||
* remove the relative wording
|
||
* keep the memory atemporal
|
||
|
||
Do not invent chronology.
|
||
|
||
**Just-happened events are events — keep them.** When the user reports something as
|
||
just completed ("just got back", "just finished", "this morning", "yesterday"), the
|
||
completion date is the Memory Date. Emit the **event itself** with that date; do not
|
||
fold it into a background statement.
|
||
|
||
* "I just got back from a trip with my family"
|
||
→ "The user returned from a family trip on <Memory Date>."
|
||
NOT → "The user takes family trips." (the return date is the answer to "when did
|
||
they get back", and dropping it makes the question unanswerable)
|
||
|
||
Where an event has both a start and an end that the conversation gives separately,
|
||
record both rather than collapsing them into one span.
|
||
|
||
---
|
||
|
||
# QUALITY RULES
|
||
|
||
Each memory must be:
|
||
|
||
* self-contained
|
||
* contextually complete
|
||
* retrieval-friendly
|
||
* information-dense
|
||
|
||
Retain important details:
|
||
|
||
* names
|
||
* products
|
||
* locations
|
||
* numbers
|
||
* equations
|
||
* constraints
|
||
|
||
Never generalise a specific into a category. Write what was actually said.
|
||
|
||
* "Banff" → keep "Banff", NOT "the Rocky Mountains"
|
||
* "a 3-year-old border collie named Luna" → keep the breed, age and name
|
||
|
||
**Specifics supplied by the other speaker still count.** A fact is often built across
|
||
turns: the other speaker names the thing, the user confirms it. The subject rules
|
||
decide *whose* memory it is — they do not license dropping detail.
|
||
|
||
* [Sam]: "Did you take advantage of the skiing opportunities in Banff?"
|
||
[user]: "Yeah, it was great for skiing!"
|
||
→ "The user went skiing in Banff and found the snow amazing."
|
||
NOT → "The user enjoys winter sports." (both the place and the activity were
|
||
established, one by each speaker)
|
||
|
||
Avoid fragmentary memories lacking context.
|
||
|
||
Choose the smallest number of memories that still preserves distinct future
|
||
retrieval needs.
|
||
|
||
* Compression means combining, not omitting. Every explicit durable proposition
|
||
in the new messages must appear exactly once in the output.
|
||
* Combine details when they share the same subject and the same real-world event,
|
||
state, preference, plan, or decision. A coherent event may use 1–3 sentences.
|
||
* Do not emit both a broad statement and narrower restatements of the same evidence.
|
||
* Split only when the subject differs, the events happened independently, or the
|
||
memories describe different states.
|
||
* Keep a cause, recommendation, or consequence with the fact it explains when
|
||
separating them would lose the relationship.
|
||
* Never merge separate dated occurrences merely because they share a topic.
|
||
* Preserve the source's time precision. Do not turn vague phrases such as "last
|
||
weekend" or "last summer" into exact calendar dates unless the date is explicit
|
||
or uniquely determined; "the weekend before <Memory Date>" is acceptable.
|
||
|
||
---
|
||
|
||
# TAGS
|
||
|
||
Each memory must include 1–3 concise tags.
|
||
- Topical keywords stay lowercase (travel, health, work).
|
||
- Proper nouns keep capitalization (Jasper, Canada, Banff, Prius).
|
||
|
||
Examples:
|
||
|
||
* work
|
||
* travel
|
||
* Jasper
|
||
* Canada
|
||
* health
|
||
|
||
---
|
||
"""
|
||
|
||
STRUCTURE_METADATA_PROMPT_EN = """
|
||
|
||
---
|
||
|
||
# STRUCTURAL ORGANIZATION
|
||
|
||
For every `memory`, also output:
|
||
|
||
* `topic_paths`: a backward-compatible list of zero to three broad-to-specific
|
||
semantic paths.
|
||
* `event_facet`: an object with only `subject`, `participants`, `action`,
|
||
`objects`, `locations`, and `state`; state is `planned`, `ongoing`,
|
||
`completed`, or `unknown`.
|
||
* `topic_assignments`: zero to six subject-specific memberships. Each item is
|
||
`{subject, role, path}`, where `path` is a list of `{name, type}` segments and
|
||
`type` is one of `topic`, `facet`, `series`, or `episode`. Use `topic` for a
|
||
broad domain, `facet` for a concrete activity/semantic aspect, `series` only
|
||
for repeated occurrences of the same concrete activity/state line, and
|
||
`episode` only for one bounded real-world occurrence. A shared event may have
|
||
one assignment per explicit participant while keeping one canonical memory.
|
||
Types follow `topic -> facet -> series -> episode`; levels may be skipped but
|
||
types may not repeat or move backward.
|
||
|
||
This extractor sees only the current write and therefore must not emit a
|
||
`series` segment. Emit Topic/Facet/Episode here. Reconcile may later establish a
|
||
Series after comparing independent stored occurrences.
|
||
|
||
This is write-time organization. Do not infer Series/Episode from path depth,
|
||
manufacture hierarchy from a proper noun or broad tag, or assign a person only
|
||
because they co-occur in unrelated context. Stop at the most specific supported
|
||
level and use empty values when uncertain. Never encode query forms such as
|
||
"when", "how many", "who", plurality, or ordinal wording.
|
||
"""
|
||
|
||
# Few-shot examples (appended to the system prompt only when few_shot_enabled).
|
||
# Each example mirrors the real input shape (Last k Messages / New Messages /
|
||
# Memory Date) and our output schema (memory / intentions / basic_info with
|
||
# owner & tags), and ends with a one-line takeaway.
|
||
EXTRACT_FEW_SHOT_EN = """
|
||
---
|
||
|
||
# EXAMPLES
|
||
|
||
## Example 1 — Boundary: old context is for resolution only, never re-extracted
|
||
|
||
Last k Messages:
|
||
[{"role": "user", "content": "I've been looking at rescue dogs at the shelter downtown."},
|
||
{"role": "assistant", "content": "That's lovely — found one you like?"}]
|
||
New Messages:
|
||
[{"role": "user", "content": "Yeah, picked her up yesterday — a 3-year-old border collie named Luna."}]
|
||
Memory Date: 2025-04-10
|
||
|
||
Output:
|
||
```json
|
||
{
|
||
"memory": [
|
||
{"content": "The user adopted a 3-year-old border collie named Luna from a downtown shelter on 2025-04-09.", "tags": ["pets", "family"], "owner": "user"}
|
||
],
|
||
"intentions": [],
|
||
"basic_info": {}
|
||
}
|
||
```
|
||
The old messages only resolve "her" and supply context ("downtown shelter"). Do NOT emit a stale memory like "the user is considering adopting a dog" — only the genuinely new fact (the completed adoption) is extracted, with "yesterday" resolved against Memory Date.
|
||
|
||
## Example 2 — Condense a long assistant reply
|
||
|
||
New Messages:
|
||
[{"role": "user", "content": "Write me a short essay on why remote work boosts productivity."},
|
||
{"role": "assistant", "content": "Sure — opening: 'Remote work has reshaped how teams operate...' [~2000 words of body omitted] ...closing: 'In short, autonomy and fewer interruptions drive higher output.'"}]
|
||
Memory Date: 2025-03-22
|
||
|
||
Output:
|
||
```json
|
||
{
|
||
"memory": [
|
||
{"content": "The assistant drafted a short essay for the user arguing that remote work boosts productivity because autonomy and fewer interruptions drive higher output.", "tags": ["writing", "remote-work"], "owner": "agent"}
|
||
],
|
||
"intentions": [],
|
||
"basic_info": {}
|
||
}
|
||
```
|
||
The assistant's reply is long. Never store the verbatim essay — distill it to topic + thesis. owner is "agent" because the assistant produced it. Bloated verbatim content makes memory unusable.
|
||
|
||
## Example 3 — Composite extraction: basic_info + intention + condensed agent plan
|
||
|
||
New Messages:
|
||
[{"role": "user", "content": "Hi, I'm Kenji. I'm planning a trip to Japan next month — could you put together a 7-day itinerary?"},
|
||
{"role": "assistant", "content": "# 7-Day Classic Japan Golden Route (Tokyo → Kyoto → Nara → Osaka)\\nFly into Tokyo, Shinkansen south to Kyoto, day-trip Nara, depart from Osaka (Kansai).\\n\\n## Day 1: Arrival in Tokyo\\n- Land at Haneda/Narita, drop bags at hotel near Tokyo Station.\\n- Late afternoon: Sensoji Temple (Asakusa), wander Nakamise Street, try taiyaki.\\n- Evening: Shibuya Crossing + Shibuya Sky rooftop for sunset; izakaya dinner (yakitori, sake).\\n\\n## Day 2: Tokyo markets & landmarks — [omitted]\\n## Day 3: Shinkansen to Kyoto, Fushimi Inari, Gion — [omitted]\\n## Day 4: Kyoto temples (Kiyomizu-dera, Kinkaku-ji, Ginkaku-ji) — [omitted]\\n## Day 5: Nara day trip (Nara Park deer, Todai-ji) — [omitted]\\n## Day 6: Osaka (Osaka Castle, Dotonbori street food) — [omitted]\\n## Day 7: Osaka sightseeing + depart from Kansai Airport — [omitted]\\n\\nTip: buy a 7-day JR Pass before arriving. Want me to adjust anything?"}]
|
||
Memory Date: 2025-09-15
|
||
|
||
Output:
|
||
```json
|
||
{
|
||
"memory": [
|
||
{"content": "The assistant created a 7-day Japan itinerary for the user along the classic golden route: Tokyo (Asakusa, Shibuya), Shinkansen to Kyoto (Fushimi Inari, Kiyomizu-dera, Kinkaku-ji, Gion), a Nara day trip (deer park, Todai-ji), then Osaka (Osaka Castle, Dotonbori), departing from Kansai Airport.", "tags": ["travel", "japan", "itinerary"], "owner": "agent"}
|
||
],
|
||
"intentions": [
|
||
{"content": "The user plans to take a 7-day trip to Japan in October 2025.", "tags": ["travel", "japan"], "valid_until": "2025-10-31", "owner": "user"}
|
||
],
|
||
"basic_info": {"name": "Kenji"}
|
||
}
|
||
```
|
||
One turn produces three outputs in different buckets: the name goes to basic_info, the trip is a forward-looking intention with a deadline, and the assistant's long multi-section itinerary is condensed into ONE dense agent memory — the route and key stops per city, NOT every day's bullets, tips, or food lists. Storing the whole reply verbatim would bloat memory; storing the skeleton keeps it queryable.
|
||
|
||
## Example 4 — A later message confirming/adopting an earlier recommendation is itself a fact
|
||
|
||
New Messages:
|
||
[{"role": "user", "content": "My 6-year-old won't eat any vegetables. Any ideas?"},
|
||
{"role": "assistant", "content": "Try blending spinach or carrots into pasta sauce — the color and flavor mostly disappear, and many kids accept it without noticing."},
|
||
{"role": "user", "content": "Oh nice, we'll try that tonight. By the way, what's a good bedtime for that age?"},
|
||
{"role": "assistant", "content": "For a 6-year-old, aim for 7–9 PM with about 10–11 hours of sleep."}]
|
||
Memory Date: 2025-06-20
|
||
|
||
Output:
|
||
```json
|
||
{
|
||
"memory": [
|
||
{"content": "The user has a 6-year-old child who refuses to eat vegetables, and accepted the assistant's suggestion to blend spinach or carrots into pasta sauce to hide them.", "tags": ["parenting", "food"], "owner": "user"},
|
||
{"content": "The assistant advised the user that a 6-year-old should aim for a 7–9 PM bedtime with about 10–11 hours of sleep.", "tags": ["parenting", "sleep"], "owner": "agent"}
|
||
],
|
||
"intentions": [],
|
||
"basic_info": {}
|
||
}
|
||
```
|
||
In the third message the user said "we'll try that tonight" — explicitly adopting the earlier suggestion. That acceptance is valuable and must be recorded ("accepted the assistant's suggestion to ..."), not discarded as small talk. The bedtime advice was just given and not yet adopted, so it is recorded as advice (owner "agent").
|
||
|
||
## Example 5 — RAG/injected context: never store the injected text
|
||
|
||
New Messages:
|
||
[{"role": "user", "content": "[Retrieved context: The Eiffel Tower is 330m tall and was completed in 1889.] Given the above, I'm going to propose to my girlfriend at the top of the Eiffel Tower this summer."}]
|
||
Memory Date: 2025-05-02
|
||
|
||
Output:
|
||
```json
|
||
{
|
||
"memory": [],
|
||
"intentions": [
|
||
{"content": "The user plans to propose to his girlfriend at the top of the Eiffel Tower in summer 2025.", "tags": ["relationship", "travel"], "valid_until": "2025-08-31", "owner": "user"}
|
||
],
|
||
"basic_info": {}
|
||
}
|
||
```
|
||
The bracketed retrieved context (the tower's height and year) is RAG-injected reference material, NOT something the user told us about themselves — do NOT store it. Only the user's own forward-looking plan is extracted. System/RAG-injected text is context for the model, never a memory.
|
||
|
||
## Example 6 — Shared reference material: extract the content, not the sharing action
|
||
|
||
New Messages:
|
||
[{"role": "user", "content": "Remember my grandma's dumpling recipe so I don't lose it. Filling: 500g ground pork, 300g napa cabbage, 2 tbsp soy sauce, 1 tbsp sesame oil, a thumb of ginger minced. Cabbage salted 15 min then squeezed dry. Boil dumplings, add cold water twice, ready when they float."},
|
||
{"role": "assistant", "content": "Got it, saved!"}]
|
||
Memory Date: 2025-07-01
|
||
|
||
Output:
|
||
```json
|
||
{
|
||
"memory": [
|
||
{"content": "The user's grandmother's dumpling recipe: filling of 500g ground pork, 300g napa cabbage (salted 15 minutes then squeezed dry), 2 tbsp soy sauce, 1 tbsp sesame oil, and a thumb of minced ginger; boil the dumplings, add cold water twice, and they are done when they float.", "tags": ["food", "recipe", "family"], "owner": "user"}
|
||
],
|
||
"intentions": [],
|
||
"basic_info": {}
|
||
}
|
||
```
|
||
Extract the actual factual content (ingredients, quantities, the salt-and-squeeze step, the boiling method) — NOT "the user shared a recipe" or "the user asked to remember the recipe". The sharing action is worthless; the content is what's queryable.
|
||
|
||
## Example 7 — Structured numbers and attributes preserved exactly
|
||
|
||
New Messages:
|
||
[{"role": "user", "content": "Logging my home-gym PRs this cycle: Squat 140kg x 3, Bench 100kg x 5, Deadlift 180kg x 2, Overhead Press 60kg x 6. Bodyweight 78kg, 3 sets per lift, 3 min rest."},
|
||
{"role": "assistant", "content": "Logged — strong numbers!"}]
|
||
Memory Date: 2026-01-20
|
||
|
||
Output:
|
||
```json
|
||
{
|
||
"memory": [
|
||
{"content": "The user's current lifting PRs are Squat 140kg x 3, Bench 100kg x 5, Deadlift 180kg x 2, and Overhead Press 60kg x 6, at a bodyweight of 78kg.", "tags": ["fitness", "strength"], "owner": "user"},
|
||
{"content": "The user trains with 3 sets per lift and 3 minutes of rest between sets.", "tags": ["fitness", "training"], "owner": "user"}
|
||
],
|
||
"intentions": [],
|
||
"basic_info": {}
|
||
}
|
||
```
|
||
Every number and unit (kg, reps, sets, rest time) is preserved exactly — dropping them would destroy the most queryable information. Split into two clean topics (PRs vs. training protocol), but never round or omit the specifics.
|
||
|
||
## Example 8 — Long multi-topic conversation: do not stop after the first topic
|
||
|
||
New Messages:
|
||
[{"role": "user", "content": "Big week — I finally defended my PhD thesis on Tuesday!"},
|
||
{"role": "assistant", "content": "Huge congrats! How do you feel?"},
|
||
{"role": "user", "content": "Relieved! I also signed a lease on an apartment in Berlin and move next month. And I've started learning German on Duolingo — 30-day streak. My partner Sofia is staying in Madrid for now, which is hard."}]
|
||
Memory Date: 2025-11-08
|
||
|
||
Output:
|
||
```json
|
||
{
|
||
"memory": [
|
||
{"content": "The user defended their PhD thesis on 2025-11-04.", "tags": ["education", "milestone"], "owner": "user"},
|
||
{"content": "The user signed a lease on an apartment in Berlin.", "tags": ["housing"], "owner": "user"},
|
||
{"content": "The user has been learning German on Duolingo and has a 30-day streak.", "tags": ["language", "learning"], "owner": "user"},
|
||
{"content": "The user's partner Sofia is staying in Madrid for now, which the user finds hard.", "tags": ["relationship"], "owner": "user"}
|
||
],
|
||
"intentions": [
|
||
{"content": "The user plans to move to Berlin next month (around December 2025).", "tags": ["housing", "relocation"], "valid_until": "2025-12-31", "owner": "user"}
|
||
],
|
||
"basic_info": {}
|
||
}
|
||
```
|
||
Four+ topics — thesis defense, the Berlin apartment, learning German, and the partner in Madrid. Do not stop after the first topic. Note one turn split into a completed fact (lease signed) AND an intention (the upcoming move), and the emotional-but-substantive detail about Sofia is kept because it carries lasting relationship context.
|
||
|
||
## Example 9 — Two real people: `name` on an assistant message means a real person
|
||
|
||
New Messages:
|
||
[{"role": "user", "name": "Tim", "content": "Just got back from the climbing gym, third time this week. Finally sent that overhang route."},
|
||
{"role": "assistant", "name": "John", "content": "Nice! I've been swimming instead — my shoulder still hurts from last month."}]
|
||
Memory Date: 2025-08-14
|
||
|
||
Output:
|
||
```json
|
||
{
|
||
"memory": [
|
||
{"content": "The user has been climbing three times a week and recently completed a difficult overhang route.", "tags": ["fitness", "climbing"], "owner": "user"},
|
||
{"content": "John has been swimming instead of other exercise because his shoulder still hurt after an injury around July 2025.", "tags": ["fitness", "health"], "owner": "user"}
|
||
],
|
||
"intentions": [],
|
||
"basic_info": {"name": "Tim"}
|
||
}
|
||
```
|
||
John carries `role: "assistant"` but he has a `name`, so he is another real person, NOT the AI: his memory uses "John ..." as the subject and is still `owner: "user"` (it belongs to the user's memory store). The user's own memory stays "The user ..." even though we know the name is Tim — never "Tim has been climbing". `basic_info.name` comes from `name: "Tim"` on the user message, which is authoritative even though Tim never said his own name out loud.
|
||
|
||
## Example 10 — Counter-example: another person's attributes never go into basic_info
|
||
|
||
New Messages:
|
||
[{"role": "user", "name": "Tim", "content": "你最近在忙什么?"},
|
||
{"role": "assistant", "name": "John", "content": "我在柏林一家医院当护士,上的是夜班,挺累的。"}]
|
||
Memory Date: 2025-09-02
|
||
|
||
Output:
|
||
```json
|
||
{
|
||
"memory": [
|
||
{"content": "John是柏林一家医院的护士,上夜班,觉得很累。", "tags": ["work", "health"], "owner": "user"}
|
||
],
|
||
"intentions": [],
|
||
"basic_info": {"name": "Tim"}
|
||
}
|
||
```
|
||
John's occupation and location are HIS attributes, so they go into `memory` with "John" as the subject. They must NEVER land in `basic_info.occupation` / `basic_info.location` — `basic_info` is exclusively the user's own profile. Only `name` (Tim, from the user message's `name` field) belongs there.
|
||
|
||
---
|
||
"""
|
||
|
||
|
||
EXTRACT_PROMPT = """## Last k Messages
|
||
|
||
{last_messages}
|
||
|
||
---
|
||
|
||
## New Messages
|
||
|
||
{content}
|
||
|
||
---
|
||
|
||
## Memory Date
|
||
|
||
{memory_at}
|
||
|
||
---
|
||
|
||
## Current Date
|
||
|
||
{current_date}
|
||
|
||
---
|
||
|
||
# Output Format
|
||
|
||
Return ONLY a JSON object inside a ```json fenced block.
|
||
|
||
```json
|
||
{{
|
||
"memory": [
|
||
{{
|
||
"content": "...",
|
||
"tags": ["..."],
|
||
"owner": "user"
|
||
}}
|
||
],
|
||
"intentions": [
|
||
{{
|
||
"content": "...",
|
||
"tags": ["..."],
|
||
"valid_until": "YYYY-MM-DD",
|
||
"owner": "user"
|
||
}}
|
||
],
|
||
"basic_info": {{}}
|
||
}}
|
||
```
|
||
|
||
Rules:
|
||
|
||
* `memory` = durable info worth remembering: preferences, opinions, attitudes,
|
||
traits, events, plans, experiences, habits, decisions — about the user or
|
||
about any other named real person, with the subject written per "# SUBJECT"
|
||
* `intentions` = forward-looking things the user (or another named person) plans /
|
||
intends to do in the future, with `valid_until` = the deadline/target date
|
||
(resolved against Memory Date) or `null` if none
|
||
* `owner` (required on every memory & intention) = `"user"` or `"agent"`
|
||
(see "# OWNER" above; default `"user"` when unsure)
|
||
* `basic_info` = the **user's own profile only**. Include stable profile
|
||
fields (e.g. name, age, location, occupation, employer) **explicitly stated**
|
||
in the NEW user messages. Never infer, never invent. If no field is explicitly
|
||
stated, output `{{}}`.
|
||
* Exception for `name`: the `name` field on the user's own message, or a leading
|
||
line "The user's name is X" / "用户名叫X", is an authoritative source for
|
||
`name` — even though the user never said it out loud.
|
||
* NEVER put another real person's attributes here. If John (an assistant-role
|
||
message carrying `name`) says "I'm a nurse in Berlin", that is a `memory`
|
||
("John is a nurse in Berlin."), NOT `basic_info.occupation` / `.location`.
|
||
* No duplicate information across sections
|
||
* Tags must contain 1–3 concise lowercase keywords
|
||
* Use empty arrays `[]` or `{{}}` when nothing is extracted
|
||
* Return valid JSON only
|
||
|
||
Output the JSON now."""
|
||
|
||
|
||
# User prompt when MEMORY_ENABLE_INTENTION=false(无 intentions 桶)
|
||
EXTRACT_PROMPT_NO_INTENTION = """## Last k Messages
|
||
|
||
{last_messages}
|
||
|
||
---
|
||
|
||
## New Messages
|
||
|
||
{content}
|
||
|
||
---
|
||
|
||
## Memory Date
|
||
|
||
{memory_at}
|
||
|
||
---
|
||
|
||
## Current Date
|
||
|
||
{current_date}
|
||
|
||
---
|
||
|
||
# Output Format
|
||
|
||
Return ONLY a JSON object inside a ```json fenced block.
|
||
|
||
```json
|
||
{{
|
||
"memory": [
|
||
{{
|
||
"content": "...",
|
||
"tags": ["..."],
|
||
"owner": "user"
|
||
}}
|
||
],
|
||
"basic_info": {{}}
|
||
}}
|
||
```
|
||
|
||
Rules:
|
||
|
||
* `memory` = durable info worth remembering: preferences, opinions, attitudes,
|
||
traits, events, experiences, habits, decisions, **and future plans / upcoming
|
||
actions** — about the user or about any other named real person, with the
|
||
subject written per "# SUBJECT". Do **not** invent a separate intentions bucket.
|
||
* `owner` (required on every memory) = `"user"` or `"agent"`
|
||
(see "# OWNER" above; default `"user"` when unsure)
|
||
* `basic_info` = the **user's own profile only**. Include stable profile
|
||
fields (e.g. name, age, location, occupation, employer) **explicitly stated**
|
||
in the NEW user messages. Never infer, never invent. If no field is explicitly
|
||
stated, output `{{}}`.
|
||
* Exception for `name`: the `name` field on the user's own message, or a leading
|
||
line "The user's name is X" / "用户名叫X", is an authoritative source for
|
||
`name` — even though the user never said it out loud.
|
||
* NEVER put another real person's attributes here. If John (an assistant-role
|
||
message carrying `name`) says "I'm a nurse in Berlin", that is a `memory`
|
||
("John is a nurse in Berlin."), NOT `basic_info.occupation` / `.location`.
|
||
* No duplicate information across sections
|
||
* Tags: 1–3 concise keywords; topical tags lowercase; proper nouns keep capitalization
|
||
* Use empty arrays `[]` or `{{}}` when nothing is extracted
|
||
* Return valid JSON only — **never** include an `intentions` key
|
||
|
||
Output the JSON now."""
|
||
|
||
|
||
# ================================================================
|
||
# 核心实现
|
||
# ================================================================
|
||
|
||
|
||
def _system_prompt_without_intention(system_prompt: str, *, lang: str) -> str:
|
||
"""从 system prompt 去掉 intentions 分桶,改为单一 memory(含未来计划)。"""
|
||
import re
|
||
|
||
if lang == "zh":
|
||
repl = (
|
||
"# 记忆\n"
|
||
"\n"
|
||
"你只提取一类记忆:**memory(记忆)**。\n"
|
||
"\n"
|
||
"## memory(记忆)\n"
|
||
"\n"
|
||
"一切值得长期记住的信息都放这里——既包括用户本人的,也包括对话中其他具名真人的:\n"
|
||
"\n"
|
||
"* 偏好、观点、态度、价值观\n"
|
||
"* 事件、行动、经历\n"
|
||
"* 习惯、行为模式\n"
|
||
"* 关系、稳定特质\n"
|
||
"* 客观事实、决策、约束条件\n"
|
||
"* 正在进行的活动\n"
|
||
"* **未来计划 / 即将发生的安排**(若值得记住,直接写进 `memory`;"
|
||
"不要单独开 intentions 桶)\n"
|
||
"\n"
|
||
"如果用户隐式表达偏好,也应提取。\n"
|
||
"\n"
|
||
"不要把\"用户是怎样的人\"和\"发生了什么\"分开。两者都放进 `memory`。\n"
|
||
"\n"
|
||
"---\n"
|
||
"\n"
|
||
)
|
||
system_prompt = re.sub(
|
||
r"# 两类记忆.*?(?=\n# OWNER)",
|
||
repl,
|
||
system_prompt,
|
||
count=1,
|
||
flags=re.S,
|
||
)
|
||
system_prompt = system_prompt.replace(
|
||
"每条 memory 和 intention 都必须带 `owner`",
|
||
"每条 memory 都必须带 `owner`",
|
||
)
|
||
system_prompt = system_prompt.replace(
|
||
"每条 memory 和 intention 都必须带",
|
||
"每条 memory 都必须带",
|
||
)
|
||
# few-shot 里若仍出现 intentions,用硬约束盖住
|
||
system_prompt += (
|
||
"\n\n---\n\n# INTENTION 关闭\n"
|
||
"当前配置关闭了 intention 抽取。即使示例里出现 `intentions`,"
|
||
"也**不要**输出该字段;未来计划写入 `memory`。\n"
|
||
)
|
||
else:
|
||
repl = (
|
||
"# MEMORY\n"
|
||
"\n"
|
||
"You extract one kind of memory: **memory**.\n"
|
||
"\n"
|
||
"## memory\n"
|
||
"\n"
|
||
"Everything durable and worth remembering about the user — and about any other\n"
|
||
"named person in the conversation — goes here:\n"
|
||
"\n"
|
||
"* preferences, opinions, values, attitudes\n"
|
||
"* events, actions, experiences\n"
|
||
"* habits, behavioral patterns\n"
|
||
"* relationships, traits\n"
|
||
"* objective facts, decisions, constraints\n"
|
||
"* ongoing activities\n"
|
||
"* **future plans / upcoming actions** that are worth remembering\n"
|
||
" (put them in `memory`; do NOT invent a separate intentions bucket)\n"
|
||
"\n"
|
||
"Implicit preferences MUST be inferred when strongly suggested.\n"
|
||
"\n"
|
||
"Do NOT separate \"what kind of person the user is\" from \"what happened\".\n"
|
||
"Both go into `memory`.\n"
|
||
"\n"
|
||
"---\n"
|
||
"\n"
|
||
)
|
||
system_prompt = re.sub(
|
||
r"# TWO KINDS OF MEMORY.*?(?=\n# OWNER\n)",
|
||
repl,
|
||
system_prompt,
|
||
count=1,
|
||
flags=re.S,
|
||
)
|
||
system_prompt = system_prompt.replace(
|
||
"Every memory and intention MUST carry an `owner`",
|
||
"Every memory MUST carry an `owner`",
|
||
)
|
||
system_prompt += (
|
||
"\n\n---\n\n# INTENTION DISABLED\n"
|
||
"Intention extraction is disabled. Even if few-shot examples show "
|
||
"`intentions`, do **not** emit that key; put future plans in `memory`.\n"
|
||
)
|
||
return system_prompt
|
||
|
||
|
||
class Extractor:
|
||
"""
|
||
提取智能体 (V2)
|
||
|
||
支持 V1 兼容接口 + V2 双模式接口。
|
||
"""
|
||
|
||
def __init__(
|
||
self,
|
||
llm_provider: LLMProvider,
|
||
llm_config: Optional[GlobalLLMConfig] = None,
|
||
):
|
||
self.llm = llm_provider
|
||
self._llm_config = llm_config or GlobalLLMConfig()
|
||
self._call_count = 0
|
||
self._total_tokens = 0
|
||
logger.info("Extractor V2 initialized")
|
||
|
||
# ================================================================
|
||
# V1 兼容接口
|
||
# ================================================================
|
||
|
||
async def extract(
|
||
self,
|
||
content: str,
|
||
context: Dict[str, Any] = None,
|
||
extract_types: List[str] = None,
|
||
current_time: str = "",
|
||
existing_tags: Optional[List[str]] = None,
|
||
tools: Optional[List[Dict[str, Any]]] = None,
|
||
tool_registry: Optional[Any] = None,
|
||
tool_context: Optional[Dict[str, Any]] = None,
|
||
history_context: str = "",
|
||
basic_profile_fields: Optional[Dict[str, str]] = None,
|
||
) -> ExtractResult:
|
||
"""
|
||
统一提取:identity / facts / basic_info
|
||
|
||
Args:
|
||
content: 原始对话内容
|
||
context: 业务上下文(目前透传,不使用)
|
||
extract_types: 兼容参数(未使用)
|
||
current_time: 当前时间字符串
|
||
existing_tags: 可选,该用户已有的所有 tags(供 LLM 优先复用)
|
||
tools: **DEPRECATED**: 自 v0.1.5.13 起 extractor 不再调用 LLM
|
||
function-calling 工具——基础画像通过 prompt + JSON 输出
|
||
的 basic_info 字段表达。保留参数仅为向后兼容;传入也不
|
||
会被透传到 LLM API。
|
||
tool_registry: **DEPRECATED**: 同上,不再使用。
|
||
tool_context: 保留,作为 dispatch context 传给(未来可能扩展的)非 basic_info
|
||
tool;当前未使用。
|
||
basic_profile_fields:
|
||
**DEPRECATED**: 曾用于渲染 EXTRACT prompt 的基础画像 schema 段落,
|
||
现已移除该段落(basic_info 由内置 few-shot / 指令引导)。保留参数
|
||
仅为向后兼容,传入不再生效。
|
||
"""
|
||
MAX_TOOL_ROUNDS = 5
|
||
|
||
try:
|
||
# 构建 last_messages(传入的 history_context 就是格式化好的历史对话文本)
|
||
_last_messages = history_context or "(none)"
|
||
|
||
# 构建日期字段
|
||
from datetime import date as _date_cls
|
||
|
||
_current_date = (
|
||
_date_cls.today().isoformat()
|
||
) # 系统当前日期 e.g. "2026-05-23"
|
||
# memory_at: 转为日精度
|
||
if current_time:
|
||
try:
|
||
from datetime import datetime as _dt_cls
|
||
|
||
_memory_at = _dt_cls.fromisoformat(current_time).strftime(
|
||
"%Y-%m-%d"
|
||
)
|
||
except (ValueError, TypeError):
|
||
_memory_at = _current_date
|
||
else:
|
||
_memory_at = _current_date
|
||
|
||
# Select prompt based on input language
|
||
from ..utils.lang_detect import is_chinese
|
||
|
||
_enable_intention = bool(
|
||
getattr(self._llm_config, "enable_intention", True)
|
||
)
|
||
if is_chinese(content):
|
||
from .prompts_zh import (
|
||
EXTRACT_PROMPT_ZH,
|
||
EXTRACT_PROMPT_ZH_NO_INTENTION,
|
||
EXTRACT_SYSTEM_PROMPT_ZH,
|
||
EXTRACT_FEW_SHOT_ZH,
|
||
)
|
||
|
||
_custom_sys = getattr(
|
||
self._llm_config, "extract_system_prompt_zh", None
|
||
)
|
||
if _custom_sys:
|
||
# client 自定义 system prompt:直接用,不追加内置 few-shot
|
||
system_prompt = _custom_sys
|
||
else:
|
||
system_prompt = EXTRACT_SYSTEM_PROMPT_ZH
|
||
if getattr(self._llm_config, "few_shot_enabled", False):
|
||
system_prompt = system_prompt + EXTRACT_FEW_SHOT_ZH
|
||
if not _enable_intention and not _custom_sys:
|
||
system_prompt = _system_prompt_without_intention(
|
||
system_prompt, lang="zh"
|
||
)
|
||
if getattr(self._llm_config, "structure_metadata_enabled", False):
|
||
from .prompts_zh import STRUCTURE_METADATA_PROMPT_ZH
|
||
system_prompt += STRUCTURE_METADATA_PROMPT_ZH
|
||
_user_tmpl = (
|
||
EXTRACT_PROMPT_ZH
|
||
if _enable_intention
|
||
else EXTRACT_PROMPT_ZH_NO_INTENTION
|
||
)
|
||
prompt = _user_tmpl.format(
|
||
content=content,
|
||
current_date=_current_date,
|
||
memory_at=_memory_at,
|
||
last_messages=_last_messages,
|
||
)
|
||
else:
|
||
_custom_sys = getattr(
|
||
self._llm_config, "extract_system_prompt_en", None
|
||
)
|
||
if _custom_sys:
|
||
system_prompt = _custom_sys
|
||
else:
|
||
system_prompt = EXTRACT_SYSTEM_PROMPT
|
||
if getattr(self._llm_config, "few_shot_enabled", False):
|
||
system_prompt = system_prompt + EXTRACT_FEW_SHOT_EN
|
||
if not _enable_intention and not _custom_sys:
|
||
system_prompt = _system_prompt_without_intention(
|
||
system_prompt, lang="en"
|
||
)
|
||
if getattr(self._llm_config, "structure_metadata_enabled", False):
|
||
system_prompt += STRUCTURE_METADATA_PROMPT_EN
|
||
_user_tmpl = (
|
||
EXTRACT_PROMPT if _enable_intention else EXTRACT_PROMPT_NO_INTENTION
|
||
)
|
||
prompt = _user_tmpl.format(
|
||
content=content,
|
||
current_date=_current_date,
|
||
memory_at=_memory_at,
|
||
last_messages=_last_messages,
|
||
)
|
||
|
||
# 累计 token 统计
|
||
if _log_llm_content_enabled():
|
||
logger.debug(f"[extractor] user prompt: {prompt!r}")
|
||
else:
|
||
logger.debug(
|
||
"[extractor] user prompt hidden; len=%d sha256=%s",
|
||
len(prompt), _content_fingerprint(prompt),
|
||
)
|
||
total_tokens = 0
|
||
total_prompt_tokens = 0
|
||
total_completion_tokens = 0
|
||
llm_finish_reason = ""
|
||
llm_model = ""
|
||
llm_response_id = ""
|
||
llm_choices_count = 0
|
||
llm_content_len = 0
|
||
llm_tool_calls_count = 0
|
||
llm_reasoning_tokens = 0
|
||
llm_reasoning_len = 0
|
||
all_tool_calls: List[Dict[str, Any]] = []
|
||
all_tool_results: List[Dict[str, Any]] = []
|
||
|
||
# 构造 messages(system + user)
|
||
messages: List[Dict[str, Any]] = [
|
||
{"role": "system", "content": system_prompt},
|
||
{"role": "user", "content": prompt},
|
||
]
|
||
# 保存实际 prompt 用于 trace 日志
|
||
_actual_user_prompt = prompt
|
||
_actual_sys_prompt = system_prompt
|
||
|
||
# 第一轮调用
|
||
# NOTE: tools / tool_choice 已废弃 — 基础画像走 prompt + JSON basic_info
|
||
# 字段,而不是 LLM function-calling tool(弱模型乱填)。
|
||
# 若调用方传入 tools/tool_registry,会被忽略。
|
||
response = await self.llm.complete_messages(
|
||
messages=messages,
|
||
max_tokens=self._llm_config.agent_max_tokens,
|
||
tools=None,
|
||
tool_choice=None,
|
||
audit_stage="extract",
|
||
)
|
||
self._call_count += 1
|
||
total_tokens += response.tokens_used
|
||
total_prompt_tokens += response.prompt_tokens
|
||
total_completion_tokens += response.completion_tokens
|
||
|
||
raw_text = response.content or ""
|
||
llm_finish_reason = getattr(response, "finish_reason", "") or ""
|
||
llm_model = getattr(response, "model", "") or ""
|
||
llm_response_id = getattr(response, "response_id", "") or ""
|
||
llm_choices_count = getattr(response, "choices_count", 0) or 0
|
||
llm_content_len = len(raw_text)
|
||
llm_tool_calls_count = len(response.tool_calls or [])
|
||
llm_reasoning_tokens = getattr(response, "reasoning_tokens", 0) or 0
|
||
llm_reasoning_len = len(getattr(response, "reasoning_content", "") or "")
|
||
|
||
# 多轮 tool-use loop(仅当传入 tool_registry 时启用)
|
||
if response.tool_calls and tool_registry:
|
||
from .tools.base import parse_tool_calls_from_json
|
||
|
||
round_count = 0
|
||
while response.tool_calls and round_count < MAX_TOOL_ROUNDS:
|
||
round_count += 1
|
||
logger.info(
|
||
f"[extractor] tool-use round {round_count}: "
|
||
f"{len(response.tool_calls)} tool call(s)"
|
||
)
|
||
|
||
all_tool_calls.extend(response.tool_calls)
|
||
|
||
# 追加 assistant 消息(含 tool_calls)
|
||
messages.append(
|
||
{
|
||
"role": "assistant",
|
||
"content": response.content or "",
|
||
"tool_calls": response.tool_calls,
|
||
}
|
||
)
|
||
|
||
# 执行每个 tool call
|
||
parsed_calls = parse_tool_calls_from_json(response.tool_calls)
|
||
for tc_raw, tc_parsed in zip(
|
||
response.tool_calls, parsed_calls or []
|
||
):
|
||
tc_id = tc_raw.get("id") or ""
|
||
logger.info(
|
||
f"[extractor] tool call: name={tc_parsed.name}, "
|
||
f"tool_call_id={tc_id!r}, "
|
||
f"raw_keys={list(tc_raw.keys())}"
|
||
)
|
||
if not tc_id:
|
||
logger.error(
|
||
f"[extractor] tool_call_id is empty! "
|
||
f"raw tc: {json.dumps(tc_raw, ensure_ascii=False, default=str)[:500]}"
|
||
)
|
||
try:
|
||
t_res = await tool_registry.dispatch(
|
||
tc_parsed, tool_context or {}
|
||
)
|
||
result_content = json.dumps(
|
||
{
|
||
"success": t_res.success,
|
||
"data": t_res.data,
|
||
"error": t_res.error,
|
||
},
|
||
ensure_ascii=False,
|
||
default=str,
|
||
)
|
||
all_tool_results.append(
|
||
{
|
||
"tool": tc_parsed.name,
|
||
"round": round_count,
|
||
"success": t_res.success,
|
||
"data": t_res.data,
|
||
"error": t_res.error,
|
||
}
|
||
)
|
||
except Exception as tool_err:
|
||
logger.error(
|
||
f"[extractor] tool '{tc_parsed.name}' raised: {tool_err}",
|
||
exc_info=True,
|
||
)
|
||
result_content = json.dumps(
|
||
{"success": False, "error": str(tool_err)},
|
||
ensure_ascii=False,
|
||
)
|
||
all_tool_results.append(
|
||
{
|
||
"tool": tc_parsed.name,
|
||
"round": round_count,
|
||
"success": False,
|
||
"error": str(tool_err),
|
||
}
|
||
)
|
||
|
||
messages.append(
|
||
{
|
||
"role": "tool",
|
||
"tool_call_id": tc_id,
|
||
"content": result_content,
|
||
}
|
||
)
|
||
|
||
# 下一轮 LLM 调用(已废弃 tools;保留 dispatch 路径仅用于
|
||
# 极端情况下 LLM 通过其他途径返回 tool_calls 的兼容处理)
|
||
response = await self.llm.complete_messages(
|
||
messages=messages,
|
||
max_tokens=self._llm_config.agent_max_tokens,
|
||
tools=None,
|
||
tool_choice=None,
|
||
audit_stage="extract",
|
||
)
|
||
self._call_count += 1
|
||
total_tokens += response.tokens_used
|
||
total_prompt_tokens += response.prompt_tokens
|
||
total_completion_tokens += response.completion_tokens
|
||
raw_text = response.content or ""
|
||
llm_finish_reason = getattr(response, "finish_reason", "") or ""
|
||
llm_model = getattr(response, "model", "") or ""
|
||
llm_response_id = getattr(response, "response_id", "") or ""
|
||
llm_choices_count = getattr(response, "choices_count", 0) or 0
|
||
llm_content_len = len(raw_text)
|
||
llm_tool_calls_count = len(response.tool_calls or [])
|
||
llm_reasoning_tokens = getattr(response, "reasoning_tokens", 0) or 0
|
||
llm_reasoning_len = len(getattr(response, "reasoning_content", "") or "")
|
||
|
||
if round_count >= MAX_TOOL_ROUNDS and response.tool_calls:
|
||
logger.warning(
|
||
f"[extractor] tool-use loop hit max rounds ({MAX_TOOL_ROUNDS})"
|
||
)
|
||
|
||
logger.info(
|
||
f"[extractor] tool-use loop done: {round_count} round(s), "
|
||
f"{len(all_tool_calls)} tool call(s), content_len={len(raw_text)}"
|
||
)
|
||
|
||
elif response.tool_calls and not tool_registry:
|
||
# 没有 tool_registry 时,透传 tool_calls 给上层(向后兼容)
|
||
all_tool_calls = response.tool_calls
|
||
|
||
self._total_tokens += total_tokens
|
||
|
||
# 空响应判断
|
||
if (not raw_text or not raw_text.strip()) and not all_tool_calls:
|
||
logger.warning(
|
||
"[extractor] LLM returned empty response "
|
||
f"finish_reason={llm_finish_reason!r} model={llm_model!r} "
|
||
f"response_id={llm_response_id!r} choices={llm_choices_count} "
|
||
f"content_len={llm_content_len} prompt_tokens={total_prompt_tokens} "
|
||
f"completion_tokens={total_completion_tokens} total_tokens={total_tokens} "
|
||
f"reasoning_tokens={llm_reasoning_tokens} reasoning_len={llm_reasoning_len}"
|
||
+ (
|
||
" | HINT: reasoning exhausted the output budget "
|
||
"(completion_tokens == max_tokens). Disable thinking "
|
||
"(hy3: reasoning_effort=no_think) or raise MEMORY_AGENT_MAX_TOKENS."
|
||
if llm_reasoning_tokens > 0
|
||
and llm_finish_reason == "length"
|
||
else ""
|
||
)
|
||
)
|
||
return ExtractResult(
|
||
success=False,
|
||
error="LLM returned empty response",
|
||
error_code="EMPTY_RESPONSE",
|
||
raw_response=raw_text,
|
||
tokens_used=total_tokens,
|
||
prompt_tokens=total_prompt_tokens,
|
||
completion_tokens=total_completion_tokens,
|
||
llm_finish_reason=llm_finish_reason,
|
||
llm_model=llm_model,
|
||
llm_response_id=llm_response_id,
|
||
llm_choices_count=llm_choices_count,
|
||
llm_content_len=llm_content_len,
|
||
llm_tool_calls_count=llm_tool_calls_count,
|
||
llm_reasoning_tokens=llm_reasoning_tokens,
|
||
llm_reasoning_len=llm_reasoning_len,
|
||
_actual_prompt=_actual_user_prompt,
|
||
_actual_system_prompt=_actual_sys_prompt,
|
||
)
|
||
|
||
# 解析 JSON
|
||
info: Dict[str, Any]
|
||
if raw_text and raw_text.strip():
|
||
parsed = self._parse_json(raw_text)
|
||
if parsed is None:
|
||
if not all_tool_calls:
|
||
logger.warning(
|
||
f"[extractor] JSON parse failed: {raw_text[:200]}"
|
||
)
|
||
return ExtractResult(
|
||
success=False,
|
||
error=f"JSON parse failed: {raw_text[:200]}",
|
||
error_code="JSON_PARSE_FAILED",
|
||
raw_response=raw_text,
|
||
tokens_used=total_tokens,
|
||
prompt_tokens=total_prompt_tokens,
|
||
completion_tokens=total_completion_tokens,
|
||
llm_finish_reason=llm_finish_reason,
|
||
llm_model=llm_model,
|
||
llm_response_id=llm_response_id,
|
||
llm_choices_count=llm_choices_count,
|
||
llm_content_len=llm_content_len,
|
||
llm_tool_calls_count=llm_tool_calls_count,
|
||
_actual_prompt=_actual_user_prompt,
|
||
_actual_system_prompt=_actual_sys_prompt,
|
||
)
|
||
info = {}
|
||
else:
|
||
info = parsed
|
||
else:
|
||
info = {}
|
||
|
||
# 记录 tool 调用信息供上层(pipeline log / writer)使用
|
||
if all_tool_calls:
|
||
info["tool_calls"] = all_tool_calls
|
||
if all_tool_results:
|
||
info["tool_results"] = all_tool_results
|
||
|
||
# One extraction call covers one current write and cannot establish
|
||
# repetition across independent occurrences. Keep any bounded
|
||
# Episode, but defer Series creation to reconcile where historical
|
||
# candidate writes are visible.
|
||
if getattr(self._llm_config, "structure_metadata_enabled", False):
|
||
from ..structure import strip_series_from_assignments
|
||
|
||
for _key in ("memory", "facts", "identity"):
|
||
_items = info.get(_key)
|
||
if not isinstance(_items, list):
|
||
continue
|
||
for _item in _items:
|
||
if isinstance(_item, dict) and "topic_assignments" in _item:
|
||
_item["topic_assignments"] = strip_series_from_assignments(
|
||
_item.get("topic_assignments")
|
||
)
|
||
|
||
# Intention 开关关闭:即使模型仍返回 intentions,也丢弃(写侧另有硬闸)
|
||
if not _enable_intention:
|
||
if info.get("intentions"):
|
||
logger.info(
|
||
"[extractor] enable_intention=false, dropping %d intention(s)",
|
||
len(info.get("intentions") or []),
|
||
)
|
||
info["intentions"] = []
|
||
|
||
# basic_info is the canonical home for the user's name. Models may
|
||
# still echo the same fact into L2 despite the prompt; remove only an
|
||
# exact name-only statement, never a sentence carrying other facts.
|
||
_basic = info.get("basic_info") if isinstance(info, dict) else None
|
||
_name = str((_basic or {}).get("name") or "").strip()
|
||
if _name:
|
||
_name_key = re.escape(_name)
|
||
_name_only = re.compile(
|
||
rf"^(?:the\s+user(?:'s|’s)\s+name\s+is|"
|
||
rf"the\s+user\s+is\s+(?:named|called))\s+{_name_key}[.!。]?$|"
|
||
rf"^(?:用户(?:的名字|名)?(?:是|叫))\s*{_name_key}[.!。]?$",
|
||
flags=re.I,
|
||
)
|
||
for _key in ("memory", "facts"):
|
||
_items = info.get(_key)
|
||
if isinstance(_items, list):
|
||
info[_key] = [
|
||
item for item in _items
|
||
if not (
|
||
isinstance(item, dict)
|
||
and _name_only.fullmatch(
|
||
str(item.get("content") or "").strip()
|
||
)
|
||
)
|
||
]
|
||
|
||
# A strong model can still repeat one durable proposition twice
|
||
# inside the same aggregated L2. Remove only near-contained complete
|
||
# sentences; keep short fragments, dates and causal/detail clauses.
|
||
for _key in ("memory", "facts"):
|
||
_items = info.get(_key)
|
||
if not isinstance(_items, list):
|
||
continue
|
||
for _item in _items:
|
||
if not isinstance(_item, dict):
|
||
continue
|
||
_content = str(_item.get("content") or "").strip()
|
||
if _content:
|
||
_item["content"] = dedup_internal_memory_sentences(_content)
|
||
|
||
tool_calls_only = (
|
||
bool(all_tool_calls)
|
||
and not info.get("memory")
|
||
and not info.get("facts") # legacy compat
|
||
and not info.get("intentions")
|
||
and not info.get("identity") # legacy compat
|
||
)
|
||
|
||
result = ExtractResult(
|
||
success=True,
|
||
info=info,
|
||
confidence=0.8,
|
||
raw_response=raw_text,
|
||
tokens_used=total_tokens,
|
||
prompt_tokens=total_prompt_tokens,
|
||
completion_tokens=total_completion_tokens,
|
||
llm_finish_reason=llm_finish_reason,
|
||
llm_model=llm_model,
|
||
llm_response_id=llm_response_id,
|
||
llm_choices_count=llm_choices_count,
|
||
llm_content_len=llm_content_len,
|
||
llm_tool_calls_count=llm_tool_calls_count,
|
||
tool_calls_only=tool_calls_only,
|
||
_actual_prompt=_actual_user_prompt,
|
||
_actual_system_prompt=_actual_sys_prompt,
|
||
)
|
||
return result
|
||
except Exception as e:
|
||
# 带异常类型 + 完整 traceback,避免只剩笼统 "LLM_ERROR" 无从排查。
|
||
# 若为 LLM 重试耗尽,RuntimeError 的 __cause__ 里含真实根因(超时/401/连接等),
|
||
# 且 LLMRetryError.attempts 带每一次 attempt 的失败明细。
|
||
logger.error(f"Extractor.extract failed: {type(e).__name__}: {e}", exc_info=True)
|
||
_root = e.__cause__ or e
|
||
_err_msg = f"{type(e).__name__}: {e}"
|
||
if _root is not e:
|
||
_err_msg += f" | root cause: {type(_root).__name__}: {_root}"
|
||
_attempts = list(getattr(e, "attempts", []) or [])
|
||
return ExtractResult(
|
||
success=False,
|
||
error=_err_msg,
|
||
error_code="LLM_ERROR",
|
||
attempts=_attempts,
|
||
_actual_prompt=(
|
||
_actual_user_prompt if "_actual_user_prompt" in dir() else None
|
||
),
|
||
_actual_system_prompt=(
|
||
_actual_sys_prompt if "_actual_sys_prompt" in dir() else None
|
||
),
|
||
)
|
||
|
||
async def extract_profile(
|
||
self,
|
||
content: str,
|
||
context: Dict[str, Any] = None,
|
||
) -> Dict[str, Any]:
|
||
"""专门提取用户画像 (profile 已包含 preferences)"""
|
||
result = await self.extract(content, context)
|
||
if result.success:
|
||
return result.info.get("profile", {})
|
||
return {}
|
||
|
||
async def extract_entities(
|
||
self,
|
||
content: str,
|
||
context: Dict[str, Any] = None,
|
||
) -> List[Dict[str, Any]]:
|
||
"""提取实体"""
|
||
result = await self.extract(content, context)
|
||
if result.success:
|
||
return result.info.get("entities", [])
|
||
return []
|
||
|
||
# ================================================================
|
||
# 工具方法
|
||
# ================================================================
|
||
|
||
def _parse_json(self, text: str) -> Optional[Dict[str, Any]]:
|
||
"""从 LLM 输出中解析 JSON。成功返回 dict,解析失败返回 None。"""
|
||
text = text.strip()
|
||
# 处理 markdown 代码块
|
||
if "```json" in text:
|
||
text = text.split("```json")[1].split("```")[0].strip()
|
||
elif "```" in text:
|
||
text = text.split("```")[1].split("```")[0].strip()
|
||
|
||
try:
|
||
return json.loads(text)
|
||
except json.JSONDecodeError:
|
||
# 尝试找到 JSON 块
|
||
match = re.search(r"\{[\s\S]*\}", text)
|
||
if match:
|
||
try:
|
||
return json.loads(match.group())
|
||
except json.JSONDecodeError:
|
||
pass
|
||
# 最后兜底:json_repair 修复常见 LLM 残缺输出(缺逗号/引号/未闭合等)
|
||
try:
|
||
from json_repair import repair_json
|
||
|
||
repaired = repair_json(text, return_objects=True)
|
||
if isinstance(repaired, dict) and repaired:
|
||
logger.info("[extractor] JSON recovered via json_repair")
|
||
return repaired
|
||
except Exception as _re:
|
||
logger.debug(f"[extractor] json_repair failed: {_re}")
|
||
return None
|
||
|
||
def get_stats(self) -> Dict[str, Any]:
|
||
"""获取统计信息"""
|
||
return {
|
||
"call_count": self._call_count,
|
||
"total_tokens": self._total_tokens,
|
||
"avg_tokens_per_call": (
|
||
self._total_tokens / self._call_count if self._call_count > 0 else 0
|
||
),
|
||
}
|