@fractary/codex 0.1.2 → 0.2.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/README.md +238 -385
- package/dist/index.cjs +3 -392
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -119
- package/dist/index.d.ts +1 -119
- package/dist/index.js +4 -385
- package/dist/index.js.map +1 -1
- package/package.json +4 -2
package/dist/index.js
CHANGED
|
@@ -1080,7 +1080,7 @@ function getCustomSyncDestinations(metadata) {
|
|
|
1080
1080
|
try {
|
|
1081
1081
|
const parsed = parseCustomDestination(destination);
|
|
1082
1082
|
results.push(parsed);
|
|
1083
|
-
} catch (
|
|
1083
|
+
} catch (_error) {
|
|
1084
1084
|
continue;
|
|
1085
1085
|
}
|
|
1086
1086
|
}
|
|
@@ -2410,387 +2410,6 @@ function setDefaultCacheManager(manager) {
|
|
|
2410
2410
|
defaultCacheManager = manager;
|
|
2411
2411
|
}
|
|
2412
2412
|
|
|
2413
|
-
// src/mcp/tools.ts
|
|
2414
|
-
var CODEX_TOOLS = [
|
|
2415
|
-
{
|
|
2416
|
-
name: "codex_fetch",
|
|
2417
|
-
description: "Fetch a document from the Codex knowledge base by URI. Returns the document content.",
|
|
2418
|
-
inputSchema: {
|
|
2419
|
-
type: "object",
|
|
2420
|
-
properties: {
|
|
2421
|
-
uri: {
|
|
2422
|
-
type: "string",
|
|
2423
|
-
description: "Codex URI in format: codex://org/project/path/to/file.md"
|
|
2424
|
-
},
|
|
2425
|
-
branch: {
|
|
2426
|
-
type: "string",
|
|
2427
|
-
description: "Git branch to fetch from (default: main)"
|
|
2428
|
-
},
|
|
2429
|
-
noCache: {
|
|
2430
|
-
type: "boolean",
|
|
2431
|
-
description: "Bypass cache and fetch fresh content"
|
|
2432
|
-
}
|
|
2433
|
-
},
|
|
2434
|
-
required: ["uri"]
|
|
2435
|
-
}
|
|
2436
|
-
},
|
|
2437
|
-
{
|
|
2438
|
-
name: "codex_search",
|
|
2439
|
-
description: "Search for documents in the Codex knowledge base.",
|
|
2440
|
-
inputSchema: {
|
|
2441
|
-
type: "object",
|
|
2442
|
-
properties: {
|
|
2443
|
-
query: {
|
|
2444
|
-
type: "string",
|
|
2445
|
-
description: "Search query string"
|
|
2446
|
-
},
|
|
2447
|
-
org: {
|
|
2448
|
-
type: "string",
|
|
2449
|
-
description: "Filter by organization"
|
|
2450
|
-
},
|
|
2451
|
-
project: {
|
|
2452
|
-
type: "string",
|
|
2453
|
-
description: "Filter by project"
|
|
2454
|
-
},
|
|
2455
|
-
limit: {
|
|
2456
|
-
type: "number",
|
|
2457
|
-
description: "Maximum number of results (default: 10)"
|
|
2458
|
-
},
|
|
2459
|
-
type: {
|
|
2460
|
-
type: "string",
|
|
2461
|
-
description: "Filter by artifact type (e.g., docs, specs, logs)"
|
|
2462
|
-
}
|
|
2463
|
-
},
|
|
2464
|
-
required: ["query"]
|
|
2465
|
-
}
|
|
2466
|
-
},
|
|
2467
|
-
{
|
|
2468
|
-
name: "codex_list",
|
|
2469
|
-
description: "List documents in the Codex cache.",
|
|
2470
|
-
inputSchema: {
|
|
2471
|
-
type: "object",
|
|
2472
|
-
properties: {
|
|
2473
|
-
org: {
|
|
2474
|
-
type: "string",
|
|
2475
|
-
description: "Filter by organization"
|
|
2476
|
-
},
|
|
2477
|
-
project: {
|
|
2478
|
-
type: "string",
|
|
2479
|
-
description: "Filter by project"
|
|
2480
|
-
},
|
|
2481
|
-
includeExpired: {
|
|
2482
|
-
type: "boolean",
|
|
2483
|
-
description: "Include expired cache entries"
|
|
2484
|
-
}
|
|
2485
|
-
}
|
|
2486
|
-
}
|
|
2487
|
-
},
|
|
2488
|
-
{
|
|
2489
|
-
name: "codex_invalidate",
|
|
2490
|
-
description: "Invalidate cached documents matching a pattern.",
|
|
2491
|
-
inputSchema: {
|
|
2492
|
-
type: "object",
|
|
2493
|
-
properties: {
|
|
2494
|
-
pattern: {
|
|
2495
|
-
type: "string",
|
|
2496
|
-
description: "URI pattern to invalidate (supports regex)"
|
|
2497
|
-
}
|
|
2498
|
-
},
|
|
2499
|
-
required: ["pattern"]
|
|
2500
|
-
}
|
|
2501
|
-
}
|
|
2502
|
-
];
|
|
2503
|
-
function textResult(text, isError = false) {
|
|
2504
|
-
return {
|
|
2505
|
-
content: [{ type: "text", text }],
|
|
2506
|
-
isError
|
|
2507
|
-
};
|
|
2508
|
-
}
|
|
2509
|
-
function resourceResult(uri, content, mimeType) {
|
|
2510
|
-
return {
|
|
2511
|
-
content: [
|
|
2512
|
-
{
|
|
2513
|
-
type: "resource",
|
|
2514
|
-
resource: {
|
|
2515
|
-
uri,
|
|
2516
|
-
mimeType,
|
|
2517
|
-
text: content
|
|
2518
|
-
}
|
|
2519
|
-
}
|
|
2520
|
-
]
|
|
2521
|
-
};
|
|
2522
|
-
}
|
|
2523
|
-
async function handleFetch(args, ctx) {
|
|
2524
|
-
const { uri, branch, noCache } = args;
|
|
2525
|
-
const ref = resolveReference(uri);
|
|
2526
|
-
if (!ref) {
|
|
2527
|
-
return textResult(`Invalid codex URI: ${uri}`, true);
|
|
2528
|
-
}
|
|
2529
|
-
try {
|
|
2530
|
-
let result;
|
|
2531
|
-
if (noCache) {
|
|
2532
|
-
result = await ctx.storage.fetch(ref, { branch });
|
|
2533
|
-
await ctx.cache.set(uri, result);
|
|
2534
|
-
} else {
|
|
2535
|
-
result = await ctx.cache.get(ref, { branch });
|
|
2536
|
-
}
|
|
2537
|
-
const content = result.content.toString("utf-8");
|
|
2538
|
-
return resourceResult(uri, content, result.contentType);
|
|
2539
|
-
} catch (error) {
|
|
2540
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
2541
|
-
return textResult(`Failed to fetch ${uri}: ${message}`, true);
|
|
2542
|
-
}
|
|
2543
|
-
}
|
|
2544
|
-
async function handleSearch(args, ctx) {
|
|
2545
|
-
const { query, org, project, limit = 10 } = args;
|
|
2546
|
-
const stats = await ctx.cache.getStats();
|
|
2547
|
-
if (stats.entryCount === 0) {
|
|
2548
|
-
return textResult("No documents in cache. Use codex_fetch to load documents first.");
|
|
2549
|
-
}
|
|
2550
|
-
const message = `Search functionality requires a search index.
|
|
2551
|
-
Query: "${query}"
|
|
2552
|
-
Filters: org=${org || "any"}, project=${project || "any"}
|
|
2553
|
-
Limit: ${limit}
|
|
2554
|
-
|
|
2555
|
-
To fetch documents, use codex_fetch with a specific URI like:
|
|
2556
|
-
codex://org/project/docs/file.md`;
|
|
2557
|
-
return textResult(message);
|
|
2558
|
-
}
|
|
2559
|
-
async function handleList(args, ctx) {
|
|
2560
|
-
const { org, project, includeExpired } = args;
|
|
2561
|
-
const stats = await ctx.cache.getStats();
|
|
2562
|
-
let message = `Cache Statistics:
|
|
2563
|
-
- Total entries: ${stats.entryCount}
|
|
2564
|
-
- Memory entries: ${stats.memoryEntries}
|
|
2565
|
-
- Memory size: ${formatBytes(stats.memorySize)}
|
|
2566
|
-
- Total size: ${formatBytes(stats.totalSize)}
|
|
2567
|
-
- Fresh: ${stats.freshCount}
|
|
2568
|
-
- Stale: ${stats.staleCount}
|
|
2569
|
-
- Expired: ${stats.expiredCount}`;
|
|
2570
|
-
if (org) {
|
|
2571
|
-
message += `
|
|
2572
|
-
|
|
2573
|
-
Filtered by org: ${org}`;
|
|
2574
|
-
}
|
|
2575
|
-
if (project) {
|
|
2576
|
-
message += `
|
|
2577
|
-
Filtered by project: ${project}`;
|
|
2578
|
-
}
|
|
2579
|
-
if (includeExpired) {
|
|
2580
|
-
message += `
|
|
2581
|
-
Including expired entries`;
|
|
2582
|
-
}
|
|
2583
|
-
return textResult(message);
|
|
2584
|
-
}
|
|
2585
|
-
async function handleInvalidate(args, ctx) {
|
|
2586
|
-
const { pattern } = args;
|
|
2587
|
-
try {
|
|
2588
|
-
const regex = new RegExp(pattern);
|
|
2589
|
-
const count = await ctx.cache.invalidatePattern(regex);
|
|
2590
|
-
return textResult(`Invalidated ${count} cache entries matching pattern: ${pattern}`);
|
|
2591
|
-
} catch (error) {
|
|
2592
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
2593
|
-
return textResult(`Invalid pattern: ${message}`, true);
|
|
2594
|
-
}
|
|
2595
|
-
}
|
|
2596
|
-
async function handleToolCall(name, args, ctx) {
|
|
2597
|
-
switch (name) {
|
|
2598
|
-
case "codex_fetch":
|
|
2599
|
-
return handleFetch(args, ctx);
|
|
2600
|
-
case "codex_search":
|
|
2601
|
-
return handleSearch(args, ctx);
|
|
2602
|
-
case "codex_list":
|
|
2603
|
-
return handleList(args, ctx);
|
|
2604
|
-
case "codex_invalidate":
|
|
2605
|
-
return handleInvalidate(args, ctx);
|
|
2606
|
-
default:
|
|
2607
|
-
return textResult(`Unknown tool: ${name}`, true);
|
|
2608
|
-
}
|
|
2609
|
-
}
|
|
2610
|
-
function formatBytes(bytes) {
|
|
2611
|
-
if (bytes === 0) return "0 B";
|
|
2612
|
-
const k = 1024;
|
|
2613
|
-
const sizes = ["B", "KB", "MB", "GB"];
|
|
2614
|
-
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
2615
|
-
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`;
|
|
2616
|
-
}
|
|
2617
|
-
|
|
2618
|
-
// src/mcp/server.ts
|
|
2619
|
-
var McpServer = class {
|
|
2620
|
-
config;
|
|
2621
|
-
toolContext;
|
|
2622
|
-
constructor(config) {
|
|
2623
|
-
this.config = {
|
|
2624
|
-
name: config.name ?? "codex",
|
|
2625
|
-
version: config.version ?? "1.0.0",
|
|
2626
|
-
cache: config.cache,
|
|
2627
|
-
storage: config.storage
|
|
2628
|
-
};
|
|
2629
|
-
this.toolContext = {
|
|
2630
|
-
cache: config.cache,
|
|
2631
|
-
storage: config.storage
|
|
2632
|
-
};
|
|
2633
|
-
}
|
|
2634
|
-
/**
|
|
2635
|
-
* Get server info
|
|
2636
|
-
*/
|
|
2637
|
-
getServerInfo() {
|
|
2638
|
-
return {
|
|
2639
|
-
name: this.config.name,
|
|
2640
|
-
version: this.config.version,
|
|
2641
|
-
capabilities: this.getCapabilities()
|
|
2642
|
-
};
|
|
2643
|
-
}
|
|
2644
|
-
/**
|
|
2645
|
-
* Get server capabilities
|
|
2646
|
-
*/
|
|
2647
|
-
getCapabilities() {
|
|
2648
|
-
return {
|
|
2649
|
-
tools: {
|
|
2650
|
-
listChanged: false
|
|
2651
|
-
},
|
|
2652
|
-
resources: {
|
|
2653
|
-
subscribe: false,
|
|
2654
|
-
listChanged: false
|
|
2655
|
-
}
|
|
2656
|
-
};
|
|
2657
|
-
}
|
|
2658
|
-
/**
|
|
2659
|
-
* List available tools
|
|
2660
|
-
*/
|
|
2661
|
-
listTools() {
|
|
2662
|
-
return CODEX_TOOLS;
|
|
2663
|
-
}
|
|
2664
|
-
/**
|
|
2665
|
-
* Call a tool
|
|
2666
|
-
*/
|
|
2667
|
-
async callTool(name, args) {
|
|
2668
|
-
return handleToolCall(name, args, this.toolContext);
|
|
2669
|
-
}
|
|
2670
|
-
/**
|
|
2671
|
-
* List available resources
|
|
2672
|
-
*/
|
|
2673
|
-
async listResources() {
|
|
2674
|
-
const resources = [];
|
|
2675
|
-
const stats = await this.config.cache.getStats();
|
|
2676
|
-
resources.push({
|
|
2677
|
-
uri: "codex://cache/summary",
|
|
2678
|
-
name: "Cache Summary",
|
|
2679
|
-
description: `${stats.entryCount} cached documents`,
|
|
2680
|
-
mimeType: "text/plain"
|
|
2681
|
-
});
|
|
2682
|
-
return resources;
|
|
2683
|
-
}
|
|
2684
|
-
/**
|
|
2685
|
-
* List resource templates
|
|
2686
|
-
*/
|
|
2687
|
-
listResourceTemplates() {
|
|
2688
|
-
return [
|
|
2689
|
-
{
|
|
2690
|
-
uriTemplate: "codex://{org}/{project}/{path}",
|
|
2691
|
-
name: "Codex Document",
|
|
2692
|
-
description: "Fetch a document from the Codex knowledge base",
|
|
2693
|
-
mimeType: "text/markdown"
|
|
2694
|
-
}
|
|
2695
|
-
];
|
|
2696
|
-
}
|
|
2697
|
-
/**
|
|
2698
|
-
* Read a resource
|
|
2699
|
-
*/
|
|
2700
|
-
async readResource(uri) {
|
|
2701
|
-
if (uri === "codex://cache/summary") {
|
|
2702
|
-
const stats = await this.config.cache.getStats();
|
|
2703
|
-
return [
|
|
2704
|
-
{
|
|
2705
|
-
uri,
|
|
2706
|
-
mimeType: "text/plain",
|
|
2707
|
-
text: `Cache Statistics:
|
|
2708
|
-
- Total entries: ${stats.entryCount}
|
|
2709
|
-
- Memory entries: ${stats.memoryEntries}
|
|
2710
|
-
- Fresh: ${stats.freshCount}
|
|
2711
|
-
- Stale: ${stats.staleCount}
|
|
2712
|
-
- Expired: ${stats.expiredCount}`
|
|
2713
|
-
}
|
|
2714
|
-
];
|
|
2715
|
-
}
|
|
2716
|
-
const ref = resolveReference(uri);
|
|
2717
|
-
if (!ref) {
|
|
2718
|
-
throw new Error(`Invalid codex URI: ${uri}`);
|
|
2719
|
-
}
|
|
2720
|
-
const result = await this.config.cache.get(ref);
|
|
2721
|
-
return [
|
|
2722
|
-
{
|
|
2723
|
-
uri,
|
|
2724
|
-
mimeType: result.contentType,
|
|
2725
|
-
text: result.content.toString("utf-8")
|
|
2726
|
-
}
|
|
2727
|
-
];
|
|
2728
|
-
}
|
|
2729
|
-
/**
|
|
2730
|
-
* Handle JSON-RPC request
|
|
2731
|
-
*
|
|
2732
|
-
* This method handles the low-level MCP protocol messages.
|
|
2733
|
-
*/
|
|
2734
|
-
async handleRequest(method, params) {
|
|
2735
|
-
switch (method) {
|
|
2736
|
-
case "initialize":
|
|
2737
|
-
return {
|
|
2738
|
-
protocolVersion: "2024-11-05",
|
|
2739
|
-
serverInfo: this.getServerInfo(),
|
|
2740
|
-
capabilities: this.getCapabilities()
|
|
2741
|
-
};
|
|
2742
|
-
case "tools/list":
|
|
2743
|
-
return { tools: this.listTools() };
|
|
2744
|
-
case "tools/call": {
|
|
2745
|
-
const { name, arguments: args } = params;
|
|
2746
|
-
return await this.callTool(name, args);
|
|
2747
|
-
}
|
|
2748
|
-
case "resources/list":
|
|
2749
|
-
return { resources: await this.listResources() };
|
|
2750
|
-
case "resources/templates/list":
|
|
2751
|
-
return { resourceTemplates: this.listResourceTemplates() };
|
|
2752
|
-
case "resources/read": {
|
|
2753
|
-
const { uri } = params;
|
|
2754
|
-
return { contents: await this.readResource(uri) };
|
|
2755
|
-
}
|
|
2756
|
-
case "prompts/list":
|
|
2757
|
-
return { prompts: [] };
|
|
2758
|
-
default:
|
|
2759
|
-
throw new Error(`Unknown method: ${method}`);
|
|
2760
|
-
}
|
|
2761
|
-
}
|
|
2762
|
-
/**
|
|
2763
|
-
* Process a JSON-RPC message
|
|
2764
|
-
*/
|
|
2765
|
-
async processMessage(message) {
|
|
2766
|
-
let id = null;
|
|
2767
|
-
try {
|
|
2768
|
-
const request = JSON.parse(message);
|
|
2769
|
-
id = request.id;
|
|
2770
|
-
const { method, params } = request;
|
|
2771
|
-
const result = await this.handleRequest(method, params);
|
|
2772
|
-
return JSON.stringify({
|
|
2773
|
-
jsonrpc: "2.0",
|
|
2774
|
-
id,
|
|
2775
|
-
result
|
|
2776
|
-
});
|
|
2777
|
-
} catch (error) {
|
|
2778
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
2779
|
-
return JSON.stringify({
|
|
2780
|
-
jsonrpc: "2.0",
|
|
2781
|
-
id,
|
|
2782
|
-
error: {
|
|
2783
|
-
code: -32603,
|
|
2784
|
-
message: errorMessage
|
|
2785
|
-
}
|
|
2786
|
-
});
|
|
2787
|
-
}
|
|
2788
|
-
}
|
|
2789
|
-
};
|
|
2790
|
-
function createMcpServer(config) {
|
|
2791
|
-
return new McpServer(config);
|
|
2792
|
-
}
|
|
2793
|
-
|
|
2794
2413
|
// src/sync/types.ts
|
|
2795
2414
|
var DEFAULT_SYNC_CONFIG = {
|
|
2796
2415
|
defaultDirection: "to-codex",
|
|
@@ -3075,10 +2694,10 @@ function formatPlanSummary(plan) {
|
|
|
3075
2694
|
lines.push(`\u26A0\uFE0F Conflicts: ${stats.conflicts} files`);
|
|
3076
2695
|
lines.push("");
|
|
3077
2696
|
}
|
|
3078
|
-
lines.push(`Total: ${plan.totalFiles} files (${
|
|
2697
|
+
lines.push(`Total: ${plan.totalFiles} files (${formatBytes(stats.totalBytes)})`);
|
|
3079
2698
|
return lines.join("\n");
|
|
3080
2699
|
}
|
|
3081
|
-
function
|
|
2700
|
+
function formatBytes(bytes) {
|
|
3082
2701
|
if (bytes === 0) return "0 B";
|
|
3083
2702
|
const k = 1024;
|
|
3084
2703
|
const sizes = ["B", "KB", "MB", "GB"];
|
|
@@ -4202,6 +3821,6 @@ function generateReferenceMigrationSummary(results) {
|
|
|
4202
3821
|
return lines.join("\n");
|
|
4203
3822
|
}
|
|
4204
3823
|
|
|
4205
|
-
export { AutoSyncPatternSchema, BUILT_IN_TYPES,
|
|
3824
|
+
export { AutoSyncPatternSchema, BUILT_IN_TYPES, CODEX_URI_PREFIX, CacheManager, CachePersistence, CodexConfigSchema, CodexError, CommonRules, ConfigurationError, CustomTypeSchema, DEFAULT_CACHE_DIR, DEFAULT_FETCH_OPTIONS, DEFAULT_MIGRATION_OPTIONS, DEFAULT_PERMISSION_CONFIG, DEFAULT_SYNC_CONFIG, DEFAULT_TYPE, GitHubStorage, HttpStorage, LEGACY_PATTERNS, LEGACY_REF_PREFIX, LocalStorage, MetadataSchema, PERMISSION_LEVEL_ORDER, PermissionDeniedError, PermissionManager, StorageManager, SyncManager, SyncRulesSchema, TTL, TypeRegistry, TypesConfigSchema, ValidationError, buildUri, calculateCachePath, calculateContentHash, convertLegacyReference, convertLegacyReferences, convertToUri, createCacheEntry, createCacheManager, createCachePersistence, createDefaultRegistry, createEmptyModernConfig, createEmptySyncPlan, createGitHubStorage, createHttpStorage, createLocalStorage, createPermissionManager, createRule, createRulesFromPatterns, createStorageManager, createSyncManager, createSyncPlan, deserializeCacheEntry, detectContentType, detectCurrentProject, detectVersion, estimateSyncTime, evaluatePath, evaluatePaths, evaluatePatterns, evaluatePermission, evaluatePermissions, extendType, extractOrgFromRepoName, extractRawFrontmatter, filterByPatterns, filterByPermission, filterPlanOperations, filterSyncablePaths, findLegacyReferences, formatPlanSummary, generateMigrationReport, generateReferenceMigrationSummary, getBuiltInType, getBuiltInTypeNames, getCacheEntryAge, getCacheEntryStatus, getCurrentContext, getCustomSyncDestinations, getDefaultCacheManager, getDefaultConfig, getDefaultDirectories, getDefaultPermissionManager, getDefaultRules, getDefaultStorageManager, getDirectory, getExtension, getFilename, getMigrationRequirements, getPlanStats, getRelativeCachePath, getRemainingTtl, getTargetRepos, hasContentChanged, hasFrontmatter, hasLegacyReferences, hasPermission as hasPermissionLevel, isBuiltInType, isCacheEntryFresh, isCacheEntryValid, isCurrentProjectUri, isLegacyConfig, isLegacyReference, isModernConfig, isAllowed as isPermissionAllowed, isValidUri, levelGrants, loadConfig, loadCustomTypes, matchAnyPattern, matchPattern, maxLevel, mergeFetchOptions, mergeRules, mergeTypes, migrateConfig, migrateFileReferences, minLevel, needsMigration, parseCustomDestination, parseMetadata, parseReference, parseTtl, resolveOrganization, resolveReference, resolveReferences, ruleMatchesAction, ruleMatchesContext, ruleMatchesPath, sanitizePath, serializeCacheEntry, setDefaultCacheManager, setDefaultPermissionManager, setDefaultStorageManager, shouldSyncToRepo, summarizeEvaluations, touchCacheEntry, validateCustomTypes, validateMetadata, validateMigratedConfig, validateOrg, validatePath, validateRules2 as validatePermissionRules, validateProject, validateRules, validateUri };
|
|
4206
3825
|
//# sourceMappingURL=index.js.map
|
|
4207
3826
|
//# sourceMappingURL=index.js.map
|