@sixb/sqlite 0.1.5 → 0.1.7
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.
- package/dist/agents/rows.d.ts +7 -4
- package/dist/agents/rows.d.ts.map +1 -1
- package/dist/agents/runs.d.ts +3 -2
- package/dist/agents/runs.d.ts.map +1 -1
- package/dist/agents/threads.d.ts.map +1 -1
- package/dist/ai-cost-groups.d.ts +5 -0
- package/dist/ai-cost-groups.d.ts.map +1 -0
- package/dist/ai-cost-storage.d.ts +2 -1
- package/dist/ai-cost-storage.d.ts.map +1 -1
- package/dist/ai-limit-storage.d.ts +35 -0
- package/dist/ai-limit-storage.d.ts.map +1 -0
- package/dist/ai-usage-storage.d.ts +1 -0
- package/dist/ai-usage-storage.d.ts.map +1 -1
- package/dist/auth-storage/access-tokens.d.ts +1 -1
- package/dist/auth-storage/access-tokens.d.ts.map +1 -1
- package/dist/auth-storage/auth-storage.d.ts +4 -1
- package/dist/auth-storage/auth-storage.d.ts.map +1 -1
- package/dist/auth-storage/device-authorizations.d.ts +29 -0
- package/dist/auth-storage/device-authorizations.d.ts.map +1 -0
- package/dist/auth-storage/rows.d.ts +19 -1
- package/dist/auth-storage/rows.d.ts.map +1 -1
- package/dist/auth-storage/shared.d.ts +1 -1
- package/dist/auth-storage/shared.d.ts.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1937 -387
- package/dist/index.js.map +20 -17
- package/dist/migrations.d.ts.map +1 -1
- package/dist/ontology-storage/materialization-state.d.ts +7 -3
- package/dist/ontology-storage/materialization-state.d.ts.map +1 -1
- package/dist/ontology-storage/materializations.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/agents/rows.ts +36 -9
- package/src/agents/runs.ts +152 -13
- package/src/agents/threads.ts +2 -17
- package/src/ai-cost-groups.ts +100 -0
- package/src/ai-cost-storage.ts +67 -25
- package/src/ai-limit-storage.ts +981 -0
- package/src/ai-usage-storage.ts +24 -1
- package/src/auth-storage/access-tokens.ts +77 -68
- package/src/auth-storage/auth-storage.ts +80 -1
- package/src/auth-storage/device-authorizations.ts +153 -0
- package/src/auth-storage/rows.ts +41 -0
- package/src/auth-storage/shared.ts +1 -0
- package/src/index.ts +6 -0
- package/src/migrations/029-model-accounting.sql +79 -0
- package/src/migrations/030-ai-usage-limits.sql +100 -0
- package/src/migrations/031-subagent-runs.sql +231 -0
- package/src/migrations/032-conversation-run-spec.sql +78 -0
- package/src/migrations/033-retire-agent-definitions.sql +89 -0
- package/src/migrations/034-device-authorizations.sql +22 -0
- package/src/migrations.ts +18 -0
- package/src/ontology-storage/materialization-state.ts +53 -33
- package/src/ontology-storage/materializations.ts +4 -5
- package/src/workflow-run-storage.ts +8 -8
package/dist/migrations.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"migrations.d.ts","sourceRoot":"","sources":["../src/migrations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAIrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AACjD,OAAO,KAAK,EAGV,YAAY,EACZ,aAAa,EACb,oBAAoB,EACrB,MAAM,oBAAoB,CAAA;
|
|
1
|
+
{"version":3,"file":"migrations.d.ts","sourceRoot":"","sources":["../src/migrations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAIrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AACjD,OAAO,KAAK,EAGV,YAAY,EACZ,aAAa,EACb,oBAAoB,EACrB,MAAM,oBAAoB,CAAA;AA6F3B,eAAO,MAAM,yBAAyB,sBAAsB,CAAA;AAC5D,eAAO,MAAM,mBAAmB,mBAAmB,CAAA;AAEnD,eAAO,MAAM,uBAAuB,wBAyElC,CAAA;AAEF,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAE1D;AAED,wBAAgB,SAAS,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,aAAa,CAAC,QAAQ,CAAC,CAQ9E;AAED,wBAAgB,UAAU,CACxB,EAAE,EAAE,MAAM,EACV,EAAE,EAAE,CAAC,EAAE,EAAE,QAAQ,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,EAC1C,OAAO,GAAE,oBAAyB,GACjC,aAAa,CAAC,QAAQ,CAAC,CAEzB;AAED,wBAAgB,wBAAwB,CAAC,EAAE,EAAE,QAAQ,GAAG,IAAI,CAS3D;AAED,wBAAgB,4BAA4B,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,eAAe,EAAE,CAOzF;AAED,wBAAgB,oBAAoB,CAAC,MAAM,EAAE;IAC3C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,UAAU,EAAE,YAAY,CAAC,QAAQ,CAAC,CAAA;CAC5C,GAAG,eAAe,CAoBlB;AAED,wBAAsB,oBAAoB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAM1E"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Database } from "bun:sqlite";
|
|
2
2
|
import type { OntologyLinkRef, OntologyObjectRef, TelemetrySeriesRef } from "@sixb/core/internal/materialization";
|
|
3
|
-
import type { MaterializationLinkScopeState, MaterializationLinkState, MaterializationObjectState, SourceReplacementLinkState, SourceReplacementObjectState, StoredTelemetryPoint } from "@sixb/core/storage";
|
|
3
|
+
import type { MaterializationLinkScopeRevision, MaterializationLinkScopeState, MaterializationLinkState, MaterializationObjectState, SourceReplacementLinkState, SourceReplacementObjectState, StoredTelemetryPoint } from "@sixb/core/storage";
|
|
4
4
|
export declare const SQLITE_MATERIALIZATION_WORK_TABLE = "ontology_materialization_work";
|
|
5
5
|
export declare const SQLITE_REPLACEMENT_WORK_TABLE = "ontology_replacement_work";
|
|
6
6
|
export type ReplacementIdentity = {
|
|
@@ -35,11 +35,15 @@ export declare class SqliteMaterializationStateReader {
|
|
|
35
35
|
readonly series: TelemetrySeriesRef;
|
|
36
36
|
readonly at: string;
|
|
37
37
|
}[]): readonly StoredTelemetryPoint[];
|
|
38
|
-
|
|
38
|
+
linkScopeRevisions(scopes: readonly {
|
|
39
|
+
readonly source: OntologyObjectRef;
|
|
40
|
+
readonly linkId: string;
|
|
41
|
+
}[]): readonly MaterializationLinkScopeRevision[];
|
|
42
|
+
linkSlotStates(scopes: readonly {
|
|
39
43
|
readonly source: OntologyObjectRef;
|
|
40
44
|
readonly linkId: string;
|
|
41
45
|
}[]): readonly MaterializationLinkScopeState[];
|
|
42
|
-
|
|
46
|
+
private readEffectiveLinkScopes;
|
|
43
47
|
incidentLinks(objects: readonly OntologyObjectRef[], pageRows: number): Iterable<OntologyLinkRef[]>;
|
|
44
48
|
replacementIdentities(input: ReplacementIdentityInput): Iterable<ReplacementIdentity[]>;
|
|
45
49
|
replacementObjectStates(sourceId: string, candidateMaterializationId: string, refs: readonly OntologyObjectRef[]): readonly SourceReplacementObjectState[];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"materialization-state.d.ts","sourceRoot":"","sources":["../../src/ontology-storage/materialization-state.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAC1C,OAAO,KAAK,EAGV,eAAe,EACf,iBAAiB,EAEjB,kBAAkB,EACnB,MAAM,qCAAqC,CAAA;AAe5C,OAAO,KAAK,EACV,6BAA6B,EAC7B,wBAAwB,EACxB,0BAA0B,EAC1B,0BAA0B,EAC1B,4BAA4B,EAO5B,oBAAoB,EACrB,MAAM,oBAAoB,CAAA;AAU3B,eAAO,MAAM,iCAAiC,kCAAkC,CAAA;AAChF,eAAO,MAAM,6BAA6B,8BAA8B,CAAA;
|
|
1
|
+
{"version":3,"file":"materialization-state.d.ts","sourceRoot":"","sources":["../../src/ontology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a,CAAC,0BAA0B,CAAC;IA0FtC,SAAS,CAAC,KAAK,EAAE,6BAA6B,GAAG,OAAO,CAAC,IAAI,CAAC;IAKpE,UAAU,CAAC,KAAK,EAAE,8BAA8B,GAAG,aAAa,CAAC,uBAAuB,CAAC;IAKnF,mBAAmB,CACvB,KAAK,EAAE,uCAAuC,GAC7C,OAAO,CAAC,SAAS,8BAA8B,EAAE,CAAC;IAK/C,UAAU,CAAC,KAAK,EAAE,8BAA8B,GAAG,OAAO,CAAC,IAAI,CAAC;IAkBhE,QAAQ,CAAC,KAAK,EAAE,4BAA4B,GAAG,OAAO,CAAC,0BAA0B,CAAC;IAYxF,kBAAkB,IAAI,IAAI;IAI1B,OAAO,CAAC,kBAAkB;IA2D1B,OAAO,CAAC,kBAAkB;IA+C1B,OAAO,CAAC,YAAY;IAgBpB,OAAO,CAAC,YAAY;IAsBpB,OAAO,CAAC,UAAU;YAsBJ,kBAAkB;IAwDhC,OAAO,CAAC,sBAAsB;IAwD9B,OAAO,CAAC,cAAc;IAqGtB,OAAO,CAAC,YAAY;IAmDpB,OAAO,CAAC,eAAe;IASvB,OAAO,CAAC,SAAS;CAYlB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sixb/sqlite",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "SQLite storage provider for Sixb",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sixb",
|
|
@@ -36,10 +36,10 @@
|
|
|
36
36
|
"prepack": "bun run build"
|
|
37
37
|
},
|
|
38
38
|
"peerDependencies": {
|
|
39
|
-
"@sixb/core": "0.1.
|
|
39
|
+
"@sixb/core": "0.1.7"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@sixb/core": "0.1.
|
|
42
|
+
"@sixb/core": "0.1.7",
|
|
43
43
|
"@types/bun": "latest",
|
|
44
44
|
"typescript": "^5.7.0"
|
|
45
45
|
},
|
package/src/agents/rows.ts
CHANGED
|
@@ -8,7 +8,10 @@ import {
|
|
|
8
8
|
type AgentRunDiagnostic,
|
|
9
9
|
type AgentRunRecord,
|
|
10
10
|
type AgentThreadRecord,
|
|
11
|
+
type ConversationAgentRunSpec,
|
|
11
12
|
coerceAgentRunFinishReason,
|
|
13
|
+
type SubagentRunResult,
|
|
14
|
+
type SubagentRunSpec,
|
|
12
15
|
} from "@sixb/core/storage"
|
|
13
16
|
import type { SqliteValue } from "../run-list-query"
|
|
14
17
|
|
|
@@ -17,7 +20,6 @@ import type { SqliteValue } from "../run-list-query"
|
|
|
17
20
|
export interface AgentThreadRow {
|
|
18
21
|
project_id: string
|
|
19
22
|
id: string
|
|
20
|
-
agent_id: string
|
|
21
23
|
owner_principal_type: Principal["type"]
|
|
22
24
|
owner_principal_id: string
|
|
23
25
|
title: string | null
|
|
@@ -33,9 +35,13 @@ export interface AgentRunRow {
|
|
|
33
35
|
project_id: string
|
|
34
36
|
id: string
|
|
35
37
|
execution_id: string
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
trigger_message_id: string
|
|
38
|
+
kind: AgentRunRecord["kind"]
|
|
39
|
+
thread_id: string | null
|
|
40
|
+
trigger_message_id: string | null
|
|
41
|
+
parent_run_id: string | null
|
|
42
|
+
spawn_key: string | null
|
|
43
|
+
spec: string | null
|
|
44
|
+
result: string | null
|
|
39
45
|
requester_group_ids: string
|
|
40
46
|
status: AgentRunRecord["status"]
|
|
41
47
|
model_id: string | null
|
|
@@ -89,7 +95,6 @@ export function rowToThreadRecord(row: AgentThreadRow): AgentThreadRecord {
|
|
|
89
95
|
return {
|
|
90
96
|
id: row.id,
|
|
91
97
|
projectId: row.project_id,
|
|
92
|
-
agentId: row.agent_id,
|
|
93
98
|
ownerPrincipal: { type: row.owner_principal_type, id: row.owner_principal_id },
|
|
94
99
|
title: row.title ?? undefined,
|
|
95
100
|
status: row.status,
|
|
@@ -102,13 +107,10 @@ export function rowToThreadRecord(row: AgentThreadRow): AgentThreadRecord {
|
|
|
102
107
|
}
|
|
103
108
|
|
|
104
109
|
export function rowToRunRecord(row: AgentRunRow): AgentRunRecord {
|
|
105
|
-
|
|
110
|
+
const base = {
|
|
106
111
|
id: row.id,
|
|
107
112
|
projectId: row.project_id,
|
|
108
113
|
executionId: row.execution_id,
|
|
109
|
-
threadId: row.thread_id,
|
|
110
|
-
agentId: row.agent_id,
|
|
111
|
-
triggerMessageId: row.trigger_message_id,
|
|
112
114
|
requesterGroupIds: JSON.parse(row.requester_group_ids) as string[],
|
|
113
115
|
status: row.status,
|
|
114
116
|
modelId: row.model_id ?? undefined,
|
|
@@ -128,6 +130,31 @@ export function rowToRunRecord(row: AgentRunRow): AgentRunRecord {
|
|
|
128
130
|
startedAt: row.started_at ? new Date(row.started_at) : undefined,
|
|
129
131
|
completedAt: row.completed_at ? new Date(row.completed_at) : undefined,
|
|
130
132
|
}
|
|
133
|
+
if (row.kind === "conversation") {
|
|
134
|
+
if (!row.thread_id || !row.trigger_message_id) {
|
|
135
|
+
throw new Error(
|
|
136
|
+
`[SixbSqlite] Conversational Agent run '${row.id}' has invalid persisted fields.`
|
|
137
|
+
)
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
...base,
|
|
141
|
+
kind: "conversation",
|
|
142
|
+
threadId: row.thread_id,
|
|
143
|
+
triggerMessageId: row.trigger_message_id,
|
|
144
|
+
...(row.spec === null ? {} : { spec: JSON.parse(row.spec) as ConversationAgentRunSpec }),
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (!row.parent_run_id || !row.spawn_key || row.spec === null) {
|
|
148
|
+
throw new Error(`[SixbSqlite] Subagent run '${row.id}' has invalid persisted fields.`)
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
...base,
|
|
152
|
+
kind: "subagent",
|
|
153
|
+
parentRunId: row.parent_run_id,
|
|
154
|
+
spawnKey: row.spawn_key,
|
|
155
|
+
spec: JSON.parse(row.spec) as SubagentRunSpec,
|
|
156
|
+
...(row.result === null ? {} : { result: JSON.parse(row.result) as SubagentRunResult }),
|
|
157
|
+
}
|
|
131
158
|
}
|
|
132
159
|
|
|
133
160
|
export function rowToMessageRecord(row: AgentMessageRow): AgentMessageRecord {
|
package/src/agents/runs.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import type { Database } from "bun:sqlite"
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
assertAgentRunExecution,
|
|
4
|
+
assertConversationAgentRunSpec,
|
|
5
|
+
assertCreateSubagentRunInput,
|
|
6
|
+
assertSubagentRunResult,
|
|
7
|
+
subagentRunMatchesCreateInput,
|
|
8
|
+
} from "@sixb/core/internal/agent-run-storage-provider"
|
|
3
9
|
import { normalizeRequesterGroupIds } from "@sixb/core/internal/auth"
|
|
4
10
|
import { serializeSixbFailure } from "@sixb/core/internal/errors"
|
|
5
11
|
import {
|
|
@@ -8,7 +14,9 @@ import {
|
|
|
8
14
|
type AgentRunStore,
|
|
9
15
|
AgentStorageError,
|
|
10
16
|
type ConfirmAgentRunExecutionOwnershipInput,
|
|
17
|
+
type ConversationAgentRunRecord,
|
|
11
18
|
type CreateAgentRunInput,
|
|
19
|
+
type CreateSubagentRunInput,
|
|
12
20
|
type ExecutionStorage,
|
|
13
21
|
type FinishAgentRunInput,
|
|
14
22
|
type FinishQueuedAgentRunInput,
|
|
@@ -16,6 +24,7 @@ import {
|
|
|
16
24
|
type ListAgentRunsResult,
|
|
17
25
|
type ReclaimAgentRunInput,
|
|
18
26
|
type StartAgentRunInput,
|
|
27
|
+
type SubagentRunRecord,
|
|
19
28
|
} from "@sixb/core/storage"
|
|
20
29
|
import {
|
|
21
30
|
appendRunListFilters,
|
|
@@ -34,14 +43,14 @@ export class SqliteAgentRunStore implements AgentRunStore {
|
|
|
34
43
|
private readonly executions: ExecutionStorage
|
|
35
44
|
) {}
|
|
36
45
|
|
|
37
|
-
async create(input: CreateAgentRunInput): Promise<
|
|
46
|
+
async create(input: CreateAgentRunInput): Promise<ConversationAgentRunRecord> {
|
|
47
|
+
assertConversationAgentRunSpec(input.spec, "SixbSqlite")
|
|
38
48
|
const createdAt = input.createdAt ?? new Date()
|
|
39
49
|
await assertAgentRunExecution({
|
|
40
50
|
executions: this.executions,
|
|
41
51
|
projectId: input.projectId,
|
|
42
52
|
executionId: input.executionId,
|
|
43
53
|
runId: input.id,
|
|
44
|
-
agentId: input.agentId,
|
|
45
54
|
})
|
|
46
55
|
|
|
47
56
|
return this.db.transaction(() => {
|
|
@@ -55,7 +64,6 @@ export class SqliteAgentRunStore implements AgentRunStore {
|
|
|
55
64
|
`[SixbSqlite] Agent thread '${input.threadId}' not found for project '${input.projectId}'.`
|
|
56
65
|
)
|
|
57
66
|
}
|
|
58
|
-
|
|
59
67
|
if (thread.active_run_id !== null) {
|
|
60
68
|
throw new AgentStorageError(
|
|
61
69
|
"active_run_exists",
|
|
@@ -71,14 +79,15 @@ export class SqliteAgentRunStore implements AgentRunStore {
|
|
|
71
79
|
project_id,
|
|
72
80
|
id,
|
|
73
81
|
execution_id,
|
|
82
|
+
kind,
|
|
74
83
|
thread_id,
|
|
75
|
-
agent_id,
|
|
76
84
|
trigger_message_id,
|
|
85
|
+
spec,
|
|
77
86
|
requester_group_ids,
|
|
78
87
|
status,
|
|
79
88
|
attempt,
|
|
80
89
|
created_at
|
|
81
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, 'queued', 0, ?)
|
|
90
|
+
) VALUES (?, ?, ?, 'conversation', ?, ?, ?, ?, 'queued', 0, ?)
|
|
82
91
|
`
|
|
83
92
|
)
|
|
84
93
|
.run(
|
|
@@ -86,8 +95,8 @@ export class SqliteAgentRunStore implements AgentRunStore {
|
|
|
86
95
|
input.id,
|
|
87
96
|
input.executionId,
|
|
88
97
|
input.threadId,
|
|
89
|
-
input.agentId,
|
|
90
98
|
input.triggerMessageId,
|
|
99
|
+
JSON.stringify(input.spec),
|
|
91
100
|
JSON.stringify(normalizeRequesterGroupIds(input.requesterGroupIds)),
|
|
92
101
|
createdAt.toISOString()
|
|
93
102
|
)
|
|
@@ -108,7 +117,115 @@ export class SqliteAgentRunStore implements AgentRunStore {
|
|
|
108
117
|
)
|
|
109
118
|
.run(input.id, createdAt.toISOString(), input.projectId, input.threadId)
|
|
110
119
|
|
|
111
|
-
|
|
120
|
+
const created = this.requireRun(input.projectId, input.id)
|
|
121
|
+
if (created.kind !== "conversation") {
|
|
122
|
+
throw new Error(`[SixbSqlite] Agent run insert '${input.id}' returned the wrong run kind.`)
|
|
123
|
+
}
|
|
124
|
+
return created
|
|
125
|
+
})()
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async createSubagent(input: CreateSubagentRunInput): Promise<SubagentRunRecord> {
|
|
129
|
+
assertCreateSubagentRunInput(input, "SixbSqlite")
|
|
130
|
+
const execution = await assertAgentRunExecution({
|
|
131
|
+
executions: this.executions,
|
|
132
|
+
projectId: input.projectId,
|
|
133
|
+
executionId: input.executionId,
|
|
134
|
+
runId: input.id,
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
return this.db.transaction(() => {
|
|
138
|
+
const parentRow = this.db
|
|
139
|
+
.query("SELECT * FROM agent_runs WHERE project_id = ? AND id = ?")
|
|
140
|
+
.get(input.projectId, input.parentRunId) as AgentRunRow | null
|
|
141
|
+
if (!parentRow || parentRow.kind !== "conversation") {
|
|
142
|
+
throw new AgentStorageError(
|
|
143
|
+
"run_not_found",
|
|
144
|
+
`[SixbSqlite] Parent Agent run '${input.parentRunId}' was not found.`
|
|
145
|
+
)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const existingRow = this.db
|
|
149
|
+
.query("SELECT * FROM agent_runs WHERE project_id = ? AND id = ?")
|
|
150
|
+
.get(input.projectId, input.id) as AgentRunRow | null
|
|
151
|
+
if (existingRow) {
|
|
152
|
+
const existing = rowToRunRecord(existingRow)
|
|
153
|
+
if (subagentRunMatchesCreateInput(existing, input)) return existing
|
|
154
|
+
throw new AgentStorageError(
|
|
155
|
+
"duplicate_id",
|
|
156
|
+
`[SixbSqlite] Subagent run '${input.id}' already exists with different immutable inputs.`
|
|
157
|
+
)
|
|
158
|
+
}
|
|
159
|
+
if (parentRow.status !== "running") {
|
|
160
|
+
throw new AgentStorageError(
|
|
161
|
+
"invalid_state",
|
|
162
|
+
`[SixbSqlite] Parent Agent run '${parentRow.id}' is not running (status '${parentRow.status}').`
|
|
163
|
+
)
|
|
164
|
+
}
|
|
165
|
+
if (
|
|
166
|
+
execution.source.type !== "execution" ||
|
|
167
|
+
execution.source.executionId !== parentRow.execution_id
|
|
168
|
+
) {
|
|
169
|
+
throw new AgentStorageError(
|
|
170
|
+
"invalid_input",
|
|
171
|
+
`[SixbSqlite] Subagent execution '${execution.id}' is not a child of parent execution '${parentRow.execution_id}'.`
|
|
172
|
+
)
|
|
173
|
+
}
|
|
174
|
+
if (parentRow.execution_token !== input.parentExecutionToken) {
|
|
175
|
+
throw new AgentStorageError(
|
|
176
|
+
"execution_lost",
|
|
177
|
+
`[SixbSqlite] Parent Agent run '${parentRow.id}' is no longer owned by this execution.`
|
|
178
|
+
)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const count = this.db
|
|
182
|
+
.query(
|
|
183
|
+
`SELECT COUNT(*) AS count FROM agent_runs
|
|
184
|
+
WHERE project_id = ? AND parent_run_id = ? AND status IN ('queued', 'running')`
|
|
185
|
+
)
|
|
186
|
+
.get(input.projectId, input.parentRunId) as { count: number }
|
|
187
|
+
if (count.count >= input.maxActiveChildren) {
|
|
188
|
+
throw new AgentStorageError(
|
|
189
|
+
"active_child_limit",
|
|
190
|
+
`[SixbSqlite] Agent run '${parentRow.id}' already has ${count.count} active subagents.`
|
|
191
|
+
)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
try {
|
|
195
|
+
this.db
|
|
196
|
+
.query(
|
|
197
|
+
`
|
|
198
|
+
INSERT INTO agent_runs (
|
|
199
|
+
project_id, id, execution_id, kind, parent_run_id, spawn_key, spec,
|
|
200
|
+
requester_group_ids, status, attempt, created_at
|
|
201
|
+
) VALUES (?, ?, ?, 'subagent', ?, ?, ?, ?, 'queued', 0, ?)
|
|
202
|
+
`
|
|
203
|
+
)
|
|
204
|
+
.run(
|
|
205
|
+
input.projectId,
|
|
206
|
+
input.id,
|
|
207
|
+
input.executionId,
|
|
208
|
+
input.parentRunId,
|
|
209
|
+
input.spawnKey,
|
|
210
|
+
JSON.stringify(input.spec),
|
|
211
|
+
parentRow.requester_group_ids,
|
|
212
|
+
(input.createdAt ?? new Date()).toISOString()
|
|
213
|
+
)
|
|
214
|
+
} catch (error) {
|
|
215
|
+
if (isUniqueConstraintError(error)) {
|
|
216
|
+
throw new AgentStorageError(
|
|
217
|
+
"duplicate_id",
|
|
218
|
+
`[SixbSqlite] Subagent run '${input.id}' conflicts with an existing durable spawn.`
|
|
219
|
+
)
|
|
220
|
+
}
|
|
221
|
+
throw error
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const created = this.requireRun(input.projectId, input.id)
|
|
225
|
+
if (created.kind !== "subagent") {
|
|
226
|
+
throw new Error(`[SixbSqlite] Subagent insert '${input.id}' returned the wrong run kind.`)
|
|
227
|
+
}
|
|
228
|
+
return created
|
|
112
229
|
})()
|
|
113
230
|
}
|
|
114
231
|
|
|
@@ -205,7 +322,6 @@ export class SqliteAgentRunStore implements AgentRunStore {
|
|
|
205
322
|
`[SixbSqlite] Execution token is no longer current on agent run '${input.id}'.`
|
|
206
323
|
)
|
|
207
324
|
}
|
|
208
|
-
|
|
209
325
|
this.db
|
|
210
326
|
.query(
|
|
211
327
|
`
|
|
@@ -235,6 +351,19 @@ export class SqliteAgentRunStore implements AgentRunStore {
|
|
|
235
351
|
`[SixbSqlite] Execution token is no longer current on agent run '${input.id}'.`
|
|
236
352
|
)
|
|
237
353
|
}
|
|
354
|
+
if (input.status === "succeeded" && run.kind === "subagent") {
|
|
355
|
+
assertSubagentRunResult(input.result, run.id, "SixbSqlite")
|
|
356
|
+
}
|
|
357
|
+
if (
|
|
358
|
+
input.status === "succeeded" &&
|
|
359
|
+
run.kind === "conversation" &&
|
|
360
|
+
input.result !== undefined
|
|
361
|
+
) {
|
|
362
|
+
throw new AgentStorageError(
|
|
363
|
+
"invalid_input",
|
|
364
|
+
`[SixbSqlite] Conversational Agent run '${run.id}' cannot persist a subagent result.`
|
|
365
|
+
)
|
|
366
|
+
}
|
|
238
367
|
|
|
239
368
|
this.db
|
|
240
369
|
.query(
|
|
@@ -246,6 +375,7 @@ export class SqliteAgentRunStore implements AgentRunStore {
|
|
|
246
375
|
finish_reason = ?,
|
|
247
376
|
error = ?,
|
|
248
377
|
diagnostics = ?,
|
|
378
|
+
result = ?,
|
|
249
379
|
execution_token = NULL,
|
|
250
380
|
execution_queue_lease_expires_at = NULL,
|
|
251
381
|
completed_at = ?
|
|
@@ -258,6 +388,9 @@ export class SqliteAgentRunStore implements AgentRunStore {
|
|
|
258
388
|
input.finishReason ?? null,
|
|
259
389
|
errorValue,
|
|
260
390
|
input.diagnostics === undefined ? null : JSON.stringify(input.diagnostics),
|
|
391
|
+
input.status === "succeeded" && input.result !== undefined
|
|
392
|
+
? JSON.stringify(input.result)
|
|
393
|
+
: null,
|
|
261
394
|
completedAt.toISOString(),
|
|
262
395
|
input.projectId,
|
|
263
396
|
input.id
|
|
@@ -304,21 +437,26 @@ export class SqliteAgentRunStore implements AgentRunStore {
|
|
|
304
437
|
}
|
|
305
438
|
|
|
306
439
|
async list(input: ListAgentRunsInput): Promise<ListAgentRunsResult> {
|
|
307
|
-
if (hasEmptyStatuses(input)) {
|
|
440
|
+
if (hasEmptyStatuses(input) || input.kinds?.length === 0) {
|
|
308
441
|
return { runs: [], hasMore: false, total: 0 }
|
|
309
442
|
}
|
|
310
443
|
|
|
311
444
|
const whereClauses = ["project_id = ?"]
|
|
312
445
|
const args: SqliteValue[] = [input.projectId]
|
|
313
446
|
|
|
447
|
+
if (input.kinds) {
|
|
448
|
+
whereClauses.push(`kind IN (${input.kinds.map(() => "?").join(", ")})`)
|
|
449
|
+
args.push(...input.kinds)
|
|
450
|
+
}
|
|
451
|
+
|
|
314
452
|
if (input.threadId) {
|
|
315
453
|
whereClauses.push("thread_id = ?")
|
|
316
454
|
args.push(input.threadId)
|
|
317
455
|
}
|
|
318
456
|
|
|
319
|
-
if (input.
|
|
320
|
-
whereClauses.push("
|
|
321
|
-
args.push(input.
|
|
457
|
+
if (input.parentRunId) {
|
|
458
|
+
whereClauses.push("parent_run_id = ?")
|
|
459
|
+
args.push(input.parentRunId)
|
|
322
460
|
}
|
|
323
461
|
|
|
324
462
|
appendRunListFilters(whereClauses, args, input, "COALESCE(started_at, created_at)")
|
|
@@ -367,6 +505,7 @@ export class SqliteAgentRunStore implements AgentRunStore {
|
|
|
367
505
|
}
|
|
368
506
|
|
|
369
507
|
private releaseThread(run: AgentRunRow, completedAt: Date): void {
|
|
508
|
+
if (run.kind !== "conversation") return
|
|
370
509
|
this.db
|
|
371
510
|
.query(
|
|
372
511
|
`
|
package/src/agents/threads.ts
CHANGED
|
@@ -25,7 +25,6 @@ export class SqliteAgentThreadStore implements AgentThreadStore {
|
|
|
25
25
|
INSERT INTO agent_threads (
|
|
26
26
|
project_id,
|
|
27
27
|
id,
|
|
28
|
-
agent_id,
|
|
29
28
|
owner_principal_type,
|
|
30
29
|
owner_principal_id,
|
|
31
30
|
title,
|
|
@@ -35,13 +34,12 @@ export class SqliteAgentThreadStore implements AgentThreadStore {
|
|
|
35
34
|
message_count,
|
|
36
35
|
created_at,
|
|
37
36
|
updated_at
|
|
38
|
-
) VALUES (?, ?, ?, ?, ?, ?,
|
|
37
|
+
) VALUES (?, ?, ?, ?, ?, ?, NULL, NULL, 0, ?, ?)
|
|
39
38
|
`
|
|
40
39
|
)
|
|
41
40
|
.run(
|
|
42
41
|
input.projectId,
|
|
43
42
|
input.id,
|
|
44
|
-
input.agentId,
|
|
45
43
|
input.ownerPrincipal.type,
|
|
46
44
|
input.ownerPrincipal.id,
|
|
47
45
|
input.title ?? null,
|
|
@@ -72,26 +70,13 @@ export class SqliteAgentThreadStore implements AgentThreadStore {
|
|
|
72
70
|
}
|
|
73
71
|
|
|
74
72
|
async list(input: ListAgentThreadsInput): Promise<ListAgentThreadsResult> {
|
|
75
|
-
if (
|
|
76
|
-
(input.statuses !== undefined && input.statuses.length === 0) ||
|
|
77
|
-
(input.agentIds !== undefined && input.agentIds.length === 0)
|
|
78
|
-
) {
|
|
73
|
+
if (input.statuses !== undefined && input.statuses.length === 0) {
|
|
79
74
|
return { threads: [], hasMore: false, total: 0 }
|
|
80
75
|
}
|
|
81
76
|
|
|
82
77
|
const whereClauses = ["project_id = ?"]
|
|
83
78
|
const args: SqliteValue[] = [input.projectId]
|
|
84
79
|
|
|
85
|
-
if (input.agentId) {
|
|
86
|
-
whereClauses.push("agent_id = ?")
|
|
87
|
-
args.push(input.agentId)
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
if (input.agentIds) {
|
|
91
|
-
whereClauses.push(`agent_id IN (${input.agentIds.map(() => "?").join(", ")})`)
|
|
92
|
-
args.push(...input.agentIds)
|
|
93
|
-
}
|
|
94
|
-
|
|
95
80
|
if (input.statuses) {
|
|
96
81
|
whereClauses.push(`status IN (${input.statuses.map(() => "?").join(", ")})`)
|
|
97
82
|
args.push(...input.statuses)
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type AiModelCallGroupRow,
|
|
3
|
+
aiModelCallGroupFragmentFromRow,
|
|
4
|
+
buildAiModelCallGroupsFromFragments,
|
|
5
|
+
normalizeAiModelCallAccountingQuery,
|
|
6
|
+
} from "@sixb/core/internal/ai-cost-storage-provider"
|
|
7
|
+
import type { ListAiModelCallGroupsInput, ListAiModelCallGroupsResult } from "@sixb/core/storage"
|
|
8
|
+
import type { SqliteStoreConnection } from "./transactions"
|
|
9
|
+
|
|
10
|
+
/** Paginate initiating executions, then aggregate their matching calls without loading usage payloads. */
|
|
11
|
+
export async function listAiModelCallGroups(
|
|
12
|
+
connection: SqliteStoreConnection,
|
|
13
|
+
input: ListAiModelCallGroupsInput
|
|
14
|
+
): Promise<ListAiModelCallGroupsResult> {
|
|
15
|
+
const query = normalizeAiModelCallAccountingQuery(input)
|
|
16
|
+
const parameters = [
|
|
17
|
+
query.projectId,
|
|
18
|
+
query.from.toISOString(),
|
|
19
|
+
query.to.toISOString(),
|
|
20
|
+
query.providerId ?? null,
|
|
21
|
+
query.modelId ?? null,
|
|
22
|
+
query.valuationStatus ?? null,
|
|
23
|
+
]
|
|
24
|
+
const count = connection.db.query(COUNT_SQL).get(...parameters) as { total: string }
|
|
25
|
+
const rows = connection.db
|
|
26
|
+
.query(PAGE_SQL)
|
|
27
|
+
.all(...parameters, query.limit, query.offset) as AiModelCallGroupRow[]
|
|
28
|
+
const total = Number(count?.total)
|
|
29
|
+
if (!Number.isSafeInteger(total) || total < 0)
|
|
30
|
+
throw new RangeError("[Sixb] Invalid model-call group count.")
|
|
31
|
+
const items = buildAiModelCallGroupsFromFragments(rows.map(aiModelCallGroupFragmentFromRow))
|
|
32
|
+
return { items, total, hasMore: query.offset + items.length < total }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const FILTERED_SQL = `
|
|
36
|
+
WITH filtered AS (
|
|
37
|
+
SELECT COALESCE(root_agent.execution_id, usage.execution_id) AS root_execution_id,
|
|
38
|
+
usage.execution_id, usage.provider_id, usage.requested_model_id, usage.occurred_at,
|
|
39
|
+
usage.total_tokens,
|
|
40
|
+
COALESCE(cost.status, 'unvalued') AS valuation_status,
|
|
41
|
+
cost.currency AS amount_currency, cost.amount_nanos,
|
|
42
|
+
root_agent.id AS root_agent_run_id,
|
|
43
|
+
root_agent.thread_id AS root_thread_id,
|
|
44
|
+
direct_agent.kind AS direct_kind,
|
|
45
|
+
direct_agent.id AS direct_run_id,
|
|
46
|
+
direct_agent.parent_run_id AS parent_run_id,
|
|
47
|
+
workflow_node.node_id AS workflow_agent_step_id,
|
|
48
|
+
workflow_agent.node_run_id AS workflow_node_run_id,
|
|
49
|
+
workflow_node.workflow_id AS workflow_id,
|
|
50
|
+
workflow_node.workflow_run_id AS workflow_run_id
|
|
51
|
+
FROM ai_model_call_usage AS usage
|
|
52
|
+
LEFT JOIN agent_runs AS direct_agent
|
|
53
|
+
ON direct_agent.project_id = usage.project_id AND direct_agent.execution_id = usage.execution_id
|
|
54
|
+
LEFT JOIN agent_runs AS root_agent
|
|
55
|
+
ON root_agent.project_id = usage.project_id
|
|
56
|
+
AND root_agent.id = COALESCE(direct_agent.parent_run_id, direct_agent.id)
|
|
57
|
+
LEFT JOIN workflow_agent_node_runs AS workflow_agent
|
|
58
|
+
ON workflow_agent.project_id = usage.project_id AND workflow_agent.execution_id = usage.execution_id
|
|
59
|
+
LEFT JOIN workflow_node_runs AS workflow_node
|
|
60
|
+
ON workflow_node.project_id = workflow_agent.project_id AND workflow_node.id = workflow_agent.node_run_id
|
|
61
|
+
LEFT JOIN ai_model_call_valuations AS cost
|
|
62
|
+
ON cost.project_id = usage.project_id AND cost.usage_record_id = usage.id
|
|
63
|
+
WHERE usage.project_id = ?1
|
|
64
|
+
AND usage.occurred_at >= ?2
|
|
65
|
+
AND usage.occurred_at < ?3
|
|
66
|
+
AND (?4 IS NULL OR usage.provider_id = ?4)
|
|
67
|
+
AND (?5 IS NULL OR usage.requested_model_id = ?5)
|
|
68
|
+
AND (?6 IS NULL OR COALESCE(cost.status, 'unvalued') = ?6)
|
|
69
|
+
), roots AS (
|
|
70
|
+
SELECT root_execution_id, MAX(occurred_at) AS last_call_at
|
|
71
|
+
FROM filtered GROUP BY root_execution_id
|
|
72
|
+
)
|
|
73
|
+
`
|
|
74
|
+
const COUNT_SQL = `${FILTERED_SQL} SELECT CAST(COUNT(*) AS TEXT) AS total FROM roots`
|
|
75
|
+
const PAGE_SQL = `${FILTERED_SQL}, page AS (
|
|
76
|
+
SELECT root_execution_id FROM roots
|
|
77
|
+
ORDER BY last_call_at DESC, root_execution_id ASC LIMIT ?7 OFFSET ?8
|
|
78
|
+
)
|
|
79
|
+
SELECT filtered.root_execution_id, execution_id, provider_id, requested_model_id,
|
|
80
|
+
MIN(occurred_at) AS first_call_at, MAX(occurred_at) AS last_call_at,
|
|
81
|
+
CAST(COUNT(*) AS TEXT) AS call_count,
|
|
82
|
+
CAST(COUNT(total_tokens) AS TEXT) AS token_count,
|
|
83
|
+
CAST(SUM(total_tokens) AS TEXT) AS total_tokens,
|
|
84
|
+
valuation_status, amount_currency,
|
|
85
|
+
CAST(SUM(amount_nanos / 1000000000000) AS TEXT) AS amount_high,
|
|
86
|
+
CAST(SUM((amount_nanos / 1000000) % 1000000) AS TEXT) AS amount_middle,
|
|
87
|
+
CAST(SUM(amount_nanos % 1000000) AS TEXT) AS amount_low,
|
|
88
|
+
MAX(root_agent_run_id) AS root_agent_run_id,
|
|
89
|
+
MAX(root_thread_id) AS root_thread_id,
|
|
90
|
+
MAX(direct_kind) AS direct_kind,
|
|
91
|
+
MAX(direct_run_id) AS direct_run_id,
|
|
92
|
+
MAX(parent_run_id) AS parent_run_id,
|
|
93
|
+
MAX(workflow_agent_step_id) AS workflow_agent_step_id,
|
|
94
|
+
MAX(workflow_node_run_id) AS workflow_node_run_id,
|
|
95
|
+
MAX(workflow_id) AS workflow_id,
|
|
96
|
+
MAX(workflow_run_id) AS workflow_run_id
|
|
97
|
+
FROM filtered JOIN page ON page.root_execution_id = filtered.root_execution_id
|
|
98
|
+
GROUP BY filtered.root_execution_id, execution_id, provider_id, requested_model_id,
|
|
99
|
+
valuation_status, amount_currency
|
|
100
|
+
`
|