// @bun
import{Ak as L_,Bk as q_,Ck as D_,Ek as P_,Fk as w_,Gk as y_,Hk as E_,Jk as v_,lk as j_,mk as W_,nk as x_,qk as I_,rk as B_,tk as C_,vk as F_,wk as k_,xk as R_,yk as A_,zk as h_}from"./chunk-06zcccwx.js";import{Nk as z_,Pk as V_}from"./chunk-qysx0asq.js";import{cl as f,dl as c,el as n,fl as Y_,gl as Z_,il as G_}from"./chunk-13vg3e02.js";import{rl as J_}from"./chunk-tkx569sk.js";import{Dl as M_,zl as $_}from"./chunk-c5jsd4mw.js";import{Fl as N_,Wl as Q_}from"./chunk-k519zv4b.js";import{cm as __,dm as K_,em as U_,hm as X_}from"./chunk-wdapmeyv.js";import{Fm as H_,Gm as O_}from"./chunk-9k6sqsj1.js";import{Kn as i,Ln as r,On as o,Rn as t,Tn as a,zo as e}from"./chunk-2szzt9yn.js";import{Is as l,Kq as T,Mq as m,Oq as Y,Pq as B,Qq as L,Qs as Z,Rs as U,Tq as q,Xq as D,Yr as b,_q as P,aq as h,br as d,dr as F,dt as s,er as k,fr as R,jr as Q,lq as E,oq as v,or as S,pq as M,rs as u,sq as p,vs as g}from"./chunk-vcm8340w.js";import{pt as y}from"./chunk-g2rmdpb3.js";import{Cy as A,VA as G,bD as H}from"./chunk-bt9y3st3.js";import{NG as p_}from"./chunk-xyhgvt88.js";var j={};p_(j,{layer:()=>w,defaultLayer:()=>m_,Service:()=>x,DataMigration:()=>j});var N=R("data_migration",{name:k().primaryKey(),time_completed:F().notNull()});var T_=h({service:"data-migration"});class x extends A.Service()("@muxcode/DataMigration"){}var w=G.effect(x,H.gen(function*(){let O=[{name:"session_usage_from_messages",run:H.gen(function*(){for(let _,I=1;;I++){let $=yield*H.gen(function*(){let V=yield*H.sync(()=>Q.use((W)=>W.select({id:Z.id}).from(Z).where(_?q(Z.id,_):void 0).orderBy(P(Z.id)).limit(100).all()));if(V.length===0)return;return yield*H.sync(()=>Q.transaction((W)=>{let C=new Map(V.map((X)=>[X.id,{cost:0,tokens:{input:0,output:0,reasoning:0,cache:{read:0,write:0}}}]));for(let X of W.select({session_id:U.session_id,cost:Y`coalesce(sum(coalesce(json_extract(${U.data}, '$.cost'), 0)), 0)`,tokens_input:Y`coalesce(sum(coalesce(json_extract(${U.data}, '$.tokens.input'), 0)), 0)`,tokens_output:Y`coalesce(sum(coalesce(json_extract(${U.data}, '$.tokens.output'), 0)), 0)`,tokens_reasoning:Y`coalesce(sum(coalesce(json_extract(${U.data}, '$.tokens.reasoning'), 0)), 0)`,tokens_cache_read:Y`coalesce(sum(coalesce(json_extract(${U.data}, '$.tokens.cache.read'), 0)), 0)`,tokens_cache_write:Y`coalesce(sum(coalesce(json_extract(${U.data}, '$.tokens.cache.write'), 0)), 0)`}).from(U).where(L(D(U.session_id,V.map((K)=>K.id)),Y`json_extract(${U.data}, '$.role') = 'assistant'`)).groupBy(U.session_id).all()){let K=C.get(X.session_id);if(!K)continue;K.cost=X.cost,K.tokens.input=X.tokens_input,K.tokens.output=X.tokens_output,K.tokens.reasoning=X.tokens_reasoning,K.tokens.cache.read=X.tokens_cache_read,K.tokens.cache.write=X.tokens_cache_write}for(let[X,K]of C)W.update(Z).set({cost:K.cost,tokens_input:K.tokens.input,tokens_output:K.tokens.output,tokens_reasoning:K.tokens.reasoning,tokens_cache_read:K.tokens.cache.read,tokens_cache_write:K.tokens.cache.write,time_updated:Y`${Z.time_updated}`}).where(B(Z.id,X)).run()})),V.at(-1)?.id}).pipe(H.withSpan("DataMigration.sessionUsage.page",{attributes:{"data_migration.name":"session_usage_from_messages","data_migration.page":I,"data_migration.cursor":_??""}}));if(!$)return;_=$,yield*H.sleep("10 millis")}})}];return yield*H.gen(function*(){if(O.length===0)return;for(let _ of O){if(Q.use(($)=>$.select({name:N.name}).from(N).where(B(N.name,_.name)).get()))continue;T_.info("running data migration",{name:_.name}),yield*_.run.pipe(H.withSpan("DataMigration",{attributes:{name:_.name}})),Q.use(($)=>$.insert(N).values({name:_.name,time_completed:Date.now()}).onConflictDoNothing().run())}}).pipe(H.tapCause((_)=>H.logError("failed to run data migrations").pipe(H.annotateLogs("cause",_))),H.ignore,H.forkScoped),x.of({})})),m_=w;var d_=G.mergeAll(b.defaultLayer,p.defaultLayer,T.defaultLayer,m.defaultLayer,S.defaultLayer,g.defaultLayer,l.defaultLayer,f.defaultLayer,c.defaultLayer,n.defaultLayer,i.defaultLayer,r.defaultLayer,M_.defaultLayer,u.defaultLayer,$_.defaultLayer,x_.defaultLayer,V_.defaultLayer,O_.defaultLayer,H_.defaultLayer,C_.defaultLayer,s.defaultLayer,P_.defaultLayer,e.defaultLayer,k_.defaultLayer,o.defaultLayer,d.defaultLayer,L_.defaultLayer,A_.defaultLayer,h_.defaultLayer,q_.defaultLayer,R_.defaultLayer,y_.defaultLayer,D_.defaultLayer,F_.defaultLayer,a.defaultLayer,K_.defaultLayer,__.defaultLayer,U_.defaultLayer,z_.defaultLayer,w_.defaultLayer,Y_.defaultLayer,X_.defaultLayer,Z_.defaultLayer,J_.defaultLayer,v_.defaultLayer,Q_.appLayer,I_.defaultLayer,B_.defaultLayer,W_.defaultLayer,G_.defaultLayer,E_.defaultLayer,t.defaultLayer,j_.defaultLayer,j.defaultLayer).pipe(G.provideMerge(N_.layer),G.provideMerge(E)),J=y.make(d_,{memoMap:v}),z=(O)=>M(O),sK={runSync(O){return J.runSync(z(O))},runPromise(O,_){return J.runPromise(z(O),_)},runPromiseExit(O,_){return J.runPromiseExit(z(O),_)},runFork(O){return J.runFork(z(O))},runCallback(O){return J.runCallback(z(O))},dispose:()=>J.dispose()};
export{d_ as $j,sK as ak};
