@yaks/model 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.
Files changed (126) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +868 -2
  3. package/_dist/afford.d.ts +69 -0
  4. package/_dist/afford.d.ts.map +1 -0
  5. package/_dist/attribute.d.ts +52 -0
  6. package/_dist/attribute.d.ts.map +1 -0
  7. package/_dist/batch.d.ts +28 -0
  8. package/_dist/batch.d.ts.map +1 -0
  9. package/_dist/cache.d.ts +15 -0
  10. package/_dist/cache.d.ts.map +1 -0
  11. package/_dist/deferred.d.ts +9 -0
  12. package/_dist/deferred.d.ts.map +1 -0
  13. package/_dist/graph.d.ts +3 -0
  14. package/_dist/graph.d.ts.map +1 -0
  15. package/_dist/image.d.ts +46 -0
  16. package/_dist/image.d.ts.map +1 -0
  17. package/_dist/inference.d.ts +58 -0
  18. package/_dist/inference.d.ts.map +1 -0
  19. package/_dist/limits.d.ts +142 -0
  20. package/_dist/limits.d.ts.map +1 -0
  21. package/_dist/media.d.ts +28 -0
  22. package/_dist/media.d.ts.map +1 -0
  23. package/_dist/message.d.ts +5 -0
  24. package/_dist/message.d.ts.map +1 -0
  25. package/_dist/mod.d.ts +298 -0
  26. package/_dist/mod.d.ts.map +1 -0
  27. package/_dist/offers.d.ts +19 -0
  28. package/_dist/offers.d.ts.map +1 -0
  29. package/_dist/onward.d.ts +77 -0
  30. package/_dist/onward.d.ts.map +1 -0
  31. package/_dist/prices.d.ts +27 -0
  32. package/_dist/prices.d.ts.map +1 -0
  33. package/_dist/provider.d.ts +49 -0
  34. package/_dist/provider.d.ts.map +1 -0
  35. package/_dist/results.d.ts +21 -0
  36. package/_dist/results.d.ts.map +1 -0
  37. package/_dist/route.d.ts +189 -0
  38. package/_dist/route.d.ts.map +1 -0
  39. package/_dist/sse.d.ts +4 -0
  40. package/_dist/sse.d.ts.map +1 -0
  41. package/_dist/stall.d.ts +47 -0
  42. package/_dist/stall.d.ts.map +1 -0
  43. package/_dist/step.d.ts +71 -0
  44. package/_dist/step.d.ts.map +1 -0
  45. package/_dist/submission.d.ts +7 -0
  46. package/_dist/submission.d.ts.map +1 -0
  47. package/_dist/vocab.d.ts +82 -0
  48. package/_dist/vocab.d.ts.map +1 -0
  49. package/_dist/windows.d.ts +6 -0
  50. package/_dist/windows.d.ts.map +1 -0
  51. package/afford.js +135 -0
  52. package/afford.js.map +1 -0
  53. package/afford.ts +208 -0
  54. package/attribute.js +109 -0
  55. package/attribute.js.map +1 -0
  56. package/attribute.ts +120 -0
  57. package/batch.js +8 -0
  58. package/batch.js.map +1 -0
  59. package/batch.ts +37 -0
  60. package/cache.js +28 -0
  61. package/cache.js.map +1 -0
  62. package/cache.ts +53 -0
  63. package/deferred.js +29 -0
  64. package/deferred.js.map +1 -0
  65. package/deferred.ts +54 -0
  66. package/deno.json +1 -0
  67. package/graph.js +8 -0
  68. package/graph.js.map +1 -0
  69. package/graph.ts +9 -0
  70. package/image.js +118 -0
  71. package/image.js.map +1 -0
  72. package/image.ts +108 -0
  73. package/inference.js +119 -0
  74. package/inference.js.map +1 -0
  75. package/inference.ts +223 -0
  76. package/limits.js +204 -0
  77. package/limits.js.map +1 -0
  78. package/limits.ts +230 -0
  79. package/media.js +76 -0
  80. package/media.js.map +1 -0
  81. package/media.ts +139 -0
  82. package/message.js +21 -0
  83. package/message.js.map +1 -0
  84. package/message.ts +17 -0
  85. package/mod.js +396 -0
  86. package/mod.js.map +1 -0
  87. package/mod.ts +695 -0
  88. package/offers.js +53 -0
  89. package/offers.js.map +1 -0
  90. package/offers.ts +84 -0
  91. package/onward.js +110 -0
  92. package/onward.js.map +1 -0
  93. package/onward.ts +123 -0
  94. package/package.json +53 -4
  95. package/prices.js +61 -0
  96. package/prices.js.map +1 -0
  97. package/prices.json +643 -0
  98. package/prices.ts +60 -0
  99. package/provider.js +6 -0
  100. package/provider.js.map +1 -0
  101. package/provider.ts +89 -0
  102. package/results.js +51 -0
  103. package/results.js.map +1 -0
  104. package/results.ts +47 -0
  105. package/route.js +263 -0
  106. package/route.js.map +1 -0
  107. package/route.ts +359 -0
  108. package/sse.js +71 -0
  109. package/sse.js.map +1 -0
  110. package/sse.ts +82 -0
  111. package/stall.js +166 -0
  112. package/stall.js.map +1 -0
  113. package/stall.ts +201 -0
  114. package/step.js +226 -0
  115. package/step.js.map +1 -0
  116. package/step.ts +265 -0
  117. package/submission.js +38 -0
  118. package/submission.js.map +1 -0
  119. package/submission.ts +38 -0
  120. package/vocab.js +83 -0
  121. package/vocab.js.map +1 -0
  122. package/vocab.json +613 -0
  123. package/vocab.ts +139 -0
  124. package/windows.js +35 -0
  125. package/windows.js.map +1 -0
  126. package/windows.ts +28 -0
