@avow-finance/sdk 0.0.0-stage → 0.2.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.
Files changed (267) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +122 -2
  3. package/dist/agent-entry.d.ts +15 -0
  4. package/dist/agent-entry.d.ts.map +1 -0
  5. package/dist/agent-entry.js +11 -0
  6. package/dist/agent-entry.js.map +1 -0
  7. package/dist/agent-key.d.ts +34 -0
  8. package/dist/agent-key.d.ts.map +1 -0
  9. package/dist/agent-key.js +33 -0
  10. package/dist/agent-key.js.map +1 -0
  11. package/dist/agent.d.ts +261 -0
  12. package/dist/agent.d.ts.map +1 -0
  13. package/dist/agent.js +493 -0
  14. package/dist/agent.js.map +1 -0
  15. package/dist/avow-api.d.ts +78 -0
  16. package/dist/avow-api.d.ts.map +1 -0
  17. package/dist/avow-api.js +2 -0
  18. package/dist/avow-api.js.map +1 -0
  19. package/dist/contest.d.ts +135 -0
  20. package/dist/contest.d.ts.map +1 -0
  21. package/dist/contest.js +186 -0
  22. package/dist/contest.js.map +1 -0
  23. package/dist/http.d.ts +161 -0
  24. package/dist/http.d.ts.map +1 -0
  25. package/dist/http.js +311 -0
  26. package/dist/http.js.map +1 -0
  27. package/dist/index.d.ts +38 -0
  28. package/dist/index.d.ts.map +1 -0
  29. package/dist/index.js +38 -0
  30. package/dist/index.js.map +1 -0
  31. package/dist/job/accept.d.ts +84 -0
  32. package/dist/job/accept.d.ts.map +1 -0
  33. package/dist/job/accept.js +136 -0
  34. package/dist/job/accept.js.map +1 -0
  35. package/dist/job/customer.d.ts +43 -0
  36. package/dist/job/customer.d.ts.map +1 -0
  37. package/dist/job/customer.js +83 -0
  38. package/dist/job/customer.js.map +1 -0
  39. package/dist/job/quote.d.ts +34 -0
  40. package/dist/job/quote.d.ts.map +1 -0
  41. package/dist/job/quote.js +37 -0
  42. package/dist/job/quote.js.map +1 -0
  43. package/dist/lock.d.ts +269 -0
  44. package/dist/lock.d.ts.map +1 -0
  45. package/dist/lock.js +283 -0
  46. package/dist/lock.js.map +1 -0
  47. package/dist/moderato.d.ts +27 -0
  48. package/dist/moderato.d.ts.map +1 -0
  49. package/dist/moderato.js +34 -0
  50. package/dist/moderato.js.map +1 -0
  51. package/dist/node/cli.d.ts +3 -0
  52. package/dist/node/cli.d.ts.map +1 -0
  53. package/dist/node/cli.js +346 -0
  54. package/dist/node/cli.js.map +1 -0
  55. package/dist/node/connect.d.ts +15 -0
  56. package/dist/node/connect.d.ts.map +1 -0
  57. package/dist/node/connect.js +91 -0
  58. package/dist/node/connect.js.map +1 -0
  59. package/dist/node/eval.d.ts +3 -0
  60. package/dist/node/eval.d.ts.map +1 -0
  61. package/dist/node/eval.js +67 -0
  62. package/dist/node/eval.js.map +1 -0
  63. package/dist/node/first-run.d.ts +50 -0
  64. package/dist/node/first-run.d.ts.map +1 -0
  65. package/dist/node/first-run.js +149 -0
  66. package/dist/node/first-run.js.map +1 -0
  67. package/dist/node/job.d.ts +50 -0
  68. package/dist/node/job.d.ts.map +1 -0
  69. package/dist/node/job.js +176 -0
  70. package/dist/node/job.js.map +1 -0
  71. package/dist/node/mailbox.d.ts +19 -0
  72. package/dist/node/mailbox.d.ts.map +1 -0
  73. package/dist/node/mailbox.js +81 -0
  74. package/dist/node/mailbox.js.map +1 -0
  75. package/dist/node/mcp.d.ts +74 -0
  76. package/dist/node/mcp.d.ts.map +1 -0
  77. package/dist/node/mcp.js +367 -0
  78. package/dist/node/mcp.js.map +1 -0
  79. package/dist/node/platform.d.ts +35 -0
  80. package/dist/node/platform.d.ts.map +1 -0
  81. package/dist/node/platform.js +102 -0
  82. package/dist/node/platform.js.map +1 -0
  83. package/dist/node/runner-session.d.ts +41 -0
  84. package/dist/node/runner-session.d.ts.map +1 -0
  85. package/dist/node/runner-session.js +100 -0
  86. package/dist/node/runner-session.js.map +1 -0
  87. package/dist/node/runner.d.ts +63 -0
  88. package/dist/node/runner.d.ts.map +1 -0
  89. package/dist/node/runner.js +341 -0
  90. package/dist/node/runner.js.map +1 -0
  91. package/dist/node/service-child.d.ts +2 -0
  92. package/dist/node/service-child.d.ts.map +1 -0
  93. package/dist/node/service-child.js +40 -0
  94. package/dist/node/service-child.js.map +1 -0
  95. package/dist/node/service-excerpt.d.ts +21 -0
  96. package/dist/node/service-excerpt.d.ts.map +1 -0
  97. package/dist/node/service-excerpt.js +32 -0
  98. package/dist/node/service-excerpt.js.map +1 -0
  99. package/dist/node/service-process.d.ts +12 -0
  100. package/dist/node/service-process.d.ts.map +1 -0
  101. package/dist/node/service-process.js +132 -0
  102. package/dist/node/service-process.js.map +1 -0
  103. package/dist/node/session-input.d.ts +12 -0
  104. package/dist/node/session-input.d.ts.map +1 -0
  105. package/dist/node/session-input.js +42 -0
  106. package/dist/node/session-input.js.map +1 -0
  107. package/dist/node/session-view.d.ts +20 -0
  108. package/dist/node/session-view.d.ts.map +1 -0
  109. package/dist/node/session-view.js +198 -0
  110. package/dist/node/session-view.js.map +1 -0
  111. package/dist/node-agent.d.ts +2 -0
  112. package/dist/node-agent.d.ts.map +1 -0
  113. package/dist/node-agent.js +6 -0
  114. package/dist/node-agent.js.map +1 -0
  115. package/dist/node.d.ts +2 -0
  116. package/dist/node.d.ts.map +1 -0
  117. package/dist/node.js +9 -0
  118. package/dist/node.js.map +1 -0
  119. package/dist/sandbox/agent.d.ts +187 -0
  120. package/dist/sandbox/agent.d.ts.map +1 -0
  121. package/dist/sandbox/agent.js +468 -0
  122. package/dist/sandbox/agent.js.map +1 -0
  123. package/dist/sandbox/business-store.d.ts +19 -0
  124. package/dist/sandbox/business-store.d.ts.map +1 -0
  125. package/dist/sandbox/business-store.js +19 -0
  126. package/dist/sandbox/business-store.js.map +1 -0
  127. package/dist/sandbox/business.d.ts +70 -0
  128. package/dist/sandbox/business.d.ts.map +1 -0
  129. package/dist/sandbox/business.js +115 -0
  130. package/dist/sandbox/business.js.map +1 -0
  131. package/dist/sandbox/connect.d.ts +64 -0
  132. package/dist/sandbox/connect.d.ts.map +1 -0
  133. package/dist/sandbox/connect.js +182 -0
  134. package/dist/sandbox/connect.js.map +1 -0
  135. package/dist/sandbox/deployment.d.ts +14 -0
  136. package/dist/sandbox/deployment.d.ts.map +1 -0
  137. package/dist/sandbox/deployment.js +38 -0
  138. package/dist/sandbox/deployment.js.map +1 -0
  139. package/dist/sandbox/keys.d.ts +45 -0
  140. package/dist/sandbox/keys.d.ts.map +1 -0
  141. package/dist/sandbox/keys.js +132 -0
  142. package/dist/sandbox/keys.js.map +1 -0
  143. package/dist/sandbox/mailbox.d.ts +90 -0
  144. package/dist/sandbox/mailbox.d.ts.map +1 -0
  145. package/dist/sandbox/mailbox.js +400 -0
  146. package/dist/sandbox/mailbox.js.map +1 -0
  147. package/dist/sandbox/mandate.d.ts +84 -0
  148. package/dist/sandbox/mandate.d.ts.map +1 -0
  149. package/dist/sandbox/mandate.js +107 -0
  150. package/dist/sandbox/mandate.js.map +1 -0
  151. package/dist/sandbox/namespace.d.ts +16 -0
  152. package/dist/sandbox/namespace.d.ts.map +1 -0
  153. package/dist/sandbox/namespace.js +16 -0
  154. package/dist/sandbox/namespace.js.map +1 -0
  155. package/dist/sandbox/session.d.ts +60 -0
  156. package/dist/sandbox/session.d.ts.map +1 -0
  157. package/dist/sandbox/session.js +94 -0
  158. package/dist/sandbox/session.js.map +1 -0
  159. package/dist/sandbox/setup.d.ts +136 -0
  160. package/dist/sandbox/setup.d.ts.map +1 -0
  161. package/dist/sandbox/setup.js +490 -0
  162. package/dist/sandbox/setup.js.map +1 -0
  163. package/dist/sandbox/store.d.ts +69 -0
  164. package/dist/sandbox/store.d.ts.map +1 -0
  165. package/dist/sandbox/store.js +115 -0
  166. package/dist/sandbox/store.js.map +1 -0
  167. package/dist/sandbox/supplier-key.d.ts +73 -0
  168. package/dist/sandbox/supplier-key.d.ts.map +1 -0
  169. package/dist/sandbox/supplier-key.js +180 -0
  170. package/dist/sandbox/supplier-key.js.map +1 -0
  171. package/dist/sandbox/suppliers.d.ts +7 -0
  172. package/dist/sandbox/suppliers.d.ts.map +1 -0
  173. package/dist/sandbox/suppliers.js +29 -0
  174. package/dist/sandbox/suppliers.js.map +1 -0
  175. package/dist/sandbox/terms.d.ts +32 -0
  176. package/dist/sandbox/terms.d.ts.map +1 -0
  177. package/dist/sandbox/terms.js +58 -0
  178. package/dist/sandbox/terms.js.map +1 -0
  179. package/dist/sandbox/vault.d.ts +24 -0
  180. package/dist/sandbox/vault.d.ts.map +1 -0
  181. package/dist/sandbox/vault.js +54 -0
  182. package/dist/sandbox/vault.js.map +1 -0
  183. package/dist/send.d.ts +122 -0
  184. package/dist/send.d.ts.map +1 -0
  185. package/dist/send.js +328 -0
  186. package/dist/send.js.map +1 -0
  187. package/dist/service-entry.d.ts +6 -0
  188. package/dist/service-entry.d.ts.map +1 -0
  189. package/dist/service-entry.js +6 -0
  190. package/dist/service-entry.js.map +1 -0
  191. package/dist/session/anthropic.d.ts +17 -0
  192. package/dist/session/anthropic.d.ts.map +1 -0
  193. package/dist/session/anthropic.js +215 -0
  194. package/dist/session/anthropic.js.map +1 -0
  195. package/dist/session/charge.d.ts +13 -0
  196. package/dist/session/charge.d.ts.map +1 -0
  197. package/dist/session/charge.js +33 -0
  198. package/dist/session/charge.js.map +1 -0
  199. package/dist/session/data.d.ts +8 -0
  200. package/dist/session/data.d.ts.map +1 -0
  201. package/dist/session/data.js +20 -0
  202. package/dist/session/data.js.map +1 -0
  203. package/dist/session/eval.d.ts +41 -0
  204. package/dist/session/eval.d.ts.map +1 -0
  205. package/dist/session/eval.js +281 -0
  206. package/dist/session/eval.js.map +1 -0
  207. package/dist/session/executor.d.ts +80 -0
  208. package/dist/session/executor.d.ts.map +1 -0
  209. package/dist/session/executor.js +381 -0
  210. package/dist/session/executor.js.map +1 -0
  211. package/dist/session/fixture-world.d.ts +29 -0
  212. package/dist/session/fixture-world.d.ts.map +1 -0
  213. package/dist/session/fixture-world.js +708 -0
  214. package/dist/session/fixture-world.js.map +1 -0
  215. package/dist/session/live-world.d.ts +22 -0
  216. package/dist/session/live-world.d.ts.map +1 -0
  217. package/dist/session/live-world.js +329 -0
  218. package/dist/session/live-world.js.map +1 -0
  219. package/dist/session/ollama.d.ts +29 -0
  220. package/dist/session/ollama.d.ts.map +1 -0
  221. package/dist/session/ollama.js +177 -0
  222. package/dist/session/ollama.js.map +1 -0
  223. package/dist/session/provider.d.ts +79 -0
  224. package/dist/session/provider.d.ts.map +1 -0
  225. package/dist/session/provider.js +19 -0
  226. package/dist/session/provider.js.map +1 -0
  227. package/dist/session/scripted.d.ts +45 -0
  228. package/dist/session/scripted.d.ts.map +1 -0
  229. package/dist/session/scripted.js +236 -0
  230. package/dist/session/scripted.js.map +1 -0
  231. package/dist/session/selection.d.ts +37 -0
  232. package/dist/session/selection.d.ts.map +1 -0
  233. package/dist/session/selection.js +104 -0
  234. package/dist/session/selection.js.map +1 -0
  235. package/dist/session/service-check.d.ts +48 -0
  236. package/dist/session/service-check.d.ts.map +1 -0
  237. package/dist/session/service-check.js +86 -0
  238. package/dist/session/service-check.js.map +1 -0
  239. package/dist/session/service.d.ts +128 -0
  240. package/dist/session/service.d.ts.map +1 -0
  241. package/dist/session/service.js +150 -0
  242. package/dist/session/service.js.map +1 -0
  243. package/dist/session/session.d.ts +95 -0
  244. package/dist/session/session.d.ts.map +1 -0
  245. package/dist/session/session.js +312 -0
  246. package/dist/session/session.js.map +1 -0
  247. package/dist/session/toolbox.d.ts +69 -0
  248. package/dist/session/toolbox.d.ts.map +1 -0
  249. package/dist/session/toolbox.js +391 -0
  250. package/dist/session/toolbox.js.map +1 -0
  251. package/dist/session/tools.d.ts +45 -0
  252. package/dist/session/tools.d.ts.map +1 -0
  253. package/dist/session/tools.js +137 -0
  254. package/dist/session/tools.js.map +1 -0
  255. package/dist/session/world.d.ts +188 -0
  256. package/dist/session/world.d.ts.map +1 -0
  257. package/dist/session/world.js +2 -0
  258. package/dist/session/world.js.map +1 -0
  259. package/dist/sign.d.ts +19 -0
  260. package/dist/sign.d.ts.map +1 -0
  261. package/dist/sign.js +33 -0
  262. package/dist/sign.js.map +1 -0
  263. package/dist/supplier.d.ts +41 -0
  264. package/dist/supplier.d.ts.map +1 -0
  265. package/dist/supplier.js +57 -0
  266. package/dist/supplier.js.map +1 -0
  267. package/package.json +56 -3
