@goondan/openharness-base 0.1.8 → 0.1.9

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.
@@ -1,83 +0,0 @@
1
- import { readFile, writeFile, readdir } from "node:fs/promises";
2
- import type { ToolDefinition, JsonObject, ToolContext } from "@goondan/openharness-types";
3
-
4
- export function FileReadTool(): ToolDefinition {
5
- return {
6
- name: "file_read",
7
- description: "Read the contents of a file.",
8
- parameters: {
9
- type: "object",
10
- properties: {
11
- path: { type: "string", description: "Absolute or relative path to the file to read." },
12
- encoding: {
13
- type: "string",
14
- enum: ["utf8", "base64"],
15
- description: "File encoding. Defaults to utf8.",
16
- },
17
- },
18
- required: ["path"],
19
- },
20
- async handler(args: JsonObject, _ctx: ToolContext) {
21
- const filePath = args["path"] as string;
22
- const encoding = (args["encoding"] as BufferEncoding | undefined) ?? "utf8";
23
- try {
24
- const content = await readFile(filePath, { encoding });
25
- return { type: "text", text: content };
26
- } catch (err) {
27
- return { type: "error", error: (err as Error).message };
28
- }
29
- },
30
- };
31
- }
32
-
33
- export function FileWriteTool(): ToolDefinition {
34
- return {
35
- name: "file_write",
36
- description: "Write content to a file, creating or overwriting it.",
37
- parameters: {
38
- type: "object",
39
- properties: {
40
- path: { type: "string", description: "Absolute or relative path to the file to write." },
41
- content: { type: "string", description: "The content to write to the file." },
42
- },
43
- required: ["path", "content"],
44
- },
45
- async handler(args: JsonObject, _ctx: ToolContext) {
46
- const filePath = args["path"] as string;
47
- const content = args["content"] as string;
48
- try {
49
- await writeFile(filePath, content, "utf8");
50
- return { type: "text", text: `File written: ${filePath}` };
51
- } catch (err) {
52
- return { type: "error", error: (err as Error).message };
53
- }
54
- },
55
- };
56
- }
57
-
58
- export function FileListTool(): ToolDefinition {
59
- return {
60
- name: "file_list",
61
- description: "List files and directories in a given directory.",
62
- parameters: {
63
- type: "object",
64
- properties: {
65
- path: { type: "string", description: "Absolute or relative path to the directory to list." },
66
- },
67
- required: ["path"],
68
- },
69
- async handler(args: JsonObject, _ctx: ToolContext) {
70
- const dirPath = args["path"] as string;
71
- try {
72
- const entries = await readdir(dirPath, { withFileTypes: true });
73
- const result = entries.map((e) => ({
74
- name: e.name,
75
- type: e.isDirectory() ? "directory" : "file",
76
- }));
77
- return { type: "json", data: result };
78
- } catch (err) {
79
- return { type: "error", error: (err as Error).message };
80
- }
81
- },
82
- };
83
- }
@@ -1,64 +0,0 @@
1
- import type { ToolDefinition, JsonObject, ToolContext, JsonValue } from "@goondan/openharness-types";
2
-
3
- export function HttpFetchTool(): ToolDefinition {
4
- return {
5
- name: "http_fetch",
6
- description: "Perform an HTTP request and return the response.",
7
- parameters: {
8
- type: "object",
9
- properties: {
10
- url: { type: "string", description: "The URL to fetch." },
11
- method: {
12
- type: "string",
13
- enum: ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD"],
14
- description: "HTTP method. Defaults to GET.",
15
- },
16
- headers: {
17
- type: "object",
18
- additionalProperties: { type: "string" },
19
- description: "Optional request headers.",
20
- },
21
- body: { type: "string", description: "Optional request body." },
22
- },
23
- required: ["url"],
24
- },
25
- async handler(args: JsonObject, _ctx: ToolContext) {
26
- const url = args["url"] as string;
27
- const method = (args["method"] as string | undefined) ?? "GET";
28
- const headers = (args["headers"] as Record<string, string> | undefined) ?? {};
29
- const body = args["body"] as string | undefined;
30
-
31
- try {
32
- const response = await fetch(url, {
33
- method,
34
- headers,
35
- body: body !== undefined ? body : undefined,
36
- });
37
-
38
- const responseHeaders: Record<string, string> = {};
39
- response.headers.forEach((value, key) => {
40
- responseHeaders[key] = value;
41
- });
42
-
43
- let responseBody: unknown;
44
- const contentType = response.headers.get("content-type") ?? "";
45
- if (contentType.includes("application/json")) {
46
- responseBody = await response.json();
47
- } else {
48
- responseBody = await response.text();
49
- }
50
-
51
- return {
52
- type: "json",
53
- data: {
54
- status: response.status,
55
- headers: responseHeaders,
56
- body: responseBody as JsonValue,
57
- },
58
- };
59
- } catch (err) {
60
- return { type: "error", error: (err as Error).message };
61
- }
62
- },
63
- };
64
- }
@@ -1,71 +0,0 @@
1
- import type { ToolDefinition, JsonObject, ToolContext, JsonValue } from "@goondan/openharness-types";
2
-
3
- /**
4
- * Simple JSONPath-like query supporting dot and bracket notation.
5
- * Supported path format: $.key.nested[0].field
6
- * The leading `$` is optional.
7
- */
8
- function jsonQuery(data: unknown, path: string): unknown {
9
- // Normalize: strip leading `$` or `$.`
10
- let normalized = path.trim();
11
- if (normalized.startsWith("$.")) {
12
- normalized = normalized.slice(2);
13
- } else if (normalized === "$") {
14
- return data;
15
- } else if (normalized.startsWith("$")) {
16
- normalized = normalized.slice(1);
17
- }
18
-
19
- if (normalized === "" || normalized === ".") {
20
- return data;
21
- }
22
-
23
- // Tokenize path into segments
24
- const segments: Array<string | number> = [];
25
- // Replace bracket notation [0] with .0
26
- const normalized2 = normalized.replace(/\[(\d+)\]/g, ".$1").replace(/\[['"](.+?)['"]\]/g, ".$1");
27
- for (const part of normalized2.split(".")) {
28
- if (part === "") continue;
29
- const num = Number(part);
30
- segments.push(Number.isInteger(num) && String(num) === part ? num : part);
31
- }
32
-
33
- let current: unknown = data;
34
- for (const segment of segments) {
35
- if (current === null || current === undefined) return undefined;
36
- if (typeof current === "object") {
37
- current = (current as Record<string | number, unknown>)[segment];
38
- } else {
39
- return undefined;
40
- }
41
- }
42
- return current;
43
- }
44
-
45
- export function JsonQueryTool(): ToolDefinition {
46
- return {
47
- name: "json_query",
48
- description: "Query JSON data using a simple JSONPath-like path expression.",
49
- parameters: {
50
- type: "object",
51
- properties: {
52
- data: { description: "The JSON data to query." },
53
- path: {
54
- type: "string",
55
- description: "JSONPath-like path, e.g. $.key.nested[0].field",
56
- },
57
- },
58
- required: ["data", "path"],
59
- },
60
- async handler(args: JsonObject, _ctx: ToolContext) {
61
- const data = args["data"];
62
- const path = args["path"] as string;
63
- try {
64
- const result = jsonQuery(data, path);
65
- return { type: "json", data: result as JsonValue };
66
- } catch (err) {
67
- return { type: "error", error: (err as Error).message };
68
- }
69
- },
70
- };
71
- }
@@ -1,59 +0,0 @@
1
- import type { ToolDefinition, JsonObject, ToolContext } from "@goondan/openharness-types";
2
-
3
- type Operation = "uppercase" | "lowercase" | "trim" | "split" | "replace";
4
-
5
- export function TextTransformTool(): ToolDefinition {
6
- return {
7
- name: "text_transform",
8
- description: "Apply a transformation operation to a text string.",
9
- parameters: {
10
- type: "object",
11
- properties: {
12
- text: { type: "string", description: "The input text to transform." },
13
- operation: {
14
- type: "string",
15
- enum: ["uppercase", "lowercase", "trim", "split", "replace"],
16
- description: "The transformation to apply.",
17
- },
18
- options: {
19
- type: "object",
20
- properties: {
21
- delimiter: { type: "string", description: "Delimiter for split operation." },
22
- find: { type: "string", description: "String to find for replace operation." },
23
- replacement: { type: "string", description: "Replacement string for replace operation." },
24
- },
25
- },
26
- },
27
- required: ["text", "operation"],
28
- },
29
- async handler(args: JsonObject, _ctx: ToolContext) {
30
- const text = args["text"] as string;
31
- const operation = args["operation"] as Operation;
32
- const options = (args["options"] as Record<string, string> | undefined) ?? {};
33
-
34
- try {
35
- switch (operation) {
36
- case "uppercase":
37
- return { type: "text", text: text.toUpperCase() };
38
- case "lowercase":
39
- return { type: "text", text: text.toLowerCase() };
40
- case "trim":
41
- return { type: "text", text: text.trim() };
42
- case "split": {
43
- const delimiter = options["delimiter"] ?? " ";
44
- return { type: "json", data: text.split(delimiter) };
45
- }
46
- case "replace": {
47
- const find = options["find"] ?? "";
48
- const replacement = options["replacement"] ?? "";
49
- return { type: "text", text: text.split(find).join(replacement) };
50
- }
51
- default:
52
- return { type: "error", error: `Unknown operation: ${operation}` };
53
- }
54
- } catch (err) {
55
- return { type: "error", error: (err as Error).message };
56
- }
57
- },
58
- };
59
- }
package/src/tools/wait.ts DELETED
@@ -1,46 +0,0 @@
1
- import type { ToolDefinition, JsonObject, ToolContext } from "@goondan/openharness-types";
2
-
3
- export interface WaitToolConfig {
4
- maxMs?: number;
5
- }
6
-
7
- export function WaitTool(config: WaitToolConfig = {}): ToolDefinition {
8
- const { maxMs = 60_000 } = config;
9
-
10
- return {
11
- name: "wait",
12
- description: "Wait for a specified number of milliseconds before continuing.",
13
- parameters: {
14
- type: "object",
15
- properties: {
16
- ms: {
17
- type: "number",
18
- description: "Number of milliseconds to wait.",
19
- minimum: 0,
20
- },
21
- },
22
- required: ["ms"],
23
- },
24
- async handler(args: JsonObject, ctx: ToolContext) {
25
- const requestedMs = args["ms"] as number;
26
- const ms = Math.min(requestedMs, maxMs);
27
-
28
- await new Promise<void>((resolve, reject) => {
29
- const timer = setTimeout(resolve, ms);
30
-
31
- if (ctx.abortSignal.aborted) {
32
- clearTimeout(timer);
33
- reject(new Error("Aborted"));
34
- return;
35
- }
36
-
37
- ctx.abortSignal.addEventListener("abort", () => {
38
- clearTimeout(timer);
39
- reject(new Error("Aborted"));
40
- });
41
- });
42
-
43
- return { type: "text", text: `Waited ${ms}ms` };
44
- },
45
- };
46
- }
package/tsconfig.json DELETED
@@ -1,8 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.base.json",
3
- "compilerOptions": {
4
- "outDir": "dist",
5
- "rootDir": "src"
6
- },
7
- "include": ["src"]
8
- }
package/vitest.config.ts DELETED
@@ -1,7 +0,0 @@
1
- import { defineConfig } from "vitest/config";
2
-
3
- export default defineConfig({
4
- test: {
5
- include: ["src/**/*.test.ts"],
6
- },
7
- });