@n8n_io/ai-assistant-sdk
Advanced tools
Comparing version 1.9.0 to 1.9.2
# Changelog | ||
## [1.9.2](https://github.com/n8n-io/ai-assistant-service/compare/ai-assistant-sdk-v1.9.1...ai-assistant-sdk-v1.9.2) (2024-08-14) | ||
### Bug Fixes | ||
* Fix up types for response ([#89](https://github.com/n8n-io/ai-assistant-service/issues/89)) ([3cad272](https://github.com/n8n-io/ai-assistant-service/commit/3cad272920e16bd27153485be2759fceffcb0273)) | ||
## [1.9.1](https://github.com/n8n-io/ai-assistant-service/compare/ai-assistant-sdk-v1.9.0...ai-assistant-sdk-v1.9.1) (2024-08-14) | ||
### Bug Fixes | ||
* Fix up types, use undici for request ([#87](https://github.com/n8n-io/ai-assistant-service/issues/87)) ([b7a001a](https://github.com/n8n-io/ai-assistant-service/commit/b7a001a5738006bdd280140d5cef33eeccdb7175)) | ||
## [1.9.0](https://github.com/n8n-io/ai-assistant-service/compare/ai-assistant-sdk-v1.8.2...ai-assistant-sdk-v1.9.0) (2024-08-14) | ||
@@ -4,0 +18,0 @@ |
@@ -0,1 +1,3 @@ | ||
import { Response } from 'undici'; | ||
type LogLevel = 'silent' | 'error' | 'warn' | 'info' | 'debug' | 'verbose'; | ||
@@ -2,0 +4,0 @@ interface User { |
@@ -30,3 +30,3 @@ "use strict"; | ||
name: "@n8n_io/ai-assistant-sdk", | ||
version: "1.9.0", | ||
version: "1.9.2", | ||
description: "n8n AI assistant SDK", | ||
@@ -59,2 +59,5 @@ author: "", | ||
typescript: "^5.5.3" | ||
}, | ||
dependencies: { | ||
undici: "^6.19.7" | ||
} | ||
@@ -64,2 +67,3 @@ }; | ||
// src/index.ts | ||
var import_undici = require("undici"); | ||
var DEFAULT_SERVICE_BASE_URL = "https://ai-assistant.n8n.io"; | ||
@@ -104,3 +108,7 @@ var AiAssistantClient = class { | ||
const response = await this.postRequest("/v1/chat/apply-suggestion", payload, user); | ||
return await response.json(); | ||
const data = await response.json(); | ||
if (isValidResponse(data)) { | ||
return data; | ||
} | ||
throw new Error("Invalid response from assistant service"); | ||
} | ||
@@ -118,3 +126,3 @@ getHeaders(user) { | ||
async refreshAuthToken() { | ||
const response = await fetch(`${this.baseUrl}/auth/token`, { | ||
const response = await (0, import_undici.fetch)(`${this.baseUrl}/auth/token`, { | ||
method: "POST", | ||
@@ -124,7 +132,7 @@ body: JSON.stringify({ licenseCert: this.licenseCert }), | ||
}); | ||
const { accessToken } = await response.json(); | ||
if (!accessToken && typeof accessToken === "string") { | ||
throw new Error("Could not retrieve access token"); | ||
const data = await response.json(); | ||
if (typeof data === "object" && data && "accessToken" in data && data.accessToken && typeof data.accessToken === "string") { | ||
this.activeToken = data.accessToken; | ||
} | ||
this.activeToken = accessToken; | ||
throw new Error("Could not retrieve access token"); | ||
} | ||
@@ -139,3 +147,3 @@ async postRequest(endpoint, payload, user) { | ||
const url = `${this.baseUrl}${endpoint}`; | ||
let response = await fetch(url, { | ||
let response = await (0, import_undici.fetch)(url, { | ||
headers: this.getHeaders(user), | ||
@@ -147,3 +155,3 @@ method: "POST", | ||
await this.refreshAuthToken(); | ||
response = await fetch(url, { | ||
response = await (0, import_undici.fetch)(url, { | ||
headers: this.getHeaders(user), | ||
@@ -156,3 +164,3 @@ method: "POST", | ||
this.debug(`API Error ${JSON.stringify(error)}`); | ||
const message = "message" in error && typeof error.message === "string" ? error.message : response.statusText; | ||
const message = typeof error === "object" && error && "message" in error && typeof error.message === "string" ? error.message : response.statusText; | ||
throw new Error(message); | ||
@@ -171,2 +179,5 @@ } | ||
} | ||
function isValidResponse(response) { | ||
return typeof response === "object" && !!response && "suggestionId" in response && "sessionId" in response; | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -173,0 +184,0 @@ 0 && (module.exports = { |
{ | ||
"name": "@n8n_io/ai-assistant-sdk", | ||
"version": "1.9.0", | ||
"version": "1.9.2", | ||
"description": "n8n AI assistant SDK", | ||
@@ -24,2 +24,5 @@ "author": "", | ||
}, | ||
"dependencies": { | ||
"undici": "^6.19.7" | ||
}, | ||
"scripts": { | ||
@@ -26,0 +29,0 @@ "typecheck": "tsc --noEmit", |
import packageJson from '../package.json'; | ||
import { fetch } from 'undici'; | ||
import type { Response } from 'undici'; | ||
@@ -69,6 +71,3 @@ // same as n8n | ||
async chat( | ||
payload: AiAssistantSDK.ChatRequestPayload, | ||
user: User, | ||
): Promise<Response> { | ||
async chat(payload: AiAssistantSDK.ChatRequestPayload, user: User): Promise<Response> { | ||
return await this.postRequest('/v1/chat', payload, user); | ||
@@ -86,3 +85,8 @@ } | ||
return await response.json(); | ||
const data = await response.json(); | ||
if (isValidResponse(data)) { | ||
return data; | ||
} | ||
throw new Error('Invalid response from assistant service'); | ||
} | ||
@@ -108,8 +112,14 @@ | ||
const { accessToken } = await response.json(); | ||
if (!accessToken && typeof accessToken === 'string') { | ||
throw new Error('Could not retrieve access token'); | ||
const data = await response.json(); | ||
if ( | ||
typeof data === 'object' && | ||
data && | ||
'accessToken' in data && | ||
data.accessToken && | ||
typeof data.accessToken === 'string' | ||
) { | ||
this.activeToken = data.accessToken; | ||
} | ||
this.activeToken = accessToken; | ||
throw new Error('Could not retrieve access token'); | ||
} | ||
@@ -146,3 +156,6 @@ | ||
const message = | ||
'message' in error && typeof error.message === 'string' | ||
typeof error === 'object' && | ||
error && | ||
'message' in error && | ||
typeof error.message === 'string' | ||
? error.message | ||
@@ -166,1 +179,10 @@ : response.statusText; | ||
} | ||
function isValidResponse(response: unknown): response is AiAssistantSDK.ApplySuggestionResponse { | ||
return ( | ||
typeof response === 'object' && | ||
!!response && | ||
'suggestionId' in response && | ||
'sessionId' in response | ||
); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
24517
556
3
1
+ Addedundici@^6.19.7
+ Addedundici@6.21.0(transitive)