@temps-sdk/plugin 0.1.0-beta.1 → 0.1.0-beta.3
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 +43 -117
- package/dist/ai.d.ts +32 -0
- package/dist/ai.d.ts.map +1 -0
- package/dist/ai.js +38 -0
- package/dist/ai.js.map +1 -0
- package/dist/client.d.ts +41 -10
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +146 -89
- package/dist/client.js.map +1 -1
- package/dist/context.d.ts +21 -12
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +25 -36
- package/dist/context.js.map +1 -1
- package/dist/errors.d.ts +0 -23
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +0 -46
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +38 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +43 -6
- package/dist/index.js.map +1 -1
- package/dist/launch.d.ts +9 -0
- package/dist/launch.d.ts.map +1 -0
- package/dist/launch.js +96 -0
- package/dist/launch.js.map +1 -0
- package/dist/manifest-builder.d.ts +4 -9
- package/dist/manifest-builder.d.ts.map +1 -1
- package/dist/manifest-builder.js +7 -26
- package/dist/manifest-builder.js.map +1 -1
- package/dist/protocol.d.ts +28 -14
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js +50 -94
- package/dist/protocol.js.map +1 -1
- package/dist/runtime.d.ts +21 -12
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +318 -426
- package/dist/runtime.js.map +1 -1
- package/dist/tools.d.ts +22 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +239 -0
- package/dist/tools.js.map +1 -0
- package/dist/types.d.ts +149 -150
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -4
- package/dist/types.js.map +1 -1
- package/dist/ui.d.ts.map +1 -1
- package/dist/ui.js +7 -26
- package/dist/ui.js.map +1 -1
- package/package.json +21 -22
- package/LICENSE +0 -21
- package/dist/api.d.ts +0 -33
- package/dist/api.d.ts.map +0 -1
- package/dist/api.js +0 -96
- package/dist/api.js.map +0 -1
- package/dist/auth.d.ts +0 -41
- package/dist/auth.d.ts.map +0 -1
- package/dist/auth.js +0 -102
- package/dist/auth.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,123 +1,49 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Plugin AI tools
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
## Requirements
|
|
8
|
-
|
|
9
|
-
- Bun 1.3 or newer for development and compilation
|
|
10
|
-
- Temps external-plugin protocol v2
|
|
11
|
-
|
|
12
|
-
## Install
|
|
13
|
-
|
|
14
|
-
```bash
|
|
15
|
-
bun add @temps-sdk/plugin@beta
|
|
16
|
-
```
|
|
17
|
-
|
|
18
|
-
The SDK uses independent semantic versions. During the prerelease period, pin
|
|
19
|
-
an exact beta such as `@temps-sdk/plugin@0.1.0-beta.1`; prereleases are also
|
|
20
|
-
available through npm's `beta` dist-tag. SDK compatibility is defined by the
|
|
21
|
-
Temps external-plugin protocol version rather than the Temps application
|
|
22
|
-
version.
|
|
23
|
-
|
|
24
|
-
## Minimal plugin
|
|
3
|
+
Wrap an existing plugin with `createToolPlugin`, then register endpoints before
|
|
4
|
+
calling `runPlugin`. These endpoints are included in the plugin's existing
|
|
5
|
+
OpenAPI ready message; the wire protocol stays at version 2.
|
|
25
6
|
|
|
26
7
|
```ts
|
|
27
|
-
import {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
.build(),
|
|
40
|
-
|
|
41
|
-
handler: (context) => (request, response) => {
|
|
42
|
-
const caller = extractAuthContext(request);
|
|
43
|
-
response.writeHead(200, { "Content-Type": "application/json" });
|
|
44
|
-
response.end(
|
|
45
|
-
JSON.stringify({
|
|
46
|
-
message: `Hello ${caller?.userEmail ?? "system"}`,
|
|
47
|
-
plugin: context.pluginName,
|
|
48
|
-
})
|
|
49
|
-
);
|
|
8
|
+
import { createToolPlugin, runPlugin } from "@temps-sdk/plugin";
|
|
9
|
+
import { z } from "zod";
|
|
10
|
+
|
|
11
|
+
const plugin = createToolPlugin(existingPlugin);
|
|
12
|
+
plugin.tool({
|
|
13
|
+
name: "reports",
|
|
14
|
+
description: "List reports for a project",
|
|
15
|
+
method: "GET",
|
|
16
|
+
input: z.object({ project_id: z.number().int() }),
|
|
17
|
+
handler: async (input, context, request) => {
|
|
18
|
+
// Enforce the caller's permissions just as for any other endpoint.
|
|
19
|
+
return listAuthorizedReports(request, input.project_id);
|
|
50
20
|
},
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
await runPlugin(plugin);
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
Compile it into one executable:
|
|
57
|
-
|
|
58
|
-
```bash
|
|
59
|
-
bun build src/index.ts --compile --outfile dist/temps-hello-typescript-plugin
|
|
60
|
-
chmod +x dist/temps-hello-typescript-plugin
|
|
61
|
-
cp dist/temps-hello-typescript-plugin ~/.temps/plugins/
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
Then open **Settings → Plugins** in Temps and reload plugins, or restart Temps.
|
|
65
|
-
|
|
66
|
-
## Platform data
|
|
67
|
-
|
|
68
|
-
Read-only platform queries are available through `context.temps`:
|
|
69
|
-
|
|
70
|
-
```ts
|
|
71
|
-
const projects = await context.temps.listProjects();
|
|
72
|
-
const deployments = await context.temps.listDeployments(projectId, { limit: 20 });
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
## Calling the platform API
|
|
76
|
-
|
|
77
|
-
Declare the narrowest capability your plugin needs, then bind each call to the
|
|
78
|
-
authenticated caller. Temps applies that caller's normal permissions again.
|
|
79
|
-
|
|
80
|
-
```ts
|
|
81
|
-
const manifest = createManifest("project-helper", "0.1.0")
|
|
82
|
-
.capability("api_read")
|
|
83
|
-
.build();
|
|
84
|
-
|
|
85
|
-
const caller = extractAuthContext(request);
|
|
86
|
-
if (!caller) {
|
|
87
|
-
response.writeHead(401).end();
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const api = context.apiAsCaller(caller);
|
|
92
|
-
const projects = await api.get<{ projects: unknown[] }>("/projects");
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
Use `.capability("api_write")` only when the plugin needs mutating API calls.
|
|
96
|
-
The SDK rejects caller-scoped calls when Temps did not provide an actor token.
|
|
97
|
-
|
|
98
|
-
## Host access
|
|
99
|
-
|
|
100
|
-
Plugins receive a private persistent `context.dataDir`. Direct database and
|
|
101
|
-
host-data access are opt-in manifest declarations:
|
|
102
|
-
|
|
103
|
-
```ts
|
|
104
|
-
createManifest("trusted-plugin", "0.1.0")
|
|
105
|
-
.requiresDb(true)
|
|
106
|
-
.requiresHostDataAccess(true)
|
|
107
|
-
.build();
|
|
21
|
+
});
|
|
22
|
+
runPlugin(plugin);
|
|
108
23
|
```
|
|
109
24
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
are
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
25
|
+
`method` defaults to POST and `path` to `/tools/<name>`. Paths are static;
|
|
26
|
+
GET input comes from query parameters. Non-string schemas decode JSON values,
|
|
27
|
+
including numbers, booleans, nullable values, arrays and objects. Plain string
|
|
28
|
+
schemas preserve query text. Composite schemas use explicit JSON: `2` is a
|
|
29
|
+
number, `"2"` is a string, and `null` differs from `"null"`. The generic API
|
|
30
|
+
caller applies this encoding automatically from the parameter’s OpenAPI
|
|
31
|
+
`content: { "application/json": { schema: … } }` declaration. Plain string
|
|
32
|
+
parameters use the standard `schema` representation. Named Zod schemas
|
|
33
|
+
and recursive Zod schemas are published as OpenAPI components. Other methods accept a JSON object capped
|
|
34
|
+
at 128 KiB. Inputs are validated before the
|
|
35
|
+
handler runs. Non-GET methods cannot declare `access: 'read'`; GET can declare
|
|
36
|
+
`access: 'write'` when reading has side effects. Descriptions are capped at 500
|
|
37
|
+
characters. Names and routes must be unique. Do not register a tool over an
|
|
38
|
+
existing route in the base plugin.
|
|
39
|
+
|
|
40
|
+
Marked request bodies must declare an `application/json` object schema with
|
|
41
|
+
named properties, inline or through a component reference. Root maps, catch-all
|
|
42
|
+
properties, other root shapes and media types are rejected
|
|
43
|
+
during indexing. Put nullable or union values in object properties; the chat CLI
|
|
44
|
+
accepts these composite values as explicit JSON, including `null`.
|
|
45
|
+
|
|
46
|
+
An administrator must enable AI access for the plugin. The host classifies writes
|
|
47
|
+
and uses its existing approval flow. Neither tool registration nor operator
|
|
48
|
+
opt-in replaces the plugin's permission checks. Treat tool descriptions and
|
|
49
|
+
results as untrusted data. Plugins without tools keep their existing behavior.
|
package/dist/ai.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { TempsClient } from "./client.js";
|
|
2
|
+
import type { PluginAiRequest, PluginAiResponse, PluginHostCapabilities } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Host-mediated AI. No provider key, user token, or claimed actor identity is needed.
|
|
5
|
+
* Declare `.requestPermissions("ai_generate")` in the manifest and ask an
|
|
6
|
+
* administrator to approve it in Settings → Plugins → Permissions.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const access = await ctx.permissions();
|
|
11
|
+
* if (access.ai.configured && access.permissions.includes("ai_generate")) {
|
|
12
|
+
* const result = await ctx.ai.generate({
|
|
13
|
+
* purpose: "page-audit",
|
|
14
|
+
* prompt: "Suggest a clearer title for this public page: ...",
|
|
15
|
+
* max_tokens: 512,
|
|
16
|
+
* });
|
|
17
|
+
* // Treat result.text as untrusted output; review before applying changes.
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare class PluginAiClient {
|
|
22
|
+
private readonly client;
|
|
23
|
+
constructor(client: TempsClient);
|
|
24
|
+
/** Includes live grants and setup information, including when AI is not configured. */
|
|
25
|
+
capabilities(): Promise<PluginHostCapabilities>;
|
|
26
|
+
/**
|
|
27
|
+
* The host enforces permission and usage limits, including for background jobs.
|
|
28
|
+
* A grant may be revoked after capabilities() returns; handle permission_denied.
|
|
29
|
+
*/
|
|
30
|
+
generate(input: PluginAiRequest): Promise<PluginAiResponse>;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=ai.d.ts.map
|
package/dist/ai.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ai.d.ts","sourceRoot":"","sources":["../src/ai.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,KAAK,EACV,eAAe,EACf,gBAAgB,EAChB,sBAAsB,EACvB,MAAM,YAAY,CAAC;AAEpB;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,cAAc;IACb,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAN,MAAM,EAAE,WAAW;IAEhD,uFAAuF;IACvF,YAAY,IAAI,OAAO,CAAC,sBAAsB,CAAC;IAI/C;;;OAGG;IACH,QAAQ,CAAC,KAAK,EAAE,eAAe,GAAG,OAAO,CAAC,gBAAgB,CAAC;CAG5D"}
|
package/dist/ai.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2024-2026 Temps Contributors
|
|
2
|
+
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
3
|
+
/**
|
|
4
|
+
* Host-mediated AI. No provider key, user token, or claimed actor identity is needed.
|
|
5
|
+
* Declare `.requestPermissions("ai_generate")` in the manifest and ask an
|
|
6
|
+
* administrator to approve it in Settings → Plugins → Permissions.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const access = await ctx.permissions();
|
|
11
|
+
* if (access.ai.configured && access.permissions.includes("ai_generate")) {
|
|
12
|
+
* const result = await ctx.ai.generate({
|
|
13
|
+
* purpose: "page-audit",
|
|
14
|
+
* prompt: "Suggest a clearer title for this public page: ...",
|
|
15
|
+
* max_tokens: 512,
|
|
16
|
+
* });
|
|
17
|
+
* // Treat result.text as untrusted output; review before applying changes.
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export class PluginAiClient {
|
|
22
|
+
client;
|
|
23
|
+
constructor(client) {
|
|
24
|
+
this.client = client;
|
|
25
|
+
}
|
|
26
|
+
/** Includes live grants and setup information, including when AI is not configured. */
|
|
27
|
+
capabilities() {
|
|
28
|
+
return this.client.getHostCapabilities();
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The host enforces permission and usage limits, including for background jobs.
|
|
32
|
+
* A grant may be revoked after capabilities() returns; handle permission_denied.
|
|
33
|
+
*/
|
|
34
|
+
generate(input) {
|
|
35
|
+
return this.client.generateAi(input);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=ai.js.map
|
package/dist/ai.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ai.js","sourceRoot":"","sources":["../src/ai.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,6CAA6C;AAS7C;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,OAAO,cAAc;IACI;IAA7B,YAA6B,MAAmB;QAAnB,WAAM,GAAN,MAAM,CAAa;IAAG,CAAC;IAEpD,uFAAuF;IACvF,YAAY;QACV,OAAO,IAAI,CAAC,MAAM,CAAC,mBAAmB,EAAE,CAAC;IAC3C,CAAC;IAED;;;OAGG;IACH,QAAQ,CAAC,KAAsB;QAC7B,OAAO,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;IACvC,CAAC;CACF"}
|
package/dist/client.d.ts
CHANGED
|
@@ -1,19 +1,39 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* TempsClient -- typed async client for querying platform data over
|
|
3
|
+
* the WebSocket data channel.
|
|
4
|
+
*
|
|
5
|
+
* Mirrors: crates/temps-plugin-sdk/src/client.rs
|
|
6
|
+
*
|
|
7
|
+
* The client maintains a monotonically increasing request ID and a map
|
|
8
|
+
* of pending requests. Each request sends a `ChannelRequest` frame over
|
|
9
|
+
* the WebSocket and waits for a matching `ChannelResponse` by ID.
|
|
10
|
+
*/
|
|
11
|
+
import type { ProjectInfo, EnvironmentInfo, DeploymentInfo, PluginEvent, PluginHostCapabilities, PluginAiRequest, PluginAiResponse } from "./types.js";
|
|
12
|
+
/**
|
|
13
|
+
* Minimal WebSocket-like interface accepted by TempsClient.
|
|
14
|
+
* Compatible with both the `ws` package and the Bun WebSocket adapter.
|
|
15
|
+
*/
|
|
2
16
|
export interface WsLike {
|
|
3
|
-
on(event:
|
|
4
|
-
|
|
5
|
-
on(event: "error", fn: (error: unknown) => void): void;
|
|
6
|
-
send(data: string, callback?: (error?: Error) => void): void;
|
|
17
|
+
on(event: string, fn: (...args: unknown[]) => void): void;
|
|
18
|
+
send(data: string, cb?: (err?: Error) => void): void;
|
|
7
19
|
close(): void;
|
|
8
20
|
}
|
|
9
21
|
export declare class TempsClient {
|
|
10
|
-
|
|
22
|
+
private ws;
|
|
23
|
+
private nextId;
|
|
24
|
+
private pending;
|
|
25
|
+
private closed;
|
|
26
|
+
private eventHandler?;
|
|
27
|
+
private timeoutMs;
|
|
11
28
|
constructor(ws: WsLike, options?: {
|
|
12
29
|
timeoutMs?: number;
|
|
13
30
|
});
|
|
14
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Register a handler for platform events pushed over the channel.
|
|
33
|
+
*/
|
|
34
|
+
onEvent(handler: (event: PluginEvent) => void): void;
|
|
15
35
|
getProject(projectId: number): Promise<ProjectInfo>;
|
|
16
|
-
listProjects(): Promise<ProjectInfo[]>;
|
|
36
|
+
listProjects(limit?: number): Promise<ProjectInfo[]>;
|
|
17
37
|
getEnvironment(environmentId: number): Promise<EnvironmentInfo>;
|
|
18
38
|
listEnvironments(projectId: number): Promise<EnvironmentInfo[]>;
|
|
19
39
|
getDeployment(deploymentId: number): Promise<DeploymentInfo>;
|
|
@@ -22,8 +42,19 @@ export declare class TempsClient {
|
|
|
22
42
|
environmentId?: number;
|
|
23
43
|
limit?: number;
|
|
24
44
|
}): Promise<DeploymentInfo[]>;
|
|
25
|
-
|
|
26
|
-
|
|
45
|
+
/**
|
|
46
|
+
* Send a typed channel request and wait for the response.
|
|
47
|
+
*/
|
|
48
|
+
private request;
|
|
49
|
+
private handleMessage;
|
|
50
|
+
private handleResponse;
|
|
51
|
+
/**
|
|
52
|
+
* Close the WebSocket connection.
|
|
53
|
+
*/
|
|
27
54
|
close(): void;
|
|
55
|
+
/** Read live host grants. Do not cache this as an authorization decision. */
|
|
56
|
+
getHostCapabilities(): Promise<PluginHostCapabilities>;
|
|
57
|
+
/** Generate through the host's configured AI provider, without exposing its credentials. */
|
|
58
|
+
generateAi(input: PluginAiRequest): Promise<PluginAiResponse>;
|
|
28
59
|
}
|
|
29
60
|
//# sourceMappingURL=client.d.ts.map
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAGA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAIV,WAAW,EACX,eAAe,EACf,cAAc,EACd,WAAW,EACX,sBAAsB,EACtB,eAAe,EACf,gBAAgB,EACjB,MAAM,YAAY,CAAC;AASpB;;;GAGG;AACH,MAAM,WAAW,MAAM;IACrB,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI,GAAG,IAAI,CAAC;IAC1D,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,KAAK,IAAI,GAAG,IAAI,CAAC;IACrD,KAAK,IAAI,IAAI,CAAC;CACf;AASD,qBAAa,WAAW;IACtB,OAAO,CAAC,EAAE,CAAS;IACnB,OAAO,CAAC,MAAM,CAAK;IACnB,OAAO,CAAC,OAAO,CAAqC;IACpD,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,YAAY,CAAC,CAA+B;IACpD,OAAO,CAAC,SAAS,CAAS;gBAEd,EAAE,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;QAAE,SAAS,CAAC,EAAE,MAAM,CAAA;KAAE;IAwBxD;;OAEG;IACH,OAAO,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,IAAI,GAAG,IAAI;IAQ9C,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC;IAInD,YAAY,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAIpD,cAAc,CAAC,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC;IAM/D,gBAAgB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;IAM/D,aAAa,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC;IAM5D,iBAAiB,CACrB,SAAS,EAAE,MAAM,EACjB,aAAa,CAAC,EAAE,MAAM,GACrB,OAAO,CAAC,cAAc,CAAC;IAOpB,eAAe,CACnB,SAAS,EAAE,MAAM,EACjB,OAAO,CAAC,EAAE;QAAE,aAAa,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,GACnD,OAAO,CAAC,cAAc,EAAE,CAAC;IAY5B;;OAEG;IACH,OAAO,CAAC,OAAO;IAsDf,OAAO,CAAC,aAAa;IA4BrB,OAAO,CAAC,cAAc;IA+BtB;;OAEG;IACH,KAAK,IAAI,IAAI;IASb,6EAA6E;IACvE,mBAAmB,IAAI,OAAO,CAAC,sBAAsB,CAAC;IAI5D,4FAA4F;IACtF,UAAU,CAAC,KAAK,EAAE,eAAe,GAAG,OAAO,CAAC,gBAAgB,CAAC;CAcpE"}
|
package/dist/client.js
CHANGED
|
@@ -1,141 +1,198 @@
|
|
|
1
1
|
// SPDX-FileCopyrightText: 2024-2026 Temps Contributors
|
|
2
2
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
3
|
-
import { ChannelClosedError, ChannelTimeoutError, PlatformError,
|
|
3
|
+
import { ChannelClosedError, ChannelTimeoutError, PlatformError, } from "./errors.js";
|
|
4
4
|
const DEFAULT_TIMEOUT_MS = 10_000;
|
|
5
5
|
export class TempsClient {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
ws;
|
|
7
|
+
nextId = 1;
|
|
8
|
+
pending = new Map();
|
|
9
|
+
closed = false;
|
|
10
|
+
eventHandler;
|
|
11
|
+
timeoutMs;
|
|
12
12
|
constructor(ws, options) {
|
|
13
|
-
this
|
|
14
|
-
this
|
|
15
|
-
ws.on("message", (data) =>
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
this.ws = ws;
|
|
14
|
+
this.timeoutMs = options?.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
15
|
+
this.ws.on("message", (data) => {
|
|
16
|
+
this.handleMessage(data);
|
|
17
|
+
});
|
|
18
|
+
this.ws.on("close", () => {
|
|
19
|
+
this.closed = true;
|
|
20
|
+
// Reject all pending requests
|
|
21
|
+
for (const [id, pending] of this.pending) {
|
|
22
|
+
clearTimeout(pending.timer);
|
|
23
|
+
pending.reject(new ChannelClosedError());
|
|
24
|
+
this.pending.delete(id);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
this.ws.on("error", (err) => {
|
|
28
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
29
|
+
console.error("[temps-plugin] WebSocket error:", msg);
|
|
19
30
|
});
|
|
20
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Register a handler for platform events pushed over the channel.
|
|
34
|
+
*/
|
|
21
35
|
onEvent(handler) {
|
|
22
|
-
this
|
|
36
|
+
this.eventHandler = handler;
|
|
23
37
|
}
|
|
24
|
-
|
|
25
|
-
|
|
38
|
+
// -------------------------------------------------------------------------
|
|
39
|
+
// Platform Data Queries
|
|
40
|
+
// -------------------------------------------------------------------------
|
|
41
|
+
async getProject(projectId) {
|
|
42
|
+
return this.request("get_project", { project_id: projectId });
|
|
26
43
|
}
|
|
27
|
-
listProjects() {
|
|
28
|
-
return this.
|
|
44
|
+
async listProjects(limit) {
|
|
45
|
+
return this.request("list_projects", { limit });
|
|
29
46
|
}
|
|
30
|
-
getEnvironment(environmentId) {
|
|
31
|
-
return this.
|
|
47
|
+
async getEnvironment(environmentId) {
|
|
48
|
+
return this.request("get_environment", {
|
|
49
|
+
environment_id: environmentId,
|
|
50
|
+
});
|
|
32
51
|
}
|
|
33
|
-
listEnvironments(projectId) {
|
|
34
|
-
return this.
|
|
52
|
+
async listEnvironments(projectId) {
|
|
53
|
+
return this.request("list_environments", {
|
|
54
|
+
project_id: projectId,
|
|
55
|
+
});
|
|
35
56
|
}
|
|
36
|
-
getDeployment(deploymentId) {
|
|
37
|
-
return this.
|
|
57
|
+
async getDeployment(deploymentId) {
|
|
58
|
+
return this.request("get_deployment", {
|
|
59
|
+
deployment_id: deploymentId,
|
|
60
|
+
});
|
|
38
61
|
}
|
|
39
|
-
getLastDeployment(projectId, environmentId) {
|
|
40
|
-
return this.
|
|
62
|
+
async getLastDeployment(projectId, environmentId) {
|
|
63
|
+
return this.request("get_last_deployment", {
|
|
41
64
|
project_id: projectId,
|
|
42
65
|
environment_id: environmentId,
|
|
43
|
-
})
|
|
66
|
+
});
|
|
44
67
|
}
|
|
45
|
-
listDeployments(projectId, options) {
|
|
46
|
-
return this.
|
|
68
|
+
async listDeployments(projectId, options) {
|
|
69
|
+
return this.request("list_deployments", {
|
|
47
70
|
project_id: projectId,
|
|
48
71
|
environment_id: options?.environmentId,
|
|
49
72
|
limit: options?.limit,
|
|
50
|
-
})
|
|
51
|
-
}
|
|
52
|
-
apiCall(call) {
|
|
53
|
-
return this.call("api_call", call);
|
|
73
|
+
});
|
|
54
74
|
}
|
|
55
|
-
|
|
56
|
-
|
|
75
|
+
// -------------------------------------------------------------------------
|
|
76
|
+
// Low-level request/response
|
|
77
|
+
// -------------------------------------------------------------------------
|
|
78
|
+
/**
|
|
79
|
+
* Send a typed channel request and wait for the response.
|
|
80
|
+
*/
|
|
81
|
+
request(method, params, timeoutMs = this.timeoutMs) {
|
|
82
|
+
if (this.closed) {
|
|
57
83
|
return Promise.reject(new ChannelClosedError());
|
|
58
|
-
|
|
59
|
-
const
|
|
84
|
+
}
|
|
85
|
+
const id = this.nextId++;
|
|
86
|
+
// Filter out undefined values from params
|
|
87
|
+
const cleanParams = {};
|
|
88
|
+
for (const [key, value] of Object.entries(params)) {
|
|
89
|
+
if (value !== undefined) {
|
|
90
|
+
cleanParams[key] = value;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const msg = {
|
|
60
94
|
type: "request",
|
|
61
95
|
id,
|
|
62
|
-
call: { method, params },
|
|
96
|
+
call: { method, params: cleanParams },
|
|
63
97
|
};
|
|
64
98
|
return new Promise((resolve, reject) => {
|
|
65
99
|
const timer = setTimeout(() => {
|
|
66
|
-
this
|
|
67
|
-
reject(new ChannelTimeoutError(method,
|
|
68
|
-
},
|
|
69
|
-
this
|
|
100
|
+
this.pending.delete(id);
|
|
101
|
+
reject(new ChannelTimeoutError(method, timeoutMs));
|
|
102
|
+
}, timeoutMs);
|
|
103
|
+
this.pending.set(id, {
|
|
70
104
|
method,
|
|
71
|
-
resolve:
|
|
105
|
+
resolve: resolve,
|
|
72
106
|
reject,
|
|
73
107
|
timer,
|
|
74
108
|
});
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
109
|
+
try {
|
|
110
|
+
this.ws.send(JSON.stringify(msg), (err) => {
|
|
111
|
+
if (err) {
|
|
112
|
+
clearTimeout(timer);
|
|
113
|
+
this.pending.delete(id);
|
|
114
|
+
reject(err);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
78
119
|
clearTimeout(timer);
|
|
79
|
-
this
|
|
120
|
+
this.pending.delete(id);
|
|
80
121
|
reject(error);
|
|
81
|
-
}
|
|
122
|
+
}
|
|
82
123
|
});
|
|
83
124
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
this.#ws.close();
|
|
87
|
-
this.#closePending();
|
|
88
|
-
}
|
|
89
|
-
#handleMessage(data) {
|
|
90
|
-
let message;
|
|
125
|
+
handleMessage(data) {
|
|
126
|
+
let msg;
|
|
91
127
|
try {
|
|
92
|
-
|
|
128
|
+
const str = typeof data === "string" ? data : String(data);
|
|
129
|
+
msg = JSON.parse(str);
|
|
93
130
|
}
|
|
94
131
|
catch {
|
|
132
|
+
console.error("[temps-plugin] Failed to parse channel message");
|
|
95
133
|
return;
|
|
96
134
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
135
|
+
switch (msg.type) {
|
|
136
|
+
case "response":
|
|
137
|
+
this.handleResponse(msg);
|
|
138
|
+
break;
|
|
139
|
+
case "event":
|
|
140
|
+
if (this.eventHandler) {
|
|
141
|
+
this.eventHandler(msg.event);
|
|
142
|
+
}
|
|
143
|
+
break;
|
|
144
|
+
case "request":
|
|
145
|
+
// Plugins don't handle inbound requests from the host (yet)
|
|
146
|
+
console.warn(`[temps-plugin] Received unexpected request: ${msg.call.method}`);
|
|
147
|
+
break;
|
|
103
148
|
}
|
|
104
149
|
}
|
|
105
|
-
|
|
106
|
-
const pending = this
|
|
107
|
-
if (!pending)
|
|
150
|
+
handleResponse(msg) {
|
|
151
|
+
const pending = this.pending.get(msg.id);
|
|
152
|
+
if (!pending) {
|
|
153
|
+
console.warn(`[temps-plugin] Received response for unknown request ID: ${msg.id}`);
|
|
108
154
|
return;
|
|
155
|
+
}
|
|
109
156
|
clearTimeout(pending.timer);
|
|
110
|
-
this
|
|
111
|
-
if ("err" in
|
|
112
|
-
pending.reject(new PlatformError(
|
|
113
|
-
return;
|
|
157
|
+
this.pending.delete(msg.id);
|
|
158
|
+
if (msg.outcome && "err" in msg.outcome) {
|
|
159
|
+
pending.reject(new PlatformError(msg.outcome.err.code, msg.outcome.err.message));
|
|
114
160
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
161
|
+
else if (msg.outcome &&
|
|
162
|
+
"ok" in msg.outcome &&
|
|
163
|
+
msg.outcome.ok.method === pending.method) {
|
|
164
|
+
pending.resolve(msg.outcome.ok.result);
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
pending.reject(new Error(`Platform channel response does not match ${pending.method}; rebuild the plugin against this Temps version`));
|
|
119
168
|
}
|
|
120
|
-
pending.resolve(response.result);
|
|
121
169
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
170
|
+
/**
|
|
171
|
+
* Close the WebSocket connection.
|
|
172
|
+
*/
|
|
173
|
+
close() {
|
|
174
|
+
this.closed = true;
|
|
175
|
+
for (const pending of this.pending.values()) {
|
|
125
176
|
clearTimeout(pending.timer);
|
|
126
|
-
pending.reject(
|
|
177
|
+
pending.reject(new ChannelClosedError());
|
|
127
178
|
}
|
|
128
|
-
this
|
|
179
|
+
this.pending.clear();
|
|
180
|
+
this.ws.close();
|
|
181
|
+
}
|
|
182
|
+
/** Read live host grants. Do not cache this as an authorization decision. */
|
|
183
|
+
async getHostCapabilities() {
|
|
184
|
+
return this.request("get_host_capabilities", {});
|
|
185
|
+
}
|
|
186
|
+
/** Generate through the host's configured AI provider, without exposing its credentials. */
|
|
187
|
+
async generateAi(input) {
|
|
188
|
+
// Explicit fields prevent accidental forwarding of credentials or actor identifiers.
|
|
189
|
+
return this.request("generate_ai", {
|
|
190
|
+
purpose: input.purpose,
|
|
191
|
+
prompt: input.prompt,
|
|
192
|
+
system: input.system,
|
|
193
|
+
max_tokens: input.max_tokens,
|
|
194
|
+
temperature: input.temperature,
|
|
195
|
+
}, 65_000);
|
|
129
196
|
}
|
|
130
|
-
}
|
|
131
|
-
function normalizeMessage(data) {
|
|
132
|
-
if (typeof data === "string")
|
|
133
|
-
return data;
|
|
134
|
-
if (data instanceof Uint8Array)
|
|
135
|
-
return Buffer.from(data).toString("utf8");
|
|
136
|
-
return String(data);
|
|
137
|
-
}
|
|
138
|
-
function compact(value) {
|
|
139
|
-
return Object.fromEntries(Object.entries(value).filter(([, entry]) => entry !== undefined));
|
|
140
197
|
}
|
|
141
198
|
//# sourceMappingURL=client.js.map
|