@celljs/ta-plot 0.0.0-stage → 4.15.2
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.en.md +12 -0
- package/README.md +11 -2
- package/extension/index.mjs +121 -0
- package/package.json +55 -4
- package/web/index.js +1320 -0
package/README.en.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# Cell - Ta Plot
|
|
2
|
+
|
|
3
|
+
A pi package that comes with Ta (on by default; Settings → Extensions of the web client turns it off): Function plot. When a reply talks of functions and curves, Ta draws them as animated plots rather than describing them or drawing them with a chart of points.
|
|
4
|
+
|
|
5
|
+
- **Functions and curves**: under the Ta service, every turn's system prompt gets a `<ta-plot>` section: to show a function or a curve (a parabola, a hyperbola, sin x, a family of curves, a graph to explain a point), Ta writes a `ta-plot` block (JSON), which the web client draws on a canvas: a grid and axes, curves drawn in one after another, parametric curves, dashed guides (asymptotes, axes of symmetry), labeled points, and with `dot` a point running along the curves.
|
|
6
|
+
- **Moving, dragged**: a param that uses `t` (seconds since shown) moves its curves; a param written as a slider (`{"value", "min", "max", "step"?, "label"?}`) gets a slider under the plot, and moving across the plot drags the first one; a curve with `family` draws itself faintly for a set of values of the slider too, the current one bold: a family of curves.
|
|
7
|
+
- **Surfaces and curves in space**: `surfaces` (z = f(x, y), colored by height without a color) or curves and points with `z` are drawn in a box of the axes on the 3D engine of the page (`ui.Game3D`), turning slowly, turned by dragging; params, sliders and `t` work the same.
|
|
8
|
+
- **Computed, never run**: expressions are read by the package's own parser (numbers, `+ - * / % ^`, parentheses, variables, the functions of Math, pi and e), never run as code; a plot that cannot be drawn says why and shows its text.
|
|
9
|
+
- **Light on power**: it animates only while the card is seen, and stops once drawn when nothing moves.
|
|
10
|
+
- **Elsewhere**: external agents (ta-agents) get the section too; replies to WeChat and WeCom bots draw no plot; blocks of language `plot` are drawn as well (the name the user extension had).
|
|
11
|
+
|
|
12
|
+
The format is in `web-src/plot-spec.ts`, the prompt in `web-src/plot-prompt.ts`, expressions in `web-src/plot-expression.ts`.
|
package/README.md
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Cell - Ta Plot
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Ta 自带的 pi 包(默认开启,可在 Web 客户端的 设置 → 扩展 关闭):函数图像。回复里讲到函数和曲线时,Ta 直接把它画成带动画的图像,而不是用文字描述或拿点拼图。
|
|
4
|
+
|
|
5
|
+
- **画函数和曲线**:在 Ta 服务下,每一轮的系统提示里多一段 `<ta-plot>`:要展示函数或曲线(抛物线、双曲线、sin x、一族曲线、解释某个概念的图)时,写一个 `ta-plot` 代码块(JSON),Web 把它画在画布上:网格与坐标轴、逐条画出的曲线、参数方程、虚线(渐近线、对称轴)、标注点,`dot` 让一个小点沿曲线跑。
|
|
6
|
+
- **会动、可拖**:参数用到 `t`(显示后的秒数)时曲线跟着动;参数写成滑块(`{"value", "min", "max", "step"?, "label"?}`)时下方出现滑条,在图上左右拖动也能调第一个滑块;曲线加 `family` 后,会把滑块取的一组值淡淡地都画出来,当前值加粗,即曲线族。
|
|
7
|
+
- **曲面与空间曲线**:写 `surfaces`(z = f(x, y),不给颜色就按高度着色)或带 `z` 的曲线、点时,用页面的 3D 引擎(`ui.Game3D`)画在一个坐标盒里,慢慢自转,拖动旋转;参数、滑块和 `t` 照样可用。
|
|
8
|
+
- **只算不跑**:表达式由包自己的解析器读(数字、`+ - * / % ^`、括号、变量、Math 的函数与 pi、e),从不当作代码执行;画不出来时显示原因和原文。
|
|
9
|
+
- **省电**:动画只在卡片可见时跑,没有会动的东西时画完就停。
|
|
10
|
+
- **别处**:外部 Agent(ta-agents)也会拿到这段写法;发到微信、企业微信机器人的回复不画图;`plot` 语言的代码块也照样画(原先的用户扩展用这个名字)。
|
|
11
|
+
|
|
12
|
+
格式见 `web-src/plot-spec.ts`,提示词见 `web-src/plot-prompt.ts`,表达式见 `web-src/plot-expression.ts`。
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// ../ta-protocol/esm/common/ta-extension-host.js
|
|
2
|
+
var TA_HOST_EVENT = "ta:host";
|
|
3
|
+
var TaHostUtil;
|
|
4
|
+
(function(TaHostUtil2) {
|
|
5
|
+
TaHostUtil2.WAIT_MS = 1000;
|
|
6
|
+
function reach(events, name, waitMs = TaHostUtil2.WAIT_MS) {
|
|
7
|
+
return new Promise((resolve) => {
|
|
8
|
+
let heard = false;
|
|
9
|
+
let answered = false;
|
|
10
|
+
const timer = setTimeout(() => resolve(undefined), waitMs);
|
|
11
|
+
const request = {
|
|
12
|
+
name,
|
|
13
|
+
heard: () => {
|
|
14
|
+
heard = true;
|
|
15
|
+
},
|
|
16
|
+
resolve: (answer) => {
|
|
17
|
+
answered = true;
|
|
18
|
+
clearTimeout(timer);
|
|
19
|
+
resolve(answer);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
events.emit(TA_HOST_EVENT, request);
|
|
23
|
+
if (!heard && !answered) {
|
|
24
|
+
clearTimeout(timer);
|
|
25
|
+
resolve(undefined);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
TaHostUtil2.reach = reach;
|
|
30
|
+
function marked(systemPrompt, mark) {
|
|
31
|
+
return role(systemPrompt).includes(mark);
|
|
32
|
+
}
|
|
33
|
+
TaHostUtil2.marked = marked;
|
|
34
|
+
function role(systemPrompt) {
|
|
35
|
+
if (!systemPrompt) {
|
|
36
|
+
return "";
|
|
37
|
+
}
|
|
38
|
+
const open = `<addendum>
|
|
39
|
+
`;
|
|
40
|
+
const start = systemPrompt.indexOf(open);
|
|
41
|
+
const context = systemPrompt.indexOf(`<project_context>
|
|
42
|
+
`);
|
|
43
|
+
if (start < 0 || context >= 0 && context < start) {
|
|
44
|
+
return "";
|
|
45
|
+
}
|
|
46
|
+
const end = systemPrompt.indexOf(`
|
|
47
|
+
</addendum>`, start);
|
|
48
|
+
return systemPrompt.slice(start + open.length, end < 0 ? undefined : end);
|
|
49
|
+
}
|
|
50
|
+
TaHostUtil2.role = role;
|
|
51
|
+
})(TaHostUtil || (TaHostUtil = {}));
|
|
52
|
+
var TA_ASK_ENTRY = "ta-ask";
|
|
53
|
+
var TaAskUtil;
|
|
54
|
+
(function(TaAskUtil2) {
|
|
55
|
+
const LENGTH = 120;
|
|
56
|
+
function entry(question) {
|
|
57
|
+
const line = question.replace(/\s+/g, " ").trim();
|
|
58
|
+
return { question: line.length > LENGTH ? `${line.slice(0, LENGTH - 1)}…` : line };
|
|
59
|
+
}
|
|
60
|
+
TaAskUtil2.entry = entry;
|
|
61
|
+
function asking(entries) {
|
|
62
|
+
var _a;
|
|
63
|
+
for (let i = entries.length - 1;i >= 0; i--) {
|
|
64
|
+
const entry2 = entries[i];
|
|
65
|
+
if (entry2.type === "message" && ((_a = entry2.message) === null || _a === undefined ? undefined : _a.role) === "user") {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (entry2.type === "custom" && entry2.customType === TA_ASK_ENTRY) {
|
|
69
|
+
return read(entry2);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
TaAskUtil2.asking = asking;
|
|
75
|
+
function next(asked, entry2) {
|
|
76
|
+
var _a;
|
|
77
|
+
if (entry2.type === "message" && ((_a = entry2.message) === null || _a === undefined ? undefined : _a.role) === "user") {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
return entry2.type === "custom" && entry2.customType === TA_ASK_ENTRY ? read(entry2) : asked;
|
|
81
|
+
}
|
|
82
|
+
TaAskUtil2.next = next;
|
|
83
|
+
function read(found) {
|
|
84
|
+
var _a;
|
|
85
|
+
const question = (_a = found.data) === null || _a === undefined ? undefined : _a.question;
|
|
86
|
+
if (typeof question !== "string" || !question.trim()) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const at = found.timestamp ? Date.parse(found.timestamp) : Number.NaN;
|
|
90
|
+
return { question: entry(question).question, at: Number.isNaN(at) ? 0 : at };
|
|
91
|
+
}
|
|
92
|
+
})(TaAskUtil || (TaAskUtil = {}));
|
|
93
|
+
|
|
94
|
+
// web-src/plot-protocol.ts
|
|
95
|
+
var NAME = "@celljs/ta-plot";
|
|
96
|
+
var LANGUAGE = "ta-plot";
|
|
97
|
+
|
|
98
|
+
// web-src/plot-prompt.ts
|
|
99
|
+
var PLOT_PROMPT = `To show the user a function or a curve (a parabola, a hyperbola, sin x, a family of curves with a parameter to drag, a surface z = f(x, y), a curve in space, a graph to explain a point), write a fenced block of language ${LANGUAGE} holding JSON rather than describing it or drawing it with a chart of points; Ta draws it, animated:
|
|
100
|
+
{"title"?: string, "x": [min, max], "y": [min, max], "params"?: {"a": "1.5 + 0.5*sin(t)", "b": 1}, "curves": [...], "points"?: [{"x": 0, "y": 0, "label": "vertex"}], "dot"?: true}
|
|
101
|
+
A param may instead be a slider the user drags, {"c": {"value": 1, "min": -2, "max": 3, "step"?: 0.1, "label"?: string}}, also dragged by moving across the plot; give one whenever the user wants to see how a param changes a curve. A curve with "family": "c" (or {"param": "c", "count": 11}) also draws itself faintly for evenly spaced values of the slider c, the current one bold: a family of curves; add its envelope as a dashed curve when there is one.
|
|
102
|
+
A curve is {"y": "a*x^2"} (y of x, over the x range) or {"x": "a*cosh(s)", "y": "b*sinh(s)", "s": [-2, 2]} (parametric, s over its range), with "label"?, "dash"?: true (asymptotes, axes of symmetry, guides) and "color"? (a CSS color).
|
|
103
|
+
In space: "surfaces": [{"z": "sin(x)*cos(y)", "label"?, "color"?}] (z of x and y, over the x and y ranges, colored by height without a color), a curve with "z" too ({"x": "cos(s)", "y": "sin(s)", "z": "s/4", "s": [0, 12]}) and points with "z"; an optional "z": [min, max]. Ta draws it in 3D, turning slowly, the user turns it by dragging; params, sliders and t work the same.
|
|
104
|
+
Expressions: numbers, + - * / ^ %, parentheses, x, y (of a surface), s, t (seconds since shown: a param or curve that uses it moves), the params (a param may use the sliders and the params before it), and sin cos tan asin acos atan atan2 sinh cosh tanh asinh acosh atanh sqrt cbrt abs exp log log2 log10 pow min max floor ceil round sign hypot pi e. Curves draw in when shown; dot puts a point running along the solid curves. Point coordinates may be expressions too. Labels and title in the user's language, short.`;
|
|
105
|
+
|
|
106
|
+
// web-src/extension.ts
|
|
107
|
+
function activate(pi) {
|
|
108
|
+
let served;
|
|
109
|
+
const underServe = () => served ??= TaHostUtil.reach(pi.events, NAME).then((answer) => answer !== undefined);
|
|
110
|
+
pi.on("session_start", () => {
|
|
111
|
+
underServe();
|
|
112
|
+
});
|
|
113
|
+
pi.on("before_agent_start", async (event) => {
|
|
114
|
+
if (await underServe()) {
|
|
115
|
+
event.systemPromptOptions.sections = { ...event.systemPromptOptions.sections, [LANGUAGE]: PLOT_PROMPT };
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
export {
|
|
120
|
+
activate as default
|
|
121
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/ta-plot",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "4.15.2",
|
|
4
|
+
"description": "A pi package for Ta: functions and curves of replies drawn as animated plots, with params to drag",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"pi-package"
|
|
7
|
+
],
|
|
8
|
+
"homepage": "https://github.com/cellbang/ta",
|
|
9
|
+
"bugs": {
|
|
10
|
+
"url": "https://github.com/cellbang/ta/issues"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/cellbang/cell.git"
|
|
15
|
+
},
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"files": [
|
|
18
|
+
"web",
|
|
19
|
+
"extension",
|
|
20
|
+
"!**/*.map"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsc -b && bun build web-src/index.tsx --format esm --target browser --outfile web/index.js && bun build web-src/extension.ts --format esm --target node --outfile extension/index.mjs",
|
|
24
|
+
"clean": "rm -rf lib web extension *.tsbuildinfo",
|
|
25
|
+
"test": "bun test ./lib",
|
|
26
|
+
"watch": "tsc -b -w --preserveWatchOutput"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@celljs/ta-protocol": "4.15.2",
|
|
30
|
+
"@celljs/ta-web": "4.15.2",
|
|
31
|
+
"@types/react": "^19.3.0"
|
|
32
|
+
},
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"pi": {
|
|
37
|
+
"title": {
|
|
38
|
+
"en": "Function plot",
|
|
39
|
+
"zh-CN": "函数图像",
|
|
40
|
+
"zh-TW": "函數圖像",
|
|
41
|
+
"ja-JP": "関数グラフ"
|
|
42
|
+
},
|
|
43
|
+
"summary": {
|
|
44
|
+
"en": "Draws the functions and curves Ta talks about (parabolas, sine waves, families of curves) in the reply, animated, with params to drag.",
|
|
45
|
+
"zh-CN": "把回复里的函数和曲线(抛物线、正弦波、曲线族)直接画成带动画的图像,参数可以拖动。",
|
|
46
|
+
"zh-TW": "把回覆裡的函數和曲線(拋物線、正弦波、曲線族)直接畫成帶動畫的圖像,參數可以拖動。",
|
|
47
|
+
"ja-JP": "返信に出てくる関数や曲線(放物線、正弦波、曲線族)をアニメーション付きのグラフで描き、パラメータはドラッグで動かせます。"
|
|
48
|
+
},
|
|
49
|
+
"icon": "ChartSpline",
|
|
50
|
+
"web": [
|
|
51
|
+
"./web/index.js"
|
|
52
|
+
],
|
|
53
|
+
"extensions": [
|
|
54
|
+
"./extension/index.mjs"
|
|
55
|
+
]
|
|
56
|
+
}
|
|
57
|
+
}
|