gatana-sdk 3.1.2 → 3.1.4

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/README.md ADDED
@@ -0,0 +1,142 @@
1
+ # gatana-sdk
2
+
3
+ [![npm](https://img.shields.io/npm/v/gatana-sdk)](https://www.npmjs.com/package/gatana-sdk)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+
6
+ JavaScript and TypeScript SDK for the [Gatana](https://gatana.ai) API. It is generated from the
7
+ backend's OpenAPI documents with [`@hey-api/openapi-ts`](https://heyapi.dev), and adds the loading
8
+ of the credentials.
9
+
10
+ ## Install
11
+
12
+ ```bash
13
+ npm install gatana-sdk
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ ```typescript
19
+ import { Gatana } from 'gatana-sdk';
20
+
21
+ // Reads GATANA_API_KEY and GATANA_ORG_ID, or ~/.gatana.config
22
+ const client = new Gatana();
23
+
24
+ const { data: servers } = await client.api.listMcpServers();
25
+ ```
26
+
27
+ Each operation of the API is a function on `client.api`. A call returns `{ data, request, response }`
28
+ and throws when the response is an error.
29
+
30
+ The SDK finds the credentials in the same way as the CLI. See
31
+ [Configuration](https://github.com/gatana-ai/gatana#configuration) for the environment variables and
32
+ the config file. Run `gatana config login` to write the config file. The SDK refreshes expired
33
+ OIDC tokens from that file and writes the new tokens back.
34
+
35
+ ### Custom authentication
36
+
37
+ Give the credentials in code:
38
+
39
+ ```typescript
40
+ import { Gatana } from 'gatana-sdk';
41
+
42
+ // An API key or a personal access token, and an organization ID or a base URL
43
+ const client = new Gatana({ options: { apiKey: 'gk_...', orgId: 'my-org' } });
44
+ ```
45
+
46
+ Give a token function, for example to read the token from a vault:
47
+
48
+ ```typescript
49
+ const client = new Gatana({
50
+ config: {
51
+ baseUrl: 'https://my-org.gatana.ai',
52
+ token: async () => readTokenFromVault(),
53
+ },
54
+ });
55
+ ```
56
+
57
+ Or select the strategies, in the order you want:
58
+
59
+ ```typescript
60
+ import { Gatana, ConfigLoader, EnvConfigStrategy, FileConfigStrategy } from 'gatana-sdk';
61
+
62
+ // Use the environment, then the "other-org" entry of ~/.gatana.config
63
+ const client = new Gatana({
64
+ configLoader: new ConfigLoader([new EnvConfigStrategy(), new FileConfigStrategy('other-org')]),
65
+ });
66
+ ```
67
+
68
+ ### V2 API client
69
+
70
+ `Gatana2` is the client for the v2 API at `/api/v2/`. In this API, you can send the object that a
71
+ GET returns back in a POST, PUT or PATCH request.
72
+
73
+ ```typescript
74
+ import { Gatana2 } from 'gatana-sdk';
75
+
76
+ const client = new Gatana2();
77
+ const { data: servers } = await client.api.listServersV2();
78
+ ```
79
+
80
+ `Gatana2` takes the same constructor arguments as `Gatana`.
81
+
82
+ ### Exports
83
+
84
+ | Export | Description |
85
+ | ----------------------- | ------------------------------------------------------------- |
86
+ | `Gatana` | V1 API client. Wraps the generated SDK at `/api/v1/` |
87
+ | `Gatana2` | V2 API client. REST-style API at `/api/v2/` |
88
+ | `ConfigLoader` | Tries the config strategies in order |
89
+ | `ConfigStrategy` | Abstract base class for a config strategy |
90
+ | `OptionsConfigStrategy` | Credentials from `{ apiKey, orgId }` or `{ apiKey, baseUrl }` |
91
+ | `EnvConfigStrategy` | Credentials from environment variables |
92
+ | `FileConfigStrategy` | Credentials from `~/.gatana.config`, with token refresh |
93
+
94
+ Subpath exports:
95
+
96
+ | Import | What it holds |
97
+ | ------------------- | ------------------------------------------------ |
98
+ | `gatana-sdk/api` | The generated v1 functions and types |
99
+ | `gatana-sdk/apiv2` | The generated v2 functions and types |
100
+ | `gatana-sdk/config` | Functions that read and write `~/.gatana.config` |
101
+
102
+ ### Debugging
103
+
104
+ The SDK logs with the [`debug`](https://www.npmjs.com/package/debug) package:
105
+
106
+ ```bash
107
+ # General debug output
108
+ DEBUG=gatana node your-script.js
109
+
110
+ # HTTP requests and responses
111
+ DEBUG=gatana:http node your-script.js
112
+ ```
113
+
114
+ ## Development
115
+
116
+ Run these from the repository root.
117
+
118
+ ```bash
119
+ just install # install the dependencies
120
+ just build # build the SDK into packages/gatana-js/dist
121
+ just dev # build again on each change
122
+ just generate # regenerate the v1 and v2 clients from the OpenAPI documents, then build
123
+ just pack # build and pack the SDK, without publishing
124
+ just fmt # format with prettier
125
+ ```
126
+
127
+ `gensdk.ts` runs the generator on the OpenAPI documents of `hello.gatana.ai`. To use a local
128
+ backend, set `OVERRIDE_OPENAPI_URL` to its v1 document, for example
129
+ `https://acme.local.gatana.ai/api/v1/openapi.json`. Do not edit the files in
130
+ `src/api/` and `src/apiv2/` by hand: `just generate` overwrites them. The hand-written code is in
131
+ `src/index.ts`, `src/v2.ts` and `src/config.ts`.
132
+
133
+ ### Releasing
134
+
135
+ `just release` in the repository root releases every package with changes, this one included. To
136
+ release only this package, run `just js-release`. It checks the npm login, asks for the version bump,
137
+ builds, publishes to npm, and tags `gatana-sdk@<version>`. The GitHub release is not marked as
138
+ latest, because `releases/latest` belongs to the CLI.
139
+
140
+ ## License
141
+
142
+ [MIT](https://github.com/gatana-ai/gatana/blob/main/LICENSE)
package/dist/config.d.cts CHANGED
@@ -7,6 +7,8 @@ interface OrganizationConfig {
7
7
  refresh_token: string;
8
8
  expires_at: number;
9
9
  };
10
+ /** The client the tokens were issued to, when the CLI signed in through the base domain (gatana.ai). Otherwise `<org>-cli`. */
11
+ clientId?: string;
10
12
  }
11
13
  type OrgId = string;
12
14
  interface GatanaFileConfig {
@@ -1 +1 @@
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"}
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;EAFA;EAKF,QAAA;AAAA;AAAA,KAGG,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
@@ -7,6 +7,8 @@ interface OrganizationConfig {
7
7
  refresh_token: string;
8
8
  expires_at: number;
9
9
  };
10
+ /** The client the tokens were issued to, when the CLI signed in through the base domain (gatana.ai). Otherwise `<org>-cli`. */
11
+ clientId?: string;
10
12
  }
11
13
  type OrgId = string;
12
14
  interface GatanaFileConfig {
@@ -1 +1 @@
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"}
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;EAFA;EAKF,QAAA;AAAA;AAAA,KAGG,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"}
@@ -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 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 ? remainingOrganizations[0] : undefined;\n }\n\n writeConfig(config);\n}\n"],"mappings":";;;;;;AAsBA,MAAM,mBAAmB,KAAK,SAAS,EAAE,iBAAiB;AAE1D,SAAgB,oBAA4B;AAC1C,QAAO;;AAGT,SAAgB,aAA+B;AAC7C,KAAI;AACF,MAAI,CAAC,WAAW,iBAAiB,CAC/B,QAAO,EAAE;EAGX,MAAM,aAAa,aAAa,kBAAkB,OAAO;AAGzD,SAFe,KAAK,MAAM,WAAW;UAG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jD,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"}
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 /** The client the tokens were issued to, when the CLI signed in through the base domain (gatana.ai). Otherwise `<org>-cli`. */\n clientId?: string;\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 ? remainingOrganizations[0] : undefined;\n }\n\n writeConfig(config);\n}\n"],"mappings":";;;;;;AAwBA,MAAM,mBAAmB,KAAK,SAAS,EAAE,iBAAiB;AAE1D,SAAgB,oBAA4B;AAC1C,QAAO;;AAGT,SAAgB,aAA+B;AAC7C,KAAI;AACF,MAAI,CAAC,WAAW,iBAAiB,CAC/B,QAAO,EAAE;EAGX,MAAM,aAAa,aAAa,kBAAkB,OAAO;AAGzD,SAFe,KAAK,MAAM,WAAW;UAG9B,OAAO;AAEd,SAAO,EAAE;;;AAIb,SAAgB,YAAY,QAAgC;AAC1D,KAAI;EAEF,MAAM,YAAY,SAAS;AAC3B,MAAI,CAAC,WAAW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uBAAuB,SAAS,IAAI,uBAAuB,KAAK;;AAGxF,aAAY,OAAO"}
package/dist/index.cjs CHANGED
@@ -133,7 +133,7 @@ var FileConfigStrategy = class extends ConfigStrategy {
133
133
  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;
134
134
  else if (tenantConfig.tokens?.refresh_token) {
135
135
  debug$1("Access token expired or about to expire, attempting to refresh");
136
- const config = await openid_client.discovery(new URL(`/.well-known/openid-configuration`, tenantConfig.baseUrl), `${orgId}-cli`);
136
+ const config = await openid_client.discovery(new URL(`/.well-known/openid-configuration`, tenantConfig.baseUrl), tenantConfig.clientId || `${orgId}-cli`);
137
137
  try {
138
138
  const token = await openid_client.refreshTokenGrant(config, tenantConfig.tokens.refresh_token);
139
139
  if (token) {
package/dist/index.mjs CHANGED
@@ -129,7 +129,7 @@ var FileConfigStrategy = class extends ConfigStrategy {
129
129
  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;
130
130
  else if (tenantConfig.tokens?.refresh_token) {
131
131
  debug("Access token expired or about to expire, attempting to refresh");
132
- const config = await openidClient.discovery(new URL(`/.well-known/openid-configuration`, tenantConfig.baseUrl), `${orgId}-cli`);
132
+ const config = await openidClient.discovery(new URL(`/.well-known/openid-configuration`, tenantConfig.baseUrl), tenantConfig.clientId || `${orgId}-cli`);
133
133
  try {
134
134
  const token = await openidClient.refreshTokenGrant(config, tenantConfig.tokens.refresh_token);
135
135
  if (token) {
@@ -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; config?: GatanaConfig; configLoader?: ConfigLoader; isCli?: boolean }) {\n // Try to get config from file if not provided via options or env vars\n const configLoader =\n (!arg?.config && arg?.configLoader) ||\n new ConfigLoader([new OptionsConfigStrategy(arg?.options), new EnvConfigStrategy(), new FileConfigStrategy()]);\n\n try {\n this.config = arg?.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}`, { body: request.body });\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 v2 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(public readonly 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 abstract help: string;\n}\n\nexport class OptionsConfigStrategy extends ConfigStrategy {\n constructor(private options?: GatanaOptions) {\n super();\n }\n\n public help =\n `OptionsConfigStrategy: Provide configuration via Gatana constructor options. ` +\n `Example: new Gatana({ options }) where options includes apiKey and orgId or baseUrl.`;\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 public help =\n `EnvConfigStrategy: Provide configuration via environment variables. ` +\n `Required: GATANA_API_KEY and either GATANA_ORG_ID or GATANA_BASE_URL. ` +\n `Example: export GATANA_API_KEY=your_api_key; export GATANA_ORG_ID=your_org_id`;\n\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 public help =\n `FileConfigStrategy: Provide configuration via a local config file. Use the ` +\n `\"gatana config\" commands to manage this configuration. The CLI will look for the default ` +\n `organization in the config file or use the org specified by GATANA_ORG_ID env var.`;\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 tenantConfig.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 setOrganizationConfig(orgId, {\n tokens: tenantConfig.tokens,\n });\n debug('Token refreshed successfully', { token });\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; config?: GatanaConfig; configLoader?: ConfigLoader; isCli?: boolean }) {\n // Try to get config from file if not provided via options or env vars\n const configLoader =\n (!arg?.config && arg?.configLoader) ||\n new ConfigLoader([new OptionsConfigStrategy(arg?.options), new EnvConfigStrategy(), new FileConfigStrategy()]);\n\n try {\n this.config = arg?.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}`, { body: request.body });\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 v1 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,KAAwG;EAElH,MAAM,eACH,CAAC,KAAK,UAAU,KAAK,gBACtB,IAAI,aAAa;GAAC,IAAI,sBAAsB,KAAK,QAAQ;GAAE,IAAI,mBAAmB;GAAE,IAAI,oBAAoB;GAAC,CAAC;AAEhH,MAAI;AACF,QAAK,SAAS,KAAK,UAAU,aAAa,WAAW;GAErD,MAAM,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AS,OAAO,GAAG,SAAS,WAAW,GAAG,QAAQ,OAAO,GAAG,QAAQ,MAAM;AACzF,WAAO;KACP;AACF,UAAO,aAAa,MAAM,KAAK,OAAO,UAAU,YAAY;AAC1D,cAAU,KAAK,SAAS,OAAO,GAAG,SAAS,WAAW,GAAG,QAAQ,OAAO,GAAG,QAAQ,OAAO,MAAM;AAChG,WAAO;KACP;AAEF,SAAM,qCAAqC,aAAa;WACjD,OAAY;AACnB,SAAM,wBAAwB,MAAM;AACpC,OAAI,KAAK,SAAS,MAAM,YAAY,kDAAkD;AACpF,YAAQ,MAAM,iGAA+F;AAC7G,SAAK,SAAS;KACZ,SAAS;KACT,OAAO,YAAY;KACpB;UACI;AACL,YAAQ,MAAM,8BAA8B,MAAM,QAAQ;AAC1D,YAAQ,KAAK,EAAE"}
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; config?: GatanaConfig; configLoader?: ConfigLoader; isCli?: boolean }) {\n // Try to get config from file if not provided via options or env vars\n const configLoader =\n (!arg?.config && arg?.configLoader) ||\n new ConfigLoader([new OptionsConfigStrategy(arg?.options), new EnvConfigStrategy(), new FileConfigStrategy()]);\n\n try {\n this.config = arg?.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}`, { body: request.body });\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 v2 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(public readonly 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 abstract help: string;\n}\n\nexport class OptionsConfigStrategy extends ConfigStrategy {\n constructor(private options?: GatanaOptions) {\n super();\n }\n\n public help =\n `OptionsConfigStrategy: Provide configuration via Gatana constructor options. ` +\n `Example: new Gatana({ options }) where options includes apiKey and orgId or baseUrl.`;\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 public help =\n `EnvConfigStrategy: Provide configuration via environment variables. ` +\n `Required: GATANA_API_KEY and either GATANA_ORG_ID or GATANA_BASE_URL. ` +\n `Example: export GATANA_API_KEY=your_api_key; export GATANA_ORG_ID=your_org_id`;\n\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 public help =\n `FileConfigStrategy: Provide configuration via a local config file. Use the ` +\n `\"gatana config\" commands to manage this configuration. The CLI will look for the default ` +\n `organization in the config file or use the org specified by GATANA_ORG_ID env var.`;\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 tenantConfig.clientId || `${orgId}-cli`\n );\n try {\n const token = await openidClient.refreshTokenGrant(config, tenantConfig.tokens.refresh_token);\n if (token) {\n tenantConfig.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 setOrganizationConfig(orgId, {\n tokens: tenantConfig.tokens,\n });\n debug('Token refreshed successfully', { token });\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; config?: GatanaConfig; configLoader?: ConfigLoader; isCli?: boolean }) {\n // Try to get config from file if not provided via options or env vars\n const configLoader =\n (!arg?.config && arg?.configLoader) ||\n new ConfigLoader([new OptionsConfigStrategy(arg?.options), new EnvConfigStrategy(), new FileConfigStrategy()]);\n\n try {\n this.config = arg?.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}`, { body: request.body });\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 v1 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,KAAwG;EAElH,MAAM,eACH,CAAC,KAAK,UAAU,KAAK,gBACtB,IAAI,aAAa;GAAC,IAAI,sBAAsB,KAAK,QAAQ;GAAE,IAAI,mBAAmB;GAAE,IAAI,oBAAoB;GAAC,CAAC;AAEhH,MAAI;AACF,QAAK,SAAS,KAAK,UAAU,aAAa,WAAW;GAErD,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,OAAO,EAAE,MAAM,QAAQ,MAAM,CAAC;AACvE,WAAO;KACP;AACF,YAAO,aAAa,SAAS,KAAK,UAAU,YAAY;AACtD,cAAU,KAAK,SAAS,OAAO,GAAG,SAAS,WAAW,GAAG,QAAQ,OAAO,GAAG,QAAQ,MAAM;AACzF,WAAO;KACP;AACF,YAAO,aAAa,MAAM,KAAK,OAAO,UAAU,YAAY;AAC1D,cAAU,KAAK,SAAS,OAAO,GAAG,SAA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.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "gatana-sdk",
3
- "version": "3.1.2",
3
+ "version": "3.1.4",
4
4
  "description": "Javascript/Typescript SDK for Gatana",
5
- "homepage": "https://github.com/gatana-ai/gatana-js#readme",
5
+ "homepage": "https://github.com/gatana-ai/gatana/tree/main/packages/gatana-js#readme",
6
6
  "bugs": {
7
- "url": "https://github.com/gatana-ai/gatana-js/issues"
7
+ "url": "https://github.com/gatana-ai/gatana/issues"
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "git+https://github.com/gatana-ai/gatana-js.git",
11
+ "url": "git+https://github.com/gatana-ai/gatana.git",
12
12
  "directory": "packages/gatana-js"
13
13
  },
14
14
  "license": "MIT",