@slashfi/agents-sdk 0.61.0 → 0.63.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 +266 -212
- package/dist/adk.js +47 -0
- package/dist/adk.js.map +1 -1
- package/dist/agent-definitions/config.js +2 -2
- package/dist/agent-definitions/config.js.map +1 -1
- package/dist/cjs/agent-definitions/config.js +2 -2
- package/dist/cjs/agent-definitions/config.js.map +1 -1
- package/dist/cjs/config-store.js +19 -11
- package/dist/cjs/config-store.js.map +1 -1
- package/dist/cjs/index.js +1 -29
- package/dist/cjs/index.js.map +1 -1
- package/dist/config-store.d.ts +2 -2
- package/dist/config-store.d.ts.map +1 -1
- package/dist/config-store.js +19 -11
- package/dist/config-store.js.map +1 -1
- package/dist/index.d.ts +1 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -18
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +14 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/adk.ts +40 -0
- package/src/agent-definitions/config.ts +2 -2
- package/src/config-store.test.ts +2 -2
- package/src/config-store.ts +17 -11
- package/src/consumer.test.ts +4 -4
- package/src/index.ts +1 -44
- package/src/types.ts +8 -0
- package/dist/cjs/client.js +0 -193
- package/dist/cjs/client.js.map +0 -1
- package/dist/cjs/codegen.js +0 -1071
- package/dist/cjs/codegen.js.map +0 -1
- package/dist/cjs/introspect.js +0 -136
- package/dist/cjs/introspect.js.map +0 -1
- package/dist/cjs/jsonc.js +0 -74
- package/dist/cjs/jsonc.js.map +0 -1
- package/dist/cjs/pack.js +0 -256
- package/dist/cjs/pack.js.map +0 -1
- package/dist/client.d.ts +0 -49
- package/dist/client.d.ts.map +0 -1
- package/dist/client.js +0 -190
- package/dist/client.js.map +0 -1
- package/dist/codegen.d.ts +0 -163
- package/dist/codegen.d.ts.map +0 -1
- package/dist/codegen.js +0 -1059
- package/dist/codegen.js.map +0 -1
- package/dist/introspect.d.ts +0 -16
- package/dist/introspect.d.ts.map +0 -1
- package/dist/introspect.js +0 -133
- package/dist/introspect.js.map +0 -1
- package/dist/jsonc.d.ts +0 -15
- package/dist/jsonc.d.ts.map +0 -1
- package/dist/jsonc.js +0 -70
- package/dist/jsonc.js.map +0 -1
- package/dist/pack.d.ts +0 -59
- package/dist/pack.d.ts.map +0 -1
- package/dist/pack.js +0 -252
- package/dist/pack.js.map +0 -1
- package/src/client.ts +0 -273
- package/src/codegen.test.ts +0 -537
- package/src/codegen.ts +0 -1423
- package/src/introspect.ts +0 -171
- package/src/jsonc.ts +0 -83
- package/src/pack.ts +0 -394
package/src/introspect.ts
DELETED
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* MCP Introspection
|
|
3
|
-
*
|
|
4
|
-
* Connects to an MCP server, introspects its tools,
|
|
5
|
-
* and outputs a SerializedAgentDefinition (agent.json).
|
|
6
|
-
*
|
|
7
|
-
* Deduplicates shared $defs across tools.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { spawn } from "node:child_process";
|
|
11
|
-
import { mkdirSync, writeFileSync } from "node:fs";
|
|
12
|
-
import { dirname, resolve } from "node:path";
|
|
13
|
-
|
|
14
|
-
export interface IntrospectOptions {
|
|
15
|
-
server: string;
|
|
16
|
-
name: string;
|
|
17
|
-
out?: string;
|
|
18
|
-
env?: Record<string, string>;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function deduplicateDefs(tools: Record<string, unknown>[]): {
|
|
22
|
-
tools: Record<string, unknown>[];
|
|
23
|
-
sharedDefs: Record<string, unknown>;
|
|
24
|
-
} {
|
|
25
|
-
const defsByContent = new Map<string, { name: string; schema: unknown }>();
|
|
26
|
-
|
|
27
|
-
for (const tool of tools) {
|
|
28
|
-
const schema = tool.inputSchema as Record<string, unknown> | undefined;
|
|
29
|
-
const defs = schema?.$defs;
|
|
30
|
-
if (!defs || typeof defs !== "object") continue;
|
|
31
|
-
for (const [defName, defSchema] of Object.entries(
|
|
32
|
-
defs as Record<string, unknown>,
|
|
33
|
-
)) {
|
|
34
|
-
const key = JSON.stringify(defSchema);
|
|
35
|
-
if (!defsByContent.has(key)) {
|
|
36
|
-
defsByContent.set(key, { name: defName, schema: defSchema });
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const sharedDefs: Record<string, unknown> = {};
|
|
42
|
-
for (const { name, schema } of Array.from(defsByContent.values())) {
|
|
43
|
-
sharedDefs[name] = schema;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const cleanedTools = tools.map((t) => {
|
|
47
|
-
const schema = { ...(t.inputSchema as Record<string, unknown>) };
|
|
48
|
-
schema.$defs = undefined;
|
|
49
|
-
return { ...t, inputSchema: schema };
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
return { tools: cleanedTools, sharedDefs };
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export async function introspectMcp(options: IntrospectOptions): Promise<void> {
|
|
56
|
-
const { server, name, out } = options;
|
|
57
|
-
|
|
58
|
-
const parts = server.split(/\s+/);
|
|
59
|
-
const proc = spawn(parts[0], parts.slice(1), {
|
|
60
|
-
stdio: ["pipe", "pipe", "pipe"],
|
|
61
|
-
env: { ...process.env, ...options.env },
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
let buffer = "";
|
|
65
|
-
let messageId = 0;
|
|
66
|
-
|
|
67
|
-
function sendJsonRpc(
|
|
68
|
-
method: string,
|
|
69
|
-
params: Record<string, unknown> = {},
|
|
70
|
-
): number {
|
|
71
|
-
const id = ++messageId;
|
|
72
|
-
const msg = JSON.stringify({ jsonrpc: "2.0", id, method, params });
|
|
73
|
-
proc.stdin?.write(`${msg}\n`);
|
|
74
|
-
return id;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function waitForResponse(targetId: number): Promise<Record<string, unknown>> {
|
|
78
|
-
return new Promise((res, reject) => {
|
|
79
|
-
const timeout = setTimeout(() => reject(new Error("Timeout")), 30000);
|
|
80
|
-
const handler = (chunk: Buffer) => {
|
|
81
|
-
buffer += chunk.toString();
|
|
82
|
-
const lines = buffer.split("\n");
|
|
83
|
-
buffer = lines.pop() || "";
|
|
84
|
-
for (const line of lines) {
|
|
85
|
-
const trimmed = line.trim();
|
|
86
|
-
if (!trimmed) continue;
|
|
87
|
-
try {
|
|
88
|
-
const parsed = JSON.parse(trimmed);
|
|
89
|
-
if (parsed.id === targetId) {
|
|
90
|
-
clearTimeout(timeout);
|
|
91
|
-
proc.stdout?.removeListener("data", handler);
|
|
92
|
-
if (parsed.error) reject(new Error(JSON.stringify(parsed.error)));
|
|
93
|
-
else res(parsed.result);
|
|
94
|
-
}
|
|
95
|
-
} catch {
|
|
96
|
-
/* ignore non-JSON */
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
proc.stdout?.on("data", handler);
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
try {
|
|
105
|
-
console.log(`Connecting to MCP server: ${server}`);
|
|
106
|
-
|
|
107
|
-
const initId = sendJsonRpc("initialize", {
|
|
108
|
-
protocolVersion: "2024-11-05",
|
|
109
|
-
capabilities: {},
|
|
110
|
-
clientInfo: { name: "adk-introspect", version: "1.0.0" },
|
|
111
|
-
});
|
|
112
|
-
const initResult = (await waitForResponse(initId)) as Record<
|
|
113
|
-
string,
|
|
114
|
-
unknown
|
|
115
|
-
>;
|
|
116
|
-
const serverInfo = initResult.serverInfo as
|
|
117
|
-
| Record<string, string>
|
|
118
|
-
| undefined;
|
|
119
|
-
console.log(`Server: ${serverInfo?.name} v${serverInfo?.version}`);
|
|
120
|
-
|
|
121
|
-
proc.stdin?.write(
|
|
122
|
-
`${JSON.stringify({ jsonrpc: "2.0", method: "notifications/initialized" })}\n`,
|
|
123
|
-
);
|
|
124
|
-
|
|
125
|
-
const toolsId = sendJsonRpc("tools/list", {});
|
|
126
|
-
const toolsResult = (await waitForResponse(toolsId)) as Record<
|
|
127
|
-
string,
|
|
128
|
-
unknown
|
|
129
|
-
>;
|
|
130
|
-
const rawTools = (
|
|
131
|
-
(toolsResult.tools || []) as Record<string, unknown>[]
|
|
132
|
-
).map((t) => ({
|
|
133
|
-
name: t.name as string,
|
|
134
|
-
description: (t.description as string) || "",
|
|
135
|
-
inputSchema: (t.inputSchema as Record<string, unknown>) || {
|
|
136
|
-
type: "object",
|
|
137
|
-
properties: {},
|
|
138
|
-
},
|
|
139
|
-
}));
|
|
140
|
-
console.log(`Discovered ${rawTools.length} tools`);
|
|
141
|
-
|
|
142
|
-
const { tools, sharedDefs } = deduplicateDefs(rawTools);
|
|
143
|
-
const defsCount = Object.keys(sharedDefs).length;
|
|
144
|
-
if (defsCount > 0) {
|
|
145
|
-
console.log(`Hoisted ${defsCount} shared $defs`);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const definition: Record<string, unknown> = {
|
|
149
|
-
path: name,
|
|
150
|
-
name: serverInfo?.name || name,
|
|
151
|
-
description: `Agent for ${serverInfo?.name || name}`,
|
|
152
|
-
version: serverInfo?.version || "1.0.0",
|
|
153
|
-
visibility: "public",
|
|
154
|
-
serverSource: server,
|
|
155
|
-
serverInfo,
|
|
156
|
-
...(defsCount > 0 ? { $defs: sharedDefs } : {}),
|
|
157
|
-
tools,
|
|
158
|
-
generatedAt: new Date().toISOString(),
|
|
159
|
-
sdkVersion: "0.22.0",
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
const outPath = out || `./${name}.json`;
|
|
163
|
-
const resolved = resolve(outPath);
|
|
164
|
-
mkdirSync(dirname(resolved), { recursive: true });
|
|
165
|
-
writeFileSync(resolved, `${JSON.stringify(definition, null, 2)}\n`);
|
|
166
|
-
const sizeKB = (JSON.stringify(definition).length / 1024).toFixed(1);
|
|
167
|
-
console.log(`\nWrote ${resolved} (${sizeKB}KB, ${tools.length} tools)`);
|
|
168
|
-
} finally {
|
|
169
|
-
proc.kill();
|
|
170
|
-
}
|
|
171
|
-
}
|
package/src/jsonc.ts
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* JSONC Parser
|
|
3
|
-
*
|
|
4
|
-
* Parses JSON with comments (single-line and multi-line)
|
|
5
|
-
* so agent.json files can be annotated.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { readFileSync } from "node:fs";
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Strip comments from JSONC content and parse as JSON.
|
|
12
|
-
*/
|
|
13
|
-
export function parseJsonc(content: string): unknown {
|
|
14
|
-
let result = "";
|
|
15
|
-
let i = 0;
|
|
16
|
-
let inString = false;
|
|
17
|
-
let stringChar = "";
|
|
18
|
-
|
|
19
|
-
while (i < content.length) {
|
|
20
|
-
if (inString) {
|
|
21
|
-
if (content[i] === "\\" && i + 1 < content.length) {
|
|
22
|
-
result += content[i] + content[i + 1];
|
|
23
|
-
i += 2;
|
|
24
|
-
continue;
|
|
25
|
-
}
|
|
26
|
-
if (content[i] === stringChar) {
|
|
27
|
-
inString = false;
|
|
28
|
-
}
|
|
29
|
-
result += content[i];
|
|
30
|
-
i++;
|
|
31
|
-
continue;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (content[i] === '"') {
|
|
35
|
-
inString = true;
|
|
36
|
-
stringChar = content[i];
|
|
37
|
-
result += content[i];
|
|
38
|
-
i++;
|
|
39
|
-
continue;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// Single-line comment
|
|
43
|
-
if (
|
|
44
|
-
content[i] === "/" &&
|
|
45
|
-
i + 1 < content.length &&
|
|
46
|
-
content[i + 1] === "/"
|
|
47
|
-
) {
|
|
48
|
-
while (i < content.length && content[i] !== "\n") i++;
|
|
49
|
-
continue;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// Multi-line comment
|
|
53
|
-
if (
|
|
54
|
-
content[i] === "/" &&
|
|
55
|
-
i + 1 < content.length &&
|
|
56
|
-
content[i + 1] === "*"
|
|
57
|
-
) {
|
|
58
|
-
i += 2;
|
|
59
|
-
while (
|
|
60
|
-
i + 1 < content.length &&
|
|
61
|
-
!(content[i] === "*" && content[i + 1] === "/")
|
|
62
|
-
)
|
|
63
|
-
i++;
|
|
64
|
-
i += 2;
|
|
65
|
-
continue;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
result += content[i];
|
|
69
|
-
i++;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// Handle trailing commas
|
|
73
|
-
const cleaned = result.replace(/,\s*([}\]])/g, "$1");
|
|
74
|
-
return JSON.parse(cleaned);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Read and parse a JSONC file.
|
|
79
|
-
*/
|
|
80
|
-
export function readJsoncFile(path: string): unknown {
|
|
81
|
-
const content = readFileSync(path, "utf-8");
|
|
82
|
-
return parseJsonc(content);
|
|
83
|
-
}
|
package/src/pack.ts
DELETED
|
@@ -1,394 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ADK Pack
|
|
3
|
-
*
|
|
4
|
-
* Generates a publishable npm package from an agent.json file.
|
|
5
|
-
*
|
|
6
|
-
* Input: agent.json (SerializedAgentDefinition)
|
|
7
|
-
* Output: ready-to-publish directory with:
|
|
8
|
-
* - package.json
|
|
9
|
-
* - agent.json (copy)
|
|
10
|
-
* - meta.json (version metadata + diff)
|
|
11
|
-
* - index.js + index.d.ts (typed export)
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import { spawnSync } from "node:child_process";
|
|
15
|
-
import { createHash } from "node:crypto";
|
|
16
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
17
|
-
import { resolve } from "node:path";
|
|
18
|
-
import { parseJsonc } from "./jsonc.js";
|
|
19
|
-
import type { SerializedAgentDefinition } from "./serialized.js";
|
|
20
|
-
import { assertValidDefinition } from "./validate.js";
|
|
21
|
-
|
|
22
|
-
// ============================================
|
|
23
|
-
// Types
|
|
24
|
-
// ============================================
|
|
25
|
-
|
|
26
|
-
export interface PackOptions {
|
|
27
|
-
/** Path to agent.json */
|
|
28
|
-
agentFile: string;
|
|
29
|
-
/** Output directory (default: ./dist) */
|
|
30
|
-
outDir?: string;
|
|
31
|
-
/** npm scope (default: @agentdef) */
|
|
32
|
-
scope?: string;
|
|
33
|
-
/** Previous version's agent.json path for diff (optional) */
|
|
34
|
-
previousAgentFile?: string;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export interface PackResult {
|
|
38
|
-
packageDir: string;
|
|
39
|
-
packageName: string;
|
|
40
|
-
version: string;
|
|
41
|
-
hash: string;
|
|
42
|
-
meta: VersionMeta;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export interface VersionMeta {
|
|
46
|
-
hash: string;
|
|
47
|
-
serverVersion: string;
|
|
48
|
-
npmPackage?: string;
|
|
49
|
-
toolCount: number;
|
|
50
|
-
sizeBytes: number;
|
|
51
|
-
generatedAt: string;
|
|
52
|
-
sdkVersion: string;
|
|
53
|
-
changes?: VersionChanges;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export interface VersionChanges {
|
|
57
|
-
previousHash?: string;
|
|
58
|
-
toolsAdded: string[];
|
|
59
|
-
toolsRemoved: string[];
|
|
60
|
-
toolsModified: string[];
|
|
61
|
-
schemaChanges: string[];
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// ============================================
|
|
65
|
-
// Hash
|
|
66
|
-
// ============================================
|
|
67
|
-
|
|
68
|
-
function contentHash(content: string): string {
|
|
69
|
-
return createHash("sha256").update(content).digest("hex").slice(0, 8);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// ============================================
|
|
73
|
-
// Diff
|
|
74
|
-
// ============================================
|
|
75
|
-
|
|
76
|
-
function diffDefinitions(
|
|
77
|
-
current: SerializedAgentDefinition,
|
|
78
|
-
previous: SerializedAgentDefinition,
|
|
79
|
-
previousRawContent: string,
|
|
80
|
-
): VersionChanges {
|
|
81
|
-
const prevToolNames = new Set(previous.tools.map((t) => t.name));
|
|
82
|
-
const currToolNames = new Set(current.tools.map((t) => t.name));
|
|
83
|
-
|
|
84
|
-
const toolsAdded = current.tools
|
|
85
|
-
.filter((t) => !prevToolNames.has(t.name))
|
|
86
|
-
.map((t) => t.name);
|
|
87
|
-
const toolsRemoved = previous.tools
|
|
88
|
-
.filter((t) => !currToolNames.has(t.name))
|
|
89
|
-
.map((t) => t.name);
|
|
90
|
-
|
|
91
|
-
// Find modified tools (same name, different schema)
|
|
92
|
-
const toolsModified: string[] = [];
|
|
93
|
-
const schemaChanges: string[] = [];
|
|
94
|
-
const prevToolMap = new Map(previous.tools.map((t) => [t.name, t]));
|
|
95
|
-
|
|
96
|
-
for (const tool of current.tools) {
|
|
97
|
-
const prev = prevToolMap.get(tool.name);
|
|
98
|
-
if (!prev) continue;
|
|
99
|
-
const currSchema = JSON.stringify(tool.inputSchema);
|
|
100
|
-
const prevSchema = JSON.stringify(prev.inputSchema);
|
|
101
|
-
if (currSchema !== prevSchema) {
|
|
102
|
-
toolsModified.push(tool.name);
|
|
103
|
-
// Generate human-readable schema change description
|
|
104
|
-
const currProps = Object.keys(
|
|
105
|
-
((tool.inputSchema as Record<string, unknown>).properties as Record<
|
|
106
|
-
string,
|
|
107
|
-
unknown
|
|
108
|
-
>) || {},
|
|
109
|
-
);
|
|
110
|
-
const prevProps = Object.keys(
|
|
111
|
-
((prev.inputSchema as Record<string, unknown>).properties as Record<
|
|
112
|
-
string,
|
|
113
|
-
unknown
|
|
114
|
-
>) || {},
|
|
115
|
-
);
|
|
116
|
-
const added = currProps.filter((p) => !prevProps.includes(p));
|
|
117
|
-
const removed = prevProps.filter((p) => !currProps.includes(p));
|
|
118
|
-
if (added.length > 0) {
|
|
119
|
-
schemaChanges.push(
|
|
120
|
-
`${tool.name}: added properties: ${added.join(", ")}`,
|
|
121
|
-
);
|
|
122
|
-
}
|
|
123
|
-
if (removed.length > 0) {
|
|
124
|
-
schemaChanges.push(
|
|
125
|
-
`${tool.name}: removed properties: ${removed.join(", ")}`,
|
|
126
|
-
);
|
|
127
|
-
}
|
|
128
|
-
if (added.length === 0 && removed.length === 0) {
|
|
129
|
-
schemaChanges.push(`${tool.name}: schema modified`);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
return {
|
|
135
|
-
previousHash: contentHash(previousRawContent),
|
|
136
|
-
toolsAdded,
|
|
137
|
-
toolsRemoved,
|
|
138
|
-
toolsModified,
|
|
139
|
-
schemaChanges,
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// ============================================
|
|
144
|
-
// Pack
|
|
145
|
-
// ============================================
|
|
146
|
-
|
|
147
|
-
export function pack(options: PackOptions): PackResult {
|
|
148
|
-
const { agentFile, outDir = "./dist", scope = "@agentdef" } = options;
|
|
149
|
-
|
|
150
|
-
// Read agent.json
|
|
151
|
-
const agentPath = resolve(agentFile);
|
|
152
|
-
if (!existsSync(agentPath)) {
|
|
153
|
-
throw new Error(`agent.json not found: ${agentPath}`);
|
|
154
|
-
}
|
|
155
|
-
const agentContent = readFileSync(agentPath, "utf-8");
|
|
156
|
-
const definition = parseJsonc(agentContent) as SerializedAgentDefinition;
|
|
157
|
-
|
|
158
|
-
// Validate the definition schema
|
|
159
|
-
assertValidDefinition(definition, agentPath);
|
|
160
|
-
|
|
161
|
-
// Compute hash + version
|
|
162
|
-
const hash = contentHash(agentContent);
|
|
163
|
-
const version = `${definition.version || "1.0.0"}`;
|
|
164
|
-
const packageName = `${scope}/${definition.path}`;
|
|
165
|
-
|
|
166
|
-
// Create output directory
|
|
167
|
-
const packageDir = resolve(outDir, definition.path);
|
|
168
|
-
mkdirSync(packageDir, { recursive: true });
|
|
169
|
-
|
|
170
|
-
// Generate meta.json
|
|
171
|
-
const meta: VersionMeta = {
|
|
172
|
-
hash,
|
|
173
|
-
serverVersion:
|
|
174
|
-
definition.serverInfo?.version || definition.version || "1.0.0",
|
|
175
|
-
npmPackage: definition.serverSource,
|
|
176
|
-
toolCount: definition.tools.length,
|
|
177
|
-
sizeBytes: Buffer.byteLength(agentContent, "utf-8"),
|
|
178
|
-
generatedAt: definition.generatedAt || new Date().toISOString(),
|
|
179
|
-
sdkVersion: definition.sdkVersion || "0.21.0",
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
// Diff against previous if provided
|
|
183
|
-
if (options.previousAgentFile) {
|
|
184
|
-
const prevPath = resolve(options.previousAgentFile);
|
|
185
|
-
if (existsSync(prevPath)) {
|
|
186
|
-
const prevContent = readFileSync(prevPath, "utf-8");
|
|
187
|
-
const prevDef = parseJsonc(prevContent) as SerializedAgentDefinition;
|
|
188
|
-
meta.changes = diffDefinitions(definition, prevDef, prevContent);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
// Write agent.json
|
|
193
|
-
writeFileSync(resolve(packageDir, "agent.json"), agentContent);
|
|
194
|
-
|
|
195
|
-
// Write meta.json
|
|
196
|
-
writeFileSync(
|
|
197
|
-
resolve(packageDir, "meta.json"),
|
|
198
|
-
`${JSON.stringify(meta, null, 2)}\n`,
|
|
199
|
-
);
|
|
200
|
-
|
|
201
|
-
// Write package.json
|
|
202
|
-
const pkg = {
|
|
203
|
-
name: packageName,
|
|
204
|
-
version,
|
|
205
|
-
description:
|
|
206
|
-
definition.description || `Agent definition for ${definition.name}`,
|
|
207
|
-
type: "module",
|
|
208
|
-
main: "index.js",
|
|
209
|
-
types: "index.d.ts",
|
|
210
|
-
exports: {
|
|
211
|
-
".": {
|
|
212
|
-
import: "./index.js",
|
|
213
|
-
types: "./index.d.ts",
|
|
214
|
-
},
|
|
215
|
-
"./agent.json": "./agent.json",
|
|
216
|
-
"./meta.json": "./meta.json",
|
|
217
|
-
},
|
|
218
|
-
files: ["index.js", "index.d.ts", "agent.json", "meta.json"],
|
|
219
|
-
peerDependencies: {
|
|
220
|
-
"@slashfi/agents-sdk": ">=0.21.0",
|
|
221
|
-
},
|
|
222
|
-
keywords: ["agent", "mcp", "agentdef", definition.path],
|
|
223
|
-
license: "MIT",
|
|
224
|
-
repository: {
|
|
225
|
-
type: "git",
|
|
226
|
-
url: "https://github.com/slashfi/agents-sdk",
|
|
227
|
-
},
|
|
228
|
-
};
|
|
229
|
-
writeFileSync(
|
|
230
|
-
resolve(packageDir, "package.json"),
|
|
231
|
-
`${JSON.stringify(pkg, null, 2)}\n`,
|
|
232
|
-
);
|
|
233
|
-
|
|
234
|
-
// Write index.js
|
|
235
|
-
writeFileSync(
|
|
236
|
-
resolve(packageDir, "index.js"),
|
|
237
|
-
[
|
|
238
|
-
'import { readFileSync } from "node:fs";',
|
|
239
|
-
'import { fileURLToPath } from "node:url";',
|
|
240
|
-
'import { dirname, resolve } from "node:path";',
|
|
241
|
-
"",
|
|
242
|
-
"const __dirname = dirname(fileURLToPath(import.meta.url));",
|
|
243
|
-
'const definition = JSON.parse(readFileSync(resolve(__dirname, "agent.json"), "utf-8"));',
|
|
244
|
-
"export default definition;",
|
|
245
|
-
"export { definition };",
|
|
246
|
-
"",
|
|
247
|
-
].join("\n"),
|
|
248
|
-
);
|
|
249
|
-
|
|
250
|
-
// Write index.d.ts
|
|
251
|
-
writeFileSync(
|
|
252
|
-
resolve(packageDir, "index.d.ts"),
|
|
253
|
-
[
|
|
254
|
-
'import type { SerializedAgentDefinition } from "@slashfi/agents-sdk";',
|
|
255
|
-
"",
|
|
256
|
-
"declare const definition: SerializedAgentDefinition;",
|
|
257
|
-
"export default definition;",
|
|
258
|
-
"export { definition };",
|
|
259
|
-
"",
|
|
260
|
-
].join("\n"),
|
|
261
|
-
);
|
|
262
|
-
|
|
263
|
-
return { packageDir, packageName, version, hash, meta };
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
// ============================================
|
|
267
|
-
// Publish
|
|
268
|
-
// ============================================
|
|
269
|
-
|
|
270
|
-
export interface PublishOptions extends PackOptions {
|
|
271
|
-
/** Dry run (don't actually publish) */
|
|
272
|
-
dryRun?: boolean;
|
|
273
|
-
/** npm tag (default: latest) */
|
|
274
|
-
tag?: string;
|
|
275
|
-
/** npm access level */
|
|
276
|
-
access?: "public" | "restricted";
|
|
277
|
-
/** npm registry URL */
|
|
278
|
-
registry?: string;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
/**
|
|
282
|
-
* Compare semver: returns true if `a` is older than `b`.
|
|
283
|
-
*/
|
|
284
|
-
function isOlderVersion(a: string, b: string): boolean {
|
|
285
|
-
const pa = a.split(".").map(Number);
|
|
286
|
-
const pb = b.split(".").map(Number);
|
|
287
|
-
for (let i = 0; i < 3; i++) {
|
|
288
|
-
if ((pa[i] || 0) < (pb[i] || 0)) return true;
|
|
289
|
-
if ((pa[i] || 0) > (pb[i] || 0)) return false;
|
|
290
|
-
}
|
|
291
|
-
return false; // equal
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
export function publish(options: PublishOptions): PackResult {
|
|
295
|
-
const result = pack(options);
|
|
296
|
-
|
|
297
|
-
// Check if this version already exists in the registry
|
|
298
|
-
const registryArgs = options.registry ? ["--registry", options.registry] : [];
|
|
299
|
-
const viewProc = spawnSync(
|
|
300
|
-
"npm",
|
|
301
|
-
["view", `${result.packageName}`, "versions", "--json", ...registryArgs],
|
|
302
|
-
{ encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] },
|
|
303
|
-
);
|
|
304
|
-
|
|
305
|
-
if (viewProc.status === 0 && viewProc.stdout) {
|
|
306
|
-
try {
|
|
307
|
-
const raw = JSON.parse(viewProc.stdout);
|
|
308
|
-
const existing: string[] = Array.isArray(raw) ? raw : [raw];
|
|
309
|
-
|
|
310
|
-
// E409 prevention: version already exists
|
|
311
|
-
if (existing.includes(result.version)) {
|
|
312
|
-
const versions = existing.join(", ");
|
|
313
|
-
throw new Error(
|
|
314
|
-
`\x1b[31m\u2717 ${result.packageName}@${result.version} already exists in registry\x1b[0m\n\n Published versions: ${versions}\n Hint: bump the version in agent.json, or use --tag to publish a pre-release`,
|
|
315
|
-
);
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
// Out-of-order protection: warn if publishing older than latest
|
|
319
|
-
const latestViewProc = spawnSync(
|
|
320
|
-
"npm",
|
|
321
|
-
["view", `${result.packageName}`, "dist-tags.latest", ...registryArgs],
|
|
322
|
-
{ encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] },
|
|
323
|
-
);
|
|
324
|
-
if (latestViewProc.status === 0 && latestViewProc.stdout) {
|
|
325
|
-
const latest = latestViewProc.stdout.trim();
|
|
326
|
-
if (latest && !options.tag && isOlderVersion(result.version, latest)) {
|
|
327
|
-
console.warn(
|
|
328
|
-
`\x1b[33m\u26A0 Warning: publishing ${result.version} which is older than latest (${latest})\x1b[0m`,
|
|
329
|
-
);
|
|
330
|
-
console.warn(
|
|
331
|
-
` This will move the "latest" tag from ${latest} to ${result.version}.`,
|
|
332
|
-
);
|
|
333
|
-
console.warn(
|
|
334
|
-
" Use --tag <name> to publish without affecting latest.",
|
|
335
|
-
);
|
|
336
|
-
throw new Error(
|
|
337
|
-
`Refusing to clobber latest tag. Use --tag <name> to publish ${result.version} alongside ${latest}.`,
|
|
338
|
-
);
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
} catch (e) {
|
|
342
|
-
if (e instanceof Error && e.message.includes("already exists")) throw e;
|
|
343
|
-
if (e instanceof Error && e.message.includes("Refusing to clobber"))
|
|
344
|
-
throw e;
|
|
345
|
-
// JSON parse error or other issue — continue with publish
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
const npmArgs = ["publish", result.packageDir];
|
|
350
|
-
npmArgs.push("--access", options.access || "public");
|
|
351
|
-
if (options.tag) npmArgs.push("--tag", options.tag);
|
|
352
|
-
if (options.dryRun) npmArgs.push("--dry-run");
|
|
353
|
-
if (options.registry) npmArgs.push("--registry", options.registry);
|
|
354
|
-
|
|
355
|
-
console.log(
|
|
356
|
-
`Publishing ${result.packageName}@${result.version} (hash: ${result.hash})`,
|
|
357
|
-
);
|
|
358
|
-
const proc = spawnSync("npm", npmArgs, {
|
|
359
|
-
stdio: ["pipe", "pipe", "pipe"],
|
|
360
|
-
encoding: "utf-8",
|
|
361
|
-
});
|
|
362
|
-
|
|
363
|
-
if (proc.status !== 0) {
|
|
364
|
-
const stderr = proc.stderr || "";
|
|
365
|
-
// Extract npm log path for debug hint
|
|
366
|
-
const logMatch = stderr.match(/complete log[^:]*:\s*(.+\.log)/i);
|
|
367
|
-
const logHint = logMatch
|
|
368
|
-
? `\n Debug: cat ${logMatch[1].trim()} | tail -40`
|
|
369
|
-
: "";
|
|
370
|
-
// Parse npm error for better messages
|
|
371
|
-
if (stderr.includes("E409") || stderr.includes("already present")) {
|
|
372
|
-
throw new Error(
|
|
373
|
-
`\x1b[31m\u2717 ${result.packageName}@${result.version} already exists in registry\x1b[0m\n\n` +
|
|
374
|
-
` Hint: bump the version in agent.json, or use --tag to publish a pre-release${logHint}`,
|
|
375
|
-
);
|
|
376
|
-
}
|
|
377
|
-
if (stderr.includes("E401") || stderr.includes("authentication")) {
|
|
378
|
-
throw new Error(
|
|
379
|
-
`\x1b[31m\u2717 Authentication failed\x1b[0m\n\n Run: npm login --scope=@agentdef\n Or set NPM_TOKEN in your environment${logHint}`,
|
|
380
|
-
);
|
|
381
|
-
}
|
|
382
|
-
if (stderr.includes("E403") || stderr.includes("Forbidden")) {
|
|
383
|
-
throw new Error(
|
|
384
|
-
`\x1b[31m\u2717 Permission denied publishing ${result.packageName}\x1b[0m\n\n Make sure you have publish access to the @agentdef scope.\n Run: npm access ls-packages @agentdef${logHint}`,
|
|
385
|
-
);
|
|
386
|
-
}
|
|
387
|
-
// Fallback: show raw error
|
|
388
|
-
throw new Error(
|
|
389
|
-
`npm publish failed (exit ${proc.status}):${logHint}\n${stderr}`,
|
|
390
|
-
);
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
return result;
|
|
394
|
-
}
|