package main // Exhibit 15. ai keeps a ledger of every call. Any app can read it back for the signed-in person: // call_app("ai", "/?api=usage", true) -> {user: {id, spent, by_app}, breakdown: {"24h": [...], "7d": [...]}} // breakdown rows are {app, user, calls, tokens, in, out}. There are no budgets (PLAN belief 7): this // exhibit shows what was spent, not what is left. import "core:fmt" import "ratcloud:rc" App_Spend :: struct { today: i64 `json:"today"`, week: i64 `json:"week"`, calls: i64 `json:"calls"`, } Last_Call :: struct { id: i64 `json:"id"`, at: i64 `json:"at"`, tokens: i64 `json:"tokens"`, } Spend :: struct { today: i64 `json:"today"`, apps: map[string]App_Spend `json:"apps"`, week: i64 `json:"week"`, last: Last_Call `json:"last"`, // the zoo's newest call for this person, on ai's ledger } spend_by_app :: proc() -> (Spend, string) { a := call_app("ai", "/?api=usage", true, timeout_ms = 20_000) r := parse(a.body) if a.status != 200 || !jhas(r, "user", "id") || !jhas(r, "breakdown") do return {}, fmt.tprintf("ai's usage answered %d%s", a.status, cat(": ", a.err) if a.err != "" else "") uid := jint(r, "user", "id") apps := make(map[string]App_Spend) for row in jarr(r, "breakdown", "24h") { if jint(row, "user") != uid do continue // the person's rows only, not the zoo's other askers x := apps[jstr(row, "app")] x.today = jint(row, "tokens") apps[jstr(row, "app")] = x } week: i64 = 0 for row in jarr(r, "breakdown", "7d") { if jint(row, "user") != uid do continue x := apps[jstr(row, "app")] x.week, x.calls = jint(row, "tokens"), jint(row, "calls") apps[jstr(row, "app")] = x week += x.week } return {jint(r, "user", "spent"), apps, week, {jint(r, "last", "id"), jint(r, "last", "at"), jint(r, "last", "tokens")}}, "" } e15 :: proc() -> Exhibit { return { n = 15, wing = "Intelligence", title = "You can see what you spent", promise = "Every question through ai is on your ledger, per app.", block = "ai writes every call to a ledger; GET ?api=usage tells an app what the signed-in person spent today and this week, by app.", src = {"e15_spend.odin", #load("e15_spend.odin", string)}, script = {"web/e15.js", #load("web/e15.js", string)}, show = proc(me: ^rc.User) -> string { if me == nil do return signin_line("to see what you spent.") return `
You spent … tokens today and … this week. Ask the model above and watch it rise.
` }, api = proc(a: ^Api) -> bool { if a.do_ != "usage" do return false if a.me == nil {reply_err(a, 401, "sign in first"); return true} s, err := spend_by_app() if err != "" {reply_err(a, 502, err); return true} reply(a, s) return true }, // The robot's spend must go up when it asks. (Exhibit 14 asks too; this does its own ask so the // check never depends on the order of two checks: it calls 14's question, as the page does.) check = proc() -> Check { b, err := robot_at() if err != "" do return fail(err) before, after: Spend r1 := post(b, self_url("/api/15/usage")) if r1.status != 200 || !json_into(r1.body, &before) do return fail(fmt.tprintf("reading the spend failed (%d): %s", r1.status, jstr(r1.j, "error"))) b.timeout_ms = 30_000 r2 := post(b, self_url("/api/14/ask"), `{"question":"Reply with the single word: ok","max_tokens":20}`) b.timeout_ms = 15_000 if r2.status != 200 do return fail(fmt.tprintf("the robot's question failed (%d): %s", r2.status, jstr(r2.j, "error"))) r3 := post(b, self_url("/api/15/usage")) if r3.status != 200 || !json_into(r3.body, &after) do return fail(fmt.tprintf("reading the spend again failed (%d)", r3.status)) // Not the totals: they are over a rolling 24 h and 7 days, and a call from 24 h ago, as large, can // leave the window while this one lands (the checks run on the same minutes every day, so it // did, and this light was red). The ledger's newest call is the question just asked. ask: Answer json_into(r2.body, &ask) if after.last.id <= before.last.id do return fail(fmt.tprintf("asked, but the robot's newest call by the zoo on ai's ledger is still #%d", before.last.id), after) if after.last.tokens != ask.tokens do return fail(fmt.tprintf("the ledger's newest call (#%d) has %d tokens; the answer said %d", after.last.id, after.last.tokens, ask.tokens), after) was, now_ := before.apps["zoo"].today, after.apps["zoo"].today return pass(fmt.tprintf("one question (%d tokens) is on the robot's ledger by the zoo as call #%d; its spend by the zoo is %d tokens over 24 h (was %d), %d this week", ask.tokens, after.last.id, now_, was, after.week), after) }, } }