optimized performance

This commit is contained in:
dswbx
2025-08-14 10:05:15 +02:00
parent 70f0240da5
commit 9ac5fa03c6
28 changed files with 134 additions and 66 deletions

View File

@@ -96,6 +96,7 @@ export class App<C extends Connection = Connection, Options extends AppOptions =
private trigger_first_boot = false;
private _building: boolean = false;
private _systemController: SystemController | null = null;
constructor(
public connection: C,
@@ -172,8 +173,8 @@ export class App<C extends Connection = Connection, Options extends AppOptions =
// load system controller
guard.registerPermissions(Object.values(SystemPermissions));
const systemController = new SystemController(this);
systemController.register(this);
this._systemController = new SystemController(this);
this._systemController.register(this);
// emit built event
$console.log("App built");
@@ -205,6 +206,10 @@ export class App<C extends Connection = Connection, Options extends AppOptions =
return this.modules.ctx().em;
}
get mcp() {
return this._systemController?._mcpServer;
}
get fetch(): Hono["fetch"] {
return this.server.fetch as any;
}

View File

@@ -1,8 +1,7 @@
import type { CliCommand } from "cli/types";
import { makeAppFromEnv } from "../run";
import { getSystemMcp } from "modules/mcp/system-mcp";
import { $console } from "bknd/utils";
import { stdioTransport } from "jsonv-ts/mcp";
import { $console, stdioTransport } from "bknd/utils";
export const mcp: CliCommand = (program) =>
program

View File

@@ -27,7 +27,7 @@ export class SchemaObject<Schema extends TSchema = TSchema> {
) {
this._default = deepFreeze(_schema.template({}, { withOptional: true }) as any);
this._value = deepFreeze(
parse(_schema, structuredClone(initial ?? {}), {
parse(_schema, initial ?? {}, {
withDefaults: true,
//withExtendedDefaults: true,
forceParse: this.isForceParse(),

View File

@@ -1,19 +1,20 @@
import { createApp as createAppInternal, type CreateAppConfig } from "App";
import { bunSqlite } from "adapter/bun/connection/BunSqliteConnection";
import { Connection } from "data/connection/Connection";
import type { getSystemMcp } from "modules/mcp/system-mcp";
import { Connection, createApp as createAppInternal, type CreateAppConfig } from "bknd";
import { bunSqlite } from "bknd/adapter/bun";
import type { McpServer } from "bknd/utils";
export { App } from "App";
export { App } from "bknd";
export function createApp({ connection, ...config }: CreateAppConfig = {}) {
return createAppInternal({
...config,
connection: Connection.isConnection(connection) ? connection : bunSqlite(connection as any),
connection: Connection.isConnection(connection)
? connection
: (bunSqlite(connection as any) as any),
});
}
export function createMcpToolCaller() {
return async (server: ReturnType<typeof getSystemMcp>, name: string, args: any, raw?: any) => {
return async (server: McpServer, name: string, args: any, raw?: any) => {
const res = await server.handle(
{
jsonrpc: "2.0",

View File

@@ -1,7 +1,7 @@
import { extension, guess, isMimeType } from "media/storage/mime-types-tiny";
import { randomString } from "core/utils/strings";
import { randomString } from "./strings";
import type { Context } from "hono";
import { invariant } from "core/utils/runtime";
import { invariant } from "./runtime";
import { $console } from "./console";
export function getContentName(request: Request): string | undefined;

View File

@@ -14,3 +14,4 @@ export * from "./test";
export * from "./runtime";
export * from "./numbers";
export * from "./schema";
export { DebugLogger } from "./DebugLogger";

View File

@@ -10,6 +10,7 @@ export {
mcpTool,
mcpResource,
getMcpServer,
stdioTransport,
type ToolAnnotation,
type ToolHandlerCtx,
} from "jsonv-ts/mcp";

View File

@@ -1,6 +1,6 @@
import { StringSchema, type IStringOptions } from "jsonv-ts";
import { s } from "bknd/utils";
export class SecretSchema<O extends IStringOptions> extends StringSchema<O> {}
export class SecretSchema<O extends s.IStringOptions> extends s.StringSchema<O> {}
export const secret = <O extends IStringOptions>(o?: O): SecretSchema<O> & O =>
export const secret = <O extends s.IStringOptions>(o?: O): SecretSchema<O> & O =>
new SecretSchema(o) as any;

View File

@@ -35,6 +35,7 @@ export type { BkndConfig } from "bknd/adapter";
export * as middlewares from "modules/middlewares";
export { registries } from "modules/registries";
export { getSystemMcp } from "modules/mcp/system-mcp";
/**
* Core

View File

@@ -6,9 +6,8 @@ import type { Hono } from "hono";
import type { ServerEnv } from "modules/Controller";
import type { ModuleHelper } from "./ModuleHelper";
import { SchemaObject } from "core/object/SchemaObject";
import type { DebugLogger } from "core/utils/DebugLogger";
import type { Guard } from "auth/authorize/Guard";
import type { McpServer } from "bknd/utils";
import type { McpServer, DebugLogger } from "bknd/utils";
type PartialRec<T> = { [P in keyof T]?: PartialRec<T[P]> };

View File

@@ -1,8 +1,16 @@
import { mark, stripMark, $console, s, objectEach, transformObject, McpServer } from "bknd/utils";
import {
mark,
stripMark,
$console,
s,
objectEach,
transformObject,
McpServer,
DebugLogger,
} from "bknd/utils";
import { Guard } from "auth/authorize/Guard";
import { env } from "core/env";
import { BkndError } from "core/errors";
import { DebugLogger } from "core/utils/DebugLogger";
import { EventManager, Event } from "core/events";
import * as $diff from "core/object/diff";
import type { Connection } from "data/connection";

View File

@@ -8,12 +8,9 @@ export function getSystemMcp(app: App) {
const appConfig = app.modules.configs();
const { version, ...appSchema } = app.getSchema();
const schema = s.strictObject(appSchema);
const nodes = [...schema.walk({ data: appConfig })].filter(
(n) => isObject(n.schema) && mcpSchemaSymbol in n.schema,
) as s.Node<McpSchema>[];
const result = [...schema.walk({ maxDepth: 3 })];
const nodes = result.filter((n) => mcpSchemaSymbol in n.schema) as s.Node<McpSchema>[];
const tools = [
// tools from hono routes
...middlewareServer.tools,

View File

@@ -16,6 +16,7 @@ import {
mcpTool,
mcp as mcpMiddleware,
isNode,
type McpServer,
} from "bknd/utils";
import type { Context, Hono } from "hono";
import { Controller } from "modules/Controller";
@@ -47,6 +48,8 @@ export type SchemaResponse = {
};
export class SystemController extends Controller {
_mcpServer: McpServer | null = null;
constructor(private readonly app: App) {
super();
}
@@ -64,8 +67,8 @@ export class SystemController extends Controller {
this.registerMcp();
const mcpServer = getSystemMcp(app);
mcpServer.onNotification((message) => {
this._mcpServer = getSystemMcp(app);
this._mcpServer.onNotification((message) => {
if (message.method === "notification/message") {
const consoleMap = {
emergency: "error",
@@ -87,7 +90,7 @@ export class SystemController extends Controller {
app.server.use(
mcpMiddleware({
server: mcpServer,
server: this._mcpServer,
sessionsEnabled: true,
debug: {
logLevel: "debug",