@k2wanko/gemini-cli-sdk 0.4.0 → 0.5.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/dist/agent.js +1 -0
- package/dist/codemode.d.ts +24 -0
- package/dist/codemode.js +163 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +4 -4
package/dist/agent.js
CHANGED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { type ToolDef } from "./tool.js";
|
|
3
|
+
export interface ExecuteResult {
|
|
4
|
+
result: unknown;
|
|
5
|
+
error?: string;
|
|
6
|
+
logs?: string[];
|
|
7
|
+
}
|
|
8
|
+
export interface Executor {
|
|
9
|
+
execute(code: string, fns: Record<string, (...args: unknown[]) => Promise<unknown>>): Promise<ExecuteResult>;
|
|
10
|
+
}
|
|
11
|
+
export declare class NodeVMExecutor implements Executor {
|
|
12
|
+
execute(code: string, fns: Record<string, (...args: unknown[]) => Promise<unknown>>): Promise<ExecuteResult>;
|
|
13
|
+
}
|
|
14
|
+
export declare function createCodeModeTool(options: {
|
|
15
|
+
tools: ToolDef<any>[];
|
|
16
|
+
executor?: Executor;
|
|
17
|
+
description?: string;
|
|
18
|
+
}): ToolDef<z.ZodObject<{
|
|
19
|
+
code: z.ZodString;
|
|
20
|
+
}, "strip", z.ZodTypeAny, {
|
|
21
|
+
code: string;
|
|
22
|
+
}, {
|
|
23
|
+
code: string;
|
|
24
|
+
}>>;
|
package/dist/codemode.js
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { debugLogger } from "@google/gemini-cli-core";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
4
|
+
import { defineTool } from "./tool.js";
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
// JSON Schema -> TypeScript type string conversion
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
function jsonSchemaToTs(schema, depth = 0) {
|
|
9
|
+
if (schema.enum) {
|
|
10
|
+
return schema.enum.map((v) => JSON.stringify(v)).join(" | ");
|
|
11
|
+
}
|
|
12
|
+
if (schema.anyOf ?? schema.oneOf) {
|
|
13
|
+
const variants = (schema.anyOf ?? schema.oneOf ?? []).map((s) => jsonSchemaToTs(s, depth));
|
|
14
|
+
return variants.join(" | ");
|
|
15
|
+
}
|
|
16
|
+
const types = Array.isArray(schema.type)
|
|
17
|
+
? schema.type
|
|
18
|
+
: schema.type
|
|
19
|
+
? [schema.type]
|
|
20
|
+
: [];
|
|
21
|
+
if (types.includes("string"))
|
|
22
|
+
return "string";
|
|
23
|
+
if (types.includes("number") || types.includes("integer"))
|
|
24
|
+
return "number";
|
|
25
|
+
if (types.includes("boolean"))
|
|
26
|
+
return "boolean";
|
|
27
|
+
if (types.includes("null"))
|
|
28
|
+
return "null";
|
|
29
|
+
if (types.includes("array") || schema.items) {
|
|
30
|
+
const items = schema.items
|
|
31
|
+
? jsonSchemaToTs(schema.items, depth)
|
|
32
|
+
: "unknown";
|
|
33
|
+
return `${items}[]`;
|
|
34
|
+
}
|
|
35
|
+
if (types.includes("object") || schema.properties) {
|
|
36
|
+
const props = schema.properties ?? {};
|
|
37
|
+
const required = new Set(schema.required ?? []);
|
|
38
|
+
const indent = " ".repeat(depth + 1);
|
|
39
|
+
const closing = " ".repeat(depth);
|
|
40
|
+
const entries = Object.entries(props).map(([key, val]) => {
|
|
41
|
+
const opt = required.has(key) ? "" : "?";
|
|
42
|
+
const comment = val.description ? `/** ${val.description} */ ` : "";
|
|
43
|
+
return `${indent}${comment}${key}${opt}: ${jsonSchemaToTs(val, depth + 1)}`;
|
|
44
|
+
});
|
|
45
|
+
if (entries.length === 0)
|
|
46
|
+
return "Record<string, unknown>";
|
|
47
|
+
return `{\n${entries.join(";\n")};\n${closing}}`;
|
|
48
|
+
}
|
|
49
|
+
return "unknown";
|
|
50
|
+
}
|
|
51
|
+
// ---------------------------------------------------------------------------
|
|
52
|
+
// API docs generation from ToolDef array (JSDoc-style, for LLM consumption)
|
|
53
|
+
// ---------------------------------------------------------------------------
|
|
54
|
+
function jsonSchemaToParams(schema, prefix = "") {
|
|
55
|
+
const props = schema.properties ?? {};
|
|
56
|
+
const required = new Set(schema.required ?? []);
|
|
57
|
+
return Object.entries(props).map(([key, val]) => {
|
|
58
|
+
const opt = required.has(key) ? "" : "?";
|
|
59
|
+
const type = jsonSchemaToTs(val);
|
|
60
|
+
const desc = val.description ? ` — ${val.description}` : "";
|
|
61
|
+
return `// ${prefix}${key}${opt}: ${type}${desc}`;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
function generateApiDocs(tools) {
|
|
65
|
+
debugLogger.debug?.("[codemode] generateApiDocs: %d tools", tools.length);
|
|
66
|
+
const sections = tools.map((tool) => {
|
|
67
|
+
const schema = zodToJsonSchema(tool.inputSchema);
|
|
68
|
+
const params = jsonSchemaToParams(schema);
|
|
69
|
+
const desc = tool.description ? ` — ${tool.description}` : "";
|
|
70
|
+
const lines = [`// codemode.${tool.name}(input)${desc}`, ...params];
|
|
71
|
+
return lines.join("\n");
|
|
72
|
+
});
|
|
73
|
+
return sections.join("\n//\n");
|
|
74
|
+
}
|
|
75
|
+
// ---------------------------------------------------------------------------
|
|
76
|
+
// NodeVMExecutor
|
|
77
|
+
// ---------------------------------------------------------------------------
|
|
78
|
+
export class NodeVMExecutor {
|
|
79
|
+
async execute(code, fns) {
|
|
80
|
+
debugLogger.debug?.("[codemode:executor] execute called, code length=%d, available fns=%s", code.length, Object.keys(fns).join(", "));
|
|
81
|
+
debugLogger.debug?.("[codemode:executor] code:\n%s", code);
|
|
82
|
+
const logs = [];
|
|
83
|
+
const stringify = (v) => typeof v === "object" && v !== null ? JSON.stringify(v) : String(v);
|
|
84
|
+
const captureConsole = {
|
|
85
|
+
log: (...args) => logs.push(args.map(stringify).join(" ")),
|
|
86
|
+
warn: (...args) => logs.push(`[warn] ${args.map(stringify).join(" ")}`),
|
|
87
|
+
error: (...args) => logs.push(`[error] ${args.map(stringify).join(" ")}`),
|
|
88
|
+
};
|
|
89
|
+
try {
|
|
90
|
+
const AsyncFunction = Object.getPrototypeOf(async () => { })
|
|
91
|
+
.constructor;
|
|
92
|
+
const fn = new AsyncFunction("codemode", "console", code);
|
|
93
|
+
debugLogger.debug?.("[codemode:executor] starting execution");
|
|
94
|
+
const result = await fn(fns, captureConsole);
|
|
95
|
+
debugLogger.debug?.("[codemode:executor] execution completed, result=%s, logs=%d", typeof result, logs.length);
|
|
96
|
+
return {
|
|
97
|
+
result,
|
|
98
|
+
logs: logs.length > 0 ? logs : undefined,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
debugLogger.debug?.("[codemode:executor] execution failed: %s", error instanceof Error ? error.message : String(error));
|
|
103
|
+
return {
|
|
104
|
+
result: undefined,
|
|
105
|
+
error: error instanceof Error ? error.message : String(error),
|
|
106
|
+
logs: logs.length > 0 ? logs : undefined,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
// ---------------------------------------------------------------------------
|
|
112
|
+
// createCodeModeTool
|
|
113
|
+
// ---------------------------------------------------------------------------
|
|
114
|
+
export function createCodeModeTool(options) {
|
|
115
|
+
const executor = options.executor ?? new NodeVMExecutor();
|
|
116
|
+
const apiDocs = generateApiDocs(options.tools);
|
|
117
|
+
const description = options.description ??
|
|
118
|
+
`Write and execute plain JavaScript (NOT TypeScript) to orchestrate multiple tools.
|
|
119
|
+
|
|
120
|
+
The \`codemode\` object exposes all available tools as async functions.
|
|
121
|
+
Always end your code with a \`return\` statement to return the final result.
|
|
122
|
+
|
|
123
|
+
Available tools:
|
|
124
|
+
\`\`\`javascript
|
|
125
|
+
${apiDocs}
|
|
126
|
+
\`\`\`
|
|
127
|
+
|
|
128
|
+
Example:
|
|
129
|
+
\`\`\`javascript
|
|
130
|
+
const result = await codemode.toolName({ param: "value" });
|
|
131
|
+
return result;
|
|
132
|
+
\`\`\``;
|
|
133
|
+
debugLogger.debug?.("[codemode] createCodeModeTool: registering %d tools", options.tools.length);
|
|
134
|
+
return defineTool({
|
|
135
|
+
name: "code",
|
|
136
|
+
description,
|
|
137
|
+
inputSchema: z.object({
|
|
138
|
+
code: z
|
|
139
|
+
.string()
|
|
140
|
+
.describe("JavaScript code to execute. Must use `return` to return the final result."),
|
|
141
|
+
}),
|
|
142
|
+
sendErrorsToModel: true,
|
|
143
|
+
}, async ({ code }, context) => {
|
|
144
|
+
debugLogger.debug?.("[codemode] tool invoked, code length=%d", code.length);
|
|
145
|
+
const fns = {};
|
|
146
|
+
for (const tool of options.tools) {
|
|
147
|
+
fns[tool.name] = (input) => {
|
|
148
|
+
debugLogger.debug?.("[codemode] calling tool '%s' with input: %s", tool.name, JSON.stringify(input));
|
|
149
|
+
return tool.action(input, context);
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
const execResult = await executor.execute(code, fns);
|
|
153
|
+
const output = {};
|
|
154
|
+
if (execResult.result !== undefined)
|
|
155
|
+
output.result = execResult.result;
|
|
156
|
+
if (execResult.error)
|
|
157
|
+
output.error = execResult.error;
|
|
158
|
+
if (execResult.logs && execResult.logs.length > 0)
|
|
159
|
+
output.logs = execResult.logs;
|
|
160
|
+
debugLogger.debug?.("[codemode] tool output: %s", JSON.stringify(output));
|
|
161
|
+
return output;
|
|
162
|
+
});
|
|
163
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@k2wanko/gemini-cli-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "A lightweight SDK for building non-interactive AI agents powered by Google Gemini",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"private": false,
|
|
@@ -38,13 +38,13 @@
|
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
40
40
|
"build": "tsc",
|
|
41
|
-
"lint": "biome check && tsc --noEmit",
|
|
42
|
-
"fix": "biome check --write . && tsc --noEmit",
|
|
41
|
+
"lint": "biome check && tsc --noEmit && tsc --project tsconfig.examples.json",
|
|
42
|
+
"fix": "biome check --write . && tsc --noEmit && tsc --project tsconfig.examples.json",
|
|
43
43
|
"test": "bun test"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@a2a-js/sdk": "^0.3.10",
|
|
47
|
-
"@google/gemini-cli-core": "
|
|
47
|
+
"@google/gemini-cli-core": "nightly",
|
|
48
48
|
"zod": "^3.23.8",
|
|
49
49
|
"zod-to-json-schema": "^3.23.1"
|
|
50
50
|
},
|