@lincyaw/addon-exec 1.0.0-rc.4 → 1.0.0-rc.40
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/package.json +6 -6
- package/src/node/presmium.ts +2 -3
- package/src/node/run-panel.ts +1 -20
- package/src/node/step-run.ts +17 -29
- package/src/node/code-component.ts +0 -48
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lincyaw/addon-exec",
|
|
3
|
-
"version": "1.0.0-rc.
|
|
3
|
+
"version": "1.0.0-rc.40",
|
|
4
4
|
"description": "Presmium plugin: run panels for code, step-by-step execution and recorded outputs; Python in the browser with Pyodide",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -40,14 +40,14 @@
|
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
42
|
"react": "^18.3.1",
|
|
43
|
-
"@lincyaw/host": "^1.0.0-rc.
|
|
44
|
-
"@lincyaw/kernel": "^1.0.0-rc.
|
|
43
|
+
"@lincyaw/host": "^1.0.0-rc.40",
|
|
44
|
+
"@lincyaw/kernel": "^1.0.0-rc.40"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"react": "18.3.1",
|
|
48
48
|
"@types/react": "18.3.28",
|
|
49
|
-
"@lincyaw/cli": "1.0.0-rc.
|
|
50
|
-
"@lincyaw/host": "1.0.0-rc.
|
|
51
|
-
"@lincyaw/kernel": "1.0.0-rc.
|
|
49
|
+
"@lincyaw/cli": "1.0.0-rc.40",
|
|
50
|
+
"@lincyaw/host": "1.0.0-rc.40",
|
|
51
|
+
"@lincyaw/kernel": "1.0.0-rc.40"
|
|
52
52
|
}
|
|
53
53
|
}
|
package/src/node/presmium.ts
CHANGED
|
@@ -4,9 +4,9 @@ import { EXEC_ID } from '../domain/index.ts'
|
|
|
4
4
|
import { browserImportsCheck } from './browser-imports.ts'
|
|
5
5
|
import { rendererExtensions } from './notebook-cells.ts'
|
|
6
6
|
import { pythonExecutor } from './python.ts'
|
|
7
|
-
import {
|
|
7
|
+
import { runPanelFallbackPass, runPanelPass } from './run-panel.ts'
|
|
8
8
|
import { scriptExecutor } from './script.ts'
|
|
9
|
-
import { stepCountPass
|
|
9
|
+
import { stepCountPass } from './step-run.ts'
|
|
10
10
|
import { timeoutPass } from './timeouts.ts'
|
|
11
11
|
import { pyodideAssets } from './pyodide.ts'
|
|
12
12
|
|
|
@@ -16,7 +16,6 @@ const plugin: PresmiumNodePlugin = {
|
|
|
16
16
|
id: EXEC_ID,
|
|
17
17
|
version,
|
|
18
18
|
kernel: '^1.0.0',
|
|
19
|
-
directives: [runPanelDirective, stepRunDirective],
|
|
20
19
|
passes: [timeoutPass, runPanelPass, stepCountPass, runPanelFallbackPass],
|
|
21
20
|
checks: [browserImportsCheck],
|
|
22
21
|
executors: [pythonExecutor, scriptExecutor],
|
package/src/node/run-panel.ts
CHANGED
|
@@ -1,25 +1,6 @@
|
|
|
1
1
|
import { componentCode, docBlocks } from '@lincyaw/kernel'
|
|
2
|
-
import type {
|
|
2
|
+
import type { IRPass, MdastNode } from '@lincyaw/kernel'
|
|
3
3
|
import { EXEC_ID, paramsPrelude, shownOutput } from '../domain/index.ts'
|
|
4
|
-
import { codeComponent } from './code-component.ts'
|
|
5
|
-
|
|
6
|
-
export const runPanelDirective: DirectiveSpec = {
|
|
7
|
-
name: 'run-panel',
|
|
8
|
-
arg: true,
|
|
9
|
-
body: true,
|
|
10
|
-
options: { params: 'string', timeout: 'number', recorded: 'string', run: 'string' },
|
|
11
|
-
toIR(input, ctx) {
|
|
12
|
-
const when = input.options.run === undefined ? 'both' : String(input.options.run).trim()
|
|
13
|
-
if (when !== 'both' && when !== 'live') ctx.report({ checkId: 'presmium.exec.syntax', severity: 'warning', message: `{run-panel} :run: ${when} is not both or live; using both` })
|
|
14
|
-
const node = codeComponent('PmRunPanel', input, ctx, { when: when === 'live' ? 'live' : 'both' })
|
|
15
|
-
const recorded = input.options.recorded
|
|
16
|
-
if (typeof recorded === 'string') {
|
|
17
|
-
node.props.recorded = recorded
|
|
18
|
-
node.fallback?.children?.push({ type: 'code', lang: 'text', value: recorded })
|
|
19
|
-
}
|
|
20
|
-
return { node }
|
|
21
|
-
},
|
|
22
|
-
}
|
|
23
4
|
|
|
24
5
|
/** Gives each run panel its place in the document and its code's executor and session, which its runs send. */
|
|
25
6
|
export const runPanelPass: IRPass = {
|
package/src/node/step-run.ts
CHANGED
|
@@ -1,35 +1,20 @@
|
|
|
1
1
|
import { componentCode, docBlocks, PROCESS_SANDBOX } from '@lincyaw/kernel'
|
|
2
|
-
import type {
|
|
3
|
-
import { minimalRunEnv } from '@lincyaw/kernel/node'
|
|
2
|
+
import type { IRPass, ProcessSandbox } from '@lincyaw/kernel'
|
|
3
|
+
import { directSandbox, minimalRunEnv } from '@lincyaw/kernel/node'
|
|
4
4
|
import { configuredTimeout, EXEC_ID, execConfig, isPython, stringArgs } from '../domain/index.ts'
|
|
5
|
-
import { codeComponent } from './code-component.ts'
|
|
6
5
|
import { pythonExecutor } from './python.ts'
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
arg: true,
|
|
11
|
-
body: true,
|
|
12
|
-
options: { steps: 'number', params: 'string', timeout: 'number' },
|
|
13
|
-
toIR(input, ctx) {
|
|
14
|
-
const node = codeComponent('PmStepRun', input, ctx)
|
|
15
|
-
const steps = Number(input.options.steps)
|
|
16
|
-
if (!Number.isInteger(steps) || steps < 1) {
|
|
17
|
-
ctx.report({ checkId: 'presmium.exec.syntax', severity: 'warning', message: '{step-run} needs :steps:, the number of lines the code executes; without it the slide has no steps' })
|
|
18
|
-
return { node }
|
|
19
|
-
}
|
|
20
|
-
node.props.steps = steps
|
|
21
|
-
return { node, span: steps }
|
|
22
|
-
},
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const traced = new Map<string, Promise<number | undefined>>()
|
|
7
|
+
const TRACE_CACHE_SIZE = 128
|
|
8
|
+
const traces = new WeakMap<ProcessSandbox, Map<string, Promise<number | undefined>>>()
|
|
26
9
|
|
|
27
|
-
function traceLength(code: string, args: Record<string, string>, maxEvents: number, timeoutMs: number, sandbox: ProcessSandbox
|
|
28
|
-
|
|
29
|
-
|
|
10
|
+
async function traceLength(deckDir: string, code: string, args: Record<string, string>, maxEvents: number, timeoutMs: number, sandbox: ProcessSandbox): Promise<number | undefined> {
|
|
11
|
+
let cache = traces.get(sandbox)
|
|
12
|
+
if (!cache) traces.set(sandbox, cache = new Map())
|
|
13
|
+
const key = JSON.stringify([deckDir, code, args, maxEvents, timeoutMs])
|
|
14
|
+
let found = cache.get(key)
|
|
30
15
|
if (!found) {
|
|
31
16
|
const program = `import __main__\nprint(__main__._pm_trace(${JSON.stringify(code)}, ${JSON.stringify(JSON.stringify(args))}, '', '{}', ${maxEvents}))\n`
|
|
32
|
-
found = pythonExecutor.run({ lang: 'python', code: program, timeoutMs }, undefined, { env: minimalRunEnv(),
|
|
17
|
+
found = pythonExecutor.run({ lang: 'python', code: program, timeoutMs }, undefined, { env: minimalRunEnv(), sandbox }).then((result) => {
|
|
33
18
|
if (!result.ok) return undefined
|
|
34
19
|
try {
|
|
35
20
|
const trace = JSON.parse(result.stdout) as { events: unknown[], truncated: boolean }
|
|
@@ -38,9 +23,12 @@ function traceLength(code: string, args: Record<string, string>, maxEvents: numb
|
|
|
38
23
|
return undefined
|
|
39
24
|
}
|
|
40
25
|
})
|
|
41
|
-
|
|
26
|
+
cache.set(key, found)
|
|
27
|
+
while (cache.size > TRACE_CACHE_SIZE) cache.delete(cache.keys().next().value!)
|
|
42
28
|
}
|
|
43
|
-
|
|
29
|
+
let length: number | undefined
|
|
30
|
+
try { return length = await found }
|
|
31
|
+
finally { if (length === undefined && cache.get(key) === found) cache.delete(key) }
|
|
44
32
|
}
|
|
45
33
|
|
|
46
34
|
/** Warns when a step run declares another number of steps than the lines its code executes. */
|
|
@@ -50,14 +38,14 @@ export const stepCountPass: IRPass = {
|
|
|
50
38
|
async run(doc, ctx) {
|
|
51
39
|
if (!pythonExecutor.supports('python')) return
|
|
52
40
|
const { maxTraceEvents } = execConfig(ctx.config)
|
|
53
|
-
const sandbox = ctx.service?.(PROCESS_SANDBOX)
|
|
41
|
+
const sandbox = ctx.service?.(PROCESS_SANDBOX) ?? directSandbox
|
|
54
42
|
for (const { section, block } of docBlocks(doc)) {
|
|
55
43
|
const { node } = block
|
|
56
44
|
if (node.type !== 'component' || node.plugin !== EXEC_ID || node.name !== 'PmStepRun' || typeof node.props.steps !== 'number') continue
|
|
57
45
|
const code = componentCode(node)
|
|
58
46
|
if (!code || !isPython(code.lang)) continue
|
|
59
47
|
const timeoutMs = (typeof node.props.timeout === 'number' ? node.props.timeout : undefined) ?? configuredTimeout(ctx.configFor(section)) ?? execConfig(ctx.config).timeoutMs
|
|
60
|
-
const lines = await traceLength(code.value, stringArgs(node.props.params as Record<string, unknown> | undefined), maxTraceEvents, timeoutMs, sandbox)
|
|
48
|
+
const lines = await traceLength(ctx.deckDir, code.value, stringArgs(node.props.params as Record<string, unknown> | undefined), maxTraceEvents, timeoutMs, sandbox)
|
|
61
49
|
if (lines === undefined || lines === node.props.steps) continue
|
|
62
50
|
ctx.report({
|
|
63
51
|
checkId: 'presmium.exec',
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import type { CodeNode, ComponentNode, DirectiveContext, DirectiveInput, IRBlock, RunSpec } from '@lincyaw/kernel'
|
|
2
|
-
import { parse } from 'yaml'
|
|
3
|
-
import { configuredTimeout, EXEC_ID, isPython, isScript, stringArgs } from '../domain/index.ts'
|
|
4
|
-
|
|
5
|
-
type Params = Record<string, unknown>
|
|
6
|
-
|
|
7
|
-
function params(value: unknown, lang: string, ctx: DirectiveContext): Params | undefined {
|
|
8
|
-
if (value === undefined) return undefined
|
|
9
|
-
let data = value
|
|
10
|
-
try {
|
|
11
|
-
if (typeof value === 'string') data = parse(value)
|
|
12
|
-
}
|
|
13
|
-
catch {}
|
|
14
|
-
const variables = isPython(lang) || isScript(lang)
|
|
15
|
-
if (data !== null && typeof data === 'object' && !Array.isArray(data)
|
|
16
|
-
&& (!variables || Object.values(data).every(v => ['string', 'number', 'boolean'].includes(typeof v)))) {
|
|
17
|
-
return data as Params
|
|
18
|
-
}
|
|
19
|
-
ctx.report({ checkId: 'presmium.exec.syntax', severity: 'error', message: variables ? ':params: must be a mapping of names to values, e.g. {n: 5}' : ':params: must be a mapping, e.g. {n: 5}' })
|
|
20
|
-
return undefined
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function positive(value: unknown): number | undefined {
|
|
24
|
-
const n = Number(value)
|
|
25
|
-
return value !== undefined && Number.isFinite(n) && n > 0 ? n : undefined
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** The component of a directive whose body is code (the run panel, the step run), with the code as its child block. */
|
|
29
|
-
export function codeComponent(name: string, input: DirectiveInput, ctx: DirectiveContext, run?: Omit<RunSpec, 'lang'>): ComponentNode {
|
|
30
|
-
const lang = input.arg?.trim() || 'python'
|
|
31
|
-
const value = input.body.replace(/\n+$/, '')
|
|
32
|
-
const props: Record<string, unknown> = { lang, code: value }
|
|
33
|
-
const values = params(input.options.params, lang, ctx)
|
|
34
|
-
if (values) props.params = values
|
|
35
|
-
const timeout = positive(input.options.timeout) ?? configuredTimeout(ctx.config)
|
|
36
|
-
if (timeout) props.timeout = timeout
|
|
37
|
-
const code: CodeNode = { type: 'code', lang, value }
|
|
38
|
-
if (run) code.run = { lang, ...run, ...values ? { args: stringArgs(values) } : {}, ...timeout ? { timeoutMs: timeout } : {} }
|
|
39
|
-
const source: IRBlock = { id: 'source', role: 'visual', at: 0, source: ctx.source, node: code }
|
|
40
|
-
return {
|
|
41
|
-
type: 'component',
|
|
42
|
-
plugin: EXEC_ID,
|
|
43
|
-
name,
|
|
44
|
-
props,
|
|
45
|
-
children: [source],
|
|
46
|
-
fallback: { type: 'root', children: [{ type: 'code', lang, value }] },
|
|
47
|
-
}
|
|
48
|
-
}
|