@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
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { isRecord } from "@mupt-ai/dari-router/policy-engine";
|
|
2
|
+
|
|
3
|
+
import { HttpError } from "../http/errors.js";
|
|
4
|
+
import type { AgentRequestContext, InteractionRequestPurpose } from "../interaction/types.js";
|
|
5
|
+
import { boundedIdentity, uuidIdentity } from "../runtime/harness_identity/ids.js";
|
|
6
|
+
import type { HarnessConversationIdentity } from "../runtime/harness_identity/conversation.js";
|
|
7
|
+
import type { Harness } from "./index.js";
|
|
8
|
+
|
|
9
|
+
export const codex = {
|
|
10
|
+
id: "codex",
|
|
11
|
+
headers: ["x-codex-turn-metadata", "thread-id"],
|
|
12
|
+
} as const satisfies Harness;
|
|
13
|
+
|
|
14
|
+
export function codexConversationIdentity(
|
|
15
|
+
headers: Headers,
|
|
16
|
+
payload: Record<string, unknown>,
|
|
17
|
+
): HarnessConversationIdentity | undefined {
|
|
18
|
+
const clientMetadata = isRecord(payload.client_metadata)
|
|
19
|
+
? payload.client_metadata
|
|
20
|
+
: undefined;
|
|
21
|
+
const turnMetadata = codexTurnMetadataCollection(headers, payload);
|
|
22
|
+
if (turnMetadata.length === 0) {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
// Clients can carry a stale projection after a resume or fork. Conflicts
|
|
26
|
+
// must not fail the request, and fields must not mix across projections:
|
|
27
|
+
// take thread, turn, and parent from one turn metadata projection, header
|
|
28
|
+
// first, falling back to the standalone thread fields only for the thread.
|
|
29
|
+
const entry = turnMetadata.find((metadata) => metadata.thread_id !== undefined)
|
|
30
|
+
?? turnMetadata[0]!;
|
|
31
|
+
const threadId = uuidIdentity(entry.thread_id, "x-codex-turn-metadata.thread_id")
|
|
32
|
+
?? uuidIdentity(headers.get("thread-id"), "thread-id")
|
|
33
|
+
?? uuidIdentity(clientMetadata?.thread_id, "client_metadata.thread_id");
|
|
34
|
+
if (threadId === undefined) {
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
const userTurnId = uuidIdentity(entry.turn_id, "x-codex-turn-metadata.turn_id");
|
|
38
|
+
const parentThreadId = uuidIdentity(entry.parent_thread_id,
|
|
39
|
+
"x-codex-turn-metadata.parent_thread_id");
|
|
40
|
+
return {
|
|
41
|
+
kind: "codex_thread",
|
|
42
|
+
value: threadId,
|
|
43
|
+
...(parentThreadId === undefined ? {} : { parentThreadId }),
|
|
44
|
+
...(userTurnId === undefined ? {} : { userTurnId }),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function codexRequestPurpose(
|
|
49
|
+
headers: Headers,
|
|
50
|
+
payload: Record<string, unknown>,
|
|
51
|
+
): InteractionRequestPurpose | undefined {
|
|
52
|
+
const turnMetadata = codexTurnMetadataCollection(headers, payload);
|
|
53
|
+
return turnMetadata.length > 0 && turnMetadata.every(isCodexMemoryExtraction)
|
|
54
|
+
? "memory_extraction"
|
|
55
|
+
: undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function codexRequestContext(
|
|
59
|
+
headers: Headers,
|
|
60
|
+
payload: Record<string, unknown>,
|
|
61
|
+
): AgentRequestContext | undefined {
|
|
62
|
+
const metadata = codexTurnMetadataCollection(headers, payload);
|
|
63
|
+
if (metadata.length === 0) return undefined;
|
|
64
|
+
// Classify from one projection, header first, skipping one that omits the
|
|
65
|
+
// request kind so a partial header falls back to a complete body.
|
|
66
|
+
const entry = metadata.find((candidate) => candidate.request_kind !== undefined)
|
|
67
|
+
?? metadata[0]!;
|
|
68
|
+
const kind = entry.request_kind;
|
|
69
|
+
const agentKind = entry.subagent_kind;
|
|
70
|
+
const agentId = uuidIdentity(entry.thread_id, "x-codex-turn-metadata.thread_id");
|
|
71
|
+
const requestClass = kind === "compaction" ? "compaction"
|
|
72
|
+
: kind === "memory" || kind === "prewarm" ? "auxiliary"
|
|
73
|
+
: kind === "turn" && agentKind === "memory_consolidation" ? "auxiliary"
|
|
74
|
+
: kind === "turn" && agentKind === "compact" ? "compaction"
|
|
75
|
+
: kind === "turn" && agentKind !== undefined ? "subagent"
|
|
76
|
+
: kind === "turn" ? "main" : "unknown";
|
|
77
|
+
return {
|
|
78
|
+
requestClass,
|
|
79
|
+
...(requestClass === "subagent" && agentId !== undefined ? { agentId } : {}),
|
|
80
|
+
parentAgentId: uuidIdentity(entry.parent_thread_id, "x-codex-turn-metadata.parent_thread_id"),
|
|
81
|
+
agentType: boundedIdentity(agentKind, "x-codex-turn-metadata.subagent_kind"),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Muse Code sends its thread on every Responses request. The main agent's
|
|
86
|
+
// thread is its session, and each subagent has its own thread, so each keeps
|
|
87
|
+
// its own conversation and lease.
|
|
88
|
+
|
|
89
|
+
function isCodexMemoryExtraction(
|
|
90
|
+
metadata: Record<string, unknown>,
|
|
91
|
+
): boolean {
|
|
92
|
+
if (
|
|
93
|
+
metadata.request_kind !== "memory" ||
|
|
94
|
+
metadata.thread_source !== "memory_consolidation"
|
|
95
|
+
) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
// Codex deliberately omits canonical turn identity for detached phase-1
|
|
99
|
+
// memory jobs, while compatibility projections still carry the live root
|
|
100
|
+
// thread UUID. Phase-2 consolidation agents use ordinary turn metadata and
|
|
101
|
+
// their own thread identities, so they do not match this shape.
|
|
102
|
+
return [
|
|
103
|
+
"session_id",
|
|
104
|
+
"thread_id",
|
|
105
|
+
"turn_id",
|
|
106
|
+
"root_turn_id",
|
|
107
|
+
"window_id",
|
|
108
|
+
"forked_from_thread_id",
|
|
109
|
+
].every((field) => metadata[field] === undefined);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function codexTurnMetadataCollection(
|
|
113
|
+
headers: Headers,
|
|
114
|
+
payload: Record<string, unknown>,
|
|
115
|
+
): Record<string, unknown>[] {
|
|
116
|
+
const clientMetadata = isRecord(payload.client_metadata)
|
|
117
|
+
? payload.client_metadata
|
|
118
|
+
: undefined;
|
|
119
|
+
return [
|
|
120
|
+
codexTurnMetadata(
|
|
121
|
+
headers.get("x-codex-turn-metadata"),
|
|
122
|
+
"x-codex-turn-metadata",
|
|
123
|
+
),
|
|
124
|
+
codexTurnMetadata(
|
|
125
|
+
clientMetadata?.["x-codex-turn-metadata"],
|
|
126
|
+
"client_metadata.x-codex-turn-metadata",
|
|
127
|
+
),
|
|
128
|
+
].filter((value): value is Record<string, unknown> => value !== undefined);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function codexTurnMetadata(
|
|
132
|
+
raw: unknown,
|
|
133
|
+
field: string,
|
|
134
|
+
): Record<string, unknown> | undefined {
|
|
135
|
+
if (raw === undefined || raw === null) return undefined;
|
|
136
|
+
if (typeof raw !== "string") {
|
|
137
|
+
throw new HttpError(
|
|
138
|
+
400,
|
|
139
|
+
`${field} must be a JSON string.`,
|
|
140
|
+
"invalid_request_error",
|
|
141
|
+
field,
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
try {
|
|
145
|
+
const parsed: unknown = JSON.parse(raw);
|
|
146
|
+
if (!isRecord(parsed)) throw new Error("not an object");
|
|
147
|
+
return parsed;
|
|
148
|
+
} catch {
|
|
149
|
+
throw new HttpError(
|
|
150
|
+
400,
|
|
151
|
+
`${field} must contain a JSON object.`,
|
|
152
|
+
"invalid_request_error",
|
|
153
|
+
field,
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { claudeCode } from "./claude_code.js";
|
|
2
|
+
import { codex } from "./codex.js";
|
|
3
|
+
import { hermes } from "./hermes.js";
|
|
4
|
+
import { museCode } from "./muse_code.js";
|
|
5
|
+
import { openclaw } from "./openclaw.js";
|
|
6
|
+
import { opencode } from "./opencode.js";
|
|
7
|
+
import { pi } from "./pi.js";
|
|
8
|
+
|
|
9
|
+
/** A coding agent the gateway serves. */
|
|
10
|
+
export type Harness = {
|
|
11
|
+
id: string;
|
|
12
|
+
// The headers it describes itself and each request with: which agent it
|
|
13
|
+
// is, its session, and what the request is for. A connected gateway passes
|
|
14
|
+
// them on to hosted Dari.
|
|
15
|
+
headers: readonly string[];
|
|
16
|
+
// Its own plan serves only it, so only its own headers name it.
|
|
17
|
+
ownPlanOnly?: true;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const HARNESSES = [claudeCode, codex, museCode, opencode, pi, openclaw, hermes] as const;
|
|
21
|
+
|
|
22
|
+
export type ClientHarness = (typeof HARNESSES)[number]["id"];
|
|
23
|
+
|
|
24
|
+
export const CLIENT_HARNESSES: readonly string[] = HARNESSES.map((harness) => harness.id);
|
|
25
|
+
|
|
26
|
+
/** The agents whose own plan serves only them. */
|
|
27
|
+
export const OWN_PLAN_HARNESSES: readonly ClientHarness[] = HARNESSES
|
|
28
|
+
.filter((harness) => "ownPlanOnly" in harness)
|
|
29
|
+
.map((harness) => harness.id);
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { boundedIdentity } from "../runtime/harness_identity/ids.js";
|
|
2
|
+
import type { HarnessConversationIdentity } from "../runtime/harness_identity/conversation.js";
|
|
3
|
+
import type { Harness } from "./index.js";
|
|
4
|
+
|
|
5
|
+
export const museCode = {
|
|
6
|
+
id: "muse_code",
|
|
7
|
+
headers: ["x-client-id", "x-tbh-thread-id"],
|
|
8
|
+
// A Muse Code plan serves only Muse Code.
|
|
9
|
+
ownPlanOnly: true,
|
|
10
|
+
} as const satisfies Harness;
|
|
11
|
+
|
|
12
|
+
// Muse Code names its surface (TUI, `muse exec`, desktop, `muse serve`) in
|
|
13
|
+
// x-client-id on every model request.
|
|
14
|
+
const MUSE_CODE_CLIENT_IDS = new Set(["tbh:tui", "tbh:exec", "tbh:desktop", "tbh:serve"]);
|
|
15
|
+
|
|
16
|
+
/** Muse Code's x-client-id when the request comes from Muse Code. */
|
|
17
|
+
export function museCodeClientId(headers: Headers): string | undefined {
|
|
18
|
+
const clientId = headers.get("x-client-id")?.trim().toLowerCase();
|
|
19
|
+
return clientId !== undefined && MUSE_CODE_CLIENT_IDS.has(clientId) ? clientId : undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Muse Code sends its thread on every Responses request. The main agent's
|
|
23
|
+
// thread is its session, and each subagent has its own thread, so each keeps
|
|
24
|
+
// its own conversation and lease.
|
|
25
|
+
export function museCodeConversationIdentity(
|
|
26
|
+
headers: Headers,
|
|
27
|
+
): HarnessConversationIdentity | undefined {
|
|
28
|
+
const threadId = boundedIdentity(headers.get("x-tbh-thread-id"), "x-tbh-thread-id");
|
|
29
|
+
return threadId === undefined ? undefined : { kind: "muse_code_thread", value: threadId };
|
|
30
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { AgentRequestClass, 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 opencode = {
|
|
6
|
+
id: "opencode",
|
|
7
|
+
// With the `dari opencode` plugin's request kind and agent.
|
|
8
|
+
headers: [
|
|
9
|
+
"x-opencode-session-id",
|
|
10
|
+
"x-opencode-parent-session-id",
|
|
11
|
+
"x-opencode-request-kind",
|
|
12
|
+
"x-opencode-agent",
|
|
13
|
+
"x-session-affinity",
|
|
14
|
+
],
|
|
15
|
+
} as const satisfies Harness;
|
|
16
|
+
|
|
17
|
+
// OpenCode names the session behind every request, and a subagent's parent
|
|
18
|
+
// session too. The `dari opencode` plugin adds the request kind and agent,
|
|
19
|
+
// which OpenCode does not send itself. Like Claude Code's headers, these are
|
|
20
|
+
// client-supplied routing hints, never inferred from prompt text.
|
|
21
|
+
export function openCodeRequestContext(headers: Headers): AgentRequestContext | undefined {
|
|
22
|
+
if (boundedIdentity(headers.get("x-opencode-session-id"), "x-opencode-session-id") === undefined) {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
const kind = headers.get("x-opencode-request-kind")?.trim();
|
|
26
|
+
const subagentId = openCodeSubagentId(headers);
|
|
27
|
+
const requestClass: AgentRequestClass = kind === "compaction" ? "compaction"
|
|
28
|
+
: kind === "title" || kind === "generate" ? "auxiliary"
|
|
29
|
+
: subagentId === undefined ? "main" : "subagent";
|
|
30
|
+
return {
|
|
31
|
+
requestClass,
|
|
32
|
+
...(subagentId === undefined ? {} : {
|
|
33
|
+
agentId: subagentId,
|
|
34
|
+
parentAgentId: boundedIdentity(
|
|
35
|
+
headers.get("x-opencode-parent-session-id"),
|
|
36
|
+
"x-opencode-parent-session-id",
|
|
37
|
+
),
|
|
38
|
+
}),
|
|
39
|
+
agentType: boundedIdentity(headers.get("x-opencode-agent"), "x-opencode-agent"),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function openCodeSubagentId(headers: Headers): string | undefined {
|
|
44
|
+
const parent = boundedIdentity(
|
|
45
|
+
headers.get("x-opencode-parent-session-id"),
|
|
46
|
+
"x-opencode-parent-session-id",
|
|
47
|
+
);
|
|
48
|
+
return parent === undefined
|
|
49
|
+
? undefined
|
|
50
|
+
: boundedIdentity(headers.get("x-opencode-session-id"), "x-opencode-session-id");
|
|
51
|
+
}
|
|
@@ -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))) {
|
package/src/link/self_hosted.ts
CHANGED
package/src/platform/platform.ts
CHANGED
|
@@ -507,6 +507,9 @@ export type ResolveRouterRequest = {
|
|
|
507
507
|
// The client's own identifying headers (ingress/client_harness.ts), for a
|
|
508
508
|
// platform that hands the request on.
|
|
509
509
|
clientHeaders?: Record<string, string>;
|
|
510
|
+
// The request asked to stay out of Activity, as ResolvedRouter.private; a
|
|
511
|
+
// platform that hands it on asks the same.
|
|
512
|
+
private?: boolean;
|
|
510
513
|
// A later resolve with this request's `conversationId` replaces this one's
|
|
511
514
|
// credentials, so none from it serve the request: a platform should not
|
|
512
515
|
// mark a credential used or refresh it for this resolve.
|
|
@@ -614,6 +617,9 @@ export type RequestRecord = {
|
|
|
614
617
|
link?: { grantId: string; thinking: string | null; costUsd: number | null; credentialId: string | null };
|
|
615
618
|
// The user's plan that served the request, when one did.
|
|
616
619
|
subscriptionCredentialId?: string;
|
|
620
|
+
// The request asked to stay out of Activity (ResolvedRouter.private): only
|
|
621
|
+
// what billing needs may be kept.
|
|
622
|
+
private: boolean;
|
|
617
623
|
// The router's non-secret routing settings, for embedders that compare or
|
|
618
624
|
// replay routing decisions.
|
|
619
625
|
router: RequestRecordRouter;
|
|
@@ -8,3 +8,8 @@ export const ROUTING_PREFIX_HASHES_MAX_COUNT = 200;
|
|
|
8
8
|
// The provider id of hosted Dari when a gateway connected to it calls it for
|
|
9
9
|
// a model, as `dari/model/<id>` with the user's Dari key.
|
|
10
10
|
export const HOSTED_DARI_PROVIDER = "dari";
|
|
11
|
+
|
|
12
|
+
// A request with this header set to `true` stays out of Activity: the
|
|
13
|
+
// platform keeps only what billing needs. A gateway in private mode sets it on
|
|
14
|
+
// every request, and on each it hands hosted Dari.
|
|
15
|
+
export const PRIVATE_HEADER = "x-dari-private";
|
|
@@ -17,6 +17,7 @@ export function anthropicMessagesResponse(args: {
|
|
|
17
17
|
request: InteractionRequest;
|
|
18
18
|
routing: RoutingDecision;
|
|
19
19
|
conversationId?: string;
|
|
20
|
+
private?: boolean;
|
|
20
21
|
}): Record<string, unknown> {
|
|
21
22
|
return {
|
|
22
23
|
id: anthropicMessageId(args.id),
|
|
@@ -29,6 +30,6 @@ export function anthropicMessagesResponse(args: {
|
|
|
29
30
|
// As on Chat Completions. Null means unpriced, never a fabricated free request.
|
|
30
31
|
usage: { ...anthropicUsage(args.output), cost: args.modelCost ?? null },
|
|
31
32
|
...(args.output.safeguardResults ? { safeguard_results: args.output.safeguardResults } : {}),
|
|
32
|
-
dari_routing: anthropicRouting(args.routing, args.id, args.conversationId),
|
|
33
|
+
dari_routing: anthropicRouting(args.routing, args.id, args.conversationId, args.private),
|
|
33
34
|
};
|
|
34
35
|
}
|
|
@@ -120,7 +120,8 @@ export function anthropicUsage(output: Pick<InteractionOutput, "usage">): Record
|
|
|
120
120
|
export function anthropicRouting(
|
|
121
121
|
decision: ServedDecision,
|
|
122
122
|
requestId: string,
|
|
123
|
-
conversationId?: string
|
|
123
|
+
conversationId?: string,
|
|
124
|
+
isPrivate?: boolean
|
|
124
125
|
): Record<string, unknown> {
|
|
125
126
|
return {
|
|
126
127
|
requested_model: "dari/routing",
|
|
@@ -129,6 +130,8 @@ export function anthropicRouting(
|
|
|
129
130
|
reason: decision.reason,
|
|
130
131
|
request_id: requestId,
|
|
131
132
|
...(conversationId ? { conversation_id: conversationId } : {}),
|
|
133
|
+
// Kept out of Activity, so no link to it there.
|
|
134
|
+
...(isPrivate ? { private: true } : {}),
|
|
132
135
|
...(decision.leaseTurnsRemaining === undefined
|
|
133
136
|
? {}
|
|
134
137
|
: { lease_turns_remaining: decision.leaseTurnsRemaining }),
|
|
@@ -24,6 +24,7 @@ export function anthropicMessagesSseStream(args: {
|
|
|
24
24
|
source: ProviderIdentity;
|
|
25
25
|
request: InteractionRequest;
|
|
26
26
|
conversationId?: string;
|
|
27
|
+
private?: boolean;
|
|
27
28
|
onFinal?: (outcome: ProviderStreamOutcome) => void;
|
|
28
29
|
modelCost?: (message: Record<string, unknown>, failed: boolean) => number | null;
|
|
29
30
|
beforeClose?: (outcome: ProviderStreamOutcome) => Promise<void> | void;
|
|
@@ -59,7 +60,7 @@ export function anthropicMessagesSseStream(args: {
|
|
|
59
60
|
stop_reason: null,
|
|
60
61
|
stop_sequence: null,
|
|
61
62
|
usage: { input_tokens: 0, output_tokens: 0 },
|
|
62
|
-
dari_routing: anthropicRouting(args.routing, args.id, args.conversationId),
|
|
63
|
+
dari_routing: anthropicRouting(args.routing, args.id, args.conversationId, args.private),
|
|
63
64
|
},
|
|
64
65
|
});
|
|
65
66
|
|
|
@@ -20,6 +20,7 @@ export function chatCompletionResponse(args: {
|
|
|
20
20
|
modelCost?: number | null;
|
|
21
21
|
routing: RoutingDecision;
|
|
22
22
|
conversationId?: string;
|
|
23
|
+
private?: boolean;
|
|
23
24
|
}): Record<string, unknown> {
|
|
24
25
|
return {
|
|
25
26
|
id: completionId(args.id),
|
|
@@ -34,7 +35,7 @@ export function chatCompletionResponse(args: {
|
|
|
34
35
|
},
|
|
35
36
|
],
|
|
36
37
|
usage: usagePayload(args.output, args.modelCost),
|
|
37
|
-
dari_routing: routingPayload(args.routing, args.id, args.conversationId),
|
|
38
|
+
dari_routing: routingPayload(args.routing, args.id, args.conversationId, args.private),
|
|
38
39
|
};
|
|
39
40
|
}
|
|
40
41
|
|
|
@@ -46,6 +47,7 @@ export function chatCompletionsSseStream(args: {
|
|
|
46
47
|
source: ProviderIdentity;
|
|
47
48
|
request: InteractionRequest;
|
|
48
49
|
conversationId?: string;
|
|
50
|
+
private?: boolean;
|
|
49
51
|
onFinal?: (outcome: ProviderStreamOutcome) => void;
|
|
50
52
|
modelCost?: (message: Record<string, unknown>, failed: boolean) => number | null;
|
|
51
53
|
// Optional terminal barrier. Streaming deltas are unaffected, but [DONE]
|
|
@@ -75,7 +77,7 @@ export function chatCompletionsSseStream(args: {
|
|
|
75
77
|
usage: usagePayload(message, args.modelCost?.(message, true)) });
|
|
76
78
|
};
|
|
77
79
|
|
|
78
|
-
send(chunk({ completion, created, model: args.model, delta: {}, finishReason: null, routing: routingPayload(args.routing, args.id, args.conversationId) }));
|
|
80
|
+
send(chunk({ completion, created, model: args.model, delta: {}, finishReason: null, routing: routingPayload(args.routing, args.id, args.conversationId, args.private) }));
|
|
79
81
|
send(chunk({ completion, created, model: args.model, delta: { role: "assistant" }, finishReason: null }));
|
|
80
82
|
|
|
81
83
|
const argumentDeltaSeen = new Set<string>();
|
|
@@ -208,7 +210,7 @@ export function chatCompletionsSseStream(args: {
|
|
|
208
210
|
});
|
|
209
211
|
}
|
|
210
212
|
|
|
211
|
-
function routingPayload(decision: RoutingDecision, requestId: string, conversationId?: string): Record<string, unknown> {
|
|
213
|
+
function routingPayload(decision: RoutingDecision, requestId: string, conversationId?: string, isPrivate?: boolean): Record<string, unknown> {
|
|
212
214
|
return {
|
|
213
215
|
requested_model: "dari/routing",
|
|
214
216
|
selected_model: decision.selectedModel,
|
|
@@ -216,6 +218,8 @@ function routingPayload(decision: RoutingDecision, requestId: string, conversati
|
|
|
216
218
|
reason: decision.reason,
|
|
217
219
|
request_id: requestId,
|
|
218
220
|
...(conversationId ? { conversation_id: conversationId } : {}),
|
|
221
|
+
// Kept out of Activity, so no link to it there.
|
|
222
|
+
...(isPrivate ? { private: true } : {}),
|
|
219
223
|
};
|
|
220
224
|
}
|
|
221
225
|
|
|
@@ -15,6 +15,7 @@ export function openAIResponsesResponse(args: {
|
|
|
15
15
|
request: InteractionRequest;
|
|
16
16
|
routing: RoutingDecision;
|
|
17
17
|
conversationId?: string;
|
|
18
|
+
private?: boolean;
|
|
18
19
|
}): Record<string, unknown> {
|
|
19
20
|
return {
|
|
20
21
|
id: openAIResponsesResponseId(args.id),
|
|
@@ -26,7 +27,7 @@ export function openAIResponsesResponse(args: {
|
|
|
26
27
|
// As on Chat Completions. Null means unpriced, never a fabricated free request.
|
|
27
28
|
usage: { ...openAIResponsesUsage(args.output), cost: args.modelCost ?? null },
|
|
28
29
|
parallel_tool_calls: args.request.parallelToolCalls ?? true,
|
|
29
|
-
dari_routing: openAIResponsesRouting(args.routing, args.id, args.conversationId),
|
|
30
|
+
dari_routing: openAIResponsesRouting(args.routing, args.id, args.conversationId, args.private),
|
|
30
31
|
};
|
|
31
32
|
}
|
|
32
33
|
import type { RoutingDecision } from "@mupt-ai/dari-router/policy-engine";
|
|
@@ -46,7 +46,8 @@ export function openAIResponsesUsage(output: Pick<InteractionOutput, "usage">):
|
|
|
46
46
|
export function openAIResponsesRouting(
|
|
47
47
|
decision: RoutingDecision,
|
|
48
48
|
requestId: string,
|
|
49
|
-
conversationId?: string
|
|
49
|
+
conversationId?: string,
|
|
50
|
+
isPrivate?: boolean
|
|
50
51
|
): Record<string, unknown> {
|
|
51
52
|
return {
|
|
52
53
|
requested_model: "dari/routing",
|
|
@@ -55,6 +56,8 @@ export function openAIResponsesRouting(
|
|
|
55
56
|
reason: decision.reason,
|
|
56
57
|
request_id: requestId,
|
|
57
58
|
...(conversationId ? { conversation_id: conversationId } : {}),
|
|
59
|
+
// Kept out of Activity, so no link to it there.
|
|
60
|
+
...(isPrivate ? { private: true } : {}),
|
|
58
61
|
};
|
|
59
62
|
}
|
|
60
63
|
|
|
@@ -29,6 +29,7 @@ type StreamArgs = {
|
|
|
29
29
|
source: ProviderIdentity;
|
|
30
30
|
request: InteractionRequest;
|
|
31
31
|
conversationId?: string;
|
|
32
|
+
private?: boolean;
|
|
32
33
|
onFinal?: (outcome: ProviderStreamOutcome) => void;
|
|
33
34
|
modelCost?: (message: Record<string, unknown>, failed: boolean) => number | null;
|
|
34
35
|
// Optional terminal barrier. Streaming deltas are unaffected, but the
|
|
@@ -623,7 +624,7 @@ function responseEnvelope(
|
|
|
623
624
|
parallel_tool_calls: args.request.parallelToolCalls ?? true,
|
|
624
625
|
// `cost` as on Chat Completions: null means unpriced.
|
|
625
626
|
...(interaction ? { usage: { ...openAIResponsesUsage(interaction), cost: cost ?? null } } : {}),
|
|
626
|
-
dari_routing: openAIResponsesRouting(args.routing, args.id, args.conversationId),
|
|
627
|
+
dari_routing: openAIResponsesRouting(args.routing, args.id, args.conversationId, args.private),
|
|
627
628
|
};
|
|
628
629
|
}
|
|
629
630
|
|