@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.
Files changed (58) hide show
  1. package/dist/harnesses/claude_code.d.ts +11 -0
  2. package/dist/{ingress/claude_code/request_context.js → harnesses/claude_code.js} +40 -1
  3. package/dist/harnesses/codex.d.ts +9 -0
  4. package/dist/harnesses/codex.js +112 -0
  5. package/dist/harnesses/hermes.d.ts +4 -0
  6. package/dist/harnesses/hermes.js +6 -0
  7. package/dist/harnesses/index.d.ts +34 -0
  8. package/dist/harnesses/index.js +13 -0
  9. package/dist/harnesses/muse_code.d.ts +9 -0
  10. package/dist/harnesses/muse_code.js +22 -0
  11. package/dist/harnesses/openclaw.d.ts +4 -0
  12. package/dist/harnesses/openclaw.js +6 -0
  13. package/dist/harnesses/opencode.d.ts +7 -0
  14. package/dist/harnesses/opencode.js +40 -0
  15. package/dist/harnesses/pi.d.ts +6 -0
  16. package/dist/harnesses/pi.js +23 -0
  17. package/dist/ingress/client_harness.d.ts +1 -2
  18. package/dist/ingress/client_harness.js +4 -39
  19. package/dist/ingress/harness_request.js +6 -3
  20. package/dist/ingress/models.js +2 -1
  21. package/dist/link/endpoints.js +5 -5
  22. package/dist/providers/execution.d.ts +1 -0
  23. package/dist/providers/execution.js +27 -7
  24. package/dist/runtime/harness_identity/extract.d.ts +0 -8
  25. package/dist/runtime/harness_identity/extract.js +2 -201
  26. package/dist/runtime/harness_identity/ids.d.ts +2 -0
  27. package/dist/runtime/harness_identity/ids.js +26 -0
  28. package/dist/runtime/harness_identity/index.d.ts +1 -1
  29. package/dist/runtime/harness_identity/index.js +1 -1
  30. package/dist/runtime/routed_completion.js +2 -1
  31. package/dist/self_host/config.js +1 -1
  32. package/dist/self_host/router_models.js +1 -1
  33. package/dist/types.d.ts +1 -1
  34. package/package.json +2 -2
  35. package/src/{ingress/claude_code/request_context.ts → harnesses/claude_code.ts} +56 -2
  36. package/src/harnesses/codex.ts +156 -0
  37. package/src/harnesses/hermes.ts +8 -0
  38. package/src/harnesses/index.ts +29 -0
  39. package/src/harnesses/muse_code.ts +30 -0
  40. package/src/harnesses/openclaw.ts +8 -0
  41. package/src/harnesses/opencode.ts +51 -0
  42. package/src/harnesses/pi.ts +28 -0
  43. package/src/ingress/client_harness.ts +4 -41
  44. package/src/ingress/harness_request.ts +9 -9
  45. package/src/ingress/models.ts +2 -1
  46. package/src/link/endpoints.ts +5 -5
  47. package/src/providers/execution.ts +34 -7
  48. package/src/runtime/harness_identity/extract.ts +2 -284
  49. package/src/runtime/harness_identity/ids.ts +41 -0
  50. package/src/runtime/harness_identity/index.ts +1 -10
  51. package/src/runtime/routed_completion.ts +2 -1
  52. package/src/self_host/config.ts +1 -1
  53. package/src/self_host/router_models.ts +1 -1
  54. package/src/types.ts +2 -10
  55. package/dist/ingress/claude_code/request_context.d.ts +0 -4
  56. package/dist/ingress/muse_code.d.ts +0 -2
  57. package/dist/ingress/muse_code.js +0 -8
  58. package/src/ingress/muse_code.ts +0 -9
