Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions apps/desktop/electron-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ extraResources:
# picks up sibling `.ts` source files too; exclude them so we don't
# bloat the installer with dead source.
- '**/*.md'
# Service manifests and their declared files are read during server startup.
- from: ../server/dist-bundle/services
to: server/services
filter:
- '**/*'
- '!**/*.test.*'
- from: ../server/dist-bundle/agentlet
to: server/agentlet
filter:
Expand Down
89 changes: 88 additions & 1 deletion apps/server/src/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@

import { execFileSync, spawn } from 'node:child_process';
import { once } from 'node:events';
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import {
cpSync,
mkdtempSync,
readFileSync,
readdirSync,
rmSync,
writeFileSync,
} from 'node:fs';
import { createServer } from 'node:http';
import { tmpdir } from 'node:os';
import path from 'node:path';
Expand All @@ -12,19 +19,99 @@ import { pathToFileURL } from 'node:url';
import archiver from 'archiver';
import { build } from 'tsup';
import { describe, expect, it } from 'vitest';
import { parse } from 'yaml';

import {
buildHuabuBootstrapEnv,
resolveRuntimeArtifacts,
} from '../../../packages/client-runtime/src/bootstrap.js';
import clientRuntimeConfig from '../../../packages/client-runtime/tsup.config.js';
import bundleConfig from '../tsup.config.js';
import { getBundledServicePackages } from './modules/services/package-loader.js';

const configured =
typeof bundleConfig === 'function' ? await bundleConfig({}) : bundleConfig;
const configurations = Array.isArray(configured) ? configured : [configured];

describe('server bundle runtime', () => {
it('ships and loads every Service package in the desktop resource layout', async () => {
const desktopConfig = parse(
readFileSync(path.resolve('../desktop/electron-builder.yml'), 'utf8'),
);
const serviceResource = desktopConfig.extraResources.find(
(resource: { from: string }) =>
resource.from === '../server/dist-bundle/services',
);
expect(serviceResource).toEqual({
from: '../server/dist-bundle/services',
to: 'server/services',
filter: ['**/*', '!**/*.test.*'],
});

const directory = mkdtempSync(path.join(tmpdir(), 'huabu-services-'));
try {
const resources = path.join(directory, 'resources');
const sourceRoot = path.resolve('src/services');
const packagedRoot = path.join(resources, serviceResource.to);
const exclusions: string[] = serviceResource.filter
.filter((pattern: string) => pattern.startsWith('!'))
.map((pattern: string) => pattern.slice(1));
cpSync(sourceRoot, packagedRoot, {
recursive: true,
filter: (source) =>
!exclusions.some((pattern) =>
path.matchesGlob(path.relative(sourceRoot, source), pattern),
),
});
const isTestFile = (file: string) =>
path.matchesGlob(file, '**/*.test.*');
expect(
readdirSync(sourceRoot, {
recursive: true,
encoding: 'utf8',
}).some(isTestFile),
).toBe(true);
expect(
readdirSync(packagedRoot, {
recursive: true,
encoding: 'utf8',
}).some(isTestFile),
).toBe(false);
const entry = path.join(resources, 'server/server.mjs');
await build({
...configurations[0],
config: false,
entry: {
server: path.resolve('src/modules/services/package-loader.ts'),
},
outDir: path.dirname(entry),
outExtension: () => ({ js: '.mjs' }),
onSuccess: undefined,
silent: true,
});
const summarize =
'(m) => [...m.getBundledServicePackages()].map(([id, p]) => [id, p.contentHash])';
const output = execFileSync(
process.execPath,
[
'--input-type=module',
'-e',
`const m = await import(process.argv[1]); console.log(JSON.stringify((${summarize})(m)));`,
pathToFileURL(entry).href,
],
{ cwd: directory, encoding: 'utf8' },
);
expect(JSON.parse(output)).toEqual(
[...getBundledServicePackages()].map(([id, service]) => [
id,
service.contentHash,
]),
);
} finally {
rmSync(directory, { recursive: true, force: true });
}
});

it('loads real Agentlet and client-runtime artifacts outside the workspace without node_modules', async () => {
const directory = mkdtempSync(path.join(tmpdir(), 'huabu-real-runtime-'));
try {
Expand Down
2 changes: 2 additions & 0 deletions docs/architecture/service-packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ The bundled packages live under `apps/server/src/services/<id>/`:

Phase 1 packages are trusted files shipped with Huabu. User upload, installation, remote registries, and Server execution of uploaded code are not supported.

Server bundling copies the Service tree to `apps/server/dist-bundle/services/`. Desktop packaging includes that tree at `resources/server/services/`, alongside `server.js`, excluding `*.test.*` source files while retaining every `service.yaml` and its declared runtime Package files. The loader validates these files during module initialization, so missing resources prevent the local Server from starting even when no Service has been configured. The bundle runtime regression test verifies the desktop resource mapping, excludes test sources, and loads the packages from an isolated packaged layout without the source-tree fallback.

## Manifest contract

`huabu-service/v1` is defined by `serviceManifestSchema` in `packages/shared/src/types/api/service-package.ts`. Unknown properties, malformed IDs, duplicate configuration IDs, invalid enum declarations, ambiguous Package paths, duplicate Package files, and `entry.mjs` without `SKILL.md` fail validation.
Expand Down
Loading