deepclause-sdk 0.0.68 → 0.0.70

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/README.md +10 -2
  2. package/dist/agent.d.ts +1 -0
  3. package/dist/agent.d.ts.map +1 -1
  4. package/dist/agent.js +252 -67
  5. package/dist/agent.js.map +1 -1
  6. package/dist/cli/commands.d.ts.map +1 -1
  7. package/dist/cli/commands.js +64 -7
  8. package/dist/cli/commands.js.map +1 -1
  9. package/dist/cli/config.d.ts +15 -12
  10. package/dist/cli/config.d.ts.map +1 -1
  11. package/dist/cli/config.js +38 -5
  12. package/dist/cli/config.js.map +1 -1
  13. package/dist/cli/default-skills.d.ts +1 -0
  14. package/dist/cli/default-skills.d.ts.map +1 -1
  15. package/dist/cli/default-skills.js +31 -0
  16. package/dist/cli/default-skills.js.map +1 -1
  17. package/dist/cli/tui.d.ts +16 -1
  18. package/dist/cli/tui.d.ts.map +1 -1
  19. package/dist/cli/tui.js +293 -21
  20. package/dist/cli/tui.js.map +1 -1
  21. package/dist/prolog/bridge.d.ts +15 -0
  22. package/dist/prolog/bridge.d.ts.map +1 -1
  23. package/dist/prolog/bridge.js +68 -0
  24. package/dist/prolog/bridge.js.map +1 -1
  25. package/dist/prolog-src/deepclause_mi.pl +135 -7
  26. package/dist/runner.d.ts +8 -0
  27. package/dist/runner.d.ts.map +1 -1
  28. package/dist/runner.js +79 -0
  29. package/dist/runner.js.map +1 -1
  30. package/dist/system/assets/docs/DML_COMPILER_PROMPT.md +7 -3
  31. package/dist/system/assets/docs/DML_REFERENCE.md +656 -0
  32. package/dist/system/assets/docs/TASK_PROMPT.md +33 -0
  33. package/dist/system/assets/docs/TUI.md +11 -0
  34. package/dist/system/assets/index.d.ts +12 -9
  35. package/dist/system/assets/index.d.ts.map +1 -1
  36. package/dist/system/assets/index.js +38 -9
  37. package/dist/system/assets/index.js.map +1 -1
  38. package/dist/system/assets/recipes/deepclause-coding-workflow/SKILL.md +56 -1
  39. package/dist/system/assets/skills/plan.dml +259 -0
  40. package/dist/system/runtime/catalog-skills.d.ts +1 -1
  41. package/dist/system/runtime/catalog-skills.d.ts.map +1 -1
  42. package/dist/system/runtime/catalog-skills.js +19 -5
  43. package/dist/system/runtime/catalog-skills.js.map +1 -1
  44. package/dist/system/runtime/conductor.d.ts +5 -1
  45. package/dist/system/runtime/conductor.d.ts.map +1 -1
  46. package/dist/system/runtime/conductor.js +22 -0
  47. package/dist/system/runtime/conductor.js.map +1 -1
  48. package/dist/system/runtime/dml-executor.d.ts +1 -1
  49. package/dist/system/runtime/dml-executor.d.ts.map +1 -1
  50. package/dist/system/runtime/dml-executor.js +2 -14
  51. package/dist/system/runtime/dml-executor.js.map +1 -1
  52. package/dist/system/runtime/dml-params.d.ts +2 -2
  53. package/dist/system/runtime/dml-params.d.ts.map +1 -1
  54. package/dist/system/runtime/dml-params.js +3 -0
  55. package/dist/system/runtime/dml-params.js.map +1 -1
  56. package/dist/system/runtime/runtime-tools.d.ts +1 -0
  57. package/dist/system/runtime/runtime-tools.d.ts.map +1 -1
  58. package/dist/system/runtime/runtime-tools.js +95 -0
  59. package/dist/system/runtime/runtime-tools.js.map +1 -1
  60. package/dist/system/runtime/shell-manager.js +1 -1
  61. package/dist/system/runtime/shell-manager.js.map +1 -1
  62. package/dist/system/runtime/skill-creator.d.ts.map +1 -1
  63. package/dist/system/runtime/skill-creator.js +52 -21
  64. package/dist/system/runtime/skill-creator.js.map +1 -1
  65. package/package.json +4 -3
  66. package/src/prolog-src/deepclause_mi.pl +135 -7
