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
21 changes: 19 additions & 2 deletions packages/cli/src/commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1348,10 +1348,27 @@ export default class Serve extends Command {
(p: any) => p.name === 'com.objectstack.service-ai'
|| p.constructor?.name === 'AIServicePlugin'
);
// Resolve optional plugin packages from the HOST APP's context (the app
// being served declares them as deps — including private packages like
// @objectstack/service-ai-studio that the framework CLI itself does not
// depend on). A bare import would resolve relative to the CLI's location
// and miss a package linked into the app's node_modules. Falls back to a
// bare import for framework-owned packages.
const { createRequire: _createRequire } = await import('node:module');
const { pathToFileURL: _pathToFileURL } = await import('node:url');
const _nodePath = await import('node:path');
const _hostRequire = _createRequire(_nodePath.join(process.cwd(), 'package.json'));
const importFromHost = async (pkg: string): Promise<any> => {
try {
return await import(_pathToFileURL(_hostRequire.resolve(pkg)).href);
} catch {
return import(/* webpackIgnore: true */ pkg);
}
};
if (!hasAIPlugin && tierEnabled('ai')) {
try {
const aiPkg = '@objectstack/service-ai';
const { AIServicePlugin } = await import(/* webpackIgnore: true */ aiPkg);
const { AIServicePlugin } = await importFromHost(aiPkg);

// AIServicePlugin will auto-detect LLM provider from environment variables
// (AI_GATEWAY_MODEL, OPENAI_API_KEY, ANTHROPIC_API_KEY, GOOGLE_GENERATIVE_AI_API_KEY)
Expand Down Expand Up @@ -1379,7 +1396,7 @@ export default class Serve extends Command {
if (!hasAIStudio) {
try {
const studioPkg = '@objectstack/service-ai-studio';
const { AIStudioPlugin } = await import(/* webpackIgnore: true */ studioPkg);
const { AIStudioPlugin } = await importFromHost(studioPkg);
await kernel.use(new AIStudioPlugin());
trackPlugin('AIStudio');
} catch (err: unknown) {
Expand Down
31 changes: 29 additions & 2 deletions packages/runtime/src/cloud/capability-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,33 @@
*/

import type { ObjectKernel } from '@objectstack/core';
import { createRequire } from 'node:module';
import { pathToFileURL } from 'node:url';
import { join } from 'node:path';

/**
* Import an optional capability package, resolving it from the HOST APP's
* context first.
*
* The host app (a tenant runtime like apps/objectos, or an enterprise
* objectos-ee install) is what declares optional plugin packages — including
* private ones such as `@objectstack/service-ai-studio` that are NOT part of
* the framework's own dependency graph. A bare `import(pkg)` from this file
* resolves relative to the framework package's location, which cannot see a
* package linked into the *app's* node_modules (the failure surfaces as
* "Cannot find package … imported from …/framework/…"). Anchoring a
* `createRequire` at the app's cwd resolves it from the app's node_modules.
* Falls back to a bare import for framework-owned packages.
*/
async function importFromHost(pkg: string): Promise<any> {
try {
const req = createRequire(join(process.cwd(), 'package.json'));
const resolved = req.resolve(pkg);
return await import(pathToFileURL(resolved).href);
} catch {
return import(/* webpackIgnore: true */ pkg);
}
}

export interface CapabilitySpec {
/** npm package name to import. */
Expand Down Expand Up @@ -161,7 +188,7 @@ export async function loadCapabilities(opts: LoadCapabilitiesOptions): Promise<s
}

try {
const mod: any = await import(/* webpackIgnore: true */ spec.pkg);
const mod: any = await importFromHost(spec.pkg);
const Ctor = mod[spec.export];
if (!Ctor) {
logger.warn?.(
Expand All @@ -187,7 +214,7 @@ export async function loadCapabilities(opts: LoadCapabilitiesOptions): Promise<s
if (spec.extras) {
for (const ex of spec.extras) {
try {
const exMod: any = await import(/* webpackIgnore: true */ ex.pkg);
const exMod: any = await importFromHost(ex.pkg);
const ExCtor = exMod[ex.export];
if (ExCtor) {
await kernel.use(new ExCtor());
Expand Down
Loading