@patternzones/koine-sdk 1.1.6 → 2.0.0
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 +33 -23
- package/dist/client.d.ts +57 -34
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +38 -278
- package/dist/client.js.map +1 -1
- package/dist/errors.d.ts +18 -3
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +13 -0
- package/dist/errors.js.map +1 -1
- package/dist/http.d.ts +29 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +103 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +8 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -9
- package/dist/index.js.map +1 -1
- package/dist/object.d.ts +55 -0
- package/dist/object.d.ts.map +1 -0
- package/dist/object.js +65 -0
- package/dist/object.js.map +1 -0
- package/dist/stream/index.d.ts +37 -0
- package/dist/stream/index.d.ts.map +1 -0
- package/dist/stream/index.js +170 -0
- package/dist/stream/index.js.map +1 -0
- package/dist/stream/sse.d.ts +9 -0
- package/dist/stream/sse.d.ts.map +1 -0
- package/dist/stream/sse.js +59 -0
- package/dist/stream/sse.js.map +1 -0
- package/dist/text.d.ts +36 -0
- package/dist/text.d.ts.map +1 -0
- package/dist/text.js +48 -0
- package/dist/text.js.map +1 -0
- package/dist/types.d.ts +26 -26
- package/dist/types.d.ts.map +1 -1
- package/package.json +6 -5
package/dist/http.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KNOWN_ERROR_CODES = void 0;
|
|
4
|
+
exports.toErrorCode = toErrorCode;
|
|
5
|
+
exports.validateConfig = validateConfig;
|
|
6
|
+
exports.createAbortSignal = createAbortSignal;
|
|
7
|
+
exports.safeFetch = safeFetch;
|
|
8
|
+
exports.safeJsonParse = safeJsonParse;
|
|
9
|
+
const errors_js_1 = require("./errors.js");
|
|
10
|
+
/**
|
|
11
|
+
* Known error codes for type-safe validation.
|
|
12
|
+
*/
|
|
13
|
+
exports.KNOWN_ERROR_CODES = new Set([
|
|
14
|
+
// SDK-generated errors
|
|
15
|
+
"HTTP_ERROR",
|
|
16
|
+
"INVALID_RESPONSE",
|
|
17
|
+
"INVALID_CONFIG",
|
|
18
|
+
"VALIDATION_ERROR",
|
|
19
|
+
"STREAM_ERROR",
|
|
20
|
+
"SSE_PARSE_ERROR",
|
|
21
|
+
"NO_SESSION",
|
|
22
|
+
"NO_USAGE",
|
|
23
|
+
"NO_RESPONSE_BODY",
|
|
24
|
+
"TIMEOUT",
|
|
25
|
+
"NETWORK_ERROR",
|
|
26
|
+
// Gateway-returned errors
|
|
27
|
+
"INVALID_PARAMS",
|
|
28
|
+
"AUTH_ERROR",
|
|
29
|
+
"UNAUTHORIZED",
|
|
30
|
+
"SERVER_ERROR",
|
|
31
|
+
"SCHEMA_ERROR",
|
|
32
|
+
"RATE_LIMITED",
|
|
33
|
+
"CONTEXT_OVERFLOW",
|
|
34
|
+
]);
|
|
35
|
+
/**
|
|
36
|
+
* Coerces an API error code to a known KoineErrorCode.
|
|
37
|
+
* Falls back to the provided default if the code is unknown.
|
|
38
|
+
*/
|
|
39
|
+
function toErrorCode(code, fallback) {
|
|
40
|
+
if (code && exports.KNOWN_ERROR_CODES.has(code)) {
|
|
41
|
+
return code;
|
|
42
|
+
}
|
|
43
|
+
return fallback;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Validates config parameters before making requests.
|
|
47
|
+
* @throws {KoineError} with code 'INVALID_CONFIG' if config is invalid
|
|
48
|
+
*/
|
|
49
|
+
function validateConfig(config) {
|
|
50
|
+
if (!config.baseUrl) {
|
|
51
|
+
throw new errors_js_1.KoineError("baseUrl is required", "INVALID_CONFIG");
|
|
52
|
+
}
|
|
53
|
+
if (!config.authKey) {
|
|
54
|
+
throw new errors_js_1.KoineError("authKey is required", "INVALID_CONFIG");
|
|
55
|
+
}
|
|
56
|
+
if (typeof config.timeout !== "number" || config.timeout <= 0) {
|
|
57
|
+
throw new errors_js_1.KoineError("timeout must be a positive number", "INVALID_CONFIG");
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Creates an AbortSignal that combines timeout with optional user signal.
|
|
62
|
+
*/
|
|
63
|
+
function createAbortSignal(timeout, userSignal) {
|
|
64
|
+
const timeoutSignal = AbortSignal.timeout(timeout);
|
|
65
|
+
if (!userSignal) {
|
|
66
|
+
return timeoutSignal;
|
|
67
|
+
}
|
|
68
|
+
// Combine signals - abort when either triggers
|
|
69
|
+
return AbortSignal.any([timeoutSignal, userSignal]);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Wraps fetch errors in KoineError for consistent error handling.
|
|
73
|
+
*/
|
|
74
|
+
async function safeFetch(url, options, timeout) {
|
|
75
|
+
try {
|
|
76
|
+
return await fetch(url, options);
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
if (error instanceof DOMException && error.name === "AbortError") {
|
|
80
|
+
// Check if it was a timeout or user cancellation
|
|
81
|
+
throw new errors_js_1.KoineError(`Request aborted (timeout: ${timeout}ms)`, "TIMEOUT");
|
|
82
|
+
}
|
|
83
|
+
if (error instanceof TypeError) {
|
|
84
|
+
// Network errors (DNS failure, connection refused, etc.)
|
|
85
|
+
throw new errors_js_1.KoineError(`Network error: ${error.message}`, "NETWORK_ERROR");
|
|
86
|
+
}
|
|
87
|
+
// Unknown error - wrap it
|
|
88
|
+
throw new errors_js_1.KoineError(`Request failed: ${error instanceof Error ? error.message : String(error)}`, "NETWORK_ERROR");
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Safely parses JSON from a response, handling non-JSON bodies gracefully.
|
|
93
|
+
*/
|
|
94
|
+
async function safeJsonParse(response) {
|
|
95
|
+
const text = await response.text();
|
|
96
|
+
try {
|
|
97
|
+
return JSON.parse(text);
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=http.js.map
|
package/dist/http.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.js","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":";;;AAiCA,kCAQC;AAMD,wCAUC;AAKD,8CAUC;AAKD,8BAyBC;AAKD,sCAOC;AAlHD,2CAA8D;AAG9D;;GAEG;AACU,QAAA,iBAAiB,GAAG,IAAI,GAAG,CAAiB;IACxD,uBAAuB;IACvB,YAAY;IACZ,kBAAkB;IAClB,gBAAgB;IAChB,kBAAkB;IAClB,cAAc;IACd,iBAAiB;IACjB,YAAY;IACZ,UAAU;IACV,kBAAkB;IAClB,SAAS;IACT,eAAe;IACf,0BAA0B;IAC1B,gBAAgB;IAChB,YAAY;IACZ,cAAc;IACd,cAAc;IACd,cAAc;IACd,cAAc;IACd,kBAAkB;CAClB,CAAC,CAAC;AAEH;;;GAGG;AACH,SAAgB,WAAW,CAC1B,IAAwB,EACxB,QAAwB;IAExB,IAAI,IAAI,IAAI,yBAAiB,CAAC,GAAG,CAAC,IAAsB,CAAC,EAAE,CAAC;QAC3D,OAAO,IAAsB,CAAC;IAC/B,CAAC;IACD,OAAO,QAAQ,CAAC;AACjB,CAAC;AAED;;;GAGG;AACH,SAAgB,cAAc,CAAC,MAAmB;IACjD,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QACrB,MAAM,IAAI,sBAAU,CAAC,qBAAqB,EAAE,gBAAgB,CAAC,CAAC;IAC/D,CAAC;IACD,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QACrB,MAAM,IAAI,sBAAU,CAAC,qBAAqB,EAAE,gBAAgB,CAAC,CAAC;IAC/D,CAAC;IACD,IAAI,OAAO,MAAM,CAAC,OAAO,KAAK,QAAQ,IAAI,MAAM,CAAC,OAAO,IAAI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AAI,EAAE,CAAC;IACnC,IAAI,CAAC;QACJ,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAM,CAAC;IAC9B,CAAC;IAAC,MAAM,CAAC;QACR,OAAO,IAAI,CAAC;IACb,CAAC;AACF,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -5,23 +5,26 @@
|
|
|
5
5
|
*
|
|
6
6
|
* @example
|
|
7
7
|
* ```typescript
|
|
8
|
-
* import {
|
|
8
|
+
* import { createKoine } from '@patternzones/koine-sdk';
|
|
9
9
|
*
|
|
10
|
-
* const
|
|
10
|
+
* const koine = createKoine({
|
|
11
11
|
* baseUrl: 'http://localhost:3100',
|
|
12
12
|
* timeout: 300000,
|
|
13
13
|
* authKey: 'your-api-key',
|
|
14
14
|
* model: 'sonnet',
|
|
15
|
-
* };
|
|
15
|
+
* });
|
|
16
16
|
*
|
|
17
|
-
* const result = await generateText(
|
|
17
|
+
* const result = await koine.generateText({
|
|
18
18
|
* prompt: 'Hello, how are you?',
|
|
19
19
|
* });
|
|
20
20
|
*
|
|
21
21
|
* console.log(result.text);
|
|
22
22
|
* ```
|
|
23
23
|
*/
|
|
24
|
-
export type { KoineConfig, KoineUsage, KoineStreamResult
|
|
24
|
+
export type { KoineConfig, KoineUsage, KoineStreamResult } from "./types.js";
|
|
25
25
|
export { KoineError } from "./errors.js";
|
|
26
|
+
export type { KoineErrorCode } from "./errors.js";
|
|
27
|
+
export { createKoine } from "./client.js";
|
|
28
|
+
export type { KoineClient, GenerateTextOptions, GenerateTextResult, StreamTextOptions, GenerateObjectOptions, GenerateObjectResult, } from "./client.js";
|
|
26
29
|
export { generateText, streamText, generateObject } from "./client.js";
|
|
27
30
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAGH,YAAY,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAGH,YAAY,EAAE,WAAW,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAG7E,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,YAAY,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAGlD,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,YAAY,EACX,WAAW,EACX,mBAAmB,EACnB,kBAAkB,EAClB,iBAAiB,EACjB,qBAAqB,EACrB,oBAAoB,GACpB,MAAM,aAAa,CAAC;AAGrB,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -6,16 +6,16 @@
|
|
|
6
6
|
*
|
|
7
7
|
* @example
|
|
8
8
|
* ```typescript
|
|
9
|
-
* import {
|
|
9
|
+
* import { createKoine } from '@patternzones/koine-sdk';
|
|
10
10
|
*
|
|
11
|
-
* const
|
|
11
|
+
* const koine = createKoine({
|
|
12
12
|
* baseUrl: 'http://localhost:3100',
|
|
13
13
|
* timeout: 300000,
|
|
14
14
|
* authKey: 'your-api-key',
|
|
15
15
|
* model: 'sonnet',
|
|
16
|
-
* };
|
|
16
|
+
* });
|
|
17
17
|
*
|
|
18
|
-
* const result = await generateText(
|
|
18
|
+
* const result = await koine.generateText({
|
|
19
19
|
* prompt: 'Hello, how are you?',
|
|
20
20
|
* });
|
|
21
21
|
*
|
|
@@ -23,13 +23,16 @@
|
|
|
23
23
|
* ```
|
|
24
24
|
*/
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.generateObject = exports.streamText = exports.generateText = exports.KoineError = void 0;
|
|
26
|
+
exports.generateObject = exports.streamText = exports.generateText = exports.createKoine = exports.KoineError = void 0;
|
|
27
27
|
// Errors
|
|
28
28
|
var errors_js_1 = require("./errors.js");
|
|
29
29
|
Object.defineProperty(exports, "KoineError", { enumerable: true, get: function () { return errors_js_1.KoineError; } });
|
|
30
|
-
// Client
|
|
30
|
+
// Client factory (recommended API)
|
|
31
31
|
var client_js_1 = require("./client.js");
|
|
32
|
-
Object.defineProperty(exports, "
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
Object.defineProperty(exports, "createKoine", { enumerable: true, get: function () { return client_js_1.createKoine; } });
|
|
33
|
+
// Standalone functions (legacy API - still supported)
|
|
34
|
+
var client_js_2 = require("./client.js");
|
|
35
|
+
Object.defineProperty(exports, "generateText", { enumerable: true, get: function () { return client_js_2.generateText; } });
|
|
36
|
+
Object.defineProperty(exports, "streamText", { enumerable: true, get: function () { return client_js_2.streamText; } });
|
|
37
|
+
Object.defineProperty(exports, "generateObject", { enumerable: true, get: function () { return client_js_2.generateObject; } });
|
|
35
38
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;;;AAKH,SAAS;AACT,yCAAyC;AAAhC,uGAAA,UAAU,OAAA;AAGnB,mCAAmC;AACnC,yCAA0C;AAAjC,wGAAA,WAAW,OAAA;AAUpB,sDAAsD;AACtD,yCAAuE;AAA9D,yGAAA,YAAY,OAAA;AAAE,uGAAA,UAAU,OAAA;AAAE,2GAAA,cAAc,OAAA"}
|
package/dist/object.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
import type { KoineConfig, KoineUsage } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Generates structured JSON response from Koine gateway service.
|
|
5
|
+
* Converts the provided Zod schema to JSON Schema format for the gateway.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam T - The type of the expected response object, inferred from schema
|
|
8
|
+
* @param config - Client configuration including baseUrl, authKey, and timeout
|
|
9
|
+
* @param options - Request options
|
|
10
|
+
* @param options.prompt - The user prompt describing what to extract
|
|
11
|
+
* @param options.schema - Zod schema defining the expected response structure
|
|
12
|
+
* @param options.system - Optional system prompt for context
|
|
13
|
+
* @param options.sessionId - Optional session ID to continue a conversation
|
|
14
|
+
* @param options.signal - Optional AbortSignal for cancellation
|
|
15
|
+
* @returns Object containing parsed and validated response, raw text, usage, and sessionId
|
|
16
|
+
* @throws {KoineError} With code 'VALIDATION_ERROR' if response doesn't match schema
|
|
17
|
+
* @throws {KoineError} With code 'HTTP_ERROR' for network/authentication failures
|
|
18
|
+
*/
|
|
19
|
+
export declare function generateObject<T>(config: KoineConfig, options: {
|
|
20
|
+
system?: string;
|
|
21
|
+
prompt: string;
|
|
22
|
+
schema: z.ZodSchema<T>;
|
|
23
|
+
sessionId?: string;
|
|
24
|
+
signal?: AbortSignal;
|
|
25
|
+
}): Promise<{
|
|
26
|
+
object: T;
|
|
27
|
+
rawText: string;
|
|
28
|
+
usage: KoineUsage;
|
|
29
|
+
sessionId: string;
|
|
30
|
+
}>;
|
|
31
|
+
/**
|
|
32
|
+
* Request options for structured object generation.
|
|
33
|
+
*/
|
|
34
|
+
export interface GenerateObjectOptions<T> {
|
|
35
|
+
/** The user prompt describing what to extract */
|
|
36
|
+
prompt: string;
|
|
37
|
+
/** Zod schema defining the expected response structure */
|
|
38
|
+
schema: z.ZodSchema<T>;
|
|
39
|
+
/** Optional system prompt for context */
|
|
40
|
+
system?: string;
|
|
41
|
+
/** Optional session ID to continue a conversation */
|
|
42
|
+
sessionId?: string;
|
|
43
|
+
/** Optional AbortSignal for cancellation */
|
|
44
|
+
signal?: AbortSignal;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Structured object generation result.
|
|
48
|
+
*/
|
|
49
|
+
export interface GenerateObjectResult<T> {
|
|
50
|
+
readonly object: T;
|
|
51
|
+
readonly rawText: string;
|
|
52
|
+
readonly usage: KoineUsage;
|
|
53
|
+
readonly sessionId: string;
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=object.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAU7B,OAAO,KAAK,EAGX,WAAW,EACX,UAAU,EACV,MAAM,YAAY,CAAC;AAEpB;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,cAAc,CAAC,CAAC,EACrC,MAAM,EAAE,WAAW,EACnB,OAAO,EAAE;IACR,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;IACvB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,WAAW,CAAC;CACrB,GACC,OAAO,CAAC;IACV,MAAM,EAAE,CAAC,CAAC;IACV,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,UAAU,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;CAClB,CAAC,CA8DD;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB,CAAC,CAAC;IACvC,iDAAiD;IACjD,MAAM,EAAE,MAAM,CAAC;IACf,0DAA0D;IAC1D,MAAM,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;IACvB,yCAAyC;IACzC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,qDAAqD;IACrD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,4CAA4C;IAC5C,MAAM,CAAC,EAAE,WAAW,CAAC;CACrB;AAED;;GAEG;AACH,MAAM,WAAW,oBAAoB,CAAC,CAAC;IACtC,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC;IACnB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC;IAC3B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC3B"}
|
package/dist/object.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateObject = generateObject;
|
|
4
|
+
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
5
|
+
const errors_js_1 = require("./errors.js");
|
|
6
|
+
const http_js_1 = require("./http.js");
|
|
7
|
+
/**
|
|
8
|
+
* Generates structured JSON response from Koine gateway service.
|
|
9
|
+
* Converts the provided Zod schema to JSON Schema format for the gateway.
|
|
10
|
+
*
|
|
11
|
+
* @typeParam T - The type of the expected response object, inferred from schema
|
|
12
|
+
* @param config - Client configuration including baseUrl, authKey, and timeout
|
|
13
|
+
* @param options - Request options
|
|
14
|
+
* @param options.prompt - The user prompt describing what to extract
|
|
15
|
+
* @param options.schema - Zod schema defining the expected response structure
|
|
16
|
+
* @param options.system - Optional system prompt for context
|
|
17
|
+
* @param options.sessionId - Optional session ID to continue a conversation
|
|
18
|
+
* @param options.signal - Optional AbortSignal for cancellation
|
|
19
|
+
* @returns Object containing parsed and validated response, raw text, usage, and sessionId
|
|
20
|
+
* @throws {KoineError} With code 'VALIDATION_ERROR' if response doesn't match schema
|
|
21
|
+
* @throws {KoineError} With code 'HTTP_ERROR' for network/authentication failures
|
|
22
|
+
*/
|
|
23
|
+
async function generateObject(config, options) {
|
|
24
|
+
(0, http_js_1.validateConfig)(config);
|
|
25
|
+
// Convert Zod schema to JSON Schema for the gateway service
|
|
26
|
+
const jsonSchema = (0, zod_to_json_schema_1.zodToJsonSchema)(options.schema, {
|
|
27
|
+
$refStrategy: "none",
|
|
28
|
+
target: "jsonSchema7",
|
|
29
|
+
});
|
|
30
|
+
const response = await (0, http_js_1.safeFetch)(`${config.baseUrl}/generate-object`, {
|
|
31
|
+
method: "POST",
|
|
32
|
+
headers: {
|
|
33
|
+
"Content-Type": "application/json",
|
|
34
|
+
Authorization: `Bearer ${config.authKey}`,
|
|
35
|
+
},
|
|
36
|
+
body: JSON.stringify({
|
|
37
|
+
system: options.system,
|
|
38
|
+
prompt: options.prompt,
|
|
39
|
+
schema: jsonSchema,
|
|
40
|
+
sessionId: options.sessionId,
|
|
41
|
+
model: config.model,
|
|
42
|
+
}),
|
|
43
|
+
signal: (0, http_js_1.createAbortSignal)(config.timeout, options.signal),
|
|
44
|
+
}, config.timeout);
|
|
45
|
+
if (!response.ok) {
|
|
46
|
+
const errorBody = await (0, http_js_1.safeJsonParse)(response);
|
|
47
|
+
throw new errors_js_1.KoineError(errorBody?.error || `HTTP ${response.status} ${response.statusText}`, (0, http_js_1.toErrorCode)(errorBody?.code, "HTTP_ERROR"), errorBody?.rawText);
|
|
48
|
+
}
|
|
49
|
+
const result = await (0, http_js_1.safeJsonParse)(response);
|
|
50
|
+
if (!result) {
|
|
51
|
+
throw new errors_js_1.KoineError("Invalid response from Koine gateway: expected JSON", "INVALID_RESPONSE");
|
|
52
|
+
}
|
|
53
|
+
// Validate the response against the Zod schema
|
|
54
|
+
const parseResult = options.schema.safeParse(result.object);
|
|
55
|
+
if (!parseResult.success) {
|
|
56
|
+
throw new errors_js_1.KoineError(`Response validation failed: ${parseResult.error.message}`, "VALIDATION_ERROR", result.rawText);
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
object: parseResult.data,
|
|
60
|
+
rawText: result.rawText,
|
|
61
|
+
usage: result.usage,
|
|
62
|
+
sessionId: result.sessionId,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=object.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"object.js","s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AAS,EAAE,OAAO,CAClB,CAAC;IACH,CAAC;IAED,MAAM,MAAM,GAAG,MAAM,IAAA,uBAAa,EAAyB,QAAQ,CAAC,CAAC;IACrE,IAAI,CAAC,MAAM,EAAE,CAAC;QACb,MAAM,IAAI,sBAAU,CACnB,oDAAoD,EACpD,kBAAkB,CAClB,CAAC;IACH,CAAC;IAED,+CAA+C;IAC/C,MAAM,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAC5D,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,CAAC;QAC1B,MAAM,IAAI,sBAAU,CACnB,+BAA+B,WAAW,CAAC,KAAK,CAAC,OAAO,EAAE,EAC1D,kBAAkB,EAClB,MAAM,CAAC,OAAO,CACd,CAAC;IACH,CAAC;IAED,OAAO;QACN,MAAM,EAAE,WAAW,CAAC,IAAI;QACxB,OAAO,EAAE,MAAM,CAAC,OAAO;QACvB,KAAK,EAAE,MAAM,CAAC,KAAK;QACnB,SAAS,EAAE,MAAM,CAAC,SAAS;KAC3B,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { KoineConfig, KoineStreamResult } from "../types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Streams text response from Koine gateway service.
|
|
4
|
+
*
|
|
5
|
+
* @param config - Client configuration including baseUrl, authKey, and timeout
|
|
6
|
+
* @param options - Request options
|
|
7
|
+
* @param options.prompt - The user prompt to send
|
|
8
|
+
* @param options.system - Optional system prompt for context
|
|
9
|
+
* @param options.sessionId - Optional session ID to continue a conversation
|
|
10
|
+
* @param options.signal - Optional AbortSignal for cancellation
|
|
11
|
+
* @returns KoineStreamResult containing:
|
|
12
|
+
* - textStream: ReadableStream of text chunks (async iterable)
|
|
13
|
+
* - sessionId: Promise that resolves early when session event arrives
|
|
14
|
+
* - usage: Promise that resolves when stream completes
|
|
15
|
+
* - text: Promise containing full accumulated text
|
|
16
|
+
* @throws {KoineError} When connection fails or stream encounters an error
|
|
17
|
+
*/
|
|
18
|
+
export declare function streamText(config: KoineConfig, options: {
|
|
19
|
+
system?: string;
|
|
20
|
+
prompt: string;
|
|
21
|
+
sessionId?: string;
|
|
22
|
+
signal?: AbortSignal;
|
|
23
|
+
}): Promise<KoineStreamResult>;
|
|
24
|
+
/**
|
|
25
|
+
* Request options for streaming text generation.
|
|
26
|
+
*/
|
|
27
|
+
export interface StreamTextOptions {
|
|
28
|
+
/** The user prompt to send */
|
|
29
|
+
prompt: string;
|
|
30
|
+
/** Optional system prompt for context */
|
|
31
|
+
system?: string;
|
|
32
|
+
/** Optional session ID to continue a conversation */
|
|
33
|
+
sessionId?: string;
|
|
34
|
+
/** Optional AbortSignal for cancellation */
|
|
35
|
+
signal?: AbortSignal;
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/stream/index.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAEX,WAAW,EACX,iBAAiB,EAKjB,MAAM,aAAa,CAAC;AAGrB;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,UAAU,CAC/B,MAAM,EAAE,WAAW,EACnB,OAAO,EAAE;IACR,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,WAAW,CAAC;CACrB,GACC,OAAO,CAAC,iBAAiB,CAAC,CA2L5B;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IACjC,8BAA8B;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,yCAAyC;IACzC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,qDAAqD;IACrD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,4CAA4C;IAC5C,MAAM,CAAC,EAAE,WAAW,CAAC;CACrB"}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.streamText = streamText;
|
|
4
|
+
const errors_js_1 = require("../errors.js");
|
|
5
|
+
const http_js_1 = require("../http.js");
|
|
6
|
+
const sse_js_1 = require("./sse.js");
|
|
7
|
+
/**
|
|
8
|
+
* Streams text response from Koine gateway service.
|
|
9
|
+
*
|
|
10
|
+
* @param config - Client configuration including baseUrl, authKey, and timeout
|
|
11
|
+
* @param options - Request options
|
|
12
|
+
* @param options.prompt - The user prompt to send
|
|
13
|
+
* @param options.system - Optional system prompt for context
|
|
14
|
+
* @param options.sessionId - Optional session ID to continue a conversation
|
|
15
|
+
* @param options.signal - Optional AbortSignal for cancellation
|
|
16
|
+
* @returns KoineStreamResult containing:
|
|
17
|
+
* - textStream: ReadableStream of text chunks (async iterable)
|
|
18
|
+
* - sessionId: Promise that resolves early when session event arrives
|
|
19
|
+
* - usage: Promise that resolves when stream completes
|
|
20
|
+
* - text: Promise containing full accumulated text
|
|
21
|
+
* @throws {KoineError} When connection fails or stream encounters an error
|
|
22
|
+
*/
|
|
23
|
+
async function streamText(config, options) {
|
|
24
|
+
(0, http_js_1.validateConfig)(config);
|
|
25
|
+
const response = await (0, http_js_1.safeFetch)(`${config.baseUrl}/stream`, {
|
|
26
|
+
method: "POST",
|
|
27
|
+
headers: {
|
|
28
|
+
"Content-Type": "application/json",
|
|
29
|
+
Authorization: `Bearer ${config.authKey}`,
|
|
30
|
+
},
|
|
31
|
+
body: JSON.stringify({
|
|
32
|
+
system: options.system,
|
|
33
|
+
prompt: options.prompt,
|
|
34
|
+
sessionId: options.sessionId,
|
|
35
|
+
model: config.model,
|
|
36
|
+
}),
|
|
37
|
+
signal: (0, http_js_1.createAbortSignal)(config.timeout, options.signal),
|
|
38
|
+
}, config.timeout);
|
|
39
|
+
if (!response.ok) {
|
|
40
|
+
const errorBody = await (0, http_js_1.safeJsonParse)(response);
|
|
41
|
+
throw new errors_js_1.KoineError(errorBody?.error || `HTTP ${response.status} ${response.statusText}`, (0, http_js_1.toErrorCode)(errorBody?.code, "HTTP_ERROR"), errorBody?.rawText);
|
|
42
|
+
}
|
|
43
|
+
if (!response.body) {
|
|
44
|
+
throw new errors_js_1.KoineError("No response body from Koine gateway", "NO_RESPONSE_BODY");
|
|
45
|
+
}
|
|
46
|
+
// Set up promises for session, usage, and accumulated text
|
|
47
|
+
let resolveSessionId;
|
|
48
|
+
let rejectSessionId;
|
|
49
|
+
const sessionIdPromise = new Promise((resolve, reject) => {
|
|
50
|
+
resolveSessionId = resolve;
|
|
51
|
+
rejectSessionId = reject;
|
|
52
|
+
});
|
|
53
|
+
let resolveUsage;
|
|
54
|
+
let rejectUsage;
|
|
55
|
+
const usagePromise = new Promise((resolve, reject) => {
|
|
56
|
+
resolveUsage = resolve;
|
|
57
|
+
rejectUsage = reject;
|
|
58
|
+
});
|
|
59
|
+
let resolveText;
|
|
60
|
+
let rejectText;
|
|
61
|
+
const textPromise = new Promise((resolve, reject) => {
|
|
62
|
+
resolveText = resolve;
|
|
63
|
+
rejectText = reject;
|
|
64
|
+
});
|
|
65
|
+
let accumulatedText = "";
|
|
66
|
+
let sessionIdReceived = false;
|
|
67
|
+
let usageReceived = false;
|
|
68
|
+
let textResolved = false;
|
|
69
|
+
// Transform SSE events into text chunks
|
|
70
|
+
const textStream = response.body.pipeThrough((0, sse_js_1.createSSEParser)()).pipeThrough(new TransformStream({
|
|
71
|
+
transform(sseEvent, controller) {
|
|
72
|
+
// Critical events (session, result, error, done) must propagate parse errors
|
|
73
|
+
// Text events can log and continue - degraded content is better than total failure
|
|
74
|
+
const isCriticalEvent = ["session", "result", "error", "done"].includes(sseEvent.event);
|
|
75
|
+
try {
|
|
76
|
+
switch (sseEvent.event) {
|
|
77
|
+
case "session": {
|
|
78
|
+
const parsed = JSON.parse(sseEvent.data);
|
|
79
|
+
if (!sessionIdReceived) {
|
|
80
|
+
sessionIdReceived = true;
|
|
81
|
+
resolveSessionId(parsed.sessionId);
|
|
82
|
+
}
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
case "text": {
|
|
86
|
+
const parsed = JSON.parse(sseEvent.data);
|
|
87
|
+
accumulatedText += parsed.text;
|
|
88
|
+
controller.enqueue(parsed.text);
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
case "result": {
|
|
92
|
+
const parsed = JSON.parse(sseEvent.data);
|
|
93
|
+
usageReceived = true;
|
|
94
|
+
resolveUsage(parsed.usage);
|
|
95
|
+
if (!sessionIdReceived) {
|
|
96
|
+
sessionIdReceived = true;
|
|
97
|
+
resolveSessionId(parsed.sessionId);
|
|
98
|
+
}
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
case "error": {
|
|
102
|
+
const parsed = JSON.parse(sseEvent.data);
|
|
103
|
+
const error = new errors_js_1.KoineError(parsed.error, (0, http_js_1.toErrorCode)(parsed.code, "STREAM_ERROR"));
|
|
104
|
+
usageReceived = true; // Prevent double rejection in flush
|
|
105
|
+
rejectUsage(error);
|
|
106
|
+
rejectText(error);
|
|
107
|
+
if (!sessionIdReceived) {
|
|
108
|
+
rejectSessionId(error);
|
|
109
|
+
}
|
|
110
|
+
controller.error(error);
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
case "done": {
|
|
114
|
+
// Stream complete, resolve the text promise
|
|
115
|
+
if (!textResolved) {
|
|
116
|
+
textResolved = true;
|
|
117
|
+
resolveText(accumulatedText);
|
|
118
|
+
}
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
catch (parseError) {
|
|
124
|
+
const parseErrorMessage = parseError instanceof Error
|
|
125
|
+
? parseError.message
|
|
126
|
+
: String(parseError);
|
|
127
|
+
if (isCriticalEvent) {
|
|
128
|
+
// Critical event parse failure - propagate error
|
|
129
|
+
const error = new errors_js_1.KoineError(`Failed to parse critical SSE event '${sseEvent.event}': ${parseErrorMessage}`, "SSE_PARSE_ERROR", sseEvent.data);
|
|
130
|
+
if (!usageReceived) {
|
|
131
|
+
usageReceived = true;
|
|
132
|
+
rejectUsage(error);
|
|
133
|
+
}
|
|
134
|
+
if (!textResolved) {
|
|
135
|
+
textResolved = true;
|
|
136
|
+
rejectText(error);
|
|
137
|
+
}
|
|
138
|
+
if (!sessionIdReceived) {
|
|
139
|
+
rejectSessionId(error);
|
|
140
|
+
}
|
|
141
|
+
controller.error(error);
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
// Non-critical event (text) - log warning but continue stream
|
|
145
|
+
// Degraded content is better than total failure
|
|
146
|
+
console.warn(`[Koine SDK] Failed to parse SSE text event: ${parseErrorMessage}. Raw data: ${sseEvent.data?.substring(0, 100)}`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
flush() {
|
|
151
|
+
// Handle promises that were never resolved/rejected during stream
|
|
152
|
+
if (!sessionIdReceived) {
|
|
153
|
+
rejectSessionId(new errors_js_1.KoineError("Stream ended without session ID", "NO_SESSION"));
|
|
154
|
+
}
|
|
155
|
+
if (!usageReceived) {
|
|
156
|
+
rejectUsage(new errors_js_1.KoineError("Stream ended without usage information", "NO_USAGE"));
|
|
157
|
+
}
|
|
158
|
+
if (!textResolved) {
|
|
159
|
+
resolveText(accumulatedText);
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
}));
|
|
163
|
+
return {
|
|
164
|
+
textStream,
|
|
165
|
+
sessionId: sessionIdPromise,
|
|
166
|
+
usage: usagePromise,
|
|
167
|
+
text: textPromise,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/stream/index.ts"],"names":[],"mappings":";;AAmCA,gCAmMC;AAtOD,4CAA0C;AAC1C,wCAMoB;AAUpB,qCAA2C;AAE3C;;;;;;;;;;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QACD,KAAK;YACJ,kEAAkE;YAClE,IAAI,CAAC,iBAAiB,EAAE,CAAC;gBACxB,eAAe,CACd,IAAI,sBAAU,CAAC,iCAAiC,EAAE,YAAY,CAAC,CAC/D,CAAC;YACH,CAAC;YACD,IAAI,CAAC,aAAa,EAAE,CAAC;gBACpB,WAAW,CACV,IAAI,sBAAU,CACb,wCAAwC,EACxC,UAAU,CACV,CACD,CAAC;YACH,CAAC;YACD,IAAI,CAAC,YAAY,EAAE,CAAC;gBACnB,WAAW,CAAC,eAAe,CAAC,CAAC;YAC9B,CAAC;QACF,CAAC;KACD,CAAC,CACF,CAAC;IAEF,OAAO;QACN,UAAU;QACV,SAAS,EAAE,gBAAgB;QAC3B,KAAK,EAAE,YAAY;QACnB,IAAI,EAAE,WAAW;KACjB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sse.d.ts","sourceRoot":"","sources":["../../src/stream/sse.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,wBAAgB,eAAe,IAAI,eAAe,CACjD,UAAU,EACV;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAC/B,CAuDA"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createSSEParser = createSSEParser;
|
|
4
|
+
/**
|
|
5
|
+
* Parses SSE events from a ReadableStream.
|
|
6
|
+
* SSE format: "event: name\ndata: {...}\n\n"
|
|
7
|
+
*/
|
|
8
|
+
function createSSEParser() {
|
|
9
|
+
let buffer = "";
|
|
10
|
+
// Reuse decoder with stream mode to correctly handle multi-byte UTF-8 chars spanning chunks
|
|
11
|
+
const decoder = new TextDecoder();
|
|
12
|
+
return new TransformStream({
|
|
13
|
+
transform(chunk, controller) {
|
|
14
|
+
buffer += decoder.decode(chunk, { stream: true });
|
|
15
|
+
// SSE events are separated by double newlines
|
|
16
|
+
const events = buffer.split("\n\n");
|
|
17
|
+
// Keep the last potentially incomplete event in the buffer
|
|
18
|
+
buffer = events.pop() || "";
|
|
19
|
+
for (const eventStr of events) {
|
|
20
|
+
if (!eventStr.trim())
|
|
21
|
+
continue;
|
|
22
|
+
const lines = eventStr.split("\n");
|
|
23
|
+
let eventType = "";
|
|
24
|
+
let data = "";
|
|
25
|
+
for (const line of lines) {
|
|
26
|
+
if (line.startsWith("event: ")) {
|
|
27
|
+
eventType = line.slice(7);
|
|
28
|
+
}
|
|
29
|
+
else if (line.startsWith("data: ")) {
|
|
30
|
+
data = line.slice(6);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (eventType && data) {
|
|
34
|
+
controller.enqueue({ event: eventType, data });
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
flush(controller) {
|
|
39
|
+
// Process any remaining data in buffer
|
|
40
|
+
if (buffer.trim()) {
|
|
41
|
+
const lines = buffer.split("\n");
|
|
42
|
+
let eventType = "";
|
|
43
|
+
let data = "";
|
|
44
|
+
for (const line of lines) {
|
|
45
|
+
if (line.startsWith("event: ")) {
|
|
46
|
+
eventType = line.slice(7);
|
|
47
|
+
}
|
|
48
|
+
else if (line.startsWith("data: ")) {
|
|
49
|
+
data = line.slice(6);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (eventType && data) {
|
|
53
|
+
controller.enqueue({ event: eventType, data });
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=sse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sse.js","sourceRoot":"","sources":["../../src/stream/sse.ts"],"names":[],"mappings":";;AAIA,0CA0DC;AA9DD;;;GAGG;AACH,SAAgB,eAAe;IAI9B,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,4FAA4F;IAC5F,MAAM,OAAO,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