@@ -22,6 +22,7 @@
22
22
  destroy_engine/1,
23
23
  post_agent_result/4,
24
24
  post_exec_result/3,
25
+ post_sample_token_result/3,
25
26
  provide_input/2,
26
27
  mi/3,
27
28
  % Tool engine predicates for isolated tool execution
@@ -46,6 +47,7 @@
46
47
  :- dynamic session_pending_input/2. % session_pending_input(SessionId, Input)
47
48
  :- dynamic session_agent_result/2. % session_agent_result(SessionId, Result)
48
49
  :- dynamic session_exec_result/2. % session_exec_result(SessionId, Result)
50
+ :- dynamic session_sample_token_result/2. % session_sample_token_result(SessionId, Result)
49
51
  :- dynamic session_trace_log/2. % session_trace_log(SessionId, TraceLog) - accumulated trace entries
50
52
  :- dynamic session_trace_enabled/2. % session_trace_enabled(SessionId, true/false)
51
53
  :- dynamic session_pending_signal/2. % session_pending_signal(SessionId, Signal) - fallback for engine_fetch
@@ -96,14 +98,40 @@ parse_clauses(Stream, SessionId) :-
96
98
  read_term(Stream, Term, [module(SessionId), variable_names(Bindings)]),
97
99
  ( Term == end_of_file
98
100
  -> true
99
- ; % First, transform task/N calls to include variable names
100
- transform_task_calls(Term, Bindings, TransformedTerm),
101
- % Then expand string interpolations
102
- expand_interpolations(TransformedTerm, Bindings, ExpandedTerm),
103
- process_clause(ExpandedTerm, SessionId),
101
+ ; preprocess_loaded_term(Term, Bindings, PreparedTerms),
102
+ forall(member(PreparedTerm, PreparedTerms), process_clause(PreparedTerm, SessionId)),
104
103
  parse_clauses(Stream, SessionId)
105
104
  ).
106
105
 
106
+ %% preprocess_loaded_term(+Term, +Bindings, -PreparedTerms)
107
+ %% Apply DML source preprocessing before asserting loaded clauses.
108
+ %% This keeps task()/prompt() rewriting and string interpolation working,
109
+ %% while also running expand_term/2 so DCG rules (`-->`) become executable
110
+ %% difference-list predicates before they are asserted into the session module.
111
+ preprocess_loaded_term(Term, Bindings, PreparedTerms) :-
112
+ transform_task_calls(Term, Bindings, TransformedTerm),
113
+ expand_interpolations(TransformedTerm, Bindings, InterpolatedTerm),
114
+ expand_loaded_term(InterpolatedTerm, PreparedTerms).
115
+
116
+ %% expand_loaded_term(+Term, -ExpandedTerms)
117
+ %% Runs SWI term expansion and normalizes the result to a list of terms.
118
+ expand_loaded_term(Term, ExpandedTerms) :-
119
+ expand_term(Term, Expanded),
120
+ normalize_expanded_terms(Expanded, ExpandedTerms).
121
+
122
+ %% normalize_expanded_terms(+Expanded, -Terms)
123
+ %% expand_term/2 may return a single term or a list. DML does not need the
124
+ %% generated non_terminal/1 directives, so skip them and keep the executable
125
+ %% clauses only.
126
+ normalize_expanded_terms(Expanded, Terms) :-
127
+ ( is_list(Expanded)
128
+ -> RawTerms = Expanded
129
+ ; RawTerms = [Expanded]
130
+ ),
131
+ exclude(skip_expanded_term, RawTerms, Terms).
132
+
133
+ skip_expanded_term((:- non_terminal(_))).
134
+
107
135
  %% ============================================================
108
136
  %% Task Call Transformation (compile-time)
109
137
  %% ============================================================
@@ -535,6 +563,8 @@ process_engine_result(request_agent_loop(Desc, Vars, Tools, Memory), request_age
535
563
  payload{taskDescription: Desc, outputVars: Vars, userTools: Tools, memory: Memory, toolScope: none}) :- !.
536
564
  process_engine_result(request_exec(Tool, Args), request_exec, '',
537
565
  payload{toolName: Tool, args: Args}) :- !.
566
+ process_engine_result(request_sample_token(Prompt, Allowed), request_sample_token, '',
567
+ payload{prompt: Prompt, allowedTokens: Allowed}) :- !.
538
568
  %% Tool result from inline tool execution
