@anthropic-ai/sdk 0.4.1 → 0.4.3

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.
@@ -6,6 +6,9 @@ export type SamplingParameters = {
6
6
  top_k?: number;
7
7
  top_p?: number;
8
8
  model: string;
9
+ tags?: {
10
+ [key: string]: string;
11
+ };
9
12
  };
10
13
  export type OnOpen = (response: Response) => void | Promise<void>;
11
14
  export type OnUpdate = (completion: CompletionResponse) => void | Promise<void>;
@@ -25,9 +28,12 @@ export declare class Client {
25
28
  constructor(apiKey: string, options?: {
26
29
  apiUrl?: string;
27
30
  });
28
- complete(params: SamplingParameters): Promise<CompletionResponse>;
29
- completeStream(params: SamplingParameters, { onOpen, onUpdate }: {
31
+ complete(params: SamplingParameters, options?: {
32
+ signal?: AbortSignal;
33
+ }): Promise<CompletionResponse>;
34
+ completeStream(params: SamplingParameters, { onOpen, onUpdate, signal, }: {
30
35
  onOpen?: OnOpen;
31
36
  onUpdate?: OnUpdate;
37
+ signal?: AbortSignal;
32
38
  }): Promise<CompletionResponse>;
33
39
  }
@@ -17,7 +17,7 @@ const fetch_event_source_1 = require("@fortaine/fetch-event-source");
17
17
  const cross_fetch_1 = __importDefault(require("cross-fetch"));
18
18
  exports.HUMAN_PROMPT = "\n\nHuman:";
19
19
  exports.AI_PROMPT = "\n\nAssistant:";
20
- const CLIENT_ID = "anthropic-typescript/0.4.1";
20
+ const CLIENT_ID = "anthropic-typescript/0.4.3";
21
21
  const DEFAULT_API_URL = "https://api.anthropic.com";
22
22
  var Event;
23
23
  (function (Event) {
@@ -30,7 +30,7 @@ class Client {
30
30
  this.apiKey = apiKey;
31
31
  this.apiUrl = (_a = options === null || options === void 0 ? void 0 : options.apiUrl) !== null && _a !== void 0 ? _a : DEFAULT_API_URL;
32
32
  }
33
- complete(params) {
33
+ complete(params, options) {
34
34
  return __awaiter(this, void 0, void 0, function* () {
35
35
  const response = yield (0, cross_fetch_1.default)(`${this.apiUrl}/v1/complete`, {
36
36
  method: "POST",
@@ -41,6 +41,7 @@ class Client {
41
41
  "X-API-Key": this.apiKey,
42
42
  },
43
43
  body: JSON.stringify(Object.assign(Object.assign({}, params), { stream: false })),
44
+ signal: options === null || options === void 0 ? void 0 : options.signal,
44
45
  });
45
46
  if (!response.ok) {
46
47
  const error = new Error(`Sampling error: ${response.status} ${response.statusText}`);
@@ -51,9 +52,16 @@ class Client {
51
52
  return completion;
52
53
  });
53
54
  }
54
- completeStream(params, { onOpen, onUpdate }) {
55
+ completeStream(params, { onOpen, onUpdate, signal, }) {
55
56
  const abortController = new AbortController();
56
57
  return new Promise((resolve, reject) => {
58
+ signal === null || signal === void 0 ? void 0 : signal.addEventListener("abort", (event) => {
59
+ abortController.abort(event);
60
+ reject({
61
+ name: "AbortError",
62
+ message: "Caller aborted completeStream",
63
+ });
64
+ });
57
65
  (0, fetch_event_source_1.fetchEventSource)(`${this.apiUrl}/v1/complete`, {
58
66
  method: "POST",
59
67
  headers: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anthropic-ai/sdk",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "description": "Library for accessing the Anthropic API",
5
5
  "repository": "https://github.com/anthropics/anthropic-sdk-typescript",
6
6
  "license": "MIT",
@@ -15,7 +15,8 @@
15
15
  "lint": "eslint src && prettier --check .",
16
16
  "format": "prettier --write .",
17
17
  "example:basic_stream": "tsc --build && node ./build/examples/basic_stream.js",
18
- "example:basic_sync": "tsc --build && node ./build/examples/basic_sync.js"
18
+ "example:basic_sync": "tsc --build && node ./build/examples/basic_sync.js",
19
+ "example:cancellation": "tsc --build && node ./build/examples/cancellation.js"
19
20
  },
20
21
  "dependencies": {
21
22
  "@fortaine/fetch-event-source": "^3.0.6",