openclaw-codex-app-server 0.0.0

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/src/config.ts ADDED
@@ -0,0 +1,103 @@
1
+ import type { PluginSettings } from "./types.js";
2
+ import {
3
+ DEFAULT_INPUT_TIMEOUT_MS,
4
+ DEFAULT_REQUEST_TIMEOUT_MS,
5
+ } from "./types.js";
6
+
7
+ function asRecord(value: unknown): Record<string, unknown> {
8
+ return value && typeof value === "object" && !Array.isArray(value)
9
+ ? (value as Record<string, unknown>)
10
+ : {};
11
+ }
12
+
13
+ function readString(record: Record<string, unknown>, key: string): string | undefined {
14
+ const value = record[key];
15
+ if (typeof value !== "string") {
16
+ return undefined;
17
+ }
18
+ const trimmed = value.trim();
19
+ return trimmed || undefined;
20
+ }
21
+
22
+ function readStringArray(record: Record<string, unknown>, key: string): string[] {
23
+ const value = record[key];
24
+ if (!Array.isArray(value)) {
25
+ return [];
26
+ }
27
+ return value
28
+ .filter((entry): entry is string => typeof entry === "string")
29
+ .map((entry) => entry.trim())
30
+ .filter(Boolean);
31
+ }
32
+
33
+ function readHeaders(record: Record<string, unknown>): Record<string, string> | undefined {
34
+ const value = record.headers;
35
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
36
+ return undefined;
37
+ }
38
+ const headers = Object.fromEntries(
39
+ Object.entries(value)
40
+ .filter((entry): entry is [string, string] => typeof entry[1] === "string")
41
+ .map(([key, entryValue]) => [key, entryValue.trim()])
42
+ .filter((entry) => entry[0].trim() && entry[1]),
43
+ );
44
+ return Object.keys(headers).length > 0 ? headers : undefined;
45
+ }
46
+
47
+ function readNumber(
48
+ record: Record<string, unknown>,
49
+ key: string,
50
+ fallback: number,
51
+ minimum: number,
52
+ ): number {
53
+ const value = record[key];
54
+ if (typeof value === "number" && Number.isFinite(value)) {
55
+ return Math.max(minimum, Math.round(value));
56
+ }
57
+ return fallback;
58
+ }
59
+
60
+ export function resolvePluginSettings(rawConfig: unknown): PluginSettings {
61
+ const record = asRecord(rawConfig);
62
+ const transport = record.transport === "websocket" ? "websocket" : "stdio";
63
+ const authToken = readString(record, "authToken");
64
+ const configuredHeaders = readHeaders(record);
65
+ const headers = {
66
+ ...(configuredHeaders ?? {}),
67
+ ...(authToken ? { Authorization: `Bearer ${authToken}` } : {}),
68
+ };
69
+
70
+ return {
71
+ enabled: record.enabled !== false,
72
+ transport,
73
+ command: readString(record, "command") ?? "codex",
74
+ args: readStringArray(record, "args"),
75
+ url: readString(record, "url"),
76
+ headers: Object.keys(headers).length > 0 ? headers : undefined,
77
+ requestTimeoutMs: readNumber(
78
+ record,
79
+ "requestTimeoutMs",
80
+ DEFAULT_REQUEST_TIMEOUT_MS,
81
+ 100,
82
+ ),
83
+ inputTimeoutMs: readNumber(record, "inputTimeoutMs", DEFAULT_INPUT_TIMEOUT_MS, 1_000),
84
+ defaultWorkspaceDir: readString(record, "defaultWorkspaceDir"),
85
+ defaultModel: readString(record, "defaultModel"),
86
+ defaultServiceTier: readString(record, "defaultServiceTier"),
87
+ };
88
+ }
89
+
90
+ export function resolveWorkspaceDir(params: {
91
+ requested?: string;
92
+ bindingWorkspaceDir?: string;
93
+ configuredWorkspaceDir?: string;
94
+ serviceWorkspaceDir?: string;
95
+ }): string {
96
+ return (
97
+ params.requested?.trim() ||
98
+ params.bindingWorkspaceDir?.trim() ||
99
+ params.configuredWorkspaceDir?.trim() ||
100
+ params.serviceWorkspaceDir?.trim() ||
101
+ process.cwd()
102
+ );
103
+ }