@@ -0,0 +1,312 @@
1
+ import { formatUsd, SAID_TEXT_MAX, speakerName, } from '@avow-finance/spec';
2
+ import { JobExecutor } from './executor.js';
3
+ import { ProviderError } from './provider.js';
4
+ import { CUSTOMER_REFUSED_PROMPT, ORDER_PROMPT, SWITCH_OFF_PROMPT } from './scripted.js';
5
+ import { policyChanges } from './service.js';
6
+ import { newSessionState, Toolbox } from './toolbox.js';
7
+ import { isJobTool, toolSpecs } from './tools.js';
8
+ /**
9
+ * The lines that take the next order. `job` or `/job` takes one that runs to payment, and
10
+ * `job cancels` takes one the test customer cancels after work starts.
11
+ */
12
+ export const JOB_COMMAND = /^\/?job(\s+cancels)?$/i;
13
+ /** The scenario a job command asks for, or null for a plain order. */
14
+ export const scenarioOf = (line) => JOB_COMMAND.exec(line.trim())?.[1] ? 'cancels_late' : null;
15
+ /** The most steps a model takes in one turn before the session stops it. */
16
+ const MAX_STEPS = 12;
17
+ export const SYSTEM_PROMPT = `You are a business's job agent in Avow's testnet sandbox. Avow's test customer sends paid fine-tuning jobs. A job needs GPUs from its start to its deadline, and the customer accepts your quote only when it names your accepted compute booking that holds those GPUs for the whole job, past its free cancellation.
18
+
19
+ For each order, call read_order, then compute_offers, then book_compute for the cheapest offer that is firm (no free cancellation left), holds the order's GPUs from before the job starts to past its deadline, takes that many GPUs in one booking and fits the compute budget. Then call submit_quote naming that booking. If the compute switch in service.ts is off, book nothing and call submit_quote with no booking.
20
+
21
+ Code outside you checks everything. service.ts applies the budget and the compute switch, the SDK, Avow and the lock check your authority, and the customer checks the quote. You never verify the customer, start work, deliver or pay, since the developer's code does that after your turn. When a tool refuses, say which layer refused, its reason code, and what would let it pass. Nothing typed can undo a transaction already sent. Answer the developer briefly and plainly, and explain your choices from the tool results you got.`;
22
+ /** What the session says once, when it starts, about the model and what goes where. */
23
+ export function whatGoesWhere(provider, brief = false) {
24
+ if (brief) {
25
+ return [
26
+ `Model: ${provider.line}.`,
27
+ provider.reaches === 'this machine'
28
+ ? 'Your keys and the whole conversation stay on this machine. Your browser shows what your agent says and does.'
29
+ : 'Your keys stay on this machine, and the conversation goes to Anthropic. Your browser shows what your agent says and does.',
30
+ ];
31
+ }
32
+ return [
33
+ `Model: ${provider.line}.`,
34
+ "Stays on this machine: your business's keys, its passphrase and the sealed key file.",
35
+ provider.reaches === 'this machine'
36
+ ? 'Stays on this machine too: the whole conversation, since the model runs here.'
37
+ : 'Goes to Anthropic: the conversation, meaning the order, the offers, tool results and what you type.',
38
+ "Goes to your screen through Avow's relay: the agent's speech, its tool calls and their results, facts, receipts and refusals. What you type isn't posted, though the agent's replies can repeat it.",
39
+ ];
40
+ }
41
+ /** Text in pieces the event schema takes. */
42
+ function chunks(text) {
43
+ const out = [];
44
+ for (let i = 0; i < text.length; i += SAID_TEXT_MAX)
45
+ out.push(text.slice(i, i + SAID_TEXT_MAX));
46
+ return out;
47
+ }
48
+ const IN_FLIGHT = {
49
+ book_compute: 'the compute booking being sent',
50
+ submit_quote: 'the quote being sent',
51
+ cancel_compute: 'the cancellation being sent',
52
+ };
53
+ export async function runSession(deps) {
54
+ const { world, provider, view } = deps;
55
+ const model = speakerName(provider.model);
56
+ const state = newSessionState();
57
+ const turns = [];
58
+ let policy = await deps.readPolicy();
59
+ const inFlight = (what) => deps.input?.inFlight(what);
60
+ const toolbox = new Toolbox(world, () => policy, deps.emit, state, refresh);
61
+ const executor = new JobExecutor({
62
+ world,
63
+ emit: deps.emit,
64
+ checkBeforeWork: deps.checkBeforeWork,
65
+ note: (line) => view.quiet(line),
66
+ busy: inFlight,
67
+ ...(deps.pollMs === undefined ? {} : { pollMs: deps.pollMs }),
68
+ ...(deps.workMs === undefined ? {} : { workMs: deps.workMs }),
69
+ });
70
+ view.intro([
71
+ ...whatGoesWhere(provider, deps.brief),
72
+ ...(deps.input && !deps.afterJob
73
+ ? [
74
+ 'Type job to take the next order, or job cancels for one the customer cancels after work starts. Anything else you type goes to your agent.',
75
+ ]
76
+ : []),
77
+ ]);
78
+ // One model turn at a time, whichever started it.
79
+ let queue = Promise.resolve();
80
+ const oneAtATime = (work) => {
81
+ const next = queue.then(work);
82
+ queue = next.catch(() => { });
83
+ return next;
84
+ };
85
+ /** What service.ts held when it was last read, apart from a switch a guide holds off. */
86
+ let inFile = policy;
87
+ /**
88
+ * Reads service.ts again, says what changed, and gives the note the model needs about it. A
89
+ * guide can hold the compute switch off for an order, which is said as the guide's doing and
90
+ * never as a change to the file.
91
+ */
92
+ async function refresh() {
93
+ const read = await deps.readPolicy();
94
+ for (const line of policyChanges(inFile, read))
95
+ view.quiet(line);
96
+ inFile = read;
97
+ const held = read.computeSwitch === 'on' && deps.withoutCompute?.() === true;
98
+ const next = held ? { ...read, computeSwitch: 'off' } : read;
99
+ if (policy.computeSwitch === 'on' && next.computeSwitch === 'off') {
100
+ if (held) {
101
+ view.quiet('For this order your agent books no compute, as bookCompute: false in service.ts would have it.');
102
+ }
103
+ notes.push(`The developer ${SWITCH_OFF_PROMPT} ${held ? 'for this order' : 'in service.ts'} and still wants a quote.`);
104
+ }
105
+ else if (policy.computeSwitch === 'off' && next.computeSwitch === 'on') {
106
+ notes.push('Compute booking is on again.');
107
+ }
108
+ policy = next;
109
+ }
110
+ /** Notes the model needs about service.ts, each given once, with the next prompt. */
111
+ const notes = [];
112
+ /** One turn of the model, with each tool call run by the tool layer, until it ends its turn. */
113
+ async function turn(run, orderId, prompt) {
114
+ turns.push({ role: 'user', text: prompt });
115
+ let call = 0;
116
+ for (let step = 0; step < MAX_STEPS; step++) {
117
+ const speech = view.speak(model);
118
+ let reply;
119
+ try {
120
+ reply = await provider.reply({
121
+ system: SYSTEM_PROMPT,
122
+ turns,
123
+ tools: toolSpecs(policy.computeSwitch),
124
+ onText: speech.write,
125
+ });
126
+ }
127
+ finally {
128
+ speech.end();
129
+ }
130
+ for (const text of chunks(reply.text.trim())) {
131
+ deps.emit({
132
+ run,
133
+ source: 'session',
134
+ orderId,
135
+ type: 'said',
136
+ speaker: { kind: 'model', model },
137
+ text,
138
+ });
139
+ }
140
+ turns.push({ role: 'assistant', text: reply.text, calls: reply.calls, native: reply.native });
141
+ if (reply.calls.length === 0)
142
+ return;
143
+ for (const c of reply.calls) {
144
+ if (!isJobTool(c.name)) {
145
+ const why = `There's no tool named ${c.name}. The tools are the seven you were given.`;
146
+ turns.push({
147
+ role: 'tool',
148
+ call: c,
149
+ ok: false,
150
+ content: JSON.stringify({ refused: true, why }),
151
+ });
152
+ continue;
153
+ }
154
+ inFlight(IN_FLIGHT[c.name] ?? null);
155
+ try {
156
+ const outcome = await toolbox.run(c.name, c.input, { run, orderId, call: ++call });
157
+ turns.push({ role: 'tool', call: c, content: outcome.content, ok: outcome.ok });
158
+ }
159
+ finally {
160
+ inFlight(null);
161
+ }
162
+ }
163
+ }
164
+ view.quiet(`The model took ${MAX_STEPS} steps without ending its turn, so the session stopped it.`);
165
+ }
166
+ /** A job: the order comes in, the model quotes it, then the executor follows the quote. */
167
+ async function job(notes, scenario = null) {
168
+ let order;
169
+ try {
170
+ order = await world.pullOrder(scenario ?? undefined);
171
+ }
172
+ catch (error) {
173
+ // A customer that can't play a scenario out refuses the order, and the session goes on.
174
+ if (scenario === null)
175
+ throw error;
176
+ const why = `Avow's test customer didn't offer a job it cancels. ${error instanceof Error ? error.message : String(error)}`;
177
+ // A guide says how each job ended itself.
178
+ if (!deps.afterJob)
179
+ view.quiet(why);
180
+ return { kind: 'stopped', why };
181
+ }
182
+ state.order = order;
183
+ const run = deps.nextRun();
184
+ const t = order.terms;
185
+ deps.emit({
186
+ run,
187
+ source: 'session',
188
+ orderId: order.orderId,
189
+ type: 'job_order_received',
190
+ customer: order.customer,
191
+ work: t.subject.work,
192
+ price: t.consideration.kind === 'fixed' ? t.consideration.amount : 0n,
193
+ deliverBy: t.subject.deliverBy,
194
+ requirement: order.requirement,
195
+ });
196
+ const prompt = [`${ORDER_PROMPT} ${order.orderId} from Avow's test customer.`, ...notes];
197
+ await oneAtATime(() => turn(run, order.orderId, prompt.join(' ')));
198
+ const quoted = state.quotes.get(order.orderId);
199
+ if (!quoted)
200
+ return null;
201
+ const outcome = await executor.follow({
202
+ run,
203
+ order,
204
+ quote: quoted.quote,
205
+ ...(scenario ? { scenario } : {}),
206
+ });
207
+ if (outcome.kind === 'refused') {
208
+ const { code, layer } = outcome.refusal;
209
+ await oneAtATime(() => turn(run, order.orderId, `${CUSTOMER_REFUSED_PROMPT} with ${code}, at the ${layer} layer. Explain to the developer what that means and what would let a quote pass.`));
210
+ }
211
+ else if (outcome.kind === 'stopped') {
212
+ view.quiet(outcome.why);
213
+ }
214
+ return outcome;
215
+ }
216
+ /** Whether an order is still waiting for its quote. */
217
+ const openOrder = () => state.order !== null && !state.quotes.has(state.order.orderId);
218
+ /** The recorded listing and booking beside an answer to "why", as results shown again. */
219
+ function showEvidence(run) {
220
+ const listing = state.listing;
221
+ if (!listing)
222
+ return;
223
+ const listed = state.recorded.find((r) => r.run === listing.run && r.call === listing.call);
224
+ const booked = [...state.recorded].reverse().find((r) => r.tool === 'book_compute' && r.offer);
225
+ for (const r of [listed, booked]) {
226
+ if (r)
227
+ deps.emit({ ...r.event, run, orderId: null, recalled: { run: r.run, call: r.call } });
228
+ }
229
+ const { offers, listedAt } = listing.listing;
230
+ const limits = listing.limits;
231
+ const marked = offers.findIndex((o) => o.id === booked?.offer);
232
+ view.evidence({
233
+ title: `The offers the agent compared in run ${listing.run}, as the tool layer recorded them`,
234
+ columns: ['offer', 'GPUs', 'per GPU-hour', 'reserve per GPU', 'free cancellation'],
235
+ rows: offers.map((o) => [
236
+ o.id,
237
+ o.gpus.min === o.gpus.max ? `${o.gpus.min}` : `${o.gpus.min} to ${o.gpus.max}`,
238
+ formatUsd(o.pricePerGpuHour),
239
+ formatUsd(o.reservePerGpu),
240
+ o.freeCancelBy <= listedAt ? 'none, firm' : 'still open',
241
+ ]),
242
+ ...(marked >= 0 ? { marked } : {}),
243
+ notes: [
244
+ `The budget in service.ts was ${formatUsd(limits.budgetPerGpuHour)} per GPU-hour, with the compute switch ${limits.computeSwitch}.`,
245
+ `The agent could commit ${formatUsd(limits.capPerCommitment)} for one commitment, with ${formatUsd(limits.openReservesLeft)} left of ${formatUsd(limits.capOpenReserves)} held at once.`,
246
+ ],
247
+ });
248
+ }
249
+ let firstJob = null;
250
+ const jobs = [];
251
+ try {
252
+ if (!deps.input) {
253
+ firstJob = await job([]);
254
+ return { firstJob, stopped: null };
255
+ }
256
+ // The first job runs on its own, and the developer can type while it goes on, except on a
257
+ // guided first run, which shows the whole job before it asks for anything.
258
+ const first = job([]).then((outcome) => {
259
+ firstJob = outcome;
260
+ });
261
+ jobs.push(first);
262
+ if (deps.afterJob) {
263
+ await first;
264
+ deps.afterJob(firstJob);
265
+ }
266
+ let failed = null;
267
+ first.catch((error) => {
268
+ failed = error;
269
+ });
270
+ for (;;) {
271
+ const line = await deps.input.next();
272
+ if (failed)
273
+ throw failed;
274
+ if (line === null)
275
+ break;
276
+ const text = line.trim();
277
+ if (!text)
278
+ continue;
279
+ await refresh();
280
+ // Only the job command takes the next order. Everything else goes to the model.
281
+ if (JOB_COMMAND.test(text)) {
282
+ if (openOrder())
283
+ view.quiet('An order is still waiting for its quote, so the session takes no other yet.');
284
+ else {
285
+ const next = job(notes.splice(0), scenarioOf(text) ?? deps.scenario?.() ?? null);
286
+ jobs.push(next);
287
+ if (deps.afterJob)
288
+ deps.afterJob(await next);
289
+ }
290
+ continue;
291
+ }
292
+ const run = deps.nextRun();
293
+ await oneAtATime(() => turn(run, null, [...notes.splice(0), text].join(' ')));
294
+ if (/\bwhy\b/i.test(text))
295
+ showEvidence(run);
296
+ }
297
+ await Promise.allSettled(jobs);
298
+ return { firstJob, stopped: null };
299
+ }
300
+ catch (error) {
301
+ if (error instanceof ProviderError) {
302
+ view.fail(`${provider.line} failed. ${error.message} The session switches to no other model.`);
303
+ return { firstJob, stopped: error.message };
304
+ }
305
+ throw error;
306
+ }
307
+ finally {
308
+ // A charge still inside its contest window is followed only while the session runs.
309
+ executor.stop();
310
+ }
311
+ }
312
+ //# sourceMappingURL=session.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session.js","sourceRoot":"","sources":["../../src/session/session.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EAGT,aAAa,EAEb,WAAW,GACZ,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EAAE,WAAW,EAAmB,MAAM,eAAe,CAAA;AAE5D,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAC7C,OAAO,EAAE,uBAAuB,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;AACxF,OAAO,EAAE,aAAa,EAAsD,MAAM,cAAc,CAAA;AAChG,OAAO,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACvD,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AA0EjD;;;GAGG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,wBAAwB,CAAA;AAEnD,sEAAsE;AACtE,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,IAAY,EAAwB,EAAE,CAC/D,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,IAAI,CAAA;AAE5D,4EAA4E;AAC5E,MAAM,SAAS,GAAG,EAAE,CAAA;AAEpB,MAAM,CAAC,MAAM,aAAa,GAAG;;;;+gBAIkf,CAAA;AAE/gB,uFAAuF;AACvF,MAAM,UAAU,aAAa,CAAC,QAAuB,EAAE,KAAK,GAAG,KAAK;IAClE,IAAI,KAAK,EAAE,CAAC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IAAI,CAAC;gBAAE,YAAY,CAAC,GAAG,CAAC,CAAA;QAC9C,CAAC;QACD,MAAM,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,CAAA;QAC9B,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,CAAA;IACpC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,aAAa,EAAE,CAAC;YACnC,IAAI,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,IAAI,YAAY,KAAK,CAAC,OAAO,0CAA0C,CAAC,CAAA;YAC9F,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,CAAA;QAC7C,CAAC;QACD,MAAM,KAAK,CAAA;IACb,CAAC;YAAS,CAAC;QACT,oFAAoF;QACpF,QAAQ,CAAC,IAAI,EAAE,CAAA;IACjB,CAAC;AACH,CAAC"}
@@ -0,0 +1,69 @@
1
+ import { type ComputeOffers, type JobOffer, type JobTool, type QuoteRequest, type SessionEvent, type ToolLimits } from '@avow-finance/spec';
2
+ import type { ServicePolicy } from './service.js';
3
+ import type { JobWorld, WorldBooking } from './world.js';
4
+ /**
5
+ * The tool layer: it reads a model's tool call, applies the developer's policy from `service.ts`,
6
+ * calls the world, and writes every result in code. The model sees only what this returns, which is
7
+ * public data, and every event it emits says what happened, apart from anything the model said.
8
+ */
9
+ /** Where a tool call runs: its run, the run's order, and the call's number in the run. */
10
+ export interface CallContext {
11
+ run: number;
12
+ orderId: string | null;
13
+ call: number;
14
+ }
15
+ /** What a tool returned to the model, and whether it succeeded. */
16
+ export interface ToolOutcome {
17
+ ok: boolean;
18
+ /** What the model reads, as JSON text. */
19
+ content: string;
20
+ }
21
+ /** A result the session recorded, so it can show it again as evidence beside an answer. */
22
+ export interface Recorded {
23
+ run: number;
24
+ call: number;
25
+ tool: JobTool;
26
+ event: Extract<SessionEvent, {
27
+ type: 'tool_result';
28
+ }>;
29
+ /** The offer a booking call named, or null for any other result. */
30
+ offer: string | null;
31
+ }
32
+ /** What the session keeps between tool calls. */
33
+ export interface SessionState {
34
+ /** The order the current job is for, or the last one. */
35
+ order: JobOffer | null;
36
+ /** The latest listing the model saw, with the call that listed it. */
37
+ listing: {
38
+ run: number;
39
+ call: number;
40
+ listing: ComputeOffers;
41
+ limits: ToolLimits;
42
+ } | null;
43
+ /** Every compute booking the session made, by its id. */
44
+ bookings: Map<string, WorldBooking>;
45
+ /** Each order's quote once it went out, with the booking it named. */
46
+ quotes: Map<string, {
47
+ quote: QuoteRequest;
48
+ booking: WorldBooking | null;
49
+ run: number;
50
+ }>;
51
+ /** The results of compute_offers and book_compute, for the evidence beside an answer. */
52
+ recorded: Recorded[];
53
+ }
54
+ export declare function newSessionState(): SessionState;
55
+ export declare class Toolbox {
56
+ #private;
57
+ private readonly world;
58
+ private readonly policy;
59
+ private readonly emit;
60
+ readonly state: SessionState;
61
+ /** Reads service.ts again before a booking, so an edit made mid-turn applies to it. */
62
+ private readonly beforeBooking;
63
+ constructor(world: JobWorld, policy: () => ServicePolicy, emit: (event: SessionEvent) => void, state: SessionState,
64
+ /** Reads service.ts again before a booking, so an edit made mid-turn applies to it. */
65
+ beforeBooking?: () => Promise<void>);
66
+ /** Runs one tool call the model made, and gives the model what it returned. */
67
+ run(tool: JobTool, input: unknown, ctx: CallContext): Promise<ToolOutcome>;
68
+ }
69
+ //# sourceMappingURL=toolbox.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toolbox.d.ts","sourceRoot":"","sources":["../../src/session/toolbox.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,aAAa,EAIlB,KAAK,QAAQ,EACb,KAAK,OAAO,EAGZ,KAAK,YAAY,EAKjB,KAAK,YAAY,EACjB,KAAK,UAAU,EAChB,MAAM,oBAAoB,CAAA;AAE3B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAEjD,OAAO,KAAK,EAAgB,QAAQ,EAAE,YAAY,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