@positronic/spec 0.0.76 → 0.0.78
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.
- package/dist/api/brains.d.ts +0 -24
- package/dist/api/brains.d.ts.map +1 -1
- package/dist/api/brains.js +0 -186
- package/dist/api/brains.js.map +1 -1
- package/dist/api/webhooks.d.ts +25 -12
- package/dist/api/webhooks.d.ts.map +1 -1
- package/dist/api/webhooks.js +101 -28
- package/dist/api/webhooks.js.map +1 -1
- package/dist/src/api/brains.js +0 -386
- package/dist/src/api/webhooks.js +188 -32
- package/package.json +1 -1
package/dist/src/api/brains.js
CHANGED
|
@@ -1794,392 +1794,6 @@ export var brains = {
|
|
|
1794
1794
|
});
|
|
1795
1795
|
})();
|
|
1796
1796
|
},
|
|
1797
|
-
watchAgentEvents: /**
|
|
1798
|
-
* Test that agent steps emit proper AGENT_* events in the SSE stream.
|
|
1799
|
-
* Requires a brain with an agent step that will pause on a webhook.
|
|
1800
|
-
*
|
|
1801
|
-
* Expected events before webhook pause:
|
|
1802
|
-
* - AGENT_START (with prompt and optional system)
|
|
1803
|
-
* - AGENT_ITERATION
|
|
1804
|
-
* - AGENT_TOOL_CALL
|
|
1805
|
-
* - AGENT_WEBHOOK (before WEBHOOK event)
|
|
1806
|
-
* - WEBHOOK
|
|
1807
|
-
*/ function watchAgentEvents(fetch, agentBrainIdentifier) {
|
|
1808
|
-
return _async_to_generator(function() {
|
|
1809
|
-
var brainRunId, watchRequest, watchResponse, events, _tmp, hasAgentStart, agentStartEvent, hasAgentIteration, hasAgentToolCall, webhookIndex, agentWebhookIndex, agentWebhookEvent, error;
|
|
1810
|
-
return _ts_generator(this, function(_state) {
|
|
1811
|
-
switch(_state.label){
|
|
1812
|
-
case 0:
|
|
1813
|
-
_state.trys.push([
|
|
1814
|
-
0,
|
|
1815
|
-
6,
|
|
1816
|
-
,
|
|
1817
|
-
7
|
|
1818
|
-
]);
|
|
1819
|
-
return [
|
|
1820
|
-
4,
|
|
1821
|
-
startBrainRun(fetch, agentBrainIdentifier)
|
|
1822
|
-
];
|
|
1823
|
-
case 1:
|
|
1824
|
-
brainRunId = _state.sent();
|
|
1825
|
-
if (!brainRunId) return [
|
|
1826
|
-
2,
|
|
1827
|
-
false
|
|
1828
|
-
];
|
|
1829
|
-
// Watch the brain run
|
|
1830
|
-
watchRequest = new Request("http://example.com/brains/runs/".concat(brainRunId, "/watch"), {
|
|
1831
|
-
method: 'GET'
|
|
1832
|
-
});
|
|
1833
|
-
return [
|
|
1834
|
-
4,
|
|
1835
|
-
fetch(watchRequest)
|
|
1836
|
-
];
|
|
1837
|
-
case 2:
|
|
1838
|
-
watchResponse = _state.sent();
|
|
1839
|
-
if (!watchResponse.ok) {
|
|
1840
|
-
console.error("GET /brains/runs/".concat(brainRunId, "/watch returned ").concat(watchResponse.status));
|
|
1841
|
-
return [
|
|
1842
|
-
2,
|
|
1843
|
-
false
|
|
1844
|
-
];
|
|
1845
|
-
}
|
|
1846
|
-
if (!watchResponse.body) return [
|
|
1847
|
-
3,
|
|
1848
|
-
4
|
|
1849
|
-
];
|
|
1850
|
-
return [
|
|
1851
|
-
4,
|
|
1852
|
-
readSseUntil(watchResponse.body, function(event) {
|
|
1853
|
-
return event.type === BRAIN_EVENTS.WEBHOOK || event.type === BRAIN_EVENTS.COMPLETE || event.type === BRAIN_EVENTS.ERROR;
|
|
1854
|
-
})
|
|
1855
|
-
];
|
|
1856
|
-
case 3:
|
|
1857
|
-
_tmp = _state.sent();
|
|
1858
|
-
return [
|
|
1859
|
-
3,
|
|
1860
|
-
5
|
|
1861
|
-
];
|
|
1862
|
-
case 4:
|
|
1863
|
-
_tmp = [];
|
|
1864
|
-
_state.label = 5;
|
|
1865
|
-
case 5:
|
|
1866
|
-
events = _tmp;
|
|
1867
|
-
// Verify required agent events are present
|
|
1868
|
-
hasAgentStart = events.some(function(e) {
|
|
1869
|
-
return e.type === BRAIN_EVENTS.AGENT_START;
|
|
1870
|
-
});
|
|
1871
|
-
if (!hasAgentStart) {
|
|
1872
|
-
console.error('Missing AGENT_START event in SSE stream');
|
|
1873
|
-
return [
|
|
1874
|
-
2,
|
|
1875
|
-
false
|
|
1876
|
-
];
|
|
1877
|
-
}
|
|
1878
|
-
// Verify AGENT_START has prompt field
|
|
1879
|
-
agentStartEvent = events.find(function(e) {
|
|
1880
|
-
return e.type === BRAIN_EVENTS.AGENT_START;
|
|
1881
|
-
});
|
|
1882
|
-
if (!agentStartEvent.prompt || typeof agentStartEvent.prompt !== 'string') {
|
|
1883
|
-
console.error('AGENT_START event missing prompt field');
|
|
1884
|
-
return [
|
|
1885
|
-
2,
|
|
1886
|
-
false
|
|
1887
|
-
];
|
|
1888
|
-
}
|
|
1889
|
-
hasAgentIteration = events.some(function(e) {
|
|
1890
|
-
return e.type === BRAIN_EVENTS.AGENT_ITERATION;
|
|
1891
|
-
});
|
|
1892
|
-
if (!hasAgentIteration) {
|
|
1893
|
-
console.error('Missing AGENT_ITERATION event in SSE stream');
|
|
1894
|
-
return [
|
|
1895
|
-
2,
|
|
1896
|
-
false
|
|
1897
|
-
];
|
|
1898
|
-
}
|
|
1899
|
-
hasAgentToolCall = events.some(function(e) {
|
|
1900
|
-
return e.type === BRAIN_EVENTS.AGENT_TOOL_CALL;
|
|
1901
|
-
});
|
|
1902
|
-
if (!hasAgentToolCall) {
|
|
1903
|
-
console.error('Missing AGENT_TOOL_CALL event in SSE stream');
|
|
1904
|
-
return [
|
|
1905
|
-
2,
|
|
1906
|
-
false
|
|
1907
|
-
];
|
|
1908
|
-
}
|
|
1909
|
-
// If we got a WEBHOOK event, verify AGENT_WEBHOOK came before it
|
|
1910
|
-
webhookIndex = events.findIndex(function(e) {
|
|
1911
|
-
return e.type === BRAIN_EVENTS.WEBHOOK;
|
|
1912
|
-
});
|
|
1913
|
-
if (webhookIndex !== -1) {
|
|
1914
|
-
agentWebhookIndex = events.findIndex(function(e) {
|
|
1915
|
-
return e.type === BRAIN_EVENTS.AGENT_WEBHOOK;
|
|
1916
|
-
});
|
|
1917
|
-
if (agentWebhookIndex === -1) {
|
|
1918
|
-
console.error('Missing AGENT_WEBHOOK event before WEBHOOK event');
|
|
1919
|
-
return [
|
|
1920
|
-
2,
|
|
1921
|
-
false
|
|
1922
|
-
];
|
|
1923
|
-
}
|
|
1924
|
-
if (agentWebhookIndex >= webhookIndex) {
|
|
1925
|
-
console.error('AGENT_WEBHOOK event must come before WEBHOOK event');
|
|
1926
|
-
return [
|
|
1927
|
-
2,
|
|
1928
|
-
false
|
|
1929
|
-
];
|
|
1930
|
-
}
|
|
1931
|
-
// Verify AGENT_WEBHOOK has required fields
|
|
1932
|
-
agentWebhookEvent = events[agentWebhookIndex];
|
|
1933
|
-
if (!agentWebhookEvent.toolCallId || !agentWebhookEvent.toolName) {
|
|
1934
|
-
console.error('AGENT_WEBHOOK event missing toolCallId or toolName fields');
|
|
1935
|
-
return [
|
|
1936
|
-
2,
|
|
1937
|
-
false
|
|
1938
|
-
];
|
|
1939
|
-
}
|
|
1940
|
-
}
|
|
1941
|
-
return [
|
|
1942
|
-
2,
|
|
1943
|
-
true
|
|
1944
|
-
];
|
|
1945
|
-
case 6:
|
|
1946
|
-
error = _state.sent();
|
|
1947
|
-
console.error("Failed to test agent events for ".concat(agentBrainIdentifier, ":"), error);
|
|
1948
|
-
return [
|
|
1949
|
-
2,
|
|
1950
|
-
false
|
|
1951
|
-
];
|
|
1952
|
-
case 7:
|
|
1953
|
-
return [
|
|
1954
|
-
2
|
|
1955
|
-
];
|
|
1956
|
-
}
|
|
1957
|
-
});
|
|
1958
|
-
})();
|
|
1959
|
-
},
|
|
1960
|
-
agentWebhookResume: /**
|
|
1961
|
-
* Test full agent webhook resumption flow:
|
|
1962
|
-
* 1. Start an agent brain that will pause on a webhook
|
|
1963
|
-
* 2. Verify it pauses with WEBHOOK event
|
|
1964
|
-
* 3. Trigger the webhook with a response
|
|
1965
|
-
* 4. Verify the brain resumes and emits WEBHOOK_RESPONSE and AGENT_TOOL_RESULT
|
|
1966
|
-
*
|
|
1967
|
-
* Requires:
|
|
1968
|
-
* - A brain with an agent step that calls a tool returning { waitFor: webhook(...) }
|
|
1969
|
-
* - The webhook slug and identifier to trigger
|
|
1970
|
-
*/ function agentWebhookResume(fetch, agentBrainIdentifier, webhookSlug, webhookPayload) {
|
|
1971
|
-
return _async_to_generator(function() {
|
|
1972
|
-
var brainRunId, watchRequest, watchResponse, earlyTermination, watchEvents, foundWebhookEvent, webhookRequest, webhookResponse, webhookResult, resumeWatchRequest, resumeWatchResponse, resumeEvents, _tmp, hasWebhookResponse, hasAgentToolResult, completeEvent, error;
|
|
1973
|
-
return _ts_generator(this, function(_state) {
|
|
1974
|
-
switch(_state.label){
|
|
1975
|
-
case 0:
|
|
1976
|
-
_state.trys.push([
|
|
1977
|
-
0,
|
|
1978
|
-
10,
|
|
1979
|
-
,
|
|
1980
|
-
11
|
|
1981
|
-
]);
|
|
1982
|
-
return [
|
|
1983
|
-
4,
|
|
1984
|
-
startBrainRun(fetch, agentBrainIdentifier)
|
|
1985
|
-
];
|
|
1986
|
-
case 1:
|
|
1987
|
-
brainRunId = _state.sent();
|
|
1988
|
-
if (!brainRunId) return [
|
|
1989
|
-
2,
|
|
1990
|
-
false
|
|
1991
|
-
];
|
|
1992
|
-
// Step 2: Watch until WEBHOOK event (brain pauses)
|
|
1993
|
-
watchRequest = new Request("http://example.com/brains/runs/".concat(brainRunId, "/watch"), {
|
|
1994
|
-
method: 'GET'
|
|
1995
|
-
});
|
|
1996
|
-
return [
|
|
1997
|
-
4,
|
|
1998
|
-
fetch(watchRequest)
|
|
1999
|
-
];
|
|
2000
|
-
case 2:
|
|
2001
|
-
watchResponse = _state.sent();
|
|
2002
|
-
if (!watchResponse.ok) {
|
|
2003
|
-
console.error("GET /brains/runs/".concat(brainRunId, "/watch returned ").concat(watchResponse.status));
|
|
2004
|
-
return [
|
|
2005
|
-
2,
|
|
2006
|
-
false
|
|
2007
|
-
];
|
|
2008
|
-
}
|
|
2009
|
-
if (!watchResponse.body) {
|
|
2010
|
-
console.error('Watch response has no body');
|
|
2011
|
-
return [
|
|
2012
|
-
2,
|
|
2013
|
-
false
|
|
2014
|
-
];
|
|
2015
|
-
}
|
|
2016
|
-
earlyTermination = false;
|
|
2017
|
-
return [
|
|
2018
|
-
4,
|
|
2019
|
-
readSseUntil(watchResponse.body, function(event) {
|
|
2020
|
-
if (event.type === BRAIN_EVENTS.WEBHOOK) return true;
|
|
2021
|
-
if (event.type === BRAIN_EVENTS.COMPLETE || event.type === BRAIN_EVENTS.ERROR) {
|
|
2022
|
-
console.error("Brain completed/errored before WEBHOOK event: ".concat(event.type));
|
|
2023
|
-
earlyTermination = true;
|
|
2024
|
-
return true;
|
|
2025
|
-
}
|
|
2026
|
-
return false;
|
|
2027
|
-
})
|
|
2028
|
-
];
|
|
2029
|
-
case 3:
|
|
2030
|
-
watchEvents = _state.sent();
|
|
2031
|
-
if (earlyTermination) return [
|
|
2032
|
-
2,
|
|
2033
|
-
false
|
|
2034
|
-
];
|
|
2035
|
-
foundWebhookEvent = watchEvents.some(function(e) {
|
|
2036
|
-
return e.type === BRAIN_EVENTS.WEBHOOK;
|
|
2037
|
-
});
|
|
2038
|
-
if (!foundWebhookEvent) {
|
|
2039
|
-
console.error('Brain did not emit WEBHOOK event');
|
|
2040
|
-
return [
|
|
2041
|
-
2,
|
|
2042
|
-
false
|
|
2043
|
-
];
|
|
2044
|
-
}
|
|
2045
|
-
// Step 3: Trigger the webhook
|
|
2046
|
-
webhookRequest = new Request("http://example.com/webhooks/".concat(encodeURIComponent(webhookSlug)), {
|
|
2047
|
-
method: 'POST',
|
|
2048
|
-
headers: {
|
|
2049
|
-
'Content-Type': 'application/json'
|
|
2050
|
-
},
|
|
2051
|
-
body: JSON.stringify(webhookPayload)
|
|
2052
|
-
});
|
|
2053
|
-
return [
|
|
2054
|
-
4,
|
|
2055
|
-
fetch(webhookRequest)
|
|
2056
|
-
];
|
|
2057
|
-
case 4:
|
|
2058
|
-
webhookResponse = _state.sent();
|
|
2059
|
-
if (!webhookResponse.ok) {
|
|
2060
|
-
console.error("POST /webhooks/".concat(webhookSlug, " returned ").concat(webhookResponse.status));
|
|
2061
|
-
return [
|
|
2062
|
-
2,
|
|
2063
|
-
false
|
|
2064
|
-
];
|
|
2065
|
-
}
|
|
2066
|
-
return [
|
|
2067
|
-
4,
|
|
2068
|
-
webhookResponse.json()
|
|
2069
|
-
];
|
|
2070
|
-
case 5:
|
|
2071
|
-
webhookResult = _state.sent();
|
|
2072
|
-
if (!webhookResult.received) {
|
|
2073
|
-
console.error('Webhook was not received');
|
|
2074
|
-
return [
|
|
2075
|
-
2,
|
|
2076
|
-
false
|
|
2077
|
-
];
|
|
2078
|
-
}
|
|
2079
|
-
if (webhookResult.action !== 'resumed') {
|
|
2080
|
-
console.error("Expected webhook action 'resumed', got '".concat(webhookResult.action, "'"));
|
|
2081
|
-
return [
|
|
2082
|
-
2,
|
|
2083
|
-
false
|
|
2084
|
-
];
|
|
2085
|
-
}
|
|
2086
|
-
// Step 4: Watch again for resumed events
|
|
2087
|
-
resumeWatchRequest = new Request("http://example.com/brains/runs/".concat(brainRunId, "/watch"), {
|
|
2088
|
-
method: 'GET'
|
|
2089
|
-
});
|
|
2090
|
-
return [
|
|
2091
|
-
4,
|
|
2092
|
-
fetch(resumeWatchRequest)
|
|
2093
|
-
];
|
|
2094
|
-
case 6:
|
|
2095
|
-
resumeWatchResponse = _state.sent();
|
|
2096
|
-
if (!resumeWatchResponse.ok) {
|
|
2097
|
-
console.error("GET /brains/runs/".concat(brainRunId, "/watch (resume) returned ").concat(resumeWatchResponse.status));
|
|
2098
|
-
return [
|
|
2099
|
-
2,
|
|
2100
|
-
false
|
|
2101
|
-
];
|
|
2102
|
-
}
|
|
2103
|
-
if (!resumeWatchResponse.body) return [
|
|
2104
|
-
3,
|
|
2105
|
-
8
|
|
2106
|
-
];
|
|
2107
|
-
return [
|
|
2108
|
-
4,
|
|
2109
|
-
readSseUntil(resumeWatchResponse.body, function(event) {
|
|
2110
|
-
return event.type === BRAIN_EVENTS.COMPLETE || event.type === BRAIN_EVENTS.ERROR;
|
|
2111
|
-
})
|
|
2112
|
-
];
|
|
2113
|
-
case 7:
|
|
2114
|
-
_tmp = _state.sent();
|
|
2115
|
-
return [
|
|
2116
|
-
3,
|
|
2117
|
-
9
|
|
2118
|
-
];
|
|
2119
|
-
case 8:
|
|
2120
|
-
_tmp = [];
|
|
2121
|
-
_state.label = 9;
|
|
2122
|
-
case 9:
|
|
2123
|
-
resumeEvents = _tmp;
|
|
2124
|
-
// Verify WEBHOOK_RESPONSE event is present
|
|
2125
|
-
hasWebhookResponse = resumeEvents.some(function(e) {
|
|
2126
|
-
return e.type === BRAIN_EVENTS.WEBHOOK_RESPONSE;
|
|
2127
|
-
});
|
|
2128
|
-
if (!hasWebhookResponse) {
|
|
2129
|
-
console.error('Missing WEBHOOK_RESPONSE event after resume');
|
|
2130
|
-
return [
|
|
2131
|
-
2,
|
|
2132
|
-
false
|
|
2133
|
-
];
|
|
2134
|
-
}
|
|
2135
|
-
// Verify AGENT_TOOL_RESULT event is present (with the webhook response as result)
|
|
2136
|
-
hasAgentToolResult = resumeEvents.some(function(e) {
|
|
2137
|
-
return e.type === BRAIN_EVENTS.AGENT_TOOL_RESULT;
|
|
2138
|
-
});
|
|
2139
|
-
if (!hasAgentToolResult) {
|
|
2140
|
-
console.error('Missing AGENT_TOOL_RESULT event after resume');
|
|
2141
|
-
return [
|
|
2142
|
-
2,
|
|
2143
|
-
false
|
|
2144
|
-
];
|
|
2145
|
-
}
|
|
2146
|
-
// Verify brain completed successfully
|
|
2147
|
-
completeEvent = resumeEvents.find(function(e) {
|
|
2148
|
-
return e.type === BRAIN_EVENTS.COMPLETE;
|
|
2149
|
-
});
|
|
2150
|
-
if (!completeEvent) {
|
|
2151
|
-
console.error('Brain did not complete after resume');
|
|
2152
|
-
return [
|
|
2153
|
-
2,
|
|
2154
|
-
false
|
|
2155
|
-
];
|
|
2156
|
-
}
|
|
2157
|
-
if (completeEvent.status !== STATUS.COMPLETE) {
|
|
2158
|
-
console.error("Expected COMPLETE status, got ".concat(completeEvent.status));
|
|
2159
|
-
return [
|
|
2160
|
-
2,
|
|
2161
|
-
false
|
|
2162
|
-
];
|
|
2163
|
-
}
|
|
2164
|
-
return [
|
|
2165
|
-
2,
|
|
2166
|
-
true
|
|
2167
|
-
];
|
|
2168
|
-
case 10:
|
|
2169
|
-
error = _state.sent();
|
|
2170
|
-
console.error("Failed to test agent webhook resume for ".concat(agentBrainIdentifier, ":"), error);
|
|
2171
|
-
return [
|
|
2172
|
-
2,
|
|
2173
|
-
false
|
|
2174
|
-
];
|
|
2175
|
-
case 11:
|
|
2176
|
-
return [
|
|
2177
|
-
2
|
|
2178
|
-
];
|
|
2179
|
-
}
|
|
2180
|
-
});
|
|
2181
|
-
})();
|
|
2182
|
-
},
|
|
2183
1797
|
innerBrainCompleteDoesNotAffectOuterStatus: /**
|
|
2184
1798
|
* Test that inner brain COMPLETE events don't overwrite outer brain status.
|
|
2185
1799
|
*
|