gatana-sdk 2.3.3 → 2.3.5
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/dist/config.cjs +1 -7
- package/dist/config.d.cts +4 -3
- package/dist/config.d.cts.map +1 -1
- package/dist/config.d.mts +4 -3
- package/dist/config.d.mts.map +1 -1
- package/dist/config.mjs +1 -7
- package/dist/config.mjs.map +1 -1
- package/dist/index.cjs +8 -3
- package/dist/index.d.cts +4 -0
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +4 -0
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +9 -4
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/config.cjs
CHANGED
|
@@ -15,13 +15,7 @@ function readConfig() {
|
|
|
15
15
|
try {
|
|
16
16
|
if (!(0, fs.existsSync)(CONFIG_FILE_PATH)) return {};
|
|
17
17
|
const configData = (0, fs.readFileSync)(CONFIG_FILE_PATH, "utf8");
|
|
18
|
-
|
|
19
|
-
if (config.orgId && !config.orgs) {
|
|
20
|
-
const baseUrl = config.baseUrl || `https://${config.orgId}.gatana.ai`;
|
|
21
|
-
config.orgs = { [baseUrl]: { apiKey: config.apiKey } };
|
|
22
|
-
config.defaultOrgId = baseUrl;
|
|
23
|
-
}
|
|
24
|
-
return config;
|
|
18
|
+
return JSON.parse(configData);
|
|
25
19
|
} catch (error) {
|
|
26
20
|
return {};
|
|
27
21
|
}
|
package/dist/config.d.cts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
//#region src/config.d.ts
|
|
2
2
|
interface OrganizationConfig {
|
|
3
3
|
baseUrl: string;
|
|
4
|
-
|
|
4
|
+
pat?: string;
|
|
5
5
|
tokens?: {
|
|
6
6
|
access_token: string;
|
|
7
7
|
refresh_token: string;
|
|
8
8
|
expires_at: number;
|
|
9
9
|
};
|
|
10
10
|
}
|
|
11
|
+
type OrgId = string;
|
|
11
12
|
interface GatanaFileConfig {
|
|
12
|
-
orgs?: Record<
|
|
13
|
-
defaultOrgId?:
|
|
13
|
+
orgs?: Record<OrgId, OrganizationConfig>;
|
|
14
|
+
defaultOrgId?: OrgId;
|
|
14
15
|
}
|
|
15
16
|
declare function getConfigFilePath(): string;
|
|
16
17
|
declare function readConfig(): GatanaFileConfig;
|
package/dist/config.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.cts","names":[],"sources":["../src/config.ts"],"mappings":";UAKiB,kBAAA;EACf,OAAA;EACA,
|
|
1
|
+
{"version":3,"file":"config.d.cts","names":[],"sources":["../src/config.ts"],"mappings":";UAKiB,kBAAA;EACf,OAAA;EACA,GAAA;EACA,MAAA;IACE,YAAA;IACA,aAAA;IACA,UAAA;EAAA;AAAA;AAAA,KAIC,KAAA;AAAA,UAEY,gBAAA;EACf,IAAA,GAAO,MAAA,CAAO,KAAA,EAAO,kBAAA;EACrB,YAAA,GAAe,KAAA;AAAA;AAAA,iBAKD,iBAAA,CAAA;AAAA,iBAIA,UAAA,CAAA,GAAc,gBAAA;AAAA,iBAgBd,WAAA,CAAY,MAAA,EAAQ,gBAAA;AAAA,iBAcpB,YAAA,CAAa,OAAA,EAAS,OAAA,CAAQ,gBAAA;AAAA,iBAQ9B,oBAAA,CAAqB,OAAA;AAAA,iBAIrB,qBAAA,CAAsB,KAAA,UAAe,SAAA,EAAW,OAAA,CAAQ,kBAAA;AAAA,iBAiBxD,eAAA,CAAgB,KAAA,WAAgB,kBAAA;AAAA,iBAKhC,iBAAA,CAAA;AAAA,iBAKA,sBAAA,CAAuB,KAAA;AAAA,iBAUvB,sBAAA,CAAA;AAAA,iBAKA,kBAAA,CAAmB,KAAA"}
|
package/dist/config.d.mts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
//#region src/config.d.ts
|
|
2
2
|
interface OrganizationConfig {
|
|
3
3
|
baseUrl: string;
|
|
4
|
-
|
|
4
|
+
pat?: string;
|
|
5
5
|
tokens?: {
|
|
6
6
|
access_token: string;
|
|
7
7
|
refresh_token: string;
|
|
8
8
|
expires_at: number;
|
|
9
9
|
};
|
|
10
10
|
}
|
|
11
|
+
type OrgId = string;
|
|
11
12
|
interface GatanaFileConfig {
|
|
12
|
-
orgs?: Record<
|
|
13
|
-
defaultOrgId?:
|
|
13
|
+
orgs?: Record<OrgId, OrganizationConfig>;
|
|
14
|
+
defaultOrgId?: OrgId;
|
|
14
15
|
}
|
|
15
16
|
declare function getConfigFilePath(): string;
|
|
16
17
|
declare function readConfig(): GatanaFileConfig;
|
package/dist/config.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.mts","names":[],"sources":["../src/config.ts"],"mappings":";UAKiB,kBAAA;EACf,OAAA;EACA,
|
|
1
|
+
{"version":3,"file":"config.d.mts","names":[],"sources":["../src/config.ts"],"mappings":";UAKiB,kBAAA;EACf,OAAA;EACA,GAAA;EACA,MAAA;IACE,YAAA;IACA,aAAA;IACA,UAAA;EAAA;AAAA;AAAA,KAIC,KAAA;AAAA,UAEY,gBAAA;EACf,IAAA,GAAO,MAAA,CAAO,KAAA,EAAO,kBAAA;EACrB,YAAA,GAAe,KAAA;AAAA;AAAA,iBAKD,iBAAA,CAAA;AAAA,iBAIA,UAAA,CAAA,GAAc,gBAAA;AAAA,iBAgBd,WAAA,CAAY,MAAA,EAAQ,gBAAA;AAAA,iBAcpB,YAAA,CAAa,OAAA,EAAS,OAAA,CAAQ,gBAAA;AAAA,iBAQ9B,oBAAA,CAAqB,OAAA;AAAA,iBAIrB,qBAAA,CAAsB,KAAA,UAAe,SAAA,EAAW,OAAA,CAAQ,kBAAA;AAAA,iBAiBxD,eAAA,CAAgB,KAAA,WAAgB,kBAAA;AAAA,iBAKhC,iBAAA,CAAA;AAAA,iBAKA,sBAAA,CAAuB,KAAA;AAAA,iBAUvB,sBAAA,CAAA;AAAA,iBAKA,kBAAA,CAAmB,KAAA"}
|
package/dist/config.mjs
CHANGED
|
@@ -12,13 +12,7 @@ function readConfig() {
|
|
|
12
12
|
try {
|
|
13
13
|
if (!existsSync(CONFIG_FILE_PATH)) return {};
|
|
14
14
|
const configData = readFileSync(CONFIG_FILE_PATH, "utf8");
|
|
15
|
-
|
|
16
|
-
if (config.orgId && !config.orgs) {
|
|
17
|
-
const baseUrl = config.baseUrl || `https://${config.orgId}.gatana.ai`;
|
|
18
|
-
config.orgs = { [baseUrl]: { apiKey: config.apiKey } };
|
|
19
|
-
config.defaultOrgId = baseUrl;
|
|
20
|
-
}
|
|
21
|
-
return config;
|
|
15
|
+
return JSON.parse(configData);
|
|
22
16
|
} catch (error) {
|
|
23
17
|
return {};
|
|
24
18
|
}
|
package/dist/config.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.mjs","names":[],"sources":["../src/config.ts"],"sourcesContent":["import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';\nimport { join } from 'path';\nimport { homedir } from 'os';\nimport _ from 'lodash';\n\nexport interface OrganizationConfig {\n baseUrl: string;\n
|
|
1
|
+
{"version":3,"file":"config.mjs","names":[],"sources":["../src/config.ts"],"sourcesContent":["import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';\nimport { join } from 'path';\nimport { homedir } from 'os';\nimport _ from 'lodash';\n\nexport interface OrganizationConfig {\n baseUrl: string;\n pat?: string;\n tokens?: {\n access_token: string;\n refresh_token: string;\n expires_at: number; // Unix timestamp\n };\n}\n\ntype OrgId = string;\n\nexport interface GatanaFileConfig {\n orgs?: Record<OrgId, OrganizationConfig>;\n defaultOrgId?: OrgId;\n}\n\nconst CONFIG_FILE_PATH = join(homedir(), '.gatana.config');\n\nexport function getConfigFilePath(): string {\n return CONFIG_FILE_PATH;\n}\n\nexport function readConfig(): GatanaFileConfig {\n try {\n if (!existsSync(CONFIG_FILE_PATH)) {\n return {};\n }\n\n const configData = readFileSync(CONFIG_FILE_PATH, 'utf8');\n const config = JSON.parse(configData);\n\n return config;\n } catch (error) {\n // If file doesn't exist or is malformed, return empty config\n return {};\n }\n}\n\nexport function writeConfig(config: GatanaFileConfig): void {\n try {\n // Ensure the directory exists (though homedir should always exist)\n const configDir = homedir();\n if (!existsSync(configDir)) {\n mkdirSync(configDir, { recursive: true });\n }\n\n writeFileSync(CONFIG_FILE_PATH, JSON.stringify(config, null, 2), 'utf8');\n } catch (error) {\n throw new Error(`Failed to write config file: ${(error as Error).message}`);\n }\n}\n\nexport function updateConfig(updates: Partial<GatanaFileConfig>): void {\n const currentConfig = readConfig();\n const newConfig = { ...currentConfig, ...updates };\n writeConfig(newConfig);\n}\n\n// Tenant management functions\n\nexport function extractTenantFromUrl(baseUrl: string): string {\n return new URL(baseUrl).hostname.split('.').shift() || '';\n}\n\nexport function setOrganizationConfig(orgId: string, orgConfig: Partial<OrganizationConfig>): void {\n const config = readConfig();\n\n if (!config.orgs) {\n config.orgs = {};\n }\n\n _.merge(config.orgs, { [orgId]: orgConfig });\n\n // Set as default if it's the first organization\n if (!config.defaultOrgId) {\n config.defaultOrgId = orgId;\n }\n\n writeConfig(config);\n}\n\nexport function getOrganization(orgId: string): OrganizationConfig | undefined {\n const config = readConfig();\n return config.orgs?.[orgId.toLowerCase()];\n}\n\nexport function listOrganizations(): string[] {\n const config = readConfig();\n return Object.keys(config.orgs || {});\n}\n\nexport function setDefaultOrganization(orgId: string): void {\n const config = readConfig();\n if (!config.orgs?.[orgId]) {\n throw new Error(`${orgId} not found in config`);\n }\n\n config.defaultOrgId = orgId;\n writeConfig(config);\n}\n\nexport function getDefaultOrganization(): string | undefined {\n const config = readConfig();\n return config?.defaultOrgId;\n}\n\nexport function removeOrganization(orgId: string): void {\n const config = readConfig();\n\n if (!config.orgs?.[orgId]) {\n throw new Error(`${orgId} not found in config`);\n }\n\n delete config.orgs[orgId];\n\n // If we're removing the default org id, pick a new one\n if (config.defaultOrgId === orgId) {\n const remainingOrganizations = Object.keys(config.orgs);\n config.defaultOrgId = remainingOrganizations.length > 0 ? remain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AY;AAC3B,KAAI,CAAC,OAAO,OAAO,OACjB,OAAM,IAAI,MAAM,GAAG,MAAM,sBAAsB;AAGjD,QAAO,eAAe;AACtB,aAAY,OAAO;;AAGrB,SAAgB,yBAA6C;AAE3D,QADe,YAAY,EACZ;;AAGjB,SAAgB,mBAAmB,OAAqB;CACtD,MAAM,SAAS,YAAY;AAE3B,KAAI,CAAC,OAAO,OAAO,OACjB,OAAM,IAAI,MAAM,GAAG,MAAM,sBAAsB;AAGjD,QAAO,OAAO,KAAK;AAGnB,KAAI,OAAO,iBAAiB,OAAO;EACjC,MAAM,yBAAyB,OAAO,KAAK,OAAO,KAAK;AACvD,SAAO,eAAe,uBAAuB,SAAS,IAAI,uBAAuB,KAAK;;AAGxF,aAAY,OAAO"}
|
package/dist/index.cjs
CHANGED
|
@@ -87,8 +87,10 @@ var OptionsConfigStrategy = class extends ConfigStrategy {
|
|
|
87
87
|
if (!this.options) return null;
|
|
88
88
|
if (this.options.apiKey && (this.options.orgId || this.options.baseUrl)) {
|
|
89
89
|
const key = this.options.apiKey;
|
|
90
|
+
const baseUrl = this.options.baseUrl || `https://${this.options.orgId}.gatana.ai`;
|
|
90
91
|
return {
|
|
91
|
-
|
|
92
|
+
orgId: this.options.orgId,
|
|
93
|
+
baseUrl,
|
|
92
94
|
token: async () => key
|
|
93
95
|
};
|
|
94
96
|
}
|
|
@@ -101,6 +103,7 @@ var EnvConfigStrategy = class extends ConfigStrategy {
|
|
|
101
103
|
const orgId = process.env.GATANA_ORG_ID;
|
|
102
104
|
const overrideBaseUrl = process.env.GATANA_BASE_URL;
|
|
103
105
|
if (apiKey && (orgId || overrideBaseUrl)) return {
|
|
106
|
+
orgId: orgId || require_config.extractTenantFromUrl(overrideBaseUrl),
|
|
104
107
|
baseUrl: overrideBaseUrl || `https://${orgId}.gatana.ai`,
|
|
105
108
|
token: async () => apiKey
|
|
106
109
|
};
|
|
@@ -117,11 +120,13 @@ var FileConfigStrategy = class extends ConfigStrategy {
|
|
|
117
120
|
if (!orgId) return null;
|
|
118
121
|
debug$1(`FileConfigLoader loading config for orgId=${orgId}`);
|
|
119
122
|
const tenantConfig = require_config.getOrganization(orgId);
|
|
123
|
+
debug$1(tenantConfig);
|
|
120
124
|
if (!tenantConfig) return null;
|
|
121
125
|
return {
|
|
126
|
+
orgId,
|
|
122
127
|
baseUrl: tenantConfig.baseUrl,
|
|
123
128
|
token: async () => {
|
|
124
|
-
if (tenantConfig?.
|
|
129
|
+
if (tenantConfig?.pat) return tenantConfig.pat;
|
|
125
130
|
else if (tenantConfig?.tokens?.access_token && tenantConfig.tokens.expires_at && tenantConfig.tokens.expires_at > Math.floor(Date.now() / 1e3) + 60) return tenantConfig.tokens.access_token;
|
|
126
131
|
else if (tenantConfig.tokens?.refresh_token) {
|
|
127
132
|
debug$1("Access token expired or about to expire, attempting to refresh");
|
|
@@ -139,7 +144,7 @@ var FileConfigStrategy = class extends ConfigStrategy {
|
|
|
139
144
|
}
|
|
140
145
|
} catch (error) {
|
|
141
146
|
debug$1("Failed to refresh token", error);
|
|
142
|
-
throw new Error(
|
|
147
|
+
throw new Error(`Failed to refresh access token. Please log in again. (${error.message})`);
|
|
143
148
|
}
|
|
144
149
|
}
|
|
145
150
|
throw new Error("No valid API key, access token or refresh token available.");
|
package/dist/index.d.cts
CHANGED
|
@@ -33,6 +33,7 @@ declare class ConfigLoader {
|
|
|
33
33
|
}
|
|
34
34
|
declare abstract class ConfigStrategy {
|
|
35
35
|
abstract getConfig(): {
|
|
36
|
+
orgId: string;
|
|
36
37
|
baseUrl: string;
|
|
37
38
|
token: () => Promise<string>;
|
|
38
39
|
} | null;
|
|
@@ -41,12 +42,14 @@ declare class OptionsConfigStrategy extends ConfigStrategy {
|
|
|
41
42
|
private options?;
|
|
42
43
|
constructor(options?: GatanaOptions | undefined);
|
|
43
44
|
getConfig(): {
|
|
45
|
+
orgId: string;
|
|
44
46
|
baseUrl: string;
|
|
45
47
|
token: () => Promise<string>;
|
|
46
48
|
} | null;
|
|
47
49
|
}
|
|
48
50
|
declare class EnvConfigStrategy extends ConfigStrategy {
|
|
49
51
|
getConfig(): {
|
|
52
|
+
orgId: string;
|
|
50
53
|
baseUrl: string;
|
|
51
54
|
token: () => Promise<string>;
|
|
52
55
|
} | null;
|
|
@@ -55,6 +58,7 @@ declare class FileConfigStrategy extends ConfigStrategy {
|
|
|
55
58
|
private orgId?;
|
|
56
59
|
constructor(orgId?: string | undefined);
|
|
57
60
|
getConfig(): {
|
|
61
|
+
orgId: string;
|
|
58
62
|
baseUrl: string;
|
|
59
63
|
token: () => Promise<string>;
|
|
60
64
|
} | null;
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/v2.ts","../src/index.ts"],"mappings":";;;;;;AAiBA;cAAa,OAAA;EACJ,GAAA,SAAG,mBAAA;EAAA,SACM,MAAA,EAAQ,YAAA;cACZ,GAAA;IAAQ,OAAA,GAAU,aAAA;IAAe,YAAA,GAAe,YAAA;IAAc,KAAA;EAAA;EA8C1E,iBAAA,CAAA;AAAA;;;UC1De,aAAA;EACf,MAAA;EACA,KAAA;EACA,OAAA;AAAA;AAAA,KAGU,YAAA;EAAiB,OAAA;EAAiB,KAAA,QAAa,OAAA;AAAA;AAAA,cAC9C,YAAA;EAAA,QACS,UAAA;cAAA,UAAA,EAAY,cAAA;EAEhC,SAAA,CAAA,GAAa,YAAA;AAAA;AAAA,uBAWO,cAAA;EAAA,SACX,SAAA,CAAA;IAAe,OAAA;IAAiB,KAAA,QAAa,OAAA;EAAA;AAAA;AAAA,
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/v2.ts","../src/index.ts"],"mappings":";;;;;;AAiBA;cAAa,OAAA;EACJ,GAAA,SAAG,mBAAA;EAAA,SACM,MAAA,EAAQ,YAAA;cACZ,GAAA;IAAQ,OAAA,GAAU,aAAA;IAAe,YAAA,GAAe,YAAA;IAAc,KAAA;EAAA;EA8C1E,iBAAA,CAAA;AAAA;;;UC1De,aAAA;EACf,MAAA;EACA,KAAA;EACA,OAAA;AAAA;AAAA,KAGU,YAAA;EAAiB,OAAA;EAAiB,KAAA,QAAa,OAAA;AAAA;AAAA,cAC9C,YAAA;EAAA,QACS,UAAA;cAAA,UAAA,EAAY,cAAA;EAEhC,SAAA,CAAA,GAAa,YAAA;AAAA;AAAA,uBAWO,cAAA;EAAA,SACX,SAAA,CAAA;IAAe,KAAA;IAAe,OAAA;IAAiB,KAAA,QAAa,OAAA;EAAA;AAAA;AAAA,cAG1D,qBAAA,SAA8B,cAAA;EAAA,QACrB,OAAA;cAAA,OAAA,GAAU,aAAA;EAI9B,SAAA,CAAA;;;;;;cAiBW,iBAAA,SAA0B,cAAA;EACrC,SAAA,CAAA;;;iBAD6B,OAAA;EAAA;AAAA;AAAA,cAgBlB,kBAAA,SAA2B,cAAA;EAAA,QAClB,KAAA;cAAA,KAAA;EAIpB,SAAA,CAAA;;;iBAL8B,OAAA;EAAA;AAAA;AAAA,cA2DnB,MAAA;EACJ,GAAA,SAAG,iBAAA;EACH,MAAA,EAAQ,YAAA;cACH,GAAA;IAAQ,OAAA,GAAU,aAAA;IAAe,YAAA,GAAe,YAAA;IAAc,KAAA;EAAA;AAAA"}
|
package/dist/index.d.mts
CHANGED
|
@@ -33,6 +33,7 @@ declare class ConfigLoader {
|
|
|
33
33
|
}
|
|
34
34
|
declare abstract class ConfigStrategy {
|
|
35
35
|
abstract getConfig(): {
|
|
36
|
+
orgId: string;
|
|
36
37
|
baseUrl: string;
|
|
37
38
|
token: () => Promise<string>;
|
|
38
39
|
} | null;
|
|
@@ -41,12 +42,14 @@ declare class OptionsConfigStrategy extends ConfigStrategy {
|
|
|
41
42
|
private options?;
|
|
42
43
|
constructor(options?: GatanaOptions | undefined);
|
|
43
44
|
getConfig(): {
|
|
45
|
+
orgId: string;
|
|
44
46
|
baseUrl: string;
|
|
45
47
|
token: () => Promise<string>;
|
|
46
48
|
} | null;
|
|
47
49
|
}
|
|
48
50
|
declare class EnvConfigStrategy extends ConfigStrategy {
|
|
49
51
|
getConfig(): {
|
|
52
|
+
orgId: string;
|
|
50
53
|
baseUrl: string;
|
|
51
54
|
token: () => Promise<string>;
|
|
52
55
|
} | null;
|
|
@@ -55,6 +58,7 @@ declare class FileConfigStrategy extends ConfigStrategy {
|
|
|
55
58
|
private orgId?;
|
|
56
59
|
constructor(orgId?: string | undefined);
|
|
57
60
|
getConfig(): {
|
|
61
|
+
orgId: string;
|
|
58
62
|
baseUrl: string;
|
|
59
63
|
token: () => Promise<string>;
|
|
60
64
|
} | null;
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/v2.ts","../src/index.ts"],"mappings":";;;;;;AAiBA;cAAa,OAAA;EACJ,GAAA,SAAG,mBAAA;EAAA,SACM,MAAA,EAAQ,YAAA;cACZ,GAAA;IAAQ,OAAA,GAAU,aAAA;IAAe,YAAA,GAAe,YAAA;IAAc,KAAA;EAAA;EA8C1E,iBAAA,CAAA;AAAA;;;UC1De,aAAA;EACf,MAAA;EACA,KAAA;EACA,OAAA;AAAA;AAAA,KAGU,YAAA;EAAiB,OAAA;EAAiB,KAAA,QAAa,OAAA;AAAA;AAAA,cAC9C,YAAA;EAAA,QACS,UAAA;cAAA,UAAA,EAAY,cAAA;EAEhC,SAAA,CAAA,GAAa,YAAA;AAAA;AAAA,uBAWO,cAAA;EAAA,SACX,SAAA,CAAA;IAAe,OAAA;IAAiB,KAAA,QAAa,OAAA;EAAA;AAAA;AAAA,
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/v2.ts","../src/index.ts"],"mappings":";;;;;;AAiBA;cAAa,OAAA;EACJ,GAAA,SAAG,mBAAA;EAAA,SACM,MAAA,EAAQ,YAAA;cACZ,GAAA;IAAQ,OAAA,GAAU,aAAA;IAAe,YAAA,GAAe,YAAA;IAAc,KAAA;EAAA;EA8C1E,iBAAA,CAAA;AAAA;;;UC1De,aAAA;EACf,MAAA;EACA,KAAA;EACA,OAAA;AAAA;AAAA,KAGU,YAAA;EAAiB,OAAA;EAAiB,KAAA,QAAa,OAAA;AAAA;AAAA,cAC9C,YAAA;EAAA,QACS,UAAA;cAAA,UAAA,EAAY,cAAA;EAEhC,SAAA,CAAA,GAAa,YAAA;AAAA;AAAA,uBAWO,cAAA;EAAA,SACX,SAAA,CAAA;IAAe,KAAA;IAAe,OAAA;IAAiB,KAAA,QAAa,OAAA;EAAA;AAAA;AAAA,cAG1D,qBAAA,SAA8B,cAAA;EAAA,QACrB,OAAA;cAAA,OAAA,GAAU,aAAA;EAI9B,SAAA,CAAA;;;;;;cAiBW,iBAAA,SAA0B,cAAA;EACrC,SAAA,CAAA;;;iBAD6B,OAAA;EAAA;AAAA;AAAA,cAgBlB,kBAAA,SAA2B,cAAA;EAAA,QAClB,KAAA;cAAA,KAAA;EAIpB,SAAA,CAAA;;;iBAL8B,OAAA;EAAA;AAAA;AAAA,cA2DnB,MAAA;EACJ,GAAA,SAAG,iBAAA;EACH,MAAA,EAAQ,YAAA;cACH,GAAA;IAAQ,OAAA,GAAU,aAAA;IAAe,YAAA,GAAe,YAAA;IAAc,KAAA;EAAA;AAAA"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { An as sdk_gen_exports, jn as client } from "./sdk.gen-DObLA2c-.mjs";
|
|
2
|
-
import { getDefaultOrganization, getOrganization, setOrganizationConfig } from "./config.mjs";
|
|
2
|
+
import { extractTenantFromUrl, getDefaultOrganization, getOrganization, setOrganizationConfig } from "./config.mjs";
|
|
3
3
|
import { d as sdk_gen_exports$1, f as client$1 } from "./sdk.gen-DUNHa0uM.mjs";
|
|
4
4
|
import createDebug from "debug";
|
|
5
5
|
import * as openidClient from "openid-client";
|
|
@@ -83,8 +83,10 @@ var OptionsConfigStrategy = class extends ConfigStrategy {
|
|
|
83
83
|
if (!this.options) return null;
|
|
84
84
|
if (this.options.apiKey && (this.options.orgId || this.options.baseUrl)) {
|
|
85
85
|
const key = this.options.apiKey;
|
|
86
|
+
const baseUrl = this.options.baseUrl || `https://${this.options.orgId}.gatana.ai`;
|
|
86
87
|
return {
|
|
87
|
-
|
|
88
|
+
orgId: this.options.orgId,
|
|
89
|
+
baseUrl,
|
|
88
90
|
token: async () => key
|
|
89
91
|
};
|
|
90
92
|
}
|
|
@@ -97,6 +99,7 @@ var EnvConfigStrategy = class extends ConfigStrategy {
|
|
|
97
99
|
const orgId = process.env.GATANA_ORG_ID;
|
|
98
100
|
const overrideBaseUrl = process.env.GATANA_BASE_URL;
|
|
99
101
|
if (apiKey && (orgId || overrideBaseUrl)) return {
|
|
102
|
+
orgId: orgId || extractTenantFromUrl(overrideBaseUrl),
|
|
100
103
|
baseUrl: overrideBaseUrl || `https://${orgId}.gatana.ai`,
|
|
101
104
|
token: async () => apiKey
|
|
102
105
|
};
|
|
@@ -113,11 +116,13 @@ var FileConfigStrategy = class extends ConfigStrategy {
|
|
|
113
116
|
if (!orgId) return null;
|
|
114
117
|
debug(`FileConfigLoader loading config for orgId=${orgId}`);
|
|
115
118
|
const tenantConfig = getOrganization(orgId);
|
|
119
|
+
debug(tenantConfig);
|
|
116
120
|
if (!tenantConfig) return null;
|
|
117
121
|
return {
|
|
122
|
+
orgId,
|
|
118
123
|
baseUrl: tenantConfig.baseUrl,
|
|
119
124
|
token: async () => {
|
|
120
|
-
if (tenantConfig?.
|
|
125
|
+
if (tenantConfig?.pat) return tenantConfig.pat;
|
|
121
126
|
else if (tenantConfig?.tokens?.access_token && tenantConfig.tokens.expires_at && tenantConfig.tokens.expires_at > Math.floor(Date.now() / 1e3) + 60) return tenantConfig.tokens.access_token;
|
|
122
127
|
else if (tenantConfig.tokens?.refresh_token) {
|
|
123
128
|
debug("Access token expired or about to expire, attempting to refresh");
|
|
@@ -135,7 +140,7 @@ var FileConfigStrategy = class extends ConfigStrategy {
|
|
|
135
140
|
}
|
|
136
141
|
} catch (error) {
|
|
137
142
|
debug("Failed to refresh token", error);
|
|
138
|
-
throw new Error(
|
|
143
|
+
throw new Error(`Failed to refresh access token. Please log in again. (${error.message})`);
|
|
139
144
|
}
|
|
140
145
|
}
|
|
141
146
|
throw new Error("No valid API key, access token or refresh token available.");
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["debug","sdk","sdk"],"sources":["../src/v2.ts","../src/index.ts"],"sourcesContent":["import { client } from './apiv2/client.gen.js';\nimport * as sdk from './apiv2/sdk.gen.js';\nimport createDebug from 'debug';\nimport { Config } from './api/client/types.gen.js';\nimport {\n ConfigLoader,\n EnvConfigStrategy,\n FileConfigStrategy,\n GatanaConfig,\n GatanaOptions,\n OptionsConfigStrategy,\n} from './index.js';\nconst debug = createDebug('gatana');\n\n/**\n * Gatana2 is representing a more REST-ful API where the object returned from GET can be used directly in POST/PUT/PATCH requests.\n */\nexport class Gatana2 {\n public api = sdk;\n public readonly config: GatanaConfig;\n constructor(arg?: { options?: GatanaOptions; configLoader?: ConfigLoader; isCli?: boolean }) {\n // Try to get config from file if not provided via options or env vars\n const configLoader =\n arg?.configLoader ||\n new ConfigLoader([new OptionsConfigStrategy(arg?.options), new EnvConfigStrategy(), new FileConfigStrategy()]);\n\n try {\n this.config = configLoader.getConfig();\n\n const clientConfig = {\n baseUrl: new URL('/api/v2/', this.config.baseUrl).toString(),\n auth: this.config.token,\n } satisfies Config;\n client.setConfig(clientConfig);\n\n // Add verbose request/response logging via debug\n const debugHttp = createDebug('gatana:http');\n client.interceptors.request.use(request => {\n debugHttp(`→ ${request.method} ${request.url}`);\n return request;\n });\n client.interceptors.response.use((response, request) => {\n debugHttp(`← ${response.status} ${response.statusText} ${request.method} ${request.url}`);\n return response;\n });\n client.interceptors.error.use((error, response, request) => {\n debugHttp(`✗ ${response.status} ${response.statusText} ${request.method} ${request.url}`, error);\n return error;\n });\n\n debug('Gatana initialized with config', clientConfig);\n } catch (error: any) {\n debug('Failed to initialize', error);\n if (arg?.isCli && error.message === 'No valid configuration found from any strategy') {\n console.error('Warning: No valid configuration found. Run \"gatana config login\" to set up your credentials.');\n this.config = {\n baseUrl: 'https://NO_ORGANIZATION_SET.gatana.ai',\n token: async () => 'PLEASE_SET_UP_CONFIGURATION',\n };\n } else {\n console.error('Error initializing Gatana:', error.message);\n process.exit(1);\n }\n }\n }\n\n getOpenApiSpecUrl() {\n return new URL('/api/v2/openapi.json', this.config.baseUrl).toString();\n }\n}\n","import { client } from './api/client.gen.js';\nimport * as sdk from './api/sdk.gen.js';\nimport createDebug from 'debug';\nimport { getOrganization, getDefaultOrganization, setOrganizationConfig } from './config.js';\nimport * as openidClient from 'openid-client';\nimport { Config } from './api/client/types.gen.js';\nconst debug = createDebug('gatana');\n\nexport interface GatanaOptions {\n apiKey: string;\n orgId?: string;\n baseUrl?: string;\n}\n\nexport type GatanaConfig = { baseUrl: string; token: () => Promise<string> };\nexport class ConfigLoader {\n constructor(private strategies: ConfigStrategy[]) {}\n\n getConfig(): GatanaConfig {\n for (const strategy of this.strategies) {\n const config = strategy.getConfig();\n if (config) {\n return config;\n }\n }\n throw new Error('No valid configuration found from any strategy');\n }\n}\n\nexport abstract class ConfigStrategy {\n abstract getConfig(): { baseUrl: string; token: () => Promise<string> } | null;\n}\n\nexport class OptionsConfigStrategy extends ConfigStrategy {\n constructor(private options?: GatanaOptions) {\n super();\n }\n\n getConfig() {\n if (!this.options) {\n return null;\n }\n if (this.options.apiKey && (this.options.orgId || this.options.baseUrl)) {\n const key = this.options.apiKey;\n const baseUrl = this.options.baseUrl || `https://${this.options.orgId}.gatana.ai`;\n return {\n baseUrl,\n token: async () => key,\n };\n }\n return null;\n }\n}\n\nexport class EnvConfigStrategy extends ConfigStrategy {\n getConfig() {\n const apiKey = process.env.GATANA_API_KEY;\n const orgId = process.env.GATANA_ORG_ID;\n const overrideBaseUrl = process.env.GATANA_BASE_URL;\n if (apiKey && (orgId || overrideBaseUrl)) {\n return {\n baseUrl: overrideBaseUrl || `https://${orgId}.gatana.ai`,\n token: async () => apiKey,\n };\n }\n return null;\n }\n}\n\nexport class FileConfigStrategy extends ConfigStrategy {\n constructor(private orgId?: string) {\n super();\n }\n\n getConfig() {\n let orgId = this.orgId || process.env.GATANA_ORG_ID || getDefaultOrganization();\n if (!orgId) {\n return null;\n }\n debug(`FileConfigLoader loading config for orgId=${orgId}`);\n const tenantConfig = getOrganization(orgId);\n if (!tenantConfig) {\n return null;\n }\n return {\n baseUrl: tenantConfig.baseUrl,\n token: async () => {\n if (tenantConfig?.apiKey) {\n return tenantConfig.apiKey;\n } else if (\n tenantConfig?.tokens?.access_token &&\n tenantConfig.tokens.expires_at &&\n tenantConfig.tokens.expires_at > Math.floor(Date.now() / 1000) + 60\n ) {\n return tenantConfig.tokens.access_token;\n } else if (tenantConfig.tokens?.refresh_token) {\n debug('Access token expired or about to expire, attempting to refresh');\n // Try to refresh the token\n const config = await openidClient.discovery(\n new URL(`/.well-known/openid-configuration`, tenantConfig.baseUrl),\n `${orgId}-cli`\n );\n try {\n const token = await openidClient.refreshTokenGrant(config, tenantConfig.tokens.refresh_token);\n if (token) {\n setOrganizationConfig(orgId, {\n tokens: {\n access_token: token.access_token,\n refresh_token: token.refresh_token || tenantConfig.tokens.refresh_token,\n expires_at: token.expires_in ? Math.floor(Date.now() / 1000) + token.expires_in : 0,\n },\n });\n debug('Token refreshed successfully');\n return token.access_token;\n }\n } catch (error) {\n debug('Failed to refresh token', error);\n throw new Error('Failed to refresh access token. Please log in again.');\n }\n }\n throw new Error('No valid API key, access token or refresh token available.');\n },\n };\n }\n}\n\nexport class Gatana {\n public api = sdk;\n public config: GatanaConfig;\n constructor(arg?: { options?: GatanaOptions; configLoader?: ConfigLoader; isCli?: boolean }) {\n // Try to get config from file if not provided via options or env vars\n const configLoader =\n arg?.configLoader ||\n new ConfigLoader([new OptionsConfigStrategy(arg?.options), new EnvConfigStrategy(), new FileConfigStrategy()]);\n\n try {\n this.config = configLoader.getConfig();\n\n const clientConfig = {\n baseUrl: new URL('/api/v1/', this.config.baseUrl).toString(),\n auth: this.config.token,\n } satisfies Config;\n client.setConfig(clientConfig);\n\n // Add verbose request/response logging via debug\n const debugHttp = createDebug('gatana:http');\n client.interceptors.request.use(request => {\n debugHttp(`→ ${request.method} ${request.url}`);\n return request;\n });\n client.interceptors.response.use((response, request) => {\n debugHttp(`← ${response.status} ${response.statusText} ${request.method} ${request.url}`);\n return response;\n });\n client.interceptors.error.use((error, response, request) => {\n debugHttp(`✗ ${response.status} ${response.statusText} ${request.method} ${request.url}`, error);\n return error;\n });\n\n debug('Gatana initialized with config', clientConfig);\n } catch (error: any) {\n debug('Failed to initialize', error);\n if (arg?.isCli && error.message === 'No valid configuration found from any strategy') {\n console.error('Warning: No valid configuration found. Run \"gatana config login\" to set up your credentials.');\n this.config = {\n baseUrl: 'https://NO_ORGANIZATION_SET.gatana.ai',\n token: async () => 'PLEASE_SET_UP_CONFIGURATION',\n };\n } else {\n console.error('Error initializing Gatana:', error.message);\n process.exit(1);\n }\n }\n }\n}\n\nexport { Gatana2 } from './v2.js';\n"],"mappings":";;;;;;;AAYA,MAAMA,UAAQ,YAAY,SAAS;;;;AAKnC,IAAa,UAAb,MAAqB;CACnB,AAAO,MAAMC;CACb,AAAgB;CAChB,YAAY,KAAiF;EAE3F,MAAM,eACJ,KAAK,gBACL,IAAI,aAAa;GAAC,IAAI,sBAAsB,KAAK,QAAQ;GAAE,IAAI,mBAAmB;GAAE,IAAI,oBAAoB;GAAC,CAAC;AAEhH,MAAI;AACF,QAAK,SAAS,aAAa,WAAW;GAEtC,MAAM,eAAe;IACnB,SAAS,IAAI,IAAI,YAAY,KAAK,OAAO,QAAQ,CAAC,UAAU;IAC5D,MAAM,KAAK,OAAO;IACnB;AACD,YAAO,UAAU,aAAa;GAG9B,MAAM,YAAY,YAAY,cAAc;AAC5C,YAAO,aAAa,QAAQ,KAAI,YAAW;AACzC,cAAU,KA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AAE"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["debug","sdk","sdk"],"sources":["../src/v2.ts","../src/index.ts"],"sourcesContent":["import { client } from './apiv2/client.gen.js';\nimport * as sdk from './apiv2/sdk.gen.js';\nimport createDebug from 'debug';\nimport { Config } from './api/client/types.gen.js';\nimport {\n ConfigLoader,\n EnvConfigStrategy,\n FileConfigStrategy,\n GatanaConfig,\n GatanaOptions,\n OptionsConfigStrategy,\n} from './index.js';\nconst debug = createDebug('gatana');\n\n/**\n * Gatana2 is representing a more REST-ful API where the object returned from GET can be used directly in POST/PUT/PATCH requests.\n */\nexport class Gatana2 {\n public api = sdk;\n public readonly config: GatanaConfig;\n constructor(arg?: { options?: GatanaOptions; configLoader?: ConfigLoader; isCli?: boolean }) {\n // Try to get config from file if not provided via options or env vars\n const configLoader =\n arg?.configLoader ||\n new ConfigLoader([new OptionsConfigStrategy(arg?.options), new EnvConfigStrategy(), new FileConfigStrategy()]);\n\n try {\n this.config = configLoader.getConfig();\n\n const clientConfig = {\n baseUrl: new URL('/api/v2/', this.config.baseUrl).toString(),\n auth: this.config.token,\n } satisfies Config;\n client.setConfig(clientConfig);\n\n // Add verbose request/response logging via debug\n const debugHttp = createDebug('gatana:http');\n client.interceptors.request.use(request => {\n debugHttp(`→ ${request.method} ${request.url}`);\n return request;\n });\n client.interceptors.response.use((response, request) => {\n debugHttp(`← ${response.status} ${response.statusText} ${request.method} ${request.url}`);\n return response;\n });\n client.interceptors.error.use((error, response, request) => {\n debugHttp(`✗ ${response.status} ${response.statusText} ${request.method} ${request.url}`, error);\n return error;\n });\n\n debug('Gatana initialized with config', clientConfig);\n } catch (error: any) {\n debug('Failed to initialize', error);\n if (arg?.isCli && error.message === 'No valid configuration found from any strategy') {\n console.error('Warning: No valid configuration found. Run \"gatana config login\" to set up your credentials.');\n this.config = {\n baseUrl: 'https://NO_ORGANIZATION_SET.gatana.ai',\n token: async () => 'PLEASE_SET_UP_CONFIGURATION',\n };\n } else {\n console.error('Error initializing Gatana:', error.message);\n process.exit(1);\n }\n }\n }\n\n getOpenApiSpecUrl() {\n return new URL('/api/v2/openapi.json', this.config.baseUrl).toString();\n }\n}\n","import { client } from './api/client.gen.js';\nimport * as sdk from './api/sdk.gen.js';\nimport createDebug from 'debug';\nimport { getOrganization, getDefaultOrganization, setOrganizationConfig, extractTenantFromUrl } from './config.js';\nimport * as openidClient from 'openid-client';\nimport { Config } from './api/client/types.gen.js';\nconst debug = createDebug('gatana');\n\nexport interface GatanaOptions {\n apiKey: string;\n orgId?: string;\n baseUrl?: string;\n}\n\nexport type GatanaConfig = { baseUrl: string; token: () => Promise<string> };\nexport class ConfigLoader {\n constructor(private strategies: ConfigStrategy[]) {}\n\n getConfig(): GatanaConfig {\n for (const strategy of this.strategies) {\n const config = strategy.getConfig();\n if (config) {\n return config;\n }\n }\n throw new Error('No valid configuration found from any strategy');\n }\n}\n\nexport abstract class ConfigStrategy {\n abstract getConfig(): { orgId: string; baseUrl: string; token: () => Promise<string> } | null;\n}\n\nexport class OptionsConfigStrategy extends ConfigStrategy {\n constructor(private options?: GatanaOptions) {\n super();\n }\n\n getConfig() {\n if (!this.options) {\n return null;\n }\n if (this.options.apiKey && (this.options.orgId || this.options.baseUrl)) {\n const key = this.options.apiKey;\n const baseUrl = this.options.baseUrl || `https://${this.options.orgId}.gatana.ai`;\n return {\n orgId: this.options.orgId!,\n baseUrl,\n token: async () => key,\n };\n }\n return null;\n }\n}\n\nexport class EnvConfigStrategy extends ConfigStrategy {\n getConfig() {\n const apiKey = process.env.GATANA_API_KEY;\n const orgId = process.env.GATANA_ORG_ID;\n const overrideBaseUrl = process.env.GATANA_BASE_URL;\n if (apiKey && (orgId || overrideBaseUrl)) {\n return {\n orgId: orgId || extractTenantFromUrl(overrideBaseUrl!),\n baseUrl: overrideBaseUrl || `https://${orgId}.gatana.ai`,\n token: async () => apiKey,\n };\n }\n return null;\n }\n}\n\nexport class FileConfigStrategy extends ConfigStrategy {\n constructor(private orgId?: string) {\n super();\n }\n\n getConfig() {\n let orgId = this.orgId || process.env.GATANA_ORG_ID || getDefaultOrganization();\n if (!orgId) {\n return null;\n }\n debug(`FileConfigLoader loading config for orgId=${orgId}`);\n const tenantConfig = getOrganization(orgId);\n debug(tenantConfig);\n if (!tenantConfig) {\n return null;\n }\n return {\n orgId,\n baseUrl: tenantConfig.baseUrl,\n token: async () => {\n if (tenantConfig?.pat) {\n return tenantConfig.pat;\n } else if (\n tenantConfig?.tokens?.access_token &&\n tenantConfig.tokens.expires_at &&\n tenantConfig.tokens.expires_at > Math.floor(Date.now() / 1000) + 60\n ) {\n return tenantConfig.tokens.access_token;\n } else if (tenantConfig.tokens?.refresh_token) {\n debug('Access token expired or about to expire, attempting to refresh');\n // Try to refresh the token\n const config = await openidClient.discovery(\n new URL(`/.well-known/openid-configuration`, tenantConfig.baseUrl),\n `${orgId}-cli`\n );\n try {\n const token = await openidClient.refreshTokenGrant(config, tenantConfig.tokens.refresh_token);\n if (token) {\n setOrganizationConfig(orgId, {\n tokens: {\n access_token: token.access_token,\n refresh_token: token.refresh_token || tenantConfig.tokens.refresh_token,\n expires_at: token.expires_in ? Math.floor(Date.now() / 1000) + token.expires_in : 0,\n },\n });\n debug('Token refreshed successfully');\n return token.access_token;\n }\n } catch (error) {\n debug('Failed to refresh token', error);\n throw new Error(`Failed to refresh access token. Please log in again. (${(error as Error).message})`);\n }\n }\n throw new Error('No valid API key, access token or refresh token available.');\n },\n };\n }\n}\n\nexport class Gatana {\n public api = sdk;\n public config: GatanaConfig;\n constructor(arg?: { options?: GatanaOptions; configLoader?: ConfigLoader; isCli?: boolean }) {\n // Try to get config from file if not provided via options or env vars\n const configLoader =\n arg?.configLoader ||\n new ConfigLoader([new OptionsConfigStrategy(arg?.options), new EnvConfigStrategy(), new FileConfigStrategy()]);\n\n try {\n this.config = configLoader.getConfig();\n\n const clientConfig = {\n baseUrl: new URL('/api/v1/', this.config.baseUrl).toString(),\n auth: this.config.token,\n } satisfies Config;\n client.setConfig(clientConfig);\n\n // Add verbose request/response logging via debug\n const debugHttp = createDebug('gatana:http');\n client.interceptors.request.use(request => {\n debugHttp(`→ ${request.method} ${request.url}`);\n return request;\n });\n client.interceptors.response.use((response, request) => {\n debugHttp(`← ${response.status} ${response.statusText} ${request.method} ${request.url}`);\n return response;\n });\n client.interceptors.error.use((error, response, request) => {\n debugHttp(`✗ ${response.status} ${response.statusText} ${request.method} ${request.url}`, error);\n return error;\n });\n\n debug('Gatana initialized with config', clientConfig);\n } catch (error: any) {\n debug('Failed to initialize', error);\n if (arg?.isCli && error.message === 'No valid configuration found from any strategy') {\n console.error('Warning: No valid configuration found. Run \"gatana config login\" to set up your credentials.');\n this.config = {\n baseUrl: 'https://NO_ORGANIZATION_SET.gatana.ai',\n token: async () => 'PLEASE_SET_UP_CONFIGURATION',\n };\n } else {\n console.error('Error initializing Gatana:', error.message);\n process.exit(1);\n }\n }\n }\n}\n\nexport { Gatana2 } from './v2.js';\n"],"mappings":";;;;;;;AAYA,MAAMA,UAAQ,YAAY,SAAS;;;;AAKnC,IAAa,UAAb,MAAqB;CACnB,AAAO,MAAMC;CACb,AAAgB;CAChB,YAAY,KAAiF;EAE3F,MAAM,eACJ,KAAK,gBACL,IAAI,aAAa;GAAC,IAAI,sBAAsB,KAAK,QAAQ;GAAE,IAAI,mBAAmB;GAAE,IAAI,oBAAoB;GAAC,CAAC;AAEhH,MAAI;AACF,QAAK,SAAS,aAAa,WAAW;GAEtC,MAAM,eAAe;IACnB,SAAS,IAAI,IAAI,YAAY,KAAK,OAAO,QAAQ,CAAC,UAAU;IAC5D,MAAM,KAAK,OAAO;IACnB;AACD,YAAO,UAAU,aAAa;GAG9B,MAAM,YAAY,YAAY,cAAc;AAC5C,YAAO,aAAa,QAAQ,KAAI,YAAW;AACzC,cAAU,KAAK,QAAQ,OAAO,GAAG,QAAQ,MAAM;AAC/C,WAAO;KACP;AACF,YAAO,aAAa,SAAS,KAAK,UAAU,YAAY;AACtD,cAAU,KAAK,SAAS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