ont-run 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts CHANGED
@@ -5,6 +5,7 @@
5
5
  * ```ts
6
6
  * import { defineOntology, fieldFrom } from 'ont-run';
7
7
  * import { z } from 'zod';
8
+ * import { hello } from './resolvers/hello.js';
8
9
  *
9
10
  * export default defineOntology({
10
11
  * name: 'my-api',
@@ -23,7 +24,7 @@
23
24
  * access: ['public'],
24
25
  * entities: [],
25
26
  * inputs: z.object({ name: z.string() }),
26
- * resolver: './resolvers/hello.ts',
27
+ * resolver: hello, // Direct function reference for type safety
27
28
  * },
28
29
  * },
29
30
  * });
@@ -31,7 +32,7 @@
31
32
  */
32
33
 
33
34
  // Main API
34
- export { defineOntology } from "./config/define.js";
35
+ export { defineOntology, defineFunction } from "./config/define.js";
35
36
  export { fieldFrom, userContext } from "./config/categorical.js";
36
37
  export { startOnt } from "./server/start.js";
37
38
  export type { StartOntOptions, StartOntResult } from "./server/start.js";
@@ -1,6 +1,5 @@
1
1
  import { Hono } from "hono";
2
2
  import { cors } from "hono/cors";
3
- import consola from "consola";
4
3
  import type { OntologyConfig } from "../../config/types.js";
5
4
  import {
6
5
  createAuthMiddleware,
@@ -9,13 +8,10 @@ import {
9
8
  type OntologyVariables,
10
9
  } from "./middleware.js";
11
10
  import { createApiRoutes, getFunctionsInfo } from "./router.js";
12
- import { findMissingResolvers } from "../resolver.js";
13
11
 
14
12
  export interface ApiServerOptions {
15
13
  /** The ontology configuration */
16
14
  config: OntologyConfig;
17
- /** Directory containing the ontology.config.ts (for resolving resolver paths) */
18
- configDir: string;
19
15
  /** Environment to use (e.g., 'dev', 'prod') */
20
16
  env: string;
21
17
  /** Enable CORS (default: true) */
@@ -26,7 +22,7 @@ export interface ApiServerOptions {
26
22
  * Create the Hono API app from an OntologyConfig
27
23
  */
28
24
  export function createApiApp(options: ApiServerOptions): Hono<{ Variables: OntologyVariables }> {
29
- const { config, configDir, env, cors: enableCors = true } = options;
25
+ const { config, env, cors: enableCors = true } = options;
30
26
 
31
27
  // Get environment config
32
28
  const envConfig = config.environments[env];
@@ -36,15 +32,6 @@ export function createApiApp(options: ApiServerOptions): Hono<{ Variables: Ontol
36
32
  );
37
33
  }
38
34
 
39
- // Check for missing resolvers
40
- const missingResolvers = findMissingResolvers(config, configDir);
41
- if (missingResolvers.length > 0) {
42
- consola.warn(`Missing resolvers (${missingResolvers.length}):`);
43
- for (const resolver of missingResolvers) {
44
- consola.warn(` - ${resolver}`);
45
- }
46
- }
47
-
48
35
  const app = new Hono<{ Variables: OntologyVariables }>();
49
36
 
50
37
  // Global middleware
@@ -83,7 +70,7 @@ export function createApiApp(options: ApiServerOptions): Hono<{ Variables: Ontol
83
70
  });
84
71
 
85
72
  // Mount function routes under /api
86
- const apiRoutes = createApiRoutes(config, configDir);
73
+ const apiRoutes = createApiRoutes(config);
87
74
  app.route("/api", apiRoutes);
88
75
 
89
76
  return app;
@@ -1,6 +1,5 @@
1
1
  import { Hono } from "hono";
2
2
  import type { OntologyConfig, FunctionDefinition } from "../../config/types.js";
3
- import { loadResolver } from "../resolver.js";
4
3
  import { getUserContextFields } from "../../config/categorical.js";
5
4
  import {
6
5
  createAccessControlMiddleware,
@@ -11,8 +10,7 @@ import {
11
10
  * Create API routes from function definitions
12
11
  */
13
12
  export function createApiRoutes(
14
- config: OntologyConfig,
15
- configDir: string
13
+ config: OntologyConfig
16
14
  ): Hono<{ Variables: OntologyVariables }> {
17
15
  const router = new Hono<{ Variables: OntologyVariables }>();
18
16
 
@@ -83,10 +81,9 @@ export function createApiRoutes(
83
81
  args = parsed.data;
84
82
  }
85
83
 
86
- // Load and execute resolver
84
+ // Execute resolver
87
85
  try {
88
- const resolver = await loadResolver(fn.resolver, configDir);
89
- const result = await resolver(resolverContext, args);
86
+ const result = await fn.resolver(resolverContext, args);
90
87
  return c.json(result);
91
88
  } catch (error) {
92
89
  console.error(`Error in resolver ${name}:`, error);
@@ -39,8 +39,6 @@ function getAuthResult(authInfo?: AuthInfo): AuthResult {
39
39
  export interface McpServerOptions {
40
40
  /** The ontology configuration */
41
41
  config: OntologyConfig;
42
- /** Directory containing the ontology.config.ts */
43
- configDir: string;
44
42
  /** Environment to use */
45
43
  env: string;
46
44
  /** Port for the MCP HTTP server */
@@ -51,7 +49,7 @@ export interface McpServerOptions {
51
49
  * Create the MCP server instance with per-request authentication
52
50
  */
53
51
  export function createMcpServer(options: McpServerOptions): Server {
54
- const { config, configDir, env } = options;
52
+ const { config, env } = options;
55
53
 
56
54
  // Get environment config
57
55
  const envConfig = config.environments[env];
@@ -67,7 +65,7 @@ export function createMcpServer(options: McpServerOptions): Server {
67
65
  const allTools = generateMcpTools(config);
68
66
 
69
67
  // Create tool executor factory that accepts per-request access groups
70
- const executeToolWithAccess = createToolExecutor(config, configDir, env, envConfig, logger);
68
+ const executeToolWithAccess = createToolExecutor(config, env, envConfig, logger);
71
69
 
72
70
  // Create MCP server
73
71
  const server = new Server(
@@ -8,7 +8,7 @@ import type {
8
8
  AuthResult,
9
9
  } from "../../config/types.js";
10
10
  import { getFieldFromMetadata, getUserContextFields, hasUserContextMetadata } from "../../config/categorical.js";
11
- import { loadResolver, type Logger } from "../resolver.js";
11
+ import type { Logger } from "../resolver.js";
12
12
 
13
13
  /**
14
14
  * Field reference info for MCP tools
@@ -198,7 +198,6 @@ export function filterToolsByAccess(
198
198
  */
199
199
  export function createToolExecutor(
200
200
  config: OntologyConfig,
201
- configDir: string,
202
201
  env: string,
203
202
  envConfig: EnvironmentConfig,
204
203
  logger: Logger
@@ -253,8 +252,7 @@ export function createToolExecutor(
253
252
  accessGroups: authResult.groups,
254
253
  };
255
254
 
256
- // Load and execute resolver
257
- const resolver = await loadResolver(fn.resolver, configDir);
258
- return resolver(resolverContext, parsed.data);
255
+ // Execute resolver
256
+ return fn.resolver(resolverContext, parsed.data);
259
257
  };
260
258
  }
@@ -1,84 +1,11 @@
1
- import { join, dirname, isAbsolute } from "path";
2
- import { existsSync } from "fs";
3
- import type { ResolverFunction, ResolverContext, OntologyConfig } from "../config/types.js";
1
+ import type { ResolverFunction } from "../config/types.js";
4
2
 
5
3
  /**
6
- * Cache of loaded resolvers to avoid re-importing
4
+ * Get a resolver function. Since resolvers are now passed directly as functions,
5
+ * this is a simple passthrough that could be removed in the future.
7
6
  */
8
- const resolverCache = new Map<string, ResolverFunction>();
9
-
10
- /**
11
- * Load a resolver from a file path.
12
- * The path is relative to the config file location.
13
- *
14
- * @param resolverPath - Path to the resolver file (relative to configDir)
15
- * @param configDir - Directory containing the ontology.config.ts
16
- */
17
- export async function loadResolver(
18
- resolverPath: string,
19
- configDir: string
20
- ): Promise<ResolverFunction> {
21
- // Resolve the full path
22
- const fullPath = isAbsolute(resolverPath)
23
- ? resolverPath
24
- : join(configDir, resolverPath);
25
-
26
- // Check cache
27
- if (resolverCache.has(fullPath)) {
28
- return resolverCache.get(fullPath)!;
29
- }
30
-
31
- try {
32
- // Dynamic import the resolver
33
- const module = await import(fullPath);
34
-
35
- // Expect default export to be the resolver function
36
- const resolver = module.default;
37
-
38
- if (typeof resolver !== "function") {
39
- throw new Error(
40
- `Resolver at ${resolverPath} must export a default function`
41
- );
42
- }
43
-
44
- // Cache and return
45
- resolverCache.set(fullPath, resolver);
46
- return resolver;
47
- } catch (error) {
48
- if ((error as NodeJS.ErrnoException).code === "ERR_MODULE_NOT_FOUND") {
49
- throw new Error(`Resolver not found: ${resolverPath}`);
50
- }
51
- throw error;
52
- }
53
- }
54
-
55
- /**
56
- * Clear the resolver cache (useful for hot reloading)
57
- */
58
- export function clearResolverCache(): void {
59
- resolverCache.clear();
60
- }
61
-
62
- /**
63
- * Check which resolvers are missing and return their paths
64
- */
65
- export function findMissingResolvers(
66
- config: OntologyConfig,
67
- configDir: string
68
- ): string[] {
69
- const missing: string[] = [];
70
-
71
- for (const [name, fn] of Object.entries(config.functions)) {
72
- const fullPath = isAbsolute(fn.resolver)
73
- ? fn.resolver
74
- : join(configDir, fn.resolver);
75
-
76
- if (!existsSync(fullPath)) {
77
- missing.push(fn.resolver);
78
- }
79
- }
80
-
81
- return missing;
7
+ export function loadResolver(resolver: ResolverFunction): ResolverFunction {
8
+ return resolver;
82
9
  }
83
10
 
84
11
  /**
@@ -121,7 +121,6 @@ export async function startOnt(options: StartOntOptions = {}): Promise<StartOntR
121
121
  if (!mcpOnly) {
122
122
  const api = createApiApp({
123
123
  config,
124
- configDir,
125
124
  env,
126
125
  });
127
126
 
@@ -144,7 +143,6 @@ export async function startOnt(options: StartOntOptions = {}): Promise<StartOntR
144
143
  if (!apiOnly) {
145
144
  const mcpServer = await startMcpServer({
146
145
  config,
147
- configDir,
148
146
  env,
149
147
  port: mcpPort,
150
148
  });