@lushly-dev/afd-server 0.1.0
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/LICENSE +26 -0
- package/README.md +381 -0
- package/dist/bootstrap/afd-docs.d.ts +27 -0
- package/dist/bootstrap/afd-docs.d.ts.map +1 -0
- package/dist/bootstrap/afd-docs.js +93 -0
- package/dist/bootstrap/afd-docs.js.map +1 -0
- package/dist/bootstrap/afd-help.d.ts +39 -0
- package/dist/bootstrap/afd-help.d.ts.map +1 -0
- package/dist/bootstrap/afd-help.js +77 -0
- package/dist/bootstrap/afd-help.js.map +1 -0
- package/dist/bootstrap/afd-schema.d.ts +34 -0
- package/dist/bootstrap/afd-schema.d.ts.map +1 -0
- package/dist/bootstrap/afd-schema.js +78 -0
- package/dist/bootstrap/afd-schema.js.map +1 -0
- package/dist/bootstrap/index.d.ts +11 -0
- package/dist/bootstrap/index.d.ts.map +1 -0
- package/dist/bootstrap/index.js +11 -0
- package/dist/bootstrap/index.js.map +1 -0
- package/dist/bootstrap/registry.d.ts +17 -0
- package/dist/bootstrap/registry.d.ts.map +1 -0
- package/dist/bootstrap/registry.js +23 -0
- package/dist/bootstrap/registry.js.map +1 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +60 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware.d.ts +125 -0
- package/dist/middleware.d.ts.map +1 -0
- package/dist/middleware.js +182 -0
- package/dist/middleware.js.map +1 -0
- package/dist/schema.d.ts +105 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +132 -0
- package/dist/schema.js.map +1 -0
- package/dist/server.d.ts +141 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +846 -0
- package/dist/server.js.map +1 -0
- package/dist/validation.d.ts +137 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +182 -0
- package/dist/validation.js.map +1 -0
- package/package.json +48 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview afd-schema bootstrap command
|
|
3
|
+
*
|
|
4
|
+
* Export JSON schemas for all commands.
|
|
5
|
+
*/
|
|
6
|
+
import { z } from 'zod';
|
|
7
|
+
import type { CommandDefinition } from '@lushly-dev/afd-core';
|
|
8
|
+
declare const inputSchema: z.ZodObject<{
|
|
9
|
+
format: z.ZodDefault<z.ZodEnum<["json", "typescript"]>>;
|
|
10
|
+
}, "strip", z.ZodTypeAny, {
|
|
11
|
+
format: "json" | "typescript";
|
|
12
|
+
}, {
|
|
13
|
+
format?: "json" | "typescript" | undefined;
|
|
14
|
+
}>;
|
|
15
|
+
type InputType = z.infer<typeof inputSchema>;
|
|
16
|
+
interface SchemaInfo {
|
|
17
|
+
name: string;
|
|
18
|
+
description: string;
|
|
19
|
+
inputSchema: Record<string, unknown>;
|
|
20
|
+
}
|
|
21
|
+
interface SchemaOutput {
|
|
22
|
+
schemas: SchemaInfo[];
|
|
23
|
+
count: number;
|
|
24
|
+
format: 'json' | 'typescript';
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Create the afd-schema bootstrap command.
|
|
28
|
+
*
|
|
29
|
+
* @param getCommands - Function to get all registered commands
|
|
30
|
+
* @param getJsonSchema - Function to get JSON schema for a command
|
|
31
|
+
*/
|
|
32
|
+
export declare function createAfdSchemaCommand(getCommands: () => CommandDefinition[], getJsonSchema?: (cmd: CommandDefinition) => Record<string, unknown>): CommandDefinition<InputType, SchemaOutput>;
|
|
33
|
+
export {};
|
|
34
|
+
//# sourceMappingURL=afd-schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"afd-schema.d.ts","sourceRoot":"","sources":["../../src/bootstrap/afd-schema.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAG9D,QAAA,MAAM,WAAW;;;;;;EAEf,CAAC;AAEH,KAAK,SAAS,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,WAAW,CAAC,CAAC;AAE7C,UAAU,UAAU;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACrC;AAED,UAAU,YAAY;IACrB,OAAO,EAAE,UAAU,EAAE,CAAC;IACtB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,GAAG,YAAY,CAAC;CAC9B;AAED;;;;;GAKG;AACH,wBAAgB,sBAAsB,CACrC,WAAW,EAAE,MAAM,iBAAiB,EAAE,EACtC,aAAa,CAAC,EAAE,CAAC,GAAG,EAAE,iBAAiB,KAAK,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GACjE,iBAAiB,CAAC,SAAS,EAAE,YAAY,CAAC,CAwE5C"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview afd-schema bootstrap command
|
|
3
|
+
*
|
|
4
|
+
* Export JSON schemas for all commands.
|
|
5
|
+
*/
|
|
6
|
+
import { z } from 'zod';
|
|
7
|
+
import { success } from '@lushly-dev/afd-core';
|
|
8
|
+
const inputSchema = z.object({
|
|
9
|
+
format: z.enum(['json', 'typescript']).default('json').describe('Output format'),
|
|
10
|
+
});
|
|
11
|
+
/**
|
|
12
|
+
* Create the afd-schema bootstrap command.
|
|
13
|
+
*
|
|
14
|
+
* @param getCommands - Function to get all registered commands
|
|
15
|
+
* @param getJsonSchema - Function to get JSON schema for a command
|
|
16
|
+
*/
|
|
17
|
+
export function createAfdSchemaCommand(getCommands, getJsonSchema) {
|
|
18
|
+
return {
|
|
19
|
+
name: 'afd-schema',
|
|
20
|
+
description: 'Export JSON schemas for all commands',
|
|
21
|
+
category: 'bootstrap',
|
|
22
|
+
tags: ['bootstrap', 'read', 'safe'],
|
|
23
|
+
mutation: false,
|
|
24
|
+
version: '1.0.0',
|
|
25
|
+
parameters: [
|
|
26
|
+
{ name: 'format', type: 'string', required: false, description: 'Output format' },
|
|
27
|
+
],
|
|
28
|
+
async handler(input) {
|
|
29
|
+
const commands = getCommands();
|
|
30
|
+
const schemas = commands.map(cmd => {
|
|
31
|
+
// Try to get JSON schema from the command or use getJsonSchema function
|
|
32
|
+
let schema = {};
|
|
33
|
+
if (getJsonSchema) {
|
|
34
|
+
schema = getJsonSchema(cmd);
|
|
35
|
+
}
|
|
36
|
+
else if ('jsonSchema' in cmd && cmd.jsonSchema) {
|
|
37
|
+
schema = cmd.jsonSchema;
|
|
38
|
+
}
|
|
39
|
+
else if (cmd.parameters) {
|
|
40
|
+
// Build basic schema from parameters
|
|
41
|
+
schema = {
|
|
42
|
+
type: 'object',
|
|
43
|
+
properties: Object.fromEntries(cmd.parameters.map(p => [
|
|
44
|
+
p.name,
|
|
45
|
+
{
|
|
46
|
+
type: p.type === 'string' ? 'string' :
|
|
47
|
+
p.type === 'number' ? 'number' :
|
|
48
|
+
p.type === 'boolean' ? 'boolean' : 'any',
|
|
49
|
+
description: p.description,
|
|
50
|
+
}
|
|
51
|
+
])),
|
|
52
|
+
required: cmd.parameters
|
|
53
|
+
.filter(p => p.required)
|
|
54
|
+
.map(p => p.name),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
name: cmd.name,
|
|
59
|
+
description: cmd.description,
|
|
60
|
+
inputSchema: schema,
|
|
61
|
+
};
|
|
62
|
+
});
|
|
63
|
+
// TODO: TypeScript format generation
|
|
64
|
+
if (input.format === 'typescript') {
|
|
65
|
+
// For now, just return JSON with a note
|
|
66
|
+
return success({ schemas, count: schemas.length, format: 'typescript' }, {
|
|
67
|
+
reasoning: `Exported ${schemas.length} schemas (TypeScript format coming soon)`,
|
|
68
|
+
confidence: 0.8,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
return success({ schemas, count: schemas.length, format: 'json' }, {
|
|
72
|
+
reasoning: `Exported JSON schemas for ${schemas.length} commands`,
|
|
73
|
+
confidence: 1.0,
|
|
74
|
+
});
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=afd-schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"afd-schema.js","sourceRoot":"","sources":["../../src/bootstrap/afd-schema.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAE/C,MAAM,WAAW,GAAG,CAAC,CAAC,MAAM,CAAC;IAC5B,MAAM,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,eAAe,CAAC;CAChF,CAAC,CAAC;AAgBH;;;;;GAKG;AACH,MAAM,UAAU,sBAAsB,CACrC,WAAsC,EACtC,aAAmE;IAEnE,OAAO;QACN,IAAI,EAAE,YAAY;QAClB,WAAW,EAAE,sCAAsC;QACnD,QAAQ,EAAE,WAAW;QACrB,IAAI,EAAE,CAAC,WAAW,EAAE,MAAM,EAAE,MAAM,CAAC;QACnC,QAAQ,EAAE,KAAK;QACf,OAAO,EAAE,OAAO;QAChB,UAAU,EAAE;YACX,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,WAAW,EAAE,eAAe,EAAE;SACjF;QAED,KAAK,CAAC,OAAO,CAAC,KAAgB;YAC7B,MAAM,QAAQ,GAAG,WAAW,EAAE,CAAC;YAE/B,MAAM,OAAO,GAAiB,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;gBAChD,wEAAwE;gBACxE,IAAI,MAAM,GAA4B,EAAE,CAAC;gBAEz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EAAE,KAAK,EAAE,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,EACxD;oBACC,SAAS,EAAE,YAAY,OAAO,CAAC,MAAM,0CAA0C;oBAC/E,UAAU,EAAE,GAAG;iBACf,CACD,CAAC;YACH,CAAC;YAED,OAAO,OAAO,CACb,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,EAClD;gBACC,SAAS,EAAE,6BAA6B,OAAO,CAAC,MAAM,WAAW;gBACjE,UAAU,EAAE,GAAG;aACf,CACD,CAAC;QACH,CAAC;KACD,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview AFD Bootstrap Tools
|
|
3
|
+
*
|
|
4
|
+
* These tools are automatically added to every AFD MCP server to enable
|
|
5
|
+
* agent discovery and onboarding.
|
|
6
|
+
*/
|
|
7
|
+
export { createAfdHelpCommand } from './afd-help.js';
|
|
8
|
+
export { createAfdDocsCommand } from './afd-docs.js';
|
|
9
|
+
export { createAfdSchemaCommand } from './afd-schema.js';
|
|
10
|
+
export { getBootstrapCommands } from './registry.js';
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/bootstrap/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AAEzD,OAAO,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview AFD Bootstrap Tools
|
|
3
|
+
*
|
|
4
|
+
* These tools are automatically added to every AFD MCP server to enable
|
|
5
|
+
* agent discovery and onboarding.
|
|
6
|
+
*/
|
|
7
|
+
export { createAfdHelpCommand } from './afd-help.js';
|
|
8
|
+
export { createAfdDocsCommand } from './afd-docs.js';
|
|
9
|
+
export { createAfdSchemaCommand } from './afd-schema.js';
|
|
10
|
+
export { getBootstrapCommands } from './registry.js';
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/bootstrap/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AAEzD,OAAO,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Bootstrap commands registry
|
|
3
|
+
*
|
|
4
|
+
* Provides a function to get all bootstrap commands for an AFD server.
|
|
5
|
+
*/
|
|
6
|
+
import type { CommandDefinition } from '@lushly-dev/afd-core';
|
|
7
|
+
/**
|
|
8
|
+
* Get all bootstrap commands for an AFD server.
|
|
9
|
+
*
|
|
10
|
+
* @param getCommands - Function to get all user-defined commands
|
|
11
|
+
* @param options - Optional configuration
|
|
12
|
+
* @returns Array of bootstrap command definitions
|
|
13
|
+
*/
|
|
14
|
+
export declare function getBootstrapCommands(getCommands: () => CommandDefinition[], options?: {
|
|
15
|
+
getJsonSchema?: (cmd: CommandDefinition) => Record<string, unknown>;
|
|
16
|
+
}): CommandDefinition[];
|
|
17
|
+
//# sourceMappingURL=registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/bootstrap/registry.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAK9D;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CACnC,WAAW,EAAE,MAAM,iBAAiB,EAAE,EACtC,OAAO,CAAC,EAAE;IACT,aAAa,CAAC,EAAE,CAAC,GAAG,EAAE,iBAAiB,KAAK,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpE,GACC,iBAAiB,EAAE,CAMrB"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Bootstrap commands registry
|
|
3
|
+
*
|
|
4
|
+
* Provides a function to get all bootstrap commands for an AFD server.
|
|
5
|
+
*/
|
|
6
|
+
import { createAfdHelpCommand } from './afd-help.js';
|
|
7
|
+
import { createAfdDocsCommand } from './afd-docs.js';
|
|
8
|
+
import { createAfdSchemaCommand } from './afd-schema.js';
|
|
9
|
+
/**
|
|
10
|
+
* Get all bootstrap commands for an AFD server.
|
|
11
|
+
*
|
|
12
|
+
* @param getCommands - Function to get all user-defined commands
|
|
13
|
+
* @param options - Optional configuration
|
|
14
|
+
* @returns Array of bootstrap command definitions
|
|
15
|
+
*/
|
|
16
|
+
export function getBootstrapCommands(getCommands, options) {
|
|
17
|
+
return [
|
|
18
|
+
createAfdHelpCommand(getCommands),
|
|
19
|
+
createAfdDocsCommand(getCommands),
|
|
20
|
+
createAfdSchemaCommand(getCommands, options?.getJsonSchema),
|
|
21
|
+
];
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/bootstrap/registry.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AAEzD;;;;;;GAMG;AACH,MAAM,UAAU,oBAAoB,CACnC,WAAsC,EACtC,OAEC;IAED,OAAO;QACN,oBAAoB,CAAC,WAAW,CAAiC;QACjE,oBAAoB,CAAC,WAAW,CAAiC;QACjE,sBAAsB,CAAC,WAAW,EAAE,OAAO,EAAE,aAAa,CAAiC;KAC3F,CAAC;AACH,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview @lushly-dev/afd-server - Server-side utilities for AFD
|
|
3
|
+
*
|
|
4
|
+
* This package provides tools for building MCP servers with:
|
|
5
|
+
* - Zod-based command definitions with automatic validation
|
|
6
|
+
* - MCP server factory with multiple transport support:
|
|
7
|
+
* - stdio: For IDE/agent integration (Cursor, Claude Code, Antigravity)
|
|
8
|
+
* - http/SSE: For browser-based clients and web UIs
|
|
9
|
+
* - auto: Auto-detect based on environment (default)
|
|
10
|
+
* - Middleware for logging, tracing, rate limiting
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { z } from 'zod';
|
|
15
|
+
* import { defineCommand, createMcpServer, success } from '@lushly-dev/afd-server';
|
|
16
|
+
*
|
|
17
|
+
* const greet = defineCommand({
|
|
18
|
+
* name: 'greet',
|
|
19
|
+
* description: 'Greet a user',
|
|
20
|
+
* input: z.object({
|
|
21
|
+
* name: z.string(),
|
|
22
|
+
* }),
|
|
23
|
+
* async handler(input) {
|
|
24
|
+
* return success({ message: `Hello, ${input.name}!` });
|
|
25
|
+
* },
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* // Auto-detect transport (recommended)
|
|
29
|
+
* const server = createMcpServer({
|
|
30
|
+
* name: 'my-server',
|
|
31
|
+
* version: '1.0.0',
|
|
32
|
+
* commands: [greet],
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* // Or explicitly set transport for IDE integration
|
|
36
|
+
* const stdioServer = createMcpServer({
|
|
37
|
+
* name: 'my-server',
|
|
38
|
+
* version: '1.0.0',
|
|
39
|
+
* commands: [greet],
|
|
40
|
+
* transport: 'stdio',
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* await server.start();
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @packageDocumentation
|
|
47
|
+
*/
|
|
48
|
+
export { success, failure, isSuccess, isFailure } from '@lushly-dev/afd-core';
|
|
49
|
+
export { defineCommand, zodToJsonSchema, getRequiredFields, isObjectSchema, type ZodCommandOptions, type ZodCommandDefinition, } from './schema.js';
|
|
50
|
+
export { createMcpServer, isStdinPiped, type McpServerOptions, type McpServer, type McpTransport, type CommandMiddleware, } from './server.js';
|
|
51
|
+
export { validateInput, validateOrThrow, isValid, formatValidationErrors, ValidationException, patterns, optional, withDefault, type ValidationResult, type ValidationError, } from './validation.js';
|
|
52
|
+
export { createLoggingMiddleware, createTimingMiddleware, createRetryMiddleware, createTracingMiddleware, createRateLimitMiddleware, composeMiddleware, type LoggingOptions, type TimingOptions, type RetryOptions, type TracingOptions, type RateLimitOptions, type Tracer, type Span, } from './middleware.js';
|
|
53
|
+
export { getBootstrapCommands, createAfdHelpCommand, createAfdDocsCommand, createAfdSchemaCommand, } from './bootstrap/index.js';
|
|
54
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AAGH,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAG9E,OAAO,EACN,aAAa,EACb,eAAe,EACf,iBAAiB,EACjB,cAAc,EACd,KAAK,iBAAiB,EACtB,KAAK,oBAAoB,GACzB,MAAM,aAAa,CAAC;AAGrB,OAAO,EACN,eAAe,EACf,YAAY,EACZ,KAAK,gBAAgB,EACrB,KAAK,SAAS,EACd,KAAK,YAAY,EACjB,KAAK,iBAAiB,GACtB,MAAM,aAAa,CAAC;AAGrB,OAAO,EACN,aAAa,EACb,eAAe,EACf,OAAO,EACP,sBAAsB,EACtB,mBAAmB,EACnB,QAAQ,EACR,QAAQ,EACR,WAAW,EACX,KAAK,gBAAgB,EACrB,KAAK,eAAe,GACpB,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EACN,uBAAuB,EACvB,sBAAsB,EACtB,qBAAqB,EACrB,uBAAuB,EACvB,yBAAyB,EACzB,iBAAiB,EACjB,KAAK,cAAc,EACnB,KAAK,aAAa,EAClB,KAAK,YAAY,EACjB,KAAK,cAAc,EACnB,KAAK,gBAAgB,EACrB,KAAK,MAAM,EACX,KAAK,IAAI,GACT,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EACN,oBAAoB,EACpB,oBAAoB,EACpB,oBAAoB,EACpB,sBAAsB,GACtB,MAAM,sBAAsB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview @lushly-dev/afd-server - Server-side utilities for AFD
|
|
3
|
+
*
|
|
4
|
+
* This package provides tools for building MCP servers with:
|
|
5
|
+
* - Zod-based command definitions with automatic validation
|
|
6
|
+
* - MCP server factory with multiple transport support:
|
|
7
|
+
* - stdio: For IDE/agent integration (Cursor, Claude Code, Antigravity)
|
|
8
|
+
* - http/SSE: For browser-based clients and web UIs
|
|
9
|
+
* - auto: Auto-detect based on environment (default)
|
|
10
|
+
* - Middleware for logging, tracing, rate limiting
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { z } from 'zod';
|
|
15
|
+
* import { defineCommand, createMcpServer, success } from '@lushly-dev/afd-server';
|
|
16
|
+
*
|
|
17
|
+
* const greet = defineCommand({
|
|
18
|
+
* name: 'greet',
|
|
19
|
+
* description: 'Greet a user',
|
|
20
|
+
* input: z.object({
|
|
21
|
+
* name: z.string(),
|
|
22
|
+
* }),
|
|
23
|
+
* async handler(input) {
|
|
24
|
+
* return success({ message: `Hello, ${input.name}!` });
|
|
25
|
+
* },
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* // Auto-detect transport (recommended)
|
|
29
|
+
* const server = createMcpServer({
|
|
30
|
+
* name: 'my-server',
|
|
31
|
+
* version: '1.0.0',
|
|
32
|
+
* commands: [greet],
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* // Or explicitly set transport for IDE integration
|
|
36
|
+
* const stdioServer = createMcpServer({
|
|
37
|
+
* name: 'my-server',
|
|
38
|
+
* version: '1.0.0',
|
|
39
|
+
* commands: [greet],
|
|
40
|
+
* transport: 'stdio',
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* await server.start();
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @packageDocumentation
|
|
47
|
+
*/
|
|
48
|
+
// Re-export core helpers for convenience
|
|
49
|
+
export { success, failure, isSuccess, isFailure } from '@lushly-dev/afd-core';
|
|
50
|
+
// Schema and command definition
|
|
51
|
+
export { defineCommand, zodToJsonSchema, getRequiredFields, isObjectSchema, } from './schema.js';
|
|
52
|
+
// Server factory
|
|
53
|
+
export { createMcpServer, isStdinPiped, } from './server.js';
|
|
54
|
+
// Validation utilities
|
|
55
|
+
export { validateInput, validateOrThrow, isValid, formatValidationErrors, ValidationException, patterns, optional, withDefault, } from './validation.js';
|
|
56
|
+
// Middleware
|
|
57
|
+
export { createLoggingMiddleware, createTimingMiddleware, createRetryMiddleware, createTracingMiddleware, createRateLimitMiddleware, composeMiddleware, } from './middleware.js';
|
|
58
|
+
// Bootstrap tools
|
|
59
|
+
export { getBootstrapCommands, createAfdHelpCommand, createAfdDocsCommand, createAfdSchemaCommand, } from './bootstrap/index.js';
|
|
60
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AAEH,yCAAyC;AACzC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAE9E,gCAAgC;AAChC,OAAO,EACN,aAAa,EACb,eAAe,EACf,iBAAiB,EACjB,cAAc,GAGd,MAAM,aAAa,CAAC;AAErB,iBAAiB;AACjB,OAAO,EACN,eAAe,EACf,YAAY,GAKZ,MAAM,aAAa,CAAC;AAErB,uBAAuB;AACvB,OAAO,EACN,aAAa,EACb,eAAe,EACf,OAAO,EACP,sBAAsB,EACtB,mBAAmB,EACnB,QAAQ,EACR,QAAQ,EACR,WAAW,GAGX,MAAM,iBAAiB,CAAC;AAEzB,aAAa;AACb,OAAO,EACN,uBAAuB,EACvB,sBAAsB,EACtB,qBAAqB,EACrB,uBAAuB,EACvB,yBAAyB,EACzB,iBAAiB,GAQjB,MAAM,iBAAiB,CAAC;AAEzB,kBAAkB;AAClB,OAAO,EACN,oBAAoB,EACpB,oBAAoB,EACpB,oBAAoB,EACpB,sBAAsB,GACtB,MAAM,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Command middleware utilities
|
|
3
|
+
*
|
|
4
|
+
* Middleware functions that can be applied to command execution
|
|
5
|
+
* for logging, tracing, rate limiting, etc.
|
|
6
|
+
*/
|
|
7
|
+
import type { CommandContext } from '@lushly-dev/afd-core';
|
|
8
|
+
import type { CommandMiddleware } from './server.js';
|
|
9
|
+
/**
|
|
10
|
+
* Options for logging middleware.
|
|
11
|
+
*/
|
|
12
|
+
export interface LoggingOptions {
|
|
13
|
+
/** Log function (defaults to console.log) */
|
|
14
|
+
log?: (message: string, data?: unknown) => void;
|
|
15
|
+
/** Include input in logs (may contain sensitive data) */
|
|
16
|
+
logInput?: boolean;
|
|
17
|
+
/** Include full result in logs */
|
|
18
|
+
logResult?: boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Create a logging middleware.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const server = createMcpServer({
|
|
26
|
+
* // ...
|
|
27
|
+
* middleware: [
|
|
28
|
+
* createLoggingMiddleware({ logInput: true }),
|
|
29
|
+
* ],
|
|
30
|
+
* });
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export declare function createLoggingMiddleware(options?: LoggingOptions): CommandMiddleware;
|
|
34
|
+
/**
|
|
35
|
+
* Options for timing middleware.
|
|
36
|
+
*/
|
|
37
|
+
export interface TimingOptions {
|
|
38
|
+
/** Slow threshold in ms (logs warning if exceeded) */
|
|
39
|
+
slowThreshold?: number;
|
|
40
|
+
/** Warning function */
|
|
41
|
+
onSlow?: (commandName: string, durationMs: number) => void;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Create a timing middleware that tracks execution duration.
|
|
45
|
+
*/
|
|
46
|
+
export declare function createTimingMiddleware(options?: TimingOptions): CommandMiddleware;
|
|
47
|
+
/**
|
|
48
|
+
* Options for retry middleware.
|
|
49
|
+
*/
|
|
50
|
+
export interface RetryOptions {
|
|
51
|
+
/** Maximum number of retries */
|
|
52
|
+
maxRetries?: number;
|
|
53
|
+
/** Delay between retries in ms */
|
|
54
|
+
retryDelay?: number;
|
|
55
|
+
/** Whether to retry based on error code */
|
|
56
|
+
shouldRetry?: (errorCode: string) => boolean;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Create a retry middleware for transient failures.
|
|
60
|
+
*/
|
|
61
|
+
export declare function createRetryMiddleware(options?: RetryOptions): CommandMiddleware;
|
|
62
|
+
/**
|
|
63
|
+
* Span interface compatible with OpenTelemetry.
|
|
64
|
+
*/
|
|
65
|
+
export interface Span {
|
|
66
|
+
setAttribute(key: string, value: string | number | boolean): void;
|
|
67
|
+
setStatus(status: {
|
|
68
|
+
code: number;
|
|
69
|
+
message?: string;
|
|
70
|
+
}): void;
|
|
71
|
+
end(): void;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Tracer interface compatible with OpenTelemetry.
|
|
75
|
+
*/
|
|
76
|
+
export interface Tracer {
|
|
77
|
+
startActiveSpan<T>(name: string, fn: (span: Span) => Promise<T>): Promise<T>;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Options for tracing middleware.
|
|
81
|
+
*/
|
|
82
|
+
export interface TracingOptions {
|
|
83
|
+
/** OpenTelemetry tracer instance */
|
|
84
|
+
tracer: Tracer;
|
|
85
|
+
/** Span name prefix */
|
|
86
|
+
spanPrefix?: string;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Create a tracing middleware for OpenTelemetry integration.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* import { trace } from '@opentelemetry/api';
|
|
94
|
+
*
|
|
95
|
+
* const tracer = trace.getTracer('my-app');
|
|
96
|
+
*
|
|
97
|
+
* const server = createMcpServer({
|
|
98
|
+
* // ...
|
|
99
|
+
* middleware: [
|
|
100
|
+
* createTracingMiddleware({ tracer }),
|
|
101
|
+
* ],
|
|
102
|
+
* });
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
export declare function createTracingMiddleware(options: TracingOptions): CommandMiddleware;
|
|
106
|
+
/**
|
|
107
|
+
* Options for rate limiting middleware.
|
|
108
|
+
*/
|
|
109
|
+
export interface RateLimitOptions {
|
|
110
|
+
/** Maximum requests per window */
|
|
111
|
+
maxRequests: number;
|
|
112
|
+
/** Window size in ms */
|
|
113
|
+
windowMs: number;
|
|
114
|
+
/** Key function to identify clients (defaults to 'global') */
|
|
115
|
+
keyFn?: (context: CommandContext) => string;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Create a simple in-memory rate limiting middleware.
|
|
119
|
+
*/
|
|
120
|
+
export declare function createRateLimitMiddleware(options: RateLimitOptions): CommandMiddleware;
|
|
121
|
+
/**
|
|
122
|
+
* Compose multiple middleware functions into one.
|
|
123
|
+
*/
|
|
124
|
+
export declare function composeMiddleware(...middlewares: CommandMiddleware[]): CommandMiddleware;
|
|
125
|
+
//# sourceMappingURL=middleware.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAiB,MAAM,sBAAsB,CAAC;AAC1E,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAMrD;;GAEG;AACH,MAAM,WAAW,cAAc;IAC9B,6CAA6C;IAC7C,GAAG,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;IAEhD,yDAAyD;IACzD,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB,kCAAkC;IAClC,SAAS,CAAC,EAAE,OAAO,CAAC;CACpB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,uBAAuB,CACtC,OAAO,GAAE,cAAmB,GAC1B,iBAAiB,CAyBnB;AAMD;;GAEG;AACH,MAAM,WAAW,aAAa;IAC7B,sDAAsD;IACtD,aAAa,CAAC,EAAE,MAAM,CAAC;IAEvB,uBAAuB;IACvB,MAAM,CAAC,EAAE,CAAC,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,KAAK,IAAI,CAAC;CAC3D;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CACrC,OAAO,GAAE,aAAkB,GACzB,iBAAiB,CAiBnB;AAMD;;GAEG;AACH,MAAM,WAAW,YAAY;IAC5B,gCAAgC;IAChC,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB,kCAAkC;IA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2BnB;AAUD;;GAEG;AACH,wBAAgB,iBAAiB,CAChC,GAAG,WAAW,EAAE,iBAAiB,EAAE,GACjC,iBAAiB,CAenB"}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Command middleware utilities
|
|
3
|
+
*
|
|
4
|
+
* Middleware functions that can be applied to command execution
|
|
5
|
+
* for logging, tracing, rate limiting, etc.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Create a logging middleware.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const server = createMcpServer({
|
|
13
|
+
* // ...
|
|
14
|
+
* middleware: [
|
|
15
|
+
* createLoggingMiddleware({ logInput: true }),
|
|
16
|
+
* ],
|
|
17
|
+
* });
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export function createLoggingMiddleware(options = {}) {
|
|
21
|
+
const { log = console.log, logInput = false, logResult = false } = options;
|
|
22
|
+
return async (commandName, input, context, next) => {
|
|
23
|
+
const startTime = Date.now();
|
|
24
|
+
const traceId = context.traceId ?? 'no-trace';
|
|
25
|
+
log(`[${traceId}] Executing: ${commandName}`, logInput ? { input } : undefined);
|
|
26
|
+
try {
|
|
27
|
+
const result = await next();
|
|
28
|
+
const duration = Date.now() - startTime;
|
|
29
|
+
log(`[${traceId}] Completed: ${commandName} (${duration}ms) - ${result.success ? 'SUCCESS' : 'FAILURE'}`, logResult ? { result } : undefined);
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
const duration = Date.now() - startTime;
|
|
34
|
+
log(`[${traceId}] Error: ${commandName} (${duration}ms)`, { error });
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Create a timing middleware that tracks execution duration.
|
|
41
|
+
*/
|
|
42
|
+
export function createTimingMiddleware(options = {}) {
|
|
43
|
+
const { slowThreshold = 1000, onSlow = (name, ms) => console.warn(`Slow command: ${name} took ${ms}ms`), } = options;
|
|
44
|
+
return async (commandName, input, context, next) => {
|
|
45
|
+
const startTime = Date.now();
|
|
46
|
+
const result = await next();
|
|
47
|
+
const duration = Date.now() - startTime;
|
|
48
|
+
if (duration > slowThreshold) {
|
|
49
|
+
onSlow(commandName, duration);
|
|
50
|
+
}
|
|
51
|
+
return result;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Create a retry middleware for transient failures.
|
|
56
|
+
*/
|
|
57
|
+
export function createRetryMiddleware(options = {}) {
|
|
58
|
+
const { maxRetries = 3, retryDelay = 100, shouldRetry = (code) => code === 'TRANSIENT_ERROR' || code === 'TIMEOUT', } = options;
|
|
59
|
+
return async (commandName, input, context, next) => {
|
|
60
|
+
let lastResult;
|
|
61
|
+
let attempts = 0;
|
|
62
|
+
while (attempts <= maxRetries) {
|
|
63
|
+
const result = await next();
|
|
64
|
+
if (result.success) {
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
67
|
+
lastResult = result;
|
|
68
|
+
// Check if we should retry
|
|
69
|
+
if (result.error && shouldRetry(result.error.code)) {
|
|
70
|
+
attempts++;
|
|
71
|
+
if (attempts <= maxRetries) {
|
|
72
|
+
await sleep(retryDelay * attempts); // Exponential backoff
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
// Not retryable or max retries reached
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
return lastResult;
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Create a tracing middleware for OpenTelemetry integration.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* import { trace } from '@opentelemetry/api';
|
|
88
|
+
*
|
|
89
|
+
* const tracer = trace.getTracer('my-app');
|
|
90
|
+
*
|
|
91
|
+
* const server = createMcpServer({
|
|
92
|
+
* // ...
|
|
93
|
+
* middleware: [
|
|
94
|
+
* createTracingMiddleware({ tracer }),
|
|
95
|
+
* ],
|
|
96
|
+
* });
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
export function createTracingMiddleware(options) {
|
|
100
|
+
const { tracer, spanPrefix = 'command' } = options;
|
|
101
|
+
return async (commandName, input, context, next) => {
|
|
102
|
+
return tracer.startActiveSpan(`${spanPrefix}.${commandName}`, async (span) => {
|
|
103
|
+
span.setAttribute('command.name', commandName);
|
|
104
|
+
span.setAttribute('command.trace_id', context.traceId ?? 'none');
|
|
105
|
+
try {
|
|
106
|
+
const result = await next();
|
|
107
|
+
span.setAttribute('command.success', result.success);
|
|
108
|
+
if (!result.success && result.error) {
|
|
109
|
+
span.setAttribute('error.code', result.error.code);
|
|
110
|
+
span.setStatus({ code: 2, message: result.error.message });
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
span.setStatus({ code: 1 });
|
|
114
|
+
}
|
|
115
|
+
if (result.confidence !== undefined) {
|
|
116
|
+
span.setAttribute('command.confidence', result.confidence);
|
|
117
|
+
}
|
|
118
|
+
span.end();
|
|
119
|
+
return result;
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
span.setAttribute('error', true);
|
|
123
|
+
span.setStatus({
|
|
124
|
+
code: 2,
|
|
125
|
+
message: error instanceof Error ? error.message : 'Unknown error',
|
|
126
|
+
});
|
|
127
|
+
span.end();
|
|
128
|
+
throw error;
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Create a simple in-memory rate limiting middleware.
|
|
135
|
+
*/
|
|
136
|
+
export function createRateLimitMiddleware(options) {
|
|
137
|
+
const { maxRequests, windowMs, keyFn = () => 'global' } = options;
|
|
138
|
+
const windows = new Map();
|
|
139
|
+
return async (_commandName, _input, context, next) => {
|
|
140
|
+
const key = keyFn(context);
|
|
141
|
+
const now = Date.now();
|
|
142
|
+
let window = windows.get(key);
|
|
143
|
+
if (!window || now >= window.resetAt) {
|
|
144
|
+
window = { count: 0, resetAt: now + windowMs };
|
|
145
|
+
windows.set(key, window);
|
|
146
|
+
}
|
|
147
|
+
if (window.count >= maxRequests) {
|
|
148
|
+
const { failure } = await import('@lushly-dev/afd-core');
|
|
149
|
+
return failure({
|
|
150
|
+
code: 'RATE_LIMITED',
|
|
151
|
+
message: 'Too many requests',
|
|
152
|
+
suggestion: `Try again in ${Math.ceil((window.resetAt - now) / 1000)} seconds`,
|
|
153
|
+
retryable: true,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
window.count++;
|
|
157
|
+
return next();
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
// ═══════════════════════════════════════════════════════════════════════════════
|
|
161
|
+
// HELPERS
|
|
162
|
+
// ═══════════════════════════════════════════════════════════════════════════════
|
|
163
|
+
function sleep(ms) {
|
|
164
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Compose multiple middleware functions into one.
|
|
168
|
+
*/
|
|
169
|
+
export function composeMiddleware(...middlewares) {
|
|
170
|
+
return async (commandName, input, context, next) => {
|
|
171
|
+
let index = 0;
|
|
172
|
+
const dispatch = async () => {
|
|
173
|
+
if (index >= middlewares.length) {
|
|
174
|
+
return next();
|
|
175
|
+
}
|
|
176
|
+
const middleware = middlewares[index++];
|
|
177
|
+
return middleware(commandName, input, context, dispatch);
|
|
178
|
+
};
|
|
179
|
+
return dispatch();
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
//# sourceMappingURL=middleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.js","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAuBH;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,uBAAuB,CACtC,UAA0B,EAAE;IAE5B,MAAM,EAAE,GAAG,GA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