@mupt-ai/dari-gateway 1.0.368 → 1.0.370
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/harnesses/claude_code.d.ts +11 -0
- package/dist/{ingress/claude_code/request_context.js → harnesses/claude_code.js} +40 -1
- package/dist/harnesses/codex.d.ts +9 -0
- package/dist/harnesses/codex.js +112 -0
- package/dist/harnesses/hermes.d.ts +4 -0
- package/dist/harnesses/hermes.js +6 -0
- package/dist/harnesses/index.d.ts +34 -0
- package/dist/harnesses/index.js +13 -0
- package/dist/harnesses/muse_code.d.ts +9 -0
- package/dist/harnesses/muse_code.js +22 -0
- package/dist/harnesses/openclaw.d.ts +4 -0
- package/dist/harnesses/openclaw.js +6 -0
- package/dist/harnesses/opencode.d.ts +7 -0
- package/dist/harnesses/opencode.js +40 -0
- package/dist/harnesses/pi.d.ts +6 -0
- package/dist/harnesses/pi.js +23 -0
- package/dist/ingress/client_harness.d.ts +1 -2
- package/dist/ingress/client_harness.js +4 -39
- package/dist/ingress/harness_request.js +6 -3
- package/dist/ingress/models.js +2 -1
- package/dist/link/endpoints.js +5 -5
- package/dist/link/self_hosted.js +0 -1
- package/dist/platform/platform.d.ts +2 -0
- package/dist/platform/routing_contract.d.ts +1 -0
- package/dist/platform/routing_contract.js +4 -0
- package/dist/protocol/output/anthropic_messages.d.ts +1 -0
- package/dist/protocol/output/anthropic_messages.js +1 -1
- package/dist/protocol/output/anthropic_messages_output.d.ts +1 -1
- package/dist/protocol/output/anthropic_messages_output.js +3 -1
- package/dist/protocol/output/anthropic_messages_stream.d.ts +1 -0
- package/dist/protocol/output/anthropic_messages_stream.js +1 -1
- package/dist/protocol/output/chat_completions.d.ts +2 -0
- package/dist/protocol/output/chat_completions.js +5 -3
- package/dist/protocol/output/openai_responses.d.ts +1 -0
- package/dist/protocol/output/openai_responses.js +1 -1
- package/dist/protocol/output/openai_responses_output.d.ts +1 -1
- package/dist/protocol/output/openai_responses_output.js +3 -1
- package/dist/protocol/output/openai_responses_stream.d.ts +1 -0
- package/dist/protocol/output/openai_responses_stream.js +1 -1
- package/dist/providers/execution.d.ts +1 -0
- package/dist/providers/execution.js +29 -7
- package/dist/providers/provider_call.d.ts +1 -0
- package/dist/providers/provider_call.js +2 -0
- package/dist/runtime/harness_identity/extract.d.ts +0 -8
- package/dist/runtime/harness_identity/extract.js +2 -201
- package/dist/runtime/harness_identity/ids.d.ts +2 -0
- package/dist/runtime/harness_identity/ids.js +26 -0
- package/dist/runtime/harness_identity/index.d.ts +1 -1
- package/dist/runtime/harness_identity/index.js +1 -1
- package/dist/runtime/prepare_routed_completion.d.ts +1 -0
- package/dist/runtime/prepare_routed_completion.js +4 -0
- package/dist/runtime/routed_completion.js +4 -1
- package/dist/runtime/routed_protocol.d.ts +2 -0
- package/dist/self_host/config.js +1 -1
- package/dist/self_host/connected.d.ts +7 -3
- package/dist/self_host/connected.js +9 -10
- package/dist/self_host/hosted_sync.js +1 -1
- package/dist/self_host/main.d.ts +3 -2
- package/dist/self_host/main.js +16 -6
- package/dist/self_host/private_mode.d.ts +16 -0
- package/dist/self_host/private_mode.js +35 -0
- package/dist/self_host/resolve.js +5 -2
- package/dist/self_host/router_models.js +1 -1
- package/dist/self_host/subscriptions/http.js +2 -17
- package/dist/self_host/subscriptions/store.d.ts +7 -6
- package/dist/self_host/subscriptions/store.js +15 -27
- package/dist/telemetry/report.js +1 -0
- package/dist/types.d.ts +2 -1
- package/package.json +2 -2
- package/src/{ingress/claude_code/request_context.ts → harnesses/claude_code.ts} +56 -2
- package/src/harnesses/codex.ts +156 -0
- package/src/harnesses/hermes.ts +8 -0
- package/src/harnesses/index.ts +29 -0
- package/src/harnesses/muse_code.ts +30 -0
- package/src/harnesses/openclaw.ts +8 -0
- package/src/harnesses/opencode.ts +51 -0
- package/src/harnesses/pi.ts +28 -0
- package/src/ingress/client_harness.ts +4 -41
- package/src/ingress/harness_request.ts +9 -9
- package/src/ingress/models.ts +2 -1
- package/src/link/endpoints.ts +5 -5
- package/src/link/self_hosted.ts +0 -1
- package/src/platform/platform.ts +6 -0
- package/src/platform/routing_contract.ts +5 -0
- package/src/protocol/output/anthropic_messages.ts +2 -1
- package/src/protocol/output/anthropic_messages_output.ts +4 -1
- package/src/protocol/output/anthropic_messages_stream.ts +2 -1
- package/src/protocol/output/chat_completions.ts +7 -3
- package/src/protocol/output/openai_responses.ts +2 -1
- package/src/protocol/output/openai_responses_output.ts +4 -1
- package/src/protocol/output/openai_responses_stream.ts +2 -1
- package/src/providers/execution.ts +36 -7
- package/src/providers/provider_call.ts +5 -0
- package/src/runtime/harness_identity/extract.ts +2 -284
- package/src/runtime/harness_identity/ids.ts +41 -0
- package/src/runtime/harness_identity/index.ts +1 -10
- package/src/runtime/prepare_routed_completion.ts +4 -0
- package/src/runtime/routed_completion.ts +4 -1
- package/src/runtime/routed_protocol.ts +2 -0
- package/src/self_host/config.ts +1 -1
- package/src/self_host/connected.ts +16 -12
- package/src/self_host/hosted_sync.ts +1 -1
- package/src/self_host/main.ts +14 -6
- package/src/self_host/private_mode.ts +41 -0
- package/src/self_host/resolve.ts +4 -2
- package/src/self_host/router_models.ts +1 -1
- package/src/self_host/subscriptions/http.ts +2 -15
- package/src/self_host/subscriptions/store.ts +15 -34
- package/src/telemetry/report.ts +1 -0
- package/src/types.ts +5 -10
- package/dist/ingress/claude_code/request_context.d.ts +0 -4
- package/dist/ingress/muse_code.d.ts +0 -2
- package/dist/ingress/muse_code.js +0 -8
- package/src/ingress/muse_code.ts +0 -9
|
@@ -25,6 +25,7 @@ import {
|
|
|
25
25
|
import type { RoutedProtocol } from "../runtime/routed_protocol.js";
|
|
26
26
|
import { trackBackgroundTelemetry } from "../telemetry/background.js";
|
|
27
27
|
import { gatewayPlatform, type SubscriptionHold } from "../platform/platform.js";
|
|
28
|
+
import { HOSTED_DARI_PROVIDER } from "../platform/routing_contract.js";
|
|
28
29
|
import { reportRoutingUsage } from "../telemetry/usage.js";
|
|
29
30
|
import {
|
|
30
31
|
prepareProviderExecution,
|
|
@@ -86,6 +87,8 @@ export type PreparedStreamExecution = {
|
|
|
86
87
|
|
|
87
88
|
export type CompletedNonStreamingExecution = {
|
|
88
89
|
choice: ProviderChoice;
|
|
90
|
+
// The request the client is told it made; see servedRequestId.
|
|
91
|
+
requestId: string;
|
|
89
92
|
output: ReturnType<typeof piMessageToInteractionOutput>;
|
|
90
93
|
responseBody: Record<string, unknown>;
|
|
91
94
|
reportBarrierError?: unknown;
|
|
@@ -93,6 +96,7 @@ export type CompletedNonStreamingExecution = {
|
|
|
93
96
|
|
|
94
97
|
type CompletedNonStreamingAttempt = {
|
|
95
98
|
completion: CompletedProviderCall;
|
|
99
|
+
responseCapture?: ProviderResponseCapture;
|
|
96
100
|
};
|
|
97
101
|
|
|
98
102
|
export async function executeNonStreaming(args: {
|
|
@@ -116,7 +120,11 @@ export async function executeNonStreaming(args: {
|
|
|
116
120
|
});
|
|
117
121
|
}
|
|
118
122
|
return Response.json(result.responseBody, {
|
|
119
|
-
headers: {
|
|
123
|
+
headers: {
|
|
124
|
+
...result.choice.execution.headers,
|
|
125
|
+
...linkUsageHeaders(result),
|
|
126
|
+
"X-Request-Id": result.requestId,
|
|
127
|
+
},
|
|
120
128
|
});
|
|
121
129
|
}
|
|
122
130
|
|
|
@@ -170,9 +178,10 @@ export async function completeNonStreaming(args: {
|
|
|
170
178
|
}
|
|
171
179
|
|
|
172
180
|
const {
|
|
173
|
-
value: { completion },
|
|
181
|
+
value: { completion, responseCapture },
|
|
174
182
|
choice,
|
|
175
183
|
} = result;
|
|
184
|
+
const requestId = servedRequestId(args.requestId, choice.execution, responseCapture);
|
|
176
185
|
let output: ReturnType<typeof piMessageToInteractionOutput>;
|
|
177
186
|
let responseBody: Record<string, unknown>;
|
|
178
187
|
try {
|
|
@@ -182,7 +191,7 @@ export async function completeNonStreaming(args: {
|
|
|
182
191
|
args.interaction,
|
|
183
192
|
);
|
|
184
193
|
responseBody = args.protocol.responseBody({
|
|
185
|
-
id:
|
|
194
|
+
id: requestId,
|
|
186
195
|
model: choice.execution.model,
|
|
187
196
|
output,
|
|
188
197
|
modelCost: args.reporter.modelCost({
|
|
@@ -193,6 +202,7 @@ export async function completeNonStreaming(args: {
|
|
|
193
202
|
request: args.interaction,
|
|
194
203
|
routing: choice.decision,
|
|
195
204
|
conversationId: choice.execution.conversationId,
|
|
205
|
+
private: choice.execution.private,
|
|
196
206
|
});
|
|
197
207
|
} catch (error) {
|
|
198
208
|
await reportTerminalProviderFailure({
|
|
@@ -246,6 +256,7 @@ export async function completeNonStreaming(args: {
|
|
|
246
256
|
await args.reporter.waitForPrefixEntryWrite();
|
|
247
257
|
return {
|
|
248
258
|
choice,
|
|
259
|
+
requestId,
|
|
249
260
|
output,
|
|
250
261
|
responseBody,
|
|
251
262
|
...(reportBarrierError === undefined ? {} : { reportBarrierError }),
|
|
@@ -328,7 +339,7 @@ async function runCompletionAttempt(args: {
|
|
|
328
339
|
}
|
|
329
340
|
return {
|
|
330
341
|
kind: "succeeded",
|
|
331
|
-
value: { completion },
|
|
342
|
+
value: { completion, responseCapture: started.responseCapture },
|
|
332
343
|
latencyMs: completion.providerLatencyMs,
|
|
333
344
|
};
|
|
334
345
|
} catch (error) {
|
|
@@ -612,6 +623,7 @@ async function runStreamSetupAttempt(args: {
|
|
|
612
623
|
source: started.providerStream.source,
|
|
613
624
|
},
|
|
614
625
|
customerAbort: started.customerAbort,
|
|
626
|
+
responseCapture: started.responseCapture,
|
|
615
627
|
},
|
|
616
628
|
latencyMs: 0,
|
|
617
629
|
};
|
|
@@ -652,6 +664,7 @@ export async function executeStreaming(args: {
|
|
|
652
664
|
}): Promise<Response> {
|
|
653
665
|
const { execution } = args.choice;
|
|
654
666
|
const started = args.startedStream;
|
|
667
|
+
const requestId = servedRequestId(args.requestId, execution, started.responseCapture);
|
|
655
668
|
args.reporter.attachServingDecision(args.choice.decision);
|
|
656
669
|
const observed = observeDeliveredOutput(started.providerStream.events);
|
|
657
670
|
const finalizer = createStreamFinalizer({
|
|
@@ -671,13 +684,14 @@ export async function executeStreaming(args: {
|
|
|
671
684
|
});
|
|
672
685
|
return new Response(
|
|
673
686
|
args.protocol.streamBody({
|
|
674
|
-
id:
|
|
687
|
+
id: requestId,
|
|
675
688
|
model: execution.model,
|
|
676
689
|
routing: args.choice.decision,
|
|
677
690
|
events: observed.events,
|
|
678
691
|
source: started.providerStream.source,
|
|
679
692
|
request: args.interaction,
|
|
680
693
|
conversationId: execution.conversationId,
|
|
694
|
+
private: execution.private,
|
|
681
695
|
clientSignal: args.clientSignal,
|
|
682
696
|
modelCost: (message, failed) => args.reporter.modelCost({
|
|
683
697
|
model: args.choice.decision.selectedModel, reasoning_effort: args.choice.decision.reasoningEffort,
|
|
@@ -693,19 +707,34 @@ export async function executeStreaming(args: {
|
|
|
693
707
|
trackBackgroundTelemetry(finalizer.afterResponseDetached(outcome));
|
|
694
708
|
},
|
|
695
709
|
}),
|
|
696
|
-
{ headers: streamHeaders(execution) },
|
|
710
|
+
{ headers: streamHeaders(execution, requestId) },
|
|
697
711
|
);
|
|
698
712
|
}
|
|
699
713
|
|
|
700
|
-
function streamHeaders(execution: ProviderExecution): Record<string, string> {
|
|
714
|
+
function streamHeaders(execution: ProviderExecution, requestId: string): Record<string, string> {
|
|
701
715
|
return {
|
|
702
716
|
"Content-Type": "text/event-stream",
|
|
703
717
|
"Cache-Control": "no-cache",
|
|
704
718
|
"Connection": "keep-alive",
|
|
705
719
|
...execution.headers,
|
|
720
|
+
"X-Request-Id": requestId,
|
|
706
721
|
};
|
|
707
722
|
}
|
|
708
723
|
|
|
724
|
+
/**
|
|
725
|
+
* The request ID the client is told, in the response and its `X-Request-Id`:
|
|
726
|
+
* this gateway's own, except for a request hosted Dari served. Hosted logs
|
|
727
|
+
* that one under the ID it answered with, and Activity finds it only by that.
|
|
728
|
+
*/
|
|
729
|
+
function servedRequestId(
|
|
730
|
+
requestId: string,
|
|
731
|
+
execution: ProviderExecution,
|
|
732
|
+
capture: ProviderResponseCapture | undefined,
|
|
733
|
+
): string {
|
|
734
|
+
if (execution.provider !== HOSTED_DARI_PROVIDER) return requestId;
|
|
735
|
+
return capture?.succeeded?.get("x-request-id")?.trim() || requestId;
|
|
736
|
+
}
|
|
737
|
+
|
|
709
738
|
function customerAbortRequested(
|
|
710
739
|
clientSignal: AbortSignal | undefined,
|
|
711
740
|
): boolean {
|
|
@@ -89,6 +89,9 @@ export type ProviderExecution = ProviderCallConfig & {
|
|
|
89
89
|
subscriptionResetMinHours?: number;
|
|
90
90
|
// The thinking the provider was sent, as X-Dari-Thinking-Level reports it.
|
|
91
91
|
thinkingLevel: string;
|
|
92
|
+
// The request stays out of Activity (ResolvedRouter.private); the response
|
|
93
|
+
// says so, for status lines that would otherwise link to it.
|
|
94
|
+
private?: boolean;
|
|
92
95
|
usageContext: RoutingUsageContext | undefined;
|
|
93
96
|
headers: Record<string, string>;
|
|
94
97
|
};
|
|
@@ -181,6 +184,7 @@ export function prepareProviderExecution(args: {
|
|
|
181
184
|
: {}),
|
|
182
185
|
...(providerKeySource === "subscription" && supplied?.account ? { subscriptionAccount: supplied.account } : {}),
|
|
183
186
|
thinkingLevel,
|
|
187
|
+
...(args.credentials.private === true ? { private: true } : {}),
|
|
184
188
|
usageContext: usageContextFor({
|
|
185
189
|
billable: args.billable,
|
|
186
190
|
credentials: args.credentials,
|
|
@@ -198,6 +202,7 @@ export function prepareProviderExecution(args: {
|
|
|
198
202
|
? {}
|
|
199
203
|
: { "X-Dari-Lease-Turns-Remaining": String(args.decision.leaseTurnsRemaining) }),
|
|
200
204
|
...(args.decision.sessionPinned === true ? { "X-Dari-Session-Pinned": "true" } : {}),
|
|
205
|
+
...(args.credentials.private === true ? { "X-Dari-Private": "true" } : {}),
|
|
201
206
|
},
|
|
202
207
|
};
|
|
203
208
|
}
|
|
@@ -1,14 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import { HttpError } from "../../http/errors.js";
|
|
4
|
-
import type {
|
|
5
|
-
AgentRequestClass,
|
|
6
|
-
AgentRequestContext,
|
|
7
|
-
InteractionRequestPurpose,
|
|
8
|
-
} from "../../interaction/types.js";
|
|
1
|
+
import { openCodeSubagentId } from "../../harnesses/opencode.js";
|
|
9
2
|
import type { HarnessConversationIdentity } from "./conversation.js";
|
|
10
|
-
|
|
11
|
-
const MAX_ID_CHARS = 200;
|
|
3
|
+
import { boundedIdentity } from "./ids.js";
|
|
12
4
|
|
|
13
5
|
export function chatCompletionsConversationIdentity(
|
|
14
6
|
headers: Headers,
|
|
@@ -32,277 +24,3 @@ export function chatCompletionsConversationIdentity(
|
|
|
32
24
|
...(subagentId === undefined ? {} : { qualifiers: [subagentId] }),
|
|
33
25
|
};
|
|
34
26
|
}
|
|
35
|
-
|
|
36
|
-
export function piRequestContext(headers: Headers): AgentRequestContext | undefined {
|
|
37
|
-
if (boundedIdentity(headers.get("x-pi-session-id"), "x-pi-session-id") === undefined) {
|
|
38
|
-
return undefined;
|
|
39
|
-
}
|
|
40
|
-
const kind = headers.get("x-pi-request-kind")?.trim();
|
|
41
|
-
return {
|
|
42
|
-
requestClass: kind === "turn" ? "main"
|
|
43
|
-
: kind === "compaction" ? "compaction"
|
|
44
|
-
// The Dari extension's web search tool: one request beside the turn,
|
|
45
|
-
// served as background work like Claude Code's own search.
|
|
46
|
-
: kind === "search" ? "auxiliary"
|
|
47
|
-
: "unknown",
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// OpenCode names the session behind every request, and a subagent's parent
|
|
52
|
-
// session too. The `dari opencode` plugin adds the request kind and agent,
|
|
53
|
-
// which OpenCode does not send itself. Like Claude Code's headers, these are
|
|
54
|
-
// client-supplied routing hints, never inferred from prompt text.
|
|
55
|
-
export function openCodeRequestContext(headers: Headers): AgentRequestContext | undefined {
|
|
56
|
-
if (boundedIdentity(headers.get("x-opencode-session-id"), "x-opencode-session-id") === undefined) {
|
|
57
|
-
return undefined;
|
|
58
|
-
}
|
|
59
|
-
const kind = headers.get("x-opencode-request-kind")?.trim();
|
|
60
|
-
const subagentId = openCodeSubagentId(headers);
|
|
61
|
-
const requestClass: AgentRequestClass = kind === "compaction" ? "compaction"
|
|
62
|
-
: kind === "title" || kind === "generate" ? "auxiliary"
|
|
63
|
-
: subagentId === undefined ? "main" : "subagent";
|
|
64
|
-
return {
|
|
65
|
-
requestClass,
|
|
66
|
-
...(subagentId === undefined ? {} : {
|
|
67
|
-
agentId: subagentId,
|
|
68
|
-
parentAgentId: boundedIdentity(
|
|
69
|
-
headers.get("x-opencode-parent-session-id"),
|
|
70
|
-
"x-opencode-parent-session-id",
|
|
71
|
-
),
|
|
72
|
-
}),
|
|
73
|
-
agentType: boundedIdentity(headers.get("x-opencode-agent"), "x-opencode-agent"),
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function openCodeSubagentId(headers: Headers): string | undefined {
|
|
78
|
-
const parent = boundedIdentity(
|
|
79
|
-
headers.get("x-opencode-parent-session-id"),
|
|
80
|
-
"x-opencode-parent-session-id",
|
|
81
|
-
);
|
|
82
|
-
return parent === undefined
|
|
83
|
-
? undefined
|
|
84
|
-
: boundedIdentity(headers.get("x-opencode-session-id"), "x-opencode-session-id");
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export function codexConversationIdentity(
|
|
88
|
-
headers: Headers,
|
|
89
|
-
payload: Record<string, unknown>,
|
|
90
|
-
): HarnessConversationIdentity | undefined {
|
|
91
|
-
const clientMetadata = isRecord(payload.client_metadata)
|
|
92
|
-
? payload.client_metadata
|
|
93
|
-
: undefined;
|
|
94
|
-
const turnMetadata = codexTurnMetadataCollection(headers, payload);
|
|
95
|
-
if (turnMetadata.length === 0) {
|
|
96
|
-
return undefined;
|
|
97
|
-
}
|
|
98
|
-
// Clients can carry a stale projection after a resume or fork. Conflicts
|
|
99
|
-
// must not fail the request, and fields must not mix across projections:
|
|
100
|
-
// take thread, turn, and parent from one turn metadata projection, header
|
|
101
|
-
// first, falling back to the standalone thread fields only for the thread.
|
|
102
|
-
const entry = turnMetadata.find((metadata) => metadata.thread_id !== undefined)
|
|
103
|
-
?? turnMetadata[0]!;
|
|
104
|
-
const threadId = uuidIdentity(entry.thread_id, "x-codex-turn-metadata.thread_id")
|
|
105
|
-
?? uuidIdentity(headers.get("thread-id"), "thread-id")
|
|
106
|
-
?? uuidIdentity(clientMetadata?.thread_id, "client_metadata.thread_id");
|
|
107
|
-
if (threadId === undefined) {
|
|
108
|
-
return undefined;
|
|
109
|
-
}
|
|
110
|
-
const userTurnId = uuidIdentity(entry.turn_id, "x-codex-turn-metadata.turn_id");
|
|
111
|
-
const parentThreadId = uuidIdentity(entry.parent_thread_id,
|
|
112
|
-
"x-codex-turn-metadata.parent_thread_id");
|
|
113
|
-
return {
|
|
114
|
-
kind: "codex_thread",
|
|
115
|
-
value: threadId,
|
|
116
|
-
...(parentThreadId === undefined ? {} : { parentThreadId }),
|
|
117
|
-
...(userTurnId === undefined ? {} : { userTurnId }),
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export function codexRequestPurpose(
|
|
122
|
-
headers: Headers,
|
|
123
|
-
payload: Record<string, unknown>,
|
|
124
|
-
): InteractionRequestPurpose | undefined {
|
|
125
|
-
const turnMetadata = codexTurnMetadataCollection(headers, payload);
|
|
126
|
-
return turnMetadata.length > 0 && turnMetadata.every(isCodexMemoryExtraction)
|
|
127
|
-
? "memory_extraction"
|
|
128
|
-
: undefined;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
export function codexRequestContext(
|
|
132
|
-
headers: Headers,
|
|
133
|
-
payload: Record<string, unknown>,
|
|
134
|
-
): AgentRequestContext | undefined {
|
|
135
|
-
const metadata = codexTurnMetadataCollection(headers, payload);
|
|
136
|
-
if (metadata.length === 0) return undefined;
|
|
137
|
-
// Classify from one projection, header first, skipping one that omits the
|
|
138
|
-
// request kind so a partial header falls back to a complete body.
|
|
139
|
-
const entry = metadata.find((candidate) => candidate.request_kind !== undefined)
|
|
140
|
-
?? metadata[0]!;
|
|
141
|
-
const kind = entry.request_kind;
|
|
142
|
-
const agentKind = entry.subagent_kind;
|
|
143
|
-
const agentId = uuidIdentity(entry.thread_id, "x-codex-turn-metadata.thread_id");
|
|
144
|
-
const requestClass = kind === "compaction" ? "compaction"
|
|
145
|
-
: kind === "memory" || kind === "prewarm" ? "auxiliary"
|
|
146
|
-
: kind === "turn" && agentKind === "memory_consolidation" ? "auxiliary"
|
|
147
|
-
: kind === "turn" && agentKind === "compact" ? "compaction"
|
|
148
|
-
: kind === "turn" && agentKind !== undefined ? "subagent"
|
|
149
|
-
: kind === "turn" ? "main" : "unknown";
|
|
150
|
-
return {
|
|
151
|
-
requestClass,
|
|
152
|
-
...(requestClass === "subagent" && agentId !== undefined ? { agentId } : {}),
|
|
153
|
-
parentAgentId: uuidIdentity(entry.parent_thread_id, "x-codex-turn-metadata.parent_thread_id"),
|
|
154
|
-
agentType: boundedIdentity(agentKind, "x-codex-turn-metadata.subagent_kind"),
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// Muse Code sends its thread on every Responses request. The main agent's
|
|
159
|
-
// thread is its session, and each subagent has its own thread, so each keeps
|
|
160
|
-
// its own conversation and lease.
|
|
161
|
-
export function museCodeConversationIdentity(
|
|
162
|
-
headers: Headers,
|
|
163
|
-
): HarnessConversationIdentity | undefined {
|
|
164
|
-
const threadId = boundedIdentity(headers.get("x-tbh-thread-id"), "x-tbh-thread-id");
|
|
165
|
-
return threadId === undefined ? undefined : { kind: "muse_code_thread", value: threadId };
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
export function claudeCodeConversationIdentity(
|
|
169
|
-
headers: Headers,
|
|
170
|
-
metadataSessionId?: string,
|
|
171
|
-
): HarnessConversationIdentity | undefined {
|
|
172
|
-
const candidates = [
|
|
173
|
-
uuidIdentity(
|
|
174
|
-
headers.get("x-claude-code-session-id"),
|
|
175
|
-
"x-claude-code-session-id",
|
|
176
|
-
),
|
|
177
|
-
uuidIdentity(
|
|
178
|
-
metadataSessionId,
|
|
179
|
-
"metadata.user_id.session_id",
|
|
180
|
-
),
|
|
181
|
-
].filter((value): value is string => value !== undefined);
|
|
182
|
-
if (candidates.length === 0) return undefined;
|
|
183
|
-
// Claude Code can carry a stale metadata session after a resume or fork.
|
|
184
|
-
// The header names the live session, so it wins over the metadata.
|
|
185
|
-
const [sessionId] = candidates;
|
|
186
|
-
// Claude Code reuses the parent session UUID for every subagent, but gives
|
|
187
|
-
// each subagent a stable agent header. Keep the parent thread on the bare
|
|
188
|
-
// session identity and scope each subagent to its own composite identity.
|
|
189
|
-
// Treat this as an opaque Claude identifier. Besides ordinary subagent IDs,
|
|
190
|
-
// Claude may send labeled agents or Agent Teams identities such as
|
|
191
|
-
// `reviewer@backend`; Dari only needs a stable, bounded value to hash.
|
|
192
|
-
const agentId = boundedIdentity(
|
|
193
|
-
headers.get("x-claude-code-agent-id"),
|
|
194
|
-
"x-claude-code-agent-id",
|
|
195
|
-
);
|
|
196
|
-
return {
|
|
197
|
-
kind: "claude_code_session",
|
|
198
|
-
value: sessionId,
|
|
199
|
-
...(agentId === undefined ? {} : { qualifiers: [agentId] }),
|
|
200
|
-
};
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
function isCodexMemoryExtraction(
|
|
204
|
-
metadata: Record<string, unknown>,
|
|
205
|
-
): boolean {
|
|
206
|
-
if (
|
|
207
|
-
metadata.request_kind !== "memory" ||
|
|
208
|
-
metadata.thread_source !== "memory_consolidation"
|
|
209
|
-
) {
|
|
210
|
-
return false;
|
|
211
|
-
}
|
|
212
|
-
// Codex deliberately omits canonical turn identity for detached phase-1
|
|
213
|
-
// memory jobs, while compatibility projections still carry the live root
|
|
214
|
-
// thread UUID. Phase-2 consolidation agents use ordinary turn metadata and
|
|
215
|
-
// their own thread identities, so they do not match this shape.
|
|
216
|
-
return [
|
|
217
|
-
"session_id",
|
|
218
|
-
"thread_id",
|
|
219
|
-
"turn_id",
|
|
220
|
-
"root_turn_id",
|
|
221
|
-
"window_id",
|
|
222
|
-
"forked_from_thread_id",
|
|
223
|
-
].every((field) => metadata[field] === undefined);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
function codexTurnMetadataCollection(
|
|
227
|
-
headers: Headers,
|
|
228
|
-
payload: Record<string, unknown>,
|
|
229
|
-
): Record<string, unknown>[] {
|
|
230
|
-
const clientMetadata = isRecord(payload.client_metadata)
|
|
231
|
-
? payload.client_metadata
|
|
232
|
-
: undefined;
|
|
233
|
-
return [
|
|
234
|
-
codexTurnMetadata(
|
|
235
|
-
headers.get("x-codex-turn-metadata"),
|
|
236
|
-
"x-codex-turn-metadata",
|
|
237
|
-
),
|
|
238
|
-
codexTurnMetadata(
|
|
239
|
-
clientMetadata?.["x-codex-turn-metadata"],
|
|
240
|
-
"client_metadata.x-codex-turn-metadata",
|
|
241
|
-
),
|
|
242
|
-
].filter((value): value is Record<string, unknown> => value !== undefined);
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
function boundedIdentity(value: unknown, field: string): string | undefined {
|
|
246
|
-
if (value === undefined || value === null) return undefined;
|
|
247
|
-
if (typeof value !== "string") {
|
|
248
|
-
throw new HttpError(
|
|
249
|
-
400,
|
|
250
|
-
`${field} must be a string.`,
|
|
251
|
-
"invalid_request_error",
|
|
252
|
-
field,
|
|
253
|
-
);
|
|
254
|
-
}
|
|
255
|
-
const normalized = value.trim();
|
|
256
|
-
if (!normalized) return undefined;
|
|
257
|
-
if (normalized.length > MAX_ID_CHARS) {
|
|
258
|
-
throw new HttpError(
|
|
259
|
-
400,
|
|
260
|
-
`${field} must not exceed ${MAX_ID_CHARS} characters.`,
|
|
261
|
-
"invalid_request_error",
|
|
262
|
-
field,
|
|
263
|
-
);
|
|
264
|
-
}
|
|
265
|
-
return normalized;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
function uuidIdentity(value: unknown, field: string): string | undefined {
|
|
269
|
-
const normalized = boundedIdentity(value, field);
|
|
270
|
-
if (normalized === undefined) return undefined;
|
|
271
|
-
const canonical = normalized.toLowerCase();
|
|
272
|
-
if (!/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/.test(canonical)) {
|
|
273
|
-
throw new HttpError(
|
|
274
|
-
400,
|
|
275
|
-
`${field} must be a UUID.`,
|
|
276
|
-
"invalid_request_error",
|
|
277
|
-
field,
|
|
278
|
-
);
|
|
279
|
-
}
|
|
280
|
-
return canonical;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
function codexTurnMetadata(
|
|
284
|
-
raw: unknown,
|
|
285
|
-
field: string,
|
|
286
|
-
): Record<string, unknown> | undefined {
|
|
287
|
-
if (raw === undefined || raw === null) return undefined;
|
|
288
|
-
if (typeof raw !== "string") {
|
|
289
|
-
throw new HttpError(
|
|
290
|
-
400,
|
|
291
|
-
`${field} must be a JSON string.`,
|
|
292
|
-
"invalid_request_error",
|
|
293
|
-
field,
|
|
294
|
-
);
|
|
295
|
-
}
|
|
296
|
-
try {
|
|
297
|
-
const parsed: unknown = JSON.parse(raw);
|
|
298
|
-
if (!isRecord(parsed)) throw new Error("not an object");
|
|
299
|
-
return parsed;
|
|
300
|
-
} catch {
|
|
301
|
-
throw new HttpError(
|
|
302
|
-
400,
|
|
303
|
-
`${field} must contain a JSON object.`,
|
|
304
|
-
"invalid_request_error",
|
|
305
|
-
field,
|
|
306
|
-
);
|
|
307
|
-
}
|
|
308
|
-
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { HttpError } from "../../http/errors.js";
|
|
2
|
+
|
|
3
|
+
const MAX_ID_CHARS = 200;
|
|
4
|
+
|
|
5
|
+
export function boundedIdentity(value: unknown, field: string): string | undefined {
|
|
6
|
+
if (value === undefined || value === null) return undefined;
|
|
7
|
+
if (typeof value !== "string") {
|
|
8
|
+
throw new HttpError(
|
|
9
|
+
400,
|
|
10
|
+
`${field} must be a string.`,
|
|
11
|
+
"invalid_request_error",
|
|
12
|
+
field,
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
const normalized = value.trim();
|
|
16
|
+
if (!normalized) return undefined;
|
|
17
|
+
if (normalized.length > MAX_ID_CHARS) {
|
|
18
|
+
throw new HttpError(
|
|
19
|
+
400,
|
|
20
|
+
`${field} must not exceed ${MAX_ID_CHARS} characters.`,
|
|
21
|
+
"invalid_request_error",
|
|
22
|
+
field,
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
return normalized;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function uuidIdentity(value: unknown, field: string): string | undefined {
|
|
29
|
+
const normalized = boundedIdentity(value, field);
|
|
30
|
+
if (normalized === undefined) return undefined;
|
|
31
|
+
const canonical = normalized.toLowerCase();
|
|
32
|
+
if (!/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/.test(canonical)) {
|
|
33
|
+
throw new HttpError(
|
|
34
|
+
400,
|
|
35
|
+
`${field} must be a UUID.`,
|
|
36
|
+
"invalid_request_error",
|
|
37
|
+
field,
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
return canonical;
|
|
41
|
+
}
|
|
@@ -5,13 +5,4 @@ export {
|
|
|
5
5
|
type HarnessConversationIdentity,
|
|
6
6
|
type HarnessConversationScope,
|
|
7
7
|
} from "./conversation.js";
|
|
8
|
-
export {
|
|
9
|
-
chatCompletionsConversationIdentity,
|
|
10
|
-
claudeCodeConversationIdentity,
|
|
11
|
-
codexConversationIdentity,
|
|
12
|
-
codexRequestPurpose,
|
|
13
|
-
codexRequestContext,
|
|
14
|
-
museCodeConversationIdentity,
|
|
15
|
-
openCodeRequestContext,
|
|
16
|
-
piRequestContext,
|
|
17
|
-
} from "./extract.js";
|
|
8
|
+
export { chatCompletionsConversationIdentity } from "./extract.js";
|
|
@@ -76,6 +76,7 @@ export async function prepareRoutedCompletion(args: {
|
|
|
76
76
|
requestId: string;
|
|
77
77
|
clientRequestId: string | null;
|
|
78
78
|
tagsHeader: string | null;
|
|
79
|
+
privateRequest?: boolean;
|
|
79
80
|
clientHarness?: ClientHarness;
|
|
80
81
|
clientHeaders?: Record<string, string>;
|
|
81
82
|
harnessConversationIdentity?: HarnessConversationIdentity;
|
|
@@ -88,6 +89,7 @@ export async function prepareRoutedCompletion(args: {
|
|
|
88
89
|
apiKey: args.apiKey,
|
|
89
90
|
includePublicEvalCatalog: true,
|
|
90
91
|
clientHeaders: args.clientHeaders,
|
|
92
|
+
...(args.privateRequest ? { private: true } : {}),
|
|
91
93
|
};
|
|
92
94
|
// Direct /v1 callers are authenticated by API key, so their canonical
|
|
93
95
|
// organization router is not known until this resolve completes. Fingerprint
|
|
@@ -177,6 +179,8 @@ export async function prepareRoutedCompletion(args: {
|
|
|
177
179
|
const strictAccess = strictProviderAccess(credentials, roleModel.interaction);
|
|
178
180
|
const access = linkGrantAccess(strictAccess.credentials, strictAccess.interaction);
|
|
179
181
|
credentials = access.credentials;
|
|
182
|
+
// A Dari Link app's usage is read back from its requests, so they are kept.
|
|
183
|
+
if (args.privateRequest && !credentials.link_grant_id) credentials = { ...credentials, private: true };
|
|
180
184
|
// A child token's tags label every request it makes, whatever the request says.
|
|
181
185
|
Object.assign(tags, credentials.link_tags);
|
|
182
186
|
args = {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
|
|
3
3
|
import { isBackgroundRequest, type InteractionRequest } from "../interaction/types.js";
|
|
4
|
+
import { PRIVATE_HEADER } from "../platform/routing_contract.js";
|
|
4
5
|
import { explicitModelId } from "../protocol/input/router_model.js";
|
|
5
6
|
import { captureRequestError } from "../observability/errors.js";
|
|
6
7
|
import type { SelectorBackend } from "../strategies/routing.js";
|
|
@@ -42,6 +43,7 @@ export async function handleRoutedCompletion(args: {
|
|
|
42
43
|
requestId,
|
|
43
44
|
clientRequestId,
|
|
44
45
|
tagsHeader: args.request.headers.get("x-dari-tags"),
|
|
46
|
+
privateRequest: args.request.headers.get(PRIVATE_HEADER) === "true",
|
|
45
47
|
clientHarness: args.clientHarness,
|
|
46
48
|
clientHeaders: clientHeaders(args.request.headers),
|
|
47
49
|
harnessConversationIdentity: args.harnessConversationIdentity,
|
|
@@ -76,7 +78,8 @@ export async function handleRoutedCompletion(args: {
|
|
|
76
78
|
for (const [name, value] of Object.entries(args.responseHeaders?.(prepared.credentials) ?? {})) {
|
|
77
79
|
response.headers.set(name, value);
|
|
78
80
|
}
|
|
79
|
-
|
|
81
|
+
// Its X-Request-Id is the request the client is told it made.
|
|
82
|
+
return response;
|
|
80
83
|
} catch (error) {
|
|
81
84
|
const terminal = prepared.reporter.freezeFailure(error);
|
|
82
85
|
terminal.report();
|
|
@@ -10,6 +10,7 @@ export type RoutedCompletionResponseArgs = {
|
|
|
10
10
|
request: InteractionRequest;
|
|
11
11
|
routing: RoutingDecision;
|
|
12
12
|
conversationId?: string;
|
|
13
|
+
private?: boolean;
|
|
13
14
|
};
|
|
14
15
|
|
|
15
16
|
export type RoutedCompletionStreamArgs = {
|
|
@@ -20,6 +21,7 @@ export type RoutedCompletionStreamArgs = {
|
|
|
20
21
|
source: ProviderIdentity;
|
|
21
22
|
request: InteractionRequest;
|
|
22
23
|
conversationId?: string;
|
|
24
|
+
private?: boolean;
|
|
23
25
|
onFinal?: (outcome: ProviderStreamOutcome) => void;
|
|
24
26
|
modelCost?: (message: Record<string, unknown>, failed: boolean) => number | null;
|
|
25
27
|
beforeClose?: (outcome: ProviderStreamOutcome) => Promise<void> | void;
|
package/src/self_host/config.ts
CHANGED
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
type ReasoningEffort,
|
|
6
6
|
} from "@mupt-ai/dari-router/policy-engine";
|
|
7
7
|
|
|
8
|
-
import { CLIENT_HARNESSES } from "../
|
|
8
|
+
import { CLIENT_HARNESSES } from "../harnesses/index.js";
|
|
9
9
|
import { checkSources, DEFAULT_RESET_MIN_HOURS, type Source } from "../models/sources.js";
|
|
10
10
|
import { modelKeyProviders, otherModelProviders, providerModel } from "../models/canonical.js";
|
|
11
11
|
import type { ClientHarness, RouterRole, RouterRoleModels, RouterRolePrompts } from "../types.js";
|
|
@@ -5,10 +5,11 @@ import { anthropicToken, bearerToken } from "../http/auth.js";
|
|
|
5
5
|
import { errorResponse, HttpError } from "../http/errors.js";
|
|
6
6
|
import { anthropicErrorResponse } from "../ingress/anthropic_messages.js";
|
|
7
7
|
import type { GatewayPlatform } from "../platform/platform.js";
|
|
8
|
-
import { DEFAULT_ROUTER_ALIAS, HOSTED_DARI_PROVIDER } from "../platform/routing_contract.js";
|
|
8
|
+
import { DEFAULT_ROUTER_ALIAS, HOSTED_DARI_PROVIDER, PRIVATE_HEADER } from "../platform/routing_contract.js";
|
|
9
9
|
import type { Source } from "../models/sources.js";
|
|
10
10
|
import { parseGatewayConfig, type RouterConfig } from "./config.js";
|
|
11
11
|
import type { HostedSync } from "./hosted_sync.js";
|
|
12
|
+
import type { PrivateMode } from "./private_mode.js";
|
|
12
13
|
import { createSelfHostedPlatform } from "./platform.js";
|
|
13
14
|
import { gatewayKeyIndex, resolveSelfHostedRouter } from "./resolve.js";
|
|
14
15
|
|
|
@@ -28,9 +29,11 @@ import { gatewayKeyIndex, resolveSelfHostedRouter } from "./resolve.js";
|
|
|
28
29
|
* reports, goes to hosted with the user's Dari key.
|
|
29
30
|
* - The requests served here sync to hosted Activity (hosted_sync.ts), each
|
|
30
31
|
* naming the plan that served it. The plans themselves never leave this
|
|
31
|
-
* computer.
|
|
32
|
-
*
|
|
33
|
-
*
|
|
32
|
+
* computer. Requests hosted Dari served are hosted's to log. Dari Link and
|
|
33
|
+
* Activity are hosted's, not served here.
|
|
34
|
+
* - In private mode (private_mode.ts) every request is private
|
|
35
|
+
* (PRIVATE_HEADER): none syncs, and hosted Dari keeps only what billing
|
|
36
|
+
* needs of those it serves.
|
|
34
37
|
*/
|
|
35
38
|
export type HostedConnection = {
|
|
36
39
|
// The hosted routing service, e.g. https://routing.dari.dev.
|
|
@@ -81,14 +84,9 @@ export function createConnectedPlatform(deps: SelfHostedPlatformDeps & {
|
|
|
81
84
|
},
|
|
82
85
|
recordRequest(record) {
|
|
83
86
|
console.info("routing request", JSON.stringify(record.summary));
|
|
84
|
-
// Hosted Dari logs the requests it served itself
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
// does one whose plan is gone, since it may have been private.
|
|
88
|
-
if (record.subscriptionCredentialId !== undefined) {
|
|
89
|
-
const plan = deps.subscriptions.get(record.subscriptionCredentialId);
|
|
90
|
-
if (plan === null || plan.private) return;
|
|
91
|
-
}
|
|
87
|
+
// Hosted Dari logs the requests it served itself, and private ones
|
|
88
|
+
// stay on this computer.
|
|
89
|
+
if (record.summary.selected_provider === HOSTED_DARI_PROVIDER || record.private) return;
|
|
92
90
|
try {
|
|
93
91
|
deps.sync.enqueue(record.row);
|
|
94
92
|
} catch (error) {
|
|
@@ -185,9 +183,15 @@ export function connectedHandler(deps: {
|
|
|
185
183
|
gatewayKeys: readonly string[];
|
|
186
184
|
hosted: HostedConnection;
|
|
187
185
|
hostedRouter: HostedRouters;
|
|
186
|
+
privateMode: PrivateMode;
|
|
188
187
|
fetch: typeof fetch;
|
|
189
188
|
}): (request: Request) => Promise<Response> {
|
|
190
189
|
return async (request) => {
|
|
190
|
+
if (deps.privateMode.on) {
|
|
191
|
+
const headers = new Headers(request.headers);
|
|
192
|
+
headers.set(PRIVATE_HEADER, "true");
|
|
193
|
+
request = new Request(request, { headers });
|
|
194
|
+
}
|
|
191
195
|
const { pathname } = new URL(request.url);
|
|
192
196
|
if (pathname === "/healthz" || pathname === "/health" || pathname === "/api/hello") {
|
|
193
197
|
return await deps.gateway(request);
|
|
@@ -94,7 +94,7 @@ export class HostedSync {
|
|
|
94
94
|
const onDari = await readHostedAccounts(this.hosted, this.fetch);
|
|
95
95
|
this.accountsFailures = 0;
|
|
96
96
|
for (const plan of this.subscriptions.list()) {
|
|
97
|
-
if (plan.owner !== null || plan.
|
|
97
|
+
if (plan.owner !== null || plan.accountId === null) continue;
|
|
98
98
|
const moved = onDari.some((account) => account.provider === plan.provider
|
|
99
99
|
&& account.accountId === plan.accountId
|
|
100
100
|
&& account.connectedAt !== undefined && account.connectedAt > plan.createdAt);
|