@mupt-ai/dari-gateway 1.0.369 → 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/providers/execution.d.ts +1 -0
- package/dist/providers/execution.js +27 -7
- 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/routed_completion.js +2 -1
- package/dist/self_host/config.js +1 -1
- package/dist/self_host/router_models.js +1 -1
- package/dist/types.d.ts +1 -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/providers/execution.ts +34 -7
- 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/routed_completion.ts +2 -1
- package/src/self_host/config.ts +1 -1
- package/src/self_host/router_models.ts +1 -1
- package/src/types.ts +2 -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
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { AgentRequestContext } from "../interaction/types.js";
|
|
2
|
+
import { boundedIdentity } from "../runtime/harness_identity/ids.js";
|
|
3
|
+
import type { Harness } from "./index.js";
|
|
4
|
+
|
|
5
|
+
export const pi = {
|
|
6
|
+
id: "pi",
|
|
7
|
+
headers: ["x-pi-session-id", "x-pi-request-kind"],
|
|
8
|
+
} as const satisfies Harness;
|
|
9
|
+
|
|
10
|
+
export function piRequestContext(headers: Headers): AgentRequestContext | undefined {
|
|
11
|
+
if (boundedIdentity(headers.get("x-pi-session-id"), "x-pi-session-id") === undefined) {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
const kind = headers.get("x-pi-request-kind")?.trim();
|
|
15
|
+
return {
|
|
16
|
+
requestClass: kind === "turn" ? "main"
|
|
17
|
+
: kind === "compaction" ? "compaction"
|
|
18
|
+
// The Dari extension's web search tool: one request beside the turn,
|
|
19
|
+
// served as background work like Claude Code's own search.
|
|
20
|
+
: kind === "search" ? "auxiliary"
|
|
21
|
+
: "unknown",
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// OpenCode names the session behind every request, and a subagent's parent
|
|
26
|
+
// session too. The `dari opencode` plugin adds the request kind and agent,
|
|
27
|
+
// which OpenCode does not send itself. Like Claude Code's headers, these are
|
|
28
|
+
// client-supplied routing hints, never inferred from prompt text.
|
|
@@ -1,46 +1,9 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { CLIENT_HARNESSES, HARNESSES, OWN_PLAN_HARNESSES, type ClientHarness } from "../harnesses/index.js";
|
|
2
2
|
|
|
3
3
|
export const CLIENT_HARNESS_HEADER = "x-dari-client-harness";
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
"codex",
|
|
8
|
-
"muse_code",
|
|
9
|
-
"opencode",
|
|
10
|
-
"pi",
|
|
11
|
-
"openclaw",
|
|
12
|
-
"hermes",
|
|
13
|
-
] satisfies ClientHarness[];
|
|
14
|
-
|
|
15
|
-
// The headers a coding agent describes itself and each request with: which
|
|
16
|
-
// agent it is, its session, and what the request is for. harness_request.ts
|
|
17
|
-
// reads every one of them.
|
|
18
|
-
const CLIENT_HEADERS = [
|
|
19
|
-
// Claude Code
|
|
20
|
-
"user-agent",
|
|
21
|
-
"x-app",
|
|
22
|
-
"anthropic-beta",
|
|
23
|
-
"x-claude-code-session-id",
|
|
24
|
-
"x-claude-code-request-class",
|
|
25
|
-
"x-claude-code-agent-id",
|
|
26
|
-
"x-claude-code-parent-agent-id",
|
|
27
|
-
"x-claude-code-agent-type",
|
|
28
|
-
// Codex
|
|
29
|
-
"x-codex-turn-metadata",
|
|
30
|
-
"thread-id",
|
|
31
|
-
// Muse Code
|
|
32
|
-
"x-client-id",
|
|
33
|
-
"x-tbh-thread-id",
|
|
34
|
-
// OpenCode, with the `dari opencode` plugin's request kind and agent
|
|
35
|
-
"x-opencode-session-id",
|
|
36
|
-
"x-opencode-parent-session-id",
|
|
37
|
-
"x-opencode-request-kind",
|
|
38
|
-
"x-opencode-agent",
|
|
39
|
-
"x-session-affinity",
|
|
40
|
-
// Pi
|
|
41
|
-
"x-pi-session-id",
|
|
42
|
-
"x-pi-request-kind",
|
|
43
|
-
];
|
|
5
|
+
// Every coding agent's own headers. harness_request.ts reads every one of them.
|
|
6
|
+
const CLIENT_HEADERS = HARNESSES.flatMap((harness) => harness.headers);
|
|
44
7
|
|
|
45
8
|
/**
|
|
46
9
|
* The headers of `CLIENT_HEADERS` the request carries, as the client sent
|
|
@@ -72,5 +35,5 @@ export function namedClientHarness(headers: Headers): ClientHarness | undefined
|
|
|
72
35
|
*/
|
|
73
36
|
export function claimedClientHarness(headers: Headers): ClientHarness | undefined {
|
|
74
37
|
const named = namedClientHarness(headers);
|
|
75
|
-
return named ===
|
|
38
|
+
return named === undefined || OWN_PLAN_HARNESSES.includes(named) ? undefined : named;
|
|
76
39
|
}
|
|
@@ -4,20 +4,20 @@ import type {
|
|
|
4
4
|
SourceProtocol,
|
|
5
5
|
} from "../interaction/types.js";
|
|
6
6
|
import {
|
|
7
|
-
chatCompletionsConversationIdentity,
|
|
8
7
|
claudeCodeConversationIdentity,
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
claudeCodeRequestContext,
|
|
9
|
+
isClaudeCodeRequest,
|
|
10
|
+
} from "../harnesses/claude_code.js";
|
|
11
|
+
import { codexConversationIdentity, codexRequestContext, codexRequestPurpose } from "../harnesses/codex.js";
|
|
12
|
+
import { museCodeClientId, museCodeConversationIdentity } from "../harnesses/muse_code.js";
|
|
13
|
+
import { openCodeRequestContext } from "../harnesses/opencode.js";
|
|
14
|
+
import { piRequestContext } from "../harnesses/pi.js";
|
|
15
|
+
import {
|
|
16
|
+
chatCompletionsConversationIdentity,
|
|
15
17
|
type HarnessConversationIdentity,
|
|
16
18
|
} from "../runtime/harness_identity/index.js";
|
|
17
19
|
import type { ClientHarness } from "../types.js";
|
|
18
|
-
import { claudeCodeRequestContext, isClaudeCodeRequest } from "./claude_code/request_context.js";
|
|
19
20
|
import { claimedClientHarness } from "./client_harness.js";
|
|
20
|
-
import { museCodeClientId } from "./muse_code.js";
|
|
21
21
|
|
|
22
22
|
/** The coding agent a request comes from, and what the agent says of it. */
|
|
23
23
|
export type HarnessRequest = {
|
package/src/ingress/models.ts
CHANGED
|
@@ -16,6 +16,7 @@ import { modelSupportsImageInput, modelSupportsNativeCompaction } from "../provi
|
|
|
16
16
|
import { modelMetadataLookup } from "../providers/pi.js";
|
|
17
17
|
import { resolveRouter } from "../strategies/routing.js";
|
|
18
18
|
import { anthropicModelsResponse } from "./anthropic_models.js";
|
|
19
|
+
import { OWN_PLAN_HARNESSES } from "../harnesses/index.js";
|
|
19
20
|
import { namedClientHarness } from "./client_harness.js";
|
|
20
21
|
import { codexModelsEtag, codexModelsResponse } from "./codex_models.js";
|
|
21
22
|
import { CANONICAL_ROUTER_MODEL, explicitModelSlug, LINK_CHILD_TOKEN_PREFIX } from "../protocol/input/router_model.js";
|
|
@@ -101,7 +102,7 @@ export async function handleModels(request: Request, routerId: string): Promise<
|
|
|
101
102
|
// A child token's one model, when it serves only Claude Code or Muse Code.
|
|
102
103
|
async function childTokenModels(routerId: string, apiKey: string): Promise<string[]> {
|
|
103
104
|
if (!apiKey.startsWith(LINK_CHILD_TOKEN_PREFIX)) return [];
|
|
104
|
-
for (const clientHarness of
|
|
105
|
+
for (const clientHarness of OWN_PLAN_HARNESSES) {
|
|
105
106
|
try {
|
|
106
107
|
const router = await resolveRouter({ routerId, apiKey, clientHarness, provisional: true });
|
|
107
108
|
return router.link_models ?? router.enabled_models;
|
package/src/link/endpoints.ts
CHANGED
|
@@ -94,6 +94,7 @@ import { catalogModelFor } from "./grant_access.js";
|
|
|
94
94
|
import { registeredRedirect } from "./grants.js";
|
|
95
95
|
import { requestTags } from "../runtime/request_tags.js";
|
|
96
96
|
import type { ClientHarness, ResolvedRouter, SubscriptionKind } from "../types.js";
|
|
97
|
+
import { CLIENT_HARNESSES } from "../harnesses/index.js";
|
|
97
98
|
import { parseJsonRequest } from "../ingress/json.js";
|
|
98
99
|
import { DARI_MARK, ICONS, linkLogo } from "./assets.js";
|
|
99
100
|
import { GEIST_WOFF2 } from "./font.js";
|
|
@@ -155,9 +156,6 @@ const CHILD_TOKEN_MAX_SECONDS = 12 * 60 * 60;
|
|
|
155
156
|
const MAX_MODEL_CHARS = 200;
|
|
156
157
|
const MAX_PURPOSE_CHARS = 200;
|
|
157
158
|
const MAX_CHILD_MODELS = 16;
|
|
158
|
-
// Clients an app can say it runs with `client` on the authorize request.
|
|
159
|
-
// Claude Code and Muse Code plans serve only an app that runs those.
|
|
160
|
-
const CLIENTS: readonly ClientHarness[] = ["claude_code", "codex", "muse_code", "opencode", "pi", "openclaw", "hermes"];
|
|
161
159
|
const PLAN_KINDS = Object.keys(SUBSCRIPTION_PLAN_LABELS) as SubscriptionKind[];
|
|
162
160
|
// An API key, as a kind an app asks for in `plans` and reads on a grant.
|
|
163
161
|
const API_KEY = "api_key";
|
|
@@ -1270,8 +1268,10 @@ async function authorizeRequest(link: LinkPlatform, params: URLSearchParams): Pr
|
|
|
1270
1268
|
return badRequest(`The only scope is ${LINK_SCOPE}.`);
|
|
1271
1269
|
}
|
|
1272
1270
|
const clients = listParam(params.get("client"));
|
|
1273
|
-
|
|
1274
|
-
|
|
1271
|
+
// The clients an app can say it runs. Claude Code and Muse Code plans serve
|
|
1272
|
+
// only an app that runs those.
|
|
1273
|
+
if (!clients.every((client) => CLIENT_HARNESSES.includes(client))) {
|
|
1274
|
+
return badRequest(`client lists the clients the app runs: ${CLIENT_HARNESSES.join(", ")}.`);
|
|
1275
1275
|
}
|
|
1276
1276
|
const plans = [...new Set(listParam(params.get("plans")).flatMap((plan): string[] => plan === SUPPORTED ? [...SUPPORTED_KINDS] : [plan]))];
|
|
1277
1277
|
if (!plans.every((plan) => (OFFER_KINDS as readonly string[]).includes(plan))) {
|
|
@@ -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({
|
|
@@ -247,6 +256,7 @@ export async function completeNonStreaming(args: {
|
|
|
247
256
|
await args.reporter.waitForPrefixEntryWrite();
|
|
248
257
|
return {
|
|
249
258
|
choice,
|
|
259
|
+
requestId,
|
|
250
260
|
output,
|
|
251
261
|
responseBody,
|
|
252
262
|
...(reportBarrierError === undefined ? {} : { reportBarrierError }),
|
|
@@ -329,7 +339,7 @@ async function runCompletionAttempt(args: {
|
|
|
329
339
|
}
|
|
330
340
|
return {
|
|
331
341
|
kind: "succeeded",
|
|
332
|
-
value: { completion },
|
|
342
|
+
value: { completion, responseCapture: started.responseCapture },
|
|
333
343
|
latencyMs: completion.providerLatencyMs,
|
|
334
344
|
};
|
|
335
345
|
} catch (error) {
|
|
@@ -613,6 +623,7 @@ async function runStreamSetupAttempt(args: {
|
|
|
613
623
|
source: started.providerStream.source,
|
|
614
624
|
},
|
|
615
625
|
customerAbort: started.customerAbort,
|
|
626
|
+
responseCapture: started.responseCapture,
|
|
616
627
|
},
|
|
617
628
|
latencyMs: 0,
|
|
618
629
|
};
|
|
@@ -653,6 +664,7 @@ export async function executeStreaming(args: {
|
|
|
653
664
|
}): Promise<Response> {
|
|
654
665
|
const { execution } = args.choice;
|
|
655
666
|
const started = args.startedStream;
|
|
667
|
+
const requestId = servedRequestId(args.requestId, execution, started.responseCapture);
|
|
656
668
|
args.reporter.attachServingDecision(args.choice.decision);
|
|
657
669
|
const observed = observeDeliveredOutput(started.providerStream.events);
|
|
658
670
|
const finalizer = createStreamFinalizer({
|
|
@@ -672,7 +684,7 @@ export async function executeStreaming(args: {
|
|
|
672
684
|
});
|
|
673
685
|
return new Response(
|
|
674
686
|
args.protocol.streamBody({
|
|
675
|
-
id:
|
|
687
|
+
id: requestId,
|
|
676
688
|
model: execution.model,
|
|
677
689
|
routing: args.choice.decision,
|
|
678
690
|
events: observed.events,
|
|
@@ -695,19 +707,34 @@ export async function executeStreaming(args: {
|
|
|
695
707
|
trackBackgroundTelemetry(finalizer.afterResponseDetached(outcome));
|
|
696
708
|
},
|
|
697
709
|
}),
|
|
698
|
-
{ headers: streamHeaders(execution) },
|
|
710
|
+
{ headers: streamHeaders(execution, requestId) },
|
|
699
711
|
);
|
|
700
712
|
}
|
|
701
713
|
|
|
702
|
-
function streamHeaders(execution: ProviderExecution): Record<string, string> {
|
|
714
|
+
function streamHeaders(execution: ProviderExecution, requestId: string): Record<string, string> {
|
|
703
715
|
return {
|
|
704
716
|
"Content-Type": "text/event-stream",
|
|
705
717
|
"Cache-Control": "no-cache",
|
|
706
718
|
"Connection": "keep-alive",
|
|
707
719
|
...execution.headers,
|
|
720
|
+
"X-Request-Id": requestId,
|
|
708
721
|
};
|
|
709
722
|
}
|
|
710
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
|
+
|
|
711
738
|
function customerAbortRequested(
|
|
712
739
|
clientSignal: AbortSignal | undefined,
|
|
713
740
|
): boolean {
|
|
@@ -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";
|
|
@@ -78,7 +78,8 @@ export async function handleRoutedCompletion(args: {
|
|
|
78
78
|
for (const [name, value] of Object.entries(args.responseHeaders?.(prepared.credentials) ?? {})) {
|
|
79
79
|
response.headers.set(name, value);
|
|
80
80
|
}
|
|
81
|
-
|
|
81
|
+
// Its X-Request-Id is the request the client is told it made.
|
|
82
|
+
return response;
|
|
82
83
|
} catch (error) {
|
|
83
84
|
const terminal = prepared.reporter.freezeFailure(error);
|
|
84
85
|
terminal.report();
|
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";
|