package/vocab.ts ADDED
@@ -0,0 +1,139 @@
1
+ // The component declarations and their price/usage helpers: the module a
2
+ // server or browser page imports at `@yaks/model/vocab`. It reaches no storage,
3
+ // no SQL and no provider runtime.
4
+ //
5
+ // What a reply carries is declared elsewhere: `tool` belongs to @yaks/tools and
6
+ // `artifact` to @yaks/blob. An application that wants them composes those
7
+ // packages in — that is what a plugin list is for. Declaring them here as well
8
+ // would declare one component in two places, which `loadVocab` rejects.
9
+
10
+ import type { VocabDoc } from 'jsr:@yaks/vocab@0.2.36'
11
+ import type { Paid } from './attribute.ts'
12
+ import doc from './vocab.json' with { type: 'json' }
13
+ import manifest from './package.json' with { type: 'json' }
14
+
15
+ /** The `provider` and `model` components, to load beside your own. */
16
+ export let modelDoc: VocabDoc = doc
17
+
18
+ /** What this package is: its manifest's description, the one place it is
19
+ * written, which a host stamps on each document here. */
20
+ export let description: string = manifest.description
21
+
22
+ /** Every document this plugin declares. */
23
+ export let docs: VocabDoc[] = [modelDoc]
24
+
25
+ export let PROVIDER = 'provider'
26
+ export let MODEL = 'model'
27
+ export let USAGE = 'usage'
28
+ export let PRICE = 'price'
29
+ export let COST = 'cost'
30
+ export let RATE_LIMIT = 'rate_limit'
31
+
32
+ /** Counts for one request. Cached input and reasoning output are subsets,
33
+ * not additional tokens. Missing counts are unknown. */
34
+ export type Usage = {
35
+ input_tokens?: number
36
+ output_tokens?: number
37
+ total_tokens?: number
38
+ cached_tokens?: number
39
+ cache_write_tokens?: number
40
+ cache_write_1h_tokens?: number
41
+ cache_write_12h_tokens?: number
42
+ reasoning_tokens?: number
43
+ /** web searches a provider ran for the request, each billed at
44
+ * {@link Price} `web_search` */
45
+ web_searches?: number
46
+ }
47
+
48
+ /** What a model costs, in dollars per million tokens: the `price` component
49
+ * on its row. `cached` is the price of an input token the provider read from
50
+ * its cache, where it has one; `cache_write` of one it wrote there, and
51
+ * `cache_write_1h` of one written to be kept an hour. `web_search` is dollars
52
+ * per web search its provider runs for a request. */
53
+ export type Price = {
54
+ input: number
55
+ output: number
56
+ cached?: number
57
+ cache_write?: number
58
+ cache_write_1h?: number
59
+ cache_write_12h?: number
60
+ web_search?: number
61
+ }
62
+
63
+ /**
64
+ * What a request cost, in dollars, weighed at its model's price: its cached
65
+ * input at the cached price, what it wrote to the cache at the write price
66
+ * (an hour's writes at theirs), the rest of its input and all of its output at
67
+ * theirs, and each web search its provider ran at the search price.
68
+ *
69
+ * ```ts
70
+ * import { assertAlmostEquals } from '@std/assert'
71
+ * import { weigh } from '@yaks/model'
72
+ *
73
+ * let flash = { input: 0.15, cached: 0.03, output: 0.5 }
74
+ * let cost = weigh(flash, {
75
+ * input_tokens: 2_000_000,
76
+ * cached_tokens: 1_000_000,
77
+ * output_tokens: 1_000_000,
78
+ * })
79
+ * assertAlmostEquals(cost, 0.15 + 0.03 + 0.5)
80
+ *
81
+ * let opus = { input: 4, cached: 0.2, cache_write: 5, cache_write_1h: 8, output: 20 }
82
+ * assertAlmostEquals(weigh(opus, {
83
+ * input_tokens: 3_000_000,
84
+ * cached_tokens: 1_000_000,
85
+ * cache_write_tokens: 2_000_000,
86
+ * cache_write_1h_tokens: 1_000_000,
87
+ * }), 0.2 + 5 + 8)
88
+ *
89
+ * let searched = { input: 2, output: 10, web_search: 0.01 }
90
+ * assertAlmostEquals(weigh(searched, { web_searches: 3 }), 0.03)
91
+ * ```
92
+ */
93
+ export let weigh = (price: Price, n: Usage): number => {
94
+ let input = n.input_tokens ?? 0
95
+ let cached = Math.min(n.cached_tokens ?? 0, input)
96
+ let written = Math.min(n.cache_write_tokens ?? 0, input - cached)
97
+ return dollars(
98
+ price,
99
+ [cached, written, input - cached - written, n.output_tokens ?? 0],
100
+ hourly(n),
101
+ n.cache_write_tokens
102
+ ? Math.min(1, (n.cache_write_12h_tokens ?? 0) / n.cache_write_tokens)
103
+ : 0,
104
+ ) + (n.web_searches ?? 0) * (price.web_search ?? 0)
105
+ }
106
+
107
+ /** The share of a request's cache writes kept an hour, 0 to 1. */
108
+ export let hourly = (n: Usage): number =>
109
+ n.cache_write_tokens
110
+ ? Math.min(1, (n.cache_write_1h_tokens ?? 0) / n.cache_write_tokens)
111
+ : 0
112
+
113
+ /**
114
+ * What tokens cost at a price, paid as `Paid` says (read from the cache,
115
+ * written to it, fresh, generated), with `hour` of the writes kept an hour.
116
+ *
117
+ * ```ts
118
+ * import { dollars } from '@yaks/model'
119
+ * import { assertAlmostEquals } from '@std/assert'
120
+ *
121
+ * let opus = { input: 4, cached: 0.2, cache_write: 5, cache_write_1h: 8, output: 20 }
122
+ * assertAlmostEquals(dollars(opus, [1e6, 1e6, 1e6, 1e6], 0.5), 0.2 + 6.5 + 4 + 20)
123
+ * ```
124
+ */
125
+ export let dollars = (
126
+ price: Price,
127
+ [read, write, fresh, output]: Paid,
128
+ hour = 0,
129
+ halfDay = 0,
130
+ ): number => {
131
+ let w = price.cache_write ?? price.input
132
+ return (read * (price.cached ?? price.input) +
133
+ write * (
134
+ (1 - hour - halfDay) * w +
135
+ hour * (price.cache_write_1h ?? w) +
136
+ halfDay * (price.cache_write_12h ?? w)
137
+ ) +
138
+ fresh * price.input + output * price.output) / 1e6
139
+ }
package/windows.js ADDED
@@ -0,0 +1,35 @@
1
+ // What a catalog says of each model's window, kept on the graph's model rows.
2
+ import { identityEid } from "@yaks/graph/identity";
3
+ import { MODEL } from "./vocab.js";
4
+ /** Give each listed model's row the window its catalog says, where the row
5
+ * has none of its own: a window set by hand stands. Answers each listed
6
+ * model with the window its row now has. */ export let windows = async (graph, models)=>{
7
+ let eids = models.map((m)=>identityEid(MODEL, [
8
+ m.name
9
+ ]));
10
+ let rows = new Map((await graph.get(eids, [
11
+ MODEL
12
+ ])).map((b)=>[
13
+ b.entity.eid,
14
+ b[MODEL]
15
+ ]));
16
+ let own = (i)=>Number(rows.get(eids[i])?.context) || undefined;
17
+ let filled = models.flatMap((m, i)=>rows.get(eids[i]) && !own(i) && m.context ? [
18
+ {
19
+ entity: {
20
+ eid: eids[i]
21
+ },
22
+ [MODEL]: {
23
+ context: m.context
24
+ }
25
+ }
26
+ ] : []);
27
+ if (filled.length) await graph.apply(filled, {
28
+ trusted: true
29
+ });
30
+ return models.map((m, i)=>({
31
+ name: m.name,
32
+ context: own(i) ?? m.context
33
+ }));
34
+ };
35
+ //# sourceMappingURL=windows.js.map
package/windows.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"windows.js","sources":["./windows.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAG9E,SAAS,WAAW,oCAAyC;AAE7D,SAAS,KAAK,qBAAoB;AAElC;;0CAE0C,GAC1C,OAAO,IAAI,UAAU,OACnB,OACA;EAEA,IAAI,OAAO,OAAO,GAAG,CAAC,CAAC,IAAM,YAAY,OAAO;MAAC,EAAE,IAAI;KAAC;EACxD,IAAI,OAAO,IAAI,IACb,CAAC,MAAM,MAAM,GAAG,CAAC,MAAM;IAAC;GAAM,CAAC,EAAE,GAAG,CAAC,CAAC,IAAM;MAAC,EAAE,MAAM,CAAC,GAAG;MAAE,CAAC,CAAC,MAAM;KAAC;EAEtE,IAAI,MAAM,CAAC,IACT,OAAQ,KAAK,GAAG,CAAC,IAAI,CAAC,EAAE,GAAwB,YAAY;EAC9D,IAAI,SAAS,OAAO,OAAO,CAAC,CAAC,GAAG,IAC9B,KAAK,GAAG,CAAC,IAAI,CAAC,EAAE,KAAK,CAAC,IAAI,MAAM,EAAE,OAAO,GACrC;MAAC;QAAE,QAAQ;UAAE,KAAK,IAAI,CAAC,EAAE;QAAC;QAAG,CAAC,MAAM,EAAE;UAAE,SAAS,EAAE,OAAO;QAAC;MAAE;KAAE,GAC/D,EAAE;EAER,IAAI,OAAO,MAAM,EAAE,MAAM,MAAM,KAAK,CAAC,QAAQ;IAAE,SAAS;EAAK;EAC7D,OAAO,OAAO,GAAG,CAAC,CAAC,GAAG,IAAM,CAAC;MAAE,MAAM,EAAE,IAAI;MAAE,SAAS,IAAI,MAAM,EAAE,OAAO;IAAC,CAAC;AAC7E,EAAC"}
package/windows.ts ADDED
@@ -0,0 +1,28 @@
1
+ // What a catalog says of each model's window, kept on the graph's model rows.
2
+
3
+ import type { Comp, Graph } from 'jsr:@yaks/graph@0.2.36'
4
+ import { identityEid } from '@yaks/graph/identity'
5
+ import type { Listed } from './mod.ts'
6
+ import { MODEL } from './vocab.js'
7
+
8
+ /** Give each listed model's row the window its catalog says, where the row
9
+ * has none of its own: a window set by hand stands. Answers each listed
10
+ * model with the window its row now has. */
11
+ export let windows = async (
12
+ graph: Pick<Graph, 'get' | 'apply'>,
13
+ models: Listed[],
14
+ ): Promise<Listed[]> => {
15
+ let eids = models.map((m) => identityEid(MODEL, [m.name]))
16
+ let rows = new Map(
17
+ (await graph.get(eids, [MODEL])).map((b) => [b.entity.eid, b[MODEL]]),
18
+ )
19
+ let own = (i: number) =>
20
+ Number((rows.get(eids[i]) as Comp | undefined)?.context) || undefined
21
+ let filled = models.flatMap((m, i) =>
22
+ rows.get(eids[i]) && !own(i) && m.context
23
+ ? [{ entity: { eid: eids[i] }, [MODEL]: { context: m.context } }]
24
+ : []
25
+ )
26
+ if (filled.length) await graph.apply(filled, { trusted: true })
27
+ return models.map((m, i) => ({ name: m.name, context: own(i) ?? m.context }))
28
+ }