@bedrockio/ai 0.3.0 → 0.4.2
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/.claude/settings.local.json +11 -0
- package/CHANGELOG.md +21 -0
- package/README.md +58 -17
- package/__mocks__/@anthropic-ai/sdk.js +16 -22
- package/__mocks__/@google/generative-ai.js +1 -1
- package/__mocks__/openai.js +33 -28
- package/dist/cjs/BaseClient.js +242 -182
- package/dist/cjs/anthropic.js +115 -93
- package/dist/cjs/google.js +74 -80
- package/dist/cjs/index.js +23 -75
- package/dist/cjs/openai.js +114 -72
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/utils/code.js +11 -0
- package/dist/cjs/utils/json.js +53 -0
- package/dist/cjs/utils/templates.js +83 -0
- package/dist/cjs/xai.js +11 -20
- package/dist/esm/BaseClient.js +243 -0
- package/dist/esm/anthropic.js +116 -0
- package/dist/esm/google.js +75 -0
- package/dist/esm/index.js +25 -0
- package/dist/esm/openai.js +113 -0
- package/dist/esm/utils/code.js +8 -0
- package/dist/esm/utils/json.js +50 -0
- package/dist/esm/utils/templates.js +76 -0
- package/dist/esm/xai.js +10 -0
- package/eslint.config.js +2 -0
- package/package.json +18 -17
- package/src/BaseClient.js +233 -140
- package/src/anthropic.js +96 -56
- package/src/google.js +3 -6
- package/src/index.js +6 -54
- package/src/openai.js +96 -33
- package/src/utils/code.js +9 -0
- package/src/utils/json.js +58 -0
- package/src/utils/templates.js +87 -0
- package/src/xai.js +2 -9
- package/tsconfig.cjs.json +8 -0
- package/tsconfig.esm.json +8 -0
- package/tsconfig.types.json +9 -0
- package/types/BaseClient.d.ts +67 -26
- package/types/BaseClient.d.ts.map +1 -1
- package/types/anthropic.d.ts +26 -2
- package/types/anthropic.d.ts.map +1 -1
- package/types/google.d.ts.map +1 -1
- package/types/index.d.ts +4 -11
- package/types/index.d.ts.map +1 -1
- package/types/openai.d.ts +45 -2
- package/types/openai.d.ts.map +1 -1
- package/types/utils/code.d.ts +2 -0
- package/types/utils/code.d.ts.map +1 -0
- package/types/utils/json.d.ts +2 -0
- package/types/utils/json.d.ts.map +1 -0
- package/types/utils/templates.d.ts +3 -0
- package/types/utils/templates.d.ts.map +1 -0
- package/types/utils.d.ts +4 -0
- package/types/utils.d.ts.map +1 -0
- package/types/xai.d.ts.map +1 -1
- package/vitest.config.js +10 -0
- package/dist/cjs/util.js +0 -62
- package/src/util.js +0 -60
package/types/BaseClient.d.ts
CHANGED
|
@@ -3,38 +3,79 @@ export default class BaseClient {
|
|
|
3
3
|
options: any;
|
|
4
4
|
templates: any;
|
|
5
5
|
/**
|
|
6
|
-
* Interpolates vars into the provided template and
|
|
7
|
-
*
|
|
8
|
-
* be omitted and will default to `"text"`.
|
|
9
|
-
* {@link https://github.com/bedrockio/ai?tab=readme-ov-file#bedrockioai Documentation}
|
|
6
|
+
* Interpolates vars into the provided template as instructions and runs the
|
|
7
|
+
* prompt.
|
|
10
8
|
*
|
|
11
|
-
* @param {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
* @param {Object.<string, any>} [options.other] - Additional props
|
|
15
|
-
* will be interpolated in the template.
|
|
16
|
-
*/
|
|
17
|
-
prompt(options: {
|
|
18
|
-
model: string;
|
|
19
|
-
output?: "raw" | "text" | "json" | "messages";
|
|
20
|
-
other?: {
|
|
21
|
-
[x: string]: any;
|
|
22
|
-
};
|
|
23
|
-
}): Promise<void>;
|
|
9
|
+
* @param {PromptOptions} options
|
|
10
|
+
*/
|
|
11
|
+
prompt(options: PromptOptions): Promise<any>;
|
|
24
12
|
/**
|
|
25
13
|
* Streams the prompt response.
|
|
14
|
+
*
|
|
15
|
+
* @param {PromptOptions & StreamOptions} options
|
|
26
16
|
* @returns {AsyncIterator}
|
|
27
17
|
*/
|
|
28
|
-
stream(options:
|
|
29
|
-
getMessages(options: any): Promise<{
|
|
30
|
-
role: any;
|
|
31
|
-
content: any;
|
|
32
|
-
}[]>;
|
|
18
|
+
stream(options: PromptOptions & StreamOptions): AsyncIterator<any, any, any>;
|
|
33
19
|
buildTemplate(options: any): Promise<any>;
|
|
34
|
-
|
|
20
|
+
runPrompt(options: any): void;
|
|
21
|
+
runStream(options: any): void;
|
|
22
|
+
getTextResponse(response: any): void;
|
|
23
|
+
/**
|
|
24
|
+
* @returns {Object}
|
|
25
|
+
*/
|
|
26
|
+
getStructuredResponse(response: any): any;
|
|
27
|
+
/**
|
|
28
|
+
* @returns {Object}
|
|
29
|
+
*/
|
|
30
|
+
getMessagesResponse(input: any, response: any): any;
|
|
31
|
+
/**
|
|
32
|
+
* @returns {Object}
|
|
33
|
+
*/
|
|
34
|
+
normalizeStreamEvent(event: any): any;
|
|
35
|
+
normalizeOptions(options: any): Promise<any>;
|
|
36
|
+
normalizeInput(options: any): any;
|
|
37
|
+
normalizeSchema(options: any): any;
|
|
38
|
+
getMessageExtractor(options: any): (event: any) => any;
|
|
39
|
+
debug(message: any, arg: any): void;
|
|
40
|
+
resolveInstructions(options: any): Promise<any>;
|
|
35
41
|
resolveTemplate(options: any): Promise<any>;
|
|
36
|
-
|
|
37
|
-
getCompletion(options: any): void;
|
|
38
|
-
getStreamedChunk(chunk: any, started: any): void;
|
|
42
|
+
loadTemplates(): Promise<void>;
|
|
39
43
|
}
|
|
44
|
+
export type PromptOptions = {
|
|
45
|
+
/**
|
|
46
|
+
* - Input to use.
|
|
47
|
+
*/
|
|
48
|
+
input: string | PromptMessage[];
|
|
49
|
+
/**
|
|
50
|
+
* - The model to use.
|
|
51
|
+
*/
|
|
52
|
+
model?: string;
|
|
53
|
+
/**
|
|
54
|
+
* - Stream response.
|
|
55
|
+
*/
|
|
56
|
+
stream: boolean;
|
|
57
|
+
/**
|
|
58
|
+
* - A JSON schema compatible object that defines the output shape.
|
|
59
|
+
*/
|
|
60
|
+
schema?: any;
|
|
61
|
+
/**
|
|
62
|
+
* - The return value type.
|
|
63
|
+
*/
|
|
64
|
+
output?: "raw" | "text" | "json" | "messages";
|
|
65
|
+
/**
|
|
66
|
+
* - Params to be interpolated into the template.
|
|
67
|
+
* May also be passed as additional props to options.
|
|
68
|
+
*/
|
|
69
|
+
params?: any;
|
|
70
|
+
};
|
|
71
|
+
export type StreamOptions = {
|
|
72
|
+
/**
|
|
73
|
+
* - Key in JSON response to extract a message stream from.
|
|
74
|
+
*/
|
|
75
|
+
extractMessages?: string;
|
|
76
|
+
};
|
|
77
|
+
export type PromptMessage = {
|
|
78
|
+
role: "system" | "user" | "assistant";
|
|
79
|
+
content: string;
|
|
80
|
+
};
|
|
40
81
|
//# sourceMappingURL=BaseClient.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseClient.d.ts","sourceRoot":"","sources":["../src/BaseClient.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"BaseClient.d.ts","sourceRoot":"","sources":["../src/BaseClient.js"],"names":[],"mappings":"AAIA;IACE,0BAOC;IANC,aAIC;IACD,eAAqB;IAKvB;;;;;OAKG;IACH,gBAFW,aAAa,gBAuCvB;IAED;;;;;OAKG;IACH,gBAHW,aAAa,GAAG,aAAa,gCAsDvC;IAED,0CAGC;IAID,8BAGC;IAED,8BAGC;IAED,qCAGC;IAED;;OAEG;IACH,0CAGC;IAED;;OAEG;IACH,oDAGC;IAED;;OAEG;IACH,sCAGC;IAID,6CAaC;IAED,kCAiBC;IAED,mCAuBC;IAED,uDAWC;IAED,oCAMC;IAED,gDAKC;IAED,4CAIC;IAED,+BAGC;CACF;;;;;WAIa,MAAM,GAAC,aAAa,EAAE;;;;YACtB,MAAM;;;;YACN,OAAO;;;;;;;;aAEP,KAAK,GAAG,MAAM,GAAG,MAAM,GAAG,UAAU;;;;;;;;;;;sBAOpC,MAAM;;;UAKN,QAAQ,GAAG,MAAM,GAAG,WAAW;aAC/B,MAAM"}
|
package/types/anthropic.d.ts
CHANGED
|
@@ -1,15 +1,39 @@
|
|
|
1
1
|
export class AnthropicClient extends BaseClient {
|
|
2
|
+
static DEFAULT_MODEL: string;
|
|
2
3
|
client: Anthropic;
|
|
3
4
|
/**
|
|
4
5
|
* Lists available models.
|
|
5
6
|
* {@link https://docs.anthropic.com/en/docs/about-claude/models Documentation}
|
|
6
7
|
*/
|
|
7
8
|
models(): Promise<string[]>;
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
runPrompt(options: any): Promise<Anthropic.Messages.Message & {
|
|
10
|
+
_request_id?: string | null;
|
|
11
|
+
} & import("@anthropic-ai/sdk/core/streaming.js").Stream<Anthropic.Messages.RawMessageStreamEvent>>;
|
|
12
|
+
runStream(options: any): Promise<Anthropic.Messages.Message & {
|
|
13
|
+
_request_id?: string | null;
|
|
14
|
+
} & import("@anthropic-ai/sdk/core/streaming.js").Stream<Anthropic.Messages.RawMessageStreamEvent>>;
|
|
15
|
+
getTextResponse(response: any): any;
|
|
16
|
+
getMessagesResponse(input: any, response: any): {
|
|
17
|
+
messages: any[];
|
|
18
|
+
};
|
|
19
|
+
normalizeStreamEvent(event: any): {
|
|
20
|
+
type: string;
|
|
21
|
+
text?: undefined;
|
|
22
|
+
} | {
|
|
10
23
|
type: string;
|
|
11
24
|
text: any;
|
|
12
25
|
};
|
|
26
|
+
getSchemaOptions(options: any): {
|
|
27
|
+
tools: {
|
|
28
|
+
name: string;
|
|
29
|
+
description: string;
|
|
30
|
+
input_schema: any;
|
|
31
|
+
}[];
|
|
32
|
+
tool_choice: {
|
|
33
|
+
type: string;
|
|
34
|
+
name: string;
|
|
35
|
+
};
|
|
36
|
+
};
|
|
13
37
|
}
|
|
14
38
|
import BaseClient from './BaseClient.js';
|
|
15
39
|
import Anthropic from '@anthropic-ai/sdk';
|
package/types/anthropic.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../src/anthropic.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../src/anthropic.js"],"names":[],"mappings":"AAMA;IACE,6BAA2C;IAIzC,kBAAoC;IAGtC;;;OAGG;IACH,4BAGC;IAED;;wGAoBC;IAED;;wGAMC;IAED,oCAKC;IASD;;MAgBC;IAED;;;;;;MAgBC;IAID;;;;;;;;;;MA8BC;CACF;uBAtIsB,iBAAiB;sBAFlB,mBAAmB"}
|
package/types/google.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"google.d.ts","sourceRoot":"","sources":["../src/google.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"google.d.ts","sourceRoot":"","sources":["../src/google.js"],"names":[],"mappings":"AAMA;IAII,2BAA4C;IAG9C;;;OAGG;IACH,4BAOC;IAED,0CAkCC;IACD,sCAKC;IAED;;;MAkBC;CACF;uBArFsB,iBAAiB;mCAFL,uBAAuB"}
|
package/types/index.d.ts
CHANGED
|
@@ -1,12 +1,5 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
constructor(options: any);
|
|
6
|
-
clients: {};
|
|
7
|
-
prompt(options: any): any;
|
|
8
|
-
stream(options: any): any;
|
|
9
|
-
buildTemplate(options: any): any;
|
|
10
|
-
getClient(options: any): any;
|
|
11
|
-
}
|
|
1
|
+
export function createClient(options?: {}): AnthropicClient | GoogleClient | OpenAiClient;
|
|
2
|
+
import { AnthropicClient } from './anthropic.js';
|
|
3
|
+
import { GoogleClient } from './google.js';
|
|
4
|
+
import { OpenAiClient } from './openai.js';
|
|
12
5
|
//# sourceMappingURL=index.d.ts.map
|
package/types/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":"AAKA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":"AAKA,0FAkBC;gCAvB+B,gBAAgB;6BACnB,aAAa;6BACb,aAAa"}
|
package/types/openai.d.ts
CHANGED
|
@@ -1,14 +1,57 @@
|
|
|
1
1
|
export class OpenAiClient extends BaseClient {
|
|
2
|
+
static DEFAULT_MODEL: string;
|
|
2
3
|
client: OpenAI;
|
|
3
4
|
/**
|
|
4
5
|
* Lists available models.
|
|
5
6
|
* {@link https://platform.openai.com/docs/models Documentation}
|
|
6
7
|
*/
|
|
7
8
|
models(): Promise<string[]>;
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
runPrompt(options: any): Promise<OpenAI.Responses.Response & {
|
|
10
|
+
_request_id?: string | null;
|
|
11
|
+
} & import("openai/core/streaming.js").Stream<OpenAI.Responses.ResponseStreamEvent>>;
|
|
12
|
+
runStream(options: any): Promise<OpenAI.Responses.Response & {
|
|
13
|
+
_request_id?: string | null;
|
|
14
|
+
} & import("openai/core/streaming.js").Stream<OpenAI.Responses.ResponseStreamEvent>>;
|
|
15
|
+
getTextResponse(response: any): any;
|
|
16
|
+
getMessagesResponse(input: any, response: any): {
|
|
17
|
+
messages: any[];
|
|
18
|
+
prevResponseId: any;
|
|
19
|
+
};
|
|
20
|
+
getOutputFormat(options: any): {
|
|
21
|
+
type: string;
|
|
22
|
+
name?: undefined;
|
|
23
|
+
strict?: undefined;
|
|
24
|
+
schema?: undefined;
|
|
25
|
+
} | {
|
|
26
|
+
type: string;
|
|
27
|
+
name: string;
|
|
28
|
+
strict: boolean;
|
|
29
|
+
schema: any;
|
|
30
|
+
};
|
|
31
|
+
normalizeStreamEvent(event: any): {
|
|
32
|
+
type: string;
|
|
33
|
+
id: any;
|
|
34
|
+
usage?: undefined;
|
|
35
|
+
delta?: undefined;
|
|
36
|
+
text?: undefined;
|
|
37
|
+
} | {
|
|
38
|
+
type: string;
|
|
39
|
+
id: any;
|
|
40
|
+
usage: any;
|
|
41
|
+
delta?: undefined;
|
|
42
|
+
text?: undefined;
|
|
43
|
+
} | {
|
|
44
|
+
type: string;
|
|
45
|
+
delta: any;
|
|
46
|
+
id?: undefined;
|
|
47
|
+
usage?: undefined;
|
|
48
|
+
text?: undefined;
|
|
49
|
+
} | {
|
|
10
50
|
type: string;
|
|
11
51
|
text: any;
|
|
52
|
+
id?: undefined;
|
|
53
|
+
usage?: undefined;
|
|
54
|
+
delta?: undefined;
|
|
12
55
|
};
|
|
13
56
|
}
|
|
14
57
|
import BaseClient from './BaseClient.js';
|
package/types/openai.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../src/openai.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../src/openai.js"],"names":[],"mappings":"AAIA;IACE,6BAAoC;IAIlC,eAAiC;IAGnC;;;OAGG;IACH,4BAGC;IAED;;yFA+BC;IAED;;yFAKC;IAED,oCAEC;IAMD;;;MAaC;IAID;;;;;;;;;;MAmBC;IAED;;;;;;;;;;;;;;;;;;;;;;;;MAyBC;CACF;uBAnIsB,iBAAiB;mBAFrB,QAAQ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"code.d.ts","sourceRoot":"","sources":["../../src/utils/code.js"],"names":[],"mappings":"AAEA,6CAMC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../src/utils/json.js"],"names":[],"mappings":"AAEA,oDAKU,UAAK,SAUd"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"templates.d.ts","sourceRoot":"","sources":["../../src/utils/templates.js"],"names":[],"mappings":"AAMA,qDAcC;AAED,iEASC"}
|
package/types/utils.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.js"],"names":[],"mappings":"AAQA,qDAcC;AAED,6CAMC;AAED,iEASC"}
|
package/types/xai.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"xai.d.ts","sourceRoot":"","sources":["../src/xai.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"xai.d.ts","sourceRoot":"","sources":["../src/xai.js"],"names":[],"mappings":"AAEA;CASC;6BAX4B,aAAa"}
|
package/vitest.config.js
ADDED
package/dist/cjs/util.js
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.loadTemplate = loadTemplate;
|
|
7
|
-
exports.loadTemplates = loadTemplates;
|
|
8
|
-
exports.transformResponse = transformResponse;
|
|
9
|
-
var _promises = _interopRequireDefault(require("fs/promises"));
|
|
10
|
-
var _path = _interopRequireDefault(require("path"));
|
|
11
|
-
var _glob = require("glob");
|
|
12
|
-
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
13
|
-
const CODE_REG = /^```\w*$(.+)```/ms;
|
|
14
|
-
const JSON_REG = /([{[].+[}\]])/s;
|
|
15
|
-
async function loadTemplates(dir) {
|
|
16
|
-
const result = {};
|
|
17
|
-
const files = await (0, _glob.glob)(_path.default.join(dir, '*.md'));
|
|
18
|
-
if (!files.length) {
|
|
19
|
-
throw new Error(`No templates found in: ${dir}.`);
|
|
20
|
-
}
|
|
21
|
-
for (let file of files) {
|
|
22
|
-
const base = _path.default.basename(file, '.md');
|
|
23
|
-
result[base] = await loadTemplate(file);
|
|
24
|
-
}
|
|
25
|
-
return result;
|
|
26
|
-
}
|
|
27
|
-
async function loadTemplate(file) {
|
|
28
|
-
return await _promises.default.readFile(file, 'utf-8');
|
|
29
|
-
}
|
|
30
|
-
function transformResponse(options) {
|
|
31
|
-
const {
|
|
32
|
-
output = 'text',
|
|
33
|
-
messages,
|
|
34
|
-
message
|
|
35
|
-
} = options;
|
|
36
|
-
const content = message.content || message.text;
|
|
37
|
-
if (output === 'text') {
|
|
38
|
-
return content;
|
|
39
|
-
} else if (output === 'messages') {
|
|
40
|
-
return [...messages, message];
|
|
41
|
-
} else if (output === 'json') {
|
|
42
|
-
return parseJson(content);
|
|
43
|
-
} else if (output === 'code') {
|
|
44
|
-
return parseCode(content);
|
|
45
|
-
} else {
|
|
46
|
-
throw new Error(`No output type provided.`);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
function parseJson(content) {
|
|
50
|
-
try {
|
|
51
|
-
return JSON.parse(content.match(JSON_REG)[0]);
|
|
52
|
-
} catch (error) {
|
|
53
|
-
throw new Error(`Unable to derive JSON from response:\n\n${content}`);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
function parseCode(content) {
|
|
57
|
-
try {
|
|
58
|
-
return content.match(CODE_REG)[1].trim();
|
|
59
|
-
} catch (error) {
|
|
60
|
-
throw new Error(`Unable to derive code from response:\n\n${content}`);
|
|
61
|
-
}
|
|
62
|
-
}
|
package/src/util.js
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import fs from 'fs/promises';
|
|
2
|
-
|
|
3
|
-
import path from 'path';
|
|
4
|
-
|
|
5
|
-
import { glob } from 'glob';
|
|
6
|
-
|
|
7
|
-
const CODE_REG = /^```\w*$(.+)```/ms;
|
|
8
|
-
const JSON_REG = /([{[].+[}\]])/s;
|
|
9
|
-
|
|
10
|
-
export async function loadTemplates(dir) {
|
|
11
|
-
const result = {};
|
|
12
|
-
const files = await glob(path.join(dir, '*.md'));
|
|
13
|
-
|
|
14
|
-
if (!files.length) {
|
|
15
|
-
throw new Error(`No templates found in: ${dir}.`);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
for (let file of files) {
|
|
19
|
-
const base = path.basename(file, '.md');
|
|
20
|
-
result[base] = await loadTemplate(file);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return result;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export async function loadTemplate(file) {
|
|
27
|
-
return await fs.readFile(file, 'utf-8');
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function transformResponse(options) {
|
|
31
|
-
const { output = 'text', messages, message } = options;
|
|
32
|
-
const content = message.content || message.text;
|
|
33
|
-
if (output === 'text') {
|
|
34
|
-
return content;
|
|
35
|
-
} else if (output === 'messages') {
|
|
36
|
-
return [...messages, message];
|
|
37
|
-
} else if (output === 'json') {
|
|
38
|
-
return parseJson(content);
|
|
39
|
-
} else if (output === 'code') {
|
|
40
|
-
return parseCode(content);
|
|
41
|
-
} else {
|
|
42
|
-
throw new Error(`No output type provided.`);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function parseJson(content) {
|
|
47
|
-
try {
|
|
48
|
-
return JSON.parse(content.match(JSON_REG)[0]);
|
|
49
|
-
} catch (error) {
|
|
50
|
-
throw new Error(`Unable to derive JSON from response:\n\n${content}`);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function parseCode(content) {
|
|
55
|
-
try {
|
|
56
|
-
return content.match(CODE_REG)[1].trim();
|
|
57
|
-
} catch (error) {
|
|
58
|
-
throw new Error(`Unable to derive code from response:\n\n${content}`);
|
|
59
|
-
}
|
|
60
|
-
}
|