@@ -92,17 +92,24 @@ export const getUsage = async ({
9292 requestedUserId : userId ,
9393 } ) ;
9494
95- if ( ! orgId ) {
96- return { } ;
95+ // For personal accounts, query by userId instead of orgId
96+ if ( ! orgId && ! effectiveUserId ) {
97+ return { } ; // Personal accounts must have a userId
9798 }
9899
99100 const userFilter = effectiveUserId ? 'AND userId = {userId: String}' : '' ;
100101
102+ // Build query conditions based on account type
103+ const orgCondition = ! orgId ? 'orgId IS NULL' : 'orgId = {orgId: String}' ;
104+
101105 const queryParams : Record < string , string | number > = {
102- orgId : orgId ! ,
103106 timePeriod,
104107 } ;
105108
109+ if ( orgId ) {
110+ queryParams . orgId = orgId ;
111+ }
112+
106113 if ( effectiveUserId ) {
107114 queryParams . userId = effectiveUserId ;
108115 }
@@ -117,7 +124,7 @@ export const getUsage = async ({
117124 SUM(COALESCE(cost, 0)) AS cost
118125 FROM events
119126 WHERE
120- orgId = {orgId: String }
127+ ${ orgCondition }
121128 AND timestamp >= toUnixTimestamp(now() - INTERVAL {timePeriod: Int32} DAY)
122129 ${ userFilter }
123130 GROUP BY 1
@@ -332,8 +339,10 @@ export const getTasks = async ({
332339 effectiveUserId = authResult . effectiveUserId ;
333340 }
334341
335- if ( ! orgId ) {
336- return [ ] ;
342+ // For personal accounts, query by userId instead of orgId
343+ // Exception: when skipAuth is true (for public shares), we can query without userId
344+ if ( ! orgId && ! effectiveUserId && ! skipAuth ) {
345+ return [ ] ; // Personal accounts must have a userId unless we're skipping auth
337346 }
338347
339348 const userFilter = effectiveUserId ? 'AND e.userId = {userId: String}' : '' ;
@@ -345,15 +354,25 @@ export const getTasks = async ({
345354
346355 const messageTaskFilter = taskId ? 'AND taskId = {taskId: String}' : '' ;
347356
357+ // Build query conditions based on account type
358+ const orgCondition = ! orgId ? 'e.orgId IS NULL' : 'e.orgId = {orgId: String}' ;
359+
360+ const messageOrgCondition = ! orgId
361+ ? 'orgId IS NULL'
362+ : 'orgId = {orgId: String}' ;
363+
348364 const queryParams : Record < string , string | string [ ] > = {
349- orgId : orgId ! ,
350365 types : [
351366 TelemetryEventName . TASK_CREATED ,
352367 TelemetryEventName . TASK_COMPLETED ,
353368 TelemetryEventName . LLM_COMPLETION ,
354369 ] ,
355370 } ;
356371
372+ if ( orgId ) {
373+ queryParams . orgId = orgId ;
374+ }
375+
357376 if ( effectiveUserId ) {
358377 queryParams . userId = effectiveUserId ;
359378 }
@@ -370,7 +389,7 @@ export const getTasks = async ({
370389 argMin(text, ts) as title,
371390 argMin(mode, ts) as mode
372391 FROM messages
373- WHERE orgId = {orgId: String }
392+ WHERE ${ messageOrgCondition }
374393 ${ messageUserFilter }
375394 ${ messageTaskFilter }
376395 GROUP BY taskId
@@ -389,7 +408,7 @@ export const getTasks = async ({
389408 FROM events e
390409 LEFT JOIN first_messages fm ON e.taskId = fm.taskId
391410 WHERE
392- e.orgId = {orgId: String }
411+ ${ orgCondition }
393412 AND e.type IN ({types: Array(String)})
394413 AND e.modelId IS NOT NULL
395414 ${ userFilter }
@@ -440,14 +459,17 @@ export const getHourlyUsageByUser = async ({
440459 requestedUserId : userId ,
441460 } ) ;
442461
443- if ( ! orgId ) {
444- return [ ] ;
462+ // For personal accounts, query by userId instead of orgId
463+ if ( ! orgId && ! effectiveUserId ) {
464+ return [ ] ; // Personal accounts must have a userId
445465 }
446466
447467 const userFilter = effectiveUserId ? 'AND userId = {userId: String}' : '' ;
448468
469+ // Build query conditions based on account type
470+ const orgCondition = ! orgId ? 'orgId IS NULL' : 'orgId = {orgId: String}' ;
471+
449472 const queryParams : Record < string , string | number | string [ ] > = {
450- orgId : orgId ! ,
451473 timePeriod,
452474 types : [
453475 TelemetryEventName . TASK_CREATED ,
@@ -456,6 +478,10 @@ export const getHourlyUsageByUser = async ({
456478 ] ,
457479 } ;
458480
481+ if ( orgId ) {
482+ queryParams . orgId = orgId ;
483+ }
484+
459485 if ( effectiveUserId ) {
460486 queryParams . userId = effectiveUserId ;
461487 }
@@ -470,7 +496,7 @@ export const getHourlyUsageByUser = async ({
470496 SUM(CASE WHEN type = '${ TelemetryEventName . LLM_COMPLETION } ' THEN COALESCE(cost, 0) ELSE 0 END) AS cost
471497 FROM events
472498 WHERE
473- orgId = {orgId: String }
499+ ${ orgCondition }
474500 AND timestamp >= toUnixTimestamp(now() - INTERVAL {timePeriod: Int32} DAY)
475501 AND type IN ({types: Array(String)})
476502 ${ userFilter }
0 commit comments