forked from CodebuffAI/codebuff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfat-sdk-openrouter-example.ts
More file actions
89 lines (83 loc) · 2.17 KB
/
Copy pathfat-sdk-openrouter-example.ts
File metadata and controls
89 lines (83 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import path from 'path'
import {
OpenAICompatibleChatLanguageModel,
VERSION,
} from '@ai-sdk/openai-compatible'
import { WEBSITE_URL } from '@codebuff/sdk'
import { generateText } from 'ai'
const apiKey = '12345'
const codebuffBackendModel = new OpenAICompatibleChatLanguageModel(
'anthropic/claude-sonnet-4.5',
{
provider: 'codebuff.chat',
url: ({ path: endpoint }) =>
new URL(path.join('/api/v1', endpoint), WEBSITE_URL).toString(),
headers: () => ({
Authorization: `Bearer ${apiKey}`,
'user-agent': `ai-sdk/openai-compatible/${VERSION}`,
}),
metadataExtractor: {
extractMetadata: async (...inputs) => {
console.dir({ extractMetadata: inputs }, { depth: null })
return undefined
},
createStreamExtractor: () => ({
processChunk: (...inputs) => {
console.log(
JSON.stringify(inputs, null, 2),
'createStreamExtractor.processChunk',
)
},
buildMetadata: (...inputs) => {
console.log(inputs, 'createStreamExtractor.buildMetadata')
return undefined
},
}),
},
fetch: undefined,
includeUsage: undefined,
supportsStructuredOutputs: true,
},
)
// const response = streamText({
// const response = await generateObject({
const response = await generateText({
model: codebuffBackendModel,
messages: [
{
role: 'system',
content:
'This is a bunch of text just to fill out some space. Ignore this.'.repeat(
100,
),
providerOptions: {
openaiCompatible: {
cache_control: { type: 'ephemeral' },
},
},
},
{
role: 'user',
content: [
{
type: 'text',
text: 'Hello',
},
],
},
],
providerOptions: {
codebuff: {
// all these get directly added to the body at the top level
reasoningEffort: 'low',
codebuff_metadata: {
run_id: '19b636d9-bfbf-40ff-b3e9-92dc86f4a8d0',
client_id: 'test-client-id-123',
},
},
},
})
// for await (const chunk of response.fullStream) {
// console.dir({ chunk }, { depth: null })
// }
console.log(response.text)