@fractary/codex 0.1.3 → 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 +9 -1
- package/dist/index.cjs +2 -391
- 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 +3 -384
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -224,7 +224,15 @@ Follow [Semantic Versioning](https://semver.org/):
|
|
|
224
224
|
|
|
225
225
|
MIT - see [LICENSE](LICENSE)
|
|
226
226
|
|
|
227
|
+
## Documentation
|
|
228
|
+
|
|
229
|
+
- [CLI Integration Guide](../../docs/guides/cli-integration.md) - How to integrate into CLI applications
|
|
230
|
+
- [API Reference](../../docs/guides/api-reference.md) - Complete API documentation
|
|
231
|
+
- [Configuration Guide](../../docs/guides/configuration.md) - Configuration reference
|
|
232
|
+
- [Troubleshooting](../../docs/guides/troubleshooting.md) - Common issues and solutions
|
|
233
|
+
- [Examples](../../docs/examples/) - Real-world usage patterns
|
|
234
|
+
|
|
227
235
|
## See Also
|
|
228
236
|
|
|
229
237
|
- [Python SDK](../python/) - `fractary-codex` on PyPI
|
|
230
|
-
- [
|
|
238
|
+
- [Technical Specifications](../../docs/specs/) - Detailed specs
|
package/dist/index.cjs
CHANGED
|
@@ -2419,387 +2419,6 @@ function setDefaultCacheManager(manager) {
|
|
|
2419
2419
|
defaultCacheManager = manager;
|
|
2420
2420
|
}
|
|
2421
2421
|
|
|
2422
|
-
// src/mcp/tools.ts
|
|
2423
|
-
var CODEX_TOOLS = [
|
|
2424
|
-
{
|
|
2425
|
-
name: "codex_fetch",
|
|
2426
|
-
description: "Fetch a document from the Codex knowledge base by URI. Returns the document content.",
|
|
2427
|
-
inputSchema: {
|
|
2428
|
-
type: "object",
|
|
2429
|
-
properties: {
|
|
2430
|
-
uri: {
|
|
2431
|
-
type: "string",
|
|
2432
|
-
description: "Codex URI in format: codex://org/project/path/to/file.md"
|
|
2433
|
-
},
|
|
2434
|
-
branch: {
|
|
2435
|
-
type: "string",
|
|
2436
|
-
description: "Git branch to fetch from (default: main)"
|
|
2437
|
-
},
|
|
2438
|
-
noCache: {
|
|
2439
|
-
type: "boolean",
|
|
2440
|
-
description: "Bypass cache and fetch fresh content"
|
|
2441
|
-
}
|
|
2442
|
-
},
|
|
2443
|
-
required: ["uri"]
|
|
2444
|
-
}
|
|
2445
|
-
},
|
|
2446
|
-
{
|
|
2447
|
-
name: "codex_search",
|
|
2448
|
-
description: "Search for documents in the Codex knowledge base.",
|
|
2449
|
-
inputSchema: {
|
|
2450
|
-
type: "object",
|
|
2451
|
-
properties: {
|
|
2452
|
-
query: {
|
|
2453
|
-
type: "string",
|
|
2454
|
-
description: "Search query string"
|
|
2455
|
-
},
|
|
2456
|
-
org: {
|
|
2457
|
-
type: "string",
|
|
2458
|
-
description: "Filter by organization"
|
|
2459
|
-
},
|
|
2460
|
-
project: {
|
|
2461
|
-
type: "string",
|
|
2462
|
-
description: "Filter by project"
|
|
2463
|
-
},
|
|
2464
|
-
limit: {
|
|
2465
|
-
type: "number",
|
|
2466
|
-
description: "Maximum number of results (default: 10)"
|
|
2467
|
-
},
|
|
2468
|
-
type: {
|
|
2469
|
-
type: "string",
|
|
2470
|
-
description: "Filter by artifact type (e.g., docs, specs, logs)"
|
|
2471
|
-
}
|
|
2472
|
-
},
|
|
2473
|
-
required: ["query"]
|
|
2474
|
-
}
|
|
2475
|
-
},
|
|
2476
|
-
{
|
|
2477
|
-
name: "codex_list",
|
|
2478
|
-
description: "List documents in the Codex cache.",
|
|
2479
|
-
inputSchema: {
|
|
2480
|
-
type: "object",
|
|
2481
|
-
properties: {
|
|
2482
|
-
org: {
|
|
2483
|
-
type: "string",
|
|
2484
|
-
description: "Filter by organization"
|
|
2485
|
-
},
|
|
2486
|
-
project: {
|
|
2487
|
-
type: "string",
|
|
2488
|
-
description: "Filter by project"
|
|
2489
|
-
},
|
|
2490
|
-
includeExpired: {
|
|
2491
|
-
type: "boolean",
|
|
2492
|
-
description: "Include expired cache entries"
|
|
2493
|
-
}
|
|
2494
|
-
}
|
|
2495
|
-
}
|
|
2496
|
-
},
|
|
2497
|
-
{
|
|
2498
|
-
name: "codex_invalidate",
|
|
2499
|
-
description: "Invalidate cached documents matching a pattern.",
|
|
2500
|
-
inputSchema: {
|
|
2501
|
-
type: "object",
|
|
2502
|
-
properties: {
|
|
2503
|
-
pattern: {
|
|
2504
|
-
type: "string",
|
|
2505
|
-
description: "URI pattern to invalidate (supports regex)"
|
|
2506
|
-
}
|
|
2507
|
-
},
|
|
2508
|
-
required: ["pattern"]
|
|
2509
|
-
}
|
|
2510
|
-
}
|
|
2511
|
-
];
|
|
2512
|
-
function textResult(text, isError = false) {
|
|
2513
|
-
return {
|
|
2514
|
-
content: [{ type: "text", text }],
|
|
2515
|
-
isError
|
|
2516
|
-
};
|
|
2517
|
-
}
|
|
2518
|
-
function resourceResult(uri, content, mimeType) {
|
|
2519
|
-
return {
|
|
2520
|
-
content: [
|
|
2521
|
-
{
|
|
2522
|
-
type: "resource",
|
|
2523
|
-
resource: {
|
|
2524
|
-
uri,
|
|
2525
|
-
mimeType,
|
|
2526
|
-
text: content
|
|
2527
|
-
}
|
|
2528
|
-
}
|
|
2529
|
-
]
|
|
2530
|
-
};
|
|
2531
|
-
}
|
|
2532
|
-
async function handleFetch(args, ctx) {
|
|
2533
|
-
const { uri, branch, noCache } = args;
|
|
2534
|
-
const ref = resolveReference(uri);
|
|
2535
|
-
if (!ref) {
|
|
2536
|
-
return textResult(`Invalid codex URI: ${uri}`, true);
|
|
2537
|
-
}
|
|
2538
|
-
try {
|
|
2539
|
-
let result;
|
|
2540
|
-
if (noCache) {
|
|
2541
|
-
result = await ctx.storage.fetch(ref, { branch });
|
|
2542
|
-
await ctx.cache.set(uri, result);
|
|
2543
|
-
} else {
|
|
2544
|
-
result = await ctx.cache.get(ref, { branch });
|
|
2545
|
-
}
|
|
2546
|
-
const content = result.content.toString("utf-8");
|
|
2547
|
-
return resourceResult(uri, content, result.contentType);
|
|
2548
|
-
} catch (error) {
|
|
2549
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
2550
|
-
return textResult(`Failed to fetch ${uri}: ${message}`, true);
|
|
2551
|
-
}
|
|
2552
|
-
}
|
|
2553
|
-
async function handleSearch(args, ctx) {
|
|
2554
|
-
const { query, org, project, limit = 10 } = args;
|
|
2555
|
-
const stats = await ctx.cache.getStats();
|
|
2556
|
-
if (stats.entryCount === 0) {
|
|
2557
|
-
return textResult("No documents in cache. Use codex_fetch to load documents first.");
|
|
2558
|
-
}
|
|
2559
|
-
const message = `Search functionality requires a search index.
|
|
2560
|
-
Query: "${query}"
|
|
2561
|
-
Filters: org=${org || "any"}, project=${project || "any"}
|
|
2562
|
-
Limit: ${limit}
|
|
2563
|
-
|
|
2564
|
-
To fetch documents, use codex_fetch with a specific URI like:
|
|
2565
|
-
codex://org/project/docs/file.md`;
|
|
2566
|
-
return textResult(message);
|
|
2567
|
-
}
|
|
2568
|
-
async function handleList(args, ctx) {
|
|
2569
|
-
const { org, project, includeExpired } = args;
|
|
2570
|
-
const stats = await ctx.cache.getStats();
|
|
2571
|
-
let message = `Cache Statistics:
|
|
2572
|
-
- Total entries: ${stats.entryCount}
|
|
2573
|
-
- Memory entries: ${stats.memoryEntries}
|
|
2574
|
-
- Memory size: ${formatBytes(stats.memorySize)}
|
|
2575
|
-
- Total size: ${formatBytes(stats.totalSize)}
|
|
2576
|
-
- Fresh: ${stats.freshCount}
|
|
2577
|
-
- Stale: ${stats.staleCount}
|
|
2578
|
-
- Expired: ${stats.expiredCount}`;
|
|
2579
|
-
if (org) {
|
|
2580
|
-
message += `
|
|
2581
|
-
|
|
2582
|
-
Filtered by org: ${org}`;
|
|
2583
|
-
}
|
|
2584
|
-
if (project) {
|
|
2585
|
-
message += `
|
|
2586
|
-
Filtered by project: ${project}`;
|
|
2587
|
-
}
|
|
2588
|
-
if (includeExpired) {
|
|
2589
|
-
message += `
|
|
2590
|
-
Including expired entries`;
|
|
2591
|
-
}
|
|
2592
|
-
return textResult(message);
|
|
2593
|
-
}
|
|
2594
|
-
async function handleInvalidate(args, ctx) {
|
|
2595
|
-
const { pattern } = args;
|
|
2596
|
-
try {
|
|
2597
|
-
const regex = new RegExp(pattern);
|
|
2598
|
-
const count = await ctx.cache.invalidatePattern(regex);
|
|
2599
|
-
return textResult(`Invalidated ${count} cache entries matching pattern: ${pattern}`);
|
|
2600
|
-
} catch (error) {
|
|
2601
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
2602
|
-
return textResult(`Invalid pattern: ${message}`, true);
|
|
2603
|
-
}
|
|
2604
|
-
}
|
|
2605
|
-
async function handleToolCall(name, args, ctx) {
|
|
2606
|
-
switch (name) {
|
|
2607
|
-
case "codex_fetch":
|
|
2608
|
-
return handleFetch(args, ctx);
|
|
2609
|
-
case "codex_search":
|
|
2610
|
-
return handleSearch(args, ctx);
|
|
2611
|
-
case "codex_list":
|
|
2612
|
-
return handleList(args, ctx);
|
|
2613
|
-
case "codex_invalidate":
|
|
2614
|
-
return handleInvalidate(args, ctx);
|
|
2615
|
-
default:
|
|
2616
|
-
return textResult(`Unknown tool: ${name}`, true);
|
|
2617
|
-
}
|
|
2618
|
-
}
|
|
2619
|
-
function formatBytes(bytes) {
|
|
2620
|
-
if (bytes === 0) return "0 B";
|
|
2621
|
-
const k = 1024;
|
|
2622
|
-
const sizes = ["B", "KB", "MB", "GB"];
|
|
2623
|
-
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
2624
|
-
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`;
|
|
2625
|
-
}
|
|
2626
|
-
|
|
2627
|
-
// src/mcp/server.ts
|
|
2628
|
-
var McpServer = class {
|
|
2629
|
-
config;
|
|
2630
|
-
toolContext;
|
|
2631
|
-
constructor(config) {
|
|
2632
|
-
this.config = {
|
|
2633
|
-
name: config.name ?? "codex",
|
|
2634
|
-
version: config.version ?? "1.0.0",
|
|
2635
|
-
cache: config.cache,
|
|
2636
|
-
storage: config.storage
|
|
2637
|
-
};
|
|
2638
|
-
this.toolContext = {
|
|
2639
|
-
cache: config.cache,
|
|
2640
|
-
storage: config.storage
|
|
2641
|
-
};
|
|
2642
|
-
}
|
|
2643
|
-
/**
|
|
2644
|
-
* Get server info
|
|
2645
|
-
*/
|
|
2646
|
-
getServerInfo() {
|
|
2647
|
-
return {
|
|
2648
|
-
name: this.config.name,
|
|
2649
|
-
version: this.config.version,
|
|
2650
|
-
capabilities: this.getCapabilities()
|
|
2651
|
-
};
|
|
2652
|
-
}
|
|
2653
|
-
/**
|
|
2654
|
-
* Get server capabilities
|
|
2655
|
-
*/
|
|
2656
|
-
getCapabilities() {
|
|
2657
|
-
return {
|
|
2658
|
-
tools: {
|
|
2659
|
-
listChanged: false
|
|
2660
|
-
},
|
|
2661
|
-
resources: {
|
|
2662
|
-
subscribe: false,
|
|
2663
|
-
listChanged: false
|
|
2664
|
-
}
|
|
2665
|
-
};
|
|
2666
|
-
}
|
|
2667
|
-
/**
|
|
2668
|
-
* List available tools
|
|
2669
|
-
*/
|
|
2670
|
-
listTools() {
|
|
2671
|
-
return CODEX_TOOLS;
|
|
2672
|
-
}
|
|
2673
|
-
/**
|
|
2674
|
-
* Call a tool
|
|
2675
|
-
*/
|
|
2676
|
-
async callTool(name, args) {
|
|
2677
|
-
return handleToolCall(name, args, this.toolContext);
|
|
2678
|
-
}
|
|
2679
|
-
/**
|
|
2680
|
-
* List available resources
|
|
2681
|
-
*/
|
|
2682
|
-
async listResources() {
|
|
2683
|
-
const resources = [];
|
|
2684
|
-
const stats = await this.config.cache.getStats();
|
|
2685
|
-
resources.push({
|
|
2686
|
-
uri: "codex://cache/summary",
|
|
2687
|
-
name: "Cache Summary",
|
|
2688
|
-
description: `${stats.entryCount} cached documents`,
|
|
2689
|
-
mimeType: "text/plain"
|
|
2690
|
-
});
|
|
2691
|
-
return resources;
|
|
2692
|
-
}
|
|
2693
|
-
/**
|
|
2694
|
-
* List resource templates
|
|
2695
|
-
*/
|
|
2696
|
-
listResourceTemplates() {
|
|
2697
|
-
return [
|
|
2698
|
-
{
|
|
2699
|
-
uriTemplate: "codex://{org}/{project}/{path}",
|
|
2700
|
-
name: "Codex Document",
|
|
2701
|
-
description: "Fetch a document from the Codex knowledge base",
|
|
2702
|
-
mimeType: "text/markdown"
|
|
2703
|
-
}
|
|
2704
|
-
];
|
|
2705
|
-
}
|
|
2706
|
-
/**
|
|
2707
|
-
* Read a resource
|
|
2708
|
-
*/
|
|
2709
|
-
async readResource(uri) {
|
|
2710
|
-
if (uri === "codex://cache/summary") {
|
|
2711
|
-
const stats = await this.config.cache.getStats();
|
|
2712
|
-
return [
|
|
2713
|
-
{
|
|
2714
|
-
uri,
|
|
2715
|
-
mimeType: "text/plain",
|
|
2716
|
-
text: `Cache Statistics:
|
|
2717
|
-
- Total entries: ${stats.entryCount}
|
|
2718
|
-
- Memory entries: ${stats.memoryEntries}
|
|
2719
|
-
- Fresh: ${stats.freshCount}
|
|
2720
|
-
- Stale: ${stats.staleCount}
|
|
2721
|
-
- Expired: ${stats.expiredCount}`
|
|
2722
|
-
}
|
|
2723
|
-
];
|
|
2724
|
-
}
|
|
2725
|
-
const ref = resolveReference(uri);
|
|
2726
|
-
if (!ref) {
|
|
2727
|
-
throw new Error(`Invalid codex URI: ${uri}`);
|
|
2728
|
-
}
|
|
2729
|
-
const result = await this.config.cache.get(ref);
|
|
2730
|
-
return [
|
|
2731
|
-
{
|
|
2732
|
-
uri,
|
|
2733
|
-
mimeType: result.contentType,
|
|
2734
|
-
text: result.content.toString("utf-8")
|
|
2735
|
-
}
|
|
2736
|
-
];
|
|
2737
|
-
}
|
|
2738
|
-
/**
|
|
2739
|
-
* Handle JSON-RPC request
|
|
2740
|
-
*
|
|
2741
|
-
* This method handles the low-level MCP protocol messages.
|
|
2742
|
-
*/
|
|
2743
|
-
async handleRequest(method, params) {
|
|
2744
|
-
switch (method) {
|
|
2745
|
-
case "initialize":
|
|
2746
|
-
return {
|
|
2747
|
-
protocolVersion: "2024-11-05",
|
|
2748
|
-
serverInfo: this.getServerInfo(),
|
|
2749
|
-
capabilities: this.getCapabilities()
|
|
2750
|
-
};
|
|
2751
|
-
case "tools/list":
|
|
2752
|
-
return { tools: this.listTools() };
|
|
2753
|
-
case "tools/call": {
|
|
2754
|
-
const { name, arguments: args } = params;
|
|
2755
|
-
return await this.callTool(name, args);
|
|
2756
|
-
}
|
|
2757
|
-
case "resources/list":
|
|
2758
|
-
return { resources: await this.listResources() };
|
|
2759
|
-
case "resources/templates/list":
|
|
2760
|
-
return { resourceTemplates: this.listResourceTemplates() };
|
|
2761
|
-
case "resources/read": {
|
|
2762
|
-
const { uri } = params;
|
|
2763
|
-
return { contents: await this.readResource(uri) };
|
|
2764
|
-
}
|
|
2765
|
-
case "prompts/list":
|
|
2766
|
-
return { prompts: [] };
|
|
2767
|
-
default:
|
|
2768
|
-
throw new Error(`Unknown method: ${method}`);
|
|
2769
|
-
}
|
|
2770
|
-
}
|
|
2771
|
-
/**
|
|
2772
|
-
* Process a JSON-RPC message
|
|
2773
|
-
*/
|
|
2774
|
-
async processMessage(message) {
|
|
2775
|
-
let id = null;
|
|
2776
|
-
try {
|
|
2777
|
-
const request = JSON.parse(message);
|
|
2778
|
-
id = request.id;
|
|
2779
|
-
const { method, params } = request;
|
|
2780
|
-
const result = await this.handleRequest(method, params);
|
|
2781
|
-
return JSON.stringify({
|
|
2782
|
-
jsonrpc: "2.0",
|
|
2783
|
-
id,
|
|
2784
|
-
result
|
|
2785
|
-
});
|
|
2786
|
-
} catch (error) {
|
|
2787
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
2788
|
-
return JSON.stringify({
|
|
2789
|
-
jsonrpc: "2.0",
|
|
2790
|
-
id,
|
|
2791
|
-
error: {
|
|
2792
|
-
code: -32603,
|
|
2793
|
-
message: errorMessage
|
|
2794
|
-
}
|
|
2795
|
-
});
|
|
2796
|
-
}
|
|
2797
|
-
}
|
|
2798
|
-
};
|
|
2799
|
-
function createMcpServer(config) {
|
|
2800
|
-
return new McpServer(config);
|
|
2801
|
-
}
|
|
2802
|
-
|
|
2803
2422
|
// src/sync/types.ts
|
|
2804
2423
|
var DEFAULT_SYNC_CONFIG = {
|
|
2805
2424
|
defaultDirection: "to-codex",
|
|
@@ -3084,10 +2703,10 @@ function formatPlanSummary(plan) {
|
|
|
3084
2703
|
lines.push(`\u26A0\uFE0F Conflicts: ${stats.conflicts} files`);
|
|
3085
2704
|
lines.push("");
|
|
3086
2705
|
}
|
|
3087
|
-
lines.push(`Total: ${plan.totalFiles} files (${
|
|
2706
|
+
lines.push(`Total: ${plan.totalFiles} files (${formatBytes(stats.totalBytes)})`);
|
|
3088
2707
|
return lines.join("\n");
|
|
3089
2708
|
}
|
|
3090
|
-
function
|
|
2709
|
+
function formatBytes(bytes) {
|
|
3091
2710
|
if (bytes === 0) return "0 B";
|
|
3092
2711
|
const k = 1024;
|
|
3093
2712
|
const sizes = ["B", "KB", "MB", "GB"];
|
|
@@ -4213,7 +3832,6 @@ function generateReferenceMigrationSummary(results) {
|
|
|
4213
3832
|
|
|
4214
3833
|
exports.AutoSyncPatternSchema = AutoSyncPatternSchema;
|
|
4215
3834
|
exports.BUILT_IN_TYPES = BUILT_IN_TYPES;
|
|
4216
|
-
exports.CODEX_TOOLS = CODEX_TOOLS;
|
|
4217
3835
|
exports.CODEX_URI_PREFIX = CODEX_URI_PREFIX;
|
|
4218
3836
|
exports.CacheManager = CacheManager;
|
|
4219
3837
|
exports.CachePersistence = CachePersistence;
|
|
@@ -4233,7 +3851,6 @@ exports.HttpStorage = HttpStorage;
|
|
|
4233
3851
|
exports.LEGACY_PATTERNS = LEGACY_PATTERNS;
|
|
4234
3852
|
exports.LEGACY_REF_PREFIX = LEGACY_REF_PREFIX;
|
|
4235
3853
|
exports.LocalStorage = LocalStorage;
|
|
4236
|
-
exports.McpServer = McpServer;
|
|
4237
3854
|
exports.MetadataSchema = MetadataSchema;
|
|
4238
3855
|
exports.PERMISSION_LEVEL_ORDER = PERMISSION_LEVEL_ORDER;
|
|
4239
3856
|
exports.PermissionDeniedError = PermissionDeniedError;
|
|
@@ -4260,7 +3877,6 @@ exports.createEmptySyncPlan = createEmptySyncPlan;
|
|
|
4260
3877
|
exports.createGitHubStorage = createGitHubStorage;
|
|
4261
3878
|
exports.createHttpStorage = createHttpStorage;
|
|
4262
3879
|
exports.createLocalStorage = createLocalStorage;
|
|
4263
|
-
exports.createMcpServer = createMcpServer;
|
|
4264
3880
|
exports.createPermissionManager = createPermissionManager;
|
|
4265
3881
|
exports.createRule = createRule;
|
|
4266
3882
|
exports.createRulesFromPatterns = createRulesFromPatterns;
|
|
@@ -4308,11 +3924,6 @@ exports.getPlanStats = getPlanStats;
|
|
|
4308
3924
|
exports.getRelativeCachePath = getRelativeCachePath;
|
|
4309
3925
|
exports.getRemainingTtl = getRemainingTtl;
|
|
4310
3926
|
exports.getTargetRepos = getTargetRepos;
|
|
4311
|
-
exports.handleFetch = handleFetch;
|
|
4312
|
-
exports.handleInvalidate = handleInvalidate;
|
|
4313
|
-
exports.handleList = handleList;
|
|
4314
|
-
exports.handleSearch = handleSearch;
|
|
4315
|
-
exports.handleToolCall = handleToolCall;
|
|
4316
3927
|
exports.hasContentChanged = hasContentChanged;
|
|
4317
3928
|
exports.hasFrontmatter = hasFrontmatter;
|
|
4318
3929
|
exports.hasLegacyReferences = hasLegacyReferences;
|