-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathopencode-bridge.prototype.logic.ts
More file actions
115 lines (108 loc) · 3.1 KB
/
opencode-bridge.prototype.logic.ts
File metadata and controls
115 lines (108 loc) · 3.1 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
export interface PrototypeTarget {
directory: string;
workspaceId: string;
baseUrl: string;
username?: string;
password?: string;
}
export interface PrototypeState {
question: string;
target: PrototypeTarget;
selectedProviderID: string;
providers: {
total: number;
connected: string[];
sample: string[];
selected: { id: string } | null;
};
authMethods: Record<string, Array<{ type?: string; label?: string }>>;
lastOAuth: Record<string, unknown> | null;
lastAction: string;
lastError: string | null;
history: string[];
}
export function createInitialState(input: {
question: string;
target: PrototypeTarget;
selectedProviderID?: string;
}): PrototypeState {
return {
question: input.question,
target: input.target,
selectedProviderID: input.selectedProviderID || "openrouter",
providers: {
total: 0,
connected: [],
sample: [],
selected: null,
},
authMethods: {},
lastOAuth: null,
lastAction: "boot",
lastError: null,
history: [],
};
}
export function reducePrototypeState(
state: PrototypeState,
event:
| { type: "selected-provider"; providerID: string }
| {
type: "refreshed";
providers: { all: Array<{ id: string }>; connected: string[] };
authMethods: Record<string, Array<{ type?: string; label?: string }>>;
}
| { type: "oauth-started"; providerID: string; authorization: Record<string, unknown> }
| { type: "action"; message: string }
| { type: "error"; message: string },
): PrototypeState {
if (event.type === "selected-provider") {
return {
...state,
selectedProviderID: event.providerID,
lastAction: `selected ${event.providerID}`,
lastError: null,
history: [...state.history, `selected ${event.providerID}`].slice(-8),
};
}
if (event.type === "refreshed") {
const selected =
event.providers.all.find((provider) => provider.id === state.selectedProviderID) || null;
return {
...state,
providers: {
total: event.providers.all.length,
connected: [...event.providers.connected].sort(),
sample: event.providers.all.slice(0, 12).map((provider) => provider.id),
selected,
},
authMethods: event.authMethods,
lastAction: `refreshed provider state (${event.providers.all.length} providers)`,
lastError: null,
history: [...state.history, `refresh -> ${event.providers.all.length} providers`].slice(-8),
};
}
if (event.type === "oauth-started") {
return {
...state,
selectedProviderID: event.providerID,
lastOAuth: event.authorization,
lastAction: `started oauth for ${event.providerID}`,
lastError: null,
history: [...state.history, `oauth -> ${event.providerID}`].slice(-8),
};
}
if (event.type === "action") {
return {
...state,
lastAction: event.message,
lastError: null,
history: [...state.history, event.message].slice(-8),
};
}
return {
...state,
lastError: event.message,
history: [...state.history, `error -> ${event.message}`].slice(-8),
};
}