forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.ts
More file actions
136 lines (109 loc) · 4.16 KB
/
basic.ts
File metadata and controls
136 lines (109 loc) · 4.16 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* opencode-mini usage examples
*
* Run with: bun run packages/opencode-mini/examples/basic.ts
*/
import { create, tool, Session } from "../src/index"
// ---------------------------------------------------------------------------
// Create an instance with copilot enabled
// ---------------------------------------------------------------------------
const mini = create({ directory: process.cwd(), copilot: {} })
await mini.init()
// ---------------------------------------------------------------------------
// Multi-tenant copilot: each user brings their own GitHub OAuth token
// ---------------------------------------------------------------------------
mini.credentials.set("user-alice", {
providerID: "copilot",
token: "gho_alice-github-oauth-token",
})
mini.credentials.set("user-bob", {
providerID: "copilot",
token: "gho_bob-github-oauth-token",
})
const session = await mini.session.create({ title: "Shared session" })
// Alice sends a message routed through copilot with her token
await mini.prompt({
sessionID: session.id,
parts: [{ type: "text", text: "Hello from Alice" }],
userId: "user-alice",
model: { providerID: "copilot", modelID: "claude-sonnet-4-20250514" },
})
// Bob continues the same conversation with his own token
await mini.prompt({
sessionID: session.id,
parts: [{ type: "text", text: "Hello from Bob" }],
userId: "user-bob",
model: { providerID: "copilot", modelID: "gpt-4o" },
})
// Retrieve conversation history
const messages = await mini.session.messages(session.id)
for (const msg of messages) {
console.log(`[${msg.info.role}]`, msg.parts.length, "parts")
}
// Remove credentials when a user logs out
mini.credentials.remove("user-alice")
// ---------------------------------------------------------------------------
// API-key providers work too (Anthropic, OpenAI, etc.)
// ---------------------------------------------------------------------------
mini.credentials.set("user-carol", {
providerID: "anthropic",
token: "sk-ant-carol-key",
})
await mini.prompt({
sessionID: session.id,
parts: [{ type: "text", text: "Hello from Carol" }],
userId: "user-carol",
model: { providerID: "anthropic", modelID: "claude-sonnet-4-20250514" },
})
// ---------------------------------------------------------------------------
// Custom copilot model list
// ---------------------------------------------------------------------------
const custom = create({
directory: process.cwd(),
copilot: {
provider: "my-copilot",
models: {
"claude-sonnet-4-20250514": { name: "Claude Sonnet 4", limit: { context: 200000, output: 16384 } },
"gpt-4o": { name: "GPT-4o", limit: { context: 128000, output: 16384 } },
},
},
})
// ---------------------------------------------------------------------------
// Custom tools
// ---------------------------------------------------------------------------
await mini.tools.register(
"current_time",
tool({
description: "Returns the current date and time",
args: {},
async execute() {
return new Date().toISOString()
},
}),
)
// ---------------------------------------------------------------------------
// Events
// ---------------------------------------------------------------------------
const unsub = await mini.subscribeAll((event) => {
console.log("Event:", event.type)
})
await mini.subscribe(Session.Event.Created, (event) => {
console.log("Session created:", event.properties.info.id)
})
unsub()
// ---------------------------------------------------------------------------
// Cancel
// ---------------------------------------------------------------------------
const long = await mini.session.create({ title: "Cancellable" })
const pending = mini.prompt({
sessionID: long.id,
parts: [{ type: "text", text: "Write a very long essay about the history of computing" }],
userId: "user-bob",
model: { providerID: "copilot", modelID: "gpt-4o" },
})
setTimeout(() => mini.cancel(long.id), 2000)
await pending.catch(() => console.log("Prompt cancelled"))
// ---------------------------------------------------------------------------
// Cleanup
// ---------------------------------------------------------------------------
await mini.dispose()