@ftai/plugin-host-api 2.0.0 → 2.3.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/lib/attachments.d.ts +112 -1
- package/lib/capabilities.d.ts +99 -0
- package/lib/capabilities.js +21 -0
- package/lib/cmdline.d.ts +52 -0
- package/lib/cmdline.js +68 -0
- package/lib/credentials.d.ts +92 -0
- package/lib/credentials.js +41 -0
- package/lib/fs.d.ts +69 -5
- package/lib/generated.d.ts +3935 -316
- package/lib/generated.js +66 -6
- package/lib/index.d.ts +365 -69
- package/lib/index.js +44 -3
- package/lib/jobs.d.ts +88 -0
- package/lib/jobs.js +2 -0
- package/lib/llm.d.ts +124 -21
- package/lib/llm.js +45 -3
- package/lib/local-install.d.ts +38 -0
- package/lib/local-install.js +9 -0
- package/lib/permissions.d.ts +97 -0
- package/lib/permissions.js +11 -0
- package/lib/platform.d.ts +167 -0
- package/lib/platform.js +74 -0
- package/lib/porting.d.ts +23 -7
- package/lib/porting.js +692 -67
- package/lib/prompt.d.ts +117 -0
- package/lib/prompt.js +43 -0
- package/lib/runtime-context.d.ts +29 -0
- package/lib/runtime-context.js +6 -0
- package/lib/schedule-types.d.ts +333 -0
- package/lib/schedule-types.js +4 -0
- package/lib/session-events.d.ts +194 -0
- package/lib/session-events.js +2 -0
- package/lib/sessions.d.ts +246 -0
- package/lib/sessions.js +2 -0
- package/lib/settings.d.ts +86 -0
- package/lib/settings.js +13 -0
- package/lib/storage.d.ts +144 -0
- package/lib/storage.js +62 -0
- package/lib/subagents.d.ts +57 -0
- package/lib/subagents.js +17 -0
- package/lib/subprocess.d.ts +97 -0
- package/lib/subprocess.js +8 -0
- package/lib/tools.d.ts +212 -0
- package/lib/tools.js +2 -0
- package/lib/web.d.ts +192 -0
- package/lib/web.js +53 -0
- package/package.json +1 -1
package/lib/web.d.ts
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 插件的 HTTP:`ctx.webServer`(0.12 的 `@deepseek-ai/dsh-host-webserver`)与 `ctx.connection` 的宿主一半
|
|
3
|
+
* (0.12 的 `@deepseek-ai/dsh-client-connection`),形状同 0.12。实现只在插件进程里(plugin-runtime 的 web-server.ts、
|
|
4
|
+
* web-connection.ts):路由由插件进程里的 `node:http` 分派,对外是宿主协议的端口,宿主把这个端口上宿主协议之外的路径转过来。
|
|
5
|
+
*/
|
|
6
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
7
|
+
import type { Duplex } from 'node:stream';
|
|
8
|
+
import type { Context } from 'cordis';
|
|
9
|
+
/** 路由的匹配方式:`exact` 只认这个路径;`prefix` 认 `p` 与 `p/<任意>`,多个前缀都认时取最长的。 */
|
|
10
|
+
export type WebRouteKind = 'exact' | 'prefix';
|
|
11
|
+
/** 一条路由。 */
|
|
12
|
+
export interface WebRoute {
|
|
13
|
+
kind: WebRouteKind;
|
|
14
|
+
/** 绝对路径,结尾不带 `/`。 */
|
|
15
|
+
path: string;
|
|
16
|
+
/** 整个答复由它负责(可以一直开着,如 SSE)。 */
|
|
17
|
+
handler: (req: IncomingMessage, res: ServerResponse) => void | Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
/** 一条升级路由(如 WebSocket),只认这个路径。 */
|
|
20
|
+
export interface WebUpgradeRoute {
|
|
21
|
+
/** 绝对路径,结尾不带 `/`。 */
|
|
22
|
+
path: string;
|
|
23
|
+
/** 协议协商与升级之后的连接都由它负责。 */
|
|
24
|
+
handler: (req: IncomingMessage, socket: Duplex, head: Buffer) => void | Promise<void>;
|
|
25
|
+
}
|
|
26
|
+
/** 首页注入行落在哪里:开头的 head 或 body 标签之后。 */
|
|
27
|
+
export type IndexInjectionPlacement = 'head' | 'body';
|
|
28
|
+
/** 首页的一行结构化注入(`webserver/index-inject`)。 */
|
|
29
|
+
export type IndexInjection =
|
|
30
|
+
/** 在 `globalThis` 上放一个可以 JSON 序列化的值,排在后面的脚本行之前。 */
|
|
31
|
+
{
|
|
32
|
+
kind: 'global';
|
|
33
|
+
name: string;
|
|
34
|
+
value: unknown;
|
|
35
|
+
}
|
|
36
|
+
/** 内联的经典脚本。`text` 不能含 `</script`。 */
|
|
37
|
+
| {
|
|
38
|
+
kind: 'script';
|
|
39
|
+
placement: IndexInjectionPlacement;
|
|
40
|
+
text: string;
|
|
41
|
+
}
|
|
42
|
+
/** 外部的经典脚本,按表里的顺序执行。 */
|
|
43
|
+
| {
|
|
44
|
+
kind: 'script-src';
|
|
45
|
+
placement: IndexInjectionPlacement;
|
|
46
|
+
src: string;
|
|
47
|
+
}
|
|
48
|
+
/** 外部脚本的预加载提示。 */
|
|
49
|
+
| {
|
|
50
|
+
kind: 'script-preload';
|
|
51
|
+
src: string;
|
|
52
|
+
}
|
|
53
|
+
/** head 里的一个 `<style>`。`text` 不能含 `</style`。 */
|
|
54
|
+
| {
|
|
55
|
+
kind: 'style';
|
|
56
|
+
text: string;
|
|
57
|
+
}
|
|
58
|
+
/** 原样的一段标记。 */
|
|
59
|
+
| {
|
|
60
|
+
kind: 'html';
|
|
61
|
+
placement: IndexInjectionPlacement;
|
|
62
|
+
html: string;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* 插件的 HTTP 服务(`ctx.webServer`)。同 0.12:注册按调用方的作用域,插件停用时撤销;重名的路由在注册处抛错。
|
|
66
|
+
* 宿主协议与应用页面自己的路径(`/`、`/index.html`、`/ws`、`/assets`、`/client` 下)不能注册。
|
|
67
|
+
*
|
|
68
|
+
* 宿主转发之前核对 Host 与 Origin(0.12 不核对):Host 要是本机回环,或用户在设置「功能与插件」页加的可信 Host(经隧道等从外部
|
|
69
|
+
* 访问时);带 Origin 时要与 Host 是同一个主机与端口。不对的答 403,到不了插件。不带 Origin 的照收:服务器发来的 webhook、
|
|
70
|
+
* 浏览器的普通跳转(如 OAuth 回调)都不带。隧道把 Host 改写成 localhost 的不用加可信 Host。
|
|
71
|
+
*/
|
|
72
|
+
export interface WebServer {
|
|
73
|
+
/** 对外的端口(宿主协议的端口),每次启动都可能不同。 */
|
|
74
|
+
readonly port: number;
|
|
75
|
+
/** 只听本机回环。 */
|
|
76
|
+
readonly host: '127.0.0.1';
|
|
77
|
+
/** 登记一条路由;返回撤销函数。 */
|
|
78
|
+
register(route: WebRoute): () => void;
|
|
79
|
+
/** 登记一条升级路由;返回撤销函数。 */
|
|
80
|
+
registerUpgrade(route: WebUpgradeRoute): () => void;
|
|
81
|
+
/** 占下兜底的位置:没有路由认的请求都交给它。只能有一个。 */
|
|
82
|
+
registerFallback(handler: WebRoute['handler']): () => void;
|
|
83
|
+
/** 登记一个首页的 HTML 变换,`renderIndex` 在结构化的行之后按登记顺序套用。 */
|
|
84
|
+
tapIndex(transform: (html: string) => string): () => void;
|
|
85
|
+
/** 按登记顺序套用 `tapIndex` 的变换。 */
|
|
86
|
+
applyIndexTaps(html: string): string;
|
|
87
|
+
/** 发一次 `webserver/index-inject`,收集各监听者现在的行。 */
|
|
88
|
+
collectIndexInjections(): IndexInjection[];
|
|
89
|
+
/** 先渲染结构化的行,再套用 `tapIndex` 的变换。 */
|
|
90
|
+
renderIndex(html: string): string;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* 把行渲染进首页(同 0.12):head 的行紧跟开头的 head 标签,body 的行紧跟开头的 body 标签,各按表里的顺序,最后是就绪信号。
|
|
94
|
+
* 没有 head 或 body 标签时分别放在最前、最后。
|
|
95
|
+
*/
|
|
96
|
+
export declare function renderIndexInjections(html: string, rows: readonly IndexInjection[]): string;
|
|
97
|
+
/** 共用的 API 前缀:`/api` 与 `/api/<任意>`。 */
|
|
98
|
+
export declare const API_PATH = "/api";
|
|
99
|
+
/** 核对一个请求要用的:请求头(node:http 或 Fetch 的写法都行)。 */
|
|
100
|
+
export interface ConnectionTrustRequest {
|
|
101
|
+
readonly headers: Headers | Readonly<Record<string, string | readonly string[] | undefined>>;
|
|
102
|
+
}
|
|
103
|
+
/** 分派之前拒绝的状态码;可以继续时为 undefined。 */
|
|
104
|
+
export type ConnectionRequestRejection = 401 | 403 | undefined;
|
|
105
|
+
/** 首页请求(用令牌换浏览器会话)。 */
|
|
106
|
+
export interface ConnectionIndexRequest extends ConnectionTrustRequest {
|
|
107
|
+
readonly method?: string | undefined;
|
|
108
|
+
readonly url?: string | undefined;
|
|
109
|
+
}
|
|
110
|
+
/** 首页的答复。 */
|
|
111
|
+
export interface ConnectionIndexResponse {
|
|
112
|
+
writeHead(status: number, headers?: Readonly<Record<string, string>>): unknown;
|
|
113
|
+
end(body?: string): unknown;
|
|
114
|
+
}
|
|
115
|
+
/** 被放行的请求代表的一方:只有使用者本人(operator)。 */
|
|
116
|
+
export interface PeerScope {
|
|
117
|
+
readonly id: string;
|
|
118
|
+
readonly ctx: Context;
|
|
119
|
+
dispose(): Promise<void>;
|
|
120
|
+
}
|
|
121
|
+
/** 放行的结果:代表使用者,或拒绝的状态码。 */
|
|
122
|
+
export type PeerAdmission = {
|
|
123
|
+
readonly peer: PeerScope;
|
|
124
|
+
} | {
|
|
125
|
+
readonly rejection: 401 | 403;
|
|
126
|
+
};
|
|
127
|
+
export type ConnectionFetchMethod = 'GET' | 'HEAD' | 'POST';
|
|
128
|
+
/** 请求体怎么交给路由:`buffered` 读完再给(受总量上限),`streaming` 按流给。 */
|
|
129
|
+
export type ConnectionRequestBodyMode = 'buffered' | 'streaming';
|
|
130
|
+
/** `/api` 下的一条精确路由,以 Fetch 的写法处理。 */
|
|
131
|
+
export interface ConnectionFetchRoute {
|
|
132
|
+
/** `/api` 下的绝对路径;查询参数在请求的 URL 上。 */
|
|
133
|
+
readonly path: string;
|
|
134
|
+
/** 这条路由认的方法;其余方法照常往下分派。 */
|
|
135
|
+
readonly methods: readonly ConnectionFetchMethod[];
|
|
136
|
+
readonly requestBody: ConnectionRequestBodyMode;
|
|
137
|
+
/** 处理一个已经核对、放行的请求。 */
|
|
138
|
+
readonly fetch: (request: Request) => Promise<Response>;
|
|
139
|
+
}
|
|
140
|
+
/** `/api` 上精确路由的登记。 */
|
|
141
|
+
export interface HostConnectionFetch {
|
|
142
|
+
/** 登记一条;返回撤销函数。同一路径已登记时抛错。 */
|
|
143
|
+
register(route: ConnectionFetchRoute): () => Promise<void>;
|
|
144
|
+
}
|
|
145
|
+
/** 以 Fetch 的写法处理一个 `/api` 请求。 */
|
|
146
|
+
export interface ConnectionFetchHandler {
|
|
147
|
+
/** 读请求体之前,按方法与地址给出请求体的交法。 */
|
|
148
|
+
requestBodyMode(request: {
|
|
149
|
+
readonly method: string;
|
|
150
|
+
readonly url: URL;
|
|
151
|
+
}): ConnectionRequestBodyMode;
|
|
152
|
+
/** 分派一个已经放行的请求;没有路由时 404。 */
|
|
153
|
+
fetch(request: Request): Promise<Response>;
|
|
154
|
+
}
|
|
155
|
+
/** `ctx.connection` 的宿主一半。 */
|
|
156
|
+
export interface HostConnection {
|
|
157
|
+
/** `/api` 上的精确路由。 */
|
|
158
|
+
readonly fetch: HostConnectionFetch;
|
|
159
|
+
/** 被放行的请求代表的一方。 */
|
|
160
|
+
readonly operator: PeerScope;
|
|
161
|
+
/** 只认登记的精确路由的 `/api` 分派。 */
|
|
162
|
+
createSharedFetchHandler(channel: '/api'): ConnectionFetchHandler;
|
|
163
|
+
/**
|
|
164
|
+
* 给插件自己的路由用的同一道关:Host 是本机回环、浏览器标的来源是同一个、并且已登录(应用自己的页面,或用
|
|
165
|
+
* {@link authenticatedUrl} 换到浏览器会话的)。可以继续时为 undefined。
|
|
166
|
+
*/
|
|
167
|
+
requestRejection(request: ConnectionTrustRequest): ConnectionRequestRejection;
|
|
168
|
+
/** 过了 {@link requestRejection} 的请求代表使用者。 */
|
|
169
|
+
admit(request: ConnectionTrustRequest): PeerAdmission;
|
|
170
|
+
/** 首页请求:带着有效令牌时发浏览器会话的 Cookie 并转到干净的 `./`,已登录时放行,否则答 401。 */
|
|
171
|
+
authorizeIndex(request: ConnectionIndexRequest, response: ConnectionIndexResponse): boolean;
|
|
172
|
+
/** 给地址加上这次启动的令牌(`?token=`),浏览器打开首页时换成会话。 */
|
|
173
|
+
authenticatedUrl(baseUrl: string): string;
|
|
174
|
+
}
|
|
175
|
+
declare module 'cordis' {
|
|
176
|
+
interface Context {
|
|
177
|
+
readonly webServer: WebServer;
|
|
178
|
+
readonly connection: HostConnection;
|
|
179
|
+
}
|
|
180
|
+
interface Events {
|
|
181
|
+
/**
|
|
182
|
+
* 收集首页的结构化注入:每次收集都发一次,监听者把自己现在的行推进表里。
|
|
183
|
+
* @param table - 可以改的表,按监听者的激活顺序追加。
|
|
184
|
+
*/
|
|
185
|
+
'webserver/index-inject'(table: IndexInjection[]): void;
|
|
186
|
+
/**
|
|
187
|
+
* 放行之后、交给 `/api` 的分派之前:监听者可以包一层或接手答复,`next` 交给下一个监听者或分派。
|
|
188
|
+
* @mode waterfall
|
|
189
|
+
*/
|
|
190
|
+
'connection/request'(request: IncomingMessage, response: ServerResponse, next: () => Promise<void>): Promise<void>;
|
|
191
|
+
}
|
|
192
|
+
}
|
package/lib/web.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/** 把行写进转义属性值之前的转义。 */
|
|
2
|
+
function escapeHtmlAttribute(value) {
|
|
3
|
+
return value.replaceAll('&', '&').replaceAll('"', '"').replaceAll('<', '<').replaceAll('>', '>');
|
|
4
|
+
}
|
|
5
|
+
function renderRow(row) {
|
|
6
|
+
switch (row.kind) {
|
|
7
|
+
case 'global': {
|
|
8
|
+
// JSON 里的 `<` 转义掉,行里的字符串跳不出 script 元素
|
|
9
|
+
const name = JSON.stringify(row.name).replaceAll('<', '\\u003c');
|
|
10
|
+
const value = row.value === undefined ? 'undefined' : JSON.stringify(row.value).replaceAll('<', '\\u003c');
|
|
11
|
+
return { placement: 'head', markup: `<script>globalThis[${name}] = ${value}</script>` };
|
|
12
|
+
}
|
|
13
|
+
case 'script':
|
|
14
|
+
return { placement: row.placement, markup: `<script>${row.text}</script>` };
|
|
15
|
+
case 'script-src':
|
|
16
|
+
return { placement: row.placement, markup: `<script src="${escapeHtmlAttribute(row.src)}"></script>` };
|
|
17
|
+
case 'script-preload':
|
|
18
|
+
return { placement: 'head', markup: `<link rel="preload" as="script" href="${escapeHtmlAttribute(row.src)}">` };
|
|
19
|
+
case 'style':
|
|
20
|
+
return { placement: 'head', markup: `<style>${row.text}</style>` };
|
|
21
|
+
case 'html':
|
|
22
|
+
return { placement: row.placement, markup: row.html };
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/** 0.12 首页入口等的就绪信号:最后一行之后兑现。 */
|
|
26
|
+
const READY_MARKUP = '<script>(globalThis.__DSH_BOOT_READY__ ??= Promise.withResolvers()).resolve()</script>';
|
|
27
|
+
/**
|
|
28
|
+
* 把行渲染进首页(同 0.12):head 的行紧跟开头的 head 标签,body 的行紧跟开头的 body 标签,各按表里的顺序,最后是就绪信号。
|
|
29
|
+
* 没有 head 或 body 标签时分别放在最前、最后。
|
|
30
|
+
*/
|
|
31
|
+
export function renderIndexInjections(html, rows) {
|
|
32
|
+
let head = '';
|
|
33
|
+
let body = '';
|
|
34
|
+
for (const row of rows) {
|
|
35
|
+
const rendered = renderRow(row);
|
|
36
|
+
if (rendered.placement === 'head')
|
|
37
|
+
head += rendered.markup;
|
|
38
|
+
else
|
|
39
|
+
body += rendered.markup;
|
|
40
|
+
}
|
|
41
|
+
body += READY_MARKUP;
|
|
42
|
+
let out = html;
|
|
43
|
+
if (head !== '') {
|
|
44
|
+
const open = /<head(?:\s[^>]*)?>/i.exec(out);
|
|
45
|
+
out = open === null ? `${head}${out}` : `${out.slice(0, open.index + open[0].length)}${head}${out.slice(open.index + open[0].length)}`;
|
|
46
|
+
}
|
|
47
|
+
const open = /<body(?:\s[^>]*)?>/i.exec(out);
|
|
48
|
+
return open === null ? `${out}${body}` : `${out.slice(0, open.index + open[0].length)}${body}${out.slice(open.index + open[0].length)}`;
|
|
49
|
+
}
|
|
50
|
+
// ---- ctx.connection 的宿主一半 ----
|
|
51
|
+
/** 共用的 API 前缀:`/api` 与 `/api/<任意>`。 */
|
|
52
|
+
export const API_PATH = '/api';
|
|
53
|
+
//# sourceMappingURL=web.js.map
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ftai/plugin-host-api",
|
|
3
3
|
"description": "FtAi Desktop 插件后端(宿主一半)的接口,插件经 @ftai/plugin-sdk/host 使用:服务与扩展点的类型由宿主的扩展点表生成,另有以 0.12 写法包的一层与常用函数",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.3.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://ftai.chat/desktop/docs/plugins/",
|
|
7
7
|
"type": "module",
|