forked from openai/symphony
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic_tool.ex
More file actions
498 lines (434 loc) · 14.5 KB
/
dynamic_tool.ex
File metadata and controls
498 lines (434 loc) · 14.5 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
defmodule SymphonyElixir.Codex.DynamicTool do
@moduledoc """
Executes client-side tool calls requested by Codex app-server turns.
"""
alias SymphonyElixir.{CodexMonitor.Store, Config, Linear.Client}
@linear_graphql_tool "linear_graphql"
@codex_monitor_task_tool "codex_monitor_task"
@linear_graphql_description """
Execute a raw GraphQL query or mutation against Linear using Symphony's configured auth.
"""
@codex_monitor_task_description """
Read and update the current CodexMonitor task, including board state, worklog entries, and run telemetry.
"""
@linear_graphql_input_schema %{
"type" => "object",
"additionalProperties" => false,
"required" => ["query"],
"properties" => %{
"query" => %{
"type" => "string",
"description" => "GraphQL query or mutation document to execute against Linear."
},
"variables" => %{
"type" => ["object", "null"],
"description" => "Optional GraphQL variables object.",
"additionalProperties" => true
}
}
}
@codex_monitor_task_input_schema %{
"type" => "object",
"additionalProperties" => false,
"required" => ["action"],
"properties" => %{
"action" => %{
"type" => "string",
"enum" => ["get_task", "append_worklog", "update_state", "update_run"],
"description" => "Operation to perform against the current CodexMonitor task."
},
"taskId" => %{
"type" => ["string", "null"],
"description" => "Optional task id override. Defaults to the current issue/task id."
},
"message" => %{
"type" => ["string", "null"],
"description" => "Worklog entry to append for `append_worklog`."
},
"state" => %{
"type" => ["string", "null"],
"description" => "Target state for `update_state`."
},
"threadId" => %{"type" => ["string", "null"]},
"worktreeWorkspaceId" => %{"type" => ["string", "null"]},
"branchName" => %{"type" => ["string", "null"]},
"pullRequestUrl" => %{"type" => ["string", "null"]},
"sessionId" => %{"type" => ["string", "null"]},
"lastEvent" => %{"type" => ["string", "null"]},
"lastMessage" => %{"type" => ["string", "null"]},
"lastError" => %{"type" => ["string", "null"]},
"retryCount" => %{"type" => ["integer", "null"]},
"tokenTotal" => %{"type" => ["integer", "null"]}
}
}
@spec execute(String.t() | nil, term(), keyword()) :: map()
def execute(tool, arguments, opts \\ []) do
case tool do
@linear_graphql_tool ->
execute_linear_graphql(arguments, opts)
@codex_monitor_task_tool ->
execute_codex_monitor_task(arguments, opts)
other ->
failure_response(%{
"error" => %{
"message" => "Unsupported dynamic tool: #{inspect(other)}.",
"supportedTools" => supported_tool_names()
}
})
end
end
@spec tool_specs() :: [map()]
def tool_specs do
case Config.settings!().tracker.kind do
"codex_monitor" ->
[
%{
"name" => @codex_monitor_task_tool,
"description" => @codex_monitor_task_description,
"inputSchema" => @codex_monitor_task_input_schema
}
]
_ ->
[
%{
"name" => @linear_graphql_tool,
"description" => @linear_graphql_description,
"inputSchema" => @linear_graphql_input_schema
}
]
end
end
defp execute_linear_graphql(arguments, opts) do
linear_client = Keyword.get(opts, :linear_client, &Client.graphql/3)
with {:ok, query, variables} <- normalize_linear_graphql_arguments(arguments),
{:ok, response} <- linear_client.(query, variables, []) do
graphql_response(response)
else
{:error, reason} ->
failure_response(tool_error_payload(reason))
end
end
defp execute_codex_monitor_task(arguments, opts) do
store = Keyword.get(opts, :codex_monitor_store, Store)
with {:ok, normalized} <- normalize_codex_monitor_task_arguments(arguments, opts),
{:ok, response} <- execute_codex_monitor_action(store, normalized) do
graphql_response(response)
else
{:error, reason} ->
failure_response(tool_error_payload(reason))
end
end
defp normalize_codex_monitor_task_arguments(arguments, opts) when is_map(arguments) do
with {:ok, action} <- required_string(arguments, "action"),
{:ok, task_id} <- resolve_task_id(arguments, opts, action) do
{:ok,
%{
action: action,
task_id: task_id,
message: optional_string(arguments, "message"),
state: optional_string(arguments, "state"),
run_updates: %{
thread_id: optional_string(arguments, "threadId") || Keyword.get(opts, :thread_id),
worktree_workspace_id: optional_string(arguments, "worktreeWorkspaceId"),
branch_name: optional_string(arguments, "branchName"),
pull_request_url: optional_string(arguments, "pullRequestUrl"),
session_id: optional_string(arguments, "sessionId") || Keyword.get(opts, :session_id),
last_event: optional_string(arguments, "lastEvent"),
last_message: optional_string(arguments, "lastMessage"),
last_error: optional_string(arguments, "lastError"),
retry_count: optional_integer(arguments, "retryCount"),
token_total: optional_integer(arguments, "tokenTotal")
}
}}
end
end
defp normalize_codex_monitor_task_arguments(_arguments, _opts),
do: {:error, :invalid_codex_monitor_task_arguments}
defp execute_codex_monitor_action(store, %{action: "get_task", task_id: task_id}) do
store.get_task_context(task_id)
end
defp execute_codex_monitor_action(store, %{action: "append_worklog", task_id: task_id, message: message}) do
with {:ok, message} <- require_value(message, :missing_codex_monitor_message),
:ok <- store.append_worklog(task_id, message),
{:ok, task_context} <- store.get_task_context(task_id) do
{:ok, task_context}
end
end
defp execute_codex_monitor_action(store, %{action: "update_state", task_id: task_id, state: state, message: message}) do
with {:ok, state} <- require_value(state, :missing_codex_monitor_state),
:ok <- store.update_issue_state(task_id, state),
:ok <- maybe_append_worklog(store, task_id, message),
{:ok, task_context} <- store.get_task_context(task_id) do
{:ok, task_context}
end
end
defp execute_codex_monitor_action(store, %{action: "update_run", task_id: task_id, run_updates: run_updates}) do
with :ok <- store.update_task_run(task_id, prune_nil_map(run_updates)),
{:ok, task_context} <- store.get_task_context(task_id) do
{:ok, task_context}
end
end
defp execute_codex_monitor_action(_store, %{action: action}) do
{:error, {:unsupported_codex_monitor_task_action, action}}
end
defp required_string(arguments, key) do
case optional_string(arguments, key) do
nil -> {:error, {:missing_required_argument, key}}
value -> {:ok, value}
end
end
defp resolve_task_id(arguments, opts, action) do
case optional_string(arguments, "taskId") || issue_id_from_opts(opts) do
nil when action in ["get_task", "append_worklog", "update_state", "update_run"] ->
{:error, :missing_codex_monitor_task_id}
task_id ->
{:ok, task_id}
end
end
defp issue_id_from_opts(opts) do
case Keyword.get(opts, :issue) do
%{id: issue_id} when is_binary(issue_id) -> issue_id
_ -> nil
end
end
defp optional_string(arguments, key) do
case Map.get(arguments, key) || Map.get(arguments, String.to_atom(key)) do
value when is_binary(value) ->
case String.trim(value) do
"" -> nil
trimmed -> trimmed
end
_ ->
nil
end
end
defp optional_integer(arguments, key) do
case Map.get(arguments, key) || Map.get(arguments, String.to_atom(key)) do
value when is_integer(value) ->
value
value when is_binary(value) ->
case Integer.parse(value) do
{parsed, ""} -> parsed
_ -> nil
end
_ ->
nil
end
end
defp require_value(nil, reason), do: {:error, reason}
defp require_value(value, _reason), do: {:ok, value}
defp maybe_append_worklog(_store, _task_id, nil), do: :ok
defp maybe_append_worklog(store, task_id, message), do: store.append_worklog(task_id, message)
defp prune_nil_map(map) when is_map(map) do
Enum.reduce(map, %{}, fn
{_key, nil}, acc -> acc
{key, value}, acc -> Map.put(acc, key, value)
end)
end
defp normalize_linear_graphql_arguments(arguments) when is_binary(arguments) do
case String.trim(arguments) do
"" -> {:error, :missing_query}
query -> {:ok, query, %{}}
end
end
defp normalize_linear_graphql_arguments(arguments) when is_map(arguments) do
case normalize_query(arguments) do
{:ok, query} ->
case normalize_variables(arguments) do
{:ok, variables} ->
{:ok, query, variables}
{:error, reason} ->
{:error, reason}
end
{:error, reason} ->
{:error, reason}
end
end
defp normalize_linear_graphql_arguments(_arguments), do: {:error, :invalid_arguments}
defp normalize_query(arguments) do
case Map.get(arguments, "query") || Map.get(arguments, :query) do
query when is_binary(query) ->
case String.trim(query) do
"" -> {:error, :missing_query}
trimmed -> {:ok, trimmed}
end
_ ->
{:error, :missing_query}
end
end
defp normalize_variables(arguments) do
case Map.get(arguments, "variables") || Map.get(arguments, :variables) || %{} do
variables when is_map(variables) -> {:ok, variables}
_ -> {:error, :invalid_variables}
end
end
defp graphql_response(response) do
success =
case response do
%{"errors" => errors} when is_list(errors) and errors != [] -> false
%{errors: errors} when is_list(errors) and errors != [] -> false
_ -> true
end
dynamic_tool_response(success, encode_payload(response))
end
defp failure_response(payload) do
dynamic_tool_response(false, encode_payload(payload))
end
defp dynamic_tool_response(success, output) when is_boolean(success) and is_binary(output) do
%{
"success" => success,
"output" => output,
"contentItems" => [
%{
"type" => "inputText",
"text" => output
}
]
}
end
defp encode_payload(payload) when is_map(payload) or is_list(payload) do
Jason.encode!(payload, pretty: true)
end
defp encode_payload(payload), do: inspect(payload)
defp tool_error_payload(:missing_query) do
%{
"error" => %{
"message" => "`linear_graphql` requires a non-empty `query` string."
}
}
end
defp tool_error_payload(:invalid_arguments) do
%{
"error" => %{
"message" => "`linear_graphql` expects either a GraphQL query string or an object with `query` and optional `variables`."
}
}
end
defp tool_error_payload(:invalid_variables) do
%{
"error" => %{
"message" => "`linear_graphql.variables` must be a JSON object when provided."
}
}
end
defp tool_error_payload(:missing_linear_api_token) do
%{
"error" => %{
"message" => "Symphony is missing Linear auth. Set `linear.api_key` in `WORKFLOW.md` or export `LINEAR_API_KEY`."
}
}
end
defp tool_error_payload(:missing_codex_monitor_database_path) do
%{
"error" => %{
"message" => "Symphony is missing `tracker.database_path` for the CodexMonitor adapter."
}
}
end
defp tool_error_payload(:missing_codex_monitor_task_id) do
%{
"error" => %{
"message" => "`codex_monitor_task` requires a task id or current issue context."
}
}
end
defp tool_error_payload(:missing_codex_monitor_message) do
%{
"error" => %{
"message" => "`codex_monitor_task` requires `message` for `append_worklog`."
}
}
end
defp tool_error_payload(:missing_codex_monitor_state) do
%{
"error" => %{
"message" => "`codex_monitor_task` requires `state` for `update_state`."
}
}
end
defp tool_error_payload(:invalid_codex_monitor_task_arguments) do
%{
"error" => %{
"message" => "`codex_monitor_task` expects a JSON object with at least an `action` field."
}
}
end
defp tool_error_payload({:missing_required_argument, key}) do
%{
"error" => %{
"message" => "Missing required argument `#{key}`."
}
}
end
defp tool_error_payload({:unsupported_codex_monitor_task_action, action}) do
%{
"error" => %{
"message" => "Unsupported `codex_monitor_task` action #{inspect(action)}."
}
}
end
defp tool_error_payload({:unknown_codex_monitor_state, state_name}) do
%{
"error" => %{
"message" => "Unknown CodexMonitor task state #{inspect(state_name)}."
}
}
end
defp tool_error_payload(:task_not_found) do
%{
"error" => %{
"message" => "The requested CodexMonitor task was not found."
}
}
end
defp tool_error_payload(:sqlite3_not_found) do
%{
"error" => %{
"message" => "The local `sqlite3` binary is required for the CodexMonitor Symphony adapter."
}
}
end
defp tool_error_payload({:sqlite_command_failed, status, detail}) do
%{
"error" => %{
"message" => "CodexMonitor SQLite command failed with status #{status}.",
"detail" => to_string(detail)
}
}
end
defp tool_error_payload({:sqlite_json_decode_failed, reason}) do
%{
"error" => %{
"message" => "Failed to decode CodexMonitor SQLite JSON output.",
"reason" => inspect(reason)
}
}
end
defp tool_error_payload({:linear_api_status, status}) do
%{
"error" => %{
"message" => "Linear GraphQL request failed with HTTP #{status}.",
"status" => status
}
}
end
defp tool_error_payload({:linear_api_request, reason}) do
%{
"error" => %{
"message" => "Linear GraphQL request failed before receiving a successful response.",
"reason" => inspect(reason)
}
}
end
defp tool_error_payload(reason) do
%{
"error" => %{
"message" => "Linear GraphQL tool execution failed.",
"reason" => inspect(reason)
}
}
end
defp supported_tool_names do
Enum.map(tool_specs(), & &1["name"])
end
end