@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,11 @@
|
|
|
1
|
+
import type { AgentRequestContext } from "../interaction/types.js";
|
|
2
|
+
import type { HarnessConversationIdentity } from "../runtime/harness_identity/conversation.js";
|
|
3
|
+
export declare const claudeCode: {
|
|
4
|
+
readonly id: "claude_code";
|
|
5
|
+
readonly headers: readonly ["user-agent", "x-app", "anthropic-beta", "x-claude-code-session-id", "x-claude-code-request-class", "x-claude-code-agent-id", "x-claude-code-parent-agent-id", "x-claude-code-agent-type"];
|
|
6
|
+
readonly ownPlanOnly: true;
|
|
7
|
+
};
|
|
8
|
+
/** Whether a request carries Claude Code's own headers. */
|
|
9
|
+
export declare function isClaudeCodeRequest(headers: Headers): boolean;
|
|
10
|
+
export declare function claudeCodeRequestContext(headers: Headers): AgentRequestContext;
|
|
11
|
+
export declare function claudeCodeConversationIdentity(headers: Headers, metadataSessionId?: string): HarnessConversationIdentity | undefined;
|
|
@@ -1,4 +1,20 @@
|
|
|
1
|
-
import { HttpError } from "
|
|
1
|
+
import { HttpError } from "../http/errors.js";
|
|
2
|
+
import { boundedIdentity, uuidIdentity } from "../runtime/harness_identity/ids.js";
|
|
3
|
+
export const claudeCode = {
|
|
4
|
+
id: "claude_code",
|
|
5
|
+
headers: [
|
|
6
|
+
"user-agent",
|
|
7
|
+
"x-app",
|
|
8
|
+
"anthropic-beta",
|
|
9
|
+
"x-claude-code-session-id",
|
|
10
|
+
"x-claude-code-request-class",
|
|
11
|
+
"x-claude-code-agent-id",
|
|
12
|
+
"x-claude-code-parent-agent-id",
|
|
13
|
+
"x-claude-code-agent-type",
|
|
14
|
+
],
|
|
15
|
+
// A Claude Code plan serves only Claude Code.
|
|
16
|
+
ownPlanOnly: true,
|
|
17
|
+
};
|
|
2
18
|
const REQUEST_CLASSES = new Set([
|
|
3
19
|
"main", "subagent", "workflow", "auxiliary", "compaction", "side_reply",
|
|
4
20
|
]);
|
|
@@ -40,3 +56,26 @@ function identifier(headers, name) {
|
|
|
40
56
|
}
|
|
41
57
|
return value;
|
|
42
58
|
}
|
|
59
|
+
export function claudeCodeConversationIdentity(headers, metadataSessionId) {
|
|
60
|
+
const candidates = [
|
|
61
|
+
uuidIdentity(headers.get("x-claude-code-session-id"), "x-claude-code-session-id"),
|
|
62
|
+
uuidIdentity(metadataSessionId, "metadata.user_id.session_id"),
|
|
63
|
+
].filter((value) => value !== undefined);
|
|
64
|
+
if (candidates.length === 0)
|
|
65
|
+
return undefined;
|
|
66
|
+
// Claude Code can carry a stale metadata session after a resume or fork.
|
|
67
|
+
// The header names the live session, so it wins over the metadata.
|
|
68
|
+
const [sessionId] = candidates;
|
|
69
|
+
// Claude Code reuses the parent session UUID for every subagent, but gives
|
|
70
|
+
// each subagent a stable agent header. Keep the parent thread on the bare
|
|
71
|
+
// session identity and scope each subagent to its own composite identity.
|
|
72
|
+
// Treat this as an opaque Claude identifier. Besides ordinary subagent IDs,
|
|
73
|
+
// Claude may send labeled agents or Agent Teams identities such as
|
|
74
|
+
// `reviewer@backend`; Dari only needs a stable, bounded value to hash.
|
|
75
|
+
const agentId = boundedIdentity(headers.get("x-claude-code-agent-id"), "x-claude-code-agent-id");
|
|
76
|
+
return {
|
|
77
|
+
kind: "claude_code_session",
|
|
78
|
+
value: sessionId,
|
|
79
|
+
...(agentId === undefined ? {} : { qualifiers: [agentId] }),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { AgentRequestContext, InteractionRequestPurpose } from "../interaction/types.js";
|
|
2
|
+
import type { HarnessConversationIdentity } from "../runtime/harness_identity/conversation.js";
|
|
3
|
+
export declare const codex: {
|
|
4
|
+
readonly id: "codex";
|
|
5
|
+
readonly headers: readonly ["x-codex-turn-metadata", "thread-id"];
|
|
6
|
+
};
|
|
7
|
+
export declare function codexConversationIdentity(headers: Headers, payload: Record<string, unknown>): HarnessConversationIdentity | undefined;
|
|
8
|
+
export declare function codexRequestPurpose(headers: Headers, payload: Record<string, unknown>): InteractionRequestPurpose | undefined;
|
|
9
|
+
export declare function codexRequestContext(headers: Headers, payload: Record<string, unknown>): AgentRequestContext | undefined;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { isRecord } from "@mupt-ai/dari-router/policy-engine";
|
|
2
|
+
import { HttpError } from "../http/errors.js";
|
|
3
|
+
import { boundedIdentity, uuidIdentity } from "../runtime/harness_identity/ids.js";
|
|
4
|
+
export const codex = {
|
|
5
|
+
id: "codex",
|
|
6
|
+
headers: ["x-codex-turn-metadata", "thread-id"],
|
|
7
|
+
};
|
|
8
|
+
export function codexConversationIdentity(headers, payload) {
|
|
9
|
+
const clientMetadata = isRecord(payload.client_metadata)
|
|
10
|
+
? payload.client_metadata
|
|
11
|
+
: undefined;
|
|
12
|
+
const turnMetadata = codexTurnMetadataCollection(headers, payload);
|
|
13
|
+
if (turnMetadata.length === 0) {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
// Clients can carry a stale projection after a resume or fork. Conflicts
|
|
17
|
+
// must not fail the request, and fields must not mix across projections:
|
|
18
|
+
// take thread, turn, and parent from one turn metadata projection, header
|
|
19
|
+
// first, falling back to the standalone thread fields only for the thread.
|
|
20
|
+
const entry = turnMetadata.find((metadata) => metadata.thread_id !== undefined)
|
|
21
|
+
?? turnMetadata[0];
|
|
22
|
+
const threadId = uuidIdentity(entry.thread_id, "x-codex-turn-metadata.thread_id")
|
|
23
|
+
?? uuidIdentity(headers.get("thread-id"), "thread-id")
|
|
24
|
+
?? uuidIdentity(clientMetadata?.thread_id, "client_metadata.thread_id");
|
|
25
|
+
if (threadId === undefined) {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
const userTurnId = uuidIdentity(entry.turn_id, "x-codex-turn-metadata.turn_id");
|
|
29
|
+
const parentThreadId = uuidIdentity(entry.parent_thread_id, "x-codex-turn-metadata.parent_thread_id");
|
|
30
|
+
return {
|
|
31
|
+
kind: "codex_thread",
|
|
32
|
+
value: threadId,
|
|
33
|
+
...(parentThreadId === undefined ? {} : { parentThreadId }),
|
|
34
|
+
...(userTurnId === undefined ? {} : { userTurnId }),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export function codexRequestPurpose(headers, payload) {
|
|
38
|
+
const turnMetadata = codexTurnMetadataCollection(headers, payload);
|
|
39
|
+
return turnMetadata.length > 0 && turnMetadata.every(isCodexMemoryExtraction)
|
|
40
|
+
? "memory_extraction"
|
|
41
|
+
: undefined;
|
|
42
|
+
}
|
|
43
|
+
export function codexRequestContext(headers, payload) {
|
|
44
|
+
const metadata = codexTurnMetadataCollection(headers, payload);
|
|
45
|
+
if (metadata.length === 0)
|
|
46
|
+
return undefined;
|
|
47
|
+
// Classify from one projection, header first, skipping one that omits the
|
|
48
|
+
// request kind so a partial header falls back to a complete body.
|
|
49
|
+
const entry = metadata.find((candidate) => candidate.request_kind !== undefined)
|
|
50
|
+
?? metadata[0];
|
|
51
|
+
const kind = entry.request_kind;
|
|
52
|
+
const agentKind = entry.subagent_kind;
|
|
53
|
+
const agentId = uuidIdentity(entry.thread_id, "x-codex-turn-metadata.thread_id");
|
|
54
|
+
const requestClass = kind === "compaction" ? "compaction"
|
|
55
|
+
: kind === "memory" || kind === "prewarm" ? "auxiliary"
|
|
56
|
+
: kind === "turn" && agentKind === "memory_consolidation" ? "auxiliary"
|
|
57
|
+
: kind === "turn" && agentKind === "compact" ? "compaction"
|
|
58
|
+
: kind === "turn" && agentKind !== undefined ? "subagent"
|
|
59
|
+
: kind === "turn" ? "main" : "unknown";
|
|
60
|
+
return {
|
|
61
|
+
requestClass,
|
|
62
|
+
...(requestClass === "subagent" && agentId !== undefined ? { agentId } : {}),
|
|
63
|
+
parentAgentId: uuidIdentity(entry.parent_thread_id, "x-codex-turn-metadata.parent_thread_id"),
|
|
64
|
+
agentType: boundedIdentity(agentKind, "x-codex-turn-metadata.subagent_kind"),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
// Muse Code sends its thread on every Responses request. The main agent's
|
|
68
|
+
// thread is its session, and each subagent has its own thread, so each keeps
|
|
69
|
+
// its own conversation and lease.
|
|
70
|
+
function isCodexMemoryExtraction(metadata) {
|
|
71
|
+
if (metadata.request_kind !== "memory" ||
|
|
72
|
+
metadata.thread_source !== "memory_consolidation") {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
// Codex deliberately omits canonical turn identity for detached phase-1
|
|
76
|
+
// memory jobs, while compatibility projections still carry the live root
|
|
77
|
+
// thread UUID. Phase-2 consolidation agents use ordinary turn metadata and
|
|
78
|
+
// their own thread identities, so they do not match this shape.
|
|
79
|
+
return [
|
|
80
|
+
"session_id",
|
|
81
|
+
"thread_id",
|
|
82
|
+
"turn_id",
|
|
83
|
+
"root_turn_id",
|
|
84
|
+
"window_id",
|
|
85
|
+
"forked_from_thread_id",
|
|
86
|
+
].every((field) => metadata[field] === undefined);
|
|
87
|
+
}
|
|
88
|
+
function codexTurnMetadataCollection(headers, payload) {
|
|
89
|
+
const clientMetadata = isRecord(payload.client_metadata)
|
|
90
|
+
? payload.client_metadata
|
|
91
|
+
: undefined;
|
|
92
|
+
return [
|
|
93
|
+
codexTurnMetadata(headers.get("x-codex-turn-metadata"), "x-codex-turn-metadata"),
|
|
94
|
+
codexTurnMetadata(clientMetadata?.["x-codex-turn-metadata"], "client_metadata.x-codex-turn-metadata"),
|
|
95
|
+
].filter((value) => value !== undefined);
|
|
96
|
+
}
|
|
97
|
+
function codexTurnMetadata(raw, field) {
|
|
98
|
+
if (raw === undefined || raw === null)
|
|
99
|
+
return undefined;
|
|
100
|
+
if (typeof raw !== "string") {
|
|
101
|
+
throw new HttpError(400, `${field} must be a JSON string.`, "invalid_request_error", field);
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
const parsed = JSON.parse(raw);
|
|
105
|
+
if (!isRecord(parsed))
|
|
106
|
+
throw new Error("not an object");
|
|
107
|
+
return parsed;
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
throw new HttpError(400, `${field} must contain a JSON object.`, "invalid_request_error", field);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/** A coding agent the gateway serves. */
|
|
2
|
+
export type Harness = {
|
|
3
|
+
id: string;
|
|
4
|
+
headers: readonly string[];
|
|
5
|
+
ownPlanOnly?: true;
|
|
6
|
+
};
|
|
7
|
+
export declare const HARNESSES: readonly [{
|
|
8
|
+
readonly id: "claude_code";
|
|
9
|
+
readonly headers: readonly ["user-agent", "x-app", "anthropic-beta", "x-claude-code-session-id", "x-claude-code-request-class", "x-claude-code-agent-id", "x-claude-code-parent-agent-id", "x-claude-code-agent-type"];
|
|
10
|
+
readonly ownPlanOnly: true;
|
|
11
|
+
}, {
|
|
12
|
+
readonly id: "codex";
|
|
13
|
+
readonly headers: readonly ["x-codex-turn-metadata", "thread-id"];
|
|
14
|
+
}, {
|
|
15
|
+
readonly id: "muse_code";
|
|
16
|
+
readonly headers: readonly ["x-client-id", "x-tbh-thread-id"];
|
|
17
|
+
readonly ownPlanOnly: true;
|
|
18
|
+
}, {
|
|
19
|
+
readonly id: "opencode";
|
|
20
|
+
readonly headers: readonly ["x-opencode-session-id", "x-opencode-parent-session-id", "x-opencode-request-kind", "x-opencode-agent", "x-session-affinity"];
|
|
21
|
+
}, {
|
|
22
|
+
readonly id: "pi";
|
|
23
|
+
readonly headers: readonly ["x-pi-session-id", "x-pi-request-kind"];
|
|
24
|
+
}, {
|
|
25
|
+
readonly id: "openclaw";
|
|
26
|
+
readonly headers: readonly [];
|
|
27
|
+
}, {
|
|
28
|
+
readonly id: "hermes";
|
|
29
|
+
readonly headers: readonly [];
|
|
30
|
+
}];
|
|
31
|
+
export type ClientHarness = (typeof HARNESSES)[number]["id"];
|
|
32
|
+
export declare const CLIENT_HARNESSES: readonly string[];
|
|
33
|
+
/** The agents whose own plan serves only them. */
|
|
34
|
+
export declare const OWN_PLAN_HARNESSES: readonly ClientHarness[];
|
|
@@ -0,0 +1,13 @@
|
|
|
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
|
+
export const HARNESSES = [claudeCode, codex, museCode, opencode, pi, openclaw, hermes];
|
|
9
|
+
export const CLIENT_HARNESSES = HARNESSES.map((harness) => harness.id);
|
|
10
|
+
/** The agents whose own plan serves only them. */
|
|
11
|
+
export const OWN_PLAN_HARNESSES = HARNESSES
|
|
12
|
+
.filter((harness) => "ownPlanOnly" in harness)
|
|
13
|
+
.map((harness) => harness.id);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { HarnessConversationIdentity } from "../runtime/harness_identity/conversation.js";
|
|
2
|
+
export declare const museCode: {
|
|
3
|
+
readonly id: "muse_code";
|
|
4
|
+
readonly headers: readonly ["x-client-id", "x-tbh-thread-id"];
|
|
5
|
+
readonly ownPlanOnly: true;
|
|
6
|
+
};
|
|
7
|
+
/** Muse Code's x-client-id when the request comes from Muse Code. */
|
|
8
|
+
export declare function museCodeClientId(headers: Headers): string | undefined;
|
|
9
|
+
export declare function museCodeConversationIdentity(headers: Headers): HarnessConversationIdentity | undefined;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { boundedIdentity } from "../runtime/harness_identity/ids.js";
|
|
2
|
+
export const museCode = {
|
|
3
|
+
id: "muse_code",
|
|
4
|
+
headers: ["x-client-id", "x-tbh-thread-id"],
|
|
5
|
+
// A Muse Code plan serves only Muse Code.
|
|
6
|
+
ownPlanOnly: true,
|
|
7
|
+
};
|
|
8
|
+
// Muse Code names its surface (TUI, `muse exec`, desktop, `muse serve`) in
|
|
9
|
+
// x-client-id on every model request.
|
|
10
|
+
const MUSE_CODE_CLIENT_IDS = new Set(["tbh:tui", "tbh:exec", "tbh:desktop", "tbh:serve"]);
|
|
11
|
+
/** Muse Code's x-client-id when the request comes from Muse Code. */
|
|
12
|
+
export function museCodeClientId(headers) {
|
|
13
|
+
const clientId = headers.get("x-client-id")?.trim().toLowerCase();
|
|
14
|
+
return clientId !== undefined && MUSE_CODE_CLIENT_IDS.has(clientId) ? clientId : undefined;
|
|
15
|
+
}
|
|
16
|
+
// Muse Code sends its thread on every Responses request. The main agent's
|
|
17
|
+
// thread is its session, and each subagent has its own thread, so each keeps
|
|
18
|
+
// its own conversation and lease.
|
|
19
|
+
export function museCodeConversationIdentity(headers) {
|
|
20
|
+
const threadId = boundedIdentity(headers.get("x-tbh-thread-id"), "x-tbh-thread-id");
|
|
21
|
+
return threadId === undefined ? undefined : { kind: "muse_code_thread", value: threadId };
|
|
22
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { AgentRequestContext } from "../interaction/types.js";
|
|
2
|
+
export declare const opencode: {
|
|
3
|
+
readonly id: "opencode";
|
|
4
|
+
readonly headers: readonly ["x-opencode-session-id", "x-opencode-parent-session-id", "x-opencode-request-kind", "x-opencode-agent", "x-session-affinity"];
|
|
5
|
+
};
|
|
6
|
+
export declare function openCodeRequestContext(headers: Headers): AgentRequestContext | undefined;
|
|
7
|
+
export declare function openCodeSubagentId(headers: Headers): string | undefined;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { boundedIdentity } from "../runtime/harness_identity/ids.js";
|
|
2
|
+
export const opencode = {
|
|
3
|
+
id: "opencode",
|
|
4
|
+
// With the `dari opencode` plugin's request kind and agent.
|
|
5
|
+
headers: [
|
|
6
|
+
"x-opencode-session-id",
|
|
7
|
+
"x-opencode-parent-session-id",
|
|
8
|
+
"x-opencode-request-kind",
|
|
9
|
+
"x-opencode-agent",
|
|
10
|
+
"x-session-affinity",
|
|
11
|
+
],
|
|
12
|
+
};
|
|
13
|
+
// OpenCode names the session behind every request, and a subagent's parent
|
|
14
|
+
// session too. The `dari opencode` plugin adds the request kind and agent,
|
|
15
|
+
// which OpenCode does not send itself. Like Claude Code's headers, these are
|
|
16
|
+
// client-supplied routing hints, never inferred from prompt text.
|
|
17
|
+
export function openCodeRequestContext(headers) {
|
|
18
|
+
if (boundedIdentity(headers.get("x-opencode-session-id"), "x-opencode-session-id") === undefined) {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
const kind = headers.get("x-opencode-request-kind")?.trim();
|
|
22
|
+
const subagentId = openCodeSubagentId(headers);
|
|
23
|
+
const requestClass = kind === "compaction" ? "compaction"
|
|
24
|
+
: kind === "title" || kind === "generate" ? "auxiliary"
|
|
25
|
+
: subagentId === undefined ? "main" : "subagent";
|
|
26
|
+
return {
|
|
27
|
+
requestClass,
|
|
28
|
+
...(subagentId === undefined ? {} : {
|
|
29
|
+
agentId: subagentId,
|
|
30
|
+
parentAgentId: boundedIdentity(headers.get("x-opencode-parent-session-id"), "x-opencode-parent-session-id"),
|
|
31
|
+
}),
|
|
32
|
+
agentType: boundedIdentity(headers.get("x-opencode-agent"), "x-opencode-agent"),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
export function openCodeSubagentId(headers) {
|
|
36
|
+
const parent = boundedIdentity(headers.get("x-opencode-parent-session-id"), "x-opencode-parent-session-id");
|
|
37
|
+
return parent === undefined
|
|
38
|
+
? undefined
|
|
39
|
+
: boundedIdentity(headers.get("x-opencode-session-id"), "x-opencode-session-id");
|
|
40
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { AgentRequestContext } from "../interaction/types.js";
|
|
2
|
+
export declare const pi: {
|
|
3
|
+
readonly id: "pi";
|
|
4
|
+
readonly headers: readonly ["x-pi-session-id", "x-pi-request-kind"];
|
|
5
|
+
};
|
|
6
|
+
export declare function piRequestContext(headers: Headers): AgentRequestContext | undefined;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { boundedIdentity } from "../runtime/harness_identity/ids.js";
|
|
2
|
+
export const pi = {
|
|
3
|
+
id: "pi",
|
|
4
|
+
headers: ["x-pi-session-id", "x-pi-request-kind"],
|
|
5
|
+
};
|
|
6
|
+
export function piRequestContext(headers) {
|
|
7
|
+
if (boundedIdentity(headers.get("x-pi-session-id"), "x-pi-session-id") === undefined) {
|
|
8
|
+
return undefined;
|
|
9
|
+
}
|
|
10
|
+
const kind = headers.get("x-pi-request-kind")?.trim();
|
|
11
|
+
return {
|
|
12
|
+
requestClass: kind === "turn" ? "main"
|
|
13
|
+
: kind === "compaction" ? "compaction"
|
|
14
|
+
// The Dari extension's web search tool: one request beside the turn,
|
|
15
|
+
// served as background work like Claude Code's own search.
|
|
16
|
+
: kind === "search" ? "auxiliary"
|
|
17
|
+
: "unknown",
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
// OpenCode names the session behind every request, and a subagent's parent
|
|
21
|
+
// session too. The `dari opencode` plugin adds the request kind and agent,
|
|
22
|
+
// which OpenCode does not send itself. Like Claude Code's headers, these are
|
|
23
|
+
// client-supplied routing hints, never inferred from prompt text.
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type ClientHarness } from "../harnesses/index.js";
|
|
2
2
|
export declare const CLIENT_HARNESS_HEADER = "x-dari-client-harness";
|
|
3
|
-
export declare const CLIENT_HARNESSES: readonly string[];
|
|
4
3
|
/**
|
|
5
4
|
* The headers of `CLIENT_HEADERS` the request carries, as the client sent
|
|
6
5
|
* them, so hosted Dari serving it for a connected gateway sees the same
|
|
@@ -1,42 +1,7 @@
|
|
|
1
|
+
import { CLIENT_HARNESSES, HARNESSES, OWN_PLAN_HARNESSES } from "../harnesses/index.js";
|
|
1
2
|
export const CLIENT_HARNESS_HEADER = "x-dari-client-harness";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
"codex",
|
|
5
|
-
"muse_code",
|
|
6
|
-
"opencode",
|
|
7
|
-
"pi",
|
|
8
|
-
"openclaw",
|
|
9
|
-
"hermes",
|
|
10
|
-
];
|
|
11
|
-
// The headers a coding agent describes itself and each request with: which
|
|
12
|
-
// agent it is, its session, and what the request is for. harness_request.ts
|
|
13
|
-
// reads every one of them.
|
|
14
|
-
const CLIENT_HEADERS = [
|
|
15
|
-
// Claude Code
|
|
16
|
-
"user-agent",
|
|
17
|
-
"x-app",
|
|
18
|
-
"anthropic-beta",
|
|
19
|
-
"x-claude-code-session-id",
|
|
20
|
-
"x-claude-code-request-class",
|
|
21
|
-
"x-claude-code-agent-id",
|
|
22
|
-
"x-claude-code-parent-agent-id",
|
|
23
|
-
"x-claude-code-agent-type",
|
|
24
|
-
// Codex
|
|
25
|
-
"x-codex-turn-metadata",
|
|
26
|
-
"thread-id",
|
|
27
|
-
// Muse Code
|
|
28
|
-
"x-client-id",
|
|
29
|
-
"x-tbh-thread-id",
|
|
30
|
-
// OpenCode, with the `dari opencode` plugin's request kind and agent
|
|
31
|
-
"x-opencode-session-id",
|
|
32
|
-
"x-opencode-parent-session-id",
|
|
33
|
-
"x-opencode-request-kind",
|
|
34
|
-
"x-opencode-agent",
|
|
35
|
-
"x-session-affinity",
|
|
36
|
-
// Pi
|
|
37
|
-
"x-pi-session-id",
|
|
38
|
-
"x-pi-request-kind",
|
|
39
|
-
];
|
|
3
|
+
// Every coding agent's own headers. harness_request.ts reads every one of them.
|
|
4
|
+
const CLIENT_HEADERS = HARNESSES.flatMap((harness) => harness.headers);
|
|
40
5
|
/**
|
|
41
6
|
* The headers of `CLIENT_HEADERS` the request carries, as the client sent
|
|
42
7
|
* them, so hosted Dari serving it for a connected gateway sees the same
|
|
@@ -66,5 +31,5 @@ export function namedClientHarness(headers) {
|
|
|
66
31
|
*/
|
|
67
32
|
export function claimedClientHarness(headers) {
|
|
68
33
|
const named = namedClientHarness(headers);
|
|
69
|
-
return named ===
|
|
34
|
+
return named === undefined || OWN_PLAN_HARNESSES.includes(named) ? undefined : named;
|
|
70
35
|
}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { claudeCodeConversationIdentity, claudeCodeRequestContext, isClaudeCodeRequest, } from "../harnesses/claude_code.js";
|
|
2
|
+
import { codexConversationIdentity, codexRequestContext, codexRequestPurpose } from "../harnesses/codex.js";
|
|
3
|
+
import { museCodeClientId, museCodeConversationIdentity } from "../harnesses/muse_code.js";
|
|
4
|
+
import { openCodeRequestContext } from "../harnesses/opencode.js";
|
|
5
|
+
import { piRequestContext } from "../harnesses/pi.js";
|
|
6
|
+
import { chatCompletionsConversationIdentity, } from "../runtime/harness_identity/index.js";
|
|
3
7
|
import { claimedClientHarness } from "./client_harness.js";
|
|
4
|
-
import { museCodeClientId } from "./muse_code.js";
|
|
5
8
|
/**
|
|
6
9
|
* Read the coding agent from its own headers.
|
|
7
10
|
*
|
package/dist/ingress/models.js
CHANGED
|
@@ -15,6 +15,7 @@ import { modelSupportsImageInput, modelSupportsNativeCompaction } from "../provi
|
|
|
15
15
|
import { modelMetadataLookup } from "../providers/pi.js";
|
|
16
16
|
import { resolveRouter } from "../strategies/routing.js";
|
|
17
17
|
import { anthropicModelsResponse } from "./anthropic_models.js";
|
|
18
|
+
import { OWN_PLAN_HARNESSES } from "../harnesses/index.js";
|
|
18
19
|
import { namedClientHarness } from "./client_harness.js";
|
|
19
20
|
import { codexModelsEtag, codexModelsResponse } from "./codex_models.js";
|
|
20
21
|
import { CANONICAL_ROUTER_MODEL, explicitModelSlug, LINK_CHILD_TOKEN_PREFIX } from "../protocol/input/router_model.js";
|
|
@@ -100,7 +101,7 @@ export async function handleModels(request, routerId) {
|
|
|
100
101
|
async function childTokenModels(routerId, apiKey) {
|
|
101
102
|
if (!apiKey.startsWith(LINK_CHILD_TOKEN_PREFIX))
|
|
102
103
|
return [];
|
|
103
|
-
for (const clientHarness of
|
|
104
|
+
for (const clientHarness of OWN_PLAN_HARNESSES) {
|
|
104
105
|
try {
|
|
105
106
|
const router = await resolveRouter({ routerId, apiKey, clientHarness, provisional: true });
|
|
106
107
|
return router.link_models ?? router.enabled_models;
|
package/dist/link/endpoints.js
CHANGED
|
@@ -68,6 +68,7 @@ import { budgetBody, limitsBody, spendBody, validBudget, validLimits } from "./b
|
|
|
68
68
|
import { catalogModelFor } from "./grant_access.js";
|
|
69
69
|
import { registeredRedirect } from "./grants.js";
|
|
70
70
|
import { requestTags } from "../runtime/request_tags.js";
|
|
71
|
+
import { CLIENT_HARNESSES } from "../harnesses/index.js";
|
|
71
72
|
import { parseJsonRequest } from "../ingress/json.js";
|
|
72
73
|
import { DARI_MARK, ICONS, linkLogo } from "./assets.js";
|
|
73
74
|
import { GEIST_WOFF2 } from "./font.js";
|
|
@@ -128,9 +129,6 @@ const CHILD_TOKEN_MAX_SECONDS = 12 * 60 * 60;
|
|
|
128
129
|
const MAX_MODEL_CHARS = 200;
|
|
129
130
|
const MAX_PURPOSE_CHARS = 200;
|
|
130
131
|
const MAX_CHILD_MODELS = 16;
|
|
131
|
-
// Clients an app can say it runs with `client` on the authorize request.
|
|
132
|
-
// Claude Code and Muse Code plans serve only an app that runs those.
|
|
133
|
-
const CLIENTS = ["claude_code", "codex", "muse_code", "opencode", "pi", "openclaw", "hermes"];
|
|
134
132
|
const PLAN_KINDS = Object.keys(SUBSCRIPTION_PLAN_LABELS);
|
|
135
133
|
// An API key, as a kind an app asks for in `plans` and reads on a grant.
|
|
136
134
|
const API_KEY = "api_key";
|
|
@@ -1124,8 +1122,10 @@ async function authorizeRequest(link, params) {
|
|
|
1124
1122
|
return badRequest(`The only scope is ${LINK_SCOPE}.`);
|
|
1125
1123
|
}
|
|
1126
1124
|
const clients = listParam(params.get("client"));
|
|
1127
|
-
|
|
1128
|
-
|
|
1125
|
+
// The clients an app can say it runs. Claude Code and Muse Code plans serve
|
|
1126
|
+
// only an app that runs those.
|
|
1127
|
+
if (!clients.every((client) => CLIENT_HARNESSES.includes(client))) {
|
|
1128
|
+
return badRequest(`client lists the clients the app runs: ${CLIENT_HARNESSES.join(", ")}.`);
|
|
1129
1129
|
}
|
|
1130
1130
|
const plans = [...new Set(listParam(params.get("plans")).flatMap((plan) => plan === SUPPORTED ? [...SUPPORTED_KINDS] : [plan]))];
|
|
1131
1131
|
if (!plans.every((plan) => OFFER_KINDS.includes(plan))) {
|
|
@@ -18,6 +18,7 @@ export type PreparedStreamExecution = {
|
|
|
18
18
|
};
|
|
19
19
|
export type CompletedNonStreamingExecution = {
|
|
20
20
|
choice: ProviderChoice;
|
|
21
|
+
requestId: string;
|
|
21
22
|
output: ReturnType<typeof piMessageToInteractionOutput>;
|
|
22
23
|
responseBody: Record<string, unknown>;
|
|
23
24
|
reportBarrierError?: unknown;
|
|
@@ -8,6 +8,7 @@ import { credentialRejectionFromFailure, failureWithCredentialRejection, isProvi
|
|
|
8
8
|
import { reportedExecution, } from "../runtime/runtime_reporter.js";
|
|
9
9
|
import { trackBackgroundTelemetry } from "../telemetry/background.js";
|
|
10
10
|
import { gatewayPlatform } from "../platform/platform.js";
|
|
11
|
+
import { HOSTED_DARI_PROVIDER } from "../platform/routing_contract.js";
|
|
11
12
|
import { reportRoutingUsage } from "../telemetry/usage.js";
|
|
12
13
|
import { primeProviderStream, scrubProviderMessage, startProviderCompletion, startProviderStream, } from "./provider_call.js";
|
|
13
14
|
import { runProviderAttemptPlan, } from "./provider_attempt_runner.js";
|
|
@@ -26,7 +27,11 @@ export async function executeNonStreaming(args) {
|
|
|
26
27
|
});
|
|
27
28
|
}
|
|
28
29
|
return Response.json(result.responseBody, {
|
|
29
|
-
headers: {
|
|
30
|
+
headers: {
|
|
31
|
+
...result.choice.execution.headers,
|
|
32
|
+
...linkUsageHeaders(result),
|
|
33
|
+
"X-Request-Id": result.requestId,
|
|
34
|
+
},
|
|
30
35
|
});
|
|
31
36
|
}
|
|
32
37
|
// One usage shape for a Dari Link app whatever protocol it speaks: the tokens
|
|
@@ -66,13 +71,14 @@ export async function completeNonStreaming(args) {
|
|
|
66
71
|
result,
|
|
67
72
|
});
|
|
68
73
|
}
|
|
69
|
-
const { value: { completion }, choice, } = result;
|
|
74
|
+
const { value: { completion, responseCapture }, choice, } = result;
|
|
75
|
+
const requestId = servedRequestId(args.requestId, choice.execution, responseCapture);
|
|
70
76
|
let output;
|
|
71
77
|
let responseBody;
|
|
72
78
|
try {
|
|
73
79
|
output = piMessageToInteractionOutput(completion.message, completion.source, args.interaction);
|
|
74
80
|
responseBody = args.protocol.responseBody({
|
|
75
|
-
id:
|
|
81
|
+
id: requestId,
|
|
76
82
|
model: choice.execution.model,
|
|
77
83
|
output,
|
|
78
84
|
modelCost: args.reporter.modelCost({
|
|
@@ -132,6 +138,7 @@ export async function completeNonStreaming(args) {
|
|
|
132
138
|
await args.reporter.waitForPrefixEntryWrite();
|
|
133
139
|
return {
|
|
134
140
|
choice,
|
|
141
|
+
requestId,
|
|
135
142
|
output,
|
|
136
143
|
responseBody,
|
|
137
144
|
...(reportBarrierError === undefined ? {} : { reportBarrierError }),
|
|
@@ -180,7 +187,7 @@ async function runCompletionAttempt(args) {
|
|
|
180
187
|
}
|
|
181
188
|
return {
|
|
182
189
|
kind: "succeeded",
|
|
183
|
-
value: { completion },
|
|
190
|
+
value: { completion, responseCapture: started.responseCapture },
|
|
184
191
|
latencyMs: completion.providerLatencyMs,
|
|
185
192
|
};
|
|
186
193
|
}
|
|
@@ -359,6 +366,7 @@ async function runStreamSetupAttempt(args) {
|
|
|
359
366
|
source: started.providerStream.source,
|
|
360
367
|
},
|
|
361
368
|
customerAbort: started.customerAbort,
|
|
369
|
+
responseCapture: started.responseCapture,
|
|
362
370
|
},
|
|
363
371
|
latencyMs: 0,
|
|
364
372
|
};
|
|
@@ -376,6 +384,7 @@ async function runStreamSetupAttempt(args) {
|
|
|
376
384
|
export async function executeStreaming(args) {
|
|
377
385
|
const { execution } = args.choice;
|
|
378
386
|
const started = args.startedStream;
|
|
387
|
+
const requestId = servedRequestId(args.requestId, execution, started.responseCapture);
|
|
379
388
|
args.reporter.attachServingDecision(args.choice.decision);
|
|
380
389
|
const observed = observeDeliveredOutput(started.providerStream.events);
|
|
381
390
|
const finalizer = createStreamFinalizer({
|
|
@@ -393,7 +402,7 @@ export async function executeStreaming(args) {
|
|
|
393
402
|
startedAtMs: started.providerStartedMs,
|
|
394
403
|
});
|
|
395
404
|
return new Response(args.protocol.streamBody({
|
|
396
|
-
id:
|
|
405
|
+
id: requestId,
|
|
397
406
|
model: execution.model,
|
|
398
407
|
routing: args.choice.decision,
|
|
399
408
|
events: observed.events,
|
|
@@ -415,16 +424,27 @@ export async function executeStreaming(args) {
|
|
|
415
424
|
started.customerAbort.dispose();
|
|
416
425
|
trackBackgroundTelemetry(finalizer.afterResponseDetached(outcome));
|
|
417
426
|
},
|
|
418
|
-
}), { headers: streamHeaders(execution) });
|
|
427
|
+
}), { headers: streamHeaders(execution, requestId) });
|
|
419
428
|
}
|
|
420
|
-
function streamHeaders(execution) {
|
|
429
|
+
function streamHeaders(execution, requestId) {
|
|
421
430
|
return {
|
|
422
431
|
"Content-Type": "text/event-stream",
|
|
423
432
|
"Cache-Control": "no-cache",
|
|
424
433
|
"Connection": "keep-alive",
|
|
425
434
|
...execution.headers,
|
|
435
|
+
"X-Request-Id": requestId,
|
|
426
436
|
};
|
|
427
437
|
}
|
|
438
|
+
/**
|
|
439
|
+
* The request ID the client is told, in the response and its `X-Request-Id`:
|
|
440
|
+
* this gateway's own, except for a request hosted Dari served. Hosted logs
|
|
441
|
+
* that one under the ID it answered with, and Activity finds it only by that.
|
|
442
|
+
*/
|
|
443
|
+
function servedRequestId(requestId, execution, capture) {
|
|
444
|
+
if (execution.provider !== HOSTED_DARI_PROVIDER)
|
|
445
|
+
return requestId;
|
|
446
|
+
return capture?.succeeded?.get("x-request-id")?.trim() || requestId;
|
|
447
|
+
}
|
|
428
448
|
function customerAbortRequested(clientSignal) {
|
|
429
449
|
// A disconnected client never starts new paid work that nobody can receive.
|
|
430
450
|
return clientSignal?.aborted === true;
|
|
@@ -1,10 +1,2 @@
|
|
|
1
|
-
import type { AgentRequestContext, InteractionRequestPurpose } from "../../interaction/types.js";
|
|
2
1
|
import type { HarnessConversationIdentity } from "./conversation.js";
|
|
3
2
|
export declare function chatCompletionsConversationIdentity(headers: Headers, promptCacheKey?: string): HarnessConversationIdentity | undefined;
|
|
4
|
-
export declare function piRequestContext(headers: Headers): AgentRequestContext | undefined;
|
|
5
|
-
export declare function openCodeRequestContext(headers: Headers): AgentRequestContext | undefined;
|
|
6
|
-
export declare function codexConversationIdentity(headers: Headers, payload: Record<string, unknown>): HarnessConversationIdentity | undefined;
|
|
7
|
-
export declare function codexRequestPurpose(headers: Headers, payload: Record<string, unknown>): InteractionRequestPurpose | undefined;
|
|
8
|
-
export declare function codexRequestContext(headers: Headers, payload: Record<string, unknown>): AgentRequestContext | undefined;
|
|
9
|
-
export declare function museCodeConversationIdentity(headers: Headers): HarnessConversationIdentity | undefined;
|
|
10
|
-
export declare function claudeCodeConversationIdentity(headers: Headers, metadataSessionId?: string): HarnessConversationIdentity | undefined;
|