@mupt-ai/dari-gateway 1.0.325 → 1.0.326
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/README.md +4 -3
- package/dist/activity/conversation_detail.d.ts +2 -0
- package/dist/activity/conversation_detail.js +259 -0
- package/dist/activity/conversation_records.d.ts +129 -0
- package/dist/activity/conversation_records.js +318 -0
- package/dist/activity/conversations.d.ts +11 -0
- package/dist/activity/conversations.js +198 -0
- package/dist/activity/core.d.ts +13 -0
- package/dist/activity/core.js +613 -0
- package/dist/activity/filters.d.ts +71 -0
- package/dist/activity/filters.js +169 -0
- package/dist/activity/http.d.ts +37 -0
- package/dist/activity/http.js +109 -0
- package/dist/activity/params.d.ts +40 -0
- package/dist/activity/params.js +130 -0
- package/dist/activity/people.d.ts +3 -0
- package/dist/activity/people.js +413 -0
- package/dist/activity/routes.d.ts +2 -0
- package/dist/activity/routes.js +23 -0
- package/dist/activity/savings.d.ts +12 -0
- package/dist/activity/savings.js +102 -0
- package/dist/activity/shared.d.ts +15 -0
- package/dist/activity/shared.js +26 -0
- package/dist/activity/sql.d.ts +52 -0
- package/dist/activity/sql.js +124 -0
- package/dist/activity/tool_exchanges.d.ts +21 -0
- package/dist/activity/tool_exchanges.js +309 -0
- package/dist/activity/tool_skill_activity.d.ts +128 -0
- package/dist/activity/tool_skill_activity.js +280 -0
- package/dist/activity/tool_skill_identities.d.ts +25 -0
- package/dist/activity/tool_skill_identities.js +162 -0
- package/dist/activity/tools.d.ts +4 -0
- package/dist/activity/tools.js +68 -0
- package/dist/activity/values.d.ts +45 -0
- package/dist/activity/values.js +178 -0
- package/dist/context/interaction_fingerprint.js +11 -3
- package/dist/context/interaction_fingerprint_format.d.ts +2 -1
- package/dist/context/interaction_fingerprint_format.js +2 -0
- package/dist/http/handler.d.ts +2 -1
- package/dist/http/handler.js +6 -1
- package/dist/interaction/types.d.ts +17 -2
- package/dist/interaction/types.js +11 -0
- package/dist/platform/platform.d.ts +7 -0
- package/dist/protocol/input/anthropic_messages.js +63 -48
- package/dist/providers/pi.js +22 -3
- package/dist/providers/pi_context.js +15 -5
- package/dist/self_host/activity_store.d.ts +35 -0
- package/dist/self_host/activity_store.js +373 -0
- package/dist/self_host/main.d.ts +3 -2
- package/dist/self_host/main.js +8 -3
- package/dist/self_host/platform.d.ts +4 -2
- package/dist/self_host/platform.js +35 -3
- package/package.json +2 -2
- package/src/activity/conversation_detail.ts +283 -0
- package/src/activity/conversation_records.ts +403 -0
- package/src/activity/conversations.ts +218 -0
- package/src/activity/core.ts +678 -0
- package/src/activity/filters.ts +206 -0
- package/src/activity/http.ts +140 -0
- package/src/activity/params.ts +138 -0
- package/src/activity/people.ts +471 -0
- package/src/activity/routes.ts +26 -0
- package/src/activity/savings.ts +121 -0
- package/src/activity/shared.ts +36 -0
- package/src/activity/sql.ts +139 -0
- package/src/activity/tool_exchanges.ts +340 -0
- package/src/activity/tool_skill_activity.ts +314 -0
- package/src/activity/tool_skill_identities.ts +196 -0
- package/src/activity/tools.ts +76 -0
- package/src/activity/values.ts +197 -0
- package/src/context/interaction_fingerprint.ts +9 -3
- package/src/context/interaction_fingerprint_format.ts +6 -1
- package/src/http/handler.ts +5 -1
- package/src/interaction/types.ts +21 -11
- package/src/platform/platform.ts +7 -0
- package/src/protocol/input/anthropic_messages.ts +66 -53
- package/src/providers/pi.ts +22 -3
- package/src/providers/pi_context.ts +14 -7
- package/src/self_host/activity_store.ts +423 -0
- package/src/self_host/main.ts +17 -3
- package/src/self_host/platform.ts +38 -3
package/README.md
CHANGED
|
@@ -31,13 +31,14 @@ The gateway is the runtime behind Dari's hosted routing service, packaged to run
|
|
|
31
31
|
- **Failover.** A provider failure retries the router's next model.
|
|
32
32
|
- **Prompt-cache aware.** Conversations keep their identity and serving model across turns, so provider caches stay warm. Responses carry `X-Dari-Selected-Model`, `X-Dari-Thinking-Level`, and `X-Dari-Conversation-Id` headers for status lines.
|
|
33
33
|
- **Coding agents.** `dari claude`, `dari codex`, `dari opencode`, and `dari pi` from the [Dari CLI](https://github.com/mupt-ai/dari) launch straight against your gateway.
|
|
34
|
+
- **Activity.** Every request's model, tokens, cost, conversation, and tool and skill use is kept in the gateway database and read at `GET /v1/organizations/current/routing/activity/...` (overview, series, models, people-keys, conversations, tools-skills, and the rest) with a gateway key, or with `dari activity`. It is the same code that serves Activity on hosted Dari.
|
|
34
35
|
|
|
35
36
|
Dari Auto Routing (choosing among a router's models with Dari's trained policy) is optional and needs a Dari API key; without it, `dari/routing` serves the router's first available model.
|
|
36
37
|
|
|
37
38
|
## Run
|
|
38
39
|
|
|
39
40
|
> [!IMPORTANT]
|
|
40
|
-
> **
|
|
41
|
+
> **Activity stays on your gateway.** Requests through your own gateway never reach Dari. The gateway keeps their Activity itself, read with `dari activity` or the Activity API; the Activity dashboard and billing are hosted only (`dari gateway setup --mode hosted`).
|
|
41
42
|
|
|
42
43
|
The easiest way to run it on your own machine is the [Dari CLI](https://github.com/mupt-ai/dari): choose **Fully local** in `dari gateway setup` (or on your first `dari claude`), and it installs this gateway's standalone binary, runs it in the background, and connects your agents.
|
|
43
44
|
|
|
@@ -63,7 +64,7 @@ docker run -p 8080:8080 \
|
|
|
63
64
|
| --- | --- |
|
|
64
65
|
| `DARI_GATEWAY_KEYS` | Required. Comma-separated bearer keys clients authenticate with. |
|
|
65
66
|
| `DARI_GATEWAY_CONFIG` | Router config file. Default `dari-gateway.yaml`. |
|
|
66
|
-
| `DARI_GATEWAY_DB` | SQLite database for subscriptions, Dari Link grants, model lists saved with `dari gateway models`, and
|
|
67
|
+
| `DARI_GATEWAY_DB` | SQLite database for subscriptions, Dari Link grants, model lists saved with `dari gateway models`, conversation state, and Activity (each request's log row and, outside ZDR routers, its request and response bodies). Default `dari-gateway.db`. Without `DARI_GATEWAY_ENCRYPTION_KEY` it stores subscription tokens in plaintext; protect it like a secret either way. |
|
|
67
68
|
| `DARI_GATEWAY_ENCRYPTION_KEY` | Encrypts the subscription tokens, pending sign-ins, and API keys in the database: a 32-byte key in base64 (`openssl rand -base64 32`). Each value is sealed with AES-256-GCM and bound to its own row, so a copied database, or a token moved into another row, is useless without the key. Keep the key outside the database's storage and backups (a secret manager, not the same disk). To rotate, list the new key first and the old one after a comma, start the gateway once (it re-encrypts everything under the first), then drop the old one. Required for Dari Link: without it the approval page takes nobody's subscriptions or keys. Losing the key loses the stored subscriptions; users connect again. |
|
|
68
69
|
| `DARI_LINK_SESSION_SECRET` | Signs the cookie that remembers the user of Dari Link's approval and connections pages. Unset, each gateway process makes its own, so a restart forgets who connected which plans. Set it on any gateway that serves Dari Link. |
|
|
69
70
|
| `DARI_AUTO_ROUTING_API_KEY` | Optional. Select among a router's models with Dari Auto Routing. |
|
|
@@ -116,7 +117,7 @@ dari accounts subscription connect claude-code # or codex or muse-code; repeat
|
|
|
116
117
|
dari claude
|
|
117
118
|
```
|
|
118
119
|
|
|
119
|
-
In gateway mode the CLI makes no calls to Dari's hosted API
|
|
120
|
+
In gateway mode the CLI makes no calls to Dari's hosted API; `dari activity` reads the gateway's own Activity.
|
|
120
121
|
|
|
121
122
|
## Dari Link
|
|
122
123
|
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/* One conversation: its agent streams, context runs, a page of its steps
|
|
2
|
+
(newest page first, each page oldest first), and the tool exchanges and
|
|
3
|
+
skill events of that page. */
|
|
4
|
+
import { consecutivePath, contextRun, conversationSummary, decisionDetails, findRecord, groupsCondition, loadEntities, loadRecords, optionalFixed, pyRepr, recordKey, requestBlobFormat, selectedEffort, supported, } from "./conversation_records.js";
|
|
5
|
+
import { notFound } from "./params.js";
|
|
6
|
+
import { thinkingLevelValue } from "./shared.js";
|
|
7
|
+
import { sql } from "./sql.js";
|
|
8
|
+
import { formatInstant, readOptionalString } from "./values.js";
|
|
9
|
+
import { EXCHANGE_MAX_LIMIT, EXCHANGE_STATES, loadToolExchanges } from "./tool_exchanges.js";
|
|
10
|
+
const REQUEST_CLASSES = ["main", "subagent", "workflow", "auxiliary", "compaction", "side_reply", "unknown"];
|
|
11
|
+
/** The selector reason older rows carry for the single-candidate fast path. */
|
|
12
|
+
const SINGLE_CANDIDATE_REASON = "The router has only one compatible model/thinking-level pair.";
|
|
13
|
+
/** The stored agent context, or an unknown request class. */
|
|
14
|
+
function agentContext(record) {
|
|
15
|
+
const metadata = record.requestMetadata;
|
|
16
|
+
const context = metadata !== null && typeof metadata === "object" && !Array.isArray(metadata)
|
|
17
|
+
? metadata.agent_context
|
|
18
|
+
: null;
|
|
19
|
+
if (context === null || typeof context !== "object" || Array.isArray(context)) {
|
|
20
|
+
return { requestClass: "unknown", agentId: null, parentAgentId: null, agentType: null };
|
|
21
|
+
}
|
|
22
|
+
const value = context;
|
|
23
|
+
return {
|
|
24
|
+
requestClass: typeof value.requestClass === "string" && REQUEST_CLASSES.includes(value.requestClass)
|
|
25
|
+
? value.requestClass
|
|
26
|
+
: "unknown",
|
|
27
|
+
agentId: readOptionalString(value.agentId),
|
|
28
|
+
parentAgentId: readOptionalString(value.parentAgentId),
|
|
29
|
+
agentType: readOptionalString(value.agentType),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function routePath(records) {
|
|
33
|
+
const path = [];
|
|
34
|
+
for (const record of records) {
|
|
35
|
+
if (record.selectedModel === null)
|
|
36
|
+
continue;
|
|
37
|
+
const route = { model: record.selectedModel, thinking_level: selectedEffort(record) };
|
|
38
|
+
const last = path.at(-1);
|
|
39
|
+
if (last?.model === route.model && last.thinking_level === route.thinking_level)
|
|
40
|
+
continue;
|
|
41
|
+
path.push(route);
|
|
42
|
+
}
|
|
43
|
+
return path;
|
|
44
|
+
}
|
|
45
|
+
function modelPath(records) {
|
|
46
|
+
return consecutivePath(records.map((record) => record.selectedModel));
|
|
47
|
+
}
|
|
48
|
+
/** Records grouped by a key, groups in first-seen order. */
|
|
49
|
+
function groupBy(records, keyOf, text) {
|
|
50
|
+
const groups = new Map();
|
|
51
|
+
for (const record of records) {
|
|
52
|
+
const key = keyOf(record);
|
|
53
|
+
const group = groups.get(text(key)) ?? { key, records: [] };
|
|
54
|
+
group.records.push(record);
|
|
55
|
+
groups.set(text(key), group);
|
|
56
|
+
}
|
|
57
|
+
return [...groups.values()];
|
|
58
|
+
}
|
|
59
|
+
export const conversation = async (request) => {
|
|
60
|
+
const { params, db } = request;
|
|
61
|
+
const limit = params.int("limit", { default: 25, ge: 1, le: 100 });
|
|
62
|
+
const offset = params.int("offset", { default: 0, ge: 0 });
|
|
63
|
+
const requestClass = params.literal("request_class", REQUEST_CLASSES);
|
|
64
|
+
const agentId = params.string("agent_id", { minLength: 1, maxLength: 200 });
|
|
65
|
+
const focusStepId = params.string("focus_step_id", { minLength: 1, maxLength: 200 });
|
|
66
|
+
params.done();
|
|
67
|
+
const organizationId = request.admitted();
|
|
68
|
+
const ref = request.path.conversation_ref;
|
|
69
|
+
const unknown = notFound(`Unknown routing conversation ${pyRepr(ref)}.`);
|
|
70
|
+
const reference = await findRecord(db, ref, organizationId);
|
|
71
|
+
if (!supported(reference, organizationId))
|
|
72
|
+
throw unknown;
|
|
73
|
+
const all = await loadRecords(db, sql `
|
|
74
|
+
r.organization_id = ${organizationId}
|
|
75
|
+
and r.source_protocol = ${reference.sourceProtocol}
|
|
76
|
+
and ${groupsCondition([recordKey(reference)])}`);
|
|
77
|
+
if (all.length === 0)
|
|
78
|
+
throw unknown;
|
|
79
|
+
const entities = await loadEntities(db, all);
|
|
80
|
+
const currentRouterZdr = reference.routerId !== null && (entities.routers.get(reference.routerId)?.zdr ?? false);
|
|
81
|
+
let wantedClass = requestClass ?? null;
|
|
82
|
+
let wantedAgent = agentId ?? null;
|
|
83
|
+
let focused = false;
|
|
84
|
+
let focusedRequestClass = null;
|
|
85
|
+
let focusedAgentId = null;
|
|
86
|
+
if (focusStepId !== undefined) {
|
|
87
|
+
const step = all.find((record) => record.id === focusStepId);
|
|
88
|
+
if (step === undefined)
|
|
89
|
+
throw notFound("Unknown step in this conversation.");
|
|
90
|
+
const context = agentContext(step);
|
|
91
|
+
focused = true;
|
|
92
|
+
wantedClass = focusedRequestClass = context.requestClass;
|
|
93
|
+
wantedAgent = focusedAgentId = context.agentId;
|
|
94
|
+
}
|
|
95
|
+
const filtered = all.filter((record) => {
|
|
96
|
+
const context = agentContext(record);
|
|
97
|
+
if (wantedClass !== null && context.requestClass !== wantedClass)
|
|
98
|
+
return false;
|
|
99
|
+
if ((wantedAgent !== null || focused) && context.agentId !== wantedAgent)
|
|
100
|
+
return false;
|
|
101
|
+
return true;
|
|
102
|
+
});
|
|
103
|
+
// The offset counts from the newest step; a page stays oldest first.
|
|
104
|
+
const end = filtered.length - offset;
|
|
105
|
+
const start = Math.max(0, end - limit);
|
|
106
|
+
const page = end > 0 ? filtered.slice(start, end) : [];
|
|
107
|
+
const skills = await loadSkillEvents(db, page);
|
|
108
|
+
const exchanges = await pageToolExchanges(db, organizationId, page);
|
|
109
|
+
const streams = groupBy(all, agentContext, (c) => JSON.stringify([c.requestClass, c.agentId])).map(({ key, records }) => {
|
|
110
|
+
const first = agentContext(records[0]);
|
|
111
|
+
return {
|
|
112
|
+
request_class: key.requestClass,
|
|
113
|
+
agent_id: key.agentId,
|
|
114
|
+
parent_agent_id: first.parentAgentId,
|
|
115
|
+
agent_type: first.agentType,
|
|
116
|
+
model_steps: records.length,
|
|
117
|
+
model_path: modelPath(records),
|
|
118
|
+
route_path: routePath(records),
|
|
119
|
+
};
|
|
120
|
+
});
|
|
121
|
+
const contextRuns = groupBy(filtered, contextRun, (run) => run).map(({ records }) => ({
|
|
122
|
+
context_run_ref: records[0].id,
|
|
123
|
+
conversation_id: records[0].conversationId || null,
|
|
124
|
+
first_at: formatInstant(records[0].receivedAt),
|
|
125
|
+
last_at: formatInstant(records.at(-1).receivedAt),
|
|
126
|
+
model_steps: records.length,
|
|
127
|
+
model_path: modelPath(records),
|
|
128
|
+
route_path: routePath(records),
|
|
129
|
+
}));
|
|
130
|
+
const hasMore = start > 0;
|
|
131
|
+
return {
|
|
132
|
+
conversation: conversationSummary(all, entities, undefined, null),
|
|
133
|
+
streams,
|
|
134
|
+
filtered_model_steps: filtered.length,
|
|
135
|
+
focused_request_class: focusedRequestClass,
|
|
136
|
+
focused_agent_id: focusedAgentId,
|
|
137
|
+
context_runs: contextRuns,
|
|
138
|
+
steps: page.map((record) => conversationStep(record, organizationId, currentRouterZdr, skills.get(record.id) ?? [])),
|
|
139
|
+
tool_exchanges: exchanges.items,
|
|
140
|
+
tool_exchanges_truncated: exchanges.next_offset !== null,
|
|
141
|
+
limit,
|
|
142
|
+
offset,
|
|
143
|
+
has_more: hasMore,
|
|
144
|
+
next_offset: hasMore ? offset + limit : null,
|
|
145
|
+
};
|
|
146
|
+
};
|
|
147
|
+
function conversationStep(record, organizationId, currentRouterZdr, skillEvents) {
|
|
148
|
+
const details = decisionDetails(record);
|
|
149
|
+
const agent = agentContext(record);
|
|
150
|
+
const effort = selectedEffort(record);
|
|
151
|
+
const bypassed = details?.selector_policy_bypass === "single_candidate" || record.selectorReason === SINGLE_CANDIDATE_REASON;
|
|
152
|
+
let selectorModel = null;
|
|
153
|
+
let selectorEffort = null;
|
|
154
|
+
if (details !== null) {
|
|
155
|
+
if (details.decision_source !== "speculative_selector") {
|
|
156
|
+
selectorModel = record.selectedModel;
|
|
157
|
+
selectorEffort = effort;
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
const speculative = details.speculative_routing;
|
|
161
|
+
if (speculative !== null && typeof speculative === "object" && !Array.isArray(speculative)) {
|
|
162
|
+
const next = speculative;
|
|
163
|
+
selectorModel = typeof next.next_model === "string" ? next.next_model : null;
|
|
164
|
+
selectorEffort = thinkingLevelValue(next.next_reasoning_effort);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
const available = (key) => Boolean(record.blobBucket) && Boolean(key);
|
|
169
|
+
const url = (name) => `/v1/organizations/${organizationId}/routing/activity/requests/${record.id}/${name}`;
|
|
170
|
+
return {
|
|
171
|
+
id: record.id,
|
|
172
|
+
conversation_id: record.conversationId || null,
|
|
173
|
+
source_protocol: record.sourceProtocol,
|
|
174
|
+
request_kind: record.requestKind,
|
|
175
|
+
request_class: agent.requestClass,
|
|
176
|
+
agent_id: agent.agentId,
|
|
177
|
+
parent_agent_id: agent.parentAgentId,
|
|
178
|
+
agent_type: agent.agentType,
|
|
179
|
+
status: record.status,
|
|
180
|
+
error_code: record.errorCode,
|
|
181
|
+
error_message: record.errorMessage,
|
|
182
|
+
selected_model: record.selectedModel,
|
|
183
|
+
selected_reasoning_effort: effort,
|
|
184
|
+
selected_provider: record.selectedProvider,
|
|
185
|
+
provider_input_tokens: record.providerInputTokens,
|
|
186
|
+
provider_cache_read_tokens: record.providerCacheReadTokens,
|
|
187
|
+
provider_cache_write_tokens: record.providerCacheWriteTokens,
|
|
188
|
+
provider_output_tokens: record.providerOutputTokens,
|
|
189
|
+
provider_total_tokens: record.providerTotalTokens,
|
|
190
|
+
provider_spend_usd: optionalFixed(record.providerTotalCostUsd),
|
|
191
|
+
provider_pricing_known: record.providerPricingKnown,
|
|
192
|
+
selector_latency_ms: record.selectorLatencyMs,
|
|
193
|
+
provider_latency_ms: record.providerLatencyMs,
|
|
194
|
+
total_latency_ms: record.totalLatencyMs,
|
|
195
|
+
message_count: record.messageCount,
|
|
196
|
+
has_declared_tools: record.hasTools,
|
|
197
|
+
received_at: formatInstant(record.receivedAt),
|
|
198
|
+
completed_at: record.completedAt === null ? null : formatInstant(record.completedAt),
|
|
199
|
+
zdr: record.zdr || currentRouterZdr,
|
|
200
|
+
decision_source: typeof details?.decision_source === "string" ? details.decision_source : null,
|
|
201
|
+
selector_reason: record.selectorReason,
|
|
202
|
+
selector_policy_bypassed: bypassed,
|
|
203
|
+
selector_selected_model: selectorModel,
|
|
204
|
+
selector_selected_reasoning_effort: selectorEffort,
|
|
205
|
+
selector_lease_turns: record.leaseTurns,
|
|
206
|
+
client_request_url: available(record.requestBlobKey) ? url("client-request") : null,
|
|
207
|
+
client_response_url: available(record.responseBlobKey) ? url("client-response") : null,
|
|
208
|
+
selector_output_url: !bypassed && available(record.selectorOutputBlobKey) ? url("selector-output") : null,
|
|
209
|
+
client_request_format: requestBlobFormat(record),
|
|
210
|
+
client_response_format: record.sourceProtocol,
|
|
211
|
+
skill_events: skillEvents,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
/** The skill uses and loads of the steps, by step, in ordinal order. */
|
|
215
|
+
async function loadSkillEvents(db, records) {
|
|
216
|
+
const events = new Map();
|
|
217
|
+
if (records.length === 0)
|
|
218
|
+
return events;
|
|
219
|
+
const rows = await db.all(sql `
|
|
220
|
+
select routing_request_id, event_type, capability_id, observed_name, skill_version, evidence_source
|
|
221
|
+
from routing_tool_skill_events
|
|
222
|
+
where routing_request_id in (${records.map((record) => record.id)})
|
|
223
|
+
and event_type in ('skill_use', 'skill_load')
|
|
224
|
+
order by routing_request_id, ordinal`);
|
|
225
|
+
for (const row of rows) {
|
|
226
|
+
const id = String(row.routing_request_id);
|
|
227
|
+
events.set(id, [...(events.get(id) ?? []), {
|
|
228
|
+
event_type: row.event_type,
|
|
229
|
+
capability_id: row.capability_id,
|
|
230
|
+
observed_name: row.observed_name,
|
|
231
|
+
skill_version: readOptionalString(row.skill_version),
|
|
232
|
+
evidence_source: row.evidence_source,
|
|
233
|
+
}]);
|
|
234
|
+
}
|
|
235
|
+
return events;
|
|
236
|
+
}
|
|
237
|
+
/** The tool exchanges of the page's steps, regardless of the time window. */
|
|
238
|
+
async function pageToolExchanges(db, organizationId, page) {
|
|
239
|
+
if (page.length === 0)
|
|
240
|
+
return { items: [], next_offset: null };
|
|
241
|
+
const start = Math.min(...page.map((record) => record.receivedAt));
|
|
242
|
+
const protocols = [...new Set(page.map((record) => record.sourceProtocol))];
|
|
243
|
+
return loadToolExchanges(db, organizationId, {
|
|
244
|
+
start,
|
|
245
|
+
end: start + 1_000_000,
|
|
246
|
+
bucketSeconds: 0,
|
|
247
|
+
protocols,
|
|
248
|
+
apiKeyIds: [],
|
|
249
|
+
userIds: [],
|
|
250
|
+
models: [],
|
|
251
|
+
}, {
|
|
252
|
+
states: EXCHANGE_STATES,
|
|
253
|
+
capabilityId: null,
|
|
254
|
+
requestIds: page.map((record) => record.id),
|
|
255
|
+
includeTotal: false,
|
|
256
|
+
limit: EXCHANGE_MAX_LIMIT,
|
|
257
|
+
offset: 0,
|
|
258
|
+
});
|
|
259
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { type ReasoningEffort } from "./shared.js";
|
|
2
|
+
import { type ActivityDb, type Sql } from "./sql.js";
|
|
3
|
+
import { type Decimal, type Instant } from "./values.js";
|
|
4
|
+
/** A routing_requests row as the conversation reads use it. */
|
|
5
|
+
export type ConversationRecord = {
|
|
6
|
+
id: string;
|
|
7
|
+
requestId: string;
|
|
8
|
+
organizationId: string | null;
|
|
9
|
+
sourceProtocol: string | null;
|
|
10
|
+
routerId: string | null;
|
|
11
|
+
apiKeyId: string | null;
|
|
12
|
+
attributedUserId: string | null;
|
|
13
|
+
requestKind: string | null;
|
|
14
|
+
conversationId: string | null;
|
|
15
|
+
agentSessionId: string | null;
|
|
16
|
+
conversationTitle: string | null;
|
|
17
|
+
zdr: boolean;
|
|
18
|
+
status: string;
|
|
19
|
+
errorCode: string | null;
|
|
20
|
+
errorMessage: string | null;
|
|
21
|
+
selectedModel: string | null;
|
|
22
|
+
selectedProvider: string | null;
|
|
23
|
+
providerInputTokens: number | null;
|
|
24
|
+
providerCacheReadTokens: number | null;
|
|
25
|
+
providerCacheWriteTokens: number | null;
|
|
26
|
+
providerOutputTokens: number | null;
|
|
27
|
+
providerTotalTokens: number | null;
|
|
28
|
+
providerTotalCostUsd: string | number | null;
|
|
29
|
+
providerPricingKnown: boolean | null;
|
|
30
|
+
selectorLatencyMs: number | null;
|
|
31
|
+
providerLatencyMs: number | null;
|
|
32
|
+
totalLatencyMs: number | null;
|
|
33
|
+
messageCount: number;
|
|
34
|
+
hasTools: boolean;
|
|
35
|
+
receivedAt: Instant;
|
|
36
|
+
completedAt: Instant | null;
|
|
37
|
+
decisionDetails: unknown;
|
|
38
|
+
requestMetadata: unknown;
|
|
39
|
+
selectorReason: string | null;
|
|
40
|
+
blobBucket: string | null;
|
|
41
|
+
requestBlobKey: string | null;
|
|
42
|
+
responseBlobKey: string | null;
|
|
43
|
+
selectorOutputBlobKey: string | null;
|
|
44
|
+
requestBlobFormat: string | null;
|
|
45
|
+
/** `selector_lease_turns` when it is a positive integer. */
|
|
46
|
+
leaseTurns: number | null;
|
|
47
|
+
};
|
|
48
|
+
/** The rows matching a condition, oldest first. */
|
|
49
|
+
export declare function loadRecords(db: ActivityDb, condition: Sql): Promise<ConversationRecord[]>;
|
|
50
|
+
/** The row with this ID, or the organization's row with this gateway request ID. */
|
|
51
|
+
export declare function findRecord(db: ActivityDb, ref: string, organizationId?: string): Promise<ConversationRecord | null>;
|
|
52
|
+
/** A row of the organization with a protocol Activity reads. */
|
|
53
|
+
export declare function supported(record: ConversationRecord | null, organizationId: string): record is ConversationRecord;
|
|
54
|
+
/** Python's `a or b or ...` over optional strings. */
|
|
55
|
+
export declare function firstTruthy(...values: Array<string | null | undefined>): string;
|
|
56
|
+
export type GroupKey = {
|
|
57
|
+
sourceProtocol: string;
|
|
58
|
+
conversationKey: string;
|
|
59
|
+
routerId: string | null;
|
|
60
|
+
apiKeyId: string | null;
|
|
61
|
+
attributedUserId: string | null;
|
|
62
|
+
};
|
|
63
|
+
export declare function groupKeyText(key: GroupKey): string;
|
|
64
|
+
export declare function recordKey(record: ConversationRecord): GroupKey;
|
|
65
|
+
/** The record's context run: its conversation ID, or its own row ID. */
|
|
66
|
+
export declare function contextRun(record: ConversationRecord): string;
|
|
67
|
+
/** The steps of any of the conversations, in any time range. */
|
|
68
|
+
export declare function groupsCondition(keys: readonly GroupKey[]): Sql;
|
|
69
|
+
/** Each conversation's latest non-empty title, from steps in any time range. */
|
|
70
|
+
export declare function conversationTitles(db: ActivityDb, organizationId: string, keys: readonly GroupKey[]): Promise<Map<string, string>>;
|
|
71
|
+
export type Entities = {
|
|
72
|
+
routers: Map<string, {
|
|
73
|
+
name: string;
|
|
74
|
+
zdr: boolean;
|
|
75
|
+
}>;
|
|
76
|
+
keys: Map<string, {
|
|
77
|
+
label: string;
|
|
78
|
+
keyPrefix: string;
|
|
79
|
+
}>;
|
|
80
|
+
users: Map<string, {
|
|
81
|
+
displayName: string | null;
|
|
82
|
+
email: string;
|
|
83
|
+
}>;
|
|
84
|
+
};
|
|
85
|
+
/** The routers, API keys, and people the steps name, deleted ones included. */
|
|
86
|
+
export declare function loadEntities(db: ActivityDb, records: readonly ConversationRecord[]): Promise<Entities>;
|
|
87
|
+
/** Python's `format(Decimal, "f")`. */
|
|
88
|
+
export declare function fixedDecimal(value: Decimal): string;
|
|
89
|
+
export declare function optionalFixed(value: string | number | null): string | null;
|
|
90
|
+
/** The values without nulls or immediate repeats. */
|
|
91
|
+
export declare function consecutivePath(values: ReadonlyArray<string | null>): string[];
|
|
92
|
+
export declare function decisionDetails(record: ConversationRecord): Record<string, unknown> | null;
|
|
93
|
+
export declare function selectedEffort(record: ConversationRecord): ReasoningEffort | null;
|
|
94
|
+
/** The conversation summary over its steps, oldest first. */
|
|
95
|
+
export declare function conversationSummary(records: readonly ConversationRecord[], entities: Entities, title: string | undefined, switches: number | null): {
|
|
96
|
+
conversation_ref: string;
|
|
97
|
+
conversation_id: string | null;
|
|
98
|
+
agent_session_id: string | null;
|
|
99
|
+
context_runs: number;
|
|
100
|
+
source_protocol: string | null;
|
|
101
|
+
title: string | null;
|
|
102
|
+
router_id: string | null;
|
|
103
|
+
router_name: string | null;
|
|
104
|
+
api_key_id: string | null;
|
|
105
|
+
api_key_label: string | null;
|
|
106
|
+
api_key_prefix: string | null;
|
|
107
|
+
attributed_user_id: string | null;
|
|
108
|
+
attributed_user_name: string | null;
|
|
109
|
+
attributed_user_email: string | null;
|
|
110
|
+
first_at: string;
|
|
111
|
+
last_at: string;
|
|
112
|
+
messages_sent: number;
|
|
113
|
+
model_steps: number;
|
|
114
|
+
model_switches: number;
|
|
115
|
+
model_path: string[];
|
|
116
|
+
provider_path: string[];
|
|
117
|
+
provider_total_tokens: number;
|
|
118
|
+
provider_spend_usd: string | null;
|
|
119
|
+
priced_model_steps: number;
|
|
120
|
+
unpriced_model_steps: number;
|
|
121
|
+
has_error: boolean;
|
|
122
|
+
has_aborted: boolean;
|
|
123
|
+
terminal_status: string;
|
|
124
|
+
retention: string;
|
|
125
|
+
};
|
|
126
|
+
/** Python's `repr` of a string. */
|
|
127
|
+
export declare function pyRepr(text: string): string;
|
|
128
|
+
/** The stored request format; a row with an unknown one is inconsistent. */
|
|
129
|
+
export declare function requestBlobFormat(record: ConversationRecord): string | null;
|