@rine-kit/plugin-sdk 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/dist/index.d.ts +96 -0
- package/dist/index.js +92 -0
- package/package.json +35 -3
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ChristmasFox / Arthur Xu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* What an AI plugin looks like to the host that runs it. A plugin knows nothing about the host
|
|
4
|
+
* (RINE's API, LiveKit, OpenClaw, ...) beyond the trusted context type `Ctx` it asks for.
|
|
5
|
+
*
|
|
6
|
+
* The rule that keeps multi-tenant hosts safe: **the model never decides who is acting.** `Ctx` is
|
|
7
|
+
* built by the host from authenticated data (who is asking, which room / tenant / team) and handed
|
|
8
|
+
* to `run`; it must not appear in a tool's `input` schema, which is all the model can write. A tool
|
|
9
|
+
* can therefore only touch what that context allows.
|
|
10
|
+
*/
|
|
11
|
+
export interface ToolDefinition<Ctx = unknown, S extends z.ZodType = z.ZodType> {
|
|
12
|
+
/** What the model calls it: snake_case, unique across the plugins a host loads. */
|
|
13
|
+
name: string;
|
|
14
|
+
/** Told to the model: when to use the tool and what it does. */
|
|
15
|
+
description: string;
|
|
16
|
+
/** The arguments the model may write; they are checked against this before `run`. */
|
|
17
|
+
input: S;
|
|
18
|
+
/**
|
|
19
|
+
* Does the work for the principal in `ctx`. The result is shown to the model as JSON; a throw is
|
|
20
|
+
* turned into `{error}` data by the plugin's `toolError` (default: a generic message), not a crash.
|
|
21
|
+
*/
|
|
22
|
+
run(input: z.output<S>, ctx: Ctx, options: {
|
|
23
|
+
signal?: AbortSignal;
|
|
24
|
+
}): Promise<unknown>;
|
|
25
|
+
}
|
|
26
|
+
export interface Plugin<Ctx = unknown> {
|
|
27
|
+
/** Stable, namespaced id such as "rine.reminders". */
|
|
28
|
+
id: string;
|
|
29
|
+
/** Added to the system prompt while the plugin is loaded: when and how to use its tools. */
|
|
30
|
+
instructions?: string;
|
|
31
|
+
tools: ToolDefinition<Ctx>[];
|
|
32
|
+
/**
|
|
33
|
+
* Memory hook: background the plugin knows that is relevant to what was just asked, as short plain
|
|
34
|
+
* lines. The host shows them to the model as untrusted reference data, never as instructions. Like
|
|
35
|
+
* tools it works for the principal in `ctx` only; it must be quick (a slow or failing hook is skipped).
|
|
36
|
+
*/
|
|
37
|
+
recall?(query: string, ctx: Ctx, options: {
|
|
38
|
+
signal?: AbortSignal;
|
|
39
|
+
}): Promise<string[]>;
|
|
40
|
+
/**
|
|
41
|
+
* Memory hook: told after a turn finished normally, with what the member asked and what the AI
|
|
42
|
+
* answered. For learning from conversation; best effort, never delays or fails the answer.
|
|
43
|
+
*/
|
|
44
|
+
observe?(turn: {
|
|
45
|
+
query: string;
|
|
46
|
+
answer: string;
|
|
47
|
+
}, ctx: Ctx, options: {
|
|
48
|
+
signal?: AbortSignal;
|
|
49
|
+
}): Promise<void>;
|
|
50
|
+
/** Turns an error thrown by one of this plugin's tools into the data the model reads. */
|
|
51
|
+
toolError?(err: unknown): unknown;
|
|
52
|
+
}
|
|
53
|
+
/** Declares a plugin; only fixes the type of its trusted context. */
|
|
54
|
+
export declare function definePlugin<Ctx>(plugin: Plugin<Ctx>): Plugin<Ctx>;
|
|
55
|
+
/**
|
|
56
|
+
* `const tool = defineTool<MyCtx>();` then `tool({ ... })`: the context type is given once and the
|
|
57
|
+
* input type is inferred from the schema.
|
|
58
|
+
*/
|
|
59
|
+
export declare function defineTool<Ctx>(): <S extends z.ZodType>(def: ToolDefinition<Ctx, S>) => ToolDefinition<Ctx>;
|
|
60
|
+
export declare const DEFAULT_TOOL_ERROR: {
|
|
61
|
+
error: string;
|
|
62
|
+
};
|
|
63
|
+
/** The plugins a host has loaded, with their tool names checked for clashes. */
|
|
64
|
+
export declare class PluginRegistry<Ctx = unknown> {
|
|
65
|
+
private readonly plugins;
|
|
66
|
+
private readonly owners;
|
|
67
|
+
register(plugin: Plugin<Ctx>): this;
|
|
68
|
+
get pluginIds(): string[];
|
|
69
|
+
get toolCount(): number;
|
|
70
|
+
/** Prompt fragments of the loaded plugins, in registration order. */
|
|
71
|
+
instructions(): string[];
|
|
72
|
+
/**
|
|
73
|
+
* The lines the plugins recall for this question. A hook that fails or takes longer than
|
|
74
|
+
* `timeoutMs` contributes nothing (`onError` hears about it): memory is a help, never a reason to
|
|
75
|
+
* fail or delay an answer.
|
|
76
|
+
*/
|
|
77
|
+
recall(query: string, ctx: Ctx, options?: {
|
|
78
|
+
signal?: AbortSignal;
|
|
79
|
+
timeoutMs?: number;
|
|
80
|
+
onError?: (pluginId: string, err: unknown) => void;
|
|
81
|
+
}): Promise<string[]>;
|
|
82
|
+
/** Tells the plugins' observe hooks about a finished turn. A hook that fails or is slow is skipped. */
|
|
83
|
+
observe(turn: {
|
|
84
|
+
query: string;
|
|
85
|
+
answer: string;
|
|
86
|
+
}, ctx: Ctx, options?: {
|
|
87
|
+
signal?: AbortSignal;
|
|
88
|
+
timeoutMs?: number;
|
|
89
|
+
onError?: (pluginId: string, err: unknown) => void;
|
|
90
|
+
}): Promise<void>;
|
|
91
|
+
/** Every tool with the function that makes its failures into model-readable data. */
|
|
92
|
+
tools(): {
|
|
93
|
+
tool: ToolDefinition<Ctx>;
|
|
94
|
+
toolError: (err: unknown) => unknown;
|
|
95
|
+
}[];
|
|
96
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/** Declares a plugin; only fixes the type of its trusted context. */
|
|
2
|
+
export function definePlugin(plugin) {
|
|
3
|
+
return plugin;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* `const tool = defineTool<MyCtx>();` then `tool({ ... })`: the context type is given once and the
|
|
7
|
+
* input type is inferred from the schema.
|
|
8
|
+
*/
|
|
9
|
+
export function defineTool() {
|
|
10
|
+
return (def) => def;
|
|
11
|
+
}
|
|
12
|
+
export const DEFAULT_TOOL_ERROR = { error: 'the tool failed' };
|
|
13
|
+
/** The plugins a host has loaded, with their tool names checked for clashes. */
|
|
14
|
+
export class PluginRegistry {
|
|
15
|
+
plugins = new Map();
|
|
16
|
+
owners = new Map();
|
|
17
|
+
register(plugin) {
|
|
18
|
+
if (this.plugins.has(plugin.id))
|
|
19
|
+
throw new Error(`plugin ${plugin.id} is already registered`);
|
|
20
|
+
for (const tool of plugin.tools) {
|
|
21
|
+
const taken = this.owners.get(tool.name);
|
|
22
|
+
if (taken)
|
|
23
|
+
throw new Error(`tool ${tool.name} of ${plugin.id} is already provided by ${taken.plugin.id}`);
|
|
24
|
+
}
|
|
25
|
+
this.plugins.set(plugin.id, plugin);
|
|
26
|
+
for (const tool of plugin.tools)
|
|
27
|
+
this.owners.set(tool.name, { plugin, tool });
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
get pluginIds() {
|
|
31
|
+
return [...this.plugins.keys()];
|
|
32
|
+
}
|
|
33
|
+
get toolCount() {
|
|
34
|
+
return this.owners.size;
|
|
35
|
+
}
|
|
36
|
+
/** Prompt fragments of the loaded plugins, in registration order. */
|
|
37
|
+
instructions() {
|
|
38
|
+
return [...this.plugins.values()].flatMap((p) => (p.instructions ? [p.instructions] : []));
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The lines the plugins recall for this question. A hook that fails or takes longer than
|
|
42
|
+
* `timeoutMs` contributes nothing (`onError` hears about it): memory is a help, never a reason to
|
|
43
|
+
* fail or delay an answer.
|
|
44
|
+
*/
|
|
45
|
+
async recall(query, ctx, options = {}) {
|
|
46
|
+
const timeoutMs = options.timeoutMs ?? 1500;
|
|
47
|
+
const hooks = [...this.plugins.values()].filter((p) => p.recall);
|
|
48
|
+
const lines = await Promise.all(hooks.map(async (plugin) => {
|
|
49
|
+
const timeout = AbortSignal.timeout(timeoutMs);
|
|
50
|
+
const signal = options.signal ? AbortSignal.any([timeout, options.signal]) : timeout;
|
|
51
|
+
try {
|
|
52
|
+
const result = await Promise.race([
|
|
53
|
+
plugin.recall(query, ctx, { signal }),
|
|
54
|
+
new Promise((_, reject) => signal.addEventListener('abort', () => reject(signal.reason), { once: true })),
|
|
55
|
+
]);
|
|
56
|
+
return result.filter((l) => typeof l === 'string');
|
|
57
|
+
}
|
|
58
|
+
catch (err) {
|
|
59
|
+
if (!options.signal?.aborted)
|
|
60
|
+
options.onError?.(plugin.id, err);
|
|
61
|
+
return [];
|
|
62
|
+
}
|
|
63
|
+
}));
|
|
64
|
+
return lines.flat();
|
|
65
|
+
}
|
|
66
|
+
/** Tells the plugins' observe hooks about a finished turn. A hook that fails or is slow is skipped. */
|
|
67
|
+
async observe(turn, ctx, options = {}) {
|
|
68
|
+
const timeoutMs = options.timeoutMs ?? 5000;
|
|
69
|
+
await Promise.all([...this.plugins.values()]
|
|
70
|
+
.filter((p) => p.observe)
|
|
71
|
+
.map(async (plugin) => {
|
|
72
|
+
const timeout = AbortSignal.timeout(timeoutMs);
|
|
73
|
+
const signal = options.signal ? AbortSignal.any([timeout, options.signal]) : timeout;
|
|
74
|
+
try {
|
|
75
|
+
await Promise.race([
|
|
76
|
+
plugin.observe(turn, ctx, { signal }),
|
|
77
|
+
new Promise((_, reject) => signal.addEventListener('abort', () => reject(signal.reason), { once: true })),
|
|
78
|
+
]);
|
|
79
|
+
}
|
|
80
|
+
catch (err) {
|
|
81
|
+
options.onError?.(plugin.id, err);
|
|
82
|
+
}
|
|
83
|
+
}));
|
|
84
|
+
}
|
|
85
|
+
/** Every tool with the function that makes its failures into model-readable data. */
|
|
86
|
+
tools() {
|
|
87
|
+
return [...this.owners.values()].map(({ plugin, tool }) => ({
|
|
88
|
+
tool,
|
|
89
|
+
toolError: (err) => (plugin.toolError ? plugin.toolError(err) : DEFAULT_TOOL_ERROR),
|
|
90
|
+
}));
|
|
91
|
+
}
|
|
92
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rine-kit/plugin-sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Contract for AI plugins: tools and prompt instructions a host can run for its own trusted users. No dependency on any host.",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"import": "./dist/index.js",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"zod": "^4.6.7"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@types/node": "^26.6.5",
|
|
18
|
+
"typescript": "^5.9.2",
|
|
19
|
+
"vitest": "^5.0.5"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+ssh://git@github.com/ChristmasFox/rine-kit.git",
|
|
25
|
+
"directory": "packages/plugin-sdk"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist"
|
|
32
|
+
],
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsc -p tsconfig.build.json",
|
|
35
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
36
|
+
"test": "vitest run"
|
|
37
|
+
}
|
|
6
38
|
}
|
package/README.md
DELETED