dsh-coding-subscription-oauth 0.6.2 → 0.6.3
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/CHANGELOG.md +222 -206
- package/CONTRIBUTING.md +129 -120
- package/INSTALL.md +218 -218
- package/LICENSE +19 -19
- package/NOTICE +11 -11
- package/README.de.md +289 -289
- package/README.es.md +290 -290
- package/README.fr.md +290 -290
- package/README.ja.md +290 -290
- package/README.ko.md +290 -290
- package/README.md +304 -304
- package/README.pt-BR.md +290 -290
- package/README.ru.md +290 -290
- package/README.zh-CN.md +302 -302
- package/compatibility/dsh-bom.json +0 -0
- package/cordis.patch.yml +13 -13
- package/docs/00-project-rules.md +212 -195
- package/docs/02-architecture.md +130 -130
- package/docs/02-architecture.zh-CN.md +130 -130
- package/lib/bin.js.map +1 -1
- package/lib/capability-settings.d.ts +8 -2
- package/lib/capability-settings.d.ts.map +1 -1
- package/lib/capability-tools.d.ts +7 -2
- package/lib/capability-tools.d.ts.map +1 -1
- package/lib/client.js +3 -3
- package/lib/client.js.map +2 -2
- package/lib/codex-images.d.ts +8 -0
- package/lib/codex-images.d.ts.map +1 -1
- package/lib/index.js +32 -7
- package/lib/index.js.map +2 -2
- package/lib/invariant.js.map +1 -1
- package/media/en/settings_accounts.png +0 -0
- package/media/en/settings_capabilities.png +0 -0
- package/media/en/settings_gateway.png +0 -0
- package/media/settings_accounts.png +0 -0
- package/media/settings_capabilities.png +0 -0
- package/media/settings_gateway.png +0 -0
- package/media/settings_overview.png +0 -0
- package/media/zh-CN/settings_accounts.png +0 -0
- package/media/zh-CN/settings_capabilities.png +0 -0
- package/media/zh-CN/settings_gateway.png +0 -0
- package/package.json +148 -148
- package/patches/dsh-agy@0.1.2.patch +25 -25
- package/scripts/release.mjs +186 -186
- package/scripts/smoke-deployed-routes.mjs +146 -146
- package/scripts/verify-deployed-catalog.mjs +87 -87
- package/src/adapter.ts +273 -273
- package/src/alias-adapter.ts +130 -130
- package/src/auth-routes.ts +921 -921
- package/src/auth.ts +67 -67
- package/src/bin.ts +350 -350
- package/src/capability-routes.ts +279 -279
- package/src/capability-runtime.ts +314 -313
- package/src/capability-settings.ts +671 -658
- package/src/capability-tools.ts +685 -666
- package/src/catalog.ts +271 -271
- package/src/client/GrokBuildSettings.tsx +771 -770
- package/src/client/api.ts +88 -88
- package/src/client/components/AboutTab.tsx +30 -30
- package/src/client/components/AccountsTab.tsx +241 -241
- package/src/client/components/Badge.tsx +33 -33
- package/src/client/components/CapabilitiesTab.tsx +265 -265
- package/src/client/components/CliPullPreview.tsx +116 -116
- package/src/client/components/CopyButton.tsx +57 -57
- package/src/client/components/GatewayTab.tsx +469 -469
- package/src/client/components/NoticeBanner.tsx +46 -46
- package/src/client/components/ProgressBar.tsx +53 -53
- package/src/client/components/ProviderCard.tsx +606 -606
- package/src/client/components/SettingsTabs.tsx +75 -75
- package/src/client/components/ToggleSwitch.tsx +71 -71
- package/src/client/constants.ts +230 -224
- package/src/client/display.ts +61 -61
- package/src/client/dshClientAdapter.ts +127 -127
- package/src/client/gatewaySnippets.ts +37 -37
- package/src/client/index.tsx +156 -156
- package/src/client/locales.ts +540 -535
- package/src/client/microStyles.ts +52 -52
- package/src/client/parsers.ts +398 -396
- package/src/client/styles.ts +325 -325
- package/src/client/types.ts +199 -197
- package/src/codex-http.ts +447 -447
- package/src/codex-images.ts +503 -485
- package/src/codex-model-capabilities.ts +320 -320
- package/src/codex-search.ts +245 -245
- package/src/codex-usage.ts +263 -263
- package/src/compatibility.ts +41 -41
- package/src/dsh-host-adapter.ts +173 -173
- package/src/gateway-anthropic-messages.ts +84 -84
- package/src/gateway-auth.ts +102 -102
- package/src/gateway-backend.ts +274 -274
- package/src/gateway-body.ts +49 -49
- package/src/gateway-config.ts +76 -76
- package/src/gateway-http.ts +104 -104
- package/src/gateway-openai-chat.ts +124 -124
- package/src/gateway-openai-responses.ts +53 -53
- package/src/gateway-parse.ts +224 -224
- package/src/gateway-protocol.ts +52 -52
- package/src/gateway-routes.ts +158 -158
- package/src/gateway.ts +258 -258
- package/src/grok-errors.ts +24 -24
- package/src/grok-imagine.ts +1627 -1627
- package/src/grok-import.ts +151 -151
- package/src/http-json.ts +82 -82
- package/src/ids.ts +59 -59
- package/src/imagine-routes.ts +463 -463
- package/src/index.ts +709 -709
- package/src/invariant.ts +17 -17
- package/src/kimi-errors.ts +26 -26
- package/src/media-store.ts +927 -927
- package/src/oauth-import-routes.ts +324 -324
- package/src/oauth-providers.ts +152 -152
- package/src/oauth-session.ts +183 -183
- package/src/oauth-sources.ts +1104 -1104
- package/src/oauth.ts +620 -620
- package/src/provider.ts +128 -128
- package/src/proxy.ts +11 -11
- package/src/redact.ts +72 -72
- package/src/session.ts +218 -218
- package/src/store.ts +217 -217
- package/src/web-origin.ts +296 -296
- package/src/web-routes.ts +38 -38
|
@@ -1,53 +1,53 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OpenAI Responses API subset for the local gateway.
|
|
3
|
-
* @module dsh-coding-subscription-oauth/gateway-openai-responses
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
7
|
-
import { type GatewayBackend, gatewayErrorEnvelope } from "./gateway-backend.ts";
|
|
8
|
-
import { beginGatewaySse, readGatewayJsonBody, writeGatewayJson } from "./gateway-body.ts";
|
|
9
|
-
import { parseOpenAiResponsesRequest } from "./gateway-parse.ts";
|
|
10
|
-
|
|
11
|
-
export async function handleOpenAiResponses(
|
|
12
|
-
req: IncomingMessage,
|
|
13
|
-
res: ServerResponse,
|
|
14
|
-
backend: GatewayBackend,
|
|
15
|
-
): Promise<void> {
|
|
16
|
-
const payload = await readGatewayJsonBody(req);
|
|
17
|
-
const request = parseOpenAiResponsesRequest(payload);
|
|
18
|
-
const stream = payload["stream"] !== false;
|
|
19
|
-
const id = `resp_gateway_${Date.now().toString(36)}`;
|
|
20
|
-
if (!stream) {
|
|
21
|
-
let text = "";
|
|
22
|
-
for await (const part of backend.stream(request)) {
|
|
23
|
-
if (part.type === "text") text += part.text;
|
|
24
|
-
}
|
|
25
|
-
writeGatewayJson(res, 200, {
|
|
26
|
-
id,
|
|
27
|
-
object: "response",
|
|
28
|
-
model: request.model,
|
|
29
|
-
status: "completed",
|
|
30
|
-
output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text }] }],
|
|
31
|
-
});
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
beginGatewaySse(res);
|
|
35
|
-
try {
|
|
36
|
-
res.write(
|
|
37
|
-
`event: response.created\ndata: ${JSON.stringify({ id, object: "response", model: request.model, status: "in_progress" })}\n\n`,
|
|
38
|
-
);
|
|
39
|
-
for await (const part of backend.stream(request)) {
|
|
40
|
-
if (part.type === "text") {
|
|
41
|
-
res.write(`event: response.output_text.delta\ndata: ${JSON.stringify({ id, delta: part.text })}\n\n`);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
res.write(
|
|
45
|
-
`event: response.completed\ndata: ${JSON.stringify({ id, object: "response", status: "completed" })}\n\n`,
|
|
46
|
-
);
|
|
47
|
-
res.write("data: [DONE]\n\n");
|
|
48
|
-
res.end();
|
|
49
|
-
} catch (error) {
|
|
50
|
-
res.write(`event: error\ndata: ${JSON.stringify(gatewayErrorEnvelope(error).body)}\n\n`);
|
|
51
|
-
res.end();
|
|
52
|
-
}
|
|
53
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI Responses API subset for the local gateway.
|
|
3
|
+
* @module dsh-coding-subscription-oauth/gateway-openai-responses
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
7
|
+
import { type GatewayBackend, gatewayErrorEnvelope } from "./gateway-backend.ts";
|
|
8
|
+
import { beginGatewaySse, readGatewayJsonBody, writeGatewayJson } from "./gateway-body.ts";
|
|
9
|
+
import { parseOpenAiResponsesRequest } from "./gateway-parse.ts";
|
|
10
|
+
|
|
11
|
+
export async function handleOpenAiResponses(
|
|
12
|
+
req: IncomingMessage,
|
|
13
|
+
res: ServerResponse,
|
|
14
|
+
backend: GatewayBackend,
|
|
15
|
+
): Promise<void> {
|
|
16
|
+
const payload = await readGatewayJsonBody(req);
|
|
17
|
+
const request = parseOpenAiResponsesRequest(payload);
|
|
18
|
+
const stream = payload["stream"] !== false;
|
|
19
|
+
const id = `resp_gateway_${Date.now().toString(36)}`;
|
|
20
|
+
if (!stream) {
|
|
21
|
+
let text = "";
|
|
22
|
+
for await (const part of backend.stream(request)) {
|
|
23
|
+
if (part.type === "text") text += part.text;
|
|
24
|
+
}
|
|
25
|
+
writeGatewayJson(res, 200, {
|
|
26
|
+
id,
|
|
27
|
+
object: "response",
|
|
28
|
+
model: request.model,
|
|
29
|
+
status: "completed",
|
|
30
|
+
output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text }] }],
|
|
31
|
+
});
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
beginGatewaySse(res);
|
|
35
|
+
try {
|
|
36
|
+
res.write(
|
|
37
|
+
`event: response.created\ndata: ${JSON.stringify({ id, object: "response", model: request.model, status: "in_progress" })}\n\n`,
|
|
38
|
+
);
|
|
39
|
+
for await (const part of backend.stream(request)) {
|
|
40
|
+
if (part.type === "text") {
|
|
41
|
+
res.write(`event: response.output_text.delta\ndata: ${JSON.stringify({ id, delta: part.text })}\n\n`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
res.write(
|
|
45
|
+
`event: response.completed\ndata: ${JSON.stringify({ id, object: "response", status: "completed" })}\n\n`,
|
|
46
|
+
);
|
|
47
|
+
res.write("data: [DONE]\n\n");
|
|
48
|
+
res.end();
|
|
49
|
+
} catch (error) {
|
|
50
|
+
res.write(`event: error\ndata: ${JSON.stringify(gatewayErrorEnvelope(error).body)}\n\n`);
|
|
51
|
+
res.end();
|
|
52
|
+
}
|
|
53
|
+
}
|
package/src/gateway-parse.ts
CHANGED
|
@@ -1,224 +1,224 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Parse OpenAI / Anthropic request bodies into the gateway completion shape.
|
|
3
|
-
* @module dsh-coding-subscription-oauth/gateway-parse
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { GatewayRequestError } from "./gateway-backend.ts";
|
|
7
|
-
import type { GatewayChatMessage, GatewayCompletionRequest, GatewayTool, GatewayToolCall } from "./gateway-protocol.ts";
|
|
8
|
-
|
|
9
|
-
export function parseOpenAiChatRequest(payload: Record<string, unknown>): GatewayCompletionRequest {
|
|
10
|
-
const model = requiredModel(payload["model"]);
|
|
11
|
-
const tools = parseOpenAiTools(payload["tools"]);
|
|
12
|
-
const reasoning = reasoningOf(payload);
|
|
13
|
-
return {
|
|
14
|
-
model,
|
|
15
|
-
messages: parseOpenAiMessages(payload["messages"]),
|
|
16
|
-
...(tools === undefined ? {} : { tools }),
|
|
17
|
-
...(reasoning === undefined ? {} : { reasoning }),
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function parseOpenAiResponsesRequest(payload: Record<string, unknown>): GatewayCompletionRequest {
|
|
22
|
-
const model = requiredModel(payload["model"]);
|
|
23
|
-
const input = payload["input"];
|
|
24
|
-
if (typeof input === "string") {
|
|
25
|
-
return { model, messages: [{ role: "user", content: input }] };
|
|
26
|
-
}
|
|
27
|
-
if (Array.isArray(input)) {
|
|
28
|
-
const tools = parseOpenAiTools(payload["tools"]);
|
|
29
|
-
const reasoning = reasoningOf(payload);
|
|
30
|
-
return {
|
|
31
|
-
model,
|
|
32
|
-
messages: parseOpenAiMessages(input),
|
|
33
|
-
...(tools === undefined ? {} : { tools }),
|
|
34
|
-
...(reasoning === undefined ? {} : { reasoning }),
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
throw new GatewayRequestError(400, "invalid_request", "input must be a string or message array");
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function parseAnthropicMessagesRequest(payload: Record<string, unknown>): GatewayCompletionRequest {
|
|
41
|
-
const model = requiredModel(payload["model"]);
|
|
42
|
-
const messages = parseAnthropicMessages(payload["messages"]);
|
|
43
|
-
const system = payload["system"];
|
|
44
|
-
if (typeof system === "string" && system.length > 0) {
|
|
45
|
-
messages.unshift({ role: "system", content: system });
|
|
46
|
-
}
|
|
47
|
-
const tools = parseAnthropicTools(payload["tools"]);
|
|
48
|
-
const reasoning = reasoningOf(payload);
|
|
49
|
-
return {
|
|
50
|
-
model,
|
|
51
|
-
messages,
|
|
52
|
-
...(tools === undefined ? {} : { tools }),
|
|
53
|
-
...(reasoning === undefined ? {} : { reasoning }),
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function anthropicMaxTokens(payload: Record<string, unknown>): number {
|
|
58
|
-
const value = payload["max_tokens"];
|
|
59
|
-
if (typeof value === "number" && Number.isSafeInteger(value) && value > 0) return Math.min(value, 32_768);
|
|
60
|
-
return 4096;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function requiredModel(value: unknown): string {
|
|
64
|
-
if (typeof value !== "string" || value.length === 0) {
|
|
65
|
-
throw new GatewayRequestError(400, "invalid_request", "model is required");
|
|
66
|
-
}
|
|
67
|
-
return value;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function reasoningOf(payload: Record<string, unknown>): string | undefined {
|
|
71
|
-
if (typeof payload["reasoning_effort"] === "string") return payload["reasoning_effort"];
|
|
72
|
-
if (typeof payload["reasoning"] === "string") return payload["reasoning"];
|
|
73
|
-
return undefined;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function parseOpenAiMessages(value: unknown): GatewayChatMessage[] {
|
|
77
|
-
if (!Array.isArray(value) || value.length === 0) {
|
|
78
|
-
throw new GatewayRequestError(400, "invalid_request", "messages must be a non-empty array");
|
|
79
|
-
}
|
|
80
|
-
return value.map((item) => {
|
|
81
|
-
if (typeof item !== "object" || item === null) {
|
|
82
|
-
throw new GatewayRequestError(400, "invalid_request", "message must be an object");
|
|
83
|
-
}
|
|
84
|
-
const record = item as Record<string, unknown>;
|
|
85
|
-
const role = record["role"];
|
|
86
|
-
if (typeof role !== "string")
|
|
87
|
-
throw new GatewayRequestError(400, "invalid_request", "message role must be a string");
|
|
88
|
-
const toolCalls = parseToolCalls(record["tool_calls"]);
|
|
89
|
-
return {
|
|
90
|
-
role,
|
|
91
|
-
content: contentToString(record["content"]),
|
|
92
|
-
...(toolCalls === undefined ? {} : { tool_calls: toolCalls }),
|
|
93
|
-
...(typeof record["reasoning_content"] === "string" ? { reasoning_content: record["reasoning_content"] } : {}),
|
|
94
|
-
...(typeof record["tool_call_id"] === "string" ? { tool_call_id: record["tool_call_id"] } : {}),
|
|
95
|
-
...(typeof record["name"] === "string" ? { tool_name: record["name"] } : {}),
|
|
96
|
-
};
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function parseAnthropicMessages(value: unknown): GatewayChatMessage[] {
|
|
101
|
-
if (!Array.isArray(value) || value.length === 0) {
|
|
102
|
-
throw new GatewayRequestError(400, "invalid_request", "messages must be a non-empty array");
|
|
103
|
-
}
|
|
104
|
-
const mapped: GatewayChatMessage[] = [];
|
|
105
|
-
for (const item of value) {
|
|
106
|
-
if (typeof item !== "object" || item === null) {
|
|
107
|
-
throw new GatewayRequestError(400, "invalid_request", "message must be an object");
|
|
108
|
-
}
|
|
109
|
-
const record = item as Record<string, unknown>;
|
|
110
|
-
const role = record["role"];
|
|
111
|
-
if (role !== "user" && role !== "assistant") {
|
|
112
|
-
throw new GatewayRequestError(400, "invalid_request", "anthropic message role must be user or assistant");
|
|
113
|
-
}
|
|
114
|
-
const blocks = Array.isArray(record["content"]) ? record["content"] : [{ type: "text", text: record["content"] }];
|
|
115
|
-
const texts: string[] = [];
|
|
116
|
-
const toolCalls: GatewayToolCall[] = [];
|
|
117
|
-
let reasoning: string | undefined;
|
|
118
|
-
for (const block of blocks) {
|
|
119
|
-
if (typeof block !== "object" || block === null) continue;
|
|
120
|
-
const entry = block as Record<string, unknown>;
|
|
121
|
-
if (entry["type"] === "text" && typeof entry["text"] === "string") texts.push(entry["text"]);
|
|
122
|
-
if (entry["type"] === "thinking" && typeof entry["thinking"] === "string") reasoning = entry["thinking"];
|
|
123
|
-
if (entry["type"] === "tool_use" && typeof entry["id"] === "string" && typeof entry["name"] === "string") {
|
|
124
|
-
toolCalls.push({
|
|
125
|
-
id: entry["id"],
|
|
126
|
-
name: entry["name"],
|
|
127
|
-
arguments: typeof entry["input"] === "string" ? entry["input"] : JSON.stringify(entry["input"] ?? {}),
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
if (entry["type"] === "tool_result" && typeof entry["tool_use_id"] === "string") {
|
|
131
|
-
mapped.push({
|
|
132
|
-
role: "tool",
|
|
133
|
-
content: typeof entry["content"] === "string" ? entry["content"] : JSON.stringify(entry["content"] ?? ""),
|
|
134
|
-
tool_call_id: entry["tool_use_id"],
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
mapped.push({
|
|
139
|
-
role,
|
|
140
|
-
content: texts.join(""),
|
|
141
|
-
...(toolCalls.length === 0 ? {} : { tool_calls: toolCalls }),
|
|
142
|
-
...(reasoning === undefined ? {} : { reasoning_content: reasoning }),
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
return mapped;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
function parseOpenAiTools(value: unknown): GatewayTool[] | undefined {
|
|
149
|
-
if (!Array.isArray(value) || value.length === 0) return undefined;
|
|
150
|
-
const tools: GatewayTool[] = [];
|
|
151
|
-
for (const item of value) {
|
|
152
|
-
if (typeof item !== "object" || item === null) continue;
|
|
153
|
-
const record = item as Record<string, unknown>;
|
|
154
|
-
const fn =
|
|
155
|
-
typeof record["function"] === "object" && record["function"] !== null
|
|
156
|
-
? (record["function"] as Record<string, unknown>)
|
|
157
|
-
: record;
|
|
158
|
-
if (typeof fn["name"] !== "string") continue;
|
|
159
|
-
tools.push({
|
|
160
|
-
name: fn["name"],
|
|
161
|
-
description: typeof fn["description"] === "string" ? fn["description"] : "",
|
|
162
|
-
parameters:
|
|
163
|
-
typeof fn["parameters"] === "object" && fn["parameters"] !== null
|
|
164
|
-
? (fn["parameters"] as Record<string, unknown>)
|
|
165
|
-
: {},
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
return tools.length === 0 ? undefined : tools;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
function parseAnthropicTools(value: unknown): GatewayTool[] | undefined {
|
|
172
|
-
if (!Array.isArray(value) || value.length === 0) return undefined;
|
|
173
|
-
const tools: GatewayTool[] = [];
|
|
174
|
-
for (const item of value) {
|
|
175
|
-
if (typeof item !== "object" || item === null) continue;
|
|
176
|
-
const record = item as Record<string, unknown>;
|
|
177
|
-
if (typeof record["name"] !== "string") continue;
|
|
178
|
-
tools.push({
|
|
179
|
-
name: record["name"],
|
|
180
|
-
description: typeof record["description"] === "string" ? record["description"] : "",
|
|
181
|
-
parameters:
|
|
182
|
-
typeof record["input_schema"] === "object" && record["input_schema"] !== null
|
|
183
|
-
? (record["input_schema"] as Record<string, unknown>)
|
|
184
|
-
: {},
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
return tools.length === 0 ? undefined : tools;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
function parseToolCalls(value: unknown): GatewayToolCall[] | undefined {
|
|
191
|
-
if (!Array.isArray(value) || value.length === 0) return undefined;
|
|
192
|
-
const calls: GatewayToolCall[] = [];
|
|
193
|
-
for (const item of value) {
|
|
194
|
-
if (typeof item !== "object" || item === null) continue;
|
|
195
|
-
const record = item as Record<string, unknown>;
|
|
196
|
-
const fn =
|
|
197
|
-
typeof record["function"] === "object" && record["function"] !== null
|
|
198
|
-
? (record["function"] as Record<string, unknown>)
|
|
199
|
-
: record;
|
|
200
|
-
if (typeof record["id"] !== "string" || typeof fn["name"] !== "string") continue;
|
|
201
|
-
calls.push({
|
|
202
|
-
id: record["id"],
|
|
203
|
-
name: fn["name"],
|
|
204
|
-
arguments: typeof fn["arguments"] === "string" ? fn["arguments"] : JSON.stringify(fn["arguments"] ?? {}),
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
return calls.length === 0 ? undefined : calls;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
function contentToString(value: unknown): string {
|
|
211
|
-
if (typeof value === "string") return value;
|
|
212
|
-
if (Array.isArray(value)) {
|
|
213
|
-
return value
|
|
214
|
-
.map((part) => {
|
|
215
|
-
if (typeof part === "string") return part;
|
|
216
|
-
if (typeof part === "object" && part !== null && typeof (part as { text?: unknown }).text === "string") {
|
|
217
|
-
return (part as { text: string }).text;
|
|
218
|
-
}
|
|
219
|
-
return "";
|
|
220
|
-
})
|
|
221
|
-
.join("");
|
|
222
|
-
}
|
|
223
|
-
return "";
|
|
224
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Parse OpenAI / Anthropic request bodies into the gateway completion shape.
|
|
3
|
+
* @module dsh-coding-subscription-oauth/gateway-parse
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { GatewayRequestError } from "./gateway-backend.ts";
|
|
7
|
+
import type { GatewayChatMessage, GatewayCompletionRequest, GatewayTool, GatewayToolCall } from "./gateway-protocol.ts";
|
|
8
|
+
|
|
9
|
+
export function parseOpenAiChatRequest(payload: Record<string, unknown>): GatewayCompletionRequest {
|
|
10
|
+
const model = requiredModel(payload["model"]);
|
|
11
|
+
const tools = parseOpenAiTools(payload["tools"]);
|
|
12
|
+
const reasoning = reasoningOf(payload);
|
|
13
|
+
return {
|
|
14
|
+
model,
|
|
15
|
+
messages: parseOpenAiMessages(payload["messages"]),
|
|
16
|
+
...(tools === undefined ? {} : { tools }),
|
|
17
|
+
...(reasoning === undefined ? {} : { reasoning }),
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function parseOpenAiResponsesRequest(payload: Record<string, unknown>): GatewayCompletionRequest {
|
|
22
|
+
const model = requiredModel(payload["model"]);
|
|
23
|
+
const input = payload["input"];
|
|
24
|
+
if (typeof input === "string") {
|
|
25
|
+
return { model, messages: [{ role: "user", content: input }] };
|
|
26
|
+
}
|
|
27
|
+
if (Array.isArray(input)) {
|
|
28
|
+
const tools = parseOpenAiTools(payload["tools"]);
|
|
29
|
+
const reasoning = reasoningOf(payload);
|
|
30
|
+
return {
|
|
31
|
+
model,
|
|
32
|
+
messages: parseOpenAiMessages(input),
|
|
33
|
+
...(tools === undefined ? {} : { tools }),
|
|
34
|
+
...(reasoning === undefined ? {} : { reasoning }),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
throw new GatewayRequestError(400, "invalid_request", "input must be a string or message array");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function parseAnthropicMessagesRequest(payload: Record<string, unknown>): GatewayCompletionRequest {
|
|
41
|
+
const model = requiredModel(payload["model"]);
|
|
42
|
+
const messages = parseAnthropicMessages(payload["messages"]);
|
|
43
|
+
const system = payload["system"];
|
|
44
|
+
if (typeof system === "string" && system.length > 0) {
|
|
45
|
+
messages.unshift({ role: "system", content: system });
|
|
46
|
+
}
|
|
47
|
+
const tools = parseAnthropicTools(payload["tools"]);
|
|
48
|
+
const reasoning = reasoningOf(payload);
|
|
49
|
+
return {
|
|
50
|
+
model,
|
|
51
|
+
messages,
|
|
52
|
+
...(tools === undefined ? {} : { tools }),
|
|
53
|
+
...(reasoning === undefined ? {} : { reasoning }),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function anthropicMaxTokens(payload: Record<string, unknown>): number {
|
|
58
|
+
const value = payload["max_tokens"];
|
|
59
|
+
if (typeof value === "number" && Number.isSafeInteger(value) && value > 0) return Math.min(value, 32_768);
|
|
60
|
+
return 4096;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function requiredModel(value: unknown): string {
|
|
64
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
65
|
+
throw new GatewayRequestError(400, "invalid_request", "model is required");
|
|
66
|
+
}
|
|
67
|
+
return value;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function reasoningOf(payload: Record<string, unknown>): string | undefined {
|
|
71
|
+
if (typeof payload["reasoning_effort"] === "string") return payload["reasoning_effort"];
|
|
72
|
+
if (typeof payload["reasoning"] === "string") return payload["reasoning"];
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function parseOpenAiMessages(value: unknown): GatewayChatMessage[] {
|
|
77
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
78
|
+
throw new GatewayRequestError(400, "invalid_request", "messages must be a non-empty array");
|
|
79
|
+
}
|
|
80
|
+
return value.map((item) => {
|
|
81
|
+
if (typeof item !== "object" || item === null) {
|
|
82
|
+
throw new GatewayRequestError(400, "invalid_request", "message must be an object");
|
|
83
|
+
}
|
|
84
|
+
const record = item as Record<string, unknown>;
|
|
85
|
+
const role = record["role"];
|
|
86
|
+
if (typeof role !== "string")
|
|
87
|
+
throw new GatewayRequestError(400, "invalid_request", "message role must be a string");
|
|
88
|
+
const toolCalls = parseToolCalls(record["tool_calls"]);
|
|
89
|
+
return {
|
|
90
|
+
role,
|
|
91
|
+
content: contentToString(record["content"]),
|
|
92
|
+
...(toolCalls === undefined ? {} : { tool_calls: toolCalls }),
|
|
93
|
+
...(typeof record["reasoning_content"] === "string" ? { reasoning_content: record["reasoning_content"] } : {}),
|
|
94
|
+
...(typeof record["tool_call_id"] === "string" ? { tool_call_id: record["tool_call_id"] } : {}),
|
|
95
|
+
...(typeof record["name"] === "string" ? { tool_name: record["name"] } : {}),
|
|
96
|
+
};
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function parseAnthropicMessages(value: unknown): GatewayChatMessage[] {
|
|
101
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
102
|
+
throw new GatewayRequestError(400, "invalid_request", "messages must be a non-empty array");
|
|
103
|
+
}
|
|
104
|
+
const mapped: GatewayChatMessage[] = [];
|
|
105
|
+
for (const item of value) {
|
|
106
|
+
if (typeof item !== "object" || item === null) {
|
|
107
|
+
throw new GatewayRequestError(400, "invalid_request", "message must be an object");
|
|
108
|
+
}
|
|
109
|
+
const record = item as Record<string, unknown>;
|
|
110
|
+
const role = record["role"];
|
|
111
|
+
if (role !== "user" && role !== "assistant") {
|
|
112
|
+
throw new GatewayRequestError(400, "invalid_request", "anthropic message role must be user or assistant");
|
|
113
|
+
}
|
|
114
|
+
const blocks = Array.isArray(record["content"]) ? record["content"] : [{ type: "text", text: record["content"] }];
|
|
115
|
+
const texts: string[] = [];
|
|
116
|
+
const toolCalls: GatewayToolCall[] = [];
|
|
117
|
+
let reasoning: string | undefined;
|
|
118
|
+
for (const block of blocks) {
|
|
119
|
+
if (typeof block !== "object" || block === null) continue;
|
|
120
|
+
const entry = block as Record<string, unknown>;
|
|
121
|
+
if (entry["type"] === "text" && typeof entry["text"] === "string") texts.push(entry["text"]);
|
|
122
|
+
if (entry["type"] === "thinking" && typeof entry["thinking"] === "string") reasoning = entry["thinking"];
|
|
123
|
+
if (entry["type"] === "tool_use" && typeof entry["id"] === "string" && typeof entry["name"] === "string") {
|
|
124
|
+
toolCalls.push({
|
|
125
|
+
id: entry["id"],
|
|
126
|
+
name: entry["name"],
|
|
127
|
+
arguments: typeof entry["input"] === "string" ? entry["input"] : JSON.stringify(entry["input"] ?? {}),
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
if (entry["type"] === "tool_result" && typeof entry["tool_use_id"] === "string") {
|
|
131
|
+
mapped.push({
|
|
132
|
+
role: "tool",
|
|
133
|
+
content: typeof entry["content"] === "string" ? entry["content"] : JSON.stringify(entry["content"] ?? ""),
|
|
134
|
+
tool_call_id: entry["tool_use_id"],
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
mapped.push({
|
|
139
|
+
role,
|
|
140
|
+
content: texts.join(""),
|
|
141
|
+
...(toolCalls.length === 0 ? {} : { tool_calls: toolCalls }),
|
|
142
|
+
...(reasoning === undefined ? {} : { reasoning_content: reasoning }),
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
return mapped;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function parseOpenAiTools(value: unknown): GatewayTool[] | undefined {
|
|
149
|
+
if (!Array.isArray(value) || value.length === 0) return undefined;
|
|
150
|
+
const tools: GatewayTool[] = [];
|
|
151
|
+
for (const item of value) {
|
|
152
|
+
if (typeof item !== "object" || item === null) continue;
|
|
153
|
+
const record = item as Record<string, unknown>;
|
|
154
|
+
const fn =
|
|
155
|
+
typeof record["function"] === "object" && record["function"] !== null
|
|
156
|
+
? (record["function"] as Record<string, unknown>)
|
|
157
|
+
: record;
|
|
158
|
+
if (typeof fn["name"] !== "string") continue;
|
|
159
|
+
tools.push({
|
|
160
|
+
name: fn["name"],
|
|
161
|
+
description: typeof fn["description"] === "string" ? fn["description"] : "",
|
|
162
|
+
parameters:
|
|
163
|
+
typeof fn["parameters"] === "object" && fn["parameters"] !== null
|
|
164
|
+
? (fn["parameters"] as Record<string, unknown>)
|
|
165
|
+
: {},
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
return tools.length === 0 ? undefined : tools;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function parseAnthropicTools(value: unknown): GatewayTool[] | undefined {
|
|
172
|
+
if (!Array.isArray(value) || value.length === 0) return undefined;
|
|
173
|
+
const tools: GatewayTool[] = [];
|
|
174
|
+
for (const item of value) {
|
|
175
|
+
if (typeof item !== "object" || item === null) continue;
|
|
176
|
+
const record = item as Record<string, unknown>;
|
|
177
|
+
if (typeof record["name"] !== "string") continue;
|
|
178
|
+
tools.push({
|
|
179
|
+
name: record["name"],
|
|
180
|
+
description: typeof record["description"] === "string" ? record["description"] : "",
|
|
181
|
+
parameters:
|
|
182
|
+
typeof record["input_schema"] === "object" && record["input_schema"] !== null
|
|
183
|
+
? (record["input_schema"] as Record<string, unknown>)
|
|
184
|
+
: {},
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
return tools.length === 0 ? undefined : tools;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function parseToolCalls(value: unknown): GatewayToolCall[] | undefined {
|
|
191
|
+
if (!Array.isArray(value) || value.length === 0) return undefined;
|
|
192
|
+
const calls: GatewayToolCall[] = [];
|
|
193
|
+
for (const item of value) {
|
|
194
|
+
if (typeof item !== "object" || item === null) continue;
|
|
195
|
+
const record = item as Record<string, unknown>;
|
|
196
|
+
const fn =
|
|
197
|
+
typeof record["function"] === "object" && record["function"] !== null
|
|
198
|
+
? (record["function"] as Record<string, unknown>)
|
|
199
|
+
: record;
|
|
200
|
+
if (typeof record["id"] !== "string" || typeof fn["name"] !== "string") continue;
|
|
201
|
+
calls.push({
|
|
202
|
+
id: record["id"],
|
|
203
|
+
name: fn["name"],
|
|
204
|
+
arguments: typeof fn["arguments"] === "string" ? fn["arguments"] : JSON.stringify(fn["arguments"] ?? {}),
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
return calls.length === 0 ? undefined : calls;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function contentToString(value: unknown): string {
|
|
211
|
+
if (typeof value === "string") return value;
|
|
212
|
+
if (Array.isArray(value)) {
|
|
213
|
+
return value
|
|
214
|
+
.map((part) => {
|
|
215
|
+
if (typeof part === "string") return part;
|
|
216
|
+
if (typeof part === "object" && part !== null && typeof (part as { text?: unknown }).text === "string") {
|
|
217
|
+
return (part as { text: string }).text;
|
|
218
|
+
}
|
|
219
|
+
return "";
|
|
220
|
+
})
|
|
221
|
+
.join("");
|
|
222
|
+
}
|
|
223
|
+
return "";
|
|
224
|
+
}
|
package/src/gateway-protocol.ts
CHANGED
|
@@ -1,52 +1,52 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shared request/stream types for the local coding-subscription gateway.
|
|
3
|
-
* @module dsh-coding-subscription-oauth/gateway-protocol
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
export interface GatewayToolCall {
|
|
7
|
-
id: string;
|
|
8
|
-
name: string;
|
|
9
|
-
arguments: string;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface GatewayTool {
|
|
13
|
-
name: string;
|
|
14
|
-
description: string;
|
|
15
|
-
parameters: Record<string, unknown>;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface GatewayChatMessage {
|
|
19
|
-
role: string;
|
|
20
|
-
content: string;
|
|
21
|
-
tool_calls?: readonly GatewayToolCall[];
|
|
22
|
-
reasoning_content?: string;
|
|
23
|
-
tool_call_id?: string;
|
|
24
|
-
tool_name?: string;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface GatewayCompletionRequest {
|
|
28
|
-
model: string;
|
|
29
|
-
messages: readonly GatewayChatMessage[];
|
|
30
|
-
tools?: readonly GatewayTool[];
|
|
31
|
-
reasoning?: string;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export type GatewayStreamPart =
|
|
35
|
-
| { type: "text"; text: string }
|
|
36
|
-
| { type: "thinking"; text: string }
|
|
37
|
-
| { type: "tool_call"; index: number; id: string; name: string; arguments: string }
|
|
38
|
-
| { type: "done"; finish: "stop" | "tool_calls" | "length" };
|
|
39
|
-
|
|
40
|
-
export type GatewayThinkingLevel = "off" | "minimal" | "low" | "medium" | "high" | "xhigh" | "max";
|
|
41
|
-
|
|
42
|
-
export function isThinkingLevel(value: string): value is GatewayThinkingLevel {
|
|
43
|
-
return (
|
|
44
|
-
value === "off" ||
|
|
45
|
-
value === "minimal" ||
|
|
46
|
-
value === "low" ||
|
|
47
|
-
value === "medium" ||
|
|
48
|
-
value === "high" ||
|
|
49
|
-
value === "xhigh" ||
|
|
50
|
-
value === "max"
|
|
51
|
-
);
|
|
52
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Shared request/stream types for the local coding-subscription gateway.
|
|
3
|
+
* @module dsh-coding-subscription-oauth/gateway-protocol
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export interface GatewayToolCall {
|
|
7
|
+
id: string;
|
|
8
|
+
name: string;
|
|
9
|
+
arguments: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface GatewayTool {
|
|
13
|
+
name: string;
|
|
14
|
+
description: string;
|
|
15
|
+
parameters: Record<string, unknown>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface GatewayChatMessage {
|
|
19
|
+
role: string;
|
|
20
|
+
content: string;
|
|
21
|
+
tool_calls?: readonly GatewayToolCall[];
|
|
22
|
+
reasoning_content?: string;
|
|
23
|
+
tool_call_id?: string;
|
|
24
|
+
tool_name?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface GatewayCompletionRequest {
|
|
28
|
+
model: string;
|
|
29
|
+
messages: readonly GatewayChatMessage[];
|
|
30
|
+
tools?: readonly GatewayTool[];
|
|
31
|
+
reasoning?: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type GatewayStreamPart =
|
|
35
|
+
| { type: "text"; text: string }
|
|
36
|
+
| { type: "thinking"; text: string }
|
|
37
|
+
| { type: "tool_call"; index: number; id: string; name: string; arguments: string }
|
|
38
|
+
| { type: "done"; finish: "stop" | "tool_calls" | "length" };
|
|
39
|
+
|
|
40
|
+
export type GatewayThinkingLevel = "off" | "minimal" | "low" | "medium" | "high" | "xhigh" | "max";
|
|
41
|
+
|
|
42
|
+
export function isThinkingLevel(value: string): value is GatewayThinkingLevel {
|
|
43
|
+
return (
|
|
44
|
+
value === "off" ||
|
|
45
|
+
value === "minimal" ||
|
|
46
|
+
value === "low" ||
|
|
47
|
+
value === "medium" ||
|
|
48
|
+
value === "high" ||
|
|
49
|
+
value === "xhigh" ||
|
|
50
|
+
value === "max"
|
|
51
|
+
);
|
|
52
|
+
}
|