@v587d/capital-generation 2.24.0 → 2.26.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/CHANGELOG.md +49 -0
- package/README.md +9 -4
- package/THIRD_PARTY_NOTICES.md +20 -0
- package/automation/brief.js +21 -24
- package/automation/builtins.js +55 -54
- package/automation/client.js +11 -3
- package/automation/client.src.cjs +70 -8
- package/automation/package.json +1 -1
- package/chart-ui/package.json +1 -1
- package/config/client.js +21 -5
- package/config/client.src.cjs +264 -0
- package/config/package.json +1 -1
- package/cordis.patch.yml +9 -2
- package/lib/agents/role-tool-policy.d.ts +1 -1
- package/lib/agents/root-tool-policy.d.ts +3 -3
- package/lib/agents/root-tool-policy.js +2 -2
- package/lib/data-collector/catalog-taxonomy.js +4 -0
- package/lib/python-runtime/child-env.d.ts +11 -0
- package/lib/python-runtime/child-env.js +39 -0
- package/lib/python-runtime/errors.d.ts +8 -0
- package/lib/python-runtime/errors.js +34 -0
- package/lib/python-runtime/lock.d.ts +11 -0
- package/lib/python-runtime/lock.js +68 -0
- package/lib/python-runtime/manager.d.ts +61 -0
- package/lib/python-runtime/manager.js +517 -0
- package/lib/python-runtime/manifest.d.ts +5 -0
- package/lib/python-runtime/manifest.js +160 -0
- package/lib/python-runtime/paths.d.ts +20 -0
- package/lib/python-runtime/paths.js +156 -0
- package/lib/python-runtime/platform.d.ts +32 -0
- package/lib/python-runtime/platform.js +145 -0
- package/lib/python-runtime/pnpm.d.ts +24 -0
- package/lib/python-runtime/pnpm.js +115 -0
- package/lib/python-runtime/probe.d.ts +13 -0
- package/lib/python-runtime/probe.js +101 -0
- package/lib/python-runtime/routes.d.ts +6 -0
- package/lib/python-runtime/routes.js +152 -0
- package/lib/python-runtime/types.d.ts +105 -0
- package/lib/python-runtime/types.js +3 -0
- package/lib/python-runtime-entry.d.ts +10 -0
- package/lib/python-runtime-entry.js +60 -0
- package/lib/sources/eastmoney-http.js +346 -1
- package/lib/sources/tencent-http.js +248 -2
- package/lib/sources/tencent-public-core.js +1 -1
- package/lib/sources-entry.d.ts +1 -1
- package/lib/time/tools.js +2 -0
- package/lib/web-retriever/local-fetch.d.ts +1 -1
- package/lib/web-retriever/local-fetch.js +1 -1
- package/lib/web-retriever/sources.d.ts +16 -1
- package/lib/web-retriever/sources.js +133 -1
- package/lib/web-retriever/tools.js +41 -1
- package/lib/web-retriever/wind-client.d.ts +1 -1
- package/lib/web-retriever/wind-client.js +1 -1
- package/package.json +13 -1
- package/plugin-page/README.en.md +1 -7
- package/plugin-page/README.zh.md +1 -8
- package/preset/capital-generation/agent.patch.yml +12 -13
- package/preset/capital-generation/skills/capital-web-protocol/SKILL.md +10 -4
- package/python/worker.py +154 -0
- package/runtime/manifest.json +75 -0
- package/scripts/python-runtime.mjs +139 -0
- package/watchlist/client.js +83 -3
- package/watchlist/client.src.cjs +565 -1
- package/watchlist/package.json +1 -1
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { mkdir } from 'node:fs/promises';
|
|
3
|
+
import { PythonRuntimeError } from './errors.js';
|
|
4
|
+
import { pythonChildEnv } from './child-env.js';
|
|
5
|
+
import { detachedChild, killTree, pythonFlags } from './platform.js';
|
|
6
|
+
function parseReport(output) {
|
|
7
|
+
const line = output.split(/\r?\n/u).map((item) => item.trim()).filter(Boolean).at(-1);
|
|
8
|
+
if (line === undefined)
|
|
9
|
+
throw new PythonRuntimeError('probe-failed', 'Python probe 没有输出。');
|
|
10
|
+
let value;
|
|
11
|
+
try {
|
|
12
|
+
value = JSON.parse(line);
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
throw new PythonRuntimeError('probe-failed', 'Python probe 输出不是 JSON。', output);
|
|
16
|
+
}
|
|
17
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
18
|
+
throw new PythonRuntimeError('probe-failed', 'Python probe 输出不是对象。', output);
|
|
19
|
+
}
|
|
20
|
+
const raw = value;
|
|
21
|
+
if (typeof raw.pythonVersion !== 'string'
|
|
22
|
+
|| typeof raw.executable !== 'string'
|
|
23
|
+
|| typeof raw.platform !== 'string'
|
|
24
|
+
|| typeof raw.arch !== 'string'
|
|
25
|
+
|| !Array.isArray(raw.sitePackages)
|
|
26
|
+
|| raw.sitePackages.some((item) => typeof item !== 'string')
|
|
27
|
+
|| raw.packages === null
|
|
28
|
+
|| typeof raw.packages !== 'object'
|
|
29
|
+
|| Array.isArray(raw.packages)) {
|
|
30
|
+
throw new PythonRuntimeError('probe-failed', 'Python probe 字段不完整。', output);
|
|
31
|
+
}
|
|
32
|
+
return raw;
|
|
33
|
+
}
|
|
34
|
+
/** Run the private interpreter once and require its self-check JSON. */
|
|
35
|
+
export async function probePython(options) {
|
|
36
|
+
await mkdir(options.homeDir, { recursive: true });
|
|
37
|
+
await mkdir(options.tempDir, { recursive: true });
|
|
38
|
+
const env = pythonChildEnv({
|
|
39
|
+
pythonDir: options.python.replace(/[\\/][^\\/]+$/u, ''),
|
|
40
|
+
homeDir: options.homeDir,
|
|
41
|
+
tempDir: options.tempDir,
|
|
42
|
+
baseEnv: options.env ?? process.env,
|
|
43
|
+
});
|
|
44
|
+
const args = [
|
|
45
|
+
...pythonFlags,
|
|
46
|
+
options.workerPath,
|
|
47
|
+
'probe',
|
|
48
|
+
...options.sitePackages.flatMap((path) => ['--site-packages', path]),
|
|
49
|
+
];
|
|
50
|
+
return await new Promise((resolvePromise, reject) => {
|
|
51
|
+
let stdout = '';
|
|
52
|
+
let stderr = '';
|
|
53
|
+
let timedOut = false;
|
|
54
|
+
let settled = false;
|
|
55
|
+
const child = spawn(options.python, args, {
|
|
56
|
+
cwd: options.tempDir,
|
|
57
|
+
env,
|
|
58
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
59
|
+
windowsHide: true,
|
|
60
|
+
detached: detachedChild,
|
|
61
|
+
});
|
|
62
|
+
const timer = setTimeout(() => {
|
|
63
|
+
timedOut = true;
|
|
64
|
+
void killTree(child.pid, child, 'SIGTERM');
|
|
65
|
+
setTimeout(() => { void killTree(child.pid, child, 'SIGKILL'); }, 2_000).unref?.();
|
|
66
|
+
}, options.timeoutMs);
|
|
67
|
+
timer.unref?.();
|
|
68
|
+
const abort = () => { void killTree(child.pid, child, 'SIGTERM'); };
|
|
69
|
+
options.signal?.addEventListener('abort', abort, { once: true });
|
|
70
|
+
const finish = (code) => {
|
|
71
|
+
if (settled)
|
|
72
|
+
return;
|
|
73
|
+
settled = true;
|
|
74
|
+
clearTimeout(timer);
|
|
75
|
+
options.signal?.removeEventListener('abort', abort);
|
|
76
|
+
if (timedOut)
|
|
77
|
+
return reject(new PythonRuntimeError('timeout', 'Python probe 超时。', stderr));
|
|
78
|
+
if (options.signal?.aborted === true)
|
|
79
|
+
return reject(new PythonRuntimeError('cancelled', undefined, stderr));
|
|
80
|
+
if (code !== 0)
|
|
81
|
+
return reject(new PythonRuntimeError('probe-failed', `Python probe 退出码 ${String(code)}。`, stderr || stdout));
|
|
82
|
+
try {
|
|
83
|
+
resolvePromise(parseReport(stdout));
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
reject(error);
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
child.stdout?.on('data', (chunk) => { stdout = (stdout + chunk.toString('utf8')).slice(-64 * 1024); });
|
|
90
|
+
child.stderr?.on('data', (chunk) => { stderr = (stderr + chunk.toString('utf8')).slice(-64 * 1024); });
|
|
91
|
+
child.once('error', (error) => {
|
|
92
|
+
if (settled)
|
|
93
|
+
return;
|
|
94
|
+
settled = true;
|
|
95
|
+
clearTimeout(timer);
|
|
96
|
+
options.signal?.removeEventListener('abort', abort);
|
|
97
|
+
reject(new PythonRuntimeError('probe-failed', `启动 Python 失败:${String(error)}`));
|
|
98
|
+
});
|
|
99
|
+
child.once('close', (code) => finish(code));
|
|
100
|
+
});
|
|
101
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import type { PythonRuntimeManager } from './manager.js';
|
|
3
|
+
type Authorize = (req: IncomingMessage) => number | undefined | null;
|
|
4
|
+
/** HTTP surface for the settings card and CLI-like clients. */
|
|
5
|
+
export declare function createPythonRuntimeHandler(manager: PythonRuntimeManager, authorize?: Authorize): (req: IncomingMessage, res: ServerResponse) => Promise<void>;
|
|
6
|
+
export {};
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { PythonRuntimeError, isPythonRuntimeError, messageOf } from './errors.js';
|
|
2
|
+
import { normalizePackNames } from './manifest.js';
|
|
3
|
+
const ROUTE = '/python-runtime';
|
|
4
|
+
const MAX_BODY = 16 * 1024;
|
|
5
|
+
function sendJson(res, status, body) {
|
|
6
|
+
const text = JSON.stringify(body);
|
|
7
|
+
res.statusCode = status;
|
|
8
|
+
res.setHeader('content-type', 'application/json; charset=utf-8');
|
|
9
|
+
res.setHeader('cache-control', 'no-store');
|
|
10
|
+
res.setHeader('x-content-type-options', 'nosniff');
|
|
11
|
+
res.end(text);
|
|
12
|
+
}
|
|
13
|
+
function sendError(res, error) {
|
|
14
|
+
if (!isPythonRuntimeError(error)) {
|
|
15
|
+
sendJson(res, 500, { error: { code: 'unknown', message: messageOf(error) } });
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const status = error.code === 'invalid-request' ? 400
|
|
19
|
+
: error.code === 'unsupported-platform' || error.code === 'runtime-not-ready' || error.code === 'busy' || error.code === 'runtime-locked' ? 409
|
|
20
|
+
: error.code === 'network' || error.code === 'not-found' ? 502
|
|
21
|
+
: error.code === 'disk-space' ? 507
|
|
22
|
+
: 500;
|
|
23
|
+
sendJson(res, status, { error: { code: error.code, message: error.message } });
|
|
24
|
+
}
|
|
25
|
+
async function readJson(req) {
|
|
26
|
+
const chunks = [];
|
|
27
|
+
let size = 0;
|
|
28
|
+
for await (const chunk of req) {
|
|
29
|
+
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
30
|
+
size += buffer.length;
|
|
31
|
+
if (size > MAX_BODY)
|
|
32
|
+
throw new PythonRuntimeError('invalid-request', '请求体超过 16 KB。');
|
|
33
|
+
chunks.push(buffer);
|
|
34
|
+
}
|
|
35
|
+
if (chunks.length === 0)
|
|
36
|
+
return {};
|
|
37
|
+
let value;
|
|
38
|
+
try {
|
|
39
|
+
value = JSON.parse(Buffer.concat(chunks).toString('utf8'));
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
throw new PythonRuntimeError('invalid-request', '请求体不是合法 JSON。');
|
|
43
|
+
}
|
|
44
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
45
|
+
throw new PythonRuntimeError('invalid-request', '请求体必须是 JSON 对象。');
|
|
46
|
+
}
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
49
|
+
function packBody(value) {
|
|
50
|
+
if (value !== 'base' && value !== 'parquet')
|
|
51
|
+
throw new PythonRuntimeError('invalid-request', 'pack 必须是 base 或 parquet。');
|
|
52
|
+
return value;
|
|
53
|
+
}
|
|
54
|
+
/** HTTP surface for the settings card and CLI-like clients. */
|
|
55
|
+
export function createPythonRuntimeHandler(manager, authorize) {
|
|
56
|
+
return async (req, res) => {
|
|
57
|
+
try {
|
|
58
|
+
let rejection;
|
|
59
|
+
try {
|
|
60
|
+
rejection = authorize?.(req);
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
rejection = 401;
|
|
64
|
+
}
|
|
65
|
+
if (rejection !== undefined && rejection !== null) {
|
|
66
|
+
res.statusCode = rejection;
|
|
67
|
+
res.setHeader('content-type', 'text/plain; charset=utf-8');
|
|
68
|
+
res.setHeader('cache-control', 'no-store');
|
|
69
|
+
res.end(rejection === 401 ? 'unauthorized' : 'forbidden');
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const url = new URL(req.url ?? '/', 'http://localhost');
|
|
73
|
+
const pathname = url.pathname;
|
|
74
|
+
if (pathname !== ROUTE && !pathname.startsWith(`${ROUTE}/`)) {
|
|
75
|
+
sendJson(res, 404, { error: { code: 'not-found', message: 'not found' } });
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
const suffix = pathname.slice(ROUTE.length);
|
|
79
|
+
const method = req.method ?? 'GET';
|
|
80
|
+
if (suffix === '/status') {
|
|
81
|
+
if (method !== 'GET') {
|
|
82
|
+
sendJson(res, 405, { error: { code: 'method-not-allowed', message: 'GET only' } });
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
sendJson(res, 200, await manager.status());
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (suffix === '/prepare') {
|
|
89
|
+
if (method !== 'POST') {
|
|
90
|
+
sendJson(res, 405, { error: { code: 'method-not-allowed', message: 'POST only' } });
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const body = await readJson(req);
|
|
94
|
+
const packs = normalizePackNames(body.packs);
|
|
95
|
+
sendJson(res, 202, { job: manager.prepare(packs) });
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (suffix.startsWith('/jobs/')) {
|
|
99
|
+
if (method !== 'GET') {
|
|
100
|
+
sendJson(res, 405, { error: { code: 'method-not-allowed', message: 'GET only' } });
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const id = suffix.slice('/jobs/'.length);
|
|
104
|
+
if (id === '' || id.includes('/'))
|
|
105
|
+
throw new PythonRuntimeError('invalid-request', 'job id 不合法。');
|
|
106
|
+
const job = manager.getJob(id);
|
|
107
|
+
if (job === undefined) {
|
|
108
|
+
sendJson(res, 404, { error: { code: 'not-found', message: 'job not found' } });
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
sendJson(res, 200, { job });
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
if (suffix === '/cancel') {
|
|
115
|
+
if (method !== 'POST') {
|
|
116
|
+
sendJson(res, 405, { error: { code: 'method-not-allowed', message: 'POST only' } });
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const body = await readJson(req);
|
|
120
|
+
if (typeof body.jobId !== 'string' || body.jobId === '')
|
|
121
|
+
throw new PythonRuntimeError('invalid-request', 'jobId 必须是非空字符串。');
|
|
122
|
+
sendJson(res, 200, { cancelled: manager.cancel(body.jobId) });
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (suffix === '/remove') {
|
|
126
|
+
if (method !== 'POST') {
|
|
127
|
+
sendJson(res, 405, { error: { code: 'method-not-allowed', message: 'POST only' } });
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const body = await readJson(req);
|
|
131
|
+
if (body.confirm !== true)
|
|
132
|
+
throw new PythonRuntimeError('invalid-request', '移除运行时必须显式 confirm: true。');
|
|
133
|
+
const pack = packBody(body.pack);
|
|
134
|
+
await manager.remove(pack);
|
|
135
|
+
sendJson(res, 200, { removed: pack });
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (suffix === '/verify') {
|
|
139
|
+
if (method !== 'POST') {
|
|
140
|
+
sendJson(res, 405, { error: { code: 'method-not-allowed', message: 'POST only' } });
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
sendJson(res, 200, { probe: await manager.verify() });
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
sendJson(res, 404, { error: { code: 'not-found', message: 'not found' } });
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
sendError(res, error);
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/** Public, JSON-serializable shapes of the plugin-owned Python runtime. */
|
|
2
|
+
export declare const PACK_NAMES: readonly ["base", "parquet"];
|
|
3
|
+
export type PackName = (typeof PACK_NAMES)[number];
|
|
4
|
+
export declare const TARGET_IDS: readonly ["win32-x64", "darwin-arm64", "darwin-x64", "linux-x64"];
|
|
5
|
+
export type TargetId = (typeof TARGET_IDS)[number];
|
|
6
|
+
export interface PackTarget {
|
|
7
|
+
package: string;
|
|
8
|
+
version: string;
|
|
9
|
+
bytes: number;
|
|
10
|
+
sha256?: string;
|
|
11
|
+
}
|
|
12
|
+
export interface PackManifest {
|
|
13
|
+
kind: 'base' | 'lib';
|
|
14
|
+
packages: Record<string, string>;
|
|
15
|
+
requires?: Partial<Record<PackName, string>>;
|
|
16
|
+
targets: Partial<Record<TargetId, PackTarget>>;
|
|
17
|
+
}
|
|
18
|
+
export interface RuntimeManifest {
|
|
19
|
+
schemaVersion: 1;
|
|
20
|
+
python: string;
|
|
21
|
+
cacheRoot: string;
|
|
22
|
+
packs: Record<PackName, PackManifest>;
|
|
23
|
+
limits: {
|
|
24
|
+
minFreeBytes: number;
|
|
25
|
+
installTimeoutMs: number;
|
|
26
|
+
probeTimeoutMs: number;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export type PackState = 'not-installed' | 'installing' | 'ready' | 'broken';
|
|
30
|
+
export interface PackStatus {
|
|
31
|
+
name: PackName;
|
|
32
|
+
state: PackState;
|
|
33
|
+
kind: 'base' | 'lib';
|
|
34
|
+
version: string;
|
|
35
|
+
package: string;
|
|
36
|
+
target: TargetId | null;
|
|
37
|
+
pythonVersion: string;
|
|
38
|
+
packages: Record<string, string>;
|
|
39
|
+
installedAt?: string;
|
|
40
|
+
bytes: number;
|
|
41
|
+
path: string;
|
|
42
|
+
error?: string;
|
|
43
|
+
}
|
|
44
|
+
export interface RuntimeStatus {
|
|
45
|
+
supported: boolean;
|
|
46
|
+
reason?: string;
|
|
47
|
+
target: TargetId | null;
|
|
48
|
+
platform: string;
|
|
49
|
+
arch: string;
|
|
50
|
+
python: string;
|
|
51
|
+
cacheRoot: string;
|
|
52
|
+
base: PackStatus;
|
|
53
|
+
parquet: PackStatus;
|
|
54
|
+
ready: boolean;
|
|
55
|
+
activeJob?: PrepareJob;
|
|
56
|
+
}
|
|
57
|
+
export type PrepareState = 'running' | 'done' | 'failed' | 'cancelled';
|
|
58
|
+
export type PrepareStage = 'checking' | 'installing' | 'verifying' | 'done';
|
|
59
|
+
export interface PrepareJobError {
|
|
60
|
+
code: string;
|
|
61
|
+
message: string;
|
|
62
|
+
}
|
|
63
|
+
export interface PrepareJob {
|
|
64
|
+
id: string;
|
|
65
|
+
packs: PackName[];
|
|
66
|
+
state: PrepareState;
|
|
67
|
+
stage: PrepareStage;
|
|
68
|
+
current?: PackName;
|
|
69
|
+
message: string;
|
|
70
|
+
startedAt: string;
|
|
71
|
+
endedAt?: string;
|
|
72
|
+
error?: PrepareJobError;
|
|
73
|
+
}
|
|
74
|
+
export interface RuntimeHandle {
|
|
75
|
+
python: string;
|
|
76
|
+
pythonVersion: string;
|
|
77
|
+
sitePackages: string[];
|
|
78
|
+
packages: Record<string, string>;
|
|
79
|
+
packs: Partial<Record<PackName, string>>;
|
|
80
|
+
}
|
|
81
|
+
export interface ProbeReport {
|
|
82
|
+
pythonVersion: string;
|
|
83
|
+
executable: string;
|
|
84
|
+
platform: string;
|
|
85
|
+
arch: string;
|
|
86
|
+
sitePackages: string[];
|
|
87
|
+
packages: Record<string, string>;
|
|
88
|
+
}
|
|
89
|
+
export interface ReadyFile {
|
|
90
|
+
schemaVersion: 1;
|
|
91
|
+
pack: PackName;
|
|
92
|
+
packVersion: string;
|
|
93
|
+
target: TargetId;
|
|
94
|
+
package: string;
|
|
95
|
+
pythonVersion: string;
|
|
96
|
+
packages: Record<string, string>;
|
|
97
|
+
python: string | null;
|
|
98
|
+
sitePackages: string[];
|
|
99
|
+
installedAt: string;
|
|
100
|
+
probe: {
|
|
101
|
+
ok: boolean;
|
|
102
|
+
at: string;
|
|
103
|
+
};
|
|
104
|
+
bytes?: number;
|
|
105
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
2
|
+
import { PythonRuntimeManager } from './python-runtime/manager.js';
|
|
3
|
+
declare module '@deepseek-ai/cordis' {
|
|
4
|
+
interface Context {
|
|
5
|
+
/** Plugin-owned Python runtime manager, provided once by the host-plane row. */
|
|
6
|
+
pythonRuntime: PythonRuntimeManager;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export declare const name = "python-runtime";
|
|
10
|
+
export declare function apply(ctx: Context): void;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { fileURLToPath } from 'node:url';
|
|
2
|
+
import { PythonRuntimeManager } from './python-runtime/manager.js';
|
|
3
|
+
import { readManifest } from './python-runtime/manifest.js';
|
|
4
|
+
import { detectPlatform } from './python-runtime/platform.js';
|
|
5
|
+
import { createPythonRuntimeHandler } from './python-runtime/routes.js';
|
|
6
|
+
import { resolveDshHome } from './python-runtime/paths.js';
|
|
7
|
+
export const name = 'python-runtime';
|
|
8
|
+
export function apply(ctx) {
|
|
9
|
+
if (ctx.get('pythonRuntime') !== undefined) {
|
|
10
|
+
ctx.logger?.error?.('python-runtime: 服务已存在,拒绝重复 provide。');
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const profile = ctx.get('profileContext');
|
|
14
|
+
const manifestPath = fileURLToPath(new URL('../runtime/manifest.json', import.meta.url));
|
|
15
|
+
const target = detectPlatform();
|
|
16
|
+
let manifest;
|
|
17
|
+
try {
|
|
18
|
+
manifest = readManifest(manifestPath);
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
ctx.logger?.error?.(`python-runtime: ${String(error)}`);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const manager = new PythonRuntimeManager({
|
|
25
|
+
manifest,
|
|
26
|
+
dshHome: resolveDshHome(profile?.home),
|
|
27
|
+
workerPath: fileURLToPath(new URL('../python/worker.py', import.meta.url)),
|
|
28
|
+
target,
|
|
29
|
+
...(profile?.dir === undefined ? {} : { profileDir: profile.dir }),
|
|
30
|
+
...(profile?.packageManager === undefined ? {} : { packageManager: profile.packageManager }),
|
|
31
|
+
...(process.env.CG_PYTHON_REGISTRY === undefined ? {} : { registry: process.env.CG_PYTHON_REGISTRY }),
|
|
32
|
+
logger: ctx.logger,
|
|
33
|
+
});
|
|
34
|
+
ctx.provide('pythonRuntime', manager);
|
|
35
|
+
ctx.effect(() => () => { void manager.dispose(); }, 'python-runtime: dispose');
|
|
36
|
+
ctx.inject(['webServer'], (webCtx) => {
|
|
37
|
+
const web = webCtx;
|
|
38
|
+
const authorize = (req) => {
|
|
39
|
+
let connection;
|
|
40
|
+
try {
|
|
41
|
+
connection = ctx.get('connection');
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return 401;
|
|
45
|
+
}
|
|
46
|
+
if (connection === undefined || connection === null)
|
|
47
|
+
return undefined;
|
|
48
|
+
if (typeof connection.requestRejection !== 'function')
|
|
49
|
+
return undefined;
|
|
50
|
+
return connection.requestRejection(req);
|
|
51
|
+
};
|
|
52
|
+
const handler = createPythonRuntimeHandler(manager, authorize);
|
|
53
|
+
web.effect(() => web.webServer.register({
|
|
54
|
+
kind: 'prefix',
|
|
55
|
+
path: '/python-runtime',
|
|
56
|
+
handler: handler,
|
|
57
|
+
}), 'python-runtime: HTTP routes');
|
|
58
|
+
});
|
|
59
|
+
ctx.logger?.info?.(`python-runtime: active (${target.target ?? 'unsupported'})`);
|
|
60
|
+
}
|