Repository navigation
Expand file tree
/
Copy pathrequest.ts
More file actions
44 lines (40 loc) · 1.57 KB
/
Copy pathrequest.ts
File metadata and controls
44 lines (40 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import type { Hooks } from "@opencode-ai/plugin";
import type { LlmStart } from "../../contract/observer.js";
import { nonNegativeInteger, nonNegativeNumber } from "../shared/number.js";
export type ChatParamsHookArgs = Parameters<NonNullable<Hooks["chat.params"]>>;
export function parseChatParams(
input: ChatParamsHookArgs[0],
output: ChatParamsHookArgs[1],
): Pick<LlmStart, "model" | "providerName" | "operation" | "parameters"> {
return {
model: input.model.api.id,
providerName: providerName(input.model.providerID, input.model.api.npm),
operation: ["@ai-sdk/google", "@ai-sdk/google-vertex"].includes(input.model.api.npm)
? "generate_content"
: "chat",
parameters: {
temperature: nonNegativeNumber(output.temperature),
topP: nonNegativeNumber(output.topP),
topK: nonNegativeInteger(output.topK),
maxOutputTokens: nonNegativeInteger(output.maxOutputTokens),
},
};
}
export function providerName(id: string, npm?: string) {
const names: Record<string, string> = {
openai: "openai",
anthropic: "anthropic",
"@ai-sdk/openai": "openai",
"@ai-sdk/anthropic": "anthropic",
"@ai-sdk/amazon-bedrock": "aws.bedrock",
"@ai-sdk/azure": "azure.ai.openai",
"@ai-sdk/google": "gcp.gemini",
"@ai-sdk/google-vertex": "gcp.vertex_ai",
"amazon-bedrock": "aws.bedrock",
azure: "azure.ai.openai",
google: "gcp.gemini",
"google-vertex": "gcp.vertex_ai",
};
// An OpenAI-compatible transport does not establish that OpenAI is the provider.
return names[id] ?? (npm ? names[npm] : undefined) ?? id;
}