@@ -1,6 +1,5 @@
1
- import { isRecord } from "@mupt-ai/dari-router/policy-engine";
2
- import { HttpError } from "../../http/errors.js";
3
- const MAX_ID_CHARS = 200;
1
+ import { openCodeSubagentId } from "../../harnesses/opencode.js";
2
+ import { boundedIdentity } from "./ids.js";
4
3
  export function chatCompletionsConversationIdentity(headers, promptCacheKey) {
5
4
  // Pi summaries use fresh cache/affinity IDs. Its explicit session header
6
5
  // groups them with the main turns without changing provider cache affinity.
@@ -18,201 +17,3 @@ export function chatCompletionsConversationIdentity(headers, promptCacheKey) {
18
17
  ...(subagentId === undefined ? {} : { qualifiers: [subagentId] }),
19
18
  };
20
19
  }
21
- export function piRequestContext(headers) {
22
- if (boundedIdentity(headers.get("x-pi-session-id"), "x-pi-session-id") === undefined) {
23
- return undefined;
24
- }
25
- const kind = headers.get("x-pi-request-kind")?.trim();
26
- return {
27
- requestClass: kind === "turn" ? "main"
28
- : kind === "compaction" ? "compaction"
29
- // The Dari extension's web search tool: one request beside the turn,
30
- // served as background work like Claude Code's own search.
31
- : kind === "search" ? "auxiliary"
32
- : "unknown",
33
- };
34
- }
35
- // OpenCode names the session behind every request, and a subagent's parent
36
- // session too. The `dari opencode` plugin adds the request kind and agent,
37
- // which OpenCode does not send itself. Like Claude Code's headers, these are
38
- // client-supplied routing hints, never inferred from prompt text.
39
- export function openCodeRequestContext(headers) {
40
- if (boundedIdentity(headers.get("x-opencode-session-id"), "x-opencode-session-id") === undefined) {
41
- return undefined;
42
- }
43
- const kind = headers.get("x-opencode-request-kind")?.trim();
44
- const subagentId = openCodeSubagentId(headers);
45
- const requestClass = kind === "compaction" ? "compaction"
46
- : kind === "title" || kind === "generate" ? "auxiliary"
47
- : subagentId === undefined ? "main" : "subagent";
48
- return {
49
- requestClass,
50
- ...(subagentId === undefined ? {} : {
51
- agentId: subagentId,
52
- parentAgentId: boundedIdentity(headers.get("x-opencode-parent-session-id"), "x-opencode-parent-session-id"),
53
- }),
54
- agentType: boundedIdentity(headers.get("x-opencode-agent"), "x-opencode-agent"),
55
- };
56
- }
57
- function openCodeSubagentId(headers) {
58
- const parent = boundedIdentity(headers.get("x-opencode-parent-session-id"), "x-opencode-parent-session-id");
59
- return parent === undefined
60
- ? undefined
61
- : boundedIdentity(headers.get("x-opencode-session-id"), "x-opencode-session-id");
62
- }
63
- export function codexConversationIdentity(headers, payload) {
64
- const clientMetadata = isRecord(payload.client_metadata)
65
- ? payload.client_metadata
66
- : undefined;
67
- const turnMetadata = codexTurnMetadataCollection(headers, payload);
68
- if (turnMetadata.length === 0) {
69
- return undefined;
70
- }
71
- // Clients can carry a stale projection after a resume or fork. Conflicts
72
- // must not fail the request, and fields must not mix across projections:
73
- // take thread, turn, and parent from one turn metadata projection, header
74
- // first, falling back to the standalone thread fields only for the thread.
75
- const entry = turnMetadata.find((metadata) => metadata.thread_id !== undefined)
76
- ?? turnMetadata[0];
77
- const threadId = uuidIdentity(entry.thread_id, "x-codex-turn-metadata.thread_id")
78
- ?? uuidIdentity(headers.get("thread-id"), "thread-id")
79
- ?? uuidIdentity(clientMetadata?.thread_id, "client_metadata.thread_id");
80
- if (threadId === undefined) {
81
- return undefined;
82
- }
83
- const userTurnId = uuidIdentity(entry.turn_id, "x-codex-turn-metadata.turn_id");
84
- const parentThreadId = uuidIdentity(entry.parent_thread_id, "x-codex-turn-metadata.parent_thread_id");
85
- return {
86
- kind: "codex_thread",
87
- value: threadId,
88
- ...(parentThreadId === undefined ? {} : { parentThreadId }),
89
- ...(userTurnId === undefined ? {} : { userTurnId }),
90
- };
91
- }
92
- export function codexRequestPurpose(headers, payload) {
93
- const turnMetadata = codexTurnMetadataCollection(headers, payload);
94
- return turnMetadata.length > 0 && turnMetadata.every(isCodexMemoryExtraction)
95
- ? "memory_extraction"
96
- : undefined;
97
- }
98
- export function codexRequestContext(headers, payload) {
99
- const metadata = codexTurnMetadataCollection(headers, payload);
100
- if (metadata.length === 0)
101
- return undefined;
102
- // Classify from one projection, header first, skipping one that omits the
103
- // request kind so a partial header falls back to a complete body.
104
- const entry = metadata.find((candidate) => candidate.request_kind !== undefined)
105
- ?? metadata[0];
106
- const kind = entry.request_kind;
107
- const agentKind = entry.subagent_kind;
108
- const agentId = uuidIdentity(entry.thread_id, "x-codex-turn-metadata.thread_id");
109
- const requestClass = kind === "compaction" ? "compaction"
110
- : kind === "memory" || kind === "prewarm" ? "auxiliary"
111
- : kind === "turn" && agentKind === "memory_consolidation" ? "auxiliary"
112
- : kind === "turn" && agentKind === "compact" ? "compaction"
113
- : kind === "turn" && agentKind !== undefined ? "subagent"
114
- : kind === "turn" ? "main" : "unknown";
115
- return {
116
- requestClass,
117
- ...(requestClass === "subagent" && agentId !== undefined ? { agentId } : {}),
118
- parentAgentId: uuidIdentity(entry.parent_thread_id, "x-codex-turn-metadata.parent_thread_id"),
119
- agentType: boundedIdentity(agentKind, "x-codex-turn-metadata.subagent_kind"),
120
- };
121
- }
122
- // Muse Code sends its thread on every Responses request. The main agent's
123
- // thread is its session, and each subagent has its own thread, so each keeps
124
- // its own conversation and lease.
125
- export function museCodeConversationIdentity(headers) {
126
- const threadId = boundedIdentity(headers.get("x-tbh-thread-id"), "x-tbh-thread-id");
127
- return threadId === undefined ? undefined : { kind: "muse_code_thread", value: threadId };
128
- }
129
- export function claudeCodeConversationIdentity(headers, metadataSessionId) {
130
- const candidates = [
131
- uuidIdentity(headers.get("x-claude-code-session-id"), "x-claude-code-session-id"),
132
- uuidIdentity(metadataSessionId, "metadata.user_id.session_id"),
133
- ].filter((value) => value !== undefined);
134
- if (candidates.length === 0)
135
- return undefined;
136
- // Claude Code can carry a stale metadata session after a resume or fork.
137
- // The header names the live session, so it wins over the metadata.
138
- const [sessionId] = candidates;
139
- // Claude Code reuses the parent session UUID for every subagent, but gives
140
- // each subagent a stable agent header. Keep the parent thread on the bare
141
- // session identity and scope each subagent to its own composite identity.
142
- // Treat this as an opaque Claude identifier. Besides ordinary subagent IDs,
143
- // Claude may send labeled agents or Agent Teams identities such as
144
- // `reviewer@backend`; Dari only needs a stable, bounded value to hash.
145
- const agentId = boundedIdentity(headers.get("x-claude-code-agent-id"), "x-claude-code-agent-id");
146
- return {
147
- kind: "claude_code_session",
148
- value: sessionId,
149
- ...(agentId === undefined ? {} : { qualifiers: [agentId] }),
150
- };
151
- }
152
- function isCodexMemoryExtraction(metadata) {
153
- if (metadata.request_kind !== "memory" ||
154
- metadata.thread_source !== "memory_consolidation") {
155
- return false;
156
- }
157
- // Codex deliberately omits canonical turn identity for detached phase-1
158
- // memory jobs, while compatibility projections still carry the live root
159
- // thread UUID. Phase-2 consolidation agents use ordinary turn metadata and
160
- // their own thread identities, so they do not match this shape.
161
- return [
162
- "session_id",
163
- "thread_id",
164
- "turn_id",
165
- "root_turn_id",
166
- "window_id",
167
- "forked_from_thread_id",
168
- ].every((field) => metadata[field] === undefined);
169
- }
170
- function codexTurnMetadataCollection(headers, payload) {
171
- const clientMetadata = isRecord(payload.client_metadata)
172
- ? payload.client_metadata
173
- : undefined;
174
- return [
175
- codexTurnMetadata(headers.get("x-codex-turn-metadata"), "x-codex-turn-metadata"),
176
- codexTurnMetadata(clientMetadata?.["x-codex-turn-metadata"], "client_metadata.x-codex-turn-metadata"),
177
- ].filter((value) => value !== undefined);
178
- }
179
- function boundedIdentity(value, field) {
180
- if (value === undefined || value === null)
181
- return undefined;
182
- if (typeof value !== "string") {
183
- throw new HttpError(400, `${field} must be a string.`, "invalid_request_error", field);
184
- }
185
- const normalized = value.trim();
186
- if (!normalized)
187
- return undefined;
188
- if (normalized.length > MAX_ID_CHARS) {
189
- throw new HttpError(400, `${field} must not exceed ${MAX_ID_CHARS} characters.`, "invalid_request_error", field);
190
- }
191
- return normalized;
192
- }
193
- function uuidIdentity(value, field) {
194
- const normalized = boundedIdentity(value, field);
195
- if (normalized === undefined)
196
- return undefined;
197
- const canonical = normalized.toLowerCase();
198
- if (!/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/.test(canonical)) {
199
- throw new HttpError(400, `${field} must be a UUID.`, "invalid_request_error", field);
200
- }
201
- return canonical;
202
- }
203
- function codexTurnMetadata(raw, field) {
204
- if (raw === undefined || raw === null)
205
- return undefined;
206
- if (typeof raw !== "string") {
207
- throw new HttpError(400, `${field} must be a JSON string.`, "invalid_request_error", field);
208
- }
209
- try {
210
- const parsed = JSON.parse(raw);
211
- if (!isRecord(parsed))
212
- throw new Error("not an object");
213
- return parsed;
214
- }
215
- catch {
216
- throw new HttpError(400, `${field} must contain a JSON object.`, "invalid_request_error", field);
217
- }
218
- }
@@ -0,0 +1,2 @@
1
+ export declare function boundedIdentity(value: unknown, field: string): string | undefined;
2
+ export declare function uuidIdentity(value: unknown, field: string): string | undefined;
@@ -0,0 +1,26 @@
1
+ import { HttpError } from "../../http/errors.js";
2
+ const MAX_ID_CHARS = 200;
3
+ export function boundedIdentity(value, field) {
4
+ if (value === undefined || value === null)
5
+ return undefined;
6
+ if (typeof value !== "string") {
7
+ throw new HttpError(400, `${field} must be a string.`, "invalid_request_error", field);
8
+ }
9
+ const normalized = value.trim();
10
+ if (!normalized)
11
+ return undefined;
12
+ if (normalized.length > MAX_ID_CHARS) {
13
+ throw new HttpError(400, `${field} must not exceed ${MAX_ID_CHARS} characters.`, "invalid_request_error", field);
14
+ }
15
+ return normalized;
16
+ }
17
+ export function uuidIdentity(value, field) {
18
+ const normalized = boundedIdentity(value, field);
19
+ if (normalized === undefined)
20
+ return undefined;
21
+ const canonical = normalized.toLowerCase();
22
+ if (!/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/.test(canonical)) {
23
+ throw new HttpError(400, `${field} must be a UUID.`, "invalid_request_error", field);
24
+ }
25
+ return canonical;
26
+ }
@@ -1,2 +1,2 @@
1
1
  export { scopedHarnessAgentId, scopedHarnessConversationId, scopedHarnessSessionId, type HarnessConversationIdentity, type HarnessConversationScope, } from "./conversation.js";
