@tanstack/ai-grok 0.7.0 → 0.7.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/dist/esm/adapters/image.d.ts +8 -7
- package/dist/esm/adapters/image.js +10 -60
- package/dist/esm/adapters/image.js.map +1 -1
- package/dist/esm/adapters/summarize.d.ts +2 -7
- package/dist/esm/adapters/summarize.js +11 -95
- package/dist/esm/adapters/summarize.js.map +1 -1
- package/dist/esm/adapters/text.d.ts +10 -34
- package/dist/esm/adapters/text.js +4 -381
- package/dist/esm/adapters/text.js.map +1 -1
- package/dist/esm/adapters/transcription.js +3 -1
- package/dist/esm/adapters/transcription.js.map +1 -1
- package/dist/esm/adapters/tts.js +3 -1
- package/dist/esm/adapters/tts.js.map +1 -1
- package/dist/esm/realtime/token.js +2 -0
- package/dist/esm/realtime/token.js.map +1 -1
- package/dist/esm/text/text-provider-options.d.ts +0 -14
- package/dist/esm/tools/index.d.ts +1 -2
- package/dist/esm/tools/index.js +3 -4
- package/dist/esm/tools/index.js.map +1 -1
- package/dist/esm/utils/client.d.ts +5 -9
- package/dist/esm/utils/client.js +11 -17
- package/dist/esm/utils/client.js.map +1 -1
- package/dist/esm/utils/index.d.ts +2 -1
- package/dist/esm/utils/schema-converter.d.ts +2 -24
- package/package.json +6 -6
- package/src/adapters/image.ts +18 -87
- package/src/adapters/summarize.ts +16 -121
- package/src/adapters/text.ts +13 -562
- package/src/text/text-provider-options.ts +0 -22
- package/src/tools/index.ts +4 -4
- package/src/utils/client.ts +15 -30
- package/src/utils/index.ts +2 -2
- package/src/utils/schema-converter.ts +2 -110
- package/dist/esm/text/text-provider-options.js +0 -6
- package/dist/esm/text/text-provider-options.js.map +0 -1
- package/dist/esm/tools/function-tool.d.ts +0 -15
- package/dist/esm/tools/function-tool.js +0 -27
- package/dist/esm/tools/function-tool.js.map +0 -1
- package/dist/esm/tools/tool-converter.d.ts +0 -7
- package/dist/esm/tools/tool-converter.js +0 -10
- package/dist/esm/tools/tool-converter.js.map +0 -1
- package/dist/esm/utils/schema-converter.js +0 -71
- package/dist/esm/utils/schema-converter.js.map +0 -1
- package/src/tools/function-tool.ts +0 -45
- package/src/tools/tool-converter.ts +0 -17
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { FunctionTool } from '../tools/function-tool.js';
|
|
2
1
|
/**
|
|
3
2
|
* Grok Text Provider Options
|
|
4
3
|
*
|
|
@@ -47,20 +46,7 @@ export interface GrokTextProviderOptions extends GrokBaseOptions {
|
|
|
47
46
|
*/
|
|
48
47
|
stop?: string | Array<string>;
|
|
49
48
|
}
|
|
50
|
-
/**
|
|
51
|
-
* Internal options interface for validation
|
|
52
|
-
* Used internally by the adapter
|
|
53
|
-
*/
|
|
54
|
-
export interface InternalTextProviderOptions extends GrokTextProviderOptions {
|
|
55
|
-
model: string;
|
|
56
|
-
stream?: boolean;
|
|
57
|
-
tools?: Array<FunctionTool>;
|
|
58
|
-
}
|
|
59
49
|
/**
|
|
60
50
|
* External provider options (what users pass in)
|
|
61
51
|
*/
|
|
62
52
|
export type ExternalTextProviderOptions = GrokTextProviderOptions;
|
|
63
|
-
/**
|
|
64
|
-
* Validates text provider options
|
|
65
|
-
*/
|
|
66
|
-
export declare function validateTextProviderOptions(_options: InternalTextProviderOptions): void;
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export { convertFunctionToolToAdapterFormat,
|
|
2
|
-
export { convertToolsToProviderFormat } from './tool-converter.js';
|
|
1
|
+
export { type ChatCompletionFunctionTool as FunctionTool, convertFunctionToolToChatCompletionsFormat as convertFunctionToolToAdapterFormat, convertToolsToChatCompletionsFormat as convertToolsToProviderFormat, } from '@tanstack/openai-base';
|
package/dist/esm/tools/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { convertToolsToProviderFormat } from "./tool-converter.js";
|
|
1
|
+
import { convertFunctionToolToChatCompletionsFormat, convertToolsToChatCompletionsFormat } from "@tanstack/openai-base";
|
|
3
2
|
export {
|
|
4
|
-
convertFunctionToolToAdapterFormat,
|
|
5
|
-
convertToolsToProviderFormat
|
|
3
|
+
convertFunctionToolToChatCompletionsFormat as convertFunctionToolToAdapterFormat,
|
|
4
|
+
convertToolsToChatCompletionsFormat as convertToolsToProviderFormat
|
|
6
5
|
};
|
|
7
6
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
|
|
@@ -1,17 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export interface GrokClientConfig extends
|
|
3
|
-
apiKey: string;
|
|
1
|
+
import { OpenAICompatibleClientConfig } from '@tanstack/openai-base';
|
|
2
|
+
export interface GrokClientConfig extends OpenAICompatibleClientConfig {
|
|
4
3
|
}
|
|
5
|
-
/**
|
|
6
|
-
* Creates a Grok SDK client instance using OpenAI SDK with xAI's base URL
|
|
7
|
-
*/
|
|
8
|
-
export declare function createGrokClient(config: GrokClientConfig): OpenAI_SDK;
|
|
9
4
|
/**
|
|
10
5
|
* Gets Grok API key from environment variables
|
|
11
6
|
* @throws Error if XAI_API_KEY is not found
|
|
12
7
|
*/
|
|
13
8
|
export declare function getGrokApiKeyFromEnv(): string;
|
|
14
9
|
/**
|
|
15
|
-
*
|
|
10
|
+
* Returns a Grok client config with the default xAI base URL applied
|
|
11
|
+
* when not already set.
|
|
16
12
|
*/
|
|
17
|
-
export declare function
|
|
13
|
+
export declare function withGrokDefaults(config: GrokClientConfig): OpenAICompatibleClientConfig;
|
package/dist/esm/utils/client.js
CHANGED
|
@@ -1,27 +1,21 @@
|
|
|
1
|
-
import
|
|
2
|
-
function createGrokClient(config) {
|
|
3
|
-
return new OpenAI_SDK({
|
|
4
|
-
...config,
|
|
5
|
-
apiKey: config.apiKey,
|
|
6
|
-
baseURL: config.baseURL || "https://api.x.ai/v1"
|
|
7
|
-
});
|
|
8
|
-
}
|
|
1
|
+
import { getApiKeyFromEnv } from "@tanstack/ai-utils";
|
|
9
2
|
function getGrokApiKeyFromEnv() {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
3
|
+
try {
|
|
4
|
+
return getApiKeyFromEnv("XAI_API_KEY");
|
|
5
|
+
} catch {
|
|
13
6
|
throw new Error(
|
|
14
7
|
"XAI_API_KEY is required. Please set it in your environment variables or use the factory function with an explicit API key."
|
|
15
8
|
);
|
|
16
9
|
}
|
|
17
|
-
return key;
|
|
18
10
|
}
|
|
19
|
-
function
|
|
20
|
-
return
|
|
11
|
+
function withGrokDefaults(config) {
|
|
12
|
+
return {
|
|
13
|
+
...config,
|
|
14
|
+
baseURL: config.baseURL || "https://api.x.ai/v1"
|
|
15
|
+
};
|
|
21
16
|
}
|
|
22
17
|
export {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
getGrokApiKeyFromEnv
|
|
18
|
+
getGrokApiKeyFromEnv,
|
|
19
|
+
withGrokDefaults
|
|
26
20
|
};
|
|
27
21
|
//# sourceMappingURL=client.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sources":["../../../src/utils/client.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"file":"client.js","sources":["../../../src/utils/client.ts"],"sourcesContent":["import { getApiKeyFromEnv } from '@tanstack/ai-utils'\nimport type { OpenAICompatibleClientConfig } from '@tanstack/openai-base'\n\nexport interface GrokClientConfig extends OpenAICompatibleClientConfig {}\n\n/**\n * Gets Grok API key from environment variables\n * @throws Error if XAI_API_KEY is not found\n */\nexport function getGrokApiKeyFromEnv(): string {\n try {\n return getApiKeyFromEnv('XAI_API_KEY')\n } catch {\n throw new Error(\n 'XAI_API_KEY is required. Please set it in your environment variables or use the factory function with an explicit API key.',\n )\n }\n}\n\n/**\n * Returns a Grok client config with the default xAI base URL applied\n * when not already set.\n */\nexport function withGrokDefaults(\n config: GrokClientConfig,\n): OpenAICompatibleClientConfig {\n return {\n ...config,\n baseURL: config.baseURL || 'https://api.x.ai/v1',\n }\n}\n"],"names":[],"mappings":";AASO,SAAS,uBAA+B;AAC7C,MAAI;AACF,WAAO,iBAAiB,aAAa;AAAA,EACvC,QAAQ;AACN,UAAM,IAAI;AAAA,MACR;AAAA,IAAA;AAAA,EAEJ;AACF;AAMO,SAAS,iBACd,QAC8B;AAC9B,SAAO;AAAA,IACL,GAAG;AAAA,IACH,SAAS,OAAO,WAAW;AAAA,EAAA;AAE/B;"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { generateId } from '@tanstack/ai-utils';
|
|
2
|
+
export { getGrokApiKeyFromEnv, withGrokDefaults, type GrokClientConfig, } from './client.js';
|
|
2
3
|
export { makeGrokStructuredOutputCompatible, transformNullsToUndefined, } from './schema-converter.js';
|
|
3
4
|
export { toAudioFile, arrayBufferToBase64 } from './audio.js';
|
|
@@ -1,24 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* This is needed because Grok's structured output (via OpenAI-compatible API) requires all fields to be
|
|
5
|
-
* in the `required` array, with optional fields made nullable (type: ["string", "null"]).
|
|
6
|
-
* When Grok returns null for optional fields, we need to convert them back to
|
|
7
|
-
* undefined to match the original Zod schema expectations.
|
|
8
|
-
*
|
|
9
|
-
* @param obj - Object to transform
|
|
10
|
-
* @returns Object with nulls converted to undefined
|
|
11
|
-
*/
|
|
12
|
-
export declare function transformNullsToUndefined<T>(obj: T): T;
|
|
13
|
-
/**
|
|
14
|
-
* Transform a JSON schema to be compatible with Grok's structured output requirements (OpenAI-compatible).
|
|
15
|
-
* Grok requires:
|
|
16
|
-
* - All properties must be in the `required` array
|
|
17
|
-
* - Optional fields should have null added to their type union
|
|
18
|
-
* - additionalProperties must be false for objects
|
|
19
|
-
*
|
|
20
|
-
* @param schema - JSON schema to transform
|
|
21
|
-
* @param originalRequired - Original required array (to know which fields were optional)
|
|
22
|
-
* @returns Transformed schema compatible with Grok structured output
|
|
23
|
-
*/
|
|
24
|
-
export declare function makeGrokStructuredOutputCompatible(schema: Record<string, any>, originalRequired?: Array<string>): Record<string, any>;
|
|
1
|
+
export { transformNullsToUndefined } from '@tanstack/ai-utils';
|
|
2
|
+
export { makeStructuredOutputCompatible as makeGrokStructuredOutputCompatible } from '@tanstack/openai-base';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai-grok",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.2",
|
|
4
4
|
"description": "Grok (xAI) adapter for TanStack AI",
|
|
5
5
|
"author": "",
|
|
6
6
|
"license": "MIT",
|
|
@@ -34,18 +34,18 @@
|
|
|
34
34
|
"adapter"
|
|
35
35
|
],
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"
|
|
37
|
+
"@tanstack/ai-utils": "0.2.0",
|
|
38
|
+
"@tanstack/openai-base": "0.2.0"
|
|
38
39
|
},
|
|
39
40
|
"devDependencies": {
|
|
40
41
|
"@vitest/coverage-v8": "4.0.14",
|
|
41
42
|
"vite": "^7.2.7",
|
|
42
|
-
"
|
|
43
|
-
"@tanstack/ai": "0.
|
|
44
|
-
"@tanstack/ai-client": "0.8.0"
|
|
43
|
+
"@tanstack/ai": "0.15.0",
|
|
44
|
+
"@tanstack/ai-client": "0.9.0"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
47
|
"zod": "^4.0.0",
|
|
48
|
-
"@tanstack/ai": "^0.
|
|
48
|
+
"@tanstack/ai": "^0.15.0"
|
|
49
49
|
},
|
|
50
50
|
"scripts": {
|
|
51
51
|
"build": "vite build",
|
package/src/adapters/image.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { OpenAICompatibleImageAdapter } from '@tanstack/openai-base'
|
|
2
|
+
import { getGrokApiKeyFromEnv, withGrokDefaults } from '../utils/client'
|
|
3
3
|
import {
|
|
4
4
|
validateImageSize,
|
|
5
5
|
validateNumberOfImages,
|
|
@@ -11,12 +11,6 @@ import type {
|
|
|
11
11
|
GrokImageModelSizeByName,
|
|
12
12
|
GrokImageProviderOptions,
|
|
13
13
|
} from '../image/image-provider-options'
|
|
14
|
-
import type {
|
|
15
|
-
GeneratedImage,
|
|
16
|
-
ImageGenerationOptions,
|
|
17
|
-
ImageGenerationResult,
|
|
18
|
-
} from '@tanstack/ai'
|
|
19
|
-
import type OpenAI_SDK from 'openai'
|
|
20
14
|
import type { GrokClientConfig } from '../utils'
|
|
21
15
|
|
|
22
16
|
/**
|
|
@@ -37,7 +31,7 @@ export interface GrokImageConfig extends GrokClientConfig {}
|
|
|
37
31
|
*/
|
|
38
32
|
export class GrokImageAdapter<
|
|
39
33
|
TModel extends GrokImageModel,
|
|
40
|
-
> extends
|
|
34
|
+
> extends OpenAICompatibleImageAdapter<
|
|
41
35
|
TModel,
|
|
42
36
|
GrokImageProviderOptions,
|
|
43
37
|
GrokImageModelProviderOptionsByName,
|
|
@@ -46,92 +40,29 @@ export class GrokImageAdapter<
|
|
|
46
40
|
readonly kind = 'image' as const
|
|
47
41
|
readonly name = 'grok' as const
|
|
48
42
|
|
|
49
|
-
private client: OpenAI_SDK
|
|
50
|
-
|
|
51
43
|
constructor(config: GrokImageConfig, model: TModel) {
|
|
52
|
-
super(model,
|
|
53
|
-
this.client = createGrokClient(config)
|
|
44
|
+
super(withGrokDefaults(config), model, 'grok')
|
|
54
45
|
}
|
|
55
46
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
logger.request(`activity=generateImage provider=grok model=${this.model}`, {
|
|
62
|
-
provider: 'grok',
|
|
63
|
-
model: this.model,
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
try {
|
|
67
|
-
// Validate inputs
|
|
68
|
-
validatePrompt({ prompt, model })
|
|
69
|
-
validateImageSize(model, size)
|
|
70
|
-
validateNumberOfImages(model, numberOfImages)
|
|
71
|
-
|
|
72
|
-
// Build request based on model type
|
|
73
|
-
const request = this.buildRequest(options)
|
|
74
|
-
|
|
75
|
-
const response = await this.client.images.generate({
|
|
76
|
-
...request,
|
|
77
|
-
stream: false,
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
return this.transformResponse(model, response)
|
|
81
|
-
} catch (error) {
|
|
82
|
-
logger.errors('grok.generateImage fatal', {
|
|
83
|
-
error,
|
|
84
|
-
source: 'grok.generateImage',
|
|
85
|
-
})
|
|
86
|
-
throw error
|
|
87
|
-
}
|
|
47
|
+
protected override validatePrompt(options: {
|
|
48
|
+
prompt: string
|
|
49
|
+
model: string
|
|
50
|
+
}): void {
|
|
51
|
+
validatePrompt(options)
|
|
88
52
|
}
|
|
89
53
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
// Spread modelOptions FIRST so explicit args (model, prompt, n, size) win
|
|
96
|
-
// and user-supplied modelOptions cannot silently override them.
|
|
97
|
-
return {
|
|
98
|
-
...modelOptions,
|
|
99
|
-
model,
|
|
100
|
-
prompt,
|
|
101
|
-
n: numberOfImages ?? 1,
|
|
102
|
-
size: size as OpenAI_SDK.Images.ImageGenerateParams['size'],
|
|
103
|
-
}
|
|
54
|
+
protected override validateImageSize(
|
|
55
|
+
model: string,
|
|
56
|
+
size: string | undefined,
|
|
57
|
+
): void {
|
|
58
|
+
validateImageSize(model, size)
|
|
104
59
|
}
|
|
105
60
|
|
|
106
|
-
|
|
61
|
+
protected override validateNumberOfImages(
|
|
107
62
|
model: string,
|
|
108
|
-
|
|
109
|
-
):
|
|
110
|
-
|
|
111
|
-
(item): Array<GeneratedImage> => {
|
|
112
|
-
const revisedPrompt = item.revised_prompt
|
|
113
|
-
if (item.b64_json) {
|
|
114
|
-
return [{ b64Json: item.b64_json, revisedPrompt }]
|
|
115
|
-
}
|
|
116
|
-
if (item.url) {
|
|
117
|
-
return [{ url: item.url, revisedPrompt }]
|
|
118
|
-
}
|
|
119
|
-
return []
|
|
120
|
-
},
|
|
121
|
-
)
|
|
122
|
-
|
|
123
|
-
return {
|
|
124
|
-
id: generateId(this.name),
|
|
125
|
-
model,
|
|
126
|
-
images,
|
|
127
|
-
usage: response.usage
|
|
128
|
-
? {
|
|
129
|
-
inputTokens: response.usage.input_tokens,
|
|
130
|
-
outputTokens: response.usage.output_tokens,
|
|
131
|
-
totalTokens: response.usage.total_tokens,
|
|
132
|
-
}
|
|
133
|
-
: undefined,
|
|
134
|
-
}
|
|
63
|
+
numberOfImages: number | undefined,
|
|
64
|
+
): void {
|
|
65
|
+
validateNumberOfImages(model, numberOfImages)
|
|
135
66
|
}
|
|
136
67
|
}
|
|
137
68
|
|
|
@@ -1,12 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { OpenAICompatibleSummarizeAdapter } from '@tanstack/openai-base'
|
|
2
2
|
import { getGrokApiKeyFromEnv } from '../utils'
|
|
3
3
|
import { GrokTextAdapter } from './text'
|
|
4
|
+
import type { ChatStreamCapable } from '@tanstack/openai-base'
|
|
4
5
|
import type { GROK_CHAT_MODELS } from '../model-meta'
|
|
5
|
-
import type {
|
|
6
|
-
StreamChunk,
|
|
7
|
-
SummarizationOptions,
|
|
8
|
-
SummarizationResult,
|
|
9
|
-
} from '@tanstack/ai'
|
|
10
6
|
import type { GrokClientConfig } from '../utils'
|
|
11
7
|
|
|
12
8
|
/**
|
|
@@ -35,125 +31,24 @@ export type GrokSummarizeModel = (typeof GROK_CHAT_MODELS)[number]
|
|
|
35
31
|
*/
|
|
36
32
|
export class GrokSummarizeAdapter<
|
|
37
33
|
TModel extends GrokSummarizeModel,
|
|
38
|
-
> extends
|
|
34
|
+
> extends OpenAICompatibleSummarizeAdapter<
|
|
35
|
+
TModel,
|
|
36
|
+
GrokSummarizeProviderOptions
|
|
37
|
+
> {
|
|
39
38
|
readonly kind = 'summarize' as const
|
|
40
39
|
readonly name = 'grok' as const
|
|
41
40
|
|
|
42
|
-
private textAdapter: GrokTextAdapter<TModel>
|
|
43
|
-
|
|
44
41
|
constructor(config: GrokSummarizeConfig, model: TModel) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
model: options.model,
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
// Use the text adapter's streaming and collect the result
|
|
59
|
-
let summary = ''
|
|
60
|
-
const id = ''
|
|
61
|
-
let model = options.model
|
|
62
|
-
let usage = { promptTokens: 0, completionTokens: 0, totalTokens: 0 }
|
|
63
|
-
|
|
64
|
-
try {
|
|
65
|
-
for await (const chunk of this.textAdapter.chatStream({
|
|
66
|
-
model: options.model,
|
|
67
|
-
messages: [{ role: 'user', content: options.text }],
|
|
68
|
-
systemPrompts: [systemPrompt],
|
|
69
|
-
maxTokens: options.maxLength,
|
|
70
|
-
temperature: 0.3,
|
|
71
|
-
logger,
|
|
72
|
-
})) {
|
|
73
|
-
// AG-UI TEXT_MESSAGE_CONTENT event
|
|
74
|
-
if (chunk.type === 'TEXT_MESSAGE_CONTENT') {
|
|
75
|
-
if (chunk.content) {
|
|
76
|
-
summary = chunk.content
|
|
77
|
-
} else {
|
|
78
|
-
summary += chunk.delta
|
|
79
|
-
}
|
|
80
|
-
model = chunk.model || model
|
|
81
|
-
}
|
|
82
|
-
// AG-UI RUN_FINISHED event
|
|
83
|
-
if (chunk.type === 'RUN_FINISHED') {
|
|
84
|
-
if (chunk.usage) {
|
|
85
|
-
usage = chunk.usage
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
} catch (error) {
|
|
90
|
-
logger.errors('grok.summarize fatal', {
|
|
91
|
-
error,
|
|
92
|
-
source: 'grok.summarize',
|
|
93
|
-
})
|
|
94
|
-
throw error
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return { id, model, summary, usage }
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
async *summarizeStream(
|
|
101
|
-
options: SummarizationOptions,
|
|
102
|
-
): AsyncIterable<StreamChunk> {
|
|
103
|
-
const { logger } = options
|
|
104
|
-
const systemPrompt = this.buildSummarizationPrompt(options)
|
|
105
|
-
|
|
106
|
-
logger.request(`activity=summarize provider=grok`, {
|
|
107
|
-
provider: 'grok',
|
|
108
|
-
model: options.model,
|
|
109
|
-
stream: true,
|
|
110
|
-
})
|
|
111
|
-
|
|
112
|
-
try {
|
|
113
|
-
// Delegate directly to the text adapter's streaming
|
|
114
|
-
yield* this.textAdapter.chatStream({
|
|
115
|
-
model: options.model,
|
|
116
|
-
messages: [{ role: 'user', content: options.text }],
|
|
117
|
-
systemPrompts: [systemPrompt],
|
|
118
|
-
maxTokens: options.maxLength,
|
|
119
|
-
temperature: 0.3,
|
|
120
|
-
logger,
|
|
121
|
-
})
|
|
122
|
-
} catch (error) {
|
|
123
|
-
logger.errors('grok.summarize fatal', {
|
|
124
|
-
error,
|
|
125
|
-
source: 'grok.summarize',
|
|
126
|
-
})
|
|
127
|
-
throw error
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
private buildSummarizationPrompt(options: SummarizationOptions): string {
|
|
132
|
-
let prompt = 'You are a professional summarizer. '
|
|
133
|
-
|
|
134
|
-
switch (options.style) {
|
|
135
|
-
case 'bullet-points':
|
|
136
|
-
prompt += 'Provide a summary in bullet point format. '
|
|
137
|
-
break
|
|
138
|
-
case 'paragraph':
|
|
139
|
-
prompt += 'Provide a summary in paragraph format. '
|
|
140
|
-
break
|
|
141
|
-
case 'concise':
|
|
142
|
-
prompt += 'Provide a very concise summary in 1-2 sentences. '
|
|
143
|
-
break
|
|
144
|
-
default:
|
|
145
|
-
prompt += 'Provide a clear and concise summary. '
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
if (options.focus && options.focus.length > 0) {
|
|
149
|
-
prompt += `Focus on the following aspects: ${options.focus.join(', ')}. `
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
if (options.maxLength) {
|
|
153
|
-
prompt += `Keep the summary under ${options.maxLength} tokens. `
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
return prompt
|
|
42
|
+
// The text adapter accepts richer provider options than the summarize adapter needs,
|
|
43
|
+
// but we only pass basic options (model, messages, systemPrompts, etc.) at call time.
|
|
44
|
+
super(
|
|
45
|
+
new GrokTextAdapter(
|
|
46
|
+
config,
|
|
47
|
+
model,
|
|
48
|
+
) as unknown as ChatStreamCapable<GrokSummarizeProviderOptions>,
|
|
49
|
+
model,
|
|
50
|
+
'grok',
|
|
51
|
+
)
|
|
157
52
|
}
|
|
158
53
|
}
|
|
159
54
|
|