Files
DeepTutor/web/lib/setup-signals.ts
Bingxi Zhao (Frank) 7c6bcfaef7 release: v1.5.14
Immersive Reading, self-configuration, and a two-way IMA library.

- Immersive Reading: a new capability that opens a document beside the
  thread. New reading engine (PDF pages / slides / page-sized sections),
  five reading tools, a deterministic locate pre-pass that grounds the
  turn without a second LLM call, /api/v1/reading, and a reader pane with
  selection, highlights, notes, and export to an annotated PDF or Markdown.
- Setup capability: inspect the install, apply a setting, install a
  parsing engine, or request a credential from a chat turn. New settings
  spec table; a candidate value is probed before it is committed, and API
  keys never enter the model's context.
- Tencent IMA: pipeline split into transport/envelope/models/sources/
  notes/media/inventory, an additive capability with five tools, and
  connected libraries that can finally be enumerated (bounded listings
  are reported as a lower bound).
- Retrieval: supports_rag_retrieval() replaces is_connected_kb() for the
  "can I sweep this?" question, so linked / lightrag_server / ima sources
  stop being silently dropped from every book.
- Notebooks: their own console at /notebook, per-notebook locks and atomic
  writes, copy / move / export routes, damaged files surfaced instead of
  dropped, and renaming a record no longer clears its knowledge-base link.
- Settings persistence: one lock and one atomic replace shared by every
  writer of interface.json; the PUT endpoints stop writing the
  defaults-merged view back to disk.

Release notes: assets/releases/ver1-5-14.md
2026-08-19 17:34:52 +08:00

93 lines
3.4 KiB
TypeScript

import type { StreamEvent } from "@/lib/unified-ws";
/**
* Reading the setup capability's signals off a turn's event stream.
*
* Kept as pure functions, apart from the components that render them, for the
* same reason `lib/ask-user-state.ts` is: the interesting logic is which events
* count and how they are deduplicated, and that deserves tests that do not need
* a DOM.
*
* Mirrors the metadata written by `deeptutor/capabilities/setup/tools.py`.
*/
/** Hand-off card shown when a step needs a credential the assistant must not touch. */
export interface SetupCredentialData {
service: string;
label: string;
settingsPath: string;
reason: string;
}
function toolMetadataOf(event: StreamEvent): Record<string, unknown> | null {
if (event.type !== "tool_result") return null;
const meta = (event.metadata ?? {}) as Record<string, unknown>;
const toolMetadata = meta.tool_metadata;
if (!toolMetadata || typeof toolMetadata !== "object") return null;
return toolMetadata as Record<string, unknown>;
}
/**
* The most recent credential hand-off in this message, if any.
*
* `settingsPath` is required to be an in-app settings route: it is rendered as
* a navigation target, and constraining it here means a malformed or hostile
* value cannot turn the card into an open redirect.
*/
export function extractSetupCredential(
events: StreamEvent[] | undefined,
): SetupCredentialData | null {
if (!events || events.length === 0) return null;
let latest: SetupCredentialData | null = null;
for (const event of events) {
const toolMetadata = toolMetadataOf(event);
if (!toolMetadata) continue;
const raw = toolMetadata.setup_credential;
if (!raw || typeof raw !== "object") continue;
const payload = raw as Record<string, unknown>;
const settingsPath = String(payload.settings_path ?? "").trim();
if (!settingsPath.startsWith("/settings")) continue;
latest = {
service: String(payload.service ?? ""),
label: String(payload.label ?? ""),
settingsPath,
reason: String(payload.reason ?? ""),
};
}
return latest;
}
/**
* Stable ids for every `setup_applied` signal across a conversation.
*
* One id per tool call, so a caller can honour each exactly once: replayed
* history and ordinary re-renders must not keep re-applying a preference the
* user may have changed by hand since. Falls back to key+seq when the transport
* did not carry a tool call id, which still distinguishes separate writes.
*/
export function collectAppliedSettingIds(
messages: ReadonlyArray<{ events?: StreamEvent[] }> | undefined,
): string[] {
if (!messages || messages.length === 0) return [];
const ids: string[] = [];
const seen = new Set<string>();
for (const message of messages) {
for (const event of message.events ?? []) {
const toolMetadata = toolMetadataOf(event);
if (!toolMetadata) continue;
const applied = toolMetadata.setup_applied;
if (!applied || typeof applied !== "object") continue;
const meta = (event.metadata ?? {}) as Record<string, unknown>;
const callId =
(event as { tool_call_id?: string }).tool_call_id ??
(typeof meta.tool_call_id === "string" ? meta.tool_call_id : "");
const key = String((applied as Record<string, unknown>).key ?? "");
const id = callId || (key ? `${key}:${event.seq ?? ""}` : "");
if (!id || seen.has(id)) continue;
seen.add(id);
ids.push(id);
}
}
return ids;
}