@jonsoc/console-app 1.1.34
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/.opencode/agent/css.md +149 -0
- package/README.md +32 -0
- package/package.json +49 -0
- package/public/apple-touch-icon-v3.png +1 -0
- package/public/apple-touch-icon.png +1 -0
- package/public/email +1 -0
- package/public/favicon-96x96-v3.png +1 -0
- package/public/favicon-96x96.png +1 -0
- package/public/favicon-v3.ico +1 -0
- package/public/favicon-v3.svg +1 -0
- package/public/favicon.ico +1 -0
- package/public/favicon.svg +1 -0
- package/public/opencode-brand-assets.zip +0 -0
- package/public/robots.txt +6 -0
- package/public/site.webmanifest +1 -0
- package/public/social-share-black.png +1 -0
- package/public/social-share-zen.png +1 -0
- package/public/social-share.png +1 -0
- package/public/theme.json +182 -0
- package/public/web-app-manifest-192x192.png +1 -0
- package/public/web-app-manifest-512x512.png +1 -0
- package/script/generate-sitemap.ts +103 -0
- package/src/app.css +1 -0
- package/src/app.tsx +27 -0
- package/src/asset/black/hero.png +0 -0
- package/src/asset/brand/opencode-brand-assets.zip +0 -0
- package/src/asset/brand/opencode-logo-dark.png +0 -0
- package/src/asset/brand/opencode-logo-dark.svg +16 -0
- package/src/asset/brand/opencode-logo-light.png +0 -0
- package/src/asset/brand/opencode-logo-light.svg +16 -0
- package/src/asset/brand/opencode-wordmark-dark.png +0 -0
- package/src/asset/brand/opencode-wordmark-dark.svg +30 -0
- package/src/asset/brand/opencode-wordmark-light.png +0 -0
- package/src/asset/brand/opencode-wordmark-light.svg +30 -0
- package/src/asset/brand/opencode-wordmark-simple-dark.png +0 -0
- package/src/asset/brand/opencode-wordmark-simple-dark.svg +22 -0
- package/src/asset/brand/opencode-wordmark-simple-light.png +0 -0
- package/src/asset/brand/opencode-wordmark-simple-light.svg +22 -0
- package/src/asset/brand/preview-opencode-dark.png +0 -0
- package/src/asset/brand/preview-opencode-logo-dark.png +0 -0
- package/src/asset/brand/preview-opencode-logo-light.png +0 -0
- package/src/asset/brand/preview-opencode-wordmark-dark.png +0 -0
- package/src/asset/brand/preview-opencode-wordmark-light.png +0 -0
- package/src/asset/brand/preview-opencode-wordmark-simple-dark.png +0 -0
- package/src/asset/brand/preview-opencode-wordmark-simple-light.png +0 -0
- package/src/asset/lander/avatar-adam.png +0 -0
- package/src/asset/lander/avatar-david.png +0 -0
- package/src/asset/lander/avatar-dax.png +0 -0
- package/src/asset/lander/avatar-frank.png +0 -0
- package/src/asset/lander/avatar-jay.png +0 -0
- package/src/asset/lander/brand-assets-dark.svg +10 -0
- package/src/asset/lander/brand-assets-light.svg +10 -0
- package/src/asset/lander/brand.png +0 -0
- package/src/asset/lander/check.svg +3 -0
- package/src/asset/lander/copy.svg +3 -0
- package/src/asset/lander/desktop-app-icon.png +0 -0
- package/src/asset/lander/dock.png +0 -0
- package/src/asset/lander/logo-dark.svg +11 -0
- package/src/asset/lander/logo-light.svg +11 -0
- package/src/asset/lander/opencode-comparison-min.mp4 +0 -0
- package/src/asset/lander/opencode-comparison-poster.png +0 -0
- package/src/asset/lander/opencode-desktop-icon.png +0 -0
- package/src/asset/lander/opencode-logo-dark.svg +11 -0
- package/src/asset/lander/opencode-logo-light.svg +11 -0
- package/src/asset/lander/opencode-min.mp4 +0 -0
- package/src/asset/lander/opencode-poster.png +0 -0
- package/src/asset/lander/opencode-wordmark-dark.svg +25 -0
- package/src/asset/lander/opencode-wordmark-light.svg +25 -0
- package/src/asset/lander/screenshot-github.png +0 -0
- package/src/asset/lander/screenshot-splash.png +0 -0
- package/src/asset/lander/screenshot-vscode.png +0 -0
- package/src/asset/lander/screenshot.png +0 -0
- package/src/asset/lander/wordmark-dark.svg +3 -0
- package/src/asset/lander/wordmark-light.svg +3 -0
- package/src/asset/logo-ornate-dark.svg +18 -0
- package/src/asset/logo-ornate-light.svg +18 -0
- package/src/asset/logo.svg +18 -0
- package/src/asset/zen-ornate-dark.svg +8 -0
- package/src/asset/zen-ornate-light.svg +8 -0
- package/src/component/dropdown.css +80 -0
- package/src/component/dropdown.tsx +79 -0
- package/src/component/email-signup.tsx +48 -0
- package/src/component/faq.tsx +33 -0
- package/src/component/footer.tsx +38 -0
- package/src/component/header-context-menu.css +63 -0
- package/src/component/header.tsx +279 -0
- package/src/component/icon.tsx +257 -0
- package/src/component/legal.tsx +20 -0
- package/src/component/modal.css +66 -0
- package/src/component/modal.tsx +24 -0
- package/src/component/spotlight.css +15 -0
- package/src/component/spotlight.tsx +820 -0
- package/src/config.ts +29 -0
- package/src/context/auth.session.ts +0 -0
- package/src/context/auth.ts +116 -0
- package/src/context/auth.withActor.ts +7 -0
- package/src/entry-client.tsx +4 -0
- package/src/entry-server.tsx +30 -0
- package/src/global.d.ts +5 -0
- package/src/lib/github.ts +38 -0
- package/src/middleware.ts +5 -0
- package/src/routes/[...404].css +130 -0
- package/src/routes/[...404].tsx +38 -0
- package/src/routes/api/enterprise.ts +47 -0
- package/src/routes/auth/[...callback].ts +41 -0
- package/src/routes/auth/authorize.ts +10 -0
- package/src/routes/auth/index.ts +12 -0
- package/src/routes/auth/logout.ts +17 -0
- package/src/routes/auth/status.ts +7 -0
- package/src/routes/bench/[id].tsx +365 -0
- package/src/routes/bench/index.tsx +86 -0
- package/src/routes/bench/submission.ts +29 -0
- package/src/routes/black/common.tsx +62 -0
- package/src/routes/black/index.tsx +108 -0
- package/src/routes/black/subscribe/[plan].tsx +449 -0
- package/src/routes/black/workspace.css +214 -0
- package/src/routes/black/workspace.tsx +229 -0
- package/src/routes/black.css +828 -0
- package/src/routes/black.tsx +285 -0
- package/src/routes/brand/index.css +555 -0
- package/src/routes/brand/index.tsx +252 -0
- package/src/routes/changelog/index.css +477 -0
- package/src/routes/changelog/index.tsx +147 -0
- package/src/routes/debug/index.ts +13 -0
- package/src/routes/desktop-feedback.ts +5 -0
- package/src/routes/discord.ts +5 -0
- package/src/routes/docs/[...path].ts +20 -0
- package/src/routes/docs/index.ts +20 -0
- package/src/routes/download/[platform].ts +38 -0
- package/src/routes/download/index.css +750 -0
- package/src/routes/download/index.tsx +482 -0
- package/src/routes/download/types.ts +4 -0
- package/src/routes/enterprise/index.css +578 -0
- package/src/routes/enterprise/index.tsx +251 -0
- package/src/routes/index.css +1251 -0
- package/src/routes/index.tsx +840 -0
- package/src/routes/legal/privacy-policy/index.css +343 -0
- package/src/routes/legal/privacy-policy/index.tsx +1512 -0
- package/src/routes/legal/terms-of-service/index.css +254 -0
- package/src/routes/legal/terms-of-service/index.tsx +512 -0
- package/src/routes/openapi.json.ts +7 -0
- package/src/routes/s/[id].ts +20 -0
- package/src/routes/stripe/webhook.ts +532 -0
- package/src/routes/t/[...path].tsx +20 -0
- package/src/routes/temp.tsx +172 -0
- package/src/routes/user-menu.css +18 -0
- package/src/routes/user-menu.tsx +32 -0
- package/src/routes/workspace/[id]/billing/billing-section.module.css +185 -0
- package/src/routes/workspace/[id]/billing/billing-section.tsx +240 -0
- package/src/routes/workspace/[id]/billing/black-section.module.css +142 -0
- package/src/routes/workspace/[id]/billing/black-section.tsx +269 -0
- package/src/routes/workspace/[id]/billing/black-waitlist-section.module.css +23 -0
- package/src/routes/workspace/[id]/billing/index.tsx +32 -0
- package/src/routes/workspace/[id]/billing/monthly-limit-section.module.css +96 -0
- package/src/routes/workspace/[id]/billing/monthly-limit-section.tsx +133 -0
- package/src/routes/workspace/[id]/billing/payment-section.module.css +93 -0
- package/src/routes/workspace/[id]/billing/payment-section.tsx +122 -0
- package/src/routes/workspace/[id]/billing/reload-section.module.css +261 -0
- package/src/routes/workspace/[id]/billing/reload-section.tsx +213 -0
- package/src/routes/workspace/[id]/graph-section.module.css +145 -0
- package/src/routes/workspace/[id]/graph-section.tsx +475 -0
- package/src/routes/workspace/[id]/index.tsx +81 -0
- package/src/routes/workspace/[id]/keys/index.tsx +11 -0
- package/src/routes/workspace/[id]/keys/key-section.module.css +197 -0
- package/src/routes/workspace/[id]/keys/key-section.tsx +176 -0
- package/src/routes/workspace/[id]/members/index.tsx +11 -0
- package/src/routes/workspace/[id]/members/member-section.module.css +249 -0
- package/src/routes/workspace/[id]/members/member-section.tsx +343 -0
- package/src/routes/workspace/[id]/members/role-dropdown.css +72 -0
- package/src/routes/workspace/[id]/members/role-dropdown.tsx +43 -0
- package/src/routes/workspace/[id]/model-section.module.css +173 -0
- package/src/routes/workspace/[id]/model-section.tsx +174 -0
- package/src/routes/workspace/[id]/new-user-section.module.css +143 -0
- package/src/routes/workspace/[id]/new-user-section.tsx +104 -0
- package/src/routes/workspace/[id]/provider-section.module.css +138 -0
- package/src/routes/workspace/[id]/provider-section.tsx +188 -0
- package/src/routes/workspace/[id]/settings/index.tsx +11 -0
- package/src/routes/workspace/[id]/settings/settings-section.module.css +94 -0
- package/src/routes/workspace/[id]/settings/settings-section.tsx +122 -0
- package/src/routes/workspace/[id]/usage-section.module.css +185 -0
- package/src/routes/workspace/[id]/usage-section.tsx +200 -0
- package/src/routes/workspace/[id].css +308 -0
- package/src/routes/workspace/[id].tsx +62 -0
- package/src/routes/workspace/common.tsx +120 -0
- package/src/routes/workspace-picker.css +74 -0
- package/src/routes/workspace-picker.tsx +122 -0
- package/src/routes/workspace.css +107 -0
- package/src/routes/workspace.tsx +38 -0
- package/src/routes/zen/index.css +866 -0
- package/src/routes/zen/index.tsx +343 -0
- package/src/routes/zen/util/dataDumper.ts +44 -0
- package/src/routes/zen/util/error.ts +13 -0
- package/src/routes/zen/util/handler.ts +784 -0
- package/src/routes/zen/util/logger.ts +12 -0
- package/src/routes/zen/util/provider/anthropic.ts +752 -0
- package/src/routes/zen/util/provider/google.ts +75 -0
- package/src/routes/zen/util/provider/openai-compatible.ts +546 -0
- package/src/routes/zen/util/provider/openai.ts +630 -0
- package/src/routes/zen/util/provider/provider.ts +210 -0
- package/src/routes/zen/util/rateLimiter.ts +41 -0
- package/src/routes/zen/util/stickyProviderTracker.ts +16 -0
- package/src/routes/zen/util/trialLimiter.ts +49 -0
- package/src/routes/zen/v1/chat/completions.ts +11 -0
- package/src/routes/zen/v1/messages.ts +11 -0
- package/src/routes/zen/v1/models/[model].ts +13 -0
- package/src/routes/zen/v1/models.ts +60 -0
- package/src/routes/zen/v1/responses.ts +11 -0
- package/src/style/base.css +21 -0
- package/src/style/component/button.css +102 -0
- package/src/style/index.css +8 -0
- package/src/style/reset.css +76 -0
- package/src/style/token/color.css +91 -0
- package/src/style/token/font.css +21 -0
- package/src/style/token/space.css +46 -0
- package/sst-env.d.ts +9 -0
- package/tsconfig.json +21 -0
- package/vite.config.ts +25 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { ProviderHelper } from "./provider"
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
{
|
|
5
|
+
promptTokenCount: 11453,
|
|
6
|
+
candidatesTokenCount: 71,
|
|
7
|
+
totalTokenCount: 11625,
|
|
8
|
+
cachedContentTokenCount: 8100,
|
|
9
|
+
promptTokensDetails: [
|
|
10
|
+
{modality: "TEXT",tokenCount: 11453}
|
|
11
|
+
],
|
|
12
|
+
cacheTokensDetails: [
|
|
13
|
+
{modality: "TEXT",tokenCount: 8100}
|
|
14
|
+
],
|
|
15
|
+
thoughtsTokenCount: 101
|
|
16
|
+
}
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
type Usage = {
|
|
20
|
+
promptTokenCount?: number
|
|
21
|
+
candidatesTokenCount?: number
|
|
22
|
+
totalTokenCount?: number
|
|
23
|
+
cachedContentTokenCount?: number
|
|
24
|
+
promptTokensDetails?: { modality: string; tokenCount: number }[]
|
|
25
|
+
cacheTokensDetails?: { modality: string; tokenCount: number }[]
|
|
26
|
+
thoughtsTokenCount?: number
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const googleHelper: ProviderHelper = ({ providerModel }) => ({
|
|
30
|
+
format: "google",
|
|
31
|
+
modifyUrl: (providerApi: string, isStream?: boolean) =>
|
|
32
|
+
`${providerApi}/models/${providerModel}:${isStream ? "streamGenerateContent?alt=sse" : "generateContent"}`,
|
|
33
|
+
modifyHeaders: (headers: Headers, body: Record<string, any>, apiKey: string) => {
|
|
34
|
+
headers.set("x-goog-api-key", apiKey)
|
|
35
|
+
},
|
|
36
|
+
modifyBody: (body: Record<string, any>) => {
|
|
37
|
+
return body
|
|
38
|
+
},
|
|
39
|
+
createBinaryStreamDecoder: () => undefined,
|
|
40
|
+
streamSeparator: "\r\n\r\n",
|
|
41
|
+
createUsageParser: () => {
|
|
42
|
+
let usage: Usage
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
parse: (chunk: string) => {
|
|
46
|
+
if (!chunk.startsWith("data: ")) return
|
|
47
|
+
|
|
48
|
+
let json
|
|
49
|
+
try {
|
|
50
|
+
json = JSON.parse(chunk.slice(6)) as { usageMetadata?: Usage }
|
|
51
|
+
} catch (e) {
|
|
52
|
+
return
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (!json.usageMetadata) return
|
|
56
|
+
usage = json.usageMetadata
|
|
57
|
+
},
|
|
58
|
+
retrieve: () => usage,
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
normalizeUsage: (usage: Usage) => {
|
|
62
|
+
const inputTokens = usage.promptTokenCount ?? 0
|
|
63
|
+
const outputTokens = usage.candidatesTokenCount ?? 0
|
|
64
|
+
const reasoningTokens = usage.thoughtsTokenCount ?? 0
|
|
65
|
+
const cacheReadTokens = usage.cachedContentTokenCount ?? 0
|
|
66
|
+
return {
|
|
67
|
+
inputTokens: inputTokens - cacheReadTokens,
|
|
68
|
+
outputTokens,
|
|
69
|
+
reasoningTokens,
|
|
70
|
+
cacheReadTokens,
|
|
71
|
+
cacheWrite5mTokens: undefined,
|
|
72
|
+
cacheWrite1hTokens: undefined,
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
})
|
|
@@ -0,0 +1,546 @@
|
|
|
1
|
+
import { ProviderHelper, CommonRequest, CommonResponse, CommonChunk } from "./provider"
|
|
2
|
+
|
|
3
|
+
type Usage = {
|
|
4
|
+
prompt_tokens?: number
|
|
5
|
+
completion_tokens?: number
|
|
6
|
+
total_tokens?: number
|
|
7
|
+
// used by moonshot
|
|
8
|
+
cached_tokens?: number
|
|
9
|
+
// used by xai
|
|
10
|
+
prompt_tokens_details?: {
|
|
11
|
+
text_tokens?: number
|
|
12
|
+
audio_tokens?: number
|
|
13
|
+
image_tokens?: number
|
|
14
|
+
cached_tokens?: number
|
|
15
|
+
}
|
|
16
|
+
completion_tokens_details?: {
|
|
17
|
+
reasoning_tokens?: number
|
|
18
|
+
audio_tokens?: number
|
|
19
|
+
accepted_prediction_tokens?: number
|
|
20
|
+
rejected_prediction_tokens?: number
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const oaCompatHelper: ProviderHelper = () => ({
|
|
25
|
+
format: "oa-compat",
|
|
26
|
+
modifyUrl: (providerApi: string) => providerApi + "/chat/completions",
|
|
27
|
+
modifyHeaders: (headers: Headers, body: Record<string, any>, apiKey: string) => {
|
|
28
|
+
headers.set("authorization", `Bearer ${apiKey}`)
|
|
29
|
+
},
|
|
30
|
+
modifyBody: (body: Record<string, any>) => {
|
|
31
|
+
return {
|
|
32
|
+
...body,
|
|
33
|
+
...(body.stream ? { stream_options: { include_usage: true } } : {}),
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
createBinaryStreamDecoder: () => undefined,
|
|
37
|
+
streamSeparator: "\n\n",
|
|
38
|
+
createUsageParser: () => {
|
|
39
|
+
let usage: Usage
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
parse: (chunk: string) => {
|
|
43
|
+
if (!chunk.startsWith("data: ")) return
|
|
44
|
+
|
|
45
|
+
let json
|
|
46
|
+
try {
|
|
47
|
+
json = JSON.parse(chunk.slice(6)) as { usage?: Usage }
|
|
48
|
+
} catch (e) {
|
|
49
|
+
return
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (!json.usage) return
|
|
53
|
+
usage = json.usage
|
|
54
|
+
},
|
|
55
|
+
retrieve: () => usage,
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
normalizeUsage: (usage: Usage) => {
|
|
59
|
+
const inputTokens = usage.prompt_tokens ?? 0
|
|
60
|
+
const outputTokens = usage.completion_tokens ?? 0
|
|
61
|
+
const reasoningTokens = usage.completion_tokens_details?.reasoning_tokens ?? undefined
|
|
62
|
+
const cacheReadTokens = usage.cached_tokens ?? usage.prompt_tokens_details?.cached_tokens ?? undefined
|
|
63
|
+
return {
|
|
64
|
+
inputTokens: inputTokens - (cacheReadTokens ?? 0),
|
|
65
|
+
outputTokens,
|
|
66
|
+
reasoningTokens,
|
|
67
|
+
cacheReadTokens,
|
|
68
|
+
cacheWrite5mTokens: undefined,
|
|
69
|
+
cacheWrite1hTokens: undefined,
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
export function fromOaCompatibleRequest(body: any): CommonRequest {
|
|
75
|
+
if (!body || typeof body !== "object") return body
|
|
76
|
+
|
|
77
|
+
const msgsIn = Array.isArray(body.messages) ? body.messages : []
|
|
78
|
+
const msgsOut: any[] = []
|
|
79
|
+
|
|
80
|
+
for (const m of msgsIn) {
|
|
81
|
+
if (!m || !m.role) continue
|
|
82
|
+
|
|
83
|
+
if (m.role === "system") {
|
|
84
|
+
if (typeof m.content === "string" && m.content.length > 0) msgsOut.push({ role: "system", content: m.content })
|
|
85
|
+
continue
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (m.role === "user") {
|
|
89
|
+
if (typeof m.content === "string") {
|
|
90
|
+
msgsOut.push({ role: "user", content: m.content })
|
|
91
|
+
} else if (Array.isArray(m.content)) {
|
|
92
|
+
const parts: any[] = []
|
|
93
|
+
for (const p of m.content) {
|
|
94
|
+
if (!p || !p.type) continue
|
|
95
|
+
if (p.type === "text" && typeof p.text === "string") parts.push({ type: "text", text: p.text })
|
|
96
|
+
if (p.type === "image_url") parts.push({ type: "image_url", image_url: p.image_url })
|
|
97
|
+
}
|
|
98
|
+
if (parts.length === 1 && parts[0].type === "text") msgsOut.push({ role: "user", content: parts[0].text })
|
|
99
|
+
else if (parts.length > 0) msgsOut.push({ role: "user", content: parts })
|
|
100
|
+
}
|
|
101
|
+
continue
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (m.role === "assistant") {
|
|
105
|
+
const out: any = { role: "assistant" }
|
|
106
|
+
if (typeof m.content === "string") out.content = m.content
|
|
107
|
+
if (Array.isArray(m.tool_calls)) out.tool_calls = m.tool_calls
|
|
108
|
+
msgsOut.push(out)
|
|
109
|
+
continue
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (m.role === "tool") {
|
|
113
|
+
msgsOut.push({ role: "tool", tool_call_id: m.tool_call_id, content: m.content })
|
|
114
|
+
continue
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return {
|
|
119
|
+
model: body.model,
|
|
120
|
+
max_tokens: body.max_tokens,
|
|
121
|
+
temperature: body.temperature,
|
|
122
|
+
top_p: body.top_p,
|
|
123
|
+
stop: body.stop,
|
|
124
|
+
messages: msgsOut,
|
|
125
|
+
stream: !!body.stream,
|
|
126
|
+
tools: Array.isArray(body.tools) ? body.tools : undefined,
|
|
127
|
+
tool_choice: body.tool_choice,
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function toOaCompatibleRequest(body: CommonRequest) {
|
|
132
|
+
if (!body || typeof body !== "object") return body
|
|
133
|
+
|
|
134
|
+
const msgsIn = Array.isArray(body.messages) ? body.messages : []
|
|
135
|
+
const msgsOut: any[] = []
|
|
136
|
+
|
|
137
|
+
const toImg = (p: any) => {
|
|
138
|
+
if (!p || typeof p !== "object") return undefined
|
|
139
|
+
if (p.type === "image_url" && p.image_url) return { type: "image_url", image_url: p.image_url }
|
|
140
|
+
const s = (p as any).source
|
|
141
|
+
if (!s || typeof s !== "object") return undefined
|
|
142
|
+
if (s.type === "url" && typeof s.url === "string") return { type: "image_url", image_url: { url: s.url } }
|
|
143
|
+
if (s.type === "base64" && typeof s.media_type === "string" && typeof s.data === "string")
|
|
144
|
+
return { type: "image_url", image_url: { url: `data:${s.media_type};base64,${s.data}` } }
|
|
145
|
+
return undefined
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
for (const m of msgsIn) {
|
|
149
|
+
if (!m || !m.role) continue
|
|
150
|
+
|
|
151
|
+
if (m.role === "system") {
|
|
152
|
+
if (typeof m.content === "string" && m.content.length > 0) msgsOut.push({ role: "system", content: m.content })
|
|
153
|
+
continue
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (m.role === "user") {
|
|
157
|
+
if (typeof m.content === "string") {
|
|
158
|
+
msgsOut.push({ role: "user", content: m.content })
|
|
159
|
+
continue
|
|
160
|
+
}
|
|
161
|
+
if (Array.isArray(m.content)) {
|
|
162
|
+
const parts: any[] = []
|
|
163
|
+
for (const p of m.content) {
|
|
164
|
+
if (!p || !p.type) continue
|
|
165
|
+
if (p.type === "text" && typeof p.text === "string") parts.push({ type: "text", text: p.text })
|
|
166
|
+
const ip = toImg(p)
|
|
167
|
+
if (ip) parts.push(ip)
|
|
168
|
+
}
|
|
169
|
+
if (parts.length === 1 && parts[0].type === "text") msgsOut.push({ role: "user", content: parts[0].text })
|
|
170
|
+
else if (parts.length > 0) msgsOut.push({ role: "user", content: parts })
|
|
171
|
+
}
|
|
172
|
+
continue
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (m.role === "assistant") {
|
|
176
|
+
const out: any = { role: "assistant" }
|
|
177
|
+
if (typeof m.content === "string") out.content = m.content
|
|
178
|
+
if (Array.isArray(m.tool_calls)) out.tool_calls = m.tool_calls
|
|
179
|
+
msgsOut.push(out)
|
|
180
|
+
continue
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (m.role === "tool") {
|
|
184
|
+
msgsOut.push({ role: "tool", tool_call_id: m.tool_call_id, content: m.content })
|
|
185
|
+
continue
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const tools = Array.isArray(body.tools)
|
|
190
|
+
? body.tools.map((tool: any) => ({
|
|
191
|
+
type: "function",
|
|
192
|
+
function: {
|
|
193
|
+
name: tool.name,
|
|
194
|
+
description: tool.description,
|
|
195
|
+
parameters: tool.parameters,
|
|
196
|
+
},
|
|
197
|
+
}))
|
|
198
|
+
: undefined
|
|
199
|
+
|
|
200
|
+
return {
|
|
201
|
+
model: body.model,
|
|
202
|
+
max_tokens: body.max_tokens,
|
|
203
|
+
temperature: body.temperature,
|
|
204
|
+
top_p: body.top_p,
|
|
205
|
+
stop: body.stop,
|
|
206
|
+
messages: msgsOut,
|
|
207
|
+
stream: !!body.stream,
|
|
208
|
+
tools,
|
|
209
|
+
tool_choice: body.tool_choice,
|
|
210
|
+
response_format: (body as any).response_format,
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export function fromOaCompatibleResponse(resp: any): CommonResponse {
|
|
215
|
+
if (!resp || typeof resp !== "object") return resp
|
|
216
|
+
|
|
217
|
+
if (!Array.isArray((resp as any).choices)) return resp
|
|
218
|
+
|
|
219
|
+
const choice = (resp as any).choices[0]
|
|
220
|
+
if (!choice) return resp
|
|
221
|
+
|
|
222
|
+
const message = choice.message
|
|
223
|
+
if (!message) return resp
|
|
224
|
+
|
|
225
|
+
const content: any[] = []
|
|
226
|
+
|
|
227
|
+
if (typeof message.content === "string" && message.content.length > 0) {
|
|
228
|
+
content.push({ type: "text", text: message.content })
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (Array.isArray(message.tool_calls)) {
|
|
232
|
+
for (const toolCall of message.tool_calls) {
|
|
233
|
+
if (toolCall.type === "function" && toolCall.function) {
|
|
234
|
+
let input
|
|
235
|
+
try {
|
|
236
|
+
input = JSON.parse(toolCall.function.arguments)
|
|
237
|
+
} catch {
|
|
238
|
+
input = toolCall.function.arguments
|
|
239
|
+
}
|
|
240
|
+
content.push({
|
|
241
|
+
type: "tool_use",
|
|
242
|
+
id: toolCall.id,
|
|
243
|
+
name: toolCall.function.name,
|
|
244
|
+
input,
|
|
245
|
+
})
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const stopReason = (() => {
|
|
251
|
+
const reason = choice.finish_reason
|
|
252
|
+
if (reason === "stop") return "stop"
|
|
253
|
+
if (reason === "tool_calls") return "tool_calls"
|
|
254
|
+
if (reason === "length") return "length"
|
|
255
|
+
if (reason === "content_filter") return "content_filter"
|
|
256
|
+
return null
|
|
257
|
+
})()
|
|
258
|
+
|
|
259
|
+
const usage = (() => {
|
|
260
|
+
const u = (resp as any).usage
|
|
261
|
+
if (!u) return undefined
|
|
262
|
+
return {
|
|
263
|
+
prompt_tokens: u.prompt_tokens,
|
|
264
|
+
completion_tokens: u.completion_tokens,
|
|
265
|
+
total_tokens: u.total_tokens,
|
|
266
|
+
...(u.prompt_tokens_details?.cached_tokens
|
|
267
|
+
? { prompt_tokens_details: { cached_tokens: u.prompt_tokens_details.cached_tokens } }
|
|
268
|
+
: {}),
|
|
269
|
+
}
|
|
270
|
+
})()
|
|
271
|
+
|
|
272
|
+
return {
|
|
273
|
+
id: (resp as any).id,
|
|
274
|
+
object: "chat.completion" as const,
|
|
275
|
+
created: Math.floor(Date.now() / 1000),
|
|
276
|
+
model: (resp as any).model,
|
|
277
|
+
choices: [
|
|
278
|
+
{
|
|
279
|
+
index: 0,
|
|
280
|
+
message: {
|
|
281
|
+
role: "assistant" as const,
|
|
282
|
+
...(content.length > 0 && content.some((c) => c.type === "text")
|
|
283
|
+
? {
|
|
284
|
+
content: content
|
|
285
|
+
.filter((c) => c.type === "text")
|
|
286
|
+
.map((c: any) => c.text)
|
|
287
|
+
.join(""),
|
|
288
|
+
}
|
|
289
|
+
: {}),
|
|
290
|
+
...(content.length > 0 && content.some((c) => c.type === "tool_use")
|
|
291
|
+
? {
|
|
292
|
+
tool_calls: content
|
|
293
|
+
.filter((c) => c.type === "tool_use")
|
|
294
|
+
.map((c: any) => ({
|
|
295
|
+
id: c.id,
|
|
296
|
+
type: "function" as const,
|
|
297
|
+
function: {
|
|
298
|
+
name: c.name,
|
|
299
|
+
arguments: typeof c.input === "string" ? c.input : JSON.stringify(c.input),
|
|
300
|
+
},
|
|
301
|
+
})),
|
|
302
|
+
}
|
|
303
|
+
: {}),
|
|
304
|
+
},
|
|
305
|
+
finish_reason: stopReason,
|
|
306
|
+
},
|
|
307
|
+
],
|
|
308
|
+
...(usage ? { usage } : {}),
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export function toOaCompatibleResponse(resp: CommonResponse) {
|
|
313
|
+
if (!resp || typeof resp !== "object") return resp
|
|
314
|
+
|
|
315
|
+
if (Array.isArray((resp as any).choices)) return resp
|
|
316
|
+
|
|
317
|
+
const isAnthropic = typeof (resp as any).type === "string" && (resp as any).type === "message"
|
|
318
|
+
if (!isAnthropic) return resp
|
|
319
|
+
|
|
320
|
+
const idIn = (resp as any).id
|
|
321
|
+
const id =
|
|
322
|
+
typeof idIn === "string" ? idIn.replace(/^msg_/, "chatcmpl_") : `chatcmpl_${Math.random().toString(36).slice(2)}`
|
|
323
|
+
const model = (resp as any).model
|
|
324
|
+
|
|
325
|
+
const blocks: any[] = Array.isArray((resp as any).content) ? (resp as any).content : []
|
|
326
|
+
const text = blocks
|
|
327
|
+
.filter((b) => b && b.type === "text" && typeof b.text === "string")
|
|
328
|
+
.map((b) => b.text)
|
|
329
|
+
.join("")
|
|
330
|
+
const tcs = blocks
|
|
331
|
+
.filter((b) => b && b.type === "tool_use")
|
|
332
|
+
.map((b) => {
|
|
333
|
+
const name = (b as any).name
|
|
334
|
+
const args = (() => {
|
|
335
|
+
const inp = (b as any).input
|
|
336
|
+
if (typeof inp === "string") return inp
|
|
337
|
+
try {
|
|
338
|
+
return JSON.stringify(inp ?? {})
|
|
339
|
+
} catch {
|
|
340
|
+
return String(inp ?? "")
|
|
341
|
+
}
|
|
342
|
+
})()
|
|
343
|
+
const tid =
|
|
344
|
+
typeof (b as any).id === "string" && (b as any).id.length > 0
|
|
345
|
+
? (b as any).id
|
|
346
|
+
: `toolu_${Math.random().toString(36).slice(2)}`
|
|
347
|
+
return { id: tid, type: "function" as const, function: { name, arguments: args } }
|
|
348
|
+
})
|
|
349
|
+
|
|
350
|
+
const finish = (r: string | null) => {
|
|
351
|
+
if (r === "end_turn") return "stop"
|
|
352
|
+
if (r === "tool_use") return "tool_calls"
|
|
353
|
+
if (r === "max_tokens") return "length"
|
|
354
|
+
if (r === "content_filter") return "content_filter"
|
|
355
|
+
return null
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
const u = (resp as any).usage
|
|
359
|
+
const usage = (() => {
|
|
360
|
+
if (!u) return undefined as any
|
|
361
|
+
const pt = typeof u.input_tokens === "number" ? u.input_tokens : undefined
|
|
362
|
+
const ct = typeof u.output_tokens === "number" ? u.output_tokens : undefined
|
|
363
|
+
const total = pt != null && ct != null ? pt + ct : undefined
|
|
364
|
+
const cached = typeof u.cache_read_input_tokens === "number" ? u.cache_read_input_tokens : undefined
|
|
365
|
+
const details = cached != null ? { cached_tokens: cached } : undefined
|
|
366
|
+
return {
|
|
367
|
+
prompt_tokens: pt,
|
|
368
|
+
completion_tokens: ct,
|
|
369
|
+
total_tokens: total,
|
|
370
|
+
...(details ? { prompt_tokens_details: details } : {}),
|
|
371
|
+
}
|
|
372
|
+
})()
|
|
373
|
+
|
|
374
|
+
return {
|
|
375
|
+
id,
|
|
376
|
+
object: "chat.completion",
|
|
377
|
+
created: Math.floor(Date.now() / 1000),
|
|
378
|
+
model,
|
|
379
|
+
choices: [
|
|
380
|
+
{
|
|
381
|
+
index: 0,
|
|
382
|
+
message: {
|
|
383
|
+
role: "assistant",
|
|
384
|
+
...(text && text.length > 0 ? { content: text } : {}),
|
|
385
|
+
...(tcs.length > 0 ? { tool_calls: tcs } : {}),
|
|
386
|
+
},
|
|
387
|
+
finish_reason: finish((resp as any).stop_reason ?? null),
|
|
388
|
+
},
|
|
389
|
+
],
|
|
390
|
+
...(usage ? { usage } : {}),
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
export function fromOaCompatibleChunk(chunk: string): CommonChunk | string {
|
|
395
|
+
if (!chunk.startsWith("data: ")) return chunk
|
|
396
|
+
|
|
397
|
+
let json
|
|
398
|
+
try {
|
|
399
|
+
json = JSON.parse(chunk.slice(6))
|
|
400
|
+
} catch {
|
|
401
|
+
return chunk
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
if (!json.choices || !Array.isArray(json.choices) || json.choices.length === 0) {
|
|
405
|
+
return chunk
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
const choice = json.choices[0]
|
|
409
|
+
const delta = choice.delta
|
|
410
|
+
|
|
411
|
+
if (!delta) return chunk
|
|
412
|
+
|
|
413
|
+
const result: CommonChunk = {
|
|
414
|
+
id: json.id ?? "",
|
|
415
|
+
object: "chat.completion.chunk",
|
|
416
|
+
created: json.created ?? Math.floor(Date.now() / 1000),
|
|
417
|
+
model: json.model ?? "",
|
|
418
|
+
choices: [],
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
if (delta.content) {
|
|
422
|
+
result.choices.push({
|
|
423
|
+
index: choice.index ?? 0,
|
|
424
|
+
delta: { content: delta.content },
|
|
425
|
+
finish_reason: null,
|
|
426
|
+
})
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
if (delta.tool_calls) {
|
|
430
|
+
for (const toolCall of delta.tool_calls) {
|
|
431
|
+
result.choices.push({
|
|
432
|
+
index: choice.index ?? 0,
|
|
433
|
+
delta: {
|
|
434
|
+
tool_calls: [
|
|
435
|
+
{
|
|
436
|
+
index: toolCall.index ?? 0,
|
|
437
|
+
id: toolCall.id,
|
|
438
|
+
type: toolCall.type ?? "function",
|
|
439
|
+
function: toolCall.function,
|
|
440
|
+
},
|
|
441
|
+
],
|
|
442
|
+
},
|
|
443
|
+
finish_reason: null,
|
|
444
|
+
})
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
if (choice.finish_reason) {
|
|
449
|
+
result.choices.push({
|
|
450
|
+
index: choice.index ?? 0,
|
|
451
|
+
delta: {},
|
|
452
|
+
finish_reason: choice.finish_reason,
|
|
453
|
+
})
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
if (json.usage) {
|
|
457
|
+
const usage = json.usage
|
|
458
|
+
result.usage = {
|
|
459
|
+
prompt_tokens: usage.prompt_tokens,
|
|
460
|
+
completion_tokens: usage.completion_tokens,
|
|
461
|
+
total_tokens: usage.total_tokens,
|
|
462
|
+
...(usage.prompt_tokens_details?.cached_tokens
|
|
463
|
+
? { prompt_tokens_details: { cached_tokens: usage.prompt_tokens_details.cached_tokens } }
|
|
464
|
+
: {}),
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
return result
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
export function toOaCompatibleChunk(chunk: CommonChunk): string {
|
|
472
|
+
const result: any = {
|
|
473
|
+
id: chunk.id,
|
|
474
|
+
object: "chat.completion.chunk",
|
|
475
|
+
created: chunk.created,
|
|
476
|
+
model: chunk.model,
|
|
477
|
+
choices: [],
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
if (!chunk.choices || chunk.choices.length === 0) {
|
|
481
|
+
return `data: ${JSON.stringify(result)}`
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
const choice = chunk.choices[0]
|
|
485
|
+
const delta = choice.delta
|
|
486
|
+
|
|
487
|
+
if (delta?.role) {
|
|
488
|
+
result.choices.push({
|
|
489
|
+
index: choice.index,
|
|
490
|
+
delta: { role: delta.role },
|
|
491
|
+
finish_reason: null,
|
|
492
|
+
})
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
if (delta?.content) {
|
|
496
|
+
result.choices.push({
|
|
497
|
+
index: choice.index,
|
|
498
|
+
delta: { content: delta.content },
|
|
499
|
+
finish_reason: null,
|
|
500
|
+
})
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
if (delta?.tool_calls) {
|
|
504
|
+
for (const tc of delta.tool_calls) {
|
|
505
|
+
result.choices.push({
|
|
506
|
+
index: choice.index,
|
|
507
|
+
delta: {
|
|
508
|
+
tool_calls: [
|
|
509
|
+
{
|
|
510
|
+
index: tc.index,
|
|
511
|
+
id: tc.id,
|
|
512
|
+
type: tc.type,
|
|
513
|
+
function: tc.function,
|
|
514
|
+
},
|
|
515
|
+
],
|
|
516
|
+
},
|
|
517
|
+
finish_reason: null,
|
|
518
|
+
})
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
if (choice.finish_reason) {
|
|
523
|
+
result.choices.push({
|
|
524
|
+
index: choice.index,
|
|
525
|
+
delta: {},
|
|
526
|
+
finish_reason: choice.finish_reason,
|
|
527
|
+
})
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
if (chunk.usage) {
|
|
531
|
+
result.usage = {
|
|
532
|
+
prompt_tokens: chunk.usage.prompt_tokens,
|
|
533
|
+
completion_tokens: chunk.usage.completion_tokens,
|
|
534
|
+
total_tokens: chunk.usage.total_tokens,
|
|
535
|
+
...(chunk.usage.prompt_tokens_details?.cached_tokens
|
|
536
|
+
? {
|
|
537
|
+
prompt_tokens_details: {
|
|
538
|
+
cached_tokens: chunk.usage.prompt_tokens_details.cached_tokens,
|
|
539
|
+
},
|
|
540
|
+
}
|
|
541
|
+
: {}),
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
return `data: ${JSON.stringify(result)}`
|
|
546
|
+
}
|