text
stringlengths
0
1.47k
# ===== file: lib/index.js =====
import z from "@deepseek-ai/schemastery";
import { defineTool } from "@deepseek-ai/dsh-tools";
import { readFile } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import { BUNDLED_SKILL_RANK } from "@deepseek-ai/dsh-skill";
//#region src/skill.ts
/**
* Bundled `visualize` skill provider: the fragment-authoring contract the
* model loads before its first `visualize` call. Mirrors the official
* `dsh-skill-badge` provider shape — one bundled candidate whose body ships
* in this package's `assets/`.
*
* @module @nagi-ovo/dsh-visualize/skill
*/
const PROVIDER_NAME = "dsh-visualize";
const SKILL_BODY_URL = new URL("../assets/visualize-skill.md", import.meta.url);
/** Absolute path of the skill body, for agents that cannot load skills. */
const SKILL_BODY_PATH = fileURLToPath(SKILL_BODY_URL);
/** Absolute directory the skill body's relative references resolve against. */
const SKILL_RESOURCE_DIR = fileURLToPath(new URL("../assets/", import.meta.url));
const RESOURCE_BASE = {
kind: "directory",
path: SKILL_RESOURCE_DIR
};
const CANDIDATE = {
name: "visualize",
description: "Authoring contract for the visualize tool, which renders interactive cards in the conversation: simulations, algorithm walkthroughs, charts, comparisons, and product-screen mockups. Load before the first visualize call in a session — it defines the fragment structure, theming variables, size ceiling, and...
invocation: {
modelInvocable: true,
userInvocable: true
},
provider: PROVIDER_NAME,
source: "bundled",
resourceBase: RESOURCE_BASE,
rank: BUNDLED_SKILL_RANK,
locator: SKILL_BODY_URL
};
/** The bundled provider registered on `ctx.skills`. */
const visualizeSkillProvider = {
name: PROVIDER_NAME,
list: () => Promise.resolve([CANDIDATE]),
async get(_candidate) {
return {
name: CANDIDATE.name,
description: CANDIDATE.description,
invocation: CANDIDATE.invocation,
provider: CANDIDATE.provider,
source: CANDIDATE.source,
resourceBase: RESOURCE_BASE,
content: await readFile(SKILL_BODY_URL, "utf8")
};
}
};
//#endregion
//#region src/fragment.ts
/**
* Pure fragment contract shared by the tool (validation at execute time), the
* browser card (meta narrowing at render time), and the specs. No I/O and no
* DOM so both halves and vitest can load it unchanged.
*
* A *fragment* is the model-authored inline-HTML body of one visualization:
* literal markup without a document skeleton. The card owns the skeleton — it
* wraps the fragment in a sandboxed iframe document with its own CSP — so a
* fragment that ships its own `<!doctype>`/`<html>`/`<head>`/`<body>` would
* nest documents and is rejected loudly instead of rendered broken.
*
* @module @nagi-ovo/dsh-visualize/fragment
*/
/**
* Wire name of the tool, the keyed toolview, and the streaming-preview match.
* Lives in this pure module so the browser half can import it without pulling
* the node-side tool implementation into the client bundle.
*/
const VISUALIZE_TOOL_NAME = "visualize";
/** Document-skeleton tags a fragment must not contain (case-insensitive). */
const SKELETON_TAG = /<!doctype\b|<\s*(?:html|head|body)\b/iu;
/**
* Validate one fragment against the inline contract.
* @param fragment - the file content the model wrote.
* @param maxBytes - deployment size ceiling for one fragment.
* @returns the fragment's UTF-8 size in bytes.
* @throws Error naming the violated rule; the tool surfaces it as `isError`.
*/
function validateFragment(fragment, maxBytes) {
if (fragment.trim().length === 0) throw new Error("invalid visualization: the fragment file is empty");
const sizeBytes = byteLength(fragment);
if (sizeBytes > maxBytes) throw new Error(`invalid visualization: fragment is ${sizeBytes} bytes, over the ${maxBytes}-byte limit — shrink the inline data first (fewer rows, coarser buckets, fewer decimals)`);
const skeleton = SKELETON_TAG.exec(fragment);
if (skeleton) throw new Error(`invalid visualization: fragment contains a document-skeleton tag (${JSON.stringify(skeleton[0])}) — write only the inline body; the host supplies <!doctype>, <html>, <head>, and <body>`);
return sizeBytes;
}
/**
* Characters of real card content quoted back when a patch fails to apply, so
* the model can correct `old_str` from the true bytes without re-reading the
* whole card.
*/
const PATCH_CONTEXT_CHARS = 160;
/**