forked from pwrdrvr/openclaw-codex-app-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.ts
More file actions
103 lines (95 loc) · 3.11 KB
/
config.ts
File metadata and controls
103 lines (95 loc) · 3.11 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import type { PluginSettings } from "./types.js";
import {
DEFAULT_INPUT_TIMEOUT_MS,
DEFAULT_REQUEST_TIMEOUT_MS,
} from "./types.js";
function asRecord(value: unknown): Record<string, unknown> {
return value && typeof value === "object" && !Array.isArray(value)
? (value as Record<string, unknown>)
: {};
}
function readString(record: Record<string, unknown>, key: string): string | undefined {
const value = record[key];
if (typeof value !== "string") {
return undefined;
}
const trimmed = value.trim();
return trimmed || undefined;
}
function readStringArray(record: Record<string, unknown>, key: string): string[] {
const value = record[key];
if (!Array.isArray(value)) {
return [];
}
return value
.filter((entry): entry is string => typeof entry === "string")
.map((entry) => entry.trim())
.filter(Boolean);
}
function readHeaders(record: Record<string, unknown>): Record<string, string> | undefined {
const value = record.headers;
if (!value || typeof value !== "object" || Array.isArray(value)) {
return undefined;
}
const headers = Object.fromEntries(
Object.entries(value)
.filter((entry): entry is [string, string] => typeof entry[1] === "string")
.map(([key, entryValue]) => [key, entryValue.trim()])
.filter((entry) => entry[0].trim() && entry[1]),
);
return Object.keys(headers).length > 0 ? headers : undefined;
}
function readNumber(
record: Record<string, unknown>,
key: string,
fallback: number,
minimum: number,
): number {
const value = record[key];
if (typeof value === "number" && Number.isFinite(value)) {
return Math.max(minimum, Math.round(value));
}
return fallback;
}
export function resolvePluginSettings(rawConfig: unknown): PluginSettings {
const record = asRecord(rawConfig);
const transport = record.transport === "websocket" ? "websocket" : "stdio";
const authToken = readString(record, "authToken");
const configuredHeaders = readHeaders(record);
const headers = {
...(configuredHeaders ?? {}),
...(authToken ? { Authorization: `Bearer ${authToken}` } : {}),
};
return {
enabled: record.enabled !== false,
transport,
command: readString(record, "command") ?? "codex",
args: readStringArray(record, "args"),
url: readString(record, "url"),
headers: Object.keys(headers).length > 0 ? headers : undefined,
requestTimeoutMs: readNumber(
record,
"requestTimeoutMs",
DEFAULT_REQUEST_TIMEOUT_MS,
100,
),
inputTimeoutMs: readNumber(record, "inputTimeoutMs", DEFAULT_INPUT_TIMEOUT_MS, 1_000),
defaultWorkspaceDir: readString(record, "defaultWorkspaceDir"),
defaultModel: readString(record, "defaultModel"),
defaultServiceTier: readString(record, "defaultServiceTier"),
};
}
export function resolveWorkspaceDir(params: {
requested?: string;
bindingWorkspaceDir?: string;
configuredWorkspaceDir?: string;
serviceWorkspaceDir?: string;
}): string {
return (
params.requested?.trim() ||
params.bindingWorkspaceDir?.trim() ||
params.configuredWorkspaceDir?.trim() ||
params.serviceWorkspaceDir?.trim() ||
process.cwd()
);
}