@praecise/conversation 0.1.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/LICENSE +202 -0
- package/README.md +72 -0
- package/conversation.css +90 -0
- package/index.js +235 -0
- package/package.json +16 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# @praecise/conversation
|
|
2
|
+
|
|
3
|
+
What an agent is doing, while it does it.
|
|
4
|
+
|
|
5
|
+
A run already tells the whole story on the wire: which self was asked, what it
|
|
6
|
+
drew on, which model answered, whether a verifier agreed, what was checked, what
|
|
7
|
+
it cost. Most interfaces throw that away and show a spinner — so a person can't
|
|
8
|
+
tell thinking from broken, or an answer that was checked from one that was
|
|
9
|
+
guessed.
|
|
10
|
+
|
|
11
|
+
This renders those frames as they arrive: one line per step, openable for the
|
|
12
|
+
detail underneath, collapsing to a summary when the answer lands. **Nothing here
|
|
13
|
+
invents a step.** Every line is a frame the server actually sent.
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm i @praecise/conversation
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Use
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
import { mountRun, readRun } from "@praecise/conversation";
|
|
23
|
+
import "@praecise/conversation/conversation.css";
|
|
24
|
+
|
|
25
|
+
const run = mountRun(document.querySelector("#trail"), { self: "economist" });
|
|
26
|
+
|
|
27
|
+
const res = await fetch("/chat/fast", { method: "POST", body: JSON.stringify({ message }) });
|
|
28
|
+
const text = await readRun(res.body, run, (sofar) => {
|
|
29
|
+
answerEl.innerHTML = renderMarkdown(sofar); // your renderer, your markup
|
|
30
|
+
});
|
|
31
|
+
run.settle(Boolean(text));
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
`readRun` is optional — if you already read the stream, hand each frame to
|
|
35
|
+
`run.frame(evt)` and call `run.settle(ok)` at the end.
|
|
36
|
+
|
|
37
|
+
## What it shows
|
|
38
|
+
|
|
39
|
+
| Step | Line |
|
|
40
|
+
|---|---|
|
|
41
|
+
| `selves` | context from economist · drew on 3 things |
|
|
42
|
+
| `routing` | routed to fleet/qwen3.8-27b · verified · difficulty 0.40 |
|
|
43
|
+
| `grounding` | grounding with live data |
|
|
44
|
+
| `checking` | a second pass is checking the answer |
|
|
45
|
+
| `checked` | checked and kept · agreement 1.00 |
|
|
46
|
+
| `done` | answered · 3,640 in · 2 out · fleet/qwen3.8-27b |
|
|
47
|
+
|
|
48
|
+
A step it does not know still appears, named as it came: a newer server never
|
|
49
|
+
breaks an older page. Any step with detail opens to the raw frame.
|
|
50
|
+
|
|
51
|
+
## Styling
|
|
52
|
+
|
|
53
|
+
No framework, no build step: it makes DOM nodes and reads the host page's own
|
|
54
|
+
CSS variables, so it looks like the product it is in rather than like itself.
|
|
55
|
+
|
|
56
|
+
```css
|
|
57
|
+
.pc-run { --pc-ink: #1a1a1a; --pc-quiet: #6b6b6b; --pc-rule: #e3e4e5; --pc-signal: #4b6a00; }
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
It also falls back to `--ink`, `--quiet`, `--rule`, `--signal-deep` and
|
|
61
|
+
`--paper-dim` if a product already defines those, and to plain greys if it
|
|
62
|
+
defines nothing. Every class is yours to restyle: `.pc-run`, `.pc-run-head`,
|
|
63
|
+
`.pc-run-steps`, `.pc-run-step`, `.pc-run-line`, `.pc-run-title`,
|
|
64
|
+
`.pc-run-note`, `.pc-run-detail`.
|
|
65
|
+
|
|
66
|
+
## Frames
|
|
67
|
+
|
|
68
|
+
The shapes are the [Praecise harness](https://www.npmjs.com/package/@praecise/harness)'s:
|
|
69
|
+
`RunStarted`, `StepStarted`, `StepFinished`, `TextMessageContent`, `RunFinished`,
|
|
70
|
+
`RunError`. Anything else is ignored rather than thrown on.
|
|
71
|
+
|
|
72
|
+
Apache-2.0.
|
package/conversation.css
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/* @praecise/conversation — the minimum, in the host page's own colours.
|
|
2
|
+
*
|
|
3
|
+
* Every colour and radius comes from a CSS variable with a plain fallback, so a
|
|
4
|
+
* product that defines --pc-ink or --ink gets its own palette without touching
|
|
5
|
+
* this file, and a product that defines nothing still reads. Flat by default:
|
|
6
|
+
* hairlines, no shadow, no gradient.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
.pc-run {
|
|
10
|
+
--_ink: var(--pc-ink, var(--ink, #1a1a1a));
|
|
11
|
+
--_quiet: var(--pc-quiet, var(--quiet, #6b6b6b));
|
|
12
|
+
--_rule: var(--pc-rule, var(--rule, #e3e4e5));
|
|
13
|
+
--_panel: var(--pc-panel, var(--paper-dim, #f5f5f4));
|
|
14
|
+
--_signal: var(--pc-signal, var(--signal-deep, #4b6a00));
|
|
15
|
+
--_bad: var(--pc-bad, var(--danger, #c1272d));
|
|
16
|
+
--_mono: var(--pc-mono, var(--mono, ui-monospace, SFMono-Regular, Menlo, Consolas, monospace));
|
|
17
|
+
|
|
18
|
+
margin: 0 0 0.75rem;
|
|
19
|
+
border-left: 2px solid var(--_rule);
|
|
20
|
+
padding-left: 0.75rem;
|
|
21
|
+
font-size: 0.8125rem;
|
|
22
|
+
color: var(--_quiet);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
.pc-run-head {
|
|
26
|
+
display: flex;
|
|
27
|
+
align-items: center;
|
|
28
|
+
gap: 0.5rem;
|
|
29
|
+
border: 0;
|
|
30
|
+
background: none;
|
|
31
|
+
padding: 0.15rem 0;
|
|
32
|
+
margin: 0;
|
|
33
|
+
color: inherit;
|
|
34
|
+
font: inherit;
|
|
35
|
+
font-family: var(--_mono);
|
|
36
|
+
cursor: pointer;
|
|
37
|
+
text-align: left;
|
|
38
|
+
}
|
|
39
|
+
.pc-run-head:hover .pc-run-label { color: var(--_ink); }
|
|
40
|
+
|
|
41
|
+
/* A mark that moves while the run does, and stops when it stops. */
|
|
42
|
+
.pc-run-dot {
|
|
43
|
+
width: 6px; height: 6px; flex: none;
|
|
44
|
+
background: var(--_signal);
|
|
45
|
+
border-radius: 50%;
|
|
46
|
+
animation: pc-run-pulse 1.1s ease-in-out infinite;
|
|
47
|
+
}
|
|
48
|
+
.pc-run-ok .pc-run-dot { background: var(--_signal); animation: none; }
|
|
49
|
+
.pc-run-bad .pc-run-dot { background: var(--_bad); animation: none; }
|
|
50
|
+
@keyframes pc-run-pulse { 0%, 100% { opacity: 0.25; } 50% { opacity: 1; } }
|
|
51
|
+
@media (prefers-reduced-motion: reduce) { .pc-run-dot { animation: none; } }
|
|
52
|
+
|
|
53
|
+
.pc-run-steps { list-style: none; margin: 0.35rem 0 0; padding: 0; display: grid; gap: 0.15rem; }
|
|
54
|
+
.pc-run-closed .pc-run-steps { display: none; }
|
|
55
|
+
|
|
56
|
+
.pc-run-line {
|
|
57
|
+
display: flex; gap: 0.5rem; align-items: baseline; flex-wrap: wrap;
|
|
58
|
+
width: 100%; text-align: left;
|
|
59
|
+
border: 0; background: none; padding: 0.1rem 0; margin: 0;
|
|
60
|
+
color: inherit; font: inherit; cursor: pointer;
|
|
61
|
+
}
|
|
62
|
+
.pc-run-line:disabled, .pc-run-bare { cursor: default; }
|
|
63
|
+
.pc-run-line:not(:disabled):hover .pc-run-title { color: var(--_ink); }
|
|
64
|
+
|
|
65
|
+
.pc-run-title::before {
|
|
66
|
+
content: "·";
|
|
67
|
+
color: var(--_rule);
|
|
68
|
+
margin-right: 0.4rem;
|
|
69
|
+
}
|
|
70
|
+
.pc-run-live .pc-run-title::before { content: "›"; color: var(--_signal); }
|
|
71
|
+
|
|
72
|
+
.pc-run-note { font-family: var(--_mono); font-size: 0.75rem; color: var(--_quiet); }
|
|
73
|
+
|
|
74
|
+
.pc-run-detail {
|
|
75
|
+
margin: 0.2rem 0 0.35rem 0.9rem;
|
|
76
|
+
padding: 0.5rem 0.6rem;
|
|
77
|
+
background: var(--_panel);
|
|
78
|
+
border: 1px solid var(--_rule);
|
|
79
|
+
border-radius: var(--pc-radius, 4px);
|
|
80
|
+
font-family: var(--_mono);
|
|
81
|
+
font-size: 0.7rem;
|
|
82
|
+
line-height: 1.5;
|
|
83
|
+
color: var(--_ink);
|
|
84
|
+
overflow-x: auto;
|
|
85
|
+
max-height: 16rem;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
@media (max-width: 480px) {
|
|
89
|
+
.pc-run-note { width: 100%; }
|
|
90
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @praecise/conversation — what an agent is doing, while it does it.
|
|
3
|
+
*
|
|
4
|
+
* A run already tells the whole story on the wire: which self was asked, what it
|
|
5
|
+
* drew on, which model answered, whether a verifier agreed, what was checked,
|
|
6
|
+
* what it cost. Most interfaces throw all of that away and show a spinner, so a
|
|
7
|
+
* person cannot tell thinking from broken, or an answer that was checked from
|
|
8
|
+
* one that was guessed.
|
|
9
|
+
*
|
|
10
|
+
* This renders those frames as they arrive: one line per step, openable for the
|
|
11
|
+
* detail underneath, collapsing to a single summary when the answer lands.
|
|
12
|
+
* Nothing here invents a step — every line is a frame the server sent.
|
|
13
|
+
*
|
|
14
|
+
* No framework and no build step: it makes DOM nodes and takes the host page's
|
|
15
|
+
* own CSS variables, so it looks like the product it is in rather than like
|
|
16
|
+
* itself. The stylesheet beside it is the minimum; a product may restyle every
|
|
17
|
+
* class.
|
|
18
|
+
*
|
|
19
|
+
* import { mountRun } from "@praecise/conversation";
|
|
20
|
+
*
|
|
21
|
+
* const run = mountRun(container); // returns a handle
|
|
22
|
+
* for await (const frame of frames) run.frame(frame);
|
|
23
|
+
* run.settle(true);
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/** Frame kinds this understands. Anything else still shows, named as it came. */
|
|
27
|
+
const KNOWN = new Set(["selves", "routing", "grounding", "checking", "checked", "done"]);
|
|
28
|
+
|
|
29
|
+
const number = (n) => (typeof n === "number" ? n.toLocaleString() : null);
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* One readable line for a step.
|
|
33
|
+
*
|
|
34
|
+
* The wording says what happened to the work, not what the system did to
|
|
35
|
+
* itself: "checked and kept" rather than "verifier returned agreement=1".
|
|
36
|
+
*/
|
|
37
|
+
function describe(kind, raw, opts) {
|
|
38
|
+
const d = raw || {};
|
|
39
|
+
switch (kind) {
|
|
40
|
+
case "selves":
|
|
41
|
+
return {
|
|
42
|
+
title: `context from ${d.self || opts.self || "the self"}`,
|
|
43
|
+
note: drewOn(d.drewOn),
|
|
44
|
+
};
|
|
45
|
+
case "routing":
|
|
46
|
+
return {
|
|
47
|
+
title: `routed to ${d.entry || "a model"}`,
|
|
48
|
+
note: [
|
|
49
|
+
d.verify ? "verified" : null,
|
|
50
|
+
typeof d.difficulty === "number" ? `difficulty ${d.difficulty.toFixed(2)}` : null,
|
|
51
|
+
d.rungs ? `${d.rungs} rungs` : null,
|
|
52
|
+
].filter(Boolean).join(" · "),
|
|
53
|
+
};
|
|
54
|
+
case "grounding":
|
|
55
|
+
return { title: "grounding with live data", note: d.source || "" };
|
|
56
|
+
case "checking":
|
|
57
|
+
return { title: "a second pass is checking the answer", note: "" };
|
|
58
|
+
case "checked":
|
|
59
|
+
return {
|
|
60
|
+
title: d.kept === false ? "the check disagreed, and it was rewritten" : "checked and kept",
|
|
61
|
+
note: typeof d.agreement === "number" ? `agreement ${d.agreement.toFixed(2)}` : "",
|
|
62
|
+
};
|
|
63
|
+
case "done":
|
|
64
|
+
return { title: "answered", note: usage(d.answer) };
|
|
65
|
+
default:
|
|
66
|
+
return { title: kind.replace(/[-_]/g, " "), note: "" };
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function drewOn(list) {
|
|
71
|
+
if (!Array.isArray(list) || !list.length) return "nothing held yet";
|
|
72
|
+
return `drew on ${list.length} ${list.length === 1 ? "thing" : "things"}`;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function usage(answer) {
|
|
76
|
+
const u = answer && answer.usage;
|
|
77
|
+
if (!u) return "";
|
|
78
|
+
const bits = [];
|
|
79
|
+
if (number(u.inputTokens)) bits.push(`${number(u.inputTokens)} in`);
|
|
80
|
+
if (number(u.outputTokens)) bits.push(`${number(u.outputTokens)} out`);
|
|
81
|
+
const path = answer.path;
|
|
82
|
+
if (Array.isArray(path) && path.length) bits.push(path[path.length - 1]);
|
|
83
|
+
return bits.join(" · ");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const el = (tag, cls, text) => {
|
|
87
|
+
const n = document.createElement(tag);
|
|
88
|
+
if (cls) n.className = cls;
|
|
89
|
+
if (text !== undefined) n.textContent = text;
|
|
90
|
+
return n;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Put a run's trail in `container` and return a handle.
|
|
95
|
+
*
|
|
96
|
+
* `opts.self` names the self when a frame does not. `opts.open` starts it
|
|
97
|
+
* expanded (the default while a run is live). `opts.labels` replaces any
|
|
98
|
+
* wording — a product that calls its selves something else says so here.
|
|
99
|
+
*/
|
|
100
|
+
export function mountRun(container, opts = {}) {
|
|
101
|
+
const box = el("div", "pc-run");
|
|
102
|
+
const head = el("button", "pc-run-head");
|
|
103
|
+
head.type = "button";
|
|
104
|
+
head.setAttribute("aria-expanded", "true");
|
|
105
|
+
const dot = el("span", "pc-run-dot");
|
|
106
|
+
const label = el("span", "pc-run-label", opts.labels?.working ?? "working");
|
|
107
|
+
head.append(dot, label);
|
|
108
|
+
|
|
109
|
+
const list = el("ol", "pc-run-steps");
|
|
110
|
+
head.addEventListener("click", () => {
|
|
111
|
+
const closed = box.classList.toggle("pc-run-closed");
|
|
112
|
+
head.setAttribute("aria-expanded", closed ? "false" : "true");
|
|
113
|
+
});
|
|
114
|
+
box.append(head, list);
|
|
115
|
+
if (opts.open === false) box.classList.add("pc-run-closed");
|
|
116
|
+
container.appendChild(box);
|
|
117
|
+
|
|
118
|
+
const rows = new Map();
|
|
119
|
+
const startedAt = Date.now();
|
|
120
|
+
let count = 0;
|
|
121
|
+
|
|
122
|
+
function line(kind, raw) {
|
|
123
|
+
const said = describe(kind, raw, opts);
|
|
124
|
+
const li = el("li", "pc-run-step pc-run-live");
|
|
125
|
+
const button = el("button", "pc-run-line");
|
|
126
|
+
button.type = "button";
|
|
127
|
+
const title = el("span", "pc-run-title", said.title);
|
|
128
|
+
const note = el("span", "pc-run-note", said.note);
|
|
129
|
+
button.append(title, note);
|
|
130
|
+
|
|
131
|
+
const detail = el("pre", "pc-run-detail");
|
|
132
|
+
detail.hidden = true;
|
|
133
|
+
const hasDetail = raw && Object.keys(raw).length > 0;
|
|
134
|
+
if (hasDetail) {
|
|
135
|
+
detail.textContent = JSON.stringify(raw, null, 2);
|
|
136
|
+
button.addEventListener("click", () => { detail.hidden = !detail.hidden; });
|
|
137
|
+
} else {
|
|
138
|
+
button.classList.add("pc-run-bare");
|
|
139
|
+
button.disabled = true;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
li.append(button, detail);
|
|
143
|
+
list.appendChild(li);
|
|
144
|
+
rows.set(kind, { li, title, note, detail, button });
|
|
145
|
+
count += 1;
|
|
146
|
+
label.textContent = said.title;
|
|
147
|
+
return rows.get(kind);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const handle = {
|
|
151
|
+
/** A step began, or was seen for the first time. */
|
|
152
|
+
step(kind, raw) {
|
|
153
|
+
if (!rows.has(kind)) line(kind, raw);
|
|
154
|
+
},
|
|
155
|
+
|
|
156
|
+
/** The same step finished: the line stays, the mark stops moving. */
|
|
157
|
+
finish(kind, raw) {
|
|
158
|
+
const row = rows.get(kind) ?? line(kind, raw);
|
|
159
|
+
row.li.className = "pc-run-step";
|
|
160
|
+
if (raw && Object.keys(raw).length) {
|
|
161
|
+
const said = describe(kind, raw, opts);
|
|
162
|
+
row.title.textContent = said.title;
|
|
163
|
+
row.note.textContent = said.note;
|
|
164
|
+
row.detail.textContent = JSON.stringify(raw, null, 2);
|
|
165
|
+
row.button.disabled = false;
|
|
166
|
+
row.button.classList.remove("pc-run-bare");
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* One frame off the wire, in the shape the Praecise harness sends. Unknown
|
|
172
|
+
* types are ignored rather than thrown on: a newer server must not break an
|
|
173
|
+
* older page.
|
|
174
|
+
*/
|
|
175
|
+
frame(evt) {
|
|
176
|
+
if (!evt || typeof evt !== "object") return;
|
|
177
|
+
const kind = evt.stepName || (evt.rawEvent && evt.rawEvent.kind) || "step";
|
|
178
|
+
switch (evt.type) {
|
|
179
|
+
case "StepStarted": return handle.step(kind, evt.rawEvent);
|
|
180
|
+
case "StepFinished": return handle.finish(kind, evt.rawEvent);
|
|
181
|
+
case "RunFinished": return evt.rawEvent ? handle.finish("done", evt.rawEvent) : undefined;
|
|
182
|
+
case "RunError": return handle.settle(false, evt.message);
|
|
183
|
+
default: return undefined;
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
|
|
187
|
+
/** The run ended. One line a person can leave closed. */
|
|
188
|
+
settle(ok, why) {
|
|
189
|
+
const seconds = ((Date.now() - startedAt) / 1000).toFixed(1);
|
|
190
|
+
box.classList.add("pc-run-closed", ok ? "pc-run-ok" : "pc-run-bad");
|
|
191
|
+
head.setAttribute("aria-expanded", "false");
|
|
192
|
+
for (const row of rows.values()) row.li.classList.remove("pc-run-live");
|
|
193
|
+
label.textContent = ok
|
|
194
|
+
? `${count} ${count === 1 ? "step" : "steps"} · ${seconds}s`
|
|
195
|
+
: `${why || "did not finish"} · ${seconds}s`;
|
|
196
|
+
},
|
|
197
|
+
|
|
198
|
+
/** The element, for a product that wants to move or remove it. */
|
|
199
|
+
element: box,
|
|
200
|
+
};
|
|
201
|
+
return handle;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Read an SSE body and hand every frame to a run. Returns the text that was
|
|
206
|
+
* streamed, so a caller can keep rendering the answer itself.
|
|
207
|
+
*/
|
|
208
|
+
export async function readRun(body, run, onText) {
|
|
209
|
+
const reader = body.getReader();
|
|
210
|
+
const decoder = new TextDecoder();
|
|
211
|
+
let buffer = "";
|
|
212
|
+
let text = "";
|
|
213
|
+
for (;;) {
|
|
214
|
+
const chunk = await reader.read();
|
|
215
|
+
if (chunk.done) break;
|
|
216
|
+
buffer += decoder.decode(chunk.value, { stream: true });
|
|
217
|
+
const frames = buffer.split("\n\n");
|
|
218
|
+
buffer = frames.pop() ?? "";
|
|
219
|
+
for (const frame of frames) {
|
|
220
|
+
const dataLine = frame.split("\n").find((l) => l.startsWith("data: "));
|
|
221
|
+
if (!dataLine) continue;
|
|
222
|
+
let evt;
|
|
223
|
+
try { evt = JSON.parse(dataLine.slice(6)); } catch { continue; }
|
|
224
|
+
if (evt.type === "TextMessageContent") {
|
|
225
|
+
text += evt.delta ?? evt.content ?? evt.text ?? "";
|
|
226
|
+
if (onText) onText(text, evt);
|
|
227
|
+
} else {
|
|
228
|
+
run.frame(evt);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return text;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export const KNOWN_STEPS = KNOWN;
|
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@praecise/conversation",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "What an agent is doing, while it does it: a live run trail from Praecise harness frames. No framework, no build step.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./index.js",
|
|
9
|
+
"./conversation.css": "./conversation.css"
|
|
10
|
+
},
|
|
11
|
+
"files": ["index.js", "conversation.css", "README.md", "LICENSE"],
|
|
12
|
+
"keywords": ["agents", "sse", "ag-ui", "praecise", "ui"],
|
|
13
|
+
"homepage": "https://platform.recursiveselves.com",
|
|
14
|
+
"publishConfig": { "access": "public" },
|
|
15
|
+
"sideEffects": ["*.css"]
|
|
16
|
+
}
|