Skip to content

Commit d56dec4

Browse files
authored
wip: optional IDs in api (#1128)
1 parent c952e9a commit d56dec4

File tree

12 files changed

+145
-99
lines changed

12 files changed

+145
-99
lines changed

packages/opencode/src/cli/cmd/run.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,6 @@ export const RunCommand = cmd({
172172
parts: [
173173
{
174174
id: Identifier.ascending("part"),
175-
sessionID: session.id,
176-
messageID: messageID,
177175
type: "text",
178176
text: message,
179177
},

packages/opencode/src/server/server.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -451,16 +451,7 @@ export namespace Server {
451451
id: z.string().openapi({ description: "Session ID" }),
452452
}),
453453
),
454-
zValidator(
455-
"json",
456-
z.object({
457-
messageID: z.string(),
458-
providerID: z.string(),
459-
modelID: z.string(),
460-
mode: z.string(),
461-
parts: z.union([MessageV2.FilePart, MessageV2.TextPart]).array(),
462-
}),
463-
),
454+
zValidator("json", Session.ChatInput.omit({ sessionID: true })),
464455
async (c) => {
465456
const sessionID = c.req.valid("param").id
466457
const body = c.req.valid("json")

packages/opencode/src/session/index.ts

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,39 @@ export namespace Session {
319319
return part
320320
}
321321

322-
export async function chat(input: {
323-
sessionID: string
324-
messageID: string
325-
providerID: string
326-
modelID: string
327-
mode?: string
328-
parts: (MessageV2.TextPart | MessageV2.FilePart)[]
329-
}) {
322+
export const ChatInput = z.object({
323+
sessionID: Identifier.schema("session"),
324+
messageID: Identifier.schema("message").optional(),
325+
providerID: z.string(),
326+
modelID: z.string(),
327+
mode: z.string().optional(),
328+
parts: z.array(
329+
z.discriminatedUnion("type", [
330+
MessageV2.TextPart.omit({
331+
messageID: true,
332+
sessionID: true,
333+
})
334+
.partial({
335+
id: true,
336+
})
337+
.openapi({
338+
ref: "TextPartInput",
339+
}),
340+
MessageV2.FilePart.omit({
341+
messageID: true,
342+
sessionID: true,
343+
})
344+
.partial({
345+
id: true,
346+
})
347+
.openapi({
348+
ref: "FilePartInput",
349+
}),
350+
]),
351+
),
352+
})
353+
354+
export async function chat(input: z.infer<typeof ChatInput>) {
330355
const l = log.clone().tag("session", input.sessionID)
331356
l.info("chatting")
332357

@@ -384,7 +409,7 @@ export namespace Session {
384409
if (lastSummary) msgs = msgs.filter((msg) => msg.info.id >= lastSummary.info.id)
385410

386411
const userMsg: MessageV2.Info = {
387-
id: input.messageID,
412+
id: input.messageID ?? Identifier.ascending("message"),
388413
role: "user",
389414
sessionID: input.sessionID,
390415
time: {
@@ -490,7 +515,14 @@ export namespace Session {
490515
]
491516
}
492517
}
493-
return [part]
518+
return [
519+
{
520+
id: Identifier.ascending("part"),
521+
...part,
522+
messageID: userMsg.id,
523+
sessionID: input.sessionID,
524+
},
525+
]
494526
}),
495527
).then((x) => x.flat())
496528

@@ -1104,8 +1136,6 @@ export namespace Session {
11041136
parts: [
11051137
{
11061138
id: Identifier.ascending("part"),
1107-
sessionID: input.sessionID,
1108-
messageID: input.messageID,
11091139
type: "text",
11101140
text: PROMPT_INITIALIZE.replace("${path}", app.path.root),
11111141
},

packages/opencode/src/tool/task.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ export const TaskTool = Tool.define({
4444
parts: [
4545
{
4646
id: Identifier.ascending("part"),
47-
messageID,
48-
sessionID: session.id,
4947
type: "text",
5048
text: params.prompt,
5149
},

packages/tui/internal/app/app.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type SessionClearedMsg struct{}
6363
type CompactSessionMsg struct{}
6464
type SendMsg struct {
6565
Text string
66-
Attachments []opencode.FilePartParam
66+
Attachments []opencode.FilePartInputParam
6767
}
6868
type SetEditorContentMsg struct {
6969
Text string
@@ -462,7 +462,7 @@ func (a *App) CreateSession(ctx context.Context) (*opencode.Session, error) {
462462
func (a *App) SendChatMessage(
463463
ctx context.Context,
464464
text string,
465-
attachments []opencode.FilePartParam,
465+
attachments []opencode.FilePartInputParam,
466466
) (*App, tea.Cmd) {
467467
var cmds []tea.Cmd
468468
if a.Session.ID == "" {
@@ -511,22 +511,18 @@ func (a *App) SendChatMessage(
511511
for _, part := range parts {
512512
switch casted := part.(type) {
513513
case opencode.TextPart:
514-
partsParam = append(partsParam, opencode.TextPartParam{
515-
ID: opencode.F(casted.ID),
516-
MessageID: opencode.F(casted.MessageID),
517-
SessionID: opencode.F(casted.SessionID),
518-
Type: opencode.F(casted.Type),
519-
Text: opencode.F(casted.Text),
514+
partsParam = append(partsParam, opencode.TextPartInputParam{
515+
ID: opencode.F(casted.ID),
516+
Type: opencode.F(opencode.TextPartInputType(casted.Type)),
517+
Text: opencode.F(casted.Text),
520518
})
521519
case opencode.FilePart:
522-
partsParam = append(partsParam, opencode.FilePartParam{
523-
ID: opencode.F(casted.ID),
524-
Mime: opencode.F(casted.Mime),
525-
MessageID: opencode.F(casted.MessageID),
526-
SessionID: opencode.F(casted.SessionID),
527-
Type: opencode.F(casted.Type),
528-
URL: opencode.F(casted.URL),
529-
Filename: opencode.F(casted.Filename),
520+
partsParam = append(partsParam, opencode.FilePartInputParam{
521+
ID: opencode.F(casted.ID),
522+
Mime: opencode.F(casted.Mime),
523+
Type: opencode.F(opencode.FilePartInputType(casted.Type)),
524+
URL: opencode.F(casted.URL),
525+
Filename: opencode.F(casted.Filename),
530526
})
531527
}
532528
}

packages/tui/internal/components/chat/editor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,10 @@ func (m *editorComponent) Submit() (tea.Model, tea.Cmd) {
306306
var cmds []tea.Cmd
307307

308308
attachments := m.textarea.GetAttachments()
309-
fileParts := make([]opencode.FilePartParam, 0)
309+
fileParts := make([]opencode.FilePartInputParam, 0)
310310
for _, attachment := range attachments {
311-
fileParts = append(fileParts, opencode.FilePartParam{
312-
Type: opencode.F(opencode.FilePartTypeFile),
311+
fileParts = append(fileParts, opencode.FilePartInputParam{
312+
Type: opencode.F(opencode.FilePartInputTypeFile),
313313
Mime: opencode.F(attachment.MediaType),
314314
URL: opencode.F(attachment.URL),
315315
Filename: opencode.F(attachment.Filename),

packages/tui/sdk/.stats.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
configured_endpoints: 22
2-
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/opencode%2Fopencode-8792f91dd070f7b4ee671fc86e8a03976dc7fb6ee49f8c99ad989e1597003774.yml
3-
openapi_spec_hash: fe9dc3a074be560de0b97df9b5af2c1b
4-
config_hash: b7f3d9742335715c458494988498b183
2+
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/opencode%2Fopencode-d34620b462127c45497743c97fd3569f9e629d9fbd97c0707087eeddbd4b3de1.yml
3+
openapi_spec_hash: 23864c98d555350fe56f1d0e56f205c5
4+
config_hash: a8441af7cb2db855d79fd372ee3b9fb1

packages/tui/sdk/api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ Methods:
7777

7878
Params Types:
7979

80-
- <a href="https://pkg.go.dev/github.com/sst/opencode-sdk-go">opencode</a>.<a href="https://pkg.go.dev/github.com/sst/opencode-sdk-go#FilePartParam">FilePartParam</a>
81-
- <a href="https://pkg.go.dev/github.com/sst/opencode-sdk-go">opencode</a>.<a href="https://pkg.go.dev/github.com/sst/opencode-sdk-go#TextPartParam">TextPartParam</a>
80+
- <a href="https://pkg.go.dev/github.com/sst/opencode-sdk-go">opencode</a>.<a href="https://pkg.go.dev/github.com/sst/opencode-sdk-go#FilePartInputParam">FilePartInputParam</a>
81+
- <a href="https://pkg.go.dev/github.com/sst/opencode-sdk-go">opencode</a>.<a href="https://pkg.go.dev/github.com/sst/opencode-sdk-go#TextPartInputParam">TextPartInputParam</a>
8282

8383
Response Types:
8484

packages/tui/sdk/config.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ type Config struct {
6767
Model string `json:"model"`
6868
// Custom provider configurations and model overrides
6969
Provider map[string]ConfigProvider `json:"provider"`
70-
// Control sharing behavior: 'auto' enables automatic sharing, 'disabled' disables
71-
// all sharing
70+
// Control sharing behavior:'manual' allows manual sharing via commands, 'auto'
71+
// enables automatic sharing, 'disabled' disables all sharing
7272
Share ConfigShare `json:"share"`
7373
// Theme name to use for the interface
7474
Theme string `json:"theme"`
@@ -206,6 +206,8 @@ type ConfigMcp struct {
206206
Enabled bool `json:"enabled"`
207207
// This field can have the runtime type of [map[string]string].
208208
Environment interface{} `json:"environment"`
209+
// This field can have the runtime type of [map[string]string].
210+
Headers interface{} `json:"headers"`
209211
// URL of the remote MCP server
210212
URL string `json:"url"`
211213
JSON configMcpJSON `json:"-"`
@@ -218,6 +220,7 @@ type configMcpJSON struct {
218220
Command apijson.Field
219221
Enabled apijson.Field
220222
Environment apijson.Field
223+
Headers apijson.Field
221224
URL apijson.Field
222225
raw string
223226
ExtraFields map[string]apijson.Field
@@ -427,18 +430,19 @@ func (r configProviderModelsLimitJSON) RawJSON() string {
427430
return r.raw
428431
}
429432

430-
// Control sharing behavior: 'auto' enables automatic sharing, 'disabled' disables
431-
// all sharing
433+
// Control sharing behavior:'manual' allows manual sharing via commands, 'auto'
434+
// enables automatic sharing, 'disabled' disables all sharing
432435
type ConfigShare string
433436

434437
const (
438+
ConfigShareManual ConfigShare = "manual"
435439
ConfigShareAuto ConfigShare = "auto"
436440
ConfigShareDisabled ConfigShare = "disabled"
437441
)
438442

439443
func (r ConfigShare) IsKnown() bool {
440444
switch r {
441-
case ConfigShareAuto, ConfigShareDisabled:
445+
case ConfigShareManual, ConfigShareAuto, ConfigShareDisabled:
442446
return true
443447
}
444448
return false
@@ -509,9 +513,9 @@ type KeybindsConfig struct {
509513
SessionShare string `json:"session_share,required"`
510514
// Unshare current session
511515
SessionUnshare string `json:"session_unshare,required"`
512-
// Switch mode
516+
// Next mode
513517
SwitchMode string `json:"switch_mode,required"`
514-
// Switch mode reverse
518+
// Previous Mode
515519
SwitchModeReverse string `json:"switch_mode_reverse,required"`
516520
// List available themes
517521
ThemeList string `json:"theme_list,required"`
@@ -638,7 +642,9 @@ type McpRemoteConfig struct {
638642
// URL of the remote MCP server
639643
URL string `json:"url,required"`
640644
// Enable or disable the MCP server on startup
641-
Enabled bool `json:"enabled"`
645+
Enabled bool `json:"enabled"`
646+
// Headers to send with the request
647+
Headers map[string]string `json:"headers"`
642648
JSON mcpRemoteConfigJSON `json:"-"`
643649
}
644650

@@ -647,6 +653,7 @@ type mcpRemoteConfigJSON struct {
647653
Type apijson.Field
648654
URL apijson.Field
649655
Enabled apijson.Field
656+
Headers apijson.Field
650657
raw string
651658
ExtraFields map[string]apijson.Field
652659
}

0 commit comments

Comments
 (0)