@mupt-ai/dari-gateway 1.0.374 → 1.0.375
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/activity/filters.d.ts +1 -1
- package/dist/activity/filters.js +1 -1
- package/dist/harnesses/antigravity.d.ts +11 -0
- package/dist/harnesses/antigravity.js +47 -0
- package/dist/harnesses/index.d.ts +3 -0
- package/dist/harnesses/index.js +2 -1
- package/dist/http/auth.d.ts +1 -0
- package/dist/http/auth.js +11 -0
- package/dist/http/handler.d.ts +1 -1
- package/dist/http/handler.js +16 -1
- package/dist/ingress/gemini.d.ts +18 -0
- package/dist/ingress/gemini.js +181 -0
- package/dist/ingress/harness_request.js +6 -2
- package/dist/interaction/types.d.ts +1 -1
- package/dist/protocol/gemini_thought_signature.d.ts +2 -0
- package/dist/protocol/gemini_thought_signature.js +23 -0
- package/dist/protocol/input/gemini_generate_content.d.ts +4 -0
- package/dist/protocol/input/gemini_generate_content.js +293 -0
- package/dist/protocol/input/router_model.d.ts +1 -0
- package/dist/protocol/input/router_model.js +7 -0
- package/dist/protocol/output/gemini_generate_content.d.ts +17 -0
- package/dist/protocol/output/gemini_generate_content.js +102 -0
- package/dist/protocol/output/gemini_generate_content_stream.d.ts +17 -0
- package/dist/protocol/output/gemini_generate_content_stream.js +86 -0
- package/dist/runtime/harness_identity/conversation.d.ts +4 -0
- package/dist/self_host/connected.js +14 -7
- package/package.json +2 -2
- package/src/activity/filters.ts +1 -1
- package/src/harnesses/antigravity.ts +50 -0
- package/src/harnesses/index.ts +2 -1
- package/src/http/auth.ts +11 -0
- package/src/http/handler.ts +20 -1
- package/src/ingress/gemini.ts +206 -0
- package/src/ingress/harness_request.ts +5 -1
- package/src/interaction/types.ts +1 -1
- package/src/protocol/gemini_thought_signature.ts +23 -0
- package/src/protocol/input/gemini_generate_content.ts +297 -0
- package/src/protocol/input/router_model.ts +8 -0
- package/src/protocol/output/gemini_generate_content.ts +108 -0
- package/src/protocol/output/gemini_generate_content_stream.ts +101 -0
- package/src/runtime/harness_identity/conversation.ts +5 -0
- package/src/self_host/connected.ts +14 -7
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { isRecord } from "@mupt-ai/dari-router/policy-engine";
|
|
2
|
+
|
|
3
|
+
import type { HarnessConversationIdentity } from "../runtime/harness_identity/conversation.js";
|
|
4
|
+
import type { Harness } from "./index.js";
|
|
5
|
+
|
|
6
|
+
// Antigravity (the CLI under `dari agy`, and the desktop app) sends no header
|
|
7
|
+
// of its own: the gateway tells it apart by its system prompt.
|
|
8
|
+
export const antigravity = {
|
|
9
|
+
id: "antigravity",
|
|
10
|
+
headers: [],
|
|
11
|
+
} as const satisfies Harness;
|
|
12
|
+
|
|
13
|
+
// Its system prompt names its app data directory (`antigravity-cli` for the
|
|
14
|
+
// CLI, `antigravity` for the desktop app) and the conversation.
|
|
15
|
+
const ANTIGRAVITY_CONVERSATION =
|
|
16
|
+
/App Data Directory: [^\n]*antigravity[^\n]*\s+Conversation ID: ([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Antigravity's conversation, from its system prompt in whichever API
|
|
20
|
+
* carries it: Gemini's from the agent, or Anthropic's or Responses when a
|
|
21
|
+
* connected gateway has hosted Dari serve the model.
|
|
22
|
+
*/
|
|
23
|
+
export function antigravityConversationIdentity(
|
|
24
|
+
payload: Record<string, unknown>,
|
|
25
|
+
): HarnessConversationIdentity | undefined {
|
|
26
|
+
for (const text of systemTexts(payload)) {
|
|
27
|
+
const match = ANTIGRAVITY_CONVERSATION.exec(text);
|
|
28
|
+
if (match !== null) return { kind: "antigravity_conversation", value: match[1]! };
|
|
29
|
+
}
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function systemTexts(payload: Record<string, unknown>): string[] {
|
|
34
|
+
const texts: string[] = [];
|
|
35
|
+
const add = (value: unknown) => {
|
|
36
|
+
if (typeof value === "string") texts.push(value);
|
|
37
|
+
else if (Array.isArray(value)) for (const part of value) if (isRecord(part) && typeof part.text === "string") texts.push(part.text);
|
|
38
|
+
};
|
|
39
|
+
// Gemini's systemInstruction, Anthropic's system, and the Responses API's
|
|
40
|
+
// instructions or system and developer messages.
|
|
41
|
+
if (isRecord(payload.systemInstruction)) add(payload.systemInstruction.parts);
|
|
42
|
+
add(payload.system);
|
|
43
|
+
add(payload.instructions);
|
|
44
|
+
if (Array.isArray(payload.input)) {
|
|
45
|
+
for (const item of payload.input) {
|
|
46
|
+
if (isRecord(item) && (item.role === "system" || item.role === "developer")) add(item.content);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return texts;
|
|
50
|
+
}
|
package/src/harnesses/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { antigravity } from "./antigravity.js";
|
|
1
2
|
import { claudeCode } from "./claude_code.js";
|
|
2
3
|
import { codex } from "./codex.js";
|
|
3
4
|
import { hermes } from "./hermes.js";
|
|
@@ -17,7 +18,7 @@ export type Harness = {
|
|
|
17
18
|
ownPlanOnly?: true;
|
|
18
19
|
};
|
|
19
20
|
|
|
20
|
-
export const HARNESSES = [claudeCode, codex, museCode, opencode, pi, openclaw, hermes] as const;
|
|
21
|
+
export const HARNESSES = [claudeCode, codex, museCode, opencode, pi, openclaw, hermes, antigravity] as const;
|
|
21
22
|
|
|
22
23
|
export type ClientHarness = (typeof HARNESSES)[number]["id"];
|
|
23
24
|
|
package/src/http/auth.ts
CHANGED
|
@@ -21,6 +21,17 @@ export function anthropicToken(headers: Headers): string {
|
|
|
21
21
|
return publicRoutingToken(apiKey);
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
// Gemini clients send `x-goog-api-key`, or a bearer token when
|
|
25
|
+
// GEMINI_API_KEY_AUTH_MECHANISM=bearer.
|
|
26
|
+
export function geminiToken(headers: Headers): string {
|
|
27
|
+
if (headers.get("authorization")) return bearerToken(headers);
|
|
28
|
+
const apiKey = headers.get("x-goog-api-key");
|
|
29
|
+
if (!apiKey) {
|
|
30
|
+
throw new HttpError(401, "Missing or invalid API key.", "authentication_error");
|
|
31
|
+
}
|
|
32
|
+
return publicRoutingToken(apiKey);
|
|
33
|
+
}
|
|
34
|
+
|
|
24
35
|
function publicRoutingToken(token: string): string {
|
|
25
36
|
const internalToken = process.env.DARI_ROUTING_INTERNAL_TOKEN;
|
|
26
37
|
if (internalToken && matchesInternalToken(token, internalToken)) {
|
package/src/http/handler.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { handleModels } from "../ingress/models.js";
|
|
|
9
9
|
import { handleMuseCodeModels } from "../ingress/muse_models.js";
|
|
10
10
|
import { handleChatCompletions } from "../ingress/chat_completions.js";
|
|
11
11
|
import { anthropicErrorResponse, handleAnthropicMessages } from "../ingress/anthropic_messages.js";
|
|
12
|
+
import { handleGeminiModels, handleGeminiRequest } from "../ingress/gemini.js";
|
|
12
13
|
import { bindGatewayPlatform, type GatewayPlatform } from "../platform/platform.js";
|
|
13
14
|
import { DEFAULT_ROUTER_ALIAS } from "../platform/routing_contract.js";
|
|
14
15
|
import type { SelectorBackend } from "../strategies/routing.js";
|
|
@@ -21,7 +22,7 @@ export type GatewayHandlerOptions = {
|
|
|
21
22
|
|
|
22
23
|
/**
|
|
23
24
|
* The gateway's routed HTTP surface: `/v1/{chat/completions,responses,
|
|
24
|
-
* messages,models}` on the default router and `/{router_id}/v1/...` on a
|
|
25
|
+
* messages,models}` and Gemini's `/v1beta/models` and `/v1beta/models/{model}:{method}` on the default router and `/{router_id}/v1/...` on a
|
|
25
26
|
* named one, the Dari CLI's commit reports at `/v1/dari/git-events` on
|
|
26
27
|
* either, the Dari Link grant endpoints under `/v1/link/`, and the Activity
|
|
27
28
|
* reads under `/v1/organizations/{organization}/routing/activity/`. Binds
|
|
@@ -59,6 +60,24 @@ export function createRequestHandler(options: GatewayHandlerOptions) {
|
|
|
59
60
|
return await handleGitEvents(request, gitEvents[1] ?? DEFAULT_ROUTER_ALIAS, options.platform.activity);
|
|
60
61
|
}
|
|
61
62
|
|
|
63
|
+
// Gemini CLI: `{base}/v1beta/models/{model}:{method}`, where the model may
|
|
64
|
+
// hold slashes, raw or percent-encoded.
|
|
65
|
+
const gemini = url.pathname.match(
|
|
66
|
+
/^(?:\/([^/]+))?\/v1beta\/models\/(.+):(generateContent|streamGenerateContent|countTokens)$/,
|
|
67
|
+
);
|
|
68
|
+
if (request.method === "POST" && gemini !== null) {
|
|
69
|
+
return await handleGeminiRequest(request, {
|
|
70
|
+
routerId: gemini[1] ?? DEFAULT_ROUTER_ALIAS,
|
|
71
|
+
model: decodeURIComponent(gemini[2]!),
|
|
72
|
+
method: gemini[3] as "generateContent" | "streamGenerateContent" | "countTokens",
|
|
73
|
+
}, selectorBackend);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const geminiModels = url.pathname.match(/^(?:\/([^/]+))?\/v1beta\/models$/);
|
|
77
|
+
if (request.method === "GET" && geminiModels !== null) {
|
|
78
|
+
return await handleGeminiModels(request, geminiModels[1] ?? DEFAULT_ROUTER_ALIAS);
|
|
79
|
+
}
|
|
80
|
+
|
|
62
81
|
const endpoint = routedEndpoint(url.pathname);
|
|
63
82
|
if (endpoint !== null) {
|
|
64
83
|
// Browsers send Origin on every cross-origin call; a request without
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import type { ReasoningEffort } from "@mupt-ai/dari-router/policy-engine";
|
|
2
|
+
|
|
3
|
+
import { geminiToken } from "../http/auth.js";
|
|
4
|
+
import { errorResponseHeaders, HttpError, normalizeError } from "../http/errors.js";
|
|
5
|
+
import { LINK_GRANT_ERROR_CODES, LINK_TOKEN_ERROR_CODES } from "../link/errors.js";
|
|
6
|
+
import { captureRequestError } from "../observability/errors.js";
|
|
7
|
+
import { geminiGenerateContentToInteraction } from "../protocol/input/gemini_generate_content.js";
|
|
8
|
+
import { listedContextWindow } from "../models/context_limits.js";
|
|
9
|
+
import { CANONICAL_ROUTER_MODEL, explicitModelId, explicitModelSlug, linkTokenModel } from "../protocol/input/router_model.js";
|
|
10
|
+
import { geminiGenerateContentResponse } from "../protocol/output/gemini_generate_content.js";
|
|
11
|
+
import { geminiGenerateContentSseStream } from "../protocol/output/gemini_generate_content_stream.js";
|
|
12
|
+
import { modelMetadataLookup } from "../providers/pi.js";
|
|
13
|
+
import { handleRoutedCompletion } from "../runtime/routed_completion.js";
|
|
14
|
+
import { resolveRouter, type SelectorBackend } from "../strategies/routing.js";
|
|
15
|
+
import type { ResolvedRouter } from "../types.js";
|
|
16
|
+
import { namedClientHarness } from "./client_harness.js";
|
|
17
|
+
import { harnessRequest } from "./harness_request.js";
|
|
18
|
+
import { parseJsonRequest } from "./json.js";
|
|
19
|
+
|
|
20
|
+
export type GeminiRoute = {
|
|
21
|
+
routerId: string;
|
|
22
|
+
model: string;
|
|
23
|
+
method: "generateContent" | "streamGenerateContent" | "countTokens";
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export async function handleGeminiRequest(
|
|
27
|
+
request: Request,
|
|
28
|
+
route: GeminiRoute,
|
|
29
|
+
selectorBackend: SelectorBackend,
|
|
30
|
+
): Promise<Response> {
|
|
31
|
+
try {
|
|
32
|
+
const apiKey = geminiToken(request.headers);
|
|
33
|
+
const rawPayload = await parseJsonRequest<Record<string, unknown>>(request);
|
|
34
|
+
if (route.method === "countTokens") return Response.json({ totalTokens: estimatedTokens(rawPayload) });
|
|
35
|
+
const named = modelAtLevel(route.model);
|
|
36
|
+
const model = String(linkTokenModel(apiKey, named.model));
|
|
37
|
+
const interaction = geminiGenerateContentToInteraction({ ...rawPayload, model }, {
|
|
38
|
+
stream: route.method === "streamGenerateContent",
|
|
39
|
+
});
|
|
40
|
+
const harness = harnessRequest(request.headers, {
|
|
41
|
+
sourceProtocol: "gemini_generate_content",
|
|
42
|
+
payload: rawPayload,
|
|
43
|
+
promptCacheKey: interaction.promptCacheKey,
|
|
44
|
+
});
|
|
45
|
+
// The client's thinking settings describe one Gemini model; when the
|
|
46
|
+
// router routes, Dari picks the effort. A level in the model's name is
|
|
47
|
+
// the one the user picked, whatever thinking budget comes with it.
|
|
48
|
+
const reasoning = explicitModelId(interaction.model) === null ? undefined
|
|
49
|
+
: named.effort === undefined ? interaction.reasoning : { effort: named.effort, enabled: true };
|
|
50
|
+
const payload = {
|
|
51
|
+
...interaction,
|
|
52
|
+
reasoning,
|
|
53
|
+
...(harness.agentContext === undefined ? {} : { agentContext: harness.agentContext }),
|
|
54
|
+
...(harness.requestPurpose === undefined ? {} : { requestPurpose: harness.requestPurpose }),
|
|
55
|
+
};
|
|
56
|
+
return await handleRoutedCompletion({
|
|
57
|
+
request,
|
|
58
|
+
routerId: route.routerId,
|
|
59
|
+
apiKey,
|
|
60
|
+
payload,
|
|
61
|
+
responseBody: geminiGenerateContentResponse,
|
|
62
|
+
streamBody: geminiGenerateContentSseStream,
|
|
63
|
+
errorResponse: geminiErrorResponse,
|
|
64
|
+
selectorBackend,
|
|
65
|
+
clientHarness: harness.clientHarness,
|
|
66
|
+
harnessConversationIdentity: harness.conversationIdentity,
|
|
67
|
+
});
|
|
68
|
+
} catch (error) {
|
|
69
|
+
const response = geminiErrorResponse(error);
|
|
70
|
+
captureRequestError(error, {
|
|
71
|
+
request,
|
|
72
|
+
routerId: route.routerId,
|
|
73
|
+
sourceProtocol: "gemini_generate_content",
|
|
74
|
+
status: response.status,
|
|
75
|
+
});
|
|
76
|
+
return response;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Antigravity CLI's own output limit, which it uses for a model that states
|
|
81
|
+
// none, and the least it accepts for the conversation: a model's window less
|
|
82
|
+
// the output it asks for.
|
|
83
|
+
const ANTIGRAVITY_OUTPUT_TOKENS = 64_000;
|
|
84
|
+
const ANTIGRAVITY_CONVERSATION_TOKENS = 20_000;
|
|
85
|
+
|
|
86
|
+
/* Antigravity CLI changes a model's thinking level only between models whose
|
|
87
|
+
names end in its own levels, as `dari/model/<id>-high` shown as
|
|
88
|
+
`<id> (High)`: it lists them as one model with an effort gauge in /model
|
|
89
|
+
and /effort, and shows the level beside the model. */
|
|
90
|
+
const ANTIGRAVITY_LEVELS = { low: "Low", medium: "Medium", high: "High", xhigh: "XHigh", max: "Max" } as const satisfies Partial<Record<ReasoningEffort, string>>;
|
|
91
|
+
const MODEL_AT_LEVEL = new RegExp(`^(.+)-(${Object.keys(ANTIGRAVITY_LEVELS).join("|")})$`);
|
|
92
|
+
|
|
93
|
+
// The `dari/model/<id>` and thinking level a name from the model list holds.
|
|
94
|
+
function modelAtLevel(model: string): { model: string; effort?: ReasoningEffort } {
|
|
95
|
+
const match = explicitModelId(model) === null ? null : MODEL_AT_LEVEL.exec(model);
|
|
96
|
+
return match === null ? { model } : { model: match[1]!, effort: match[2] as ReasoningEffort };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The Gemini API's model list. Antigravity CLI reads it from its model
|
|
101
|
+
* gateway and offers these models and no others: `dari/routing` when the
|
|
102
|
+
* router routes, then each of its models at each thinking level it serves. It
|
|
103
|
+
* keeps a conversation within `inputTokenLimit` less `outputTokenLimit`, the
|
|
104
|
+
* output it asks for, and asks a model not marked `thinking` for none.
|
|
105
|
+
*/
|
|
106
|
+
export function geminiModelsResponse(
|
|
107
|
+
router: Pick<ResolvedRouter, "enabled_models" | "model_configs" | "model_thinking_levels" | "allow_long_context" | "autorouting_enabled" | "routing_listed">,
|
|
108
|
+
): Record<string, unknown> {
|
|
109
|
+
const metadata = modelMetadataLookup(router.model_configs);
|
|
110
|
+
const limits = (model: string) => {
|
|
111
|
+
const { model: known, supportedThinkingLevels } = metadata(model);
|
|
112
|
+
const enabled: readonly string[] = router.model_thinking_levels?.[model] ?? supportedThinkingLevels;
|
|
113
|
+
return {
|
|
114
|
+
input: listedContextWindow(router, [model]),
|
|
115
|
+
output: Number(known.maxTokens),
|
|
116
|
+
levels: supportedThinkingLevels.filter((level) => level !== "off" && enabled.includes(level)),
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
// Routing lists its largest model's window, as it serves a long conversation on the models that fit it.
|
|
120
|
+
const listed = (id: string, displayName: string, models: string[]) => {
|
|
121
|
+
const served = models.map(limits);
|
|
122
|
+
const input = Math.max(...served.map(({ input }) => input));
|
|
123
|
+
return {
|
|
124
|
+
name: `models/${id}`,
|
|
125
|
+
displayName,
|
|
126
|
+
...(served.length === 0 ? {} : {
|
|
127
|
+
inputTokenLimit: input,
|
|
128
|
+
// A small window gives up output to leave room for the conversation.
|
|
129
|
+
outputTokenLimit: Math.max(1, Math.min(ANTIGRAVITY_OUTPUT_TOKENS, input - ANTIGRAVITY_CONVERSATION_TOKENS, ...served.map(({ output }) => output))),
|
|
130
|
+
}),
|
|
131
|
+
thinking: served.length === 0 || served.some(({ levels }) => levels.length > 0),
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
return {
|
|
135
|
+
models: [
|
|
136
|
+
...(router.autorouting_enabled === false || router.routing_listed === false ? [] : [listed(CANONICAL_ROUTER_MODEL, "Dari Routing", router.enabled_models)]),
|
|
137
|
+
...router.enabled_models.flatMap((model) => {
|
|
138
|
+
// Antigravity starts on the first model listed when its saved pick is
|
|
139
|
+
// not one of these, so each model's medium level comes first.
|
|
140
|
+
const levels = Object.entries(ANTIGRAVITY_LEVELS)
|
|
141
|
+
.filter(([level]) => limits(model).levels.includes(level))
|
|
142
|
+
.sort(([a], [b]) => Number(b === "medium") - Number(a === "medium"));
|
|
143
|
+
return levels.length === 0
|
|
144
|
+
? [listed(explicitModelSlug(model), model, [model])]
|
|
145
|
+
: levels.map(([level, label]) => listed(`${explicitModelSlug(model)}-${level}`, `${model} (${label})`, [model]));
|
|
146
|
+
}),
|
|
147
|
+
],
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export async function handleGeminiModels(request: Request, routerId: string): Promise<Response> {
|
|
152
|
+
try {
|
|
153
|
+
// The Dari CLI names the agent it launched, whose models the router may set apart.
|
|
154
|
+
const clientHarness = namedClientHarness(request.headers);
|
|
155
|
+
const router = await resolveRouter({
|
|
156
|
+
routerId,
|
|
157
|
+
apiKey: geminiToken(request.headers),
|
|
158
|
+
...(clientHarness ? { clientHarness } : {}),
|
|
159
|
+
});
|
|
160
|
+
return Response.json(geminiModelsResponse(router), { headers: { "Cache-Control": "no-store" } });
|
|
161
|
+
} catch (error) {
|
|
162
|
+
return geminiErrorResponse(error);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// The gateway counts no tokens; Gemini CLI only needs a size for non-text
|
|
167
|
+
// parts, so this is the usual four characters per token.
|
|
168
|
+
function estimatedTokens(payload: Record<string, unknown>): number {
|
|
169
|
+
return Math.ceil(JSON.stringify(payload.contents ?? []).length / 4);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Dari-specific codes clients act on; every other code stays internal.
|
|
173
|
+
const CLIENT_VISIBLE_ERROR_CODES = new Set<string>([
|
|
174
|
+
...LINK_GRANT_ERROR_CODES,
|
|
175
|
+
...LINK_TOKEN_ERROR_CODES,
|
|
176
|
+
"personal_subscription_unavailable",
|
|
177
|
+
"subscription_switched",
|
|
178
|
+
"subscription_usage_limit",
|
|
179
|
+
]);
|
|
180
|
+
|
|
181
|
+
export function geminiErrorResponse(error: unknown): Response {
|
|
182
|
+
const { status, message, code } = normalizeError(error);
|
|
183
|
+
return Response.json(
|
|
184
|
+
{
|
|
185
|
+
error: {
|
|
186
|
+
code: status,
|
|
187
|
+
message,
|
|
188
|
+
status: geminiStatus(status),
|
|
189
|
+
...(CLIENT_VISIBLE_ERROR_CODES.has(code) ? { dari_code: code } : {}),
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
{ status, headers: errorResponseHeaders(error) },
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function geminiStatus(status: number): string {
|
|
197
|
+
switch (status) {
|
|
198
|
+
case 400: return "INVALID_ARGUMENT";
|
|
199
|
+
case 401: return "UNAUTHENTICATED";
|
|
200
|
+
case 403: return "PERMISSION_DENIED";
|
|
201
|
+
case 404: return "NOT_FOUND";
|
|
202
|
+
case 429: return "RESOURCE_EXHAUSTED";
|
|
203
|
+
case 503: return "UNAVAILABLE";
|
|
204
|
+
default: return status >= 500 ? "INTERNAL" : "INVALID_ARGUMENT";
|
|
205
|
+
}
|
|
206
|
+
}
|
|
@@ -3,6 +3,7 @@ import type {
|
|
|
3
3
|
InteractionRequestPurpose,
|
|
4
4
|
SourceProtocol,
|
|
5
5
|
} from "../interaction/types.js";
|
|
6
|
+
import { antigravityConversationIdentity } from "../harnesses/antigravity.js";
|
|
6
7
|
import {
|
|
7
8
|
claudeCodeConversationIdentity,
|
|
8
9
|
claudeCodeRequestContext,
|
|
@@ -70,16 +71,19 @@ export function harnessRequest(headers: Headers, body: {
|
|
|
70
71
|
const session = chat || piContext !== undefined || openCodeContext !== undefined
|
|
71
72
|
? chatCompletionsConversationIdentity(headers, chat ? body.promptCacheKey : undefined)
|
|
72
73
|
: undefined;
|
|
74
|
+
const antigravity = chat || codexContext !== undefined ? undefined : antigravityConversationIdentity(body.payload);
|
|
73
75
|
const conversationIdentity = chat ? session
|
|
74
76
|
: codexConversationIdentity(headers, body.payload)
|
|
75
77
|
?? (museClientId === undefined ? undefined : museCodeConversationIdentity(headers))
|
|
76
|
-
?? session
|
|
78
|
+
?? session
|
|
79
|
+
?? antigravity;
|
|
77
80
|
// OpenClaw and Hermes send nothing of their own that tells them apart, so
|
|
78
81
|
// the Dari CLI sets them up to name themselves.
|
|
79
82
|
const clientHarness: ClientHarness | undefined = museClientId !== undefined ? "muse_code"
|
|
80
83
|
: codexContext !== undefined ? "codex"
|
|
81
84
|
: piContext !== undefined ? "pi"
|
|
82
85
|
: openCodeContext !== undefined ? "opencode"
|
|
86
|
+
: antigravity !== undefined ? "antigravity"
|
|
83
87
|
: claimedClientHarness(headers);
|
|
84
88
|
return {
|
|
85
89
|
...(clientHarness === undefined ? {} : { clientHarness }),
|
package/src/interaction/types.ts
CHANGED
|
@@ -9,7 +9,7 @@ import type {
|
|
|
9
9
|
ProviderIdentity,
|
|
10
10
|
} from "@mupt-ai/dari-router/protocols";
|
|
11
11
|
|
|
12
|
-
export type SourceProtocol = "anthropic_messages" | "openai_chat" | "openai_responses";
|
|
12
|
+
export type SourceProtocol = "anthropic_messages" | "openai_chat" | "openai_responses" | "gemini_generate_content";
|
|
13
13
|
export type AgentRequestClass =
|
|
14
14
|
| "main" | "subagent" | "workflow" | "auxiliary" | "compaction" | "side_reply" | "unknown";
|
|
15
15
|
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/* Gemini clients hand back a part's `thoughtSignature` verbatim on the next
|
|
2
|
+
turn. Dari puts the turn's reasoning state there: each reasoning item's
|
|
3
|
+
continuation envelope (or portable state), as a JSON array behind a prefix
|
|
4
|
+
that tells it apart from Google's own signatures. The field is bytes, which
|
|
5
|
+
clients such as Antigravity CLI parse, so the whole value is base64. */
|
|
6
|
+
const PREFIX = "dari_reasoning:";
|
|
7
|
+
|
|
8
|
+
export function encodeGeminiThoughtSignature(states: readonly string[]): string {
|
|
9
|
+
return Buffer.from(PREFIX + JSON.stringify(states)).toString("base64");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// Null for any signature Dari did not write, including the clients' synthetic
|
|
13
|
+
// `skip_thought_signature_validator`.
|
|
14
|
+
export function decodeGeminiThoughtSignature(signature: string): string[] | null {
|
|
15
|
+
const decoded = Buffer.from(signature, "base64").toString("utf8");
|
|
16
|
+
if (!decoded.startsWith(PREFIX)) return null;
|
|
17
|
+
try {
|
|
18
|
+
const states: unknown = JSON.parse(decoded.slice(PREFIX.length));
|
|
19
|
+
return Array.isArray(states) && states.every((state) => typeof state === "string") ? states : null;
|
|
20
|
+
} catch {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}
|