2
- export { chatCompletionsConversationIdentity, claudeCodeConversationIdentity, codexConversationIdentity, codexRequestPurpose, codexRequestContext, museCodeConversationIdentity, openCodeRequestContext, piRequestContext, } from "./extract.js";
2
+ export { chatCompletionsConversationIdentity } from "./extract.js";
@@ -1,2 +1,2 @@
1
1
  export { scopedHarnessAgentId, scopedHarnessConversationId, scopedHarnessSessionId, } from "./conversation.js";
2
- export { chatCompletionsConversationIdentity, claudeCodeConversationIdentity, codexConversationIdentity, codexRequestPurpose, codexRequestContext, museCodeConversationIdentity, openCodeRequestContext, piRequestContext, } from "./extract.js";
2
+ export { chatCompletionsConversationIdentity } from "./extract.js";
@@ -56,7 +56,8 @@ export async function handleRoutedCompletion(args) {
56
56
  for (const [name, value] of Object.entries(args.responseHeaders?.(prepared.credentials) ?? {})) {
57
57
  response.headers.set(name, value);
58
58
  }
59
- return responseWithRequestId(response, requestId);
59
+ // Its X-Request-Id is the request the client is told it made.
60
+ return response;
60
61
  }
61
62
  catch (error) {
62
63
  const terminal = prepared.reporter.freezeFailure(error);
@@ -1,5 +1,5 @@
1
1
  import { isReasoningEffort, isRecord, providerForModel, } from "@mupt-ai/dari-router/policy-engine";
2
- import { CLIENT_HARNESSES } from "../ingress/client_harness.js";
2
+ import { CLIENT_HARNESSES } from "../harnesses/index.js";
3
3
  import { checkSources, DEFAULT_RESET_MIN_HOURS } from "../models/sources.js";
4
4
  import { modelKeyProviders, otherModelProviders, providerModel } from "../models/canonical.js";
5
5
  const REDIRECT_HOST_PATTERN = /^(\*\.)?[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/;
@@ -1,5 +1,5 @@
1
1
  import { errorResponse, HttpError } from "../http/errors.js";
2
- import { CLIENT_HARNESSES } from "../ingress/client_harness.js";
2
+ import { CLIENT_HARNESSES } from "../harnesses/index.js";
3
3
  import { DEFAULT_ROUTER_ALIAS } from "../platform/routing_contract.js";
4
4
  import { DEFAULT_GATEWAY_CONFIG, parseRouterModels, } from "./config.js";
5
5
  // A router's own list is saved under no agent.
package/dist/types.d.ts CHANGED
@@ -48,7 +48,7 @@ export type PublicEvalCatalogCard = Omit<RouterEval, "scores"> & {
48
48
  export type ServedDecision = RoutingDecision & {
49
49
  sessionPinned?: boolean;
50
50
  };
51
- export type ClientHarness = "claude_code" | "codex" | "muse_code" | "opencode" | "pi" | "openclaw" | "hermes";
51
+ export type { ClientHarness } from "./harnesses/index.js";
52
52
  export type ResolvedRouterModelConfig = {
53
53
  provider: string;
54
54
  native_model_id: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mupt-ai/dari-gateway",
3
- "version": "1.0.369",
3
+ "version": "1.0.370",
4
4
  "description": "Self-hostable LLM gateway for coding agents and APIs: one endpoint for Anthropic Messages, OpenAI Chat Completions, and OpenAI Responses, with subscription pooling, failover, and prompt-cache-aware routing.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -52,7 +52,7 @@
52
52
  },
53
53
  "dependencies": {
54
54
  "@aws-sdk/client-sts": "3.1063.0",
55
- "@mupt-ai/dari-router": "1.0.369",
55
+ "@mupt-ai/dari-router": "1.0.370",
56
56
  "@mupt-ai/pi-ai": "1.5.2-mupt.3"
57
57
  },
58
58
  "devDependencies": {
@@ -1,5 +1,24 @@
1
- import { HttpError } from "../../http/errors.js";
2
- import type { AgentRequestContext, AgentRequestClass } from "../../interaction/types.js";
1
+ import { HttpError } from "../http/errors.js";
2
+ import type { AgentRequestContext, AgentRequestClass } from "../interaction/types.js";
3
+ import { boundedIdentity, uuidIdentity } from "../runtime/harness_identity/ids.js";
4
+ import type { HarnessConversationIdentity } from "../runtime/harness_identity/conversation.js";
5
+ import type { Harness } from "./index.js";
6
+
7
+ export const claudeCode = {
8
+ id: "claude_code",
9
+ headers: [
10
+ "user-agent",
11
+ "x-app",
12
+ "anthropic-beta",
13
+ "x-claude-code-session-id",
14
+ "x-claude-code-request-class",
15
+ "x-claude-code-agent-id",
16
+ "x-claude-code-parent-agent-id",
17
+ "x-claude-code-agent-type",
18
+ ],
19
+ // A Claude Code plan serves only Claude Code.
20
+ ownPlanOnly: true,
21
+ } as const satisfies Harness;
3
22
 
4
23
  const REQUEST_CLASSES = new Set<AgentRequestClass>([
5
24
  "main", "subagent", "workflow", "auxiliary", "compaction", "side_reply",
@@ -51,3 +70,38 @@ function identifier(headers: Headers, name: string): string | undefined {
51
70
  }
52
71
  return value;
53
72
  }
73
+
74
+ export function claudeCodeConversationIdentity(
75
+ headers: Headers,
76
+ metadataSessionId?: string,
77
+ ): HarnessConversationIdentity | undefined {
78
+ const candidates = [
79
+ uuidIdentity(
80
+ headers.get("x-claude-code-session-id"),
81
+ "x-claude-code-session-id",
82
+ ),
83
+ uuidIdentity(
84
+ metadataSessionId,
85
+ "metadata.user_id.session_id",
86
+ ),
87
+ ].filter((value): value is string => value !== undefined);
88
+ if (candidates.length === 0) return undefined;
89
+ // Claude Code can carry a stale metadata session after a resume or fork.
90
+ // The header names the live session, so it wins over the metadata.
91
+ const [sessionId] = candidates;
92
+ // Claude Code reuses the parent session UUID for every subagent, but gives
93
+ // each subagent a stable agent header. Keep the parent thread on the bare
94
+ // session identity and scope each subagent to its own composite identity.
95
+ // Treat this as an opaque Claude identifier. Besides ordinary subagent IDs,
96
+ // Claude may send labeled agents or Agent Teams identities such as
97
+ // `reviewer@backend`; Dari only needs a stable, bounded value to hash.
98
+ const agentId = boundedIdentity(
99
+ headers.get("x-claude-code-agent-id"),
100
+ "x-claude-code-agent-id",
101
+ );
102
+ return {
103
+ kind: "claude_code_session",
104
+ value: sessionId,
105
+ ...(agentId === undefined ? {} : { qualifiers: [agentId] }),
106
+ };
107
+ }
@@ -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,8 @@
1
+ import type { Harness } from "./index.js";
2
+
3
+ // Sends nothing of its own that tells it apart, so the Dari CLI sets it up to
4
+ // name itself in x-dari-client-harness.
5
+ export const hermes = {
6
+ id: "hermes",
7
+ headers: [],
8
+ } as const satisfies Harness;
@@ -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,8 @@
1
+ import type { Harness } from "./index.js";
2
+
3
+ // Sends nothing of its own that tells it apart, so the Dari CLI sets it up to
4
+ // name itself in x-dari-client-harness.
5
+ export const openclaw = {
6
+ id: "openclaw",
7
+ headers: [],
8
+ } as const satisfies Harness;
@@ -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
+ }