@raseef/openclaw-raseef-studio 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 +50 -2
- package/dist/client.d.ts +16 -0
- package/dist/client.js +74 -0
- package/dist/config.d.ts +10 -0
- package/dist/config.js +30 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +222 -0
- package/dist/media.d.ts +17 -0
- package/dist/media.js +65 -0
- package/openclaw.plugin.json +104 -0
- package/package.json +37 -4
package/README.md
CHANGED
|
@@ -1,3 +1,51 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Raseef Studio
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
OpenClaw tools for the Raseef Studio member API (`https://studio.raseef.dev/api/v1`): read the post inventory, upload images, create and edit bilingual posts, and publish them. The token stays in OpenClaw's secret store; no shell or scripts are needed.
|
|
4
|
+
|
|
5
|
+
| Tool | What it does |
|
|
6
|
+
| --- | --- |
|
|
7
|
+
| `studio_me` | Which Studio member the token belongs to. |
|
|
8
|
+
| `studio_posts_list` | Posts, newest first, filtered by text and status, with paging. |
|
|
9
|
+
| `studio_post_get` | One post with both languages, its status and version. |
|
|
10
|
+
| `studio_post_create` | A new draft with both languages. Returns its id and version. |
|
|
11
|
+
| `studio_post_update` | Save changed fields, with the version last read. |
|
|
12
|
+
| `studio_post_state` | Publish now or at a time, return to draft, or archive. |
|
|
13
|
+
| `studio_post_revisions` | Saved versions of one post. |
|
|
14
|
+
| `studio_media_list` | The image library. |
|
|
15
|
+
| `studio_media_upload` | Upload one PNG, JPEG or WebP under 2,000,000 bytes with Arabic and English alt text. Returns the id and its `https://raseef.dev/blog/media/<id>` address. |
|
|
16
|
+
|
|
17
|
+
## Publishing an article
|
|
18
|
+
|
|
19
|
+
1. `studio_posts_list` across every status, to avoid duplicates.
|
|
20
|
+
2. `image_generate` a cover as `jpeg` or `webp`, then `studio_media_upload` it.
|
|
21
|
+
3. `studio_post_create` with both languages and `cover_media_id`.
|
|
22
|
+
4. `studio_post_state` with `status: "published"` and the version from step 3.
|
|
23
|
+
|
|
24
|
+
## Safety
|
|
25
|
+
|
|
26
|
+
- The token is sent only to the configured https base URL, and redirects are refused.
|
|
27
|
+
- Images are read only from the agent workspace, OpenClaw's media folder (where `image_generate` saves), and any `uploadRoots`. The real path after symlinks must be inside one of them, and the bytes must be PNG, JPEG or WebP.
|
|
28
|
+
- Ids are checked before they go into a request path.
|
|
29
|
+
- Every create, save, state change and upload is recorded in OpenClaw's state database, without post bodies.
|
|
30
|
+
- Studio itself limits a member token: it cannot delete anything or see the team or the activity log.
|
|
31
|
+
|
|
32
|
+
## Install and configure
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
openclaw plugins install npm:@raseef/openclaw-raseef-studio
|
|
36
|
+
openclaw secrets store set RASEEF_STUDIO_API_TOKEN --kind secret --value-file token.txt
|
|
37
|
+
openclaw config set plugins.entries.raseef-studio.config --strict-json '{
|
|
38
|
+
"apiToken": { "source": "store", "provider": "default", "id": "RASEEF_STUDIO_API_TOKEN" }
|
|
39
|
+
}'
|
|
40
|
+
openclaw plugins enable raseef-studio
|
|
41
|
+
openclaw secrets audit --check
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Add `raseef-studio` to `plugins.allow` and the `studio_*` tools to the agent's `tools.allow`.
|
|
45
|
+
|
|
46
|
+
| Key | Required | Default |
|
|
47
|
+
| --- | --- | --- |
|
|
48
|
+
| `apiToken` | yes | |
|
|
49
|
+
| `baseUrl` | no | `https://studio.raseef.dev/api/v1` |
|
|
50
|
+
| `requestTimeoutMs` | no | `30000` |
|
|
51
|
+
| `uploadRoots` | no; absolute folders | |
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { StudioConfig } from "./config.js";
|
|
2
|
+
export declare class StudioError extends Error {
|
|
3
|
+
readonly status: number;
|
|
4
|
+
constructor(message: string, status: number);
|
|
5
|
+
}
|
|
6
|
+
/** A post or image id from Studio, safe to place in a path. */
|
|
7
|
+
export declare function studioId(value: unknown, label?: string): string;
|
|
8
|
+
/** Thin client for the Studio member API. The token only ever goes to the configured https base URL. */
|
|
9
|
+
export declare class StudioClient {
|
|
10
|
+
private readonly config;
|
|
11
|
+
private readonly fetchImpl;
|
|
12
|
+
constructor(config: StudioConfig, fetchImpl?: typeof fetch);
|
|
13
|
+
request(method: "GET" | "POST", path: string, body?: unknown, signal?: AbortSignal): Promise<any>;
|
|
14
|
+
}
|
|
15
|
+
/** Query string from defined values only. */
|
|
16
|
+
export declare function query(params: Record<string, unknown>): string;
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export class StudioError extends Error {
|
|
2
|
+
status;
|
|
3
|
+
constructor(message, status) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.status = status;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
const ID = /^[A-Za-z0-9_-]{1,100}$/;
|
|
9
|
+
/** A post or image id from Studio, safe to place in a path. */
|
|
10
|
+
export function studioId(value, label = "id") {
|
|
11
|
+
const id = String(value ?? "").trim();
|
|
12
|
+
if (!ID.test(id))
|
|
13
|
+
throw new Error(`${label} must be a Studio id (letters, digits, - and _)`);
|
|
14
|
+
return encodeURIComponent(id);
|
|
15
|
+
}
|
|
16
|
+
function errorText(data) {
|
|
17
|
+
if (typeof data === "string")
|
|
18
|
+
return data.slice(0, 300);
|
|
19
|
+
const d = data;
|
|
20
|
+
const value = d?.error?.message ?? d?.message ?? d?.error ?? d?.detail;
|
|
21
|
+
return typeof value === "string" ? value.slice(0, 300) : "";
|
|
22
|
+
}
|
|
23
|
+
/** Thin client for the Studio member API. The token only ever goes to the configured https base URL. */
|
|
24
|
+
export class StudioClient {
|
|
25
|
+
config;
|
|
26
|
+
fetchImpl;
|
|
27
|
+
constructor(config, fetchImpl = fetch) {
|
|
28
|
+
this.config = config;
|
|
29
|
+
this.fetchImpl = fetchImpl;
|
|
30
|
+
}
|
|
31
|
+
async request(method, path, body, signal) {
|
|
32
|
+
const limit = AbortSignal.timeout(this.config.requestTimeoutMs);
|
|
33
|
+
const response = await this.fetchImpl(`${this.config.baseUrl}${path}`, {
|
|
34
|
+
method,
|
|
35
|
+
headers: {
|
|
36
|
+
authorization: `Bearer ${this.config.apiToken}`,
|
|
37
|
+
accept: "application/json",
|
|
38
|
+
...(body === undefined ? {} : { "content-type": "application/json" }),
|
|
39
|
+
},
|
|
40
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
41
|
+
// A redirect could carry the request somewhere else; Studio never needs one.
|
|
42
|
+
redirect: "error",
|
|
43
|
+
signal: signal ? AbortSignal.any([signal, limit]) : limit,
|
|
44
|
+
});
|
|
45
|
+
const text = await response.text();
|
|
46
|
+
let data = null;
|
|
47
|
+
if (text) {
|
|
48
|
+
try {
|
|
49
|
+
data = JSON.parse(text);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
data = text;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (!response.ok) {
|
|
56
|
+
if (response.status === 409) {
|
|
57
|
+
throw new StudioError("Studio version conflict: the post was saved after you read it. Read it again with studio_post_get, then repeat the change with the new version.", 409);
|
|
58
|
+
}
|
|
59
|
+
const detail = errorText(data).split(this.config.apiToken).join("[token]");
|
|
60
|
+
throw new StudioError(`Studio ${method} ${path.split("?")[0]} failed: HTTP ${response.status}${detail ? `: ${detail}` : ""}`, response.status);
|
|
61
|
+
}
|
|
62
|
+
return data;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/** Query string from defined values only. */
|
|
66
|
+
export function query(params) {
|
|
67
|
+
const search = new URLSearchParams();
|
|
68
|
+
for (const [key, value] of Object.entries(params)) {
|
|
69
|
+
if (value !== undefined && value !== null && value !== "")
|
|
70
|
+
search.set(key, String(value));
|
|
71
|
+
}
|
|
72
|
+
const text = search.toString();
|
|
73
|
+
return text ? `?${text}` : "";
|
|
74
|
+
}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type StudioConfig = {
|
|
2
|
+
apiToken: string;
|
|
3
|
+
/** Studio API root, always https and without a trailing slash. */
|
|
4
|
+
baseUrl: string;
|
|
5
|
+
requestTimeoutMs: number;
|
|
6
|
+
/** Extra absolute folders images may be uploaded from, besides the agent workspace and OpenClaw's media folder. */
|
|
7
|
+
uploadRoots: string[];
|
|
8
|
+
};
|
|
9
|
+
export declare const DEFAULT_BASE_URL = "https://studio.raseef.dev/api/v1";
|
|
10
|
+
export declare function normalizeConfig(input?: Record<string, any>): StudioConfig;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
export const DEFAULT_BASE_URL = "https://studio.raseef.dev/api/v1";
|
|
3
|
+
export function normalizeConfig(input = {}) {
|
|
4
|
+
// OpenClaw resolves SecretRefs before the plugin sees its config, so this is the token itself.
|
|
5
|
+
const apiToken = typeof input.apiToken === "string" ? input.apiToken.trim() : "";
|
|
6
|
+
if (!apiToken)
|
|
7
|
+
throw new Error("raseef-studio apiToken is not configured");
|
|
8
|
+
let base;
|
|
9
|
+
try {
|
|
10
|
+
base = new URL(String(input.baseUrl ?? DEFAULT_BASE_URL).trim());
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
throw new Error("raseef-studio baseUrl must be an https URL");
|
|
14
|
+
}
|
|
15
|
+
if (base.protocol !== "https:" || base.username || base.password || base.search || base.hash) {
|
|
16
|
+
throw new Error("raseef-studio baseUrl must be a plain https URL");
|
|
17
|
+
}
|
|
18
|
+
const baseUrl = base.toString().replace(/\/+$/, "");
|
|
19
|
+
const timeout = input.requestTimeoutMs === undefined ? 30_000 : Number(input.requestTimeoutMs);
|
|
20
|
+
if (!Number.isInteger(timeout) || timeout < 1000 || timeout > 120_000) {
|
|
21
|
+
throw new Error("raseef-studio requestTimeoutMs must be between 1000 and 120000");
|
|
22
|
+
}
|
|
23
|
+
const uploadRoots = (input.uploadRoots ?? []).map((root, index) => {
|
|
24
|
+
const value = String(root ?? "").trim();
|
|
25
|
+
if (!path.isAbsolute(value))
|
|
26
|
+
throw new Error(`raseef-studio uploadRoots[${index}] must be an absolute path`);
|
|
27
|
+
return path.resolve(value);
|
|
28
|
+
});
|
|
29
|
+
return { apiToken, baseUrl, requestTimeoutMs: timeout, uploadRoots };
|
|
30
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { StudioClient } from "./client.js";
|
|
2
|
+
import { type StudioConfig } from "./config.js";
|
|
3
|
+
/** One client per resolved config. */
|
|
4
|
+
export declare function clientFor(rawConfig: unknown, fetchImpl?: typeof fetch): {
|
|
5
|
+
key: string;
|
|
6
|
+
config: StudioConfig;
|
|
7
|
+
client: StudioClient;
|
|
8
|
+
};
|
|
9
|
+
export declare function resetClientCache(): void;
|
|
10
|
+
declare const _default: import("openclaw/plugin-sdk/tool-plugin").DefinedToolPluginEntry;
|
|
11
|
+
export default _default;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import { Type } from "typebox";
|
|
3
|
+
import { defineToolPlugin } from "openclaw/plugin-sdk/tool-plugin";
|
|
4
|
+
import { StudioClient, query, studioId } from "./client.js";
|
|
5
|
+
import { normalizeConfig } from "./config.js";
|
|
6
|
+
import { openclawMediaDir, readUploadImage } from "./media.js";
|
|
7
|
+
const SecretRef = Type.Object({
|
|
8
|
+
source: Type.Union([Type.Literal("env"), Type.Literal("file"), Type.Literal("exec"), Type.Literal("store")]),
|
|
9
|
+
provider: Type.String(),
|
|
10
|
+
id: Type.String(),
|
|
11
|
+
}, { additionalProperties: false });
|
|
12
|
+
const configSchema = Type.Object({
|
|
13
|
+
apiToken: Type.Union([Type.String({ minLength: 1 }), SecretRef], { description: "Studio member API token (raseef_studio_…), from the admin console's Studio team page." }),
|
|
14
|
+
baseUrl: Type.Optional(Type.String({ description: "Studio API root. Defaults to https://studio.raseef.dev/api/v1." })),
|
|
15
|
+
requestTimeoutMs: Type.Optional(Type.Integer({ minimum: 1000, maximum: 120000 })),
|
|
16
|
+
uploadRoots: Type.Optional(Type.Array(Type.String(), { description: "Extra absolute folders images may be uploaded from. The agent workspace and OpenClaw's media folder are always allowed." })),
|
|
17
|
+
}, { additionalProperties: false });
|
|
18
|
+
let cached;
|
|
19
|
+
/** One client per resolved config. */
|
|
20
|
+
export function clientFor(rawConfig, fetchImpl = fetch) {
|
|
21
|
+
const config = normalizeConfig((rawConfig ?? {}));
|
|
22
|
+
const key = JSON.stringify(config);
|
|
23
|
+
if (cached?.key !== key)
|
|
24
|
+
cached = { key, config, client: new StudioClient(config, fetchImpl) };
|
|
25
|
+
return cached;
|
|
26
|
+
}
|
|
27
|
+
export function resetClientCache() {
|
|
28
|
+
cached = undefined;
|
|
29
|
+
}
|
|
30
|
+
/** Records each change in OpenClaw's state database, without post bodies. */
|
|
31
|
+
async function audit(api, entry) {
|
|
32
|
+
const store = api?.runtime?.state?.openKeyedStoreV2?.({ namespace: "changes", maxEntries: 5000 });
|
|
33
|
+
if (!store)
|
|
34
|
+
return;
|
|
35
|
+
await store.register(`${String(Date.now()).padStart(15, "0")}-${crypto.randomUUID().slice(0, 8)}`, { at: new Date().toISOString(), ...entry });
|
|
36
|
+
}
|
|
37
|
+
const postId = Type.String({ description: "Post id from studio_posts_list or studio_post_create." });
|
|
38
|
+
const version = Type.Union([Type.Integer({ minimum: 0 }), Type.String({ minLength: 1 })], { description: "The post's current version, from the last read or save." });
|
|
39
|
+
const slug = Type.String({ minLength: 3, maxLength: 80, pattern: "^[a-z0-9]+(?:-[a-z0-9]+)*$", description: "Address on raseef.dev, lowercase letters, digits and dashes. Same for both languages." });
|
|
40
|
+
const title = (lang) => Type.String({ maxLength: 150, description: `Title in ${lang}, up to 150 characters.` });
|
|
41
|
+
const summary = (lang) => Type.String({ maxLength: 300, description: `Summary in ${lang}, up to 300 characters; shown in lists and search results.` });
|
|
42
|
+
const body = (lang) => Type.String({ maxLength: 100000, description: `Body in ${lang}, Studio Markdown (no HTML), starting with an H2. Images: a line .` });
|
|
43
|
+
const cover = Type.Union([Type.String(), Type.Null()], { description: "Image id from studio_media_upload, or null for no cover." });
|
|
44
|
+
const UPLOAD_DESCRIPTION = "Upload one PNG, JPEG or WebP image (under 2,000,000 bytes) to Studio with Arabic and English alt text. The file must be in the agent workspace or OpenClaw's media folder, such as an image_generate output; generate covers as jpeg or webp to stay under the limit. Returns the image id for cover_media_id or .";
|
|
45
|
+
const uploadParameters = Type.Object({
|
|
46
|
+
path: Type.String({ description: "Image path: absolute, or relative to the agent workspace." }),
|
|
47
|
+
alt_en: Type.String({ minLength: 1, maxLength: 300, description: "What the image shows, in English." }),
|
|
48
|
+
alt_ar: Type.String({ minLength: 1, maxLength: 300, description: "What the image shows, in Arabic." }),
|
|
49
|
+
});
|
|
50
|
+
const POST_FIELDS = ["slug", "title_en", "title_ar", "summary_en", "summary_ar", "body_en", "body_ar", "cover_media_id"];
|
|
51
|
+
function pick(params) {
|
|
52
|
+
const out = {};
|
|
53
|
+
for (const field of POST_FIELDS)
|
|
54
|
+
if (params[field] !== undefined)
|
|
55
|
+
out[field] = params[field];
|
|
56
|
+
if (typeof out.cover_media_id === "string")
|
|
57
|
+
out.cover_media_id = decodeURIComponent(studioId(out.cover_media_id, "cover_media_id"));
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
export default defineToolPlugin({
|
|
61
|
+
id: "raseef-studio",
|
|
62
|
+
name: "Raseef Studio",
|
|
63
|
+
description: "Reads and writes Raseef Studio blog posts and images through the Studio member API.",
|
|
64
|
+
configSchema,
|
|
65
|
+
tools: (tool) => [
|
|
66
|
+
tool({
|
|
67
|
+
name: "studio_me",
|
|
68
|
+
label: "Studio account",
|
|
69
|
+
description: "Show which Studio member the configured token belongs to (id, email, name, role).",
|
|
70
|
+
workspaceAccess: false,
|
|
71
|
+
parameters: Type.Object({}),
|
|
72
|
+
async execute(_params, config, context) {
|
|
73
|
+
return clientFor(config).client.request("GET", "/me", undefined, context.signal);
|
|
74
|
+
},
|
|
75
|
+
}),
|
|
76
|
+
tool({
|
|
77
|
+
name: "studio_posts_list",
|
|
78
|
+
label: "Studio posts",
|
|
79
|
+
description: "List Studio posts, newest first. Filter by text and status; page with limit and offset. Check every status before writing a new article to avoid duplicates.",
|
|
80
|
+
workspaceAccess: false,
|
|
81
|
+
parameters: Type.Object({
|
|
82
|
+
q: Type.Optional(Type.String({ description: "Text to search for." })),
|
|
83
|
+
status: Type.Optional(Type.Union([Type.Literal("draft"), Type.Literal("published"), Type.Literal("scheduled"), Type.Literal("archived")])),
|
|
84
|
+
limit: Type.Optional(Type.Integer({ minimum: 1, maximum: 100, description: "1 to 100 (default 10)." })),
|
|
85
|
+
offset: Type.Optional(Type.Integer({ minimum: 0 })),
|
|
86
|
+
}),
|
|
87
|
+
async execute(params, config, context) {
|
|
88
|
+
return clientFor(config).client.request("GET", `/posts${query(params)}`, undefined, context.signal);
|
|
89
|
+
},
|
|
90
|
+
}),
|
|
91
|
+
tool({
|
|
92
|
+
name: "studio_post_get",
|
|
93
|
+
label: "Studio post",
|
|
94
|
+
description: "Read one Studio post with both languages, its status and its current version.",
|
|
95
|
+
workspaceAccess: false,
|
|
96
|
+
parameters: Type.Object({ id: postId }),
|
|
97
|
+
async execute(params, config, context) {
|
|
98
|
+
return clientFor(config).client.request("GET", `/posts/${studioId(params.id)}`, undefined, context.signal);
|
|
99
|
+
},
|
|
100
|
+
}),
|
|
101
|
+
tool({
|
|
102
|
+
name: "studio_post_create",
|
|
103
|
+
label: "Studio post create",
|
|
104
|
+
description: "Create a new Studio post as a draft with both languages. Returns its id and version. To make it public, call studio_post_state with status published and that version.",
|
|
105
|
+
workspaceAccess: false,
|
|
106
|
+
parameters: Type.Object({
|
|
107
|
+
slug,
|
|
108
|
+
title_en: title("English"),
|
|
109
|
+
title_ar: title("Arabic"),
|
|
110
|
+
summary_en: summary("English"),
|
|
111
|
+
summary_ar: summary("Arabic"),
|
|
112
|
+
body_en: body("English"),
|
|
113
|
+
body_ar: body("Arabic"),
|
|
114
|
+
cover_media_id: Type.Optional(cover),
|
|
115
|
+
}),
|
|
116
|
+
async execute(params, config, context) {
|
|
117
|
+
const post = await clientFor(config).client.request("POST", "/posts", pick(params), context.signal);
|
|
118
|
+
await audit(context.api, { action: "post.create", postId: post?.id ?? null, slug: params.slug });
|
|
119
|
+
return post;
|
|
120
|
+
},
|
|
121
|
+
}),
|
|
122
|
+
tool({
|
|
123
|
+
name: "studio_post_update",
|
|
124
|
+
label: "Studio post update",
|
|
125
|
+
description: "Save changes to a Studio post. Send only the fields to change, with the version you last read. A version conflict means someone saved after you read it: read it again and retry.",
|
|
126
|
+
workspaceAccess: false,
|
|
127
|
+
parameters: Type.Object({
|
|
128
|
+
id: postId,
|
|
129
|
+
version,
|
|
130
|
+
slug: Type.Optional(slug),
|
|
131
|
+
title_en: Type.Optional(title("English")),
|
|
132
|
+
title_ar: Type.Optional(title("Arabic")),
|
|
133
|
+
summary_en: Type.Optional(summary("English")),
|
|
134
|
+
summary_ar: Type.Optional(summary("Arabic")),
|
|
135
|
+
body_en: Type.Optional(body("English")),
|
|
136
|
+
body_ar: Type.Optional(body("Arabic")),
|
|
137
|
+
cover_media_id: Type.Optional(cover),
|
|
138
|
+
}),
|
|
139
|
+
async execute(params, config, context) {
|
|
140
|
+
const fields = pick(params);
|
|
141
|
+
if (!Object.keys(fields).length)
|
|
142
|
+
throw new Error("nothing to change: send at least one field");
|
|
143
|
+
const post = await clientFor(config).client.request("POST", `/posts/${studioId(params.id)}`, { ...fields, version: params.version }, context.signal);
|
|
144
|
+
await audit(context.api, { action: "post.update", postId: params.id, fields: Object.keys(fields) });
|
|
145
|
+
return post;
|
|
146
|
+
},
|
|
147
|
+
}),
|
|
148
|
+
tool({
|
|
149
|
+
name: "studio_post_state",
|
|
150
|
+
label: "Studio post state",
|
|
151
|
+
description: "Publish a Studio post (now, or at publish_at), return it to draft, or archive it. Publishing needs a title, summary and body in both languages. Send the version you last read.",
|
|
152
|
+
workspaceAccess: false,
|
|
153
|
+
parameters: Type.Object({
|
|
154
|
+
id: postId,
|
|
155
|
+
version,
|
|
156
|
+
status: Type.Union([Type.Literal("published"), Type.Literal("draft"), Type.Literal("archived")]),
|
|
157
|
+
publish_at: Type.Optional(Type.String({ description: "ISO 8601 time to publish later; omit to publish now." })),
|
|
158
|
+
}),
|
|
159
|
+
async execute(params, config, context) {
|
|
160
|
+
if (params.publish_at !== undefined && (params.status !== "published" || Number.isNaN(Date.parse(params.publish_at)))) {
|
|
161
|
+
throw new Error("publish_at must be an ISO 8601 time and is only used with status published");
|
|
162
|
+
}
|
|
163
|
+
const payload = { version: params.version, status: params.status };
|
|
164
|
+
if (params.publish_at !== undefined)
|
|
165
|
+
payload.publish_at = params.publish_at;
|
|
166
|
+
const post = await clientFor(config).client.request("POST", `/posts/${studioId(params.id)}/state`, payload, context.signal);
|
|
167
|
+
await audit(context.api, { action: `post.${params.status}`, postId: params.id, publishAt: params.publish_at ?? null });
|
|
168
|
+
return post;
|
|
169
|
+
},
|
|
170
|
+
}),
|
|
171
|
+
tool({
|
|
172
|
+
name: "studio_post_revisions",
|
|
173
|
+
label: "Studio post revisions",
|
|
174
|
+
description: "List the saved versions of one Studio post.",
|
|
175
|
+
workspaceAccess: false,
|
|
176
|
+
parameters: Type.Object({
|
|
177
|
+
id: postId,
|
|
178
|
+
limit: Type.Optional(Type.Integer({ minimum: 1, maximum: 100 })),
|
|
179
|
+
offset: Type.Optional(Type.Integer({ minimum: 0 })),
|
|
180
|
+
}),
|
|
181
|
+
async execute(params, config, context) {
|
|
182
|
+
return clientFor(config).client.request("GET", `/posts/${studioId(params.id)}/revisions${query({ limit: params.limit, offset: params.offset })}`, undefined, context.signal);
|
|
183
|
+
},
|
|
184
|
+
}),
|
|
185
|
+
tool({
|
|
186
|
+
name: "studio_media_list",
|
|
187
|
+
label: "Studio images",
|
|
188
|
+
description: "List the Studio image library.",
|
|
189
|
+
workspaceAccess: false,
|
|
190
|
+
parameters: Type.Object({
|
|
191
|
+
limit: Type.Optional(Type.Integer({ minimum: 1, maximum: 100 })),
|
|
192
|
+
offset: Type.Optional(Type.Integer({ minimum: 0 })),
|
|
193
|
+
}),
|
|
194
|
+
async execute(params, config, context) {
|
|
195
|
+
return clientFor(config).client.request("GET", `/media${query(params)}`, undefined, context.signal);
|
|
196
|
+
},
|
|
197
|
+
}),
|
|
198
|
+
tool({
|
|
199
|
+
name: "studio_media_upload",
|
|
200
|
+
label: "Studio image upload",
|
|
201
|
+
description: UPLOAD_DESCRIPTION,
|
|
202
|
+
workspaceAccess: "execute",
|
|
203
|
+
parameters: uploadParameters,
|
|
204
|
+
factory({ api, config, toolContext }) {
|
|
205
|
+
return {
|
|
206
|
+
name: "studio_media_upload",
|
|
207
|
+
label: "Studio image upload",
|
|
208
|
+
description: UPLOAD_DESCRIPTION,
|
|
209
|
+
parameters: uploadParameters,
|
|
210
|
+
async execute(_toolCallId, params, signal) {
|
|
211
|
+
const state = clientFor(config);
|
|
212
|
+
const image = await readUploadImage(params.path, [toolContext.workspaceDir, openclawMediaDir(), ...state.config.uploadRoots]);
|
|
213
|
+
const media = await state.client.request("POST", "/media", { data: image.base64, alt_en: params.alt_en, alt_ar: params.alt_ar }, signal);
|
|
214
|
+
await audit(api, { action: "media.upload", mediaId: media?.id ?? null, bytes: image.bytes, mime: image.mime });
|
|
215
|
+
const details = { ...media, url: media?.id ? `https://raseef.dev/blog/media/${media.id}` : null };
|
|
216
|
+
return { content: [{ type: "text", text: JSON.stringify(details, null, 2) }], details };
|
|
217
|
+
},
|
|
218
|
+
};
|
|
219
|
+
},
|
|
220
|
+
}),
|
|
221
|
+
],
|
|
222
|
+
});
|
package/dist/media.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** Studio accepts up to 2 MB; stay under a conservative byte bound. */
|
|
2
|
+
export declare const MAX_IMAGE_BYTES = 2000000;
|
|
3
|
+
export type ImageMime = "image/png" | "image/jpeg" | "image/webp";
|
|
4
|
+
/** The image type from the file's first bytes, the same way Studio recognises it. */
|
|
5
|
+
export declare function sniffImage(bytes: Uint8Array): ImageMime | null;
|
|
6
|
+
/** OpenClaw's own media folder, where image_generate saves its outputs. */
|
|
7
|
+
export declare function openclawMediaDir(env?: NodeJS.ProcessEnv): string;
|
|
8
|
+
/**
|
|
9
|
+
* Reads one image for upload. The real path (after symlinks) must be inside one of the roots,
|
|
10
|
+
* the file must be a PNG, JPEG or WebP by its bytes, and under the size bound.
|
|
11
|
+
*/
|
|
12
|
+
export declare function readUploadImage(input: string, roots: (string | undefined)[]): Promise<{
|
|
13
|
+
path: string;
|
|
14
|
+
mime: ImageMime;
|
|
15
|
+
bytes: number;
|
|
16
|
+
base64: string;
|
|
17
|
+
}>;
|
package/dist/media.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
/** Studio accepts up to 2 MB; stay under a conservative byte bound. */
|
|
5
|
+
export const MAX_IMAGE_BYTES = 2_000_000;
|
|
6
|
+
/** The image type from the file's first bytes, the same way Studio recognises it. */
|
|
7
|
+
export function sniffImage(bytes) {
|
|
8
|
+
if (bytes.length >= 8 && bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4e && bytes[3] === 0x47 && bytes[4] === 0x0d && bytes[5] === 0x0a && bytes[6] === 0x1a && bytes[7] === 0x0a)
|
|
9
|
+
return "image/png";
|
|
10
|
+
if (bytes.length >= 3 && bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff)
|
|
11
|
+
return "image/jpeg";
|
|
12
|
+
if (bytes.length >= 12 && String.fromCharCode(...bytes.slice(0, 4)) === "RIFF" && String.fromCharCode(...bytes.slice(8, 12)) === "WEBP")
|
|
13
|
+
return "image/webp";
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
/** OpenClaw's own media folder, where image_generate saves its outputs. */
|
|
17
|
+
export function openclawMediaDir(env = process.env) {
|
|
18
|
+
const state = env.OPENCLAW_STATE_DIR?.trim() || path.join(os.homedir(), ".openclaw");
|
|
19
|
+
return path.join(state, "media");
|
|
20
|
+
}
|
|
21
|
+
async function realRoot(root) {
|
|
22
|
+
if (!root)
|
|
23
|
+
return null;
|
|
24
|
+
try {
|
|
25
|
+
return await fs.realpath(root);
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const inside = (file, root) => file === root || file.startsWith(root.endsWith(path.sep) ? root : root + path.sep);
|
|
32
|
+
/**
|
|
33
|
+
* Reads one image for upload. The real path (after symlinks) must be inside one of the roots,
|
|
34
|
+
* the file must be a PNG, JPEG or WebP by its bytes, and under the size bound.
|
|
35
|
+
*/
|
|
36
|
+
export async function readUploadImage(input, roots) {
|
|
37
|
+
const resolvedRoots = (await Promise.all(roots.map(realRoot))).filter((r) => Boolean(r));
|
|
38
|
+
if (!resolvedRoots.length)
|
|
39
|
+
throw new Error("no folder is available to upload images from");
|
|
40
|
+
const raw = String(input ?? "").trim();
|
|
41
|
+
if (!raw)
|
|
42
|
+
throw new Error("path is required");
|
|
43
|
+
const candidate = path.isAbsolute(raw) ? raw : path.join(resolvedRoots[0], raw);
|
|
44
|
+
let real;
|
|
45
|
+
try {
|
|
46
|
+
real = await fs.realpath(candidate);
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
throw new Error(`image not found: ${raw}`);
|
|
50
|
+
}
|
|
51
|
+
if (!resolvedRoots.some((root) => inside(real, root))) {
|
|
52
|
+
throw new Error(`image must be inside the agent workspace or OpenClaw's media folder: ${raw}`);
|
|
53
|
+
}
|
|
54
|
+
const stat = await fs.stat(real);
|
|
55
|
+
if (!stat.isFile())
|
|
56
|
+
throw new Error(`not a file: ${raw}`);
|
|
57
|
+
if (stat.size > MAX_IMAGE_BYTES) {
|
|
58
|
+
throw new Error(`image is ${stat.size} bytes; Studio needs under ${MAX_IMAGE_BYTES}. Generate it again as jpeg or webp with lower quality or size.`);
|
|
59
|
+
}
|
|
60
|
+
const bytes = await fs.readFile(real);
|
|
61
|
+
const mime = sniffImage(bytes);
|
|
62
|
+
if (!mime)
|
|
63
|
+
throw new Error(`not a PNG, JPEG or WebP image: ${raw}`);
|
|
64
|
+
return { path: real, mime, bytes: bytes.length, base64: bytes.toString("base64") };
|
|
65
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "raseef-studio",
|
|
3
|
+
"name": "Raseef Studio",
|
|
4
|
+
"description": "Reads and writes Raseef Studio blog posts and images through the Studio member API.",
|
|
5
|
+
"version": "1.0.0",
|
|
6
|
+
"activation": {
|
|
7
|
+
"onStartup": true
|
|
8
|
+
},
|
|
9
|
+
"configContracts": {
|
|
10
|
+
"secretInputs": {
|
|
11
|
+
"paths": [
|
|
12
|
+
{
|
|
13
|
+
"path": "apiToken",
|
|
14
|
+
"expected": "string",
|
|
15
|
+
"ownerKind": "capability"
|
|
16
|
+
}
|
|
17
|
+
]
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"configSchema": {
|
|
21
|
+
"type": "object",
|
|
22
|
+
"required": [
|
|
23
|
+
"apiToken"
|
|
24
|
+
],
|
|
25
|
+
"properties": {
|
|
26
|
+
"apiToken": {
|
|
27
|
+
"anyOf": [
|
|
28
|
+
{
|
|
29
|
+
"type": "string",
|
|
30
|
+
"minLength": 1
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
"type": "object",
|
|
34
|
+
"required": [
|
|
35
|
+
"source",
|
|
36
|
+
"provider",
|
|
37
|
+
"id"
|
|
38
|
+
],
|
|
39
|
+
"properties": {
|
|
40
|
+
"source": {
|
|
41
|
+
"anyOf": [
|
|
42
|
+
{
|
|
43
|
+
"type": "string",
|
|
44
|
+
"const": "env"
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
"type": "string",
|
|
48
|
+
"const": "file"
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
"type": "string",
|
|
52
|
+
"const": "exec"
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"type": "string",
|
|
56
|
+
"const": "store"
|
|
57
|
+
}
|
|
58
|
+
]
|
|
59
|
+
},
|
|
60
|
+
"provider": {
|
|
61
|
+
"type": "string"
|
|
62
|
+
},
|
|
63
|
+
"id": {
|
|
64
|
+
"type": "string"
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
"additionalProperties": false
|
|
68
|
+
}
|
|
69
|
+
],
|
|
70
|
+
"description": "Studio member API token (raseef_studio_…), from the admin console's Studio team page."
|
|
71
|
+
},
|
|
72
|
+
"baseUrl": {
|
|
73
|
+
"type": "string",
|
|
74
|
+
"description": "Studio API root. Defaults to https://studio.raseef.dev/api/v1."
|
|
75
|
+
},
|
|
76
|
+
"requestTimeoutMs": {
|
|
77
|
+
"type": "integer",
|
|
78
|
+
"minimum": 1000,
|
|
79
|
+
"maximum": 120000
|
|
80
|
+
},
|
|
81
|
+
"uploadRoots": {
|
|
82
|
+
"type": "array",
|
|
83
|
+
"items": {
|
|
84
|
+
"type": "string"
|
|
85
|
+
},
|
|
86
|
+
"description": "Extra absolute folders images may be uploaded from. The agent workspace and OpenClaw's media folder are always allowed."
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
"additionalProperties": false
|
|
90
|
+
},
|
|
91
|
+
"contracts": {
|
|
92
|
+
"tools": [
|
|
93
|
+
"studio_me",
|
|
94
|
+
"studio_posts_list",
|
|
95
|
+
"studio_post_get",
|
|
96
|
+
"studio_post_create",
|
|
97
|
+
"studio_post_update",
|
|
98
|
+
"studio_post_state",
|
|
99
|
+
"studio_post_revisions",
|
|
100
|
+
"studio_media_list",
|
|
101
|
+
"studio_media_upload"
|
|
102
|
+
]
|
|
103
|
+
}
|
|
104
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@raseef/openclaw-raseef-studio",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Raseef Studio posts and images for OpenClaw agents: read the inventory, upload images, create and edit bilingual posts, and publish them, with the token 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
|
+
}
|