539
569
  process_engine_result(tool_result(Result), tool_result, '',
540
570
  payload{result: Result}) :- !.
@@ -556,6 +586,7 @@ destroy_engine(SessionId) :-
556
586
  retractall(session_pending_input(SessionId, _)),
557
587
  retractall(session_agent_result(SessionId, _)),
558
588
  retractall(session_exec_result(SessionId, _)),
589
+ retractall(session_sample_token_result(SessionId, _)),
559
590
  retractall(session_trace_log(SessionId, _)),
560
591
  retractall(session_trace_enabled(SessionId, _)),
561
592
  ( current_module(SessionId)
@@ -596,6 +627,12 @@ post_exec_result(SessionId, Status, Result) :-
596
627
  assertz(session_exec_result(SessionId, result{status: Status, result: Result})),
597
628
  post_signal_to_engine(SessionId, exec_done).
598
629
 
630
+ %% post_sample_token_result(+SessionId, +Status, +TokenOrError)
631
+ post_sample_token_result(SessionId, Status, TokenOrError) :-
632
+ retractall(session_sample_token_result(SessionId, _)),
633
+ assertz(session_sample_token_result(SessionId, result{status: Status, token: TokenOrError})),
634
+ post_signal_to_engine(SessionId, sample_token_done).
635
+
599
636
  %% provide_input(+SessionId, +Input)
600
637
  provide_input(SessionId, Input) :-
601
638
  assertz(session_pending_input(SessionId, Input)),
@@ -924,6 +961,8 @@ step_tool_engine(ToolEngineId, Status, Content, Payload) :-
924
961
  process_tool_engine_result(tool_finished(ToolResult), finished, '', payload{result: ToolResult}) :- !.
925
962
  process_tool_engine_result(request_exec(Tool, Args), request_exec, '',
926
963
  payload{toolName: Tool, args: Args}) :- !.
964
+ process_tool_engine_result(request_sample_token(Prompt, Allowed), request_sample_token, '',
965
+ payload{prompt: Prompt, allowedTokens: Allowed}) :- !.
927
966
  process_tool_engine_result(request_agent_loop(Desc, Vars, Tools, Memory, ToolScope), request_agent_loop, '',
928
967
  payload{taskDescription: Desc, outputVars: Vars, userTools: Tools, memory: Memory, toolScope: ToolScope}) :- !.
929
968
  %% Legacy 4-arg format (no tool scope)
@@ -1076,6 +1115,18 @@ mi_call(task_named(Desc, Vars, VarNames), StateIn, StateOut) :-
1076
1115
  %% Prompt Handling - Fresh LLM call without existing memory
1077
1116
  %% ============================================================
1078
1117
 
1118
+ %% mi_call(sample_token(Desc, Token), +StateIn, -StateOut)
1119
+ %% Ask the host runtime for exactly one token via the provider.
1120
+ mi_call(sample_token(Desc, Token), StateIn, StateOut) :-
1121
+ !,
1122
+ mi_call_sample_token(Desc, none, Token, StateIn, StateOut).
1123
+
1124
+ %% mi_call(sample_token(Desc, Allowed, Token), +StateIn, -StateOut)
1125
+ %% Same as sample_token/2, but provides an allowed token list to runtime.
1126
+ mi_call(sample_token(Desc, Allowed, Token), StateIn, StateOut) :-
1127
+ !,
1128
+ mi_call_sample_token(Desc, Allowed, Token, StateIn, StateOut).
1129
+
1079
1130
  %% mi_call(prompt(Desc), +StateIn, -StateOut)
1080
1131
  %% Simple LLM call with empty memory and NO tools (pure text completion)
1081
1132
  mi_call(prompt(Desc), StateIn, StateOut) :-
@@ -1142,6 +1193,42 @@ mi_call_prompt_n(Desc, Vars, VarNames, StateIn, StateOut) :-
1142
1193
  bind_task_variables(Result.variables, VarNames, Vars).
1143
1194
  % Don't modify memory - but state may have changed from tool calls
1144
1195
 
1196
+ mi_call_sample_token(Desc, Allowed, Token, StateIn, StateOut) :-
1197
+ get_params(StateIn, Params),
1198
+ interpolate_desc(Desc, Params, InterpDesc),
1199
+ get_session_id(SessionId),
1200
+ get_depth(StateIn, Depth),
1201
+ add_trace_entry(SessionId, llm_call, sample_token, [InterpDesc, Allowed], Depth),
1202
+ engine_yield(request_sample_token(InterpDesc, Allowed)),
1203
+ wait_sample_token_signal(SessionId),
1204
+ ( session_sample_token_result(SessionId, Result)
1205
+ -> retract(session_sample_token_result(SessionId, _)),
1206
+ ( Result.status == success
1207
+ -> Token = Result.token,
1208
+ add_trace_with_result(SessionId, exit, sample_token, [InterpDesc, Allowed], Token, Depth),
1209
+ StateOut = StateIn
1210
+ ; ( get_dict(token, Result, ErrorMsg), ErrorMsg \= ""
1211
+ -> format(atom(WarnMsg), 'Warning: sample_token failed: ~w', [ErrorMsg])
1212
+ ; format(atom(WarnMsg), 'Warning: sample_token failed (status=~w)', [Result.status])
1213
+ ),
1214
+ add_trace_entry(SessionId, fail, sample_token, [InterpDesc, Allowed], Depth),
1215
+ engine_yield(output(WarnMsg)),
1216
+ fail
1217
+ )
1218
+ ; add_trace_entry(SessionId, fail, sample_token, [InterpDesc, Allowed], Depth),
1219
+ engine_yield(output('Warning: sample_token failed (no result returned)')),
1220
+ fail
1221
+ ).
1222
+
1223
+ wait_sample_token_signal(SessionId) :-
1224
+ get_next_signal(SessionId, Signal),
1225
+ ( Signal == sample_token_done
1226
+ -> true
1227
+ ; Signal = error_signal(_)
1228
+ -> true
1229
+ ; wait_sample_token_signal(SessionId)
1230
+ ).
1231
+
1145
1232
  %% mi_call_task_n(+Desc, +Vars, +VarNames, +StateIn, -StateOut)
1146
1233
  mi_call_task_n(Desc, Vars, VarNames, StateIn, StateOut) :-
1147
1234
  !, % Cut to make this predicate deterministic - no backtracking once started
@@ -1695,10 +1782,11 @@ consult_string(Code, SessionId) :-
1695
1782
  %% consult_clauses(+Stream, +SessionId)
1696
1783
  %% Read and assert all clauses from a stream
1697
1784
  consult_clauses(Stream, SessionId) :-
1698
- read_term(Stream, Term, [module(SessionId)]),
1785
+ read_term(Stream, Term, [module(SessionId), variable_names(Bindings)]),
1699
1786
  ( Term == end_of_file
1700
1787
  -> true
1701
- ; consult_process_term(Term, SessionId),
1788
+ ; preprocess_loaded_term(Term, Bindings, PreparedTerms),
1789
+ forall(member(PreparedTerm, PreparedTerms), consult_process_term(PreparedTerm, SessionId)),
1702
1790
  consult_clauses(Stream, SessionId)
1703
1791
  ).
1704
1792
 
@@ -1823,6 +1911,12 @@ mi_call(make_directory(Dir), StateIn, StateIn) :-
1823
1911
  resolve_workspace_path(Dir, FullPath),
1824
1912
  make_directory(FullPath).
1825
1913
 
1914
+ %% mi_call(make_directory_path(Dir), +StateIn, -StateOut)
1915
+ mi_call(make_directory_path(Dir), StateIn, StateIn) :-
1916
+ !,
1917
+ resolve_workspace_path(Dir, FullPath),
1918
+ make_directory_path(FullPath).
1919
+
1826
1920
  %% mi_call(delete_file(File), +StateIn, -StateOut)
1827
1921
  mi_call(delete_file(File), StateIn, StateIn) :-
1828
1922
  !,
@@ -1852,6 +1946,22 @@ mi_call(throw(Error), _StateIn, _StateOut) :-
1852
1946
  !,
1853
1947
  throw(Error).
1854
1948
 
1949
+ %% ============================================================
1950
+ %% DCG Helpers
1951
+ %% ============================================================
1952
+
1953
+ %% phrase/2 and phrase/3 must run in the session module so they can resolve
1954
+ %% grammar predicates asserted from the current DML source.
1955
+ mi_call(phrase(Grammar, Input), StateIn, StateIn) :-
1956
+ !,
1957
+ get_session_id(SessionId),
1958
+ call(SessionId:phrase(Grammar, Input)).
1959
+
1960
+ mi_call(phrase(Grammar, Input, Rest), StateIn, StateIn) :-
1961
+ !,
1962
+ get_session_id(SessionId),
1963
+ call(SessionId:phrase(Grammar, Input, Rest)).
1964
+
1855
1965
  %% ============================================================
1856
1966
  %% Module-Qualified Goals
1857
1967
  %% ============================================================
@@ -1859,6 +1969,14 @@ mi_call(throw(Error), _StateIn, _StateOut) :-
1859
1969
  %% mi_call(Module:Goal, +StateIn, -StateOut)
1860
1970
  %% Module-qualified goals - handle special predicates and user-defined predicates
1861
1971
 
1972
+ mi_call(Module:phrase(Grammar, Input), StateIn, StateIn) :-
1973
+ !,
1974
+ call(Module:phrase(Grammar, Input)).
1975
+
1976
+ mi_call(Module:phrase(Grammar, Input, Rest), StateIn, StateIn) :-
1977
+ !,
1978
+ call(Module:phrase(Grammar, Input, Rest)).
1979
+
1862
1980
  % First check if the unqualified Goal is a special MI predicate - if so, handle it directly
1863
1981
  mi_call(_Module:Goal, StateIn, StateOut) :-
1864
1982
  is_mi_special_predicate(Goal),
@@ -1898,6 +2016,8 @@ is_mi_special_predicate(task(_,_,_)).
1898
2016
  is_mi_special_predicate(task(_,_,_,_)).
1899
2017
  is_mi_special_predicate(task(_,_,_,_,_)).
1900
2018
  is_mi_special_predicate(task_named(_,_,_)). %% Transformed form of task/N
2019
+ is_mi_special_predicate(sample_token(_,_)).
2020
+ is_mi_special_predicate(sample_token(_,_,_)).
1901
2021
  is_mi_special_predicate(exec(_,_)).
1902
2022
  is_mi_special_predicate(param(_,_)).
1903
2023
  is_mi_special_predicate(param(_,_,_)).
@@ -1951,6 +2071,9 @@ is_mi_special_predicate(maplist(_,_)).
1951
2071
  is_mi_special_predicate(maplist(_,_,_)).
1952
2072
  is_mi_special_predicate(maplist(_,_,_,_)).
1953
2073
  is_mi_special_predicate(foldl(_,_,_,_)).
2074
+ %% DCG predicates
2075
+ is_mi_special_predicate(phrase(_,_)).
2076
+ is_mi_special_predicate(phrase(_,_,_)).
1954
2077
  %% File I/O predicates
1955
2078
  is_mi_special_predicate(read_file_to_string(_,_,_)).
1956
2079
  is_mi_special_predicate(read_file(_,_)).
@@ -1964,6 +2087,7 @@ is_mi_special_predicate(exists_file(_)).
1964
2087
  is_mi_special_predicate(exists_directory(_)).
1965
2088
  is_mi_special_predicate(directory_files(_,_)).
1966
2089
  is_mi_special_predicate(make_directory(_)).
2090
+ is_mi_special_predicate(make_directory_path(_)).
1967
2091
  is_mi_special_predicate(delete_file(_)).
1968
2092
  is_mi_special_predicate(delete_directory(_)).
1969
2093
  %% Consult and module predicates
@@ -2234,6 +2358,10 @@ goal_with_string_arg(prompt(S, V1, V2, V3), prompt, S, [V1, V2, V3]) :- atom(S),
2234
2358
  %% prompt_named/3 - transformed form with embedded variable names
2235
2359
  goal_with_string_arg(prompt_named(S, Vars, Names), prompt_named, S, [Vars, Names]) :- string(S).
2236
2360
  goal_with_string_arg(prompt_named(S, Vars, Names), prompt_named, S, [Vars, Names]) :- atom(S), \+ S = [].
2361
+ goal_with_string_arg(sample_token(S, Token), sample_token, S, [Token]) :- string(S).
2362
+ goal_with_string_arg(sample_token(S, Token), sample_token, S, [Token]) :- atom(S), \+ S = [].
2363
+ goal_with_string_arg(sample_token(S, Allowed, Token), sample_token, S, [Allowed, Token]) :- string(S).
2364
+ goal_with_string_arg(sample_token(S, Allowed, Token), sample_token, S, [Allowed, Token]) :- atom(S), \+ S = [].
2237
2365
 
2238
2366
  %% rebuild_goal(+Functor, +StringArg, +RestArgs, -Goal)
2239
2367
  %% Rebuild a goal with the string argument and rest args