@faberon/chat 0.1.0 → 0.1.1
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/package.json +1 -1
- package/src/api/client.ts +23 -12
- package/src/api/config.ts +40 -0
- package/src/index.ts +1 -1
package/package.json
CHANGED
package/src/api/client.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Thin typed HTTP client for the v0 API.
|
|
3
3
|
*
|
|
4
|
-
* Reads the base URL and bearer token from the
|
|
5
|
-
*
|
|
4
|
+
* Reads the base URL and bearer token from the shared config file written
|
|
5
|
+
* by `faberon init`.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
+
import { configPath, readConfigFile } from "./config.js";
|
|
8
9
|
import type {
|
|
9
10
|
ApiErrorBody,
|
|
10
11
|
CampaignCreate,
|
|
@@ -14,25 +15,35 @@ import type {
|
|
|
14
15
|
StatusResponse,
|
|
15
16
|
} from "./types.js";
|
|
16
17
|
|
|
17
|
-
const DEFAULT_BASE_URL = "http://127.0.0.1:8000";
|
|
18
|
-
|
|
19
18
|
export interface ClientConfig {
|
|
20
19
|
baseUrl: string;
|
|
21
20
|
token: string;
|
|
22
21
|
}
|
|
23
22
|
|
|
24
|
-
/** Resolve the client config from the
|
|
25
|
-
export function
|
|
23
|
+
/** Resolve the client config from the Faberon config file. */
|
|
24
|
+
export function loadClientConfig(
|
|
26
25
|
env: NodeJS.ProcessEnv = process.env,
|
|
27
26
|
): ClientConfig {
|
|
28
|
-
const
|
|
27
|
+
const file = readConfigFile(configPath(env));
|
|
28
|
+
const token = file.api_token;
|
|
29
29
|
if (!token) {
|
|
30
30
|
throw new Error(
|
|
31
|
-
|
|
32
|
-
"over an authenticated channel;
|
|
31
|
+
`No API token in ${configPath(env)}. Faberon Chat talks to the API ` +
|
|
32
|
+
"over an authenticated channel; run `faberon init` to set one up.",
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
// api_url wins; otherwise derive from host and port, which the file must
|
|
36
|
+
// carry (defaults exist only to write the file, never to read it).
|
|
37
|
+
const derived =
|
|
38
|
+
file.host !== undefined && file.port !== undefined
|
|
39
|
+
? `http://${file.host}:${file.port}`
|
|
40
|
+
: undefined;
|
|
41
|
+
const baseUrl = (file.api_url ?? derived)?.replace(/\/+$/, "");
|
|
42
|
+
if (!baseUrl) {
|
|
43
|
+
throw new Error(
|
|
44
|
+
`No api_url (or host/port) in ${configPath(env)}: run \`faberon init\`.`,
|
|
33
45
|
);
|
|
34
46
|
}
|
|
35
|
-
const baseUrl = (env.FABERON_API_URL ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
36
47
|
return { baseUrl, token };
|
|
37
48
|
}
|
|
38
49
|
|
|
@@ -49,8 +60,8 @@ export class ApiError extends Error {
|
|
|
49
60
|
export class FaberonClient {
|
|
50
61
|
constructor(private readonly config: ClientConfig) {}
|
|
51
62
|
|
|
52
|
-
static
|
|
53
|
-
return new FaberonClient(
|
|
63
|
+
static fromConfig(env: NodeJS.ProcessEnv = process.env): FaberonClient {
|
|
64
|
+
return new FaberonClient(loadClientConfig(env));
|
|
54
65
|
}
|
|
55
66
|
|
|
56
67
|
private async request<T>(path: string, init?: RequestInit): Promise<T> {
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reader for the shared Faberon config file (`faberon init` writes it).
|
|
3
|
+
*
|
|
4
|
+
* The clients only need the top-level scalars.
|
|
5
|
+
* The `[env]` section is server-side only.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { readFileSync } from "node:fs";
|
|
9
|
+
import { homedir } from "node:os";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
|
|
12
|
+
/** The config file path: `$FABERON_CONFIG` or the default location. */
|
|
13
|
+
export function configPath(env: NodeJS.ProcessEnv = process.env): string {
|
|
14
|
+
return (
|
|
15
|
+
env.FABERON_CONFIG ?? join(homedir(), ".config", "faberon", "config.toml")
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Top-level scalar entries from the config file; empty when absent. */
|
|
20
|
+
export function readConfigFile(path: string): Record<string, string> {
|
|
21
|
+
let text: string;
|
|
22
|
+
try {
|
|
23
|
+
text = readFileSync(path, "utf8");
|
|
24
|
+
} catch {
|
|
25
|
+
return {};
|
|
26
|
+
}
|
|
27
|
+
const values: Record<string, string> = {};
|
|
28
|
+
for (const line of text.split("\n")) {
|
|
29
|
+
const trimmed = line.trim();
|
|
30
|
+
if (trimmed.startsWith("[")) break;
|
|
31
|
+
const match =
|
|
32
|
+
/^([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(?:"((?:[^"\\]|\\.)*)"|(\d+))\s*(?:#.*)?$/.exec(
|
|
33
|
+
trimmed,
|
|
34
|
+
);
|
|
35
|
+
const [, key, str, int] = match ?? [];
|
|
36
|
+
const value = str ?? int;
|
|
37
|
+
if (key !== undefined && value !== undefined) values[key] = value;
|
|
38
|
+
}
|
|
39
|
+
return values;
|
|
40
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -57,7 +57,7 @@ async function promptNonEmpty(
|
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
export default function faberonChat(pi: ExtensionAPI): void {
|
|
60
|
-
const client = FaberonClient.
|
|
60
|
+
const client = FaberonClient.fromConfig();
|
|
61
61
|
|
|
62
62
|
pi.registerTool(listCampaignsTool(client));
|
|
63
63
|
pi.registerTool(showCampaignTool(client));
|