55 */
66import { create , tool , Session } from "../src/index"
77
8- // 1. Create an instance pointed at your project directory
9- const mini = create ( { directory : process . cwd ( ) } )
8+ // ---------------------------------------------------------------------------
9+ // Create an instance with copilot enabled
10+ // ---------------------------------------------------------------------------
1011
11- // 2. Bootstrap opencode (loads plugins, DB, tools, etc. )
12+ const mini = create ( { directory : process . cwd ( ) , copilot : { } } )
1213await mini . init ( )
1314
14- // 3. Create a session
15- const session = await mini . session . create ( { title : "My first session" } )
16- console . log ( "Created session:" , session . id )
17-
18- // 4. Send a message and get the assistant response
19- const response = await mini . prompt ( {
20- sessionID : session . id ,
21- parts : [ { type : "text" , text : "What files are in the current directory?" } ] ,
22- } )
23- console . log ( "Assistant responded with" , response . parts . length , "parts" )
24-
25- // 5. Retrieve conversation history
26- const messages = await mini . session . messages ( session . id )
27- for ( const msg of messages ) {
28- console . log ( `[${ msg . info . role } ]` , msg . parts . length , "parts" )
29- }
30-
31- // 6. List all sessions
32- const all = await mini . session . list ( )
33- console . log ( "Total sessions:" , all . length )
34-
35- // 7. Restore a session by ID
36- const restored = await mini . session . get ( session . id )
37- console . log ( "Restored session:" , restored . title )
38-
3915// ---------------------------------------------------------------------------
40- // Multi-tenant: per- user API credentials
16+ // Multi-tenant copilot: each user brings their own GitHub OAuth token
4117// ---------------------------------------------------------------------------
4218
43- // Register credentials for different users
4419mini . credentials . set ( "user-alice" , {
45- providerID : "anthropic " ,
46- apiKey : "sk-ant-alice-key " ,
20+ providerID : "copilot " ,
21+ token : "gho_alice-github-oauth-token " ,
4722} )
23+
4824mini . credentials . set ( "user-bob" , {
49- providerID : "openai " ,
50- apiKey : "sk-bob-key " ,
25+ providerID : "copilot " ,
26+ token : "gho_bob-github-oauth-token " ,
5127} )
5228
53- // Each prompt specifies which user's credentials to use.
54- // Credentials are scoped to the prompt lifetime only — different users
55- // can take turns in the same session, each using their own API key.
56- const shared = await mini . session . create ( { title : "Shared session" } )
29+ const session = await mini . session . create ( { title : "Shared session" } )
5730
58- // Alice sends a message (uses her Anthropic key)
31+ // Alice sends a message routed through copilot with her token
5932await mini . prompt ( {
60- sessionID : shared . id ,
33+ sessionID : session . id ,
6134 parts : [ { type : "text" , text : "Hello from Alice" } ] ,
6235 userId : "user-alice" ,
63- model : { providerID : "anthropic " , modelID : "claude-sonnet-4-20250514" } ,
36+ model : { providerID : "copilot " , modelID : "claude-sonnet-4-20250514" } ,
6437} )
6538
66- // Bob continues the same conversation (uses his OpenAI key)
39+ // Bob continues the same conversation with his own token
6740await mini . prompt ( {
68- sessionID : shared . id ,
41+ sessionID : session . id ,
6942 parts : [ { type : "text" , text : "Hello from Bob" } ] ,
7043 userId : "user-bob" ,
71- model : { providerID : "openai " , modelID : "gpt-4o" } ,
44+ model : { providerID : "copilot " , modelID : "gpt-4o" } ,
7245} )
7346
47+ // Retrieve conversation history
48+ const messages = await mini . session . messages ( session . id )
49+ for ( const msg of messages ) {
50+ console . log ( `[${ msg . info . role } ]` , msg . parts . length , "parts" )
51+ }
52+
7453// Remove credentials when a user logs out
7554mini . credentials . remove ( "user-alice" )
7655
56+ // ---------------------------------------------------------------------------
57+ // API-key providers work too (Anthropic, OpenAI, etc.)
58+ // ---------------------------------------------------------------------------
59+
60+ mini . credentials . set ( "user-carol" , {
61+ providerID : "anthropic" ,
62+ token : "sk-ant-carol-key" ,
63+ } )
64+
65+ await mini . prompt ( {
66+ sessionID : session . id ,
67+ parts : [ { type : "text" , text : "Hello from Carol" } ] ,
68+ userId : "user-carol" ,
69+ model : { providerID : "anthropic" , modelID : "claude-sonnet-4-20250514" } ,
70+ } )
71+
72+ // ---------------------------------------------------------------------------
73+ // Custom copilot model list
74+ // ---------------------------------------------------------------------------
75+
76+ const custom = create ( {
77+ directory : process . cwd ( ) ,
78+ copilot : {
79+ provider : "my-copilot" ,
80+ models : {
81+ "claude-sonnet-4-20250514" : { name : "Claude Sonnet 4" , limit : { context : 200000 , output : 16384 } } ,
82+ "gpt-4o" : { name : "GPT-4o" , limit : { context : 128000 , output : 16384 } } ,
83+ } ,
84+ } ,
85+ } )
86+
7787// ---------------------------------------------------------------------------
7888// Custom tools
7989// ---------------------------------------------------------------------------
@@ -90,35 +100,32 @@ await mini.tools.register(
90100)
91101
92102// ---------------------------------------------------------------------------
93- // Event subscription
103+ // Events
94104// ---------------------------------------------------------------------------
95105
96- // Subscribe to all events (raw bus)
97106const unsub = await mini . subscribeAll ( ( event ) => {
98107 console . log ( "Event:" , event . type )
99108} )
100109
101- // Subscribe to specific events
102110await mini . subscribe ( Session . Event . Created , ( event ) => {
103111 console . log ( "Session created:" , event . properties . info . id )
104112} )
105113
106- // Unsubscribe when done
107114unsub ( )
108115
109116// ---------------------------------------------------------------------------
110- // Cancel an in-progress prompt
117+ // Cancel
111118// ---------------------------------------------------------------------------
112119
113120const long = await mini . session . create ( { title : "Cancellable" } )
114121
115- // Start a prompt in the background
116122const pending = mini . prompt ( {
117123 sessionID : long . id ,
118124 parts : [ { type : "text" , text : "Write a very long essay about the history of computing" } ] ,
125+ userId : "user-bob" ,
126+ model : { providerID : "copilot" , modelID : "gpt-4o" } ,
119127} )
120128
121- // Cancel it after 2 seconds
122129setTimeout ( ( ) => mini . cancel ( long . id ) , 2000 )
123130await pending . catch ( ( ) => console . log ( "Prompt cancelled" ) )
124131
0 commit comments