Skip to content

Commit 1566eef

Browse files
committed
Merge branch 'release/3.0.2'
2 parents 14856ff + 286315d commit 1566eef

File tree

4 files changed

+37
-24
lines changed

4 files changed

+37
-24
lines changed

LangChainDemo/LangChainDemo/AgentExecutor.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ public func runAgent( input: String, llm: LLM, tools: [BaseTool], callbacks: [Ba
211211
return [ "intermediate_steps" : (action, result) ]
212212
}
213213

214-
try workflow.setEntryPoint("call_start")
215-
workflow.setFinishPoint("call_end")
214+
try workflow.addEdge(sourceId: START, targetId: "call_start")
215+
try workflow.addEdge(sourceId: "call_end", targetId: END)
216216

217217
try workflow.addEdge(sourceId: "call_start", targetId: "call_agent")
218218
try workflow.addEdge(sourceId: "call_action", targetId: "call_agent")

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ In the [LangChainDemo](LangChainDemo) project, you can find the porting of [Agen
156156
return [ "intermediate_steps" : (action, result) ]
157157
}
158158

159-
try workflow.setEntryPoint("call_agent")
160-
159+
try workflow.addEdge(sourceId: START, targetId: "call_agent")
160+
161161
try workflow.addConditionalEdge( sourceId: "call_agent", condition: { state in
162162

163163
guard let agentOutcome = state.agentOutcome else {

Sources/LangGraph/LangGraph.swift

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ public enum CompiledGraphError : Error, LocalizedError {
186186
}
187187
}
188188

189+
public let START = "__START__" // id of the edge staring workflow
189190
public let END = "__END__" // id of the edge ending workflow
190191

191192
//enum Either<Left, Right> {
@@ -419,7 +420,7 @@ public class StateGraph<State: AgentState> {
419420

420421
}
421422

422-
public func addNode( _ id: String, action: @escaping NodeAction<State> ) throws {
423+
public func addNode( _ id: String, action: @escaping NodeAction<State> ) throws {
423424
guard id != END else {
424425
throw StateGraphError.invalidNodeIdentifier( "END is not a valid node id!")
425426
}
@@ -434,6 +435,13 @@ public class StateGraph<State: AgentState> {
434435
guard sourceId != END else {
435436
throw StateGraphError.invalidEdgeIdentifier( "END is not a valid edge sourceId!")
436437
}
438+
guard sourceId != START else {
439+
if targetId == END {
440+
throw StateGraphError.invalidNodeIdentifier( "END is not a valid node entry point!")
441+
}
442+
entryPoint = EdgeValue.id(targetId)
443+
return
444+
}
437445

438446
let edge = Edge(sourceId: sourceId, target: .id(targetId) )
439447
if edges.contains(edge) {
@@ -448,25 +456,30 @@ public class StateGraph<State: AgentState> {
448456
if edgeMapping.isEmpty {
449457
throw StateGraphError.edgeMappingIsEmpty
450458
}
459+
guard sourceId != START else {
460+
entryPoint = EdgeValue.condition((condition, edgeMapping))
461+
return
462+
}
451463

452464
let edge = Edge(sourceId: sourceId, target: .condition(( condition, edgeMapping)) )
453465
if edges.contains(edge) {
454466
throw StateGraphError.duplicateEdgeError("edge with id:\(sourceId) already exist!")
455467
}
456468
edges.insert( edge)
469+
return
457470
}
471+
472+
@available(*, deprecated, message: "This method is deprecated. Use `addEdge( START, nodeId )` instead.")
458473
public func setEntryPoint( _ nodeId: String ) throws {
459-
guard nodeId != END else {
460-
throw StateGraphError.invalidNodeIdentifier( "END is not a valid node entry point!")
461-
}
462-
entryPoint = EdgeValue.id(nodeId)
474+
let _ = try addEdge( sourceId: START, targetId: nodeId )
463475
}
476+
477+
@available(*, deprecated, message: "This method is deprecated. Use `addConditionalEdge( START, condition, edgeMappings )` instead.")
464478
public func setConditionalEntryPoint( condition: @escaping EdgeCondition<State>, edgeMapping: [String:String] ) throws {
465-
if edgeMapping.isEmpty {
466-
throw StateGraphError.edgeMappingIsEmpty
467-
}
468-
entryPoint = EdgeValue.condition((condition, edgeMapping))
479+
let _ = try self.addConditionalEdge(sourceId: START, condition: condition, edgeMapping: edgeMapping )
469480
}
481+
482+
@available(*, deprecated, message: "This method is deprecated. Use `addEdge( nodeId, END )` instead.")
470483
public func setFinishPoint( _ nodeId: String ) {
471484
finishPoint = nodeId
472485
}

Tests/LangGraphTests/LangGraphTests.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ final class LangGraphTests: XCTestCase {
4747
XCTAssertTrue(error is StateGraphError, "\(error) is not a GraphStateError")
4848
}
4949

50-
try workflow.setEntryPoint("agent_1")
50+
try workflow.addEdge(sourceId: START, targetId: "agent_1")
5151

5252
XCTAssertThrowsError( try workflow.compile() ) {error in
5353
print( error )
@@ -121,7 +121,7 @@ final class LangGraphTests: XCTestCase {
121121
func testRunningOneNode() async throws {
122122

123123
let workflow = StateGraph { BaseAgentState($0) }
124-
try workflow.setEntryPoint("agent_1")
124+
try workflow.addEdge( sourceId: START, targetId: "agent_1")
125125
try workflow.addNode("agent_1") { state in
126126

127127
print( "agent_1", state )
@@ -188,8 +188,8 @@ final class LangGraphTests: XCTestCase {
188188
try workflow.addEdge(sourceId: "agent_1", targetId: "agent_2")
189189
try workflow.addEdge(sourceId: "agent_2", targetId: "sum")
190190

191-
try workflow.setEntryPoint("agent_1")
192-
workflow.setFinishPoint("sum")
191+
try workflow.addEdge( sourceId: START, targetId: "agent_1")
192+
try workflow.addEdge(sourceId: "sum", targetId: END )
193193

194194
let app = try workflow.compile()
195195

@@ -255,7 +255,7 @@ final class LangGraphTests: XCTestCase {
255255
try workflow.addEdge(sourceId: "sum", targetId: END)
256256
try workflow.addEdge(sourceId: "mul", targetId: END)
257257

258-
try workflow.setEntryPoint("agent_1")
258+
try workflow.addEdge(sourceId: START, targetId: "agent_1")
259259

260260
let app = try workflow.compile()
261261

@@ -306,8 +306,8 @@ final class LangGraphTests: XCTestCase {
306306
try workflow.addEdge(sourceId: "agent_1", targetId: "agent_2")
307307
try workflow.addEdge(sourceId: "agent_2", targetId: "agent_3")
308308

309-
try workflow.setEntryPoint("agent_1")
310-
workflow.setFinishPoint("agent_3")
309+
try workflow.addEdge(sourceId: START, targetId: "agent_1")
310+
try workflow.addEdge(sourceId: "agent_3", targetId: END)
311311

312312
let app = try workflow.compile()
313313

@@ -335,8 +335,8 @@ final class LangGraphTests: XCTestCase {
335335
try workflow.addEdge(sourceId: "agent_1", targetId: "agent_2")
336336
try workflow.addEdge(sourceId: "agent_2", targetId: "agent_3")
337337

338-
try workflow.setEntryPoint("agent_1")
339-
workflow.setFinishPoint("agent_3")
338+
try workflow.addEdge(sourceId: START, targetId: "agent_1")
339+
try workflow.addEdge(sourceId: "agent_3", targetId: END)
340340

341341
let app = try workflow.compile()
342342

@@ -374,8 +374,8 @@ final class LangGraphTests: XCTestCase {
374374
try workflow.addEdge(sourceId: "agent_1", targetId: "agent_2")
375375
try workflow.addEdge(sourceId: "agent_2", targetId: "agent_3")
376376

377-
try workflow.setEntryPoint("agent_1")
378-
workflow.setFinishPoint("agent_3")
377+
try workflow.addEdge(sourceId: START, targetId: "agent_1")
378+
try workflow.addEdge(sourceId: "agent_3", targetId: END)
379379

380380
let app = try workflow.compile()
381381

0 commit comments

Comments
 (0)