@andreprado/agentkit 0.1.0-alpha.13 → 0.1.0-alpha.14

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andreprado/agentkit",
3
- "version": "0.1.0-alpha.13",
3
+ "version": "0.1.0-alpha.14",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "repository": {
@@ -1,4 +1,4 @@
1
- import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
1
+ import { chmod, mkdir, readFile, rm, writeFile } from "node:fs/promises";
2
2
  import { homedir } from "node:os";
3
3
  import { join } from "node:path";
4
4
 
@@ -97,6 +97,7 @@ export async function readCloudAuth(): Promise<CloudAuthConfig | null> {
97
97
  export async function writeCloudAuth(config: CloudAuthConfig): Promise<void> {
98
98
  await mkdir(join(homedir(), ".agentkit"), { recursive: true });
99
99
  await writeFile(cloudAuthPath(), `${JSON.stringify(config, null, 2)}\n`, { mode: 0o600 });
100
+ await chmod(cloudAuthPath(), 0o600);
100
101
  }
101
102
 
102
103
  export async function clearCloudAuth(): Promise<void> {
@@ -178,13 +179,17 @@ function cloudAuthMatchesApiUrl(auth: CloudAuthConfig, apiUrl: string): boolean
178
179
  export async function cloudApiRequest(apiUrl: string, path: string, init: RequestInit): Promise<unknown> {
179
180
  const auth = await readCloudAuth();
180
181
  const headers = new Headers(init.headers);
182
+ const apiToken =
183
+ auth?.source === "file" && !cloudAuthMatchesApiUrl(auth, apiUrl)
184
+ ? undefined
185
+ : auth?.token;
181
186
 
182
187
  if (!headers.has("Content-Type") && init.body !== undefined) {
183
188
  headers.set("Content-Type", "application/json");
184
189
  }
185
190
 
186
- if (auth?.token && !headers.has("Authorization")) {
187
- headers.set("Authorization", `Bearer ${auth.token}`);
191
+ if (apiToken && !headers.has("Authorization")) {
192
+ headers.set("Authorization", `Bearer ${apiToken}`);
188
193
  }
189
194
 
190
195
  const response = await fetch(new URL(path, parseCloudApiUrl(apiUrl)).href, {
@@ -32,6 +32,7 @@ const KNOWLEDGE_EMBEDDING_PROVIDERS = new Set<AgentKnowledgeEmbeddingProvider>([
32
32
  const CHANNEL_NAME_PATTERN = /^[a-z][a-z0-9-]*$/;
33
33
  const SECRET_NAME_PATTERN = /^[A-Z_][A-Z0-9_]*$/;
34
34
  const moduleDir = dirname(fileURLToPath(import.meta.url));
35
+ let configImportNonce = 0;
35
36
 
36
37
  export type LoadedAgentCapsule = {
37
38
  root: string;
@@ -108,15 +109,13 @@ function resolveLocalStoragePath(root: string, config: AgentConfig): string | un
108
109
  }
109
110
 
110
111
  async function loadConfigModule(configPath: string): Promise<unknown> {
111
- const configUrl = pathToFileURL(configPath);
112
- configUrl.searchParams.set("t", String(Date.now()));
112
+ const source = await readFile(configPath, "utf8");
113
113
 
114
114
  try {
115
- const module = (await import(configUrl.href)) as { default?: unknown };
116
- return module.default;
115
+ return await importConfigSource(configPath, source);
117
116
  } catch (error) {
118
117
  if (isMissingAgentKitSelfImport(error)) {
119
- return loadConfigModuleWithLocalSelfImport(configPath, error);
118
+ return loadConfigModuleWithLocalSelfImport(configPath, source, error);
120
119
  }
121
120
 
122
121
  throw new AgentKitError(
@@ -127,8 +126,11 @@ async function loadConfigModule(configPath: string): Promise<unknown> {
127
126
  }
128
127
  }
129
128
 
130
- async function loadConfigModuleWithLocalSelfImport(configPath: string, originalError: unknown): Promise<unknown> {
131
- const source = await readFile(configPath, "utf8");
129
+ async function loadConfigModuleWithLocalSelfImport(
130
+ configPath: string,
131
+ source: string,
132
+ originalError: unknown,
133
+ ): Promise<unknown> {
132
134
  const localAgentKitUrl = pathToFileURL(resolve(moduleDir, "../index.ts")).href;
133
135
  const patchedSource = source.replace(
134
136
  /from\s+["']@andreprado\/agentkit["']/g,
@@ -143,15 +145,20 @@ async function loadConfigModuleWithLocalSelfImport(configPath: string, originalE
143
145
  );
144
146
  }
145
147
 
148
+ return importConfigSource(configPath, patchedSource);
149
+ }
150
+
151
+ async function importConfigSource(configPath: string, source: string): Promise<unknown> {
152
+ const nonce = nextConfigImportNonce();
146
153
  const tempConfigPath = join(
147
154
  dirname(configPath),
148
- `.agentkit.config.loader-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}.ts`,
155
+ `.agentkit.config.loader-${process.pid}-${nonce}-${Math.random().toString(16).slice(2)}.ts`,
149
156
  );
150
157
 
151
158
  try {
152
- await writeFile(tempConfigPath, patchedSource);
159
+ await writeFile(tempConfigPath, source);
153
160
  const configUrl = pathToFileURL(tempConfigPath);
154
- configUrl.searchParams.set("t", String(Date.now()));
161
+ configUrl.searchParams.set("t", nonce);
155
162
  const module = (await import(configUrl.href)) as { default?: unknown };
156
163
  return module.default;
157
164
  } catch (error) {
@@ -165,6 +172,11 @@ async function loadConfigModuleWithLocalSelfImport(configPath: string, originalE
165
172
  }
166
173
  }
167
174
 
175
+ function nextConfigImportNonce(): string {
176
+ configImportNonce += 1;
177
+ return `${Date.now()}-${configImportNonce}`;
178
+ }
179
+
168
180
  function isMissingAgentKitSelfImport(error: unknown): boolean {
169
181
  const message = error instanceof Error ? error.message : String(error);
170
182
  return message.includes("@andreprado/agentkit");