@yaks/session 0.0.0-stage → 0.2.36
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 +1392 -2
- package/_dist/admission.d.ts +15 -0
- package/_dist/admission.d.ts.map +1 -0
- package/_dist/announce.d.ts +17 -0
- package/_dist/announce.d.ts.map +1 -0
- package/_dist/answer.d.ts +200 -0
- package/_dist/answer.d.ts.map +1 -0
- package/_dist/append.d.ts +10 -0
- package/_dist/append.d.ts.map +1 -0
- package/_dist/archive.d.ts +6 -0
- package/_dist/archive.d.ts.map +1 -0
- package/_dist/asks.d.ts +11 -0
- package/_dist/asks.d.ts.map +1 -0
- package/_dist/audit.d.ts +15 -0
- package/_dist/audit.d.ts.map +1 -0
- package/_dist/bounce.d.ts +19 -0
- package/_dist/bounce.d.ts.map +1 -0
- package/_dist/bus.d.ts +8 -0
- package/_dist/bus.d.ts.map +1 -0
- package/_dist/cache.d.ts +45 -0
- package/_dist/cache.d.ts.map +1 -0
- package/_dist/child-notice.d.ts +6 -0
- package/_dist/child-notice.d.ts.map +1 -0
- package/_dist/children.d.ts +59 -0
- package/_dist/children.d.ts.map +1 -0
- package/_dist/cli.d.ts +8 -0
- package/_dist/cli.d.ts.map +1 -0
- package/_dist/comp.d.ts +9 -0
- package/_dist/comp.d.ts.map +1 -0
- package/_dist/compact.d.ts +19 -0
- package/_dist/compact.d.ts.map +1 -0
- package/_dist/conversation.d.ts +158 -0
- package/_dist/conversation.d.ts.map +1 -0
- package/_dist/cost.d.ts +13 -0
- package/_dist/cost.d.ts.map +1 -0
- package/_dist/deferred.d.ts +79 -0
- package/_dist/deferred.d.ts.map +1 -0
- package/_dist/ended.d.ts +27 -0
- package/_dist/ended.d.ts.map +1 -0
- package/_dist/files.d.ts +73 -0
- package/_dist/files.d.ts.map +1 -0
- package/_dist/fork.d.ts +23 -0
- package/_dist/fork.d.ts.map +1 -0
- package/_dist/graft.d.ts +33 -0
- package/_dist/graft.d.ts.map +1 -0
- package/_dist/graph.d.ts +14 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/handoff.d.ts +29 -0
- package/_dist/handoff.d.ts.map +1 -0
- package/_dist/imported.d.ts +63 -0
- package/_dist/imported.d.ts.map +1 -0
- package/_dist/intake.d.ts +43 -0
- package/_dist/intake.d.ts.map +1 -0
- package/_dist/joined.d.ts +35 -0
- package/_dist/joined.d.ts.map +1 -0
- package/_dist/lease.d.ts +21 -0
- package/_dist/lease.d.ts.map +1 -0
- package/_dist/lines.d.ts +13 -0
- package/_dist/lines.d.ts.map +1 -0
- package/_dist/mod.d.ts +107 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/naming.d.ts +10 -0
- package/_dist/naming.d.ts.map +1 -0
- package/_dist/native.d.ts +13 -0
- package/_dist/native.d.ts.map +1 -0
- package/_dist/parts.d.ts +145 -0
- package/_dist/parts.d.ts.map +1 -0
- package/_dist/plugin.d.ts +33 -0
- package/_dist/plugin.d.ts.map +1 -0
- package/_dist/project.d.ts +67 -0
- package/_dist/project.d.ts.map +1 -0
- package/_dist/prose.d.ts +7 -0
- package/_dist/prose.d.ts.map +1 -0
- package/_dist/providers.d.ts +36 -0
- package/_dist/providers.d.ts.map +1 -0
- package/_dist/reach.d.ts +39 -0
- package/_dist/reach.d.ts.map +1 -0
- package/_dist/react.d.ts +236 -0
- package/_dist/react.d.ts.map +1 -0
- package/_dist/readers.d.ts +221 -0
- package/_dist/readers.d.ts.map +1 -0
- package/_dist/reap.d.ts +21 -0
- package/_dist/reap.d.ts.map +1 -0
- package/_dist/recall.d.ts +32 -0
- package/_dist/recall.d.ts.map +1 -0
- package/_dist/routes.d.ts +15 -0
- package/_dist/routes.d.ts.map +1 -0
- package/_dist/run.d.ts +81 -0
- package/_dist/run.d.ts.map +1 -0
- package/_dist/service.d.ts +131 -0
- package/_dist/service.d.ts.map +1 -0
- package/_dist/source.d.ts +9 -0
- package/_dist/source.d.ts.map +1 -0
- package/_dist/start.d.ts +29 -0
- package/_dist/start.d.ts.map +1 -0
- package/_dist/state.d.ts +75 -0
- package/_dist/state.d.ts.map +1 -0
- package/_dist/status.d.ts +38 -0
- package/_dist/status.d.ts.map +1 -0
- package/_dist/tail.d.ts +121 -0
- package/_dist/tail.d.ts.map +1 -0
- package/_dist/timing.d.ts +9 -0
- package/_dist/timing.d.ts.map +1 -0
- package/_dist/told.d.ts +17 -0
- package/_dist/told.d.ts.map +1 -0
- package/_dist/tools.d.ts +41 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/transcript.d.ts +5 -0
- package/_dist/transcript.d.ts.map +1 -0
- package/_dist/turn.d.ts +49 -0
- package/_dist/turn.d.ts.map +1 -0
- package/_dist/unknown.d.ts +6 -0
- package/_dist/unknown.d.ts.map +1 -0
- package/_dist/views.d.ts +9 -0
- package/_dist/views.d.ts.map +1 -0
- package/_dist/vocab.d.ts +9 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/_dist/wait.d.ts +15 -0
- package/_dist/wait.d.ts.map +1 -0
- package/_dist/wake.d.ts +116 -0
- package/_dist/wake.d.ts.map +1 -0
- package/_dist/watch.d.ts +52 -0
- package/_dist/watch.d.ts.map +1 -0
- package/_dist/who.d.ts +84 -0
- package/_dist/who.d.ts.map +1 -0
- package/_dist/window.d.ts +43 -0
- package/_dist/window.d.ts.map +1 -0
- package/admission.js +137 -0
- package/admission.js.map +1 -0
- package/admission.ts +164 -0
- package/announce.js +123 -0
- package/announce.js.map +1 -0
- package/announce.ts +126 -0
- package/answer.js +372 -0
- package/answer.js.map +1 -0
- package/answer.ts +426 -0
- package/append.js +125 -0
- package/append.js.map +1 -0
- package/append.ts +205 -0
- package/archive.js +62 -0
- package/archive.js.map +1 -0
- package/archive.ts +62 -0
- package/asks.js +80 -0
- package/asks.js.map +1 -0
- package/asks.ts +106 -0
- package/attempt.js +46 -0
- package/attempt.js.map +1 -0
- package/attempt.ts +60 -0
- package/audit.js +57 -0
- package/audit.js.map +1 -0
- package/audit.ts +65 -0
- package/bounce.js +29 -0
- package/bounce.js.map +1 -0
- package/bounce.ts +34 -0
- package/bus.js +568 -0
- package/bus.js.map +1 -0
- package/bus.ts +683 -0
- package/cache.js +169 -0
- package/cache.js.map +1 -0
- package/cache.ts +230 -0
- package/child-notice.js +128 -0
- package/child-notice.js.map +1 -0
- package/child-notice.ts +136 -0
- package/children.js +937 -0
- package/children.js.map +1 -0
- package/children.ts +925 -0
- package/claude-control.js +8 -0
- package/claude-control.js.map +1 -0
- package/claude-control.ts +12 -0
- package/cli.js +23 -0
- package/cli.js.map +1 -0
- package/cli.ts +28 -0
- package/comp.js +76 -0
- package/comp.js.map +1 -0
- package/comp.ts +78 -0
- package/compact.js +157 -0
- package/compact.js.map +1 -0
- package/compact.ts +209 -0
- package/compact_ask.js +280 -0
- package/compact_ask.js.map +1 -0
- package/compact_ask.ts +199 -0
- package/continue.js +131 -0
- package/continue.js.map +1 -0
- package/continue.ts +143 -0
- package/conversation.js +337 -0
- package/conversation.js.map +1 -0
- package/conversation.ts +593 -0
- package/cost.js +111 -0
- package/cost.js.map +1 -0
- package/cost.ts +93 -0
- package/deferred.js +207 -0
- package/deferred.js.map +1 -0
- package/deferred.ts +219 -0
- package/deno.json +1 -0
- package/dependencies.js +114 -0
- package/dependencies.js.map +1 -0
- package/dependencies.ts +110 -0
- package/draining.js +20 -0
- package/draining.js.map +1 -0
- package/draining.ts +26 -0
- package/ended.js +210 -0
- package/ended.js.map +1 -0
- package/ended.ts +225 -0
- package/files.js +285 -0
- package/files.js.map +1 -0
- package/files.ts +318 -0
- package/fork.js +96 -0
- package/fork.js.map +1 -0
- package/fork.ts +124 -0
- package/graft.js +97 -0
- package/graft.js.map +1 -0
- package/graft.ts +88 -0
- package/graph.js +49 -0
- package/graph.js.map +1 -0
- package/graph.ts +61 -0
- package/handoff.js +111 -0
- package/handoff.js.map +1 -0
- package/handoff.ts +147 -0
- package/help.js +432 -0
- package/help.js.map +1 -0
- package/help.ts +431 -0
- package/hooks.js +253 -0
- package/hooks.js.map +1 -0
- package/hooks.ts +333 -0
- package/imported.js +259 -0
- package/imported.js.map +1 -0
- package/imported.ts +201 -0
- package/inspect.js +353 -0
- package/inspect.js.map +1 -0
- package/inspect.ts +378 -0
- package/intake.js +115 -0
- package/intake.js.map +1 -0
- package/intake.ts +124 -0
- package/joined.js +67 -0
- package/joined.js.map +1 -0
- package/joined.ts +69 -0
- package/lease.js +146 -0
- package/lease.js.map +1 -0
- package/lease.ts +151 -0
- package/lifecycle.js +49 -0
- package/lifecycle.js.map +1 -0
- package/lifecycle.ts +46 -0
- package/lines.js +171 -0
- package/lines.js.map +1 -0
- package/lines.ts +177 -0
- package/listen.js +604 -0
- package/listen.js.map +1 -0
- package/listen.ts +976 -0
- package/log.js +78 -0
- package/log.js.map +1 -0
- package/log.ts +88 -0
- package/mod.js +107 -0
- package/mod.js.map +1 -0
- package/mod.ts +113 -0
- package/name.js +51 -0
- package/name.js.map +1 -0
- package/name.ts +84 -0
- package/naming.js +96 -0
- package/naming.js.map +1 -0
- package/naming.ts +102 -0
- package/native.js +17 -0
- package/native.js.map +1 -0
- package/native.ts +17 -0
- package/package.json +133 -4
- package/parts.js +334 -0
- package/parts.js.map +1 -0
- package/parts.ts +383 -0
- package/plugin.js +77 -0
- package/plugin.js.map +1 -0
- package/plugin.ts +98 -0
- package/project.js +354 -0
- package/project.js.map +1 -0
- package/project.ts +407 -0
- package/prompt.js +57 -0
- package/prompt.js.map +1 -0
- package/prompt.ts +92 -0
- package/prose.js +43 -0
- package/prose.js.map +1 -0
- package/prose.ts +63 -0
- package/providers.js +279 -0
- package/providers.js.map +1 -0
- package/providers.ts +306 -0
- package/pruning.js +80 -0
- package/pruning.js.map +1 -0
- package/pruning.ts +147 -0
- package/reach.js +923 -0
- package/reach.js.map +1 -0
- package/reach.ts +1107 -0
- package/react.js +2259 -0
- package/react.js.map +1 -0
- package/react.ts +2864 -0
- package/readers.js +956 -0
- package/readers.js.map +1 -0
- package/readers.ts +1017 -0
- package/reap.js +60 -0
- package/reap.js.map +1 -0
- package/reap.ts +65 -0
- package/recall.js +67 -0
- package/recall.js.map +1 -0
- package/recall.ts +114 -0
- package/reopen.js +45 -0
- package/reopen.js.map +1 -0
- package/reopen.ts +44 -0
- package/result-entry.js +32 -0
- package/result-entry.js.map +1 -0
- package/result-entry.ts +34 -0
- package/resume.js +266 -0
- package/resume.js.map +1 -0
- package/resume.ts +283 -0
- package/routes.js +129 -0
- package/routes.js.map +1 -0
- package/routes.ts +147 -0
- package/routine.js +179 -0
- package/routine.js.map +1 -0
- package/routine.ts +191 -0
- package/run.js +932 -0
- package/run.js.map +1 -0
- package/run.ts +999 -0
- package/service.js +806 -0
- package/service.js.map +1 -0
- package/service.ts +911 -0
- package/source.js +26 -0
- package/source.js.map +1 -0
- package/source.ts +40 -0
- package/start.js +197 -0
- package/start.js.map +1 -0
- package/start.ts +214 -0
- package/state.js +247 -0
- package/state.js.map +1 -0
- package/state.ts +404 -0
- package/status.js +1222 -0
- package/status.js.map +1 -0
- package/status.ts +1555 -0
- package/status_bench.ts +50 -0
- package/statusline.js +187 -0
- package/statusline.js.map +1 -0
- package/statusline.ts +232 -0
- package/tail.js +748 -0
- package/tail.js.map +1 -0
- package/tail.ts +815 -0
- package/talk.js +58 -0
- package/talk.js.map +1 -0
- package/talk.ts +51 -0
- package/task-context.js +41 -0
- package/task-context.js.map +1 -0
- package/task-context.ts +53 -0
- package/texted.js +65 -0
- package/texted.js.map +1 -0
- package/texted.ts +65 -0
- package/timing.js +37 -0
- package/timing.js.map +1 -0
- package/timing.ts +52 -0
- package/told.js +56 -0
- package/told.js.map +1 -0
- package/told.ts +54 -0
- package/tools.js +595 -0
- package/tools.js.map +1 -0
- package/tools.ts +633 -0
- package/transcript.js +34 -0
- package/transcript.js.map +1 -0
- package/transcript.ts +41 -0
- package/turn.js +47 -0
- package/turn.js.map +1 -0
- package/turn.ts +68 -0
- package/unknown.js +8 -0
- package/unknown.js.map +1 -0
- package/unknown.ts +9 -0
- package/views.js +15 -0
- package/views.js.map +1 -0
- package/views.ts +21 -0
- package/vocab.js +24 -0
- package/vocab.js.map +1 -0
- package/vocab.json +1573 -0
- package/vocab.ts +30 -0
- package/wait.js +30 -0
- package/wait.js.map +1 -0
- package/wait.ts +45 -0
- package/wake.js +552 -0
- package/wake.js.map +1 -0
- package/wake.ts +597 -0
- package/watch.js +520 -0
- package/watch.js.map +1 -0
- package/watch.ts +596 -0
- package/who.js +309 -0
- package/who.js.map +1 -0
- package/who.ts +347 -0
- package/window.js +179 -0
- package/window.js.map +1 -0
- package/window.ts +268 -0
- package/woken.js +129 -0
- package/woken.js.map +1 -0
- package/woken.ts +127 -0
package/conversation.ts
ADDED
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How a conversation draws its lines, wherever one is shown: a person's words
|
|
3
|
+
* as a bubble on their side, a reply as formatted text, each tool call as one
|
|
4
|
+
* short line in plain words that opens on what it asked for and what came
|
|
5
|
+
* back, a failure said in words with what went wrong inside a closed
|
|
6
|
+
* "Details", a stop, and the files that came with a line ({@link files}).
|
|
7
|
+
* Drawn with @yaks/ui's `Transcript`, `More`, `Pairs`, `Notice` and
|
|
8
|
+
* `Attachments`, so it bundles for a server, a browser and the iOS shell
|
|
9
|
+
* alike.
|
|
10
|
+
*
|
|
11
|
+
* Each kind of line is a {@link Kind}: a `Conversation.Line` registration
|
|
12
|
+
* (@yaks/render) that matches on the components an entry wears, and draws it.
|
|
13
|
+
* A host with words of its own (a channel's mark, a helper) puts its kinds
|
|
14
|
+
* ahead of {@link kinds} in its own registry; the most specific match draws
|
|
15
|
+
* the entry, and {@link draw} gives it to the next best when a kind passes.
|
|
16
|
+
* What a view draws beside a line, outside its reading column, is a kind
|
|
17
|
+
* registered under `Conversation.Margin` ({@link margin}): a view that loads
|
|
18
|
+
* none draws its lines without one. Which renderers a view loads is the list
|
|
19
|
+
* its registry is defined from. Which entries make a line is `joined`
|
|
20
|
+
* (./lines.ts), which reads no kit.
|
|
21
|
+
*
|
|
22
|
+
* @module
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/* @ts-types="npm:@types/react@19.3.0" */ import {
|
|
26
|
+
createElement as h,
|
|
27
|
+
type ReactElement,
|
|
28
|
+
type ReactNode as Node,
|
|
29
|
+
useContext,
|
|
30
|
+
} from 'react'
|
|
31
|
+
import { Host, type Options } from '@yaks/markdown'
|
|
32
|
+
import {
|
|
33
|
+
Attachments,
|
|
34
|
+
Body,
|
|
35
|
+
Logo,
|
|
36
|
+
More,
|
|
37
|
+
Notice,
|
|
38
|
+
Pairs,
|
|
39
|
+
StepLabel,
|
|
40
|
+
Transcript,
|
|
41
|
+
} from '@yaks/ui/parts'
|
|
42
|
+
import { Fold } from '@yaks/ux/conversation'
|
|
43
|
+
import {
|
|
44
|
+
type Bundle,
|
|
45
|
+
define,
|
|
46
|
+
type Registration,
|
|
47
|
+
type Registry,
|
|
48
|
+
resolve,
|
|
49
|
+
} from '@yaks/render'
|
|
50
|
+
import { parse as query } from '@yaks/query/core'
|
|
51
|
+
import { loadVocab } from '@yaks/vocab/runtime'
|
|
52
|
+
import { type Step, stepOf, type Steps } from '@yaks/tools/title'
|
|
53
|
+
import { markdown } from './prose.js'
|
|
54
|
+
|
|
55
|
+
export { type Joined, joined } from './joined.js'
|
|
56
|
+
export { markdown } from './prose.js'
|
|
57
|
+
|
|
58
|
+
type Comp = Record<string, unknown>
|
|
59
|
+
let comp = (b: Bundle | undefined, name: string) =>
|
|
60
|
+
b?.[name] as Comp | undefined
|
|
61
|
+
let body = (b?: Bundle) => String(comp(b, 'content')?.body ?? '')
|
|
62
|
+
|
|
63
|
+
let cut = (text: string, most: number) =>
|
|
64
|
+
text.length > most ? text.slice(0, most) + '…' : text
|
|
65
|
+
|
|
66
|
+
/** A line as it is drawn: its parts, the words it says when copied where they
|
|
67
|
+
* are not its entries' own (`text`), the kind of row and a step's tone. */
|
|
68
|
+
export type Said = {
|
|
69
|
+
node: Node
|
|
70
|
+
text?: string
|
|
71
|
+
is?: 'step' | 'notice' | 'theirs'
|
|
72
|
+
tone?: Tone
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** A step's tone: under way (`on`) or failed (`no`). */
|
|
76
|
+
export type Tone = 'on' | 'no'
|
|
77
|
+
|
|
78
|
+
/** How a call reads to the person, by its tool's eid (`call.to`) and its
|
|
79
|
+
* arguments: what it is doing while it runs and what it did once it answered
|
|
80
|
+
* (@yaks/tools `stepOf`). */
|
|
81
|
+
export type Names = (tool: string, args?: Record<string, unknown>) => Called
|
|
82
|
+
|
|
83
|
+
/** How a call reads, and where the logo of the integration it names is drawn
|
|
84
|
+
* from (@yaks/ui `Logo`): `null` for one with none, which wears the plug, and
|
|
85
|
+
* absent where it names none. */
|
|
86
|
+
export type Called = Step & { logo?: string | null }
|
|
87
|
+
|
|
88
|
+
/** What a call's arguments name an integration by: its `integration`, or the
|
|
89
|
+
* `connection` it attaches. */
|
|
90
|
+
export let integrationNamed = (
|
|
91
|
+
args?: Record<string, unknown> | null,
|
|
92
|
+
): string | undefined =>
|
|
93
|
+
[args?.integration, args?.connection].find((v): v is string =>
|
|
94
|
+
typeof v == 'string' && !!v
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
/** What a tool is called, of its `tool` component: its name, title and
|
|
98
|
+
* steps (@yaks/tools `stepOf`). */
|
|
99
|
+
export type Named = { name?: string; title?: string; step?: Steps }
|
|
100
|
+
|
|
101
|
+
/** What the calls' steps are said from: each tool a call points at
|
|
102
|
+
* (`call.to`) by its eid, as its `tool` component names it, and whom the
|
|
103
|
+
* calls work for; and what the entities the lines' prose names are called,
|
|
104
|
+
* by eid (`ids`, an entity's id such as `T-2`), drawn by {@link called}. */
|
|
105
|
+
export type Naming = {
|
|
106
|
+
tools?: Record<string, Named>
|
|
107
|
+
person?: string | null
|
|
108
|
+
ids?: Record<string, string>
|
|
109
|
+
/** where the logo of each integration a call names is drawn from, by what
|
|
110
|
+
* it names it by ({@link integrationNamed}); `null` for one with none */
|
|
111
|
+
logos?: Record<string, string | null>
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** How the calls read, from what names them. */
|
|
115
|
+
export let namer =
|
|
116
|
+
({ tools = {}, person, logos = {} }: Naming): Names => (tool, args) => {
|
|
117
|
+
let step = stepOf(
|
|
118
|
+
tools[tool] ?? { title: 'Use a tool' },
|
|
119
|
+
args,
|
|
120
|
+
person ? { person } : undefined,
|
|
121
|
+
)
|
|
122
|
+
let named = integrationNamed(args)
|
|
123
|
+
return named != null && named in logos
|
|
124
|
+
? { ...step, logo: logos[named] }
|
|
125
|
+
: step
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** What a line is drawn with beside its entry: the result that answered it
|
|
129
|
+
* when it is a call (`joined`), how calls read, what a call's step holds
|
|
130
|
+
* after what it asked for (`under`), what came back of it drawn in place of
|
|
131
|
+
* its result's own words (`came`: the helper a call started, not its id),
|
|
132
|
+
* who works the conversation, as a sentence names them partway through
|
|
133
|
+
* (`who`, "the Operator"), and whether the conversation is `stalled`: a
|
|
134
|
+
* failure the host files to fix and then takes it up again on its own. */
|
|
135
|
+
export type Beside = {
|
|
136
|
+
result?: Bundle
|
|
137
|
+
names: Names
|
|
138
|
+
under?: Node[]
|
|
139
|
+
came?: Node[]
|
|
140
|
+
who?: string
|
|
141
|
+
stalled?: boolean
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** A kind of line: a `Conversation.Line` registration and what it draws of an
|
|
145
|
+
* entry; `null` where the entry draws nothing, and `undefined` where this kind
|
|
146
|
+
* passes it to the next best match. */
|
|
147
|
+
export type Kind<B extends Beside = Beside> = Registration & {
|
|
148
|
+
draw: (b: Bundle, beside: B) => Said | null | undefined
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** The view every kind of line is registered under. */
|
|
152
|
+
export let LINE = 'Conversation.Line'
|
|
153
|
+
|
|
154
|
+
/** The view what a line has beside it is registered under: drawn outside
|
|
155
|
+
* its reading column, of the entry the line is put by (an admin's sequence
|
|
156
|
+
* and cost). */
|
|
157
|
+
export let MARGIN = 'Conversation.Margin'
|
|
158
|
+
|
|
159
|
+
// Every kind matches on which components an entry wears and on nothing they
|
|
160
|
+
// hold, and a vocabulary reads an undeclared name in a presence test as a
|
|
161
|
+
// component: kinds select the same under any vocabulary, so they need none.
|
|
162
|
+
let NONE = loadVocab([])
|
|
163
|
+
|
|
164
|
+
// An entry drawn in a view by the most specific kind registered under it
|
|
165
|
+
// that draws it.
|
|
166
|
+
let drawn = <B extends Beside>(
|
|
167
|
+
view: string,
|
|
168
|
+
registry: Registry<Kind<B>>,
|
|
169
|
+
b: Bundle,
|
|
170
|
+
beside: B,
|
|
171
|
+
): Said | null => {
|
|
172
|
+
let pool = registry
|
|
173
|
+
for (;;) {
|
|
174
|
+
let kind = resolve(pool, b, view, NONE)
|
|
175
|
+
if (!kind) return null
|
|
176
|
+
let said = kind.draw(b, beside)
|
|
177
|
+
if (said !== undefined) return said
|
|
178
|
+
pool = { ...pool, renderers: pool.renderers.filter((k) => k != kind) }
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** An entry drawn as its line, by the most specific kind that draws it. */
|
|
183
|
+
export let draw = <B extends Beside>(
|
|
184
|
+
registry: Registry<Kind<B>>,
|
|
185
|
+
b: Bundle,
|
|
186
|
+
beside: B,
|
|
187
|
+
): Said | null => drawn(LINE, registry, b, beside)
|
|
188
|
+
|
|
189
|
+
/** What the line an entry is put by has beside it, by the most specific
|
|
190
|
+
* margin kind that draws it; nothing where the registry loads none. */
|
|
191
|
+
export let margin = <B extends Beside>(
|
|
192
|
+
registry: Registry<Kind<B>>,
|
|
193
|
+
b: Bundle,
|
|
194
|
+
beside: B,
|
|
195
|
+
): Node => drawn(MARGIN, registry, b, beside)?.node ?? null
|
|
196
|
+
|
|
197
|
+
/** Where the ids in a reader's Markdown open (@yaks/markdown `Host`): `url`,
|
|
198
|
+
* with `{id}` where the id goes, as the reader's own server says; none, and
|
|
199
|
+
* ids stay words. */
|
|
200
|
+
export let opening = (url?: string | null): Options =>
|
|
201
|
+
url ? { refs: (id) => url.replaceAll('{id}', encodeURIComponent(id)) } : {}
|
|
202
|
+
|
|
203
|
+
/** Lines drawn with each eid their prose holds called as `ids` calls it,
|
|
204
|
+
* else as the page's own Host does, else as its short handle; where the page
|
|
205
|
+
* opens an id is the page's. */
|
|
206
|
+
export let called = (
|
|
207
|
+
ids: Record<string, string> | undefined,
|
|
208
|
+
node: Node,
|
|
209
|
+
): Node => ids && Object.keys(ids).length ? h(Called, { ids }, node) : node
|
|
210
|
+
|
|
211
|
+
let Called = (
|
|
212
|
+
{ ids, children }: { ids: Record<string, string>; children?: Node },
|
|
213
|
+
) => {
|
|
214
|
+
let host = useContext(Host)
|
|
215
|
+
let value = { ...host, ids: (eid: string) => ids[eid] ?? host.ids?.(eid) }
|
|
216
|
+
return h(Host.Provider, { value }, children)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** A reply as it was written, in Markdown. */
|
|
220
|
+
export let reply = (text: string): ReactElement =>
|
|
221
|
+
h(Transcript.Reply, {}, markdown(text))
|
|
222
|
+
|
|
223
|
+
/** A person's words, on their side, in Markdown as a reply is. `mark` sits
|
|
224
|
+
* before them: how they came. */
|
|
225
|
+
export let yours = (text: string, mark?: Node): ReactElement =>
|
|
226
|
+
h(Transcript.Bubble, {}, mark ?? null, markdown(text))
|
|
227
|
+
|
|
228
|
+
/** A file as a line names it: its name, and its media type where known. */
|
|
229
|
+
export type Sent = { name: string; mime?: string }
|
|
230
|
+
|
|
231
|
+
/** What a person said who sent files and no words. */
|
|
232
|
+
export let unsaid = (files: Sent[]): string =>
|
|
233
|
+
'Attached: ' + files.map((f) => f.name).join(', ')
|
|
234
|
+
|
|
235
|
+
/** The kinds a browser draws as a picture, never a drawing that runs (SVG). */
|
|
236
|
+
export let PICTURES = ['image/png', 'image/jpeg', 'image/gif', 'image/webp']
|
|
237
|
+
|
|
238
|
+
/** Whether a file is words a browser shows as they are: text, and JSON,
|
|
239
|
+
* XML and YAML. */
|
|
240
|
+
export let textual = (mime = ''): boolean =>
|
|
241
|
+
/^text\/|^application\/(json|xml|yaml|x-yaml)$|\+(json|xml)$/.test(mime)
|
|
242
|
+
|
|
243
|
+
/** A file's kind as a person names it: its extension, else "File". */
|
|
244
|
+
export let fileKind = (name: string): string =>
|
|
245
|
+
name.match(/\.([a-z0-9]{1,5})$/i)?.[1].toUpperCase() ?? 'File'
|
|
246
|
+
|
|
247
|
+
/** A size in bytes as a person reads it. */
|
|
248
|
+
export let fileSize = (n: number): string =>
|
|
249
|
+
n < 1000
|
|
250
|
+
? `${n} B`
|
|
251
|
+
: n < 1e6
|
|
252
|
+
? `${Math.round(n / 1e3)} KB`
|
|
253
|
+
: `${(n / 1e6).toFixed(n < 1e7 ? 1 : 0)} MB`
|
|
254
|
+
|
|
255
|
+
/** A file as a conversation draws it, sent or held in a box to send: a
|
|
256
|
+
* picture as itself, any other file as its kind and name. What differs is
|
|
257
|
+
* given: where its original opens (`href`), a picture's source where that is
|
|
258
|
+
* not it (`src`), and for one held, its `size`, a `note` and how to take it
|
|
259
|
+
* back (`remove`, what its button carries). */
|
|
260
|
+
export type Filed = Sent & {
|
|
261
|
+
href?: string
|
|
262
|
+
src?: string
|
|
263
|
+
/** a picture's size, which its place keeps before it loads */
|
|
264
|
+
width?: number
|
|
265
|
+
height?: number
|
|
266
|
+
size?: number
|
|
267
|
+
note?: string
|
|
268
|
+
remove?: Record<string, unknown>
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** One file, as `Filed` says. */
|
|
272
|
+
export let Filed = (
|
|
273
|
+
{ name, mime, href, src, width, height, size, note, remove }: Filed,
|
|
274
|
+
): ReactElement => {
|
|
275
|
+
let away = remove
|
|
276
|
+
? h(Attachments.Remove, { label: `Remove ${name}`, ...remove })
|
|
277
|
+
: null
|
|
278
|
+
return PICTURES.includes(mime ?? '')
|
|
279
|
+
? h(
|
|
280
|
+
Attachments.Picture,
|
|
281
|
+
{ href, target: href ? '_blank' : undefined },
|
|
282
|
+
h(Attachments.Image, {
|
|
283
|
+
src: src ?? href,
|
|
284
|
+
alt: name,
|
|
285
|
+
loading: 'lazy',
|
|
286
|
+
width,
|
|
287
|
+
height,
|
|
288
|
+
}),
|
|
289
|
+
away,
|
|
290
|
+
)
|
|
291
|
+
: h(
|
|
292
|
+
Attachments.File,
|
|
293
|
+
{ href },
|
|
294
|
+
h(Attachments.Kind, null, fileKind(name)),
|
|
295
|
+
h(Attachments.Name, null, name),
|
|
296
|
+
size == null ? null : h(Attachments.Size, null, fileSize(size)),
|
|
297
|
+
note ? h(Attachments.Note, null, note) : null,
|
|
298
|
+
away,
|
|
299
|
+
)
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** A file opened where it is named, showing what it holds: a picture as
|
|
303
|
+
* itself, words in a frame of their own that loads once it is seen (from its
|
|
304
|
+
* `src`, a page of them, else its `href`), and any
|
|
305
|
+
* other file as itself, which opens it. */
|
|
306
|
+
export let Shown = (file: Filed): ReactElement =>
|
|
307
|
+
textual(file.mime) && file.href
|
|
308
|
+
? h(Attachments.Frame, {
|
|
309
|
+
src: file.src ?? file.href,
|
|
310
|
+
title: file.name,
|
|
311
|
+
loading: 'lazy',
|
|
312
|
+
sandbox: '',
|
|
313
|
+
})
|
|
314
|
+
: h(Attachments, null, h(Filed, file))
|
|
315
|
+
|
|
316
|
+
/** A file a step read or made, named in a quiet line that opens to what it
|
|
317
|
+
* holds (`Shown`): "Read notes.txt". */
|
|
318
|
+
export let opened = (doing: string, file: Filed): ReactElement =>
|
|
319
|
+
step(`${doing} ${file.name}`, '', h(Shown, file))
|
|
320
|
+
|
|
321
|
+
/** The files that came with a line, under it as a chat app shows them: a
|
|
322
|
+
* person's sit at the `end`, on their side. None draws nothing. */
|
|
323
|
+
export let files = (sent: Filed[], mod?: 'end'): ReactElement | null =>
|
|
324
|
+
sent.length ? h(Attachments, { mod }, sent.map((f) => h(Filed, f))) : null
|
|
325
|
+
|
|
326
|
+
/** The one way a conversation draws anything that opens: a step, by what it
|
|
327
|
+
* did, opening on what it holds; `on` while it runs, `no` once it did not
|
|
328
|
+
* work. A fold in a fold is the same fold. Whether it is open is its line's
|
|
329
|
+
* fold state (@yaks/ux `Fold`), which a redrawn line keeps. */
|
|
330
|
+
export let step = (
|
|
331
|
+
name: Node,
|
|
332
|
+
mod: '' | Tone,
|
|
333
|
+
...inside: Node[]
|
|
334
|
+
): ReactElement =>
|
|
335
|
+
h(
|
|
336
|
+
Fold,
|
|
337
|
+
{ mod: ['quiet', 'step', mod], summary: h(StepLabel, {}, name) },
|
|
338
|
+
...inside,
|
|
339
|
+
) as ReactElement
|
|
340
|
+
|
|
341
|
+
/** A {@link step} whose inside is drawn only once it is opened (@yaks/ux
|
|
342
|
+
* `Disclosure` `lazy`): what a model was told, say, which runs to tens of
|
|
343
|
+
* kilobytes a step and is rarely opened. */
|
|
344
|
+
export let lazyStep = (
|
|
345
|
+
name: Node,
|
|
346
|
+
mod: '' | Tone,
|
|
347
|
+
...inside: Node[]
|
|
348
|
+
): ReactElement =>
|
|
349
|
+
h(
|
|
350
|
+
Fold,
|
|
351
|
+
{
|
|
352
|
+
mod: ['quiet', 'step', mod],
|
|
353
|
+
summary: h(StepLabel, {}, name),
|
|
354
|
+
lazy: true,
|
|
355
|
+
},
|
|
356
|
+
...inside,
|
|
357
|
+
) as ReactElement
|
|
358
|
+
|
|
359
|
+
/** A value a tool was given or gave back, the way a person reads it: a list
|
|
360
|
+
* of what it holds under names in words, never its JSON. Bounded in depth,
|
|
361
|
+
* length and count, since a tool can answer with a whole file. */
|
|
362
|
+
export let readable = (value: unknown, depth = 0): Node => {
|
|
363
|
+
if (typeof value == 'string' && /^\s*[[{]/.test(value)) {
|
|
364
|
+
try {
|
|
365
|
+
return readable(JSON.parse(value), depth)
|
|
366
|
+
} catch {
|
|
367
|
+
// Words that happen to start with a bracket.
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
if (value == null || value === '') {
|
|
371
|
+
return h(More.Value, {}, 'Nothing')
|
|
372
|
+
}
|
|
373
|
+
if (typeof value != 'object') {
|
|
374
|
+
return h(More.Value, {}, cut(String(value), 600))
|
|
375
|
+
}
|
|
376
|
+
if (depth > 2) return h(More.Value, {}, '…')
|
|
377
|
+
let list = Array.isArray(value)
|
|
378
|
+
? value.map((v, i) => [String(i + 1), v] as const)
|
|
379
|
+
: Object.entries(value)
|
|
380
|
+
if (!list.length) return h(More.Value, {}, 'Nothing')
|
|
381
|
+
let more = list.length - 12
|
|
382
|
+
return h(
|
|
383
|
+
Pairs,
|
|
384
|
+
{ mod: 'nested' },
|
|
385
|
+
list.slice(0, 12).map(([k, v]) =>
|
|
386
|
+
h(
|
|
387
|
+
Pairs.Row,
|
|
388
|
+
{},
|
|
389
|
+
h(
|
|
390
|
+
Pairs.Key,
|
|
391
|
+
{},
|
|
392
|
+
Array.isArray(value)
|
|
393
|
+
? k
|
|
394
|
+
: k.replace(/[_-]+/g, ' ').replace(/^./, (c) => c.toUpperCase()),
|
|
395
|
+
),
|
|
396
|
+
h(Pairs.Value, {}, readable(v, depth + 1)),
|
|
397
|
+
)
|
|
398
|
+
),
|
|
399
|
+
more > 0 ? h(Pairs.Row, {}, h(Pairs.Value, {}, `and ${more} more`)) : null,
|
|
400
|
+
)
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// An error's own words, laid out to read: JSON indented, after the status a
|
|
404
|
+
// provider puts before it ("400 {…}").
|
|
405
|
+
let laid = (detail: unknown): string => {
|
|
406
|
+
if (typeof detail != 'string') return JSON.stringify(detail, null, 2)
|
|
407
|
+
let [, head, rest] = detail.match(/^(\d{3} )?\s*([[{][\s\S]*)$/) ?? []
|
|
408
|
+
if (rest == null) return detail
|
|
409
|
+
try {
|
|
410
|
+
return (head ?? '') + JSON.stringify(JSON.parse(rest), null, 2)
|
|
411
|
+
} catch {
|
|
412
|
+
return detail
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/** The one way a conversation draws an error: what happened and what to do,
|
|
417
|
+
* in words, with what the error said only inside a closed "Details". */
|
|
418
|
+
export let trouble = (said: string, detail?: unknown): ReactElement =>
|
|
419
|
+
h(
|
|
420
|
+
Transcript.Error,
|
|
421
|
+
{},
|
|
422
|
+
h(Notice, { mod: 'negative' }, said),
|
|
423
|
+
detail == null || detail === '' ||
|
|
424
|
+
(typeof detail == 'object' && !Object.keys(detail).length)
|
|
425
|
+
? null
|
|
426
|
+
: step('Details', '', h(Body.Source, { mod: 'detail' }, laid(detail))),
|
|
427
|
+
)
|
|
428
|
+
|
|
429
|
+
// Who works the conversation, partway through a sentence and at its start.
|
|
430
|
+
let who = (beside: Beside) => beside.who ?? 'the agent'
|
|
431
|
+
let Who = (beside: Beside) => who(beside).replace(/^./, (c) => c.toUpperCase())
|
|
432
|
+
|
|
433
|
+
// A step that did not work: "Tried texting Jeff".
|
|
434
|
+
let tried = (doing: string) =>
|
|
435
|
+
'Tried ' + doing.replace(/…$/, '').replace(/^./, (c) => c.toLowerCase())
|
|
436
|
+
|
|
437
|
+
// A tool call and, once it came back, its answer, which takes the call's
|
|
438
|
+
// place: what it asked for, what came back, and what `under` adds between.
|
|
439
|
+
let tool = (call: Bundle, beside: Beside): Said => {
|
|
440
|
+
let { result, names, under = [], came } = beside
|
|
441
|
+
let c = comp(call, 'call')!
|
|
442
|
+
let said = names(String(c.to), c.args as Record<string, unknown>)
|
|
443
|
+
let ok = result ? !comp(result, 'refusal') && !result.exception : null
|
|
444
|
+
let words = ok == null ? said.doing : ok ? said.done : tried(said.doing)
|
|
445
|
+
let name = said.logo === undefined
|
|
446
|
+
? words
|
|
447
|
+
: h(Logo, { src: said.logo ?? undefined }, words)
|
|
448
|
+
let problem = `This step did not work, so ${who(beside)} went on without it.`
|
|
449
|
+
return {
|
|
450
|
+
node: step(
|
|
451
|
+
name,
|
|
452
|
+
ok == null ? 'on' : ok ? '' : 'no',
|
|
453
|
+
h(More.Label, {}, 'What it asked for'),
|
|
454
|
+
readable(c.args ?? {}),
|
|
455
|
+
...under,
|
|
456
|
+
ok == null ? came ?? null : ok
|
|
457
|
+
? came ?? [
|
|
458
|
+
h(More.Label, {}, 'What came back'),
|
|
459
|
+
readable(body(result)),
|
|
460
|
+
]
|
|
461
|
+
: trouble(
|
|
462
|
+
problem,
|
|
463
|
+
comp(result, 'content')?.body ?? result!.exception ??
|
|
464
|
+
comp(result, 'refusal'),
|
|
465
|
+
),
|
|
466
|
+
),
|
|
467
|
+
text: ok === false ? `${words}\n\n${problem}` : undefined,
|
|
468
|
+
is: 'step',
|
|
469
|
+
...ok ? {} : { tone: ok == null ? 'on' : 'no' },
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// Words said in plain words. What a provider or the host said of a failure
|
|
474
|
+
// is never a person's to read: it is the host's tracker's (./answer.ts
|
|
475
|
+
// `replyOf` says it as `fault`), and the transcript keeps it for agents.
|
|
476
|
+
let failure = (said: string): Said => ({ node: trouble(said), text: said })
|
|
477
|
+
|
|
478
|
+
// A ceiling met (./state.ts `LIMIT`): said in the host's own sentence, which
|
|
479
|
+
// is the line's words.
|
|
480
|
+
let LIMIT = 'limit'
|
|
481
|
+
|
|
482
|
+
// What comes of a stop: the conversation is taken up again once its fault
|
|
483
|
+
// is fixed where it stalled, else the person sends again.
|
|
484
|
+
let after = (beside: Beside) =>
|
|
485
|
+
beside.stalled
|
|
486
|
+
? 'It will be taken up again once the problem is fixed.'
|
|
487
|
+
: 'Send your message again to try once more.'
|
|
488
|
+
|
|
489
|
+
// A provider's refusal, or the host's own ceiling in its words.
|
|
490
|
+
let refused = (b: Bundle, beside: Beside) =>
|
|
491
|
+
comp(b, 'refusal')?.code == LIMIT && body(b)
|
|
492
|
+
? { node: reply(body(b)) }
|
|
493
|
+
: failure(
|
|
494
|
+
`${
|
|
495
|
+
Who(beside)
|
|
496
|
+
} could not get an answer from its model, so it stopped before replying. ${
|
|
497
|
+
after(beside)
|
|
498
|
+
}`,
|
|
499
|
+
)
|
|
500
|
+
|
|
501
|
+
let failed = (beside: Beside) =>
|
|
502
|
+
failure(
|
|
503
|
+
`Something went wrong, and ${who(beside)} stopped before it finished. ${
|
|
504
|
+
after(beside)
|
|
505
|
+
}`,
|
|
506
|
+
)
|
|
507
|
+
|
|
508
|
+
// A model's answer cut off on the way (the provider dropped it, or no longer
|
|
509
|
+
// held what it continued from): the turn goes on, or ends on its own line.
|
|
510
|
+
let interrupted = (beside: Beside): Said => ({
|
|
511
|
+
node: h(Notice, {}, `${Who(beside)}'s answer was interrupted.`),
|
|
512
|
+
})
|
|
513
|
+
|
|
514
|
+
let stopped = (): Said => ({ node: h(Notice, {}, 'Stopped.') })
|
|
515
|
+
|
|
516
|
+
/** What a model said of its reasoning: a quiet step among its tool calls,
|
|
517
|
+
* opening on its words, so an answer never appears from nowhere. */
|
|
518
|
+
export let thought = (b: Bundle): Said | undefined =>
|
|
519
|
+
body(b).trim()
|
|
520
|
+
? { node: step('Thought it through', '', markdown(body(b))), is: 'step' }
|
|
521
|
+
: undefined
|
|
522
|
+
|
|
523
|
+
/** What a transcript's kinds are that are none of the person's words. */
|
|
524
|
+
export let NOT_INPUT = [
|
|
525
|
+
'operator_stopped',
|
|
526
|
+
'output',
|
|
527
|
+
'result',
|
|
528
|
+
'refusal',
|
|
529
|
+
'exception',
|
|
530
|
+
'notice',
|
|
531
|
+
'task_context',
|
|
532
|
+
'failed',
|
|
533
|
+
'stop',
|
|
534
|
+
'interrupted',
|
|
535
|
+
'call',
|
|
536
|
+
'reasoning',
|
|
537
|
+
'ask',
|
|
538
|
+
'prompt',
|
|
539
|
+
'tool_progress',
|
|
540
|
+
]
|
|
541
|
+
|
|
542
|
+
/** A person's own words: an entry with words that is none of the other
|
|
543
|
+
* kinds. */
|
|
544
|
+
export let theirs = (b: Bundle): boolean =>
|
|
545
|
+
!!comp(b, 'content')?.body && !NOT_INPUT.some((c) => b[c])
|
|
546
|
+
|
|
547
|
+
/** What a person wrote. A task's first input is the task itself as JSON (its
|
|
548
|
+
* row, and on older sessions its comments): its body is what they wrote. */
|
|
549
|
+
export let words = (said: string): string => {
|
|
550
|
+
if (said.startsWith('{')) {
|
|
551
|
+
try {
|
|
552
|
+
let task = JSON.parse(said).task
|
|
553
|
+
let doc = task?.doc ?? task
|
|
554
|
+
if (typeof doc?.body == 'string') return doc.body
|
|
555
|
+
} catch {
|
|
556
|
+
// Not the task: words that happen to start with a brace.
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
return said
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/** A kind drawn by `draw`, matching `match` (a query of which components
|
|
563
|
+
* an entry wears), or any entry where it is empty: a kind of line, or under
|
|
564
|
+
* `view` another place a line is drawn ({@link MARGIN}). */
|
|
565
|
+
export let kind = <B extends Beside = Beside>(
|
|
566
|
+
match: string,
|
|
567
|
+
draw: Kind<B>['draw'],
|
|
568
|
+
view = LINE,
|
|
569
|
+
): Kind<B> => ({ view, match: match ? query(match) : true, draw })
|
|
570
|
+
|
|
571
|
+
/** The kinds of line any transcript has: a tool call, a thought, a refusal,
|
|
572
|
+
* a reply, a failure, a stop, and a person's words, which is any entry no
|
|
573
|
+
* other kind draws. A model call cut off on the way (`interrupted`) draws no
|
|
574
|
+
* line of its own: the turn goes on from it, or a line after it says why it
|
|
575
|
+
* ended. */
|
|
576
|
+
export let kinds: Registry<Kind> = define<Kind>([
|
|
577
|
+
kind('.call', tool),
|
|
578
|
+
kind('.reasoning', thought),
|
|
579
|
+
kind('.refusal&!result', refused),
|
|
580
|
+
kind('.output&.notice', (_, beside) => interrupted(beside)),
|
|
581
|
+
kind('.output&.exception', (_, beside) => failed(beside)),
|
|
582
|
+
// Words that are only blank space (a model's break between its tool calls)
|
|
583
|
+
// draw nothing: no empty line with a menu between real ones.
|
|
584
|
+
kind('.output', (b) => body(b).trim() ? { node: reply(body(b)) } : undefined),
|
|
585
|
+
kind('.failed', (_, beside) => failed(beside)),
|
|
586
|
+
kind('.exception', (_, beside) => failed(beside)),
|
|
587
|
+
kind('.stop', stopped),
|
|
588
|
+
kind('.cancel', stopped),
|
|
589
|
+
kind(
|
|
590
|
+
'',
|
|
591
|
+
(b) => theirs(b) ? { node: yours(words(body(b))), is: 'theirs' } : null,
|
|
592
|
+
),
|
|
593
|
+
])
|
package/cost.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// What a transcript cost, in dollars. Each request's `cost` (@yaks/model) is
|
|
2
|
+
// stored on its entry, beside its `usage`: the dollars the provider reported
|
|
3
|
+
// wherever it reports them (OpenRouter's usage, Claude Code's closing line),
|
|
4
|
+
// and otherwise the usage weighed at the `price` on the row of the model that
|
|
5
|
+
// answered it, in the transaction that writes the usage: the price that
|
|
6
|
+
// transaction gives it, as a model's first request does, or else the one it
|
|
7
|
+
// has (@yaks/model `requesting`). A model with no price
|
|
8
|
+
// leaves its requests unweighed, since absent beats zero.
|
|
9
|
+
//
|
|
10
|
+
// A session's `cost` is the sum of its entries' and is never stored: a second
|
|
11
|
+
// copy would drift from the entries it sums, and a transcript whose entries
|
|
12
|
+
// record no cost has none, not zero.
|
|
13
|
+
import { after } from "@yaks/fp";
|
|
14
|
+
import { COST, PRICE, USAGE, weigh } from "@yaks/model/vocab";
|
|
15
|
+
import { col, eq, fn, join, select, sub, table } from "@yaks/sql/core";
|
|
16
|
+
import { ASK, USING } from "./native.js";
|
|
17
|
+
// The models whose price weighs an entry, first that has one: the model that
|
|
18
|
+
// answered (`ask.served`), then the one it asked (`ask.to`) or its `using`
|
|
19
|
+
// names.
|
|
20
|
+
let served = (b)=>{
|
|
21
|
+
let ask = b?.[ASK];
|
|
22
|
+
let to = ask?.to ?? b?.[USING]?.model;
|
|
23
|
+
return [
|
|
24
|
+
ask?.served,
|
|
25
|
+
to
|
|
26
|
+
].filter((m)=>m != null).map(String);
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* The precondition that weighs a request its provider named no price for: a
|
|
30
|
+
* bundle carrying `usage` and no `cost` gains `cost{dollars, reported: false}`
|
|
31
|
+
* when the model that answered it has a `price`, the batch's or else the
|
|
32
|
+
* stored one. The model and the counts are the bundle's over the entry as
|
|
33
|
+
* stored, so a patch that only adds the usage is weighed whole; a cost the
|
|
34
|
+
* entry already records stands.
|
|
35
|
+
*/ export let weighing = (bundles, tx)=>{
|
|
36
|
+
let owed = bundles.filter((b)=>b[USAGE] && !(COST in b));
|
|
37
|
+
if (!owed.length) return bundles;
|
|
38
|
+
let eids = owed.map((b)=>b.entity.eid);
|
|
39
|
+
return after(tx.get(eids, [
|
|
40
|
+
ASK,
|
|
41
|
+
USING,
|
|
42
|
+
USAGE,
|
|
43
|
+
COST
|
|
44
|
+
]), (held)=>{
|
|
45
|
+
let stored = new Map(held.map((b)=>[
|
|
46
|
+
b.entity.eid,
|
|
47
|
+
b
|
|
48
|
+
]));
|
|
49
|
+
let was = (b)=>stored.get(b.entity.eid);
|
|
50
|
+
let fresh = owed.filter((b)=>!was(b)?.[COST]);
|
|
51
|
+
let model = (b)=>{
|
|
52
|
+
let w = was(b);
|
|
53
|
+
let over = (c)=>({
|
|
54
|
+
...w?.[c],
|
|
55
|
+
...b[c]
|
|
56
|
+
});
|
|
57
|
+
return served({
|
|
58
|
+
...b,
|
|
59
|
+
[ASK]: over(ASK),
|
|
60
|
+
[USING]: over(USING)
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
let models = [
|
|
64
|
+
...new Set(fresh.flatMap(model))
|
|
65
|
+
];
|
|
66
|
+
return after(models.length ? tx.get(models, [
|
|
67
|
+
PRICE
|
|
68
|
+
]) : [], (rows)=>{
|
|
69
|
+
let price = new Map([
|
|
70
|
+
...rows,
|
|
71
|
+
...bundles
|
|
72
|
+
].filter((r)=>r[PRICE]).map((r)=>[
|
|
73
|
+
r.entity.eid,
|
|
74
|
+
r[PRICE]
|
|
75
|
+
]));
|
|
76
|
+
return bundles.map((b)=>{
|
|
77
|
+
let at = fresh.includes(b) && model(b).map((m)=>price.get(m)).find(Boolean);
|
|
78
|
+
if (!at) return b;
|
|
79
|
+
let usage = {
|
|
80
|
+
...was(b)?.[USAGE],
|
|
81
|
+
...b[USAGE]
|
|
82
|
+
};
|
|
83
|
+
let dollars = weigh(at, usage);
|
|
84
|
+
return {
|
|
85
|
+
...b,
|
|
86
|
+
[COST]: {
|
|
87
|
+
dollars,
|
|
88
|
+
reported: false
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
/** `session.cost` as SQL, for @yaks/sqlite's derived-property registry: the
|
|
96
|
+
* dollars its entries record, or nothing where none records any. */ export let sessionCost = {
|
|
97
|
+
stable: true,
|
|
98
|
+
tag: 'number',
|
|
99
|
+
whole: false,
|
|
100
|
+
expr: (owner)=>sub(select({
|
|
101
|
+
cols: [
|
|
102
|
+
fn('sum', col('dollars', 'k'))
|
|
103
|
+
],
|
|
104
|
+
from: table('entry', 'e'),
|
|
105
|
+
joins: [
|
|
106
|
+
join(table(COST, 'k'), eq(col('entity', 'k'), col('entity', 'e')))
|
|
107
|
+
],
|
|
108
|
+
where: eq(col('session', 'e'), owner)
|
|
109
|
+
}))
|
|
110
|
+
};
|
|
111
|
+
//# sourceMappingURL=cost.js.map
|