@raseef/openclaw-zoho-books 0.0.0-stage → 1.0.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 +51 -2
- package/dist/client.d.ts +29 -0
- package/dist/client.js +112 -0
- package/dist/config.d.ts +32 -0
- package/dist/config.js +31 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +230 -0
- package/openclaw.plugin.json +222 -0
- package/package.json +37 -4
package/README.md
CHANGED
|
@@ -1,3 +1,52 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Zoho Books
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
OpenClaw tools for Zoho Books items (products and services) in one organization: search, read, create and update them. The Zoho sign-in (client id, client secret, refresh token) stays in OpenClaw's secret store; no shell or scripts are needed.
|
|
4
|
+
|
|
5
|
+
| Tool | What it does |
|
|
6
|
+
| --- | --- |
|
|
7
|
+
| `zoho_books_items_search` | Items by name, SKU or description text (active by default), in short form: id, name, SKU, prices, tax, stock, vendor. |
|
|
8
|
+
| `zoho_books_item_get` | One item with all its fields. |
|
|
9
|
+
| `zoho_books_item_create` | A new item. `name` and `rate` are required; goods default to an inventory item. Refused when the SKU is already used (the existing item is returned). |
|
|
10
|
+
| `zoho_books_item_update` | Changes only the fields given (the current name and rate are kept, as Zoho requires them), and/or marks the item active or inactive. Refused when the new SKU belongs to another item. |
|
|
11
|
+
| `zoho_books_taxes` | The organization's taxes, for `tax_id`. |
|
|
12
|
+
| `zoho_books_vendors_search` | Vendors by name, for `vendor_id`. |
|
|
13
|
+
| `zoho_books_accounts` | Active accounts by type (income, expense, cost of goods sold, stock), for the item's accounts. |
|
|
14
|
+
|
|
15
|
+
## Adding an item
|
|
16
|
+
|
|
17
|
+
1. `zoho_books_items_search` with the name and with the SKU, to avoid duplicates.
|
|
18
|
+
2. `zoho_books_taxes` for the VAT `tax_id`, and `zoho_books_vendors_search` for the supplier.
|
|
19
|
+
3. `zoho_books_item_create` with name, SKU, selling price (`rate`), purchase price (`purchase_rate`), `tax_id` and `vendor_id`.
|
|
20
|
+
|
|
21
|
+
## Safety
|
|
22
|
+
|
|
23
|
+
- The refresh token goes only to the Zoho accounts server of the configured data center, the access token only to its Zoho Books API; redirects are refused.
|
|
24
|
+
- Secrets never appear in errors or results.
|
|
25
|
+
- Ids are checked (digits only) before they go into a request path.
|
|
26
|
+
- Every create and update is recorded in OpenClaw's state database: the item id and which fields changed, never their values.
|
|
27
|
+
- The tools cannot delete anything and touch items only.
|
|
28
|
+
|
|
29
|
+
## Install and configure
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
openclaw plugins install npm:@raseef/openclaw-zoho-books
|
|
33
|
+
openclaw config set plugins.entries.zoho-books.config --strict-json '{
|
|
34
|
+
"clientId": { "source": "store", "provider": "default", "id": "ZOHO_CLIENT_ID" },
|
|
35
|
+
"clientSecret": { "source": "store", "provider": "default", "id": "ZOHO_CLIENT_SECRET" },
|
|
36
|
+
"refreshToken": { "source": "store", "provider": "default", "id": "ZOHO_REFRESH_TOKEN" },
|
|
37
|
+
"organizationId": "<organization number>"
|
|
38
|
+
}'
|
|
39
|
+
openclaw plugins enable zoho-books
|
|
40
|
+
openclaw secrets audit --check
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Add `zoho-books` to `plugins.allow` and the `zoho_books_*` tools to the agent's `tools.allow`.
|
|
44
|
+
|
|
45
|
+
| Key | Required | Default |
|
|
46
|
+
| --- | --- | --- |
|
|
47
|
+
| `clientId`, `clientSecret`, `refreshToken` | yes (secret references) | |
|
|
48
|
+
| `organizationId` | yes | |
|
|
49
|
+
| `dataCenter` | no: `eu`, `com`, `in`, `sa` | `eu` |
|
|
50
|
+
| `requestTimeoutMs` | no | `30000` |
|
|
51
|
+
|
|
52
|
+
The refresh token needs Zoho Books access to items, taxes, contacts and the chart of accounts (for example `ZohoBooks.fullaccess.all`).
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { ZohoBooksConfig } from "./config.js";
|
|
2
|
+
export declare class ZohoBooksError extends Error {
|
|
3
|
+
readonly status: number;
|
|
4
|
+
constructor(message: string, status: number);
|
|
5
|
+
}
|
|
6
|
+
/** A Zoho record id (all digits), safe to place in a path. */
|
|
7
|
+
export declare function zohoId(value: unknown, label?: string): string;
|
|
8
|
+
/** Query string from defined values only. */
|
|
9
|
+
export declare function query(params: Record<string, unknown>): string;
|
|
10
|
+
type Fetch = typeof fetch;
|
|
11
|
+
/**
|
|
12
|
+
* Thin client for the Zoho Books API. The refresh token goes only to the configured Zoho accounts server, the access
|
|
13
|
+
* token only to the configured Zoho Books API; redirects are refused, and no secret ever appears in an error.
|
|
14
|
+
*/
|
|
15
|
+
export declare class ZohoBooksClient {
|
|
16
|
+
private readonly config;
|
|
17
|
+
private readonly fetchImpl;
|
|
18
|
+
private readonly now;
|
|
19
|
+
private access;
|
|
20
|
+
private refreshing;
|
|
21
|
+
constructor(config: ZohoBooksConfig, fetchImpl?: Fetch, now?: () => number);
|
|
22
|
+
private redact;
|
|
23
|
+
private send;
|
|
24
|
+
/** A current access token, refreshed with the refresh token when missing or about to expire (one refresh at a time). */
|
|
25
|
+
private token;
|
|
26
|
+
/** One Zoho Books call in the configured organization; a rejected access token is refreshed once. */
|
|
27
|
+
request(method: "GET" | "POST" | "PUT", path: string, params?: Record<string, unknown>, body?: unknown, signal?: AbortSignal): Promise<any>;
|
|
28
|
+
}
|
|
29
|
+
export {};
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
export class ZohoBooksError extends Error {
|
|
2
|
+
status;
|
|
3
|
+
constructor(message, status) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.status = status;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
const ID = /^\d{1,30}$/;
|
|
9
|
+
/** A Zoho record id (all digits), safe to place in a path. */
|
|
10
|
+
export function zohoId(value, label = "id") {
|
|
11
|
+
const id = String(value ?? "").trim();
|
|
12
|
+
if (!ID.test(id))
|
|
13
|
+
throw new Error(`${label} must be a Zoho id (digits only)`);
|
|
14
|
+
return id;
|
|
15
|
+
}
|
|
16
|
+
/** Query string from defined values only. */
|
|
17
|
+
export function query(params) {
|
|
18
|
+
const search = new URLSearchParams();
|
|
19
|
+
for (const [key, value] of Object.entries(params)) {
|
|
20
|
+
if (value !== undefined && value !== null && value !== "")
|
|
21
|
+
search.set(key, String(value));
|
|
22
|
+
}
|
|
23
|
+
return search.toString();
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Thin client for the Zoho Books API. The refresh token goes only to the configured Zoho accounts server, the access
|
|
27
|
+
* token only to the configured Zoho Books API; redirects are refused, and no secret ever appears in an error.
|
|
28
|
+
*/
|
|
29
|
+
export class ZohoBooksClient {
|
|
30
|
+
config;
|
|
31
|
+
fetchImpl;
|
|
32
|
+
now;
|
|
33
|
+
access;
|
|
34
|
+
refreshing;
|
|
35
|
+
constructor(config, fetchImpl = fetch, now = Date.now) {
|
|
36
|
+
this.config = config;
|
|
37
|
+
this.fetchImpl = fetchImpl;
|
|
38
|
+
this.now = now;
|
|
39
|
+
}
|
|
40
|
+
redact(text) {
|
|
41
|
+
let out = text;
|
|
42
|
+
for (const s of [this.config.clientSecret, this.config.refreshToken, this.config.clientId, this.access?.token])
|
|
43
|
+
if (s)
|
|
44
|
+
out = out.split(s).join("[hidden]");
|
|
45
|
+
return out.replace(/1000\.[A-Za-z0-9._-]{8,}/g, "[hidden]");
|
|
46
|
+
}
|
|
47
|
+
async send(url, init, signal) {
|
|
48
|
+
const limit = AbortSignal.timeout(this.config.requestTimeoutMs);
|
|
49
|
+
const response = await this.fetchImpl(url, { ...init, redirect: "error", signal: signal ? AbortSignal.any([signal, limit]) : limit });
|
|
50
|
+
const text = await response.text();
|
|
51
|
+
let data = null;
|
|
52
|
+
if (text) {
|
|
53
|
+
try {
|
|
54
|
+
data = JSON.parse(text);
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
data = text;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return { status: response.status, ok: response.ok, data };
|
|
61
|
+
}
|
|
62
|
+
/** A current access token, refreshed with the refresh token when missing or about to expire (one refresh at a time). */
|
|
63
|
+
async token(signal, force = false) {
|
|
64
|
+
if (!force && this.access && this.access.until > this.now() + 60_000)
|
|
65
|
+
return this.access.token;
|
|
66
|
+
this.refreshing ??= (async () => {
|
|
67
|
+
const body = new URLSearchParams({
|
|
68
|
+
refresh_token: this.config.refreshToken,
|
|
69
|
+
client_id: this.config.clientId,
|
|
70
|
+
client_secret: this.config.clientSecret,
|
|
71
|
+
grant_type: "refresh_token",
|
|
72
|
+
});
|
|
73
|
+
const r = await this.send(`${this.config.accountsUrl}/oauth/v2/token`, { method: "POST", headers: { "content-type": "application/x-www-form-urlencoded", accept: "application/json" }, body }, signal);
|
|
74
|
+
const token = typeof r.data?.access_token === "string" ? r.data.access_token : "";
|
|
75
|
+
if (!r.ok || !token) {
|
|
76
|
+
const reason = typeof r.data?.error === "string" ? r.data.error : `HTTP ${r.status}`;
|
|
77
|
+
throw new ZohoBooksError(`Zoho sign-in failed (${this.redact(reason).slice(0, 120)}). The refresh token may be revoked or belong to another data center.`, r.status || 401);
|
|
78
|
+
}
|
|
79
|
+
const seconds = Number(r.data?.expires_in);
|
|
80
|
+
this.access = { token, until: this.now() + (Number.isFinite(seconds) && seconds > 0 ? seconds : 3600) * 1000 };
|
|
81
|
+
return token;
|
|
82
|
+
})().finally(() => {
|
|
83
|
+
this.refreshing = undefined;
|
|
84
|
+
});
|
|
85
|
+
return this.refreshing;
|
|
86
|
+
}
|
|
87
|
+
/** One Zoho Books call in the configured organization; a rejected access token is refreshed once. */
|
|
88
|
+
async request(method, path, params = {}, body, signal) {
|
|
89
|
+
const url = `${this.config.apiUrl}${path}?${query({ ...params, organization_id: this.config.organizationId })}`;
|
|
90
|
+
for (let attempt = 0;; attempt++) {
|
|
91
|
+
const token = await this.token(signal, attempt > 0);
|
|
92
|
+
const r = await this.send(url, {
|
|
93
|
+
method,
|
|
94
|
+
headers: {
|
|
95
|
+
authorization: `Zoho-oauthtoken ${token}`,
|
|
96
|
+
accept: "application/json",
|
|
97
|
+
...(body === undefined ? {} : { "content-type": "application/json" }),
|
|
98
|
+
},
|
|
99
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
100
|
+
}, signal);
|
|
101
|
+
if (r.status === 401 && attempt === 0)
|
|
102
|
+
continue;
|
|
103
|
+
// Zoho answers {code: 0, message, ...} on success and a non-zero code with a message otherwise.
|
|
104
|
+
const code = typeof r.data?.code === "number" ? r.data.code : r.ok ? 0 : -1;
|
|
105
|
+
if (!r.ok || code !== 0) {
|
|
106
|
+
const message = typeof r.data?.message === "string" ? r.data.message : typeof r.data === "string" ? r.data : "";
|
|
107
|
+
throw new ZohoBooksError(`Zoho Books ${method} ${path} failed: HTTP ${r.status}${code > 0 ? `, code ${code}` : ""}${message ? `: ${this.redact(message).slice(0, 300)}` : ""}`, r.status);
|
|
108
|
+
}
|
|
109
|
+
return r.data;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export type ZohoBooksConfig = {
|
|
2
|
+
clientId: string;
|
|
3
|
+
clientSecret: string;
|
|
4
|
+
refreshToken: string;
|
|
5
|
+
/** Zoho Books organization the tools work in. */
|
|
6
|
+
organizationId: string;
|
|
7
|
+
/** Zoho accounts server for the refresh token, always https and without a trailing slash. */
|
|
8
|
+
accountsUrl: string;
|
|
9
|
+
/** Zoho Books API root, always https and without a trailing slash. */
|
|
10
|
+
apiUrl: string;
|
|
11
|
+
requestTimeoutMs: number;
|
|
12
|
+
};
|
|
13
|
+
/** Zoho data centers: the refresh token only works on the accounts server of the account's own data center. */
|
|
14
|
+
export declare const DATA_CENTERS: {
|
|
15
|
+
readonly eu: {
|
|
16
|
+
readonly accountsUrl: "https://accounts.zoho.eu";
|
|
17
|
+
readonly apiUrl: "https://www.zohoapis.eu/books/v3";
|
|
18
|
+
};
|
|
19
|
+
readonly com: {
|
|
20
|
+
readonly accountsUrl: "https://accounts.zoho.com";
|
|
21
|
+
readonly apiUrl: "https://www.zohoapis.com/books/v3";
|
|
22
|
+
};
|
|
23
|
+
readonly in: {
|
|
24
|
+
readonly accountsUrl: "https://accounts.zoho.in";
|
|
25
|
+
readonly apiUrl: "https://www.zohoapis.in/books/v3";
|
|
26
|
+
};
|
|
27
|
+
readonly sa: {
|
|
28
|
+
readonly accountsUrl: "https://accounts.zoho.sa";
|
|
29
|
+
readonly apiUrl: "https://www.zohoapis.sa/books/v3";
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
export declare function normalizeConfig(input?: Record<string, any>): ZohoBooksConfig;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** Zoho data centers: the refresh token only works on the accounts server of the account's own data center. */
|
|
2
|
+
export const DATA_CENTERS = {
|
|
3
|
+
eu: { accountsUrl: "https://accounts.zoho.eu", apiUrl: "https://www.zohoapis.eu/books/v3" },
|
|
4
|
+
com: { accountsUrl: "https://accounts.zoho.com", apiUrl: "https://www.zohoapis.com/books/v3" },
|
|
5
|
+
in: { accountsUrl: "https://accounts.zoho.in", apiUrl: "https://www.zohoapis.in/books/v3" },
|
|
6
|
+
sa: { accountsUrl: "https://accounts.zoho.sa", apiUrl: "https://www.zohoapis.sa/books/v3" },
|
|
7
|
+
};
|
|
8
|
+
const secret = (input, key) => {
|
|
9
|
+
// OpenClaw resolves SecretRefs before the plugin sees its config, so these are the values themselves.
|
|
10
|
+
const value = typeof input[key] === "string" ? input[key].trim() : "";
|
|
11
|
+
if (!value)
|
|
12
|
+
throw new Error(`zoho-books ${key} is not configured`);
|
|
13
|
+
return value;
|
|
14
|
+
};
|
|
15
|
+
export function normalizeConfig(input = {}) {
|
|
16
|
+
const clientId = secret(input, "clientId");
|
|
17
|
+
const clientSecret = secret(input, "clientSecret");
|
|
18
|
+
const refreshToken = secret(input, "refreshToken");
|
|
19
|
+
const organizationId = String(input.organizationId ?? "").trim();
|
|
20
|
+
if (!/^\d{1,30}$/.test(organizationId))
|
|
21
|
+
throw new Error("zoho-books organizationId must be the Zoho Books organization number");
|
|
22
|
+
const dataCenter = String(input.dataCenter ?? "eu").trim();
|
|
23
|
+
if (!(dataCenter in DATA_CENTERS))
|
|
24
|
+
throw new Error(`zoho-books dataCenter must be one of ${Object.keys(DATA_CENTERS).join(", ")}`);
|
|
25
|
+
const { accountsUrl, apiUrl } = DATA_CENTERS[dataCenter];
|
|
26
|
+
const timeout = input.requestTimeoutMs === undefined ? 30_000 : Number(input.requestTimeoutMs);
|
|
27
|
+
if (!Number.isInteger(timeout) || timeout < 1000 || timeout > 120_000) {
|
|
28
|
+
throw new Error("zoho-books requestTimeoutMs must be between 1000 and 120000");
|
|
29
|
+
}
|
|
30
|
+
return { clientId, clientSecret, refreshToken, organizationId, accountsUrl, apiUrl, requestTimeoutMs: timeout };
|
|
31
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ZohoBooksClient } from "./client.js";
|
|
2
|
+
import { type ZohoBooksConfig } from "./config.js";
|
|
3
|
+
/** One client (and access token) per resolved config. */
|
|
4
|
+
export declare function clientFor(rawConfig: unknown, fetchImpl?: typeof fetch): {
|
|
5
|
+
key: string;
|
|
6
|
+
config: ZohoBooksConfig;
|
|
7
|
+
client: ZohoBooksClient;
|
|
8
|
+
};
|
|
9
|
+
export declare function resetClientCache(): void;
|
|
10
|
+
/** The fields worth showing for an item in a list. */
|
|
11
|
+
export declare function itemSummary(i: any): {
|
|
12
|
+
item_id: any;
|
|
13
|
+
name: any;
|
|
14
|
+
sku: any;
|
|
15
|
+
status: any;
|
|
16
|
+
item_type: any;
|
|
17
|
+
product_type: any;
|
|
18
|
+
unit: any;
|
|
19
|
+
rate: any;
|
|
20
|
+
purchase_rate: any;
|
|
21
|
+
tax_name: any;
|
|
22
|
+
tax_percentage: any;
|
|
23
|
+
stock_on_hand: any;
|
|
24
|
+
vendor_name: any;
|
|
25
|
+
};
|
|
26
|
+
declare const _default: import("openclaw/plugin-sdk/tool-plugin").DefinedToolPluginEntry;
|
|
27
|
+
export default _default;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import { Type } from "typebox";
|
|
3
|
+
import { defineToolPlugin } from "openclaw/plugin-sdk/tool-plugin";
|
|
4
|
+
import { ZohoBooksClient, zohoId } from "./client.js";
|
|
5
|
+
import { normalizeConfig } from "./config.js";
|
|
6
|
+
const SecretRef = Type.Object({
|
|
7
|
+
source: Type.Union([Type.Literal("env"), Type.Literal("file"), Type.Literal("exec"), Type.Literal("store")]),
|
|
8
|
+
provider: Type.String(),
|
|
9
|
+
id: Type.String(),
|
|
10
|
+
}, { additionalProperties: false });
|
|
11
|
+
const secretInput = (description) => Type.Union([Type.String({ minLength: 1 }), SecretRef], { description });
|
|
12
|
+
const configSchema = Type.Object({
|
|
13
|
+
clientId: secretInput("Zoho API client id (Zoho API console, self client or server-based client)."),
|
|
14
|
+
clientSecret: secretInput("Zoho API client secret."),
|
|
15
|
+
refreshToken: secretInput("Zoho refresh token with Zoho Books access for the organization."),
|
|
16
|
+
organizationId: Type.String({ pattern: "^\\d{1,30}$", description: "Zoho Books organization number (Settings → Organization profile)." }),
|
|
17
|
+
dataCenter: Type.Optional(Type.Union([Type.Literal("eu"), Type.Literal("com"), Type.Literal("in"), Type.Literal("sa")], { description: "Zoho data center of the account. Defaults to eu." })),
|
|
18
|
+
requestTimeoutMs: Type.Optional(Type.Integer({ minimum: 1000, maximum: 120000 })),
|
|
19
|
+
}, { additionalProperties: false });
|
|
20
|
+
let cached;
|
|
21
|
+
/** One client (and access token) per resolved config. */
|
|
22
|
+
export function clientFor(rawConfig, fetchImpl = fetch) {
|
|
23
|
+
const config = normalizeConfig((rawConfig ?? {}));
|
|
24
|
+
const key = crypto.createHash("sha256").update(JSON.stringify(config)).digest("hex");
|
|
25
|
+
if (cached?.key !== key)
|
|
26
|
+
cached = { key, config, client: new ZohoBooksClient(config, fetchImpl) };
|
|
27
|
+
return cached;
|
|
28
|
+
}
|
|
29
|
+
export function resetClientCache() {
|
|
30
|
+
cached = undefined;
|
|
31
|
+
}
|
|
32
|
+
/** Records each change in OpenClaw's state database: which record, which fields, never their values. */
|
|
33
|
+
async function audit(api, entry) {
|
|
34
|
+
const store = api?.runtime?.state?.openKeyedStoreV2?.({ namespace: "changes", maxEntries: 5000 });
|
|
35
|
+
if (!store)
|
|
36
|
+
return;
|
|
37
|
+
await store.register(`${String(Date.now()).padStart(15, "0")}-${crypto.randomUUID().slice(0, 8)}`, { at: new Date().toISOString(), ...entry });
|
|
38
|
+
}
|
|
39
|
+
/** The fields worth showing for an item in a list. */
|
|
40
|
+
export function itemSummary(i) {
|
|
41
|
+
return {
|
|
42
|
+
item_id: i?.item_id,
|
|
43
|
+
name: i?.name,
|
|
44
|
+
sku: i?.sku || null,
|
|
45
|
+
status: i?.status,
|
|
46
|
+
item_type: i?.item_type,
|
|
47
|
+
product_type: i?.product_type,
|
|
48
|
+
unit: i?.unit || null,
|
|
49
|
+
rate: i?.rate,
|
|
50
|
+
purchase_rate: i?.purchase_rate,
|
|
51
|
+
tax_name: i?.tax_name || null,
|
|
52
|
+
tax_percentage: i?.tax_percentage ?? null,
|
|
53
|
+
stock_on_hand: i?.stock_on_hand ?? null,
|
|
54
|
+
vendor_name: i?.vendor_name || null,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
const id = (what) => Type.String({ pattern: "^\\d{1,30}$", description: `${what} (digits).` });
|
|
58
|
+
const ITEM_FIELDS = {
|
|
59
|
+
name: Type.String({ minLength: 1, maxLength: 100, description: "Item name, up to 100 characters; unique in Zoho Books." }),
|
|
60
|
+
rate: Type.Number({ minimum: 0, description: "Selling price before tax." }),
|
|
61
|
+
sku: Type.String({ minLength: 1, maxLength: 100, description: "SKU; must not be used by another item." }),
|
|
62
|
+
description: Type.String({ maxLength: 2000, description: "Sales description." }),
|
|
63
|
+
unit: Type.String({ maxLength: 50, description: "Unit, e.g. pcs, box, m." }),
|
|
64
|
+
product_type: Type.Union([Type.Literal("goods"), Type.Literal("service"), Type.Literal("digital_service")], { description: "goods (default), service or digital_service." }),
|
|
65
|
+
item_type: Type.Union([Type.Literal("inventory"), Type.Literal("sales"), Type.Literal("purchases"), Type.Literal("sales_and_purchases")], {
|
|
66
|
+
description: "inventory (tracked stock, default for goods), sales, purchases or sales_and_purchases.",
|
|
67
|
+
}),
|
|
68
|
+
purchase_rate: Type.Number({ minimum: 0, description: "Purchase price before tax." }),
|
|
69
|
+
purchase_description: Type.String({ maxLength: 2000, description: "Purchase description." }),
|
|
70
|
+
tax_id: id("Tax id from zoho_books_taxes (for example the 15% VAT)"),
|
|
71
|
+
is_taxable: Type.Boolean({ description: "Whether the item is taxable." }),
|
|
72
|
+
vendor_id: id("Preferred vendor id from zoho_books_vendors_search"),
|
|
73
|
+
account_id: id("Sales account id from zoho_books_accounts"),
|
|
74
|
+
purchase_account_id: id("Purchase account id from zoho_books_accounts"),
|
|
75
|
+
inventory_account_id: id("Inventory asset account id from zoho_books_accounts (inventory items)"),
|
|
76
|
+
reorder_level: Type.Number({ minimum: 0, description: "Stock level that should trigger reordering." }),
|
|
77
|
+
};
|
|
78
|
+
const FIELD_NAMES = Object.keys(ITEM_FIELDS);
|
|
79
|
+
function pick(params) {
|
|
80
|
+
const out = {};
|
|
81
|
+
for (const field of FIELD_NAMES)
|
|
82
|
+
if (params[field] !== undefined)
|
|
83
|
+
out[field] = params[field];
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
const optionalFields = Object.fromEntries(FIELD_NAMES.map((f) => [f, Type.Optional(ITEM_FIELDS[f])]));
|
|
87
|
+
/** Items whose SKU is exactly this one (Zoho's search matches parts of names and SKUs). */
|
|
88
|
+
async function itemsWithSku(client, sku, signal) {
|
|
89
|
+
const found = await client.request("GET", "/items", { search_text: sku, filter_by: "Status.All", per_page: 200 }, undefined, signal);
|
|
90
|
+
return (found?.items ?? []).filter((i) => String(i?.sku ?? "").trim().toLowerCase() === sku.trim().toLowerCase());
|
|
91
|
+
}
|
|
92
|
+
export default defineToolPlugin({
|
|
93
|
+
id: "zoho-books",
|
|
94
|
+
name: "Zoho Books",
|
|
95
|
+
description: "Reads and writes Zoho Books items (products and services) for the configured organization, with the Zoho sign-in kept in OpenClaw's secret store.",
|
|
96
|
+
configSchema,
|
|
97
|
+
tools: (tool) => [
|
|
98
|
+
tool({
|
|
99
|
+
name: "zoho_books_items_search",
|
|
100
|
+
label: "Zoho Books items",
|
|
101
|
+
description: "Search Zoho Books items by name, SKU or description text. Returns a short summary of each (id, name, SKU, prices, tax, stock, vendor). Always search before creating an item, to avoid duplicates.",
|
|
102
|
+
workspaceAccess: false,
|
|
103
|
+
parameters: Type.Object({
|
|
104
|
+
search_text: Type.Optional(Type.String({ maxLength: 100, description: "Text to search for in names, SKUs and descriptions." })),
|
|
105
|
+
status: Type.Optional(Type.Union([Type.Literal("active"), Type.Literal("inactive"), Type.Literal("all")], { description: "active (default), inactive or all." })),
|
|
106
|
+
page: Type.Optional(Type.Integer({ minimum: 1 })),
|
|
107
|
+
per_page: Type.Optional(Type.Integer({ minimum: 1, maximum: 200, description: "1 to 200 (default 25)." })),
|
|
108
|
+
}),
|
|
109
|
+
async execute(params, config, context) {
|
|
110
|
+
const status = { active: "Status.Active", inactive: "Status.Inactive", all: "Status.All" }[(params.status ?? "active")];
|
|
111
|
+
const data = await clientFor(config).client.request("GET", "/items", { search_text: params.search_text, filter_by: status, page: params.page, per_page: params.per_page ?? 25 }, undefined, context.signal);
|
|
112
|
+
return ({ items: (data?.items ?? []).map(itemSummary), page: data?.page_context?.page ?? params.page ?? 1, has_more_page: data?.page_context?.has_more_page ?? false });
|
|
113
|
+
},
|
|
114
|
+
}),
|
|
115
|
+
tool({
|
|
116
|
+
name: "zoho_books_item_get",
|
|
117
|
+
label: "Zoho Books item",
|
|
118
|
+
description: "Read one Zoho Books item with all its fields (prices, tax, accounts, vendor, stock).",
|
|
119
|
+
workspaceAccess: false,
|
|
120
|
+
parameters: Type.Object({ item_id: id("Item id from zoho_books_items_search") }),
|
|
121
|
+
async execute(params, config, context) {
|
|
122
|
+
const data = await clientFor(config).client.request("GET", `/items/${zohoId(params.item_id, "item_id")}`, {}, undefined, context.signal);
|
|
123
|
+
return (data?.item ?? null);
|
|
124
|
+
},
|
|
125
|
+
}),
|
|
126
|
+
tool({
|
|
127
|
+
name: "zoho_books_item_create",
|
|
128
|
+
label: "Zoho Books item create",
|
|
129
|
+
description: "Create a Zoho Books item. Only name and rate are required; goods default to an inventory item. Refused when another item already has the same SKU (returns that item instead). Use zoho_books_taxes, zoho_books_vendors_search and zoho_books_accounts for the ids.",
|
|
130
|
+
workspaceAccess: false,
|
|
131
|
+
parameters: Type.Object({ name: ITEM_FIELDS.name, rate: ITEM_FIELDS.rate, ...Object.fromEntries(FIELD_NAMES.filter((f) => f !== "name" && f !== "rate").map((f) => [f, Type.Optional(ITEM_FIELDS[f])])) }),
|
|
132
|
+
async execute(params, config, context) {
|
|
133
|
+
const { client } = clientFor(config);
|
|
134
|
+
const body = pick(params);
|
|
135
|
+
body.product_type ??= "goods";
|
|
136
|
+
if (body.item_type === undefined && body.product_type === "goods")
|
|
137
|
+
body.item_type = "inventory";
|
|
138
|
+
if (typeof body.sku === "string") {
|
|
139
|
+
const same = await itemsWithSku(client, body.sku, context.signal);
|
|
140
|
+
if (same.length) {
|
|
141
|
+
return ({ created: false, reason: `SKU ${body.sku} is already used`, existing: same.map(itemSummary) });
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
const data = await client.request("POST", "/items", {}, body, context.signal);
|
|
145
|
+
const item = data?.item ?? null;
|
|
146
|
+
await audit(context.api, { action: "item.create", itemId: item?.item_id ?? null, sku: body.sku ?? null, fields: Object.keys(body) });
|
|
147
|
+
return ({ created: true, item });
|
|
148
|
+
},
|
|
149
|
+
}),
|
|
150
|
+
tool({
|
|
151
|
+
name: "zoho_books_item_update",
|
|
152
|
+
label: "Zoho Books item update",
|
|
153
|
+
description: "Change fields of one Zoho Books item (only the fields given), and/or mark it active or inactive. Read it first with zoho_books_item_get. Refused when the new SKU belongs to another item.",
|
|
154
|
+
workspaceAccess: false,
|
|
155
|
+
parameters: Type.Object({
|
|
156
|
+
item_id: id("Item id from zoho_books_items_search"),
|
|
157
|
+
...optionalFields,
|
|
158
|
+
status: Type.Optional(Type.Union([Type.Literal("active"), Type.Literal("inactive")], { description: "Mark the item active or inactive." })),
|
|
159
|
+
}),
|
|
160
|
+
async execute(params, config, context) {
|
|
161
|
+
const { client } = clientFor(config);
|
|
162
|
+
const itemId = zohoId(params.item_id, "item_id");
|
|
163
|
+
const changes = pick(params);
|
|
164
|
+
if (!Object.keys(changes).length && !params.status)
|
|
165
|
+
throw new Error("nothing to change: give at least one field or status");
|
|
166
|
+
let item;
|
|
167
|
+
if (Object.keys(changes).length) {
|
|
168
|
+
if (typeof changes.sku === "string") {
|
|
169
|
+
const others = (await itemsWithSku(client, changes.sku, context.signal)).filter((i) => String(i.item_id) !== itemId);
|
|
170
|
+
if (others.length)
|
|
171
|
+
return ({ updated: false, reason: `SKU ${changes.sku} is used by another item`, existing: others.map(itemSummary) });
|
|
172
|
+
}
|
|
173
|
+
// Zoho requires name and rate on every update: the current ones are kept unless changed.
|
|
174
|
+
const current = (await client.request("GET", `/items/${itemId}`, {}, undefined, context.signal))?.item;
|
|
175
|
+
if (!current)
|
|
176
|
+
throw new Error(`item ${itemId} not found`);
|
|
177
|
+
const data = await client.request("PUT", `/items/${itemId}`, {}, { name: current.name, rate: current.rate, ...changes }, context.signal);
|
|
178
|
+
item = data?.item ?? null;
|
|
179
|
+
}
|
|
180
|
+
if (params.status)
|
|
181
|
+
await client.request("POST", `/items/${itemId}/${params.status}`, {}, undefined, context.signal);
|
|
182
|
+
await audit(context.api, { action: "item.update", itemId, fields: Object.keys(changes), status: params.status ?? null });
|
|
183
|
+
if (!item || params.status)
|
|
184
|
+
item = (await client.request("GET", `/items/${itemId}`, {}, undefined, context.signal))?.item ?? item;
|
|
185
|
+
return ({ updated: true, item });
|
|
186
|
+
},
|
|
187
|
+
}),
|
|
188
|
+
tool({
|
|
189
|
+
name: "zoho_books_taxes",
|
|
190
|
+
label: "Zoho Books taxes",
|
|
191
|
+
description: "List the organization's taxes (id, name, percentage), for an item's tax_id.",
|
|
192
|
+
workspaceAccess: false,
|
|
193
|
+
parameters: Type.Object({}),
|
|
194
|
+
async execute(_params, config, context) {
|
|
195
|
+
const data = await clientFor(config).client.request("GET", "/settings/taxes", {}, undefined, context.signal);
|
|
196
|
+
return ((data?.taxes ?? []).map((t) => ({ tax_id: t.tax_id, tax_name: t.tax_name, tax_percentage: t.tax_percentage, tax_type: t.tax_type, is_default_tax: t.is_default_tax ?? false })));
|
|
197
|
+
},
|
|
198
|
+
}),
|
|
199
|
+
tool({
|
|
200
|
+
name: "zoho_books_vendors_search",
|
|
201
|
+
label: "Zoho Books vendors",
|
|
202
|
+
description: "Search vendors (suppliers) by name, for an item's vendor_id.",
|
|
203
|
+
workspaceAccess: false,
|
|
204
|
+
parameters: Type.Object({
|
|
205
|
+
search_text: Type.String({ minLength: 1, maxLength: 100, description: "Part of the vendor or company name." }),
|
|
206
|
+
}),
|
|
207
|
+
async execute(params, config, context) {
|
|
208
|
+
const data = await clientFor(config).client.request("GET", "/contacts", { contact_type: "vendor", search_text: params.search_text, per_page: 50 }, undefined, context.signal);
|
|
209
|
+
return ((data?.contacts ?? []).map((c) => ({ vendor_id: c.contact_id, name: c.contact_name, company_name: c.company_name || null, status: c.status })));
|
|
210
|
+
},
|
|
211
|
+
}),
|
|
212
|
+
tool({
|
|
213
|
+
name: "zoho_books_accounts",
|
|
214
|
+
label: "Zoho Books accounts",
|
|
215
|
+
description: "List active accounts from the chart of accounts (id, name, type), for an item's sales, purchase and inventory accounts.",
|
|
216
|
+
workspaceAccess: false,
|
|
217
|
+
parameters: Type.Object({
|
|
218
|
+
account_type: Type.Optional(Type.Union([Type.Literal("income"), Type.Literal("expense"), Type.Literal("cost_of_goods_sold"), Type.Literal("stock")], {
|
|
219
|
+
description: "Only this type: income (sales), expense, cost_of_goods_sold (purchases) or stock (inventory).",
|
|
220
|
+
})),
|
|
221
|
+
}),
|
|
222
|
+
async execute(params, config, context) {
|
|
223
|
+
const data = await clientFor(config).client.request("GET", "/chartofaccounts", { filter_by: "AccountType.Active", per_page: 200 }, undefined, context.signal);
|
|
224
|
+
return ((data?.chartofaccounts ?? [])
|
|
225
|
+
.filter((a) => !params.account_type || a.account_type === params.account_type)
|
|
226
|
+
.map((a) => ({ account_id: a.account_id, account_name: a.account_name, account_type: a.account_type })));
|
|
227
|
+
},
|
|
228
|
+
}),
|
|
229
|
+
],
|
|
230
|
+
});
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "zoho-books",
|
|
3
|
+
"name": "Zoho Books",
|
|
4
|
+
"description": "Reads and writes Zoho Books items (products and services) for the configured organization, with the Zoho sign-in kept in OpenClaw's secret store.",
|
|
5
|
+
"version": "1.0.0",
|
|
6
|
+
"activation": {
|
|
7
|
+
"onStartup": true
|
|
8
|
+
},
|
|
9
|
+
"configContracts": {
|
|
10
|
+
"secretInputs": {
|
|
11
|
+
"paths": [
|
|
12
|
+
{
|
|
13
|
+
"path": "clientId",
|
|
14
|
+
"expected": "string",
|
|
15
|
+
"ownerKind": "capability"
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"path": "clientSecret",
|
|
19
|
+
"expected": "string",
|
|
20
|
+
"ownerKind": "capability"
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"path": "refreshToken",
|
|
24
|
+
"expected": "string",
|
|
25
|
+
"ownerKind": "capability"
|
|
26
|
+
}
|
|
27
|
+
]
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"configSchema": {
|
|
31
|
+
"type": "object",
|
|
32
|
+
"required": [
|
|
33
|
+
"clientId",
|
|
34
|
+
"clientSecret",
|
|
35
|
+
"refreshToken",
|
|
36
|
+
"organizationId"
|
|
37
|
+
],
|
|
38
|
+
"properties": {
|
|
39
|
+
"clientId": {
|
|
40
|
+
"anyOf": [
|
|
41
|
+
{
|
|
42
|
+
"type": "string",
|
|
43
|
+
"minLength": 1
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
"type": "object",
|
|
47
|
+
"required": [
|
|
48
|
+
"source",
|
|
49
|
+
"provider",
|
|
50
|
+
"id"
|
|
51
|
+
],
|
|
52
|
+
"properties": {
|
|
53
|
+
"source": {
|
|
54
|
+
"anyOf": [
|
|
55
|
+
{
|
|
56
|
+
"type": "string",
|
|
57
|
+
"const": "env"
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
"type": "string",
|
|
61
|
+
"const": "file"
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
"type": "string",
|
|
65
|
+
"const": "exec"
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
"type": "string",
|
|
69
|
+
"const": "store"
|
|
70
|
+
}
|
|
71
|
+
]
|
|
72
|
+
},
|
|
73
|
+
"provider": {
|
|
74
|
+
"type": "string"
|
|
75
|
+
},
|
|
76
|
+
"id": {
|
|
77
|
+
"type": "string"
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
"additionalProperties": false
|
|
81
|
+
}
|
|
82
|
+
],
|
|
83
|
+
"description": "Zoho API client id (Zoho API console, self client or server-based client)."
|
|
84
|
+
},
|
|
85
|
+
"clientSecret": {
|
|
86
|
+
"anyOf": [
|
|
87
|
+
{
|
|
88
|
+
"type": "string",
|
|
89
|
+
"minLength": 1
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
"type": "object",
|
|
93
|
+
"required": [
|
|
94
|
+
"source",
|
|
95
|
+
"provider",
|
|
96
|
+
"id"
|
|
97
|
+
],
|
|
98
|
+
"properties": {
|
|
99
|
+
"source": {
|
|
100
|
+
"anyOf": [
|
|
101
|
+
{
|
|
102
|
+
"type": "string",
|
|
103
|
+
"const": "env"
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
"type": "string",
|
|
107
|
+
"const": "file"
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
"type": "string",
|
|
111
|
+
"const": "exec"
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
"type": "string",
|
|
115
|
+
"const": "store"
|
|
116
|
+
}
|
|
117
|
+
]
|
|
118
|
+
},
|
|
119
|
+
"provider": {
|
|
120
|
+
"type": "string"
|
|
121
|
+
},
|
|
122
|
+
"id": {
|
|
123
|
+
"type": "string"
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
"additionalProperties": false
|
|
127
|
+
}
|
|
128
|
+
],
|
|
129
|
+
"description": "Zoho API client secret."
|
|
130
|
+
},
|
|
131
|
+
"refreshToken": {
|
|
132
|
+
"anyOf": [
|
|
133
|
+
{
|
|
134
|
+
"type": "string",
|
|
135
|
+
"minLength": 1
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
"type": "object",
|
|
139
|
+
"required": [
|
|
140
|
+
"source",
|
|
141
|
+
"provider",
|
|
142
|
+
"id"
|
|
143
|
+
],
|
|
144
|
+
"properties": {
|
|
145
|
+
"source": {
|
|
146
|
+
"anyOf": [
|
|
147
|
+
{
|
|
148
|
+
"type": "string",
|
|
149
|
+
"const": "env"
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
"type": "string",
|
|
153
|
+
"const": "file"
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
"type": "string",
|
|
157
|
+
"const": "exec"
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
"type": "string",
|
|
161
|
+
"const": "store"
|
|
162
|
+
}
|
|
163
|
+
]
|
|
164
|
+
},
|
|
165
|
+
"provider": {
|
|
166
|
+
"type": "string"
|
|
167
|
+
},
|
|
168
|
+
"id": {
|
|
169
|
+
"type": "string"
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
"additionalProperties": false
|
|
173
|
+
}
|
|
174
|
+
],
|
|
175
|
+
"description": "Zoho refresh token with Zoho Books access for the organization."
|
|
176
|
+
},
|
|
177
|
+
"organizationId": {
|
|
178
|
+
"type": "string",
|
|
179
|
+
"pattern": "^\\d{1,30}$",
|
|
180
|
+
"description": "Zoho Books organization number (Settings → Organization profile)."
|
|
181
|
+
},
|
|
182
|
+
"dataCenter": {
|
|
183
|
+
"anyOf": [
|
|
184
|
+
{
|
|
185
|
+
"type": "string",
|
|
186
|
+
"const": "eu"
|
|
187
|
+
},
|
|
188
|
+
{
|
|
189
|
+
"type": "string",
|
|
190
|
+
"const": "com"
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
"type": "string",
|
|
194
|
+
"const": "in"
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
"type": "string",
|
|
198
|
+
"const": "sa"
|
|
199
|
+
}
|
|
200
|
+
],
|
|
201
|
+
"description": "Zoho data center of the account. Defaults to eu."
|
|
202
|
+
},
|
|
203
|
+
"requestTimeoutMs": {
|
|
204
|
+
"type": "integer",
|
|
205
|
+
"minimum": 1000,
|
|
206
|
+
"maximum": 120000
|
|
207
|
+
}
|
|
208
|
+
},
|
|
209
|
+
"additionalProperties": false
|
|
210
|
+
},
|
|
211
|
+
"contracts": {
|
|
212
|
+
"tools": [
|
|
213
|
+
"zoho_books_items_search",
|
|
214
|
+
"zoho_books_item_get",
|
|
215
|
+
"zoho_books_item_create",
|
|
216
|
+
"zoho_books_item_update",
|
|
217
|
+
"zoho_books_taxes",
|
|
218
|
+
"zoho_books_vendors_search",
|
|
219
|
+
"zoho_books_accounts"
|
|
220
|
+
]
|
|
221
|
+
}
|
|
222
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@raseef/openclaw-zoho-books",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Zoho Books items for OpenClaw agents: search, read, create and update products and services, with the Zoho sign-in kept in OpenClaw's secret store.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc -p tsconfig.json",
|
|
9
|
+
"plugin:build": "npm run build && openclaw plugins build --entry ./dist/index.js",
|
|
10
|
+
"plugin:check": "npm run build && openclaw plugins build --entry ./dist/index.js --check && openclaw plugins validate --entry ./dist/index.js",
|
|
11
|
+
"test": "vitest run --config ./vitest.config.ts",
|
|
12
|
+
"prepack": "npm run plugin:build"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"openclaw.plugin.json",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"openclaw": ">=2026.11.6"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"typebox": "^1.1.38"
|
|
24
|
+
},
|
|
25
|
+
"openclaw": {
|
|
26
|
+
"extensions": [
|
|
27
|
+
"./dist/index.js"
|
|
28
|
+
],
|
|
29
|
+
"compat": {
|
|
30
|
+
"pluginApi": ">=2026.11.6"
|
|
31
|
+
},
|
|
32
|
+
"build": {
|
|
33
|
+
"openclawVersion": "2026.11.6"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
}
|
|
39
|
+
}
|