Files
openclaw-openclaw/scripts/check-duplicates.mjs
Peter Steinberger 062f88e3e3 refactor: extract reusable AI runtime package (#99059)
* refactor: extract reusable AI runtime package

* refactor: complete AI provider relocation

* refactor: keep llm core internal

* refactor(ai): make @openclaw/ai self-contained with host policy ports

Move pure transport helpers (tool projections, strict-schema normalization,
prompt-cache boundary, stream guards, anthropic/openai compat, request
activity) from src into packages/ai; move utf16-slice into
normalization-core. Inject host policy (guarded fetch, redaction,
strict-tool defaults, diagnostics logging) through AiTransportHost with
inert library defaults installed by src/llm/stream.ts. Narrow the public
barrel to instance-scoped createApiRegistry/createLlmRuntime; the
process-default runtime moves behind internal/ and
registerBuiltInApiProviders takes an explicit registry. Delete the
src/llm/api-registry re-export facade.

* fix(ai): teach node, jiti, and vite resolvers the @openclaw/ai and utf16-slice subpaths

The workspace alias tables in root-alias.cjs, plugin-sdk-native-resolver,
sdk-alias, the shared vitest config, and the Control UI vite config only
knew @openclaw/llm-core; Node-side plugin loading resolved @openclaw/ai
through the pnpm symlink to the unbuilt dist (checks-node-compact CI
failures), and the Control UI build broke on the new
normalization-core/utf16-slice subpath.

* chore(ui): drop leftover service-worker debug logging

* build(release): ship @openclaw/ai with its own shrinkwrap and honest dependency set

packages/ai declares only its six real runtime deps (kysely, chalk, json5,
tslog, zod, fs-safe, and proxyline were never imported); orphaned root deps
removed. generate-npm-shrinkwrap now treats publishable packages/* like
publishable plugins so the AI tarball pins its transitive tree even though
workspace deps are omitted from the root shrinkwrap. knip learns the
package entry points; the tsdown dts neverBundle option moves to its
documented deps.dts home; the README documents the no-semver internal/*
contract and host ports.

* docs(ai): add minimal external-consumer example app

examples/ai-chat consumes only the public @openclaw/ai surface (built dist
via the workspace link): isolated runtime, built-in provider registration,
one streamed completion. Supports Anthropic/OpenAI via env keys and a
keyless local Ollama target; live-verified against Ollama.

* docs(ai): document the @openclaw/ai package and workspace shrinkwrap boundary

* chore(check): include examples/ in duplicate-scan targets

* fix: emit normalization package subpaths

* fix: complete AI package boundary artifacts

* fix: align AI package boundary contracts

* fix(ci): stabilize package release contracts

* test: align documentation contract checks

* test: keep cron docs guard aligned

* test: align restored docs contract guards

* test: follow upstream docs contracts

* docs: drop superseded talk wording
2026-07-05 01:56:40 -04:00

209 lines
4.7 KiB
JavaScript

#!/usr/bin/env node
// Runs duplicate-code detection with repo-specific excludes.
import { spawnSync } from "node:child_process";
import path from "node:path";
import { fileURLToPath } from "node:url";
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const jscpdBin = path.join(repoRoot, "node_modules", "jscpd", "bin", "jscpd");
const targets = [
"src",
"extensions",
"examples",
"scripts",
"packages",
"ui",
"apps",
"docs",
"qa",
"security",
"test",
"skills",
"openclaw.mjs",
"config/knip.config.ts",
"tsdown.ai.config.ts",
"tsdown.config.ts",
"vitest.config.ts",
];
const sourceExtensions = new Set([".ts", ".tsx", ".js", ".mjs", ".cjs"]);
const sourcePattern = "**/*.{ts,tsx,js,mjs,cjs}";
const testPattern = "**/*.{test,e2e.test,live.test}.{ts,tsx,js,mjs,cjs}";
// Keep local agent support trees and vendored snapshots classified but outside jscpd.
const intentionallyUnscannedPrefixes = [".agents/", "vendor/"];
const generatedIgnores = [
"extensions/qa-matrix/src/shared/**",
"extensions/qa-matrix/src/cli-paths.ts",
"**/node_modules/**",
"**/dist/**",
"**/.git/**",
"**/coverage/**",
"**/build/**",
"**/.build/**",
"**/.artifacts/**",
"vendor/**",
];
const testIgnores = [
"**/*.test.ts",
"**/*.test.tsx",
"**/*.test.js",
"**/*.test.mjs",
"**/*.test.cjs",
"**/*.e2e.test.ts",
"**/*.e2e.test.tsx",
"**/*.e2e.test.js",
"**/*.e2e.test.mjs",
"**/*.e2e.test.cjs",
"**/*.live.test.ts",
"**/*.live.test.tsx",
"**/*.live.test.js",
"**/*.live.test.mjs",
"**/*.live.test.cjs",
];
const commonArgs = [
"--format",
"typescript,javascript",
"--gitignore",
"--noSymlinks",
"--min-lines",
"50",
"--min-tokens",
"300",
];
const json = process.argv.includes("--json");
const coverageOnly = process.argv.includes("--coverage");
function normalizeRepoPath(value) {
return value.split(path.sep).join("/");
}
function isUnderPrefix(value, prefix) {
return value === prefix.slice(0, -1) || value.startsWith(prefix);
}
function isCoveredByTargets(file) {
return targets.some((target) => {
const normalizedTarget = normalizeRepoPath(target);
if (file === normalizedTarget) {
return true;
}
return file.startsWith(`${normalizedTarget}/`);
});
}
function listTrackedSourceFiles() {
const result = spawnSync("git", ["ls-files", "-z"], {
cwd: repoRoot,
encoding: "utf8",
maxBuffer: 64 * 1024 * 1024,
});
if (result.status !== 0) {
throw new Error(result.stderr || "git ls-files failed");
}
return result.stdout
.split("\0")
.filter(Boolean)
.map(normalizeRepoPath)
.filter((file) => sourceExtensions.has(path.extname(file)))
.filter((file) => !intentionallyUnscannedPrefixes.some((prefix) => isUnderPrefix(file, prefix)))
.toSorted((left, right) => left.localeCompare(right));
}
function assertTargetCoverage() {
const uncovered = listTrackedSourceFiles().filter((file) => !isCoveredByTargets(file));
if (uncovered.length === 0) {
console.log(`[dup:check] target coverage ok`);
return true;
}
console.error(
"[dup:check] tracked duplicate-scan source files are outside scan targets or intentional excludes:",
);
for (const file of uncovered) {
console.error(` - ${file}`);
}
return false;
}
function reportArgs(name) {
if (!json) {
return ["--reporters", "console"];
}
return ["--reporters", "json", "--output", path.join(".artifacts", "jscpd", name)];
}
const scans = [
{
name: "production",
targets,
pattern: sourcePattern,
ignore: [...testIgnores, ...generatedIgnores],
},
{
name: "tests",
targets,
pattern: testPattern,
ignore: generatedIgnores,
},
{
name: "src-mixed",
targets: ["src"],
pattern: sourcePattern,
ignore: generatedIgnores,
},
{
name: "extensions-mixed",
targets: ["extensions"],
pattern: sourcePattern,
ignore: generatedIgnores,
},
{
name: "test-mixed",
targets: ["test"],
pattern: sourcePattern,
ignore: generatedIgnores,
},
];
let failed = !assertTargetCoverage();
if (coverageOnly) {
process.exit(failed ? 1 : 0);
}
for (const scan of scans) {
console.log(`\n[dup:check] ${scan.name}`);
const result = spawnSync(
process.execPath,
[
"--max-old-space-size=8192",
jscpdBin,
...scan.targets,
...commonArgs,
"--pattern",
scan.pattern,
"--ignore",
scan.ignore.join(","),
...reportArgs(scan.name),
],
{
cwd: repoRoot,
env: process.env,
stdio: "inherit",
},
);
if (result.status !== 0) {
failed = true;
}
if (result.error) {
console.error(result.error.message);
failed = true;
}
}
if (failed) {
process.exit(1);
}